binutils/
[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
1075 they are not currently supported: .asm0, .endr, .lab, .repeat,
1076 .struct. */
1077
e972090a
NC
1078static const pseudo_typeS mips_pseudo_table[] =
1079{
beae10d5 1080 /* MIPS specific pseudo-ops. */
252b5132
RH
1081 {"option", s_option, 0},
1082 {"set", s_mipsset, 0},
1083 {"rdata", s_change_sec, 'r'},
1084 {"sdata", s_change_sec, 's'},
1085 {"livereg", s_ignore, 0},
1086 {"abicalls", s_abicalls, 0},
1087 {"cpload", s_cpload, 0},
6478892d
TS
1088 {"cpsetup", s_cpsetup, 0},
1089 {"cplocal", s_cplocal, 0},
252b5132 1090 {"cprestore", s_cprestore, 0},
6478892d
TS
1091 {"cpreturn", s_cpreturn, 0},
1092 {"gpvalue", s_gpvalue, 0},
252b5132 1093 {"gpword", s_gpword, 0},
10181a0d 1094 {"gpdword", s_gpdword, 0},
252b5132
RH
1095 {"cpadd", s_cpadd, 0},
1096 {"insn", s_insn, 0},
1097
beae10d5 1098 /* Relatively generic pseudo-ops that happen to be used on MIPS
252b5132
RH
1099 chips. */
1100 {"asciiz", stringer, 1},
1101 {"bss", s_change_sec, 'b'},
1102 {"err", s_err, 0},
1103 {"half", s_cons, 1},
1104 {"dword", s_cons, 3},
1105 {"weakext", s_mips_weakext, 0},
7c752c2a
TS
1106 {"origin", s_org, 0},
1107 {"repeat", s_rept, 0},
252b5132 1108
beae10d5 1109 /* These pseudo-ops are defined in read.c, but must be overridden
252b5132
RH
1110 here for one reason or another. */
1111 {"align", s_align, 0},
1112 {"byte", s_cons, 0},
1113 {"data", s_change_sec, 'd'},
1114 {"double", s_float_cons, 'd'},
1115 {"float", s_float_cons, 'f'},
1116 {"globl", s_mips_globl, 0},
1117 {"global", s_mips_globl, 0},
1118 {"hword", s_cons, 1},
1119 {"int", s_cons, 2},
1120 {"long", s_cons, 2},
1121 {"octa", s_cons, 4},
1122 {"quad", s_cons, 3},
cca86cc8 1123 {"section", s_change_section, 0},
252b5132
RH
1124 {"short", s_cons, 1},
1125 {"single", s_float_cons, 'f'},
1126 {"stabn", s_mips_stab, 'n'},
1127 {"text", s_change_sec, 't'},
1128 {"word", s_cons, 2},
add56521 1129
add56521 1130 { "extern", ecoff_directive_extern, 0},
add56521 1131
43841e91 1132 { NULL, NULL, 0 },
252b5132
RH
1133};
1134
e972090a
NC
1135static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1136{
beae10d5
KH
1137 /* These pseudo-ops should be defined by the object file format.
1138 However, a.out doesn't support them, so we have versions here. */
252b5132
RH
1139 {"aent", s_mips_ent, 1},
1140 {"bgnb", s_ignore, 0},
1141 {"end", s_mips_end, 0},
1142 {"endb", s_ignore, 0},
1143 {"ent", s_mips_ent, 0},
c5dd6aab 1144 {"file", s_mips_file, 0},
252b5132
RH
1145 {"fmask", s_mips_mask, 'F'},
1146 {"frame", s_mips_frame, 0},
c5dd6aab 1147 {"loc", s_mips_loc, 0},
252b5132
RH
1148 {"mask", s_mips_mask, 'R'},
1149 {"verstamp", s_ignore, 0},
43841e91 1150 { NULL, NULL, 0 },
252b5132
RH
1151};
1152
17a2f251 1153extern void pop_insert (const pseudo_typeS *);
252b5132
RH
1154
1155void
17a2f251 1156mips_pop_insert (void)
252b5132
RH
1157{
1158 pop_insert (mips_pseudo_table);
1159 if (! ECOFF_DEBUGGING)
1160 pop_insert (mips_nonecoff_pseudo_table);
1161}
1162\f
1163/* Symbols labelling the current insn. */
1164
e972090a
NC
1165struct insn_label_list
1166{
252b5132
RH
1167 struct insn_label_list *next;
1168 symbolS *label;
1169};
1170
1171static struct insn_label_list *insn_labels;
1172static struct insn_label_list *free_insn_labels;
1173
17a2f251 1174static void mips_clear_insn_labels (void);
252b5132
RH
1175
1176static inline void
17a2f251 1177mips_clear_insn_labels (void)
252b5132
RH
1178{
1179 register struct insn_label_list **pl;
1180
1181 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1182 ;
1183 *pl = insn_labels;
1184 insn_labels = NULL;
1185}
1186\f
1187static char *expr_end;
1188
1189/* Expressions which appear in instructions. These are set by
1190 mips_ip. */
1191
1192static expressionS imm_expr;
5f74bc13 1193static expressionS imm2_expr;
252b5132
RH
1194static expressionS offset_expr;
1195
1196/* Relocs associated with imm_expr and offset_expr. */
1197
f6688943
TS
1198static bfd_reloc_code_real_type imm_reloc[3]
1199 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1200static bfd_reloc_code_real_type offset_reloc[3]
1201 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 1202
252b5132
RH
1203/* These are set by mips16_ip if an explicit extension is used. */
1204
b34976b6 1205static bfd_boolean mips16_small, mips16_ext;
252b5132 1206
7ed4a06a 1207#ifdef OBJ_ELF
ecb4347a
DJ
1208/* The pdr segment for per procedure frame/regmask info. Not used for
1209 ECOFF debugging. */
252b5132
RH
1210
1211static segT pdr_seg;
7ed4a06a 1212#endif
252b5132 1213
e013f690
TS
1214/* The default target format to use. */
1215
1216const char *
17a2f251 1217mips_target_format (void)
e013f690
TS
1218{
1219 switch (OUTPUT_FLAVOR)
1220 {
e013f690
TS
1221 case bfd_target_ecoff_flavour:
1222 return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
1223 case bfd_target_coff_flavour:
1224 return "pe-mips";
1225 case bfd_target_elf_flavour:
0a44bf69
RS
1226#ifdef TE_VXWORKS
1227 if (!HAVE_64BIT_OBJECTS && !HAVE_NEWABI)
1228 return (target_big_endian
1229 ? "elf32-bigmips-vxworks"
1230 : "elf32-littlemips-vxworks");
1231#endif
e013f690 1232#ifdef TE_TMIPS
cfe86eaa 1233 /* This is traditional mips. */
e013f690 1234 return (target_big_endian
cfe86eaa
TS
1235 ? (HAVE_64BIT_OBJECTS
1236 ? "elf64-tradbigmips"
1237 : (HAVE_NEWABI
1238 ? "elf32-ntradbigmips" : "elf32-tradbigmips"))
1239 : (HAVE_64BIT_OBJECTS
1240 ? "elf64-tradlittlemips"
1241 : (HAVE_NEWABI
1242 ? "elf32-ntradlittlemips" : "elf32-tradlittlemips")));
e013f690
TS
1243#else
1244 return (target_big_endian
cfe86eaa
TS
1245 ? (HAVE_64BIT_OBJECTS
1246 ? "elf64-bigmips"
1247 : (HAVE_NEWABI
1248 ? "elf32-nbigmips" : "elf32-bigmips"))
1249 : (HAVE_64BIT_OBJECTS
1250 ? "elf64-littlemips"
1251 : (HAVE_NEWABI
1252 ? "elf32-nlittlemips" : "elf32-littlemips")));
e013f690
TS
1253#endif
1254 default:
1255 abort ();
1256 return NULL;
1257 }
1258}
1259
1e915849
RS
1260/* Return the length of instruction INSN. */
1261
1262static inline unsigned int
1263insn_length (const struct mips_cl_insn *insn)
1264{
1265 if (!mips_opts.mips16)
1266 return 4;
1267 return insn->mips16_absolute_jump_p || insn->use_extend ? 4 : 2;
1268}
1269
1270/* Initialise INSN from opcode entry MO. Leave its position unspecified. */
1271
1272static void
1273create_insn (struct mips_cl_insn *insn, const struct mips_opcode *mo)
1274{
1275 size_t i;
1276
1277 insn->insn_mo = mo;
1278 insn->use_extend = FALSE;
1279 insn->extend = 0;
1280 insn->insn_opcode = mo->match;
1281 insn->frag = NULL;
1282 insn->where = 0;
1283 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
1284 insn->fixp[i] = NULL;
1285 insn->fixed_p = (mips_opts.noreorder > 0);
1286 insn->noreorder_p = (mips_opts.noreorder > 0);
1287 insn->mips16_absolute_jump_p = 0;
1288}
1289
1290/* Install INSN at the location specified by its "frag" and "where" fields. */
1291
1292static void
1293install_insn (const struct mips_cl_insn *insn)
1294{
1295 char *f = insn->frag->fr_literal + insn->where;
1296 if (!mips_opts.mips16)
1297 md_number_to_chars (f, insn->insn_opcode, 4);
1298 else if (insn->mips16_absolute_jump_p)
1299 {
1300 md_number_to_chars (f, insn->insn_opcode >> 16, 2);
1301 md_number_to_chars (f + 2, insn->insn_opcode & 0xffff, 2);
1302 }
1303 else
1304 {
1305 if (insn->use_extend)
1306 {
1307 md_number_to_chars (f, 0xf000 | insn->extend, 2);
1308 f += 2;
1309 }
1310 md_number_to_chars (f, insn->insn_opcode, 2);
1311 }
1312}
1313
1314/* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly
1315 and install the opcode in the new location. */
1316
1317static void
1318move_insn (struct mips_cl_insn *insn, fragS *frag, long where)
1319{
1320 size_t i;
1321
1322 insn->frag = frag;
1323 insn->where = where;
1324 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
1325 if (insn->fixp[i] != NULL)
1326 {
1327 insn->fixp[i]->fx_frag = frag;
1328 insn->fixp[i]->fx_where = where;
1329 }
1330 install_insn (insn);
1331}
1332
1333/* Add INSN to the end of the output. */
1334
1335static void
1336add_fixed_insn (struct mips_cl_insn *insn)
1337{
1338 char *f = frag_more (insn_length (insn));
1339 move_insn (insn, frag_now, f - frag_now->fr_literal);
1340}
1341
1342/* Start a variant frag and move INSN to the start of the variant part,
1343 marking it as fixed. The other arguments are as for frag_var. */
1344
1345static void
1346add_relaxed_insn (struct mips_cl_insn *insn, int max_chars, int var,
1347 relax_substateT subtype, symbolS *symbol, offsetT offset)
1348{
1349 frag_grow (max_chars);
1350 move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
1351 insn->fixed_p = 1;
1352 frag_var (rs_machine_dependent, max_chars, var,
1353 subtype, symbol, offset, NULL);
1354}
1355
1356/* Insert N copies of INSN into the history buffer, starting at
1357 position FIRST. Neither FIRST nor N need to be clipped. */
1358
1359static void
1360insert_into_history (unsigned int first, unsigned int n,
1361 const struct mips_cl_insn *insn)
1362{
1363 if (mips_relax.sequence != 2)
1364 {
1365 unsigned int i;
1366
1367 for (i = ARRAY_SIZE (history); i-- > first;)
1368 if (i >= first + n)
1369 history[i] = history[i - n];
1370 else
1371 history[i] = *insn;
1372 }
1373}
1374
1375/* Emit a nop instruction, recording it in the history buffer. */
1376
1377static void
1378emit_nop (void)
1379{
1380 add_fixed_insn (NOP_INSN);
1381 insert_into_history (0, 1, NOP_INSN);
1382}
1383
71400594
RS
1384/* Initialize vr4120_conflicts. There is a bit of duplication here:
1385 the idea is to make it obvious at a glance that each errata is
1386 included. */
1387
1388static void
1389init_vr4120_conflicts (void)
1390{
1391#define CONFLICT(FIRST, SECOND) \
1392 vr4120_conflicts[FIX_VR4120_##FIRST] |= 1 << FIX_VR4120_##SECOND
1393
1394 /* Errata 21 - [D]DIV[U] after [D]MACC */
1395 CONFLICT (MACC, DIV);
1396 CONFLICT (DMACC, DIV);
1397
1398 /* Errata 23 - Continuous DMULT[U]/DMACC instructions. */
1399 CONFLICT (DMULT, DMULT);
1400 CONFLICT (DMULT, DMACC);
1401 CONFLICT (DMACC, DMULT);
1402 CONFLICT (DMACC, DMACC);
1403
1404 /* Errata 24 - MT{LO,HI} after [D]MACC */
1405 CONFLICT (MACC, MTHILO);
1406 CONFLICT (DMACC, MTHILO);
1407
1408 /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
1409 instruction is executed immediately after a MACC or DMACC
1410 instruction, the result of [either instruction] is incorrect." */
1411 CONFLICT (MACC, MULT);
1412 CONFLICT (MACC, DMULT);
1413 CONFLICT (DMACC, MULT);
1414 CONFLICT (DMACC, DMULT);
1415
1416 /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
1417 executed immediately after a DMULT, DMULTU, DIV, DIVU,
1418 DDIV or DDIVU instruction, the result of the MACC or
1419 DMACC instruction is incorrect.". */
1420 CONFLICT (DMULT, MACC);
1421 CONFLICT (DMULT, DMACC);
1422 CONFLICT (DIV, MACC);
1423 CONFLICT (DIV, DMACC);
1424
1425#undef CONFLICT
1426}
1427
707bfff6
TS
1428struct regname {
1429 const char *name;
1430 unsigned int num;
1431};
1432
1433#define RTYPE_MASK 0x1ff00
1434#define RTYPE_NUM 0x00100
1435#define RTYPE_FPU 0x00200
1436#define RTYPE_FCC 0x00400
1437#define RTYPE_VEC 0x00800
1438#define RTYPE_GP 0x01000
1439#define RTYPE_CP0 0x02000
1440#define RTYPE_PC 0x04000
1441#define RTYPE_ACC 0x08000
1442#define RTYPE_CCC 0x10000
1443#define RNUM_MASK 0x000ff
1444#define RWARN 0x80000
1445
1446#define GENERIC_REGISTER_NUMBERS \
1447 {"$0", RTYPE_NUM | 0}, \
1448 {"$1", RTYPE_NUM | 1}, \
1449 {"$2", RTYPE_NUM | 2}, \
1450 {"$3", RTYPE_NUM | 3}, \
1451 {"$4", RTYPE_NUM | 4}, \
1452 {"$5", RTYPE_NUM | 5}, \
1453 {"$6", RTYPE_NUM | 6}, \
1454 {"$7", RTYPE_NUM | 7}, \
1455 {"$8", RTYPE_NUM | 8}, \
1456 {"$9", RTYPE_NUM | 9}, \
1457 {"$10", RTYPE_NUM | 10}, \
1458 {"$11", RTYPE_NUM | 11}, \
1459 {"$12", RTYPE_NUM | 12}, \
1460 {"$13", RTYPE_NUM | 13}, \
1461 {"$14", RTYPE_NUM | 14}, \
1462 {"$15", RTYPE_NUM | 15}, \
1463 {"$16", RTYPE_NUM | 16}, \
1464 {"$17", RTYPE_NUM | 17}, \
1465 {"$18", RTYPE_NUM | 18}, \
1466 {"$19", RTYPE_NUM | 19}, \
1467 {"$20", RTYPE_NUM | 20}, \
1468 {"$21", RTYPE_NUM | 21}, \
1469 {"$22", RTYPE_NUM | 22}, \
1470 {"$23", RTYPE_NUM | 23}, \
1471 {"$24", RTYPE_NUM | 24}, \
1472 {"$25", RTYPE_NUM | 25}, \
1473 {"$26", RTYPE_NUM | 26}, \
1474 {"$27", RTYPE_NUM | 27}, \
1475 {"$28", RTYPE_NUM | 28}, \
1476 {"$29", RTYPE_NUM | 29}, \
1477 {"$30", RTYPE_NUM | 30}, \
1478 {"$31", RTYPE_NUM | 31}
1479
1480#define FPU_REGISTER_NAMES \
1481 {"$f0", RTYPE_FPU | 0}, \
1482 {"$f1", RTYPE_FPU | 1}, \
1483 {"$f2", RTYPE_FPU | 2}, \
1484 {"$f3", RTYPE_FPU | 3}, \
1485 {"$f4", RTYPE_FPU | 4}, \
1486 {"$f5", RTYPE_FPU | 5}, \
1487 {"$f6", RTYPE_FPU | 6}, \
1488 {"$f7", RTYPE_FPU | 7}, \
1489 {"$f8", RTYPE_FPU | 8}, \
1490 {"$f9", RTYPE_FPU | 9}, \
1491 {"$f10", RTYPE_FPU | 10}, \
1492 {"$f11", RTYPE_FPU | 11}, \
1493 {"$f12", RTYPE_FPU | 12}, \
1494 {"$f13", RTYPE_FPU | 13}, \
1495 {"$f14", RTYPE_FPU | 14}, \
1496 {"$f15", RTYPE_FPU | 15}, \
1497 {"$f16", RTYPE_FPU | 16}, \
1498 {"$f17", RTYPE_FPU | 17}, \
1499 {"$f18", RTYPE_FPU | 18}, \
1500 {"$f19", RTYPE_FPU | 19}, \
1501 {"$f20", RTYPE_FPU | 20}, \
1502 {"$f21", RTYPE_FPU | 21}, \
1503 {"$f22", RTYPE_FPU | 22}, \
1504 {"$f23", RTYPE_FPU | 23}, \
1505 {"$f24", RTYPE_FPU | 24}, \
1506 {"$f25", RTYPE_FPU | 25}, \
1507 {"$f26", RTYPE_FPU | 26}, \
1508 {"$f27", RTYPE_FPU | 27}, \
1509 {"$f28", RTYPE_FPU | 28}, \
1510 {"$f29", RTYPE_FPU | 29}, \
1511 {"$f30", RTYPE_FPU | 30}, \
1512 {"$f31", RTYPE_FPU | 31}
1513
1514#define FPU_CONDITION_CODE_NAMES \
1515 {"$fcc0", RTYPE_FCC | 0}, \
1516 {"$fcc1", RTYPE_FCC | 1}, \
1517 {"$fcc2", RTYPE_FCC | 2}, \
1518 {"$fcc3", RTYPE_FCC | 3}, \
1519 {"$fcc4", RTYPE_FCC | 4}, \
1520 {"$fcc5", RTYPE_FCC | 5}, \
1521 {"$fcc6", RTYPE_FCC | 6}, \
1522 {"$fcc7", RTYPE_FCC | 7}
1523
1524#define COPROC_CONDITION_CODE_NAMES \
1525 {"$cc0", RTYPE_FCC | RTYPE_CCC | 0}, \
1526 {"$cc1", RTYPE_FCC | RTYPE_CCC | 1}, \
1527 {"$cc2", RTYPE_FCC | RTYPE_CCC | 2}, \
1528 {"$cc3", RTYPE_FCC | RTYPE_CCC | 3}, \
1529 {"$cc4", RTYPE_FCC | RTYPE_CCC | 4}, \
1530 {"$cc5", RTYPE_FCC | RTYPE_CCC | 5}, \
1531 {"$cc6", RTYPE_FCC | RTYPE_CCC | 6}, \
1532 {"$cc7", RTYPE_FCC | RTYPE_CCC | 7}
1533
1534#define N32N64_SYMBOLIC_REGISTER_NAMES \
1535 {"$a4", RTYPE_GP | 8}, \
1536 {"$a5", RTYPE_GP | 9}, \
1537 {"$a6", RTYPE_GP | 10}, \
1538 {"$a7", RTYPE_GP | 11}, \
1539 {"$ta0", RTYPE_GP | 8}, /* alias for $a4 */ \
1540 {"$ta1", RTYPE_GP | 9}, /* alias for $a5 */ \
1541 {"$ta2", RTYPE_GP | 10}, /* alias for $a6 */ \
1542 {"$ta3", RTYPE_GP | 11}, /* alias for $a7 */ \
1543 {"$t0", RTYPE_GP | 12}, \
1544 {"$t1", RTYPE_GP | 13}, \
1545 {"$t2", RTYPE_GP | 14}, \
1546 {"$t3", RTYPE_GP | 15}
1547
1548#define O32_SYMBOLIC_REGISTER_NAMES \
1549 {"$t0", RTYPE_GP | 8}, \
1550 {"$t1", RTYPE_GP | 9}, \
1551 {"$t2", RTYPE_GP | 10}, \
1552 {"$t3", RTYPE_GP | 11}, \
1553 {"$t4", RTYPE_GP | 12}, \
1554 {"$t5", RTYPE_GP | 13}, \
1555 {"$t6", RTYPE_GP | 14}, \
1556 {"$t7", RTYPE_GP | 15}, \
1557 {"$ta0", RTYPE_GP | 12}, /* alias for $t4 */ \
1558 {"$ta1", RTYPE_GP | 13}, /* alias for $t5 */ \
1559 {"$ta2", RTYPE_GP | 14}, /* alias for $t6 */ \
1560 {"$ta3", RTYPE_GP | 15} /* alias for $t7 */
1561
1562/* Remaining symbolic register names */
1563#define SYMBOLIC_REGISTER_NAMES \
1564 {"$zero", RTYPE_GP | 0}, \
1565 {"$at", RTYPE_GP | 1}, \
1566 {"$AT", RTYPE_GP | 1}, \
1567 {"$v0", RTYPE_GP | 2}, \
1568 {"$v1", RTYPE_GP | 3}, \
1569 {"$a0", RTYPE_GP | 4}, \
1570 {"$a1", RTYPE_GP | 5}, \
1571 {"$a2", RTYPE_GP | 6}, \
1572 {"$a3", RTYPE_GP | 7}, \
1573 {"$s0", RTYPE_GP | 16}, \
1574 {"$s1", RTYPE_GP | 17}, \
1575 {"$s2", RTYPE_GP | 18}, \
1576 {"$s3", RTYPE_GP | 19}, \
1577 {"$s4", RTYPE_GP | 20}, \
1578 {"$s5", RTYPE_GP | 21}, \
1579 {"$s6", RTYPE_GP | 22}, \
1580 {"$s7", RTYPE_GP | 23}, \
1581 {"$t8", RTYPE_GP | 24}, \
1582 {"$t9", RTYPE_GP | 25}, \
1583 {"$k0", RTYPE_GP | 26}, \
1584 {"$kt0", RTYPE_GP | 26}, \
1585 {"$k1", RTYPE_GP | 27}, \
1586 {"$kt1", RTYPE_GP | 27}, \
1587 {"$gp", RTYPE_GP | 28}, \
1588 {"$sp", RTYPE_GP | 29}, \
1589 {"$s8", RTYPE_GP | 30}, \
1590 {"$fp", RTYPE_GP | 30}, \
1591 {"$ra", RTYPE_GP | 31}
1592
1593#define MIPS16_SPECIAL_REGISTER_NAMES \
1594 {"$pc", RTYPE_PC | 0}
1595
1596#define MDMX_VECTOR_REGISTER_NAMES \
1597 /* {"$v0", RTYPE_VEC | 0}, clash with REG 2 above */ \
1598 /* {"$v1", RTYPE_VEC | 1}, clash with REG 3 above */ \
1599 {"$v2", RTYPE_VEC | 2}, \
1600 {"$v3", RTYPE_VEC | 3}, \
1601 {"$v4", RTYPE_VEC | 4}, \
1602 {"$v5", RTYPE_VEC | 5}, \
1603 {"$v6", RTYPE_VEC | 6}, \
1604 {"$v7", RTYPE_VEC | 7}, \
1605 {"$v8", RTYPE_VEC | 8}, \
1606 {"$v9", RTYPE_VEC | 9}, \
1607 {"$v10", RTYPE_VEC | 10}, \
1608 {"$v11", RTYPE_VEC | 11}, \
1609 {"$v12", RTYPE_VEC | 12}, \
1610 {"$v13", RTYPE_VEC | 13}, \
1611 {"$v14", RTYPE_VEC | 14}, \
1612 {"$v15", RTYPE_VEC | 15}, \
1613 {"$v16", RTYPE_VEC | 16}, \
1614 {"$v17", RTYPE_VEC | 17}, \
1615 {"$v18", RTYPE_VEC | 18}, \
1616 {"$v19", RTYPE_VEC | 19}, \
1617 {"$v20", RTYPE_VEC | 20}, \
1618 {"$v21", RTYPE_VEC | 21}, \
1619 {"$v22", RTYPE_VEC | 22}, \
1620 {"$v23", RTYPE_VEC | 23}, \
1621 {"$v24", RTYPE_VEC | 24}, \
1622 {"$v25", RTYPE_VEC | 25}, \
1623 {"$v26", RTYPE_VEC | 26}, \
1624 {"$v27", RTYPE_VEC | 27}, \
1625 {"$v28", RTYPE_VEC | 28}, \
1626 {"$v29", RTYPE_VEC | 29}, \
1627 {"$v30", RTYPE_VEC | 30}, \
1628 {"$v31", RTYPE_VEC | 31}
1629
1630#define MIPS_DSP_ACCUMULATOR_NAMES \
1631 {"$ac0", RTYPE_ACC | 0}, \
1632 {"$ac1", RTYPE_ACC | 1}, \
1633 {"$ac2", RTYPE_ACC | 2}, \
1634 {"$ac3", RTYPE_ACC | 3}
1635
1636static const struct regname reg_names[] = {
1637 GENERIC_REGISTER_NUMBERS,
1638 FPU_REGISTER_NAMES,
1639 FPU_CONDITION_CODE_NAMES,
1640 COPROC_CONDITION_CODE_NAMES,
1641
1642 /* The $txx registers depends on the abi,
1643 these will be added later into the symbol table from
1644 one of the tables below once mips_abi is set after
1645 parsing of arguments from the command line. */
1646 SYMBOLIC_REGISTER_NAMES,
1647
1648 MIPS16_SPECIAL_REGISTER_NAMES,
1649 MDMX_VECTOR_REGISTER_NAMES,
1650 MIPS_DSP_ACCUMULATOR_NAMES,
1651 {0, 0}
1652};
1653
1654static const struct regname reg_names_o32[] = {
1655 O32_SYMBOLIC_REGISTER_NAMES,
1656 {0, 0}
1657};
1658
1659static const struct regname reg_names_n32n64[] = {
1660 N32N64_SYMBOLIC_REGISTER_NAMES,
1661 {0, 0}
1662};
1663
1664static int
1665reg_lookup (char **s, unsigned int types, unsigned int *regnop)
1666{
1667 symbolS *symbolP;
1668 char *e;
1669 char save_c;
1670 int reg = -1;
1671
1672 /* Find end of name. */
1673 e = *s;
1674 if (is_name_beginner (*e))
1675 ++e;
1676 while (is_part_of_name (*e))
1677 ++e;
1678
1679 /* Terminate name. */
1680 save_c = *e;
1681 *e = '\0';
1682
1683 /* Look for a register symbol. */
1684 if ((symbolP = symbol_find (*s)) && S_GET_SEGMENT (symbolP) == reg_section)
1685 {
1686 int r = S_GET_VALUE (symbolP);
1687 if (r & types)
1688 reg = r & RNUM_MASK;
1689 else if ((types & RTYPE_VEC) && (r & ~1) == (RTYPE_GP | 2))
1690 /* Convert GP reg $v0/1 to MDMX reg $v0/1! */
1691 reg = (r & RNUM_MASK) - 2;
1692 }
1693 /* Else see if this is a register defined in an itbl entry. */
1694 else if ((types & RTYPE_GP) && itbl_have_entries)
1695 {
1696 char *n = *s;
1697 unsigned long r;
1698
1699 if (*n == '$')
1700 ++n;
1701 if (itbl_get_reg_val (n, &r))
1702 reg = r & RNUM_MASK;
1703 }
1704
1705 /* Advance to next token if a register was recognised. */
1706 if (reg >= 0)
1707 *s = e;
1708 else if (types & RWARN)
1709 as_warn ("Unrecognized register name `%s'", *s);
1710
1711 *e = save_c;
1712 if (regnop)
1713 *regnop = reg;
1714 return reg >= 0;
1715}
1716
1717/* This function is called once, at assembler startup time. It should set up
1718 all the tables, etc. that the MD part of the assembler will need. */
156c2f8b 1719
252b5132 1720void
17a2f251 1721md_begin (void)
252b5132 1722{
3994f87e 1723 const char *retval = NULL;
156c2f8b 1724 int i = 0;
252b5132 1725 int broken = 0;
1f25f5d3 1726
0a44bf69
RS
1727 if (mips_pic != NO_PIC)
1728 {
1729 if (g_switch_seen && g_switch_value != 0)
1730 as_bad (_("-G may not be used in position-independent code"));
1731 g_switch_value = 0;
1732 }
1733
fef14a42 1734 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_arch))
252b5132
RH
1735 as_warn (_("Could not set architecture and machine"));
1736
252b5132
RH
1737 op_hash = hash_new ();
1738
1739 for (i = 0; i < NUMOPCODES;)
1740 {
1741 const char *name = mips_opcodes[i].name;
1742
17a2f251 1743 retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
252b5132
RH
1744 if (retval != NULL)
1745 {
1746 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1747 mips_opcodes[i].name, retval);
1748 /* Probably a memory allocation problem? Give up now. */
1749 as_fatal (_("Broken assembler. No assembly attempted."));
1750 }
1751 do
1752 {
1753 if (mips_opcodes[i].pinfo != INSN_MACRO)
1754 {
1755 if (!validate_mips_insn (&mips_opcodes[i]))
1756 broken = 1;
1e915849
RS
1757 if (nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
1758 {
1759 create_insn (&nop_insn, mips_opcodes + i);
1760 nop_insn.fixed_p = 1;
1761 }
252b5132
RH
1762 }
1763 ++i;
1764 }
1765 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1766 }
1767
1768 mips16_op_hash = hash_new ();
1769
1770 i = 0;
1771 while (i < bfd_mips16_num_opcodes)
1772 {
1773 const char *name = mips16_opcodes[i].name;
1774
17a2f251 1775 retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
252b5132
RH
1776 if (retval != NULL)
1777 as_fatal (_("internal: can't hash `%s': %s"),
1778 mips16_opcodes[i].name, retval);
1779 do
1780 {
1781 if (mips16_opcodes[i].pinfo != INSN_MACRO
1782 && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1783 != mips16_opcodes[i].match))
1784 {
1785 fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1786 mips16_opcodes[i].name, mips16_opcodes[i].args);
1787 broken = 1;
1788 }
1e915849
RS
1789 if (mips16_nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
1790 {
1791 create_insn (&mips16_nop_insn, mips16_opcodes + i);
1792 mips16_nop_insn.fixed_p = 1;
1793 }
252b5132
RH
1794 ++i;
1795 }
1796 while (i < bfd_mips16_num_opcodes
1797 && strcmp (mips16_opcodes[i].name, name) == 0);
1798 }
1799
1800 if (broken)
1801 as_fatal (_("Broken assembler. No assembly attempted."));
1802
1803 /* We add all the general register names to the symbol table. This
1804 helps us detect invalid uses of them. */
707bfff6
TS
1805 for (i = 0; reg_names[i].name; i++)
1806 symbol_table_insert (symbol_new (reg_names[i].name, reg_section,
1807 reg_names[i].num, // & RNUM_MASK,
1808 &zero_address_frag));
1809 if (HAVE_NEWABI)
1810 for (i = 0; reg_names_n32n64[i].name; i++)
1811 symbol_table_insert (symbol_new (reg_names_n32n64[i].name, reg_section,
1812 reg_names_n32n64[i].num, // & RNUM_MASK,
252b5132 1813 &zero_address_frag));
707bfff6
TS
1814 else
1815 for (i = 0; reg_names_o32[i].name; i++)
1816 symbol_table_insert (symbol_new (reg_names_o32[i].name, reg_section,
1817 reg_names_o32[i].num, // & RNUM_MASK,
6047c971 1818 &zero_address_frag));
6047c971 1819
7d10b47d 1820 mips_no_prev_insn ();
252b5132
RH
1821
1822 mips_gprmask = 0;
1823 mips_cprmask[0] = 0;
1824 mips_cprmask[1] = 0;
1825 mips_cprmask[2] = 0;
1826 mips_cprmask[3] = 0;
1827
1828 /* set the default alignment for the text section (2**2) */
1829 record_alignment (text_section, 2);
1830
4d0d148d 1831 bfd_set_gp_size (stdoutput, g_switch_value);
252b5132 1832
707bfff6 1833#ifdef OBJ_ELF
252b5132
RH
1834 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1835 {
0a44bf69
RS
1836 /* On a native system other than VxWorks, sections must be aligned
1837 to 16 byte boundaries. When configured for an embedded ELF
1838 target, we don't bother. */
1839 if (strcmp (TARGET_OS, "elf") != 0
1840 && strcmp (TARGET_OS, "vxworks") != 0)
252b5132
RH
1841 {
1842 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1843 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1844 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1845 }
1846
1847 /* Create a .reginfo section for register masks and a .mdebug
1848 section for debugging information. */
1849 {
1850 segT seg;
1851 subsegT subseg;
1852 flagword flags;
1853 segT sec;
1854
1855 seg = now_seg;
1856 subseg = now_subseg;
1857
1858 /* The ABI says this section should be loaded so that the
1859 running program can access it. However, we don't load it
1860 if we are configured for an embedded target */
1861 flags = SEC_READONLY | SEC_DATA;
1862 if (strcmp (TARGET_OS, "elf") != 0)
1863 flags |= SEC_ALLOC | SEC_LOAD;
1864
316f5878 1865 if (mips_abi != N64_ABI)
252b5132
RH
1866 {
1867 sec = subseg_new (".reginfo", (subsegT) 0);
1868
195325d2
TS
1869 bfd_set_section_flags (stdoutput, sec, flags);
1870 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
bdaaa2e1 1871
252b5132 1872 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
252b5132
RH
1873 }
1874 else
1875 {
1876 /* The 64-bit ABI uses a .MIPS.options section rather than
1877 .reginfo section. */
1878 sec = subseg_new (".MIPS.options", (subsegT) 0);
195325d2
TS
1879 bfd_set_section_flags (stdoutput, sec, flags);
1880 bfd_set_section_alignment (stdoutput, sec, 3);
252b5132 1881
252b5132
RH
1882 /* Set up the option header. */
1883 {
1884 Elf_Internal_Options opthdr;
1885 char *f;
1886
1887 opthdr.kind = ODK_REGINFO;
1888 opthdr.size = (sizeof (Elf_External_Options)
1889 + sizeof (Elf64_External_RegInfo));
1890 opthdr.section = 0;
1891 opthdr.info = 0;
1892 f = frag_more (sizeof (Elf_External_Options));
1893 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1894 (Elf_External_Options *) f);
1895
1896 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1897 }
252b5132
RH
1898 }
1899
1900 if (ECOFF_DEBUGGING)
1901 {
1902 sec = subseg_new (".mdebug", (subsegT) 0);
1903 (void) bfd_set_section_flags (stdoutput, sec,
1904 SEC_HAS_CONTENTS | SEC_READONLY);
1905 (void) bfd_set_section_alignment (stdoutput, sec, 2);
1906 }
dcd410fe 1907 else if (OUTPUT_FLAVOR == bfd_target_elf_flavour && mips_flag_pdr)
ecb4347a
DJ
1908 {
1909 pdr_seg = subseg_new (".pdr", (subsegT) 0);
1910 (void) bfd_set_section_flags (stdoutput, pdr_seg,
1911 SEC_READONLY | SEC_RELOC
1912 | SEC_DEBUGGING);
1913 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1914 }
252b5132
RH
1915
1916 subseg_set (seg, subseg);
1917 }
1918 }
707bfff6 1919#endif /* OBJ_ELF */
252b5132
RH
1920
1921 if (! ECOFF_DEBUGGING)
1922 md_obj_begin ();
71400594
RS
1923
1924 if (mips_fix_vr4120)
1925 init_vr4120_conflicts ();
252b5132
RH
1926}
1927
1928void
17a2f251 1929md_mips_end (void)
252b5132
RH
1930{
1931 if (! ECOFF_DEBUGGING)
1932 md_obj_end ();
1933}
1934
1935void
17a2f251 1936md_assemble (char *str)
252b5132
RH
1937{
1938 struct mips_cl_insn insn;
f6688943
TS
1939 bfd_reloc_code_real_type unused_reloc[3]
1940 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132
RH
1941
1942 imm_expr.X_op = O_absent;
5f74bc13 1943 imm2_expr.X_op = O_absent;
252b5132 1944 offset_expr.X_op = O_absent;
f6688943
TS
1945 imm_reloc[0] = BFD_RELOC_UNUSED;
1946 imm_reloc[1] = BFD_RELOC_UNUSED;
1947 imm_reloc[2] = BFD_RELOC_UNUSED;
1948 offset_reloc[0] = BFD_RELOC_UNUSED;
1949 offset_reloc[1] = BFD_RELOC_UNUSED;
1950 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
1951
1952 if (mips_opts.mips16)
1953 mips16_ip (str, &insn);
1954 else
1955 {
1956 mips_ip (str, &insn);
beae10d5
KH
1957 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1958 str, insn.insn_opcode));
252b5132
RH
1959 }
1960
1961 if (insn_error)
1962 {
1963 as_bad ("%s `%s'", insn_error, str);
1964 return;
1965 }
1966
1967 if (insn.insn_mo->pinfo == INSN_MACRO)
1968 {
584892a6 1969 macro_start ();
252b5132
RH
1970 if (mips_opts.mips16)
1971 mips16_macro (&insn);
1972 else
1973 macro (&insn);
584892a6 1974 macro_end ();
252b5132
RH
1975 }
1976 else
1977 {
1978 if (imm_expr.X_op != O_absent)
4d7206a2 1979 append_insn (&insn, &imm_expr, imm_reloc);
252b5132 1980 else if (offset_expr.X_op != O_absent)
4d7206a2 1981 append_insn (&insn, &offset_expr, offset_reloc);
252b5132 1982 else
4d7206a2 1983 append_insn (&insn, NULL, unused_reloc);
252b5132
RH
1984 }
1985}
1986
5919d012 1987/* Return true if the given relocation might need a matching %lo().
0a44bf69
RS
1988 This is only "might" because SVR4 R_MIPS_GOT16 relocations only
1989 need a matching %lo() when applied to local symbols. */
5919d012
RS
1990
1991static inline bfd_boolean
17a2f251 1992reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
5919d012 1993{
3b91255e
RS
1994 return (HAVE_IN_PLACE_ADDENDS
1995 && (reloc == BFD_RELOC_HI16_S
0a44bf69
RS
1996 || reloc == BFD_RELOC_MIPS16_HI16_S
1997 /* VxWorks R_MIPS_GOT16 relocs never need a matching %lo();
1998 all GOT16 relocations evaluate to "G". */
1999 || (reloc == BFD_RELOC_MIPS_GOT16 && mips_pic != VXWORKS_PIC)));
5919d012
RS
2000}
2001
2002/* Return true if the given fixup is followed by a matching R_MIPS_LO16
2003 relocation. */
2004
2005static inline bfd_boolean
17a2f251 2006fixup_has_matching_lo_p (fixS *fixp)
5919d012
RS
2007{
2008 return (fixp->fx_next != NULL
d6f16593
MR
2009 && (fixp->fx_next->fx_r_type == BFD_RELOC_LO16
2010 || fixp->fx_next->fx_r_type == BFD_RELOC_MIPS16_LO16)
5919d012
RS
2011 && fixp->fx_addsy == fixp->fx_next->fx_addsy
2012 && fixp->fx_offset == fixp->fx_next->fx_offset);
2013}
2014
252b5132
RH
2015/* See whether instruction IP reads register REG. CLASS is the type
2016 of register. */
2017
2018static int
71400594 2019insn_uses_reg (const struct mips_cl_insn *ip, unsigned int reg,
17a2f251 2020 enum mips_regclass class)
252b5132
RH
2021{
2022 if (class == MIPS16_REG)
2023 {
2024 assert (mips_opts.mips16);
2025 reg = mips16_to_32_reg_map[reg];
2026 class = MIPS_GR_REG;
2027 }
2028
85b51719
TS
2029 /* Don't report on general register ZERO, since it never changes. */
2030 if (class == MIPS_GR_REG && reg == ZERO)
252b5132
RH
2031 return 0;
2032
2033 if (class == MIPS_FP_REG)
2034 {
2035 assert (! mips_opts.mips16);
2036 /* If we are called with either $f0 or $f1, we must check $f0.
2037 This is not optimal, because it will introduce an unnecessary
2038 NOP between "lwc1 $f0" and "swc1 $f1". To fix this we would
2039 need to distinguish reading both $f0 and $f1 or just one of
2040 them. Note that we don't have to check the other way,
2041 because there is no instruction that sets both $f0 and $f1
2042 and requires a delay. */
2043 if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
bf12938e 2044 && ((EXTRACT_OPERAND (FS, *ip) & ~(unsigned) 1)
252b5132
RH
2045 == (reg &~ (unsigned) 1)))
2046 return 1;
2047 if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
bf12938e 2048 && ((EXTRACT_OPERAND (FT, *ip) & ~(unsigned) 1)
252b5132
RH
2049 == (reg &~ (unsigned) 1)))
2050 return 1;
2051 }
2052 else if (! mips_opts.mips16)
2053 {
2054 if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
bf12938e 2055 && EXTRACT_OPERAND (RS, *ip) == reg)
252b5132
RH
2056 return 1;
2057 if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
bf12938e 2058 && EXTRACT_OPERAND (RT, *ip) == reg)
252b5132
RH
2059 return 1;
2060 }
2061 else
2062 {
2063 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
bf12938e 2064 && mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RX, *ip)] == reg)
252b5132
RH
2065 return 1;
2066 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
bf12938e 2067 && mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RY, *ip)] == reg)
252b5132
RH
2068 return 1;
2069 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
bf12938e 2070 && (mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (MOVE32Z, *ip)]
252b5132
RH
2071 == reg))
2072 return 1;
2073 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
2074 return 1;
2075 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
2076 return 1;
2077 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
2078 return 1;
2079 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
bf12938e 2080 && MIPS16_EXTRACT_OPERAND (REGR32, *ip) == reg)
252b5132
RH
2081 return 1;
2082 }
2083
2084 return 0;
2085}
2086
2087/* This function returns true if modifying a register requires a
2088 delay. */
2089
2090static int
17a2f251 2091reg_needs_delay (unsigned int reg)
252b5132
RH
2092{
2093 unsigned long prev_pinfo;
2094
47e39b9d 2095 prev_pinfo = history[0].insn_mo->pinfo;
252b5132 2096 if (! mips_opts.noreorder
81912461
ILT
2097 && (((prev_pinfo & INSN_LOAD_MEMORY_DELAY)
2098 && ! gpr_interlocks)
2099 || ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
2100 && ! cop_interlocks)))
252b5132 2101 {
81912461
ILT
2102 /* A load from a coprocessor or from memory. All load delays
2103 delay the use of general register rt for one instruction. */
bdaaa2e1 2104 /* Itbl support may require additional care here. */
252b5132 2105 know (prev_pinfo & INSN_WRITE_GPR_T);
bf12938e 2106 if (reg == EXTRACT_OPERAND (RT, history[0]))
252b5132
RH
2107 return 1;
2108 }
2109
2110 return 0;
2111}
2112
404a8071
RS
2113/* Move all labels in insn_labels to the current insertion point. */
2114
2115static void
2116mips_move_labels (void)
2117{
2118 struct insn_label_list *l;
2119 valueT val;
2120
2121 for (l = insn_labels; l != NULL; l = l->next)
2122 {
2123 assert (S_GET_SEGMENT (l->label) == now_seg);
2124 symbol_set_frag (l->label, frag_now);
2125 val = (valueT) frag_now_fix ();
2126 /* mips16 text labels are stored as odd. */
2127 if (mips_opts.mips16)
2128 ++val;
2129 S_SET_VALUE (l->label, val);
2130 }
2131}
2132
252b5132
RH
2133/* Mark instruction labels in mips16 mode. This permits the linker to
2134 handle them specially, such as generating jalx instructions when
2135 needed. We also make them odd for the duration of the assembly, in
2136 order to generate the right sort of code. We will make them even
2137 in the adjust_symtab routine, while leaving them marked. This is
2138 convenient for the debugger and the disassembler. The linker knows
2139 to make them odd again. */
2140
2141static void
17a2f251 2142mips16_mark_labels (void)
252b5132
RH
2143{
2144 if (mips_opts.mips16)
2145 {
2146 struct insn_label_list *l;
98aa84af 2147 valueT val;
252b5132
RH
2148
2149 for (l = insn_labels; l != NULL; l = l->next)
2150 {
2151#ifdef OBJ_ELF
2152 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
2153 S_SET_OTHER (l->label, STO_MIPS16);
2154#endif
98aa84af
AM
2155 val = S_GET_VALUE (l->label);
2156 if ((val & 1) == 0)
2157 S_SET_VALUE (l->label, val + 1);
252b5132
RH
2158 }
2159 }
2160}
2161
4d7206a2
RS
2162/* End the current frag. Make it a variant frag and record the
2163 relaxation info. */
2164
2165static void
2166relax_close_frag (void)
2167{
584892a6 2168 mips_macro_warning.first_frag = frag_now;
4d7206a2 2169 frag_var (rs_machine_dependent, 0, 0,
584892a6 2170 RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1]),
4d7206a2
RS
2171 mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
2172
2173 memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
2174 mips_relax.first_fixup = 0;
2175}
2176
2177/* Start a new relaxation sequence whose expansion depends on SYMBOL.
2178 See the comment above RELAX_ENCODE for more details. */
2179
2180static void
2181relax_start (symbolS *symbol)
2182{
2183 assert (mips_relax.sequence == 0);
2184 mips_relax.sequence = 1;
2185 mips_relax.symbol = symbol;
2186}
2187
2188/* Start generating the second version of a relaxable sequence.
2189 See the comment above RELAX_ENCODE for more details. */
252b5132
RH
2190
2191static void
4d7206a2
RS
2192relax_switch (void)
2193{
2194 assert (mips_relax.sequence == 1);
2195 mips_relax.sequence = 2;
2196}
2197
2198/* End the current relaxable sequence. */
2199
2200static void
2201relax_end (void)
2202{
2203 assert (mips_relax.sequence == 2);
2204 relax_close_frag ();
2205 mips_relax.sequence = 0;
2206}
2207
71400594
RS
2208/* Classify an instruction according to the FIX_VR4120_* enumeration.
2209 Return NUM_FIX_VR4120_CLASSES if the instruction isn't affected
2210 by VR4120 errata. */
4d7206a2 2211
71400594
RS
2212static unsigned int
2213classify_vr4120_insn (const char *name)
252b5132 2214{
71400594
RS
2215 if (strncmp (name, "macc", 4) == 0)
2216 return FIX_VR4120_MACC;
2217 if (strncmp (name, "dmacc", 5) == 0)
2218 return FIX_VR4120_DMACC;
2219 if (strncmp (name, "mult", 4) == 0)
2220 return FIX_VR4120_MULT;
2221 if (strncmp (name, "dmult", 5) == 0)
2222 return FIX_VR4120_DMULT;
2223 if (strstr (name, "div"))
2224 return FIX_VR4120_DIV;
2225 if (strcmp (name, "mtlo") == 0 || strcmp (name, "mthi") == 0)
2226 return FIX_VR4120_MTHILO;
2227 return NUM_FIX_VR4120_CLASSES;
2228}
252b5132 2229
71400594
RS
2230/* Return the number of instructions that must separate INSN1 and INSN2,
2231 where INSN1 is the earlier instruction. Return the worst-case value
2232 for any INSN2 if INSN2 is null. */
252b5132 2233
71400594
RS
2234static unsigned int
2235insns_between (const struct mips_cl_insn *insn1,
2236 const struct mips_cl_insn *insn2)
2237{
2238 unsigned long pinfo1, pinfo2;
2239
2240 /* This function needs to know which pinfo flags are set for INSN2
2241 and which registers INSN2 uses. The former is stored in PINFO2 and
2242 the latter is tested via INSN2_USES_REG. If INSN2 is null, PINFO2
2243 will have every flag set and INSN2_USES_REG will always return true. */
2244 pinfo1 = insn1->insn_mo->pinfo;
2245 pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~0U;
252b5132 2246
71400594
RS
2247#define INSN2_USES_REG(REG, CLASS) \
2248 (insn2 == NULL || insn_uses_reg (insn2, REG, CLASS))
2249
2250 /* For most targets, write-after-read dependencies on the HI and LO
2251 registers must be separated by at least two instructions. */
2252 if (!hilo_interlocks)
252b5132 2253 {
71400594
RS
2254 if ((pinfo1 & INSN_READ_LO) && (pinfo2 & INSN_WRITE_LO))
2255 return 2;
2256 if ((pinfo1 & INSN_READ_HI) && (pinfo2 & INSN_WRITE_HI))
2257 return 2;
2258 }
2259
2260 /* If we're working around r7000 errata, there must be two instructions
2261 between an mfhi or mflo and any instruction that uses the result. */
2262 if (mips_7000_hilo_fix
2263 && MF_HILO_INSN (pinfo1)
2264 && INSN2_USES_REG (EXTRACT_OPERAND (RD, *insn1), MIPS_GR_REG))
2265 return 2;
2266
2267 /* If working around VR4120 errata, check for combinations that need
2268 a single intervening instruction. */
2269 if (mips_fix_vr4120)
2270 {
2271 unsigned int class1, class2;
252b5132 2272
71400594
RS
2273 class1 = classify_vr4120_insn (insn1->insn_mo->name);
2274 if (class1 != NUM_FIX_VR4120_CLASSES && vr4120_conflicts[class1] != 0)
252b5132 2275 {
71400594
RS
2276 if (insn2 == NULL)
2277 return 1;
2278 class2 = classify_vr4120_insn (insn2->insn_mo->name);
2279 if (vr4120_conflicts[class1] & (1 << class2))
2280 return 1;
252b5132 2281 }
71400594
RS
2282 }
2283
2284 if (!mips_opts.mips16)
2285 {
2286 /* Check for GPR or coprocessor load delays. All such delays
2287 are on the RT register. */
2288 /* Itbl support may require additional care here. */
2289 if ((!gpr_interlocks && (pinfo1 & INSN_LOAD_MEMORY_DELAY))
2290 || (!cop_interlocks && (pinfo1 & INSN_LOAD_COPROC_DELAY)))
252b5132 2291 {
71400594
RS
2292 know (pinfo1 & INSN_WRITE_GPR_T);
2293 if (INSN2_USES_REG (EXTRACT_OPERAND (RT, *insn1), MIPS_GR_REG))
2294 return 1;
2295 }
2296
2297 /* Check for generic coprocessor hazards.
2298
2299 This case is not handled very well. There is no special
2300 knowledge of CP0 handling, and the coprocessors other than
2301 the floating point unit are not distinguished at all. */
2302 /* Itbl support may require additional care here. FIXME!
2303 Need to modify this to include knowledge about
2304 user specified delays! */
2305 else if ((!cop_interlocks && (pinfo1 & INSN_COPROC_MOVE_DELAY))
2306 || (!cop_mem_interlocks && (pinfo1 & INSN_COPROC_MEMORY_DELAY)))
2307 {
2308 /* Handle cases where INSN1 writes to a known general coprocessor
2309 register. There must be a one instruction delay before INSN2
2310 if INSN2 reads that register, otherwise no delay is needed. */
2311 if (pinfo1 & INSN_WRITE_FPR_T)
252b5132 2312 {
71400594
RS
2313 if (INSN2_USES_REG (EXTRACT_OPERAND (FT, *insn1), MIPS_FP_REG))
2314 return 1;
252b5132 2315 }
71400594 2316 else if (pinfo1 & INSN_WRITE_FPR_S)
252b5132 2317 {
71400594
RS
2318 if (INSN2_USES_REG (EXTRACT_OPERAND (FS, *insn1), MIPS_FP_REG))
2319 return 1;
252b5132
RH
2320 }
2321 else
2322 {
71400594
RS
2323 /* Read-after-write dependencies on the control registers
2324 require a two-instruction gap. */
2325 if ((pinfo1 & INSN_WRITE_COND_CODE)
2326 && (pinfo2 & INSN_READ_COND_CODE))
2327 return 2;
2328
2329 /* We don't know exactly what INSN1 does. If INSN2 is
2330 also a coprocessor instruction, assume there must be
2331 a one instruction gap. */
2332 if (pinfo2 & INSN_COP)
2333 return 1;
252b5132
RH
2334 }
2335 }
6b76fefe 2336
71400594
RS
2337 /* Check for read-after-write dependencies on the coprocessor
2338 control registers in cases where INSN1 does not need a general
2339 coprocessor delay. This means that INSN1 is a floating point
2340 comparison instruction. */
2341 /* Itbl support may require additional care here. */
2342 else if (!cop_interlocks
2343 && (pinfo1 & INSN_WRITE_COND_CODE)
2344 && (pinfo2 & INSN_READ_COND_CODE))
2345 return 1;
2346 }
6b76fefe 2347
71400594 2348#undef INSN2_USES_REG
6b76fefe 2349
71400594
RS
2350 return 0;
2351}
6b76fefe 2352
7d8e00cf
RS
2353/* Return the number of nops that would be needed to work around the
2354 VR4130 mflo/mfhi errata if instruction INSN immediately followed
2355 the MAX_VR4130_NOPS instructions described by HISTORY. */
2356
2357static int
2358nops_for_vr4130 (const struct mips_cl_insn *history,
2359 const struct mips_cl_insn *insn)
2360{
2361 int i, j, reg;
2362
2363 /* Check if the instruction writes to HI or LO. MTHI and MTLO
2364 are not affected by the errata. */
2365 if (insn != 0
2366 && ((insn->insn_mo->pinfo & (INSN_WRITE_HI | INSN_WRITE_LO)) == 0
2367 || strcmp (insn->insn_mo->name, "mtlo") == 0
2368 || strcmp (insn->insn_mo->name, "mthi") == 0))
2369 return 0;
2370
2371 /* Search for the first MFLO or MFHI. */
2372 for (i = 0; i < MAX_VR4130_NOPS; i++)
2373 if (!history[i].noreorder_p && MF_HILO_INSN (history[i].insn_mo->pinfo))
2374 {
2375 /* Extract the destination register. */
2376 if (mips_opts.mips16)
2377 reg = mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RX, history[i])];
2378 else
2379 reg = EXTRACT_OPERAND (RD, history[i]);
2380
2381 /* No nops are needed if INSN reads that register. */
2382 if (insn != NULL && insn_uses_reg (insn, reg, MIPS_GR_REG))
2383 return 0;
2384
2385 /* ...or if any of the intervening instructions do. */
2386 for (j = 0; j < i; j++)
2387 if (insn_uses_reg (&history[j], reg, MIPS_GR_REG))
2388 return 0;
2389
2390 return MAX_VR4130_NOPS - i;
2391 }
2392 return 0;
2393}
2394
71400594
RS
2395/* Return the number of nops that would be needed if instruction INSN
2396 immediately followed the MAX_NOPS instructions given by HISTORY,
2397 where HISTORY[0] is the most recent instruction. If INSN is null,
2398 return the worse-case number of nops for any instruction. */
bdaaa2e1 2399
71400594
RS
2400static int
2401nops_for_insn (const struct mips_cl_insn *history,
2402 const struct mips_cl_insn *insn)
2403{
2404 int i, nops, tmp_nops;
bdaaa2e1 2405
71400594 2406 nops = 0;
7d8e00cf 2407 for (i = 0; i < MAX_DELAY_NOPS; i++)
71400594
RS
2408 if (!history[i].noreorder_p)
2409 {
2410 tmp_nops = insns_between (history + i, insn) - i;
2411 if (tmp_nops > nops)
2412 nops = tmp_nops;
2413 }
7d8e00cf
RS
2414
2415 if (mips_fix_vr4130)
2416 {
2417 tmp_nops = nops_for_vr4130 (history, insn);
2418 if (tmp_nops > nops)
2419 nops = tmp_nops;
2420 }
2421
71400594
RS
2422 return nops;
2423}
252b5132 2424
71400594
RS
2425/* The variable arguments provide NUM_INSNS extra instructions that
2426 might be added to HISTORY. Return the largest number of nops that
2427 would be needed after the extended sequence. */
252b5132 2428
71400594
RS
2429static int
2430nops_for_sequence (int num_insns, const struct mips_cl_insn *history, ...)
2431{
2432 va_list args;
2433 struct mips_cl_insn buffer[MAX_NOPS];
2434 struct mips_cl_insn *cursor;
2435 int nops;
2436
2437 va_start (args, history);
2438 cursor = buffer + num_insns;
2439 memcpy (cursor, history, (MAX_NOPS - num_insns) * sizeof (*cursor));
2440 while (cursor > buffer)
2441 *--cursor = *va_arg (args, const struct mips_cl_insn *);
2442
2443 nops = nops_for_insn (buffer, NULL);
2444 va_end (args);
2445 return nops;
2446}
252b5132 2447
71400594
RS
2448/* Like nops_for_insn, but if INSN is a branch, take into account the
2449 worst-case delay for the branch target. */
252b5132 2450
71400594
RS
2451static int
2452nops_for_insn_or_target (const struct mips_cl_insn *history,
2453 const struct mips_cl_insn *insn)
2454{
2455 int nops, tmp_nops;
60b63b72 2456
71400594
RS
2457 nops = nops_for_insn (history, insn);
2458 if (insn->insn_mo->pinfo & (INSN_UNCOND_BRANCH_DELAY
2459 | INSN_COND_BRANCH_DELAY
2460 | INSN_COND_BRANCH_LIKELY))
2461 {
2462 tmp_nops = nops_for_sequence (2, history, insn, NOP_INSN);
2463 if (tmp_nops > nops)
2464 nops = tmp_nops;
2465 }
2466 else if (mips_opts.mips16 && (insn->insn_mo->pinfo & MIPS16_INSN_BRANCH))
2467 {
2468 tmp_nops = nops_for_sequence (1, history, insn);
2469 if (tmp_nops > nops)
2470 nops = tmp_nops;
2471 }
2472 return nops;
2473}
2474
2475/* Output an instruction. IP is the instruction information.
2476 ADDRESS_EXPR is an operand of the instruction to be used with
2477 RELOC_TYPE. */
2478
2479static void
2480append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
2481 bfd_reloc_code_real_type *reloc_type)
2482{
3994f87e 2483 unsigned long prev_pinfo, pinfo;
71400594
RS
2484 relax_stateT prev_insn_frag_type = 0;
2485 bfd_boolean relaxed_branch = FALSE;
2486
2487 /* Mark instruction labels in mips16 mode. */
2488 mips16_mark_labels ();
2489
2490 prev_pinfo = history[0].insn_mo->pinfo;
2491 pinfo = ip->insn_mo->pinfo;
2492
2493 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
2494 {
2495 /* There are a lot of optimizations we could do that we don't.
2496 In particular, we do not, in general, reorder instructions.
2497 If you use gcc with optimization, it will reorder
2498 instructions and generally do much more optimization then we
2499 do here; repeating all that work in the assembler would only
2500 benefit hand written assembly code, and does not seem worth
2501 it. */
2502 int nops = (mips_optimize == 0
2503 ? nops_for_insn (history, NULL)
2504 : nops_for_insn_or_target (history, ip));
2505 if (nops > 0)
252b5132
RH
2506 {
2507 fragS *old_frag;
2508 unsigned long old_frag_offset;
2509 int i;
252b5132
RH
2510
2511 old_frag = frag_now;
2512 old_frag_offset = frag_now_fix ();
2513
2514 for (i = 0; i < nops; i++)
2515 emit_nop ();
2516
2517 if (listing)
2518 {
2519 listing_prev_line ();
2520 /* We may be at the start of a variant frag. In case we
2521 are, make sure there is enough space for the frag
2522 after the frags created by listing_prev_line. The
2523 argument to frag_grow here must be at least as large
2524 as the argument to all other calls to frag_grow in
2525 this file. We don't have to worry about being in the
2526 middle of a variant frag, because the variants insert
2527 all needed nop instructions themselves. */
2528 frag_grow (40);
2529 }
2530
404a8071 2531 mips_move_labels ();
252b5132
RH
2532
2533#ifndef NO_ECOFF_DEBUGGING
2534 if (ECOFF_DEBUGGING)
2535 ecoff_fix_loc (old_frag, old_frag_offset);
2536#endif
2537 }
71400594
RS
2538 }
2539 else if (mips_relax.sequence != 2 && prev_nop_frag != NULL)
2540 {
2541 /* Work out how many nops in prev_nop_frag are needed by IP. */
2542 int nops = nops_for_insn_or_target (history, ip);
2543 assert (nops <= prev_nop_frag_holds);
252b5132 2544
71400594
RS
2545 /* Enforce NOPS as a minimum. */
2546 if (nops > prev_nop_frag_required)
2547 prev_nop_frag_required = nops;
252b5132 2548
71400594
RS
2549 if (prev_nop_frag_holds == prev_nop_frag_required)
2550 {
2551 /* Settle for the current number of nops. Update the history
2552 accordingly (for the benefit of any future .set reorder code). */
2553 prev_nop_frag = NULL;
2554 insert_into_history (prev_nop_frag_since,
2555 prev_nop_frag_holds, NOP_INSN);
2556 }
2557 else
2558 {
2559 /* Allow this instruction to replace one of the nops that was
2560 tentatively added to prev_nop_frag. */
2561 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
2562 prev_nop_frag_holds--;
2563 prev_nop_frag_since++;
252b5132
RH
2564 }
2565 }
2566
58e2ea4d
MR
2567#ifdef OBJ_ELF
2568 /* The value passed to dwarf2_emit_insn is the distance between
2569 the beginning of the current instruction and the address that
2570 should be recorded in the debug tables. For MIPS16 debug info
2571 we want to use ISA-encoded addresses, so we pass -1 for an
2572 address higher by one than the current. */
2573 dwarf2_emit_insn (mips_opts.mips16 ? -1 : 0);
2574#endif
2575
895921c9 2576 /* Record the frag type before frag_var. */
47e39b9d
RS
2577 if (history[0].frag)
2578 prev_insn_frag_type = history[0].frag->fr_type;
895921c9 2579
4d7206a2 2580 if (address_expr
0b25d3e6 2581 && *reloc_type == BFD_RELOC_16_PCREL_S2
4a6a3df4
AO
2582 && (pinfo & INSN_UNCOND_BRANCH_DELAY || pinfo & INSN_COND_BRANCH_DELAY
2583 || pinfo & INSN_COND_BRANCH_LIKELY)
2584 && mips_relax_branch
2585 /* Don't try branch relaxation within .set nomacro, or within
2586 .set noat if we use $at for PIC computations. If it turns
2587 out that the branch was out-of-range, we'll get an error. */
2588 && !mips_opts.warn_about_macros
2589 && !(mips_opts.noat && mips_pic != NO_PIC)
2590 && !mips_opts.mips16)
2591 {
895921c9 2592 relaxed_branch = TRUE;
1e915849
RS
2593 add_relaxed_insn (ip, (relaxed_branch_length
2594 (NULL, NULL,
2595 (pinfo & INSN_UNCOND_BRANCH_DELAY) ? -1
2596 : (pinfo & INSN_COND_BRANCH_LIKELY) ? 1
2597 : 0)), 4,
2598 RELAX_BRANCH_ENCODE
2599 (pinfo & INSN_UNCOND_BRANCH_DELAY,
2600 pinfo & INSN_COND_BRANCH_LIKELY,
2601 pinfo & INSN_WRITE_GPR_31,
2602 0),
2603 address_expr->X_add_symbol,
2604 address_expr->X_add_number);
4a6a3df4
AO
2605 *reloc_type = BFD_RELOC_UNUSED;
2606 }
2607 else if (*reloc_type > BFD_RELOC_UNUSED)
252b5132
RH
2608 {
2609 /* We need to set up a variant frag. */
2610 assert (mips_opts.mips16 && address_expr != NULL);
1e915849
RS
2611 add_relaxed_insn (ip, 4, 0,
2612 RELAX_MIPS16_ENCODE
2613 (*reloc_type - BFD_RELOC_UNUSED,
2614 mips16_small, mips16_ext,
2615 prev_pinfo & INSN_UNCOND_BRANCH_DELAY,
2616 history[0].mips16_absolute_jump_p),
2617 make_expr_symbol (address_expr), 0);
252b5132 2618 }
252b5132
RH
2619 else if (mips_opts.mips16
2620 && ! ip->use_extend
f6688943 2621 && *reloc_type != BFD_RELOC_MIPS16_JMP)
9497f5ac 2622 {
b8ee1a6e
DU
2623 if ((pinfo & INSN_UNCOND_BRANCH_DELAY) == 0)
2624 /* Make sure there is enough room to swap this instruction with
2625 a following jump instruction. */
2626 frag_grow (6);
1e915849 2627 add_fixed_insn (ip);
252b5132
RH
2628 }
2629 else
2630 {
2631 if (mips_opts.mips16
2632 && mips_opts.noreorder
2633 && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
2634 as_warn (_("extended instruction in delay slot"));
2635
4d7206a2
RS
2636 if (mips_relax.sequence)
2637 {
2638 /* If we've reached the end of this frag, turn it into a variant
2639 frag and record the information for the instructions we've
2640 written so far. */
2641 if (frag_room () < 4)
2642 relax_close_frag ();
2643 mips_relax.sizes[mips_relax.sequence - 1] += 4;
2644 }
2645
584892a6
RS
2646 if (mips_relax.sequence != 2)
2647 mips_macro_warning.sizes[0] += 4;
2648 if (mips_relax.sequence != 1)
2649 mips_macro_warning.sizes[1] += 4;
2650
1e915849
RS
2651 if (mips_opts.mips16)
2652 {
2653 ip->fixed_p = 1;
2654 ip->mips16_absolute_jump_p = (*reloc_type == BFD_RELOC_MIPS16_JMP);
2655 }
2656 add_fixed_insn (ip);
252b5132
RH
2657 }
2658
01a3f561 2659 if (address_expr != NULL && *reloc_type <= BFD_RELOC_UNUSED)
252b5132
RH
2660 {
2661 if (address_expr->X_op == O_constant)
2662 {
f17c130b 2663 unsigned int tmp;
f6688943
TS
2664
2665 switch (*reloc_type)
252b5132
RH
2666 {
2667 case BFD_RELOC_32:
2668 ip->insn_opcode |= address_expr->X_add_number;
2669 break;
2670
f6688943 2671 case BFD_RELOC_MIPS_HIGHEST:
f17c130b
AM
2672 tmp = (address_expr->X_add_number + 0x800080008000ull) >> 48;
2673 ip->insn_opcode |= tmp & 0xffff;
f6688943
TS
2674 break;
2675
2676 case BFD_RELOC_MIPS_HIGHER:
f17c130b
AM
2677 tmp = (address_expr->X_add_number + 0x80008000ull) >> 32;
2678 ip->insn_opcode |= tmp & 0xffff;
f6688943
TS
2679 break;
2680
2681 case BFD_RELOC_HI16_S:
f17c130b
AM
2682 tmp = (address_expr->X_add_number + 0x8000) >> 16;
2683 ip->insn_opcode |= tmp & 0xffff;
f6688943
TS
2684 break;
2685
2686 case BFD_RELOC_HI16:
2687 ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
2688 break;
2689
01a3f561 2690 case BFD_RELOC_UNUSED:
252b5132 2691 case BFD_RELOC_LO16:
ed6fb7bd 2692 case BFD_RELOC_MIPS_GOT_DISP:
252b5132
RH
2693 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2694 break;
2695
2696 case BFD_RELOC_MIPS_JMP:
2697 if ((address_expr->X_add_number & 3) != 0)
2698 as_bad (_("jump to misaligned address (0x%lx)"),
2699 (unsigned long) address_expr->X_add_number);
2700 ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
2701 break;
2702
2703 case BFD_RELOC_MIPS16_JMP:
2704 if ((address_expr->X_add_number & 3) != 0)
2705 as_bad (_("jump to misaligned address (0x%lx)"),
2706 (unsigned long) address_expr->X_add_number);
2707 ip->insn_opcode |=
2708 (((address_expr->X_add_number & 0x7c0000) << 3)
2709 | ((address_expr->X_add_number & 0xf800000) >> 7)
2710 | ((address_expr->X_add_number & 0x3fffc) >> 2));
2711 break;
2712
252b5132 2713 case BFD_RELOC_16_PCREL_S2:
bad36eac
DJ
2714 if ((address_expr->X_add_number & 3) != 0)
2715 as_bad (_("branch to misaligned address (0x%lx)"),
2716 (unsigned long) address_expr->X_add_number);
2717 if (mips_relax_branch)
2718 goto need_reloc;
2719 if ((address_expr->X_add_number + 0x20000) & ~0x3ffff)
2720 as_bad (_("branch address range overflow (0x%lx)"),
2721 (unsigned long) address_expr->X_add_number);
2722 ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0xffff;
2723 break;
252b5132
RH
2724
2725 default:
2726 internalError ();
2727 }
2728 }
01a3f561 2729 else if (*reloc_type < BFD_RELOC_UNUSED)
252b5132 2730 need_reloc:
4d7206a2
RS
2731 {
2732 reloc_howto_type *howto;
2733 int i;
34ce925e 2734
4d7206a2
RS
2735 /* In a compound relocation, it is the final (outermost)
2736 operator that determines the relocated field. */
2737 for (i = 1; i < 3; i++)
2738 if (reloc_type[i] == BFD_RELOC_UNUSED)
2739 break;
34ce925e 2740
4d7206a2 2741 howto = bfd_reloc_type_lookup (stdoutput, reloc_type[i - 1]);
1e915849
RS
2742 ip->fixp[0] = fix_new_exp (ip->frag, ip->where,
2743 bfd_get_reloc_size (howto),
2744 address_expr,
2745 reloc_type[0] == BFD_RELOC_16_PCREL_S2,
2746 reloc_type[0]);
4d7206a2
RS
2747
2748 /* These relocations can have an addend that won't fit in
2749 4 octets for 64bit assembly. */
2750 if (HAVE_64BIT_GPRS
2751 && ! howto->partial_inplace
2752 && (reloc_type[0] == BFD_RELOC_16
2753 || reloc_type[0] == BFD_RELOC_32
2754 || reloc_type[0] == BFD_RELOC_MIPS_JMP
2755 || reloc_type[0] == BFD_RELOC_HI16_S
2756 || reloc_type[0] == BFD_RELOC_LO16
2757 || reloc_type[0] == BFD_RELOC_GPREL16
2758 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
2759 || reloc_type[0] == BFD_RELOC_GPREL32
2760 || reloc_type[0] == BFD_RELOC_64
2761 || reloc_type[0] == BFD_RELOC_CTOR
2762 || reloc_type[0] == BFD_RELOC_MIPS_SUB
2763 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
2764 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
2765 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
2766 || reloc_type[0] == BFD_RELOC_MIPS_REL16
d6f16593
MR
2767 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
2768 || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
2769 || reloc_type[0] == BFD_RELOC_MIPS16_HI16_S
2770 || reloc_type[0] == BFD_RELOC_MIPS16_LO16))
1e915849 2771 ip->fixp[0]->fx_no_overflow = 1;
4d7206a2
RS
2772
2773 if (mips_relax.sequence)
2774 {
2775 if (mips_relax.first_fixup == 0)
1e915849 2776 mips_relax.first_fixup = ip->fixp[0];
4d7206a2
RS
2777 }
2778 else if (reloc_needs_lo_p (*reloc_type))
2779 {
2780 struct mips_hi_fixup *hi_fixup;
252b5132 2781
4d7206a2
RS
2782 /* Reuse the last entry if it already has a matching %lo. */
2783 hi_fixup = mips_hi_fixup_list;
2784 if (hi_fixup == 0
2785 || !fixup_has_matching_lo_p (hi_fixup->fixp))
2786 {
2787 hi_fixup = ((struct mips_hi_fixup *)
2788 xmalloc (sizeof (struct mips_hi_fixup)));
2789 hi_fixup->next = mips_hi_fixup_list;
2790 mips_hi_fixup_list = hi_fixup;
252b5132 2791 }
1e915849 2792 hi_fixup->fixp = ip->fixp[0];
4d7206a2
RS
2793 hi_fixup->seg = now_seg;
2794 }
f6688943 2795
4d7206a2
RS
2796 /* Add fixups for the second and third relocations, if given.
2797 Note that the ABI allows the second relocation to be
2798 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the
2799 moment we only use RSS_UNDEF, but we could add support
2800 for the others if it ever becomes necessary. */
2801 for (i = 1; i < 3; i++)
2802 if (reloc_type[i] != BFD_RELOC_UNUSED)
2803 {
1e915849
RS
2804 ip->fixp[i] = fix_new (ip->frag, ip->where,
2805 ip->fixp[0]->fx_size, NULL, 0,
2806 FALSE, reloc_type[i]);
b1dca8ee
RS
2807
2808 /* Use fx_tcbit to mark compound relocs. */
1e915849
RS
2809 ip->fixp[0]->fx_tcbit = 1;
2810 ip->fixp[i]->fx_tcbit = 1;
4d7206a2 2811 }
252b5132
RH
2812 }
2813 }
1e915849 2814 install_insn (ip);
252b5132
RH
2815
2816 /* Update the register mask information. */
2817 if (! mips_opts.mips16)
2818 {
2819 if (pinfo & INSN_WRITE_GPR_D)
bf12938e 2820 mips_gprmask |= 1 << EXTRACT_OPERAND (RD, *ip);
252b5132 2821 if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
bf12938e 2822 mips_gprmask |= 1 << EXTRACT_OPERAND (RT, *ip);
252b5132 2823 if (pinfo & INSN_READ_GPR_S)
bf12938e 2824 mips_gprmask |= 1 << EXTRACT_OPERAND (RS, *ip);
252b5132 2825 if (pinfo & INSN_WRITE_GPR_31)
f9419b05 2826 mips_gprmask |= 1 << RA;
252b5132 2827 if (pinfo & INSN_WRITE_FPR_D)
bf12938e 2828 mips_cprmask[1] |= 1 << EXTRACT_OPERAND (FD, *ip);
252b5132 2829 if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
bf12938e 2830 mips_cprmask[1] |= 1 << EXTRACT_OPERAND (FS, *ip);
252b5132 2831 if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
bf12938e 2832 mips_cprmask[1] |= 1 << EXTRACT_OPERAND (FT, *ip);
252b5132 2833 if ((pinfo & INSN_READ_FPR_R) != 0)
bf12938e 2834 mips_cprmask[1] |= 1 << EXTRACT_OPERAND (FR, *ip);
252b5132
RH
2835 if (pinfo & INSN_COP)
2836 {
bdaaa2e1
KH
2837 /* We don't keep enough information to sort these cases out.
2838 The itbl support does keep this information however, although
2839 we currently don't support itbl fprmats as part of the cop
2840 instruction. May want to add this support in the future. */
252b5132
RH
2841 }
2842 /* Never set the bit for $0, which is always zero. */
beae10d5 2843 mips_gprmask &= ~1 << 0;
252b5132
RH
2844 }
2845 else
2846 {
2847 if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
bf12938e 2848 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (RX, *ip);
252b5132 2849 if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
bf12938e 2850 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (RY, *ip);
252b5132 2851 if (pinfo & MIPS16_INSN_WRITE_Z)
bf12938e 2852 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (RZ, *ip);
252b5132
RH
2853 if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2854 mips_gprmask |= 1 << TREG;
2855 if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2856 mips_gprmask |= 1 << SP;
2857 if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2858 mips_gprmask |= 1 << RA;
2859 if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2860 mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2861 if (pinfo & MIPS16_INSN_READ_Z)
bf12938e 2862 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (MOVE32Z, *ip);
252b5132 2863 if (pinfo & MIPS16_INSN_READ_GPR_X)
bf12938e 2864 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (REGR32, *ip);
252b5132
RH
2865 }
2866
4d7206a2 2867 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
252b5132
RH
2868 {
2869 /* Filling the branch delay slot is more complex. We try to
2870 switch the branch with the previous instruction, which we can
2871 do if the previous instruction does not set up a condition
2872 that the branch tests and if the branch is not itself the
2873 target of any branch. */
2874 if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2875 || (pinfo & INSN_COND_BRANCH_DELAY))
2876 {
2877 if (mips_optimize < 2
2878 /* If we have seen .set volatile or .set nomove, don't
2879 optimize. */
2880 || mips_opts.nomove != 0
a38419a5
RS
2881 /* We can't swap if the previous instruction's position
2882 is fixed. */
2883 || history[0].fixed_p
252b5132
RH
2884 /* If the previous previous insn was in a .set
2885 noreorder, we can't swap. Actually, the MIPS
2886 assembler will swap in this situation. However, gcc
2887 configured -with-gnu-as will generate code like
2888 .set noreorder
2889 lw $4,XXX
2890 .set reorder
2891 INSN
2892 bne $4,$0,foo
2893 in which we can not swap the bne and INSN. If gcc is
2894 not configured -with-gnu-as, it does not output the
a38419a5 2895 .set pseudo-ops. */
47e39b9d 2896 || history[1].noreorder_p
252b5132
RH
2897 /* If the branch is itself the target of a branch, we
2898 can not swap. We cheat on this; all we check for is
2899 whether there is a label on this instruction. If
2900 there are any branches to anything other than a
2901 label, users must use .set noreorder. */
2902 || insn_labels != NULL
895921c9
MR
2903 /* If the previous instruction is in a variant frag
2904 other than this branch's one, we cannot do the swap.
2905 This does not apply to the mips16, which uses variant
2906 frags for different purposes. */
252b5132 2907 || (! mips_opts.mips16
895921c9 2908 && prev_insn_frag_type == rs_machine_dependent)
71400594
RS
2909 /* Check for conflicts between the branch and the instructions
2910 before the candidate delay slot. */
2911 || nops_for_insn (history + 1, ip) > 0
2912 /* Check for conflicts between the swapped sequence and the
2913 target of the branch. */
2914 || nops_for_sequence (2, history + 1, ip, history) > 0
252b5132
RH
2915 /* We do not swap with a trap instruction, since it
2916 complicates trap handlers to have the trap
2917 instruction be in a delay slot. */
2918 || (prev_pinfo & INSN_TRAP)
2919 /* If the branch reads a register that the previous
2920 instruction sets, we can not swap. */
2921 || (! mips_opts.mips16
2922 && (prev_pinfo & INSN_WRITE_GPR_T)
bf12938e 2923 && insn_uses_reg (ip, EXTRACT_OPERAND (RT, history[0]),
252b5132
RH
2924 MIPS_GR_REG))
2925 || (! mips_opts.mips16
2926 && (prev_pinfo & INSN_WRITE_GPR_D)
bf12938e 2927 && insn_uses_reg (ip, EXTRACT_OPERAND (RD, history[0]),
252b5132
RH
2928 MIPS_GR_REG))
2929 || (mips_opts.mips16
2930 && (((prev_pinfo & MIPS16_INSN_WRITE_X)
bf12938e
RS
2931 && (insn_uses_reg
2932 (ip, MIPS16_EXTRACT_OPERAND (RX, history[0]),
2933 MIPS16_REG)))
252b5132 2934 || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
bf12938e
RS
2935 && (insn_uses_reg
2936 (ip, MIPS16_EXTRACT_OPERAND (RY, history[0]),
2937 MIPS16_REG)))
252b5132 2938 || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
bf12938e
RS
2939 && (insn_uses_reg
2940 (ip, MIPS16_EXTRACT_OPERAND (RZ, history[0]),
2941 MIPS16_REG)))
252b5132
RH
2942 || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2943 && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2944 || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2945 && insn_uses_reg (ip, RA, MIPS_GR_REG))
2946 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2947 && insn_uses_reg (ip,
47e39b9d
RS
2948 MIPS16OP_EXTRACT_REG32R
2949 (history[0].insn_opcode),
252b5132
RH
2950 MIPS_GR_REG))))
2951 /* If the branch writes a register that the previous
2952 instruction sets, we can not swap (we know that
2953 branches write only to RD or to $31). */
2954 || (! mips_opts.mips16
2955 && (prev_pinfo & INSN_WRITE_GPR_T)
2956 && (((pinfo & INSN_WRITE_GPR_D)
bf12938e
RS
2957 && (EXTRACT_OPERAND (RT, history[0])
2958 == EXTRACT_OPERAND (RD, *ip)))
252b5132 2959 || ((pinfo & INSN_WRITE_GPR_31)
bf12938e 2960 && EXTRACT_OPERAND (RT, history[0]) == RA)))
252b5132
RH
2961 || (! mips_opts.mips16
2962 && (prev_pinfo & INSN_WRITE_GPR_D)
2963 && (((pinfo & INSN_WRITE_GPR_D)
bf12938e
RS
2964 && (EXTRACT_OPERAND (RD, history[0])
2965 == EXTRACT_OPERAND (RD, *ip)))
252b5132 2966 || ((pinfo & INSN_WRITE_GPR_31)
bf12938e 2967 && EXTRACT_OPERAND (RD, history[0]) == RA)))
252b5132
RH
2968 || (mips_opts.mips16
2969 && (pinfo & MIPS16_INSN_WRITE_31)
2970 && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2971 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
47e39b9d 2972 && (MIPS16OP_EXTRACT_REG32R (history[0].insn_opcode)
252b5132
RH
2973 == RA))))
2974 /* If the branch writes a register that the previous
2975 instruction reads, we can not swap (we know that
2976 branches only write to RD or to $31). */
2977 || (! mips_opts.mips16
2978 && (pinfo & INSN_WRITE_GPR_D)
47e39b9d 2979 && insn_uses_reg (&history[0],
bf12938e 2980 EXTRACT_OPERAND (RD, *ip),
252b5132
RH
2981 MIPS_GR_REG))
2982 || (! mips_opts.mips16
2983 && (pinfo & INSN_WRITE_GPR_31)
47e39b9d 2984 && insn_uses_reg (&history[0], RA, MIPS_GR_REG))
252b5132
RH
2985 || (mips_opts.mips16
2986 && (pinfo & MIPS16_INSN_WRITE_31)
47e39b9d 2987 && insn_uses_reg (&history[0], RA, MIPS_GR_REG))
252b5132
RH
2988 /* If one instruction sets a condition code and the
2989 other one uses a condition code, we can not swap. */
2990 || ((pinfo & INSN_READ_COND_CODE)
2991 && (prev_pinfo & INSN_WRITE_COND_CODE))
2992 || ((pinfo & INSN_WRITE_COND_CODE)
2993 && (prev_pinfo & INSN_READ_COND_CODE))
2994 /* If the previous instruction uses the PC, we can not
2995 swap. */
2996 || (mips_opts.mips16
2997 && (prev_pinfo & MIPS16_INSN_READ_PC))
252b5132
RH
2998 /* If the previous instruction had a fixup in mips16
2999 mode, we can not swap. This normally means that the
3000 previous instruction was a 4 byte branch anyhow. */
47e39b9d 3001 || (mips_opts.mips16 && history[0].fixp[0])
bdaaa2e1
KH
3002 /* If the previous instruction is a sync, sync.l, or
3003 sync.p, we can not swap. */
f173e82e 3004 || (prev_pinfo & INSN_SYNC))
252b5132 3005 {
29024861
DU
3006 if (mips_opts.mips16
3007 && (pinfo & INSN_UNCOND_BRANCH_DELAY)
3008 && (pinfo & (MIPS16_INSN_READ_X | MIPS16_INSN_READ_31))
3994f87e 3009 && ISA_SUPPORTS_MIPS16E)
29024861
DU
3010 {
3011 /* Convert MIPS16 jr/jalr into a "compact" jump. */
3012 ip->insn_opcode |= 0x0080;
3013 install_insn (ip);
3014 insert_into_history (0, 1, ip);
3015 }
3016 else
3017 {
3018 /* We could do even better for unconditional branches to
3019 portions of this object file; we could pick up the
3020 instruction at the destination, put it in the delay
3021 slot, and bump the destination address. */
3022 insert_into_history (0, 1, ip);
3023 emit_nop ();
3024 }
3025
dd22970f
ILT
3026 if (mips_relax.sequence)
3027 mips_relax.sizes[mips_relax.sequence - 1] += 4;
252b5132
RH
3028 }
3029 else
3030 {
3031 /* It looks like we can actually do the swap. */
1e915849
RS
3032 struct mips_cl_insn delay = history[0];
3033 if (mips_opts.mips16)
252b5132 3034 {
b8ee1a6e
DU
3035 know (delay.frag == ip->frag);
3036 move_insn (ip, delay.frag, delay.where);
3037 move_insn (&delay, ip->frag, ip->where + insn_length (ip));
1e915849
RS
3038 }
3039 else if (relaxed_branch)
3040 {
3041 /* Add the delay slot instruction to the end of the
3042 current frag and shrink the fixed part of the
3043 original frag. If the branch occupies the tail of
3044 the latter, move it backwards to cover the gap. */
3045 delay.frag->fr_fix -= 4;
3046 if (delay.frag == ip->frag)
3047 move_insn (ip, ip->frag, ip->where - 4);
3048 add_fixed_insn (&delay);
252b5132
RH
3049 }
3050 else
3051 {
1e915849
RS
3052 move_insn (&delay, ip->frag, ip->where);
3053 move_insn (ip, history[0].frag, history[0].where);
252b5132 3054 }
1e915849
RS
3055 history[0] = *ip;
3056 delay.fixed_p = 1;
3057 insert_into_history (0, 1, &delay);
252b5132 3058 }
252b5132
RH
3059
3060 /* If that was an unconditional branch, forget the previous
3061 insn information. */
3062 if (pinfo & INSN_UNCOND_BRANCH_DELAY)
7d10b47d 3063 mips_no_prev_insn ();
252b5132
RH
3064 }
3065 else if (pinfo & INSN_COND_BRANCH_LIKELY)
3066 {
3067 /* We don't yet optimize a branch likely. What we should do
3068 is look at the target, copy the instruction found there
3069 into the delay slot, and increment the branch to jump to
3070 the next instruction. */
1e915849 3071 insert_into_history (0, 1, ip);
252b5132 3072 emit_nop ();
252b5132
RH
3073 }
3074 else
1e915849 3075 insert_into_history (0, 1, ip);
252b5132 3076 }
1e915849
RS
3077 else
3078 insert_into_history (0, 1, ip);
252b5132
RH
3079
3080 /* We just output an insn, so the next one doesn't have a label. */
3081 mips_clear_insn_labels ();
252b5132
RH
3082}
3083
7d10b47d 3084/* Forget that there was any previous instruction or label. */
252b5132
RH
3085
3086static void
7d10b47d 3087mips_no_prev_insn (void)
252b5132 3088{
7d10b47d
RS
3089 prev_nop_frag = NULL;
3090 insert_into_history (0, ARRAY_SIZE (history), NOP_INSN);
252b5132
RH
3091 mips_clear_insn_labels ();
3092}
3093
7d10b47d
RS
3094/* This function must be called before we emit something other than
3095 instructions. It is like mips_no_prev_insn except that it inserts
3096 any NOPS that might be needed by previous instructions. */
252b5132 3097
7d10b47d
RS
3098void
3099mips_emit_delays (void)
252b5132
RH
3100{
3101 if (! mips_opts.noreorder)
3102 {
71400594 3103 int nops = nops_for_insn (history, NULL);
252b5132
RH
3104 if (nops > 0)
3105 {
7d10b47d
RS
3106 while (nops-- > 0)
3107 add_fixed_insn (NOP_INSN);
3108 mips_move_labels ();
3109 }
3110 }
3111 mips_no_prev_insn ();
3112}
3113
3114/* Start a (possibly nested) noreorder block. */
3115
3116static void
3117start_noreorder (void)
3118{
3119 if (mips_opts.noreorder == 0)
3120 {
3121 unsigned int i;
3122 int nops;
3123
3124 /* None of the instructions before the .set noreorder can be moved. */
3125 for (i = 0; i < ARRAY_SIZE (history); i++)
3126 history[i].fixed_p = 1;
3127
3128 /* Insert any nops that might be needed between the .set noreorder
3129 block and the previous instructions. We will later remove any
3130 nops that turn out not to be needed. */
3131 nops = nops_for_insn (history, NULL);
3132 if (nops > 0)
3133 {
3134 if (mips_optimize != 0)
252b5132
RH
3135 {
3136 /* Record the frag which holds the nop instructions, so
3137 that we can remove them if we don't need them. */
3138 frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
3139 prev_nop_frag = frag_now;
3140 prev_nop_frag_holds = nops;
3141 prev_nop_frag_required = 0;
3142 prev_nop_frag_since = 0;
3143 }
3144
3145 for (; nops > 0; --nops)
1e915849 3146 add_fixed_insn (NOP_INSN);
252b5132 3147
7d10b47d
RS
3148 /* Move on to a new frag, so that it is safe to simply
3149 decrease the size of prev_nop_frag. */
3150 frag_wane (frag_now);
3151 frag_new (0);
404a8071 3152 mips_move_labels ();
252b5132 3153 }
7d10b47d
RS
3154 mips16_mark_labels ();
3155 mips_clear_insn_labels ();
252b5132 3156 }
7d10b47d
RS
3157 mips_opts.noreorder++;
3158 mips_any_noreorder = 1;
3159}
252b5132 3160
7d10b47d 3161/* End a nested noreorder block. */
252b5132 3162
7d10b47d
RS
3163static void
3164end_noreorder (void)
3165{
3166 mips_opts.noreorder--;
3167 if (mips_opts.noreorder == 0 && prev_nop_frag != NULL)
3168 {
3169 /* Commit to inserting prev_nop_frag_required nops and go back to
3170 handling nop insertion the .set reorder way. */
3171 prev_nop_frag->fr_fix -= ((prev_nop_frag_holds - prev_nop_frag_required)
3172 * (mips_opts.mips16 ? 2 : 4));
3173 insert_into_history (prev_nop_frag_since,
3174 prev_nop_frag_required, NOP_INSN);
3175 prev_nop_frag = NULL;
3176 }
252b5132
RH
3177}
3178
584892a6
RS
3179/* Set up global variables for the start of a new macro. */
3180
3181static void
3182macro_start (void)
3183{
3184 memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
3185 mips_macro_warning.delay_slot_p = (mips_opts.noreorder
47e39b9d 3186 && (history[0].insn_mo->pinfo
584892a6
RS
3187 & (INSN_UNCOND_BRANCH_DELAY
3188 | INSN_COND_BRANCH_DELAY
3189 | INSN_COND_BRANCH_LIKELY)) != 0);
3190}
3191
3192/* Given that a macro is longer than 4 bytes, return the appropriate warning
3193 for it. Return null if no warning is needed. SUBTYPE is a bitmask of
3194 RELAX_DELAY_SLOT and RELAX_NOMACRO. */
3195
3196static const char *
3197macro_warning (relax_substateT subtype)
3198{
3199 if (subtype & RELAX_DELAY_SLOT)
3200 return _("Macro instruction expanded into multiple instructions"
3201 " in a branch delay slot");
3202 else if (subtype & RELAX_NOMACRO)
3203 return _("Macro instruction expanded into multiple instructions");
3204 else
3205 return 0;
3206}
3207
3208/* Finish up a macro. Emit warnings as appropriate. */
3209
3210static void
3211macro_end (void)
3212{
3213 if (mips_macro_warning.sizes[0] > 4 || mips_macro_warning.sizes[1] > 4)
3214 {
3215 relax_substateT subtype;
3216
3217 /* Set up the relaxation warning flags. */
3218 subtype = 0;
3219 if (mips_macro_warning.sizes[1] > mips_macro_warning.sizes[0])
3220 subtype |= RELAX_SECOND_LONGER;
3221 if (mips_opts.warn_about_macros)
3222 subtype |= RELAX_NOMACRO;
3223 if (mips_macro_warning.delay_slot_p)
3224 subtype |= RELAX_DELAY_SLOT;
3225
3226 if (mips_macro_warning.sizes[0] > 4 && mips_macro_warning.sizes[1] > 4)
3227 {
3228 /* Either the macro has a single implementation or both
3229 implementations are longer than 4 bytes. Emit the
3230 warning now. */
3231 const char *msg = macro_warning (subtype);
3232 if (msg != 0)
3233 as_warn (msg);
3234 }
3235 else
3236 {
3237 /* One implementation might need a warning but the other
3238 definitely doesn't. */
3239 mips_macro_warning.first_frag->fr_subtype |= subtype;
3240 }
3241 }
3242}
3243
6e1304d8
RS
3244/* Read a macro's relocation codes from *ARGS and store them in *R.
3245 The first argument in *ARGS will be either the code for a single
3246 relocation or -1 followed by the three codes that make up a
3247 composite relocation. */
3248
3249static void
3250macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
3251{
3252 int i, next;
3253
3254 next = va_arg (*args, int);
3255 if (next >= 0)
3256 r[0] = (bfd_reloc_code_real_type) next;
3257 else
3258 for (i = 0; i < 3; i++)
3259 r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
3260}
3261
252b5132
RH
3262/* Build an instruction created by a macro expansion. This is passed
3263 a pointer to the count of instructions created so far, an
3264 expression, the name of the instruction to build, an operand format
3265 string, and corresponding arguments. */
3266
252b5132 3267static void
67c0d1eb 3268macro_build (expressionS *ep, const char *name, const char *fmt, ...)
252b5132 3269{
1e915849 3270 const struct mips_opcode *mo;
252b5132 3271 struct mips_cl_insn insn;
f6688943 3272 bfd_reloc_code_real_type r[3];
252b5132 3273 va_list args;
252b5132 3274
252b5132 3275 va_start (args, fmt);
252b5132 3276
252b5132
RH
3277 if (mips_opts.mips16)
3278 {
67c0d1eb 3279 mips16_macro_build (ep, name, fmt, args);
252b5132
RH
3280 va_end (args);
3281 return;
3282 }
3283
f6688943
TS
3284 r[0] = BFD_RELOC_UNUSED;
3285 r[1] = BFD_RELOC_UNUSED;
3286 r[2] = BFD_RELOC_UNUSED;
1e915849
RS
3287 mo = (struct mips_opcode *) hash_find (op_hash, name);
3288 assert (mo);
3289 assert (strcmp (name, mo->name) == 0);
3290
3291 /* Search until we get a match for NAME. It is assumed here that
65263ce3 3292 macros will never generate MDMX, MIPS-3D, DSP or MT instructions. */
1e915849
RS
3293 while (strcmp (fmt, mo->args) != 0
3294 || mo->pinfo == INSN_MACRO
3295 || !OPCODE_IS_MEMBER (mo,
3296 (mips_opts.isa
9b3f89ee 3297 | (mips_opts.mips16 ? INSN_MIPS16 : 0)
e16bfa71 3298 | (mips_opts.ase_smartmips ? INSN_SMARTMIPS : 0)),
fef14a42 3299 mips_opts.arch)
1e915849
RS
3300 || (mips_opts.arch == CPU_R4650 && (mo->pinfo & FP_D) != 0))
3301 {
3302 ++mo;
3303 assert (mo->name);
3304 assert (strcmp (name, mo->name) == 0);
252b5132
RH
3305 }
3306
1e915849 3307 create_insn (&insn, mo);
252b5132
RH
3308 for (;;)
3309 {
3310 switch (*fmt++)
3311 {
3312 case '\0':
3313 break;
3314
3315 case ',':
3316 case '(':
3317 case ')':
3318 continue;
3319
5f74bc13
CD
3320 case '+':
3321 switch (*fmt++)
3322 {
3323 case 'A':
3324 case 'E':
bf12938e 3325 INSERT_OPERAND (SHAMT, insn, va_arg (args, int));
5f74bc13
CD
3326 continue;
3327
3328 case 'B':
3329 case 'F':
3330 /* Note that in the macro case, these arguments are already
3331 in MSB form. (When handling the instruction in the
3332 non-macro case, these arguments are sizes from which
3333 MSB values must be calculated.) */
bf12938e 3334 INSERT_OPERAND (INSMSB, insn, va_arg (args, int));
5f74bc13
CD
3335 continue;
3336
3337 case 'C':
3338 case 'G':
3339 case 'H':
3340 /* Note that in the macro case, these arguments are already
3341 in MSBD form. (When handling the instruction in the
3342 non-macro case, these arguments are sizes from which
3343 MSBD values must be calculated.) */
bf12938e 3344 INSERT_OPERAND (EXTMSBD, insn, va_arg (args, int));
5f74bc13
CD
3345 continue;
3346
3347 default:
3348 internalError ();
3349 }
3350 continue;
3351
252b5132
RH
3352 case 't':
3353 case 'w':
3354 case 'E':
bf12938e 3355 INSERT_OPERAND (RT, insn, va_arg (args, int));
252b5132
RH
3356 continue;
3357
3358 case 'c':
bf12938e 3359 INSERT_OPERAND (CODE, insn, va_arg (args, int));
38487616
TS
3360 continue;
3361
252b5132
RH
3362 case 'T':
3363 case 'W':
bf12938e 3364 INSERT_OPERAND (FT, insn, va_arg (args, int));
252b5132
RH
3365 continue;
3366
3367 case 'd':
3368 case 'G':
af7ee8bf 3369 case 'K':
bf12938e 3370 INSERT_OPERAND (RD, insn, va_arg (args, int));
252b5132
RH
3371 continue;
3372
4372b673
NC
3373 case 'U':
3374 {
3375 int tmp = va_arg (args, int);
3376
bf12938e
RS
3377 INSERT_OPERAND (RT, insn, tmp);
3378 INSERT_OPERAND (RD, insn, tmp);
beae10d5 3379 continue;
4372b673
NC
3380 }
3381
252b5132
RH
3382 case 'V':
3383 case 'S':
bf12938e 3384 INSERT_OPERAND (FS, insn, va_arg (args, int));
252b5132
RH
3385 continue;
3386
3387 case 'z':
3388 continue;
3389
3390 case '<':
bf12938e 3391 INSERT_OPERAND (SHAMT, insn, va_arg (args, int));
252b5132
RH
3392 continue;
3393
3394 case 'D':
bf12938e 3395 INSERT_OPERAND (FD, insn, va_arg (args, int));
252b5132
RH
3396 continue;
3397
3398 case 'B':
bf12938e 3399 INSERT_OPERAND (CODE20, insn, va_arg (args, int));
252b5132
RH
3400 continue;
3401
4372b673 3402 case 'J':
bf12938e 3403 INSERT_OPERAND (CODE19, insn, va_arg (args, int));
4372b673
NC
3404 continue;
3405
252b5132 3406 case 'q':
bf12938e 3407 INSERT_OPERAND (CODE2, insn, va_arg (args, int));
252b5132
RH
3408 continue;
3409
3410 case 'b':
3411 case 's':
3412 case 'r':
3413 case 'v':
bf12938e 3414 INSERT_OPERAND (RS, insn, va_arg (args, int));
252b5132
RH
3415 continue;
3416
3417 case 'i':
3418 case 'j':
3419 case 'o':
6e1304d8 3420 macro_read_relocs (&args, r);
cdf6fd85 3421 assert (*r == BFD_RELOC_GPREL16
f6688943
TS
3422 || *r == BFD_RELOC_MIPS_LITERAL
3423 || *r == BFD_RELOC_MIPS_HIGHER
3424 || *r == BFD_RELOC_HI16_S
3425 || *r == BFD_RELOC_LO16
3426 || *r == BFD_RELOC_MIPS_GOT16
3427 || *r == BFD_RELOC_MIPS_CALL16
438c16b8
TS
3428 || *r == BFD_RELOC_MIPS_GOT_DISP
3429 || *r == BFD_RELOC_MIPS_GOT_PAGE
3430 || *r == BFD_RELOC_MIPS_GOT_OFST
f6688943 3431 || *r == BFD_RELOC_MIPS_GOT_LO16
3e722fb5 3432 || *r == BFD_RELOC_MIPS_CALL_LO16);
252b5132
RH
3433 continue;
3434
3435 case 'u':
6e1304d8 3436 macro_read_relocs (&args, r);
252b5132
RH
3437 assert (ep != NULL
3438 && (ep->X_op == O_constant
3439 || (ep->X_op == O_symbol
f6688943
TS
3440 && (*r == BFD_RELOC_MIPS_HIGHEST
3441 || *r == BFD_RELOC_HI16_S
3442 || *r == BFD_RELOC_HI16
3443 || *r == BFD_RELOC_GPREL16
3444 || *r == BFD_RELOC_MIPS_GOT_HI16
3e722fb5 3445 || *r == BFD_RELOC_MIPS_CALL_HI16))));
252b5132
RH
3446 continue;
3447
3448 case 'p':
3449 assert (ep != NULL);
bad36eac 3450
252b5132
RH
3451 /*
3452 * This allows macro() to pass an immediate expression for
3453 * creating short branches without creating a symbol.
bad36eac
DJ
3454 *
3455 * We don't allow branch relaxation for these branches, as
3456 * they should only appear in ".set nomacro" anyway.
252b5132
RH
3457 */
3458 if (ep->X_op == O_constant)
3459 {
bad36eac
DJ
3460 if ((ep->X_add_number & 3) != 0)
3461 as_bad (_("branch to misaligned address (0x%lx)"),
3462 (unsigned long) ep->X_add_number);
3463 if ((ep->X_add_number + 0x20000) & ~0x3ffff)
3464 as_bad (_("branch address range overflow (0x%lx)"),
3465 (unsigned long) ep->X_add_number);
252b5132
RH
3466 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
3467 ep = NULL;
3468 }
3469 else
0b25d3e6 3470 *r = BFD_RELOC_16_PCREL_S2;
252b5132
RH
3471 continue;
3472
3473 case 'a':
3474 assert (ep != NULL);
f6688943 3475 *r = BFD_RELOC_MIPS_JMP;
252b5132
RH
3476 continue;
3477
3478 case 'C':
a9e24354 3479 INSERT_OPERAND (COPZ, insn, va_arg (args, unsigned long));
252b5132
RH
3480 continue;
3481
d43b4baf 3482 case 'k':
a9e24354 3483 INSERT_OPERAND (CACHE, insn, va_arg (args, unsigned long));
d43b4baf
TS
3484 continue;
3485
252b5132
RH
3486 default:
3487 internalError ();
3488 }
3489 break;
3490 }
3491 va_end (args);
f6688943 3492 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 3493
4d7206a2 3494 append_insn (&insn, ep, r);
252b5132
RH
3495}
3496
3497static void
67c0d1eb 3498mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
17a2f251 3499 va_list args)
252b5132 3500{
1e915849 3501 struct mips_opcode *mo;
252b5132 3502 struct mips_cl_insn insn;
f6688943
TS
3503 bfd_reloc_code_real_type r[3]
3504 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 3505
1e915849
RS
3506 mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
3507 assert (mo);
3508 assert (strcmp (name, mo->name) == 0);
252b5132 3509
1e915849 3510 while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO)
252b5132 3511 {
1e915849
RS
3512 ++mo;
3513 assert (mo->name);
3514 assert (strcmp (name, mo->name) == 0);
252b5132
RH
3515 }
3516
1e915849 3517 create_insn (&insn, mo);
252b5132
RH
3518 for (;;)
3519 {
3520 int c;
3521
3522 c = *fmt++;
3523 switch (c)
3524 {
3525 case '\0':
3526 break;
3527
3528 case ',':
3529 case '(':
3530 case ')':
3531 continue;
3532
3533 case 'y':
3534 case 'w':
bf12938e 3535 MIPS16_INSERT_OPERAND (RY, insn, va_arg (args, int));
252b5132
RH
3536 continue;
3537
3538 case 'x':
3539 case 'v':
bf12938e 3540 MIPS16_INSERT_OPERAND (RX, insn, va_arg (args, int));
252b5132
RH
3541 continue;
3542
3543 case 'z':
bf12938e 3544 MIPS16_INSERT_OPERAND (RZ, insn, va_arg (args, int));
252b5132
RH
3545 continue;
3546
3547 case 'Z':
bf12938e 3548 MIPS16_INSERT_OPERAND (MOVE32Z, insn, va_arg (args, int));
252b5132
RH
3549 continue;
3550
3551 case '0':
3552 case 'S':
3553 case 'P':
3554 case 'R':
3555 continue;
3556
3557 case 'X':
bf12938e 3558 MIPS16_INSERT_OPERAND (REGR32, insn, va_arg (args, int));
252b5132
RH
3559 continue;
3560
3561 case 'Y':
3562 {
3563 int regno;
3564
3565 regno = va_arg (args, int);
3566 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
a9e24354 3567 MIPS16_INSERT_OPERAND (REG32R, insn, regno);
252b5132
RH
3568 }
3569 continue;
3570
3571 case '<':
3572 case '>':
3573 case '4':
3574 case '5':
3575 case 'H':
3576 case 'W':
3577 case 'D':
3578 case 'j':
3579 case '8':
3580 case 'V':
3581 case 'C':
3582 case 'U':
3583 case 'k':
3584 case 'K':
3585 case 'p':
3586 case 'q':
3587 {
3588 assert (ep != NULL);
3589
3590 if (ep->X_op != O_constant)
874e8986 3591 *r = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
3592 else
3593 {
b34976b6
AM
3594 mips16_immed (NULL, 0, c, ep->X_add_number, FALSE, FALSE,
3595 FALSE, &insn.insn_opcode, &insn.use_extend,
c4e7957c 3596 &insn.extend);
252b5132 3597 ep = NULL;
f6688943 3598 *r = BFD_RELOC_UNUSED;
252b5132
RH
3599 }
3600 }
3601 continue;
3602
3603 case '6':
bf12938e 3604 MIPS16_INSERT_OPERAND (IMM6, insn, va_arg (args, int));
252b5132
RH
3605 continue;
3606 }
3607
3608 break;
3609 }
3610
f6688943 3611 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 3612
4d7206a2 3613 append_insn (&insn, ep, r);
252b5132
RH
3614}
3615
2051e8c4
MR
3616/*
3617 * Sign-extend 32-bit mode constants that have bit 31 set and all
3618 * higher bits unset.
3619 */
9f872bbe 3620static void
2051e8c4
MR
3621normalize_constant_expr (expressionS *ex)
3622{
9ee2a2d4 3623 if (ex->X_op == O_constant
2051e8c4
MR
3624 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
3625 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
3626 - 0x80000000);
3627}
3628
3629/*
3630 * Sign-extend 32-bit mode address offsets that have bit 31 set and
3631 * all higher bits unset.
3632 */
3633static void
3634normalize_address_expr (expressionS *ex)
3635{
3636 if (((ex->X_op == O_constant && HAVE_32BIT_ADDRESSES)
3637 || (ex->X_op == O_symbol && HAVE_32BIT_SYMBOLS))
3638 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
3639 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
3640 - 0x80000000);
3641}
3642
438c16b8
TS
3643/*
3644 * Generate a "jalr" instruction with a relocation hint to the called
3645 * function. This occurs in NewABI PIC code.
3646 */
3647static void
67c0d1eb 3648macro_build_jalr (expressionS *ep)
438c16b8 3649{
685736be 3650 char *f = NULL;
b34976b6 3651
438c16b8 3652 if (HAVE_NEWABI)
f21f8242 3653 {
cc3d92a5 3654 frag_grow (8);
f21f8242
AO
3655 f = frag_more (0);
3656 }
67c0d1eb 3657 macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
438c16b8 3658 if (HAVE_NEWABI)
f21f8242 3659 fix_new_exp (frag_now, f - frag_now->fr_literal,
a105a300 3660 4, ep, FALSE, BFD_RELOC_MIPS_JALR);
438c16b8
TS
3661}
3662
252b5132
RH
3663/*
3664 * Generate a "lui" instruction.
3665 */
3666static void
67c0d1eb 3667macro_build_lui (expressionS *ep, int regnum)
252b5132
RH
3668{
3669 expressionS high_expr;
1e915849 3670 const struct mips_opcode *mo;
252b5132 3671 struct mips_cl_insn insn;
f6688943
TS
3672 bfd_reloc_code_real_type r[3]
3673 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
5a38dc70
AM
3674 const char *name = "lui";
3675 const char *fmt = "t,u";
252b5132
RH
3676
3677 assert (! mips_opts.mips16);
3678
4d7206a2 3679 high_expr = *ep;
252b5132
RH
3680
3681 if (high_expr.X_op == O_constant)
3682 {
3683 /* we can compute the instruction now without a relocation entry */
e7d556df
TS
3684 high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3685 >> 16) & 0xffff;
f6688943 3686 *r = BFD_RELOC_UNUSED;
252b5132 3687 }
78e1bb40 3688 else
252b5132
RH
3689 {
3690 assert (ep->X_op == O_symbol);
bbe506e8
TS
3691 /* _gp_disp is a special case, used from s_cpload.
3692 __gnu_local_gp is used if mips_no_shared. */
252b5132 3693 assert (mips_pic == NO_PIC
78e1bb40 3694 || (! HAVE_NEWABI
aa6975fb
ILT
3695 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
3696 || (! mips_in_shared
bbe506e8
TS
3697 && strcmp (S_GET_NAME (ep->X_add_symbol),
3698 "__gnu_local_gp") == 0));
f6688943 3699 *r = BFD_RELOC_HI16_S;
252b5132
RH
3700 }
3701
1e915849
RS
3702 mo = hash_find (op_hash, name);
3703 assert (strcmp (name, mo->name) == 0);
3704 assert (strcmp (fmt, mo->args) == 0);
3705 create_insn (&insn, mo);
252b5132 3706
bf12938e
RS
3707 insn.insn_opcode = insn.insn_mo->match;
3708 INSERT_OPERAND (RT, insn, regnum);
f6688943 3709 if (*r == BFD_RELOC_UNUSED)
252b5132
RH
3710 {
3711 insn.insn_opcode |= high_expr.X_add_number;
4d7206a2 3712 append_insn (&insn, NULL, r);
252b5132
RH
3713 }
3714 else
4d7206a2 3715 append_insn (&insn, &high_expr, r);
252b5132
RH
3716}
3717
885add95
CD
3718/* Generate a sequence of instructions to do a load or store from a constant
3719 offset off of a base register (breg) into/from a target register (treg),
3720 using AT if necessary. */
3721static void
67c0d1eb
RS
3722macro_build_ldst_constoffset (expressionS *ep, const char *op,
3723 int treg, int breg, int dbl)
885add95
CD
3724{
3725 assert (ep->X_op == O_constant);
3726
256ab948 3727 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
3728 if (!dbl)
3729 normalize_constant_expr (ep);
256ab948 3730
67c1ffbe 3731 /* Right now, this routine can only handle signed 32-bit constants. */
ecd13cd3 3732 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
885add95
CD
3733 as_warn (_("operand overflow"));
3734
3735 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
3736 {
3737 /* Signed 16-bit offset will fit in the op. Easy! */
67c0d1eb 3738 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
885add95
CD
3739 }
3740 else
3741 {
3742 /* 32-bit offset, need multiple instructions and AT, like:
3743 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
3744 addu $tempreg,$tempreg,$breg
3745 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
3746 to handle the complete offset. */
67c0d1eb
RS
3747 macro_build_lui (ep, AT);
3748 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
3749 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
885add95
CD
3750
3751 if (mips_opts.noat)
8fc2e39e 3752 as_bad (_("Macro used $at after \".set noat\""));
885add95
CD
3753 }
3754}
3755
252b5132
RH
3756/* set_at()
3757 * Generates code to set the $at register to true (one)
3758 * if reg is less than the immediate expression.
3759 */
3760static void
67c0d1eb 3761set_at (int reg, int unsignedp)
252b5132
RH
3762{
3763 if (imm_expr.X_op == O_constant
3764 && imm_expr.X_add_number >= -0x8000
3765 && imm_expr.X_add_number < 0x8000)
67c0d1eb
RS
3766 macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
3767 AT, reg, BFD_RELOC_LO16);
252b5132
RH
3768 else
3769 {
67c0d1eb
RS
3770 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
3771 macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
252b5132
RH
3772 }
3773}
3774
3775/* Warn if an expression is not a constant. */
3776
3777static void
17a2f251 3778check_absolute_expr (struct mips_cl_insn *ip, expressionS *ex)
252b5132
RH
3779{
3780 if (ex->X_op == O_big)
3781 as_bad (_("unsupported large constant"));
3782 else if (ex->X_op != O_constant)
9ee2a2d4
MR
3783 as_bad (_("Instruction %s requires absolute expression"),
3784 ip->insn_mo->name);
13757d0c 3785
9ee2a2d4
MR
3786 if (HAVE_32BIT_GPRS)
3787 normalize_constant_expr (ex);
252b5132
RH
3788}
3789
3790/* Count the leading zeroes by performing a binary chop. This is a
3791 bulky bit of source, but performance is a LOT better for the
3792 majority of values than a simple loop to count the bits:
3793 for (lcnt = 0; (lcnt < 32); lcnt++)
3794 if ((v) & (1 << (31 - lcnt)))
3795 break;
3796 However it is not code size friendly, and the gain will drop a bit
3797 on certain cached systems.
3798*/
3799#define COUNT_TOP_ZEROES(v) \
3800 (((v) & ~0xffff) == 0 \
3801 ? ((v) & ~0xff) == 0 \
3802 ? ((v) & ~0xf) == 0 \
3803 ? ((v) & ~0x3) == 0 \
3804 ? ((v) & ~0x1) == 0 \
3805 ? !(v) \
3806 ? 32 \
3807 : 31 \
3808 : 30 \
3809 : ((v) & ~0x7) == 0 \
3810 ? 29 \
3811 : 28 \
3812 : ((v) & ~0x3f) == 0 \
3813 ? ((v) & ~0x1f) == 0 \
3814 ? 27 \
3815 : 26 \
3816 : ((v) & ~0x7f) == 0 \
3817 ? 25 \
3818 : 24 \
3819 : ((v) & ~0xfff) == 0 \
3820 ? ((v) & ~0x3ff) == 0 \
3821 ? ((v) & ~0x1ff) == 0 \
3822 ? 23 \
3823 : 22 \
3824 : ((v) & ~0x7ff) == 0 \
3825 ? 21 \
3826 : 20 \
3827 : ((v) & ~0x3fff) == 0 \
3828 ? ((v) & ~0x1fff) == 0 \
3829 ? 19 \
3830 : 18 \
3831 : ((v) & ~0x7fff) == 0 \
3832 ? 17 \
3833 : 16 \
3834 : ((v) & ~0xffffff) == 0 \
3835 ? ((v) & ~0xfffff) == 0 \
3836 ? ((v) & ~0x3ffff) == 0 \
3837 ? ((v) & ~0x1ffff) == 0 \
3838 ? 15 \
3839 : 14 \
3840 : ((v) & ~0x7ffff) == 0 \
3841 ? 13 \
3842 : 12 \
3843 : ((v) & ~0x3fffff) == 0 \
3844 ? ((v) & ~0x1fffff) == 0 \
3845 ? 11 \
3846 : 10 \
3847 : ((v) & ~0x7fffff) == 0 \
3848 ? 9 \
3849 : 8 \
3850 : ((v) & ~0xfffffff) == 0 \
3851 ? ((v) & ~0x3ffffff) == 0 \
3852 ? ((v) & ~0x1ffffff) == 0 \
3853 ? 7 \
3854 : 6 \
3855 : ((v) & ~0x7ffffff) == 0 \
3856 ? 5 \
3857 : 4 \
3858 : ((v) & ~0x3fffffff) == 0 \
3859 ? ((v) & ~0x1fffffff) == 0 \
3860 ? 3 \
3861 : 2 \
3862 : ((v) & ~0x7fffffff) == 0 \
3863 ? 1 \
3864 : 0)
3865
3866/* load_register()
67c1ffbe 3867 * This routine generates the least number of instructions necessary to load
252b5132
RH
3868 * an absolute expression value into a register.
3869 */
3870static void
67c0d1eb 3871load_register (int reg, expressionS *ep, int dbl)
252b5132
RH
3872{
3873 int freg;
3874 expressionS hi32, lo32;
3875
3876 if (ep->X_op != O_big)
3877 {
3878 assert (ep->X_op == O_constant);
256ab948
TS
3879
3880 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
3881 if (!dbl)
3882 normalize_constant_expr (ep);
256ab948
TS
3883
3884 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
252b5132
RH
3885 {
3886 /* We can handle 16 bit signed values with an addiu to
3887 $zero. No need to ever use daddiu here, since $zero and
3888 the result are always correct in 32 bit mode. */
67c0d1eb 3889 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
3890 return;
3891 }
3892 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3893 {
3894 /* We can handle 16 bit unsigned values with an ori to
3895 $zero. */
67c0d1eb 3896 macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
252b5132
RH
3897 return;
3898 }
256ab948 3899 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
252b5132
RH
3900 {
3901 /* 32 bit values require an lui. */
67c0d1eb 3902 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_HI16);
252b5132 3903 if ((ep->X_add_number & 0xffff) != 0)
67c0d1eb 3904 macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
252b5132
RH
3905 return;
3906 }
3907 }
3908
3909 /* The value is larger than 32 bits. */
3910
2051e8c4 3911 if (!dbl || HAVE_32BIT_GPRS)
252b5132 3912 {
55e08f71
NC
3913 char value[32];
3914
3915 sprintf_vma (value, ep->X_add_number);
20e1fcfd 3916 as_bad (_("Number (0x%s) larger than 32 bits"), value);
67c0d1eb 3917 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
3918 return;
3919 }
3920
3921 if (ep->X_op != O_big)
3922 {
3923 hi32 = *ep;
3924 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3925 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3926 hi32.X_add_number &= 0xffffffff;
3927 lo32 = *ep;
3928 lo32.X_add_number &= 0xffffffff;
3929 }
3930 else
3931 {
3932 assert (ep->X_add_number > 2);
3933 if (ep->X_add_number == 3)
3934 generic_bignum[3] = 0;
3935 else if (ep->X_add_number > 4)
3936 as_bad (_("Number larger than 64 bits"));
3937 lo32.X_op = O_constant;
3938 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3939 hi32.X_op = O_constant;
3940 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3941 }
3942
3943 if (hi32.X_add_number == 0)
3944 freg = 0;
3945 else
3946 {
3947 int shift, bit;
3948 unsigned long hi, lo;
3949
956cd1d6 3950 if (hi32.X_add_number == (offsetT) 0xffffffff)
beae10d5
KH
3951 {
3952 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3953 {
67c0d1eb 3954 macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
3955 return;
3956 }
3957 if (lo32.X_add_number & 0x80000000)
3958 {
67c0d1eb 3959 macro_build (&lo32, "lui", "t,u", reg, BFD_RELOC_HI16);
252b5132 3960 if (lo32.X_add_number & 0xffff)
67c0d1eb 3961 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
beae10d5
KH
3962 return;
3963 }
3964 }
252b5132
RH
3965
3966 /* Check for 16bit shifted constant. We know that hi32 is
3967 non-zero, so start the mask on the first bit of the hi32
3968 value. */
3969 shift = 17;
3970 do
beae10d5
KH
3971 {
3972 unsigned long himask, lomask;
3973
3974 if (shift < 32)
3975 {
3976 himask = 0xffff >> (32 - shift);
3977 lomask = (0xffff << shift) & 0xffffffff;
3978 }
3979 else
3980 {
3981 himask = 0xffff << (shift - 32);
3982 lomask = 0;
3983 }
3984 if ((hi32.X_add_number & ~(offsetT) himask) == 0
3985 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3986 {
3987 expressionS tmp;
3988
3989 tmp.X_op = O_constant;
3990 if (shift < 32)
3991 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3992 | (lo32.X_add_number >> shift));
3993 else
3994 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
67c0d1eb
RS
3995 macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
3996 macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", "d,w,<",
3997 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
3998 return;
3999 }
f9419b05 4000 ++shift;
beae10d5
KH
4001 }
4002 while (shift <= (64 - 16));
252b5132
RH
4003
4004 /* Find the bit number of the lowest one bit, and store the
4005 shifted value in hi/lo. */
4006 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
4007 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
4008 if (lo != 0)
4009 {
4010 bit = 0;
4011 while ((lo & 1) == 0)
4012 {
4013 lo >>= 1;
4014 ++bit;
4015 }
4016 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
4017 hi >>= bit;
4018 }
4019 else
4020 {
4021 bit = 32;
4022 while ((hi & 1) == 0)
4023 {
4024 hi >>= 1;
4025 ++bit;
4026 }
4027 lo = hi;
4028 hi = 0;
4029 }
4030
4031 /* Optimize if the shifted value is a (power of 2) - 1. */
4032 if ((hi == 0 && ((lo + 1) & lo) == 0)
4033 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
beae10d5
KH
4034 {
4035 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
252b5132 4036 if (shift != 0)
beae10d5 4037 {
252b5132
RH
4038 expressionS tmp;
4039
4040 /* This instruction will set the register to be all
4041 ones. */
beae10d5
KH
4042 tmp.X_op = O_constant;
4043 tmp.X_add_number = (offsetT) -1;
67c0d1eb 4044 macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
4045 if (bit != 0)
4046 {
4047 bit += shift;
67c0d1eb
RS
4048 macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", "d,w,<",
4049 reg, reg, (bit >= 32) ? bit - 32 : bit);
beae10d5 4050 }
67c0d1eb
RS
4051 macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", "d,w,<",
4052 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
4053 return;
4054 }
4055 }
252b5132
RH
4056
4057 /* Sign extend hi32 before calling load_register, because we can
4058 generally get better code when we load a sign extended value. */
4059 if ((hi32.X_add_number & 0x80000000) != 0)
beae10d5 4060 hi32.X_add_number |= ~(offsetT) 0xffffffff;
67c0d1eb 4061 load_register (reg, &hi32, 0);
252b5132
RH
4062 freg = reg;
4063 }
4064 if ((lo32.X_add_number & 0xffff0000) == 0)
4065 {
4066 if (freg != 0)
4067 {
67c0d1eb 4068 macro_build (NULL, "dsll32", "d,w,<", reg, freg, 0);
252b5132
RH
4069 freg = reg;
4070 }
4071 }
4072 else
4073 {
4074 expressionS mid16;
4075
956cd1d6 4076 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
beae10d5 4077 {
67c0d1eb
RS
4078 macro_build (&lo32, "lui", "t,u", reg, BFD_RELOC_HI16);
4079 macro_build (NULL, "dsrl32", "d,w,<", reg, reg, 0);
beae10d5
KH
4080 return;
4081 }
252b5132
RH
4082
4083 if (freg != 0)
4084 {
67c0d1eb 4085 macro_build (NULL, "dsll", "d,w,<", reg, freg, 16);
252b5132
RH
4086 freg = reg;
4087 }
4088 mid16 = lo32;
4089 mid16.X_add_number >>= 16;
67c0d1eb
RS
4090 macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
4091 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
252b5132
RH
4092 freg = reg;
4093 }
4094 if ((lo32.X_add_number & 0xffff) != 0)
67c0d1eb 4095 macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
252b5132
RH
4096}
4097
269137b2
TS
4098static inline void
4099load_delay_nop (void)
4100{
4101 if (!gpr_interlocks)
4102 macro_build (NULL, "nop", "");
4103}
4104
252b5132
RH
4105/* Load an address into a register. */
4106
4107static void
67c0d1eb 4108load_address (int reg, expressionS *ep, int *used_at)
252b5132 4109{
252b5132
RH
4110 if (ep->X_op != O_constant
4111 && ep->X_op != O_symbol)
4112 {
4113 as_bad (_("expression too complex"));
4114 ep->X_op = O_constant;
4115 }
4116
4117 if (ep->X_op == O_constant)
4118 {
67c0d1eb 4119 load_register (reg, ep, HAVE_64BIT_ADDRESSES);
252b5132
RH
4120 return;
4121 }
4122
4123 if (mips_pic == NO_PIC)
4124 {
4125 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 4126 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
4127 Otherwise we want
4128 lui $reg,<sym> (BFD_RELOC_HI16_S)
4129 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
d6bc6245 4130 If we have an addend, we always use the latter form.
76b3015f 4131
d6bc6245
TS
4132 With 64bit address space and a usable $at we want
4133 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4134 lui $at,<sym> (BFD_RELOC_HI16_S)
4135 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
4136 daddiu $at,<sym> (BFD_RELOC_LO16)
4137 dsll32 $reg,0
3a482fd5 4138 daddu $reg,$reg,$at
76b3015f 4139
c03099e6 4140 If $at is already in use, we use a path which is suboptimal
d6bc6245
TS
4141 on superscalar processors.
4142 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4143 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
4144 dsll $reg,16
4145 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
4146 dsll $reg,16
4147 daddiu $reg,<sym> (BFD_RELOC_LO16)
6caf9ef4
TS
4148
4149 For GP relative symbols in 64bit address space we can use
4150 the same sequence as in 32bit address space. */
aed1a261 4151 if (HAVE_64BIT_SYMBOLS)
d6bc6245 4152 {
6caf9ef4
TS
4153 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
4154 && !nopic_need_relax (ep->X_add_symbol, 1))
4155 {
4156 relax_start (ep->X_add_symbol);
4157 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
4158 mips_gp_register, BFD_RELOC_GPREL16);
4159 relax_switch ();
4160 }
d6bc6245 4161
b8285c27 4162 if (*used_at == 0 && !mips_opts.noat)
d6bc6245 4163 {
67c0d1eb
RS
4164 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_HIGHEST);
4165 macro_build (ep, "lui", "t,u", AT, BFD_RELOC_HI16_S);
4166 macro_build (ep, "daddiu", "t,r,j", reg, reg,
4167 BFD_RELOC_MIPS_HIGHER);
4168 macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
4169 macro_build (NULL, "dsll32", "d,w,<", reg, reg, 0);
4170 macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
d6bc6245
TS
4171 *used_at = 1;
4172 }
4173 else
4174 {
67c0d1eb
RS
4175 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_HIGHEST);
4176 macro_build (ep, "daddiu", "t,r,j", reg, reg,
4177 BFD_RELOC_MIPS_HIGHER);
4178 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
4179 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
4180 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
4181 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
d6bc6245 4182 }
6caf9ef4
TS
4183
4184 if (mips_relax.sequence)
4185 relax_end ();
d6bc6245 4186 }
252b5132
RH
4187 else
4188 {
d6bc6245 4189 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 4190 && !nopic_need_relax (ep->X_add_symbol, 1))
d6bc6245 4191 {
4d7206a2 4192 relax_start (ep->X_add_symbol);
67c0d1eb 4193 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
17a2f251 4194 mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 4195 relax_switch ();
d6bc6245 4196 }
67c0d1eb
RS
4197 macro_build_lui (ep, reg);
4198 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
4199 reg, reg, BFD_RELOC_LO16);
4d7206a2
RS
4200 if (mips_relax.sequence)
4201 relax_end ();
d6bc6245 4202 }
252b5132 4203 }
0a44bf69 4204 else if (!mips_big_got)
252b5132
RH
4205 {
4206 expressionS ex;
4207
4208 /* If this is a reference to an external symbol, we want
4209 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4210 Otherwise we want
4211 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4212 nop
4213 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
f5040a92
AO
4214 If there is a constant, it must be added in after.
4215
ed6fb7bd 4216 If we have NewABI, we want
f5040a92
AO
4217 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
4218 unless we're referencing a global symbol with a non-zero
4219 offset, in which case cst must be added separately. */
ed6fb7bd
SC
4220 if (HAVE_NEWABI)
4221 {
f5040a92
AO
4222 if (ep->X_add_number)
4223 {
4d7206a2 4224 ex.X_add_number = ep->X_add_number;
f5040a92 4225 ep->X_add_number = 0;
4d7206a2 4226 relax_start (ep->X_add_symbol);
67c0d1eb
RS
4227 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
4228 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
4229 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4230 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4231 ex.X_op = O_constant;
67c0d1eb 4232 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 4233 reg, reg, BFD_RELOC_LO16);
f5040a92 4234 ep->X_add_number = ex.X_add_number;
4d7206a2 4235 relax_switch ();
f5040a92 4236 }
67c0d1eb 4237 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 4238 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2
RS
4239 if (mips_relax.sequence)
4240 relax_end ();
ed6fb7bd
SC
4241 }
4242 else
4243 {
f5040a92
AO
4244 ex.X_add_number = ep->X_add_number;
4245 ep->X_add_number = 0;
67c0d1eb
RS
4246 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
4247 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 4248 load_delay_nop ();
4d7206a2
RS
4249 relax_start (ep->X_add_symbol);
4250 relax_switch ();
67c0d1eb 4251 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 4252 BFD_RELOC_LO16);
4d7206a2 4253 relax_end ();
ed6fb7bd 4254
f5040a92
AO
4255 if (ex.X_add_number != 0)
4256 {
4257 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4258 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4259 ex.X_op = O_constant;
67c0d1eb 4260 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 4261 reg, reg, BFD_RELOC_LO16);
f5040a92 4262 }
252b5132
RH
4263 }
4264 }
0a44bf69 4265 else if (mips_big_got)
252b5132
RH
4266 {
4267 expressionS ex;
252b5132
RH
4268
4269 /* This is the large GOT case. If this is a reference to an
4270 external symbol, we want
4271 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
4272 addu $reg,$reg,$gp
4273 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
f5040a92
AO
4274
4275 Otherwise, for a reference to a local symbol in old ABI, we want
252b5132
RH
4276 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4277 nop
4278 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
684022ea 4279 If there is a constant, it must be added in after.
f5040a92
AO
4280
4281 In the NewABI, for local symbols, with or without offsets, we want:
438c16b8
TS
4282 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
4283 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
f5040a92 4284 */
438c16b8
TS
4285 if (HAVE_NEWABI)
4286 {
4d7206a2 4287 ex.X_add_number = ep->X_add_number;
f5040a92 4288 ep->X_add_number = 0;
4d7206a2 4289 relax_start (ep->X_add_symbol);
67c0d1eb
RS
4290 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_GOT_HI16);
4291 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
4292 reg, reg, mips_gp_register);
4293 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
4294 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
f5040a92
AO
4295 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4296 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4297 else if (ex.X_add_number)
4298 {
4299 ex.X_op = O_constant;
67c0d1eb
RS
4300 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
4301 BFD_RELOC_LO16);
f5040a92
AO
4302 }
4303
4304 ep->X_add_number = ex.X_add_number;
4d7206a2 4305 relax_switch ();
67c0d1eb 4306 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 4307 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
67c0d1eb
RS
4308 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
4309 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 4310 relax_end ();
438c16b8 4311 }
252b5132 4312 else
438c16b8 4313 {
f5040a92
AO
4314 ex.X_add_number = ep->X_add_number;
4315 ep->X_add_number = 0;
4d7206a2 4316 relax_start (ep->X_add_symbol);
67c0d1eb
RS
4317 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_GOT_HI16);
4318 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
4319 reg, reg, mips_gp_register);
4320 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
4321 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
4d7206a2
RS
4322 relax_switch ();
4323 if (reg_needs_delay (mips_gp_register))
438c16b8
TS
4324 {
4325 /* We need a nop before loading from $gp. This special
4326 check is required because the lui which starts the main
4327 instruction stream does not refer to $gp, and so will not
4328 insert the nop which may be required. */
67c0d1eb 4329 macro_build (NULL, "nop", "");
438c16b8 4330 }
67c0d1eb 4331 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 4332 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 4333 load_delay_nop ();
67c0d1eb 4334 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 4335 BFD_RELOC_LO16);
4d7206a2 4336 relax_end ();
438c16b8 4337
f5040a92
AO
4338 if (ex.X_add_number != 0)
4339 {
4340 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4341 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4342 ex.X_op = O_constant;
67c0d1eb
RS
4343 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
4344 BFD_RELOC_LO16);
f5040a92 4345 }
252b5132
RH
4346 }
4347 }
252b5132
RH
4348 else
4349 abort ();
8fc2e39e
TS
4350
4351 if (mips_opts.noat && *used_at == 1)
4352 as_bad (_("Macro used $at after \".set noat\""));
252b5132
RH
4353}
4354
ea1fb5dc
RS
4355/* Move the contents of register SOURCE into register DEST. */
4356
4357static void
67c0d1eb 4358move_register (int dest, int source)
ea1fb5dc 4359{
67c0d1eb
RS
4360 macro_build (NULL, HAVE_32BIT_GPRS ? "addu" : "daddu", "d,v,t",
4361 dest, source, 0);
ea1fb5dc
RS
4362}
4363
4d7206a2 4364/* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
f6a22291
MR
4365 LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
4366 The two alternatives are:
4d7206a2
RS
4367
4368 Global symbol Local sybmol
4369 ------------- ------------
4370 lw DEST,%got(SYMBOL) lw DEST,%got(SYMBOL + OFFSET)
4371 ... ...
4372 addiu DEST,DEST,OFFSET addiu DEST,DEST,%lo(SYMBOL + OFFSET)
4373
4374 load_got_offset emits the first instruction and add_got_offset
f6a22291
MR
4375 emits the second for a 16-bit offset or add_got_offset_hilo emits
4376 a sequence to add a 32-bit offset using a scratch register. */
4d7206a2
RS
4377
4378static void
67c0d1eb 4379load_got_offset (int dest, expressionS *local)
4d7206a2
RS
4380{
4381 expressionS global;
4382
4383 global = *local;
4384 global.X_add_number = 0;
4385
4386 relax_start (local->X_add_symbol);
67c0d1eb
RS
4387 macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
4388 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2 4389 relax_switch ();
67c0d1eb
RS
4390 macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
4391 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2
RS
4392 relax_end ();
4393}
4394
4395static void
67c0d1eb 4396add_got_offset (int dest, expressionS *local)
4d7206a2
RS
4397{
4398 expressionS global;
4399
4400 global.X_op = O_constant;
4401 global.X_op_symbol = NULL;
4402 global.X_add_symbol = NULL;
4403 global.X_add_number = local->X_add_number;
4404
4405 relax_start (local->X_add_symbol);
67c0d1eb 4406 macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
4d7206a2
RS
4407 dest, dest, BFD_RELOC_LO16);
4408 relax_switch ();
67c0d1eb 4409 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
4d7206a2
RS
4410 relax_end ();
4411}
4412
f6a22291
MR
4413static void
4414add_got_offset_hilo (int dest, expressionS *local, int tmp)
4415{
4416 expressionS global;
4417 int hold_mips_optimize;
4418
4419 global.X_op = O_constant;
4420 global.X_op_symbol = NULL;
4421 global.X_add_symbol = NULL;
4422 global.X_add_number = local->X_add_number;
4423
4424 relax_start (local->X_add_symbol);
4425 load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
4426 relax_switch ();
4427 /* Set mips_optimize around the lui instruction to avoid
4428 inserting an unnecessary nop after the lw. */
4429 hold_mips_optimize = mips_optimize;
4430 mips_optimize = 2;
4431 macro_build_lui (&global, tmp);
4432 mips_optimize = hold_mips_optimize;
4433 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
4434 relax_end ();
4435
4436 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
4437}
4438
252b5132
RH
4439/*
4440 * Build macros
4441 * This routine implements the seemingly endless macro or synthesized
4442 * instructions and addressing modes in the mips assembly language. Many
4443 * of these macros are simple and are similar to each other. These could
67c1ffbe 4444 * probably be handled by some kind of table or grammar approach instead of
252b5132
RH
4445 * this verbose method. Others are not simple macros but are more like
4446 * optimizing code generation.
4447 * One interesting optimization is when several store macros appear
67c1ffbe 4448 * consecutively that would load AT with the upper half of the same address.
252b5132
RH
4449 * The ensuing load upper instructions are ommited. This implies some kind
4450 * of global optimization. We currently only optimize within a single macro.
4451 * For many of the load and store macros if the address is specified as a
4452 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
4453 * first load register 'at' with zero and use it as the base register. The
4454 * mips assembler simply uses register $zero. Just one tiny optimization
4455 * we're missing.
4456 */
4457static void
17a2f251 4458macro (struct mips_cl_insn *ip)
252b5132 4459{
3994f87e 4460 int treg, sreg, dreg, breg;
252b5132
RH
4461 int tempreg;
4462 int mask;
43841e91 4463 int used_at = 0;
252b5132
RH
4464 expressionS expr1;
4465 const char *s;
4466 const char *s2;
4467 const char *fmt;
4468 int likely = 0;
4469 int dbl = 0;
4470 int coproc = 0;
4471 int lr = 0;
4472 int imm = 0;
1abe91b1 4473 int call = 0;
252b5132 4474 int off;
67c0d1eb 4475 offsetT maxnum;
252b5132 4476 bfd_reloc_code_real_type r;
252b5132
RH
4477 int hold_mips_optimize;
4478
4479 assert (! mips_opts.mips16);
4480
4481 treg = (ip->insn_opcode >> 16) & 0x1f;
4482 dreg = (ip->insn_opcode >> 11) & 0x1f;
4483 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
4484 mask = ip->insn_mo->mask;
4485
4486 expr1.X_op = O_constant;
4487 expr1.X_op_symbol = NULL;
4488 expr1.X_add_symbol = NULL;
4489 expr1.X_add_number = 1;
4490
4491 switch (mask)
4492 {
4493 case M_DABS:
4494 dbl = 1;
4495 case M_ABS:
4496 /* bgez $a0,.+12
4497 move v0,$a0
4498 sub v0,$zero,$a0
4499 */
4500
7d10b47d 4501 start_noreorder ();
252b5132
RH
4502
4503 expr1.X_add_number = 8;
67c0d1eb 4504 macro_build (&expr1, "bgez", "s,p", sreg);
252b5132 4505 if (dreg == sreg)
67c0d1eb 4506 macro_build (NULL, "nop", "", 0);
252b5132 4507 else
67c0d1eb
RS
4508 move_register (dreg, sreg);
4509 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
252b5132 4510
7d10b47d 4511 end_noreorder ();
8fc2e39e 4512 break;
252b5132
RH
4513
4514 case M_ADD_I:
4515 s = "addi";
4516 s2 = "add";
4517 goto do_addi;
4518 case M_ADDU_I:
4519 s = "addiu";
4520 s2 = "addu";
4521 goto do_addi;
4522 case M_DADD_I:
4523 dbl = 1;
4524 s = "daddi";
4525 s2 = "dadd";
4526 goto do_addi;
4527 case M_DADDU_I:
4528 dbl = 1;
4529 s = "daddiu";
4530 s2 = "daddu";
4531 do_addi:
4532 if (imm_expr.X_op == O_constant
4533 && imm_expr.X_add_number >= -0x8000
4534 && imm_expr.X_add_number < 0x8000)
4535 {
67c0d1eb 4536 macro_build (&imm_expr, s, "t,r,j", treg, sreg, BFD_RELOC_LO16);
8fc2e39e 4537 break;
252b5132 4538 }
8fc2e39e 4539 used_at = 1;
67c0d1eb
RS
4540 load_register (AT, &imm_expr, dbl);
4541 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
252b5132
RH
4542 break;
4543
4544 case M_AND_I:
4545 s = "andi";
4546 s2 = "and";
4547 goto do_bit;
4548 case M_OR_I:
4549 s = "ori";
4550 s2 = "or";
4551 goto do_bit;
4552 case M_NOR_I:
4553 s = "";
4554 s2 = "nor";
4555 goto do_bit;
4556 case M_XOR_I:
4557 s = "xori";
4558 s2 = "xor";
4559 do_bit:
4560 if (imm_expr.X_op == O_constant
4561 && imm_expr.X_add_number >= 0
4562 && imm_expr.X_add_number < 0x10000)
4563 {
4564 if (mask != M_NOR_I)
67c0d1eb 4565 macro_build (&imm_expr, s, "t,r,i", treg, sreg, BFD_RELOC_LO16);
252b5132
RH
4566 else
4567 {
67c0d1eb
RS
4568 macro_build (&imm_expr, "ori", "t,r,i",
4569 treg, sreg, BFD_RELOC_LO16);
4570 macro_build (NULL, "nor", "d,v,t", treg, treg, 0);
252b5132 4571 }
8fc2e39e 4572 break;
252b5132
RH
4573 }
4574
8fc2e39e 4575 used_at = 1;
67c0d1eb
RS
4576 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
4577 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
252b5132
RH
4578 break;
4579
4580 case M_BEQ_I:
4581 s = "beq";
4582 goto beq_i;
4583 case M_BEQL_I:
4584 s = "beql";
4585 likely = 1;
4586 goto beq_i;
4587 case M_BNE_I:
4588 s = "bne";
4589 goto beq_i;
4590 case M_BNEL_I:
4591 s = "bnel";
4592 likely = 1;
4593 beq_i:
4594 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4595 {
67c0d1eb 4596 macro_build (&offset_expr, s, "s,t,p", sreg, 0);
8fc2e39e 4597 break;
252b5132 4598 }
8fc2e39e 4599 used_at = 1;
67c0d1eb
RS
4600 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
4601 macro_build (&offset_expr, s, "s,t,p", sreg, AT);
252b5132
RH
4602 break;
4603
4604 case M_BGEL:
4605 likely = 1;
4606 case M_BGE:
4607 if (treg == 0)
4608 {
67c0d1eb 4609 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", sreg);
8fc2e39e 4610 break;
252b5132
RH
4611 }
4612 if (sreg == 0)
4613 {
67c0d1eb 4614 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", treg);
8fc2e39e 4615 break;
252b5132 4616 }
8fc2e39e 4617 used_at = 1;
67c0d1eb
RS
4618 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
4619 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4620 break;
4621
4622 case M_BGTL_I:
4623 likely = 1;
4624 case M_BGT_I:
4625 /* check for > max integer */
4626 maxnum = 0x7fffffff;
ca4e0257 4627 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
4628 {
4629 maxnum <<= 16;
4630 maxnum |= 0xffff;
4631 maxnum <<= 16;
4632 maxnum |= 0xffff;
4633 }
4634 if (imm_expr.X_op == O_constant
4635 && imm_expr.X_add_number >= maxnum
ca4e0257 4636 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
4637 {
4638 do_false:
4639 /* result is always false */
4640 if (! likely)
67c0d1eb 4641 macro_build (NULL, "nop", "", 0);
252b5132 4642 else
67c0d1eb 4643 macro_build (&offset_expr, "bnel", "s,t,p", 0, 0);
8fc2e39e 4644 break;
252b5132
RH
4645 }
4646 if (imm_expr.X_op != O_constant)
4647 as_bad (_("Unsupported large constant"));
f9419b05 4648 ++imm_expr.X_add_number;
252b5132
RH
4649 /* FALLTHROUGH */
4650 case M_BGE_I:
4651 case M_BGEL_I:
4652 if (mask == M_BGEL_I)
4653 likely = 1;
4654 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4655 {
67c0d1eb 4656 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", sreg);
8fc2e39e 4657 break;
252b5132
RH
4658 }
4659 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4660 {
67c0d1eb 4661 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", sreg);
8fc2e39e 4662 break;
252b5132
RH
4663 }
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 maxnum = - maxnum - 1;
4673 if (imm_expr.X_op == O_constant
4674 && imm_expr.X_add_number <= maxnum
ca4e0257 4675 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
4676 {
4677 do_true:
4678 /* result is always true */
4679 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
67c0d1eb 4680 macro_build (&offset_expr, "b", "p");
8fc2e39e 4681 break;
252b5132 4682 }
8fc2e39e 4683 used_at = 1;
67c0d1eb
RS
4684 set_at (sreg, 0);
4685 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4686 break;
4687
4688 case M_BGEUL:
4689 likely = 1;
4690 case M_BGEU:
4691 if (treg == 0)
4692 goto do_true;
4693 if (sreg == 0)
4694 {
67c0d1eb 4695 macro_build (&offset_expr, likely ? "beql" : "beq",
17a2f251 4696 "s,t,p", 0, treg);
8fc2e39e 4697 break;
252b5132 4698 }
8fc2e39e 4699 used_at = 1;
67c0d1eb
RS
4700 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
4701 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4702 break;
4703
4704 case M_BGTUL_I:
4705 likely = 1;
4706 case M_BGTU_I:
4707 if (sreg == 0
ca4e0257 4708 || (HAVE_32BIT_GPRS
252b5132 4709 && imm_expr.X_op == O_constant
956cd1d6 4710 && imm_expr.X_add_number == (offsetT) 0xffffffff))
252b5132
RH
4711 goto do_false;
4712 if (imm_expr.X_op != O_constant)
4713 as_bad (_("Unsupported large constant"));
f9419b05 4714 ++imm_expr.X_add_number;
252b5132
RH
4715 /* FALLTHROUGH */
4716 case M_BGEU_I:
4717 case M_BGEUL_I:
4718 if (mask == M_BGEUL_I)
4719 likely = 1;
4720 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4721 goto do_true;
4722 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4723 {
67c0d1eb 4724 macro_build (&offset_expr, likely ? "bnel" : "bne",
17a2f251 4725 "s,t,p", sreg, 0);
8fc2e39e 4726 break;
252b5132 4727 }
8fc2e39e 4728 used_at = 1;
67c0d1eb
RS
4729 set_at (sreg, 1);
4730 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4731 break;
4732
4733 case M_BGTL:
4734 likely = 1;
4735 case M_BGT:
4736 if (treg == 0)
4737 {
67c0d1eb 4738 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", sreg);
8fc2e39e 4739 break;
252b5132
RH
4740 }
4741 if (sreg == 0)
4742 {
67c0d1eb 4743 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", treg);
8fc2e39e 4744 break;
252b5132 4745 }
8fc2e39e 4746 used_at = 1;
67c0d1eb
RS
4747 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
4748 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4749 break;
4750
4751 case M_BGTUL:
4752 likely = 1;
4753 case M_BGTU:
4754 if (treg == 0)
4755 {
67c0d1eb 4756 macro_build (&offset_expr, likely ? "bnel" : "bne",
17a2f251 4757 "s,t,p", sreg, 0);
8fc2e39e 4758 break;
252b5132
RH
4759 }
4760 if (sreg == 0)
4761 goto do_false;
8fc2e39e 4762 used_at = 1;
67c0d1eb
RS
4763 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
4764 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4765 break;
4766
4767 case M_BLEL:
4768 likely = 1;
4769 case M_BLE:
4770 if (treg == 0)
4771 {
67c0d1eb 4772 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", sreg);
8fc2e39e 4773 break;
252b5132
RH
4774 }
4775 if (sreg == 0)
4776 {
67c0d1eb 4777 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", treg);
8fc2e39e 4778 break;
252b5132 4779 }
8fc2e39e 4780 used_at = 1;
67c0d1eb
RS
4781 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
4782 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4783 break;
4784
4785 case M_BLEL_I:
4786 likely = 1;
4787 case M_BLE_I:
4788 maxnum = 0x7fffffff;
ca4e0257 4789 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
4790 {
4791 maxnum <<= 16;
4792 maxnum |= 0xffff;
4793 maxnum <<= 16;
4794 maxnum |= 0xffff;
4795 }
4796 if (imm_expr.X_op == O_constant
4797 && imm_expr.X_add_number >= maxnum
ca4e0257 4798 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
4799 goto do_true;
4800 if (imm_expr.X_op != O_constant)
4801 as_bad (_("Unsupported large constant"));
f9419b05 4802 ++imm_expr.X_add_number;
252b5132
RH
4803 /* FALLTHROUGH */
4804 case M_BLT_I:
4805 case M_BLTL_I:
4806 if (mask == M_BLTL_I)
4807 likely = 1;
4808 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4809 {
67c0d1eb 4810 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", sreg);
8fc2e39e 4811 break;
252b5132
RH
4812 }
4813 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4814 {
67c0d1eb 4815 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", sreg);
8fc2e39e 4816 break;
252b5132 4817 }
8fc2e39e 4818 used_at = 1;
67c0d1eb
RS
4819 set_at (sreg, 0);
4820 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4821 break;
4822
4823 case M_BLEUL:
4824 likely = 1;
4825 case M_BLEU:
4826 if (treg == 0)
4827 {
67c0d1eb 4828 macro_build (&offset_expr, likely ? "beql" : "beq",
17a2f251 4829 "s,t,p", sreg, 0);
8fc2e39e 4830 break;
252b5132
RH
4831 }
4832 if (sreg == 0)
4833 goto do_true;
8fc2e39e 4834 used_at = 1;
67c0d1eb
RS
4835 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
4836 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4837 break;
4838
4839 case M_BLEUL_I:
4840 likely = 1;
4841 case M_BLEU_I:
4842 if (sreg == 0
ca4e0257 4843 || (HAVE_32BIT_GPRS
252b5132 4844 && imm_expr.X_op == O_constant
956cd1d6 4845 && imm_expr.X_add_number == (offsetT) 0xffffffff))
252b5132
RH
4846 goto do_true;
4847 if (imm_expr.X_op != O_constant)
4848 as_bad (_("Unsupported large constant"));
f9419b05 4849 ++imm_expr.X_add_number;
252b5132
RH
4850 /* FALLTHROUGH */
4851 case M_BLTU_I:
4852 case M_BLTUL_I:
4853 if (mask == M_BLTUL_I)
4854 likely = 1;
4855 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4856 goto do_false;
4857 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4858 {
67c0d1eb 4859 macro_build (&offset_expr, likely ? "beql" : "beq",
252b5132 4860 "s,t,p", sreg, 0);
8fc2e39e 4861 break;
252b5132 4862 }
8fc2e39e 4863 used_at = 1;
67c0d1eb
RS
4864 set_at (sreg, 1);
4865 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4866 break;
4867
4868 case M_BLTL:
4869 likely = 1;
4870 case M_BLT:
4871 if (treg == 0)
4872 {
67c0d1eb 4873 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", sreg);
8fc2e39e 4874 break;
252b5132
RH
4875 }
4876 if (sreg == 0)
4877 {
67c0d1eb 4878 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", treg);
8fc2e39e 4879 break;
252b5132 4880 }
8fc2e39e 4881 used_at = 1;
67c0d1eb
RS
4882 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
4883 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4884 break;
4885
4886 case M_BLTUL:
4887 likely = 1;
4888 case M_BLTU:
4889 if (treg == 0)
4890 goto do_false;
4891 if (sreg == 0)
4892 {
67c0d1eb 4893 macro_build (&offset_expr, likely ? "bnel" : "bne",
17a2f251 4894 "s,t,p", 0, treg);
8fc2e39e 4895 break;
252b5132 4896 }
8fc2e39e 4897 used_at = 1;
67c0d1eb
RS
4898 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
4899 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4900 break;
4901
5f74bc13
CD
4902 case M_DEXT:
4903 {
4904 unsigned long pos;
4905 unsigned long size;
4906
4907 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
4908 {
4909 as_bad (_("Unsupported large constant"));
4910 pos = size = 1;
4911 }
4912 else
4913 {
4914 pos = (unsigned long) imm_expr.X_add_number;
4915 size = (unsigned long) imm2_expr.X_add_number;
4916 }
4917
4918 if (pos > 63)
4919 {
4920 as_bad (_("Improper position (%lu)"), pos);
4921 pos = 1;
4922 }
4923 if (size == 0 || size > 64
4924 || (pos + size - 1) > 63)
4925 {
4926 as_bad (_("Improper extract size (%lu, position %lu)"),
4927 size, pos);
4928 size = 1;
4929 }
4930
4931 if (size <= 32 && pos < 32)
4932 {
4933 s = "dext";
4934 fmt = "t,r,+A,+C";
4935 }
4936 else if (size <= 32)
4937 {
4938 s = "dextu";
4939 fmt = "t,r,+E,+H";
4940 }
4941 else
4942 {
4943 s = "dextm";
4944 fmt = "t,r,+A,+G";
4945 }
67c0d1eb 4946 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, pos, size - 1);
5f74bc13 4947 }
8fc2e39e 4948 break;
5f74bc13
CD
4949
4950 case M_DINS:
4951 {
4952 unsigned long pos;
4953 unsigned long size;
4954
4955 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
4956 {
4957 as_bad (_("Unsupported large constant"));
4958 pos = size = 1;
4959 }
4960 else
4961 {
4962 pos = (unsigned long) imm_expr.X_add_number;
4963 size = (unsigned long) imm2_expr.X_add_number;
4964 }
4965
4966 if (pos > 63)
4967 {
4968 as_bad (_("Improper position (%lu)"), pos);
4969 pos = 1;
4970 }
4971 if (size == 0 || size > 64
4972 || (pos + size - 1) > 63)
4973 {
4974 as_bad (_("Improper insert size (%lu, position %lu)"),
4975 size, pos);
4976 size = 1;
4977 }
4978
4979 if (pos < 32 && (pos + size - 1) < 32)
4980 {
4981 s = "dins";
4982 fmt = "t,r,+A,+B";
4983 }
4984 else if (pos >= 32)
4985 {
4986 s = "dinsu";
4987 fmt = "t,r,+E,+F";
4988 }
4989 else
4990 {
4991 s = "dinsm";
4992 fmt = "t,r,+A,+F";
4993 }
67c0d1eb
RS
4994 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, pos,
4995 pos + size - 1);
5f74bc13 4996 }
8fc2e39e 4997 break;
5f74bc13 4998
252b5132
RH
4999 case M_DDIV_3:
5000 dbl = 1;
5001 case M_DIV_3:
5002 s = "mflo";
5003 goto do_div3;
5004 case M_DREM_3:
5005 dbl = 1;
5006 case M_REM_3:
5007 s = "mfhi";
5008 do_div3:
5009 if (treg == 0)
5010 {
5011 as_warn (_("Divide by zero."));
5012 if (mips_trap)
67c0d1eb 5013 macro_build (NULL, "teq", "s,t,q", 0, 0, 7);
252b5132 5014 else
67c0d1eb 5015 macro_build (NULL, "break", "c", 7);
8fc2e39e 5016 break;
252b5132
RH
5017 }
5018
7d10b47d 5019 start_noreorder ();
252b5132
RH
5020 if (mips_trap)
5021 {
67c0d1eb
RS
5022 macro_build (NULL, "teq", "s,t,q", treg, 0, 7);
5023 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
252b5132
RH
5024 }
5025 else
5026 {
5027 expr1.X_add_number = 8;
67c0d1eb
RS
5028 macro_build (&expr1, "bne", "s,t,p", treg, 0);
5029 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
5030 macro_build (NULL, "break", "c", 7);
252b5132
RH
5031 }
5032 expr1.X_add_number = -1;
8fc2e39e 5033 used_at = 1;
f6a22291 5034 load_register (AT, &expr1, dbl);
252b5132 5035 expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
67c0d1eb 5036 macro_build (&expr1, "bne", "s,t,p", treg, AT);
252b5132
RH
5037 if (dbl)
5038 {
5039 expr1.X_add_number = 1;
f6a22291 5040 load_register (AT, &expr1, dbl);
67c0d1eb 5041 macro_build (NULL, "dsll32", "d,w,<", AT, AT, 31);
252b5132
RH
5042 }
5043 else
5044 {
5045 expr1.X_add_number = 0x80000000;
67c0d1eb 5046 macro_build (&expr1, "lui", "t,u", AT, BFD_RELOC_HI16);
252b5132
RH
5047 }
5048 if (mips_trap)
5049 {
67c0d1eb 5050 macro_build (NULL, "teq", "s,t,q", sreg, AT, 6);
252b5132
RH
5051 /* We want to close the noreorder block as soon as possible, so
5052 that later insns are available for delay slot filling. */
7d10b47d 5053 end_noreorder ();
252b5132
RH
5054 }
5055 else
5056 {
5057 expr1.X_add_number = 8;
67c0d1eb
RS
5058 macro_build (&expr1, "bne", "s,t,p", sreg, AT);
5059 macro_build (NULL, "nop", "", 0);
252b5132
RH
5060
5061 /* We want to close the noreorder block as soon as possible, so
5062 that later insns are available for delay slot filling. */
7d10b47d 5063 end_noreorder ();
252b5132 5064
67c0d1eb 5065 macro_build (NULL, "break", "c", 6);
252b5132 5066 }
67c0d1eb 5067 macro_build (NULL, s, "d", dreg);
252b5132
RH
5068 break;
5069
5070 case M_DIV_3I:
5071 s = "div";
5072 s2 = "mflo";
5073 goto do_divi;
5074 case M_DIVU_3I:
5075 s = "divu";
5076 s2 = "mflo";
5077 goto do_divi;
5078 case M_REM_3I:
5079 s = "div";
5080 s2 = "mfhi";
5081 goto do_divi;
5082 case M_REMU_3I:
5083 s = "divu";
5084 s2 = "mfhi";
5085 goto do_divi;
5086 case M_DDIV_3I:
5087 dbl = 1;
5088 s = "ddiv";
5089 s2 = "mflo";
5090 goto do_divi;
5091 case M_DDIVU_3I:
5092 dbl = 1;
5093 s = "ddivu";
5094 s2 = "mflo";
5095 goto do_divi;
5096 case M_DREM_3I:
5097 dbl = 1;
5098 s = "ddiv";
5099 s2 = "mfhi";
5100 goto do_divi;
5101 case M_DREMU_3I:
5102 dbl = 1;
5103 s = "ddivu";
5104 s2 = "mfhi";
5105 do_divi:
5106 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
5107 {
5108 as_warn (_("Divide by zero."));
5109 if (mips_trap)
67c0d1eb 5110 macro_build (NULL, "teq", "s,t,q", 0, 0, 7);
252b5132 5111 else
67c0d1eb 5112 macro_build (NULL, "break", "c", 7);
8fc2e39e 5113 break;
252b5132
RH
5114 }
5115 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
5116 {
5117 if (strcmp (s2, "mflo") == 0)
67c0d1eb 5118 move_register (dreg, sreg);
252b5132 5119 else
67c0d1eb 5120 move_register (dreg, 0);
8fc2e39e 5121 break;
252b5132
RH
5122 }
5123 if (imm_expr.X_op == O_constant
5124 && imm_expr.X_add_number == -1
5125 && s[strlen (s) - 1] != 'u')
5126 {
5127 if (strcmp (s2, "mflo") == 0)
5128 {
67c0d1eb 5129 macro_build (NULL, dbl ? "dneg" : "neg", "d,w", dreg, sreg);
252b5132
RH
5130 }
5131 else
67c0d1eb 5132 move_register (dreg, 0);
8fc2e39e 5133 break;
252b5132
RH
5134 }
5135
8fc2e39e 5136 used_at = 1;
67c0d1eb
RS
5137 load_register (AT, &imm_expr, dbl);
5138 macro_build (NULL, s, "z,s,t", sreg, AT);
5139 macro_build (NULL, s2, "d", dreg);
252b5132
RH
5140 break;
5141
5142 case M_DIVU_3:
5143 s = "divu";
5144 s2 = "mflo";
5145 goto do_divu3;
5146 case M_REMU_3:
5147 s = "divu";
5148 s2 = "mfhi";
5149 goto do_divu3;
5150 case M_DDIVU_3:
5151 s = "ddivu";
5152 s2 = "mflo";
5153 goto do_divu3;
5154 case M_DREMU_3:
5155 s = "ddivu";
5156 s2 = "mfhi";
5157 do_divu3:
7d10b47d 5158 start_noreorder ();
252b5132
RH
5159 if (mips_trap)
5160 {
67c0d1eb
RS
5161 macro_build (NULL, "teq", "s,t,q", treg, 0, 7);
5162 macro_build (NULL, s, "z,s,t", sreg, treg);
252b5132
RH
5163 /* We want to close the noreorder block as soon as possible, so
5164 that later insns are available for delay slot filling. */
7d10b47d 5165 end_noreorder ();
252b5132
RH
5166 }
5167 else
5168 {
5169 expr1.X_add_number = 8;
67c0d1eb
RS
5170 macro_build (&expr1, "bne", "s,t,p", treg, 0);
5171 macro_build (NULL, s, "z,s,t", sreg, treg);
252b5132
RH
5172
5173 /* We want to close the noreorder block as soon as possible, so
5174 that later insns are available for delay slot filling. */
7d10b47d 5175 end_noreorder ();
67c0d1eb 5176 macro_build (NULL, "break", "c", 7);
252b5132 5177 }
67c0d1eb 5178 macro_build (NULL, s2, "d", dreg);
8fc2e39e 5179 break;
252b5132 5180
1abe91b1
MR
5181 case M_DLCA_AB:
5182 dbl = 1;
5183 case M_LCA_AB:
5184 call = 1;
5185 goto do_la;
252b5132
RH
5186 case M_DLA_AB:
5187 dbl = 1;
5188 case M_LA_AB:
1abe91b1 5189 do_la:
252b5132
RH
5190 /* Load the address of a symbol into a register. If breg is not
5191 zero, we then add a base register to it. */
5192
3bec30a8
TS
5193 if (dbl && HAVE_32BIT_GPRS)
5194 as_warn (_("dla used to load 32-bit register"));
5195
c90bbe5b 5196 if (! dbl && HAVE_64BIT_OBJECTS)
3bec30a8
TS
5197 as_warn (_("la used to load 64-bit address"));
5198
0c11417f
MR
5199 if (offset_expr.X_op == O_constant
5200 && offset_expr.X_add_number >= -0x8000
5201 && offset_expr.X_add_number < 0x8000)
5202 {
aed1a261 5203 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
17a2f251 5204 "t,r,j", treg, sreg, BFD_RELOC_LO16);
8fc2e39e 5205 break;
0c11417f
MR
5206 }
5207
b8285c27 5208 if (!mips_opts.noat && (treg == breg))
afdbd6d0
CD
5209 {
5210 tempreg = AT;
5211 used_at = 1;
5212 }
5213 else
5214 {
5215 tempreg = treg;
afdbd6d0
CD
5216 }
5217
252b5132
RH
5218 if (offset_expr.X_op != O_symbol
5219 && offset_expr.X_op != O_constant)
5220 {
5221 as_bad (_("expression too complex"));
5222 offset_expr.X_op = O_constant;
5223 }
5224
252b5132 5225 if (offset_expr.X_op == O_constant)
aed1a261 5226 load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
252b5132
RH
5227 else if (mips_pic == NO_PIC)
5228 {
d6bc6245 5229 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 5230 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
5231 Otherwise we want
5232 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5233 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5234 If we have a constant, we need two instructions anyhow,
d6bc6245 5235 so we may as well always use the latter form.
76b3015f 5236
6caf9ef4
TS
5237 With 64bit address space and a usable $at we want
5238 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5239 lui $at,<sym> (BFD_RELOC_HI16_S)
5240 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5241 daddiu $at,<sym> (BFD_RELOC_LO16)
5242 dsll32 $tempreg,0
5243 daddu $tempreg,$tempreg,$at
5244
5245 If $at is already in use, we use a path which is suboptimal
5246 on superscalar processors.
5247 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5248 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5249 dsll $tempreg,16
5250 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5251 dsll $tempreg,16
5252 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
5253
5254 For GP relative symbols in 64bit address space we can use
5255 the same sequence as in 32bit address space. */
aed1a261 5256 if (HAVE_64BIT_SYMBOLS)
252b5132 5257 {
6caf9ef4
TS
5258 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
5259 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
5260 {
5261 relax_start (offset_expr.X_add_symbol);
5262 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5263 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
5264 relax_switch ();
5265 }
d6bc6245 5266
b8285c27 5267 if (used_at == 0 && !mips_opts.noat)
98d3f06f 5268 {
67c0d1eb 5269 macro_build (&offset_expr, "lui", "t,u",
17a2f251 5270 tempreg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb 5271 macro_build (&offset_expr, "lui", "t,u",
17a2f251 5272 AT, BFD_RELOC_HI16_S);
67c0d1eb 5273 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 5274 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
67c0d1eb 5275 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 5276 AT, AT, BFD_RELOC_LO16);
67c0d1eb
RS
5277 macro_build (NULL, "dsll32", "d,w,<", tempreg, tempreg, 0);
5278 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
98d3f06f
KH
5279 used_at = 1;
5280 }
5281 else
5282 {
67c0d1eb 5283 macro_build (&offset_expr, "lui", "t,u",
17a2f251 5284 tempreg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb 5285 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 5286 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
67c0d1eb
RS
5287 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
5288 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 5289 tempreg, tempreg, BFD_RELOC_HI16_S);
67c0d1eb
RS
5290 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
5291 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 5292 tempreg, tempreg, BFD_RELOC_LO16);
98d3f06f 5293 }
6caf9ef4
TS
5294
5295 if (mips_relax.sequence)
5296 relax_end ();
98d3f06f
KH
5297 }
5298 else
5299 {
5300 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 5301 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
98d3f06f 5302 {
4d7206a2 5303 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
5304 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5305 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 5306 relax_switch ();
98d3f06f 5307 }
6943caf0
ILT
5308 if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
5309 as_bad (_("offset too large"));
67c0d1eb
RS
5310 macro_build_lui (&offset_expr, tempreg);
5311 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5312 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2
RS
5313 if (mips_relax.sequence)
5314 relax_end ();
98d3f06f 5315 }
252b5132 5316 }
0a44bf69 5317 else if (!mips_big_got && !HAVE_NEWABI)
252b5132 5318 {
9117d219
NC
5319 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5320
252b5132
RH
5321 /* If this is a reference to an external symbol, and there
5322 is no constant, we want
5323 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
1abe91b1 5324 or for lca or if tempreg is PIC_CALL_REG
9117d219 5325 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
252b5132
RH
5326 For a local symbol, we want
5327 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5328 nop
5329 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5330
5331 If we have a small constant, and this is a reference to
5332 an external symbol, we want
5333 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5334 nop
5335 addiu $tempreg,$tempreg,<constant>
5336 For a local symbol, we want the same instruction
5337 sequence, but we output a BFD_RELOC_LO16 reloc on the
5338 addiu instruction.
5339
5340 If we have a large constant, and this is a reference to
5341 an external symbol, we want
5342 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5343 lui $at,<hiconstant>
5344 addiu $at,$at,<loconstant>
5345 addu $tempreg,$tempreg,$at
5346 For a local symbol, we want the same instruction
5347 sequence, but we output a BFD_RELOC_LO16 reloc on the
ed6fb7bd 5348 addiu instruction.
ed6fb7bd
SC
5349 */
5350
4d7206a2 5351 if (offset_expr.X_add_number == 0)
252b5132 5352 {
0a44bf69
RS
5353 if (mips_pic == SVR4_PIC
5354 && breg == 0
5355 && (call || tempreg == PIC_CALL_REG))
4d7206a2
RS
5356 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
5357
5358 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
5359 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5360 lw_reloc_type, mips_gp_register);
4d7206a2 5361 if (breg != 0)
252b5132
RH
5362 {
5363 /* We're going to put in an addu instruction using
5364 tempreg, so we may as well insert the nop right
5365 now. */
269137b2 5366 load_delay_nop ();
252b5132 5367 }
4d7206a2 5368 relax_switch ();
67c0d1eb
RS
5369 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5370 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 5371 load_delay_nop ();
67c0d1eb
RS
5372 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5373 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2 5374 relax_end ();
252b5132
RH
5375 /* FIXME: If breg == 0, and the next instruction uses
5376 $tempreg, then if this variant case is used an extra
5377 nop will be generated. */
5378 }
4d7206a2
RS
5379 else if (offset_expr.X_add_number >= -0x8000
5380 && offset_expr.X_add_number < 0x8000)
252b5132 5381 {
67c0d1eb 5382 load_got_offset (tempreg, &offset_expr);
269137b2 5383 load_delay_nop ();
67c0d1eb 5384 add_got_offset (tempreg, &offset_expr);
252b5132
RH
5385 }
5386 else
5387 {
4d7206a2
RS
5388 expr1.X_add_number = offset_expr.X_add_number;
5389 offset_expr.X_add_number =
5390 ((offset_expr.X_add_number + 0x8000) & 0xffff) - 0x8000;
67c0d1eb 5391 load_got_offset (tempreg, &offset_expr);
f6a22291 5392 offset_expr.X_add_number = expr1.X_add_number;
252b5132
RH
5393 /* If we are going to add in a base register, and the
5394 target register and the base register are the same,
5395 then we are using AT as a temporary register. Since
5396 we want to load the constant into AT, we add our
5397 current AT (from the global offset table) and the
5398 register into the register now, and pretend we were
5399 not using a base register. */
67c0d1eb 5400 if (breg == treg)
252b5132 5401 {
269137b2 5402 load_delay_nop ();
67c0d1eb 5403 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5404 treg, AT, breg);
252b5132
RH
5405 breg = 0;
5406 tempreg = treg;
252b5132 5407 }
f6a22291 5408 add_got_offset_hilo (tempreg, &offset_expr, AT);
252b5132
RH
5409 used_at = 1;
5410 }
5411 }
0a44bf69 5412 else if (!mips_big_got && HAVE_NEWABI)
f5040a92 5413 {
67c0d1eb 5414 int add_breg_early = 0;
f5040a92
AO
5415
5416 /* If this is a reference to an external, and there is no
5417 constant, or local symbol (*), with or without a
5418 constant, we want
5419 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
1abe91b1 5420 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
5421 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5422
5423 If we have a small constant, and this is a reference to
5424 an external symbol, we want
5425 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5426 addiu $tempreg,$tempreg,<constant>
5427
5428 If we have a large constant, and this is a reference to
5429 an external symbol, we want
5430 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5431 lui $at,<hiconstant>
5432 addiu $at,$at,<loconstant>
5433 addu $tempreg,$tempreg,$at
5434
5435 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
5436 local symbols, even though it introduces an additional
5437 instruction. */
5438
f5040a92
AO
5439 if (offset_expr.X_add_number)
5440 {
4d7206a2 5441 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
5442 offset_expr.X_add_number = 0;
5443
4d7206a2 5444 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
5445 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5446 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
5447
5448 if (expr1.X_add_number >= -0x8000
5449 && expr1.X_add_number < 0x8000)
5450 {
67c0d1eb
RS
5451 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
5452 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 5453 }
ecd13cd3 5454 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92
AO
5455 {
5456 int dreg;
5457
5458 /* If we are going to add in a base register, and the
5459 target register and the base register are the same,
5460 then we are using AT as a temporary register. Since
5461 we want to load the constant into AT, we add our
5462 current AT (from the global offset table) and the
5463 register into the register now, and pretend we were
5464 not using a base register. */
5465 if (breg != treg)
5466 dreg = tempreg;
5467 else
5468 {
5469 assert (tempreg == AT);
67c0d1eb
RS
5470 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5471 treg, AT, breg);
f5040a92 5472 dreg = treg;
67c0d1eb 5473 add_breg_early = 1;
f5040a92
AO
5474 }
5475
f6a22291 5476 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 5477 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5478 dreg, dreg, AT);
f5040a92 5479
f5040a92
AO
5480 used_at = 1;
5481 }
5482 else
5483 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5484
4d7206a2 5485 relax_switch ();
f5040a92
AO
5486 offset_expr.X_add_number = expr1.X_add_number;
5487
67c0d1eb
RS
5488 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5489 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5490 if (add_breg_early)
f5040a92 5491 {
67c0d1eb 5492 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
f899b4b8 5493 treg, tempreg, breg);
f5040a92
AO
5494 breg = 0;
5495 tempreg = treg;
5496 }
4d7206a2 5497 relax_end ();
f5040a92 5498 }
4d7206a2 5499 else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
f5040a92 5500 {
4d7206a2 5501 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
5502 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5503 BFD_RELOC_MIPS_CALL16, mips_gp_register);
4d7206a2 5504 relax_switch ();
67c0d1eb
RS
5505 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5506 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2 5507 relax_end ();
f5040a92 5508 }
4d7206a2 5509 else
f5040a92 5510 {
67c0d1eb
RS
5511 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5512 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
5513 }
5514 }
0a44bf69 5515 else if (mips_big_got && !HAVE_NEWABI)
252b5132 5516 {
67c0d1eb 5517 int gpdelay;
9117d219
NC
5518 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5519 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
ed6fb7bd 5520 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
252b5132
RH
5521
5522 /* This is the large GOT case. If this is a reference to an
5523 external symbol, and there is no constant, we want
5524 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5525 addu $tempreg,$tempreg,$gp
5526 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 5527 or for lca or if tempreg is PIC_CALL_REG
9117d219
NC
5528 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5529 addu $tempreg,$tempreg,$gp
5530 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
252b5132
RH
5531 For a local symbol, we want
5532 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5533 nop
5534 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5535
5536 If we have a small constant, and this is a reference to
5537 an external symbol, we want
5538 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5539 addu $tempreg,$tempreg,$gp
5540 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5541 nop
5542 addiu $tempreg,$tempreg,<constant>
5543 For a local symbol, we want
5544 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5545 nop
5546 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
5547
5548 If we have a large constant, and this is a reference to
5549 an external symbol, we want
5550 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5551 addu $tempreg,$tempreg,$gp
5552 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5553 lui $at,<hiconstant>
5554 addiu $at,$at,<loconstant>
5555 addu $tempreg,$tempreg,$at
5556 For a local symbol, we want
5557 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5558 lui $at,<hiconstant>
5559 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
5560 addu $tempreg,$tempreg,$at
f5040a92 5561 */
438c16b8 5562
252b5132
RH
5563 expr1.X_add_number = offset_expr.X_add_number;
5564 offset_expr.X_add_number = 0;
4d7206a2 5565 relax_start (offset_expr.X_add_symbol);
67c0d1eb 5566 gpdelay = reg_needs_delay (mips_gp_register);
1abe91b1
MR
5567 if (expr1.X_add_number == 0 && breg == 0
5568 && (call || tempreg == PIC_CALL_REG))
9117d219
NC
5569 {
5570 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5571 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5572 }
67c0d1eb
RS
5573 macro_build (&offset_expr, "lui", "t,u", tempreg, lui_reloc_type);
5574 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5575 tempreg, tempreg, mips_gp_register);
67c0d1eb 5576 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 5577 tempreg, lw_reloc_type, tempreg);
252b5132
RH
5578 if (expr1.X_add_number == 0)
5579 {
67c0d1eb 5580 if (breg != 0)
252b5132
RH
5581 {
5582 /* We're going to put in an addu instruction using
5583 tempreg, so we may as well insert the nop right
5584 now. */
269137b2 5585 load_delay_nop ();
252b5132 5586 }
252b5132
RH
5587 }
5588 else if (expr1.X_add_number >= -0x8000
5589 && expr1.X_add_number < 0x8000)
5590 {
269137b2 5591 load_delay_nop ();
67c0d1eb 5592 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 5593 tempreg, tempreg, BFD_RELOC_LO16);
252b5132
RH
5594 }
5595 else
5596 {
67c0d1eb 5597 int dreg;
252b5132
RH
5598
5599 /* If we are going to add in a base register, and the
5600 target register and the base register are the same,
5601 then we are using AT as a temporary register. Since
5602 we want to load the constant into AT, we add our
5603 current AT (from the global offset table) and the
5604 register into the register now, and pretend we were
5605 not using a base register. */
5606 if (breg != treg)
67c0d1eb 5607 dreg = tempreg;
252b5132
RH
5608 else
5609 {
5610 assert (tempreg == AT);
269137b2 5611 load_delay_nop ();
67c0d1eb 5612 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5613 treg, AT, breg);
252b5132 5614 dreg = treg;
252b5132
RH
5615 }
5616
f6a22291 5617 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 5618 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
252b5132 5619
252b5132
RH
5620 used_at = 1;
5621 }
4d7206a2
RS
5622 offset_expr.X_add_number =
5623 ((expr1.X_add_number + 0x8000) & 0xffff) - 0x8000;
5624 relax_switch ();
252b5132 5625
67c0d1eb 5626 if (gpdelay)
252b5132
RH
5627 {
5628 /* This is needed because this instruction uses $gp, but
f5040a92 5629 the first instruction on the main stream does not. */
67c0d1eb 5630 macro_build (NULL, "nop", "");
252b5132 5631 }
ed6fb7bd 5632
67c0d1eb
RS
5633 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5634 local_reloc_type, mips_gp_register);
f5040a92 5635 if (expr1.X_add_number >= -0x8000
252b5132
RH
5636 && expr1.X_add_number < 0x8000)
5637 {
269137b2 5638 load_delay_nop ();
67c0d1eb
RS
5639 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5640 tempreg, tempreg, BFD_RELOC_LO16);
252b5132 5641 /* FIXME: If add_number is 0, and there was no base
f5040a92
AO
5642 register, the external symbol case ended with a load,
5643 so if the symbol turns out to not be external, and
5644 the next instruction uses tempreg, an unnecessary nop
5645 will be inserted. */
252b5132
RH
5646 }
5647 else
5648 {
5649 if (breg == treg)
5650 {
5651 /* We must add in the base register now, as in the
f5040a92 5652 external symbol case. */
252b5132 5653 assert (tempreg == AT);
269137b2 5654 load_delay_nop ();
67c0d1eb 5655 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5656 treg, AT, breg);
252b5132
RH
5657 tempreg = treg;
5658 /* We set breg to 0 because we have arranged to add
f5040a92 5659 it in in both cases. */
252b5132
RH
5660 breg = 0;
5661 }
5662
67c0d1eb
RS
5663 macro_build_lui (&expr1, AT);
5664 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 5665 AT, AT, BFD_RELOC_LO16);
67c0d1eb 5666 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5667 tempreg, tempreg, AT);
8fc2e39e 5668 used_at = 1;
252b5132 5669 }
4d7206a2 5670 relax_end ();
252b5132 5671 }
0a44bf69 5672 else if (mips_big_got && HAVE_NEWABI)
f5040a92 5673 {
f5040a92
AO
5674 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5675 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
67c0d1eb 5676 int add_breg_early = 0;
f5040a92
AO
5677
5678 /* This is the large GOT case. If this is a reference to an
5679 external symbol, and there is no constant, we want
5680 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5681 add $tempreg,$tempreg,$gp
5682 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 5683 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
5684 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5685 add $tempreg,$tempreg,$gp
5686 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5687
5688 If we have a small constant, and this is a reference to
5689 an external symbol, we want
5690 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5691 add $tempreg,$tempreg,$gp
5692 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5693 addi $tempreg,$tempreg,<constant>
5694
5695 If we have a large constant, and this is a reference to
5696 an external symbol, we want
5697 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5698 addu $tempreg,$tempreg,$gp
5699 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5700 lui $at,<hiconstant>
5701 addi $at,$at,<loconstant>
5702 add $tempreg,$tempreg,$at
5703
5704 If we have NewABI, and we know it's a local symbol, we want
5705 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
5706 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
5707 otherwise we have to resort to GOT_HI16/GOT_LO16. */
5708
4d7206a2 5709 relax_start (offset_expr.X_add_symbol);
f5040a92 5710
4d7206a2 5711 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
5712 offset_expr.X_add_number = 0;
5713
1abe91b1
MR
5714 if (expr1.X_add_number == 0 && breg == 0
5715 && (call || tempreg == PIC_CALL_REG))
f5040a92
AO
5716 {
5717 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5718 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5719 }
67c0d1eb
RS
5720 macro_build (&offset_expr, "lui", "t,u", tempreg, lui_reloc_type);
5721 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5722 tempreg, tempreg, mips_gp_register);
67c0d1eb
RS
5723 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5724 tempreg, lw_reloc_type, tempreg);
f5040a92
AO
5725
5726 if (expr1.X_add_number == 0)
4d7206a2 5727 ;
f5040a92
AO
5728 else if (expr1.X_add_number >= -0x8000
5729 && expr1.X_add_number < 0x8000)
5730 {
67c0d1eb 5731 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 5732 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 5733 }
ecd13cd3 5734 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92
AO
5735 {
5736 int dreg;
5737
5738 /* If we are going to add in a base register, and the
5739 target register and the base register are the same,
5740 then we are using AT as a temporary register. Since
5741 we want to load the constant into AT, we add our
5742 current AT (from the global offset table) and the
5743 register into the register now, and pretend we were
5744 not using a base register. */
5745 if (breg != treg)
5746 dreg = tempreg;
5747 else
5748 {
5749 assert (tempreg == AT);
67c0d1eb 5750 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5751 treg, AT, breg);
f5040a92 5752 dreg = treg;
67c0d1eb 5753 add_breg_early = 1;
f5040a92
AO
5754 }
5755
f6a22291 5756 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 5757 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
f5040a92 5758
f5040a92
AO
5759 used_at = 1;
5760 }
5761 else
5762 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5763
4d7206a2 5764 relax_switch ();
f5040a92 5765 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
5766 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5767 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
5768 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
5769 tempreg, BFD_RELOC_MIPS_GOT_OFST);
5770 if (add_breg_early)
f5040a92 5771 {
67c0d1eb 5772 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5773 treg, tempreg, breg);
f5040a92
AO
5774 breg = 0;
5775 tempreg = treg;
5776 }
4d7206a2 5777 relax_end ();
f5040a92 5778 }
252b5132
RH
5779 else
5780 abort ();
5781
5782 if (breg != 0)
aed1a261 5783 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", treg, tempreg, breg);
252b5132
RH
5784 break;
5785
5786 case M_J_A:
5787 /* The j instruction may not be used in PIC code, since it
5788 requires an absolute address. We convert it to a b
5789 instruction. */
5790 if (mips_pic == NO_PIC)
67c0d1eb 5791 macro_build (&offset_expr, "j", "a");
252b5132 5792 else
67c0d1eb 5793 macro_build (&offset_expr, "b", "p");
8fc2e39e 5794 break;
252b5132
RH
5795
5796 /* The jal instructions must be handled as macros because when
5797 generating PIC code they expand to multi-instruction
5798 sequences. Normally they are simple instructions. */
5799 case M_JAL_1:
5800 dreg = RA;
5801 /* Fall through. */
5802 case M_JAL_2:
3e722fb5 5803 if (mips_pic == NO_PIC)
67c0d1eb 5804 macro_build (NULL, "jalr", "d,s", dreg, sreg);
0a44bf69 5805 else
252b5132
RH
5806 {
5807 if (sreg != PIC_CALL_REG)
5808 as_warn (_("MIPS PIC call to register other than $25"));
bdaaa2e1 5809
67c0d1eb 5810 macro_build (NULL, "jalr", "d,s", dreg, sreg);
0a44bf69 5811 if (mips_pic == SVR4_PIC && !HAVE_NEWABI)
252b5132 5812 {
6478892d
TS
5813 if (mips_cprestore_offset < 0)
5814 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5815 else
5816 {
7a621144
DJ
5817 if (! mips_frame_reg_valid)
5818 {
5819 as_warn (_("No .frame pseudo-op used in PIC code"));
5820 /* Quiet this warning. */
5821 mips_frame_reg_valid = 1;
5822 }
5823 if (! mips_cprestore_valid)
5824 {
5825 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5826 /* Quiet this warning. */
5827 mips_cprestore_valid = 1;
5828 }
6478892d 5829 expr1.X_add_number = mips_cprestore_offset;
67c0d1eb 5830 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 5831 mips_gp_register,
256ab948
TS
5832 mips_frame_reg,
5833 HAVE_64BIT_ADDRESSES);
6478892d 5834 }
252b5132
RH
5835 }
5836 }
252b5132 5837
8fc2e39e 5838 break;
252b5132
RH
5839
5840 case M_JAL_A:
5841 if (mips_pic == NO_PIC)
67c0d1eb 5842 macro_build (&offset_expr, "jal", "a");
252b5132
RH
5843 else if (mips_pic == SVR4_PIC)
5844 {
5845 /* If this is a reference to an external symbol, and we are
5846 using a small GOT, we want
5847 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5848 nop
f9419b05 5849 jalr $ra,$25
252b5132
RH
5850 nop
5851 lw $gp,cprestore($sp)
5852 The cprestore value is set using the .cprestore
5853 pseudo-op. If we are using a big GOT, we want
5854 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5855 addu $25,$25,$gp
5856 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
5857 nop
f9419b05 5858 jalr $ra,$25
252b5132
RH
5859 nop
5860 lw $gp,cprestore($sp)
5861 If the symbol is not external, we want
5862 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5863 nop
5864 addiu $25,$25,<sym> (BFD_RELOC_LO16)
f9419b05 5865 jalr $ra,$25
252b5132 5866 nop
438c16b8 5867 lw $gp,cprestore($sp)
f5040a92
AO
5868
5869 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
5870 sequences above, minus nops, unless the symbol is local,
5871 which enables us to use GOT_PAGE/GOT_OFST (big got) or
5872 GOT_DISP. */
438c16b8 5873 if (HAVE_NEWABI)
252b5132 5874 {
f5040a92
AO
5875 if (! mips_big_got)
5876 {
4d7206a2 5877 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
5878 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5879 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
f5040a92 5880 mips_gp_register);
4d7206a2 5881 relax_switch ();
67c0d1eb
RS
5882 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5883 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
4d7206a2
RS
5884 mips_gp_register);
5885 relax_end ();
f5040a92
AO
5886 }
5887 else
5888 {
4d7206a2 5889 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
5890 macro_build (&offset_expr, "lui", "t,u", PIC_CALL_REG,
5891 BFD_RELOC_MIPS_CALL_HI16);
5892 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
5893 PIC_CALL_REG, mips_gp_register);
5894 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5895 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
5896 PIC_CALL_REG);
4d7206a2 5897 relax_switch ();
67c0d1eb
RS
5898 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5899 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
5900 mips_gp_register);
5901 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5902 PIC_CALL_REG, PIC_CALL_REG,
17a2f251 5903 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 5904 relax_end ();
f5040a92 5905 }
684022ea 5906
67c0d1eb 5907 macro_build_jalr (&offset_expr);
252b5132
RH
5908 }
5909 else
5910 {
4d7206a2 5911 relax_start (offset_expr.X_add_symbol);
438c16b8
TS
5912 if (! mips_big_got)
5913 {
67c0d1eb
RS
5914 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5915 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
17a2f251 5916 mips_gp_register);
269137b2 5917 load_delay_nop ();
4d7206a2 5918 relax_switch ();
438c16b8 5919 }
252b5132 5920 else
252b5132 5921 {
67c0d1eb
RS
5922 int gpdelay;
5923
5924 gpdelay = reg_needs_delay (mips_gp_register);
5925 macro_build (&offset_expr, "lui", "t,u", PIC_CALL_REG,
5926 BFD_RELOC_MIPS_CALL_HI16);
5927 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
5928 PIC_CALL_REG, mips_gp_register);
5929 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5930 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
5931 PIC_CALL_REG);
269137b2 5932 load_delay_nop ();
4d7206a2 5933 relax_switch ();
67c0d1eb
RS
5934 if (gpdelay)
5935 macro_build (NULL, "nop", "");
252b5132 5936 }
67c0d1eb
RS
5937 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5938 PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
4d7206a2 5939 mips_gp_register);
269137b2 5940 load_delay_nop ();
67c0d1eb
RS
5941 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5942 PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
4d7206a2 5943 relax_end ();
67c0d1eb 5944 macro_build_jalr (&offset_expr);
438c16b8 5945
6478892d
TS
5946 if (mips_cprestore_offset < 0)
5947 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5948 else
5949 {
7a621144
DJ
5950 if (! mips_frame_reg_valid)
5951 {
5952 as_warn (_("No .frame pseudo-op used in PIC code"));
5953 /* Quiet this warning. */
5954 mips_frame_reg_valid = 1;
5955 }
5956 if (! mips_cprestore_valid)
5957 {
5958 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5959 /* Quiet this warning. */
5960 mips_cprestore_valid = 1;
5961 }
6478892d 5962 if (mips_opts.noreorder)
67c0d1eb 5963 macro_build (NULL, "nop", "");
6478892d 5964 expr1.X_add_number = mips_cprestore_offset;
67c0d1eb 5965 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 5966 mips_gp_register,
256ab948
TS
5967 mips_frame_reg,
5968 HAVE_64BIT_ADDRESSES);
6478892d 5969 }
252b5132
RH
5970 }
5971 }
0a44bf69
RS
5972 else if (mips_pic == VXWORKS_PIC)
5973 as_bad (_("Non-PIC jump used in PIC library"));
252b5132
RH
5974 else
5975 abort ();
5976
8fc2e39e 5977 break;
252b5132
RH
5978
5979 case M_LB_AB:
5980 s = "lb";
5981 goto ld;
5982 case M_LBU_AB:
5983 s = "lbu";
5984 goto ld;
5985 case M_LH_AB:
5986 s = "lh";
5987 goto ld;
5988 case M_LHU_AB:
5989 s = "lhu";
5990 goto ld;
5991 case M_LW_AB:
5992 s = "lw";
5993 goto ld;
5994 case M_LWC0_AB:
5995 s = "lwc0";
bdaaa2e1 5996 /* Itbl support may require additional care here. */
252b5132
RH
5997 coproc = 1;
5998 goto ld;
5999 case M_LWC1_AB:
6000 s = "lwc1";
bdaaa2e1 6001 /* Itbl support may require additional care here. */
252b5132
RH
6002 coproc = 1;
6003 goto ld;
6004 case M_LWC2_AB:
6005 s = "lwc2";
bdaaa2e1 6006 /* Itbl support may require additional care here. */
252b5132
RH
6007 coproc = 1;
6008 goto ld;
6009 case M_LWC3_AB:
6010 s = "lwc3";
bdaaa2e1 6011 /* Itbl support may require additional care here. */
252b5132
RH
6012 coproc = 1;
6013 goto ld;
6014 case M_LWL_AB:
6015 s = "lwl";
6016 lr = 1;
6017 goto ld;
6018 case M_LWR_AB:
6019 s = "lwr";
6020 lr = 1;
6021 goto ld;
6022 case M_LDC1_AB:
fef14a42 6023 if (mips_opts.arch == CPU_R4650)
252b5132
RH
6024 {
6025 as_bad (_("opcode not supported on this processor"));
8fc2e39e 6026 break;
252b5132
RH
6027 }
6028 s = "ldc1";
bdaaa2e1 6029 /* Itbl support may require additional care here. */
252b5132
RH
6030 coproc = 1;
6031 goto ld;
6032 case M_LDC2_AB:
6033 s = "ldc2";
bdaaa2e1 6034 /* Itbl support may require additional care here. */
252b5132
RH
6035 coproc = 1;
6036 goto ld;
6037 case M_LDC3_AB:
6038 s = "ldc3";
bdaaa2e1 6039 /* Itbl support may require additional care here. */
252b5132
RH
6040 coproc = 1;
6041 goto ld;
6042 case M_LDL_AB:
6043 s = "ldl";
6044 lr = 1;
6045 goto ld;
6046 case M_LDR_AB:
6047 s = "ldr";
6048 lr = 1;
6049 goto ld;
6050 case M_LL_AB:
6051 s = "ll";
6052 goto ld;
6053 case M_LLD_AB:
6054 s = "lld";
6055 goto ld;
6056 case M_LWU_AB:
6057 s = "lwu";
6058 ld:
8fc2e39e 6059 if (breg == treg || coproc || lr)
252b5132
RH
6060 {
6061 tempreg = AT;
6062 used_at = 1;
6063 }
6064 else
6065 {
6066 tempreg = treg;
252b5132
RH
6067 }
6068 goto ld_st;
6069 case M_SB_AB:
6070 s = "sb";
6071 goto st;
6072 case M_SH_AB:
6073 s = "sh";
6074 goto st;
6075 case M_SW_AB:
6076 s = "sw";
6077 goto st;
6078 case M_SWC0_AB:
6079 s = "swc0";
bdaaa2e1 6080 /* Itbl support may require additional care here. */
252b5132
RH
6081 coproc = 1;
6082 goto st;
6083 case M_SWC1_AB:
6084 s = "swc1";
bdaaa2e1 6085 /* Itbl support may require additional care here. */
252b5132
RH
6086 coproc = 1;
6087 goto st;
6088 case M_SWC2_AB:
6089 s = "swc2";
bdaaa2e1 6090 /* Itbl support may require additional care here. */
252b5132
RH
6091 coproc = 1;
6092 goto st;
6093 case M_SWC3_AB:
6094 s = "swc3";
bdaaa2e1 6095 /* Itbl support may require additional care here. */
252b5132
RH
6096 coproc = 1;
6097 goto st;
6098 case M_SWL_AB:
6099 s = "swl";
6100 goto st;
6101 case M_SWR_AB:
6102 s = "swr";
6103 goto st;
6104 case M_SC_AB:
6105 s = "sc";
6106 goto st;
6107 case M_SCD_AB:
6108 s = "scd";
6109 goto st;
d43b4baf
TS
6110 case M_CACHE_AB:
6111 s = "cache";
6112 goto st;
252b5132 6113 case M_SDC1_AB:
fef14a42 6114 if (mips_opts.arch == CPU_R4650)
252b5132
RH
6115 {
6116 as_bad (_("opcode not supported on this processor"));
8fc2e39e 6117 break;
252b5132
RH
6118 }
6119 s = "sdc1";
6120 coproc = 1;
bdaaa2e1 6121 /* Itbl support may require additional care here. */
252b5132
RH
6122 goto st;
6123 case M_SDC2_AB:
6124 s = "sdc2";
bdaaa2e1 6125 /* Itbl support may require additional care here. */
252b5132
RH
6126 coproc = 1;
6127 goto st;
6128 case M_SDC3_AB:
6129 s = "sdc3";
bdaaa2e1 6130 /* Itbl support may require additional care here. */
252b5132
RH
6131 coproc = 1;
6132 goto st;
6133 case M_SDL_AB:
6134 s = "sdl";
6135 goto st;
6136 case M_SDR_AB:
6137 s = "sdr";
6138 st:
8fc2e39e
TS
6139 tempreg = AT;
6140 used_at = 1;
252b5132 6141 ld_st:
bdaaa2e1 6142 /* Itbl support may require additional care here. */
252b5132
RH
6143 if (mask == M_LWC1_AB
6144 || mask == M_SWC1_AB
6145 || mask == M_LDC1_AB
6146 || mask == M_SDC1_AB
6147 || mask == M_L_DAB
6148 || mask == M_S_DAB)
6149 fmt = "T,o(b)";
d43b4baf
TS
6150 else if (mask == M_CACHE_AB)
6151 fmt = "k,o(b)";
252b5132
RH
6152 else if (coproc)
6153 fmt = "E,o(b)";
6154 else
6155 fmt = "t,o(b)";
6156
6157 if (offset_expr.X_op != O_constant
6158 && offset_expr.X_op != O_symbol)
6159 {
6160 as_bad (_("expression too complex"));
6161 offset_expr.X_op = O_constant;
6162 }
6163
2051e8c4
MR
6164 if (HAVE_32BIT_ADDRESSES
6165 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
6166 {
6167 char value [32];
6168
6169 sprintf_vma (value, offset_expr.X_add_number);
20e1fcfd 6170 as_bad (_("Number (0x%s) larger than 32 bits"), value);
55e08f71 6171 }
2051e8c4 6172
252b5132
RH
6173 /* A constant expression in PIC code can be handled just as it
6174 is in non PIC code. */
aed1a261
RS
6175 if (offset_expr.X_op == O_constant)
6176 {
aed1a261
RS
6177 expr1.X_add_number = ((offset_expr.X_add_number + 0x8000)
6178 & ~(bfd_vma) 0xffff);
2051e8c4 6179 normalize_address_expr (&expr1);
aed1a261
RS
6180 load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
6181 if (breg != 0)
6182 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6183 tempreg, tempreg, breg);
6184 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16, tempreg);
6185 }
6186 else if (mips_pic == NO_PIC)
252b5132
RH
6187 {
6188 /* If this is a reference to a GP relative symbol, and there
6189 is no base register, we want
cdf6fd85 6190 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
6191 Otherwise, if there is no base register, we want
6192 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
6193 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6194 If we have a constant, we need two instructions anyhow,
6195 so we always use the latter form.
6196
6197 If we have a base register, and this is a reference to a
6198 GP relative symbol, we want
6199 addu $tempreg,$breg,$gp
cdf6fd85 6200 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
6201 Otherwise we want
6202 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
6203 addu $tempreg,$tempreg,$breg
6204 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245 6205 With a constant we always use the latter case.
76b3015f 6206
d6bc6245
TS
6207 With 64bit address space and no base register and $at usable,
6208 we want
6209 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
6210 lui $at,<sym> (BFD_RELOC_HI16_S)
6211 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
6212 dsll32 $tempreg,0
6213 daddu $tempreg,$at
6214 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6215 If we have a base register, we want
6216 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
6217 lui $at,<sym> (BFD_RELOC_HI16_S)
6218 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
6219 daddu $at,$breg
6220 dsll32 $tempreg,0
6221 daddu $tempreg,$at
6222 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6223
6224 Without $at we can't generate the optimal path for superscalar
6225 processors here since this would require two temporary registers.
6226 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
6227 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
6228 dsll $tempreg,16
6229 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
6230 dsll $tempreg,16
6231 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6232 If we have a base register, we want
6233 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
6234 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
6235 dsll $tempreg,16
6236 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
6237 dsll $tempreg,16
6238 daddu $tempreg,$tempreg,$breg
6239 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6373ee54 6240
6caf9ef4 6241 For GP relative symbols in 64bit address space we can use
aed1a261
RS
6242 the same sequence as in 32bit address space. */
6243 if (HAVE_64BIT_SYMBOLS)
d6bc6245 6244 {
aed1a261 6245 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4
TS
6246 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
6247 {
6248 relax_start (offset_expr.X_add_symbol);
6249 if (breg == 0)
6250 {
6251 macro_build (&offset_expr, s, fmt, treg,
6252 BFD_RELOC_GPREL16, mips_gp_register);
6253 }
6254 else
6255 {
6256 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6257 tempreg, breg, mips_gp_register);
6258 macro_build (&offset_expr, s, fmt, treg,
6259 BFD_RELOC_GPREL16, tempreg);
6260 }
6261 relax_switch ();
6262 }
d6bc6245 6263
b8285c27 6264 if (used_at == 0 && !mips_opts.noat)
d6bc6245 6265 {
67c0d1eb
RS
6266 macro_build (&offset_expr, "lui", "t,u", tempreg,
6267 BFD_RELOC_MIPS_HIGHEST);
6268 macro_build (&offset_expr, "lui", "t,u", AT,
6269 BFD_RELOC_HI16_S);
6270 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
6271 tempreg, BFD_RELOC_MIPS_HIGHER);
d6bc6245 6272 if (breg != 0)
67c0d1eb
RS
6273 macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
6274 macro_build (NULL, "dsll32", "d,w,<", tempreg, tempreg, 0);
6275 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
6276 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16,
6277 tempreg);
d6bc6245
TS
6278 used_at = 1;
6279 }
6280 else
6281 {
67c0d1eb
RS
6282 macro_build (&offset_expr, "lui", "t,u", tempreg,
6283 BFD_RELOC_MIPS_HIGHEST);
6284 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
6285 tempreg, BFD_RELOC_MIPS_HIGHER);
6286 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
6287 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
6288 tempreg, BFD_RELOC_HI16_S);
6289 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
d6bc6245 6290 if (breg != 0)
67c0d1eb 6291 macro_build (NULL, "daddu", "d,v,t",
17a2f251 6292 tempreg, tempreg, breg);
67c0d1eb 6293 macro_build (&offset_expr, s, fmt, treg,
17a2f251 6294 BFD_RELOC_LO16, tempreg);
d6bc6245 6295 }
6caf9ef4
TS
6296
6297 if (mips_relax.sequence)
6298 relax_end ();
8fc2e39e 6299 break;
d6bc6245 6300 }
256ab948 6301
252b5132
RH
6302 if (breg == 0)
6303 {
67c0d1eb 6304 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 6305 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 6306 {
4d7206a2 6307 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
6308 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_GPREL16,
6309 mips_gp_register);
4d7206a2 6310 relax_switch ();
252b5132 6311 }
67c0d1eb
RS
6312 macro_build_lui (&offset_expr, tempreg);
6313 macro_build (&offset_expr, s, fmt, treg,
17a2f251 6314 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
6315 if (mips_relax.sequence)
6316 relax_end ();
252b5132
RH
6317 }
6318 else
6319 {
67c0d1eb 6320 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 6321 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 6322 {
4d7206a2 6323 relax_start (offset_expr.X_add_symbol);
67c0d1eb 6324 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6325 tempreg, breg, mips_gp_register);
67c0d1eb 6326 macro_build (&offset_expr, s, fmt, treg,
17a2f251 6327 BFD_RELOC_GPREL16, tempreg);
4d7206a2 6328 relax_switch ();
252b5132 6329 }
67c0d1eb
RS
6330 macro_build_lui (&offset_expr, tempreg);
6331 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6332 tempreg, tempreg, breg);
67c0d1eb 6333 macro_build (&offset_expr, s, fmt, treg,
17a2f251 6334 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
6335 if (mips_relax.sequence)
6336 relax_end ();
252b5132
RH
6337 }
6338 }
0a44bf69 6339 else if (!mips_big_got)
252b5132 6340 {
ed6fb7bd 6341 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
f9419b05 6342
252b5132
RH
6343 /* If this is a reference to an external symbol, we want
6344 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6345 nop
6346 <op> $treg,0($tempreg)
6347 Otherwise we want
6348 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6349 nop
6350 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6351 <op> $treg,0($tempreg)
f5040a92
AO
6352
6353 For NewABI, we want
6354 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6355 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
6356
252b5132
RH
6357 If there is a base register, we add it to $tempreg before
6358 the <op>. If there is a constant, we stick it in the
6359 <op> instruction. We don't handle constants larger than
6360 16 bits, because we have no way to load the upper 16 bits
6361 (actually, we could handle them for the subset of cases
6362 in which we are not using $at). */
6363 assert (offset_expr.X_op == O_symbol);
f5040a92
AO
6364 if (HAVE_NEWABI)
6365 {
67c0d1eb
RS
6366 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6367 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 6368 if (breg != 0)
67c0d1eb 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_MIPS_GOT_OFST, tempreg);
f5040a92
AO
6373 break;
6374 }
252b5132
RH
6375 expr1.X_add_number = offset_expr.X_add_number;
6376 offset_expr.X_add_number = 0;
6377 if (expr1.X_add_number < -0x8000
6378 || expr1.X_add_number >= 0x8000)
6379 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb
RS
6380 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6381 lw_reloc_type, mips_gp_register);
269137b2 6382 load_delay_nop ();
4d7206a2
RS
6383 relax_start (offset_expr.X_add_symbol);
6384 relax_switch ();
67c0d1eb
RS
6385 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
6386 tempreg, BFD_RELOC_LO16);
4d7206a2 6387 relax_end ();
252b5132 6388 if (breg != 0)
67c0d1eb 6389 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6390 tempreg, tempreg, breg);
67c0d1eb 6391 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
252b5132 6392 }
0a44bf69 6393 else if (mips_big_got && !HAVE_NEWABI)
252b5132 6394 {
67c0d1eb 6395 int gpdelay;
252b5132
RH
6396
6397 /* If this is a reference to an external symbol, we want
6398 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6399 addu $tempreg,$tempreg,$gp
6400 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6401 <op> $treg,0($tempreg)
6402 Otherwise we want
6403 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6404 nop
6405 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6406 <op> $treg,0($tempreg)
6407 If there is a base register, we add it to $tempreg before
6408 the <op>. If there is a constant, we stick it in the
6409 <op> instruction. We don't handle constants larger than
6410 16 bits, because we have no way to load the upper 16 bits
6411 (actually, we could handle them for the subset of cases
f5040a92 6412 in which we are not using $at). */
252b5132
RH
6413 assert (offset_expr.X_op == O_symbol);
6414 expr1.X_add_number = offset_expr.X_add_number;
6415 offset_expr.X_add_number = 0;
6416 if (expr1.X_add_number < -0x8000
6417 || expr1.X_add_number >= 0x8000)
6418 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 6419 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 6420 relax_start (offset_expr.X_add_symbol);
67c0d1eb 6421 macro_build (&offset_expr, "lui", "t,u", tempreg,
17a2f251 6422 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
6423 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
6424 mips_gp_register);
6425 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6426 BFD_RELOC_MIPS_GOT_LO16, tempreg);
4d7206a2 6427 relax_switch ();
67c0d1eb
RS
6428 if (gpdelay)
6429 macro_build (NULL, "nop", "");
6430 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6431 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 6432 load_delay_nop ();
67c0d1eb
RS
6433 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
6434 tempreg, BFD_RELOC_LO16);
4d7206a2
RS
6435 relax_end ();
6436
252b5132 6437 if (breg != 0)
67c0d1eb 6438 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6439 tempreg, tempreg, breg);
67c0d1eb 6440 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
252b5132 6441 }
0a44bf69 6442 else if (mips_big_got && HAVE_NEWABI)
f5040a92 6443 {
f5040a92
AO
6444 /* If this is a reference to an external symbol, we want
6445 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6446 add $tempreg,$tempreg,$gp
6447 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6448 <op> $treg,<ofst>($tempreg)
6449 Otherwise, for local symbols, we want:
6450 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6451 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
6452 assert (offset_expr.X_op == O_symbol);
4d7206a2 6453 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
6454 offset_expr.X_add_number = 0;
6455 if (expr1.X_add_number < -0x8000
6456 || expr1.X_add_number >= 0x8000)
6457 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
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);
f5040a92 6465 if (breg != 0)
67c0d1eb 6466 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6467 tempreg, tempreg, breg);
67c0d1eb 6468 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
684022ea 6469
4d7206a2 6470 relax_switch ();
f5040a92 6471 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
6472 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6473 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 6474 if (breg != 0)
67c0d1eb 6475 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6476 tempreg, tempreg, breg);
67c0d1eb 6477 macro_build (&offset_expr, s, fmt, treg,
17a2f251 6478 BFD_RELOC_MIPS_GOT_OFST, tempreg);
4d7206a2 6479 relax_end ();
f5040a92 6480 }
252b5132
RH
6481 else
6482 abort ();
6483
252b5132
RH
6484 break;
6485
6486 case M_LI:
6487 case M_LI_S:
67c0d1eb 6488 load_register (treg, &imm_expr, 0);
8fc2e39e 6489 break;
252b5132
RH
6490
6491 case M_DLI:
67c0d1eb 6492 load_register (treg, &imm_expr, 1);
8fc2e39e 6493 break;
252b5132
RH
6494
6495 case M_LI_SS:
6496 if (imm_expr.X_op == O_constant)
6497 {
8fc2e39e 6498 used_at = 1;
67c0d1eb
RS
6499 load_register (AT, &imm_expr, 0);
6500 macro_build (NULL, "mtc1", "t,G", AT, treg);
252b5132
RH
6501 break;
6502 }
6503 else
6504 {
6505 assert (offset_expr.X_op == O_symbol
6506 && strcmp (segment_name (S_GET_SEGMENT
6507 (offset_expr.X_add_symbol)),
6508 ".lit4") == 0
6509 && offset_expr.X_add_number == 0);
67c0d1eb 6510 macro_build (&offset_expr, "lwc1", "T,o(b)", treg,
17a2f251 6511 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
8fc2e39e 6512 break;
252b5132
RH
6513 }
6514
6515 case M_LI_D:
ca4e0257
RS
6516 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
6517 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
6518 order 32 bits of the value and the low order 32 bits are either
6519 zero or in OFFSET_EXPR. */
252b5132
RH
6520 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6521 {
ca4e0257 6522 if (HAVE_64BIT_GPRS)
67c0d1eb 6523 load_register (treg, &imm_expr, 1);
252b5132
RH
6524 else
6525 {
6526 int hreg, lreg;
6527
6528 if (target_big_endian)
6529 {
6530 hreg = treg;
6531 lreg = treg + 1;
6532 }
6533 else
6534 {
6535 hreg = treg + 1;
6536 lreg = treg;
6537 }
6538
6539 if (hreg <= 31)
67c0d1eb 6540 load_register (hreg, &imm_expr, 0);
252b5132
RH
6541 if (lreg <= 31)
6542 {
6543 if (offset_expr.X_op == O_absent)
67c0d1eb 6544 move_register (lreg, 0);
252b5132
RH
6545 else
6546 {
6547 assert (offset_expr.X_op == O_constant);
67c0d1eb 6548 load_register (lreg, &offset_expr, 0);
252b5132
RH
6549 }
6550 }
6551 }
8fc2e39e 6552 break;
252b5132
RH
6553 }
6554
6555 /* We know that sym is in the .rdata section. First we get the
6556 upper 16 bits of the address. */
6557 if (mips_pic == NO_PIC)
6558 {
67c0d1eb 6559 macro_build_lui (&offset_expr, AT);
8fc2e39e 6560 used_at = 1;
252b5132 6561 }
0a44bf69 6562 else
252b5132 6563 {
67c0d1eb
RS
6564 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6565 BFD_RELOC_MIPS_GOT16, mips_gp_register);
8fc2e39e 6566 used_at = 1;
252b5132 6567 }
bdaaa2e1 6568
252b5132 6569 /* Now we load the register(s). */
ca4e0257 6570 if (HAVE_64BIT_GPRS)
8fc2e39e
TS
6571 {
6572 used_at = 1;
6573 macro_build (&offset_expr, "ld", "t,o(b)", treg, BFD_RELOC_LO16, AT);
6574 }
252b5132
RH
6575 else
6576 {
8fc2e39e 6577 used_at = 1;
67c0d1eb 6578 macro_build (&offset_expr, "lw", "t,o(b)", treg, BFD_RELOC_LO16, AT);
f9419b05 6579 if (treg != RA)
252b5132
RH
6580 {
6581 /* FIXME: How in the world do we deal with the possible
6582 overflow here? */
6583 offset_expr.X_add_number += 4;
67c0d1eb 6584 macro_build (&offset_expr, "lw", "t,o(b)",
17a2f251 6585 treg + 1, BFD_RELOC_LO16, AT);
252b5132
RH
6586 }
6587 }
252b5132
RH
6588 break;
6589
6590 case M_LI_DD:
ca4e0257
RS
6591 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
6592 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
6593 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
6594 the value and the low order 32 bits are either zero or in
6595 OFFSET_EXPR. */
252b5132
RH
6596 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6597 {
8fc2e39e 6598 used_at = 1;
67c0d1eb 6599 load_register (AT, &imm_expr, HAVE_64BIT_FPRS);
ca4e0257
RS
6600 if (HAVE_64BIT_FPRS)
6601 {
6602 assert (HAVE_64BIT_GPRS);
67c0d1eb 6603 macro_build (NULL, "dmtc1", "t,S", AT, treg);
ca4e0257 6604 }
252b5132
RH
6605 else
6606 {
67c0d1eb 6607 macro_build (NULL, "mtc1", "t,G", AT, treg + 1);
252b5132 6608 if (offset_expr.X_op == O_absent)
67c0d1eb 6609 macro_build (NULL, "mtc1", "t,G", 0, treg);
252b5132
RH
6610 else
6611 {
6612 assert (offset_expr.X_op == O_constant);
67c0d1eb
RS
6613 load_register (AT, &offset_expr, 0);
6614 macro_build (NULL, "mtc1", "t,G", AT, treg);
252b5132
RH
6615 }
6616 }
6617 break;
6618 }
6619
6620 assert (offset_expr.X_op == O_symbol
6621 && offset_expr.X_add_number == 0);
6622 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
6623 if (strcmp (s, ".lit8") == 0)
6624 {
e7af610e 6625 if (mips_opts.isa != ISA_MIPS1)
252b5132 6626 {
67c0d1eb 6627 macro_build (&offset_expr, "ldc1", "T,o(b)", treg,
17a2f251 6628 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
8fc2e39e 6629 break;
252b5132 6630 }
c9914766 6631 breg = mips_gp_register;
252b5132
RH
6632 r = BFD_RELOC_MIPS_LITERAL;
6633 goto dob;
6634 }
6635 else
6636 {
6637 assert (strcmp (s, RDATA_SECTION_NAME) == 0);
8fc2e39e 6638 used_at = 1;
0a44bf69 6639 if (mips_pic != NO_PIC)
67c0d1eb
RS
6640 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6641 BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132
RH
6642 else
6643 {
6644 /* FIXME: This won't work for a 64 bit address. */
67c0d1eb 6645 macro_build_lui (&offset_expr, AT);
252b5132 6646 }
bdaaa2e1 6647
e7af610e 6648 if (mips_opts.isa != ISA_MIPS1)
252b5132 6649 {
67c0d1eb
RS
6650 macro_build (&offset_expr, "ldc1", "T,o(b)",
6651 treg, BFD_RELOC_LO16, AT);
252b5132
RH
6652 break;
6653 }
6654 breg = AT;
6655 r = BFD_RELOC_LO16;
6656 goto dob;
6657 }
6658
6659 case M_L_DOB:
fef14a42 6660 if (mips_opts.arch == CPU_R4650)
252b5132
RH
6661 {
6662 as_bad (_("opcode not supported on this processor"));
8fc2e39e 6663 break;
252b5132
RH
6664 }
6665 /* Even on a big endian machine $fn comes before $fn+1. We have
6666 to adjust when loading from memory. */
6667 r = BFD_RELOC_LO16;
6668 dob:
e7af610e 6669 assert (mips_opts.isa == ISA_MIPS1);
67c0d1eb 6670 macro_build (&offset_expr, "lwc1", "T,o(b)",
17a2f251 6671 target_big_endian ? treg + 1 : treg, r, breg);
252b5132
RH
6672 /* FIXME: A possible overflow which I don't know how to deal
6673 with. */
6674 offset_expr.X_add_number += 4;
67c0d1eb 6675 macro_build (&offset_expr, "lwc1", "T,o(b)",
17a2f251 6676 target_big_endian ? treg : treg + 1, r, breg);
252b5132
RH
6677 break;
6678
6679 case M_L_DAB:
6680 /*
6681 * The MIPS assembler seems to check for X_add_number not
6682 * being double aligned and generating:
6683 * lui at,%hi(foo+1)
6684 * addu at,at,v1
6685 * addiu at,at,%lo(foo+1)
6686 * lwc1 f2,0(at)
6687 * lwc1 f3,4(at)
6688 * But, the resulting address is the same after relocation so why
6689 * generate the extra instruction?
6690 */
fef14a42 6691 if (mips_opts.arch == CPU_R4650)
252b5132
RH
6692 {
6693 as_bad (_("opcode not supported on this processor"));
8fc2e39e 6694 break;
252b5132 6695 }
bdaaa2e1 6696 /* Itbl support may require additional care here. */
252b5132 6697 coproc = 1;
e7af610e 6698 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
6699 {
6700 s = "ldc1";
6701 goto ld;
6702 }
6703
6704 s = "lwc1";
6705 fmt = "T,o(b)";
6706 goto ldd_std;
6707
6708 case M_S_DAB:
fef14a42 6709 if (mips_opts.arch == CPU_R4650)
252b5132
RH
6710 {
6711 as_bad (_("opcode not supported on this processor"));
8fc2e39e 6712 break;
252b5132
RH
6713 }
6714
e7af610e 6715 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
6716 {
6717 s = "sdc1";
6718 goto st;
6719 }
6720
6721 s = "swc1";
6722 fmt = "T,o(b)";
bdaaa2e1 6723 /* Itbl support may require additional care here. */
252b5132
RH
6724 coproc = 1;
6725 goto ldd_std;
6726
6727 case M_LD_AB:
ca4e0257 6728 if (HAVE_64BIT_GPRS)
252b5132
RH
6729 {
6730 s = "ld";
6731 goto ld;
6732 }
6733
6734 s = "lw";
6735 fmt = "t,o(b)";
6736 goto ldd_std;
6737
6738 case M_SD_AB:
ca4e0257 6739 if (HAVE_64BIT_GPRS)
252b5132
RH
6740 {
6741 s = "sd";
6742 goto st;
6743 }
6744
6745 s = "sw";
6746 fmt = "t,o(b)";
6747
6748 ldd_std:
6749 if (offset_expr.X_op != O_symbol
6750 && offset_expr.X_op != O_constant)
6751 {
6752 as_bad (_("expression too complex"));
6753 offset_expr.X_op = O_constant;
6754 }
6755
2051e8c4
MR
6756 if (HAVE_32BIT_ADDRESSES
6757 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
6758 {
6759 char value [32];
6760
6761 sprintf_vma (value, offset_expr.X_add_number);
20e1fcfd 6762 as_bad (_("Number (0x%s) larger than 32 bits"), value);
55e08f71 6763 }
2051e8c4 6764
252b5132
RH
6765 /* Even on a big endian machine $fn comes before $fn+1. We have
6766 to adjust when loading from memory. We set coproc if we must
6767 load $fn+1 first. */
bdaaa2e1 6768 /* Itbl support may require additional care here. */
252b5132
RH
6769 if (! target_big_endian)
6770 coproc = 0;
6771
6772 if (mips_pic == NO_PIC
6773 || offset_expr.X_op == O_constant)
6774 {
6775 /* If this is a reference to a GP relative symbol, we want
cdf6fd85
TS
6776 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6777 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
6778 If we have a base register, we use this
6779 addu $at,$breg,$gp
cdf6fd85
TS
6780 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6781 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
6782 If this is not a GP relative symbol, we want
6783 lui $at,<sym> (BFD_RELOC_HI16_S)
6784 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6785 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6786 If there is a base register, we add it to $at after the
6787 lui instruction. If there is a constant, we always use
6788 the last case. */
39a59cf8
MR
6789 if (offset_expr.X_op == O_symbol
6790 && (valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 6791 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 6792 {
4d7206a2 6793 relax_start (offset_expr.X_add_symbol);
252b5132
RH
6794 if (breg == 0)
6795 {
c9914766 6796 tempreg = mips_gp_register;
252b5132
RH
6797 }
6798 else
6799 {
67c0d1eb 6800 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6801 AT, breg, mips_gp_register);
252b5132 6802 tempreg = AT;
252b5132
RH
6803 used_at = 1;
6804 }
6805
beae10d5 6806 /* Itbl support may require additional care here. */
67c0d1eb 6807 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
17a2f251 6808 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6809 offset_expr.X_add_number += 4;
6810
6811 /* Set mips_optimize to 2 to avoid inserting an
6812 undesired nop. */
6813 hold_mips_optimize = mips_optimize;
6814 mips_optimize = 2;
beae10d5 6815 /* Itbl support may require additional care here. */
67c0d1eb 6816 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
17a2f251 6817 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6818 mips_optimize = hold_mips_optimize;
6819
4d7206a2 6820 relax_switch ();
252b5132
RH
6821
6822 /* We just generated two relocs. When tc_gen_reloc
6823 handles this case, it will skip the first reloc and
6824 handle the second. The second reloc already has an
6825 extra addend of 4, which we added above. We must
6826 subtract it out, and then subtract another 4 to make
6827 the first reloc come out right. The second reloc
6828 will come out right because we are going to add 4 to
6829 offset_expr when we build its instruction below.
6830
6831 If we have a symbol, then we don't want to include
6832 the offset, because it will wind up being included
6833 when we generate the reloc. */
6834
6835 if (offset_expr.X_op == O_constant)
6836 offset_expr.X_add_number -= 8;
6837 else
6838 {
6839 offset_expr.X_add_number = -4;
6840 offset_expr.X_op = O_constant;
6841 }
6842 }
8fc2e39e 6843 used_at = 1;
67c0d1eb 6844 macro_build_lui (&offset_expr, AT);
252b5132 6845 if (breg != 0)
67c0d1eb 6846 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 6847 /* Itbl support may require additional care here. */
67c0d1eb 6848 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
17a2f251 6849 BFD_RELOC_LO16, AT);
252b5132
RH
6850 /* FIXME: How do we handle overflow here? */
6851 offset_expr.X_add_number += 4;
beae10d5 6852 /* Itbl support may require additional care here. */
67c0d1eb 6853 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
17a2f251 6854 BFD_RELOC_LO16, AT);
4d7206a2
RS
6855 if (mips_relax.sequence)
6856 relax_end ();
bdaaa2e1 6857 }
0a44bf69 6858 else if (!mips_big_got)
252b5132 6859 {
252b5132
RH
6860 /* If this is a reference to an external symbol, we want
6861 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6862 nop
6863 <op> $treg,0($at)
6864 <op> $treg+1,4($at)
6865 Otherwise we want
6866 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6867 nop
6868 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6869 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6870 If there is a base register we add it to $at before the
6871 lwc1 instructions. If there is a constant we include it
6872 in the lwc1 instructions. */
6873 used_at = 1;
6874 expr1.X_add_number = offset_expr.X_add_number;
252b5132
RH
6875 if (expr1.X_add_number < -0x8000
6876 || expr1.X_add_number >= 0x8000 - 4)
6877 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 6878 load_got_offset (AT, &offset_expr);
269137b2 6879 load_delay_nop ();
252b5132 6880 if (breg != 0)
67c0d1eb 6881 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
252b5132
RH
6882
6883 /* Set mips_optimize to 2 to avoid inserting an undesired
6884 nop. */
6885 hold_mips_optimize = mips_optimize;
6886 mips_optimize = 2;
4d7206a2 6887
beae10d5 6888 /* Itbl support may require additional care here. */
4d7206a2 6889 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
6890 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
6891 BFD_RELOC_LO16, AT);
4d7206a2 6892 expr1.X_add_number += 4;
67c0d1eb
RS
6893 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
6894 BFD_RELOC_LO16, AT);
4d7206a2 6895 relax_switch ();
67c0d1eb
RS
6896 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6897 BFD_RELOC_LO16, AT);
4d7206a2 6898 offset_expr.X_add_number += 4;
67c0d1eb
RS
6899 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6900 BFD_RELOC_LO16, AT);
4d7206a2 6901 relax_end ();
252b5132 6902
4d7206a2 6903 mips_optimize = hold_mips_optimize;
252b5132 6904 }
0a44bf69 6905 else if (mips_big_got)
252b5132 6906 {
67c0d1eb 6907 int gpdelay;
252b5132
RH
6908
6909 /* If this is a reference to an external symbol, we want
6910 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6911 addu $at,$at,$gp
6912 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
6913 nop
6914 <op> $treg,0($at)
6915 <op> $treg+1,4($at)
6916 Otherwise we want
6917 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6918 nop
6919 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6920 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6921 If there is a base register we add it to $at before the
6922 lwc1 instructions. If there is a constant we include it
6923 in the lwc1 instructions. */
6924 used_at = 1;
6925 expr1.X_add_number = offset_expr.X_add_number;
6926 offset_expr.X_add_number = 0;
6927 if (expr1.X_add_number < -0x8000
6928 || expr1.X_add_number >= 0x8000 - 4)
6929 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 6930 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 6931 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
6932 macro_build (&offset_expr, "lui", "t,u",
6933 AT, BFD_RELOC_MIPS_GOT_HI16);
6934 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6935 AT, AT, mips_gp_register);
67c0d1eb 6936 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 6937 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
269137b2 6938 load_delay_nop ();
252b5132 6939 if (breg != 0)
67c0d1eb 6940 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 6941 /* Itbl support may require additional care here. */
67c0d1eb 6942 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
17a2f251 6943 BFD_RELOC_LO16, AT);
252b5132
RH
6944 expr1.X_add_number += 4;
6945
6946 /* Set mips_optimize to 2 to avoid inserting an undesired
6947 nop. */
6948 hold_mips_optimize = mips_optimize;
6949 mips_optimize = 2;
beae10d5 6950 /* Itbl support may require additional care here. */
67c0d1eb 6951 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
17a2f251 6952 BFD_RELOC_LO16, AT);
252b5132
RH
6953 mips_optimize = hold_mips_optimize;
6954 expr1.X_add_number -= 4;
6955
4d7206a2
RS
6956 relax_switch ();
6957 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
6958 if (gpdelay)
6959 macro_build (NULL, "nop", "");
6960 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6961 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 6962 load_delay_nop ();
252b5132 6963 if (breg != 0)
67c0d1eb 6964 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 6965 /* Itbl support may require additional care here. */
67c0d1eb
RS
6966 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6967 BFD_RELOC_LO16, AT);
4d7206a2 6968 offset_expr.X_add_number += 4;
252b5132
RH
6969
6970 /* Set mips_optimize to 2 to avoid inserting an undesired
6971 nop. */
6972 hold_mips_optimize = mips_optimize;
6973 mips_optimize = 2;
beae10d5 6974 /* Itbl support may require additional care here. */
67c0d1eb
RS
6975 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6976 BFD_RELOC_LO16, AT);
252b5132 6977 mips_optimize = hold_mips_optimize;
4d7206a2 6978 relax_end ();
252b5132 6979 }
252b5132
RH
6980 else
6981 abort ();
6982
252b5132
RH
6983 break;
6984
6985 case M_LD_OB:
6986 s = "lw";
6987 goto sd_ob;
6988 case M_SD_OB:
6989 s = "sw";
6990 sd_ob:
ca4e0257 6991 assert (HAVE_32BIT_ADDRESSES);
67c0d1eb 6992 macro_build (&offset_expr, s, "t,o(b)", treg, BFD_RELOC_LO16, breg);
252b5132 6993 offset_expr.X_add_number += 4;
67c0d1eb 6994 macro_build (&offset_expr, s, "t,o(b)", treg + 1, BFD_RELOC_LO16, breg);
8fc2e39e 6995 break;
252b5132
RH
6996
6997 /* New code added to support COPZ instructions.
6998 This code builds table entries out of the macros in mip_opcodes.
6999 R4000 uses interlocks to handle coproc delays.
7000 Other chips (like the R3000) require nops to be inserted for delays.
7001
f72c8c98 7002 FIXME: Currently, we require that the user handle delays.
252b5132
RH
7003 In order to fill delay slots for non-interlocked chips,
7004 we must have a way to specify delays based on the coprocessor.
7005 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
7006 What are the side-effects of the cop instruction?
7007 What cache support might we have and what are its effects?
7008 Both coprocessor & memory require delays. how long???
bdaaa2e1 7009 What registers are read/set/modified?
252b5132
RH
7010
7011 If an itbl is provided to interpret cop instructions,
bdaaa2e1 7012 this knowledge can be encoded in the itbl spec. */
252b5132
RH
7013
7014 case M_COP0:
7015 s = "c0";
7016 goto copz;
7017 case M_COP1:
7018 s = "c1";
7019 goto copz;
7020 case M_COP2:
7021 s = "c2";
7022 goto copz;
7023 case M_COP3:
7024 s = "c3";
7025 copz:
7026 /* For now we just do C (same as Cz). The parameter will be
7027 stored in insn_opcode by mips_ip. */
67c0d1eb 7028 macro_build (NULL, s, "C", ip->insn_opcode);
8fc2e39e 7029 break;
252b5132 7030
ea1fb5dc 7031 case M_MOVE:
67c0d1eb 7032 move_register (dreg, sreg);
8fc2e39e 7033 break;
ea1fb5dc 7034
252b5132
RH
7035#ifdef LOSING_COMPILER
7036 default:
7037 /* Try and see if this is a new itbl instruction.
7038 This code builds table entries out of the macros in mip_opcodes.
7039 FIXME: For now we just assemble the expression and pass it's
7040 value along as a 32-bit immediate.
bdaaa2e1 7041 We may want to have the assembler assemble this value,
252b5132
RH
7042 so that we gain the assembler's knowledge of delay slots,
7043 symbols, etc.
7044 Would it be more efficient to use mask (id) here? */
bdaaa2e1 7045 if (itbl_have_entries
252b5132 7046 && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
beae10d5 7047 {
252b5132
RH
7048 s = ip->insn_mo->name;
7049 s2 = "cop3";
7050 coproc = ITBL_DECODE_PNUM (immed_expr);;
67c0d1eb 7051 macro_build (&immed_expr, s, "C");
8fc2e39e 7052 break;
beae10d5 7053 }
252b5132 7054 macro2 (ip);
8fc2e39e 7055 break;
252b5132 7056 }
8fc2e39e
TS
7057 if (mips_opts.noat && used_at)
7058 as_bad (_("Macro used $at after \".set noat\""));
252b5132 7059}
bdaaa2e1 7060
252b5132 7061static void
17a2f251 7062macro2 (struct mips_cl_insn *ip)
252b5132 7063{
3994f87e 7064 int treg, sreg, dreg, breg;
252b5132
RH
7065 int tempreg;
7066 int mask;
252b5132
RH
7067 int used_at;
7068 expressionS expr1;
7069 const char *s;
7070 const char *s2;
7071 const char *fmt;
7072 int likely = 0;
7073 int dbl = 0;
7074 int coproc = 0;
7075 int lr = 0;
7076 int imm = 0;
7077 int off;
7078 offsetT maxnum;
7079 bfd_reloc_code_real_type r;
bdaaa2e1 7080
252b5132
RH
7081 treg = (ip->insn_opcode >> 16) & 0x1f;
7082 dreg = (ip->insn_opcode >> 11) & 0x1f;
7083 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
7084 mask = ip->insn_mo->mask;
bdaaa2e1 7085
252b5132
RH
7086 expr1.X_op = O_constant;
7087 expr1.X_op_symbol = NULL;
7088 expr1.X_add_symbol = NULL;
7089 expr1.X_add_number = 1;
bdaaa2e1 7090
252b5132
RH
7091 switch (mask)
7092 {
7093#endif /* LOSING_COMPILER */
7094
7095 case M_DMUL:
7096 dbl = 1;
7097 case M_MUL:
67c0d1eb
RS
7098 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", sreg, treg);
7099 macro_build (NULL, "mflo", "d", dreg);
8fc2e39e 7100 break;
252b5132
RH
7101
7102 case M_DMUL_I:
7103 dbl = 1;
7104 case M_MUL_I:
7105 /* The MIPS assembler some times generates shifts and adds. I'm
7106 not trying to be that fancy. GCC should do this for us
7107 anyway. */
8fc2e39e 7108 used_at = 1;
67c0d1eb
RS
7109 load_register (AT, &imm_expr, dbl);
7110 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, AT);
7111 macro_build (NULL, "mflo", "d", dreg);
252b5132
RH
7112 break;
7113
7114 case M_DMULO_I:
7115 dbl = 1;
7116 case M_MULO_I:
7117 imm = 1;
7118 goto do_mulo;
7119
7120 case M_DMULO:
7121 dbl = 1;
7122 case M_MULO:
7123 do_mulo:
7d10b47d 7124 start_noreorder ();
8fc2e39e 7125 used_at = 1;
252b5132 7126 if (imm)
67c0d1eb
RS
7127 load_register (AT, &imm_expr, dbl);
7128 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
7129 macro_build (NULL, "mflo", "d", dreg);
7130 macro_build (NULL, dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, RA);
7131 macro_build (NULL, "mfhi", "d", AT);
252b5132 7132 if (mips_trap)
67c0d1eb 7133 macro_build (NULL, "tne", "s,t,q", dreg, AT, 6);
252b5132
RH
7134 else
7135 {
7136 expr1.X_add_number = 8;
67c0d1eb
RS
7137 macro_build (&expr1, "beq", "s,t,p", dreg, AT);
7138 macro_build (NULL, "nop", "", 0);
7139 macro_build (NULL, "break", "c", 6);
252b5132 7140 }
7d10b47d 7141 end_noreorder ();
67c0d1eb 7142 macro_build (NULL, "mflo", "d", dreg);
252b5132
RH
7143 break;
7144
7145 case M_DMULOU_I:
7146 dbl = 1;
7147 case M_MULOU_I:
7148 imm = 1;
7149 goto do_mulou;
7150
7151 case M_DMULOU:
7152 dbl = 1;
7153 case M_MULOU:
7154 do_mulou:
7d10b47d 7155 start_noreorder ();
8fc2e39e 7156 used_at = 1;
252b5132 7157 if (imm)
67c0d1eb
RS
7158 load_register (AT, &imm_expr, dbl);
7159 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
17a2f251 7160 sreg, imm ? AT : treg);
67c0d1eb
RS
7161 macro_build (NULL, "mfhi", "d", AT);
7162 macro_build (NULL, "mflo", "d", dreg);
252b5132 7163 if (mips_trap)
67c0d1eb 7164 macro_build (NULL, "tne", "s,t,q", AT, 0, 6);
252b5132
RH
7165 else
7166 {
7167 expr1.X_add_number = 8;
67c0d1eb
RS
7168 macro_build (&expr1, "beq", "s,t,p", AT, 0);
7169 macro_build (NULL, "nop", "", 0);
7170 macro_build (NULL, "break", "c", 6);
252b5132 7171 }
7d10b47d 7172 end_noreorder ();
252b5132
RH
7173 break;
7174
771c7ce4 7175 case M_DROL:
fef14a42 7176 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097
CD
7177 {
7178 if (dreg == sreg)
7179 {
7180 tempreg = AT;
7181 used_at = 1;
7182 }
7183 else
7184 {
7185 tempreg = dreg;
82dd0097 7186 }
67c0d1eb
RS
7187 macro_build (NULL, "dnegu", "d,w", tempreg, treg);
7188 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, tempreg);
8fc2e39e 7189 break;
82dd0097 7190 }
8fc2e39e 7191 used_at = 1;
67c0d1eb
RS
7192 macro_build (NULL, "dsubu", "d,v,t", AT, 0, treg);
7193 macro_build (NULL, "dsrlv", "d,t,s", AT, sreg, AT);
7194 macro_build (NULL, "dsllv", "d,t,s", dreg, sreg, treg);
7195 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
7196 break;
7197
252b5132 7198 case M_ROL:
fef14a42 7199 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097
CD
7200 {
7201 if (dreg == sreg)
7202 {
7203 tempreg = AT;
7204 used_at = 1;
7205 }
7206 else
7207 {
7208 tempreg = dreg;
82dd0097 7209 }
67c0d1eb
RS
7210 macro_build (NULL, "negu", "d,w", tempreg, treg);
7211 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, tempreg);
8fc2e39e 7212 break;
82dd0097 7213 }
8fc2e39e 7214 used_at = 1;
67c0d1eb
RS
7215 macro_build (NULL, "subu", "d,v,t", AT, 0, treg);
7216 macro_build (NULL, "srlv", "d,t,s", AT, sreg, AT);
7217 macro_build (NULL, "sllv", "d,t,s", dreg, sreg, treg);
7218 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
7219 break;
7220
771c7ce4
TS
7221 case M_DROL_I:
7222 {
7223 unsigned int rot;
82dd0097 7224 char *l, *r;
771c7ce4
TS
7225
7226 if (imm_expr.X_op != O_constant)
82dd0097 7227 as_bad (_("Improper rotate count"));
771c7ce4 7228 rot = imm_expr.X_add_number & 0x3f;
fef14a42 7229 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
60b63b72
RS
7230 {
7231 rot = (64 - rot) & 0x3f;
7232 if (rot >= 32)
67c0d1eb 7233 macro_build (NULL, "dror32", "d,w,<", dreg, sreg, rot - 32);
60b63b72 7234 else
67c0d1eb 7235 macro_build (NULL, "dror", "d,w,<", dreg, sreg, rot);
8fc2e39e 7236 break;
60b63b72 7237 }
483fc7cd 7238 if (rot == 0)
483fc7cd 7239 {
67c0d1eb 7240 macro_build (NULL, "dsrl", "d,w,<", dreg, sreg, 0);
8fc2e39e 7241 break;
483fc7cd 7242 }
82dd0097
CD
7243 l = (rot < 0x20) ? "dsll" : "dsll32";
7244 r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
7245 rot &= 0x1f;
8fc2e39e 7246 used_at = 1;
67c0d1eb
RS
7247 macro_build (NULL, l, "d,w,<", AT, sreg, rot);
7248 macro_build (NULL, r, "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7249 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
7250 }
7251 break;
7252
252b5132 7253 case M_ROL_I:
771c7ce4
TS
7254 {
7255 unsigned int rot;
7256
7257 if (imm_expr.X_op != O_constant)
82dd0097 7258 as_bad (_("Improper rotate count"));
771c7ce4 7259 rot = imm_expr.X_add_number & 0x1f;
fef14a42 7260 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
60b63b72 7261 {
67c0d1eb 7262 macro_build (NULL, "ror", "d,w,<", dreg, sreg, (32 - rot) & 0x1f);
8fc2e39e 7263 break;
60b63b72 7264 }
483fc7cd 7265 if (rot == 0)
483fc7cd 7266 {
67c0d1eb 7267 macro_build (NULL, "srl", "d,w,<", dreg, sreg, 0);
8fc2e39e 7268 break;
483fc7cd 7269 }
8fc2e39e 7270 used_at = 1;
67c0d1eb
RS
7271 macro_build (NULL, "sll", "d,w,<", AT, sreg, rot);
7272 macro_build (NULL, "srl", "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7273 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
7274 }
7275 break;
7276
7277 case M_DROR:
fef14a42 7278 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097 7279 {
67c0d1eb 7280 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, treg);
8fc2e39e 7281 break;
82dd0097 7282 }
8fc2e39e 7283 used_at = 1;
67c0d1eb
RS
7284 macro_build (NULL, "dsubu", "d,v,t", AT, 0, treg);
7285 macro_build (NULL, "dsllv", "d,t,s", AT, sreg, AT);
7286 macro_build (NULL, "dsrlv", "d,t,s", dreg, sreg, treg);
7287 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
7288 break;
7289
7290 case M_ROR:
fef14a42 7291 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 7292 {
67c0d1eb 7293 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, treg);
8fc2e39e 7294 break;
82dd0097 7295 }
8fc2e39e 7296 used_at = 1;
67c0d1eb
RS
7297 macro_build (NULL, "subu", "d,v,t", AT, 0, treg);
7298 macro_build (NULL, "sllv", "d,t,s", AT, sreg, AT);
7299 macro_build (NULL, "srlv", "d,t,s", dreg, sreg, treg);
7300 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
7301 break;
7302
771c7ce4
TS
7303 case M_DROR_I:
7304 {
7305 unsigned int rot;
82dd0097 7306 char *l, *r;
771c7ce4
TS
7307
7308 if (imm_expr.X_op != O_constant)
82dd0097 7309 as_bad (_("Improper rotate count"));
771c7ce4 7310 rot = imm_expr.X_add_number & 0x3f;
fef14a42 7311 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097
CD
7312 {
7313 if (rot >= 32)
67c0d1eb 7314 macro_build (NULL, "dror32", "d,w,<", dreg, sreg, rot - 32);
82dd0097 7315 else
67c0d1eb 7316 macro_build (NULL, "dror", "d,w,<", dreg, sreg, rot);
8fc2e39e 7317 break;
82dd0097 7318 }
483fc7cd 7319 if (rot == 0)
483fc7cd 7320 {
67c0d1eb 7321 macro_build (NULL, "dsrl", "d,w,<", dreg, sreg, 0);
8fc2e39e 7322 break;
483fc7cd 7323 }
82dd0097
CD
7324 r = (rot < 0x20) ? "dsrl" : "dsrl32";
7325 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
7326 rot &= 0x1f;
8fc2e39e 7327 used_at = 1;
67c0d1eb
RS
7328 macro_build (NULL, r, "d,w,<", AT, sreg, rot);
7329 macro_build (NULL, l, "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7330 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
7331 }
7332 break;
7333
252b5132 7334 case M_ROR_I:
771c7ce4
TS
7335 {
7336 unsigned int rot;
7337
7338 if (imm_expr.X_op != O_constant)
82dd0097 7339 as_bad (_("Improper rotate count"));
771c7ce4 7340 rot = imm_expr.X_add_number & 0x1f;
fef14a42 7341 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 7342 {
67c0d1eb 7343 macro_build (NULL, "ror", "d,w,<", dreg, sreg, rot);
8fc2e39e 7344 break;
82dd0097 7345 }
483fc7cd 7346 if (rot == 0)
483fc7cd 7347 {
67c0d1eb 7348 macro_build (NULL, "srl", "d,w,<", dreg, sreg, 0);
8fc2e39e 7349 break;
483fc7cd 7350 }
8fc2e39e 7351 used_at = 1;
67c0d1eb
RS
7352 macro_build (NULL, "srl", "d,w,<", AT, sreg, rot);
7353 macro_build (NULL, "sll", "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7354 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4 7355 }
252b5132
RH
7356 break;
7357
7358 case M_S_DOB:
fef14a42 7359 if (mips_opts.arch == CPU_R4650)
252b5132
RH
7360 {
7361 as_bad (_("opcode not supported on this processor"));
8fc2e39e 7362 break;
252b5132 7363 }
e7af610e 7364 assert (mips_opts.isa == ISA_MIPS1);
252b5132
RH
7365 /* Even on a big endian machine $fn comes before $fn+1. We have
7366 to adjust when storing to memory. */
67c0d1eb
RS
7367 macro_build (&offset_expr, "swc1", "T,o(b)",
7368 target_big_endian ? treg + 1 : treg, BFD_RELOC_LO16, breg);
252b5132 7369 offset_expr.X_add_number += 4;
67c0d1eb
RS
7370 macro_build (&offset_expr, "swc1", "T,o(b)",
7371 target_big_endian ? treg : treg + 1, BFD_RELOC_LO16, breg);
8fc2e39e 7372 break;
252b5132
RH
7373
7374 case M_SEQ:
7375 if (sreg == 0)
67c0d1eb 7376 macro_build (&expr1, "sltiu", "t,r,j", dreg, treg, BFD_RELOC_LO16);
252b5132 7377 else if (treg == 0)
67c0d1eb 7378 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7379 else
7380 {
67c0d1eb
RS
7381 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
7382 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
252b5132 7383 }
8fc2e39e 7384 break;
252b5132
RH
7385
7386 case M_SEQ_I:
7387 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7388 {
67c0d1eb 7389 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
8fc2e39e 7390 break;
252b5132
RH
7391 }
7392 if (sreg == 0)
7393 {
7394 as_warn (_("Instruction %s: result is always false"),
7395 ip->insn_mo->name);
67c0d1eb 7396 move_register (dreg, 0);
8fc2e39e 7397 break;
252b5132
RH
7398 }
7399 if (imm_expr.X_op == O_constant
7400 && imm_expr.X_add_number >= 0
7401 && imm_expr.X_add_number < 0x10000)
7402 {
67c0d1eb 7403 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7404 }
7405 else if (imm_expr.X_op == O_constant
7406 && imm_expr.X_add_number > -0x8000
7407 && imm_expr.X_add_number < 0)
7408 {
7409 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 7410 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
17a2f251 7411 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7412 }
7413 else
7414 {
67c0d1eb
RS
7415 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7416 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
252b5132
RH
7417 used_at = 1;
7418 }
67c0d1eb 7419 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
8fc2e39e 7420 break;
252b5132
RH
7421
7422 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
7423 s = "slt";
7424 goto sge;
7425 case M_SGEU:
7426 s = "sltu";
7427 sge:
67c0d1eb
RS
7428 macro_build (NULL, s, "d,v,t", dreg, sreg, treg);
7429 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
8fc2e39e 7430 break;
252b5132
RH
7431
7432 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
7433 case M_SGEU_I:
7434 if (imm_expr.X_op == O_constant
7435 && imm_expr.X_add_number >= -0x8000
7436 && imm_expr.X_add_number < 0x8000)
7437 {
67c0d1eb
RS
7438 macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
7439 dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7440 }
7441 else
7442 {
67c0d1eb
RS
7443 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7444 macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
7445 dreg, sreg, AT);
252b5132
RH
7446 used_at = 1;
7447 }
67c0d1eb 7448 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
8fc2e39e 7449 break;
252b5132
RH
7450
7451 case M_SGT: /* sreg > treg <==> treg < sreg */
7452 s = "slt";
7453 goto sgt;
7454 case M_SGTU:
7455 s = "sltu";
7456 sgt:
67c0d1eb 7457 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
8fc2e39e 7458 break;
252b5132
RH
7459
7460 case M_SGT_I: /* sreg > I <==> I < sreg */
7461 s = "slt";
7462 goto sgti;
7463 case M_SGTU_I:
7464 s = "sltu";
7465 sgti:
8fc2e39e 7466 used_at = 1;
67c0d1eb
RS
7467 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7468 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
252b5132
RH
7469 break;
7470
2396cfb9 7471 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
252b5132
RH
7472 s = "slt";
7473 goto sle;
7474 case M_SLEU:
7475 s = "sltu";
7476 sle:
67c0d1eb
RS
7477 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
7478 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
8fc2e39e 7479 break;
252b5132 7480
2396cfb9 7481 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
252b5132
RH
7482 s = "slt";
7483 goto slei;
7484 case M_SLEU_I:
7485 s = "sltu";
7486 slei:
8fc2e39e 7487 used_at = 1;
67c0d1eb
RS
7488 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7489 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
7490 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
252b5132
RH
7491 break;
7492
7493 case M_SLT_I:
7494 if (imm_expr.X_op == O_constant
7495 && imm_expr.X_add_number >= -0x8000
7496 && imm_expr.X_add_number < 0x8000)
7497 {
67c0d1eb 7498 macro_build (&imm_expr, "slti", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
8fc2e39e 7499 break;
252b5132 7500 }
8fc2e39e 7501 used_at = 1;
67c0d1eb
RS
7502 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7503 macro_build (NULL, "slt", "d,v,t", dreg, sreg, AT);
252b5132
RH
7504 break;
7505
7506 case M_SLTU_I:
7507 if (imm_expr.X_op == O_constant
7508 && imm_expr.X_add_number >= -0x8000
7509 && imm_expr.X_add_number < 0x8000)
7510 {
67c0d1eb 7511 macro_build (&imm_expr, "sltiu", "t,r,j", dreg, sreg,
17a2f251 7512 BFD_RELOC_LO16);
8fc2e39e 7513 break;
252b5132 7514 }
8fc2e39e 7515 used_at = 1;
67c0d1eb
RS
7516 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7517 macro_build (NULL, "sltu", "d,v,t", dreg, sreg, AT);
252b5132
RH
7518 break;
7519
7520 case M_SNE:
7521 if (sreg == 0)
67c0d1eb 7522 macro_build (NULL, "sltu", "d,v,t", dreg, 0, treg);
252b5132 7523 else if (treg == 0)
67c0d1eb 7524 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
252b5132
RH
7525 else
7526 {
67c0d1eb
RS
7527 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
7528 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
252b5132 7529 }
8fc2e39e 7530 break;
252b5132
RH
7531
7532 case M_SNE_I:
7533 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7534 {
67c0d1eb 7535 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
8fc2e39e 7536 break;
252b5132
RH
7537 }
7538 if (sreg == 0)
7539 {
7540 as_warn (_("Instruction %s: result is always true"),
7541 ip->insn_mo->name);
67c0d1eb
RS
7542 macro_build (&expr1, HAVE_32BIT_GPRS ? "addiu" : "daddiu", "t,r,j",
7543 dreg, 0, BFD_RELOC_LO16);
8fc2e39e 7544 break;
252b5132
RH
7545 }
7546 if (imm_expr.X_op == O_constant
7547 && imm_expr.X_add_number >= 0
7548 && imm_expr.X_add_number < 0x10000)
7549 {
67c0d1eb 7550 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7551 }
7552 else if (imm_expr.X_op == O_constant
7553 && imm_expr.X_add_number > -0x8000
7554 && imm_expr.X_add_number < 0)
7555 {
7556 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 7557 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
17a2f251 7558 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7559 }
7560 else
7561 {
67c0d1eb
RS
7562 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7563 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
252b5132
RH
7564 used_at = 1;
7565 }
67c0d1eb 7566 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
8fc2e39e 7567 break;
252b5132
RH
7568
7569 case M_DSUB_I:
7570 dbl = 1;
7571 case M_SUB_I:
7572 if (imm_expr.X_op == O_constant
7573 && imm_expr.X_add_number > -0x8000
7574 && imm_expr.X_add_number <= 0x8000)
7575 {
7576 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb
RS
7577 macro_build (&imm_expr, dbl ? "daddi" : "addi", "t,r,j",
7578 dreg, sreg, BFD_RELOC_LO16);
8fc2e39e 7579 break;
252b5132 7580 }
8fc2e39e 7581 used_at = 1;
67c0d1eb
RS
7582 load_register (AT, &imm_expr, dbl);
7583 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
252b5132
RH
7584 break;
7585
7586 case M_DSUBU_I:
7587 dbl = 1;
7588 case M_SUBU_I:
7589 if (imm_expr.X_op == O_constant
7590 && imm_expr.X_add_number > -0x8000
7591 && imm_expr.X_add_number <= 0x8000)
7592 {
7593 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb
RS
7594 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "t,r,j",
7595 dreg, sreg, BFD_RELOC_LO16);
8fc2e39e 7596 break;
252b5132 7597 }
8fc2e39e 7598 used_at = 1;
67c0d1eb
RS
7599 load_register (AT, &imm_expr, dbl);
7600 macro_build (NULL, dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
252b5132
RH
7601 break;
7602
7603 case M_TEQ_I:
7604 s = "teq";
7605 goto trap;
7606 case M_TGE_I:
7607 s = "tge";
7608 goto trap;
7609 case M_TGEU_I:
7610 s = "tgeu";
7611 goto trap;
7612 case M_TLT_I:
7613 s = "tlt";
7614 goto trap;
7615 case M_TLTU_I:
7616 s = "tltu";
7617 goto trap;
7618 case M_TNE_I:
7619 s = "tne";
7620 trap:
8fc2e39e 7621 used_at = 1;
67c0d1eb
RS
7622 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7623 macro_build (NULL, s, "s,t", sreg, AT);
252b5132
RH
7624 break;
7625
252b5132 7626 case M_TRUNCWS:
43841e91 7627 case M_TRUNCWD:
e7af610e 7628 assert (mips_opts.isa == ISA_MIPS1);
8fc2e39e 7629 used_at = 1;
252b5132
RH
7630 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
7631 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
7632
7633 /*
7634 * Is the double cfc1 instruction a bug in the mips assembler;
7635 * or is there a reason for it?
7636 */
7d10b47d 7637 start_noreorder ();
67c0d1eb
RS
7638 macro_build (NULL, "cfc1", "t,G", treg, RA);
7639 macro_build (NULL, "cfc1", "t,G", treg, RA);
7640 macro_build (NULL, "nop", "");
252b5132 7641 expr1.X_add_number = 3;
67c0d1eb 7642 macro_build (&expr1, "ori", "t,r,i", AT, treg, BFD_RELOC_LO16);
252b5132 7643 expr1.X_add_number = 2;
67c0d1eb
RS
7644 macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
7645 macro_build (NULL, "ctc1", "t,G", AT, RA);
7646 macro_build (NULL, "nop", "");
7647 macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
7648 dreg, sreg);
7649 macro_build (NULL, "ctc1", "t,G", treg, RA);
7650 macro_build (NULL, "nop", "");
7d10b47d 7651 end_noreorder ();
252b5132
RH
7652 break;
7653
7654 case M_ULH:
7655 s = "lb";
7656 goto ulh;
7657 case M_ULHU:
7658 s = "lbu";
7659 ulh:
8fc2e39e 7660 used_at = 1;
252b5132
RH
7661 if (offset_expr.X_add_number >= 0x7fff)
7662 as_bad (_("operand overflow"));
252b5132 7663 if (! target_big_endian)
f9419b05 7664 ++offset_expr.X_add_number;
67c0d1eb 7665 macro_build (&offset_expr, s, "t,o(b)", AT, BFD_RELOC_LO16, breg);
252b5132 7666 if (! target_big_endian)
f9419b05 7667 --offset_expr.X_add_number;
252b5132 7668 else
f9419b05 7669 ++offset_expr.X_add_number;
67c0d1eb
RS
7670 macro_build (&offset_expr, "lbu", "t,o(b)", treg, BFD_RELOC_LO16, breg);
7671 macro_build (NULL, "sll", "d,w,<", AT, AT, 8);
7672 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
252b5132
RH
7673 break;
7674
7675 case M_ULD:
7676 s = "ldl";
7677 s2 = "ldr";
7678 off = 7;
7679 goto ulw;
7680 case M_ULW:
7681 s = "lwl";
7682 s2 = "lwr";
7683 off = 3;
7684 ulw:
7685 if (offset_expr.X_add_number >= 0x8000 - off)
7686 as_bad (_("operand overflow"));
af22f5b2
CD
7687 if (treg != breg)
7688 tempreg = treg;
7689 else
8fc2e39e
TS
7690 {
7691 used_at = 1;
7692 tempreg = AT;
7693 }
252b5132
RH
7694 if (! target_big_endian)
7695 offset_expr.X_add_number += off;
67c0d1eb 7696 macro_build (&offset_expr, s, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
252b5132
RH
7697 if (! target_big_endian)
7698 offset_expr.X_add_number -= off;
7699 else
7700 offset_expr.X_add_number += off;
67c0d1eb 7701 macro_build (&offset_expr, s2, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
af22f5b2
CD
7702
7703 /* If necessary, move the result in tempreg the final destination. */
7704 if (treg == tempreg)
8fc2e39e 7705 break;
af22f5b2 7706 /* Protect second load's delay slot. */
017315e4 7707 load_delay_nop ();
67c0d1eb 7708 move_register (treg, tempreg);
af22f5b2 7709 break;
252b5132
RH
7710
7711 case M_ULD_A:
7712 s = "ldl";
7713 s2 = "ldr";
7714 off = 7;
7715 goto ulwa;
7716 case M_ULW_A:
7717 s = "lwl";
7718 s2 = "lwr";
7719 off = 3;
7720 ulwa:
d6bc6245 7721 used_at = 1;
67c0d1eb 7722 load_address (AT, &offset_expr, &used_at);
252b5132 7723 if (breg != 0)
67c0d1eb 7724 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
252b5132
RH
7725 if (! target_big_endian)
7726 expr1.X_add_number = off;
7727 else
7728 expr1.X_add_number = 0;
67c0d1eb 7729 macro_build (&expr1, s, "t,o(b)", treg, BFD_RELOC_LO16, AT);
252b5132
RH
7730 if (! target_big_endian)
7731 expr1.X_add_number = 0;
7732 else
7733 expr1.X_add_number = off;
67c0d1eb 7734 macro_build (&expr1, s2, "t,o(b)", treg, BFD_RELOC_LO16, AT);
252b5132
RH
7735 break;
7736
7737 case M_ULH_A:
7738 case M_ULHU_A:
d6bc6245 7739 used_at = 1;
67c0d1eb 7740 load_address (AT, &offset_expr, &used_at);
252b5132 7741 if (breg != 0)
67c0d1eb 7742 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
252b5132
RH
7743 if (target_big_endian)
7744 expr1.X_add_number = 0;
67c0d1eb 7745 macro_build (&expr1, mask == M_ULH_A ? "lb" : "lbu", "t,o(b)",
17a2f251 7746 treg, BFD_RELOC_LO16, AT);
252b5132
RH
7747 if (target_big_endian)
7748 expr1.X_add_number = 1;
7749 else
7750 expr1.X_add_number = 0;
67c0d1eb
RS
7751 macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
7752 macro_build (NULL, "sll", "d,w,<", treg, treg, 8);
7753 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
252b5132
RH
7754 break;
7755
7756 case M_USH:
8fc2e39e 7757 used_at = 1;
252b5132
RH
7758 if (offset_expr.X_add_number >= 0x7fff)
7759 as_bad (_("operand overflow"));
7760 if (target_big_endian)
f9419b05 7761 ++offset_expr.X_add_number;
67c0d1eb
RS
7762 macro_build (&offset_expr, "sb", "t,o(b)", treg, BFD_RELOC_LO16, breg);
7763 macro_build (NULL, "srl", "d,w,<", AT, treg, 8);
252b5132 7764 if (target_big_endian)
f9419b05 7765 --offset_expr.X_add_number;
252b5132 7766 else
f9419b05 7767 ++offset_expr.X_add_number;
67c0d1eb 7768 macro_build (&offset_expr, "sb", "t,o(b)", AT, BFD_RELOC_LO16, breg);
252b5132
RH
7769 break;
7770
7771 case M_USD:
7772 s = "sdl";
7773 s2 = "sdr";
7774 off = 7;
7775 goto usw;
7776 case M_USW:
7777 s = "swl";
7778 s2 = "swr";
7779 off = 3;
7780 usw:
7781 if (offset_expr.X_add_number >= 0x8000 - off)
7782 as_bad (_("operand overflow"));
7783 if (! target_big_endian)
7784 offset_expr.X_add_number += off;
67c0d1eb 7785 macro_build (&offset_expr, s, "t,o(b)", treg, BFD_RELOC_LO16, breg);
252b5132
RH
7786 if (! target_big_endian)
7787 offset_expr.X_add_number -= off;
7788 else
7789 offset_expr.X_add_number += off;
67c0d1eb 7790 macro_build (&offset_expr, s2, "t,o(b)", treg, BFD_RELOC_LO16, breg);
8fc2e39e 7791 break;
252b5132
RH
7792
7793 case M_USD_A:
7794 s = "sdl";
7795 s2 = "sdr";
7796 off = 7;
7797 goto uswa;
7798 case M_USW_A:
7799 s = "swl";
7800 s2 = "swr";
7801 off = 3;
7802 uswa:
d6bc6245 7803 used_at = 1;
67c0d1eb 7804 load_address (AT, &offset_expr, &used_at);
252b5132 7805 if (breg != 0)
67c0d1eb 7806 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
252b5132
RH
7807 if (! target_big_endian)
7808 expr1.X_add_number = off;
7809 else
7810 expr1.X_add_number = 0;
67c0d1eb 7811 macro_build (&expr1, s, "t,o(b)", treg, BFD_RELOC_LO16, AT);
252b5132
RH
7812 if (! target_big_endian)
7813 expr1.X_add_number = 0;
7814 else
7815 expr1.X_add_number = off;
67c0d1eb 7816 macro_build (&expr1, s2, "t,o(b)", treg, BFD_RELOC_LO16, AT);
252b5132
RH
7817 break;
7818
7819 case M_USH_A:
d6bc6245 7820 used_at = 1;
67c0d1eb 7821 load_address (AT, &offset_expr, &used_at);
252b5132 7822 if (breg != 0)
67c0d1eb 7823 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
252b5132
RH
7824 if (! target_big_endian)
7825 expr1.X_add_number = 0;
67c0d1eb
RS
7826 macro_build (&expr1, "sb", "t,o(b)", treg, BFD_RELOC_LO16, AT);
7827 macro_build (NULL, "srl", "d,w,<", treg, treg, 8);
252b5132
RH
7828 if (! target_big_endian)
7829 expr1.X_add_number = 1;
7830 else
7831 expr1.X_add_number = 0;
67c0d1eb 7832 macro_build (&expr1, "sb", "t,o(b)", treg, BFD_RELOC_LO16, AT);
252b5132
RH
7833 if (! target_big_endian)
7834 expr1.X_add_number = 0;
7835 else
7836 expr1.X_add_number = 1;
67c0d1eb
RS
7837 macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
7838 macro_build (NULL, "sll", "d,w,<", treg, treg, 8);
7839 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
252b5132
RH
7840 break;
7841
7842 default:
7843 /* FIXME: Check if this is one of the itbl macros, since they
bdaaa2e1 7844 are added dynamically. */
252b5132
RH
7845 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7846 break;
7847 }
8fc2e39e
TS
7848 if (mips_opts.noat && used_at)
7849 as_bad (_("Macro used $at after \".set noat\""));
252b5132
RH
7850}
7851
7852/* Implement macros in mips16 mode. */
7853
7854static void
17a2f251 7855mips16_macro (struct mips_cl_insn *ip)
252b5132
RH
7856{
7857 int mask;
7858 int xreg, yreg, zreg, tmp;
252b5132
RH
7859 expressionS expr1;
7860 int dbl;
7861 const char *s, *s2, *s3;
7862
7863 mask = ip->insn_mo->mask;
7864
bf12938e
RS
7865 xreg = MIPS16_EXTRACT_OPERAND (RX, *ip);
7866 yreg = MIPS16_EXTRACT_OPERAND (RY, *ip);
7867 zreg = MIPS16_EXTRACT_OPERAND (RZ, *ip);
252b5132 7868
252b5132
RH
7869 expr1.X_op = O_constant;
7870 expr1.X_op_symbol = NULL;
7871 expr1.X_add_symbol = NULL;
7872 expr1.X_add_number = 1;
7873
7874 dbl = 0;
7875
7876 switch (mask)
7877 {
7878 default:
7879 internalError ();
7880
7881 case M_DDIV_3:
7882 dbl = 1;
7883 case M_DIV_3:
7884 s = "mflo";
7885 goto do_div3;
7886 case M_DREM_3:
7887 dbl = 1;
7888 case M_REM_3:
7889 s = "mfhi";
7890 do_div3:
7d10b47d 7891 start_noreorder ();
67c0d1eb 7892 macro_build (NULL, dbl ? "ddiv" : "div", "0,x,y", xreg, yreg);
252b5132 7893 expr1.X_add_number = 2;
67c0d1eb
RS
7894 macro_build (&expr1, "bnez", "x,p", yreg);
7895 macro_build (NULL, "break", "6", 7);
bdaaa2e1 7896
252b5132
RH
7897 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7898 since that causes an overflow. We should do that as well,
7899 but I don't see how to do the comparisons without a temporary
7900 register. */
7d10b47d 7901 end_noreorder ();
67c0d1eb 7902 macro_build (NULL, s, "x", zreg);
252b5132
RH
7903 break;
7904
7905 case M_DIVU_3:
7906 s = "divu";
7907 s2 = "mflo";
7908 goto do_divu3;
7909 case M_REMU_3:
7910 s = "divu";
7911 s2 = "mfhi";
7912 goto do_divu3;
7913 case M_DDIVU_3:
7914 s = "ddivu";
7915 s2 = "mflo";
7916 goto do_divu3;
7917 case M_DREMU_3:
7918 s = "ddivu";
7919 s2 = "mfhi";
7920 do_divu3:
7d10b47d 7921 start_noreorder ();
67c0d1eb 7922 macro_build (NULL, s, "0,x,y", xreg, yreg);
252b5132 7923 expr1.X_add_number = 2;
67c0d1eb
RS
7924 macro_build (&expr1, "bnez", "x,p", yreg);
7925 macro_build (NULL, "break", "6", 7);
7d10b47d 7926 end_noreorder ();
67c0d1eb 7927 macro_build (NULL, s2, "x", zreg);
252b5132
RH
7928 break;
7929
7930 case M_DMUL:
7931 dbl = 1;
7932 case M_MUL:
67c0d1eb
RS
7933 macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
7934 macro_build (NULL, "mflo", "x", zreg);
8fc2e39e 7935 break;
252b5132
RH
7936
7937 case M_DSUBU_I:
7938 dbl = 1;
7939 goto do_subu;
7940 case M_SUBU_I:
7941 do_subu:
7942 if (imm_expr.X_op != O_constant)
7943 as_bad (_("Unsupported large constant"));
7944 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 7945 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
252b5132
RH
7946 break;
7947
7948 case M_SUBU_I_2:
7949 if (imm_expr.X_op != O_constant)
7950 as_bad (_("Unsupported large constant"));
7951 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 7952 macro_build (&imm_expr, "addiu", "x,k", xreg);
252b5132
RH
7953 break;
7954
7955 case M_DSUBU_I_2:
7956 if (imm_expr.X_op != O_constant)
7957 as_bad (_("Unsupported large constant"));
7958 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 7959 macro_build (&imm_expr, "daddiu", "y,j", yreg);
252b5132
RH
7960 break;
7961
7962 case M_BEQ:
7963 s = "cmp";
7964 s2 = "bteqz";
7965 goto do_branch;
7966 case M_BNE:
7967 s = "cmp";
7968 s2 = "btnez";
7969 goto do_branch;
7970 case M_BLT:
7971 s = "slt";
7972 s2 = "btnez";
7973 goto do_branch;
7974 case M_BLTU:
7975 s = "sltu";
7976 s2 = "btnez";
7977 goto do_branch;
7978 case M_BLE:
7979 s = "slt";
7980 s2 = "bteqz";
7981 goto do_reverse_branch;
7982 case M_BLEU:
7983 s = "sltu";
7984 s2 = "bteqz";
7985 goto do_reverse_branch;
7986 case M_BGE:
7987 s = "slt";
7988 s2 = "bteqz";
7989 goto do_branch;
7990 case M_BGEU:
7991 s = "sltu";
7992 s2 = "bteqz";
7993 goto do_branch;
7994 case M_BGT:
7995 s = "slt";
7996 s2 = "btnez";
7997 goto do_reverse_branch;
7998 case M_BGTU:
7999 s = "sltu";
8000 s2 = "btnez";
8001
8002 do_reverse_branch:
8003 tmp = xreg;
8004 xreg = yreg;
8005 yreg = tmp;
8006
8007 do_branch:
67c0d1eb
RS
8008 macro_build (NULL, s, "x,y", xreg, yreg);
8009 macro_build (&offset_expr, s2, "p");
252b5132
RH
8010 break;
8011
8012 case M_BEQ_I:
8013 s = "cmpi";
8014 s2 = "bteqz";
8015 s3 = "x,U";
8016 goto do_branch_i;
8017 case M_BNE_I:
8018 s = "cmpi";
8019 s2 = "btnez";
8020 s3 = "x,U";
8021 goto do_branch_i;
8022 case M_BLT_I:
8023 s = "slti";
8024 s2 = "btnez";
8025 s3 = "x,8";
8026 goto do_branch_i;
8027 case M_BLTU_I:
8028 s = "sltiu";
8029 s2 = "btnez";
8030 s3 = "x,8";
8031 goto do_branch_i;
8032 case M_BLE_I:
8033 s = "slti";
8034 s2 = "btnez";
8035 s3 = "x,8";
8036 goto do_addone_branch_i;
8037 case M_BLEU_I:
8038 s = "sltiu";
8039 s2 = "btnez";
8040 s3 = "x,8";
8041 goto do_addone_branch_i;
8042 case M_BGE_I:
8043 s = "slti";
8044 s2 = "bteqz";
8045 s3 = "x,8";
8046 goto do_branch_i;
8047 case M_BGEU_I:
8048 s = "sltiu";
8049 s2 = "bteqz";
8050 s3 = "x,8";
8051 goto do_branch_i;
8052 case M_BGT_I:
8053 s = "slti";
8054 s2 = "bteqz";
8055 s3 = "x,8";
8056 goto do_addone_branch_i;
8057 case M_BGTU_I:
8058 s = "sltiu";
8059 s2 = "bteqz";
8060 s3 = "x,8";
8061
8062 do_addone_branch_i:
8063 if (imm_expr.X_op != O_constant)
8064 as_bad (_("Unsupported large constant"));
8065 ++imm_expr.X_add_number;
8066
8067 do_branch_i:
67c0d1eb
RS
8068 macro_build (&imm_expr, s, s3, xreg);
8069 macro_build (&offset_expr, s2, "p");
252b5132
RH
8070 break;
8071
8072 case M_ABS:
8073 expr1.X_add_number = 0;
67c0d1eb 8074 macro_build (&expr1, "slti", "x,8", yreg);
252b5132 8075 if (xreg != yreg)
67c0d1eb 8076 move_register (xreg, yreg);
252b5132 8077 expr1.X_add_number = 2;
67c0d1eb
RS
8078 macro_build (&expr1, "bteqz", "p");
8079 macro_build (NULL, "neg", "x,w", xreg, xreg);
252b5132
RH
8080 }
8081}
8082
8083/* For consistency checking, verify that all bits are specified either
8084 by the match/mask part of the instruction definition, or by the
8085 operand list. */
8086static int
17a2f251 8087validate_mips_insn (const struct mips_opcode *opc)
252b5132
RH
8088{
8089 const char *p = opc->args;
8090 char c;
8091 unsigned long used_bits = opc->mask;
8092
8093 if ((used_bits & opc->match) != opc->match)
8094 {
8095 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
8096 opc->name, opc->args);
8097 return 0;
8098 }
8099#define USE_BITS(mask,shift) (used_bits |= ((mask) << (shift)))
8100 while (*p)
8101 switch (c = *p++)
8102 {
8103 case ',': break;
8104 case '(': break;
8105 case ')': break;
af7ee8bf
CD
8106 case '+':
8107 switch (c = *p++)
8108 {
9bcd4f99
TS
8109 case '1': USE_BITS (OP_MASK_UDI1, OP_SH_UDI1); break;
8110 case '2': USE_BITS (OP_MASK_UDI2, OP_SH_UDI2); break;
8111 case '3': USE_BITS (OP_MASK_UDI3, OP_SH_UDI3); break;
8112 case '4': USE_BITS (OP_MASK_UDI4, OP_SH_UDI4); break;
af7ee8bf
CD
8113 case 'A': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
8114 case 'B': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
8115 case 'C': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
bbcc0807
CD
8116 case 'D': USE_BITS (OP_MASK_RD, OP_SH_RD);
8117 USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
5f74bc13
CD
8118 case 'E': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
8119 case 'F': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
8120 case 'G': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
8121 case 'H': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
8122 case 'I': break;
ef2e4d86
CF
8123 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8124 case 'T': USE_BITS (OP_MASK_RT, OP_SH_RT);
8125 USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
af7ee8bf
CD
8126 default:
8127 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8128 c, opc->name, opc->args);
8129 return 0;
8130 }
8131 break;
252b5132
RH
8132 case '<': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
8133 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
8134 case 'A': break;
4372b673 8135 case 'B': USE_BITS (OP_MASK_CODE20, OP_SH_CODE20); break;
252b5132
RH
8136 case 'C': USE_BITS (OP_MASK_COPZ, OP_SH_COPZ); break;
8137 case 'D': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
8138 case 'E': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8139 case 'F': break;
8140 case 'G': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
156c2f8b 8141 case 'H': USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
252b5132 8142 case 'I': break;
e972090a 8143 case 'J': USE_BITS (OP_MASK_CODE19, OP_SH_CODE19); break;
af7ee8bf 8144 case 'K': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
252b5132
RH
8145 case 'L': break;
8146 case 'M': USE_BITS (OP_MASK_CCC, OP_SH_CCC); break;
8147 case 'N': USE_BITS (OP_MASK_BCC, OP_SH_BCC); break;
deec1734
CD
8148 case 'O': USE_BITS (OP_MASK_ALN, OP_SH_ALN); break;
8149 case 'Q': USE_BITS (OP_MASK_VSEL, OP_SH_VSEL);
8150 USE_BITS (OP_MASK_FT, OP_SH_FT); break;
252b5132
RH
8151 case 'R': USE_BITS (OP_MASK_FR, OP_SH_FR); break;
8152 case 'S': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
8153 case 'T': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
8154 case 'V': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
8155 case 'W': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
deec1734
CD
8156 case 'X': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
8157 case 'Y': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
8158 case 'Z': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
252b5132
RH
8159 case 'a': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break;
8160 case 'b': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8161 case 'c': USE_BITS (OP_MASK_CODE, OP_SH_CODE); break;
8162 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
8163 case 'f': break;
8164 case 'h': USE_BITS (OP_MASK_PREFX, OP_SH_PREFX); break;
8165 case 'i': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
8166 case 'j': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
8167 case 'k': USE_BITS (OP_MASK_CACHE, OP_SH_CACHE); break;
8168 case 'l': break;
8169 case 'o': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
8170 case 'p': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
8171 case 'q': USE_BITS (OP_MASK_CODE2, OP_SH_CODE2); break;
8172 case 'r': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8173 case 's': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8174 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8175 case 'u': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
8176 case 'v': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8177 case 'w': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8178 case 'x': break;
8179 case 'z': break;
8180 case 'P': USE_BITS (OP_MASK_PERFREG, OP_SH_PERFREG); break;
4372b673
NC
8181 case 'U': USE_BITS (OP_MASK_RD, OP_SH_RD);
8182 USE_BITS (OP_MASK_RT, OP_SH_RT); break;
60b63b72
RS
8183 case 'e': USE_BITS (OP_MASK_VECBYTE, OP_SH_VECBYTE); break;
8184 case '%': USE_BITS (OP_MASK_VECALIGN, OP_SH_VECALIGN); break;
8185 case '[': break;
8186 case ']': break;
74cd071d
CF
8187 case '3': USE_BITS (OP_MASK_SA3, OP_SH_SA3); break;
8188 case '4': USE_BITS (OP_MASK_SA4, OP_SH_SA4); break;
8189 case '5': USE_BITS (OP_MASK_IMM8, OP_SH_IMM8); break;
8190 case '6': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8191 case '7': USE_BITS (OP_MASK_DSPACC, OP_SH_DSPACC); break;
8192 case '8': USE_BITS (OP_MASK_WRDSP, OP_SH_WRDSP); break;
8193 case '9': USE_BITS (OP_MASK_DSPACC_S, OP_SH_DSPACC_S);break;
8194 case '0': USE_BITS (OP_MASK_DSPSFT, OP_SH_DSPSFT); break;
8195 case '\'': USE_BITS (OP_MASK_RDDSP, OP_SH_RDDSP); break;
8196 case ':': USE_BITS (OP_MASK_DSPSFT_7, OP_SH_DSPSFT_7);break;
8197 case '@': USE_BITS (OP_MASK_IMM10, OP_SH_IMM10); break;
ef2e4d86
CF
8198 case '!': USE_BITS (OP_MASK_MT_U, OP_SH_MT_U); break;
8199 case '$': USE_BITS (OP_MASK_MT_H, OP_SH_MT_H); break;
8200 case '*': USE_BITS (OP_MASK_MTACC_T, OP_SH_MTACC_T); break;
8201 case '&': USE_BITS (OP_MASK_MTACC_D, OP_SH_MTACC_D); break;
8202 case 'g': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
252b5132
RH
8203 default:
8204 as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
8205 c, opc->name, opc->args);
8206 return 0;
8207 }
8208#undef USE_BITS
8209 if (used_bits != 0xffffffff)
8210 {
8211 as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
8212 ~used_bits & 0xffffffff, opc->name, opc->args);
8213 return 0;
8214 }
8215 return 1;
8216}
8217
9bcd4f99
TS
8218/* UDI immediates. */
8219struct mips_immed {
8220 char type;
8221 unsigned int shift;
8222 unsigned long mask;
8223 const char * desc;
8224};
8225
8226static const struct mips_immed mips_immed[] = {
8227 { '1', OP_SH_UDI1, OP_MASK_UDI1, 0},
8228 { '2', OP_SH_UDI2, OP_MASK_UDI2, 0},
8229 { '3', OP_SH_UDI3, OP_MASK_UDI3, 0},
8230 { '4', OP_SH_UDI4, OP_MASK_UDI4, 0},
8231 { 0,0,0,0 }
8232};
8233
7455baf8
TS
8234/* Check whether an odd floating-point register is allowed. */
8235static int
8236mips_oddfpreg_ok (const struct mips_opcode *insn, int argnum)
8237{
8238 const char *s = insn->name;
8239
8240 if (insn->pinfo == INSN_MACRO)
8241 /* Let a macro pass, we'll catch it later when it is expanded. */
8242 return 1;
8243
8244 if (ISA_HAS_ODD_SINGLE_FPR (mips_opts.isa))
8245 {
8246 /* Allow odd registers for single-precision ops. */
8247 switch (insn->pinfo & (FP_S | FP_D))
8248 {
8249 case FP_S:
8250 case 0:
8251 return 1; /* both single precision - ok */
8252 case FP_D:
8253 return 0; /* both double precision - fail */
8254 default:
8255 break;
8256 }
8257
8258 /* Cvt.w.x and cvt.x.w allow an odd register for a 'w' or 's' operand. */
8259 s = strchr (insn->name, '.');
8260 if (argnum == 2)
8261 s = s != NULL ? strchr (s + 1, '.') : NULL;
8262 return (s != NULL && (s[1] == 'w' || s[1] == 's'));
8263 }
8264
8265 /* Single-precision coprocessor loads and moves are OK too. */
8266 if ((insn->pinfo & FP_S)
8267 && (insn->pinfo & (INSN_COPROC_MEMORY_DELAY | INSN_STORE_MEMORY
8268 | INSN_LOAD_COPROC_DELAY | INSN_COPROC_MOVE_DELAY)))
8269 return 1;
8270
8271 return 0;
8272}
8273
252b5132
RH
8274/* This routine assembles an instruction into its binary format. As a
8275 side effect, it sets one of the global variables imm_reloc or
8276 offset_reloc to the type of relocation to do if one of the operands
8277 is an address expression. */
8278
8279static void
17a2f251 8280mips_ip (char *str, struct mips_cl_insn *ip)
252b5132
RH
8281{
8282 char *s;
8283 const char *args;
43841e91 8284 char c = 0;
252b5132
RH
8285 struct mips_opcode *insn;
8286 char *argsStart;
8287 unsigned int regno;
8288 unsigned int lastregno = 0;
af7ee8bf 8289 unsigned int lastpos = 0;
071742cf 8290 unsigned int limlo, limhi;
252b5132
RH
8291 char *s_reset;
8292 char save_c = 0;
74cd071d 8293 offsetT min_range, max_range;
707bfff6
TS
8294 int argnum;
8295 unsigned int rtype;
252b5132
RH
8296
8297 insn_error = NULL;
8298
8299 /* If the instruction contains a '.', we first try to match an instruction
8300 including the '.'. Then we try again without the '.'. */
8301 insn = NULL;
3882b010 8302 for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
252b5132
RH
8303 continue;
8304
8305 /* If we stopped on whitespace, then replace the whitespace with null for
8306 the call to hash_find. Save the character we replaced just in case we
8307 have to re-parse the instruction. */
3882b010 8308 if (ISSPACE (*s))
252b5132
RH
8309 {
8310 save_c = *s;
8311 *s++ = '\0';
8312 }
bdaaa2e1 8313
252b5132
RH
8314 insn = (struct mips_opcode *) hash_find (op_hash, str);
8315
8316 /* If we didn't find the instruction in the opcode table, try again, but
8317 this time with just the instruction up to, but not including the
8318 first '.'. */
8319 if (insn == NULL)
8320 {
bdaaa2e1 8321 /* Restore the character we overwrite above (if any). */
252b5132
RH
8322 if (save_c)
8323 *(--s) = save_c;
8324
8325 /* Scan up to the first '.' or whitespace. */
3882b010
L
8326 for (s = str;
8327 *s != '\0' && *s != '.' && !ISSPACE (*s);
8328 ++s)
252b5132
RH
8329 continue;
8330
8331 /* If we did not find a '.', then we can quit now. */
8332 if (*s != '.')
8333 {
8334 insn_error = "unrecognized opcode";
8335 return;
8336 }
8337
8338 /* Lookup the instruction in the hash table. */
8339 *s++ = '\0';
8340 if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
8341 {
8342 insn_error = "unrecognized opcode";
8343 return;
8344 }
252b5132
RH
8345 }
8346
8347 argsStart = s;
8348 for (;;)
8349 {
b34976b6 8350 bfd_boolean ok;
252b5132
RH
8351
8352 assert (strcmp (insn->name, str) == 0);
8353
1f25f5d3
CD
8354 if (OPCODE_IS_MEMBER (insn,
8355 (mips_opts.isa
9b3f89ee
TS
8356 /* We don't check for mips_opts.mips16 here since
8357 we want to allow jalx if -mips16 was specified
8358 on the command line. */
3396de36 8359 | (file_ase_mips16 ? INSN_MIPS16 : 0)
deec1734 8360 | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
74cd071d 8361 | (mips_opts.ase_dsp ? INSN_DSP : 0)
65263ce3
TS
8362 | ((mips_opts.ase_dsp && ISA_SUPPORTS_DSP64_ASE)
8363 ? INSN_DSP64 : 0)
ef2e4d86 8364 | (mips_opts.ase_mt ? INSN_MT : 0)
e16bfa71
TS
8365 | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)
8366 | (mips_opts.ase_smartmips ? INSN_SMARTMIPS : 0)),
fef14a42 8367 mips_opts.arch))
b34976b6 8368 ok = TRUE;
bdaaa2e1 8369 else
b34976b6 8370 ok = FALSE;
bdaaa2e1 8371
252b5132
RH
8372 if (insn->pinfo != INSN_MACRO)
8373 {
fef14a42 8374 if (mips_opts.arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
b34976b6 8375 ok = FALSE;
252b5132
RH
8376 }
8377
8378 if (! ok)
8379 {
8380 if (insn + 1 < &mips_opcodes[NUMOPCODES]
8381 && strcmp (insn->name, insn[1].name) == 0)
8382 {
8383 ++insn;
8384 continue;
8385 }
252b5132 8386 else
beae10d5 8387 {
268f6bed
L
8388 if (!insn_error)
8389 {
8390 static char buf[100];
fef14a42
TS
8391 sprintf (buf,
8392 _("opcode not supported on this processor: %s (%s)"),
8393 mips_cpu_info_from_arch (mips_opts.arch)->name,
8394 mips_cpu_info_from_isa (mips_opts.isa)->name);
268f6bed
L
8395 insn_error = buf;
8396 }
8397 if (save_c)
8398 *(--s) = save_c;
2bd7f1f3 8399 return;
252b5132 8400 }
252b5132
RH
8401 }
8402
1e915849 8403 create_insn (ip, insn);
268f6bed 8404 insn_error = NULL;
707bfff6 8405 argnum = 1;
252b5132
RH
8406 for (args = insn->args;; ++args)
8407 {
deec1734
CD
8408 int is_mdmx;
8409
ad8d3bb3 8410 s += strspn (s, " \t");
deec1734 8411 is_mdmx = 0;
252b5132
RH
8412 switch (*args)
8413 {
8414 case '\0': /* end of args */
8415 if (*s == '\0')
8416 return;
8417 break;
8418
74cd071d
CF
8419 case '3': /* dsp 3-bit unsigned immediate in bit 21 */
8420 my_getExpression (&imm_expr, s);
8421 check_absolute_expr (ip, &imm_expr);
8422 if (imm_expr.X_add_number & ~OP_MASK_SA3)
8423 {
a9e24354
TS
8424 as_bad (_("DSP immediate not in range 0..%d (%lu)"),
8425 OP_MASK_SA3, (unsigned long) imm_expr.X_add_number);
74cd071d 8426 }
a9e24354 8427 INSERT_OPERAND (SA3, *ip, imm_expr.X_add_number);
74cd071d
CF
8428 imm_expr.X_op = O_absent;
8429 s = expr_end;
8430 continue;
8431
8432 case '4': /* dsp 4-bit unsigned immediate in bit 21 */
8433 my_getExpression (&imm_expr, s);
8434 check_absolute_expr (ip, &imm_expr);
8435 if (imm_expr.X_add_number & ~OP_MASK_SA4)
8436 {
a9e24354
TS
8437 as_bad (_("DSP immediate not in range 0..%d (%lu)"),
8438 OP_MASK_SA4, (unsigned long) imm_expr.X_add_number);
74cd071d 8439 }
a9e24354 8440 INSERT_OPERAND (SA4, *ip, imm_expr.X_add_number);
74cd071d
CF
8441 imm_expr.X_op = O_absent;
8442 s = expr_end;
8443 continue;
8444
8445 case '5': /* dsp 8-bit unsigned immediate in bit 16 */
8446 my_getExpression (&imm_expr, s);
8447 check_absolute_expr (ip, &imm_expr);
8448 if (imm_expr.X_add_number & ~OP_MASK_IMM8)
8449 {
a9e24354
TS
8450 as_bad (_("DSP immediate not in range 0..%d (%lu)"),
8451 OP_MASK_IMM8, (unsigned long) imm_expr.X_add_number);
74cd071d 8452 }
a9e24354 8453 INSERT_OPERAND (IMM8, *ip, imm_expr.X_add_number);
74cd071d
CF
8454 imm_expr.X_op = O_absent;
8455 s = expr_end;
8456 continue;
8457
8458 case '6': /* dsp 5-bit unsigned immediate in bit 21 */
8459 my_getExpression (&imm_expr, s);
8460 check_absolute_expr (ip, &imm_expr);
8461 if (imm_expr.X_add_number & ~OP_MASK_RS)
8462 {
a9e24354
TS
8463 as_bad (_("DSP immediate not in range 0..%d (%lu)"),
8464 OP_MASK_RS, (unsigned long) imm_expr.X_add_number);
74cd071d 8465 }
a9e24354 8466 INSERT_OPERAND (RS, *ip, imm_expr.X_add_number);
74cd071d
CF
8467 imm_expr.X_op = O_absent;
8468 s = expr_end;
8469 continue;
8470
8471 case '7': /* four dsp accumulators in bits 11,12 */
8472 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
8473 s[3] >= '0' && s[3] <= '3')
8474 {
8475 regno = s[3] - '0';
8476 s += 4;
a9e24354 8477 INSERT_OPERAND (DSPACC, *ip, regno);
74cd071d
CF
8478 continue;
8479 }
8480 else
8481 as_bad (_("Invalid dsp acc register"));
8482 break;
8483
8484 case '8': /* dsp 6-bit unsigned immediate in bit 11 */
8485 my_getExpression (&imm_expr, s);
8486 check_absolute_expr (ip, &imm_expr);
8487 if (imm_expr.X_add_number & ~OP_MASK_WRDSP)
8488 {
a9e24354
TS
8489 as_bad (_("DSP immediate not in range 0..%d (%lu)"),
8490 OP_MASK_WRDSP,
8491 (unsigned long) imm_expr.X_add_number);
74cd071d 8492 }
a9e24354 8493 INSERT_OPERAND (WRDSP, *ip, imm_expr.X_add_number);
74cd071d
CF
8494 imm_expr.X_op = O_absent;
8495 s = expr_end;
8496 continue;
8497
8498 case '9': /* four dsp accumulators in bits 21,22 */
8499 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
8500 s[3] >= '0' && s[3] <= '3')
8501 {
8502 regno = s[3] - '0';
8503 s += 4;
a9e24354 8504 INSERT_OPERAND (DSPACC_S, *ip, regno);
74cd071d
CF
8505 continue;
8506 }
8507 else
8508 as_bad (_("Invalid dsp acc register"));
8509 break;
8510
8511 case '0': /* dsp 6-bit signed immediate in bit 20 */
8512 my_getExpression (&imm_expr, s);
8513 check_absolute_expr (ip, &imm_expr);
8514 min_range = -((OP_MASK_DSPSFT + 1) >> 1);
8515 max_range = ((OP_MASK_DSPSFT + 1) >> 1) - 1;
8516 if (imm_expr.X_add_number < min_range ||
8517 imm_expr.X_add_number > max_range)
8518 {
a9e24354
TS
8519 as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
8520 (long) min_range, (long) max_range,
8521 (long) imm_expr.X_add_number);
74cd071d 8522 }
a9e24354 8523 INSERT_OPERAND (DSPSFT, *ip, imm_expr.X_add_number);
74cd071d
CF
8524 imm_expr.X_op = O_absent;
8525 s = expr_end;
8526 continue;
8527
8528 case '\'': /* dsp 6-bit unsigned immediate in bit 16 */
8529 my_getExpression (&imm_expr, s);
8530 check_absolute_expr (ip, &imm_expr);
8531 if (imm_expr.X_add_number & ~OP_MASK_RDDSP)
8532 {
a9e24354
TS
8533 as_bad (_("DSP immediate not in range 0..%d (%lu)"),
8534 OP_MASK_RDDSP,
8535 (unsigned long) imm_expr.X_add_number);
74cd071d 8536 }
a9e24354 8537 INSERT_OPERAND (RDDSP, *ip, imm_expr.X_add_number);
74cd071d
CF
8538 imm_expr.X_op = O_absent;
8539 s = expr_end;
8540 continue;
8541
8542 case ':': /* dsp 7-bit signed immediate in bit 19 */
8543 my_getExpression (&imm_expr, s);
8544 check_absolute_expr (ip, &imm_expr);
8545 min_range = -((OP_MASK_DSPSFT_7 + 1) >> 1);
8546 max_range = ((OP_MASK_DSPSFT_7 + 1) >> 1) - 1;
8547 if (imm_expr.X_add_number < min_range ||
8548 imm_expr.X_add_number > max_range)
8549 {
a9e24354
TS
8550 as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
8551 (long) min_range, (long) max_range,
8552 (long) imm_expr.X_add_number);
74cd071d 8553 }
a9e24354 8554 INSERT_OPERAND (DSPSFT_7, *ip, imm_expr.X_add_number);
74cd071d
CF
8555 imm_expr.X_op = O_absent;
8556 s = expr_end;
8557 continue;
8558
8559 case '@': /* dsp 10-bit signed immediate in bit 16 */
8560 my_getExpression (&imm_expr, s);
8561 check_absolute_expr (ip, &imm_expr);
8562 min_range = -((OP_MASK_IMM10 + 1) >> 1);
8563 max_range = ((OP_MASK_IMM10 + 1) >> 1) - 1;
8564 if (imm_expr.X_add_number < min_range ||
8565 imm_expr.X_add_number > max_range)
8566 {
a9e24354
TS
8567 as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
8568 (long) min_range, (long) max_range,
8569 (long) imm_expr.X_add_number);
74cd071d 8570 }
a9e24354 8571 INSERT_OPERAND (IMM10, *ip, imm_expr.X_add_number);
74cd071d
CF
8572 imm_expr.X_op = O_absent;
8573 s = expr_end;
8574 continue;
8575
a9e24354 8576 case '!': /* MT usermode flag bit. */
ef2e4d86
CF
8577 my_getExpression (&imm_expr, s);
8578 check_absolute_expr (ip, &imm_expr);
8579 if (imm_expr.X_add_number & ~OP_MASK_MT_U)
a9e24354
TS
8580 as_bad (_("MT usermode bit not 0 or 1 (%lu)"),
8581 (unsigned long) imm_expr.X_add_number);
8582 INSERT_OPERAND (MT_U, *ip, imm_expr.X_add_number);
ef2e4d86
CF
8583 imm_expr.X_op = O_absent;
8584 s = expr_end;
8585 continue;
8586
a9e24354 8587 case '$': /* MT load high flag bit. */
ef2e4d86
CF
8588 my_getExpression (&imm_expr, s);
8589 check_absolute_expr (ip, &imm_expr);
8590 if (imm_expr.X_add_number & ~OP_MASK_MT_H)
a9e24354
TS
8591 as_bad (_("MT load high bit not 0 or 1 (%lu)"),
8592 (unsigned long) imm_expr.X_add_number);
8593 INSERT_OPERAND (MT_H, *ip, imm_expr.X_add_number);
ef2e4d86
CF
8594 imm_expr.X_op = O_absent;
8595 s = expr_end;
8596 continue;
8597
8598 case '*': /* four dsp accumulators in bits 18,19 */
8599 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
8600 s[3] >= '0' && s[3] <= '3')
8601 {
8602 regno = s[3] - '0';
8603 s += 4;
a9e24354 8604 INSERT_OPERAND (MTACC_T, *ip, regno);
ef2e4d86
CF
8605 continue;
8606 }
8607 else
8608 as_bad (_("Invalid dsp/smartmips acc register"));
8609 break;
8610
8611 case '&': /* four dsp accumulators in bits 13,14 */
8612 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
8613 s[3] >= '0' && s[3] <= '3')
8614 {
8615 regno = s[3] - '0';
8616 s += 4;
a9e24354 8617 INSERT_OPERAND (MTACC_D, *ip, regno);
ef2e4d86
CF
8618 continue;
8619 }
8620 else
8621 as_bad (_("Invalid dsp/smartmips acc register"));
8622 break;
8623
252b5132 8624 case ',':
a339155f 8625 ++argnum;
252b5132
RH
8626 if (*s++ == *args)
8627 continue;
8628 s--;
8629 switch (*++args)
8630 {
8631 case 'r':
8632 case 'v':
bf12938e 8633 INSERT_OPERAND (RS, *ip, lastregno);
252b5132
RH
8634 continue;
8635
8636 case 'w':
bf12938e 8637 INSERT_OPERAND (RT, *ip, lastregno);
38487616
TS
8638 continue;
8639
252b5132 8640 case 'W':
bf12938e 8641 INSERT_OPERAND (FT, *ip, lastregno);
252b5132
RH
8642 continue;
8643
8644 case 'V':
bf12938e 8645 INSERT_OPERAND (FS, *ip, lastregno);
252b5132
RH
8646 continue;
8647 }
8648 break;
8649
8650 case '(':
8651 /* Handle optional base register.
8652 Either the base register is omitted or
bdaaa2e1 8653 we must have a left paren. */
252b5132
RH
8654 /* This is dependent on the next operand specifier
8655 is a base register specification. */
8656 assert (args[1] == 'b' || args[1] == '5'
8657 || args[1] == '-' || args[1] == '4');
8658 if (*s == '\0')
8659 return;
8660
8661 case ')': /* these must match exactly */
60b63b72
RS
8662 case '[':
8663 case ']':
252b5132
RH
8664 if (*s++ == *args)
8665 continue;
8666 break;
8667
af7ee8bf
CD
8668 case '+': /* Opcode extension character. */
8669 switch (*++args)
8670 {
9bcd4f99
TS
8671 case '1': /* UDI immediates. */
8672 case '2':
8673 case '3':
8674 case '4':
8675 {
8676 const struct mips_immed *imm = mips_immed;
8677
8678 while (imm->type && imm->type != *args)
8679 ++imm;
8680 if (! imm->type)
8681 internalError ();
8682 my_getExpression (&imm_expr, s);
8683 check_absolute_expr (ip, &imm_expr);
8684 if ((unsigned long) imm_expr.X_add_number & ~imm->mask)
8685 {
8686 as_warn (_("Illegal %s number (%lu, 0x%lx)"),
8687 imm->desc ? imm->desc : ip->insn_mo->name,
8688 (unsigned long) imm_expr.X_add_number,
8689 (unsigned long) imm_expr.X_add_number);
8690 imm_expr.X_add_number &= imm->mask;
8691 }
8692 ip->insn_opcode |= ((unsigned long) imm_expr.X_add_number
8693 << imm->shift);
8694 imm_expr.X_op = O_absent;
8695 s = expr_end;
8696 }
8697 continue;
8698
071742cf
CD
8699 case 'A': /* ins/ext position, becomes LSB. */
8700 limlo = 0;
8701 limhi = 31;
5f74bc13
CD
8702 goto do_lsb;
8703 case 'E':
8704 limlo = 32;
8705 limhi = 63;
8706 goto do_lsb;
8707do_lsb:
071742cf
CD
8708 my_getExpression (&imm_expr, s);
8709 check_absolute_expr (ip, &imm_expr);
8710 if ((unsigned long) imm_expr.X_add_number < limlo
8711 || (unsigned long) imm_expr.X_add_number > limhi)
8712 {
8713 as_bad (_("Improper position (%lu)"),
8714 (unsigned long) imm_expr.X_add_number);
8715 imm_expr.X_add_number = limlo;
8716 }
8717 lastpos = imm_expr.X_add_number;
bf12938e 8718 INSERT_OPERAND (SHAMT, *ip, imm_expr.X_add_number);
071742cf
CD
8719 imm_expr.X_op = O_absent;
8720 s = expr_end;
8721 continue;
8722
8723 case 'B': /* ins size, becomes MSB. */
8724 limlo = 1;
8725 limhi = 32;
5f74bc13
CD
8726 goto do_msb;
8727 case 'F':
8728 limlo = 33;
8729 limhi = 64;
8730 goto do_msb;
8731do_msb:
071742cf
CD
8732 my_getExpression (&imm_expr, s);
8733 check_absolute_expr (ip, &imm_expr);
8734 /* Check for negative input so that small negative numbers
8735 will not succeed incorrectly. The checks against
8736 (pos+size) transitively check "size" itself,
8737 assuming that "pos" is reasonable. */
8738 if ((long) imm_expr.X_add_number < 0
8739 || ((unsigned long) imm_expr.X_add_number
8740 + lastpos) < limlo
8741 || ((unsigned long) imm_expr.X_add_number
8742 + lastpos) > limhi)
8743 {
8744 as_bad (_("Improper insert size (%lu, position %lu)"),
8745 (unsigned long) imm_expr.X_add_number,
8746 (unsigned long) lastpos);
8747 imm_expr.X_add_number = limlo - lastpos;
8748 }
bf12938e
RS
8749 INSERT_OPERAND (INSMSB, *ip,
8750 lastpos + imm_expr.X_add_number - 1);
071742cf
CD
8751 imm_expr.X_op = O_absent;
8752 s = expr_end;
8753 continue;
8754
8755 case 'C': /* ext size, becomes MSBD. */
8756 limlo = 1;
8757 limhi = 32;
5f74bc13
CD
8758 goto do_msbd;
8759 case 'G':
8760 limlo = 33;
8761 limhi = 64;
8762 goto do_msbd;
8763 case 'H':
8764 limlo = 33;
8765 limhi = 64;
8766 goto do_msbd;
8767do_msbd:
071742cf
CD
8768 my_getExpression (&imm_expr, s);
8769 check_absolute_expr (ip, &imm_expr);
8770 /* Check for negative input so that small negative numbers
8771 will not succeed incorrectly. The checks against
8772 (pos+size) transitively check "size" itself,
8773 assuming that "pos" is reasonable. */
8774 if ((long) imm_expr.X_add_number < 0
8775 || ((unsigned long) imm_expr.X_add_number
8776 + lastpos) < limlo
8777 || ((unsigned long) imm_expr.X_add_number
8778 + lastpos) > limhi)
8779 {
8780 as_bad (_("Improper extract size (%lu, position %lu)"),
8781 (unsigned long) imm_expr.X_add_number,
8782 (unsigned long) lastpos);
8783 imm_expr.X_add_number = limlo - lastpos;
8784 }
bf12938e 8785 INSERT_OPERAND (EXTMSBD, *ip, imm_expr.X_add_number - 1);
071742cf
CD
8786 imm_expr.X_op = O_absent;
8787 s = expr_end;
8788 continue;
af7ee8bf 8789
bbcc0807
CD
8790 case 'D':
8791 /* +D is for disassembly only; never match. */
8792 break;
8793
5f74bc13
CD
8794 case 'I':
8795 /* "+I" is like "I", except that imm2_expr is used. */
8796 my_getExpression (&imm2_expr, s);
8797 if (imm2_expr.X_op != O_big
8798 && imm2_expr.X_op != O_constant)
8799 insn_error = _("absolute expression required");
9ee2a2d4
MR
8800 if (HAVE_32BIT_GPRS)
8801 normalize_constant_expr (&imm2_expr);
5f74bc13
CD
8802 s = expr_end;
8803 continue;
8804
707bfff6 8805 case 'T': /* Coprocessor register. */
ef2e4d86
CF
8806 /* +T is for disassembly only; never match. */
8807 break;
8808
707bfff6 8809 case 't': /* Coprocessor register number. */
ef2e4d86
CF
8810 if (s[0] == '$' && ISDIGIT (s[1]))
8811 {
8812 ++s;
8813 regno = 0;
8814 do
8815 {
8816 regno *= 10;
8817 regno += *s - '0';
8818 ++s;
8819 }
8820 while (ISDIGIT (*s));
8821 if (regno > 31)
8822 as_bad (_("Invalid register number (%d)"), regno);
8823 else
8824 {
a9e24354 8825 INSERT_OPERAND (RT, *ip, regno);
ef2e4d86
CF
8826 continue;
8827 }
8828 }
8829 else
8830 as_bad (_("Invalid coprocessor 0 register number"));
8831 break;
8832
af7ee8bf
CD
8833 default:
8834 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8835 *args, insn->name, insn->args);
8836 /* Further processing is fruitless. */
8837 return;
8838 }
8839 break;
8840
252b5132
RH
8841 case '<': /* must be at least one digit */
8842 /*
8843 * According to the manual, if the shift amount is greater
b6ff326e
KH
8844 * than 31 or less than 0, then the shift amount should be
8845 * mod 32. In reality the mips assembler issues an error.
252b5132
RH
8846 * We issue a warning and mask out all but the low 5 bits.
8847 */
8848 my_getExpression (&imm_expr, s);
8849 check_absolute_expr (ip, &imm_expr);
8850 if ((unsigned long) imm_expr.X_add_number > 31)
bf12938e
RS
8851 as_warn (_("Improper shift amount (%lu)"),
8852 (unsigned long) imm_expr.X_add_number);
8853 INSERT_OPERAND (SHAMT, *ip, imm_expr.X_add_number);
252b5132
RH
8854 imm_expr.X_op = O_absent;
8855 s = expr_end;
8856 continue;
8857
8858 case '>': /* shift amount minus 32 */
8859 my_getExpression (&imm_expr, s);
8860 check_absolute_expr (ip, &imm_expr);
8861 if ((unsigned long) imm_expr.X_add_number < 32
8862 || (unsigned long) imm_expr.X_add_number > 63)
8863 break;
bf12938e 8864 INSERT_OPERAND (SHAMT, *ip, imm_expr.X_add_number - 32);
252b5132
RH
8865 imm_expr.X_op = O_absent;
8866 s = expr_end;
8867 continue;
8868
252b5132
RH
8869 case 'k': /* cache code */
8870 case 'h': /* prefx code */
8871 my_getExpression (&imm_expr, s);
8872 check_absolute_expr (ip, &imm_expr);
8873 if ((unsigned long) imm_expr.X_add_number > 31)
bf12938e
RS
8874 as_warn (_("Invalid value for `%s' (%lu)"),
8875 ip->insn_mo->name,
8876 (unsigned long) imm_expr.X_add_number);
252b5132 8877 if (*args == 'k')
bf12938e 8878 INSERT_OPERAND (CACHE, *ip, imm_expr.X_add_number);
252b5132 8879 else
bf12938e 8880 INSERT_OPERAND (PREFX, *ip, imm_expr.X_add_number);
252b5132
RH
8881 imm_expr.X_op = O_absent;
8882 s = expr_end;
8883 continue;
8884
8885 case 'c': /* break code */
8886 my_getExpression (&imm_expr, s);
8887 check_absolute_expr (ip, &imm_expr);
a9e24354
TS
8888 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE)
8889 as_warn (_("Code for %s not in range 0..1023 (%lu)"),
8890 ip->insn_mo->name,
bf12938e
RS
8891 (unsigned long) imm_expr.X_add_number);
8892 INSERT_OPERAND (CODE, *ip, imm_expr.X_add_number);
252b5132
RH
8893 imm_expr.X_op = O_absent;
8894 s = expr_end;
8895 continue;
8896
8897 case 'q': /* lower break code */
8898 my_getExpression (&imm_expr, s);
8899 check_absolute_expr (ip, &imm_expr);
a9e24354
TS
8900 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE2)
8901 as_warn (_("Lower code for %s not in range 0..1023 (%lu)"),
8902 ip->insn_mo->name,
bf12938e
RS
8903 (unsigned long) imm_expr.X_add_number);
8904 INSERT_OPERAND (CODE2, *ip, imm_expr.X_add_number);
252b5132
RH
8905 imm_expr.X_op = O_absent;
8906 s = expr_end;
8907 continue;
8908
4372b673 8909 case 'B': /* 20-bit syscall/break code. */
156c2f8b 8910 my_getExpression (&imm_expr, s);
156c2f8b 8911 check_absolute_expr (ip, &imm_expr);
793b27f4 8912 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
a9e24354
TS
8913 as_warn (_("Code for %s not in range 0..1048575 (%lu)"),
8914 ip->insn_mo->name,
793b27f4 8915 (unsigned long) imm_expr.X_add_number);
bf12938e 8916 INSERT_OPERAND (CODE20, *ip, imm_expr.X_add_number);
252b5132
RH
8917 imm_expr.X_op = O_absent;
8918 s = expr_end;
8919 continue;
8920
98d3f06f 8921 case 'C': /* Coprocessor code */
beae10d5 8922 my_getExpression (&imm_expr, s);
252b5132 8923 check_absolute_expr (ip, &imm_expr);
a9e24354 8924 if ((unsigned long) imm_expr.X_add_number > OP_MASK_COPZ)
252b5132 8925 {
793b27f4
TS
8926 as_warn (_("Coproccesor code > 25 bits (%lu)"),
8927 (unsigned long) imm_expr.X_add_number);
a9e24354 8928 imm_expr.X_add_number &= OP_MASK_COPZ;
252b5132 8929 }
a9e24354 8930 INSERT_OPERAND (COPZ, *ip, imm_expr.X_add_number);
beae10d5
KH
8931 imm_expr.X_op = O_absent;
8932 s = expr_end;
8933 continue;
252b5132 8934
4372b673
NC
8935 case 'J': /* 19-bit wait code. */
8936 my_getExpression (&imm_expr, s);
8937 check_absolute_expr (ip, &imm_expr);
793b27f4 8938 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
a9e24354
TS
8939 {
8940 as_warn (_("Illegal 19-bit code (%lu)"),
8941 (unsigned long) imm_expr.X_add_number);
8942 imm_expr.X_add_number &= OP_MASK_CODE19;
8943 }
bf12938e 8944 INSERT_OPERAND (CODE19, *ip, imm_expr.X_add_number);
4372b673
NC
8945 imm_expr.X_op = O_absent;
8946 s = expr_end;
8947 continue;
8948
707bfff6 8949 case 'P': /* Performance register. */
beae10d5 8950 my_getExpression (&imm_expr, s);
252b5132 8951 check_absolute_expr (ip, &imm_expr);
beae10d5 8952 if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
bf12938e
RS
8953 as_warn (_("Invalid performance register (%lu)"),
8954 (unsigned long) imm_expr.X_add_number);
8955 INSERT_OPERAND (PERFREG, *ip, imm_expr.X_add_number);
beae10d5
KH
8956 imm_expr.X_op = O_absent;
8957 s = expr_end;
8958 continue;
252b5132 8959
707bfff6
TS
8960 case 'G': /* Coprocessor destination register. */
8961 if (((ip->insn_opcode >> OP_SH_OP) & OP_MASK_OP) == OP_OP_COP0)
8962 ok = reg_lookup (&s, RTYPE_NUM | RTYPE_CP0, &regno);
8963 else
8964 ok = reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno);
a9e24354 8965 INSERT_OPERAND (RD, *ip, regno);
707bfff6
TS
8966 if (ok)
8967 {
8968 lastregno = regno;
8969 continue;
8970 }
8971 else
8972 break;
8973
252b5132
RH
8974 case 'b': /* base register */
8975 case 'd': /* destination register */
8976 case 's': /* source register */
8977 case 't': /* target register */
8978 case 'r': /* both target and source */
8979 case 'v': /* both dest and source */
8980 case 'w': /* both dest and target */
8981 case 'E': /* coprocessor target register */
af7ee8bf 8982 case 'K': /* 'rdhwr' destination register */
252b5132
RH
8983 case 'x': /* ignore register name */
8984 case 'z': /* must be zero register */
4372b673 8985 case 'U': /* destination register (clo/clz). */
ef2e4d86 8986 case 'g': /* coprocessor destination register */
707bfff6
TS
8987 s_reset = s;
8988 if (*args == 'E' || *args == 'K')
8989 ok = reg_lookup (&s, RTYPE_NUM, &regno);
8990 else
8991 {
8992 ok = reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno);
8993 if (regno == AT && ! mips_opts.noat)
8994 as_warn ("Used $at without \".set noat\"");
8995 }
8996 if (ok)
252b5132 8997 {
252b5132
RH
8998 c = *args;
8999 if (*s == ' ')
f9419b05 9000 ++s;
252b5132
RH
9001 if (args[1] != *s)
9002 {
9003 if (c == 'r' || c == 'v' || c == 'w')
9004 {
9005 regno = lastregno;
9006 s = s_reset;
f9419b05 9007 ++args;
252b5132
RH
9008 }
9009 }
9010 /* 'z' only matches $0. */
9011 if (c == 'z' && regno != 0)
9012 break;
9013
bdaaa2e1
KH
9014 /* Now that we have assembled one operand, we use the args string
9015 * to figure out where it goes in the instruction. */
252b5132
RH
9016 switch (c)
9017 {
9018 case 'r':
9019 case 's':
9020 case 'v':
9021 case 'b':
bf12938e 9022 INSERT_OPERAND (RS, *ip, regno);
252b5132
RH
9023 break;
9024 case 'd':
9025 case 'G':
af7ee8bf 9026 case 'K':
ef2e4d86 9027 case 'g':
bf12938e 9028 INSERT_OPERAND (RD, *ip, regno);
252b5132 9029 break;
4372b673 9030 case 'U':
bf12938e
RS
9031 INSERT_OPERAND (RD, *ip, regno);
9032 INSERT_OPERAND (RT, *ip, regno);
4372b673 9033 break;
252b5132
RH
9034 case 'w':
9035 case 't':
9036 case 'E':
bf12938e 9037 INSERT_OPERAND (RT, *ip, regno);
252b5132
RH
9038 break;
9039 case 'x':
9040 /* This case exists because on the r3000 trunc
9041 expands into a macro which requires a gp
9042 register. On the r6000 or r4000 it is
9043 assembled into a single instruction which
9044 ignores the register. Thus the insn version
9045 is MIPS_ISA2 and uses 'x', and the macro
9046 version is MIPS_ISA1 and uses 't'. */
9047 break;
9048 case 'z':
9049 /* This case is for the div instruction, which
9050 acts differently if the destination argument
9051 is $0. This only matches $0, and is checked
9052 outside the switch. */
9053 break;
9054 case 'D':
9055 /* Itbl operand; not yet implemented. FIXME ?? */
9056 break;
9057 /* What about all other operands like 'i', which
9058 can be specified in the opcode table? */
9059 }
9060 lastregno = regno;
9061 continue;
9062 }
252b5132
RH
9063 switch (*args++)
9064 {
9065 case 'r':
9066 case 'v':
bf12938e 9067 INSERT_OPERAND (RS, *ip, lastregno);
252b5132
RH
9068 continue;
9069 case 'w':
bf12938e 9070 INSERT_OPERAND (RT, *ip, lastregno);
252b5132
RH
9071 continue;
9072 }
9073 break;
9074
deec1734
CD
9075 case 'O': /* MDMX alignment immediate constant. */
9076 my_getExpression (&imm_expr, s);
9077 check_absolute_expr (ip, &imm_expr);
9078 if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
bf12938e
RS
9079 as_warn ("Improper align amount (%ld), using low bits",
9080 (long) imm_expr.X_add_number);
9081 INSERT_OPERAND (ALN, *ip, imm_expr.X_add_number);
deec1734
CD
9082 imm_expr.X_op = O_absent;
9083 s = expr_end;
9084 continue;
9085
9086 case 'Q': /* MDMX vector, element sel, or const. */
9087 if (s[0] != '$')
9088 {
9089 /* MDMX Immediate. */
9090 my_getExpression (&imm_expr, s);
9091 check_absolute_expr (ip, &imm_expr);
9092 if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
bf12938e
RS
9093 as_warn (_("Invalid MDMX Immediate (%ld)"),
9094 (long) imm_expr.X_add_number);
9095 INSERT_OPERAND (FT, *ip, imm_expr.X_add_number);
deec1734
CD
9096 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
9097 ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
9098 else
9099 ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
deec1734
CD
9100 imm_expr.X_op = O_absent;
9101 s = expr_end;
9102 continue;
9103 }
9104 /* Not MDMX Immediate. Fall through. */
9105 case 'X': /* MDMX destination register. */
9106 case 'Y': /* MDMX source register. */
9107 case 'Z': /* MDMX target register. */
9108 is_mdmx = 1;
252b5132
RH
9109 case 'D': /* floating point destination register */
9110 case 'S': /* floating point source register */
9111 case 'T': /* floating point target register */
9112 case 'R': /* floating point source register */
9113 case 'V':
9114 case 'W':
707bfff6
TS
9115 rtype = RTYPE_FPU;
9116 if (is_mdmx
9117 || (mips_opts.ase_mdmx
9118 && (ip->insn_mo->pinfo & FP_D)
9119 && (ip->insn_mo->pinfo & (INSN_COPROC_MOVE_DELAY
9120 | INSN_COPROC_MEMORY_DELAY
9121 | INSN_LOAD_COPROC_DELAY
9122 | INSN_LOAD_MEMORY_DELAY
9123 | INSN_STORE_MEMORY))))
9124 rtype |= RTYPE_VEC;
252b5132 9125 s_reset = s;
707bfff6 9126 if (reg_lookup (&s, rtype, &regno))
252b5132 9127 {
252b5132 9128 if ((regno & 1) != 0
ca4e0257 9129 && HAVE_32BIT_FPRS
7455baf8 9130 && ! mips_oddfpreg_ok (ip->insn_mo, argnum))
252b5132
RH
9131 as_warn (_("Float register should be even, was %d"),
9132 regno);
9133
9134 c = *args;
9135 if (*s == ' ')
f9419b05 9136 ++s;
252b5132
RH
9137 if (args[1] != *s)
9138 {
9139 if (c == 'V' || c == 'W')
9140 {
9141 regno = lastregno;
9142 s = s_reset;
f9419b05 9143 ++args;
252b5132
RH
9144 }
9145 }
9146 switch (c)
9147 {
9148 case 'D':
deec1734 9149 case 'X':
bf12938e 9150 INSERT_OPERAND (FD, *ip, regno);
252b5132
RH
9151 break;
9152 case 'V':
9153 case 'S':
deec1734 9154 case 'Y':
bf12938e 9155 INSERT_OPERAND (FS, *ip, regno);
252b5132 9156 break;
deec1734
CD
9157 case 'Q':
9158 /* This is like 'Z', but also needs to fix the MDMX
9159 vector/scalar select bits. Note that the
9160 scalar immediate case is handled above. */
9161 if (*s == '[')
9162 {
9163 int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
9164 int max_el = (is_qh ? 3 : 7);
9165 s++;
9166 my_getExpression(&imm_expr, s);
9167 check_absolute_expr (ip, &imm_expr);
9168 s = expr_end;
9169 if (imm_expr.X_add_number > max_el)
9170 as_bad(_("Bad element selector %ld"),
9171 (long) imm_expr.X_add_number);
9172 imm_expr.X_add_number &= max_el;
9173 ip->insn_opcode |= (imm_expr.X_add_number
9174 << (OP_SH_VSEL +
9175 (is_qh ? 2 : 1)));
01a3f561 9176 imm_expr.X_op = O_absent;
deec1734
CD
9177 if (*s != ']')
9178 as_warn(_("Expecting ']' found '%s'"), s);
9179 else
9180 s++;
9181 }
9182 else
9183 {
9184 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
9185 ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
9186 << OP_SH_VSEL);
9187 else
9188 ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
9189 OP_SH_VSEL);
9190 }
9191 /* Fall through */
252b5132
RH
9192 case 'W':
9193 case 'T':
deec1734 9194 case 'Z':
bf12938e 9195 INSERT_OPERAND (FT, *ip, regno);
252b5132
RH
9196 break;
9197 case 'R':
bf12938e 9198 INSERT_OPERAND (FR, *ip, regno);
252b5132
RH
9199 break;
9200 }
9201 lastregno = regno;
9202 continue;
9203 }
9204
252b5132
RH
9205 switch (*args++)
9206 {
9207 case 'V':
bf12938e 9208 INSERT_OPERAND (FS, *ip, lastregno);
252b5132
RH
9209 continue;
9210 case 'W':
bf12938e 9211 INSERT_OPERAND (FT, *ip, lastregno);
252b5132
RH
9212 continue;
9213 }
9214 break;
9215
9216 case 'I':
9217 my_getExpression (&imm_expr, s);
9218 if (imm_expr.X_op != O_big
9219 && imm_expr.X_op != O_constant)
9220 insn_error = _("absolute expression required");
9ee2a2d4
MR
9221 if (HAVE_32BIT_GPRS)
9222 normalize_constant_expr (&imm_expr);
252b5132
RH
9223 s = expr_end;
9224 continue;
9225
9226 case 'A':
9227 my_getExpression (&offset_expr, s);
2051e8c4 9228 normalize_address_expr (&offset_expr);
f6688943 9229 *imm_reloc = BFD_RELOC_32;
252b5132
RH
9230 s = expr_end;
9231 continue;
9232
9233 case 'F':
9234 case 'L':
9235 case 'f':
9236 case 'l':
9237 {
9238 int f64;
ca4e0257 9239 int using_gprs;
252b5132
RH
9240 char *save_in;
9241 char *err;
9242 unsigned char temp[8];
9243 int len;
9244 unsigned int length;
9245 segT seg;
9246 subsegT subseg;
9247 char *p;
9248
9249 /* These only appear as the last operand in an
9250 instruction, and every instruction that accepts
9251 them in any variant accepts them in all variants.
9252 This means we don't have to worry about backing out
9253 any changes if the instruction does not match.
9254
9255 The difference between them is the size of the
9256 floating point constant and where it goes. For 'F'
9257 and 'L' the constant is 64 bits; for 'f' and 'l' it
9258 is 32 bits. Where the constant is placed is based
9259 on how the MIPS assembler does things:
9260 F -- .rdata
9261 L -- .lit8
9262 f -- immediate value
9263 l -- .lit4
9264
9265 The .lit4 and .lit8 sections are only used if
9266 permitted by the -G argument.
9267
ca4e0257
RS
9268 The code below needs to know whether the target register
9269 is 32 or 64 bits wide. It relies on the fact 'f' and
9270 'F' are used with GPR-based instructions and 'l' and
9271 'L' are used with FPR-based instructions. */
252b5132
RH
9272
9273 f64 = *args == 'F' || *args == 'L';
ca4e0257 9274 using_gprs = *args == 'F' || *args == 'f';
252b5132
RH
9275
9276 save_in = input_line_pointer;
9277 input_line_pointer = s;
9278 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
9279 length = len;
9280 s = input_line_pointer;
9281 input_line_pointer = save_in;
9282 if (err != NULL && *err != '\0')
9283 {
9284 as_bad (_("Bad floating point constant: %s"), err);
9285 memset (temp, '\0', sizeof temp);
9286 length = f64 ? 8 : 4;
9287 }
9288
156c2f8b 9289 assert (length == (unsigned) (f64 ? 8 : 4));
252b5132
RH
9290
9291 if (*args == 'f'
9292 || (*args == 'l'
3e722fb5 9293 && (g_switch_value < 4
252b5132
RH
9294 || (temp[0] == 0 && temp[1] == 0)
9295 || (temp[2] == 0 && temp[3] == 0))))
9296 {
9297 imm_expr.X_op = O_constant;
9298 if (! target_big_endian)
9299 imm_expr.X_add_number = bfd_getl32 (temp);
9300 else
9301 imm_expr.X_add_number = bfd_getb32 (temp);
9302 }
9303 else if (length > 4
119d663a 9304 && ! mips_disable_float_construction
ca4e0257
RS
9305 /* Constants can only be constructed in GPRs and
9306 copied to FPRs if the GPRs are at least as wide
9307 as the FPRs. Force the constant into memory if
9308 we are using 64-bit FPRs but the GPRs are only
9309 32 bits wide. */
9310 && (using_gprs
9311 || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
252b5132
RH
9312 && ((temp[0] == 0 && temp[1] == 0)
9313 || (temp[2] == 0 && temp[3] == 0))
9314 && ((temp[4] == 0 && temp[5] == 0)
9315 || (temp[6] == 0 && temp[7] == 0)))
9316 {
ca4e0257
RS
9317 /* The value is simple enough to load with a couple of
9318 instructions. If using 32-bit registers, set
9319 imm_expr to the high order 32 bits and offset_expr to
9320 the low order 32 bits. Otherwise, set imm_expr to
9321 the entire 64 bit constant. */
9322 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
252b5132
RH
9323 {
9324 imm_expr.X_op = O_constant;
9325 offset_expr.X_op = O_constant;
9326 if (! target_big_endian)
9327 {
9328 imm_expr.X_add_number = bfd_getl32 (temp + 4);
9329 offset_expr.X_add_number = bfd_getl32 (temp);
9330 }
9331 else
9332 {
9333 imm_expr.X_add_number = bfd_getb32 (temp);
9334 offset_expr.X_add_number = bfd_getb32 (temp + 4);
9335 }
9336 if (offset_expr.X_add_number == 0)
9337 offset_expr.X_op = O_absent;
9338 }
9339 else if (sizeof (imm_expr.X_add_number) > 4)
9340 {
9341 imm_expr.X_op = O_constant;
9342 if (! target_big_endian)
9343 imm_expr.X_add_number = bfd_getl64 (temp);
9344 else
9345 imm_expr.X_add_number = bfd_getb64 (temp);
9346 }
9347 else
9348 {
9349 imm_expr.X_op = O_big;
9350 imm_expr.X_add_number = 4;
9351 if (! target_big_endian)
9352 {
9353 generic_bignum[0] = bfd_getl16 (temp);
9354 generic_bignum[1] = bfd_getl16 (temp + 2);
9355 generic_bignum[2] = bfd_getl16 (temp + 4);
9356 generic_bignum[3] = bfd_getl16 (temp + 6);
9357 }
9358 else
9359 {
9360 generic_bignum[0] = bfd_getb16 (temp + 6);
9361 generic_bignum[1] = bfd_getb16 (temp + 4);
9362 generic_bignum[2] = bfd_getb16 (temp + 2);
9363 generic_bignum[3] = bfd_getb16 (temp);
9364 }
9365 }
9366 }
9367 else
9368 {
9369 const char *newname;
9370 segT new_seg;
9371
9372 /* Switch to the right section. */
9373 seg = now_seg;
9374 subseg = now_subseg;
9375 switch (*args)
9376 {
9377 default: /* unused default case avoids warnings. */
9378 case 'L':
9379 newname = RDATA_SECTION_NAME;
3e722fb5 9380 if (g_switch_value >= 8)
252b5132
RH
9381 newname = ".lit8";
9382 break;
9383 case 'F':
3e722fb5 9384 newname = RDATA_SECTION_NAME;
252b5132
RH
9385 break;
9386 case 'l':
4d0d148d 9387 assert (g_switch_value >= 4);
252b5132
RH
9388 newname = ".lit4";
9389 break;
9390 }
9391 new_seg = subseg_new (newname, (subsegT) 0);
9392 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
9393 bfd_set_section_flags (stdoutput, new_seg,
9394 (SEC_ALLOC
9395 | SEC_LOAD
9396 | SEC_READONLY
9397 | SEC_DATA));
9398 frag_align (*args == 'l' ? 2 : 3, 0, 0);
9399 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
9400 && strcmp (TARGET_OS, "elf") != 0)
9401 record_alignment (new_seg, 4);
9402 else
9403 record_alignment (new_seg, *args == 'l' ? 2 : 3);
9404 if (seg == now_seg)
9405 as_bad (_("Can't use floating point insn in this section"));
9406
9407 /* Set the argument to the current address in the
9408 section. */
9409 offset_expr.X_op = O_symbol;
9410 offset_expr.X_add_symbol =
9411 symbol_new ("L0\001", now_seg,
9412 (valueT) frag_now_fix (), frag_now);
9413 offset_expr.X_add_number = 0;
9414
9415 /* Put the floating point number into the section. */
9416 p = frag_more ((int) length);
9417 memcpy (p, temp, length);
9418
9419 /* Switch back to the original section. */
9420 subseg_set (seg, subseg);
9421 }
9422 }
9423 continue;
9424
9425 case 'i': /* 16 bit unsigned immediate */
9426 case 'j': /* 16 bit signed immediate */
f6688943 9427 *imm_reloc = BFD_RELOC_LO16;
5e0116d5 9428 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0)
252b5132
RH
9429 {
9430 int more;
5e0116d5
RS
9431 offsetT minval, maxval;
9432
9433 more = (insn + 1 < &mips_opcodes[NUMOPCODES]
9434 && strcmp (insn->name, insn[1].name) == 0);
9435
9436 /* If the expression was written as an unsigned number,
9437 only treat it as signed if there are no more
9438 alternatives. */
9439 if (more
9440 && *args == 'j'
9441 && sizeof (imm_expr.X_add_number) <= 4
9442 && imm_expr.X_op == O_constant
9443 && imm_expr.X_add_number < 0
9444 && imm_expr.X_unsigned
9445 && HAVE_64BIT_GPRS)
9446 break;
9447
9448 /* For compatibility with older assemblers, we accept
9449 0x8000-0xffff as signed 16-bit numbers when only
9450 signed numbers are allowed. */
9451 if (*args == 'i')
9452 minval = 0, maxval = 0xffff;
9453 else if (more)
9454 minval = -0x8000, maxval = 0x7fff;
252b5132 9455 else
5e0116d5
RS
9456 minval = -0x8000, maxval = 0xffff;
9457
9458 if (imm_expr.X_op != O_constant
9459 || imm_expr.X_add_number < minval
9460 || imm_expr.X_add_number > maxval)
252b5132
RH
9461 {
9462 if (more)
9463 break;
2ae7e77b
AH
9464 if (imm_expr.X_op == O_constant
9465 || imm_expr.X_op == O_big)
5e0116d5 9466 as_bad (_("expression out of range"));
252b5132
RH
9467 }
9468 }
9469 s = expr_end;
9470 continue;
9471
9472 case 'o': /* 16 bit offset */
5e0116d5
RS
9473 /* Check whether there is only a single bracketed expression
9474 left. If so, it must be the base register and the
9475 constant must be zero. */
9476 if (*s == '(' && strchr (s + 1, '(') == 0)
9477 {
9478 offset_expr.X_op = O_constant;
9479 offset_expr.X_add_number = 0;
9480 continue;
9481 }
252b5132
RH
9482
9483 /* If this value won't fit into a 16 bit offset, then go
9484 find a macro that will generate the 32 bit offset
afdbd6d0 9485 code pattern. */
5e0116d5 9486 if (my_getSmallExpression (&offset_expr, offset_reloc, s) == 0
252b5132
RH
9487 && (offset_expr.X_op != O_constant
9488 || offset_expr.X_add_number >= 0x8000
afdbd6d0 9489 || offset_expr.X_add_number < -0x8000))
252b5132
RH
9490 break;
9491
252b5132
RH
9492 s = expr_end;
9493 continue;
9494
9495 case 'p': /* pc relative offset */
0b25d3e6 9496 *offset_reloc = BFD_RELOC_16_PCREL_S2;
252b5132
RH
9497 my_getExpression (&offset_expr, s);
9498 s = expr_end;
9499 continue;
9500
9501 case 'u': /* upper 16 bits */
5e0116d5
RS
9502 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0
9503 && imm_expr.X_op == O_constant
9504 && (imm_expr.X_add_number < 0
9505 || imm_expr.X_add_number >= 0x10000))
252b5132
RH
9506 as_bad (_("lui expression not in range 0..65535"));
9507 s = expr_end;
9508 continue;
9509
9510 case 'a': /* 26 bit address */
9511 my_getExpression (&offset_expr, s);
9512 s = expr_end;
f6688943 9513 *offset_reloc = BFD_RELOC_MIPS_JMP;
252b5132
RH
9514 continue;
9515
9516 case 'N': /* 3 bit branch condition code */
9517 case 'M': /* 3 bit compare condition code */
707bfff6
TS
9518 rtype = RTYPE_CCC;
9519 if (ip->insn_mo->pinfo & (FP_D| FP_S))
9520 rtype |= RTYPE_FCC;
9521 if (!reg_lookup (&s, rtype, &regno))
252b5132 9522 break;
30c378fd
CD
9523 if ((strcmp(str + strlen(str) - 3, ".ps") == 0
9524 || strcmp(str + strlen(str) - 5, "any2f") == 0
9525 || strcmp(str + strlen(str) - 5, "any2t") == 0)
9526 && (regno & 1) != 0)
9527 as_warn(_("Condition code register should be even for %s, was %d"),
9528 str, regno);
9529 if ((strcmp(str + strlen(str) - 5, "any4f") == 0
9530 || strcmp(str + strlen(str) - 5, "any4t") == 0)
9531 && (regno & 3) != 0)
9532 as_warn(_("Condition code register should be 0 or 4 for %s, was %d"),
9533 str, regno);
252b5132 9534 if (*args == 'N')
bf12938e 9535 INSERT_OPERAND (BCC, *ip, regno);
252b5132 9536 else
bf12938e 9537 INSERT_OPERAND (CCC, *ip, regno);
beae10d5 9538 continue;
252b5132 9539
156c2f8b
NC
9540 case 'H':
9541 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
9542 s += 2;
3882b010 9543 if (ISDIGIT (*s))
156c2f8b
NC
9544 {
9545 c = 0;
9546 do
9547 {
9548 c *= 10;
9549 c += *s - '0';
9550 ++s;
9551 }
3882b010 9552 while (ISDIGIT (*s));
156c2f8b
NC
9553 }
9554 else
9555 c = 8; /* Invalid sel value. */
9556
9557 if (c > 7)
9558 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
9559 ip->insn_opcode |= c;
9560 continue;
9561
60b63b72
RS
9562 case 'e':
9563 /* Must be at least one digit. */
9564 my_getExpression (&imm_expr, s);
9565 check_absolute_expr (ip, &imm_expr);
9566
9567 if ((unsigned long) imm_expr.X_add_number
9568 > (unsigned long) OP_MASK_VECBYTE)
9569 {
9570 as_bad (_("bad byte vector index (%ld)"),
9571 (long) imm_expr.X_add_number);
9572 imm_expr.X_add_number = 0;
9573 }
9574
bf12938e 9575 INSERT_OPERAND (VECBYTE, *ip, imm_expr.X_add_number);
60b63b72
RS
9576 imm_expr.X_op = O_absent;
9577 s = expr_end;
9578 continue;
9579
9580 case '%':
9581 my_getExpression (&imm_expr, s);
9582 check_absolute_expr (ip, &imm_expr);
9583
9584 if ((unsigned long) imm_expr.X_add_number
9585 > (unsigned long) OP_MASK_VECALIGN)
9586 {
9587 as_bad (_("bad byte vector index (%ld)"),
9588 (long) imm_expr.X_add_number);
9589 imm_expr.X_add_number = 0;
9590 }
9591
bf12938e 9592 INSERT_OPERAND (VECALIGN, *ip, imm_expr.X_add_number);
60b63b72
RS
9593 imm_expr.X_op = O_absent;
9594 s = expr_end;
9595 continue;
9596
252b5132
RH
9597 default:
9598 as_bad (_("bad char = '%c'\n"), *args);
9599 internalError ();
9600 }
9601 break;
9602 }
9603 /* Args don't match. */
9604 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
9605 !strcmp (insn->name, insn[1].name))
9606 {
9607 ++insn;
9608 s = argsStart;
268f6bed 9609 insn_error = _("illegal operands");
252b5132
RH
9610 continue;
9611 }
268f6bed
L
9612 if (save_c)
9613 *(--s) = save_c;
252b5132
RH
9614 insn_error = _("illegal operands");
9615 return;
9616 }
9617}
9618
0499d65b
TS
9619#define SKIP_SPACE_TABS(S) { while (*(S) == ' ' || *(S) == '\t') ++(S); }
9620
252b5132
RH
9621/* This routine assembles an instruction into its binary format when
9622 assembling for the mips16. As a side effect, it sets one of the
9623 global variables imm_reloc or offset_reloc to the type of
9624 relocation to do if one of the operands is an address expression.
9625 It also sets mips16_small and mips16_ext if the user explicitly
9626 requested a small or extended instruction. */
9627
9628static void
17a2f251 9629mips16_ip (char *str, struct mips_cl_insn *ip)
252b5132
RH
9630{
9631 char *s;
9632 const char *args;
9633 struct mips_opcode *insn;
9634 char *argsstart;
9635 unsigned int regno;
9636 unsigned int lastregno = 0;
9637 char *s_reset;
d6f16593 9638 size_t i;
252b5132
RH
9639
9640 insn_error = NULL;
9641
b34976b6
AM
9642 mips16_small = FALSE;
9643 mips16_ext = FALSE;
252b5132 9644
3882b010 9645 for (s = str; ISLOWER (*s); ++s)
252b5132
RH
9646 ;
9647 switch (*s)
9648 {
9649 case '\0':
9650 break;
9651
9652 case ' ':
9653 *s++ = '\0';
9654 break;
9655
9656 case '.':
9657 if (s[1] == 't' && s[2] == ' ')
9658 {
9659 *s = '\0';
b34976b6 9660 mips16_small = TRUE;
252b5132
RH
9661 s += 3;
9662 break;
9663 }
9664 else if (s[1] == 'e' && s[2] == ' ')
9665 {
9666 *s = '\0';
b34976b6 9667 mips16_ext = TRUE;
252b5132
RH
9668 s += 3;
9669 break;
9670 }
9671 /* Fall through. */
9672 default:
9673 insn_error = _("unknown opcode");
9674 return;
9675 }
9676
9677 if (mips_opts.noautoextend && ! mips16_ext)
b34976b6 9678 mips16_small = TRUE;
252b5132
RH
9679
9680 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
9681 {
9682 insn_error = _("unrecognized opcode");
9683 return;
9684 }
9685
9686 argsstart = s;
9687 for (;;)
9688 {
9b3f89ee
TS
9689 bfd_boolean ok;
9690
252b5132
RH
9691 assert (strcmp (insn->name, str) == 0);
9692
9b3f89ee
TS
9693 if (OPCODE_IS_MEMBER (insn, mips_opts.isa, mips_opts.arch))
9694 ok = TRUE;
9695 else
9696 ok = FALSE;
9697
9698 if (! ok)
9699 {
9700 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes]
9701 && strcmp (insn->name, insn[1].name) == 0)
9702 {
9703 ++insn;
9704 continue;
9705 }
9706 else
9707 {
9708 if (!insn_error)
9709 {
9710 static char buf[100];
9711 sprintf (buf,
9712 _("opcode not supported on this processor: %s (%s)"),
9713 mips_cpu_info_from_arch (mips_opts.arch)->name,
9714 mips_cpu_info_from_isa (mips_opts.isa)->name);
9715 insn_error = buf;
9716 }
9717 return;
9718 }
9719 }
9720
1e915849 9721 create_insn (ip, insn);
252b5132 9722 imm_expr.X_op = O_absent;
f6688943
TS
9723 imm_reloc[0] = BFD_RELOC_UNUSED;
9724 imm_reloc[1] = BFD_RELOC_UNUSED;
9725 imm_reloc[2] = BFD_RELOC_UNUSED;
5f74bc13 9726 imm2_expr.X_op = O_absent;
252b5132 9727 offset_expr.X_op = O_absent;
f6688943
TS
9728 offset_reloc[0] = BFD_RELOC_UNUSED;
9729 offset_reloc[1] = BFD_RELOC_UNUSED;
9730 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
9731 for (args = insn->args; 1; ++args)
9732 {
9733 int c;
9734
9735 if (*s == ' ')
9736 ++s;
9737
9738 /* In this switch statement we call break if we did not find
9739 a match, continue if we did find a match, or return if we
9740 are done. */
9741
9742 c = *args;
9743 switch (c)
9744 {
9745 case '\0':
9746 if (*s == '\0')
9747 {
9748 /* Stuff the immediate value in now, if we can. */
9749 if (imm_expr.X_op == O_constant
f6688943 9750 && *imm_reloc > BFD_RELOC_UNUSED
252b5132
RH
9751 && insn->pinfo != INSN_MACRO)
9752 {
d6f16593
MR
9753 valueT tmp;
9754
9755 switch (*offset_reloc)
9756 {
9757 case BFD_RELOC_MIPS16_HI16_S:
9758 tmp = (imm_expr.X_add_number + 0x8000) >> 16;
9759 break;
9760
9761 case BFD_RELOC_MIPS16_HI16:
9762 tmp = imm_expr.X_add_number >> 16;
9763 break;
9764
9765 case BFD_RELOC_MIPS16_LO16:
9766 tmp = ((imm_expr.X_add_number + 0x8000) & 0xffff)
9767 - 0x8000;
9768 break;
9769
9770 case BFD_RELOC_UNUSED:
9771 tmp = imm_expr.X_add_number;
9772 break;
9773
9774 default:
9775 internalError ();
9776 }
9777 *offset_reloc = BFD_RELOC_UNUSED;
9778
c4e7957c 9779 mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
d6f16593 9780 tmp, TRUE, mips16_small,
252b5132
RH
9781 mips16_ext, &ip->insn_opcode,
9782 &ip->use_extend, &ip->extend);
9783 imm_expr.X_op = O_absent;
f6688943 9784 *imm_reloc = BFD_RELOC_UNUSED;
252b5132
RH
9785 }
9786
9787 return;
9788 }
9789 break;
9790
9791 case ',':
9792 if (*s++ == c)
9793 continue;
9794 s--;
9795 switch (*++args)
9796 {
9797 case 'v':
bf12938e 9798 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
252b5132
RH
9799 continue;
9800 case 'w':
bf12938e 9801 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
252b5132
RH
9802 continue;
9803 }
9804 break;
9805
9806 case '(':
9807 case ')':
9808 if (*s++ == c)
9809 continue;
9810 break;
9811
9812 case 'v':
9813 case 'w':
9814 if (s[0] != '$')
9815 {
9816 if (c == 'v')
bf12938e 9817 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
252b5132 9818 else
bf12938e 9819 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
252b5132
RH
9820 ++args;
9821 continue;
9822 }
9823 /* Fall through. */
9824 case 'x':
9825 case 'y':
9826 case 'z':
9827 case 'Z':
9828 case '0':
9829 case 'S':
9830 case 'R':
9831 case 'X':
9832 case 'Y':
707bfff6
TS
9833 s_reset = s;
9834 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno))
252b5132 9835 {
707bfff6 9836 if (c == 'v' || c == 'w')
85b51719 9837 {
707bfff6 9838 if (c == 'v')
a9e24354 9839 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
707bfff6 9840 else
a9e24354 9841 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
707bfff6
TS
9842 ++args;
9843 continue;
85b51719 9844 }
707bfff6 9845 break;
252b5132
RH
9846 }
9847
9848 if (*s == ' ')
9849 ++s;
9850 if (args[1] != *s)
9851 {
9852 if (c == 'v' || c == 'w')
9853 {
9854 regno = mips16_to_32_reg_map[lastregno];
9855 s = s_reset;
f9419b05 9856 ++args;
252b5132
RH
9857 }
9858 }
9859
9860 switch (c)
9861 {
9862 case 'x':
9863 case 'y':
9864 case 'z':
9865 case 'v':
9866 case 'w':
9867 case 'Z':
9868 regno = mips32_to_16_reg_map[regno];
9869 break;
9870
9871 case '0':
9872 if (regno != 0)
9873 regno = ILLEGAL_REG;
9874 break;
9875
9876 case 'S':
9877 if (regno != SP)
9878 regno = ILLEGAL_REG;
9879 break;
9880
9881 case 'R':
9882 if (regno != RA)
9883 regno = ILLEGAL_REG;
9884 break;
9885
9886 case 'X':
9887 case 'Y':
9888 if (regno == AT && ! mips_opts.noat)
9889 as_warn (_("used $at without \".set noat\""));
9890 break;
9891
9892 default:
9893 internalError ();
9894 }
9895
9896 if (regno == ILLEGAL_REG)
9897 break;
9898
9899 switch (c)
9900 {
9901 case 'x':
9902 case 'v':
bf12938e 9903 MIPS16_INSERT_OPERAND (RX, *ip, regno);
252b5132
RH
9904 break;
9905 case 'y':
9906 case 'w':
bf12938e 9907 MIPS16_INSERT_OPERAND (RY, *ip, regno);
252b5132
RH
9908 break;
9909 case 'z':
bf12938e 9910 MIPS16_INSERT_OPERAND (RZ, *ip, regno);
252b5132
RH
9911 break;
9912 case 'Z':
bf12938e 9913 MIPS16_INSERT_OPERAND (MOVE32Z, *ip, regno);
252b5132
RH
9914 case '0':
9915 case 'S':
9916 case 'R':
9917 break;
9918 case 'X':
bf12938e 9919 MIPS16_INSERT_OPERAND (REGR32, *ip, regno);
252b5132
RH
9920 break;
9921 case 'Y':
9922 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
bf12938e 9923 MIPS16_INSERT_OPERAND (REG32R, *ip, regno);
252b5132
RH
9924 break;
9925 default:
9926 internalError ();
9927 }
9928
9929 lastregno = regno;
9930 continue;
9931
9932 case 'P':
9933 if (strncmp (s, "$pc", 3) == 0)
9934 {
9935 s += 3;
9936 continue;
9937 }
9938 break;
9939
252b5132
RH
9940 case '5':
9941 case 'H':
9942 case 'W':
9943 case 'D':
9944 case 'j':
252b5132
RH
9945 case 'V':
9946 case 'C':
9947 case 'U':
9948 case 'k':
9949 case 'K':
d6f16593
MR
9950 i = my_getSmallExpression (&imm_expr, imm_reloc, s);
9951 if (i > 0)
252b5132 9952 {
d6f16593 9953 if (imm_expr.X_op != O_constant)
252b5132 9954 {
b34976b6 9955 mips16_ext = TRUE;
b34976b6 9956 ip->use_extend = TRUE;
252b5132 9957 ip->extend = 0;
252b5132 9958 }
d6f16593
MR
9959 else
9960 {
9961 /* We need to relax this instruction. */
9962 *offset_reloc = *imm_reloc;
9963 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9964 }
9965 s = expr_end;
9966 continue;
252b5132 9967 }
d6f16593
MR
9968 *imm_reloc = BFD_RELOC_UNUSED;
9969 /* Fall through. */
9970 case '<':
9971 case '>':
9972 case '[':
9973 case ']':
9974 case '4':
9975 case '8':
9976 my_getExpression (&imm_expr, s);
252b5132
RH
9977 if (imm_expr.X_op == O_register)
9978 {
9979 /* What we thought was an expression turned out to
9980 be a register. */
9981
9982 if (s[0] == '(' && args[1] == '(')
9983 {
9984 /* It looks like the expression was omitted
9985 before a register indirection, which means
9986 that the expression is implicitly zero. We
9987 still set up imm_expr, so that we handle
9988 explicit extensions correctly. */
9989 imm_expr.X_op = O_constant;
9990 imm_expr.X_add_number = 0;
f6688943 9991 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
9992 continue;
9993 }
9994
9995 break;
9996 }
9997
9998 /* We need to relax this instruction. */
f6688943 9999 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
10000 s = expr_end;
10001 continue;
10002
10003 case 'p':
10004 case 'q':
10005 case 'A':
10006 case 'B':
10007 case 'E':
10008 /* We use offset_reloc rather than imm_reloc for the PC
10009 relative operands. This lets macros with both
10010 immediate and address operands work correctly. */
10011 my_getExpression (&offset_expr, s);
10012
10013 if (offset_expr.X_op == O_register)
10014 break;
10015
10016 /* We need to relax this instruction. */
f6688943 10017 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
10018 s = expr_end;
10019 continue;
10020
10021 case '6': /* break code */
10022 my_getExpression (&imm_expr, s);
10023 check_absolute_expr (ip, &imm_expr);
10024 if ((unsigned long) imm_expr.X_add_number > 63)
bf12938e
RS
10025 as_warn (_("Invalid value for `%s' (%lu)"),
10026 ip->insn_mo->name,
10027 (unsigned long) imm_expr.X_add_number);
10028 MIPS16_INSERT_OPERAND (IMM6, *ip, imm_expr.X_add_number);
252b5132
RH
10029 imm_expr.X_op = O_absent;
10030 s = expr_end;
10031 continue;
10032
10033 case 'a': /* 26 bit address */
10034 my_getExpression (&offset_expr, s);
10035 s = expr_end;
f6688943 10036 *offset_reloc = BFD_RELOC_MIPS16_JMP;
252b5132
RH
10037 ip->insn_opcode <<= 16;
10038 continue;
10039
10040 case 'l': /* register list for entry macro */
10041 case 'L': /* register list for exit macro */
10042 {
10043 int mask;
10044
10045 if (c == 'l')
10046 mask = 0;
10047 else
10048 mask = 7 << 3;
10049 while (*s != '\0')
10050 {
707bfff6 10051 unsigned int freg, reg1, reg2;
252b5132
RH
10052
10053 while (*s == ' ' || *s == ',')
10054 ++s;
707bfff6 10055 if (reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg1))
252b5132 10056 freg = 0;
707bfff6
TS
10057 else if (reg_lookup (&s, RTYPE_FPU, &reg1))
10058 freg = 1;
252b5132
RH
10059 else
10060 {
707bfff6
TS
10061 as_bad (_("can't parse register list"));
10062 break;
252b5132
RH
10063 }
10064 if (*s == ' ')
10065 ++s;
10066 if (*s != '-')
10067 reg2 = reg1;
10068 else
10069 {
10070 ++s;
707bfff6
TS
10071 if (!reg_lookup (&s, freg ? RTYPE_FPU
10072 : (RTYPE_GP | RTYPE_NUM), &reg2))
252b5132 10073 {
707bfff6
TS
10074 as_bad (_("invalid register list"));
10075 break;
252b5132
RH
10076 }
10077 }
10078 if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
10079 {
10080 mask &= ~ (7 << 3);
10081 mask |= 5 << 3;
10082 }
10083 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
10084 {
10085 mask &= ~ (7 << 3);
10086 mask |= 6 << 3;
10087 }
10088 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
10089 mask |= (reg2 - 3) << 3;
10090 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
10091 mask |= (reg2 - 15) << 1;
f9419b05 10092 else if (reg1 == RA && reg2 == RA)
252b5132
RH
10093 mask |= 1;
10094 else
10095 {
10096 as_bad (_("invalid register list"));
10097 break;
10098 }
10099 }
10100 /* The mask is filled in in the opcode table for the
10101 benefit of the disassembler. We remove it before
10102 applying the actual mask. */
10103 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
10104 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
10105 }
10106 continue;
10107
0499d65b
TS
10108 case 'm': /* Register list for save insn. */
10109 case 'M': /* Register list for restore insn. */
10110 {
10111 int opcode = 0;
10112 int framesz = 0, seen_framesz = 0;
10113 int args = 0, statics = 0, sregs = 0;
10114
10115 while (*s != '\0')
10116 {
10117 unsigned int reg1, reg2;
10118
10119 SKIP_SPACE_TABS (s);
10120 while (*s == ',')
10121 ++s;
10122 SKIP_SPACE_TABS (s);
10123
10124 my_getExpression (&imm_expr, s);
10125 if (imm_expr.X_op == O_constant)
10126 {
10127 /* Handle the frame size. */
10128 if (seen_framesz)
10129 {
10130 as_bad (_("more than one frame size in list"));
10131 break;
10132 }
10133 seen_framesz = 1;
10134 framesz = imm_expr.X_add_number;
10135 imm_expr.X_op = O_absent;
10136 s = expr_end;
10137 continue;
10138 }
10139
707bfff6 10140 if (! reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg1))
0499d65b
TS
10141 {
10142 as_bad (_("can't parse register list"));
10143 break;
10144 }
0499d65b 10145
707bfff6
TS
10146 while (*s == ' ')
10147 ++s;
10148
0499d65b
TS
10149 if (*s != '-')
10150 reg2 = reg1;
10151 else
10152 {
10153 ++s;
707bfff6
TS
10154 if (! reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg2)
10155 || reg2 < reg1)
0499d65b
TS
10156 {
10157 as_bad (_("can't parse register list"));
10158 break;
10159 }
0499d65b
TS
10160 }
10161
10162 while (reg1 <= reg2)
10163 {
10164 if (reg1 >= 4 && reg1 <= 7)
10165 {
10166 if (c == 'm' && !seen_framesz)
10167 /* args $a0-$a3 */
10168 args |= 1 << (reg1 - 4);
10169 else
10170 /* statics $a0-$a3 */
10171 statics |= 1 << (reg1 - 4);
10172 }
10173 else if ((reg1 >= 16 && reg1 <= 23) || reg1 == 30)
10174 {
10175 /* $s0-$s8 */
10176 sregs |= 1 << ((reg1 == 30) ? 8 : (reg1 - 16));
10177 }
10178 else if (reg1 == 31)
10179 {
10180 /* Add $ra to insn. */
10181 opcode |= 0x40;
10182 }
10183 else
10184 {
10185 as_bad (_("unexpected register in list"));
10186 break;
10187 }
10188 if (++reg1 == 24)
10189 reg1 = 30;
10190 }
10191 }
10192
10193 /* Encode args/statics combination. */
10194 if (args & statics)
10195 as_bad (_("arg/static registers overlap"));
10196 else if (args == 0xf)
10197 /* All $a0-$a3 are args. */
10198 opcode |= MIPS16_ALL_ARGS << 16;
10199 else if (statics == 0xf)
10200 /* All $a0-$a3 are statics. */
10201 opcode |= MIPS16_ALL_STATICS << 16;
10202 else
10203 {
10204 int narg = 0, nstat = 0;
10205
10206 /* Count arg registers. */
10207 while (args & 0x1)
10208 {
10209 args >>= 1;
10210 narg++;
10211 }
10212 if (args != 0)
10213 as_bad (_("invalid arg register list"));
10214
10215 /* Count static registers. */
10216 while (statics & 0x8)
10217 {
10218 statics = (statics << 1) & 0xf;
10219 nstat++;
10220 }
10221 if (statics != 0)
10222 as_bad (_("invalid static register list"));
10223
10224 /* Encode args/statics. */
10225 opcode |= ((narg << 2) | nstat) << 16;
10226 }
10227
10228 /* Encode $s0/$s1. */
10229 if (sregs & (1 << 0)) /* $s0 */
10230 opcode |= 0x20;
10231 if (sregs & (1 << 1)) /* $s1 */
10232 opcode |= 0x10;
10233 sregs >>= 2;
10234
10235 if (sregs != 0)
10236 {
10237 /* Count regs $s2-$s8. */
10238 int nsreg = 0;
10239 while (sregs & 1)
10240 {
10241 sregs >>= 1;
10242 nsreg++;
10243 }
10244 if (sregs != 0)
10245 as_bad (_("invalid static register list"));
10246 /* Encode $s2-$s8. */
10247 opcode |= nsreg << 24;
10248 }
10249
10250 /* Encode frame size. */
10251 if (!seen_framesz)
10252 as_bad (_("missing frame size"));
10253 else if ((framesz & 7) != 0 || framesz < 0
10254 || framesz > 0xff * 8)
10255 as_bad (_("invalid frame size"));
10256 else if (framesz != 128 || (opcode >> 16) != 0)
10257 {
10258 framesz /= 8;
10259 opcode |= (((framesz & 0xf0) << 16)
10260 | (framesz & 0x0f));
10261 }
10262
10263 /* Finally build the instruction. */
10264 if ((opcode >> 16) != 0 || framesz == 0)
10265 {
10266 ip->use_extend = TRUE;
10267 ip->extend = opcode >> 16;
10268 }
10269 ip->insn_opcode |= opcode & 0x7f;
10270 }
10271 continue;
10272
252b5132
RH
10273 case 'e': /* extend code */
10274 my_getExpression (&imm_expr, s);
10275 check_absolute_expr (ip, &imm_expr);
10276 if ((unsigned long) imm_expr.X_add_number > 0x7ff)
10277 {
10278 as_warn (_("Invalid value for `%s' (%lu)"),
10279 ip->insn_mo->name,
10280 (unsigned long) imm_expr.X_add_number);
10281 imm_expr.X_add_number &= 0x7ff;
10282 }
10283 ip->insn_opcode |= imm_expr.X_add_number;
10284 imm_expr.X_op = O_absent;
10285 s = expr_end;
10286 continue;
10287
10288 default:
10289 internalError ();
10290 }
10291 break;
10292 }
10293
10294 /* Args don't match. */
10295 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
10296 strcmp (insn->name, insn[1].name) == 0)
10297 {
10298 ++insn;
10299 s = argsstart;
10300 continue;
10301 }
10302
10303 insn_error = _("illegal operands");
10304
10305 return;
10306 }
10307}
10308
10309/* This structure holds information we know about a mips16 immediate
10310 argument type. */
10311
e972090a
NC
10312struct mips16_immed_operand
10313{
252b5132
RH
10314 /* The type code used in the argument string in the opcode table. */
10315 int type;
10316 /* The number of bits in the short form of the opcode. */
10317 int nbits;
10318 /* The number of bits in the extended form of the opcode. */
10319 int extbits;
10320 /* The amount by which the short form is shifted when it is used;
10321 for example, the sw instruction has a shift count of 2. */
10322 int shift;
10323 /* The amount by which the short form is shifted when it is stored
10324 into the instruction code. */
10325 int op_shift;
10326 /* Non-zero if the short form is unsigned. */
10327 int unsp;
10328 /* Non-zero if the extended form is unsigned. */
10329 int extu;
10330 /* Non-zero if the value is PC relative. */
10331 int pcrel;
10332};
10333
10334/* The mips16 immediate operand types. */
10335
10336static const struct mips16_immed_operand mips16_immed_operands[] =
10337{
10338 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
10339 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 },
10340 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
10341 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 },
10342 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
10343 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
10344 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
10345 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
10346 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
10347 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
10348 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
10349 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
10350 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
10351 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
10352 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
10353 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
10354 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
10355 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
10356 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
10357 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
10358 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
10359};
10360
10361#define MIPS16_NUM_IMMED \
10362 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
10363
10364/* Handle a mips16 instruction with an immediate value. This or's the
10365 small immediate value into *INSN. It sets *USE_EXTEND to indicate
10366 whether an extended value is needed; if one is needed, it sets
10367 *EXTEND to the value. The argument type is TYPE. The value is VAL.
10368 If SMALL is true, an unextended opcode was explicitly requested.
10369 If EXT is true, an extended opcode was explicitly requested. If
10370 WARN is true, warn if EXT does not match reality. */
10371
10372static void
17a2f251
TS
10373mips16_immed (char *file, unsigned int line, int type, offsetT val,
10374 bfd_boolean warn, bfd_boolean small, bfd_boolean ext,
10375 unsigned long *insn, bfd_boolean *use_extend,
10376 unsigned short *extend)
252b5132 10377{
3994f87e 10378 const struct mips16_immed_operand *op;
252b5132 10379 int mintiny, maxtiny;
b34976b6 10380 bfd_boolean needext;
252b5132
RH
10381
10382 op = mips16_immed_operands;
10383 while (op->type != type)
10384 {
10385 ++op;
10386 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
10387 }
10388
10389 if (op->unsp)
10390 {
10391 if (type == '<' || type == '>' || type == '[' || type == ']')
10392 {
10393 mintiny = 1;
10394 maxtiny = 1 << op->nbits;
10395 }
10396 else
10397 {
10398 mintiny = 0;
10399 maxtiny = (1 << op->nbits) - 1;
10400 }
10401 }
10402 else
10403 {
10404 mintiny = - (1 << (op->nbits - 1));
10405 maxtiny = (1 << (op->nbits - 1)) - 1;
10406 }
10407
10408 /* Branch offsets have an implicit 0 in the lowest bit. */
10409 if (type == 'p' || type == 'q')
10410 val /= 2;
10411
10412 if ((val & ((1 << op->shift) - 1)) != 0
10413 || val < (mintiny << op->shift)
10414 || val > (maxtiny << op->shift))
b34976b6 10415 needext = TRUE;
252b5132 10416 else
b34976b6 10417 needext = FALSE;
252b5132
RH
10418
10419 if (warn && ext && ! needext)
beae10d5
KH
10420 as_warn_where (file, line,
10421 _("extended operand requested but not required"));
252b5132
RH
10422 if (small && needext)
10423 as_bad_where (file, line, _("invalid unextended operand value"));
10424
10425 if (small || (! ext && ! needext))
10426 {
10427 int insnval;
10428
b34976b6 10429 *use_extend = FALSE;
252b5132
RH
10430 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
10431 insnval <<= op->op_shift;
10432 *insn |= insnval;
10433 }
10434 else
10435 {
10436 long minext, maxext;
10437 int extval;
10438
10439 if (op->extu)
10440 {
10441 minext = 0;
10442 maxext = (1 << op->extbits) - 1;
10443 }
10444 else
10445 {
10446 minext = - (1 << (op->extbits - 1));
10447 maxext = (1 << (op->extbits - 1)) - 1;
10448 }
10449 if (val < minext || val > maxext)
10450 as_bad_where (file, line,
10451 _("operand value out of range for instruction"));
10452
b34976b6 10453 *use_extend = TRUE;
252b5132
RH
10454 if (op->extbits == 16)
10455 {
10456 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
10457 val &= 0x1f;
10458 }
10459 else if (op->extbits == 15)
10460 {
10461 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
10462 val &= 0xf;
10463 }
10464 else
10465 {
10466 extval = ((val & 0x1f) << 6) | (val & 0x20);
10467 val = 0;
10468 }
10469
10470 *extend = (unsigned short) extval;
10471 *insn |= val;
10472 }
10473}
10474\f
d6f16593 10475struct percent_op_match
ad8d3bb3 10476{
5e0116d5
RS
10477 const char *str;
10478 bfd_reloc_code_real_type reloc;
d6f16593
MR
10479};
10480
10481static const struct percent_op_match mips_percent_op[] =
ad8d3bb3 10482{
5e0116d5 10483 {"%lo", BFD_RELOC_LO16},
ad8d3bb3 10484#ifdef OBJ_ELF
5e0116d5
RS
10485 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
10486 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
10487 {"%call16", BFD_RELOC_MIPS_CALL16},
10488 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
10489 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
10490 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
10491 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
10492 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
10493 {"%got", BFD_RELOC_MIPS_GOT16},
10494 {"%gp_rel", BFD_RELOC_GPREL16},
10495 {"%half", BFD_RELOC_16},
10496 {"%highest", BFD_RELOC_MIPS_HIGHEST},
10497 {"%higher", BFD_RELOC_MIPS_HIGHER},
10498 {"%neg", BFD_RELOC_MIPS_SUB},
3f98094e
DJ
10499 {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
10500 {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
10501 {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
10502 {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
10503 {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
10504 {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
10505 {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
ad8d3bb3 10506#endif
5e0116d5 10507 {"%hi", BFD_RELOC_HI16_S}
ad8d3bb3
TS
10508};
10509
d6f16593
MR
10510static const struct percent_op_match mips16_percent_op[] =
10511{
10512 {"%lo", BFD_RELOC_MIPS16_LO16},
10513 {"%gprel", BFD_RELOC_MIPS16_GPREL},
10514 {"%hi", BFD_RELOC_MIPS16_HI16_S}
10515};
10516
252b5132 10517
5e0116d5
RS
10518/* Return true if *STR points to a relocation operator. When returning true,
10519 move *STR over the operator and store its relocation code in *RELOC.
10520 Leave both *STR and *RELOC alone when returning false. */
10521
10522static bfd_boolean
17a2f251 10523parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
252b5132 10524{
d6f16593
MR
10525 const struct percent_op_match *percent_op;
10526 size_t limit, i;
10527
10528 if (mips_opts.mips16)
10529 {
10530 percent_op = mips16_percent_op;
10531 limit = ARRAY_SIZE (mips16_percent_op);
10532 }
10533 else
10534 {
10535 percent_op = mips_percent_op;
10536 limit = ARRAY_SIZE (mips_percent_op);
10537 }
76b3015f 10538
d6f16593 10539 for (i = 0; i < limit; i++)
5e0116d5 10540 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
394f9b3a 10541 {
3f98094e
DJ
10542 int len = strlen (percent_op[i].str);
10543
10544 if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
10545 continue;
10546
5e0116d5
RS
10547 *str += strlen (percent_op[i].str);
10548 *reloc = percent_op[i].reloc;
394f9b3a 10549
5e0116d5
RS
10550 /* Check whether the output BFD supports this relocation.
10551 If not, issue an error and fall back on something safe. */
10552 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
394f9b3a 10553 {
5e0116d5
RS
10554 as_bad ("relocation %s isn't supported by the current ABI",
10555 percent_op[i].str);
01a3f561 10556 *reloc = BFD_RELOC_UNUSED;
394f9b3a 10557 }
5e0116d5 10558 return TRUE;
394f9b3a 10559 }
5e0116d5 10560 return FALSE;
394f9b3a 10561}
ad8d3bb3 10562
ad8d3bb3 10563
5e0116d5
RS
10564/* Parse string STR as a 16-bit relocatable operand. Store the
10565 expression in *EP and the relocations in the array starting
10566 at RELOC. Return the number of relocation operators used.
ad8d3bb3 10567
01a3f561 10568 On exit, EXPR_END points to the first character after the expression. */
ad8d3bb3 10569
5e0116d5 10570static size_t
17a2f251
TS
10571my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
10572 char *str)
ad8d3bb3 10573{
5e0116d5
RS
10574 bfd_reloc_code_real_type reversed_reloc[3];
10575 size_t reloc_index, i;
09b8f35a
RS
10576 int crux_depth, str_depth;
10577 char *crux;
5e0116d5
RS
10578
10579 /* Search for the start of the main expression, recoding relocations
09b8f35a
RS
10580 in REVERSED_RELOC. End the loop with CRUX pointing to the start
10581 of the main expression and with CRUX_DEPTH containing the number
10582 of open brackets at that point. */
10583 reloc_index = -1;
10584 str_depth = 0;
10585 do
fb1b3232 10586 {
09b8f35a
RS
10587 reloc_index++;
10588 crux = str;
10589 crux_depth = str_depth;
10590
10591 /* Skip over whitespace and brackets, keeping count of the number
10592 of brackets. */
10593 while (*str == ' ' || *str == '\t' || *str == '(')
10594 if (*str++ == '(')
10595 str_depth++;
5e0116d5 10596 }
09b8f35a
RS
10597 while (*str == '%'
10598 && reloc_index < (HAVE_NEWABI ? 3 : 1)
10599 && parse_relocation (&str, &reversed_reloc[reloc_index]));
ad8d3bb3 10600
09b8f35a 10601 my_getExpression (ep, crux);
5e0116d5 10602 str = expr_end;
394f9b3a 10603
5e0116d5 10604 /* Match every open bracket. */
09b8f35a 10605 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
5e0116d5 10606 if (*str++ == ')')
09b8f35a 10607 crux_depth--;
394f9b3a 10608
09b8f35a 10609 if (crux_depth > 0)
5e0116d5 10610 as_bad ("unclosed '('");
394f9b3a 10611
5e0116d5 10612 expr_end = str;
252b5132 10613
01a3f561 10614 if (reloc_index != 0)
64bdfcaf
RS
10615 {
10616 prev_reloc_op_frag = frag_now;
10617 for (i = 0; i < reloc_index; i++)
10618 reloc[i] = reversed_reloc[reloc_index - 1 - i];
10619 }
fb1b3232 10620
5e0116d5 10621 return reloc_index;
252b5132
RH
10622}
10623
10624static void
17a2f251 10625my_getExpression (expressionS *ep, char *str)
252b5132
RH
10626{
10627 char *save_in;
98aa84af 10628 valueT val;
252b5132
RH
10629
10630 save_in = input_line_pointer;
10631 input_line_pointer = str;
10632 expression (ep);
10633 expr_end = input_line_pointer;
10634 input_line_pointer = save_in;
10635
10636 /* If we are in mips16 mode, and this is an expression based on `.',
10637 then we bump the value of the symbol by 1 since that is how other
10638 text symbols are handled. We don't bother to handle complex
10639 expressions, just `.' plus or minus a constant. */
10640 if (mips_opts.mips16
10641 && ep->X_op == O_symbol
10642 && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
10643 && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
49309057
ILT
10644 && symbol_get_frag (ep->X_add_symbol) == frag_now
10645 && symbol_constant_p (ep->X_add_symbol)
98aa84af
AM
10646 && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
10647 S_SET_VALUE (ep->X_add_symbol, val + 1);
252b5132
RH
10648}
10649
10650/* Turn a string in input_line_pointer into a floating point constant
bc0d738a
NC
10651 of type TYPE, and store the appropriate bytes in *LITP. The number
10652 of LITTLENUMS emitted is stored in *SIZEP. An error message is
252b5132
RH
10653 returned, or NULL on OK. */
10654
10655char *
17a2f251 10656md_atof (int type, char *litP, int *sizeP)
252b5132
RH
10657{
10658 int prec;
10659 LITTLENUM_TYPE words[4];
10660 char *t;
10661 int i;
10662
10663 switch (type)
10664 {
10665 case 'f':
10666 prec = 2;
10667 break;
10668
10669 case 'd':
10670 prec = 4;
10671 break;
10672
10673 default:
10674 *sizeP = 0;
10675 return _("bad call to md_atof");
10676 }
10677
10678 t = atof_ieee (input_line_pointer, type, words);
10679 if (t)
10680 input_line_pointer = t;
10681
10682 *sizeP = prec * 2;
10683
10684 if (! target_big_endian)
10685 {
10686 for (i = prec - 1; i >= 0; i--)
10687 {
17a2f251 10688 md_number_to_chars (litP, words[i], 2);
252b5132
RH
10689 litP += 2;
10690 }
10691 }
10692 else
10693 {
10694 for (i = 0; i < prec; i++)
10695 {
17a2f251 10696 md_number_to_chars (litP, words[i], 2);
252b5132
RH
10697 litP += 2;
10698 }
10699 }
bdaaa2e1 10700
252b5132
RH
10701 return NULL;
10702}
10703
10704void
17a2f251 10705md_number_to_chars (char *buf, valueT val, int n)
252b5132
RH
10706{
10707 if (target_big_endian)
10708 number_to_chars_bigendian (buf, val, n);
10709 else
10710 number_to_chars_littleendian (buf, val, n);
10711}
10712\f
ae948b86 10713#ifdef OBJ_ELF
e013f690
TS
10714static int support_64bit_objects(void)
10715{
10716 const char **list, **l;
aa3d8fdf 10717 int yes;
e013f690
TS
10718
10719 list = bfd_target_list ();
10720 for (l = list; *l != NULL; l++)
10721#ifdef TE_TMIPS
10722 /* This is traditional mips */
10723 if (strcmp (*l, "elf64-tradbigmips") == 0
10724 || strcmp (*l, "elf64-tradlittlemips") == 0)
10725#else
10726 if (strcmp (*l, "elf64-bigmips") == 0
10727 || strcmp (*l, "elf64-littlemips") == 0)
10728#endif
10729 break;
aa3d8fdf 10730 yes = (*l != NULL);
e013f690 10731 free (list);
aa3d8fdf 10732 return yes;
e013f690 10733}
ae948b86 10734#endif /* OBJ_ELF */
e013f690 10735
78849248 10736const char *md_shortopts = "O::g::G:";
252b5132 10737
e972090a
NC
10738struct option md_longopts[] =
10739{
f9b4148d
CD
10740 /* Options which specify architecture. */
10741#define OPTION_ARCH_BASE (OPTION_MD_BASE)
10742#define OPTION_MARCH (OPTION_ARCH_BASE + 0)
10743 {"march", required_argument, NULL, OPTION_MARCH},
10744#define OPTION_MTUNE (OPTION_ARCH_BASE + 1)
10745 {"mtune", required_argument, NULL, OPTION_MTUNE},
10746#define OPTION_MIPS1 (OPTION_ARCH_BASE + 2)
252b5132
RH
10747 {"mips0", no_argument, NULL, OPTION_MIPS1},
10748 {"mips1", no_argument, NULL, OPTION_MIPS1},
f9b4148d 10749#define OPTION_MIPS2 (OPTION_ARCH_BASE + 3)
252b5132 10750 {"mips2", no_argument, NULL, OPTION_MIPS2},
f9b4148d 10751#define OPTION_MIPS3 (OPTION_ARCH_BASE + 4)
252b5132 10752 {"mips3", no_argument, NULL, OPTION_MIPS3},
f9b4148d 10753#define OPTION_MIPS4 (OPTION_ARCH_BASE + 5)
252b5132 10754 {"mips4", no_argument, NULL, OPTION_MIPS4},
f9b4148d 10755#define OPTION_MIPS5 (OPTION_ARCH_BASE + 6)
ae948b86 10756 {"mips5", no_argument, NULL, OPTION_MIPS5},
f9b4148d 10757#define OPTION_MIPS32 (OPTION_ARCH_BASE + 7)
ae948b86 10758 {"mips32", no_argument, NULL, OPTION_MIPS32},
f9b4148d 10759#define OPTION_MIPS64 (OPTION_ARCH_BASE + 8)
ae948b86 10760 {"mips64", no_argument, NULL, OPTION_MIPS64},
f9b4148d
CD
10761#define OPTION_MIPS32R2 (OPTION_ARCH_BASE + 9)
10762 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
5f74bc13
CD
10763#define OPTION_MIPS64R2 (OPTION_ARCH_BASE + 10)
10764 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
f9b4148d
CD
10765
10766 /* Options which specify Application Specific Extensions (ASEs). */
5f74bc13 10767#define OPTION_ASE_BASE (OPTION_ARCH_BASE + 11)
f9b4148d
CD
10768#define OPTION_MIPS16 (OPTION_ASE_BASE + 0)
10769 {"mips16", no_argument, NULL, OPTION_MIPS16},
10770#define OPTION_NO_MIPS16 (OPTION_ASE_BASE + 1)
10771 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
10772#define OPTION_MIPS3D (OPTION_ASE_BASE + 2)
10773 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
10774#define OPTION_NO_MIPS3D (OPTION_ASE_BASE + 3)
10775 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
10776#define OPTION_MDMX (OPTION_ASE_BASE + 4)
10777 {"mdmx", no_argument, NULL, OPTION_MDMX},
10778#define OPTION_NO_MDMX (OPTION_ASE_BASE + 5)
10779 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
74cd071d
CF
10780#define OPTION_DSP (OPTION_ASE_BASE + 6)
10781 {"mdsp", no_argument, NULL, OPTION_DSP},
10782#define OPTION_NO_DSP (OPTION_ASE_BASE + 7)
10783 {"mno-dsp", no_argument, NULL, OPTION_NO_DSP},
ef2e4d86
CF
10784#define OPTION_MT (OPTION_ASE_BASE + 8)
10785 {"mmt", no_argument, NULL, OPTION_MT},
10786#define OPTION_NO_MT (OPTION_ASE_BASE + 9)
10787 {"mno-mt", no_argument, NULL, OPTION_NO_MT},
e16bfa71
TS
10788#define OPTION_SMARTMIPS (OPTION_ASE_BASE + 10)
10789 {"msmartmips", no_argument, NULL, OPTION_SMARTMIPS},
10790#define OPTION_NO_SMARTMIPS (OPTION_ASE_BASE + 11)
10791 {"mno-smartmips", no_argument, NULL, OPTION_NO_SMARTMIPS},
f9b4148d
CD
10792
10793 /* Old-style architecture options. Don't add more of these. */
e16bfa71 10794#define OPTION_COMPAT_ARCH_BASE (OPTION_ASE_BASE + 12)
f9b4148d
CD
10795#define OPTION_M4650 (OPTION_COMPAT_ARCH_BASE + 0)
10796 {"m4650", no_argument, NULL, OPTION_M4650},
10797#define OPTION_NO_M4650 (OPTION_COMPAT_ARCH_BASE + 1)
10798 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
10799#define OPTION_M4010 (OPTION_COMPAT_ARCH_BASE + 2)
10800 {"m4010", no_argument, NULL, OPTION_M4010},
10801#define OPTION_NO_M4010 (OPTION_COMPAT_ARCH_BASE + 3)
10802 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
10803#define OPTION_M4100 (OPTION_COMPAT_ARCH_BASE + 4)
10804 {"m4100", no_argument, NULL, OPTION_M4100},
10805#define OPTION_NO_M4100 (OPTION_COMPAT_ARCH_BASE + 5)
10806 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
10807#define OPTION_M3900 (OPTION_COMPAT_ARCH_BASE + 6)
10808 {"m3900", no_argument, NULL, OPTION_M3900},
10809#define OPTION_NO_M3900 (OPTION_COMPAT_ARCH_BASE + 7)
10810 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
10811
10812 /* Options which enable bug fixes. */
10813#define OPTION_FIX_BASE (OPTION_COMPAT_ARCH_BASE + 8)
10814#define OPTION_M7000_HILO_FIX (OPTION_FIX_BASE + 0)
10815 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
10816#define OPTION_MNO_7000_HILO_FIX (OPTION_FIX_BASE + 1)
10817 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10818 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
d766e8ec
RS
10819#define OPTION_FIX_VR4120 (OPTION_FIX_BASE + 2)
10820#define OPTION_NO_FIX_VR4120 (OPTION_FIX_BASE + 3)
10821 {"mfix-vr4120", no_argument, NULL, OPTION_FIX_VR4120},
10822 {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
7d8e00cf
RS
10823#define OPTION_FIX_VR4130 (OPTION_FIX_BASE + 4)
10824#define OPTION_NO_FIX_VR4130 (OPTION_FIX_BASE + 5)
10825 {"mfix-vr4130", no_argument, NULL, OPTION_FIX_VR4130},
10826 {"mno-fix-vr4130", no_argument, NULL, OPTION_NO_FIX_VR4130},
f9b4148d
CD
10827
10828 /* Miscellaneous options. */
7d8e00cf 10829#define OPTION_MISC_BASE (OPTION_FIX_BASE + 6)
1ffcab4b 10830#define OPTION_TRAP (OPTION_MISC_BASE + 0)
252b5132
RH
10831 {"trap", no_argument, NULL, OPTION_TRAP},
10832 {"no-break", no_argument, NULL, OPTION_TRAP},
1ffcab4b 10833#define OPTION_BREAK (OPTION_MISC_BASE + 1)
252b5132
RH
10834 {"break", no_argument, NULL, OPTION_BREAK},
10835 {"no-trap", no_argument, NULL, OPTION_BREAK},
1ffcab4b 10836#define OPTION_EB (OPTION_MISC_BASE + 2)
252b5132 10837 {"EB", no_argument, NULL, OPTION_EB},
1ffcab4b 10838#define OPTION_EL (OPTION_MISC_BASE + 3)
252b5132 10839 {"EL", no_argument, NULL, OPTION_EL},
1ffcab4b 10840#define OPTION_FP32 (OPTION_MISC_BASE + 4)
ae948b86 10841 {"mfp32", no_argument, NULL, OPTION_FP32},
1ffcab4b 10842#define OPTION_GP32 (OPTION_MISC_BASE + 5)
c97ef257 10843 {"mgp32", no_argument, NULL, OPTION_GP32},
1ffcab4b 10844#define OPTION_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 6)
119d663a 10845 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
1ffcab4b 10846#define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 7)
119d663a 10847 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
1ffcab4b 10848#define OPTION_FP64 (OPTION_MISC_BASE + 8)
316f5878 10849 {"mfp64", no_argument, NULL, OPTION_FP64},
1ffcab4b 10850#define OPTION_GP64 (OPTION_MISC_BASE + 9)
ae948b86 10851 {"mgp64", no_argument, NULL, OPTION_GP64},
1ffcab4b
CD
10852#define OPTION_RELAX_BRANCH (OPTION_MISC_BASE + 10)
10853#define OPTION_NO_RELAX_BRANCH (OPTION_MISC_BASE + 11)
4a6a3df4
AO
10854 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
10855 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
aa6975fb
ILT
10856#define OPTION_MSHARED (OPTION_MISC_BASE + 12)
10857#define OPTION_MNO_SHARED (OPTION_MISC_BASE + 13)
10858 {"mshared", no_argument, NULL, OPTION_MSHARED},
10859 {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
aed1a261
RS
10860#define OPTION_MSYM32 (OPTION_MISC_BASE + 14)
10861#define OPTION_MNO_SYM32 (OPTION_MISC_BASE + 15)
10862 {"msym32", no_argument, NULL, OPTION_MSYM32},
10863 {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
f9b4148d
CD
10864
10865 /* ELF-specific options. */
156c2f8b 10866#ifdef OBJ_ELF
aed1a261 10867#define OPTION_ELF_BASE (OPTION_MISC_BASE + 16)
156c2f8b 10868#define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
156c2f8b
NC
10869 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
10870 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
ae948b86 10871#define OPTION_NON_SHARED (OPTION_ELF_BASE + 1)
156c2f8b 10872 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
ae948b86 10873#define OPTION_XGOT (OPTION_ELF_BASE + 2)
156c2f8b 10874 {"xgot", no_argument, NULL, OPTION_XGOT},
ae948b86
TS
10875#define OPTION_MABI (OPTION_ELF_BASE + 3)
10876 {"mabi", required_argument, NULL, OPTION_MABI},
10877#define OPTION_32 (OPTION_ELF_BASE + 4)
156c2f8b 10878 {"32", no_argument, NULL, OPTION_32},
ae948b86 10879#define OPTION_N32 (OPTION_ELF_BASE + 5)
e013f690 10880 {"n32", no_argument, NULL, OPTION_N32},
ae948b86 10881#define OPTION_64 (OPTION_ELF_BASE + 6)
156c2f8b 10882 {"64", no_argument, NULL, OPTION_64},
ecb4347a
DJ
10883#define OPTION_MDEBUG (OPTION_ELF_BASE + 7)
10884 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
10885#define OPTION_NO_MDEBUG (OPTION_ELF_BASE + 8)
10886 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
dcd410fe
RO
10887#define OPTION_PDR (OPTION_ELF_BASE + 9)
10888 {"mpdr", no_argument, NULL, OPTION_PDR},
10889#define OPTION_NO_PDR (OPTION_ELF_BASE + 10)
10890 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
0a44bf69
RS
10891#define OPTION_MVXWORKS_PIC (OPTION_ELF_BASE + 11)
10892 {"mvxworks-pic", no_argument, NULL, OPTION_MVXWORKS_PIC},
ae948b86 10893#endif /* OBJ_ELF */
f9b4148d 10894
252b5132
RH
10895 {NULL, no_argument, NULL, 0}
10896};
156c2f8b 10897size_t md_longopts_size = sizeof (md_longopts);
252b5132 10898
316f5878
RS
10899/* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
10900 NEW_VALUE. Warn if another value was already specified. Note:
10901 we have to defer parsing the -march and -mtune arguments in order
10902 to handle 'from-abi' correctly, since the ABI might be specified
10903 in a later argument. */
10904
10905static void
17a2f251 10906mips_set_option_string (const char **string_ptr, const char *new_value)
316f5878
RS
10907{
10908 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
10909 as_warn (_("A different %s was already specified, is now %s"),
10910 string_ptr == &mips_arch_string ? "-march" : "-mtune",
10911 new_value);
10912
10913 *string_ptr = new_value;
10914}
10915
252b5132 10916int
17a2f251 10917md_parse_option (int c, char *arg)
252b5132
RH
10918{
10919 switch (c)
10920 {
119d663a
NC
10921 case OPTION_CONSTRUCT_FLOATS:
10922 mips_disable_float_construction = 0;
10923 break;
bdaaa2e1 10924
119d663a
NC
10925 case OPTION_NO_CONSTRUCT_FLOATS:
10926 mips_disable_float_construction = 1;
10927 break;
bdaaa2e1 10928
252b5132
RH
10929 case OPTION_TRAP:
10930 mips_trap = 1;
10931 break;
10932
10933 case OPTION_BREAK:
10934 mips_trap = 0;
10935 break;
10936
10937 case OPTION_EB:
10938 target_big_endian = 1;
10939 break;
10940
10941 case OPTION_EL:
10942 target_big_endian = 0;
10943 break;
10944
10945 case 'O':
10946 if (arg && arg[1] == '0')
10947 mips_optimize = 1;
10948 else
10949 mips_optimize = 2;
10950 break;
10951
10952 case 'g':
10953 if (arg == NULL)
10954 mips_debug = 2;
10955 else
10956 mips_debug = atoi (arg);
10957 /* When the MIPS assembler sees -g or -g2, it does not do
10958 optimizations which limit full symbolic debugging. We take
10959 that to be equivalent to -O0. */
10960 if (mips_debug == 2)
10961 mips_optimize = 1;
10962 break;
10963
10964 case OPTION_MIPS1:
316f5878 10965 file_mips_isa = ISA_MIPS1;
252b5132
RH
10966 break;
10967
10968 case OPTION_MIPS2:
316f5878 10969 file_mips_isa = ISA_MIPS2;
252b5132
RH
10970 break;
10971
10972 case OPTION_MIPS3:
316f5878 10973 file_mips_isa = ISA_MIPS3;
252b5132
RH
10974 break;
10975
10976 case OPTION_MIPS4:
316f5878 10977 file_mips_isa = ISA_MIPS4;
e7af610e
NC
10978 break;
10979
84ea6cf2 10980 case OPTION_MIPS5:
316f5878 10981 file_mips_isa = ISA_MIPS5;
84ea6cf2
NC
10982 break;
10983
e7af610e 10984 case OPTION_MIPS32:
316f5878 10985 file_mips_isa = ISA_MIPS32;
252b5132
RH
10986 break;
10987
af7ee8bf
CD
10988 case OPTION_MIPS32R2:
10989 file_mips_isa = ISA_MIPS32R2;
10990 break;
10991
5f74bc13
CD
10992 case OPTION_MIPS64R2:
10993 file_mips_isa = ISA_MIPS64R2;
10994 break;
10995
84ea6cf2 10996 case OPTION_MIPS64:
316f5878 10997 file_mips_isa = ISA_MIPS64;
84ea6cf2
NC
10998 break;
10999
ec68c924 11000 case OPTION_MTUNE:
316f5878
RS
11001 mips_set_option_string (&mips_tune_string, arg);
11002 break;
ec68c924 11003
316f5878
RS
11004 case OPTION_MARCH:
11005 mips_set_option_string (&mips_arch_string, arg);
252b5132
RH
11006 break;
11007
11008 case OPTION_M4650:
316f5878
RS
11009 mips_set_option_string (&mips_arch_string, "4650");
11010 mips_set_option_string (&mips_tune_string, "4650");
252b5132
RH
11011 break;
11012
11013 case OPTION_NO_M4650:
11014 break;
11015
11016 case OPTION_M4010:
316f5878
RS
11017 mips_set_option_string (&mips_arch_string, "4010");
11018 mips_set_option_string (&mips_tune_string, "4010");
252b5132
RH
11019 break;
11020
11021 case OPTION_NO_M4010:
11022 break;
11023
11024 case OPTION_M4100:
316f5878
RS
11025 mips_set_option_string (&mips_arch_string, "4100");
11026 mips_set_option_string (&mips_tune_string, "4100");
252b5132
RH
11027 break;
11028
11029 case OPTION_NO_M4100:
11030 break;
11031
252b5132 11032 case OPTION_M3900:
316f5878
RS
11033 mips_set_option_string (&mips_arch_string, "3900");
11034 mips_set_option_string (&mips_tune_string, "3900");
252b5132 11035 break;
bdaaa2e1 11036
252b5132
RH
11037 case OPTION_NO_M3900:
11038 break;
11039
deec1734
CD
11040 case OPTION_MDMX:
11041 mips_opts.ase_mdmx = 1;
11042 break;
11043
11044 case OPTION_NO_MDMX:
11045 mips_opts.ase_mdmx = 0;
11046 break;
11047
74cd071d
CF
11048 case OPTION_DSP:
11049 mips_opts.ase_dsp = 1;
11050 break;
11051
11052 case OPTION_NO_DSP:
11053 mips_opts.ase_dsp = 0;
11054 break;
11055
ef2e4d86
CF
11056 case OPTION_MT:
11057 mips_opts.ase_mt = 1;
11058 break;
11059
11060 case OPTION_NO_MT:
11061 mips_opts.ase_mt = 0;
11062 break;
11063
252b5132
RH
11064 case OPTION_MIPS16:
11065 mips_opts.mips16 = 1;
7d10b47d 11066 mips_no_prev_insn ();
252b5132
RH
11067 break;
11068
11069 case OPTION_NO_MIPS16:
11070 mips_opts.mips16 = 0;
7d10b47d 11071 mips_no_prev_insn ();
252b5132
RH
11072 break;
11073
1f25f5d3
CD
11074 case OPTION_MIPS3D:
11075 mips_opts.ase_mips3d = 1;
11076 break;
11077
11078 case OPTION_NO_MIPS3D:
11079 mips_opts.ase_mips3d = 0;
11080 break;
11081
e16bfa71
TS
11082 case OPTION_SMARTMIPS:
11083 mips_opts.ase_smartmips = 1;
11084 break;
11085
11086 case OPTION_NO_SMARTMIPS:
11087 mips_opts.ase_smartmips = 0;
11088 break;
11089
d766e8ec
RS
11090 case OPTION_FIX_VR4120:
11091 mips_fix_vr4120 = 1;
60b63b72
RS
11092 break;
11093
d766e8ec
RS
11094 case OPTION_NO_FIX_VR4120:
11095 mips_fix_vr4120 = 0;
60b63b72
RS
11096 break;
11097
7d8e00cf
RS
11098 case OPTION_FIX_VR4130:
11099 mips_fix_vr4130 = 1;
11100 break;
11101
11102 case OPTION_NO_FIX_VR4130:
11103 mips_fix_vr4130 = 0;
11104 break;
11105
4a6a3df4
AO
11106 case OPTION_RELAX_BRANCH:
11107 mips_relax_branch = 1;
11108 break;
11109
11110 case OPTION_NO_RELAX_BRANCH:
11111 mips_relax_branch = 0;
11112 break;
11113
aa6975fb
ILT
11114 case OPTION_MSHARED:
11115 mips_in_shared = TRUE;
11116 break;
11117
11118 case OPTION_MNO_SHARED:
11119 mips_in_shared = FALSE;
11120 break;
11121
aed1a261
RS
11122 case OPTION_MSYM32:
11123 mips_opts.sym32 = TRUE;
11124 break;
11125
11126 case OPTION_MNO_SYM32:
11127 mips_opts.sym32 = FALSE;
11128 break;
11129
0f074f60 11130#ifdef OBJ_ELF
252b5132
RH
11131 /* When generating ELF code, we permit -KPIC and -call_shared to
11132 select SVR4_PIC, and -non_shared to select no PIC. This is
11133 intended to be compatible with Irix 5. */
11134 case OPTION_CALL_SHARED:
11135 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11136 {
11137 as_bad (_("-call_shared is supported only for ELF format"));
11138 return 0;
11139 }
11140 mips_pic = SVR4_PIC;
143d77c5 11141 mips_abicalls = TRUE;
252b5132
RH
11142 break;
11143
11144 case OPTION_NON_SHARED:
11145 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11146 {
11147 as_bad (_("-non_shared is supported only for ELF format"));
11148 return 0;
11149 }
11150 mips_pic = NO_PIC;
143d77c5 11151 mips_abicalls = FALSE;
252b5132
RH
11152 break;
11153
44075ae2
TS
11154 /* The -xgot option tells the assembler to use 32 bit offsets
11155 when accessing the got in SVR4_PIC mode. It is for Irix
252b5132
RH
11156 compatibility. */
11157 case OPTION_XGOT:
11158 mips_big_got = 1;
11159 break;
0f074f60 11160#endif /* OBJ_ELF */
252b5132
RH
11161
11162 case 'G':
6caf9ef4
TS
11163 g_switch_value = atoi (arg);
11164 g_switch_seen = 1;
252b5132
RH
11165 break;
11166
0f074f60 11167#ifdef OBJ_ELF
34ba82a8
TS
11168 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
11169 and -mabi=64. */
252b5132 11170 case OPTION_32:
34ba82a8
TS
11171 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11172 {
11173 as_bad (_("-32 is supported for ELF format only"));
11174 return 0;
11175 }
316f5878 11176 mips_abi = O32_ABI;
252b5132
RH
11177 break;
11178
e013f690 11179 case OPTION_N32:
34ba82a8
TS
11180 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11181 {
11182 as_bad (_("-n32 is supported for ELF format only"));
11183 return 0;
11184 }
316f5878 11185 mips_abi = N32_ABI;
e013f690 11186 break;
252b5132 11187
e013f690 11188 case OPTION_64:
34ba82a8
TS
11189 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11190 {
11191 as_bad (_("-64 is supported for ELF format only"));
11192 return 0;
11193 }
316f5878 11194 mips_abi = N64_ABI;
e013f690
TS
11195 if (! support_64bit_objects())
11196 as_fatal (_("No compiled in support for 64 bit object file format"));
252b5132 11197 break;
ae948b86 11198#endif /* OBJ_ELF */
252b5132 11199
c97ef257 11200 case OPTION_GP32:
a325df1d 11201 file_mips_gp32 = 1;
c97ef257
AH
11202 break;
11203
11204 case OPTION_GP64:
a325df1d 11205 file_mips_gp32 = 0;
c97ef257 11206 break;
252b5132 11207
ca4e0257 11208 case OPTION_FP32:
a325df1d 11209 file_mips_fp32 = 1;
316f5878
RS
11210 break;
11211
11212 case OPTION_FP64:
11213 file_mips_fp32 = 0;
ca4e0257
RS
11214 break;
11215
ae948b86 11216#ifdef OBJ_ELF
252b5132 11217 case OPTION_MABI:
34ba82a8
TS
11218 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11219 {
11220 as_bad (_("-mabi is supported for ELF format only"));
11221 return 0;
11222 }
e013f690 11223 if (strcmp (arg, "32") == 0)
316f5878 11224 mips_abi = O32_ABI;
e013f690 11225 else if (strcmp (arg, "o64") == 0)
316f5878 11226 mips_abi = O64_ABI;
e013f690 11227 else if (strcmp (arg, "n32") == 0)
316f5878 11228 mips_abi = N32_ABI;
e013f690
TS
11229 else if (strcmp (arg, "64") == 0)
11230 {
316f5878 11231 mips_abi = N64_ABI;
e013f690
TS
11232 if (! support_64bit_objects())
11233 as_fatal (_("No compiled in support for 64 bit object file "
11234 "format"));
11235 }
11236 else if (strcmp (arg, "eabi") == 0)
316f5878 11237 mips_abi = EABI_ABI;
e013f690 11238 else
da0e507f
TS
11239 {
11240 as_fatal (_("invalid abi -mabi=%s"), arg);
11241 return 0;
11242 }
252b5132 11243 break;
e013f690 11244#endif /* OBJ_ELF */
252b5132 11245
6b76fefe 11246 case OPTION_M7000_HILO_FIX:
b34976b6 11247 mips_7000_hilo_fix = TRUE;
6b76fefe
CM
11248 break;
11249
9ee72ff1 11250 case OPTION_MNO_7000_HILO_FIX:
b34976b6 11251 mips_7000_hilo_fix = FALSE;
6b76fefe
CM
11252 break;
11253
ecb4347a
DJ
11254#ifdef OBJ_ELF
11255 case OPTION_MDEBUG:
b34976b6 11256 mips_flag_mdebug = TRUE;
ecb4347a
DJ
11257 break;
11258
11259 case OPTION_NO_MDEBUG:
b34976b6 11260 mips_flag_mdebug = FALSE;
ecb4347a 11261 break;
dcd410fe
RO
11262
11263 case OPTION_PDR:
11264 mips_flag_pdr = TRUE;
11265 break;
11266
11267 case OPTION_NO_PDR:
11268 mips_flag_pdr = FALSE;
11269 break;
0a44bf69
RS
11270
11271 case OPTION_MVXWORKS_PIC:
11272 mips_pic = VXWORKS_PIC;
11273 break;
ecb4347a
DJ
11274#endif /* OBJ_ELF */
11275
252b5132
RH
11276 default:
11277 return 0;
11278 }
11279
11280 return 1;
11281}
316f5878
RS
11282\f
11283/* Set up globals to generate code for the ISA or processor
11284 described by INFO. */
252b5132 11285
252b5132 11286static void
17a2f251 11287mips_set_architecture (const struct mips_cpu_info *info)
252b5132 11288{
316f5878 11289 if (info != 0)
252b5132 11290 {
fef14a42
TS
11291 file_mips_arch = info->cpu;
11292 mips_opts.arch = info->cpu;
316f5878 11293 mips_opts.isa = info->isa;
252b5132 11294 }
252b5132
RH
11295}
11296
252b5132 11297
316f5878 11298/* Likewise for tuning. */
252b5132 11299
316f5878 11300static void
17a2f251 11301mips_set_tune (const struct mips_cpu_info *info)
316f5878
RS
11302{
11303 if (info != 0)
fef14a42 11304 mips_tune = info->cpu;
316f5878 11305}
80cc45a5 11306
34ba82a8 11307
252b5132 11308void
17a2f251 11309mips_after_parse_args (void)
e9670677 11310{
fef14a42
TS
11311 const struct mips_cpu_info *arch_info = 0;
11312 const struct mips_cpu_info *tune_info = 0;
11313
e9670677 11314 /* GP relative stuff not working for PE */
6caf9ef4 11315 if (strncmp (TARGET_OS, "pe", 2) == 0)
e9670677 11316 {
6caf9ef4 11317 if (g_switch_seen && g_switch_value != 0)
e9670677
MR
11318 as_bad (_("-G not supported in this configuration."));
11319 g_switch_value = 0;
11320 }
11321
cac012d6
AO
11322 if (mips_abi == NO_ABI)
11323 mips_abi = MIPS_DEFAULT_ABI;
11324
22923709
RS
11325 /* The following code determines the architecture and register size.
11326 Similar code was added to GCC 3.3 (see override_options() in
11327 config/mips/mips.c). The GAS and GCC code should be kept in sync
11328 as much as possible. */
e9670677 11329
316f5878 11330 if (mips_arch_string != 0)
fef14a42 11331 arch_info = mips_parse_cpu ("-march", mips_arch_string);
e9670677 11332
316f5878 11333 if (file_mips_isa != ISA_UNKNOWN)
e9670677 11334 {
316f5878 11335 /* Handle -mipsN. At this point, file_mips_isa contains the
fef14a42 11336 ISA level specified by -mipsN, while arch_info->isa contains
316f5878 11337 the -march selection (if any). */
fef14a42 11338 if (arch_info != 0)
e9670677 11339 {
316f5878
RS
11340 /* -march takes precedence over -mipsN, since it is more descriptive.
11341 There's no harm in specifying both as long as the ISA levels
11342 are the same. */
fef14a42 11343 if (file_mips_isa != arch_info->isa)
316f5878
RS
11344 as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
11345 mips_cpu_info_from_isa (file_mips_isa)->name,
fef14a42 11346 mips_cpu_info_from_isa (arch_info->isa)->name);
e9670677 11347 }
316f5878 11348 else
fef14a42 11349 arch_info = mips_cpu_info_from_isa (file_mips_isa);
e9670677
MR
11350 }
11351
fef14a42
TS
11352 if (arch_info == 0)
11353 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
e9670677 11354
fef14a42 11355 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
316f5878 11356 as_bad ("-march=%s is not compatible with the selected ABI",
fef14a42
TS
11357 arch_info->name);
11358
11359 mips_set_architecture (arch_info);
11360
11361 /* Optimize for file_mips_arch, unless -mtune selects a different processor. */
11362 if (mips_tune_string != 0)
11363 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
e9670677 11364
fef14a42
TS
11365 if (tune_info == 0)
11366 mips_set_tune (arch_info);
11367 else
11368 mips_set_tune (tune_info);
e9670677 11369
316f5878 11370 if (file_mips_gp32 >= 0)
e9670677 11371 {
316f5878
RS
11372 /* The user specified the size of the integer registers. Make sure
11373 it agrees with the ABI and ISA. */
11374 if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
11375 as_bad (_("-mgp64 used with a 32-bit processor"));
11376 else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
11377 as_bad (_("-mgp32 used with a 64-bit ABI"));
11378 else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
11379 as_bad (_("-mgp64 used with a 32-bit ABI"));
e9670677
MR
11380 }
11381 else
11382 {
316f5878
RS
11383 /* Infer the integer register size from the ABI and processor.
11384 Restrict ourselves to 32-bit registers if that's all the
11385 processor has, or if the ABI cannot handle 64-bit registers. */
11386 file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
11387 || !ISA_HAS_64BIT_REGS (mips_opts.isa));
e9670677
MR
11388 }
11389
ad3fea08
TS
11390 switch (file_mips_fp32)
11391 {
11392 default:
11393 case -1:
11394 /* No user specified float register size.
11395 ??? GAS treats single-float processors as though they had 64-bit
11396 float registers (although it complains when double-precision
11397 instructions are used). As things stand, saying they have 32-bit
11398 registers would lead to spurious "register must be even" messages.
11399 So here we assume float registers are never smaller than the
11400 integer ones. */
11401 if (file_mips_gp32 == 0)
11402 /* 64-bit integer registers implies 64-bit float registers. */
11403 file_mips_fp32 = 0;
11404 else if ((mips_opts.ase_mips3d > 0 || mips_opts.ase_mdmx > 0)
11405 && ISA_HAS_64BIT_FPRS (mips_opts.isa))
11406 /* -mips3d and -mdmx imply 64-bit float registers, if possible. */
11407 file_mips_fp32 = 0;
11408 else
11409 /* 32-bit float registers. */
11410 file_mips_fp32 = 1;
11411 break;
11412
11413 /* The user specified the size of the float registers. Check if it
11414 agrees with the ABI and ISA. */
11415 case 0:
11416 if (!ISA_HAS_64BIT_FPRS (mips_opts.isa))
11417 as_bad (_("-mfp64 used with a 32-bit fpu"));
11418 else if (ABI_NEEDS_32BIT_REGS (mips_abi)
11419 && !ISA_HAS_MXHC1 (mips_opts.isa))
11420 as_warn (_("-mfp64 used with a 32-bit ABI"));
11421 break;
11422 case 1:
11423 if (ABI_NEEDS_64BIT_REGS (mips_abi))
11424 as_warn (_("-mfp32 used with a 64-bit ABI"));
11425 break;
11426 }
e9670677 11427
316f5878 11428 /* End of GCC-shared inference code. */
e9670677 11429
17a2f251
TS
11430 /* This flag is set when we have a 64-bit capable CPU but use only
11431 32-bit wide registers. Note that EABI does not use it. */
11432 if (ISA_HAS_64BIT_REGS (mips_opts.isa)
11433 && ((mips_abi == NO_ABI && file_mips_gp32 == 1)
11434 || mips_abi == O32_ABI))
316f5878 11435 mips_32bitmode = 1;
e9670677
MR
11436
11437 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
11438 as_bad (_("trap exception not supported at ISA 1"));
11439
e9670677
MR
11440 /* If the selected architecture includes support for ASEs, enable
11441 generation of code for them. */
a4672219 11442 if (mips_opts.mips16 == -1)
fef14a42 11443 mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_arch)) ? 1 : 0;
ffdefa66 11444 if (mips_opts.ase_mips3d == -1)
65263ce3 11445 mips_opts.ase_mips3d = ((arch_info->flags & MIPS_CPU_ASE_MIPS3D)
ad3fea08
TS
11446 && file_mips_fp32 == 0) ? 1 : 0;
11447 if (mips_opts.ase_mips3d && file_mips_fp32 == 1)
11448 as_bad (_("-mfp32 used with -mips3d"));
11449
ffdefa66 11450 if (mips_opts.ase_mdmx == -1)
65263ce3 11451 mips_opts.ase_mdmx = ((arch_info->flags & MIPS_CPU_ASE_MDMX)
ad3fea08
TS
11452 && file_mips_fp32 == 0) ? 1 : 0;
11453 if (mips_opts.ase_mdmx && file_mips_fp32 == 1)
11454 as_bad (_("-mfp32 used with -mdmx"));
11455
11456 if (mips_opts.ase_smartmips == -1)
11457 mips_opts.ase_smartmips = (arch_info->flags & MIPS_CPU_ASE_SMARTMIPS) ? 1 : 0;
11458 if (mips_opts.ase_smartmips && !ISA_SUPPORTS_SMARTMIPS)
11459 as_warn ("%s ISA does not support SmartMIPS",
11460 mips_cpu_info_from_isa (mips_opts.isa)->name);
11461
74cd071d 11462 if (mips_opts.ase_dsp == -1)
ad3fea08
TS
11463 mips_opts.ase_dsp = (arch_info->flags & MIPS_CPU_ASE_DSP) ? 1 : 0;
11464 if (mips_opts.ase_dsp && !ISA_SUPPORTS_DSP_ASE)
11465 as_warn ("%s ISA does not support DSP ASE",
11466 mips_cpu_info_from_isa (mips_opts.isa)->name);
11467
ef2e4d86 11468 if (mips_opts.ase_mt == -1)
ad3fea08
TS
11469 mips_opts.ase_mt = (arch_info->flags & MIPS_CPU_ASE_MT) ? 1 : 0;
11470 if (mips_opts.ase_mt && !ISA_SUPPORTS_MT_ASE)
11471 as_warn ("%s ISA does not support MT ASE",
11472 mips_cpu_info_from_isa (mips_opts.isa)->name);
e9670677 11473
e9670677 11474 file_mips_isa = mips_opts.isa;
a4672219 11475 file_ase_mips16 = mips_opts.mips16;
e9670677
MR
11476 file_ase_mips3d = mips_opts.ase_mips3d;
11477 file_ase_mdmx = mips_opts.ase_mdmx;
e16bfa71 11478 file_ase_smartmips = mips_opts.ase_smartmips;
74cd071d 11479 file_ase_dsp = mips_opts.ase_dsp;
ef2e4d86 11480 file_ase_mt = mips_opts.ase_mt;
e9670677
MR
11481 mips_opts.gp32 = file_mips_gp32;
11482 mips_opts.fp32 = file_mips_fp32;
11483
ecb4347a
DJ
11484 if (mips_flag_mdebug < 0)
11485 {
11486#ifdef OBJ_MAYBE_ECOFF
11487 if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
11488 mips_flag_mdebug = 1;
11489 else
11490#endif /* OBJ_MAYBE_ECOFF */
11491 mips_flag_mdebug = 0;
11492 }
e9670677
MR
11493}
11494\f
11495void
17a2f251 11496mips_init_after_args (void)
252b5132
RH
11497{
11498 /* initialize opcodes */
11499 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
beae10d5 11500 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
252b5132
RH
11501}
11502
11503long
17a2f251 11504md_pcrel_from (fixS *fixP)
252b5132 11505{
a7ebbfdf
TS
11506 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
11507 switch (fixP->fx_r_type)
11508 {
11509 case BFD_RELOC_16_PCREL_S2:
11510 case BFD_RELOC_MIPS_JMP:
11511 /* Return the address of the delay slot. */
11512 return addr + 4;
11513 default:
11514 return addr;
11515 }
252b5132
RH
11516}
11517
252b5132
RH
11518/* This is called before the symbol table is processed. In order to
11519 work with gcc when using mips-tfile, we must keep all local labels.
11520 However, in other cases, we want to discard them. If we were
11521 called with -g, but we didn't see any debugging information, it may
11522 mean that gcc is smuggling debugging information through to
11523 mips-tfile, in which case we must generate all local labels. */
11524
11525void
17a2f251 11526mips_frob_file_before_adjust (void)
252b5132
RH
11527{
11528#ifndef NO_ECOFF_DEBUGGING
11529 if (ECOFF_DEBUGGING
11530 && mips_debug != 0
11531 && ! ecoff_debugging_seen)
11532 flag_keep_locals = 1;
11533#endif
11534}
11535
3b91255e 11536/* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
55cf6793 11537 the corresponding LO16 reloc. This is called before md_apply_fix and
3b91255e
RS
11538 tc_gen_reloc. Unmatched relocs can only be generated by use of explicit
11539 relocation operators.
11540
11541 For our purposes, a %lo() expression matches a %got() or %hi()
11542 expression if:
11543
11544 (a) it refers to the same symbol; and
11545 (b) the offset applied in the %lo() expression is no lower than
11546 the offset applied in the %got() or %hi().
11547
11548 (b) allows us to cope with code like:
11549
11550 lui $4,%hi(foo)
11551 lh $4,%lo(foo+2)($4)
11552
11553 ...which is legal on RELA targets, and has a well-defined behaviour
11554 if the user knows that adding 2 to "foo" will not induce a carry to
11555 the high 16 bits.
11556
11557 When several %lo()s match a particular %got() or %hi(), we use the
11558 following rules to distinguish them:
11559
11560 (1) %lo()s with smaller offsets are a better match than %lo()s with
11561 higher offsets.
11562
11563 (2) %lo()s with no matching %got() or %hi() are better than those
11564 that already have a matching %got() or %hi().
11565
11566 (3) later %lo()s are better than earlier %lo()s.
11567
11568 These rules are applied in order.
11569
11570 (1) means, among other things, that %lo()s with identical offsets are
11571 chosen if they exist.
11572
11573 (2) means that we won't associate several high-part relocations with
11574 the same low-part relocation unless there's no alternative. Having
11575 several high parts for the same low part is a GNU extension; this rule
11576 allows careful users to avoid it.
11577
11578 (3) is purely cosmetic. mips_hi_fixup_list is is in reverse order,
11579 with the last high-part relocation being at the front of the list.
11580 It therefore makes sense to choose the last matching low-part
11581 relocation, all other things being equal. It's also easier
11582 to code that way. */
252b5132
RH
11583
11584void
17a2f251 11585mips_frob_file (void)
252b5132
RH
11586{
11587 struct mips_hi_fixup *l;
11588
11589 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
11590 {
11591 segment_info_type *seginfo;
3b91255e
RS
11592 bfd_boolean matched_lo_p;
11593 fixS **hi_pos, **lo_pos, **pos;
252b5132 11594
5919d012 11595 assert (reloc_needs_lo_p (l->fixp->fx_r_type));
252b5132 11596
5919d012
RS
11597 /* If a GOT16 relocation turns out to be against a global symbol,
11598 there isn't supposed to be a matching LO. */
11599 if (l->fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
11600 && !pic_need_relax (l->fixp->fx_addsy, l->seg))
11601 continue;
11602
11603 /* Check quickly whether the next fixup happens to be a matching %lo. */
11604 if (fixup_has_matching_lo_p (l->fixp))
252b5132
RH
11605 continue;
11606
252b5132 11607 seginfo = seg_info (l->seg);
252b5132 11608
3b91255e
RS
11609 /* Set HI_POS to the position of this relocation in the chain.
11610 Set LO_POS to the position of the chosen low-part relocation.
11611 MATCHED_LO_P is true on entry to the loop if *POS is a low-part
11612 relocation that matches an immediately-preceding high-part
11613 relocation. */
11614 hi_pos = NULL;
11615 lo_pos = NULL;
11616 matched_lo_p = FALSE;
11617 for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
11618 {
11619 if (*pos == l->fixp)
11620 hi_pos = pos;
11621
704803a9
MR
11622 if (((*pos)->fx_r_type == BFD_RELOC_LO16
11623 || (*pos)->fx_r_type == BFD_RELOC_MIPS16_LO16)
3b91255e
RS
11624 && (*pos)->fx_addsy == l->fixp->fx_addsy
11625 && (*pos)->fx_offset >= l->fixp->fx_offset
11626 && (lo_pos == NULL
11627 || (*pos)->fx_offset < (*lo_pos)->fx_offset
11628 || (!matched_lo_p
11629 && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
11630 lo_pos = pos;
11631
11632 matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
11633 && fixup_has_matching_lo_p (*pos));
11634 }
11635
11636 /* If we found a match, remove the high-part relocation from its
11637 current position and insert it before the low-part relocation.
11638 Make the offsets match so that fixup_has_matching_lo_p()
11639 will return true.
11640
11641 We don't warn about unmatched high-part relocations since some
11642 versions of gcc have been known to emit dead "lui ...%hi(...)"
11643 instructions. */
11644 if (lo_pos != NULL)
11645 {
11646 l->fixp->fx_offset = (*lo_pos)->fx_offset;
11647 if (l->fixp->fx_next != *lo_pos)
252b5132 11648 {
3b91255e
RS
11649 *hi_pos = l->fixp->fx_next;
11650 l->fixp->fx_next = *lo_pos;
11651 *lo_pos = l->fixp;
252b5132 11652 }
252b5132
RH
11653 }
11654 }
11655}
11656
3e722fb5 11657/* We may have combined relocations without symbols in the N32/N64 ABI.
f6688943 11658 We have to prevent gas from dropping them. */
252b5132 11659
252b5132 11660int
17a2f251 11661mips_force_relocation (fixS *fixp)
252b5132 11662{
ae6063d4 11663 if (generic_force_reloc (fixp))
252b5132
RH
11664 return 1;
11665
f6688943
TS
11666 if (HAVE_NEWABI
11667 && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
11668 && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
11669 || fixp->fx_r_type == BFD_RELOC_HI16_S
11670 || fixp->fx_r_type == BFD_RELOC_LO16))
11671 return 1;
11672
3e722fb5 11673 return 0;
252b5132
RH
11674}
11675
11676/* Apply a fixup to the object file. */
11677
94f592af 11678void
55cf6793 11679md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 11680{
874e8986 11681 bfd_byte *buf;
98aa84af 11682 long insn;
a7ebbfdf 11683 reloc_howto_type *howto;
252b5132 11684
a7ebbfdf
TS
11685 /* We ignore generic BFD relocations we don't know about. */
11686 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
11687 if (! howto)
11688 return;
65551fa4 11689
252b5132
RH
11690 assert (fixP->fx_size == 4
11691 || fixP->fx_r_type == BFD_RELOC_16
11692 || fixP->fx_r_type == BFD_RELOC_64
f6688943
TS
11693 || fixP->fx_r_type == BFD_RELOC_CTOR
11694 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
252b5132 11695 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
a7ebbfdf 11696 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY);
252b5132 11697
a7ebbfdf 11698 buf = (bfd_byte *) (fixP->fx_frag->fr_literal + fixP->fx_where);
252b5132 11699
3994f87e 11700 assert (!fixP->fx_pcrel || fixP->fx_r_type == BFD_RELOC_16_PCREL_S2);
b1dca8ee
RS
11701
11702 /* Don't treat parts of a composite relocation as done. There are two
11703 reasons for this:
11704
11705 (1) The second and third parts will be against 0 (RSS_UNDEF) but
11706 should nevertheless be emitted if the first part is.
11707
11708 (2) In normal usage, composite relocations are never assembly-time
11709 constants. The easiest way of dealing with the pathological
11710 exceptions is to generate a relocation against STN_UNDEF and
11711 leave everything up to the linker. */
3994f87e 11712 if (fixP->fx_addsy == NULL && !fixP->fx_pcrel && fixP->fx_tcbit == 0)
252b5132
RH
11713 fixP->fx_done = 1;
11714
11715 switch (fixP->fx_r_type)
11716 {
3f98094e
DJ
11717 case BFD_RELOC_MIPS_TLS_GD:
11718 case BFD_RELOC_MIPS_TLS_LDM:
11719 case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
11720 case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
11721 case BFD_RELOC_MIPS_TLS_GOTTPREL:
11722 case BFD_RELOC_MIPS_TLS_TPREL_HI16:
11723 case BFD_RELOC_MIPS_TLS_TPREL_LO16:
11724 S_SET_THREAD_LOCAL (fixP->fx_addsy);
11725 /* fall through */
11726
252b5132 11727 case BFD_RELOC_MIPS_JMP:
e369bcce
TS
11728 case BFD_RELOC_MIPS_SHIFT5:
11729 case BFD_RELOC_MIPS_SHIFT6:
11730 case BFD_RELOC_MIPS_GOT_DISP:
11731 case BFD_RELOC_MIPS_GOT_PAGE:
11732 case BFD_RELOC_MIPS_GOT_OFST:
11733 case BFD_RELOC_MIPS_SUB:
11734 case BFD_RELOC_MIPS_INSERT_A:
11735 case BFD_RELOC_MIPS_INSERT_B:
11736 case BFD_RELOC_MIPS_DELETE:
11737 case BFD_RELOC_MIPS_HIGHEST:
11738 case BFD_RELOC_MIPS_HIGHER:
11739 case BFD_RELOC_MIPS_SCN_DISP:
11740 case BFD_RELOC_MIPS_REL16:
11741 case BFD_RELOC_MIPS_RELGOT:
11742 case BFD_RELOC_MIPS_JALR:
252b5132
RH
11743 case BFD_RELOC_HI16:
11744 case BFD_RELOC_HI16_S:
cdf6fd85 11745 case BFD_RELOC_GPREL16:
252b5132
RH
11746 case BFD_RELOC_MIPS_LITERAL:
11747 case BFD_RELOC_MIPS_CALL16:
11748 case BFD_RELOC_MIPS_GOT16:
cdf6fd85 11749 case BFD_RELOC_GPREL32:
252b5132
RH
11750 case BFD_RELOC_MIPS_GOT_HI16:
11751 case BFD_RELOC_MIPS_GOT_LO16:
11752 case BFD_RELOC_MIPS_CALL_HI16:
11753 case BFD_RELOC_MIPS_CALL_LO16:
11754 case BFD_RELOC_MIPS16_GPREL:
d6f16593
MR
11755 case BFD_RELOC_MIPS16_HI16:
11756 case BFD_RELOC_MIPS16_HI16_S:
252b5132
RH
11757 /* Nothing needed to do. The value comes from the reloc entry */
11758 break;
11759
11760 case BFD_RELOC_MIPS16_JMP:
11761 /* We currently always generate a reloc against a symbol, which
11762 means that we don't want an addend even if the symbol is
11763 defined. */
a7ebbfdf 11764 *valP = 0;
252b5132
RH
11765 break;
11766
252b5132
RH
11767 case BFD_RELOC_64:
11768 /* This is handled like BFD_RELOC_32, but we output a sign
11769 extended value if we are only 32 bits. */
3e722fb5 11770 if (fixP->fx_done)
252b5132
RH
11771 {
11772 if (8 <= sizeof (valueT))
2132e3a3 11773 md_number_to_chars ((char *) buf, *valP, 8);
252b5132
RH
11774 else
11775 {
a7ebbfdf 11776 valueT hiv;
252b5132 11777
a7ebbfdf 11778 if ((*valP & 0x80000000) != 0)
252b5132
RH
11779 hiv = 0xffffffff;
11780 else
11781 hiv = 0;
b215186b 11782 md_number_to_chars ((char *)(buf + (target_big_endian ? 4 : 0)),
a7ebbfdf 11783 *valP, 4);
b215186b 11784 md_number_to_chars ((char *)(buf + (target_big_endian ? 0 : 4)),
a7ebbfdf 11785 hiv, 4);
252b5132
RH
11786 }
11787 }
11788 break;
11789
056350c6 11790 case BFD_RELOC_RVA:
252b5132
RH
11791 case BFD_RELOC_32:
11792 /* If we are deleting this reloc entry, we must fill in the
11793 value now. This can happen if we have a .word which is not
3e722fb5
CD
11794 resolved when it appears but is later defined. */
11795 if (fixP->fx_done)
2132e3a3 11796 md_number_to_chars ((char *) buf, *valP, 4);
252b5132
RH
11797 break;
11798
11799 case BFD_RELOC_16:
11800 /* If we are deleting this reloc entry, we must fill in the
11801 value now. */
252b5132 11802 if (fixP->fx_done)
2132e3a3 11803 md_number_to_chars ((char *) buf, *valP, 2);
252b5132
RH
11804 break;
11805
11806 case BFD_RELOC_LO16:
d6f16593 11807 case BFD_RELOC_MIPS16_LO16:
3e722fb5
CD
11808 /* FIXME: Now that embedded-PIC is gone, some of this code/comment
11809 may be safe to remove, but if so it's not obvious. */
252b5132
RH
11810 /* When handling an embedded PIC switch statement, we can wind
11811 up deleting a LO16 reloc. See the 'o' case in mips_ip. */
11812 if (fixP->fx_done)
11813 {
a7ebbfdf 11814 if (*valP + 0x8000 > 0xffff)
252b5132
RH
11815 as_bad_where (fixP->fx_file, fixP->fx_line,
11816 _("relocation overflow"));
252b5132
RH
11817 if (target_big_endian)
11818 buf += 2;
2132e3a3 11819 md_number_to_chars ((char *) buf, *valP, 2);
252b5132
RH
11820 }
11821 break;
11822
11823 case BFD_RELOC_16_PCREL_S2:
a7ebbfdf 11824 if ((*valP & 0x3) != 0)
cb56d3d3 11825 as_bad_where (fixP->fx_file, fixP->fx_line,
bad36eac 11826 _("Branch to misaligned address (%lx)"), (long) *valP);
cb56d3d3 11827
252b5132
RH
11828 /*
11829 * We need to save the bits in the instruction since fixup_segment()
11830 * might be deleting the relocation entry (i.e., a branch within
11831 * the current segment).
11832 */
a7ebbfdf 11833 if (! fixP->fx_done)
bb2d6cd7 11834 break;
252b5132
RH
11835
11836 /* update old instruction data */
252b5132
RH
11837 if (target_big_endian)
11838 insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
11839 else
11840 insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
11841
a7ebbfdf
TS
11842 if (*valP + 0x20000 <= 0x3ffff)
11843 {
11844 insn |= (*valP >> 2) & 0xffff;
2132e3a3 11845 md_number_to_chars ((char *) buf, insn, 4);
a7ebbfdf
TS
11846 }
11847 else if (mips_pic == NO_PIC
11848 && fixP->fx_done
11849 && fixP->fx_frag->fr_address >= text_section->vma
11850 && (fixP->fx_frag->fr_address
587aac4e 11851 < text_section->vma + bfd_get_section_size (text_section))
a7ebbfdf
TS
11852 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
11853 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
11854 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
252b5132
RH
11855 {
11856 /* The branch offset is too large. If this is an
11857 unconditional branch, and we are not generating PIC code,
11858 we can convert it to an absolute jump instruction. */
a7ebbfdf
TS
11859 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
11860 insn = 0x0c000000; /* jal */
252b5132 11861 else
a7ebbfdf
TS
11862 insn = 0x08000000; /* j */
11863 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
11864 fixP->fx_done = 0;
11865 fixP->fx_addsy = section_symbol (text_section);
11866 *valP += md_pcrel_from (fixP);
2132e3a3 11867 md_number_to_chars ((char *) buf, insn, 4);
a7ebbfdf
TS
11868 }
11869 else
11870 {
11871 /* If we got here, we have branch-relaxation disabled,
11872 and there's nothing we can do to fix this instruction
11873 without turning it into a longer sequence. */
11874 as_bad_where (fixP->fx_file, fixP->fx_line,
11875 _("Branch out of range"));
252b5132 11876 }
252b5132
RH
11877 break;
11878
11879 case BFD_RELOC_VTABLE_INHERIT:
11880 fixP->fx_done = 0;
11881 if (fixP->fx_addsy
11882 && !S_IS_DEFINED (fixP->fx_addsy)
11883 && !S_IS_WEAK (fixP->fx_addsy))
11884 S_SET_WEAK (fixP->fx_addsy);
11885 break;
11886
11887 case BFD_RELOC_VTABLE_ENTRY:
11888 fixP->fx_done = 0;
11889 break;
11890
11891 default:
11892 internalError ();
11893 }
a7ebbfdf
TS
11894
11895 /* Remember value for tc_gen_reloc. */
11896 fixP->fx_addnumber = *valP;
252b5132
RH
11897}
11898
252b5132 11899static symbolS *
17a2f251 11900get_symbol (void)
252b5132
RH
11901{
11902 int c;
11903 char *name;
11904 symbolS *p;
11905
11906 name = input_line_pointer;
11907 c = get_symbol_end ();
11908 p = (symbolS *) symbol_find_or_make (name);
11909 *input_line_pointer = c;
11910 return p;
11911}
11912
11913/* Align the current frag to a given power of two. The MIPS assembler
11914 also automatically adjusts any preceding label. */
11915
11916static void
17a2f251 11917mips_align (int to, int fill, symbolS *label)
252b5132 11918{
7d10b47d 11919 mips_emit_delays ();
252b5132
RH
11920 frag_align (to, fill, 0);
11921 record_alignment (now_seg, to);
11922 if (label != NULL)
11923 {
11924 assert (S_GET_SEGMENT (label) == now_seg);
49309057 11925 symbol_set_frag (label, frag_now);
252b5132
RH
11926 S_SET_VALUE (label, (valueT) frag_now_fix ());
11927 }
11928}
11929
11930/* Align to a given power of two. .align 0 turns off the automatic
11931 alignment used by the data creating pseudo-ops. */
11932
11933static void
17a2f251 11934s_align (int x ATTRIBUTE_UNUSED)
252b5132 11935{
3994f87e
TS
11936 int temp;
11937 long temp_fill;
252b5132
RH
11938 long max_alignment = 15;
11939
11940 /*
11941
67c1ffbe 11942 o Note that the assembler pulls down any immediately preceding label
252b5132
RH
11943 to the aligned address.
11944 o It's not documented but auto alignment is reinstated by
11945 a .align pseudo instruction.
11946 o Note also that after auto alignment is turned off the mips assembler
11947 issues an error on attempt to assemble an improperly aligned data item.
11948 We don't.
11949
11950 */
11951
11952 temp = get_absolute_expression ();
11953 if (temp > max_alignment)
11954 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
11955 else if (temp < 0)
11956 {
11957 as_warn (_("Alignment negative: 0 assumed."));
11958 temp = 0;
11959 }
11960 if (*input_line_pointer == ',')
11961 {
f9419b05 11962 ++input_line_pointer;
252b5132
RH
11963 temp_fill = get_absolute_expression ();
11964 }
11965 else
11966 temp_fill = 0;
11967 if (temp)
11968 {
11969 auto_align = 1;
11970 mips_align (temp, (int) temp_fill,
11971 insn_labels != NULL ? insn_labels->label : NULL);
11972 }
11973 else
11974 {
11975 auto_align = 0;
11976 }
11977
11978 demand_empty_rest_of_line ();
11979}
11980
252b5132 11981static void
17a2f251 11982s_change_sec (int sec)
252b5132
RH
11983{
11984 segT seg;
11985
252b5132
RH
11986#ifdef OBJ_ELF
11987 /* The ELF backend needs to know that we are changing sections, so
11988 that .previous works correctly. We could do something like check
b6ff326e 11989 for an obj_section_change_hook macro, but that might be confusing
252b5132
RH
11990 as it would not be appropriate to use it in the section changing
11991 functions in read.c, since obj-elf.c intercepts those. FIXME:
11992 This should be cleaner, somehow. */
11993 obj_elf_section_change_hook ();
11994#endif
11995
7d10b47d 11996 mips_emit_delays ();
252b5132
RH
11997 switch (sec)
11998 {
11999 case 't':
12000 s_text (0);
12001 break;
12002 case 'd':
12003 s_data (0);
12004 break;
12005 case 'b':
12006 subseg_set (bss_section, (subsegT) get_absolute_expression ());
12007 demand_empty_rest_of_line ();
12008 break;
12009
12010 case 'r':
4d0d148d
TS
12011 seg = subseg_new (RDATA_SECTION_NAME,
12012 (subsegT) get_absolute_expression ());
12013 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
252b5132 12014 {
4d0d148d
TS
12015 bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
12016 | SEC_READONLY | SEC_RELOC
12017 | SEC_DATA));
12018 if (strcmp (TARGET_OS, "elf") != 0)
12019 record_alignment (seg, 4);
252b5132 12020 }
4d0d148d 12021 demand_empty_rest_of_line ();
252b5132
RH
12022 break;
12023
12024 case 's':
4d0d148d
TS
12025 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
12026 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
252b5132 12027 {
4d0d148d
TS
12028 bfd_set_section_flags (stdoutput, seg,
12029 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
12030 if (strcmp (TARGET_OS, "elf") != 0)
12031 record_alignment (seg, 4);
252b5132 12032 }
4d0d148d
TS
12033 demand_empty_rest_of_line ();
12034 break;
252b5132
RH
12035 }
12036
12037 auto_align = 1;
12038}
b34976b6 12039
cca86cc8 12040void
17a2f251 12041s_change_section (int ignore ATTRIBUTE_UNUSED)
cca86cc8 12042{
7ed4a06a 12043#ifdef OBJ_ELF
cca86cc8
SC
12044 char *section_name;
12045 char c;
684022ea 12046 char next_c = 0;
cca86cc8
SC
12047 int section_type;
12048 int section_flag;
12049 int section_entry_size;
12050 int section_alignment;
b34976b6 12051
7ed4a06a
TS
12052 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
12053 return;
12054
cca86cc8
SC
12055 section_name = input_line_pointer;
12056 c = get_symbol_end ();
a816d1ed
AO
12057 if (c)
12058 next_c = *(input_line_pointer + 1);
cca86cc8 12059
4cf0dd0d
TS
12060 /* Do we have .section Name<,"flags">? */
12061 if (c != ',' || (c == ',' && next_c == '"'))
cca86cc8 12062 {
4cf0dd0d
TS
12063 /* just after name is now '\0'. */
12064 *input_line_pointer = c;
cca86cc8
SC
12065 input_line_pointer = section_name;
12066 obj_elf_section (ignore);
12067 return;
12068 }
12069 input_line_pointer++;
12070
12071 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
12072 if (c == ',')
12073 section_type = get_absolute_expression ();
12074 else
12075 section_type = 0;
12076 if (*input_line_pointer++ == ',')
12077 section_flag = get_absolute_expression ();
12078 else
12079 section_flag = 0;
12080 if (*input_line_pointer++ == ',')
12081 section_entry_size = get_absolute_expression ();
12082 else
12083 section_entry_size = 0;
12084 if (*input_line_pointer++ == ',')
12085 section_alignment = get_absolute_expression ();
12086 else
12087 section_alignment = 0;
12088
a816d1ed
AO
12089 section_name = xstrdup (section_name);
12090
8ab8a5c8
RS
12091 /* When using the generic form of .section (as implemented by obj-elf.c),
12092 there's no way to set the section type to SHT_MIPS_DWARF. Users have
12093 traditionally had to fall back on the more common @progbits instead.
12094
12095 There's nothing really harmful in this, since bfd will correct
12096 SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file. But it
708587a4 12097 means that, for backwards compatibility, the special_section entries
8ab8a5c8
RS
12098 for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
12099
12100 Even so, we shouldn't force users of the MIPS .section syntax to
12101 incorrectly label the sections as SHT_PROGBITS. The best compromise
12102 seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
12103 generic type-checking code. */
12104 if (section_type == SHT_MIPS_DWARF)
12105 section_type = SHT_PROGBITS;
12106
cca86cc8
SC
12107 obj_elf_change_section (section_name, section_type, section_flag,
12108 section_entry_size, 0, 0, 0);
a816d1ed
AO
12109
12110 if (now_seg->name != section_name)
12111 free (section_name);
7ed4a06a 12112#endif /* OBJ_ELF */
cca86cc8 12113}
252b5132
RH
12114
12115void
17a2f251 12116mips_enable_auto_align (void)
252b5132
RH
12117{
12118 auto_align = 1;
12119}
12120
12121static void
17a2f251 12122s_cons (int log_size)
252b5132
RH
12123{
12124 symbolS *label;
12125
12126 label = insn_labels != NULL ? insn_labels->label : NULL;
7d10b47d 12127 mips_emit_delays ();
252b5132
RH
12128 if (log_size > 0 && auto_align)
12129 mips_align (log_size, 0, label);
12130 mips_clear_insn_labels ();
12131 cons (1 << log_size);
12132}
12133
12134static void
17a2f251 12135s_float_cons (int type)
252b5132
RH
12136{
12137 symbolS *label;
12138
12139 label = insn_labels != NULL ? insn_labels->label : NULL;
12140
7d10b47d 12141 mips_emit_delays ();
252b5132
RH
12142
12143 if (auto_align)
49309057
ILT
12144 {
12145 if (type == 'd')
12146 mips_align (3, 0, label);
12147 else
12148 mips_align (2, 0, label);
12149 }
252b5132
RH
12150
12151 mips_clear_insn_labels ();
12152
12153 float_cons (type);
12154}
12155
12156/* Handle .globl. We need to override it because on Irix 5 you are
12157 permitted to say
12158 .globl foo .text
12159 where foo is an undefined symbol, to mean that foo should be
12160 considered to be the address of a function. */
12161
12162static void
17a2f251 12163s_mips_globl (int x ATTRIBUTE_UNUSED)
252b5132
RH
12164{
12165 char *name;
12166 int c;
12167 symbolS *symbolP;
12168 flagword flag;
12169
8a06b769 12170 do
252b5132 12171 {
8a06b769 12172 name = input_line_pointer;
252b5132 12173 c = get_symbol_end ();
8a06b769
TS
12174 symbolP = symbol_find_or_make (name);
12175 S_SET_EXTERNAL (symbolP);
12176
252b5132 12177 *input_line_pointer = c;
8a06b769 12178 SKIP_WHITESPACE ();
252b5132 12179
8a06b769
TS
12180 /* On Irix 5, every global symbol that is not explicitly labelled as
12181 being a function is apparently labelled as being an object. */
12182 flag = BSF_OBJECT;
252b5132 12183
8a06b769
TS
12184 if (!is_end_of_line[(unsigned char) *input_line_pointer]
12185 && (*input_line_pointer != ','))
12186 {
12187 char *secname;
12188 asection *sec;
12189
12190 secname = input_line_pointer;
12191 c = get_symbol_end ();
12192 sec = bfd_get_section_by_name (stdoutput, secname);
12193 if (sec == NULL)
12194 as_bad (_("%s: no such section"), secname);
12195 *input_line_pointer = c;
12196
12197 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
12198 flag = BSF_FUNCTION;
12199 }
12200
12201 symbol_get_bfdsym (symbolP)->flags |= flag;
12202
12203 c = *input_line_pointer;
12204 if (c == ',')
12205 {
12206 input_line_pointer++;
12207 SKIP_WHITESPACE ();
12208 if (is_end_of_line[(unsigned char) *input_line_pointer])
12209 c = '\n';
12210 }
12211 }
12212 while (c == ',');
252b5132 12213
252b5132
RH
12214 demand_empty_rest_of_line ();
12215}
12216
12217static void
17a2f251 12218s_option (int x ATTRIBUTE_UNUSED)
252b5132
RH
12219{
12220 char *opt;
12221 char c;
12222
12223 opt = input_line_pointer;
12224 c = get_symbol_end ();
12225
12226 if (*opt == 'O')
12227 {
12228 /* FIXME: What does this mean? */
12229 }
12230 else if (strncmp (opt, "pic", 3) == 0)
12231 {
12232 int i;
12233
12234 i = atoi (opt + 3);
12235 if (i == 0)
12236 mips_pic = NO_PIC;
12237 else if (i == 2)
143d77c5 12238 {
252b5132 12239 mips_pic = SVR4_PIC;
143d77c5
EC
12240 mips_abicalls = TRUE;
12241 }
252b5132
RH
12242 else
12243 as_bad (_(".option pic%d not supported"), i);
12244
4d0d148d 12245 if (mips_pic == SVR4_PIC)
252b5132
RH
12246 {
12247 if (g_switch_seen && g_switch_value != 0)
12248 as_warn (_("-G may not be used with SVR4 PIC code"));
12249 g_switch_value = 0;
12250 bfd_set_gp_size (stdoutput, 0);
12251 }
12252 }
12253 else
12254 as_warn (_("Unrecognized option \"%s\""), opt);
12255
12256 *input_line_pointer = c;
12257 demand_empty_rest_of_line ();
12258}
12259
12260/* This structure is used to hold a stack of .set values. */
12261
e972090a
NC
12262struct mips_option_stack
12263{
252b5132
RH
12264 struct mips_option_stack *next;
12265 struct mips_set_options options;
12266};
12267
12268static struct mips_option_stack *mips_opts_stack;
12269
12270/* Handle the .set pseudo-op. */
12271
12272static void
17a2f251 12273s_mipsset (int x ATTRIBUTE_UNUSED)
252b5132
RH
12274{
12275 char *name = input_line_pointer, ch;
12276
12277 while (!is_end_of_line[(unsigned char) *input_line_pointer])
f9419b05 12278 ++input_line_pointer;
252b5132
RH
12279 ch = *input_line_pointer;
12280 *input_line_pointer = '\0';
12281
12282 if (strcmp (name, "reorder") == 0)
12283 {
7d10b47d
RS
12284 if (mips_opts.noreorder)
12285 end_noreorder ();
252b5132
RH
12286 }
12287 else if (strcmp (name, "noreorder") == 0)
12288 {
7d10b47d
RS
12289 if (!mips_opts.noreorder)
12290 start_noreorder ();
252b5132
RH
12291 }
12292 else if (strcmp (name, "at") == 0)
12293 {
12294 mips_opts.noat = 0;
12295 }
12296 else if (strcmp (name, "noat") == 0)
12297 {
12298 mips_opts.noat = 1;
12299 }
12300 else if (strcmp (name, "macro") == 0)
12301 {
12302 mips_opts.warn_about_macros = 0;
12303 }
12304 else if (strcmp (name, "nomacro") == 0)
12305 {
12306 if (mips_opts.noreorder == 0)
12307 as_bad (_("`noreorder' must be set before `nomacro'"));
12308 mips_opts.warn_about_macros = 1;
12309 }
12310 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
12311 {
12312 mips_opts.nomove = 0;
12313 }
12314 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
12315 {
12316 mips_opts.nomove = 1;
12317 }
12318 else if (strcmp (name, "bopt") == 0)
12319 {
12320 mips_opts.nobopt = 0;
12321 }
12322 else if (strcmp (name, "nobopt") == 0)
12323 {
12324 mips_opts.nobopt = 1;
12325 }
ad3fea08
TS
12326 else if (strcmp (name, "gp=default") == 0)
12327 mips_opts.gp32 = file_mips_gp32;
12328 else if (strcmp (name, "gp=32") == 0)
12329 mips_opts.gp32 = 1;
12330 else if (strcmp (name, "gp=64") == 0)
12331 {
12332 if (!ISA_HAS_64BIT_REGS (mips_opts.isa))
12333 as_warn ("%s isa does not support 64-bit registers",
12334 mips_cpu_info_from_isa (mips_opts.isa)->name);
12335 mips_opts.gp32 = 0;
12336 }
12337 else if (strcmp (name, "fp=default") == 0)
12338 mips_opts.fp32 = file_mips_fp32;
12339 else if (strcmp (name, "fp=32") == 0)
12340 mips_opts.fp32 = 1;
12341 else if (strcmp (name, "fp=64") == 0)
12342 {
12343 if (!ISA_HAS_64BIT_FPRS (mips_opts.isa))
12344 as_warn ("%s isa does not support 64-bit floating point registers",
12345 mips_cpu_info_from_isa (mips_opts.isa)->name);
12346 mips_opts.fp32 = 0;
12347 }
252b5132
RH
12348 else if (strcmp (name, "mips16") == 0
12349 || strcmp (name, "MIPS-16") == 0)
12350 mips_opts.mips16 = 1;
12351 else if (strcmp (name, "nomips16") == 0
12352 || strcmp (name, "noMIPS-16") == 0)
12353 mips_opts.mips16 = 0;
e16bfa71
TS
12354 else if (strcmp (name, "smartmips") == 0)
12355 {
ad3fea08 12356 if (!ISA_SUPPORTS_SMARTMIPS)
e16bfa71
TS
12357 as_warn ("%s ISA does not support SmartMIPS ASE",
12358 mips_cpu_info_from_isa (mips_opts.isa)->name);
12359 mips_opts.ase_smartmips = 1;
12360 }
12361 else if (strcmp (name, "nosmartmips") == 0)
12362 mips_opts.ase_smartmips = 0;
1f25f5d3
CD
12363 else if (strcmp (name, "mips3d") == 0)
12364 mips_opts.ase_mips3d = 1;
12365 else if (strcmp (name, "nomips3d") == 0)
12366 mips_opts.ase_mips3d = 0;
a4672219
TS
12367 else if (strcmp (name, "mdmx") == 0)
12368 mips_opts.ase_mdmx = 1;
12369 else if (strcmp (name, "nomdmx") == 0)
12370 mips_opts.ase_mdmx = 0;
74cd071d 12371 else if (strcmp (name, "dsp") == 0)
ad3fea08
TS
12372 {
12373 if (!ISA_SUPPORTS_DSP_ASE)
12374 as_warn ("%s ISA does not support DSP ASE",
12375 mips_cpu_info_from_isa (mips_opts.isa)->name);
12376 mips_opts.ase_dsp = 1;
12377 }
74cd071d
CF
12378 else if (strcmp (name, "nodsp") == 0)
12379 mips_opts.ase_dsp = 0;
ef2e4d86 12380 else if (strcmp (name, "mt") == 0)
ad3fea08
TS
12381 {
12382 if (!ISA_SUPPORTS_MT_ASE)
12383 as_warn ("%s ISA does not support MT ASE",
12384 mips_cpu_info_from_isa (mips_opts.isa)->name);
12385 mips_opts.ase_mt = 1;
12386 }
ef2e4d86
CF
12387 else if (strcmp (name, "nomt") == 0)
12388 mips_opts.ase_mt = 0;
1a2c1fad 12389 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
252b5132 12390 {
af7ee8bf 12391 int reset = 0;
252b5132 12392
1a2c1fad
CD
12393 /* Permit the user to change the ISA and architecture on the fly.
12394 Needless to say, misuse can cause serious problems. */
81a21e38 12395 if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
af7ee8bf
CD
12396 {
12397 reset = 1;
12398 mips_opts.isa = file_mips_isa;
1a2c1fad 12399 mips_opts.arch = file_mips_arch;
1a2c1fad
CD
12400 }
12401 else if (strncmp (name, "arch=", 5) == 0)
12402 {
12403 const struct mips_cpu_info *p;
12404
12405 p = mips_parse_cpu("internal use", name + 5);
12406 if (!p)
12407 as_bad (_("unknown architecture %s"), name + 5);
12408 else
12409 {
12410 mips_opts.arch = p->cpu;
12411 mips_opts.isa = p->isa;
12412 }
12413 }
81a21e38
TS
12414 else if (strncmp (name, "mips", 4) == 0)
12415 {
12416 const struct mips_cpu_info *p;
12417
12418 p = mips_parse_cpu("internal use", name);
12419 if (!p)
12420 as_bad (_("unknown ISA level %s"), name + 4);
12421 else
12422 {
12423 mips_opts.arch = p->cpu;
12424 mips_opts.isa = p->isa;
12425 }
12426 }
af7ee8bf 12427 else
81a21e38 12428 as_bad (_("unknown ISA or architecture %s"), name);
af7ee8bf
CD
12429
12430 switch (mips_opts.isa)
98d3f06f
KH
12431 {
12432 case 0:
98d3f06f 12433 break;
af7ee8bf
CD
12434 case ISA_MIPS1:
12435 case ISA_MIPS2:
12436 case ISA_MIPS32:
12437 case ISA_MIPS32R2:
98d3f06f
KH
12438 mips_opts.gp32 = 1;
12439 mips_opts.fp32 = 1;
12440 break;
af7ee8bf
CD
12441 case ISA_MIPS3:
12442 case ISA_MIPS4:
12443 case ISA_MIPS5:
12444 case ISA_MIPS64:
5f74bc13 12445 case ISA_MIPS64R2:
98d3f06f
KH
12446 mips_opts.gp32 = 0;
12447 mips_opts.fp32 = 0;
12448 break;
12449 default:
12450 as_bad (_("unknown ISA level %s"), name + 4);
12451 break;
12452 }
af7ee8bf 12453 if (reset)
98d3f06f 12454 {
af7ee8bf
CD
12455 mips_opts.gp32 = file_mips_gp32;
12456 mips_opts.fp32 = file_mips_fp32;
98d3f06f 12457 }
252b5132
RH
12458 }
12459 else if (strcmp (name, "autoextend") == 0)
12460 mips_opts.noautoextend = 0;
12461 else if (strcmp (name, "noautoextend") == 0)
12462 mips_opts.noautoextend = 1;
12463 else if (strcmp (name, "push") == 0)
12464 {
12465 struct mips_option_stack *s;
12466
12467 s = (struct mips_option_stack *) xmalloc (sizeof *s);
12468 s->next = mips_opts_stack;
12469 s->options = mips_opts;
12470 mips_opts_stack = s;
12471 }
12472 else if (strcmp (name, "pop") == 0)
12473 {
12474 struct mips_option_stack *s;
12475
12476 s = mips_opts_stack;
12477 if (s == NULL)
12478 as_bad (_(".set pop with no .set push"));
12479 else
12480 {
12481 /* If we're changing the reorder mode we need to handle
12482 delay slots correctly. */
12483 if (s->options.noreorder && ! mips_opts.noreorder)
7d10b47d 12484 start_noreorder ();
252b5132 12485 else if (! s->options.noreorder && mips_opts.noreorder)
7d10b47d 12486 end_noreorder ();
252b5132
RH
12487
12488 mips_opts = s->options;
12489 mips_opts_stack = s->next;
12490 free (s);
12491 }
12492 }
aed1a261
RS
12493 else if (strcmp (name, "sym32") == 0)
12494 mips_opts.sym32 = TRUE;
12495 else if (strcmp (name, "nosym32") == 0)
12496 mips_opts.sym32 = FALSE;
252b5132
RH
12497 else
12498 {
12499 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
12500 }
12501 *input_line_pointer = ch;
12502 demand_empty_rest_of_line ();
12503}
12504
12505/* Handle the .abicalls pseudo-op. I believe this is equivalent to
12506 .option pic2. It means to generate SVR4 PIC calls. */
12507
12508static void
17a2f251 12509s_abicalls (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
12510{
12511 mips_pic = SVR4_PIC;
143d77c5 12512 mips_abicalls = TRUE;
4d0d148d
TS
12513
12514 if (g_switch_seen && g_switch_value != 0)
12515 as_warn (_("-G may not be used with SVR4 PIC code"));
12516 g_switch_value = 0;
12517
252b5132
RH
12518 bfd_set_gp_size (stdoutput, 0);
12519 demand_empty_rest_of_line ();
12520}
12521
12522/* Handle the .cpload pseudo-op. This is used when generating SVR4
12523 PIC code. It sets the $gp register for the function based on the
12524 function address, which is in the register named in the argument.
12525 This uses a relocation against _gp_disp, which is handled specially
12526 by the linker. The result is:
12527 lui $gp,%hi(_gp_disp)
12528 addiu $gp,$gp,%lo(_gp_disp)
12529 addu $gp,$gp,.cpload argument
aa6975fb
ILT
12530 The .cpload argument is normally $25 == $t9.
12531
12532 The -mno-shared option changes this to:
bbe506e8
TS
12533 lui $gp,%hi(__gnu_local_gp)
12534 addiu $gp,$gp,%lo(__gnu_local_gp)
aa6975fb
ILT
12535 and the argument is ignored. This saves an instruction, but the
12536 resulting code is not position independent; it uses an absolute
bbe506e8
TS
12537 address for __gnu_local_gp. Thus code assembled with -mno-shared
12538 can go into an ordinary executable, but not into a shared library. */
252b5132
RH
12539
12540static void
17a2f251 12541s_cpload (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
12542{
12543 expressionS ex;
aa6975fb
ILT
12544 int reg;
12545 int in_shared;
252b5132 12546
6478892d
TS
12547 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12548 .cpload is ignored. */
12549 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
12550 {
12551 s_ignore (0);
12552 return;
12553 }
12554
d3ecfc59 12555 /* .cpload should be in a .set noreorder section. */
252b5132
RH
12556 if (mips_opts.noreorder == 0)
12557 as_warn (_(".cpload not in noreorder section"));
12558
aa6975fb
ILT
12559 reg = tc_get_register (0);
12560
12561 /* If we need to produce a 64-bit address, we are better off using
12562 the default instruction sequence. */
aed1a261 12563 in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
aa6975fb 12564
252b5132 12565 ex.X_op = O_symbol;
bbe506e8
TS
12566 ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
12567 "__gnu_local_gp");
252b5132
RH
12568 ex.X_op_symbol = NULL;
12569 ex.X_add_number = 0;
12570
12571 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
49309057 12572 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
252b5132 12573
584892a6 12574 macro_start ();
67c0d1eb
RS
12575 macro_build_lui (&ex, mips_gp_register);
12576 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
17a2f251 12577 mips_gp_register, BFD_RELOC_LO16);
aa6975fb
ILT
12578 if (in_shared)
12579 macro_build (NULL, "addu", "d,v,t", mips_gp_register,
12580 mips_gp_register, reg);
584892a6 12581 macro_end ();
252b5132
RH
12582
12583 demand_empty_rest_of_line ();
12584}
12585
6478892d
TS
12586/* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
12587 .cpsetup $reg1, offset|$reg2, label
12588
12589 If offset is given, this results in:
12590 sd $gp, offset($sp)
956cd1d6 12591 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
12592 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
12593 daddu $gp, $gp, $reg1
6478892d
TS
12594
12595 If $reg2 is given, this results in:
12596 daddu $reg2, $gp, $0
956cd1d6 12597 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
12598 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
12599 daddu $gp, $gp, $reg1
aa6975fb
ILT
12600 $reg1 is normally $25 == $t9.
12601
12602 The -mno-shared option replaces the last three instructions with
12603 lui $gp,%hi(_gp)
12604 addiu $gp,$gp,%lo(_gp)
12605 */
12606
6478892d 12607static void
17a2f251 12608s_cpsetup (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
12609{
12610 expressionS ex_off;
12611 expressionS ex_sym;
12612 int reg1;
6478892d 12613
8586fc66 12614 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
6478892d
TS
12615 We also need NewABI support. */
12616 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12617 {
12618 s_ignore (0);
12619 return;
12620 }
12621
12622 reg1 = tc_get_register (0);
12623 SKIP_WHITESPACE ();
12624 if (*input_line_pointer != ',')
12625 {
12626 as_bad (_("missing argument separator ',' for .cpsetup"));
12627 return;
12628 }
12629 else
80245285 12630 ++input_line_pointer;
6478892d
TS
12631 SKIP_WHITESPACE ();
12632 if (*input_line_pointer == '$')
80245285
TS
12633 {
12634 mips_cpreturn_register = tc_get_register (0);
12635 mips_cpreturn_offset = -1;
12636 }
6478892d 12637 else
80245285
TS
12638 {
12639 mips_cpreturn_offset = get_absolute_expression ();
12640 mips_cpreturn_register = -1;
12641 }
6478892d
TS
12642 SKIP_WHITESPACE ();
12643 if (*input_line_pointer != ',')
12644 {
12645 as_bad (_("missing argument separator ',' for .cpsetup"));
12646 return;
12647 }
12648 else
f9419b05 12649 ++input_line_pointer;
6478892d 12650 SKIP_WHITESPACE ();
f21f8242 12651 expression (&ex_sym);
6478892d 12652
584892a6 12653 macro_start ();
6478892d
TS
12654 if (mips_cpreturn_register == -1)
12655 {
12656 ex_off.X_op = O_constant;
12657 ex_off.X_add_symbol = NULL;
12658 ex_off.X_op_symbol = NULL;
12659 ex_off.X_add_number = mips_cpreturn_offset;
12660
67c0d1eb 12661 macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
17a2f251 12662 BFD_RELOC_LO16, SP);
6478892d
TS
12663 }
12664 else
67c0d1eb 12665 macro_build (NULL, "daddu", "d,v,t", mips_cpreturn_register,
17a2f251 12666 mips_gp_register, 0);
6478892d 12667
aed1a261 12668 if (mips_in_shared || HAVE_64BIT_SYMBOLS)
aa6975fb
ILT
12669 {
12670 macro_build (&ex_sym, "lui", "t,u", mips_gp_register,
12671 -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
12672 BFD_RELOC_HI16_S);
12673
12674 macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
12675 mips_gp_register, -1, BFD_RELOC_GPREL16,
12676 BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
12677
12678 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
12679 mips_gp_register, reg1);
12680 }
12681 else
12682 {
12683 expressionS ex;
12684
12685 ex.X_op = O_symbol;
4184909a 12686 ex.X_add_symbol = symbol_find_or_make ("__gnu_local_gp");
aa6975fb
ILT
12687 ex.X_op_symbol = NULL;
12688 ex.X_add_number = 0;
6e1304d8 12689
aa6975fb
ILT
12690 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
12691 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
12692
12693 macro_build_lui (&ex, mips_gp_register);
12694 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
12695 mips_gp_register, BFD_RELOC_LO16);
12696 }
f21f8242 12697
584892a6 12698 macro_end ();
6478892d
TS
12699
12700 demand_empty_rest_of_line ();
12701}
12702
12703static void
17a2f251 12704s_cplocal (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
12705{
12706 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
12707 .cplocal is ignored. */
12708 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12709 {
12710 s_ignore (0);
12711 return;
12712 }
12713
12714 mips_gp_register = tc_get_register (0);
85b51719 12715 demand_empty_rest_of_line ();
6478892d
TS
12716}
12717
252b5132
RH
12718/* Handle the .cprestore pseudo-op. This stores $gp into a given
12719 offset from $sp. The offset is remembered, and after making a PIC
12720 call $gp is restored from that location. */
12721
12722static void
17a2f251 12723s_cprestore (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
12724{
12725 expressionS ex;
252b5132 12726
6478892d 12727 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
c9914766 12728 .cprestore is ignored. */
6478892d 12729 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
12730 {
12731 s_ignore (0);
12732 return;
12733 }
12734
12735 mips_cprestore_offset = get_absolute_expression ();
7a621144 12736 mips_cprestore_valid = 1;
252b5132
RH
12737
12738 ex.X_op = O_constant;
12739 ex.X_add_symbol = NULL;
12740 ex.X_op_symbol = NULL;
12741 ex.X_add_number = mips_cprestore_offset;
12742
584892a6 12743 macro_start ();
67c0d1eb
RS
12744 macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
12745 SP, HAVE_64BIT_ADDRESSES);
584892a6 12746 macro_end ();
252b5132
RH
12747
12748 demand_empty_rest_of_line ();
12749}
12750
6478892d 12751/* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
67c1ffbe 12752 was given in the preceding .cpsetup, it results in:
6478892d 12753 ld $gp, offset($sp)
76b3015f 12754
6478892d 12755 If a register $reg2 was given there, it results in:
609f23f4 12756 daddu $gp, $reg2, $0
6478892d
TS
12757 */
12758static void
17a2f251 12759s_cpreturn (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
12760{
12761 expressionS ex;
6478892d
TS
12762
12763 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
12764 We also need NewABI support. */
12765 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12766 {
12767 s_ignore (0);
12768 return;
12769 }
12770
584892a6 12771 macro_start ();
6478892d
TS
12772 if (mips_cpreturn_register == -1)
12773 {
12774 ex.X_op = O_constant;
12775 ex.X_add_symbol = NULL;
12776 ex.X_op_symbol = NULL;
12777 ex.X_add_number = mips_cpreturn_offset;
12778
67c0d1eb 12779 macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
6478892d
TS
12780 }
12781 else
67c0d1eb 12782 macro_build (NULL, "daddu", "d,v,t", mips_gp_register,
17a2f251 12783 mips_cpreturn_register, 0);
584892a6 12784 macro_end ();
6478892d
TS
12785
12786 demand_empty_rest_of_line ();
12787}
12788
12789/* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
12790 code. It sets the offset to use in gp_rel relocations. */
12791
12792static void
17a2f251 12793s_gpvalue (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
12794{
12795 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
12796 We also need NewABI support. */
12797 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12798 {
12799 s_ignore (0);
12800 return;
12801 }
12802
def2e0dd 12803 mips_gprel_offset = get_absolute_expression ();
6478892d
TS
12804
12805 demand_empty_rest_of_line ();
12806}
12807
252b5132
RH
12808/* Handle the .gpword pseudo-op. This is used when generating PIC
12809 code. It generates a 32 bit GP relative reloc. */
12810
12811static void
17a2f251 12812s_gpword (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
12813{
12814 symbolS *label;
12815 expressionS ex;
12816 char *p;
12817
12818 /* When not generating PIC code, this is treated as .word. */
12819 if (mips_pic != SVR4_PIC)
12820 {
12821 s_cons (2);
12822 return;
12823 }
12824
12825 label = insn_labels != NULL ? insn_labels->label : NULL;
7d10b47d 12826 mips_emit_delays ();
252b5132
RH
12827 if (auto_align)
12828 mips_align (2, 0, label);
12829 mips_clear_insn_labels ();
12830
12831 expression (&ex);
12832
12833 if (ex.X_op != O_symbol || ex.X_add_number != 0)
12834 {
12835 as_bad (_("Unsupported use of .gpword"));
12836 ignore_rest_of_line ();
12837 }
12838
12839 p = frag_more (4);
17a2f251 12840 md_number_to_chars (p, 0, 4);
b34976b6 12841 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
cdf6fd85 12842 BFD_RELOC_GPREL32);
252b5132
RH
12843
12844 demand_empty_rest_of_line ();
12845}
12846
10181a0d 12847static void
17a2f251 12848s_gpdword (int ignore ATTRIBUTE_UNUSED)
10181a0d
AO
12849{
12850 symbolS *label;
12851 expressionS ex;
12852 char *p;
12853
12854 /* When not generating PIC code, this is treated as .dword. */
12855 if (mips_pic != SVR4_PIC)
12856 {
12857 s_cons (3);
12858 return;
12859 }
12860
12861 label = insn_labels != NULL ? insn_labels->label : NULL;
7d10b47d 12862 mips_emit_delays ();
10181a0d
AO
12863 if (auto_align)
12864 mips_align (3, 0, label);
12865 mips_clear_insn_labels ();
12866
12867 expression (&ex);
12868
12869 if (ex.X_op != O_symbol || ex.X_add_number != 0)
12870 {
12871 as_bad (_("Unsupported use of .gpdword"));
12872 ignore_rest_of_line ();
12873 }
12874
12875 p = frag_more (8);
17a2f251 12876 md_number_to_chars (p, 0, 8);
a105a300 12877 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
6e1304d8 12878 BFD_RELOC_GPREL32)->fx_tcbit = 1;
10181a0d
AO
12879
12880 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
6e1304d8
RS
12881 fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
12882 FALSE, BFD_RELOC_64)->fx_tcbit = 1;
10181a0d
AO
12883
12884 demand_empty_rest_of_line ();
12885}
12886
252b5132
RH
12887/* Handle the .cpadd pseudo-op. This is used when dealing with switch
12888 tables in SVR4 PIC code. */
12889
12890static void
17a2f251 12891s_cpadd (int ignore ATTRIBUTE_UNUSED)
252b5132 12892{
252b5132
RH
12893 int reg;
12894
10181a0d
AO
12895 /* This is ignored when not generating SVR4 PIC code. */
12896 if (mips_pic != SVR4_PIC)
252b5132
RH
12897 {
12898 s_ignore (0);
12899 return;
12900 }
12901
12902 /* Add $gp to the register named as an argument. */
584892a6 12903 macro_start ();
252b5132 12904 reg = tc_get_register (0);
67c0d1eb 12905 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
584892a6 12906 macro_end ();
252b5132 12907
bdaaa2e1 12908 demand_empty_rest_of_line ();
252b5132
RH
12909}
12910
12911/* Handle the .insn pseudo-op. This marks instruction labels in
12912 mips16 mode. This permits the linker to handle them specially,
12913 such as generating jalx instructions when needed. We also make
12914 them odd for the duration of the assembly, in order to generate the
12915 right sort of code. We will make them even in the adjust_symtab
12916 routine, while leaving them marked. This is convenient for the
12917 debugger and the disassembler. The linker knows to make them odd
12918 again. */
12919
12920static void
17a2f251 12921s_insn (int ignore ATTRIBUTE_UNUSED)
252b5132 12922{
f9419b05 12923 mips16_mark_labels ();
252b5132
RH
12924
12925 demand_empty_rest_of_line ();
12926}
12927
12928/* Handle a .stabn directive. We need these in order to mark a label
12929 as being a mips16 text label correctly. Sometimes the compiler
12930 will emit a label, followed by a .stabn, and then switch sections.
12931 If the label and .stabn are in mips16 mode, then the label is
12932 really a mips16 text label. */
12933
12934static void
17a2f251 12935s_mips_stab (int type)
252b5132 12936{
f9419b05 12937 if (type == 'n')
252b5132
RH
12938 mips16_mark_labels ();
12939
12940 s_stab (type);
12941}
12942
12943/* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
12944 */
12945
12946static void
17a2f251 12947s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
12948{
12949 char *name;
12950 int c;
12951 symbolS *symbolP;
12952 expressionS exp;
12953
12954 name = input_line_pointer;
12955 c = get_symbol_end ();
12956 symbolP = symbol_find_or_make (name);
12957 S_SET_WEAK (symbolP);
12958 *input_line_pointer = c;
12959
12960 SKIP_WHITESPACE ();
12961
12962 if (! is_end_of_line[(unsigned char) *input_line_pointer])
12963 {
12964 if (S_IS_DEFINED (symbolP))
12965 {
956cd1d6 12966 as_bad ("ignoring attempt to redefine symbol %s",
252b5132
RH
12967 S_GET_NAME (symbolP));
12968 ignore_rest_of_line ();
12969 return;
12970 }
bdaaa2e1 12971
252b5132
RH
12972 if (*input_line_pointer == ',')
12973 {
12974 ++input_line_pointer;
12975 SKIP_WHITESPACE ();
12976 }
bdaaa2e1 12977
252b5132
RH
12978 expression (&exp);
12979 if (exp.X_op != O_symbol)
12980 {
12981 as_bad ("bad .weakext directive");
98d3f06f 12982 ignore_rest_of_line ();
252b5132
RH
12983 return;
12984 }
49309057 12985 symbol_set_value_expression (symbolP, &exp);
252b5132
RH
12986 }
12987
12988 demand_empty_rest_of_line ();
12989}
12990
12991/* Parse a register string into a number. Called from the ECOFF code
12992 to parse .frame. The argument is non-zero if this is the frame
12993 register, so that we can record it in mips_frame_reg. */
12994
12995int
17a2f251 12996tc_get_register (int frame)
252b5132 12997{
707bfff6 12998 unsigned int reg;
252b5132
RH
12999
13000 SKIP_WHITESPACE ();
707bfff6
TS
13001 if (! reg_lookup (&input_line_pointer, RWARN | RTYPE_NUM | RTYPE_GP, &reg))
13002 reg = 0;
252b5132 13003 if (frame)
7a621144
DJ
13004 {
13005 mips_frame_reg = reg != 0 ? reg : SP;
13006 mips_frame_reg_valid = 1;
13007 mips_cprestore_valid = 0;
13008 }
252b5132
RH
13009 return reg;
13010}
13011
13012valueT
17a2f251 13013md_section_align (asection *seg, valueT addr)
252b5132
RH
13014{
13015 int align = bfd_get_section_alignment (stdoutput, seg);
13016
13017#ifdef OBJ_ELF
13018 /* We don't need to align ELF sections to the full alignment.
13019 However, Irix 5 may prefer that we align them at least to a 16
13020 byte boundary. We don't bother to align the sections if we are
13021 targeted for an embedded system. */
13022 if (strcmp (TARGET_OS, "elf") == 0)
13023 return addr;
13024 if (align > 4)
13025 align = 4;
13026#endif
13027
13028 return ((addr + (1 << align) - 1) & (-1 << align));
13029}
13030
13031/* Utility routine, called from above as well. If called while the
13032 input file is still being read, it's only an approximation. (For
13033 example, a symbol may later become defined which appeared to be
13034 undefined earlier.) */
13035
13036static int
17a2f251 13037nopic_need_relax (symbolS *sym, int before_relaxing)
252b5132
RH
13038{
13039 if (sym == 0)
13040 return 0;
13041
4d0d148d 13042 if (g_switch_value > 0)
252b5132
RH
13043 {
13044 const char *symname;
13045 int change;
13046
c9914766 13047 /* Find out whether this symbol can be referenced off the $gp
252b5132
RH
13048 register. It can be if it is smaller than the -G size or if
13049 it is in the .sdata or .sbss section. Certain symbols can
c9914766 13050 not be referenced off the $gp, although it appears as though
252b5132
RH
13051 they can. */
13052 symname = S_GET_NAME (sym);
13053 if (symname != (const char *) NULL
13054 && (strcmp (symname, "eprol") == 0
13055 || strcmp (symname, "etext") == 0
13056 || strcmp (symname, "_gp") == 0
13057 || strcmp (symname, "edata") == 0
13058 || strcmp (symname, "_fbss") == 0
13059 || strcmp (symname, "_fdata") == 0
13060 || strcmp (symname, "_ftext") == 0
13061 || strcmp (symname, "end") == 0
13062 || strcmp (symname, "_gp_disp") == 0))
13063 change = 1;
13064 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
13065 && (0
13066#ifndef NO_ECOFF_DEBUGGING
49309057
ILT
13067 || (symbol_get_obj (sym)->ecoff_extern_size != 0
13068 && (symbol_get_obj (sym)->ecoff_extern_size
13069 <= g_switch_value))
252b5132
RH
13070#endif
13071 /* We must defer this decision until after the whole
13072 file has been read, since there might be a .extern
13073 after the first use of this symbol. */
13074 || (before_relaxing
13075#ifndef NO_ECOFF_DEBUGGING
49309057 13076 && symbol_get_obj (sym)->ecoff_extern_size == 0
252b5132
RH
13077#endif
13078 && S_GET_VALUE (sym) == 0)
13079 || (S_GET_VALUE (sym) != 0
13080 && S_GET_VALUE (sym) <= g_switch_value)))
13081 change = 0;
13082 else
13083 {
13084 const char *segname;
13085
13086 segname = segment_name (S_GET_SEGMENT (sym));
13087 assert (strcmp (segname, ".lit8") != 0
13088 && strcmp (segname, ".lit4") != 0);
13089 change = (strcmp (segname, ".sdata") != 0
fba2b7f9
GK
13090 && strcmp (segname, ".sbss") != 0
13091 && strncmp (segname, ".sdata.", 7) != 0
13092 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
252b5132
RH
13093 }
13094 return change;
13095 }
13096 else
c9914766 13097 /* We are not optimizing for the $gp register. */
252b5132
RH
13098 return 1;
13099}
13100
5919d012
RS
13101
13102/* Return true if the given symbol should be considered local for SVR4 PIC. */
13103
13104static bfd_boolean
17a2f251 13105pic_need_relax (symbolS *sym, asection *segtype)
5919d012
RS
13106{
13107 asection *symsec;
13108 bfd_boolean linkonce;
13109
13110 /* Handle the case of a symbol equated to another symbol. */
13111 while (symbol_equated_reloc_p (sym))
13112 {
13113 symbolS *n;
13114
13115 /* It's possible to get a loop here in a badly written
13116 program. */
13117 n = symbol_get_value_expression (sym)->X_add_symbol;
13118 if (n == sym)
13119 break;
13120 sym = n;
13121 }
13122
13123 symsec = S_GET_SEGMENT (sym);
13124
13125 /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
13126 linkonce = FALSE;
13127 if (symsec != segtype && ! S_IS_LOCAL (sym))
13128 {
13129 if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
13130 != 0)
13131 linkonce = TRUE;
13132
13133 /* The GNU toolchain uses an extension for ELF: a section
13134 beginning with the magic string .gnu.linkonce is a linkonce
13135 section. */
13136 if (strncmp (segment_name (symsec), ".gnu.linkonce",
13137 sizeof ".gnu.linkonce" - 1) == 0)
13138 linkonce = TRUE;
13139 }
13140
13141 /* This must duplicate the test in adjust_reloc_syms. */
13142 return (symsec != &bfd_und_section
13143 && symsec != &bfd_abs_section
13144 && ! bfd_is_com_section (symsec)
13145 && !linkonce
13146#ifdef OBJ_ELF
13147 /* A global or weak symbol is treated as external. */
13148 && (OUTPUT_FLAVOR != bfd_target_elf_flavour
3e722fb5 13149 || (! S_IS_WEAK (sym) && ! S_IS_EXTERNAL (sym)))
5919d012
RS
13150#endif
13151 );
13152}
13153
13154
252b5132
RH
13155/* Given a mips16 variant frag FRAGP, return non-zero if it needs an
13156 extended opcode. SEC is the section the frag is in. */
13157
13158static int
17a2f251 13159mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
252b5132
RH
13160{
13161 int type;
3994f87e 13162 const struct mips16_immed_operand *op;
252b5132
RH
13163 offsetT val;
13164 int mintiny, maxtiny;
13165 segT symsec;
98aa84af 13166 fragS *sym_frag;
252b5132
RH
13167
13168 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
13169 return 0;
13170 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
13171 return 1;
13172
13173 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
13174 op = mips16_immed_operands;
13175 while (op->type != type)
13176 {
13177 ++op;
13178 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
13179 }
13180
13181 if (op->unsp)
13182 {
13183 if (type == '<' || type == '>' || type == '[' || type == ']')
13184 {
13185 mintiny = 1;
13186 maxtiny = 1 << op->nbits;
13187 }
13188 else
13189 {
13190 mintiny = 0;
13191 maxtiny = (1 << op->nbits) - 1;
13192 }
13193 }
13194 else
13195 {
13196 mintiny = - (1 << (op->nbits - 1));
13197 maxtiny = (1 << (op->nbits - 1)) - 1;
13198 }
13199
98aa84af 13200 sym_frag = symbol_get_frag (fragp->fr_symbol);
ac62c346 13201 val = S_GET_VALUE (fragp->fr_symbol);
98aa84af 13202 symsec = S_GET_SEGMENT (fragp->fr_symbol);
252b5132
RH
13203
13204 if (op->pcrel)
13205 {
13206 addressT addr;
13207
13208 /* We won't have the section when we are called from
13209 mips_relax_frag. However, we will always have been called
13210 from md_estimate_size_before_relax first. If this is a
13211 branch to a different section, we mark it as such. If SEC is
13212 NULL, and the frag is not marked, then it must be a branch to
13213 the same section. */
13214 if (sec == NULL)
13215 {
13216 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
13217 return 1;
13218 }
13219 else
13220 {
98aa84af 13221 /* Must have been called from md_estimate_size_before_relax. */
252b5132
RH
13222 if (symsec != sec)
13223 {
13224 fragp->fr_subtype =
13225 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13226
13227 /* FIXME: We should support this, and let the linker
13228 catch branches and loads that are out of range. */
13229 as_bad_where (fragp->fr_file, fragp->fr_line,
13230 _("unsupported PC relative reference to different section"));
13231
13232 return 1;
13233 }
98aa84af
AM
13234 if (fragp != sym_frag && sym_frag->fr_address == 0)
13235 /* Assume non-extended on the first relaxation pass.
13236 The address we have calculated will be bogus if this is
13237 a forward branch to another frag, as the forward frag
13238 will have fr_address == 0. */
13239 return 0;
252b5132
RH
13240 }
13241
13242 /* In this case, we know for sure that the symbol fragment is in
98aa84af
AM
13243 the same section. If the relax_marker of the symbol fragment
13244 differs from the relax_marker of this fragment, we have not
13245 yet adjusted the symbol fragment fr_address. We want to add
252b5132
RH
13246 in STRETCH in order to get a better estimate of the address.
13247 This particularly matters because of the shift bits. */
13248 if (stretch != 0
98aa84af 13249 && sym_frag->relax_marker != fragp->relax_marker)
252b5132
RH
13250 {
13251 fragS *f;
13252
13253 /* Adjust stretch for any alignment frag. Note that if have
13254 been expanding the earlier code, the symbol may be
13255 defined in what appears to be an earlier frag. FIXME:
13256 This doesn't handle the fr_subtype field, which specifies
13257 a maximum number of bytes to skip when doing an
13258 alignment. */
98aa84af 13259 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
252b5132
RH
13260 {
13261 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
13262 {
13263 if (stretch < 0)
13264 stretch = - ((- stretch)
13265 & ~ ((1 << (int) f->fr_offset) - 1));
13266 else
13267 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
13268 if (stretch == 0)
13269 break;
13270 }
13271 }
13272 if (f != NULL)
13273 val += stretch;
13274 }
13275
13276 addr = fragp->fr_address + fragp->fr_fix;
13277
13278 /* The base address rules are complicated. The base address of
13279 a branch is the following instruction. The base address of a
13280 PC relative load or add is the instruction itself, but if it
13281 is in a delay slot (in which case it can not be extended) use
13282 the address of the instruction whose delay slot it is in. */
13283 if (type == 'p' || type == 'q')
13284 {
13285 addr += 2;
13286
13287 /* If we are currently assuming that this frag should be
13288 extended, then, the current address is two bytes
bdaaa2e1 13289 higher. */
252b5132
RH
13290 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13291 addr += 2;
13292
13293 /* Ignore the low bit in the target, since it will be set
13294 for a text label. */
13295 if ((val & 1) != 0)
13296 --val;
13297 }
13298 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13299 addr -= 4;
13300 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13301 addr -= 2;
13302
13303 val -= addr & ~ ((1 << op->shift) - 1);
13304
13305 /* Branch offsets have an implicit 0 in the lowest bit. */
13306 if (type == 'p' || type == 'q')
13307 val /= 2;
13308
13309 /* If any of the shifted bits are set, we must use an extended
13310 opcode. If the address depends on the size of this
13311 instruction, this can lead to a loop, so we arrange to always
13312 use an extended opcode. We only check this when we are in
13313 the main relaxation loop, when SEC is NULL. */
13314 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
13315 {
13316 fragp->fr_subtype =
13317 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13318 return 1;
13319 }
13320
13321 /* If we are about to mark a frag as extended because the value
13322 is precisely maxtiny + 1, then there is a chance of an
13323 infinite loop as in the following code:
13324 la $4,foo
13325 .skip 1020
13326 .align 2
13327 foo:
13328 In this case when the la is extended, foo is 0x3fc bytes
13329 away, so the la can be shrunk, but then foo is 0x400 away, so
13330 the la must be extended. To avoid this loop, we mark the
13331 frag as extended if it was small, and is about to become
13332 extended with a value of maxtiny + 1. */
13333 if (val == ((maxtiny + 1) << op->shift)
13334 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
13335 && sec == NULL)
13336 {
13337 fragp->fr_subtype =
13338 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13339 return 1;
13340 }
13341 }
13342 else if (symsec != absolute_section && sec != NULL)
13343 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
13344
13345 if ((val & ((1 << op->shift) - 1)) != 0
13346 || val < (mintiny << op->shift)
13347 || val > (maxtiny << op->shift))
13348 return 1;
13349 else
13350 return 0;
13351}
13352
4a6a3df4
AO
13353/* Compute the length of a branch sequence, and adjust the
13354 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
13355 worst-case length is computed, with UPDATE being used to indicate
13356 whether an unconditional (-1), branch-likely (+1) or regular (0)
13357 branch is to be computed. */
13358static int
17a2f251 13359relaxed_branch_length (fragS *fragp, asection *sec, int update)
4a6a3df4 13360{
b34976b6 13361 bfd_boolean toofar;
4a6a3df4
AO
13362 int length;
13363
13364 if (fragp
13365 && S_IS_DEFINED (fragp->fr_symbol)
13366 && sec == S_GET_SEGMENT (fragp->fr_symbol))
13367 {
13368 addressT addr;
13369 offsetT val;
13370
13371 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
13372
13373 addr = fragp->fr_address + fragp->fr_fix + 4;
13374
13375 val -= addr;
13376
13377 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
13378 }
13379 else if (fragp)
13380 /* If the symbol is not defined or it's in a different segment,
13381 assume the user knows what's going on and emit a short
13382 branch. */
b34976b6 13383 toofar = FALSE;
4a6a3df4 13384 else
b34976b6 13385 toofar = TRUE;
4a6a3df4
AO
13386
13387 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13388 fragp->fr_subtype
af6ae2ad 13389 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_UNCOND (fragp->fr_subtype),
4a6a3df4
AO
13390 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
13391 RELAX_BRANCH_LINK (fragp->fr_subtype),
13392 toofar);
13393
13394 length = 4;
13395 if (toofar)
13396 {
13397 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
13398 length += 8;
13399
13400 if (mips_pic != NO_PIC)
13401 {
13402 /* Additional space for PIC loading of target address. */
13403 length += 8;
13404 if (mips_opts.isa == ISA_MIPS1)
13405 /* Additional space for $at-stabilizing nop. */
13406 length += 4;
13407 }
13408
13409 /* If branch is conditional. */
13410 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
13411 length += 8;
13412 }
b34976b6 13413
4a6a3df4
AO
13414 return length;
13415}
13416
252b5132
RH
13417/* Estimate the size of a frag before relaxing. Unless this is the
13418 mips16, we are not really relaxing here, and the final size is
13419 encoded in the subtype information. For the mips16, we have to
13420 decide whether we are using an extended opcode or not. */
13421
252b5132 13422int
17a2f251 13423md_estimate_size_before_relax (fragS *fragp, asection *segtype)
252b5132 13424{
5919d012 13425 int change;
252b5132 13426
4a6a3df4
AO
13427 if (RELAX_BRANCH_P (fragp->fr_subtype))
13428 {
13429
b34976b6
AM
13430 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
13431
4a6a3df4
AO
13432 return fragp->fr_var;
13433 }
13434
252b5132 13435 if (RELAX_MIPS16_P (fragp->fr_subtype))
177b4a6a
AO
13436 /* We don't want to modify the EXTENDED bit here; it might get us
13437 into infinite loops. We change it only in mips_relax_frag(). */
13438 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
252b5132
RH
13439
13440 if (mips_pic == NO_PIC)
5919d012 13441 change = nopic_need_relax (fragp->fr_symbol, 0);
252b5132 13442 else if (mips_pic == SVR4_PIC)
5919d012 13443 change = pic_need_relax (fragp->fr_symbol, segtype);
0a44bf69
RS
13444 else if (mips_pic == VXWORKS_PIC)
13445 /* For vxworks, GOT16 relocations never have a corresponding LO16. */
13446 change = 0;
252b5132
RH
13447 else
13448 abort ();
13449
13450 if (change)
13451 {
4d7206a2 13452 fragp->fr_subtype |= RELAX_USE_SECOND;
4d7206a2 13453 return -RELAX_FIRST (fragp->fr_subtype);
252b5132 13454 }
4d7206a2
RS
13455 else
13456 return -RELAX_SECOND (fragp->fr_subtype);
252b5132
RH
13457}
13458
13459/* This is called to see whether a reloc against a defined symbol
de7e6852 13460 should be converted into a reloc against a section. */
252b5132
RH
13461
13462int
17a2f251 13463mips_fix_adjustable (fixS *fixp)
252b5132 13464{
de7e6852
RS
13465 /* Don't adjust MIPS16 jump relocations, so we don't have to worry
13466 about the format of the offset in the .o file. */
252b5132
RH
13467 if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
13468 return 0;
a161fe53 13469
252b5132
RH
13470 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
13471 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13472 return 0;
a161fe53 13473
252b5132
RH
13474 if (fixp->fx_addsy == NULL)
13475 return 1;
a161fe53 13476
de7e6852
RS
13477 /* If symbol SYM is in a mergeable section, relocations of the form
13478 SYM + 0 can usually be made section-relative. The mergeable data
13479 is then identified by the section offset rather than by the symbol.
13480
13481 However, if we're generating REL LO16 relocations, the offset is split
13482 between the LO16 and parterning high part relocation. The linker will
13483 need to recalculate the complete offset in order to correctly identify
13484 the merge data.
13485
13486 The linker has traditionally not looked for the parterning high part
13487 relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
13488 placed anywhere. Rather than break backwards compatibility by changing
13489 this, it seems better not to force the issue, and instead keep the
13490 original symbol. This will work with either linker behavior. */
704803a9
MR
13491 if ((fixp->fx_r_type == BFD_RELOC_LO16
13492 || fixp->fx_r_type == BFD_RELOC_MIPS16_LO16
13493 || reloc_needs_lo_p (fixp->fx_r_type))
de7e6852
RS
13494 && HAVE_IN_PLACE_ADDENDS
13495 && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
13496 return 0;
13497
252b5132 13498#ifdef OBJ_ELF
de7e6852
RS
13499 /* Don't adjust relocations against mips16 symbols, so that the linker
13500 can find them if it needs to set up a stub. */
252b5132
RH
13501 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
13502 && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
13503 && fixp->fx_subsy == NULL)
13504 return 0;
13505#endif
a161fe53 13506
252b5132
RH
13507 return 1;
13508}
13509
13510/* Translate internal representation of relocation info to BFD target
13511 format. */
13512
13513arelent **
17a2f251 13514tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
13515{
13516 static arelent *retval[4];
13517 arelent *reloc;
13518 bfd_reloc_code_real_type code;
13519
4b0cff4e
TS
13520 memset (retval, 0, sizeof(retval));
13521 reloc = retval[0] = (arelent *) xcalloc (1, sizeof (arelent));
49309057
ILT
13522 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13523 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
13524 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
13525
bad36eac
DJ
13526 if (fixp->fx_pcrel)
13527 {
13528 assert (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2);
13529
13530 /* At this point, fx_addnumber is "symbol offset - pcrel address".
13531 Relocations want only the symbol offset. */
13532 reloc->addend = fixp->fx_addnumber + reloc->address;
13533 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
13534 {
13535 /* A gruesome hack which is a result of the gruesome gas
13536 reloc handling. What's worse, for COFF (as opposed to
13537 ECOFF), we might need yet another copy of reloc->address.
13538 See bfd_install_relocation. */
13539 reloc->addend += reloc->address;
13540 }
13541 }
13542 else
13543 reloc->addend = fixp->fx_addnumber;
252b5132 13544
438c16b8
TS
13545 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
13546 entry to be used in the relocation's section offset. */
13547 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
252b5132
RH
13548 {
13549 reloc->address = reloc->addend;
13550 reloc->addend = 0;
13551 }
13552
252b5132 13553 code = fixp->fx_r_type;
252b5132 13554
bad36eac 13555 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
252b5132
RH
13556 if (reloc->howto == NULL)
13557 {
13558 as_bad_where (fixp->fx_file, fixp->fx_line,
13559 _("Can not represent %s relocation in this object file format"),
13560 bfd_get_reloc_code_name (code));
13561 retval[0] = NULL;
13562 }
13563
13564 return retval;
13565}
13566
13567/* Relax a machine dependent frag. This returns the amount by which
13568 the current size of the frag should change. */
13569
13570int
17a2f251 13571mips_relax_frag (asection *sec, fragS *fragp, long stretch)
252b5132 13572{
4a6a3df4
AO
13573 if (RELAX_BRANCH_P (fragp->fr_subtype))
13574 {
13575 offsetT old_var = fragp->fr_var;
b34976b6
AM
13576
13577 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
4a6a3df4
AO
13578
13579 return fragp->fr_var - old_var;
13580 }
13581
252b5132
RH
13582 if (! RELAX_MIPS16_P (fragp->fr_subtype))
13583 return 0;
13584
c4e7957c 13585 if (mips16_extended_frag (fragp, NULL, stretch))
252b5132
RH
13586 {
13587 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13588 return 0;
13589 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
13590 return 2;
13591 }
13592 else
13593 {
13594 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13595 return 0;
13596 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
13597 return -2;
13598 }
13599
13600 return 0;
13601}
13602
13603/* Convert a machine dependent frag. */
13604
13605void
17a2f251 13606md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
252b5132 13607{
4a6a3df4
AO
13608 if (RELAX_BRANCH_P (fragp->fr_subtype))
13609 {
13610 bfd_byte *buf;
13611 unsigned long insn;
13612 expressionS exp;
13613 fixS *fixp;
b34976b6 13614
4a6a3df4
AO
13615 buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
13616
13617 if (target_big_endian)
13618 insn = bfd_getb32 (buf);
13619 else
13620 insn = bfd_getl32 (buf);
b34976b6 13621
4a6a3df4
AO
13622 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13623 {
13624 /* We generate a fixup instead of applying it right now
13625 because, if there are linker relaxations, we're going to
13626 need the relocations. */
13627 exp.X_op = O_symbol;
13628 exp.X_add_symbol = fragp->fr_symbol;
13629 exp.X_add_number = fragp->fr_offset;
13630
13631 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
3994f87e 13632 4, &exp, TRUE, BFD_RELOC_16_PCREL_S2);
4a6a3df4
AO
13633 fixp->fx_file = fragp->fr_file;
13634 fixp->fx_line = fragp->fr_line;
b34976b6 13635
2132e3a3 13636 md_number_to_chars ((char *) buf, insn, 4);
4a6a3df4
AO
13637 buf += 4;
13638 }
13639 else
13640 {
13641 int i;
13642
13643 as_warn_where (fragp->fr_file, fragp->fr_line,
13644 _("relaxed out-of-range branch into a jump"));
13645
13646 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
13647 goto uncond;
13648
13649 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13650 {
13651 /* Reverse the branch. */
13652 switch ((insn >> 28) & 0xf)
13653 {
13654 case 4:
13655 /* bc[0-3][tf]l? and bc1any[24][ft] instructions can
13656 have the condition reversed by tweaking a single
13657 bit, and their opcodes all have 0x4???????. */
13658 assert ((insn & 0xf1000000) == 0x41000000);
13659 insn ^= 0x00010000;
13660 break;
13661
13662 case 0:
13663 /* bltz 0x04000000 bgez 0x04010000
13664 bltzal 0x04100000 bgezal 0x04110000 */
13665 assert ((insn & 0xfc0e0000) == 0x04000000);
13666 insn ^= 0x00010000;
13667 break;
b34976b6 13668
4a6a3df4
AO
13669 case 1:
13670 /* beq 0x10000000 bne 0x14000000
13671 blez 0x18000000 bgtz 0x1c000000 */
13672 insn ^= 0x04000000;
13673 break;
13674
13675 default:
13676 abort ();
13677 }
13678 }
13679
13680 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13681 {
13682 /* Clear the and-link bit. */
13683 assert ((insn & 0xfc1c0000) == 0x04100000);
13684
13685 /* bltzal 0x04100000 bgezal 0x04110000
13686 bltzall 0x04120000 bgezall 0x04130000 */
13687 insn &= ~0x00100000;
13688 }
13689
13690 /* Branch over the branch (if the branch was likely) or the
13691 full jump (not likely case). Compute the offset from the
13692 current instruction to branch to. */
13693 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13694 i = 16;
13695 else
13696 {
13697 /* How many bytes in instructions we've already emitted? */
13698 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13699 /* How many bytes in instructions from here to the end? */
13700 i = fragp->fr_var - i;
13701 }
13702 /* Convert to instruction count. */
13703 i >>= 2;
13704 /* Branch counts from the next instruction. */
b34976b6 13705 i--;
4a6a3df4
AO
13706 insn |= i;
13707 /* Branch over the jump. */
2132e3a3 13708 md_number_to_chars ((char *) buf, insn, 4);
4a6a3df4
AO
13709 buf += 4;
13710
13711 /* Nop */
2132e3a3 13712 md_number_to_chars ((char *) buf, 0, 4);
4a6a3df4
AO
13713 buf += 4;
13714
13715 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13716 {
13717 /* beql $0, $0, 2f */
13718 insn = 0x50000000;
13719 /* Compute the PC offset from the current instruction to
13720 the end of the variable frag. */
13721 /* How many bytes in instructions we've already emitted? */
13722 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13723 /* How many bytes in instructions from here to the end? */
13724 i = fragp->fr_var - i;
13725 /* Convert to instruction count. */
13726 i >>= 2;
13727 /* Don't decrement i, because we want to branch over the
13728 delay slot. */
13729
13730 insn |= i;
2132e3a3 13731 md_number_to_chars ((char *) buf, insn, 4);
4a6a3df4
AO
13732 buf += 4;
13733
2132e3a3 13734 md_number_to_chars ((char *) buf, 0, 4);
4a6a3df4
AO
13735 buf += 4;
13736 }
13737
13738 uncond:
13739 if (mips_pic == NO_PIC)
13740 {
13741 /* j or jal. */
13742 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
13743 ? 0x0c000000 : 0x08000000);
13744 exp.X_op = O_symbol;
13745 exp.X_add_symbol = fragp->fr_symbol;
13746 exp.X_add_number = fragp->fr_offset;
13747
13748 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
3994f87e 13749 4, &exp, FALSE, BFD_RELOC_MIPS_JMP);
4a6a3df4
AO
13750 fixp->fx_file = fragp->fr_file;
13751 fixp->fx_line = fragp->fr_line;
13752
2132e3a3 13753 md_number_to_chars ((char *) buf, insn, 4);
4a6a3df4
AO
13754 buf += 4;
13755 }
13756 else
13757 {
13758 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
13759 insn = HAVE_64BIT_ADDRESSES ? 0xdf810000 : 0x8f810000;
13760 exp.X_op = O_symbol;
13761 exp.X_add_symbol = fragp->fr_symbol;
13762 exp.X_add_number = fragp->fr_offset;
13763
13764 if (fragp->fr_offset)
13765 {
13766 exp.X_add_symbol = make_expr_symbol (&exp);
13767 exp.X_add_number = 0;
13768 }
13769
13770 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
3994f87e 13771 4, &exp, FALSE, BFD_RELOC_MIPS_GOT16);
4a6a3df4
AO
13772 fixp->fx_file = fragp->fr_file;
13773 fixp->fx_line = fragp->fr_line;
13774
2132e3a3 13775 md_number_to_chars ((char *) buf, insn, 4);
4a6a3df4 13776 buf += 4;
b34976b6 13777
4a6a3df4
AO
13778 if (mips_opts.isa == ISA_MIPS1)
13779 {
13780 /* nop */
2132e3a3 13781 md_number_to_chars ((char *) buf, 0, 4);
4a6a3df4
AO
13782 buf += 4;
13783 }
13784
13785 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
13786 insn = HAVE_64BIT_ADDRESSES ? 0x64210000 : 0x24210000;
13787
13788 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
3994f87e 13789 4, &exp, FALSE, BFD_RELOC_LO16);
4a6a3df4
AO
13790 fixp->fx_file = fragp->fr_file;
13791 fixp->fx_line = fragp->fr_line;
b34976b6 13792
2132e3a3 13793 md_number_to_chars ((char *) buf, insn, 4);
4a6a3df4
AO
13794 buf += 4;
13795
13796 /* j(al)r $at. */
13797 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13798 insn = 0x0020f809;
13799 else
13800 insn = 0x00200008;
13801
2132e3a3 13802 md_number_to_chars ((char *) buf, insn, 4);
4a6a3df4
AO
13803 buf += 4;
13804 }
13805 }
13806
13807 assert (buf == (bfd_byte *)fragp->fr_literal
13808 + fragp->fr_fix + fragp->fr_var);
13809
13810 fragp->fr_fix += fragp->fr_var;
13811
13812 return;
13813 }
13814
252b5132
RH
13815 if (RELAX_MIPS16_P (fragp->fr_subtype))
13816 {
13817 int type;
3994f87e 13818 const struct mips16_immed_operand *op;
b34976b6 13819 bfd_boolean small, ext;
252b5132
RH
13820 offsetT val;
13821 bfd_byte *buf;
13822 unsigned long insn;
b34976b6 13823 bfd_boolean use_extend;
252b5132
RH
13824 unsigned short extend;
13825
13826 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
13827 op = mips16_immed_operands;
13828 while (op->type != type)
13829 ++op;
13830
13831 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13832 {
b34976b6
AM
13833 small = FALSE;
13834 ext = TRUE;
252b5132
RH
13835 }
13836 else
13837 {
b34976b6
AM
13838 small = TRUE;
13839 ext = FALSE;
252b5132
RH
13840 }
13841
6386f3a7 13842 resolve_symbol_value (fragp->fr_symbol);
252b5132
RH
13843 val = S_GET_VALUE (fragp->fr_symbol);
13844 if (op->pcrel)
13845 {
13846 addressT addr;
13847
13848 addr = fragp->fr_address + fragp->fr_fix;
13849
13850 /* The rules for the base address of a PC relative reloc are
13851 complicated; see mips16_extended_frag. */
13852 if (type == 'p' || type == 'q')
13853 {
13854 addr += 2;
13855 if (ext)
13856 addr += 2;
13857 /* Ignore the low bit in the target, since it will be
13858 set for a text label. */
13859 if ((val & 1) != 0)
13860 --val;
13861 }
13862 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13863 addr -= 4;
13864 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13865 addr -= 2;
13866
13867 addr &= ~ (addressT) ((1 << op->shift) - 1);
13868 val -= addr;
13869
13870 /* Make sure the section winds up with the alignment we have
13871 assumed. */
13872 if (op->shift > 0)
13873 record_alignment (asec, op->shift);
13874 }
13875
13876 if (ext
13877 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
13878 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
13879 as_warn_where (fragp->fr_file, fragp->fr_line,
13880 _("extended instruction in delay slot"));
13881
13882 buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
13883
13884 if (target_big_endian)
13885 insn = bfd_getb16 (buf);
13886 else
13887 insn = bfd_getl16 (buf);
13888
13889 mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
13890 RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
13891 small, ext, &insn, &use_extend, &extend);
13892
13893 if (use_extend)
13894 {
2132e3a3 13895 md_number_to_chars ((char *) buf, 0xf000 | extend, 2);
252b5132
RH
13896 fragp->fr_fix += 2;
13897 buf += 2;
13898 }
13899
2132e3a3 13900 md_number_to_chars ((char *) buf, insn, 2);
252b5132
RH
13901 fragp->fr_fix += 2;
13902 buf += 2;
13903 }
13904 else
13905 {
4d7206a2
RS
13906 int first, second;
13907 fixS *fixp;
252b5132 13908
4d7206a2
RS
13909 first = RELAX_FIRST (fragp->fr_subtype);
13910 second = RELAX_SECOND (fragp->fr_subtype);
13911 fixp = (fixS *) fragp->fr_opcode;
252b5132 13912
584892a6
RS
13913 /* Possibly emit a warning if we've chosen the longer option. */
13914 if (((fragp->fr_subtype & RELAX_USE_SECOND) != 0)
13915 == ((fragp->fr_subtype & RELAX_SECOND_LONGER) != 0))
13916 {
13917 const char *msg = macro_warning (fragp->fr_subtype);
13918 if (msg != 0)
13919 as_warn_where (fragp->fr_file, fragp->fr_line, msg);
13920 }
13921
4d7206a2
RS
13922 /* Go through all the fixups for the first sequence. Disable them
13923 (by marking them as done) if we're going to use the second
13924 sequence instead. */
13925 while (fixp
13926 && fixp->fx_frag == fragp
13927 && fixp->fx_where < fragp->fr_fix - second)
13928 {
13929 if (fragp->fr_subtype & RELAX_USE_SECOND)
13930 fixp->fx_done = 1;
13931 fixp = fixp->fx_next;
13932 }
252b5132 13933
4d7206a2
RS
13934 /* Go through the fixups for the second sequence. Disable them if
13935 we're going to use the first sequence, otherwise adjust their
13936 addresses to account for the relaxation. */
13937 while (fixp && fixp->fx_frag == fragp)
13938 {
13939 if (fragp->fr_subtype & RELAX_USE_SECOND)
13940 fixp->fx_where -= first;
13941 else
13942 fixp->fx_done = 1;
13943 fixp = fixp->fx_next;
13944 }
13945
13946 /* Now modify the frag contents. */
13947 if (fragp->fr_subtype & RELAX_USE_SECOND)
13948 {
13949 char *start;
13950
13951 start = fragp->fr_literal + fragp->fr_fix - first - second;
13952 memmove (start, start + first, second);
13953 fragp->fr_fix -= first;
13954 }
13955 else
13956 fragp->fr_fix -= second;
252b5132
RH
13957 }
13958}
13959
13960#ifdef OBJ_ELF
13961
13962/* This function is called after the relocs have been generated.
13963 We've been storing mips16 text labels as odd. Here we convert them
13964 back to even for the convenience of the debugger. */
13965
13966void
17a2f251 13967mips_frob_file_after_relocs (void)
252b5132
RH
13968{
13969 asymbol **syms;
13970 unsigned int count, i;
13971
13972 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
13973 return;
13974
13975 syms = bfd_get_outsymbols (stdoutput);
13976 count = bfd_get_symcount (stdoutput);
13977 for (i = 0; i < count; i++, syms++)
13978 {
13979 if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
13980 && ((*syms)->value & 1) != 0)
13981 {
13982 (*syms)->value &= ~1;
13983 /* If the symbol has an odd size, it was probably computed
13984 incorrectly, so adjust that as well. */
13985 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
13986 ++elf_symbol (*syms)->internal_elf_sym.st_size;
13987 }
13988 }
13989}
13990
13991#endif
13992
13993/* This function is called whenever a label is defined. It is used
13994 when handling branch delays; if a branch has a label, we assume we
13995 can not move it. */
13996
13997void
17a2f251 13998mips_define_label (symbolS *sym)
252b5132
RH
13999{
14000 struct insn_label_list *l;
14001
14002 if (free_insn_labels == NULL)
14003 l = (struct insn_label_list *) xmalloc (sizeof *l);
14004 else
14005 {
14006 l = free_insn_labels;
14007 free_insn_labels = l->next;
14008 }
14009
14010 l->label = sym;
14011 l->next = insn_labels;
14012 insn_labels = l;
07a53e5c
RH
14013
14014#ifdef OBJ_ELF
14015 dwarf2_emit_label (sym);
14016#endif
252b5132
RH
14017}
14018\f
14019#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14020
14021/* Some special processing for a MIPS ELF file. */
14022
14023void
17a2f251 14024mips_elf_final_processing (void)
252b5132
RH
14025{
14026 /* Write out the register information. */
316f5878 14027 if (mips_abi != N64_ABI)
252b5132
RH
14028 {
14029 Elf32_RegInfo s;
14030
14031 s.ri_gprmask = mips_gprmask;
14032 s.ri_cprmask[0] = mips_cprmask[0];
14033 s.ri_cprmask[1] = mips_cprmask[1];
14034 s.ri_cprmask[2] = mips_cprmask[2];
14035 s.ri_cprmask[3] = mips_cprmask[3];
14036 /* The gp_value field is set by the MIPS ELF backend. */
14037
14038 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
14039 ((Elf32_External_RegInfo *)
14040 mips_regmask_frag));
14041 }
14042 else
14043 {
14044 Elf64_Internal_RegInfo s;
14045
14046 s.ri_gprmask = mips_gprmask;
14047 s.ri_pad = 0;
14048 s.ri_cprmask[0] = mips_cprmask[0];
14049 s.ri_cprmask[1] = mips_cprmask[1];
14050 s.ri_cprmask[2] = mips_cprmask[2];
14051 s.ri_cprmask[3] = mips_cprmask[3];
14052 /* The gp_value field is set by the MIPS ELF backend. */
14053
14054 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
14055 ((Elf64_External_RegInfo *)
14056 mips_regmask_frag));
14057 }
14058
14059 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
14060 sort of BFD interface for this. */
14061 if (mips_any_noreorder)
14062 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
14063 if (mips_pic != NO_PIC)
143d77c5 14064 {
252b5132 14065 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
143d77c5
EC
14066 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
14067 }
14068 if (mips_abicalls)
14069 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
252b5132 14070
98d3f06f 14071 /* Set MIPS ELF flags for ASEs. */
74cd071d
CF
14072 /* We may need to define a new flag for DSP ASE, and set this flag when
14073 file_ase_dsp is true. */
ef2e4d86
CF
14074 /* We may need to define a new flag for MT ASE, and set this flag when
14075 file_ase_mt is true. */
a4672219
TS
14076 if (file_ase_mips16)
14077 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
1f25f5d3
CD
14078#if 0 /* XXX FIXME */
14079 if (file_ase_mips3d)
14080 elf_elfheader (stdoutput)->e_flags |= ???;
14081#endif
deec1734
CD
14082 if (file_ase_mdmx)
14083 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
1f25f5d3 14084
bdaaa2e1 14085 /* Set the MIPS ELF ABI flags. */
316f5878 14086 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
252b5132 14087 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
316f5878 14088 else if (mips_abi == O64_ABI)
252b5132 14089 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
316f5878 14090 else if (mips_abi == EABI_ABI)
252b5132 14091 {
316f5878 14092 if (!file_mips_gp32)
252b5132
RH
14093 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
14094 else
14095 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
14096 }
316f5878 14097 else if (mips_abi == N32_ABI)
be00bddd
TS
14098 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
14099
c9914766 14100 /* Nothing to do for N64_ABI. */
252b5132
RH
14101
14102 if (mips_32bitmode)
14103 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
ad3fea08
TS
14104
14105#if 0 /* XXX FIXME */
14106 /* 32 bit code with 64 bit FP registers. */
14107 if (!file_mips_fp32 && ABI_NEEDS_32BIT_REGS (mips_abi))
14108 elf_elfheader (stdoutput)->e_flags |= ???;
14109#endif
252b5132
RH
14110}
14111
14112#endif /* OBJ_ELF || OBJ_MAYBE_ELF */
14113\f
beae10d5 14114typedef struct proc {
9b2f1d35
EC
14115 symbolS *func_sym;
14116 symbolS *func_end_sym;
beae10d5
KH
14117 unsigned long reg_mask;
14118 unsigned long reg_offset;
14119 unsigned long fpreg_mask;
14120 unsigned long fpreg_offset;
14121 unsigned long frame_offset;
14122 unsigned long frame_reg;
14123 unsigned long pc_reg;
14124} procS;
252b5132
RH
14125
14126static procS cur_proc;
14127static procS *cur_proc_ptr;
14128static int numprocs;
14129
0a9ef439 14130/* Fill in an rs_align_code fragment. */
a19d8eb0 14131
0a9ef439 14132void
17a2f251 14133mips_handle_align (fragS *fragp)
a19d8eb0 14134{
0a9ef439
RH
14135 if (fragp->fr_type != rs_align_code)
14136 return;
14137
14138 if (mips_opts.mips16)
a19d8eb0
CP
14139 {
14140 static const unsigned char be_nop[] = { 0x65, 0x00 };
14141 static const unsigned char le_nop[] = { 0x00, 0x65 };
14142
0a9ef439
RH
14143 int bytes;
14144 char *p;
a19d8eb0 14145
0a9ef439
RH
14146 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
14147 p = fragp->fr_literal + fragp->fr_fix;
14148
14149 if (bytes & 1)
14150 {
14151 *p++ = 0;
f9419b05 14152 fragp->fr_fix++;
0a9ef439
RH
14153 }
14154
14155 memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
14156 fragp->fr_var = 2;
a19d8eb0
CP
14157 }
14158
0a9ef439 14159 /* For mips32, a nop is a zero, which we trivially get by doing nothing. */
a19d8eb0
CP
14160}
14161
252b5132 14162static void
17a2f251 14163md_obj_begin (void)
252b5132
RH
14164{
14165}
14166
14167static void
17a2f251 14168md_obj_end (void)
252b5132
RH
14169{
14170 /* check for premature end, nesting errors, etc */
14171 if (cur_proc_ptr)
9a41af64 14172 as_warn (_("missing .end at end of assembly"));
252b5132
RH
14173}
14174
14175static long
17a2f251 14176get_number (void)
252b5132
RH
14177{
14178 int negative = 0;
14179 long val = 0;
14180
14181 if (*input_line_pointer == '-')
14182 {
14183 ++input_line_pointer;
14184 negative = 1;
14185 }
3882b010 14186 if (!ISDIGIT (*input_line_pointer))
956cd1d6 14187 as_bad (_("expected simple number"));
252b5132
RH
14188 if (input_line_pointer[0] == '0')
14189 {
14190 if (input_line_pointer[1] == 'x')
14191 {
14192 input_line_pointer += 2;
3882b010 14193 while (ISXDIGIT (*input_line_pointer))
252b5132
RH
14194 {
14195 val <<= 4;
14196 val |= hex_value (*input_line_pointer++);
14197 }
14198 return negative ? -val : val;
14199 }
14200 else
14201 {
14202 ++input_line_pointer;
3882b010 14203 while (ISDIGIT (*input_line_pointer))
252b5132
RH
14204 {
14205 val <<= 3;
14206 val |= *input_line_pointer++ - '0';
14207 }
14208 return negative ? -val : val;
14209 }
14210 }
3882b010 14211 if (!ISDIGIT (*input_line_pointer))
252b5132
RH
14212 {
14213 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
14214 *input_line_pointer, *input_line_pointer);
956cd1d6 14215 as_warn (_("invalid number"));
252b5132
RH
14216 return -1;
14217 }
3882b010 14218 while (ISDIGIT (*input_line_pointer))
252b5132
RH
14219 {
14220 val *= 10;
14221 val += *input_line_pointer++ - '0';
14222 }
14223 return negative ? -val : val;
14224}
14225
14226/* The .file directive; just like the usual .file directive, but there
c5dd6aab
DJ
14227 is an initial number which is the ECOFF file index. In the non-ECOFF
14228 case .file implies DWARF-2. */
14229
14230static void
17a2f251 14231s_mips_file (int x ATTRIBUTE_UNUSED)
c5dd6aab 14232{
ecb4347a
DJ
14233 static int first_file_directive = 0;
14234
c5dd6aab
DJ
14235 if (ECOFF_DEBUGGING)
14236 {
14237 get_number ();
14238 s_app_file (0);
14239 }
14240 else
ecb4347a
DJ
14241 {
14242 char *filename;
14243
14244 filename = dwarf2_directive_file (0);
14245
14246 /* Versions of GCC up to 3.1 start files with a ".file"
14247 directive even for stabs output. Make sure that this
14248 ".file" is handled. Note that you need a version of GCC
14249 after 3.1 in order to support DWARF-2 on MIPS. */
14250 if (filename != NULL && ! first_file_directive)
14251 {
14252 (void) new_logical_line (filename, -1);
c04f5787 14253 s_app_file_string (filename, 0);
ecb4347a
DJ
14254 }
14255 first_file_directive = 1;
14256 }
c5dd6aab
DJ
14257}
14258
14259/* The .loc directive, implying DWARF-2. */
252b5132
RH
14260
14261static void
17a2f251 14262s_mips_loc (int x ATTRIBUTE_UNUSED)
252b5132 14263{
c5dd6aab
DJ
14264 if (!ECOFF_DEBUGGING)
14265 dwarf2_directive_loc (0);
252b5132
RH
14266}
14267
252b5132
RH
14268/* The .end directive. */
14269
14270static void
17a2f251 14271s_mips_end (int x ATTRIBUTE_UNUSED)
252b5132
RH
14272{
14273 symbolS *p;
252b5132 14274
7a621144
DJ
14275 /* Following functions need their own .frame and .cprestore directives. */
14276 mips_frame_reg_valid = 0;
14277 mips_cprestore_valid = 0;
14278
252b5132
RH
14279 if (!is_end_of_line[(unsigned char) *input_line_pointer])
14280 {
14281 p = get_symbol ();
14282 demand_empty_rest_of_line ();
14283 }
14284 else
14285 p = NULL;
14286
14949570 14287 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
14288 as_warn (_(".end not in text section"));
14289
14290 if (!cur_proc_ptr)
14291 {
14292 as_warn (_(".end directive without a preceding .ent directive."));
14293 demand_empty_rest_of_line ();
14294 return;
14295 }
14296
14297 if (p != NULL)
14298 {
14299 assert (S_GET_NAME (p));
9b2f1d35 14300 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
252b5132 14301 as_warn (_(".end symbol does not match .ent symbol."));
ecb4347a
DJ
14302
14303 if (debug_type == DEBUG_STABS)
14304 stabs_generate_asm_endfunc (S_GET_NAME (p),
14305 S_GET_NAME (p));
252b5132
RH
14306 }
14307 else
14308 as_warn (_(".end directive missing or unknown symbol"));
14309
2132e3a3 14310#ifdef OBJ_ELF
9b2f1d35
EC
14311 /* Create an expression to calculate the size of the function. */
14312 if (p && cur_proc_ptr)
14313 {
14314 OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
14315 expressionS *exp = xmalloc (sizeof (expressionS));
14316
14317 obj->size = exp;
14318 exp->X_op = O_subtract;
14319 exp->X_add_symbol = symbol_temp_new_now ();
14320 exp->X_op_symbol = p;
14321 exp->X_add_number = 0;
14322
14323 cur_proc_ptr->func_end_sym = exp->X_add_symbol;
14324 }
14325
ecb4347a 14326 /* Generate a .pdr section. */
dcd410fe
RO
14327 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING
14328 && mips_flag_pdr)
ecb4347a
DJ
14329 {
14330 segT saved_seg = now_seg;
14331 subsegT saved_subseg = now_subseg;
14332 valueT dot;
14333 expressionS exp;
14334 char *fragp;
252b5132 14335
ecb4347a 14336 dot = frag_now_fix ();
252b5132
RH
14337
14338#ifdef md_flush_pending_output
ecb4347a 14339 md_flush_pending_output ();
252b5132
RH
14340#endif
14341
ecb4347a
DJ
14342 assert (pdr_seg);
14343 subseg_set (pdr_seg, 0);
252b5132 14344
ecb4347a
DJ
14345 /* Write the symbol. */
14346 exp.X_op = O_symbol;
14347 exp.X_add_symbol = p;
14348 exp.X_add_number = 0;
14349 emit_expr (&exp, 4);
252b5132 14350
ecb4347a 14351 fragp = frag_more (7 * 4);
252b5132 14352
17a2f251
TS
14353 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
14354 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
14355 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
14356 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
14357 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
14358 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
14359 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
252b5132 14360
ecb4347a
DJ
14361 subseg_set (saved_seg, saved_subseg);
14362 }
14363#endif /* OBJ_ELF */
252b5132
RH
14364
14365 cur_proc_ptr = NULL;
14366}
14367
14368/* The .aent and .ent directives. */
14369
14370static void
17a2f251 14371s_mips_ent (int aent)
252b5132 14372{
252b5132 14373 symbolS *symbolP;
252b5132
RH
14374
14375 symbolP = get_symbol ();
14376 if (*input_line_pointer == ',')
f9419b05 14377 ++input_line_pointer;
252b5132 14378 SKIP_WHITESPACE ();
3882b010 14379 if (ISDIGIT (*input_line_pointer)
d9a62219 14380 || *input_line_pointer == '-')
874e8986 14381 get_number ();
252b5132 14382
14949570 14383 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
14384 as_warn (_(".ent or .aent not in text section."));
14385
14386 if (!aent && cur_proc_ptr)
9a41af64 14387 as_warn (_("missing .end"));
252b5132
RH
14388
14389 if (!aent)
14390 {
7a621144
DJ
14391 /* This function needs its own .frame and .cprestore directives. */
14392 mips_frame_reg_valid = 0;
14393 mips_cprestore_valid = 0;
14394
252b5132
RH
14395 cur_proc_ptr = &cur_proc;
14396 memset (cur_proc_ptr, '\0', sizeof (procS));
14397
9b2f1d35 14398 cur_proc_ptr->func_sym = symbolP;
252b5132 14399
49309057 14400 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
252b5132 14401
f9419b05 14402 ++numprocs;
ecb4347a
DJ
14403
14404 if (debug_type == DEBUG_STABS)
14405 stabs_generate_asm_func (S_GET_NAME (symbolP),
14406 S_GET_NAME (symbolP));
252b5132
RH
14407 }
14408
14409 demand_empty_rest_of_line ();
14410}
14411
14412/* The .frame directive. If the mdebug section is present (IRIX 5 native)
bdaaa2e1 14413 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
252b5132 14414 s_mips_frame is used so that we can set the PDR information correctly.
bdaaa2e1 14415 We can't use the ecoff routines because they make reference to the ecoff
252b5132
RH
14416 symbol table (in the mdebug section). */
14417
14418static void
17a2f251 14419s_mips_frame (int ignore ATTRIBUTE_UNUSED)
252b5132 14420{
ecb4347a
DJ
14421#ifdef OBJ_ELF
14422 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14423 {
14424 long val;
252b5132 14425
ecb4347a
DJ
14426 if (cur_proc_ptr == (procS *) NULL)
14427 {
14428 as_warn (_(".frame outside of .ent"));
14429 demand_empty_rest_of_line ();
14430 return;
14431 }
252b5132 14432
ecb4347a
DJ
14433 cur_proc_ptr->frame_reg = tc_get_register (1);
14434
14435 SKIP_WHITESPACE ();
14436 if (*input_line_pointer++ != ','
14437 || get_absolute_expression_and_terminator (&val) != ',')
14438 {
14439 as_warn (_("Bad .frame directive"));
14440 --input_line_pointer;
14441 demand_empty_rest_of_line ();
14442 return;
14443 }
252b5132 14444
ecb4347a
DJ
14445 cur_proc_ptr->frame_offset = val;
14446 cur_proc_ptr->pc_reg = tc_get_register (0);
252b5132 14447
252b5132 14448 demand_empty_rest_of_line ();
252b5132 14449 }
ecb4347a
DJ
14450 else
14451#endif /* OBJ_ELF */
14452 s_ignore (ignore);
252b5132
RH
14453}
14454
bdaaa2e1
KH
14455/* The .fmask and .mask directives. If the mdebug section is present
14456 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
252b5132 14457 embedded targets, s_mips_mask is used so that we can set the PDR
bdaaa2e1 14458 information correctly. We can't use the ecoff routines because they
252b5132
RH
14459 make reference to the ecoff symbol table (in the mdebug section). */
14460
14461static void
17a2f251 14462s_mips_mask (int reg_type)
252b5132 14463{
ecb4347a
DJ
14464#ifdef OBJ_ELF
14465 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
252b5132 14466 {
ecb4347a 14467 long mask, off;
252b5132 14468
ecb4347a
DJ
14469 if (cur_proc_ptr == (procS *) NULL)
14470 {
14471 as_warn (_(".mask/.fmask outside of .ent"));
14472 demand_empty_rest_of_line ();
14473 return;
14474 }
252b5132 14475
ecb4347a
DJ
14476 if (get_absolute_expression_and_terminator (&mask) != ',')
14477 {
14478 as_warn (_("Bad .mask/.fmask directive"));
14479 --input_line_pointer;
14480 demand_empty_rest_of_line ();
14481 return;
14482 }
252b5132 14483
ecb4347a
DJ
14484 off = get_absolute_expression ();
14485
14486 if (reg_type == 'F')
14487 {
14488 cur_proc_ptr->fpreg_mask = mask;
14489 cur_proc_ptr->fpreg_offset = off;
14490 }
14491 else
14492 {
14493 cur_proc_ptr->reg_mask = mask;
14494 cur_proc_ptr->reg_offset = off;
14495 }
14496
14497 demand_empty_rest_of_line ();
252b5132
RH
14498 }
14499 else
ecb4347a
DJ
14500#endif /* OBJ_ELF */
14501 s_ignore (reg_type);
252b5132
RH
14502}
14503
316f5878
RS
14504/* A table describing all the processors gas knows about. Names are
14505 matched in the order listed.
e7af610e 14506
316f5878
RS
14507 To ease comparison, please keep this table in the same order as
14508 gcc's mips_cpu_info_table[]. */
e972090a
NC
14509static const struct mips_cpu_info mips_cpu_info_table[] =
14510{
316f5878 14511 /* Entries for generic ISAs */
ad3fea08
TS
14512 { "mips1", MIPS_CPU_IS_ISA, ISA_MIPS1, CPU_R3000 },
14513 { "mips2", MIPS_CPU_IS_ISA, ISA_MIPS2, CPU_R6000 },
14514 { "mips3", MIPS_CPU_IS_ISA, ISA_MIPS3, CPU_R4000 },
14515 { "mips4", MIPS_CPU_IS_ISA, ISA_MIPS4, CPU_R8000 },
14516 { "mips5", MIPS_CPU_IS_ISA, ISA_MIPS5, CPU_MIPS5 },
14517 { "mips32", MIPS_CPU_IS_ISA, ISA_MIPS32, CPU_MIPS32 },
14518 { "mips32r2", MIPS_CPU_IS_ISA, ISA_MIPS32R2, CPU_MIPS32R2 },
14519 { "mips64", MIPS_CPU_IS_ISA, ISA_MIPS64, CPU_MIPS64 },
14520 { "mips64r2", MIPS_CPU_IS_ISA, ISA_MIPS64R2, CPU_MIPS64R2 },
316f5878
RS
14521
14522 /* MIPS I */
ad3fea08
TS
14523 { "r3000", 0, ISA_MIPS1, CPU_R3000 },
14524 { "r2000", 0, ISA_MIPS1, CPU_R3000 },
14525 { "r3900", 0, ISA_MIPS1, CPU_R3900 },
316f5878
RS
14526
14527 /* MIPS II */
ad3fea08 14528 { "r6000", 0, ISA_MIPS2, CPU_R6000 },
316f5878
RS
14529
14530 /* MIPS III */
ad3fea08
TS
14531 { "r4000", 0, ISA_MIPS3, CPU_R4000 },
14532 { "r4010", 0, ISA_MIPS2, CPU_R4010 },
14533 { "vr4100", 0, ISA_MIPS3, CPU_VR4100 },
14534 { "vr4111", 0, ISA_MIPS3, CPU_R4111 },
14535 { "vr4120", 0, ISA_MIPS3, CPU_VR4120 },
14536 { "vr4130", 0, ISA_MIPS3, CPU_VR4120 },
14537 { "vr4181", 0, ISA_MIPS3, CPU_R4111 },
14538 { "vr4300", 0, ISA_MIPS3, CPU_R4300 },
14539 { "r4400", 0, ISA_MIPS3, CPU_R4400 },
14540 { "r4600", 0, ISA_MIPS3, CPU_R4600 },
14541 { "orion", 0, ISA_MIPS3, CPU_R4600 },
14542 { "r4650", 0, ISA_MIPS3, CPU_R4650 },
316f5878
RS
14543
14544 /* MIPS IV */
ad3fea08
TS
14545 { "r8000", 0, ISA_MIPS4, CPU_R8000 },
14546 { "r10000", 0, ISA_MIPS4, CPU_R10000 },
14547 { "r12000", 0, ISA_MIPS4, CPU_R12000 },
14548 { "vr5000", 0, ISA_MIPS4, CPU_R5000 },
14549 { "vr5400", 0, ISA_MIPS4, CPU_VR5400 },
14550 { "vr5500", 0, ISA_MIPS4, CPU_VR5500 },
14551 { "rm5200", 0, ISA_MIPS4, CPU_R5000 },
14552 { "rm5230", 0, ISA_MIPS4, CPU_R5000 },
14553 { "rm5231", 0, ISA_MIPS4, CPU_R5000 },
14554 { "rm5261", 0, ISA_MIPS4, CPU_R5000 },
14555 { "rm5721", 0, ISA_MIPS4, CPU_R5000 },
14556 { "rm7000", 0, ISA_MIPS4, CPU_RM7000 },
14557 { "rm9000", 0, ISA_MIPS4, CPU_RM9000 },
316f5878
RS
14558
14559 /* MIPS 32 */
ad3fea08
TS
14560 { "4kc", 0, ISA_MIPS32, CPU_MIPS32 },
14561 { "4km", 0, ISA_MIPS32, CPU_MIPS32 },
14562 { "4kp", 0, ISA_MIPS32, CPU_MIPS32 },
14563 { "4ksc", MIPS_CPU_ASE_SMARTMIPS, ISA_MIPS32, CPU_MIPS32 },
14564
14565 /* MIPS 32 Release 2 */
14566 { "4kec", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
14567 { "4kem", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
14568 { "4kep", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
14569 { "4ksd", MIPS_CPU_ASE_SMARTMIPS, ISA_MIPS32R2, CPU_MIPS32R2 },
14570 { "m4k", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
14571 { "m4kp", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
14572 { "24k", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
14573 { "24kc", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
14574 { "24kf", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
14575 { "24kx", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
14576 /* 24ke is a 24k with DSP ASE, other ASEs are optional. */
14577 { "24ke", MIPS_CPU_ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
14578 { "24kec", MIPS_CPU_ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
14579 { "24kef", MIPS_CPU_ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
65263ce3 14580 { "24kex", MIPS_CPU_ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
ad3fea08
TS
14581 /* 34k is a 24k with MT ASE, other ASEs are optional. */
14582 { "34kc", MIPS_CPU_ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
14583 { "34kf", MIPS_CPU_ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
65263ce3 14584 { "34kx", MIPS_CPU_ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
32b26a03 14585
316f5878 14586 /* MIPS 64 */
ad3fea08
TS
14587 { "5kc", 0, ISA_MIPS64, CPU_MIPS64 },
14588 { "5kf", 0, ISA_MIPS64, CPU_MIPS64 },
14589 { "20kc", MIPS_CPU_ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
14590
14591 /* MIPS 64 Release 2 */
14592 { "25kf", MIPS_CPU_ASE_MIPS3D, ISA_MIPS64R2, CPU_MIPS64R2 },
e7af610e 14593
c7a23324 14594 /* Broadcom SB-1 CPU core */
65263ce3
TS
14595 { "sb1", MIPS_CPU_ASE_MIPS3D | MIPS_CPU_ASE_MDMX,
14596 ISA_MIPS64, CPU_SB1 },
e7af610e 14597
316f5878
RS
14598 /* End marker */
14599 { NULL, 0, 0, 0 }
14600};
e7af610e 14601
84ea6cf2 14602
316f5878
RS
14603/* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
14604 with a final "000" replaced by "k". Ignore case.
e7af610e 14605
316f5878 14606 Note: this function is shared between GCC and GAS. */
c6c98b38 14607
b34976b6 14608static bfd_boolean
17a2f251 14609mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
14610{
14611 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
14612 given++, canonical++;
14613
14614 return ((*given == 0 && *canonical == 0)
14615 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
14616}
14617
14618
14619/* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
14620 CPU name. We've traditionally allowed a lot of variation here.
14621
14622 Note: this function is shared between GCC and GAS. */
14623
b34976b6 14624static bfd_boolean
17a2f251 14625mips_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
14626{
14627 /* First see if the name matches exactly, or with a final "000"
14628 turned into "k". */
14629 if (mips_strict_matching_cpu_name_p (canonical, given))
b34976b6 14630 return TRUE;
316f5878
RS
14631
14632 /* If not, try comparing based on numerical designation alone.
14633 See if GIVEN is an unadorned number, or 'r' followed by a number. */
14634 if (TOLOWER (*given) == 'r')
14635 given++;
14636 if (!ISDIGIT (*given))
b34976b6 14637 return FALSE;
316f5878
RS
14638
14639 /* Skip over some well-known prefixes in the canonical name,
14640 hoping to find a number there too. */
14641 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
14642 canonical += 2;
14643 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
14644 canonical += 2;
14645 else if (TOLOWER (canonical[0]) == 'r')
14646 canonical += 1;
14647
14648 return mips_strict_matching_cpu_name_p (canonical, given);
14649}
14650
14651
14652/* Parse an option that takes the name of a processor as its argument.
14653 OPTION is the name of the option and CPU_STRING is the argument.
14654 Return the corresponding processor enumeration if the CPU_STRING is
14655 recognized, otherwise report an error and return null.
14656
14657 A similar function exists in GCC. */
e7af610e
NC
14658
14659static const struct mips_cpu_info *
17a2f251 14660mips_parse_cpu (const char *option, const char *cpu_string)
e7af610e 14661{
316f5878 14662 const struct mips_cpu_info *p;
e7af610e 14663
316f5878
RS
14664 /* 'from-abi' selects the most compatible architecture for the given
14665 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
14666 EABIs, we have to decide whether we're using the 32-bit or 64-bit
14667 version. Look first at the -mgp options, if given, otherwise base
14668 the choice on MIPS_DEFAULT_64BIT.
e7af610e 14669
316f5878
RS
14670 Treat NO_ABI like the EABIs. One reason to do this is that the
14671 plain 'mips' and 'mips64' configs have 'from-abi' as their default
14672 architecture. This code picks MIPS I for 'mips' and MIPS III for
14673 'mips64', just as we did in the days before 'from-abi'. */
14674 if (strcasecmp (cpu_string, "from-abi") == 0)
14675 {
14676 if (ABI_NEEDS_32BIT_REGS (mips_abi))
14677 return mips_cpu_info_from_isa (ISA_MIPS1);
14678
14679 if (ABI_NEEDS_64BIT_REGS (mips_abi))
14680 return mips_cpu_info_from_isa (ISA_MIPS3);
14681
14682 if (file_mips_gp32 >= 0)
14683 return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
14684
14685 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
14686 ? ISA_MIPS3
14687 : ISA_MIPS1);
14688 }
14689
14690 /* 'default' has traditionally been a no-op. Probably not very useful. */
14691 if (strcasecmp (cpu_string, "default") == 0)
14692 return 0;
14693
14694 for (p = mips_cpu_info_table; p->name != 0; p++)
14695 if (mips_matching_cpu_name_p (p->name, cpu_string))
14696 return p;
14697
14698 as_bad ("Bad value (%s) for %s", cpu_string, option);
14699 return 0;
e7af610e
NC
14700}
14701
316f5878
RS
14702/* Return the canonical processor information for ISA (a member of the
14703 ISA_MIPS* enumeration). */
14704
e7af610e 14705static const struct mips_cpu_info *
17a2f251 14706mips_cpu_info_from_isa (int isa)
e7af610e
NC
14707{
14708 int i;
14709
14710 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
ad3fea08 14711 if ((mips_cpu_info_table[i].flags & MIPS_CPU_IS_ISA)
316f5878 14712 && isa == mips_cpu_info_table[i].isa)
e7af610e
NC
14713 return (&mips_cpu_info_table[i]);
14714
e972090a 14715 return NULL;
e7af610e 14716}
fef14a42
TS
14717
14718static const struct mips_cpu_info *
17a2f251 14719mips_cpu_info_from_arch (int arch)
fef14a42
TS
14720{
14721 int i;
14722
14723 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14724 if (arch == mips_cpu_info_table[i].cpu)
14725 return (&mips_cpu_info_table[i]);
14726
14727 return NULL;
14728}
316f5878
RS
14729\f
14730static void
17a2f251 14731show (FILE *stream, const char *string, int *col_p, int *first_p)
316f5878
RS
14732{
14733 if (*first_p)
14734 {
14735 fprintf (stream, "%24s", "");
14736 *col_p = 24;
14737 }
14738 else
14739 {
14740 fprintf (stream, ", ");
14741 *col_p += 2;
14742 }
e7af610e 14743
316f5878
RS
14744 if (*col_p + strlen (string) > 72)
14745 {
14746 fprintf (stream, "\n%24s", "");
14747 *col_p = 24;
14748 }
14749
14750 fprintf (stream, "%s", string);
14751 *col_p += strlen (string);
14752
14753 *first_p = 0;
14754}
14755
14756void
17a2f251 14757md_show_usage (FILE *stream)
e7af610e 14758{
316f5878
RS
14759 int column, first;
14760 size_t i;
14761
14762 fprintf (stream, _("\
14763MIPS options:\n\
316f5878
RS
14764-EB generate big endian output\n\
14765-EL generate little endian output\n\
14766-g, -g2 do not remove unneeded NOPs or swap branches\n\
14767-G NUM allow referencing objects up to NUM bytes\n\
14768 implicitly with the gp register [default 8]\n"));
14769 fprintf (stream, _("\
14770-mips1 generate MIPS ISA I instructions\n\
14771-mips2 generate MIPS ISA II instructions\n\
14772-mips3 generate MIPS ISA III instructions\n\
14773-mips4 generate MIPS ISA IV instructions\n\
14774-mips5 generate MIPS ISA V instructions\n\
14775-mips32 generate MIPS32 ISA instructions\n\
af7ee8bf 14776-mips32r2 generate MIPS32 release 2 ISA instructions\n\
316f5878 14777-mips64 generate MIPS64 ISA instructions\n\
5f74bc13 14778-mips64r2 generate MIPS64 release 2 ISA instructions\n\
316f5878
RS
14779-march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
14780
14781 first = 1;
e7af610e
NC
14782
14783 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
316f5878
RS
14784 show (stream, mips_cpu_info_table[i].name, &column, &first);
14785 show (stream, "from-abi", &column, &first);
14786 fputc ('\n', stream);
e7af610e 14787
316f5878
RS
14788 fprintf (stream, _("\
14789-mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
14790-no-mCPU don't generate code specific to CPU.\n\
14791 For -mCPU and -no-mCPU, CPU must be one of:\n"));
14792
14793 first = 1;
14794
14795 show (stream, "3900", &column, &first);
14796 show (stream, "4010", &column, &first);
14797 show (stream, "4100", &column, &first);
14798 show (stream, "4650", &column, &first);
14799 fputc ('\n', stream);
14800
14801 fprintf (stream, _("\
14802-mips16 generate mips16 instructions\n\
14803-no-mips16 do not generate mips16 instructions\n"));
14804 fprintf (stream, _("\
e16bfa71
TS
14805-msmartmips generate smartmips instructions\n\
14806-mno-smartmips do not generate smartmips instructions\n"));
14807 fprintf (stream, _("\
74cd071d
CF
14808-mdsp generate DSP instructions\n\
14809-mno-dsp do not generate DSP instructions\n"));
14810 fprintf (stream, _("\
ef2e4d86
CF
14811-mmt generate MT instructions\n\
14812-mno-mt do not generate MT instructions\n"));
14813 fprintf (stream, _("\
d766e8ec 14814-mfix-vr4120 work around certain VR4120 errata\n\
7d8e00cf 14815-mfix-vr4130 work around VR4130 mflo/mfhi errata\n\
316f5878
RS
14816-mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
14817-mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
aed1a261
RS
14818-mno-shared optimize output for executables\n\
14819-msym32 assume all symbols have 32-bit values\n\
316f5878
RS
14820-O0 remove unneeded NOPs, do not swap branches\n\
14821-O remove unneeded NOPs and swap branches\n\
316f5878
RS
14822--[no-]construct-floats [dis]allow floating point values to be constructed\n\
14823--trap, --no-break trap exception on div by 0 and mult overflow\n\
14824--break, --no-trap break exception on div by 0 and mult overflow\n"));
14825#ifdef OBJ_ELF
14826 fprintf (stream, _("\
14827-KPIC, -call_shared generate SVR4 position independent code\n\
14828-non_shared do not generate position independent code\n\
14829-xgot assume a 32 bit GOT\n\
dcd410fe 14830-mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
bbe506e8
TS
14831-mshared, -mno-shared disable/enable .cpload optimization for\n\
14832 non-shared code\n\
316f5878
RS
14833-mabi=ABI create ABI conformant object file for:\n"));
14834
14835 first = 1;
14836
14837 show (stream, "32", &column, &first);
14838 show (stream, "o64", &column, &first);
14839 show (stream, "n32", &column, &first);
14840 show (stream, "64", &column, &first);
14841 show (stream, "eabi", &column, &first);
14842
14843 fputc ('\n', stream);
14844
14845 fprintf (stream, _("\
14846-32 create o32 ABI object file (default)\n\
14847-n32 create n32 ABI object file\n\
14848-64 create 64 ABI object file\n"));
14849#endif
e7af610e 14850}
14e777e0
KB
14851
14852enum dwarf2_format
17a2f251 14853mips_dwarf2_format (void)
14e777e0
KB
14854{
14855 if (mips_abi == N64_ABI)
1de5b6a1
AO
14856 {
14857#ifdef TE_IRIX
14858 return dwarf2_format_64bit_irix;
14859#else
14860 return dwarf2_format_64bit;
14861#endif
14862 }
14e777e0
KB
14863 else
14864 return dwarf2_format_32bit;
14865}
73369e65
EC
14866
14867int
14868mips_dwarf2_addr_size (void)
14869{
14870 if (mips_abi == N64_ABI)
14871 return 8;
73369e65
EC
14872 else
14873 return 4;
14874}
5862107c
EC
14875
14876/* Standard calling conventions leave the CFA at SP on entry. */
14877void
14878mips_cfi_frame_initial_instructions (void)
14879{
14880 cfi_add_CFA_def_cfa_register (SP);
14881}
14882
707bfff6
TS
14883int
14884tc_mips_regname_to_dw2regnum (char *regname)
14885{
14886 unsigned int regnum = -1;
14887 unsigned int reg;
14888
14889 if (reg_lookup (&regname, RTYPE_GP | RTYPE_NUM, &reg))
14890 regnum = reg;
14891
14892 return regnum;
14893}
This page took 1.580706 seconds and 4 git commands to generate.