* ldexp.c (fold_name <SIZEOF>): Return 0 for non-existent section.
[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)
252b5132
RH
2315 {
2316 /* Make sure there is enough room to swap this instruction with
2317 a following jump instruction. */
2318 frag_grow (6);
1e915849 2319 add_fixed_insn (ip);
252b5132
RH
2320 }
2321 else
2322 {
2323 if (mips_opts.mips16
2324 && mips_opts.noreorder
2325 && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
2326 as_warn (_("extended instruction in delay slot"));
2327
4d7206a2
RS
2328 if (mips_relax.sequence)
2329 {
2330 /* If we've reached the end of this frag, turn it into a variant
2331 frag and record the information for the instructions we've
2332 written so far. */
2333 if (frag_room () < 4)
2334 relax_close_frag ();
2335 mips_relax.sizes[mips_relax.sequence - 1] += 4;
2336 }
2337
584892a6
RS
2338 if (mips_relax.sequence != 2)
2339 mips_macro_warning.sizes[0] += 4;
2340 if (mips_relax.sequence != 1)
2341 mips_macro_warning.sizes[1] += 4;
2342
1e915849
RS
2343 if (mips_opts.mips16)
2344 {
2345 ip->fixed_p = 1;
2346 ip->mips16_absolute_jump_p = (*reloc_type == BFD_RELOC_MIPS16_JMP);
2347 }
2348 add_fixed_insn (ip);
252b5132
RH
2349 }
2350
01a3f561 2351 if (address_expr != NULL && *reloc_type <= BFD_RELOC_UNUSED)
252b5132
RH
2352 {
2353 if (address_expr->X_op == O_constant)
2354 {
f17c130b 2355 unsigned int tmp;
f6688943
TS
2356
2357 switch (*reloc_type)
252b5132
RH
2358 {
2359 case BFD_RELOC_32:
2360 ip->insn_opcode |= address_expr->X_add_number;
2361 break;
2362
f6688943 2363 case BFD_RELOC_MIPS_HIGHEST:
f17c130b
AM
2364 tmp = (address_expr->X_add_number + 0x800080008000ull) >> 48;
2365 ip->insn_opcode |= tmp & 0xffff;
f6688943
TS
2366 break;
2367
2368 case BFD_RELOC_MIPS_HIGHER:
f17c130b
AM
2369 tmp = (address_expr->X_add_number + 0x80008000ull) >> 32;
2370 ip->insn_opcode |= tmp & 0xffff;
f6688943
TS
2371 break;
2372
2373 case BFD_RELOC_HI16_S:
f17c130b
AM
2374 tmp = (address_expr->X_add_number + 0x8000) >> 16;
2375 ip->insn_opcode |= tmp & 0xffff;
f6688943
TS
2376 break;
2377
2378 case BFD_RELOC_HI16:
2379 ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
2380 break;
2381
01a3f561 2382 case BFD_RELOC_UNUSED:
252b5132 2383 case BFD_RELOC_LO16:
ed6fb7bd 2384 case BFD_RELOC_MIPS_GOT_DISP:
252b5132
RH
2385 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2386 break;
2387
2388 case BFD_RELOC_MIPS_JMP:
2389 if ((address_expr->X_add_number & 3) != 0)
2390 as_bad (_("jump to misaligned address (0x%lx)"),
2391 (unsigned long) address_expr->X_add_number);
f3c0ec86 2392 if (address_expr->X_add_number & ~0xfffffff)
7496292d
TS
2393 as_bad (_("jump address range overflow (0x%lx)"),
2394 (unsigned long) address_expr->X_add_number);
252b5132
RH
2395 ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
2396 break;
2397
2398 case BFD_RELOC_MIPS16_JMP:
2399 if ((address_expr->X_add_number & 3) != 0)
2400 as_bad (_("jump to misaligned address (0x%lx)"),
2401 (unsigned long) address_expr->X_add_number);
f3c0ec86 2402 if (address_expr->X_add_number & ~0xfffffff)
7496292d
TS
2403 as_bad (_("jump address range overflow (0x%lx)"),
2404 (unsigned long) address_expr->X_add_number);
252b5132
RH
2405 ip->insn_opcode |=
2406 (((address_expr->X_add_number & 0x7c0000) << 3)
2407 | ((address_expr->X_add_number & 0xf800000) >> 7)
2408 | ((address_expr->X_add_number & 0x3fffc) >> 2));
2409 break;
2410
252b5132
RH
2411 case BFD_RELOC_16_PCREL_S2:
2412 goto need_reloc;
2413
2414 default:
2415 internalError ();
2416 }
2417 }
01a3f561 2418 else if (*reloc_type < BFD_RELOC_UNUSED)
252b5132 2419 need_reloc:
4d7206a2
RS
2420 {
2421 reloc_howto_type *howto;
2422 int i;
34ce925e 2423
4d7206a2
RS
2424 /* In a compound relocation, it is the final (outermost)
2425 operator that determines the relocated field. */
2426 for (i = 1; i < 3; i++)
2427 if (reloc_type[i] == BFD_RELOC_UNUSED)
2428 break;
34ce925e 2429
4d7206a2 2430 howto = bfd_reloc_type_lookup (stdoutput, reloc_type[i - 1]);
1e915849
RS
2431 ip->fixp[0] = fix_new_exp (ip->frag, ip->where,
2432 bfd_get_reloc_size (howto),
2433 address_expr,
2434 reloc_type[0] == BFD_RELOC_16_PCREL_S2,
2435 reloc_type[0]);
4d7206a2
RS
2436
2437 /* These relocations can have an addend that won't fit in
2438 4 octets for 64bit assembly. */
2439 if (HAVE_64BIT_GPRS
2440 && ! howto->partial_inplace
2441 && (reloc_type[0] == BFD_RELOC_16
2442 || reloc_type[0] == BFD_RELOC_32
2443 || reloc_type[0] == BFD_RELOC_MIPS_JMP
2444 || reloc_type[0] == BFD_RELOC_HI16_S
2445 || reloc_type[0] == BFD_RELOC_LO16
2446 || reloc_type[0] == BFD_RELOC_GPREL16
2447 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
2448 || reloc_type[0] == BFD_RELOC_GPREL32
2449 || reloc_type[0] == BFD_RELOC_64
2450 || reloc_type[0] == BFD_RELOC_CTOR
2451 || reloc_type[0] == BFD_RELOC_MIPS_SUB
2452 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
2453 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
2454 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
2455 || reloc_type[0] == BFD_RELOC_MIPS_REL16
d6f16593
MR
2456 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
2457 || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
2458 || reloc_type[0] == BFD_RELOC_MIPS16_HI16_S
2459 || reloc_type[0] == BFD_RELOC_MIPS16_LO16))
1e915849 2460 ip->fixp[0]->fx_no_overflow = 1;
4d7206a2
RS
2461
2462 if (mips_relax.sequence)
2463 {
2464 if (mips_relax.first_fixup == 0)
1e915849 2465 mips_relax.first_fixup = ip->fixp[0];
4d7206a2
RS
2466 }
2467 else if (reloc_needs_lo_p (*reloc_type))
2468 {
2469 struct mips_hi_fixup *hi_fixup;
252b5132 2470
4d7206a2
RS
2471 /* Reuse the last entry if it already has a matching %lo. */
2472 hi_fixup = mips_hi_fixup_list;
2473 if (hi_fixup == 0
2474 || !fixup_has_matching_lo_p (hi_fixup->fixp))
2475 {
2476 hi_fixup = ((struct mips_hi_fixup *)
2477 xmalloc (sizeof (struct mips_hi_fixup)));
2478 hi_fixup->next = mips_hi_fixup_list;
2479 mips_hi_fixup_list = hi_fixup;
252b5132 2480 }
1e915849 2481 hi_fixup->fixp = ip->fixp[0];
4d7206a2
RS
2482 hi_fixup->seg = now_seg;
2483 }
f6688943 2484
4d7206a2
RS
2485 /* Add fixups for the second and third relocations, if given.
2486 Note that the ABI allows the second relocation to be
2487 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the
2488 moment we only use RSS_UNDEF, but we could add support
2489 for the others if it ever becomes necessary. */
2490 for (i = 1; i < 3; i++)
2491 if (reloc_type[i] != BFD_RELOC_UNUSED)
2492 {
1e915849
RS
2493 ip->fixp[i] = fix_new (ip->frag, ip->where,
2494 ip->fixp[0]->fx_size, NULL, 0,
2495 FALSE, reloc_type[i]);
b1dca8ee
RS
2496
2497 /* Use fx_tcbit to mark compound relocs. */
1e915849
RS
2498 ip->fixp[0]->fx_tcbit = 1;
2499 ip->fixp[i]->fx_tcbit = 1;
4d7206a2 2500 }
252b5132
RH
2501 }
2502 }
1e915849 2503 install_insn (ip);
252b5132
RH
2504
2505 /* Update the register mask information. */
2506 if (! mips_opts.mips16)
2507 {
2508 if (pinfo & INSN_WRITE_GPR_D)
bf12938e 2509 mips_gprmask |= 1 << EXTRACT_OPERAND (RD, *ip);
252b5132 2510 if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
bf12938e 2511 mips_gprmask |= 1 << EXTRACT_OPERAND (RT, *ip);
252b5132 2512 if (pinfo & INSN_READ_GPR_S)
bf12938e 2513 mips_gprmask |= 1 << EXTRACT_OPERAND (RS, *ip);
252b5132 2514 if (pinfo & INSN_WRITE_GPR_31)
f9419b05 2515 mips_gprmask |= 1 << RA;
252b5132 2516 if (pinfo & INSN_WRITE_FPR_D)
bf12938e 2517 mips_cprmask[1] |= 1 << EXTRACT_OPERAND (FD, *ip);
252b5132 2518 if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
bf12938e 2519 mips_cprmask[1] |= 1 << EXTRACT_OPERAND (FS, *ip);
252b5132 2520 if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
bf12938e 2521 mips_cprmask[1] |= 1 << EXTRACT_OPERAND (FT, *ip);
252b5132 2522 if ((pinfo & INSN_READ_FPR_R) != 0)
bf12938e 2523 mips_cprmask[1] |= 1 << EXTRACT_OPERAND (FR, *ip);
252b5132
RH
2524 if (pinfo & INSN_COP)
2525 {
bdaaa2e1
KH
2526 /* We don't keep enough information to sort these cases out.
2527 The itbl support does keep this information however, although
2528 we currently don't support itbl fprmats as part of the cop
2529 instruction. May want to add this support in the future. */
252b5132
RH
2530 }
2531 /* Never set the bit for $0, which is always zero. */
beae10d5 2532 mips_gprmask &= ~1 << 0;
252b5132
RH
2533 }
2534 else
2535 {
2536 if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
bf12938e 2537 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (RX, *ip);
252b5132 2538 if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
bf12938e 2539 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (RY, *ip);
252b5132 2540 if (pinfo & MIPS16_INSN_WRITE_Z)
bf12938e 2541 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (RZ, *ip);
252b5132
RH
2542 if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2543 mips_gprmask |= 1 << TREG;
2544 if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2545 mips_gprmask |= 1 << SP;
2546 if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2547 mips_gprmask |= 1 << RA;
2548 if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2549 mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2550 if (pinfo & MIPS16_INSN_READ_Z)
bf12938e 2551 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (MOVE32Z, *ip);
252b5132 2552 if (pinfo & MIPS16_INSN_READ_GPR_X)
bf12938e 2553 mips_gprmask |= 1 << MIPS16_EXTRACT_OPERAND (REGR32, *ip);
252b5132
RH
2554 }
2555
4d7206a2 2556 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
252b5132
RH
2557 {
2558 /* Filling the branch delay slot is more complex. We try to
2559 switch the branch with the previous instruction, which we can
2560 do if the previous instruction does not set up a condition
2561 that the branch tests and if the branch is not itself the
2562 target of any branch. */
2563 if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2564 || (pinfo & INSN_COND_BRANCH_DELAY))
2565 {
2566 if (mips_optimize < 2
2567 /* If we have seen .set volatile or .set nomove, don't
2568 optimize. */
2569 || mips_opts.nomove != 0
a38419a5
RS
2570 /* We can't swap if the previous instruction's position
2571 is fixed. */
2572 || history[0].fixed_p
252b5132
RH
2573 /* If the previous previous insn was in a .set
2574 noreorder, we can't swap. Actually, the MIPS
2575 assembler will swap in this situation. However, gcc
2576 configured -with-gnu-as will generate code like
2577 .set noreorder
2578 lw $4,XXX
2579 .set reorder
2580 INSN
2581 bne $4,$0,foo
2582 in which we can not swap the bne and INSN. If gcc is
2583 not configured -with-gnu-as, it does not output the
a38419a5 2584 .set pseudo-ops. */
47e39b9d 2585 || history[1].noreorder_p
252b5132
RH
2586 /* If the branch is itself the target of a branch, we
2587 can not swap. We cheat on this; all we check for is
2588 whether there is a label on this instruction. If
2589 there are any branches to anything other than a
2590 label, users must use .set noreorder. */
2591 || insn_labels != NULL
895921c9
MR
2592 /* If the previous instruction is in a variant frag
2593 other than this branch's one, we cannot do the swap.
2594 This does not apply to the mips16, which uses variant
2595 frags for different purposes. */
252b5132 2596 || (! mips_opts.mips16
895921c9 2597 && prev_insn_frag_type == rs_machine_dependent)
71400594
RS
2598 /* Check for conflicts between the branch and the instructions
2599 before the candidate delay slot. */
2600 || nops_for_insn (history + 1, ip) > 0
2601 /* Check for conflicts between the swapped sequence and the
2602 target of the branch. */
2603 || nops_for_sequence (2, history + 1, ip, history) > 0
252b5132
RH
2604 /* We do not swap with a trap instruction, since it
2605 complicates trap handlers to have the trap
2606 instruction be in a delay slot. */
2607 || (prev_pinfo & INSN_TRAP)
2608 /* If the branch reads a register that the previous
2609 instruction sets, we can not swap. */
2610 || (! mips_opts.mips16
2611 && (prev_pinfo & INSN_WRITE_GPR_T)
bf12938e 2612 && insn_uses_reg (ip, EXTRACT_OPERAND (RT, history[0]),
252b5132
RH
2613 MIPS_GR_REG))
2614 || (! mips_opts.mips16
2615 && (prev_pinfo & INSN_WRITE_GPR_D)
bf12938e 2616 && insn_uses_reg (ip, EXTRACT_OPERAND (RD, history[0]),
252b5132
RH
2617 MIPS_GR_REG))
2618 || (mips_opts.mips16
2619 && (((prev_pinfo & MIPS16_INSN_WRITE_X)
bf12938e
RS
2620 && (insn_uses_reg
2621 (ip, MIPS16_EXTRACT_OPERAND (RX, history[0]),
2622 MIPS16_REG)))
252b5132 2623 || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
bf12938e
RS
2624 && (insn_uses_reg
2625 (ip, MIPS16_EXTRACT_OPERAND (RY, history[0]),
2626 MIPS16_REG)))
252b5132 2627 || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
bf12938e
RS
2628 && (insn_uses_reg
2629 (ip, MIPS16_EXTRACT_OPERAND (RZ, history[0]),
2630 MIPS16_REG)))
252b5132
RH
2631 || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2632 && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2633 || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2634 && insn_uses_reg (ip, RA, MIPS_GR_REG))
2635 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2636 && insn_uses_reg (ip,
47e39b9d
RS
2637 MIPS16OP_EXTRACT_REG32R
2638 (history[0].insn_opcode),
252b5132
RH
2639 MIPS_GR_REG))))
2640 /* If the branch writes a register that the previous
2641 instruction sets, we can not swap (we know that
2642 branches write only to RD or to $31). */
2643 || (! mips_opts.mips16
2644 && (prev_pinfo & INSN_WRITE_GPR_T)
2645 && (((pinfo & INSN_WRITE_GPR_D)
bf12938e
RS
2646 && (EXTRACT_OPERAND (RT, history[0])
2647 == EXTRACT_OPERAND (RD, *ip)))
252b5132 2648 || ((pinfo & INSN_WRITE_GPR_31)
bf12938e 2649 && EXTRACT_OPERAND (RT, history[0]) == RA)))
252b5132
RH
2650 || (! mips_opts.mips16
2651 && (prev_pinfo & INSN_WRITE_GPR_D)
2652 && (((pinfo & INSN_WRITE_GPR_D)
bf12938e
RS
2653 && (EXTRACT_OPERAND (RD, history[0])
2654 == EXTRACT_OPERAND (RD, *ip)))
252b5132 2655 || ((pinfo & INSN_WRITE_GPR_31)
bf12938e 2656 && EXTRACT_OPERAND (RD, history[0]) == RA)))
252b5132
RH
2657 || (mips_opts.mips16
2658 && (pinfo & MIPS16_INSN_WRITE_31)
2659 && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2660 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
47e39b9d 2661 && (MIPS16OP_EXTRACT_REG32R (history[0].insn_opcode)
252b5132
RH
2662 == RA))))
2663 /* If the branch writes a register that the previous
2664 instruction reads, we can not swap (we know that
2665 branches only write to RD or to $31). */
2666 || (! mips_opts.mips16
2667 && (pinfo & INSN_WRITE_GPR_D)
47e39b9d 2668 && insn_uses_reg (&history[0],
bf12938e 2669 EXTRACT_OPERAND (RD, *ip),
252b5132
RH
2670 MIPS_GR_REG))
2671 || (! mips_opts.mips16
2672 && (pinfo & INSN_WRITE_GPR_31)
47e39b9d 2673 && insn_uses_reg (&history[0], RA, MIPS_GR_REG))
252b5132
RH
2674 || (mips_opts.mips16
2675 && (pinfo & MIPS16_INSN_WRITE_31)
47e39b9d 2676 && insn_uses_reg (&history[0], RA, MIPS_GR_REG))
252b5132
RH
2677 /* If one instruction sets a condition code and the
2678 other one uses a condition code, we can not swap. */
2679 || ((pinfo & INSN_READ_COND_CODE)
2680 && (prev_pinfo & INSN_WRITE_COND_CODE))
2681 || ((pinfo & INSN_WRITE_COND_CODE)
2682 && (prev_pinfo & INSN_READ_COND_CODE))
2683 /* If the previous instruction uses the PC, we can not
2684 swap. */
2685 || (mips_opts.mips16
2686 && (prev_pinfo & MIPS16_INSN_READ_PC))
252b5132
RH
2687 /* If the previous instruction had a fixup in mips16
2688 mode, we can not swap. This normally means that the
2689 previous instruction was a 4 byte branch anyhow. */
47e39b9d 2690 || (mips_opts.mips16 && history[0].fixp[0])
bdaaa2e1
KH
2691 /* If the previous instruction is a sync, sync.l, or
2692 sync.p, we can not swap. */
f173e82e 2693 || (prev_pinfo & INSN_SYNC))
252b5132
RH
2694 {
2695 /* We could do even better for unconditional branches to
2696 portions of this object file; we could pick up the
2697 instruction at the destination, put it in the delay
2698 slot, and bump the destination address. */
1e915849 2699 insert_into_history (0, 1, ip);
252b5132 2700 emit_nop ();
dd22970f
ILT
2701 if (mips_relax.sequence)
2702 mips_relax.sizes[mips_relax.sequence - 1] += 4;
252b5132
RH
2703 }
2704 else
2705 {
2706 /* It looks like we can actually do the swap. */
1e915849
RS
2707 struct mips_cl_insn delay = history[0];
2708 if (mips_opts.mips16)
252b5132 2709 {
e9df6573
DU
2710 if (delay.frag == ip->frag)
2711 {
2712 move_insn (ip, delay.frag, delay.where);
2713 move_insn (&delay, ip->frag, delay.where
2714 + insn_length (ip));
2715 }
2716 else if (insn_length (ip) == insn_length (&delay))
2717 {
2718 move_insn (&delay, ip->frag, ip->where);
2719 move_insn (ip, history[0].frag, history[0].where);
2720 }
2721 else
2722 {
2723 add_fixed_insn (NOP_INSN);
2724 delay = *NOP_INSN;
2725 }
1e915849
RS
2726 }
2727 else if (relaxed_branch)
2728 {
2729 /* Add the delay slot instruction to the end of the
2730 current frag and shrink the fixed part of the
2731 original frag. If the branch occupies the tail of
2732 the latter, move it backwards to cover the gap. */
2733 delay.frag->fr_fix -= 4;
2734 if (delay.frag == ip->frag)
2735 move_insn (ip, ip->frag, ip->where - 4);
2736 add_fixed_insn (&delay);
252b5132
RH
2737 }
2738 else
2739 {
1e915849
RS
2740 move_insn (&delay, ip->frag, ip->where);
2741 move_insn (ip, history[0].frag, history[0].where);
252b5132 2742 }
1e915849
RS
2743 history[0] = *ip;
2744 delay.fixed_p = 1;
2745 insert_into_history (0, 1, &delay);
252b5132 2746 }
252b5132
RH
2747
2748 /* If that was an unconditional branch, forget the previous
2749 insn information. */
2750 if (pinfo & INSN_UNCOND_BRANCH_DELAY)
7d10b47d 2751 mips_no_prev_insn ();
252b5132
RH
2752 }
2753 else if (pinfo & INSN_COND_BRANCH_LIKELY)
2754 {
2755 /* We don't yet optimize a branch likely. What we should do
2756 is look at the target, copy the instruction found there
2757 into the delay slot, and increment the branch to jump to
2758 the next instruction. */
1e915849 2759 insert_into_history (0, 1, ip);
252b5132 2760 emit_nop ();
252b5132
RH
2761 }
2762 else
1e915849 2763 insert_into_history (0, 1, ip);
252b5132 2764 }
1e915849
RS
2765 else
2766 insert_into_history (0, 1, ip);
252b5132
RH
2767
2768 /* We just output an insn, so the next one doesn't have a label. */
2769 mips_clear_insn_labels ();
252b5132
RH
2770}
2771
7d10b47d 2772/* Forget that there was any previous instruction or label. */
252b5132
RH
2773
2774static void
7d10b47d 2775mips_no_prev_insn (void)
252b5132 2776{
7d10b47d
RS
2777 prev_nop_frag = NULL;
2778 insert_into_history (0, ARRAY_SIZE (history), NOP_INSN);
252b5132
RH
2779 mips_clear_insn_labels ();
2780}
2781
7d10b47d
RS
2782/* This function must be called before we emit something other than
2783 instructions. It is like mips_no_prev_insn except that it inserts
2784 any NOPS that might be needed by previous instructions. */
252b5132 2785
7d10b47d
RS
2786void
2787mips_emit_delays (void)
252b5132
RH
2788{
2789 if (! mips_opts.noreorder)
2790 {
71400594 2791 int nops = nops_for_insn (history, NULL);
252b5132
RH
2792 if (nops > 0)
2793 {
7d10b47d
RS
2794 while (nops-- > 0)
2795 add_fixed_insn (NOP_INSN);
2796 mips_move_labels ();
2797 }
2798 }
2799 mips_no_prev_insn ();
2800}
2801
2802/* Start a (possibly nested) noreorder block. */
2803
2804static void
2805start_noreorder (void)
2806{
2807 if (mips_opts.noreorder == 0)
2808 {
2809 unsigned int i;
2810 int nops;
2811
2812 /* None of the instructions before the .set noreorder can be moved. */
2813 for (i = 0; i < ARRAY_SIZE (history); i++)
2814 history[i].fixed_p = 1;
2815
2816 /* Insert any nops that might be needed between the .set noreorder
2817 block and the previous instructions. We will later remove any
2818 nops that turn out not to be needed. */
2819 nops = nops_for_insn (history, NULL);
2820 if (nops > 0)
2821 {
2822 if (mips_optimize != 0)
252b5132
RH
2823 {
2824 /* Record the frag which holds the nop instructions, so
2825 that we can remove them if we don't need them. */
2826 frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2827 prev_nop_frag = frag_now;
2828 prev_nop_frag_holds = nops;
2829 prev_nop_frag_required = 0;
2830 prev_nop_frag_since = 0;
2831 }
2832
2833 for (; nops > 0; --nops)
1e915849 2834 add_fixed_insn (NOP_INSN);
252b5132 2835
7d10b47d
RS
2836 /* Move on to a new frag, so that it is safe to simply
2837 decrease the size of prev_nop_frag. */
2838 frag_wane (frag_now);
2839 frag_new (0);
404a8071 2840 mips_move_labels ();
252b5132 2841 }
7d10b47d
RS
2842 mips16_mark_labels ();
2843 mips_clear_insn_labels ();
252b5132 2844 }
7d10b47d
RS
2845 mips_opts.noreorder++;
2846 mips_any_noreorder = 1;
2847}
252b5132 2848
7d10b47d 2849/* End a nested noreorder block. */
252b5132 2850
7d10b47d
RS
2851static void
2852end_noreorder (void)
2853{
2854 mips_opts.noreorder--;
2855 if (mips_opts.noreorder == 0 && prev_nop_frag != NULL)
2856 {
2857 /* Commit to inserting prev_nop_frag_required nops and go back to
2858 handling nop insertion the .set reorder way. */
2859 prev_nop_frag->fr_fix -= ((prev_nop_frag_holds - prev_nop_frag_required)
2860 * (mips_opts.mips16 ? 2 : 4));
2861 insert_into_history (prev_nop_frag_since,
2862 prev_nop_frag_required, NOP_INSN);
2863 prev_nop_frag = NULL;
2864 }
252b5132
RH
2865}
2866
584892a6
RS
2867/* Set up global variables for the start of a new macro. */
2868
2869static void
2870macro_start (void)
2871{
2872 memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
2873 mips_macro_warning.delay_slot_p = (mips_opts.noreorder
47e39b9d 2874 && (history[0].insn_mo->pinfo
584892a6
RS
2875 & (INSN_UNCOND_BRANCH_DELAY
2876 | INSN_COND_BRANCH_DELAY
2877 | INSN_COND_BRANCH_LIKELY)) != 0);
2878}
2879
2880/* Given that a macro is longer than 4 bytes, return the appropriate warning
2881 for it. Return null if no warning is needed. SUBTYPE is a bitmask of
2882 RELAX_DELAY_SLOT and RELAX_NOMACRO. */
2883
2884static const char *
2885macro_warning (relax_substateT subtype)
2886{
2887 if (subtype & RELAX_DELAY_SLOT)
2888 return _("Macro instruction expanded into multiple instructions"
2889 " in a branch delay slot");
2890 else if (subtype & RELAX_NOMACRO)
2891 return _("Macro instruction expanded into multiple instructions");
2892 else
2893 return 0;
2894}
2895
2896/* Finish up a macro. Emit warnings as appropriate. */
2897
2898static void
2899macro_end (void)
2900{
2901 if (mips_macro_warning.sizes[0] > 4 || mips_macro_warning.sizes[1] > 4)
2902 {
2903 relax_substateT subtype;
2904
2905 /* Set up the relaxation warning flags. */
2906 subtype = 0;
2907 if (mips_macro_warning.sizes[1] > mips_macro_warning.sizes[0])
2908 subtype |= RELAX_SECOND_LONGER;
2909 if (mips_opts.warn_about_macros)
2910 subtype |= RELAX_NOMACRO;
2911 if (mips_macro_warning.delay_slot_p)
2912 subtype |= RELAX_DELAY_SLOT;
2913
2914 if (mips_macro_warning.sizes[0] > 4 && mips_macro_warning.sizes[1] > 4)
2915 {
2916 /* Either the macro has a single implementation or both
2917 implementations are longer than 4 bytes. Emit the
2918 warning now. */
2919 const char *msg = macro_warning (subtype);
2920 if (msg != 0)
2921 as_warn (msg);
2922 }
2923 else
2924 {
2925 /* One implementation might need a warning but the other
2926 definitely doesn't. */
2927 mips_macro_warning.first_frag->fr_subtype |= subtype;
2928 }
2929 }
2930}
2931
6e1304d8
RS
2932/* Read a macro's relocation codes from *ARGS and store them in *R.
2933 The first argument in *ARGS will be either the code for a single
2934 relocation or -1 followed by the three codes that make up a
2935 composite relocation. */
2936
2937static void
2938macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
2939{
2940 int i, next;
2941
2942 next = va_arg (*args, int);
2943 if (next >= 0)
2944 r[0] = (bfd_reloc_code_real_type) next;
2945 else
2946 for (i = 0; i < 3; i++)
2947 r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
2948}
2949
252b5132
RH
2950/* Build an instruction created by a macro expansion. This is passed
2951 a pointer to the count of instructions created so far, an
2952 expression, the name of the instruction to build, an operand format
2953 string, and corresponding arguments. */
2954
252b5132 2955static void
67c0d1eb 2956macro_build (expressionS *ep, const char *name, const char *fmt, ...)
252b5132 2957{
1e915849 2958 const struct mips_opcode *mo;
252b5132 2959 struct mips_cl_insn insn;
f6688943 2960 bfd_reloc_code_real_type r[3];
252b5132 2961 va_list args;
252b5132 2962
252b5132 2963 va_start (args, fmt);
252b5132 2964
252b5132
RH
2965 if (mips_opts.mips16)
2966 {
67c0d1eb 2967 mips16_macro_build (ep, name, fmt, args);
252b5132
RH
2968 va_end (args);
2969 return;
2970 }
2971
f6688943
TS
2972 r[0] = BFD_RELOC_UNUSED;
2973 r[1] = BFD_RELOC_UNUSED;
2974 r[2] = BFD_RELOC_UNUSED;
1e915849
RS
2975 mo = (struct mips_opcode *) hash_find (op_hash, name);
2976 assert (mo);
2977 assert (strcmp (name, mo->name) == 0);
2978
2979 /* Search until we get a match for NAME. It is assumed here that
2980 macros will never generate MDMX or MIPS-3D instructions. */
2981 while (strcmp (fmt, mo->args) != 0
2982 || mo->pinfo == INSN_MACRO
2983 || !OPCODE_IS_MEMBER (mo,
2984 (mips_opts.isa
2985 | (file_ase_mips16 ? INSN_MIPS16 : 0)),
fef14a42 2986 mips_opts.arch)
1e915849
RS
2987 || (mips_opts.arch == CPU_R4650 && (mo->pinfo & FP_D) != 0))
2988 {
2989 ++mo;
2990 assert (mo->name);
2991 assert (strcmp (name, mo->name) == 0);
252b5132
RH
2992 }
2993
1e915849 2994 create_insn (&insn, mo);
252b5132
RH
2995 for (;;)
2996 {
2997 switch (*fmt++)
2998 {
2999 case '\0':
3000 break;
3001
3002 case ',':
3003 case '(':
3004 case ')':
3005 continue;
3006
5f74bc13
CD
3007 case '+':
3008 switch (*fmt++)
3009 {
3010 case 'A':
3011 case 'E':
bf12938e 3012 INSERT_OPERAND (SHAMT, insn, va_arg (args, int));
5f74bc13
CD
3013 continue;
3014
3015 case 'B':
3016 case 'F':
3017 /* Note that in the macro case, these arguments are already
3018 in MSB form. (When handling the instruction in the
3019 non-macro case, these arguments are sizes from which
3020 MSB values must be calculated.) */
bf12938e 3021 INSERT_OPERAND (INSMSB, insn, va_arg (args, int));
5f74bc13
CD
3022 continue;
3023
3024 case 'C':
3025 case 'G':
3026 case 'H':
3027 /* Note that in the macro case, these arguments are already
3028 in MSBD form. (When handling the instruction in the
3029 non-macro case, these arguments are sizes from which
3030 MSBD values must be calculated.) */
bf12938e 3031 INSERT_OPERAND (EXTMSBD, insn, va_arg (args, int));
5f74bc13
CD
3032 continue;
3033
3034 default:
3035 internalError ();
3036 }
3037 continue;
3038
252b5132
RH
3039 case 't':
3040 case 'w':
3041 case 'E':
bf12938e 3042 INSERT_OPERAND (RT, insn, va_arg (args, int));
252b5132
RH
3043 continue;
3044
3045 case 'c':
bf12938e 3046 INSERT_OPERAND (CODE, insn, va_arg (args, int));
38487616
TS
3047 continue;
3048
252b5132
RH
3049 case 'T':
3050 case 'W':
bf12938e 3051 INSERT_OPERAND (FT, insn, va_arg (args, int));
252b5132
RH
3052 continue;
3053
3054 case 'd':
3055 case 'G':
af7ee8bf 3056 case 'K':
bf12938e 3057 INSERT_OPERAND (RD, insn, va_arg (args, int));
252b5132
RH
3058 continue;
3059
4372b673
NC
3060 case 'U':
3061 {
3062 int tmp = va_arg (args, int);
3063
bf12938e
RS
3064 INSERT_OPERAND (RT, insn, tmp);
3065 INSERT_OPERAND (RD, insn, tmp);
beae10d5 3066 continue;
4372b673
NC
3067 }
3068
252b5132
RH
3069 case 'V':
3070 case 'S':
bf12938e 3071 INSERT_OPERAND (FS, insn, va_arg (args, int));
252b5132
RH
3072 continue;
3073
3074 case 'z':
3075 continue;
3076
3077 case '<':
bf12938e 3078 INSERT_OPERAND (SHAMT, insn, va_arg (args, int));
252b5132
RH
3079 continue;
3080
3081 case 'D':
bf12938e 3082 INSERT_OPERAND (FD, insn, va_arg (args, int));
252b5132
RH
3083 continue;
3084
3085 case 'B':
bf12938e 3086 INSERT_OPERAND (CODE20, insn, va_arg (args, int));
252b5132
RH
3087 continue;
3088
4372b673 3089 case 'J':
bf12938e 3090 INSERT_OPERAND (CODE19, insn, va_arg (args, int));
4372b673
NC
3091 continue;
3092
252b5132 3093 case 'q':
bf12938e 3094 INSERT_OPERAND (CODE2, insn, va_arg (args, int));
252b5132
RH
3095 continue;
3096
3097 case 'b':
3098 case 's':
3099 case 'r':
3100 case 'v':
bf12938e 3101 INSERT_OPERAND (RS, insn, va_arg (args, int));
252b5132
RH
3102 continue;
3103
3104 case 'i':
3105 case 'j':
3106 case 'o':
6e1304d8 3107 macro_read_relocs (&args, r);
cdf6fd85 3108 assert (*r == BFD_RELOC_GPREL16
f6688943
TS
3109 || *r == BFD_RELOC_MIPS_LITERAL
3110 || *r == BFD_RELOC_MIPS_HIGHER
3111 || *r == BFD_RELOC_HI16_S
3112 || *r == BFD_RELOC_LO16
3113 || *r == BFD_RELOC_MIPS_GOT16
3114 || *r == BFD_RELOC_MIPS_CALL16
438c16b8
TS
3115 || *r == BFD_RELOC_MIPS_GOT_DISP
3116 || *r == BFD_RELOC_MIPS_GOT_PAGE
3117 || *r == BFD_RELOC_MIPS_GOT_OFST
f6688943 3118 || *r == BFD_RELOC_MIPS_GOT_LO16
3e722fb5 3119 || *r == BFD_RELOC_MIPS_CALL_LO16);
252b5132
RH
3120 continue;
3121
3122 case 'u':
6e1304d8 3123 macro_read_relocs (&args, r);
252b5132
RH
3124 assert (ep != NULL
3125 && (ep->X_op == O_constant
3126 || (ep->X_op == O_symbol
f6688943
TS
3127 && (*r == BFD_RELOC_MIPS_HIGHEST
3128 || *r == BFD_RELOC_HI16_S
3129 || *r == BFD_RELOC_HI16
3130 || *r == BFD_RELOC_GPREL16
3131 || *r == BFD_RELOC_MIPS_GOT_HI16
3e722fb5 3132 || *r == BFD_RELOC_MIPS_CALL_HI16))));
252b5132
RH
3133 continue;
3134
3135 case 'p':
3136 assert (ep != NULL);
3137 /*
3138 * This allows macro() to pass an immediate expression for
3139 * creating short branches without creating a symbol.
0b25d3e6
AO
3140 * Note that the expression still might come from the assembly
3141 * input, in which case the value is not checked for range nor
3142 * is a relocation entry generated (yuck).
252b5132
RH
3143 */
3144 if (ep->X_op == O_constant)
3145 {
3146 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
3147 ep = NULL;
3148 }
3149 else
0b25d3e6 3150 *r = BFD_RELOC_16_PCREL_S2;
252b5132
RH
3151 continue;
3152
3153 case 'a':
3154 assert (ep != NULL);
f6688943 3155 *r = BFD_RELOC_MIPS_JMP;
252b5132
RH
3156 continue;
3157
3158 case 'C':
3159 insn.insn_opcode |= va_arg (args, unsigned long);
3160 continue;
3161
3162 default:
3163 internalError ();
3164 }
3165 break;
3166 }
3167 va_end (args);
f6688943 3168 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 3169
4d7206a2 3170 append_insn (&insn, ep, r);
252b5132
RH
3171}
3172
3173static void
67c0d1eb 3174mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
17a2f251 3175 va_list args)
252b5132 3176{
1e915849 3177 struct mips_opcode *mo;
252b5132 3178 struct mips_cl_insn insn;
f6688943
TS
3179 bfd_reloc_code_real_type r[3]
3180 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 3181
1e915849
RS
3182 mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
3183 assert (mo);
3184 assert (strcmp (name, mo->name) == 0);
252b5132 3185
1e915849 3186 while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO)
252b5132 3187 {
1e915849
RS
3188 ++mo;
3189 assert (mo->name);
3190 assert (strcmp (name, mo->name) == 0);
252b5132
RH
3191 }
3192
1e915849 3193 create_insn (&insn, mo);
252b5132
RH
3194 for (;;)
3195 {
3196 int c;
3197
3198 c = *fmt++;
3199 switch (c)
3200 {
3201 case '\0':
3202 break;
3203
3204 case ',':
3205 case '(':
3206 case ')':
3207 continue;
3208
3209 case 'y':
3210 case 'w':
bf12938e 3211 MIPS16_INSERT_OPERAND (RY, insn, va_arg (args, int));
252b5132
RH
3212 continue;
3213
3214 case 'x':
3215 case 'v':
bf12938e 3216 MIPS16_INSERT_OPERAND (RX, insn, va_arg (args, int));
252b5132
RH
3217 continue;
3218
3219 case 'z':
bf12938e 3220 MIPS16_INSERT_OPERAND (RZ, insn, va_arg (args, int));
252b5132
RH
3221 continue;
3222
3223 case 'Z':
bf12938e 3224 MIPS16_INSERT_OPERAND (MOVE32Z, insn, va_arg (args, int));
252b5132
RH
3225 continue;
3226
3227 case '0':
3228 case 'S':
3229 case 'P':
3230 case 'R':
3231 continue;
3232
3233 case 'X':
bf12938e 3234 MIPS16_INSERT_OPERAND (REGR32, insn, va_arg (args, int));
252b5132
RH
3235 continue;
3236
3237 case 'Y':
3238 {
3239 int regno;
3240
3241 regno = va_arg (args, int);
3242 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
3243 insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
3244 }
3245 continue;
3246
3247 case '<':
3248 case '>':
3249 case '4':
3250 case '5':
3251 case 'H':
3252 case 'W':
3253 case 'D':
3254 case 'j':
3255 case '8':
3256 case 'V':
3257 case 'C':
3258 case 'U':
3259 case 'k':
3260 case 'K':
3261 case 'p':
3262 case 'q':
3263 {
3264 assert (ep != NULL);
3265
3266 if (ep->X_op != O_constant)
874e8986 3267 *r = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
3268 else
3269 {
b34976b6
AM
3270 mips16_immed (NULL, 0, c, ep->X_add_number, FALSE, FALSE,
3271 FALSE, &insn.insn_opcode, &insn.use_extend,
c4e7957c 3272 &insn.extend);
252b5132 3273 ep = NULL;
f6688943 3274 *r = BFD_RELOC_UNUSED;
252b5132
RH
3275 }
3276 }
3277 continue;
3278
3279 case '6':
bf12938e 3280 MIPS16_INSERT_OPERAND (IMM6, insn, va_arg (args, int));
252b5132
RH
3281 continue;
3282 }
3283
3284 break;
3285 }
3286
f6688943 3287 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 3288
4d7206a2 3289 append_insn (&insn, ep, r);
252b5132
RH
3290}
3291
2051e8c4
MR
3292/*
3293 * Sign-extend 32-bit mode constants that have bit 31 set and all
3294 * higher bits unset.
3295 */
9f872bbe 3296static void
2051e8c4
MR
3297normalize_constant_expr (expressionS *ex)
3298{
9ee2a2d4 3299 if (ex->X_op == O_constant
2051e8c4
MR
3300 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
3301 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
3302 - 0x80000000);
3303}
3304
3305/*
3306 * Sign-extend 32-bit mode address offsets that have bit 31 set and
3307 * all higher bits unset.
3308 */
3309static void
3310normalize_address_expr (expressionS *ex)
3311{
3312 if (((ex->X_op == O_constant && HAVE_32BIT_ADDRESSES)
3313 || (ex->X_op == O_symbol && HAVE_32BIT_SYMBOLS))
3314 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
3315 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
3316 - 0x80000000);
3317}
3318
438c16b8
TS
3319/*
3320 * Generate a "jalr" instruction with a relocation hint to the called
3321 * function. This occurs in NewABI PIC code.
3322 */
3323static void
67c0d1eb 3324macro_build_jalr (expressionS *ep)
438c16b8 3325{
685736be 3326 char *f = NULL;
b34976b6 3327
438c16b8 3328 if (HAVE_NEWABI)
f21f8242 3329 {
cc3d92a5 3330 frag_grow (8);
f21f8242
AO
3331 f = frag_more (0);
3332 }
67c0d1eb 3333 macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
438c16b8 3334 if (HAVE_NEWABI)
f21f8242 3335 fix_new_exp (frag_now, f - frag_now->fr_literal,
a105a300 3336 4, ep, FALSE, BFD_RELOC_MIPS_JALR);
438c16b8
TS
3337}
3338
252b5132
RH
3339/*
3340 * Generate a "lui" instruction.
3341 */
3342static void
67c0d1eb 3343macro_build_lui (expressionS *ep, int regnum)
252b5132
RH
3344{
3345 expressionS high_expr;
1e915849 3346 const struct mips_opcode *mo;
252b5132 3347 struct mips_cl_insn insn;
f6688943
TS
3348 bfd_reloc_code_real_type r[3]
3349 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
5a38dc70
AM
3350 const char *name = "lui";
3351 const char *fmt = "t,u";
252b5132
RH
3352
3353 assert (! mips_opts.mips16);
3354
4d7206a2 3355 high_expr = *ep;
252b5132
RH
3356
3357 if (high_expr.X_op == O_constant)
3358 {
3359 /* we can compute the instruction now without a relocation entry */
e7d556df
TS
3360 high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3361 >> 16) & 0xffff;
f6688943 3362 *r = BFD_RELOC_UNUSED;
252b5132 3363 }
78e1bb40 3364 else
252b5132
RH
3365 {
3366 assert (ep->X_op == O_symbol);
bbe506e8
TS
3367 /* _gp_disp is a special case, used from s_cpload.
3368 __gnu_local_gp is used if mips_no_shared. */
252b5132 3369 assert (mips_pic == NO_PIC
78e1bb40 3370 || (! HAVE_NEWABI
aa6975fb
ILT
3371 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
3372 || (! mips_in_shared
bbe506e8
TS
3373 && strcmp (S_GET_NAME (ep->X_add_symbol),
3374 "__gnu_local_gp") == 0));
f6688943 3375 *r = BFD_RELOC_HI16_S;
252b5132
RH
3376 }
3377
1e915849
RS
3378 mo = hash_find (op_hash, name);
3379 assert (strcmp (name, mo->name) == 0);
3380 assert (strcmp (fmt, mo->args) == 0);
3381 create_insn (&insn, mo);
252b5132 3382
bf12938e
RS
3383 insn.insn_opcode = insn.insn_mo->match;
3384 INSERT_OPERAND (RT, insn, regnum);
f6688943 3385 if (*r == BFD_RELOC_UNUSED)
252b5132
RH
3386 {
3387 insn.insn_opcode |= high_expr.X_add_number;
4d7206a2 3388 append_insn (&insn, NULL, r);
252b5132
RH
3389 }
3390 else
4d7206a2 3391 append_insn (&insn, &high_expr, r);
252b5132
RH
3392}
3393
885add95
CD
3394/* Generate a sequence of instructions to do a load or store from a constant
3395 offset off of a base register (breg) into/from a target register (treg),
3396 using AT if necessary. */
3397static void
67c0d1eb
RS
3398macro_build_ldst_constoffset (expressionS *ep, const char *op,
3399 int treg, int breg, int dbl)
885add95
CD
3400{
3401 assert (ep->X_op == O_constant);
3402
256ab948 3403 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
3404 if (!dbl)
3405 normalize_constant_expr (ep);
256ab948 3406
67c1ffbe 3407 /* Right now, this routine can only handle signed 32-bit constants. */
ecd13cd3 3408 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
885add95
CD
3409 as_warn (_("operand overflow"));
3410
3411 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
3412 {
3413 /* Signed 16-bit offset will fit in the op. Easy! */
67c0d1eb 3414 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
885add95
CD
3415 }
3416 else
3417 {
3418 /* 32-bit offset, need multiple instructions and AT, like:
3419 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
3420 addu $tempreg,$tempreg,$breg
3421 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
3422 to handle the complete offset. */
67c0d1eb
RS
3423 macro_build_lui (ep, AT);
3424 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
3425 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
885add95
CD
3426
3427 if (mips_opts.noat)
8fc2e39e 3428 as_bad (_("Macro used $at after \".set noat\""));
885add95
CD
3429 }
3430}
3431
252b5132
RH
3432/* set_at()
3433 * Generates code to set the $at register to true (one)
3434 * if reg is less than the immediate expression.
3435 */
3436static void
67c0d1eb 3437set_at (int reg, int unsignedp)
252b5132
RH
3438{
3439 if (imm_expr.X_op == O_constant
3440 && imm_expr.X_add_number >= -0x8000
3441 && imm_expr.X_add_number < 0x8000)
67c0d1eb
RS
3442 macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
3443 AT, reg, BFD_RELOC_LO16);
252b5132
RH
3444 else
3445 {
67c0d1eb
RS
3446 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
3447 macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
252b5132
RH
3448 }
3449}
3450
3451/* Warn if an expression is not a constant. */
3452
3453static void
17a2f251 3454check_absolute_expr (struct mips_cl_insn *ip, expressionS *ex)
252b5132
RH
3455{
3456 if (ex->X_op == O_big)
3457 as_bad (_("unsupported large constant"));
3458 else if (ex->X_op != O_constant)
9ee2a2d4
MR
3459 as_bad (_("Instruction %s requires absolute expression"),
3460 ip->insn_mo->name);
13757d0c 3461
9ee2a2d4
MR
3462 if (HAVE_32BIT_GPRS)
3463 normalize_constant_expr (ex);
252b5132
RH
3464}
3465
3466/* Count the leading zeroes by performing a binary chop. This is a
3467 bulky bit of source, but performance is a LOT better for the
3468 majority of values than a simple loop to count the bits:
3469 for (lcnt = 0; (lcnt < 32); lcnt++)
3470 if ((v) & (1 << (31 - lcnt)))
3471 break;
3472 However it is not code size friendly, and the gain will drop a bit
3473 on certain cached systems.
3474*/
3475#define COUNT_TOP_ZEROES(v) \
3476 (((v) & ~0xffff) == 0 \
3477 ? ((v) & ~0xff) == 0 \
3478 ? ((v) & ~0xf) == 0 \
3479 ? ((v) & ~0x3) == 0 \
3480 ? ((v) & ~0x1) == 0 \
3481 ? !(v) \
3482 ? 32 \
3483 : 31 \
3484 : 30 \
3485 : ((v) & ~0x7) == 0 \
3486 ? 29 \
3487 : 28 \
3488 : ((v) & ~0x3f) == 0 \
3489 ? ((v) & ~0x1f) == 0 \
3490 ? 27 \
3491 : 26 \
3492 : ((v) & ~0x7f) == 0 \
3493 ? 25 \
3494 : 24 \
3495 : ((v) & ~0xfff) == 0 \
3496 ? ((v) & ~0x3ff) == 0 \
3497 ? ((v) & ~0x1ff) == 0 \
3498 ? 23 \
3499 : 22 \
3500 : ((v) & ~0x7ff) == 0 \
3501 ? 21 \
3502 : 20 \
3503 : ((v) & ~0x3fff) == 0 \
3504 ? ((v) & ~0x1fff) == 0 \
3505 ? 19 \
3506 : 18 \
3507 : ((v) & ~0x7fff) == 0 \
3508 ? 17 \
3509 : 16 \
3510 : ((v) & ~0xffffff) == 0 \
3511 ? ((v) & ~0xfffff) == 0 \
3512 ? ((v) & ~0x3ffff) == 0 \
3513 ? ((v) & ~0x1ffff) == 0 \
3514 ? 15 \
3515 : 14 \
3516 : ((v) & ~0x7ffff) == 0 \
3517 ? 13 \
3518 : 12 \
3519 : ((v) & ~0x3fffff) == 0 \
3520 ? ((v) & ~0x1fffff) == 0 \
3521 ? 11 \
3522 : 10 \
3523 : ((v) & ~0x7fffff) == 0 \
3524 ? 9 \
3525 : 8 \
3526 : ((v) & ~0xfffffff) == 0 \
3527 ? ((v) & ~0x3ffffff) == 0 \
3528 ? ((v) & ~0x1ffffff) == 0 \
3529 ? 7 \
3530 : 6 \
3531 : ((v) & ~0x7ffffff) == 0 \
3532 ? 5 \
3533 : 4 \
3534 : ((v) & ~0x3fffffff) == 0 \
3535 ? ((v) & ~0x1fffffff) == 0 \
3536 ? 3 \
3537 : 2 \
3538 : ((v) & ~0x7fffffff) == 0 \
3539 ? 1 \
3540 : 0)
3541
3542/* load_register()
67c1ffbe 3543 * This routine generates the least number of instructions necessary to load
252b5132
RH
3544 * an absolute expression value into a register.
3545 */
3546static void
67c0d1eb 3547load_register (int reg, expressionS *ep, int dbl)
252b5132
RH
3548{
3549 int freg;
3550 expressionS hi32, lo32;
3551
3552 if (ep->X_op != O_big)
3553 {
3554 assert (ep->X_op == O_constant);
256ab948
TS
3555
3556 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
3557 if (!dbl)
3558 normalize_constant_expr (ep);
256ab948
TS
3559
3560 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
252b5132
RH
3561 {
3562 /* We can handle 16 bit signed values with an addiu to
3563 $zero. No need to ever use daddiu here, since $zero and
3564 the result are always correct in 32 bit mode. */
67c0d1eb 3565 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
3566 return;
3567 }
3568 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3569 {
3570 /* We can handle 16 bit unsigned values with an ori to
3571 $zero. */
67c0d1eb 3572 macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
252b5132
RH
3573 return;
3574 }
256ab948 3575 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
252b5132
RH
3576 {
3577 /* 32 bit values require an lui. */
67c0d1eb 3578 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_HI16);
252b5132 3579 if ((ep->X_add_number & 0xffff) != 0)
67c0d1eb 3580 macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
252b5132
RH
3581 return;
3582 }
3583 }
3584
3585 /* The value is larger than 32 bits. */
3586
2051e8c4 3587 if (!dbl || HAVE_32BIT_GPRS)
252b5132 3588 {
55e08f71
NC
3589 char value[32];
3590
3591 sprintf_vma (value, ep->X_add_number);
20e1fcfd 3592 as_bad (_("Number (0x%s) larger than 32 bits"), value);
67c0d1eb 3593 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
3594 return;
3595 }
3596
3597 if (ep->X_op != O_big)
3598 {
3599 hi32 = *ep;
3600 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3601 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3602 hi32.X_add_number &= 0xffffffff;
3603 lo32 = *ep;
3604 lo32.X_add_number &= 0xffffffff;
3605 }
3606 else
3607 {
3608 assert (ep->X_add_number > 2);
3609 if (ep->X_add_number == 3)
3610 generic_bignum[3] = 0;
3611 else if (ep->X_add_number > 4)
3612 as_bad (_("Number larger than 64 bits"));
3613 lo32.X_op = O_constant;
3614 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3615 hi32.X_op = O_constant;
3616 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3617 }
3618
3619 if (hi32.X_add_number == 0)
3620 freg = 0;
3621 else
3622 {
3623 int shift, bit;
3624 unsigned long hi, lo;
3625
956cd1d6 3626 if (hi32.X_add_number == (offsetT) 0xffffffff)
beae10d5
KH
3627 {
3628 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3629 {
67c0d1eb 3630 macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
3631 return;
3632 }
3633 if (lo32.X_add_number & 0x80000000)
3634 {
67c0d1eb 3635 macro_build (&lo32, "lui", "t,u", reg, BFD_RELOC_HI16);
252b5132 3636 if (lo32.X_add_number & 0xffff)
67c0d1eb 3637 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
beae10d5
KH
3638 return;
3639 }
3640 }
252b5132
RH
3641
3642 /* Check for 16bit shifted constant. We know that hi32 is
3643 non-zero, so start the mask on the first bit of the hi32
3644 value. */
3645 shift = 17;
3646 do
beae10d5
KH
3647 {
3648 unsigned long himask, lomask;
3649
3650 if (shift < 32)
3651 {
3652 himask = 0xffff >> (32 - shift);
3653 lomask = (0xffff << shift) & 0xffffffff;
3654 }
3655 else
3656 {
3657 himask = 0xffff << (shift - 32);
3658 lomask = 0;
3659 }
3660 if ((hi32.X_add_number & ~(offsetT) himask) == 0
3661 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3662 {
3663 expressionS tmp;
3664
3665 tmp.X_op = O_constant;
3666 if (shift < 32)
3667 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3668 | (lo32.X_add_number >> shift));
3669 else
3670 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
67c0d1eb
RS
3671 macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
3672 macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", "d,w,<",
3673 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
3674 return;
3675 }
f9419b05 3676 ++shift;
beae10d5
KH
3677 }
3678 while (shift <= (64 - 16));
252b5132
RH
3679
3680 /* Find the bit number of the lowest one bit, and store the
3681 shifted value in hi/lo. */
3682 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3683 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3684 if (lo != 0)
3685 {
3686 bit = 0;
3687 while ((lo & 1) == 0)
3688 {
3689 lo >>= 1;
3690 ++bit;
3691 }
3692 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3693 hi >>= bit;
3694 }
3695 else
3696 {
3697 bit = 32;
3698 while ((hi & 1) == 0)
3699 {
3700 hi >>= 1;
3701 ++bit;
3702 }
3703 lo = hi;
3704 hi = 0;
3705 }
3706
3707 /* Optimize if the shifted value is a (power of 2) - 1. */
3708 if ((hi == 0 && ((lo + 1) & lo) == 0)
3709 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
beae10d5
KH
3710 {
3711 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
252b5132 3712 if (shift != 0)
beae10d5 3713 {
252b5132
RH
3714 expressionS tmp;
3715
3716 /* This instruction will set the register to be all
3717 ones. */
beae10d5
KH
3718 tmp.X_op = O_constant;
3719 tmp.X_add_number = (offsetT) -1;
67c0d1eb 3720 macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
3721 if (bit != 0)
3722 {
3723 bit += shift;
67c0d1eb
RS
3724 macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", "d,w,<",
3725 reg, reg, (bit >= 32) ? bit - 32 : bit);
beae10d5 3726 }
67c0d1eb
RS
3727 macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", "d,w,<",
3728 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
3729 return;
3730 }
3731 }
252b5132
RH
3732
3733 /* Sign extend hi32 before calling load_register, because we can
3734 generally get better code when we load a sign extended value. */
3735 if ((hi32.X_add_number & 0x80000000) != 0)
beae10d5 3736 hi32.X_add_number |= ~(offsetT) 0xffffffff;
67c0d1eb 3737 load_register (reg, &hi32, 0);
252b5132
RH
3738 freg = reg;
3739 }
3740 if ((lo32.X_add_number & 0xffff0000) == 0)
3741 {
3742 if (freg != 0)
3743 {
67c0d1eb 3744 macro_build (NULL, "dsll32", "d,w,<", reg, freg, 0);
252b5132
RH
3745 freg = reg;
3746 }
3747 }
3748 else
3749 {
3750 expressionS mid16;
3751
956cd1d6 3752 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
beae10d5 3753 {
67c0d1eb
RS
3754 macro_build (&lo32, "lui", "t,u", reg, BFD_RELOC_HI16);
3755 macro_build (NULL, "dsrl32", "d,w,<", reg, reg, 0);
beae10d5
KH
3756 return;
3757 }
252b5132
RH
3758
3759 if (freg != 0)
3760 {
67c0d1eb 3761 macro_build (NULL, "dsll", "d,w,<", reg, freg, 16);
252b5132
RH
3762 freg = reg;
3763 }
3764 mid16 = lo32;
3765 mid16.X_add_number >>= 16;
67c0d1eb
RS
3766 macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
3767 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
252b5132
RH
3768 freg = reg;
3769 }
3770 if ((lo32.X_add_number & 0xffff) != 0)
67c0d1eb 3771 macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
252b5132
RH
3772}
3773
269137b2
TS
3774static inline void
3775load_delay_nop (void)
3776{
3777 if (!gpr_interlocks)
3778 macro_build (NULL, "nop", "");
3779}
3780
252b5132
RH
3781/* Load an address into a register. */
3782
3783static void
67c0d1eb 3784load_address (int reg, expressionS *ep, int *used_at)
252b5132 3785{
252b5132
RH
3786 if (ep->X_op != O_constant
3787 && ep->X_op != O_symbol)
3788 {
3789 as_bad (_("expression too complex"));
3790 ep->X_op = O_constant;
3791 }
3792
3793 if (ep->X_op == O_constant)
3794 {
67c0d1eb 3795 load_register (reg, ep, HAVE_64BIT_ADDRESSES);
252b5132
RH
3796 return;
3797 }
3798
3799 if (mips_pic == NO_PIC)
3800 {
3801 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 3802 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
3803 Otherwise we want
3804 lui $reg,<sym> (BFD_RELOC_HI16_S)
3805 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
d6bc6245 3806 If we have an addend, we always use the latter form.
76b3015f 3807
d6bc6245
TS
3808 With 64bit address space and a usable $at we want
3809 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3810 lui $at,<sym> (BFD_RELOC_HI16_S)
3811 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3812 daddiu $at,<sym> (BFD_RELOC_LO16)
3813 dsll32 $reg,0
3a482fd5 3814 daddu $reg,$reg,$at
76b3015f 3815
c03099e6 3816 If $at is already in use, we use a path which is suboptimal
d6bc6245
TS
3817 on superscalar processors.
3818 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3819 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3820 dsll $reg,16
3821 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
3822 dsll $reg,16
3823 daddiu $reg,<sym> (BFD_RELOC_LO16)
6caf9ef4
TS
3824
3825 For GP relative symbols in 64bit address space we can use
3826 the same sequence as in 32bit address space. */
aed1a261 3827 if (HAVE_64BIT_SYMBOLS)
d6bc6245 3828 {
6caf9ef4
TS
3829 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3830 && !nopic_need_relax (ep->X_add_symbol, 1))
3831 {
3832 relax_start (ep->X_add_symbol);
3833 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
3834 mips_gp_register, BFD_RELOC_GPREL16);
3835 relax_switch ();
3836 }
d6bc6245 3837
b8285c27 3838 if (*used_at == 0 && !mips_opts.noat)
d6bc6245 3839 {
67c0d1eb
RS
3840 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_HIGHEST);
3841 macro_build (ep, "lui", "t,u", AT, BFD_RELOC_HI16_S);
3842 macro_build (ep, "daddiu", "t,r,j", reg, reg,
3843 BFD_RELOC_MIPS_HIGHER);
3844 macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
3845 macro_build (NULL, "dsll32", "d,w,<", reg, reg, 0);
3846 macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
d6bc6245
TS
3847 *used_at = 1;
3848 }
3849 else
3850 {
67c0d1eb
RS
3851 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_HIGHEST);
3852 macro_build (ep, "daddiu", "t,r,j", reg, reg,
3853 BFD_RELOC_MIPS_HIGHER);
3854 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
3855 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
3856 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
3857 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
d6bc6245 3858 }
6caf9ef4
TS
3859
3860 if (mips_relax.sequence)
3861 relax_end ();
d6bc6245 3862 }
252b5132
RH
3863 else
3864 {
d6bc6245 3865 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 3866 && !nopic_need_relax (ep->X_add_symbol, 1))
d6bc6245 3867 {
4d7206a2 3868 relax_start (ep->X_add_symbol);
67c0d1eb 3869 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
17a2f251 3870 mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 3871 relax_switch ();
d6bc6245 3872 }
67c0d1eb
RS
3873 macro_build_lui (ep, reg);
3874 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
3875 reg, reg, BFD_RELOC_LO16);
4d7206a2
RS
3876 if (mips_relax.sequence)
3877 relax_end ();
d6bc6245 3878 }
252b5132
RH
3879 }
3880 else if (mips_pic == SVR4_PIC && ! mips_big_got)
3881 {
3882 expressionS ex;
3883
3884 /* If this is a reference to an external symbol, we want
3885 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3886 Otherwise we want
3887 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3888 nop
3889 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
f5040a92
AO
3890 If there is a constant, it must be added in after.
3891
ed6fb7bd 3892 If we have NewABI, we want
f5040a92
AO
3893 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
3894 unless we're referencing a global symbol with a non-zero
3895 offset, in which case cst must be added separately. */
ed6fb7bd
SC
3896 if (HAVE_NEWABI)
3897 {
f5040a92
AO
3898 if (ep->X_add_number)
3899 {
4d7206a2 3900 ex.X_add_number = ep->X_add_number;
f5040a92 3901 ep->X_add_number = 0;
4d7206a2 3902 relax_start (ep->X_add_symbol);
67c0d1eb
RS
3903 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3904 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
3905 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3906 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3907 ex.X_op = O_constant;
67c0d1eb 3908 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 3909 reg, reg, BFD_RELOC_LO16);
f5040a92 3910 ep->X_add_number = ex.X_add_number;
4d7206a2 3911 relax_switch ();
f5040a92 3912 }
67c0d1eb 3913 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 3914 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2
RS
3915 if (mips_relax.sequence)
3916 relax_end ();
ed6fb7bd
SC
3917 }
3918 else
3919 {
f5040a92
AO
3920 ex.X_add_number = ep->X_add_number;
3921 ep->X_add_number = 0;
67c0d1eb
RS
3922 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3923 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 3924 load_delay_nop ();
4d7206a2
RS
3925 relax_start (ep->X_add_symbol);
3926 relax_switch ();
67c0d1eb 3927 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 3928 BFD_RELOC_LO16);
4d7206a2 3929 relax_end ();
ed6fb7bd 3930
f5040a92
AO
3931 if (ex.X_add_number != 0)
3932 {
3933 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3934 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3935 ex.X_op = O_constant;
67c0d1eb 3936 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 3937 reg, reg, BFD_RELOC_LO16);
f5040a92 3938 }
252b5132
RH
3939 }
3940 }
3941 else if (mips_pic == SVR4_PIC)
3942 {
3943 expressionS ex;
252b5132
RH
3944
3945 /* This is the large GOT case. If this is a reference to an
3946 external symbol, we want
3947 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
3948 addu $reg,$reg,$gp
3949 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
f5040a92
AO
3950
3951 Otherwise, for a reference to a local symbol in old ABI, we want
252b5132
RH
3952 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3953 nop
3954 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
684022ea 3955 If there is a constant, it must be added in after.
f5040a92
AO
3956
3957 In the NewABI, for local symbols, with or without offsets, we want:
438c16b8
TS
3958 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
3959 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
f5040a92 3960 */
438c16b8
TS
3961 if (HAVE_NEWABI)
3962 {
4d7206a2 3963 ex.X_add_number = ep->X_add_number;
f5040a92 3964 ep->X_add_number = 0;
4d7206a2 3965 relax_start (ep->X_add_symbol);
67c0d1eb
RS
3966 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_GOT_HI16);
3967 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
3968 reg, reg, mips_gp_register);
3969 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
3970 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
f5040a92
AO
3971 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3972 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3973 else if (ex.X_add_number)
3974 {
3975 ex.X_op = O_constant;
67c0d1eb
RS
3976 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
3977 BFD_RELOC_LO16);
f5040a92
AO
3978 }
3979
3980 ep->X_add_number = ex.X_add_number;
4d7206a2 3981 relax_switch ();
67c0d1eb 3982 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 3983 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
67c0d1eb
RS
3984 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
3985 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 3986 relax_end ();
438c16b8 3987 }
252b5132 3988 else
438c16b8 3989 {
f5040a92
AO
3990 ex.X_add_number = ep->X_add_number;
3991 ep->X_add_number = 0;
4d7206a2 3992 relax_start (ep->X_add_symbol);
67c0d1eb
RS
3993 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_GOT_HI16);
3994 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
3995 reg, reg, mips_gp_register);
3996 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
3997 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
4d7206a2
RS
3998 relax_switch ();
3999 if (reg_needs_delay (mips_gp_register))
438c16b8
TS
4000 {
4001 /* We need a nop before loading from $gp. This special
4002 check is required because the lui which starts the main
4003 instruction stream does not refer to $gp, and so will not
4004 insert the nop which may be required. */
67c0d1eb 4005 macro_build (NULL, "nop", "");
438c16b8 4006 }
67c0d1eb 4007 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 4008 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 4009 load_delay_nop ();
67c0d1eb 4010 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 4011 BFD_RELOC_LO16);
4d7206a2 4012 relax_end ();
438c16b8 4013
f5040a92
AO
4014 if (ex.X_add_number != 0)
4015 {
4016 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4017 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4018 ex.X_op = O_constant;
67c0d1eb
RS
4019 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
4020 BFD_RELOC_LO16);
f5040a92 4021 }
252b5132
RH
4022 }
4023 }
252b5132
RH
4024 else
4025 abort ();
8fc2e39e
TS
4026
4027 if (mips_opts.noat && *used_at == 1)
4028 as_bad (_("Macro used $at after \".set noat\""));
252b5132
RH
4029}
4030
ea1fb5dc
RS
4031/* Move the contents of register SOURCE into register DEST. */
4032
4033static void
67c0d1eb 4034move_register (int dest, int source)
ea1fb5dc 4035{
67c0d1eb
RS
4036 macro_build (NULL, HAVE_32BIT_GPRS ? "addu" : "daddu", "d,v,t",
4037 dest, source, 0);
ea1fb5dc
RS
4038}
4039
4d7206a2 4040/* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
f6a22291
MR
4041 LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
4042 The two alternatives are:
4d7206a2
RS
4043
4044 Global symbol Local sybmol
4045 ------------- ------------
4046 lw DEST,%got(SYMBOL) lw DEST,%got(SYMBOL + OFFSET)
4047 ... ...
4048 addiu DEST,DEST,OFFSET addiu DEST,DEST,%lo(SYMBOL + OFFSET)
4049
4050 load_got_offset emits the first instruction and add_got_offset
f6a22291
MR
4051 emits the second for a 16-bit offset or add_got_offset_hilo emits
4052 a sequence to add a 32-bit offset using a scratch register. */
4d7206a2
RS
4053
4054static void
67c0d1eb 4055load_got_offset (int dest, expressionS *local)
4d7206a2
RS
4056{
4057 expressionS global;
4058
4059 global = *local;
4060 global.X_add_number = 0;
4061
4062 relax_start (local->X_add_symbol);
67c0d1eb
RS
4063 macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
4064 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2 4065 relax_switch ();
67c0d1eb
RS
4066 macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
4067 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2
RS
4068 relax_end ();
4069}
4070
4071static void
67c0d1eb 4072add_got_offset (int dest, expressionS *local)
4d7206a2
RS
4073{
4074 expressionS global;
4075
4076 global.X_op = O_constant;
4077 global.X_op_symbol = NULL;
4078 global.X_add_symbol = NULL;
4079 global.X_add_number = local->X_add_number;
4080
4081 relax_start (local->X_add_symbol);
67c0d1eb 4082 macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
4d7206a2
RS
4083 dest, dest, BFD_RELOC_LO16);
4084 relax_switch ();
67c0d1eb 4085 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
4d7206a2
RS
4086 relax_end ();
4087}
4088
f6a22291
MR
4089static void
4090add_got_offset_hilo (int dest, expressionS *local, int tmp)
4091{
4092 expressionS global;
4093 int hold_mips_optimize;
4094
4095 global.X_op = O_constant;
4096 global.X_op_symbol = NULL;
4097 global.X_add_symbol = NULL;
4098 global.X_add_number = local->X_add_number;
4099
4100 relax_start (local->X_add_symbol);
4101 load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
4102 relax_switch ();
4103 /* Set mips_optimize around the lui instruction to avoid
4104 inserting an unnecessary nop after the lw. */
4105 hold_mips_optimize = mips_optimize;
4106 mips_optimize = 2;
4107 macro_build_lui (&global, tmp);
4108 mips_optimize = hold_mips_optimize;
4109 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
4110 relax_end ();
4111
4112 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
4113}
4114
252b5132
RH
4115/*
4116 * Build macros
4117 * This routine implements the seemingly endless macro or synthesized
4118 * instructions and addressing modes in the mips assembly language. Many
4119 * of these macros are simple and are similar to each other. These could
67c1ffbe 4120 * probably be handled by some kind of table or grammar approach instead of
252b5132
RH
4121 * this verbose method. Others are not simple macros but are more like
4122 * optimizing code generation.
4123 * One interesting optimization is when several store macros appear
67c1ffbe 4124 * consecutively that would load AT with the upper half of the same address.
252b5132
RH
4125 * The ensuing load upper instructions are ommited. This implies some kind
4126 * of global optimization. We currently only optimize within a single macro.
4127 * For many of the load and store macros if the address is specified as a
4128 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
4129 * first load register 'at' with zero and use it as the base register. The
4130 * mips assembler simply uses register $zero. Just one tiny optimization
4131 * we're missing.
4132 */
4133static void
17a2f251 4134macro (struct mips_cl_insn *ip)
252b5132
RH
4135{
4136 register int treg, sreg, dreg, breg;
4137 int tempreg;
4138 int mask;
43841e91 4139 int used_at = 0;
252b5132
RH
4140 expressionS expr1;
4141 const char *s;
4142 const char *s2;
4143 const char *fmt;
4144 int likely = 0;
4145 int dbl = 0;
4146 int coproc = 0;
4147 int lr = 0;
4148 int imm = 0;
1abe91b1 4149 int call = 0;
252b5132 4150 int off;
67c0d1eb 4151 offsetT maxnum;
252b5132 4152 bfd_reloc_code_real_type r;
252b5132
RH
4153 int hold_mips_optimize;
4154
4155 assert (! mips_opts.mips16);
4156
4157 treg = (ip->insn_opcode >> 16) & 0x1f;
4158 dreg = (ip->insn_opcode >> 11) & 0x1f;
4159 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
4160 mask = ip->insn_mo->mask;
4161
4162 expr1.X_op = O_constant;
4163 expr1.X_op_symbol = NULL;
4164 expr1.X_add_symbol = NULL;
4165 expr1.X_add_number = 1;
4166
4167 switch (mask)
4168 {
4169 case M_DABS:
4170 dbl = 1;
4171 case M_ABS:
4172 /* bgez $a0,.+12
4173 move v0,$a0
4174 sub v0,$zero,$a0
4175 */
4176
7d10b47d 4177 start_noreorder ();
252b5132
RH
4178
4179 expr1.X_add_number = 8;
67c0d1eb 4180 macro_build (&expr1, "bgez", "s,p", sreg);
252b5132 4181 if (dreg == sreg)
67c0d1eb 4182 macro_build (NULL, "nop", "", 0);
252b5132 4183 else
67c0d1eb
RS
4184 move_register (dreg, sreg);
4185 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
252b5132 4186
7d10b47d 4187 end_noreorder ();
8fc2e39e 4188 break;
252b5132
RH
4189
4190 case M_ADD_I:
4191 s = "addi";
4192 s2 = "add";
4193 goto do_addi;
4194 case M_ADDU_I:
4195 s = "addiu";
4196 s2 = "addu";
4197 goto do_addi;
4198 case M_DADD_I:
4199 dbl = 1;
4200 s = "daddi";
4201 s2 = "dadd";
4202 goto do_addi;
4203 case M_DADDU_I:
4204 dbl = 1;
4205 s = "daddiu";
4206 s2 = "daddu";
4207 do_addi:
4208 if (imm_expr.X_op == O_constant
4209 && imm_expr.X_add_number >= -0x8000
4210 && imm_expr.X_add_number < 0x8000)
4211 {
67c0d1eb 4212 macro_build (&imm_expr, s, "t,r,j", treg, sreg, BFD_RELOC_LO16);
8fc2e39e 4213 break;
252b5132 4214 }
8fc2e39e 4215 used_at = 1;
67c0d1eb
RS
4216 load_register (AT, &imm_expr, dbl);
4217 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
252b5132
RH
4218 break;
4219
4220 case M_AND_I:
4221 s = "andi";
4222 s2 = "and";
4223 goto do_bit;
4224 case M_OR_I:
4225 s = "ori";
4226 s2 = "or";
4227 goto do_bit;
4228 case M_NOR_I:
4229 s = "";
4230 s2 = "nor";
4231 goto do_bit;
4232 case M_XOR_I:
4233 s = "xori";
4234 s2 = "xor";
4235 do_bit:
4236 if (imm_expr.X_op == O_constant
4237 && imm_expr.X_add_number >= 0
4238 && imm_expr.X_add_number < 0x10000)
4239 {
4240 if (mask != M_NOR_I)
67c0d1eb 4241 macro_build (&imm_expr, s, "t,r,i", treg, sreg, BFD_RELOC_LO16);
252b5132
RH
4242 else
4243 {
67c0d1eb
RS
4244 macro_build (&imm_expr, "ori", "t,r,i",
4245 treg, sreg, BFD_RELOC_LO16);
4246 macro_build (NULL, "nor", "d,v,t", treg, treg, 0);
252b5132 4247 }
8fc2e39e 4248 break;
252b5132
RH
4249 }
4250
8fc2e39e 4251 used_at = 1;
67c0d1eb
RS
4252 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
4253 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
252b5132
RH
4254 break;
4255
4256 case M_BEQ_I:
4257 s = "beq";
4258 goto beq_i;
4259 case M_BEQL_I:
4260 s = "beql";
4261 likely = 1;
4262 goto beq_i;
4263 case M_BNE_I:
4264 s = "bne";
4265 goto beq_i;
4266 case M_BNEL_I:
4267 s = "bnel";
4268 likely = 1;
4269 beq_i:
4270 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4271 {
67c0d1eb 4272 macro_build (&offset_expr, s, "s,t,p", sreg, 0);
8fc2e39e 4273 break;
252b5132 4274 }
8fc2e39e 4275 used_at = 1;
67c0d1eb
RS
4276 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
4277 macro_build (&offset_expr, s, "s,t,p", sreg, AT);
252b5132
RH
4278 break;
4279
4280 case M_BGEL:
4281 likely = 1;
4282 case M_BGE:
4283 if (treg == 0)
4284 {
67c0d1eb 4285 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", sreg);
8fc2e39e 4286 break;
252b5132
RH
4287 }
4288 if (sreg == 0)
4289 {
67c0d1eb 4290 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", treg);
8fc2e39e 4291 break;
252b5132 4292 }
8fc2e39e 4293 used_at = 1;
67c0d1eb
RS
4294 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
4295 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4296 break;
4297
4298 case M_BGTL_I:
4299 likely = 1;
4300 case M_BGT_I:
4301 /* check for > max integer */
4302 maxnum = 0x7fffffff;
ca4e0257 4303 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
4304 {
4305 maxnum <<= 16;
4306 maxnum |= 0xffff;
4307 maxnum <<= 16;
4308 maxnum |= 0xffff;
4309 }
4310 if (imm_expr.X_op == O_constant
4311 && imm_expr.X_add_number >= maxnum
ca4e0257 4312 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
4313 {
4314 do_false:
4315 /* result is always false */
4316 if (! likely)
67c0d1eb 4317 macro_build (NULL, "nop", "", 0);
252b5132 4318 else
67c0d1eb 4319 macro_build (&offset_expr, "bnel", "s,t,p", 0, 0);
8fc2e39e 4320 break;
252b5132
RH
4321 }
4322 if (imm_expr.X_op != O_constant)
4323 as_bad (_("Unsupported large constant"));
f9419b05 4324 ++imm_expr.X_add_number;
252b5132
RH
4325 /* FALLTHROUGH */
4326 case M_BGE_I:
4327 case M_BGEL_I:
4328 if (mask == M_BGEL_I)
4329 likely = 1;
4330 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4331 {
67c0d1eb 4332 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", sreg);
8fc2e39e 4333 break;
252b5132
RH
4334 }
4335 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4336 {
67c0d1eb 4337 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", sreg);
8fc2e39e 4338 break;
252b5132
RH
4339 }
4340 maxnum = 0x7fffffff;
ca4e0257 4341 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
4342 {
4343 maxnum <<= 16;
4344 maxnum |= 0xffff;
4345 maxnum <<= 16;
4346 maxnum |= 0xffff;
4347 }
4348 maxnum = - maxnum - 1;
4349 if (imm_expr.X_op == O_constant
4350 && imm_expr.X_add_number <= maxnum
ca4e0257 4351 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
4352 {
4353 do_true:
4354 /* result is always true */
4355 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
67c0d1eb 4356 macro_build (&offset_expr, "b", "p");
8fc2e39e 4357 break;
252b5132 4358 }
8fc2e39e 4359 used_at = 1;
67c0d1eb
RS
4360 set_at (sreg, 0);
4361 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4362 break;
4363
4364 case M_BGEUL:
4365 likely = 1;
4366 case M_BGEU:
4367 if (treg == 0)
4368 goto do_true;
4369 if (sreg == 0)
4370 {
67c0d1eb 4371 macro_build (&offset_expr, likely ? "beql" : "beq",
17a2f251 4372 "s,t,p", 0, treg);
8fc2e39e 4373 break;
252b5132 4374 }
8fc2e39e 4375 used_at = 1;
67c0d1eb
RS
4376 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
4377 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4378 break;
4379
4380 case M_BGTUL_I:
4381 likely = 1;
4382 case M_BGTU_I:
4383 if (sreg == 0
ca4e0257 4384 || (HAVE_32BIT_GPRS
252b5132 4385 && imm_expr.X_op == O_constant
956cd1d6 4386 && imm_expr.X_add_number == (offsetT) 0xffffffff))
252b5132
RH
4387 goto do_false;
4388 if (imm_expr.X_op != O_constant)
4389 as_bad (_("Unsupported large constant"));
f9419b05 4390 ++imm_expr.X_add_number;
252b5132
RH
4391 /* FALLTHROUGH */
4392 case M_BGEU_I:
4393 case M_BGEUL_I:
4394 if (mask == M_BGEUL_I)
4395 likely = 1;
4396 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4397 goto do_true;
4398 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4399 {
67c0d1eb 4400 macro_build (&offset_expr, likely ? "bnel" : "bne",
17a2f251 4401 "s,t,p", sreg, 0);
8fc2e39e 4402 break;
252b5132 4403 }
8fc2e39e 4404 used_at = 1;
67c0d1eb
RS
4405 set_at (sreg, 1);
4406 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4407 break;
4408
4409 case M_BGTL:
4410 likely = 1;
4411 case M_BGT:
4412 if (treg == 0)
4413 {
67c0d1eb 4414 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", sreg);
8fc2e39e 4415 break;
252b5132
RH
4416 }
4417 if (sreg == 0)
4418 {
67c0d1eb 4419 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", treg);
8fc2e39e 4420 break;
252b5132 4421 }
8fc2e39e 4422 used_at = 1;
67c0d1eb
RS
4423 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
4424 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4425 break;
4426
4427 case M_BGTUL:
4428 likely = 1;
4429 case M_BGTU:
4430 if (treg == 0)
4431 {
67c0d1eb 4432 macro_build (&offset_expr, likely ? "bnel" : "bne",
17a2f251 4433 "s,t,p", sreg, 0);
8fc2e39e 4434 break;
252b5132
RH
4435 }
4436 if (sreg == 0)
4437 goto do_false;
8fc2e39e 4438 used_at = 1;
67c0d1eb
RS
4439 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
4440 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4441 break;
4442
4443 case M_BLEL:
4444 likely = 1;
4445 case M_BLE:
4446 if (treg == 0)
4447 {
67c0d1eb 4448 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", sreg);
8fc2e39e 4449 break;
252b5132
RH
4450 }
4451 if (sreg == 0)
4452 {
67c0d1eb 4453 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", treg);
8fc2e39e 4454 break;
252b5132 4455 }
8fc2e39e 4456 used_at = 1;
67c0d1eb
RS
4457 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
4458 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4459 break;
4460
4461 case M_BLEL_I:
4462 likely = 1;
4463 case M_BLE_I:
4464 maxnum = 0x7fffffff;
ca4e0257 4465 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
4466 {
4467 maxnum <<= 16;
4468 maxnum |= 0xffff;
4469 maxnum <<= 16;
4470 maxnum |= 0xffff;
4471 }
4472 if (imm_expr.X_op == O_constant
4473 && imm_expr.X_add_number >= maxnum
ca4e0257 4474 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
4475 goto do_true;
4476 if (imm_expr.X_op != O_constant)
4477 as_bad (_("Unsupported large constant"));
f9419b05 4478 ++imm_expr.X_add_number;
252b5132
RH
4479 /* FALLTHROUGH */
4480 case M_BLT_I:
4481 case M_BLTL_I:
4482 if (mask == M_BLTL_I)
4483 likely = 1;
4484 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4485 {
67c0d1eb 4486 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", sreg);
8fc2e39e 4487 break;
252b5132
RH
4488 }
4489 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4490 {
67c0d1eb 4491 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", sreg);
8fc2e39e 4492 break;
252b5132 4493 }
8fc2e39e 4494 used_at = 1;
67c0d1eb
RS
4495 set_at (sreg, 0);
4496 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4497 break;
4498
4499 case M_BLEUL:
4500 likely = 1;
4501 case M_BLEU:
4502 if (treg == 0)
4503 {
67c0d1eb 4504 macro_build (&offset_expr, likely ? "beql" : "beq",
17a2f251 4505 "s,t,p", sreg, 0);
8fc2e39e 4506 break;
252b5132
RH
4507 }
4508 if (sreg == 0)
4509 goto do_true;
8fc2e39e 4510 used_at = 1;
67c0d1eb
RS
4511 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
4512 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4513 break;
4514
4515 case M_BLEUL_I:
4516 likely = 1;
4517 case M_BLEU_I:
4518 if (sreg == 0
ca4e0257 4519 || (HAVE_32BIT_GPRS
252b5132 4520 && imm_expr.X_op == O_constant
956cd1d6 4521 && imm_expr.X_add_number == (offsetT) 0xffffffff))
252b5132
RH
4522 goto do_true;
4523 if (imm_expr.X_op != O_constant)
4524 as_bad (_("Unsupported large constant"));
f9419b05 4525 ++imm_expr.X_add_number;
252b5132
RH
4526 /* FALLTHROUGH */
4527 case M_BLTU_I:
4528 case M_BLTUL_I:
4529 if (mask == M_BLTUL_I)
4530 likely = 1;
4531 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4532 goto do_false;
4533 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4534 {
67c0d1eb 4535 macro_build (&offset_expr, likely ? "beql" : "beq",
252b5132 4536 "s,t,p", sreg, 0);
8fc2e39e 4537 break;
252b5132 4538 }
8fc2e39e 4539 used_at = 1;
67c0d1eb
RS
4540 set_at (sreg, 1);
4541 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4542 break;
4543
4544 case M_BLTL:
4545 likely = 1;
4546 case M_BLT:
4547 if (treg == 0)
4548 {
67c0d1eb 4549 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", sreg);
8fc2e39e 4550 break;
252b5132
RH
4551 }
4552 if (sreg == 0)
4553 {
67c0d1eb 4554 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", treg);
8fc2e39e 4555 break;
252b5132 4556 }
8fc2e39e 4557 used_at = 1;
67c0d1eb
RS
4558 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
4559 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4560 break;
4561
4562 case M_BLTUL:
4563 likely = 1;
4564 case M_BLTU:
4565 if (treg == 0)
4566 goto do_false;
4567 if (sreg == 0)
4568 {
67c0d1eb 4569 macro_build (&offset_expr, likely ? "bnel" : "bne",
17a2f251 4570 "s,t,p", 0, treg);
8fc2e39e 4571 break;
252b5132 4572 }
8fc2e39e 4573 used_at = 1;
67c0d1eb
RS
4574 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
4575 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4576 break;
4577
5f74bc13
CD
4578 case M_DEXT:
4579 {
4580 unsigned long pos;
4581 unsigned long size;
4582
4583 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
4584 {
4585 as_bad (_("Unsupported large constant"));
4586 pos = size = 1;
4587 }
4588 else
4589 {
4590 pos = (unsigned long) imm_expr.X_add_number;
4591 size = (unsigned long) imm2_expr.X_add_number;
4592 }
4593
4594 if (pos > 63)
4595 {
4596 as_bad (_("Improper position (%lu)"), pos);
4597 pos = 1;
4598 }
4599 if (size == 0 || size > 64
4600 || (pos + size - 1) > 63)
4601 {
4602 as_bad (_("Improper extract size (%lu, position %lu)"),
4603 size, pos);
4604 size = 1;
4605 }
4606
4607 if (size <= 32 && pos < 32)
4608 {
4609 s = "dext";
4610 fmt = "t,r,+A,+C";
4611 }
4612 else if (size <= 32)
4613 {
4614 s = "dextu";
4615 fmt = "t,r,+E,+H";
4616 }
4617 else
4618 {
4619 s = "dextm";
4620 fmt = "t,r,+A,+G";
4621 }
67c0d1eb 4622 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, pos, size - 1);
5f74bc13 4623 }
8fc2e39e 4624 break;
5f74bc13
CD
4625
4626 case M_DINS:
4627 {
4628 unsigned long pos;
4629 unsigned long size;
4630
4631 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
4632 {
4633 as_bad (_("Unsupported large constant"));
4634 pos = size = 1;
4635 }
4636 else
4637 {
4638 pos = (unsigned long) imm_expr.X_add_number;
4639 size = (unsigned long) imm2_expr.X_add_number;
4640 }
4641
4642 if (pos > 63)
4643 {
4644 as_bad (_("Improper position (%lu)"), pos);
4645 pos = 1;
4646 }
4647 if (size == 0 || size > 64
4648 || (pos + size - 1) > 63)
4649 {
4650 as_bad (_("Improper insert size (%lu, position %lu)"),
4651 size, pos);
4652 size = 1;
4653 }
4654
4655 if (pos < 32 && (pos + size - 1) < 32)
4656 {
4657 s = "dins";
4658 fmt = "t,r,+A,+B";
4659 }
4660 else if (pos >= 32)
4661 {
4662 s = "dinsu";
4663 fmt = "t,r,+E,+F";
4664 }
4665 else
4666 {
4667 s = "dinsm";
4668 fmt = "t,r,+A,+F";
4669 }
67c0d1eb
RS
4670 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, pos,
4671 pos + size - 1);
5f74bc13 4672 }
8fc2e39e 4673 break;
5f74bc13 4674
252b5132
RH
4675 case M_DDIV_3:
4676 dbl = 1;
4677 case M_DIV_3:
4678 s = "mflo";
4679 goto do_div3;
4680 case M_DREM_3:
4681 dbl = 1;
4682 case M_REM_3:
4683 s = "mfhi";
4684 do_div3:
4685 if (treg == 0)
4686 {
4687 as_warn (_("Divide by zero."));
4688 if (mips_trap)
67c0d1eb 4689 macro_build (NULL, "teq", "s,t,q", 0, 0, 7);
252b5132 4690 else
67c0d1eb 4691 macro_build (NULL, "break", "c", 7);
8fc2e39e 4692 break;
252b5132
RH
4693 }
4694
7d10b47d 4695 start_noreorder ();
252b5132
RH
4696 if (mips_trap)
4697 {
67c0d1eb
RS
4698 macro_build (NULL, "teq", "s,t,q", treg, 0, 7);
4699 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
252b5132
RH
4700 }
4701 else
4702 {
4703 expr1.X_add_number = 8;
67c0d1eb
RS
4704 macro_build (&expr1, "bne", "s,t,p", treg, 0);
4705 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4706 macro_build (NULL, "break", "c", 7);
252b5132
RH
4707 }
4708 expr1.X_add_number = -1;
8fc2e39e 4709 used_at = 1;
f6a22291 4710 load_register (AT, &expr1, dbl);
252b5132 4711 expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
67c0d1eb 4712 macro_build (&expr1, "bne", "s,t,p", treg, AT);
252b5132
RH
4713 if (dbl)
4714 {
4715 expr1.X_add_number = 1;
f6a22291 4716 load_register (AT, &expr1, dbl);
67c0d1eb 4717 macro_build (NULL, "dsll32", "d,w,<", AT, AT, 31);
252b5132
RH
4718 }
4719 else
4720 {
4721 expr1.X_add_number = 0x80000000;
67c0d1eb 4722 macro_build (&expr1, "lui", "t,u", AT, BFD_RELOC_HI16);
252b5132
RH
4723 }
4724 if (mips_trap)
4725 {
67c0d1eb 4726 macro_build (NULL, "teq", "s,t,q", sreg, AT, 6);
252b5132
RH
4727 /* We want to close the noreorder block as soon as possible, so
4728 that later insns are available for delay slot filling. */
7d10b47d 4729 end_noreorder ();
252b5132
RH
4730 }
4731 else
4732 {
4733 expr1.X_add_number = 8;
67c0d1eb
RS
4734 macro_build (&expr1, "bne", "s,t,p", sreg, AT);
4735 macro_build (NULL, "nop", "", 0);
252b5132
RH
4736
4737 /* We want to close the noreorder block as soon as possible, so
4738 that later insns are available for delay slot filling. */
7d10b47d 4739 end_noreorder ();
252b5132 4740
67c0d1eb 4741 macro_build (NULL, "break", "c", 6);
252b5132 4742 }
67c0d1eb 4743 macro_build (NULL, s, "d", dreg);
252b5132
RH
4744 break;
4745
4746 case M_DIV_3I:
4747 s = "div";
4748 s2 = "mflo";
4749 goto do_divi;
4750 case M_DIVU_3I:
4751 s = "divu";
4752 s2 = "mflo";
4753 goto do_divi;
4754 case M_REM_3I:
4755 s = "div";
4756 s2 = "mfhi";
4757 goto do_divi;
4758 case M_REMU_3I:
4759 s = "divu";
4760 s2 = "mfhi";
4761 goto do_divi;
4762 case M_DDIV_3I:
4763 dbl = 1;
4764 s = "ddiv";
4765 s2 = "mflo";
4766 goto do_divi;
4767 case M_DDIVU_3I:
4768 dbl = 1;
4769 s = "ddivu";
4770 s2 = "mflo";
4771 goto do_divi;
4772 case M_DREM_3I:
4773 dbl = 1;
4774 s = "ddiv";
4775 s2 = "mfhi";
4776 goto do_divi;
4777 case M_DREMU_3I:
4778 dbl = 1;
4779 s = "ddivu";
4780 s2 = "mfhi";
4781 do_divi:
4782 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4783 {
4784 as_warn (_("Divide by zero."));
4785 if (mips_trap)
67c0d1eb 4786 macro_build (NULL, "teq", "s,t,q", 0, 0, 7);
252b5132 4787 else
67c0d1eb 4788 macro_build (NULL, "break", "c", 7);
8fc2e39e 4789 break;
252b5132
RH
4790 }
4791 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4792 {
4793 if (strcmp (s2, "mflo") == 0)
67c0d1eb 4794 move_register (dreg, sreg);
252b5132 4795 else
67c0d1eb 4796 move_register (dreg, 0);
8fc2e39e 4797 break;
252b5132
RH
4798 }
4799 if (imm_expr.X_op == O_constant
4800 && imm_expr.X_add_number == -1
4801 && s[strlen (s) - 1] != 'u')
4802 {
4803 if (strcmp (s2, "mflo") == 0)
4804 {
67c0d1eb 4805 macro_build (NULL, dbl ? "dneg" : "neg", "d,w", dreg, sreg);
252b5132
RH
4806 }
4807 else
67c0d1eb 4808 move_register (dreg, 0);
8fc2e39e 4809 break;
252b5132
RH
4810 }
4811
8fc2e39e 4812 used_at = 1;
67c0d1eb
RS
4813 load_register (AT, &imm_expr, dbl);
4814 macro_build (NULL, s, "z,s,t", sreg, AT);
4815 macro_build (NULL, s2, "d", dreg);
252b5132
RH
4816 break;
4817
4818 case M_DIVU_3:
4819 s = "divu";
4820 s2 = "mflo";
4821 goto do_divu3;
4822 case M_REMU_3:
4823 s = "divu";
4824 s2 = "mfhi";
4825 goto do_divu3;
4826 case M_DDIVU_3:
4827 s = "ddivu";
4828 s2 = "mflo";
4829 goto do_divu3;
4830 case M_DREMU_3:
4831 s = "ddivu";
4832 s2 = "mfhi";
4833 do_divu3:
7d10b47d 4834 start_noreorder ();
252b5132
RH
4835 if (mips_trap)
4836 {
67c0d1eb
RS
4837 macro_build (NULL, "teq", "s,t,q", treg, 0, 7);
4838 macro_build (NULL, s, "z,s,t", sreg, treg);
252b5132
RH
4839 /* We want to close the noreorder block as soon as possible, so
4840 that later insns are available for delay slot filling. */
7d10b47d 4841 end_noreorder ();
252b5132
RH
4842 }
4843 else
4844 {
4845 expr1.X_add_number = 8;
67c0d1eb
RS
4846 macro_build (&expr1, "bne", "s,t,p", treg, 0);
4847 macro_build (NULL, s, "z,s,t", sreg, treg);
252b5132
RH
4848
4849 /* We want to close the noreorder block as soon as possible, so
4850 that later insns are available for delay slot filling. */
7d10b47d 4851 end_noreorder ();
67c0d1eb 4852 macro_build (NULL, "break", "c", 7);
252b5132 4853 }
67c0d1eb 4854 macro_build (NULL, s2, "d", dreg);
8fc2e39e 4855 break;
252b5132 4856
1abe91b1
MR
4857 case M_DLCA_AB:
4858 dbl = 1;
4859 case M_LCA_AB:
4860 call = 1;
4861 goto do_la;
252b5132
RH
4862 case M_DLA_AB:
4863 dbl = 1;
4864 case M_LA_AB:
1abe91b1 4865 do_la:
252b5132
RH
4866 /* Load the address of a symbol into a register. If breg is not
4867 zero, we then add a base register to it. */
4868
3bec30a8
TS
4869 if (dbl && HAVE_32BIT_GPRS)
4870 as_warn (_("dla used to load 32-bit register"));
4871
c90bbe5b 4872 if (! dbl && HAVE_64BIT_OBJECTS)
3bec30a8
TS
4873 as_warn (_("la used to load 64-bit address"));
4874
0c11417f
MR
4875 if (offset_expr.X_op == O_constant
4876 && offset_expr.X_add_number >= -0x8000
4877 && offset_expr.X_add_number < 0x8000)
4878 {
aed1a261 4879 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
17a2f251 4880 "t,r,j", treg, sreg, BFD_RELOC_LO16);
8fc2e39e 4881 break;
0c11417f
MR
4882 }
4883
b8285c27 4884 if (!mips_opts.noat && (treg == breg))
afdbd6d0
CD
4885 {
4886 tempreg = AT;
4887 used_at = 1;
4888 }
4889 else
4890 {
4891 tempreg = treg;
afdbd6d0
CD
4892 }
4893
252b5132
RH
4894 if (offset_expr.X_op != O_symbol
4895 && offset_expr.X_op != O_constant)
4896 {
4897 as_bad (_("expression too complex"));
4898 offset_expr.X_op = O_constant;
4899 }
4900
252b5132 4901 if (offset_expr.X_op == O_constant)
aed1a261 4902 load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
252b5132
RH
4903 else if (mips_pic == NO_PIC)
4904 {
d6bc6245 4905 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 4906 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
4907 Otherwise we want
4908 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
4909 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4910 If we have a constant, we need two instructions anyhow,
d6bc6245 4911 so we may as well always use the latter form.
76b3015f 4912
6caf9ef4
TS
4913 With 64bit address space and a usable $at we want
4914 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4915 lui $at,<sym> (BFD_RELOC_HI16_S)
4916 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4917 daddiu $at,<sym> (BFD_RELOC_LO16)
4918 dsll32 $tempreg,0
4919 daddu $tempreg,$tempreg,$at
4920
4921 If $at is already in use, we use a path which is suboptimal
4922 on superscalar processors.
4923 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4924 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4925 dsll $tempreg,16
4926 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
4927 dsll $tempreg,16
4928 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
4929
4930 For GP relative symbols in 64bit address space we can use
4931 the same sequence as in 32bit address space. */
aed1a261 4932 if (HAVE_64BIT_SYMBOLS)
252b5132 4933 {
6caf9ef4
TS
4934 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4935 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
4936 {
4937 relax_start (offset_expr.X_add_symbol);
4938 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
4939 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
4940 relax_switch ();
4941 }
d6bc6245 4942
b8285c27 4943 if (used_at == 0 && !mips_opts.noat)
98d3f06f 4944 {
67c0d1eb 4945 macro_build (&offset_expr, "lui", "t,u",
17a2f251 4946 tempreg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb 4947 macro_build (&offset_expr, "lui", "t,u",
17a2f251 4948 AT, BFD_RELOC_HI16_S);
67c0d1eb 4949 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 4950 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
67c0d1eb 4951 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 4952 AT, AT, BFD_RELOC_LO16);
67c0d1eb
RS
4953 macro_build (NULL, "dsll32", "d,w,<", tempreg, tempreg, 0);
4954 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
98d3f06f
KH
4955 used_at = 1;
4956 }
4957 else
4958 {
67c0d1eb 4959 macro_build (&offset_expr, "lui", "t,u",
17a2f251 4960 tempreg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb 4961 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 4962 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
67c0d1eb
RS
4963 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
4964 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 4965 tempreg, tempreg, BFD_RELOC_HI16_S);
67c0d1eb
RS
4966 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
4967 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 4968 tempreg, tempreg, BFD_RELOC_LO16);
98d3f06f 4969 }
6caf9ef4
TS
4970
4971 if (mips_relax.sequence)
4972 relax_end ();
98d3f06f
KH
4973 }
4974 else
4975 {
4976 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 4977 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
98d3f06f 4978 {
4d7206a2 4979 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
4980 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
4981 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 4982 relax_switch ();
98d3f06f 4983 }
6943caf0
ILT
4984 if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
4985 as_bad (_("offset too large"));
67c0d1eb
RS
4986 macro_build_lui (&offset_expr, tempreg);
4987 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
4988 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2
RS
4989 if (mips_relax.sequence)
4990 relax_end ();
98d3f06f 4991 }
252b5132 4992 }
f5040a92 4993 else if (mips_pic == SVR4_PIC && ! mips_big_got && ! HAVE_NEWABI)
252b5132 4994 {
9117d219
NC
4995 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
4996
252b5132
RH
4997 /* If this is a reference to an external symbol, and there
4998 is no constant, we want
4999 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
1abe91b1 5000 or for lca or if tempreg is PIC_CALL_REG
9117d219 5001 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
252b5132
RH
5002 For a local symbol, we want
5003 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5004 nop
5005 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5006
5007 If we have a small constant, and this is a reference to
5008 an external symbol, we want
5009 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5010 nop
5011 addiu $tempreg,$tempreg,<constant>
5012 For a local symbol, we want the same instruction
5013 sequence, but we output a BFD_RELOC_LO16 reloc on the
5014 addiu instruction.
5015
5016 If we have a large constant, and this is a reference to
5017 an external symbol, we want
5018 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5019 lui $at,<hiconstant>
5020 addiu $at,$at,<loconstant>
5021 addu $tempreg,$tempreg,$at
5022 For a local symbol, we want the same instruction
5023 sequence, but we output a BFD_RELOC_LO16 reloc on the
ed6fb7bd 5024 addiu instruction.
ed6fb7bd
SC
5025 */
5026
4d7206a2 5027 if (offset_expr.X_add_number == 0)
252b5132 5028 {
4d7206a2
RS
5029 if (breg == 0 && (call || tempreg == PIC_CALL_REG))
5030 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
5031
5032 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
5033 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5034 lw_reloc_type, mips_gp_register);
4d7206a2 5035 if (breg != 0)
252b5132
RH
5036 {
5037 /* We're going to put in an addu instruction using
5038 tempreg, so we may as well insert the nop right
5039 now. */
269137b2 5040 load_delay_nop ();
252b5132 5041 }
4d7206a2 5042 relax_switch ();
67c0d1eb
RS
5043 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5044 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 5045 load_delay_nop ();
67c0d1eb
RS
5046 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5047 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2 5048 relax_end ();
252b5132
RH
5049 /* FIXME: If breg == 0, and the next instruction uses
5050 $tempreg, then if this variant case is used an extra
5051 nop will be generated. */
5052 }
4d7206a2
RS
5053 else if (offset_expr.X_add_number >= -0x8000
5054 && offset_expr.X_add_number < 0x8000)
252b5132 5055 {
67c0d1eb 5056 load_got_offset (tempreg, &offset_expr);
269137b2 5057 load_delay_nop ();
67c0d1eb 5058 add_got_offset (tempreg, &offset_expr);
252b5132
RH
5059 }
5060 else
5061 {
4d7206a2
RS
5062 expr1.X_add_number = offset_expr.X_add_number;
5063 offset_expr.X_add_number =
5064 ((offset_expr.X_add_number + 0x8000) & 0xffff) - 0x8000;
67c0d1eb 5065 load_got_offset (tempreg, &offset_expr);
f6a22291 5066 offset_expr.X_add_number = expr1.X_add_number;
252b5132
RH
5067 /* If we are going to add in a base register, and the
5068 target register and the base register are the same,
5069 then we are using AT as a temporary register. Since
5070 we want to load the constant into AT, we add our
5071 current AT (from the global offset table) and the
5072 register into the register now, and pretend we were
5073 not using a base register. */
67c0d1eb 5074 if (breg == treg)
252b5132 5075 {
269137b2 5076 load_delay_nop ();
67c0d1eb 5077 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5078 treg, AT, breg);
252b5132
RH
5079 breg = 0;
5080 tempreg = treg;
252b5132 5081 }
f6a22291 5082 add_got_offset_hilo (tempreg, &offset_expr, AT);
252b5132
RH
5083 used_at = 1;
5084 }
5085 }
f5040a92
AO
5086 else if (mips_pic == SVR4_PIC && ! mips_big_got && HAVE_NEWABI)
5087 {
67c0d1eb 5088 int add_breg_early = 0;
f5040a92
AO
5089
5090 /* If this is a reference to an external, and there is no
5091 constant, or local symbol (*), with or without a
5092 constant, we want
5093 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
1abe91b1 5094 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
5095 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5096
5097 If we have a small constant, and this is a reference to
5098 an external symbol, we want
5099 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5100 addiu $tempreg,$tempreg,<constant>
5101
5102 If we have a large constant, and this is a reference to
5103 an external symbol, we want
5104 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5105 lui $at,<hiconstant>
5106 addiu $at,$at,<loconstant>
5107 addu $tempreg,$tempreg,$at
5108
5109 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
5110 local symbols, even though it introduces an additional
5111 instruction. */
5112
f5040a92
AO
5113 if (offset_expr.X_add_number)
5114 {
4d7206a2 5115 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
5116 offset_expr.X_add_number = 0;
5117
4d7206a2 5118 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
5119 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5120 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
5121
5122 if (expr1.X_add_number >= -0x8000
5123 && expr1.X_add_number < 0x8000)
5124 {
67c0d1eb
RS
5125 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
5126 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 5127 }
ecd13cd3 5128 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92
AO
5129 {
5130 int dreg;
5131
5132 /* If we are going to add in a base register, and the
5133 target register and the base register are the same,
5134 then we are using AT as a temporary register. Since
5135 we want to load the constant into AT, we add our
5136 current AT (from the global offset table) and the
5137 register into the register now, and pretend we were
5138 not using a base register. */
5139 if (breg != treg)
5140 dreg = tempreg;
5141 else
5142 {
5143 assert (tempreg == AT);
67c0d1eb
RS
5144 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5145 treg, AT, breg);
f5040a92 5146 dreg = treg;
67c0d1eb 5147 add_breg_early = 1;
f5040a92
AO
5148 }
5149
f6a22291 5150 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 5151 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5152 dreg, dreg, AT);
f5040a92 5153
f5040a92
AO
5154 used_at = 1;
5155 }
5156 else
5157 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5158
4d7206a2 5159 relax_switch ();
f5040a92
AO
5160 offset_expr.X_add_number = expr1.X_add_number;
5161
67c0d1eb
RS
5162 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5163 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5164 if (add_breg_early)
f5040a92 5165 {
67c0d1eb 5166 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
f899b4b8 5167 treg, tempreg, breg);
f5040a92
AO
5168 breg = 0;
5169 tempreg = treg;
5170 }
4d7206a2 5171 relax_end ();
f5040a92 5172 }
4d7206a2 5173 else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
f5040a92 5174 {
4d7206a2 5175 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
5176 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5177 BFD_RELOC_MIPS_CALL16, mips_gp_register);
4d7206a2 5178 relax_switch ();
67c0d1eb
RS
5179 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5180 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2 5181 relax_end ();
f5040a92 5182 }
4d7206a2 5183 else
f5040a92 5184 {
67c0d1eb
RS
5185 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5186 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
5187 }
5188 }
5189 else if (mips_pic == SVR4_PIC && ! HAVE_NEWABI)
252b5132 5190 {
67c0d1eb 5191 int gpdelay;
9117d219
NC
5192 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5193 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
ed6fb7bd 5194 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
252b5132
RH
5195
5196 /* This is the large GOT case. If this is a reference to an
5197 external symbol, and there is no constant, we want
5198 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5199 addu $tempreg,$tempreg,$gp
5200 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 5201 or for lca or if tempreg is PIC_CALL_REG
9117d219
NC
5202 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5203 addu $tempreg,$tempreg,$gp
5204 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
252b5132
RH
5205 For a local symbol, we want
5206 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5207 nop
5208 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5209
5210 If we have a small constant, and this is a reference to
5211 an external symbol, we want
5212 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5213 addu $tempreg,$tempreg,$gp
5214 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5215 nop
5216 addiu $tempreg,$tempreg,<constant>
5217 For a local symbol, we want
5218 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5219 nop
5220 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
5221
5222 If we have a large constant, and this is a reference to
5223 an external symbol, we want
5224 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5225 addu $tempreg,$tempreg,$gp
5226 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5227 lui $at,<hiconstant>
5228 addiu $at,$at,<loconstant>
5229 addu $tempreg,$tempreg,$at
5230 For a local symbol, we want
5231 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5232 lui $at,<hiconstant>
5233 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
5234 addu $tempreg,$tempreg,$at
f5040a92 5235 */
438c16b8 5236
252b5132
RH
5237 expr1.X_add_number = offset_expr.X_add_number;
5238 offset_expr.X_add_number = 0;
4d7206a2 5239 relax_start (offset_expr.X_add_symbol);
67c0d1eb 5240 gpdelay = reg_needs_delay (mips_gp_register);
1abe91b1
MR
5241 if (expr1.X_add_number == 0 && breg == 0
5242 && (call || tempreg == PIC_CALL_REG))
9117d219
NC
5243 {
5244 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5245 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5246 }
67c0d1eb
RS
5247 macro_build (&offset_expr, "lui", "t,u", tempreg, lui_reloc_type);
5248 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5249 tempreg, tempreg, mips_gp_register);
67c0d1eb 5250 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 5251 tempreg, lw_reloc_type, tempreg);
252b5132
RH
5252 if (expr1.X_add_number == 0)
5253 {
67c0d1eb 5254 if (breg != 0)
252b5132
RH
5255 {
5256 /* We're going to put in an addu instruction using
5257 tempreg, so we may as well insert the nop right
5258 now. */
269137b2 5259 load_delay_nop ();
252b5132 5260 }
252b5132
RH
5261 }
5262 else if (expr1.X_add_number >= -0x8000
5263 && expr1.X_add_number < 0x8000)
5264 {
269137b2 5265 load_delay_nop ();
67c0d1eb 5266 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 5267 tempreg, tempreg, BFD_RELOC_LO16);
252b5132
RH
5268 }
5269 else
5270 {
67c0d1eb 5271 int dreg;
252b5132
RH
5272
5273 /* If we are going to add in a base register, and the
5274 target register and the base register are the same,
5275 then we are using AT as a temporary register. Since
5276 we want to load the constant into AT, we add our
5277 current AT (from the global offset table) and the
5278 register into the register now, and pretend we were
5279 not using a base register. */
5280 if (breg != treg)
67c0d1eb 5281 dreg = tempreg;
252b5132
RH
5282 else
5283 {
5284 assert (tempreg == AT);
269137b2 5285 load_delay_nop ();
67c0d1eb 5286 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5287 treg, AT, breg);
252b5132 5288 dreg = treg;
252b5132
RH
5289 }
5290
f6a22291 5291 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 5292 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
252b5132 5293
252b5132
RH
5294 used_at = 1;
5295 }
4d7206a2
RS
5296 offset_expr.X_add_number =
5297 ((expr1.X_add_number + 0x8000) & 0xffff) - 0x8000;
5298 relax_switch ();
252b5132 5299
67c0d1eb 5300 if (gpdelay)
252b5132
RH
5301 {
5302 /* This is needed because this instruction uses $gp, but
f5040a92 5303 the first instruction on the main stream does not. */
67c0d1eb 5304 macro_build (NULL, "nop", "");
252b5132 5305 }
ed6fb7bd 5306
67c0d1eb
RS
5307 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5308 local_reloc_type, mips_gp_register);
f5040a92 5309 if (expr1.X_add_number >= -0x8000
252b5132
RH
5310 && expr1.X_add_number < 0x8000)
5311 {
269137b2 5312 load_delay_nop ();
67c0d1eb
RS
5313 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5314 tempreg, tempreg, BFD_RELOC_LO16);
252b5132 5315 /* FIXME: If add_number is 0, and there was no base
f5040a92
AO
5316 register, the external symbol case ended with a load,
5317 so if the symbol turns out to not be external, and
5318 the next instruction uses tempreg, an unnecessary nop
5319 will be inserted. */
252b5132
RH
5320 }
5321 else
5322 {
5323 if (breg == treg)
5324 {
5325 /* We must add in the base register now, as in the
f5040a92 5326 external symbol case. */
252b5132 5327 assert (tempreg == AT);
269137b2 5328 load_delay_nop ();
67c0d1eb 5329 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5330 treg, AT, breg);
252b5132
RH
5331 tempreg = treg;
5332 /* We set breg to 0 because we have arranged to add
f5040a92 5333 it in in both cases. */
252b5132
RH
5334 breg = 0;
5335 }
5336
67c0d1eb
RS
5337 macro_build_lui (&expr1, AT);
5338 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 5339 AT, AT, BFD_RELOC_LO16);
67c0d1eb 5340 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5341 tempreg, tempreg, AT);
8fc2e39e 5342 used_at = 1;
252b5132 5343 }
4d7206a2 5344 relax_end ();
252b5132 5345 }
f5040a92
AO
5346 else if (mips_pic == SVR4_PIC && HAVE_NEWABI)
5347 {
f5040a92
AO
5348 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5349 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
67c0d1eb 5350 int add_breg_early = 0;
f5040a92
AO
5351
5352 /* This is the large GOT case. If this is a reference to an
5353 external symbol, and there is no constant, we want
5354 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5355 add $tempreg,$tempreg,$gp
5356 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 5357 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
5358 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5359 add $tempreg,$tempreg,$gp
5360 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5361
5362 If we have a small constant, and this is a reference to
5363 an external symbol, we want
5364 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5365 add $tempreg,$tempreg,$gp
5366 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5367 addi $tempreg,$tempreg,<constant>
5368
5369 If we have a large constant, and this is a reference to
5370 an external symbol, we want
5371 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5372 addu $tempreg,$tempreg,$gp
5373 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5374 lui $at,<hiconstant>
5375 addi $at,$at,<loconstant>
5376 add $tempreg,$tempreg,$at
5377
5378 If we have NewABI, and we know it's a local symbol, we want
5379 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
5380 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
5381 otherwise we have to resort to GOT_HI16/GOT_LO16. */
5382
4d7206a2 5383 relax_start (offset_expr.X_add_symbol);
f5040a92 5384
4d7206a2 5385 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
5386 offset_expr.X_add_number = 0;
5387
1abe91b1
MR
5388 if (expr1.X_add_number == 0 && breg == 0
5389 && (call || tempreg == PIC_CALL_REG))
f5040a92
AO
5390 {
5391 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5392 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5393 }
67c0d1eb
RS
5394 macro_build (&offset_expr, "lui", "t,u", tempreg, lui_reloc_type);
5395 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5396 tempreg, tempreg, mips_gp_register);
67c0d1eb
RS
5397 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5398 tempreg, lw_reloc_type, tempreg);
f5040a92
AO
5399
5400 if (expr1.X_add_number == 0)
4d7206a2 5401 ;
f5040a92
AO
5402 else if (expr1.X_add_number >= -0x8000
5403 && expr1.X_add_number < 0x8000)
5404 {
67c0d1eb 5405 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 5406 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 5407 }
ecd13cd3 5408 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92
AO
5409 {
5410 int dreg;
5411
5412 /* If we are going to add in a base register, and the
5413 target register and the base register are the same,
5414 then we are using AT as a temporary register. Since
5415 we want to load the constant into AT, we add our
5416 current AT (from the global offset table) and the
5417 register into the register now, and pretend we were
5418 not using a base register. */
5419 if (breg != treg)
5420 dreg = tempreg;
5421 else
5422 {
5423 assert (tempreg == AT);
67c0d1eb 5424 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5425 treg, AT, breg);
f5040a92 5426 dreg = treg;
67c0d1eb 5427 add_breg_early = 1;
f5040a92
AO
5428 }
5429
f6a22291 5430 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 5431 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
f5040a92 5432
f5040a92
AO
5433 used_at = 1;
5434 }
5435 else
5436 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5437
4d7206a2 5438 relax_switch ();
f5040a92 5439 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
5440 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5441 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
5442 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
5443 tempreg, BFD_RELOC_MIPS_GOT_OFST);
5444 if (add_breg_early)
f5040a92 5445 {
67c0d1eb 5446 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5447 treg, tempreg, breg);
f5040a92
AO
5448 breg = 0;
5449 tempreg = treg;
5450 }
4d7206a2 5451 relax_end ();
f5040a92 5452 }
252b5132
RH
5453 else
5454 abort ();
5455
5456 if (breg != 0)
aed1a261 5457 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", treg, tempreg, breg);
252b5132
RH
5458 break;
5459
5460 case M_J_A:
5461 /* The j instruction may not be used in PIC code, since it
5462 requires an absolute address. We convert it to a b
5463 instruction. */
5464 if (mips_pic == NO_PIC)
67c0d1eb 5465 macro_build (&offset_expr, "j", "a");
252b5132 5466 else
67c0d1eb 5467 macro_build (&offset_expr, "b", "p");
8fc2e39e 5468 break;
252b5132
RH
5469
5470 /* The jal instructions must be handled as macros because when
5471 generating PIC code they expand to multi-instruction
5472 sequences. Normally they are simple instructions. */
5473 case M_JAL_1:
5474 dreg = RA;
5475 /* Fall through. */
5476 case M_JAL_2:
3e722fb5 5477 if (mips_pic == NO_PIC)
67c0d1eb 5478 macro_build (NULL, "jalr", "d,s", dreg, sreg);
252b5132
RH
5479 else if (mips_pic == SVR4_PIC)
5480 {
5481 if (sreg != PIC_CALL_REG)
5482 as_warn (_("MIPS PIC call to register other than $25"));
bdaaa2e1 5483
67c0d1eb 5484 macro_build (NULL, "jalr", "d,s", dreg, sreg);
6478892d 5485 if (! HAVE_NEWABI)
252b5132 5486 {
6478892d
TS
5487 if (mips_cprestore_offset < 0)
5488 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5489 else
5490 {
7a621144
DJ
5491 if (! mips_frame_reg_valid)
5492 {
5493 as_warn (_("No .frame pseudo-op used in PIC code"));
5494 /* Quiet this warning. */
5495 mips_frame_reg_valid = 1;
5496 }
5497 if (! mips_cprestore_valid)
5498 {
5499 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5500 /* Quiet this warning. */
5501 mips_cprestore_valid = 1;
5502 }
6478892d 5503 expr1.X_add_number = mips_cprestore_offset;
67c0d1eb 5504 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 5505 mips_gp_register,
256ab948
TS
5506 mips_frame_reg,
5507 HAVE_64BIT_ADDRESSES);
6478892d 5508 }
252b5132
RH
5509 }
5510 }
5511 else
5512 abort ();
5513
8fc2e39e 5514 break;
252b5132
RH
5515
5516 case M_JAL_A:
5517 if (mips_pic == NO_PIC)
67c0d1eb 5518 macro_build (&offset_expr, "jal", "a");
252b5132
RH
5519 else if (mips_pic == SVR4_PIC)
5520 {
5521 /* If this is a reference to an external symbol, and we are
5522 using a small GOT, we want
5523 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5524 nop
f9419b05 5525 jalr $ra,$25
252b5132
RH
5526 nop
5527 lw $gp,cprestore($sp)
5528 The cprestore value is set using the .cprestore
5529 pseudo-op. If we are using a big GOT, we want
5530 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5531 addu $25,$25,$gp
5532 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
5533 nop
f9419b05 5534 jalr $ra,$25
252b5132
RH
5535 nop
5536 lw $gp,cprestore($sp)
5537 If the symbol is not external, we want
5538 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5539 nop
5540 addiu $25,$25,<sym> (BFD_RELOC_LO16)
f9419b05 5541 jalr $ra,$25
252b5132 5542 nop
438c16b8 5543 lw $gp,cprestore($sp)
f5040a92
AO
5544
5545 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
5546 sequences above, minus nops, unless the symbol is local,
5547 which enables us to use GOT_PAGE/GOT_OFST (big got) or
5548 GOT_DISP. */
438c16b8 5549 if (HAVE_NEWABI)
252b5132 5550 {
f5040a92
AO
5551 if (! mips_big_got)
5552 {
4d7206a2 5553 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
5554 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5555 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
f5040a92 5556 mips_gp_register);
4d7206a2 5557 relax_switch ();
67c0d1eb
RS
5558 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5559 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
4d7206a2
RS
5560 mips_gp_register);
5561 relax_end ();
f5040a92
AO
5562 }
5563 else
5564 {
4d7206a2 5565 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
5566 macro_build (&offset_expr, "lui", "t,u", PIC_CALL_REG,
5567 BFD_RELOC_MIPS_CALL_HI16);
5568 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
5569 PIC_CALL_REG, mips_gp_register);
5570 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5571 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
5572 PIC_CALL_REG);
4d7206a2 5573 relax_switch ();
67c0d1eb
RS
5574 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5575 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
5576 mips_gp_register);
5577 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5578 PIC_CALL_REG, PIC_CALL_REG,
17a2f251 5579 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 5580 relax_end ();
f5040a92 5581 }
684022ea 5582
67c0d1eb 5583 macro_build_jalr (&offset_expr);
252b5132
RH
5584 }
5585 else
5586 {
4d7206a2 5587 relax_start (offset_expr.X_add_symbol);
438c16b8
TS
5588 if (! mips_big_got)
5589 {
67c0d1eb
RS
5590 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5591 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
17a2f251 5592 mips_gp_register);
269137b2 5593 load_delay_nop ();
4d7206a2 5594 relax_switch ();
438c16b8 5595 }
252b5132 5596 else
252b5132 5597 {
67c0d1eb
RS
5598 int gpdelay;
5599
5600 gpdelay = reg_needs_delay (mips_gp_register);
5601 macro_build (&offset_expr, "lui", "t,u", PIC_CALL_REG,
5602 BFD_RELOC_MIPS_CALL_HI16);
5603 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
5604 PIC_CALL_REG, mips_gp_register);
5605 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5606 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
5607 PIC_CALL_REG);
269137b2 5608 load_delay_nop ();
4d7206a2 5609 relax_switch ();
67c0d1eb
RS
5610 if (gpdelay)
5611 macro_build (NULL, "nop", "");
252b5132 5612 }
67c0d1eb
RS
5613 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5614 PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
4d7206a2 5615 mips_gp_register);
269137b2 5616 load_delay_nop ();
67c0d1eb
RS
5617 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5618 PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
4d7206a2 5619 relax_end ();
67c0d1eb 5620 macro_build_jalr (&offset_expr);
438c16b8 5621
6478892d
TS
5622 if (mips_cprestore_offset < 0)
5623 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5624 else
5625 {
7a621144
DJ
5626 if (! mips_frame_reg_valid)
5627 {
5628 as_warn (_("No .frame pseudo-op used in PIC code"));
5629 /* Quiet this warning. */
5630 mips_frame_reg_valid = 1;
5631 }
5632 if (! mips_cprestore_valid)
5633 {
5634 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5635 /* Quiet this warning. */
5636 mips_cprestore_valid = 1;
5637 }
6478892d 5638 if (mips_opts.noreorder)
67c0d1eb 5639 macro_build (NULL, "nop", "");
6478892d 5640 expr1.X_add_number = mips_cprestore_offset;
67c0d1eb 5641 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 5642 mips_gp_register,
256ab948
TS
5643 mips_frame_reg,
5644 HAVE_64BIT_ADDRESSES);
6478892d 5645 }
252b5132
RH
5646 }
5647 }
252b5132
RH
5648 else
5649 abort ();
5650
8fc2e39e 5651 break;
252b5132
RH
5652
5653 case M_LB_AB:
5654 s = "lb";
5655 goto ld;
5656 case M_LBU_AB:
5657 s = "lbu";
5658 goto ld;
5659 case M_LH_AB:
5660 s = "lh";
5661 goto ld;
5662 case M_LHU_AB:
5663 s = "lhu";
5664 goto ld;
5665 case M_LW_AB:
5666 s = "lw";
5667 goto ld;
5668 case M_LWC0_AB:
5669 s = "lwc0";
bdaaa2e1 5670 /* Itbl support may require additional care here. */
252b5132
RH
5671 coproc = 1;
5672 goto ld;
5673 case M_LWC1_AB:
5674 s = "lwc1";
bdaaa2e1 5675 /* Itbl support may require additional care here. */
252b5132
RH
5676 coproc = 1;
5677 goto ld;
5678 case M_LWC2_AB:
5679 s = "lwc2";
bdaaa2e1 5680 /* Itbl support may require additional care here. */
252b5132
RH
5681 coproc = 1;
5682 goto ld;
5683 case M_LWC3_AB:
5684 s = "lwc3";
bdaaa2e1 5685 /* Itbl support may require additional care here. */
252b5132
RH
5686 coproc = 1;
5687 goto ld;
5688 case M_LWL_AB:
5689 s = "lwl";
5690 lr = 1;
5691 goto ld;
5692 case M_LWR_AB:
5693 s = "lwr";
5694 lr = 1;
5695 goto ld;
5696 case M_LDC1_AB:
fef14a42 5697 if (mips_opts.arch == CPU_R4650)
252b5132
RH
5698 {
5699 as_bad (_("opcode not supported on this processor"));
8fc2e39e 5700 break;
252b5132
RH
5701 }
5702 s = "ldc1";
bdaaa2e1 5703 /* Itbl support may require additional care here. */
252b5132
RH
5704 coproc = 1;
5705 goto ld;
5706 case M_LDC2_AB:
5707 s = "ldc2";
bdaaa2e1 5708 /* Itbl support may require additional care here. */
252b5132
RH
5709 coproc = 1;
5710 goto ld;
5711 case M_LDC3_AB:
5712 s = "ldc3";
bdaaa2e1 5713 /* Itbl support may require additional care here. */
252b5132
RH
5714 coproc = 1;
5715 goto ld;
5716 case M_LDL_AB:
5717 s = "ldl";
5718 lr = 1;
5719 goto ld;
5720 case M_LDR_AB:
5721 s = "ldr";
5722 lr = 1;
5723 goto ld;
5724 case M_LL_AB:
5725 s = "ll";
5726 goto ld;
5727 case M_LLD_AB:
5728 s = "lld";
5729 goto ld;
5730 case M_LWU_AB:
5731 s = "lwu";
5732 ld:
8fc2e39e 5733 if (breg == treg || coproc || lr)
252b5132
RH
5734 {
5735 tempreg = AT;
5736 used_at = 1;
5737 }
5738 else
5739 {
5740 tempreg = treg;
252b5132
RH
5741 }
5742 goto ld_st;
5743 case M_SB_AB:
5744 s = "sb";
5745 goto st;
5746 case M_SH_AB:
5747 s = "sh";
5748 goto st;
5749 case M_SW_AB:
5750 s = "sw";
5751 goto st;
5752 case M_SWC0_AB:
5753 s = "swc0";
bdaaa2e1 5754 /* Itbl support may require additional care here. */
252b5132
RH
5755 coproc = 1;
5756 goto st;
5757 case M_SWC1_AB:
5758 s = "swc1";
bdaaa2e1 5759 /* Itbl support may require additional care here. */
252b5132
RH
5760 coproc = 1;
5761 goto st;
5762 case M_SWC2_AB:
5763 s = "swc2";
bdaaa2e1 5764 /* Itbl support may require additional care here. */
252b5132
RH
5765 coproc = 1;
5766 goto st;
5767 case M_SWC3_AB:
5768 s = "swc3";
bdaaa2e1 5769 /* Itbl support may require additional care here. */
252b5132
RH
5770 coproc = 1;
5771 goto st;
5772 case M_SWL_AB:
5773 s = "swl";
5774 goto st;
5775 case M_SWR_AB:
5776 s = "swr";
5777 goto st;
5778 case M_SC_AB:
5779 s = "sc";
5780 goto st;
5781 case M_SCD_AB:
5782 s = "scd";
5783 goto st;
5784 case M_SDC1_AB:
fef14a42 5785 if (mips_opts.arch == CPU_R4650)
252b5132
RH
5786 {
5787 as_bad (_("opcode not supported on this processor"));
8fc2e39e 5788 break;
252b5132
RH
5789 }
5790 s = "sdc1";
5791 coproc = 1;
bdaaa2e1 5792 /* Itbl support may require additional care here. */
252b5132
RH
5793 goto st;
5794 case M_SDC2_AB:
5795 s = "sdc2";
bdaaa2e1 5796 /* Itbl support may require additional care here. */
252b5132
RH
5797 coproc = 1;
5798 goto st;
5799 case M_SDC3_AB:
5800 s = "sdc3";
bdaaa2e1 5801 /* Itbl support may require additional care here. */
252b5132
RH
5802 coproc = 1;
5803 goto st;
5804 case M_SDL_AB:
5805 s = "sdl";
5806 goto st;
5807 case M_SDR_AB:
5808 s = "sdr";
5809 st:
8fc2e39e
TS
5810 tempreg = AT;
5811 used_at = 1;
252b5132 5812 ld_st:
bdaaa2e1 5813 /* Itbl support may require additional care here. */
252b5132
RH
5814 if (mask == M_LWC1_AB
5815 || mask == M_SWC1_AB
5816 || mask == M_LDC1_AB
5817 || mask == M_SDC1_AB
5818 || mask == M_L_DAB
5819 || mask == M_S_DAB)
5820 fmt = "T,o(b)";
5821 else if (coproc)
5822 fmt = "E,o(b)";
5823 else
5824 fmt = "t,o(b)";
5825
5826 if (offset_expr.X_op != O_constant
5827 && offset_expr.X_op != O_symbol)
5828 {
5829 as_bad (_("expression too complex"));
5830 offset_expr.X_op = O_constant;
5831 }
5832
2051e8c4
MR
5833 if (HAVE_32BIT_ADDRESSES
5834 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
5835 {
5836 char value [32];
5837
5838 sprintf_vma (value, offset_expr.X_add_number);
20e1fcfd 5839 as_bad (_("Number (0x%s) larger than 32 bits"), value);
55e08f71 5840 }
2051e8c4 5841
252b5132
RH
5842 /* A constant expression in PIC code can be handled just as it
5843 is in non PIC code. */
aed1a261
RS
5844 if (offset_expr.X_op == O_constant)
5845 {
aed1a261
RS
5846 expr1.X_add_number = ((offset_expr.X_add_number + 0x8000)
5847 & ~(bfd_vma) 0xffff);
2051e8c4 5848 normalize_address_expr (&expr1);
aed1a261
RS
5849 load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
5850 if (breg != 0)
5851 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5852 tempreg, tempreg, breg);
5853 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16, tempreg);
5854 }
5855 else if (mips_pic == NO_PIC)
252b5132
RH
5856 {
5857 /* If this is a reference to a GP relative symbol, and there
5858 is no base register, we want
cdf6fd85 5859 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
5860 Otherwise, if there is no base register, we want
5861 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5862 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5863 If we have a constant, we need two instructions anyhow,
5864 so we always use the latter form.
5865
5866 If we have a base register, and this is a reference to a
5867 GP relative symbol, we want
5868 addu $tempreg,$breg,$gp
cdf6fd85 5869 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
5870 Otherwise we want
5871 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5872 addu $tempreg,$tempreg,$breg
5873 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245 5874 With a constant we always use the latter case.
76b3015f 5875
d6bc6245
TS
5876 With 64bit address space and no base register and $at usable,
5877 we want
5878 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5879 lui $at,<sym> (BFD_RELOC_HI16_S)
5880 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5881 dsll32 $tempreg,0
5882 daddu $tempreg,$at
5883 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5884 If we have a base register, we want
5885 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5886 lui $at,<sym> (BFD_RELOC_HI16_S)
5887 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5888 daddu $at,$breg
5889 dsll32 $tempreg,0
5890 daddu $tempreg,$at
5891 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5892
5893 Without $at we can't generate the optimal path for superscalar
5894 processors here since this would require two temporary registers.
5895 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5896 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5897 dsll $tempreg,16
5898 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5899 dsll $tempreg,16
5900 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5901 If we have a base register, we want
5902 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5903 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5904 dsll $tempreg,16
5905 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5906 dsll $tempreg,16
5907 daddu $tempreg,$tempreg,$breg
5908 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6373ee54 5909
6caf9ef4 5910 For GP relative symbols in 64bit address space we can use
aed1a261
RS
5911 the same sequence as in 32bit address space. */
5912 if (HAVE_64BIT_SYMBOLS)
d6bc6245 5913 {
aed1a261 5914 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4
TS
5915 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
5916 {
5917 relax_start (offset_expr.X_add_symbol);
5918 if (breg == 0)
5919 {
5920 macro_build (&offset_expr, s, fmt, treg,
5921 BFD_RELOC_GPREL16, mips_gp_register);
5922 }
5923 else
5924 {
5925 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5926 tempreg, breg, mips_gp_register);
5927 macro_build (&offset_expr, s, fmt, treg,
5928 BFD_RELOC_GPREL16, tempreg);
5929 }
5930 relax_switch ();
5931 }
d6bc6245 5932
b8285c27 5933 if (used_at == 0 && !mips_opts.noat)
d6bc6245 5934 {
67c0d1eb
RS
5935 macro_build (&offset_expr, "lui", "t,u", tempreg,
5936 BFD_RELOC_MIPS_HIGHEST);
5937 macro_build (&offset_expr, "lui", "t,u", AT,
5938 BFD_RELOC_HI16_S);
5939 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
5940 tempreg, BFD_RELOC_MIPS_HIGHER);
d6bc6245 5941 if (breg != 0)
67c0d1eb
RS
5942 macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
5943 macro_build (NULL, "dsll32", "d,w,<", tempreg, tempreg, 0);
5944 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
5945 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16,
5946 tempreg);
d6bc6245
TS
5947 used_at = 1;
5948 }
5949 else
5950 {
67c0d1eb
RS
5951 macro_build (&offset_expr, "lui", "t,u", tempreg,
5952 BFD_RELOC_MIPS_HIGHEST);
5953 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
5954 tempreg, BFD_RELOC_MIPS_HIGHER);
5955 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
5956 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
5957 tempreg, BFD_RELOC_HI16_S);
5958 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
d6bc6245 5959 if (breg != 0)
67c0d1eb 5960 macro_build (NULL, "daddu", "d,v,t",
17a2f251 5961 tempreg, tempreg, breg);
67c0d1eb 5962 macro_build (&offset_expr, s, fmt, treg,
17a2f251 5963 BFD_RELOC_LO16, tempreg);
d6bc6245 5964 }
6caf9ef4
TS
5965
5966 if (mips_relax.sequence)
5967 relax_end ();
8fc2e39e 5968 break;
d6bc6245 5969 }
256ab948 5970
252b5132
RH
5971 if (breg == 0)
5972 {
67c0d1eb 5973 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 5974 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 5975 {
4d7206a2 5976 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
5977 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_GPREL16,
5978 mips_gp_register);
4d7206a2 5979 relax_switch ();
252b5132 5980 }
67c0d1eb
RS
5981 macro_build_lui (&offset_expr, tempreg);
5982 macro_build (&offset_expr, s, fmt, treg,
17a2f251 5983 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
5984 if (mips_relax.sequence)
5985 relax_end ();
252b5132
RH
5986 }
5987 else
5988 {
67c0d1eb 5989 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 5990 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 5991 {
4d7206a2 5992 relax_start (offset_expr.X_add_symbol);
67c0d1eb 5993 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5994 tempreg, breg, mips_gp_register);
67c0d1eb 5995 macro_build (&offset_expr, s, fmt, treg,
17a2f251 5996 BFD_RELOC_GPREL16, tempreg);
4d7206a2 5997 relax_switch ();
252b5132 5998 }
67c0d1eb
RS
5999 macro_build_lui (&offset_expr, tempreg);
6000 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6001 tempreg, tempreg, breg);
67c0d1eb 6002 macro_build (&offset_expr, s, fmt, treg,
17a2f251 6003 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
6004 if (mips_relax.sequence)
6005 relax_end ();
252b5132
RH
6006 }
6007 }
6008 else if (mips_pic == SVR4_PIC && ! mips_big_got)
6009 {
ed6fb7bd 6010 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
f9419b05 6011
252b5132
RH
6012 /* If this is a reference to an external symbol, we want
6013 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6014 nop
6015 <op> $treg,0($tempreg)
6016 Otherwise we want
6017 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6018 nop
6019 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6020 <op> $treg,0($tempreg)
f5040a92
AO
6021
6022 For NewABI, we want
6023 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6024 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
6025
252b5132
RH
6026 If there is a base register, we add it to $tempreg before
6027 the <op>. If there is a constant, we stick it in the
6028 <op> instruction. We don't handle constants larger than
6029 16 bits, because we have no way to load the upper 16 bits
6030 (actually, we could handle them for the subset of cases
6031 in which we are not using $at). */
6032 assert (offset_expr.X_op == O_symbol);
f5040a92
AO
6033 if (HAVE_NEWABI)
6034 {
67c0d1eb
RS
6035 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6036 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 6037 if (breg != 0)
67c0d1eb 6038 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6039 tempreg, tempreg, breg);
67c0d1eb 6040 macro_build (&offset_expr, s, fmt, treg,
17a2f251 6041 BFD_RELOC_MIPS_GOT_OFST, tempreg);
f5040a92
AO
6042 break;
6043 }
252b5132
RH
6044 expr1.X_add_number = offset_expr.X_add_number;
6045 offset_expr.X_add_number = 0;
6046 if (expr1.X_add_number < -0x8000
6047 || expr1.X_add_number >= 0x8000)
6048 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb
RS
6049 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6050 lw_reloc_type, mips_gp_register);
269137b2 6051 load_delay_nop ();
4d7206a2
RS
6052 relax_start (offset_expr.X_add_symbol);
6053 relax_switch ();
67c0d1eb
RS
6054 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
6055 tempreg, BFD_RELOC_LO16);
4d7206a2 6056 relax_end ();
252b5132 6057 if (breg != 0)
67c0d1eb 6058 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6059 tempreg, tempreg, breg);
67c0d1eb 6060 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
252b5132 6061 }
f5040a92 6062 else if (mips_pic == SVR4_PIC && ! HAVE_NEWABI)
252b5132 6063 {
67c0d1eb 6064 int gpdelay;
252b5132
RH
6065
6066 /* If this is a reference to an external symbol, we want
6067 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6068 addu $tempreg,$tempreg,$gp
6069 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6070 <op> $treg,0($tempreg)
6071 Otherwise we want
6072 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6073 nop
6074 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6075 <op> $treg,0($tempreg)
6076 If there is a base register, we add it to $tempreg before
6077 the <op>. If there is a constant, we stick it in the
6078 <op> instruction. We don't handle constants larger than
6079 16 bits, because we have no way to load the upper 16 bits
6080 (actually, we could handle them for the subset of cases
f5040a92 6081 in which we are not using $at). */
252b5132
RH
6082 assert (offset_expr.X_op == O_symbol);
6083 expr1.X_add_number = offset_expr.X_add_number;
6084 offset_expr.X_add_number = 0;
6085 if (expr1.X_add_number < -0x8000
6086 || expr1.X_add_number >= 0x8000)
6087 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 6088 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 6089 relax_start (offset_expr.X_add_symbol);
67c0d1eb 6090 macro_build (&offset_expr, "lui", "t,u", tempreg,
17a2f251 6091 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
6092 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
6093 mips_gp_register);
6094 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6095 BFD_RELOC_MIPS_GOT_LO16, tempreg);
4d7206a2 6096 relax_switch ();
67c0d1eb
RS
6097 if (gpdelay)
6098 macro_build (NULL, "nop", "");
6099 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6100 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 6101 load_delay_nop ();
67c0d1eb
RS
6102 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
6103 tempreg, BFD_RELOC_LO16);
4d7206a2
RS
6104 relax_end ();
6105
252b5132 6106 if (breg != 0)
67c0d1eb 6107 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6108 tempreg, tempreg, breg);
67c0d1eb 6109 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
252b5132 6110 }
f5040a92
AO
6111 else if (mips_pic == SVR4_PIC && HAVE_NEWABI)
6112 {
f5040a92
AO
6113 /* If this is a reference to an external symbol, we want
6114 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6115 add $tempreg,$tempreg,$gp
6116 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6117 <op> $treg,<ofst>($tempreg)
6118 Otherwise, for local symbols, we want:
6119 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6120 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
6121 assert (offset_expr.X_op == O_symbol);
4d7206a2 6122 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
6123 offset_expr.X_add_number = 0;
6124 if (expr1.X_add_number < -0x8000
6125 || expr1.X_add_number >= 0x8000)
6126 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4d7206a2 6127 relax_start (offset_expr.X_add_symbol);
67c0d1eb 6128 macro_build (&offset_expr, "lui", "t,u", tempreg,
17a2f251 6129 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
6130 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
6131 mips_gp_register);
6132 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6133 BFD_RELOC_MIPS_GOT_LO16, tempreg);
f5040a92 6134 if (breg != 0)
67c0d1eb 6135 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6136 tempreg, tempreg, breg);
67c0d1eb 6137 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
684022ea 6138
4d7206a2 6139 relax_switch ();
f5040a92 6140 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
6141 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6142 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 6143 if (breg != 0)
67c0d1eb 6144 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6145 tempreg, tempreg, breg);
67c0d1eb 6146 macro_build (&offset_expr, s, fmt, treg,
17a2f251 6147 BFD_RELOC_MIPS_GOT_OFST, tempreg);
4d7206a2 6148 relax_end ();
f5040a92 6149 }
252b5132
RH
6150 else
6151 abort ();
6152
252b5132
RH
6153 break;
6154
6155 case M_LI:
6156 case M_LI_S:
67c0d1eb 6157 load_register (treg, &imm_expr, 0);
8fc2e39e 6158 break;
252b5132
RH
6159
6160 case M_DLI:
67c0d1eb 6161 load_register (treg, &imm_expr, 1);
8fc2e39e 6162 break;
252b5132
RH
6163
6164 case M_LI_SS:
6165 if (imm_expr.X_op == O_constant)
6166 {
8fc2e39e 6167 used_at = 1;
67c0d1eb
RS
6168 load_register (AT, &imm_expr, 0);
6169 macro_build (NULL, "mtc1", "t,G", AT, treg);
252b5132
RH
6170 break;
6171 }
6172 else
6173 {
6174 assert (offset_expr.X_op == O_symbol
6175 && strcmp (segment_name (S_GET_SEGMENT
6176 (offset_expr.X_add_symbol)),
6177 ".lit4") == 0
6178 && offset_expr.X_add_number == 0);
67c0d1eb 6179 macro_build (&offset_expr, "lwc1", "T,o(b)", treg,
17a2f251 6180 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
8fc2e39e 6181 break;
252b5132
RH
6182 }
6183
6184 case M_LI_D:
ca4e0257
RS
6185 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
6186 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
6187 order 32 bits of the value and the low order 32 bits are either
6188 zero or in OFFSET_EXPR. */
252b5132
RH
6189 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6190 {
ca4e0257 6191 if (HAVE_64BIT_GPRS)
67c0d1eb 6192 load_register (treg, &imm_expr, 1);
252b5132
RH
6193 else
6194 {
6195 int hreg, lreg;
6196
6197 if (target_big_endian)
6198 {
6199 hreg = treg;
6200 lreg = treg + 1;
6201 }
6202 else
6203 {
6204 hreg = treg + 1;
6205 lreg = treg;
6206 }
6207
6208 if (hreg <= 31)
67c0d1eb 6209 load_register (hreg, &imm_expr, 0);
252b5132
RH
6210 if (lreg <= 31)
6211 {
6212 if (offset_expr.X_op == O_absent)
67c0d1eb 6213 move_register (lreg, 0);
252b5132
RH
6214 else
6215 {
6216 assert (offset_expr.X_op == O_constant);
67c0d1eb 6217 load_register (lreg, &offset_expr, 0);
252b5132
RH
6218 }
6219 }
6220 }
8fc2e39e 6221 break;
252b5132
RH
6222 }
6223
6224 /* We know that sym is in the .rdata section. First we get the
6225 upper 16 bits of the address. */
6226 if (mips_pic == NO_PIC)
6227 {
67c0d1eb 6228 macro_build_lui (&offset_expr, AT);
8fc2e39e 6229 used_at = 1;
252b5132
RH
6230 }
6231 else if (mips_pic == SVR4_PIC)
6232 {
67c0d1eb
RS
6233 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6234 BFD_RELOC_MIPS_GOT16, mips_gp_register);
8fc2e39e 6235 used_at = 1;
252b5132 6236 }
252b5132
RH
6237 else
6238 abort ();
bdaaa2e1 6239
252b5132 6240 /* Now we load the register(s). */
ca4e0257 6241 if (HAVE_64BIT_GPRS)
8fc2e39e
TS
6242 {
6243 used_at = 1;
6244 macro_build (&offset_expr, "ld", "t,o(b)", treg, BFD_RELOC_LO16, AT);
6245 }
252b5132
RH
6246 else
6247 {
8fc2e39e 6248 used_at = 1;
67c0d1eb 6249 macro_build (&offset_expr, "lw", "t,o(b)", treg, BFD_RELOC_LO16, AT);
f9419b05 6250 if (treg != RA)
252b5132
RH
6251 {
6252 /* FIXME: How in the world do we deal with the possible
6253 overflow here? */
6254 offset_expr.X_add_number += 4;
67c0d1eb 6255 macro_build (&offset_expr, "lw", "t,o(b)",
17a2f251 6256 treg + 1, BFD_RELOC_LO16, AT);
252b5132
RH
6257 }
6258 }
252b5132
RH
6259 break;
6260
6261 case M_LI_DD:
ca4e0257
RS
6262 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
6263 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
6264 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
6265 the value and the low order 32 bits are either zero or in
6266 OFFSET_EXPR. */
252b5132
RH
6267 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6268 {
8fc2e39e 6269 used_at = 1;
67c0d1eb 6270 load_register (AT, &imm_expr, HAVE_64BIT_FPRS);
ca4e0257
RS
6271 if (HAVE_64BIT_FPRS)
6272 {
6273 assert (HAVE_64BIT_GPRS);
67c0d1eb 6274 macro_build (NULL, "dmtc1", "t,S", AT, treg);
ca4e0257 6275 }
252b5132
RH
6276 else
6277 {
67c0d1eb 6278 macro_build (NULL, "mtc1", "t,G", AT, treg + 1);
252b5132 6279 if (offset_expr.X_op == O_absent)
67c0d1eb 6280 macro_build (NULL, "mtc1", "t,G", 0, treg);
252b5132
RH
6281 else
6282 {
6283 assert (offset_expr.X_op == O_constant);
67c0d1eb
RS
6284 load_register (AT, &offset_expr, 0);
6285 macro_build (NULL, "mtc1", "t,G", AT, treg);
252b5132
RH
6286 }
6287 }
6288 break;
6289 }
6290
6291 assert (offset_expr.X_op == O_symbol
6292 && offset_expr.X_add_number == 0);
6293 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
6294 if (strcmp (s, ".lit8") == 0)
6295 {
e7af610e 6296 if (mips_opts.isa != ISA_MIPS1)
252b5132 6297 {
67c0d1eb 6298 macro_build (&offset_expr, "ldc1", "T,o(b)", treg,
17a2f251 6299 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
8fc2e39e 6300 break;
252b5132 6301 }
c9914766 6302 breg = mips_gp_register;
252b5132
RH
6303 r = BFD_RELOC_MIPS_LITERAL;
6304 goto dob;
6305 }
6306 else
6307 {
6308 assert (strcmp (s, RDATA_SECTION_NAME) == 0);
8fc2e39e 6309 used_at = 1;
252b5132 6310 if (mips_pic == SVR4_PIC)
67c0d1eb
RS
6311 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6312 BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132
RH
6313 else
6314 {
6315 /* FIXME: This won't work for a 64 bit address. */
67c0d1eb 6316 macro_build_lui (&offset_expr, AT);
252b5132 6317 }
bdaaa2e1 6318
e7af610e 6319 if (mips_opts.isa != ISA_MIPS1)
252b5132 6320 {
67c0d1eb
RS
6321 macro_build (&offset_expr, "ldc1", "T,o(b)",
6322 treg, BFD_RELOC_LO16, AT);
252b5132
RH
6323 break;
6324 }
6325 breg = AT;
6326 r = BFD_RELOC_LO16;
6327 goto dob;
6328 }
6329
6330 case M_L_DOB:
fef14a42 6331 if (mips_opts.arch == CPU_R4650)
252b5132
RH
6332 {
6333 as_bad (_("opcode not supported on this processor"));
8fc2e39e 6334 break;
252b5132
RH
6335 }
6336 /* Even on a big endian machine $fn comes before $fn+1. We have
6337 to adjust when loading from memory. */
6338 r = BFD_RELOC_LO16;
6339 dob:
e7af610e 6340 assert (mips_opts.isa == ISA_MIPS1);
67c0d1eb 6341 macro_build (&offset_expr, "lwc1", "T,o(b)",
17a2f251 6342 target_big_endian ? treg + 1 : treg, r, breg);
252b5132
RH
6343 /* FIXME: A possible overflow which I don't know how to deal
6344 with. */
6345 offset_expr.X_add_number += 4;
67c0d1eb 6346 macro_build (&offset_expr, "lwc1", "T,o(b)",
17a2f251 6347 target_big_endian ? treg : treg + 1, r, breg);
252b5132
RH
6348 break;
6349
6350 case M_L_DAB:
6351 /*
6352 * The MIPS assembler seems to check for X_add_number not
6353 * being double aligned and generating:
6354 * lui at,%hi(foo+1)
6355 * addu at,at,v1
6356 * addiu at,at,%lo(foo+1)
6357 * lwc1 f2,0(at)
6358 * lwc1 f3,4(at)
6359 * But, the resulting address is the same after relocation so why
6360 * generate the extra instruction?
6361 */
fef14a42 6362 if (mips_opts.arch == CPU_R4650)
252b5132
RH
6363 {
6364 as_bad (_("opcode not supported on this processor"));
8fc2e39e 6365 break;
252b5132 6366 }
bdaaa2e1 6367 /* Itbl support may require additional care here. */
252b5132 6368 coproc = 1;
e7af610e 6369 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
6370 {
6371 s = "ldc1";
6372 goto ld;
6373 }
6374
6375 s = "lwc1";
6376 fmt = "T,o(b)";
6377 goto ldd_std;
6378
6379 case M_S_DAB:
fef14a42 6380 if (mips_opts.arch == CPU_R4650)
252b5132
RH
6381 {
6382 as_bad (_("opcode not supported on this processor"));
8fc2e39e 6383 break;
252b5132
RH
6384 }
6385
e7af610e 6386 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
6387 {
6388 s = "sdc1";
6389 goto st;
6390 }
6391
6392 s = "swc1";
6393 fmt = "T,o(b)";
bdaaa2e1 6394 /* Itbl support may require additional care here. */
252b5132
RH
6395 coproc = 1;
6396 goto ldd_std;
6397
6398 case M_LD_AB:
ca4e0257 6399 if (HAVE_64BIT_GPRS)
252b5132
RH
6400 {
6401 s = "ld";
6402 goto ld;
6403 }
6404
6405 s = "lw";
6406 fmt = "t,o(b)";
6407 goto ldd_std;
6408
6409 case M_SD_AB:
ca4e0257 6410 if (HAVE_64BIT_GPRS)
252b5132
RH
6411 {
6412 s = "sd";
6413 goto st;
6414 }
6415
6416 s = "sw";
6417 fmt = "t,o(b)";
6418
6419 ldd_std:
6420 if (offset_expr.X_op != O_symbol
6421 && offset_expr.X_op != O_constant)
6422 {
6423 as_bad (_("expression too complex"));
6424 offset_expr.X_op = O_constant;
6425 }
6426
2051e8c4
MR
6427 if (HAVE_32BIT_ADDRESSES
6428 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
6429 {
6430 char value [32];
6431
6432 sprintf_vma (value, offset_expr.X_add_number);
20e1fcfd 6433 as_bad (_("Number (0x%s) larger than 32 bits"), value);
55e08f71 6434 }
2051e8c4 6435
252b5132
RH
6436 /* Even on a big endian machine $fn comes before $fn+1. We have
6437 to adjust when loading from memory. We set coproc if we must
6438 load $fn+1 first. */
bdaaa2e1 6439 /* Itbl support may require additional care here. */
252b5132
RH
6440 if (! target_big_endian)
6441 coproc = 0;
6442
6443 if (mips_pic == NO_PIC
6444 || offset_expr.X_op == O_constant)
6445 {
6446 /* If this is a reference to a GP relative symbol, we want
cdf6fd85
TS
6447 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6448 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
6449 If we have a base register, we use this
6450 addu $at,$breg,$gp
cdf6fd85
TS
6451 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6452 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
6453 If this is not a GP relative symbol, we want
6454 lui $at,<sym> (BFD_RELOC_HI16_S)
6455 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6456 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6457 If there is a base register, we add it to $at after the
6458 lui instruction. If there is a constant, we always use
6459 the last case. */
39a59cf8
MR
6460 if (offset_expr.X_op == O_symbol
6461 && (valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 6462 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 6463 {
4d7206a2 6464 relax_start (offset_expr.X_add_symbol);
252b5132
RH
6465 if (breg == 0)
6466 {
c9914766 6467 tempreg = mips_gp_register;
252b5132
RH
6468 }
6469 else
6470 {
67c0d1eb 6471 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6472 AT, breg, mips_gp_register);
252b5132 6473 tempreg = AT;
252b5132
RH
6474 used_at = 1;
6475 }
6476
beae10d5 6477 /* Itbl support may require additional care here. */
67c0d1eb 6478 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
17a2f251 6479 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6480 offset_expr.X_add_number += 4;
6481
6482 /* Set mips_optimize to 2 to avoid inserting an
6483 undesired nop. */
6484 hold_mips_optimize = mips_optimize;
6485 mips_optimize = 2;
beae10d5 6486 /* Itbl support may require additional care here. */
67c0d1eb 6487 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
17a2f251 6488 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6489 mips_optimize = hold_mips_optimize;
6490
4d7206a2 6491 relax_switch ();
252b5132
RH
6492
6493 /* We just generated two relocs. When tc_gen_reloc
6494 handles this case, it will skip the first reloc and
6495 handle the second. The second reloc already has an
6496 extra addend of 4, which we added above. We must
6497 subtract it out, and then subtract another 4 to make
6498 the first reloc come out right. The second reloc
6499 will come out right because we are going to add 4 to
6500 offset_expr when we build its instruction below.
6501
6502 If we have a symbol, then we don't want to include
6503 the offset, because it will wind up being included
6504 when we generate the reloc. */
6505
6506 if (offset_expr.X_op == O_constant)
6507 offset_expr.X_add_number -= 8;
6508 else
6509 {
6510 offset_expr.X_add_number = -4;
6511 offset_expr.X_op = O_constant;
6512 }
6513 }
8fc2e39e 6514 used_at = 1;
67c0d1eb 6515 macro_build_lui (&offset_expr, AT);
252b5132 6516 if (breg != 0)
67c0d1eb 6517 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 6518 /* Itbl support may require additional care here. */
67c0d1eb 6519 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
17a2f251 6520 BFD_RELOC_LO16, AT);
252b5132
RH
6521 /* FIXME: How do we handle overflow here? */
6522 offset_expr.X_add_number += 4;
beae10d5 6523 /* Itbl support may require additional care here. */
67c0d1eb 6524 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
17a2f251 6525 BFD_RELOC_LO16, AT);
4d7206a2
RS
6526 if (mips_relax.sequence)
6527 relax_end ();
bdaaa2e1 6528 }
252b5132
RH
6529 else if (mips_pic == SVR4_PIC && ! mips_big_got)
6530 {
252b5132
RH
6531 /* If this is a reference to an external symbol, we want
6532 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6533 nop
6534 <op> $treg,0($at)
6535 <op> $treg+1,4($at)
6536 Otherwise we want
6537 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6538 nop
6539 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6540 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6541 If there is a base register we add it to $at before the
6542 lwc1 instructions. If there is a constant we include it
6543 in the lwc1 instructions. */
6544 used_at = 1;
6545 expr1.X_add_number = offset_expr.X_add_number;
252b5132
RH
6546 if (expr1.X_add_number < -0x8000
6547 || expr1.X_add_number >= 0x8000 - 4)
6548 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 6549 load_got_offset (AT, &offset_expr);
269137b2 6550 load_delay_nop ();
252b5132 6551 if (breg != 0)
67c0d1eb 6552 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
252b5132
RH
6553
6554 /* Set mips_optimize to 2 to avoid inserting an undesired
6555 nop. */
6556 hold_mips_optimize = mips_optimize;
6557 mips_optimize = 2;
4d7206a2 6558
beae10d5 6559 /* Itbl support may require additional care here. */
4d7206a2 6560 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
6561 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
6562 BFD_RELOC_LO16, AT);
4d7206a2 6563 expr1.X_add_number += 4;
67c0d1eb
RS
6564 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
6565 BFD_RELOC_LO16, AT);
4d7206a2 6566 relax_switch ();
67c0d1eb
RS
6567 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6568 BFD_RELOC_LO16, AT);
4d7206a2 6569 offset_expr.X_add_number += 4;
67c0d1eb
RS
6570 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6571 BFD_RELOC_LO16, AT);
4d7206a2 6572 relax_end ();
252b5132 6573
4d7206a2 6574 mips_optimize = hold_mips_optimize;
252b5132
RH
6575 }
6576 else if (mips_pic == SVR4_PIC)
6577 {
67c0d1eb 6578 int gpdelay;
252b5132
RH
6579
6580 /* If this is a reference to an external symbol, we want
6581 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6582 addu $at,$at,$gp
6583 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
6584 nop
6585 <op> $treg,0($at)
6586 <op> $treg+1,4($at)
6587 Otherwise we want
6588 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6589 nop
6590 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6591 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6592 If there is a base register we add it to $at before the
6593 lwc1 instructions. If there is a constant we include it
6594 in the lwc1 instructions. */
6595 used_at = 1;
6596 expr1.X_add_number = offset_expr.X_add_number;
6597 offset_expr.X_add_number = 0;
6598 if (expr1.X_add_number < -0x8000
6599 || expr1.X_add_number >= 0x8000 - 4)
6600 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 6601 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 6602 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
6603 macro_build (&offset_expr, "lui", "t,u",
6604 AT, BFD_RELOC_MIPS_GOT_HI16);
6605 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6606 AT, AT, mips_gp_register);
67c0d1eb 6607 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 6608 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
269137b2 6609 load_delay_nop ();
252b5132 6610 if (breg != 0)
67c0d1eb 6611 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 6612 /* Itbl support may require additional care here. */
67c0d1eb 6613 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
17a2f251 6614 BFD_RELOC_LO16, AT);
252b5132
RH
6615 expr1.X_add_number += 4;
6616
6617 /* Set mips_optimize to 2 to avoid inserting an undesired
6618 nop. */
6619 hold_mips_optimize = mips_optimize;
6620 mips_optimize = 2;
beae10d5 6621 /* Itbl support may require additional care here. */
67c0d1eb 6622 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
17a2f251 6623 BFD_RELOC_LO16, AT);
252b5132
RH
6624 mips_optimize = hold_mips_optimize;
6625 expr1.X_add_number -= 4;
6626
4d7206a2
RS
6627 relax_switch ();
6628 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
6629 if (gpdelay)
6630 macro_build (NULL, "nop", "");
6631 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6632 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 6633 load_delay_nop ();
252b5132 6634 if (breg != 0)
67c0d1eb 6635 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 6636 /* Itbl support may require additional care here. */
67c0d1eb
RS
6637 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6638 BFD_RELOC_LO16, AT);
4d7206a2 6639 offset_expr.X_add_number += 4;
252b5132
RH
6640
6641 /* Set mips_optimize to 2 to avoid inserting an undesired
6642 nop. */
6643 hold_mips_optimize = mips_optimize;
6644 mips_optimize = 2;
beae10d5 6645 /* Itbl support may require additional care here. */
67c0d1eb
RS
6646 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6647 BFD_RELOC_LO16, AT);
252b5132 6648 mips_optimize = hold_mips_optimize;
4d7206a2 6649 relax_end ();
252b5132 6650 }
252b5132
RH
6651 else
6652 abort ();
6653
252b5132
RH
6654 break;
6655
6656 case M_LD_OB:
6657 s = "lw";
6658 goto sd_ob;
6659 case M_SD_OB:
6660 s = "sw";
6661 sd_ob:
ca4e0257 6662 assert (HAVE_32BIT_ADDRESSES);
67c0d1eb 6663 macro_build (&offset_expr, s, "t,o(b)", treg, BFD_RELOC_LO16, breg);
252b5132 6664 offset_expr.X_add_number += 4;
67c0d1eb 6665 macro_build (&offset_expr, s, "t,o(b)", treg + 1, BFD_RELOC_LO16, breg);
8fc2e39e 6666 break;
252b5132
RH
6667
6668 /* New code added to support COPZ instructions.
6669 This code builds table entries out of the macros in mip_opcodes.
6670 R4000 uses interlocks to handle coproc delays.
6671 Other chips (like the R3000) require nops to be inserted for delays.
6672
f72c8c98 6673 FIXME: Currently, we require that the user handle delays.
252b5132
RH
6674 In order to fill delay slots for non-interlocked chips,
6675 we must have a way to specify delays based on the coprocessor.
6676 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
6677 What are the side-effects of the cop instruction?
6678 What cache support might we have and what are its effects?
6679 Both coprocessor & memory require delays. how long???
bdaaa2e1 6680 What registers are read/set/modified?
252b5132
RH
6681
6682 If an itbl is provided to interpret cop instructions,
bdaaa2e1 6683 this knowledge can be encoded in the itbl spec. */
252b5132
RH
6684
6685 case M_COP0:
6686 s = "c0";
6687 goto copz;
6688 case M_COP1:
6689 s = "c1";
6690 goto copz;
6691 case M_COP2:
6692 s = "c2";
6693 goto copz;
6694 case M_COP3:
6695 s = "c3";
6696 copz:
6697 /* For now we just do C (same as Cz). The parameter will be
6698 stored in insn_opcode by mips_ip. */
67c0d1eb 6699 macro_build (NULL, s, "C", ip->insn_opcode);
8fc2e39e 6700 break;
252b5132 6701
ea1fb5dc 6702 case M_MOVE:
67c0d1eb 6703 move_register (dreg, sreg);
8fc2e39e 6704 break;
ea1fb5dc 6705
252b5132
RH
6706#ifdef LOSING_COMPILER
6707 default:
6708 /* Try and see if this is a new itbl instruction.
6709 This code builds table entries out of the macros in mip_opcodes.
6710 FIXME: For now we just assemble the expression and pass it's
6711 value along as a 32-bit immediate.
bdaaa2e1 6712 We may want to have the assembler assemble this value,
252b5132
RH
6713 so that we gain the assembler's knowledge of delay slots,
6714 symbols, etc.
6715 Would it be more efficient to use mask (id) here? */
bdaaa2e1 6716 if (itbl_have_entries
252b5132 6717 && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
beae10d5 6718 {
252b5132
RH
6719 s = ip->insn_mo->name;
6720 s2 = "cop3";
6721 coproc = ITBL_DECODE_PNUM (immed_expr);;
67c0d1eb 6722 macro_build (&immed_expr, s, "C");
8fc2e39e 6723 break;
beae10d5 6724 }
252b5132 6725 macro2 (ip);
8fc2e39e 6726 break;
252b5132 6727 }
8fc2e39e
TS
6728 if (mips_opts.noat && used_at)
6729 as_bad (_("Macro used $at after \".set noat\""));
252b5132 6730}
bdaaa2e1 6731
252b5132 6732static void
17a2f251 6733macro2 (struct mips_cl_insn *ip)
252b5132
RH
6734{
6735 register int treg, sreg, dreg, breg;
6736 int tempreg;
6737 int mask;
252b5132
RH
6738 int used_at;
6739 expressionS expr1;
6740 const char *s;
6741 const char *s2;
6742 const char *fmt;
6743 int likely = 0;
6744 int dbl = 0;
6745 int coproc = 0;
6746 int lr = 0;
6747 int imm = 0;
6748 int off;
6749 offsetT maxnum;
6750 bfd_reloc_code_real_type r;
bdaaa2e1 6751
252b5132
RH
6752 treg = (ip->insn_opcode >> 16) & 0x1f;
6753 dreg = (ip->insn_opcode >> 11) & 0x1f;
6754 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
6755 mask = ip->insn_mo->mask;
bdaaa2e1 6756
252b5132
RH
6757 expr1.X_op = O_constant;
6758 expr1.X_op_symbol = NULL;
6759 expr1.X_add_symbol = NULL;
6760 expr1.X_add_number = 1;
bdaaa2e1 6761
252b5132
RH
6762 switch (mask)
6763 {
6764#endif /* LOSING_COMPILER */
6765
6766 case M_DMUL:
6767 dbl = 1;
6768 case M_MUL:
67c0d1eb
RS
6769 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", sreg, treg);
6770 macro_build (NULL, "mflo", "d", dreg);
8fc2e39e 6771 break;
252b5132
RH
6772
6773 case M_DMUL_I:
6774 dbl = 1;
6775 case M_MUL_I:
6776 /* The MIPS assembler some times generates shifts and adds. I'm
6777 not trying to be that fancy. GCC should do this for us
6778 anyway. */
8fc2e39e 6779 used_at = 1;
67c0d1eb
RS
6780 load_register (AT, &imm_expr, dbl);
6781 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, AT);
6782 macro_build (NULL, "mflo", "d", dreg);
252b5132
RH
6783 break;
6784
6785 case M_DMULO_I:
6786 dbl = 1;
6787 case M_MULO_I:
6788 imm = 1;
6789 goto do_mulo;
6790
6791 case M_DMULO:
6792 dbl = 1;
6793 case M_MULO:
6794 do_mulo:
7d10b47d 6795 start_noreorder ();
8fc2e39e 6796 used_at = 1;
252b5132 6797 if (imm)
67c0d1eb
RS
6798 load_register (AT, &imm_expr, dbl);
6799 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
6800 macro_build (NULL, "mflo", "d", dreg);
6801 macro_build (NULL, dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, RA);
6802 macro_build (NULL, "mfhi", "d", AT);
252b5132 6803 if (mips_trap)
67c0d1eb 6804 macro_build (NULL, "tne", "s,t,q", dreg, AT, 6);
252b5132
RH
6805 else
6806 {
6807 expr1.X_add_number = 8;
67c0d1eb
RS
6808 macro_build (&expr1, "beq", "s,t,p", dreg, AT);
6809 macro_build (NULL, "nop", "", 0);
6810 macro_build (NULL, "break", "c", 6);
252b5132 6811 }
7d10b47d 6812 end_noreorder ();
67c0d1eb 6813 macro_build (NULL, "mflo", "d", dreg);
252b5132
RH
6814 break;
6815
6816 case M_DMULOU_I:
6817 dbl = 1;
6818 case M_MULOU_I:
6819 imm = 1;
6820 goto do_mulou;
6821
6822 case M_DMULOU:
6823 dbl = 1;
6824 case M_MULOU:
6825 do_mulou:
7d10b47d 6826 start_noreorder ();
8fc2e39e 6827 used_at = 1;
252b5132 6828 if (imm)
67c0d1eb
RS
6829 load_register (AT, &imm_expr, dbl);
6830 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
17a2f251 6831 sreg, imm ? AT : treg);
67c0d1eb
RS
6832 macro_build (NULL, "mfhi", "d", AT);
6833 macro_build (NULL, "mflo", "d", dreg);
252b5132 6834 if (mips_trap)
67c0d1eb 6835 macro_build (NULL, "tne", "s,t,q", AT, 0, 6);
252b5132
RH
6836 else
6837 {
6838 expr1.X_add_number = 8;
67c0d1eb
RS
6839 macro_build (&expr1, "beq", "s,t,p", AT, 0);
6840 macro_build (NULL, "nop", "", 0);
6841 macro_build (NULL, "break", "c", 6);
252b5132 6842 }
7d10b47d 6843 end_noreorder ();
252b5132
RH
6844 break;
6845
771c7ce4 6846 case M_DROL:
fef14a42 6847 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097
CD
6848 {
6849 if (dreg == sreg)
6850 {
6851 tempreg = AT;
6852 used_at = 1;
6853 }
6854 else
6855 {
6856 tempreg = dreg;
82dd0097 6857 }
67c0d1eb
RS
6858 macro_build (NULL, "dnegu", "d,w", tempreg, treg);
6859 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, tempreg);
8fc2e39e 6860 break;
82dd0097 6861 }
8fc2e39e 6862 used_at = 1;
67c0d1eb
RS
6863 macro_build (NULL, "dsubu", "d,v,t", AT, 0, treg);
6864 macro_build (NULL, "dsrlv", "d,t,s", AT, sreg, AT);
6865 macro_build (NULL, "dsllv", "d,t,s", dreg, sreg, treg);
6866 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
6867 break;
6868
252b5132 6869 case M_ROL:
fef14a42 6870 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097
CD
6871 {
6872 if (dreg == sreg)
6873 {
6874 tempreg = AT;
6875 used_at = 1;
6876 }
6877 else
6878 {
6879 tempreg = dreg;
82dd0097 6880 }
67c0d1eb
RS
6881 macro_build (NULL, "negu", "d,w", tempreg, treg);
6882 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, tempreg);
8fc2e39e 6883 break;
82dd0097 6884 }
8fc2e39e 6885 used_at = 1;
67c0d1eb
RS
6886 macro_build (NULL, "subu", "d,v,t", AT, 0, treg);
6887 macro_build (NULL, "srlv", "d,t,s", AT, sreg, AT);
6888 macro_build (NULL, "sllv", "d,t,s", dreg, sreg, treg);
6889 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
6890 break;
6891
771c7ce4
TS
6892 case M_DROL_I:
6893 {
6894 unsigned int rot;
82dd0097 6895 char *l, *r;
771c7ce4
TS
6896
6897 if (imm_expr.X_op != O_constant)
82dd0097 6898 as_bad (_("Improper rotate count"));
771c7ce4 6899 rot = imm_expr.X_add_number & 0x3f;
fef14a42 6900 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
60b63b72
RS
6901 {
6902 rot = (64 - rot) & 0x3f;
6903 if (rot >= 32)
67c0d1eb 6904 macro_build (NULL, "dror32", "d,w,<", dreg, sreg, rot - 32);
60b63b72 6905 else
67c0d1eb 6906 macro_build (NULL, "dror", "d,w,<", dreg, sreg, rot);
8fc2e39e 6907 break;
60b63b72 6908 }
483fc7cd 6909 if (rot == 0)
483fc7cd 6910 {
67c0d1eb 6911 macro_build (NULL, "dsrl", "d,w,<", dreg, sreg, 0);
8fc2e39e 6912 break;
483fc7cd 6913 }
82dd0097
CD
6914 l = (rot < 0x20) ? "dsll" : "dsll32";
6915 r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
6916 rot &= 0x1f;
8fc2e39e 6917 used_at = 1;
67c0d1eb
RS
6918 macro_build (NULL, l, "d,w,<", AT, sreg, rot);
6919 macro_build (NULL, r, "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6920 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
6921 }
6922 break;
6923
252b5132 6924 case M_ROL_I:
771c7ce4
TS
6925 {
6926 unsigned int rot;
6927
6928 if (imm_expr.X_op != O_constant)
82dd0097 6929 as_bad (_("Improper rotate count"));
771c7ce4 6930 rot = imm_expr.X_add_number & 0x1f;
fef14a42 6931 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
60b63b72 6932 {
67c0d1eb 6933 macro_build (NULL, "ror", "d,w,<", dreg, sreg, (32 - rot) & 0x1f);
8fc2e39e 6934 break;
60b63b72 6935 }
483fc7cd 6936 if (rot == 0)
483fc7cd 6937 {
67c0d1eb 6938 macro_build (NULL, "srl", "d,w,<", dreg, sreg, 0);
8fc2e39e 6939 break;
483fc7cd 6940 }
8fc2e39e 6941 used_at = 1;
67c0d1eb
RS
6942 macro_build (NULL, "sll", "d,w,<", AT, sreg, rot);
6943 macro_build (NULL, "srl", "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6944 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
6945 }
6946 break;
6947
6948 case M_DROR:
fef14a42 6949 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097 6950 {
67c0d1eb 6951 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, treg);
8fc2e39e 6952 break;
82dd0097 6953 }
8fc2e39e 6954 used_at = 1;
67c0d1eb
RS
6955 macro_build (NULL, "dsubu", "d,v,t", AT, 0, treg);
6956 macro_build (NULL, "dsllv", "d,t,s", AT, sreg, AT);
6957 macro_build (NULL, "dsrlv", "d,t,s", dreg, sreg, treg);
6958 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
6959 break;
6960
6961 case M_ROR:
fef14a42 6962 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 6963 {
67c0d1eb 6964 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, treg);
8fc2e39e 6965 break;
82dd0097 6966 }
8fc2e39e 6967 used_at = 1;
67c0d1eb
RS
6968 macro_build (NULL, "subu", "d,v,t", AT, 0, treg);
6969 macro_build (NULL, "sllv", "d,t,s", AT, sreg, AT);
6970 macro_build (NULL, "srlv", "d,t,s", dreg, sreg, treg);
6971 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
6972 break;
6973
771c7ce4
TS
6974 case M_DROR_I:
6975 {
6976 unsigned int rot;
82dd0097 6977 char *l, *r;
771c7ce4
TS
6978
6979 if (imm_expr.X_op != O_constant)
82dd0097 6980 as_bad (_("Improper rotate count"));
771c7ce4 6981 rot = imm_expr.X_add_number & 0x3f;
fef14a42 6982 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097
CD
6983 {
6984 if (rot >= 32)
67c0d1eb 6985 macro_build (NULL, "dror32", "d,w,<", dreg, sreg, rot - 32);
82dd0097 6986 else
67c0d1eb 6987 macro_build (NULL, "dror", "d,w,<", dreg, sreg, rot);
8fc2e39e 6988 break;
82dd0097 6989 }
483fc7cd 6990 if (rot == 0)
483fc7cd 6991 {
67c0d1eb 6992 macro_build (NULL, "dsrl", "d,w,<", dreg, sreg, 0);
8fc2e39e 6993 break;
483fc7cd 6994 }
82dd0097
CD
6995 r = (rot < 0x20) ? "dsrl" : "dsrl32";
6996 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
6997 rot &= 0x1f;
8fc2e39e 6998 used_at = 1;
67c0d1eb
RS
6999 macro_build (NULL, r, "d,w,<", AT, sreg, rot);
7000 macro_build (NULL, l, "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7001 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
7002 }
7003 break;
7004
252b5132 7005 case M_ROR_I:
771c7ce4
TS
7006 {
7007 unsigned int rot;
7008
7009 if (imm_expr.X_op != O_constant)
82dd0097 7010 as_bad (_("Improper rotate count"));
771c7ce4 7011 rot = imm_expr.X_add_number & 0x1f;
fef14a42 7012 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 7013 {
67c0d1eb 7014 macro_build (NULL, "ror", "d,w,<", dreg, sreg, rot);
8fc2e39e 7015 break;
82dd0097 7016 }
483fc7cd 7017 if (rot == 0)
483fc7cd 7018 {
67c0d1eb 7019 macro_build (NULL, "srl", "d,w,<", dreg, sreg, 0);
8fc2e39e 7020 break;
483fc7cd 7021 }
8fc2e39e 7022 used_at = 1;
67c0d1eb
RS
7023 macro_build (NULL, "srl", "d,w,<", AT, sreg, rot);
7024 macro_build (NULL, "sll", "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7025 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4 7026 }
252b5132
RH
7027 break;
7028
7029 case M_S_DOB:
fef14a42 7030 if (mips_opts.arch == CPU_R4650)
252b5132
RH
7031 {
7032 as_bad (_("opcode not supported on this processor"));
8fc2e39e 7033 break;
252b5132 7034 }
e7af610e 7035 assert (mips_opts.isa == ISA_MIPS1);
252b5132
RH
7036 /* Even on a big endian machine $fn comes before $fn+1. We have
7037 to adjust when storing to memory. */
67c0d1eb
RS
7038 macro_build (&offset_expr, "swc1", "T,o(b)",
7039 target_big_endian ? treg + 1 : treg, BFD_RELOC_LO16, breg);
252b5132 7040 offset_expr.X_add_number += 4;
67c0d1eb
RS
7041 macro_build (&offset_expr, "swc1", "T,o(b)",
7042 target_big_endian ? treg : treg + 1, BFD_RELOC_LO16, breg);
8fc2e39e 7043 break;
252b5132
RH
7044
7045 case M_SEQ:
7046 if (sreg == 0)
67c0d1eb 7047 macro_build (&expr1, "sltiu", "t,r,j", dreg, treg, BFD_RELOC_LO16);
252b5132 7048 else if (treg == 0)
67c0d1eb 7049 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7050 else
7051 {
67c0d1eb
RS
7052 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
7053 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
252b5132 7054 }
8fc2e39e 7055 break;
252b5132
RH
7056
7057 case M_SEQ_I:
7058 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7059 {
67c0d1eb 7060 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
8fc2e39e 7061 break;
252b5132
RH
7062 }
7063 if (sreg == 0)
7064 {
7065 as_warn (_("Instruction %s: result is always false"),
7066 ip->insn_mo->name);
67c0d1eb 7067 move_register (dreg, 0);
8fc2e39e 7068 break;
252b5132
RH
7069 }
7070 if (imm_expr.X_op == O_constant
7071 && imm_expr.X_add_number >= 0
7072 && imm_expr.X_add_number < 0x10000)
7073 {
67c0d1eb 7074 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7075 }
7076 else if (imm_expr.X_op == O_constant
7077 && imm_expr.X_add_number > -0x8000
7078 && imm_expr.X_add_number < 0)
7079 {
7080 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 7081 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
17a2f251 7082 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7083 }
7084 else
7085 {
67c0d1eb
RS
7086 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7087 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
252b5132
RH
7088 used_at = 1;
7089 }
67c0d1eb 7090 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
8fc2e39e 7091 break;
252b5132
RH
7092
7093 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
7094 s = "slt";
7095 goto sge;
7096 case M_SGEU:
7097 s = "sltu";
7098 sge:
67c0d1eb
RS
7099 macro_build (NULL, s, "d,v,t", dreg, sreg, treg);
7100 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
8fc2e39e 7101 break;
252b5132
RH
7102
7103 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
7104 case M_SGEU_I:
7105 if (imm_expr.X_op == O_constant
7106 && imm_expr.X_add_number >= -0x8000
7107 && imm_expr.X_add_number < 0x8000)
7108 {
67c0d1eb
RS
7109 macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
7110 dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7111 }
7112 else
7113 {
67c0d1eb
RS
7114 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7115 macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
7116 dreg, sreg, AT);
252b5132
RH
7117 used_at = 1;
7118 }
67c0d1eb 7119 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
8fc2e39e 7120 break;
252b5132
RH
7121
7122 case M_SGT: /* sreg > treg <==> treg < sreg */
7123 s = "slt";
7124 goto sgt;
7125 case M_SGTU:
7126 s = "sltu";
7127 sgt:
67c0d1eb 7128 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
8fc2e39e 7129 break;
252b5132
RH
7130
7131 case M_SGT_I: /* sreg > I <==> I < sreg */
7132 s = "slt";
7133 goto sgti;
7134 case M_SGTU_I:
7135 s = "sltu";
7136 sgti:
8fc2e39e 7137 used_at = 1;
67c0d1eb
RS
7138 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7139 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
252b5132
RH
7140 break;
7141
2396cfb9 7142 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
252b5132
RH
7143 s = "slt";
7144 goto sle;
7145 case M_SLEU:
7146 s = "sltu";
7147 sle:
67c0d1eb
RS
7148 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
7149 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
8fc2e39e 7150 break;
252b5132 7151
2396cfb9 7152 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
252b5132
RH
7153 s = "slt";
7154 goto slei;
7155 case M_SLEU_I:
7156 s = "sltu";
7157 slei:
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);
7161 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
252b5132
RH
7162 break;
7163
7164 case M_SLT_I:
7165 if (imm_expr.X_op == O_constant
7166 && imm_expr.X_add_number >= -0x8000
7167 && imm_expr.X_add_number < 0x8000)
7168 {
67c0d1eb 7169 macro_build (&imm_expr, "slti", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
8fc2e39e 7170 break;
252b5132 7171 }
8fc2e39e 7172 used_at = 1;
67c0d1eb
RS
7173 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7174 macro_build (NULL, "slt", "d,v,t", dreg, sreg, AT);
252b5132
RH
7175 break;
7176
7177 case M_SLTU_I:
7178 if (imm_expr.X_op == O_constant
7179 && imm_expr.X_add_number >= -0x8000
7180 && imm_expr.X_add_number < 0x8000)
7181 {
67c0d1eb 7182 macro_build (&imm_expr, "sltiu", "t,r,j", dreg, sreg,
17a2f251 7183 BFD_RELOC_LO16);
8fc2e39e 7184 break;
252b5132 7185 }
8fc2e39e 7186 used_at = 1;
67c0d1eb
RS
7187 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7188 macro_build (NULL, "sltu", "d,v,t", dreg, sreg, AT);
252b5132
RH
7189 break;
7190
7191 case M_SNE:
7192 if (sreg == 0)
67c0d1eb 7193 macro_build (NULL, "sltu", "d,v,t", dreg, 0, treg);
252b5132 7194 else if (treg == 0)
67c0d1eb 7195 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
252b5132
RH
7196 else
7197 {
67c0d1eb
RS
7198 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
7199 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
252b5132 7200 }
8fc2e39e 7201 break;
252b5132
RH
7202
7203 case M_SNE_I:
7204 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7205 {
67c0d1eb 7206 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
8fc2e39e 7207 break;
252b5132
RH
7208 }
7209 if (sreg == 0)
7210 {
7211 as_warn (_("Instruction %s: result is always true"),
7212 ip->insn_mo->name);
67c0d1eb
RS
7213 macro_build (&expr1, HAVE_32BIT_GPRS ? "addiu" : "daddiu", "t,r,j",
7214 dreg, 0, BFD_RELOC_LO16);
8fc2e39e 7215 break;
252b5132
RH
7216 }
7217 if (imm_expr.X_op == O_constant
7218 && imm_expr.X_add_number >= 0
7219 && imm_expr.X_add_number < 0x10000)
7220 {
67c0d1eb 7221 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7222 }
7223 else if (imm_expr.X_op == O_constant
7224 && imm_expr.X_add_number > -0x8000
7225 && imm_expr.X_add_number < 0)
7226 {
7227 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 7228 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
17a2f251 7229 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7230 }
7231 else
7232 {
67c0d1eb
RS
7233 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7234 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
252b5132
RH
7235 used_at = 1;
7236 }
67c0d1eb 7237 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
8fc2e39e 7238 break;
252b5132
RH
7239
7240 case M_DSUB_I:
7241 dbl = 1;
7242 case M_SUB_I:
7243 if (imm_expr.X_op == O_constant
7244 && imm_expr.X_add_number > -0x8000
7245 && imm_expr.X_add_number <= 0x8000)
7246 {
7247 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb
RS
7248 macro_build (&imm_expr, dbl ? "daddi" : "addi", "t,r,j",
7249 dreg, sreg, BFD_RELOC_LO16);
8fc2e39e 7250 break;
252b5132 7251 }
8fc2e39e 7252 used_at = 1;
67c0d1eb
RS
7253 load_register (AT, &imm_expr, dbl);
7254 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
252b5132
RH
7255 break;
7256
7257 case M_DSUBU_I:
7258 dbl = 1;
7259 case M_SUBU_I:
7260 if (imm_expr.X_op == O_constant
7261 && imm_expr.X_add_number > -0x8000
7262 && imm_expr.X_add_number <= 0x8000)
7263 {
7264 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb
RS
7265 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "t,r,j",
7266 dreg, sreg, BFD_RELOC_LO16);
8fc2e39e 7267 break;
252b5132 7268 }
8fc2e39e 7269 used_at = 1;
67c0d1eb
RS
7270 load_register (AT, &imm_expr, dbl);
7271 macro_build (NULL, dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
252b5132
RH
7272 break;
7273
7274 case M_TEQ_I:
7275 s = "teq";
7276 goto trap;
7277 case M_TGE_I:
7278 s = "tge";
7279 goto trap;
7280 case M_TGEU_I:
7281 s = "tgeu";
7282 goto trap;
7283 case M_TLT_I:
7284 s = "tlt";
7285 goto trap;
7286 case M_TLTU_I:
7287 s = "tltu";
7288 goto trap;
7289 case M_TNE_I:
7290 s = "tne";
7291 trap:
8fc2e39e 7292 used_at = 1;
67c0d1eb
RS
7293 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7294 macro_build (NULL, s, "s,t", sreg, AT);
252b5132
RH
7295 break;
7296
252b5132 7297 case M_TRUNCWS:
43841e91 7298 case M_TRUNCWD:
e7af610e 7299 assert (mips_opts.isa == ISA_MIPS1);
8fc2e39e 7300 used_at = 1;
252b5132
RH
7301 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
7302 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
7303
7304 /*
7305 * Is the double cfc1 instruction a bug in the mips assembler;
7306 * or is there a reason for it?
7307 */
7d10b47d 7308 start_noreorder ();
67c0d1eb
RS
7309 macro_build (NULL, "cfc1", "t,G", treg, RA);
7310 macro_build (NULL, "cfc1", "t,G", treg, RA);
7311 macro_build (NULL, "nop", "");
252b5132 7312 expr1.X_add_number = 3;
67c0d1eb 7313 macro_build (&expr1, "ori", "t,r,i", AT, treg, BFD_RELOC_LO16);
252b5132 7314 expr1.X_add_number = 2;
67c0d1eb
RS
7315 macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
7316 macro_build (NULL, "ctc1", "t,G", AT, RA);
7317 macro_build (NULL, "nop", "");
7318 macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
7319 dreg, sreg);
7320 macro_build (NULL, "ctc1", "t,G", treg, RA);
7321 macro_build (NULL, "nop", "");
7d10b47d 7322 end_noreorder ();
252b5132
RH
7323 break;
7324
7325 case M_ULH:
7326 s = "lb";
7327 goto ulh;
7328 case M_ULHU:
7329 s = "lbu";
7330 ulh:
8fc2e39e 7331 used_at = 1;
252b5132
RH
7332 if (offset_expr.X_add_number >= 0x7fff)
7333 as_bad (_("operand overflow"));
252b5132 7334 if (! target_big_endian)
f9419b05 7335 ++offset_expr.X_add_number;
67c0d1eb 7336 macro_build (&offset_expr, s, "t,o(b)", AT, BFD_RELOC_LO16, breg);
252b5132 7337 if (! target_big_endian)
f9419b05 7338 --offset_expr.X_add_number;
252b5132 7339 else
f9419b05 7340 ++offset_expr.X_add_number;
67c0d1eb
RS
7341 macro_build (&offset_expr, "lbu", "t,o(b)", treg, BFD_RELOC_LO16, breg);
7342 macro_build (NULL, "sll", "d,w,<", AT, AT, 8);
7343 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
252b5132
RH
7344 break;
7345
7346 case M_ULD:
7347 s = "ldl";
7348 s2 = "ldr";
7349 off = 7;
7350 goto ulw;
7351 case M_ULW:
7352 s = "lwl";
7353 s2 = "lwr";
7354 off = 3;
7355 ulw:
7356 if (offset_expr.X_add_number >= 0x8000 - off)
7357 as_bad (_("operand overflow"));
af22f5b2
CD
7358 if (treg != breg)
7359 tempreg = treg;
7360 else
8fc2e39e
TS
7361 {
7362 used_at = 1;
7363 tempreg = AT;
7364 }
252b5132
RH
7365 if (! target_big_endian)
7366 offset_expr.X_add_number += off;
67c0d1eb 7367 macro_build (&offset_expr, s, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
252b5132
RH
7368 if (! target_big_endian)
7369 offset_expr.X_add_number -= off;
7370 else
7371 offset_expr.X_add_number += off;
67c0d1eb 7372 macro_build (&offset_expr, s2, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
af22f5b2
CD
7373
7374 /* If necessary, move the result in tempreg the final destination. */
7375 if (treg == tempreg)
8fc2e39e 7376 break;
af22f5b2 7377 /* Protect second load's delay slot. */
017315e4 7378 load_delay_nop ();
67c0d1eb 7379 move_register (treg, tempreg);
af22f5b2 7380 break;
252b5132
RH
7381
7382 case M_ULD_A:
7383 s = "ldl";
7384 s2 = "ldr";
7385 off = 7;
7386 goto ulwa;
7387 case M_ULW_A:
7388 s = "lwl";
7389 s2 = "lwr";
7390 off = 3;
7391 ulwa:
d6bc6245 7392 used_at = 1;
67c0d1eb 7393 load_address (AT, &offset_expr, &used_at);
252b5132 7394 if (breg != 0)
67c0d1eb 7395 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
252b5132
RH
7396 if (! target_big_endian)
7397 expr1.X_add_number = off;
7398 else
7399 expr1.X_add_number = 0;
67c0d1eb 7400 macro_build (&expr1, s, "t,o(b)", treg, BFD_RELOC_LO16, AT);
252b5132
RH
7401 if (! target_big_endian)
7402 expr1.X_add_number = 0;
7403 else
7404 expr1.X_add_number = off;
67c0d1eb 7405 macro_build (&expr1, s2, "t,o(b)", treg, BFD_RELOC_LO16, AT);
252b5132
RH
7406 break;
7407
7408 case M_ULH_A:
7409 case M_ULHU_A:
d6bc6245 7410 used_at = 1;
67c0d1eb 7411 load_address (AT, &offset_expr, &used_at);
252b5132 7412 if (breg != 0)
67c0d1eb 7413 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
252b5132
RH
7414 if (target_big_endian)
7415 expr1.X_add_number = 0;
67c0d1eb 7416 macro_build (&expr1, mask == M_ULH_A ? "lb" : "lbu", "t,o(b)",
17a2f251 7417 treg, BFD_RELOC_LO16, AT);
252b5132
RH
7418 if (target_big_endian)
7419 expr1.X_add_number = 1;
7420 else
7421 expr1.X_add_number = 0;
67c0d1eb
RS
7422 macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
7423 macro_build (NULL, "sll", "d,w,<", treg, treg, 8);
7424 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
252b5132
RH
7425 break;
7426
7427 case M_USH:
8fc2e39e 7428 used_at = 1;
252b5132
RH
7429 if (offset_expr.X_add_number >= 0x7fff)
7430 as_bad (_("operand overflow"));
7431 if (target_big_endian)
f9419b05 7432 ++offset_expr.X_add_number;
67c0d1eb
RS
7433 macro_build (&offset_expr, "sb", "t,o(b)", treg, BFD_RELOC_LO16, breg);
7434 macro_build (NULL, "srl", "d,w,<", AT, treg, 8);
252b5132 7435 if (target_big_endian)
f9419b05 7436 --offset_expr.X_add_number;
252b5132 7437 else
f9419b05 7438 ++offset_expr.X_add_number;
67c0d1eb 7439 macro_build (&offset_expr, "sb", "t,o(b)", AT, BFD_RELOC_LO16, breg);
252b5132
RH
7440 break;
7441
7442 case M_USD:
7443 s = "sdl";
7444 s2 = "sdr";
7445 off = 7;
7446 goto usw;
7447 case M_USW:
7448 s = "swl";
7449 s2 = "swr";
7450 off = 3;
7451 usw:
7452 if (offset_expr.X_add_number >= 0x8000 - off)
7453 as_bad (_("operand overflow"));
7454 if (! target_big_endian)
7455 offset_expr.X_add_number += off;
67c0d1eb 7456 macro_build (&offset_expr, s, "t,o(b)", treg, BFD_RELOC_LO16, breg);
252b5132
RH
7457 if (! target_big_endian)
7458 offset_expr.X_add_number -= off;
7459 else
7460 offset_expr.X_add_number += off;
67c0d1eb 7461 macro_build (&offset_expr, s2, "t,o(b)", treg, BFD_RELOC_LO16, breg);
8fc2e39e 7462 break;
252b5132
RH
7463
7464 case M_USD_A:
7465 s = "sdl";
7466 s2 = "sdr";
7467 off = 7;
7468 goto uswa;
7469 case M_USW_A:
7470 s = "swl";
7471 s2 = "swr";
7472 off = 3;
7473 uswa:
d6bc6245 7474 used_at = 1;
67c0d1eb 7475 load_address (AT, &offset_expr, &used_at);
252b5132 7476 if (breg != 0)
67c0d1eb 7477 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
252b5132
RH
7478 if (! target_big_endian)
7479 expr1.X_add_number = off;
7480 else
7481 expr1.X_add_number = 0;
67c0d1eb 7482 macro_build (&expr1, s, "t,o(b)", treg, BFD_RELOC_LO16, AT);
252b5132
RH
7483 if (! target_big_endian)
7484 expr1.X_add_number = 0;
7485 else
7486 expr1.X_add_number = off;
67c0d1eb 7487 macro_build (&expr1, s2, "t,o(b)", treg, BFD_RELOC_LO16, AT);
252b5132
RH
7488 break;
7489
7490 case M_USH_A:
d6bc6245 7491 used_at = 1;
67c0d1eb 7492 load_address (AT, &offset_expr, &used_at);
252b5132 7493 if (breg != 0)
67c0d1eb 7494 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
252b5132
RH
7495 if (! target_big_endian)
7496 expr1.X_add_number = 0;
67c0d1eb
RS
7497 macro_build (&expr1, "sb", "t,o(b)", treg, BFD_RELOC_LO16, AT);
7498 macro_build (NULL, "srl", "d,w,<", treg, treg, 8);
252b5132
RH
7499 if (! target_big_endian)
7500 expr1.X_add_number = 1;
7501 else
7502 expr1.X_add_number = 0;
67c0d1eb 7503 macro_build (&expr1, "sb", "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 = 1;
67c0d1eb
RS
7508 macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
7509 macro_build (NULL, "sll", "d,w,<", treg, treg, 8);
7510 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
252b5132
RH
7511 break;
7512
7513 default:
7514 /* FIXME: Check if this is one of the itbl macros, since they
bdaaa2e1 7515 are added dynamically. */
252b5132
RH
7516 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7517 break;
7518 }
8fc2e39e
TS
7519 if (mips_opts.noat && used_at)
7520 as_bad (_("Macro used $at after \".set noat\""));
252b5132
RH
7521}
7522
7523/* Implement macros in mips16 mode. */
7524
7525static void
17a2f251 7526mips16_macro (struct mips_cl_insn *ip)
252b5132
RH
7527{
7528 int mask;
7529 int xreg, yreg, zreg, tmp;
252b5132
RH
7530 expressionS expr1;
7531 int dbl;
7532 const char *s, *s2, *s3;
7533
7534 mask = ip->insn_mo->mask;
7535
bf12938e
RS
7536 xreg = MIPS16_EXTRACT_OPERAND (RX, *ip);
7537 yreg = MIPS16_EXTRACT_OPERAND (RY, *ip);
7538 zreg = MIPS16_EXTRACT_OPERAND (RZ, *ip);
252b5132 7539
252b5132
RH
7540 expr1.X_op = O_constant;
7541 expr1.X_op_symbol = NULL;
7542 expr1.X_add_symbol = NULL;
7543 expr1.X_add_number = 1;
7544
7545 dbl = 0;
7546
7547 switch (mask)
7548 {
7549 default:
7550 internalError ();
7551
7552 case M_DDIV_3:
7553 dbl = 1;
7554 case M_DIV_3:
7555 s = "mflo";
7556 goto do_div3;
7557 case M_DREM_3:
7558 dbl = 1;
7559 case M_REM_3:
7560 s = "mfhi";
7561 do_div3:
7d10b47d 7562 start_noreorder ();
67c0d1eb 7563 macro_build (NULL, dbl ? "ddiv" : "div", "0,x,y", xreg, yreg);
252b5132 7564 expr1.X_add_number = 2;
67c0d1eb
RS
7565 macro_build (&expr1, "bnez", "x,p", yreg);
7566 macro_build (NULL, "break", "6", 7);
bdaaa2e1 7567
252b5132
RH
7568 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7569 since that causes an overflow. We should do that as well,
7570 but I don't see how to do the comparisons without a temporary
7571 register. */
7d10b47d 7572 end_noreorder ();
67c0d1eb 7573 macro_build (NULL, s, "x", zreg);
252b5132
RH
7574 break;
7575
7576 case M_DIVU_3:
7577 s = "divu";
7578 s2 = "mflo";
7579 goto do_divu3;
7580 case M_REMU_3:
7581 s = "divu";
7582 s2 = "mfhi";
7583 goto do_divu3;
7584 case M_DDIVU_3:
7585 s = "ddivu";
7586 s2 = "mflo";
7587 goto do_divu3;
7588 case M_DREMU_3:
7589 s = "ddivu";
7590 s2 = "mfhi";
7591 do_divu3:
7d10b47d 7592 start_noreorder ();
67c0d1eb 7593 macro_build (NULL, s, "0,x,y", xreg, yreg);
252b5132 7594 expr1.X_add_number = 2;
67c0d1eb
RS
7595 macro_build (&expr1, "bnez", "x,p", yreg);
7596 macro_build (NULL, "break", "6", 7);
7d10b47d 7597 end_noreorder ();
67c0d1eb 7598 macro_build (NULL, s2, "x", zreg);
252b5132
RH
7599 break;
7600
7601 case M_DMUL:
7602 dbl = 1;
7603 case M_MUL:
67c0d1eb
RS
7604 macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
7605 macro_build (NULL, "mflo", "x", zreg);
8fc2e39e 7606 break;
252b5132
RH
7607
7608 case M_DSUBU_I:
7609 dbl = 1;
7610 goto do_subu;
7611 case M_SUBU_I:
7612 do_subu:
7613 if (imm_expr.X_op != O_constant)
7614 as_bad (_("Unsupported large constant"));
7615 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 7616 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
252b5132
RH
7617 break;
7618
7619 case M_SUBU_I_2:
7620 if (imm_expr.X_op != O_constant)
7621 as_bad (_("Unsupported large constant"));
7622 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 7623 macro_build (&imm_expr, "addiu", "x,k", xreg);
252b5132
RH
7624 break;
7625
7626 case M_DSUBU_I_2:
7627 if (imm_expr.X_op != O_constant)
7628 as_bad (_("Unsupported large constant"));
7629 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 7630 macro_build (&imm_expr, "daddiu", "y,j", yreg);
252b5132
RH
7631 break;
7632
7633 case M_BEQ:
7634 s = "cmp";
7635 s2 = "bteqz";
7636 goto do_branch;
7637 case M_BNE:
7638 s = "cmp";
7639 s2 = "btnez";
7640 goto do_branch;
7641 case M_BLT:
7642 s = "slt";
7643 s2 = "btnez";
7644 goto do_branch;
7645 case M_BLTU:
7646 s = "sltu";
7647 s2 = "btnez";
7648 goto do_branch;
7649 case M_BLE:
7650 s = "slt";
7651 s2 = "bteqz";
7652 goto do_reverse_branch;
7653 case M_BLEU:
7654 s = "sltu";
7655 s2 = "bteqz";
7656 goto do_reverse_branch;
7657 case M_BGE:
7658 s = "slt";
7659 s2 = "bteqz";
7660 goto do_branch;
7661 case M_BGEU:
7662 s = "sltu";
7663 s2 = "bteqz";
7664 goto do_branch;
7665 case M_BGT:
7666 s = "slt";
7667 s2 = "btnez";
7668 goto do_reverse_branch;
7669 case M_BGTU:
7670 s = "sltu";
7671 s2 = "btnez";
7672
7673 do_reverse_branch:
7674 tmp = xreg;
7675 xreg = yreg;
7676 yreg = tmp;
7677
7678 do_branch:
67c0d1eb
RS
7679 macro_build (NULL, s, "x,y", xreg, yreg);
7680 macro_build (&offset_expr, s2, "p");
252b5132
RH
7681 break;
7682
7683 case M_BEQ_I:
7684 s = "cmpi";
7685 s2 = "bteqz";
7686 s3 = "x,U";
7687 goto do_branch_i;
7688 case M_BNE_I:
7689 s = "cmpi";
7690 s2 = "btnez";
7691 s3 = "x,U";
7692 goto do_branch_i;
7693 case M_BLT_I:
7694 s = "slti";
7695 s2 = "btnez";
7696 s3 = "x,8";
7697 goto do_branch_i;
7698 case M_BLTU_I:
7699 s = "sltiu";
7700 s2 = "btnez";
7701 s3 = "x,8";
7702 goto do_branch_i;
7703 case M_BLE_I:
7704 s = "slti";
7705 s2 = "btnez";
7706 s3 = "x,8";
7707 goto do_addone_branch_i;
7708 case M_BLEU_I:
7709 s = "sltiu";
7710 s2 = "btnez";
7711 s3 = "x,8";
7712 goto do_addone_branch_i;
7713 case M_BGE_I:
7714 s = "slti";
7715 s2 = "bteqz";
7716 s3 = "x,8";
7717 goto do_branch_i;
7718 case M_BGEU_I:
7719 s = "sltiu";
7720 s2 = "bteqz";
7721 s3 = "x,8";
7722 goto do_branch_i;
7723 case M_BGT_I:
7724 s = "slti";
7725 s2 = "bteqz";
7726 s3 = "x,8";
7727 goto do_addone_branch_i;
7728 case M_BGTU_I:
7729 s = "sltiu";
7730 s2 = "bteqz";
7731 s3 = "x,8";
7732
7733 do_addone_branch_i:
7734 if (imm_expr.X_op != O_constant)
7735 as_bad (_("Unsupported large constant"));
7736 ++imm_expr.X_add_number;
7737
7738 do_branch_i:
67c0d1eb
RS
7739 macro_build (&imm_expr, s, s3, xreg);
7740 macro_build (&offset_expr, s2, "p");
252b5132
RH
7741 break;
7742
7743 case M_ABS:
7744 expr1.X_add_number = 0;
67c0d1eb 7745 macro_build (&expr1, "slti", "x,8", yreg);
252b5132 7746 if (xreg != yreg)
67c0d1eb 7747 move_register (xreg, yreg);
252b5132 7748 expr1.X_add_number = 2;
67c0d1eb
RS
7749 macro_build (&expr1, "bteqz", "p");
7750 macro_build (NULL, "neg", "x,w", xreg, xreg);
252b5132
RH
7751 }
7752}
7753
7754/* For consistency checking, verify that all bits are specified either
7755 by the match/mask part of the instruction definition, or by the
7756 operand list. */
7757static int
17a2f251 7758validate_mips_insn (const struct mips_opcode *opc)
252b5132
RH
7759{
7760 const char *p = opc->args;
7761 char c;
7762 unsigned long used_bits = opc->mask;
7763
7764 if ((used_bits & opc->match) != opc->match)
7765 {
7766 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
7767 opc->name, opc->args);
7768 return 0;
7769 }
7770#define USE_BITS(mask,shift) (used_bits |= ((mask) << (shift)))
7771 while (*p)
7772 switch (c = *p++)
7773 {
7774 case ',': break;
7775 case '(': break;
7776 case ')': break;
af7ee8bf
CD
7777 case '+':
7778 switch (c = *p++)
7779 {
7780 case 'A': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7781 case 'B': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
7782 case 'C': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
bbcc0807
CD
7783 case 'D': USE_BITS (OP_MASK_RD, OP_SH_RD);
7784 USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
5f74bc13
CD
7785 case 'E': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7786 case 'F': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
7787 case 'G': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
7788 case 'H': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
7789 case 'I': break;
ef2e4d86
CF
7790 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7791 case 'T': USE_BITS (OP_MASK_RT, OP_SH_RT);
7792 USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
af7ee8bf
CD
7793 default:
7794 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
7795 c, opc->name, opc->args);
7796 return 0;
7797 }
7798 break;
252b5132
RH
7799 case '<': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7800 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7801 case 'A': break;
4372b673 7802 case 'B': USE_BITS (OP_MASK_CODE20, OP_SH_CODE20); break;
252b5132
RH
7803 case 'C': USE_BITS (OP_MASK_COPZ, OP_SH_COPZ); break;
7804 case 'D': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
7805 case 'E': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7806 case 'F': break;
7807 case 'G': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
156c2f8b 7808 case 'H': USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
252b5132 7809 case 'I': break;
e972090a 7810 case 'J': USE_BITS (OP_MASK_CODE19, OP_SH_CODE19); break;
af7ee8bf 7811 case 'K': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
252b5132
RH
7812 case 'L': break;
7813 case 'M': USE_BITS (OP_MASK_CCC, OP_SH_CCC); break;
7814 case 'N': USE_BITS (OP_MASK_BCC, OP_SH_BCC); break;
deec1734
CD
7815 case 'O': USE_BITS (OP_MASK_ALN, OP_SH_ALN); break;
7816 case 'Q': USE_BITS (OP_MASK_VSEL, OP_SH_VSEL);
7817 USE_BITS (OP_MASK_FT, OP_SH_FT); break;
252b5132
RH
7818 case 'R': USE_BITS (OP_MASK_FR, OP_SH_FR); break;
7819 case 'S': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7820 case 'T': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7821 case 'V': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7822 case 'W': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
deec1734
CD
7823 case 'X': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
7824 case 'Y': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7825 case 'Z': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
252b5132
RH
7826 case 'a': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break;
7827 case 'b': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7828 case 'c': USE_BITS (OP_MASK_CODE, OP_SH_CODE); break;
7829 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
7830 case 'f': break;
7831 case 'h': USE_BITS (OP_MASK_PREFX, OP_SH_PREFX); break;
7832 case 'i': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
7833 case 'j': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7834 case 'k': USE_BITS (OP_MASK_CACHE, OP_SH_CACHE); break;
7835 case 'l': break;
7836 case 'o': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7837 case 'p': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7838 case 'q': USE_BITS (OP_MASK_CODE2, OP_SH_CODE2); break;
7839 case 'r': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7840 case 's': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7841 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7842 case 'u': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
7843 case 'v': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7844 case 'w': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7845 case 'x': break;
7846 case 'z': break;
7847 case 'P': USE_BITS (OP_MASK_PERFREG, OP_SH_PERFREG); break;
4372b673
NC
7848 case 'U': USE_BITS (OP_MASK_RD, OP_SH_RD);
7849 USE_BITS (OP_MASK_RT, OP_SH_RT); break;
60b63b72
RS
7850 case 'e': USE_BITS (OP_MASK_VECBYTE, OP_SH_VECBYTE); break;
7851 case '%': USE_BITS (OP_MASK_VECALIGN, OP_SH_VECALIGN); break;
7852 case '[': break;
7853 case ']': break;
74cd071d
CF
7854 case '3': USE_BITS (OP_MASK_SA3, OP_SH_SA3); break;
7855 case '4': USE_BITS (OP_MASK_SA4, OP_SH_SA4); break;
7856 case '5': USE_BITS (OP_MASK_IMM8, OP_SH_IMM8); break;
7857 case '6': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7858 case '7': USE_BITS (OP_MASK_DSPACC, OP_SH_DSPACC); break;
7859 case '8': USE_BITS (OP_MASK_WRDSP, OP_SH_WRDSP); break;
7860 case '9': USE_BITS (OP_MASK_DSPACC_S, OP_SH_DSPACC_S);break;
7861 case '0': USE_BITS (OP_MASK_DSPSFT, OP_SH_DSPSFT); break;
7862 case '\'': USE_BITS (OP_MASK_RDDSP, OP_SH_RDDSP); break;
7863 case ':': USE_BITS (OP_MASK_DSPSFT_7, OP_SH_DSPSFT_7);break;
7864 case '@': USE_BITS (OP_MASK_IMM10, OP_SH_IMM10); break;
ef2e4d86
CF
7865 case '!': USE_BITS (OP_MASK_MT_U, OP_SH_MT_U); break;
7866 case '$': USE_BITS (OP_MASK_MT_H, OP_SH_MT_H); break;
7867 case '*': USE_BITS (OP_MASK_MTACC_T, OP_SH_MTACC_T); break;
7868 case '&': USE_BITS (OP_MASK_MTACC_D, OP_SH_MTACC_D); break;
7869 case 'g': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
252b5132
RH
7870 default:
7871 as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
7872 c, opc->name, opc->args);
7873 return 0;
7874 }
7875#undef USE_BITS
7876 if (used_bits != 0xffffffff)
7877 {
7878 as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
7879 ~used_bits & 0xffffffff, opc->name, opc->args);
7880 return 0;
7881 }
7882 return 1;
7883}
7884
7885/* This routine assembles an instruction into its binary format. As a
7886 side effect, it sets one of the global variables imm_reloc or
7887 offset_reloc to the type of relocation to do if one of the operands
7888 is an address expression. */
7889
7890static void
17a2f251 7891mips_ip (char *str, struct mips_cl_insn *ip)
252b5132
RH
7892{
7893 char *s;
7894 const char *args;
43841e91 7895 char c = 0;
252b5132
RH
7896 struct mips_opcode *insn;
7897 char *argsStart;
7898 unsigned int regno;
7899 unsigned int lastregno = 0;
af7ee8bf 7900 unsigned int lastpos = 0;
071742cf 7901 unsigned int limlo, limhi;
252b5132
RH
7902 char *s_reset;
7903 char save_c = 0;
74cd071d 7904 offsetT min_range, max_range;
252b5132
RH
7905
7906 insn_error = NULL;
7907
7908 /* If the instruction contains a '.', we first try to match an instruction
7909 including the '.'. Then we try again without the '.'. */
7910 insn = NULL;
3882b010 7911 for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
252b5132
RH
7912 continue;
7913
7914 /* If we stopped on whitespace, then replace the whitespace with null for
7915 the call to hash_find. Save the character we replaced just in case we
7916 have to re-parse the instruction. */
3882b010 7917 if (ISSPACE (*s))
252b5132
RH
7918 {
7919 save_c = *s;
7920 *s++ = '\0';
7921 }
bdaaa2e1 7922
252b5132
RH
7923 insn = (struct mips_opcode *) hash_find (op_hash, str);
7924
7925 /* If we didn't find the instruction in the opcode table, try again, but
7926 this time with just the instruction up to, but not including the
7927 first '.'. */
7928 if (insn == NULL)
7929 {
bdaaa2e1 7930 /* Restore the character we overwrite above (if any). */
252b5132
RH
7931 if (save_c)
7932 *(--s) = save_c;
7933
7934 /* Scan up to the first '.' or whitespace. */
3882b010
L
7935 for (s = str;
7936 *s != '\0' && *s != '.' && !ISSPACE (*s);
7937 ++s)
252b5132
RH
7938 continue;
7939
7940 /* If we did not find a '.', then we can quit now. */
7941 if (*s != '.')
7942 {
7943 insn_error = "unrecognized opcode";
7944 return;
7945 }
7946
7947 /* Lookup the instruction in the hash table. */
7948 *s++ = '\0';
7949 if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
7950 {
7951 insn_error = "unrecognized opcode";
7952 return;
7953 }
252b5132
RH
7954 }
7955
7956 argsStart = s;
7957 for (;;)
7958 {
b34976b6 7959 bfd_boolean ok;
252b5132
RH
7960
7961 assert (strcmp (insn->name, str) == 0);
7962
1f25f5d3
CD
7963 if (OPCODE_IS_MEMBER (insn,
7964 (mips_opts.isa
3396de36 7965 | (file_ase_mips16 ? INSN_MIPS16 : 0)
deec1734 7966 | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
74cd071d 7967 | (mips_opts.ase_dsp ? INSN_DSP : 0)
ef2e4d86 7968 | (mips_opts.ase_mt ? INSN_MT : 0)
98d3f06f 7969 | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
fef14a42 7970 mips_opts.arch))
b34976b6 7971 ok = TRUE;
bdaaa2e1 7972 else
b34976b6 7973 ok = FALSE;
bdaaa2e1 7974
252b5132
RH
7975 if (insn->pinfo != INSN_MACRO)
7976 {
fef14a42 7977 if (mips_opts.arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
b34976b6 7978 ok = FALSE;
252b5132
RH
7979 }
7980
7981 if (! ok)
7982 {
7983 if (insn + 1 < &mips_opcodes[NUMOPCODES]
7984 && strcmp (insn->name, insn[1].name) == 0)
7985 {
7986 ++insn;
7987 continue;
7988 }
252b5132 7989 else
beae10d5 7990 {
268f6bed
L
7991 if (!insn_error)
7992 {
7993 static char buf[100];
fef14a42
TS
7994 sprintf (buf,
7995 _("opcode not supported on this processor: %s (%s)"),
7996 mips_cpu_info_from_arch (mips_opts.arch)->name,
7997 mips_cpu_info_from_isa (mips_opts.isa)->name);
268f6bed
L
7998 insn_error = buf;
7999 }
8000 if (save_c)
8001 *(--s) = save_c;
2bd7f1f3 8002 return;
252b5132 8003 }
252b5132
RH
8004 }
8005
1e915849 8006 create_insn (ip, insn);
268f6bed 8007 insn_error = NULL;
252b5132
RH
8008 for (args = insn->args;; ++args)
8009 {
deec1734
CD
8010 int is_mdmx;
8011
ad8d3bb3 8012 s += strspn (s, " \t");
deec1734 8013 is_mdmx = 0;
252b5132
RH
8014 switch (*args)
8015 {
8016 case '\0': /* end of args */
8017 if (*s == '\0')
8018 return;
8019 break;
8020
74cd071d
CF
8021 case '3': /* dsp 3-bit unsigned immediate in bit 21 */
8022 my_getExpression (&imm_expr, s);
8023 check_absolute_expr (ip, &imm_expr);
8024 if (imm_expr.X_add_number & ~OP_MASK_SA3)
8025 {
8026 as_warn (_("DSP immediate not in range 0..%d (%lu)"),
8027 OP_MASK_SA3, (unsigned long) imm_expr.X_add_number);
8028 imm_expr.X_add_number &= OP_MASK_SA3;
8029 }
8030 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SA3;
8031 imm_expr.X_op = O_absent;
8032 s = expr_end;
8033 continue;
8034
8035 case '4': /* dsp 4-bit unsigned immediate in bit 21 */
8036 my_getExpression (&imm_expr, s);
8037 check_absolute_expr (ip, &imm_expr);
8038 if (imm_expr.X_add_number & ~OP_MASK_SA4)
8039 {
8040 as_warn (_("DSP immediate not in range 0..%d (%lu)"),
8041 OP_MASK_SA4, (unsigned long) imm_expr.X_add_number);
8042 imm_expr.X_add_number &= OP_MASK_SA4;
8043 }
8044 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SA4;
8045 imm_expr.X_op = O_absent;
8046 s = expr_end;
8047 continue;
8048
8049 case '5': /* dsp 8-bit unsigned immediate in bit 16 */
8050 my_getExpression (&imm_expr, s);
8051 check_absolute_expr (ip, &imm_expr);
8052 if (imm_expr.X_add_number & ~OP_MASK_IMM8)
8053 {
8054 as_warn (_("DSP immediate not in range 0..%d (%lu)"),
8055 OP_MASK_IMM8, (unsigned long) imm_expr.X_add_number);
8056 imm_expr.X_add_number &= OP_MASK_IMM8;
8057 }
8058 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_IMM8;
8059 imm_expr.X_op = O_absent;
8060 s = expr_end;
8061 continue;
8062
8063 case '6': /* dsp 5-bit unsigned immediate in bit 21 */
8064 my_getExpression (&imm_expr, s);
8065 check_absolute_expr (ip, &imm_expr);
8066 if (imm_expr.X_add_number & ~OP_MASK_RS)
8067 {
8068 as_warn (_("DSP immediate not in range 0..%d (%lu)"),
8069 OP_MASK_RS, (unsigned long) imm_expr.X_add_number);
8070 imm_expr.X_add_number &= OP_MASK_RS;
8071 }
8072 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_RS;
8073 imm_expr.X_op = O_absent;
8074 s = expr_end;
8075 continue;
8076
8077 case '7': /* four dsp accumulators in bits 11,12 */
8078 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
8079 s[3] >= '0' && s[3] <= '3')
8080 {
8081 regno = s[3] - '0';
8082 s += 4;
8083 ip->insn_opcode |= regno << OP_SH_DSPACC;
8084 continue;
8085 }
8086 else
8087 as_bad (_("Invalid dsp acc register"));
8088 break;
8089
8090 case '8': /* dsp 6-bit unsigned immediate in bit 11 */
8091 my_getExpression (&imm_expr, s);
8092 check_absolute_expr (ip, &imm_expr);
8093 if (imm_expr.X_add_number & ~OP_MASK_WRDSP)
8094 {
8095 as_warn (_("DSP immediate not in range 0..%d (%lu)"),
8096 OP_MASK_WRDSP,
8097 (unsigned long) imm_expr.X_add_number);
8098 imm_expr.X_add_number &= OP_MASK_WRDSP;
8099 }
8100 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_WRDSP;
8101 imm_expr.X_op = O_absent;
8102 s = expr_end;
8103 continue;
8104
8105 case '9': /* four dsp accumulators in bits 21,22 */
8106 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
8107 s[3] >= '0' && s[3] <= '3')
8108 {
8109 regno = s[3] - '0';
8110 s += 4;
8111 ip->insn_opcode |= regno << OP_SH_DSPACC_S;
8112 continue;
8113 }
8114 else
8115 as_bad (_("Invalid dsp acc register"));
8116 break;
8117
8118 case '0': /* dsp 6-bit signed immediate in bit 20 */
8119 my_getExpression (&imm_expr, s);
8120 check_absolute_expr (ip, &imm_expr);
8121 min_range = -((OP_MASK_DSPSFT + 1) >> 1);
8122 max_range = ((OP_MASK_DSPSFT + 1) >> 1) - 1;
8123 if (imm_expr.X_add_number < min_range ||
8124 imm_expr.X_add_number > max_range)
8125 {
8126 as_warn (_("DSP immediate not in range %ld..%ld (%ld)"),
8127 (long) min_range, (long) max_range,
8128 (long) imm_expr.X_add_number);
8129 }
8130 imm_expr.X_add_number &= OP_MASK_DSPSFT;
8131 ip->insn_opcode |= ((unsigned long) imm_expr.X_add_number
8132 << OP_SH_DSPSFT);
8133 imm_expr.X_op = O_absent;
8134 s = expr_end;
8135 continue;
8136
8137 case '\'': /* dsp 6-bit unsigned immediate in bit 16 */
8138 my_getExpression (&imm_expr, s);
8139 check_absolute_expr (ip, &imm_expr);
8140 if (imm_expr.X_add_number & ~OP_MASK_RDDSP)
8141 {
8142 as_warn (_("DSP immediate not in range 0..%d (%lu)"),
8143 OP_MASK_RDDSP,
8144 (unsigned long) imm_expr.X_add_number);
8145 imm_expr.X_add_number &= OP_MASK_RDDSP;
8146 }
8147 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_RDDSP;
8148 imm_expr.X_op = O_absent;
8149 s = expr_end;
8150 continue;
8151
8152 case ':': /* dsp 7-bit signed immediate in bit 19 */
8153 my_getExpression (&imm_expr, s);
8154 check_absolute_expr (ip, &imm_expr);
8155 min_range = -((OP_MASK_DSPSFT_7 + 1) >> 1);
8156 max_range = ((OP_MASK_DSPSFT_7 + 1) >> 1) - 1;
8157 if (imm_expr.X_add_number < min_range ||
8158 imm_expr.X_add_number > max_range)
8159 {
8160 as_warn (_("DSP immediate not in range %ld..%ld (%ld)"),
8161 (long) min_range, (long) max_range,
8162 (long) imm_expr.X_add_number);
8163 }
8164 imm_expr.X_add_number &= OP_MASK_DSPSFT_7;
8165 ip->insn_opcode |= ((unsigned long) imm_expr.X_add_number
8166 << OP_SH_DSPSFT_7);
8167 imm_expr.X_op = O_absent;
8168 s = expr_end;
8169 continue;
8170
8171 case '@': /* dsp 10-bit signed immediate in bit 16 */
8172 my_getExpression (&imm_expr, s);
8173 check_absolute_expr (ip, &imm_expr);
8174 min_range = -((OP_MASK_IMM10 + 1) >> 1);
8175 max_range = ((OP_MASK_IMM10 + 1) >> 1) - 1;
8176 if (imm_expr.X_add_number < min_range ||
8177 imm_expr.X_add_number > max_range)
8178 {
8179 as_warn (_("DSP immediate not in range %ld..%ld (%ld)"),
8180 (long) min_range, (long) max_range,
8181 (long) imm_expr.X_add_number);
8182 }
8183 imm_expr.X_add_number &= OP_MASK_IMM10;
8184 ip->insn_opcode |= ((unsigned long) imm_expr.X_add_number
8185 << OP_SH_IMM10);
8186 imm_expr.X_op = O_absent;
8187 s = expr_end;
8188 continue;
8189
ef2e4d86
CF
8190 case '!': /* mt 1-bit unsigned immediate in bit 5 */
8191 my_getExpression (&imm_expr, s);
8192 check_absolute_expr (ip, &imm_expr);
8193 if (imm_expr.X_add_number & ~OP_MASK_MT_U)
8194 {
8195 as_warn (_("MT immediate not in range 0..%d (%lu)"),
8196 OP_MASK_MT_U, (unsigned long) imm_expr.X_add_number);
8197 imm_expr.X_add_number &= OP_MASK_MT_U;
8198 }
8199 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_MT_U;
8200 imm_expr.X_op = O_absent;
8201 s = expr_end;
8202 continue;
8203
8204 case '$': /* mt 1-bit unsigned immediate in bit 4 */
8205 my_getExpression (&imm_expr, s);
8206 check_absolute_expr (ip, &imm_expr);
8207 if (imm_expr.X_add_number & ~OP_MASK_MT_H)
8208 {
8209 as_warn (_("MT immediate not in range 0..%d (%lu)"),
8210 OP_MASK_MT_H, (unsigned long) imm_expr.X_add_number);
8211 imm_expr.X_add_number &= OP_MASK_MT_H;
8212 }
8213 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_MT_H;
8214 imm_expr.X_op = O_absent;
8215 s = expr_end;
8216 continue;
8217
8218 case '*': /* four dsp accumulators in bits 18,19 */
8219 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
8220 s[3] >= '0' && s[3] <= '3')
8221 {
8222 regno = s[3] - '0';
8223 s += 4;
8224 ip->insn_opcode |= regno << OP_SH_MTACC_T;
8225 continue;
8226 }
8227 else
8228 as_bad (_("Invalid dsp/smartmips acc register"));
8229 break;
8230
8231 case '&': /* four dsp accumulators in bits 13,14 */
8232 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
8233 s[3] >= '0' && s[3] <= '3')
8234 {
8235 regno = s[3] - '0';
8236 s += 4;
8237 ip->insn_opcode |= regno << OP_SH_MTACC_D;
8238 continue;
8239 }
8240 else
8241 as_bad (_("Invalid dsp/smartmips acc register"));
8242 break;
8243
252b5132
RH
8244 case ',':
8245 if (*s++ == *args)
8246 continue;
8247 s--;
8248 switch (*++args)
8249 {
8250 case 'r':
8251 case 'v':
bf12938e 8252 INSERT_OPERAND (RS, *ip, lastregno);
252b5132
RH
8253 continue;
8254
8255 case 'w':
bf12938e 8256 INSERT_OPERAND (RT, *ip, lastregno);
38487616
TS
8257 continue;
8258
252b5132 8259 case 'W':
bf12938e 8260 INSERT_OPERAND (FT, *ip, lastregno);
252b5132
RH
8261 continue;
8262
8263 case 'V':
bf12938e 8264 INSERT_OPERAND (FS, *ip, lastregno);
252b5132
RH
8265 continue;
8266 }
8267 break;
8268
8269 case '(':
8270 /* Handle optional base register.
8271 Either the base register is omitted or
bdaaa2e1 8272 we must have a left paren. */
252b5132
RH
8273 /* This is dependent on the next operand specifier
8274 is a base register specification. */
8275 assert (args[1] == 'b' || args[1] == '5'
8276 || args[1] == '-' || args[1] == '4');
8277 if (*s == '\0')
8278 return;
8279
8280 case ')': /* these must match exactly */
60b63b72
RS
8281 case '[':
8282 case ']':
252b5132
RH
8283 if (*s++ == *args)
8284 continue;
8285 break;
8286
af7ee8bf
CD
8287 case '+': /* Opcode extension character. */
8288 switch (*++args)
8289 {
071742cf
CD
8290 case 'A': /* ins/ext position, becomes LSB. */
8291 limlo = 0;
8292 limhi = 31;
5f74bc13
CD
8293 goto do_lsb;
8294 case 'E':
8295 limlo = 32;
8296 limhi = 63;
8297 goto do_lsb;
8298do_lsb:
071742cf
CD
8299 my_getExpression (&imm_expr, s);
8300 check_absolute_expr (ip, &imm_expr);
8301 if ((unsigned long) imm_expr.X_add_number < limlo
8302 || (unsigned long) imm_expr.X_add_number > limhi)
8303 {
8304 as_bad (_("Improper position (%lu)"),
8305 (unsigned long) imm_expr.X_add_number);
8306 imm_expr.X_add_number = limlo;
8307 }
8308 lastpos = imm_expr.X_add_number;
bf12938e 8309 INSERT_OPERAND (SHAMT, *ip, imm_expr.X_add_number);
071742cf
CD
8310 imm_expr.X_op = O_absent;
8311 s = expr_end;
8312 continue;
8313
8314 case 'B': /* ins size, becomes MSB. */
8315 limlo = 1;
8316 limhi = 32;
5f74bc13
CD
8317 goto do_msb;
8318 case 'F':
8319 limlo = 33;
8320 limhi = 64;
8321 goto do_msb;
8322do_msb:
071742cf
CD
8323 my_getExpression (&imm_expr, s);
8324 check_absolute_expr (ip, &imm_expr);
8325 /* Check for negative input so that small negative numbers
8326 will not succeed incorrectly. The checks against
8327 (pos+size) transitively check "size" itself,
8328 assuming that "pos" is reasonable. */
8329 if ((long) imm_expr.X_add_number < 0
8330 || ((unsigned long) imm_expr.X_add_number
8331 + lastpos) < limlo
8332 || ((unsigned long) imm_expr.X_add_number
8333 + lastpos) > limhi)
8334 {
8335 as_bad (_("Improper insert size (%lu, position %lu)"),
8336 (unsigned long) imm_expr.X_add_number,
8337 (unsigned long) lastpos);
8338 imm_expr.X_add_number = limlo - lastpos;
8339 }
bf12938e
RS
8340 INSERT_OPERAND (INSMSB, *ip,
8341 lastpos + imm_expr.X_add_number - 1);
071742cf
CD
8342 imm_expr.X_op = O_absent;
8343 s = expr_end;
8344 continue;
8345
8346 case 'C': /* ext size, becomes MSBD. */
8347 limlo = 1;
8348 limhi = 32;
5f74bc13
CD
8349 goto do_msbd;
8350 case 'G':
8351 limlo = 33;
8352 limhi = 64;
8353 goto do_msbd;
8354 case 'H':
8355 limlo = 33;
8356 limhi = 64;
8357 goto do_msbd;
8358do_msbd:
071742cf
CD
8359 my_getExpression (&imm_expr, s);
8360 check_absolute_expr (ip, &imm_expr);
8361 /* Check for negative input so that small negative numbers
8362 will not succeed incorrectly. The checks against
8363 (pos+size) transitively check "size" itself,
8364 assuming that "pos" is reasonable. */
8365 if ((long) imm_expr.X_add_number < 0
8366 || ((unsigned long) imm_expr.X_add_number
8367 + lastpos) < limlo
8368 || ((unsigned long) imm_expr.X_add_number
8369 + lastpos) > limhi)
8370 {
8371 as_bad (_("Improper extract size (%lu, position %lu)"),
8372 (unsigned long) imm_expr.X_add_number,
8373 (unsigned long) lastpos);
8374 imm_expr.X_add_number = limlo - lastpos;
8375 }
bf12938e 8376 INSERT_OPERAND (EXTMSBD, *ip, imm_expr.X_add_number - 1);
071742cf
CD
8377 imm_expr.X_op = O_absent;
8378 s = expr_end;
8379 continue;
af7ee8bf 8380
bbcc0807
CD
8381 case 'D':
8382 /* +D is for disassembly only; never match. */
8383 break;
8384
5f74bc13
CD
8385 case 'I':
8386 /* "+I" is like "I", except that imm2_expr is used. */
8387 my_getExpression (&imm2_expr, s);
8388 if (imm2_expr.X_op != O_big
8389 && imm2_expr.X_op != O_constant)
8390 insn_error = _("absolute expression required");
9ee2a2d4
MR
8391 if (HAVE_32BIT_GPRS)
8392 normalize_constant_expr (&imm2_expr);
5f74bc13
CD
8393 s = expr_end;
8394 continue;
8395
ef2e4d86
CF
8396 case 'T': /* Coprocessor register */
8397 /* +T is for disassembly only; never match. */
8398 break;
8399
8400 case 't': /* Coprocessor register number */
8401 if (s[0] == '$' && ISDIGIT (s[1]))
8402 {
8403 ++s;
8404 regno = 0;
8405 do
8406 {
8407 regno *= 10;
8408 regno += *s - '0';
8409 ++s;
8410 }
8411 while (ISDIGIT (*s));
8412 if (regno > 31)
8413 as_bad (_("Invalid register number (%d)"), regno);
8414 else
8415 {
8416 ip->insn_opcode |= regno << OP_SH_RT;
8417 continue;
8418 }
8419 }
8420 else
8421 as_bad (_("Invalid coprocessor 0 register number"));
8422 break;
8423
af7ee8bf
CD
8424 default:
8425 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8426 *args, insn->name, insn->args);
8427 /* Further processing is fruitless. */
8428 return;
8429 }
8430 break;
8431
252b5132
RH
8432 case '<': /* must be at least one digit */
8433 /*
8434 * According to the manual, if the shift amount is greater
b6ff326e
KH
8435 * than 31 or less than 0, then the shift amount should be
8436 * mod 32. In reality the mips assembler issues an error.
252b5132
RH
8437 * We issue a warning and mask out all but the low 5 bits.
8438 */
8439 my_getExpression (&imm_expr, s);
8440 check_absolute_expr (ip, &imm_expr);
8441 if ((unsigned long) imm_expr.X_add_number > 31)
bf12938e
RS
8442 as_warn (_("Improper shift amount (%lu)"),
8443 (unsigned long) imm_expr.X_add_number);
8444 INSERT_OPERAND (SHAMT, *ip, imm_expr.X_add_number);
252b5132
RH
8445 imm_expr.X_op = O_absent;
8446 s = expr_end;
8447 continue;
8448
8449 case '>': /* shift amount minus 32 */
8450 my_getExpression (&imm_expr, s);
8451 check_absolute_expr (ip, &imm_expr);
8452 if ((unsigned long) imm_expr.X_add_number < 32
8453 || (unsigned long) imm_expr.X_add_number > 63)
8454 break;
bf12938e 8455 INSERT_OPERAND (SHAMT, *ip, imm_expr.X_add_number - 32);
252b5132
RH
8456 imm_expr.X_op = O_absent;
8457 s = expr_end;
8458 continue;
8459
252b5132
RH
8460 case 'k': /* cache code */
8461 case 'h': /* prefx code */
8462 my_getExpression (&imm_expr, s);
8463 check_absolute_expr (ip, &imm_expr);
8464 if ((unsigned long) imm_expr.X_add_number > 31)
bf12938e
RS
8465 as_warn (_("Invalid value for `%s' (%lu)"),
8466 ip->insn_mo->name,
8467 (unsigned long) imm_expr.X_add_number);
252b5132 8468 if (*args == 'k')
bf12938e 8469 INSERT_OPERAND (CACHE, *ip, imm_expr.X_add_number);
252b5132 8470 else
bf12938e 8471 INSERT_OPERAND (PREFX, *ip, imm_expr.X_add_number);
252b5132
RH
8472 imm_expr.X_op = O_absent;
8473 s = expr_end;
8474 continue;
8475
8476 case 'c': /* break code */
8477 my_getExpression (&imm_expr, s);
8478 check_absolute_expr (ip, &imm_expr);
793b27f4 8479 if ((unsigned long) imm_expr.X_add_number > 1023)
bf12938e
RS
8480 as_warn (_("Illegal break code (%lu)"),
8481 (unsigned long) imm_expr.X_add_number);
8482 INSERT_OPERAND (CODE, *ip, imm_expr.X_add_number);
252b5132
RH
8483 imm_expr.X_op = O_absent;
8484 s = expr_end;
8485 continue;
8486
8487 case 'q': /* lower break code */
8488 my_getExpression (&imm_expr, s);
8489 check_absolute_expr (ip, &imm_expr);
793b27f4 8490 if ((unsigned long) imm_expr.X_add_number > 1023)
bf12938e
RS
8491 as_warn (_("Illegal lower break code (%lu)"),
8492 (unsigned long) imm_expr.X_add_number);
8493 INSERT_OPERAND (CODE2, *ip, imm_expr.X_add_number);
252b5132
RH
8494 imm_expr.X_op = O_absent;
8495 s = expr_end;
8496 continue;
8497
4372b673 8498 case 'B': /* 20-bit syscall/break code. */
156c2f8b 8499 my_getExpression (&imm_expr, s);
156c2f8b 8500 check_absolute_expr (ip, &imm_expr);
793b27f4
TS
8501 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
8502 as_warn (_("Illegal 20-bit code (%lu)"),
8503 (unsigned long) imm_expr.X_add_number);
bf12938e 8504 INSERT_OPERAND (CODE20, *ip, imm_expr.X_add_number);
252b5132
RH
8505 imm_expr.X_op = O_absent;
8506 s = expr_end;
8507 continue;
8508
98d3f06f 8509 case 'C': /* Coprocessor code */
beae10d5 8510 my_getExpression (&imm_expr, s);
252b5132 8511 check_absolute_expr (ip, &imm_expr);
98d3f06f 8512 if ((unsigned long) imm_expr.X_add_number >= (1 << 25))
252b5132 8513 {
793b27f4
TS
8514 as_warn (_("Coproccesor code > 25 bits (%lu)"),
8515 (unsigned long) imm_expr.X_add_number);
98d3f06f 8516 imm_expr.X_add_number &= ((1 << 25) - 1);
252b5132 8517 }
beae10d5
KH
8518 ip->insn_opcode |= imm_expr.X_add_number;
8519 imm_expr.X_op = O_absent;
8520 s = expr_end;
8521 continue;
252b5132 8522
4372b673
NC
8523 case 'J': /* 19-bit wait code. */
8524 my_getExpression (&imm_expr, s);
8525 check_absolute_expr (ip, &imm_expr);
793b27f4
TS
8526 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
8527 as_warn (_("Illegal 19-bit code (%lu)"),
8528 (unsigned long) imm_expr.X_add_number);
bf12938e 8529 INSERT_OPERAND (CODE19, *ip, imm_expr.X_add_number);
4372b673
NC
8530 imm_expr.X_op = O_absent;
8531 s = expr_end;
8532 continue;
8533
252b5132 8534 case 'P': /* Performance register */
beae10d5 8535 my_getExpression (&imm_expr, s);
252b5132 8536 check_absolute_expr (ip, &imm_expr);
beae10d5 8537 if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
bf12938e
RS
8538 as_warn (_("Invalid performance register (%lu)"),
8539 (unsigned long) imm_expr.X_add_number);
8540 INSERT_OPERAND (PERFREG, *ip, imm_expr.X_add_number);
beae10d5
KH
8541 imm_expr.X_op = O_absent;
8542 s = expr_end;
8543 continue;
252b5132
RH
8544
8545 case 'b': /* base register */
8546 case 'd': /* destination register */
8547 case 's': /* source register */
8548 case 't': /* target register */
8549 case 'r': /* both target and source */
8550 case 'v': /* both dest and source */
8551 case 'w': /* both dest and target */
8552 case 'E': /* coprocessor target register */
8553 case 'G': /* coprocessor destination register */
af7ee8bf 8554 case 'K': /* 'rdhwr' destination register */
252b5132
RH
8555 case 'x': /* ignore register name */
8556 case 'z': /* must be zero register */
4372b673 8557 case 'U': /* destination register (clo/clz). */
ef2e4d86 8558 case 'g': /* coprocessor destination register */
252b5132
RH
8559 s_reset = s;
8560 if (s[0] == '$')
8561 {
8562
3882b010 8563 if (ISDIGIT (s[1]))
252b5132
RH
8564 {
8565 ++s;
8566 regno = 0;
8567 do
8568 {
8569 regno *= 10;
8570 regno += *s - '0';
8571 ++s;
8572 }
3882b010 8573 while (ISDIGIT (*s));
252b5132
RH
8574 if (regno > 31)
8575 as_bad (_("Invalid register number (%d)"), regno);
8576 }
af7ee8bf 8577 else if (*args == 'E' || *args == 'G' || *args == 'K')
252b5132
RH
8578 goto notreg;
8579 else
8580 {
76db943d
TS
8581 if (s[1] == 'r' && s[2] == 'a')
8582 {
8583 s += 3;
8584 regno = RA;
8585 }
8586 else if (s[1] == 'f' && s[2] == 'p')
252b5132
RH
8587 {
8588 s += 3;
8589 regno = FP;
8590 }
8591 else if (s[1] == 's' && s[2] == 'p')
8592 {
8593 s += 3;
8594 regno = SP;
8595 }
8596 else if (s[1] == 'g' && s[2] == 'p')
8597 {
8598 s += 3;
8599 regno = GP;
8600 }
8601 else if (s[1] == 'a' && s[2] == 't')
8602 {
8603 s += 3;
8604 regno = AT;
8605 }
8606 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8607 {
8608 s += 4;
8609 regno = KT0;
8610 }
8611 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8612 {
8613 s += 4;
8614 regno = KT1;
8615 }
85b51719
TS
8616 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
8617 {
8618 s += 5;
8619 regno = ZERO;
8620 }
252b5132
RH
8621 else if (itbl_have_entries)
8622 {
8623 char *p, *n;
d7ba4a77 8624 unsigned long r;
252b5132 8625
d7ba4a77 8626 p = s + 1; /* advance past '$' */
252b5132
RH
8627 n = itbl_get_field (&p); /* n is name */
8628
d7ba4a77
ILT
8629 /* See if this is a register defined in an
8630 itbl entry. */
8631 if (itbl_get_reg_val (n, &r))
252b5132
RH
8632 {
8633 /* Get_field advances to the start of
8634 the next field, so we need to back
d7ba4a77 8635 rack to the end of the last field. */
bdaaa2e1 8636 if (p)
252b5132 8637 s = p - 1;
bdaaa2e1 8638 else
d7ba4a77 8639 s = strchr (s, '\0');
252b5132
RH
8640 regno = r;
8641 }
8642 else
8643 goto notreg;
beae10d5 8644 }
252b5132
RH
8645 else
8646 goto notreg;
8647 }
8648 if (regno == AT
8649 && ! mips_opts.noat
8650 && *args != 'E'
af7ee8bf
CD
8651 && *args != 'G'
8652 && *args != 'K')
252b5132
RH
8653 as_warn (_("Used $at without \".set noat\""));
8654 c = *args;
8655 if (*s == ' ')
f9419b05 8656 ++s;
252b5132
RH
8657 if (args[1] != *s)
8658 {
8659 if (c == 'r' || c == 'v' || c == 'w')
8660 {
8661 regno = lastregno;
8662 s = s_reset;
f9419b05 8663 ++args;
252b5132
RH
8664 }
8665 }
8666 /* 'z' only matches $0. */
8667 if (c == 'z' && regno != 0)
8668 break;
8669
bdaaa2e1
KH
8670 /* Now that we have assembled one operand, we use the args string
8671 * to figure out where it goes in the instruction. */
252b5132
RH
8672 switch (c)
8673 {
8674 case 'r':
8675 case 's':
8676 case 'v':
8677 case 'b':
bf12938e 8678 INSERT_OPERAND (RS, *ip, regno);
252b5132
RH
8679 break;
8680 case 'd':
8681 case 'G':
af7ee8bf 8682 case 'K':
ef2e4d86 8683 case 'g':
bf12938e 8684 INSERT_OPERAND (RD, *ip, regno);
252b5132 8685 break;
4372b673 8686 case 'U':
bf12938e
RS
8687 INSERT_OPERAND (RD, *ip, regno);
8688 INSERT_OPERAND (RT, *ip, regno);
4372b673 8689 break;
252b5132
RH
8690 case 'w':
8691 case 't':
8692 case 'E':
bf12938e 8693 INSERT_OPERAND (RT, *ip, regno);
252b5132
RH
8694 break;
8695 case 'x':
8696 /* This case exists because on the r3000 trunc
8697 expands into a macro which requires a gp
8698 register. On the r6000 or r4000 it is
8699 assembled into a single instruction which
8700 ignores the register. Thus the insn version
8701 is MIPS_ISA2 and uses 'x', and the macro
8702 version is MIPS_ISA1 and uses 't'. */
8703 break;
8704 case 'z':
8705 /* This case is for the div instruction, which
8706 acts differently if the destination argument
8707 is $0. This only matches $0, and is checked
8708 outside the switch. */
8709 break;
8710 case 'D':
8711 /* Itbl operand; not yet implemented. FIXME ?? */
8712 break;
8713 /* What about all other operands like 'i', which
8714 can be specified in the opcode table? */
8715 }
8716 lastregno = regno;
8717 continue;
8718 }
8719 notreg:
8720 switch (*args++)
8721 {
8722 case 'r':
8723 case 'v':
bf12938e 8724 INSERT_OPERAND (RS, *ip, lastregno);
252b5132
RH
8725 continue;
8726 case 'w':
bf12938e 8727 INSERT_OPERAND (RT, *ip, lastregno);
252b5132
RH
8728 continue;
8729 }
8730 break;
8731
deec1734
CD
8732 case 'O': /* MDMX alignment immediate constant. */
8733 my_getExpression (&imm_expr, s);
8734 check_absolute_expr (ip, &imm_expr);
8735 if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
bf12938e
RS
8736 as_warn ("Improper align amount (%ld), using low bits",
8737 (long) imm_expr.X_add_number);
8738 INSERT_OPERAND (ALN, *ip, imm_expr.X_add_number);
deec1734
CD
8739 imm_expr.X_op = O_absent;
8740 s = expr_end;
8741 continue;
8742
8743 case 'Q': /* MDMX vector, element sel, or const. */
8744 if (s[0] != '$')
8745 {
8746 /* MDMX Immediate. */
8747 my_getExpression (&imm_expr, s);
8748 check_absolute_expr (ip, &imm_expr);
8749 if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
bf12938e
RS
8750 as_warn (_("Invalid MDMX Immediate (%ld)"),
8751 (long) imm_expr.X_add_number);
8752 INSERT_OPERAND (FT, *ip, imm_expr.X_add_number);
deec1734
CD
8753 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8754 ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
8755 else
8756 ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
deec1734
CD
8757 imm_expr.X_op = O_absent;
8758 s = expr_end;
8759 continue;
8760 }
8761 /* Not MDMX Immediate. Fall through. */
8762 case 'X': /* MDMX destination register. */
8763 case 'Y': /* MDMX source register. */
8764 case 'Z': /* MDMX target register. */
8765 is_mdmx = 1;
252b5132
RH
8766 case 'D': /* floating point destination register */
8767 case 'S': /* floating point source register */
8768 case 'T': /* floating point target register */
8769 case 'R': /* floating point source register */
8770 case 'V':
8771 case 'W':
8772 s_reset = s;
deec1734
CD
8773 /* Accept $fN for FP and MDMX register numbers, and in
8774 addition accept $vN for MDMX register numbers. */
8775 if ((s[0] == '$' && s[1] == 'f' && ISDIGIT (s[2]))
8776 || (is_mdmx != 0 && s[0] == '$' && s[1] == 'v'
8777 && ISDIGIT (s[2])))
252b5132
RH
8778 {
8779 s += 2;
8780 regno = 0;
8781 do
8782 {
8783 regno *= 10;
8784 regno += *s - '0';
8785 ++s;
8786 }
3882b010 8787 while (ISDIGIT (*s));
252b5132
RH
8788
8789 if (regno > 31)
8790 as_bad (_("Invalid float register number (%d)"), regno);
8791
8792 if ((regno & 1) != 0
ca4e0257 8793 && HAVE_32BIT_FPRS
252b5132
RH
8794 && ! (strcmp (str, "mtc1") == 0
8795 || strcmp (str, "mfc1") == 0
8796 || strcmp (str, "lwc1") == 0
8797 || strcmp (str, "swc1") == 0
8798 || strcmp (str, "l.s") == 0
ef2e4d86
CF
8799 || strcmp (str, "s.s") == 0
8800 || strcmp (str, "mftc1") == 0
8801 || strcmp (str, "mfthc1") == 0
8802 || strcmp (str, "cftc1") == 0
8803 || strcmp (str, "mttc1") == 0
8804 || strcmp (str, "mtthc1") == 0
8805 || strcmp (str, "cttc1") == 0))
252b5132
RH
8806 as_warn (_("Float register should be even, was %d"),
8807 regno);
8808
8809 c = *args;
8810 if (*s == ' ')
f9419b05 8811 ++s;
252b5132
RH
8812 if (args[1] != *s)
8813 {
8814 if (c == 'V' || c == 'W')
8815 {
8816 regno = lastregno;
8817 s = s_reset;
f9419b05 8818 ++args;
252b5132
RH
8819 }
8820 }
8821 switch (c)
8822 {
8823 case 'D':
deec1734 8824 case 'X':
bf12938e 8825 INSERT_OPERAND (FD, *ip, regno);
252b5132
RH
8826 break;
8827 case 'V':
8828 case 'S':
deec1734 8829 case 'Y':
bf12938e 8830 INSERT_OPERAND (FS, *ip, regno);
252b5132 8831 break;
deec1734
CD
8832 case 'Q':
8833 /* This is like 'Z', but also needs to fix the MDMX
8834 vector/scalar select bits. Note that the
8835 scalar immediate case is handled above. */
8836 if (*s == '[')
8837 {
8838 int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
8839 int max_el = (is_qh ? 3 : 7);
8840 s++;
8841 my_getExpression(&imm_expr, s);
8842 check_absolute_expr (ip, &imm_expr);
8843 s = expr_end;
8844 if (imm_expr.X_add_number > max_el)
8845 as_bad(_("Bad element selector %ld"),
8846 (long) imm_expr.X_add_number);
8847 imm_expr.X_add_number &= max_el;
8848 ip->insn_opcode |= (imm_expr.X_add_number
8849 << (OP_SH_VSEL +
8850 (is_qh ? 2 : 1)));
01a3f561 8851 imm_expr.X_op = O_absent;
deec1734
CD
8852 if (*s != ']')
8853 as_warn(_("Expecting ']' found '%s'"), s);
8854 else
8855 s++;
8856 }
8857 else
8858 {
8859 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8860 ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
8861 << OP_SH_VSEL);
8862 else
8863 ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
8864 OP_SH_VSEL);
8865 }
8866 /* Fall through */
252b5132
RH
8867 case 'W':
8868 case 'T':
deec1734 8869 case 'Z':
bf12938e 8870 INSERT_OPERAND (FT, *ip, regno);
252b5132
RH
8871 break;
8872 case 'R':
bf12938e 8873 INSERT_OPERAND (FR, *ip, regno);
252b5132
RH
8874 break;
8875 }
8876 lastregno = regno;
8877 continue;
8878 }
8879
252b5132
RH
8880 switch (*args++)
8881 {
8882 case 'V':
bf12938e 8883 INSERT_OPERAND (FS, *ip, lastregno);
252b5132
RH
8884 continue;
8885 case 'W':
bf12938e 8886 INSERT_OPERAND (FT, *ip, lastregno);
252b5132
RH
8887 continue;
8888 }
8889 break;
8890
8891 case 'I':
8892 my_getExpression (&imm_expr, s);
8893 if (imm_expr.X_op != O_big
8894 && imm_expr.X_op != O_constant)
8895 insn_error = _("absolute expression required");
9ee2a2d4
MR
8896 if (HAVE_32BIT_GPRS)
8897 normalize_constant_expr (&imm_expr);
252b5132
RH
8898 s = expr_end;
8899 continue;
8900
8901 case 'A':
8902 my_getExpression (&offset_expr, s);
2051e8c4 8903 normalize_address_expr (&offset_expr);
f6688943 8904 *imm_reloc = BFD_RELOC_32;
252b5132
RH
8905 s = expr_end;
8906 continue;
8907
8908 case 'F':
8909 case 'L':
8910 case 'f':
8911 case 'l':
8912 {
8913 int f64;
ca4e0257 8914 int using_gprs;
252b5132
RH
8915 char *save_in;
8916 char *err;
8917 unsigned char temp[8];
8918 int len;
8919 unsigned int length;
8920 segT seg;
8921 subsegT subseg;
8922 char *p;
8923
8924 /* These only appear as the last operand in an
8925 instruction, and every instruction that accepts
8926 them in any variant accepts them in all variants.
8927 This means we don't have to worry about backing out
8928 any changes if the instruction does not match.
8929
8930 The difference between them is the size of the
8931 floating point constant and where it goes. For 'F'
8932 and 'L' the constant is 64 bits; for 'f' and 'l' it
8933 is 32 bits. Where the constant is placed is based
8934 on how the MIPS assembler does things:
8935 F -- .rdata
8936 L -- .lit8
8937 f -- immediate value
8938 l -- .lit4
8939
8940 The .lit4 and .lit8 sections are only used if
8941 permitted by the -G argument.
8942
ca4e0257
RS
8943 The code below needs to know whether the target register
8944 is 32 or 64 bits wide. It relies on the fact 'f' and
8945 'F' are used with GPR-based instructions and 'l' and
8946 'L' are used with FPR-based instructions. */
252b5132
RH
8947
8948 f64 = *args == 'F' || *args == 'L';
ca4e0257 8949 using_gprs = *args == 'F' || *args == 'f';
252b5132
RH
8950
8951 save_in = input_line_pointer;
8952 input_line_pointer = s;
8953 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
8954 length = len;
8955 s = input_line_pointer;
8956 input_line_pointer = save_in;
8957 if (err != NULL && *err != '\0')
8958 {
8959 as_bad (_("Bad floating point constant: %s"), err);
8960 memset (temp, '\0', sizeof temp);
8961 length = f64 ? 8 : 4;
8962 }
8963
156c2f8b 8964 assert (length == (unsigned) (f64 ? 8 : 4));
252b5132
RH
8965
8966 if (*args == 'f'
8967 || (*args == 'l'
3e722fb5 8968 && (g_switch_value < 4
252b5132
RH
8969 || (temp[0] == 0 && temp[1] == 0)
8970 || (temp[2] == 0 && temp[3] == 0))))
8971 {
8972 imm_expr.X_op = O_constant;
8973 if (! target_big_endian)
8974 imm_expr.X_add_number = bfd_getl32 (temp);
8975 else
8976 imm_expr.X_add_number = bfd_getb32 (temp);
8977 }
8978 else if (length > 4
119d663a 8979 && ! mips_disable_float_construction
ca4e0257
RS
8980 /* Constants can only be constructed in GPRs and
8981 copied to FPRs if the GPRs are at least as wide
8982 as the FPRs. Force the constant into memory if
8983 we are using 64-bit FPRs but the GPRs are only
8984 32 bits wide. */
8985 && (using_gprs
8986 || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
252b5132
RH
8987 && ((temp[0] == 0 && temp[1] == 0)
8988 || (temp[2] == 0 && temp[3] == 0))
8989 && ((temp[4] == 0 && temp[5] == 0)
8990 || (temp[6] == 0 && temp[7] == 0)))
8991 {
ca4e0257
RS
8992 /* The value is simple enough to load with a couple of
8993 instructions. If using 32-bit registers, set
8994 imm_expr to the high order 32 bits and offset_expr to
8995 the low order 32 bits. Otherwise, set imm_expr to
8996 the entire 64 bit constant. */
8997 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
252b5132
RH
8998 {
8999 imm_expr.X_op = O_constant;
9000 offset_expr.X_op = O_constant;
9001 if (! target_big_endian)
9002 {
9003 imm_expr.X_add_number = bfd_getl32 (temp + 4);
9004 offset_expr.X_add_number = bfd_getl32 (temp);
9005 }
9006 else
9007 {
9008 imm_expr.X_add_number = bfd_getb32 (temp);
9009 offset_expr.X_add_number = bfd_getb32 (temp + 4);
9010 }
9011 if (offset_expr.X_add_number == 0)
9012 offset_expr.X_op = O_absent;
9013 }
9014 else if (sizeof (imm_expr.X_add_number) > 4)
9015 {
9016 imm_expr.X_op = O_constant;
9017 if (! target_big_endian)
9018 imm_expr.X_add_number = bfd_getl64 (temp);
9019 else
9020 imm_expr.X_add_number = bfd_getb64 (temp);
9021 }
9022 else
9023 {
9024 imm_expr.X_op = O_big;
9025 imm_expr.X_add_number = 4;
9026 if (! target_big_endian)
9027 {
9028 generic_bignum[0] = bfd_getl16 (temp);
9029 generic_bignum[1] = bfd_getl16 (temp + 2);
9030 generic_bignum[2] = bfd_getl16 (temp + 4);
9031 generic_bignum[3] = bfd_getl16 (temp + 6);
9032 }
9033 else
9034 {
9035 generic_bignum[0] = bfd_getb16 (temp + 6);
9036 generic_bignum[1] = bfd_getb16 (temp + 4);
9037 generic_bignum[2] = bfd_getb16 (temp + 2);
9038 generic_bignum[3] = bfd_getb16 (temp);
9039 }
9040 }
9041 }
9042 else
9043 {
9044 const char *newname;
9045 segT new_seg;
9046
9047 /* Switch to the right section. */
9048 seg = now_seg;
9049 subseg = now_subseg;
9050 switch (*args)
9051 {
9052 default: /* unused default case avoids warnings. */
9053 case 'L':
9054 newname = RDATA_SECTION_NAME;
3e722fb5 9055 if (g_switch_value >= 8)
252b5132
RH
9056 newname = ".lit8";
9057 break;
9058 case 'F':
3e722fb5 9059 newname = RDATA_SECTION_NAME;
252b5132
RH
9060 break;
9061 case 'l':
4d0d148d 9062 assert (g_switch_value >= 4);
252b5132
RH
9063 newname = ".lit4";
9064 break;
9065 }
9066 new_seg = subseg_new (newname, (subsegT) 0);
9067 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
9068 bfd_set_section_flags (stdoutput, new_seg,
9069 (SEC_ALLOC
9070 | SEC_LOAD
9071 | SEC_READONLY
9072 | SEC_DATA));
9073 frag_align (*args == 'l' ? 2 : 3, 0, 0);
9074 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
9075 && strcmp (TARGET_OS, "elf") != 0)
9076 record_alignment (new_seg, 4);
9077 else
9078 record_alignment (new_seg, *args == 'l' ? 2 : 3);
9079 if (seg == now_seg)
9080 as_bad (_("Can't use floating point insn in this section"));
9081
9082 /* Set the argument to the current address in the
9083 section. */
9084 offset_expr.X_op = O_symbol;
9085 offset_expr.X_add_symbol =
9086 symbol_new ("L0\001", now_seg,
9087 (valueT) frag_now_fix (), frag_now);
9088 offset_expr.X_add_number = 0;
9089
9090 /* Put the floating point number into the section. */
9091 p = frag_more ((int) length);
9092 memcpy (p, temp, length);
9093
9094 /* Switch back to the original section. */
9095 subseg_set (seg, subseg);
9096 }
9097 }
9098 continue;
9099
9100 case 'i': /* 16 bit unsigned immediate */
9101 case 'j': /* 16 bit signed immediate */
f6688943 9102 *imm_reloc = BFD_RELOC_LO16;
5e0116d5 9103 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0)
252b5132
RH
9104 {
9105 int more;
5e0116d5
RS
9106 offsetT minval, maxval;
9107
9108 more = (insn + 1 < &mips_opcodes[NUMOPCODES]
9109 && strcmp (insn->name, insn[1].name) == 0);
9110
9111 /* If the expression was written as an unsigned number,
9112 only treat it as signed if there are no more
9113 alternatives. */
9114 if (more
9115 && *args == 'j'
9116 && sizeof (imm_expr.X_add_number) <= 4
9117 && imm_expr.X_op == O_constant
9118 && imm_expr.X_add_number < 0
9119 && imm_expr.X_unsigned
9120 && HAVE_64BIT_GPRS)
9121 break;
9122
9123 /* For compatibility with older assemblers, we accept
9124 0x8000-0xffff as signed 16-bit numbers when only
9125 signed numbers are allowed. */
9126 if (*args == 'i')
9127 minval = 0, maxval = 0xffff;
9128 else if (more)
9129 minval = -0x8000, maxval = 0x7fff;
252b5132 9130 else
5e0116d5
RS
9131 minval = -0x8000, maxval = 0xffff;
9132
9133 if (imm_expr.X_op != O_constant
9134 || imm_expr.X_add_number < minval
9135 || imm_expr.X_add_number > maxval)
252b5132
RH
9136 {
9137 if (more)
9138 break;
2ae7e77b
AH
9139 if (imm_expr.X_op == O_constant
9140 || imm_expr.X_op == O_big)
5e0116d5 9141 as_bad (_("expression out of range"));
252b5132
RH
9142 }
9143 }
9144 s = expr_end;
9145 continue;
9146
9147 case 'o': /* 16 bit offset */
5e0116d5
RS
9148 /* Check whether there is only a single bracketed expression
9149 left. If so, it must be the base register and the
9150 constant must be zero. */
9151 if (*s == '(' && strchr (s + 1, '(') == 0)
9152 {
9153 offset_expr.X_op = O_constant;
9154 offset_expr.X_add_number = 0;
9155 continue;
9156 }
252b5132
RH
9157
9158 /* If this value won't fit into a 16 bit offset, then go
9159 find a macro that will generate the 32 bit offset
afdbd6d0 9160 code pattern. */
5e0116d5 9161 if (my_getSmallExpression (&offset_expr, offset_reloc, s) == 0
252b5132
RH
9162 && (offset_expr.X_op != O_constant
9163 || offset_expr.X_add_number >= 0x8000
afdbd6d0 9164 || offset_expr.X_add_number < -0x8000))
252b5132
RH
9165 break;
9166
252b5132
RH
9167 s = expr_end;
9168 continue;
9169
9170 case 'p': /* pc relative offset */
0b25d3e6 9171 *offset_reloc = BFD_RELOC_16_PCREL_S2;
252b5132
RH
9172 my_getExpression (&offset_expr, s);
9173 s = expr_end;
9174 continue;
9175
9176 case 'u': /* upper 16 bits */
5e0116d5
RS
9177 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0
9178 && imm_expr.X_op == O_constant
9179 && (imm_expr.X_add_number < 0
9180 || imm_expr.X_add_number >= 0x10000))
252b5132
RH
9181 as_bad (_("lui expression not in range 0..65535"));
9182 s = expr_end;
9183 continue;
9184
9185 case 'a': /* 26 bit address */
9186 my_getExpression (&offset_expr, s);
9187 s = expr_end;
f6688943 9188 *offset_reloc = BFD_RELOC_MIPS_JMP;
252b5132
RH
9189 continue;
9190
9191 case 'N': /* 3 bit branch condition code */
9192 case 'M': /* 3 bit compare condition code */
9193 if (strncmp (s, "$fcc", 4) != 0)
9194 break;
9195 s += 4;
9196 regno = 0;
9197 do
9198 {
9199 regno *= 10;
9200 regno += *s - '0';
9201 ++s;
9202 }
3882b010 9203 while (ISDIGIT (*s));
252b5132 9204 if (regno > 7)
30c378fd
CD
9205 as_bad (_("Invalid condition code register $fcc%d"), regno);
9206 if ((strcmp(str + strlen(str) - 3, ".ps") == 0
9207 || strcmp(str + strlen(str) - 5, "any2f") == 0
9208 || strcmp(str + strlen(str) - 5, "any2t") == 0)
9209 && (regno & 1) != 0)
9210 as_warn(_("Condition code register should be even for %s, was %d"),
9211 str, regno);
9212 if ((strcmp(str + strlen(str) - 5, "any4f") == 0
9213 || strcmp(str + strlen(str) - 5, "any4t") == 0)
9214 && (regno & 3) != 0)
9215 as_warn(_("Condition code register should be 0 or 4 for %s, was %d"),
9216 str, regno);
252b5132 9217 if (*args == 'N')
bf12938e 9218 INSERT_OPERAND (BCC, *ip, regno);
252b5132 9219 else
bf12938e 9220 INSERT_OPERAND (CCC, *ip, regno);
beae10d5 9221 continue;
252b5132 9222
156c2f8b
NC
9223 case 'H':
9224 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
9225 s += 2;
3882b010 9226 if (ISDIGIT (*s))
156c2f8b
NC
9227 {
9228 c = 0;
9229 do
9230 {
9231 c *= 10;
9232 c += *s - '0';
9233 ++s;
9234 }
3882b010 9235 while (ISDIGIT (*s));
156c2f8b
NC
9236 }
9237 else
9238 c = 8; /* Invalid sel value. */
9239
9240 if (c > 7)
9241 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
9242 ip->insn_opcode |= c;
9243 continue;
9244
60b63b72
RS
9245 case 'e':
9246 /* Must be at least one digit. */
9247 my_getExpression (&imm_expr, s);
9248 check_absolute_expr (ip, &imm_expr);
9249
9250 if ((unsigned long) imm_expr.X_add_number
9251 > (unsigned long) OP_MASK_VECBYTE)
9252 {
9253 as_bad (_("bad byte vector index (%ld)"),
9254 (long) imm_expr.X_add_number);
9255 imm_expr.X_add_number = 0;
9256 }
9257
bf12938e 9258 INSERT_OPERAND (VECBYTE, *ip, imm_expr.X_add_number);
60b63b72
RS
9259 imm_expr.X_op = O_absent;
9260 s = expr_end;
9261 continue;
9262
9263 case '%':
9264 my_getExpression (&imm_expr, s);
9265 check_absolute_expr (ip, &imm_expr);
9266
9267 if ((unsigned long) imm_expr.X_add_number
9268 > (unsigned long) OP_MASK_VECALIGN)
9269 {
9270 as_bad (_("bad byte vector index (%ld)"),
9271 (long) imm_expr.X_add_number);
9272 imm_expr.X_add_number = 0;
9273 }
9274
bf12938e 9275 INSERT_OPERAND (VECALIGN, *ip, imm_expr.X_add_number);
60b63b72
RS
9276 imm_expr.X_op = O_absent;
9277 s = expr_end;
9278 continue;
9279
252b5132
RH
9280 default:
9281 as_bad (_("bad char = '%c'\n"), *args);
9282 internalError ();
9283 }
9284 break;
9285 }
9286 /* Args don't match. */
9287 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
9288 !strcmp (insn->name, insn[1].name))
9289 {
9290 ++insn;
9291 s = argsStart;
268f6bed 9292 insn_error = _("illegal operands");
252b5132
RH
9293 continue;
9294 }
268f6bed
L
9295 if (save_c)
9296 *(--s) = save_c;
252b5132
RH
9297 insn_error = _("illegal operands");
9298 return;
9299 }
9300}
9301
9302/* This routine assembles an instruction into its binary format when
9303 assembling for the mips16. As a side effect, it sets one of the
9304 global variables imm_reloc or offset_reloc to the type of
9305 relocation to do if one of the operands is an address expression.
9306 It also sets mips16_small and mips16_ext if the user explicitly
9307 requested a small or extended instruction. */
9308
9309static void
17a2f251 9310mips16_ip (char *str, struct mips_cl_insn *ip)
252b5132
RH
9311{
9312 char *s;
9313 const char *args;
9314 struct mips_opcode *insn;
9315 char *argsstart;
9316 unsigned int regno;
9317 unsigned int lastregno = 0;
9318 char *s_reset;
d6f16593 9319 size_t i;
252b5132
RH
9320
9321 insn_error = NULL;
9322
b34976b6
AM
9323 mips16_small = FALSE;
9324 mips16_ext = FALSE;
252b5132 9325
3882b010 9326 for (s = str; ISLOWER (*s); ++s)
252b5132
RH
9327 ;
9328 switch (*s)
9329 {
9330 case '\0':
9331 break;
9332
9333 case ' ':
9334 *s++ = '\0';
9335 break;
9336
9337 case '.':
9338 if (s[1] == 't' && s[2] == ' ')
9339 {
9340 *s = '\0';
b34976b6 9341 mips16_small = TRUE;
252b5132
RH
9342 s += 3;
9343 break;
9344 }
9345 else if (s[1] == 'e' && s[2] == ' ')
9346 {
9347 *s = '\0';
b34976b6 9348 mips16_ext = TRUE;
252b5132
RH
9349 s += 3;
9350 break;
9351 }
9352 /* Fall through. */
9353 default:
9354 insn_error = _("unknown opcode");
9355 return;
9356 }
9357
9358 if (mips_opts.noautoextend && ! mips16_ext)
b34976b6 9359 mips16_small = TRUE;
252b5132
RH
9360
9361 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
9362 {
9363 insn_error = _("unrecognized opcode");
9364 return;
9365 }
9366
9367 argsstart = s;
9368 for (;;)
9369 {
9370 assert (strcmp (insn->name, str) == 0);
9371
1e915849 9372 create_insn (ip, insn);
252b5132 9373 imm_expr.X_op = O_absent;
f6688943
TS
9374 imm_reloc[0] = BFD_RELOC_UNUSED;
9375 imm_reloc[1] = BFD_RELOC_UNUSED;
9376 imm_reloc[2] = BFD_RELOC_UNUSED;
5f74bc13 9377 imm2_expr.X_op = O_absent;
252b5132 9378 offset_expr.X_op = O_absent;
f6688943
TS
9379 offset_reloc[0] = BFD_RELOC_UNUSED;
9380 offset_reloc[1] = BFD_RELOC_UNUSED;
9381 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
9382 for (args = insn->args; 1; ++args)
9383 {
9384 int c;
9385
9386 if (*s == ' ')
9387 ++s;
9388
9389 /* In this switch statement we call break if we did not find
9390 a match, continue if we did find a match, or return if we
9391 are done. */
9392
9393 c = *args;
9394 switch (c)
9395 {
9396 case '\0':
9397 if (*s == '\0')
9398 {
9399 /* Stuff the immediate value in now, if we can. */
9400 if (imm_expr.X_op == O_constant
f6688943 9401 && *imm_reloc > BFD_RELOC_UNUSED
252b5132
RH
9402 && insn->pinfo != INSN_MACRO)
9403 {
d6f16593
MR
9404 valueT tmp;
9405
9406 switch (*offset_reloc)
9407 {
9408 case BFD_RELOC_MIPS16_HI16_S:
9409 tmp = (imm_expr.X_add_number + 0x8000) >> 16;
9410 break;
9411
9412 case BFD_RELOC_MIPS16_HI16:
9413 tmp = imm_expr.X_add_number >> 16;
9414 break;
9415
9416 case BFD_RELOC_MIPS16_LO16:
9417 tmp = ((imm_expr.X_add_number + 0x8000) & 0xffff)
9418 - 0x8000;
9419 break;
9420
9421 case BFD_RELOC_UNUSED:
9422 tmp = imm_expr.X_add_number;
9423 break;
9424
9425 default:
9426 internalError ();
9427 }
9428 *offset_reloc = BFD_RELOC_UNUSED;
9429
c4e7957c 9430 mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
d6f16593 9431 tmp, TRUE, mips16_small,
252b5132
RH
9432 mips16_ext, &ip->insn_opcode,
9433 &ip->use_extend, &ip->extend);
9434 imm_expr.X_op = O_absent;
f6688943 9435 *imm_reloc = BFD_RELOC_UNUSED;
252b5132
RH
9436 }
9437
9438 return;
9439 }
9440 break;
9441
9442 case ',':
9443 if (*s++ == c)
9444 continue;
9445 s--;
9446 switch (*++args)
9447 {
9448 case 'v':
bf12938e 9449 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
252b5132
RH
9450 continue;
9451 case 'w':
bf12938e 9452 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
252b5132
RH
9453 continue;
9454 }
9455 break;
9456
9457 case '(':
9458 case ')':
9459 if (*s++ == c)
9460 continue;
9461 break;
9462
9463 case 'v':
9464 case 'w':
9465 if (s[0] != '$')
9466 {
9467 if (c == 'v')
bf12938e 9468 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
252b5132 9469 else
bf12938e 9470 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
252b5132
RH
9471 ++args;
9472 continue;
9473 }
9474 /* Fall through. */
9475 case 'x':
9476 case 'y':
9477 case 'z':
9478 case 'Z':
9479 case '0':
9480 case 'S':
9481 case 'R':
9482 case 'X':
9483 case 'Y':
9484 if (s[0] != '$')
9485 break;
9486 s_reset = s;
3882b010 9487 if (ISDIGIT (s[1]))
252b5132
RH
9488 {
9489 ++s;
9490 regno = 0;
9491 do
9492 {
9493 regno *= 10;
9494 regno += *s - '0';
9495 ++s;
9496 }
3882b010 9497 while (ISDIGIT (*s));
252b5132
RH
9498 if (regno > 31)
9499 {
9500 as_bad (_("invalid register number (%d)"), regno);
9501 regno = 2;
9502 }
9503 }
9504 else
9505 {
76db943d
TS
9506 if (s[1] == 'r' && s[2] == 'a')
9507 {
9508 s += 3;
9509 regno = RA;
9510 }
9511 else if (s[1] == 'f' && s[2] == 'p')
252b5132
RH
9512 {
9513 s += 3;
9514 regno = FP;
9515 }
9516 else if (s[1] == 's' && s[2] == 'p')
9517 {
9518 s += 3;
9519 regno = SP;
9520 }
9521 else if (s[1] == 'g' && s[2] == 'p')
9522 {
9523 s += 3;
9524 regno = GP;
9525 }
9526 else if (s[1] == 'a' && s[2] == 't')
9527 {
9528 s += 3;
9529 regno = AT;
9530 }
9531 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
9532 {
9533 s += 4;
9534 regno = KT0;
9535 }
9536 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
9537 {
9538 s += 4;
9539 regno = KT1;
9540 }
85b51719
TS
9541 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
9542 {
9543 s += 5;
9544 regno = ZERO;
9545 }
252b5132
RH
9546 else
9547 break;
9548 }
9549
9550 if (*s == ' ')
9551 ++s;
9552 if (args[1] != *s)
9553 {
9554 if (c == 'v' || c == 'w')
9555 {
9556 regno = mips16_to_32_reg_map[lastregno];
9557 s = s_reset;
f9419b05 9558 ++args;
252b5132
RH
9559 }
9560 }
9561
9562 switch (c)
9563 {
9564 case 'x':
9565 case 'y':
9566 case 'z':
9567 case 'v':
9568 case 'w':
9569 case 'Z':
9570 regno = mips32_to_16_reg_map[regno];
9571 break;
9572
9573 case '0':
9574 if (regno != 0)
9575 regno = ILLEGAL_REG;
9576 break;
9577
9578 case 'S':
9579 if (regno != SP)
9580 regno = ILLEGAL_REG;
9581 break;
9582
9583 case 'R':
9584 if (regno != RA)
9585 regno = ILLEGAL_REG;
9586 break;
9587
9588 case 'X':
9589 case 'Y':
9590 if (regno == AT && ! mips_opts.noat)
9591 as_warn (_("used $at without \".set noat\""));
9592 break;
9593
9594 default:
9595 internalError ();
9596 }
9597
9598 if (regno == ILLEGAL_REG)
9599 break;
9600
9601 switch (c)
9602 {
9603 case 'x':
9604 case 'v':
bf12938e 9605 MIPS16_INSERT_OPERAND (RX, *ip, regno);
252b5132
RH
9606 break;
9607 case 'y':
9608 case 'w':
bf12938e 9609 MIPS16_INSERT_OPERAND (RY, *ip, regno);
252b5132
RH
9610 break;
9611 case 'z':
bf12938e 9612 MIPS16_INSERT_OPERAND (RZ, *ip, regno);
252b5132
RH
9613 break;
9614 case 'Z':
bf12938e 9615 MIPS16_INSERT_OPERAND (MOVE32Z, *ip, regno);
252b5132
RH
9616 case '0':
9617 case 'S':
9618 case 'R':
9619 break;
9620 case 'X':
bf12938e 9621 MIPS16_INSERT_OPERAND (REGR32, *ip, regno);
252b5132
RH
9622 break;
9623 case 'Y':
9624 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
bf12938e 9625 MIPS16_INSERT_OPERAND (REG32R, *ip, regno);
252b5132
RH
9626 break;
9627 default:
9628 internalError ();
9629 }
9630
9631 lastregno = regno;
9632 continue;
9633
9634 case 'P':
9635 if (strncmp (s, "$pc", 3) == 0)
9636 {
9637 s += 3;
9638 continue;
9639 }
9640 break;
9641
252b5132
RH
9642 case '5':
9643 case 'H':
9644 case 'W':
9645 case 'D':
9646 case 'j':
252b5132
RH
9647 case 'V':
9648 case 'C':
9649 case 'U':
9650 case 'k':
9651 case 'K':
d6f16593
MR
9652 i = my_getSmallExpression (&imm_expr, imm_reloc, s);
9653 if (i > 0)
252b5132 9654 {
d6f16593 9655 if (imm_expr.X_op != O_constant)
252b5132 9656 {
b34976b6 9657 mips16_ext = TRUE;
b34976b6 9658 ip->use_extend = TRUE;
252b5132 9659 ip->extend = 0;
252b5132 9660 }
d6f16593
MR
9661 else
9662 {
9663 /* We need to relax this instruction. */
9664 *offset_reloc = *imm_reloc;
9665 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
9666 }
9667 s = expr_end;
9668 continue;
252b5132 9669 }
d6f16593
MR
9670 *imm_reloc = BFD_RELOC_UNUSED;
9671 /* Fall through. */
9672 case '<':
9673 case '>':
9674 case '[':
9675 case ']':
9676 case '4':
9677 case '8':
9678 my_getExpression (&imm_expr, s);
252b5132
RH
9679 if (imm_expr.X_op == O_register)
9680 {
9681 /* What we thought was an expression turned out to
9682 be a register. */
9683
9684 if (s[0] == '(' && args[1] == '(')
9685 {
9686 /* It looks like the expression was omitted
9687 before a register indirection, which means
9688 that the expression is implicitly zero. We
9689 still set up imm_expr, so that we handle
9690 explicit extensions correctly. */
9691 imm_expr.X_op = O_constant;
9692 imm_expr.X_add_number = 0;
f6688943 9693 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
9694 continue;
9695 }
9696
9697 break;
9698 }
9699
9700 /* We need to relax this instruction. */
f6688943 9701 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
9702 s = expr_end;
9703 continue;
9704
9705 case 'p':
9706 case 'q':
9707 case 'A':
9708 case 'B':
9709 case 'E':
9710 /* We use offset_reloc rather than imm_reloc for the PC
9711 relative operands. This lets macros with both
9712 immediate and address operands work correctly. */
9713 my_getExpression (&offset_expr, s);
9714
9715 if (offset_expr.X_op == O_register)
9716 break;
9717
9718 /* We need to relax this instruction. */
f6688943 9719 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
9720 s = expr_end;
9721 continue;
9722
9723 case '6': /* break code */
9724 my_getExpression (&imm_expr, s);
9725 check_absolute_expr (ip, &imm_expr);
9726 if ((unsigned long) imm_expr.X_add_number > 63)
bf12938e
RS
9727 as_warn (_("Invalid value for `%s' (%lu)"),
9728 ip->insn_mo->name,
9729 (unsigned long) imm_expr.X_add_number);
9730 MIPS16_INSERT_OPERAND (IMM6, *ip, imm_expr.X_add_number);
252b5132
RH
9731 imm_expr.X_op = O_absent;
9732 s = expr_end;
9733 continue;
9734
9735 case 'a': /* 26 bit address */
9736 my_getExpression (&offset_expr, s);
9737 s = expr_end;
f6688943 9738 *offset_reloc = BFD_RELOC_MIPS16_JMP;
252b5132
RH
9739 ip->insn_opcode <<= 16;
9740 continue;
9741
9742 case 'l': /* register list for entry macro */
9743 case 'L': /* register list for exit macro */
9744 {
9745 int mask;
9746
9747 if (c == 'l')
9748 mask = 0;
9749 else
9750 mask = 7 << 3;
9751 while (*s != '\0')
9752 {
9753 int freg, reg1, reg2;
9754
9755 while (*s == ' ' || *s == ',')
9756 ++s;
9757 if (*s != '$')
9758 {
9759 as_bad (_("can't parse register list"));
9760 break;
9761 }
9762 ++s;
9763 if (*s != 'f')
9764 freg = 0;
9765 else
9766 {
9767 freg = 1;
9768 ++s;
9769 }
9770 reg1 = 0;
3882b010 9771 while (ISDIGIT (*s))
252b5132
RH
9772 {
9773 reg1 *= 10;
9774 reg1 += *s - '0';
9775 ++s;
9776 }
9777 if (*s == ' ')
9778 ++s;
9779 if (*s != '-')
9780 reg2 = reg1;
9781 else
9782 {
9783 ++s;
9784 if (*s != '$')
9785 break;
9786 ++s;
9787 if (freg)
9788 {
9789 if (*s == 'f')
9790 ++s;
9791 else
9792 {
9793 as_bad (_("invalid register list"));
9794 break;
9795 }
9796 }
9797 reg2 = 0;
3882b010 9798 while (ISDIGIT (*s))
252b5132
RH
9799 {
9800 reg2 *= 10;
9801 reg2 += *s - '0';
9802 ++s;
9803 }
9804 }
9805 if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
9806 {
9807 mask &= ~ (7 << 3);
9808 mask |= 5 << 3;
9809 }
9810 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
9811 {
9812 mask &= ~ (7 << 3);
9813 mask |= 6 << 3;
9814 }
9815 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
9816 mask |= (reg2 - 3) << 3;
9817 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
9818 mask |= (reg2 - 15) << 1;
f9419b05 9819 else if (reg1 == RA && reg2 == RA)
252b5132
RH
9820 mask |= 1;
9821 else
9822 {
9823 as_bad (_("invalid register list"));
9824 break;
9825 }
9826 }
9827 /* The mask is filled in in the opcode table for the
9828 benefit of the disassembler. We remove it before
9829 applying the actual mask. */
9830 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
9831 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
9832 }
9833 continue;
9834
9835 case 'e': /* extend code */
9836 my_getExpression (&imm_expr, s);
9837 check_absolute_expr (ip, &imm_expr);
9838 if ((unsigned long) imm_expr.X_add_number > 0x7ff)
9839 {
9840 as_warn (_("Invalid value for `%s' (%lu)"),
9841 ip->insn_mo->name,
9842 (unsigned long) imm_expr.X_add_number);
9843 imm_expr.X_add_number &= 0x7ff;
9844 }
9845 ip->insn_opcode |= imm_expr.X_add_number;
9846 imm_expr.X_op = O_absent;
9847 s = expr_end;
9848 continue;
9849
9850 default:
9851 internalError ();
9852 }
9853 break;
9854 }
9855
9856 /* Args don't match. */
9857 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
9858 strcmp (insn->name, insn[1].name) == 0)
9859 {
9860 ++insn;
9861 s = argsstart;
9862 continue;
9863 }
9864
9865 insn_error = _("illegal operands");
9866
9867 return;
9868 }
9869}
9870
9871/* This structure holds information we know about a mips16 immediate
9872 argument type. */
9873
e972090a
NC
9874struct mips16_immed_operand
9875{
252b5132
RH
9876 /* The type code used in the argument string in the opcode table. */
9877 int type;
9878 /* The number of bits in the short form of the opcode. */
9879 int nbits;
9880 /* The number of bits in the extended form of the opcode. */
9881 int extbits;
9882 /* The amount by which the short form is shifted when it is used;
9883 for example, the sw instruction has a shift count of 2. */
9884 int shift;
9885 /* The amount by which the short form is shifted when it is stored
9886 into the instruction code. */
9887 int op_shift;
9888 /* Non-zero if the short form is unsigned. */
9889 int unsp;
9890 /* Non-zero if the extended form is unsigned. */
9891 int extu;
9892 /* Non-zero if the value is PC relative. */
9893 int pcrel;
9894};
9895
9896/* The mips16 immediate operand types. */
9897
9898static const struct mips16_immed_operand mips16_immed_operands[] =
9899{
9900 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9901 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9902 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9903 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9904 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
9905 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
9906 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
9907 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
9908 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
9909 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
9910 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
9911 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
9912 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
9913 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
9914 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
9915 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
9916 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9917 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9918 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
9919 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
9920 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
9921};
9922
9923#define MIPS16_NUM_IMMED \
9924 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
9925
9926/* Handle a mips16 instruction with an immediate value. This or's the
9927 small immediate value into *INSN. It sets *USE_EXTEND to indicate
9928 whether an extended value is needed; if one is needed, it sets
9929 *EXTEND to the value. The argument type is TYPE. The value is VAL.
9930 If SMALL is true, an unextended opcode was explicitly requested.
9931 If EXT is true, an extended opcode was explicitly requested. If
9932 WARN is true, warn if EXT does not match reality. */
9933
9934static void
17a2f251
TS
9935mips16_immed (char *file, unsigned int line, int type, offsetT val,
9936 bfd_boolean warn, bfd_boolean small, bfd_boolean ext,
9937 unsigned long *insn, bfd_boolean *use_extend,
9938 unsigned short *extend)
252b5132
RH
9939{
9940 register const struct mips16_immed_operand *op;
9941 int mintiny, maxtiny;
b34976b6 9942 bfd_boolean needext;
252b5132
RH
9943
9944 op = mips16_immed_operands;
9945 while (op->type != type)
9946 {
9947 ++op;
9948 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
9949 }
9950
9951 if (op->unsp)
9952 {
9953 if (type == '<' || type == '>' || type == '[' || type == ']')
9954 {
9955 mintiny = 1;
9956 maxtiny = 1 << op->nbits;
9957 }
9958 else
9959 {
9960 mintiny = 0;
9961 maxtiny = (1 << op->nbits) - 1;
9962 }
9963 }
9964 else
9965 {
9966 mintiny = - (1 << (op->nbits - 1));
9967 maxtiny = (1 << (op->nbits - 1)) - 1;
9968 }
9969
9970 /* Branch offsets have an implicit 0 in the lowest bit. */
9971 if (type == 'p' || type == 'q')
9972 val /= 2;
9973
9974 if ((val & ((1 << op->shift) - 1)) != 0
9975 || val < (mintiny << op->shift)
9976 || val > (maxtiny << op->shift))
b34976b6 9977 needext = TRUE;
252b5132 9978 else
b34976b6 9979 needext = FALSE;
252b5132
RH
9980
9981 if (warn && ext && ! needext)
beae10d5
KH
9982 as_warn_where (file, line,
9983 _("extended operand requested but not required"));
252b5132
RH
9984 if (small && needext)
9985 as_bad_where (file, line, _("invalid unextended operand value"));
9986
9987 if (small || (! ext && ! needext))
9988 {
9989 int insnval;
9990
b34976b6 9991 *use_extend = FALSE;
252b5132
RH
9992 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
9993 insnval <<= op->op_shift;
9994 *insn |= insnval;
9995 }
9996 else
9997 {
9998 long minext, maxext;
9999 int extval;
10000
10001 if (op->extu)
10002 {
10003 minext = 0;
10004 maxext = (1 << op->extbits) - 1;
10005 }
10006 else
10007 {
10008 minext = - (1 << (op->extbits - 1));
10009 maxext = (1 << (op->extbits - 1)) - 1;
10010 }
10011 if (val < minext || val > maxext)
10012 as_bad_where (file, line,
10013 _("operand value out of range for instruction"));
10014
b34976b6 10015 *use_extend = TRUE;
252b5132
RH
10016 if (op->extbits == 16)
10017 {
10018 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
10019 val &= 0x1f;
10020 }
10021 else if (op->extbits == 15)
10022 {
10023 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
10024 val &= 0xf;
10025 }
10026 else
10027 {
10028 extval = ((val & 0x1f) << 6) | (val & 0x20);
10029 val = 0;
10030 }
10031
10032 *extend = (unsigned short) extval;
10033 *insn |= val;
10034 }
10035}
10036\f
d6f16593 10037struct percent_op_match
ad8d3bb3 10038{
5e0116d5
RS
10039 const char *str;
10040 bfd_reloc_code_real_type reloc;
d6f16593
MR
10041};
10042
10043static const struct percent_op_match mips_percent_op[] =
ad8d3bb3 10044{
5e0116d5 10045 {"%lo", BFD_RELOC_LO16},
ad8d3bb3 10046#ifdef OBJ_ELF
5e0116d5
RS
10047 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
10048 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
10049 {"%call16", BFD_RELOC_MIPS_CALL16},
10050 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
10051 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
10052 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
10053 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
10054 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
10055 {"%got", BFD_RELOC_MIPS_GOT16},
10056 {"%gp_rel", BFD_RELOC_GPREL16},
10057 {"%half", BFD_RELOC_16},
10058 {"%highest", BFD_RELOC_MIPS_HIGHEST},
10059 {"%higher", BFD_RELOC_MIPS_HIGHER},
10060 {"%neg", BFD_RELOC_MIPS_SUB},
3f98094e
DJ
10061 {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
10062 {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
10063 {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
10064 {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
10065 {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
10066 {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
10067 {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
ad8d3bb3 10068#endif
5e0116d5 10069 {"%hi", BFD_RELOC_HI16_S}
ad8d3bb3
TS
10070};
10071
d6f16593
MR
10072static const struct percent_op_match mips16_percent_op[] =
10073{
10074 {"%lo", BFD_RELOC_MIPS16_LO16},
10075 {"%gprel", BFD_RELOC_MIPS16_GPREL},
10076 {"%hi", BFD_RELOC_MIPS16_HI16_S}
10077};
10078
252b5132 10079
5e0116d5
RS
10080/* Return true if *STR points to a relocation operator. When returning true,
10081 move *STR over the operator and store its relocation code in *RELOC.
10082 Leave both *STR and *RELOC alone when returning false. */
10083
10084static bfd_boolean
17a2f251 10085parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
252b5132 10086{
d6f16593
MR
10087 const struct percent_op_match *percent_op;
10088 size_t limit, i;
10089
10090 if (mips_opts.mips16)
10091 {
10092 percent_op = mips16_percent_op;
10093 limit = ARRAY_SIZE (mips16_percent_op);
10094 }
10095 else
10096 {
10097 percent_op = mips_percent_op;
10098 limit = ARRAY_SIZE (mips_percent_op);
10099 }
76b3015f 10100
d6f16593 10101 for (i = 0; i < limit; i++)
5e0116d5 10102 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
394f9b3a 10103 {
3f98094e
DJ
10104 int len = strlen (percent_op[i].str);
10105
10106 if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
10107 continue;
10108
5e0116d5
RS
10109 *str += strlen (percent_op[i].str);
10110 *reloc = percent_op[i].reloc;
394f9b3a 10111
5e0116d5
RS
10112 /* Check whether the output BFD supports this relocation.
10113 If not, issue an error and fall back on something safe. */
10114 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
394f9b3a 10115 {
5e0116d5
RS
10116 as_bad ("relocation %s isn't supported by the current ABI",
10117 percent_op[i].str);
01a3f561 10118 *reloc = BFD_RELOC_UNUSED;
394f9b3a 10119 }
5e0116d5 10120 return TRUE;
394f9b3a 10121 }
5e0116d5 10122 return FALSE;
394f9b3a 10123}
ad8d3bb3 10124
ad8d3bb3 10125
5e0116d5
RS
10126/* Parse string STR as a 16-bit relocatable operand. Store the
10127 expression in *EP and the relocations in the array starting
10128 at RELOC. Return the number of relocation operators used.
ad8d3bb3 10129
01a3f561 10130 On exit, EXPR_END points to the first character after the expression. */
ad8d3bb3 10131
5e0116d5 10132static size_t
17a2f251
TS
10133my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
10134 char *str)
ad8d3bb3 10135{
5e0116d5
RS
10136 bfd_reloc_code_real_type reversed_reloc[3];
10137 size_t reloc_index, i;
09b8f35a
RS
10138 int crux_depth, str_depth;
10139 char *crux;
5e0116d5
RS
10140
10141 /* Search for the start of the main expression, recoding relocations
09b8f35a
RS
10142 in REVERSED_RELOC. End the loop with CRUX pointing to the start
10143 of the main expression and with CRUX_DEPTH containing the number
10144 of open brackets at that point. */
10145 reloc_index = -1;
10146 str_depth = 0;
10147 do
fb1b3232 10148 {
09b8f35a
RS
10149 reloc_index++;
10150 crux = str;
10151 crux_depth = str_depth;
10152
10153 /* Skip over whitespace and brackets, keeping count of the number
10154 of brackets. */
10155 while (*str == ' ' || *str == '\t' || *str == '(')
10156 if (*str++ == '(')
10157 str_depth++;
5e0116d5 10158 }
09b8f35a
RS
10159 while (*str == '%'
10160 && reloc_index < (HAVE_NEWABI ? 3 : 1)
10161 && parse_relocation (&str, &reversed_reloc[reloc_index]));
ad8d3bb3 10162
09b8f35a 10163 my_getExpression (ep, crux);
5e0116d5 10164 str = expr_end;
394f9b3a 10165
5e0116d5 10166 /* Match every open bracket. */
09b8f35a 10167 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
5e0116d5 10168 if (*str++ == ')')
09b8f35a 10169 crux_depth--;
394f9b3a 10170
09b8f35a 10171 if (crux_depth > 0)
5e0116d5 10172 as_bad ("unclosed '('");
394f9b3a 10173
5e0116d5 10174 expr_end = str;
252b5132 10175
01a3f561 10176 if (reloc_index != 0)
64bdfcaf
RS
10177 {
10178 prev_reloc_op_frag = frag_now;
10179 for (i = 0; i < reloc_index; i++)
10180 reloc[i] = reversed_reloc[reloc_index - 1 - i];
10181 }
fb1b3232 10182
5e0116d5 10183 return reloc_index;
252b5132
RH
10184}
10185
10186static void
17a2f251 10187my_getExpression (expressionS *ep, char *str)
252b5132
RH
10188{
10189 char *save_in;
98aa84af 10190 valueT val;
252b5132
RH
10191
10192 save_in = input_line_pointer;
10193 input_line_pointer = str;
10194 expression (ep);
10195 expr_end = input_line_pointer;
10196 input_line_pointer = save_in;
10197
10198 /* If we are in mips16 mode, and this is an expression based on `.',
10199 then we bump the value of the symbol by 1 since that is how other
10200 text symbols are handled. We don't bother to handle complex
10201 expressions, just `.' plus or minus a constant. */
10202 if (mips_opts.mips16
10203 && ep->X_op == O_symbol
10204 && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
10205 && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
49309057
ILT
10206 && symbol_get_frag (ep->X_add_symbol) == frag_now
10207 && symbol_constant_p (ep->X_add_symbol)
98aa84af
AM
10208 && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
10209 S_SET_VALUE (ep->X_add_symbol, val + 1);
252b5132
RH
10210}
10211
10212/* Turn a string in input_line_pointer into a floating point constant
bc0d738a
NC
10213 of type TYPE, and store the appropriate bytes in *LITP. The number
10214 of LITTLENUMS emitted is stored in *SIZEP. An error message is
252b5132
RH
10215 returned, or NULL on OK. */
10216
10217char *
17a2f251 10218md_atof (int type, char *litP, int *sizeP)
252b5132
RH
10219{
10220 int prec;
10221 LITTLENUM_TYPE words[4];
10222 char *t;
10223 int i;
10224
10225 switch (type)
10226 {
10227 case 'f':
10228 prec = 2;
10229 break;
10230
10231 case 'd':
10232 prec = 4;
10233 break;
10234
10235 default:
10236 *sizeP = 0;
10237 return _("bad call to md_atof");
10238 }
10239
10240 t = atof_ieee (input_line_pointer, type, words);
10241 if (t)
10242 input_line_pointer = t;
10243
10244 *sizeP = prec * 2;
10245
10246 if (! target_big_endian)
10247 {
10248 for (i = prec - 1; i >= 0; i--)
10249 {
17a2f251 10250 md_number_to_chars (litP, words[i], 2);
252b5132
RH
10251 litP += 2;
10252 }
10253 }
10254 else
10255 {
10256 for (i = 0; i < prec; i++)
10257 {
17a2f251 10258 md_number_to_chars (litP, words[i], 2);
252b5132
RH
10259 litP += 2;
10260 }
10261 }
bdaaa2e1 10262
252b5132
RH
10263 return NULL;
10264}
10265
10266void
17a2f251 10267md_number_to_chars (char *buf, valueT val, int n)
252b5132
RH
10268{
10269 if (target_big_endian)
10270 number_to_chars_bigendian (buf, val, n);
10271 else
10272 number_to_chars_littleendian (buf, val, n);
10273}
10274\f
ae948b86 10275#ifdef OBJ_ELF
e013f690
TS
10276static int support_64bit_objects(void)
10277{
10278 const char **list, **l;
aa3d8fdf 10279 int yes;
e013f690
TS
10280
10281 list = bfd_target_list ();
10282 for (l = list; *l != NULL; l++)
10283#ifdef TE_TMIPS
10284 /* This is traditional mips */
10285 if (strcmp (*l, "elf64-tradbigmips") == 0
10286 || strcmp (*l, "elf64-tradlittlemips") == 0)
10287#else
10288 if (strcmp (*l, "elf64-bigmips") == 0
10289 || strcmp (*l, "elf64-littlemips") == 0)
10290#endif
10291 break;
aa3d8fdf 10292 yes = (*l != NULL);
e013f690 10293 free (list);
aa3d8fdf 10294 return yes;
e013f690 10295}
ae948b86 10296#endif /* OBJ_ELF */
e013f690 10297
78849248 10298const char *md_shortopts = "O::g::G:";
252b5132 10299
e972090a
NC
10300struct option md_longopts[] =
10301{
f9b4148d
CD
10302 /* Options which specify architecture. */
10303#define OPTION_ARCH_BASE (OPTION_MD_BASE)
10304#define OPTION_MARCH (OPTION_ARCH_BASE + 0)
10305 {"march", required_argument, NULL, OPTION_MARCH},
10306#define OPTION_MTUNE (OPTION_ARCH_BASE + 1)
10307 {"mtune", required_argument, NULL, OPTION_MTUNE},
10308#define OPTION_MIPS1 (OPTION_ARCH_BASE + 2)
252b5132
RH
10309 {"mips0", no_argument, NULL, OPTION_MIPS1},
10310 {"mips1", no_argument, NULL, OPTION_MIPS1},
f9b4148d 10311#define OPTION_MIPS2 (OPTION_ARCH_BASE + 3)
252b5132 10312 {"mips2", no_argument, NULL, OPTION_MIPS2},
f9b4148d 10313#define OPTION_MIPS3 (OPTION_ARCH_BASE + 4)
252b5132 10314 {"mips3", no_argument, NULL, OPTION_MIPS3},
f9b4148d 10315#define OPTION_MIPS4 (OPTION_ARCH_BASE + 5)
252b5132 10316 {"mips4", no_argument, NULL, OPTION_MIPS4},
f9b4148d 10317#define OPTION_MIPS5 (OPTION_ARCH_BASE + 6)
ae948b86 10318 {"mips5", no_argument, NULL, OPTION_MIPS5},
f9b4148d 10319#define OPTION_MIPS32 (OPTION_ARCH_BASE + 7)
ae948b86 10320 {"mips32", no_argument, NULL, OPTION_MIPS32},
f9b4148d 10321#define OPTION_MIPS64 (OPTION_ARCH_BASE + 8)
ae948b86 10322 {"mips64", no_argument, NULL, OPTION_MIPS64},
f9b4148d
CD
10323#define OPTION_MIPS32R2 (OPTION_ARCH_BASE + 9)
10324 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
5f74bc13
CD
10325#define OPTION_MIPS64R2 (OPTION_ARCH_BASE + 10)
10326 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
f9b4148d
CD
10327
10328 /* Options which specify Application Specific Extensions (ASEs). */
5f74bc13 10329#define OPTION_ASE_BASE (OPTION_ARCH_BASE + 11)
f9b4148d
CD
10330#define OPTION_MIPS16 (OPTION_ASE_BASE + 0)
10331 {"mips16", no_argument, NULL, OPTION_MIPS16},
10332#define OPTION_NO_MIPS16 (OPTION_ASE_BASE + 1)
10333 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
10334#define OPTION_MIPS3D (OPTION_ASE_BASE + 2)
10335 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
10336#define OPTION_NO_MIPS3D (OPTION_ASE_BASE + 3)
10337 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
10338#define OPTION_MDMX (OPTION_ASE_BASE + 4)
10339 {"mdmx", no_argument, NULL, OPTION_MDMX},
10340#define OPTION_NO_MDMX (OPTION_ASE_BASE + 5)
10341 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
74cd071d
CF
10342#define OPTION_DSP (OPTION_ASE_BASE + 6)
10343 {"mdsp", no_argument, NULL, OPTION_DSP},
10344#define OPTION_NO_DSP (OPTION_ASE_BASE + 7)
10345 {"mno-dsp", no_argument, NULL, OPTION_NO_DSP},
ef2e4d86
CF
10346#define OPTION_MT (OPTION_ASE_BASE + 8)
10347 {"mmt", no_argument, NULL, OPTION_MT},
10348#define OPTION_NO_MT (OPTION_ASE_BASE + 9)
10349 {"mno-mt", no_argument, NULL, OPTION_NO_MT},
f9b4148d
CD
10350
10351 /* Old-style architecture options. Don't add more of these. */
ef2e4d86 10352#define OPTION_COMPAT_ARCH_BASE (OPTION_ASE_BASE + 10)
f9b4148d
CD
10353#define OPTION_M4650 (OPTION_COMPAT_ARCH_BASE + 0)
10354 {"m4650", no_argument, NULL, OPTION_M4650},
10355#define OPTION_NO_M4650 (OPTION_COMPAT_ARCH_BASE + 1)
10356 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
10357#define OPTION_M4010 (OPTION_COMPAT_ARCH_BASE + 2)
10358 {"m4010", no_argument, NULL, OPTION_M4010},
10359#define OPTION_NO_M4010 (OPTION_COMPAT_ARCH_BASE + 3)
10360 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
10361#define OPTION_M4100 (OPTION_COMPAT_ARCH_BASE + 4)
10362 {"m4100", no_argument, NULL, OPTION_M4100},
10363#define OPTION_NO_M4100 (OPTION_COMPAT_ARCH_BASE + 5)
10364 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
10365#define OPTION_M3900 (OPTION_COMPAT_ARCH_BASE + 6)
10366 {"m3900", no_argument, NULL, OPTION_M3900},
10367#define OPTION_NO_M3900 (OPTION_COMPAT_ARCH_BASE + 7)
10368 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
10369
10370 /* Options which enable bug fixes. */
10371#define OPTION_FIX_BASE (OPTION_COMPAT_ARCH_BASE + 8)
10372#define OPTION_M7000_HILO_FIX (OPTION_FIX_BASE + 0)
10373 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
10374#define OPTION_MNO_7000_HILO_FIX (OPTION_FIX_BASE + 1)
10375 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10376 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
d766e8ec
RS
10377#define OPTION_FIX_VR4120 (OPTION_FIX_BASE + 2)
10378#define OPTION_NO_FIX_VR4120 (OPTION_FIX_BASE + 3)
10379 {"mfix-vr4120", no_argument, NULL, OPTION_FIX_VR4120},
10380 {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
7d8e00cf
RS
10381#define OPTION_FIX_VR4130 (OPTION_FIX_BASE + 4)
10382#define OPTION_NO_FIX_VR4130 (OPTION_FIX_BASE + 5)
10383 {"mfix-vr4130", no_argument, NULL, OPTION_FIX_VR4130},
10384 {"mno-fix-vr4130", no_argument, NULL, OPTION_NO_FIX_VR4130},
f9b4148d
CD
10385
10386 /* Miscellaneous options. */
7d8e00cf 10387#define OPTION_MISC_BASE (OPTION_FIX_BASE + 6)
1ffcab4b 10388#define OPTION_TRAP (OPTION_MISC_BASE + 0)
252b5132
RH
10389 {"trap", no_argument, NULL, OPTION_TRAP},
10390 {"no-break", no_argument, NULL, OPTION_TRAP},
1ffcab4b 10391#define OPTION_BREAK (OPTION_MISC_BASE + 1)
252b5132
RH
10392 {"break", no_argument, NULL, OPTION_BREAK},
10393 {"no-trap", no_argument, NULL, OPTION_BREAK},
1ffcab4b 10394#define OPTION_EB (OPTION_MISC_BASE + 2)
252b5132 10395 {"EB", no_argument, NULL, OPTION_EB},
1ffcab4b 10396#define OPTION_EL (OPTION_MISC_BASE + 3)
252b5132 10397 {"EL", no_argument, NULL, OPTION_EL},
1ffcab4b 10398#define OPTION_FP32 (OPTION_MISC_BASE + 4)
ae948b86 10399 {"mfp32", no_argument, NULL, OPTION_FP32},
1ffcab4b 10400#define OPTION_GP32 (OPTION_MISC_BASE + 5)
c97ef257 10401 {"mgp32", no_argument, NULL, OPTION_GP32},
1ffcab4b 10402#define OPTION_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 6)
119d663a 10403 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
1ffcab4b 10404#define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 7)
119d663a 10405 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
1ffcab4b 10406#define OPTION_FP64 (OPTION_MISC_BASE + 8)
316f5878 10407 {"mfp64", no_argument, NULL, OPTION_FP64},
1ffcab4b 10408#define OPTION_GP64 (OPTION_MISC_BASE + 9)
ae948b86 10409 {"mgp64", no_argument, NULL, OPTION_GP64},
1ffcab4b
CD
10410#define OPTION_RELAX_BRANCH (OPTION_MISC_BASE + 10)
10411#define OPTION_NO_RELAX_BRANCH (OPTION_MISC_BASE + 11)
4a6a3df4
AO
10412 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
10413 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
aa6975fb
ILT
10414#define OPTION_MSHARED (OPTION_MISC_BASE + 12)
10415#define OPTION_MNO_SHARED (OPTION_MISC_BASE + 13)
10416 {"mshared", no_argument, NULL, OPTION_MSHARED},
10417 {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
aed1a261
RS
10418#define OPTION_MSYM32 (OPTION_MISC_BASE + 14)
10419#define OPTION_MNO_SYM32 (OPTION_MISC_BASE + 15)
10420 {"msym32", no_argument, NULL, OPTION_MSYM32},
10421 {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
f9b4148d
CD
10422
10423 /* ELF-specific options. */
156c2f8b 10424#ifdef OBJ_ELF
aed1a261 10425#define OPTION_ELF_BASE (OPTION_MISC_BASE + 16)
156c2f8b 10426#define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
156c2f8b
NC
10427 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
10428 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
ae948b86 10429#define OPTION_NON_SHARED (OPTION_ELF_BASE + 1)
156c2f8b 10430 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
ae948b86 10431#define OPTION_XGOT (OPTION_ELF_BASE + 2)
156c2f8b 10432 {"xgot", no_argument, NULL, OPTION_XGOT},
ae948b86
TS
10433#define OPTION_MABI (OPTION_ELF_BASE + 3)
10434 {"mabi", required_argument, NULL, OPTION_MABI},
10435#define OPTION_32 (OPTION_ELF_BASE + 4)
156c2f8b 10436 {"32", no_argument, NULL, OPTION_32},
ae948b86 10437#define OPTION_N32 (OPTION_ELF_BASE + 5)
e013f690 10438 {"n32", no_argument, NULL, OPTION_N32},
ae948b86 10439#define OPTION_64 (OPTION_ELF_BASE + 6)
156c2f8b 10440 {"64", no_argument, NULL, OPTION_64},
ecb4347a
DJ
10441#define OPTION_MDEBUG (OPTION_ELF_BASE + 7)
10442 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
10443#define OPTION_NO_MDEBUG (OPTION_ELF_BASE + 8)
10444 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
dcd410fe
RO
10445#define OPTION_PDR (OPTION_ELF_BASE + 9)
10446 {"mpdr", no_argument, NULL, OPTION_PDR},
10447#define OPTION_NO_PDR (OPTION_ELF_BASE + 10)
10448 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
ae948b86 10449#endif /* OBJ_ELF */
f9b4148d 10450
252b5132
RH
10451 {NULL, no_argument, NULL, 0}
10452};
156c2f8b 10453size_t md_longopts_size = sizeof (md_longopts);
252b5132 10454
316f5878
RS
10455/* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
10456 NEW_VALUE. Warn if another value was already specified. Note:
10457 we have to defer parsing the -march and -mtune arguments in order
10458 to handle 'from-abi' correctly, since the ABI might be specified
10459 in a later argument. */
10460
10461static void
17a2f251 10462mips_set_option_string (const char **string_ptr, const char *new_value)
316f5878
RS
10463{
10464 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
10465 as_warn (_("A different %s was already specified, is now %s"),
10466 string_ptr == &mips_arch_string ? "-march" : "-mtune",
10467 new_value);
10468
10469 *string_ptr = new_value;
10470}
10471
252b5132 10472int
17a2f251 10473md_parse_option (int c, char *arg)
252b5132
RH
10474{
10475 switch (c)
10476 {
119d663a
NC
10477 case OPTION_CONSTRUCT_FLOATS:
10478 mips_disable_float_construction = 0;
10479 break;
bdaaa2e1 10480
119d663a
NC
10481 case OPTION_NO_CONSTRUCT_FLOATS:
10482 mips_disable_float_construction = 1;
10483 break;
bdaaa2e1 10484
252b5132
RH
10485 case OPTION_TRAP:
10486 mips_trap = 1;
10487 break;
10488
10489 case OPTION_BREAK:
10490 mips_trap = 0;
10491 break;
10492
10493 case OPTION_EB:
10494 target_big_endian = 1;
10495 break;
10496
10497 case OPTION_EL:
10498 target_big_endian = 0;
10499 break;
10500
10501 case 'O':
10502 if (arg && arg[1] == '0')
10503 mips_optimize = 1;
10504 else
10505 mips_optimize = 2;
10506 break;
10507
10508 case 'g':
10509 if (arg == NULL)
10510 mips_debug = 2;
10511 else
10512 mips_debug = atoi (arg);
10513 /* When the MIPS assembler sees -g or -g2, it does not do
10514 optimizations which limit full symbolic debugging. We take
10515 that to be equivalent to -O0. */
10516 if (mips_debug == 2)
10517 mips_optimize = 1;
10518 break;
10519
10520 case OPTION_MIPS1:
316f5878 10521 file_mips_isa = ISA_MIPS1;
252b5132
RH
10522 break;
10523
10524 case OPTION_MIPS2:
316f5878 10525 file_mips_isa = ISA_MIPS2;
252b5132
RH
10526 break;
10527
10528 case OPTION_MIPS3:
316f5878 10529 file_mips_isa = ISA_MIPS3;
252b5132
RH
10530 break;
10531
10532 case OPTION_MIPS4:
316f5878 10533 file_mips_isa = ISA_MIPS4;
e7af610e
NC
10534 break;
10535
84ea6cf2 10536 case OPTION_MIPS5:
316f5878 10537 file_mips_isa = ISA_MIPS5;
84ea6cf2
NC
10538 break;
10539
e7af610e 10540 case OPTION_MIPS32:
316f5878 10541 file_mips_isa = ISA_MIPS32;
252b5132
RH
10542 break;
10543
af7ee8bf
CD
10544 case OPTION_MIPS32R2:
10545 file_mips_isa = ISA_MIPS32R2;
10546 break;
10547
5f74bc13
CD
10548 case OPTION_MIPS64R2:
10549 file_mips_isa = ISA_MIPS64R2;
10550 break;
10551
84ea6cf2 10552 case OPTION_MIPS64:
316f5878 10553 file_mips_isa = ISA_MIPS64;
84ea6cf2
NC
10554 break;
10555
ec68c924 10556 case OPTION_MTUNE:
316f5878
RS
10557 mips_set_option_string (&mips_tune_string, arg);
10558 break;
ec68c924 10559
316f5878
RS
10560 case OPTION_MARCH:
10561 mips_set_option_string (&mips_arch_string, arg);
252b5132
RH
10562 break;
10563
10564 case OPTION_M4650:
316f5878
RS
10565 mips_set_option_string (&mips_arch_string, "4650");
10566 mips_set_option_string (&mips_tune_string, "4650");
252b5132
RH
10567 break;
10568
10569 case OPTION_NO_M4650:
10570 break;
10571
10572 case OPTION_M4010:
316f5878
RS
10573 mips_set_option_string (&mips_arch_string, "4010");
10574 mips_set_option_string (&mips_tune_string, "4010");
252b5132
RH
10575 break;
10576
10577 case OPTION_NO_M4010:
10578 break;
10579
10580 case OPTION_M4100:
316f5878
RS
10581 mips_set_option_string (&mips_arch_string, "4100");
10582 mips_set_option_string (&mips_tune_string, "4100");
252b5132
RH
10583 break;
10584
10585 case OPTION_NO_M4100:
10586 break;
10587
252b5132 10588 case OPTION_M3900:
316f5878
RS
10589 mips_set_option_string (&mips_arch_string, "3900");
10590 mips_set_option_string (&mips_tune_string, "3900");
252b5132 10591 break;
bdaaa2e1 10592
252b5132
RH
10593 case OPTION_NO_M3900:
10594 break;
10595
deec1734
CD
10596 case OPTION_MDMX:
10597 mips_opts.ase_mdmx = 1;
10598 break;
10599
10600 case OPTION_NO_MDMX:
10601 mips_opts.ase_mdmx = 0;
10602 break;
10603
74cd071d
CF
10604 case OPTION_DSP:
10605 mips_opts.ase_dsp = 1;
10606 break;
10607
10608 case OPTION_NO_DSP:
10609 mips_opts.ase_dsp = 0;
10610 break;
10611
ef2e4d86
CF
10612 case OPTION_MT:
10613 mips_opts.ase_mt = 1;
10614 break;
10615
10616 case OPTION_NO_MT:
10617 mips_opts.ase_mt = 0;
10618 break;
10619
252b5132
RH
10620 case OPTION_MIPS16:
10621 mips_opts.mips16 = 1;
7d10b47d 10622 mips_no_prev_insn ();
252b5132
RH
10623 break;
10624
10625 case OPTION_NO_MIPS16:
10626 mips_opts.mips16 = 0;
7d10b47d 10627 mips_no_prev_insn ();
252b5132
RH
10628 break;
10629
1f25f5d3
CD
10630 case OPTION_MIPS3D:
10631 mips_opts.ase_mips3d = 1;
10632 break;
10633
10634 case OPTION_NO_MIPS3D:
10635 mips_opts.ase_mips3d = 0;
10636 break;
10637
d766e8ec
RS
10638 case OPTION_FIX_VR4120:
10639 mips_fix_vr4120 = 1;
60b63b72
RS
10640 break;
10641
d766e8ec
RS
10642 case OPTION_NO_FIX_VR4120:
10643 mips_fix_vr4120 = 0;
60b63b72
RS
10644 break;
10645
7d8e00cf
RS
10646 case OPTION_FIX_VR4130:
10647 mips_fix_vr4130 = 1;
10648 break;
10649
10650 case OPTION_NO_FIX_VR4130:
10651 mips_fix_vr4130 = 0;
10652 break;
10653
4a6a3df4
AO
10654 case OPTION_RELAX_BRANCH:
10655 mips_relax_branch = 1;
10656 break;
10657
10658 case OPTION_NO_RELAX_BRANCH:
10659 mips_relax_branch = 0;
10660 break;
10661
aa6975fb
ILT
10662 case OPTION_MSHARED:
10663 mips_in_shared = TRUE;
10664 break;
10665
10666 case OPTION_MNO_SHARED:
10667 mips_in_shared = FALSE;
10668 break;
10669
aed1a261
RS
10670 case OPTION_MSYM32:
10671 mips_opts.sym32 = TRUE;
10672 break;
10673
10674 case OPTION_MNO_SYM32:
10675 mips_opts.sym32 = FALSE;
10676 break;
10677
0f074f60 10678#ifdef OBJ_ELF
252b5132
RH
10679 /* When generating ELF code, we permit -KPIC and -call_shared to
10680 select SVR4_PIC, and -non_shared to select no PIC. This is
10681 intended to be compatible with Irix 5. */
10682 case OPTION_CALL_SHARED:
10683 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10684 {
10685 as_bad (_("-call_shared is supported only for ELF format"));
10686 return 0;
10687 }
10688 mips_pic = SVR4_PIC;
143d77c5 10689 mips_abicalls = TRUE;
252b5132
RH
10690 if (g_switch_seen && g_switch_value != 0)
10691 {
10692 as_bad (_("-G may not be used with SVR4 PIC code"));
10693 return 0;
10694 }
10695 g_switch_value = 0;
10696 break;
10697
10698 case OPTION_NON_SHARED:
10699 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10700 {
10701 as_bad (_("-non_shared is supported only for ELF format"));
10702 return 0;
10703 }
10704 mips_pic = NO_PIC;
143d77c5 10705 mips_abicalls = FALSE;
252b5132
RH
10706 break;
10707
10708 /* The -xgot option tells the assembler to use 32 offsets when
10709 accessing the got in SVR4_PIC mode. It is for Irix
10710 compatibility. */
10711 case OPTION_XGOT:
10712 mips_big_got = 1;
10713 break;
0f074f60 10714#endif /* OBJ_ELF */
252b5132
RH
10715
10716 case 'G':
6caf9ef4
TS
10717 g_switch_value = atoi (arg);
10718 g_switch_seen = 1;
10719 if (mips_pic == SVR4_PIC && g_switch_value != 0)
252b5132 10720 {
3e722fb5 10721 as_bad (_("-G may not be used with SVR4 PIC code"));
252b5132
RH
10722 return 0;
10723 }
252b5132
RH
10724 break;
10725
0f074f60 10726#ifdef OBJ_ELF
34ba82a8
TS
10727 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
10728 and -mabi=64. */
252b5132 10729 case OPTION_32:
34ba82a8
TS
10730 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10731 {
10732 as_bad (_("-32 is supported for ELF format only"));
10733 return 0;
10734 }
316f5878 10735 mips_abi = O32_ABI;
252b5132
RH
10736 break;
10737
e013f690 10738 case OPTION_N32:
34ba82a8
TS
10739 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10740 {
10741 as_bad (_("-n32 is supported for ELF format only"));
10742 return 0;
10743 }
316f5878 10744 mips_abi = N32_ABI;
e013f690 10745 break;
252b5132 10746
e013f690 10747 case OPTION_64:
34ba82a8
TS
10748 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10749 {
10750 as_bad (_("-64 is supported for ELF format only"));
10751 return 0;
10752 }
316f5878 10753 mips_abi = N64_ABI;
e013f690
TS
10754 if (! support_64bit_objects())
10755 as_fatal (_("No compiled in support for 64 bit object file format"));
252b5132 10756 break;
ae948b86 10757#endif /* OBJ_ELF */
252b5132 10758
c97ef257 10759 case OPTION_GP32:
a325df1d 10760 file_mips_gp32 = 1;
c97ef257
AH
10761 break;
10762
10763 case OPTION_GP64:
a325df1d 10764 file_mips_gp32 = 0;
c97ef257 10765 break;
252b5132 10766
ca4e0257 10767 case OPTION_FP32:
a325df1d 10768 file_mips_fp32 = 1;
316f5878
RS
10769 break;
10770
10771 case OPTION_FP64:
10772 file_mips_fp32 = 0;
ca4e0257
RS
10773 break;
10774
ae948b86 10775#ifdef OBJ_ELF
252b5132 10776 case OPTION_MABI:
34ba82a8
TS
10777 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10778 {
10779 as_bad (_("-mabi is supported for ELF format only"));
10780 return 0;
10781 }
e013f690 10782 if (strcmp (arg, "32") == 0)
316f5878 10783 mips_abi = O32_ABI;
e013f690 10784 else if (strcmp (arg, "o64") == 0)
316f5878 10785 mips_abi = O64_ABI;
e013f690 10786 else if (strcmp (arg, "n32") == 0)
316f5878 10787 mips_abi = N32_ABI;
e013f690
TS
10788 else if (strcmp (arg, "64") == 0)
10789 {
316f5878 10790 mips_abi = N64_ABI;
e013f690
TS
10791 if (! support_64bit_objects())
10792 as_fatal (_("No compiled in support for 64 bit object file "
10793 "format"));
10794 }
10795 else if (strcmp (arg, "eabi") == 0)
316f5878 10796 mips_abi = EABI_ABI;
e013f690 10797 else
da0e507f
TS
10798 {
10799 as_fatal (_("invalid abi -mabi=%s"), arg);
10800 return 0;
10801 }
252b5132 10802 break;
e013f690 10803#endif /* OBJ_ELF */
252b5132 10804
6b76fefe 10805 case OPTION_M7000_HILO_FIX:
b34976b6 10806 mips_7000_hilo_fix = TRUE;
6b76fefe
CM
10807 break;
10808
9ee72ff1 10809 case OPTION_MNO_7000_HILO_FIX:
b34976b6 10810 mips_7000_hilo_fix = FALSE;
6b76fefe
CM
10811 break;
10812
ecb4347a
DJ
10813#ifdef OBJ_ELF
10814 case OPTION_MDEBUG:
b34976b6 10815 mips_flag_mdebug = TRUE;
ecb4347a
DJ
10816 break;
10817
10818 case OPTION_NO_MDEBUG:
b34976b6 10819 mips_flag_mdebug = FALSE;
ecb4347a 10820 break;
dcd410fe
RO
10821
10822 case OPTION_PDR:
10823 mips_flag_pdr = TRUE;
10824 break;
10825
10826 case OPTION_NO_PDR:
10827 mips_flag_pdr = FALSE;
10828 break;
ecb4347a
DJ
10829#endif /* OBJ_ELF */
10830
252b5132
RH
10831 default:
10832 return 0;
10833 }
10834
10835 return 1;
10836}
316f5878
RS
10837\f
10838/* Set up globals to generate code for the ISA or processor
10839 described by INFO. */
252b5132 10840
252b5132 10841static void
17a2f251 10842mips_set_architecture (const struct mips_cpu_info *info)
252b5132 10843{
316f5878 10844 if (info != 0)
252b5132 10845 {
fef14a42
TS
10846 file_mips_arch = info->cpu;
10847 mips_opts.arch = info->cpu;
316f5878 10848 mips_opts.isa = info->isa;
252b5132 10849 }
252b5132
RH
10850}
10851
252b5132 10852
316f5878 10853/* Likewise for tuning. */
252b5132 10854
316f5878 10855static void
17a2f251 10856mips_set_tune (const struct mips_cpu_info *info)
316f5878
RS
10857{
10858 if (info != 0)
fef14a42 10859 mips_tune = info->cpu;
316f5878 10860}
80cc45a5 10861
34ba82a8 10862
252b5132 10863void
17a2f251 10864mips_after_parse_args (void)
e9670677 10865{
fef14a42
TS
10866 const struct mips_cpu_info *arch_info = 0;
10867 const struct mips_cpu_info *tune_info = 0;
10868
e9670677 10869 /* GP relative stuff not working for PE */
6caf9ef4 10870 if (strncmp (TARGET_OS, "pe", 2) == 0)
e9670677 10871 {
6caf9ef4 10872 if (g_switch_seen && g_switch_value != 0)
e9670677
MR
10873 as_bad (_("-G not supported in this configuration."));
10874 g_switch_value = 0;
10875 }
10876
cac012d6
AO
10877 if (mips_abi == NO_ABI)
10878 mips_abi = MIPS_DEFAULT_ABI;
10879
22923709
RS
10880 /* The following code determines the architecture and register size.
10881 Similar code was added to GCC 3.3 (see override_options() in
10882 config/mips/mips.c). The GAS and GCC code should be kept in sync
10883 as much as possible. */
e9670677 10884
316f5878 10885 if (mips_arch_string != 0)
fef14a42 10886 arch_info = mips_parse_cpu ("-march", mips_arch_string);
e9670677 10887
316f5878 10888 if (file_mips_isa != ISA_UNKNOWN)
e9670677 10889 {
316f5878 10890 /* Handle -mipsN. At this point, file_mips_isa contains the
fef14a42 10891 ISA level specified by -mipsN, while arch_info->isa contains
316f5878 10892 the -march selection (if any). */
fef14a42 10893 if (arch_info != 0)
e9670677 10894 {
316f5878
RS
10895 /* -march takes precedence over -mipsN, since it is more descriptive.
10896 There's no harm in specifying both as long as the ISA levels
10897 are the same. */
fef14a42 10898 if (file_mips_isa != arch_info->isa)
316f5878
RS
10899 as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
10900 mips_cpu_info_from_isa (file_mips_isa)->name,
fef14a42 10901 mips_cpu_info_from_isa (arch_info->isa)->name);
e9670677 10902 }
316f5878 10903 else
fef14a42 10904 arch_info = mips_cpu_info_from_isa (file_mips_isa);
e9670677
MR
10905 }
10906
fef14a42
TS
10907 if (arch_info == 0)
10908 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
e9670677 10909
fef14a42 10910 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
316f5878 10911 as_bad ("-march=%s is not compatible with the selected ABI",
fef14a42
TS
10912 arch_info->name);
10913
10914 mips_set_architecture (arch_info);
10915
10916 /* Optimize for file_mips_arch, unless -mtune selects a different processor. */
10917 if (mips_tune_string != 0)
10918 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
e9670677 10919
fef14a42
TS
10920 if (tune_info == 0)
10921 mips_set_tune (arch_info);
10922 else
10923 mips_set_tune (tune_info);
e9670677 10924
316f5878 10925 if (file_mips_gp32 >= 0)
e9670677 10926 {
316f5878
RS
10927 /* The user specified the size of the integer registers. Make sure
10928 it agrees with the ABI and ISA. */
10929 if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
10930 as_bad (_("-mgp64 used with a 32-bit processor"));
10931 else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
10932 as_bad (_("-mgp32 used with a 64-bit ABI"));
10933 else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
10934 as_bad (_("-mgp64 used with a 32-bit ABI"));
e9670677
MR
10935 }
10936 else
10937 {
316f5878
RS
10938 /* Infer the integer register size from the ABI and processor.
10939 Restrict ourselves to 32-bit registers if that's all the
10940 processor has, or if the ABI cannot handle 64-bit registers. */
10941 file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
10942 || !ISA_HAS_64BIT_REGS (mips_opts.isa));
e9670677
MR
10943 }
10944
316f5878
RS
10945 /* ??? GAS treats single-float processors as though they had 64-bit
10946 float registers (although it complains when double-precision
10947 instructions are used). As things stand, saying they have 32-bit
10948 registers would lead to spurious "register must be even" messages.
10949 So here we assume float registers are always the same size as
10950 integer ones, unless the user says otherwise. */
10951 if (file_mips_fp32 < 0)
10952 file_mips_fp32 = file_mips_gp32;
e9670677 10953
316f5878 10954 /* End of GCC-shared inference code. */
e9670677 10955
17a2f251
TS
10956 /* This flag is set when we have a 64-bit capable CPU but use only
10957 32-bit wide registers. Note that EABI does not use it. */
10958 if (ISA_HAS_64BIT_REGS (mips_opts.isa)
10959 && ((mips_abi == NO_ABI && file_mips_gp32 == 1)
10960 || mips_abi == O32_ABI))
316f5878 10961 mips_32bitmode = 1;
e9670677
MR
10962
10963 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
10964 as_bad (_("trap exception not supported at ISA 1"));
10965
e9670677
MR
10966 /* If the selected architecture includes support for ASEs, enable
10967 generation of code for them. */
a4672219 10968 if (mips_opts.mips16 == -1)
fef14a42 10969 mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_arch)) ? 1 : 0;
ffdefa66 10970 if (mips_opts.ase_mips3d == -1)
fef14a42 10971 mips_opts.ase_mips3d = (CPU_HAS_MIPS3D (file_mips_arch)) ? 1 : 0;
ffdefa66 10972 if (mips_opts.ase_mdmx == -1)
fef14a42 10973 mips_opts.ase_mdmx = (CPU_HAS_MDMX (file_mips_arch)) ? 1 : 0;
74cd071d
CF
10974 if (mips_opts.ase_dsp == -1)
10975 mips_opts.ase_dsp = (CPU_HAS_DSP (file_mips_arch)) ? 1 : 0;
ef2e4d86
CF
10976 if (mips_opts.ase_mt == -1)
10977 mips_opts.ase_mt = (CPU_HAS_MT (file_mips_arch)) ? 1 : 0;
e9670677 10978
e9670677 10979 file_mips_isa = mips_opts.isa;
a4672219 10980 file_ase_mips16 = mips_opts.mips16;
e9670677
MR
10981 file_ase_mips3d = mips_opts.ase_mips3d;
10982 file_ase_mdmx = mips_opts.ase_mdmx;
74cd071d 10983 file_ase_dsp = mips_opts.ase_dsp;
ef2e4d86 10984 file_ase_mt = mips_opts.ase_mt;
e9670677
MR
10985 mips_opts.gp32 = file_mips_gp32;
10986 mips_opts.fp32 = file_mips_fp32;
10987
ecb4347a
DJ
10988 if (mips_flag_mdebug < 0)
10989 {
10990#ifdef OBJ_MAYBE_ECOFF
10991 if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
10992 mips_flag_mdebug = 1;
10993 else
10994#endif /* OBJ_MAYBE_ECOFF */
10995 mips_flag_mdebug = 0;
10996 }
e9670677
MR
10997}
10998\f
10999void
17a2f251 11000mips_init_after_args (void)
252b5132
RH
11001{
11002 /* initialize opcodes */
11003 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
beae10d5 11004 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
252b5132
RH
11005}
11006
11007long
17a2f251 11008md_pcrel_from (fixS *fixP)
252b5132 11009{
a7ebbfdf
TS
11010 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
11011 switch (fixP->fx_r_type)
11012 {
11013 case BFD_RELOC_16_PCREL_S2:
11014 case BFD_RELOC_MIPS_JMP:
11015 /* Return the address of the delay slot. */
11016 return addr + 4;
11017 default:
11018 return addr;
11019 }
252b5132
RH
11020}
11021
252b5132
RH
11022/* This is called before the symbol table is processed. In order to
11023 work with gcc when using mips-tfile, we must keep all local labels.
11024 However, in other cases, we want to discard them. If we were
11025 called with -g, but we didn't see any debugging information, it may
11026 mean that gcc is smuggling debugging information through to
11027 mips-tfile, in which case we must generate all local labels. */
11028
11029void
17a2f251 11030mips_frob_file_before_adjust (void)
252b5132
RH
11031{
11032#ifndef NO_ECOFF_DEBUGGING
11033 if (ECOFF_DEBUGGING
11034 && mips_debug != 0
11035 && ! ecoff_debugging_seen)
11036 flag_keep_locals = 1;
11037#endif
11038}
11039
3b91255e 11040/* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
55cf6793 11041 the corresponding LO16 reloc. This is called before md_apply_fix and
3b91255e
RS
11042 tc_gen_reloc. Unmatched relocs can only be generated by use of explicit
11043 relocation operators.
11044
11045 For our purposes, a %lo() expression matches a %got() or %hi()
11046 expression if:
11047
11048 (a) it refers to the same symbol; and
11049 (b) the offset applied in the %lo() expression is no lower than
11050 the offset applied in the %got() or %hi().
11051
11052 (b) allows us to cope with code like:
11053
11054 lui $4,%hi(foo)
11055 lh $4,%lo(foo+2)($4)
11056
11057 ...which is legal on RELA targets, and has a well-defined behaviour
11058 if the user knows that adding 2 to "foo" will not induce a carry to
11059 the high 16 bits.
11060
11061 When several %lo()s match a particular %got() or %hi(), we use the
11062 following rules to distinguish them:
11063
11064 (1) %lo()s with smaller offsets are a better match than %lo()s with
11065 higher offsets.
11066
11067 (2) %lo()s with no matching %got() or %hi() are better than those
11068 that already have a matching %got() or %hi().
11069
11070 (3) later %lo()s are better than earlier %lo()s.
11071
11072 These rules are applied in order.
11073
11074 (1) means, among other things, that %lo()s with identical offsets are
11075 chosen if they exist.
11076
11077 (2) means that we won't associate several high-part relocations with
11078 the same low-part relocation unless there's no alternative. Having
11079 several high parts for the same low part is a GNU extension; this rule
11080 allows careful users to avoid it.
11081
11082 (3) is purely cosmetic. mips_hi_fixup_list is is in reverse order,
11083 with the last high-part relocation being at the front of the list.
11084 It therefore makes sense to choose the last matching low-part
11085 relocation, all other things being equal. It's also easier
11086 to code that way. */
252b5132
RH
11087
11088void
17a2f251 11089mips_frob_file (void)
252b5132
RH
11090{
11091 struct mips_hi_fixup *l;
11092
11093 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
11094 {
11095 segment_info_type *seginfo;
3b91255e
RS
11096 bfd_boolean matched_lo_p;
11097 fixS **hi_pos, **lo_pos, **pos;
252b5132 11098
5919d012 11099 assert (reloc_needs_lo_p (l->fixp->fx_r_type));
252b5132 11100
5919d012
RS
11101 /* If a GOT16 relocation turns out to be against a global symbol,
11102 there isn't supposed to be a matching LO. */
11103 if (l->fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
11104 && !pic_need_relax (l->fixp->fx_addsy, l->seg))
11105 continue;
11106
11107 /* Check quickly whether the next fixup happens to be a matching %lo. */
11108 if (fixup_has_matching_lo_p (l->fixp))
252b5132
RH
11109 continue;
11110
252b5132 11111 seginfo = seg_info (l->seg);
252b5132 11112
3b91255e
RS
11113 /* Set HI_POS to the position of this relocation in the chain.
11114 Set LO_POS to the position of the chosen low-part relocation.
11115 MATCHED_LO_P is true on entry to the loop if *POS is a low-part
11116 relocation that matches an immediately-preceding high-part
11117 relocation. */
11118 hi_pos = NULL;
11119 lo_pos = NULL;
11120 matched_lo_p = FALSE;
11121 for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
11122 {
11123 if (*pos == l->fixp)
11124 hi_pos = pos;
11125
704803a9
MR
11126 if (((*pos)->fx_r_type == BFD_RELOC_LO16
11127 || (*pos)->fx_r_type == BFD_RELOC_MIPS16_LO16)
3b91255e
RS
11128 && (*pos)->fx_addsy == l->fixp->fx_addsy
11129 && (*pos)->fx_offset >= l->fixp->fx_offset
11130 && (lo_pos == NULL
11131 || (*pos)->fx_offset < (*lo_pos)->fx_offset
11132 || (!matched_lo_p
11133 && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
11134 lo_pos = pos;
11135
11136 matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
11137 && fixup_has_matching_lo_p (*pos));
11138 }
11139
11140 /* If we found a match, remove the high-part relocation from its
11141 current position and insert it before the low-part relocation.
11142 Make the offsets match so that fixup_has_matching_lo_p()
11143 will return true.
11144
11145 We don't warn about unmatched high-part relocations since some
11146 versions of gcc have been known to emit dead "lui ...%hi(...)"
11147 instructions. */
11148 if (lo_pos != NULL)
11149 {
11150 l->fixp->fx_offset = (*lo_pos)->fx_offset;
11151 if (l->fixp->fx_next != *lo_pos)
252b5132 11152 {
3b91255e
RS
11153 *hi_pos = l->fixp->fx_next;
11154 l->fixp->fx_next = *lo_pos;
11155 *lo_pos = l->fixp;
252b5132 11156 }
252b5132
RH
11157 }
11158 }
11159}
11160
3e722fb5 11161/* We may have combined relocations without symbols in the N32/N64 ABI.
f6688943 11162 We have to prevent gas from dropping them. */
252b5132 11163
252b5132 11164int
17a2f251 11165mips_force_relocation (fixS *fixp)
252b5132 11166{
ae6063d4 11167 if (generic_force_reloc (fixp))
252b5132
RH
11168 return 1;
11169
f6688943
TS
11170 if (HAVE_NEWABI
11171 && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
11172 && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
11173 || fixp->fx_r_type == BFD_RELOC_HI16_S
11174 || fixp->fx_r_type == BFD_RELOC_LO16))
11175 return 1;
11176
3e722fb5 11177 return 0;
252b5132
RH
11178}
11179
45f8dfe8
AO
11180/* This hook is called before a fix is simplified. We don't really
11181 decide whether to skip a fix here. Rather, we turn global symbols
11182 used as branch targets into local symbols, such that they undergo
11183 simplification. We can only do this if the symbol is defined and
11184 it is in the same section as the branch. If this doesn't hold, we
11185 emit a better error message than just saying the relocation is not
11186 valid for the selected object format.
11187
11188 FIXP is the fix-up we're going to try to simplify, SEG is the
11189 segment in which the fix up occurs. The return value should be
11190 non-zero to indicate the fix-up is valid for further
11191 simplifications. */
11192
11193int
17a2f251 11194mips_validate_fix (struct fix *fixP, asection *seg)
45f8dfe8
AO
11195{
11196 /* There's a lot of discussion on whether it should be possible to
11197 use R_MIPS_PC16 to represent branch relocations. The outcome
11198 seems to be that it can, but gas/bfd are very broken in creating
11199 RELA relocations for this, so for now we only accept branches to
11200 symbols in the same section. Anything else is of dubious value,
11201 since there's no guarantee that at link time the symbol would be
11202 in range. Even for branches to local symbols this is arguably
11203 wrong, since it we assume the symbol is not going to be
11204 overridden, which should be possible per ELF library semantics,
11205 but then, there isn't a dynamic relocation that could be used to
11206 this effect, and the target would likely be out of range as well.
11207
11208 Unfortunately, it seems that there is too much code out there
11209 that relies on branches to symbols that are global to be resolved
11210 as if they were local, like the IRIX tools do, so we do it as
11211 well, but with a warning so that people are reminded to fix their
11212 code. If we ever get back to using R_MIPS_PC16 for branch
11213 targets, this entire block should go away (and probably the
11214 whole function). */
11215
11216 if (fixP->fx_r_type == BFD_RELOC_16_PCREL_S2
3e722fb5
CD
11217 && ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
11218 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
45f8dfe8
AO
11219 || bfd_reloc_type_lookup (stdoutput, BFD_RELOC_16_PCREL_S2) == NULL)
11220 && fixP->fx_addsy)
11221 {
11222 if (! S_IS_DEFINED (fixP->fx_addsy))
11223 {
11224 as_bad_where (fixP->fx_file, fixP->fx_line,
11225 _("Cannot branch to undefined symbol."));
11226 /* Avoid any further errors about this fixup. */
11227 fixP->fx_done = 1;
11228 }
11229 else if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
11230 {
11231 as_bad_where (fixP->fx_file, fixP->fx_line,
11232 _("Cannot branch to symbol in another section."));
11233 fixP->fx_done = 1;
11234 }
11235 else if (S_IS_EXTERNAL (fixP->fx_addsy))
11236 {
11237 symbolS *sym = fixP->fx_addsy;
11238
115695a8
CD
11239 if (mips_pic == SVR4_PIC)
11240 as_warn_where (fixP->fx_file, fixP->fx_line,
11241 _("Pretending global symbol used as branch target is local."));
45f8dfe8
AO
11242
11243 fixP->fx_addsy = symbol_create (S_GET_NAME (sym),
11244 S_GET_SEGMENT (sym),
11245 S_GET_VALUE (sym),
11246 symbol_get_frag (sym));
11247 copy_symbol_attributes (fixP->fx_addsy, sym);
11248 S_CLEAR_EXTERNAL (fixP->fx_addsy);
11249 assert (symbol_resolved_p (sym));
11250 symbol_mark_resolved (fixP->fx_addsy);
11251 }
11252 }
11253
11254 return 1;
11255}
11256
252b5132
RH
11257/* Apply a fixup to the object file. */
11258
94f592af 11259void
55cf6793 11260md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 11261{
874e8986 11262 bfd_byte *buf;
98aa84af 11263 long insn;
a7ebbfdf 11264 reloc_howto_type *howto;
252b5132 11265
a7ebbfdf
TS
11266 /* We ignore generic BFD relocations we don't know about. */
11267 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
11268 if (! howto)
11269 return;
65551fa4 11270
252b5132
RH
11271 assert (fixP->fx_size == 4
11272 || fixP->fx_r_type == BFD_RELOC_16
11273 || fixP->fx_r_type == BFD_RELOC_64
f6688943
TS
11274 || fixP->fx_r_type == BFD_RELOC_CTOR
11275 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
252b5132 11276 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
a7ebbfdf 11277 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY);
252b5132 11278
a7ebbfdf 11279 buf = (bfd_byte *) (fixP->fx_frag->fr_literal + fixP->fx_where);
252b5132 11280
3e722fb5 11281 assert (! fixP->fx_pcrel);
b1dca8ee
RS
11282
11283 /* Don't treat parts of a composite relocation as done. There are two
11284 reasons for this:
11285
11286 (1) The second and third parts will be against 0 (RSS_UNDEF) but
11287 should nevertheless be emitted if the first part is.
11288
11289 (2) In normal usage, composite relocations are never assembly-time
11290 constants. The easiest way of dealing with the pathological
11291 exceptions is to generate a relocation against STN_UNDEF and
11292 leave everything up to the linker. */
11293 if (fixP->fx_addsy == NULL && fixP->fx_tcbit == 0)
252b5132
RH
11294 fixP->fx_done = 1;
11295
11296 switch (fixP->fx_r_type)
11297 {
3f98094e
DJ
11298 case BFD_RELOC_MIPS_TLS_GD:
11299 case BFD_RELOC_MIPS_TLS_LDM:
11300 case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
11301 case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
11302 case BFD_RELOC_MIPS_TLS_GOTTPREL:
11303 case BFD_RELOC_MIPS_TLS_TPREL_HI16:
11304 case BFD_RELOC_MIPS_TLS_TPREL_LO16:
11305 S_SET_THREAD_LOCAL (fixP->fx_addsy);
11306 /* fall through */
11307
252b5132 11308 case BFD_RELOC_MIPS_JMP:
e369bcce
TS
11309 case BFD_RELOC_MIPS_SHIFT5:
11310 case BFD_RELOC_MIPS_SHIFT6:
11311 case BFD_RELOC_MIPS_GOT_DISP:
11312 case BFD_RELOC_MIPS_GOT_PAGE:
11313 case BFD_RELOC_MIPS_GOT_OFST:
11314 case BFD_RELOC_MIPS_SUB:
11315 case BFD_RELOC_MIPS_INSERT_A:
11316 case BFD_RELOC_MIPS_INSERT_B:
11317 case BFD_RELOC_MIPS_DELETE:
11318 case BFD_RELOC_MIPS_HIGHEST:
11319 case BFD_RELOC_MIPS_HIGHER:
11320 case BFD_RELOC_MIPS_SCN_DISP:
11321 case BFD_RELOC_MIPS_REL16:
11322 case BFD_RELOC_MIPS_RELGOT:
11323 case BFD_RELOC_MIPS_JALR:
252b5132
RH
11324 case BFD_RELOC_HI16:
11325 case BFD_RELOC_HI16_S:
cdf6fd85 11326 case BFD_RELOC_GPREL16:
252b5132
RH
11327 case BFD_RELOC_MIPS_LITERAL:
11328 case BFD_RELOC_MIPS_CALL16:
11329 case BFD_RELOC_MIPS_GOT16:
cdf6fd85 11330 case BFD_RELOC_GPREL32:
252b5132
RH
11331 case BFD_RELOC_MIPS_GOT_HI16:
11332 case BFD_RELOC_MIPS_GOT_LO16:
11333 case BFD_RELOC_MIPS_CALL_HI16:
11334 case BFD_RELOC_MIPS_CALL_LO16:
11335 case BFD_RELOC_MIPS16_GPREL:
d6f16593
MR
11336 case BFD_RELOC_MIPS16_HI16:
11337 case BFD_RELOC_MIPS16_HI16_S:
3e722fb5 11338 assert (! fixP->fx_pcrel);
252b5132
RH
11339 /* Nothing needed to do. The value comes from the reloc entry */
11340 break;
11341
11342 case BFD_RELOC_MIPS16_JMP:
11343 /* We currently always generate a reloc against a symbol, which
11344 means that we don't want an addend even if the symbol is
11345 defined. */
a7ebbfdf 11346 *valP = 0;
252b5132
RH
11347 break;
11348
252b5132
RH
11349 case BFD_RELOC_64:
11350 /* This is handled like BFD_RELOC_32, but we output a sign
11351 extended value if we are only 32 bits. */
3e722fb5 11352 if (fixP->fx_done)
252b5132
RH
11353 {
11354 if (8 <= sizeof (valueT))
2132e3a3 11355 md_number_to_chars ((char *) buf, *valP, 8);
252b5132
RH
11356 else
11357 {
a7ebbfdf 11358 valueT hiv;
252b5132 11359
a7ebbfdf 11360 if ((*valP & 0x80000000) != 0)
252b5132
RH
11361 hiv = 0xffffffff;
11362 else
11363 hiv = 0;
b215186b 11364 md_number_to_chars ((char *)(buf + (target_big_endian ? 4 : 0)),
a7ebbfdf 11365 *valP, 4);
b215186b 11366 md_number_to_chars ((char *)(buf + (target_big_endian ? 0 : 4)),
a7ebbfdf 11367 hiv, 4);
252b5132
RH
11368 }
11369 }
11370 break;
11371
056350c6 11372 case BFD_RELOC_RVA:
252b5132
RH
11373 case BFD_RELOC_32:
11374 /* If we are deleting this reloc entry, we must fill in the
11375 value now. This can happen if we have a .word which is not
3e722fb5
CD
11376 resolved when it appears but is later defined. */
11377 if (fixP->fx_done)
2132e3a3 11378 md_number_to_chars ((char *) buf, *valP, 4);
252b5132
RH
11379 break;
11380
11381 case BFD_RELOC_16:
11382 /* If we are deleting this reloc entry, we must fill in the
11383 value now. */
252b5132 11384 if (fixP->fx_done)
2132e3a3 11385 md_number_to_chars ((char *) buf, *valP, 2);
252b5132
RH
11386 break;
11387
11388 case BFD_RELOC_LO16:
d6f16593 11389 case BFD_RELOC_MIPS16_LO16:
3e722fb5
CD
11390 /* FIXME: Now that embedded-PIC is gone, some of this code/comment
11391 may be safe to remove, but if so it's not obvious. */
252b5132
RH
11392 /* When handling an embedded PIC switch statement, we can wind
11393 up deleting a LO16 reloc. See the 'o' case in mips_ip. */
11394 if (fixP->fx_done)
11395 {
a7ebbfdf 11396 if (*valP + 0x8000 > 0xffff)
252b5132
RH
11397 as_bad_where (fixP->fx_file, fixP->fx_line,
11398 _("relocation overflow"));
252b5132
RH
11399 if (target_big_endian)
11400 buf += 2;
2132e3a3 11401 md_number_to_chars ((char *) buf, *valP, 2);
252b5132
RH
11402 }
11403 break;
11404
11405 case BFD_RELOC_16_PCREL_S2:
a7ebbfdf 11406 if ((*valP & 0x3) != 0)
cb56d3d3 11407 as_bad_where (fixP->fx_file, fixP->fx_line,
a7ebbfdf 11408 _("Branch to odd address (%lx)"), (long) *valP);
cb56d3d3 11409
252b5132
RH
11410 /*
11411 * We need to save the bits in the instruction since fixup_segment()
11412 * might be deleting the relocation entry (i.e., a branch within
11413 * the current segment).
11414 */
a7ebbfdf 11415 if (! fixP->fx_done)
bb2d6cd7 11416 break;
252b5132
RH
11417
11418 /* update old instruction data */
252b5132
RH
11419 if (target_big_endian)
11420 insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
11421 else
11422 insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
11423
a7ebbfdf
TS
11424 if (*valP + 0x20000 <= 0x3ffff)
11425 {
11426 insn |= (*valP >> 2) & 0xffff;
2132e3a3 11427 md_number_to_chars ((char *) buf, insn, 4);
a7ebbfdf
TS
11428 }
11429 else if (mips_pic == NO_PIC
11430 && fixP->fx_done
11431 && fixP->fx_frag->fr_address >= text_section->vma
11432 && (fixP->fx_frag->fr_address
587aac4e 11433 < text_section->vma + bfd_get_section_size (text_section))
a7ebbfdf
TS
11434 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
11435 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
11436 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
252b5132
RH
11437 {
11438 /* The branch offset is too large. If this is an
11439 unconditional branch, and we are not generating PIC code,
11440 we can convert it to an absolute jump instruction. */
a7ebbfdf
TS
11441 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
11442 insn = 0x0c000000; /* jal */
252b5132 11443 else
a7ebbfdf
TS
11444 insn = 0x08000000; /* j */
11445 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
11446 fixP->fx_done = 0;
11447 fixP->fx_addsy = section_symbol (text_section);
11448 *valP += md_pcrel_from (fixP);
2132e3a3 11449 md_number_to_chars ((char *) buf, insn, 4);
a7ebbfdf
TS
11450 }
11451 else
11452 {
11453 /* If we got here, we have branch-relaxation disabled,
11454 and there's nothing we can do to fix this instruction
11455 without turning it into a longer sequence. */
11456 as_bad_where (fixP->fx_file, fixP->fx_line,
11457 _("Branch out of range"));
252b5132 11458 }
252b5132
RH
11459 break;
11460
11461 case BFD_RELOC_VTABLE_INHERIT:
11462 fixP->fx_done = 0;
11463 if (fixP->fx_addsy
11464 && !S_IS_DEFINED (fixP->fx_addsy)
11465 && !S_IS_WEAK (fixP->fx_addsy))
11466 S_SET_WEAK (fixP->fx_addsy);
11467 break;
11468
11469 case BFD_RELOC_VTABLE_ENTRY:
11470 fixP->fx_done = 0;
11471 break;
11472
11473 default:
11474 internalError ();
11475 }
a7ebbfdf
TS
11476
11477 /* Remember value for tc_gen_reloc. */
11478 fixP->fx_addnumber = *valP;
252b5132
RH
11479}
11480
252b5132 11481static symbolS *
17a2f251 11482get_symbol (void)
252b5132
RH
11483{
11484 int c;
11485 char *name;
11486 symbolS *p;
11487
11488 name = input_line_pointer;
11489 c = get_symbol_end ();
11490 p = (symbolS *) symbol_find_or_make (name);
11491 *input_line_pointer = c;
11492 return p;
11493}
11494
11495/* Align the current frag to a given power of two. The MIPS assembler
11496 also automatically adjusts any preceding label. */
11497
11498static void
17a2f251 11499mips_align (int to, int fill, symbolS *label)
252b5132 11500{
7d10b47d 11501 mips_emit_delays ();
252b5132
RH
11502 frag_align (to, fill, 0);
11503 record_alignment (now_seg, to);
11504 if (label != NULL)
11505 {
11506 assert (S_GET_SEGMENT (label) == now_seg);
49309057 11507 symbol_set_frag (label, frag_now);
252b5132
RH
11508 S_SET_VALUE (label, (valueT) frag_now_fix ());
11509 }
11510}
11511
11512/* Align to a given power of two. .align 0 turns off the automatic
11513 alignment used by the data creating pseudo-ops. */
11514
11515static void
17a2f251 11516s_align (int x ATTRIBUTE_UNUSED)
252b5132
RH
11517{
11518 register int temp;
11519 register long temp_fill;
11520 long max_alignment = 15;
11521
11522 /*
11523
67c1ffbe 11524 o Note that the assembler pulls down any immediately preceding label
252b5132
RH
11525 to the aligned address.
11526 o It's not documented but auto alignment is reinstated by
11527 a .align pseudo instruction.
11528 o Note also that after auto alignment is turned off the mips assembler
11529 issues an error on attempt to assemble an improperly aligned data item.
11530 We don't.
11531
11532 */
11533
11534 temp = get_absolute_expression ();
11535 if (temp > max_alignment)
11536 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
11537 else if (temp < 0)
11538 {
11539 as_warn (_("Alignment negative: 0 assumed."));
11540 temp = 0;
11541 }
11542 if (*input_line_pointer == ',')
11543 {
f9419b05 11544 ++input_line_pointer;
252b5132
RH
11545 temp_fill = get_absolute_expression ();
11546 }
11547 else
11548 temp_fill = 0;
11549 if (temp)
11550 {
11551 auto_align = 1;
11552 mips_align (temp, (int) temp_fill,
11553 insn_labels != NULL ? insn_labels->label : NULL);
11554 }
11555 else
11556 {
11557 auto_align = 0;
11558 }
11559
11560 demand_empty_rest_of_line ();
11561}
11562
252b5132 11563static void
17a2f251 11564s_change_sec (int sec)
252b5132
RH
11565{
11566 segT seg;
11567
252b5132
RH
11568#ifdef OBJ_ELF
11569 /* The ELF backend needs to know that we are changing sections, so
11570 that .previous works correctly. We could do something like check
b6ff326e 11571 for an obj_section_change_hook macro, but that might be confusing
252b5132
RH
11572 as it would not be appropriate to use it in the section changing
11573 functions in read.c, since obj-elf.c intercepts those. FIXME:
11574 This should be cleaner, somehow. */
11575 obj_elf_section_change_hook ();
11576#endif
11577
7d10b47d 11578 mips_emit_delays ();
252b5132
RH
11579 switch (sec)
11580 {
11581 case 't':
11582 s_text (0);
11583 break;
11584 case 'd':
11585 s_data (0);
11586 break;
11587 case 'b':
11588 subseg_set (bss_section, (subsegT) get_absolute_expression ());
11589 demand_empty_rest_of_line ();
11590 break;
11591
11592 case 'r':
4d0d148d
TS
11593 seg = subseg_new (RDATA_SECTION_NAME,
11594 (subsegT) get_absolute_expression ());
11595 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
252b5132 11596 {
4d0d148d
TS
11597 bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
11598 | SEC_READONLY | SEC_RELOC
11599 | SEC_DATA));
11600 if (strcmp (TARGET_OS, "elf") != 0)
11601 record_alignment (seg, 4);
252b5132 11602 }
4d0d148d 11603 demand_empty_rest_of_line ();
252b5132
RH
11604 break;
11605
11606 case 's':
4d0d148d
TS
11607 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
11608 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
252b5132 11609 {
4d0d148d
TS
11610 bfd_set_section_flags (stdoutput, seg,
11611 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
11612 if (strcmp (TARGET_OS, "elf") != 0)
11613 record_alignment (seg, 4);
252b5132 11614 }
4d0d148d
TS
11615 demand_empty_rest_of_line ();
11616 break;
252b5132
RH
11617 }
11618
11619 auto_align = 1;
11620}
b34976b6 11621
cca86cc8 11622void
17a2f251 11623s_change_section (int ignore ATTRIBUTE_UNUSED)
cca86cc8 11624{
7ed4a06a 11625#ifdef OBJ_ELF
cca86cc8
SC
11626 char *section_name;
11627 char c;
684022ea 11628 char next_c = 0;
cca86cc8
SC
11629 int section_type;
11630 int section_flag;
11631 int section_entry_size;
11632 int section_alignment;
b34976b6 11633
7ed4a06a
TS
11634 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11635 return;
11636
cca86cc8
SC
11637 section_name = input_line_pointer;
11638 c = get_symbol_end ();
a816d1ed
AO
11639 if (c)
11640 next_c = *(input_line_pointer + 1);
cca86cc8 11641
4cf0dd0d
TS
11642 /* Do we have .section Name<,"flags">? */
11643 if (c != ',' || (c == ',' && next_c == '"'))
cca86cc8 11644 {
4cf0dd0d
TS
11645 /* just after name is now '\0'. */
11646 *input_line_pointer = c;
cca86cc8
SC
11647 input_line_pointer = section_name;
11648 obj_elf_section (ignore);
11649 return;
11650 }
11651 input_line_pointer++;
11652
11653 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
11654 if (c == ',')
11655 section_type = get_absolute_expression ();
11656 else
11657 section_type = 0;
11658 if (*input_line_pointer++ == ',')
11659 section_flag = get_absolute_expression ();
11660 else
11661 section_flag = 0;
11662 if (*input_line_pointer++ == ',')
11663 section_entry_size = get_absolute_expression ();
11664 else
11665 section_entry_size = 0;
11666 if (*input_line_pointer++ == ',')
11667 section_alignment = get_absolute_expression ();
11668 else
11669 section_alignment = 0;
11670
a816d1ed
AO
11671 section_name = xstrdup (section_name);
11672
8ab8a5c8
RS
11673 /* When using the generic form of .section (as implemented by obj-elf.c),
11674 there's no way to set the section type to SHT_MIPS_DWARF. Users have
11675 traditionally had to fall back on the more common @progbits instead.
11676
11677 There's nothing really harmful in this, since bfd will correct
11678 SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file. But it
11679 means that, for backwards compatibiltiy, the special_section entries
11680 for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
11681
11682 Even so, we shouldn't force users of the MIPS .section syntax to
11683 incorrectly label the sections as SHT_PROGBITS. The best compromise
11684 seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
11685 generic type-checking code. */
11686 if (section_type == SHT_MIPS_DWARF)
11687 section_type = SHT_PROGBITS;
11688
cca86cc8
SC
11689 obj_elf_change_section (section_name, section_type, section_flag,
11690 section_entry_size, 0, 0, 0);
a816d1ed
AO
11691
11692 if (now_seg->name != section_name)
11693 free (section_name);
7ed4a06a 11694#endif /* OBJ_ELF */
cca86cc8 11695}
252b5132
RH
11696
11697void
17a2f251 11698mips_enable_auto_align (void)
252b5132
RH
11699{
11700 auto_align = 1;
11701}
11702
11703static void
17a2f251 11704s_cons (int log_size)
252b5132
RH
11705{
11706 symbolS *label;
11707
11708 label = insn_labels != NULL ? insn_labels->label : NULL;
7d10b47d 11709 mips_emit_delays ();
252b5132
RH
11710 if (log_size > 0 && auto_align)
11711 mips_align (log_size, 0, label);
11712 mips_clear_insn_labels ();
11713 cons (1 << log_size);
11714}
11715
11716static void
17a2f251 11717s_float_cons (int type)
252b5132
RH
11718{
11719 symbolS *label;
11720
11721 label = insn_labels != NULL ? insn_labels->label : NULL;
11722
7d10b47d 11723 mips_emit_delays ();
252b5132
RH
11724
11725 if (auto_align)
49309057
ILT
11726 {
11727 if (type == 'd')
11728 mips_align (3, 0, label);
11729 else
11730 mips_align (2, 0, label);
11731 }
252b5132
RH
11732
11733 mips_clear_insn_labels ();
11734
11735 float_cons (type);
11736}
11737
11738/* Handle .globl. We need to override it because on Irix 5 you are
11739 permitted to say
11740 .globl foo .text
11741 where foo is an undefined symbol, to mean that foo should be
11742 considered to be the address of a function. */
11743
11744static void
17a2f251 11745s_mips_globl (int x ATTRIBUTE_UNUSED)
252b5132
RH
11746{
11747 char *name;
11748 int c;
11749 symbolS *symbolP;
11750 flagword flag;
11751
8a06b769 11752 do
252b5132 11753 {
8a06b769 11754 name = input_line_pointer;
252b5132 11755 c = get_symbol_end ();
8a06b769
TS
11756 symbolP = symbol_find_or_make (name);
11757 S_SET_EXTERNAL (symbolP);
11758
252b5132 11759 *input_line_pointer = c;
8a06b769 11760 SKIP_WHITESPACE ();
252b5132 11761
8a06b769
TS
11762 /* On Irix 5, every global symbol that is not explicitly labelled as
11763 being a function is apparently labelled as being an object. */
11764 flag = BSF_OBJECT;
252b5132 11765
8a06b769
TS
11766 if (!is_end_of_line[(unsigned char) *input_line_pointer]
11767 && (*input_line_pointer != ','))
11768 {
11769 char *secname;
11770 asection *sec;
11771
11772 secname = input_line_pointer;
11773 c = get_symbol_end ();
11774 sec = bfd_get_section_by_name (stdoutput, secname);
11775 if (sec == NULL)
11776 as_bad (_("%s: no such section"), secname);
11777 *input_line_pointer = c;
11778
11779 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
11780 flag = BSF_FUNCTION;
11781 }
11782
11783 symbol_get_bfdsym (symbolP)->flags |= flag;
11784
11785 c = *input_line_pointer;
11786 if (c == ',')
11787 {
11788 input_line_pointer++;
11789 SKIP_WHITESPACE ();
11790 if (is_end_of_line[(unsigned char) *input_line_pointer])
11791 c = '\n';
11792 }
11793 }
11794 while (c == ',');
252b5132 11795
252b5132
RH
11796 demand_empty_rest_of_line ();
11797}
11798
11799static void
17a2f251 11800s_option (int x ATTRIBUTE_UNUSED)
252b5132
RH
11801{
11802 char *opt;
11803 char c;
11804
11805 opt = input_line_pointer;
11806 c = get_symbol_end ();
11807
11808 if (*opt == 'O')
11809 {
11810 /* FIXME: What does this mean? */
11811 }
11812 else if (strncmp (opt, "pic", 3) == 0)
11813 {
11814 int i;
11815
11816 i = atoi (opt + 3);
11817 if (i == 0)
11818 mips_pic = NO_PIC;
11819 else if (i == 2)
143d77c5 11820 {
252b5132 11821 mips_pic = SVR4_PIC;
143d77c5
EC
11822 mips_abicalls = TRUE;
11823 }
252b5132
RH
11824 else
11825 as_bad (_(".option pic%d not supported"), i);
11826
4d0d148d 11827 if (mips_pic == SVR4_PIC)
252b5132
RH
11828 {
11829 if (g_switch_seen && g_switch_value != 0)
11830 as_warn (_("-G may not be used with SVR4 PIC code"));
11831 g_switch_value = 0;
11832 bfd_set_gp_size (stdoutput, 0);
11833 }
11834 }
11835 else
11836 as_warn (_("Unrecognized option \"%s\""), opt);
11837
11838 *input_line_pointer = c;
11839 demand_empty_rest_of_line ();
11840}
11841
11842/* This structure is used to hold a stack of .set values. */
11843
e972090a
NC
11844struct mips_option_stack
11845{
252b5132
RH
11846 struct mips_option_stack *next;
11847 struct mips_set_options options;
11848};
11849
11850static struct mips_option_stack *mips_opts_stack;
11851
11852/* Handle the .set pseudo-op. */
11853
11854static void
17a2f251 11855s_mipsset (int x ATTRIBUTE_UNUSED)
252b5132
RH
11856{
11857 char *name = input_line_pointer, ch;
11858
11859 while (!is_end_of_line[(unsigned char) *input_line_pointer])
f9419b05 11860 ++input_line_pointer;
252b5132
RH
11861 ch = *input_line_pointer;
11862 *input_line_pointer = '\0';
11863
11864 if (strcmp (name, "reorder") == 0)
11865 {
7d10b47d
RS
11866 if (mips_opts.noreorder)
11867 end_noreorder ();
252b5132
RH
11868 }
11869 else if (strcmp (name, "noreorder") == 0)
11870 {
7d10b47d
RS
11871 if (!mips_opts.noreorder)
11872 start_noreorder ();
252b5132
RH
11873 }
11874 else if (strcmp (name, "at") == 0)
11875 {
11876 mips_opts.noat = 0;
11877 }
11878 else if (strcmp (name, "noat") == 0)
11879 {
11880 mips_opts.noat = 1;
11881 }
11882 else if (strcmp (name, "macro") == 0)
11883 {
11884 mips_opts.warn_about_macros = 0;
11885 }
11886 else if (strcmp (name, "nomacro") == 0)
11887 {
11888 if (mips_opts.noreorder == 0)
11889 as_bad (_("`noreorder' must be set before `nomacro'"));
11890 mips_opts.warn_about_macros = 1;
11891 }
11892 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
11893 {
11894 mips_opts.nomove = 0;
11895 }
11896 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
11897 {
11898 mips_opts.nomove = 1;
11899 }
11900 else if (strcmp (name, "bopt") == 0)
11901 {
11902 mips_opts.nobopt = 0;
11903 }
11904 else if (strcmp (name, "nobopt") == 0)
11905 {
11906 mips_opts.nobopt = 1;
11907 }
11908 else if (strcmp (name, "mips16") == 0
11909 || strcmp (name, "MIPS-16") == 0)
11910 mips_opts.mips16 = 1;
11911 else if (strcmp (name, "nomips16") == 0
11912 || strcmp (name, "noMIPS-16") == 0)
11913 mips_opts.mips16 = 0;
1f25f5d3
CD
11914 else if (strcmp (name, "mips3d") == 0)
11915 mips_opts.ase_mips3d = 1;
11916 else if (strcmp (name, "nomips3d") == 0)
11917 mips_opts.ase_mips3d = 0;
a4672219
TS
11918 else if (strcmp (name, "mdmx") == 0)
11919 mips_opts.ase_mdmx = 1;
11920 else if (strcmp (name, "nomdmx") == 0)
11921 mips_opts.ase_mdmx = 0;
74cd071d
CF
11922 else if (strcmp (name, "dsp") == 0)
11923 mips_opts.ase_dsp = 1;
11924 else if (strcmp (name, "nodsp") == 0)
11925 mips_opts.ase_dsp = 0;
ef2e4d86
CF
11926 else if (strcmp (name, "mt") == 0)
11927 mips_opts.ase_mt = 1;
11928 else if (strcmp (name, "nomt") == 0)
11929 mips_opts.ase_mt = 0;
1a2c1fad 11930 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
252b5132 11931 {
af7ee8bf 11932 int reset = 0;
252b5132 11933
1a2c1fad
CD
11934 /* Permit the user to change the ISA and architecture on the fly.
11935 Needless to say, misuse can cause serious problems. */
81a21e38 11936 if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
af7ee8bf
CD
11937 {
11938 reset = 1;
11939 mips_opts.isa = file_mips_isa;
1a2c1fad 11940 mips_opts.arch = file_mips_arch;
1a2c1fad
CD
11941 }
11942 else if (strncmp (name, "arch=", 5) == 0)
11943 {
11944 const struct mips_cpu_info *p;
11945
11946 p = mips_parse_cpu("internal use", name + 5);
11947 if (!p)
11948 as_bad (_("unknown architecture %s"), name + 5);
11949 else
11950 {
11951 mips_opts.arch = p->cpu;
11952 mips_opts.isa = p->isa;
11953 }
11954 }
81a21e38
TS
11955 else if (strncmp (name, "mips", 4) == 0)
11956 {
11957 const struct mips_cpu_info *p;
11958
11959 p = mips_parse_cpu("internal use", name);
11960 if (!p)
11961 as_bad (_("unknown ISA level %s"), name + 4);
11962 else
11963 {
11964 mips_opts.arch = p->cpu;
11965 mips_opts.isa = p->isa;
11966 }
11967 }
af7ee8bf 11968 else
81a21e38 11969 as_bad (_("unknown ISA or architecture %s"), name);
af7ee8bf
CD
11970
11971 switch (mips_opts.isa)
98d3f06f
KH
11972 {
11973 case 0:
98d3f06f 11974 break;
af7ee8bf
CD
11975 case ISA_MIPS1:
11976 case ISA_MIPS2:
11977 case ISA_MIPS32:
11978 case ISA_MIPS32R2:
98d3f06f
KH
11979 mips_opts.gp32 = 1;
11980 mips_opts.fp32 = 1;
11981 break;
af7ee8bf
CD
11982 case ISA_MIPS3:
11983 case ISA_MIPS4:
11984 case ISA_MIPS5:
11985 case ISA_MIPS64:
5f74bc13 11986 case ISA_MIPS64R2:
98d3f06f
KH
11987 mips_opts.gp32 = 0;
11988 mips_opts.fp32 = 0;
11989 break;
11990 default:
11991 as_bad (_("unknown ISA level %s"), name + 4);
11992 break;
11993 }
af7ee8bf 11994 if (reset)
98d3f06f 11995 {
af7ee8bf
CD
11996 mips_opts.gp32 = file_mips_gp32;
11997 mips_opts.fp32 = file_mips_fp32;
98d3f06f 11998 }
252b5132
RH
11999 }
12000 else if (strcmp (name, "autoextend") == 0)
12001 mips_opts.noautoextend = 0;
12002 else if (strcmp (name, "noautoextend") == 0)
12003 mips_opts.noautoextend = 1;
12004 else if (strcmp (name, "push") == 0)
12005 {
12006 struct mips_option_stack *s;
12007
12008 s = (struct mips_option_stack *) xmalloc (sizeof *s);
12009 s->next = mips_opts_stack;
12010 s->options = mips_opts;
12011 mips_opts_stack = s;
12012 }
12013 else if (strcmp (name, "pop") == 0)
12014 {
12015 struct mips_option_stack *s;
12016
12017 s = mips_opts_stack;
12018 if (s == NULL)
12019 as_bad (_(".set pop with no .set push"));
12020 else
12021 {
12022 /* If we're changing the reorder mode we need to handle
12023 delay slots correctly. */
12024 if (s->options.noreorder && ! mips_opts.noreorder)
7d10b47d 12025 start_noreorder ();
252b5132 12026 else if (! s->options.noreorder && mips_opts.noreorder)
7d10b47d 12027 end_noreorder ();
252b5132
RH
12028
12029 mips_opts = s->options;
12030 mips_opts_stack = s->next;
12031 free (s);
12032 }
12033 }
aed1a261
RS
12034 else if (strcmp (name, "sym32") == 0)
12035 mips_opts.sym32 = TRUE;
12036 else if (strcmp (name, "nosym32") == 0)
12037 mips_opts.sym32 = FALSE;
252b5132
RH
12038 else
12039 {
12040 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
12041 }
12042 *input_line_pointer = ch;
12043 demand_empty_rest_of_line ();
12044}
12045
12046/* Handle the .abicalls pseudo-op. I believe this is equivalent to
12047 .option pic2. It means to generate SVR4 PIC calls. */
12048
12049static void
17a2f251 12050s_abicalls (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
12051{
12052 mips_pic = SVR4_PIC;
143d77c5 12053 mips_abicalls = TRUE;
4d0d148d
TS
12054
12055 if (g_switch_seen && g_switch_value != 0)
12056 as_warn (_("-G may not be used with SVR4 PIC code"));
12057 g_switch_value = 0;
12058
252b5132
RH
12059 bfd_set_gp_size (stdoutput, 0);
12060 demand_empty_rest_of_line ();
12061}
12062
12063/* Handle the .cpload pseudo-op. This is used when generating SVR4
12064 PIC code. It sets the $gp register for the function based on the
12065 function address, which is in the register named in the argument.
12066 This uses a relocation against _gp_disp, which is handled specially
12067 by the linker. The result is:
12068 lui $gp,%hi(_gp_disp)
12069 addiu $gp,$gp,%lo(_gp_disp)
12070 addu $gp,$gp,.cpload argument
aa6975fb
ILT
12071 The .cpload argument is normally $25 == $t9.
12072
12073 The -mno-shared option changes this to:
bbe506e8
TS
12074 lui $gp,%hi(__gnu_local_gp)
12075 addiu $gp,$gp,%lo(__gnu_local_gp)
aa6975fb
ILT
12076 and the argument is ignored. This saves an instruction, but the
12077 resulting code is not position independent; it uses an absolute
bbe506e8
TS
12078 address for __gnu_local_gp. Thus code assembled with -mno-shared
12079 can go into an ordinary executable, but not into a shared library. */
252b5132
RH
12080
12081static void
17a2f251 12082s_cpload (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
12083{
12084 expressionS ex;
aa6975fb
ILT
12085 int reg;
12086 int in_shared;
252b5132 12087
6478892d
TS
12088 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12089 .cpload is ignored. */
12090 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
12091 {
12092 s_ignore (0);
12093 return;
12094 }
12095
d3ecfc59 12096 /* .cpload should be in a .set noreorder section. */
252b5132
RH
12097 if (mips_opts.noreorder == 0)
12098 as_warn (_(".cpload not in noreorder section"));
12099
aa6975fb
ILT
12100 reg = tc_get_register (0);
12101
12102 /* If we need to produce a 64-bit address, we are better off using
12103 the default instruction sequence. */
aed1a261 12104 in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
aa6975fb 12105
252b5132 12106 ex.X_op = O_symbol;
bbe506e8
TS
12107 ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
12108 "__gnu_local_gp");
252b5132
RH
12109 ex.X_op_symbol = NULL;
12110 ex.X_add_number = 0;
12111
12112 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
49309057 12113 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
252b5132 12114
584892a6 12115 macro_start ();
67c0d1eb
RS
12116 macro_build_lui (&ex, mips_gp_register);
12117 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
17a2f251 12118 mips_gp_register, BFD_RELOC_LO16);
aa6975fb
ILT
12119 if (in_shared)
12120 macro_build (NULL, "addu", "d,v,t", mips_gp_register,
12121 mips_gp_register, reg);
584892a6 12122 macro_end ();
252b5132
RH
12123
12124 demand_empty_rest_of_line ();
12125}
12126
6478892d
TS
12127/* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
12128 .cpsetup $reg1, offset|$reg2, label
12129
12130 If offset is given, this results in:
12131 sd $gp, offset($sp)
956cd1d6 12132 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
12133 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
12134 daddu $gp, $gp, $reg1
6478892d
TS
12135
12136 If $reg2 is given, this results in:
12137 daddu $reg2, $gp, $0
956cd1d6 12138 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
12139 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
12140 daddu $gp, $gp, $reg1
aa6975fb
ILT
12141 $reg1 is normally $25 == $t9.
12142
12143 The -mno-shared option replaces the last three instructions with
12144 lui $gp,%hi(_gp)
12145 addiu $gp,$gp,%lo(_gp)
12146 */
12147
6478892d 12148static void
17a2f251 12149s_cpsetup (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
12150{
12151 expressionS ex_off;
12152 expressionS ex_sym;
12153 int reg1;
6478892d 12154
8586fc66 12155 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
6478892d
TS
12156 We also need NewABI support. */
12157 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12158 {
12159 s_ignore (0);
12160 return;
12161 }
12162
12163 reg1 = tc_get_register (0);
12164 SKIP_WHITESPACE ();
12165 if (*input_line_pointer != ',')
12166 {
12167 as_bad (_("missing argument separator ',' for .cpsetup"));
12168 return;
12169 }
12170 else
80245285 12171 ++input_line_pointer;
6478892d
TS
12172 SKIP_WHITESPACE ();
12173 if (*input_line_pointer == '$')
80245285
TS
12174 {
12175 mips_cpreturn_register = tc_get_register (0);
12176 mips_cpreturn_offset = -1;
12177 }
6478892d 12178 else
80245285
TS
12179 {
12180 mips_cpreturn_offset = get_absolute_expression ();
12181 mips_cpreturn_register = -1;
12182 }
6478892d
TS
12183 SKIP_WHITESPACE ();
12184 if (*input_line_pointer != ',')
12185 {
12186 as_bad (_("missing argument separator ',' for .cpsetup"));
12187 return;
12188 }
12189 else
f9419b05 12190 ++input_line_pointer;
6478892d 12191 SKIP_WHITESPACE ();
f21f8242 12192 expression (&ex_sym);
6478892d 12193
584892a6 12194 macro_start ();
6478892d
TS
12195 if (mips_cpreturn_register == -1)
12196 {
12197 ex_off.X_op = O_constant;
12198 ex_off.X_add_symbol = NULL;
12199 ex_off.X_op_symbol = NULL;
12200 ex_off.X_add_number = mips_cpreturn_offset;
12201
67c0d1eb 12202 macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
17a2f251 12203 BFD_RELOC_LO16, SP);
6478892d
TS
12204 }
12205 else
67c0d1eb 12206 macro_build (NULL, "daddu", "d,v,t", mips_cpreturn_register,
17a2f251 12207 mips_gp_register, 0);
6478892d 12208
aed1a261 12209 if (mips_in_shared || HAVE_64BIT_SYMBOLS)
aa6975fb
ILT
12210 {
12211 macro_build (&ex_sym, "lui", "t,u", mips_gp_register,
12212 -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
12213 BFD_RELOC_HI16_S);
12214
12215 macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
12216 mips_gp_register, -1, BFD_RELOC_GPREL16,
12217 BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
12218
12219 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
12220 mips_gp_register, reg1);
12221 }
12222 else
12223 {
12224 expressionS ex;
12225
12226 ex.X_op = O_symbol;
4184909a 12227 ex.X_add_symbol = symbol_find_or_make ("__gnu_local_gp");
aa6975fb
ILT
12228 ex.X_op_symbol = NULL;
12229 ex.X_add_number = 0;
6e1304d8 12230
aa6975fb
ILT
12231 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
12232 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
12233
12234 macro_build_lui (&ex, mips_gp_register);
12235 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
12236 mips_gp_register, BFD_RELOC_LO16);
12237 }
f21f8242 12238
584892a6 12239 macro_end ();
6478892d
TS
12240
12241 demand_empty_rest_of_line ();
12242}
12243
12244static void
17a2f251 12245s_cplocal (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
12246{
12247 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
12248 .cplocal is ignored. */
12249 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12250 {
12251 s_ignore (0);
12252 return;
12253 }
12254
12255 mips_gp_register = tc_get_register (0);
85b51719 12256 demand_empty_rest_of_line ();
6478892d
TS
12257}
12258
252b5132
RH
12259/* Handle the .cprestore pseudo-op. This stores $gp into a given
12260 offset from $sp. The offset is remembered, and after making a PIC
12261 call $gp is restored from that location. */
12262
12263static void
17a2f251 12264s_cprestore (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
12265{
12266 expressionS ex;
252b5132 12267
6478892d 12268 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
c9914766 12269 .cprestore is ignored. */
6478892d 12270 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
12271 {
12272 s_ignore (0);
12273 return;
12274 }
12275
12276 mips_cprestore_offset = get_absolute_expression ();
7a621144 12277 mips_cprestore_valid = 1;
252b5132
RH
12278
12279 ex.X_op = O_constant;
12280 ex.X_add_symbol = NULL;
12281 ex.X_op_symbol = NULL;
12282 ex.X_add_number = mips_cprestore_offset;
12283
584892a6 12284 macro_start ();
67c0d1eb
RS
12285 macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
12286 SP, HAVE_64BIT_ADDRESSES);
584892a6 12287 macro_end ();
252b5132
RH
12288
12289 demand_empty_rest_of_line ();
12290}
12291
6478892d 12292/* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
67c1ffbe 12293 was given in the preceding .cpsetup, it results in:
6478892d 12294 ld $gp, offset($sp)
76b3015f 12295
6478892d 12296 If a register $reg2 was given there, it results in:
609f23f4 12297 daddu $gp, $reg2, $0
6478892d
TS
12298 */
12299static void
17a2f251 12300s_cpreturn (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
12301{
12302 expressionS ex;
6478892d
TS
12303
12304 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
12305 We also need NewABI support. */
12306 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12307 {
12308 s_ignore (0);
12309 return;
12310 }
12311
584892a6 12312 macro_start ();
6478892d
TS
12313 if (mips_cpreturn_register == -1)
12314 {
12315 ex.X_op = O_constant;
12316 ex.X_add_symbol = NULL;
12317 ex.X_op_symbol = NULL;
12318 ex.X_add_number = mips_cpreturn_offset;
12319
67c0d1eb 12320 macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
6478892d
TS
12321 }
12322 else
67c0d1eb 12323 macro_build (NULL, "daddu", "d,v,t", mips_gp_register,
17a2f251 12324 mips_cpreturn_register, 0);
584892a6 12325 macro_end ();
6478892d
TS
12326
12327 demand_empty_rest_of_line ();
12328}
12329
12330/* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
12331 code. It sets the offset to use in gp_rel relocations. */
12332
12333static void
17a2f251 12334s_gpvalue (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
12335{
12336 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
12337 We also need NewABI support. */
12338 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12339 {
12340 s_ignore (0);
12341 return;
12342 }
12343
def2e0dd 12344 mips_gprel_offset = get_absolute_expression ();
6478892d
TS
12345
12346 demand_empty_rest_of_line ();
12347}
12348
252b5132
RH
12349/* Handle the .gpword pseudo-op. This is used when generating PIC
12350 code. It generates a 32 bit GP relative reloc. */
12351
12352static void
17a2f251 12353s_gpword (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
12354{
12355 symbolS *label;
12356 expressionS ex;
12357 char *p;
12358
12359 /* When not generating PIC code, this is treated as .word. */
12360 if (mips_pic != SVR4_PIC)
12361 {
12362 s_cons (2);
12363 return;
12364 }
12365
12366 label = insn_labels != NULL ? insn_labels->label : NULL;
7d10b47d 12367 mips_emit_delays ();
252b5132
RH
12368 if (auto_align)
12369 mips_align (2, 0, label);
12370 mips_clear_insn_labels ();
12371
12372 expression (&ex);
12373
12374 if (ex.X_op != O_symbol || ex.X_add_number != 0)
12375 {
12376 as_bad (_("Unsupported use of .gpword"));
12377 ignore_rest_of_line ();
12378 }
12379
12380 p = frag_more (4);
17a2f251 12381 md_number_to_chars (p, 0, 4);
b34976b6 12382 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
cdf6fd85 12383 BFD_RELOC_GPREL32);
252b5132
RH
12384
12385 demand_empty_rest_of_line ();
12386}
12387
10181a0d 12388static void
17a2f251 12389s_gpdword (int ignore ATTRIBUTE_UNUSED)
10181a0d
AO
12390{
12391 symbolS *label;
12392 expressionS ex;
12393 char *p;
12394
12395 /* When not generating PIC code, this is treated as .dword. */
12396 if (mips_pic != SVR4_PIC)
12397 {
12398 s_cons (3);
12399 return;
12400 }
12401
12402 label = insn_labels != NULL ? insn_labels->label : NULL;
7d10b47d 12403 mips_emit_delays ();
10181a0d
AO
12404 if (auto_align)
12405 mips_align (3, 0, label);
12406 mips_clear_insn_labels ();
12407
12408 expression (&ex);
12409
12410 if (ex.X_op != O_symbol || ex.X_add_number != 0)
12411 {
12412 as_bad (_("Unsupported use of .gpdword"));
12413 ignore_rest_of_line ();
12414 }
12415
12416 p = frag_more (8);
17a2f251 12417 md_number_to_chars (p, 0, 8);
a105a300 12418 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
6e1304d8 12419 BFD_RELOC_GPREL32)->fx_tcbit = 1;
10181a0d
AO
12420
12421 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
6e1304d8
RS
12422 fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
12423 FALSE, BFD_RELOC_64)->fx_tcbit = 1;
10181a0d
AO
12424
12425 demand_empty_rest_of_line ();
12426}
12427
252b5132
RH
12428/* Handle the .cpadd pseudo-op. This is used when dealing with switch
12429 tables in SVR4 PIC code. */
12430
12431static void
17a2f251 12432s_cpadd (int ignore ATTRIBUTE_UNUSED)
252b5132 12433{
252b5132
RH
12434 int reg;
12435
10181a0d
AO
12436 /* This is ignored when not generating SVR4 PIC code. */
12437 if (mips_pic != SVR4_PIC)
252b5132
RH
12438 {
12439 s_ignore (0);
12440 return;
12441 }
12442
12443 /* Add $gp to the register named as an argument. */
584892a6 12444 macro_start ();
252b5132 12445 reg = tc_get_register (0);
67c0d1eb 12446 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
584892a6 12447 macro_end ();
252b5132 12448
bdaaa2e1 12449 demand_empty_rest_of_line ();
252b5132
RH
12450}
12451
12452/* Handle the .insn pseudo-op. This marks instruction labels in
12453 mips16 mode. This permits the linker to handle them specially,
12454 such as generating jalx instructions when needed. We also make
12455 them odd for the duration of the assembly, in order to generate the
12456 right sort of code. We will make them even in the adjust_symtab
12457 routine, while leaving them marked. This is convenient for the
12458 debugger and the disassembler. The linker knows to make them odd
12459 again. */
12460
12461static void
17a2f251 12462s_insn (int ignore ATTRIBUTE_UNUSED)
252b5132 12463{
f9419b05 12464 mips16_mark_labels ();
252b5132
RH
12465
12466 demand_empty_rest_of_line ();
12467}
12468
12469/* Handle a .stabn directive. We need these in order to mark a label
12470 as being a mips16 text label correctly. Sometimes the compiler
12471 will emit a label, followed by a .stabn, and then switch sections.
12472 If the label and .stabn are in mips16 mode, then the label is
12473 really a mips16 text label. */
12474
12475static void
17a2f251 12476s_mips_stab (int type)
252b5132 12477{
f9419b05 12478 if (type == 'n')
252b5132
RH
12479 mips16_mark_labels ();
12480
12481 s_stab (type);
12482}
12483
12484/* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
12485 */
12486
12487static void
17a2f251 12488s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
12489{
12490 char *name;
12491 int c;
12492 symbolS *symbolP;
12493 expressionS exp;
12494
12495 name = input_line_pointer;
12496 c = get_symbol_end ();
12497 symbolP = symbol_find_or_make (name);
12498 S_SET_WEAK (symbolP);
12499 *input_line_pointer = c;
12500
12501 SKIP_WHITESPACE ();
12502
12503 if (! is_end_of_line[(unsigned char) *input_line_pointer])
12504 {
12505 if (S_IS_DEFINED (symbolP))
12506 {
956cd1d6 12507 as_bad ("ignoring attempt to redefine symbol %s",
252b5132
RH
12508 S_GET_NAME (symbolP));
12509 ignore_rest_of_line ();
12510 return;
12511 }
bdaaa2e1 12512
252b5132
RH
12513 if (*input_line_pointer == ',')
12514 {
12515 ++input_line_pointer;
12516 SKIP_WHITESPACE ();
12517 }
bdaaa2e1 12518
252b5132
RH
12519 expression (&exp);
12520 if (exp.X_op != O_symbol)
12521 {
12522 as_bad ("bad .weakext directive");
98d3f06f 12523 ignore_rest_of_line ();
252b5132
RH
12524 return;
12525 }
49309057 12526 symbol_set_value_expression (symbolP, &exp);
252b5132
RH
12527 }
12528
12529 demand_empty_rest_of_line ();
12530}
12531
12532/* Parse a register string into a number. Called from the ECOFF code
12533 to parse .frame. The argument is non-zero if this is the frame
12534 register, so that we can record it in mips_frame_reg. */
12535
12536int
17a2f251 12537tc_get_register (int frame)
252b5132
RH
12538{
12539 int reg;
12540
12541 SKIP_WHITESPACE ();
12542 if (*input_line_pointer++ != '$')
12543 {
12544 as_warn (_("expected `$'"));
85b51719 12545 reg = ZERO;
252b5132 12546 }
3882b010 12547 else if (ISDIGIT (*input_line_pointer))
252b5132
RH
12548 {
12549 reg = get_absolute_expression ();
12550 if (reg < 0 || reg >= 32)
12551 {
12552 as_warn (_("Bad register number"));
85b51719 12553 reg = ZERO;
252b5132
RH
12554 }
12555 }
12556 else
12557 {
76db943d 12558 if (strncmp (input_line_pointer, "ra", 2) == 0)
85b51719
TS
12559 {
12560 reg = RA;
12561 input_line_pointer += 2;
12562 }
76db943d 12563 else if (strncmp (input_line_pointer, "fp", 2) == 0)
85b51719
TS
12564 {
12565 reg = FP;
12566 input_line_pointer += 2;
12567 }
252b5132 12568 else if (strncmp (input_line_pointer, "sp", 2) == 0)
85b51719
TS
12569 {
12570 reg = SP;
12571 input_line_pointer += 2;
12572 }
252b5132 12573 else if (strncmp (input_line_pointer, "gp", 2) == 0)
85b51719
TS
12574 {
12575 reg = GP;
12576 input_line_pointer += 2;
12577 }
252b5132 12578 else if (strncmp (input_line_pointer, "at", 2) == 0)
85b51719
TS
12579 {
12580 reg = AT;
12581 input_line_pointer += 2;
12582 }
12583 else if (strncmp (input_line_pointer, "kt0", 3) == 0)
12584 {
12585 reg = KT0;
12586 input_line_pointer += 3;
12587 }
12588 else if (strncmp (input_line_pointer, "kt1", 3) == 0)
12589 {
12590 reg = KT1;
12591 input_line_pointer += 3;
12592 }
12593 else if (strncmp (input_line_pointer, "zero", 4) == 0)
12594 {
12595 reg = ZERO;
12596 input_line_pointer += 4;
12597 }
252b5132
RH
12598 else
12599 {
12600 as_warn (_("Unrecognized register name"));
85b51719
TS
12601 reg = ZERO;
12602 while (ISALNUM(*input_line_pointer))
12603 input_line_pointer++;
252b5132 12604 }
252b5132
RH
12605 }
12606 if (frame)
7a621144
DJ
12607 {
12608 mips_frame_reg = reg != 0 ? reg : SP;
12609 mips_frame_reg_valid = 1;
12610 mips_cprestore_valid = 0;
12611 }
252b5132
RH
12612 return reg;
12613}
12614
12615valueT
17a2f251 12616md_section_align (asection *seg, valueT addr)
252b5132
RH
12617{
12618 int align = bfd_get_section_alignment (stdoutput, seg);
12619
12620#ifdef OBJ_ELF
12621 /* We don't need to align ELF sections to the full alignment.
12622 However, Irix 5 may prefer that we align them at least to a 16
12623 byte boundary. We don't bother to align the sections if we are
12624 targeted for an embedded system. */
12625 if (strcmp (TARGET_OS, "elf") == 0)
12626 return addr;
12627 if (align > 4)
12628 align = 4;
12629#endif
12630
12631 return ((addr + (1 << align) - 1) & (-1 << align));
12632}
12633
12634/* Utility routine, called from above as well. If called while the
12635 input file is still being read, it's only an approximation. (For
12636 example, a symbol may later become defined which appeared to be
12637 undefined earlier.) */
12638
12639static int
17a2f251 12640nopic_need_relax (symbolS *sym, int before_relaxing)
252b5132
RH
12641{
12642 if (sym == 0)
12643 return 0;
12644
4d0d148d 12645 if (g_switch_value > 0)
252b5132
RH
12646 {
12647 const char *symname;
12648 int change;
12649
c9914766 12650 /* Find out whether this symbol can be referenced off the $gp
252b5132
RH
12651 register. It can be if it is smaller than the -G size or if
12652 it is in the .sdata or .sbss section. Certain symbols can
c9914766 12653 not be referenced off the $gp, although it appears as though
252b5132
RH
12654 they can. */
12655 symname = S_GET_NAME (sym);
12656 if (symname != (const char *) NULL
12657 && (strcmp (symname, "eprol") == 0
12658 || strcmp (symname, "etext") == 0
12659 || strcmp (symname, "_gp") == 0
12660 || strcmp (symname, "edata") == 0
12661 || strcmp (symname, "_fbss") == 0
12662 || strcmp (symname, "_fdata") == 0
12663 || strcmp (symname, "_ftext") == 0
12664 || strcmp (symname, "end") == 0
12665 || strcmp (symname, "_gp_disp") == 0))
12666 change = 1;
12667 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
12668 && (0
12669#ifndef NO_ECOFF_DEBUGGING
49309057
ILT
12670 || (symbol_get_obj (sym)->ecoff_extern_size != 0
12671 && (symbol_get_obj (sym)->ecoff_extern_size
12672 <= g_switch_value))
252b5132
RH
12673#endif
12674 /* We must defer this decision until after the whole
12675 file has been read, since there might be a .extern
12676 after the first use of this symbol. */
12677 || (before_relaxing
12678#ifndef NO_ECOFF_DEBUGGING
49309057 12679 && symbol_get_obj (sym)->ecoff_extern_size == 0
252b5132
RH
12680#endif
12681 && S_GET_VALUE (sym) == 0)
12682 || (S_GET_VALUE (sym) != 0
12683 && S_GET_VALUE (sym) <= g_switch_value)))
12684 change = 0;
12685 else
12686 {
12687 const char *segname;
12688
12689 segname = segment_name (S_GET_SEGMENT (sym));
12690 assert (strcmp (segname, ".lit8") != 0
12691 && strcmp (segname, ".lit4") != 0);
12692 change = (strcmp (segname, ".sdata") != 0
fba2b7f9
GK
12693 && strcmp (segname, ".sbss") != 0
12694 && strncmp (segname, ".sdata.", 7) != 0
12695 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
252b5132
RH
12696 }
12697 return change;
12698 }
12699 else
c9914766 12700 /* We are not optimizing for the $gp register. */
252b5132
RH
12701 return 1;
12702}
12703
5919d012
RS
12704
12705/* Return true if the given symbol should be considered local for SVR4 PIC. */
12706
12707static bfd_boolean
17a2f251 12708pic_need_relax (symbolS *sym, asection *segtype)
5919d012
RS
12709{
12710 asection *symsec;
12711 bfd_boolean linkonce;
12712
12713 /* Handle the case of a symbol equated to another symbol. */
12714 while (symbol_equated_reloc_p (sym))
12715 {
12716 symbolS *n;
12717
12718 /* It's possible to get a loop here in a badly written
12719 program. */
12720 n = symbol_get_value_expression (sym)->X_add_symbol;
12721 if (n == sym)
12722 break;
12723 sym = n;
12724 }
12725
12726 symsec = S_GET_SEGMENT (sym);
12727
12728 /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
12729 linkonce = FALSE;
12730 if (symsec != segtype && ! S_IS_LOCAL (sym))
12731 {
12732 if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
12733 != 0)
12734 linkonce = TRUE;
12735
12736 /* The GNU toolchain uses an extension for ELF: a section
12737 beginning with the magic string .gnu.linkonce is a linkonce
12738 section. */
12739 if (strncmp (segment_name (symsec), ".gnu.linkonce",
12740 sizeof ".gnu.linkonce" - 1) == 0)
12741 linkonce = TRUE;
12742 }
12743
12744 /* This must duplicate the test in adjust_reloc_syms. */
12745 return (symsec != &bfd_und_section
12746 && symsec != &bfd_abs_section
12747 && ! bfd_is_com_section (symsec)
12748 && !linkonce
12749#ifdef OBJ_ELF
12750 /* A global or weak symbol is treated as external. */
12751 && (OUTPUT_FLAVOR != bfd_target_elf_flavour
3e722fb5 12752 || (! S_IS_WEAK (sym) && ! S_IS_EXTERNAL (sym)))
5919d012
RS
12753#endif
12754 );
12755}
12756
12757
252b5132
RH
12758/* Given a mips16 variant frag FRAGP, return non-zero if it needs an
12759 extended opcode. SEC is the section the frag is in. */
12760
12761static int
17a2f251 12762mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
252b5132
RH
12763{
12764 int type;
12765 register const struct mips16_immed_operand *op;
12766 offsetT val;
12767 int mintiny, maxtiny;
12768 segT symsec;
98aa84af 12769 fragS *sym_frag;
252b5132
RH
12770
12771 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
12772 return 0;
12773 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
12774 return 1;
12775
12776 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12777 op = mips16_immed_operands;
12778 while (op->type != type)
12779 {
12780 ++op;
12781 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
12782 }
12783
12784 if (op->unsp)
12785 {
12786 if (type == '<' || type == '>' || type == '[' || type == ']')
12787 {
12788 mintiny = 1;
12789 maxtiny = 1 << op->nbits;
12790 }
12791 else
12792 {
12793 mintiny = 0;
12794 maxtiny = (1 << op->nbits) - 1;
12795 }
12796 }
12797 else
12798 {
12799 mintiny = - (1 << (op->nbits - 1));
12800 maxtiny = (1 << (op->nbits - 1)) - 1;
12801 }
12802
98aa84af 12803 sym_frag = symbol_get_frag (fragp->fr_symbol);
ac62c346 12804 val = S_GET_VALUE (fragp->fr_symbol);
98aa84af 12805 symsec = S_GET_SEGMENT (fragp->fr_symbol);
252b5132
RH
12806
12807 if (op->pcrel)
12808 {
12809 addressT addr;
12810
12811 /* We won't have the section when we are called from
12812 mips_relax_frag. However, we will always have been called
12813 from md_estimate_size_before_relax first. If this is a
12814 branch to a different section, we mark it as such. If SEC is
12815 NULL, and the frag is not marked, then it must be a branch to
12816 the same section. */
12817 if (sec == NULL)
12818 {
12819 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
12820 return 1;
12821 }
12822 else
12823 {
98aa84af 12824 /* Must have been called from md_estimate_size_before_relax. */
252b5132
RH
12825 if (symsec != sec)
12826 {
12827 fragp->fr_subtype =
12828 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12829
12830 /* FIXME: We should support this, and let the linker
12831 catch branches and loads that are out of range. */
12832 as_bad_where (fragp->fr_file, fragp->fr_line,
12833 _("unsupported PC relative reference to different section"));
12834
12835 return 1;
12836 }
98aa84af
AM
12837 if (fragp != sym_frag && sym_frag->fr_address == 0)
12838 /* Assume non-extended on the first relaxation pass.
12839 The address we have calculated will be bogus if this is
12840 a forward branch to another frag, as the forward frag
12841 will have fr_address == 0. */
12842 return 0;
252b5132
RH
12843 }
12844
12845 /* In this case, we know for sure that the symbol fragment is in
98aa84af
AM
12846 the same section. If the relax_marker of the symbol fragment
12847 differs from the relax_marker of this fragment, we have not
12848 yet adjusted the symbol fragment fr_address. We want to add
252b5132
RH
12849 in STRETCH in order to get a better estimate of the address.
12850 This particularly matters because of the shift bits. */
12851 if (stretch != 0
98aa84af 12852 && sym_frag->relax_marker != fragp->relax_marker)
252b5132
RH
12853 {
12854 fragS *f;
12855
12856 /* Adjust stretch for any alignment frag. Note that if have
12857 been expanding the earlier code, the symbol may be
12858 defined in what appears to be an earlier frag. FIXME:
12859 This doesn't handle the fr_subtype field, which specifies
12860 a maximum number of bytes to skip when doing an
12861 alignment. */
98aa84af 12862 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
252b5132
RH
12863 {
12864 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
12865 {
12866 if (stretch < 0)
12867 stretch = - ((- stretch)
12868 & ~ ((1 << (int) f->fr_offset) - 1));
12869 else
12870 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
12871 if (stretch == 0)
12872 break;
12873 }
12874 }
12875 if (f != NULL)
12876 val += stretch;
12877 }
12878
12879 addr = fragp->fr_address + fragp->fr_fix;
12880
12881 /* The base address rules are complicated. The base address of
12882 a branch is the following instruction. The base address of a
12883 PC relative load or add is the instruction itself, but if it
12884 is in a delay slot (in which case it can not be extended) use
12885 the address of the instruction whose delay slot it is in. */
12886 if (type == 'p' || type == 'q')
12887 {
12888 addr += 2;
12889
12890 /* If we are currently assuming that this frag should be
12891 extended, then, the current address is two bytes
bdaaa2e1 12892 higher. */
252b5132
RH
12893 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12894 addr += 2;
12895
12896 /* Ignore the low bit in the target, since it will be set
12897 for a text label. */
12898 if ((val & 1) != 0)
12899 --val;
12900 }
12901 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12902 addr -= 4;
12903 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12904 addr -= 2;
12905
12906 val -= addr & ~ ((1 << op->shift) - 1);
12907
12908 /* Branch offsets have an implicit 0 in the lowest bit. */
12909 if (type == 'p' || type == 'q')
12910 val /= 2;
12911
12912 /* If any of the shifted bits are set, we must use an extended
12913 opcode. If the address depends on the size of this
12914 instruction, this can lead to a loop, so we arrange to always
12915 use an extended opcode. We only check this when we are in
12916 the main relaxation loop, when SEC is NULL. */
12917 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
12918 {
12919 fragp->fr_subtype =
12920 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12921 return 1;
12922 }
12923
12924 /* If we are about to mark a frag as extended because the value
12925 is precisely maxtiny + 1, then there is a chance of an
12926 infinite loop as in the following code:
12927 la $4,foo
12928 .skip 1020
12929 .align 2
12930 foo:
12931 In this case when the la is extended, foo is 0x3fc bytes
12932 away, so the la can be shrunk, but then foo is 0x400 away, so
12933 the la must be extended. To avoid this loop, we mark the
12934 frag as extended if it was small, and is about to become
12935 extended with a value of maxtiny + 1. */
12936 if (val == ((maxtiny + 1) << op->shift)
12937 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
12938 && sec == NULL)
12939 {
12940 fragp->fr_subtype =
12941 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12942 return 1;
12943 }
12944 }
12945 else if (symsec != absolute_section && sec != NULL)
12946 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
12947
12948 if ((val & ((1 << op->shift) - 1)) != 0
12949 || val < (mintiny << op->shift)
12950 || val > (maxtiny << op->shift))
12951 return 1;
12952 else
12953 return 0;
12954}
12955
4a6a3df4
AO
12956/* Compute the length of a branch sequence, and adjust the
12957 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
12958 worst-case length is computed, with UPDATE being used to indicate
12959 whether an unconditional (-1), branch-likely (+1) or regular (0)
12960 branch is to be computed. */
12961static int
17a2f251 12962relaxed_branch_length (fragS *fragp, asection *sec, int update)
4a6a3df4 12963{
b34976b6 12964 bfd_boolean toofar;
4a6a3df4
AO
12965 int length;
12966
12967 if (fragp
12968 && S_IS_DEFINED (fragp->fr_symbol)
12969 && sec == S_GET_SEGMENT (fragp->fr_symbol))
12970 {
12971 addressT addr;
12972 offsetT val;
12973
12974 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
12975
12976 addr = fragp->fr_address + fragp->fr_fix + 4;
12977
12978 val -= addr;
12979
12980 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
12981 }
12982 else if (fragp)
12983 /* If the symbol is not defined or it's in a different segment,
12984 assume the user knows what's going on and emit a short
12985 branch. */
b34976b6 12986 toofar = FALSE;
4a6a3df4 12987 else
b34976b6 12988 toofar = TRUE;
4a6a3df4
AO
12989
12990 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
12991 fragp->fr_subtype
af6ae2ad 12992 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_UNCOND (fragp->fr_subtype),
4a6a3df4
AO
12993 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
12994 RELAX_BRANCH_LINK (fragp->fr_subtype),
12995 toofar);
12996
12997 length = 4;
12998 if (toofar)
12999 {
13000 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
13001 length += 8;
13002
13003 if (mips_pic != NO_PIC)
13004 {
13005 /* Additional space for PIC loading of target address. */
13006 length += 8;
13007 if (mips_opts.isa == ISA_MIPS1)
13008 /* Additional space for $at-stabilizing nop. */
13009 length += 4;
13010 }
13011
13012 /* If branch is conditional. */
13013 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
13014 length += 8;
13015 }
b34976b6 13016
4a6a3df4
AO
13017 return length;
13018}
13019
252b5132
RH
13020/* Estimate the size of a frag before relaxing. Unless this is the
13021 mips16, we are not really relaxing here, and the final size is
13022 encoded in the subtype information. For the mips16, we have to
13023 decide whether we are using an extended opcode or not. */
13024
252b5132 13025int
17a2f251 13026md_estimate_size_before_relax (fragS *fragp, asection *segtype)
252b5132 13027{
5919d012 13028 int change;
252b5132 13029
4a6a3df4
AO
13030 if (RELAX_BRANCH_P (fragp->fr_subtype))
13031 {
13032
b34976b6
AM
13033 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
13034
4a6a3df4
AO
13035 return fragp->fr_var;
13036 }
13037
252b5132 13038 if (RELAX_MIPS16_P (fragp->fr_subtype))
177b4a6a
AO
13039 /* We don't want to modify the EXTENDED bit here; it might get us
13040 into infinite loops. We change it only in mips_relax_frag(). */
13041 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
252b5132
RH
13042
13043 if (mips_pic == NO_PIC)
5919d012 13044 change = nopic_need_relax (fragp->fr_symbol, 0);
252b5132 13045 else if (mips_pic == SVR4_PIC)
5919d012 13046 change = pic_need_relax (fragp->fr_symbol, segtype);
252b5132
RH
13047 else
13048 abort ();
13049
13050 if (change)
13051 {
4d7206a2 13052 fragp->fr_subtype |= RELAX_USE_SECOND;
4d7206a2 13053 return -RELAX_FIRST (fragp->fr_subtype);
252b5132 13054 }
4d7206a2
RS
13055 else
13056 return -RELAX_SECOND (fragp->fr_subtype);
252b5132
RH
13057}
13058
13059/* This is called to see whether a reloc against a defined symbol
de7e6852 13060 should be converted into a reloc against a section. */
252b5132
RH
13061
13062int
17a2f251 13063mips_fix_adjustable (fixS *fixp)
252b5132 13064{
de7e6852
RS
13065 /* Don't adjust MIPS16 jump relocations, so we don't have to worry
13066 about the format of the offset in the .o file. */
252b5132
RH
13067 if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
13068 return 0;
a161fe53 13069
252b5132
RH
13070 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
13071 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13072 return 0;
a161fe53 13073
252b5132
RH
13074 if (fixp->fx_addsy == NULL)
13075 return 1;
a161fe53 13076
de7e6852
RS
13077 /* If symbol SYM is in a mergeable section, relocations of the form
13078 SYM + 0 can usually be made section-relative. The mergeable data
13079 is then identified by the section offset rather than by the symbol.
13080
13081 However, if we're generating REL LO16 relocations, the offset is split
13082 between the LO16 and parterning high part relocation. The linker will
13083 need to recalculate the complete offset in order to correctly identify
13084 the merge data.
13085
13086 The linker has traditionally not looked for the parterning high part
13087 relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
13088 placed anywhere. Rather than break backwards compatibility by changing
13089 this, it seems better not to force the issue, and instead keep the
13090 original symbol. This will work with either linker behavior. */
704803a9
MR
13091 if ((fixp->fx_r_type == BFD_RELOC_LO16
13092 || fixp->fx_r_type == BFD_RELOC_MIPS16_LO16
13093 || reloc_needs_lo_p (fixp->fx_r_type))
de7e6852
RS
13094 && HAVE_IN_PLACE_ADDENDS
13095 && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
13096 return 0;
13097
252b5132 13098#ifdef OBJ_ELF
de7e6852
RS
13099 /* Don't adjust relocations against mips16 symbols, so that the linker
13100 can find them if it needs to set up a stub. */
252b5132
RH
13101 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
13102 && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
13103 && fixp->fx_subsy == NULL)
13104 return 0;
13105#endif
a161fe53 13106
252b5132
RH
13107 return 1;
13108}
13109
13110/* Translate internal representation of relocation info to BFD target
13111 format. */
13112
13113arelent **
17a2f251 13114tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
13115{
13116 static arelent *retval[4];
13117 arelent *reloc;
13118 bfd_reloc_code_real_type code;
13119
4b0cff4e
TS
13120 memset (retval, 0, sizeof(retval));
13121 reloc = retval[0] = (arelent *) xcalloc (1, sizeof (arelent));
49309057
ILT
13122 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13123 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
13124 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
13125
3e722fb5
CD
13126 assert (! fixp->fx_pcrel);
13127 reloc->addend = fixp->fx_addnumber;
252b5132 13128
438c16b8
TS
13129 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
13130 entry to be used in the relocation's section offset. */
13131 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
252b5132
RH
13132 {
13133 reloc->address = reloc->addend;
13134 reloc->addend = 0;
13135 }
13136
252b5132 13137 code = fixp->fx_r_type;
252b5132 13138
3e722fb5
CD
13139 /* To support a PC relative reloc, we used a Cygnus extension.
13140 We check for that here to make sure that we don't let such a
13141 reloc escape normally. (FIXME: This was formerly used by
13142 embedded-PIC support, but is now used by branch handling in
13143 general. That probably should be fixed.) */
0b25d3e6
AO
13144 if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
13145 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
3e722fb5 13146 && code == BFD_RELOC_16_PCREL_S2)
0b25d3e6
AO
13147 reloc->howto = NULL;
13148 else
13149 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
13150
252b5132
RH
13151 if (reloc->howto == NULL)
13152 {
13153 as_bad_where (fixp->fx_file, fixp->fx_line,
13154 _("Can not represent %s relocation in this object file format"),
13155 bfd_get_reloc_code_name (code));
13156 retval[0] = NULL;
13157 }
13158
13159 return retval;
13160}
13161
13162/* Relax a machine dependent frag. This returns the amount by which
13163 the current size of the frag should change. */
13164
13165int
17a2f251 13166mips_relax_frag (asection *sec, fragS *fragp, long stretch)
252b5132 13167{
4a6a3df4
AO
13168 if (RELAX_BRANCH_P (fragp->fr_subtype))
13169 {
13170 offsetT old_var = fragp->fr_var;
b34976b6
AM
13171
13172 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
4a6a3df4
AO
13173
13174 return fragp->fr_var - old_var;
13175 }
13176
252b5132
RH
13177 if (! RELAX_MIPS16_P (fragp->fr_subtype))
13178 return 0;
13179
c4e7957c 13180 if (mips16_extended_frag (fragp, NULL, stretch))
252b5132
RH
13181 {
13182 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13183 return 0;
13184 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
13185 return 2;
13186 }
13187 else
13188 {
13189 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13190 return 0;
13191 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
13192 return -2;
13193 }
13194
13195 return 0;
13196}
13197
13198/* Convert a machine dependent frag. */
13199
13200void
17a2f251 13201md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
252b5132 13202{
4a6a3df4
AO
13203 if (RELAX_BRANCH_P (fragp->fr_subtype))
13204 {
13205 bfd_byte *buf;
13206 unsigned long insn;
13207 expressionS exp;
13208 fixS *fixp;
b34976b6 13209
4a6a3df4
AO
13210 buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
13211
13212 if (target_big_endian)
13213 insn = bfd_getb32 (buf);
13214 else
13215 insn = bfd_getl32 (buf);
b34976b6 13216
4a6a3df4
AO
13217 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13218 {
13219 /* We generate a fixup instead of applying it right now
13220 because, if there are linker relaxations, we're going to
13221 need the relocations. */
13222 exp.X_op = O_symbol;
13223 exp.X_add_symbol = fragp->fr_symbol;
13224 exp.X_add_number = fragp->fr_offset;
13225
13226 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
0b25d3e6
AO
13227 4, &exp, 1,
13228 BFD_RELOC_16_PCREL_S2);
4a6a3df4
AO
13229 fixp->fx_file = fragp->fr_file;
13230 fixp->fx_line = fragp->fr_line;
b34976b6 13231
2132e3a3 13232 md_number_to_chars ((char *) buf, insn, 4);
4a6a3df4
AO
13233 buf += 4;
13234 }
13235 else
13236 {
13237 int i;
13238
13239 as_warn_where (fragp->fr_file, fragp->fr_line,
13240 _("relaxed out-of-range branch into a jump"));
13241
13242 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
13243 goto uncond;
13244
13245 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13246 {
13247 /* Reverse the branch. */
13248 switch ((insn >> 28) & 0xf)
13249 {
13250 case 4:
13251 /* bc[0-3][tf]l? and bc1any[24][ft] instructions can
13252 have the condition reversed by tweaking a single
13253 bit, and their opcodes all have 0x4???????. */
13254 assert ((insn & 0xf1000000) == 0x41000000);
13255 insn ^= 0x00010000;
13256 break;
13257
13258 case 0:
13259 /* bltz 0x04000000 bgez 0x04010000
13260 bltzal 0x04100000 bgezal 0x04110000 */
13261 assert ((insn & 0xfc0e0000) == 0x04000000);
13262 insn ^= 0x00010000;
13263 break;
b34976b6 13264
4a6a3df4
AO
13265 case 1:
13266 /* beq 0x10000000 bne 0x14000000
13267 blez 0x18000000 bgtz 0x1c000000 */
13268 insn ^= 0x04000000;
13269 break;
13270
13271 default:
13272 abort ();
13273 }
13274 }
13275
13276 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13277 {
13278 /* Clear the and-link bit. */
13279 assert ((insn & 0xfc1c0000) == 0x04100000);
13280
13281 /* bltzal 0x04100000 bgezal 0x04110000
13282 bltzall 0x04120000 bgezall 0x04130000 */
13283 insn &= ~0x00100000;
13284 }
13285
13286 /* Branch over the branch (if the branch was likely) or the
13287 full jump (not likely case). Compute the offset from the
13288 current instruction to branch to. */
13289 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13290 i = 16;
13291 else
13292 {
13293 /* How many bytes in instructions we've already emitted? */
13294 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13295 /* How many bytes in instructions from here to the end? */
13296 i = fragp->fr_var - i;
13297 }
13298 /* Convert to instruction count. */
13299 i >>= 2;
13300 /* Branch counts from the next instruction. */
b34976b6 13301 i--;
4a6a3df4
AO
13302 insn |= i;
13303 /* Branch over the jump. */
2132e3a3 13304 md_number_to_chars ((char *) buf, insn, 4);
4a6a3df4
AO
13305 buf += 4;
13306
13307 /* Nop */
2132e3a3 13308 md_number_to_chars ((char *) buf, 0, 4);
4a6a3df4
AO
13309 buf += 4;
13310
13311 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13312 {
13313 /* beql $0, $0, 2f */
13314 insn = 0x50000000;
13315 /* Compute the PC offset from the current instruction to
13316 the end of the variable frag. */
13317 /* How many bytes in instructions we've already emitted? */
13318 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13319 /* How many bytes in instructions from here to the end? */
13320 i = fragp->fr_var - i;
13321 /* Convert to instruction count. */
13322 i >>= 2;
13323 /* Don't decrement i, because we want to branch over the
13324 delay slot. */
13325
13326 insn |= i;
2132e3a3 13327 md_number_to_chars ((char *) buf, insn, 4);
4a6a3df4
AO
13328 buf += 4;
13329
2132e3a3 13330 md_number_to_chars ((char *) buf, 0, 4);
4a6a3df4
AO
13331 buf += 4;
13332 }
13333
13334 uncond:
13335 if (mips_pic == NO_PIC)
13336 {
13337 /* j or jal. */
13338 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
13339 ? 0x0c000000 : 0x08000000);
13340 exp.X_op = O_symbol;
13341 exp.X_add_symbol = fragp->fr_symbol;
13342 exp.X_add_number = fragp->fr_offset;
13343
13344 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13345 4, &exp, 0, BFD_RELOC_MIPS_JMP);
13346 fixp->fx_file = fragp->fr_file;
13347 fixp->fx_line = fragp->fr_line;
13348
2132e3a3 13349 md_number_to_chars ((char *) buf, insn, 4);
4a6a3df4
AO
13350 buf += 4;
13351 }
13352 else
13353 {
13354 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
13355 insn = HAVE_64BIT_ADDRESSES ? 0xdf810000 : 0x8f810000;
13356 exp.X_op = O_symbol;
13357 exp.X_add_symbol = fragp->fr_symbol;
13358 exp.X_add_number = fragp->fr_offset;
13359
13360 if (fragp->fr_offset)
13361 {
13362 exp.X_add_symbol = make_expr_symbol (&exp);
13363 exp.X_add_number = 0;
13364 }
13365
13366 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13367 4, &exp, 0, BFD_RELOC_MIPS_GOT16);
13368 fixp->fx_file = fragp->fr_file;
13369 fixp->fx_line = fragp->fr_line;
13370
2132e3a3 13371 md_number_to_chars ((char *) buf, insn, 4);
4a6a3df4 13372 buf += 4;
b34976b6 13373
4a6a3df4
AO
13374 if (mips_opts.isa == ISA_MIPS1)
13375 {
13376 /* nop */
2132e3a3 13377 md_number_to_chars ((char *) buf, 0, 4);
4a6a3df4
AO
13378 buf += 4;
13379 }
13380
13381 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
13382 insn = HAVE_64BIT_ADDRESSES ? 0x64210000 : 0x24210000;
13383
13384 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13385 4, &exp, 0, BFD_RELOC_LO16);
13386 fixp->fx_file = fragp->fr_file;
13387 fixp->fx_line = fragp->fr_line;
b34976b6 13388
2132e3a3 13389 md_number_to_chars ((char *) buf, insn, 4);
4a6a3df4
AO
13390 buf += 4;
13391
13392 /* j(al)r $at. */
13393 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13394 insn = 0x0020f809;
13395 else
13396 insn = 0x00200008;
13397
2132e3a3 13398 md_number_to_chars ((char *) buf, insn, 4);
4a6a3df4
AO
13399 buf += 4;
13400 }
13401 }
13402
13403 assert (buf == (bfd_byte *)fragp->fr_literal
13404 + fragp->fr_fix + fragp->fr_var);
13405
13406 fragp->fr_fix += fragp->fr_var;
13407
13408 return;
13409 }
13410
252b5132
RH
13411 if (RELAX_MIPS16_P (fragp->fr_subtype))
13412 {
13413 int type;
13414 register const struct mips16_immed_operand *op;
b34976b6 13415 bfd_boolean small, ext;
252b5132
RH
13416 offsetT val;
13417 bfd_byte *buf;
13418 unsigned long insn;
b34976b6 13419 bfd_boolean use_extend;
252b5132
RH
13420 unsigned short extend;
13421
13422 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
13423 op = mips16_immed_operands;
13424 while (op->type != type)
13425 ++op;
13426
13427 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13428 {
b34976b6
AM
13429 small = FALSE;
13430 ext = TRUE;
252b5132
RH
13431 }
13432 else
13433 {
b34976b6
AM
13434 small = TRUE;
13435 ext = FALSE;
252b5132
RH
13436 }
13437
6386f3a7 13438 resolve_symbol_value (fragp->fr_symbol);
252b5132
RH
13439 val = S_GET_VALUE (fragp->fr_symbol);
13440 if (op->pcrel)
13441 {
13442 addressT addr;
13443
13444 addr = fragp->fr_address + fragp->fr_fix;
13445
13446 /* The rules for the base address of a PC relative reloc are
13447 complicated; see mips16_extended_frag. */
13448 if (type == 'p' || type == 'q')
13449 {
13450 addr += 2;
13451 if (ext)
13452 addr += 2;
13453 /* Ignore the low bit in the target, since it will be
13454 set for a text label. */
13455 if ((val & 1) != 0)
13456 --val;
13457 }
13458 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13459 addr -= 4;
13460 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13461 addr -= 2;
13462
13463 addr &= ~ (addressT) ((1 << op->shift) - 1);
13464 val -= addr;
13465
13466 /* Make sure the section winds up with the alignment we have
13467 assumed. */
13468 if (op->shift > 0)
13469 record_alignment (asec, op->shift);
13470 }
13471
13472 if (ext
13473 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
13474 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
13475 as_warn_where (fragp->fr_file, fragp->fr_line,
13476 _("extended instruction in delay slot"));
13477
13478 buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
13479
13480 if (target_big_endian)
13481 insn = bfd_getb16 (buf);
13482 else
13483 insn = bfd_getl16 (buf);
13484
13485 mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
13486 RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
13487 small, ext, &insn, &use_extend, &extend);
13488
13489 if (use_extend)
13490 {
2132e3a3 13491 md_number_to_chars ((char *) buf, 0xf000 | extend, 2);
252b5132
RH
13492 fragp->fr_fix += 2;
13493 buf += 2;
13494 }
13495
2132e3a3 13496 md_number_to_chars ((char *) buf, insn, 2);
252b5132
RH
13497 fragp->fr_fix += 2;
13498 buf += 2;
13499 }
13500 else
13501 {
4d7206a2
RS
13502 int first, second;
13503 fixS *fixp;
252b5132 13504
4d7206a2
RS
13505 first = RELAX_FIRST (fragp->fr_subtype);
13506 second = RELAX_SECOND (fragp->fr_subtype);
13507 fixp = (fixS *) fragp->fr_opcode;
252b5132 13508
584892a6
RS
13509 /* Possibly emit a warning if we've chosen the longer option. */
13510 if (((fragp->fr_subtype & RELAX_USE_SECOND) != 0)
13511 == ((fragp->fr_subtype & RELAX_SECOND_LONGER) != 0))
13512 {
13513 const char *msg = macro_warning (fragp->fr_subtype);
13514 if (msg != 0)
13515 as_warn_where (fragp->fr_file, fragp->fr_line, msg);
13516 }
13517
4d7206a2
RS
13518 /* Go through all the fixups for the first sequence. Disable them
13519 (by marking them as done) if we're going to use the second
13520 sequence instead. */
13521 while (fixp
13522 && fixp->fx_frag == fragp
13523 && fixp->fx_where < fragp->fr_fix - second)
13524 {
13525 if (fragp->fr_subtype & RELAX_USE_SECOND)
13526 fixp->fx_done = 1;
13527 fixp = fixp->fx_next;
13528 }
252b5132 13529
4d7206a2
RS
13530 /* Go through the fixups for the second sequence. Disable them if
13531 we're going to use the first sequence, otherwise adjust their
13532 addresses to account for the relaxation. */
13533 while (fixp && fixp->fx_frag == fragp)
13534 {
13535 if (fragp->fr_subtype & RELAX_USE_SECOND)
13536 fixp->fx_where -= first;
13537 else
13538 fixp->fx_done = 1;
13539 fixp = fixp->fx_next;
13540 }
13541
13542 /* Now modify the frag contents. */
13543 if (fragp->fr_subtype & RELAX_USE_SECOND)
13544 {
13545 char *start;
13546
13547 start = fragp->fr_literal + fragp->fr_fix - first - second;
13548 memmove (start, start + first, second);
13549 fragp->fr_fix -= first;
13550 }
13551 else
13552 fragp->fr_fix -= second;
252b5132
RH
13553 }
13554}
13555
13556#ifdef OBJ_ELF
13557
13558/* This function is called after the relocs have been generated.
13559 We've been storing mips16 text labels as odd. Here we convert them
13560 back to even for the convenience of the debugger. */
13561
13562void
17a2f251 13563mips_frob_file_after_relocs (void)
252b5132
RH
13564{
13565 asymbol **syms;
13566 unsigned int count, i;
13567
13568 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
13569 return;
13570
13571 syms = bfd_get_outsymbols (stdoutput);
13572 count = bfd_get_symcount (stdoutput);
13573 for (i = 0; i < count; i++, syms++)
13574 {
13575 if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
13576 && ((*syms)->value & 1) != 0)
13577 {
13578 (*syms)->value &= ~1;
13579 /* If the symbol has an odd size, it was probably computed
13580 incorrectly, so adjust that as well. */
13581 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
13582 ++elf_symbol (*syms)->internal_elf_sym.st_size;
13583 }
13584 }
13585}
13586
13587#endif
13588
13589/* This function is called whenever a label is defined. It is used
13590 when handling branch delays; if a branch has a label, we assume we
13591 can not move it. */
13592
13593void
17a2f251 13594mips_define_label (symbolS *sym)
252b5132
RH
13595{
13596 struct insn_label_list *l;
13597
13598 if (free_insn_labels == NULL)
13599 l = (struct insn_label_list *) xmalloc (sizeof *l);
13600 else
13601 {
13602 l = free_insn_labels;
13603 free_insn_labels = l->next;
13604 }
13605
13606 l->label = sym;
13607 l->next = insn_labels;
13608 insn_labels = l;
13609}
13610\f
13611#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13612
13613/* Some special processing for a MIPS ELF file. */
13614
13615void
17a2f251 13616mips_elf_final_processing (void)
252b5132
RH
13617{
13618 /* Write out the register information. */
316f5878 13619 if (mips_abi != N64_ABI)
252b5132
RH
13620 {
13621 Elf32_RegInfo s;
13622
13623 s.ri_gprmask = mips_gprmask;
13624 s.ri_cprmask[0] = mips_cprmask[0];
13625 s.ri_cprmask[1] = mips_cprmask[1];
13626 s.ri_cprmask[2] = mips_cprmask[2];
13627 s.ri_cprmask[3] = mips_cprmask[3];
13628 /* The gp_value field is set by the MIPS ELF backend. */
13629
13630 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
13631 ((Elf32_External_RegInfo *)
13632 mips_regmask_frag));
13633 }
13634 else
13635 {
13636 Elf64_Internal_RegInfo s;
13637
13638 s.ri_gprmask = mips_gprmask;
13639 s.ri_pad = 0;
13640 s.ri_cprmask[0] = mips_cprmask[0];
13641 s.ri_cprmask[1] = mips_cprmask[1];
13642 s.ri_cprmask[2] = mips_cprmask[2];
13643 s.ri_cprmask[3] = mips_cprmask[3];
13644 /* The gp_value field is set by the MIPS ELF backend. */
13645
13646 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
13647 ((Elf64_External_RegInfo *)
13648 mips_regmask_frag));
13649 }
13650
13651 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
13652 sort of BFD interface for this. */
13653 if (mips_any_noreorder)
13654 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
13655 if (mips_pic != NO_PIC)
143d77c5 13656 {
252b5132 13657 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
143d77c5
EC
13658 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
13659 }
13660 if (mips_abicalls)
13661 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
252b5132 13662
98d3f06f 13663 /* Set MIPS ELF flags for ASEs. */
74cd071d
CF
13664 /* We may need to define a new flag for DSP ASE, and set this flag when
13665 file_ase_dsp is true. */
ef2e4d86
CF
13666 /* We may need to define a new flag for MT ASE, and set this flag when
13667 file_ase_mt is true. */
a4672219
TS
13668 if (file_ase_mips16)
13669 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
1f25f5d3
CD
13670#if 0 /* XXX FIXME */
13671 if (file_ase_mips3d)
13672 elf_elfheader (stdoutput)->e_flags |= ???;
13673#endif
deec1734
CD
13674 if (file_ase_mdmx)
13675 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
1f25f5d3 13676
bdaaa2e1 13677 /* Set the MIPS ELF ABI flags. */
316f5878 13678 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
252b5132 13679 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
316f5878 13680 else if (mips_abi == O64_ABI)
252b5132 13681 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
316f5878 13682 else if (mips_abi == EABI_ABI)
252b5132 13683 {
316f5878 13684 if (!file_mips_gp32)
252b5132
RH
13685 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
13686 else
13687 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
13688 }
316f5878 13689 else if (mips_abi == N32_ABI)
be00bddd
TS
13690 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
13691
c9914766 13692 /* Nothing to do for N64_ABI. */
252b5132
RH
13693
13694 if (mips_32bitmode)
13695 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
13696}
13697
13698#endif /* OBJ_ELF || OBJ_MAYBE_ELF */
13699\f
beae10d5 13700typedef struct proc {
9b2f1d35
EC
13701 symbolS *func_sym;
13702 symbolS *func_end_sym;
beae10d5
KH
13703 unsigned long reg_mask;
13704 unsigned long reg_offset;
13705 unsigned long fpreg_mask;
13706 unsigned long fpreg_offset;
13707 unsigned long frame_offset;
13708 unsigned long frame_reg;
13709 unsigned long pc_reg;
13710} procS;
252b5132
RH
13711
13712static procS cur_proc;
13713static procS *cur_proc_ptr;
13714static int numprocs;
13715
0a9ef439 13716/* Fill in an rs_align_code fragment. */
a19d8eb0 13717
0a9ef439 13718void
17a2f251 13719mips_handle_align (fragS *fragp)
a19d8eb0 13720{
0a9ef439
RH
13721 if (fragp->fr_type != rs_align_code)
13722 return;
13723
13724 if (mips_opts.mips16)
a19d8eb0
CP
13725 {
13726 static const unsigned char be_nop[] = { 0x65, 0x00 };
13727 static const unsigned char le_nop[] = { 0x00, 0x65 };
13728
0a9ef439
RH
13729 int bytes;
13730 char *p;
a19d8eb0 13731
0a9ef439
RH
13732 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
13733 p = fragp->fr_literal + fragp->fr_fix;
13734
13735 if (bytes & 1)
13736 {
13737 *p++ = 0;
f9419b05 13738 fragp->fr_fix++;
0a9ef439
RH
13739 }
13740
13741 memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
13742 fragp->fr_var = 2;
a19d8eb0
CP
13743 }
13744
0a9ef439 13745 /* For mips32, a nop is a zero, which we trivially get by doing nothing. */
a19d8eb0
CP
13746}
13747
252b5132 13748static void
17a2f251 13749md_obj_begin (void)
252b5132
RH
13750{
13751}
13752
13753static void
17a2f251 13754md_obj_end (void)
252b5132
RH
13755{
13756 /* check for premature end, nesting errors, etc */
13757 if (cur_proc_ptr)
9a41af64 13758 as_warn (_("missing .end at end of assembly"));
252b5132
RH
13759}
13760
13761static long
17a2f251 13762get_number (void)
252b5132
RH
13763{
13764 int negative = 0;
13765 long val = 0;
13766
13767 if (*input_line_pointer == '-')
13768 {
13769 ++input_line_pointer;
13770 negative = 1;
13771 }
3882b010 13772 if (!ISDIGIT (*input_line_pointer))
956cd1d6 13773 as_bad (_("expected simple number"));
252b5132
RH
13774 if (input_line_pointer[0] == '0')
13775 {
13776 if (input_line_pointer[1] == 'x')
13777 {
13778 input_line_pointer += 2;
3882b010 13779 while (ISXDIGIT (*input_line_pointer))
252b5132
RH
13780 {
13781 val <<= 4;
13782 val |= hex_value (*input_line_pointer++);
13783 }
13784 return negative ? -val : val;
13785 }
13786 else
13787 {
13788 ++input_line_pointer;
3882b010 13789 while (ISDIGIT (*input_line_pointer))
252b5132
RH
13790 {
13791 val <<= 3;
13792 val |= *input_line_pointer++ - '0';
13793 }
13794 return negative ? -val : val;
13795 }
13796 }
3882b010 13797 if (!ISDIGIT (*input_line_pointer))
252b5132
RH
13798 {
13799 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
13800 *input_line_pointer, *input_line_pointer);
956cd1d6 13801 as_warn (_("invalid number"));
252b5132
RH
13802 return -1;
13803 }
3882b010 13804 while (ISDIGIT (*input_line_pointer))
252b5132
RH
13805 {
13806 val *= 10;
13807 val += *input_line_pointer++ - '0';
13808 }
13809 return negative ? -val : val;
13810}
13811
13812/* The .file directive; just like the usual .file directive, but there
c5dd6aab
DJ
13813 is an initial number which is the ECOFF file index. In the non-ECOFF
13814 case .file implies DWARF-2. */
13815
13816static void
17a2f251 13817s_mips_file (int x ATTRIBUTE_UNUSED)
c5dd6aab 13818{
ecb4347a
DJ
13819 static int first_file_directive = 0;
13820
c5dd6aab
DJ
13821 if (ECOFF_DEBUGGING)
13822 {
13823 get_number ();
13824 s_app_file (0);
13825 }
13826 else
ecb4347a
DJ
13827 {
13828 char *filename;
13829
13830 filename = dwarf2_directive_file (0);
13831
13832 /* Versions of GCC up to 3.1 start files with a ".file"
13833 directive even for stabs output. Make sure that this
13834 ".file" is handled. Note that you need a version of GCC
13835 after 3.1 in order to support DWARF-2 on MIPS. */
13836 if (filename != NULL && ! first_file_directive)
13837 {
13838 (void) new_logical_line (filename, -1);
c04f5787 13839 s_app_file_string (filename, 0);
ecb4347a
DJ
13840 }
13841 first_file_directive = 1;
13842 }
c5dd6aab
DJ
13843}
13844
13845/* The .loc directive, implying DWARF-2. */
252b5132
RH
13846
13847static void
17a2f251 13848s_mips_loc (int x ATTRIBUTE_UNUSED)
252b5132 13849{
c5dd6aab
DJ
13850 if (!ECOFF_DEBUGGING)
13851 dwarf2_directive_loc (0);
252b5132
RH
13852}
13853
252b5132
RH
13854/* The .end directive. */
13855
13856static void
17a2f251 13857s_mips_end (int x ATTRIBUTE_UNUSED)
252b5132
RH
13858{
13859 symbolS *p;
252b5132 13860
7a621144
DJ
13861 /* Following functions need their own .frame and .cprestore directives. */
13862 mips_frame_reg_valid = 0;
13863 mips_cprestore_valid = 0;
13864
252b5132
RH
13865 if (!is_end_of_line[(unsigned char) *input_line_pointer])
13866 {
13867 p = get_symbol ();
13868 demand_empty_rest_of_line ();
13869 }
13870 else
13871 p = NULL;
13872
14949570 13873 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
13874 as_warn (_(".end not in text section"));
13875
13876 if (!cur_proc_ptr)
13877 {
13878 as_warn (_(".end directive without a preceding .ent directive."));
13879 demand_empty_rest_of_line ();
13880 return;
13881 }
13882
13883 if (p != NULL)
13884 {
13885 assert (S_GET_NAME (p));
9b2f1d35 13886 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
252b5132 13887 as_warn (_(".end symbol does not match .ent symbol."));
ecb4347a
DJ
13888
13889 if (debug_type == DEBUG_STABS)
13890 stabs_generate_asm_endfunc (S_GET_NAME (p),
13891 S_GET_NAME (p));
252b5132
RH
13892 }
13893 else
13894 as_warn (_(".end directive missing or unknown symbol"));
13895
2132e3a3 13896#ifdef OBJ_ELF
9b2f1d35
EC
13897 /* Create an expression to calculate the size of the function. */
13898 if (p && cur_proc_ptr)
13899 {
13900 OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
13901 expressionS *exp = xmalloc (sizeof (expressionS));
13902
13903 obj->size = exp;
13904 exp->X_op = O_subtract;
13905 exp->X_add_symbol = symbol_temp_new_now ();
13906 exp->X_op_symbol = p;
13907 exp->X_add_number = 0;
13908
13909 cur_proc_ptr->func_end_sym = exp->X_add_symbol;
13910 }
13911
ecb4347a 13912 /* Generate a .pdr section. */
dcd410fe
RO
13913 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING
13914 && mips_flag_pdr)
ecb4347a
DJ
13915 {
13916 segT saved_seg = now_seg;
13917 subsegT saved_subseg = now_subseg;
13918 valueT dot;
13919 expressionS exp;
13920 char *fragp;
252b5132 13921
ecb4347a 13922 dot = frag_now_fix ();
252b5132
RH
13923
13924#ifdef md_flush_pending_output
ecb4347a 13925 md_flush_pending_output ();
252b5132
RH
13926#endif
13927
ecb4347a
DJ
13928 assert (pdr_seg);
13929 subseg_set (pdr_seg, 0);
252b5132 13930
ecb4347a
DJ
13931 /* Write the symbol. */
13932 exp.X_op = O_symbol;
13933 exp.X_add_symbol = p;
13934 exp.X_add_number = 0;
13935 emit_expr (&exp, 4);
252b5132 13936
ecb4347a 13937 fragp = frag_more (7 * 4);
252b5132 13938
17a2f251
TS
13939 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
13940 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
13941 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
13942 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
13943 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
13944 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
13945 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
252b5132 13946
ecb4347a
DJ
13947 subseg_set (saved_seg, saved_subseg);
13948 }
13949#endif /* OBJ_ELF */
252b5132
RH
13950
13951 cur_proc_ptr = NULL;
13952}
13953
13954/* The .aent and .ent directives. */
13955
13956static void
17a2f251 13957s_mips_ent (int aent)
252b5132 13958{
252b5132 13959 symbolS *symbolP;
252b5132
RH
13960
13961 symbolP = get_symbol ();
13962 if (*input_line_pointer == ',')
f9419b05 13963 ++input_line_pointer;
252b5132 13964 SKIP_WHITESPACE ();
3882b010 13965 if (ISDIGIT (*input_line_pointer)
d9a62219 13966 || *input_line_pointer == '-')
874e8986 13967 get_number ();
252b5132 13968
14949570 13969 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
13970 as_warn (_(".ent or .aent not in text section."));
13971
13972 if (!aent && cur_proc_ptr)
9a41af64 13973 as_warn (_("missing .end"));
252b5132
RH
13974
13975 if (!aent)
13976 {
7a621144
DJ
13977 /* This function needs its own .frame and .cprestore directives. */
13978 mips_frame_reg_valid = 0;
13979 mips_cprestore_valid = 0;
13980
252b5132
RH
13981 cur_proc_ptr = &cur_proc;
13982 memset (cur_proc_ptr, '\0', sizeof (procS));
13983
9b2f1d35 13984 cur_proc_ptr->func_sym = symbolP;
252b5132 13985
49309057 13986 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
252b5132 13987
f9419b05 13988 ++numprocs;
ecb4347a
DJ
13989
13990 if (debug_type == DEBUG_STABS)
13991 stabs_generate_asm_func (S_GET_NAME (symbolP),
13992 S_GET_NAME (symbolP));
252b5132
RH
13993 }
13994
13995 demand_empty_rest_of_line ();
13996}
13997
13998/* The .frame directive. If the mdebug section is present (IRIX 5 native)
bdaaa2e1 13999 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
252b5132 14000 s_mips_frame is used so that we can set the PDR information correctly.
bdaaa2e1 14001 We can't use the ecoff routines because they make reference to the ecoff
252b5132
RH
14002 symbol table (in the mdebug section). */
14003
14004static void
17a2f251 14005s_mips_frame (int ignore ATTRIBUTE_UNUSED)
252b5132 14006{
ecb4347a
DJ
14007#ifdef OBJ_ELF
14008 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14009 {
14010 long val;
252b5132 14011
ecb4347a
DJ
14012 if (cur_proc_ptr == (procS *) NULL)
14013 {
14014 as_warn (_(".frame outside of .ent"));
14015 demand_empty_rest_of_line ();
14016 return;
14017 }
252b5132 14018
ecb4347a
DJ
14019 cur_proc_ptr->frame_reg = tc_get_register (1);
14020
14021 SKIP_WHITESPACE ();
14022 if (*input_line_pointer++ != ','
14023 || get_absolute_expression_and_terminator (&val) != ',')
14024 {
14025 as_warn (_("Bad .frame directive"));
14026 --input_line_pointer;
14027 demand_empty_rest_of_line ();
14028 return;
14029 }
252b5132 14030
ecb4347a
DJ
14031 cur_proc_ptr->frame_offset = val;
14032 cur_proc_ptr->pc_reg = tc_get_register (0);
252b5132 14033
252b5132 14034 demand_empty_rest_of_line ();
252b5132 14035 }
ecb4347a
DJ
14036 else
14037#endif /* OBJ_ELF */
14038 s_ignore (ignore);
252b5132
RH
14039}
14040
bdaaa2e1
KH
14041/* The .fmask and .mask directives. If the mdebug section is present
14042 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
252b5132 14043 embedded targets, s_mips_mask is used so that we can set the PDR
bdaaa2e1 14044 information correctly. We can't use the ecoff routines because they
252b5132
RH
14045 make reference to the ecoff symbol table (in the mdebug section). */
14046
14047static void
17a2f251 14048s_mips_mask (int reg_type)
252b5132 14049{
ecb4347a
DJ
14050#ifdef OBJ_ELF
14051 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
252b5132 14052 {
ecb4347a 14053 long mask, off;
252b5132 14054
ecb4347a
DJ
14055 if (cur_proc_ptr == (procS *) NULL)
14056 {
14057 as_warn (_(".mask/.fmask outside of .ent"));
14058 demand_empty_rest_of_line ();
14059 return;
14060 }
252b5132 14061
ecb4347a
DJ
14062 if (get_absolute_expression_and_terminator (&mask) != ',')
14063 {
14064 as_warn (_("Bad .mask/.fmask directive"));
14065 --input_line_pointer;
14066 demand_empty_rest_of_line ();
14067 return;
14068 }
252b5132 14069
ecb4347a
DJ
14070 off = get_absolute_expression ();
14071
14072 if (reg_type == 'F')
14073 {
14074 cur_proc_ptr->fpreg_mask = mask;
14075 cur_proc_ptr->fpreg_offset = off;
14076 }
14077 else
14078 {
14079 cur_proc_ptr->reg_mask = mask;
14080 cur_proc_ptr->reg_offset = off;
14081 }
14082
14083 demand_empty_rest_of_line ();
252b5132
RH
14084 }
14085 else
ecb4347a
DJ
14086#endif /* OBJ_ELF */
14087 s_ignore (reg_type);
252b5132
RH
14088}
14089
316f5878
RS
14090/* A table describing all the processors gas knows about. Names are
14091 matched in the order listed.
e7af610e 14092
316f5878
RS
14093 To ease comparison, please keep this table in the same order as
14094 gcc's mips_cpu_info_table[]. */
e972090a
NC
14095static const struct mips_cpu_info mips_cpu_info_table[] =
14096{
316f5878
RS
14097 /* Entries for generic ISAs */
14098 { "mips1", 1, ISA_MIPS1, CPU_R3000 },
14099 { "mips2", 1, ISA_MIPS2, CPU_R6000 },
14100 { "mips3", 1, ISA_MIPS3, CPU_R4000 },
14101 { "mips4", 1, ISA_MIPS4, CPU_R8000 },
14102 { "mips5", 1, ISA_MIPS5, CPU_MIPS5 },
14103 { "mips32", 1, ISA_MIPS32, CPU_MIPS32 },
af7ee8bf 14104 { "mips32r2", 1, ISA_MIPS32R2, CPU_MIPS32R2 },
316f5878 14105 { "mips64", 1, ISA_MIPS64, CPU_MIPS64 },
5f74bc13 14106 { "mips64r2", 1, ISA_MIPS64R2, CPU_MIPS64R2 },
316f5878
RS
14107
14108 /* MIPS I */
14109 { "r3000", 0, ISA_MIPS1, CPU_R3000 },
14110 { "r2000", 0, ISA_MIPS1, CPU_R3000 },
14111 { "r3900", 0, ISA_MIPS1, CPU_R3900 },
14112
14113 /* MIPS II */
14114 { "r6000", 0, ISA_MIPS2, CPU_R6000 },
14115
14116 /* MIPS III */
14117 { "r4000", 0, ISA_MIPS3, CPU_R4000 },
14118 { "r4010", 0, ISA_MIPS2, CPU_R4010 },
14119 { "vr4100", 0, ISA_MIPS3, CPU_VR4100 },
14120 { "vr4111", 0, ISA_MIPS3, CPU_R4111 },
60b63b72
RS
14121 { "vr4120", 0, ISA_MIPS3, CPU_VR4120 },
14122 { "vr4130", 0, ISA_MIPS3, CPU_VR4120 },
14123 { "vr4181", 0, ISA_MIPS3, CPU_R4111 },
316f5878
RS
14124 { "vr4300", 0, ISA_MIPS3, CPU_R4300 },
14125 { "r4400", 0, ISA_MIPS3, CPU_R4400 },
14126 { "r4600", 0, ISA_MIPS3, CPU_R4600 },
14127 { "orion", 0, ISA_MIPS3, CPU_R4600 },
14128 { "r4650", 0, ISA_MIPS3, CPU_R4650 },
14129
14130 /* MIPS IV */
14131 { "r8000", 0, ISA_MIPS4, CPU_R8000 },
14132 { "r10000", 0, ISA_MIPS4, CPU_R10000 },
14133 { "r12000", 0, ISA_MIPS4, CPU_R12000 },
14134 { "vr5000", 0, ISA_MIPS4, CPU_R5000 },
60b63b72
RS
14135 { "vr5400", 0, ISA_MIPS4, CPU_VR5400 },
14136 { "vr5500", 0, ISA_MIPS4, CPU_VR5500 },
316f5878
RS
14137 { "rm5200", 0, ISA_MIPS4, CPU_R5000 },
14138 { "rm5230", 0, ISA_MIPS4, CPU_R5000 },
14139 { "rm5231", 0, ISA_MIPS4, CPU_R5000 },
14140 { "rm5261", 0, ISA_MIPS4, CPU_R5000 },
14141 { "rm5721", 0, ISA_MIPS4, CPU_R5000 },
5a7ea749 14142 { "rm7000", 0, ISA_MIPS4, CPU_RM7000 },
9a92f48d 14143 { "rm9000", 0, ISA_MIPS4, CPU_RM9000 },
316f5878
RS
14144
14145 /* MIPS 32 */
fef14a42 14146 { "4kc", 0, ISA_MIPS32, CPU_MIPS32 },
316f5878
RS
14147 { "4km", 0, ISA_MIPS32, CPU_MIPS32 },
14148 { "4kp", 0, ISA_MIPS32, CPU_MIPS32 },
e7af610e 14149
32b26a03
MR
14150 /* MIPS32 Release 2 */
14151 { "m4k", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
14152 { "24k", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
14153 { "24kc", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
14154 { "24kf", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
14155 { "24kx", 0, ISA_MIPS32R2, CPU_MIPS32R2 },
14156
316f5878
RS
14157 /* MIPS 64 */
14158 { "5kc", 0, ISA_MIPS64, CPU_MIPS64 },
ff324200 14159 { "5kf", 0, ISA_MIPS64, CPU_MIPS64 },
316f5878 14160 { "20kc", 0, ISA_MIPS64, CPU_MIPS64 },
e7af610e 14161
c7a23324 14162 /* Broadcom SB-1 CPU core */
316f5878 14163 { "sb1", 0, ISA_MIPS64, CPU_SB1 },
e7af610e 14164
316f5878
RS
14165 /* End marker */
14166 { NULL, 0, 0, 0 }
14167};
e7af610e 14168
84ea6cf2 14169
316f5878
RS
14170/* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
14171 with a final "000" replaced by "k". Ignore case.
e7af610e 14172
316f5878 14173 Note: this function is shared between GCC and GAS. */
c6c98b38 14174
b34976b6 14175static bfd_boolean
17a2f251 14176mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
14177{
14178 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
14179 given++, canonical++;
14180
14181 return ((*given == 0 && *canonical == 0)
14182 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
14183}
14184
14185
14186/* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
14187 CPU name. We've traditionally allowed a lot of variation here.
14188
14189 Note: this function is shared between GCC and GAS. */
14190
b34976b6 14191static bfd_boolean
17a2f251 14192mips_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
14193{
14194 /* First see if the name matches exactly, or with a final "000"
14195 turned into "k". */
14196 if (mips_strict_matching_cpu_name_p (canonical, given))
b34976b6 14197 return TRUE;
316f5878
RS
14198
14199 /* If not, try comparing based on numerical designation alone.
14200 See if GIVEN is an unadorned number, or 'r' followed by a number. */
14201 if (TOLOWER (*given) == 'r')
14202 given++;
14203 if (!ISDIGIT (*given))
b34976b6 14204 return FALSE;
316f5878
RS
14205
14206 /* Skip over some well-known prefixes in the canonical name,
14207 hoping to find a number there too. */
14208 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
14209 canonical += 2;
14210 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
14211 canonical += 2;
14212 else if (TOLOWER (canonical[0]) == 'r')
14213 canonical += 1;
14214
14215 return mips_strict_matching_cpu_name_p (canonical, given);
14216}
14217
14218
14219/* Parse an option that takes the name of a processor as its argument.
14220 OPTION is the name of the option and CPU_STRING is the argument.
14221 Return the corresponding processor enumeration if the CPU_STRING is
14222 recognized, otherwise report an error and return null.
14223
14224 A similar function exists in GCC. */
e7af610e
NC
14225
14226static const struct mips_cpu_info *
17a2f251 14227mips_parse_cpu (const char *option, const char *cpu_string)
e7af610e 14228{
316f5878 14229 const struct mips_cpu_info *p;
e7af610e 14230
316f5878
RS
14231 /* 'from-abi' selects the most compatible architecture for the given
14232 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
14233 EABIs, we have to decide whether we're using the 32-bit or 64-bit
14234 version. Look first at the -mgp options, if given, otherwise base
14235 the choice on MIPS_DEFAULT_64BIT.
e7af610e 14236
316f5878
RS
14237 Treat NO_ABI like the EABIs. One reason to do this is that the
14238 plain 'mips' and 'mips64' configs have 'from-abi' as their default
14239 architecture. This code picks MIPS I for 'mips' and MIPS III for
14240 'mips64', just as we did in the days before 'from-abi'. */
14241 if (strcasecmp (cpu_string, "from-abi") == 0)
14242 {
14243 if (ABI_NEEDS_32BIT_REGS (mips_abi))
14244 return mips_cpu_info_from_isa (ISA_MIPS1);
14245
14246 if (ABI_NEEDS_64BIT_REGS (mips_abi))
14247 return mips_cpu_info_from_isa (ISA_MIPS3);
14248
14249 if (file_mips_gp32 >= 0)
14250 return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
14251
14252 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
14253 ? ISA_MIPS3
14254 : ISA_MIPS1);
14255 }
14256
14257 /* 'default' has traditionally been a no-op. Probably not very useful. */
14258 if (strcasecmp (cpu_string, "default") == 0)
14259 return 0;
14260
14261 for (p = mips_cpu_info_table; p->name != 0; p++)
14262 if (mips_matching_cpu_name_p (p->name, cpu_string))
14263 return p;
14264
14265 as_bad ("Bad value (%s) for %s", cpu_string, option);
14266 return 0;
e7af610e
NC
14267}
14268
316f5878
RS
14269/* Return the canonical processor information for ISA (a member of the
14270 ISA_MIPS* enumeration). */
14271
e7af610e 14272static const struct mips_cpu_info *
17a2f251 14273mips_cpu_info_from_isa (int isa)
e7af610e
NC
14274{
14275 int i;
14276
14277 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14278 if (mips_cpu_info_table[i].is_isa
316f5878 14279 && isa == mips_cpu_info_table[i].isa)
e7af610e
NC
14280 return (&mips_cpu_info_table[i]);
14281
e972090a 14282 return NULL;
e7af610e 14283}
fef14a42
TS
14284
14285static const struct mips_cpu_info *
17a2f251 14286mips_cpu_info_from_arch (int arch)
fef14a42
TS
14287{
14288 int i;
14289
14290 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14291 if (arch == mips_cpu_info_table[i].cpu)
14292 return (&mips_cpu_info_table[i]);
14293
14294 return NULL;
14295}
316f5878
RS
14296\f
14297static void
17a2f251 14298show (FILE *stream, const char *string, int *col_p, int *first_p)
316f5878
RS
14299{
14300 if (*first_p)
14301 {
14302 fprintf (stream, "%24s", "");
14303 *col_p = 24;
14304 }
14305 else
14306 {
14307 fprintf (stream, ", ");
14308 *col_p += 2;
14309 }
e7af610e 14310
316f5878
RS
14311 if (*col_p + strlen (string) > 72)
14312 {
14313 fprintf (stream, "\n%24s", "");
14314 *col_p = 24;
14315 }
14316
14317 fprintf (stream, "%s", string);
14318 *col_p += strlen (string);
14319
14320 *first_p = 0;
14321}
14322
14323void
17a2f251 14324md_show_usage (FILE *stream)
e7af610e 14325{
316f5878
RS
14326 int column, first;
14327 size_t i;
14328
14329 fprintf (stream, _("\
14330MIPS options:\n\
316f5878
RS
14331-EB generate big endian output\n\
14332-EL generate little endian output\n\
14333-g, -g2 do not remove unneeded NOPs or swap branches\n\
14334-G NUM allow referencing objects up to NUM bytes\n\
14335 implicitly with the gp register [default 8]\n"));
14336 fprintf (stream, _("\
14337-mips1 generate MIPS ISA I instructions\n\
14338-mips2 generate MIPS ISA II instructions\n\
14339-mips3 generate MIPS ISA III instructions\n\
14340-mips4 generate MIPS ISA IV instructions\n\
14341-mips5 generate MIPS ISA V instructions\n\
14342-mips32 generate MIPS32 ISA instructions\n\
af7ee8bf 14343-mips32r2 generate MIPS32 release 2 ISA instructions\n\
316f5878 14344-mips64 generate MIPS64 ISA instructions\n\
5f74bc13 14345-mips64r2 generate MIPS64 release 2 ISA instructions\n\
316f5878
RS
14346-march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
14347
14348 first = 1;
e7af610e
NC
14349
14350 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
316f5878
RS
14351 show (stream, mips_cpu_info_table[i].name, &column, &first);
14352 show (stream, "from-abi", &column, &first);
14353 fputc ('\n', stream);
e7af610e 14354
316f5878
RS
14355 fprintf (stream, _("\
14356-mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
14357-no-mCPU don't generate code specific to CPU.\n\
14358 For -mCPU and -no-mCPU, CPU must be one of:\n"));
14359
14360 first = 1;
14361
14362 show (stream, "3900", &column, &first);
14363 show (stream, "4010", &column, &first);
14364 show (stream, "4100", &column, &first);
14365 show (stream, "4650", &column, &first);
14366 fputc ('\n', stream);
14367
14368 fprintf (stream, _("\
14369-mips16 generate mips16 instructions\n\
14370-no-mips16 do not generate mips16 instructions\n"));
14371 fprintf (stream, _("\
74cd071d
CF
14372-mdsp generate DSP instructions\n\
14373-mno-dsp do not generate DSP instructions\n"));
14374 fprintf (stream, _("\
ef2e4d86
CF
14375-mmt generate MT instructions\n\
14376-mno-mt do not generate MT instructions\n"));
14377 fprintf (stream, _("\
d766e8ec 14378-mfix-vr4120 work around certain VR4120 errata\n\
7d8e00cf 14379-mfix-vr4130 work around VR4130 mflo/mfhi errata\n\
316f5878
RS
14380-mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
14381-mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
aed1a261
RS
14382-mno-shared optimize output for executables\n\
14383-msym32 assume all symbols have 32-bit values\n\
316f5878
RS
14384-O0 remove unneeded NOPs, do not swap branches\n\
14385-O remove unneeded NOPs and swap branches\n\
316f5878
RS
14386--[no-]construct-floats [dis]allow floating point values to be constructed\n\
14387--trap, --no-break trap exception on div by 0 and mult overflow\n\
14388--break, --no-trap break exception on div by 0 and mult overflow\n"));
14389#ifdef OBJ_ELF
14390 fprintf (stream, _("\
14391-KPIC, -call_shared generate SVR4 position independent code\n\
14392-non_shared do not generate position independent code\n\
14393-xgot assume a 32 bit GOT\n\
dcd410fe 14394-mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
bbe506e8
TS
14395-mshared, -mno-shared disable/enable .cpload optimization for\n\
14396 non-shared code\n\
316f5878
RS
14397-mabi=ABI create ABI conformant object file for:\n"));
14398
14399 first = 1;
14400
14401 show (stream, "32", &column, &first);
14402 show (stream, "o64", &column, &first);
14403 show (stream, "n32", &column, &first);
14404 show (stream, "64", &column, &first);
14405 show (stream, "eabi", &column, &first);
14406
14407 fputc ('\n', stream);
14408
14409 fprintf (stream, _("\
14410-32 create o32 ABI object file (default)\n\
14411-n32 create n32 ABI object file\n\
14412-64 create 64 ABI object file\n"));
14413#endif
e7af610e 14414}
14e777e0
KB
14415
14416enum dwarf2_format
17a2f251 14417mips_dwarf2_format (void)
14e777e0
KB
14418{
14419 if (mips_abi == N64_ABI)
1de5b6a1
AO
14420 {
14421#ifdef TE_IRIX
14422 return dwarf2_format_64bit_irix;
14423#else
14424 return dwarf2_format_64bit;
14425#endif
14426 }
14e777e0
KB
14427 else
14428 return dwarf2_format_32bit;
14429}
73369e65
EC
14430
14431int
14432mips_dwarf2_addr_size (void)
14433{
14434 if (mips_abi == N64_ABI)
14435 return 8;
73369e65
EC
14436 else
14437 return 4;
14438}
5862107c
EC
14439
14440/* Standard calling conventions leave the CFA at SP on entry. */
14441void
14442mips_cfi_frame_initial_instructions (void)
14443{
14444 cfi_add_CFA_def_cfa_register (SP);
14445}
14446
This page took 1.996615 seconds and 4 git commands to generate.