* elf32-s390.c (invalid_tls_insn): Call bfd_set_error.
[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,
f17c130b 3 2003, 2004, 2005 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 31#include <stdarg.h>
252b5132
RH
32
33#include "opcode/mips.h"
34#include "itbl-ops.h"
c5dd6aab 35#include "dwarf2dbg.h"
5862107c 36#include "dw2gencfi.h"
252b5132
RH
37
38#ifdef DEBUG
39#define DBG(x) printf x
40#else
41#define DBG(x)
42#endif
43
44#ifdef OBJ_MAYBE_ELF
45/* Clean up namespace so we can include obj-elf.h too. */
17a2f251
TS
46static int mips_output_flavor (void);
47static int mips_output_flavor (void) { return OUTPUT_FLAVOR; }
252b5132
RH
48#undef OBJ_PROCESS_STAB
49#undef OUTPUT_FLAVOR
50#undef S_GET_ALIGN
51#undef S_GET_SIZE
52#undef S_SET_ALIGN
53#undef S_SET_SIZE
252b5132
RH
54#undef obj_frob_file
55#undef obj_frob_file_after_relocs
56#undef obj_frob_symbol
57#undef obj_pop_insert
58#undef obj_sec_sym_ok_for_reloc
59#undef OBJ_COPY_SYMBOL_ATTRIBUTES
60
61#include "obj-elf.h"
62/* Fix any of them that we actually care about. */
63#undef OUTPUT_FLAVOR
64#define OUTPUT_FLAVOR mips_output_flavor()
65#endif
66
67#if defined (OBJ_ELF)
68#include "elf/mips.h"
69#endif
70
71#ifndef ECOFF_DEBUGGING
72#define NO_ECOFF_DEBUGGING
73#define ECOFF_DEBUGGING 0
74#endif
75
ecb4347a
DJ
76int mips_flag_mdebug = -1;
77
dcd410fe
RO
78/* Control generation of .pdr sections. Off by default on IRIX: the native
79 linker doesn't know about and discards them, but relocations against them
80 remain, leading to rld crashes. */
81#ifdef TE_IRIX
82int mips_flag_pdr = FALSE;
83#else
84int mips_flag_pdr = TRUE;
85#endif
86
252b5132
RH
87#include "ecoff.h"
88
89#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
90static char *mips_regmask_frag;
91#endif
92
85b51719 93#define ZERO 0
252b5132
RH
94#define AT 1
95#define TREG 24
96#define PIC_CALL_REG 25
97#define KT0 26
98#define KT1 27
99#define GP 28
100#define SP 29
101#define FP 30
102#define RA 31
103
104#define ILLEGAL_REG (32)
105
106/* Allow override of standard little-endian ECOFF format. */
107
108#ifndef ECOFF_LITTLE_FORMAT
109#define ECOFF_LITTLE_FORMAT "ecoff-littlemips"
110#endif
111
112extern int target_big_endian;
113
252b5132 114/* The name of the readonly data section. */
4d0d148d 115#define RDATA_SECTION_NAME (OUTPUT_FLAVOR == bfd_target_ecoff_flavour \
252b5132 116 ? ".rdata" \
056350c6
NC
117 : OUTPUT_FLAVOR == bfd_target_coff_flavour \
118 ? ".rdata" \
252b5132
RH
119 : OUTPUT_FLAVOR == bfd_target_elf_flavour \
120 ? ".rodata" \
121 : (abort (), ""))
122
47e39b9d
RS
123/* Information about an instruction, including its format, operands
124 and fixups. */
125struct mips_cl_insn
126{
127 /* The opcode's entry in mips_opcodes or mips16_opcodes. */
128 const struct mips_opcode *insn_mo;
129
130 /* True if this is a mips16 instruction and if we want the extended
131 form of INSN_MO. */
132 bfd_boolean use_extend;
133
134 /* The 16-bit extension instruction to use when USE_EXTEND is true. */
135 unsigned short extend;
136
137 /* The 16-bit or 32-bit bitstring of the instruction itself. This is
138 a copy of INSN_MO->match with the operands filled in. */
139 unsigned long insn_opcode;
140
141 /* The frag that contains the instruction. */
142 struct frag *frag;
143
144 /* The offset into FRAG of the first instruction byte. */
145 long where;
146
147 /* The relocs associated with the instruction, if any. */
148 fixS *fixp[3];
149
a38419a5
RS
150 /* True if this entry cannot be moved from its current position. */
151 unsigned int fixed_p : 1;
47e39b9d 152
708587a4 153 /* True if this instruction occurred in a .set noreorder block. */
47e39b9d
RS
154 unsigned int noreorder_p : 1;
155
2fa15973
RS
156 /* True for mips16 instructions that jump to an absolute address. */
157 unsigned int mips16_absolute_jump_p : 1;
47e39b9d
RS
158};
159
a325df1d
TS
160/* The ABI to use. */
161enum mips_abi_level
162{
163 NO_ABI = 0,
164 O32_ABI,
165 O64_ABI,
166 N32_ABI,
167 N64_ABI,
168 EABI_ABI
169};
170
171/* MIPS ABI we are using for this output file. */
316f5878 172static enum mips_abi_level mips_abi = NO_ABI;
a325df1d 173
143d77c5
EC
174/* Whether or not we have code that can call pic code. */
175int mips_abicalls = FALSE;
176
aa6975fb
ILT
177/* Whether or not we have code which can be put into a shared
178 library. */
179static bfd_boolean mips_in_shared = TRUE;
180
252b5132
RH
181/* This is the set of options which may be modified by the .set
182 pseudo-op. We use a struct so that .set push and .set pop are more
183 reliable. */
184
e972090a
NC
185struct mips_set_options
186{
252b5132
RH
187 /* MIPS ISA (Instruction Set Architecture) level. This is set to -1
188 if it has not been initialized. Changed by `.set mipsN', and the
189 -mipsN command line option, and the default CPU. */
190 int isa;
1f25f5d3
CD
191 /* Enabled Application Specific Extensions (ASEs). These are set to -1
192 if they have not been initialized. Changed by `.set <asename>', by
193 command line options, and based on the default architecture. */
194 int ase_mips3d;
deec1734 195 int ase_mdmx;
74cd071d 196 int ase_dsp;
ef2e4d86 197 int ase_mt;
252b5132
RH
198 /* Whether we are assembling for the mips16 processor. 0 if we are
199 not, 1 if we are, and -1 if the value has not been initialized.
200 Changed by `.set mips16' and `.set nomips16', and the -mips16 and
201 -nomips16 command line options, and the default CPU. */
202 int mips16;
203 /* Non-zero if we should not reorder instructions. Changed by `.set
204 reorder' and `.set noreorder'. */
205 int noreorder;
206 /* Non-zero if we should not permit the $at ($1) register to be used
207 in instructions. Changed by `.set at' and `.set noat'. */
208 int noat;
209 /* Non-zero if we should warn when a macro instruction expands into
210 more than one machine instruction. Changed by `.set nomacro' and
211 `.set macro'. */
212 int warn_about_macros;
213 /* Non-zero if we should not move instructions. Changed by `.set
214 move', `.set volatile', `.set nomove', and `.set novolatile'. */
215 int nomove;
216 /* Non-zero if we should not optimize branches by moving the target
217 of the branch into the delay slot. Actually, we don't perform
218 this optimization anyhow. Changed by `.set bopt' and `.set
219 nobopt'. */
220 int nobopt;
221 /* Non-zero if we should not autoextend mips16 instructions.
222 Changed by `.set autoextend' and `.set noautoextend'. */
223 int noautoextend;
a325df1d
TS
224 /* Restrict general purpose registers and floating point registers
225 to 32 bit. This is initially determined when -mgp32 or -mfp32
226 is passed but can changed if the assembler code uses .set mipsN. */
227 int gp32;
228 int fp32;
fef14a42
TS
229 /* MIPS architecture (CPU) type. Changed by .set arch=FOO, the -march
230 command line option, and the default CPU. */
231 int arch;
aed1a261
RS
232 /* True if ".set sym32" is in effect. */
233 bfd_boolean sym32;
252b5132
RH
234};
235
a325df1d 236/* True if -mgp32 was passed. */
a8e8e863 237static int file_mips_gp32 = -1;
a325df1d
TS
238
239/* True if -mfp32 was passed. */
a8e8e863 240static int file_mips_fp32 = -1;
a325df1d 241
252b5132 242/* This is the struct we use to hold the current set of options. Note
a4672219 243 that we must set the isa field to ISA_UNKNOWN and the ASE fields to
e7af610e 244 -1 to indicate that they have not been initialized. */
252b5132 245
e972090a
NC
246static struct mips_set_options mips_opts =
247{
ef2e4d86 248 ISA_UNKNOWN, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, CPU_UNKNOWN, FALSE
e7af610e 249};
252b5132
RH
250
251/* These variables are filled in with the masks of registers used.
252 The object format code reads them and puts them in the appropriate
253 place. */
254unsigned long mips_gprmask;
255unsigned long mips_cprmask[4];
256
257/* MIPS ISA we are using for this output file. */
e7af610e 258static int file_mips_isa = ISA_UNKNOWN;
252b5132 259
a4672219
TS
260/* True if -mips16 was passed or implied by arguments passed on the
261 command line (e.g., by -march). */
262static int file_ase_mips16;
263
1f25f5d3
CD
264/* True if -mips3d was passed or implied by arguments passed on the
265 command line (e.g., by -march). */
266static int file_ase_mips3d;
267
deec1734
CD
268/* True if -mdmx was passed or implied by arguments passed on the
269 command line (e.g., by -march). */
270static int file_ase_mdmx;
271
74cd071d
CF
272/* True if -mdsp was passed or implied by arguments passed on the
273 command line (e.g., by -march). */
274static int file_ase_dsp;
275
ef2e4d86
CF
276/* True if -mmt was passed or implied by arguments passed on the
277 command line (e.g., by -march). */
278static int file_ase_mt;
279
ec68c924 280/* The argument of the -march= flag. The architecture we are assembling. */
fef14a42 281static int file_mips_arch = CPU_UNKNOWN;
316f5878 282static const char *mips_arch_string;
ec68c924
EC
283
284/* The argument of the -mtune= flag. The architecture for which we
285 are optimizing. */
286static int mips_tune = CPU_UNKNOWN;
316f5878 287static const char *mips_tune_string;
ec68c924 288
316f5878 289/* True when generating 32-bit code for a 64-bit processor. */
252b5132
RH
290static int mips_32bitmode = 0;
291
316f5878
RS
292/* True if the given ABI requires 32-bit registers. */
293#define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
294
295/* Likewise 64-bit registers. */
296#define ABI_NEEDS_64BIT_REGS(ABI) \
297 ((ABI) == N32_ABI \
298 || (ABI) == N64_ABI \
299 || (ABI) == O64_ABI)
300
bdaaa2e1 301/* Return true if ISA supports 64 bit gp register instructions. */
9ce8a5dd 302#define ISA_HAS_64BIT_REGS(ISA) ( \
e7af610e
NC
303 (ISA) == ISA_MIPS3 \
304 || (ISA) == ISA_MIPS4 \
84ea6cf2 305 || (ISA) == ISA_MIPS5 \
d1cf510e 306 || (ISA) == ISA_MIPS64 \
5f74bc13 307 || (ISA) == ISA_MIPS64R2 \
9ce8a5dd
GRK
308 )
309
af7ee8bf
CD
310/* Return true if ISA supports 64-bit right rotate (dror et al.)
311 instructions. */
312#define ISA_HAS_DROR(ISA) ( \
5f74bc13 313 (ISA) == ISA_MIPS64R2 \
af7ee8bf
CD
314 )
315
316/* Return true if ISA supports 32-bit right rotate (ror et al.)
317 instructions. */
318#define ISA_HAS_ROR(ISA) ( \
319 (ISA) == ISA_MIPS32R2 \
5f74bc13 320 || (ISA) == ISA_MIPS64R2 \
af7ee8bf
CD
321 )
322
e013f690 323#define HAVE_32BIT_GPRS \
316f5878 324 (mips_opts.gp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
ca4e0257 325
e013f690 326#define HAVE_32BIT_FPRS \
316f5878 327 (mips_opts.fp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
ca4e0257
RS
328
329#define HAVE_64BIT_GPRS (! HAVE_32BIT_GPRS)
330#define HAVE_64BIT_FPRS (! HAVE_32BIT_FPRS)
331
316f5878 332#define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
e013f690 333
316f5878 334#define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
e013f690 335
3b91255e
RS
336/* True if relocations are stored in-place. */
337#define HAVE_IN_PLACE_ADDENDS (!HAVE_NEWABI)
338
aed1a261
RS
339/* The ABI-derived address size. */
340#define HAVE_64BIT_ADDRESSES \
341 (HAVE_64BIT_GPRS && (mips_abi == EABI_ABI || mips_abi == N64_ABI))
342#define HAVE_32BIT_ADDRESSES (!HAVE_64BIT_ADDRESSES)
e013f690 343
aed1a261
RS
344/* The size of symbolic constants (i.e., expressions of the form
345 "SYMBOL" or "SYMBOL + OFFSET"). */
346#define HAVE_32BIT_SYMBOLS \
347 (HAVE_32BIT_ADDRESSES || !HAVE_64BIT_OBJECTS || mips_opts.sym32)
348#define HAVE_64BIT_SYMBOLS (!HAVE_32BIT_SYMBOLS)
ca4e0257 349
b7c7d6c1
TS
350/* Addresses are loaded in different ways, depending on the address size
351 in use. The n32 ABI Documentation also mandates the use of additions
352 with overflow checking, but existing implementations don't follow it. */
f899b4b8 353#define ADDRESS_ADD_INSN \
b7c7d6c1 354 (HAVE_32BIT_ADDRESSES ? "addu" : "daddu")
f899b4b8
TS
355
356#define ADDRESS_ADDI_INSN \
b7c7d6c1 357 (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu")
f899b4b8
TS
358
359#define ADDRESS_LOAD_INSN \
360 (HAVE_32BIT_ADDRESSES ? "lw" : "ld")
361
362#define ADDRESS_STORE_INSN \
363 (HAVE_32BIT_ADDRESSES ? "sw" : "sd")
364
a4672219 365/* Return true if the given CPU supports the MIPS16 ASE. */
3396de36
TS
366#define CPU_HAS_MIPS16(cpu) \
367 (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0 \
368 || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
a4672219 369
1f25f5d3
CD
370/* Return true if the given CPU supports the MIPS3D ASE. */
371#define CPU_HAS_MIPS3D(cpu) ((cpu) == CPU_SB1 \
372 )
373
deec1734 374/* Return true if the given CPU supports the MDMX ASE. */
b34976b6 375#define CPU_HAS_MDMX(cpu) (FALSE \
deec1734
CD
376 )
377
74cd071d
CF
378/* Return true if the given CPU supports the DSP ASE. */
379#define CPU_HAS_DSP(cpu) (FALSE \
380 )
381
ef2e4d86
CF
382/* Return true if the given CPU supports the MT ASE. */
383#define CPU_HAS_MT(cpu) (FALSE \
384 )
385
60b63b72
RS
386/* True if CPU has a dror instruction. */
387#define CPU_HAS_DROR(CPU) ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
388
389/* True if CPU has a ror instruction. */
390#define CPU_HAS_ROR(CPU) CPU_HAS_DROR (CPU)
391
c8978940
CD
392/* True if mflo and mfhi can be immediately followed by instructions
393 which write to the HI and LO registers.
394
395 According to MIPS specifications, MIPS ISAs I, II, and III need
396 (at least) two instructions between the reads of HI/LO and
397 instructions which write them, and later ISAs do not. Contradicting
398 the MIPS specifications, some MIPS IV processor user manuals (e.g.
399 the UM for the NEC Vr5000) document needing the instructions between
400 HI/LO reads and writes, as well. Therefore, we declare only MIPS32,
401 MIPS64 and later ISAs to have the interlocks, plus any specific
402 earlier-ISA CPUs for which CPU documentation declares that the
403 instructions are really interlocked. */
404#define hilo_interlocks \
405 (mips_opts.isa == ISA_MIPS32 \
406 || mips_opts.isa == ISA_MIPS32R2 \
407 || mips_opts.isa == ISA_MIPS64 \
408 || mips_opts.isa == ISA_MIPS64R2 \
409 || mips_opts.arch == CPU_R4010 \
410 || mips_opts.arch == CPU_R10000 \
411 || mips_opts.arch == CPU_R12000 \
412 || mips_opts.arch == CPU_RM7000 \
c8978940
CD
413 || mips_opts.arch == CPU_VR5500 \
414 )
252b5132
RH
415
416/* Whether the processor uses hardware interlocks to protect reads
81912461
ILT
417 from the GPRs after they are loaded from memory, and thus does not
418 require nops to be inserted. This applies to instructions marked
419 INSN_LOAD_MEMORY_DELAY. These nops are only required at MIPS ISA
420 level I. */
252b5132 421#define gpr_interlocks \
e7af610e 422 (mips_opts.isa != ISA_MIPS1 \
fef14a42 423 || mips_opts.arch == CPU_R3900)
252b5132 424
81912461
ILT
425/* Whether the processor uses hardware interlocks to avoid delays
426 required by coprocessor instructions, and thus does not require
427 nops to be inserted. This applies to instructions marked
428 INSN_LOAD_COPROC_DELAY, INSN_COPROC_MOVE_DELAY, and to delays
429 between instructions marked INSN_WRITE_COND_CODE and ones marked
430 INSN_READ_COND_CODE. These nops are only required at MIPS ISA
431 levels I, II, and III. */
bdaaa2e1 432/* Itbl support may require additional care here. */
81912461
ILT
433#define cop_interlocks \
434 ((mips_opts.isa != ISA_MIPS1 \
435 && mips_opts.isa != ISA_MIPS2 \
436 && mips_opts.isa != ISA_MIPS3) \
437 || mips_opts.arch == CPU_R4300 \
81912461
ILT
438 )
439
440/* Whether the processor uses hardware interlocks to protect reads
441 from coprocessor registers after they are loaded from memory, and
442 thus does not require nops to be inserted. This applies to
443 instructions marked INSN_COPROC_MEMORY_DELAY. These nops are only
444 requires at MIPS ISA level I. */
445#define cop_mem_interlocks (mips_opts.isa != ISA_MIPS1)
252b5132 446
6b76fefe
CM
447/* Is this a mfhi or mflo instruction? */
448#define MF_HILO_INSN(PINFO) \
449 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
450
252b5132
RH
451/* MIPS PIC level. */
452
a161fe53 453enum mips_pic_level mips_pic;
252b5132 454
c9914766 455/* 1 if we should generate 32 bit offsets from the $gp register in
252b5132 456 SVR4_PIC mode. Currently has no meaning in other modes. */
c9914766 457static int mips_big_got = 0;
252b5132
RH
458
459/* 1 if trap instructions should used for overflow rather than break
460 instructions. */
c9914766 461static int mips_trap = 0;
252b5132 462
119d663a 463/* 1 if double width floating point constants should not be constructed
b6ff326e 464 by assembling two single width halves into two single width floating
119d663a
NC
465 point registers which just happen to alias the double width destination
466 register. On some architectures this aliasing can be disabled by a bit
d547a75e 467 in the status register, and the setting of this bit cannot be determined
119d663a
NC
468 automatically at assemble time. */
469static int mips_disable_float_construction;
470
252b5132
RH
471/* Non-zero if any .set noreorder directives were used. */
472
473static int mips_any_noreorder;
474
6b76fefe
CM
475/* Non-zero if nops should be inserted when the register referenced in
476 an mfhi/mflo instruction is read in the next two instructions. */
477static int mips_7000_hilo_fix;
478
252b5132 479/* The size of the small data section. */
156c2f8b 480static unsigned int g_switch_value = 8;
252b5132
RH
481/* Whether the -G option was used. */
482static int g_switch_seen = 0;
483
484#define N_RMASK 0xc4
485#define N_VFP 0xd4
486
487/* If we can determine in advance that GP optimization won't be
488 possible, we can skip the relaxation stuff that tries to produce
489 GP-relative references. This makes delay slot optimization work
490 better.
491
492 This function can only provide a guess, but it seems to work for
fba2b7f9
GK
493 gcc output. It needs to guess right for gcc, otherwise gcc
494 will put what it thinks is a GP-relative instruction in a branch
495 delay slot.
252b5132
RH
496
497 I don't know if a fix is needed for the SVR4_PIC mode. I've only
498 fixed it for the non-PIC mode. KR 95/04/07 */
17a2f251 499static int nopic_need_relax (symbolS *, int);
252b5132
RH
500
501/* handle of the OPCODE hash table */
502static struct hash_control *op_hash = NULL;
503
504/* The opcode hash table we use for the mips16. */
505static struct hash_control *mips16_op_hash = NULL;
506
507/* This array holds the chars that always start a comment. If the
508 pre-processor is disabled, these aren't very useful */
509const char comment_chars[] = "#";
510
511/* This array holds the chars that only start a comment at the beginning of
512 a line. If the line seems to have the form '# 123 filename'
513 .line and .file directives will appear in the pre-processed output */
514/* Note that input_file.c hand checks for '#' at the beginning of the
515 first line of the input file. This is because the compiler outputs
bdaaa2e1 516 #NO_APP at the beginning of its output. */
252b5132
RH
517/* Also note that C style comments are always supported. */
518const char line_comment_chars[] = "#";
519
bdaaa2e1 520/* This array holds machine specific line separator characters. */
63a0b638 521const char line_separator_chars[] = ";";
252b5132
RH
522
523/* Chars that can be used to separate mant from exp in floating point nums */
524const char EXP_CHARS[] = "eE";
525
526/* Chars that mean this number is a floating point constant */
527/* As in 0f12.456 */
528/* or 0d1.2345e12 */
529const char FLT_CHARS[] = "rRsSfFdDxXpP";
530
531/* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
532 changed in read.c . Ideally it shouldn't have to know about it at all,
533 but nothing is ideal around here.
534 */
535
536static char *insn_error;
537
538static int auto_align = 1;
539
540/* When outputting SVR4 PIC code, the assembler needs to know the
541 offset in the stack frame from which to restore the $gp register.
542 This is set by the .cprestore pseudo-op, and saved in this
543 variable. */
544static offsetT mips_cprestore_offset = -1;
545
67c1ffbe 546/* Similar for NewABI PIC code, where $gp is callee-saved. NewABI has some
6478892d 547 more optimizations, it can use a register value instead of a memory-saved
956cd1d6 548 offset and even an other register than $gp as global pointer. */
6478892d
TS
549static offsetT mips_cpreturn_offset = -1;
550static int mips_cpreturn_register = -1;
551static int mips_gp_register = GP;
def2e0dd 552static int mips_gprel_offset = 0;
6478892d 553
7a621144
DJ
554/* Whether mips_cprestore_offset has been set in the current function
555 (or whether it has already been warned about, if not). */
556static int mips_cprestore_valid = 0;
557
252b5132
RH
558/* This is the register which holds the stack frame, as set by the
559 .frame pseudo-op. This is needed to implement .cprestore. */
560static int mips_frame_reg = SP;
561
7a621144
DJ
562/* Whether mips_frame_reg has been set in the current function
563 (or whether it has already been warned about, if not). */
564static int mips_frame_reg_valid = 0;
565
252b5132
RH
566/* To output NOP instructions correctly, we need to keep information
567 about the previous two instructions. */
568
569/* Whether we are optimizing. The default value of 2 means to remove
570 unneeded NOPs and swap branch instructions when possible. A value
571 of 1 means to not swap branches. A value of 0 means to always
572 insert NOPs. */
573static int mips_optimize = 2;
574
575/* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
576 equivalent to seeing no -g option at all. */
577static int mips_debug = 0;
578
7d8e00cf
RS
579/* The maximum number of NOPs needed to avoid the VR4130 mflo/mfhi errata. */
580#define MAX_VR4130_NOPS 4
581
582/* The maximum number of NOPs needed to fill delay slots. */
583#define MAX_DELAY_NOPS 2
584
585/* The maximum number of NOPs needed for any purpose. */
586#define MAX_NOPS 4
71400594
RS
587
588/* A list of previous instructions, with index 0 being the most recent.
589 We need to look back MAX_NOPS instructions when filling delay slots
590 or working around processor errata. We need to look back one
591 instruction further if we're thinking about using history[0] to
592 fill a branch delay slot. */
593static struct mips_cl_insn history[1 + MAX_NOPS];
252b5132 594
1e915849
RS
595/* Nop instructions used by emit_nop. */
596static struct mips_cl_insn nop_insn, mips16_nop_insn;
597
598/* The appropriate nop for the current mode. */
599#define NOP_INSN (mips_opts.mips16 ? &mips16_nop_insn : &nop_insn)
252b5132 600
252b5132
RH
601/* If this is set, it points to a frag holding nop instructions which
602 were inserted before the start of a noreorder section. If those
603 nops turn out to be unnecessary, the size of the frag can be
604 decreased. */
605static fragS *prev_nop_frag;
606
607/* The number of nop instructions we created in prev_nop_frag. */
608static int prev_nop_frag_holds;
609
610/* The number of nop instructions that we know we need in
bdaaa2e1 611 prev_nop_frag. */
252b5132
RH
612static int prev_nop_frag_required;
613
614/* The number of instructions we've seen since prev_nop_frag. */
615static int prev_nop_frag_since;
616
617/* For ECOFF and ELF, relocations against symbols are done in two
618 parts, with a HI relocation and a LO relocation. Each relocation
619 has only 16 bits of space to store an addend. This means that in
620 order for the linker to handle carries correctly, it must be able
621 to locate both the HI and the LO relocation. This means that the
622 relocations must appear in order in the relocation table.
623
624 In order to implement this, we keep track of each unmatched HI
625 relocation. We then sort them so that they immediately precede the
bdaaa2e1 626 corresponding LO relocation. */
252b5132 627
e972090a
NC
628struct mips_hi_fixup
629{
252b5132
RH
630 /* Next HI fixup. */
631 struct mips_hi_fixup *next;
632 /* This fixup. */
633 fixS *fixp;
634 /* The section this fixup is in. */
635 segT seg;
636};
637
638/* The list of unmatched HI relocs. */
639
640static struct mips_hi_fixup *mips_hi_fixup_list;
641
64bdfcaf
RS
642/* The frag containing the last explicit relocation operator.
643 Null if explicit relocations have not been used. */
644
645static fragS *prev_reloc_op_frag;
646
252b5132
RH
647/* Map normal MIPS register numbers to mips16 register numbers. */
648
649#define X ILLEGAL_REG
e972090a
NC
650static const int mips32_to_16_reg_map[] =
651{
252b5132
RH
652 X, X, 2, 3, 4, 5, 6, 7,
653 X, X, X, X, X, X, X, X,
654 0, 1, X, X, X, X, X, X,
655 X, X, X, X, X, X, X, X
656};
657#undef X
658
659/* Map mips16 register numbers to normal MIPS register numbers. */
660
e972090a
NC
661static const unsigned int mips16_to_32_reg_map[] =
662{
252b5132
RH
663 16, 17, 2, 3, 4, 5, 6, 7
664};
60b63b72 665
71400594
RS
666/* Classifies the kind of instructions we're interested in when
667 implementing -mfix-vr4120. */
668enum fix_vr4120_class {
669 FIX_VR4120_MACC,
670 FIX_VR4120_DMACC,
671 FIX_VR4120_MULT,
672 FIX_VR4120_DMULT,
673 FIX_VR4120_DIV,
674 FIX_VR4120_MTHILO,
675 NUM_FIX_VR4120_CLASSES
676};
677
678/* Given two FIX_VR4120_* values X and Y, bit Y of element X is set if
679 there must be at least one other instruction between an instruction
680 of type X and an instruction of type Y. */
681static unsigned int vr4120_conflicts[NUM_FIX_VR4120_CLASSES];
682
683/* True if -mfix-vr4120 is in force. */
d766e8ec 684static int mips_fix_vr4120;
4a6a3df4 685
7d8e00cf
RS
686/* ...likewise -mfix-vr4130. */
687static int mips_fix_vr4130;
688
4a6a3df4
AO
689/* We don't relax branches by default, since this causes us to expand
690 `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
691 fail to compute the offset before expanding the macro to the most
692 efficient expansion. */
693
694static int mips_relax_branch;
252b5132 695\f
4d7206a2
RS
696/* The expansion of many macros depends on the type of symbol that
697 they refer to. For example, when generating position-dependent code,
698 a macro that refers to a symbol may have two different expansions,
699 one which uses GP-relative addresses and one which uses absolute
700 addresses. When generating SVR4-style PIC, a macro may have
701 different expansions for local and global symbols.
702
703 We handle these situations by generating both sequences and putting
704 them in variant frags. In position-dependent code, the first sequence
705 will be the GP-relative one and the second sequence will be the
706 absolute one. In SVR4 PIC, the first sequence will be for global
707 symbols and the second will be for local symbols.
708
584892a6
RS
709 The frag's "subtype" is RELAX_ENCODE (FIRST, SECOND), where FIRST and
710 SECOND are the lengths of the two sequences in bytes. These fields
711 can be extracted using RELAX_FIRST() and RELAX_SECOND(). In addition,
712 the subtype has the following flags:
4d7206a2 713
584892a6
RS
714 RELAX_USE_SECOND
715 Set if it has been decided that we should use the second
716 sequence instead of the first.
717
718 RELAX_SECOND_LONGER
719 Set in the first variant frag if the macro's second implementation
720 is longer than its first. This refers to the macro as a whole,
721 not an individual relaxation.
722
723 RELAX_NOMACRO
724 Set in the first variant frag if the macro appeared in a .set nomacro
725 block and if one alternative requires a warning but the other does not.
726
727 RELAX_DELAY_SLOT
728 Like RELAX_NOMACRO, but indicates that the macro appears in a branch
729 delay slot.
4d7206a2
RS
730
731 The frag's "opcode" points to the first fixup for relaxable code.
732
733 Relaxable macros are generated using a sequence such as:
734
735 relax_start (SYMBOL);
736 ... generate first expansion ...
737 relax_switch ();
738 ... generate second expansion ...
739 relax_end ();
740
741 The code and fixups for the unwanted alternative are discarded
742 by md_convert_frag. */
584892a6 743#define RELAX_ENCODE(FIRST, SECOND) (((FIRST) << 8) | (SECOND))
4d7206a2 744
584892a6
RS
745#define RELAX_FIRST(X) (((X) >> 8) & 0xff)
746#define RELAX_SECOND(X) ((X) & 0xff)
747#define RELAX_USE_SECOND 0x10000
748#define RELAX_SECOND_LONGER 0x20000
749#define RELAX_NOMACRO 0x40000
750#define RELAX_DELAY_SLOT 0x80000
252b5132 751
4a6a3df4
AO
752/* Branch without likely bit. If label is out of range, we turn:
753
754 beq reg1, reg2, label
755 delay slot
756
757 into
758
759 bne reg1, reg2, 0f
760 nop
761 j label
762 0: delay slot
763
764 with the following opcode replacements:
765
766 beq <-> bne
767 blez <-> bgtz
768 bltz <-> bgez
769 bc1f <-> bc1t
770
771 bltzal <-> bgezal (with jal label instead of j label)
772
773 Even though keeping the delay slot instruction in the delay slot of
774 the branch would be more efficient, it would be very tricky to do
775 correctly, because we'd have to introduce a variable frag *after*
776 the delay slot instruction, and expand that instead. Let's do it
777 the easy way for now, even if the branch-not-taken case now costs
778 one additional instruction. Out-of-range branches are not supposed
779 to be common, anyway.
780
781 Branch likely. If label is out of range, we turn:
782
783 beql reg1, reg2, label
784 delay slot (annulled if branch not taken)
785
786 into
787
788 beql reg1, reg2, 1f
789 nop
790 beql $0, $0, 2f
791 nop
792 1: j[al] label
793 delay slot (executed only if branch taken)
794 2:
795
796 It would be possible to generate a shorter sequence by losing the
797 likely bit, generating something like:
b34976b6 798
4a6a3df4
AO
799 bne reg1, reg2, 0f
800 nop
801 j[al] label
802 delay slot (executed only if branch taken)
803 0:
804
805 beql -> bne
806 bnel -> beq
807 blezl -> bgtz
808 bgtzl -> blez
809 bltzl -> bgez
810 bgezl -> bltz
811 bc1fl -> bc1t
812 bc1tl -> bc1f
813
814 bltzall -> bgezal (with jal label instead of j label)
815 bgezall -> bltzal (ditto)
816
817
818 but it's not clear that it would actually improve performance. */
af6ae2ad 819#define RELAX_BRANCH_ENCODE(uncond, likely, link, toofar) \
4a6a3df4
AO
820 ((relax_substateT) \
821 (0xc0000000 \
822 | ((toofar) ? 1 : 0) \
823 | ((link) ? 2 : 0) \
824 | ((likely) ? 4 : 0) \
af6ae2ad 825 | ((uncond) ? 8 : 0)))
4a6a3df4 826#define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
4a6a3df4
AO
827#define RELAX_BRANCH_UNCOND(i) (((i) & 8) != 0)
828#define RELAX_BRANCH_LIKELY(i) (((i) & 4) != 0)
829#define RELAX_BRANCH_LINK(i) (((i) & 2) != 0)
ae6063d4 830#define RELAX_BRANCH_TOOFAR(i) (((i) & 1) != 0)
4a6a3df4 831
252b5132
RH
832/* For mips16 code, we use an entirely different form of relaxation.
833 mips16 supports two versions of most instructions which take
834 immediate values: a small one which takes some small value, and a
835 larger one which takes a 16 bit value. Since branches also follow
836 this pattern, relaxing these values is required.
837
838 We can assemble both mips16 and normal MIPS code in a single
839 object. Therefore, we need to support this type of relaxation at
840 the same time that we support the relaxation described above. We
841 use the high bit of the subtype field to distinguish these cases.
842
843 The information we store for this type of relaxation is the
844 argument code found in the opcode file for this relocation, whether
845 the user explicitly requested a small or extended form, and whether
846 the relocation is in a jump or jal delay slot. That tells us the
847 size of the value, and how it should be stored. We also store
848 whether the fragment is considered to be extended or not. We also
849 store whether this is known to be a branch to a different section,
850 whether we have tried to relax this frag yet, and whether we have
851 ever extended a PC relative fragment because of a shift count. */
852#define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
853 (0x80000000 \
854 | ((type) & 0xff) \
855 | ((small) ? 0x100 : 0) \
856 | ((ext) ? 0x200 : 0) \
857 | ((dslot) ? 0x400 : 0) \
858 | ((jal_dslot) ? 0x800 : 0))
4a6a3df4 859#define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
252b5132
RH
860#define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
861#define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
862#define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
863#define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
864#define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
865#define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
866#define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
867#define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
868#define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
869#define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
870#define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
885add95
CD
871
872/* Is the given value a sign-extended 32-bit value? */
873#define IS_SEXT_32BIT_NUM(x) \
874 (((x) &~ (offsetT) 0x7fffffff) == 0 \
875 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
876
877/* Is the given value a sign-extended 16-bit value? */
878#define IS_SEXT_16BIT_NUM(x) \
879 (((x) &~ (offsetT) 0x7fff) == 0 \
880 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
881
2051e8c4
MR
882/* Is the given value a zero-extended 32-bit value? Or a negated one? */
883#define IS_ZEXT_32BIT_NUM(x) \
884 (((x) &~ (offsetT) 0xffffffff) == 0 \
885 || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
886
bf12938e
RS
887/* Replace bits MASK << SHIFT of STRUCT with the equivalent bits in
888 VALUE << SHIFT. VALUE is evaluated exactly once. */
889#define INSERT_BITS(STRUCT, VALUE, MASK, SHIFT) \
890 (STRUCT) = (((STRUCT) & ~((MASK) << (SHIFT))) \
891 | (((VALUE) & (MASK)) << (SHIFT)))
892
893/* Extract bits MASK << SHIFT from STRUCT and shift them right
894 SHIFT places. */
895#define EXTRACT_BITS(STRUCT, MASK, SHIFT) \
896 (((STRUCT) >> (SHIFT)) & (MASK))
897
898/* Change INSN's opcode so that the operand given by FIELD has value VALUE.
899 INSN is a mips_cl_insn structure and VALUE is evaluated exactly once.
900
901 include/opcode/mips.h specifies operand fields using the macros
902 OP_MASK_<FIELD> and OP_SH_<FIELD>. The MIPS16 equivalents start
903 with "MIPS16OP" instead of "OP". */
904#define INSERT_OPERAND(FIELD, INSN, VALUE) \
905 INSERT_BITS ((INSN).insn_opcode, VALUE, OP_MASK_##FIELD, OP_SH_##FIELD)
906#define MIPS16_INSERT_OPERAND(FIELD, INSN, VALUE) \
907 INSERT_BITS ((INSN).insn_opcode, VALUE, \
908 MIPS16OP_MASK_##FIELD, MIPS16OP_SH_##FIELD)
909
910/* Extract the operand given by FIELD from mips_cl_insn INSN. */
911#define EXTRACT_OPERAND(FIELD, INSN) \
912 EXTRACT_BITS ((INSN).insn_opcode, OP_MASK_##FIELD, OP_SH_##FIELD)
913#define MIPS16_EXTRACT_OPERAND(FIELD, INSN) \
914 EXTRACT_BITS ((INSN).insn_opcode, \
915 MIPS16OP_MASK_##FIELD, \
916 MIPS16OP_SH_##FIELD)
4d7206a2
RS
917\f
918/* Global variables used when generating relaxable macros. See the
919 comment above RELAX_ENCODE for more details about how relaxation
920 is used. */
921static struct {
922 /* 0 if we're not emitting a relaxable macro.
923 1 if we're emitting the first of the two relaxation alternatives.
924 2 if we're emitting the second alternative. */
925 int sequence;
926
927 /* The first relaxable fixup in the current frag. (In other words,
928 the first fixup that refers to relaxable code.) */
929 fixS *first_fixup;
930
931 /* sizes[0] says how many bytes of the first alternative are stored in
932 the current frag. Likewise sizes[1] for the second alternative. */
933 unsigned int sizes[2];
934
935 /* The symbol on which the choice of sequence depends. */
936 symbolS *symbol;
937} mips_relax;
252b5132 938\f
584892a6
RS
939/* Global variables used to decide whether a macro needs a warning. */
940static struct {
941 /* True if the macro is in a branch delay slot. */
942 bfd_boolean delay_slot_p;
943
944 /* For relaxable macros, sizes[0] is the length of the first alternative
945 in bytes and sizes[1] is the length of the second alternative.
946 For non-relaxable macros, both elements give the length of the
947 macro in bytes. */
948 unsigned int sizes[2];
949
950 /* The first variant frag for this macro. */
951 fragS *first_frag;
952} mips_macro_warning;
953\f
252b5132
RH
954/* Prototypes for static functions. */
955
17a2f251 956#define internalError() \
252b5132 957 as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
252b5132
RH
958
959enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
960
b34976b6 961static void append_insn
4d7206a2 962 (struct mips_cl_insn *ip, expressionS *p, bfd_reloc_code_real_type *r);
7d10b47d 963static void mips_no_prev_insn (void);
b34976b6 964static void mips16_macro_build
67c0d1eb
RS
965 (expressionS *, const char *, const char *, va_list);
966static void load_register (int, expressionS *, int);
584892a6
RS
967static void macro_start (void);
968static void macro_end (void);
17a2f251
TS
969static void macro (struct mips_cl_insn * ip);
970static void mips16_macro (struct mips_cl_insn * ip);
252b5132 971#ifdef LOSING_COMPILER
17a2f251 972static void macro2 (struct mips_cl_insn * ip);
252b5132 973#endif
17a2f251
TS
974static void mips_ip (char *str, struct mips_cl_insn * ip);
975static void mips16_ip (char *str, struct mips_cl_insn * ip);
b34976b6 976static void mips16_immed
17a2f251
TS
977 (char *, unsigned int, int, offsetT, bfd_boolean, bfd_boolean, bfd_boolean,
978 unsigned long *, bfd_boolean *, unsigned short *);
5e0116d5 979static size_t my_getSmallExpression
17a2f251
TS
980 (expressionS *, bfd_reloc_code_real_type *, char *);
981static void my_getExpression (expressionS *, char *);
982static void s_align (int);
983static void s_change_sec (int);
984static void s_change_section (int);
985static void s_cons (int);
986static void s_float_cons (int);
987static void s_mips_globl (int);
988static void s_option (int);
989static void s_mipsset (int);
990static void s_abicalls (int);
991static void s_cpload (int);
992static void s_cpsetup (int);
993static void s_cplocal (int);
994static void s_cprestore (int);
995static void s_cpreturn (int);
996static void s_gpvalue (int);
997static void s_gpword (int);
998static void s_gpdword (int);
999static void s_cpadd (int);
1000static void s_insn (int);
1001static void md_obj_begin (void);
1002static void md_obj_end (void);
1003static void s_mips_ent (int);
1004static void s_mips_end (int);
1005static void s_mips_frame (int);
1006static void s_mips_mask (int reg_type);
1007static void s_mips_stab (int);
1008static void s_mips_weakext (int);
1009static void s_mips_file (int);
1010static void s_mips_loc (int);
1011static bfd_boolean pic_need_relax (symbolS *, asection *);
4a6a3df4 1012static int relaxed_branch_length (fragS *, asection *, int);
17a2f251 1013static int validate_mips_insn (const struct mips_opcode *);
e7af610e
NC
1014
1015/* Table and functions used to map between CPU/ISA names, and
1016 ISA levels, and CPU numbers. */
1017
e972090a
NC
1018struct mips_cpu_info
1019{
e7af610e
NC
1020 const char *name; /* CPU or ISA name. */
1021 int is_isa; /* Is this an ISA? (If 0, a CPU.) */
1022 int isa; /* ISA level. */
1023 int cpu; /* CPU number (default CPU if ISA). */
1024};
1025
17a2f251
TS
1026static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
1027static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
1028static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
252b5132
RH
1029\f
1030/* Pseudo-op table.
1031
1032 The following pseudo-ops from the Kane and Heinrich MIPS book
1033 should be defined here, but are currently unsupported: .alias,
1034 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
1035
1036 The following pseudo-ops from the Kane and Heinrich MIPS book are
1037 specific to the type of debugging information being generated, and
1038 should be defined by the object format: .aent, .begin, .bend,
1039 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
1040 .vreg.
1041
1042 The following pseudo-ops from the Kane and Heinrich MIPS book are
1043 not MIPS CPU specific, but are also not specific to the object file
1044 format. This file is probably the best place to define them, but
1045 they are not currently supported: .asm0, .endr, .lab, .repeat,
1046 .struct. */
1047
e972090a
NC
1048static const pseudo_typeS mips_pseudo_table[] =
1049{
beae10d5 1050 /* MIPS specific pseudo-ops. */
252b5132
RH
1051 {"option", s_option, 0},
1052 {"set", s_mipsset, 0},
1053 {"rdata", s_change_sec, 'r'},
1054 {"sdata", s_change_sec, 's'},
1055 {"livereg", s_ignore, 0},
1056 {"abicalls", s_abicalls, 0},
1057 {"cpload", s_cpload, 0},
6478892d
TS
1058 {"cpsetup", s_cpsetup, 0},
1059 {"cplocal", s_cplocal, 0},
252b5132 1060 {"cprestore", s_cprestore, 0},
6478892d
TS
1061 {"cpreturn", s_cpreturn, 0},
1062 {"gpvalue", s_gpvalue, 0},
252b5132 1063 {"gpword", s_gpword, 0},
10181a0d 1064 {"gpdword", s_gpdword, 0},
252b5132
RH
1065 {"cpadd", s_cpadd, 0},
1066 {"insn", s_insn, 0},
1067
beae10d5 1068 /* Relatively generic pseudo-ops that happen to be used on MIPS
252b5132
RH
1069 chips. */
1070 {"asciiz", stringer, 1},
1071 {"bss", s_change_sec, 'b'},
1072 {"err", s_err, 0},
1073 {"half", s_cons, 1},
1074 {"dword", s_cons, 3},
1075 {"weakext", s_mips_weakext, 0},
1076
beae10d5 1077 /* These pseudo-ops are defined in read.c, but must be overridden
252b5132
RH
1078 here for one reason or another. */
1079 {"align", s_align, 0},
1080 {"byte", s_cons, 0},
1081 {"data", s_change_sec, 'd'},
1082 {"double", s_float_cons, 'd'},
1083 {"float", s_float_cons, 'f'},
1084 {"globl", s_mips_globl, 0},
1085 {"global", s_mips_globl, 0},
1086 {"hword", s_cons, 1},
1087 {"int", s_cons, 2},
1088 {"long", s_cons, 2},
1089 {"octa", s_cons, 4},
1090 {"quad", s_cons, 3},
cca86cc8 1091 {"section", s_change_section, 0},
252b5132
RH
1092 {"short", s_cons, 1},
1093 {"single", s_float_cons, 'f'},
1094 {"stabn", s_mips_stab, 'n'},
1095 {"text", s_change_sec, 't'},
1096 {"word", s_cons, 2},
add56521 1097
add56521 1098 { "extern", ecoff_directive_extern, 0},
add56521 1099
43841e91 1100 { NULL, NULL, 0 },
252b5132
RH
1101};
1102
e972090a
NC
1103static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1104{
beae10d5
KH
1105 /* These pseudo-ops should be defined by the object file format.
1106 However, a.out doesn't support them, so we have versions here. */
252b5132
RH
1107 {"aent", s_mips_ent, 1},
1108 {"bgnb", s_ignore, 0},
1109 {"end", s_mips_end, 0},
1110 {"endb", s_ignore, 0},
1111 {"ent", s_mips_ent, 0},
c5dd6aab 1112 {"file", s_mips_file, 0},
252b5132
RH
1113 {"fmask", s_mips_mask, 'F'},
1114 {"frame", s_mips_frame, 0},
c5dd6aab 1115 {"loc", s_mips_loc, 0},
252b5132
RH
1116 {"mask", s_mips_mask, 'R'},
1117 {"verstamp", s_ignore, 0},
43841e91 1118 { NULL, NULL, 0 },
252b5132
RH
1119};
1120
17a2f251 1121extern void pop_insert (const pseudo_typeS *);
252b5132
RH
1122
1123void
17a2f251 1124mips_pop_insert (void)
252b5132
RH
1125{
1126 pop_insert (mips_pseudo_table);
1127 if (! ECOFF_DEBUGGING)
1128 pop_insert (mips_nonecoff_pseudo_table);
1129}
1130\f
1131/* Symbols labelling the current insn. */
1132
e972090a
NC
1133struct insn_label_list
1134{
252b5132
RH
1135 struct insn_label_list *next;
1136 symbolS *label;
1137};
1138
1139static struct insn_label_list *insn_labels;
1140static struct insn_label_list *free_insn_labels;
1141
17a2f251 1142static void mips_clear_insn_labels (void);
252b5132
RH
1143
1144static inline void
17a2f251 1145mips_clear_insn_labels (void)
252b5132
RH
1146{
1147 register struct insn_label_list **pl;
1148
1149 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1150 ;
1151 *pl = insn_labels;
1152 insn_labels = NULL;
1153}
1154\f
1155static char *expr_end;
1156
1157/* Expressions which appear in instructions. These are set by
1158 mips_ip. */
1159
1160static expressionS imm_expr;
5f74bc13 1161static expressionS imm2_expr;
252b5132
RH
1162static expressionS offset_expr;
1163
1164/* Relocs associated with imm_expr and offset_expr. */
1165
f6688943
TS
1166static bfd_reloc_code_real_type imm_reloc[3]
1167 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1168static bfd_reloc_code_real_type offset_reloc[3]
1169 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 1170
252b5132
RH
1171/* These are set by mips16_ip if an explicit extension is used. */
1172
b34976b6 1173static bfd_boolean mips16_small, mips16_ext;
252b5132 1174
7ed4a06a 1175#ifdef OBJ_ELF
ecb4347a
DJ
1176/* The pdr segment for per procedure frame/regmask info. Not used for
1177 ECOFF debugging. */
252b5132
RH
1178
1179static segT pdr_seg;
7ed4a06a 1180#endif
252b5132 1181
e013f690
TS
1182/* The default target format to use. */
1183
1184const char *
17a2f251 1185mips_target_format (void)
e013f690
TS
1186{
1187 switch (OUTPUT_FLAVOR)
1188 {
e013f690
TS
1189 case bfd_target_ecoff_flavour:
1190 return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
1191 case bfd_target_coff_flavour:
1192 return "pe-mips";
1193 case bfd_target_elf_flavour:
0a44bf69
RS
1194#ifdef TE_VXWORKS
1195 if (!HAVE_64BIT_OBJECTS && !HAVE_NEWABI)
1196 return (target_big_endian
1197 ? "elf32-bigmips-vxworks"
1198 : "elf32-littlemips-vxworks");
1199#endif
e013f690 1200#ifdef TE_TMIPS
cfe86eaa 1201 /* This is traditional mips. */
e013f690 1202 return (target_big_endian
cfe86eaa
TS
1203 ? (HAVE_64BIT_OBJECTS
1204 ? "elf64-tradbigmips"
1205 : (HAVE_NEWABI
1206 ? "elf32-ntradbigmips" : "elf32-tradbigmips"))
1207 : (HAVE_64BIT_OBJECTS
1208 ? "elf64-tradlittlemips"
1209 : (HAVE_NEWABI
1210 ? "elf32-ntradlittlemips" : "elf32-tradlittlemips")));
e013f690
TS
1211#else
1212 return (target_big_endian
cfe86eaa
TS
1213 ? (HAVE_64BIT_OBJECTS
1214 ? "elf64-bigmips"
1215 : (HAVE_NEWABI
1216 ? "elf32-nbigmips" : "elf32-bigmips"))
1217 : (HAVE_64BIT_OBJECTS
1218 ? "elf64-littlemips"
1219 : (HAVE_NEWABI
1220 ? "elf32-nlittlemips" : "elf32-littlemips")));
e013f690
TS
1221#endif
1222 default:
1223 abort ();
1224 return NULL;
1225 }
1226}
1227
1e915849
RS
1228/* Return the length of instruction INSN. */
1229
1230static inline unsigned int
1231insn_length (const struct mips_cl_insn *insn)
1232{
1233 if (!mips_opts.mips16)
1234 return 4;
1235 return insn->mips16_absolute_jump_p || insn->use_extend ? 4 : 2;
1236}
1237
1238/* Initialise INSN from opcode entry MO. Leave its position unspecified. */
1239
1240static void
1241create_insn (struct mips_cl_insn *insn, const struct mips_opcode *mo)
1242{
1243 size_t i;
1244
1245 insn->insn_mo = mo;
1246 insn->use_extend = FALSE;
1247 insn->extend = 0;
1248 insn->insn_opcode = mo->match;
1249 insn->frag = NULL;
1250 insn->where = 0;
1251 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
1252 insn->fixp[i] = NULL;
1253 insn->fixed_p = (mips_opts.noreorder > 0);
1254 insn->noreorder_p = (mips_opts.noreorder > 0);
1255 insn->mips16_absolute_jump_p = 0;
1256}
1257
1258/* Install INSN at the location specified by its "frag" and "where" fields. */
1259
1260static void
1261install_insn (const struct mips_cl_insn *insn)
1262{
1263 char *f = insn->frag->fr_literal + insn->where;
1264 if (!mips_opts.mips16)
1265 md_number_to_chars (f, insn->insn_opcode, 4);
1266 else if (insn->mips16_absolute_jump_p)
1267 {
1268 md_number_to_chars (f, insn->insn_opcode >> 16, 2);
1269 md_number_to_chars (f + 2, insn->insn_opcode & 0xffff, 2);
1270 }
1271 else
1272 {
1273 if (insn->use_extend)
1274 {
1275 md_number_to_chars (f, 0xf000 | insn->extend, 2);
1276 f += 2;
1277 }
1278 md_number_to_chars (f, insn->insn_opcode, 2);
1279 }
1280}
1281
1282/* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly
1283 and install the opcode in the new location. */
1284
1285static void
1286move_insn (struct mips_cl_insn *insn, fragS *frag, long where)
1287{
1288 size_t i;
1289
1290 insn->frag = frag;
1291 insn->where = where;
1292 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
1293 if (insn->fixp[i] != NULL)
1294 {
1295 insn->fixp[i]->fx_frag = frag;
1296 insn->fixp[i]->fx_where = where;
1297 }
1298 install_insn (insn);
1299}
1300
1301/* Add INSN to the end of the output. */
1302
1303static void
1304add_fixed_insn (struct mips_cl_insn *insn)
1305{
1306 char *f = frag_more (insn_length (insn));
1307 move_insn (insn, frag_now, f - frag_now->fr_literal);
1308}
1309
1310/* Start a variant frag and move INSN to the start of the variant part,
1311 marking it as fixed. The other arguments are as for frag_var. */
1312
1313static void
1314add_relaxed_insn (struct mips_cl_insn *insn, int max_chars, int var,
1315 relax_substateT subtype, symbolS *symbol, offsetT offset)
1316{
1317 frag_grow (max_chars);
1318 move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
1319 insn->fixed_p = 1;
1320 frag_var (rs_machine_dependent, max_chars, var,
1321 subtype, symbol, offset, NULL);
1322}
1323
1324/* Insert N copies of INSN into the history buffer, starting at
1325 position FIRST. Neither FIRST nor N need to be clipped. */
1326
1327static void
1328insert_into_history (unsigned int first, unsigned int n,
1329 const struct mips_cl_insn *insn)
1330{
1331 if (mips_relax.sequence != 2)
1332 {
1333 unsigned int i;
1334
1335 for (i = ARRAY_SIZE (history); i-- > first;)
1336 if (i >= first + n)
1337 history[i] = history[i - n];
1338 else
1339 history[i] = *insn;
1340 }
1341}
1342
1343/* Emit a nop instruction, recording it in the history buffer. */
1344
1345static void
1346emit_nop (void)
1347{
1348 add_fixed_insn (NOP_INSN);
1349 insert_into_history (0, 1, NOP_INSN);
1350}
1351
71400594
RS
1352/* Initialize vr4120_conflicts. There is a bit of duplication here:
1353 the idea is to make it obvious at a glance that each errata is
1354 included. */
1355
1356static void
1357init_vr4120_conflicts (void)
1358{
1359#define CONFLICT(FIRST, SECOND) \
1360 vr4120_conflicts[FIX_VR4120_##FIRST] |= 1 << FIX_VR4120_##SECOND
1361
1362 /* Errata 21 - [D]DIV[U] after [D]MACC */
1363 CONFLICT (MACC, DIV);
1364 CONFLICT (DMACC, DIV);
1365
1366 /* Errata 23 - Continuous DMULT[U]/DMACC instructions. */
1367 CONFLICT (DMULT, DMULT);
1368 CONFLICT (DMULT, DMACC);
1369 CONFLICT (DMACC, DMULT);
1370 CONFLICT (DMACC, DMACC);
1371
1372 /* Errata 24 - MT{LO,HI} after [D]MACC */
1373 CONFLICT (MACC, MTHILO);
1374 CONFLICT (DMACC, MTHILO);
1375
1376 /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
1377 instruction is executed immediately after a MACC or DMACC
1378 instruction, the result of [either instruction] is incorrect." */
1379 CONFLICT (MACC, MULT);
1380 CONFLICT (MACC, DMULT);
1381 CONFLICT (DMACC, MULT);
1382 CONFLICT (DMACC, DMULT);
1383
1384 /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
1385 executed immediately after a DMULT, DMULTU, DIV, DIVU,
1386 DDIV or DDIVU instruction, the result of the MACC or
1387 DMACC instruction is incorrect.". */
1388 CONFLICT (DMULT, MACC);
1389 CONFLICT (DMULT, DMACC);
1390 CONFLICT (DIV, MACC);
1391 CONFLICT (DIV, DMACC);
1392
1393#undef CONFLICT
1394}
1395
156c2f8b
NC
1396/* This function is called once, at assembler startup time. It should
1397 set up all the tables, etc. that the MD part of the assembler will need. */
1398
252b5132 1399void
17a2f251 1400md_begin (void)
252b5132 1401{
252b5132 1402 register const char *retval = NULL;
156c2f8b 1403 int i = 0;
252b5132 1404 int broken = 0;
1f25f5d3 1405
0a44bf69
RS
1406 if (mips_pic != NO_PIC)
1407 {
1408 if (g_switch_seen && g_switch_value != 0)
1409 as_bad (_("-G may not be used in position-independent code"));
1410 g_switch_value = 0;
1411 }
1412
fef14a42 1413 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_arch))
252b5132
RH
1414 as_warn (_("Could not set architecture and machine"));
1415
252b5132
RH
1416 op_hash = hash_new ();
1417
1418 for (i = 0; i < NUMOPCODES;)
1419 {
1420 const char *name = mips_opcodes[i].name;
1421
17a2f251 1422 retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
252b5132
RH
1423 if (retval != NULL)
1424 {
1425 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1426 mips_opcodes[i].name, retval);
1427 /* Probably a memory allocation problem? Give up now. */
1428 as_fatal (_("Broken assembler. No assembly attempted."));
1429 }
1430 do
1431 {
1432 if (mips_opcodes[i].pinfo != INSN_MACRO)
1433 {
1434 if (!validate_mips_insn (&mips_opcodes[i]))
1435 broken = 1;
1e915849
RS
1436 if (nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
1437 {
1438 create_insn (&nop_insn, mips_opcodes + i);
1439 nop_insn.fixed_p = 1;
1440 }
252b5132
RH
1441 }
1442 ++i;
1443 }
1444 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1445 }
1446
1447 mips16_op_hash = hash_new ();
1448
1449 i = 0;
1450 while (i < bfd_mips16_num_opcodes)
1451 {
1452 const char *name = mips16_opcodes[i].name;
1453
17a2f251 1454 retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
252b5132
RH
1455 if (retval != NULL)
1456 as_fatal (_("internal: can't hash `%s': %s"),
1457 mips16_opcodes[i].name, retval);
1458 do
1459 {
1460 if (mips16_opcodes[i].pinfo != INSN_MACRO
1461 && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1462 != mips16_opcodes[i].match))
1463 {
1464 fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1465 mips16_opcodes[i].name, mips16_opcodes[i].args);
1466 broken = 1;
1467 }
1e915849
RS
1468 if (mips16_nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
1469 {
1470 create_insn (&mips16_nop_insn, mips16_opcodes + i);
1471 mips16_nop_insn.fixed_p = 1;
1472 }
252b5132
RH
1473 ++i;
1474 }
1475 while (i < bfd_mips16_num_opcodes
1476 && strcmp (mips16_opcodes[i].name, name) == 0);
1477 }
1478
1479 if (broken)
1480 as_fatal (_("Broken assembler. No assembly attempted."));
1481
1482 /* We add all the general register names to the symbol table. This
1483 helps us detect invalid uses of them. */
1484 for (i = 0; i < 32; i++)
1485 {
1486 char buf[5];
1487
1488 sprintf (buf, "$%d", i);
1489 symbol_table_insert (symbol_new (buf, reg_section, i,
1490 &zero_address_frag));
1491 }
76db943d
TS
1492 symbol_table_insert (symbol_new ("$ra", reg_section, RA,
1493 &zero_address_frag));
252b5132
RH
1494 symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1495 &zero_address_frag));
1496 symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1497 &zero_address_frag));
1498 symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1499 &zero_address_frag));
1500 symbol_table_insert (symbol_new ("$at", reg_section, AT,
1501 &zero_address_frag));
1502 symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1503 &zero_address_frag));
1504 symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1505 &zero_address_frag));
85b51719
TS
1506 symbol_table_insert (symbol_new ("$zero", reg_section, ZERO,
1507 &zero_address_frag));
252b5132
RH
1508 symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1509 &zero_address_frag));
1510
6047c971
AO
1511 /* If we don't add these register names to the symbol table, they
1512 may end up being added as regular symbols by operand(), and then
1513 make it to the object file as undefined in case they're not
1514 regarded as local symbols. They're local in o32, since `$' is a
1515 local symbol prefix, but not in n32 or n64. */
1516 for (i = 0; i < 8; i++)
1517 {
1518 char buf[6];
1519
1520 sprintf (buf, "$fcc%i", i);
1521 symbol_table_insert (symbol_new (buf, reg_section, -1,
1522 &zero_address_frag));
1523 }
1524
7d10b47d 1525 mips_no_prev_insn ();
252b5132
RH
1526
1527 mips_gprmask = 0;
1528 mips_cprmask[0] = 0;
1529 mips_cprmask[1] = 0;
1530 mips_cprmask[2] = 0;
1531 mips_cprmask[3] = 0;
1532
1533 /* set the default alignment for the text section (2**2) */
1534 record_alignment (text_section, 2);
1535
4d0d148d 1536 bfd_set_gp_size (stdoutput, g_switch_value);
252b5132
RH
1537
1538 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1539 {
0a44bf69
RS
1540 /* On a native system other than VxWorks, sections must be aligned
1541 to 16 byte boundaries. When configured for an embedded ELF
1542 target, we don't bother. */
1543 if (strcmp (TARGET_OS, "elf") != 0
1544 && strcmp (TARGET_OS, "vxworks") != 0)
252b5132
RH
1545 {
1546 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1547 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1548 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1549 }
1550
1551 /* Create a .reginfo section for register masks and a .mdebug
1552 section for debugging information. */
1553 {
1554 segT seg;
1555 subsegT subseg;
1556 flagword flags;
1557 segT sec;
1558
1559 seg = now_seg;
1560 subseg = now_subseg;
1561
1562 /* The ABI says this section should be loaded so that the
1563 running program can access it. However, we don't load it
1564 if we are configured for an embedded target */
1565 flags = SEC_READONLY | SEC_DATA;
1566 if (strcmp (TARGET_OS, "elf") != 0)
1567 flags |= SEC_ALLOC | SEC_LOAD;
1568
316f5878 1569 if (mips_abi != N64_ABI)
252b5132
RH
1570 {
1571 sec = subseg_new (".reginfo", (subsegT) 0);
1572
195325d2
TS
1573 bfd_set_section_flags (stdoutput, sec, flags);
1574 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
bdaaa2e1 1575
252b5132
RH
1576#ifdef OBJ_ELF
1577 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1578#endif
1579 }
1580 else
1581 {
1582 /* The 64-bit ABI uses a .MIPS.options section rather than
1583 .reginfo section. */
1584 sec = subseg_new (".MIPS.options", (subsegT) 0);
195325d2
TS
1585 bfd_set_section_flags (stdoutput, sec, flags);
1586 bfd_set_section_alignment (stdoutput, sec, 3);
252b5132
RH
1587
1588#ifdef OBJ_ELF
1589 /* Set up the option header. */
1590 {
1591 Elf_Internal_Options opthdr;
1592 char *f;
1593
1594 opthdr.kind = ODK_REGINFO;
1595 opthdr.size = (sizeof (Elf_External_Options)
1596 + sizeof (Elf64_External_RegInfo));
1597 opthdr.section = 0;
1598 opthdr.info = 0;
1599 f = frag_more (sizeof (Elf_External_Options));
1600 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1601 (Elf_External_Options *) f);
1602
1603 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1604 }
1605#endif
1606 }
1607
1608 if (ECOFF_DEBUGGING)
1609 {
1610 sec = subseg_new (".mdebug", (subsegT) 0);
1611 (void) bfd_set_section_flags (stdoutput, sec,
1612 SEC_HAS_CONTENTS | SEC_READONLY);
1613 (void) bfd_set_section_alignment (stdoutput, sec, 2);
1614 }
ecb4347a 1615#ifdef OBJ_ELF
dcd410fe 1616 else if (OUTPUT_FLAVOR == bfd_target_elf_flavour && mips_flag_pdr)
ecb4347a
DJ
1617 {
1618 pdr_seg = subseg_new (".pdr", (subsegT) 0);
1619 (void) bfd_set_section_flags (stdoutput, pdr_seg,
1620 SEC_READONLY | SEC_RELOC
1621 | SEC_DEBUGGING);
1622 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1623 }
252b5132
RH
1624#endif
1625
1626 subseg_set (seg, subseg);
1627 }
1628 }
1629
1630 if (! ECOFF_DEBUGGING)
1631 md_obj_begin ();
71400594
RS
1632
1633 if (mips_fix_vr4120)
1634 init_vr4120_conflicts ();
252b5132
RH
1635}
1636
1637void
17a2f251 1638md_mips_end (void)
252b5132
RH
1639{
1640 if (! ECOFF_DEBUGGING)
1641 md_obj_end ();
1642}
1643
1644void
17a2f251 1645md_assemble (char *str)
252b5132
RH
1646{
1647 struct mips_cl_insn insn;
f6688943
TS
1648 bfd_reloc_code_real_type unused_reloc[3]
1649 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132
RH
1650
1651 imm_expr.X_op = O_absent;
5f74bc13 1652 imm2_expr.X_op = O_absent;
252b5132 1653 offset_expr.X_op = O_absent;
f6688943
TS
1654 imm_reloc[0] = BFD_RELOC_UNUSED;
1655 imm_reloc[1] = BFD_RELOC_UNUSED;
1656 imm_reloc[2] = BFD_RELOC_UNUSED;
1657 offset_reloc[0] = BFD_RELOC_UNUSED;
1658 offset_reloc[1] = BFD_RELOC_UNUSED;
1659 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
1660
1661 if (mips_opts.mips16)
1662 mips16_ip (str, &insn);
1663 else
1664 {
1665 mips_ip (str, &insn);
beae10d5
KH
1666 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1667 str, insn.insn_opcode));
252b5132
RH
1668 }
1669
1670 if (insn_error)
1671 {
1672 as_bad ("%s `%s'", insn_error, str);
1673 return;
1674 }
1675
1676 if (insn.insn_mo->pinfo == INSN_MACRO)
1677 {
584892a6 1678 macro_start ();
252b5132
RH
1679 if (mips_opts.mips16)
1680 mips16_macro (&insn);
1681 else
1682 macro (&insn);
584892a6 1683 macro_end ();
252b5132
RH
1684 }
1685 else
1686 {
1687 if (imm_expr.X_op != O_absent)
4d7206a2 1688 append_insn (&insn, &imm_expr, imm_reloc);
252b5132 1689 else if (offset_expr.X_op != O_absent)
4d7206a2 1690 append_insn (&insn, &offset_expr, offset_reloc);
252b5132 1691 else
4d7206a2 1692 append_insn (&insn, NULL, unused_reloc);
252b5132
RH
1693 }
1694}
1695
5919d012 1696/* Return true if the given relocation might need a matching %lo().
0a44bf69
RS
1697 This is only "might" because SVR4 R_MIPS_GOT16 relocations only
1698 need a matching %lo() when applied to local symbols. */
5919d012
RS
1699
1700static inline bfd_boolean
17a2f251 1701reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
5919d012 1702{
3b91255e
RS
1703 return (HAVE_IN_PLACE_ADDENDS
1704 && (reloc == BFD_RELOC_HI16_S
0a44bf69
RS
1705 || reloc == BFD_RELOC_MIPS16_HI16_S
1706 /* VxWorks R_MIPS_GOT16 relocs never need a matching %lo();
1707 all GOT16 relocations evaluate to "G". */
1708 || (reloc == BFD_RELOC_MIPS_GOT16 && mips_pic != VXWORKS_PIC)));
5919d012
RS
1709}
1710
1711/* Return true if the given fixup is followed by a matching R_MIPS_LO16
1712 relocation. */
1713
1714static inline bfd_boolean
17a2f251 1715fixup_has_matching_lo_p (fixS *fixp)
5919d012
RS
1716{
1717 return (fixp->fx_next != NULL
d6f16593
MR
1718 && (fixp->fx_next->fx_r_type == BFD_RELOC_LO16
1719 || fixp->fx_next->fx_r_type == BFD_RELOC_MIPS16_LO16)
5919d012
RS
1720 && fixp->fx_addsy == fixp->fx_next->fx_addsy
1721 && fixp->fx_offset == fixp->fx_next->fx_offset);
1722}
1723
252b5132
RH
1724/* See whether instruction IP reads register REG. CLASS is the type
1725 of register. */
1726
1727static int
71400594 1728insn_uses_reg (const struct mips_cl_insn *ip, unsigned int reg,
17a2f251 1729 enum mips_regclass class)
252b5132
RH
1730{
1731 if (class == MIPS16_REG)
1732 {
1733 assert (mips_opts.mips16);
1734 reg = mips16_to_32_reg_map[reg];
1735 class = MIPS_GR_REG;
1736 }
1737
85b51719
TS
1738 /* Don't report on general register ZERO, since it never changes. */
1739 if (class == MIPS_GR_REG && reg == ZERO)
252b5132
RH
1740 return 0;
1741
1742 if (class == MIPS_FP_REG)
1743 {
1744 assert (! mips_opts.mips16);
1745 /* If we are called with either $f0 or $f1, we must check $f0.
1746 This is not optimal, because it will introduce an unnecessary
1747 NOP between "lwc1 $f0" and "swc1 $f1". To fix this we would
1748 need to distinguish reading both $f0 and $f1 or just one of
1749 them. Note that we don't have to check the other way,
1750 because there is no instruction that sets both $f0 and $f1
1751 and requires a delay. */
1752 if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
bf12938e 1753 && ((EXTRACT_OPERAND (FS, *ip) & ~(unsigned) 1)
252b5132
RH
1754 == (reg &~ (unsigned) 1)))
1755 return 1;
1756 if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
bf12938e 1757 && ((EXTRACT_OPERAND (FT, *ip) & ~(unsigned) 1)
252b5132
RH
1758 == (reg &~ (unsigned) 1)))
1759 return 1;
1760 }
1761 else if (! mips_opts.mips16)
1762 {
1763 if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
bf12938e 1764 && EXTRACT_OPERAND (RS, *ip) == reg)
252b5132
RH
1765 return 1;
1766 if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
bf12938e 1767 && EXTRACT_OPERAND (RT, *ip) == reg)
252b5132
RH
1768 return 1;
1769 }
1770 else
1771 {
1772 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
bf12938e 1773 && mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RX, *ip)] == reg)
252b5132
RH
1774 return 1;
1775 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
bf12938e 1776 && mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RY, *ip)] == reg)
252b5132
RH
1777 return 1;
1778 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
bf12938e 1779 && (mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (MOVE32Z, *ip)]
252b5132
RH
1780 == reg))
1781 return 1;
1782 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1783 return 1;
1784 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1785 return 1;
1786 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1787 return 1;
1788 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
bf12938e 1789 && MIPS16_EXTRACT_OPERAND (REGR32, *ip) == reg)
252b5132
RH
1790 return 1;
1791 }
1792
1793 return 0;
1794}
1795
1796/* This function returns true if modifying a register requires a
1797 delay. */
1798
1799static int
17a2f251 1800reg_needs_delay (unsigned int reg)
252b5132
RH
1801{
1802 unsigned long prev_pinfo;
1803
47e39b9d 1804 prev_pinfo = history[0].insn_mo->pinfo;
252b5132 1805 if (! mips_opts.noreorder
81912461
ILT
1806 && (((prev_pinfo & INSN_LOAD_MEMORY_DELAY)
1807 && ! gpr_interlocks)
1808 || ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1809 && ! cop_interlocks)))
252b5132 1810 {
81912461
ILT
1811 /* A load from a coprocessor or from memory. All load delays
1812 delay the use of general register rt for one instruction. */
bdaaa2e1 1813 /* Itbl support may require additional care here. */
252b5132 1814 know (prev_pinfo & INSN_WRITE_GPR_T);
bf12938e 1815 if (reg == EXTRACT_OPERAND (RT, history[0]))
252b5132
RH
1816 return 1;
1817 }
1818
1819 return 0;
1820}
1821
404a8071
RS
1822/* Move all labels in insn_labels to the current insertion point. */
1823
1824static void
1825mips_move_labels (void)
1826{
1827 struct insn_label_list *l;
1828 valueT val;
1829
1830 for (l = insn_labels; l != NULL; l = l->next)
1831 {
1832 assert (S_GET_SEGMENT (l->label) == now_seg);
1833 symbol_set_frag (l->label, frag_now);
1834 val = (valueT) frag_now_fix ();
1835 /* mips16 text labels are stored as odd. */
1836 if (mips_opts.mips16)
1837 ++val;
1838 S_SET_VALUE (l->label, val);
1839 }
1840}
1841
252b5132
RH
1842/* Mark instruction labels in mips16 mode. This permits the linker to
1843 handle them specially, such as generating jalx instructions when
1844 needed. We also make them odd for the duration of the assembly, in
1845 order to generate the right sort of code. We will make them even
1846 in the adjust_symtab routine, while leaving them marked. This is
1847 convenient for the debugger and the disassembler. The linker knows
1848 to make them odd again. */
1849
1850static void
17a2f251 1851mips16_mark_labels (void)
252b5132
RH
1852{
1853 if (mips_opts.mips16)
1854 {
1855 struct insn_label_list *l;
98aa84af 1856 valueT val;
252b5132
RH
1857
1858 for (l = insn_labels; l != NULL; l = l->next)
1859 {
1860#ifdef OBJ_ELF
1861 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1862 S_SET_OTHER (l->label, STO_MIPS16);
1863#endif
98aa84af
AM
1864 val = S_GET_VALUE (l->label);
1865 if ((val & 1) == 0)
1866 S_SET_VALUE (l->label, val + 1);
252b5132
RH
1867 }
1868 }
1869}
1870
4d7206a2
RS
1871/* End the current frag. Make it a variant frag and record the
1872 relaxation info. */
1873
1874static void
1875relax_close_frag (void)
1876{
584892a6 1877 mips_macro_warning.first_frag = frag_now;
4d7206a2 1878 frag_var (rs_machine_dependent, 0, 0,
584892a6 1879 RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1]),
4d7206a2
RS
1880 mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
1881
1882 memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
1883 mips_relax.first_fixup = 0;
1884}
1885
1886/* Start a new relaxation sequence whose expansion depends on SYMBOL.
1887 See the comment above RELAX_ENCODE for more details. */
1888
1889static void
1890relax_start (symbolS *symbol)
1891{
1892 assert (mips_relax.sequence == 0);
1893 mips_relax.sequence = 1;
1894 mips_relax.symbol = symbol;
1895}
1896
1897/* Start generating the second version of a relaxable sequence.
1898 See the comment above RELAX_ENCODE for more details. */
252b5132
RH
1899
1900static void
4d7206a2
RS
1901relax_switch (void)
1902{
1903 assert (mips_relax.sequence == 1);
1904 mips_relax.sequence = 2;
1905}
1906
1907/* End the current relaxable sequence. */
1908
1909static void
1910relax_end (void)
1911{
1912 assert (mips_relax.sequence == 2);
1913 relax_close_frag ();
1914 mips_relax.sequence = 0;
1915}
1916
71400594
RS
1917/* Classify an instruction according to the FIX_VR4120_* enumeration.
1918 Return NUM_FIX_VR4120_CLASSES if the instruction isn't affected
1919 by VR4120 errata. */
4d7206a2 1920
71400594
RS
1921static unsigned int
1922classify_vr4120_insn (const char *name)
252b5132 1923{
71400594
RS
1924 if (strncmp (name, "macc", 4) == 0)
1925 return FIX_VR4120_MACC;
1926 if (strncmp (name, "dmacc", 5) == 0)
1927 return FIX_VR4120_DMACC;
1928 if (strncmp (name, "mult", 4) == 0)
1929 return FIX_VR4120_MULT;
1930 if (strncmp (name, "dmult", 5) == 0)
1931 return FIX_VR4120_DMULT;
1932 if (strstr (name, "div"))
1933 return FIX_VR4120_DIV;
1934 if (strcmp (name, "mtlo") == 0 || strcmp (name, "mthi") == 0)
1935 return FIX_VR4120_MTHILO;
1936 return NUM_FIX_VR4120_CLASSES;
1937}
252b5132 1938
71400594
RS
1939/* Return the number of instructions that must separate INSN1 and INSN2,
1940 where INSN1 is the earlier instruction. Return the worst-case value
1941 for any INSN2 if INSN2 is null. */
252b5132 1942
71400594
RS
1943static unsigned int
1944insns_between (const struct mips_cl_insn *insn1,
1945 const struct mips_cl_insn *insn2)
1946{
1947 unsigned long pinfo1, pinfo2;
1948
1949 /* This function needs to know which pinfo flags are set for INSN2
1950 and which registers INSN2 uses. The former is stored in PINFO2 and
1951 the latter is tested via INSN2_USES_REG. If INSN2 is null, PINFO2
1952 will have every flag set and INSN2_USES_REG will always return true. */
1953 pinfo1 = insn1->insn_mo->pinfo;
1954 pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~0U;
252b5132 1955
71400594
RS
1956#define INSN2_USES_REG(REG, CLASS) \
1957 (insn2 == NULL || insn_uses_reg (insn2, REG, CLASS))
1958
1959 /* For most targets, write-after-read dependencies on the HI and LO
1960 registers must be separated by at least two instructions. */
1961 if (!hilo_interlocks)
252b5132 1962 {
71400594
RS
1963 if ((pinfo1 & INSN_READ_LO) && (pinfo2 & INSN_WRITE_LO))
1964 return 2;
1965 if ((pinfo1 & INSN_READ_HI) && (pinfo2 & INSN_WRITE_HI))
1966 return 2;
1967 }
1968
1969 /* If we're working around r7000 errata, there must be two instructions
1970 between an mfhi or mflo and any instruction that uses the result. */
1971 if (mips_7000_hilo_fix
1972 && MF_HILO_INSN (pinfo1)
1973 && INSN2_USES_REG (EXTRACT_OPERAND (RD, *insn1), MIPS_GR_REG))
1974 return 2;
1975
1976 /* If working around VR4120 errata, check for combinations that need
1977 a single intervening instruction. */
1978 if (mips_fix_vr4120)
1979 {
1980 unsigned int class1, class2;
252b5132 1981
71400594
RS
1982 class1 = classify_vr4120_insn (insn1->insn_mo->name);
1983 if (class1 != NUM_FIX_VR4120_CLASSES && vr4120_conflicts[class1] != 0)
252b5132 1984 {
71400594
RS
1985 if (insn2 == NULL)
1986 return 1;
1987 class2 = classify_vr4120_insn (insn2->insn_mo->name);
1988 if (vr4120_conflicts[class1] & (1 << class2))
1989 return 1;
252b5132 1990 }
71400594
RS
1991 }
1992
1993 if (!mips_opts.mips16)
1994 {
1995 /* Check for GPR or coprocessor load delays. All such delays
1996 are on the RT register. */
1997 /* Itbl support may require additional care here. */
1998 if ((!gpr_interlocks && (pinfo1 & INSN_LOAD_MEMORY_DELAY))
1999 || (!cop_interlocks && (pinfo1 & INSN_LOAD_COPROC_DELAY)))
252b5132 2000 {
71400594
RS
2001 know (pinfo1 & INSN_WRITE_GPR_T);
2002 if (INSN2_USES_REG (EXTRACT_OPERAND (RT, *insn1), MIPS_GR_REG))
2003 return 1;
2004 }
2005
2006 /* Check for generic coprocessor hazards.
2007
2008 This case is not handled very well. There is no special
2009 knowledge of CP0 handling, and the coprocessors other than
2010 the floating point unit are not distinguished at all. */
2011 /* Itbl support may require additional care here. FIXME!
2012 Need to modify this to include knowledge about
2013 user specified delays! */
2014 else if ((!cop_interlocks && (pinfo1 & INSN_COPROC_MOVE_DELAY))
2015 || (!cop_mem_interlocks && (pinfo1 & INSN_COPROC_MEMORY_DELAY)))
2016 {
2017 /* Handle cases where INSN1 writes to a known general coprocessor
2018 register. There must be a one instruction delay before INSN2
2019 if INSN2 reads that register, otherwise no delay is needed. */
2020 if (pinfo1 & INSN_WRITE_FPR_T)
252b5132 2021 {
71400594
RS
2022 if (INSN2_USES_REG (EXTRACT_OPERAND (FT, *insn1), MIPS_FP_REG))
2023 return 1;
252b5132 2024 }
71400594 2025 else if (pinfo1 & INSN_WRITE_FPR_S)
252b5132 2026 {
71400594
RS
2027 if (INSN2_USES_REG (EXTRACT_OPERAND (FS, *insn1), MIPS_FP_REG))
2028 return 1;
252b5132
RH
2029 }
2030 else
2031 {
71400594
RS
2032 /* Read-after-write dependencies on the control registers
2033 require a two-instruction gap. */
2034 if ((pinfo1 & INSN_WRITE_COND_CODE)
2035 && (pinfo2 & INSN_READ_COND_CODE))
2036 return 2;
2037
2038 /* We don't know exactly what INSN1 does. If INSN2 is
2039 also a coprocessor instruction, assume there must be
2040 a one instruction gap. */
2041 if (pinfo2 & INSN_COP)
2042 return 1;
252b5132
RH
2043 }
2044 }
6b76fefe 2045
71400594
RS
2046 /* Check for read-after-write dependencies on the coprocessor
2047 control registers in cases where INSN1 does not need a general
2048 coprocessor delay. This means that INSN1 is a floating point
2049 comparison instruction. */
2050 /* Itbl support may require additional care here. */
2051 else if (!cop_interlocks
2052 && (pinfo1 & INSN_WRITE_COND_CODE)
2053 && (pinfo2 & INSN_READ_COND_CODE))
2054 return 1;
2055 }
6b76fefe 2056
71400594 2057#undef INSN2_USES_REG
6b76fefe 2058
71400594
RS
2059 return 0;
2060}
6b76fefe 2061
7d8e00cf
RS
2062/* Return the number of nops that would be needed to work around the
2063 VR4130 mflo/mfhi errata if instruction INSN immediately followed
2064 the MAX_VR4130_NOPS instructions described by HISTORY. */
2065
2066static int
2067nops_for_vr4130 (const struct mips_cl_insn *history,
2068 const struct mips_cl_insn *insn)
2069{
2070 int i, j, reg;
2071
2072 /* Check if the instruction writes to HI or LO. MTHI and MTLO
2073 are not affected by the errata. */
2074 if (insn != 0
2075 && ((insn->insn_mo->pinfo & (INSN_WRITE_HI | INSN_WRITE_LO)) == 0
2076 || strcmp (insn->insn_mo->name, "mtlo") == 0
2077 || strcmp (insn->insn_mo->name, "mthi") == 0))
2078 return 0;
2079
2080 /* Search for the first MFLO or MFHI. */
2081 for (i = 0; i < MAX_VR4130_NOPS; i++)
2082 if (!history[i].noreorder_p && MF_HILO_INSN (history[i].insn_mo->pinfo))
2083 {
2084 /* Extract the destination register. */
2085 if (mips_opts.mips16)
2086 reg = mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RX, history[i])];
2087 else
2088 reg = EXTRACT_OPERAND (RD, history[i]);
2089
2090 /* No nops are needed if INSN reads that register. */
2091 if (insn != NULL && insn_uses_reg (insn, reg, MIPS_GR_REG))
2092 return 0;
2093
2094 /* ...or if any of the intervening instructions do. */
2095 for (j = 0; j < i; j++)
2096 if (insn_uses_reg (&history[j], reg, MIPS_GR_REG))
2097 return 0;
2098
2099 return MAX_VR4130_NOPS - i;
2100 }
2101 return 0;
2102}
2103
71400594
RS
2104/* Return the number of nops that would be needed if instruction INSN
2105 immediately followed the MAX_NOPS instructions given by HISTORY,
2106 where HISTORY[0] is the most recent instruction. If INSN is null,
2107 return the worse-case number of nops for any instruction. */
bdaaa2e1 2108
71400594
RS
2109static int
2110nops_for_insn (const struct mips_cl_insn *history,
2111 const struct mips_cl_insn *insn)
2112{
2113 int i, nops, tmp_nops;
bdaaa2e1 2114
71400594 2115 nops = 0;
7d8e00cf 2116 for (i = 0; i < MAX_DELAY_NOPS; i++)
71400594
RS
2117 if (!history[i].noreorder_p)
2118 {
2119 tmp_nops = insns_between (history + i, insn) - i;
2120 if (tmp_nops > nops)
2121 nops = tmp_nops;
2122 }
7d8e00cf
RS
2123
2124 if (mips_fix_vr4130)
2125 {
2126 tmp_nops = nops_for_vr4130 (history, insn);
2127 if (tmp_nops > nops)
2128 nops = tmp_nops;
2129 }
2130
71400594
RS
2131 return nops;
2132}
252b5132 2133
71400594
RS
2134/* The variable arguments provide NUM_INSNS extra instructions that
2135 might be added to HISTORY. Return the largest number of nops that
2136 would be needed after the extended sequence. */
252b5132 2137
71400594
RS
2138static int
2139nops_for_sequence (int num_insns, const struct mips_cl_insn *history, ...)
2140{
2141 va_list args;
2142 struct mips_cl_insn buffer[MAX_NOPS];
2143 struct mips_cl_insn *cursor;
2144 int nops;
2145
2146 va_start (args, history);
2147 cursor = buffer + num_insns;
2148 memcpy (cursor, history, (MAX_NOPS - num_insns) * sizeof (*cursor));
2149 while (cursor > buffer)
2150 *--cursor = *va_arg (args, const struct mips_cl_insn *);
2151
2152 nops = nops_for_insn (buffer, NULL);
2153 va_end (args);
2154 return nops;
2155}
252b5132 2156
71400594
RS
2157/* Like nops_for_insn, but if INSN is a branch, take into account the
2158 worst-case delay for the branch target. */
252b5132 2159
71400594
RS
2160static int
2161nops_for_insn_or_target (const struct mips_cl_insn *history,
2162 const struct mips_cl_insn *insn)
2163{
2164 int nops, tmp_nops;
60b63b72 2165
71400594
RS
2166 nops = nops_for_insn (history, insn);
2167 if (insn->insn_mo->pinfo & (INSN_UNCOND_BRANCH_DELAY
2168 | INSN_COND_BRANCH_DELAY
2169 | INSN_COND_BRANCH_LIKELY))
2170 {
2171 tmp_nops = nops_for_sequence (2, history, insn, NOP_INSN);
2172 if (tmp_nops > nops)
2173 nops = tmp_nops;
2174 }
2175 else if (mips_opts.mips16 && (insn->insn_mo->pinfo & MIPS16_INSN_BRANCH))
2176 {
2177 tmp_nops = nops_for_sequence (1, history, insn);
2178 if (tmp_nops > nops)
2179 nops = tmp_nops;
2180 }
2181 return nops;
2182}
2183
2184/* Output an instruction. IP is the instruction information.
2185 ADDRESS_EXPR is an operand of the instruction to be used with
2186 RELOC_TYPE. */
2187
2188static void
2189append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
2190 bfd_reloc_code_real_type *reloc_type)
2191{
2192 register unsigned long prev_pinfo, pinfo;
2193 relax_stateT prev_insn_frag_type = 0;
2194 bfd_boolean relaxed_branch = FALSE;
2195
2196 /* Mark instruction labels in mips16 mode. */
2197 mips16_mark_labels ();
2198
2199 prev_pinfo = history[0].insn_mo->pinfo;
2200 pinfo = ip->insn_mo->pinfo;
2201
2202 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
2203 {
2204 /* There are a lot of optimizations we could do that we don't.
2205 In particular, we do not, in general, reorder instructions.
2206 If you use gcc with optimization, it will reorder
2207 instructions and generally do much more optimization then we
2208 do here; repeating all that work in the assembler would only
2209 benefit hand written assembly code, and does not seem worth
2210 it. */
2211 int nops = (mips_optimize == 0
2212 ? nops_for_insn (history, NULL)
2213 : nops_for_insn_or_target (history, ip));
2214 if (nops > 0)
252b5132
RH
2215 {
2216 fragS *old_frag;
2217 unsigned long old_frag_offset;
2218 int i;
252b5132
RH
2219
2220 old_frag = frag_now;
2221 old_frag_offset = frag_now_fix ();
2222
2223 for (i = 0; i < nops; i++)
2224 emit_nop ();
2225
2226 if (listing)
2227 {
2228 listing_prev_line ();
2229 /* We may be at the start of a variant frag. In case we
2230 are, make sure there is enough space for the frag
2231 after the frags created by listing_prev_line. The
2232 argument to frag_grow here must be at least as large
2233 as the argument to all other calls to frag_grow in
2234 this file. We don't have to worry about being in the
2235 middle of a variant frag, because the variants insert
2236 all needed nop instructions themselves. */
2237 frag_grow (40);
2238 }
2239
404a8071 2240 mips_move_labels ();
252b5132
RH
2241
2242#ifndef NO_ECOFF_DEBUGGING
2243 if (ECOFF_DEBUGGING)
2244 ecoff_fix_loc (old_frag, old_frag_offset);
2245#endif
2246 }
71400594
RS
2247 }
2248 else if (mips_relax.sequence != 2 && prev_nop_frag != NULL)
2249 {
2250 /* Work out how many nops in prev_nop_frag are needed by IP. */
2251 int nops = nops_for_insn_or_target (history, ip);
2252 assert (nops <= prev_nop_frag_holds);
252b5132 2253
71400594
RS
2254 /* Enforce NOPS as a minimum. */
2255 if (nops > prev_nop_frag_required)
2256 prev_nop_frag_required = nops;
252b5132 2257
71400594
RS
2258 if (prev_nop_frag_holds == prev_nop_frag_required)
2259 {
2260 /* Settle for the current number of nops. Update the history
2261 accordingly (for the benefit of any future .set reorder code). */
2262 prev_nop_frag = NULL;
2263 insert_into_history (prev_nop_frag_since,
2264 prev_nop_frag_holds, NOP_INSN);
2265 }
2266 else
2267 {
2268 /* Allow this instruction to replace one of the nops that was
2269 tentatively added to prev_nop_frag. */
2270 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
2271 prev_nop_frag_holds--;
2272 prev_nop_frag_since++;
252b5132
RH
2273 }
2274 }
2275
58e2ea4d
MR
2276#ifdef OBJ_ELF
2277 /* The value passed to dwarf2_emit_insn is the distance between
2278 the beginning of the current instruction and the address that
2279 should be recorded in the debug tables. For MIPS16 debug info
2280 we want to use ISA-encoded addresses, so we pass -1 for an
2281 address higher by one than the current. */
2282 dwarf2_emit_insn (mips_opts.mips16 ? -1 : 0);
2283#endif
2284
895921c9 2285 /* Record the frag type before frag_var. */
47e39b9d
RS
2286 if (history[0].frag)
2287 prev_insn_frag_type = history[0].frag->fr_type;
895921c9 2288
4d7206a2 2289 if (address_expr
0b25d3e6 2290 && *reloc_type == BFD_RELOC_16_PCREL_S2
4a6a3df4
AO
2291 && (pinfo & INSN_UNCOND_BRANCH_DELAY || pinfo & INSN_COND_BRANCH_DELAY
2292 || pinfo & INSN_COND_BRANCH_LIKELY)
2293 && mips_relax_branch
2294 /* Don't try branch relaxation within .set nomacro, or within
2295 .set noat if we use $at for PIC computations. If it turns
2296 out that the branch was out-of-range, we'll get an error. */
2297 && !mips_opts.warn_about_macros
2298 && !(mips_opts.noat && mips_pic != NO_PIC)
2299 && !mips_opts.mips16)
2300 {
895921c9 2301 relaxed_branch = TRUE;
1e915849
RS
2302 add_relaxed_insn (ip, (relaxed_branch_length
2303 (NULL, NULL,
2304 (pinfo & INSN_UNCOND_BRANCH_DELAY) ? -1
2305 : (pinfo & INSN_COND_BRANCH_LIKELY) ? 1
2306 : 0)), 4,
2307 RELAX_BRANCH_ENCODE
2308 (pinfo & INSN_UNCOND_BRANCH_DELAY,
2309 pinfo & INSN_COND_BRANCH_LIKELY,
2310 pinfo & INSN_WRITE_GPR_31,
2311 0),
2312 address_expr->X_add_symbol,
2313 address_expr->X_add_number);
4a6a3df4
AO
2314 *reloc_type = BFD_RELOC_UNUSED;
2315 }
2316 else if (*reloc_type > BFD_RELOC_UNUSED)
252b5132
RH
2317 {
2318 /* We need to set up a variant frag. */
2319 assert (mips_opts.mips16 && address_expr != NULL);
1e915849
RS
2320 add_relaxed_insn (ip, 4, 0,
2321 RELAX_MIPS16_ENCODE
2322 (*reloc_type - BFD_RELOC_UNUSED,
2323 mips16_small, mips16_ext,
2324 prev_pinfo & INSN_UNCOND_BRANCH_DELAY,
2325 history[0].mips16_absolute_jump_p),
2326 make_expr_symbol (address_expr), 0);
252b5132 2327 }
252b5132
RH
2328 else if (mips_opts.mips16
2329 && ! ip->use_extend
f6688943 2330 && *reloc_type != BFD_RELOC_MIPS16_JMP)
9497f5ac 2331 {
b8ee1a6e
DU
2332 if ((pinfo & INSN_UNCOND_BRANCH_DELAY) == 0)
2333 /* Make sure there is enough room to swap this instruction with
2334 a following jump instruction. */
2335 frag_grow (6);
1e915849 2336 add_fixed_insn (ip);
252b5132
RH
2337 }
2338 else
2339 {
2340 if (mips_opts.mips16
2341 && mips_opts.noreorder
2342 && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
2343 as_warn (_("extended instruction in delay slot"));
2344
4d7206a2
RS
2345 if (mips_relax.sequence)
2346 {
2347 /* If we've reached the end of this frag, turn it into a variant
2348 frag and record the information for the instructions we've
2349 written so far. */
2350 if (frag_room () < 4)
2351 relax_close_frag ();
2352 mips_relax.sizes[mips_relax.sequence - 1] += 4;
2353 }
2354
584892a6
RS
2355 if (mips_relax.sequence != 2)
2356 mips_macro_warning.sizes[0] += 4;
2357 if (mips_relax.sequence != 1)
2358 mips_macro_warning.sizes[1] += 4;
2359
1e915849
RS
2360 if (mips_opts.mips16)
2361 {
2362 ip->fixed_p = 1;
2363 ip->mips16_absolute_jump_p = (*reloc_type == BFD_RELOC_MIPS16_JMP);
2364 }
2365 add_fixed_insn (ip);
252b5132
RH
2366 }
2367
01a3f561 2368 if (address_expr != NULL && *reloc_type <= BFD_RELOC_UNUSED)
252b5132
RH
2369 {
2370 if (address_expr->X_op == O_constant)
2371 {
f17c130b 2372 unsigned int tmp;
f6688943
TS
2373
2374 switch (*reloc_type)
252b5132
RH
2375 {
2376 case BFD_RELOC_32:
2377 ip->insn_opcode |= address_expr->X_add_number;
2378 break;
2379
f6688943 2380 case BFD_RELOC_MIPS_HIGHEST:
f17c130b
AM
2381 tmp = (address_expr->X_add_number + 0x800080008000ull) >> 48;
2382 ip->insn_opcode |= tmp & 0xffff;
f6688943
TS
2383 break;
2384
2385 case BFD_RELOC_MIPS_HIGHER:
f17c130b
AM
2386 tmp = (address_expr->X_add_number + 0x80008000ull) >> 32;
2387 ip->insn_opcode |= tmp & 0xffff;
f6688943
TS
2388 break;
2389
2390 case BFD_RELOC_HI16_S:
f17c130b
AM
2391 tmp = (address_expr->X_add_number + 0x8000) >> 16;
2392 ip->insn_opcode |= tmp & 0xffff;
f6688943
TS
2393 break;
2394
2395 case BFD_RELOC_HI16:
2396 ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
2397 break;
2398
01a3f561 2399 case BFD_RELOC_UNUSED:
252b5132 2400 case BFD_RELOC_LO16:
ed6fb7bd 2401 case BFD_RELOC_MIPS_GOT_DISP:
252b5132
RH
2402 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2403 break;
2404
2405 case BFD_RELOC_MIPS_JMP:
2406 if ((address_expr->X_add_number & 3) != 0)
2407 as_bad (_("jump to misaligned address (0x%lx)"),
2408 (unsigned long) address_expr->X_add_number);
f3c0ec86 2409 if (address_expr->X_add_number & ~0xfffffff)
7496292d
TS
2410 as_bad (_("jump address range overflow (0x%lx)"),
2411 (unsigned long) address_expr->X_add_number);
252b5132
RH
2412 ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
2413 break;
2414
2415 case BFD_RELOC_MIPS16_JMP:
2416 if ((address_expr->X_add_number & 3) != 0)
2417 as_bad (_("jump to misaligned address (0x%lx)"),
2418 (unsigned long) address_expr->X_add_number);
f3c0ec86 2419 if (address_expr->X_add_number & ~0xfffffff)
7496292d
TS
2420 as_bad (_("jump address range overflow (0x%lx)"),
2421 (unsigned long) address_expr->X_add_number);
252b5132
RH
2422 ip->insn_opcode |=
2423 (((address_expr->X_add_number & 0x7c0000) << 3)
2424 | ((address_expr->X_add_number & 0xf800000) >> 7)
2425 | ((address_expr->X_add_number & 0x3fffc) >> 2));
2426 break;
2427
252b5132 2428 case BFD_RELOC_16_PCREL_S2:
bad36eac
DJ
2429 if ((address_expr->X_add_number & 3) != 0)
2430 as_bad (_("branch to misaligned address (0x%lx)"),
2431 (unsigned long) address_expr->X_add_number);
2432 if (mips_relax_branch)
2433 goto need_reloc;
2434 if ((address_expr->X_add_number + 0x20000) & ~0x3ffff)
2435 as_bad (_("branch address range overflow (0x%lx)"),
2436 (unsigned long) address_expr->X_add_number);
2437 ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0xffff;
2438 break;
252b5132
RH
2439
2440 default:
2441 internalError ();
2442 }
2443 }
01a3f561 2444 else if (*reloc_type < BFD_RELOC_UNUSED)
252b5132 2445 need_reloc:
4d7206a2
RS
2446 {
2447 reloc_howto_type *howto;
2448 int i;
34ce925e 2449
4d7206a2
RS
2450 /* In a compound relocation, it is the final (outermost)
2451 operator that determines the relocated field. */
2452 for (i = 1; i < 3; i++)
2453 if (reloc_type[i] == BFD_RELOC_UNUSED)
2454 break;
34ce925e 2455
4d7206a2 2456 howto = bfd_reloc_type_lookup (stdoutput, reloc_type[i - 1]);
1e915849
RS
2457 ip->fixp[0] = fix_new_exp (ip->frag, ip->where,
2458 bfd_get_reloc_size (howto),
2459 address_expr,
2460 reloc_type[0] == BFD_RELOC_16_PCREL_S2,
2461 reloc_type[0]);
4d7206a2
RS
2462
2463 /* These relocations can have an addend that won't fit in
2464 4 octets for 64bit assembly. */
2465 if (HAVE_64BIT_GPRS
2466 && ! howto->partial_inplace
2467 && (reloc_type[0] == BFD_RELOC_16
2468 || reloc_type[0] == BFD_RELOC_32
2469 || reloc_type[0] == BFD_RELOC_MIPS_JMP
2470 || reloc_type[0] == BFD_RELOC_HI16_S
2471 || reloc_type[0] == BFD_RELOC_LO16
2472 || reloc_type[0] == BFD_RELOC_GPREL16
2473 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
2474 || reloc_type[0] == BFD_RELOC_GPREL32
2475 || reloc_type[0] == BFD_RELOC_64
2476 || reloc_type[0] == BFD_RELOC_CTOR
2477 || reloc_type[0] == BFD_RELOC_MIPS_SUB
2478 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
2479 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
2480 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
2481 || reloc_type[0] == BFD_RELOC_MIPS_REL16
d6f16593
MR
2482 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
2483 || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
2484 || reloc_type[0] == BFD_RELOC_MIPS16_HI16_S
2485 || reloc_type[0] == BFD_RELOC_MIPS16_LO16))
1e915849 2486 ip->fixp[0]->fx_no_overflow = 1;
4d7206a2
RS
2487
2488 if (mips_relax.sequence)
2489 {
2490 if (mips_relax.first_fixup == 0)
1e915849 2491 mips_relax.first_fixup = ip->fixp[0];
4d7206a2
RS
2492 }
2493 else if (reloc_needs_lo_p (*reloc_type))
2494 {
2495 struct mips_hi_fixup *hi_fixup;
252b5132 2496
4d7206a2
RS
2497 /* Reuse the last entry if it already has a matching %lo. */
2498 hi_fixup = mips_hi_fixup_list;
2499 if (hi_fixup == 0
2500 || !fixup_has_matching_lo_p (hi_fixup->fixp))
2501 {
2502 hi_fixup = ((struct mips_hi_fixup *)
2503 xmalloc (sizeof (struct mips_hi_fixup)));
2504 hi_fixup->next = mips_hi_fixup_list;
2505 mips_hi_fixup_list = hi_fixup;
252b5132 2506 }
1e915849 2507 hi_fixup->fixp = ip->fixp[0];
4d7206a2
RS
2508 hi_fixup->seg = now_seg;
2509 }
f6688943 2510
4d7206a2
RS
2511 /* Add fixups for the second and third relocations, if given.
2512 Note that the ABI allows the second relocation to be
2513 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the
2514 moment we only use RSS_UNDEF, but we could add support
2515 for the others if it ever becomes necessary. */
2516 for (i = 1; i < 3; i++)
2517 if (reloc_type[i] != BFD_RELOC_UNUSED)
2518 {
1e915849
RS
2519 ip->fixp[i] = fix_new (ip->frag, ip->where,
2520 ip->fixp[0]->fx_size, NULL, 0,
2521 FALSE, reloc_type[i]);
b1dca8ee
RS
2522
2523 /* Use fx_tcbit to mark compound relocs. */
1e915849
RS
2524 ip->fixp[0]->fx_tcbit = 1;
2525 ip->fixp[i]->fx_tcbit = 1;
4d7206a2 2526 }
252b5132
RH
2527 }
2528 }
1e915849 2529 install_insn (ip);
252b5132
RH
2530
2531 /* Update the register mask information. */
2532 if (! mips_opts.mips16)
2533 {
2534 if (pinfo & INSN_WRITE_GPR_D)
bf12938e 2535 mips_gprmask |= 1 << EXTRACT_OPERAND (RD, *ip);
252b5132 2536 if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
bf12938e 2537 mips_gprmask |= 1 << EXTRACT_OPERAND (RT, *ip);
252b5132 2538 if (pinfo & INSN_READ_GPR_S)
bf12938e 2539 mips_gprmask |= 1 << EXTRACT_OPERAND (RS, *ip);
252b5132 2540 if (pinfo & INSN_WRITE_GPR_31)
f9419b05 2541 mips_gprmask |= 1 << RA;
252b5132 2542 if (pinfo & INSN_WRITE_FPR_D)
bf12938e 2543 mips_cprmask[1] |= 1 << EXTRACT_OPERAND (FD, *ip);
252b5132 2544 if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
bf12938e 2545 mips_cprmask[1] |= 1 << EXTRACT_OPERAND (FS, *ip);
252b5132 2546 if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
bf12938e 2547 mips_cprmask[1] |= 1 << EXTRACT_OPERAND (FT, *ip);
252b5132 2548 if ((pinfo & INSN_READ_FPR_R) != 0)
bf12938e 2549 mips_cprmask[1] |= 1 << EXTRACT_OPERAND (FR, *ip);
252b5132
RH
2550 if (pinfo & INSN_COP)
2551 {
bdaaa2e1
KH
2552 /* We don't keep enough information to sort these cases out.
2553 The itbl support does keep this information however, although
2554 we currently don't support itbl fprmats as part of the cop
2555 instruction. May want to add this support in the future. */
252b5132
RH
2556 }
2557 /* Never set the bit for $0, which is always zero. */
beae10d5 2558 mips_gprmask &= ~1 << 0;
252b5132
RH
2559 }
2560 else
2561 {
2562 if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
bf12938e 2563 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (RX, *ip);
252b5132 2564 if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
bf12938e 2565 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (RY, *ip);
252b5132 2566 if (pinfo & MIPS16_INSN_WRITE_Z)
bf12938e 2567 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (RZ, *ip);
252b5132
RH
2568 if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2569 mips_gprmask |= 1 << TREG;
2570 if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2571 mips_gprmask |= 1 << SP;
2572 if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2573 mips_gprmask |= 1 << RA;
2574 if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2575 mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2576 if (pinfo & MIPS16_INSN_READ_Z)
bf12938e 2577 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (MOVE32Z, *ip);
252b5132 2578 if (pinfo & MIPS16_INSN_READ_GPR_X)
bf12938e 2579 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (REGR32, *ip);
252b5132
RH
2580 }
2581
4d7206a2 2582 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
252b5132
RH
2583 {
2584 /* Filling the branch delay slot is more complex. We try to
2585 switch the branch with the previous instruction, which we can
2586 do if the previous instruction does not set up a condition
2587 that the branch tests and if the branch is not itself the
2588 target of any branch. */
2589 if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2590 || (pinfo & INSN_COND_BRANCH_DELAY))
2591 {
2592 if (mips_optimize < 2
2593 /* If we have seen .set volatile or .set nomove, don't
2594 optimize. */
2595 || mips_opts.nomove != 0
a38419a5
RS
2596 /* We can't swap if the previous instruction's position
2597 is fixed. */
2598 || history[0].fixed_p
252b5132
RH
2599 /* If the previous previous insn was in a .set
2600 noreorder, we can't swap. Actually, the MIPS
2601 assembler will swap in this situation. However, gcc
2602 configured -with-gnu-as will generate code like
2603 .set noreorder
2604 lw $4,XXX
2605 .set reorder
2606 INSN
2607 bne $4,$0,foo
2608 in which we can not swap the bne and INSN. If gcc is
2609 not configured -with-gnu-as, it does not output the
a38419a5 2610 .set pseudo-ops. */
47e39b9d 2611 || history[1].noreorder_p
252b5132
RH
2612 /* If the branch is itself the target of a branch, we
2613 can not swap. We cheat on this; all we check for is
2614 whether there is a label on this instruction. If
2615 there are any branches to anything other than a
2616 label, users must use .set noreorder. */
2617 || insn_labels != NULL
895921c9
MR
2618 /* If the previous instruction is in a variant frag
2619 other than this branch's one, we cannot do the swap.
2620 This does not apply to the mips16, which uses variant
2621 frags for different purposes. */
252b5132 2622 || (! mips_opts.mips16
895921c9 2623 && prev_insn_frag_type == rs_machine_dependent)
71400594
RS
2624 /* Check for conflicts between the branch and the instructions
2625 before the candidate delay slot. */
2626 || nops_for_insn (history + 1, ip) > 0
2627 /* Check for conflicts between the swapped sequence and the
2628 target of the branch. */
2629 || nops_for_sequence (2, history + 1, ip, history) > 0
252b5132
RH
2630 /* We do not swap with a trap instruction, since it
2631 complicates trap handlers to have the trap
2632 instruction be in a delay slot. */
2633 || (prev_pinfo & INSN_TRAP)
2634 /* If the branch reads a register that the previous
2635 instruction sets, we can not swap. */
2636 || (! mips_opts.mips16
2637 && (prev_pinfo & INSN_WRITE_GPR_T)
bf12938e 2638 && insn_uses_reg (ip, EXTRACT_OPERAND (RT, history[0]),
252b5132
RH
2639 MIPS_GR_REG))
2640 || (! mips_opts.mips16
2641 && (prev_pinfo & INSN_WRITE_GPR_D)
bf12938e 2642 && insn_uses_reg (ip, EXTRACT_OPERAND (RD, history[0]),
252b5132
RH
2643 MIPS_GR_REG))
2644 || (mips_opts.mips16
2645 && (((prev_pinfo & MIPS16_INSN_WRITE_X)
bf12938e
RS
2646 && (insn_uses_reg
2647 (ip, MIPS16_EXTRACT_OPERAND (RX, history[0]),
2648 MIPS16_REG)))
252b5132 2649 || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
bf12938e
RS
2650 && (insn_uses_reg
2651 (ip, MIPS16_EXTRACT_OPERAND (RY, history[0]),
2652 MIPS16_REG)))
252b5132 2653 || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
bf12938e
RS
2654 && (insn_uses_reg
2655 (ip, MIPS16_EXTRACT_OPERAND (RZ, history[0]),
2656 MIPS16_REG)))
252b5132
RH
2657 || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2658 && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2659 || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2660 && insn_uses_reg (ip, RA, MIPS_GR_REG))
2661 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2662 && insn_uses_reg (ip,
47e39b9d
RS
2663 MIPS16OP_EXTRACT_REG32R
2664 (history[0].insn_opcode),
252b5132
RH
2665 MIPS_GR_REG))))
2666 /* If the branch writes a register that the previous
2667 instruction sets, we can not swap (we know that
2668 branches write only to RD or to $31). */
2669 || (! mips_opts.mips16
2670 && (prev_pinfo & INSN_WRITE_GPR_T)
2671 && (((pinfo & INSN_WRITE_GPR_D)
bf12938e
RS
2672 && (EXTRACT_OPERAND (RT, history[0])
2673 == EXTRACT_OPERAND (RD, *ip)))
252b5132 2674 || ((pinfo & INSN_WRITE_GPR_31)
bf12938e 2675 && EXTRACT_OPERAND (RT, history[0]) == RA)))
252b5132
RH
2676 || (! mips_opts.mips16
2677 && (prev_pinfo & INSN_WRITE_GPR_D)
2678 && (((pinfo & INSN_WRITE_GPR_D)
bf12938e
RS
2679 && (EXTRACT_OPERAND (RD, history[0])
2680 == EXTRACT_OPERAND (RD, *ip)))
252b5132 2681 || ((pinfo & INSN_WRITE_GPR_31)
bf12938e 2682 && EXTRACT_OPERAND (RD, history[0]) == RA)))
252b5132
RH
2683 || (mips_opts.mips16
2684 && (pinfo & MIPS16_INSN_WRITE_31)
2685 && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2686 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
47e39b9d 2687 && (MIPS16OP_EXTRACT_REG32R (history[0].insn_opcode)
252b5132
RH
2688 == RA))))
2689 /* If the branch writes a register that the previous
2690 instruction reads, we can not swap (we know that
2691 branches only write to RD or to $31). */
2692 || (! mips_opts.mips16
2693 && (pinfo & INSN_WRITE_GPR_D)
47e39b9d 2694 && insn_uses_reg (&history[0],
bf12938e 2695 EXTRACT_OPERAND (RD, *ip),
252b5132
RH
2696 MIPS_GR_REG))
2697 || (! mips_opts.mips16
2698 && (pinfo & INSN_WRITE_GPR_31)
47e39b9d 2699 && insn_uses_reg (&history[0], RA, MIPS_GR_REG))
252b5132
RH
2700 || (mips_opts.mips16
2701 && (pinfo & MIPS16_INSN_WRITE_31)
47e39b9d 2702 && insn_uses_reg (&history[0], RA, MIPS_GR_REG))
252b5132
RH
2703 /* If one instruction sets a condition code and the
2704 other one uses a condition code, we can not swap. */
2705 || ((pinfo & INSN_READ_COND_CODE)
2706 && (prev_pinfo & INSN_WRITE_COND_CODE))
2707 || ((pinfo & INSN_WRITE_COND_CODE)
2708 && (prev_pinfo & INSN_READ_COND_CODE))
2709 /* If the previous instruction uses the PC, we can not
2710 swap. */
2711 || (mips_opts.mips16
2712 && (prev_pinfo & MIPS16_INSN_READ_PC))
252b5132
RH
2713 /* If the previous instruction had a fixup in mips16
2714 mode, we can not swap. This normally means that the
2715 previous instruction was a 4 byte branch anyhow. */
47e39b9d 2716 || (mips_opts.mips16 && history[0].fixp[0])
bdaaa2e1
KH
2717 /* If the previous instruction is a sync, sync.l, or
2718 sync.p, we can not swap. */
f173e82e 2719 || (prev_pinfo & INSN_SYNC))
252b5132 2720 {
29024861
DU
2721 if (mips_opts.mips16
2722 && (pinfo & INSN_UNCOND_BRANCH_DELAY)
2723 && (pinfo & (MIPS16_INSN_READ_X | MIPS16_INSN_READ_31))
2724 && (mips_opts.isa == ISA_MIPS32
2725 || mips_opts.isa == ISA_MIPS32R2
2726 || mips_opts.isa == ISA_MIPS64
2727 || mips_opts.isa == ISA_MIPS64R2))
2728 {
2729 /* Convert MIPS16 jr/jalr into a "compact" jump. */
2730 ip->insn_opcode |= 0x0080;
2731 install_insn (ip);
2732 insert_into_history (0, 1, ip);
2733 }
2734 else
2735 {
2736 /* We could do even better for unconditional branches to
2737 portions of this object file; we could pick up the
2738 instruction at the destination, put it in the delay
2739 slot, and bump the destination address. */
2740 insert_into_history (0, 1, ip);
2741 emit_nop ();
2742 }
2743
dd22970f
ILT
2744 if (mips_relax.sequence)
2745 mips_relax.sizes[mips_relax.sequence - 1] += 4;
252b5132
RH
2746 }
2747 else
2748 {
2749 /* It looks like we can actually do the swap. */
1e915849
RS
2750 struct mips_cl_insn delay = history[0];
2751 if (mips_opts.mips16)
252b5132 2752 {
b8ee1a6e
DU
2753 know (delay.frag == ip->frag);
2754 move_insn (ip, delay.frag, delay.where);
2755 move_insn (&delay, ip->frag, ip->where + insn_length (ip));
1e915849
RS
2756 }
2757 else if (relaxed_branch)
2758 {
2759 /* Add the delay slot instruction to the end of the
2760 current frag and shrink the fixed part of the
2761 original frag. If the branch occupies the tail of
2762 the latter, move it backwards to cover the gap. */
2763 delay.frag->fr_fix -= 4;
2764 if (delay.frag == ip->frag)
2765 move_insn (ip, ip->frag, ip->where - 4);
2766 add_fixed_insn (&delay);
252b5132
RH
2767 }
2768 else
2769 {
1e915849
RS
2770 move_insn (&delay, ip->frag, ip->where);
2771 move_insn (ip, history[0].frag, history[0].where);
252b5132 2772 }
1e915849
RS
2773 history[0] = *ip;
2774 delay.fixed_p = 1;
2775 insert_into_history (0, 1, &delay);
252b5132 2776 }
252b5132
RH
2777
2778 /* If that was an unconditional branch, forget the previous
2779 insn information. */
2780 if (pinfo & INSN_UNCOND_BRANCH_DELAY)
7d10b47d 2781 mips_no_prev_insn ();
252b5132
RH
2782 }
2783 else if (pinfo & INSN_COND_BRANCH_LIKELY)
2784 {
2785 /* We don't yet optimize a branch likely. What we should do
2786 is look at the target, copy the instruction found there
2787 into the delay slot, and increment the branch to jump to
2788 the next instruction. */
1e915849 2789 insert_into_history (0, 1, ip);
252b5132 2790 emit_nop ();
252b5132
RH
2791 }
2792 else
1e915849 2793 insert_into_history (0, 1, ip);
252b5132 2794 }
1e915849
RS
2795 else
2796 insert_into_history (0, 1, ip);
252b5132
RH
2797
2798 /* We just output an insn, so the next one doesn't have a label. */
2799 mips_clear_insn_labels ();
252b5132
RH
2800}
2801
7d10b47d 2802/* Forget that there was any previous instruction or label. */
252b5132
RH
2803
2804static void
7d10b47d 2805mips_no_prev_insn (void)
252b5132 2806{
7d10b47d
RS
2807 prev_nop_frag = NULL;
2808 insert_into_history (0, ARRAY_SIZE (history), NOP_INSN);
252b5132
RH
2809 mips_clear_insn_labels ();
2810}
2811
7d10b47d
RS
2812/* This function must be called before we emit something other than
2813 instructions. It is like mips_no_prev_insn except that it inserts
2814 any NOPS that might be needed by previous instructions. */
252b5132 2815
7d10b47d
RS
2816void
2817mips_emit_delays (void)
252b5132
RH
2818{
2819 if (! mips_opts.noreorder)
2820 {
71400594 2821 int nops = nops_for_insn (history, NULL);
252b5132
RH
2822 if (nops > 0)
2823 {
7d10b47d
RS
2824 while (nops-- > 0)
2825 add_fixed_insn (NOP_INSN);
2826 mips_move_labels ();
2827 }
2828 }
2829 mips_no_prev_insn ();
2830}
2831
2832/* Start a (possibly nested) noreorder block. */
2833
2834static void
2835start_noreorder (void)
2836{
2837 if (mips_opts.noreorder == 0)
2838 {
2839 unsigned int i;
2840 int nops;
2841
2842 /* None of the instructions before the .set noreorder can be moved. */
2843 for (i = 0; i < ARRAY_SIZE (history); i++)
2844 history[i].fixed_p = 1;
2845
2846 /* Insert any nops that might be needed between the .set noreorder
2847 block and the previous instructions. We will later remove any
2848 nops that turn out not to be needed. */
2849 nops = nops_for_insn (history, NULL);
2850 if (nops > 0)
2851 {
2852 if (mips_optimize != 0)
252b5132
RH
2853 {
2854 /* Record the frag which holds the nop instructions, so
2855 that we can remove them if we don't need them. */
2856 frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2857 prev_nop_frag = frag_now;
2858 prev_nop_frag_holds = nops;
2859 prev_nop_frag_required = 0;
2860 prev_nop_frag_since = 0;
2861 }
2862
2863 for (; nops > 0; --nops)
1e915849 2864 add_fixed_insn (NOP_INSN);
252b5132 2865
7d10b47d
RS
2866 /* Move on to a new frag, so that it is safe to simply
2867 decrease the size of prev_nop_frag. */
2868 frag_wane (frag_now);
2869 frag_new (0);
404a8071 2870 mips_move_labels ();
252b5132 2871 }
7d10b47d
RS
2872 mips16_mark_labels ();
2873 mips_clear_insn_labels ();
252b5132 2874 }
7d10b47d
RS
2875 mips_opts.noreorder++;
2876 mips_any_noreorder = 1;
2877}
252b5132 2878
7d10b47d 2879/* End a nested noreorder block. */
252b5132 2880
7d10b47d
RS
2881static void
2882end_noreorder (void)
2883{
2884 mips_opts.noreorder--;
2885 if (mips_opts.noreorder == 0 && prev_nop_frag != NULL)
2886 {
2887 /* Commit to inserting prev_nop_frag_required nops and go back to
2888 handling nop insertion the .set reorder way. */
2889 prev_nop_frag->fr_fix -= ((prev_nop_frag_holds - prev_nop_frag_required)
2890 * (mips_opts.mips16 ? 2 : 4));
2891 insert_into_history (prev_nop_frag_since,
2892 prev_nop_frag_required, NOP_INSN);
2893 prev_nop_frag = NULL;
2894 }
252b5132
RH
2895}
2896
584892a6
RS
2897/* Set up global variables for the start of a new macro. */
2898
2899static void
2900macro_start (void)
2901{
2902 memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
2903 mips_macro_warning.delay_slot_p = (mips_opts.noreorder
47e39b9d 2904 && (history[0].insn_mo->pinfo
584892a6
RS
2905 & (INSN_UNCOND_BRANCH_DELAY
2906 | INSN_COND_BRANCH_DELAY
2907 | INSN_COND_BRANCH_LIKELY)) != 0);
2908}
2909
2910/* Given that a macro is longer than 4 bytes, return the appropriate warning
2911 for it. Return null if no warning is needed. SUBTYPE is a bitmask of
2912 RELAX_DELAY_SLOT and RELAX_NOMACRO. */
2913
2914static const char *
2915macro_warning (relax_substateT subtype)
2916{
2917 if (subtype & RELAX_DELAY_SLOT)
2918 return _("Macro instruction expanded into multiple instructions"
2919 " in a branch delay slot");
2920 else if (subtype & RELAX_NOMACRO)
2921 return _("Macro instruction expanded into multiple instructions");
2922 else
2923 return 0;
2924}
2925
2926/* Finish up a macro. Emit warnings as appropriate. */
2927
2928static void
2929macro_end (void)
2930{
2931 if (mips_macro_warning.sizes[0] > 4 || mips_macro_warning.sizes[1] > 4)
2932 {
2933 relax_substateT subtype;
2934
2935 /* Set up the relaxation warning flags. */
2936 subtype = 0;
2937 if (mips_macro_warning.sizes[1] > mips_macro_warning.sizes[0])
2938 subtype |= RELAX_SECOND_LONGER;
2939 if (mips_opts.warn_about_macros)
2940 subtype |= RELAX_NOMACRO;
2941 if (mips_macro_warning.delay_slot_p)
2942 subtype |= RELAX_DELAY_SLOT;
2943
2944 if (mips_macro_warning.sizes[0] > 4 && mips_macro_warning.sizes[1] > 4)
2945 {
2946 /* Either the macro has a single implementation or both
2947 implementations are longer than 4 bytes. Emit the
2948 warning now. */
2949 const char *msg = macro_warning (subtype);
2950 if (msg != 0)
2951 as_warn (msg);
2952 }
2953 else
2954 {
2955 /* One implementation might need a warning but the other
2956 definitely doesn't. */
2957 mips_macro_warning.first_frag->fr_subtype |= subtype;
2958 }
2959 }
2960}
2961
6e1304d8
RS
2962/* Read a macro's relocation codes from *ARGS and store them in *R.
2963 The first argument in *ARGS will be either the code for a single
2964 relocation or -1 followed by the three codes that make up a
2965 composite relocation. */
2966
2967static void
2968macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
2969{
2970 int i, next;
2971
2972 next = va_arg (*args, int);
2973 if (next >= 0)
2974 r[0] = (bfd_reloc_code_real_type) next;
2975 else
2976 for (i = 0; i < 3; i++)
2977 r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
2978}
2979
252b5132
RH
2980/* Build an instruction created by a macro expansion. This is passed
2981 a pointer to the count of instructions created so far, an
2982 expression, the name of the instruction to build, an operand format
2983 string, and corresponding arguments. */
2984
252b5132 2985static void
67c0d1eb 2986macro_build (expressionS *ep, const char *name, const char *fmt, ...)
252b5132 2987{
1e915849 2988 const struct mips_opcode *mo;
252b5132 2989 struct mips_cl_insn insn;
f6688943 2990 bfd_reloc_code_real_type r[3];
252b5132 2991 va_list args;
252b5132 2992
252b5132 2993 va_start (args, fmt);
252b5132 2994
252b5132
RH
2995 if (mips_opts.mips16)
2996 {
67c0d1eb 2997 mips16_macro_build (ep, name, fmt, args);
252b5132
RH
2998 va_end (args);
2999 return;
3000 }
3001
f6688943
TS
3002 r[0] = BFD_RELOC_UNUSED;
3003 r[1] = BFD_RELOC_UNUSED;
3004 r[2] = BFD_RELOC_UNUSED;
1e915849
RS
3005 mo = (struct mips_opcode *) hash_find (op_hash, name);
3006 assert (mo);
3007 assert (strcmp (name, mo->name) == 0);
3008
3009 /* Search until we get a match for NAME. It is assumed here that
3010 macros will never generate MDMX or MIPS-3D instructions. */
3011 while (strcmp (fmt, mo->args) != 0
3012 || mo->pinfo == INSN_MACRO
3013 || !OPCODE_IS_MEMBER (mo,
3014 (mips_opts.isa
3015 | (file_ase_mips16 ? INSN_MIPS16 : 0)),
fef14a42 3016 mips_opts.arch)
1e915849
RS
3017 || (mips_opts.arch == CPU_R4650 && (mo->pinfo & FP_D) != 0))
3018 {
3019 ++mo;
3020 assert (mo->name);
3021 assert (strcmp (name, mo->name) == 0);
252b5132
RH
3022 }
3023
1e915849 3024 create_insn (&insn, mo);
252b5132
RH
3025 for (;;)
3026 {
3027 switch (*fmt++)
3028 {
3029 case '\0':
3030 break;
3031
3032 case ',':
3033 case '(':
3034 case ')':
3035 continue;
3036
5f74bc13
CD
3037 case '+':
3038 switch (*fmt++)
3039 {
3040 case 'A':
3041 case 'E':
bf12938e 3042 INSERT_OPERAND (SHAMT, insn, va_arg (args, int));
5f74bc13
CD
3043 continue;
3044
3045 case 'B':
3046 case 'F':
3047 /* Note that in the macro case, these arguments are already
3048 in MSB form. (When handling the instruction in the
3049 non-macro case, these arguments are sizes from which
3050 MSB values must be calculated.) */
bf12938e 3051 INSERT_OPERAND (INSMSB, insn, va_arg (args, int));
5f74bc13
CD
3052 continue;
3053
3054 case 'C':
3055 case 'G':
3056 case 'H':
3057 /* Note that in the macro case, these arguments are already
3058 in MSBD form. (When handling the instruction in the
3059 non-macro case, these arguments are sizes from which
3060 MSBD values must be calculated.) */
bf12938e 3061 INSERT_OPERAND (EXTMSBD, insn, va_arg (args, int));
5f74bc13
CD
3062 continue;
3063
3064 default:
3065 internalError ();
3066 }
3067 continue;
3068
252b5132
RH
3069 case 't':
3070 case 'w':
3071 case 'E':
bf12938e 3072 INSERT_OPERAND (RT, insn, va_arg (args, int));
252b5132
RH
3073 continue;
3074
3075 case 'c':
bf12938e 3076 INSERT_OPERAND (CODE, insn, va_arg (args, int));
38487616
TS
3077 continue;
3078
252b5132
RH
3079 case 'T':
3080 case 'W':
bf12938e 3081 INSERT_OPERAND (FT, insn, va_arg (args, int));
252b5132
RH
3082 continue;
3083
3084 case 'd':
3085 case 'G':
af7ee8bf 3086 case 'K':
bf12938e 3087 INSERT_OPERAND (RD, insn, va_arg (args, int));
252b5132
RH
3088 continue;
3089
4372b673
NC
3090 case 'U':
3091 {
3092 int tmp = va_arg (args, int);
3093
bf12938e
RS
3094 INSERT_OPERAND (RT, insn, tmp);
3095 INSERT_OPERAND (RD, insn, tmp);
beae10d5 3096 continue;
4372b673
NC
3097 }
3098
252b5132
RH
3099 case 'V':
3100 case 'S':
bf12938e 3101 INSERT_OPERAND (FS, insn, va_arg (args, int));
252b5132
RH
3102 continue;
3103
3104 case 'z':
3105 continue;
3106
3107 case '<':
bf12938e 3108 INSERT_OPERAND (SHAMT, insn, va_arg (args, int));
252b5132
RH
3109 continue;
3110
3111 case 'D':
bf12938e 3112 INSERT_OPERAND (FD, insn, va_arg (args, int));
252b5132
RH
3113 continue;
3114
3115 case 'B':
bf12938e 3116 INSERT_OPERAND (CODE20, insn, va_arg (args, int));
252b5132
RH
3117 continue;
3118
4372b673 3119 case 'J':
bf12938e 3120 INSERT_OPERAND (CODE19, insn, va_arg (args, int));
4372b673
NC
3121 continue;
3122
252b5132 3123 case 'q':
bf12938e 3124 INSERT_OPERAND (CODE2, insn, va_arg (args, int));
252b5132
RH
3125 continue;
3126
3127 case 'b':
3128 case 's':
3129 case 'r':
3130 case 'v':
bf12938e 3131 INSERT_OPERAND (RS, insn, va_arg (args, int));
252b5132
RH
3132 continue;
3133
3134 case 'i':
3135 case 'j':
3136 case 'o':
6e1304d8 3137 macro_read_relocs (&args, r);
cdf6fd85 3138 assert (*r == BFD_RELOC_GPREL16
f6688943
TS
3139 || *r == BFD_RELOC_MIPS_LITERAL
3140 || *r == BFD_RELOC_MIPS_HIGHER
3141 || *r == BFD_RELOC_HI16_S
3142 || *r == BFD_RELOC_LO16
3143 || *r == BFD_RELOC_MIPS_GOT16
3144 || *r == BFD_RELOC_MIPS_CALL16
438c16b8
TS
3145 || *r == BFD_RELOC_MIPS_GOT_DISP
3146 || *r == BFD_RELOC_MIPS_GOT_PAGE
3147 || *r == BFD_RELOC_MIPS_GOT_OFST
f6688943 3148 || *r == BFD_RELOC_MIPS_GOT_LO16
3e722fb5 3149 || *r == BFD_RELOC_MIPS_CALL_LO16);
252b5132
RH
3150 continue;
3151
3152 case 'u':
6e1304d8 3153 macro_read_relocs (&args, r);
252b5132
RH
3154 assert (ep != NULL
3155 && (ep->X_op == O_constant
3156 || (ep->X_op == O_symbol
f6688943
TS
3157 && (*r == BFD_RELOC_MIPS_HIGHEST
3158 || *r == BFD_RELOC_HI16_S
3159 || *r == BFD_RELOC_HI16
3160 || *r == BFD_RELOC_GPREL16
3161 || *r == BFD_RELOC_MIPS_GOT_HI16
3e722fb5 3162 || *r == BFD_RELOC_MIPS_CALL_HI16))));
252b5132
RH
3163 continue;
3164
3165 case 'p':
3166 assert (ep != NULL);
bad36eac 3167
252b5132
RH
3168 /*
3169 * This allows macro() to pass an immediate expression for
3170 * creating short branches without creating a symbol.
bad36eac
DJ
3171 *
3172 * We don't allow branch relaxation for these branches, as
3173 * they should only appear in ".set nomacro" anyway.
252b5132
RH
3174 */
3175 if (ep->X_op == O_constant)
3176 {
bad36eac
DJ
3177 if ((ep->X_add_number & 3) != 0)
3178 as_bad (_("branch to misaligned address (0x%lx)"),
3179 (unsigned long) ep->X_add_number);
3180 if ((ep->X_add_number + 0x20000) & ~0x3ffff)
3181 as_bad (_("branch address range overflow (0x%lx)"),
3182 (unsigned long) ep->X_add_number);
252b5132
RH
3183 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
3184 ep = NULL;
3185 }
3186 else
0b25d3e6 3187 *r = BFD_RELOC_16_PCREL_S2;
252b5132
RH
3188 continue;
3189
3190 case 'a':
3191 assert (ep != NULL);
f6688943 3192 *r = BFD_RELOC_MIPS_JMP;
252b5132
RH
3193 continue;
3194
3195 case 'C':
3196 insn.insn_opcode |= va_arg (args, unsigned long);
3197 continue;
3198
3199 default:
3200 internalError ();
3201 }
3202 break;
3203 }
3204 va_end (args);
f6688943 3205 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 3206
4d7206a2 3207 append_insn (&insn, ep, r);
252b5132
RH
3208}
3209
3210static void
67c0d1eb 3211mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
17a2f251 3212 va_list args)
252b5132 3213{
1e915849 3214 struct mips_opcode *mo;
252b5132 3215 struct mips_cl_insn insn;
f6688943
TS
3216 bfd_reloc_code_real_type r[3]
3217 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 3218
1e915849
RS
3219 mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
3220 assert (mo);
3221 assert (strcmp (name, mo->name) == 0);
252b5132 3222
1e915849 3223 while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO)
252b5132 3224 {
1e915849
RS
3225 ++mo;
3226 assert (mo->name);
3227 assert (strcmp (name, mo->name) == 0);
252b5132
RH
3228 }
3229
1e915849 3230 create_insn (&insn, mo);
252b5132
RH
3231 for (;;)
3232 {
3233 int c;
3234
3235 c = *fmt++;
3236 switch (c)
3237 {
3238 case '\0':
3239 break;
3240
3241 case ',':
3242 case '(':
3243 case ')':
3244 continue;
3245
3246 case 'y':
3247 case 'w':
bf12938e 3248 MIPS16_INSERT_OPERAND (RY, insn, va_arg (args, int));
252b5132
RH
3249 continue;
3250
3251 case 'x':
3252 case 'v':
bf12938e 3253 MIPS16_INSERT_OPERAND (RX, insn, va_arg (args, int));
252b5132
RH
3254 continue;
3255
3256 case 'z':
bf12938e 3257 MIPS16_INSERT_OPERAND (RZ, insn, va_arg (args, int));
252b5132
RH
3258 continue;
3259
3260 case 'Z':
bf12938e 3261 MIPS16_INSERT_OPERAND (MOVE32Z, insn, va_arg (args, int));
252b5132
RH
3262 continue;
3263
3264 case '0':
3265 case 'S':
3266 case 'P':
3267 case 'R':
3268 continue;
3269
3270 case 'X':
bf12938e 3271 MIPS16_INSERT_OPERAND (REGR32, insn, va_arg (args, int));
252b5132
RH
3272 continue;
3273
3274 case 'Y':
3275 {
3276 int regno;
3277
3278 regno = va_arg (args, int);
3279 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
3280 insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
3281 }
3282 continue;
3283
3284 case '<':
3285 case '>':
3286 case '4':
3287 case '5':
3288 case 'H':
3289 case 'W':
3290 case 'D':
3291 case 'j':
3292 case '8':
3293 case 'V':
3294 case 'C':
3295 case 'U':
3296 case 'k':
3297 case 'K':
3298 case 'p':
3299 case 'q':
3300 {
3301 assert (ep != NULL);
3302
3303 if (ep->X_op != O_constant)
874e8986 3304 *r = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
3305 else
3306 {
b34976b6
AM
3307 mips16_immed (NULL, 0, c, ep->X_add_number, FALSE, FALSE,
3308 FALSE, &insn.insn_opcode, &insn.use_extend,
c4e7957c 3309 &insn.extend);
252b5132 3310 ep = NULL;
f6688943 3311 *r = BFD_RELOC_UNUSED;
252b5132
RH
3312 }
3313 }
3314 continue;
3315
3316 case '6':
bf12938e 3317 MIPS16_INSERT_OPERAND (IMM6, insn, va_arg (args, int));
252b5132
RH
3318 continue;
3319 }
3320
3321 break;
3322 }
3323
f6688943 3324 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 3325
4d7206a2 3326 append_insn (&insn, ep, r);
252b5132
RH
3327}
3328
2051e8c4
MR
3329/*
3330 * Sign-extend 32-bit mode constants that have bit 31 set and all
3331 * higher bits unset.
3332 */
9f872bbe 3333static void
2051e8c4
MR
3334normalize_constant_expr (expressionS *ex)
3335{
9ee2a2d4 3336 if (ex->X_op == O_constant
2051e8c4
MR
3337 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
3338 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
3339 - 0x80000000);
3340}
3341
3342/*
3343 * Sign-extend 32-bit mode address offsets that have bit 31 set and
3344 * all higher bits unset.
3345 */
3346static void
3347normalize_address_expr (expressionS *ex)
3348{
3349 if (((ex->X_op == O_constant && HAVE_32BIT_ADDRESSES)
3350 || (ex->X_op == O_symbol && HAVE_32BIT_SYMBOLS))
3351 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
3352 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
3353 - 0x80000000);
3354}
3355
438c16b8
TS
3356/*
3357 * Generate a "jalr" instruction with a relocation hint to the called
3358 * function. This occurs in NewABI PIC code.
3359 */
3360static void
67c0d1eb 3361macro_build_jalr (expressionS *ep)
438c16b8 3362{
685736be 3363 char *f = NULL;
b34976b6 3364
438c16b8 3365 if (HAVE_NEWABI)
f21f8242 3366 {
cc3d92a5 3367 frag_grow (8);
f21f8242
AO
3368 f = frag_more (0);
3369 }
67c0d1eb 3370 macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
438c16b8 3371 if (HAVE_NEWABI)
f21f8242 3372 fix_new_exp (frag_now, f - frag_now->fr_literal,
a105a300 3373 4, ep, FALSE, BFD_RELOC_MIPS_JALR);
438c16b8
TS
3374}
3375
252b5132
RH
3376/*
3377 * Generate a "lui" instruction.
3378 */
3379static void
67c0d1eb 3380macro_build_lui (expressionS *ep, int regnum)
252b5132
RH
3381{
3382 expressionS high_expr;
1e915849 3383 const struct mips_opcode *mo;
252b5132 3384 struct mips_cl_insn insn;
f6688943
TS
3385 bfd_reloc_code_real_type r[3]
3386 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
5a38dc70
AM
3387 const char *name = "lui";
3388 const char *fmt = "t,u";
252b5132
RH
3389
3390 assert (! mips_opts.mips16);
3391
4d7206a2 3392 high_expr = *ep;
252b5132
RH
3393
3394 if (high_expr.X_op == O_constant)
3395 {
3396 /* we can compute the instruction now without a relocation entry */
e7d556df
TS
3397 high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3398 >> 16) & 0xffff;
f6688943 3399 *r = BFD_RELOC_UNUSED;
252b5132 3400 }
78e1bb40 3401 else
252b5132
RH
3402 {
3403 assert (ep->X_op == O_symbol);
bbe506e8
TS
3404 /* _gp_disp is a special case, used from s_cpload.
3405 __gnu_local_gp is used if mips_no_shared. */
252b5132 3406 assert (mips_pic == NO_PIC
78e1bb40 3407 || (! HAVE_NEWABI
aa6975fb
ILT
3408 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
3409 || (! mips_in_shared
bbe506e8
TS
3410 && strcmp (S_GET_NAME (ep->X_add_symbol),
3411 "__gnu_local_gp") == 0));
f6688943 3412 *r = BFD_RELOC_HI16_S;
252b5132
RH
3413 }
3414
1e915849
RS
3415 mo = hash_find (op_hash, name);
3416 assert (strcmp (name, mo->name) == 0);
3417 assert (strcmp (fmt, mo->args) == 0);
3418 create_insn (&insn, mo);
252b5132 3419
bf12938e
RS
3420 insn.insn_opcode = insn.insn_mo->match;
3421 INSERT_OPERAND (RT, insn, regnum);
f6688943 3422 if (*r == BFD_RELOC_UNUSED)
252b5132
RH
3423 {
3424 insn.insn_opcode |= high_expr.X_add_number;
4d7206a2 3425 append_insn (&insn, NULL, r);
252b5132
RH
3426 }
3427 else
4d7206a2 3428 append_insn (&insn, &high_expr, r);
252b5132
RH
3429}
3430
885add95
CD
3431/* Generate a sequence of instructions to do a load or store from a constant
3432 offset off of a base register (breg) into/from a target register (treg),
3433 using AT if necessary. */
3434static void
67c0d1eb
RS
3435macro_build_ldst_constoffset (expressionS *ep, const char *op,
3436 int treg, int breg, int dbl)
885add95
CD
3437{
3438 assert (ep->X_op == O_constant);
3439
256ab948 3440 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
3441 if (!dbl)
3442 normalize_constant_expr (ep);
256ab948 3443
67c1ffbe 3444 /* Right now, this routine can only handle signed 32-bit constants. */
ecd13cd3 3445 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
885add95
CD
3446 as_warn (_("operand overflow"));
3447
3448 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
3449 {
3450 /* Signed 16-bit offset will fit in the op. Easy! */
67c0d1eb 3451 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
885add95
CD
3452 }
3453 else
3454 {
3455 /* 32-bit offset, need multiple instructions and AT, like:
3456 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
3457 addu $tempreg,$tempreg,$breg
3458 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
3459 to handle the complete offset. */
67c0d1eb
RS
3460 macro_build_lui (ep, AT);
3461 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
3462 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
885add95
CD
3463
3464 if (mips_opts.noat)
8fc2e39e 3465 as_bad (_("Macro used $at after \".set noat\""));
885add95
CD
3466 }
3467}
3468
252b5132
RH
3469/* set_at()
3470 * Generates code to set the $at register to true (one)
3471 * if reg is less than the immediate expression.
3472 */
3473static void
67c0d1eb 3474set_at (int reg, int unsignedp)
252b5132
RH
3475{
3476 if (imm_expr.X_op == O_constant
3477 && imm_expr.X_add_number >= -0x8000
3478 && imm_expr.X_add_number < 0x8000)
67c0d1eb
RS
3479 macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
3480 AT, reg, BFD_RELOC_LO16);
252b5132
RH
3481 else
3482 {
67c0d1eb
RS
3483 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
3484 macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
252b5132
RH
3485 }
3486}
3487
3488/* Warn if an expression is not a constant. */
3489
3490static void
17a2f251 3491check_absolute_expr (struct mips_cl_insn *ip, expressionS *ex)
252b5132
RH
3492{
3493 if (ex->X_op == O_big)
3494 as_bad (_("unsupported large constant"));
3495 else if (ex->X_op != O_constant)
9ee2a2d4
MR
3496 as_bad (_("Instruction %s requires absolute expression"),
3497 ip->insn_mo->name);
13757d0c 3498
9ee2a2d4
MR
3499 if (HAVE_32BIT_GPRS)
3500 normalize_constant_expr (ex);
252b5132
RH
3501}
3502
3503/* Count the leading zeroes by performing a binary chop. This is a
3504 bulky bit of source, but performance is a LOT better for the
3505 majority of values than a simple loop to count the bits:
3506 for (lcnt = 0; (lcnt < 32); lcnt++)
3507 if ((v) & (1 << (31 - lcnt)))
3508 break;
3509 However it is not code size friendly, and the gain will drop a bit
3510 on certain cached systems.
3511*/
3512#define COUNT_TOP_ZEROES(v) \
3513 (((v) & ~0xffff) == 0 \
3514 ? ((v) & ~0xff) == 0 \
3515 ? ((v) & ~0xf) == 0 \
3516 ? ((v) & ~0x3) == 0 \
3517 ? ((v) & ~0x1) == 0 \
3518 ? !(v) \
3519 ? 32 \
3520 : 31 \
3521 : 30 \
3522 : ((v) & ~0x7) == 0 \
3523 ? 29 \
3524 : 28 \
3525 : ((v) & ~0x3f) == 0 \
3526 ? ((v) & ~0x1f) == 0 \
3527 ? 27 \
3528 : 26 \
3529 : ((v) & ~0x7f) == 0 \
3530 ? 25 \
3531 : 24 \
3532 : ((v) & ~0xfff) == 0 \
3533 ? ((v) & ~0x3ff) == 0 \
3534 ? ((v) & ~0x1ff) == 0 \
3535 ? 23 \
3536 : 22 \
3537 : ((v) & ~0x7ff) == 0 \
3538 ? 21 \
3539 : 20 \
3540 : ((v) & ~0x3fff) == 0 \
3541 ? ((v) & ~0x1fff) == 0 \
3542 ? 19 \
3543 : 18 \
3544 : ((v) & ~0x7fff) == 0 \
3545 ? 17 \
3546 : 16 \
3547 : ((v) & ~0xffffff) == 0 \
3548 ? ((v) & ~0xfffff) == 0 \
3549 ? ((v) & ~0x3ffff) == 0 \
3550 ? ((v) & ~0x1ffff) == 0 \
3551 ? 15 \
3552 : 14 \
3553 : ((v) & ~0x7ffff) == 0 \
3554 ? 13 \
3555 : 12 \
3556 : ((v) & ~0x3fffff) == 0 \
3557 ? ((v) & ~0x1fffff) == 0 \
3558 ? 11 \
3559 : 10 \
3560 : ((v) & ~0x7fffff) == 0 \
3561 ? 9 \
3562 : 8 \
3563 : ((v) & ~0xfffffff) == 0 \
3564 ? ((v) & ~0x3ffffff) == 0 \
3565 ? ((v) & ~0x1ffffff) == 0 \
3566 ? 7 \
3567 : 6 \
3568 : ((v) & ~0x7ffffff) == 0 \
3569 ? 5 \
3570 : 4 \
3571 : ((v) & ~0x3fffffff) == 0 \
3572 ? ((v) & ~0x1fffffff) == 0 \
3573 ? 3 \
3574 : 2 \
3575 : ((v) & ~0x7fffffff) == 0 \
3576 ? 1 \
3577 : 0)
3578
3579/* load_register()
67c1ffbe 3580 * This routine generates the least number of instructions necessary to load
252b5132
RH
3581 * an absolute expression value into a register.
3582 */
3583static void
67c0d1eb 3584load_register (int reg, expressionS *ep, int dbl)
252b5132
RH
3585{
3586 int freg;
3587 expressionS hi32, lo32;
3588
3589 if (ep->X_op != O_big)
3590 {
3591 assert (ep->X_op == O_constant);
256ab948
TS
3592
3593 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
3594 if (!dbl)
3595 normalize_constant_expr (ep);
256ab948
TS
3596
3597 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
252b5132
RH
3598 {
3599 /* We can handle 16 bit signed values with an addiu to
3600 $zero. No need to ever use daddiu here, since $zero and
3601 the result are always correct in 32 bit mode. */
67c0d1eb 3602 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
3603 return;
3604 }
3605 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3606 {
3607 /* We can handle 16 bit unsigned values with an ori to
3608 $zero. */
67c0d1eb 3609 macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
252b5132
RH
3610 return;
3611 }
256ab948 3612 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
252b5132
RH
3613 {
3614 /* 32 bit values require an lui. */
67c0d1eb 3615 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_HI16);
252b5132 3616 if ((ep->X_add_number & 0xffff) != 0)
67c0d1eb 3617 macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
252b5132
RH
3618 return;
3619 }
3620 }
3621
3622 /* The value is larger than 32 bits. */
3623
2051e8c4 3624 if (!dbl || HAVE_32BIT_GPRS)
252b5132 3625 {
55e08f71
NC
3626 char value[32];
3627
3628 sprintf_vma (value, ep->X_add_number);
20e1fcfd 3629 as_bad (_("Number (0x%s) larger than 32 bits"), value);
67c0d1eb 3630 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
3631 return;
3632 }
3633
3634 if (ep->X_op != O_big)
3635 {
3636 hi32 = *ep;
3637 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3638 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3639 hi32.X_add_number &= 0xffffffff;
3640 lo32 = *ep;
3641 lo32.X_add_number &= 0xffffffff;
3642 }
3643 else
3644 {
3645 assert (ep->X_add_number > 2);
3646 if (ep->X_add_number == 3)
3647 generic_bignum[3] = 0;
3648 else if (ep->X_add_number > 4)
3649 as_bad (_("Number larger than 64 bits"));
3650 lo32.X_op = O_constant;
3651 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3652 hi32.X_op = O_constant;
3653 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3654 }
3655
3656 if (hi32.X_add_number == 0)
3657 freg = 0;
3658 else
3659 {
3660 int shift, bit;
3661 unsigned long hi, lo;
3662
956cd1d6 3663 if (hi32.X_add_number == (offsetT) 0xffffffff)
beae10d5
KH
3664 {
3665 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3666 {
67c0d1eb 3667 macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
3668 return;
3669 }
3670 if (lo32.X_add_number & 0x80000000)
3671 {
67c0d1eb 3672 macro_build (&lo32, "lui", "t,u", reg, BFD_RELOC_HI16);
252b5132 3673 if (lo32.X_add_number & 0xffff)
67c0d1eb 3674 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
beae10d5
KH
3675 return;
3676 }
3677 }
252b5132
RH
3678
3679 /* Check for 16bit shifted constant. We know that hi32 is
3680 non-zero, so start the mask on the first bit of the hi32
3681 value. */
3682 shift = 17;
3683 do
beae10d5
KH
3684 {
3685 unsigned long himask, lomask;
3686
3687 if (shift < 32)
3688 {
3689 himask = 0xffff >> (32 - shift);
3690 lomask = (0xffff << shift) & 0xffffffff;
3691 }
3692 else
3693 {
3694 himask = 0xffff << (shift - 32);
3695 lomask = 0;
3696 }
3697 if ((hi32.X_add_number & ~(offsetT) himask) == 0
3698 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3699 {
3700 expressionS tmp;
3701
3702 tmp.X_op = O_constant;
3703 if (shift < 32)
3704 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3705 | (lo32.X_add_number >> shift));
3706 else
3707 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
67c0d1eb
RS
3708 macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
3709 macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", "d,w,<",
3710 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
3711 return;
3712 }
f9419b05 3713 ++shift;
beae10d5
KH
3714 }
3715 while (shift <= (64 - 16));
252b5132
RH
3716
3717 /* Find the bit number of the lowest one bit, and store the
3718 shifted value in hi/lo. */
3719 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3720 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3721 if (lo != 0)
3722 {
3723 bit = 0;
3724 while ((lo & 1) == 0)
3725 {
3726 lo >>= 1;
3727 ++bit;
3728 }
3729 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3730 hi >>= bit;
3731 }
3732 else
3733 {
3734 bit = 32;
3735 while ((hi & 1) == 0)
3736 {
3737 hi >>= 1;
3738 ++bit;
3739 }
3740 lo = hi;
3741 hi = 0;
3742 }
3743
3744 /* Optimize if the shifted value is a (power of 2) - 1. */
3745 if ((hi == 0 && ((lo + 1) & lo) == 0)
3746 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
beae10d5
KH
3747 {
3748 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
252b5132 3749 if (shift != 0)
beae10d5 3750 {
252b5132
RH
3751 expressionS tmp;
3752
3753 /* This instruction will set the register to be all
3754 ones. */
beae10d5
KH
3755 tmp.X_op = O_constant;
3756 tmp.X_add_number = (offsetT) -1;
67c0d1eb 3757 macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
3758 if (bit != 0)
3759 {
3760 bit += shift;
67c0d1eb
RS
3761 macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", "d,w,<",
3762 reg, reg, (bit >= 32) ? bit - 32 : bit);
beae10d5 3763 }
67c0d1eb
RS
3764 macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", "d,w,<",
3765 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
3766 return;
3767 }
3768 }
252b5132
RH
3769
3770 /* Sign extend hi32 before calling load_register, because we can
3771 generally get better code when we load a sign extended value. */
3772 if ((hi32.X_add_number & 0x80000000) != 0)
beae10d5 3773 hi32.X_add_number |= ~(offsetT) 0xffffffff;
67c0d1eb 3774 load_register (reg, &hi32, 0);
252b5132
RH
3775 freg = reg;
3776 }
3777 if ((lo32.X_add_number & 0xffff0000) == 0)
3778 {
3779 if (freg != 0)
3780 {
67c0d1eb 3781 macro_build (NULL, "dsll32", "d,w,<", reg, freg, 0);
252b5132
RH
3782 freg = reg;
3783 }
3784 }
3785 else
3786 {
3787 expressionS mid16;
3788
956cd1d6 3789 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
beae10d5 3790 {
67c0d1eb
RS
3791 macro_build (&lo32, "lui", "t,u", reg, BFD_RELOC_HI16);
3792 macro_build (NULL, "dsrl32", "d,w,<", reg, reg, 0);
beae10d5
KH
3793 return;
3794 }
252b5132
RH
3795
3796 if (freg != 0)
3797 {
67c0d1eb 3798 macro_build (NULL, "dsll", "d,w,<", reg, freg, 16);
252b5132
RH
3799 freg = reg;
3800 }
3801 mid16 = lo32;
3802 mid16.X_add_number >>= 16;
67c0d1eb
RS
3803 macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
3804 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
252b5132
RH
3805 freg = reg;
3806 }
3807 if ((lo32.X_add_number & 0xffff) != 0)
67c0d1eb 3808 macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
252b5132
RH
3809}
3810
269137b2
TS
3811static inline void
3812load_delay_nop (void)
3813{
3814 if (!gpr_interlocks)
3815 macro_build (NULL, "nop", "");
3816}
3817
252b5132
RH
3818/* Load an address into a register. */
3819
3820static void
67c0d1eb 3821load_address (int reg, expressionS *ep, int *used_at)
252b5132 3822{
252b5132
RH
3823 if (ep->X_op != O_constant
3824 && ep->X_op != O_symbol)
3825 {
3826 as_bad (_("expression too complex"));
3827 ep->X_op = O_constant;
3828 }
3829
3830 if (ep->X_op == O_constant)
3831 {
67c0d1eb 3832 load_register (reg, ep, HAVE_64BIT_ADDRESSES);
252b5132
RH
3833 return;
3834 }
3835
3836 if (mips_pic == NO_PIC)
3837 {
3838 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 3839 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
3840 Otherwise we want
3841 lui $reg,<sym> (BFD_RELOC_HI16_S)
3842 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
d6bc6245 3843 If we have an addend, we always use the latter form.
76b3015f 3844
d6bc6245
TS
3845 With 64bit address space and a usable $at we want
3846 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3847 lui $at,<sym> (BFD_RELOC_HI16_S)
3848 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3849 daddiu $at,<sym> (BFD_RELOC_LO16)
3850 dsll32 $reg,0
3a482fd5 3851 daddu $reg,$reg,$at
76b3015f 3852
c03099e6 3853 If $at is already in use, we use a path which is suboptimal
d6bc6245
TS
3854 on superscalar processors.
3855 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3856 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3857 dsll $reg,16
3858 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
3859 dsll $reg,16
3860 daddiu $reg,<sym> (BFD_RELOC_LO16)
6caf9ef4
TS
3861
3862 For GP relative symbols in 64bit address space we can use
3863 the same sequence as in 32bit address space. */
aed1a261 3864 if (HAVE_64BIT_SYMBOLS)
d6bc6245 3865 {
6caf9ef4
TS
3866 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3867 && !nopic_need_relax (ep->X_add_symbol, 1))
3868 {
3869 relax_start (ep->X_add_symbol);
3870 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
3871 mips_gp_register, BFD_RELOC_GPREL16);
3872 relax_switch ();
3873 }
d6bc6245 3874
b8285c27 3875 if (*used_at == 0 && !mips_opts.noat)
d6bc6245 3876 {
67c0d1eb
RS
3877 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_HIGHEST);
3878 macro_build (ep, "lui", "t,u", AT, BFD_RELOC_HI16_S);
3879 macro_build (ep, "daddiu", "t,r,j", reg, reg,
3880 BFD_RELOC_MIPS_HIGHER);
3881 macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
3882 macro_build (NULL, "dsll32", "d,w,<", reg, reg, 0);
3883 macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
d6bc6245
TS
3884 *used_at = 1;
3885 }
3886 else
3887 {
67c0d1eb
RS
3888 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_HIGHEST);
3889 macro_build (ep, "daddiu", "t,r,j", reg, reg,
3890 BFD_RELOC_MIPS_HIGHER);
3891 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
3892 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
3893 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
3894 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
d6bc6245 3895 }
6caf9ef4
TS
3896
3897 if (mips_relax.sequence)
3898 relax_end ();
d6bc6245 3899 }
252b5132
RH
3900 else
3901 {
d6bc6245 3902 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 3903 && !nopic_need_relax (ep->X_add_symbol, 1))
d6bc6245 3904 {
4d7206a2 3905 relax_start (ep->X_add_symbol);
67c0d1eb 3906 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
17a2f251 3907 mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 3908 relax_switch ();
d6bc6245 3909 }
67c0d1eb
RS
3910 macro_build_lui (ep, reg);
3911 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
3912 reg, reg, BFD_RELOC_LO16);
4d7206a2
RS
3913 if (mips_relax.sequence)
3914 relax_end ();
d6bc6245 3915 }
252b5132 3916 }
0a44bf69 3917 else if (!mips_big_got)
252b5132
RH
3918 {
3919 expressionS ex;
3920
3921 /* If this is a reference to an external symbol, we want
3922 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3923 Otherwise we want
3924 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3925 nop
3926 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
f5040a92
AO
3927 If there is a constant, it must be added in after.
3928
ed6fb7bd 3929 If we have NewABI, we want
f5040a92
AO
3930 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
3931 unless we're referencing a global symbol with a non-zero
3932 offset, in which case cst must be added separately. */
ed6fb7bd
SC
3933 if (HAVE_NEWABI)
3934 {
f5040a92
AO
3935 if (ep->X_add_number)
3936 {
4d7206a2 3937 ex.X_add_number = ep->X_add_number;
f5040a92 3938 ep->X_add_number = 0;
4d7206a2 3939 relax_start (ep->X_add_symbol);
67c0d1eb
RS
3940 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3941 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
3942 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3943 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3944 ex.X_op = O_constant;
67c0d1eb 3945 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 3946 reg, reg, BFD_RELOC_LO16);
f5040a92 3947 ep->X_add_number = ex.X_add_number;
4d7206a2 3948 relax_switch ();
f5040a92 3949 }
67c0d1eb 3950 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 3951 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2
RS
3952 if (mips_relax.sequence)
3953 relax_end ();
ed6fb7bd
SC
3954 }
3955 else
3956 {
f5040a92
AO
3957 ex.X_add_number = ep->X_add_number;
3958 ep->X_add_number = 0;
67c0d1eb
RS
3959 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3960 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 3961 load_delay_nop ();
4d7206a2
RS
3962 relax_start (ep->X_add_symbol);
3963 relax_switch ();
67c0d1eb 3964 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 3965 BFD_RELOC_LO16);
4d7206a2 3966 relax_end ();
ed6fb7bd 3967
f5040a92
AO
3968 if (ex.X_add_number != 0)
3969 {
3970 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3971 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3972 ex.X_op = O_constant;
67c0d1eb 3973 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 3974 reg, reg, BFD_RELOC_LO16);
f5040a92 3975 }
252b5132
RH
3976 }
3977 }
0a44bf69 3978 else if (mips_big_got)
252b5132
RH
3979 {
3980 expressionS ex;
252b5132
RH
3981
3982 /* This is the large GOT case. If this is a reference to an
3983 external symbol, we want
3984 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
3985 addu $reg,$reg,$gp
3986 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
f5040a92
AO
3987
3988 Otherwise, for a reference to a local symbol in old ABI, we want
252b5132
RH
3989 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3990 nop
3991 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
684022ea 3992 If there is a constant, it must be added in after.
f5040a92
AO
3993
3994 In the NewABI, for local symbols, with or without offsets, we want:
438c16b8
TS
3995 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
3996 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
f5040a92 3997 */
438c16b8
TS
3998 if (HAVE_NEWABI)
3999 {
4d7206a2 4000 ex.X_add_number = ep->X_add_number;
f5040a92 4001 ep->X_add_number = 0;
4d7206a2 4002 relax_start (ep->X_add_symbol);
67c0d1eb
RS
4003 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_GOT_HI16);
4004 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
4005 reg, reg, mips_gp_register);
4006 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
4007 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
f5040a92
AO
4008 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4009 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4010 else if (ex.X_add_number)
4011 {
4012 ex.X_op = O_constant;
67c0d1eb
RS
4013 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
4014 BFD_RELOC_LO16);
f5040a92
AO
4015 }
4016
4017 ep->X_add_number = ex.X_add_number;
4d7206a2 4018 relax_switch ();
67c0d1eb 4019 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 4020 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
67c0d1eb
RS
4021 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
4022 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 4023 relax_end ();
438c16b8 4024 }
252b5132 4025 else
438c16b8 4026 {
f5040a92
AO
4027 ex.X_add_number = ep->X_add_number;
4028 ep->X_add_number = 0;
4d7206a2 4029 relax_start (ep->X_add_symbol);
67c0d1eb
RS
4030 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_GOT_HI16);
4031 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
4032 reg, reg, mips_gp_register);
4033 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
4034 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
4d7206a2
RS
4035 relax_switch ();
4036 if (reg_needs_delay (mips_gp_register))
438c16b8
TS
4037 {
4038 /* We need a nop before loading from $gp. This special
4039 check is required because the lui which starts the main
4040 instruction stream does not refer to $gp, and so will not
4041 insert the nop which may be required. */
67c0d1eb 4042 macro_build (NULL, "nop", "");
438c16b8 4043 }
67c0d1eb 4044 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 4045 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 4046 load_delay_nop ();
67c0d1eb 4047 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 4048 BFD_RELOC_LO16);
4d7206a2 4049 relax_end ();
438c16b8 4050
f5040a92
AO
4051 if (ex.X_add_number != 0)
4052 {
4053 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4054 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4055 ex.X_op = O_constant;
67c0d1eb
RS
4056 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
4057 BFD_RELOC_LO16);
f5040a92 4058 }
252b5132
RH
4059 }
4060 }
252b5132
RH
4061 else
4062 abort ();
8fc2e39e
TS
4063
4064 if (mips_opts.noat && *used_at == 1)
4065 as_bad (_("Macro used $at after \".set noat\""));
252b5132
RH
4066}
4067
ea1fb5dc
RS
4068/* Move the contents of register SOURCE into register DEST. */
4069
4070static void
67c0d1eb 4071move_register (int dest, int source)
ea1fb5dc 4072{
67c0d1eb
RS
4073 macro_build (NULL, HAVE_32BIT_GPRS ? "addu" : "daddu", "d,v,t",
4074 dest, source, 0);
ea1fb5dc
RS
4075}
4076
4d7206a2 4077/* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
f6a22291
MR
4078 LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
4079 The two alternatives are:
4d7206a2
RS
4080
4081 Global symbol Local sybmol
4082 ------------- ------------
4083 lw DEST,%got(SYMBOL) lw DEST,%got(SYMBOL + OFFSET)
4084 ... ...
4085 addiu DEST,DEST,OFFSET addiu DEST,DEST,%lo(SYMBOL + OFFSET)
4086
4087 load_got_offset emits the first instruction and add_got_offset
f6a22291
MR
4088 emits the second for a 16-bit offset or add_got_offset_hilo emits
4089 a sequence to add a 32-bit offset using a scratch register. */
4d7206a2
RS
4090
4091static void
67c0d1eb 4092load_got_offset (int dest, expressionS *local)
4d7206a2
RS
4093{
4094 expressionS global;
4095
4096 global = *local;
4097 global.X_add_number = 0;
4098
4099 relax_start (local->X_add_symbol);
67c0d1eb
RS
4100 macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
4101 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2 4102 relax_switch ();
67c0d1eb
RS
4103 macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
4104 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2
RS
4105 relax_end ();
4106}
4107
4108static void
67c0d1eb 4109add_got_offset (int dest, expressionS *local)
4d7206a2
RS
4110{
4111 expressionS global;
4112
4113 global.X_op = O_constant;
4114 global.X_op_symbol = NULL;
4115 global.X_add_symbol = NULL;
4116 global.X_add_number = local->X_add_number;
4117
4118 relax_start (local->X_add_symbol);
67c0d1eb 4119 macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
4d7206a2
RS
4120 dest, dest, BFD_RELOC_LO16);
4121 relax_switch ();
67c0d1eb 4122 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
4d7206a2
RS
4123 relax_end ();
4124}
4125
f6a22291
MR
4126static void
4127add_got_offset_hilo (int dest, expressionS *local, int tmp)
4128{
4129 expressionS global;
4130 int hold_mips_optimize;
4131
4132 global.X_op = O_constant;
4133 global.X_op_symbol = NULL;
4134 global.X_add_symbol = NULL;
4135 global.X_add_number = local->X_add_number;
4136
4137 relax_start (local->X_add_symbol);
4138 load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
4139 relax_switch ();
4140 /* Set mips_optimize around the lui instruction to avoid
4141 inserting an unnecessary nop after the lw. */
4142 hold_mips_optimize = mips_optimize;
4143 mips_optimize = 2;
4144 macro_build_lui (&global, tmp);
4145 mips_optimize = hold_mips_optimize;
4146 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
4147 relax_end ();
4148
4149 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
4150}
4151
252b5132
RH
4152/*
4153 * Build macros
4154 * This routine implements the seemingly endless macro or synthesized
4155 * instructions and addressing modes in the mips assembly language. Many
4156 * of these macros are simple and are similar to each other. These could
67c1ffbe 4157 * probably be handled by some kind of table or grammar approach instead of
252b5132
RH
4158 * this verbose method. Others are not simple macros but are more like
4159 * optimizing code generation.
4160 * One interesting optimization is when several store macros appear
67c1ffbe 4161 * consecutively that would load AT with the upper half of the same address.
252b5132
RH
4162 * The ensuing load upper instructions are ommited. This implies some kind
4163 * of global optimization. We currently only optimize within a single macro.
4164 * For many of the load and store macros if the address is specified as a
4165 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
4166 * first load register 'at' with zero and use it as the base register. The
4167 * mips assembler simply uses register $zero. Just one tiny optimization
4168 * we're missing.
4169 */
4170static void
17a2f251 4171macro (struct mips_cl_insn *ip)
252b5132
RH
4172{
4173 register int treg, sreg, dreg, breg;
4174 int tempreg;
4175 int mask;
43841e91 4176 int used_at = 0;
252b5132
RH
4177 expressionS expr1;
4178 const char *s;
4179 const char *s2;
4180 const char *fmt;
4181 int likely = 0;
4182 int dbl = 0;
4183 int coproc = 0;
4184 int lr = 0;
4185 int imm = 0;
1abe91b1 4186 int call = 0;
252b5132 4187 int off;
67c0d1eb 4188 offsetT maxnum;
252b5132 4189 bfd_reloc_code_real_type r;
252b5132
RH
4190 int hold_mips_optimize;
4191
4192 assert (! mips_opts.mips16);
4193
4194 treg = (ip->insn_opcode >> 16) & 0x1f;
4195 dreg = (ip->insn_opcode >> 11) & 0x1f;
4196 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
4197 mask = ip->insn_mo->mask;
4198
4199 expr1.X_op = O_constant;
4200 expr1.X_op_symbol = NULL;
4201 expr1.X_add_symbol = NULL;
4202 expr1.X_add_number = 1;
4203
4204 switch (mask)
4205 {
4206 case M_DABS:
4207 dbl = 1;
4208 case M_ABS:
4209 /* bgez $a0,.+12
4210 move v0,$a0
4211 sub v0,$zero,$a0
4212 */
4213
7d10b47d 4214 start_noreorder ();
252b5132
RH
4215
4216 expr1.X_add_number = 8;
67c0d1eb 4217 macro_build (&expr1, "bgez", "s,p", sreg);
252b5132 4218 if (dreg == sreg)
67c0d1eb 4219 macro_build (NULL, "nop", "", 0);
252b5132 4220 else
67c0d1eb
RS
4221 move_register (dreg, sreg);
4222 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
252b5132 4223
7d10b47d 4224 end_noreorder ();
8fc2e39e 4225 break;
252b5132
RH
4226
4227 case M_ADD_I:
4228 s = "addi";
4229 s2 = "add";
4230 goto do_addi;
4231 case M_ADDU_I:
4232 s = "addiu";
4233 s2 = "addu";
4234 goto do_addi;
4235 case M_DADD_I:
4236 dbl = 1;
4237 s = "daddi";
4238 s2 = "dadd";
4239 goto do_addi;
4240 case M_DADDU_I:
4241 dbl = 1;
4242 s = "daddiu";
4243 s2 = "daddu";
4244 do_addi:
4245 if (imm_expr.X_op == O_constant
4246 && imm_expr.X_add_number >= -0x8000
4247 && imm_expr.X_add_number < 0x8000)
4248 {
67c0d1eb 4249 macro_build (&imm_expr, s, "t,r,j", treg, sreg, BFD_RELOC_LO16);
8fc2e39e 4250 break;
252b5132 4251 }
8fc2e39e 4252 used_at = 1;
67c0d1eb
RS
4253 load_register (AT, &imm_expr, dbl);
4254 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
252b5132
RH
4255 break;
4256
4257 case M_AND_I:
4258 s = "andi";
4259 s2 = "and";
4260 goto do_bit;
4261 case M_OR_I:
4262 s = "ori";
4263 s2 = "or";
4264 goto do_bit;
4265 case M_NOR_I:
4266 s = "";
4267 s2 = "nor";
4268 goto do_bit;
4269 case M_XOR_I:
4270 s = "xori";
4271 s2 = "xor";
4272 do_bit:
4273 if (imm_expr.X_op == O_constant
4274 && imm_expr.X_add_number >= 0
4275 && imm_expr.X_add_number < 0x10000)
4276 {
4277 if (mask != M_NOR_I)
67c0d1eb 4278 macro_build (&imm_expr, s, "t,r,i", treg, sreg, BFD_RELOC_LO16);
252b5132
RH
4279 else
4280 {
67c0d1eb
RS
4281 macro_build (&imm_expr, "ori", "t,r,i",
4282 treg, sreg, BFD_RELOC_LO16);
4283 macro_build (NULL, "nor", "d,v,t", treg, treg, 0);
252b5132 4284 }
8fc2e39e 4285 break;
252b5132
RH
4286 }
4287
8fc2e39e 4288 used_at = 1;
67c0d1eb
RS
4289 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
4290 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
252b5132
RH
4291 break;
4292
4293 case M_BEQ_I:
4294 s = "beq";
4295 goto beq_i;
4296 case M_BEQL_I:
4297 s = "beql";
4298 likely = 1;
4299 goto beq_i;
4300 case M_BNE_I:
4301 s = "bne";
4302 goto beq_i;
4303 case M_BNEL_I:
4304 s = "bnel";
4305 likely = 1;
4306 beq_i:
4307 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4308 {
67c0d1eb 4309 macro_build (&offset_expr, s, "s,t,p", sreg, 0);
8fc2e39e 4310 break;
252b5132 4311 }
8fc2e39e 4312 used_at = 1;
67c0d1eb
RS
4313 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
4314 macro_build (&offset_expr, s, "s,t,p", sreg, AT);
252b5132
RH
4315 break;
4316
4317 case M_BGEL:
4318 likely = 1;
4319 case M_BGE:
4320 if (treg == 0)
4321 {
67c0d1eb 4322 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", sreg);
8fc2e39e 4323 break;
252b5132
RH
4324 }
4325 if (sreg == 0)
4326 {
67c0d1eb 4327 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", treg);
8fc2e39e 4328 break;
252b5132 4329 }
8fc2e39e 4330 used_at = 1;
67c0d1eb
RS
4331 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
4332 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4333 break;
4334
4335 case M_BGTL_I:
4336 likely = 1;
4337 case M_BGT_I:
4338 /* check for > max integer */
4339 maxnum = 0x7fffffff;
ca4e0257 4340 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
4341 {
4342 maxnum <<= 16;
4343 maxnum |= 0xffff;
4344 maxnum <<= 16;
4345 maxnum |= 0xffff;
4346 }
4347 if (imm_expr.X_op == O_constant
4348 && imm_expr.X_add_number >= maxnum
ca4e0257 4349 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
4350 {
4351 do_false:
4352 /* result is always false */
4353 if (! likely)
67c0d1eb 4354 macro_build (NULL, "nop", "", 0);
252b5132 4355 else
67c0d1eb 4356 macro_build (&offset_expr, "bnel", "s,t,p", 0, 0);
8fc2e39e 4357 break;
252b5132
RH
4358 }
4359 if (imm_expr.X_op != O_constant)
4360 as_bad (_("Unsupported large constant"));
f9419b05 4361 ++imm_expr.X_add_number;
252b5132
RH
4362 /* FALLTHROUGH */
4363 case M_BGE_I:
4364 case M_BGEL_I:
4365 if (mask == M_BGEL_I)
4366 likely = 1;
4367 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4368 {
67c0d1eb 4369 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", sreg);
8fc2e39e 4370 break;
252b5132
RH
4371 }
4372 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4373 {
67c0d1eb 4374 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", sreg);
8fc2e39e 4375 break;
252b5132
RH
4376 }
4377 maxnum = 0x7fffffff;
ca4e0257 4378 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
4379 {
4380 maxnum <<= 16;
4381 maxnum |= 0xffff;
4382 maxnum <<= 16;
4383 maxnum |= 0xffff;
4384 }
4385 maxnum = - maxnum - 1;
4386 if (imm_expr.X_op == O_constant
4387 && imm_expr.X_add_number <= maxnum
ca4e0257 4388 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
4389 {
4390 do_true:
4391 /* result is always true */
4392 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
67c0d1eb 4393 macro_build (&offset_expr, "b", "p");
8fc2e39e 4394 break;
252b5132 4395 }
8fc2e39e 4396 used_at = 1;
67c0d1eb
RS
4397 set_at (sreg, 0);
4398 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4399 break;
4400
4401 case M_BGEUL:
4402 likely = 1;
4403 case M_BGEU:
4404 if (treg == 0)
4405 goto do_true;
4406 if (sreg == 0)
4407 {
67c0d1eb 4408 macro_build (&offset_expr, likely ? "beql" : "beq",
17a2f251 4409 "s,t,p", 0, treg);
8fc2e39e 4410 break;
252b5132 4411 }
8fc2e39e 4412 used_at = 1;
67c0d1eb
RS
4413 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
4414 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4415 break;
4416
4417 case M_BGTUL_I:
4418 likely = 1;
4419 case M_BGTU_I:
4420 if (sreg == 0
ca4e0257 4421 || (HAVE_32BIT_GPRS
252b5132 4422 && imm_expr.X_op == O_constant
956cd1d6 4423 && imm_expr.X_add_number == (offsetT) 0xffffffff))
252b5132
RH
4424 goto do_false;
4425 if (imm_expr.X_op != O_constant)
4426 as_bad (_("Unsupported large constant"));
f9419b05 4427 ++imm_expr.X_add_number;
252b5132
RH
4428 /* FALLTHROUGH */
4429 case M_BGEU_I:
4430 case M_BGEUL_I:
4431 if (mask == M_BGEUL_I)
4432 likely = 1;
4433 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4434 goto do_true;
4435 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4436 {
67c0d1eb 4437 macro_build (&offset_expr, likely ? "bnel" : "bne",
17a2f251 4438 "s,t,p", sreg, 0);
8fc2e39e 4439 break;
252b5132 4440 }
8fc2e39e 4441 used_at = 1;
67c0d1eb
RS
4442 set_at (sreg, 1);
4443 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4444 break;
4445
4446 case M_BGTL:
4447 likely = 1;
4448 case M_BGT:
4449 if (treg == 0)
4450 {
67c0d1eb 4451 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", sreg);
8fc2e39e 4452 break;
252b5132
RH
4453 }
4454 if (sreg == 0)
4455 {
67c0d1eb 4456 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", treg);
8fc2e39e 4457 break;
252b5132 4458 }
8fc2e39e 4459 used_at = 1;
67c0d1eb
RS
4460 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
4461 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4462 break;
4463
4464 case M_BGTUL:
4465 likely = 1;
4466 case M_BGTU:
4467 if (treg == 0)
4468 {
67c0d1eb 4469 macro_build (&offset_expr, likely ? "bnel" : "bne",
17a2f251 4470 "s,t,p", sreg, 0);
8fc2e39e 4471 break;
252b5132
RH
4472 }
4473 if (sreg == 0)
4474 goto do_false;
8fc2e39e 4475 used_at = 1;
67c0d1eb
RS
4476 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
4477 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4478 break;
4479
4480 case M_BLEL:
4481 likely = 1;
4482 case M_BLE:
4483 if (treg == 0)
4484 {
67c0d1eb 4485 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", sreg);
8fc2e39e 4486 break;
252b5132
RH
4487 }
4488 if (sreg == 0)
4489 {
67c0d1eb 4490 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", treg);
8fc2e39e 4491 break;
252b5132 4492 }
8fc2e39e 4493 used_at = 1;
67c0d1eb
RS
4494 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
4495 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4496 break;
4497
4498 case M_BLEL_I:
4499 likely = 1;
4500 case M_BLE_I:
4501 maxnum = 0x7fffffff;
ca4e0257 4502 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
4503 {
4504 maxnum <<= 16;
4505 maxnum |= 0xffff;
4506 maxnum <<= 16;
4507 maxnum |= 0xffff;
4508 }
4509 if (imm_expr.X_op == O_constant
4510 && imm_expr.X_add_number >= maxnum
ca4e0257 4511 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
4512 goto do_true;
4513 if (imm_expr.X_op != O_constant)
4514 as_bad (_("Unsupported large constant"));
f9419b05 4515 ++imm_expr.X_add_number;
252b5132
RH
4516 /* FALLTHROUGH */
4517 case M_BLT_I:
4518 case M_BLTL_I:
4519 if (mask == M_BLTL_I)
4520 likely = 1;
4521 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4522 {
67c0d1eb 4523 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", sreg);
8fc2e39e 4524 break;
252b5132
RH
4525 }
4526 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4527 {
67c0d1eb 4528 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", sreg);
8fc2e39e 4529 break;
252b5132 4530 }
8fc2e39e 4531 used_at = 1;
67c0d1eb
RS
4532 set_at (sreg, 0);
4533 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4534 break;
4535
4536 case M_BLEUL:
4537 likely = 1;
4538 case M_BLEU:
4539 if (treg == 0)
4540 {
67c0d1eb 4541 macro_build (&offset_expr, likely ? "beql" : "beq",
17a2f251 4542 "s,t,p", sreg, 0);
8fc2e39e 4543 break;
252b5132
RH
4544 }
4545 if (sreg == 0)
4546 goto do_true;
8fc2e39e 4547 used_at = 1;
67c0d1eb
RS
4548 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
4549 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4550 break;
4551
4552 case M_BLEUL_I:
4553 likely = 1;
4554 case M_BLEU_I:
4555 if (sreg == 0
ca4e0257 4556 || (HAVE_32BIT_GPRS
252b5132 4557 && imm_expr.X_op == O_constant
956cd1d6 4558 && imm_expr.X_add_number == (offsetT) 0xffffffff))
252b5132
RH
4559 goto do_true;
4560 if (imm_expr.X_op != O_constant)
4561 as_bad (_("Unsupported large constant"));
f9419b05 4562 ++imm_expr.X_add_number;
252b5132
RH
4563 /* FALLTHROUGH */
4564 case M_BLTU_I:
4565 case M_BLTUL_I:
4566 if (mask == M_BLTUL_I)
4567 likely = 1;
4568 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4569 goto do_false;
4570 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4571 {
67c0d1eb 4572 macro_build (&offset_expr, likely ? "beql" : "beq",
252b5132 4573 "s,t,p", sreg, 0);
8fc2e39e 4574 break;
252b5132 4575 }
8fc2e39e 4576 used_at = 1;
67c0d1eb
RS
4577 set_at (sreg, 1);
4578 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4579 break;
4580
4581 case M_BLTL:
4582 likely = 1;
4583 case M_BLT:
4584 if (treg == 0)
4585 {
67c0d1eb 4586 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", sreg);
8fc2e39e 4587 break;
252b5132
RH
4588 }
4589 if (sreg == 0)
4590 {
67c0d1eb 4591 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", treg);
8fc2e39e 4592 break;
252b5132 4593 }
8fc2e39e 4594 used_at = 1;
67c0d1eb
RS
4595 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
4596 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4597 break;
4598
4599 case M_BLTUL:
4600 likely = 1;
4601 case M_BLTU:
4602 if (treg == 0)
4603 goto do_false;
4604 if (sreg == 0)
4605 {
67c0d1eb 4606 macro_build (&offset_expr, likely ? "bnel" : "bne",
17a2f251 4607 "s,t,p", 0, treg);
8fc2e39e 4608 break;
252b5132 4609 }
8fc2e39e 4610 used_at = 1;
67c0d1eb
RS
4611 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
4612 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4613 break;
4614
5f74bc13
CD
4615 case M_DEXT:
4616 {
4617 unsigned long pos;
4618 unsigned long size;
4619
4620 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
4621 {
4622 as_bad (_("Unsupported large constant"));
4623 pos = size = 1;
4624 }
4625 else
4626 {
4627 pos = (unsigned long) imm_expr.X_add_number;
4628 size = (unsigned long) imm2_expr.X_add_number;
4629 }
4630
4631 if (pos > 63)
4632 {
4633 as_bad (_("Improper position (%lu)"), pos);
4634 pos = 1;
4635 }
4636 if (size == 0 || size > 64
4637 || (pos + size - 1) > 63)
4638 {
4639 as_bad (_("Improper extract size (%lu, position %lu)"),
4640 size, pos);
4641 size = 1;
4642 }
4643
4644 if (size <= 32 && pos < 32)
4645 {
4646 s = "dext";
4647 fmt = "t,r,+A,+C";
4648 }
4649 else if (size <= 32)
4650 {
4651 s = "dextu";
4652 fmt = "t,r,+E,+H";
4653 }
4654 else
4655 {
4656 s = "dextm";
4657 fmt = "t,r,+A,+G";
4658 }
67c0d1eb 4659 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, pos, size - 1);
5f74bc13 4660 }
8fc2e39e 4661 break;
5f74bc13
CD
4662
4663 case M_DINS:
4664 {
4665 unsigned long pos;
4666 unsigned long size;
4667
4668 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
4669 {
4670 as_bad (_("Unsupported large constant"));
4671 pos = size = 1;
4672 }
4673 else
4674 {
4675 pos = (unsigned long) imm_expr.X_add_number;
4676 size = (unsigned long) imm2_expr.X_add_number;
4677 }
4678
4679 if (pos > 63)
4680 {
4681 as_bad (_("Improper position (%lu)"), pos);
4682 pos = 1;
4683 }
4684 if (size == 0 || size > 64
4685 || (pos + size - 1) > 63)
4686 {
4687 as_bad (_("Improper insert size (%lu, position %lu)"),
4688 size, pos);
4689 size = 1;
4690 }
4691
4692 if (pos < 32 && (pos + size - 1) < 32)
4693 {
4694 s = "dins";
4695 fmt = "t,r,+A,+B";
4696 }
4697 else if (pos >= 32)
4698 {
4699 s = "dinsu";
4700 fmt = "t,r,+E,+F";
4701 }
4702 else
4703 {
4704 s = "dinsm";
4705 fmt = "t,r,+A,+F";
4706 }
67c0d1eb
RS
4707 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, pos,
4708 pos + size - 1);
5f74bc13 4709 }
8fc2e39e 4710 break;
5f74bc13 4711
252b5132
RH
4712 case M_DDIV_3:
4713 dbl = 1;
4714 case M_DIV_3:
4715 s = "mflo";
4716 goto do_div3;
4717 case M_DREM_3:
4718 dbl = 1;
4719 case M_REM_3:
4720 s = "mfhi";
4721 do_div3:
4722 if (treg == 0)
4723 {
4724 as_warn (_("Divide by zero."));
4725 if (mips_trap)
67c0d1eb 4726 macro_build (NULL, "teq", "s,t,q", 0, 0, 7);
252b5132 4727 else
67c0d1eb 4728 macro_build (NULL, "break", "c", 7);
8fc2e39e 4729 break;
252b5132
RH
4730 }
4731
7d10b47d 4732 start_noreorder ();
252b5132
RH
4733 if (mips_trap)
4734 {
67c0d1eb
RS
4735 macro_build (NULL, "teq", "s,t,q", treg, 0, 7);
4736 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
252b5132
RH
4737 }
4738 else
4739 {
4740 expr1.X_add_number = 8;
67c0d1eb
RS
4741 macro_build (&expr1, "bne", "s,t,p", treg, 0);
4742 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4743 macro_build (NULL, "break", "c", 7);
252b5132
RH
4744 }
4745 expr1.X_add_number = -1;
8fc2e39e 4746 used_at = 1;
f6a22291 4747 load_register (AT, &expr1, dbl);
252b5132 4748 expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
67c0d1eb 4749 macro_build (&expr1, "bne", "s,t,p", treg, AT);
252b5132
RH
4750 if (dbl)
4751 {
4752 expr1.X_add_number = 1;
f6a22291 4753 load_register (AT, &expr1, dbl);
67c0d1eb 4754 macro_build (NULL, "dsll32", "d,w,<", AT, AT, 31);
252b5132
RH
4755 }
4756 else
4757 {
4758 expr1.X_add_number = 0x80000000;
67c0d1eb 4759 macro_build (&expr1, "lui", "t,u", AT, BFD_RELOC_HI16);
252b5132
RH
4760 }
4761 if (mips_trap)
4762 {
67c0d1eb 4763 macro_build (NULL, "teq", "s,t,q", sreg, AT, 6);
252b5132
RH
4764 /* We want to close the noreorder block as soon as possible, so
4765 that later insns are available for delay slot filling. */
7d10b47d 4766 end_noreorder ();
252b5132
RH
4767 }
4768 else
4769 {
4770 expr1.X_add_number = 8;
67c0d1eb
RS
4771 macro_build (&expr1, "bne", "s,t,p", sreg, AT);
4772 macro_build (NULL, "nop", "", 0);
252b5132
RH
4773
4774 /* We want to close the noreorder block as soon as possible, so
4775 that later insns are available for delay slot filling. */
7d10b47d 4776 end_noreorder ();
252b5132 4777
67c0d1eb 4778 macro_build (NULL, "break", "c", 6);
252b5132 4779 }
67c0d1eb 4780 macro_build (NULL, s, "d", dreg);
252b5132
RH
4781 break;
4782
4783 case M_DIV_3I:
4784 s = "div";
4785 s2 = "mflo";
4786 goto do_divi;
4787 case M_DIVU_3I:
4788 s = "divu";
4789 s2 = "mflo";
4790 goto do_divi;
4791 case M_REM_3I:
4792 s = "div";
4793 s2 = "mfhi";
4794 goto do_divi;
4795 case M_REMU_3I:
4796 s = "divu";
4797 s2 = "mfhi";
4798 goto do_divi;
4799 case M_DDIV_3I:
4800 dbl = 1;
4801 s = "ddiv";
4802 s2 = "mflo";
4803 goto do_divi;
4804 case M_DDIVU_3I:
4805 dbl = 1;
4806 s = "ddivu";
4807 s2 = "mflo";
4808 goto do_divi;
4809 case M_DREM_3I:
4810 dbl = 1;
4811 s = "ddiv";
4812 s2 = "mfhi";
4813 goto do_divi;
4814 case M_DREMU_3I:
4815 dbl = 1;
4816 s = "ddivu";
4817 s2 = "mfhi";
4818 do_divi:
4819 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4820 {
4821 as_warn (_("Divide by zero."));
4822 if (mips_trap)
67c0d1eb 4823 macro_build (NULL, "teq", "s,t,q", 0, 0, 7);
252b5132 4824 else
67c0d1eb 4825 macro_build (NULL, "break", "c", 7);
8fc2e39e 4826 break;
252b5132
RH
4827 }
4828 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4829 {
4830 if (strcmp (s2, "mflo") == 0)
67c0d1eb 4831 move_register (dreg, sreg);
252b5132 4832 else
67c0d1eb 4833 move_register (dreg, 0);
8fc2e39e 4834 break;
252b5132
RH
4835 }
4836 if (imm_expr.X_op == O_constant
4837 && imm_expr.X_add_number == -1
4838 && s[strlen (s) - 1] != 'u')
4839 {
4840 if (strcmp (s2, "mflo") == 0)
4841 {
67c0d1eb 4842 macro_build (NULL, dbl ? "dneg" : "neg", "d,w", dreg, sreg);
252b5132
RH
4843 }
4844 else
67c0d1eb 4845 move_register (dreg, 0);
8fc2e39e 4846 break;
252b5132
RH
4847 }
4848
8fc2e39e 4849 used_at = 1;
67c0d1eb
RS
4850 load_register (AT, &imm_expr, dbl);
4851 macro_build (NULL, s, "z,s,t", sreg, AT);
4852 macro_build (NULL, s2, "d", dreg);
252b5132
RH
4853 break;
4854
4855 case M_DIVU_3:
4856 s = "divu";
4857 s2 = "mflo";
4858 goto do_divu3;
4859 case M_REMU_3:
4860 s = "divu";
4861 s2 = "mfhi";
4862 goto do_divu3;
4863 case M_DDIVU_3:
4864 s = "ddivu";
4865 s2 = "mflo";
4866 goto do_divu3;
4867 case M_DREMU_3:
4868 s = "ddivu";
4869 s2 = "mfhi";
4870 do_divu3:
7d10b47d 4871 start_noreorder ();
252b5132
RH
4872 if (mips_trap)
4873 {
67c0d1eb
RS
4874 macro_build (NULL, "teq", "s,t,q", treg, 0, 7);
4875 macro_build (NULL, s, "z,s,t", sreg, treg);
252b5132
RH
4876 /* We want to close the noreorder block as soon as possible, so
4877 that later insns are available for delay slot filling. */
7d10b47d 4878 end_noreorder ();
252b5132
RH
4879 }
4880 else
4881 {
4882 expr1.X_add_number = 8;
67c0d1eb
RS
4883 macro_build (&expr1, "bne", "s,t,p", treg, 0);
4884 macro_build (NULL, s, "z,s,t", sreg, treg);
252b5132
RH
4885
4886 /* We want to close the noreorder block as soon as possible, so
4887 that later insns are available for delay slot filling. */
7d10b47d 4888 end_noreorder ();
67c0d1eb 4889 macro_build (NULL, "break", "c", 7);
252b5132 4890 }
67c0d1eb 4891 macro_build (NULL, s2, "d", dreg);
8fc2e39e 4892 break;
252b5132 4893
1abe91b1
MR
4894 case M_DLCA_AB:
4895 dbl = 1;
4896 case M_LCA_AB:
4897 call = 1;
4898 goto do_la;
252b5132
RH
4899 case M_DLA_AB:
4900 dbl = 1;
4901 case M_LA_AB:
1abe91b1 4902 do_la:
252b5132
RH
4903 /* Load the address of a symbol into a register. If breg is not
4904 zero, we then add a base register to it. */
4905
3bec30a8
TS
4906 if (dbl && HAVE_32BIT_GPRS)
4907 as_warn (_("dla used to load 32-bit register"));
4908
c90bbe5b 4909 if (! dbl && HAVE_64BIT_OBJECTS)
3bec30a8
TS
4910 as_warn (_("la used to load 64-bit address"));
4911
0c11417f
MR
4912 if (offset_expr.X_op == O_constant
4913 && offset_expr.X_add_number >= -0x8000
4914 && offset_expr.X_add_number < 0x8000)
4915 {
aed1a261 4916 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
17a2f251 4917 "t,r,j", treg, sreg, BFD_RELOC_LO16);
8fc2e39e 4918 break;
0c11417f
MR
4919 }
4920
b8285c27 4921 if (!mips_opts.noat && (treg == breg))
afdbd6d0
CD
4922 {
4923 tempreg = AT;
4924 used_at = 1;
4925 }
4926 else
4927 {
4928 tempreg = treg;
afdbd6d0
CD
4929 }
4930
252b5132
RH
4931 if (offset_expr.X_op != O_symbol
4932 && offset_expr.X_op != O_constant)
4933 {
4934 as_bad (_("expression too complex"));
4935 offset_expr.X_op = O_constant;
4936 }
4937
252b5132 4938 if (offset_expr.X_op == O_constant)
aed1a261 4939 load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
252b5132
RH
4940 else if (mips_pic == NO_PIC)
4941 {
d6bc6245 4942 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 4943 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
4944 Otherwise we want
4945 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
4946 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4947 If we have a constant, we need two instructions anyhow,
d6bc6245 4948 so we may as well always use the latter form.
76b3015f 4949
6caf9ef4
TS
4950 With 64bit address space and a usable $at we want
4951 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4952 lui $at,<sym> (BFD_RELOC_HI16_S)
4953 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4954 daddiu $at,<sym> (BFD_RELOC_LO16)
4955 dsll32 $tempreg,0
4956 daddu $tempreg,$tempreg,$at
4957
4958 If $at is already in use, we use a path which is suboptimal
4959 on superscalar processors.
4960 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4961 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4962 dsll $tempreg,16
4963 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
4964 dsll $tempreg,16
4965 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
4966
4967 For GP relative symbols in 64bit address space we can use
4968 the same sequence as in 32bit address space. */
aed1a261 4969 if (HAVE_64BIT_SYMBOLS)
252b5132 4970 {
6caf9ef4
TS
4971 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4972 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
4973 {
4974 relax_start (offset_expr.X_add_symbol);
4975 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
4976 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
4977 relax_switch ();
4978 }
d6bc6245 4979
b8285c27 4980 if (used_at == 0 && !mips_opts.noat)
98d3f06f 4981 {
67c0d1eb 4982 macro_build (&offset_expr, "lui", "t,u",
17a2f251 4983 tempreg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb 4984 macro_build (&offset_expr, "lui", "t,u",
17a2f251 4985 AT, BFD_RELOC_HI16_S);
67c0d1eb 4986 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 4987 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
67c0d1eb 4988 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 4989 AT, AT, BFD_RELOC_LO16);
67c0d1eb
RS
4990 macro_build (NULL, "dsll32", "d,w,<", tempreg, tempreg, 0);
4991 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
98d3f06f
KH
4992 used_at = 1;
4993 }
4994 else
4995 {
67c0d1eb 4996 macro_build (&offset_expr, "lui", "t,u",
17a2f251 4997 tempreg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb 4998 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 4999 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
67c0d1eb
RS
5000 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
5001 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 5002 tempreg, tempreg, BFD_RELOC_HI16_S);
67c0d1eb
RS
5003 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
5004 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 5005 tempreg, tempreg, BFD_RELOC_LO16);
98d3f06f 5006 }
6caf9ef4
TS
5007
5008 if (mips_relax.sequence)
5009 relax_end ();
98d3f06f
KH
5010 }
5011 else
5012 {
5013 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 5014 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
98d3f06f 5015 {
4d7206a2 5016 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
5017 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5018 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 5019 relax_switch ();
98d3f06f 5020 }
6943caf0
ILT
5021 if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
5022 as_bad (_("offset too large"));
67c0d1eb
RS
5023 macro_build_lui (&offset_expr, tempreg);
5024 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5025 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2
RS
5026 if (mips_relax.sequence)
5027 relax_end ();
98d3f06f 5028 }
252b5132 5029 }
0a44bf69 5030 else if (!mips_big_got && !HAVE_NEWABI)
252b5132 5031 {
9117d219
NC
5032 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5033
252b5132
RH
5034 /* If this is a reference to an external symbol, and there
5035 is no constant, we want
5036 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
1abe91b1 5037 or for lca or if tempreg is PIC_CALL_REG
9117d219 5038 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
252b5132
RH
5039 For a local symbol, we want
5040 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5041 nop
5042 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5043
5044 If we have a small constant, and this is a reference to
5045 an external symbol, we want
5046 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5047 nop
5048 addiu $tempreg,$tempreg,<constant>
5049 For a local symbol, we want the same instruction
5050 sequence, but we output a BFD_RELOC_LO16 reloc on the
5051 addiu instruction.
5052
5053 If we have a large constant, and this is a reference to
5054 an external symbol, we want
5055 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5056 lui $at,<hiconstant>
5057 addiu $at,$at,<loconstant>
5058 addu $tempreg,$tempreg,$at
5059 For a local symbol, we want the same instruction
5060 sequence, but we output a BFD_RELOC_LO16 reloc on the
ed6fb7bd 5061 addiu instruction.
ed6fb7bd
SC
5062 */
5063
4d7206a2 5064 if (offset_expr.X_add_number == 0)
252b5132 5065 {
0a44bf69
RS
5066 if (mips_pic == SVR4_PIC
5067 && breg == 0
5068 && (call || tempreg == PIC_CALL_REG))
4d7206a2
RS
5069 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
5070
5071 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
5072 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5073 lw_reloc_type, mips_gp_register);
4d7206a2 5074 if (breg != 0)
252b5132
RH
5075 {
5076 /* We're going to put in an addu instruction using
5077 tempreg, so we may as well insert the nop right
5078 now. */
269137b2 5079 load_delay_nop ();
252b5132 5080 }
4d7206a2 5081 relax_switch ();
67c0d1eb
RS
5082 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5083 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 5084 load_delay_nop ();
67c0d1eb
RS
5085 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5086 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2 5087 relax_end ();
252b5132
RH
5088 /* FIXME: If breg == 0, and the next instruction uses
5089 $tempreg, then if this variant case is used an extra
5090 nop will be generated. */
5091 }
4d7206a2
RS
5092 else if (offset_expr.X_add_number >= -0x8000
5093 && offset_expr.X_add_number < 0x8000)
252b5132 5094 {
67c0d1eb 5095 load_got_offset (tempreg, &offset_expr);
269137b2 5096 load_delay_nop ();
67c0d1eb 5097 add_got_offset (tempreg, &offset_expr);
252b5132
RH
5098 }
5099 else
5100 {
4d7206a2
RS
5101 expr1.X_add_number = offset_expr.X_add_number;
5102 offset_expr.X_add_number =
5103 ((offset_expr.X_add_number + 0x8000) & 0xffff) - 0x8000;
67c0d1eb 5104 load_got_offset (tempreg, &offset_expr);
f6a22291 5105 offset_expr.X_add_number = expr1.X_add_number;
252b5132
RH
5106 /* If we are going to add in a base register, and the
5107 target register and the base register are the same,
5108 then we are using AT as a temporary register. Since
5109 we want to load the constant into AT, we add our
5110 current AT (from the global offset table) and the
5111 register into the register now, and pretend we were
5112 not using a base register. */
67c0d1eb 5113 if (breg == treg)
252b5132 5114 {
269137b2 5115 load_delay_nop ();
67c0d1eb 5116 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5117 treg, AT, breg);
252b5132
RH
5118 breg = 0;
5119 tempreg = treg;
252b5132 5120 }
f6a22291 5121 add_got_offset_hilo (tempreg, &offset_expr, AT);
252b5132
RH
5122 used_at = 1;
5123 }
5124 }
0a44bf69 5125 else if (!mips_big_got && HAVE_NEWABI)
f5040a92 5126 {
67c0d1eb 5127 int add_breg_early = 0;
f5040a92
AO
5128
5129 /* If this is a reference to an external, and there is no
5130 constant, or local symbol (*), with or without a
5131 constant, we want
5132 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
1abe91b1 5133 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
5134 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5135
5136 If we have a small constant, and this is a reference to
5137 an external symbol, we want
5138 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5139 addiu $tempreg,$tempreg,<constant>
5140
5141 If we have a large constant, and this is a reference to
5142 an external symbol, we want
5143 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5144 lui $at,<hiconstant>
5145 addiu $at,$at,<loconstant>
5146 addu $tempreg,$tempreg,$at
5147
5148 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
5149 local symbols, even though it introduces an additional
5150 instruction. */
5151
f5040a92
AO
5152 if (offset_expr.X_add_number)
5153 {
4d7206a2 5154 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
5155 offset_expr.X_add_number = 0;
5156
4d7206a2 5157 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
5158 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5159 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
5160
5161 if (expr1.X_add_number >= -0x8000
5162 && expr1.X_add_number < 0x8000)
5163 {
67c0d1eb
RS
5164 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
5165 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 5166 }
ecd13cd3 5167 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92
AO
5168 {
5169 int dreg;
5170
5171 /* If we are going to add in a base register, and the
5172 target register and the base register are the same,
5173 then we are using AT as a temporary register. Since
5174 we want to load the constant into AT, we add our
5175 current AT (from the global offset table) and the
5176 register into the register now, and pretend we were
5177 not using a base register. */
5178 if (breg != treg)
5179 dreg = tempreg;
5180 else
5181 {
5182 assert (tempreg == AT);
67c0d1eb
RS
5183 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5184 treg, AT, breg);
f5040a92 5185 dreg = treg;
67c0d1eb 5186 add_breg_early = 1;
f5040a92
AO
5187 }
5188
f6a22291 5189 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 5190 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5191 dreg, dreg, AT);
f5040a92 5192
f5040a92
AO
5193 used_at = 1;
5194 }
5195 else
5196 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5197
4d7206a2 5198 relax_switch ();
f5040a92
AO
5199 offset_expr.X_add_number = expr1.X_add_number;
5200
67c0d1eb
RS
5201 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5202 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5203 if (add_breg_early)
f5040a92 5204 {
67c0d1eb 5205 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
f899b4b8 5206 treg, tempreg, breg);
f5040a92
AO
5207 breg = 0;
5208 tempreg = treg;
5209 }
4d7206a2 5210 relax_end ();
f5040a92 5211 }
4d7206a2 5212 else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
f5040a92 5213 {
4d7206a2 5214 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
5215 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5216 BFD_RELOC_MIPS_CALL16, mips_gp_register);
4d7206a2 5217 relax_switch ();
67c0d1eb
RS
5218 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5219 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2 5220 relax_end ();
f5040a92 5221 }
4d7206a2 5222 else
f5040a92 5223 {
67c0d1eb
RS
5224 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5225 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
5226 }
5227 }
0a44bf69 5228 else if (mips_big_got && !HAVE_NEWABI)
252b5132 5229 {
67c0d1eb 5230 int gpdelay;
9117d219
NC
5231 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5232 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
ed6fb7bd 5233 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
252b5132
RH
5234
5235 /* This is the large GOT case. If this is a reference to an
5236 external symbol, and there is no constant, we want
5237 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5238 addu $tempreg,$tempreg,$gp
5239 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 5240 or for lca or if tempreg is PIC_CALL_REG
9117d219
NC
5241 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5242 addu $tempreg,$tempreg,$gp
5243 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
252b5132
RH
5244 For a local symbol, we want
5245 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5246 nop
5247 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5248
5249 If we have a small constant, and this is a reference to
5250 an external symbol, we want
5251 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5252 addu $tempreg,$tempreg,$gp
5253 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5254 nop
5255 addiu $tempreg,$tempreg,<constant>
5256 For a local symbol, we want
5257 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5258 nop
5259 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
5260
5261 If we have a large constant, and this is a reference to
5262 an external symbol, we want
5263 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5264 addu $tempreg,$tempreg,$gp
5265 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5266 lui $at,<hiconstant>
5267 addiu $at,$at,<loconstant>
5268 addu $tempreg,$tempreg,$at
5269 For a local symbol, we want
5270 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5271 lui $at,<hiconstant>
5272 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
5273 addu $tempreg,$tempreg,$at
f5040a92 5274 */
438c16b8 5275
252b5132
RH
5276 expr1.X_add_number = offset_expr.X_add_number;
5277 offset_expr.X_add_number = 0;
4d7206a2 5278 relax_start (offset_expr.X_add_symbol);
67c0d1eb 5279 gpdelay = reg_needs_delay (mips_gp_register);
1abe91b1
MR
5280 if (expr1.X_add_number == 0 && breg == 0
5281 && (call || tempreg == PIC_CALL_REG))
9117d219
NC
5282 {
5283 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5284 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5285 }
67c0d1eb
RS
5286 macro_build (&offset_expr, "lui", "t,u", tempreg, lui_reloc_type);
5287 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5288 tempreg, tempreg, mips_gp_register);
67c0d1eb 5289 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 5290 tempreg, lw_reloc_type, tempreg);
252b5132
RH
5291 if (expr1.X_add_number == 0)
5292 {
67c0d1eb 5293 if (breg != 0)
252b5132
RH
5294 {
5295 /* We're going to put in an addu instruction using
5296 tempreg, so we may as well insert the nop right
5297 now. */
269137b2 5298 load_delay_nop ();
252b5132 5299 }
252b5132
RH
5300 }
5301 else if (expr1.X_add_number >= -0x8000
5302 && expr1.X_add_number < 0x8000)
5303 {
269137b2 5304 load_delay_nop ();
67c0d1eb 5305 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 5306 tempreg, tempreg, BFD_RELOC_LO16);
252b5132
RH
5307 }
5308 else
5309 {
67c0d1eb 5310 int dreg;
252b5132
RH
5311
5312 /* If we are going to add in a base register, and the
5313 target register and the base register are the same,
5314 then we are using AT as a temporary register. Since
5315 we want to load the constant into AT, we add our
5316 current AT (from the global offset table) and the
5317 register into the register now, and pretend we were
5318 not using a base register. */
5319 if (breg != treg)
67c0d1eb 5320 dreg = tempreg;
252b5132
RH
5321 else
5322 {
5323 assert (tempreg == AT);
269137b2 5324 load_delay_nop ();
67c0d1eb 5325 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5326 treg, AT, breg);
252b5132 5327 dreg = treg;
252b5132
RH
5328 }
5329
f6a22291 5330 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 5331 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
252b5132 5332
252b5132
RH
5333 used_at = 1;
5334 }
4d7206a2
RS
5335 offset_expr.X_add_number =
5336 ((expr1.X_add_number + 0x8000) & 0xffff) - 0x8000;
5337 relax_switch ();
252b5132 5338
67c0d1eb 5339 if (gpdelay)
252b5132
RH
5340 {
5341 /* This is needed because this instruction uses $gp, but
f5040a92 5342 the first instruction on the main stream does not. */
67c0d1eb 5343 macro_build (NULL, "nop", "");
252b5132 5344 }
ed6fb7bd 5345
67c0d1eb
RS
5346 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5347 local_reloc_type, mips_gp_register);
f5040a92 5348 if (expr1.X_add_number >= -0x8000
252b5132
RH
5349 && expr1.X_add_number < 0x8000)
5350 {
269137b2 5351 load_delay_nop ();
67c0d1eb
RS
5352 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5353 tempreg, tempreg, BFD_RELOC_LO16);
252b5132 5354 /* FIXME: If add_number is 0, and there was no base
f5040a92
AO
5355 register, the external symbol case ended with a load,
5356 so if the symbol turns out to not be external, and
5357 the next instruction uses tempreg, an unnecessary nop
5358 will be inserted. */
252b5132
RH
5359 }
5360 else
5361 {
5362 if (breg == treg)
5363 {
5364 /* We must add in the base register now, as in the
f5040a92 5365 external symbol case. */
252b5132 5366 assert (tempreg == AT);
269137b2 5367 load_delay_nop ();
67c0d1eb 5368 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5369 treg, AT, breg);
252b5132
RH
5370 tempreg = treg;
5371 /* We set breg to 0 because we have arranged to add
f5040a92 5372 it in in both cases. */
252b5132
RH
5373 breg = 0;
5374 }
5375
67c0d1eb
RS
5376 macro_build_lui (&expr1, AT);
5377 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 5378 AT, AT, BFD_RELOC_LO16);
67c0d1eb 5379 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5380 tempreg, tempreg, AT);
8fc2e39e 5381 used_at = 1;
252b5132 5382 }
4d7206a2 5383 relax_end ();
252b5132 5384 }
0a44bf69 5385 else if (mips_big_got && HAVE_NEWABI)
f5040a92 5386 {
f5040a92
AO
5387 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5388 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
67c0d1eb 5389 int add_breg_early = 0;
f5040a92
AO
5390
5391 /* This is the large GOT case. If this is a reference to an
5392 external symbol, and there is no constant, we want
5393 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5394 add $tempreg,$tempreg,$gp
5395 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 5396 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
5397 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5398 add $tempreg,$tempreg,$gp
5399 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5400
5401 If we have a small constant, and this is a reference to
5402 an external symbol, we want
5403 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5404 add $tempreg,$tempreg,$gp
5405 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5406 addi $tempreg,$tempreg,<constant>
5407
5408 If we have a large constant, and this is a reference to
5409 an external symbol, we want
5410 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5411 addu $tempreg,$tempreg,$gp
5412 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5413 lui $at,<hiconstant>
5414 addi $at,$at,<loconstant>
5415 add $tempreg,$tempreg,$at
5416
5417 If we have NewABI, and we know it's a local symbol, we want
5418 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
5419 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
5420 otherwise we have to resort to GOT_HI16/GOT_LO16. */
5421
4d7206a2 5422 relax_start (offset_expr.X_add_symbol);
f5040a92 5423
4d7206a2 5424 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
5425 offset_expr.X_add_number = 0;
5426
1abe91b1
MR
5427 if (expr1.X_add_number == 0 && breg == 0
5428 && (call || tempreg == PIC_CALL_REG))
f5040a92
AO
5429 {
5430 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5431 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5432 }
67c0d1eb
RS
5433 macro_build (&offset_expr, "lui", "t,u", tempreg, lui_reloc_type);
5434 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5435 tempreg, tempreg, mips_gp_register);
67c0d1eb
RS
5436 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5437 tempreg, lw_reloc_type, tempreg);
f5040a92
AO
5438
5439 if (expr1.X_add_number == 0)
4d7206a2 5440 ;
f5040a92
AO
5441 else if (expr1.X_add_number >= -0x8000
5442 && expr1.X_add_number < 0x8000)
5443 {
67c0d1eb 5444 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 5445 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 5446 }
ecd13cd3 5447 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92
AO
5448 {
5449 int dreg;
5450
5451 /* If we are going to add in a base register, and the
5452 target register and the base register are the same,
5453 then we are using AT as a temporary register. Since
5454 we want to load the constant into AT, we add our
5455 current AT (from the global offset table) and the
5456 register into the register now, and pretend we were
5457 not using a base register. */
5458 if (breg != treg)
5459 dreg = tempreg;
5460 else
5461 {
5462 assert (tempreg == AT);
67c0d1eb 5463 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5464 treg, AT, breg);
f5040a92 5465 dreg = treg;
67c0d1eb 5466 add_breg_early = 1;
f5040a92
AO
5467 }
5468
f6a22291 5469 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 5470 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
f5040a92 5471
f5040a92
AO
5472 used_at = 1;
5473 }
5474 else
5475 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5476
4d7206a2 5477 relax_switch ();
f5040a92 5478 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
5479 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5480 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
5481 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
5482 tempreg, BFD_RELOC_MIPS_GOT_OFST);
5483 if (add_breg_early)
f5040a92 5484 {
67c0d1eb 5485 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5486 treg, tempreg, breg);
f5040a92
AO
5487 breg = 0;
5488 tempreg = treg;
5489 }
4d7206a2 5490 relax_end ();
f5040a92 5491 }
252b5132
RH
5492 else
5493 abort ();
5494
5495 if (breg != 0)
aed1a261 5496 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", treg, tempreg, breg);
252b5132
RH
5497 break;
5498
5499 case M_J_A:
5500 /* The j instruction may not be used in PIC code, since it
5501 requires an absolute address. We convert it to a b
5502 instruction. */
5503 if (mips_pic == NO_PIC)
67c0d1eb 5504 macro_build (&offset_expr, "j", "a");
252b5132 5505 else
67c0d1eb 5506 macro_build (&offset_expr, "b", "p");
8fc2e39e 5507 break;
252b5132
RH
5508
5509 /* The jal instructions must be handled as macros because when
5510 generating PIC code they expand to multi-instruction
5511 sequences. Normally they are simple instructions. */
5512 case M_JAL_1:
5513 dreg = RA;
5514 /* Fall through. */
5515 case M_JAL_2:
3e722fb5 5516 if (mips_pic == NO_PIC)
67c0d1eb 5517 macro_build (NULL, "jalr", "d,s", dreg, sreg);
0a44bf69 5518 else
252b5132
RH
5519 {
5520 if (sreg != PIC_CALL_REG)
5521 as_warn (_("MIPS PIC call to register other than $25"));
bdaaa2e1 5522
67c0d1eb 5523 macro_build (NULL, "jalr", "d,s", dreg, sreg);
0a44bf69 5524 if (mips_pic == SVR4_PIC && !HAVE_NEWABI)
252b5132 5525 {
6478892d
TS
5526 if (mips_cprestore_offset < 0)
5527 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5528 else
5529 {
7a621144
DJ
5530 if (! mips_frame_reg_valid)
5531 {
5532 as_warn (_("No .frame pseudo-op used in PIC code"));
5533 /* Quiet this warning. */
5534 mips_frame_reg_valid = 1;
5535 }
5536 if (! mips_cprestore_valid)
5537 {
5538 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5539 /* Quiet this warning. */
5540 mips_cprestore_valid = 1;
5541 }
6478892d 5542 expr1.X_add_number = mips_cprestore_offset;
67c0d1eb 5543 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 5544 mips_gp_register,
256ab948
TS
5545 mips_frame_reg,
5546 HAVE_64BIT_ADDRESSES);
6478892d 5547 }
252b5132
RH
5548 }
5549 }
252b5132 5550
8fc2e39e 5551 break;
252b5132
RH
5552
5553 case M_JAL_A:
5554 if (mips_pic == NO_PIC)
67c0d1eb 5555 macro_build (&offset_expr, "jal", "a");
252b5132
RH
5556 else if (mips_pic == SVR4_PIC)
5557 {
5558 /* If this is a reference to an external symbol, and we are
5559 using a small GOT, we want
5560 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5561 nop
f9419b05 5562 jalr $ra,$25
252b5132
RH
5563 nop
5564 lw $gp,cprestore($sp)
5565 The cprestore value is set using the .cprestore
5566 pseudo-op. If we are using a big GOT, we want
5567 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5568 addu $25,$25,$gp
5569 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
5570 nop
f9419b05 5571 jalr $ra,$25
252b5132
RH
5572 nop
5573 lw $gp,cprestore($sp)
5574 If the symbol is not external, we want
5575 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5576 nop
5577 addiu $25,$25,<sym> (BFD_RELOC_LO16)
f9419b05 5578 jalr $ra,$25
252b5132 5579 nop
438c16b8 5580 lw $gp,cprestore($sp)
f5040a92
AO
5581
5582 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
5583 sequences above, minus nops, unless the symbol is local,
5584 which enables us to use GOT_PAGE/GOT_OFST (big got) or
5585 GOT_DISP. */
438c16b8 5586 if (HAVE_NEWABI)
252b5132 5587 {
f5040a92
AO
5588 if (! mips_big_got)
5589 {
4d7206a2 5590 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
5591 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5592 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
f5040a92 5593 mips_gp_register);
4d7206a2 5594 relax_switch ();
67c0d1eb
RS
5595 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5596 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
4d7206a2
RS
5597 mips_gp_register);
5598 relax_end ();
f5040a92
AO
5599 }
5600 else
5601 {
4d7206a2 5602 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
5603 macro_build (&offset_expr, "lui", "t,u", PIC_CALL_REG,
5604 BFD_RELOC_MIPS_CALL_HI16);
5605 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
5606 PIC_CALL_REG, mips_gp_register);
5607 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5608 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
5609 PIC_CALL_REG);
4d7206a2 5610 relax_switch ();
67c0d1eb
RS
5611 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5612 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
5613 mips_gp_register);
5614 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5615 PIC_CALL_REG, PIC_CALL_REG,
17a2f251 5616 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 5617 relax_end ();
f5040a92 5618 }
684022ea 5619
67c0d1eb 5620 macro_build_jalr (&offset_expr);
252b5132
RH
5621 }
5622 else
5623 {
4d7206a2 5624 relax_start (offset_expr.X_add_symbol);
438c16b8
TS
5625 if (! mips_big_got)
5626 {
67c0d1eb
RS
5627 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5628 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
17a2f251 5629 mips_gp_register);
269137b2 5630 load_delay_nop ();
4d7206a2 5631 relax_switch ();
438c16b8 5632 }
252b5132 5633 else
252b5132 5634 {
67c0d1eb
RS
5635 int gpdelay;
5636
5637 gpdelay = reg_needs_delay (mips_gp_register);
5638 macro_build (&offset_expr, "lui", "t,u", PIC_CALL_REG,
5639 BFD_RELOC_MIPS_CALL_HI16);
5640 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
5641 PIC_CALL_REG, mips_gp_register);
5642 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5643 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
5644 PIC_CALL_REG);
269137b2 5645 load_delay_nop ();
4d7206a2 5646 relax_switch ();
67c0d1eb
RS
5647 if (gpdelay)
5648 macro_build (NULL, "nop", "");
252b5132 5649 }
67c0d1eb
RS
5650 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5651 PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
4d7206a2 5652 mips_gp_register);
269137b2 5653 load_delay_nop ();
67c0d1eb
RS
5654 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5655 PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
4d7206a2 5656 relax_end ();
67c0d1eb 5657 macro_build_jalr (&offset_expr);
438c16b8 5658
6478892d
TS
5659 if (mips_cprestore_offset < 0)
5660 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5661 else
5662 {
7a621144
DJ
5663 if (! mips_frame_reg_valid)
5664 {
5665 as_warn (_("No .frame pseudo-op used in PIC code"));
5666 /* Quiet this warning. */
5667 mips_frame_reg_valid = 1;
5668 }
5669 if (! mips_cprestore_valid)
5670 {
5671 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5672 /* Quiet this warning. */
5673 mips_cprestore_valid = 1;
5674 }
6478892d 5675 if (mips_opts.noreorder)
67c0d1eb 5676 macro_build (NULL, "nop", "");
6478892d 5677 expr1.X_add_number = mips_cprestore_offset;
67c0d1eb 5678 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 5679 mips_gp_register,
256ab948
TS
5680 mips_frame_reg,
5681 HAVE_64BIT_ADDRESSES);
6478892d 5682 }
252b5132
RH
5683 }
5684 }
0a44bf69
RS
5685 else if (mips_pic == VXWORKS_PIC)
5686 as_bad (_("Non-PIC jump used in PIC library"));
252b5132
RH
5687 else
5688 abort ();
5689
8fc2e39e 5690 break;
252b5132
RH
5691
5692 case M_LB_AB:
5693 s = "lb";
5694 goto ld;
5695 case M_LBU_AB:
5696 s = "lbu";
5697 goto ld;
5698 case M_LH_AB:
5699 s = "lh";
5700 goto ld;
5701 case M_LHU_AB:
5702 s = "lhu";
5703 goto ld;
5704 case M_LW_AB:
5705 s = "lw";
5706 goto ld;
5707 case M_LWC0_AB:
5708 s = "lwc0";
bdaaa2e1 5709 /* Itbl support may require additional care here. */
252b5132
RH
5710 coproc = 1;
5711 goto ld;
5712 case M_LWC1_AB:
5713 s = "lwc1";
bdaaa2e1 5714 /* Itbl support may require additional care here. */
252b5132
RH
5715 coproc = 1;
5716 goto ld;
5717 case M_LWC2_AB:
5718 s = "lwc2";
bdaaa2e1 5719 /* Itbl support may require additional care here. */
252b5132
RH
5720 coproc = 1;
5721 goto ld;
5722 case M_LWC3_AB:
5723 s = "lwc3";
bdaaa2e1 5724 /* Itbl support may require additional care here. */
252b5132
RH
5725 coproc = 1;
5726 goto ld;
5727 case M_LWL_AB:
5728 s = "lwl";
5729 lr = 1;
5730 goto ld;
5731 case M_LWR_AB:
5732 s = "lwr";
5733 lr = 1;
5734 goto ld;
5735 case M_LDC1_AB:
fef14a42 5736 if (mips_opts.arch == CPU_R4650)
252b5132
RH
5737 {
5738 as_bad (_("opcode not supported on this processor"));
8fc2e39e 5739 break;
252b5132
RH
5740 }
5741 s = "ldc1";
bdaaa2e1 5742 /* Itbl support may require additional care here. */
252b5132
RH
5743 coproc = 1;
5744 goto ld;
5745 case M_LDC2_AB:
5746 s = "ldc2";
bdaaa2e1 5747 /* Itbl support may require additional care here. */
252b5132
RH
5748 coproc = 1;
5749 goto ld;
5750 case M_LDC3_AB:
5751 s = "ldc3";
bdaaa2e1 5752 /* Itbl support may require additional care here. */
252b5132
RH
5753 coproc = 1;
5754 goto ld;
5755 case M_LDL_AB:
5756 s = "ldl";
5757 lr = 1;
5758 goto ld;
5759 case M_LDR_AB:
5760 s = "ldr";
5761 lr = 1;
5762 goto ld;
5763 case M_LL_AB:
5764 s = "ll";
5765 goto ld;
5766 case M_LLD_AB:
5767 s = "lld";
5768 goto ld;
5769 case M_LWU_AB:
5770 s = "lwu";
5771 ld:
8fc2e39e 5772 if (breg == treg || coproc || lr)
252b5132
RH
5773 {
5774 tempreg = AT;
5775 used_at = 1;
5776 }
5777 else
5778 {
5779 tempreg = treg;
252b5132
RH
5780 }
5781 goto ld_st;
5782 case M_SB_AB:
5783 s = "sb";
5784 goto st;
5785 case M_SH_AB:
5786 s = "sh";
5787 goto st;
5788 case M_SW_AB:
5789 s = "sw";
5790 goto st;
5791 case M_SWC0_AB:
5792 s = "swc0";
bdaaa2e1 5793 /* Itbl support may require additional care here. */
252b5132
RH
5794 coproc = 1;
5795 goto st;
5796 case M_SWC1_AB:
5797 s = "swc1";
bdaaa2e1 5798 /* Itbl support may require additional care here. */
252b5132
RH
5799 coproc = 1;
5800 goto st;
5801 case M_SWC2_AB:
5802 s = "swc2";
bdaaa2e1 5803 /* Itbl support may require additional care here. */
252b5132
RH
5804 coproc = 1;
5805 goto st;
5806 case M_SWC3_AB:
5807 s = "swc3";
bdaaa2e1 5808 /* Itbl support may require additional care here. */
252b5132
RH
5809 coproc = 1;
5810 goto st;
5811 case M_SWL_AB:
5812 s = "swl";
5813 goto st;
5814 case M_SWR_AB:
5815 s = "swr";
5816 goto st;
5817 case M_SC_AB:
5818 s = "sc";
5819 goto st;
5820 case M_SCD_AB:
5821 s = "scd";
5822 goto st;
5823 case M_SDC1_AB:
fef14a42 5824 if (mips_opts.arch == CPU_R4650)
252b5132
RH
5825 {
5826 as_bad (_("opcode not supported on this processor"));
8fc2e39e 5827 break;
252b5132
RH
5828 }
5829 s = "sdc1";
5830 coproc = 1;
bdaaa2e1 5831 /* Itbl support may require additional care here. */
252b5132
RH
5832 goto st;
5833 case M_SDC2_AB:
5834 s = "sdc2";
bdaaa2e1 5835 /* Itbl support may require additional care here. */
252b5132
RH
5836 coproc = 1;
5837 goto st;
5838 case M_SDC3_AB:
5839 s = "sdc3";
bdaaa2e1 5840 /* Itbl support may require additional care here. */
252b5132
RH
5841 coproc = 1;
5842 goto st;
5843 case M_SDL_AB:
5844 s = "sdl";
5845 goto st;
5846 case M_SDR_AB:
5847 s = "sdr";
5848 st:
8fc2e39e
TS
5849 tempreg = AT;
5850 used_at = 1;
252b5132 5851 ld_st:
bdaaa2e1 5852 /* Itbl support may require additional care here. */
252b5132
RH
5853 if (mask == M_LWC1_AB
5854 || mask == M_SWC1_AB
5855 || mask == M_LDC1_AB
5856 || mask == M_SDC1_AB
5857 || mask == M_L_DAB
5858 || mask == M_S_DAB)
5859 fmt = "T,o(b)";
5860 else if (coproc)
5861 fmt = "E,o(b)";
5862 else
5863 fmt = "t,o(b)";
5864
5865 if (offset_expr.X_op != O_constant
5866 && offset_expr.X_op != O_symbol)
5867 {
5868 as_bad (_("expression too complex"));
5869 offset_expr.X_op = O_constant;
5870 }
5871
2051e8c4
MR
5872 if (HAVE_32BIT_ADDRESSES
5873 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
5874 {
5875 char value [32];
5876
5877 sprintf_vma (value, offset_expr.X_add_number);
20e1fcfd 5878 as_bad (_("Number (0x%s) larger than 32 bits"), value);
55e08f71 5879 }
2051e8c4 5880
252b5132
RH
5881 /* A constant expression in PIC code can be handled just as it
5882 is in non PIC code. */
aed1a261
RS
5883 if (offset_expr.X_op == O_constant)
5884 {
aed1a261
RS
5885 expr1.X_add_number = ((offset_expr.X_add_number + 0x8000)
5886 & ~(bfd_vma) 0xffff);
2051e8c4 5887 normalize_address_expr (&expr1);
aed1a261
RS
5888 load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
5889 if (breg != 0)
5890 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5891 tempreg, tempreg, breg);
5892 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16, tempreg);
5893 }
5894 else if (mips_pic == NO_PIC)
252b5132
RH
5895 {
5896 /* If this is a reference to a GP relative symbol, and there
5897 is no base register, we want
cdf6fd85 5898 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
5899 Otherwise, if there is no base register, we want
5900 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5901 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5902 If we have a constant, we need two instructions anyhow,
5903 so we always use the latter form.
5904
5905 If we have a base register, and this is a reference to a
5906 GP relative symbol, we want
5907 addu $tempreg,$breg,$gp
cdf6fd85 5908 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
5909 Otherwise we want
5910 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5911 addu $tempreg,$tempreg,$breg
5912 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245 5913 With a constant we always use the latter case.
76b3015f 5914
d6bc6245
TS
5915 With 64bit address space and no base register and $at usable,
5916 we want
5917 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5918 lui $at,<sym> (BFD_RELOC_HI16_S)
5919 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5920 dsll32 $tempreg,0
5921 daddu $tempreg,$at
5922 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5923 If we have a base register, we want
5924 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5925 lui $at,<sym> (BFD_RELOC_HI16_S)
5926 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5927 daddu $at,$breg
5928 dsll32 $tempreg,0
5929 daddu $tempreg,$at
5930 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5931
5932 Without $at we can't generate the optimal path for superscalar
5933 processors here since this would require two temporary registers.
5934 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5935 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5936 dsll $tempreg,16
5937 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5938 dsll $tempreg,16
5939 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5940 If we have a base register, we want
5941 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5942 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5943 dsll $tempreg,16
5944 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5945 dsll $tempreg,16
5946 daddu $tempreg,$tempreg,$breg
5947 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6373ee54 5948
6caf9ef4 5949 For GP relative symbols in 64bit address space we can use
aed1a261
RS
5950 the same sequence as in 32bit address space. */
5951 if (HAVE_64BIT_SYMBOLS)
d6bc6245 5952 {
aed1a261 5953 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4
TS
5954 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
5955 {
5956 relax_start (offset_expr.X_add_symbol);
5957 if (breg == 0)
5958 {
5959 macro_build (&offset_expr, s, fmt, treg,
5960 BFD_RELOC_GPREL16, mips_gp_register);
5961 }
5962 else
5963 {
5964 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5965 tempreg, breg, mips_gp_register);
5966 macro_build (&offset_expr, s, fmt, treg,
5967 BFD_RELOC_GPREL16, tempreg);
5968 }
5969 relax_switch ();
5970 }
d6bc6245 5971
b8285c27 5972 if (used_at == 0 && !mips_opts.noat)
d6bc6245 5973 {
67c0d1eb
RS
5974 macro_build (&offset_expr, "lui", "t,u", tempreg,
5975 BFD_RELOC_MIPS_HIGHEST);
5976 macro_build (&offset_expr, "lui", "t,u", AT,
5977 BFD_RELOC_HI16_S);
5978 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
5979 tempreg, BFD_RELOC_MIPS_HIGHER);
d6bc6245 5980 if (breg != 0)
67c0d1eb
RS
5981 macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
5982 macro_build (NULL, "dsll32", "d,w,<", tempreg, tempreg, 0);
5983 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
5984 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16,
5985 tempreg);
d6bc6245
TS
5986 used_at = 1;
5987 }
5988 else
5989 {
67c0d1eb
RS
5990 macro_build (&offset_expr, "lui", "t,u", tempreg,
5991 BFD_RELOC_MIPS_HIGHEST);
5992 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
5993 tempreg, BFD_RELOC_MIPS_HIGHER);
5994 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
5995 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
5996 tempreg, BFD_RELOC_HI16_S);
5997 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
d6bc6245 5998 if (breg != 0)
67c0d1eb 5999 macro_build (NULL, "daddu", "d,v,t",
17a2f251 6000 tempreg, tempreg, breg);
67c0d1eb 6001 macro_build (&offset_expr, s, fmt, treg,
17a2f251 6002 BFD_RELOC_LO16, tempreg);
d6bc6245 6003 }
6caf9ef4
TS
6004
6005 if (mips_relax.sequence)
6006 relax_end ();
8fc2e39e 6007 break;
d6bc6245 6008 }
256ab948 6009
252b5132
RH
6010 if (breg == 0)
6011 {
67c0d1eb 6012 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 6013 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 6014 {
4d7206a2 6015 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
6016 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_GPREL16,
6017 mips_gp_register);
4d7206a2 6018 relax_switch ();
252b5132 6019 }
67c0d1eb
RS
6020 macro_build_lui (&offset_expr, tempreg);
6021 macro_build (&offset_expr, s, fmt, treg,
17a2f251 6022 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
6023 if (mips_relax.sequence)
6024 relax_end ();
252b5132
RH
6025 }
6026 else
6027 {
67c0d1eb 6028 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 6029 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 6030 {
4d7206a2 6031 relax_start (offset_expr.X_add_symbol);
67c0d1eb 6032 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6033 tempreg, breg, mips_gp_register);
67c0d1eb 6034 macro_build (&offset_expr, s, fmt, treg,
17a2f251 6035 BFD_RELOC_GPREL16, tempreg);
4d7206a2 6036 relax_switch ();
252b5132 6037 }
67c0d1eb
RS
6038 macro_build_lui (&offset_expr, tempreg);
6039 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6040 tempreg, tempreg, breg);
67c0d1eb 6041 macro_build (&offset_expr, s, fmt, treg,
17a2f251 6042 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
6043 if (mips_relax.sequence)
6044 relax_end ();
252b5132
RH
6045 }
6046 }
0a44bf69 6047 else if (!mips_big_got)
252b5132 6048 {
ed6fb7bd 6049 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
f9419b05 6050
252b5132
RH
6051 /* If this is a reference to an external symbol, we want
6052 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6053 nop
6054 <op> $treg,0($tempreg)
6055 Otherwise we want
6056 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6057 nop
6058 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6059 <op> $treg,0($tempreg)
f5040a92
AO
6060
6061 For NewABI, we want
6062 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6063 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
6064
252b5132
RH
6065 If there is a base register, we add it to $tempreg before
6066 the <op>. If there is a constant, we stick it in the
6067 <op> instruction. We don't handle constants larger than
6068 16 bits, because we have no way to load the upper 16 bits
6069 (actually, we could handle them for the subset of cases
6070 in which we are not using $at). */
6071 assert (offset_expr.X_op == O_symbol);
f5040a92
AO
6072 if (HAVE_NEWABI)
6073 {
67c0d1eb
RS
6074 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6075 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 6076 if (breg != 0)
67c0d1eb 6077 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6078 tempreg, tempreg, breg);
67c0d1eb 6079 macro_build (&offset_expr, s, fmt, treg,
17a2f251 6080 BFD_RELOC_MIPS_GOT_OFST, tempreg);
f5040a92
AO
6081 break;
6082 }
252b5132
RH
6083 expr1.X_add_number = offset_expr.X_add_number;
6084 offset_expr.X_add_number = 0;
6085 if (expr1.X_add_number < -0x8000
6086 || expr1.X_add_number >= 0x8000)
6087 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb
RS
6088 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6089 lw_reloc_type, mips_gp_register);
269137b2 6090 load_delay_nop ();
4d7206a2
RS
6091 relax_start (offset_expr.X_add_symbol);
6092 relax_switch ();
67c0d1eb
RS
6093 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
6094 tempreg, BFD_RELOC_LO16);
4d7206a2 6095 relax_end ();
252b5132 6096 if (breg != 0)
67c0d1eb 6097 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6098 tempreg, tempreg, breg);
67c0d1eb 6099 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
252b5132 6100 }
0a44bf69 6101 else if (mips_big_got && !HAVE_NEWABI)
252b5132 6102 {
67c0d1eb 6103 int gpdelay;
252b5132
RH
6104
6105 /* If this is a reference to an external symbol, we want
6106 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6107 addu $tempreg,$tempreg,$gp
6108 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6109 <op> $treg,0($tempreg)
6110 Otherwise we want
6111 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6112 nop
6113 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6114 <op> $treg,0($tempreg)
6115 If there is a base register, we add it to $tempreg before
6116 the <op>. If there is a constant, we stick it in the
6117 <op> instruction. We don't handle constants larger than
6118 16 bits, because we have no way to load the upper 16 bits
6119 (actually, we could handle them for the subset of cases
f5040a92 6120 in which we are not using $at). */
252b5132
RH
6121 assert (offset_expr.X_op == O_symbol);
6122 expr1.X_add_number = offset_expr.X_add_number;
6123 offset_expr.X_add_number = 0;
6124 if (expr1.X_add_number < -0x8000
6125 || expr1.X_add_number >= 0x8000)
6126 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 6127 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 6128 relax_start (offset_expr.X_add_symbol);
67c0d1eb 6129 macro_build (&offset_expr, "lui", "t,u", tempreg,
17a2f251 6130 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
6131 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
6132 mips_gp_register);
6133 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6134 BFD_RELOC_MIPS_GOT_LO16, tempreg);
4d7206a2 6135 relax_switch ();
67c0d1eb
RS
6136 if (gpdelay)
6137 macro_build (NULL, "nop", "");
6138 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6139 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 6140 load_delay_nop ();
67c0d1eb
RS
6141 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
6142 tempreg, BFD_RELOC_LO16);
4d7206a2
RS
6143 relax_end ();
6144
252b5132 6145 if (breg != 0)
67c0d1eb 6146 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6147 tempreg, tempreg, breg);
67c0d1eb 6148 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
252b5132 6149 }
0a44bf69 6150 else if (mips_big_got && HAVE_NEWABI)
f5040a92 6151 {
f5040a92
AO
6152 /* If this is a reference to an external symbol, we want
6153 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6154 add $tempreg,$tempreg,$gp
6155 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6156 <op> $treg,<ofst>($tempreg)
6157 Otherwise, for local symbols, we want:
6158 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6159 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
6160 assert (offset_expr.X_op == O_symbol);
4d7206a2 6161 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
6162 offset_expr.X_add_number = 0;
6163 if (expr1.X_add_number < -0x8000
6164 || expr1.X_add_number >= 0x8000)
6165 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4d7206a2 6166 relax_start (offset_expr.X_add_symbol);
67c0d1eb 6167 macro_build (&offset_expr, "lui", "t,u", tempreg,
17a2f251 6168 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
6169 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
6170 mips_gp_register);
6171 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6172 BFD_RELOC_MIPS_GOT_LO16, tempreg);
f5040a92 6173 if (breg != 0)
67c0d1eb 6174 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6175 tempreg, tempreg, breg);
67c0d1eb 6176 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
684022ea 6177
4d7206a2 6178 relax_switch ();
f5040a92 6179 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
6180 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6181 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 6182 if (breg != 0)
67c0d1eb 6183 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6184 tempreg, tempreg, breg);
67c0d1eb 6185 macro_build (&offset_expr, s, fmt, treg,
17a2f251 6186 BFD_RELOC_MIPS_GOT_OFST, tempreg);
4d7206a2 6187 relax_end ();
f5040a92 6188 }
252b5132
RH
6189 else
6190 abort ();
6191
252b5132
RH
6192 break;
6193
6194 case M_LI:
6195 case M_LI_S:
67c0d1eb 6196 load_register (treg, &imm_expr, 0);
8fc2e39e 6197 break;
252b5132
RH
6198
6199 case M_DLI:
67c0d1eb 6200 load_register (treg, &imm_expr, 1);
8fc2e39e 6201 break;
252b5132
RH
6202
6203 case M_LI_SS:
6204 if (imm_expr.X_op == O_constant)
6205 {
8fc2e39e 6206 used_at = 1;
67c0d1eb
RS
6207 load_register (AT, &imm_expr, 0);
6208 macro_build (NULL, "mtc1", "t,G", AT, treg);
252b5132
RH
6209 break;
6210 }
6211 else
6212 {
6213 assert (offset_expr.X_op == O_symbol
6214 && strcmp (segment_name (S_GET_SEGMENT
6215 (offset_expr.X_add_symbol)),
6216 ".lit4") == 0
6217 && offset_expr.X_add_number == 0);
67c0d1eb 6218 macro_build (&offset_expr, "lwc1", "T,o(b)", treg,
17a2f251 6219 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
8fc2e39e 6220 break;
252b5132
RH
6221 }
6222
6223 case M_LI_D:
ca4e0257
RS
6224 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
6225 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
6226 order 32 bits of the value and the low order 32 bits are either
6227 zero or in OFFSET_EXPR. */
252b5132
RH
6228 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6229 {
ca4e0257 6230 if (HAVE_64BIT_GPRS)
67c0d1eb 6231 load_register (treg, &imm_expr, 1);
252b5132
RH
6232 else
6233 {
6234 int hreg, lreg;
6235
6236 if (target_big_endian)
6237 {
6238 hreg = treg;
6239 lreg = treg + 1;
6240 }
6241 else
6242 {
6243 hreg = treg + 1;
6244 lreg = treg;
6245 }
6246
6247 if (hreg <= 31)
67c0d1eb 6248 load_register (hreg, &imm_expr, 0);
252b5132
RH
6249 if (lreg <= 31)
6250 {
6251 if (offset_expr.X_op == O_absent)
67c0d1eb 6252 move_register (lreg, 0);
252b5132
RH
6253 else
6254 {
6255 assert (offset_expr.X_op == O_constant);
67c0d1eb 6256 load_register (lreg, &offset_expr, 0);
252b5132
RH
6257 }
6258 }
6259 }
8fc2e39e 6260 break;
252b5132
RH
6261 }
6262
6263 /* We know that sym is in the .rdata section. First we get the
6264 upper 16 bits of the address. */
6265 if (mips_pic == NO_PIC)
6266 {
67c0d1eb 6267 macro_build_lui (&offset_expr, AT);
8fc2e39e 6268 used_at = 1;
252b5132 6269 }
0a44bf69 6270 else
252b5132 6271 {
67c0d1eb
RS
6272 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6273 BFD_RELOC_MIPS_GOT16, mips_gp_register);
8fc2e39e 6274 used_at = 1;
252b5132 6275 }
bdaaa2e1 6276
252b5132 6277 /* Now we load the register(s). */
ca4e0257 6278 if (HAVE_64BIT_GPRS)
8fc2e39e
TS
6279 {
6280 used_at = 1;
6281 macro_build (&offset_expr, "ld", "t,o(b)", treg, BFD_RELOC_LO16, AT);
6282 }
252b5132
RH
6283 else
6284 {
8fc2e39e 6285 used_at = 1;
67c0d1eb 6286 macro_build (&offset_expr, "lw", "t,o(b)", treg, BFD_RELOC_LO16, AT);
f9419b05 6287 if (treg != RA)
252b5132
RH
6288 {
6289 /* FIXME: How in the world do we deal with the possible
6290 overflow here? */
6291 offset_expr.X_add_number += 4;
67c0d1eb 6292 macro_build (&offset_expr, "lw", "t,o(b)",
17a2f251 6293 treg + 1, BFD_RELOC_LO16, AT);
252b5132
RH
6294 }
6295 }
252b5132
RH
6296 break;
6297
6298 case M_LI_DD:
ca4e0257
RS
6299 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
6300 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
6301 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
6302 the value and the low order 32 bits are either zero or in
6303 OFFSET_EXPR. */
252b5132
RH
6304 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6305 {
8fc2e39e 6306 used_at = 1;
67c0d1eb 6307 load_register (AT, &imm_expr, HAVE_64BIT_FPRS);
ca4e0257
RS
6308 if (HAVE_64BIT_FPRS)
6309 {
6310 assert (HAVE_64BIT_GPRS);
67c0d1eb 6311 macro_build (NULL, "dmtc1", "t,S", AT, treg);
ca4e0257 6312 }
252b5132
RH
6313 else
6314 {
67c0d1eb 6315 macro_build (NULL, "mtc1", "t,G", AT, treg + 1);
252b5132 6316 if (offset_expr.X_op == O_absent)
67c0d1eb 6317 macro_build (NULL, "mtc1", "t,G", 0, treg);
252b5132
RH
6318 else
6319 {
6320 assert (offset_expr.X_op == O_constant);
67c0d1eb
RS
6321 load_register (AT, &offset_expr, 0);
6322 macro_build (NULL, "mtc1", "t,G", AT, treg);
252b5132
RH
6323 }
6324 }
6325 break;
6326 }
6327
6328 assert (offset_expr.X_op == O_symbol
6329 && offset_expr.X_add_number == 0);
6330 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
6331 if (strcmp (s, ".lit8") == 0)
6332 {
e7af610e 6333 if (mips_opts.isa != ISA_MIPS1)
252b5132 6334 {
67c0d1eb 6335 macro_build (&offset_expr, "ldc1", "T,o(b)", treg,
17a2f251 6336 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
8fc2e39e 6337 break;
252b5132 6338 }
c9914766 6339 breg = mips_gp_register;
252b5132
RH
6340 r = BFD_RELOC_MIPS_LITERAL;
6341 goto dob;
6342 }
6343 else
6344 {
6345 assert (strcmp (s, RDATA_SECTION_NAME) == 0);
8fc2e39e 6346 used_at = 1;
0a44bf69 6347 if (mips_pic != NO_PIC)
67c0d1eb
RS
6348 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6349 BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132
RH
6350 else
6351 {
6352 /* FIXME: This won't work for a 64 bit address. */
67c0d1eb 6353 macro_build_lui (&offset_expr, AT);
252b5132 6354 }
bdaaa2e1 6355
e7af610e 6356 if (mips_opts.isa != ISA_MIPS1)
252b5132 6357 {
67c0d1eb
RS
6358 macro_build (&offset_expr, "ldc1", "T,o(b)",
6359 treg, BFD_RELOC_LO16, AT);
252b5132
RH
6360 break;
6361 }
6362 breg = AT;
6363 r = BFD_RELOC_LO16;
6364 goto dob;
6365 }
6366
6367 case M_L_DOB:
fef14a42 6368 if (mips_opts.arch == CPU_R4650)
252b5132
RH
6369 {
6370 as_bad (_("opcode not supported on this processor"));
8fc2e39e 6371 break;
252b5132
RH
6372 }
6373 /* Even on a big endian machine $fn comes before $fn+1. We have
6374 to adjust when loading from memory. */
6375 r = BFD_RELOC_LO16;
6376 dob:
e7af610e 6377 assert (mips_opts.isa == ISA_MIPS1);
67c0d1eb 6378 macro_build (&offset_expr, "lwc1", "T,o(b)",
17a2f251 6379 target_big_endian ? treg + 1 : treg, r, breg);
252b5132
RH
6380 /* FIXME: A possible overflow which I don't know how to deal
6381 with. */
6382 offset_expr.X_add_number += 4;
67c0d1eb 6383 macro_build (&offset_expr, "lwc1", "T,o(b)",
17a2f251 6384 target_big_endian ? treg : treg + 1, r, breg);
252b5132
RH
6385 break;
6386
6387 case M_L_DAB:
6388 /*
6389 * The MIPS assembler seems to check for X_add_number not
6390 * being double aligned and generating:
6391 * lui at,%hi(foo+1)
6392 * addu at,at,v1
6393 * addiu at,at,%lo(foo+1)
6394 * lwc1 f2,0(at)
6395 * lwc1 f3,4(at)
6396 * But, the resulting address is the same after relocation so why
6397 * generate the extra instruction?
6398 */
fef14a42 6399 if (mips_opts.arch == CPU_R4650)
252b5132
RH
6400 {
6401 as_bad (_("opcode not supported on this processor"));
8fc2e39e 6402 break;
252b5132 6403 }
bdaaa2e1 6404 /* Itbl support may require additional care here. */
252b5132 6405 coproc = 1;
e7af610e 6406 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
6407 {
6408 s = "ldc1";
6409 goto ld;
6410 }
6411
6412 s = "lwc1";
6413 fmt = "T,o(b)";
6414 goto ldd_std;
6415
6416 case M_S_DAB:
fef14a42 6417 if (mips_opts.arch == CPU_R4650)
252b5132
RH
6418 {
6419 as_bad (_("opcode not supported on this processor"));
8fc2e39e 6420 break;
252b5132
RH
6421 }
6422
e7af610e 6423 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
6424 {
6425 s = "sdc1";
6426 goto st;
6427 }
6428
6429 s = "swc1";
6430 fmt = "T,o(b)";
bdaaa2e1 6431 /* Itbl support may require additional care here. */
252b5132
RH
6432 coproc = 1;
6433 goto ldd_std;
6434
6435 case M_LD_AB:
ca4e0257 6436 if (HAVE_64BIT_GPRS)
252b5132
RH
6437 {
6438 s = "ld";
6439 goto ld;
6440 }
6441
6442 s = "lw";
6443 fmt = "t,o(b)";
6444 goto ldd_std;
6445
6446 case M_SD_AB:
ca4e0257 6447 if (HAVE_64BIT_GPRS)
252b5132
RH
6448 {
6449 s = "sd";
6450 goto st;
6451 }
6452
6453 s = "sw";
6454 fmt = "t,o(b)";
6455
6456 ldd_std:
6457 if (offset_expr.X_op != O_symbol
6458 && offset_expr.X_op != O_constant)
6459 {
6460 as_bad (_("expression too complex"));
6461 offset_expr.X_op = O_constant;
6462 }
6463
2051e8c4
MR
6464 if (HAVE_32BIT_ADDRESSES
6465 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
6466 {
6467 char value [32];
6468
6469 sprintf_vma (value, offset_expr.X_add_number);
20e1fcfd 6470 as_bad (_("Number (0x%s) larger than 32 bits"), value);
55e08f71 6471 }
2051e8c4 6472
252b5132
RH
6473 /* Even on a big endian machine $fn comes before $fn+1. We have
6474 to adjust when loading from memory. We set coproc if we must
6475 load $fn+1 first. */
bdaaa2e1 6476 /* Itbl support may require additional care here. */
252b5132
RH
6477 if (! target_big_endian)
6478 coproc = 0;
6479
6480 if (mips_pic == NO_PIC
6481 || offset_expr.X_op == O_constant)
6482 {
6483 /* If this is a reference to a GP relative symbol, we want
cdf6fd85
TS
6484 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6485 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
6486 If we have a base register, we use this
6487 addu $at,$breg,$gp
cdf6fd85
TS
6488 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6489 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
6490 If this is not a GP relative symbol, we want
6491 lui $at,<sym> (BFD_RELOC_HI16_S)
6492 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6493 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6494 If there is a base register, we add it to $at after the
6495 lui instruction. If there is a constant, we always use
6496 the last case. */
39a59cf8
MR
6497 if (offset_expr.X_op == O_symbol
6498 && (valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 6499 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 6500 {
4d7206a2 6501 relax_start (offset_expr.X_add_symbol);
252b5132
RH
6502 if (breg == 0)
6503 {
c9914766 6504 tempreg = mips_gp_register;
252b5132
RH
6505 }
6506 else
6507 {
67c0d1eb 6508 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6509 AT, breg, mips_gp_register);
252b5132 6510 tempreg = AT;
252b5132
RH
6511 used_at = 1;
6512 }
6513
beae10d5 6514 /* Itbl support may require additional care here. */
67c0d1eb 6515 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
17a2f251 6516 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6517 offset_expr.X_add_number += 4;
6518
6519 /* Set mips_optimize to 2 to avoid inserting an
6520 undesired nop. */
6521 hold_mips_optimize = mips_optimize;
6522 mips_optimize = 2;
beae10d5 6523 /* Itbl support may require additional care here. */
67c0d1eb 6524 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
17a2f251 6525 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6526 mips_optimize = hold_mips_optimize;
6527
4d7206a2 6528 relax_switch ();
252b5132
RH
6529
6530 /* We just generated two relocs. When tc_gen_reloc
6531 handles this case, it will skip the first reloc and
6532 handle the second. The second reloc already has an
6533 extra addend of 4, which we added above. We must
6534 subtract it out, and then subtract another 4 to make
6535 the first reloc come out right. The second reloc
6536 will come out right because we are going to add 4 to
6537 offset_expr when we build its instruction below.
6538
6539 If we have a symbol, then we don't want to include
6540 the offset, because it will wind up being included
6541 when we generate the reloc. */
6542
6543 if (offset_expr.X_op == O_constant)
6544 offset_expr.X_add_number -= 8;
6545 else
6546 {
6547 offset_expr.X_add_number = -4;
6548 offset_expr.X_op = O_constant;
6549 }
6550 }
8fc2e39e 6551 used_at = 1;
67c0d1eb 6552 macro_build_lui (&offset_expr, AT);
252b5132 6553 if (breg != 0)
67c0d1eb 6554 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 6555 /* Itbl support may require additional care here. */
67c0d1eb 6556 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
17a2f251 6557 BFD_RELOC_LO16, AT);
252b5132
RH
6558 /* FIXME: How do we handle overflow here? */
6559 offset_expr.X_add_number += 4;
beae10d5 6560 /* Itbl support may require additional care here. */
67c0d1eb 6561 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
17a2f251 6562 BFD_RELOC_LO16, AT);
4d7206a2
RS
6563 if (mips_relax.sequence)
6564 relax_end ();
bdaaa2e1 6565 }
0a44bf69 6566 else if (!mips_big_got)
252b5132 6567 {
252b5132
RH
6568 /* If this is a reference to an external symbol, we want
6569 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6570 nop
6571 <op> $treg,0($at)
6572 <op> $treg+1,4($at)
6573 Otherwise we want
6574 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6575 nop
6576 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6577 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6578 If there is a base register we add it to $at before the
6579 lwc1 instructions. If there is a constant we include it
6580 in the lwc1 instructions. */
6581 used_at = 1;
6582 expr1.X_add_number = offset_expr.X_add_number;
252b5132
RH
6583 if (expr1.X_add_number < -0x8000
6584 || expr1.X_add_number >= 0x8000 - 4)
6585 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 6586 load_got_offset (AT, &offset_expr);
269137b2 6587 load_delay_nop ();
252b5132 6588 if (breg != 0)
67c0d1eb 6589 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
252b5132
RH
6590
6591 /* Set mips_optimize to 2 to avoid inserting an undesired
6592 nop. */
6593 hold_mips_optimize = mips_optimize;
6594 mips_optimize = 2;
4d7206a2 6595
beae10d5 6596 /* Itbl support may require additional care here. */
4d7206a2 6597 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
6598 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
6599 BFD_RELOC_LO16, AT);
4d7206a2 6600 expr1.X_add_number += 4;
67c0d1eb
RS
6601 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
6602 BFD_RELOC_LO16, AT);
4d7206a2 6603 relax_switch ();
67c0d1eb
RS
6604 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6605 BFD_RELOC_LO16, AT);
4d7206a2 6606 offset_expr.X_add_number += 4;
67c0d1eb
RS
6607 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6608 BFD_RELOC_LO16, AT);
4d7206a2 6609 relax_end ();
252b5132 6610
4d7206a2 6611 mips_optimize = hold_mips_optimize;
252b5132 6612 }
0a44bf69 6613 else if (mips_big_got)
252b5132 6614 {
67c0d1eb 6615 int gpdelay;
252b5132
RH
6616
6617 /* If this is a reference to an external symbol, we want
6618 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6619 addu $at,$at,$gp
6620 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
6621 nop
6622 <op> $treg,0($at)
6623 <op> $treg+1,4($at)
6624 Otherwise we want
6625 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6626 nop
6627 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6628 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6629 If there is a base register we add it to $at before the
6630 lwc1 instructions. If there is a constant we include it
6631 in the lwc1 instructions. */
6632 used_at = 1;
6633 expr1.X_add_number = offset_expr.X_add_number;
6634 offset_expr.X_add_number = 0;
6635 if (expr1.X_add_number < -0x8000
6636 || expr1.X_add_number >= 0x8000 - 4)
6637 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 6638 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 6639 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
6640 macro_build (&offset_expr, "lui", "t,u",
6641 AT, BFD_RELOC_MIPS_GOT_HI16);
6642 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6643 AT, AT, mips_gp_register);
67c0d1eb 6644 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 6645 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
269137b2 6646 load_delay_nop ();
252b5132 6647 if (breg != 0)
67c0d1eb 6648 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 6649 /* Itbl support may require additional care here. */
67c0d1eb 6650 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
17a2f251 6651 BFD_RELOC_LO16, AT);
252b5132
RH
6652 expr1.X_add_number += 4;
6653
6654 /* Set mips_optimize to 2 to avoid inserting an undesired
6655 nop. */
6656 hold_mips_optimize = mips_optimize;
6657 mips_optimize = 2;
beae10d5 6658 /* Itbl support may require additional care here. */
67c0d1eb 6659 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
17a2f251 6660 BFD_RELOC_LO16, AT);
252b5132
RH
6661 mips_optimize = hold_mips_optimize;
6662 expr1.X_add_number -= 4;
6663
4d7206a2
RS
6664 relax_switch ();
6665 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
6666 if (gpdelay)
6667 macro_build (NULL, "nop", "");
6668 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6669 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 6670 load_delay_nop ();
252b5132 6671 if (breg != 0)
67c0d1eb 6672 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 6673 /* Itbl support may require additional care here. */
67c0d1eb
RS
6674 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6675 BFD_RELOC_LO16, AT);
4d7206a2 6676 offset_expr.X_add_number += 4;
252b5132
RH
6677
6678 /* Set mips_optimize to 2 to avoid inserting an undesired
6679 nop. */
6680 hold_mips_optimize = mips_optimize;
6681 mips_optimize = 2;
beae10d5 6682 /* Itbl support may require additional care here. */
67c0d1eb
RS
6683 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6684 BFD_RELOC_LO16, AT);
252b5132 6685 mips_optimize = hold_mips_optimize;
4d7206a2 6686 relax_end ();
252b5132 6687 }
252b5132
RH
6688 else
6689 abort ();
6690
252b5132
RH
6691 break;
6692
6693 case M_LD_OB:
6694 s = "lw";
6695 goto sd_ob;
6696 case M_SD_OB:
6697 s = "sw";
6698 sd_ob:
ca4e0257 6699 assert (HAVE_32BIT_ADDRESSES);
67c0d1eb 6700 macro_build (&offset_expr, s, "t,o(b)", treg, BFD_RELOC_LO16, breg);
252b5132 6701 offset_expr.X_add_number += 4;
67c0d1eb 6702 macro_build (&offset_expr, s, "t,o(b)", treg + 1, BFD_RELOC_LO16, breg);
8fc2e39e 6703 break;
252b5132
RH
6704
6705 /* New code added to support COPZ instructions.
6706 This code builds table entries out of the macros in mip_opcodes.
6707 R4000 uses interlocks to handle coproc delays.
6708 Other chips (like the R3000) require nops to be inserted for delays.
6709
f72c8c98 6710 FIXME: Currently, we require that the user handle delays.
252b5132
RH
6711 In order to fill delay slots for non-interlocked chips,
6712 we must have a way to specify delays based on the coprocessor.
6713 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
6714 What are the side-effects of the cop instruction?
6715 What cache support might we have and what are its effects?
6716 Both coprocessor & memory require delays. how long???
bdaaa2e1 6717 What registers are read/set/modified?
252b5132
RH
6718
6719 If an itbl is provided to interpret cop instructions,
bdaaa2e1 6720 this knowledge can be encoded in the itbl spec. */
252b5132
RH
6721
6722 case M_COP0:
6723 s = "c0";
6724 goto copz;
6725 case M_COP1:
6726 s = "c1";
6727 goto copz;
6728 case M_COP2:
6729 s = "c2";
6730 goto copz;
6731 case M_COP3:
6732 s = "c3";
6733 copz:
6734 /* For now we just do C (same as Cz). The parameter will be
6735 stored in insn_opcode by mips_ip. */
67c0d1eb 6736 macro_build (NULL, s, "C", ip->insn_opcode);
8fc2e39e 6737 break;
252b5132 6738
ea1fb5dc 6739 case M_MOVE:
67c0d1eb 6740 move_register (dreg, sreg);
8fc2e39e 6741 break;
ea1fb5dc 6742
252b5132
RH
6743#ifdef LOSING_COMPILER
6744 default:
6745 /* Try and see if this is a new itbl instruction.
6746 This code builds table entries out of the macros in mip_opcodes.
6747 FIXME: For now we just assemble the expression and pass it's
6748 value along as a 32-bit immediate.
bdaaa2e1 6749 We may want to have the assembler assemble this value,
252b5132
RH
6750 so that we gain the assembler's knowledge of delay slots,
6751 symbols, etc.
6752 Would it be more efficient to use mask (id) here? */
bdaaa2e1 6753 if (itbl_have_entries
252b5132 6754 && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
beae10d5 6755 {
252b5132
RH
6756 s = ip->insn_mo->name;
6757 s2 = "cop3";
6758 coproc = ITBL_DECODE_PNUM (immed_expr);;
67c0d1eb 6759 macro_build (&immed_expr, s, "C");
8fc2e39e 6760 break;
beae10d5 6761 }
252b5132 6762 macro2 (ip);
8fc2e39e 6763 break;
252b5132 6764 }
8fc2e39e
TS
6765 if (mips_opts.noat && used_at)
6766 as_bad (_("Macro used $at after \".set noat\""));
252b5132 6767}
bdaaa2e1 6768
252b5132 6769static void
17a2f251 6770macro2 (struct mips_cl_insn *ip)
252b5132
RH
6771{
6772 register int treg, sreg, dreg, breg;
6773 int tempreg;
6774 int mask;
252b5132
RH
6775 int used_at;
6776 expressionS expr1;
6777 const char *s;
6778 const char *s2;
6779 const char *fmt;
6780 int likely = 0;
6781 int dbl = 0;
6782 int coproc = 0;
6783 int lr = 0;
6784 int imm = 0;
6785 int off;
6786 offsetT maxnum;
6787 bfd_reloc_code_real_type r;
bdaaa2e1 6788
252b5132
RH
6789 treg = (ip->insn_opcode >> 16) & 0x1f;
6790 dreg = (ip->insn_opcode >> 11) & 0x1f;
6791 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
6792 mask = ip->insn_mo->mask;
bdaaa2e1 6793
252b5132
RH
6794 expr1.X_op = O_constant;
6795 expr1.X_op_symbol = NULL;
6796 expr1.X_add_symbol = NULL;
6797 expr1.X_add_number = 1;
bdaaa2e1 6798
252b5132
RH
6799 switch (mask)
6800 {
6801#endif /* LOSING_COMPILER */
6802
6803 case M_DMUL:
6804 dbl = 1;
6805 case M_MUL:
67c0d1eb
RS
6806 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", sreg, treg);
6807 macro_build (NULL, "mflo", "d", dreg);
8fc2e39e 6808 break;
252b5132
RH
6809
6810 case M_DMUL_I:
6811 dbl = 1;
6812 case M_MUL_I:
6813 /* The MIPS assembler some times generates shifts and adds. I'm
6814 not trying to be that fancy. GCC should do this for us
6815 anyway. */
8fc2e39e 6816 used_at = 1;
67c0d1eb
RS
6817 load_register (AT, &imm_expr, dbl);
6818 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, AT);
6819 macro_build (NULL, "mflo", "d", dreg);
252b5132
RH
6820 break;
6821
6822 case M_DMULO_I:
6823 dbl = 1;
6824 case M_MULO_I:
6825 imm = 1;
6826 goto do_mulo;
6827
6828 case M_DMULO:
6829 dbl = 1;
6830 case M_MULO:
6831 do_mulo:
7d10b47d 6832 start_noreorder ();
8fc2e39e 6833 used_at = 1;
252b5132 6834 if (imm)
67c0d1eb
RS
6835 load_register (AT, &imm_expr, dbl);
6836 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
6837 macro_build (NULL, "mflo", "d", dreg);
6838 macro_build (NULL, dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, RA);
6839 macro_build (NULL, "mfhi", "d", AT);
252b5132 6840 if (mips_trap)
67c0d1eb 6841 macro_build (NULL, "tne", "s,t,q", dreg, AT, 6);
252b5132
RH
6842 else
6843 {
6844 expr1.X_add_number = 8;
67c0d1eb
RS
6845 macro_build (&expr1, "beq", "s,t,p", dreg, AT);
6846 macro_build (NULL, "nop", "", 0);
6847 macro_build (NULL, "break", "c", 6);
252b5132 6848 }
7d10b47d 6849 end_noreorder ();
67c0d1eb 6850 macro_build (NULL, "mflo", "d", dreg);
252b5132
RH
6851 break;
6852
6853 case M_DMULOU_I:
6854 dbl = 1;
6855 case M_MULOU_I:
6856 imm = 1;
6857 goto do_mulou;
6858
6859 case M_DMULOU:
6860 dbl = 1;
6861 case M_MULOU:
6862 do_mulou:
7d10b47d 6863 start_noreorder ();
8fc2e39e 6864 used_at = 1;
252b5132 6865 if (imm)
67c0d1eb
RS
6866 load_register (AT, &imm_expr, dbl);
6867 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
17a2f251 6868 sreg, imm ? AT : treg);
67c0d1eb
RS
6869 macro_build (NULL, "mfhi", "d", AT);
6870 macro_build (NULL, "mflo", "d", dreg);
252b5132 6871 if (mips_trap)
67c0d1eb 6872 macro_build (NULL, "tne", "s,t,q", AT, 0, 6);
252b5132
RH
6873 else
6874 {
6875 expr1.X_add_number = 8;
67c0d1eb
RS
6876 macro_build (&expr1, "beq", "s,t,p", AT, 0);
6877 macro_build (NULL, "nop", "", 0);
6878 macro_build (NULL, "break", "c", 6);
252b5132 6879 }
7d10b47d 6880 end_noreorder ();
252b5132
RH
6881 break;
6882
771c7ce4 6883 case M_DROL:
fef14a42 6884 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097
CD
6885 {
6886 if (dreg == sreg)
6887 {
6888 tempreg = AT;
6889 used_at = 1;
6890 }
6891 else
6892 {
6893 tempreg = dreg;
82dd0097 6894 }
67c0d1eb
RS
6895 macro_build (NULL, "dnegu", "d,w", tempreg, treg);
6896 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, tempreg);
8fc2e39e 6897 break;
82dd0097 6898 }
8fc2e39e 6899 used_at = 1;
67c0d1eb
RS
6900 macro_build (NULL, "dsubu", "d,v,t", AT, 0, treg);
6901 macro_build (NULL, "dsrlv", "d,t,s", AT, sreg, AT);
6902 macro_build (NULL, "dsllv", "d,t,s", dreg, sreg, treg);
6903 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
6904 break;
6905
252b5132 6906 case M_ROL:
fef14a42 6907 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097
CD
6908 {
6909 if (dreg == sreg)
6910 {
6911 tempreg = AT;
6912 used_at = 1;
6913 }
6914 else
6915 {
6916 tempreg = dreg;
82dd0097 6917 }
67c0d1eb
RS
6918 macro_build (NULL, "negu", "d,w", tempreg, treg);
6919 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, tempreg);
8fc2e39e 6920 break;
82dd0097 6921 }
8fc2e39e 6922 used_at = 1;
67c0d1eb
RS
6923 macro_build (NULL, "subu", "d,v,t", AT, 0, treg);
6924 macro_build (NULL, "srlv", "d,t,s", AT, sreg, AT);
6925 macro_build (NULL, "sllv", "d,t,s", dreg, sreg, treg);
6926 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
6927 break;
6928
771c7ce4
TS
6929 case M_DROL_I:
6930 {
6931 unsigned int rot;
82dd0097 6932 char *l, *r;
771c7ce4
TS
6933
6934 if (imm_expr.X_op != O_constant)
82dd0097 6935 as_bad (_("Improper rotate count"));
771c7ce4 6936 rot = imm_expr.X_add_number & 0x3f;
fef14a42 6937 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
60b63b72
RS
6938 {
6939 rot = (64 - rot) & 0x3f;
6940 if (rot >= 32)
67c0d1eb 6941 macro_build (NULL, "dror32", "d,w,<", dreg, sreg, rot - 32);
60b63b72 6942 else
67c0d1eb 6943 macro_build (NULL, "dror", "d,w,<", dreg, sreg, rot);
8fc2e39e 6944 break;
60b63b72 6945 }
483fc7cd 6946 if (rot == 0)
483fc7cd 6947 {
67c0d1eb 6948 macro_build (NULL, "dsrl", "d,w,<", dreg, sreg, 0);
8fc2e39e 6949 break;
483fc7cd 6950 }
82dd0097
CD
6951 l = (rot < 0x20) ? "dsll" : "dsll32";
6952 r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
6953 rot &= 0x1f;
8fc2e39e 6954 used_at = 1;
67c0d1eb
RS
6955 macro_build (NULL, l, "d,w,<", AT, sreg, rot);
6956 macro_build (NULL, r, "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6957 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
6958 }
6959 break;
6960
252b5132 6961 case M_ROL_I:
771c7ce4
TS
6962 {
6963 unsigned int rot;
6964
6965 if (imm_expr.X_op != O_constant)
82dd0097 6966 as_bad (_("Improper rotate count"));
771c7ce4 6967 rot = imm_expr.X_add_number & 0x1f;
fef14a42 6968 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
60b63b72 6969 {
67c0d1eb 6970 macro_build (NULL, "ror", "d,w,<", dreg, sreg, (32 - rot) & 0x1f);
8fc2e39e 6971 break;
60b63b72 6972 }
483fc7cd 6973 if (rot == 0)
483fc7cd 6974 {
67c0d1eb 6975 macro_build (NULL, "srl", "d,w,<", dreg, sreg, 0);
8fc2e39e 6976 break;
483fc7cd 6977 }
8fc2e39e 6978 used_at = 1;
67c0d1eb
RS
6979 macro_build (NULL, "sll", "d,w,<", AT, sreg, rot);
6980 macro_build (NULL, "srl", "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6981 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
6982 }
6983 break;
6984
6985 case M_DROR:
fef14a42 6986 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097 6987 {
67c0d1eb 6988 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, treg);
8fc2e39e 6989 break;
82dd0097 6990 }
8fc2e39e 6991 used_at = 1;
67c0d1eb
RS
6992 macro_build (NULL, "dsubu", "d,v,t", AT, 0, treg);
6993 macro_build (NULL, "dsllv", "d,t,s", AT, sreg, AT);
6994 macro_build (NULL, "dsrlv", "d,t,s", dreg, sreg, treg);
6995 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
6996 break;
6997
6998 case M_ROR:
fef14a42 6999 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 7000 {
67c0d1eb 7001 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, treg);
8fc2e39e 7002 break;
82dd0097 7003 }
8fc2e39e 7004 used_at = 1;
67c0d1eb
RS
7005 macro_build (NULL, "subu", "d,v,t", AT, 0, treg);
7006 macro_build (NULL, "sllv", "d,t,s", AT, sreg, AT);
7007 macro_build (NULL, "srlv", "d,t,s", dreg, sreg, treg);
7008 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
7009 break;
7010
771c7ce4
TS
7011 case M_DROR_I:
7012 {
7013 unsigned int rot;
82dd0097 7014 char *l, *r;
771c7ce4
TS
7015
7016 if (imm_expr.X_op != O_constant)
82dd0097 7017 as_bad (_("Improper rotate count"));
771c7ce4 7018 rot = imm_expr.X_add_number & 0x3f;
fef14a42 7019 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097
CD
7020 {
7021 if (rot >= 32)
67c0d1eb 7022 macro_build (NULL, "dror32", "d,w,<", dreg, sreg, rot - 32);
82dd0097 7023 else
67c0d1eb 7024 macro_build (NULL, "dror", "d,w,<", dreg, sreg, rot);
8fc2e39e 7025 break;
82dd0097 7026 }
483fc7cd 7027 if (rot == 0)
483fc7cd 7028 {
67c0d1eb 7029 macro_build (NULL, "dsrl", "d,w,<", dreg, sreg, 0);
8fc2e39e 7030 break;
483fc7cd 7031 }
82dd0097
CD
7032 r = (rot < 0x20) ? "dsrl" : "dsrl32";
7033 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
7034 rot &= 0x1f;
8fc2e39e 7035 used_at = 1;
67c0d1eb
RS
7036 macro_build (NULL, r, "d,w,<", AT, sreg, rot);
7037 macro_build (NULL, l, "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7038 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
7039 }
7040 break;
7041
252b5132 7042 case M_ROR_I:
771c7ce4
TS
7043 {
7044 unsigned int rot;
7045
7046 if (imm_expr.X_op != O_constant)
82dd0097 7047 as_bad (_("Improper rotate count"));
771c7ce4 7048 rot = imm_expr.X_add_number & 0x1f;
fef14a42 7049 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 7050 {
67c0d1eb 7051 macro_build (NULL, "ror", "d,w,<", dreg, sreg, rot);
8fc2e39e 7052 break;
82dd0097 7053 }
483fc7cd 7054 if (rot == 0)
483fc7cd 7055 {
67c0d1eb 7056 macro_build (NULL, "srl", "d,w,<", dreg, sreg, 0);
8fc2e39e 7057 break;
483fc7cd 7058 }
8fc2e39e 7059 used_at = 1;
67c0d1eb
RS
7060 macro_build (NULL, "srl", "d,w,<", AT, sreg, rot);
7061 macro_build (NULL, "sll", "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7062 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4 7063 }
252b5132
RH
7064 break;
7065
7066 case M_S_DOB:
fef14a42 7067 if (mips_opts.arch == CPU_R4650)
252b5132
RH
7068 {
7069 as_bad (_("opcode not supported on this processor"));
8fc2e39e 7070 break;
252b5132 7071 }
e7af610e 7072 assert (mips_opts.isa == ISA_MIPS1);
252b5132
RH
7073 /* Even on a big endian machine $fn comes before $fn+1. We have
7074 to adjust when storing to memory. */
67c0d1eb
RS
7075 macro_build (&offset_expr, "swc1", "T,o(b)",
7076 target_big_endian ? treg + 1 : treg, BFD_RELOC_LO16, breg);
252b5132 7077 offset_expr.X_add_number += 4;
67c0d1eb
RS
7078 macro_build (&offset_expr, "swc1", "T,o(b)",
7079 target_big_endian ? treg : treg + 1, BFD_RELOC_LO16, breg);
8fc2e39e 7080 break;
252b5132
RH
7081
7082 case M_SEQ:
7083 if (sreg == 0)
67c0d1eb 7084 macro_build (&expr1, "sltiu", "t,r,j", dreg, treg, BFD_RELOC_LO16);
252b5132 7085 else if (treg == 0)
67c0d1eb 7086 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7087 else
7088 {
67c0d1eb
RS
7089 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
7090 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
252b5132 7091 }
8fc2e39e 7092 break;
252b5132
RH
7093
7094 case M_SEQ_I:
7095 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7096 {
67c0d1eb 7097 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
8fc2e39e 7098 break;
252b5132
RH
7099 }
7100 if (sreg == 0)
7101 {
7102 as_warn (_("Instruction %s: result is always false"),
7103 ip->insn_mo->name);
67c0d1eb 7104 move_register (dreg, 0);
8fc2e39e 7105 break;
252b5132
RH
7106 }
7107 if (imm_expr.X_op == O_constant
7108 && imm_expr.X_add_number >= 0
7109 && imm_expr.X_add_number < 0x10000)
7110 {
67c0d1eb 7111 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7112 }
7113 else if (imm_expr.X_op == O_constant
7114 && imm_expr.X_add_number > -0x8000
7115 && imm_expr.X_add_number < 0)
7116 {
7117 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 7118 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
17a2f251 7119 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7120 }
7121 else
7122 {
67c0d1eb
RS
7123 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7124 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
252b5132
RH
7125 used_at = 1;
7126 }
67c0d1eb 7127 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
8fc2e39e 7128 break;
252b5132
RH
7129
7130 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
7131 s = "slt";
7132 goto sge;
7133 case M_SGEU:
7134 s = "sltu";
7135 sge:
67c0d1eb
RS
7136 macro_build (NULL, s, "d,v,t", dreg, sreg, treg);
7137 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
8fc2e39e 7138 break;
252b5132
RH
7139
7140 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
7141 case M_SGEU_I:
7142 if (imm_expr.X_op == O_constant
7143 && imm_expr.X_add_number >= -0x8000
7144 && imm_expr.X_add_number < 0x8000)
7145 {
67c0d1eb
RS
7146 macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
7147 dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7148 }
7149 else
7150 {
67c0d1eb
RS
7151 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7152 macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
7153 dreg, sreg, AT);
252b5132
RH
7154 used_at = 1;
7155 }
67c0d1eb 7156 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
8fc2e39e 7157 break;
252b5132
RH
7158
7159 case M_SGT: /* sreg > treg <==> treg < sreg */
7160 s = "slt";
7161 goto sgt;
7162 case M_SGTU:
7163 s = "sltu";
7164 sgt:
67c0d1eb 7165 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
8fc2e39e 7166 break;
252b5132
RH
7167
7168 case M_SGT_I: /* sreg > I <==> I < sreg */
7169 s = "slt";
7170 goto sgti;
7171 case M_SGTU_I:
7172 s = "sltu";
7173 sgti:
8fc2e39e 7174 used_at = 1;
67c0d1eb
RS
7175 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7176 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
252b5132
RH
7177 break;
7178
2396cfb9 7179 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
252b5132
RH
7180 s = "slt";
7181 goto sle;
7182 case M_SLEU:
7183 s = "sltu";
7184 sle:
67c0d1eb
RS
7185 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
7186 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
8fc2e39e 7187 break;
252b5132 7188
2396cfb9 7189 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
252b5132
RH
7190 s = "slt";
7191 goto slei;
7192 case M_SLEU_I:
7193 s = "sltu";
7194 slei:
8fc2e39e 7195 used_at = 1;
67c0d1eb
RS
7196 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7197 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
7198 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
252b5132
RH
7199 break;
7200
7201 case M_SLT_I:
7202 if (imm_expr.X_op == O_constant
7203 && imm_expr.X_add_number >= -0x8000
7204 && imm_expr.X_add_number < 0x8000)
7205 {
67c0d1eb 7206 macro_build (&imm_expr, "slti", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
8fc2e39e 7207 break;
252b5132 7208 }
8fc2e39e 7209 used_at = 1;
67c0d1eb
RS
7210 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7211 macro_build (NULL, "slt", "d,v,t", dreg, sreg, AT);
252b5132
RH
7212 break;
7213
7214 case M_SLTU_I:
7215 if (imm_expr.X_op == O_constant
7216 && imm_expr.X_add_number >= -0x8000
7217 && imm_expr.X_add_number < 0x8000)
7218 {
67c0d1eb 7219 macro_build (&imm_expr, "sltiu", "t,r,j", dreg, sreg,
17a2f251 7220 BFD_RELOC_LO16);
8fc2e39e 7221 break;
252b5132 7222 }
8fc2e39e 7223 used_at = 1;
67c0d1eb
RS
7224 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7225 macro_build (NULL, "sltu", "d,v,t", dreg, sreg, AT);
252b5132
RH
7226 break;
7227
7228 case M_SNE:
7229 if (sreg == 0)
67c0d1eb 7230 macro_build (NULL, "sltu", "d,v,t", dreg, 0, treg);
252b5132 7231 else if (treg == 0)
67c0d1eb 7232 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
252b5132
RH
7233 else
7234 {
67c0d1eb
RS
7235 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
7236 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
252b5132 7237 }
8fc2e39e 7238 break;
252b5132
RH
7239
7240 case M_SNE_I:
7241 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7242 {
67c0d1eb 7243 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
8fc2e39e 7244 break;
252b5132
RH
7245 }
7246 if (sreg == 0)
7247 {
7248 as_warn (_("Instruction %s: result is always true"),
7249 ip->insn_mo->name);
67c0d1eb
RS
7250 macro_build (&expr1, HAVE_32BIT_GPRS ? "addiu" : "daddiu", "t,r,j",
7251 dreg, 0, BFD_RELOC_LO16);
8fc2e39e 7252 break;
252b5132
RH
7253 }
7254 if (imm_expr.X_op == O_constant
7255 && imm_expr.X_add_number >= 0
7256 && imm_expr.X_add_number < 0x10000)
7257 {
67c0d1eb 7258 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7259 }
7260 else if (imm_expr.X_op == O_constant
7261 && imm_expr.X_add_number > -0x8000
7262 && imm_expr.X_add_number < 0)
7263 {
7264 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 7265 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
17a2f251 7266 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7267 }
7268 else
7269 {
67c0d1eb
RS
7270 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7271 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
252b5132
RH
7272 used_at = 1;
7273 }
67c0d1eb 7274 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
8fc2e39e 7275 break;
252b5132
RH
7276
7277 case M_DSUB_I:
7278 dbl = 1;
7279 case M_SUB_I:
7280 if (imm_expr.X_op == O_constant
7281 && imm_expr.X_add_number > -0x8000
7282 && imm_expr.X_add_number <= 0x8000)
7283 {
7284 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb
RS
7285 macro_build (&imm_expr, dbl ? "daddi" : "addi", "t,r,j",
7286 dreg, sreg, BFD_RELOC_LO16);
8fc2e39e 7287 break;
252b5132 7288 }
8fc2e39e 7289 used_at = 1;
67c0d1eb
RS
7290 load_register (AT, &imm_expr, dbl);
7291 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
252b5132
RH
7292 break;
7293
7294 case M_DSUBU_I:
7295 dbl = 1;
7296 case M_SUBU_I:
7297 if (imm_expr.X_op == O_constant
7298 && imm_expr.X_add_number > -0x8000
7299 && imm_expr.X_add_number <= 0x8000)
7300 {
7301 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb
RS
7302 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "t,r,j",
7303 dreg, sreg, BFD_RELOC_LO16);
8fc2e39e 7304 break;
252b5132 7305 }
8fc2e39e 7306 used_at = 1;
67c0d1eb
RS
7307 load_register (AT, &imm_expr, dbl);
7308 macro_build (NULL, dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
252b5132
RH
7309 break;
7310
7311 case M_TEQ_I:
7312 s = "teq";
7313 goto trap;
7314 case M_TGE_I:
7315 s = "tge";
7316 goto trap;
7317 case M_TGEU_I:
7318 s = "tgeu";
7319 goto trap;
7320 case M_TLT_I:
7321 s = "tlt";
7322 goto trap;
7323 case M_TLTU_I:
7324 s = "tltu";
7325 goto trap;
7326 case M_TNE_I:
7327 s = "tne";
7328 trap:
8fc2e39e 7329 used_at = 1;
67c0d1eb
RS
7330 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7331 macro_build (NULL, s, "s,t", sreg, AT);
252b5132
RH
7332 break;
7333
252b5132 7334 case M_TRUNCWS:
43841e91 7335 case M_TRUNCWD:
e7af610e 7336 assert (mips_opts.isa == ISA_MIPS1);
8fc2e39e 7337 used_at = 1;
252b5132
RH
7338 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
7339 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
7340
7341 /*
7342 * Is the double cfc1 instruction a bug in the mips assembler;
7343 * or is there a reason for it?
7344 */
7d10b47d 7345 start_noreorder ();
67c0d1eb
RS
7346 macro_build (NULL, "cfc1", "t,G", treg, RA);
7347 macro_build (NULL, "cfc1", "t,G", treg, RA);
7348 macro_build (NULL, "nop", "");
252b5132 7349 expr1.X_add_number = 3;
67c0d1eb 7350 macro_build (&expr1, "ori", "t,r,i", AT, treg, BFD_RELOC_LO16);
252b5132 7351 expr1.X_add_number = 2;
67c0d1eb
RS
7352 macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
7353 macro_build (NULL, "ctc1", "t,G", AT, RA);
7354 macro_build (NULL, "nop", "");
7355 macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
7356 dreg, sreg);
7357 macro_build (NULL, "ctc1", "t,G", treg, RA);
7358 macro_build (NULL, "nop", "");
7d10b47d 7359 end_noreorder ();
252b5132
RH
7360 break;
7361
7362 case M_ULH:
7363 s = "lb";
7364 goto ulh;
7365 case M_ULHU:
7366 s = "lbu";
7367 ulh:
8fc2e39e 7368 used_at = 1;
252b5132
RH
7369 if (offset_expr.X_add_number >= 0x7fff)
7370 as_bad (_("operand overflow"));
252b5132 7371 if (! target_big_endian)
f9419b05 7372 ++offset_expr.X_add_number;
67c0d1eb 7373 macro_build (&offset_expr, s, "t,o(b)", AT, BFD_RELOC_LO16, breg);
252b5132 7374 if (! target_big_endian)
f9419b05 7375 --offset_expr.X_add_number;
252b5132 7376 else
f9419b05 7377 ++offset_expr.X_add_number;
67c0d1eb
RS
7378 macro_build (&offset_expr, "lbu", "t,o(b)", treg, BFD_RELOC_LO16, breg);
7379 macro_build (NULL, "sll", "d,w,<", AT, AT, 8);
7380 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
252b5132
RH
7381 break;
7382
7383 case M_ULD:
7384 s = "ldl";
7385 s2 = "ldr";
7386 off = 7;
7387 goto ulw;
7388 case M_ULW:
7389 s = "lwl";
7390 s2 = "lwr";
7391 off = 3;
7392 ulw:
7393 if (offset_expr.X_add_number >= 0x8000 - off)
7394 as_bad (_("operand overflow"));
af22f5b2
CD
7395 if (treg != breg)
7396 tempreg = treg;
7397 else
8fc2e39e
TS
7398 {
7399 used_at = 1;
7400 tempreg = AT;
7401 }
252b5132
RH
7402 if (! target_big_endian)
7403 offset_expr.X_add_number += off;
67c0d1eb 7404 macro_build (&offset_expr, s, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
252b5132
RH
7405 if (! target_big_endian)
7406 offset_expr.X_add_number -= off;
7407 else
7408 offset_expr.X_add_number += off;
67c0d1eb 7409 macro_build (&offset_expr, s2, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
af22f5b2
CD
7410
7411 /* If necessary, move the result in tempreg the final destination. */
7412 if (treg == tempreg)
8fc2e39e 7413 break;
af22f5b2 7414 /* Protect second load's delay slot. */
017315e4 7415 load_delay_nop ();
67c0d1eb 7416 move_register (treg, tempreg);
af22f5b2 7417 break;
252b5132
RH
7418
7419 case M_ULD_A:
7420 s = "ldl";
7421 s2 = "ldr";
7422 off = 7;
7423 goto ulwa;
7424 case M_ULW_A:
7425 s = "lwl";
7426 s2 = "lwr";
7427 off = 3;
7428 ulwa:
d6bc6245 7429 used_at = 1;
67c0d1eb 7430 load_address (AT, &offset_expr, &used_at);
252b5132 7431 if (breg != 0)
67c0d1eb 7432 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
252b5132
RH
7433 if (! target_big_endian)
7434 expr1.X_add_number = off;
7435 else
7436 expr1.X_add_number = 0;
67c0d1eb 7437 macro_build (&expr1, s, "t,o(b)", treg, BFD_RELOC_LO16, AT);
252b5132
RH
7438 if (! target_big_endian)
7439 expr1.X_add_number = 0;
7440 else
7441 expr1.X_add_number = off;
67c0d1eb 7442 macro_build (&expr1, s2, "t,o(b)", treg, BFD_RELOC_LO16, AT);
252b5132
RH
7443 break;
7444
7445 case M_ULH_A:
7446 case M_ULHU_A:
d6bc6245 7447 used_at = 1;
67c0d1eb 7448 load_address (AT, &offset_expr, &used_at);
252b5132 7449 if (breg != 0)
67c0d1eb 7450 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
252b5132
RH
7451 if (target_big_endian)
7452 expr1.X_add_number = 0;
67c0d1eb 7453 macro_build (&expr1, mask == M_ULH_A ? "lb" : "lbu", "t,o(b)",
17a2f251 7454 treg, BFD_RELOC_LO16, AT);
252b5132
RH
7455 if (target_big_endian)
7456 expr1.X_add_number = 1;
7457 else
7458 expr1.X_add_number = 0;
67c0d1eb
RS
7459 macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
7460 macro_build (NULL, "sll", "d,w,<", treg, treg, 8);
7461 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
252b5132
RH
7462 break;
7463
7464 case M_USH:
8fc2e39e 7465 used_at = 1;
252b5132
RH
7466 if (offset_expr.X_add_number >= 0x7fff)
7467 as_bad (_("operand overflow"));
7468 if (target_big_endian)
f9419b05 7469 ++offset_expr.X_add_number;
67c0d1eb
RS
7470 macro_build (&offset_expr, "sb", "t,o(b)", treg, BFD_RELOC_LO16, breg);
7471 macro_build (NULL, "srl", "d,w,<", AT, treg, 8);
252b5132 7472 if (target_big_endian)
f9419b05 7473 --offset_expr.X_add_number;
252b5132 7474 else
f9419b05 7475 ++offset_expr.X_add_number;
67c0d1eb 7476 macro_build (&offset_expr, "sb", "t,o(b)", AT, BFD_RELOC_LO16, breg);
252b5132
RH
7477 break;
7478
7479 case M_USD:
7480 s = "sdl";
7481 s2 = "sdr";
7482 off = 7;
7483 goto usw;
7484 case M_USW:
7485 s = "swl";
7486 s2 = "swr";
7487 off = 3;
7488 usw:
7489 if (offset_expr.X_add_number >= 0x8000 - off)
7490 as_bad (_("operand overflow"));
7491 if (! target_big_endian)
7492 offset_expr.X_add_number += off;
67c0d1eb 7493 macro_build (&offset_expr, s, "t,o(b)", treg, BFD_RELOC_LO16, breg);
252b5132
RH
7494 if (! target_big_endian)
7495 offset_expr.X_add_number -= off;
7496 else
7497 offset_expr.X_add_number += off;
67c0d1eb 7498 macro_build (&offset_expr, s2, "t,o(b)", treg, BFD_RELOC_LO16, breg);
8fc2e39e 7499 break;
252b5132
RH
7500
7501 case M_USD_A:
7502 s = "sdl";
7503 s2 = "sdr";
7504 off = 7;
7505 goto uswa;
7506 case M_USW_A:
7507 s = "swl";
7508 s2 = "swr";
7509 off = 3;
7510 uswa:
d6bc6245 7511 used_at = 1;
67c0d1eb 7512 load_address (AT, &offset_expr, &used_at);
252b5132 7513 if (breg != 0)
67c0d1eb 7514 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
252b5132
RH
7515 if (! target_big_endian)
7516 expr1.X_add_number = off;
7517 else
7518 expr1.X_add_number = 0;
67c0d1eb 7519 macro_build (&expr1, s, "t,o(b)", treg, BFD_RELOC_LO16, AT);
252b5132
RH
7520 if (! target_big_endian)
7521 expr1.X_add_number = 0;
7522 else
7523 expr1.X_add_number = off;
67c0d1eb 7524 macro_build (&expr1, s2, "t,o(b)", treg, BFD_RELOC_LO16, AT);
252b5132
RH
7525 break;
7526
7527 case M_USH_A:
d6bc6245 7528 used_at = 1;
67c0d1eb 7529 load_address (AT, &offset_expr, &used_at);
252b5132 7530 if (breg != 0)
67c0d1eb 7531 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
252b5132
RH
7532 if (! target_big_endian)
7533 expr1.X_add_number = 0;
67c0d1eb
RS
7534 macro_build (&expr1, "sb", "t,o(b)", treg, BFD_RELOC_LO16, AT);
7535 macro_build (NULL, "srl", "d,w,<", treg, treg, 8);
252b5132
RH
7536 if (! target_big_endian)
7537 expr1.X_add_number = 1;
7538 else
7539 expr1.X_add_number = 0;
67c0d1eb 7540 macro_build (&expr1, "sb", "t,o(b)", treg, BFD_RELOC_LO16, AT);
252b5132
RH
7541 if (! target_big_endian)
7542 expr1.X_add_number = 0;
7543 else
7544 expr1.X_add_number = 1;
67c0d1eb
RS
7545 macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
7546 macro_build (NULL, "sll", "d,w,<", treg, treg, 8);
7547 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
252b5132
RH
7548 break;
7549
7550 default:
7551 /* FIXME: Check if this is one of the itbl macros, since they
bdaaa2e1 7552 are added dynamically. */
252b5132
RH
7553 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7554 break;
7555 }
8fc2e39e
TS
7556 if (mips_opts.noat && used_at)
7557 as_bad (_("Macro used $at after \".set noat\""));
252b5132
RH
7558}
7559
7560/* Implement macros in mips16 mode. */
7561
7562static void
17a2f251 7563mips16_macro (struct mips_cl_insn *ip)
252b5132
RH
7564{
7565 int mask;
7566 int xreg, yreg, zreg, tmp;
252b5132
RH
7567 expressionS expr1;
7568 int dbl;
7569 const char *s, *s2, *s3;
7570
7571 mask = ip->insn_mo->mask;
7572
bf12938e
RS
7573 xreg = MIPS16_EXTRACT_OPERAND (RX, *ip);
7574 yreg = MIPS16_EXTRACT_OPERAND (RY, *ip);
7575 zreg = MIPS16_EXTRACT_OPERAND (RZ, *ip);
252b5132 7576
252b5132
RH
7577 expr1.X_op = O_constant;
7578 expr1.X_op_symbol = NULL;
7579 expr1.X_add_symbol = NULL;
7580 expr1.X_add_number = 1;
7581
7582 dbl = 0;
7583
7584 switch (mask)
7585 {
7586 default:
7587 internalError ();
7588
7589 case M_DDIV_3:
7590 dbl = 1;
7591 case M_DIV_3:
7592 s = "mflo";
7593 goto do_div3;
7594 case M_DREM_3:
7595 dbl = 1;
7596 case M_REM_3:
7597 s = "mfhi";
7598 do_div3:
7d10b47d 7599 start_noreorder ();
67c0d1eb 7600 macro_build (NULL, dbl ? "ddiv" : "div", "0,x,y", xreg, yreg);
252b5132 7601 expr1.X_add_number = 2;
67c0d1eb
RS
7602 macro_build (&expr1, "bnez", "x,p", yreg);
7603 macro_build (NULL, "break", "6", 7);
bdaaa2e1 7604
252b5132
RH
7605 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7606 since that causes an overflow. We should do that as well,
7607 but I don't see how to do the comparisons without a temporary
7608 register. */
7d10b47d 7609 end_noreorder ();
67c0d1eb 7610 macro_build (NULL, s, "x", zreg);
252b5132
RH
7611 break;
7612
7613 case M_DIVU_3:
7614 s = "divu";
7615 s2 = "mflo";
7616 goto do_divu3;
7617 case M_REMU_3:
7618 s = "divu";
7619 s2 = "mfhi";
7620 goto do_divu3;
7621 case M_DDIVU_3:
7622 s = "ddivu";
7623 s2 = "mflo";
7624 goto do_divu3;
7625 case M_DREMU_3:
7626 s = "ddivu";
7627 s2 = "mfhi";
7628 do_divu3:
7d10b47d 7629 start_noreorder ();
67c0d1eb 7630 macro_build (NULL, s, "0,x,y", xreg, yreg);
252b5132 7631 expr1.X_add_number = 2;
67c0d1eb
RS
7632 macro_build (&expr1, "bnez", "x,p", yreg);
7633 macro_build (NULL, "break", "6", 7);
7d10b47d 7634 end_noreorder ();
67c0d1eb 7635 macro_build (NULL, s2, "x", zreg);
252b5132
RH
7636 break;
7637
7638 case M_DMUL:
7639 dbl = 1;
7640 case M_MUL:
67c0d1eb
RS
7641 macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
7642 macro_build (NULL, "mflo", "x", zreg);
8fc2e39e 7643 break;
252b5132
RH
7644
7645 case M_DSUBU_I:
7646 dbl = 1;
7647 goto do_subu;
7648 case M_SUBU_I:
7649 do_subu:
7650 if (imm_expr.X_op != O_constant)
7651 as_bad (_("Unsupported large constant"));
7652 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 7653 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
252b5132
RH
7654 break;
7655
7656 case M_SUBU_I_2:
7657 if (imm_expr.X_op != O_constant)
7658 as_bad (_("Unsupported large constant"));
7659 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 7660 macro_build (&imm_expr, "addiu", "x,k", xreg);
252b5132
RH
7661 break;
7662
7663 case M_DSUBU_I_2:
7664 if (imm_expr.X_op != O_constant)
7665 as_bad (_("Unsupported large constant"));
7666 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 7667 macro_build (&imm_expr, "daddiu", "y,j", yreg);
252b5132
RH
7668 break;
7669
7670 case M_BEQ:
7671 s = "cmp";
7672 s2 = "bteqz";
7673 goto do_branch;
7674 case M_BNE:
7675 s = "cmp";
7676 s2 = "btnez";
7677 goto do_branch;
7678 case M_BLT:
7679 s = "slt";
7680 s2 = "btnez";
7681 goto do_branch;
7682 case M_BLTU:
7683 s = "sltu";
7684 s2 = "btnez";
7685 goto do_branch;
7686 case M_BLE:
7687 s = "slt";
7688 s2 = "bteqz";
7689 goto do_reverse_branch;
7690 case M_BLEU:
7691 s = "sltu";
7692 s2 = "bteqz";
7693 goto do_reverse_branch;
7694 case M_BGE:
7695 s = "slt";
7696 s2 = "bteqz";
7697 goto do_branch;
7698 case M_BGEU:
7699 s = "sltu";
7700 s2 = "bteqz";
7701 goto do_branch;
7702 case M_BGT:
7703 s = "slt";
7704 s2 = "btnez";
7705 goto do_reverse_branch;
7706 case M_BGTU:
7707 s = "sltu";
7708 s2 = "btnez";
7709
7710 do_reverse_branch:
7711 tmp = xreg;
7712 xreg = yreg;
7713 yreg = tmp;
7714
7715 do_branch:
67c0d1eb
RS
7716 macro_build (NULL, s, "x,y", xreg, yreg);
7717 macro_build (&offset_expr, s2, "p");
252b5132
RH
7718 break;
7719
7720 case M_BEQ_I:
7721 s = "cmpi";
7722 s2 = "bteqz";
7723 s3 = "x,U";
7724 goto do_branch_i;
7725 case M_BNE_I:
7726 s = "cmpi";
7727 s2 = "btnez";
7728 s3 = "x,U";
7729 goto do_branch_i;
7730 case M_BLT_I:
7731 s = "slti";
7732 s2 = "btnez";
7733 s3 = "x,8";
7734 goto do_branch_i;
7735 case M_BLTU_I:
7736 s = "sltiu";
7737 s2 = "btnez";
7738 s3 = "x,8";
7739 goto do_branch_i;
7740 case M_BLE_I:
7741 s = "slti";
7742 s2 = "btnez";
7743 s3 = "x,8";
7744 goto do_addone_branch_i;
7745 case M_BLEU_I:
7746 s = "sltiu";
7747 s2 = "btnez";
7748 s3 = "x,8";
7749 goto do_addone_branch_i;
7750 case M_BGE_I:
7751 s = "slti";
7752 s2 = "bteqz";
7753 s3 = "x,8";
7754 goto do_branch_i;
7755 case M_BGEU_I:
7756 s = "sltiu";
7757 s2 = "bteqz";
7758 s3 = "x,8";
7759 goto do_branch_i;
7760 case M_BGT_I:
7761 s = "slti";
7762 s2 = "bteqz";
7763 s3 = "x,8";
7764 goto do_addone_branch_i;
7765 case M_BGTU_I:
7766 s = "sltiu";
7767 s2 = "bteqz";
7768 s3 = "x,8";
7769
7770 do_addone_branch_i:
7771 if (imm_expr.X_op != O_constant)
7772 as_bad (_("Unsupported large constant"));
7773 ++imm_expr.X_add_number;
7774
7775 do_branch_i:
67c0d1eb
RS
7776 macro_build (&imm_expr, s, s3, xreg);
7777 macro_build (&offset_expr, s2, "p");
252b5132
RH
7778 break;
7779
7780 case M_ABS:
7781 expr1.X_add_number = 0;
67c0d1eb 7782 macro_build (&expr1, "slti", "x,8", yreg);
252b5132 7783 if (xreg != yreg)
67c0d1eb 7784 move_register (xreg, yreg);
252b5132 7785 expr1.X_add_number = 2;
67c0d1eb
RS
7786 macro_build (&expr1, "bteqz", "p");
7787 macro_build (NULL, "neg", "x,w", xreg, xreg);
252b5132
RH
7788 }
7789}
7790
7791/* For consistency checking, verify that all bits are specified either
7792 by the match/mask part of the instruction definition, or by the
7793 operand list. */
7794static int
17a2f251 7795validate_mips_insn (const struct mips_opcode *opc)
252b5132
RH
7796{
7797 const char *p = opc->args;
7798 char c;
7799 unsigned long used_bits = opc->mask;
7800
7801 if ((used_bits & opc->match) != opc->match)
7802 {
7803 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
7804 opc->name, opc->args);
7805 return 0;
7806 }
7807#define USE_BITS(mask,shift) (used_bits |= ((mask) << (shift)))
7808 while (*p)
7809 switch (c = *p++)
7810 {
7811 case ',': break;
7812 case '(': break;
7813 case ')': break;
af7ee8bf
CD
7814 case '+':
7815 switch (c = *p++)
7816 {
9bcd4f99
TS
7817 case '1': USE_BITS (OP_MASK_UDI1, OP_SH_UDI1); break;
7818 case '2': USE_BITS (OP_MASK_UDI2, OP_SH_UDI2); break;
7819 case '3': USE_BITS (OP_MASK_UDI3, OP_SH_UDI3); break;
7820 case '4': USE_BITS (OP_MASK_UDI4, OP_SH_UDI4); break;
af7ee8bf
CD
7821 case 'A': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7822 case 'B': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
7823 case 'C': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
bbcc0807
CD
7824 case 'D': USE_BITS (OP_MASK_RD, OP_SH_RD);
7825 USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
5f74bc13
CD
7826 case 'E': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7827 case 'F': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
7828 case 'G': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
7829 case 'H': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
7830 case 'I': break;
ef2e4d86
CF
7831 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7832 case 'T': USE_BITS (OP_MASK_RT, OP_SH_RT);
7833 USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
af7ee8bf
CD
7834 default:
7835 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
7836 c, opc->name, opc->args);
7837 return 0;
7838 }
7839 break;
252b5132
RH
7840 case '<': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7841 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7842 case 'A': break;
4372b673 7843 case 'B': USE_BITS (OP_MASK_CODE20, OP_SH_CODE20); break;
252b5132
RH
7844 case 'C': USE_BITS (OP_MASK_COPZ, OP_SH_COPZ); break;
7845 case 'D': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
7846 case 'E': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7847 case 'F': break;
7848 case 'G': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
156c2f8b 7849 case 'H': USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
252b5132 7850 case 'I': break;
e972090a 7851 case 'J': USE_BITS (OP_MASK_CODE19, OP_SH_CODE19); break;
af7ee8bf 7852 case 'K': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
252b5132
RH
7853 case 'L': break;
7854 case 'M': USE_BITS (OP_MASK_CCC, OP_SH_CCC); break;
7855 case 'N': USE_BITS (OP_MASK_BCC, OP_SH_BCC); break;
deec1734
CD
7856 case 'O': USE_BITS (OP_MASK_ALN, OP_SH_ALN); break;
7857 case 'Q': USE_BITS (OP_MASK_VSEL, OP_SH_VSEL);
7858 USE_BITS (OP_MASK_FT, OP_SH_FT); break;
252b5132
RH
7859 case 'R': USE_BITS (OP_MASK_FR, OP_SH_FR); break;
7860 case 'S': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7861 case 'T': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7862 case 'V': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7863 case 'W': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
deec1734
CD
7864 case 'X': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
7865 case 'Y': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7866 case 'Z': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
252b5132
RH
7867 case 'a': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break;
7868 case 'b': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7869 case 'c': USE_BITS (OP_MASK_CODE, OP_SH_CODE); break;
7870 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
7871 case 'f': break;
7872 case 'h': USE_BITS (OP_MASK_PREFX, OP_SH_PREFX); break;
7873 case 'i': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
7874 case 'j': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7875 case 'k': USE_BITS (OP_MASK_CACHE, OP_SH_CACHE); break;
7876 case 'l': break;
7877 case 'o': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7878 case 'p': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7879 case 'q': USE_BITS (OP_MASK_CODE2, OP_SH_CODE2); break;
7880 case 'r': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7881 case 's': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7882 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7883 case 'u': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
7884 case 'v': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7885 case 'w': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7886 case 'x': break;
7887 case 'z': break;
7888 case 'P': USE_BITS (OP_MASK_PERFREG, OP_SH_PERFREG); break;
4372b673
NC
7889 case 'U': USE_BITS (OP_MASK_RD, OP_SH_RD);
7890 USE_BITS (OP_MASK_RT, OP_SH_RT); break;
60b63b72
RS
7891 case 'e': USE_BITS (OP_MASK_VECBYTE, OP_SH_VECBYTE); break;
7892 case '%': USE_BITS (OP_MASK_VECALIGN, OP_SH_VECALIGN); break;
7893 case '[': break;
7894 case ']': break;
74cd071d
CF
7895 case '3': USE_BITS (OP_MASK_SA3, OP_SH_SA3); break;
7896 case '4': USE_BITS (OP_MASK_SA4, OP_SH_SA4); break;
7897 case '5': USE_BITS (OP_MASK_IMM8, OP_SH_IMM8); break;
7898 case '6': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7899 case '7': USE_BITS (OP_MASK_DSPACC, OP_SH_DSPACC); break;
7900 case '8': USE_BITS (OP_MASK_WRDSP, OP_SH_WRDSP); break;
7901 case '9': USE_BITS (OP_MASK_DSPACC_S, OP_SH_DSPACC_S);break;
7902 case '0': USE_BITS (OP_MASK_DSPSFT, OP_SH_DSPSFT); break;
7903 case '\'': USE_BITS (OP_MASK_RDDSP, OP_SH_RDDSP); break;
7904 case ':': USE_BITS (OP_MASK_DSPSFT_7, OP_SH_DSPSFT_7);break;
7905 case '@': USE_BITS (OP_MASK_IMM10, OP_SH_IMM10); break;
ef2e4d86
CF
7906 case '!': USE_BITS (OP_MASK_MT_U, OP_SH_MT_U); break;
7907 case '$': USE_BITS (OP_MASK_MT_H, OP_SH_MT_H); break;
7908 case '*': USE_BITS (OP_MASK_MTACC_T, OP_SH_MTACC_T); break;
7909 case '&': USE_BITS (OP_MASK_MTACC_D, OP_SH_MTACC_D); break;
7910 case 'g': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
252b5132
RH
7911 default:
7912 as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
7913 c, opc->name, opc->args);
7914 return 0;
7915 }
7916#undef USE_BITS
7917 if (used_bits != 0xffffffff)
7918 {
7919 as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
7920 ~used_bits & 0xffffffff, opc->name, opc->args);
7921 return 0;
7922 }
7923 return 1;
7924}
7925
9bcd4f99
TS
7926/* UDI immediates. */
7927struct mips_immed {
7928 char type;
7929 unsigned int shift;
7930 unsigned long mask;
7931 const char * desc;
7932};
7933
7934static const struct mips_immed mips_immed[] = {
7935 { '1', OP_SH_UDI1, OP_MASK_UDI1, 0},
7936 { '2', OP_SH_UDI2, OP_MASK_UDI2, 0},
7937 { '3', OP_SH_UDI3, OP_MASK_UDI3, 0},
7938 { '4', OP_SH_UDI4, OP_MASK_UDI4, 0},
7939 { 0,0,0,0 }
7940};
7941
252b5132
RH
7942/* This routine assembles an instruction into its binary format. As a
7943 side effect, it sets one of the global variables imm_reloc or
7944 offset_reloc to the type of relocation to do if one of the operands
7945 is an address expression. */
7946
7947static void
17a2f251 7948mips_ip (char *str, struct mips_cl_insn *ip)
252b5132
RH
7949{
7950 char *s;
7951 const char *args;
43841e91 7952 char c = 0;
252b5132
RH
7953 struct mips_opcode *insn;
7954 char *argsStart;
7955 unsigned int regno;
7956 unsigned int lastregno = 0;
af7ee8bf 7957 unsigned int lastpos = 0;
071742cf 7958 unsigned int limlo, limhi;
252b5132
RH
7959 char *s_reset;
7960 char save_c = 0;
74cd071d 7961 offsetT min_range, max_range;
252b5132
RH
7962
7963 insn_error = NULL;
7964
7965 /* If the instruction contains a '.', we first try to match an instruction
7966 including the '.'. Then we try again without the '.'. */
7967 insn = NULL;
3882b010 7968 for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
252b5132
RH
7969 continue;
7970
7971 /* If we stopped on whitespace, then replace the whitespace with null for
7972 the call to hash_find. Save the character we replaced just in case we
7973 have to re-parse the instruction. */
3882b010 7974 if (ISSPACE (*s))
252b5132
RH
7975 {
7976 save_c = *s;
7977 *s++ = '\0';
7978 }
bdaaa2e1 7979
252b5132
RH
7980 insn = (struct mips_opcode *) hash_find (op_hash, str);
7981
7982 /* If we didn't find the instruction in the opcode table, try again, but
7983 this time with just the instruction up to, but not including the
7984 first '.'. */
7985 if (insn == NULL)
7986 {
bdaaa2e1 7987 /* Restore the character we overwrite above (if any). */
252b5132
RH
7988 if (save_c)
7989 *(--s) = save_c;
7990
7991 /* Scan up to the first '.' or whitespace. */
3882b010
L
7992 for (s = str;
7993 *s != '\0' && *s != '.' && !ISSPACE (*s);
7994 ++s)
252b5132
RH
7995 continue;
7996
7997 /* If we did not find a '.', then we can quit now. */
7998 if (*s != '.')
7999 {
8000 insn_error = "unrecognized opcode";
8001 return;
8002 }
8003
8004 /* Lookup the instruction in the hash table. */
8005 *s++ = '\0';
8006 if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
8007 {
8008 insn_error = "unrecognized opcode";
8009 return;
8010 }
252b5132
RH
8011 }
8012
8013 argsStart = s;
8014 for (;;)
8015 {
b34976b6 8016 bfd_boolean ok;
252b5132
RH
8017
8018 assert (strcmp (insn->name, str) == 0);
8019
1f25f5d3
CD
8020 if (OPCODE_IS_MEMBER (insn,
8021 (mips_opts.isa
3396de36 8022 | (file_ase_mips16 ? INSN_MIPS16 : 0)
deec1734 8023 | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
74cd071d 8024 | (mips_opts.ase_dsp ? INSN_DSP : 0)
ef2e4d86 8025 | (mips_opts.ase_mt ? INSN_MT : 0)
98d3f06f 8026 | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
fef14a42 8027 mips_opts.arch))
b34976b6 8028 ok = TRUE;
bdaaa2e1 8029 else
b34976b6 8030 ok = FALSE;
bdaaa2e1 8031
252b5132
RH
8032 if (insn->pinfo != INSN_MACRO)
8033 {
fef14a42 8034 if (mips_opts.arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
b34976b6 8035 ok = FALSE;
252b5132
RH
8036 }
8037
8038 if (! ok)
8039 {
8040 if (insn + 1 < &mips_opcodes[NUMOPCODES]
8041 && strcmp (insn->name, insn[1].name) == 0)
8042 {
8043 ++insn;
8044 continue;
8045 }
252b5132 8046 else
beae10d5 8047 {
268f6bed
L
8048 if (!insn_error)
8049 {
8050 static char buf[100];
fef14a42
TS
8051 sprintf (buf,
8052 _("opcode not supported on this processor: %s (%s)"),
8053 mips_cpu_info_from_arch (mips_opts.arch)->name,
8054 mips_cpu_info_from_isa (mips_opts.isa)->name);
268f6bed
L
8055 insn_error = buf;
8056 }
8057 if (save_c)
8058 *(--s) = save_c;
2bd7f1f3 8059 return;
252b5132 8060 }
252b5132
RH
8061 }
8062
1e915849 8063 create_insn (ip, insn);
268f6bed 8064 insn_error = NULL;
252b5132
RH
8065 for (args = insn->args;; ++args)
8066 {
deec1734
CD
8067 int is_mdmx;
8068
ad8d3bb3 8069 s += strspn (s, " \t");
deec1734 8070 is_mdmx = 0;
252b5132
RH
8071 switch (*args)
8072 {
8073 case '\0': /* end of args */
8074 if (*s == '\0')
8075 return;
8076 break;
8077
74cd071d
CF
8078 case '3': /* dsp 3-bit unsigned immediate in bit 21 */
8079 my_getExpression (&imm_expr, s);
8080 check_absolute_expr (ip, &imm_expr);
8081 if (imm_expr.X_add_number & ~OP_MASK_SA3)
8082 {
8083 as_warn (_("DSP immediate not in range 0..%d (%lu)"),
8084 OP_MASK_SA3, (unsigned long) imm_expr.X_add_number);
8085 imm_expr.X_add_number &= OP_MASK_SA3;
8086 }
8087 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SA3;
8088 imm_expr.X_op = O_absent;
8089 s = expr_end;
8090 continue;
8091
8092 case '4': /* dsp 4-bit unsigned immediate in bit 21 */
8093 my_getExpression (&imm_expr, s);
8094 check_absolute_expr (ip, &imm_expr);
8095 if (imm_expr.X_add_number & ~OP_MASK_SA4)
8096 {
8097 as_warn (_("DSP immediate not in range 0..%d (%lu)"),
8098 OP_MASK_SA4, (unsigned long) imm_expr.X_add_number);
8099 imm_expr.X_add_number &= OP_MASK_SA4;
8100 }
8101 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SA4;
8102 imm_expr.X_op = O_absent;
8103 s = expr_end;
8104 continue;
8105
8106 case '5': /* dsp 8-bit unsigned immediate in bit 16 */
8107 my_getExpression (&imm_expr, s);
8108 check_absolute_expr (ip, &imm_expr);
8109 if (imm_expr.X_add_number & ~OP_MASK_IMM8)
8110 {
8111 as_warn (_("DSP immediate not in range 0..%d (%lu)"),
8112 OP_MASK_IMM8, (unsigned long) imm_expr.X_add_number);
8113 imm_expr.X_add_number &= OP_MASK_IMM8;
8114 }
8115 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_IMM8;
8116 imm_expr.X_op = O_absent;
8117 s = expr_end;
8118 continue;
8119
8120 case '6': /* dsp 5-bit unsigned immediate in bit 21 */
8121 my_getExpression (&imm_expr, s);
8122 check_absolute_expr (ip, &imm_expr);
8123 if (imm_expr.X_add_number & ~OP_MASK_RS)
8124 {
8125 as_warn (_("DSP immediate not in range 0..%d (%lu)"),
8126 OP_MASK_RS, (unsigned long) imm_expr.X_add_number);
8127 imm_expr.X_add_number &= OP_MASK_RS;
8128 }
8129 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_RS;
8130 imm_expr.X_op = O_absent;
8131 s = expr_end;
8132 continue;
8133
8134 case '7': /* four dsp accumulators in bits 11,12 */
8135 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
8136 s[3] >= '0' && s[3] <= '3')
8137 {
8138 regno = s[3] - '0';
8139 s += 4;
8140 ip->insn_opcode |= regno << OP_SH_DSPACC;
8141 continue;
8142 }
8143 else
8144 as_bad (_("Invalid dsp acc register"));
8145 break;
8146
8147 case '8': /* dsp 6-bit unsigned immediate in bit 11 */
8148 my_getExpression (&imm_expr, s);
8149 check_absolute_expr (ip, &imm_expr);
8150 if (imm_expr.X_add_number & ~OP_MASK_WRDSP)
8151 {
8152 as_warn (_("DSP immediate not in range 0..%d (%lu)"),
8153 OP_MASK_WRDSP,
8154 (unsigned long) imm_expr.X_add_number);
8155 imm_expr.X_add_number &= OP_MASK_WRDSP;
8156 }
8157 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_WRDSP;
8158 imm_expr.X_op = O_absent;
8159 s = expr_end;
8160 continue;
8161
8162 case '9': /* four dsp accumulators in bits 21,22 */
8163 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
8164 s[3] >= '0' && s[3] <= '3')
8165 {
8166 regno = s[3] - '0';
8167 s += 4;
8168 ip->insn_opcode |= regno << OP_SH_DSPACC_S;
8169 continue;
8170 }
8171 else
8172 as_bad (_("Invalid dsp acc register"));
8173 break;
8174
8175 case '0': /* dsp 6-bit signed immediate in bit 20 */
8176 my_getExpression (&imm_expr, s);
8177 check_absolute_expr (ip, &imm_expr);
8178 min_range = -((OP_MASK_DSPSFT + 1) >> 1);
8179 max_range = ((OP_MASK_DSPSFT + 1) >> 1) - 1;
8180 if (imm_expr.X_add_number < min_range ||
8181 imm_expr.X_add_number > max_range)
8182 {
8183 as_warn (_("DSP immediate not in range %ld..%ld (%ld)"),
8184 (long) min_range, (long) max_range,
8185 (long) imm_expr.X_add_number);
8186 }
8187 imm_expr.X_add_number &= OP_MASK_DSPSFT;
8188 ip->insn_opcode |= ((unsigned long) imm_expr.X_add_number
8189 << OP_SH_DSPSFT);
8190 imm_expr.X_op = O_absent;
8191 s = expr_end;
8192 continue;
8193
8194 case '\'': /* dsp 6-bit unsigned immediate in bit 16 */
8195 my_getExpression (&imm_expr, s);
8196 check_absolute_expr (ip, &imm_expr);
8197 if (imm_expr.X_add_number & ~OP_MASK_RDDSP)
8198 {
8199 as_warn (_("DSP immediate not in range 0..%d (%lu)"),
8200 OP_MASK_RDDSP,
8201 (unsigned long) imm_expr.X_add_number);
8202 imm_expr.X_add_number &= OP_MASK_RDDSP;
8203 }
8204 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_RDDSP;
8205 imm_expr.X_op = O_absent;
8206 s = expr_end;
8207 continue;
8208
8209 case ':': /* dsp 7-bit signed immediate in bit 19 */
8210 my_getExpression (&imm_expr, s);
8211 check_absolute_expr (ip, &imm_expr);
8212 min_range = -((OP_MASK_DSPSFT_7 + 1) >> 1);
8213 max_range = ((OP_MASK_DSPSFT_7 + 1) >> 1) - 1;
8214 if (imm_expr.X_add_number < min_range ||
8215 imm_expr.X_add_number > max_range)
8216 {
8217 as_warn (_("DSP immediate not in range %ld..%ld (%ld)"),
8218 (long) min_range, (long) max_range,
8219 (long) imm_expr.X_add_number);
8220 }
8221 imm_expr.X_add_number &= OP_MASK_DSPSFT_7;
8222 ip->insn_opcode |= ((unsigned long) imm_expr.X_add_number
8223 << OP_SH_DSPSFT_7);
8224 imm_expr.X_op = O_absent;
8225 s = expr_end;
8226 continue;
8227
8228 case '@': /* dsp 10-bit signed immediate in bit 16 */
8229 my_getExpression (&imm_expr, s);
8230 check_absolute_expr (ip, &imm_expr);
8231 min_range = -((OP_MASK_IMM10 + 1) >> 1);
8232 max_range = ((OP_MASK_IMM10 + 1) >> 1) - 1;
8233 if (imm_expr.X_add_number < min_range ||
8234 imm_expr.X_add_number > max_range)
8235 {
8236 as_warn (_("DSP immediate not in range %ld..%ld (%ld)"),
8237 (long) min_range, (long) max_range,
8238 (long) imm_expr.X_add_number);
8239 }
8240 imm_expr.X_add_number &= OP_MASK_IMM10;
8241 ip->insn_opcode |= ((unsigned long) imm_expr.X_add_number
8242 << OP_SH_IMM10);
8243 imm_expr.X_op = O_absent;
8244 s = expr_end;
8245 continue;
8246
ef2e4d86
CF
8247 case '!': /* mt 1-bit unsigned immediate in bit 5 */
8248 my_getExpression (&imm_expr, s);
8249 check_absolute_expr (ip, &imm_expr);
8250 if (imm_expr.X_add_number & ~OP_MASK_MT_U)
8251 {
8252 as_warn (_("MT immediate not in range 0..%d (%lu)"),
8253 OP_MASK_MT_U, (unsigned long) imm_expr.X_add_number);
8254 imm_expr.X_add_number &= OP_MASK_MT_U;
8255 }
8256 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_MT_U;
8257 imm_expr.X_op = O_absent;
8258 s = expr_end;
8259 continue;
8260
8261 case '$': /* mt 1-bit unsigned immediate in bit 4 */
8262 my_getExpression (&imm_expr, s);
8263 check_absolute_expr (ip, &imm_expr);
8264 if (imm_expr.X_add_number & ~OP_MASK_MT_H)
8265 {
8266 as_warn (_("MT immediate not in range 0..%d (%lu)"),
8267 OP_MASK_MT_H, (unsigned long) imm_expr.X_add_number);
8268 imm_expr.X_add_number &= OP_MASK_MT_H;
8269 }
8270 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_MT_H;
8271 imm_expr.X_op = O_absent;
8272 s = expr_end;
8273 continue;
8274
8275 case '*': /* four dsp accumulators in bits 18,19 */
8276 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
8277 s[3] >= '0' && s[3] <= '3')
8278 {
8279 regno = s[3] - '0';
8280 s += 4;
8281 ip->insn_opcode |= regno << OP_SH_MTACC_T;
8282 continue;
8283 }
8284 else
8285 as_bad (_("Invalid dsp/smartmips acc register"));
8286 break;
8287
8288 case '&': /* four dsp accumulators in bits 13,14 */
8289 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
8290 s[3] >= '0' && s[3] <= '3')
8291 {
8292 regno = s[3] - '0';
8293 s += 4;
8294 ip->insn_opcode |= regno << OP_SH_MTACC_D;
8295 continue;
8296 }
8297 else
8298 as_bad (_("Invalid dsp/smartmips acc register"));
8299 break;
8300
252b5132
RH
8301 case ',':
8302 if (*s++ == *args)
8303 continue;
8304 s--;
8305 switch (*++args)
8306 {
8307 case 'r':
8308 case 'v':
bf12938e 8309 INSERT_OPERAND (RS, *ip, lastregno);
252b5132
RH
8310 continue;
8311
8312 case 'w':
bf12938e 8313 INSERT_OPERAND (RT, *ip, lastregno);
38487616
TS
8314 continue;
8315
252b5132 8316 case 'W':
bf12938e 8317 INSERT_OPERAND (FT, *ip, lastregno);
252b5132
RH
8318 continue;
8319
8320 case 'V':
bf12938e 8321 INSERT_OPERAND (FS, *ip, lastregno);
252b5132
RH
8322 continue;
8323 }
8324 break;
8325
8326 case '(':
8327 /* Handle optional base register.
8328 Either the base register is omitted or
bdaaa2e1 8329 we must have a left paren. */
252b5132
RH
8330 /* This is dependent on the next operand specifier
8331 is a base register specification. */
8332 assert (args[1] == 'b' || args[1] == '5'
8333 || args[1] == '-' || args[1] == '4');
8334 if (*s == '\0')
8335 return;
8336
8337 case ')': /* these must match exactly */
60b63b72
RS
8338 case '[':
8339 case ']':
252b5132
RH
8340 if (*s++ == *args)
8341 continue;
8342 break;
8343
af7ee8bf
CD
8344 case '+': /* Opcode extension character. */
8345 switch (*++args)
8346 {
9bcd4f99
TS
8347 case '1': /* UDI immediates. */
8348 case '2':
8349 case '3':
8350 case '4':
8351 {
8352 const struct mips_immed *imm = mips_immed;
8353
8354 while (imm->type && imm->type != *args)
8355 ++imm;
8356 if (! imm->type)
8357 internalError ();
8358 my_getExpression (&imm_expr, s);
8359 check_absolute_expr (ip, &imm_expr);
8360 if ((unsigned long) imm_expr.X_add_number & ~imm->mask)
8361 {
8362 as_warn (_("Illegal %s number (%lu, 0x%lx)"),
8363 imm->desc ? imm->desc : ip->insn_mo->name,
8364 (unsigned long) imm_expr.X_add_number,
8365 (unsigned long) imm_expr.X_add_number);
8366 imm_expr.X_add_number &= imm->mask;
8367 }
8368 ip->insn_opcode |= ((unsigned long) imm_expr.X_add_number
8369 << imm->shift);
8370 imm_expr.X_op = O_absent;
8371 s = expr_end;
8372 }
8373 continue;
8374
071742cf
CD
8375 case 'A': /* ins/ext position, becomes LSB. */
8376 limlo = 0;
8377 limhi = 31;
5f74bc13
CD
8378 goto do_lsb;
8379 case 'E':
8380 limlo = 32;
8381 limhi = 63;
8382 goto do_lsb;
8383do_lsb:
071742cf
CD
8384 my_getExpression (&imm_expr, s);
8385 check_absolute_expr (ip, &imm_expr);
8386 if ((unsigned long) imm_expr.X_add_number < limlo
8387 || (unsigned long) imm_expr.X_add_number > limhi)
8388 {
8389 as_bad (_("Improper position (%lu)"),
8390 (unsigned long) imm_expr.X_add_number);
8391 imm_expr.X_add_number = limlo;
8392 }
8393 lastpos = imm_expr.X_add_number;
bf12938e 8394 INSERT_OPERAND (SHAMT, *ip, imm_expr.X_add_number);
071742cf
CD
8395 imm_expr.X_op = O_absent;
8396 s = expr_end;
8397 continue;
8398
8399 case 'B': /* ins size, becomes MSB. */
8400 limlo = 1;
8401 limhi = 32;
5f74bc13
CD
8402 goto do_msb;
8403 case 'F':
8404 limlo = 33;
8405 limhi = 64;
8406 goto do_msb;
8407do_msb:
071742cf
CD
8408 my_getExpression (&imm_expr, s);
8409 check_absolute_expr (ip, &imm_expr);
8410 /* Check for negative input so that small negative numbers
8411 will not succeed incorrectly. The checks against
8412 (pos+size) transitively check "size" itself,
8413 assuming that "pos" is reasonable. */
8414 if ((long) imm_expr.X_add_number < 0
8415 || ((unsigned long) imm_expr.X_add_number
8416 + lastpos) < limlo
8417 || ((unsigned long) imm_expr.X_add_number
8418 + lastpos) > limhi)
8419 {
8420 as_bad (_("Improper insert size (%lu, position %lu)"),
8421 (unsigned long) imm_expr.X_add_number,
8422 (unsigned long) lastpos);
8423 imm_expr.X_add_number = limlo - lastpos;
8424 }
bf12938e
RS
8425 INSERT_OPERAND (INSMSB, *ip,
8426 lastpos + imm_expr.X_add_number - 1);
071742cf
CD
8427 imm_expr.X_op = O_absent;
8428 s = expr_end;
8429 continue;
8430
8431 case 'C': /* ext size, becomes MSBD. */
8432 limlo = 1;
8433 limhi = 32;
5f74bc13
CD
8434 goto do_msbd;
8435 case 'G':
8436 limlo = 33;
8437 limhi = 64;
8438 goto do_msbd;
8439 case 'H':
8440 limlo = 33;
8441 limhi = 64;
8442 goto do_msbd;
8443do_msbd:
071742cf
CD
8444 my_getExpression (&imm_expr, s);
8445 check_absolute_expr (ip, &imm_expr);
8446 /* Check for negative input so that small negative numbers
8447 will not succeed incorrectly. The checks against
8448 (pos+size) transitively check "size" itself,
8449 assuming that "pos" is reasonable. */
8450 if ((long) imm_expr.X_add_number < 0
8451 || ((unsigned long) imm_expr.X_add_number
8452 + lastpos) < limlo
8453 || ((unsigned long) imm_expr.X_add_number
8454 + lastpos) > limhi)
8455 {
8456 as_bad (_("Improper extract size (%lu, position %lu)"),
8457 (unsigned long) imm_expr.X_add_number,
8458 (unsigned long) lastpos);
8459 imm_expr.X_add_number = limlo - lastpos;
8460 }
bf12938e 8461 INSERT_OPERAND (EXTMSBD, *ip, imm_expr.X_add_number - 1);
071742cf
CD
8462 imm_expr.X_op = O_absent;
8463 s = expr_end;
8464 continue;
af7ee8bf 8465
bbcc0807
CD
8466 case 'D':
8467 /* +D is for disassembly only; never match. */
8468 break;
8469
5f74bc13
CD
8470 case 'I':
8471 /* "+I" is like "I", except that imm2_expr is used. */
8472 my_getExpression (&imm2_expr, s);
8473 if (imm2_expr.X_op != O_big
8474 && imm2_expr.X_op != O_constant)
8475 insn_error = _("absolute expression required");
9ee2a2d4
MR
8476 if (HAVE_32BIT_GPRS)
8477 normalize_constant_expr (&imm2_expr);
5f74bc13
CD
8478 s = expr_end;
8479 continue;
8480
ef2e4d86
CF
8481 case 'T': /* Coprocessor register */
8482 /* +T is for disassembly only; never match. */
8483 break;
8484
8485 case 't': /* Coprocessor register number */
8486 if (s[0] == '$' && ISDIGIT (s[1]))
8487 {
8488 ++s;
8489 regno = 0;
8490 do
8491 {
8492 regno *= 10;
8493 regno += *s - '0';
8494 ++s;
8495 }
8496 while (ISDIGIT (*s));
8497 if (regno > 31)
8498 as_bad (_("Invalid register number (%d)"), regno);
8499 else
8500 {
8501 ip->insn_opcode |= regno << OP_SH_RT;
8502 continue;
8503 }
8504 }
8505 else
8506 as_bad (_("Invalid coprocessor 0 register number"));
8507 break;
8508
af7ee8bf
CD
8509 default:
8510 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8511 *args, insn->name, insn->args);
8512 /* Further processing is fruitless. */
8513 return;
8514 }
8515 break;
8516
252b5132
RH
8517 case '<': /* must be at least one digit */
8518 /*
8519 * According to the manual, if the shift amount is greater
b6ff326e
KH
8520 * than 31 or less than 0, then the shift amount should be
8521 * mod 32. In reality the mips assembler issues an error.
252b5132
RH
8522 * We issue a warning and mask out all but the low 5 bits.
8523 */
8524 my_getExpression (&imm_expr, s);
8525 check_absolute_expr (ip, &imm_expr);
8526 if ((unsigned long) imm_expr.X_add_number > 31)
bf12938e
RS
8527 as_warn (_("Improper shift amount (%lu)"),
8528 (unsigned long) imm_expr.X_add_number);
8529 INSERT_OPERAND (SHAMT, *ip, imm_expr.X_add_number);
252b5132
RH
8530 imm_expr.X_op = O_absent;
8531 s = expr_end;
8532 continue;
8533
8534 case '>': /* shift amount minus 32 */
8535 my_getExpression (&imm_expr, s);
8536 check_absolute_expr (ip, &imm_expr);
8537 if ((unsigned long) imm_expr.X_add_number < 32
8538 || (unsigned long) imm_expr.X_add_number > 63)
8539 break;
bf12938e 8540 INSERT_OPERAND (SHAMT, *ip, imm_expr.X_add_number - 32);
252b5132
RH
8541 imm_expr.X_op = O_absent;
8542 s = expr_end;
8543 continue;
8544
252b5132
RH
8545 case 'k': /* cache code */
8546 case 'h': /* prefx code */
8547 my_getExpression (&imm_expr, s);
8548 check_absolute_expr (ip, &imm_expr);
8549 if ((unsigned long) imm_expr.X_add_number > 31)
bf12938e
RS
8550 as_warn (_("Invalid value for `%s' (%lu)"),
8551 ip->insn_mo->name,
8552 (unsigned long) imm_expr.X_add_number);
252b5132 8553 if (*args == 'k')
bf12938e 8554 INSERT_OPERAND (CACHE, *ip, imm_expr.X_add_number);
252b5132 8555 else
bf12938e 8556 INSERT_OPERAND (PREFX, *ip, imm_expr.X_add_number);
252b5132
RH
8557 imm_expr.X_op = O_absent;
8558 s = expr_end;
8559 continue;
8560
8561 case 'c': /* break code */
8562 my_getExpression (&imm_expr, s);
8563 check_absolute_expr (ip, &imm_expr);
793b27f4 8564 if ((unsigned long) imm_expr.X_add_number > 1023)
bf12938e
RS
8565 as_warn (_("Illegal break code (%lu)"),
8566 (unsigned long) imm_expr.X_add_number);
8567 INSERT_OPERAND (CODE, *ip, imm_expr.X_add_number);
252b5132
RH
8568 imm_expr.X_op = O_absent;
8569 s = expr_end;
8570 continue;
8571
8572 case 'q': /* lower break code */
8573 my_getExpression (&imm_expr, s);
8574 check_absolute_expr (ip, &imm_expr);
793b27f4 8575 if ((unsigned long) imm_expr.X_add_number > 1023)
bf12938e
RS
8576 as_warn (_("Illegal lower break code (%lu)"),
8577 (unsigned long) imm_expr.X_add_number);
8578 INSERT_OPERAND (CODE2, *ip, imm_expr.X_add_number);
252b5132
RH
8579 imm_expr.X_op = O_absent;
8580 s = expr_end;
8581 continue;
8582
4372b673 8583 case 'B': /* 20-bit syscall/break code. */
156c2f8b 8584 my_getExpression (&imm_expr, s);
156c2f8b 8585 check_absolute_expr (ip, &imm_expr);
793b27f4
TS
8586 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
8587 as_warn (_("Illegal 20-bit code (%lu)"),
8588 (unsigned long) imm_expr.X_add_number);
bf12938e 8589 INSERT_OPERAND (CODE20, *ip, imm_expr.X_add_number);
252b5132
RH
8590 imm_expr.X_op = O_absent;
8591 s = expr_end;
8592 continue;
8593
98d3f06f 8594 case 'C': /* Coprocessor code */
beae10d5 8595 my_getExpression (&imm_expr, s);
252b5132 8596 check_absolute_expr (ip, &imm_expr);
98d3f06f 8597 if ((unsigned long) imm_expr.X_add_number >= (1 << 25))
252b5132 8598 {
793b27f4
TS
8599 as_warn (_("Coproccesor code > 25 bits (%lu)"),
8600 (unsigned long) imm_expr.X_add_number);
98d3f06f 8601 imm_expr.X_add_number &= ((1 << 25) - 1);
252b5132 8602 }
beae10d5
KH
8603 ip->insn_opcode |= imm_expr.X_add_number;
8604 imm_expr.X_op = O_absent;
8605 s = expr_end;
8606 continue;
252b5132 8607
4372b673
NC
8608 case 'J': /* 19-bit wait code. */
8609 my_getExpression (&imm_expr, s);
8610 check_absolute_expr (ip, &imm_expr);
793b27f4
TS
8611 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
8612 as_warn (_("Illegal 19-bit code (%lu)"),
8613 (unsigned long) imm_expr.X_add_number);
bf12938e 8614 INSERT_OPERAND (CODE19, *ip, imm_expr.X_add_number);
4372b673
NC
8615 imm_expr.X_op = O_absent;
8616 s = expr_end;
8617 continue;
8618
252b5132 8619 case 'P': /* Performance register */
beae10d5 8620 my_getExpression (&imm_expr, s);
252b5132 8621 check_absolute_expr (ip, &imm_expr);
beae10d5 8622 if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
bf12938e
RS
8623 as_warn (_("Invalid performance register (%lu)"),
8624 (unsigned long) imm_expr.X_add_number);
8625 INSERT_OPERAND (PERFREG, *ip, imm_expr.X_add_number);
beae10d5
KH
8626 imm_expr.X_op = O_absent;
8627 s = expr_end;
8628 continue;
252b5132
RH
8629
8630 case 'b': /* base register */
8631 case 'd': /* destination register */
8632 case 's': /* source register */
8633 case 't': /* target register */
8634 case 'r': /* both target and source */
8635 case 'v': /* both dest and source */
8636 case 'w': /* both dest and target */
8637 case 'E': /* coprocessor target register */
8638 case 'G': /* coprocessor destination register */
af7ee8bf 8639 case 'K': /* 'rdhwr' destination register */
252b5132
RH
8640 case 'x': /* ignore register name */
8641 case 'z': /* must be zero register */
4372b673 8642 case 'U': /* destination register (clo/clz). */
ef2e4d86 8643 case 'g': /* coprocessor destination register */
252b5132
RH
8644 s_reset = s;
8645 if (s[0] == '$')
8646 {
3882b010 8647 if (ISDIGIT (s[1]))
252b5132
RH
8648 {
8649 ++s;
8650 regno = 0;
8651 do
8652 {
8653 regno *= 10;
8654 regno += *s - '0';
8655 ++s;
8656 }
3882b010 8657 while (ISDIGIT (*s));
252b5132
RH
8658 if (regno > 31)
8659 as_bad (_("Invalid register number (%d)"), regno);
8660 }
af7ee8bf 8661 else if (*args == 'E' || *args == 'G' || *args == 'K')
252b5132
RH
8662 goto notreg;
8663 else
8664 {
76db943d
TS
8665 if (s[1] == 'r' && s[2] == 'a')
8666 {
8667 s += 3;
8668 regno = RA;
8669 }
8670 else if (s[1] == 'f' && s[2] == 'p')
252b5132
RH
8671 {
8672 s += 3;
8673 regno = FP;
8674 }
8675 else if (s[1] == 's' && s[2] == 'p')
8676 {
8677 s += 3;
8678 regno = SP;
8679 }
8680 else if (s[1] == 'g' && s[2] == 'p')
8681 {
8682 s += 3;
8683 regno = GP;
8684 }
8685 else if (s[1] == 'a' && s[2] == 't')
8686 {
8687 s += 3;
8688 regno = AT;
8689 }
8690 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8691 {
8692 s += 4;
8693 regno = KT0;
8694 }
8695 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8696 {
8697 s += 4;
8698 regno = KT1;
8699 }
85b51719
TS
8700 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
8701 {
8702 s += 5;
8703 regno = ZERO;
8704 }
252b5132
RH
8705 else if (itbl_have_entries)
8706 {
8707 char *p, *n;
d7ba4a77 8708 unsigned long r;
252b5132 8709
d7ba4a77 8710 p = s + 1; /* advance past '$' */
252b5132
RH
8711 n = itbl_get_field (&p); /* n is name */
8712
d7ba4a77
ILT
8713 /* See if this is a register defined in an
8714 itbl entry. */
8715 if (itbl_get_reg_val (n, &r))
252b5132
RH
8716 {
8717 /* Get_field advances to the start of
8718 the next field, so we need to back
d7ba4a77 8719 rack to the end of the last field. */
bdaaa2e1 8720 if (p)
252b5132 8721 s = p - 1;
bdaaa2e1 8722 else
d7ba4a77 8723 s = strchr (s, '\0');
252b5132
RH
8724 regno = r;
8725 }
8726 else
8727 goto notreg;
beae10d5 8728 }
252b5132
RH
8729 else
8730 goto notreg;
8731 }
8732 if (regno == AT
8733 && ! mips_opts.noat
8734 && *args != 'E'
af7ee8bf
CD
8735 && *args != 'G'
8736 && *args != 'K')
252b5132
RH
8737 as_warn (_("Used $at without \".set noat\""));
8738 c = *args;
8739 if (*s == ' ')
f9419b05 8740 ++s;
252b5132
RH
8741 if (args[1] != *s)
8742 {
8743 if (c == 'r' || c == 'v' || c == 'w')
8744 {
8745 regno = lastregno;
8746 s = s_reset;
f9419b05 8747 ++args;
252b5132
RH
8748 }
8749 }
8750 /* 'z' only matches $0. */
8751 if (c == 'z' && regno != 0)
8752 break;
8753
bdaaa2e1
KH
8754 /* Now that we have assembled one operand, we use the args string
8755 * to figure out where it goes in the instruction. */
252b5132
RH
8756 switch (c)
8757 {
8758 case 'r':
8759 case 's':
8760 case 'v':
8761 case 'b':
bf12938e 8762 INSERT_OPERAND (RS, *ip, regno);
252b5132
RH
8763 break;
8764 case 'd':
8765 case 'G':
af7ee8bf 8766 case 'K':
ef2e4d86 8767 case 'g':
bf12938e 8768 INSERT_OPERAND (RD, *ip, regno);
252b5132 8769 break;
4372b673 8770 case 'U':
bf12938e
RS
8771 INSERT_OPERAND (RD, *ip, regno);
8772 INSERT_OPERAND (RT, *ip, regno);
4372b673 8773 break;
252b5132
RH
8774 case 'w':
8775 case 't':
8776 case 'E':
bf12938e 8777 INSERT_OPERAND (RT, *ip, regno);
252b5132
RH
8778 break;
8779 case 'x':
8780 /* This case exists because on the r3000 trunc
8781 expands into a macro which requires a gp
8782 register. On the r6000 or r4000 it is
8783 assembled into a single instruction which
8784 ignores the register. Thus the insn version
8785 is MIPS_ISA2 and uses 'x', and the macro
8786 version is MIPS_ISA1 and uses 't'. */
8787 break;
8788 case 'z':
8789 /* This case is for the div instruction, which
8790 acts differently if the destination argument
8791 is $0. This only matches $0, and is checked
8792 outside the switch. */
8793 break;
8794 case 'D':
8795 /* Itbl operand; not yet implemented. FIXME ?? */
8796 break;
8797 /* What about all other operands like 'i', which
8798 can be specified in the opcode table? */
8799 }
8800 lastregno = regno;
8801 continue;
8802 }
8803 notreg:
8804 switch (*args++)
8805 {
8806 case 'r':
8807 case 'v':
bf12938e 8808 INSERT_OPERAND (RS, *ip, lastregno);
252b5132
RH
8809 continue;
8810 case 'w':
bf12938e 8811 INSERT_OPERAND (RT, *ip, lastregno);
252b5132
RH
8812 continue;
8813 }
8814 break;
8815
deec1734
CD
8816 case 'O': /* MDMX alignment immediate constant. */
8817 my_getExpression (&imm_expr, s);
8818 check_absolute_expr (ip, &imm_expr);
8819 if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
bf12938e
RS
8820 as_warn ("Improper align amount (%ld), using low bits",
8821 (long) imm_expr.X_add_number);
8822 INSERT_OPERAND (ALN, *ip, imm_expr.X_add_number);
deec1734
CD
8823 imm_expr.X_op = O_absent;
8824 s = expr_end;
8825 continue;
8826
8827 case 'Q': /* MDMX vector, element sel, or const. */
8828 if (s[0] != '$')
8829 {
8830 /* MDMX Immediate. */
8831 my_getExpression (&imm_expr, s);
8832 check_absolute_expr (ip, &imm_expr);
8833 if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
bf12938e
RS
8834 as_warn (_("Invalid MDMX Immediate (%ld)"),
8835 (long) imm_expr.X_add_number);
8836 INSERT_OPERAND (FT, *ip, imm_expr.X_add_number);
deec1734
CD
8837 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8838 ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
8839 else
8840 ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
deec1734
CD
8841 imm_expr.X_op = O_absent;
8842 s = expr_end;
8843 continue;
8844 }
8845 /* Not MDMX Immediate. Fall through. */
8846 case 'X': /* MDMX destination register. */
8847 case 'Y': /* MDMX source register. */
8848 case 'Z': /* MDMX target register. */
8849 is_mdmx = 1;
252b5132
RH
8850 case 'D': /* floating point destination register */
8851 case 'S': /* floating point source register */
8852 case 'T': /* floating point target register */
8853 case 'R': /* floating point source register */
8854 case 'V':
8855 case 'W':
8856 s_reset = s;
deec1734
CD
8857 /* Accept $fN for FP and MDMX register numbers, and in
8858 addition accept $vN for MDMX register numbers. */
8859 if ((s[0] == '$' && s[1] == 'f' && ISDIGIT (s[2]))
8860 || (is_mdmx != 0 && s[0] == '$' && s[1] == 'v'
8861 && ISDIGIT (s[2])))
252b5132
RH
8862 {
8863 s += 2;
8864 regno = 0;
8865 do
8866 {
8867 regno *= 10;
8868 regno += *s - '0';
8869 ++s;
8870 }
3882b010 8871 while (ISDIGIT (*s));
252b5132
RH
8872
8873 if (regno > 31)
8874 as_bad (_("Invalid float register number (%d)"), regno);
8875
8876 if ((regno & 1) != 0
ca4e0257 8877 && HAVE_32BIT_FPRS
252b5132
RH
8878 && ! (strcmp (str, "mtc1") == 0
8879 || strcmp (str, "mfc1") == 0
8880 || strcmp (str, "lwc1") == 0
8881 || strcmp (str, "swc1") == 0
8882 || strcmp (str, "l.s") == 0
ef2e4d86
CF
8883 || strcmp (str, "s.s") == 0
8884 || strcmp (str, "mftc1") == 0
8885 || strcmp (str, "mfthc1") == 0
8886 || strcmp (str, "cftc1") == 0
8887 || strcmp (str, "mttc1") == 0
8888 || strcmp (str, "mtthc1") == 0
8889 || strcmp (str, "cttc1") == 0))
252b5132
RH
8890 as_warn (_("Float register should be even, was %d"),
8891 regno);
8892
8893 c = *args;
8894 if (*s == ' ')
f9419b05 8895 ++s;
252b5132
RH
8896 if (args[1] != *s)
8897 {
8898 if (c == 'V' || c == 'W')
8899 {
8900 regno = lastregno;
8901 s = s_reset;
f9419b05 8902 ++args;
252b5132
RH
8903 }
8904 }
8905 switch (c)
8906 {
8907 case 'D':
deec1734 8908 case 'X':
bf12938e 8909 INSERT_OPERAND (FD, *ip, regno);
252b5132
RH
8910 break;
8911 case 'V':
8912 case 'S':
deec1734 8913 case 'Y':
bf12938e 8914 INSERT_OPERAND (FS, *ip, regno);
252b5132 8915 break;
deec1734
CD
8916 case 'Q':
8917 /* This is like 'Z', but also needs to fix the MDMX
8918 vector/scalar select bits. Note that the
8919 scalar immediate case is handled above. */
8920 if (*s == '[')
8921 {
8922 int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
8923 int max_el = (is_qh ? 3 : 7);
8924 s++;
8925 my_getExpression(&imm_expr, s);
8926 check_absolute_expr (ip, &imm_expr);
8927 s = expr_end;
8928 if (imm_expr.X_add_number > max_el)
8929 as_bad(_("Bad element selector %ld"),
8930 (long) imm_expr.X_add_number);
8931 imm_expr.X_add_number &= max_el;
8932 ip->insn_opcode |= (imm_expr.X_add_number
8933 << (OP_SH_VSEL +
8934 (is_qh ? 2 : 1)));
01a3f561 8935 imm_expr.X_op = O_absent;
deec1734
CD
8936 if (*s != ']')
8937 as_warn(_("Expecting ']' found '%s'"), s);
8938 else
8939 s++;
8940 }
8941 else
8942 {
8943 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8944 ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
8945 << OP_SH_VSEL);
8946 else
8947 ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
8948 OP_SH_VSEL);
8949 }
8950 /* Fall through */
252b5132
RH
8951 case 'W':
8952 case 'T':
deec1734 8953 case 'Z':
bf12938e 8954 INSERT_OPERAND (FT, *ip, regno);
252b5132
RH
8955 break;
8956 case 'R':
bf12938e 8957 INSERT_OPERAND (FR, *ip, regno);
252b5132
RH
8958 break;
8959 }
8960 lastregno = regno;
8961 continue;
8962 }
8963
252b5132
RH
8964 switch (*args++)
8965 {
8966 case 'V':
bf12938e 8967 INSERT_OPERAND (FS, *ip, lastregno);
252b5132
RH
8968 continue;
8969 case 'W':
bf12938e 8970 INSERT_OPERAND (FT, *ip, lastregno);
252b5132
RH
8971 continue;
8972 }
8973 break;
8974
8975 case 'I':
8976 my_getExpression (&imm_expr, s);
8977 if (imm_expr.X_op != O_big
8978 && imm_expr.X_op != O_constant)
8979 insn_error = _("absolute expression required");
9ee2a2d4
MR
8980 if (HAVE_32BIT_GPRS)
8981 normalize_constant_expr (&imm_expr);
252b5132
RH
8982 s = expr_end;
8983 continue;
8984
8985 case 'A':
8986 my_getExpression (&offset_expr, s);
2051e8c4 8987 normalize_address_expr (&offset_expr);
f6688943 8988 *imm_reloc = BFD_RELOC_32;
252b5132
RH
8989 s = expr_end;
8990 continue;
8991
8992 case 'F':
8993 case 'L':
8994 case 'f':
8995 case 'l':
8996 {
8997 int f64;
ca4e0257 8998 int using_gprs;
252b5132
RH
8999 char *save_in;
9000 char *err;
9001 unsigned char temp[8];
9002 int len;
9003 unsigned int length;
9004 segT seg;
9005 subsegT subseg;
9006 char *p;
9007
9008 /* These only appear as the last operand in an
9009 instruction, and every instruction that accepts
9010 them in any variant accepts them in all variants.
9011 This means we don't have to worry about backing out
9012 any changes if the instruction does not match.
9013
9014 The difference between them is the size of the
9015 floating point constant and where it goes. For 'F'
9016 and 'L' the constant is 64 bits; for 'f' and 'l' it
9017 is 32 bits. Where the constant is placed is based
9018 on how the MIPS assembler does things:
9019 F -- .rdata
9020 L -- .lit8
9021 f -- immediate value
9022 l -- .lit4
9023
9024 The .lit4 and .lit8 sections are only used if
9025 permitted by the -G argument.
9026
ca4e0257
RS
9027 The code below needs to know whether the target register
9028 is 32 or 64 bits wide. It relies on the fact 'f' and
9029 'F' are used with GPR-based instructions and 'l' and
9030 'L' are used with FPR-based instructions. */
252b5132
RH
9031
9032 f64 = *args == 'F' || *args == 'L';
ca4e0257 9033 using_gprs = *args == 'F' || *args == 'f';
252b5132
RH
9034
9035 save_in = input_line_pointer;
9036 input_line_pointer = s;
9037 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
9038 length = len;
9039 s = input_line_pointer;
9040 input_line_pointer = save_in;
9041 if (err != NULL && *err != '\0')
9042 {
9043 as_bad (_("Bad floating point constant: %s"), err);
9044 memset (temp, '\0', sizeof temp);
9045 length = f64 ? 8 : 4;
9046 }
9047
156c2f8b 9048 assert (length == (unsigned) (f64 ? 8 : 4));
252b5132
RH
9049
9050 if (*args == 'f'
9051 || (*args == 'l'
3e722fb5 9052 && (g_switch_value < 4
252b5132
RH
9053 || (temp[0] == 0 && temp[1] == 0)
9054 || (temp[2] == 0 && temp[3] == 0))))
9055 {
9056 imm_expr.X_op = O_constant;
9057 if (! target_big_endian)
9058 imm_expr.X_add_number = bfd_getl32 (temp);
9059 else
9060 imm_expr.X_add_number = bfd_getb32 (temp);
9061 }
9062 else if (length > 4
119d663a 9063 && ! mips_disable_float_construction
ca4e0257
RS
9064 /* Constants can only be constructed in GPRs and
9065 copied to FPRs if the GPRs are at least as wide
9066 as the FPRs. Force the constant into memory if
9067 we are using 64-bit FPRs but the GPRs are only
9068 32 bits wide. */
9069 && (using_gprs
9070 || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
252b5132
RH
9071 && ((temp[0] == 0 && temp[1] == 0)
9072 || (temp[2] == 0 && temp[3] == 0))
9073 && ((temp[4] == 0 && temp[5] == 0)
9074 || (temp[6] == 0 && temp[7] == 0)))
9075 {
ca4e0257
RS
9076 /* The value is simple enough to load with a couple of
9077 instructions. If using 32-bit registers, set
9078 imm_expr to the high order 32 bits and offset_expr to
9079 the low order 32 bits. Otherwise, set imm_expr to
9080 the entire 64 bit constant. */
9081 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
252b5132
RH
9082 {
9083 imm_expr.X_op = O_constant;
9084 offset_expr.X_op = O_constant;
9085 if (! target_big_endian)
9086 {
9087 imm_expr.X_add_number = bfd_getl32 (temp + 4);
9088 offset_expr.X_add_number = bfd_getl32 (temp);
9089 }
9090 else
9091 {
9092 imm_expr.X_add_number = bfd_getb32 (temp);
9093 offset_expr.X_add_number = bfd_getb32 (temp + 4);
9094 }
9095 if (offset_expr.X_add_number == 0)
9096 offset_expr.X_op = O_absent;
9097 }
9098 else if (sizeof (imm_expr.X_add_number) > 4)
9099 {
9100 imm_expr.X_op = O_constant;
9101 if (! target_big_endian)
9102 imm_expr.X_add_number = bfd_getl64 (temp);
9103 else
9104 imm_expr.X_add_number = bfd_getb64 (temp);
9105 }
9106 else
9107 {
9108 imm_expr.X_op = O_big;
9109 imm_expr.X_add_number = 4;
9110 if (! target_big_endian)
9111 {
9112 generic_bignum[0] = bfd_getl16 (temp);
9113 generic_bignum[1] = bfd_getl16 (temp + 2);
9114 generic_bignum[2] = bfd_getl16 (temp + 4);
9115 generic_bignum[3] = bfd_getl16 (temp + 6);
9116 }
9117 else
9118 {
9119 generic_bignum[0] = bfd_getb16 (temp + 6);
9120 generic_bignum[1] = bfd_getb16 (temp + 4);
9121 generic_bignum[2] = bfd_getb16 (temp + 2);
9122 generic_bignum[3] = bfd_getb16 (temp);
9123 }
9124 }
9125 }
9126 else
9127 {
9128 const char *newname;
9129 segT new_seg;
9130
9131 /* Switch to the right section. */
9132 seg = now_seg;
9133 subseg = now_subseg;
9134 switch (*args)
9135 {
9136 default: /* unused default case avoids warnings. */
9137 case 'L':
9138 newname = RDATA_SECTION_NAME;
3e722fb5 9139 if (g_switch_value >= 8)
252b5132
RH
9140 newname = ".lit8";
9141 break;
9142 case 'F':
3e722fb5 9143 newname = RDATA_SECTION_NAME;
252b5132
RH
9144 break;
9145 case 'l':
4d0d148d 9146 assert (g_switch_value >= 4);
252b5132
RH
9147 newname = ".lit4";
9148 break;
9149 }
9150 new_seg = subseg_new (newname, (subsegT) 0);
9151 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
9152 bfd_set_section_flags (stdoutput, new_seg,
9153 (SEC_ALLOC
9154 | SEC_LOAD
9155 | SEC_READONLY
9156 | SEC_DATA));
9157 frag_align (*args == 'l' ? 2 : 3, 0, 0);
9158 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
9159 && strcmp (TARGET_OS, "elf") != 0)
9160 record_alignment (new_seg, 4);
9161 else
9162 record_alignment (new_seg, *args == 'l' ? 2 : 3);
9163 if (seg == now_seg)
9164 as_bad (_("Can't use floating point insn in this section"));
9165
9166 /* Set the argument to the current address in the
9167 section. */
9168 offset_expr.X_op = O_symbol;
9169 offset_expr.X_add_symbol =
9170 symbol_new ("L0\001", now_seg,
9171 (valueT) frag_now_fix (), frag_now);
9172 offset_expr.X_add_number = 0;
9173
9174 /* Put the floating point number into the section. */
9175 p = frag_more ((int) length);
9176 memcpy (p, temp, length);
9177
9178 /* Switch back to the original section. */
9179 subseg_set (seg, subseg);
9180 }
9181 }
9182 continue;
9183
9184 case 'i': /* 16 bit unsigned immediate */
9185 case 'j': /* 16 bit signed immediate */
f6688943 9186 *imm_reloc = BFD_RELOC_LO16;
5e0116d5 9187 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0)
252b5132
RH
9188 {
9189 int more;
5e0116d5
RS
9190 offsetT minval, maxval;
9191
9192 more = (insn + 1 < &mips_opcodes[NUMOPCODES]
9193 && strcmp (insn->name, insn[1].name) == 0);
9194
9195 /* If the expression was written as an unsigned number,
9196 only treat it as signed if there are no more
9197 alternatives. */
9198 if (more
9199 && *args == 'j'
9200 && sizeof (imm_expr.X_add_number) <= 4
9201 && imm_expr.X_op == O_constant
9202 && imm_expr.X_add_number < 0
9203 && imm_expr.X_unsigned
9204 && HAVE_64BIT_GPRS)
9205 break;
9206
9207 /* For compatibility with older assemblers, we accept
9208 0x8000-0xffff as signed 16-bit numbers when only
9209 signed numbers are allowed. */
9210 if (*args == 'i')
9211 minval = 0, maxval = 0xffff;
9212 else if (more)
9213 minval = -0x8000, maxval = 0x7fff;
252b5132 9214 else
5e0116d5
RS
9215 minval = -0x8000, maxval = 0xffff;
9216
9217 if (imm_expr.X_op != O_constant
9218 || imm_expr.X_add_number < minval
9219 || imm_expr.X_add_number > maxval)
252b5132
RH
9220 {
9221 if (more)
9222 break;
2ae7e77b
AH
9223 if (imm_expr.X_op == O_constant
9224 || imm_expr.X_op == O_big)
5e0116d5 9225 as_bad (_("expression out of range"));
252b5132
RH
9226 }
9227 }
9228 s = expr_end;
9229 continue;
9230
9231 case 'o': /* 16 bit offset */
5e0116d5
RS
9232 /* Check whether there is only a single bracketed expression
9233 left. If so, it must be the base register and the
9234 constant must be zero. */
9235 if (*s == '(' && strchr (s + 1, '(') == 0)
9236 {
9237 offset_expr.X_op = O_constant;
9238 offset_expr.X_add_number = 0;
9239 continue;
9240 }
252b5132
RH
9241
9242 /* If this value won't fit into a 16 bit offset, then go
9243 find a macro that will generate the 32 bit offset
afdbd6d0 9244 code pattern. */
5e0116d5 9245 if (my_getSmallExpression (&offset_expr, offset_reloc, s) == 0
252b5132
RH
9246 && (offset_expr.X_op != O_constant
9247 || offset_expr.X_add_number >= 0x8000
afdbd6d0 9248 || offset_expr.X_add_number < -0x8000))
252b5132
RH
9249 break;
9250
252b5132
RH
9251 s = expr_end;
9252 continue;
9253
9254 case 'p': /* pc relative offset */
0b25d3e6 9255 *offset_reloc = BFD_RELOC_16_PCREL_S2;
252b5132
RH
9256 my_getExpression (&offset_expr, s);
9257 s = expr_end;
9258 continue;
9259
9260 case 'u': /* upper 16 bits */
5e0116d5
RS
9261 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0
9262 && imm_expr.X_op == O_constant
9263 && (imm_expr.X_add_number < 0
9264 || imm_expr.X_add_number >= 0x10000))
252b5132
RH
9265 as_bad (_("lui expression not in range 0..65535"));
9266 s = expr_end;
9267 continue;
9268
9269 case 'a': /* 26 bit address */
9270 my_getExpression (&offset_expr, s);
9271 s = expr_end;
f6688943 9272 *offset_reloc = BFD_RELOC_MIPS_JMP;
252b5132
RH
9273 continue;
9274
9275 case 'N': /* 3 bit branch condition code */
9276 case 'M': /* 3 bit compare condition code */
9277 if (strncmp (s, "$fcc", 4) != 0)
9278 break;
9279 s += 4;
9280 regno = 0;
9281 do
9282 {
9283 regno *= 10;
9284 regno += *s - '0';
9285 ++s;
9286 }
3882b010 9287 while (ISDIGIT (*s));
252b5132 9288 if (regno > 7)
30c378fd
CD
9289 as_bad (_("Invalid condition code register $fcc%d"), regno);
9290 if ((strcmp(str + strlen(str) - 3, ".ps") == 0
9291 || strcmp(str + strlen(str) - 5, "any2f") == 0
9292 || strcmp(str + strlen(str) - 5, "any2t") == 0)
9293 && (regno & 1) != 0)
9294 as_warn(_("Condition code register should be even for %s, was %d"),
9295 str, regno);
9296 if ((strcmp(str + strlen(str) - 5, "any4f") == 0
9297 || strcmp(str + strlen(str) - 5, "any4t") == 0)
9298 && (regno & 3) != 0)
9299 as_warn(_("Condition code register should be 0 or 4 for %s, was %d"),
9300 str, regno);
252b5132 9301 if (*args == 'N')
bf12938e 9302 INSERT_OPERAND (BCC, *ip, regno);
252b5132 9303 else
bf12938e 9304 INSERT_OPERAND (CCC, *ip, regno);
beae10d5 9305 continue;
252b5132 9306
156c2f8b
NC
9307 case 'H':
9308 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
9309 s += 2;
3882b010 9310 if (ISDIGIT (*s))
156c2f8b
NC
9311 {
9312 c = 0;
9313 do
9314 {
9315 c *= 10;
9316 c += *s - '0';
9317 ++s;
9318 }
3882b010 9319 while (ISDIGIT (*s));
156c2f8b
NC
9320 }
9321 else
9322 c = 8; /* Invalid sel value. */
9323
9324 if (c > 7)
9325 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
9326 ip->insn_opcode |= c;
9327 continue;
9328
60b63b72
RS
9329 case 'e':
9330 /* Must be at least one digit. */
9331 my_getExpression (&imm_expr, s);
9332 check_absolute_expr (ip, &imm_expr);
9333
9334 if ((unsigned long) imm_expr.X_add_number
9335 > (unsigned long) OP_MASK_VECBYTE)
9336 {
9337 as_bad (_("bad byte vector index (%ld)"),
9338 (long) imm_expr.X_add_number);
9339 imm_expr.X_add_number = 0;
9340 }
9341
bf12938e 9342 INSERT_OPERAND (VECBYTE, *ip, imm_expr.X_add_number);
60b63b72
RS
9343 imm_expr.X_op = O_absent;
9344 s = expr_end;
9345 continue;
9346
9347 case '%':
9348 my_getExpression (&imm_expr, s);
9349 check_absolute_expr (ip, &imm_expr);
9350
9351 if ((unsigned long) imm_expr.X_add_number
9352 > (unsigned long) OP_MASK_VECALIGN)
9353 {
9354 as_bad (_("bad byte vector index (%ld)"),
9355 (long) imm_expr.X_add_number);
9356 imm_expr.X_add_number = 0;
9357 }
9358
bf12938e 9359 INSERT_OPERAND (VECALIGN, *ip, imm_expr.X_add_number);
60b63b72
RS
9360 imm_expr.X_op = O_absent;
9361 s = expr_end;
9362 continue;
9363
252b5132
RH
9364 default:
9365 as_bad (_("bad char = '%c'\n"), *args);
9366 internalError ();
9367 }
9368 break;
9369 }
9370 /* Args don't match. */
9371 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
9372 !strcmp (insn->name, insn[1].name))
9373 {
9374 ++insn;
9375 s = argsStart;
268f6bed 9376 insn_error = _("illegal operands");
252b5132
RH
9377 continue;
9378 }
268f6bed
L
9379 if (save_c)
9380 *(--s) = save_c;
252b5132
RH
9381 insn_error = _("illegal operands");
9382 return;
9383 }
9384}
9385
0499d65b
TS
9386#define SKIP_SPACE_TABS(S) { while (*(S) == ' ' || *(S) == '\t') ++(S); }
9387
252b5132
RH
9388/* This routine assembles an instruction into its binary format when
9389 assembling for the mips16. As a side effect, it sets one of the
9390 global variables imm_reloc or offset_reloc to the type of
9391 relocation to do if one of the operands is an address expression.
9392 It also sets mips16_small and mips16_ext if the user explicitly
9393 requested a small or extended instruction. */
9394
9395static void
17a2f251 9396mips16_ip (char *str, struct mips_cl_insn *ip)
252b5132
RH
9397{
9398 char *s;
9399 const char *args;
9400 struct mips_opcode *insn;
9401 char *argsstart;
9402 unsigned int regno;
9403 unsigned int lastregno = 0;
9404 char *s_reset;
d6f16593 9405 size_t i;
252b5132
RH
9406
9407 insn_error = NULL;
9408
b34976b6
AM
9409 mips16_small = FALSE;
9410 mips16_ext = FALSE;
252b5132 9411
3882b010 9412 for (s = str; ISLOWER (*s); ++s)
252b5132
RH
9413 ;
9414 switch (*s)
9415 {
9416 case '\0':
9417 break;
9418
9419 case ' ':
9420 *s++ = '\0';
9421 break;
9422
9423 case '.':
9424 if (s[1] == 't' && s[2] == ' ')
9425 {
9426 *s = '\0';
b34976b6 9427 mips16_small = TRUE;
252b5132
RH
9428 s += 3;
9429 break;
9430 }
9431 else if (s[1] == 'e' && s[2] == ' ')
9432 {
9433 *s = '\0';
b34976b6 9434 mips16_ext = TRUE;
252b5132
RH
9435 s += 3;
9436 break;
9437 }
9438 /* Fall through. */
9439 default:
9440 insn_error = _("unknown opcode");
9441 return;
9442 }
9443
9444 if (mips_opts.noautoextend && ! mips16_ext)
b34976b6 9445 mips16_small = TRUE;
252b5132
RH
9446
9447 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
9448 {
9449 insn_error = _("unrecognized opcode");
9450 return;
9451 }
9452
9453 argsstart = s;
9454 for (;;)
9455 {
9456 assert (strcmp (insn->name, str) == 0);
9457
1e915849 9458 create_insn (ip, insn);
252b5132 9459 imm_expr.X_op = O_absent;
f6688943
TS
9460 imm_reloc[0] = BFD_RELOC_UNUSED;
9461 imm_reloc[1] = BFD_RELOC_UNUSED;
9462 imm_reloc[2] = BFD_RELOC_UNUSED;
5f74bc13 9463 imm2_expr.X_op = O_absent;
252b5132 9464 offset_expr.X_op = O_absent;
f6688943
TS
9465 offset_reloc[0] = BFD_RELOC_UNUSED;
9466 offset_reloc[1] = BFD_RELOC_UNUSED;
9467 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
9468 for (args = insn->args; 1; ++args)
9469 {
9470 int c;
9471
9472 if (*s == ' ')
9473 ++s;
9474
9475 /* In this switch statement we call break if we did not find
9476 a match, continue if we did find a match, or return if we
9477 are done. */
9478
9479 c = *args;
9480 switch (c)
9481 {
9482 case '\0':
9483 if (*s == '\0')
9484 {
9485 /* Stuff the immediate value in now, if we can. */
9486 if (imm_expr.X_op == O_constant
f6688943 9487 && *imm_reloc > BFD_RELOC_UNUSED
252b5132
RH
9488 && insn->pinfo != INSN_MACRO)
9489 {
d6f16593
MR
9490 valueT tmp;
9491
9492 switch (*offset_reloc)
9493 {
9494 case BFD_RELOC_MIPS16_HI16_S:
9495 tmp = (imm_expr.X_add_number + 0x8000) >> 16;
9496 break;
9497
9498 case BFD_RELOC_MIPS16_HI16:
9499 tmp = imm_expr.X_add_number >> 16;
9500 break;
9501
9502 case BFD_RELOC_MIPS16_LO16:
9503 tmp = ((imm_expr.X_add_number + 0x8000) & 0xffff)
9504 - 0x8000;
9505 break;
9506
9507 case BFD_RELOC_UNUSED:
9508 tmp = imm_expr.X_add_number;
9509 break;
9510
9511 default:
9512 internalError ();
9513 }
9514 *offset_reloc = BFD_RELOC_UNUSED;
9515
c4e7957c 9516 mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
d6f16593 9517 tmp, TRUE, mips16_small,
252b5132
RH
9518 mips16_ext, &ip->insn_opcode,
9519 &ip->use_extend, &ip->extend);
9520 imm_expr.X_op = O_absent;
f6688943 9521 *imm_reloc = BFD_RELOC_UNUSED;
252b5132
RH
9522 }
9523
9524 return;
9525 }
9526 break;
9527
9528 case ',':
9529 if (*s++ == c)
9530 continue;
9531 s--;
9532 switch (*++args)
9533 {
9534 case 'v':
bf12938e 9535 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
252b5132
RH
9536 continue;
9537 case 'w':
bf12938e 9538 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
252b5132
RH
9539 continue;
9540 }
9541 break;
9542
9543 case '(':
9544 case ')':
9545 if (*s++ == c)
9546 continue;
9547 break;
9548
9549 case 'v':
9550 case 'w':
9551 if (s[0] != '$')
9552 {
9553 if (c == 'v')
bf12938e 9554 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
252b5132 9555 else
bf12938e 9556 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
252b5132
RH
9557 ++args;
9558 continue;
9559 }
9560 /* Fall through. */
9561 case 'x':
9562 case 'y':
9563 case 'z':
9564 case 'Z':
9565 case '0':
9566 case 'S':
9567 case 'R':
9568 case 'X':
9569 case 'Y':
9570 if (s[0] != '$')
9571 break;
9572 s_reset = s;
3882b010 9573 if (ISDIGIT (s[1]))
252b5132
RH
9574 {
9575 ++s;
9576 regno = 0;
9577 do
9578 {
9579 regno *= 10;
9580 regno += *s - '0';
9581 ++s;
9582 }
3882b010 9583 while (ISDIGIT (*s));
252b5132
RH
9584 if (regno > 31)
9585 {
9586 as_bad (_("invalid register number (%d)"), regno);
9587 regno = 2;
9588 }
9589 }
9590 else
9591 {
76db943d
TS
9592 if (s[1] == 'r' && s[2] == 'a')
9593 {
9594 s += 3;
9595 regno = RA;
9596 }
9597 else if (s[1] == 'f' && s[2] == 'p')
252b5132
RH
9598 {
9599 s += 3;
9600 regno = FP;
9601 }
9602 else if (s[1] == 's' && s[2] == 'p')
9603 {
9604 s += 3;
9605 regno = SP;
9606 }
9607 else if (s[1] == 'g' && s[2] == 'p')
9608 {
9609 s += 3;
9610 regno = GP;
9611 }
9612 else if (s[1] == 'a' && s[2] == 't')
9613 {
9614 s += 3;
9615 regno = AT;
9616 }
9617 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
9618 {
9619 s += 4;
9620 regno = KT0;
9621 }
9622 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
9623 {
9624 s += 4;
9625 regno = KT1;
9626 }
85b51719
TS
9627 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
9628 {
9629 s += 5;
9630 regno = ZERO;
9631 }
252b5132
RH
9632 else
9633 break;
9634 }
9635
9636 if (*s == ' ')
9637 ++s;
9638 if (args[1] != *s)
9639 {
9640 if (c == 'v' || c == 'w')
9641 {
9642 regno = mips16_to_32_reg_map[lastregno];
9643 s = s_reset;
f9419b05 9644 ++args;
252b5132
RH
9645 }
9646 }
9647
9648 switch (c)
9649 {
9650 case 'x':
9651 case 'y':
9652 case 'z':
9653 case 'v':
9654 case 'w':
9655 case 'Z':
9656 regno = mips32_to_16_reg_map[regno];
9657 break;
9658
9659 case '0':
9660 if (regno != 0)
9661 regno = ILLEGAL_REG;
9662 break;
9663
9664 case 'S':
9665 if (regno != SP)
9666 regno = ILLEGAL_REG;
9667 break;
9668
9669 case 'R':
9670 if (regno != RA)
9671 regno = ILLEGAL_REG;
9672 break;
9673
9674 case 'X':
9675 case 'Y':
9676 if (regno == AT && ! mips_opts.noat)
9677 as_warn (_("used $at without \".set noat\""));
9678 break;
9679
9680 default:
9681 internalError ();
9682 }
9683
9684 if (regno == ILLEGAL_REG)
9685 break;
9686
9687 switch (c)
9688 {
9689 case 'x':
9690 case 'v':
bf12938e 9691 MIPS16_INSERT_OPERAND (RX, *ip, regno);
252b5132
RH
9692 break;
9693 case 'y':
9694 case 'w':
bf12938e 9695 MIPS16_INSERT_OPERAND (RY, *ip, regno);
252b5132
RH
9696 break;
9697 case 'z':
bf12938e 9698 MIPS16_INSERT_OPERAND (RZ, *ip, regno);
252b5132
RH
9699 break;
9700 case 'Z':
bf12938e 9701 MIPS16_INSERT_OPERAND (MOVE32Z, *ip, regno);
252b5132
RH
9702 case '0':
9703 case 'S':
9704 case 'R':
9705 break;
9706 case 'X':
bf12938e 9707 MIPS16_INSERT_OPERAND (REGR32, *ip, regno);
252b5132
RH
9708 break;
9709 case 'Y':
9710 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
bf12938e 9711 MIPS16_INSERT_OPERAND (REG32R, *ip, regno);
252b5132
RH
9712 break;
9713 default:
9714 internalError ();
9715 }
9716
9717 lastregno = regno;
9718 continue;
9719
9720 case 'P':
9721 if (strncmp (s, "$pc", 3) == 0)
9722 {
9723 s += 3;
9724 continue;
9725 }
9726 break;
9727
252b5132
RH
9728 case '5':
9729 case 'H':
9730 case 'W':
9731 case 'D':
9732 case 'j':
252b5132
RH
9733 case 'V':
9734 case 'C':
9735 case 'U':
9736 case 'k':
9737 case 'K':
d6f16593
MR
9738 i = my_getSmallExpression (&imm_expr, imm_reloc, s);
9739 if (i > 0)
252b5132 9740 {
d6f16593 9741 if (imm_expr.X_op != O_constant)
252b5132 9742 {
b34976b6 9743 mips16_ext = TRUE;
b34976b6 9744 ip->use_extend = TRUE;
252b5132 9745 ip->extend = 0;
252b5132 9746 }
d6f16593
MR
9747 else
9748 {
9749 /* We need to relax this instruction. */
9750 *offset_reloc = *imm_reloc;
9751 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9752 }
9753 s = expr_end;
9754 continue;
252b5132 9755 }
d6f16593
MR
9756 *imm_reloc = BFD_RELOC_UNUSED;
9757 /* Fall through. */
9758 case '<':
9759 case '>':
9760 case '[':
9761 case ']':
9762 case '4':
9763 case '8':
9764 my_getExpression (&imm_expr, s);
252b5132
RH
9765 if (imm_expr.X_op == O_register)
9766 {
9767 /* What we thought was an expression turned out to
9768 be a register. */
9769
9770 if (s[0] == '(' && args[1] == '(')
9771 {
9772 /* It looks like the expression was omitted
9773 before a register indirection, which means
9774 that the expression is implicitly zero. We
9775 still set up imm_expr, so that we handle
9776 explicit extensions correctly. */
9777 imm_expr.X_op = O_constant;
9778 imm_expr.X_add_number = 0;
f6688943 9779 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
9780 continue;
9781 }
9782
9783 break;
9784 }
9785
9786 /* We need to relax this instruction. */
f6688943 9787 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
9788 s = expr_end;
9789 continue;
9790
9791 case 'p':
9792 case 'q':
9793 case 'A':
9794 case 'B':
9795 case 'E':
9796 /* We use offset_reloc rather than imm_reloc for the PC
9797 relative operands. This lets macros with both
9798 immediate and address operands work correctly. */
9799 my_getExpression (&offset_expr, s);
9800
9801 if (offset_expr.X_op == O_register)
9802 break;
9803
9804 /* We need to relax this instruction. */
f6688943 9805 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
9806 s = expr_end;
9807 continue;
9808
9809 case '6': /* break code */
9810 my_getExpression (&imm_expr, s);
9811 check_absolute_expr (ip, &imm_expr);
9812 if ((unsigned long) imm_expr.X_add_number > 63)
bf12938e
RS
9813 as_warn (_("Invalid value for `%s' (%lu)"),
9814 ip->insn_mo->name,
9815 (unsigned long) imm_expr.X_add_number);
9816 MIPS16_INSERT_OPERAND (IMM6, *ip, imm_expr.X_add_number);
252b5132
RH
9817 imm_expr.X_op = O_absent;
9818 s = expr_end;
9819 continue;
9820
9821 case 'a': /* 26 bit address */
9822 my_getExpression (&offset_expr, s);
9823 s = expr_end;
f6688943 9824 *offset_reloc = BFD_RELOC_MIPS16_JMP;
252b5132
RH
9825 ip->insn_opcode <<= 16;
9826 continue;
9827
9828 case 'l': /* register list for entry macro */
9829 case 'L': /* register list for exit macro */
9830 {
9831 int mask;
9832
9833 if (c == 'l')
9834 mask = 0;
9835 else
9836 mask = 7 << 3;
9837 while (*s != '\0')
9838 {
9839 int freg, reg1, reg2;
9840
9841 while (*s == ' ' || *s == ',')
9842 ++s;
9843 if (*s != '$')
9844 {
9845 as_bad (_("can't parse register list"));
9846 break;
9847 }
9848 ++s;
9849 if (*s != 'f')
9850 freg = 0;
9851 else
9852 {
9853 freg = 1;
9854 ++s;
9855 }
9856 reg1 = 0;
3882b010 9857 while (ISDIGIT (*s))
252b5132
RH
9858 {
9859 reg1 *= 10;
9860 reg1 += *s - '0';
9861 ++s;
9862 }
9863 if (*s == ' ')
9864 ++s;
9865 if (*s != '-')
9866 reg2 = reg1;
9867 else
9868 {
9869 ++s;
9870 if (*s != '$')
9871 break;
9872 ++s;
9873 if (freg)
9874 {
9875 if (*s == 'f')
9876 ++s;
9877 else
9878 {
9879 as_bad (_("invalid register list"));
9880 break;
9881 }
9882 }
9883 reg2 = 0;
3882b010 9884 while (ISDIGIT (*s))
252b5132
RH
9885 {
9886 reg2 *= 10;
9887 reg2 += *s - '0';
9888 ++s;
9889 }
9890 }
9891 if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
9892 {
9893 mask &= ~ (7 << 3);
9894 mask |= 5 << 3;
9895 }
9896 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
9897 {
9898 mask &= ~ (7 << 3);
9899 mask |= 6 << 3;
9900 }
9901 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
9902 mask |= (reg2 - 3) << 3;
9903 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
9904 mask |= (reg2 - 15) << 1;
f9419b05 9905 else if (reg1 == RA && reg2 == RA)
252b5132
RH
9906 mask |= 1;
9907 else
9908 {
9909 as_bad (_("invalid register list"));
9910 break;
9911 }
9912 }
9913 /* The mask is filled in in the opcode table for the
9914 benefit of the disassembler. We remove it before
9915 applying the actual mask. */
9916 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
9917 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
9918 }
9919 continue;
9920
0499d65b
TS
9921 case 'm': /* Register list for save insn. */
9922 case 'M': /* Register list for restore insn. */
9923 {
9924 int opcode = 0;
9925 int framesz = 0, seen_framesz = 0;
9926 int args = 0, statics = 0, sregs = 0;
9927
9928 while (*s != '\0')
9929 {
9930 unsigned int reg1, reg2;
9931
9932 SKIP_SPACE_TABS (s);
9933 while (*s == ',')
9934 ++s;
9935 SKIP_SPACE_TABS (s);
9936
9937 my_getExpression (&imm_expr, s);
9938 if (imm_expr.X_op == O_constant)
9939 {
9940 /* Handle the frame size. */
9941 if (seen_framesz)
9942 {
9943 as_bad (_("more than one frame size in list"));
9944 break;
9945 }
9946 seen_framesz = 1;
9947 framesz = imm_expr.X_add_number;
9948 imm_expr.X_op = O_absent;
9949 s = expr_end;
9950 continue;
9951 }
9952
9953 if (*s != '$')
9954 {
9955 as_bad (_("can't parse register list"));
9956 break;
9957 }
9958 ++s;
9959
9960 reg1 = 0;
9961 while (ISDIGIT (*s))
9962 {
9963 reg1 *= 10;
9964 reg1 += *s - '0';
9965 ++s;
9966 }
9967 SKIP_SPACE_TABS (s);
9968 if (*s != '-')
9969 reg2 = reg1;
9970 else
9971 {
9972 ++s;
9973 if (*s != '$')
9974 {
9975 as_bad (_("can't parse register list"));
9976 break;
9977 }
9978 ++s;
9979 reg2 = 0;
9980 while (ISDIGIT (*s))
9981 {
9982 reg2 *= 10;
9983 reg2 += *s - '0';
9984 ++s;
9985 }
9986 }
9987
9988 while (reg1 <= reg2)
9989 {
9990 if (reg1 >= 4 && reg1 <= 7)
9991 {
9992 if (c == 'm' && !seen_framesz)
9993 /* args $a0-$a3 */
9994 args |= 1 << (reg1 - 4);
9995 else
9996 /* statics $a0-$a3 */
9997 statics |= 1 << (reg1 - 4);
9998 }
9999 else if ((reg1 >= 16 && reg1 <= 23) || reg1 == 30)
10000 {
10001 /* $s0-$s8 */
10002 sregs |= 1 << ((reg1 == 30) ? 8 : (reg1 - 16));
10003 }
10004 else if (reg1 == 31)
10005 {
10006 /* Add $ra to insn. */
10007 opcode |= 0x40;
10008 }
10009 else
10010 {
10011 as_bad (_("unexpected register in list"));
10012 break;
10013 }
10014 if (++reg1 == 24)
10015 reg1 = 30;
10016 }
10017 }
10018
10019 /* Encode args/statics combination. */
10020 if (args & statics)
10021 as_bad (_("arg/static registers overlap"));
10022 else if (args == 0xf)
10023 /* All $a0-$a3 are args. */
10024 opcode |= MIPS16_ALL_ARGS << 16;
10025 else if (statics == 0xf)
10026 /* All $a0-$a3 are statics. */
10027 opcode |= MIPS16_ALL_STATICS << 16;
10028 else
10029 {
10030 int narg = 0, nstat = 0;
10031
10032 /* Count arg registers. */
10033 while (args & 0x1)
10034 {
10035 args >>= 1;
10036 narg++;
10037 }
10038 if (args != 0)
10039 as_bad (_("invalid arg register list"));
10040
10041 /* Count static registers. */
10042 while (statics & 0x8)
10043 {
10044 statics = (statics << 1) & 0xf;
10045 nstat++;
10046 }
10047 if (statics != 0)
10048 as_bad (_("invalid static register list"));
10049
10050 /* Encode args/statics. */
10051 opcode |= ((narg << 2) | nstat) << 16;
10052 }
10053
10054 /* Encode $s0/$s1. */
10055 if (sregs & (1 << 0)) /* $s0 */
10056 opcode |= 0x20;
10057 if (sregs & (1 << 1)) /* $s1 */
10058 opcode |= 0x10;
10059 sregs >>= 2;
10060
10061 if (sregs != 0)
10062 {
10063 /* Count regs $s2-$s8. */
10064 int nsreg = 0;
10065 while (sregs & 1)
10066 {
10067 sregs >>= 1;
10068 nsreg++;
10069 }
10070 if (sregs != 0)
10071 as_bad (_("invalid static register list"));
10072 /* Encode $s2-$s8. */
10073 opcode |= nsreg << 24;
10074 }
10075
10076 /* Encode frame size. */
10077 if (!seen_framesz)
10078 as_bad (_("missing frame size"));
10079 else if ((framesz & 7) != 0 || framesz < 0
10080 || framesz > 0xff * 8)
10081 as_bad (_("invalid frame size"));
10082 else if (framesz != 128 || (opcode >> 16) != 0)
10083 {
10084 framesz /= 8;
10085 opcode |= (((framesz & 0xf0) << 16)
10086 | (framesz & 0x0f));
10087 }
10088
10089 /* Finally build the instruction. */
10090 if ((opcode >> 16) != 0 || framesz == 0)
10091 {
10092 ip->use_extend = TRUE;
10093 ip->extend = opcode >> 16;
10094 }
10095 ip->insn_opcode |= opcode & 0x7f;
10096 }
10097 continue;
10098
252b5132
RH
10099 case 'e': /* extend code */
10100 my_getExpression (&imm_expr, s);
10101 check_absolute_expr (ip, &imm_expr);
10102 if ((unsigned long) imm_expr.X_add_number > 0x7ff)
10103 {
10104 as_warn (_("Invalid value for `%s' (%lu)"),
10105 ip->insn_mo->name,
10106 (unsigned long) imm_expr.X_add_number);
10107 imm_expr.X_add_number &= 0x7ff;
10108 }
10109 ip->insn_opcode |= imm_expr.X_add_number;
10110 imm_expr.X_op = O_absent;
10111 s = expr_end;
10112 continue;
10113
10114 default:
10115 internalError ();
10116 }
10117 break;
10118 }
10119
10120 /* Args don't match. */
10121 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
10122 strcmp (insn->name, insn[1].name) == 0)
10123 {
10124 ++insn;
10125 s = argsstart;
10126 continue;
10127 }
10128
10129 insn_error = _("illegal operands");
10130
10131 return;
10132 }
10133}
10134
10135/* This structure holds information we know about a mips16 immediate
10136 argument type. */
10137
e972090a
NC
10138struct mips16_immed_operand
10139{
252b5132
RH
10140 /* The type code used in the argument string in the opcode table. */
10141 int type;
10142 /* The number of bits in the short form of the opcode. */
10143 int nbits;
10144 /* The number of bits in the extended form of the opcode. */
10145 int extbits;
10146 /* The amount by which the short form is shifted when it is used;
10147 for example, the sw instruction has a shift count of 2. */
10148 int shift;
10149 /* The amount by which the short form is shifted when it is stored
10150 into the instruction code. */
10151 int op_shift;
10152 /* Non-zero if the short form is unsigned. */
10153 int unsp;
10154 /* Non-zero if the extended form is unsigned. */
10155 int extu;
10156 /* Non-zero if the value is PC relative. */
10157 int pcrel;
10158};
10159
10160/* The mips16 immediate operand types. */
10161
10162static const struct mips16_immed_operand mips16_immed_operands[] =
10163{
10164 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
10165 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 },
10166 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
10167 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 },
10168 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
10169 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
10170 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
10171 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
10172 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
10173 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
10174 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
10175 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
10176 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
10177 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
10178 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
10179 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
10180 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
10181 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
10182 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
10183 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
10184 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
10185};
10186
10187#define MIPS16_NUM_IMMED \
10188 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
10189
10190/* Handle a mips16 instruction with an immediate value. This or's the
10191 small immediate value into *INSN. It sets *USE_EXTEND to indicate
10192 whether an extended value is needed; if one is needed, it sets
10193 *EXTEND to the value. The argument type is TYPE. The value is VAL.
10194 If SMALL is true, an unextended opcode was explicitly requested.
10195 If EXT is true, an extended opcode was explicitly requested. If
10196 WARN is true, warn if EXT does not match reality. */
10197
10198static void
17a2f251
TS
10199mips16_immed (char *file, unsigned int line, int type, offsetT val,
10200 bfd_boolean warn, bfd_boolean small, bfd_boolean ext,
10201 unsigned long *insn, bfd_boolean *use_extend,
10202 unsigned short *extend)
252b5132
RH
10203{
10204 register const struct mips16_immed_operand *op;
10205 int mintiny, maxtiny;
b34976b6 10206 bfd_boolean needext;
252b5132
RH
10207
10208 op = mips16_immed_operands;
10209 while (op->type != type)
10210 {
10211 ++op;
10212 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
10213 }
10214
10215 if (op->unsp)
10216 {
10217 if (type == '<' || type == '>' || type == '[' || type == ']')
10218 {
10219 mintiny = 1;
10220 maxtiny = 1 << op->nbits;
10221 }
10222 else
10223 {
10224 mintiny = 0;
10225 maxtiny = (1 << op->nbits) - 1;
10226 }
10227 }
10228 else
10229 {
10230 mintiny = - (1 << (op->nbits - 1));
10231 maxtiny = (1 << (op->nbits - 1)) - 1;
10232 }
10233
10234 /* Branch offsets have an implicit 0 in the lowest bit. */
10235 if (type == 'p' || type == 'q')
10236 val /= 2;
10237
10238 if ((val & ((1 << op->shift) - 1)) != 0
10239 || val < (mintiny << op->shift)
10240 || val > (maxtiny << op->shift))
b34976b6 10241 needext = TRUE;
252b5132 10242 else
b34976b6 10243 needext = FALSE;
252b5132
RH
10244
10245 if (warn && ext && ! needext)
beae10d5
KH
10246 as_warn_where (file, line,
10247 _("extended operand requested but not required"));
252b5132
RH
10248 if (small && needext)
10249 as_bad_where (file, line, _("invalid unextended operand value"));
10250
10251 if (small || (! ext && ! needext))
10252 {
10253 int insnval;
10254
b34976b6 10255 *use_extend = FALSE;
252b5132
RH
10256 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
10257 insnval <<= op->op_shift;
10258 *insn |= insnval;
10259 }
10260 else
10261 {
10262 long minext, maxext;
10263 int extval;
10264
10265 if (op->extu)
10266 {
10267 minext = 0;
10268 maxext = (1 << op->extbits) - 1;
10269 }
10270 else
10271 {
10272 minext = - (1 << (op->extbits - 1));
10273 maxext = (1 << (op->extbits - 1)) - 1;
10274 }
10275 if (val < minext || val > maxext)
10276 as_bad_where (file, line,
10277 _("operand value out of range for instruction"));
10278
b34976b6 10279 *use_extend = TRUE;
252b5132
RH
10280 if (op->extbits == 16)
10281 {
10282 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
10283 val &= 0x1f;
10284 }
10285 else if (op->extbits == 15)
10286 {
10287 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
10288 val &= 0xf;
10289 }
10290 else
10291 {
10292 extval = ((val & 0x1f) << 6) | (val & 0x20);
10293 val = 0;
10294 }
10295
10296 *extend = (unsigned short) extval;
10297 *insn |= val;
10298 }
10299}
10300\f
d6f16593 10301struct percent_op_match
ad8d3bb3 10302{
5e0116d5
RS
10303 const char *str;
10304 bfd_reloc_code_real_type reloc;
d6f16593
MR
10305};
10306
10307static const struct percent_op_match mips_percent_op[] =
ad8d3bb3 10308{
5e0116d5 10309 {"%lo", BFD_RELOC_LO16},
ad8d3bb3 10310#ifdef OBJ_ELF
5e0116d5
RS
10311 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
10312 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
10313 {"%call16", BFD_RELOC_MIPS_CALL16},
10314 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
10315 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
10316 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
10317 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
10318 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
10319 {"%got", BFD_RELOC_MIPS_GOT16},
10320 {"%gp_rel", BFD_RELOC_GPREL16},
10321 {"%half", BFD_RELOC_16},
10322 {"%highest", BFD_RELOC_MIPS_HIGHEST},
10323 {"%higher", BFD_RELOC_MIPS_HIGHER},
10324 {"%neg", BFD_RELOC_MIPS_SUB},
3f98094e
DJ
10325 {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
10326 {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
10327 {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
10328 {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
10329 {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
10330 {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
10331 {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
ad8d3bb3 10332#endif
5e0116d5 10333 {"%hi", BFD_RELOC_HI16_S}
ad8d3bb3
TS
10334};
10335
d6f16593
MR
10336static const struct percent_op_match mips16_percent_op[] =
10337{
10338 {"%lo", BFD_RELOC_MIPS16_LO16},
10339 {"%gprel", BFD_RELOC_MIPS16_GPREL},
10340 {"%hi", BFD_RELOC_MIPS16_HI16_S}
10341};
10342
252b5132 10343
5e0116d5
RS
10344/* Return true if *STR points to a relocation operator. When returning true,
10345 move *STR over the operator and store its relocation code in *RELOC.
10346 Leave both *STR and *RELOC alone when returning false. */
10347
10348static bfd_boolean
17a2f251 10349parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
252b5132 10350{
d6f16593
MR
10351 const struct percent_op_match *percent_op;
10352 size_t limit, i;
10353
10354 if (mips_opts.mips16)
10355 {
10356 percent_op = mips16_percent_op;
10357 limit = ARRAY_SIZE (mips16_percent_op);
10358 }
10359 else
10360 {
10361 percent_op = mips_percent_op;
10362 limit = ARRAY_SIZE (mips_percent_op);
10363 }
76b3015f 10364
d6f16593 10365 for (i = 0; i < limit; i++)
5e0116d5 10366 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
394f9b3a 10367 {
3f98094e
DJ
10368 int len = strlen (percent_op[i].str);
10369
10370 if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
10371 continue;
10372
5e0116d5
RS
10373 *str += strlen (percent_op[i].str);
10374 *reloc = percent_op[i].reloc;
394f9b3a 10375
5e0116d5
RS
10376 /* Check whether the output BFD supports this relocation.
10377 If not, issue an error and fall back on something safe. */
10378 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
394f9b3a 10379 {
5e0116d5
RS
10380 as_bad ("relocation %s isn't supported by the current ABI",
10381 percent_op[i].str);
01a3f561 10382 *reloc = BFD_RELOC_UNUSED;
394f9b3a 10383 }
5e0116d5 10384 return TRUE;
394f9b3a 10385 }
5e0116d5 10386 return FALSE;
394f9b3a 10387}
ad8d3bb3 10388
ad8d3bb3 10389
5e0116d5
RS
10390/* Parse string STR as a 16-bit relocatable operand. Store the
10391 expression in *EP and the relocations in the array starting
10392 at RELOC. Return the number of relocation operators used.
ad8d3bb3 10393
01a3f561 10394 On exit, EXPR_END points to the first character after the expression. */
ad8d3bb3 10395
5e0116d5 10396static size_t
17a2f251
TS
10397my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
10398 char *str)
ad8d3bb3 10399{
5e0116d5
RS
10400 bfd_reloc_code_real_type reversed_reloc[3];
10401 size_t reloc_index, i;
09b8f35a
RS
10402 int crux_depth, str_depth;
10403 char *crux;
5e0116d5
RS
10404
10405 /* Search for the start of the main expression, recoding relocations
09b8f35a
RS
10406 in REVERSED_RELOC. End the loop with CRUX pointing to the start
10407 of the main expression and with CRUX_DEPTH containing the number
10408 of open brackets at that point. */
10409 reloc_index = -1;
10410 str_depth = 0;
10411 do
fb1b3232 10412 {
09b8f35a
RS
10413 reloc_index++;
10414 crux = str;
10415 crux_depth = str_depth;
10416
10417 /* Skip over whitespace and brackets, keeping count of the number
10418 of brackets. */
10419 while (*str == ' ' || *str == '\t' || *str == '(')
10420 if (*str++ == '(')
10421 str_depth++;
5e0116d5 10422 }
09b8f35a
RS
10423 while (*str == '%'
10424 && reloc_index < (HAVE_NEWABI ? 3 : 1)
10425 && parse_relocation (&str, &reversed_reloc[reloc_index]));
ad8d3bb3 10426
09b8f35a 10427 my_getExpression (ep, crux);
5e0116d5 10428 str = expr_end;
394f9b3a 10429
5e0116d5 10430 /* Match every open bracket. */
09b8f35a 10431 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
5e0116d5 10432 if (*str++ == ')')
09b8f35a 10433 crux_depth--;
394f9b3a 10434
09b8f35a 10435 if (crux_depth > 0)
5e0116d5 10436 as_bad ("unclosed '('");
394f9b3a 10437
5e0116d5 10438 expr_end = str;
252b5132 10439
01a3f561 10440 if (reloc_index != 0)
64bdfcaf
RS
10441 {
10442 prev_reloc_op_frag = frag_now;
10443 for (i = 0; i < reloc_index; i++)
10444 reloc[i] = reversed_reloc[reloc_index - 1 - i];
10445 }
fb1b3232 10446
5e0116d5 10447 return reloc_index;
252b5132
RH
10448}
10449
10450static void
17a2f251 10451my_getExpression (expressionS *ep, char *str)
252b5132
RH
10452{
10453 char *save_in;
98aa84af 10454 valueT val;
252b5132
RH
10455
10456 save_in = input_line_pointer;
10457 input_line_pointer = str;
10458 expression (ep);
10459 expr_end = input_line_pointer;
10460 input_line_pointer = save_in;
10461
10462 /* If we are in mips16 mode, and this is an expression based on `.',
10463 then we bump the value of the symbol by 1 since that is how other
10464 text symbols are handled. We don't bother to handle complex
10465 expressions, just `.' plus or minus a constant. */
10466 if (mips_opts.mips16
10467 && ep->X_op == O_symbol
10468 && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
10469 && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
49309057
ILT
10470 && symbol_get_frag (ep->X_add_symbol) == frag_now
10471 && symbol_constant_p (ep->X_add_symbol)
98aa84af
AM
10472 && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
10473 S_SET_VALUE (ep->X_add_symbol, val + 1);
252b5132
RH
10474}
10475
10476/* Turn a string in input_line_pointer into a floating point constant
bc0d738a
NC
10477 of type TYPE, and store the appropriate bytes in *LITP. The number
10478 of LITTLENUMS emitted is stored in *SIZEP. An error message is
252b5132
RH
10479 returned, or NULL on OK. */
10480
10481char *
17a2f251 10482md_atof (int type, char *litP, int *sizeP)
252b5132
RH
10483{
10484 int prec;
10485 LITTLENUM_TYPE words[4];
10486 char *t;
10487 int i;
10488
10489 switch (type)
10490 {
10491 case 'f':
10492 prec = 2;
10493 break;
10494
10495 case 'd':
10496 prec = 4;
10497 break;
10498
10499 default:
10500 *sizeP = 0;
10501 return _("bad call to md_atof");
10502 }
10503
10504 t = atof_ieee (input_line_pointer, type, words);
10505 if (t)
10506 input_line_pointer = t;
10507
10508 *sizeP = prec * 2;
10509
10510 if (! target_big_endian)
10511 {
10512 for (i = prec - 1; i >= 0; i--)
10513 {
17a2f251 10514 md_number_to_chars (litP, words[i], 2);
252b5132
RH
10515 litP += 2;
10516 }
10517 }
10518 else
10519 {
10520 for (i = 0; i < prec; i++)
10521 {
17a2f251 10522 md_number_to_chars (litP, words[i], 2);
252b5132
RH
10523 litP += 2;
10524 }
10525 }
bdaaa2e1 10526
252b5132
RH
10527 return NULL;
10528}
10529
10530void
17a2f251 10531md_number_to_chars (char *buf, valueT val, int n)
252b5132
RH
10532{
10533 if (target_big_endian)
10534 number_to_chars_bigendian (buf, val, n);
10535 else
10536 number_to_chars_littleendian (buf, val, n);
10537}
10538\f
ae948b86 10539#ifdef OBJ_ELF
e013f690
TS
10540static int support_64bit_objects(void)
10541{
10542 const char **list, **l;
aa3d8fdf 10543 int yes;
e013f690
TS
10544
10545 list = bfd_target_list ();
10546 for (l = list; *l != NULL; l++)
10547#ifdef TE_TMIPS
10548 /* This is traditional mips */
10549 if (strcmp (*l, "elf64-tradbigmips") == 0
10550 || strcmp (*l, "elf64-tradlittlemips") == 0)
10551#else
10552 if (strcmp (*l, "elf64-bigmips") == 0
10553 || strcmp (*l, "elf64-littlemips") == 0)
10554#endif
10555 break;
aa3d8fdf 10556 yes = (*l != NULL);
e013f690 10557 free (list);
aa3d8fdf 10558 return yes;
e013f690 10559}
ae948b86 10560#endif /* OBJ_ELF */
e013f690 10561
78849248 10562const char *md_shortopts = "O::g::G:";
252b5132 10563
e972090a
NC
10564struct option md_longopts[] =
10565{
f9b4148d
CD
10566 /* Options which specify architecture. */
10567#define OPTION_ARCH_BASE (OPTION_MD_BASE)
10568#define OPTION_MARCH (OPTION_ARCH_BASE + 0)
10569 {"march", required_argument, NULL, OPTION_MARCH},
10570#define OPTION_MTUNE (OPTION_ARCH_BASE + 1)
10571 {"mtune", required_argument, NULL, OPTION_MTUNE},
10572#define OPTION_MIPS1 (OPTION_ARCH_BASE + 2)
252b5132
RH
10573 {"mips0", no_argument, NULL, OPTION_MIPS1},
10574 {"mips1", no_argument, NULL, OPTION_MIPS1},
f9b4148d 10575#define OPTION_MIPS2 (OPTION_ARCH_BASE + 3)
252b5132 10576 {"mips2", no_argument, NULL, OPTION_MIPS2},
f9b4148d 10577#define OPTION_MIPS3 (OPTION_ARCH_BASE + 4)
252b5132 10578 {"mips3", no_argument, NULL, OPTION_MIPS3},
f9b4148d 10579#define OPTION_MIPS4 (OPTION_ARCH_BASE + 5)
252b5132 10580 {"mips4", no_argument, NULL, OPTION_MIPS4},
f9b4148d 10581#define OPTION_MIPS5 (OPTION_ARCH_BASE + 6)
ae948b86 10582 {"mips5", no_argument, NULL, OPTION_MIPS5},
f9b4148d 10583#define OPTION_MIPS32 (OPTION_ARCH_BASE + 7)
ae948b86 10584 {"mips32", no_argument, NULL, OPTION_MIPS32},
f9b4148d 10585#define OPTION_MIPS64 (OPTION_ARCH_BASE + 8)
ae948b86 10586 {"mips64", no_argument, NULL, OPTION_MIPS64},
f9b4148d
CD
10587#define OPTION_MIPS32R2 (OPTION_ARCH_BASE + 9)
10588 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
5f74bc13
CD
10589#define OPTION_MIPS64R2 (OPTION_ARCH_BASE + 10)
10590 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
f9b4148d
CD
10591
10592 /* Options which specify Application Specific Extensions (ASEs). */
5f74bc13 10593#define OPTION_ASE_BASE (OPTION_ARCH_BASE + 11)
f9b4148d
CD
10594#define OPTION_MIPS16 (OPTION_ASE_BASE + 0)
10595 {"mips16", no_argument, NULL, OPTION_MIPS16},
10596#define OPTION_NO_MIPS16 (OPTION_ASE_BASE + 1)
10597 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
10598#define OPTION_MIPS3D (OPTION_ASE_BASE + 2)
10599 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
10600#define OPTION_NO_MIPS3D (OPTION_ASE_BASE + 3)
10601 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
10602#define OPTION_MDMX (OPTION_ASE_BASE + 4)
10603 {"mdmx", no_argument, NULL, OPTION_MDMX},
10604#define OPTION_NO_MDMX (OPTION_ASE_BASE + 5)
10605 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
74cd071d
CF
10606#define OPTION_DSP (OPTION_ASE_BASE + 6)
10607 {"mdsp", no_argument, NULL, OPTION_DSP},
10608#define OPTION_NO_DSP (OPTION_ASE_BASE + 7)
10609 {"mno-dsp", no_argument, NULL, OPTION_NO_DSP},
ef2e4d86
CF
10610#define OPTION_MT (OPTION_ASE_BASE + 8)
10611 {"mmt", no_argument, NULL, OPTION_MT},
10612#define OPTION_NO_MT (OPTION_ASE_BASE + 9)
10613 {"mno-mt", no_argument, NULL, OPTION_NO_MT},
f9b4148d
CD
10614
10615 /* Old-style architecture options. Don't add more of these. */
ef2e4d86 10616#define OPTION_COMPAT_ARCH_BASE (OPTION_ASE_BASE + 10)
f9b4148d
CD
10617#define OPTION_M4650 (OPTION_COMPAT_ARCH_BASE + 0)
10618 {"m4650", no_argument, NULL, OPTION_M4650},
10619#define OPTION_NO_M4650 (OPTION_COMPAT_ARCH_BASE + 1)
10620 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
10621#define OPTION_M4010 (OPTION_COMPAT_ARCH_BASE + 2)
10622 {"m4010", no_argument, NULL, OPTION_M4010},
10623#define OPTION_NO_M4010 (OPTION_COMPAT_ARCH_BASE + 3)
10624 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
10625#define OPTION_M4100 (OPTION_COMPAT_ARCH_BASE + 4)
10626 {"m4100", no_argument, NULL, OPTION_M4100},
10627#define OPTION_NO_M4100 (OPTION_COMPAT_ARCH_BASE + 5)
10628 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
10629#define OPTION_M3900 (OPTION_COMPAT_ARCH_BASE + 6)
10630 {"m3900", no_argument, NULL, OPTION_M3900},
10631#define OPTION_NO_M3900 (OPTION_COMPAT_ARCH_BASE + 7)
10632 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
10633
10634 /* Options which enable bug fixes. */
10635#define OPTION_FIX_BASE (OPTION_COMPAT_ARCH_BASE + 8)
10636#define OPTION_M7000_HILO_FIX (OPTION_FIX_BASE + 0)
10637 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
10638#define OPTION_MNO_7000_HILO_FIX (OPTION_FIX_BASE + 1)
10639 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10640 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
d766e8ec
RS
10641#define OPTION_FIX_VR4120 (OPTION_FIX_BASE + 2)
10642#define OPTION_NO_FIX_VR4120 (OPTION_FIX_BASE + 3)
10643 {"mfix-vr4120", no_argument, NULL, OPTION_FIX_VR4120},
10644 {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
7d8e00cf
RS
10645#define OPTION_FIX_VR4130 (OPTION_FIX_BASE + 4)
10646#define OPTION_NO_FIX_VR4130 (OPTION_FIX_BASE + 5)
10647 {"mfix-vr4130", no_argument, NULL, OPTION_FIX_VR4130},
10648 {"mno-fix-vr4130", no_argument, NULL, OPTION_NO_FIX_VR4130},
f9b4148d
CD
10649
10650 /* Miscellaneous options. */
7d8e00cf 10651#define OPTION_MISC_BASE (OPTION_FIX_BASE + 6)
1ffcab4b 10652#define OPTION_TRAP (OPTION_MISC_BASE + 0)
252b5132
RH
10653 {"trap", no_argument, NULL, OPTION_TRAP},
10654 {"no-break", no_argument, NULL, OPTION_TRAP},
1ffcab4b 10655#define OPTION_BREAK (OPTION_MISC_BASE + 1)
252b5132
RH
10656 {"break", no_argument, NULL, OPTION_BREAK},
10657 {"no-trap", no_argument, NULL, OPTION_BREAK},
1ffcab4b 10658#define OPTION_EB (OPTION_MISC_BASE + 2)
252b5132 10659 {"EB", no_argument, NULL, OPTION_EB},
1ffcab4b 10660#define OPTION_EL (OPTION_MISC_BASE + 3)
252b5132 10661 {"EL", no_argument, NULL, OPTION_EL},
1ffcab4b 10662#define OPTION_FP32 (OPTION_MISC_BASE + 4)
ae948b86 10663 {"mfp32", no_argument, NULL, OPTION_FP32},
1ffcab4b 10664#define OPTION_GP32 (OPTION_MISC_BASE + 5)
c97ef257 10665 {"mgp32", no_argument, NULL, OPTION_GP32},
1ffcab4b 10666#define OPTION_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 6)
119d663a 10667 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
1ffcab4b 10668#define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 7)
119d663a 10669 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
1ffcab4b 10670#define OPTION_FP64 (OPTION_MISC_BASE + 8)
316f5878 10671 {"mfp64", no_argument, NULL, OPTION_FP64},
1ffcab4b 10672#define OPTION_GP64 (OPTION_MISC_BASE + 9)
ae948b86 10673 {"mgp64", no_argument, NULL, OPTION_GP64},
1ffcab4b
CD
10674#define OPTION_RELAX_BRANCH (OPTION_MISC_BASE + 10)
10675#define OPTION_NO_RELAX_BRANCH (OPTION_MISC_BASE + 11)
4a6a3df4
AO
10676 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
10677 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
aa6975fb
ILT
10678#define OPTION_MSHARED (OPTION_MISC_BASE + 12)
10679#define OPTION_MNO_SHARED (OPTION_MISC_BASE + 13)
10680 {"mshared", no_argument, NULL, OPTION_MSHARED},
10681 {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
aed1a261
RS
10682#define OPTION_MSYM32 (OPTION_MISC_BASE + 14)
10683#define OPTION_MNO_SYM32 (OPTION_MISC_BASE + 15)
10684 {"msym32", no_argument, NULL, OPTION_MSYM32},
10685 {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
f9b4148d
CD
10686
10687 /* ELF-specific options. */
156c2f8b 10688#ifdef OBJ_ELF
aed1a261 10689#define OPTION_ELF_BASE (OPTION_MISC_BASE + 16)
156c2f8b 10690#define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
156c2f8b
NC
10691 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
10692 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
ae948b86 10693#define OPTION_NON_SHARED (OPTION_ELF_BASE + 1)
156c2f8b 10694 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
ae948b86 10695#define OPTION_XGOT (OPTION_ELF_BASE + 2)
156c2f8b 10696 {"xgot", no_argument, NULL, OPTION_XGOT},
ae948b86
TS
10697#define OPTION_MABI (OPTION_ELF_BASE + 3)
10698 {"mabi", required_argument, NULL, OPTION_MABI},
10699#define OPTION_32 (OPTION_ELF_BASE + 4)
156c2f8b 10700 {"32", no_argument, NULL, OPTION_32},
ae948b86 10701#define OPTION_N32 (OPTION_ELF_BASE + 5)
e013f690 10702 {"n32", no_argument, NULL, OPTION_N32},
ae948b86 10703#define OPTION_64 (OPTION_ELF_BASE + 6)
156c2f8b 10704 {"64", no_argument, NULL, OPTION_64},
ecb4347a
DJ
10705#define OPTION_MDEBUG (OPTION_ELF_BASE + 7)
10706 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
10707#define OPTION_NO_MDEBUG (OPTION_ELF_BASE + 8)
10708 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
dcd410fe
RO
10709#define OPTION_PDR (OPTION_ELF_BASE + 9)
10710 {"mpdr", no_argument, NULL, OPTION_PDR},
10711#define OPTION_NO_PDR (OPTION_ELF_BASE + 10)
10712 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
0a44bf69
RS
10713#define OPTION_MVXWORKS_PIC (OPTION_ELF_BASE + 11)
10714 {"mvxworks-pic", no_argument, NULL, OPTION_MVXWORKS_PIC},
ae948b86 10715#endif /* OBJ_ELF */
f9b4148d 10716
252b5132
RH
10717 {NULL, no_argument, NULL, 0}
10718};
156c2f8b 10719size_t md_longopts_size = sizeof (md_longopts);
252b5132 10720
316f5878
RS
10721/* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
10722 NEW_VALUE. Warn if another value was already specified. Note:
10723 we have to defer parsing the -march and -mtune arguments in order
10724 to handle 'from-abi' correctly, since the ABI might be specified
10725 in a later argument. */
10726
10727static void
17a2f251 10728mips_set_option_string (const char **string_ptr, const char *new_value)
316f5878
RS
10729{
10730 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
10731 as_warn (_("A different %s was already specified, is now %s"),
10732 string_ptr == &mips_arch_string ? "-march" : "-mtune",
10733 new_value);
10734
10735 *string_ptr = new_value;
10736}
10737
252b5132 10738int
17a2f251 10739md_parse_option (int c, char *arg)
252b5132
RH
10740{
10741 switch (c)
10742 {
119d663a
NC
10743 case OPTION_CONSTRUCT_FLOATS:
10744 mips_disable_float_construction = 0;
10745 break;
bdaaa2e1 10746
119d663a
NC
10747 case OPTION_NO_CONSTRUCT_FLOATS:
10748 mips_disable_float_construction = 1;
10749 break;
bdaaa2e1 10750
252b5132
RH
10751 case OPTION_TRAP:
10752 mips_trap = 1;
10753 break;
10754
10755 case OPTION_BREAK:
10756 mips_trap = 0;
10757 break;
10758
10759 case OPTION_EB:
10760 target_big_endian = 1;
10761 break;
10762
10763 case OPTION_EL:
10764 target_big_endian = 0;
10765 break;
10766
10767 case 'O':
10768 if (arg && arg[1] == '0')
10769 mips_optimize = 1;
10770 else
10771 mips_optimize = 2;
10772 break;
10773
10774 case 'g':
10775 if (arg == NULL)
10776 mips_debug = 2;
10777 else
10778 mips_debug = atoi (arg);
10779 /* When the MIPS assembler sees -g or -g2, it does not do
10780 optimizations which limit full symbolic debugging. We take
10781 that to be equivalent to -O0. */
10782 if (mips_debug == 2)
10783 mips_optimize = 1;
10784 break;
10785
10786 case OPTION_MIPS1:
316f5878 10787 file_mips_isa = ISA_MIPS1;
252b5132
RH
10788 break;
10789
10790 case OPTION_MIPS2:
316f5878 10791 file_mips_isa = ISA_MIPS2;
252b5132
RH
10792 break;
10793
10794 case OPTION_MIPS3:
316f5878 10795 file_mips_isa = ISA_MIPS3;
252b5132
RH
10796 break;
10797
10798 case OPTION_MIPS4:
316f5878 10799 file_mips_isa = ISA_MIPS4;
e7af610e
NC
10800 break;
10801
84ea6cf2 10802 case OPTION_MIPS5:
316f5878 10803 file_mips_isa = ISA_MIPS5;
84ea6cf2
NC
10804 break;
10805
e7af610e 10806 case OPTION_MIPS32:
316f5878 10807 file_mips_isa = ISA_MIPS32;
252b5132
RH
10808 break;
10809
af7ee8bf
CD
10810 case OPTION_MIPS32R2:
10811 file_mips_isa = ISA_MIPS32R2;
10812 break;
10813
5f74bc13
CD
10814 case OPTION_MIPS64R2:
10815 file_mips_isa = ISA_MIPS64R2;
10816 break;
10817
84ea6cf2 10818 case OPTION_MIPS64:
316f5878 10819 file_mips_isa = ISA_MIPS64;
84ea6cf2
NC
10820 break;
10821
ec68c924 10822 case OPTION_MTUNE:
316f5878
RS
10823 mips_set_option_string (&mips_tune_string, arg);
10824 break;
ec68c924 10825
316f5878
RS
10826 case OPTION_MARCH:
10827 mips_set_option_string (&mips_arch_string, arg);
252b5132
RH
10828 break;
10829
10830 case OPTION_M4650:
316f5878
RS
10831 mips_set_option_string (&mips_arch_string, "4650");
10832 mips_set_option_string (&mips_tune_string, "4650");
252b5132
RH
10833 break;
10834
10835 case OPTION_NO_M4650:
10836 break;
10837
10838 case OPTION_M4010:
316f5878
RS
10839 mips_set_option_string (&mips_arch_string, "4010");
10840 mips_set_option_string (&mips_tune_string, "4010");
252b5132
RH
10841 break;
10842
10843 case OPTION_NO_M4010:
10844 break;
10845
10846 case OPTION_M4100:
316f5878
RS
10847 mips_set_option_string (&mips_arch_string, "4100");
10848 mips_set_option_string (&mips_tune_string, "4100");
252b5132
RH
10849 break;
10850
10851 case OPTION_NO_M4100:
10852 break;
10853
252b5132 10854 case OPTION_M3900:
316f5878
RS
10855 mips_set_option_string (&mips_arch_string, "3900");
10856 mips_set_option_string (&mips_tune_string, "3900");
252b5132 10857 break;
bdaaa2e1 10858
252b5132
RH
10859 case OPTION_NO_M3900:
10860 break;
10861
deec1734
CD
10862 case OPTION_MDMX:
10863 mips_opts.ase_mdmx = 1;
10864 break;
10865
10866 case OPTION_NO_MDMX:
10867 mips_opts.ase_mdmx = 0;
10868 break;
10869
74cd071d
CF
10870 case OPTION_DSP:
10871 mips_opts.ase_dsp = 1;
10872 break;
10873
10874 case OPTION_NO_DSP:
10875 mips_opts.ase_dsp = 0;
10876 break;
10877
ef2e4d86
CF
10878 case OPTION_MT:
10879 mips_opts.ase_mt = 1;
10880 break;
10881
10882 case OPTION_NO_MT:
10883 mips_opts.ase_mt = 0;
10884 break;
10885
252b5132
RH
10886 case OPTION_MIPS16:
10887 mips_opts.mips16 = 1;
7d10b47d 10888 mips_no_prev_insn ();
252b5132
RH
10889 break;
10890
10891 case OPTION_NO_MIPS16:
10892 mips_opts.mips16 = 0;
7d10b47d 10893 mips_no_prev_insn ();
252b5132
RH
10894 break;
10895
1f25f5d3
CD
10896 case OPTION_MIPS3D:
10897 mips_opts.ase_mips3d = 1;
10898 break;
10899
10900 case OPTION_NO_MIPS3D:
10901 mips_opts.ase_mips3d = 0;
10902 break;
10903
d766e8ec
RS
10904 case OPTION_FIX_VR4120:
10905 mips_fix_vr4120 = 1;
60b63b72
RS
10906 break;
10907
d766e8ec
RS
10908 case OPTION_NO_FIX_VR4120:
10909 mips_fix_vr4120 = 0;
60b63b72
RS
10910 break;
10911
7d8e00cf
RS
10912 case OPTION_FIX_VR4130:
10913 mips_fix_vr4130 = 1;
10914 break;
10915
10916 case OPTION_NO_FIX_VR4130:
10917 mips_fix_vr4130 = 0;
10918 break;
10919
4a6a3df4
AO
10920 case OPTION_RELAX_BRANCH:
10921 mips_relax_branch = 1;
10922 break;
10923
10924 case OPTION_NO_RELAX_BRANCH:
10925 mips_relax_branch = 0;
10926 break;
10927
aa6975fb
ILT
10928 case OPTION_MSHARED:
10929 mips_in_shared = TRUE;
10930 break;
10931
10932 case OPTION_MNO_SHARED:
10933 mips_in_shared = FALSE;
10934 break;
10935
aed1a261
RS
10936 case OPTION_MSYM32:
10937 mips_opts.sym32 = TRUE;
10938 break;
10939
10940 case OPTION_MNO_SYM32:
10941 mips_opts.sym32 = FALSE;
10942 break;
10943
0f074f60 10944#ifdef OBJ_ELF
252b5132
RH
10945 /* When generating ELF code, we permit -KPIC and -call_shared to
10946 select SVR4_PIC, and -non_shared to select no PIC. This is
10947 intended to be compatible with Irix 5. */
10948 case OPTION_CALL_SHARED:
10949 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10950 {
10951 as_bad (_("-call_shared is supported only for ELF format"));
10952 return 0;
10953 }
10954 mips_pic = SVR4_PIC;
143d77c5 10955 mips_abicalls = TRUE;
252b5132
RH
10956 break;
10957
10958 case OPTION_NON_SHARED:
10959 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10960 {
10961 as_bad (_("-non_shared is supported only for ELF format"));
10962 return 0;
10963 }
10964 mips_pic = NO_PIC;
143d77c5 10965 mips_abicalls = FALSE;
252b5132
RH
10966 break;
10967
44075ae2
TS
10968 /* The -xgot option tells the assembler to use 32 bit offsets
10969 when accessing the got in SVR4_PIC mode. It is for Irix
252b5132
RH
10970 compatibility. */
10971 case OPTION_XGOT:
10972 mips_big_got = 1;
10973 break;
0f074f60 10974#endif /* OBJ_ELF */
252b5132
RH
10975
10976 case 'G':
6caf9ef4
TS
10977 g_switch_value = atoi (arg);
10978 g_switch_seen = 1;
252b5132
RH
10979 break;
10980
0f074f60 10981#ifdef OBJ_ELF
34ba82a8
TS
10982 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
10983 and -mabi=64. */
252b5132 10984 case OPTION_32:
34ba82a8
TS
10985 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10986 {
10987 as_bad (_("-32 is supported for ELF format only"));
10988 return 0;
10989 }
316f5878 10990 mips_abi = O32_ABI;
252b5132
RH
10991 break;
10992
e013f690 10993 case OPTION_N32:
34ba82a8
TS
10994 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10995 {
10996 as_bad (_("-n32 is supported for ELF format only"));
10997 return 0;
10998 }
316f5878 10999 mips_abi = N32_ABI;
e013f690 11000 break;
252b5132 11001
e013f690 11002 case OPTION_64:
34ba82a8
TS
11003 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11004 {
11005 as_bad (_("-64 is supported for ELF format only"));
11006 return 0;
11007 }
316f5878 11008 mips_abi = N64_ABI;
e013f690
TS
11009 if (! support_64bit_objects())
11010 as_fatal (_("No compiled in support for 64 bit object file format"));
252b5132 11011 break;
ae948b86 11012#endif /* OBJ_ELF */
252b5132 11013
c97ef257 11014 case OPTION_GP32:
a325df1d 11015 file_mips_gp32 = 1;
c97ef257
AH
11016 break;
11017
11018 case OPTION_GP64:
a325df1d 11019 file_mips_gp32 = 0;
c97ef257 11020 break;
252b5132 11021
ca4e0257 11022 case OPTION_FP32:
a325df1d 11023 file_mips_fp32 = 1;
316f5878
RS
11024 break;
11025
11026 case OPTION_FP64:
11027 file_mips_fp32 = 0;
ca4e0257
RS
11028 break;
11029
ae948b86 11030#ifdef OBJ_ELF
252b5132 11031 case OPTION_MABI:
34ba82a8
TS
11032 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11033 {
11034 as_bad (_("-mabi is supported for ELF format only"));
11035 return 0;
11036 }
e013f690 11037 if (strcmp (arg, "32") == 0)
316f5878 11038 mips_abi = O32_ABI;
e013f690 11039 else if (strcmp (arg, "o64") == 0)
316f5878 11040 mips_abi = O64_ABI;
e013f690 11041 else if (strcmp (arg, "n32") == 0)
316f5878 11042 mips_abi = N32_ABI;
e013f690
TS
11043 else if (strcmp (arg, "64") == 0)
11044 {
316f5878 11045 mips_abi = N64_ABI;
e013f690
TS
11046 if (! support_64bit_objects())
11047 as_fatal (_("No compiled in support for 64 bit object file "
11048 "format"));
11049 }
11050 else if (strcmp (arg, "eabi") == 0)
316f5878 11051 mips_abi = EABI_ABI;
e013f690 11052 else
da0e507f
TS
11053 {
11054 as_fatal (_("invalid abi -mabi=%s"), arg);
11055 return 0;
11056 }
252b5132 11057 break;
e013f690 11058#endif /* OBJ_ELF */
252b5132 11059
6b76fefe 11060 case OPTION_M7000_HILO_FIX:
b34976b6 11061 mips_7000_hilo_fix = TRUE;
6b76fefe
CM
11062 break;
11063
9ee72ff1 11064 case OPTION_MNO_7000_HILO_FIX:
b34976b6 11065 mips_7000_hilo_fix = FALSE;
6b76fefe
CM
11066 break;
11067
ecb4347a
DJ
11068#ifdef OBJ_ELF
11069 case OPTION_MDEBUG:
b34976b6 11070 mips_flag_mdebug = TRUE;
ecb4347a
DJ
11071 break;
11072
11073 case OPTION_NO_MDEBUG:
b34976b6 11074 mips_flag_mdebug = FALSE;
ecb4347a 11075 break;
dcd410fe
RO
11076
11077 case OPTION_PDR:
11078 mips_flag_pdr = TRUE;
11079 break;
11080
11081 case OPTION_NO_PDR:
11082 mips_flag_pdr = FALSE;
11083 break;
0a44bf69
RS
11084
11085 case OPTION_MVXWORKS_PIC:
11086 mips_pic = VXWORKS_PIC;
11087 break;
ecb4347a
DJ
11088#endif /* OBJ_ELF */
11089
252b5132
RH
11090 default:
11091 return 0;
11092 }
11093
11094 return 1;
11095}
316f5878
RS
11096\f
11097/* Set up globals to generate code for the ISA or processor
11098 described by INFO. */
252b5132 11099
252b5132 11100static void
17a2f251 11101mips_set_architecture (const struct mips_cpu_info *info)
252b5132 11102{
316f5878 11103 if (info != 0)
252b5132 11104 {
fef14a42
TS
11105 file_mips_arch = info->cpu;
11106 mips_opts.arch = info->cpu;
316f5878 11107 mips_opts.isa = info->isa;
252b5132 11108 }
252b5132
RH
11109}
11110
252b5132 11111
316f5878 11112/* Likewise for tuning. */
252b5132 11113
316f5878 11114static void
17a2f251 11115mips_set_tune (const struct mips_cpu_info *info)
316f5878
RS
11116{
11117 if (info != 0)
fef14a42 11118 mips_tune = info->cpu;
316f5878 11119}
80cc45a5 11120
34ba82a8 11121
252b5132 11122void
17a2f251 11123mips_after_parse_args (void)
e9670677 11124{
fef14a42
TS
11125 const struct mips_cpu_info *arch_info = 0;
11126 const struct mips_cpu_info *tune_info = 0;
11127
e9670677 11128 /* GP relative stuff not working for PE */
6caf9ef4 11129 if (strncmp (TARGET_OS, "pe", 2) == 0)
e9670677 11130 {
6caf9ef4 11131 if (g_switch_seen && g_switch_value != 0)
e9670677
MR
11132 as_bad (_("-G not supported in this configuration."));
11133 g_switch_value = 0;
11134 }
11135
cac012d6
AO
11136 if (mips_abi == NO_ABI)
11137 mips_abi = MIPS_DEFAULT_ABI;
11138
22923709
RS
11139 /* The following code determines the architecture and register size.
11140 Similar code was added to GCC 3.3 (see override_options() in
11141 config/mips/mips.c). The GAS and GCC code should be kept in sync
11142 as much as possible. */
e9670677 11143
316f5878 11144 if (mips_arch_string != 0)
fef14a42 11145 arch_info = mips_parse_cpu ("-march", mips_arch_string);
e9670677 11146
316f5878 11147 if (file_mips_isa != ISA_UNKNOWN)
e9670677 11148 {
316f5878 11149 /* Handle -mipsN. At this point, file_mips_isa contains the
fef14a42 11150 ISA level specified by -mipsN, while arch_info->isa contains
316f5878 11151 the -march selection (if any). */
fef14a42 11152 if (arch_info != 0)
e9670677 11153 {
316f5878
RS
11154 /* -march takes precedence over -mipsN, since it is more descriptive.
11155 There's no harm in specifying both as long as the ISA levels
11156 are the same. */
fef14a42 11157 if (file_mips_isa != arch_info->isa)
316f5878
RS
11158 as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
11159 mips_cpu_info_from_isa (file_mips_isa)->name,
fef14a42 11160 mips_cpu_info_from_isa (arch_info->isa)->name);
e9670677 11161 }
316f5878 11162 else
fef14a42 11163 arch_info = mips_cpu_info_from_isa (file_mips_isa);
e9670677
MR
11164 }
11165
fef14a42
TS
11166 if (arch_info == 0)
11167 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
e9670677 11168
fef14a42 11169 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
316f5878 11170 as_bad ("-march=%s is not compatible with the selected ABI",
fef14a42
TS
11171 arch_info->name);
11172
11173 mips_set_architecture (arch_info);
11174
11175 /* Optimize for file_mips_arch, unless -mtune selects a different processor. */
11176 if (mips_tune_string != 0)
11177 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
e9670677 11178
fef14a42
TS
11179 if (tune_info == 0)
11180 mips_set_tune (arch_info);
11181 else
11182 mips_set_tune (tune_info);
e9670677 11183
316f5878 11184 if (file_mips_gp32 >= 0)
e9670677 11185 {
316f5878
RS
11186 /* The user specified the size of the integer registers. Make sure
11187 it agrees with the ABI and ISA. */
11188 if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
11189 as_bad (_("-mgp64 used with a 32-bit processor"));
11190 else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
11191 as_bad (_("-mgp32 used with a 64-bit ABI"));
11192 else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
11193 as_bad (_("-mgp64 used with a 32-bit ABI"));
e9670677
MR
11194 }
11195 else
11196 {
316f5878
RS
11197 /* Infer the integer register size from the ABI and processor.
11198 Restrict ourselves to 32-bit registers if that's all the
11199 processor has, or if the ABI cannot handle 64-bit registers. */
11200 file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
11201 || !ISA_HAS_64BIT_REGS (mips_opts.isa));
e9670677
MR
11202 }
11203
316f5878
RS
11204 /* ??? GAS treats single-float processors as though they had 64-bit
11205 float registers (although it complains when double-precision
11206 instructions are used). As things stand, saying they have 32-bit
11207 registers would lead to spurious "register must be even" messages.
11208 So here we assume float registers are always the same size as
11209 integer ones, unless the user says otherwise. */
11210 if (file_mips_fp32 < 0)
11211 file_mips_fp32 = file_mips_gp32;
e9670677 11212
316f5878 11213 /* End of GCC-shared inference code. */
e9670677 11214
17a2f251
TS
11215 /* This flag is set when we have a 64-bit capable CPU but use only
11216 32-bit wide registers. Note that EABI does not use it. */
11217 if (ISA_HAS_64BIT_REGS (mips_opts.isa)
11218 && ((mips_abi == NO_ABI && file_mips_gp32 == 1)
11219 || mips_abi == O32_ABI))
316f5878 11220 mips_32bitmode = 1;
e9670677
MR
11221
11222 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
11223 as_bad (_("trap exception not supported at ISA 1"));
11224
e9670677
MR
11225 /* If the selected architecture includes support for ASEs, enable
11226 generation of code for them. */
a4672219 11227 if (mips_opts.mips16 == -1)
fef14a42 11228 mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_arch)) ? 1 : 0;
ffdefa66 11229 if (mips_opts.ase_mips3d == -1)
fef14a42 11230 mips_opts.ase_mips3d = (CPU_HAS_MIPS3D (file_mips_arch)) ? 1 : 0;
ffdefa66 11231 if (mips_opts.ase_mdmx == -1)
fef14a42 11232 mips_opts.ase_mdmx = (CPU_HAS_MDMX (file_mips_arch)) ? 1 : 0;
74cd071d
CF
11233 if (mips_opts.ase_dsp == -1)
11234 mips_opts.ase_dsp = (CPU_HAS_DSP (file_mips_arch)) ? 1 : 0;
ef2e4d86
CF
11235 if (mips_opts.ase_mt == -1)
11236 mips_opts.ase_mt = (CPU_HAS_MT (file_mips_arch)) ? 1 : 0;
e9670677 11237
e9670677 11238 file_mips_isa = mips_opts.isa;
a4672219 11239 file_ase_mips16 = mips_opts.mips16;
e9670677
MR
11240 file_ase_mips3d = mips_opts.ase_mips3d;
11241 file_ase_mdmx = mips_opts.ase_mdmx;
74cd071d 11242 file_ase_dsp = mips_opts.ase_dsp;
ef2e4d86 11243 file_ase_mt = mips_opts.ase_mt;
e9670677
MR
11244 mips_opts.gp32 = file_mips_gp32;
11245 mips_opts.fp32 = file_mips_fp32;
11246
ecb4347a
DJ
11247 if (mips_flag_mdebug < 0)
11248 {
11249#ifdef OBJ_MAYBE_ECOFF
11250 if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
11251 mips_flag_mdebug = 1;
11252 else
11253#endif /* OBJ_MAYBE_ECOFF */
11254 mips_flag_mdebug = 0;
11255 }
e9670677
MR
11256}
11257\f
11258void
17a2f251 11259mips_init_after_args (void)
252b5132
RH
11260{
11261 /* initialize opcodes */
11262 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
beae10d5 11263 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
252b5132
RH
11264}
11265
11266long
17a2f251 11267md_pcrel_from (fixS *fixP)
252b5132 11268{
a7ebbfdf
TS
11269 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
11270 switch (fixP->fx_r_type)
11271 {
11272 case BFD_RELOC_16_PCREL_S2:
11273 case BFD_RELOC_MIPS_JMP:
11274 /* Return the address of the delay slot. */
11275 return addr + 4;
11276 default:
11277 return addr;
11278 }
252b5132
RH
11279}
11280
252b5132
RH
11281/* This is called before the symbol table is processed. In order to
11282 work with gcc when using mips-tfile, we must keep all local labels.
11283 However, in other cases, we want to discard them. If we were
11284 called with -g, but we didn't see any debugging information, it may
11285 mean that gcc is smuggling debugging information through to
11286 mips-tfile, in which case we must generate all local labels. */
11287
11288void
17a2f251 11289mips_frob_file_before_adjust (void)
252b5132
RH
11290{
11291#ifndef NO_ECOFF_DEBUGGING
11292 if (ECOFF_DEBUGGING
11293 && mips_debug != 0
11294 && ! ecoff_debugging_seen)
11295 flag_keep_locals = 1;
11296#endif
11297}
11298
3b91255e 11299/* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
55cf6793 11300 the corresponding LO16 reloc. This is called before md_apply_fix and
3b91255e
RS
11301 tc_gen_reloc. Unmatched relocs can only be generated by use of explicit
11302 relocation operators.
11303
11304 For our purposes, a %lo() expression matches a %got() or %hi()
11305 expression if:
11306
11307 (a) it refers to the same symbol; and
11308 (b) the offset applied in the %lo() expression is no lower than
11309 the offset applied in the %got() or %hi().
11310
11311 (b) allows us to cope with code like:
11312
11313 lui $4,%hi(foo)
11314 lh $4,%lo(foo+2)($4)
11315
11316 ...which is legal on RELA targets, and has a well-defined behaviour
11317 if the user knows that adding 2 to "foo" will not induce a carry to
11318 the high 16 bits.
11319
11320 When several %lo()s match a particular %got() or %hi(), we use the
11321 following rules to distinguish them:
11322
11323 (1) %lo()s with smaller offsets are a better match than %lo()s with
11324 higher offsets.
11325
11326 (2) %lo()s with no matching %got() or %hi() are better than those
11327 that already have a matching %got() or %hi().
11328
11329 (3) later %lo()s are better than earlier %lo()s.
11330
11331 These rules are applied in order.
11332
11333 (1) means, among other things, that %lo()s with identical offsets are
11334 chosen if they exist.
11335
11336 (2) means that we won't associate several high-part relocations with
11337 the same low-part relocation unless there's no alternative. Having
11338 several high parts for the same low part is a GNU extension; this rule
11339 allows careful users to avoid it.
11340
11341 (3) is purely cosmetic. mips_hi_fixup_list is is in reverse order,
11342 with the last high-part relocation being at the front of the list.
11343 It therefore makes sense to choose the last matching low-part
11344 relocation, all other things being equal. It's also easier
11345 to code that way. */
252b5132
RH
11346
11347void
17a2f251 11348mips_frob_file (void)
252b5132
RH
11349{
11350 struct mips_hi_fixup *l;
11351
11352 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
11353 {
11354 segment_info_type *seginfo;
3b91255e
RS
11355 bfd_boolean matched_lo_p;
11356 fixS **hi_pos, **lo_pos, **pos;
252b5132 11357
5919d012 11358 assert (reloc_needs_lo_p (l->fixp->fx_r_type));
252b5132 11359
5919d012
RS
11360 /* If a GOT16 relocation turns out to be against a global symbol,
11361 there isn't supposed to be a matching LO. */
11362 if (l->fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
11363 && !pic_need_relax (l->fixp->fx_addsy, l->seg))
11364 continue;
11365
11366 /* Check quickly whether the next fixup happens to be a matching %lo. */
11367 if (fixup_has_matching_lo_p (l->fixp))
252b5132
RH
11368 continue;
11369
252b5132 11370 seginfo = seg_info (l->seg);
252b5132 11371
3b91255e
RS
11372 /* Set HI_POS to the position of this relocation in the chain.
11373 Set LO_POS to the position of the chosen low-part relocation.
11374 MATCHED_LO_P is true on entry to the loop if *POS is a low-part
11375 relocation that matches an immediately-preceding high-part
11376 relocation. */
11377 hi_pos = NULL;
11378 lo_pos = NULL;
11379 matched_lo_p = FALSE;
11380 for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
11381 {
11382 if (*pos == l->fixp)
11383 hi_pos = pos;
11384
704803a9
MR
11385 if (((*pos)->fx_r_type == BFD_RELOC_LO16
11386 || (*pos)->fx_r_type == BFD_RELOC_MIPS16_LO16)
3b91255e
RS
11387 && (*pos)->fx_addsy == l->fixp->fx_addsy
11388 && (*pos)->fx_offset >= l->fixp->fx_offset
11389 && (lo_pos == NULL
11390 || (*pos)->fx_offset < (*lo_pos)->fx_offset
11391 || (!matched_lo_p
11392 && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
11393 lo_pos = pos;
11394
11395 matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
11396 && fixup_has_matching_lo_p (*pos));
11397 }
11398
11399 /* If we found a match, remove the high-part relocation from its
11400 current position and insert it before the low-part relocation.
11401 Make the offsets match so that fixup_has_matching_lo_p()
11402 will return true.
11403
11404 We don't warn about unmatched high-part relocations since some
11405 versions of gcc have been known to emit dead "lui ...%hi(...)"
11406 instructions. */
11407 if (lo_pos != NULL)
11408 {
11409 l->fixp->fx_offset = (*lo_pos)->fx_offset;
11410 if (l->fixp->fx_next != *lo_pos)
252b5132 11411 {
3b91255e
RS
11412 *hi_pos = l->fixp->fx_next;
11413 l->fixp->fx_next = *lo_pos;
11414 *lo_pos = l->fixp;
252b5132 11415 }
252b5132
RH
11416 }
11417 }
11418}
11419
3e722fb5 11420/* We may have combined relocations without symbols in the N32/N64 ABI.
f6688943 11421 We have to prevent gas from dropping them. */
252b5132 11422
252b5132 11423int
17a2f251 11424mips_force_relocation (fixS *fixp)
252b5132 11425{
ae6063d4 11426 if (generic_force_reloc (fixp))
252b5132
RH
11427 return 1;
11428
f6688943
TS
11429 if (HAVE_NEWABI
11430 && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
11431 && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
11432 || fixp->fx_r_type == BFD_RELOC_HI16_S
11433 || fixp->fx_r_type == BFD_RELOC_LO16))
11434 return 1;
11435
3e722fb5 11436 return 0;
252b5132
RH
11437}
11438
11439/* Apply a fixup to the object file. */
11440
94f592af 11441void
55cf6793 11442md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 11443{
874e8986 11444 bfd_byte *buf;
98aa84af 11445 long insn;
a7ebbfdf 11446 reloc_howto_type *howto;
252b5132 11447
a7ebbfdf
TS
11448 /* We ignore generic BFD relocations we don't know about. */
11449 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
11450 if (! howto)
11451 return;
65551fa4 11452
252b5132
RH
11453 assert (fixP->fx_size == 4
11454 || fixP->fx_r_type == BFD_RELOC_16
11455 || fixP->fx_r_type == BFD_RELOC_64
f6688943
TS
11456 || fixP->fx_r_type == BFD_RELOC_CTOR
11457 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
252b5132 11458 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
a7ebbfdf 11459 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY);
252b5132 11460
a7ebbfdf 11461 buf = (bfd_byte *) (fixP->fx_frag->fr_literal + fixP->fx_where);
252b5132 11462
bad36eac 11463 assert (! fixP->fx_pcrel || fixP->fx_r_type == BFD_RELOC_16_PCREL_S2);
b1dca8ee
RS
11464
11465 /* Don't treat parts of a composite relocation as done. There are two
11466 reasons for this:
11467
11468 (1) The second and third parts will be against 0 (RSS_UNDEF) but
11469 should nevertheless be emitted if the first part is.
11470
11471 (2) In normal usage, composite relocations are never assembly-time
11472 constants. The easiest way of dealing with the pathological
11473 exceptions is to generate a relocation against STN_UNDEF and
11474 leave everything up to the linker. */
bad36eac 11475 if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel && fixP->fx_tcbit == 0)
252b5132
RH
11476 fixP->fx_done = 1;
11477
11478 switch (fixP->fx_r_type)
11479 {
3f98094e
DJ
11480 case BFD_RELOC_MIPS_TLS_GD:
11481 case BFD_RELOC_MIPS_TLS_LDM:
11482 case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
11483 case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
11484 case BFD_RELOC_MIPS_TLS_GOTTPREL:
11485 case BFD_RELOC_MIPS_TLS_TPREL_HI16:
11486 case BFD_RELOC_MIPS_TLS_TPREL_LO16:
11487 S_SET_THREAD_LOCAL (fixP->fx_addsy);
11488 /* fall through */
11489
252b5132 11490 case BFD_RELOC_MIPS_JMP:
e369bcce
TS
11491 case BFD_RELOC_MIPS_SHIFT5:
11492 case BFD_RELOC_MIPS_SHIFT6:
11493 case BFD_RELOC_MIPS_GOT_DISP:
11494 case BFD_RELOC_MIPS_GOT_PAGE:
11495 case BFD_RELOC_MIPS_GOT_OFST:
11496 case BFD_RELOC_MIPS_SUB:
11497 case BFD_RELOC_MIPS_INSERT_A:
11498 case BFD_RELOC_MIPS_INSERT_B:
11499 case BFD_RELOC_MIPS_DELETE:
11500 case BFD_RELOC_MIPS_HIGHEST:
11501 case BFD_RELOC_MIPS_HIGHER:
11502 case BFD_RELOC_MIPS_SCN_DISP:
11503 case BFD_RELOC_MIPS_REL16:
11504 case BFD_RELOC_MIPS_RELGOT:
11505 case BFD_RELOC_MIPS_JALR:
252b5132
RH
11506 case BFD_RELOC_HI16:
11507 case BFD_RELOC_HI16_S:
cdf6fd85 11508 case BFD_RELOC_GPREL16:
252b5132
RH
11509 case BFD_RELOC_MIPS_LITERAL:
11510 case BFD_RELOC_MIPS_CALL16:
11511 case BFD_RELOC_MIPS_GOT16:
cdf6fd85 11512 case BFD_RELOC_GPREL32:
252b5132
RH
11513 case BFD_RELOC_MIPS_GOT_HI16:
11514 case BFD_RELOC_MIPS_GOT_LO16:
11515 case BFD_RELOC_MIPS_CALL_HI16:
11516 case BFD_RELOC_MIPS_CALL_LO16:
11517 case BFD_RELOC_MIPS16_GPREL:
d6f16593
MR
11518 case BFD_RELOC_MIPS16_HI16:
11519 case BFD_RELOC_MIPS16_HI16_S:
252b5132
RH
11520 /* Nothing needed to do. The value comes from the reloc entry */
11521 break;
11522
11523 case BFD_RELOC_MIPS16_JMP:
11524 /* We currently always generate a reloc against a symbol, which
11525 means that we don't want an addend even if the symbol is
11526 defined. */
a7ebbfdf 11527 *valP = 0;
252b5132
RH
11528 break;
11529
252b5132
RH
11530 case BFD_RELOC_64:
11531 /* This is handled like BFD_RELOC_32, but we output a sign
11532 extended value if we are only 32 bits. */
3e722fb5 11533 if (fixP->fx_done)
252b5132
RH
11534 {
11535 if (8 <= sizeof (valueT))
2132e3a3 11536 md_number_to_chars ((char *) buf, *valP, 8);
252b5132
RH
11537 else
11538 {
a7ebbfdf 11539 valueT hiv;
252b5132 11540
a7ebbfdf 11541 if ((*valP & 0x80000000) != 0)
252b5132
RH
11542 hiv = 0xffffffff;
11543 else
11544 hiv = 0;
b215186b 11545 md_number_to_chars ((char *)(buf + (target_big_endian ? 4 : 0)),
a7ebbfdf 11546 *valP, 4);
b215186b 11547 md_number_to_chars ((char *)(buf + (target_big_endian ? 0 : 4)),
a7ebbfdf 11548 hiv, 4);
252b5132
RH
11549 }
11550 }
11551 break;
11552
056350c6 11553 case BFD_RELOC_RVA:
252b5132
RH
11554 case BFD_RELOC_32:
11555 /* If we are deleting this reloc entry, we must fill in the
11556 value now. This can happen if we have a .word which is not
3e722fb5
CD
11557 resolved when it appears but is later defined. */
11558 if (fixP->fx_done)
2132e3a3 11559 md_number_to_chars ((char *) buf, *valP, 4);
252b5132
RH
11560 break;
11561
11562 case BFD_RELOC_16:
11563 /* If we are deleting this reloc entry, we must fill in the
11564 value now. */
252b5132 11565 if (fixP->fx_done)
2132e3a3 11566 md_number_to_chars ((char *) buf, *valP, 2);
252b5132
RH
11567 break;
11568
11569 case BFD_RELOC_LO16:
d6f16593 11570 case BFD_RELOC_MIPS16_LO16:
3e722fb5
CD
11571 /* FIXME: Now that embedded-PIC is gone, some of this code/comment
11572 may be safe to remove, but if so it's not obvious. */
252b5132
RH
11573 /* When handling an embedded PIC switch statement, we can wind
11574 up deleting a LO16 reloc. See the 'o' case in mips_ip. */
11575 if (fixP->fx_done)
11576 {
a7ebbfdf 11577 if (*valP + 0x8000 > 0xffff)
252b5132
RH
11578 as_bad_where (fixP->fx_file, fixP->fx_line,
11579 _("relocation overflow"));
252b5132
RH
11580 if (target_big_endian)
11581 buf += 2;
2132e3a3 11582 md_number_to_chars ((char *) buf, *valP, 2);
252b5132
RH
11583 }
11584 break;
11585
11586 case BFD_RELOC_16_PCREL_S2:
a7ebbfdf 11587 if ((*valP & 0x3) != 0)
cb56d3d3 11588 as_bad_where (fixP->fx_file, fixP->fx_line,
bad36eac 11589 _("Branch to misaligned address (%lx)"), (long) *valP);
cb56d3d3 11590
252b5132
RH
11591 /*
11592 * We need to save the bits in the instruction since fixup_segment()
11593 * might be deleting the relocation entry (i.e., a branch within
11594 * the current segment).
11595 */
a7ebbfdf 11596 if (! fixP->fx_done)
bb2d6cd7 11597 break;
252b5132
RH
11598
11599 /* update old instruction data */
252b5132
RH
11600 if (target_big_endian)
11601 insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
11602 else
11603 insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
11604
a7ebbfdf
TS
11605 if (*valP + 0x20000 <= 0x3ffff)
11606 {
11607 insn |= (*valP >> 2) & 0xffff;
2132e3a3 11608 md_number_to_chars ((char *) buf, insn, 4);
a7ebbfdf
TS
11609 }
11610 else if (mips_pic == NO_PIC
11611 && fixP->fx_done
11612 && fixP->fx_frag->fr_address >= text_section->vma
11613 && (fixP->fx_frag->fr_address
587aac4e 11614 < text_section->vma + bfd_get_section_size (text_section))
a7ebbfdf
TS
11615 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
11616 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
11617 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
252b5132
RH
11618 {
11619 /* The branch offset is too large. If this is an
11620 unconditional branch, and we are not generating PIC code,
11621 we can convert it to an absolute jump instruction. */
a7ebbfdf
TS
11622 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
11623 insn = 0x0c000000; /* jal */
252b5132 11624 else
a7ebbfdf
TS
11625 insn = 0x08000000; /* j */
11626 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
11627 fixP->fx_done = 0;
11628 fixP->fx_addsy = section_symbol (text_section);
11629 *valP += md_pcrel_from (fixP);
2132e3a3 11630 md_number_to_chars ((char *) buf, insn, 4);
a7ebbfdf
TS
11631 }
11632 else
11633 {
11634 /* If we got here, we have branch-relaxation disabled,
11635 and there's nothing we can do to fix this instruction
11636 without turning it into a longer sequence. */
11637 as_bad_where (fixP->fx_file, fixP->fx_line,
11638 _("Branch out of range"));
252b5132 11639 }
252b5132
RH
11640 break;
11641
11642 case BFD_RELOC_VTABLE_INHERIT:
11643 fixP->fx_done = 0;
11644 if (fixP->fx_addsy
11645 && !S_IS_DEFINED (fixP->fx_addsy)
11646 && !S_IS_WEAK (fixP->fx_addsy))
11647 S_SET_WEAK (fixP->fx_addsy);
11648 break;
11649
11650 case BFD_RELOC_VTABLE_ENTRY:
11651 fixP->fx_done = 0;
11652 break;
11653
11654 default:
11655 internalError ();
11656 }
a7ebbfdf
TS
11657
11658 /* Remember value for tc_gen_reloc. */
11659 fixP->fx_addnumber = *valP;
252b5132
RH
11660}
11661
252b5132 11662static symbolS *
17a2f251 11663get_symbol (void)
252b5132
RH
11664{
11665 int c;
11666 char *name;
11667 symbolS *p;
11668
11669 name = input_line_pointer;
11670 c = get_symbol_end ();
11671 p = (symbolS *) symbol_find_or_make (name);
11672 *input_line_pointer = c;
11673 return p;
11674}
11675
11676/* Align the current frag to a given power of two. The MIPS assembler
11677 also automatically adjusts any preceding label. */
11678
11679static void
17a2f251 11680mips_align (int to, int fill, symbolS *label)
252b5132 11681{
7d10b47d 11682 mips_emit_delays ();
252b5132
RH
11683 frag_align (to, fill, 0);
11684 record_alignment (now_seg, to);
11685 if (label != NULL)
11686 {
11687 assert (S_GET_SEGMENT (label) == now_seg);
49309057 11688 symbol_set_frag (label, frag_now);
252b5132
RH
11689 S_SET_VALUE (label, (valueT) frag_now_fix ());
11690 }
11691}
11692
11693/* Align to a given power of two. .align 0 turns off the automatic
11694 alignment used by the data creating pseudo-ops. */
11695
11696static void
17a2f251 11697s_align (int x ATTRIBUTE_UNUSED)
252b5132
RH
11698{
11699 register int temp;
11700 register long temp_fill;
11701 long max_alignment = 15;
11702
11703 /*
11704
67c1ffbe 11705 o Note that the assembler pulls down any immediately preceding label
252b5132
RH
11706 to the aligned address.
11707 o It's not documented but auto alignment is reinstated by
11708 a .align pseudo instruction.
11709 o Note also that after auto alignment is turned off the mips assembler
11710 issues an error on attempt to assemble an improperly aligned data item.
11711 We don't.
11712
11713 */
11714
11715 temp = get_absolute_expression ();
11716 if (temp > max_alignment)
11717 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
11718 else if (temp < 0)
11719 {
11720 as_warn (_("Alignment negative: 0 assumed."));
11721 temp = 0;
11722 }
11723 if (*input_line_pointer == ',')
11724 {
f9419b05 11725 ++input_line_pointer;
252b5132
RH
11726 temp_fill = get_absolute_expression ();
11727 }
11728 else
11729 temp_fill = 0;
11730 if (temp)
11731 {
11732 auto_align = 1;
11733 mips_align (temp, (int) temp_fill,
11734 insn_labels != NULL ? insn_labels->label : NULL);
11735 }
11736 else
11737 {
11738 auto_align = 0;
11739 }
11740
11741 demand_empty_rest_of_line ();
11742}
11743
252b5132 11744static void
17a2f251 11745s_change_sec (int sec)
252b5132
RH
11746{
11747 segT seg;
11748
252b5132
RH
11749#ifdef OBJ_ELF
11750 /* The ELF backend needs to know that we are changing sections, so
11751 that .previous works correctly. We could do something like check
b6ff326e 11752 for an obj_section_change_hook macro, but that might be confusing
252b5132
RH
11753 as it would not be appropriate to use it in the section changing
11754 functions in read.c, since obj-elf.c intercepts those. FIXME:
11755 This should be cleaner, somehow. */
11756 obj_elf_section_change_hook ();
11757#endif
11758
7d10b47d 11759 mips_emit_delays ();
252b5132
RH
11760 switch (sec)
11761 {
11762 case 't':
11763 s_text (0);
11764 break;
11765 case 'd':
11766 s_data (0);
11767 break;
11768 case 'b':
11769 subseg_set (bss_section, (subsegT) get_absolute_expression ());
11770 demand_empty_rest_of_line ();
11771 break;
11772
11773 case 'r':
4d0d148d
TS
11774 seg = subseg_new (RDATA_SECTION_NAME,
11775 (subsegT) get_absolute_expression ());
11776 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
252b5132 11777 {
4d0d148d
TS
11778 bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
11779 | SEC_READONLY | SEC_RELOC
11780 | SEC_DATA));
11781 if (strcmp (TARGET_OS, "elf") != 0)
11782 record_alignment (seg, 4);
252b5132 11783 }
4d0d148d 11784 demand_empty_rest_of_line ();
252b5132
RH
11785 break;
11786
11787 case 's':
4d0d148d
TS
11788 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
11789 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
252b5132 11790 {
4d0d148d
TS
11791 bfd_set_section_flags (stdoutput, seg,
11792 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
11793 if (strcmp (TARGET_OS, "elf") != 0)
11794 record_alignment (seg, 4);
252b5132 11795 }
4d0d148d
TS
11796 demand_empty_rest_of_line ();
11797 break;
252b5132
RH
11798 }
11799
11800 auto_align = 1;
11801}
b34976b6 11802
cca86cc8 11803void
17a2f251 11804s_change_section (int ignore ATTRIBUTE_UNUSED)
cca86cc8 11805{
7ed4a06a 11806#ifdef OBJ_ELF
cca86cc8
SC
11807 char *section_name;
11808 char c;
684022ea 11809 char next_c = 0;
cca86cc8
SC
11810 int section_type;
11811 int section_flag;
11812 int section_entry_size;
11813 int section_alignment;
b34976b6 11814
7ed4a06a
TS
11815 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11816 return;
11817
cca86cc8
SC
11818 section_name = input_line_pointer;
11819 c = get_symbol_end ();
a816d1ed
AO
11820 if (c)
11821 next_c = *(input_line_pointer + 1);
cca86cc8 11822
4cf0dd0d
TS
11823 /* Do we have .section Name<,"flags">? */
11824 if (c != ',' || (c == ',' && next_c == '"'))
cca86cc8 11825 {
4cf0dd0d
TS
11826 /* just after name is now '\0'. */
11827 *input_line_pointer = c;
cca86cc8
SC
11828 input_line_pointer = section_name;
11829 obj_elf_section (ignore);
11830 return;
11831 }
11832 input_line_pointer++;
11833
11834 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
11835 if (c == ',')
11836 section_type = get_absolute_expression ();
11837 else
11838 section_type = 0;
11839 if (*input_line_pointer++ == ',')
11840 section_flag = get_absolute_expression ();
11841 else
11842 section_flag = 0;
11843 if (*input_line_pointer++ == ',')
11844 section_entry_size = get_absolute_expression ();
11845 else
11846 section_entry_size = 0;
11847 if (*input_line_pointer++ == ',')
11848 section_alignment = get_absolute_expression ();
11849 else
11850 section_alignment = 0;
11851
a816d1ed
AO
11852 section_name = xstrdup (section_name);
11853
8ab8a5c8
RS
11854 /* When using the generic form of .section (as implemented by obj-elf.c),
11855 there's no way to set the section type to SHT_MIPS_DWARF. Users have
11856 traditionally had to fall back on the more common @progbits instead.
11857
11858 There's nothing really harmful in this, since bfd will correct
11859 SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file. But it
708587a4 11860 means that, for backwards compatibility, the special_section entries
8ab8a5c8
RS
11861 for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
11862
11863 Even so, we shouldn't force users of the MIPS .section syntax to
11864 incorrectly label the sections as SHT_PROGBITS. The best compromise
11865 seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
11866 generic type-checking code. */
11867 if (section_type == SHT_MIPS_DWARF)
11868 section_type = SHT_PROGBITS;
11869
cca86cc8
SC
11870 obj_elf_change_section (section_name, section_type, section_flag,
11871 section_entry_size, 0, 0, 0);
a816d1ed
AO
11872
11873 if (now_seg->name != section_name)
11874 free (section_name);
7ed4a06a 11875#endif /* OBJ_ELF */
cca86cc8 11876}
252b5132
RH
11877
11878void
17a2f251 11879mips_enable_auto_align (void)
252b5132
RH
11880{
11881 auto_align = 1;
11882}
11883
11884static void
17a2f251 11885s_cons (int log_size)
252b5132
RH
11886{
11887 symbolS *label;
11888
11889 label = insn_labels != NULL ? insn_labels->label : NULL;
7d10b47d 11890 mips_emit_delays ();
252b5132
RH
11891 if (log_size > 0 && auto_align)
11892 mips_align (log_size, 0, label);
11893 mips_clear_insn_labels ();
11894 cons (1 << log_size);
11895}
11896
11897static void
17a2f251 11898s_float_cons (int type)
252b5132
RH
11899{
11900 symbolS *label;
11901
11902 label = insn_labels != NULL ? insn_labels->label : NULL;
11903
7d10b47d 11904 mips_emit_delays ();
252b5132
RH
11905
11906 if (auto_align)
49309057
ILT
11907 {
11908 if (type == 'd')
11909 mips_align (3, 0, label);
11910 else
11911 mips_align (2, 0, label);
11912 }
252b5132
RH
11913
11914 mips_clear_insn_labels ();
11915
11916 float_cons (type);
11917}
11918
11919/* Handle .globl. We need to override it because on Irix 5 you are
11920 permitted to say
11921 .globl foo .text
11922 where foo is an undefined symbol, to mean that foo should be
11923 considered to be the address of a function. */
11924
11925static void
17a2f251 11926s_mips_globl (int x ATTRIBUTE_UNUSED)
252b5132
RH
11927{
11928 char *name;
11929 int c;
11930 symbolS *symbolP;
11931 flagword flag;
11932
8a06b769 11933 do
252b5132 11934 {
8a06b769 11935 name = input_line_pointer;
252b5132 11936 c = get_symbol_end ();
8a06b769
TS
11937 symbolP = symbol_find_or_make (name);
11938 S_SET_EXTERNAL (symbolP);
11939
252b5132 11940 *input_line_pointer = c;
8a06b769 11941 SKIP_WHITESPACE ();
252b5132 11942
8a06b769
TS
11943 /* On Irix 5, every global symbol that is not explicitly labelled as
11944 being a function is apparently labelled as being an object. */
11945 flag = BSF_OBJECT;
252b5132 11946
8a06b769
TS
11947 if (!is_end_of_line[(unsigned char) *input_line_pointer]
11948 && (*input_line_pointer != ','))
11949 {
11950 char *secname;
11951 asection *sec;
11952
11953 secname = input_line_pointer;
11954 c = get_symbol_end ();
11955 sec = bfd_get_section_by_name (stdoutput, secname);
11956 if (sec == NULL)
11957 as_bad (_("%s: no such section"), secname);
11958 *input_line_pointer = c;
11959
11960 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
11961 flag = BSF_FUNCTION;
11962 }
11963
11964 symbol_get_bfdsym (symbolP)->flags |= flag;
11965
11966 c = *input_line_pointer;
11967 if (c == ',')
11968 {
11969 input_line_pointer++;
11970 SKIP_WHITESPACE ();
11971 if (is_end_of_line[(unsigned char) *input_line_pointer])
11972 c = '\n';
11973 }
11974 }
11975 while (c == ',');
252b5132 11976
252b5132
RH
11977 demand_empty_rest_of_line ();
11978}
11979
11980static void
17a2f251 11981s_option (int x ATTRIBUTE_UNUSED)
252b5132
RH
11982{
11983 char *opt;
11984 char c;
11985
11986 opt = input_line_pointer;
11987 c = get_symbol_end ();
11988
11989 if (*opt == 'O')
11990 {
11991 /* FIXME: What does this mean? */
11992 }
11993 else if (strncmp (opt, "pic", 3) == 0)
11994 {
11995 int i;
11996
11997 i = atoi (opt + 3);
11998 if (i == 0)
11999 mips_pic = NO_PIC;
12000 else if (i == 2)
143d77c5 12001 {
252b5132 12002 mips_pic = SVR4_PIC;
143d77c5
EC
12003 mips_abicalls = TRUE;
12004 }
252b5132
RH
12005 else
12006 as_bad (_(".option pic%d not supported"), i);
12007
4d0d148d 12008 if (mips_pic == SVR4_PIC)
252b5132
RH
12009 {
12010 if (g_switch_seen && g_switch_value != 0)
12011 as_warn (_("-G may not be used with SVR4 PIC code"));
12012 g_switch_value = 0;
12013 bfd_set_gp_size (stdoutput, 0);
12014 }
12015 }
12016 else
12017 as_warn (_("Unrecognized option \"%s\""), opt);
12018
12019 *input_line_pointer = c;
12020 demand_empty_rest_of_line ();
12021}
12022
12023/* This structure is used to hold a stack of .set values. */
12024
e972090a
NC
12025struct mips_option_stack
12026{
252b5132
RH
12027 struct mips_option_stack *next;
12028 struct mips_set_options options;
12029};
12030
12031static struct mips_option_stack *mips_opts_stack;
12032
12033/* Handle the .set pseudo-op. */
12034
12035static void
17a2f251 12036s_mipsset (int x ATTRIBUTE_UNUSED)
252b5132
RH
12037{
12038 char *name = input_line_pointer, ch;
12039
12040 while (!is_end_of_line[(unsigned char) *input_line_pointer])
f9419b05 12041 ++input_line_pointer;
252b5132
RH
12042 ch = *input_line_pointer;
12043 *input_line_pointer = '\0';
12044
12045 if (strcmp (name, "reorder") == 0)
12046 {
7d10b47d
RS
12047 if (mips_opts.noreorder)
12048 end_noreorder ();
252b5132
RH
12049 }
12050 else if (strcmp (name, "noreorder") == 0)
12051 {
7d10b47d
RS
12052 if (!mips_opts.noreorder)
12053 start_noreorder ();
252b5132
RH
12054 }
12055 else if (strcmp (name, "at") == 0)
12056 {
12057 mips_opts.noat = 0;
12058 }
12059 else if (strcmp (name, "noat") == 0)
12060 {
12061 mips_opts.noat = 1;
12062 }
12063 else if (strcmp (name, "macro") == 0)
12064 {
12065 mips_opts.warn_about_macros = 0;
12066 }
12067 else if (strcmp (name, "nomacro") == 0)
12068 {
12069 if (mips_opts.noreorder == 0)
12070 as_bad (_("`noreorder' must be set before `nomacro'"));
12071 mips_opts.warn_about_macros = 1;
12072 }
12073 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
12074 {
12075 mips_opts.nomove = 0;
12076 }
12077 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
12078 {
12079 mips_opts.nomove = 1;
12080 }
12081 else if (strcmp (name, "bopt") == 0)
12082 {
12083 mips_opts.nobopt = 0;
12084 }
12085 else if (strcmp (name, "nobopt") == 0)
12086 {
12087 mips_opts.nobopt = 1;
12088 }
12089 else if (strcmp (name, "mips16") == 0
12090 || strcmp (name, "MIPS-16") == 0)
12091 mips_opts.mips16 = 1;
12092 else if (strcmp (name, "nomips16") == 0
12093 || strcmp (name, "noMIPS-16") == 0)
12094 mips_opts.mips16 = 0;
1f25f5d3
CD
12095 else if (strcmp (name, "mips3d") == 0)
12096 mips_opts.ase_mips3d = 1;
12097 else if (strcmp (name, "nomips3d") == 0)
12098 mips_opts.ase_mips3d = 0;
a4672219
TS
12099 else if (strcmp (name, "mdmx") == 0)
12100 mips_opts.ase_mdmx = 1;
12101 else if (strcmp (name, "nomdmx") == 0)
12102 mips_opts.ase_mdmx = 0;
74cd071d
CF
12103 else if (strcmp (name, "dsp") == 0)
12104 mips_opts.ase_dsp = 1;
12105 else if (strcmp (name, "nodsp") == 0)
12106 mips_opts.ase_dsp = 0;
ef2e4d86
CF
12107 else if (strcmp (name, "mt") == 0)
12108 mips_opts.ase_mt = 1;
12109 else if (strcmp (name, "nomt") == 0)
12110 mips_opts.ase_mt = 0;
1a2c1fad 12111 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
252b5132 12112 {
af7ee8bf 12113 int reset = 0;
252b5132 12114
1a2c1fad
CD
12115 /* Permit the user to change the ISA and architecture on the fly.
12116 Needless to say, misuse can cause serious problems. */
81a21e38 12117 if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
af7ee8bf
CD
12118 {
12119 reset = 1;
12120 mips_opts.isa = file_mips_isa;
1a2c1fad 12121 mips_opts.arch = file_mips_arch;
1a2c1fad
CD
12122 }
12123 else if (strncmp (name, "arch=", 5) == 0)
12124 {
12125 const struct mips_cpu_info *p;
12126
12127 p = mips_parse_cpu("internal use", name + 5);
12128 if (!p)
12129 as_bad (_("unknown architecture %s"), name + 5);
12130 else
12131 {
12132 mips_opts.arch = p->cpu;
12133 mips_opts.isa = p->isa;
12134 }
12135 }
81a21e38
TS
12136 else if (strncmp (name, "mips", 4) == 0)
12137 {
12138 const struct mips_cpu_info *p;
12139
12140 p = mips_parse_cpu("internal use", name);
12141 if (!p)
12142 as_bad (_("unknown ISA level %s"), name + 4);
12143 else
12144 {
12145 mips_opts.arch = p->cpu;
12146 mips_opts.isa = p->isa;
12147 }
12148 }
af7ee8bf 12149 else
81a21e38 12150 as_bad (_("unknown ISA or architecture %s"), name);
af7ee8bf
CD
12151
12152 switch (mips_opts.isa)
98d3f06f
KH
12153 {
12154 case 0:
98d3f06f 12155 break;
af7ee8bf
CD
12156 case ISA_MIPS1:
12157 case ISA_MIPS2:
12158 case ISA_MIPS32:
12159 case ISA_MIPS32R2:
98d3f06f
KH
12160 mips_opts.gp32 = 1;
12161 mips_opts.fp32 = 1;
12162 break;
af7ee8bf
CD
12163 case ISA_MIPS3:
12164 case ISA_MIPS4:
12165 case ISA_MIPS5:
12166 case ISA_MIPS64:
5f74bc13 12167 case ISA_MIPS64R2:
98d3f06f
KH
12168 mips_opts.gp32 = 0;
12169 mips_opts.fp32 = 0;
12170 break;
12171 default:
12172 as_bad (_("unknown ISA level %s"), name + 4);
12173 break;
12174 }
af7ee8bf 12175 if (reset)
98d3f06f 12176 {
af7ee8bf
CD
12177 mips_opts.gp32 = file_mips_gp32;
12178 mips_opts.fp32 = file_mips_fp32;
98d3f06f 12179 }
252b5132
RH
12180 }
12181 else if (strcmp (name, "autoextend") == 0)
12182 mips_opts.noautoextend = 0;
12183 else if (strcmp (name, "noautoextend") == 0)
12184 mips_opts.noautoextend = 1;
12185 else if (strcmp (name, "push") == 0)
12186 {
12187 struct mips_option_stack *s;
12188
12189 s = (struct mips_option_stack *) xmalloc (sizeof *s);
12190 s->next = mips_opts_stack;
12191 s->options = mips_opts;
12192 mips_opts_stack = s;
12193 }
12194 else if (strcmp (name, "pop") == 0)
12195 {
12196 struct mips_option_stack *s;
12197
12198 s = mips_opts_stack;
12199 if (s == NULL)
12200 as_bad (_(".set pop with no .set push"));
12201 else
12202 {
12203 /* If we're changing the reorder mode we need to handle
12204 delay slots correctly. */
12205 if (s->options.noreorder && ! mips_opts.noreorder)
7d10b47d 12206 start_noreorder ();
252b5132 12207 else if (! s->options.noreorder && mips_opts.noreorder)
7d10b47d 12208 end_noreorder ();
252b5132
RH
12209
12210 mips_opts = s->options;
12211 mips_opts_stack = s->next;
12212 free (s);
12213 }
12214 }
aed1a261
RS
12215 else if (strcmp (name, "sym32") == 0)
12216 mips_opts.sym32 = TRUE;
12217 else if (strcmp (name, "nosym32") == 0)
12218 mips_opts.sym32 = FALSE;
252b5132
RH
12219 else
12220 {
12221 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
12222 }
12223 *input_line_pointer = ch;
12224 demand_empty_rest_of_line ();
12225}
12226
12227/* Handle the .abicalls pseudo-op. I believe this is equivalent to
12228 .option pic2. It means to generate SVR4 PIC calls. */
12229
12230static void
17a2f251 12231s_abicalls (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
12232{
12233 mips_pic = SVR4_PIC;
143d77c5 12234 mips_abicalls = TRUE;
4d0d148d
TS
12235
12236 if (g_switch_seen && g_switch_value != 0)
12237 as_warn (_("-G may not be used with SVR4 PIC code"));
12238 g_switch_value = 0;
12239
252b5132
RH
12240 bfd_set_gp_size (stdoutput, 0);
12241 demand_empty_rest_of_line ();
12242}
12243
12244/* Handle the .cpload pseudo-op. This is used when generating SVR4
12245 PIC code. It sets the $gp register for the function based on the
12246 function address, which is in the register named in the argument.
12247 This uses a relocation against _gp_disp, which is handled specially
12248 by the linker. The result is:
12249 lui $gp,%hi(_gp_disp)
12250 addiu $gp,$gp,%lo(_gp_disp)
12251 addu $gp,$gp,.cpload argument
aa6975fb
ILT
12252 The .cpload argument is normally $25 == $t9.
12253
12254 The -mno-shared option changes this to:
bbe506e8
TS
12255 lui $gp,%hi(__gnu_local_gp)
12256 addiu $gp,$gp,%lo(__gnu_local_gp)
aa6975fb
ILT
12257 and the argument is ignored. This saves an instruction, but the
12258 resulting code is not position independent; it uses an absolute
bbe506e8
TS
12259 address for __gnu_local_gp. Thus code assembled with -mno-shared
12260 can go into an ordinary executable, but not into a shared library. */
252b5132
RH
12261
12262static void
17a2f251 12263s_cpload (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
12264{
12265 expressionS ex;
aa6975fb
ILT
12266 int reg;
12267 int in_shared;
252b5132 12268
6478892d
TS
12269 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12270 .cpload is ignored. */
12271 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
12272 {
12273 s_ignore (0);
12274 return;
12275 }
12276
d3ecfc59 12277 /* .cpload should be in a .set noreorder section. */
252b5132
RH
12278 if (mips_opts.noreorder == 0)
12279 as_warn (_(".cpload not in noreorder section"));
12280
aa6975fb
ILT
12281 reg = tc_get_register (0);
12282
12283 /* If we need to produce a 64-bit address, we are better off using
12284 the default instruction sequence. */
aed1a261 12285 in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
aa6975fb 12286
252b5132 12287 ex.X_op = O_symbol;
bbe506e8
TS
12288 ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
12289 "__gnu_local_gp");
252b5132
RH
12290 ex.X_op_symbol = NULL;
12291 ex.X_add_number = 0;
12292
12293 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
49309057 12294 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
252b5132 12295
584892a6 12296 macro_start ();
67c0d1eb
RS
12297 macro_build_lui (&ex, mips_gp_register);
12298 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
17a2f251 12299 mips_gp_register, BFD_RELOC_LO16);
aa6975fb
ILT
12300 if (in_shared)
12301 macro_build (NULL, "addu", "d,v,t", mips_gp_register,
12302 mips_gp_register, reg);
584892a6 12303 macro_end ();
252b5132
RH
12304
12305 demand_empty_rest_of_line ();
12306}
12307
6478892d
TS
12308/* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
12309 .cpsetup $reg1, offset|$reg2, label
12310
12311 If offset is given, this results in:
12312 sd $gp, offset($sp)
956cd1d6 12313 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
12314 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
12315 daddu $gp, $gp, $reg1
6478892d
TS
12316
12317 If $reg2 is given, this results in:
12318 daddu $reg2, $gp, $0
956cd1d6 12319 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
12320 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
12321 daddu $gp, $gp, $reg1
aa6975fb
ILT
12322 $reg1 is normally $25 == $t9.
12323
12324 The -mno-shared option replaces the last three instructions with
12325 lui $gp,%hi(_gp)
12326 addiu $gp,$gp,%lo(_gp)
12327 */
12328
6478892d 12329static void
17a2f251 12330s_cpsetup (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
12331{
12332 expressionS ex_off;
12333 expressionS ex_sym;
12334 int reg1;
6478892d 12335
8586fc66 12336 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
6478892d
TS
12337 We also need NewABI support. */
12338 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12339 {
12340 s_ignore (0);
12341 return;
12342 }
12343
12344 reg1 = tc_get_register (0);
12345 SKIP_WHITESPACE ();
12346 if (*input_line_pointer != ',')
12347 {
12348 as_bad (_("missing argument separator ',' for .cpsetup"));
12349 return;
12350 }
12351 else
80245285 12352 ++input_line_pointer;
6478892d
TS
12353 SKIP_WHITESPACE ();
12354 if (*input_line_pointer == '$')
80245285
TS
12355 {
12356 mips_cpreturn_register = tc_get_register (0);
12357 mips_cpreturn_offset = -1;
12358 }
6478892d 12359 else
80245285
TS
12360 {
12361 mips_cpreturn_offset = get_absolute_expression ();
12362 mips_cpreturn_register = -1;
12363 }
6478892d
TS
12364 SKIP_WHITESPACE ();
12365 if (*input_line_pointer != ',')
12366 {
12367 as_bad (_("missing argument separator ',' for .cpsetup"));
12368 return;
12369 }
12370 else
f9419b05 12371 ++input_line_pointer;
6478892d 12372 SKIP_WHITESPACE ();
f21f8242 12373 expression (&ex_sym);
6478892d 12374
584892a6 12375 macro_start ();
6478892d
TS
12376 if (mips_cpreturn_register == -1)
12377 {
12378 ex_off.X_op = O_constant;
12379 ex_off.X_add_symbol = NULL;
12380 ex_off.X_op_symbol = NULL;
12381 ex_off.X_add_number = mips_cpreturn_offset;
12382
67c0d1eb 12383 macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
17a2f251 12384 BFD_RELOC_LO16, SP);
6478892d
TS
12385 }
12386 else
67c0d1eb 12387 macro_build (NULL, "daddu", "d,v,t", mips_cpreturn_register,
17a2f251 12388 mips_gp_register, 0);
6478892d 12389
aed1a261 12390 if (mips_in_shared || HAVE_64BIT_SYMBOLS)
aa6975fb
ILT
12391 {
12392 macro_build (&ex_sym, "lui", "t,u", mips_gp_register,
12393 -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
12394 BFD_RELOC_HI16_S);
12395
12396 macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
12397 mips_gp_register, -1, BFD_RELOC_GPREL16,
12398 BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
12399
12400 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
12401 mips_gp_register, reg1);
12402 }
12403 else
12404 {
12405 expressionS ex;
12406
12407 ex.X_op = O_symbol;
4184909a 12408 ex.X_add_symbol = symbol_find_or_make ("__gnu_local_gp");
aa6975fb
ILT
12409 ex.X_op_symbol = NULL;
12410 ex.X_add_number = 0;
6e1304d8 12411
aa6975fb
ILT
12412 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
12413 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
12414
12415 macro_build_lui (&ex, mips_gp_register);
12416 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
12417 mips_gp_register, BFD_RELOC_LO16);
12418 }
f21f8242 12419
584892a6 12420 macro_end ();
6478892d
TS
12421
12422 demand_empty_rest_of_line ();
12423}
12424
12425static void
17a2f251 12426s_cplocal (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
12427{
12428 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
12429 .cplocal is ignored. */
12430 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12431 {
12432 s_ignore (0);
12433 return;
12434 }
12435
12436 mips_gp_register = tc_get_register (0);
85b51719 12437 demand_empty_rest_of_line ();
6478892d
TS
12438}
12439
252b5132
RH
12440/* Handle the .cprestore pseudo-op. This stores $gp into a given
12441 offset from $sp. The offset is remembered, and after making a PIC
12442 call $gp is restored from that location. */
12443
12444static void
17a2f251 12445s_cprestore (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
12446{
12447 expressionS ex;
252b5132 12448
6478892d 12449 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
c9914766 12450 .cprestore is ignored. */
6478892d 12451 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
12452 {
12453 s_ignore (0);
12454 return;
12455 }
12456
12457 mips_cprestore_offset = get_absolute_expression ();
7a621144 12458 mips_cprestore_valid = 1;
252b5132
RH
12459
12460 ex.X_op = O_constant;
12461 ex.X_add_symbol = NULL;
12462 ex.X_op_symbol = NULL;
12463 ex.X_add_number = mips_cprestore_offset;
12464
584892a6 12465 macro_start ();
67c0d1eb
RS
12466 macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
12467 SP, HAVE_64BIT_ADDRESSES);
584892a6 12468 macro_end ();
252b5132
RH
12469
12470 demand_empty_rest_of_line ();
12471}
12472
6478892d 12473/* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
67c1ffbe 12474 was given in the preceding .cpsetup, it results in:
6478892d 12475 ld $gp, offset($sp)
76b3015f 12476
6478892d 12477 If a register $reg2 was given there, it results in:
609f23f4 12478 daddu $gp, $reg2, $0
6478892d
TS
12479 */
12480static void
17a2f251 12481s_cpreturn (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
12482{
12483 expressionS ex;
6478892d
TS
12484
12485 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
12486 We also need NewABI support. */
12487 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12488 {
12489 s_ignore (0);
12490 return;
12491 }
12492
584892a6 12493 macro_start ();
6478892d
TS
12494 if (mips_cpreturn_register == -1)
12495 {
12496 ex.X_op = O_constant;
12497 ex.X_add_symbol = NULL;
12498 ex.X_op_symbol = NULL;
12499 ex.X_add_number = mips_cpreturn_offset;
12500
67c0d1eb 12501 macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
6478892d
TS
12502 }
12503 else
67c0d1eb 12504 macro_build (NULL, "daddu", "d,v,t", mips_gp_register,
17a2f251 12505 mips_cpreturn_register, 0);
584892a6 12506 macro_end ();
6478892d
TS
12507
12508 demand_empty_rest_of_line ();
12509}
12510
12511/* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
12512 code. It sets the offset to use in gp_rel relocations. */
12513
12514static void
17a2f251 12515s_gpvalue (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
12516{
12517 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
12518 We also need NewABI support. */
12519 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12520 {
12521 s_ignore (0);
12522 return;
12523 }
12524
def2e0dd 12525 mips_gprel_offset = get_absolute_expression ();
6478892d
TS
12526
12527 demand_empty_rest_of_line ();
12528}
12529
252b5132
RH
12530/* Handle the .gpword pseudo-op. This is used when generating PIC
12531 code. It generates a 32 bit GP relative reloc. */
12532
12533static void
17a2f251 12534s_gpword (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
12535{
12536 symbolS *label;
12537 expressionS ex;
12538 char *p;
12539
12540 /* When not generating PIC code, this is treated as .word. */
12541 if (mips_pic != SVR4_PIC)
12542 {
12543 s_cons (2);
12544 return;
12545 }
12546
12547 label = insn_labels != NULL ? insn_labels->label : NULL;
7d10b47d 12548 mips_emit_delays ();
252b5132
RH
12549 if (auto_align)
12550 mips_align (2, 0, label);
12551 mips_clear_insn_labels ();
12552
12553 expression (&ex);
12554
12555 if (ex.X_op != O_symbol || ex.X_add_number != 0)
12556 {
12557 as_bad (_("Unsupported use of .gpword"));
12558 ignore_rest_of_line ();
12559 }
12560
12561 p = frag_more (4);
17a2f251 12562 md_number_to_chars (p, 0, 4);
b34976b6 12563 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
cdf6fd85 12564 BFD_RELOC_GPREL32);
252b5132
RH
12565
12566 demand_empty_rest_of_line ();
12567}
12568
10181a0d 12569static void
17a2f251 12570s_gpdword (int ignore ATTRIBUTE_UNUSED)
10181a0d
AO
12571{
12572 symbolS *label;
12573 expressionS ex;
12574 char *p;
12575
12576 /* When not generating PIC code, this is treated as .dword. */
12577 if (mips_pic != SVR4_PIC)
12578 {
12579 s_cons (3);
12580 return;
12581 }
12582
12583 label = insn_labels != NULL ? insn_labels->label : NULL;
7d10b47d 12584 mips_emit_delays ();
10181a0d
AO
12585 if (auto_align)
12586 mips_align (3, 0, label);
12587 mips_clear_insn_labels ();
12588
12589 expression (&ex);
12590
12591 if (ex.X_op != O_symbol || ex.X_add_number != 0)
12592 {
12593 as_bad (_("Unsupported use of .gpdword"));
12594 ignore_rest_of_line ();
12595 }
12596
12597 p = frag_more (8);
17a2f251 12598 md_number_to_chars (p, 0, 8);
a105a300 12599 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
6e1304d8 12600 BFD_RELOC_GPREL32)->fx_tcbit = 1;
10181a0d
AO
12601
12602 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
6e1304d8
RS
12603 fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
12604 FALSE, BFD_RELOC_64)->fx_tcbit = 1;
10181a0d
AO
12605
12606 demand_empty_rest_of_line ();
12607}
12608
252b5132
RH
12609/* Handle the .cpadd pseudo-op. This is used when dealing with switch
12610 tables in SVR4 PIC code. */
12611
12612static void
17a2f251 12613s_cpadd (int ignore ATTRIBUTE_UNUSED)
252b5132 12614{
252b5132
RH
12615 int reg;
12616
10181a0d
AO
12617 /* This is ignored when not generating SVR4 PIC code. */
12618 if (mips_pic != SVR4_PIC)
252b5132
RH
12619 {
12620 s_ignore (0);
12621 return;
12622 }
12623
12624 /* Add $gp to the register named as an argument. */
584892a6 12625 macro_start ();
252b5132 12626 reg = tc_get_register (0);
67c0d1eb 12627 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
584892a6 12628 macro_end ();
252b5132 12629
bdaaa2e1 12630 demand_empty_rest_of_line ();
252b5132
RH
12631}
12632
12633/* Handle the .insn pseudo-op. This marks instruction labels in
12634 mips16 mode. This permits the linker to handle them specially,
12635 such as generating jalx instructions when needed. We also make
12636 them odd for the duration of the assembly, in order to generate the
12637 right sort of code. We will make them even in the adjust_symtab
12638 routine, while leaving them marked. This is convenient for the
12639 debugger and the disassembler. The linker knows to make them odd
12640 again. */
12641
12642static void
17a2f251 12643s_insn (int ignore ATTRIBUTE_UNUSED)
252b5132 12644{
f9419b05 12645 mips16_mark_labels ();
252b5132
RH
12646
12647 demand_empty_rest_of_line ();
12648}
12649
12650/* Handle a .stabn directive. We need these in order to mark a label
12651 as being a mips16 text label correctly. Sometimes the compiler
12652 will emit a label, followed by a .stabn, and then switch sections.
12653 If the label and .stabn are in mips16 mode, then the label is
12654 really a mips16 text label. */
12655
12656static void
17a2f251 12657s_mips_stab (int type)
252b5132 12658{
f9419b05 12659 if (type == 'n')
252b5132
RH
12660 mips16_mark_labels ();
12661
12662 s_stab (type);
12663}
12664
12665/* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
12666 */
12667
12668static void
17a2f251 12669s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
12670{
12671 char *name;
12672 int c;
12673 symbolS *symbolP;
12674 expressionS exp;
12675
12676 name = input_line_pointer;
12677 c = get_symbol_end ();
12678 symbolP = symbol_find_or_make (name);
12679 S_SET_WEAK (symbolP);
12680 *input_line_pointer = c;
12681
12682 SKIP_WHITESPACE ();
12683
12684 if (! is_end_of_line[(unsigned char) *input_line_pointer])
12685 {
12686 if (S_IS_DEFINED (symbolP))
12687 {
956cd1d6 12688 as_bad ("ignoring attempt to redefine symbol %s",
252b5132
RH
12689 S_GET_NAME (symbolP));
12690 ignore_rest_of_line ();
12691 return;
12692 }
bdaaa2e1 12693
252b5132
RH
12694 if (*input_line_pointer == ',')
12695 {
12696 ++input_line_pointer;
12697 SKIP_WHITESPACE ();
12698 }
bdaaa2e1 12699
252b5132
RH
12700 expression (&exp);
12701 if (exp.X_op != O_symbol)
12702 {
12703 as_bad ("bad .weakext directive");
98d3f06f 12704 ignore_rest_of_line ();
252b5132
RH
12705 return;
12706 }
49309057 12707 symbol_set_value_expression (symbolP, &exp);
252b5132
RH
12708 }
12709
12710 demand_empty_rest_of_line ();
12711}
12712
12713/* Parse a register string into a number. Called from the ECOFF code
12714 to parse .frame. The argument is non-zero if this is the frame
12715 register, so that we can record it in mips_frame_reg. */
12716
12717int
17a2f251 12718tc_get_register (int frame)
252b5132
RH
12719{
12720 int reg;
12721
12722 SKIP_WHITESPACE ();
12723 if (*input_line_pointer++ != '$')
12724 {
12725 as_warn (_("expected `$'"));
85b51719 12726 reg = ZERO;
252b5132 12727 }
3882b010 12728 else if (ISDIGIT (*input_line_pointer))
252b5132
RH
12729 {
12730 reg = get_absolute_expression ();
12731 if (reg < 0 || reg >= 32)
12732 {
12733 as_warn (_("Bad register number"));
85b51719 12734 reg = ZERO;
252b5132
RH
12735 }
12736 }
12737 else
12738 {
76db943d 12739 if (strncmp (input_line_pointer, "ra", 2) == 0)
85b51719
TS
12740 {
12741 reg = RA;
12742 input_line_pointer += 2;
12743 }
76db943d 12744 else if (strncmp (input_line_pointer, "fp", 2) == 0)
85b51719
TS
12745 {
12746 reg = FP;
12747 input_line_pointer += 2;
12748 }
252b5132 12749 else if (strncmp (input_line_pointer, "sp", 2) == 0)
85b51719
TS
12750 {
12751 reg = SP;
12752 input_line_pointer += 2;
12753 }
252b5132 12754 else if (strncmp (input_line_pointer, "gp", 2) == 0)
85b51719
TS
12755 {
12756 reg = GP;
12757 input_line_pointer += 2;
12758 }
252b5132 12759 else if (strncmp (input_line_pointer, "at", 2) == 0)
85b51719
TS
12760 {
12761 reg = AT;
12762 input_line_pointer += 2;
12763 }
12764 else if (strncmp (input_line_pointer, "kt0", 3) == 0)
12765 {
12766 reg = KT0;
12767 input_line_pointer += 3;
12768 }
12769 else if (strncmp (input_line_pointer, "kt1", 3) == 0)
12770 {
12771 reg = KT1;
12772 input_line_pointer += 3;
12773 }
12774 else if (strncmp (input_line_pointer, "zero", 4) == 0)
12775 {
12776 reg = ZERO;
12777 input_line_pointer += 4;
12778 }
252b5132
RH
12779 else
12780 {
12781 as_warn (_("Unrecognized register name"));
85b51719
TS
12782 reg = ZERO;
12783 while (ISALNUM(*input_line_pointer))
12784 input_line_pointer++;
252b5132 12785 }
252b5132
RH
12786 }
12787 if (frame)
7a621144
DJ
12788 {
12789 mips_frame_reg = reg != 0 ? reg : SP;
12790 mips_frame_reg_valid = 1;
12791 mips_cprestore_valid = 0;
12792 }
252b5132
RH
12793 return reg;
12794}
12795
12796valueT
17a2f251 12797md_section_align (asection *seg, valueT addr)
252b5132
RH
12798{
12799 int align = bfd_get_section_alignment (stdoutput, seg);
12800
12801#ifdef OBJ_ELF
12802 /* We don't need to align ELF sections to the full alignment.
12803 However, Irix 5 may prefer that we align them at least to a 16
12804 byte boundary. We don't bother to align the sections if we are
12805 targeted for an embedded system. */
12806 if (strcmp (TARGET_OS, "elf") == 0)
12807 return addr;
12808 if (align > 4)
12809 align = 4;
12810#endif
12811
12812 return ((addr + (1 << align) - 1) & (-1 << align));
12813}
12814
12815/* Utility routine, called from above as well. If called while the
12816 input file is still being read, it's only an approximation. (For
12817 example, a symbol may later become defined which appeared to be
12818 undefined earlier.) */
12819
12820static int
17a2f251 12821nopic_need_relax (symbolS *sym, int before_relaxing)
252b5132
RH
12822{
12823 if (sym == 0)
12824 return 0;
12825
4d0d148d 12826 if (g_switch_value > 0)
252b5132
RH
12827 {
12828 const char *symname;
12829 int change;
12830
c9914766 12831 /* Find out whether this symbol can be referenced off the $gp
252b5132
RH
12832 register. It can be if it is smaller than the -G size or if
12833 it is in the .sdata or .sbss section. Certain symbols can
c9914766 12834 not be referenced off the $gp, although it appears as though
252b5132
RH
12835 they can. */
12836 symname = S_GET_NAME (sym);
12837 if (symname != (const char *) NULL
12838 && (strcmp (symname, "eprol") == 0
12839 || strcmp (symname, "etext") == 0
12840 || strcmp (symname, "_gp") == 0
12841 || strcmp (symname, "edata") == 0
12842 || strcmp (symname, "_fbss") == 0
12843 || strcmp (symname, "_fdata") == 0
12844 || strcmp (symname, "_ftext") == 0
12845 || strcmp (symname, "end") == 0
12846 || strcmp (symname, "_gp_disp") == 0))
12847 change = 1;
12848 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
12849 && (0
12850#ifndef NO_ECOFF_DEBUGGING
49309057
ILT
12851 || (symbol_get_obj (sym)->ecoff_extern_size != 0
12852 && (symbol_get_obj (sym)->ecoff_extern_size
12853 <= g_switch_value))
252b5132
RH
12854#endif
12855 /* We must defer this decision until after the whole
12856 file has been read, since there might be a .extern
12857 after the first use of this symbol. */
12858 || (before_relaxing
12859#ifndef NO_ECOFF_DEBUGGING
49309057 12860 && symbol_get_obj (sym)->ecoff_extern_size == 0
252b5132
RH
12861#endif
12862 && S_GET_VALUE (sym) == 0)
12863 || (S_GET_VALUE (sym) != 0
12864 && S_GET_VALUE (sym) <= g_switch_value)))
12865 change = 0;
12866 else
12867 {
12868 const char *segname;
12869
12870 segname = segment_name (S_GET_SEGMENT (sym));
12871 assert (strcmp (segname, ".lit8") != 0
12872 && strcmp (segname, ".lit4") != 0);
12873 change = (strcmp (segname, ".sdata") != 0
fba2b7f9
GK
12874 && strcmp (segname, ".sbss") != 0
12875 && strncmp (segname, ".sdata.", 7) != 0
12876 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
252b5132
RH
12877 }
12878 return change;
12879 }
12880 else
c9914766 12881 /* We are not optimizing for the $gp register. */
252b5132
RH
12882 return 1;
12883}
12884
5919d012
RS
12885
12886/* Return true if the given symbol should be considered local for SVR4 PIC. */
12887
12888static bfd_boolean
17a2f251 12889pic_need_relax (symbolS *sym, asection *segtype)
5919d012
RS
12890{
12891 asection *symsec;
12892 bfd_boolean linkonce;
12893
12894 /* Handle the case of a symbol equated to another symbol. */
12895 while (symbol_equated_reloc_p (sym))
12896 {
12897 symbolS *n;
12898
12899 /* It's possible to get a loop here in a badly written
12900 program. */
12901 n = symbol_get_value_expression (sym)->X_add_symbol;
12902 if (n == sym)
12903 break;
12904 sym = n;
12905 }
12906
12907 symsec = S_GET_SEGMENT (sym);
12908
12909 /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
12910 linkonce = FALSE;
12911 if (symsec != segtype && ! S_IS_LOCAL (sym))
12912 {
12913 if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
12914 != 0)
12915 linkonce = TRUE;
12916
12917 /* The GNU toolchain uses an extension for ELF: a section
12918 beginning with the magic string .gnu.linkonce is a linkonce
12919 section. */
12920 if (strncmp (segment_name (symsec), ".gnu.linkonce",
12921 sizeof ".gnu.linkonce" - 1) == 0)
12922 linkonce = TRUE;
12923 }
12924
12925 /* This must duplicate the test in adjust_reloc_syms. */
12926 return (symsec != &bfd_und_section
12927 && symsec != &bfd_abs_section
12928 && ! bfd_is_com_section (symsec)
12929 && !linkonce
12930#ifdef OBJ_ELF
12931 /* A global or weak symbol is treated as external. */
12932 && (OUTPUT_FLAVOR != bfd_target_elf_flavour
3e722fb5 12933 || (! S_IS_WEAK (sym) && ! S_IS_EXTERNAL (sym)))
5919d012
RS
12934#endif
12935 );
12936}
12937
12938
252b5132
RH
12939/* Given a mips16 variant frag FRAGP, return non-zero if it needs an
12940 extended opcode. SEC is the section the frag is in. */
12941
12942static int
17a2f251 12943mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
252b5132
RH
12944{
12945 int type;
12946 register const struct mips16_immed_operand *op;
12947 offsetT val;
12948 int mintiny, maxtiny;
12949 segT symsec;
98aa84af 12950 fragS *sym_frag;
252b5132
RH
12951
12952 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
12953 return 0;
12954 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
12955 return 1;
12956
12957 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12958 op = mips16_immed_operands;
12959 while (op->type != type)
12960 {
12961 ++op;
12962 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
12963 }
12964
12965 if (op->unsp)
12966 {
12967 if (type == '<' || type == '>' || type == '[' || type == ']')
12968 {
12969 mintiny = 1;
12970 maxtiny = 1 << op->nbits;
12971 }
12972 else
12973 {
12974 mintiny = 0;
12975 maxtiny = (1 << op->nbits) - 1;
12976 }
12977 }
12978 else
12979 {
12980 mintiny = - (1 << (op->nbits - 1));
12981 maxtiny = (1 << (op->nbits - 1)) - 1;
12982 }
12983
98aa84af 12984 sym_frag = symbol_get_frag (fragp->fr_symbol);
ac62c346 12985 val = S_GET_VALUE (fragp->fr_symbol);
98aa84af 12986 symsec = S_GET_SEGMENT (fragp->fr_symbol);
252b5132
RH
12987
12988 if (op->pcrel)
12989 {
12990 addressT addr;
12991
12992 /* We won't have the section when we are called from
12993 mips_relax_frag. However, we will always have been called
12994 from md_estimate_size_before_relax first. If this is a
12995 branch to a different section, we mark it as such. If SEC is
12996 NULL, and the frag is not marked, then it must be a branch to
12997 the same section. */
12998 if (sec == NULL)
12999 {
13000 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
13001 return 1;
13002 }
13003 else
13004 {
98aa84af 13005 /* Must have been called from md_estimate_size_before_relax. */
252b5132
RH
13006 if (symsec != sec)
13007 {
13008 fragp->fr_subtype =
13009 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13010
13011 /* FIXME: We should support this, and let the linker
13012 catch branches and loads that are out of range. */
13013 as_bad_where (fragp->fr_file, fragp->fr_line,
13014 _("unsupported PC relative reference to different section"));
13015
13016 return 1;
13017 }
98aa84af
AM
13018 if (fragp != sym_frag && sym_frag->fr_address == 0)
13019 /* Assume non-extended on the first relaxation pass.
13020 The address we have calculated will be bogus if this is
13021 a forward branch to another frag, as the forward frag
13022 will have fr_address == 0. */
13023 return 0;
252b5132
RH
13024 }
13025
13026 /* In this case, we know for sure that the symbol fragment is in
98aa84af
AM
13027 the same section. If the relax_marker of the symbol fragment
13028 differs from the relax_marker of this fragment, we have not
13029 yet adjusted the symbol fragment fr_address. We want to add
252b5132
RH
13030 in STRETCH in order to get a better estimate of the address.
13031 This particularly matters because of the shift bits. */
13032 if (stretch != 0
98aa84af 13033 && sym_frag->relax_marker != fragp->relax_marker)
252b5132
RH
13034 {
13035 fragS *f;
13036
13037 /* Adjust stretch for any alignment frag. Note that if have
13038 been expanding the earlier code, the symbol may be
13039 defined in what appears to be an earlier frag. FIXME:
13040 This doesn't handle the fr_subtype field, which specifies
13041 a maximum number of bytes to skip when doing an
13042 alignment. */
98aa84af 13043 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
252b5132
RH
13044 {
13045 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
13046 {
13047 if (stretch < 0)
13048 stretch = - ((- stretch)
13049 & ~ ((1 << (int) f->fr_offset) - 1));
13050 else
13051 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
13052 if (stretch == 0)
13053 break;
13054 }
13055 }
13056 if (f != NULL)
13057 val += stretch;
13058 }
13059
13060 addr = fragp->fr_address + fragp->fr_fix;
13061
13062 /* The base address rules are complicated. The base address of
13063 a branch is the following instruction. The base address of a
13064 PC relative load or add is the instruction itself, but if it
13065 is in a delay slot (in which case it can not be extended) use
13066 the address of the instruction whose delay slot it is in. */
13067 if (type == 'p' || type == 'q')
13068 {
13069 addr += 2;
13070
13071 /* If we are currently assuming that this frag should be
13072 extended, then, the current address is two bytes
bdaaa2e1 13073 higher. */
252b5132
RH
13074 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13075 addr += 2;
13076
13077 /* Ignore the low bit in the target, since it will be set
13078 for a text label. */
13079 if ((val & 1) != 0)
13080 --val;
13081 }
13082 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13083 addr -= 4;
13084 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13085 addr -= 2;
13086
13087 val -= addr & ~ ((1 << op->shift) - 1);
13088
13089 /* Branch offsets have an implicit 0 in the lowest bit. */
13090 if (type == 'p' || type == 'q')
13091 val /= 2;
13092
13093 /* If any of the shifted bits are set, we must use an extended
13094 opcode. If the address depends on the size of this
13095 instruction, this can lead to a loop, so we arrange to always
13096 use an extended opcode. We only check this when we are in
13097 the main relaxation loop, when SEC is NULL. */
13098 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
13099 {
13100 fragp->fr_subtype =
13101 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13102 return 1;
13103 }
13104
13105 /* If we are about to mark a frag as extended because the value
13106 is precisely maxtiny + 1, then there is a chance of an
13107 infinite loop as in the following code:
13108 la $4,foo
13109 .skip 1020
13110 .align 2
13111 foo:
13112 In this case when the la is extended, foo is 0x3fc bytes
13113 away, so the la can be shrunk, but then foo is 0x400 away, so
13114 the la must be extended. To avoid this loop, we mark the
13115 frag as extended if it was small, and is about to become
13116 extended with a value of maxtiny + 1. */
13117 if (val == ((maxtiny + 1) << op->shift)
13118 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
13119 && sec == NULL)
13120 {
13121 fragp->fr_subtype =
13122 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13123 return 1;
13124 }
13125 }
13126 else if (symsec != absolute_section && sec != NULL)
13127 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
13128
13129 if ((val & ((1 << op->shift) - 1)) != 0
13130 || val < (mintiny << op->shift)
13131 || val > (maxtiny << op->shift))
13132 return 1;
13133 else
13134 return 0;
13135}
13136
4a6a3df4
AO
13137/* Compute the length of a branch sequence, and adjust the
13138 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
13139 worst-case length is computed, with UPDATE being used to indicate
13140 whether an unconditional (-1), branch-likely (+1) or regular (0)
13141 branch is to be computed. */
13142static int
17a2f251 13143relaxed_branch_length (fragS *fragp, asection *sec, int update)
4a6a3df4 13144{
b34976b6 13145 bfd_boolean toofar;
4a6a3df4
AO
13146 int length;
13147
13148 if (fragp
13149 && S_IS_DEFINED (fragp->fr_symbol)
13150 && sec == S_GET_SEGMENT (fragp->fr_symbol))
13151 {
13152 addressT addr;
13153 offsetT val;
13154
13155 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
13156
13157 addr = fragp->fr_address + fragp->fr_fix + 4;
13158
13159 val -= addr;
13160
13161 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
13162 }
13163 else if (fragp)
13164 /* If the symbol is not defined or it's in a different segment,
13165 assume the user knows what's going on and emit a short
13166 branch. */
b34976b6 13167 toofar = FALSE;
4a6a3df4 13168 else
b34976b6 13169 toofar = TRUE;
4a6a3df4
AO
13170
13171 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13172 fragp->fr_subtype
af6ae2ad 13173 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_UNCOND (fragp->fr_subtype),
4a6a3df4
AO
13174 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
13175 RELAX_BRANCH_LINK (fragp->fr_subtype),
13176 toofar);
13177
13178 length = 4;
13179 if (toofar)
13180 {
13181 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
13182 length += 8;
13183
13184 if (mips_pic != NO_PIC)
13185 {
13186 /* Additional space for PIC loading of target address. */
13187 length += 8;
13188 if (mips_opts.isa == ISA_MIPS1)
13189 /* Additional space for $at-stabilizing nop. */
13190 length += 4;
13191 }
13192
13193 /* If branch is conditional. */
13194 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
13195 length += 8;
13196 }
b34976b6 13197
4a6a3df4
AO
13198 return length;
13199}
13200
252b5132
RH
13201/* Estimate the size of a frag before relaxing. Unless this is the
13202 mips16, we are not really relaxing here, and the final size is
13203 encoded in the subtype information. For the mips16, we have to
13204 decide whether we are using an extended opcode or not. */
13205
252b5132 13206int
17a2f251 13207md_estimate_size_before_relax (fragS *fragp, asection *segtype)
252b5132 13208{
5919d012 13209 int change;
252b5132 13210
4a6a3df4
AO
13211 if (RELAX_BRANCH_P (fragp->fr_subtype))
13212 {
13213
b34976b6
AM
13214 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
13215
4a6a3df4
AO
13216 return fragp->fr_var;
13217 }
13218
252b5132 13219 if (RELAX_MIPS16_P (fragp->fr_subtype))
177b4a6a
AO
13220 /* We don't want to modify the EXTENDED bit here; it might get us
13221 into infinite loops. We change it only in mips_relax_frag(). */
13222 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
252b5132
RH
13223
13224 if (mips_pic == NO_PIC)
5919d012 13225 change = nopic_need_relax (fragp->fr_symbol, 0);
252b5132 13226 else if (mips_pic == SVR4_PIC)
5919d012 13227 change = pic_need_relax (fragp->fr_symbol, segtype);
0a44bf69
RS
13228 else if (mips_pic == VXWORKS_PIC)
13229 /* For vxworks, GOT16 relocations never have a corresponding LO16. */
13230 change = 0;
252b5132
RH
13231 else
13232 abort ();
13233
13234 if (change)
13235 {
4d7206a2 13236 fragp->fr_subtype |= RELAX_USE_SECOND;
4d7206a2 13237 return -RELAX_FIRST (fragp->fr_subtype);
252b5132 13238 }
4d7206a2
RS
13239 else
13240 return -RELAX_SECOND (fragp->fr_subtype);
252b5132
RH
13241}
13242
13243/* This is called to see whether a reloc against a defined symbol
de7e6852 13244 should be converted into a reloc against a section. */
252b5132
RH
13245
13246int
17a2f251 13247mips_fix_adjustable (fixS *fixp)
252b5132 13248{
de7e6852
RS
13249 /* Don't adjust MIPS16 jump relocations, so we don't have to worry
13250 about the format of the offset in the .o file. */
252b5132
RH
13251 if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
13252 return 0;
a161fe53 13253
252b5132
RH
13254 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
13255 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13256 return 0;
a161fe53 13257
252b5132
RH
13258 if (fixp->fx_addsy == NULL)
13259 return 1;
a161fe53 13260
de7e6852
RS
13261 /* If symbol SYM is in a mergeable section, relocations of the form
13262 SYM + 0 can usually be made section-relative. The mergeable data
13263 is then identified by the section offset rather than by the symbol.
13264
13265 However, if we're generating REL LO16 relocations, the offset is split
13266 between the LO16 and parterning high part relocation. The linker will
13267 need to recalculate the complete offset in order to correctly identify
13268 the merge data.
13269
13270 The linker has traditionally not looked for the parterning high part
13271 relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
13272 placed anywhere. Rather than break backwards compatibility by changing
13273 this, it seems better not to force the issue, and instead keep the
13274 original symbol. This will work with either linker behavior. */
704803a9
MR
13275 if ((fixp->fx_r_type == BFD_RELOC_LO16
13276 || fixp->fx_r_type == BFD_RELOC_MIPS16_LO16
13277 || reloc_needs_lo_p (fixp->fx_r_type))
de7e6852
RS
13278 && HAVE_IN_PLACE_ADDENDS
13279 && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
13280 return 0;
13281
252b5132 13282#ifdef OBJ_ELF
de7e6852
RS
13283 /* Don't adjust relocations against mips16 symbols, so that the linker
13284 can find them if it needs to set up a stub. */
252b5132
RH
13285 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
13286 && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
13287 && fixp->fx_subsy == NULL)
13288 return 0;
13289#endif
a161fe53 13290
252b5132
RH
13291 return 1;
13292}
13293
13294/* Translate internal representation of relocation info to BFD target
13295 format. */
13296
13297arelent **
17a2f251 13298tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
13299{
13300 static arelent *retval[4];
13301 arelent *reloc;
13302 bfd_reloc_code_real_type code;
13303
4b0cff4e
TS
13304 memset (retval, 0, sizeof(retval));
13305 reloc = retval[0] = (arelent *) xcalloc (1, sizeof (arelent));
49309057
ILT
13306 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13307 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
13308 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
13309
bad36eac
DJ
13310 if (fixp->fx_pcrel)
13311 {
13312 assert (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2);
13313
13314 /* At this point, fx_addnumber is "symbol offset - pcrel address".
13315 Relocations want only the symbol offset. */
13316 reloc->addend = fixp->fx_addnumber + reloc->address;
13317 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
13318 {
13319 /* A gruesome hack which is a result of the gruesome gas
13320 reloc handling. What's worse, for COFF (as opposed to
13321 ECOFF), we might need yet another copy of reloc->address.
13322 See bfd_install_relocation. */
13323 reloc->addend += reloc->address;
13324 }
13325 }
13326 else
13327 reloc->addend = fixp->fx_addnumber;
252b5132 13328
438c16b8
TS
13329 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
13330 entry to be used in the relocation's section offset. */
13331 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
252b5132
RH
13332 {
13333 reloc->address = reloc->addend;
13334 reloc->addend = 0;
13335 }
13336
252b5132 13337 code = fixp->fx_r_type;
252b5132 13338
bad36eac 13339 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
252b5132
RH
13340 if (reloc->howto == NULL)
13341 {
13342 as_bad_where (fixp->fx_file, fixp->fx_line,
13343 _("Can not represent %s relocation in this object file format"),
13344 bfd_get_reloc_code_name (code));
13345 retval[0] = NULL;
13346 }
13347
13348 return retval;
13349}
13350
13351/* Relax a machine dependent frag. This returns the amount by which
13352 the current size of the frag should change. */
13353
13354int
17a2f251 13355mips_relax_frag (asection *sec, fragS *fragp, long stretch)
252b5132 13356{
4a6a3df4
AO
13357 if (RELAX_BRANCH_P (fragp->fr_subtype))
13358 {
13359 offsetT old_var = fragp->fr_var;
b34976b6
AM
13360
13361 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
4a6a3df4
AO
13362
13363 return fragp->fr_var - old_var;
13364 }
13365
252b5132
RH
13366 if (! RELAX_MIPS16_P (fragp->fr_subtype))
13367 return 0;
13368
c4e7957c 13369 if (mips16_extended_frag (fragp, NULL, stretch))
252b5132
RH
13370 {
13371 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13372 return 0;
13373 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
13374 return 2;
13375 }
13376 else
13377 {
13378 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13379 return 0;
13380 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
13381 return -2;
13382 }
13383
13384 return 0;
13385}
13386
13387/* Convert a machine dependent frag. */
13388
13389void
17a2f251 13390md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
252b5132 13391{
4a6a3df4
AO
13392 if (RELAX_BRANCH_P (fragp->fr_subtype))
13393 {
13394 bfd_byte *buf;
13395 unsigned long insn;
13396 expressionS exp;
13397 fixS *fixp;
b34976b6 13398
4a6a3df4
AO
13399 buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
13400
13401 if (target_big_endian)
13402 insn = bfd_getb32 (buf);
13403 else
13404 insn = bfd_getl32 (buf);
b34976b6 13405
4a6a3df4
AO
13406 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13407 {
13408 /* We generate a fixup instead of applying it right now
13409 because, if there are linker relaxations, we're going to
13410 need the relocations. */
13411 exp.X_op = O_symbol;
13412 exp.X_add_symbol = fragp->fr_symbol;
13413 exp.X_add_number = fragp->fr_offset;
13414
13415 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
bad36eac 13416 4, &exp, 1, BFD_RELOC_16_PCREL_S2);
4a6a3df4
AO
13417 fixp->fx_file = fragp->fr_file;
13418 fixp->fx_line = fragp->fr_line;
b34976b6 13419
2132e3a3 13420 md_number_to_chars ((char *) buf, insn, 4);
4a6a3df4
AO
13421 buf += 4;
13422 }
13423 else
13424 {
13425 int i;
13426
13427 as_warn_where (fragp->fr_file, fragp->fr_line,
13428 _("relaxed out-of-range branch into a jump"));
13429
13430 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
13431 goto uncond;
13432
13433 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13434 {
13435 /* Reverse the branch. */
13436 switch ((insn >> 28) & 0xf)
13437 {
13438 case 4:
13439 /* bc[0-3][tf]l? and bc1any[24][ft] instructions can
13440 have the condition reversed by tweaking a single
13441 bit, and their opcodes all have 0x4???????. */
13442 assert ((insn & 0xf1000000) == 0x41000000);
13443 insn ^= 0x00010000;
13444 break;
13445
13446 case 0:
13447 /* bltz 0x04000000 bgez 0x04010000
13448 bltzal 0x04100000 bgezal 0x04110000 */
13449 assert ((insn & 0xfc0e0000) == 0x04000000);
13450 insn ^= 0x00010000;
13451 break;
b34976b6 13452
4a6a3df4
AO
13453 case 1:
13454 /* beq 0x10000000 bne 0x14000000
13455 blez 0x18000000 bgtz 0x1c000000 */
13456 insn ^= 0x04000000;
13457 break;
13458
13459 default:
13460 abort ();
13461 }
13462 }
13463
13464 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13465 {
13466 /* Clear the and-link bit. */
13467 assert ((insn & 0xfc1c0000) == 0x04100000);
13468
13469 /* bltzal 0x04100000 bgezal 0x04110000
13470 bltzall 0x04120000 bgezall 0x04130000 */
13471 insn &= ~0x00100000;
13472 }
13473
13474 /* Branch over the branch (if the branch was likely) or the
13475 full jump (not likely case). Compute the offset from the
13476 current instruction to branch to. */
13477 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13478 i = 16;
13479 else
13480 {
13481 /* How many bytes in instructions we've already emitted? */
13482 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13483 /* How many bytes in instructions from here to the end? */
13484 i = fragp->fr_var - i;
13485 }
13486 /* Convert to instruction count. */
13487 i >>= 2;
13488 /* Branch counts from the next instruction. */
b34976b6 13489 i--;
4a6a3df4
AO
13490 insn |= i;
13491 /* Branch over the jump. */
2132e3a3 13492 md_number_to_chars ((char *) buf, insn, 4);
4a6a3df4
AO
13493 buf += 4;
13494
13495 /* Nop */
2132e3a3 13496 md_number_to_chars ((char *) buf, 0, 4);
4a6a3df4
AO
13497 buf += 4;
13498
13499 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13500 {
13501 /* beql $0, $0, 2f */
13502 insn = 0x50000000;
13503 /* Compute the PC offset from the current instruction to
13504 the end of the variable frag. */
13505 /* How many bytes in instructions we've already emitted? */
13506 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13507 /* How many bytes in instructions from here to the end? */
13508 i = fragp->fr_var - i;
13509 /* Convert to instruction count. */
13510 i >>= 2;
13511 /* Don't decrement i, because we want to branch over the
13512 delay slot. */
13513
13514 insn |= i;
2132e3a3 13515 md_number_to_chars ((char *) buf, insn, 4);
4a6a3df4
AO
13516 buf += 4;
13517
2132e3a3 13518 md_number_to_chars ((char *) buf, 0, 4);
4a6a3df4
AO
13519 buf += 4;
13520 }
13521
13522 uncond:
13523 if (mips_pic == NO_PIC)
13524 {
13525 /* j or jal. */
13526 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
13527 ? 0x0c000000 : 0x08000000);
13528 exp.X_op = O_symbol;
13529 exp.X_add_symbol = fragp->fr_symbol;
13530 exp.X_add_number = fragp->fr_offset;
13531
13532 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13533 4, &exp, 0, BFD_RELOC_MIPS_JMP);
13534 fixp->fx_file = fragp->fr_file;
13535 fixp->fx_line = fragp->fr_line;
13536
2132e3a3 13537 md_number_to_chars ((char *) buf, insn, 4);
4a6a3df4
AO
13538 buf += 4;
13539 }
13540 else
13541 {
13542 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
13543 insn = HAVE_64BIT_ADDRESSES ? 0xdf810000 : 0x8f810000;
13544 exp.X_op = O_symbol;
13545 exp.X_add_symbol = fragp->fr_symbol;
13546 exp.X_add_number = fragp->fr_offset;
13547
13548 if (fragp->fr_offset)
13549 {
13550 exp.X_add_symbol = make_expr_symbol (&exp);
13551 exp.X_add_number = 0;
13552 }
13553
13554 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13555 4, &exp, 0, BFD_RELOC_MIPS_GOT16);
13556 fixp->fx_file = fragp->fr_file;
13557 fixp->fx_line = fragp->fr_line;
13558
2132e3a3 13559 md_number_to_chars ((char *) buf, insn, 4);
4a6a3df4 13560 buf += 4;
b34976b6 13561
4a6a3df4
AO
13562 if (mips_opts.isa == ISA_MIPS1)
13563 {
13564 /* nop */
2132e3a3 13565 md_number_to_chars ((char *) buf, 0, 4);
4a6a3df4
AO
13566 buf += 4;
13567 }
13568
13569 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
13570 insn = HAVE_64BIT_ADDRESSES ? 0x64210000 : 0x24210000;
13571
13572 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13573 4, &exp, 0, BFD_RELOC_LO16);
13574 fixp->fx_file = fragp->fr_file;
13575 fixp->fx_line = fragp->fr_line;
b34976b6 13576
2132e3a3 13577 md_number_to_chars ((char *) buf, insn, 4);
4a6a3df4
AO
13578 buf += 4;
13579
13580 /* j(al)r $at. */
13581 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13582 insn = 0x0020f809;
13583 else
13584 insn = 0x00200008;
13585
2132e3a3 13586 md_number_to_chars ((char *) buf, insn, 4);
4a6a3df4
AO
13587 buf += 4;
13588 }
13589 }
13590
13591 assert (buf == (bfd_byte *)fragp->fr_literal
13592 + fragp->fr_fix + fragp->fr_var);
13593
13594 fragp->fr_fix += fragp->fr_var;
13595
13596 return;
13597 }
13598
252b5132
RH
13599 if (RELAX_MIPS16_P (fragp->fr_subtype))
13600 {
13601 int type;
13602 register const struct mips16_immed_operand *op;
b34976b6 13603 bfd_boolean small, ext;
252b5132
RH
13604 offsetT val;
13605 bfd_byte *buf;
13606 unsigned long insn;
b34976b6 13607 bfd_boolean use_extend;
252b5132
RH
13608 unsigned short extend;
13609
13610 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
13611 op = mips16_immed_operands;
13612 while (op->type != type)
13613 ++op;
13614
13615 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13616 {
b34976b6
AM
13617 small = FALSE;
13618 ext = TRUE;
252b5132
RH
13619 }
13620 else
13621 {
b34976b6
AM
13622 small = TRUE;
13623 ext = FALSE;
252b5132
RH
13624 }
13625
6386f3a7 13626 resolve_symbol_value (fragp->fr_symbol);
252b5132
RH
13627 val = S_GET_VALUE (fragp->fr_symbol);
13628 if (op->pcrel)
13629 {
13630 addressT addr;
13631
13632 addr = fragp->fr_address + fragp->fr_fix;
13633
13634 /* The rules for the base address of a PC relative reloc are
13635 complicated; see mips16_extended_frag. */
13636 if (type == 'p' || type == 'q')
13637 {
13638 addr += 2;
13639 if (ext)
13640 addr += 2;
13641 /* Ignore the low bit in the target, since it will be
13642 set for a text label. */
13643 if ((val & 1) != 0)
13644 --val;
13645 }
13646 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13647 addr -= 4;
13648 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13649 addr -= 2;
13650
13651 addr &= ~ (addressT) ((1 << op->shift) - 1);
13652 val -= addr;
13653
13654 /* Make sure the section winds up with the alignment we have
13655 assumed. */
13656 if (op->shift > 0)
13657 record_alignment (asec, op->shift);
13658 }
13659
13660 if (ext
13661 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
13662 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
13663 as_warn_where (fragp->fr_file, fragp->fr_line,
13664 _("extended instruction in delay slot"));
13665
13666 buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
13667
13668 if (target_big_endian)
13669 insn = bfd_getb16 (buf);
13670 else
13671 insn = bfd_getl16 (buf);
13672
13673 mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
13674 RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
13675 small, ext, &insn, &use_extend, &extend);
13676
13677 if (use_extend)
13678 {
2132e3a3 13679 md_number_to_chars ((char *) buf, 0xf000 | extend, 2);
252b5132
RH
13680 fragp->fr_fix += 2;
13681 buf += 2;
13682 }
13683
2132e3a3 13684 md_number_to_chars ((char *) buf, insn, 2);
252b5132
RH
13685 fragp->fr_fix += 2;
13686 buf += 2;
13687 }
13688 else
13689 {
4d7206a2
RS
13690 int first, second;
13691 fixS *fixp;
252b5132 13692
4d7206a2
RS
13693 first = RELAX_FIRST (fragp->fr_subtype);
13694 second = RELAX_SECOND (fragp->fr_subtype);
13695 fixp = (fixS *) fragp->fr_opcode;
252b5132 13696
584892a6
RS
13697 /* Possibly emit a warning if we've chosen the longer option. */
13698 if (((fragp->fr_subtype & RELAX_USE_SECOND) != 0)
13699 == ((fragp->fr_subtype & RELAX_SECOND_LONGER) != 0))
13700 {
13701 const char *msg = macro_warning (fragp->fr_subtype);
13702 if (msg != 0)
13703 as_warn_where (fragp->fr_file, fragp->fr_line, msg);
13704 }
13705
4d7206a2
RS
13706 /* Go through all the fixups for the first sequence. Disable them
13707 (by marking them as done) if we're going to use the second
13708 sequence instead. */
13709 while (fixp
13710 && fixp->fx_frag == fragp
13711 && fixp->fx_where < fragp->fr_fix - second)
13712 {
13713 if (fragp->fr_subtype & RELAX_USE_SECOND)
13714 fixp->fx_done = 1;
13715 fixp = fixp->fx_next;
13716 }
252b5132 13717
4d7206a2
RS
13718 /* Go through the fixups for the second sequence. Disable them if
13719 we're going to use the first sequence, otherwise adjust their
13720 addresses to account for the relaxation. */
13721 while (fixp && fixp->fx_frag == fragp)
13722 {
13723 if (fragp->fr_subtype & RELAX_USE_SECOND)
13724 fixp->fx_where -= first;
13725 else
13726 fixp->fx_done = 1;
13727 fixp = fixp->fx_next;
13728 }
13729
13730 /* Now modify the frag contents. */
13731 if (fragp->fr_subtype & RELAX_USE_SECOND)
13732 {
13733 char *start;
13734
13735 start = fragp->fr_literal + fragp->fr_fix - first - second;
13736 memmove (start, start + first, second);
13737 fragp->fr_fix -= first;
13738 }
13739 else
13740 fragp->fr_fix -= second;
252b5132
RH
13741 }
13742}
13743
13744#ifdef OBJ_ELF
13745
13746/* This function is called after the relocs have been generated.
13747 We've been storing mips16 text labels as odd. Here we convert them
13748 back to even for the convenience of the debugger. */
13749
13750void
17a2f251 13751mips_frob_file_after_relocs (void)
252b5132
RH
13752{
13753 asymbol **syms;
13754 unsigned int count, i;
13755
13756 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
13757 return;
13758
13759 syms = bfd_get_outsymbols (stdoutput);
13760 count = bfd_get_symcount (stdoutput);
13761 for (i = 0; i < count; i++, syms++)
13762 {
13763 if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
13764 && ((*syms)->value & 1) != 0)
13765 {
13766 (*syms)->value &= ~1;
13767 /* If the symbol has an odd size, it was probably computed
13768 incorrectly, so adjust that as well. */
13769 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
13770 ++elf_symbol (*syms)->internal_elf_sym.st_size;
13771 }
13772 }
13773}
13774
13775#endif
13776
13777/* This function is called whenever a label is defined. It is used
13778 when handling branch delays; if a branch has a label, we assume we
13779 can not move it. */
13780
13781void
17a2f251 13782mips_define_label (symbolS *sym)
252b5132
RH
13783{
13784 struct insn_label_list *l;
13785
13786 if (free_insn_labels == NULL)
13787 l = (struct insn_label_list *) xmalloc (sizeof *l);
13788 else
13789 {
13790 l = free_insn_labels;
13791 free_insn_labels = l->next;
13792 }
13793
13794 l->label = sym;
13795 l->next = insn_labels;
13796 insn_labels = l;
07a53e5c
RH
13797
13798#ifdef OBJ_ELF
13799 dwarf2_emit_label (sym);
13800#endif
252b5132
RH
13801}
13802\f
13803#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13804
13805/* Some special processing for a MIPS ELF file. */
13806
13807void
17a2f251 13808mips_elf_final_processing (void)
252b5132
RH
13809{
13810 /* Write out the register information. */
316f5878 13811 if (mips_abi != N64_ABI)
252b5132
RH
13812 {
13813 Elf32_RegInfo s;
13814
13815 s.ri_gprmask = mips_gprmask;
13816 s.ri_cprmask[0] = mips_cprmask[0];
13817 s.ri_cprmask[1] = mips_cprmask[1];
13818 s.ri_cprmask[2] = mips_cprmask[2];
13819 s.ri_cprmask[3] = mips_cprmask[3];
13820 /* The gp_value field is set by the MIPS ELF backend. */
13821
13822 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
13823 ((Elf32_External_RegInfo *)
13824 mips_regmask_frag));
13825 }
13826 else
13827 {
13828 Elf64_Internal_RegInfo s;
13829
13830 s.ri_gprmask = mips_gprmask;
13831 s.ri_pad = 0;
13832 s.ri_cprmask[0] = mips_cprmask[0];
13833 s.ri_cprmask[1] = mips_cprmask[1];
13834 s.ri_cprmask[2] = mips_cprmask[2];
13835 s.ri_cprmask[3] = mips_cprmask[3];
13836 /* The gp_value field is set by the MIPS ELF backend. */
13837
13838 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
13839 ((Elf64_External_RegInfo *)
13840 mips_regmask_frag));
13841 }
13842
13843 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
13844 sort of BFD interface for this. */
13845 if (mips_any_noreorder)
13846 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
13847 if (mips_pic != NO_PIC)
143d77c5 13848 {
252b5132 13849 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
143d77c5
EC
13850 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
13851 }
13852 if (mips_abicalls)
13853 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
252b5132 13854
98d3f06f 13855 /* Set MIPS ELF flags for ASEs. */
74cd071d
CF
13856 /* We may need to define a new flag for DSP ASE, and set this flag when
13857 file_ase_dsp is true. */
ef2e4d86
CF
13858 /* We may need to define a new flag for MT ASE, and set this flag when
13859 file_ase_mt is true. */
a4672219
TS
13860 if (file_ase_mips16)
13861 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
1f25f5d3
CD
13862#if 0 /* XXX FIXME */
13863 if (file_ase_mips3d)
13864 elf_elfheader (stdoutput)->e_flags |= ???;
13865#endif
deec1734
CD
13866 if (file_ase_mdmx)
13867 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
1f25f5d3 13868
bdaaa2e1 13869 /* Set the MIPS ELF ABI flags. */
316f5878 13870 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
252b5132 13871 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
316f5878 13872 else if (mips_abi == O64_ABI)
252b5132 13873 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
316f5878 13874 else if (mips_abi == EABI_ABI)
252b5132 13875 {
316f5878 13876 if (!file_mips_gp32)
252b5132
RH
13877 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
13878 else
13879 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
13880 }
316f5878 13881 else if (mips_abi == N32_ABI)
be00bddd
TS
13882 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
13883
c9914766 13884 /* Nothing to do for N64_ABI. */
252b5132
RH
13885
13886 if (mips_32bitmode)
13887 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
13888}
13889
13890#endif /* OBJ_ELF || OBJ_MAYBE_ELF */
13891\f
beae10d5 13892typedef struct proc {
9b2f1d35
EC
13893 symbolS *func_sym;
13894 symbolS *func_end_sym;
beae10d5
KH
13895 unsigned long reg_mask;
13896 unsigned long reg_offset;
13897 unsigned long fpreg_mask;
13898 unsigned long fpreg_offset;
13899 unsigned long frame_offset;
13900 unsigned long frame_reg;
13901 unsigned long pc_reg;
13902} procS;
252b5132
RH
13903
13904static procS cur_proc;
13905static procS *cur_proc_ptr;
13906static int numprocs;
13907
0a9ef439 13908/* Fill in an rs_align_code fragment. */
a19d8eb0 13909
0a9ef439 13910void
17a2f251 13911mips_handle_align (fragS *fragp)
a19d8eb0 13912{
0a9ef439
RH
13913 if (fragp->fr_type != rs_align_code)
13914 return;
13915
13916 if (mips_opts.mips16)
a19d8eb0
CP
13917 {
13918 static const unsigned char be_nop[] = { 0x65, 0x00 };
13919 static const unsigned char le_nop[] = { 0x00, 0x65 };
13920
0a9ef439
RH
13921 int bytes;
13922 char *p;
a19d8eb0 13923
0a9ef439
RH
13924 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
13925 p = fragp->fr_literal + fragp->fr_fix;
13926
13927 if (bytes & 1)
13928 {
13929 *p++ = 0;
f9419b05 13930 fragp->fr_fix++;
0a9ef439
RH
13931 }
13932
13933 memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
13934 fragp->fr_var = 2;
a19d8eb0
CP
13935 }
13936
0a9ef439 13937 /* For mips32, a nop is a zero, which we trivially get by doing nothing. */
a19d8eb0
CP
13938}
13939
252b5132 13940static void
17a2f251 13941md_obj_begin (void)
252b5132
RH
13942{
13943}
13944
13945static void
17a2f251 13946md_obj_end (void)
252b5132
RH
13947{
13948 /* check for premature end, nesting errors, etc */
13949 if (cur_proc_ptr)
9a41af64 13950 as_warn (_("missing .end at end of assembly"));
252b5132
RH
13951}
13952
13953static long
17a2f251 13954get_number (void)
252b5132
RH
13955{
13956 int negative = 0;
13957 long val = 0;
13958
13959 if (*input_line_pointer == '-')
13960 {
13961 ++input_line_pointer;
13962 negative = 1;
13963 }
3882b010 13964 if (!ISDIGIT (*input_line_pointer))
956cd1d6 13965 as_bad (_("expected simple number"));
252b5132
RH
13966 if (input_line_pointer[0] == '0')
13967 {
13968 if (input_line_pointer[1] == 'x')
13969 {
13970 input_line_pointer += 2;
3882b010 13971 while (ISXDIGIT (*input_line_pointer))
252b5132
RH
13972 {
13973 val <<= 4;
13974 val |= hex_value (*input_line_pointer++);
13975 }
13976 return negative ? -val : val;
13977 }
13978 else
13979 {
13980 ++input_line_pointer;
3882b010 13981 while (ISDIGIT (*input_line_pointer))
252b5132
RH
13982 {
13983 val <<= 3;
13984 val |= *input_line_pointer++ - '0';
13985 }
13986 return negative ? -val : val;
13987 }
13988 }
3882b010 13989 if (!ISDIGIT (*input_line_pointer))
252b5132
RH
13990 {
13991 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
13992 *input_line_pointer, *input_line_pointer);
956cd1d6 13993 as_warn (_("invalid number"));
252b5132
RH
13994 return -1;
13995 }
3882b010 13996 while (ISDIGIT (*input_line_pointer))
252b5132
RH
13997 {
13998 val *= 10;
13999 val += *input_line_pointer++ - '0';
14000 }
14001 return negative ? -val : val;
14002}
14003
14004/* The .file directive; just like the usual .file directive, but there
c5dd6aab
DJ
14005 is an initial number which is the ECOFF file index. In the non-ECOFF
14006 case .file implies DWARF-2. */
14007
14008static void
17a2f251 14009s_mips_file (int x ATTRIBUTE_UNUSED)
c5dd6aab 14010{
ecb4347a
DJ
14011 static int first_file_directive = 0;
14012
c5dd6aab
DJ
14013 if (ECOFF_DEBUGGING)
14014 {
14015 get_number ();
14016 s_app_file (0);
14017 }
14018 else
ecb4347a
DJ
14019 {
14020 char *filename;
14021
14022 filename = dwarf2_directive_file (0);
14023
14024 /* Versions of GCC up to 3.1 start files with a ".file"
14025 directive even for stabs output. Make sure that this
14026 ".file" is handled. Note that you need a version of GCC
14027 after 3.1 in order to support DWARF-2 on MIPS. */
14028 if (filename != NULL && ! first_file_directive)
14029 {
14030 (void) new_logical_line (filename, -1);
c04f5787 14031 s_app_file_string (filename, 0);
ecb4347a
DJ
14032 }
14033 first_file_directive = 1;
14034 }
c5dd6aab
DJ
14035}
14036
14037/* The .loc directive, implying DWARF-2. */
252b5132
RH
14038
14039static void
17a2f251 14040s_mips_loc (int x ATTRIBUTE_UNUSED)
252b5132 14041{
c5dd6aab
DJ
14042 if (!ECOFF_DEBUGGING)
14043 dwarf2_directive_loc (0);
252b5132
RH
14044}
14045
252b5132
RH
14046/* The .end directive. */
14047
14048static void
17a2f251 14049s_mips_end (int x ATTRIBUTE_UNUSED)
252b5132
RH
14050{
14051 symbolS *p;
252b5132 14052
7a621144
DJ
14053 /* Following functions need their own .frame and .cprestore directives. */
14054 mips_frame_reg_valid = 0;
14055 mips_cprestore_valid = 0;
14056
252b5132
RH
14057 if (!is_end_of_line[(unsigned char) *input_line_pointer])
14058 {
14059 p = get_symbol ();
14060 demand_empty_rest_of_line ();
14061 }
14062 else
14063 p = NULL;
14064
14949570 14065 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
14066 as_warn (_(".end not in text section"));
14067
14068 if (!cur_proc_ptr)
14069 {
14070 as_warn (_(".end directive without a preceding .ent directive."));
14071 demand_empty_rest_of_line ();
14072 return;
14073 }
14074
14075 if (p != NULL)
14076 {
14077 assert (S_GET_NAME (p));
9b2f1d35 14078 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
252b5132 14079 as_warn (_(".end symbol does not match .ent symbol."));
ecb4347a
DJ
14080
14081 if (debug_type == DEBUG_STABS)
14082 stabs_generate_asm_endfunc (S_GET_NAME (p),
14083 S_GET_NAME (p));
252b5132
RH
14084 }
14085 else
14086 as_warn (_(".end directive missing or unknown symbol"));
14087
2132e3a3 14088#ifdef OBJ_ELF
9b2f1d35
EC
14089 /* Create an expression to calculate the size of the function. */
14090 if (p && cur_proc_ptr)
14091 {
14092 OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
14093 expressionS *exp = xmalloc (sizeof (expressionS));
14094
14095 obj->size = exp;
14096 exp->X_op = O_subtract;
14097 exp->X_add_symbol = symbol_temp_new_now ();
14098 exp->X_op_symbol = p;
14099 exp->X_add_number = 0;
14100
14101 cur_proc_ptr->func_end_sym = exp->X_add_symbol;
14102 }
14103
ecb4347a 14104 /* Generate a .pdr section. */
dcd410fe
RO
14105 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING
14106 && mips_flag_pdr)
ecb4347a
DJ
14107 {
14108 segT saved_seg = now_seg;
14109 subsegT saved_subseg = now_subseg;
14110 valueT dot;
14111 expressionS exp;
14112 char *fragp;
252b5132 14113
ecb4347a 14114 dot = frag_now_fix ();
252b5132
RH
14115
14116#ifdef md_flush_pending_output
ecb4347a 14117 md_flush_pending_output ();
252b5132
RH
14118#endif
14119
ecb4347a
DJ
14120 assert (pdr_seg);
14121 subseg_set (pdr_seg, 0);
252b5132 14122
ecb4347a
DJ
14123 /* Write the symbol. */
14124 exp.X_op = O_symbol;
14125 exp.X_add_symbol = p;
14126 exp.X_add_number = 0;
14127 emit_expr (&exp, 4);
252b5132 14128
ecb4347a 14129 fragp = frag_more (7 * 4);
252b5132 14130
17a2f251
TS
14131 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
14132 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
14133 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
14134 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
14135 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
14136 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
14137 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
252b5132 14138
ecb4347a
DJ
14139 subseg_set (saved_seg, saved_subseg);
14140 }
14141#endif /* OBJ_ELF */
252b5132
RH
14142
14143 cur_proc_ptr = NULL;
14144}
14145
14146/* The .aent and .ent directives. */
14147
14148static void
17a2f251 14149s_mips_ent (int aent)
252b5132 14150{
252b5132 14151 symbolS *symbolP;
252b5132
RH
14152
14153 symbolP = get_symbol ();
14154 if (*input_line_pointer == ',')
f9419b05 14155 ++input_line_pointer;
252b5132 14156 SKIP_WHITESPACE ();
3882b010 14157 if (ISDIGIT (*input_line_pointer)
d9a62219 14158 || *input_line_pointer == '-')
874e8986 14159 get_number ();
252b5132 14160
14949570 14161 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
14162 as_warn (_(".ent or .aent not in text section."));
14163
14164 if (!aent && cur_proc_ptr)
9a41af64 14165 as_warn (_("missing .end"));
252b5132
RH
14166
14167 if (!aent)
14168 {
7a621144
DJ
14169 /* This function needs its own .frame and .cprestore directives. */
14170 mips_frame_reg_valid = 0;
14171 mips_cprestore_valid = 0;
14172
252b5132
RH
14173 cur_proc_ptr = &cur_proc;
14174 memset (cur_proc_ptr, '\0', sizeof (procS));
14175
9b2f1d35 14176 cur_proc_ptr->func_sym = symbolP;
252b5132 14177
49309057 14178 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
252b5132 14179
f9419b05 14180 ++numprocs;
ecb4347a
DJ
14181
14182 if (debug_type == DEBUG_STABS)
14183 stabs_generate_asm_func (S_GET_NAME (symbolP),
14184 S_GET_NAME (symbolP));
252b5132
RH
14185 }
14186
14187 demand_empty_rest_of_line ();
14188}
14189
14190/* The .frame directive. If the mdebug section is present (IRIX 5 native)
bdaaa2e1 14191 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
252b5132 14192 s_mips_frame is used so that we can set the PDR information correctly.
bdaaa2e1 14193 We can't use the ecoff routines because they make reference to the ecoff
252b5132
RH
14194 symbol table (in the mdebug section). */
14195
14196static void
17a2f251 14197s_mips_frame (int ignore ATTRIBUTE_UNUSED)
252b5132 14198{
ecb4347a
DJ
14199#ifdef OBJ_ELF
14200 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14201 {
14202 long val;
252b5132 14203
ecb4347a
DJ
14204 if (cur_proc_ptr == (procS *) NULL)
14205 {
14206 as_warn (_(".frame outside of .ent"));
14207 demand_empty_rest_of_line ();
14208 return;
14209 }
252b5132 14210
ecb4347a
DJ
14211 cur_proc_ptr->frame_reg = tc_get_register (1);
14212
14213 SKIP_WHITESPACE ();
14214 if (*input_line_pointer++ != ','
14215 || get_absolute_expression_and_terminator (&val) != ',')
14216 {
14217 as_warn (_("Bad .frame directive"));
14218 --input_line_pointer;
14219 demand_empty_rest_of_line ();
14220 return;
14221 }
252b5132 14222
ecb4347a
DJ
14223 cur_proc_ptr->frame_offset = val;
14224 cur_proc_ptr->pc_reg = tc_get_register (0);
252b5132 14225
252b5132 14226 demand_empty_rest_of_line ();
252b5132 14227 }
ecb4347a
DJ
14228 else
14229#endif /* OBJ_ELF */
14230 s_ignore (ignore);
252b5132
RH
14231}
14232
bdaaa2e1
KH
14233/* The .fmask and .mask directives. If the mdebug section is present
14234 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
252b5132 14235 embedded targets, s_mips_mask is used so that we can set the PDR
bdaaa2e1 14236 information correctly. We can't use the ecoff routines because they
252b5132
RH
14237 make reference to the ecoff symbol table (in the mdebug section). */
14238
14239static void
17a2f251 14240s_mips_mask (int reg_type)
252b5132 14241{
ecb4347a
DJ
14242#ifdef OBJ_ELF
14243 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
252b5132 14244 {
ecb4347a 14245 long mask, off;
252b5132 14246
ecb4347a
DJ
14247 if (cur_proc_ptr == (procS *) NULL)
14248 {
14249 as_warn (_(".mask/.fmask outside of .ent"));
14250 demand_empty_rest_of_line ();
14251 return;
14252 }
252b5132 14253
ecb4347a
DJ
14254 if (get_absolute_expression_and_terminator (&mask) != ',')
14255 {
14256 as_warn (_("Bad .mask/.fmask directive"));
14257 --input_line_pointer;
14258 demand_empty_rest_of_line ();
14259 return;
14260 }
252b5132 14261
ecb4347a
DJ
14262 off = get_absolute_expression ();
14263
14264 if (reg_type == 'F')
14265 {
14266 cur_proc_ptr->fpreg_mask = mask;
14267 cur_proc_ptr->fpreg_offset = off;
14268 }
14269 else
14270 {
14271 cur_proc_ptr->reg_mask = mask;
14272 cur_proc_ptr->reg_offset = off;
14273 }
14274
14275 demand_empty_rest_of_line ();
252b5132
RH
14276 }
14277 else
ecb4347a
DJ
14278#endif /* OBJ_ELF */
14279 s_ignore (reg_type);
252b5132
RH
14280}
14281
316f5878
RS
14282/* A table describing all the processors gas knows about. Names are
14283 matched in the order listed.
e7af610e 14284
316f5878
RS
14285 To ease comparison, please keep this table in the same order as
14286 gcc's mips_cpu_info_table[]. */
e972090a
NC
14287static const struct mips_cpu_info mips_cpu_info_table[] =
14288{
316f5878
RS
14289 /* Entries for generic ISAs */
14290 { "mips1", 1, ISA_MIPS1, CPU_R3000 },
14291 { "mips2", 1, ISA_MIPS2, CPU_R6000 },
14292 { "mips3", 1, ISA_MIPS3, CPU_R4000 },
14293 { "mips4", 1, ISA_MIPS4, CPU_R8000 },
14294 { "mips5", 1, ISA_MIPS5, CPU_MIPS5 },
14295 { "mips32", 1, ISA_MIPS32, CPU_MIPS32 },
af7ee8bf 14296 { "mips32r2", 1, ISA_MIPS32R2, CPU_MIPS32R2 },
316f5878 14297 { "mips64", 1, ISA_MIPS64, CPU_MIPS64 },
5f74bc13 14298 { "mips64r2", 1, ISA_MIPS64R2, CPU_MIPS64R2 },
316f5878
RS
14299
14300 /* MIPS I */
14301 { "r3000", 0, ISA_MIPS1, CPU_R3000 },
14302 { "r2000", 0, ISA_MIPS1, CPU_R3000 },
14303 { "r3900", 0, ISA_MIPS1, CPU_R3900 },
14304
14305 /* MIPS II */
14306 { "r6000", 0, ISA_MIPS2, CPU_R6000 },
14307
14308 /* MIPS III */
14309 { "r4000", 0, ISA_MIPS3, CPU_R4000 },
14310 { "r4010", 0, ISA_MIPS2, CPU_R4010 },
14311 { "vr4100", 0, ISA_MIPS3, CPU_VR4100 },
14312 { "vr4111", 0, ISA_MIPS3, CPU_R4111 },
60b63b72
RS
14313 { "vr4120", 0, ISA_MIPS3, CPU_VR4120 },
14314 { "vr4130", 0, ISA_MIPS3, CPU_VR4120 },
14315 { "vr4181", 0, ISA_MIPS3, CPU_R4111 },
316f5878
RS
14316 { "vr4300", 0, ISA_MIPS3, CPU_R4300 },
14317 { "r4400", 0, ISA_MIPS3, CPU_R4400 },
14318 { "r4600", 0, ISA_MIPS3, CPU_R4600 },
14319 { "orion", 0, ISA_MIPS3, CPU_R4600 },
14320 { "r4650", 0, ISA_MIPS3, CPU_R4650 },
14321
14322 /* MIPS IV */
14323 { "r8000", 0, ISA_MIPS4, CPU_R8000 },
14324 { "r10000", 0, ISA_MIPS4, CPU_R10000 },
14325 { "r12000", 0, ISA_MIPS4, CPU_R12000 },
14326 { "vr5000", 0, ISA_MIPS4, CPU_R5000 },
60b63b72
RS
14327 { "vr5400", 0, ISA_MIPS4, CPU_VR5400 },
14328 { "vr5500", 0, ISA_MIPS4, CPU_VR5500 },
316f5878
RS
14329 { "rm5200", 0, ISA_MIPS4, CPU_R5000 },
14330 { "rm5230", 0, ISA_MIPS4, CPU_R5000 },
14331 { "rm5231", 0, ISA_MIPS4, CPU_R5000 },
14332 { "rm5261", 0, ISA_MIPS4, CPU_R5000 },
14333 { "rm5721", 0, ISA_MIPS4, CPU_R5000 },
5a7ea749 14334 { "rm7000", 0, ISA_MIPS4, CPU_RM7000 },
9a92f48d 14335 { "rm9000", 0, ISA_MIPS4, CPU_RM9000 },
316f5878
RS
14336
14337 /* MIPS 32 */
fef14a42 14338 { "4kc", 0, ISA_MIPS32, CPU_MIPS32 },
316f5878
RS
14339 { "4km", 0, ISA_MIPS32, CPU_MIPS32 },
14340 { "4kp", 0, ISA_MIPS32, CPU_MIPS32 },
e7af610e 14341
32b26a03
MR
14342 /* MIPS32 Release 2 */
14343 { "m4k", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
14344 { "24k", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
14345 { "24kc", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
14346 { "24kf", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
14347 { "24kx", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
14348
316f5878
RS
14349 /* MIPS 64 */
14350 { "5kc", 0, ISA_MIPS64, CPU_MIPS64 },
ff324200 14351 { "5kf", 0, ISA_MIPS64, CPU_MIPS64 },
316f5878 14352 { "20kc", 0, ISA_MIPS64, CPU_MIPS64 },
e7af610e 14353
c7a23324 14354 /* Broadcom SB-1 CPU core */
316f5878 14355 { "sb1", 0, ISA_MIPS64, CPU_SB1 },
e7af610e 14356
316f5878
RS
14357 /* End marker */
14358 { NULL, 0, 0, 0 }
14359};
e7af610e 14360
84ea6cf2 14361
316f5878
RS
14362/* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
14363 with a final "000" replaced by "k". Ignore case.
e7af610e 14364
316f5878 14365 Note: this function is shared between GCC and GAS. */
c6c98b38 14366
b34976b6 14367static bfd_boolean
17a2f251 14368mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
14369{
14370 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
14371 given++, canonical++;
14372
14373 return ((*given == 0 && *canonical == 0)
14374 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
14375}
14376
14377
14378/* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
14379 CPU name. We've traditionally allowed a lot of variation here.
14380
14381 Note: this function is shared between GCC and GAS. */
14382
b34976b6 14383static bfd_boolean
17a2f251 14384mips_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
14385{
14386 /* First see if the name matches exactly, or with a final "000"
14387 turned into "k". */
14388 if (mips_strict_matching_cpu_name_p (canonical, given))
b34976b6 14389 return TRUE;
316f5878
RS
14390
14391 /* If not, try comparing based on numerical designation alone.
14392 See if GIVEN is an unadorned number, or 'r' followed by a number. */
14393 if (TOLOWER (*given) == 'r')
14394 given++;
14395 if (!ISDIGIT (*given))
b34976b6 14396 return FALSE;
316f5878
RS
14397
14398 /* Skip over some well-known prefixes in the canonical name,
14399 hoping to find a number there too. */
14400 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
14401 canonical += 2;
14402 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
14403 canonical += 2;
14404 else if (TOLOWER (canonical[0]) == 'r')
14405 canonical += 1;
14406
14407 return mips_strict_matching_cpu_name_p (canonical, given);
14408}
14409
14410
14411/* Parse an option that takes the name of a processor as its argument.
14412 OPTION is the name of the option and CPU_STRING is the argument.
14413 Return the corresponding processor enumeration if the CPU_STRING is
14414 recognized, otherwise report an error and return null.
14415
14416 A similar function exists in GCC. */
e7af610e
NC
14417
14418static const struct mips_cpu_info *
17a2f251 14419mips_parse_cpu (const char *option, const char *cpu_string)
e7af610e 14420{
316f5878 14421 const struct mips_cpu_info *p;
e7af610e 14422
316f5878
RS
14423 /* 'from-abi' selects the most compatible architecture for the given
14424 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
14425 EABIs, we have to decide whether we're using the 32-bit or 64-bit
14426 version. Look first at the -mgp options, if given, otherwise base
14427 the choice on MIPS_DEFAULT_64BIT.
e7af610e 14428
316f5878
RS
14429 Treat NO_ABI like the EABIs. One reason to do this is that the
14430 plain 'mips' and 'mips64' configs have 'from-abi' as their default
14431 architecture. This code picks MIPS I for 'mips' and MIPS III for
14432 'mips64', just as we did in the days before 'from-abi'. */
14433 if (strcasecmp (cpu_string, "from-abi") == 0)
14434 {
14435 if (ABI_NEEDS_32BIT_REGS (mips_abi))
14436 return mips_cpu_info_from_isa (ISA_MIPS1);
14437
14438 if (ABI_NEEDS_64BIT_REGS (mips_abi))
14439 return mips_cpu_info_from_isa (ISA_MIPS3);
14440
14441 if (file_mips_gp32 >= 0)
14442 return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
14443
14444 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
14445 ? ISA_MIPS3
14446 : ISA_MIPS1);
14447 }
14448
14449 /* 'default' has traditionally been a no-op. Probably not very useful. */
14450 if (strcasecmp (cpu_string, "default") == 0)
14451 return 0;
14452
14453 for (p = mips_cpu_info_table; p->name != 0; p++)
14454 if (mips_matching_cpu_name_p (p->name, cpu_string))
14455 return p;
14456
14457 as_bad ("Bad value (%s) for %s", cpu_string, option);
14458 return 0;
e7af610e
NC
14459}
14460
316f5878
RS
14461/* Return the canonical processor information for ISA (a member of the
14462 ISA_MIPS* enumeration). */
14463
e7af610e 14464static const struct mips_cpu_info *
17a2f251 14465mips_cpu_info_from_isa (int isa)
e7af610e
NC
14466{
14467 int i;
14468
14469 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14470 if (mips_cpu_info_table[i].is_isa
316f5878 14471 && isa == mips_cpu_info_table[i].isa)
e7af610e
NC
14472 return (&mips_cpu_info_table[i]);
14473
e972090a 14474 return NULL;
e7af610e 14475}
fef14a42
TS
14476
14477static const struct mips_cpu_info *
17a2f251 14478mips_cpu_info_from_arch (int arch)
fef14a42
TS
14479{
14480 int i;
14481
14482 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14483 if (arch == mips_cpu_info_table[i].cpu)
14484 return (&mips_cpu_info_table[i]);
14485
14486 return NULL;
14487}
316f5878
RS
14488\f
14489static void
17a2f251 14490show (FILE *stream, const char *string, int *col_p, int *first_p)
316f5878
RS
14491{
14492 if (*first_p)
14493 {
14494 fprintf (stream, "%24s", "");
14495 *col_p = 24;
14496 }
14497 else
14498 {
14499 fprintf (stream, ", ");
14500 *col_p += 2;
14501 }
e7af610e 14502
316f5878
RS
14503 if (*col_p + strlen (string) > 72)
14504 {
14505 fprintf (stream, "\n%24s", "");
14506 *col_p = 24;
14507 }
14508
14509 fprintf (stream, "%s", string);
14510 *col_p += strlen (string);
14511
14512 *first_p = 0;
14513}
14514
14515void
17a2f251 14516md_show_usage (FILE *stream)
e7af610e 14517{
316f5878
RS
14518 int column, first;
14519 size_t i;
14520
14521 fprintf (stream, _("\
14522MIPS options:\n\
316f5878
RS
14523-EB generate big endian output\n\
14524-EL generate little endian output\n\
14525-g, -g2 do not remove unneeded NOPs or swap branches\n\
14526-G NUM allow referencing objects up to NUM bytes\n\
14527 implicitly with the gp register [default 8]\n"));
14528 fprintf (stream, _("\
14529-mips1 generate MIPS ISA I instructions\n\
14530-mips2 generate MIPS ISA II instructions\n\
14531-mips3 generate MIPS ISA III instructions\n\
14532-mips4 generate MIPS ISA IV instructions\n\
14533-mips5 generate MIPS ISA V instructions\n\
14534-mips32 generate MIPS32 ISA instructions\n\
af7ee8bf 14535-mips32r2 generate MIPS32 release 2 ISA instructions\n\
316f5878 14536-mips64 generate MIPS64 ISA instructions\n\
5f74bc13 14537-mips64r2 generate MIPS64 release 2 ISA instructions\n\
316f5878
RS
14538-march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
14539
14540 first = 1;
e7af610e
NC
14541
14542 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
316f5878
RS
14543 show (stream, mips_cpu_info_table[i].name, &column, &first);
14544 show (stream, "from-abi", &column, &first);
14545 fputc ('\n', stream);
e7af610e 14546
316f5878
RS
14547 fprintf (stream, _("\
14548-mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
14549-no-mCPU don't generate code specific to CPU.\n\
14550 For -mCPU and -no-mCPU, CPU must be one of:\n"));
14551
14552 first = 1;
14553
14554 show (stream, "3900", &column, &first);
14555 show (stream, "4010", &column, &first);
14556 show (stream, "4100", &column, &first);
14557 show (stream, "4650", &column, &first);
14558 fputc ('\n', stream);
14559
14560 fprintf (stream, _("\
14561-mips16 generate mips16 instructions\n\
14562-no-mips16 do not generate mips16 instructions\n"));
14563 fprintf (stream, _("\
74cd071d
CF
14564-mdsp generate DSP instructions\n\
14565-mno-dsp do not generate DSP instructions\n"));
14566 fprintf (stream, _("\
ef2e4d86
CF
14567-mmt generate MT instructions\n\
14568-mno-mt do not generate MT instructions\n"));
14569 fprintf (stream, _("\
d766e8ec 14570-mfix-vr4120 work around certain VR4120 errata\n\
7d8e00cf 14571-mfix-vr4130 work around VR4130 mflo/mfhi errata\n\
316f5878
RS
14572-mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
14573-mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
aed1a261
RS
14574-mno-shared optimize output for executables\n\
14575-msym32 assume all symbols have 32-bit values\n\
316f5878
RS
14576-O0 remove unneeded NOPs, do not swap branches\n\
14577-O remove unneeded NOPs and swap branches\n\
316f5878
RS
14578--[no-]construct-floats [dis]allow floating point values to be constructed\n\
14579--trap, --no-break trap exception on div by 0 and mult overflow\n\
14580--break, --no-trap break exception on div by 0 and mult overflow\n"));
14581#ifdef OBJ_ELF
14582 fprintf (stream, _("\
14583-KPIC, -call_shared generate SVR4 position independent code\n\
14584-non_shared do not generate position independent code\n\
14585-xgot assume a 32 bit GOT\n\
dcd410fe 14586-mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
bbe506e8
TS
14587-mshared, -mno-shared disable/enable .cpload optimization for\n\
14588 non-shared code\n\
316f5878
RS
14589-mabi=ABI create ABI conformant object file for:\n"));
14590
14591 first = 1;
14592
14593 show (stream, "32", &column, &first);
14594 show (stream, "o64", &column, &first);
14595 show (stream, "n32", &column, &first);
14596 show (stream, "64", &column, &first);
14597 show (stream, "eabi", &column, &first);
14598
14599 fputc ('\n', stream);
14600
14601 fprintf (stream, _("\
14602-32 create o32 ABI object file (default)\n\
14603-n32 create n32 ABI object file\n\
14604-64 create 64 ABI object file\n"));
14605#endif
e7af610e 14606}
14e777e0
KB
14607
14608enum dwarf2_format
17a2f251 14609mips_dwarf2_format (void)
14e777e0
KB
14610{
14611 if (mips_abi == N64_ABI)
1de5b6a1
AO
14612 {
14613#ifdef TE_IRIX
14614 return dwarf2_format_64bit_irix;
14615#else
14616 return dwarf2_format_64bit;
14617#endif
14618 }
14e777e0
KB
14619 else
14620 return dwarf2_format_32bit;
14621}
73369e65
EC
14622
14623int
14624mips_dwarf2_addr_size (void)
14625{
14626 if (mips_abi == N64_ABI)
14627 return 8;
73369e65
EC
14628 else
14629 return 4;
14630}
5862107c
EC
14631
14632/* Standard calling conventions leave the CFA at SP on entry. */
14633void
14634mips_cfi_frame_initial_instructions (void)
14635{
14636 cfi_add_CFA_def_cfa_register (SP);
14637}
14638
This page took 1.667121 seconds and 4 git commands to generate.