2005-12-12 Paul Brook <paul@codesourcery.com>
[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
RS
152
153 /* True if this instruction occured in a .set noreorder block. */
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:
1194#ifdef TE_TMIPS
cfe86eaa 1195 /* This is traditional mips. */
e013f690 1196 return (target_big_endian
cfe86eaa
TS
1197 ? (HAVE_64BIT_OBJECTS
1198 ? "elf64-tradbigmips"
1199 : (HAVE_NEWABI
1200 ? "elf32-ntradbigmips" : "elf32-tradbigmips"))
1201 : (HAVE_64BIT_OBJECTS
1202 ? "elf64-tradlittlemips"
1203 : (HAVE_NEWABI
1204 ? "elf32-ntradlittlemips" : "elf32-tradlittlemips")));
e013f690
TS
1205#else
1206 return (target_big_endian
cfe86eaa
TS
1207 ? (HAVE_64BIT_OBJECTS
1208 ? "elf64-bigmips"
1209 : (HAVE_NEWABI
1210 ? "elf32-nbigmips" : "elf32-bigmips"))
1211 : (HAVE_64BIT_OBJECTS
1212 ? "elf64-littlemips"
1213 : (HAVE_NEWABI
1214 ? "elf32-nlittlemips" : "elf32-littlemips")));
e013f690
TS
1215#endif
1216 default:
1217 abort ();
1218 return NULL;
1219 }
1220}
1221
1e915849
RS
1222/* Return the length of instruction INSN. */
1223
1224static inline unsigned int
1225insn_length (const struct mips_cl_insn *insn)
1226{
1227 if (!mips_opts.mips16)
1228 return 4;
1229 return insn->mips16_absolute_jump_p || insn->use_extend ? 4 : 2;
1230}
1231
1232/* Initialise INSN from opcode entry MO. Leave its position unspecified. */
1233
1234static void
1235create_insn (struct mips_cl_insn *insn, const struct mips_opcode *mo)
1236{
1237 size_t i;
1238
1239 insn->insn_mo = mo;
1240 insn->use_extend = FALSE;
1241 insn->extend = 0;
1242 insn->insn_opcode = mo->match;
1243 insn->frag = NULL;
1244 insn->where = 0;
1245 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
1246 insn->fixp[i] = NULL;
1247 insn->fixed_p = (mips_opts.noreorder > 0);
1248 insn->noreorder_p = (mips_opts.noreorder > 0);
1249 insn->mips16_absolute_jump_p = 0;
1250}
1251
1252/* Install INSN at the location specified by its "frag" and "where" fields. */
1253
1254static void
1255install_insn (const struct mips_cl_insn *insn)
1256{
1257 char *f = insn->frag->fr_literal + insn->where;
1258 if (!mips_opts.mips16)
1259 md_number_to_chars (f, insn->insn_opcode, 4);
1260 else if (insn->mips16_absolute_jump_p)
1261 {
1262 md_number_to_chars (f, insn->insn_opcode >> 16, 2);
1263 md_number_to_chars (f + 2, insn->insn_opcode & 0xffff, 2);
1264 }
1265 else
1266 {
1267 if (insn->use_extend)
1268 {
1269 md_number_to_chars (f, 0xf000 | insn->extend, 2);
1270 f += 2;
1271 }
1272 md_number_to_chars (f, insn->insn_opcode, 2);
1273 }
1274}
1275
1276/* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly
1277 and install the opcode in the new location. */
1278
1279static void
1280move_insn (struct mips_cl_insn *insn, fragS *frag, long where)
1281{
1282 size_t i;
1283
1284 insn->frag = frag;
1285 insn->where = where;
1286 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
1287 if (insn->fixp[i] != NULL)
1288 {
1289 insn->fixp[i]->fx_frag = frag;
1290 insn->fixp[i]->fx_where = where;
1291 }
1292 install_insn (insn);
1293}
1294
1295/* Add INSN to the end of the output. */
1296
1297static void
1298add_fixed_insn (struct mips_cl_insn *insn)
1299{
1300 char *f = frag_more (insn_length (insn));
1301 move_insn (insn, frag_now, f - frag_now->fr_literal);
1302}
1303
1304/* Start a variant frag and move INSN to the start of the variant part,
1305 marking it as fixed. The other arguments are as for frag_var. */
1306
1307static void
1308add_relaxed_insn (struct mips_cl_insn *insn, int max_chars, int var,
1309 relax_substateT subtype, symbolS *symbol, offsetT offset)
1310{
1311 frag_grow (max_chars);
1312 move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
1313 insn->fixed_p = 1;
1314 frag_var (rs_machine_dependent, max_chars, var,
1315 subtype, symbol, offset, NULL);
1316}
1317
1318/* Insert N copies of INSN into the history buffer, starting at
1319 position FIRST. Neither FIRST nor N need to be clipped. */
1320
1321static void
1322insert_into_history (unsigned int first, unsigned int n,
1323 const struct mips_cl_insn *insn)
1324{
1325 if (mips_relax.sequence != 2)
1326 {
1327 unsigned int i;
1328
1329 for (i = ARRAY_SIZE (history); i-- > first;)
1330 if (i >= first + n)
1331 history[i] = history[i - n];
1332 else
1333 history[i] = *insn;
1334 }
1335}
1336
1337/* Emit a nop instruction, recording it in the history buffer. */
1338
1339static void
1340emit_nop (void)
1341{
1342 add_fixed_insn (NOP_INSN);
1343 insert_into_history (0, 1, NOP_INSN);
1344}
1345
71400594
RS
1346/* Initialize vr4120_conflicts. There is a bit of duplication here:
1347 the idea is to make it obvious at a glance that each errata is
1348 included. */
1349
1350static void
1351init_vr4120_conflicts (void)
1352{
1353#define CONFLICT(FIRST, SECOND) \
1354 vr4120_conflicts[FIX_VR4120_##FIRST] |= 1 << FIX_VR4120_##SECOND
1355
1356 /* Errata 21 - [D]DIV[U] after [D]MACC */
1357 CONFLICT (MACC, DIV);
1358 CONFLICT (DMACC, DIV);
1359
1360 /* Errata 23 - Continuous DMULT[U]/DMACC instructions. */
1361 CONFLICT (DMULT, DMULT);
1362 CONFLICT (DMULT, DMACC);
1363 CONFLICT (DMACC, DMULT);
1364 CONFLICT (DMACC, DMACC);
1365
1366 /* Errata 24 - MT{LO,HI} after [D]MACC */
1367 CONFLICT (MACC, MTHILO);
1368 CONFLICT (DMACC, MTHILO);
1369
1370 /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
1371 instruction is executed immediately after a MACC or DMACC
1372 instruction, the result of [either instruction] is incorrect." */
1373 CONFLICT (MACC, MULT);
1374 CONFLICT (MACC, DMULT);
1375 CONFLICT (DMACC, MULT);
1376 CONFLICT (DMACC, DMULT);
1377
1378 /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
1379 executed immediately after a DMULT, DMULTU, DIV, DIVU,
1380 DDIV or DDIVU instruction, the result of the MACC or
1381 DMACC instruction is incorrect.". */
1382 CONFLICT (DMULT, MACC);
1383 CONFLICT (DMULT, DMACC);
1384 CONFLICT (DIV, MACC);
1385 CONFLICT (DIV, DMACC);
1386
1387#undef CONFLICT
1388}
1389
156c2f8b
NC
1390/* This function is called once, at assembler startup time. It should
1391 set up all the tables, etc. that the MD part of the assembler will need. */
1392
252b5132 1393void
17a2f251 1394md_begin (void)
252b5132 1395{
252b5132 1396 register const char *retval = NULL;
156c2f8b 1397 int i = 0;
252b5132 1398 int broken = 0;
1f25f5d3 1399
fef14a42 1400 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_arch))
252b5132
RH
1401 as_warn (_("Could not set architecture and machine"));
1402
252b5132
RH
1403 op_hash = hash_new ();
1404
1405 for (i = 0; i < NUMOPCODES;)
1406 {
1407 const char *name = mips_opcodes[i].name;
1408
17a2f251 1409 retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
252b5132
RH
1410 if (retval != NULL)
1411 {
1412 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1413 mips_opcodes[i].name, retval);
1414 /* Probably a memory allocation problem? Give up now. */
1415 as_fatal (_("Broken assembler. No assembly attempted."));
1416 }
1417 do
1418 {
1419 if (mips_opcodes[i].pinfo != INSN_MACRO)
1420 {
1421 if (!validate_mips_insn (&mips_opcodes[i]))
1422 broken = 1;
1e915849
RS
1423 if (nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
1424 {
1425 create_insn (&nop_insn, mips_opcodes + i);
1426 nop_insn.fixed_p = 1;
1427 }
252b5132
RH
1428 }
1429 ++i;
1430 }
1431 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1432 }
1433
1434 mips16_op_hash = hash_new ();
1435
1436 i = 0;
1437 while (i < bfd_mips16_num_opcodes)
1438 {
1439 const char *name = mips16_opcodes[i].name;
1440
17a2f251 1441 retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
252b5132
RH
1442 if (retval != NULL)
1443 as_fatal (_("internal: can't hash `%s': %s"),
1444 mips16_opcodes[i].name, retval);
1445 do
1446 {
1447 if (mips16_opcodes[i].pinfo != INSN_MACRO
1448 && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1449 != mips16_opcodes[i].match))
1450 {
1451 fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1452 mips16_opcodes[i].name, mips16_opcodes[i].args);
1453 broken = 1;
1454 }
1e915849
RS
1455 if (mips16_nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
1456 {
1457 create_insn (&mips16_nop_insn, mips16_opcodes + i);
1458 mips16_nop_insn.fixed_p = 1;
1459 }
252b5132
RH
1460 ++i;
1461 }
1462 while (i < bfd_mips16_num_opcodes
1463 && strcmp (mips16_opcodes[i].name, name) == 0);
1464 }
1465
1466 if (broken)
1467 as_fatal (_("Broken assembler. No assembly attempted."));
1468
1469 /* We add all the general register names to the symbol table. This
1470 helps us detect invalid uses of them. */
1471 for (i = 0; i < 32; i++)
1472 {
1473 char buf[5];
1474
1475 sprintf (buf, "$%d", i);
1476 symbol_table_insert (symbol_new (buf, reg_section, i,
1477 &zero_address_frag));
1478 }
76db943d
TS
1479 symbol_table_insert (symbol_new ("$ra", reg_section, RA,
1480 &zero_address_frag));
252b5132
RH
1481 symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1482 &zero_address_frag));
1483 symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1484 &zero_address_frag));
1485 symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1486 &zero_address_frag));
1487 symbol_table_insert (symbol_new ("$at", reg_section, AT,
1488 &zero_address_frag));
1489 symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1490 &zero_address_frag));
1491 symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1492 &zero_address_frag));
85b51719
TS
1493 symbol_table_insert (symbol_new ("$zero", reg_section, ZERO,
1494 &zero_address_frag));
252b5132
RH
1495 symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1496 &zero_address_frag));
1497
6047c971
AO
1498 /* If we don't add these register names to the symbol table, they
1499 may end up being added as regular symbols by operand(), and then
1500 make it to the object file as undefined in case they're not
1501 regarded as local symbols. They're local in o32, since `$' is a
1502 local symbol prefix, but not in n32 or n64. */
1503 for (i = 0; i < 8; i++)
1504 {
1505 char buf[6];
1506
1507 sprintf (buf, "$fcc%i", i);
1508 symbol_table_insert (symbol_new (buf, reg_section, -1,
1509 &zero_address_frag));
1510 }
1511
7d10b47d 1512 mips_no_prev_insn ();
252b5132
RH
1513
1514 mips_gprmask = 0;
1515 mips_cprmask[0] = 0;
1516 mips_cprmask[1] = 0;
1517 mips_cprmask[2] = 0;
1518 mips_cprmask[3] = 0;
1519
1520 /* set the default alignment for the text section (2**2) */
1521 record_alignment (text_section, 2);
1522
4d0d148d 1523 bfd_set_gp_size (stdoutput, g_switch_value);
252b5132
RH
1524
1525 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1526 {
1527 /* On a native system, sections must be aligned to 16 byte
1528 boundaries. When configured for an embedded ELF target, we
1529 don't bother. */
1530 if (strcmp (TARGET_OS, "elf") != 0)
1531 {
1532 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1533 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1534 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1535 }
1536
1537 /* Create a .reginfo section for register masks and a .mdebug
1538 section for debugging information. */
1539 {
1540 segT seg;
1541 subsegT subseg;
1542 flagword flags;
1543 segT sec;
1544
1545 seg = now_seg;
1546 subseg = now_subseg;
1547
1548 /* The ABI says this section should be loaded so that the
1549 running program can access it. However, we don't load it
1550 if we are configured for an embedded target */
1551 flags = SEC_READONLY | SEC_DATA;
1552 if (strcmp (TARGET_OS, "elf") != 0)
1553 flags |= SEC_ALLOC | SEC_LOAD;
1554
316f5878 1555 if (mips_abi != N64_ABI)
252b5132
RH
1556 {
1557 sec = subseg_new (".reginfo", (subsegT) 0);
1558
195325d2
TS
1559 bfd_set_section_flags (stdoutput, sec, flags);
1560 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
bdaaa2e1 1561
252b5132
RH
1562#ifdef OBJ_ELF
1563 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1564#endif
1565 }
1566 else
1567 {
1568 /* The 64-bit ABI uses a .MIPS.options section rather than
1569 .reginfo section. */
1570 sec = subseg_new (".MIPS.options", (subsegT) 0);
195325d2
TS
1571 bfd_set_section_flags (stdoutput, sec, flags);
1572 bfd_set_section_alignment (stdoutput, sec, 3);
252b5132
RH
1573
1574#ifdef OBJ_ELF
1575 /* Set up the option header. */
1576 {
1577 Elf_Internal_Options opthdr;
1578 char *f;
1579
1580 opthdr.kind = ODK_REGINFO;
1581 opthdr.size = (sizeof (Elf_External_Options)
1582 + sizeof (Elf64_External_RegInfo));
1583 opthdr.section = 0;
1584 opthdr.info = 0;
1585 f = frag_more (sizeof (Elf_External_Options));
1586 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1587 (Elf_External_Options *) f);
1588
1589 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1590 }
1591#endif
1592 }
1593
1594 if (ECOFF_DEBUGGING)
1595 {
1596 sec = subseg_new (".mdebug", (subsegT) 0);
1597 (void) bfd_set_section_flags (stdoutput, sec,
1598 SEC_HAS_CONTENTS | SEC_READONLY);
1599 (void) bfd_set_section_alignment (stdoutput, sec, 2);
1600 }
ecb4347a 1601#ifdef OBJ_ELF
dcd410fe 1602 else if (OUTPUT_FLAVOR == bfd_target_elf_flavour && mips_flag_pdr)
ecb4347a
DJ
1603 {
1604 pdr_seg = subseg_new (".pdr", (subsegT) 0);
1605 (void) bfd_set_section_flags (stdoutput, pdr_seg,
1606 SEC_READONLY | SEC_RELOC
1607 | SEC_DEBUGGING);
1608 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1609 }
252b5132
RH
1610#endif
1611
1612 subseg_set (seg, subseg);
1613 }
1614 }
1615
1616 if (! ECOFF_DEBUGGING)
1617 md_obj_begin ();
71400594
RS
1618
1619 if (mips_fix_vr4120)
1620 init_vr4120_conflicts ();
252b5132
RH
1621}
1622
1623void
17a2f251 1624md_mips_end (void)
252b5132
RH
1625{
1626 if (! ECOFF_DEBUGGING)
1627 md_obj_end ();
1628}
1629
1630void
17a2f251 1631md_assemble (char *str)
252b5132
RH
1632{
1633 struct mips_cl_insn insn;
f6688943
TS
1634 bfd_reloc_code_real_type unused_reloc[3]
1635 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132
RH
1636
1637 imm_expr.X_op = O_absent;
5f74bc13 1638 imm2_expr.X_op = O_absent;
252b5132 1639 offset_expr.X_op = O_absent;
f6688943
TS
1640 imm_reloc[0] = BFD_RELOC_UNUSED;
1641 imm_reloc[1] = BFD_RELOC_UNUSED;
1642 imm_reloc[2] = BFD_RELOC_UNUSED;
1643 offset_reloc[0] = BFD_RELOC_UNUSED;
1644 offset_reloc[1] = BFD_RELOC_UNUSED;
1645 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
1646
1647 if (mips_opts.mips16)
1648 mips16_ip (str, &insn);
1649 else
1650 {
1651 mips_ip (str, &insn);
beae10d5
KH
1652 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1653 str, insn.insn_opcode));
252b5132
RH
1654 }
1655
1656 if (insn_error)
1657 {
1658 as_bad ("%s `%s'", insn_error, str);
1659 return;
1660 }
1661
1662 if (insn.insn_mo->pinfo == INSN_MACRO)
1663 {
584892a6 1664 macro_start ();
252b5132
RH
1665 if (mips_opts.mips16)
1666 mips16_macro (&insn);
1667 else
1668 macro (&insn);
584892a6 1669 macro_end ();
252b5132
RH
1670 }
1671 else
1672 {
1673 if (imm_expr.X_op != O_absent)
4d7206a2 1674 append_insn (&insn, &imm_expr, imm_reloc);
252b5132 1675 else if (offset_expr.X_op != O_absent)
4d7206a2 1676 append_insn (&insn, &offset_expr, offset_reloc);
252b5132 1677 else
4d7206a2 1678 append_insn (&insn, NULL, unused_reloc);
252b5132
RH
1679 }
1680}
1681
5919d012
RS
1682/* Return true if the given relocation might need a matching %lo().
1683 Note that R_MIPS_GOT16 relocations only need a matching %lo() when
1684 applied to local symbols. */
1685
1686static inline bfd_boolean
17a2f251 1687reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
5919d012 1688{
3b91255e
RS
1689 return (HAVE_IN_PLACE_ADDENDS
1690 && (reloc == BFD_RELOC_HI16_S
d6f16593
MR
1691 || reloc == BFD_RELOC_MIPS_GOT16
1692 || reloc == BFD_RELOC_MIPS16_HI16_S));
5919d012
RS
1693}
1694
1695/* Return true if the given fixup is followed by a matching R_MIPS_LO16
1696 relocation. */
1697
1698static inline bfd_boolean
17a2f251 1699fixup_has_matching_lo_p (fixS *fixp)
5919d012
RS
1700{
1701 return (fixp->fx_next != NULL
d6f16593
MR
1702 && (fixp->fx_next->fx_r_type == BFD_RELOC_LO16
1703 || fixp->fx_next->fx_r_type == BFD_RELOC_MIPS16_LO16)
5919d012
RS
1704 && fixp->fx_addsy == fixp->fx_next->fx_addsy
1705 && fixp->fx_offset == fixp->fx_next->fx_offset);
1706}
1707
252b5132
RH
1708/* See whether instruction IP reads register REG. CLASS is the type
1709 of register. */
1710
1711static int
71400594 1712insn_uses_reg (const struct mips_cl_insn *ip, unsigned int reg,
17a2f251 1713 enum mips_regclass class)
252b5132
RH
1714{
1715 if (class == MIPS16_REG)
1716 {
1717 assert (mips_opts.mips16);
1718 reg = mips16_to_32_reg_map[reg];
1719 class = MIPS_GR_REG;
1720 }
1721
85b51719
TS
1722 /* Don't report on general register ZERO, since it never changes. */
1723 if (class == MIPS_GR_REG && reg == ZERO)
252b5132
RH
1724 return 0;
1725
1726 if (class == MIPS_FP_REG)
1727 {
1728 assert (! mips_opts.mips16);
1729 /* If we are called with either $f0 or $f1, we must check $f0.
1730 This is not optimal, because it will introduce an unnecessary
1731 NOP between "lwc1 $f0" and "swc1 $f1". To fix this we would
1732 need to distinguish reading both $f0 and $f1 or just one of
1733 them. Note that we don't have to check the other way,
1734 because there is no instruction that sets both $f0 and $f1
1735 and requires a delay. */
1736 if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
bf12938e 1737 && ((EXTRACT_OPERAND (FS, *ip) & ~(unsigned) 1)
252b5132
RH
1738 == (reg &~ (unsigned) 1)))
1739 return 1;
1740 if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
bf12938e 1741 && ((EXTRACT_OPERAND (FT, *ip) & ~(unsigned) 1)
252b5132
RH
1742 == (reg &~ (unsigned) 1)))
1743 return 1;
1744 }
1745 else if (! mips_opts.mips16)
1746 {
1747 if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
bf12938e 1748 && EXTRACT_OPERAND (RS, *ip) == reg)
252b5132
RH
1749 return 1;
1750 if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
bf12938e 1751 && EXTRACT_OPERAND (RT, *ip) == reg)
252b5132
RH
1752 return 1;
1753 }
1754 else
1755 {
1756 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
bf12938e 1757 && mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RX, *ip)] == reg)
252b5132
RH
1758 return 1;
1759 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
bf12938e 1760 && mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RY, *ip)] == reg)
252b5132
RH
1761 return 1;
1762 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
bf12938e 1763 && (mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (MOVE32Z, *ip)]
252b5132
RH
1764 == reg))
1765 return 1;
1766 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1767 return 1;
1768 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1769 return 1;
1770 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1771 return 1;
1772 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
bf12938e 1773 && MIPS16_EXTRACT_OPERAND (REGR32, *ip) == reg)
252b5132
RH
1774 return 1;
1775 }
1776
1777 return 0;
1778}
1779
1780/* This function returns true if modifying a register requires a
1781 delay. */
1782
1783static int
17a2f251 1784reg_needs_delay (unsigned int reg)
252b5132
RH
1785{
1786 unsigned long prev_pinfo;
1787
47e39b9d 1788 prev_pinfo = history[0].insn_mo->pinfo;
252b5132 1789 if (! mips_opts.noreorder
81912461
ILT
1790 && (((prev_pinfo & INSN_LOAD_MEMORY_DELAY)
1791 && ! gpr_interlocks)
1792 || ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1793 && ! cop_interlocks)))
252b5132 1794 {
81912461
ILT
1795 /* A load from a coprocessor or from memory. All load delays
1796 delay the use of general register rt for one instruction. */
bdaaa2e1 1797 /* Itbl support may require additional care here. */
252b5132 1798 know (prev_pinfo & INSN_WRITE_GPR_T);
bf12938e 1799 if (reg == EXTRACT_OPERAND (RT, history[0]))
252b5132
RH
1800 return 1;
1801 }
1802
1803 return 0;
1804}
1805
404a8071
RS
1806/* Move all labels in insn_labels to the current insertion point. */
1807
1808static void
1809mips_move_labels (void)
1810{
1811 struct insn_label_list *l;
1812 valueT val;
1813
1814 for (l = insn_labels; l != NULL; l = l->next)
1815 {
1816 assert (S_GET_SEGMENT (l->label) == now_seg);
1817 symbol_set_frag (l->label, frag_now);
1818 val = (valueT) frag_now_fix ();
1819 /* mips16 text labels are stored as odd. */
1820 if (mips_opts.mips16)
1821 ++val;
1822 S_SET_VALUE (l->label, val);
1823 }
1824}
1825
252b5132
RH
1826/* Mark instruction labels in mips16 mode. This permits the linker to
1827 handle them specially, such as generating jalx instructions when
1828 needed. We also make them odd for the duration of the assembly, in
1829 order to generate the right sort of code. We will make them even
1830 in the adjust_symtab routine, while leaving them marked. This is
1831 convenient for the debugger and the disassembler. The linker knows
1832 to make them odd again. */
1833
1834static void
17a2f251 1835mips16_mark_labels (void)
252b5132
RH
1836{
1837 if (mips_opts.mips16)
1838 {
1839 struct insn_label_list *l;
98aa84af 1840 valueT val;
252b5132
RH
1841
1842 for (l = insn_labels; l != NULL; l = l->next)
1843 {
1844#ifdef OBJ_ELF
1845 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1846 S_SET_OTHER (l->label, STO_MIPS16);
1847#endif
98aa84af
AM
1848 val = S_GET_VALUE (l->label);
1849 if ((val & 1) == 0)
1850 S_SET_VALUE (l->label, val + 1);
252b5132
RH
1851 }
1852 }
1853}
1854
4d7206a2
RS
1855/* End the current frag. Make it a variant frag and record the
1856 relaxation info. */
1857
1858static void
1859relax_close_frag (void)
1860{
584892a6 1861 mips_macro_warning.first_frag = frag_now;
4d7206a2 1862 frag_var (rs_machine_dependent, 0, 0,
584892a6 1863 RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1]),
4d7206a2
RS
1864 mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
1865
1866 memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
1867 mips_relax.first_fixup = 0;
1868}
1869
1870/* Start a new relaxation sequence whose expansion depends on SYMBOL.
1871 See the comment above RELAX_ENCODE for more details. */
1872
1873static void
1874relax_start (symbolS *symbol)
1875{
1876 assert (mips_relax.sequence == 0);
1877 mips_relax.sequence = 1;
1878 mips_relax.symbol = symbol;
1879}
1880
1881/* Start generating the second version of a relaxable sequence.
1882 See the comment above RELAX_ENCODE for more details. */
252b5132
RH
1883
1884static void
4d7206a2
RS
1885relax_switch (void)
1886{
1887 assert (mips_relax.sequence == 1);
1888 mips_relax.sequence = 2;
1889}
1890
1891/* End the current relaxable sequence. */
1892
1893static void
1894relax_end (void)
1895{
1896 assert (mips_relax.sequence == 2);
1897 relax_close_frag ();
1898 mips_relax.sequence = 0;
1899}
1900
71400594
RS
1901/* Classify an instruction according to the FIX_VR4120_* enumeration.
1902 Return NUM_FIX_VR4120_CLASSES if the instruction isn't affected
1903 by VR4120 errata. */
4d7206a2 1904
71400594
RS
1905static unsigned int
1906classify_vr4120_insn (const char *name)
252b5132 1907{
71400594
RS
1908 if (strncmp (name, "macc", 4) == 0)
1909 return FIX_VR4120_MACC;
1910 if (strncmp (name, "dmacc", 5) == 0)
1911 return FIX_VR4120_DMACC;
1912 if (strncmp (name, "mult", 4) == 0)
1913 return FIX_VR4120_MULT;
1914 if (strncmp (name, "dmult", 5) == 0)
1915 return FIX_VR4120_DMULT;
1916 if (strstr (name, "div"))
1917 return FIX_VR4120_DIV;
1918 if (strcmp (name, "mtlo") == 0 || strcmp (name, "mthi") == 0)
1919 return FIX_VR4120_MTHILO;
1920 return NUM_FIX_VR4120_CLASSES;
1921}
252b5132 1922
71400594
RS
1923/* Return the number of instructions that must separate INSN1 and INSN2,
1924 where INSN1 is the earlier instruction. Return the worst-case value
1925 for any INSN2 if INSN2 is null. */
252b5132 1926
71400594
RS
1927static unsigned int
1928insns_between (const struct mips_cl_insn *insn1,
1929 const struct mips_cl_insn *insn2)
1930{
1931 unsigned long pinfo1, pinfo2;
1932
1933 /* This function needs to know which pinfo flags are set for INSN2
1934 and which registers INSN2 uses. The former is stored in PINFO2 and
1935 the latter is tested via INSN2_USES_REG. If INSN2 is null, PINFO2
1936 will have every flag set and INSN2_USES_REG will always return true. */
1937 pinfo1 = insn1->insn_mo->pinfo;
1938 pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~0U;
252b5132 1939
71400594
RS
1940#define INSN2_USES_REG(REG, CLASS) \
1941 (insn2 == NULL || insn_uses_reg (insn2, REG, CLASS))
1942
1943 /* For most targets, write-after-read dependencies on the HI and LO
1944 registers must be separated by at least two instructions. */
1945 if (!hilo_interlocks)
252b5132 1946 {
71400594
RS
1947 if ((pinfo1 & INSN_READ_LO) && (pinfo2 & INSN_WRITE_LO))
1948 return 2;
1949 if ((pinfo1 & INSN_READ_HI) && (pinfo2 & INSN_WRITE_HI))
1950 return 2;
1951 }
1952
1953 /* If we're working around r7000 errata, there must be two instructions
1954 between an mfhi or mflo and any instruction that uses the result. */
1955 if (mips_7000_hilo_fix
1956 && MF_HILO_INSN (pinfo1)
1957 && INSN2_USES_REG (EXTRACT_OPERAND (RD, *insn1), MIPS_GR_REG))
1958 return 2;
1959
1960 /* If working around VR4120 errata, check for combinations that need
1961 a single intervening instruction. */
1962 if (mips_fix_vr4120)
1963 {
1964 unsigned int class1, class2;
252b5132 1965
71400594
RS
1966 class1 = classify_vr4120_insn (insn1->insn_mo->name);
1967 if (class1 != NUM_FIX_VR4120_CLASSES && vr4120_conflicts[class1] != 0)
252b5132 1968 {
71400594
RS
1969 if (insn2 == NULL)
1970 return 1;
1971 class2 = classify_vr4120_insn (insn2->insn_mo->name);
1972 if (vr4120_conflicts[class1] & (1 << class2))
1973 return 1;
252b5132 1974 }
71400594
RS
1975 }
1976
1977 if (!mips_opts.mips16)
1978 {
1979 /* Check for GPR or coprocessor load delays. All such delays
1980 are on the RT register. */
1981 /* Itbl support may require additional care here. */
1982 if ((!gpr_interlocks && (pinfo1 & INSN_LOAD_MEMORY_DELAY))
1983 || (!cop_interlocks && (pinfo1 & INSN_LOAD_COPROC_DELAY)))
252b5132 1984 {
71400594
RS
1985 know (pinfo1 & INSN_WRITE_GPR_T);
1986 if (INSN2_USES_REG (EXTRACT_OPERAND (RT, *insn1), MIPS_GR_REG))
1987 return 1;
1988 }
1989
1990 /* Check for generic coprocessor hazards.
1991
1992 This case is not handled very well. There is no special
1993 knowledge of CP0 handling, and the coprocessors other than
1994 the floating point unit are not distinguished at all. */
1995 /* Itbl support may require additional care here. FIXME!
1996 Need to modify this to include knowledge about
1997 user specified delays! */
1998 else if ((!cop_interlocks && (pinfo1 & INSN_COPROC_MOVE_DELAY))
1999 || (!cop_mem_interlocks && (pinfo1 & INSN_COPROC_MEMORY_DELAY)))
2000 {
2001 /* Handle cases where INSN1 writes to a known general coprocessor
2002 register. There must be a one instruction delay before INSN2
2003 if INSN2 reads that register, otherwise no delay is needed. */
2004 if (pinfo1 & INSN_WRITE_FPR_T)
252b5132 2005 {
71400594
RS
2006 if (INSN2_USES_REG (EXTRACT_OPERAND (FT, *insn1), MIPS_FP_REG))
2007 return 1;
252b5132 2008 }
71400594 2009 else if (pinfo1 & INSN_WRITE_FPR_S)
252b5132 2010 {
71400594
RS
2011 if (INSN2_USES_REG (EXTRACT_OPERAND (FS, *insn1), MIPS_FP_REG))
2012 return 1;
252b5132
RH
2013 }
2014 else
2015 {
71400594
RS
2016 /* Read-after-write dependencies on the control registers
2017 require a two-instruction gap. */
2018 if ((pinfo1 & INSN_WRITE_COND_CODE)
2019 && (pinfo2 & INSN_READ_COND_CODE))
2020 return 2;
2021
2022 /* We don't know exactly what INSN1 does. If INSN2 is
2023 also a coprocessor instruction, assume there must be
2024 a one instruction gap. */
2025 if (pinfo2 & INSN_COP)
2026 return 1;
252b5132
RH
2027 }
2028 }
6b76fefe 2029
71400594
RS
2030 /* Check for read-after-write dependencies on the coprocessor
2031 control registers in cases where INSN1 does not need a general
2032 coprocessor delay. This means that INSN1 is a floating point
2033 comparison instruction. */
2034 /* Itbl support may require additional care here. */
2035 else if (!cop_interlocks
2036 && (pinfo1 & INSN_WRITE_COND_CODE)
2037 && (pinfo2 & INSN_READ_COND_CODE))
2038 return 1;
2039 }
6b76fefe 2040
71400594 2041#undef INSN2_USES_REG
6b76fefe 2042
71400594
RS
2043 return 0;
2044}
6b76fefe 2045
7d8e00cf
RS
2046/* Return the number of nops that would be needed to work around the
2047 VR4130 mflo/mfhi errata if instruction INSN immediately followed
2048 the MAX_VR4130_NOPS instructions described by HISTORY. */
2049
2050static int
2051nops_for_vr4130 (const struct mips_cl_insn *history,
2052 const struct mips_cl_insn *insn)
2053{
2054 int i, j, reg;
2055
2056 /* Check if the instruction writes to HI or LO. MTHI and MTLO
2057 are not affected by the errata. */
2058 if (insn != 0
2059 && ((insn->insn_mo->pinfo & (INSN_WRITE_HI | INSN_WRITE_LO)) == 0
2060 || strcmp (insn->insn_mo->name, "mtlo") == 0
2061 || strcmp (insn->insn_mo->name, "mthi") == 0))
2062 return 0;
2063
2064 /* Search for the first MFLO or MFHI. */
2065 for (i = 0; i < MAX_VR4130_NOPS; i++)
2066 if (!history[i].noreorder_p && MF_HILO_INSN (history[i].insn_mo->pinfo))
2067 {
2068 /* Extract the destination register. */
2069 if (mips_opts.mips16)
2070 reg = mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RX, history[i])];
2071 else
2072 reg = EXTRACT_OPERAND (RD, history[i]);
2073
2074 /* No nops are needed if INSN reads that register. */
2075 if (insn != NULL && insn_uses_reg (insn, reg, MIPS_GR_REG))
2076 return 0;
2077
2078 /* ...or if any of the intervening instructions do. */
2079 for (j = 0; j < i; j++)
2080 if (insn_uses_reg (&history[j], reg, MIPS_GR_REG))
2081 return 0;
2082
2083 return MAX_VR4130_NOPS - i;
2084 }
2085 return 0;
2086}
2087
71400594
RS
2088/* Return the number of nops that would be needed if instruction INSN
2089 immediately followed the MAX_NOPS instructions given by HISTORY,
2090 where HISTORY[0] is the most recent instruction. If INSN is null,
2091 return the worse-case number of nops for any instruction. */
bdaaa2e1 2092
71400594
RS
2093static int
2094nops_for_insn (const struct mips_cl_insn *history,
2095 const struct mips_cl_insn *insn)
2096{
2097 int i, nops, tmp_nops;
bdaaa2e1 2098
71400594 2099 nops = 0;
7d8e00cf 2100 for (i = 0; i < MAX_DELAY_NOPS; i++)
71400594
RS
2101 if (!history[i].noreorder_p)
2102 {
2103 tmp_nops = insns_between (history + i, insn) - i;
2104 if (tmp_nops > nops)
2105 nops = tmp_nops;
2106 }
7d8e00cf
RS
2107
2108 if (mips_fix_vr4130)
2109 {
2110 tmp_nops = nops_for_vr4130 (history, insn);
2111 if (tmp_nops > nops)
2112 nops = tmp_nops;
2113 }
2114
71400594
RS
2115 return nops;
2116}
252b5132 2117
71400594
RS
2118/* The variable arguments provide NUM_INSNS extra instructions that
2119 might be added to HISTORY. Return the largest number of nops that
2120 would be needed after the extended sequence. */
252b5132 2121
71400594
RS
2122static int
2123nops_for_sequence (int num_insns, const struct mips_cl_insn *history, ...)
2124{
2125 va_list args;
2126 struct mips_cl_insn buffer[MAX_NOPS];
2127 struct mips_cl_insn *cursor;
2128 int nops;
2129
2130 va_start (args, history);
2131 cursor = buffer + num_insns;
2132 memcpy (cursor, history, (MAX_NOPS - num_insns) * sizeof (*cursor));
2133 while (cursor > buffer)
2134 *--cursor = *va_arg (args, const struct mips_cl_insn *);
2135
2136 nops = nops_for_insn (buffer, NULL);
2137 va_end (args);
2138 return nops;
2139}
252b5132 2140
71400594
RS
2141/* Like nops_for_insn, but if INSN is a branch, take into account the
2142 worst-case delay for the branch target. */
252b5132 2143
71400594
RS
2144static int
2145nops_for_insn_or_target (const struct mips_cl_insn *history,
2146 const struct mips_cl_insn *insn)
2147{
2148 int nops, tmp_nops;
60b63b72 2149
71400594
RS
2150 nops = nops_for_insn (history, insn);
2151 if (insn->insn_mo->pinfo & (INSN_UNCOND_BRANCH_DELAY
2152 | INSN_COND_BRANCH_DELAY
2153 | INSN_COND_BRANCH_LIKELY))
2154 {
2155 tmp_nops = nops_for_sequence (2, history, insn, NOP_INSN);
2156 if (tmp_nops > nops)
2157 nops = tmp_nops;
2158 }
2159 else if (mips_opts.mips16 && (insn->insn_mo->pinfo & MIPS16_INSN_BRANCH))
2160 {
2161 tmp_nops = nops_for_sequence (1, history, insn);
2162 if (tmp_nops > nops)
2163 nops = tmp_nops;
2164 }
2165 return nops;
2166}
2167
2168/* Output an instruction. IP is the instruction information.
2169 ADDRESS_EXPR is an operand of the instruction to be used with
2170 RELOC_TYPE. */
2171
2172static void
2173append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
2174 bfd_reloc_code_real_type *reloc_type)
2175{
2176 register unsigned long prev_pinfo, pinfo;
2177 relax_stateT prev_insn_frag_type = 0;
2178 bfd_boolean relaxed_branch = FALSE;
2179
2180 /* Mark instruction labels in mips16 mode. */
2181 mips16_mark_labels ();
2182
2183 prev_pinfo = history[0].insn_mo->pinfo;
2184 pinfo = ip->insn_mo->pinfo;
2185
2186 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
2187 {
2188 /* There are a lot of optimizations we could do that we don't.
2189 In particular, we do not, in general, reorder instructions.
2190 If you use gcc with optimization, it will reorder
2191 instructions and generally do much more optimization then we
2192 do here; repeating all that work in the assembler would only
2193 benefit hand written assembly code, and does not seem worth
2194 it. */
2195 int nops = (mips_optimize == 0
2196 ? nops_for_insn (history, NULL)
2197 : nops_for_insn_or_target (history, ip));
2198 if (nops > 0)
252b5132
RH
2199 {
2200 fragS *old_frag;
2201 unsigned long old_frag_offset;
2202 int i;
252b5132
RH
2203
2204 old_frag = frag_now;
2205 old_frag_offset = frag_now_fix ();
2206
2207 for (i = 0; i < nops; i++)
2208 emit_nop ();
2209
2210 if (listing)
2211 {
2212 listing_prev_line ();
2213 /* We may be at the start of a variant frag. In case we
2214 are, make sure there is enough space for the frag
2215 after the frags created by listing_prev_line. The
2216 argument to frag_grow here must be at least as large
2217 as the argument to all other calls to frag_grow in
2218 this file. We don't have to worry about being in the
2219 middle of a variant frag, because the variants insert
2220 all needed nop instructions themselves. */
2221 frag_grow (40);
2222 }
2223
404a8071 2224 mips_move_labels ();
252b5132
RH
2225
2226#ifndef NO_ECOFF_DEBUGGING
2227 if (ECOFF_DEBUGGING)
2228 ecoff_fix_loc (old_frag, old_frag_offset);
2229#endif
2230 }
71400594
RS
2231 }
2232 else if (mips_relax.sequence != 2 && prev_nop_frag != NULL)
2233 {
2234 /* Work out how many nops in prev_nop_frag are needed by IP. */
2235 int nops = nops_for_insn_or_target (history, ip);
2236 assert (nops <= prev_nop_frag_holds);
252b5132 2237
71400594
RS
2238 /* Enforce NOPS as a minimum. */
2239 if (nops > prev_nop_frag_required)
2240 prev_nop_frag_required = nops;
252b5132 2241
71400594
RS
2242 if (prev_nop_frag_holds == prev_nop_frag_required)
2243 {
2244 /* Settle for the current number of nops. Update the history
2245 accordingly (for the benefit of any future .set reorder code). */
2246 prev_nop_frag = NULL;
2247 insert_into_history (prev_nop_frag_since,
2248 prev_nop_frag_holds, NOP_INSN);
2249 }
2250 else
2251 {
2252 /* Allow this instruction to replace one of the nops that was
2253 tentatively added to prev_nop_frag. */
2254 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
2255 prev_nop_frag_holds--;
2256 prev_nop_frag_since++;
252b5132
RH
2257 }
2258 }
2259
58e2ea4d
MR
2260#ifdef OBJ_ELF
2261 /* The value passed to dwarf2_emit_insn is the distance between
2262 the beginning of the current instruction and the address that
2263 should be recorded in the debug tables. For MIPS16 debug info
2264 we want to use ISA-encoded addresses, so we pass -1 for an
2265 address higher by one than the current. */
2266 dwarf2_emit_insn (mips_opts.mips16 ? -1 : 0);
2267#endif
2268
895921c9 2269 /* Record the frag type before frag_var. */
47e39b9d
RS
2270 if (history[0].frag)
2271 prev_insn_frag_type = history[0].frag->fr_type;
895921c9 2272
4d7206a2 2273 if (address_expr
0b25d3e6 2274 && *reloc_type == BFD_RELOC_16_PCREL_S2
4a6a3df4
AO
2275 && (pinfo & INSN_UNCOND_BRANCH_DELAY || pinfo & INSN_COND_BRANCH_DELAY
2276 || pinfo & INSN_COND_BRANCH_LIKELY)
2277 && mips_relax_branch
2278 /* Don't try branch relaxation within .set nomacro, or within
2279 .set noat if we use $at for PIC computations. If it turns
2280 out that the branch was out-of-range, we'll get an error. */
2281 && !mips_opts.warn_about_macros
2282 && !(mips_opts.noat && mips_pic != NO_PIC)
2283 && !mips_opts.mips16)
2284 {
895921c9 2285 relaxed_branch = TRUE;
1e915849
RS
2286 add_relaxed_insn (ip, (relaxed_branch_length
2287 (NULL, NULL,
2288 (pinfo & INSN_UNCOND_BRANCH_DELAY) ? -1
2289 : (pinfo & INSN_COND_BRANCH_LIKELY) ? 1
2290 : 0)), 4,
2291 RELAX_BRANCH_ENCODE
2292 (pinfo & INSN_UNCOND_BRANCH_DELAY,
2293 pinfo & INSN_COND_BRANCH_LIKELY,
2294 pinfo & INSN_WRITE_GPR_31,
2295 0),
2296 address_expr->X_add_symbol,
2297 address_expr->X_add_number);
4a6a3df4
AO
2298 *reloc_type = BFD_RELOC_UNUSED;
2299 }
2300 else if (*reloc_type > BFD_RELOC_UNUSED)
252b5132
RH
2301 {
2302 /* We need to set up a variant frag. */
2303 assert (mips_opts.mips16 && address_expr != NULL);
1e915849
RS
2304 add_relaxed_insn (ip, 4, 0,
2305 RELAX_MIPS16_ENCODE
2306 (*reloc_type - BFD_RELOC_UNUSED,
2307 mips16_small, mips16_ext,
2308 prev_pinfo & INSN_UNCOND_BRANCH_DELAY,
2309 history[0].mips16_absolute_jump_p),
2310 make_expr_symbol (address_expr), 0);
252b5132 2311 }
252b5132
RH
2312 else if (mips_opts.mips16
2313 && ! ip->use_extend
f6688943 2314 && *reloc_type != BFD_RELOC_MIPS16_JMP)
9497f5ac 2315 {
b8ee1a6e
DU
2316 if ((pinfo & INSN_UNCOND_BRANCH_DELAY) == 0)
2317 /* Make sure there is enough room to swap this instruction with
2318 a following jump instruction. */
2319 frag_grow (6);
1e915849 2320 add_fixed_insn (ip);
252b5132
RH
2321 }
2322 else
2323 {
2324 if (mips_opts.mips16
2325 && mips_opts.noreorder
2326 && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
2327 as_warn (_("extended instruction in delay slot"));
2328
4d7206a2
RS
2329 if (mips_relax.sequence)
2330 {
2331 /* If we've reached the end of this frag, turn it into a variant
2332 frag and record the information for the instructions we've
2333 written so far. */
2334 if (frag_room () < 4)
2335 relax_close_frag ();
2336 mips_relax.sizes[mips_relax.sequence - 1] += 4;
2337 }
2338
584892a6
RS
2339 if (mips_relax.sequence != 2)
2340 mips_macro_warning.sizes[0] += 4;
2341 if (mips_relax.sequence != 1)
2342 mips_macro_warning.sizes[1] += 4;
2343
1e915849
RS
2344 if (mips_opts.mips16)
2345 {
2346 ip->fixed_p = 1;
2347 ip->mips16_absolute_jump_p = (*reloc_type == BFD_RELOC_MIPS16_JMP);
2348 }
2349 add_fixed_insn (ip);
252b5132
RH
2350 }
2351
01a3f561 2352 if (address_expr != NULL && *reloc_type <= BFD_RELOC_UNUSED)
252b5132
RH
2353 {
2354 if (address_expr->X_op == O_constant)
2355 {
f17c130b 2356 unsigned int tmp;
f6688943
TS
2357
2358 switch (*reloc_type)
252b5132
RH
2359 {
2360 case BFD_RELOC_32:
2361 ip->insn_opcode |= address_expr->X_add_number;
2362 break;
2363
f6688943 2364 case BFD_RELOC_MIPS_HIGHEST:
f17c130b
AM
2365 tmp = (address_expr->X_add_number + 0x800080008000ull) >> 48;
2366 ip->insn_opcode |= tmp & 0xffff;
f6688943
TS
2367 break;
2368
2369 case BFD_RELOC_MIPS_HIGHER:
f17c130b
AM
2370 tmp = (address_expr->X_add_number + 0x80008000ull) >> 32;
2371 ip->insn_opcode |= tmp & 0xffff;
f6688943
TS
2372 break;
2373
2374 case BFD_RELOC_HI16_S:
f17c130b
AM
2375 tmp = (address_expr->X_add_number + 0x8000) >> 16;
2376 ip->insn_opcode |= tmp & 0xffff;
f6688943
TS
2377 break;
2378
2379 case BFD_RELOC_HI16:
2380 ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
2381 break;
2382
01a3f561 2383 case BFD_RELOC_UNUSED:
252b5132 2384 case BFD_RELOC_LO16:
ed6fb7bd 2385 case BFD_RELOC_MIPS_GOT_DISP:
252b5132
RH
2386 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2387 break;
2388
2389 case BFD_RELOC_MIPS_JMP:
2390 if ((address_expr->X_add_number & 3) != 0)
2391 as_bad (_("jump to misaligned address (0x%lx)"),
2392 (unsigned long) address_expr->X_add_number);
f3c0ec86 2393 if (address_expr->X_add_number & ~0xfffffff)
7496292d
TS
2394 as_bad (_("jump address range overflow (0x%lx)"),
2395 (unsigned long) address_expr->X_add_number);
252b5132
RH
2396 ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
2397 break;
2398
2399 case BFD_RELOC_MIPS16_JMP:
2400 if ((address_expr->X_add_number & 3) != 0)
2401 as_bad (_("jump to misaligned address (0x%lx)"),
2402 (unsigned long) address_expr->X_add_number);
f3c0ec86 2403 if (address_expr->X_add_number & ~0xfffffff)
7496292d
TS
2404 as_bad (_("jump address range overflow (0x%lx)"),
2405 (unsigned long) address_expr->X_add_number);
252b5132
RH
2406 ip->insn_opcode |=
2407 (((address_expr->X_add_number & 0x7c0000) << 3)
2408 | ((address_expr->X_add_number & 0xf800000) >> 7)
2409 | ((address_expr->X_add_number & 0x3fffc) >> 2));
2410 break;
2411
252b5132 2412 case BFD_RELOC_16_PCREL_S2:
bad36eac
DJ
2413 if ((address_expr->X_add_number & 3) != 0)
2414 as_bad (_("branch to misaligned address (0x%lx)"),
2415 (unsigned long) address_expr->X_add_number);
2416 if (mips_relax_branch)
2417 goto need_reloc;
2418 if ((address_expr->X_add_number + 0x20000) & ~0x3ffff)
2419 as_bad (_("branch address range overflow (0x%lx)"),
2420 (unsigned long) address_expr->X_add_number);
2421 ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0xffff;
2422 break;
252b5132
RH
2423
2424 default:
2425 internalError ();
2426 }
2427 }
01a3f561 2428 else if (*reloc_type < BFD_RELOC_UNUSED)
252b5132 2429 need_reloc:
4d7206a2
RS
2430 {
2431 reloc_howto_type *howto;
2432 int i;
34ce925e 2433
4d7206a2
RS
2434 /* In a compound relocation, it is the final (outermost)
2435 operator that determines the relocated field. */
2436 for (i = 1; i < 3; i++)
2437 if (reloc_type[i] == BFD_RELOC_UNUSED)
2438 break;
34ce925e 2439
4d7206a2 2440 howto = bfd_reloc_type_lookup (stdoutput, reloc_type[i - 1]);
1e915849
RS
2441 ip->fixp[0] = fix_new_exp (ip->frag, ip->where,
2442 bfd_get_reloc_size (howto),
2443 address_expr,
2444 reloc_type[0] == BFD_RELOC_16_PCREL_S2,
2445 reloc_type[0]);
4d7206a2
RS
2446
2447 /* These relocations can have an addend that won't fit in
2448 4 octets for 64bit assembly. */
2449 if (HAVE_64BIT_GPRS
2450 && ! howto->partial_inplace
2451 && (reloc_type[0] == BFD_RELOC_16
2452 || reloc_type[0] == BFD_RELOC_32
2453 || reloc_type[0] == BFD_RELOC_MIPS_JMP
2454 || reloc_type[0] == BFD_RELOC_HI16_S
2455 || reloc_type[0] == BFD_RELOC_LO16
2456 || reloc_type[0] == BFD_RELOC_GPREL16
2457 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
2458 || reloc_type[0] == BFD_RELOC_GPREL32
2459 || reloc_type[0] == BFD_RELOC_64
2460 || reloc_type[0] == BFD_RELOC_CTOR
2461 || reloc_type[0] == BFD_RELOC_MIPS_SUB
2462 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
2463 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
2464 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
2465 || reloc_type[0] == BFD_RELOC_MIPS_REL16
d6f16593
MR
2466 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
2467 || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
2468 || reloc_type[0] == BFD_RELOC_MIPS16_HI16_S
2469 || reloc_type[0] == BFD_RELOC_MIPS16_LO16))
1e915849 2470 ip->fixp[0]->fx_no_overflow = 1;
4d7206a2
RS
2471
2472 if (mips_relax.sequence)
2473 {
2474 if (mips_relax.first_fixup == 0)
1e915849 2475 mips_relax.first_fixup = ip->fixp[0];
4d7206a2
RS
2476 }
2477 else if (reloc_needs_lo_p (*reloc_type))
2478 {
2479 struct mips_hi_fixup *hi_fixup;
252b5132 2480
4d7206a2
RS
2481 /* Reuse the last entry if it already has a matching %lo. */
2482 hi_fixup = mips_hi_fixup_list;
2483 if (hi_fixup == 0
2484 || !fixup_has_matching_lo_p (hi_fixup->fixp))
2485 {
2486 hi_fixup = ((struct mips_hi_fixup *)
2487 xmalloc (sizeof (struct mips_hi_fixup)));
2488 hi_fixup->next = mips_hi_fixup_list;
2489 mips_hi_fixup_list = hi_fixup;
252b5132 2490 }
1e915849 2491 hi_fixup->fixp = ip->fixp[0];
4d7206a2
RS
2492 hi_fixup->seg = now_seg;
2493 }
f6688943 2494
4d7206a2
RS
2495 /* Add fixups for the second and third relocations, if given.
2496 Note that the ABI allows the second relocation to be
2497 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the
2498 moment we only use RSS_UNDEF, but we could add support
2499 for the others if it ever becomes necessary. */
2500 for (i = 1; i < 3; i++)
2501 if (reloc_type[i] != BFD_RELOC_UNUSED)
2502 {
1e915849
RS
2503 ip->fixp[i] = fix_new (ip->frag, ip->where,
2504 ip->fixp[0]->fx_size, NULL, 0,
2505 FALSE, reloc_type[i]);
b1dca8ee
RS
2506
2507 /* Use fx_tcbit to mark compound relocs. */
1e915849
RS
2508 ip->fixp[0]->fx_tcbit = 1;
2509 ip->fixp[i]->fx_tcbit = 1;
4d7206a2 2510 }
252b5132
RH
2511 }
2512 }
1e915849 2513 install_insn (ip);
252b5132
RH
2514
2515 /* Update the register mask information. */
2516 if (! mips_opts.mips16)
2517 {
2518 if (pinfo & INSN_WRITE_GPR_D)
bf12938e 2519 mips_gprmask |= 1 << EXTRACT_OPERAND (RD, *ip);
252b5132 2520 if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
bf12938e 2521 mips_gprmask |= 1 << EXTRACT_OPERAND (RT, *ip);
252b5132 2522 if (pinfo & INSN_READ_GPR_S)
bf12938e 2523 mips_gprmask |= 1 << EXTRACT_OPERAND (RS, *ip);
252b5132 2524 if (pinfo & INSN_WRITE_GPR_31)
f9419b05 2525 mips_gprmask |= 1 << RA;
252b5132 2526 if (pinfo & INSN_WRITE_FPR_D)
bf12938e 2527 mips_cprmask[1] |= 1 << EXTRACT_OPERAND (FD, *ip);
252b5132 2528 if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
bf12938e 2529 mips_cprmask[1] |= 1 << EXTRACT_OPERAND (FS, *ip);
252b5132 2530 if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
bf12938e 2531 mips_cprmask[1] |= 1 << EXTRACT_OPERAND (FT, *ip);
252b5132 2532 if ((pinfo & INSN_READ_FPR_R) != 0)
bf12938e 2533 mips_cprmask[1] |= 1 << EXTRACT_OPERAND (FR, *ip);
252b5132
RH
2534 if (pinfo & INSN_COP)
2535 {
bdaaa2e1
KH
2536 /* We don't keep enough information to sort these cases out.
2537 The itbl support does keep this information however, although
2538 we currently don't support itbl fprmats as part of the cop
2539 instruction. May want to add this support in the future. */
252b5132
RH
2540 }
2541 /* Never set the bit for $0, which is always zero. */
beae10d5 2542 mips_gprmask &= ~1 << 0;
252b5132
RH
2543 }
2544 else
2545 {
2546 if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
bf12938e 2547 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (RX, *ip);
252b5132 2548 if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
bf12938e 2549 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (RY, *ip);
252b5132 2550 if (pinfo & MIPS16_INSN_WRITE_Z)
bf12938e 2551 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (RZ, *ip);
252b5132
RH
2552 if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2553 mips_gprmask |= 1 << TREG;
2554 if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2555 mips_gprmask |= 1 << SP;
2556 if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2557 mips_gprmask |= 1 << RA;
2558 if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2559 mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2560 if (pinfo & MIPS16_INSN_READ_Z)
bf12938e 2561 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (MOVE32Z, *ip);
252b5132 2562 if (pinfo & MIPS16_INSN_READ_GPR_X)
bf12938e 2563 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (REGR32, *ip);
252b5132
RH
2564 }
2565
4d7206a2 2566 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
252b5132
RH
2567 {
2568 /* Filling the branch delay slot is more complex. We try to
2569 switch the branch with the previous instruction, which we can
2570 do if the previous instruction does not set up a condition
2571 that the branch tests and if the branch is not itself the
2572 target of any branch. */
2573 if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2574 || (pinfo & INSN_COND_BRANCH_DELAY))
2575 {
2576 if (mips_optimize < 2
2577 /* If we have seen .set volatile or .set nomove, don't
2578 optimize. */
2579 || mips_opts.nomove != 0
a38419a5
RS
2580 /* We can't swap if the previous instruction's position
2581 is fixed. */
2582 || history[0].fixed_p
252b5132
RH
2583 /* If the previous previous insn was in a .set
2584 noreorder, we can't swap. Actually, the MIPS
2585 assembler will swap in this situation. However, gcc
2586 configured -with-gnu-as will generate code like
2587 .set noreorder
2588 lw $4,XXX
2589 .set reorder
2590 INSN
2591 bne $4,$0,foo
2592 in which we can not swap the bne and INSN. If gcc is
2593 not configured -with-gnu-as, it does not output the
a38419a5 2594 .set pseudo-ops. */
47e39b9d 2595 || history[1].noreorder_p
252b5132
RH
2596 /* If the branch is itself the target of a branch, we
2597 can not swap. We cheat on this; all we check for is
2598 whether there is a label on this instruction. If
2599 there are any branches to anything other than a
2600 label, users must use .set noreorder. */
2601 || insn_labels != NULL
895921c9
MR
2602 /* If the previous instruction is in a variant frag
2603 other than this branch's one, we cannot do the swap.
2604 This does not apply to the mips16, which uses variant
2605 frags for different purposes. */
252b5132 2606 || (! mips_opts.mips16
895921c9 2607 && prev_insn_frag_type == rs_machine_dependent)
71400594
RS
2608 /* Check for conflicts between the branch and the instructions
2609 before the candidate delay slot. */
2610 || nops_for_insn (history + 1, ip) > 0
2611 /* Check for conflicts between the swapped sequence and the
2612 target of the branch. */
2613 || nops_for_sequence (2, history + 1, ip, history) > 0
252b5132
RH
2614 /* We do not swap with a trap instruction, since it
2615 complicates trap handlers to have the trap
2616 instruction be in a delay slot. */
2617 || (prev_pinfo & INSN_TRAP)
2618 /* If the branch reads a register that the previous
2619 instruction sets, we can not swap. */
2620 || (! mips_opts.mips16
2621 && (prev_pinfo & INSN_WRITE_GPR_T)
bf12938e 2622 && insn_uses_reg (ip, EXTRACT_OPERAND (RT, history[0]),
252b5132
RH
2623 MIPS_GR_REG))
2624 || (! mips_opts.mips16
2625 && (prev_pinfo & INSN_WRITE_GPR_D)
bf12938e 2626 && insn_uses_reg (ip, EXTRACT_OPERAND (RD, history[0]),
252b5132
RH
2627 MIPS_GR_REG))
2628 || (mips_opts.mips16
2629 && (((prev_pinfo & MIPS16_INSN_WRITE_X)
bf12938e
RS
2630 && (insn_uses_reg
2631 (ip, MIPS16_EXTRACT_OPERAND (RX, history[0]),
2632 MIPS16_REG)))
252b5132 2633 || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
bf12938e
RS
2634 && (insn_uses_reg
2635 (ip, MIPS16_EXTRACT_OPERAND (RY, history[0]),
2636 MIPS16_REG)))
252b5132 2637 || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
bf12938e
RS
2638 && (insn_uses_reg
2639 (ip, MIPS16_EXTRACT_OPERAND (RZ, history[0]),
2640 MIPS16_REG)))
252b5132
RH
2641 || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2642 && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2643 || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2644 && insn_uses_reg (ip, RA, MIPS_GR_REG))
2645 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2646 && insn_uses_reg (ip,
47e39b9d
RS
2647 MIPS16OP_EXTRACT_REG32R
2648 (history[0].insn_opcode),
252b5132
RH
2649 MIPS_GR_REG))))
2650 /* If the branch writes a register that the previous
2651 instruction sets, we can not swap (we know that
2652 branches write only to RD or to $31). */
2653 || (! mips_opts.mips16
2654 && (prev_pinfo & INSN_WRITE_GPR_T)
2655 && (((pinfo & INSN_WRITE_GPR_D)
bf12938e
RS
2656 && (EXTRACT_OPERAND (RT, history[0])
2657 == EXTRACT_OPERAND (RD, *ip)))
252b5132 2658 || ((pinfo & INSN_WRITE_GPR_31)
bf12938e 2659 && EXTRACT_OPERAND (RT, history[0]) == RA)))
252b5132
RH
2660 || (! mips_opts.mips16
2661 && (prev_pinfo & INSN_WRITE_GPR_D)
2662 && (((pinfo & INSN_WRITE_GPR_D)
bf12938e
RS
2663 && (EXTRACT_OPERAND (RD, history[0])
2664 == EXTRACT_OPERAND (RD, *ip)))
252b5132 2665 || ((pinfo & INSN_WRITE_GPR_31)
bf12938e 2666 && EXTRACT_OPERAND (RD, history[0]) == RA)))
252b5132
RH
2667 || (mips_opts.mips16
2668 && (pinfo & MIPS16_INSN_WRITE_31)
2669 && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2670 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
47e39b9d 2671 && (MIPS16OP_EXTRACT_REG32R (history[0].insn_opcode)
252b5132
RH
2672 == RA))))
2673 /* If the branch writes a register that the previous
2674 instruction reads, we can not swap (we know that
2675 branches only write to RD or to $31). */
2676 || (! mips_opts.mips16
2677 && (pinfo & INSN_WRITE_GPR_D)
47e39b9d 2678 && insn_uses_reg (&history[0],
bf12938e 2679 EXTRACT_OPERAND (RD, *ip),
252b5132
RH
2680 MIPS_GR_REG))
2681 || (! mips_opts.mips16
2682 && (pinfo & INSN_WRITE_GPR_31)
47e39b9d 2683 && insn_uses_reg (&history[0], RA, MIPS_GR_REG))
252b5132
RH
2684 || (mips_opts.mips16
2685 && (pinfo & MIPS16_INSN_WRITE_31)
47e39b9d 2686 && insn_uses_reg (&history[0], RA, MIPS_GR_REG))
252b5132
RH
2687 /* If one instruction sets a condition code and the
2688 other one uses a condition code, we can not swap. */
2689 || ((pinfo & INSN_READ_COND_CODE)
2690 && (prev_pinfo & INSN_WRITE_COND_CODE))
2691 || ((pinfo & INSN_WRITE_COND_CODE)
2692 && (prev_pinfo & INSN_READ_COND_CODE))
2693 /* If the previous instruction uses the PC, we can not
2694 swap. */
2695 || (mips_opts.mips16
2696 && (prev_pinfo & MIPS16_INSN_READ_PC))
252b5132
RH
2697 /* If the previous instruction had a fixup in mips16
2698 mode, we can not swap. This normally means that the
2699 previous instruction was a 4 byte branch anyhow. */
47e39b9d 2700 || (mips_opts.mips16 && history[0].fixp[0])
bdaaa2e1
KH
2701 /* If the previous instruction is a sync, sync.l, or
2702 sync.p, we can not swap. */
f173e82e 2703 || (prev_pinfo & INSN_SYNC))
252b5132 2704 {
29024861
DU
2705 if (mips_opts.mips16
2706 && (pinfo & INSN_UNCOND_BRANCH_DELAY)
2707 && (pinfo & (MIPS16_INSN_READ_X | MIPS16_INSN_READ_31))
2708 && (mips_opts.isa == ISA_MIPS32
2709 || mips_opts.isa == ISA_MIPS32R2
2710 || mips_opts.isa == ISA_MIPS64
2711 || mips_opts.isa == ISA_MIPS64R2))
2712 {
2713 /* Convert MIPS16 jr/jalr into a "compact" jump. */
2714 ip->insn_opcode |= 0x0080;
2715 install_insn (ip);
2716 insert_into_history (0, 1, ip);
2717 }
2718 else
2719 {
2720 /* We could do even better for unconditional branches to
2721 portions of this object file; we could pick up the
2722 instruction at the destination, put it in the delay
2723 slot, and bump the destination address. */
2724 insert_into_history (0, 1, ip);
2725 emit_nop ();
2726 }
2727
dd22970f
ILT
2728 if (mips_relax.sequence)
2729 mips_relax.sizes[mips_relax.sequence - 1] += 4;
252b5132
RH
2730 }
2731 else
2732 {
2733 /* It looks like we can actually do the swap. */
1e915849
RS
2734 struct mips_cl_insn delay = history[0];
2735 if (mips_opts.mips16)
252b5132 2736 {
b8ee1a6e
DU
2737 know (delay.frag == ip->frag);
2738 move_insn (ip, delay.frag, delay.where);
2739 move_insn (&delay, ip->frag, ip->where + insn_length (ip));
1e915849
RS
2740 }
2741 else if (relaxed_branch)
2742 {
2743 /* Add the delay slot instruction to the end of the
2744 current frag and shrink the fixed part of the
2745 original frag. If the branch occupies the tail of
2746 the latter, move it backwards to cover the gap. */
2747 delay.frag->fr_fix -= 4;
2748 if (delay.frag == ip->frag)
2749 move_insn (ip, ip->frag, ip->where - 4);
2750 add_fixed_insn (&delay);
252b5132
RH
2751 }
2752 else
2753 {
1e915849
RS
2754 move_insn (&delay, ip->frag, ip->where);
2755 move_insn (ip, history[0].frag, history[0].where);
252b5132 2756 }
1e915849
RS
2757 history[0] = *ip;
2758 delay.fixed_p = 1;
2759 insert_into_history (0, 1, &delay);
252b5132 2760 }
252b5132
RH
2761
2762 /* If that was an unconditional branch, forget the previous
2763 insn information. */
2764 if (pinfo & INSN_UNCOND_BRANCH_DELAY)
7d10b47d 2765 mips_no_prev_insn ();
252b5132
RH
2766 }
2767 else if (pinfo & INSN_COND_BRANCH_LIKELY)
2768 {
2769 /* We don't yet optimize a branch likely. What we should do
2770 is look at the target, copy the instruction found there
2771 into the delay slot, and increment the branch to jump to
2772 the next instruction. */
1e915849 2773 insert_into_history (0, 1, ip);
252b5132 2774 emit_nop ();
252b5132
RH
2775 }
2776 else
1e915849 2777 insert_into_history (0, 1, ip);
252b5132 2778 }
1e915849
RS
2779 else
2780 insert_into_history (0, 1, ip);
252b5132
RH
2781
2782 /* We just output an insn, so the next one doesn't have a label. */
2783 mips_clear_insn_labels ();
252b5132
RH
2784}
2785
7d10b47d 2786/* Forget that there was any previous instruction or label. */
252b5132
RH
2787
2788static void
7d10b47d 2789mips_no_prev_insn (void)
252b5132 2790{
7d10b47d
RS
2791 prev_nop_frag = NULL;
2792 insert_into_history (0, ARRAY_SIZE (history), NOP_INSN);
252b5132
RH
2793 mips_clear_insn_labels ();
2794}
2795
7d10b47d
RS
2796/* This function must be called before we emit something other than
2797 instructions. It is like mips_no_prev_insn except that it inserts
2798 any NOPS that might be needed by previous instructions. */
252b5132 2799
7d10b47d
RS
2800void
2801mips_emit_delays (void)
252b5132
RH
2802{
2803 if (! mips_opts.noreorder)
2804 {
71400594 2805 int nops = nops_for_insn (history, NULL);
252b5132
RH
2806 if (nops > 0)
2807 {
7d10b47d
RS
2808 while (nops-- > 0)
2809 add_fixed_insn (NOP_INSN);
2810 mips_move_labels ();
2811 }
2812 }
2813 mips_no_prev_insn ();
2814}
2815
2816/* Start a (possibly nested) noreorder block. */
2817
2818static void
2819start_noreorder (void)
2820{
2821 if (mips_opts.noreorder == 0)
2822 {
2823 unsigned int i;
2824 int nops;
2825
2826 /* None of the instructions before the .set noreorder can be moved. */
2827 for (i = 0; i < ARRAY_SIZE (history); i++)
2828 history[i].fixed_p = 1;
2829
2830 /* Insert any nops that might be needed between the .set noreorder
2831 block and the previous instructions. We will later remove any
2832 nops that turn out not to be needed. */
2833 nops = nops_for_insn (history, NULL);
2834 if (nops > 0)
2835 {
2836 if (mips_optimize != 0)
252b5132
RH
2837 {
2838 /* Record the frag which holds the nop instructions, so
2839 that we can remove them if we don't need them. */
2840 frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2841 prev_nop_frag = frag_now;
2842 prev_nop_frag_holds = nops;
2843 prev_nop_frag_required = 0;
2844 prev_nop_frag_since = 0;
2845 }
2846
2847 for (; nops > 0; --nops)
1e915849 2848 add_fixed_insn (NOP_INSN);
252b5132 2849
7d10b47d
RS
2850 /* Move on to a new frag, so that it is safe to simply
2851 decrease the size of prev_nop_frag. */
2852 frag_wane (frag_now);
2853 frag_new (0);
404a8071 2854 mips_move_labels ();
252b5132 2855 }
7d10b47d
RS
2856 mips16_mark_labels ();
2857 mips_clear_insn_labels ();
252b5132 2858 }
7d10b47d
RS
2859 mips_opts.noreorder++;
2860 mips_any_noreorder = 1;
2861}
252b5132 2862
7d10b47d 2863/* End a nested noreorder block. */
252b5132 2864
7d10b47d
RS
2865static void
2866end_noreorder (void)
2867{
2868 mips_opts.noreorder--;
2869 if (mips_opts.noreorder == 0 && prev_nop_frag != NULL)
2870 {
2871 /* Commit to inserting prev_nop_frag_required nops and go back to
2872 handling nop insertion the .set reorder way. */
2873 prev_nop_frag->fr_fix -= ((prev_nop_frag_holds - prev_nop_frag_required)
2874 * (mips_opts.mips16 ? 2 : 4));
2875 insert_into_history (prev_nop_frag_since,
2876 prev_nop_frag_required, NOP_INSN);
2877 prev_nop_frag = NULL;
2878 }
252b5132
RH
2879}
2880
584892a6
RS
2881/* Set up global variables for the start of a new macro. */
2882
2883static void
2884macro_start (void)
2885{
2886 memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
2887 mips_macro_warning.delay_slot_p = (mips_opts.noreorder
47e39b9d 2888 && (history[0].insn_mo->pinfo
584892a6
RS
2889 & (INSN_UNCOND_BRANCH_DELAY
2890 | INSN_COND_BRANCH_DELAY
2891 | INSN_COND_BRANCH_LIKELY)) != 0);
2892}
2893
2894/* Given that a macro is longer than 4 bytes, return the appropriate warning
2895 for it. Return null if no warning is needed. SUBTYPE is a bitmask of
2896 RELAX_DELAY_SLOT and RELAX_NOMACRO. */
2897
2898static const char *
2899macro_warning (relax_substateT subtype)
2900{
2901 if (subtype & RELAX_DELAY_SLOT)
2902 return _("Macro instruction expanded into multiple instructions"
2903 " in a branch delay slot");
2904 else if (subtype & RELAX_NOMACRO)
2905 return _("Macro instruction expanded into multiple instructions");
2906 else
2907 return 0;
2908}
2909
2910/* Finish up a macro. Emit warnings as appropriate. */
2911
2912static void
2913macro_end (void)
2914{
2915 if (mips_macro_warning.sizes[0] > 4 || mips_macro_warning.sizes[1] > 4)
2916 {
2917 relax_substateT subtype;
2918
2919 /* Set up the relaxation warning flags. */
2920 subtype = 0;
2921 if (mips_macro_warning.sizes[1] > mips_macro_warning.sizes[0])
2922 subtype |= RELAX_SECOND_LONGER;
2923 if (mips_opts.warn_about_macros)
2924 subtype |= RELAX_NOMACRO;
2925 if (mips_macro_warning.delay_slot_p)
2926 subtype |= RELAX_DELAY_SLOT;
2927
2928 if (mips_macro_warning.sizes[0] > 4 && mips_macro_warning.sizes[1] > 4)
2929 {
2930 /* Either the macro has a single implementation or both
2931 implementations are longer than 4 bytes. Emit the
2932 warning now. */
2933 const char *msg = macro_warning (subtype);
2934 if (msg != 0)
2935 as_warn (msg);
2936 }
2937 else
2938 {
2939 /* One implementation might need a warning but the other
2940 definitely doesn't. */
2941 mips_macro_warning.first_frag->fr_subtype |= subtype;
2942 }
2943 }
2944}
2945
6e1304d8
RS
2946/* Read a macro's relocation codes from *ARGS and store them in *R.
2947 The first argument in *ARGS will be either the code for a single
2948 relocation or -1 followed by the three codes that make up a
2949 composite relocation. */
2950
2951static void
2952macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
2953{
2954 int i, next;
2955
2956 next = va_arg (*args, int);
2957 if (next >= 0)
2958 r[0] = (bfd_reloc_code_real_type) next;
2959 else
2960 for (i = 0; i < 3; i++)
2961 r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
2962}
2963
252b5132
RH
2964/* Build an instruction created by a macro expansion. This is passed
2965 a pointer to the count of instructions created so far, an
2966 expression, the name of the instruction to build, an operand format
2967 string, and corresponding arguments. */
2968
252b5132 2969static void
67c0d1eb 2970macro_build (expressionS *ep, const char *name, const char *fmt, ...)
252b5132 2971{
1e915849 2972 const struct mips_opcode *mo;
252b5132 2973 struct mips_cl_insn insn;
f6688943 2974 bfd_reloc_code_real_type r[3];
252b5132 2975 va_list args;
252b5132 2976
252b5132 2977 va_start (args, fmt);
252b5132 2978
252b5132
RH
2979 if (mips_opts.mips16)
2980 {
67c0d1eb 2981 mips16_macro_build (ep, name, fmt, args);
252b5132
RH
2982 va_end (args);
2983 return;
2984 }
2985
f6688943
TS
2986 r[0] = BFD_RELOC_UNUSED;
2987 r[1] = BFD_RELOC_UNUSED;
2988 r[2] = BFD_RELOC_UNUSED;
1e915849
RS
2989 mo = (struct mips_opcode *) hash_find (op_hash, name);
2990 assert (mo);
2991 assert (strcmp (name, mo->name) == 0);
2992
2993 /* Search until we get a match for NAME. It is assumed here that
2994 macros will never generate MDMX or MIPS-3D instructions. */
2995 while (strcmp (fmt, mo->args) != 0
2996 || mo->pinfo == INSN_MACRO
2997 || !OPCODE_IS_MEMBER (mo,
2998 (mips_opts.isa
2999 | (file_ase_mips16 ? INSN_MIPS16 : 0)),
fef14a42 3000 mips_opts.arch)
1e915849
RS
3001 || (mips_opts.arch == CPU_R4650 && (mo->pinfo & FP_D) != 0))
3002 {
3003 ++mo;
3004 assert (mo->name);
3005 assert (strcmp (name, mo->name) == 0);
252b5132
RH
3006 }
3007
1e915849 3008 create_insn (&insn, mo);
252b5132
RH
3009 for (;;)
3010 {
3011 switch (*fmt++)
3012 {
3013 case '\0':
3014 break;
3015
3016 case ',':
3017 case '(':
3018 case ')':
3019 continue;
3020
5f74bc13
CD
3021 case '+':
3022 switch (*fmt++)
3023 {
3024 case 'A':
3025 case 'E':
bf12938e 3026 INSERT_OPERAND (SHAMT, insn, va_arg (args, int));
5f74bc13
CD
3027 continue;
3028
3029 case 'B':
3030 case 'F':
3031 /* Note that in the macro case, these arguments are already
3032 in MSB form. (When handling the instruction in the
3033 non-macro case, these arguments are sizes from which
3034 MSB values must be calculated.) */
bf12938e 3035 INSERT_OPERAND (INSMSB, insn, va_arg (args, int));
5f74bc13
CD
3036 continue;
3037
3038 case 'C':
3039 case 'G':
3040 case 'H':
3041 /* Note that in the macro case, these arguments are already
3042 in MSBD form. (When handling the instruction in the
3043 non-macro case, these arguments are sizes from which
3044 MSBD values must be calculated.) */
bf12938e 3045 INSERT_OPERAND (EXTMSBD, insn, va_arg (args, int));
5f74bc13
CD
3046 continue;
3047
3048 default:
3049 internalError ();
3050 }
3051 continue;
3052
252b5132
RH
3053 case 't':
3054 case 'w':
3055 case 'E':
bf12938e 3056 INSERT_OPERAND (RT, insn, va_arg (args, int));
252b5132
RH
3057 continue;
3058
3059 case 'c':
bf12938e 3060 INSERT_OPERAND (CODE, insn, va_arg (args, int));
38487616
TS
3061 continue;
3062
252b5132
RH
3063 case 'T':
3064 case 'W':
bf12938e 3065 INSERT_OPERAND (FT, insn, va_arg (args, int));
252b5132
RH
3066 continue;
3067
3068 case 'd':
3069 case 'G':
af7ee8bf 3070 case 'K':
bf12938e 3071 INSERT_OPERAND (RD, insn, va_arg (args, int));
252b5132
RH
3072 continue;
3073
4372b673
NC
3074 case 'U':
3075 {
3076 int tmp = va_arg (args, int);
3077
bf12938e
RS
3078 INSERT_OPERAND (RT, insn, tmp);
3079 INSERT_OPERAND (RD, insn, tmp);
beae10d5 3080 continue;
4372b673
NC
3081 }
3082
252b5132
RH
3083 case 'V':
3084 case 'S':
bf12938e 3085 INSERT_OPERAND (FS, insn, va_arg (args, int));
252b5132
RH
3086 continue;
3087
3088 case 'z':
3089 continue;
3090
3091 case '<':
bf12938e 3092 INSERT_OPERAND (SHAMT, insn, va_arg (args, int));
252b5132
RH
3093 continue;
3094
3095 case 'D':
bf12938e 3096 INSERT_OPERAND (FD, insn, va_arg (args, int));
252b5132
RH
3097 continue;
3098
3099 case 'B':
bf12938e 3100 INSERT_OPERAND (CODE20, insn, va_arg (args, int));
252b5132
RH
3101 continue;
3102
4372b673 3103 case 'J':
bf12938e 3104 INSERT_OPERAND (CODE19, insn, va_arg (args, int));
4372b673
NC
3105 continue;
3106
252b5132 3107 case 'q':
bf12938e 3108 INSERT_OPERAND (CODE2, insn, va_arg (args, int));
252b5132
RH
3109 continue;
3110
3111 case 'b':
3112 case 's':
3113 case 'r':
3114 case 'v':
bf12938e 3115 INSERT_OPERAND (RS, insn, va_arg (args, int));
252b5132
RH
3116 continue;
3117
3118 case 'i':
3119 case 'j':
3120 case 'o':
6e1304d8 3121 macro_read_relocs (&args, r);
cdf6fd85 3122 assert (*r == BFD_RELOC_GPREL16
f6688943
TS
3123 || *r == BFD_RELOC_MIPS_LITERAL
3124 || *r == BFD_RELOC_MIPS_HIGHER
3125 || *r == BFD_RELOC_HI16_S
3126 || *r == BFD_RELOC_LO16
3127 || *r == BFD_RELOC_MIPS_GOT16
3128 || *r == BFD_RELOC_MIPS_CALL16
438c16b8
TS
3129 || *r == BFD_RELOC_MIPS_GOT_DISP
3130 || *r == BFD_RELOC_MIPS_GOT_PAGE
3131 || *r == BFD_RELOC_MIPS_GOT_OFST
f6688943 3132 || *r == BFD_RELOC_MIPS_GOT_LO16
3e722fb5 3133 || *r == BFD_RELOC_MIPS_CALL_LO16);
252b5132
RH
3134 continue;
3135
3136 case 'u':
6e1304d8 3137 macro_read_relocs (&args, r);
252b5132
RH
3138 assert (ep != NULL
3139 && (ep->X_op == O_constant
3140 || (ep->X_op == O_symbol
f6688943
TS
3141 && (*r == BFD_RELOC_MIPS_HIGHEST
3142 || *r == BFD_RELOC_HI16_S
3143 || *r == BFD_RELOC_HI16
3144 || *r == BFD_RELOC_GPREL16
3145 || *r == BFD_RELOC_MIPS_GOT_HI16
3e722fb5 3146 || *r == BFD_RELOC_MIPS_CALL_HI16))));
252b5132
RH
3147 continue;
3148
3149 case 'p':
3150 assert (ep != NULL);
bad36eac 3151
252b5132
RH
3152 /*
3153 * This allows macro() to pass an immediate expression for
3154 * creating short branches without creating a symbol.
bad36eac
DJ
3155 *
3156 * We don't allow branch relaxation for these branches, as
3157 * they should only appear in ".set nomacro" anyway.
252b5132
RH
3158 */
3159 if (ep->X_op == O_constant)
3160 {
bad36eac
DJ
3161 if ((ep->X_add_number & 3) != 0)
3162 as_bad (_("branch to misaligned address (0x%lx)"),
3163 (unsigned long) ep->X_add_number);
3164 if ((ep->X_add_number + 0x20000) & ~0x3ffff)
3165 as_bad (_("branch address range overflow (0x%lx)"),
3166 (unsigned long) ep->X_add_number);
252b5132
RH
3167 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
3168 ep = NULL;
3169 }
3170 else
0b25d3e6 3171 *r = BFD_RELOC_16_PCREL_S2;
252b5132
RH
3172 continue;
3173
3174 case 'a':
3175 assert (ep != NULL);
f6688943 3176 *r = BFD_RELOC_MIPS_JMP;
252b5132
RH
3177 continue;
3178
3179 case 'C':
3180 insn.insn_opcode |= va_arg (args, unsigned long);
3181 continue;
3182
3183 default:
3184 internalError ();
3185 }
3186 break;
3187 }
3188 va_end (args);
f6688943 3189 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 3190
4d7206a2 3191 append_insn (&insn, ep, r);
252b5132
RH
3192}
3193
3194static void
67c0d1eb 3195mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
17a2f251 3196 va_list args)
252b5132 3197{
1e915849 3198 struct mips_opcode *mo;
252b5132 3199 struct mips_cl_insn insn;
f6688943
TS
3200 bfd_reloc_code_real_type r[3]
3201 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 3202
1e915849
RS
3203 mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
3204 assert (mo);
3205 assert (strcmp (name, mo->name) == 0);
252b5132 3206
1e915849 3207 while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO)
252b5132 3208 {
1e915849
RS
3209 ++mo;
3210 assert (mo->name);
3211 assert (strcmp (name, mo->name) == 0);
252b5132
RH
3212 }
3213
1e915849 3214 create_insn (&insn, mo);
252b5132
RH
3215 for (;;)
3216 {
3217 int c;
3218
3219 c = *fmt++;
3220 switch (c)
3221 {
3222 case '\0':
3223 break;
3224
3225 case ',':
3226 case '(':
3227 case ')':
3228 continue;
3229
3230 case 'y':
3231 case 'w':
bf12938e 3232 MIPS16_INSERT_OPERAND (RY, insn, va_arg (args, int));
252b5132
RH
3233 continue;
3234
3235 case 'x':
3236 case 'v':
bf12938e 3237 MIPS16_INSERT_OPERAND (RX, insn, va_arg (args, int));
252b5132
RH
3238 continue;
3239
3240 case 'z':
bf12938e 3241 MIPS16_INSERT_OPERAND (RZ, insn, va_arg (args, int));
252b5132
RH
3242 continue;
3243
3244 case 'Z':
bf12938e 3245 MIPS16_INSERT_OPERAND (MOVE32Z, insn, va_arg (args, int));
252b5132
RH
3246 continue;
3247
3248 case '0':
3249 case 'S':
3250 case 'P':
3251 case 'R':
3252 continue;
3253
3254 case 'X':
bf12938e 3255 MIPS16_INSERT_OPERAND (REGR32, insn, va_arg (args, int));
252b5132
RH
3256 continue;
3257
3258 case 'Y':
3259 {
3260 int regno;
3261
3262 regno = va_arg (args, int);
3263 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
3264 insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
3265 }
3266 continue;
3267
3268 case '<':
3269 case '>':
3270 case '4':
3271 case '5':
3272 case 'H':
3273 case 'W':
3274 case 'D':
3275 case 'j':
3276 case '8':
3277 case 'V':
3278 case 'C':
3279 case 'U':
3280 case 'k':
3281 case 'K':
3282 case 'p':
3283 case 'q':
3284 {
3285 assert (ep != NULL);
3286
3287 if (ep->X_op != O_constant)
874e8986 3288 *r = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
3289 else
3290 {
b34976b6
AM
3291 mips16_immed (NULL, 0, c, ep->X_add_number, FALSE, FALSE,
3292 FALSE, &insn.insn_opcode, &insn.use_extend,
c4e7957c 3293 &insn.extend);
252b5132 3294 ep = NULL;
f6688943 3295 *r = BFD_RELOC_UNUSED;
252b5132
RH
3296 }
3297 }
3298 continue;
3299
3300 case '6':
bf12938e 3301 MIPS16_INSERT_OPERAND (IMM6, insn, va_arg (args, int));
252b5132
RH
3302 continue;
3303 }
3304
3305 break;
3306 }
3307
f6688943 3308 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 3309
4d7206a2 3310 append_insn (&insn, ep, r);
252b5132
RH
3311}
3312
2051e8c4
MR
3313/*
3314 * Sign-extend 32-bit mode constants that have bit 31 set and all
3315 * higher bits unset.
3316 */
9f872bbe 3317static void
2051e8c4
MR
3318normalize_constant_expr (expressionS *ex)
3319{
9ee2a2d4 3320 if (ex->X_op == O_constant
2051e8c4
MR
3321 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
3322 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
3323 - 0x80000000);
3324}
3325
3326/*
3327 * Sign-extend 32-bit mode address offsets that have bit 31 set and
3328 * all higher bits unset.
3329 */
3330static void
3331normalize_address_expr (expressionS *ex)
3332{
3333 if (((ex->X_op == O_constant && HAVE_32BIT_ADDRESSES)
3334 || (ex->X_op == O_symbol && HAVE_32BIT_SYMBOLS))
3335 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
3336 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
3337 - 0x80000000);
3338}
3339
438c16b8
TS
3340/*
3341 * Generate a "jalr" instruction with a relocation hint to the called
3342 * function. This occurs in NewABI PIC code.
3343 */
3344static void
67c0d1eb 3345macro_build_jalr (expressionS *ep)
438c16b8 3346{
685736be 3347 char *f = NULL;
b34976b6 3348
438c16b8 3349 if (HAVE_NEWABI)
f21f8242 3350 {
cc3d92a5 3351 frag_grow (8);
f21f8242
AO
3352 f = frag_more (0);
3353 }
67c0d1eb 3354 macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
438c16b8 3355 if (HAVE_NEWABI)
f21f8242 3356 fix_new_exp (frag_now, f - frag_now->fr_literal,
a105a300 3357 4, ep, FALSE, BFD_RELOC_MIPS_JALR);
438c16b8
TS
3358}
3359
252b5132
RH
3360/*
3361 * Generate a "lui" instruction.
3362 */
3363static void
67c0d1eb 3364macro_build_lui (expressionS *ep, int regnum)
252b5132
RH
3365{
3366 expressionS high_expr;
1e915849 3367 const struct mips_opcode *mo;
252b5132 3368 struct mips_cl_insn insn;
f6688943
TS
3369 bfd_reloc_code_real_type r[3]
3370 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
5a38dc70
AM
3371 const char *name = "lui";
3372 const char *fmt = "t,u";
252b5132
RH
3373
3374 assert (! mips_opts.mips16);
3375
4d7206a2 3376 high_expr = *ep;
252b5132
RH
3377
3378 if (high_expr.X_op == O_constant)
3379 {
3380 /* we can compute the instruction now without a relocation entry */
e7d556df
TS
3381 high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3382 >> 16) & 0xffff;
f6688943 3383 *r = BFD_RELOC_UNUSED;
252b5132 3384 }
78e1bb40 3385 else
252b5132
RH
3386 {
3387 assert (ep->X_op == O_symbol);
bbe506e8
TS
3388 /* _gp_disp is a special case, used from s_cpload.
3389 __gnu_local_gp is used if mips_no_shared. */
252b5132 3390 assert (mips_pic == NO_PIC
78e1bb40 3391 || (! HAVE_NEWABI
aa6975fb
ILT
3392 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
3393 || (! mips_in_shared
bbe506e8
TS
3394 && strcmp (S_GET_NAME (ep->X_add_symbol),
3395 "__gnu_local_gp") == 0));
f6688943 3396 *r = BFD_RELOC_HI16_S;
252b5132
RH
3397 }
3398
1e915849
RS
3399 mo = hash_find (op_hash, name);
3400 assert (strcmp (name, mo->name) == 0);
3401 assert (strcmp (fmt, mo->args) == 0);
3402 create_insn (&insn, mo);
252b5132 3403
bf12938e
RS
3404 insn.insn_opcode = insn.insn_mo->match;
3405 INSERT_OPERAND (RT, insn, regnum);
f6688943 3406 if (*r == BFD_RELOC_UNUSED)
252b5132
RH
3407 {
3408 insn.insn_opcode |= high_expr.X_add_number;
4d7206a2 3409 append_insn (&insn, NULL, r);
252b5132
RH
3410 }
3411 else
4d7206a2 3412 append_insn (&insn, &high_expr, r);
252b5132
RH
3413}
3414
885add95
CD
3415/* Generate a sequence of instructions to do a load or store from a constant
3416 offset off of a base register (breg) into/from a target register (treg),
3417 using AT if necessary. */
3418static void
67c0d1eb
RS
3419macro_build_ldst_constoffset (expressionS *ep, const char *op,
3420 int treg, int breg, int dbl)
885add95
CD
3421{
3422 assert (ep->X_op == O_constant);
3423
256ab948 3424 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
3425 if (!dbl)
3426 normalize_constant_expr (ep);
256ab948 3427
67c1ffbe 3428 /* Right now, this routine can only handle signed 32-bit constants. */
ecd13cd3 3429 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
885add95
CD
3430 as_warn (_("operand overflow"));
3431
3432 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
3433 {
3434 /* Signed 16-bit offset will fit in the op. Easy! */
67c0d1eb 3435 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
885add95
CD
3436 }
3437 else
3438 {
3439 /* 32-bit offset, need multiple instructions and AT, like:
3440 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
3441 addu $tempreg,$tempreg,$breg
3442 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
3443 to handle the complete offset. */
67c0d1eb
RS
3444 macro_build_lui (ep, AT);
3445 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
3446 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
885add95
CD
3447
3448 if (mips_opts.noat)
8fc2e39e 3449 as_bad (_("Macro used $at after \".set noat\""));
885add95
CD
3450 }
3451}
3452
252b5132
RH
3453/* set_at()
3454 * Generates code to set the $at register to true (one)
3455 * if reg is less than the immediate expression.
3456 */
3457static void
67c0d1eb 3458set_at (int reg, int unsignedp)
252b5132
RH
3459{
3460 if (imm_expr.X_op == O_constant
3461 && imm_expr.X_add_number >= -0x8000
3462 && imm_expr.X_add_number < 0x8000)
67c0d1eb
RS
3463 macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
3464 AT, reg, BFD_RELOC_LO16);
252b5132
RH
3465 else
3466 {
67c0d1eb
RS
3467 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
3468 macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
252b5132
RH
3469 }
3470}
3471
3472/* Warn if an expression is not a constant. */
3473
3474static void
17a2f251 3475check_absolute_expr (struct mips_cl_insn *ip, expressionS *ex)
252b5132
RH
3476{
3477 if (ex->X_op == O_big)
3478 as_bad (_("unsupported large constant"));
3479 else if (ex->X_op != O_constant)
9ee2a2d4
MR
3480 as_bad (_("Instruction %s requires absolute expression"),
3481 ip->insn_mo->name);
13757d0c 3482
9ee2a2d4
MR
3483 if (HAVE_32BIT_GPRS)
3484 normalize_constant_expr (ex);
252b5132
RH
3485}
3486
3487/* Count the leading zeroes by performing a binary chop. This is a
3488 bulky bit of source, but performance is a LOT better for the
3489 majority of values than a simple loop to count the bits:
3490 for (lcnt = 0; (lcnt < 32); lcnt++)
3491 if ((v) & (1 << (31 - lcnt)))
3492 break;
3493 However it is not code size friendly, and the gain will drop a bit
3494 on certain cached systems.
3495*/
3496#define COUNT_TOP_ZEROES(v) \
3497 (((v) & ~0xffff) == 0 \
3498 ? ((v) & ~0xff) == 0 \
3499 ? ((v) & ~0xf) == 0 \
3500 ? ((v) & ~0x3) == 0 \
3501 ? ((v) & ~0x1) == 0 \
3502 ? !(v) \
3503 ? 32 \
3504 : 31 \
3505 : 30 \
3506 : ((v) & ~0x7) == 0 \
3507 ? 29 \
3508 : 28 \
3509 : ((v) & ~0x3f) == 0 \
3510 ? ((v) & ~0x1f) == 0 \
3511 ? 27 \
3512 : 26 \
3513 : ((v) & ~0x7f) == 0 \
3514 ? 25 \
3515 : 24 \
3516 : ((v) & ~0xfff) == 0 \
3517 ? ((v) & ~0x3ff) == 0 \
3518 ? ((v) & ~0x1ff) == 0 \
3519 ? 23 \
3520 : 22 \
3521 : ((v) & ~0x7ff) == 0 \
3522 ? 21 \
3523 : 20 \
3524 : ((v) & ~0x3fff) == 0 \
3525 ? ((v) & ~0x1fff) == 0 \
3526 ? 19 \
3527 : 18 \
3528 : ((v) & ~0x7fff) == 0 \
3529 ? 17 \
3530 : 16 \
3531 : ((v) & ~0xffffff) == 0 \
3532 ? ((v) & ~0xfffff) == 0 \
3533 ? ((v) & ~0x3ffff) == 0 \
3534 ? ((v) & ~0x1ffff) == 0 \
3535 ? 15 \
3536 : 14 \
3537 : ((v) & ~0x7ffff) == 0 \
3538 ? 13 \
3539 : 12 \
3540 : ((v) & ~0x3fffff) == 0 \
3541 ? ((v) & ~0x1fffff) == 0 \
3542 ? 11 \
3543 : 10 \
3544 : ((v) & ~0x7fffff) == 0 \
3545 ? 9 \
3546 : 8 \
3547 : ((v) & ~0xfffffff) == 0 \
3548 ? ((v) & ~0x3ffffff) == 0 \
3549 ? ((v) & ~0x1ffffff) == 0 \
3550 ? 7 \
3551 : 6 \
3552 : ((v) & ~0x7ffffff) == 0 \
3553 ? 5 \
3554 : 4 \
3555 : ((v) & ~0x3fffffff) == 0 \
3556 ? ((v) & ~0x1fffffff) == 0 \
3557 ? 3 \
3558 : 2 \
3559 : ((v) & ~0x7fffffff) == 0 \
3560 ? 1 \
3561 : 0)
3562
3563/* load_register()
67c1ffbe 3564 * This routine generates the least number of instructions necessary to load
252b5132
RH
3565 * an absolute expression value into a register.
3566 */
3567static void
67c0d1eb 3568load_register (int reg, expressionS *ep, int dbl)
252b5132
RH
3569{
3570 int freg;
3571 expressionS hi32, lo32;
3572
3573 if (ep->X_op != O_big)
3574 {
3575 assert (ep->X_op == O_constant);
256ab948
TS
3576
3577 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
3578 if (!dbl)
3579 normalize_constant_expr (ep);
256ab948
TS
3580
3581 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
252b5132
RH
3582 {
3583 /* We can handle 16 bit signed values with an addiu to
3584 $zero. No need to ever use daddiu here, since $zero and
3585 the result are always correct in 32 bit mode. */
67c0d1eb 3586 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
3587 return;
3588 }
3589 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3590 {
3591 /* We can handle 16 bit unsigned values with an ori to
3592 $zero. */
67c0d1eb 3593 macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
252b5132
RH
3594 return;
3595 }
256ab948 3596 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
252b5132
RH
3597 {
3598 /* 32 bit values require an lui. */
67c0d1eb 3599 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_HI16);
252b5132 3600 if ((ep->X_add_number & 0xffff) != 0)
67c0d1eb 3601 macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
252b5132
RH
3602 return;
3603 }
3604 }
3605
3606 /* The value is larger than 32 bits. */
3607
2051e8c4 3608 if (!dbl || HAVE_32BIT_GPRS)
252b5132 3609 {
55e08f71
NC
3610 char value[32];
3611
3612 sprintf_vma (value, ep->X_add_number);
20e1fcfd 3613 as_bad (_("Number (0x%s) larger than 32 bits"), value);
67c0d1eb 3614 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
3615 return;
3616 }
3617
3618 if (ep->X_op != O_big)
3619 {
3620 hi32 = *ep;
3621 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3622 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3623 hi32.X_add_number &= 0xffffffff;
3624 lo32 = *ep;
3625 lo32.X_add_number &= 0xffffffff;
3626 }
3627 else
3628 {
3629 assert (ep->X_add_number > 2);
3630 if (ep->X_add_number == 3)
3631 generic_bignum[3] = 0;
3632 else if (ep->X_add_number > 4)
3633 as_bad (_("Number larger than 64 bits"));
3634 lo32.X_op = O_constant;
3635 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3636 hi32.X_op = O_constant;
3637 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3638 }
3639
3640 if (hi32.X_add_number == 0)
3641 freg = 0;
3642 else
3643 {
3644 int shift, bit;
3645 unsigned long hi, lo;
3646
956cd1d6 3647 if (hi32.X_add_number == (offsetT) 0xffffffff)
beae10d5
KH
3648 {
3649 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3650 {
67c0d1eb 3651 macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
3652 return;
3653 }
3654 if (lo32.X_add_number & 0x80000000)
3655 {
67c0d1eb 3656 macro_build (&lo32, "lui", "t,u", reg, BFD_RELOC_HI16);
252b5132 3657 if (lo32.X_add_number & 0xffff)
67c0d1eb 3658 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
beae10d5
KH
3659 return;
3660 }
3661 }
252b5132
RH
3662
3663 /* Check for 16bit shifted constant. We know that hi32 is
3664 non-zero, so start the mask on the first bit of the hi32
3665 value. */
3666 shift = 17;
3667 do
beae10d5
KH
3668 {
3669 unsigned long himask, lomask;
3670
3671 if (shift < 32)
3672 {
3673 himask = 0xffff >> (32 - shift);
3674 lomask = (0xffff << shift) & 0xffffffff;
3675 }
3676 else
3677 {
3678 himask = 0xffff << (shift - 32);
3679 lomask = 0;
3680 }
3681 if ((hi32.X_add_number & ~(offsetT) himask) == 0
3682 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3683 {
3684 expressionS tmp;
3685
3686 tmp.X_op = O_constant;
3687 if (shift < 32)
3688 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3689 | (lo32.X_add_number >> shift));
3690 else
3691 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
67c0d1eb
RS
3692 macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
3693 macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", "d,w,<",
3694 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
3695 return;
3696 }
f9419b05 3697 ++shift;
beae10d5
KH
3698 }
3699 while (shift <= (64 - 16));
252b5132
RH
3700
3701 /* Find the bit number of the lowest one bit, and store the
3702 shifted value in hi/lo. */
3703 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3704 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3705 if (lo != 0)
3706 {
3707 bit = 0;
3708 while ((lo & 1) == 0)
3709 {
3710 lo >>= 1;
3711 ++bit;
3712 }
3713 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3714 hi >>= bit;
3715 }
3716 else
3717 {
3718 bit = 32;
3719 while ((hi & 1) == 0)
3720 {
3721 hi >>= 1;
3722 ++bit;
3723 }
3724 lo = hi;
3725 hi = 0;
3726 }
3727
3728 /* Optimize if the shifted value is a (power of 2) - 1. */
3729 if ((hi == 0 && ((lo + 1) & lo) == 0)
3730 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
beae10d5
KH
3731 {
3732 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
252b5132 3733 if (shift != 0)
beae10d5 3734 {
252b5132
RH
3735 expressionS tmp;
3736
3737 /* This instruction will set the register to be all
3738 ones. */
beae10d5
KH
3739 tmp.X_op = O_constant;
3740 tmp.X_add_number = (offsetT) -1;
67c0d1eb 3741 macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
3742 if (bit != 0)
3743 {
3744 bit += shift;
67c0d1eb
RS
3745 macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", "d,w,<",
3746 reg, reg, (bit >= 32) ? bit - 32 : bit);
beae10d5 3747 }
67c0d1eb
RS
3748 macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", "d,w,<",
3749 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
3750 return;
3751 }
3752 }
252b5132
RH
3753
3754 /* Sign extend hi32 before calling load_register, because we can
3755 generally get better code when we load a sign extended value. */
3756 if ((hi32.X_add_number & 0x80000000) != 0)
beae10d5 3757 hi32.X_add_number |= ~(offsetT) 0xffffffff;
67c0d1eb 3758 load_register (reg, &hi32, 0);
252b5132
RH
3759 freg = reg;
3760 }
3761 if ((lo32.X_add_number & 0xffff0000) == 0)
3762 {
3763 if (freg != 0)
3764 {
67c0d1eb 3765 macro_build (NULL, "dsll32", "d,w,<", reg, freg, 0);
252b5132
RH
3766 freg = reg;
3767 }
3768 }
3769 else
3770 {
3771 expressionS mid16;
3772
956cd1d6 3773 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
beae10d5 3774 {
67c0d1eb
RS
3775 macro_build (&lo32, "lui", "t,u", reg, BFD_RELOC_HI16);
3776 macro_build (NULL, "dsrl32", "d,w,<", reg, reg, 0);
beae10d5
KH
3777 return;
3778 }
252b5132
RH
3779
3780 if (freg != 0)
3781 {
67c0d1eb 3782 macro_build (NULL, "dsll", "d,w,<", reg, freg, 16);
252b5132
RH
3783 freg = reg;
3784 }
3785 mid16 = lo32;
3786 mid16.X_add_number >>= 16;
67c0d1eb
RS
3787 macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
3788 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
252b5132
RH
3789 freg = reg;
3790 }
3791 if ((lo32.X_add_number & 0xffff) != 0)
67c0d1eb 3792 macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
252b5132
RH
3793}
3794
269137b2
TS
3795static inline void
3796load_delay_nop (void)
3797{
3798 if (!gpr_interlocks)
3799 macro_build (NULL, "nop", "");
3800}
3801
252b5132
RH
3802/* Load an address into a register. */
3803
3804static void
67c0d1eb 3805load_address (int reg, expressionS *ep, int *used_at)
252b5132 3806{
252b5132
RH
3807 if (ep->X_op != O_constant
3808 && ep->X_op != O_symbol)
3809 {
3810 as_bad (_("expression too complex"));
3811 ep->X_op = O_constant;
3812 }
3813
3814 if (ep->X_op == O_constant)
3815 {
67c0d1eb 3816 load_register (reg, ep, HAVE_64BIT_ADDRESSES);
252b5132
RH
3817 return;
3818 }
3819
3820 if (mips_pic == NO_PIC)
3821 {
3822 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 3823 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
3824 Otherwise we want
3825 lui $reg,<sym> (BFD_RELOC_HI16_S)
3826 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
d6bc6245 3827 If we have an addend, we always use the latter form.
76b3015f 3828
d6bc6245
TS
3829 With 64bit address space and a usable $at we want
3830 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3831 lui $at,<sym> (BFD_RELOC_HI16_S)
3832 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3833 daddiu $at,<sym> (BFD_RELOC_LO16)
3834 dsll32 $reg,0
3a482fd5 3835 daddu $reg,$reg,$at
76b3015f 3836
c03099e6 3837 If $at is already in use, we use a path which is suboptimal
d6bc6245
TS
3838 on superscalar processors.
3839 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3840 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3841 dsll $reg,16
3842 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
3843 dsll $reg,16
3844 daddiu $reg,<sym> (BFD_RELOC_LO16)
6caf9ef4
TS
3845
3846 For GP relative symbols in 64bit address space we can use
3847 the same sequence as in 32bit address space. */
aed1a261 3848 if (HAVE_64BIT_SYMBOLS)
d6bc6245 3849 {
6caf9ef4
TS
3850 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3851 && !nopic_need_relax (ep->X_add_symbol, 1))
3852 {
3853 relax_start (ep->X_add_symbol);
3854 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
3855 mips_gp_register, BFD_RELOC_GPREL16);
3856 relax_switch ();
3857 }
d6bc6245 3858
b8285c27 3859 if (*used_at == 0 && !mips_opts.noat)
d6bc6245 3860 {
67c0d1eb
RS
3861 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_HIGHEST);
3862 macro_build (ep, "lui", "t,u", AT, BFD_RELOC_HI16_S);
3863 macro_build (ep, "daddiu", "t,r,j", reg, reg,
3864 BFD_RELOC_MIPS_HIGHER);
3865 macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
3866 macro_build (NULL, "dsll32", "d,w,<", reg, reg, 0);
3867 macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
d6bc6245
TS
3868 *used_at = 1;
3869 }
3870 else
3871 {
67c0d1eb
RS
3872 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_HIGHEST);
3873 macro_build (ep, "daddiu", "t,r,j", reg, reg,
3874 BFD_RELOC_MIPS_HIGHER);
3875 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
3876 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
3877 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
3878 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
d6bc6245 3879 }
6caf9ef4
TS
3880
3881 if (mips_relax.sequence)
3882 relax_end ();
d6bc6245 3883 }
252b5132
RH
3884 else
3885 {
d6bc6245 3886 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 3887 && !nopic_need_relax (ep->X_add_symbol, 1))
d6bc6245 3888 {
4d7206a2 3889 relax_start (ep->X_add_symbol);
67c0d1eb 3890 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
17a2f251 3891 mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 3892 relax_switch ();
d6bc6245 3893 }
67c0d1eb
RS
3894 macro_build_lui (ep, reg);
3895 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
3896 reg, reg, BFD_RELOC_LO16);
4d7206a2
RS
3897 if (mips_relax.sequence)
3898 relax_end ();
d6bc6245 3899 }
252b5132
RH
3900 }
3901 else if (mips_pic == SVR4_PIC && ! mips_big_got)
3902 {
3903 expressionS ex;
3904
3905 /* If this is a reference to an external symbol, we want
3906 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3907 Otherwise we want
3908 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3909 nop
3910 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
f5040a92
AO
3911 If there is a constant, it must be added in after.
3912
ed6fb7bd 3913 If we have NewABI, we want
f5040a92
AO
3914 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
3915 unless we're referencing a global symbol with a non-zero
3916 offset, in which case cst must be added separately. */
ed6fb7bd
SC
3917 if (HAVE_NEWABI)
3918 {
f5040a92
AO
3919 if (ep->X_add_number)
3920 {
4d7206a2 3921 ex.X_add_number = ep->X_add_number;
f5040a92 3922 ep->X_add_number = 0;
4d7206a2 3923 relax_start (ep->X_add_symbol);
67c0d1eb
RS
3924 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3925 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
3926 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3927 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3928 ex.X_op = O_constant;
67c0d1eb 3929 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 3930 reg, reg, BFD_RELOC_LO16);
f5040a92 3931 ep->X_add_number = ex.X_add_number;
4d7206a2 3932 relax_switch ();
f5040a92 3933 }
67c0d1eb 3934 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 3935 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2
RS
3936 if (mips_relax.sequence)
3937 relax_end ();
ed6fb7bd
SC
3938 }
3939 else
3940 {
f5040a92
AO
3941 ex.X_add_number = ep->X_add_number;
3942 ep->X_add_number = 0;
67c0d1eb
RS
3943 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3944 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 3945 load_delay_nop ();
4d7206a2
RS
3946 relax_start (ep->X_add_symbol);
3947 relax_switch ();
67c0d1eb 3948 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 3949 BFD_RELOC_LO16);
4d7206a2 3950 relax_end ();
ed6fb7bd 3951
f5040a92
AO
3952 if (ex.X_add_number != 0)
3953 {
3954 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3955 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3956 ex.X_op = O_constant;
67c0d1eb 3957 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 3958 reg, reg, BFD_RELOC_LO16);
f5040a92 3959 }
252b5132
RH
3960 }
3961 }
3962 else if (mips_pic == SVR4_PIC)
3963 {
3964 expressionS ex;
252b5132
RH
3965
3966 /* This is the large GOT case. If this is a reference to an
3967 external symbol, we want
3968 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
3969 addu $reg,$reg,$gp
3970 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
f5040a92
AO
3971
3972 Otherwise, for a reference to a local symbol in old ABI, we want
252b5132
RH
3973 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3974 nop
3975 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
684022ea 3976 If there is a constant, it must be added in after.
f5040a92
AO
3977
3978 In the NewABI, for local symbols, with or without offsets, we want:
438c16b8
TS
3979 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
3980 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
f5040a92 3981 */
438c16b8
TS
3982 if (HAVE_NEWABI)
3983 {
4d7206a2 3984 ex.X_add_number = ep->X_add_number;
f5040a92 3985 ep->X_add_number = 0;
4d7206a2 3986 relax_start (ep->X_add_symbol);
67c0d1eb
RS
3987 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_GOT_HI16);
3988 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
3989 reg, reg, mips_gp_register);
3990 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
3991 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
f5040a92
AO
3992 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3993 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3994 else if (ex.X_add_number)
3995 {
3996 ex.X_op = O_constant;
67c0d1eb
RS
3997 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
3998 BFD_RELOC_LO16);
f5040a92
AO
3999 }
4000
4001 ep->X_add_number = ex.X_add_number;
4d7206a2 4002 relax_switch ();
67c0d1eb 4003 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 4004 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
67c0d1eb
RS
4005 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
4006 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 4007 relax_end ();
438c16b8 4008 }
252b5132 4009 else
438c16b8 4010 {
f5040a92
AO
4011 ex.X_add_number = ep->X_add_number;
4012 ep->X_add_number = 0;
4d7206a2 4013 relax_start (ep->X_add_symbol);
67c0d1eb
RS
4014 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_GOT_HI16);
4015 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
4016 reg, reg, mips_gp_register);
4017 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
4018 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
4d7206a2
RS
4019 relax_switch ();
4020 if (reg_needs_delay (mips_gp_register))
438c16b8
TS
4021 {
4022 /* We need a nop before loading from $gp. This special
4023 check is required because the lui which starts the main
4024 instruction stream does not refer to $gp, and so will not
4025 insert the nop which may be required. */
67c0d1eb 4026 macro_build (NULL, "nop", "");
438c16b8 4027 }
67c0d1eb 4028 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 4029 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 4030 load_delay_nop ();
67c0d1eb 4031 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 4032 BFD_RELOC_LO16);
4d7206a2 4033 relax_end ();
438c16b8 4034
f5040a92
AO
4035 if (ex.X_add_number != 0)
4036 {
4037 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4038 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4039 ex.X_op = O_constant;
67c0d1eb
RS
4040 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
4041 BFD_RELOC_LO16);
f5040a92 4042 }
252b5132
RH
4043 }
4044 }
252b5132
RH
4045 else
4046 abort ();
8fc2e39e
TS
4047
4048 if (mips_opts.noat && *used_at == 1)
4049 as_bad (_("Macro used $at after \".set noat\""));
252b5132
RH
4050}
4051
ea1fb5dc
RS
4052/* Move the contents of register SOURCE into register DEST. */
4053
4054static void
67c0d1eb 4055move_register (int dest, int source)
ea1fb5dc 4056{
67c0d1eb
RS
4057 macro_build (NULL, HAVE_32BIT_GPRS ? "addu" : "daddu", "d,v,t",
4058 dest, source, 0);
ea1fb5dc
RS
4059}
4060
4d7206a2 4061/* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
f6a22291
MR
4062 LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
4063 The two alternatives are:
4d7206a2
RS
4064
4065 Global symbol Local sybmol
4066 ------------- ------------
4067 lw DEST,%got(SYMBOL) lw DEST,%got(SYMBOL + OFFSET)
4068 ... ...
4069 addiu DEST,DEST,OFFSET addiu DEST,DEST,%lo(SYMBOL + OFFSET)
4070
4071 load_got_offset emits the first instruction and add_got_offset
f6a22291
MR
4072 emits the second for a 16-bit offset or add_got_offset_hilo emits
4073 a sequence to add a 32-bit offset using a scratch register. */
4d7206a2
RS
4074
4075static void
67c0d1eb 4076load_got_offset (int dest, expressionS *local)
4d7206a2
RS
4077{
4078 expressionS global;
4079
4080 global = *local;
4081 global.X_add_number = 0;
4082
4083 relax_start (local->X_add_symbol);
67c0d1eb
RS
4084 macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
4085 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2 4086 relax_switch ();
67c0d1eb
RS
4087 macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
4088 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2
RS
4089 relax_end ();
4090}
4091
4092static void
67c0d1eb 4093add_got_offset (int dest, expressionS *local)
4d7206a2
RS
4094{
4095 expressionS global;
4096
4097 global.X_op = O_constant;
4098 global.X_op_symbol = NULL;
4099 global.X_add_symbol = NULL;
4100 global.X_add_number = local->X_add_number;
4101
4102 relax_start (local->X_add_symbol);
67c0d1eb 4103 macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
4d7206a2
RS
4104 dest, dest, BFD_RELOC_LO16);
4105 relax_switch ();
67c0d1eb 4106 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
4d7206a2
RS
4107 relax_end ();
4108}
4109
f6a22291
MR
4110static void
4111add_got_offset_hilo (int dest, expressionS *local, int tmp)
4112{
4113 expressionS global;
4114 int hold_mips_optimize;
4115
4116 global.X_op = O_constant;
4117 global.X_op_symbol = NULL;
4118 global.X_add_symbol = NULL;
4119 global.X_add_number = local->X_add_number;
4120
4121 relax_start (local->X_add_symbol);
4122 load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
4123 relax_switch ();
4124 /* Set mips_optimize around the lui instruction to avoid
4125 inserting an unnecessary nop after the lw. */
4126 hold_mips_optimize = mips_optimize;
4127 mips_optimize = 2;
4128 macro_build_lui (&global, tmp);
4129 mips_optimize = hold_mips_optimize;
4130 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
4131 relax_end ();
4132
4133 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
4134}
4135
252b5132
RH
4136/*
4137 * Build macros
4138 * This routine implements the seemingly endless macro or synthesized
4139 * instructions and addressing modes in the mips assembly language. Many
4140 * of these macros are simple and are similar to each other. These could
67c1ffbe 4141 * probably be handled by some kind of table or grammar approach instead of
252b5132
RH
4142 * this verbose method. Others are not simple macros but are more like
4143 * optimizing code generation.
4144 * One interesting optimization is when several store macros appear
67c1ffbe 4145 * consecutively that would load AT with the upper half of the same address.
252b5132
RH
4146 * The ensuing load upper instructions are ommited. This implies some kind
4147 * of global optimization. We currently only optimize within a single macro.
4148 * For many of the load and store macros if the address is specified as a
4149 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
4150 * first load register 'at' with zero and use it as the base register. The
4151 * mips assembler simply uses register $zero. Just one tiny optimization
4152 * we're missing.
4153 */
4154static void
17a2f251 4155macro (struct mips_cl_insn *ip)
252b5132
RH
4156{
4157 register int treg, sreg, dreg, breg;
4158 int tempreg;
4159 int mask;
43841e91 4160 int used_at = 0;
252b5132
RH
4161 expressionS expr1;
4162 const char *s;
4163 const char *s2;
4164 const char *fmt;
4165 int likely = 0;
4166 int dbl = 0;
4167 int coproc = 0;
4168 int lr = 0;
4169 int imm = 0;
1abe91b1 4170 int call = 0;
252b5132 4171 int off;
67c0d1eb 4172 offsetT maxnum;
252b5132 4173 bfd_reloc_code_real_type r;
252b5132
RH
4174 int hold_mips_optimize;
4175
4176 assert (! mips_opts.mips16);
4177
4178 treg = (ip->insn_opcode >> 16) & 0x1f;
4179 dreg = (ip->insn_opcode >> 11) & 0x1f;
4180 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
4181 mask = ip->insn_mo->mask;
4182
4183 expr1.X_op = O_constant;
4184 expr1.X_op_symbol = NULL;
4185 expr1.X_add_symbol = NULL;
4186 expr1.X_add_number = 1;
4187
4188 switch (mask)
4189 {
4190 case M_DABS:
4191 dbl = 1;
4192 case M_ABS:
4193 /* bgez $a0,.+12
4194 move v0,$a0
4195 sub v0,$zero,$a0
4196 */
4197
7d10b47d 4198 start_noreorder ();
252b5132
RH
4199
4200 expr1.X_add_number = 8;
67c0d1eb 4201 macro_build (&expr1, "bgez", "s,p", sreg);
252b5132 4202 if (dreg == sreg)
67c0d1eb 4203 macro_build (NULL, "nop", "", 0);
252b5132 4204 else
67c0d1eb
RS
4205 move_register (dreg, sreg);
4206 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
252b5132 4207
7d10b47d 4208 end_noreorder ();
8fc2e39e 4209 break;
252b5132
RH
4210
4211 case M_ADD_I:
4212 s = "addi";
4213 s2 = "add";
4214 goto do_addi;
4215 case M_ADDU_I:
4216 s = "addiu";
4217 s2 = "addu";
4218 goto do_addi;
4219 case M_DADD_I:
4220 dbl = 1;
4221 s = "daddi";
4222 s2 = "dadd";
4223 goto do_addi;
4224 case M_DADDU_I:
4225 dbl = 1;
4226 s = "daddiu";
4227 s2 = "daddu";
4228 do_addi:
4229 if (imm_expr.X_op == O_constant
4230 && imm_expr.X_add_number >= -0x8000
4231 && imm_expr.X_add_number < 0x8000)
4232 {
67c0d1eb 4233 macro_build (&imm_expr, s, "t,r,j", treg, sreg, BFD_RELOC_LO16);
8fc2e39e 4234 break;
252b5132 4235 }
8fc2e39e 4236 used_at = 1;
67c0d1eb
RS
4237 load_register (AT, &imm_expr, dbl);
4238 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
252b5132
RH
4239 break;
4240
4241 case M_AND_I:
4242 s = "andi";
4243 s2 = "and";
4244 goto do_bit;
4245 case M_OR_I:
4246 s = "ori";
4247 s2 = "or";
4248 goto do_bit;
4249 case M_NOR_I:
4250 s = "";
4251 s2 = "nor";
4252 goto do_bit;
4253 case M_XOR_I:
4254 s = "xori";
4255 s2 = "xor";
4256 do_bit:
4257 if (imm_expr.X_op == O_constant
4258 && imm_expr.X_add_number >= 0
4259 && imm_expr.X_add_number < 0x10000)
4260 {
4261 if (mask != M_NOR_I)
67c0d1eb 4262 macro_build (&imm_expr, s, "t,r,i", treg, sreg, BFD_RELOC_LO16);
252b5132
RH
4263 else
4264 {
67c0d1eb
RS
4265 macro_build (&imm_expr, "ori", "t,r,i",
4266 treg, sreg, BFD_RELOC_LO16);
4267 macro_build (NULL, "nor", "d,v,t", treg, treg, 0);
252b5132 4268 }
8fc2e39e 4269 break;
252b5132
RH
4270 }
4271
8fc2e39e 4272 used_at = 1;
67c0d1eb
RS
4273 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
4274 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
252b5132
RH
4275 break;
4276
4277 case M_BEQ_I:
4278 s = "beq";
4279 goto beq_i;
4280 case M_BEQL_I:
4281 s = "beql";
4282 likely = 1;
4283 goto beq_i;
4284 case M_BNE_I:
4285 s = "bne";
4286 goto beq_i;
4287 case M_BNEL_I:
4288 s = "bnel";
4289 likely = 1;
4290 beq_i:
4291 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4292 {
67c0d1eb 4293 macro_build (&offset_expr, s, "s,t,p", sreg, 0);
8fc2e39e 4294 break;
252b5132 4295 }
8fc2e39e 4296 used_at = 1;
67c0d1eb
RS
4297 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
4298 macro_build (&offset_expr, s, "s,t,p", sreg, AT);
252b5132
RH
4299 break;
4300
4301 case M_BGEL:
4302 likely = 1;
4303 case M_BGE:
4304 if (treg == 0)
4305 {
67c0d1eb 4306 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", sreg);
8fc2e39e 4307 break;
252b5132
RH
4308 }
4309 if (sreg == 0)
4310 {
67c0d1eb 4311 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", treg);
8fc2e39e 4312 break;
252b5132 4313 }
8fc2e39e 4314 used_at = 1;
67c0d1eb
RS
4315 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
4316 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4317 break;
4318
4319 case M_BGTL_I:
4320 likely = 1;
4321 case M_BGT_I:
4322 /* check for > max integer */
4323 maxnum = 0x7fffffff;
ca4e0257 4324 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
4325 {
4326 maxnum <<= 16;
4327 maxnum |= 0xffff;
4328 maxnum <<= 16;
4329 maxnum |= 0xffff;
4330 }
4331 if (imm_expr.X_op == O_constant
4332 && imm_expr.X_add_number >= maxnum
ca4e0257 4333 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
4334 {
4335 do_false:
4336 /* result is always false */
4337 if (! likely)
67c0d1eb 4338 macro_build (NULL, "nop", "", 0);
252b5132 4339 else
67c0d1eb 4340 macro_build (&offset_expr, "bnel", "s,t,p", 0, 0);
8fc2e39e 4341 break;
252b5132
RH
4342 }
4343 if (imm_expr.X_op != O_constant)
4344 as_bad (_("Unsupported large constant"));
f9419b05 4345 ++imm_expr.X_add_number;
252b5132
RH
4346 /* FALLTHROUGH */
4347 case M_BGE_I:
4348 case M_BGEL_I:
4349 if (mask == M_BGEL_I)
4350 likely = 1;
4351 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4352 {
67c0d1eb 4353 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", sreg);
8fc2e39e 4354 break;
252b5132
RH
4355 }
4356 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4357 {
67c0d1eb 4358 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", sreg);
8fc2e39e 4359 break;
252b5132
RH
4360 }
4361 maxnum = 0x7fffffff;
ca4e0257 4362 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
4363 {
4364 maxnum <<= 16;
4365 maxnum |= 0xffff;
4366 maxnum <<= 16;
4367 maxnum |= 0xffff;
4368 }
4369 maxnum = - maxnum - 1;
4370 if (imm_expr.X_op == O_constant
4371 && imm_expr.X_add_number <= maxnum
ca4e0257 4372 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
4373 {
4374 do_true:
4375 /* result is always true */
4376 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
67c0d1eb 4377 macro_build (&offset_expr, "b", "p");
8fc2e39e 4378 break;
252b5132 4379 }
8fc2e39e 4380 used_at = 1;
67c0d1eb
RS
4381 set_at (sreg, 0);
4382 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4383 break;
4384
4385 case M_BGEUL:
4386 likely = 1;
4387 case M_BGEU:
4388 if (treg == 0)
4389 goto do_true;
4390 if (sreg == 0)
4391 {
67c0d1eb 4392 macro_build (&offset_expr, likely ? "beql" : "beq",
17a2f251 4393 "s,t,p", 0, treg);
8fc2e39e 4394 break;
252b5132 4395 }
8fc2e39e 4396 used_at = 1;
67c0d1eb
RS
4397 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
4398 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4399 break;
4400
4401 case M_BGTUL_I:
4402 likely = 1;
4403 case M_BGTU_I:
4404 if (sreg == 0
ca4e0257 4405 || (HAVE_32BIT_GPRS
252b5132 4406 && imm_expr.X_op == O_constant
956cd1d6 4407 && imm_expr.X_add_number == (offsetT) 0xffffffff))
252b5132
RH
4408 goto do_false;
4409 if (imm_expr.X_op != O_constant)
4410 as_bad (_("Unsupported large constant"));
f9419b05 4411 ++imm_expr.X_add_number;
252b5132
RH
4412 /* FALLTHROUGH */
4413 case M_BGEU_I:
4414 case M_BGEUL_I:
4415 if (mask == M_BGEUL_I)
4416 likely = 1;
4417 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4418 goto do_true;
4419 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4420 {
67c0d1eb 4421 macro_build (&offset_expr, likely ? "bnel" : "bne",
17a2f251 4422 "s,t,p", sreg, 0);
8fc2e39e 4423 break;
252b5132 4424 }
8fc2e39e 4425 used_at = 1;
67c0d1eb
RS
4426 set_at (sreg, 1);
4427 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4428 break;
4429
4430 case M_BGTL:
4431 likely = 1;
4432 case M_BGT:
4433 if (treg == 0)
4434 {
67c0d1eb 4435 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", sreg);
8fc2e39e 4436 break;
252b5132
RH
4437 }
4438 if (sreg == 0)
4439 {
67c0d1eb 4440 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", treg);
8fc2e39e 4441 break;
252b5132 4442 }
8fc2e39e 4443 used_at = 1;
67c0d1eb
RS
4444 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
4445 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4446 break;
4447
4448 case M_BGTUL:
4449 likely = 1;
4450 case M_BGTU:
4451 if (treg == 0)
4452 {
67c0d1eb 4453 macro_build (&offset_expr, likely ? "bnel" : "bne",
17a2f251 4454 "s,t,p", sreg, 0);
8fc2e39e 4455 break;
252b5132
RH
4456 }
4457 if (sreg == 0)
4458 goto do_false;
8fc2e39e 4459 used_at = 1;
67c0d1eb
RS
4460 macro_build (NULL, "sltu", "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_BLEL:
4465 likely = 1;
4466 case M_BLE:
4467 if (treg == 0)
4468 {
67c0d1eb 4469 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", sreg);
8fc2e39e 4470 break;
252b5132
RH
4471 }
4472 if (sreg == 0)
4473 {
67c0d1eb 4474 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", treg);
8fc2e39e 4475 break;
252b5132 4476 }
8fc2e39e 4477 used_at = 1;
67c0d1eb
RS
4478 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
4479 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4480 break;
4481
4482 case M_BLEL_I:
4483 likely = 1;
4484 case M_BLE_I:
4485 maxnum = 0x7fffffff;
ca4e0257 4486 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
4487 {
4488 maxnum <<= 16;
4489 maxnum |= 0xffff;
4490 maxnum <<= 16;
4491 maxnum |= 0xffff;
4492 }
4493 if (imm_expr.X_op == O_constant
4494 && imm_expr.X_add_number >= maxnum
ca4e0257 4495 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
4496 goto do_true;
4497 if (imm_expr.X_op != O_constant)
4498 as_bad (_("Unsupported large constant"));
f9419b05 4499 ++imm_expr.X_add_number;
252b5132
RH
4500 /* FALLTHROUGH */
4501 case M_BLT_I:
4502 case M_BLTL_I:
4503 if (mask == M_BLTL_I)
4504 likely = 1;
4505 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4506 {
67c0d1eb 4507 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", sreg);
8fc2e39e 4508 break;
252b5132
RH
4509 }
4510 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4511 {
67c0d1eb 4512 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", sreg);
8fc2e39e 4513 break;
252b5132 4514 }
8fc2e39e 4515 used_at = 1;
67c0d1eb
RS
4516 set_at (sreg, 0);
4517 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4518 break;
4519
4520 case M_BLEUL:
4521 likely = 1;
4522 case M_BLEU:
4523 if (treg == 0)
4524 {
67c0d1eb 4525 macro_build (&offset_expr, likely ? "beql" : "beq",
17a2f251 4526 "s,t,p", sreg, 0);
8fc2e39e 4527 break;
252b5132
RH
4528 }
4529 if (sreg == 0)
4530 goto do_true;
8fc2e39e 4531 used_at = 1;
67c0d1eb
RS
4532 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
4533 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4534 break;
4535
4536 case M_BLEUL_I:
4537 likely = 1;
4538 case M_BLEU_I:
4539 if (sreg == 0
ca4e0257 4540 || (HAVE_32BIT_GPRS
252b5132 4541 && imm_expr.X_op == O_constant
956cd1d6 4542 && imm_expr.X_add_number == (offsetT) 0xffffffff))
252b5132
RH
4543 goto do_true;
4544 if (imm_expr.X_op != O_constant)
4545 as_bad (_("Unsupported large constant"));
f9419b05 4546 ++imm_expr.X_add_number;
252b5132
RH
4547 /* FALLTHROUGH */
4548 case M_BLTU_I:
4549 case M_BLTUL_I:
4550 if (mask == M_BLTUL_I)
4551 likely = 1;
4552 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4553 goto do_false;
4554 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4555 {
67c0d1eb 4556 macro_build (&offset_expr, likely ? "beql" : "beq",
252b5132 4557 "s,t,p", sreg, 0);
8fc2e39e 4558 break;
252b5132 4559 }
8fc2e39e 4560 used_at = 1;
67c0d1eb
RS
4561 set_at (sreg, 1);
4562 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4563 break;
4564
4565 case M_BLTL:
4566 likely = 1;
4567 case M_BLT:
4568 if (treg == 0)
4569 {
67c0d1eb 4570 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", sreg);
8fc2e39e 4571 break;
252b5132
RH
4572 }
4573 if (sreg == 0)
4574 {
67c0d1eb 4575 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", treg);
8fc2e39e 4576 break;
252b5132 4577 }
8fc2e39e 4578 used_at = 1;
67c0d1eb
RS
4579 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
4580 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4581 break;
4582
4583 case M_BLTUL:
4584 likely = 1;
4585 case M_BLTU:
4586 if (treg == 0)
4587 goto do_false;
4588 if (sreg == 0)
4589 {
67c0d1eb 4590 macro_build (&offset_expr, likely ? "bnel" : "bne",
17a2f251 4591 "s,t,p", 0, treg);
8fc2e39e 4592 break;
252b5132 4593 }
8fc2e39e 4594 used_at = 1;
67c0d1eb
RS
4595 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
4596 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4597 break;
4598
5f74bc13
CD
4599 case M_DEXT:
4600 {
4601 unsigned long pos;
4602 unsigned long size;
4603
4604 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
4605 {
4606 as_bad (_("Unsupported large constant"));
4607 pos = size = 1;
4608 }
4609 else
4610 {
4611 pos = (unsigned long) imm_expr.X_add_number;
4612 size = (unsigned long) imm2_expr.X_add_number;
4613 }
4614
4615 if (pos > 63)
4616 {
4617 as_bad (_("Improper position (%lu)"), pos);
4618 pos = 1;
4619 }
4620 if (size == 0 || size > 64
4621 || (pos + size - 1) > 63)
4622 {
4623 as_bad (_("Improper extract size (%lu, position %lu)"),
4624 size, pos);
4625 size = 1;
4626 }
4627
4628 if (size <= 32 && pos < 32)
4629 {
4630 s = "dext";
4631 fmt = "t,r,+A,+C";
4632 }
4633 else if (size <= 32)
4634 {
4635 s = "dextu";
4636 fmt = "t,r,+E,+H";
4637 }
4638 else
4639 {
4640 s = "dextm";
4641 fmt = "t,r,+A,+G";
4642 }
67c0d1eb 4643 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, pos, size - 1);
5f74bc13 4644 }
8fc2e39e 4645 break;
5f74bc13
CD
4646
4647 case M_DINS:
4648 {
4649 unsigned long pos;
4650 unsigned long size;
4651
4652 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
4653 {
4654 as_bad (_("Unsupported large constant"));
4655 pos = size = 1;
4656 }
4657 else
4658 {
4659 pos = (unsigned long) imm_expr.X_add_number;
4660 size = (unsigned long) imm2_expr.X_add_number;
4661 }
4662
4663 if (pos > 63)
4664 {
4665 as_bad (_("Improper position (%lu)"), pos);
4666 pos = 1;
4667 }
4668 if (size == 0 || size > 64
4669 || (pos + size - 1) > 63)
4670 {
4671 as_bad (_("Improper insert size (%lu, position %lu)"),
4672 size, pos);
4673 size = 1;
4674 }
4675
4676 if (pos < 32 && (pos + size - 1) < 32)
4677 {
4678 s = "dins";
4679 fmt = "t,r,+A,+B";
4680 }
4681 else if (pos >= 32)
4682 {
4683 s = "dinsu";
4684 fmt = "t,r,+E,+F";
4685 }
4686 else
4687 {
4688 s = "dinsm";
4689 fmt = "t,r,+A,+F";
4690 }
67c0d1eb
RS
4691 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, pos,
4692 pos + size - 1);
5f74bc13 4693 }
8fc2e39e 4694 break;
5f74bc13 4695
252b5132
RH
4696 case M_DDIV_3:
4697 dbl = 1;
4698 case M_DIV_3:
4699 s = "mflo";
4700 goto do_div3;
4701 case M_DREM_3:
4702 dbl = 1;
4703 case M_REM_3:
4704 s = "mfhi";
4705 do_div3:
4706 if (treg == 0)
4707 {
4708 as_warn (_("Divide by zero."));
4709 if (mips_trap)
67c0d1eb 4710 macro_build (NULL, "teq", "s,t,q", 0, 0, 7);
252b5132 4711 else
67c0d1eb 4712 macro_build (NULL, "break", "c", 7);
8fc2e39e 4713 break;
252b5132
RH
4714 }
4715
7d10b47d 4716 start_noreorder ();
252b5132
RH
4717 if (mips_trap)
4718 {
67c0d1eb
RS
4719 macro_build (NULL, "teq", "s,t,q", treg, 0, 7);
4720 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
252b5132
RH
4721 }
4722 else
4723 {
4724 expr1.X_add_number = 8;
67c0d1eb
RS
4725 macro_build (&expr1, "bne", "s,t,p", treg, 0);
4726 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4727 macro_build (NULL, "break", "c", 7);
252b5132
RH
4728 }
4729 expr1.X_add_number = -1;
8fc2e39e 4730 used_at = 1;
f6a22291 4731 load_register (AT, &expr1, dbl);
252b5132 4732 expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
67c0d1eb 4733 macro_build (&expr1, "bne", "s,t,p", treg, AT);
252b5132
RH
4734 if (dbl)
4735 {
4736 expr1.X_add_number = 1;
f6a22291 4737 load_register (AT, &expr1, dbl);
67c0d1eb 4738 macro_build (NULL, "dsll32", "d,w,<", AT, AT, 31);
252b5132
RH
4739 }
4740 else
4741 {
4742 expr1.X_add_number = 0x80000000;
67c0d1eb 4743 macro_build (&expr1, "lui", "t,u", AT, BFD_RELOC_HI16);
252b5132
RH
4744 }
4745 if (mips_trap)
4746 {
67c0d1eb 4747 macro_build (NULL, "teq", "s,t,q", sreg, AT, 6);
252b5132
RH
4748 /* We want to close the noreorder block as soon as possible, so
4749 that later insns are available for delay slot filling. */
7d10b47d 4750 end_noreorder ();
252b5132
RH
4751 }
4752 else
4753 {
4754 expr1.X_add_number = 8;
67c0d1eb
RS
4755 macro_build (&expr1, "bne", "s,t,p", sreg, AT);
4756 macro_build (NULL, "nop", "", 0);
252b5132
RH
4757
4758 /* We want to close the noreorder block as soon as possible, so
4759 that later insns are available for delay slot filling. */
7d10b47d 4760 end_noreorder ();
252b5132 4761
67c0d1eb 4762 macro_build (NULL, "break", "c", 6);
252b5132 4763 }
67c0d1eb 4764 macro_build (NULL, s, "d", dreg);
252b5132
RH
4765 break;
4766
4767 case M_DIV_3I:
4768 s = "div";
4769 s2 = "mflo";
4770 goto do_divi;
4771 case M_DIVU_3I:
4772 s = "divu";
4773 s2 = "mflo";
4774 goto do_divi;
4775 case M_REM_3I:
4776 s = "div";
4777 s2 = "mfhi";
4778 goto do_divi;
4779 case M_REMU_3I:
4780 s = "divu";
4781 s2 = "mfhi";
4782 goto do_divi;
4783 case M_DDIV_3I:
4784 dbl = 1;
4785 s = "ddiv";
4786 s2 = "mflo";
4787 goto do_divi;
4788 case M_DDIVU_3I:
4789 dbl = 1;
4790 s = "ddivu";
4791 s2 = "mflo";
4792 goto do_divi;
4793 case M_DREM_3I:
4794 dbl = 1;
4795 s = "ddiv";
4796 s2 = "mfhi";
4797 goto do_divi;
4798 case M_DREMU_3I:
4799 dbl = 1;
4800 s = "ddivu";
4801 s2 = "mfhi";
4802 do_divi:
4803 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4804 {
4805 as_warn (_("Divide by zero."));
4806 if (mips_trap)
67c0d1eb 4807 macro_build (NULL, "teq", "s,t,q", 0, 0, 7);
252b5132 4808 else
67c0d1eb 4809 macro_build (NULL, "break", "c", 7);
8fc2e39e 4810 break;
252b5132
RH
4811 }
4812 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4813 {
4814 if (strcmp (s2, "mflo") == 0)
67c0d1eb 4815 move_register (dreg, sreg);
252b5132 4816 else
67c0d1eb 4817 move_register (dreg, 0);
8fc2e39e 4818 break;
252b5132
RH
4819 }
4820 if (imm_expr.X_op == O_constant
4821 && imm_expr.X_add_number == -1
4822 && s[strlen (s) - 1] != 'u')
4823 {
4824 if (strcmp (s2, "mflo") == 0)
4825 {
67c0d1eb 4826 macro_build (NULL, dbl ? "dneg" : "neg", "d,w", dreg, sreg);
252b5132
RH
4827 }
4828 else
67c0d1eb 4829 move_register (dreg, 0);
8fc2e39e 4830 break;
252b5132
RH
4831 }
4832
8fc2e39e 4833 used_at = 1;
67c0d1eb
RS
4834 load_register (AT, &imm_expr, dbl);
4835 macro_build (NULL, s, "z,s,t", sreg, AT);
4836 macro_build (NULL, s2, "d", dreg);
252b5132
RH
4837 break;
4838
4839 case M_DIVU_3:
4840 s = "divu";
4841 s2 = "mflo";
4842 goto do_divu3;
4843 case M_REMU_3:
4844 s = "divu";
4845 s2 = "mfhi";
4846 goto do_divu3;
4847 case M_DDIVU_3:
4848 s = "ddivu";
4849 s2 = "mflo";
4850 goto do_divu3;
4851 case M_DREMU_3:
4852 s = "ddivu";
4853 s2 = "mfhi";
4854 do_divu3:
7d10b47d 4855 start_noreorder ();
252b5132
RH
4856 if (mips_trap)
4857 {
67c0d1eb
RS
4858 macro_build (NULL, "teq", "s,t,q", treg, 0, 7);
4859 macro_build (NULL, s, "z,s,t", sreg, treg);
252b5132
RH
4860 /* We want to close the noreorder block as soon as possible, so
4861 that later insns are available for delay slot filling. */
7d10b47d 4862 end_noreorder ();
252b5132
RH
4863 }
4864 else
4865 {
4866 expr1.X_add_number = 8;
67c0d1eb
RS
4867 macro_build (&expr1, "bne", "s,t,p", treg, 0);
4868 macro_build (NULL, s, "z,s,t", sreg, treg);
252b5132
RH
4869
4870 /* We want to close the noreorder block as soon as possible, so
4871 that later insns are available for delay slot filling. */
7d10b47d 4872 end_noreorder ();
67c0d1eb 4873 macro_build (NULL, "break", "c", 7);
252b5132 4874 }
67c0d1eb 4875 macro_build (NULL, s2, "d", dreg);
8fc2e39e 4876 break;
252b5132 4877
1abe91b1
MR
4878 case M_DLCA_AB:
4879 dbl = 1;
4880 case M_LCA_AB:
4881 call = 1;
4882 goto do_la;
252b5132
RH
4883 case M_DLA_AB:
4884 dbl = 1;
4885 case M_LA_AB:
1abe91b1 4886 do_la:
252b5132
RH
4887 /* Load the address of a symbol into a register. If breg is not
4888 zero, we then add a base register to it. */
4889
3bec30a8
TS
4890 if (dbl && HAVE_32BIT_GPRS)
4891 as_warn (_("dla used to load 32-bit register"));
4892
c90bbe5b 4893 if (! dbl && HAVE_64BIT_OBJECTS)
3bec30a8
TS
4894 as_warn (_("la used to load 64-bit address"));
4895
0c11417f
MR
4896 if (offset_expr.X_op == O_constant
4897 && offset_expr.X_add_number >= -0x8000
4898 && offset_expr.X_add_number < 0x8000)
4899 {
aed1a261 4900 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
17a2f251 4901 "t,r,j", treg, sreg, BFD_RELOC_LO16);
8fc2e39e 4902 break;
0c11417f
MR
4903 }
4904
b8285c27 4905 if (!mips_opts.noat && (treg == breg))
afdbd6d0
CD
4906 {
4907 tempreg = AT;
4908 used_at = 1;
4909 }
4910 else
4911 {
4912 tempreg = treg;
afdbd6d0
CD
4913 }
4914
252b5132
RH
4915 if (offset_expr.X_op != O_symbol
4916 && offset_expr.X_op != O_constant)
4917 {
4918 as_bad (_("expression too complex"));
4919 offset_expr.X_op = O_constant;
4920 }
4921
252b5132 4922 if (offset_expr.X_op == O_constant)
aed1a261 4923 load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
252b5132
RH
4924 else if (mips_pic == NO_PIC)
4925 {
d6bc6245 4926 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 4927 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
4928 Otherwise we want
4929 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
4930 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4931 If we have a constant, we need two instructions anyhow,
d6bc6245 4932 so we may as well always use the latter form.
76b3015f 4933
6caf9ef4
TS
4934 With 64bit address space and a usable $at we want
4935 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4936 lui $at,<sym> (BFD_RELOC_HI16_S)
4937 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4938 daddiu $at,<sym> (BFD_RELOC_LO16)
4939 dsll32 $tempreg,0
4940 daddu $tempreg,$tempreg,$at
4941
4942 If $at is already in use, we use a path which is suboptimal
4943 on superscalar processors.
4944 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4945 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4946 dsll $tempreg,16
4947 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
4948 dsll $tempreg,16
4949 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
4950
4951 For GP relative symbols in 64bit address space we can use
4952 the same sequence as in 32bit address space. */
aed1a261 4953 if (HAVE_64BIT_SYMBOLS)
252b5132 4954 {
6caf9ef4
TS
4955 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4956 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
4957 {
4958 relax_start (offset_expr.X_add_symbol);
4959 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
4960 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
4961 relax_switch ();
4962 }
d6bc6245 4963
b8285c27 4964 if (used_at == 0 && !mips_opts.noat)
98d3f06f 4965 {
67c0d1eb 4966 macro_build (&offset_expr, "lui", "t,u",
17a2f251 4967 tempreg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb 4968 macro_build (&offset_expr, "lui", "t,u",
17a2f251 4969 AT, BFD_RELOC_HI16_S);
67c0d1eb 4970 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 4971 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
67c0d1eb 4972 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 4973 AT, AT, BFD_RELOC_LO16);
67c0d1eb
RS
4974 macro_build (NULL, "dsll32", "d,w,<", tempreg, tempreg, 0);
4975 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
98d3f06f
KH
4976 used_at = 1;
4977 }
4978 else
4979 {
67c0d1eb 4980 macro_build (&offset_expr, "lui", "t,u",
17a2f251 4981 tempreg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb 4982 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 4983 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
67c0d1eb
RS
4984 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
4985 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 4986 tempreg, tempreg, BFD_RELOC_HI16_S);
67c0d1eb
RS
4987 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
4988 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 4989 tempreg, tempreg, BFD_RELOC_LO16);
98d3f06f 4990 }
6caf9ef4
TS
4991
4992 if (mips_relax.sequence)
4993 relax_end ();
98d3f06f
KH
4994 }
4995 else
4996 {
4997 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 4998 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
98d3f06f 4999 {
4d7206a2 5000 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
5001 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5002 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 5003 relax_switch ();
98d3f06f 5004 }
6943caf0
ILT
5005 if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
5006 as_bad (_("offset too large"));
67c0d1eb
RS
5007 macro_build_lui (&offset_expr, tempreg);
5008 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5009 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2
RS
5010 if (mips_relax.sequence)
5011 relax_end ();
98d3f06f 5012 }
252b5132 5013 }
f5040a92 5014 else if (mips_pic == SVR4_PIC && ! mips_big_got && ! HAVE_NEWABI)
252b5132 5015 {
9117d219
NC
5016 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5017
252b5132
RH
5018 /* If this is a reference to an external symbol, and there
5019 is no constant, we want
5020 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
1abe91b1 5021 or for lca or if tempreg is PIC_CALL_REG
9117d219 5022 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
252b5132
RH
5023 For a local symbol, we want
5024 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5025 nop
5026 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5027
5028 If we have a small constant, and this is a reference to
5029 an external symbol, we want
5030 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5031 nop
5032 addiu $tempreg,$tempreg,<constant>
5033 For a local symbol, we want the same instruction
5034 sequence, but we output a BFD_RELOC_LO16 reloc on the
5035 addiu instruction.
5036
5037 If we have a large constant, and this is a reference to
5038 an external symbol, we want
5039 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5040 lui $at,<hiconstant>
5041 addiu $at,$at,<loconstant>
5042 addu $tempreg,$tempreg,$at
5043 For a local symbol, we want the same instruction
5044 sequence, but we output a BFD_RELOC_LO16 reloc on the
ed6fb7bd 5045 addiu instruction.
ed6fb7bd
SC
5046 */
5047
4d7206a2 5048 if (offset_expr.X_add_number == 0)
252b5132 5049 {
4d7206a2
RS
5050 if (breg == 0 && (call || tempreg == PIC_CALL_REG))
5051 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
5052
5053 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
5054 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5055 lw_reloc_type, mips_gp_register);
4d7206a2 5056 if (breg != 0)
252b5132
RH
5057 {
5058 /* We're going to put in an addu instruction using
5059 tempreg, so we may as well insert the nop right
5060 now. */
269137b2 5061 load_delay_nop ();
252b5132 5062 }
4d7206a2 5063 relax_switch ();
67c0d1eb
RS
5064 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5065 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 5066 load_delay_nop ();
67c0d1eb
RS
5067 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5068 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2 5069 relax_end ();
252b5132
RH
5070 /* FIXME: If breg == 0, and the next instruction uses
5071 $tempreg, then if this variant case is used an extra
5072 nop will be generated. */
5073 }
4d7206a2
RS
5074 else if (offset_expr.X_add_number >= -0x8000
5075 && offset_expr.X_add_number < 0x8000)
252b5132 5076 {
67c0d1eb 5077 load_got_offset (tempreg, &offset_expr);
269137b2 5078 load_delay_nop ();
67c0d1eb 5079 add_got_offset (tempreg, &offset_expr);
252b5132
RH
5080 }
5081 else
5082 {
4d7206a2
RS
5083 expr1.X_add_number = offset_expr.X_add_number;
5084 offset_expr.X_add_number =
5085 ((offset_expr.X_add_number + 0x8000) & 0xffff) - 0x8000;
67c0d1eb 5086 load_got_offset (tempreg, &offset_expr);
f6a22291 5087 offset_expr.X_add_number = expr1.X_add_number;
252b5132
RH
5088 /* If we are going to add in a base register, and the
5089 target register and the base register are the same,
5090 then we are using AT as a temporary register. Since
5091 we want to load the constant into AT, we add our
5092 current AT (from the global offset table) and the
5093 register into the register now, and pretend we were
5094 not using a base register. */
67c0d1eb 5095 if (breg == treg)
252b5132 5096 {
269137b2 5097 load_delay_nop ();
67c0d1eb 5098 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5099 treg, AT, breg);
252b5132
RH
5100 breg = 0;
5101 tempreg = treg;
252b5132 5102 }
f6a22291 5103 add_got_offset_hilo (tempreg, &offset_expr, AT);
252b5132
RH
5104 used_at = 1;
5105 }
5106 }
f5040a92
AO
5107 else if (mips_pic == SVR4_PIC && ! mips_big_got && HAVE_NEWABI)
5108 {
67c0d1eb 5109 int add_breg_early = 0;
f5040a92
AO
5110
5111 /* If this is a reference to an external, and there is no
5112 constant, or local symbol (*), with or without a
5113 constant, we want
5114 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
1abe91b1 5115 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
5116 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5117
5118 If we have a small constant, and this is a reference to
5119 an external symbol, we want
5120 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5121 addiu $tempreg,$tempreg,<constant>
5122
5123 If we have a large constant, and this is a reference to
5124 an external symbol, we want
5125 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5126 lui $at,<hiconstant>
5127 addiu $at,$at,<loconstant>
5128 addu $tempreg,$tempreg,$at
5129
5130 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
5131 local symbols, even though it introduces an additional
5132 instruction. */
5133
f5040a92
AO
5134 if (offset_expr.X_add_number)
5135 {
4d7206a2 5136 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
5137 offset_expr.X_add_number = 0;
5138
4d7206a2 5139 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
5140 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5141 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
5142
5143 if (expr1.X_add_number >= -0x8000
5144 && expr1.X_add_number < 0x8000)
5145 {
67c0d1eb
RS
5146 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
5147 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 5148 }
ecd13cd3 5149 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92
AO
5150 {
5151 int dreg;
5152
5153 /* If we are going to add in a base register, and the
5154 target register and the base register are the same,
5155 then we are using AT as a temporary register. Since
5156 we want to load the constant into AT, we add our
5157 current AT (from the global offset table) and the
5158 register into the register now, and pretend we were
5159 not using a base register. */
5160 if (breg != treg)
5161 dreg = tempreg;
5162 else
5163 {
5164 assert (tempreg == AT);
67c0d1eb
RS
5165 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5166 treg, AT, breg);
f5040a92 5167 dreg = treg;
67c0d1eb 5168 add_breg_early = 1;
f5040a92
AO
5169 }
5170
f6a22291 5171 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 5172 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5173 dreg, dreg, AT);
f5040a92 5174
f5040a92
AO
5175 used_at = 1;
5176 }
5177 else
5178 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5179
4d7206a2 5180 relax_switch ();
f5040a92
AO
5181 offset_expr.X_add_number = expr1.X_add_number;
5182
67c0d1eb
RS
5183 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5184 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5185 if (add_breg_early)
f5040a92 5186 {
67c0d1eb 5187 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
f899b4b8 5188 treg, tempreg, breg);
f5040a92
AO
5189 breg = 0;
5190 tempreg = treg;
5191 }
4d7206a2 5192 relax_end ();
f5040a92 5193 }
4d7206a2 5194 else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
f5040a92 5195 {
4d7206a2 5196 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
5197 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5198 BFD_RELOC_MIPS_CALL16, mips_gp_register);
4d7206a2 5199 relax_switch ();
67c0d1eb
RS
5200 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5201 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2 5202 relax_end ();
f5040a92 5203 }
4d7206a2 5204 else
f5040a92 5205 {
67c0d1eb
RS
5206 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5207 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
5208 }
5209 }
5210 else if (mips_pic == SVR4_PIC && ! HAVE_NEWABI)
252b5132 5211 {
67c0d1eb 5212 int gpdelay;
9117d219
NC
5213 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5214 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
ed6fb7bd 5215 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
252b5132
RH
5216
5217 /* This is the large GOT case. If this is a reference to an
5218 external symbol, and there is no constant, we want
5219 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5220 addu $tempreg,$tempreg,$gp
5221 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 5222 or for lca or if tempreg is PIC_CALL_REG
9117d219
NC
5223 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5224 addu $tempreg,$tempreg,$gp
5225 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
252b5132
RH
5226 For a local symbol, we want
5227 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5228 nop
5229 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5230
5231 If we have a small constant, and this is a reference to
5232 an external symbol, we want
5233 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5234 addu $tempreg,$tempreg,$gp
5235 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5236 nop
5237 addiu $tempreg,$tempreg,<constant>
5238 For a local symbol, we want
5239 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5240 nop
5241 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
5242
5243 If we have a large constant, and this is a reference to
5244 an external symbol, we want
5245 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5246 addu $tempreg,$tempreg,$gp
5247 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5248 lui $at,<hiconstant>
5249 addiu $at,$at,<loconstant>
5250 addu $tempreg,$tempreg,$at
5251 For a local symbol, we want
5252 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5253 lui $at,<hiconstant>
5254 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
5255 addu $tempreg,$tempreg,$at
f5040a92 5256 */
438c16b8 5257
252b5132
RH
5258 expr1.X_add_number = offset_expr.X_add_number;
5259 offset_expr.X_add_number = 0;
4d7206a2 5260 relax_start (offset_expr.X_add_symbol);
67c0d1eb 5261 gpdelay = reg_needs_delay (mips_gp_register);
1abe91b1
MR
5262 if (expr1.X_add_number == 0 && breg == 0
5263 && (call || tempreg == PIC_CALL_REG))
9117d219
NC
5264 {
5265 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5266 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5267 }
67c0d1eb
RS
5268 macro_build (&offset_expr, "lui", "t,u", tempreg, lui_reloc_type);
5269 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5270 tempreg, tempreg, mips_gp_register);
67c0d1eb 5271 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 5272 tempreg, lw_reloc_type, tempreg);
252b5132
RH
5273 if (expr1.X_add_number == 0)
5274 {
67c0d1eb 5275 if (breg != 0)
252b5132
RH
5276 {
5277 /* We're going to put in an addu instruction using
5278 tempreg, so we may as well insert the nop right
5279 now. */
269137b2 5280 load_delay_nop ();
252b5132 5281 }
252b5132
RH
5282 }
5283 else if (expr1.X_add_number >= -0x8000
5284 && expr1.X_add_number < 0x8000)
5285 {
269137b2 5286 load_delay_nop ();
67c0d1eb 5287 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 5288 tempreg, tempreg, BFD_RELOC_LO16);
252b5132
RH
5289 }
5290 else
5291 {
67c0d1eb 5292 int dreg;
252b5132
RH
5293
5294 /* If we are going to add in a base register, and the
5295 target register and the base register are the same,
5296 then we are using AT as a temporary register. Since
5297 we want to load the constant into AT, we add our
5298 current AT (from the global offset table) and the
5299 register into the register now, and pretend we were
5300 not using a base register. */
5301 if (breg != treg)
67c0d1eb 5302 dreg = tempreg;
252b5132
RH
5303 else
5304 {
5305 assert (tempreg == AT);
269137b2 5306 load_delay_nop ();
67c0d1eb 5307 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5308 treg, AT, breg);
252b5132 5309 dreg = treg;
252b5132
RH
5310 }
5311
f6a22291 5312 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 5313 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
252b5132 5314
252b5132
RH
5315 used_at = 1;
5316 }
4d7206a2
RS
5317 offset_expr.X_add_number =
5318 ((expr1.X_add_number + 0x8000) & 0xffff) - 0x8000;
5319 relax_switch ();
252b5132 5320
67c0d1eb 5321 if (gpdelay)
252b5132
RH
5322 {
5323 /* This is needed because this instruction uses $gp, but
f5040a92 5324 the first instruction on the main stream does not. */
67c0d1eb 5325 macro_build (NULL, "nop", "");
252b5132 5326 }
ed6fb7bd 5327
67c0d1eb
RS
5328 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5329 local_reloc_type, mips_gp_register);
f5040a92 5330 if (expr1.X_add_number >= -0x8000
252b5132
RH
5331 && expr1.X_add_number < 0x8000)
5332 {
269137b2 5333 load_delay_nop ();
67c0d1eb
RS
5334 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5335 tempreg, tempreg, BFD_RELOC_LO16);
252b5132 5336 /* FIXME: If add_number is 0, and there was no base
f5040a92
AO
5337 register, the external symbol case ended with a load,
5338 so if the symbol turns out to not be external, and
5339 the next instruction uses tempreg, an unnecessary nop
5340 will be inserted. */
252b5132
RH
5341 }
5342 else
5343 {
5344 if (breg == treg)
5345 {
5346 /* We must add in the base register now, as in the
f5040a92 5347 external symbol case. */
252b5132 5348 assert (tempreg == AT);
269137b2 5349 load_delay_nop ();
67c0d1eb 5350 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5351 treg, AT, breg);
252b5132
RH
5352 tempreg = treg;
5353 /* We set breg to 0 because we have arranged to add
f5040a92 5354 it in in both cases. */
252b5132
RH
5355 breg = 0;
5356 }
5357
67c0d1eb
RS
5358 macro_build_lui (&expr1, AT);
5359 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 5360 AT, AT, BFD_RELOC_LO16);
67c0d1eb 5361 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5362 tempreg, tempreg, AT);
8fc2e39e 5363 used_at = 1;
252b5132 5364 }
4d7206a2 5365 relax_end ();
252b5132 5366 }
f5040a92
AO
5367 else if (mips_pic == SVR4_PIC && HAVE_NEWABI)
5368 {
f5040a92
AO
5369 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5370 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
67c0d1eb 5371 int add_breg_early = 0;
f5040a92
AO
5372
5373 /* This is the large GOT case. If this is a reference to an
5374 external symbol, and there is no constant, we want
5375 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5376 add $tempreg,$tempreg,$gp
5377 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 5378 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
5379 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5380 add $tempreg,$tempreg,$gp
5381 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5382
5383 If we have a small constant, and this is a reference to
5384 an external symbol, we want
5385 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5386 add $tempreg,$tempreg,$gp
5387 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5388 addi $tempreg,$tempreg,<constant>
5389
5390 If we have a large constant, and this is a reference to
5391 an external symbol, we want
5392 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5393 addu $tempreg,$tempreg,$gp
5394 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5395 lui $at,<hiconstant>
5396 addi $at,$at,<loconstant>
5397 add $tempreg,$tempreg,$at
5398
5399 If we have NewABI, and we know it's a local symbol, we want
5400 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
5401 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
5402 otherwise we have to resort to GOT_HI16/GOT_LO16. */
5403
4d7206a2 5404 relax_start (offset_expr.X_add_symbol);
f5040a92 5405
4d7206a2 5406 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
5407 offset_expr.X_add_number = 0;
5408
1abe91b1
MR
5409 if (expr1.X_add_number == 0 && breg == 0
5410 && (call || tempreg == PIC_CALL_REG))
f5040a92
AO
5411 {
5412 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5413 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5414 }
67c0d1eb
RS
5415 macro_build (&offset_expr, "lui", "t,u", tempreg, lui_reloc_type);
5416 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5417 tempreg, tempreg, mips_gp_register);
67c0d1eb
RS
5418 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5419 tempreg, lw_reloc_type, tempreg);
f5040a92
AO
5420
5421 if (expr1.X_add_number == 0)
4d7206a2 5422 ;
f5040a92
AO
5423 else if (expr1.X_add_number >= -0x8000
5424 && expr1.X_add_number < 0x8000)
5425 {
67c0d1eb 5426 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 5427 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 5428 }
ecd13cd3 5429 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92
AO
5430 {
5431 int dreg;
5432
5433 /* If we are going to add in a base register, and the
5434 target register and the base register are the same,
5435 then we are using AT as a temporary register. Since
5436 we want to load the constant into AT, we add our
5437 current AT (from the global offset table) and the
5438 register into the register now, and pretend we were
5439 not using a base register. */
5440 if (breg != treg)
5441 dreg = tempreg;
5442 else
5443 {
5444 assert (tempreg == AT);
67c0d1eb 5445 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5446 treg, AT, breg);
f5040a92 5447 dreg = treg;
67c0d1eb 5448 add_breg_early = 1;
f5040a92
AO
5449 }
5450
f6a22291 5451 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 5452 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
f5040a92 5453
f5040a92
AO
5454 used_at = 1;
5455 }
5456 else
5457 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5458
4d7206a2 5459 relax_switch ();
f5040a92 5460 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
5461 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5462 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
5463 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
5464 tempreg, BFD_RELOC_MIPS_GOT_OFST);
5465 if (add_breg_early)
f5040a92 5466 {
67c0d1eb 5467 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5468 treg, tempreg, breg);
f5040a92
AO
5469 breg = 0;
5470 tempreg = treg;
5471 }
4d7206a2 5472 relax_end ();
f5040a92 5473 }
252b5132
RH
5474 else
5475 abort ();
5476
5477 if (breg != 0)
aed1a261 5478 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", treg, tempreg, breg);
252b5132
RH
5479 break;
5480
5481 case M_J_A:
5482 /* The j instruction may not be used in PIC code, since it
5483 requires an absolute address. We convert it to a b
5484 instruction. */
5485 if (mips_pic == NO_PIC)
67c0d1eb 5486 macro_build (&offset_expr, "j", "a");
252b5132 5487 else
67c0d1eb 5488 macro_build (&offset_expr, "b", "p");
8fc2e39e 5489 break;
252b5132
RH
5490
5491 /* The jal instructions must be handled as macros because when
5492 generating PIC code they expand to multi-instruction
5493 sequences. Normally they are simple instructions. */
5494 case M_JAL_1:
5495 dreg = RA;
5496 /* Fall through. */
5497 case M_JAL_2:
3e722fb5 5498 if (mips_pic == NO_PIC)
67c0d1eb 5499 macro_build (NULL, "jalr", "d,s", dreg, sreg);
252b5132
RH
5500 else if (mips_pic == SVR4_PIC)
5501 {
5502 if (sreg != PIC_CALL_REG)
5503 as_warn (_("MIPS PIC call to register other than $25"));
bdaaa2e1 5504
67c0d1eb 5505 macro_build (NULL, "jalr", "d,s", dreg, sreg);
6478892d 5506 if (! HAVE_NEWABI)
252b5132 5507 {
6478892d
TS
5508 if (mips_cprestore_offset < 0)
5509 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5510 else
5511 {
7a621144
DJ
5512 if (! mips_frame_reg_valid)
5513 {
5514 as_warn (_("No .frame pseudo-op used in PIC code"));
5515 /* Quiet this warning. */
5516 mips_frame_reg_valid = 1;
5517 }
5518 if (! mips_cprestore_valid)
5519 {
5520 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5521 /* Quiet this warning. */
5522 mips_cprestore_valid = 1;
5523 }
6478892d 5524 expr1.X_add_number = mips_cprestore_offset;
67c0d1eb 5525 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 5526 mips_gp_register,
256ab948
TS
5527 mips_frame_reg,
5528 HAVE_64BIT_ADDRESSES);
6478892d 5529 }
252b5132
RH
5530 }
5531 }
5532 else
5533 abort ();
5534
8fc2e39e 5535 break;
252b5132
RH
5536
5537 case M_JAL_A:
5538 if (mips_pic == NO_PIC)
67c0d1eb 5539 macro_build (&offset_expr, "jal", "a");
252b5132
RH
5540 else if (mips_pic == SVR4_PIC)
5541 {
5542 /* If this is a reference to an external symbol, and we are
5543 using a small GOT, we want
5544 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5545 nop
f9419b05 5546 jalr $ra,$25
252b5132
RH
5547 nop
5548 lw $gp,cprestore($sp)
5549 The cprestore value is set using the .cprestore
5550 pseudo-op. If we are using a big GOT, we want
5551 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5552 addu $25,$25,$gp
5553 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
5554 nop
f9419b05 5555 jalr $ra,$25
252b5132
RH
5556 nop
5557 lw $gp,cprestore($sp)
5558 If the symbol is not external, we want
5559 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5560 nop
5561 addiu $25,$25,<sym> (BFD_RELOC_LO16)
f9419b05 5562 jalr $ra,$25
252b5132 5563 nop
438c16b8 5564 lw $gp,cprestore($sp)
f5040a92
AO
5565
5566 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
5567 sequences above, minus nops, unless the symbol is local,
5568 which enables us to use GOT_PAGE/GOT_OFST (big got) or
5569 GOT_DISP. */
438c16b8 5570 if (HAVE_NEWABI)
252b5132 5571 {
f5040a92
AO
5572 if (! mips_big_got)
5573 {
4d7206a2 5574 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
5575 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5576 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
f5040a92 5577 mips_gp_register);
4d7206a2 5578 relax_switch ();
67c0d1eb
RS
5579 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5580 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
4d7206a2
RS
5581 mips_gp_register);
5582 relax_end ();
f5040a92
AO
5583 }
5584 else
5585 {
4d7206a2 5586 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
5587 macro_build (&offset_expr, "lui", "t,u", PIC_CALL_REG,
5588 BFD_RELOC_MIPS_CALL_HI16);
5589 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
5590 PIC_CALL_REG, mips_gp_register);
5591 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5592 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
5593 PIC_CALL_REG);
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_PAGE,
5597 mips_gp_register);
5598 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5599 PIC_CALL_REG, PIC_CALL_REG,
17a2f251 5600 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 5601 relax_end ();
f5040a92 5602 }
684022ea 5603
67c0d1eb 5604 macro_build_jalr (&offset_expr);
252b5132
RH
5605 }
5606 else
5607 {
4d7206a2 5608 relax_start (offset_expr.X_add_symbol);
438c16b8
TS
5609 if (! mips_big_got)
5610 {
67c0d1eb
RS
5611 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5612 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
17a2f251 5613 mips_gp_register);
269137b2 5614 load_delay_nop ();
4d7206a2 5615 relax_switch ();
438c16b8 5616 }
252b5132 5617 else
252b5132 5618 {
67c0d1eb
RS
5619 int gpdelay;
5620
5621 gpdelay = reg_needs_delay (mips_gp_register);
5622 macro_build (&offset_expr, "lui", "t,u", PIC_CALL_REG,
5623 BFD_RELOC_MIPS_CALL_HI16);
5624 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
5625 PIC_CALL_REG, mips_gp_register);
5626 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5627 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
5628 PIC_CALL_REG);
269137b2 5629 load_delay_nop ();
4d7206a2 5630 relax_switch ();
67c0d1eb
RS
5631 if (gpdelay)
5632 macro_build (NULL, "nop", "");
252b5132 5633 }
67c0d1eb
RS
5634 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5635 PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
4d7206a2 5636 mips_gp_register);
269137b2 5637 load_delay_nop ();
67c0d1eb
RS
5638 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5639 PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
4d7206a2 5640 relax_end ();
67c0d1eb 5641 macro_build_jalr (&offset_expr);
438c16b8 5642
6478892d
TS
5643 if (mips_cprestore_offset < 0)
5644 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5645 else
5646 {
7a621144
DJ
5647 if (! mips_frame_reg_valid)
5648 {
5649 as_warn (_("No .frame pseudo-op used in PIC code"));
5650 /* Quiet this warning. */
5651 mips_frame_reg_valid = 1;
5652 }
5653 if (! mips_cprestore_valid)
5654 {
5655 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5656 /* Quiet this warning. */
5657 mips_cprestore_valid = 1;
5658 }
6478892d 5659 if (mips_opts.noreorder)
67c0d1eb 5660 macro_build (NULL, "nop", "");
6478892d 5661 expr1.X_add_number = mips_cprestore_offset;
67c0d1eb 5662 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 5663 mips_gp_register,
256ab948
TS
5664 mips_frame_reg,
5665 HAVE_64BIT_ADDRESSES);
6478892d 5666 }
252b5132
RH
5667 }
5668 }
252b5132
RH
5669 else
5670 abort ();
5671
8fc2e39e 5672 break;
252b5132
RH
5673
5674 case M_LB_AB:
5675 s = "lb";
5676 goto ld;
5677 case M_LBU_AB:
5678 s = "lbu";
5679 goto ld;
5680 case M_LH_AB:
5681 s = "lh";
5682 goto ld;
5683 case M_LHU_AB:
5684 s = "lhu";
5685 goto ld;
5686 case M_LW_AB:
5687 s = "lw";
5688 goto ld;
5689 case M_LWC0_AB:
5690 s = "lwc0";
bdaaa2e1 5691 /* Itbl support may require additional care here. */
252b5132
RH
5692 coproc = 1;
5693 goto ld;
5694 case M_LWC1_AB:
5695 s = "lwc1";
bdaaa2e1 5696 /* Itbl support may require additional care here. */
252b5132
RH
5697 coproc = 1;
5698 goto ld;
5699 case M_LWC2_AB:
5700 s = "lwc2";
bdaaa2e1 5701 /* Itbl support may require additional care here. */
252b5132
RH
5702 coproc = 1;
5703 goto ld;
5704 case M_LWC3_AB:
5705 s = "lwc3";
bdaaa2e1 5706 /* Itbl support may require additional care here. */
252b5132
RH
5707 coproc = 1;
5708 goto ld;
5709 case M_LWL_AB:
5710 s = "lwl";
5711 lr = 1;
5712 goto ld;
5713 case M_LWR_AB:
5714 s = "lwr";
5715 lr = 1;
5716 goto ld;
5717 case M_LDC1_AB:
fef14a42 5718 if (mips_opts.arch == CPU_R4650)
252b5132
RH
5719 {
5720 as_bad (_("opcode not supported on this processor"));
8fc2e39e 5721 break;
252b5132
RH
5722 }
5723 s = "ldc1";
bdaaa2e1 5724 /* Itbl support may require additional care here. */
252b5132
RH
5725 coproc = 1;
5726 goto ld;
5727 case M_LDC2_AB:
5728 s = "ldc2";
bdaaa2e1 5729 /* Itbl support may require additional care here. */
252b5132
RH
5730 coproc = 1;
5731 goto ld;
5732 case M_LDC3_AB:
5733 s = "ldc3";
bdaaa2e1 5734 /* Itbl support may require additional care here. */
252b5132
RH
5735 coproc = 1;
5736 goto ld;
5737 case M_LDL_AB:
5738 s = "ldl";
5739 lr = 1;
5740 goto ld;
5741 case M_LDR_AB:
5742 s = "ldr";
5743 lr = 1;
5744 goto ld;
5745 case M_LL_AB:
5746 s = "ll";
5747 goto ld;
5748 case M_LLD_AB:
5749 s = "lld";
5750 goto ld;
5751 case M_LWU_AB:
5752 s = "lwu";
5753 ld:
8fc2e39e 5754 if (breg == treg || coproc || lr)
252b5132
RH
5755 {
5756 tempreg = AT;
5757 used_at = 1;
5758 }
5759 else
5760 {
5761 tempreg = treg;
252b5132
RH
5762 }
5763 goto ld_st;
5764 case M_SB_AB:
5765 s = "sb";
5766 goto st;
5767 case M_SH_AB:
5768 s = "sh";
5769 goto st;
5770 case M_SW_AB:
5771 s = "sw";
5772 goto st;
5773 case M_SWC0_AB:
5774 s = "swc0";
bdaaa2e1 5775 /* Itbl support may require additional care here. */
252b5132
RH
5776 coproc = 1;
5777 goto st;
5778 case M_SWC1_AB:
5779 s = "swc1";
bdaaa2e1 5780 /* Itbl support may require additional care here. */
252b5132
RH
5781 coproc = 1;
5782 goto st;
5783 case M_SWC2_AB:
5784 s = "swc2";
bdaaa2e1 5785 /* Itbl support may require additional care here. */
252b5132
RH
5786 coproc = 1;
5787 goto st;
5788 case M_SWC3_AB:
5789 s = "swc3";
bdaaa2e1 5790 /* Itbl support may require additional care here. */
252b5132
RH
5791 coproc = 1;
5792 goto st;
5793 case M_SWL_AB:
5794 s = "swl";
5795 goto st;
5796 case M_SWR_AB:
5797 s = "swr";
5798 goto st;
5799 case M_SC_AB:
5800 s = "sc";
5801 goto st;
5802 case M_SCD_AB:
5803 s = "scd";
5804 goto st;
5805 case M_SDC1_AB:
fef14a42 5806 if (mips_opts.arch == CPU_R4650)
252b5132
RH
5807 {
5808 as_bad (_("opcode not supported on this processor"));
8fc2e39e 5809 break;
252b5132
RH
5810 }
5811 s = "sdc1";
5812 coproc = 1;
bdaaa2e1 5813 /* Itbl support may require additional care here. */
252b5132
RH
5814 goto st;
5815 case M_SDC2_AB:
5816 s = "sdc2";
bdaaa2e1 5817 /* Itbl support may require additional care here. */
252b5132
RH
5818 coproc = 1;
5819 goto st;
5820 case M_SDC3_AB:
5821 s = "sdc3";
bdaaa2e1 5822 /* Itbl support may require additional care here. */
252b5132
RH
5823 coproc = 1;
5824 goto st;
5825 case M_SDL_AB:
5826 s = "sdl";
5827 goto st;
5828 case M_SDR_AB:
5829 s = "sdr";
5830 st:
8fc2e39e
TS
5831 tempreg = AT;
5832 used_at = 1;
252b5132 5833 ld_st:
bdaaa2e1 5834 /* Itbl support may require additional care here. */
252b5132
RH
5835 if (mask == M_LWC1_AB
5836 || mask == M_SWC1_AB
5837 || mask == M_LDC1_AB
5838 || mask == M_SDC1_AB
5839 || mask == M_L_DAB
5840 || mask == M_S_DAB)
5841 fmt = "T,o(b)";
5842 else if (coproc)
5843 fmt = "E,o(b)";
5844 else
5845 fmt = "t,o(b)";
5846
5847 if (offset_expr.X_op != O_constant
5848 && offset_expr.X_op != O_symbol)
5849 {
5850 as_bad (_("expression too complex"));
5851 offset_expr.X_op = O_constant;
5852 }
5853
2051e8c4
MR
5854 if (HAVE_32BIT_ADDRESSES
5855 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
5856 {
5857 char value [32];
5858
5859 sprintf_vma (value, offset_expr.X_add_number);
20e1fcfd 5860 as_bad (_("Number (0x%s) larger than 32 bits"), value);
55e08f71 5861 }
2051e8c4 5862
252b5132
RH
5863 /* A constant expression in PIC code can be handled just as it
5864 is in non PIC code. */
aed1a261
RS
5865 if (offset_expr.X_op == O_constant)
5866 {
aed1a261
RS
5867 expr1.X_add_number = ((offset_expr.X_add_number + 0x8000)
5868 & ~(bfd_vma) 0xffff);
2051e8c4 5869 normalize_address_expr (&expr1);
aed1a261
RS
5870 load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
5871 if (breg != 0)
5872 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5873 tempreg, tempreg, breg);
5874 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16, tempreg);
5875 }
5876 else if (mips_pic == NO_PIC)
252b5132
RH
5877 {
5878 /* If this is a reference to a GP relative symbol, and there
5879 is no base register, we want
cdf6fd85 5880 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
5881 Otherwise, if there is no base register, we want
5882 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5883 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5884 If we have a constant, we need two instructions anyhow,
5885 so we always use the latter form.
5886
5887 If we have a base register, and this is a reference to a
5888 GP relative symbol, we want
5889 addu $tempreg,$breg,$gp
cdf6fd85 5890 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
5891 Otherwise we want
5892 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5893 addu $tempreg,$tempreg,$breg
5894 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245 5895 With a constant we always use the latter case.
76b3015f 5896
d6bc6245
TS
5897 With 64bit address space and no base register and $at usable,
5898 we want
5899 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5900 lui $at,<sym> (BFD_RELOC_HI16_S)
5901 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5902 dsll32 $tempreg,0
5903 daddu $tempreg,$at
5904 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5905 If we have a base register, we want
5906 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5907 lui $at,<sym> (BFD_RELOC_HI16_S)
5908 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5909 daddu $at,$breg
5910 dsll32 $tempreg,0
5911 daddu $tempreg,$at
5912 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5913
5914 Without $at we can't generate the optimal path for superscalar
5915 processors here since this would require two temporary registers.
5916 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5917 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5918 dsll $tempreg,16
5919 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5920 dsll $tempreg,16
5921 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5922 If we have a base register, we want
5923 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5924 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5925 dsll $tempreg,16
5926 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5927 dsll $tempreg,16
5928 daddu $tempreg,$tempreg,$breg
5929 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6373ee54 5930
6caf9ef4 5931 For GP relative symbols in 64bit address space we can use
aed1a261
RS
5932 the same sequence as in 32bit address space. */
5933 if (HAVE_64BIT_SYMBOLS)
d6bc6245 5934 {
aed1a261 5935 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4
TS
5936 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
5937 {
5938 relax_start (offset_expr.X_add_symbol);
5939 if (breg == 0)
5940 {
5941 macro_build (&offset_expr, s, fmt, treg,
5942 BFD_RELOC_GPREL16, mips_gp_register);
5943 }
5944 else
5945 {
5946 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5947 tempreg, breg, mips_gp_register);
5948 macro_build (&offset_expr, s, fmt, treg,
5949 BFD_RELOC_GPREL16, tempreg);
5950 }
5951 relax_switch ();
5952 }
d6bc6245 5953
b8285c27 5954 if (used_at == 0 && !mips_opts.noat)
d6bc6245 5955 {
67c0d1eb
RS
5956 macro_build (&offset_expr, "lui", "t,u", tempreg,
5957 BFD_RELOC_MIPS_HIGHEST);
5958 macro_build (&offset_expr, "lui", "t,u", AT,
5959 BFD_RELOC_HI16_S);
5960 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
5961 tempreg, BFD_RELOC_MIPS_HIGHER);
d6bc6245 5962 if (breg != 0)
67c0d1eb
RS
5963 macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
5964 macro_build (NULL, "dsll32", "d,w,<", tempreg, tempreg, 0);
5965 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
5966 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16,
5967 tempreg);
d6bc6245
TS
5968 used_at = 1;
5969 }
5970 else
5971 {
67c0d1eb
RS
5972 macro_build (&offset_expr, "lui", "t,u", tempreg,
5973 BFD_RELOC_MIPS_HIGHEST);
5974 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
5975 tempreg, BFD_RELOC_MIPS_HIGHER);
5976 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
5977 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
5978 tempreg, BFD_RELOC_HI16_S);
5979 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
d6bc6245 5980 if (breg != 0)
67c0d1eb 5981 macro_build (NULL, "daddu", "d,v,t",
17a2f251 5982 tempreg, tempreg, breg);
67c0d1eb 5983 macro_build (&offset_expr, s, fmt, treg,
17a2f251 5984 BFD_RELOC_LO16, tempreg);
d6bc6245 5985 }
6caf9ef4
TS
5986
5987 if (mips_relax.sequence)
5988 relax_end ();
8fc2e39e 5989 break;
d6bc6245 5990 }
256ab948 5991
252b5132
RH
5992 if (breg == 0)
5993 {
67c0d1eb 5994 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 5995 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 5996 {
4d7206a2 5997 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
5998 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_GPREL16,
5999 mips_gp_register);
4d7206a2 6000 relax_switch ();
252b5132 6001 }
67c0d1eb
RS
6002 macro_build_lui (&offset_expr, tempreg);
6003 macro_build (&offset_expr, s, fmt, treg,
17a2f251 6004 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
6005 if (mips_relax.sequence)
6006 relax_end ();
252b5132
RH
6007 }
6008 else
6009 {
67c0d1eb 6010 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 6011 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 6012 {
4d7206a2 6013 relax_start (offset_expr.X_add_symbol);
67c0d1eb 6014 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6015 tempreg, breg, mips_gp_register);
67c0d1eb 6016 macro_build (&offset_expr, s, fmt, treg,
17a2f251 6017 BFD_RELOC_GPREL16, tempreg);
4d7206a2 6018 relax_switch ();
252b5132 6019 }
67c0d1eb
RS
6020 macro_build_lui (&offset_expr, tempreg);
6021 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6022 tempreg, tempreg, breg);
67c0d1eb 6023 macro_build (&offset_expr, s, fmt, treg,
17a2f251 6024 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
6025 if (mips_relax.sequence)
6026 relax_end ();
252b5132
RH
6027 }
6028 }
6029 else if (mips_pic == SVR4_PIC && ! mips_big_got)
6030 {
ed6fb7bd 6031 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
f9419b05 6032
252b5132
RH
6033 /* If this is a reference to an external symbol, we want
6034 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6035 nop
6036 <op> $treg,0($tempreg)
6037 Otherwise we want
6038 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6039 nop
6040 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6041 <op> $treg,0($tempreg)
f5040a92
AO
6042
6043 For NewABI, we want
6044 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6045 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
6046
252b5132
RH
6047 If there is a base register, we add it to $tempreg before
6048 the <op>. If there is a constant, we stick it in the
6049 <op> instruction. We don't handle constants larger than
6050 16 bits, because we have no way to load the upper 16 bits
6051 (actually, we could handle them for the subset of cases
6052 in which we are not using $at). */
6053 assert (offset_expr.X_op == O_symbol);
f5040a92
AO
6054 if (HAVE_NEWABI)
6055 {
67c0d1eb
RS
6056 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6057 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 6058 if (breg != 0)
67c0d1eb 6059 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6060 tempreg, tempreg, breg);
67c0d1eb 6061 macro_build (&offset_expr, s, fmt, treg,
17a2f251 6062 BFD_RELOC_MIPS_GOT_OFST, tempreg);
f5040a92
AO
6063 break;
6064 }
252b5132
RH
6065 expr1.X_add_number = offset_expr.X_add_number;
6066 offset_expr.X_add_number = 0;
6067 if (expr1.X_add_number < -0x8000
6068 || expr1.X_add_number >= 0x8000)
6069 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb
RS
6070 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6071 lw_reloc_type, mips_gp_register);
269137b2 6072 load_delay_nop ();
4d7206a2
RS
6073 relax_start (offset_expr.X_add_symbol);
6074 relax_switch ();
67c0d1eb
RS
6075 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
6076 tempreg, BFD_RELOC_LO16);
4d7206a2 6077 relax_end ();
252b5132 6078 if (breg != 0)
67c0d1eb 6079 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6080 tempreg, tempreg, breg);
67c0d1eb 6081 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
252b5132 6082 }
f5040a92 6083 else if (mips_pic == SVR4_PIC && ! HAVE_NEWABI)
252b5132 6084 {
67c0d1eb 6085 int gpdelay;
252b5132
RH
6086
6087 /* If this is a reference to an external symbol, we want
6088 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6089 addu $tempreg,$tempreg,$gp
6090 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6091 <op> $treg,0($tempreg)
6092 Otherwise we want
6093 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6094 nop
6095 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6096 <op> $treg,0($tempreg)
6097 If there is a base register, we add it to $tempreg before
6098 the <op>. If there is a constant, we stick it in the
6099 <op> instruction. We don't handle constants larger than
6100 16 bits, because we have no way to load the upper 16 bits
6101 (actually, we could handle them for the subset of cases
f5040a92 6102 in which we are not using $at). */
252b5132
RH
6103 assert (offset_expr.X_op == O_symbol);
6104 expr1.X_add_number = offset_expr.X_add_number;
6105 offset_expr.X_add_number = 0;
6106 if (expr1.X_add_number < -0x8000
6107 || expr1.X_add_number >= 0x8000)
6108 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 6109 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 6110 relax_start (offset_expr.X_add_symbol);
67c0d1eb 6111 macro_build (&offset_expr, "lui", "t,u", tempreg,
17a2f251 6112 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
6113 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
6114 mips_gp_register);
6115 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6116 BFD_RELOC_MIPS_GOT_LO16, tempreg);
4d7206a2 6117 relax_switch ();
67c0d1eb
RS
6118 if (gpdelay)
6119 macro_build (NULL, "nop", "");
6120 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6121 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 6122 load_delay_nop ();
67c0d1eb
RS
6123 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
6124 tempreg, BFD_RELOC_LO16);
4d7206a2
RS
6125 relax_end ();
6126
252b5132 6127 if (breg != 0)
67c0d1eb 6128 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6129 tempreg, tempreg, breg);
67c0d1eb 6130 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
252b5132 6131 }
f5040a92
AO
6132 else if (mips_pic == SVR4_PIC && HAVE_NEWABI)
6133 {
f5040a92
AO
6134 /* If this is a reference to an external symbol, we want
6135 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6136 add $tempreg,$tempreg,$gp
6137 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6138 <op> $treg,<ofst>($tempreg)
6139 Otherwise, for local symbols, we want:
6140 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6141 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
6142 assert (offset_expr.X_op == O_symbol);
4d7206a2 6143 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
6144 offset_expr.X_add_number = 0;
6145 if (expr1.X_add_number < -0x8000
6146 || expr1.X_add_number >= 0x8000)
6147 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4d7206a2 6148 relax_start (offset_expr.X_add_symbol);
67c0d1eb 6149 macro_build (&offset_expr, "lui", "t,u", tempreg,
17a2f251 6150 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
6151 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
6152 mips_gp_register);
6153 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6154 BFD_RELOC_MIPS_GOT_LO16, tempreg);
f5040a92 6155 if (breg != 0)
67c0d1eb 6156 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6157 tempreg, tempreg, breg);
67c0d1eb 6158 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
684022ea 6159
4d7206a2 6160 relax_switch ();
f5040a92 6161 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
6162 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6163 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 6164 if (breg != 0)
67c0d1eb 6165 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6166 tempreg, tempreg, breg);
67c0d1eb 6167 macro_build (&offset_expr, s, fmt, treg,
17a2f251 6168 BFD_RELOC_MIPS_GOT_OFST, tempreg);
4d7206a2 6169 relax_end ();
f5040a92 6170 }
252b5132
RH
6171 else
6172 abort ();
6173
252b5132
RH
6174 break;
6175
6176 case M_LI:
6177 case M_LI_S:
67c0d1eb 6178 load_register (treg, &imm_expr, 0);
8fc2e39e 6179 break;
252b5132
RH
6180
6181 case M_DLI:
67c0d1eb 6182 load_register (treg, &imm_expr, 1);
8fc2e39e 6183 break;
252b5132
RH
6184
6185 case M_LI_SS:
6186 if (imm_expr.X_op == O_constant)
6187 {
8fc2e39e 6188 used_at = 1;
67c0d1eb
RS
6189 load_register (AT, &imm_expr, 0);
6190 macro_build (NULL, "mtc1", "t,G", AT, treg);
252b5132
RH
6191 break;
6192 }
6193 else
6194 {
6195 assert (offset_expr.X_op == O_symbol
6196 && strcmp (segment_name (S_GET_SEGMENT
6197 (offset_expr.X_add_symbol)),
6198 ".lit4") == 0
6199 && offset_expr.X_add_number == 0);
67c0d1eb 6200 macro_build (&offset_expr, "lwc1", "T,o(b)", treg,
17a2f251 6201 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
8fc2e39e 6202 break;
252b5132
RH
6203 }
6204
6205 case M_LI_D:
ca4e0257
RS
6206 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
6207 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
6208 order 32 bits of the value and the low order 32 bits are either
6209 zero or in OFFSET_EXPR. */
252b5132
RH
6210 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6211 {
ca4e0257 6212 if (HAVE_64BIT_GPRS)
67c0d1eb 6213 load_register (treg, &imm_expr, 1);
252b5132
RH
6214 else
6215 {
6216 int hreg, lreg;
6217
6218 if (target_big_endian)
6219 {
6220 hreg = treg;
6221 lreg = treg + 1;
6222 }
6223 else
6224 {
6225 hreg = treg + 1;
6226 lreg = treg;
6227 }
6228
6229 if (hreg <= 31)
67c0d1eb 6230 load_register (hreg, &imm_expr, 0);
252b5132
RH
6231 if (lreg <= 31)
6232 {
6233 if (offset_expr.X_op == O_absent)
67c0d1eb 6234 move_register (lreg, 0);
252b5132
RH
6235 else
6236 {
6237 assert (offset_expr.X_op == O_constant);
67c0d1eb 6238 load_register (lreg, &offset_expr, 0);
252b5132
RH
6239 }
6240 }
6241 }
8fc2e39e 6242 break;
252b5132
RH
6243 }
6244
6245 /* We know that sym is in the .rdata section. First we get the
6246 upper 16 bits of the address. */
6247 if (mips_pic == NO_PIC)
6248 {
67c0d1eb 6249 macro_build_lui (&offset_expr, AT);
8fc2e39e 6250 used_at = 1;
252b5132
RH
6251 }
6252 else if (mips_pic == SVR4_PIC)
6253 {
67c0d1eb
RS
6254 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6255 BFD_RELOC_MIPS_GOT16, mips_gp_register);
8fc2e39e 6256 used_at = 1;
252b5132 6257 }
252b5132
RH
6258 else
6259 abort ();
bdaaa2e1 6260
252b5132 6261 /* Now we load the register(s). */
ca4e0257 6262 if (HAVE_64BIT_GPRS)
8fc2e39e
TS
6263 {
6264 used_at = 1;
6265 macro_build (&offset_expr, "ld", "t,o(b)", treg, BFD_RELOC_LO16, AT);
6266 }
252b5132
RH
6267 else
6268 {
8fc2e39e 6269 used_at = 1;
67c0d1eb 6270 macro_build (&offset_expr, "lw", "t,o(b)", treg, BFD_RELOC_LO16, AT);
f9419b05 6271 if (treg != RA)
252b5132
RH
6272 {
6273 /* FIXME: How in the world do we deal with the possible
6274 overflow here? */
6275 offset_expr.X_add_number += 4;
67c0d1eb 6276 macro_build (&offset_expr, "lw", "t,o(b)",
17a2f251 6277 treg + 1, BFD_RELOC_LO16, AT);
252b5132
RH
6278 }
6279 }
252b5132
RH
6280 break;
6281
6282 case M_LI_DD:
ca4e0257
RS
6283 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
6284 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
6285 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
6286 the value and the low order 32 bits are either zero or in
6287 OFFSET_EXPR. */
252b5132
RH
6288 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6289 {
8fc2e39e 6290 used_at = 1;
67c0d1eb 6291 load_register (AT, &imm_expr, HAVE_64BIT_FPRS);
ca4e0257
RS
6292 if (HAVE_64BIT_FPRS)
6293 {
6294 assert (HAVE_64BIT_GPRS);
67c0d1eb 6295 macro_build (NULL, "dmtc1", "t,S", AT, treg);
ca4e0257 6296 }
252b5132
RH
6297 else
6298 {
67c0d1eb 6299 macro_build (NULL, "mtc1", "t,G", AT, treg + 1);
252b5132 6300 if (offset_expr.X_op == O_absent)
67c0d1eb 6301 macro_build (NULL, "mtc1", "t,G", 0, treg);
252b5132
RH
6302 else
6303 {
6304 assert (offset_expr.X_op == O_constant);
67c0d1eb
RS
6305 load_register (AT, &offset_expr, 0);
6306 macro_build (NULL, "mtc1", "t,G", AT, treg);
252b5132
RH
6307 }
6308 }
6309 break;
6310 }
6311
6312 assert (offset_expr.X_op == O_symbol
6313 && offset_expr.X_add_number == 0);
6314 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
6315 if (strcmp (s, ".lit8") == 0)
6316 {
e7af610e 6317 if (mips_opts.isa != ISA_MIPS1)
252b5132 6318 {
67c0d1eb 6319 macro_build (&offset_expr, "ldc1", "T,o(b)", treg,
17a2f251 6320 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
8fc2e39e 6321 break;
252b5132 6322 }
c9914766 6323 breg = mips_gp_register;
252b5132
RH
6324 r = BFD_RELOC_MIPS_LITERAL;
6325 goto dob;
6326 }
6327 else
6328 {
6329 assert (strcmp (s, RDATA_SECTION_NAME) == 0);
8fc2e39e 6330 used_at = 1;
252b5132 6331 if (mips_pic == SVR4_PIC)
67c0d1eb
RS
6332 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6333 BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132
RH
6334 else
6335 {
6336 /* FIXME: This won't work for a 64 bit address. */
67c0d1eb 6337 macro_build_lui (&offset_expr, AT);
252b5132 6338 }
bdaaa2e1 6339
e7af610e 6340 if (mips_opts.isa != ISA_MIPS1)
252b5132 6341 {
67c0d1eb
RS
6342 macro_build (&offset_expr, "ldc1", "T,o(b)",
6343 treg, BFD_RELOC_LO16, AT);
252b5132
RH
6344 break;
6345 }
6346 breg = AT;
6347 r = BFD_RELOC_LO16;
6348 goto dob;
6349 }
6350
6351 case M_L_DOB:
fef14a42 6352 if (mips_opts.arch == CPU_R4650)
252b5132
RH
6353 {
6354 as_bad (_("opcode not supported on this processor"));
8fc2e39e 6355 break;
252b5132
RH
6356 }
6357 /* Even on a big endian machine $fn comes before $fn+1. We have
6358 to adjust when loading from memory. */
6359 r = BFD_RELOC_LO16;
6360 dob:
e7af610e 6361 assert (mips_opts.isa == ISA_MIPS1);
67c0d1eb 6362 macro_build (&offset_expr, "lwc1", "T,o(b)",
17a2f251 6363 target_big_endian ? treg + 1 : treg, r, breg);
252b5132
RH
6364 /* FIXME: A possible overflow which I don't know how to deal
6365 with. */
6366 offset_expr.X_add_number += 4;
67c0d1eb 6367 macro_build (&offset_expr, "lwc1", "T,o(b)",
17a2f251 6368 target_big_endian ? treg : treg + 1, r, breg);
252b5132
RH
6369 break;
6370
6371 case M_L_DAB:
6372 /*
6373 * The MIPS assembler seems to check for X_add_number not
6374 * being double aligned and generating:
6375 * lui at,%hi(foo+1)
6376 * addu at,at,v1
6377 * addiu at,at,%lo(foo+1)
6378 * lwc1 f2,0(at)
6379 * lwc1 f3,4(at)
6380 * But, the resulting address is the same after relocation so why
6381 * generate the extra instruction?
6382 */
fef14a42 6383 if (mips_opts.arch == CPU_R4650)
252b5132
RH
6384 {
6385 as_bad (_("opcode not supported on this processor"));
8fc2e39e 6386 break;
252b5132 6387 }
bdaaa2e1 6388 /* Itbl support may require additional care here. */
252b5132 6389 coproc = 1;
e7af610e 6390 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
6391 {
6392 s = "ldc1";
6393 goto ld;
6394 }
6395
6396 s = "lwc1";
6397 fmt = "T,o(b)";
6398 goto ldd_std;
6399
6400 case M_S_DAB:
fef14a42 6401 if (mips_opts.arch == CPU_R4650)
252b5132
RH
6402 {
6403 as_bad (_("opcode not supported on this processor"));
8fc2e39e 6404 break;
252b5132
RH
6405 }
6406
e7af610e 6407 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
6408 {
6409 s = "sdc1";
6410 goto st;
6411 }
6412
6413 s = "swc1";
6414 fmt = "T,o(b)";
bdaaa2e1 6415 /* Itbl support may require additional care here. */
252b5132
RH
6416 coproc = 1;
6417 goto ldd_std;
6418
6419 case M_LD_AB:
ca4e0257 6420 if (HAVE_64BIT_GPRS)
252b5132
RH
6421 {
6422 s = "ld";
6423 goto ld;
6424 }
6425
6426 s = "lw";
6427 fmt = "t,o(b)";
6428 goto ldd_std;
6429
6430 case M_SD_AB:
ca4e0257 6431 if (HAVE_64BIT_GPRS)
252b5132
RH
6432 {
6433 s = "sd";
6434 goto st;
6435 }
6436
6437 s = "sw";
6438 fmt = "t,o(b)";
6439
6440 ldd_std:
6441 if (offset_expr.X_op != O_symbol
6442 && offset_expr.X_op != O_constant)
6443 {
6444 as_bad (_("expression too complex"));
6445 offset_expr.X_op = O_constant;
6446 }
6447
2051e8c4
MR
6448 if (HAVE_32BIT_ADDRESSES
6449 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
6450 {
6451 char value [32];
6452
6453 sprintf_vma (value, offset_expr.X_add_number);
20e1fcfd 6454 as_bad (_("Number (0x%s) larger than 32 bits"), value);
55e08f71 6455 }
2051e8c4 6456
252b5132
RH
6457 /* Even on a big endian machine $fn comes before $fn+1. We have
6458 to adjust when loading from memory. We set coproc if we must
6459 load $fn+1 first. */
bdaaa2e1 6460 /* Itbl support may require additional care here. */
252b5132
RH
6461 if (! target_big_endian)
6462 coproc = 0;
6463
6464 if (mips_pic == NO_PIC
6465 || offset_expr.X_op == O_constant)
6466 {
6467 /* If this is a reference to a GP relative symbol, we want
cdf6fd85
TS
6468 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6469 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
6470 If we have a base register, we use this
6471 addu $at,$breg,$gp
cdf6fd85
TS
6472 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6473 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
6474 If this is not a GP relative symbol, we want
6475 lui $at,<sym> (BFD_RELOC_HI16_S)
6476 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6477 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6478 If there is a base register, we add it to $at after the
6479 lui instruction. If there is a constant, we always use
6480 the last case. */
39a59cf8
MR
6481 if (offset_expr.X_op == O_symbol
6482 && (valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 6483 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 6484 {
4d7206a2 6485 relax_start (offset_expr.X_add_symbol);
252b5132
RH
6486 if (breg == 0)
6487 {
c9914766 6488 tempreg = mips_gp_register;
252b5132
RH
6489 }
6490 else
6491 {
67c0d1eb 6492 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6493 AT, breg, mips_gp_register);
252b5132 6494 tempreg = AT;
252b5132
RH
6495 used_at = 1;
6496 }
6497
beae10d5 6498 /* Itbl support may require additional care here. */
67c0d1eb 6499 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
17a2f251 6500 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6501 offset_expr.X_add_number += 4;
6502
6503 /* Set mips_optimize to 2 to avoid inserting an
6504 undesired nop. */
6505 hold_mips_optimize = mips_optimize;
6506 mips_optimize = 2;
beae10d5 6507 /* Itbl support may require additional care here. */
67c0d1eb 6508 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
17a2f251 6509 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6510 mips_optimize = hold_mips_optimize;
6511
4d7206a2 6512 relax_switch ();
252b5132
RH
6513
6514 /* We just generated two relocs. When tc_gen_reloc
6515 handles this case, it will skip the first reloc and
6516 handle the second. The second reloc already has an
6517 extra addend of 4, which we added above. We must
6518 subtract it out, and then subtract another 4 to make
6519 the first reloc come out right. The second reloc
6520 will come out right because we are going to add 4 to
6521 offset_expr when we build its instruction below.
6522
6523 If we have a symbol, then we don't want to include
6524 the offset, because it will wind up being included
6525 when we generate the reloc. */
6526
6527 if (offset_expr.X_op == O_constant)
6528 offset_expr.X_add_number -= 8;
6529 else
6530 {
6531 offset_expr.X_add_number = -4;
6532 offset_expr.X_op = O_constant;
6533 }
6534 }
8fc2e39e 6535 used_at = 1;
67c0d1eb 6536 macro_build_lui (&offset_expr, AT);
252b5132 6537 if (breg != 0)
67c0d1eb 6538 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 6539 /* Itbl support may require additional care here. */
67c0d1eb 6540 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
17a2f251 6541 BFD_RELOC_LO16, AT);
252b5132
RH
6542 /* FIXME: How do we handle overflow here? */
6543 offset_expr.X_add_number += 4;
beae10d5 6544 /* Itbl support may require additional care here. */
67c0d1eb 6545 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
17a2f251 6546 BFD_RELOC_LO16, AT);
4d7206a2
RS
6547 if (mips_relax.sequence)
6548 relax_end ();
bdaaa2e1 6549 }
252b5132
RH
6550 else if (mips_pic == SVR4_PIC && ! mips_big_got)
6551 {
252b5132
RH
6552 /* If this is a reference to an external symbol, we want
6553 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6554 nop
6555 <op> $treg,0($at)
6556 <op> $treg+1,4($at)
6557 Otherwise we want
6558 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6559 nop
6560 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6561 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6562 If there is a base register we add it to $at before the
6563 lwc1 instructions. If there is a constant we include it
6564 in the lwc1 instructions. */
6565 used_at = 1;
6566 expr1.X_add_number = offset_expr.X_add_number;
252b5132
RH
6567 if (expr1.X_add_number < -0x8000
6568 || expr1.X_add_number >= 0x8000 - 4)
6569 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 6570 load_got_offset (AT, &offset_expr);
269137b2 6571 load_delay_nop ();
252b5132 6572 if (breg != 0)
67c0d1eb 6573 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
252b5132
RH
6574
6575 /* Set mips_optimize to 2 to avoid inserting an undesired
6576 nop. */
6577 hold_mips_optimize = mips_optimize;
6578 mips_optimize = 2;
4d7206a2 6579
beae10d5 6580 /* Itbl support may require additional care here. */
4d7206a2 6581 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
6582 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
6583 BFD_RELOC_LO16, AT);
4d7206a2 6584 expr1.X_add_number += 4;
67c0d1eb
RS
6585 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
6586 BFD_RELOC_LO16, AT);
4d7206a2 6587 relax_switch ();
67c0d1eb
RS
6588 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6589 BFD_RELOC_LO16, AT);
4d7206a2 6590 offset_expr.X_add_number += 4;
67c0d1eb
RS
6591 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6592 BFD_RELOC_LO16, AT);
4d7206a2 6593 relax_end ();
252b5132 6594
4d7206a2 6595 mips_optimize = hold_mips_optimize;
252b5132
RH
6596 }
6597 else if (mips_pic == SVR4_PIC)
6598 {
67c0d1eb 6599 int gpdelay;
252b5132
RH
6600
6601 /* If this is a reference to an external symbol, we want
6602 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6603 addu $at,$at,$gp
6604 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
6605 nop
6606 <op> $treg,0($at)
6607 <op> $treg+1,4($at)
6608 Otherwise we want
6609 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6610 nop
6611 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6612 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6613 If there is a base register we add it to $at before the
6614 lwc1 instructions. If there is a constant we include it
6615 in the lwc1 instructions. */
6616 used_at = 1;
6617 expr1.X_add_number = offset_expr.X_add_number;
6618 offset_expr.X_add_number = 0;
6619 if (expr1.X_add_number < -0x8000
6620 || expr1.X_add_number >= 0x8000 - 4)
6621 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 6622 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 6623 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
6624 macro_build (&offset_expr, "lui", "t,u",
6625 AT, BFD_RELOC_MIPS_GOT_HI16);
6626 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6627 AT, AT, mips_gp_register);
67c0d1eb 6628 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 6629 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
269137b2 6630 load_delay_nop ();
252b5132 6631 if (breg != 0)
67c0d1eb 6632 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 6633 /* Itbl support may require additional care here. */
67c0d1eb 6634 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
17a2f251 6635 BFD_RELOC_LO16, AT);
252b5132
RH
6636 expr1.X_add_number += 4;
6637
6638 /* Set mips_optimize to 2 to avoid inserting an undesired
6639 nop. */
6640 hold_mips_optimize = mips_optimize;
6641 mips_optimize = 2;
beae10d5 6642 /* Itbl support may require additional care here. */
67c0d1eb 6643 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
17a2f251 6644 BFD_RELOC_LO16, AT);
252b5132
RH
6645 mips_optimize = hold_mips_optimize;
6646 expr1.X_add_number -= 4;
6647
4d7206a2
RS
6648 relax_switch ();
6649 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
6650 if (gpdelay)
6651 macro_build (NULL, "nop", "");
6652 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6653 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 6654 load_delay_nop ();
252b5132 6655 if (breg != 0)
67c0d1eb 6656 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 6657 /* Itbl support may require additional care here. */
67c0d1eb
RS
6658 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6659 BFD_RELOC_LO16, AT);
4d7206a2 6660 offset_expr.X_add_number += 4;
252b5132
RH
6661
6662 /* Set mips_optimize to 2 to avoid inserting an undesired
6663 nop. */
6664 hold_mips_optimize = mips_optimize;
6665 mips_optimize = 2;
beae10d5 6666 /* Itbl support may require additional care here. */
67c0d1eb
RS
6667 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6668 BFD_RELOC_LO16, AT);
252b5132 6669 mips_optimize = hold_mips_optimize;
4d7206a2 6670 relax_end ();
252b5132 6671 }
252b5132
RH
6672 else
6673 abort ();
6674
252b5132
RH
6675 break;
6676
6677 case M_LD_OB:
6678 s = "lw";
6679 goto sd_ob;
6680 case M_SD_OB:
6681 s = "sw";
6682 sd_ob:
ca4e0257 6683 assert (HAVE_32BIT_ADDRESSES);
67c0d1eb 6684 macro_build (&offset_expr, s, "t,o(b)", treg, BFD_RELOC_LO16, breg);
252b5132 6685 offset_expr.X_add_number += 4;
67c0d1eb 6686 macro_build (&offset_expr, s, "t,o(b)", treg + 1, BFD_RELOC_LO16, breg);
8fc2e39e 6687 break;
252b5132
RH
6688
6689 /* New code added to support COPZ instructions.
6690 This code builds table entries out of the macros in mip_opcodes.
6691 R4000 uses interlocks to handle coproc delays.
6692 Other chips (like the R3000) require nops to be inserted for delays.
6693
f72c8c98 6694 FIXME: Currently, we require that the user handle delays.
252b5132
RH
6695 In order to fill delay slots for non-interlocked chips,
6696 we must have a way to specify delays based on the coprocessor.
6697 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
6698 What are the side-effects of the cop instruction?
6699 What cache support might we have and what are its effects?
6700 Both coprocessor & memory require delays. how long???
bdaaa2e1 6701 What registers are read/set/modified?
252b5132
RH
6702
6703 If an itbl is provided to interpret cop instructions,
bdaaa2e1 6704 this knowledge can be encoded in the itbl spec. */
252b5132
RH
6705
6706 case M_COP0:
6707 s = "c0";
6708 goto copz;
6709 case M_COP1:
6710 s = "c1";
6711 goto copz;
6712 case M_COP2:
6713 s = "c2";
6714 goto copz;
6715 case M_COP3:
6716 s = "c3";
6717 copz:
6718 /* For now we just do C (same as Cz). The parameter will be
6719 stored in insn_opcode by mips_ip. */
67c0d1eb 6720 macro_build (NULL, s, "C", ip->insn_opcode);
8fc2e39e 6721 break;
252b5132 6722
ea1fb5dc 6723 case M_MOVE:
67c0d1eb 6724 move_register (dreg, sreg);
8fc2e39e 6725 break;
ea1fb5dc 6726
252b5132
RH
6727#ifdef LOSING_COMPILER
6728 default:
6729 /* Try and see if this is a new itbl instruction.
6730 This code builds table entries out of the macros in mip_opcodes.
6731 FIXME: For now we just assemble the expression and pass it's
6732 value along as a 32-bit immediate.
bdaaa2e1 6733 We may want to have the assembler assemble this value,
252b5132
RH
6734 so that we gain the assembler's knowledge of delay slots,
6735 symbols, etc.
6736 Would it be more efficient to use mask (id) here? */
bdaaa2e1 6737 if (itbl_have_entries
252b5132 6738 && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
beae10d5 6739 {
252b5132
RH
6740 s = ip->insn_mo->name;
6741 s2 = "cop3";
6742 coproc = ITBL_DECODE_PNUM (immed_expr);;
67c0d1eb 6743 macro_build (&immed_expr, s, "C");
8fc2e39e 6744 break;
beae10d5 6745 }
252b5132 6746 macro2 (ip);
8fc2e39e 6747 break;
252b5132 6748 }
8fc2e39e
TS
6749 if (mips_opts.noat && used_at)
6750 as_bad (_("Macro used $at after \".set noat\""));
252b5132 6751}
bdaaa2e1 6752
252b5132 6753static void
17a2f251 6754macro2 (struct mips_cl_insn *ip)
252b5132
RH
6755{
6756 register int treg, sreg, dreg, breg;
6757 int tempreg;
6758 int mask;
252b5132
RH
6759 int used_at;
6760 expressionS expr1;
6761 const char *s;
6762 const char *s2;
6763 const char *fmt;
6764 int likely = 0;
6765 int dbl = 0;
6766 int coproc = 0;
6767 int lr = 0;
6768 int imm = 0;
6769 int off;
6770 offsetT maxnum;
6771 bfd_reloc_code_real_type r;
bdaaa2e1 6772
252b5132
RH
6773 treg = (ip->insn_opcode >> 16) & 0x1f;
6774 dreg = (ip->insn_opcode >> 11) & 0x1f;
6775 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
6776 mask = ip->insn_mo->mask;
bdaaa2e1 6777
252b5132
RH
6778 expr1.X_op = O_constant;
6779 expr1.X_op_symbol = NULL;
6780 expr1.X_add_symbol = NULL;
6781 expr1.X_add_number = 1;
bdaaa2e1 6782
252b5132
RH
6783 switch (mask)
6784 {
6785#endif /* LOSING_COMPILER */
6786
6787 case M_DMUL:
6788 dbl = 1;
6789 case M_MUL:
67c0d1eb
RS
6790 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", sreg, treg);
6791 macro_build (NULL, "mflo", "d", dreg);
8fc2e39e 6792 break;
252b5132
RH
6793
6794 case M_DMUL_I:
6795 dbl = 1;
6796 case M_MUL_I:
6797 /* The MIPS assembler some times generates shifts and adds. I'm
6798 not trying to be that fancy. GCC should do this for us
6799 anyway. */
8fc2e39e 6800 used_at = 1;
67c0d1eb
RS
6801 load_register (AT, &imm_expr, dbl);
6802 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, AT);
6803 macro_build (NULL, "mflo", "d", dreg);
252b5132
RH
6804 break;
6805
6806 case M_DMULO_I:
6807 dbl = 1;
6808 case M_MULO_I:
6809 imm = 1;
6810 goto do_mulo;
6811
6812 case M_DMULO:
6813 dbl = 1;
6814 case M_MULO:
6815 do_mulo:
7d10b47d 6816 start_noreorder ();
8fc2e39e 6817 used_at = 1;
252b5132 6818 if (imm)
67c0d1eb
RS
6819 load_register (AT, &imm_expr, dbl);
6820 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
6821 macro_build (NULL, "mflo", "d", dreg);
6822 macro_build (NULL, dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, RA);
6823 macro_build (NULL, "mfhi", "d", AT);
252b5132 6824 if (mips_trap)
67c0d1eb 6825 macro_build (NULL, "tne", "s,t,q", dreg, AT, 6);
252b5132
RH
6826 else
6827 {
6828 expr1.X_add_number = 8;
67c0d1eb
RS
6829 macro_build (&expr1, "beq", "s,t,p", dreg, AT);
6830 macro_build (NULL, "nop", "", 0);
6831 macro_build (NULL, "break", "c", 6);
252b5132 6832 }
7d10b47d 6833 end_noreorder ();
67c0d1eb 6834 macro_build (NULL, "mflo", "d", dreg);
252b5132
RH
6835 break;
6836
6837 case M_DMULOU_I:
6838 dbl = 1;
6839 case M_MULOU_I:
6840 imm = 1;
6841 goto do_mulou;
6842
6843 case M_DMULOU:
6844 dbl = 1;
6845 case M_MULOU:
6846 do_mulou:
7d10b47d 6847 start_noreorder ();
8fc2e39e 6848 used_at = 1;
252b5132 6849 if (imm)
67c0d1eb
RS
6850 load_register (AT, &imm_expr, dbl);
6851 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
17a2f251 6852 sreg, imm ? AT : treg);
67c0d1eb
RS
6853 macro_build (NULL, "mfhi", "d", AT);
6854 macro_build (NULL, "mflo", "d", dreg);
252b5132 6855 if (mips_trap)
67c0d1eb 6856 macro_build (NULL, "tne", "s,t,q", AT, 0, 6);
252b5132
RH
6857 else
6858 {
6859 expr1.X_add_number = 8;
67c0d1eb
RS
6860 macro_build (&expr1, "beq", "s,t,p", AT, 0);
6861 macro_build (NULL, "nop", "", 0);
6862 macro_build (NULL, "break", "c", 6);
252b5132 6863 }
7d10b47d 6864 end_noreorder ();
252b5132
RH
6865 break;
6866
771c7ce4 6867 case M_DROL:
fef14a42 6868 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097
CD
6869 {
6870 if (dreg == sreg)
6871 {
6872 tempreg = AT;
6873 used_at = 1;
6874 }
6875 else
6876 {
6877 tempreg = dreg;
82dd0097 6878 }
67c0d1eb
RS
6879 macro_build (NULL, "dnegu", "d,w", tempreg, treg);
6880 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, tempreg);
8fc2e39e 6881 break;
82dd0097 6882 }
8fc2e39e 6883 used_at = 1;
67c0d1eb
RS
6884 macro_build (NULL, "dsubu", "d,v,t", AT, 0, treg);
6885 macro_build (NULL, "dsrlv", "d,t,s", AT, sreg, AT);
6886 macro_build (NULL, "dsllv", "d,t,s", dreg, sreg, treg);
6887 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
6888 break;
6889
252b5132 6890 case M_ROL:
fef14a42 6891 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097
CD
6892 {
6893 if (dreg == sreg)
6894 {
6895 tempreg = AT;
6896 used_at = 1;
6897 }
6898 else
6899 {
6900 tempreg = dreg;
82dd0097 6901 }
67c0d1eb
RS
6902 macro_build (NULL, "negu", "d,w", tempreg, treg);
6903 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, tempreg);
8fc2e39e 6904 break;
82dd0097 6905 }
8fc2e39e 6906 used_at = 1;
67c0d1eb
RS
6907 macro_build (NULL, "subu", "d,v,t", AT, 0, treg);
6908 macro_build (NULL, "srlv", "d,t,s", AT, sreg, AT);
6909 macro_build (NULL, "sllv", "d,t,s", dreg, sreg, treg);
6910 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
6911 break;
6912
771c7ce4
TS
6913 case M_DROL_I:
6914 {
6915 unsigned int rot;
82dd0097 6916 char *l, *r;
771c7ce4
TS
6917
6918 if (imm_expr.X_op != O_constant)
82dd0097 6919 as_bad (_("Improper rotate count"));
771c7ce4 6920 rot = imm_expr.X_add_number & 0x3f;
fef14a42 6921 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
60b63b72
RS
6922 {
6923 rot = (64 - rot) & 0x3f;
6924 if (rot >= 32)
67c0d1eb 6925 macro_build (NULL, "dror32", "d,w,<", dreg, sreg, rot - 32);
60b63b72 6926 else
67c0d1eb 6927 macro_build (NULL, "dror", "d,w,<", dreg, sreg, rot);
8fc2e39e 6928 break;
60b63b72 6929 }
483fc7cd 6930 if (rot == 0)
483fc7cd 6931 {
67c0d1eb 6932 macro_build (NULL, "dsrl", "d,w,<", dreg, sreg, 0);
8fc2e39e 6933 break;
483fc7cd 6934 }
82dd0097
CD
6935 l = (rot < 0x20) ? "dsll" : "dsll32";
6936 r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
6937 rot &= 0x1f;
8fc2e39e 6938 used_at = 1;
67c0d1eb
RS
6939 macro_build (NULL, l, "d,w,<", AT, sreg, rot);
6940 macro_build (NULL, r, "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6941 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
6942 }
6943 break;
6944
252b5132 6945 case M_ROL_I:
771c7ce4
TS
6946 {
6947 unsigned int rot;
6948
6949 if (imm_expr.X_op != O_constant)
82dd0097 6950 as_bad (_("Improper rotate count"));
771c7ce4 6951 rot = imm_expr.X_add_number & 0x1f;
fef14a42 6952 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
60b63b72 6953 {
67c0d1eb 6954 macro_build (NULL, "ror", "d,w,<", dreg, sreg, (32 - rot) & 0x1f);
8fc2e39e 6955 break;
60b63b72 6956 }
483fc7cd 6957 if (rot == 0)
483fc7cd 6958 {
67c0d1eb 6959 macro_build (NULL, "srl", "d,w,<", dreg, sreg, 0);
8fc2e39e 6960 break;
483fc7cd 6961 }
8fc2e39e 6962 used_at = 1;
67c0d1eb
RS
6963 macro_build (NULL, "sll", "d,w,<", AT, sreg, rot);
6964 macro_build (NULL, "srl", "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6965 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
6966 }
6967 break;
6968
6969 case M_DROR:
fef14a42 6970 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097 6971 {
67c0d1eb 6972 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, treg);
8fc2e39e 6973 break;
82dd0097 6974 }
8fc2e39e 6975 used_at = 1;
67c0d1eb
RS
6976 macro_build (NULL, "dsubu", "d,v,t", AT, 0, treg);
6977 macro_build (NULL, "dsllv", "d,t,s", AT, sreg, AT);
6978 macro_build (NULL, "dsrlv", "d,t,s", dreg, sreg, treg);
6979 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
6980 break;
6981
6982 case M_ROR:
fef14a42 6983 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 6984 {
67c0d1eb 6985 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, treg);
8fc2e39e 6986 break;
82dd0097 6987 }
8fc2e39e 6988 used_at = 1;
67c0d1eb
RS
6989 macro_build (NULL, "subu", "d,v,t", AT, 0, treg);
6990 macro_build (NULL, "sllv", "d,t,s", AT, sreg, AT);
6991 macro_build (NULL, "srlv", "d,t,s", dreg, sreg, treg);
6992 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
6993 break;
6994
771c7ce4
TS
6995 case M_DROR_I:
6996 {
6997 unsigned int rot;
82dd0097 6998 char *l, *r;
771c7ce4
TS
6999
7000 if (imm_expr.X_op != O_constant)
82dd0097 7001 as_bad (_("Improper rotate count"));
771c7ce4 7002 rot = imm_expr.X_add_number & 0x3f;
fef14a42 7003 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097
CD
7004 {
7005 if (rot >= 32)
67c0d1eb 7006 macro_build (NULL, "dror32", "d,w,<", dreg, sreg, rot - 32);
82dd0097 7007 else
67c0d1eb 7008 macro_build (NULL, "dror", "d,w,<", dreg, sreg, rot);
8fc2e39e 7009 break;
82dd0097 7010 }
483fc7cd 7011 if (rot == 0)
483fc7cd 7012 {
67c0d1eb 7013 macro_build (NULL, "dsrl", "d,w,<", dreg, sreg, 0);
8fc2e39e 7014 break;
483fc7cd 7015 }
82dd0097
CD
7016 r = (rot < 0x20) ? "dsrl" : "dsrl32";
7017 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
7018 rot &= 0x1f;
8fc2e39e 7019 used_at = 1;
67c0d1eb
RS
7020 macro_build (NULL, r, "d,w,<", AT, sreg, rot);
7021 macro_build (NULL, l, "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7022 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
7023 }
7024 break;
7025
252b5132 7026 case M_ROR_I:
771c7ce4
TS
7027 {
7028 unsigned int rot;
7029
7030 if (imm_expr.X_op != O_constant)
82dd0097 7031 as_bad (_("Improper rotate count"));
771c7ce4 7032 rot = imm_expr.X_add_number & 0x1f;
fef14a42 7033 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 7034 {
67c0d1eb 7035 macro_build (NULL, "ror", "d,w,<", dreg, sreg, rot);
8fc2e39e 7036 break;
82dd0097 7037 }
483fc7cd 7038 if (rot == 0)
483fc7cd 7039 {
67c0d1eb 7040 macro_build (NULL, "srl", "d,w,<", dreg, sreg, 0);
8fc2e39e 7041 break;
483fc7cd 7042 }
8fc2e39e 7043 used_at = 1;
67c0d1eb
RS
7044 macro_build (NULL, "srl", "d,w,<", AT, sreg, rot);
7045 macro_build (NULL, "sll", "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7046 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4 7047 }
252b5132
RH
7048 break;
7049
7050 case M_S_DOB:
fef14a42 7051 if (mips_opts.arch == CPU_R4650)
252b5132
RH
7052 {
7053 as_bad (_("opcode not supported on this processor"));
8fc2e39e 7054 break;
252b5132 7055 }
e7af610e 7056 assert (mips_opts.isa == ISA_MIPS1);
252b5132
RH
7057 /* Even on a big endian machine $fn comes before $fn+1. We have
7058 to adjust when storing to memory. */
67c0d1eb
RS
7059 macro_build (&offset_expr, "swc1", "T,o(b)",
7060 target_big_endian ? treg + 1 : treg, BFD_RELOC_LO16, breg);
252b5132 7061 offset_expr.X_add_number += 4;
67c0d1eb
RS
7062 macro_build (&offset_expr, "swc1", "T,o(b)",
7063 target_big_endian ? treg : treg + 1, BFD_RELOC_LO16, breg);
8fc2e39e 7064 break;
252b5132
RH
7065
7066 case M_SEQ:
7067 if (sreg == 0)
67c0d1eb 7068 macro_build (&expr1, "sltiu", "t,r,j", dreg, treg, BFD_RELOC_LO16);
252b5132 7069 else if (treg == 0)
67c0d1eb 7070 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7071 else
7072 {
67c0d1eb
RS
7073 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
7074 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
252b5132 7075 }
8fc2e39e 7076 break;
252b5132
RH
7077
7078 case M_SEQ_I:
7079 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7080 {
67c0d1eb 7081 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
8fc2e39e 7082 break;
252b5132
RH
7083 }
7084 if (sreg == 0)
7085 {
7086 as_warn (_("Instruction %s: result is always false"),
7087 ip->insn_mo->name);
67c0d1eb 7088 move_register (dreg, 0);
8fc2e39e 7089 break;
252b5132
RH
7090 }
7091 if (imm_expr.X_op == O_constant
7092 && imm_expr.X_add_number >= 0
7093 && imm_expr.X_add_number < 0x10000)
7094 {
67c0d1eb 7095 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7096 }
7097 else if (imm_expr.X_op == O_constant
7098 && imm_expr.X_add_number > -0x8000
7099 && imm_expr.X_add_number < 0)
7100 {
7101 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 7102 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
17a2f251 7103 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7104 }
7105 else
7106 {
67c0d1eb
RS
7107 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7108 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
252b5132
RH
7109 used_at = 1;
7110 }
67c0d1eb 7111 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
8fc2e39e 7112 break;
252b5132
RH
7113
7114 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
7115 s = "slt";
7116 goto sge;
7117 case M_SGEU:
7118 s = "sltu";
7119 sge:
67c0d1eb
RS
7120 macro_build (NULL, s, "d,v,t", dreg, sreg, treg);
7121 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
8fc2e39e 7122 break;
252b5132
RH
7123
7124 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
7125 case M_SGEU_I:
7126 if (imm_expr.X_op == O_constant
7127 && imm_expr.X_add_number >= -0x8000
7128 && imm_expr.X_add_number < 0x8000)
7129 {
67c0d1eb
RS
7130 macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
7131 dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7132 }
7133 else
7134 {
67c0d1eb
RS
7135 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7136 macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
7137 dreg, sreg, AT);
252b5132
RH
7138 used_at = 1;
7139 }
67c0d1eb 7140 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
8fc2e39e 7141 break;
252b5132
RH
7142
7143 case M_SGT: /* sreg > treg <==> treg < sreg */
7144 s = "slt";
7145 goto sgt;
7146 case M_SGTU:
7147 s = "sltu";
7148 sgt:
67c0d1eb 7149 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
8fc2e39e 7150 break;
252b5132
RH
7151
7152 case M_SGT_I: /* sreg > I <==> I < sreg */
7153 s = "slt";
7154 goto sgti;
7155 case M_SGTU_I:
7156 s = "sltu";
7157 sgti:
8fc2e39e 7158 used_at = 1;
67c0d1eb
RS
7159 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7160 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
252b5132
RH
7161 break;
7162
2396cfb9 7163 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
252b5132
RH
7164 s = "slt";
7165 goto sle;
7166 case M_SLEU:
7167 s = "sltu";
7168 sle:
67c0d1eb
RS
7169 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
7170 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
8fc2e39e 7171 break;
252b5132 7172
2396cfb9 7173 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
252b5132
RH
7174 s = "slt";
7175 goto slei;
7176 case M_SLEU_I:
7177 s = "sltu";
7178 slei:
8fc2e39e 7179 used_at = 1;
67c0d1eb
RS
7180 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7181 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
7182 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
252b5132
RH
7183 break;
7184
7185 case M_SLT_I:
7186 if (imm_expr.X_op == O_constant
7187 && imm_expr.X_add_number >= -0x8000
7188 && imm_expr.X_add_number < 0x8000)
7189 {
67c0d1eb 7190 macro_build (&imm_expr, "slti", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
8fc2e39e 7191 break;
252b5132 7192 }
8fc2e39e 7193 used_at = 1;
67c0d1eb
RS
7194 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7195 macro_build (NULL, "slt", "d,v,t", dreg, sreg, AT);
252b5132
RH
7196 break;
7197
7198 case M_SLTU_I:
7199 if (imm_expr.X_op == O_constant
7200 && imm_expr.X_add_number >= -0x8000
7201 && imm_expr.X_add_number < 0x8000)
7202 {
67c0d1eb 7203 macro_build (&imm_expr, "sltiu", "t,r,j", dreg, sreg,
17a2f251 7204 BFD_RELOC_LO16);
8fc2e39e 7205 break;
252b5132 7206 }
8fc2e39e 7207 used_at = 1;
67c0d1eb
RS
7208 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7209 macro_build (NULL, "sltu", "d,v,t", dreg, sreg, AT);
252b5132
RH
7210 break;
7211
7212 case M_SNE:
7213 if (sreg == 0)
67c0d1eb 7214 macro_build (NULL, "sltu", "d,v,t", dreg, 0, treg);
252b5132 7215 else if (treg == 0)
67c0d1eb 7216 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
252b5132
RH
7217 else
7218 {
67c0d1eb
RS
7219 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
7220 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
252b5132 7221 }
8fc2e39e 7222 break;
252b5132
RH
7223
7224 case M_SNE_I:
7225 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7226 {
67c0d1eb 7227 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
8fc2e39e 7228 break;
252b5132
RH
7229 }
7230 if (sreg == 0)
7231 {
7232 as_warn (_("Instruction %s: result is always true"),
7233 ip->insn_mo->name);
67c0d1eb
RS
7234 macro_build (&expr1, HAVE_32BIT_GPRS ? "addiu" : "daddiu", "t,r,j",
7235 dreg, 0, BFD_RELOC_LO16);
8fc2e39e 7236 break;
252b5132
RH
7237 }
7238 if (imm_expr.X_op == O_constant
7239 && imm_expr.X_add_number >= 0
7240 && imm_expr.X_add_number < 0x10000)
7241 {
67c0d1eb 7242 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7243 }
7244 else if (imm_expr.X_op == O_constant
7245 && imm_expr.X_add_number > -0x8000
7246 && imm_expr.X_add_number < 0)
7247 {
7248 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 7249 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
17a2f251 7250 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7251 }
7252 else
7253 {
67c0d1eb
RS
7254 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7255 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
252b5132
RH
7256 used_at = 1;
7257 }
67c0d1eb 7258 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
8fc2e39e 7259 break;
252b5132
RH
7260
7261 case M_DSUB_I:
7262 dbl = 1;
7263 case M_SUB_I:
7264 if (imm_expr.X_op == O_constant
7265 && imm_expr.X_add_number > -0x8000
7266 && imm_expr.X_add_number <= 0x8000)
7267 {
7268 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb
RS
7269 macro_build (&imm_expr, dbl ? "daddi" : "addi", "t,r,j",
7270 dreg, sreg, BFD_RELOC_LO16);
8fc2e39e 7271 break;
252b5132 7272 }
8fc2e39e 7273 used_at = 1;
67c0d1eb
RS
7274 load_register (AT, &imm_expr, dbl);
7275 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
252b5132
RH
7276 break;
7277
7278 case M_DSUBU_I:
7279 dbl = 1;
7280 case M_SUBU_I:
7281 if (imm_expr.X_op == O_constant
7282 && imm_expr.X_add_number > -0x8000
7283 && imm_expr.X_add_number <= 0x8000)
7284 {
7285 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb
RS
7286 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "t,r,j",
7287 dreg, sreg, BFD_RELOC_LO16);
8fc2e39e 7288 break;
252b5132 7289 }
8fc2e39e 7290 used_at = 1;
67c0d1eb
RS
7291 load_register (AT, &imm_expr, dbl);
7292 macro_build (NULL, dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
252b5132
RH
7293 break;
7294
7295 case M_TEQ_I:
7296 s = "teq";
7297 goto trap;
7298 case M_TGE_I:
7299 s = "tge";
7300 goto trap;
7301 case M_TGEU_I:
7302 s = "tgeu";
7303 goto trap;
7304 case M_TLT_I:
7305 s = "tlt";
7306 goto trap;
7307 case M_TLTU_I:
7308 s = "tltu";
7309 goto trap;
7310 case M_TNE_I:
7311 s = "tne";
7312 trap:
8fc2e39e 7313 used_at = 1;
67c0d1eb
RS
7314 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7315 macro_build (NULL, s, "s,t", sreg, AT);
252b5132
RH
7316 break;
7317
252b5132 7318 case M_TRUNCWS:
43841e91 7319 case M_TRUNCWD:
e7af610e 7320 assert (mips_opts.isa == ISA_MIPS1);
8fc2e39e 7321 used_at = 1;
252b5132
RH
7322 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
7323 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
7324
7325 /*
7326 * Is the double cfc1 instruction a bug in the mips assembler;
7327 * or is there a reason for it?
7328 */
7d10b47d 7329 start_noreorder ();
67c0d1eb
RS
7330 macro_build (NULL, "cfc1", "t,G", treg, RA);
7331 macro_build (NULL, "cfc1", "t,G", treg, RA);
7332 macro_build (NULL, "nop", "");
252b5132 7333 expr1.X_add_number = 3;
67c0d1eb 7334 macro_build (&expr1, "ori", "t,r,i", AT, treg, BFD_RELOC_LO16);
252b5132 7335 expr1.X_add_number = 2;
67c0d1eb
RS
7336 macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
7337 macro_build (NULL, "ctc1", "t,G", AT, RA);
7338 macro_build (NULL, "nop", "");
7339 macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
7340 dreg, sreg);
7341 macro_build (NULL, "ctc1", "t,G", treg, RA);
7342 macro_build (NULL, "nop", "");
7d10b47d 7343 end_noreorder ();
252b5132
RH
7344 break;
7345
7346 case M_ULH:
7347 s = "lb";
7348 goto ulh;
7349 case M_ULHU:
7350 s = "lbu";
7351 ulh:
8fc2e39e 7352 used_at = 1;
252b5132
RH
7353 if (offset_expr.X_add_number >= 0x7fff)
7354 as_bad (_("operand overflow"));
252b5132 7355 if (! target_big_endian)
f9419b05 7356 ++offset_expr.X_add_number;
67c0d1eb 7357 macro_build (&offset_expr, s, "t,o(b)", AT, BFD_RELOC_LO16, breg);
252b5132 7358 if (! target_big_endian)
f9419b05 7359 --offset_expr.X_add_number;
252b5132 7360 else
f9419b05 7361 ++offset_expr.X_add_number;
67c0d1eb
RS
7362 macro_build (&offset_expr, "lbu", "t,o(b)", treg, BFD_RELOC_LO16, breg);
7363 macro_build (NULL, "sll", "d,w,<", AT, AT, 8);
7364 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
252b5132
RH
7365 break;
7366
7367 case M_ULD:
7368 s = "ldl";
7369 s2 = "ldr";
7370 off = 7;
7371 goto ulw;
7372 case M_ULW:
7373 s = "lwl";
7374 s2 = "lwr";
7375 off = 3;
7376 ulw:
7377 if (offset_expr.X_add_number >= 0x8000 - off)
7378 as_bad (_("operand overflow"));
af22f5b2
CD
7379 if (treg != breg)
7380 tempreg = treg;
7381 else
8fc2e39e
TS
7382 {
7383 used_at = 1;
7384 tempreg = AT;
7385 }
252b5132
RH
7386 if (! target_big_endian)
7387 offset_expr.X_add_number += off;
67c0d1eb 7388 macro_build (&offset_expr, s, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
252b5132
RH
7389 if (! target_big_endian)
7390 offset_expr.X_add_number -= off;
7391 else
7392 offset_expr.X_add_number += off;
67c0d1eb 7393 macro_build (&offset_expr, s2, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
af22f5b2
CD
7394
7395 /* If necessary, move the result in tempreg the final destination. */
7396 if (treg == tempreg)
8fc2e39e 7397 break;
af22f5b2 7398 /* Protect second load's delay slot. */
017315e4 7399 load_delay_nop ();
67c0d1eb 7400 move_register (treg, tempreg);
af22f5b2 7401 break;
252b5132
RH
7402
7403 case M_ULD_A:
7404 s = "ldl";
7405 s2 = "ldr";
7406 off = 7;
7407 goto ulwa;
7408 case M_ULW_A:
7409 s = "lwl";
7410 s2 = "lwr";
7411 off = 3;
7412 ulwa:
d6bc6245 7413 used_at = 1;
67c0d1eb 7414 load_address (AT, &offset_expr, &used_at);
252b5132 7415 if (breg != 0)
67c0d1eb 7416 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
252b5132
RH
7417 if (! target_big_endian)
7418 expr1.X_add_number = off;
7419 else
7420 expr1.X_add_number = 0;
67c0d1eb 7421 macro_build (&expr1, s, "t,o(b)", treg, BFD_RELOC_LO16, AT);
252b5132
RH
7422 if (! target_big_endian)
7423 expr1.X_add_number = 0;
7424 else
7425 expr1.X_add_number = off;
67c0d1eb 7426 macro_build (&expr1, s2, "t,o(b)", treg, BFD_RELOC_LO16, AT);
252b5132
RH
7427 break;
7428
7429 case M_ULH_A:
7430 case M_ULHU_A:
d6bc6245 7431 used_at = 1;
67c0d1eb 7432 load_address (AT, &offset_expr, &used_at);
252b5132 7433 if (breg != 0)
67c0d1eb 7434 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
252b5132
RH
7435 if (target_big_endian)
7436 expr1.X_add_number = 0;
67c0d1eb 7437 macro_build (&expr1, mask == M_ULH_A ? "lb" : "lbu", "t,o(b)",
17a2f251 7438 treg, BFD_RELOC_LO16, AT);
252b5132
RH
7439 if (target_big_endian)
7440 expr1.X_add_number = 1;
7441 else
7442 expr1.X_add_number = 0;
67c0d1eb
RS
7443 macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
7444 macro_build (NULL, "sll", "d,w,<", treg, treg, 8);
7445 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
252b5132
RH
7446 break;
7447
7448 case M_USH:
8fc2e39e 7449 used_at = 1;
252b5132
RH
7450 if (offset_expr.X_add_number >= 0x7fff)
7451 as_bad (_("operand overflow"));
7452 if (target_big_endian)
f9419b05 7453 ++offset_expr.X_add_number;
67c0d1eb
RS
7454 macro_build (&offset_expr, "sb", "t,o(b)", treg, BFD_RELOC_LO16, breg);
7455 macro_build (NULL, "srl", "d,w,<", AT, treg, 8);
252b5132 7456 if (target_big_endian)
f9419b05 7457 --offset_expr.X_add_number;
252b5132 7458 else
f9419b05 7459 ++offset_expr.X_add_number;
67c0d1eb 7460 macro_build (&offset_expr, "sb", "t,o(b)", AT, BFD_RELOC_LO16, breg);
252b5132
RH
7461 break;
7462
7463 case M_USD:
7464 s = "sdl";
7465 s2 = "sdr";
7466 off = 7;
7467 goto usw;
7468 case M_USW:
7469 s = "swl";
7470 s2 = "swr";
7471 off = 3;
7472 usw:
7473 if (offset_expr.X_add_number >= 0x8000 - off)
7474 as_bad (_("operand overflow"));
7475 if (! target_big_endian)
7476 offset_expr.X_add_number += off;
67c0d1eb 7477 macro_build (&offset_expr, s, "t,o(b)", treg, BFD_RELOC_LO16, breg);
252b5132
RH
7478 if (! target_big_endian)
7479 offset_expr.X_add_number -= off;
7480 else
7481 offset_expr.X_add_number += off;
67c0d1eb 7482 macro_build (&offset_expr, s2, "t,o(b)", treg, BFD_RELOC_LO16, breg);
8fc2e39e 7483 break;
252b5132
RH
7484
7485 case M_USD_A:
7486 s = "sdl";
7487 s2 = "sdr";
7488 off = 7;
7489 goto uswa;
7490 case M_USW_A:
7491 s = "swl";
7492 s2 = "swr";
7493 off = 3;
7494 uswa:
d6bc6245 7495 used_at = 1;
67c0d1eb 7496 load_address (AT, &offset_expr, &used_at);
252b5132 7497 if (breg != 0)
67c0d1eb 7498 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
252b5132
RH
7499 if (! target_big_endian)
7500 expr1.X_add_number = off;
7501 else
7502 expr1.X_add_number = 0;
67c0d1eb 7503 macro_build (&expr1, s, "t,o(b)", treg, BFD_RELOC_LO16, AT);
252b5132
RH
7504 if (! target_big_endian)
7505 expr1.X_add_number = 0;
7506 else
7507 expr1.X_add_number = off;
67c0d1eb 7508 macro_build (&expr1, s2, "t,o(b)", treg, BFD_RELOC_LO16, AT);
252b5132
RH
7509 break;
7510
7511 case M_USH_A:
d6bc6245 7512 used_at = 1;
67c0d1eb 7513 load_address (AT, &offset_expr, &used_at);
252b5132 7514 if (breg != 0)
67c0d1eb 7515 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
252b5132
RH
7516 if (! target_big_endian)
7517 expr1.X_add_number = 0;
67c0d1eb
RS
7518 macro_build (&expr1, "sb", "t,o(b)", treg, BFD_RELOC_LO16, AT);
7519 macro_build (NULL, "srl", "d,w,<", treg, treg, 8);
252b5132
RH
7520 if (! target_big_endian)
7521 expr1.X_add_number = 1;
7522 else
7523 expr1.X_add_number = 0;
67c0d1eb 7524 macro_build (&expr1, "sb", "t,o(b)", treg, BFD_RELOC_LO16, AT);
252b5132
RH
7525 if (! target_big_endian)
7526 expr1.X_add_number = 0;
7527 else
7528 expr1.X_add_number = 1;
67c0d1eb
RS
7529 macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
7530 macro_build (NULL, "sll", "d,w,<", treg, treg, 8);
7531 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
252b5132
RH
7532 break;
7533
7534 default:
7535 /* FIXME: Check if this is one of the itbl macros, since they
bdaaa2e1 7536 are added dynamically. */
252b5132
RH
7537 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7538 break;
7539 }
8fc2e39e
TS
7540 if (mips_opts.noat && used_at)
7541 as_bad (_("Macro used $at after \".set noat\""));
252b5132
RH
7542}
7543
7544/* Implement macros in mips16 mode. */
7545
7546static void
17a2f251 7547mips16_macro (struct mips_cl_insn *ip)
252b5132
RH
7548{
7549 int mask;
7550 int xreg, yreg, zreg, tmp;
252b5132
RH
7551 expressionS expr1;
7552 int dbl;
7553 const char *s, *s2, *s3;
7554
7555 mask = ip->insn_mo->mask;
7556
bf12938e
RS
7557 xreg = MIPS16_EXTRACT_OPERAND (RX, *ip);
7558 yreg = MIPS16_EXTRACT_OPERAND (RY, *ip);
7559 zreg = MIPS16_EXTRACT_OPERAND (RZ, *ip);
252b5132 7560
252b5132
RH
7561 expr1.X_op = O_constant;
7562 expr1.X_op_symbol = NULL;
7563 expr1.X_add_symbol = NULL;
7564 expr1.X_add_number = 1;
7565
7566 dbl = 0;
7567
7568 switch (mask)
7569 {
7570 default:
7571 internalError ();
7572
7573 case M_DDIV_3:
7574 dbl = 1;
7575 case M_DIV_3:
7576 s = "mflo";
7577 goto do_div3;
7578 case M_DREM_3:
7579 dbl = 1;
7580 case M_REM_3:
7581 s = "mfhi";
7582 do_div3:
7d10b47d 7583 start_noreorder ();
67c0d1eb 7584 macro_build (NULL, dbl ? "ddiv" : "div", "0,x,y", xreg, yreg);
252b5132 7585 expr1.X_add_number = 2;
67c0d1eb
RS
7586 macro_build (&expr1, "bnez", "x,p", yreg);
7587 macro_build (NULL, "break", "6", 7);
bdaaa2e1 7588
252b5132
RH
7589 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7590 since that causes an overflow. We should do that as well,
7591 but I don't see how to do the comparisons without a temporary
7592 register. */
7d10b47d 7593 end_noreorder ();
67c0d1eb 7594 macro_build (NULL, s, "x", zreg);
252b5132
RH
7595 break;
7596
7597 case M_DIVU_3:
7598 s = "divu";
7599 s2 = "mflo";
7600 goto do_divu3;
7601 case M_REMU_3:
7602 s = "divu";
7603 s2 = "mfhi";
7604 goto do_divu3;
7605 case M_DDIVU_3:
7606 s = "ddivu";
7607 s2 = "mflo";
7608 goto do_divu3;
7609 case M_DREMU_3:
7610 s = "ddivu";
7611 s2 = "mfhi";
7612 do_divu3:
7d10b47d 7613 start_noreorder ();
67c0d1eb 7614 macro_build (NULL, s, "0,x,y", xreg, yreg);
252b5132 7615 expr1.X_add_number = 2;
67c0d1eb
RS
7616 macro_build (&expr1, "bnez", "x,p", yreg);
7617 macro_build (NULL, "break", "6", 7);
7d10b47d 7618 end_noreorder ();
67c0d1eb 7619 macro_build (NULL, s2, "x", zreg);
252b5132
RH
7620 break;
7621
7622 case M_DMUL:
7623 dbl = 1;
7624 case M_MUL:
67c0d1eb
RS
7625 macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
7626 macro_build (NULL, "mflo", "x", zreg);
8fc2e39e 7627 break;
252b5132
RH
7628
7629 case M_DSUBU_I:
7630 dbl = 1;
7631 goto do_subu;
7632 case M_SUBU_I:
7633 do_subu:
7634 if (imm_expr.X_op != O_constant)
7635 as_bad (_("Unsupported large constant"));
7636 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 7637 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
252b5132
RH
7638 break;
7639
7640 case M_SUBU_I_2:
7641 if (imm_expr.X_op != O_constant)
7642 as_bad (_("Unsupported large constant"));
7643 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 7644 macro_build (&imm_expr, "addiu", "x,k", xreg);
252b5132
RH
7645 break;
7646
7647 case M_DSUBU_I_2:
7648 if (imm_expr.X_op != O_constant)
7649 as_bad (_("Unsupported large constant"));
7650 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 7651 macro_build (&imm_expr, "daddiu", "y,j", yreg);
252b5132
RH
7652 break;
7653
7654 case M_BEQ:
7655 s = "cmp";
7656 s2 = "bteqz";
7657 goto do_branch;
7658 case M_BNE:
7659 s = "cmp";
7660 s2 = "btnez";
7661 goto do_branch;
7662 case M_BLT:
7663 s = "slt";
7664 s2 = "btnez";
7665 goto do_branch;
7666 case M_BLTU:
7667 s = "sltu";
7668 s2 = "btnez";
7669 goto do_branch;
7670 case M_BLE:
7671 s = "slt";
7672 s2 = "bteqz";
7673 goto do_reverse_branch;
7674 case M_BLEU:
7675 s = "sltu";
7676 s2 = "bteqz";
7677 goto do_reverse_branch;
7678 case M_BGE:
7679 s = "slt";
7680 s2 = "bteqz";
7681 goto do_branch;
7682 case M_BGEU:
7683 s = "sltu";
7684 s2 = "bteqz";
7685 goto do_branch;
7686 case M_BGT:
7687 s = "slt";
7688 s2 = "btnez";
7689 goto do_reverse_branch;
7690 case M_BGTU:
7691 s = "sltu";
7692 s2 = "btnez";
7693
7694 do_reverse_branch:
7695 tmp = xreg;
7696 xreg = yreg;
7697 yreg = tmp;
7698
7699 do_branch:
67c0d1eb
RS
7700 macro_build (NULL, s, "x,y", xreg, yreg);
7701 macro_build (&offset_expr, s2, "p");
252b5132
RH
7702 break;
7703
7704 case M_BEQ_I:
7705 s = "cmpi";
7706 s2 = "bteqz";
7707 s3 = "x,U";
7708 goto do_branch_i;
7709 case M_BNE_I:
7710 s = "cmpi";
7711 s2 = "btnez";
7712 s3 = "x,U";
7713 goto do_branch_i;
7714 case M_BLT_I:
7715 s = "slti";
7716 s2 = "btnez";
7717 s3 = "x,8";
7718 goto do_branch_i;
7719 case M_BLTU_I:
7720 s = "sltiu";
7721 s2 = "btnez";
7722 s3 = "x,8";
7723 goto do_branch_i;
7724 case M_BLE_I:
7725 s = "slti";
7726 s2 = "btnez";
7727 s3 = "x,8";
7728 goto do_addone_branch_i;
7729 case M_BLEU_I:
7730 s = "sltiu";
7731 s2 = "btnez";
7732 s3 = "x,8";
7733 goto do_addone_branch_i;
7734 case M_BGE_I:
7735 s = "slti";
7736 s2 = "bteqz";
7737 s3 = "x,8";
7738 goto do_branch_i;
7739 case M_BGEU_I:
7740 s = "sltiu";
7741 s2 = "bteqz";
7742 s3 = "x,8";
7743 goto do_branch_i;
7744 case M_BGT_I:
7745 s = "slti";
7746 s2 = "bteqz";
7747 s3 = "x,8";
7748 goto do_addone_branch_i;
7749 case M_BGTU_I:
7750 s = "sltiu";
7751 s2 = "bteqz";
7752 s3 = "x,8";
7753
7754 do_addone_branch_i:
7755 if (imm_expr.X_op != O_constant)
7756 as_bad (_("Unsupported large constant"));
7757 ++imm_expr.X_add_number;
7758
7759 do_branch_i:
67c0d1eb
RS
7760 macro_build (&imm_expr, s, s3, xreg);
7761 macro_build (&offset_expr, s2, "p");
252b5132
RH
7762 break;
7763
7764 case M_ABS:
7765 expr1.X_add_number = 0;
67c0d1eb 7766 macro_build (&expr1, "slti", "x,8", yreg);
252b5132 7767 if (xreg != yreg)
67c0d1eb 7768 move_register (xreg, yreg);
252b5132 7769 expr1.X_add_number = 2;
67c0d1eb
RS
7770 macro_build (&expr1, "bteqz", "p");
7771 macro_build (NULL, "neg", "x,w", xreg, xreg);
252b5132
RH
7772 }
7773}
7774
7775/* For consistency checking, verify that all bits are specified either
7776 by the match/mask part of the instruction definition, or by the
7777 operand list. */
7778static int
17a2f251 7779validate_mips_insn (const struct mips_opcode *opc)
252b5132
RH
7780{
7781 const char *p = opc->args;
7782 char c;
7783 unsigned long used_bits = opc->mask;
7784
7785 if ((used_bits & opc->match) != opc->match)
7786 {
7787 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
7788 opc->name, opc->args);
7789 return 0;
7790 }
7791#define USE_BITS(mask,shift) (used_bits |= ((mask) << (shift)))
7792 while (*p)
7793 switch (c = *p++)
7794 {
7795 case ',': break;
7796 case '(': break;
7797 case ')': break;
af7ee8bf
CD
7798 case '+':
7799 switch (c = *p++)
7800 {
7801 case 'A': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7802 case 'B': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
7803 case 'C': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
bbcc0807
CD
7804 case 'D': USE_BITS (OP_MASK_RD, OP_SH_RD);
7805 USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
5f74bc13
CD
7806 case 'E': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7807 case 'F': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
7808 case 'G': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
7809 case 'H': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
7810 case 'I': break;
ef2e4d86
CF
7811 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7812 case 'T': USE_BITS (OP_MASK_RT, OP_SH_RT);
7813 USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
af7ee8bf
CD
7814 default:
7815 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
7816 c, opc->name, opc->args);
7817 return 0;
7818 }
7819 break;
252b5132
RH
7820 case '<': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7821 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7822 case 'A': break;
4372b673 7823 case 'B': USE_BITS (OP_MASK_CODE20, OP_SH_CODE20); break;
252b5132
RH
7824 case 'C': USE_BITS (OP_MASK_COPZ, OP_SH_COPZ); break;
7825 case 'D': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
7826 case 'E': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7827 case 'F': break;
7828 case 'G': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
156c2f8b 7829 case 'H': USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
252b5132 7830 case 'I': break;
e972090a 7831 case 'J': USE_BITS (OP_MASK_CODE19, OP_SH_CODE19); break;
af7ee8bf 7832 case 'K': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
252b5132
RH
7833 case 'L': break;
7834 case 'M': USE_BITS (OP_MASK_CCC, OP_SH_CCC); break;
7835 case 'N': USE_BITS (OP_MASK_BCC, OP_SH_BCC); break;
deec1734
CD
7836 case 'O': USE_BITS (OP_MASK_ALN, OP_SH_ALN); break;
7837 case 'Q': USE_BITS (OP_MASK_VSEL, OP_SH_VSEL);
7838 USE_BITS (OP_MASK_FT, OP_SH_FT); break;
252b5132
RH
7839 case 'R': USE_BITS (OP_MASK_FR, OP_SH_FR); break;
7840 case 'S': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7841 case 'T': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7842 case 'V': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7843 case 'W': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
deec1734
CD
7844 case 'X': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
7845 case 'Y': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7846 case 'Z': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
252b5132
RH
7847 case 'a': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break;
7848 case 'b': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7849 case 'c': USE_BITS (OP_MASK_CODE, OP_SH_CODE); break;
7850 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
7851 case 'f': break;
7852 case 'h': USE_BITS (OP_MASK_PREFX, OP_SH_PREFX); break;
7853 case 'i': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
7854 case 'j': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7855 case 'k': USE_BITS (OP_MASK_CACHE, OP_SH_CACHE); break;
7856 case 'l': break;
7857 case 'o': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7858 case 'p': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7859 case 'q': USE_BITS (OP_MASK_CODE2, OP_SH_CODE2); break;
7860 case 'r': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7861 case 's': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7862 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7863 case 'u': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
7864 case 'v': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7865 case 'w': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7866 case 'x': break;
7867 case 'z': break;
7868 case 'P': USE_BITS (OP_MASK_PERFREG, OP_SH_PERFREG); break;
4372b673
NC
7869 case 'U': USE_BITS (OP_MASK_RD, OP_SH_RD);
7870 USE_BITS (OP_MASK_RT, OP_SH_RT); break;
60b63b72
RS
7871 case 'e': USE_BITS (OP_MASK_VECBYTE, OP_SH_VECBYTE); break;
7872 case '%': USE_BITS (OP_MASK_VECALIGN, OP_SH_VECALIGN); break;
7873 case '[': break;
7874 case ']': break;
74cd071d
CF
7875 case '3': USE_BITS (OP_MASK_SA3, OP_SH_SA3); break;
7876 case '4': USE_BITS (OP_MASK_SA4, OP_SH_SA4); break;
7877 case '5': USE_BITS (OP_MASK_IMM8, OP_SH_IMM8); break;
7878 case '6': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7879 case '7': USE_BITS (OP_MASK_DSPACC, OP_SH_DSPACC); break;
7880 case '8': USE_BITS (OP_MASK_WRDSP, OP_SH_WRDSP); break;
7881 case '9': USE_BITS (OP_MASK_DSPACC_S, OP_SH_DSPACC_S);break;
7882 case '0': USE_BITS (OP_MASK_DSPSFT, OP_SH_DSPSFT); break;
7883 case '\'': USE_BITS (OP_MASK_RDDSP, OP_SH_RDDSP); break;
7884 case ':': USE_BITS (OP_MASK_DSPSFT_7, OP_SH_DSPSFT_7);break;
7885 case '@': USE_BITS (OP_MASK_IMM10, OP_SH_IMM10); break;
ef2e4d86
CF
7886 case '!': USE_BITS (OP_MASK_MT_U, OP_SH_MT_U); break;
7887 case '$': USE_BITS (OP_MASK_MT_H, OP_SH_MT_H); break;
7888 case '*': USE_BITS (OP_MASK_MTACC_T, OP_SH_MTACC_T); break;
7889 case '&': USE_BITS (OP_MASK_MTACC_D, OP_SH_MTACC_D); break;
7890 case 'g': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
252b5132
RH
7891 default:
7892 as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
7893 c, opc->name, opc->args);
7894 return 0;
7895 }
7896#undef USE_BITS
7897 if (used_bits != 0xffffffff)
7898 {
7899 as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
7900 ~used_bits & 0xffffffff, opc->name, opc->args);
7901 return 0;
7902 }
7903 return 1;
7904}
7905
7906/* This routine assembles an instruction into its binary format. As a
7907 side effect, it sets one of the global variables imm_reloc or
7908 offset_reloc to the type of relocation to do if one of the operands
7909 is an address expression. */
7910
7911static void
17a2f251 7912mips_ip (char *str, struct mips_cl_insn *ip)
252b5132
RH
7913{
7914 char *s;
7915 const char *args;
43841e91 7916 char c = 0;
252b5132
RH
7917 struct mips_opcode *insn;
7918 char *argsStart;
7919 unsigned int regno;
7920 unsigned int lastregno = 0;
af7ee8bf 7921 unsigned int lastpos = 0;
071742cf 7922 unsigned int limlo, limhi;
252b5132
RH
7923 char *s_reset;
7924 char save_c = 0;
74cd071d 7925 offsetT min_range, max_range;
252b5132
RH
7926
7927 insn_error = NULL;
7928
7929 /* If the instruction contains a '.', we first try to match an instruction
7930 including the '.'. Then we try again without the '.'. */
7931 insn = NULL;
3882b010 7932 for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
252b5132
RH
7933 continue;
7934
7935 /* If we stopped on whitespace, then replace the whitespace with null for
7936 the call to hash_find. Save the character we replaced just in case we
7937 have to re-parse the instruction. */
3882b010 7938 if (ISSPACE (*s))
252b5132
RH
7939 {
7940 save_c = *s;
7941 *s++ = '\0';
7942 }
bdaaa2e1 7943
252b5132
RH
7944 insn = (struct mips_opcode *) hash_find (op_hash, str);
7945
7946 /* If we didn't find the instruction in the opcode table, try again, but
7947 this time with just the instruction up to, but not including the
7948 first '.'. */
7949 if (insn == NULL)
7950 {
bdaaa2e1 7951 /* Restore the character we overwrite above (if any). */
252b5132
RH
7952 if (save_c)
7953 *(--s) = save_c;
7954
7955 /* Scan up to the first '.' or whitespace. */
3882b010
L
7956 for (s = str;
7957 *s != '\0' && *s != '.' && !ISSPACE (*s);
7958 ++s)
252b5132
RH
7959 continue;
7960
7961 /* If we did not find a '.', then we can quit now. */
7962 if (*s != '.')
7963 {
7964 insn_error = "unrecognized opcode";
7965 return;
7966 }
7967
7968 /* Lookup the instruction in the hash table. */
7969 *s++ = '\0';
7970 if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
7971 {
7972 insn_error = "unrecognized opcode";
7973 return;
7974 }
252b5132
RH
7975 }
7976
7977 argsStart = s;
7978 for (;;)
7979 {
b34976b6 7980 bfd_boolean ok;
252b5132
RH
7981
7982 assert (strcmp (insn->name, str) == 0);
7983
1f25f5d3
CD
7984 if (OPCODE_IS_MEMBER (insn,
7985 (mips_opts.isa
3396de36 7986 | (file_ase_mips16 ? INSN_MIPS16 : 0)
deec1734 7987 | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
74cd071d 7988 | (mips_opts.ase_dsp ? INSN_DSP : 0)
ef2e4d86 7989 | (mips_opts.ase_mt ? INSN_MT : 0)
98d3f06f 7990 | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
fef14a42 7991 mips_opts.arch))
b34976b6 7992 ok = TRUE;
bdaaa2e1 7993 else
b34976b6 7994 ok = FALSE;
bdaaa2e1 7995
252b5132
RH
7996 if (insn->pinfo != INSN_MACRO)
7997 {
fef14a42 7998 if (mips_opts.arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
b34976b6 7999 ok = FALSE;
252b5132
RH
8000 }
8001
8002 if (! ok)
8003 {
8004 if (insn + 1 < &mips_opcodes[NUMOPCODES]
8005 && strcmp (insn->name, insn[1].name) == 0)
8006 {
8007 ++insn;
8008 continue;
8009 }
252b5132 8010 else
beae10d5 8011 {
268f6bed
L
8012 if (!insn_error)
8013 {
8014 static char buf[100];
fef14a42
TS
8015 sprintf (buf,
8016 _("opcode not supported on this processor: %s (%s)"),
8017 mips_cpu_info_from_arch (mips_opts.arch)->name,
8018 mips_cpu_info_from_isa (mips_opts.isa)->name);
268f6bed
L
8019 insn_error = buf;
8020 }
8021 if (save_c)
8022 *(--s) = save_c;
2bd7f1f3 8023 return;
252b5132 8024 }
252b5132
RH
8025 }
8026
1e915849 8027 create_insn (ip, insn);
268f6bed 8028 insn_error = NULL;
252b5132
RH
8029 for (args = insn->args;; ++args)
8030 {
deec1734
CD
8031 int is_mdmx;
8032
ad8d3bb3 8033 s += strspn (s, " \t");
deec1734 8034 is_mdmx = 0;
252b5132
RH
8035 switch (*args)
8036 {
8037 case '\0': /* end of args */
8038 if (*s == '\0')
8039 return;
8040 break;
8041
74cd071d
CF
8042 case '3': /* dsp 3-bit unsigned immediate in bit 21 */
8043 my_getExpression (&imm_expr, s);
8044 check_absolute_expr (ip, &imm_expr);
8045 if (imm_expr.X_add_number & ~OP_MASK_SA3)
8046 {
8047 as_warn (_("DSP immediate not in range 0..%d (%lu)"),
8048 OP_MASK_SA3, (unsigned long) imm_expr.X_add_number);
8049 imm_expr.X_add_number &= OP_MASK_SA3;
8050 }
8051 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SA3;
8052 imm_expr.X_op = O_absent;
8053 s = expr_end;
8054 continue;
8055
8056 case '4': /* dsp 4-bit unsigned immediate in bit 21 */
8057 my_getExpression (&imm_expr, s);
8058 check_absolute_expr (ip, &imm_expr);
8059 if (imm_expr.X_add_number & ~OP_MASK_SA4)
8060 {
8061 as_warn (_("DSP immediate not in range 0..%d (%lu)"),
8062 OP_MASK_SA4, (unsigned long) imm_expr.X_add_number);
8063 imm_expr.X_add_number &= OP_MASK_SA4;
8064 }
8065 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SA4;
8066 imm_expr.X_op = O_absent;
8067 s = expr_end;
8068 continue;
8069
8070 case '5': /* dsp 8-bit unsigned immediate in bit 16 */
8071 my_getExpression (&imm_expr, s);
8072 check_absolute_expr (ip, &imm_expr);
8073 if (imm_expr.X_add_number & ~OP_MASK_IMM8)
8074 {
8075 as_warn (_("DSP immediate not in range 0..%d (%lu)"),
8076 OP_MASK_IMM8, (unsigned long) imm_expr.X_add_number);
8077 imm_expr.X_add_number &= OP_MASK_IMM8;
8078 }
8079 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_IMM8;
8080 imm_expr.X_op = O_absent;
8081 s = expr_end;
8082 continue;
8083
8084 case '6': /* dsp 5-bit unsigned immediate in bit 21 */
8085 my_getExpression (&imm_expr, s);
8086 check_absolute_expr (ip, &imm_expr);
8087 if (imm_expr.X_add_number & ~OP_MASK_RS)
8088 {
8089 as_warn (_("DSP immediate not in range 0..%d (%lu)"),
8090 OP_MASK_RS, (unsigned long) imm_expr.X_add_number);
8091 imm_expr.X_add_number &= OP_MASK_RS;
8092 }
8093 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_RS;
8094 imm_expr.X_op = O_absent;
8095 s = expr_end;
8096 continue;
8097
8098 case '7': /* four dsp accumulators in bits 11,12 */
8099 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
8100 s[3] >= '0' && s[3] <= '3')
8101 {
8102 regno = s[3] - '0';
8103 s += 4;
8104 ip->insn_opcode |= regno << OP_SH_DSPACC;
8105 continue;
8106 }
8107 else
8108 as_bad (_("Invalid dsp acc register"));
8109 break;
8110
8111 case '8': /* dsp 6-bit unsigned immediate in bit 11 */
8112 my_getExpression (&imm_expr, s);
8113 check_absolute_expr (ip, &imm_expr);
8114 if (imm_expr.X_add_number & ~OP_MASK_WRDSP)
8115 {
8116 as_warn (_("DSP immediate not in range 0..%d (%lu)"),
8117 OP_MASK_WRDSP,
8118 (unsigned long) imm_expr.X_add_number);
8119 imm_expr.X_add_number &= OP_MASK_WRDSP;
8120 }
8121 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_WRDSP;
8122 imm_expr.X_op = O_absent;
8123 s = expr_end;
8124 continue;
8125
8126 case '9': /* four dsp accumulators in bits 21,22 */
8127 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
8128 s[3] >= '0' && s[3] <= '3')
8129 {
8130 regno = s[3] - '0';
8131 s += 4;
8132 ip->insn_opcode |= regno << OP_SH_DSPACC_S;
8133 continue;
8134 }
8135 else
8136 as_bad (_("Invalid dsp acc register"));
8137 break;
8138
8139 case '0': /* dsp 6-bit signed immediate in bit 20 */
8140 my_getExpression (&imm_expr, s);
8141 check_absolute_expr (ip, &imm_expr);
8142 min_range = -((OP_MASK_DSPSFT + 1) >> 1);
8143 max_range = ((OP_MASK_DSPSFT + 1) >> 1) - 1;
8144 if (imm_expr.X_add_number < min_range ||
8145 imm_expr.X_add_number > max_range)
8146 {
8147 as_warn (_("DSP immediate not in range %ld..%ld (%ld)"),
8148 (long) min_range, (long) max_range,
8149 (long) imm_expr.X_add_number);
8150 }
8151 imm_expr.X_add_number &= OP_MASK_DSPSFT;
8152 ip->insn_opcode |= ((unsigned long) imm_expr.X_add_number
8153 << OP_SH_DSPSFT);
8154 imm_expr.X_op = O_absent;
8155 s = expr_end;
8156 continue;
8157
8158 case '\'': /* dsp 6-bit unsigned immediate in bit 16 */
8159 my_getExpression (&imm_expr, s);
8160 check_absolute_expr (ip, &imm_expr);
8161 if (imm_expr.X_add_number & ~OP_MASK_RDDSP)
8162 {
8163 as_warn (_("DSP immediate not in range 0..%d (%lu)"),
8164 OP_MASK_RDDSP,
8165 (unsigned long) imm_expr.X_add_number);
8166 imm_expr.X_add_number &= OP_MASK_RDDSP;
8167 }
8168 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_RDDSP;
8169 imm_expr.X_op = O_absent;
8170 s = expr_end;
8171 continue;
8172
8173 case ':': /* dsp 7-bit signed immediate in bit 19 */
8174 my_getExpression (&imm_expr, s);
8175 check_absolute_expr (ip, &imm_expr);
8176 min_range = -((OP_MASK_DSPSFT_7 + 1) >> 1);
8177 max_range = ((OP_MASK_DSPSFT_7 + 1) >> 1) - 1;
8178 if (imm_expr.X_add_number < min_range ||
8179 imm_expr.X_add_number > max_range)
8180 {
8181 as_warn (_("DSP immediate not in range %ld..%ld (%ld)"),
8182 (long) min_range, (long) max_range,
8183 (long) imm_expr.X_add_number);
8184 }
8185 imm_expr.X_add_number &= OP_MASK_DSPSFT_7;
8186 ip->insn_opcode |= ((unsigned long) imm_expr.X_add_number
8187 << OP_SH_DSPSFT_7);
8188 imm_expr.X_op = O_absent;
8189 s = expr_end;
8190 continue;
8191
8192 case '@': /* dsp 10-bit signed immediate in bit 16 */
8193 my_getExpression (&imm_expr, s);
8194 check_absolute_expr (ip, &imm_expr);
8195 min_range = -((OP_MASK_IMM10 + 1) >> 1);
8196 max_range = ((OP_MASK_IMM10 + 1) >> 1) - 1;
8197 if (imm_expr.X_add_number < min_range ||
8198 imm_expr.X_add_number > max_range)
8199 {
8200 as_warn (_("DSP immediate not in range %ld..%ld (%ld)"),
8201 (long) min_range, (long) max_range,
8202 (long) imm_expr.X_add_number);
8203 }
8204 imm_expr.X_add_number &= OP_MASK_IMM10;
8205 ip->insn_opcode |= ((unsigned long) imm_expr.X_add_number
8206 << OP_SH_IMM10);
8207 imm_expr.X_op = O_absent;
8208 s = expr_end;
8209 continue;
8210
ef2e4d86
CF
8211 case '!': /* mt 1-bit unsigned immediate in bit 5 */
8212 my_getExpression (&imm_expr, s);
8213 check_absolute_expr (ip, &imm_expr);
8214 if (imm_expr.X_add_number & ~OP_MASK_MT_U)
8215 {
8216 as_warn (_("MT immediate not in range 0..%d (%lu)"),
8217 OP_MASK_MT_U, (unsigned long) imm_expr.X_add_number);
8218 imm_expr.X_add_number &= OP_MASK_MT_U;
8219 }
8220 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_MT_U;
8221 imm_expr.X_op = O_absent;
8222 s = expr_end;
8223 continue;
8224
8225 case '$': /* mt 1-bit unsigned immediate in bit 4 */
8226 my_getExpression (&imm_expr, s);
8227 check_absolute_expr (ip, &imm_expr);
8228 if (imm_expr.X_add_number & ~OP_MASK_MT_H)
8229 {
8230 as_warn (_("MT immediate not in range 0..%d (%lu)"),
8231 OP_MASK_MT_H, (unsigned long) imm_expr.X_add_number);
8232 imm_expr.X_add_number &= OP_MASK_MT_H;
8233 }
8234 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_MT_H;
8235 imm_expr.X_op = O_absent;
8236 s = expr_end;
8237 continue;
8238
8239 case '*': /* four dsp accumulators in bits 18,19 */
8240 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
8241 s[3] >= '0' && s[3] <= '3')
8242 {
8243 regno = s[3] - '0';
8244 s += 4;
8245 ip->insn_opcode |= regno << OP_SH_MTACC_T;
8246 continue;
8247 }
8248 else
8249 as_bad (_("Invalid dsp/smartmips acc register"));
8250 break;
8251
8252 case '&': /* four dsp accumulators in bits 13,14 */
8253 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
8254 s[3] >= '0' && s[3] <= '3')
8255 {
8256 regno = s[3] - '0';
8257 s += 4;
8258 ip->insn_opcode |= regno << OP_SH_MTACC_D;
8259 continue;
8260 }
8261 else
8262 as_bad (_("Invalid dsp/smartmips acc register"));
8263 break;
8264
252b5132
RH
8265 case ',':
8266 if (*s++ == *args)
8267 continue;
8268 s--;
8269 switch (*++args)
8270 {
8271 case 'r':
8272 case 'v':
bf12938e 8273 INSERT_OPERAND (RS, *ip, lastregno);
252b5132
RH
8274 continue;
8275
8276 case 'w':
bf12938e 8277 INSERT_OPERAND (RT, *ip, lastregno);
38487616
TS
8278 continue;
8279
252b5132 8280 case 'W':
bf12938e 8281 INSERT_OPERAND (FT, *ip, lastregno);
252b5132
RH
8282 continue;
8283
8284 case 'V':
bf12938e 8285 INSERT_OPERAND (FS, *ip, lastregno);
252b5132
RH
8286 continue;
8287 }
8288 break;
8289
8290 case '(':
8291 /* Handle optional base register.
8292 Either the base register is omitted or
bdaaa2e1 8293 we must have a left paren. */
252b5132
RH
8294 /* This is dependent on the next operand specifier
8295 is a base register specification. */
8296 assert (args[1] == 'b' || args[1] == '5'
8297 || args[1] == '-' || args[1] == '4');
8298 if (*s == '\0')
8299 return;
8300
8301 case ')': /* these must match exactly */
60b63b72
RS
8302 case '[':
8303 case ']':
252b5132
RH
8304 if (*s++ == *args)
8305 continue;
8306 break;
8307
af7ee8bf
CD
8308 case '+': /* Opcode extension character. */
8309 switch (*++args)
8310 {
071742cf
CD
8311 case 'A': /* ins/ext position, becomes LSB. */
8312 limlo = 0;
8313 limhi = 31;
5f74bc13
CD
8314 goto do_lsb;
8315 case 'E':
8316 limlo = 32;
8317 limhi = 63;
8318 goto do_lsb;
8319do_lsb:
071742cf
CD
8320 my_getExpression (&imm_expr, s);
8321 check_absolute_expr (ip, &imm_expr);
8322 if ((unsigned long) imm_expr.X_add_number < limlo
8323 || (unsigned long) imm_expr.X_add_number > limhi)
8324 {
8325 as_bad (_("Improper position (%lu)"),
8326 (unsigned long) imm_expr.X_add_number);
8327 imm_expr.X_add_number = limlo;
8328 }
8329 lastpos = imm_expr.X_add_number;
bf12938e 8330 INSERT_OPERAND (SHAMT, *ip, imm_expr.X_add_number);
071742cf
CD
8331 imm_expr.X_op = O_absent;
8332 s = expr_end;
8333 continue;
8334
8335 case 'B': /* ins size, becomes MSB. */
8336 limlo = 1;
8337 limhi = 32;
5f74bc13
CD
8338 goto do_msb;
8339 case 'F':
8340 limlo = 33;
8341 limhi = 64;
8342 goto do_msb;
8343do_msb:
071742cf
CD
8344 my_getExpression (&imm_expr, s);
8345 check_absolute_expr (ip, &imm_expr);
8346 /* Check for negative input so that small negative numbers
8347 will not succeed incorrectly. The checks against
8348 (pos+size) transitively check "size" itself,
8349 assuming that "pos" is reasonable. */
8350 if ((long) imm_expr.X_add_number < 0
8351 || ((unsigned long) imm_expr.X_add_number
8352 + lastpos) < limlo
8353 || ((unsigned long) imm_expr.X_add_number
8354 + lastpos) > limhi)
8355 {
8356 as_bad (_("Improper insert size (%lu, position %lu)"),
8357 (unsigned long) imm_expr.X_add_number,
8358 (unsigned long) lastpos);
8359 imm_expr.X_add_number = limlo - lastpos;
8360 }
bf12938e
RS
8361 INSERT_OPERAND (INSMSB, *ip,
8362 lastpos + imm_expr.X_add_number - 1);
071742cf
CD
8363 imm_expr.X_op = O_absent;
8364 s = expr_end;
8365 continue;
8366
8367 case 'C': /* ext size, becomes MSBD. */
8368 limlo = 1;
8369 limhi = 32;
5f74bc13
CD
8370 goto do_msbd;
8371 case 'G':
8372 limlo = 33;
8373 limhi = 64;
8374 goto do_msbd;
8375 case 'H':
8376 limlo = 33;
8377 limhi = 64;
8378 goto do_msbd;
8379do_msbd:
071742cf
CD
8380 my_getExpression (&imm_expr, s);
8381 check_absolute_expr (ip, &imm_expr);
8382 /* Check for negative input so that small negative numbers
8383 will not succeed incorrectly. The checks against
8384 (pos+size) transitively check "size" itself,
8385 assuming that "pos" is reasonable. */
8386 if ((long) imm_expr.X_add_number < 0
8387 || ((unsigned long) imm_expr.X_add_number
8388 + lastpos) < limlo
8389 || ((unsigned long) imm_expr.X_add_number
8390 + lastpos) > limhi)
8391 {
8392 as_bad (_("Improper extract size (%lu, position %lu)"),
8393 (unsigned long) imm_expr.X_add_number,
8394 (unsigned long) lastpos);
8395 imm_expr.X_add_number = limlo - lastpos;
8396 }
bf12938e 8397 INSERT_OPERAND (EXTMSBD, *ip, imm_expr.X_add_number - 1);
071742cf
CD
8398 imm_expr.X_op = O_absent;
8399 s = expr_end;
8400 continue;
af7ee8bf 8401
bbcc0807
CD
8402 case 'D':
8403 /* +D is for disassembly only; never match. */
8404 break;
8405
5f74bc13
CD
8406 case 'I':
8407 /* "+I" is like "I", except that imm2_expr is used. */
8408 my_getExpression (&imm2_expr, s);
8409 if (imm2_expr.X_op != O_big
8410 && imm2_expr.X_op != O_constant)
8411 insn_error = _("absolute expression required");
9ee2a2d4
MR
8412 if (HAVE_32BIT_GPRS)
8413 normalize_constant_expr (&imm2_expr);
5f74bc13
CD
8414 s = expr_end;
8415 continue;
8416
ef2e4d86
CF
8417 case 'T': /* Coprocessor register */
8418 /* +T is for disassembly only; never match. */
8419 break;
8420
8421 case 't': /* Coprocessor register number */
8422 if (s[0] == '$' && ISDIGIT (s[1]))
8423 {
8424 ++s;
8425 regno = 0;
8426 do
8427 {
8428 regno *= 10;
8429 regno += *s - '0';
8430 ++s;
8431 }
8432 while (ISDIGIT (*s));
8433 if (regno > 31)
8434 as_bad (_("Invalid register number (%d)"), regno);
8435 else
8436 {
8437 ip->insn_opcode |= regno << OP_SH_RT;
8438 continue;
8439 }
8440 }
8441 else
8442 as_bad (_("Invalid coprocessor 0 register number"));
8443 break;
8444
af7ee8bf
CD
8445 default:
8446 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8447 *args, insn->name, insn->args);
8448 /* Further processing is fruitless. */
8449 return;
8450 }
8451 break;
8452
252b5132
RH
8453 case '<': /* must be at least one digit */
8454 /*
8455 * According to the manual, if the shift amount is greater
b6ff326e
KH
8456 * than 31 or less than 0, then the shift amount should be
8457 * mod 32. In reality the mips assembler issues an error.
252b5132
RH
8458 * We issue a warning and mask out all but the low 5 bits.
8459 */
8460 my_getExpression (&imm_expr, s);
8461 check_absolute_expr (ip, &imm_expr);
8462 if ((unsigned long) imm_expr.X_add_number > 31)
bf12938e
RS
8463 as_warn (_("Improper shift amount (%lu)"),
8464 (unsigned long) imm_expr.X_add_number);
8465 INSERT_OPERAND (SHAMT, *ip, imm_expr.X_add_number);
252b5132
RH
8466 imm_expr.X_op = O_absent;
8467 s = expr_end;
8468 continue;
8469
8470 case '>': /* shift amount minus 32 */
8471 my_getExpression (&imm_expr, s);
8472 check_absolute_expr (ip, &imm_expr);
8473 if ((unsigned long) imm_expr.X_add_number < 32
8474 || (unsigned long) imm_expr.X_add_number > 63)
8475 break;
bf12938e 8476 INSERT_OPERAND (SHAMT, *ip, imm_expr.X_add_number - 32);
252b5132
RH
8477 imm_expr.X_op = O_absent;
8478 s = expr_end;
8479 continue;
8480
252b5132
RH
8481 case 'k': /* cache code */
8482 case 'h': /* prefx code */
8483 my_getExpression (&imm_expr, s);
8484 check_absolute_expr (ip, &imm_expr);
8485 if ((unsigned long) imm_expr.X_add_number > 31)
bf12938e
RS
8486 as_warn (_("Invalid value for `%s' (%lu)"),
8487 ip->insn_mo->name,
8488 (unsigned long) imm_expr.X_add_number);
252b5132 8489 if (*args == 'k')
bf12938e 8490 INSERT_OPERAND (CACHE, *ip, imm_expr.X_add_number);
252b5132 8491 else
bf12938e 8492 INSERT_OPERAND (PREFX, *ip, imm_expr.X_add_number);
252b5132
RH
8493 imm_expr.X_op = O_absent;
8494 s = expr_end;
8495 continue;
8496
8497 case 'c': /* break code */
8498 my_getExpression (&imm_expr, s);
8499 check_absolute_expr (ip, &imm_expr);
793b27f4 8500 if ((unsigned long) imm_expr.X_add_number > 1023)
bf12938e
RS
8501 as_warn (_("Illegal break code (%lu)"),
8502 (unsigned long) imm_expr.X_add_number);
8503 INSERT_OPERAND (CODE, *ip, imm_expr.X_add_number);
252b5132
RH
8504 imm_expr.X_op = O_absent;
8505 s = expr_end;
8506 continue;
8507
8508 case 'q': /* lower break code */
8509 my_getExpression (&imm_expr, s);
8510 check_absolute_expr (ip, &imm_expr);
793b27f4 8511 if ((unsigned long) imm_expr.X_add_number > 1023)
bf12938e
RS
8512 as_warn (_("Illegal lower break code (%lu)"),
8513 (unsigned long) imm_expr.X_add_number);
8514 INSERT_OPERAND (CODE2, *ip, imm_expr.X_add_number);
252b5132
RH
8515 imm_expr.X_op = O_absent;
8516 s = expr_end;
8517 continue;
8518
4372b673 8519 case 'B': /* 20-bit syscall/break code. */
156c2f8b 8520 my_getExpression (&imm_expr, s);
156c2f8b 8521 check_absolute_expr (ip, &imm_expr);
793b27f4
TS
8522 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
8523 as_warn (_("Illegal 20-bit code (%lu)"),
8524 (unsigned long) imm_expr.X_add_number);
bf12938e 8525 INSERT_OPERAND (CODE20, *ip, imm_expr.X_add_number);
252b5132
RH
8526 imm_expr.X_op = O_absent;
8527 s = expr_end;
8528 continue;
8529
98d3f06f 8530 case 'C': /* Coprocessor code */
beae10d5 8531 my_getExpression (&imm_expr, s);
252b5132 8532 check_absolute_expr (ip, &imm_expr);
98d3f06f 8533 if ((unsigned long) imm_expr.X_add_number >= (1 << 25))
252b5132 8534 {
793b27f4
TS
8535 as_warn (_("Coproccesor code > 25 bits (%lu)"),
8536 (unsigned long) imm_expr.X_add_number);
98d3f06f 8537 imm_expr.X_add_number &= ((1 << 25) - 1);
252b5132 8538 }
beae10d5
KH
8539 ip->insn_opcode |= imm_expr.X_add_number;
8540 imm_expr.X_op = O_absent;
8541 s = expr_end;
8542 continue;
252b5132 8543
4372b673
NC
8544 case 'J': /* 19-bit wait code. */
8545 my_getExpression (&imm_expr, s);
8546 check_absolute_expr (ip, &imm_expr);
793b27f4
TS
8547 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
8548 as_warn (_("Illegal 19-bit code (%lu)"),
8549 (unsigned long) imm_expr.X_add_number);
bf12938e 8550 INSERT_OPERAND (CODE19, *ip, imm_expr.X_add_number);
4372b673
NC
8551 imm_expr.X_op = O_absent;
8552 s = expr_end;
8553 continue;
8554
252b5132 8555 case 'P': /* Performance register */
beae10d5 8556 my_getExpression (&imm_expr, s);
252b5132 8557 check_absolute_expr (ip, &imm_expr);
beae10d5 8558 if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
bf12938e
RS
8559 as_warn (_("Invalid performance register (%lu)"),
8560 (unsigned long) imm_expr.X_add_number);
8561 INSERT_OPERAND (PERFREG, *ip, imm_expr.X_add_number);
beae10d5
KH
8562 imm_expr.X_op = O_absent;
8563 s = expr_end;
8564 continue;
252b5132
RH
8565
8566 case 'b': /* base register */
8567 case 'd': /* destination register */
8568 case 's': /* source register */
8569 case 't': /* target register */
8570 case 'r': /* both target and source */
8571 case 'v': /* both dest and source */
8572 case 'w': /* both dest and target */
8573 case 'E': /* coprocessor target register */
8574 case 'G': /* coprocessor destination register */
af7ee8bf 8575 case 'K': /* 'rdhwr' destination register */
252b5132
RH
8576 case 'x': /* ignore register name */
8577 case 'z': /* must be zero register */
4372b673 8578 case 'U': /* destination register (clo/clz). */
ef2e4d86 8579 case 'g': /* coprocessor destination register */
252b5132
RH
8580 s_reset = s;
8581 if (s[0] == '$')
8582 {
3882b010 8583 if (ISDIGIT (s[1]))
252b5132
RH
8584 {
8585 ++s;
8586 regno = 0;
8587 do
8588 {
8589 regno *= 10;
8590 regno += *s - '0';
8591 ++s;
8592 }
3882b010 8593 while (ISDIGIT (*s));
252b5132
RH
8594 if (regno > 31)
8595 as_bad (_("Invalid register number (%d)"), regno);
8596 }
af7ee8bf 8597 else if (*args == 'E' || *args == 'G' || *args == 'K')
252b5132
RH
8598 goto notreg;
8599 else
8600 {
76db943d
TS
8601 if (s[1] == 'r' && s[2] == 'a')
8602 {
8603 s += 3;
8604 regno = RA;
8605 }
8606 else if (s[1] == 'f' && s[2] == 'p')
252b5132
RH
8607 {
8608 s += 3;
8609 regno = FP;
8610 }
8611 else if (s[1] == 's' && s[2] == 'p')
8612 {
8613 s += 3;
8614 regno = SP;
8615 }
8616 else if (s[1] == 'g' && s[2] == 'p')
8617 {
8618 s += 3;
8619 regno = GP;
8620 }
8621 else if (s[1] == 'a' && s[2] == 't')
8622 {
8623 s += 3;
8624 regno = AT;
8625 }
8626 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8627 {
8628 s += 4;
8629 regno = KT0;
8630 }
8631 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8632 {
8633 s += 4;
8634 regno = KT1;
8635 }
85b51719
TS
8636 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
8637 {
8638 s += 5;
8639 regno = ZERO;
8640 }
252b5132
RH
8641 else if (itbl_have_entries)
8642 {
8643 char *p, *n;
d7ba4a77 8644 unsigned long r;
252b5132 8645
d7ba4a77 8646 p = s + 1; /* advance past '$' */
252b5132
RH
8647 n = itbl_get_field (&p); /* n is name */
8648
d7ba4a77
ILT
8649 /* See if this is a register defined in an
8650 itbl entry. */
8651 if (itbl_get_reg_val (n, &r))
252b5132
RH
8652 {
8653 /* Get_field advances to the start of
8654 the next field, so we need to back
d7ba4a77 8655 rack to the end of the last field. */
bdaaa2e1 8656 if (p)
252b5132 8657 s = p - 1;
bdaaa2e1 8658 else
d7ba4a77 8659 s = strchr (s, '\0');
252b5132
RH
8660 regno = r;
8661 }
8662 else
8663 goto notreg;
beae10d5 8664 }
252b5132
RH
8665 else
8666 goto notreg;
8667 }
8668 if (regno == AT
8669 && ! mips_opts.noat
8670 && *args != 'E'
af7ee8bf
CD
8671 && *args != 'G'
8672 && *args != 'K')
252b5132
RH
8673 as_warn (_("Used $at without \".set noat\""));
8674 c = *args;
8675 if (*s == ' ')
f9419b05 8676 ++s;
252b5132
RH
8677 if (args[1] != *s)
8678 {
8679 if (c == 'r' || c == 'v' || c == 'w')
8680 {
8681 regno = lastregno;
8682 s = s_reset;
f9419b05 8683 ++args;
252b5132
RH
8684 }
8685 }
8686 /* 'z' only matches $0. */
8687 if (c == 'z' && regno != 0)
8688 break;
8689
bdaaa2e1
KH
8690 /* Now that we have assembled one operand, we use the args string
8691 * to figure out where it goes in the instruction. */
252b5132
RH
8692 switch (c)
8693 {
8694 case 'r':
8695 case 's':
8696 case 'v':
8697 case 'b':
bf12938e 8698 INSERT_OPERAND (RS, *ip, regno);
252b5132
RH
8699 break;
8700 case 'd':
8701 case 'G':
af7ee8bf 8702 case 'K':
ef2e4d86 8703 case 'g':
bf12938e 8704 INSERT_OPERAND (RD, *ip, regno);
252b5132 8705 break;
4372b673 8706 case 'U':
bf12938e
RS
8707 INSERT_OPERAND (RD, *ip, regno);
8708 INSERT_OPERAND (RT, *ip, regno);
4372b673 8709 break;
252b5132
RH
8710 case 'w':
8711 case 't':
8712 case 'E':
bf12938e 8713 INSERT_OPERAND (RT, *ip, regno);
252b5132
RH
8714 break;
8715 case 'x':
8716 /* This case exists because on the r3000 trunc
8717 expands into a macro which requires a gp
8718 register. On the r6000 or r4000 it is
8719 assembled into a single instruction which
8720 ignores the register. Thus the insn version
8721 is MIPS_ISA2 and uses 'x', and the macro
8722 version is MIPS_ISA1 and uses 't'. */
8723 break;
8724 case 'z':
8725 /* This case is for the div instruction, which
8726 acts differently if the destination argument
8727 is $0. This only matches $0, and is checked
8728 outside the switch. */
8729 break;
8730 case 'D':
8731 /* Itbl operand; not yet implemented. FIXME ?? */
8732 break;
8733 /* What about all other operands like 'i', which
8734 can be specified in the opcode table? */
8735 }
8736 lastregno = regno;
8737 continue;
8738 }
8739 notreg:
8740 switch (*args++)
8741 {
8742 case 'r':
8743 case 'v':
bf12938e 8744 INSERT_OPERAND (RS, *ip, lastregno);
252b5132
RH
8745 continue;
8746 case 'w':
bf12938e 8747 INSERT_OPERAND (RT, *ip, lastregno);
252b5132
RH
8748 continue;
8749 }
8750 break;
8751
deec1734
CD
8752 case 'O': /* MDMX alignment immediate constant. */
8753 my_getExpression (&imm_expr, s);
8754 check_absolute_expr (ip, &imm_expr);
8755 if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
bf12938e
RS
8756 as_warn ("Improper align amount (%ld), using low bits",
8757 (long) imm_expr.X_add_number);
8758 INSERT_OPERAND (ALN, *ip, imm_expr.X_add_number);
deec1734
CD
8759 imm_expr.X_op = O_absent;
8760 s = expr_end;
8761 continue;
8762
8763 case 'Q': /* MDMX vector, element sel, or const. */
8764 if (s[0] != '$')
8765 {
8766 /* MDMX Immediate. */
8767 my_getExpression (&imm_expr, s);
8768 check_absolute_expr (ip, &imm_expr);
8769 if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
bf12938e
RS
8770 as_warn (_("Invalid MDMX Immediate (%ld)"),
8771 (long) imm_expr.X_add_number);
8772 INSERT_OPERAND (FT, *ip, imm_expr.X_add_number);
deec1734
CD
8773 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8774 ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
8775 else
8776 ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
deec1734
CD
8777 imm_expr.X_op = O_absent;
8778 s = expr_end;
8779 continue;
8780 }
8781 /* Not MDMX Immediate. Fall through. */
8782 case 'X': /* MDMX destination register. */
8783 case 'Y': /* MDMX source register. */
8784 case 'Z': /* MDMX target register. */
8785 is_mdmx = 1;
252b5132
RH
8786 case 'D': /* floating point destination register */
8787 case 'S': /* floating point source register */
8788 case 'T': /* floating point target register */
8789 case 'R': /* floating point source register */
8790 case 'V':
8791 case 'W':
8792 s_reset = s;
deec1734
CD
8793 /* Accept $fN for FP and MDMX register numbers, and in
8794 addition accept $vN for MDMX register numbers. */
8795 if ((s[0] == '$' && s[1] == 'f' && ISDIGIT (s[2]))
8796 || (is_mdmx != 0 && s[0] == '$' && s[1] == 'v'
8797 && ISDIGIT (s[2])))
252b5132
RH
8798 {
8799 s += 2;
8800 regno = 0;
8801 do
8802 {
8803 regno *= 10;
8804 regno += *s - '0';
8805 ++s;
8806 }
3882b010 8807 while (ISDIGIT (*s));
252b5132
RH
8808
8809 if (regno > 31)
8810 as_bad (_("Invalid float register number (%d)"), regno);
8811
8812 if ((regno & 1) != 0
ca4e0257 8813 && HAVE_32BIT_FPRS
252b5132
RH
8814 && ! (strcmp (str, "mtc1") == 0
8815 || strcmp (str, "mfc1") == 0
8816 || strcmp (str, "lwc1") == 0
8817 || strcmp (str, "swc1") == 0
8818 || strcmp (str, "l.s") == 0
ef2e4d86
CF
8819 || strcmp (str, "s.s") == 0
8820 || strcmp (str, "mftc1") == 0
8821 || strcmp (str, "mfthc1") == 0
8822 || strcmp (str, "cftc1") == 0
8823 || strcmp (str, "mttc1") == 0
8824 || strcmp (str, "mtthc1") == 0
8825 || strcmp (str, "cttc1") == 0))
252b5132
RH
8826 as_warn (_("Float register should be even, was %d"),
8827 regno);
8828
8829 c = *args;
8830 if (*s == ' ')
f9419b05 8831 ++s;
252b5132
RH
8832 if (args[1] != *s)
8833 {
8834 if (c == 'V' || c == 'W')
8835 {
8836 regno = lastregno;
8837 s = s_reset;
f9419b05 8838 ++args;
252b5132
RH
8839 }
8840 }
8841 switch (c)
8842 {
8843 case 'D':
deec1734 8844 case 'X':
bf12938e 8845 INSERT_OPERAND (FD, *ip, regno);
252b5132
RH
8846 break;
8847 case 'V':
8848 case 'S':
deec1734 8849 case 'Y':
bf12938e 8850 INSERT_OPERAND (FS, *ip, regno);
252b5132 8851 break;
deec1734
CD
8852 case 'Q':
8853 /* This is like 'Z', but also needs to fix the MDMX
8854 vector/scalar select bits. Note that the
8855 scalar immediate case is handled above. */
8856 if (*s == '[')
8857 {
8858 int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
8859 int max_el = (is_qh ? 3 : 7);
8860 s++;
8861 my_getExpression(&imm_expr, s);
8862 check_absolute_expr (ip, &imm_expr);
8863 s = expr_end;
8864 if (imm_expr.X_add_number > max_el)
8865 as_bad(_("Bad element selector %ld"),
8866 (long) imm_expr.X_add_number);
8867 imm_expr.X_add_number &= max_el;
8868 ip->insn_opcode |= (imm_expr.X_add_number
8869 << (OP_SH_VSEL +
8870 (is_qh ? 2 : 1)));
01a3f561 8871 imm_expr.X_op = O_absent;
deec1734
CD
8872 if (*s != ']')
8873 as_warn(_("Expecting ']' found '%s'"), s);
8874 else
8875 s++;
8876 }
8877 else
8878 {
8879 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8880 ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
8881 << OP_SH_VSEL);
8882 else
8883 ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
8884 OP_SH_VSEL);
8885 }
8886 /* Fall through */
252b5132
RH
8887 case 'W':
8888 case 'T':
deec1734 8889 case 'Z':
bf12938e 8890 INSERT_OPERAND (FT, *ip, regno);
252b5132
RH
8891 break;
8892 case 'R':
bf12938e 8893 INSERT_OPERAND (FR, *ip, regno);
252b5132
RH
8894 break;
8895 }
8896 lastregno = regno;
8897 continue;
8898 }
8899
252b5132
RH
8900 switch (*args++)
8901 {
8902 case 'V':
bf12938e 8903 INSERT_OPERAND (FS, *ip, lastregno);
252b5132
RH
8904 continue;
8905 case 'W':
bf12938e 8906 INSERT_OPERAND (FT, *ip, lastregno);
252b5132
RH
8907 continue;
8908 }
8909 break;
8910
8911 case 'I':
8912 my_getExpression (&imm_expr, s);
8913 if (imm_expr.X_op != O_big
8914 && imm_expr.X_op != O_constant)
8915 insn_error = _("absolute expression required");
9ee2a2d4
MR
8916 if (HAVE_32BIT_GPRS)
8917 normalize_constant_expr (&imm_expr);
252b5132
RH
8918 s = expr_end;
8919 continue;
8920
8921 case 'A':
8922 my_getExpression (&offset_expr, s);
2051e8c4 8923 normalize_address_expr (&offset_expr);
f6688943 8924 *imm_reloc = BFD_RELOC_32;
252b5132
RH
8925 s = expr_end;
8926 continue;
8927
8928 case 'F':
8929 case 'L':
8930 case 'f':
8931 case 'l':
8932 {
8933 int f64;
ca4e0257 8934 int using_gprs;
252b5132
RH
8935 char *save_in;
8936 char *err;
8937 unsigned char temp[8];
8938 int len;
8939 unsigned int length;
8940 segT seg;
8941 subsegT subseg;
8942 char *p;
8943
8944 /* These only appear as the last operand in an
8945 instruction, and every instruction that accepts
8946 them in any variant accepts them in all variants.
8947 This means we don't have to worry about backing out
8948 any changes if the instruction does not match.
8949
8950 The difference between them is the size of the
8951 floating point constant and where it goes. For 'F'
8952 and 'L' the constant is 64 bits; for 'f' and 'l' it
8953 is 32 bits. Where the constant is placed is based
8954 on how the MIPS assembler does things:
8955 F -- .rdata
8956 L -- .lit8
8957 f -- immediate value
8958 l -- .lit4
8959
8960 The .lit4 and .lit8 sections are only used if
8961 permitted by the -G argument.
8962
ca4e0257
RS
8963 The code below needs to know whether the target register
8964 is 32 or 64 bits wide. It relies on the fact 'f' and
8965 'F' are used with GPR-based instructions and 'l' and
8966 'L' are used with FPR-based instructions. */
252b5132
RH
8967
8968 f64 = *args == 'F' || *args == 'L';
ca4e0257 8969 using_gprs = *args == 'F' || *args == 'f';
252b5132
RH
8970
8971 save_in = input_line_pointer;
8972 input_line_pointer = s;
8973 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
8974 length = len;
8975 s = input_line_pointer;
8976 input_line_pointer = save_in;
8977 if (err != NULL && *err != '\0')
8978 {
8979 as_bad (_("Bad floating point constant: %s"), err);
8980 memset (temp, '\0', sizeof temp);
8981 length = f64 ? 8 : 4;
8982 }
8983
156c2f8b 8984 assert (length == (unsigned) (f64 ? 8 : 4));
252b5132
RH
8985
8986 if (*args == 'f'
8987 || (*args == 'l'
3e722fb5 8988 && (g_switch_value < 4
252b5132
RH
8989 || (temp[0] == 0 && temp[1] == 0)
8990 || (temp[2] == 0 && temp[3] == 0))))
8991 {
8992 imm_expr.X_op = O_constant;
8993 if (! target_big_endian)
8994 imm_expr.X_add_number = bfd_getl32 (temp);
8995 else
8996 imm_expr.X_add_number = bfd_getb32 (temp);
8997 }
8998 else if (length > 4
119d663a 8999 && ! mips_disable_float_construction
ca4e0257
RS
9000 /* Constants can only be constructed in GPRs and
9001 copied to FPRs if the GPRs are at least as wide
9002 as the FPRs. Force the constant into memory if
9003 we are using 64-bit FPRs but the GPRs are only
9004 32 bits wide. */
9005 && (using_gprs
9006 || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
252b5132
RH
9007 && ((temp[0] == 0 && temp[1] == 0)
9008 || (temp[2] == 0 && temp[3] == 0))
9009 && ((temp[4] == 0 && temp[5] == 0)
9010 || (temp[6] == 0 && temp[7] == 0)))
9011 {
ca4e0257
RS
9012 /* The value is simple enough to load with a couple of
9013 instructions. If using 32-bit registers, set
9014 imm_expr to the high order 32 bits and offset_expr to
9015 the low order 32 bits. Otherwise, set imm_expr to
9016 the entire 64 bit constant. */
9017 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
252b5132
RH
9018 {
9019 imm_expr.X_op = O_constant;
9020 offset_expr.X_op = O_constant;
9021 if (! target_big_endian)
9022 {
9023 imm_expr.X_add_number = bfd_getl32 (temp + 4);
9024 offset_expr.X_add_number = bfd_getl32 (temp);
9025 }
9026 else
9027 {
9028 imm_expr.X_add_number = bfd_getb32 (temp);
9029 offset_expr.X_add_number = bfd_getb32 (temp + 4);
9030 }
9031 if (offset_expr.X_add_number == 0)
9032 offset_expr.X_op = O_absent;
9033 }
9034 else if (sizeof (imm_expr.X_add_number) > 4)
9035 {
9036 imm_expr.X_op = O_constant;
9037 if (! target_big_endian)
9038 imm_expr.X_add_number = bfd_getl64 (temp);
9039 else
9040 imm_expr.X_add_number = bfd_getb64 (temp);
9041 }
9042 else
9043 {
9044 imm_expr.X_op = O_big;
9045 imm_expr.X_add_number = 4;
9046 if (! target_big_endian)
9047 {
9048 generic_bignum[0] = bfd_getl16 (temp);
9049 generic_bignum[1] = bfd_getl16 (temp + 2);
9050 generic_bignum[2] = bfd_getl16 (temp + 4);
9051 generic_bignum[3] = bfd_getl16 (temp + 6);
9052 }
9053 else
9054 {
9055 generic_bignum[0] = bfd_getb16 (temp + 6);
9056 generic_bignum[1] = bfd_getb16 (temp + 4);
9057 generic_bignum[2] = bfd_getb16 (temp + 2);
9058 generic_bignum[3] = bfd_getb16 (temp);
9059 }
9060 }
9061 }
9062 else
9063 {
9064 const char *newname;
9065 segT new_seg;
9066
9067 /* Switch to the right section. */
9068 seg = now_seg;
9069 subseg = now_subseg;
9070 switch (*args)
9071 {
9072 default: /* unused default case avoids warnings. */
9073 case 'L':
9074 newname = RDATA_SECTION_NAME;
3e722fb5 9075 if (g_switch_value >= 8)
252b5132
RH
9076 newname = ".lit8";
9077 break;
9078 case 'F':
3e722fb5 9079 newname = RDATA_SECTION_NAME;
252b5132
RH
9080 break;
9081 case 'l':
4d0d148d 9082 assert (g_switch_value >= 4);
252b5132
RH
9083 newname = ".lit4";
9084 break;
9085 }
9086 new_seg = subseg_new (newname, (subsegT) 0);
9087 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
9088 bfd_set_section_flags (stdoutput, new_seg,
9089 (SEC_ALLOC
9090 | SEC_LOAD
9091 | SEC_READONLY
9092 | SEC_DATA));
9093 frag_align (*args == 'l' ? 2 : 3, 0, 0);
9094 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
9095 && strcmp (TARGET_OS, "elf") != 0)
9096 record_alignment (new_seg, 4);
9097 else
9098 record_alignment (new_seg, *args == 'l' ? 2 : 3);
9099 if (seg == now_seg)
9100 as_bad (_("Can't use floating point insn in this section"));
9101
9102 /* Set the argument to the current address in the
9103 section. */
9104 offset_expr.X_op = O_symbol;
9105 offset_expr.X_add_symbol =
9106 symbol_new ("L0\001", now_seg,
9107 (valueT) frag_now_fix (), frag_now);
9108 offset_expr.X_add_number = 0;
9109
9110 /* Put the floating point number into the section. */
9111 p = frag_more ((int) length);
9112 memcpy (p, temp, length);
9113
9114 /* Switch back to the original section. */
9115 subseg_set (seg, subseg);
9116 }
9117 }
9118 continue;
9119
9120 case 'i': /* 16 bit unsigned immediate */
9121 case 'j': /* 16 bit signed immediate */
f6688943 9122 *imm_reloc = BFD_RELOC_LO16;
5e0116d5 9123 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0)
252b5132
RH
9124 {
9125 int more;
5e0116d5
RS
9126 offsetT minval, maxval;
9127
9128 more = (insn + 1 < &mips_opcodes[NUMOPCODES]
9129 && strcmp (insn->name, insn[1].name) == 0);
9130
9131 /* If the expression was written as an unsigned number,
9132 only treat it as signed if there are no more
9133 alternatives. */
9134 if (more
9135 && *args == 'j'
9136 && sizeof (imm_expr.X_add_number) <= 4
9137 && imm_expr.X_op == O_constant
9138 && imm_expr.X_add_number < 0
9139 && imm_expr.X_unsigned
9140 && HAVE_64BIT_GPRS)
9141 break;
9142
9143 /* For compatibility with older assemblers, we accept
9144 0x8000-0xffff as signed 16-bit numbers when only
9145 signed numbers are allowed. */
9146 if (*args == 'i')
9147 minval = 0, maxval = 0xffff;
9148 else if (more)
9149 minval = -0x8000, maxval = 0x7fff;
252b5132 9150 else
5e0116d5
RS
9151 minval = -0x8000, maxval = 0xffff;
9152
9153 if (imm_expr.X_op != O_constant
9154 || imm_expr.X_add_number < minval
9155 || imm_expr.X_add_number > maxval)
252b5132
RH
9156 {
9157 if (more)
9158 break;
2ae7e77b
AH
9159 if (imm_expr.X_op == O_constant
9160 || imm_expr.X_op == O_big)
5e0116d5 9161 as_bad (_("expression out of range"));
252b5132
RH
9162 }
9163 }
9164 s = expr_end;
9165 continue;
9166
9167 case 'o': /* 16 bit offset */
5e0116d5
RS
9168 /* Check whether there is only a single bracketed expression
9169 left. If so, it must be the base register and the
9170 constant must be zero. */
9171 if (*s == '(' && strchr (s + 1, '(') == 0)
9172 {
9173 offset_expr.X_op = O_constant;
9174 offset_expr.X_add_number = 0;
9175 continue;
9176 }
252b5132
RH
9177
9178 /* If this value won't fit into a 16 bit offset, then go
9179 find a macro that will generate the 32 bit offset
afdbd6d0 9180 code pattern. */
5e0116d5 9181 if (my_getSmallExpression (&offset_expr, offset_reloc, s) == 0
252b5132
RH
9182 && (offset_expr.X_op != O_constant
9183 || offset_expr.X_add_number >= 0x8000
afdbd6d0 9184 || offset_expr.X_add_number < -0x8000))
252b5132
RH
9185 break;
9186
252b5132
RH
9187 s = expr_end;
9188 continue;
9189
9190 case 'p': /* pc relative offset */
0b25d3e6 9191 *offset_reloc = BFD_RELOC_16_PCREL_S2;
252b5132
RH
9192 my_getExpression (&offset_expr, s);
9193 s = expr_end;
9194 continue;
9195
9196 case 'u': /* upper 16 bits */
5e0116d5
RS
9197 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0
9198 && imm_expr.X_op == O_constant
9199 && (imm_expr.X_add_number < 0
9200 || imm_expr.X_add_number >= 0x10000))
252b5132
RH
9201 as_bad (_("lui expression not in range 0..65535"));
9202 s = expr_end;
9203 continue;
9204
9205 case 'a': /* 26 bit address */
9206 my_getExpression (&offset_expr, s);
9207 s = expr_end;
f6688943 9208 *offset_reloc = BFD_RELOC_MIPS_JMP;
252b5132
RH
9209 continue;
9210
9211 case 'N': /* 3 bit branch condition code */
9212 case 'M': /* 3 bit compare condition code */
9213 if (strncmp (s, "$fcc", 4) != 0)
9214 break;
9215 s += 4;
9216 regno = 0;
9217 do
9218 {
9219 regno *= 10;
9220 regno += *s - '0';
9221 ++s;
9222 }
3882b010 9223 while (ISDIGIT (*s));
252b5132 9224 if (regno > 7)
30c378fd
CD
9225 as_bad (_("Invalid condition code register $fcc%d"), regno);
9226 if ((strcmp(str + strlen(str) - 3, ".ps") == 0
9227 || strcmp(str + strlen(str) - 5, "any2f") == 0
9228 || strcmp(str + strlen(str) - 5, "any2t") == 0)
9229 && (regno & 1) != 0)
9230 as_warn(_("Condition code register should be even for %s, was %d"),
9231 str, regno);
9232 if ((strcmp(str + strlen(str) - 5, "any4f") == 0
9233 || strcmp(str + strlen(str) - 5, "any4t") == 0)
9234 && (regno & 3) != 0)
9235 as_warn(_("Condition code register should be 0 or 4 for %s, was %d"),
9236 str, regno);
252b5132 9237 if (*args == 'N')
bf12938e 9238 INSERT_OPERAND (BCC, *ip, regno);
252b5132 9239 else
bf12938e 9240 INSERT_OPERAND (CCC, *ip, regno);
beae10d5 9241 continue;
252b5132 9242
156c2f8b
NC
9243 case 'H':
9244 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
9245 s += 2;
3882b010 9246 if (ISDIGIT (*s))
156c2f8b
NC
9247 {
9248 c = 0;
9249 do
9250 {
9251 c *= 10;
9252 c += *s - '0';
9253 ++s;
9254 }
3882b010 9255 while (ISDIGIT (*s));
156c2f8b
NC
9256 }
9257 else
9258 c = 8; /* Invalid sel value. */
9259
9260 if (c > 7)
9261 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
9262 ip->insn_opcode |= c;
9263 continue;
9264
60b63b72
RS
9265 case 'e':
9266 /* Must be at least one digit. */
9267 my_getExpression (&imm_expr, s);
9268 check_absolute_expr (ip, &imm_expr);
9269
9270 if ((unsigned long) imm_expr.X_add_number
9271 > (unsigned long) OP_MASK_VECBYTE)
9272 {
9273 as_bad (_("bad byte vector index (%ld)"),
9274 (long) imm_expr.X_add_number);
9275 imm_expr.X_add_number = 0;
9276 }
9277
bf12938e 9278 INSERT_OPERAND (VECBYTE, *ip, imm_expr.X_add_number);
60b63b72
RS
9279 imm_expr.X_op = O_absent;
9280 s = expr_end;
9281 continue;
9282
9283 case '%':
9284 my_getExpression (&imm_expr, s);
9285 check_absolute_expr (ip, &imm_expr);
9286
9287 if ((unsigned long) imm_expr.X_add_number
9288 > (unsigned long) OP_MASK_VECALIGN)
9289 {
9290 as_bad (_("bad byte vector index (%ld)"),
9291 (long) imm_expr.X_add_number);
9292 imm_expr.X_add_number = 0;
9293 }
9294
bf12938e 9295 INSERT_OPERAND (VECALIGN, *ip, imm_expr.X_add_number);
60b63b72
RS
9296 imm_expr.X_op = O_absent;
9297 s = expr_end;
9298 continue;
9299
252b5132
RH
9300 default:
9301 as_bad (_("bad char = '%c'\n"), *args);
9302 internalError ();
9303 }
9304 break;
9305 }
9306 /* Args don't match. */
9307 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
9308 !strcmp (insn->name, insn[1].name))
9309 {
9310 ++insn;
9311 s = argsStart;
268f6bed 9312 insn_error = _("illegal operands");
252b5132
RH
9313 continue;
9314 }
268f6bed
L
9315 if (save_c)
9316 *(--s) = save_c;
252b5132
RH
9317 insn_error = _("illegal operands");
9318 return;
9319 }
9320}
9321
0499d65b
TS
9322#define SKIP_SPACE_TABS(S) { while (*(S) == ' ' || *(S) == '\t') ++(S); }
9323
252b5132
RH
9324/* This routine assembles an instruction into its binary format when
9325 assembling for the mips16. As a side effect, it sets one of the
9326 global variables imm_reloc or offset_reloc to the type of
9327 relocation to do if one of the operands is an address expression.
9328 It also sets mips16_small and mips16_ext if the user explicitly
9329 requested a small or extended instruction. */
9330
9331static void
17a2f251 9332mips16_ip (char *str, struct mips_cl_insn *ip)
252b5132
RH
9333{
9334 char *s;
9335 const char *args;
9336 struct mips_opcode *insn;
9337 char *argsstart;
9338 unsigned int regno;
9339 unsigned int lastregno = 0;
9340 char *s_reset;
d6f16593 9341 size_t i;
252b5132
RH
9342
9343 insn_error = NULL;
9344
b34976b6
AM
9345 mips16_small = FALSE;
9346 mips16_ext = FALSE;
252b5132 9347
3882b010 9348 for (s = str; ISLOWER (*s); ++s)
252b5132
RH
9349 ;
9350 switch (*s)
9351 {
9352 case '\0':
9353 break;
9354
9355 case ' ':
9356 *s++ = '\0';
9357 break;
9358
9359 case '.':
9360 if (s[1] == 't' && s[2] == ' ')
9361 {
9362 *s = '\0';
b34976b6 9363 mips16_small = TRUE;
252b5132
RH
9364 s += 3;
9365 break;
9366 }
9367 else if (s[1] == 'e' && s[2] == ' ')
9368 {
9369 *s = '\0';
b34976b6 9370 mips16_ext = TRUE;
252b5132
RH
9371 s += 3;
9372 break;
9373 }
9374 /* Fall through. */
9375 default:
9376 insn_error = _("unknown opcode");
9377 return;
9378 }
9379
9380 if (mips_opts.noautoextend && ! mips16_ext)
b34976b6 9381 mips16_small = TRUE;
252b5132
RH
9382
9383 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
9384 {
9385 insn_error = _("unrecognized opcode");
9386 return;
9387 }
9388
9389 argsstart = s;
9390 for (;;)
9391 {
9392 assert (strcmp (insn->name, str) == 0);
9393
1e915849 9394 create_insn (ip, insn);
252b5132 9395 imm_expr.X_op = O_absent;
f6688943
TS
9396 imm_reloc[0] = BFD_RELOC_UNUSED;
9397 imm_reloc[1] = BFD_RELOC_UNUSED;
9398 imm_reloc[2] = BFD_RELOC_UNUSED;
5f74bc13 9399 imm2_expr.X_op = O_absent;
252b5132 9400 offset_expr.X_op = O_absent;
f6688943
TS
9401 offset_reloc[0] = BFD_RELOC_UNUSED;
9402 offset_reloc[1] = BFD_RELOC_UNUSED;
9403 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
9404 for (args = insn->args; 1; ++args)
9405 {
9406 int c;
9407
9408 if (*s == ' ')
9409 ++s;
9410
9411 /* In this switch statement we call break if we did not find
9412 a match, continue if we did find a match, or return if we
9413 are done. */
9414
9415 c = *args;
9416 switch (c)
9417 {
9418 case '\0':
9419 if (*s == '\0')
9420 {
9421 /* Stuff the immediate value in now, if we can. */
9422 if (imm_expr.X_op == O_constant
f6688943 9423 && *imm_reloc > BFD_RELOC_UNUSED
252b5132
RH
9424 && insn->pinfo != INSN_MACRO)
9425 {
d6f16593
MR
9426 valueT tmp;
9427
9428 switch (*offset_reloc)
9429 {
9430 case BFD_RELOC_MIPS16_HI16_S:
9431 tmp = (imm_expr.X_add_number + 0x8000) >> 16;
9432 break;
9433
9434 case BFD_RELOC_MIPS16_HI16:
9435 tmp = imm_expr.X_add_number >> 16;
9436 break;
9437
9438 case BFD_RELOC_MIPS16_LO16:
9439 tmp = ((imm_expr.X_add_number + 0x8000) & 0xffff)
9440 - 0x8000;
9441 break;
9442
9443 case BFD_RELOC_UNUSED:
9444 tmp = imm_expr.X_add_number;
9445 break;
9446
9447 default:
9448 internalError ();
9449 }
9450 *offset_reloc = BFD_RELOC_UNUSED;
9451
c4e7957c 9452 mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
d6f16593 9453 tmp, TRUE, mips16_small,
252b5132
RH
9454 mips16_ext, &ip->insn_opcode,
9455 &ip->use_extend, &ip->extend);
9456 imm_expr.X_op = O_absent;
f6688943 9457 *imm_reloc = BFD_RELOC_UNUSED;
252b5132
RH
9458 }
9459
9460 return;
9461 }
9462 break;
9463
9464 case ',':
9465 if (*s++ == c)
9466 continue;
9467 s--;
9468 switch (*++args)
9469 {
9470 case 'v':
bf12938e 9471 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
252b5132
RH
9472 continue;
9473 case 'w':
bf12938e 9474 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
252b5132
RH
9475 continue;
9476 }
9477 break;
9478
9479 case '(':
9480 case ')':
9481 if (*s++ == c)
9482 continue;
9483 break;
9484
9485 case 'v':
9486 case 'w':
9487 if (s[0] != '$')
9488 {
9489 if (c == 'v')
bf12938e 9490 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
252b5132 9491 else
bf12938e 9492 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
252b5132
RH
9493 ++args;
9494 continue;
9495 }
9496 /* Fall through. */
9497 case 'x':
9498 case 'y':
9499 case 'z':
9500 case 'Z':
9501 case '0':
9502 case 'S':
9503 case 'R':
9504 case 'X':
9505 case 'Y':
9506 if (s[0] != '$')
9507 break;
9508 s_reset = s;
3882b010 9509 if (ISDIGIT (s[1]))
252b5132
RH
9510 {
9511 ++s;
9512 regno = 0;
9513 do
9514 {
9515 regno *= 10;
9516 regno += *s - '0';
9517 ++s;
9518 }
3882b010 9519 while (ISDIGIT (*s));
252b5132
RH
9520 if (regno > 31)
9521 {
9522 as_bad (_("invalid register number (%d)"), regno);
9523 regno = 2;
9524 }
9525 }
9526 else
9527 {
76db943d
TS
9528 if (s[1] == 'r' && s[2] == 'a')
9529 {
9530 s += 3;
9531 regno = RA;
9532 }
9533 else if (s[1] == 'f' && s[2] == 'p')
252b5132
RH
9534 {
9535 s += 3;
9536 regno = FP;
9537 }
9538 else if (s[1] == 's' && s[2] == 'p')
9539 {
9540 s += 3;
9541 regno = SP;
9542 }
9543 else if (s[1] == 'g' && s[2] == 'p')
9544 {
9545 s += 3;
9546 regno = GP;
9547 }
9548 else if (s[1] == 'a' && s[2] == 't')
9549 {
9550 s += 3;
9551 regno = AT;
9552 }
9553 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
9554 {
9555 s += 4;
9556 regno = KT0;
9557 }
9558 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
9559 {
9560 s += 4;
9561 regno = KT1;
9562 }
85b51719
TS
9563 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
9564 {
9565 s += 5;
9566 regno = ZERO;
9567 }
252b5132
RH
9568 else
9569 break;
9570 }
9571
9572 if (*s == ' ')
9573 ++s;
9574 if (args[1] != *s)
9575 {
9576 if (c == 'v' || c == 'w')
9577 {
9578 regno = mips16_to_32_reg_map[lastregno];
9579 s = s_reset;
f9419b05 9580 ++args;
252b5132
RH
9581 }
9582 }
9583
9584 switch (c)
9585 {
9586 case 'x':
9587 case 'y':
9588 case 'z':
9589 case 'v':
9590 case 'w':
9591 case 'Z':
9592 regno = mips32_to_16_reg_map[regno];
9593 break;
9594
9595 case '0':
9596 if (regno != 0)
9597 regno = ILLEGAL_REG;
9598 break;
9599
9600 case 'S':
9601 if (regno != SP)
9602 regno = ILLEGAL_REG;
9603 break;
9604
9605 case 'R':
9606 if (regno != RA)
9607 regno = ILLEGAL_REG;
9608 break;
9609
9610 case 'X':
9611 case 'Y':
9612 if (regno == AT && ! mips_opts.noat)
9613 as_warn (_("used $at without \".set noat\""));
9614 break;
9615
9616 default:
9617 internalError ();
9618 }
9619
9620 if (regno == ILLEGAL_REG)
9621 break;
9622
9623 switch (c)
9624 {
9625 case 'x':
9626 case 'v':
bf12938e 9627 MIPS16_INSERT_OPERAND (RX, *ip, regno);
252b5132
RH
9628 break;
9629 case 'y':
9630 case 'w':
bf12938e 9631 MIPS16_INSERT_OPERAND (RY, *ip, regno);
252b5132
RH
9632 break;
9633 case 'z':
bf12938e 9634 MIPS16_INSERT_OPERAND (RZ, *ip, regno);
252b5132
RH
9635 break;
9636 case 'Z':
bf12938e 9637 MIPS16_INSERT_OPERAND (MOVE32Z, *ip, regno);
252b5132
RH
9638 case '0':
9639 case 'S':
9640 case 'R':
9641 break;
9642 case 'X':
bf12938e 9643 MIPS16_INSERT_OPERAND (REGR32, *ip, regno);
252b5132
RH
9644 break;
9645 case 'Y':
9646 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
bf12938e 9647 MIPS16_INSERT_OPERAND (REG32R, *ip, regno);
252b5132
RH
9648 break;
9649 default:
9650 internalError ();
9651 }
9652
9653 lastregno = regno;
9654 continue;
9655
9656 case 'P':
9657 if (strncmp (s, "$pc", 3) == 0)
9658 {
9659 s += 3;
9660 continue;
9661 }
9662 break;
9663
252b5132
RH
9664 case '5':
9665 case 'H':
9666 case 'W':
9667 case 'D':
9668 case 'j':
252b5132
RH
9669 case 'V':
9670 case 'C':
9671 case 'U':
9672 case 'k':
9673 case 'K':
d6f16593
MR
9674 i = my_getSmallExpression (&imm_expr, imm_reloc, s);
9675 if (i > 0)
252b5132 9676 {
d6f16593 9677 if (imm_expr.X_op != O_constant)
252b5132 9678 {
b34976b6 9679 mips16_ext = TRUE;
b34976b6 9680 ip->use_extend = TRUE;
252b5132 9681 ip->extend = 0;
252b5132 9682 }
d6f16593
MR
9683 else
9684 {
9685 /* We need to relax this instruction. */
9686 *offset_reloc = *imm_reloc;
9687 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9688 }
9689 s = expr_end;
9690 continue;
252b5132 9691 }
d6f16593
MR
9692 *imm_reloc = BFD_RELOC_UNUSED;
9693 /* Fall through. */
9694 case '<':
9695 case '>':
9696 case '[':
9697 case ']':
9698 case '4':
9699 case '8':
9700 my_getExpression (&imm_expr, s);
252b5132
RH
9701 if (imm_expr.X_op == O_register)
9702 {
9703 /* What we thought was an expression turned out to
9704 be a register. */
9705
9706 if (s[0] == '(' && args[1] == '(')
9707 {
9708 /* It looks like the expression was omitted
9709 before a register indirection, which means
9710 that the expression is implicitly zero. We
9711 still set up imm_expr, so that we handle
9712 explicit extensions correctly. */
9713 imm_expr.X_op = O_constant;
9714 imm_expr.X_add_number = 0;
f6688943 9715 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
9716 continue;
9717 }
9718
9719 break;
9720 }
9721
9722 /* We need to relax this instruction. */
f6688943 9723 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
9724 s = expr_end;
9725 continue;
9726
9727 case 'p':
9728 case 'q':
9729 case 'A':
9730 case 'B':
9731 case 'E':
9732 /* We use offset_reloc rather than imm_reloc for the PC
9733 relative operands. This lets macros with both
9734 immediate and address operands work correctly. */
9735 my_getExpression (&offset_expr, s);
9736
9737 if (offset_expr.X_op == O_register)
9738 break;
9739
9740 /* We need to relax this instruction. */
f6688943 9741 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
9742 s = expr_end;
9743 continue;
9744
9745 case '6': /* break code */
9746 my_getExpression (&imm_expr, s);
9747 check_absolute_expr (ip, &imm_expr);
9748 if ((unsigned long) imm_expr.X_add_number > 63)
bf12938e
RS
9749 as_warn (_("Invalid value for `%s' (%lu)"),
9750 ip->insn_mo->name,
9751 (unsigned long) imm_expr.X_add_number);
9752 MIPS16_INSERT_OPERAND (IMM6, *ip, imm_expr.X_add_number);
252b5132
RH
9753 imm_expr.X_op = O_absent;
9754 s = expr_end;
9755 continue;
9756
9757 case 'a': /* 26 bit address */
9758 my_getExpression (&offset_expr, s);
9759 s = expr_end;
f6688943 9760 *offset_reloc = BFD_RELOC_MIPS16_JMP;
252b5132
RH
9761 ip->insn_opcode <<= 16;
9762 continue;
9763
9764 case 'l': /* register list for entry macro */
9765 case 'L': /* register list for exit macro */
9766 {
9767 int mask;
9768
9769 if (c == 'l')
9770 mask = 0;
9771 else
9772 mask = 7 << 3;
9773 while (*s != '\0')
9774 {
9775 int freg, reg1, reg2;
9776
9777 while (*s == ' ' || *s == ',')
9778 ++s;
9779 if (*s != '$')
9780 {
9781 as_bad (_("can't parse register list"));
9782 break;
9783 }
9784 ++s;
9785 if (*s != 'f')
9786 freg = 0;
9787 else
9788 {
9789 freg = 1;
9790 ++s;
9791 }
9792 reg1 = 0;
3882b010 9793 while (ISDIGIT (*s))
252b5132
RH
9794 {
9795 reg1 *= 10;
9796 reg1 += *s - '0';
9797 ++s;
9798 }
9799 if (*s == ' ')
9800 ++s;
9801 if (*s != '-')
9802 reg2 = reg1;
9803 else
9804 {
9805 ++s;
9806 if (*s != '$')
9807 break;
9808 ++s;
9809 if (freg)
9810 {
9811 if (*s == 'f')
9812 ++s;
9813 else
9814 {
9815 as_bad (_("invalid register list"));
9816 break;
9817 }
9818 }
9819 reg2 = 0;
3882b010 9820 while (ISDIGIT (*s))
252b5132
RH
9821 {
9822 reg2 *= 10;
9823 reg2 += *s - '0';
9824 ++s;
9825 }
9826 }
9827 if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
9828 {
9829 mask &= ~ (7 << 3);
9830 mask |= 5 << 3;
9831 }
9832 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
9833 {
9834 mask &= ~ (7 << 3);
9835 mask |= 6 << 3;
9836 }
9837 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
9838 mask |= (reg2 - 3) << 3;
9839 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
9840 mask |= (reg2 - 15) << 1;
f9419b05 9841 else if (reg1 == RA && reg2 == RA)
252b5132
RH
9842 mask |= 1;
9843 else
9844 {
9845 as_bad (_("invalid register list"));
9846 break;
9847 }
9848 }
9849 /* The mask is filled in in the opcode table for the
9850 benefit of the disassembler. We remove it before
9851 applying the actual mask. */
9852 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
9853 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
9854 }
9855 continue;
9856
0499d65b
TS
9857 case 'm': /* Register list for save insn. */
9858 case 'M': /* Register list for restore insn. */
9859 {
9860 int opcode = 0;
9861 int framesz = 0, seen_framesz = 0;
9862 int args = 0, statics = 0, sregs = 0;
9863
9864 while (*s != '\0')
9865 {
9866 unsigned int reg1, reg2;
9867
9868 SKIP_SPACE_TABS (s);
9869 while (*s == ',')
9870 ++s;
9871 SKIP_SPACE_TABS (s);
9872
9873 my_getExpression (&imm_expr, s);
9874 if (imm_expr.X_op == O_constant)
9875 {
9876 /* Handle the frame size. */
9877 if (seen_framesz)
9878 {
9879 as_bad (_("more than one frame size in list"));
9880 break;
9881 }
9882 seen_framesz = 1;
9883 framesz = imm_expr.X_add_number;
9884 imm_expr.X_op = O_absent;
9885 s = expr_end;
9886 continue;
9887 }
9888
9889 if (*s != '$')
9890 {
9891 as_bad (_("can't parse register list"));
9892 break;
9893 }
9894 ++s;
9895
9896 reg1 = 0;
9897 while (ISDIGIT (*s))
9898 {
9899 reg1 *= 10;
9900 reg1 += *s - '0';
9901 ++s;
9902 }
9903 SKIP_SPACE_TABS (s);
9904 if (*s != '-')
9905 reg2 = reg1;
9906 else
9907 {
9908 ++s;
9909 if (*s != '$')
9910 {
9911 as_bad (_("can't parse register list"));
9912 break;
9913 }
9914 ++s;
9915 reg2 = 0;
9916 while (ISDIGIT (*s))
9917 {
9918 reg2 *= 10;
9919 reg2 += *s - '0';
9920 ++s;
9921 }
9922 }
9923
9924 while (reg1 <= reg2)
9925 {
9926 if (reg1 >= 4 && reg1 <= 7)
9927 {
9928 if (c == 'm' && !seen_framesz)
9929 /* args $a0-$a3 */
9930 args |= 1 << (reg1 - 4);
9931 else
9932 /* statics $a0-$a3 */
9933 statics |= 1 << (reg1 - 4);
9934 }
9935 else if ((reg1 >= 16 && reg1 <= 23) || reg1 == 30)
9936 {
9937 /* $s0-$s8 */
9938 sregs |= 1 << ((reg1 == 30) ? 8 : (reg1 - 16));
9939 }
9940 else if (reg1 == 31)
9941 {
9942 /* Add $ra to insn. */
9943 opcode |= 0x40;
9944 }
9945 else
9946 {
9947 as_bad (_("unexpected register in list"));
9948 break;
9949 }
9950 if (++reg1 == 24)
9951 reg1 = 30;
9952 }
9953 }
9954
9955 /* Encode args/statics combination. */
9956 if (args & statics)
9957 as_bad (_("arg/static registers overlap"));
9958 else if (args == 0xf)
9959 /* All $a0-$a3 are args. */
9960 opcode |= MIPS16_ALL_ARGS << 16;
9961 else if (statics == 0xf)
9962 /* All $a0-$a3 are statics. */
9963 opcode |= MIPS16_ALL_STATICS << 16;
9964 else
9965 {
9966 int narg = 0, nstat = 0;
9967
9968 /* Count arg registers. */
9969 while (args & 0x1)
9970 {
9971 args >>= 1;
9972 narg++;
9973 }
9974 if (args != 0)
9975 as_bad (_("invalid arg register list"));
9976
9977 /* Count static registers. */
9978 while (statics & 0x8)
9979 {
9980 statics = (statics << 1) & 0xf;
9981 nstat++;
9982 }
9983 if (statics != 0)
9984 as_bad (_("invalid static register list"));
9985
9986 /* Encode args/statics. */
9987 opcode |= ((narg << 2) | nstat) << 16;
9988 }
9989
9990 /* Encode $s0/$s1. */
9991 if (sregs & (1 << 0)) /* $s0 */
9992 opcode |= 0x20;
9993 if (sregs & (1 << 1)) /* $s1 */
9994 opcode |= 0x10;
9995 sregs >>= 2;
9996
9997 if (sregs != 0)
9998 {
9999 /* Count regs $s2-$s8. */
10000 int nsreg = 0;
10001 while (sregs & 1)
10002 {
10003 sregs >>= 1;
10004 nsreg++;
10005 }
10006 if (sregs != 0)
10007 as_bad (_("invalid static register list"));
10008 /* Encode $s2-$s8. */
10009 opcode |= nsreg << 24;
10010 }
10011
10012 /* Encode frame size. */
10013 if (!seen_framesz)
10014 as_bad (_("missing frame size"));
10015 else if ((framesz & 7) != 0 || framesz < 0
10016 || framesz > 0xff * 8)
10017 as_bad (_("invalid frame size"));
10018 else if (framesz != 128 || (opcode >> 16) != 0)
10019 {
10020 framesz /= 8;
10021 opcode |= (((framesz & 0xf0) << 16)
10022 | (framesz & 0x0f));
10023 }
10024
10025 /* Finally build the instruction. */
10026 if ((opcode >> 16) != 0 || framesz == 0)
10027 {
10028 ip->use_extend = TRUE;
10029 ip->extend = opcode >> 16;
10030 }
10031 ip->insn_opcode |= opcode & 0x7f;
10032 }
10033 continue;
10034
252b5132
RH
10035 case 'e': /* extend code */
10036 my_getExpression (&imm_expr, s);
10037 check_absolute_expr (ip, &imm_expr);
10038 if ((unsigned long) imm_expr.X_add_number > 0x7ff)
10039 {
10040 as_warn (_("Invalid value for `%s' (%lu)"),
10041 ip->insn_mo->name,
10042 (unsigned long) imm_expr.X_add_number);
10043 imm_expr.X_add_number &= 0x7ff;
10044 }
10045 ip->insn_opcode |= imm_expr.X_add_number;
10046 imm_expr.X_op = O_absent;
10047 s = expr_end;
10048 continue;
10049
10050 default:
10051 internalError ();
10052 }
10053 break;
10054 }
10055
10056 /* Args don't match. */
10057 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
10058 strcmp (insn->name, insn[1].name) == 0)
10059 {
10060 ++insn;
10061 s = argsstart;
10062 continue;
10063 }
10064
10065 insn_error = _("illegal operands");
10066
10067 return;
10068 }
10069}
10070
10071/* This structure holds information we know about a mips16 immediate
10072 argument type. */
10073
e972090a
NC
10074struct mips16_immed_operand
10075{
252b5132
RH
10076 /* The type code used in the argument string in the opcode table. */
10077 int type;
10078 /* The number of bits in the short form of the opcode. */
10079 int nbits;
10080 /* The number of bits in the extended form of the opcode. */
10081 int extbits;
10082 /* The amount by which the short form is shifted when it is used;
10083 for example, the sw instruction has a shift count of 2. */
10084 int shift;
10085 /* The amount by which the short form is shifted when it is stored
10086 into the instruction code. */
10087 int op_shift;
10088 /* Non-zero if the short form is unsigned. */
10089 int unsp;
10090 /* Non-zero if the extended form is unsigned. */
10091 int extu;
10092 /* Non-zero if the value is PC relative. */
10093 int pcrel;
10094};
10095
10096/* The mips16 immediate operand types. */
10097
10098static const struct mips16_immed_operand mips16_immed_operands[] =
10099{
10100 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
10101 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 },
10102 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
10103 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 },
10104 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
10105 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
10106 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
10107 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
10108 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
10109 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
10110 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
10111 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
10112 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
10113 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
10114 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
10115 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
10116 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
10117 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
10118 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
10119 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
10120 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
10121};
10122
10123#define MIPS16_NUM_IMMED \
10124 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
10125
10126/* Handle a mips16 instruction with an immediate value. This or's the
10127 small immediate value into *INSN. It sets *USE_EXTEND to indicate
10128 whether an extended value is needed; if one is needed, it sets
10129 *EXTEND to the value. The argument type is TYPE. The value is VAL.
10130 If SMALL is true, an unextended opcode was explicitly requested.
10131 If EXT is true, an extended opcode was explicitly requested. If
10132 WARN is true, warn if EXT does not match reality. */
10133
10134static void
17a2f251
TS
10135mips16_immed (char *file, unsigned int line, int type, offsetT val,
10136 bfd_boolean warn, bfd_boolean small, bfd_boolean ext,
10137 unsigned long *insn, bfd_boolean *use_extend,
10138 unsigned short *extend)
252b5132
RH
10139{
10140 register const struct mips16_immed_operand *op;
10141 int mintiny, maxtiny;
b34976b6 10142 bfd_boolean needext;
252b5132
RH
10143
10144 op = mips16_immed_operands;
10145 while (op->type != type)
10146 {
10147 ++op;
10148 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
10149 }
10150
10151 if (op->unsp)
10152 {
10153 if (type == '<' || type == '>' || type == '[' || type == ']')
10154 {
10155 mintiny = 1;
10156 maxtiny = 1 << op->nbits;
10157 }
10158 else
10159 {
10160 mintiny = 0;
10161 maxtiny = (1 << op->nbits) - 1;
10162 }
10163 }
10164 else
10165 {
10166 mintiny = - (1 << (op->nbits - 1));
10167 maxtiny = (1 << (op->nbits - 1)) - 1;
10168 }
10169
10170 /* Branch offsets have an implicit 0 in the lowest bit. */
10171 if (type == 'p' || type == 'q')
10172 val /= 2;
10173
10174 if ((val & ((1 << op->shift) - 1)) != 0
10175 || val < (mintiny << op->shift)
10176 || val > (maxtiny << op->shift))
b34976b6 10177 needext = TRUE;
252b5132 10178 else
b34976b6 10179 needext = FALSE;
252b5132
RH
10180
10181 if (warn && ext && ! needext)
beae10d5
KH
10182 as_warn_where (file, line,
10183 _("extended operand requested but not required"));
252b5132
RH
10184 if (small && needext)
10185 as_bad_where (file, line, _("invalid unextended operand value"));
10186
10187 if (small || (! ext && ! needext))
10188 {
10189 int insnval;
10190
b34976b6 10191 *use_extend = FALSE;
252b5132
RH
10192 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
10193 insnval <<= op->op_shift;
10194 *insn |= insnval;
10195 }
10196 else
10197 {
10198 long minext, maxext;
10199 int extval;
10200
10201 if (op->extu)
10202 {
10203 minext = 0;
10204 maxext = (1 << op->extbits) - 1;
10205 }
10206 else
10207 {
10208 minext = - (1 << (op->extbits - 1));
10209 maxext = (1 << (op->extbits - 1)) - 1;
10210 }
10211 if (val < minext || val > maxext)
10212 as_bad_where (file, line,
10213 _("operand value out of range for instruction"));
10214
b34976b6 10215 *use_extend = TRUE;
252b5132
RH
10216 if (op->extbits == 16)
10217 {
10218 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
10219 val &= 0x1f;
10220 }
10221 else if (op->extbits == 15)
10222 {
10223 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
10224 val &= 0xf;
10225 }
10226 else
10227 {
10228 extval = ((val & 0x1f) << 6) | (val & 0x20);
10229 val = 0;
10230 }
10231
10232 *extend = (unsigned short) extval;
10233 *insn |= val;
10234 }
10235}
10236\f
d6f16593 10237struct percent_op_match
ad8d3bb3 10238{
5e0116d5
RS
10239 const char *str;
10240 bfd_reloc_code_real_type reloc;
d6f16593
MR
10241};
10242
10243static const struct percent_op_match mips_percent_op[] =
ad8d3bb3 10244{
5e0116d5 10245 {"%lo", BFD_RELOC_LO16},
ad8d3bb3 10246#ifdef OBJ_ELF
5e0116d5
RS
10247 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
10248 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
10249 {"%call16", BFD_RELOC_MIPS_CALL16},
10250 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
10251 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
10252 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
10253 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
10254 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
10255 {"%got", BFD_RELOC_MIPS_GOT16},
10256 {"%gp_rel", BFD_RELOC_GPREL16},
10257 {"%half", BFD_RELOC_16},
10258 {"%highest", BFD_RELOC_MIPS_HIGHEST},
10259 {"%higher", BFD_RELOC_MIPS_HIGHER},
10260 {"%neg", BFD_RELOC_MIPS_SUB},
3f98094e
DJ
10261 {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
10262 {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
10263 {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
10264 {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
10265 {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
10266 {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
10267 {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
ad8d3bb3 10268#endif
5e0116d5 10269 {"%hi", BFD_RELOC_HI16_S}
ad8d3bb3
TS
10270};
10271
d6f16593
MR
10272static const struct percent_op_match mips16_percent_op[] =
10273{
10274 {"%lo", BFD_RELOC_MIPS16_LO16},
10275 {"%gprel", BFD_RELOC_MIPS16_GPREL},
10276 {"%hi", BFD_RELOC_MIPS16_HI16_S}
10277};
10278
252b5132 10279
5e0116d5
RS
10280/* Return true if *STR points to a relocation operator. When returning true,
10281 move *STR over the operator and store its relocation code in *RELOC.
10282 Leave both *STR and *RELOC alone when returning false. */
10283
10284static bfd_boolean
17a2f251 10285parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
252b5132 10286{
d6f16593
MR
10287 const struct percent_op_match *percent_op;
10288 size_t limit, i;
10289
10290 if (mips_opts.mips16)
10291 {
10292 percent_op = mips16_percent_op;
10293 limit = ARRAY_SIZE (mips16_percent_op);
10294 }
10295 else
10296 {
10297 percent_op = mips_percent_op;
10298 limit = ARRAY_SIZE (mips_percent_op);
10299 }
76b3015f 10300
d6f16593 10301 for (i = 0; i < limit; i++)
5e0116d5 10302 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
394f9b3a 10303 {
3f98094e
DJ
10304 int len = strlen (percent_op[i].str);
10305
10306 if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
10307 continue;
10308
5e0116d5
RS
10309 *str += strlen (percent_op[i].str);
10310 *reloc = percent_op[i].reloc;
394f9b3a 10311
5e0116d5
RS
10312 /* Check whether the output BFD supports this relocation.
10313 If not, issue an error and fall back on something safe. */
10314 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
394f9b3a 10315 {
5e0116d5
RS
10316 as_bad ("relocation %s isn't supported by the current ABI",
10317 percent_op[i].str);
01a3f561 10318 *reloc = BFD_RELOC_UNUSED;
394f9b3a 10319 }
5e0116d5 10320 return TRUE;
394f9b3a 10321 }
5e0116d5 10322 return FALSE;
394f9b3a 10323}
ad8d3bb3 10324
ad8d3bb3 10325
5e0116d5
RS
10326/* Parse string STR as a 16-bit relocatable operand. Store the
10327 expression in *EP and the relocations in the array starting
10328 at RELOC. Return the number of relocation operators used.
ad8d3bb3 10329
01a3f561 10330 On exit, EXPR_END points to the first character after the expression. */
ad8d3bb3 10331
5e0116d5 10332static size_t
17a2f251
TS
10333my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
10334 char *str)
ad8d3bb3 10335{
5e0116d5
RS
10336 bfd_reloc_code_real_type reversed_reloc[3];
10337 size_t reloc_index, i;
09b8f35a
RS
10338 int crux_depth, str_depth;
10339 char *crux;
5e0116d5
RS
10340
10341 /* Search for the start of the main expression, recoding relocations
09b8f35a
RS
10342 in REVERSED_RELOC. End the loop with CRUX pointing to the start
10343 of the main expression and with CRUX_DEPTH containing the number
10344 of open brackets at that point. */
10345 reloc_index = -1;
10346 str_depth = 0;
10347 do
fb1b3232 10348 {
09b8f35a
RS
10349 reloc_index++;
10350 crux = str;
10351 crux_depth = str_depth;
10352
10353 /* Skip over whitespace and brackets, keeping count of the number
10354 of brackets. */
10355 while (*str == ' ' || *str == '\t' || *str == '(')
10356 if (*str++ == '(')
10357 str_depth++;
5e0116d5 10358 }
09b8f35a
RS
10359 while (*str == '%'
10360 && reloc_index < (HAVE_NEWABI ? 3 : 1)
10361 && parse_relocation (&str, &reversed_reloc[reloc_index]));
ad8d3bb3 10362
09b8f35a 10363 my_getExpression (ep, crux);
5e0116d5 10364 str = expr_end;
394f9b3a 10365
5e0116d5 10366 /* Match every open bracket. */
09b8f35a 10367 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
5e0116d5 10368 if (*str++ == ')')
09b8f35a 10369 crux_depth--;
394f9b3a 10370
09b8f35a 10371 if (crux_depth > 0)
5e0116d5 10372 as_bad ("unclosed '('");
394f9b3a 10373
5e0116d5 10374 expr_end = str;
252b5132 10375
01a3f561 10376 if (reloc_index != 0)
64bdfcaf
RS
10377 {
10378 prev_reloc_op_frag = frag_now;
10379 for (i = 0; i < reloc_index; i++)
10380 reloc[i] = reversed_reloc[reloc_index - 1 - i];
10381 }
fb1b3232 10382
5e0116d5 10383 return reloc_index;
252b5132
RH
10384}
10385
10386static void
17a2f251 10387my_getExpression (expressionS *ep, char *str)
252b5132
RH
10388{
10389 char *save_in;
98aa84af 10390 valueT val;
252b5132
RH
10391
10392 save_in = input_line_pointer;
10393 input_line_pointer = str;
10394 expression (ep);
10395 expr_end = input_line_pointer;
10396 input_line_pointer = save_in;
10397
10398 /* If we are in mips16 mode, and this is an expression based on `.',
10399 then we bump the value of the symbol by 1 since that is how other
10400 text symbols are handled. We don't bother to handle complex
10401 expressions, just `.' plus or minus a constant. */
10402 if (mips_opts.mips16
10403 && ep->X_op == O_symbol
10404 && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
10405 && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
49309057
ILT
10406 && symbol_get_frag (ep->X_add_symbol) == frag_now
10407 && symbol_constant_p (ep->X_add_symbol)
98aa84af
AM
10408 && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
10409 S_SET_VALUE (ep->X_add_symbol, val + 1);
252b5132
RH
10410}
10411
10412/* Turn a string in input_line_pointer into a floating point constant
bc0d738a
NC
10413 of type TYPE, and store the appropriate bytes in *LITP. The number
10414 of LITTLENUMS emitted is stored in *SIZEP. An error message is
252b5132
RH
10415 returned, or NULL on OK. */
10416
10417char *
17a2f251 10418md_atof (int type, char *litP, int *sizeP)
252b5132
RH
10419{
10420 int prec;
10421 LITTLENUM_TYPE words[4];
10422 char *t;
10423 int i;
10424
10425 switch (type)
10426 {
10427 case 'f':
10428 prec = 2;
10429 break;
10430
10431 case 'd':
10432 prec = 4;
10433 break;
10434
10435 default:
10436 *sizeP = 0;
10437 return _("bad call to md_atof");
10438 }
10439
10440 t = atof_ieee (input_line_pointer, type, words);
10441 if (t)
10442 input_line_pointer = t;
10443
10444 *sizeP = prec * 2;
10445
10446 if (! target_big_endian)
10447 {
10448 for (i = prec - 1; i >= 0; i--)
10449 {
17a2f251 10450 md_number_to_chars (litP, words[i], 2);
252b5132
RH
10451 litP += 2;
10452 }
10453 }
10454 else
10455 {
10456 for (i = 0; i < prec; i++)
10457 {
17a2f251 10458 md_number_to_chars (litP, words[i], 2);
252b5132
RH
10459 litP += 2;
10460 }
10461 }
bdaaa2e1 10462
252b5132
RH
10463 return NULL;
10464}
10465
10466void
17a2f251 10467md_number_to_chars (char *buf, valueT val, int n)
252b5132
RH
10468{
10469 if (target_big_endian)
10470 number_to_chars_bigendian (buf, val, n);
10471 else
10472 number_to_chars_littleendian (buf, val, n);
10473}
10474\f
ae948b86 10475#ifdef OBJ_ELF
e013f690
TS
10476static int support_64bit_objects(void)
10477{
10478 const char **list, **l;
aa3d8fdf 10479 int yes;
e013f690
TS
10480
10481 list = bfd_target_list ();
10482 for (l = list; *l != NULL; l++)
10483#ifdef TE_TMIPS
10484 /* This is traditional mips */
10485 if (strcmp (*l, "elf64-tradbigmips") == 0
10486 || strcmp (*l, "elf64-tradlittlemips") == 0)
10487#else
10488 if (strcmp (*l, "elf64-bigmips") == 0
10489 || strcmp (*l, "elf64-littlemips") == 0)
10490#endif
10491 break;
aa3d8fdf 10492 yes = (*l != NULL);
e013f690 10493 free (list);
aa3d8fdf 10494 return yes;
e013f690 10495}
ae948b86 10496#endif /* OBJ_ELF */
e013f690 10497
78849248 10498const char *md_shortopts = "O::g::G:";
252b5132 10499
e972090a
NC
10500struct option md_longopts[] =
10501{
f9b4148d
CD
10502 /* Options which specify architecture. */
10503#define OPTION_ARCH_BASE (OPTION_MD_BASE)
10504#define OPTION_MARCH (OPTION_ARCH_BASE + 0)
10505 {"march", required_argument, NULL, OPTION_MARCH},
10506#define OPTION_MTUNE (OPTION_ARCH_BASE + 1)
10507 {"mtune", required_argument, NULL, OPTION_MTUNE},
10508#define OPTION_MIPS1 (OPTION_ARCH_BASE + 2)
252b5132
RH
10509 {"mips0", no_argument, NULL, OPTION_MIPS1},
10510 {"mips1", no_argument, NULL, OPTION_MIPS1},
f9b4148d 10511#define OPTION_MIPS2 (OPTION_ARCH_BASE + 3)
252b5132 10512 {"mips2", no_argument, NULL, OPTION_MIPS2},
f9b4148d 10513#define OPTION_MIPS3 (OPTION_ARCH_BASE + 4)
252b5132 10514 {"mips3", no_argument, NULL, OPTION_MIPS3},
f9b4148d 10515#define OPTION_MIPS4 (OPTION_ARCH_BASE + 5)
252b5132 10516 {"mips4", no_argument, NULL, OPTION_MIPS4},
f9b4148d 10517#define OPTION_MIPS5 (OPTION_ARCH_BASE + 6)
ae948b86 10518 {"mips5", no_argument, NULL, OPTION_MIPS5},
f9b4148d 10519#define OPTION_MIPS32 (OPTION_ARCH_BASE + 7)
ae948b86 10520 {"mips32", no_argument, NULL, OPTION_MIPS32},
f9b4148d 10521#define OPTION_MIPS64 (OPTION_ARCH_BASE + 8)
ae948b86 10522 {"mips64", no_argument, NULL, OPTION_MIPS64},
f9b4148d
CD
10523#define OPTION_MIPS32R2 (OPTION_ARCH_BASE + 9)
10524 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
5f74bc13
CD
10525#define OPTION_MIPS64R2 (OPTION_ARCH_BASE + 10)
10526 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
f9b4148d
CD
10527
10528 /* Options which specify Application Specific Extensions (ASEs). */
5f74bc13 10529#define OPTION_ASE_BASE (OPTION_ARCH_BASE + 11)
f9b4148d
CD
10530#define OPTION_MIPS16 (OPTION_ASE_BASE + 0)
10531 {"mips16", no_argument, NULL, OPTION_MIPS16},
10532#define OPTION_NO_MIPS16 (OPTION_ASE_BASE + 1)
10533 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
10534#define OPTION_MIPS3D (OPTION_ASE_BASE + 2)
10535 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
10536#define OPTION_NO_MIPS3D (OPTION_ASE_BASE + 3)
10537 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
10538#define OPTION_MDMX (OPTION_ASE_BASE + 4)
10539 {"mdmx", no_argument, NULL, OPTION_MDMX},
10540#define OPTION_NO_MDMX (OPTION_ASE_BASE + 5)
10541 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
74cd071d
CF
10542#define OPTION_DSP (OPTION_ASE_BASE + 6)
10543 {"mdsp", no_argument, NULL, OPTION_DSP},
10544#define OPTION_NO_DSP (OPTION_ASE_BASE + 7)
10545 {"mno-dsp", no_argument, NULL, OPTION_NO_DSP},
ef2e4d86
CF
10546#define OPTION_MT (OPTION_ASE_BASE + 8)
10547 {"mmt", no_argument, NULL, OPTION_MT},
10548#define OPTION_NO_MT (OPTION_ASE_BASE + 9)
10549 {"mno-mt", no_argument, NULL, OPTION_NO_MT},
f9b4148d
CD
10550
10551 /* Old-style architecture options. Don't add more of these. */
ef2e4d86 10552#define OPTION_COMPAT_ARCH_BASE (OPTION_ASE_BASE + 10)
f9b4148d
CD
10553#define OPTION_M4650 (OPTION_COMPAT_ARCH_BASE + 0)
10554 {"m4650", no_argument, NULL, OPTION_M4650},
10555#define OPTION_NO_M4650 (OPTION_COMPAT_ARCH_BASE + 1)
10556 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
10557#define OPTION_M4010 (OPTION_COMPAT_ARCH_BASE + 2)
10558 {"m4010", no_argument, NULL, OPTION_M4010},
10559#define OPTION_NO_M4010 (OPTION_COMPAT_ARCH_BASE + 3)
10560 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
10561#define OPTION_M4100 (OPTION_COMPAT_ARCH_BASE + 4)
10562 {"m4100", no_argument, NULL, OPTION_M4100},
10563#define OPTION_NO_M4100 (OPTION_COMPAT_ARCH_BASE + 5)
10564 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
10565#define OPTION_M3900 (OPTION_COMPAT_ARCH_BASE + 6)
10566 {"m3900", no_argument, NULL, OPTION_M3900},
10567#define OPTION_NO_M3900 (OPTION_COMPAT_ARCH_BASE + 7)
10568 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
10569
10570 /* Options which enable bug fixes. */
10571#define OPTION_FIX_BASE (OPTION_COMPAT_ARCH_BASE + 8)
10572#define OPTION_M7000_HILO_FIX (OPTION_FIX_BASE + 0)
10573 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
10574#define OPTION_MNO_7000_HILO_FIX (OPTION_FIX_BASE + 1)
10575 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10576 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
d766e8ec
RS
10577#define OPTION_FIX_VR4120 (OPTION_FIX_BASE + 2)
10578#define OPTION_NO_FIX_VR4120 (OPTION_FIX_BASE + 3)
10579 {"mfix-vr4120", no_argument, NULL, OPTION_FIX_VR4120},
10580 {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
7d8e00cf
RS
10581#define OPTION_FIX_VR4130 (OPTION_FIX_BASE + 4)
10582#define OPTION_NO_FIX_VR4130 (OPTION_FIX_BASE + 5)
10583 {"mfix-vr4130", no_argument, NULL, OPTION_FIX_VR4130},
10584 {"mno-fix-vr4130", no_argument, NULL, OPTION_NO_FIX_VR4130},
f9b4148d
CD
10585
10586 /* Miscellaneous options. */
7d8e00cf 10587#define OPTION_MISC_BASE (OPTION_FIX_BASE + 6)
1ffcab4b 10588#define OPTION_TRAP (OPTION_MISC_BASE + 0)
252b5132
RH
10589 {"trap", no_argument, NULL, OPTION_TRAP},
10590 {"no-break", no_argument, NULL, OPTION_TRAP},
1ffcab4b 10591#define OPTION_BREAK (OPTION_MISC_BASE + 1)
252b5132
RH
10592 {"break", no_argument, NULL, OPTION_BREAK},
10593 {"no-trap", no_argument, NULL, OPTION_BREAK},
1ffcab4b 10594#define OPTION_EB (OPTION_MISC_BASE + 2)
252b5132 10595 {"EB", no_argument, NULL, OPTION_EB},
1ffcab4b 10596#define OPTION_EL (OPTION_MISC_BASE + 3)
252b5132 10597 {"EL", no_argument, NULL, OPTION_EL},
1ffcab4b 10598#define OPTION_FP32 (OPTION_MISC_BASE + 4)
ae948b86 10599 {"mfp32", no_argument, NULL, OPTION_FP32},
1ffcab4b 10600#define OPTION_GP32 (OPTION_MISC_BASE + 5)
c97ef257 10601 {"mgp32", no_argument, NULL, OPTION_GP32},
1ffcab4b 10602#define OPTION_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 6)
119d663a 10603 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
1ffcab4b 10604#define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 7)
119d663a 10605 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
1ffcab4b 10606#define OPTION_FP64 (OPTION_MISC_BASE + 8)
316f5878 10607 {"mfp64", no_argument, NULL, OPTION_FP64},
1ffcab4b 10608#define OPTION_GP64 (OPTION_MISC_BASE + 9)
ae948b86 10609 {"mgp64", no_argument, NULL, OPTION_GP64},
1ffcab4b
CD
10610#define OPTION_RELAX_BRANCH (OPTION_MISC_BASE + 10)
10611#define OPTION_NO_RELAX_BRANCH (OPTION_MISC_BASE + 11)
4a6a3df4
AO
10612 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
10613 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
aa6975fb
ILT
10614#define OPTION_MSHARED (OPTION_MISC_BASE + 12)
10615#define OPTION_MNO_SHARED (OPTION_MISC_BASE + 13)
10616 {"mshared", no_argument, NULL, OPTION_MSHARED},
10617 {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
aed1a261
RS
10618#define OPTION_MSYM32 (OPTION_MISC_BASE + 14)
10619#define OPTION_MNO_SYM32 (OPTION_MISC_BASE + 15)
10620 {"msym32", no_argument, NULL, OPTION_MSYM32},
10621 {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
f9b4148d
CD
10622
10623 /* ELF-specific options. */
156c2f8b 10624#ifdef OBJ_ELF
aed1a261 10625#define OPTION_ELF_BASE (OPTION_MISC_BASE + 16)
156c2f8b 10626#define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
156c2f8b
NC
10627 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
10628 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
ae948b86 10629#define OPTION_NON_SHARED (OPTION_ELF_BASE + 1)
156c2f8b 10630 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
ae948b86 10631#define OPTION_XGOT (OPTION_ELF_BASE + 2)
156c2f8b 10632 {"xgot", no_argument, NULL, OPTION_XGOT},
ae948b86
TS
10633#define OPTION_MABI (OPTION_ELF_BASE + 3)
10634 {"mabi", required_argument, NULL, OPTION_MABI},
10635#define OPTION_32 (OPTION_ELF_BASE + 4)
156c2f8b 10636 {"32", no_argument, NULL, OPTION_32},
ae948b86 10637#define OPTION_N32 (OPTION_ELF_BASE + 5)
e013f690 10638 {"n32", no_argument, NULL, OPTION_N32},
ae948b86 10639#define OPTION_64 (OPTION_ELF_BASE + 6)
156c2f8b 10640 {"64", no_argument, NULL, OPTION_64},
ecb4347a
DJ
10641#define OPTION_MDEBUG (OPTION_ELF_BASE + 7)
10642 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
10643#define OPTION_NO_MDEBUG (OPTION_ELF_BASE + 8)
10644 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
dcd410fe
RO
10645#define OPTION_PDR (OPTION_ELF_BASE + 9)
10646 {"mpdr", no_argument, NULL, OPTION_PDR},
10647#define OPTION_NO_PDR (OPTION_ELF_BASE + 10)
10648 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
ae948b86 10649#endif /* OBJ_ELF */
f9b4148d 10650
252b5132
RH
10651 {NULL, no_argument, NULL, 0}
10652};
156c2f8b 10653size_t md_longopts_size = sizeof (md_longopts);
252b5132 10654
316f5878
RS
10655/* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
10656 NEW_VALUE. Warn if another value was already specified. Note:
10657 we have to defer parsing the -march and -mtune arguments in order
10658 to handle 'from-abi' correctly, since the ABI might be specified
10659 in a later argument. */
10660
10661static void
17a2f251 10662mips_set_option_string (const char **string_ptr, const char *new_value)
316f5878
RS
10663{
10664 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
10665 as_warn (_("A different %s was already specified, is now %s"),
10666 string_ptr == &mips_arch_string ? "-march" : "-mtune",
10667 new_value);
10668
10669 *string_ptr = new_value;
10670}
10671
252b5132 10672int
17a2f251 10673md_parse_option (int c, char *arg)
252b5132
RH
10674{
10675 switch (c)
10676 {
119d663a
NC
10677 case OPTION_CONSTRUCT_FLOATS:
10678 mips_disable_float_construction = 0;
10679 break;
bdaaa2e1 10680
119d663a
NC
10681 case OPTION_NO_CONSTRUCT_FLOATS:
10682 mips_disable_float_construction = 1;
10683 break;
bdaaa2e1 10684
252b5132
RH
10685 case OPTION_TRAP:
10686 mips_trap = 1;
10687 break;
10688
10689 case OPTION_BREAK:
10690 mips_trap = 0;
10691 break;
10692
10693 case OPTION_EB:
10694 target_big_endian = 1;
10695 break;
10696
10697 case OPTION_EL:
10698 target_big_endian = 0;
10699 break;
10700
10701 case 'O':
10702 if (arg && arg[1] == '0')
10703 mips_optimize = 1;
10704 else
10705 mips_optimize = 2;
10706 break;
10707
10708 case 'g':
10709 if (arg == NULL)
10710 mips_debug = 2;
10711 else
10712 mips_debug = atoi (arg);
10713 /* When the MIPS assembler sees -g or -g2, it does not do
10714 optimizations which limit full symbolic debugging. We take
10715 that to be equivalent to -O0. */
10716 if (mips_debug == 2)
10717 mips_optimize = 1;
10718 break;
10719
10720 case OPTION_MIPS1:
316f5878 10721 file_mips_isa = ISA_MIPS1;
252b5132
RH
10722 break;
10723
10724 case OPTION_MIPS2:
316f5878 10725 file_mips_isa = ISA_MIPS2;
252b5132
RH
10726 break;
10727
10728 case OPTION_MIPS3:
316f5878 10729 file_mips_isa = ISA_MIPS3;
252b5132
RH
10730 break;
10731
10732 case OPTION_MIPS4:
316f5878 10733 file_mips_isa = ISA_MIPS4;
e7af610e
NC
10734 break;
10735
84ea6cf2 10736 case OPTION_MIPS5:
316f5878 10737 file_mips_isa = ISA_MIPS5;
84ea6cf2
NC
10738 break;
10739
e7af610e 10740 case OPTION_MIPS32:
316f5878 10741 file_mips_isa = ISA_MIPS32;
252b5132
RH
10742 break;
10743
af7ee8bf
CD
10744 case OPTION_MIPS32R2:
10745 file_mips_isa = ISA_MIPS32R2;
10746 break;
10747
5f74bc13
CD
10748 case OPTION_MIPS64R2:
10749 file_mips_isa = ISA_MIPS64R2;
10750 break;
10751
84ea6cf2 10752 case OPTION_MIPS64:
316f5878 10753 file_mips_isa = ISA_MIPS64;
84ea6cf2
NC
10754 break;
10755
ec68c924 10756 case OPTION_MTUNE:
316f5878
RS
10757 mips_set_option_string (&mips_tune_string, arg);
10758 break;
ec68c924 10759
316f5878
RS
10760 case OPTION_MARCH:
10761 mips_set_option_string (&mips_arch_string, arg);
252b5132
RH
10762 break;
10763
10764 case OPTION_M4650:
316f5878
RS
10765 mips_set_option_string (&mips_arch_string, "4650");
10766 mips_set_option_string (&mips_tune_string, "4650");
252b5132
RH
10767 break;
10768
10769 case OPTION_NO_M4650:
10770 break;
10771
10772 case OPTION_M4010:
316f5878
RS
10773 mips_set_option_string (&mips_arch_string, "4010");
10774 mips_set_option_string (&mips_tune_string, "4010");
252b5132
RH
10775 break;
10776
10777 case OPTION_NO_M4010:
10778 break;
10779
10780 case OPTION_M4100:
316f5878
RS
10781 mips_set_option_string (&mips_arch_string, "4100");
10782 mips_set_option_string (&mips_tune_string, "4100");
252b5132
RH
10783 break;
10784
10785 case OPTION_NO_M4100:
10786 break;
10787
252b5132 10788 case OPTION_M3900:
316f5878
RS
10789 mips_set_option_string (&mips_arch_string, "3900");
10790 mips_set_option_string (&mips_tune_string, "3900");
252b5132 10791 break;
bdaaa2e1 10792
252b5132
RH
10793 case OPTION_NO_M3900:
10794 break;
10795
deec1734
CD
10796 case OPTION_MDMX:
10797 mips_opts.ase_mdmx = 1;
10798 break;
10799
10800 case OPTION_NO_MDMX:
10801 mips_opts.ase_mdmx = 0;
10802 break;
10803
74cd071d
CF
10804 case OPTION_DSP:
10805 mips_opts.ase_dsp = 1;
10806 break;
10807
10808 case OPTION_NO_DSP:
10809 mips_opts.ase_dsp = 0;
10810 break;
10811
ef2e4d86
CF
10812 case OPTION_MT:
10813 mips_opts.ase_mt = 1;
10814 break;
10815
10816 case OPTION_NO_MT:
10817 mips_opts.ase_mt = 0;
10818 break;
10819
252b5132
RH
10820 case OPTION_MIPS16:
10821 mips_opts.mips16 = 1;
7d10b47d 10822 mips_no_prev_insn ();
252b5132
RH
10823 break;
10824
10825 case OPTION_NO_MIPS16:
10826 mips_opts.mips16 = 0;
7d10b47d 10827 mips_no_prev_insn ();
252b5132
RH
10828 break;
10829
1f25f5d3
CD
10830 case OPTION_MIPS3D:
10831 mips_opts.ase_mips3d = 1;
10832 break;
10833
10834 case OPTION_NO_MIPS3D:
10835 mips_opts.ase_mips3d = 0;
10836 break;
10837
d766e8ec
RS
10838 case OPTION_FIX_VR4120:
10839 mips_fix_vr4120 = 1;
60b63b72
RS
10840 break;
10841
d766e8ec
RS
10842 case OPTION_NO_FIX_VR4120:
10843 mips_fix_vr4120 = 0;
60b63b72
RS
10844 break;
10845
7d8e00cf
RS
10846 case OPTION_FIX_VR4130:
10847 mips_fix_vr4130 = 1;
10848 break;
10849
10850 case OPTION_NO_FIX_VR4130:
10851 mips_fix_vr4130 = 0;
10852 break;
10853
4a6a3df4
AO
10854 case OPTION_RELAX_BRANCH:
10855 mips_relax_branch = 1;
10856 break;
10857
10858 case OPTION_NO_RELAX_BRANCH:
10859 mips_relax_branch = 0;
10860 break;
10861
aa6975fb
ILT
10862 case OPTION_MSHARED:
10863 mips_in_shared = TRUE;
10864 break;
10865
10866 case OPTION_MNO_SHARED:
10867 mips_in_shared = FALSE;
10868 break;
10869
aed1a261
RS
10870 case OPTION_MSYM32:
10871 mips_opts.sym32 = TRUE;
10872 break;
10873
10874 case OPTION_MNO_SYM32:
10875 mips_opts.sym32 = FALSE;
10876 break;
10877
0f074f60 10878#ifdef OBJ_ELF
252b5132
RH
10879 /* When generating ELF code, we permit -KPIC and -call_shared to
10880 select SVR4_PIC, and -non_shared to select no PIC. This is
10881 intended to be compatible with Irix 5. */
10882 case OPTION_CALL_SHARED:
10883 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10884 {
10885 as_bad (_("-call_shared is supported only for ELF format"));
10886 return 0;
10887 }
10888 mips_pic = SVR4_PIC;
143d77c5 10889 mips_abicalls = TRUE;
252b5132
RH
10890 if (g_switch_seen && g_switch_value != 0)
10891 {
10892 as_bad (_("-G may not be used with SVR4 PIC code"));
10893 return 0;
10894 }
10895 g_switch_value = 0;
10896 break;
10897
10898 case OPTION_NON_SHARED:
10899 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10900 {
10901 as_bad (_("-non_shared is supported only for ELF format"));
10902 return 0;
10903 }
10904 mips_pic = NO_PIC;
143d77c5 10905 mips_abicalls = FALSE;
252b5132
RH
10906 break;
10907
44075ae2
TS
10908 /* The -xgot option tells the assembler to use 32 bit offsets
10909 when accessing the got in SVR4_PIC mode. It is for Irix
252b5132
RH
10910 compatibility. */
10911 case OPTION_XGOT:
10912 mips_big_got = 1;
10913 break;
0f074f60 10914#endif /* OBJ_ELF */
252b5132
RH
10915
10916 case 'G':
6caf9ef4
TS
10917 g_switch_value = atoi (arg);
10918 g_switch_seen = 1;
10919 if (mips_pic == SVR4_PIC && g_switch_value != 0)
252b5132 10920 {
3e722fb5 10921 as_bad (_("-G may not be used with SVR4 PIC code"));
252b5132
RH
10922 return 0;
10923 }
252b5132
RH
10924 break;
10925
0f074f60 10926#ifdef OBJ_ELF
34ba82a8
TS
10927 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
10928 and -mabi=64. */
252b5132 10929 case OPTION_32:
34ba82a8
TS
10930 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10931 {
10932 as_bad (_("-32 is supported for ELF format only"));
10933 return 0;
10934 }
316f5878 10935 mips_abi = O32_ABI;
252b5132
RH
10936 break;
10937
e013f690 10938 case OPTION_N32:
34ba82a8
TS
10939 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10940 {
10941 as_bad (_("-n32 is supported for ELF format only"));
10942 return 0;
10943 }
316f5878 10944 mips_abi = N32_ABI;
e013f690 10945 break;
252b5132 10946
e013f690 10947 case OPTION_64:
34ba82a8
TS
10948 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10949 {
10950 as_bad (_("-64 is supported for ELF format only"));
10951 return 0;
10952 }
316f5878 10953 mips_abi = N64_ABI;
e013f690
TS
10954 if (! support_64bit_objects())
10955 as_fatal (_("No compiled in support for 64 bit object file format"));
252b5132 10956 break;
ae948b86 10957#endif /* OBJ_ELF */
252b5132 10958
c97ef257 10959 case OPTION_GP32:
a325df1d 10960 file_mips_gp32 = 1;
c97ef257
AH
10961 break;
10962
10963 case OPTION_GP64:
a325df1d 10964 file_mips_gp32 = 0;
c97ef257 10965 break;
252b5132 10966
ca4e0257 10967 case OPTION_FP32:
a325df1d 10968 file_mips_fp32 = 1;
316f5878
RS
10969 break;
10970
10971 case OPTION_FP64:
10972 file_mips_fp32 = 0;
ca4e0257
RS
10973 break;
10974
ae948b86 10975#ifdef OBJ_ELF
252b5132 10976 case OPTION_MABI:
34ba82a8
TS
10977 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10978 {
10979 as_bad (_("-mabi is supported for ELF format only"));
10980 return 0;
10981 }
e013f690 10982 if (strcmp (arg, "32") == 0)
316f5878 10983 mips_abi = O32_ABI;
e013f690 10984 else if (strcmp (arg, "o64") == 0)
316f5878 10985 mips_abi = O64_ABI;
e013f690 10986 else if (strcmp (arg, "n32") == 0)
316f5878 10987 mips_abi = N32_ABI;
e013f690
TS
10988 else if (strcmp (arg, "64") == 0)
10989 {
316f5878 10990 mips_abi = N64_ABI;
e013f690
TS
10991 if (! support_64bit_objects())
10992 as_fatal (_("No compiled in support for 64 bit object file "
10993 "format"));
10994 }
10995 else if (strcmp (arg, "eabi") == 0)
316f5878 10996 mips_abi = EABI_ABI;
e013f690 10997 else
da0e507f
TS
10998 {
10999 as_fatal (_("invalid abi -mabi=%s"), arg);
11000 return 0;
11001 }
252b5132 11002 break;
e013f690 11003#endif /* OBJ_ELF */
252b5132 11004
6b76fefe 11005 case OPTION_M7000_HILO_FIX:
b34976b6 11006 mips_7000_hilo_fix = TRUE;
6b76fefe
CM
11007 break;
11008
9ee72ff1 11009 case OPTION_MNO_7000_HILO_FIX:
b34976b6 11010 mips_7000_hilo_fix = FALSE;
6b76fefe
CM
11011 break;
11012
ecb4347a
DJ
11013#ifdef OBJ_ELF
11014 case OPTION_MDEBUG:
b34976b6 11015 mips_flag_mdebug = TRUE;
ecb4347a
DJ
11016 break;
11017
11018 case OPTION_NO_MDEBUG:
b34976b6 11019 mips_flag_mdebug = FALSE;
ecb4347a 11020 break;
dcd410fe
RO
11021
11022 case OPTION_PDR:
11023 mips_flag_pdr = TRUE;
11024 break;
11025
11026 case OPTION_NO_PDR:
11027 mips_flag_pdr = FALSE;
11028 break;
ecb4347a
DJ
11029#endif /* OBJ_ELF */
11030
252b5132
RH
11031 default:
11032 return 0;
11033 }
11034
11035 return 1;
11036}
316f5878
RS
11037\f
11038/* Set up globals to generate code for the ISA or processor
11039 described by INFO. */
252b5132 11040
252b5132 11041static void
17a2f251 11042mips_set_architecture (const struct mips_cpu_info *info)
252b5132 11043{
316f5878 11044 if (info != 0)
252b5132 11045 {
fef14a42
TS
11046 file_mips_arch = info->cpu;
11047 mips_opts.arch = info->cpu;
316f5878 11048 mips_opts.isa = info->isa;
252b5132 11049 }
252b5132
RH
11050}
11051
252b5132 11052
316f5878 11053/* Likewise for tuning. */
252b5132 11054
316f5878 11055static void
17a2f251 11056mips_set_tune (const struct mips_cpu_info *info)
316f5878
RS
11057{
11058 if (info != 0)
fef14a42 11059 mips_tune = info->cpu;
316f5878 11060}
80cc45a5 11061
34ba82a8 11062
252b5132 11063void
17a2f251 11064mips_after_parse_args (void)
e9670677 11065{
fef14a42
TS
11066 const struct mips_cpu_info *arch_info = 0;
11067 const struct mips_cpu_info *tune_info = 0;
11068
e9670677 11069 /* GP relative stuff not working for PE */
6caf9ef4 11070 if (strncmp (TARGET_OS, "pe", 2) == 0)
e9670677 11071 {
6caf9ef4 11072 if (g_switch_seen && g_switch_value != 0)
e9670677
MR
11073 as_bad (_("-G not supported in this configuration."));
11074 g_switch_value = 0;
11075 }
11076
cac012d6
AO
11077 if (mips_abi == NO_ABI)
11078 mips_abi = MIPS_DEFAULT_ABI;
11079
22923709
RS
11080 /* The following code determines the architecture and register size.
11081 Similar code was added to GCC 3.3 (see override_options() in
11082 config/mips/mips.c). The GAS and GCC code should be kept in sync
11083 as much as possible. */
e9670677 11084
316f5878 11085 if (mips_arch_string != 0)
fef14a42 11086 arch_info = mips_parse_cpu ("-march", mips_arch_string);
e9670677 11087
316f5878 11088 if (file_mips_isa != ISA_UNKNOWN)
e9670677 11089 {
316f5878 11090 /* Handle -mipsN. At this point, file_mips_isa contains the
fef14a42 11091 ISA level specified by -mipsN, while arch_info->isa contains
316f5878 11092 the -march selection (if any). */
fef14a42 11093 if (arch_info != 0)
e9670677 11094 {
316f5878
RS
11095 /* -march takes precedence over -mipsN, since it is more descriptive.
11096 There's no harm in specifying both as long as the ISA levels
11097 are the same. */
fef14a42 11098 if (file_mips_isa != arch_info->isa)
316f5878
RS
11099 as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
11100 mips_cpu_info_from_isa (file_mips_isa)->name,
fef14a42 11101 mips_cpu_info_from_isa (arch_info->isa)->name);
e9670677 11102 }
316f5878 11103 else
fef14a42 11104 arch_info = mips_cpu_info_from_isa (file_mips_isa);
e9670677
MR
11105 }
11106
fef14a42
TS
11107 if (arch_info == 0)
11108 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
e9670677 11109
fef14a42 11110 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
316f5878 11111 as_bad ("-march=%s is not compatible with the selected ABI",
fef14a42
TS
11112 arch_info->name);
11113
11114 mips_set_architecture (arch_info);
11115
11116 /* Optimize for file_mips_arch, unless -mtune selects a different processor. */
11117 if (mips_tune_string != 0)
11118 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
e9670677 11119
fef14a42
TS
11120 if (tune_info == 0)
11121 mips_set_tune (arch_info);
11122 else
11123 mips_set_tune (tune_info);
e9670677 11124
316f5878 11125 if (file_mips_gp32 >= 0)
e9670677 11126 {
316f5878
RS
11127 /* The user specified the size of the integer registers. Make sure
11128 it agrees with the ABI and ISA. */
11129 if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
11130 as_bad (_("-mgp64 used with a 32-bit processor"));
11131 else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
11132 as_bad (_("-mgp32 used with a 64-bit ABI"));
11133 else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
11134 as_bad (_("-mgp64 used with a 32-bit ABI"));
e9670677
MR
11135 }
11136 else
11137 {
316f5878
RS
11138 /* Infer the integer register size from the ABI and processor.
11139 Restrict ourselves to 32-bit registers if that's all the
11140 processor has, or if the ABI cannot handle 64-bit registers. */
11141 file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
11142 || !ISA_HAS_64BIT_REGS (mips_opts.isa));
e9670677
MR
11143 }
11144
316f5878
RS
11145 /* ??? GAS treats single-float processors as though they had 64-bit
11146 float registers (although it complains when double-precision
11147 instructions are used). As things stand, saying they have 32-bit
11148 registers would lead to spurious "register must be even" messages.
11149 So here we assume float registers are always the same size as
11150 integer ones, unless the user says otherwise. */
11151 if (file_mips_fp32 < 0)
11152 file_mips_fp32 = file_mips_gp32;
e9670677 11153
316f5878 11154 /* End of GCC-shared inference code. */
e9670677 11155
17a2f251
TS
11156 /* This flag is set when we have a 64-bit capable CPU but use only
11157 32-bit wide registers. Note that EABI does not use it. */
11158 if (ISA_HAS_64BIT_REGS (mips_opts.isa)
11159 && ((mips_abi == NO_ABI && file_mips_gp32 == 1)
11160 || mips_abi == O32_ABI))
316f5878 11161 mips_32bitmode = 1;
e9670677
MR
11162
11163 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
11164 as_bad (_("trap exception not supported at ISA 1"));
11165
e9670677
MR
11166 /* If the selected architecture includes support for ASEs, enable
11167 generation of code for them. */
a4672219 11168 if (mips_opts.mips16 == -1)
fef14a42 11169 mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_arch)) ? 1 : 0;
ffdefa66 11170 if (mips_opts.ase_mips3d == -1)
fef14a42 11171 mips_opts.ase_mips3d = (CPU_HAS_MIPS3D (file_mips_arch)) ? 1 : 0;
ffdefa66 11172 if (mips_opts.ase_mdmx == -1)
fef14a42 11173 mips_opts.ase_mdmx = (CPU_HAS_MDMX (file_mips_arch)) ? 1 : 0;
74cd071d
CF
11174 if (mips_opts.ase_dsp == -1)
11175 mips_opts.ase_dsp = (CPU_HAS_DSP (file_mips_arch)) ? 1 : 0;
ef2e4d86
CF
11176 if (mips_opts.ase_mt == -1)
11177 mips_opts.ase_mt = (CPU_HAS_MT (file_mips_arch)) ? 1 : 0;
e9670677 11178
e9670677 11179 file_mips_isa = mips_opts.isa;
a4672219 11180 file_ase_mips16 = mips_opts.mips16;
e9670677
MR
11181 file_ase_mips3d = mips_opts.ase_mips3d;
11182 file_ase_mdmx = mips_opts.ase_mdmx;
74cd071d 11183 file_ase_dsp = mips_opts.ase_dsp;
ef2e4d86 11184 file_ase_mt = mips_opts.ase_mt;
e9670677
MR
11185 mips_opts.gp32 = file_mips_gp32;
11186 mips_opts.fp32 = file_mips_fp32;
11187
ecb4347a
DJ
11188 if (mips_flag_mdebug < 0)
11189 {
11190#ifdef OBJ_MAYBE_ECOFF
11191 if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
11192 mips_flag_mdebug = 1;
11193 else
11194#endif /* OBJ_MAYBE_ECOFF */
11195 mips_flag_mdebug = 0;
11196 }
e9670677
MR
11197}
11198\f
11199void
17a2f251 11200mips_init_after_args (void)
252b5132
RH
11201{
11202 /* initialize opcodes */
11203 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
beae10d5 11204 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
252b5132
RH
11205}
11206
11207long
17a2f251 11208md_pcrel_from (fixS *fixP)
252b5132 11209{
a7ebbfdf
TS
11210 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
11211 switch (fixP->fx_r_type)
11212 {
11213 case BFD_RELOC_16_PCREL_S2:
11214 case BFD_RELOC_MIPS_JMP:
11215 /* Return the address of the delay slot. */
11216 return addr + 4;
11217 default:
11218 return addr;
11219 }
252b5132
RH
11220}
11221
252b5132
RH
11222/* This is called before the symbol table is processed. In order to
11223 work with gcc when using mips-tfile, we must keep all local labels.
11224 However, in other cases, we want to discard them. If we were
11225 called with -g, but we didn't see any debugging information, it may
11226 mean that gcc is smuggling debugging information through to
11227 mips-tfile, in which case we must generate all local labels. */
11228
11229void
17a2f251 11230mips_frob_file_before_adjust (void)
252b5132
RH
11231{
11232#ifndef NO_ECOFF_DEBUGGING
11233 if (ECOFF_DEBUGGING
11234 && mips_debug != 0
11235 && ! ecoff_debugging_seen)
11236 flag_keep_locals = 1;
11237#endif
11238}
11239
3b91255e 11240/* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
55cf6793 11241 the corresponding LO16 reloc. This is called before md_apply_fix and
3b91255e
RS
11242 tc_gen_reloc. Unmatched relocs can only be generated by use of explicit
11243 relocation operators.
11244
11245 For our purposes, a %lo() expression matches a %got() or %hi()
11246 expression if:
11247
11248 (a) it refers to the same symbol; and
11249 (b) the offset applied in the %lo() expression is no lower than
11250 the offset applied in the %got() or %hi().
11251
11252 (b) allows us to cope with code like:
11253
11254 lui $4,%hi(foo)
11255 lh $4,%lo(foo+2)($4)
11256
11257 ...which is legal on RELA targets, and has a well-defined behaviour
11258 if the user knows that adding 2 to "foo" will not induce a carry to
11259 the high 16 bits.
11260
11261 When several %lo()s match a particular %got() or %hi(), we use the
11262 following rules to distinguish them:
11263
11264 (1) %lo()s with smaller offsets are a better match than %lo()s with
11265 higher offsets.
11266
11267 (2) %lo()s with no matching %got() or %hi() are better than those
11268 that already have a matching %got() or %hi().
11269
11270 (3) later %lo()s are better than earlier %lo()s.
11271
11272 These rules are applied in order.
11273
11274 (1) means, among other things, that %lo()s with identical offsets are
11275 chosen if they exist.
11276
11277 (2) means that we won't associate several high-part relocations with
11278 the same low-part relocation unless there's no alternative. Having
11279 several high parts for the same low part is a GNU extension; this rule
11280 allows careful users to avoid it.
11281
11282 (3) is purely cosmetic. mips_hi_fixup_list is is in reverse order,
11283 with the last high-part relocation being at the front of the list.
11284 It therefore makes sense to choose the last matching low-part
11285 relocation, all other things being equal. It's also easier
11286 to code that way. */
252b5132
RH
11287
11288void
17a2f251 11289mips_frob_file (void)
252b5132
RH
11290{
11291 struct mips_hi_fixup *l;
11292
11293 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
11294 {
11295 segment_info_type *seginfo;
3b91255e
RS
11296 bfd_boolean matched_lo_p;
11297 fixS **hi_pos, **lo_pos, **pos;
252b5132 11298
5919d012 11299 assert (reloc_needs_lo_p (l->fixp->fx_r_type));
252b5132 11300
5919d012
RS
11301 /* If a GOT16 relocation turns out to be against a global symbol,
11302 there isn't supposed to be a matching LO. */
11303 if (l->fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
11304 && !pic_need_relax (l->fixp->fx_addsy, l->seg))
11305 continue;
11306
11307 /* Check quickly whether the next fixup happens to be a matching %lo. */
11308 if (fixup_has_matching_lo_p (l->fixp))
252b5132
RH
11309 continue;
11310
252b5132 11311 seginfo = seg_info (l->seg);
252b5132 11312
3b91255e
RS
11313 /* Set HI_POS to the position of this relocation in the chain.
11314 Set LO_POS to the position of the chosen low-part relocation.
11315 MATCHED_LO_P is true on entry to the loop if *POS is a low-part
11316 relocation that matches an immediately-preceding high-part
11317 relocation. */
11318 hi_pos = NULL;
11319 lo_pos = NULL;
11320 matched_lo_p = FALSE;
11321 for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
11322 {
11323 if (*pos == l->fixp)
11324 hi_pos = pos;
11325
704803a9
MR
11326 if (((*pos)->fx_r_type == BFD_RELOC_LO16
11327 || (*pos)->fx_r_type == BFD_RELOC_MIPS16_LO16)
3b91255e
RS
11328 && (*pos)->fx_addsy == l->fixp->fx_addsy
11329 && (*pos)->fx_offset >= l->fixp->fx_offset
11330 && (lo_pos == NULL
11331 || (*pos)->fx_offset < (*lo_pos)->fx_offset
11332 || (!matched_lo_p
11333 && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
11334 lo_pos = pos;
11335
11336 matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
11337 && fixup_has_matching_lo_p (*pos));
11338 }
11339
11340 /* If we found a match, remove the high-part relocation from its
11341 current position and insert it before the low-part relocation.
11342 Make the offsets match so that fixup_has_matching_lo_p()
11343 will return true.
11344
11345 We don't warn about unmatched high-part relocations since some
11346 versions of gcc have been known to emit dead "lui ...%hi(...)"
11347 instructions. */
11348 if (lo_pos != NULL)
11349 {
11350 l->fixp->fx_offset = (*lo_pos)->fx_offset;
11351 if (l->fixp->fx_next != *lo_pos)
252b5132 11352 {
3b91255e
RS
11353 *hi_pos = l->fixp->fx_next;
11354 l->fixp->fx_next = *lo_pos;
11355 *lo_pos = l->fixp;
252b5132 11356 }
252b5132
RH
11357 }
11358 }
11359}
11360
3e722fb5 11361/* We may have combined relocations without symbols in the N32/N64 ABI.
f6688943 11362 We have to prevent gas from dropping them. */
252b5132 11363
252b5132 11364int
17a2f251 11365mips_force_relocation (fixS *fixp)
252b5132 11366{
ae6063d4 11367 if (generic_force_reloc (fixp))
252b5132
RH
11368 return 1;
11369
f6688943
TS
11370 if (HAVE_NEWABI
11371 && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
11372 && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
11373 || fixp->fx_r_type == BFD_RELOC_HI16_S
11374 || fixp->fx_r_type == BFD_RELOC_LO16))
11375 return 1;
11376
3e722fb5 11377 return 0;
252b5132
RH
11378}
11379
11380/* Apply a fixup to the object file. */
11381
94f592af 11382void
55cf6793 11383md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 11384{
874e8986 11385 bfd_byte *buf;
98aa84af 11386 long insn;
a7ebbfdf 11387 reloc_howto_type *howto;
252b5132 11388
a7ebbfdf
TS
11389 /* We ignore generic BFD relocations we don't know about. */
11390 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
11391 if (! howto)
11392 return;
65551fa4 11393
252b5132
RH
11394 assert (fixP->fx_size == 4
11395 || fixP->fx_r_type == BFD_RELOC_16
11396 || fixP->fx_r_type == BFD_RELOC_64
f6688943
TS
11397 || fixP->fx_r_type == BFD_RELOC_CTOR
11398 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
252b5132 11399 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
a7ebbfdf 11400 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY);
252b5132 11401
a7ebbfdf 11402 buf = (bfd_byte *) (fixP->fx_frag->fr_literal + fixP->fx_where);
252b5132 11403
bad36eac 11404 assert (! fixP->fx_pcrel || fixP->fx_r_type == BFD_RELOC_16_PCREL_S2);
b1dca8ee
RS
11405
11406 /* Don't treat parts of a composite relocation as done. There are two
11407 reasons for this:
11408
11409 (1) The second and third parts will be against 0 (RSS_UNDEF) but
11410 should nevertheless be emitted if the first part is.
11411
11412 (2) In normal usage, composite relocations are never assembly-time
11413 constants. The easiest way of dealing with the pathological
11414 exceptions is to generate a relocation against STN_UNDEF and
11415 leave everything up to the linker. */
bad36eac 11416 if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel && fixP->fx_tcbit == 0)
252b5132
RH
11417 fixP->fx_done = 1;
11418
11419 switch (fixP->fx_r_type)
11420 {
3f98094e
DJ
11421 case BFD_RELOC_MIPS_TLS_GD:
11422 case BFD_RELOC_MIPS_TLS_LDM:
11423 case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
11424 case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
11425 case BFD_RELOC_MIPS_TLS_GOTTPREL:
11426 case BFD_RELOC_MIPS_TLS_TPREL_HI16:
11427 case BFD_RELOC_MIPS_TLS_TPREL_LO16:
11428 S_SET_THREAD_LOCAL (fixP->fx_addsy);
11429 /* fall through */
11430
252b5132 11431 case BFD_RELOC_MIPS_JMP:
e369bcce
TS
11432 case BFD_RELOC_MIPS_SHIFT5:
11433 case BFD_RELOC_MIPS_SHIFT6:
11434 case BFD_RELOC_MIPS_GOT_DISP:
11435 case BFD_RELOC_MIPS_GOT_PAGE:
11436 case BFD_RELOC_MIPS_GOT_OFST:
11437 case BFD_RELOC_MIPS_SUB:
11438 case BFD_RELOC_MIPS_INSERT_A:
11439 case BFD_RELOC_MIPS_INSERT_B:
11440 case BFD_RELOC_MIPS_DELETE:
11441 case BFD_RELOC_MIPS_HIGHEST:
11442 case BFD_RELOC_MIPS_HIGHER:
11443 case BFD_RELOC_MIPS_SCN_DISP:
11444 case BFD_RELOC_MIPS_REL16:
11445 case BFD_RELOC_MIPS_RELGOT:
11446 case BFD_RELOC_MIPS_JALR:
252b5132
RH
11447 case BFD_RELOC_HI16:
11448 case BFD_RELOC_HI16_S:
cdf6fd85 11449 case BFD_RELOC_GPREL16:
252b5132
RH
11450 case BFD_RELOC_MIPS_LITERAL:
11451 case BFD_RELOC_MIPS_CALL16:
11452 case BFD_RELOC_MIPS_GOT16:
cdf6fd85 11453 case BFD_RELOC_GPREL32:
252b5132
RH
11454 case BFD_RELOC_MIPS_GOT_HI16:
11455 case BFD_RELOC_MIPS_GOT_LO16:
11456 case BFD_RELOC_MIPS_CALL_HI16:
11457 case BFD_RELOC_MIPS_CALL_LO16:
11458 case BFD_RELOC_MIPS16_GPREL:
d6f16593
MR
11459 case BFD_RELOC_MIPS16_HI16:
11460 case BFD_RELOC_MIPS16_HI16_S:
252b5132
RH
11461 /* Nothing needed to do. The value comes from the reloc entry */
11462 break;
11463
11464 case BFD_RELOC_MIPS16_JMP:
11465 /* We currently always generate a reloc against a symbol, which
11466 means that we don't want an addend even if the symbol is
11467 defined. */
a7ebbfdf 11468 *valP = 0;
252b5132
RH
11469 break;
11470
252b5132
RH
11471 case BFD_RELOC_64:
11472 /* This is handled like BFD_RELOC_32, but we output a sign
11473 extended value if we are only 32 bits. */
3e722fb5 11474 if (fixP->fx_done)
252b5132
RH
11475 {
11476 if (8 <= sizeof (valueT))
2132e3a3 11477 md_number_to_chars ((char *) buf, *valP, 8);
252b5132
RH
11478 else
11479 {
a7ebbfdf 11480 valueT hiv;
252b5132 11481
a7ebbfdf 11482 if ((*valP & 0x80000000) != 0)
252b5132
RH
11483 hiv = 0xffffffff;
11484 else
11485 hiv = 0;
b215186b 11486 md_number_to_chars ((char *)(buf + (target_big_endian ? 4 : 0)),
a7ebbfdf 11487 *valP, 4);
b215186b 11488 md_number_to_chars ((char *)(buf + (target_big_endian ? 0 : 4)),
a7ebbfdf 11489 hiv, 4);
252b5132
RH
11490 }
11491 }
11492 break;
11493
056350c6 11494 case BFD_RELOC_RVA:
252b5132
RH
11495 case BFD_RELOC_32:
11496 /* If we are deleting this reloc entry, we must fill in the
11497 value now. This can happen if we have a .word which is not
3e722fb5
CD
11498 resolved when it appears but is later defined. */
11499 if (fixP->fx_done)
2132e3a3 11500 md_number_to_chars ((char *) buf, *valP, 4);
252b5132
RH
11501 break;
11502
11503 case BFD_RELOC_16:
11504 /* If we are deleting this reloc entry, we must fill in the
11505 value now. */
252b5132 11506 if (fixP->fx_done)
2132e3a3 11507 md_number_to_chars ((char *) buf, *valP, 2);
252b5132
RH
11508 break;
11509
11510 case BFD_RELOC_LO16:
d6f16593 11511 case BFD_RELOC_MIPS16_LO16:
3e722fb5
CD
11512 /* FIXME: Now that embedded-PIC is gone, some of this code/comment
11513 may be safe to remove, but if so it's not obvious. */
252b5132
RH
11514 /* When handling an embedded PIC switch statement, we can wind
11515 up deleting a LO16 reloc. See the 'o' case in mips_ip. */
11516 if (fixP->fx_done)
11517 {
a7ebbfdf 11518 if (*valP + 0x8000 > 0xffff)
252b5132
RH
11519 as_bad_where (fixP->fx_file, fixP->fx_line,
11520 _("relocation overflow"));
252b5132
RH
11521 if (target_big_endian)
11522 buf += 2;
2132e3a3 11523 md_number_to_chars ((char *) buf, *valP, 2);
252b5132
RH
11524 }
11525 break;
11526
11527 case BFD_RELOC_16_PCREL_S2:
a7ebbfdf 11528 if ((*valP & 0x3) != 0)
cb56d3d3 11529 as_bad_where (fixP->fx_file, fixP->fx_line,
bad36eac 11530 _("Branch to misaligned address (%lx)"), (long) *valP);
cb56d3d3 11531
252b5132
RH
11532 /*
11533 * We need to save the bits in the instruction since fixup_segment()
11534 * might be deleting the relocation entry (i.e., a branch within
11535 * the current segment).
11536 */
a7ebbfdf 11537 if (! fixP->fx_done)
bb2d6cd7 11538 break;
252b5132
RH
11539
11540 /* update old instruction data */
252b5132
RH
11541 if (target_big_endian)
11542 insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
11543 else
11544 insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
11545
a7ebbfdf
TS
11546 if (*valP + 0x20000 <= 0x3ffff)
11547 {
11548 insn |= (*valP >> 2) & 0xffff;
2132e3a3 11549 md_number_to_chars ((char *) buf, insn, 4);
a7ebbfdf
TS
11550 }
11551 else if (mips_pic == NO_PIC
11552 && fixP->fx_done
11553 && fixP->fx_frag->fr_address >= text_section->vma
11554 && (fixP->fx_frag->fr_address
587aac4e 11555 < text_section->vma + bfd_get_section_size (text_section))
a7ebbfdf
TS
11556 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
11557 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
11558 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
252b5132
RH
11559 {
11560 /* The branch offset is too large. If this is an
11561 unconditional branch, and we are not generating PIC code,
11562 we can convert it to an absolute jump instruction. */
a7ebbfdf
TS
11563 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
11564 insn = 0x0c000000; /* jal */
252b5132 11565 else
a7ebbfdf
TS
11566 insn = 0x08000000; /* j */
11567 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
11568 fixP->fx_done = 0;
11569 fixP->fx_addsy = section_symbol (text_section);
11570 *valP += md_pcrel_from (fixP);
2132e3a3 11571 md_number_to_chars ((char *) buf, insn, 4);
a7ebbfdf
TS
11572 }
11573 else
11574 {
11575 /* If we got here, we have branch-relaxation disabled,
11576 and there's nothing we can do to fix this instruction
11577 without turning it into a longer sequence. */
11578 as_bad_where (fixP->fx_file, fixP->fx_line,
11579 _("Branch out of range"));
252b5132 11580 }
252b5132
RH
11581 break;
11582
11583 case BFD_RELOC_VTABLE_INHERIT:
11584 fixP->fx_done = 0;
11585 if (fixP->fx_addsy
11586 && !S_IS_DEFINED (fixP->fx_addsy)
11587 && !S_IS_WEAK (fixP->fx_addsy))
11588 S_SET_WEAK (fixP->fx_addsy);
11589 break;
11590
11591 case BFD_RELOC_VTABLE_ENTRY:
11592 fixP->fx_done = 0;
11593 break;
11594
11595 default:
11596 internalError ();
11597 }
a7ebbfdf
TS
11598
11599 /* Remember value for tc_gen_reloc. */
11600 fixP->fx_addnumber = *valP;
252b5132
RH
11601}
11602
252b5132 11603static symbolS *
17a2f251 11604get_symbol (void)
252b5132
RH
11605{
11606 int c;
11607 char *name;
11608 symbolS *p;
11609
11610 name = input_line_pointer;
11611 c = get_symbol_end ();
11612 p = (symbolS *) symbol_find_or_make (name);
11613 *input_line_pointer = c;
11614 return p;
11615}
11616
11617/* Align the current frag to a given power of two. The MIPS assembler
11618 also automatically adjusts any preceding label. */
11619
11620static void
17a2f251 11621mips_align (int to, int fill, symbolS *label)
252b5132 11622{
7d10b47d 11623 mips_emit_delays ();
252b5132
RH
11624 frag_align (to, fill, 0);
11625 record_alignment (now_seg, to);
11626 if (label != NULL)
11627 {
11628 assert (S_GET_SEGMENT (label) == now_seg);
49309057 11629 symbol_set_frag (label, frag_now);
252b5132
RH
11630 S_SET_VALUE (label, (valueT) frag_now_fix ());
11631 }
11632}
11633
11634/* Align to a given power of two. .align 0 turns off the automatic
11635 alignment used by the data creating pseudo-ops. */
11636
11637static void
17a2f251 11638s_align (int x ATTRIBUTE_UNUSED)
252b5132
RH
11639{
11640 register int temp;
11641 register long temp_fill;
11642 long max_alignment = 15;
11643
11644 /*
11645
67c1ffbe 11646 o Note that the assembler pulls down any immediately preceding label
252b5132
RH
11647 to the aligned address.
11648 o It's not documented but auto alignment is reinstated by
11649 a .align pseudo instruction.
11650 o Note also that after auto alignment is turned off the mips assembler
11651 issues an error on attempt to assemble an improperly aligned data item.
11652 We don't.
11653
11654 */
11655
11656 temp = get_absolute_expression ();
11657 if (temp > max_alignment)
11658 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
11659 else if (temp < 0)
11660 {
11661 as_warn (_("Alignment negative: 0 assumed."));
11662 temp = 0;
11663 }
11664 if (*input_line_pointer == ',')
11665 {
f9419b05 11666 ++input_line_pointer;
252b5132
RH
11667 temp_fill = get_absolute_expression ();
11668 }
11669 else
11670 temp_fill = 0;
11671 if (temp)
11672 {
11673 auto_align = 1;
11674 mips_align (temp, (int) temp_fill,
11675 insn_labels != NULL ? insn_labels->label : NULL);
11676 }
11677 else
11678 {
11679 auto_align = 0;
11680 }
11681
11682 demand_empty_rest_of_line ();
11683}
11684
252b5132 11685static void
17a2f251 11686s_change_sec (int sec)
252b5132
RH
11687{
11688 segT seg;
11689
252b5132
RH
11690#ifdef OBJ_ELF
11691 /* The ELF backend needs to know that we are changing sections, so
11692 that .previous works correctly. We could do something like check
b6ff326e 11693 for an obj_section_change_hook macro, but that might be confusing
252b5132
RH
11694 as it would not be appropriate to use it in the section changing
11695 functions in read.c, since obj-elf.c intercepts those. FIXME:
11696 This should be cleaner, somehow. */
11697 obj_elf_section_change_hook ();
11698#endif
11699
7d10b47d 11700 mips_emit_delays ();
252b5132
RH
11701 switch (sec)
11702 {
11703 case 't':
11704 s_text (0);
11705 break;
11706 case 'd':
11707 s_data (0);
11708 break;
11709 case 'b':
11710 subseg_set (bss_section, (subsegT) get_absolute_expression ());
11711 demand_empty_rest_of_line ();
11712 break;
11713
11714 case 'r':
4d0d148d
TS
11715 seg = subseg_new (RDATA_SECTION_NAME,
11716 (subsegT) get_absolute_expression ());
11717 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
252b5132 11718 {
4d0d148d
TS
11719 bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
11720 | SEC_READONLY | SEC_RELOC
11721 | SEC_DATA));
11722 if (strcmp (TARGET_OS, "elf") != 0)
11723 record_alignment (seg, 4);
252b5132 11724 }
4d0d148d 11725 demand_empty_rest_of_line ();
252b5132
RH
11726 break;
11727
11728 case 's':
4d0d148d
TS
11729 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
11730 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
252b5132 11731 {
4d0d148d
TS
11732 bfd_set_section_flags (stdoutput, seg,
11733 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
11734 if (strcmp (TARGET_OS, "elf") != 0)
11735 record_alignment (seg, 4);
252b5132 11736 }
4d0d148d
TS
11737 demand_empty_rest_of_line ();
11738 break;
252b5132
RH
11739 }
11740
11741 auto_align = 1;
11742}
b34976b6 11743
cca86cc8 11744void
17a2f251 11745s_change_section (int ignore ATTRIBUTE_UNUSED)
cca86cc8 11746{
7ed4a06a 11747#ifdef OBJ_ELF
cca86cc8
SC
11748 char *section_name;
11749 char c;
684022ea 11750 char next_c = 0;
cca86cc8
SC
11751 int section_type;
11752 int section_flag;
11753 int section_entry_size;
11754 int section_alignment;
b34976b6 11755
7ed4a06a
TS
11756 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11757 return;
11758
cca86cc8
SC
11759 section_name = input_line_pointer;
11760 c = get_symbol_end ();
a816d1ed
AO
11761 if (c)
11762 next_c = *(input_line_pointer + 1);
cca86cc8 11763
4cf0dd0d
TS
11764 /* Do we have .section Name<,"flags">? */
11765 if (c != ',' || (c == ',' && next_c == '"'))
cca86cc8 11766 {
4cf0dd0d
TS
11767 /* just after name is now '\0'. */
11768 *input_line_pointer = c;
cca86cc8
SC
11769 input_line_pointer = section_name;
11770 obj_elf_section (ignore);
11771 return;
11772 }
11773 input_line_pointer++;
11774
11775 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
11776 if (c == ',')
11777 section_type = get_absolute_expression ();
11778 else
11779 section_type = 0;
11780 if (*input_line_pointer++ == ',')
11781 section_flag = get_absolute_expression ();
11782 else
11783 section_flag = 0;
11784 if (*input_line_pointer++ == ',')
11785 section_entry_size = get_absolute_expression ();
11786 else
11787 section_entry_size = 0;
11788 if (*input_line_pointer++ == ',')
11789 section_alignment = get_absolute_expression ();
11790 else
11791 section_alignment = 0;
11792
a816d1ed
AO
11793 section_name = xstrdup (section_name);
11794
8ab8a5c8
RS
11795 /* When using the generic form of .section (as implemented by obj-elf.c),
11796 there's no way to set the section type to SHT_MIPS_DWARF. Users have
11797 traditionally had to fall back on the more common @progbits instead.
11798
11799 There's nothing really harmful in this, since bfd will correct
11800 SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file. But it
11801 means that, for backwards compatibiltiy, the special_section entries
11802 for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
11803
11804 Even so, we shouldn't force users of the MIPS .section syntax to
11805 incorrectly label the sections as SHT_PROGBITS. The best compromise
11806 seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
11807 generic type-checking code. */
11808 if (section_type == SHT_MIPS_DWARF)
11809 section_type = SHT_PROGBITS;
11810
cca86cc8
SC
11811 obj_elf_change_section (section_name, section_type, section_flag,
11812 section_entry_size, 0, 0, 0);
a816d1ed
AO
11813
11814 if (now_seg->name != section_name)
11815 free (section_name);
7ed4a06a 11816#endif /* OBJ_ELF */
cca86cc8 11817}
252b5132
RH
11818
11819void
17a2f251 11820mips_enable_auto_align (void)
252b5132
RH
11821{
11822 auto_align = 1;
11823}
11824
11825static void
17a2f251 11826s_cons (int log_size)
252b5132
RH
11827{
11828 symbolS *label;
11829
11830 label = insn_labels != NULL ? insn_labels->label : NULL;
7d10b47d 11831 mips_emit_delays ();
252b5132
RH
11832 if (log_size > 0 && auto_align)
11833 mips_align (log_size, 0, label);
11834 mips_clear_insn_labels ();
11835 cons (1 << log_size);
11836}
11837
11838static void
17a2f251 11839s_float_cons (int type)
252b5132
RH
11840{
11841 symbolS *label;
11842
11843 label = insn_labels != NULL ? insn_labels->label : NULL;
11844
7d10b47d 11845 mips_emit_delays ();
252b5132
RH
11846
11847 if (auto_align)
49309057
ILT
11848 {
11849 if (type == 'd')
11850 mips_align (3, 0, label);
11851 else
11852 mips_align (2, 0, label);
11853 }
252b5132
RH
11854
11855 mips_clear_insn_labels ();
11856
11857 float_cons (type);
11858}
11859
11860/* Handle .globl. We need to override it because on Irix 5 you are
11861 permitted to say
11862 .globl foo .text
11863 where foo is an undefined symbol, to mean that foo should be
11864 considered to be the address of a function. */
11865
11866static void
17a2f251 11867s_mips_globl (int x ATTRIBUTE_UNUSED)
252b5132
RH
11868{
11869 char *name;
11870 int c;
11871 symbolS *symbolP;
11872 flagword flag;
11873
8a06b769 11874 do
252b5132 11875 {
8a06b769 11876 name = input_line_pointer;
252b5132 11877 c = get_symbol_end ();
8a06b769
TS
11878 symbolP = symbol_find_or_make (name);
11879 S_SET_EXTERNAL (symbolP);
11880
252b5132 11881 *input_line_pointer = c;
8a06b769 11882 SKIP_WHITESPACE ();
252b5132 11883
8a06b769
TS
11884 /* On Irix 5, every global symbol that is not explicitly labelled as
11885 being a function is apparently labelled as being an object. */
11886 flag = BSF_OBJECT;
252b5132 11887
8a06b769
TS
11888 if (!is_end_of_line[(unsigned char) *input_line_pointer]
11889 && (*input_line_pointer != ','))
11890 {
11891 char *secname;
11892 asection *sec;
11893
11894 secname = input_line_pointer;
11895 c = get_symbol_end ();
11896 sec = bfd_get_section_by_name (stdoutput, secname);
11897 if (sec == NULL)
11898 as_bad (_("%s: no such section"), secname);
11899 *input_line_pointer = c;
11900
11901 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
11902 flag = BSF_FUNCTION;
11903 }
11904
11905 symbol_get_bfdsym (symbolP)->flags |= flag;
11906
11907 c = *input_line_pointer;
11908 if (c == ',')
11909 {
11910 input_line_pointer++;
11911 SKIP_WHITESPACE ();
11912 if (is_end_of_line[(unsigned char) *input_line_pointer])
11913 c = '\n';
11914 }
11915 }
11916 while (c == ',');
252b5132 11917
252b5132
RH
11918 demand_empty_rest_of_line ();
11919}
11920
11921static void
17a2f251 11922s_option (int x ATTRIBUTE_UNUSED)
252b5132
RH
11923{
11924 char *opt;
11925 char c;
11926
11927 opt = input_line_pointer;
11928 c = get_symbol_end ();
11929
11930 if (*opt == 'O')
11931 {
11932 /* FIXME: What does this mean? */
11933 }
11934 else if (strncmp (opt, "pic", 3) == 0)
11935 {
11936 int i;
11937
11938 i = atoi (opt + 3);
11939 if (i == 0)
11940 mips_pic = NO_PIC;
11941 else if (i == 2)
143d77c5 11942 {
252b5132 11943 mips_pic = SVR4_PIC;
143d77c5
EC
11944 mips_abicalls = TRUE;
11945 }
252b5132
RH
11946 else
11947 as_bad (_(".option pic%d not supported"), i);
11948
4d0d148d 11949 if (mips_pic == SVR4_PIC)
252b5132
RH
11950 {
11951 if (g_switch_seen && g_switch_value != 0)
11952 as_warn (_("-G may not be used with SVR4 PIC code"));
11953 g_switch_value = 0;
11954 bfd_set_gp_size (stdoutput, 0);
11955 }
11956 }
11957 else
11958 as_warn (_("Unrecognized option \"%s\""), opt);
11959
11960 *input_line_pointer = c;
11961 demand_empty_rest_of_line ();
11962}
11963
11964/* This structure is used to hold a stack of .set values. */
11965
e972090a
NC
11966struct mips_option_stack
11967{
252b5132
RH
11968 struct mips_option_stack *next;
11969 struct mips_set_options options;
11970};
11971
11972static struct mips_option_stack *mips_opts_stack;
11973
11974/* Handle the .set pseudo-op. */
11975
11976static void
17a2f251 11977s_mipsset (int x ATTRIBUTE_UNUSED)
252b5132
RH
11978{
11979 char *name = input_line_pointer, ch;
11980
11981 while (!is_end_of_line[(unsigned char) *input_line_pointer])
f9419b05 11982 ++input_line_pointer;
252b5132
RH
11983 ch = *input_line_pointer;
11984 *input_line_pointer = '\0';
11985
11986 if (strcmp (name, "reorder") == 0)
11987 {
7d10b47d
RS
11988 if (mips_opts.noreorder)
11989 end_noreorder ();
252b5132
RH
11990 }
11991 else if (strcmp (name, "noreorder") == 0)
11992 {
7d10b47d
RS
11993 if (!mips_opts.noreorder)
11994 start_noreorder ();
252b5132
RH
11995 }
11996 else if (strcmp (name, "at") == 0)
11997 {
11998 mips_opts.noat = 0;
11999 }
12000 else if (strcmp (name, "noat") == 0)
12001 {
12002 mips_opts.noat = 1;
12003 }
12004 else if (strcmp (name, "macro") == 0)
12005 {
12006 mips_opts.warn_about_macros = 0;
12007 }
12008 else if (strcmp (name, "nomacro") == 0)
12009 {
12010 if (mips_opts.noreorder == 0)
12011 as_bad (_("`noreorder' must be set before `nomacro'"));
12012 mips_opts.warn_about_macros = 1;
12013 }
12014 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
12015 {
12016 mips_opts.nomove = 0;
12017 }
12018 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
12019 {
12020 mips_opts.nomove = 1;
12021 }
12022 else if (strcmp (name, "bopt") == 0)
12023 {
12024 mips_opts.nobopt = 0;
12025 }
12026 else if (strcmp (name, "nobopt") == 0)
12027 {
12028 mips_opts.nobopt = 1;
12029 }
12030 else if (strcmp (name, "mips16") == 0
12031 || strcmp (name, "MIPS-16") == 0)
12032 mips_opts.mips16 = 1;
12033 else if (strcmp (name, "nomips16") == 0
12034 || strcmp (name, "noMIPS-16") == 0)
12035 mips_opts.mips16 = 0;
1f25f5d3
CD
12036 else if (strcmp (name, "mips3d") == 0)
12037 mips_opts.ase_mips3d = 1;
12038 else if (strcmp (name, "nomips3d") == 0)
12039 mips_opts.ase_mips3d = 0;
a4672219
TS
12040 else if (strcmp (name, "mdmx") == 0)
12041 mips_opts.ase_mdmx = 1;
12042 else if (strcmp (name, "nomdmx") == 0)
12043 mips_opts.ase_mdmx = 0;
74cd071d
CF
12044 else if (strcmp (name, "dsp") == 0)
12045 mips_opts.ase_dsp = 1;
12046 else if (strcmp (name, "nodsp") == 0)
12047 mips_opts.ase_dsp = 0;
ef2e4d86
CF
12048 else if (strcmp (name, "mt") == 0)
12049 mips_opts.ase_mt = 1;
12050 else if (strcmp (name, "nomt") == 0)
12051 mips_opts.ase_mt = 0;
1a2c1fad 12052 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
252b5132 12053 {
af7ee8bf 12054 int reset = 0;
252b5132 12055
1a2c1fad
CD
12056 /* Permit the user to change the ISA and architecture on the fly.
12057 Needless to say, misuse can cause serious problems. */
81a21e38 12058 if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
af7ee8bf
CD
12059 {
12060 reset = 1;
12061 mips_opts.isa = file_mips_isa;
1a2c1fad 12062 mips_opts.arch = file_mips_arch;
1a2c1fad
CD
12063 }
12064 else if (strncmp (name, "arch=", 5) == 0)
12065 {
12066 const struct mips_cpu_info *p;
12067
12068 p = mips_parse_cpu("internal use", name + 5);
12069 if (!p)
12070 as_bad (_("unknown architecture %s"), name + 5);
12071 else
12072 {
12073 mips_opts.arch = p->cpu;
12074 mips_opts.isa = p->isa;
12075 }
12076 }
81a21e38
TS
12077 else if (strncmp (name, "mips", 4) == 0)
12078 {
12079 const struct mips_cpu_info *p;
12080
12081 p = mips_parse_cpu("internal use", name);
12082 if (!p)
12083 as_bad (_("unknown ISA level %s"), name + 4);
12084 else
12085 {
12086 mips_opts.arch = p->cpu;
12087 mips_opts.isa = p->isa;
12088 }
12089 }
af7ee8bf 12090 else
81a21e38 12091 as_bad (_("unknown ISA or architecture %s"), name);
af7ee8bf
CD
12092
12093 switch (mips_opts.isa)
98d3f06f
KH
12094 {
12095 case 0:
98d3f06f 12096 break;
af7ee8bf
CD
12097 case ISA_MIPS1:
12098 case ISA_MIPS2:
12099 case ISA_MIPS32:
12100 case ISA_MIPS32R2:
98d3f06f
KH
12101 mips_opts.gp32 = 1;
12102 mips_opts.fp32 = 1;
12103 break;
af7ee8bf
CD
12104 case ISA_MIPS3:
12105 case ISA_MIPS4:
12106 case ISA_MIPS5:
12107 case ISA_MIPS64:
5f74bc13 12108 case ISA_MIPS64R2:
98d3f06f
KH
12109 mips_opts.gp32 = 0;
12110 mips_opts.fp32 = 0;
12111 break;
12112 default:
12113 as_bad (_("unknown ISA level %s"), name + 4);
12114 break;
12115 }
af7ee8bf 12116 if (reset)
98d3f06f 12117 {
af7ee8bf
CD
12118 mips_opts.gp32 = file_mips_gp32;
12119 mips_opts.fp32 = file_mips_fp32;
98d3f06f 12120 }
252b5132
RH
12121 }
12122 else if (strcmp (name, "autoextend") == 0)
12123 mips_opts.noautoextend = 0;
12124 else if (strcmp (name, "noautoextend") == 0)
12125 mips_opts.noautoextend = 1;
12126 else if (strcmp (name, "push") == 0)
12127 {
12128 struct mips_option_stack *s;
12129
12130 s = (struct mips_option_stack *) xmalloc (sizeof *s);
12131 s->next = mips_opts_stack;
12132 s->options = mips_opts;
12133 mips_opts_stack = s;
12134 }
12135 else if (strcmp (name, "pop") == 0)
12136 {
12137 struct mips_option_stack *s;
12138
12139 s = mips_opts_stack;
12140 if (s == NULL)
12141 as_bad (_(".set pop with no .set push"));
12142 else
12143 {
12144 /* If we're changing the reorder mode we need to handle
12145 delay slots correctly. */
12146 if (s->options.noreorder && ! mips_opts.noreorder)
7d10b47d 12147 start_noreorder ();
252b5132 12148 else if (! s->options.noreorder && mips_opts.noreorder)
7d10b47d 12149 end_noreorder ();
252b5132
RH
12150
12151 mips_opts = s->options;
12152 mips_opts_stack = s->next;
12153 free (s);
12154 }
12155 }
aed1a261
RS
12156 else if (strcmp (name, "sym32") == 0)
12157 mips_opts.sym32 = TRUE;
12158 else if (strcmp (name, "nosym32") == 0)
12159 mips_opts.sym32 = FALSE;
252b5132
RH
12160 else
12161 {
12162 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
12163 }
12164 *input_line_pointer = ch;
12165 demand_empty_rest_of_line ();
12166}
12167
12168/* Handle the .abicalls pseudo-op. I believe this is equivalent to
12169 .option pic2. It means to generate SVR4 PIC calls. */
12170
12171static void
17a2f251 12172s_abicalls (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
12173{
12174 mips_pic = SVR4_PIC;
143d77c5 12175 mips_abicalls = TRUE;
4d0d148d
TS
12176
12177 if (g_switch_seen && g_switch_value != 0)
12178 as_warn (_("-G may not be used with SVR4 PIC code"));
12179 g_switch_value = 0;
12180
252b5132
RH
12181 bfd_set_gp_size (stdoutput, 0);
12182 demand_empty_rest_of_line ();
12183}
12184
12185/* Handle the .cpload pseudo-op. This is used when generating SVR4
12186 PIC code. It sets the $gp register for the function based on the
12187 function address, which is in the register named in the argument.
12188 This uses a relocation against _gp_disp, which is handled specially
12189 by the linker. The result is:
12190 lui $gp,%hi(_gp_disp)
12191 addiu $gp,$gp,%lo(_gp_disp)
12192 addu $gp,$gp,.cpload argument
aa6975fb
ILT
12193 The .cpload argument is normally $25 == $t9.
12194
12195 The -mno-shared option changes this to:
bbe506e8
TS
12196 lui $gp,%hi(__gnu_local_gp)
12197 addiu $gp,$gp,%lo(__gnu_local_gp)
aa6975fb
ILT
12198 and the argument is ignored. This saves an instruction, but the
12199 resulting code is not position independent; it uses an absolute
bbe506e8
TS
12200 address for __gnu_local_gp. Thus code assembled with -mno-shared
12201 can go into an ordinary executable, but not into a shared library. */
252b5132
RH
12202
12203static void
17a2f251 12204s_cpload (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
12205{
12206 expressionS ex;
aa6975fb
ILT
12207 int reg;
12208 int in_shared;
252b5132 12209
6478892d
TS
12210 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12211 .cpload is ignored. */
12212 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
12213 {
12214 s_ignore (0);
12215 return;
12216 }
12217
d3ecfc59 12218 /* .cpload should be in a .set noreorder section. */
252b5132
RH
12219 if (mips_opts.noreorder == 0)
12220 as_warn (_(".cpload not in noreorder section"));
12221
aa6975fb
ILT
12222 reg = tc_get_register (0);
12223
12224 /* If we need to produce a 64-bit address, we are better off using
12225 the default instruction sequence. */
aed1a261 12226 in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
aa6975fb 12227
252b5132 12228 ex.X_op = O_symbol;
bbe506e8
TS
12229 ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
12230 "__gnu_local_gp");
252b5132
RH
12231 ex.X_op_symbol = NULL;
12232 ex.X_add_number = 0;
12233
12234 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
49309057 12235 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
252b5132 12236
584892a6 12237 macro_start ();
67c0d1eb
RS
12238 macro_build_lui (&ex, mips_gp_register);
12239 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
17a2f251 12240 mips_gp_register, BFD_RELOC_LO16);
aa6975fb
ILT
12241 if (in_shared)
12242 macro_build (NULL, "addu", "d,v,t", mips_gp_register,
12243 mips_gp_register, reg);
584892a6 12244 macro_end ();
252b5132
RH
12245
12246 demand_empty_rest_of_line ();
12247}
12248
6478892d
TS
12249/* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
12250 .cpsetup $reg1, offset|$reg2, label
12251
12252 If offset is given, this results in:
12253 sd $gp, offset($sp)
956cd1d6 12254 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
12255 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
12256 daddu $gp, $gp, $reg1
6478892d
TS
12257
12258 If $reg2 is given, this results in:
12259 daddu $reg2, $gp, $0
956cd1d6 12260 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
12261 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
12262 daddu $gp, $gp, $reg1
aa6975fb
ILT
12263 $reg1 is normally $25 == $t9.
12264
12265 The -mno-shared option replaces the last three instructions with
12266 lui $gp,%hi(_gp)
12267 addiu $gp,$gp,%lo(_gp)
12268 */
12269
6478892d 12270static void
17a2f251 12271s_cpsetup (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
12272{
12273 expressionS ex_off;
12274 expressionS ex_sym;
12275 int reg1;
6478892d 12276
8586fc66 12277 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
6478892d
TS
12278 We also need NewABI support. */
12279 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12280 {
12281 s_ignore (0);
12282 return;
12283 }
12284
12285 reg1 = tc_get_register (0);
12286 SKIP_WHITESPACE ();
12287 if (*input_line_pointer != ',')
12288 {
12289 as_bad (_("missing argument separator ',' for .cpsetup"));
12290 return;
12291 }
12292 else
80245285 12293 ++input_line_pointer;
6478892d
TS
12294 SKIP_WHITESPACE ();
12295 if (*input_line_pointer == '$')
80245285
TS
12296 {
12297 mips_cpreturn_register = tc_get_register (0);
12298 mips_cpreturn_offset = -1;
12299 }
6478892d 12300 else
80245285
TS
12301 {
12302 mips_cpreturn_offset = get_absolute_expression ();
12303 mips_cpreturn_register = -1;
12304 }
6478892d
TS
12305 SKIP_WHITESPACE ();
12306 if (*input_line_pointer != ',')
12307 {
12308 as_bad (_("missing argument separator ',' for .cpsetup"));
12309 return;
12310 }
12311 else
f9419b05 12312 ++input_line_pointer;
6478892d 12313 SKIP_WHITESPACE ();
f21f8242 12314 expression (&ex_sym);
6478892d 12315
584892a6 12316 macro_start ();
6478892d
TS
12317 if (mips_cpreturn_register == -1)
12318 {
12319 ex_off.X_op = O_constant;
12320 ex_off.X_add_symbol = NULL;
12321 ex_off.X_op_symbol = NULL;
12322 ex_off.X_add_number = mips_cpreturn_offset;
12323
67c0d1eb 12324 macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
17a2f251 12325 BFD_RELOC_LO16, SP);
6478892d
TS
12326 }
12327 else
67c0d1eb 12328 macro_build (NULL, "daddu", "d,v,t", mips_cpreturn_register,
17a2f251 12329 mips_gp_register, 0);
6478892d 12330
aed1a261 12331 if (mips_in_shared || HAVE_64BIT_SYMBOLS)
aa6975fb
ILT
12332 {
12333 macro_build (&ex_sym, "lui", "t,u", mips_gp_register,
12334 -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
12335 BFD_RELOC_HI16_S);
12336
12337 macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
12338 mips_gp_register, -1, BFD_RELOC_GPREL16,
12339 BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
12340
12341 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
12342 mips_gp_register, reg1);
12343 }
12344 else
12345 {
12346 expressionS ex;
12347
12348 ex.X_op = O_symbol;
4184909a 12349 ex.X_add_symbol = symbol_find_or_make ("__gnu_local_gp");
aa6975fb
ILT
12350 ex.X_op_symbol = NULL;
12351 ex.X_add_number = 0;
6e1304d8 12352
aa6975fb
ILT
12353 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
12354 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
12355
12356 macro_build_lui (&ex, mips_gp_register);
12357 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
12358 mips_gp_register, BFD_RELOC_LO16);
12359 }
f21f8242 12360
584892a6 12361 macro_end ();
6478892d
TS
12362
12363 demand_empty_rest_of_line ();
12364}
12365
12366static void
17a2f251 12367s_cplocal (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
12368{
12369 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
12370 .cplocal is ignored. */
12371 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12372 {
12373 s_ignore (0);
12374 return;
12375 }
12376
12377 mips_gp_register = tc_get_register (0);
85b51719 12378 demand_empty_rest_of_line ();
6478892d
TS
12379}
12380
252b5132
RH
12381/* Handle the .cprestore pseudo-op. This stores $gp into a given
12382 offset from $sp. The offset is remembered, and after making a PIC
12383 call $gp is restored from that location. */
12384
12385static void
17a2f251 12386s_cprestore (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
12387{
12388 expressionS ex;
252b5132 12389
6478892d 12390 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
c9914766 12391 .cprestore is ignored. */
6478892d 12392 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
12393 {
12394 s_ignore (0);
12395 return;
12396 }
12397
12398 mips_cprestore_offset = get_absolute_expression ();
7a621144 12399 mips_cprestore_valid = 1;
252b5132
RH
12400
12401 ex.X_op = O_constant;
12402 ex.X_add_symbol = NULL;
12403 ex.X_op_symbol = NULL;
12404 ex.X_add_number = mips_cprestore_offset;
12405
584892a6 12406 macro_start ();
67c0d1eb
RS
12407 macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
12408 SP, HAVE_64BIT_ADDRESSES);
584892a6 12409 macro_end ();
252b5132
RH
12410
12411 demand_empty_rest_of_line ();
12412}
12413
6478892d 12414/* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
67c1ffbe 12415 was given in the preceding .cpsetup, it results in:
6478892d 12416 ld $gp, offset($sp)
76b3015f 12417
6478892d 12418 If a register $reg2 was given there, it results in:
609f23f4 12419 daddu $gp, $reg2, $0
6478892d
TS
12420 */
12421static void
17a2f251 12422s_cpreturn (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
12423{
12424 expressionS ex;
6478892d
TS
12425
12426 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
12427 We also need NewABI support. */
12428 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12429 {
12430 s_ignore (0);
12431 return;
12432 }
12433
584892a6 12434 macro_start ();
6478892d
TS
12435 if (mips_cpreturn_register == -1)
12436 {
12437 ex.X_op = O_constant;
12438 ex.X_add_symbol = NULL;
12439 ex.X_op_symbol = NULL;
12440 ex.X_add_number = mips_cpreturn_offset;
12441
67c0d1eb 12442 macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
6478892d
TS
12443 }
12444 else
67c0d1eb 12445 macro_build (NULL, "daddu", "d,v,t", mips_gp_register,
17a2f251 12446 mips_cpreturn_register, 0);
584892a6 12447 macro_end ();
6478892d
TS
12448
12449 demand_empty_rest_of_line ();
12450}
12451
12452/* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
12453 code. It sets the offset to use in gp_rel relocations. */
12454
12455static void
17a2f251 12456s_gpvalue (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
12457{
12458 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
12459 We also need NewABI support. */
12460 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12461 {
12462 s_ignore (0);
12463 return;
12464 }
12465
def2e0dd 12466 mips_gprel_offset = get_absolute_expression ();
6478892d
TS
12467
12468 demand_empty_rest_of_line ();
12469}
12470
252b5132
RH
12471/* Handle the .gpword pseudo-op. This is used when generating PIC
12472 code. It generates a 32 bit GP relative reloc. */
12473
12474static void
17a2f251 12475s_gpword (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
12476{
12477 symbolS *label;
12478 expressionS ex;
12479 char *p;
12480
12481 /* When not generating PIC code, this is treated as .word. */
12482 if (mips_pic != SVR4_PIC)
12483 {
12484 s_cons (2);
12485 return;
12486 }
12487
12488 label = insn_labels != NULL ? insn_labels->label : NULL;
7d10b47d 12489 mips_emit_delays ();
252b5132
RH
12490 if (auto_align)
12491 mips_align (2, 0, label);
12492 mips_clear_insn_labels ();
12493
12494 expression (&ex);
12495
12496 if (ex.X_op != O_symbol || ex.X_add_number != 0)
12497 {
12498 as_bad (_("Unsupported use of .gpword"));
12499 ignore_rest_of_line ();
12500 }
12501
12502 p = frag_more (4);
17a2f251 12503 md_number_to_chars (p, 0, 4);
b34976b6 12504 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
cdf6fd85 12505 BFD_RELOC_GPREL32);
252b5132
RH
12506
12507 demand_empty_rest_of_line ();
12508}
12509
10181a0d 12510static void
17a2f251 12511s_gpdword (int ignore ATTRIBUTE_UNUSED)
10181a0d
AO
12512{
12513 symbolS *label;
12514 expressionS ex;
12515 char *p;
12516
12517 /* When not generating PIC code, this is treated as .dword. */
12518 if (mips_pic != SVR4_PIC)
12519 {
12520 s_cons (3);
12521 return;
12522 }
12523
12524 label = insn_labels != NULL ? insn_labels->label : NULL;
7d10b47d 12525 mips_emit_delays ();
10181a0d
AO
12526 if (auto_align)
12527 mips_align (3, 0, label);
12528 mips_clear_insn_labels ();
12529
12530 expression (&ex);
12531
12532 if (ex.X_op != O_symbol || ex.X_add_number != 0)
12533 {
12534 as_bad (_("Unsupported use of .gpdword"));
12535 ignore_rest_of_line ();
12536 }
12537
12538 p = frag_more (8);
17a2f251 12539 md_number_to_chars (p, 0, 8);
a105a300 12540 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
6e1304d8 12541 BFD_RELOC_GPREL32)->fx_tcbit = 1;
10181a0d
AO
12542
12543 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
6e1304d8
RS
12544 fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
12545 FALSE, BFD_RELOC_64)->fx_tcbit = 1;
10181a0d
AO
12546
12547 demand_empty_rest_of_line ();
12548}
12549
252b5132
RH
12550/* Handle the .cpadd pseudo-op. This is used when dealing with switch
12551 tables in SVR4 PIC code. */
12552
12553static void
17a2f251 12554s_cpadd (int ignore ATTRIBUTE_UNUSED)
252b5132 12555{
252b5132
RH
12556 int reg;
12557
10181a0d
AO
12558 /* This is ignored when not generating SVR4 PIC code. */
12559 if (mips_pic != SVR4_PIC)
252b5132
RH
12560 {
12561 s_ignore (0);
12562 return;
12563 }
12564
12565 /* Add $gp to the register named as an argument. */
584892a6 12566 macro_start ();
252b5132 12567 reg = tc_get_register (0);
67c0d1eb 12568 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
584892a6 12569 macro_end ();
252b5132 12570
bdaaa2e1 12571 demand_empty_rest_of_line ();
252b5132
RH
12572}
12573
12574/* Handle the .insn pseudo-op. This marks instruction labels in
12575 mips16 mode. This permits the linker to handle them specially,
12576 such as generating jalx instructions when needed. We also make
12577 them odd for the duration of the assembly, in order to generate the
12578 right sort of code. We will make them even in the adjust_symtab
12579 routine, while leaving them marked. This is convenient for the
12580 debugger and the disassembler. The linker knows to make them odd
12581 again. */
12582
12583static void
17a2f251 12584s_insn (int ignore ATTRIBUTE_UNUSED)
252b5132 12585{
f9419b05 12586 mips16_mark_labels ();
252b5132
RH
12587
12588 demand_empty_rest_of_line ();
12589}
12590
12591/* Handle a .stabn directive. We need these in order to mark a label
12592 as being a mips16 text label correctly. Sometimes the compiler
12593 will emit a label, followed by a .stabn, and then switch sections.
12594 If the label and .stabn are in mips16 mode, then the label is
12595 really a mips16 text label. */
12596
12597static void
17a2f251 12598s_mips_stab (int type)
252b5132 12599{
f9419b05 12600 if (type == 'n')
252b5132
RH
12601 mips16_mark_labels ();
12602
12603 s_stab (type);
12604}
12605
12606/* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
12607 */
12608
12609static void
17a2f251 12610s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
12611{
12612 char *name;
12613 int c;
12614 symbolS *symbolP;
12615 expressionS exp;
12616
12617 name = input_line_pointer;
12618 c = get_symbol_end ();
12619 symbolP = symbol_find_or_make (name);
12620 S_SET_WEAK (symbolP);
12621 *input_line_pointer = c;
12622
12623 SKIP_WHITESPACE ();
12624
12625 if (! is_end_of_line[(unsigned char) *input_line_pointer])
12626 {
12627 if (S_IS_DEFINED (symbolP))
12628 {
956cd1d6 12629 as_bad ("ignoring attempt to redefine symbol %s",
252b5132
RH
12630 S_GET_NAME (symbolP));
12631 ignore_rest_of_line ();
12632 return;
12633 }
bdaaa2e1 12634
252b5132
RH
12635 if (*input_line_pointer == ',')
12636 {
12637 ++input_line_pointer;
12638 SKIP_WHITESPACE ();
12639 }
bdaaa2e1 12640
252b5132
RH
12641 expression (&exp);
12642 if (exp.X_op != O_symbol)
12643 {
12644 as_bad ("bad .weakext directive");
98d3f06f 12645 ignore_rest_of_line ();
252b5132
RH
12646 return;
12647 }
49309057 12648 symbol_set_value_expression (symbolP, &exp);
252b5132
RH
12649 }
12650
12651 demand_empty_rest_of_line ();
12652}
12653
12654/* Parse a register string into a number. Called from the ECOFF code
12655 to parse .frame. The argument is non-zero if this is the frame
12656 register, so that we can record it in mips_frame_reg. */
12657
12658int
17a2f251 12659tc_get_register (int frame)
252b5132
RH
12660{
12661 int reg;
12662
12663 SKIP_WHITESPACE ();
12664 if (*input_line_pointer++ != '$')
12665 {
12666 as_warn (_("expected `$'"));
85b51719 12667 reg = ZERO;
252b5132 12668 }
3882b010 12669 else if (ISDIGIT (*input_line_pointer))
252b5132
RH
12670 {
12671 reg = get_absolute_expression ();
12672 if (reg < 0 || reg >= 32)
12673 {
12674 as_warn (_("Bad register number"));
85b51719 12675 reg = ZERO;
252b5132
RH
12676 }
12677 }
12678 else
12679 {
76db943d 12680 if (strncmp (input_line_pointer, "ra", 2) == 0)
85b51719
TS
12681 {
12682 reg = RA;
12683 input_line_pointer += 2;
12684 }
76db943d 12685 else if (strncmp (input_line_pointer, "fp", 2) == 0)
85b51719
TS
12686 {
12687 reg = FP;
12688 input_line_pointer += 2;
12689 }
252b5132 12690 else if (strncmp (input_line_pointer, "sp", 2) == 0)
85b51719
TS
12691 {
12692 reg = SP;
12693 input_line_pointer += 2;
12694 }
252b5132 12695 else if (strncmp (input_line_pointer, "gp", 2) == 0)
85b51719
TS
12696 {
12697 reg = GP;
12698 input_line_pointer += 2;
12699 }
252b5132 12700 else if (strncmp (input_line_pointer, "at", 2) == 0)
85b51719
TS
12701 {
12702 reg = AT;
12703 input_line_pointer += 2;
12704 }
12705 else if (strncmp (input_line_pointer, "kt0", 3) == 0)
12706 {
12707 reg = KT0;
12708 input_line_pointer += 3;
12709 }
12710 else if (strncmp (input_line_pointer, "kt1", 3) == 0)
12711 {
12712 reg = KT1;
12713 input_line_pointer += 3;
12714 }
12715 else if (strncmp (input_line_pointer, "zero", 4) == 0)
12716 {
12717 reg = ZERO;
12718 input_line_pointer += 4;
12719 }
252b5132
RH
12720 else
12721 {
12722 as_warn (_("Unrecognized register name"));
85b51719
TS
12723 reg = ZERO;
12724 while (ISALNUM(*input_line_pointer))
12725 input_line_pointer++;
252b5132 12726 }
252b5132
RH
12727 }
12728 if (frame)
7a621144
DJ
12729 {
12730 mips_frame_reg = reg != 0 ? reg : SP;
12731 mips_frame_reg_valid = 1;
12732 mips_cprestore_valid = 0;
12733 }
252b5132
RH
12734 return reg;
12735}
12736
12737valueT
17a2f251 12738md_section_align (asection *seg, valueT addr)
252b5132
RH
12739{
12740 int align = bfd_get_section_alignment (stdoutput, seg);
12741
12742#ifdef OBJ_ELF
12743 /* We don't need to align ELF sections to the full alignment.
12744 However, Irix 5 may prefer that we align them at least to a 16
12745 byte boundary. We don't bother to align the sections if we are
12746 targeted for an embedded system. */
12747 if (strcmp (TARGET_OS, "elf") == 0)
12748 return addr;
12749 if (align > 4)
12750 align = 4;
12751#endif
12752
12753 return ((addr + (1 << align) - 1) & (-1 << align));
12754}
12755
12756/* Utility routine, called from above as well. If called while the
12757 input file is still being read, it's only an approximation. (For
12758 example, a symbol may later become defined which appeared to be
12759 undefined earlier.) */
12760
12761static int
17a2f251 12762nopic_need_relax (symbolS *sym, int before_relaxing)
252b5132
RH
12763{
12764 if (sym == 0)
12765 return 0;
12766
4d0d148d 12767 if (g_switch_value > 0)
252b5132
RH
12768 {
12769 const char *symname;
12770 int change;
12771
c9914766 12772 /* Find out whether this symbol can be referenced off the $gp
252b5132
RH
12773 register. It can be if it is smaller than the -G size or if
12774 it is in the .sdata or .sbss section. Certain symbols can
c9914766 12775 not be referenced off the $gp, although it appears as though
252b5132
RH
12776 they can. */
12777 symname = S_GET_NAME (sym);
12778 if (symname != (const char *) NULL
12779 && (strcmp (symname, "eprol") == 0
12780 || strcmp (symname, "etext") == 0
12781 || strcmp (symname, "_gp") == 0
12782 || strcmp (symname, "edata") == 0
12783 || strcmp (symname, "_fbss") == 0
12784 || strcmp (symname, "_fdata") == 0
12785 || strcmp (symname, "_ftext") == 0
12786 || strcmp (symname, "end") == 0
12787 || strcmp (symname, "_gp_disp") == 0))
12788 change = 1;
12789 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
12790 && (0
12791#ifndef NO_ECOFF_DEBUGGING
49309057
ILT
12792 || (symbol_get_obj (sym)->ecoff_extern_size != 0
12793 && (symbol_get_obj (sym)->ecoff_extern_size
12794 <= g_switch_value))
252b5132
RH
12795#endif
12796 /* We must defer this decision until after the whole
12797 file has been read, since there might be a .extern
12798 after the first use of this symbol. */
12799 || (before_relaxing
12800#ifndef NO_ECOFF_DEBUGGING
49309057 12801 && symbol_get_obj (sym)->ecoff_extern_size == 0
252b5132
RH
12802#endif
12803 && S_GET_VALUE (sym) == 0)
12804 || (S_GET_VALUE (sym) != 0
12805 && S_GET_VALUE (sym) <= g_switch_value)))
12806 change = 0;
12807 else
12808 {
12809 const char *segname;
12810
12811 segname = segment_name (S_GET_SEGMENT (sym));
12812 assert (strcmp (segname, ".lit8") != 0
12813 && strcmp (segname, ".lit4") != 0);
12814 change = (strcmp (segname, ".sdata") != 0
fba2b7f9
GK
12815 && strcmp (segname, ".sbss") != 0
12816 && strncmp (segname, ".sdata.", 7) != 0
12817 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
252b5132
RH
12818 }
12819 return change;
12820 }
12821 else
c9914766 12822 /* We are not optimizing for the $gp register. */
252b5132
RH
12823 return 1;
12824}
12825
5919d012
RS
12826
12827/* Return true if the given symbol should be considered local for SVR4 PIC. */
12828
12829static bfd_boolean
17a2f251 12830pic_need_relax (symbolS *sym, asection *segtype)
5919d012
RS
12831{
12832 asection *symsec;
12833 bfd_boolean linkonce;
12834
12835 /* Handle the case of a symbol equated to another symbol. */
12836 while (symbol_equated_reloc_p (sym))
12837 {
12838 symbolS *n;
12839
12840 /* It's possible to get a loop here in a badly written
12841 program. */
12842 n = symbol_get_value_expression (sym)->X_add_symbol;
12843 if (n == sym)
12844 break;
12845 sym = n;
12846 }
12847
12848 symsec = S_GET_SEGMENT (sym);
12849
12850 /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
12851 linkonce = FALSE;
12852 if (symsec != segtype && ! S_IS_LOCAL (sym))
12853 {
12854 if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
12855 != 0)
12856 linkonce = TRUE;
12857
12858 /* The GNU toolchain uses an extension for ELF: a section
12859 beginning with the magic string .gnu.linkonce is a linkonce
12860 section. */
12861 if (strncmp (segment_name (symsec), ".gnu.linkonce",
12862 sizeof ".gnu.linkonce" - 1) == 0)
12863 linkonce = TRUE;
12864 }
12865
12866 /* This must duplicate the test in adjust_reloc_syms. */
12867 return (symsec != &bfd_und_section
12868 && symsec != &bfd_abs_section
12869 && ! bfd_is_com_section (symsec)
12870 && !linkonce
12871#ifdef OBJ_ELF
12872 /* A global or weak symbol is treated as external. */
12873 && (OUTPUT_FLAVOR != bfd_target_elf_flavour
3e722fb5 12874 || (! S_IS_WEAK (sym) && ! S_IS_EXTERNAL (sym)))
5919d012
RS
12875#endif
12876 );
12877}
12878
12879
252b5132
RH
12880/* Given a mips16 variant frag FRAGP, return non-zero if it needs an
12881 extended opcode. SEC is the section the frag is in. */
12882
12883static int
17a2f251 12884mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
252b5132
RH
12885{
12886 int type;
12887 register const struct mips16_immed_operand *op;
12888 offsetT val;
12889 int mintiny, maxtiny;
12890 segT symsec;
98aa84af 12891 fragS *sym_frag;
252b5132
RH
12892
12893 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
12894 return 0;
12895 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
12896 return 1;
12897
12898 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12899 op = mips16_immed_operands;
12900 while (op->type != type)
12901 {
12902 ++op;
12903 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
12904 }
12905
12906 if (op->unsp)
12907 {
12908 if (type == '<' || type == '>' || type == '[' || type == ']')
12909 {
12910 mintiny = 1;
12911 maxtiny = 1 << op->nbits;
12912 }
12913 else
12914 {
12915 mintiny = 0;
12916 maxtiny = (1 << op->nbits) - 1;
12917 }
12918 }
12919 else
12920 {
12921 mintiny = - (1 << (op->nbits - 1));
12922 maxtiny = (1 << (op->nbits - 1)) - 1;
12923 }
12924
98aa84af 12925 sym_frag = symbol_get_frag (fragp->fr_symbol);
ac62c346 12926 val = S_GET_VALUE (fragp->fr_symbol);
98aa84af 12927 symsec = S_GET_SEGMENT (fragp->fr_symbol);
252b5132
RH
12928
12929 if (op->pcrel)
12930 {
12931 addressT addr;
12932
12933 /* We won't have the section when we are called from
12934 mips_relax_frag. However, we will always have been called
12935 from md_estimate_size_before_relax first. If this is a
12936 branch to a different section, we mark it as such. If SEC is
12937 NULL, and the frag is not marked, then it must be a branch to
12938 the same section. */
12939 if (sec == NULL)
12940 {
12941 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
12942 return 1;
12943 }
12944 else
12945 {
98aa84af 12946 /* Must have been called from md_estimate_size_before_relax. */
252b5132
RH
12947 if (symsec != sec)
12948 {
12949 fragp->fr_subtype =
12950 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12951
12952 /* FIXME: We should support this, and let the linker
12953 catch branches and loads that are out of range. */
12954 as_bad_where (fragp->fr_file, fragp->fr_line,
12955 _("unsupported PC relative reference to different section"));
12956
12957 return 1;
12958 }
98aa84af
AM
12959 if (fragp != sym_frag && sym_frag->fr_address == 0)
12960 /* Assume non-extended on the first relaxation pass.
12961 The address we have calculated will be bogus if this is
12962 a forward branch to another frag, as the forward frag
12963 will have fr_address == 0. */
12964 return 0;
252b5132
RH
12965 }
12966
12967 /* In this case, we know for sure that the symbol fragment is in
98aa84af
AM
12968 the same section. If the relax_marker of the symbol fragment
12969 differs from the relax_marker of this fragment, we have not
12970 yet adjusted the symbol fragment fr_address. We want to add
252b5132
RH
12971 in STRETCH in order to get a better estimate of the address.
12972 This particularly matters because of the shift bits. */
12973 if (stretch != 0
98aa84af 12974 && sym_frag->relax_marker != fragp->relax_marker)
252b5132
RH
12975 {
12976 fragS *f;
12977
12978 /* Adjust stretch for any alignment frag. Note that if have
12979 been expanding the earlier code, the symbol may be
12980 defined in what appears to be an earlier frag. FIXME:
12981 This doesn't handle the fr_subtype field, which specifies
12982 a maximum number of bytes to skip when doing an
12983 alignment. */
98aa84af 12984 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
252b5132
RH
12985 {
12986 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
12987 {
12988 if (stretch < 0)
12989 stretch = - ((- stretch)
12990 & ~ ((1 << (int) f->fr_offset) - 1));
12991 else
12992 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
12993 if (stretch == 0)
12994 break;
12995 }
12996 }
12997 if (f != NULL)
12998 val += stretch;
12999 }
13000
13001 addr = fragp->fr_address + fragp->fr_fix;
13002
13003 /* The base address rules are complicated. The base address of
13004 a branch is the following instruction. The base address of a
13005 PC relative load or add is the instruction itself, but if it
13006 is in a delay slot (in which case it can not be extended) use
13007 the address of the instruction whose delay slot it is in. */
13008 if (type == 'p' || type == 'q')
13009 {
13010 addr += 2;
13011
13012 /* If we are currently assuming that this frag should be
13013 extended, then, the current address is two bytes
bdaaa2e1 13014 higher. */
252b5132
RH
13015 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13016 addr += 2;
13017
13018 /* Ignore the low bit in the target, since it will be set
13019 for a text label. */
13020 if ((val & 1) != 0)
13021 --val;
13022 }
13023 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13024 addr -= 4;
13025 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13026 addr -= 2;
13027
13028 val -= addr & ~ ((1 << op->shift) - 1);
13029
13030 /* Branch offsets have an implicit 0 in the lowest bit. */
13031 if (type == 'p' || type == 'q')
13032 val /= 2;
13033
13034 /* If any of the shifted bits are set, we must use an extended
13035 opcode. If the address depends on the size of this
13036 instruction, this can lead to a loop, so we arrange to always
13037 use an extended opcode. We only check this when we are in
13038 the main relaxation loop, when SEC is NULL. */
13039 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
13040 {
13041 fragp->fr_subtype =
13042 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13043 return 1;
13044 }
13045
13046 /* If we are about to mark a frag as extended because the value
13047 is precisely maxtiny + 1, then there is a chance of an
13048 infinite loop as in the following code:
13049 la $4,foo
13050 .skip 1020
13051 .align 2
13052 foo:
13053 In this case when the la is extended, foo is 0x3fc bytes
13054 away, so the la can be shrunk, but then foo is 0x400 away, so
13055 the la must be extended. To avoid this loop, we mark the
13056 frag as extended if it was small, and is about to become
13057 extended with a value of maxtiny + 1. */
13058 if (val == ((maxtiny + 1) << op->shift)
13059 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
13060 && sec == NULL)
13061 {
13062 fragp->fr_subtype =
13063 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13064 return 1;
13065 }
13066 }
13067 else if (symsec != absolute_section && sec != NULL)
13068 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
13069
13070 if ((val & ((1 << op->shift) - 1)) != 0
13071 || val < (mintiny << op->shift)
13072 || val > (maxtiny << op->shift))
13073 return 1;
13074 else
13075 return 0;
13076}
13077
4a6a3df4
AO
13078/* Compute the length of a branch sequence, and adjust the
13079 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
13080 worst-case length is computed, with UPDATE being used to indicate
13081 whether an unconditional (-1), branch-likely (+1) or regular (0)
13082 branch is to be computed. */
13083static int
17a2f251 13084relaxed_branch_length (fragS *fragp, asection *sec, int update)
4a6a3df4 13085{
b34976b6 13086 bfd_boolean toofar;
4a6a3df4
AO
13087 int length;
13088
13089 if (fragp
13090 && S_IS_DEFINED (fragp->fr_symbol)
13091 && sec == S_GET_SEGMENT (fragp->fr_symbol))
13092 {
13093 addressT addr;
13094 offsetT val;
13095
13096 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
13097
13098 addr = fragp->fr_address + fragp->fr_fix + 4;
13099
13100 val -= addr;
13101
13102 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
13103 }
13104 else if (fragp)
13105 /* If the symbol is not defined or it's in a different segment,
13106 assume the user knows what's going on and emit a short
13107 branch. */
b34976b6 13108 toofar = FALSE;
4a6a3df4 13109 else
b34976b6 13110 toofar = TRUE;
4a6a3df4
AO
13111
13112 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13113 fragp->fr_subtype
af6ae2ad 13114 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_UNCOND (fragp->fr_subtype),
4a6a3df4
AO
13115 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
13116 RELAX_BRANCH_LINK (fragp->fr_subtype),
13117 toofar);
13118
13119 length = 4;
13120 if (toofar)
13121 {
13122 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
13123 length += 8;
13124
13125 if (mips_pic != NO_PIC)
13126 {
13127 /* Additional space for PIC loading of target address. */
13128 length += 8;
13129 if (mips_opts.isa == ISA_MIPS1)
13130 /* Additional space for $at-stabilizing nop. */
13131 length += 4;
13132 }
13133
13134 /* If branch is conditional. */
13135 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
13136 length += 8;
13137 }
b34976b6 13138
4a6a3df4
AO
13139 return length;
13140}
13141
252b5132
RH
13142/* Estimate the size of a frag before relaxing. Unless this is the
13143 mips16, we are not really relaxing here, and the final size is
13144 encoded in the subtype information. For the mips16, we have to
13145 decide whether we are using an extended opcode or not. */
13146
252b5132 13147int
17a2f251 13148md_estimate_size_before_relax (fragS *fragp, asection *segtype)
252b5132 13149{
5919d012 13150 int change;
252b5132 13151
4a6a3df4
AO
13152 if (RELAX_BRANCH_P (fragp->fr_subtype))
13153 {
13154
b34976b6
AM
13155 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
13156
4a6a3df4
AO
13157 return fragp->fr_var;
13158 }
13159
252b5132 13160 if (RELAX_MIPS16_P (fragp->fr_subtype))
177b4a6a
AO
13161 /* We don't want to modify the EXTENDED bit here; it might get us
13162 into infinite loops. We change it only in mips_relax_frag(). */
13163 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
252b5132
RH
13164
13165 if (mips_pic == NO_PIC)
5919d012 13166 change = nopic_need_relax (fragp->fr_symbol, 0);
252b5132 13167 else if (mips_pic == SVR4_PIC)
5919d012 13168 change = pic_need_relax (fragp->fr_symbol, segtype);
252b5132
RH
13169 else
13170 abort ();
13171
13172 if (change)
13173 {
4d7206a2 13174 fragp->fr_subtype |= RELAX_USE_SECOND;
4d7206a2 13175 return -RELAX_FIRST (fragp->fr_subtype);
252b5132 13176 }
4d7206a2
RS
13177 else
13178 return -RELAX_SECOND (fragp->fr_subtype);
252b5132
RH
13179}
13180
13181/* This is called to see whether a reloc against a defined symbol
de7e6852 13182 should be converted into a reloc against a section. */
252b5132
RH
13183
13184int
17a2f251 13185mips_fix_adjustable (fixS *fixp)
252b5132 13186{
de7e6852
RS
13187 /* Don't adjust MIPS16 jump relocations, so we don't have to worry
13188 about the format of the offset in the .o file. */
252b5132
RH
13189 if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
13190 return 0;
a161fe53 13191
252b5132
RH
13192 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
13193 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13194 return 0;
a161fe53 13195
252b5132
RH
13196 if (fixp->fx_addsy == NULL)
13197 return 1;
a161fe53 13198
de7e6852
RS
13199 /* If symbol SYM is in a mergeable section, relocations of the form
13200 SYM + 0 can usually be made section-relative. The mergeable data
13201 is then identified by the section offset rather than by the symbol.
13202
13203 However, if we're generating REL LO16 relocations, the offset is split
13204 between the LO16 and parterning high part relocation. The linker will
13205 need to recalculate the complete offset in order to correctly identify
13206 the merge data.
13207
13208 The linker has traditionally not looked for the parterning high part
13209 relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
13210 placed anywhere. Rather than break backwards compatibility by changing
13211 this, it seems better not to force the issue, and instead keep the
13212 original symbol. This will work with either linker behavior. */
704803a9
MR
13213 if ((fixp->fx_r_type == BFD_RELOC_LO16
13214 || fixp->fx_r_type == BFD_RELOC_MIPS16_LO16
13215 || reloc_needs_lo_p (fixp->fx_r_type))
de7e6852
RS
13216 && HAVE_IN_PLACE_ADDENDS
13217 && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
13218 return 0;
13219
252b5132 13220#ifdef OBJ_ELF
de7e6852
RS
13221 /* Don't adjust relocations against mips16 symbols, so that the linker
13222 can find them if it needs to set up a stub. */
252b5132
RH
13223 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
13224 && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
13225 && fixp->fx_subsy == NULL)
13226 return 0;
13227#endif
a161fe53 13228
252b5132
RH
13229 return 1;
13230}
13231
13232/* Translate internal representation of relocation info to BFD target
13233 format. */
13234
13235arelent **
17a2f251 13236tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
13237{
13238 static arelent *retval[4];
13239 arelent *reloc;
13240 bfd_reloc_code_real_type code;
13241
4b0cff4e
TS
13242 memset (retval, 0, sizeof(retval));
13243 reloc = retval[0] = (arelent *) xcalloc (1, sizeof (arelent));
49309057
ILT
13244 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13245 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
13246 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
13247
bad36eac
DJ
13248 if (fixp->fx_pcrel)
13249 {
13250 assert (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2);
13251
13252 /* At this point, fx_addnumber is "symbol offset - pcrel address".
13253 Relocations want only the symbol offset. */
13254 reloc->addend = fixp->fx_addnumber + reloc->address;
13255 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
13256 {
13257 /* A gruesome hack which is a result of the gruesome gas
13258 reloc handling. What's worse, for COFF (as opposed to
13259 ECOFF), we might need yet another copy of reloc->address.
13260 See bfd_install_relocation. */
13261 reloc->addend += reloc->address;
13262 }
13263 }
13264 else
13265 reloc->addend = fixp->fx_addnumber;
252b5132 13266
438c16b8
TS
13267 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
13268 entry to be used in the relocation's section offset. */
13269 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
252b5132
RH
13270 {
13271 reloc->address = reloc->addend;
13272 reloc->addend = 0;
13273 }
13274
252b5132 13275 code = fixp->fx_r_type;
252b5132 13276
bad36eac 13277 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
252b5132
RH
13278 if (reloc->howto == NULL)
13279 {
13280 as_bad_where (fixp->fx_file, fixp->fx_line,
13281 _("Can not represent %s relocation in this object file format"),
13282 bfd_get_reloc_code_name (code));
13283 retval[0] = NULL;
13284 }
13285
13286 return retval;
13287}
13288
13289/* Relax a machine dependent frag. This returns the amount by which
13290 the current size of the frag should change. */
13291
13292int
17a2f251 13293mips_relax_frag (asection *sec, fragS *fragp, long stretch)
252b5132 13294{
4a6a3df4
AO
13295 if (RELAX_BRANCH_P (fragp->fr_subtype))
13296 {
13297 offsetT old_var = fragp->fr_var;
b34976b6
AM
13298
13299 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
4a6a3df4
AO
13300
13301 return fragp->fr_var - old_var;
13302 }
13303
252b5132
RH
13304 if (! RELAX_MIPS16_P (fragp->fr_subtype))
13305 return 0;
13306
c4e7957c 13307 if (mips16_extended_frag (fragp, NULL, stretch))
252b5132
RH
13308 {
13309 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13310 return 0;
13311 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
13312 return 2;
13313 }
13314 else
13315 {
13316 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13317 return 0;
13318 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
13319 return -2;
13320 }
13321
13322 return 0;
13323}
13324
13325/* Convert a machine dependent frag. */
13326
13327void
17a2f251 13328md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
252b5132 13329{
4a6a3df4
AO
13330 if (RELAX_BRANCH_P (fragp->fr_subtype))
13331 {
13332 bfd_byte *buf;
13333 unsigned long insn;
13334 expressionS exp;
13335 fixS *fixp;
b34976b6 13336
4a6a3df4
AO
13337 buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
13338
13339 if (target_big_endian)
13340 insn = bfd_getb32 (buf);
13341 else
13342 insn = bfd_getl32 (buf);
b34976b6 13343
4a6a3df4
AO
13344 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13345 {
13346 /* We generate a fixup instead of applying it right now
13347 because, if there are linker relaxations, we're going to
13348 need the relocations. */
13349 exp.X_op = O_symbol;
13350 exp.X_add_symbol = fragp->fr_symbol;
13351 exp.X_add_number = fragp->fr_offset;
13352
13353 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
bad36eac 13354 4, &exp, 1, BFD_RELOC_16_PCREL_S2);
4a6a3df4
AO
13355 fixp->fx_file = fragp->fr_file;
13356 fixp->fx_line = fragp->fr_line;
b34976b6 13357
2132e3a3 13358 md_number_to_chars ((char *) buf, insn, 4);
4a6a3df4
AO
13359 buf += 4;
13360 }
13361 else
13362 {
13363 int i;
13364
13365 as_warn_where (fragp->fr_file, fragp->fr_line,
13366 _("relaxed out-of-range branch into a jump"));
13367
13368 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
13369 goto uncond;
13370
13371 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13372 {
13373 /* Reverse the branch. */
13374 switch ((insn >> 28) & 0xf)
13375 {
13376 case 4:
13377 /* bc[0-3][tf]l? and bc1any[24][ft] instructions can
13378 have the condition reversed by tweaking a single
13379 bit, and their opcodes all have 0x4???????. */
13380 assert ((insn & 0xf1000000) == 0x41000000);
13381 insn ^= 0x00010000;
13382 break;
13383
13384 case 0:
13385 /* bltz 0x04000000 bgez 0x04010000
13386 bltzal 0x04100000 bgezal 0x04110000 */
13387 assert ((insn & 0xfc0e0000) == 0x04000000);
13388 insn ^= 0x00010000;
13389 break;
b34976b6 13390
4a6a3df4
AO
13391 case 1:
13392 /* beq 0x10000000 bne 0x14000000
13393 blez 0x18000000 bgtz 0x1c000000 */
13394 insn ^= 0x04000000;
13395 break;
13396
13397 default:
13398 abort ();
13399 }
13400 }
13401
13402 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13403 {
13404 /* Clear the and-link bit. */
13405 assert ((insn & 0xfc1c0000) == 0x04100000);
13406
13407 /* bltzal 0x04100000 bgezal 0x04110000
13408 bltzall 0x04120000 bgezall 0x04130000 */
13409 insn &= ~0x00100000;
13410 }
13411
13412 /* Branch over the branch (if the branch was likely) or the
13413 full jump (not likely case). Compute the offset from the
13414 current instruction to branch to. */
13415 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13416 i = 16;
13417 else
13418 {
13419 /* How many bytes in instructions we've already emitted? */
13420 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13421 /* How many bytes in instructions from here to the end? */
13422 i = fragp->fr_var - i;
13423 }
13424 /* Convert to instruction count. */
13425 i >>= 2;
13426 /* Branch counts from the next instruction. */
b34976b6 13427 i--;
4a6a3df4
AO
13428 insn |= i;
13429 /* Branch over the jump. */
2132e3a3 13430 md_number_to_chars ((char *) buf, insn, 4);
4a6a3df4
AO
13431 buf += 4;
13432
13433 /* Nop */
2132e3a3 13434 md_number_to_chars ((char *) buf, 0, 4);
4a6a3df4
AO
13435 buf += 4;
13436
13437 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13438 {
13439 /* beql $0, $0, 2f */
13440 insn = 0x50000000;
13441 /* Compute the PC offset from the current instruction to
13442 the end of the variable frag. */
13443 /* How many bytes in instructions we've already emitted? */
13444 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13445 /* How many bytes in instructions from here to the end? */
13446 i = fragp->fr_var - i;
13447 /* Convert to instruction count. */
13448 i >>= 2;
13449 /* Don't decrement i, because we want to branch over the
13450 delay slot. */
13451
13452 insn |= i;
2132e3a3 13453 md_number_to_chars ((char *) buf, insn, 4);
4a6a3df4
AO
13454 buf += 4;
13455
2132e3a3 13456 md_number_to_chars ((char *) buf, 0, 4);
4a6a3df4
AO
13457 buf += 4;
13458 }
13459
13460 uncond:
13461 if (mips_pic == NO_PIC)
13462 {
13463 /* j or jal. */
13464 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
13465 ? 0x0c000000 : 0x08000000);
13466 exp.X_op = O_symbol;
13467 exp.X_add_symbol = fragp->fr_symbol;
13468 exp.X_add_number = fragp->fr_offset;
13469
13470 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13471 4, &exp, 0, BFD_RELOC_MIPS_JMP);
13472 fixp->fx_file = fragp->fr_file;
13473 fixp->fx_line = fragp->fr_line;
13474
2132e3a3 13475 md_number_to_chars ((char *) buf, insn, 4);
4a6a3df4
AO
13476 buf += 4;
13477 }
13478 else
13479 {
13480 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
13481 insn = HAVE_64BIT_ADDRESSES ? 0xdf810000 : 0x8f810000;
13482 exp.X_op = O_symbol;
13483 exp.X_add_symbol = fragp->fr_symbol;
13484 exp.X_add_number = fragp->fr_offset;
13485
13486 if (fragp->fr_offset)
13487 {
13488 exp.X_add_symbol = make_expr_symbol (&exp);
13489 exp.X_add_number = 0;
13490 }
13491
13492 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13493 4, &exp, 0, BFD_RELOC_MIPS_GOT16);
13494 fixp->fx_file = fragp->fr_file;
13495 fixp->fx_line = fragp->fr_line;
13496
2132e3a3 13497 md_number_to_chars ((char *) buf, insn, 4);
4a6a3df4 13498 buf += 4;
b34976b6 13499
4a6a3df4
AO
13500 if (mips_opts.isa == ISA_MIPS1)
13501 {
13502 /* nop */
2132e3a3 13503 md_number_to_chars ((char *) buf, 0, 4);
4a6a3df4
AO
13504 buf += 4;
13505 }
13506
13507 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
13508 insn = HAVE_64BIT_ADDRESSES ? 0x64210000 : 0x24210000;
13509
13510 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13511 4, &exp, 0, BFD_RELOC_LO16);
13512 fixp->fx_file = fragp->fr_file;
13513 fixp->fx_line = fragp->fr_line;
b34976b6 13514
2132e3a3 13515 md_number_to_chars ((char *) buf, insn, 4);
4a6a3df4
AO
13516 buf += 4;
13517
13518 /* j(al)r $at. */
13519 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13520 insn = 0x0020f809;
13521 else
13522 insn = 0x00200008;
13523
2132e3a3 13524 md_number_to_chars ((char *) buf, insn, 4);
4a6a3df4
AO
13525 buf += 4;
13526 }
13527 }
13528
13529 assert (buf == (bfd_byte *)fragp->fr_literal
13530 + fragp->fr_fix + fragp->fr_var);
13531
13532 fragp->fr_fix += fragp->fr_var;
13533
13534 return;
13535 }
13536
252b5132
RH
13537 if (RELAX_MIPS16_P (fragp->fr_subtype))
13538 {
13539 int type;
13540 register const struct mips16_immed_operand *op;
b34976b6 13541 bfd_boolean small, ext;
252b5132
RH
13542 offsetT val;
13543 bfd_byte *buf;
13544 unsigned long insn;
b34976b6 13545 bfd_boolean use_extend;
252b5132
RH
13546 unsigned short extend;
13547
13548 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
13549 op = mips16_immed_operands;
13550 while (op->type != type)
13551 ++op;
13552
13553 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13554 {
b34976b6
AM
13555 small = FALSE;
13556 ext = TRUE;
252b5132
RH
13557 }
13558 else
13559 {
b34976b6
AM
13560 small = TRUE;
13561 ext = FALSE;
252b5132
RH
13562 }
13563
6386f3a7 13564 resolve_symbol_value (fragp->fr_symbol);
252b5132
RH
13565 val = S_GET_VALUE (fragp->fr_symbol);
13566 if (op->pcrel)
13567 {
13568 addressT addr;
13569
13570 addr = fragp->fr_address + fragp->fr_fix;
13571
13572 /* The rules for the base address of a PC relative reloc are
13573 complicated; see mips16_extended_frag. */
13574 if (type == 'p' || type == 'q')
13575 {
13576 addr += 2;
13577 if (ext)
13578 addr += 2;
13579 /* Ignore the low bit in the target, since it will be
13580 set for a text label. */
13581 if ((val & 1) != 0)
13582 --val;
13583 }
13584 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13585 addr -= 4;
13586 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13587 addr -= 2;
13588
13589 addr &= ~ (addressT) ((1 << op->shift) - 1);
13590 val -= addr;
13591
13592 /* Make sure the section winds up with the alignment we have
13593 assumed. */
13594 if (op->shift > 0)
13595 record_alignment (asec, op->shift);
13596 }
13597
13598 if (ext
13599 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
13600 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
13601 as_warn_where (fragp->fr_file, fragp->fr_line,
13602 _("extended instruction in delay slot"));
13603
13604 buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
13605
13606 if (target_big_endian)
13607 insn = bfd_getb16 (buf);
13608 else
13609 insn = bfd_getl16 (buf);
13610
13611 mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
13612 RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
13613 small, ext, &insn, &use_extend, &extend);
13614
13615 if (use_extend)
13616 {
2132e3a3 13617 md_number_to_chars ((char *) buf, 0xf000 | extend, 2);
252b5132
RH
13618 fragp->fr_fix += 2;
13619 buf += 2;
13620 }
13621
2132e3a3 13622 md_number_to_chars ((char *) buf, insn, 2);
252b5132
RH
13623 fragp->fr_fix += 2;
13624 buf += 2;
13625 }
13626 else
13627 {
4d7206a2
RS
13628 int first, second;
13629 fixS *fixp;
252b5132 13630
4d7206a2
RS
13631 first = RELAX_FIRST (fragp->fr_subtype);
13632 second = RELAX_SECOND (fragp->fr_subtype);
13633 fixp = (fixS *) fragp->fr_opcode;
252b5132 13634
584892a6
RS
13635 /* Possibly emit a warning if we've chosen the longer option. */
13636 if (((fragp->fr_subtype & RELAX_USE_SECOND) != 0)
13637 == ((fragp->fr_subtype & RELAX_SECOND_LONGER) != 0))
13638 {
13639 const char *msg = macro_warning (fragp->fr_subtype);
13640 if (msg != 0)
13641 as_warn_where (fragp->fr_file, fragp->fr_line, msg);
13642 }
13643
4d7206a2
RS
13644 /* Go through all the fixups for the first sequence. Disable them
13645 (by marking them as done) if we're going to use the second
13646 sequence instead. */
13647 while (fixp
13648 && fixp->fx_frag == fragp
13649 && fixp->fx_where < fragp->fr_fix - second)
13650 {
13651 if (fragp->fr_subtype & RELAX_USE_SECOND)
13652 fixp->fx_done = 1;
13653 fixp = fixp->fx_next;
13654 }
252b5132 13655
4d7206a2
RS
13656 /* Go through the fixups for the second sequence. Disable them if
13657 we're going to use the first sequence, otherwise adjust their
13658 addresses to account for the relaxation. */
13659 while (fixp && fixp->fx_frag == fragp)
13660 {
13661 if (fragp->fr_subtype & RELAX_USE_SECOND)
13662 fixp->fx_where -= first;
13663 else
13664 fixp->fx_done = 1;
13665 fixp = fixp->fx_next;
13666 }
13667
13668 /* Now modify the frag contents. */
13669 if (fragp->fr_subtype & RELAX_USE_SECOND)
13670 {
13671 char *start;
13672
13673 start = fragp->fr_literal + fragp->fr_fix - first - second;
13674 memmove (start, start + first, second);
13675 fragp->fr_fix -= first;
13676 }
13677 else
13678 fragp->fr_fix -= second;
252b5132
RH
13679 }
13680}
13681
13682#ifdef OBJ_ELF
13683
13684/* This function is called after the relocs have been generated.
13685 We've been storing mips16 text labels as odd. Here we convert them
13686 back to even for the convenience of the debugger. */
13687
13688void
17a2f251 13689mips_frob_file_after_relocs (void)
252b5132
RH
13690{
13691 asymbol **syms;
13692 unsigned int count, i;
13693
13694 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
13695 return;
13696
13697 syms = bfd_get_outsymbols (stdoutput);
13698 count = bfd_get_symcount (stdoutput);
13699 for (i = 0; i < count; i++, syms++)
13700 {
13701 if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
13702 && ((*syms)->value & 1) != 0)
13703 {
13704 (*syms)->value &= ~1;
13705 /* If the symbol has an odd size, it was probably computed
13706 incorrectly, so adjust that as well. */
13707 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
13708 ++elf_symbol (*syms)->internal_elf_sym.st_size;
13709 }
13710 }
13711}
13712
13713#endif
13714
13715/* This function is called whenever a label is defined. It is used
13716 when handling branch delays; if a branch has a label, we assume we
13717 can not move it. */
13718
13719void
17a2f251 13720mips_define_label (symbolS *sym)
252b5132
RH
13721{
13722 struct insn_label_list *l;
13723
13724 if (free_insn_labels == NULL)
13725 l = (struct insn_label_list *) xmalloc (sizeof *l);
13726 else
13727 {
13728 l = free_insn_labels;
13729 free_insn_labels = l->next;
13730 }
13731
13732 l->label = sym;
13733 l->next = insn_labels;
13734 insn_labels = l;
07a53e5c
RH
13735
13736#ifdef OBJ_ELF
13737 dwarf2_emit_label (sym);
13738#endif
252b5132
RH
13739}
13740\f
13741#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13742
13743/* Some special processing for a MIPS ELF file. */
13744
13745void
17a2f251 13746mips_elf_final_processing (void)
252b5132
RH
13747{
13748 /* Write out the register information. */
316f5878 13749 if (mips_abi != N64_ABI)
252b5132
RH
13750 {
13751 Elf32_RegInfo s;
13752
13753 s.ri_gprmask = mips_gprmask;
13754 s.ri_cprmask[0] = mips_cprmask[0];
13755 s.ri_cprmask[1] = mips_cprmask[1];
13756 s.ri_cprmask[2] = mips_cprmask[2];
13757 s.ri_cprmask[3] = mips_cprmask[3];
13758 /* The gp_value field is set by the MIPS ELF backend. */
13759
13760 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
13761 ((Elf32_External_RegInfo *)
13762 mips_regmask_frag));
13763 }
13764 else
13765 {
13766 Elf64_Internal_RegInfo s;
13767
13768 s.ri_gprmask = mips_gprmask;
13769 s.ri_pad = 0;
13770 s.ri_cprmask[0] = mips_cprmask[0];
13771 s.ri_cprmask[1] = mips_cprmask[1];
13772 s.ri_cprmask[2] = mips_cprmask[2];
13773 s.ri_cprmask[3] = mips_cprmask[3];
13774 /* The gp_value field is set by the MIPS ELF backend. */
13775
13776 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
13777 ((Elf64_External_RegInfo *)
13778 mips_regmask_frag));
13779 }
13780
13781 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
13782 sort of BFD interface for this. */
13783 if (mips_any_noreorder)
13784 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
13785 if (mips_pic != NO_PIC)
143d77c5 13786 {
252b5132 13787 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
143d77c5
EC
13788 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
13789 }
13790 if (mips_abicalls)
13791 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
252b5132 13792
98d3f06f 13793 /* Set MIPS ELF flags for ASEs. */
74cd071d
CF
13794 /* We may need to define a new flag for DSP ASE, and set this flag when
13795 file_ase_dsp is true. */
ef2e4d86
CF
13796 /* We may need to define a new flag for MT ASE, and set this flag when
13797 file_ase_mt is true. */
a4672219
TS
13798 if (file_ase_mips16)
13799 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
1f25f5d3
CD
13800#if 0 /* XXX FIXME */
13801 if (file_ase_mips3d)
13802 elf_elfheader (stdoutput)->e_flags |= ???;
13803#endif
deec1734
CD
13804 if (file_ase_mdmx)
13805 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
1f25f5d3 13806
bdaaa2e1 13807 /* Set the MIPS ELF ABI flags. */
316f5878 13808 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
252b5132 13809 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
316f5878 13810 else if (mips_abi == O64_ABI)
252b5132 13811 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
316f5878 13812 else if (mips_abi == EABI_ABI)
252b5132 13813 {
316f5878 13814 if (!file_mips_gp32)
252b5132
RH
13815 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
13816 else
13817 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
13818 }
316f5878 13819 else if (mips_abi == N32_ABI)
be00bddd
TS
13820 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
13821
c9914766 13822 /* Nothing to do for N64_ABI. */
252b5132
RH
13823
13824 if (mips_32bitmode)
13825 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
13826}
13827
13828#endif /* OBJ_ELF || OBJ_MAYBE_ELF */
13829\f
beae10d5 13830typedef struct proc {
9b2f1d35
EC
13831 symbolS *func_sym;
13832 symbolS *func_end_sym;
beae10d5
KH
13833 unsigned long reg_mask;
13834 unsigned long reg_offset;
13835 unsigned long fpreg_mask;
13836 unsigned long fpreg_offset;
13837 unsigned long frame_offset;
13838 unsigned long frame_reg;
13839 unsigned long pc_reg;
13840} procS;
252b5132
RH
13841
13842static procS cur_proc;
13843static procS *cur_proc_ptr;
13844static int numprocs;
13845
0a9ef439 13846/* Fill in an rs_align_code fragment. */
a19d8eb0 13847
0a9ef439 13848void
17a2f251 13849mips_handle_align (fragS *fragp)
a19d8eb0 13850{
0a9ef439
RH
13851 if (fragp->fr_type != rs_align_code)
13852 return;
13853
13854 if (mips_opts.mips16)
a19d8eb0
CP
13855 {
13856 static const unsigned char be_nop[] = { 0x65, 0x00 };
13857 static const unsigned char le_nop[] = { 0x00, 0x65 };
13858
0a9ef439
RH
13859 int bytes;
13860 char *p;
a19d8eb0 13861
0a9ef439
RH
13862 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
13863 p = fragp->fr_literal + fragp->fr_fix;
13864
13865 if (bytes & 1)
13866 {
13867 *p++ = 0;
f9419b05 13868 fragp->fr_fix++;
0a9ef439
RH
13869 }
13870
13871 memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
13872 fragp->fr_var = 2;
a19d8eb0
CP
13873 }
13874
0a9ef439 13875 /* For mips32, a nop is a zero, which we trivially get by doing nothing. */
a19d8eb0
CP
13876}
13877
252b5132 13878static void
17a2f251 13879md_obj_begin (void)
252b5132
RH
13880{
13881}
13882
13883static void
17a2f251 13884md_obj_end (void)
252b5132
RH
13885{
13886 /* check for premature end, nesting errors, etc */
13887 if (cur_proc_ptr)
9a41af64 13888 as_warn (_("missing .end at end of assembly"));
252b5132
RH
13889}
13890
13891static long
17a2f251 13892get_number (void)
252b5132
RH
13893{
13894 int negative = 0;
13895 long val = 0;
13896
13897 if (*input_line_pointer == '-')
13898 {
13899 ++input_line_pointer;
13900 negative = 1;
13901 }
3882b010 13902 if (!ISDIGIT (*input_line_pointer))
956cd1d6 13903 as_bad (_("expected simple number"));
252b5132
RH
13904 if (input_line_pointer[0] == '0')
13905 {
13906 if (input_line_pointer[1] == 'x')
13907 {
13908 input_line_pointer += 2;
3882b010 13909 while (ISXDIGIT (*input_line_pointer))
252b5132
RH
13910 {
13911 val <<= 4;
13912 val |= hex_value (*input_line_pointer++);
13913 }
13914 return negative ? -val : val;
13915 }
13916 else
13917 {
13918 ++input_line_pointer;
3882b010 13919 while (ISDIGIT (*input_line_pointer))
252b5132
RH
13920 {
13921 val <<= 3;
13922 val |= *input_line_pointer++ - '0';
13923 }
13924 return negative ? -val : val;
13925 }
13926 }
3882b010 13927 if (!ISDIGIT (*input_line_pointer))
252b5132
RH
13928 {
13929 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
13930 *input_line_pointer, *input_line_pointer);
956cd1d6 13931 as_warn (_("invalid number"));
252b5132
RH
13932 return -1;
13933 }
3882b010 13934 while (ISDIGIT (*input_line_pointer))
252b5132
RH
13935 {
13936 val *= 10;
13937 val += *input_line_pointer++ - '0';
13938 }
13939 return negative ? -val : val;
13940}
13941
13942/* The .file directive; just like the usual .file directive, but there
c5dd6aab
DJ
13943 is an initial number which is the ECOFF file index. In the non-ECOFF
13944 case .file implies DWARF-2. */
13945
13946static void
17a2f251 13947s_mips_file (int x ATTRIBUTE_UNUSED)
c5dd6aab 13948{
ecb4347a
DJ
13949 static int first_file_directive = 0;
13950
c5dd6aab
DJ
13951 if (ECOFF_DEBUGGING)
13952 {
13953 get_number ();
13954 s_app_file (0);
13955 }
13956 else
ecb4347a
DJ
13957 {
13958 char *filename;
13959
13960 filename = dwarf2_directive_file (0);
13961
13962 /* Versions of GCC up to 3.1 start files with a ".file"
13963 directive even for stabs output. Make sure that this
13964 ".file" is handled. Note that you need a version of GCC
13965 after 3.1 in order to support DWARF-2 on MIPS. */
13966 if (filename != NULL && ! first_file_directive)
13967 {
13968 (void) new_logical_line (filename, -1);
c04f5787 13969 s_app_file_string (filename, 0);
ecb4347a
DJ
13970 }
13971 first_file_directive = 1;
13972 }
c5dd6aab
DJ
13973}
13974
13975/* The .loc directive, implying DWARF-2. */
252b5132
RH
13976
13977static void
17a2f251 13978s_mips_loc (int x ATTRIBUTE_UNUSED)
252b5132 13979{
c5dd6aab
DJ
13980 if (!ECOFF_DEBUGGING)
13981 dwarf2_directive_loc (0);
252b5132
RH
13982}
13983
252b5132
RH
13984/* The .end directive. */
13985
13986static void
17a2f251 13987s_mips_end (int x ATTRIBUTE_UNUSED)
252b5132
RH
13988{
13989 symbolS *p;
252b5132 13990
7a621144
DJ
13991 /* Following functions need their own .frame and .cprestore directives. */
13992 mips_frame_reg_valid = 0;
13993 mips_cprestore_valid = 0;
13994
252b5132
RH
13995 if (!is_end_of_line[(unsigned char) *input_line_pointer])
13996 {
13997 p = get_symbol ();
13998 demand_empty_rest_of_line ();
13999 }
14000 else
14001 p = NULL;
14002
14949570 14003 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
14004 as_warn (_(".end not in text section"));
14005
14006 if (!cur_proc_ptr)
14007 {
14008 as_warn (_(".end directive without a preceding .ent directive."));
14009 demand_empty_rest_of_line ();
14010 return;
14011 }
14012
14013 if (p != NULL)
14014 {
14015 assert (S_GET_NAME (p));
9b2f1d35 14016 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
252b5132 14017 as_warn (_(".end symbol does not match .ent symbol."));
ecb4347a
DJ
14018
14019 if (debug_type == DEBUG_STABS)
14020 stabs_generate_asm_endfunc (S_GET_NAME (p),
14021 S_GET_NAME (p));
252b5132
RH
14022 }
14023 else
14024 as_warn (_(".end directive missing or unknown symbol"));
14025
2132e3a3 14026#ifdef OBJ_ELF
9b2f1d35
EC
14027 /* Create an expression to calculate the size of the function. */
14028 if (p && cur_proc_ptr)
14029 {
14030 OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
14031 expressionS *exp = xmalloc (sizeof (expressionS));
14032
14033 obj->size = exp;
14034 exp->X_op = O_subtract;
14035 exp->X_add_symbol = symbol_temp_new_now ();
14036 exp->X_op_symbol = p;
14037 exp->X_add_number = 0;
14038
14039 cur_proc_ptr->func_end_sym = exp->X_add_symbol;
14040 }
14041
ecb4347a 14042 /* Generate a .pdr section. */
dcd410fe
RO
14043 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING
14044 && mips_flag_pdr)
ecb4347a
DJ
14045 {
14046 segT saved_seg = now_seg;
14047 subsegT saved_subseg = now_subseg;
14048 valueT dot;
14049 expressionS exp;
14050 char *fragp;
252b5132 14051
ecb4347a 14052 dot = frag_now_fix ();
252b5132
RH
14053
14054#ifdef md_flush_pending_output
ecb4347a 14055 md_flush_pending_output ();
252b5132
RH
14056#endif
14057
ecb4347a
DJ
14058 assert (pdr_seg);
14059 subseg_set (pdr_seg, 0);
252b5132 14060
ecb4347a
DJ
14061 /* Write the symbol. */
14062 exp.X_op = O_symbol;
14063 exp.X_add_symbol = p;
14064 exp.X_add_number = 0;
14065 emit_expr (&exp, 4);
252b5132 14066
ecb4347a 14067 fragp = frag_more (7 * 4);
252b5132 14068
17a2f251
TS
14069 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
14070 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
14071 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
14072 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
14073 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
14074 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
14075 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
252b5132 14076
ecb4347a
DJ
14077 subseg_set (saved_seg, saved_subseg);
14078 }
14079#endif /* OBJ_ELF */
252b5132
RH
14080
14081 cur_proc_ptr = NULL;
14082}
14083
14084/* The .aent and .ent directives. */
14085
14086static void
17a2f251 14087s_mips_ent (int aent)
252b5132 14088{
252b5132 14089 symbolS *symbolP;
252b5132
RH
14090
14091 symbolP = get_symbol ();
14092 if (*input_line_pointer == ',')
f9419b05 14093 ++input_line_pointer;
252b5132 14094 SKIP_WHITESPACE ();
3882b010 14095 if (ISDIGIT (*input_line_pointer)
d9a62219 14096 || *input_line_pointer == '-')
874e8986 14097 get_number ();
252b5132 14098
14949570 14099 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
14100 as_warn (_(".ent or .aent not in text section."));
14101
14102 if (!aent && cur_proc_ptr)
9a41af64 14103 as_warn (_("missing .end"));
252b5132
RH
14104
14105 if (!aent)
14106 {
7a621144
DJ
14107 /* This function needs its own .frame and .cprestore directives. */
14108 mips_frame_reg_valid = 0;
14109 mips_cprestore_valid = 0;
14110
252b5132
RH
14111 cur_proc_ptr = &cur_proc;
14112 memset (cur_proc_ptr, '\0', sizeof (procS));
14113
9b2f1d35 14114 cur_proc_ptr->func_sym = symbolP;
252b5132 14115
49309057 14116 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
252b5132 14117
f9419b05 14118 ++numprocs;
ecb4347a
DJ
14119
14120 if (debug_type == DEBUG_STABS)
14121 stabs_generate_asm_func (S_GET_NAME (symbolP),
14122 S_GET_NAME (symbolP));
252b5132
RH
14123 }
14124
14125 demand_empty_rest_of_line ();
14126}
14127
14128/* The .frame directive. If the mdebug section is present (IRIX 5 native)
bdaaa2e1 14129 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
252b5132 14130 s_mips_frame is used so that we can set the PDR information correctly.
bdaaa2e1 14131 We can't use the ecoff routines because they make reference to the ecoff
252b5132
RH
14132 symbol table (in the mdebug section). */
14133
14134static void
17a2f251 14135s_mips_frame (int ignore ATTRIBUTE_UNUSED)
252b5132 14136{
ecb4347a
DJ
14137#ifdef OBJ_ELF
14138 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14139 {
14140 long val;
252b5132 14141
ecb4347a
DJ
14142 if (cur_proc_ptr == (procS *) NULL)
14143 {
14144 as_warn (_(".frame outside of .ent"));
14145 demand_empty_rest_of_line ();
14146 return;
14147 }
252b5132 14148
ecb4347a
DJ
14149 cur_proc_ptr->frame_reg = tc_get_register (1);
14150
14151 SKIP_WHITESPACE ();
14152 if (*input_line_pointer++ != ','
14153 || get_absolute_expression_and_terminator (&val) != ',')
14154 {
14155 as_warn (_("Bad .frame directive"));
14156 --input_line_pointer;
14157 demand_empty_rest_of_line ();
14158 return;
14159 }
252b5132 14160
ecb4347a
DJ
14161 cur_proc_ptr->frame_offset = val;
14162 cur_proc_ptr->pc_reg = tc_get_register (0);
252b5132 14163
252b5132 14164 demand_empty_rest_of_line ();
252b5132 14165 }
ecb4347a
DJ
14166 else
14167#endif /* OBJ_ELF */
14168 s_ignore (ignore);
252b5132
RH
14169}
14170
bdaaa2e1
KH
14171/* The .fmask and .mask directives. If the mdebug section is present
14172 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
252b5132 14173 embedded targets, s_mips_mask is used so that we can set the PDR
bdaaa2e1 14174 information correctly. We can't use the ecoff routines because they
252b5132
RH
14175 make reference to the ecoff symbol table (in the mdebug section). */
14176
14177static void
17a2f251 14178s_mips_mask (int reg_type)
252b5132 14179{
ecb4347a
DJ
14180#ifdef OBJ_ELF
14181 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
252b5132 14182 {
ecb4347a 14183 long mask, off;
252b5132 14184
ecb4347a
DJ
14185 if (cur_proc_ptr == (procS *) NULL)
14186 {
14187 as_warn (_(".mask/.fmask outside of .ent"));
14188 demand_empty_rest_of_line ();
14189 return;
14190 }
252b5132 14191
ecb4347a
DJ
14192 if (get_absolute_expression_and_terminator (&mask) != ',')
14193 {
14194 as_warn (_("Bad .mask/.fmask directive"));
14195 --input_line_pointer;
14196 demand_empty_rest_of_line ();
14197 return;
14198 }
252b5132 14199
ecb4347a
DJ
14200 off = get_absolute_expression ();
14201
14202 if (reg_type == 'F')
14203 {
14204 cur_proc_ptr->fpreg_mask = mask;
14205 cur_proc_ptr->fpreg_offset = off;
14206 }
14207 else
14208 {
14209 cur_proc_ptr->reg_mask = mask;
14210 cur_proc_ptr->reg_offset = off;
14211 }
14212
14213 demand_empty_rest_of_line ();
252b5132
RH
14214 }
14215 else
ecb4347a
DJ
14216#endif /* OBJ_ELF */
14217 s_ignore (reg_type);
252b5132
RH
14218}
14219
316f5878
RS
14220/* A table describing all the processors gas knows about. Names are
14221 matched in the order listed.
e7af610e 14222
316f5878
RS
14223 To ease comparison, please keep this table in the same order as
14224 gcc's mips_cpu_info_table[]. */
e972090a
NC
14225static const struct mips_cpu_info mips_cpu_info_table[] =
14226{
316f5878
RS
14227 /* Entries for generic ISAs */
14228 { "mips1", 1, ISA_MIPS1, CPU_R3000 },
14229 { "mips2", 1, ISA_MIPS2, CPU_R6000 },
14230 { "mips3", 1, ISA_MIPS3, CPU_R4000 },
14231 { "mips4", 1, ISA_MIPS4, CPU_R8000 },
14232 { "mips5", 1, ISA_MIPS5, CPU_MIPS5 },
14233 { "mips32", 1, ISA_MIPS32, CPU_MIPS32 },
af7ee8bf 14234 { "mips32r2", 1, ISA_MIPS32R2, CPU_MIPS32R2 },
316f5878 14235 { "mips64", 1, ISA_MIPS64, CPU_MIPS64 },
5f74bc13 14236 { "mips64r2", 1, ISA_MIPS64R2, CPU_MIPS64R2 },
316f5878
RS
14237
14238 /* MIPS I */
14239 { "r3000", 0, ISA_MIPS1, CPU_R3000 },
14240 { "r2000", 0, ISA_MIPS1, CPU_R3000 },
14241 { "r3900", 0, ISA_MIPS1, CPU_R3900 },
14242
14243 /* MIPS II */
14244 { "r6000", 0, ISA_MIPS2, CPU_R6000 },
14245
14246 /* MIPS III */
14247 { "r4000", 0, ISA_MIPS3, CPU_R4000 },
14248 { "r4010", 0, ISA_MIPS2, CPU_R4010 },
14249 { "vr4100", 0, ISA_MIPS3, CPU_VR4100 },
14250 { "vr4111", 0, ISA_MIPS3, CPU_R4111 },
60b63b72
RS
14251 { "vr4120", 0, ISA_MIPS3, CPU_VR4120 },
14252 { "vr4130", 0, ISA_MIPS3, CPU_VR4120 },
14253 { "vr4181", 0, ISA_MIPS3, CPU_R4111 },
316f5878
RS
14254 { "vr4300", 0, ISA_MIPS3, CPU_R4300 },
14255 { "r4400", 0, ISA_MIPS3, CPU_R4400 },
14256 { "r4600", 0, ISA_MIPS3, CPU_R4600 },
14257 { "orion", 0, ISA_MIPS3, CPU_R4600 },
14258 { "r4650", 0, ISA_MIPS3, CPU_R4650 },
14259
14260 /* MIPS IV */
14261 { "r8000", 0, ISA_MIPS4, CPU_R8000 },
14262 { "r10000", 0, ISA_MIPS4, CPU_R10000 },
14263 { "r12000", 0, ISA_MIPS4, CPU_R12000 },
14264 { "vr5000", 0, ISA_MIPS4, CPU_R5000 },
60b63b72
RS
14265 { "vr5400", 0, ISA_MIPS4, CPU_VR5400 },
14266 { "vr5500", 0, ISA_MIPS4, CPU_VR5500 },
316f5878
RS
14267 { "rm5200", 0, ISA_MIPS4, CPU_R5000 },
14268 { "rm5230", 0, ISA_MIPS4, CPU_R5000 },
14269 { "rm5231", 0, ISA_MIPS4, CPU_R5000 },
14270 { "rm5261", 0, ISA_MIPS4, CPU_R5000 },
14271 { "rm5721", 0, ISA_MIPS4, CPU_R5000 },
5a7ea749 14272 { "rm7000", 0, ISA_MIPS4, CPU_RM7000 },
9a92f48d 14273 { "rm9000", 0, ISA_MIPS4, CPU_RM9000 },
316f5878
RS
14274
14275 /* MIPS 32 */
fef14a42 14276 { "4kc", 0, ISA_MIPS32, CPU_MIPS32 },
316f5878
RS
14277 { "4km", 0, ISA_MIPS32, CPU_MIPS32 },
14278 { "4kp", 0, ISA_MIPS32, CPU_MIPS32 },
e7af610e 14279
32b26a03
MR
14280 /* MIPS32 Release 2 */
14281 { "m4k", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
14282 { "24k", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
14283 { "24kc", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
14284 { "24kf", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
14285 { "24kx", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
14286
316f5878
RS
14287 /* MIPS 64 */
14288 { "5kc", 0, ISA_MIPS64, CPU_MIPS64 },
ff324200 14289 { "5kf", 0, ISA_MIPS64, CPU_MIPS64 },
316f5878 14290 { "20kc", 0, ISA_MIPS64, CPU_MIPS64 },
e7af610e 14291
c7a23324 14292 /* Broadcom SB-1 CPU core */
316f5878 14293 { "sb1", 0, ISA_MIPS64, CPU_SB1 },
e7af610e 14294
316f5878
RS
14295 /* End marker */
14296 { NULL, 0, 0, 0 }
14297};
e7af610e 14298
84ea6cf2 14299
316f5878
RS
14300/* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
14301 with a final "000" replaced by "k". Ignore case.
e7af610e 14302
316f5878 14303 Note: this function is shared between GCC and GAS. */
c6c98b38 14304
b34976b6 14305static bfd_boolean
17a2f251 14306mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
14307{
14308 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
14309 given++, canonical++;
14310
14311 return ((*given == 0 && *canonical == 0)
14312 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
14313}
14314
14315
14316/* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
14317 CPU name. We've traditionally allowed a lot of variation here.
14318
14319 Note: this function is shared between GCC and GAS. */
14320
b34976b6 14321static bfd_boolean
17a2f251 14322mips_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
14323{
14324 /* First see if the name matches exactly, or with a final "000"
14325 turned into "k". */
14326 if (mips_strict_matching_cpu_name_p (canonical, given))
b34976b6 14327 return TRUE;
316f5878
RS
14328
14329 /* If not, try comparing based on numerical designation alone.
14330 See if GIVEN is an unadorned number, or 'r' followed by a number. */
14331 if (TOLOWER (*given) == 'r')
14332 given++;
14333 if (!ISDIGIT (*given))
b34976b6 14334 return FALSE;
316f5878
RS
14335
14336 /* Skip over some well-known prefixes in the canonical name,
14337 hoping to find a number there too. */
14338 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
14339 canonical += 2;
14340 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
14341 canonical += 2;
14342 else if (TOLOWER (canonical[0]) == 'r')
14343 canonical += 1;
14344
14345 return mips_strict_matching_cpu_name_p (canonical, given);
14346}
14347
14348
14349/* Parse an option that takes the name of a processor as its argument.
14350 OPTION is the name of the option and CPU_STRING is the argument.
14351 Return the corresponding processor enumeration if the CPU_STRING is
14352 recognized, otherwise report an error and return null.
14353
14354 A similar function exists in GCC. */
e7af610e
NC
14355
14356static const struct mips_cpu_info *
17a2f251 14357mips_parse_cpu (const char *option, const char *cpu_string)
e7af610e 14358{
316f5878 14359 const struct mips_cpu_info *p;
e7af610e 14360
316f5878
RS
14361 /* 'from-abi' selects the most compatible architecture for the given
14362 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
14363 EABIs, we have to decide whether we're using the 32-bit or 64-bit
14364 version. Look first at the -mgp options, if given, otherwise base
14365 the choice on MIPS_DEFAULT_64BIT.
e7af610e 14366
316f5878
RS
14367 Treat NO_ABI like the EABIs. One reason to do this is that the
14368 plain 'mips' and 'mips64' configs have 'from-abi' as their default
14369 architecture. This code picks MIPS I for 'mips' and MIPS III for
14370 'mips64', just as we did in the days before 'from-abi'. */
14371 if (strcasecmp (cpu_string, "from-abi") == 0)
14372 {
14373 if (ABI_NEEDS_32BIT_REGS (mips_abi))
14374 return mips_cpu_info_from_isa (ISA_MIPS1);
14375
14376 if (ABI_NEEDS_64BIT_REGS (mips_abi))
14377 return mips_cpu_info_from_isa (ISA_MIPS3);
14378
14379 if (file_mips_gp32 >= 0)
14380 return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
14381
14382 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
14383 ? ISA_MIPS3
14384 : ISA_MIPS1);
14385 }
14386
14387 /* 'default' has traditionally been a no-op. Probably not very useful. */
14388 if (strcasecmp (cpu_string, "default") == 0)
14389 return 0;
14390
14391 for (p = mips_cpu_info_table; p->name != 0; p++)
14392 if (mips_matching_cpu_name_p (p->name, cpu_string))
14393 return p;
14394
14395 as_bad ("Bad value (%s) for %s", cpu_string, option);
14396 return 0;
e7af610e
NC
14397}
14398
316f5878
RS
14399/* Return the canonical processor information for ISA (a member of the
14400 ISA_MIPS* enumeration). */
14401
e7af610e 14402static const struct mips_cpu_info *
17a2f251 14403mips_cpu_info_from_isa (int isa)
e7af610e
NC
14404{
14405 int i;
14406
14407 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14408 if (mips_cpu_info_table[i].is_isa
316f5878 14409 && isa == mips_cpu_info_table[i].isa)
e7af610e
NC
14410 return (&mips_cpu_info_table[i]);
14411
e972090a 14412 return NULL;
e7af610e 14413}
fef14a42
TS
14414
14415static const struct mips_cpu_info *
17a2f251 14416mips_cpu_info_from_arch (int arch)
fef14a42
TS
14417{
14418 int i;
14419
14420 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14421 if (arch == mips_cpu_info_table[i].cpu)
14422 return (&mips_cpu_info_table[i]);
14423
14424 return NULL;
14425}
316f5878
RS
14426\f
14427static void
17a2f251 14428show (FILE *stream, const char *string, int *col_p, int *first_p)
316f5878
RS
14429{
14430 if (*first_p)
14431 {
14432 fprintf (stream, "%24s", "");
14433 *col_p = 24;
14434 }
14435 else
14436 {
14437 fprintf (stream, ", ");
14438 *col_p += 2;
14439 }
e7af610e 14440
316f5878
RS
14441 if (*col_p + strlen (string) > 72)
14442 {
14443 fprintf (stream, "\n%24s", "");
14444 *col_p = 24;
14445 }
14446
14447 fprintf (stream, "%s", string);
14448 *col_p += strlen (string);
14449
14450 *first_p = 0;
14451}
14452
14453void
17a2f251 14454md_show_usage (FILE *stream)
e7af610e 14455{
316f5878
RS
14456 int column, first;
14457 size_t i;
14458
14459 fprintf (stream, _("\
14460MIPS options:\n\
316f5878
RS
14461-EB generate big endian output\n\
14462-EL generate little endian output\n\
14463-g, -g2 do not remove unneeded NOPs or swap branches\n\
14464-G NUM allow referencing objects up to NUM bytes\n\
14465 implicitly with the gp register [default 8]\n"));
14466 fprintf (stream, _("\
14467-mips1 generate MIPS ISA I instructions\n\
14468-mips2 generate MIPS ISA II instructions\n\
14469-mips3 generate MIPS ISA III instructions\n\
14470-mips4 generate MIPS ISA IV instructions\n\
14471-mips5 generate MIPS ISA V instructions\n\
14472-mips32 generate MIPS32 ISA instructions\n\
af7ee8bf 14473-mips32r2 generate MIPS32 release 2 ISA instructions\n\
316f5878 14474-mips64 generate MIPS64 ISA instructions\n\
5f74bc13 14475-mips64r2 generate MIPS64 release 2 ISA instructions\n\
316f5878
RS
14476-march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
14477
14478 first = 1;
e7af610e
NC
14479
14480 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
316f5878
RS
14481 show (stream, mips_cpu_info_table[i].name, &column, &first);
14482 show (stream, "from-abi", &column, &first);
14483 fputc ('\n', stream);
e7af610e 14484
316f5878
RS
14485 fprintf (stream, _("\
14486-mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
14487-no-mCPU don't generate code specific to CPU.\n\
14488 For -mCPU and -no-mCPU, CPU must be one of:\n"));
14489
14490 first = 1;
14491
14492 show (stream, "3900", &column, &first);
14493 show (stream, "4010", &column, &first);
14494 show (stream, "4100", &column, &first);
14495 show (stream, "4650", &column, &first);
14496 fputc ('\n', stream);
14497
14498 fprintf (stream, _("\
14499-mips16 generate mips16 instructions\n\
14500-no-mips16 do not generate mips16 instructions\n"));
14501 fprintf (stream, _("\
74cd071d
CF
14502-mdsp generate DSP instructions\n\
14503-mno-dsp do not generate DSP instructions\n"));
14504 fprintf (stream, _("\
ef2e4d86
CF
14505-mmt generate MT instructions\n\
14506-mno-mt do not generate MT instructions\n"));
14507 fprintf (stream, _("\
d766e8ec 14508-mfix-vr4120 work around certain VR4120 errata\n\
7d8e00cf 14509-mfix-vr4130 work around VR4130 mflo/mfhi errata\n\
316f5878
RS
14510-mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
14511-mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
aed1a261
RS
14512-mno-shared optimize output for executables\n\
14513-msym32 assume all symbols have 32-bit values\n\
316f5878
RS
14514-O0 remove unneeded NOPs, do not swap branches\n\
14515-O remove unneeded NOPs and swap branches\n\
316f5878
RS
14516--[no-]construct-floats [dis]allow floating point values to be constructed\n\
14517--trap, --no-break trap exception on div by 0 and mult overflow\n\
14518--break, --no-trap break exception on div by 0 and mult overflow\n"));
14519#ifdef OBJ_ELF
14520 fprintf (stream, _("\
14521-KPIC, -call_shared generate SVR4 position independent code\n\
14522-non_shared do not generate position independent code\n\
14523-xgot assume a 32 bit GOT\n\
dcd410fe 14524-mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
bbe506e8
TS
14525-mshared, -mno-shared disable/enable .cpload optimization for\n\
14526 non-shared code\n\
316f5878
RS
14527-mabi=ABI create ABI conformant object file for:\n"));
14528
14529 first = 1;
14530
14531 show (stream, "32", &column, &first);
14532 show (stream, "o64", &column, &first);
14533 show (stream, "n32", &column, &first);
14534 show (stream, "64", &column, &first);
14535 show (stream, "eabi", &column, &first);
14536
14537 fputc ('\n', stream);
14538
14539 fprintf (stream, _("\
14540-32 create o32 ABI object file (default)\n\
14541-n32 create n32 ABI object file\n\
14542-64 create 64 ABI object file\n"));
14543#endif
e7af610e 14544}
14e777e0
KB
14545
14546enum dwarf2_format
17a2f251 14547mips_dwarf2_format (void)
14e777e0
KB
14548{
14549 if (mips_abi == N64_ABI)
1de5b6a1
AO
14550 {
14551#ifdef TE_IRIX
14552 return dwarf2_format_64bit_irix;
14553#else
14554 return dwarf2_format_64bit;
14555#endif
14556 }
14e777e0
KB
14557 else
14558 return dwarf2_format_32bit;
14559}
73369e65
EC
14560
14561int
14562mips_dwarf2_addr_size (void)
14563{
14564 if (mips_abi == N64_ABI)
14565 return 8;
73369e65
EC
14566 else
14567 return 4;
14568}
5862107c
EC
14569
14570/* Standard calling conventions leave the CFA at SP on entry. */
14571void
14572mips_cfi_frame_initial_instructions (void)
14573{
14574 cfi_add_CFA_def_cfa_register (SP);
14575}
14576
This page took 1.475374 seconds and 4 git commands to generate.