]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gas/config/tc-mips.c
2004-12-10 Michael Snyder <msnyder@redhat.com>
[thirdparty/binutils-gdb.git] / gas / config / tc-mips.c
CommitLineData
252b5132 1/* tc-mips.c -- assemble code for a MIPS chip.
81912461
ILT
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3 2003, 2004 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
23 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 02111-1307, USA. */
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"
252b5132
RH
36
37#ifdef DEBUG
38#define DBG(x) printf x
39#else
40#define DBG(x)
41#endif
42
43#ifdef OBJ_MAYBE_ELF
44/* Clean up namespace so we can include obj-elf.h too. */
17a2f251
TS
45static int mips_output_flavor (void);
46static int mips_output_flavor (void) { return OUTPUT_FLAVOR; }
252b5132
RH
47#undef OBJ_PROCESS_STAB
48#undef OUTPUT_FLAVOR
49#undef S_GET_ALIGN
50#undef S_GET_SIZE
51#undef S_SET_ALIGN
52#undef S_SET_SIZE
252b5132
RH
53#undef obj_frob_file
54#undef obj_frob_file_after_relocs
55#undef obj_frob_symbol
56#undef obj_pop_insert
57#undef obj_sec_sym_ok_for_reloc
58#undef OBJ_COPY_SYMBOL_ATTRIBUTES
59
60#include "obj-elf.h"
61/* Fix any of them that we actually care about. */
62#undef OUTPUT_FLAVOR
63#define OUTPUT_FLAVOR mips_output_flavor()
64#endif
65
66#if defined (OBJ_ELF)
67#include "elf/mips.h"
68#endif
69
70#ifndef ECOFF_DEBUGGING
71#define NO_ECOFF_DEBUGGING
72#define ECOFF_DEBUGGING 0
73#endif
74
ecb4347a
DJ
75int mips_flag_mdebug = -1;
76
dcd410fe
RO
77/* Control generation of .pdr sections. Off by default on IRIX: the native
78 linker doesn't know about and discards them, but relocations against them
79 remain, leading to rld crashes. */
80#ifdef TE_IRIX
81int mips_flag_pdr = FALSE;
82#else
83int mips_flag_pdr = TRUE;
84#endif
85
252b5132
RH
86#include "ecoff.h"
87
88#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
89static char *mips_regmask_frag;
90#endif
91
85b51719 92#define ZERO 0
252b5132
RH
93#define AT 1
94#define TREG 24
95#define PIC_CALL_REG 25
96#define KT0 26
97#define KT1 27
98#define GP 28
99#define SP 29
100#define FP 30
101#define RA 31
102
103#define ILLEGAL_REG (32)
104
105/* Allow override of standard little-endian ECOFF format. */
106
107#ifndef ECOFF_LITTLE_FORMAT
108#define ECOFF_LITTLE_FORMAT "ecoff-littlemips"
109#endif
110
111extern int target_big_endian;
112
252b5132 113/* The name of the readonly data section. */
4d0d148d 114#define RDATA_SECTION_NAME (OUTPUT_FLAVOR == bfd_target_ecoff_flavour \
252b5132 115 ? ".rdata" \
056350c6
NC
116 : OUTPUT_FLAVOR == bfd_target_coff_flavour \
117 ? ".rdata" \
252b5132
RH
118 : OUTPUT_FLAVOR == bfd_target_elf_flavour \
119 ? ".rodata" \
120 : (abort (), ""))
121
a325df1d
TS
122/* The ABI to use. */
123enum mips_abi_level
124{
125 NO_ABI = 0,
126 O32_ABI,
127 O64_ABI,
128 N32_ABI,
129 N64_ABI,
130 EABI_ABI
131};
132
133/* MIPS ABI we are using for this output file. */
316f5878 134static enum mips_abi_level mips_abi = NO_ABI;
a325df1d 135
143d77c5
EC
136/* Whether or not we have code that can call pic code. */
137int mips_abicalls = FALSE;
138
aa6975fb
ILT
139/* Whether or not we have code which can be put into a shared
140 library. */
141static bfd_boolean mips_in_shared = TRUE;
142
252b5132
RH
143/* This is the set of options which may be modified by the .set
144 pseudo-op. We use a struct so that .set push and .set pop are more
145 reliable. */
146
e972090a
NC
147struct mips_set_options
148{
252b5132
RH
149 /* MIPS ISA (Instruction Set Architecture) level. This is set to -1
150 if it has not been initialized. Changed by `.set mipsN', and the
151 -mipsN command line option, and the default CPU. */
152 int isa;
1f25f5d3
CD
153 /* Enabled Application Specific Extensions (ASEs). These are set to -1
154 if they have not been initialized. Changed by `.set <asename>', by
155 command line options, and based on the default architecture. */
156 int ase_mips3d;
deec1734 157 int ase_mdmx;
252b5132
RH
158 /* Whether we are assembling for the mips16 processor. 0 if we are
159 not, 1 if we are, and -1 if the value has not been initialized.
160 Changed by `.set mips16' and `.set nomips16', and the -mips16 and
161 -nomips16 command line options, and the default CPU. */
162 int mips16;
163 /* Non-zero if we should not reorder instructions. Changed by `.set
164 reorder' and `.set noreorder'. */
165 int noreorder;
166 /* Non-zero if we should not permit the $at ($1) register to be used
167 in instructions. Changed by `.set at' and `.set noat'. */
168 int noat;
169 /* Non-zero if we should warn when a macro instruction expands into
170 more than one machine instruction. Changed by `.set nomacro' and
171 `.set macro'. */
172 int warn_about_macros;
173 /* Non-zero if we should not move instructions. Changed by `.set
174 move', `.set volatile', `.set nomove', and `.set novolatile'. */
175 int nomove;
176 /* Non-zero if we should not optimize branches by moving the target
177 of the branch into the delay slot. Actually, we don't perform
178 this optimization anyhow. Changed by `.set bopt' and `.set
179 nobopt'. */
180 int nobopt;
181 /* Non-zero if we should not autoextend mips16 instructions.
182 Changed by `.set autoextend' and `.set noautoextend'. */
183 int noautoextend;
a325df1d
TS
184 /* Restrict general purpose registers and floating point registers
185 to 32 bit. This is initially determined when -mgp32 or -mfp32
186 is passed but can changed if the assembler code uses .set mipsN. */
187 int gp32;
188 int fp32;
fef14a42
TS
189 /* MIPS architecture (CPU) type. Changed by .set arch=FOO, the -march
190 command line option, and the default CPU. */
191 int arch;
252b5132
RH
192};
193
a325df1d 194/* True if -mgp32 was passed. */
a8e8e863 195static int file_mips_gp32 = -1;
a325df1d
TS
196
197/* True if -mfp32 was passed. */
a8e8e863 198static int file_mips_fp32 = -1;
a325df1d 199
252b5132 200/* This is the struct we use to hold the current set of options. Note
a4672219 201 that we must set the isa field to ISA_UNKNOWN and the ASE fields to
e7af610e 202 -1 to indicate that they have not been initialized. */
252b5132 203
e972090a
NC
204static struct mips_set_options mips_opts =
205{
fef14a42 206 ISA_UNKNOWN, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, CPU_UNKNOWN
e7af610e 207};
252b5132
RH
208
209/* These variables are filled in with the masks of registers used.
210 The object format code reads them and puts them in the appropriate
211 place. */
212unsigned long mips_gprmask;
213unsigned long mips_cprmask[4];
214
215/* MIPS ISA we are using for this output file. */
e7af610e 216static int file_mips_isa = ISA_UNKNOWN;
252b5132 217
a4672219
TS
218/* True if -mips16 was passed or implied by arguments passed on the
219 command line (e.g., by -march). */
220static int file_ase_mips16;
221
1f25f5d3
CD
222/* True if -mips3d was passed or implied by arguments passed on the
223 command line (e.g., by -march). */
224static int file_ase_mips3d;
225
deec1734
CD
226/* True if -mdmx was passed or implied by arguments passed on the
227 command line (e.g., by -march). */
228static int file_ase_mdmx;
229
ec68c924 230/* The argument of the -march= flag. The architecture we are assembling. */
fef14a42 231static int file_mips_arch = CPU_UNKNOWN;
316f5878 232static const char *mips_arch_string;
ec68c924
EC
233
234/* The argument of the -mtune= flag. The architecture for which we
235 are optimizing. */
236static int mips_tune = CPU_UNKNOWN;
316f5878 237static const char *mips_tune_string;
ec68c924 238
316f5878 239/* True when generating 32-bit code for a 64-bit processor. */
252b5132
RH
240static int mips_32bitmode = 0;
241
316f5878
RS
242/* True if the given ABI requires 32-bit registers. */
243#define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
244
245/* Likewise 64-bit registers. */
246#define ABI_NEEDS_64BIT_REGS(ABI) \
247 ((ABI) == N32_ABI \
248 || (ABI) == N64_ABI \
249 || (ABI) == O64_ABI)
250
bdaaa2e1 251/* Return true if ISA supports 64 bit gp register instructions. */
9ce8a5dd 252#define ISA_HAS_64BIT_REGS(ISA) ( \
e7af610e
NC
253 (ISA) == ISA_MIPS3 \
254 || (ISA) == ISA_MIPS4 \
84ea6cf2 255 || (ISA) == ISA_MIPS5 \
d1cf510e 256 || (ISA) == ISA_MIPS64 \
5f74bc13 257 || (ISA) == ISA_MIPS64R2 \
9ce8a5dd
GRK
258 )
259
af7ee8bf
CD
260/* Return true if ISA supports 64-bit right rotate (dror et al.)
261 instructions. */
262#define ISA_HAS_DROR(ISA) ( \
5f74bc13 263 (ISA) == ISA_MIPS64R2 \
af7ee8bf
CD
264 )
265
266/* Return true if ISA supports 32-bit right rotate (ror et al.)
267 instructions. */
268#define ISA_HAS_ROR(ISA) ( \
269 (ISA) == ISA_MIPS32R2 \
5f74bc13 270 || (ISA) == ISA_MIPS64R2 \
af7ee8bf
CD
271 )
272
e013f690 273#define HAVE_32BIT_GPRS \
316f5878 274 (mips_opts.gp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
ca4e0257 275
e013f690 276#define HAVE_32BIT_FPRS \
316f5878 277 (mips_opts.fp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
ca4e0257
RS
278
279#define HAVE_64BIT_GPRS (! HAVE_32BIT_GPRS)
280#define HAVE_64BIT_FPRS (! HAVE_32BIT_FPRS)
281
316f5878 282#define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
e013f690 283
316f5878 284#define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
e013f690 285
3b91255e
RS
286/* True if relocations are stored in-place. */
287#define HAVE_IN_PLACE_ADDENDS (!HAVE_NEWABI)
288
3e722fb5 289/* We can only have 64bit addresses if the object file format supports it. */
afdbd6d0
CD
290#define HAVE_32BIT_ADDRESSES \
291 (HAVE_32BIT_GPRS \
3e722fb5
CD
292 || (bfd_arch_bits_per_address (stdoutput) == 32 \
293 || ! HAVE_64BIT_OBJECTS)) \
e013f690
TS
294
295#define HAVE_64BIT_ADDRESSES (! HAVE_32BIT_ADDRESSES)
ca4e0257 296
b7c7d6c1
TS
297/* Addresses are loaded in different ways, depending on the address size
298 in use. The n32 ABI Documentation also mandates the use of additions
299 with overflow checking, but existing implementations don't follow it. */
f899b4b8 300#define ADDRESS_ADD_INSN \
b7c7d6c1 301 (HAVE_32BIT_ADDRESSES ? "addu" : "daddu")
f899b4b8
TS
302
303#define ADDRESS_ADDI_INSN \
b7c7d6c1 304 (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu")
f899b4b8
TS
305
306#define ADDRESS_LOAD_INSN \
307 (HAVE_32BIT_ADDRESSES ? "lw" : "ld")
308
309#define ADDRESS_STORE_INSN \
310 (HAVE_32BIT_ADDRESSES ? "sw" : "sd")
311
a4672219 312/* Return true if the given CPU supports the MIPS16 ASE. */
3396de36
TS
313#define CPU_HAS_MIPS16(cpu) \
314 (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0 \
315 || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
a4672219 316
1f25f5d3
CD
317/* Return true if the given CPU supports the MIPS3D ASE. */
318#define CPU_HAS_MIPS3D(cpu) ((cpu) == CPU_SB1 \
319 )
320
deec1734 321/* Return true if the given CPU supports the MDMX ASE. */
b34976b6 322#define CPU_HAS_MDMX(cpu) (FALSE \
deec1734
CD
323 )
324
60b63b72
RS
325/* True if CPU has a dror instruction. */
326#define CPU_HAS_DROR(CPU) ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
327
328/* True if CPU has a ror instruction. */
329#define CPU_HAS_ROR(CPU) CPU_HAS_DROR (CPU)
330
c8978940
CD
331/* True if mflo and mfhi can be immediately followed by instructions
332 which write to the HI and LO registers.
333
334 According to MIPS specifications, MIPS ISAs I, II, and III need
335 (at least) two instructions between the reads of HI/LO and
336 instructions which write them, and later ISAs do not. Contradicting
337 the MIPS specifications, some MIPS IV processor user manuals (e.g.
338 the UM for the NEC Vr5000) document needing the instructions between
339 HI/LO reads and writes, as well. Therefore, we declare only MIPS32,
340 MIPS64 and later ISAs to have the interlocks, plus any specific
341 earlier-ISA CPUs for which CPU documentation declares that the
342 instructions are really interlocked. */
343#define hilo_interlocks \
344 (mips_opts.isa == ISA_MIPS32 \
345 || mips_opts.isa == ISA_MIPS32R2 \
346 || mips_opts.isa == ISA_MIPS64 \
347 || mips_opts.isa == ISA_MIPS64R2 \
348 || mips_opts.arch == CPU_R4010 \
349 || mips_opts.arch == CPU_R10000 \
350 || mips_opts.arch == CPU_R12000 \
351 || mips_opts.arch == CPU_RM7000 \
c8978940
CD
352 || mips_opts.arch == CPU_VR5500 \
353 )
252b5132
RH
354
355/* Whether the processor uses hardware interlocks to protect reads
81912461
ILT
356 from the GPRs after they are loaded from memory, and thus does not
357 require nops to be inserted. This applies to instructions marked
358 INSN_LOAD_MEMORY_DELAY. These nops are only required at MIPS ISA
359 level I. */
252b5132 360#define gpr_interlocks \
e7af610e 361 (mips_opts.isa != ISA_MIPS1 \
fef14a42 362 || mips_opts.arch == CPU_R3900)
252b5132 363
81912461
ILT
364/* Whether the processor uses hardware interlocks to avoid delays
365 required by coprocessor instructions, and thus does not require
366 nops to be inserted. This applies to instructions marked
367 INSN_LOAD_COPROC_DELAY, INSN_COPROC_MOVE_DELAY, and to delays
368 between instructions marked INSN_WRITE_COND_CODE and ones marked
369 INSN_READ_COND_CODE. These nops are only required at MIPS ISA
370 levels I, II, and III. */
bdaaa2e1 371/* Itbl support may require additional care here. */
81912461
ILT
372#define cop_interlocks \
373 ((mips_opts.isa != ISA_MIPS1 \
374 && mips_opts.isa != ISA_MIPS2 \
375 && mips_opts.isa != ISA_MIPS3) \
376 || mips_opts.arch == CPU_R4300 \
81912461
ILT
377 )
378
379/* Whether the processor uses hardware interlocks to protect reads
380 from coprocessor registers after they are loaded from memory, and
381 thus does not require nops to be inserted. This applies to
382 instructions marked INSN_COPROC_MEMORY_DELAY. These nops are only
383 requires at MIPS ISA level I. */
384#define cop_mem_interlocks (mips_opts.isa != ISA_MIPS1)
252b5132 385
6b76fefe
CM
386/* Is this a mfhi or mflo instruction? */
387#define MF_HILO_INSN(PINFO) \
388 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
389
252b5132
RH
390/* MIPS PIC level. */
391
a161fe53 392enum mips_pic_level mips_pic;
252b5132 393
c9914766 394/* 1 if we should generate 32 bit offsets from the $gp register in
252b5132 395 SVR4_PIC mode. Currently has no meaning in other modes. */
c9914766 396static int mips_big_got = 0;
252b5132
RH
397
398/* 1 if trap instructions should used for overflow rather than break
399 instructions. */
c9914766 400static int mips_trap = 0;
252b5132 401
119d663a 402/* 1 if double width floating point constants should not be constructed
b6ff326e 403 by assembling two single width halves into two single width floating
119d663a
NC
404 point registers which just happen to alias the double width destination
405 register. On some architectures this aliasing can be disabled by a bit
d547a75e 406 in the status register, and the setting of this bit cannot be determined
119d663a
NC
407 automatically at assemble time. */
408static int mips_disable_float_construction;
409
252b5132
RH
410/* Non-zero if any .set noreorder directives were used. */
411
412static int mips_any_noreorder;
413
6b76fefe
CM
414/* Non-zero if nops should be inserted when the register referenced in
415 an mfhi/mflo instruction is read in the next two instructions. */
416static int mips_7000_hilo_fix;
417
252b5132 418/* The size of the small data section. */
156c2f8b 419static unsigned int g_switch_value = 8;
252b5132
RH
420/* Whether the -G option was used. */
421static int g_switch_seen = 0;
422
423#define N_RMASK 0xc4
424#define N_VFP 0xd4
425
426/* If we can determine in advance that GP optimization won't be
427 possible, we can skip the relaxation stuff that tries to produce
428 GP-relative references. This makes delay slot optimization work
429 better.
430
431 This function can only provide a guess, but it seems to work for
fba2b7f9
GK
432 gcc output. It needs to guess right for gcc, otherwise gcc
433 will put what it thinks is a GP-relative instruction in a branch
434 delay slot.
252b5132
RH
435
436 I don't know if a fix is needed for the SVR4_PIC mode. I've only
437 fixed it for the non-PIC mode. KR 95/04/07 */
17a2f251 438static int nopic_need_relax (symbolS *, int);
252b5132
RH
439
440/* handle of the OPCODE hash table */
441static struct hash_control *op_hash = NULL;
442
443/* The opcode hash table we use for the mips16. */
444static struct hash_control *mips16_op_hash = NULL;
445
446/* This array holds the chars that always start a comment. If the
447 pre-processor is disabled, these aren't very useful */
448const char comment_chars[] = "#";
449
450/* This array holds the chars that only start a comment at the beginning of
451 a line. If the line seems to have the form '# 123 filename'
452 .line and .file directives will appear in the pre-processed output */
453/* Note that input_file.c hand checks for '#' at the beginning of the
454 first line of the input file. This is because the compiler outputs
bdaaa2e1 455 #NO_APP at the beginning of its output. */
252b5132
RH
456/* Also note that C style comments are always supported. */
457const char line_comment_chars[] = "#";
458
bdaaa2e1 459/* This array holds machine specific line separator characters. */
63a0b638 460const char line_separator_chars[] = ";";
252b5132
RH
461
462/* Chars that can be used to separate mant from exp in floating point nums */
463const char EXP_CHARS[] = "eE";
464
465/* Chars that mean this number is a floating point constant */
466/* As in 0f12.456 */
467/* or 0d1.2345e12 */
468const char FLT_CHARS[] = "rRsSfFdDxXpP";
469
470/* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
471 changed in read.c . Ideally it shouldn't have to know about it at all,
472 but nothing is ideal around here.
473 */
474
475static char *insn_error;
476
477static int auto_align = 1;
478
479/* When outputting SVR4 PIC code, the assembler needs to know the
480 offset in the stack frame from which to restore the $gp register.
481 This is set by the .cprestore pseudo-op, and saved in this
482 variable. */
483static offsetT mips_cprestore_offset = -1;
484
67c1ffbe 485/* Similar for NewABI PIC code, where $gp is callee-saved. NewABI has some
6478892d 486 more optimizations, it can use a register value instead of a memory-saved
956cd1d6 487 offset and even an other register than $gp as global pointer. */
6478892d
TS
488static offsetT mips_cpreturn_offset = -1;
489static int mips_cpreturn_register = -1;
490static int mips_gp_register = GP;
def2e0dd 491static int mips_gprel_offset = 0;
6478892d 492
7a621144
DJ
493/* Whether mips_cprestore_offset has been set in the current function
494 (or whether it has already been warned about, if not). */
495static int mips_cprestore_valid = 0;
496
252b5132
RH
497/* This is the register which holds the stack frame, as set by the
498 .frame pseudo-op. This is needed to implement .cprestore. */
499static int mips_frame_reg = SP;
500
7a621144
DJ
501/* Whether mips_frame_reg has been set in the current function
502 (or whether it has already been warned about, if not). */
503static int mips_frame_reg_valid = 0;
504
252b5132
RH
505/* To output NOP instructions correctly, we need to keep information
506 about the previous two instructions. */
507
508/* Whether we are optimizing. The default value of 2 means to remove
509 unneeded NOPs and swap branch instructions when possible. A value
510 of 1 means to not swap branches. A value of 0 means to always
511 insert NOPs. */
512static int mips_optimize = 2;
513
514/* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
515 equivalent to seeing no -g option at all. */
516static int mips_debug = 0;
517
518/* The previous instruction. */
519static struct mips_cl_insn prev_insn;
520
521/* The instruction before prev_insn. */
522static struct mips_cl_insn prev_prev_insn;
523
524/* If we don't want information for prev_insn or prev_prev_insn, we
525 point the insn_mo field at this dummy integer. */
43841e91 526static const struct mips_opcode dummy_opcode = { NULL, NULL, 0, 0, 0, 0 };
252b5132
RH
527
528/* Non-zero if prev_insn is valid. */
529static int prev_insn_valid;
530
531/* The frag for the previous instruction. */
532static struct frag *prev_insn_frag;
533
534/* The offset into prev_insn_frag for the previous instruction. */
535static long prev_insn_where;
536
537/* The reloc type for the previous instruction, if any. */
f6688943 538static bfd_reloc_code_real_type prev_insn_reloc_type[3];
252b5132
RH
539
540/* The reloc for the previous instruction, if any. */
f6688943 541static fixS *prev_insn_fixp[3];
252b5132
RH
542
543/* Non-zero if the previous instruction was in a delay slot. */
544static int prev_insn_is_delay_slot;
545
546/* Non-zero if the previous instruction was in a .set noreorder. */
547static int prev_insn_unreordered;
548
549/* Non-zero if the previous instruction uses an extend opcode (if
550 mips16). */
551static int prev_insn_extended;
552
553/* Non-zero if the previous previous instruction was in a .set
554 noreorder. */
555static int prev_prev_insn_unreordered;
556
557/* If this is set, it points to a frag holding nop instructions which
558 were inserted before the start of a noreorder section. If those
559 nops turn out to be unnecessary, the size of the frag can be
560 decreased. */
561static fragS *prev_nop_frag;
562
563/* The number of nop instructions we created in prev_nop_frag. */
564static int prev_nop_frag_holds;
565
566/* The number of nop instructions that we know we need in
bdaaa2e1 567 prev_nop_frag. */
252b5132
RH
568static int prev_nop_frag_required;
569
570/* The number of instructions we've seen since prev_nop_frag. */
571static int prev_nop_frag_since;
572
573/* For ECOFF and ELF, relocations against symbols are done in two
574 parts, with a HI relocation and a LO relocation. Each relocation
575 has only 16 bits of space to store an addend. This means that in
576 order for the linker to handle carries correctly, it must be able
577 to locate both the HI and the LO relocation. This means that the
578 relocations must appear in order in the relocation table.
579
580 In order to implement this, we keep track of each unmatched HI
581 relocation. We then sort them so that they immediately precede the
bdaaa2e1 582 corresponding LO relocation. */
252b5132 583
e972090a
NC
584struct mips_hi_fixup
585{
252b5132
RH
586 /* Next HI fixup. */
587 struct mips_hi_fixup *next;
588 /* This fixup. */
589 fixS *fixp;
590 /* The section this fixup is in. */
591 segT seg;
592};
593
594/* The list of unmatched HI relocs. */
595
596static struct mips_hi_fixup *mips_hi_fixup_list;
597
64bdfcaf
RS
598/* The frag containing the last explicit relocation operator.
599 Null if explicit relocations have not been used. */
600
601static fragS *prev_reloc_op_frag;
602
252b5132
RH
603/* Map normal MIPS register numbers to mips16 register numbers. */
604
605#define X ILLEGAL_REG
e972090a
NC
606static const int mips32_to_16_reg_map[] =
607{
252b5132
RH
608 X, X, 2, 3, 4, 5, 6, 7,
609 X, X, X, X, X, X, X, X,
610 0, 1, X, X, X, X, X, X,
611 X, X, X, X, X, X, X, X
612};
613#undef X
614
615/* Map mips16 register numbers to normal MIPS register numbers. */
616
e972090a
NC
617static const unsigned int mips16_to_32_reg_map[] =
618{
252b5132
RH
619 16, 17, 2, 3, 4, 5, 6, 7
620};
60b63b72 621
d766e8ec 622static int mips_fix_vr4120;
4a6a3df4
AO
623
624/* We don't relax branches by default, since this causes us to expand
625 `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
626 fail to compute the offset before expanding the macro to the most
627 efficient expansion. */
628
629static int mips_relax_branch;
252b5132 630\f
4d7206a2
RS
631/* The expansion of many macros depends on the type of symbol that
632 they refer to. For example, when generating position-dependent code,
633 a macro that refers to a symbol may have two different expansions,
634 one which uses GP-relative addresses and one which uses absolute
635 addresses. When generating SVR4-style PIC, a macro may have
636 different expansions for local and global symbols.
637
638 We handle these situations by generating both sequences and putting
639 them in variant frags. In position-dependent code, the first sequence
640 will be the GP-relative one and the second sequence will be the
641 absolute one. In SVR4 PIC, the first sequence will be for global
642 symbols and the second will be for local symbols.
643
584892a6
RS
644 The frag's "subtype" is RELAX_ENCODE (FIRST, SECOND), where FIRST and
645 SECOND are the lengths of the two sequences in bytes. These fields
646 can be extracted using RELAX_FIRST() and RELAX_SECOND(). In addition,
647 the subtype has the following flags:
4d7206a2 648
584892a6
RS
649 RELAX_USE_SECOND
650 Set if it has been decided that we should use the second
651 sequence instead of the first.
652
653 RELAX_SECOND_LONGER
654 Set in the first variant frag if the macro's second implementation
655 is longer than its first. This refers to the macro as a whole,
656 not an individual relaxation.
657
658 RELAX_NOMACRO
659 Set in the first variant frag if the macro appeared in a .set nomacro
660 block and if one alternative requires a warning but the other does not.
661
662 RELAX_DELAY_SLOT
663 Like RELAX_NOMACRO, but indicates that the macro appears in a branch
664 delay slot.
4d7206a2
RS
665
666 The frag's "opcode" points to the first fixup for relaxable code.
667
668 Relaxable macros are generated using a sequence such as:
669
670 relax_start (SYMBOL);
671 ... generate first expansion ...
672 relax_switch ();
673 ... generate second expansion ...
674 relax_end ();
675
676 The code and fixups for the unwanted alternative are discarded
677 by md_convert_frag. */
584892a6 678#define RELAX_ENCODE(FIRST, SECOND) (((FIRST) << 8) | (SECOND))
4d7206a2 679
584892a6
RS
680#define RELAX_FIRST(X) (((X) >> 8) & 0xff)
681#define RELAX_SECOND(X) ((X) & 0xff)
682#define RELAX_USE_SECOND 0x10000
683#define RELAX_SECOND_LONGER 0x20000
684#define RELAX_NOMACRO 0x40000
685#define RELAX_DELAY_SLOT 0x80000
252b5132 686
4a6a3df4
AO
687/* Branch without likely bit. If label is out of range, we turn:
688
689 beq reg1, reg2, label
690 delay slot
691
692 into
693
694 bne reg1, reg2, 0f
695 nop
696 j label
697 0: delay slot
698
699 with the following opcode replacements:
700
701 beq <-> bne
702 blez <-> bgtz
703 bltz <-> bgez
704 bc1f <-> bc1t
705
706 bltzal <-> bgezal (with jal label instead of j label)
707
708 Even though keeping the delay slot instruction in the delay slot of
709 the branch would be more efficient, it would be very tricky to do
710 correctly, because we'd have to introduce a variable frag *after*
711 the delay slot instruction, and expand that instead. Let's do it
712 the easy way for now, even if the branch-not-taken case now costs
713 one additional instruction. Out-of-range branches are not supposed
714 to be common, anyway.
715
716 Branch likely. If label is out of range, we turn:
717
718 beql reg1, reg2, label
719 delay slot (annulled if branch not taken)
720
721 into
722
723 beql reg1, reg2, 1f
724 nop
725 beql $0, $0, 2f
726 nop
727 1: j[al] label
728 delay slot (executed only if branch taken)
729 2:
730
731 It would be possible to generate a shorter sequence by losing the
732 likely bit, generating something like:
b34976b6 733
4a6a3df4
AO
734 bne reg1, reg2, 0f
735 nop
736 j[al] label
737 delay slot (executed only if branch taken)
738 0:
739
740 beql -> bne
741 bnel -> beq
742 blezl -> bgtz
743 bgtzl -> blez
744 bltzl -> bgez
745 bgezl -> bltz
746 bc1fl -> bc1t
747 bc1tl -> bc1f
748
749 bltzall -> bgezal (with jal label instead of j label)
750 bgezall -> bltzal (ditto)
751
752
753 but it's not clear that it would actually improve performance. */
af6ae2ad 754#define RELAX_BRANCH_ENCODE(uncond, likely, link, toofar) \
4a6a3df4
AO
755 ((relax_substateT) \
756 (0xc0000000 \
757 | ((toofar) ? 1 : 0) \
758 | ((link) ? 2 : 0) \
759 | ((likely) ? 4 : 0) \
af6ae2ad 760 | ((uncond) ? 8 : 0)))
4a6a3df4 761#define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
4a6a3df4
AO
762#define RELAX_BRANCH_UNCOND(i) (((i) & 8) != 0)
763#define RELAX_BRANCH_LIKELY(i) (((i) & 4) != 0)
764#define RELAX_BRANCH_LINK(i) (((i) & 2) != 0)
ae6063d4 765#define RELAX_BRANCH_TOOFAR(i) (((i) & 1) != 0)
4a6a3df4 766
252b5132
RH
767/* For mips16 code, we use an entirely different form of relaxation.
768 mips16 supports two versions of most instructions which take
769 immediate values: a small one which takes some small value, and a
770 larger one which takes a 16 bit value. Since branches also follow
771 this pattern, relaxing these values is required.
772
773 We can assemble both mips16 and normal MIPS code in a single
774 object. Therefore, we need to support this type of relaxation at
775 the same time that we support the relaxation described above. We
776 use the high bit of the subtype field to distinguish these cases.
777
778 The information we store for this type of relaxation is the
779 argument code found in the opcode file for this relocation, whether
780 the user explicitly requested a small or extended form, and whether
781 the relocation is in a jump or jal delay slot. That tells us the
782 size of the value, and how it should be stored. We also store
783 whether the fragment is considered to be extended or not. We also
784 store whether this is known to be a branch to a different section,
785 whether we have tried to relax this frag yet, and whether we have
786 ever extended a PC relative fragment because of a shift count. */
787#define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
788 (0x80000000 \
789 | ((type) & 0xff) \
790 | ((small) ? 0x100 : 0) \
791 | ((ext) ? 0x200 : 0) \
792 | ((dslot) ? 0x400 : 0) \
793 | ((jal_dslot) ? 0x800 : 0))
4a6a3df4 794#define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
252b5132
RH
795#define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
796#define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
797#define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
798#define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
799#define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
800#define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
801#define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
802#define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
803#define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
804#define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
805#define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
885add95
CD
806
807/* Is the given value a sign-extended 32-bit value? */
808#define IS_SEXT_32BIT_NUM(x) \
809 (((x) &~ (offsetT) 0x7fffffff) == 0 \
810 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
811
812/* Is the given value a sign-extended 16-bit value? */
813#define IS_SEXT_16BIT_NUM(x) \
814 (((x) &~ (offsetT) 0x7fff) == 0 \
815 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
816
4d7206a2
RS
817\f
818/* Global variables used when generating relaxable macros. See the
819 comment above RELAX_ENCODE for more details about how relaxation
820 is used. */
821static struct {
822 /* 0 if we're not emitting a relaxable macro.
823 1 if we're emitting the first of the two relaxation alternatives.
824 2 if we're emitting the second alternative. */
825 int sequence;
826
827 /* The first relaxable fixup in the current frag. (In other words,
828 the first fixup that refers to relaxable code.) */
829 fixS *first_fixup;
830
831 /* sizes[0] says how many bytes of the first alternative are stored in
832 the current frag. Likewise sizes[1] for the second alternative. */
833 unsigned int sizes[2];
834
835 /* The symbol on which the choice of sequence depends. */
836 symbolS *symbol;
837} mips_relax;
252b5132 838\f
584892a6
RS
839/* Global variables used to decide whether a macro needs a warning. */
840static struct {
841 /* True if the macro is in a branch delay slot. */
842 bfd_boolean delay_slot_p;
843
844 /* For relaxable macros, sizes[0] is the length of the first alternative
845 in bytes and sizes[1] is the length of the second alternative.
846 For non-relaxable macros, both elements give the length of the
847 macro in bytes. */
848 unsigned int sizes[2];
849
850 /* The first variant frag for this macro. */
851 fragS *first_frag;
852} mips_macro_warning;
853\f
252b5132
RH
854/* Prototypes for static functions. */
855
17a2f251 856#define internalError() \
252b5132 857 as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
252b5132
RH
858
859enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
860
b34976b6 861static void append_insn
4d7206a2 862 (struct mips_cl_insn *ip, expressionS *p, bfd_reloc_code_real_type *r);
17a2f251 863static void mips_no_prev_insn (int);
b34976b6 864static void mips16_macro_build
67c0d1eb
RS
865 (expressionS *, const char *, const char *, va_list);
866static void load_register (int, expressionS *, int);
584892a6
RS
867static void macro_start (void);
868static void macro_end (void);
17a2f251
TS
869static void macro (struct mips_cl_insn * ip);
870static void mips16_macro (struct mips_cl_insn * ip);
252b5132 871#ifdef LOSING_COMPILER
17a2f251 872static void macro2 (struct mips_cl_insn * ip);
252b5132 873#endif
17a2f251
TS
874static void mips_ip (char *str, struct mips_cl_insn * ip);
875static void mips16_ip (char *str, struct mips_cl_insn * ip);
b34976b6 876static void mips16_immed
17a2f251
TS
877 (char *, unsigned int, int, offsetT, bfd_boolean, bfd_boolean, bfd_boolean,
878 unsigned long *, bfd_boolean *, unsigned short *);
5e0116d5 879static size_t my_getSmallExpression
17a2f251
TS
880 (expressionS *, bfd_reloc_code_real_type *, char *);
881static void my_getExpression (expressionS *, char *);
882static void s_align (int);
883static void s_change_sec (int);
884static void s_change_section (int);
885static void s_cons (int);
886static void s_float_cons (int);
887static void s_mips_globl (int);
888static void s_option (int);
889static void s_mipsset (int);
890static void s_abicalls (int);
891static void s_cpload (int);
892static void s_cpsetup (int);
893static void s_cplocal (int);
894static void s_cprestore (int);
895static void s_cpreturn (int);
896static void s_gpvalue (int);
897static void s_gpword (int);
898static void s_gpdword (int);
899static void s_cpadd (int);
900static void s_insn (int);
901static void md_obj_begin (void);
902static void md_obj_end (void);
903static void s_mips_ent (int);
904static void s_mips_end (int);
905static void s_mips_frame (int);
906static void s_mips_mask (int reg_type);
907static void s_mips_stab (int);
908static void s_mips_weakext (int);
909static void s_mips_file (int);
910static void s_mips_loc (int);
911static bfd_boolean pic_need_relax (symbolS *, asection *);
4a6a3df4 912static int relaxed_branch_length (fragS *, asection *, int);
17a2f251 913static int validate_mips_insn (const struct mips_opcode *);
e7af610e
NC
914
915/* Table and functions used to map between CPU/ISA names, and
916 ISA levels, and CPU numbers. */
917
e972090a
NC
918struct mips_cpu_info
919{
e7af610e
NC
920 const char *name; /* CPU or ISA name. */
921 int is_isa; /* Is this an ISA? (If 0, a CPU.) */
922 int isa; /* ISA level. */
923 int cpu; /* CPU number (default CPU if ISA). */
924};
925
17a2f251
TS
926static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
927static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
928static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
252b5132
RH
929\f
930/* Pseudo-op table.
931
932 The following pseudo-ops from the Kane and Heinrich MIPS book
933 should be defined here, but are currently unsupported: .alias,
934 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
935
936 The following pseudo-ops from the Kane and Heinrich MIPS book are
937 specific to the type of debugging information being generated, and
938 should be defined by the object format: .aent, .begin, .bend,
939 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
940 .vreg.
941
942 The following pseudo-ops from the Kane and Heinrich MIPS book are
943 not MIPS CPU specific, but are also not specific to the object file
944 format. This file is probably the best place to define them, but
945 they are not currently supported: .asm0, .endr, .lab, .repeat,
946 .struct. */
947
e972090a
NC
948static const pseudo_typeS mips_pseudo_table[] =
949{
beae10d5 950 /* MIPS specific pseudo-ops. */
252b5132
RH
951 {"option", s_option, 0},
952 {"set", s_mipsset, 0},
953 {"rdata", s_change_sec, 'r'},
954 {"sdata", s_change_sec, 's'},
955 {"livereg", s_ignore, 0},
956 {"abicalls", s_abicalls, 0},
957 {"cpload", s_cpload, 0},
6478892d
TS
958 {"cpsetup", s_cpsetup, 0},
959 {"cplocal", s_cplocal, 0},
252b5132 960 {"cprestore", s_cprestore, 0},
6478892d
TS
961 {"cpreturn", s_cpreturn, 0},
962 {"gpvalue", s_gpvalue, 0},
252b5132 963 {"gpword", s_gpword, 0},
10181a0d 964 {"gpdword", s_gpdword, 0},
252b5132
RH
965 {"cpadd", s_cpadd, 0},
966 {"insn", s_insn, 0},
967
beae10d5 968 /* Relatively generic pseudo-ops that happen to be used on MIPS
252b5132
RH
969 chips. */
970 {"asciiz", stringer, 1},
971 {"bss", s_change_sec, 'b'},
972 {"err", s_err, 0},
973 {"half", s_cons, 1},
974 {"dword", s_cons, 3},
975 {"weakext", s_mips_weakext, 0},
976
beae10d5 977 /* These pseudo-ops are defined in read.c, but must be overridden
252b5132
RH
978 here for one reason or another. */
979 {"align", s_align, 0},
980 {"byte", s_cons, 0},
981 {"data", s_change_sec, 'd'},
982 {"double", s_float_cons, 'd'},
983 {"float", s_float_cons, 'f'},
984 {"globl", s_mips_globl, 0},
985 {"global", s_mips_globl, 0},
986 {"hword", s_cons, 1},
987 {"int", s_cons, 2},
988 {"long", s_cons, 2},
989 {"octa", s_cons, 4},
990 {"quad", s_cons, 3},
cca86cc8 991 {"section", s_change_section, 0},
252b5132
RH
992 {"short", s_cons, 1},
993 {"single", s_float_cons, 'f'},
994 {"stabn", s_mips_stab, 'n'},
995 {"text", s_change_sec, 't'},
996 {"word", s_cons, 2},
add56521 997
add56521 998 { "extern", ecoff_directive_extern, 0},
add56521 999
43841e91 1000 { NULL, NULL, 0 },
252b5132
RH
1001};
1002
e972090a
NC
1003static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1004{
beae10d5
KH
1005 /* These pseudo-ops should be defined by the object file format.
1006 However, a.out doesn't support them, so we have versions here. */
252b5132
RH
1007 {"aent", s_mips_ent, 1},
1008 {"bgnb", s_ignore, 0},
1009 {"end", s_mips_end, 0},
1010 {"endb", s_ignore, 0},
1011 {"ent", s_mips_ent, 0},
c5dd6aab 1012 {"file", s_mips_file, 0},
252b5132
RH
1013 {"fmask", s_mips_mask, 'F'},
1014 {"frame", s_mips_frame, 0},
c5dd6aab 1015 {"loc", s_mips_loc, 0},
252b5132
RH
1016 {"mask", s_mips_mask, 'R'},
1017 {"verstamp", s_ignore, 0},
43841e91 1018 { NULL, NULL, 0 },
252b5132
RH
1019};
1020
17a2f251 1021extern void pop_insert (const pseudo_typeS *);
252b5132
RH
1022
1023void
17a2f251 1024mips_pop_insert (void)
252b5132
RH
1025{
1026 pop_insert (mips_pseudo_table);
1027 if (! ECOFF_DEBUGGING)
1028 pop_insert (mips_nonecoff_pseudo_table);
1029}
1030\f
1031/* Symbols labelling the current insn. */
1032
e972090a
NC
1033struct insn_label_list
1034{
252b5132
RH
1035 struct insn_label_list *next;
1036 symbolS *label;
1037};
1038
1039static struct insn_label_list *insn_labels;
1040static struct insn_label_list *free_insn_labels;
1041
17a2f251 1042static void mips_clear_insn_labels (void);
252b5132
RH
1043
1044static inline void
17a2f251 1045mips_clear_insn_labels (void)
252b5132
RH
1046{
1047 register struct insn_label_list **pl;
1048
1049 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1050 ;
1051 *pl = insn_labels;
1052 insn_labels = NULL;
1053}
1054\f
1055static char *expr_end;
1056
1057/* Expressions which appear in instructions. These are set by
1058 mips_ip. */
1059
1060static expressionS imm_expr;
5f74bc13 1061static expressionS imm2_expr;
252b5132
RH
1062static expressionS offset_expr;
1063
1064/* Relocs associated with imm_expr and offset_expr. */
1065
f6688943
TS
1066static bfd_reloc_code_real_type imm_reloc[3]
1067 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1068static bfd_reloc_code_real_type offset_reloc[3]
1069 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 1070
252b5132
RH
1071/* These are set by mips16_ip if an explicit extension is used. */
1072
b34976b6 1073static bfd_boolean mips16_small, mips16_ext;
252b5132 1074
7ed4a06a 1075#ifdef OBJ_ELF
ecb4347a
DJ
1076/* The pdr segment for per procedure frame/regmask info. Not used for
1077 ECOFF debugging. */
252b5132
RH
1078
1079static segT pdr_seg;
7ed4a06a 1080#endif
252b5132 1081
e013f690
TS
1082/* The default target format to use. */
1083
1084const char *
17a2f251 1085mips_target_format (void)
e013f690
TS
1086{
1087 switch (OUTPUT_FLAVOR)
1088 {
e013f690
TS
1089 case bfd_target_ecoff_flavour:
1090 return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
1091 case bfd_target_coff_flavour:
1092 return "pe-mips";
1093 case bfd_target_elf_flavour:
1094#ifdef TE_TMIPS
cfe86eaa 1095 /* This is traditional mips. */
e013f690 1096 return (target_big_endian
cfe86eaa
TS
1097 ? (HAVE_64BIT_OBJECTS
1098 ? "elf64-tradbigmips"
1099 : (HAVE_NEWABI
1100 ? "elf32-ntradbigmips" : "elf32-tradbigmips"))
1101 : (HAVE_64BIT_OBJECTS
1102 ? "elf64-tradlittlemips"
1103 : (HAVE_NEWABI
1104 ? "elf32-ntradlittlemips" : "elf32-tradlittlemips")));
e013f690
TS
1105#else
1106 return (target_big_endian
cfe86eaa
TS
1107 ? (HAVE_64BIT_OBJECTS
1108 ? "elf64-bigmips"
1109 : (HAVE_NEWABI
1110 ? "elf32-nbigmips" : "elf32-bigmips"))
1111 : (HAVE_64BIT_OBJECTS
1112 ? "elf64-littlemips"
1113 : (HAVE_NEWABI
1114 ? "elf32-nlittlemips" : "elf32-littlemips")));
e013f690
TS
1115#endif
1116 default:
1117 abort ();
1118 return NULL;
1119 }
1120}
1121
156c2f8b
NC
1122/* This function is called once, at assembler startup time. It should
1123 set up all the tables, etc. that the MD part of the assembler will need. */
1124
252b5132 1125void
17a2f251 1126md_begin (void)
252b5132 1127{
252b5132 1128 register const char *retval = NULL;
156c2f8b 1129 int i = 0;
252b5132 1130 int broken = 0;
1f25f5d3 1131
fef14a42 1132 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_arch))
252b5132
RH
1133 as_warn (_("Could not set architecture and machine"));
1134
252b5132
RH
1135 op_hash = hash_new ();
1136
1137 for (i = 0; i < NUMOPCODES;)
1138 {
1139 const char *name = mips_opcodes[i].name;
1140
17a2f251 1141 retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
252b5132
RH
1142 if (retval != NULL)
1143 {
1144 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1145 mips_opcodes[i].name, retval);
1146 /* Probably a memory allocation problem? Give up now. */
1147 as_fatal (_("Broken assembler. No assembly attempted."));
1148 }
1149 do
1150 {
1151 if (mips_opcodes[i].pinfo != INSN_MACRO)
1152 {
1153 if (!validate_mips_insn (&mips_opcodes[i]))
1154 broken = 1;
1155 }
1156 ++i;
1157 }
1158 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1159 }
1160
1161 mips16_op_hash = hash_new ();
1162
1163 i = 0;
1164 while (i < bfd_mips16_num_opcodes)
1165 {
1166 const char *name = mips16_opcodes[i].name;
1167
17a2f251 1168 retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
252b5132
RH
1169 if (retval != NULL)
1170 as_fatal (_("internal: can't hash `%s': %s"),
1171 mips16_opcodes[i].name, retval);
1172 do
1173 {
1174 if (mips16_opcodes[i].pinfo != INSN_MACRO
1175 && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1176 != mips16_opcodes[i].match))
1177 {
1178 fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1179 mips16_opcodes[i].name, mips16_opcodes[i].args);
1180 broken = 1;
1181 }
1182 ++i;
1183 }
1184 while (i < bfd_mips16_num_opcodes
1185 && strcmp (mips16_opcodes[i].name, name) == 0);
1186 }
1187
1188 if (broken)
1189 as_fatal (_("Broken assembler. No assembly attempted."));
1190
1191 /* We add all the general register names to the symbol table. This
1192 helps us detect invalid uses of them. */
1193 for (i = 0; i < 32; i++)
1194 {
1195 char buf[5];
1196
1197 sprintf (buf, "$%d", i);
1198 symbol_table_insert (symbol_new (buf, reg_section, i,
1199 &zero_address_frag));
1200 }
76db943d
TS
1201 symbol_table_insert (symbol_new ("$ra", reg_section, RA,
1202 &zero_address_frag));
252b5132
RH
1203 symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1204 &zero_address_frag));
1205 symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1206 &zero_address_frag));
1207 symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1208 &zero_address_frag));
1209 symbol_table_insert (symbol_new ("$at", reg_section, AT,
1210 &zero_address_frag));
1211 symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1212 &zero_address_frag));
1213 symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1214 &zero_address_frag));
85b51719
TS
1215 symbol_table_insert (symbol_new ("$zero", reg_section, ZERO,
1216 &zero_address_frag));
252b5132
RH
1217 symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1218 &zero_address_frag));
1219
6047c971
AO
1220 /* If we don't add these register names to the symbol table, they
1221 may end up being added as regular symbols by operand(), and then
1222 make it to the object file as undefined in case they're not
1223 regarded as local symbols. They're local in o32, since `$' is a
1224 local symbol prefix, but not in n32 or n64. */
1225 for (i = 0; i < 8; i++)
1226 {
1227 char buf[6];
1228
1229 sprintf (buf, "$fcc%i", i);
1230 symbol_table_insert (symbol_new (buf, reg_section, -1,
1231 &zero_address_frag));
1232 }
1233
b34976b6 1234 mips_no_prev_insn (FALSE);
252b5132
RH
1235
1236 mips_gprmask = 0;
1237 mips_cprmask[0] = 0;
1238 mips_cprmask[1] = 0;
1239 mips_cprmask[2] = 0;
1240 mips_cprmask[3] = 0;
1241
1242 /* set the default alignment for the text section (2**2) */
1243 record_alignment (text_section, 2);
1244
4d0d148d 1245 bfd_set_gp_size (stdoutput, g_switch_value);
252b5132
RH
1246
1247 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1248 {
1249 /* On a native system, sections must be aligned to 16 byte
1250 boundaries. When configured for an embedded ELF target, we
1251 don't bother. */
1252 if (strcmp (TARGET_OS, "elf") != 0)
1253 {
1254 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1255 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1256 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1257 }
1258
1259 /* Create a .reginfo section for register masks and a .mdebug
1260 section for debugging information. */
1261 {
1262 segT seg;
1263 subsegT subseg;
1264 flagword flags;
1265 segT sec;
1266
1267 seg = now_seg;
1268 subseg = now_subseg;
1269
1270 /* The ABI says this section should be loaded so that the
1271 running program can access it. However, we don't load it
1272 if we are configured for an embedded target */
1273 flags = SEC_READONLY | SEC_DATA;
1274 if (strcmp (TARGET_OS, "elf") != 0)
1275 flags |= SEC_ALLOC | SEC_LOAD;
1276
316f5878 1277 if (mips_abi != N64_ABI)
252b5132
RH
1278 {
1279 sec = subseg_new (".reginfo", (subsegT) 0);
1280
195325d2
TS
1281 bfd_set_section_flags (stdoutput, sec, flags);
1282 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
bdaaa2e1 1283
252b5132
RH
1284#ifdef OBJ_ELF
1285 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1286#endif
1287 }
1288 else
1289 {
1290 /* The 64-bit ABI uses a .MIPS.options section rather than
1291 .reginfo section. */
1292 sec = subseg_new (".MIPS.options", (subsegT) 0);
195325d2
TS
1293 bfd_set_section_flags (stdoutput, sec, flags);
1294 bfd_set_section_alignment (stdoutput, sec, 3);
252b5132
RH
1295
1296#ifdef OBJ_ELF
1297 /* Set up the option header. */
1298 {
1299 Elf_Internal_Options opthdr;
1300 char *f;
1301
1302 opthdr.kind = ODK_REGINFO;
1303 opthdr.size = (sizeof (Elf_External_Options)
1304 + sizeof (Elf64_External_RegInfo));
1305 opthdr.section = 0;
1306 opthdr.info = 0;
1307 f = frag_more (sizeof (Elf_External_Options));
1308 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1309 (Elf_External_Options *) f);
1310
1311 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1312 }
1313#endif
1314 }
1315
1316 if (ECOFF_DEBUGGING)
1317 {
1318 sec = subseg_new (".mdebug", (subsegT) 0);
1319 (void) bfd_set_section_flags (stdoutput, sec,
1320 SEC_HAS_CONTENTS | SEC_READONLY);
1321 (void) bfd_set_section_alignment (stdoutput, sec, 2);
1322 }
ecb4347a 1323#ifdef OBJ_ELF
dcd410fe 1324 else if (OUTPUT_FLAVOR == bfd_target_elf_flavour && mips_flag_pdr)
ecb4347a
DJ
1325 {
1326 pdr_seg = subseg_new (".pdr", (subsegT) 0);
1327 (void) bfd_set_section_flags (stdoutput, pdr_seg,
1328 SEC_READONLY | SEC_RELOC
1329 | SEC_DEBUGGING);
1330 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1331 }
252b5132
RH
1332#endif
1333
1334 subseg_set (seg, subseg);
1335 }
1336 }
1337
1338 if (! ECOFF_DEBUGGING)
1339 md_obj_begin ();
1340}
1341
1342void
17a2f251 1343md_mips_end (void)
252b5132
RH
1344{
1345 if (! ECOFF_DEBUGGING)
1346 md_obj_end ();
1347}
1348
1349void
17a2f251 1350md_assemble (char *str)
252b5132
RH
1351{
1352 struct mips_cl_insn insn;
f6688943
TS
1353 bfd_reloc_code_real_type unused_reloc[3]
1354 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132
RH
1355
1356 imm_expr.X_op = O_absent;
5f74bc13 1357 imm2_expr.X_op = O_absent;
252b5132 1358 offset_expr.X_op = O_absent;
f6688943
TS
1359 imm_reloc[0] = BFD_RELOC_UNUSED;
1360 imm_reloc[1] = BFD_RELOC_UNUSED;
1361 imm_reloc[2] = BFD_RELOC_UNUSED;
1362 offset_reloc[0] = BFD_RELOC_UNUSED;
1363 offset_reloc[1] = BFD_RELOC_UNUSED;
1364 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
1365
1366 if (mips_opts.mips16)
1367 mips16_ip (str, &insn);
1368 else
1369 {
1370 mips_ip (str, &insn);
beae10d5
KH
1371 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1372 str, insn.insn_opcode));
252b5132
RH
1373 }
1374
1375 if (insn_error)
1376 {
1377 as_bad ("%s `%s'", insn_error, str);
1378 return;
1379 }
1380
1381 if (insn.insn_mo->pinfo == INSN_MACRO)
1382 {
584892a6 1383 macro_start ();
252b5132
RH
1384 if (mips_opts.mips16)
1385 mips16_macro (&insn);
1386 else
1387 macro (&insn);
584892a6 1388 macro_end ();
252b5132
RH
1389 }
1390 else
1391 {
1392 if (imm_expr.X_op != O_absent)
4d7206a2 1393 append_insn (&insn, &imm_expr, imm_reloc);
252b5132 1394 else if (offset_expr.X_op != O_absent)
4d7206a2 1395 append_insn (&insn, &offset_expr, offset_reloc);
252b5132 1396 else
4d7206a2 1397 append_insn (&insn, NULL, unused_reloc);
252b5132
RH
1398 }
1399}
1400
5919d012
RS
1401/* Return true if the given relocation might need a matching %lo().
1402 Note that R_MIPS_GOT16 relocations only need a matching %lo() when
1403 applied to local symbols. */
1404
1405static inline bfd_boolean
17a2f251 1406reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
5919d012 1407{
3b91255e
RS
1408 return (HAVE_IN_PLACE_ADDENDS
1409 && (reloc == BFD_RELOC_HI16_S
1410 || reloc == BFD_RELOC_MIPS_GOT16));
5919d012
RS
1411}
1412
1413/* Return true if the given fixup is followed by a matching R_MIPS_LO16
1414 relocation. */
1415
1416static inline bfd_boolean
17a2f251 1417fixup_has_matching_lo_p (fixS *fixp)
5919d012
RS
1418{
1419 return (fixp->fx_next != NULL
1420 && fixp->fx_next->fx_r_type == BFD_RELOC_LO16
1421 && fixp->fx_addsy == fixp->fx_next->fx_addsy
1422 && fixp->fx_offset == fixp->fx_next->fx_offset);
1423}
1424
252b5132
RH
1425/* See whether instruction IP reads register REG. CLASS is the type
1426 of register. */
1427
1428static int
17a2f251
TS
1429insn_uses_reg (struct mips_cl_insn *ip, unsigned int reg,
1430 enum mips_regclass class)
252b5132
RH
1431{
1432 if (class == MIPS16_REG)
1433 {
1434 assert (mips_opts.mips16);
1435 reg = mips16_to_32_reg_map[reg];
1436 class = MIPS_GR_REG;
1437 }
1438
85b51719
TS
1439 /* Don't report on general register ZERO, since it never changes. */
1440 if (class == MIPS_GR_REG && reg == ZERO)
252b5132
RH
1441 return 0;
1442
1443 if (class == MIPS_FP_REG)
1444 {
1445 assert (! mips_opts.mips16);
1446 /* If we are called with either $f0 or $f1, we must check $f0.
1447 This is not optimal, because it will introduce an unnecessary
1448 NOP between "lwc1 $f0" and "swc1 $f1". To fix this we would
1449 need to distinguish reading both $f0 and $f1 or just one of
1450 them. Note that we don't have to check the other way,
1451 because there is no instruction that sets both $f0 and $f1
1452 and requires a delay. */
1453 if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1454 && ((((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS) &~(unsigned)1)
1455 == (reg &~ (unsigned) 1)))
1456 return 1;
1457 if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1458 && ((((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT) &~(unsigned)1)
1459 == (reg &~ (unsigned) 1)))
1460 return 1;
1461 }
1462 else if (! mips_opts.mips16)
1463 {
1464 if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1465 && ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == reg)
1466 return 1;
1467 if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1468 && ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT) == reg)
1469 return 1;
1470 }
1471 else
1472 {
1473 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1474 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RX)
1475 & MIPS16OP_MASK_RX)]
1476 == reg))
1477 return 1;
1478 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1479 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RY)
1480 & MIPS16OP_MASK_RY)]
1481 == reg))
1482 return 1;
1483 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1484 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
1485 & MIPS16OP_MASK_MOVE32Z)]
1486 == reg))
1487 return 1;
1488 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1489 return 1;
1490 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1491 return 1;
1492 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1493 return 1;
1494 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1495 && ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
1496 & MIPS16OP_MASK_REGR32) == reg)
1497 return 1;
1498 }
1499
1500 return 0;
1501}
1502
1503/* This function returns true if modifying a register requires a
1504 delay. */
1505
1506static int
17a2f251 1507reg_needs_delay (unsigned int reg)
252b5132
RH
1508{
1509 unsigned long prev_pinfo;
1510
1511 prev_pinfo = prev_insn.insn_mo->pinfo;
1512 if (! mips_opts.noreorder
81912461
ILT
1513 && (((prev_pinfo & INSN_LOAD_MEMORY_DELAY)
1514 && ! gpr_interlocks)
1515 || ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1516 && ! cop_interlocks)))
252b5132 1517 {
81912461
ILT
1518 /* A load from a coprocessor or from memory. All load delays
1519 delay the use of general register rt for one instruction. */
bdaaa2e1 1520 /* Itbl support may require additional care here. */
252b5132
RH
1521 know (prev_pinfo & INSN_WRITE_GPR_T);
1522 if (reg == ((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT))
1523 return 1;
1524 }
1525
1526 return 0;
1527}
1528
1529/* Mark instruction labels in mips16 mode. This permits the linker to
1530 handle them specially, such as generating jalx instructions when
1531 needed. We also make them odd for the duration of the assembly, in
1532 order to generate the right sort of code. We will make them even
1533 in the adjust_symtab routine, while leaving them marked. This is
1534 convenient for the debugger and the disassembler. The linker knows
1535 to make them odd again. */
1536
1537static void
17a2f251 1538mips16_mark_labels (void)
252b5132
RH
1539{
1540 if (mips_opts.mips16)
1541 {
1542 struct insn_label_list *l;
98aa84af 1543 valueT val;
252b5132
RH
1544
1545 for (l = insn_labels; l != NULL; l = l->next)
1546 {
1547#ifdef OBJ_ELF
1548 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1549 S_SET_OTHER (l->label, STO_MIPS16);
1550#endif
98aa84af
AM
1551 val = S_GET_VALUE (l->label);
1552 if ((val & 1) == 0)
1553 S_SET_VALUE (l->label, val + 1);
252b5132
RH
1554 }
1555 }
1556}
1557
4d7206a2
RS
1558/* End the current frag. Make it a variant frag and record the
1559 relaxation info. */
1560
1561static void
1562relax_close_frag (void)
1563{
584892a6 1564 mips_macro_warning.first_frag = frag_now;
4d7206a2 1565 frag_var (rs_machine_dependent, 0, 0,
584892a6 1566 RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1]),
4d7206a2
RS
1567 mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
1568
1569 memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
1570 mips_relax.first_fixup = 0;
1571}
1572
1573/* Start a new relaxation sequence whose expansion depends on SYMBOL.
1574 See the comment above RELAX_ENCODE for more details. */
1575
1576static void
1577relax_start (symbolS *symbol)
1578{
1579 assert (mips_relax.sequence == 0);
1580 mips_relax.sequence = 1;
1581 mips_relax.symbol = symbol;
1582}
1583
1584/* Start generating the second version of a relaxable sequence.
1585 See the comment above RELAX_ENCODE for more details. */
252b5132
RH
1586
1587static void
4d7206a2
RS
1588relax_switch (void)
1589{
1590 assert (mips_relax.sequence == 1);
1591 mips_relax.sequence = 2;
1592}
1593
1594/* End the current relaxable sequence. */
1595
1596static void
1597relax_end (void)
1598{
1599 assert (mips_relax.sequence == 2);
1600 relax_close_frag ();
1601 mips_relax.sequence = 0;
1602}
1603
1604/* Output an instruction. IP is the instruction information.
1605 ADDRESS_EXPR is an operand of the instruction to be used with
1606 RELOC_TYPE. */
1607
1608static void
1609append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
17a2f251 1610 bfd_reloc_code_real_type *reloc_type)
252b5132
RH
1611{
1612 register unsigned long prev_pinfo, pinfo;
1613 char *f;
f6688943 1614 fixS *fixp[3];
252b5132 1615 int nops = 0;
895921c9
MR
1616 relax_stateT prev_insn_frag_type = 0;
1617 bfd_boolean relaxed_branch = FALSE;
f5040a92 1618 bfd_boolean force_new_frag = FALSE;
252b5132
RH
1619
1620 /* Mark instruction labels in mips16 mode. */
f9419b05 1621 mips16_mark_labels ();
252b5132
RH
1622
1623 prev_pinfo = prev_insn.insn_mo->pinfo;
1624 pinfo = ip->insn_mo->pinfo;
1625
4d7206a2
RS
1626 if (mips_relax.sequence != 2
1627 && (!mips_opts.noreorder || prev_nop_frag != NULL))
252b5132
RH
1628 {
1629 int prev_prev_nop;
1630
1631 /* If the previous insn required any delay slots, see if we need
1632 to insert a NOP or two. There are eight kinds of possible
1633 hazards, of which an instruction can have at most one type.
1634 (1) a load from memory delay
1635 (2) a load from a coprocessor delay
1636 (3) an unconditional branch delay
1637 (4) a conditional branch delay
1638 (5) a move to coprocessor register delay
1639 (6) a load coprocessor register from memory delay
1640 (7) a coprocessor condition code delay
1641 (8) a HI/LO special register delay
1642
1643 There are a lot of optimizations we could do that we don't.
1644 In particular, we do not, in general, reorder instructions.
1645 If you use gcc with optimization, it will reorder
1646 instructions and generally do much more optimization then we
1647 do here; repeating all that work in the assembler would only
1648 benefit hand written assembly code, and does not seem worth
1649 it. */
1650
1651 /* This is how a NOP is emitted. */
1652#define emit_nop() \
1653 (mips_opts.mips16 \
1654 ? md_number_to_chars (frag_more (2), 0x6500, 2) \
1655 : md_number_to_chars (frag_more (4), 0, 4))
1656
1657 /* The previous insn might require a delay slot, depending upon
1658 the contents of the current insn. */
1659 if (! mips_opts.mips16
81912461
ILT
1660 && (((prev_pinfo & INSN_LOAD_MEMORY_DELAY)
1661 && ! gpr_interlocks)
1662 || ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1663 && ! cop_interlocks)))
252b5132
RH
1664 {
1665 /* A load from a coprocessor or from memory. All load
1666 delays delay the use of general register rt for one
81912461 1667 instruction. */
beae10d5 1668 /* Itbl support may require additional care here. */
252b5132
RH
1669 know (prev_pinfo & INSN_WRITE_GPR_T);
1670 if (mips_optimize == 0
1671 || insn_uses_reg (ip,
1672 ((prev_insn.insn_opcode >> OP_SH_RT)
1673 & OP_MASK_RT),
1674 MIPS_GR_REG))
1675 ++nops;
1676 }
1677 else if (! mips_opts.mips16
252b5132 1678 && (((prev_pinfo & INSN_COPROC_MOVE_DELAY)
beae10d5 1679 && ! cop_interlocks)
81912461
ILT
1680 || ((prev_pinfo & INSN_COPROC_MEMORY_DELAY)
1681 && ! cop_mem_interlocks)))
252b5132
RH
1682 {
1683 /* A generic coprocessor delay. The previous instruction
1684 modified a coprocessor general or control register. If
1685 it modified a control register, we need to avoid any
1686 coprocessor instruction (this is probably not always
1687 required, but it sometimes is). If it modified a general
1688 register, we avoid using that register.
1689
252b5132
RH
1690 This case is not handled very well. There is no special
1691 knowledge of CP0 handling, and the coprocessors other
1692 than the floating point unit are not distinguished at
1693 all. */
1694 /* Itbl support may require additional care here. FIXME!
bdaaa2e1 1695 Need to modify this to include knowledge about
252b5132
RH
1696 user specified delays! */
1697 if (prev_pinfo & INSN_WRITE_FPR_T)
1698 {
1699 if (mips_optimize == 0
1700 || insn_uses_reg (ip,
1701 ((prev_insn.insn_opcode >> OP_SH_FT)
1702 & OP_MASK_FT),
1703 MIPS_FP_REG))
1704 ++nops;
1705 }
1706 else if (prev_pinfo & INSN_WRITE_FPR_S)
1707 {
1708 if (mips_optimize == 0
1709 || insn_uses_reg (ip,
1710 ((prev_insn.insn_opcode >> OP_SH_FS)
1711 & OP_MASK_FS),
1712 MIPS_FP_REG))
1713 ++nops;
1714 }
1715 else
1716 {
1717 /* We don't know exactly what the previous instruction
1718 does. If the current instruction uses a coprocessor
1719 register, we must insert a NOP. If previous
1720 instruction may set the condition codes, and the
1721 current instruction uses them, we must insert two
1722 NOPS. */
bdaaa2e1 1723 /* Itbl support may require additional care here. */
252b5132
RH
1724 if (mips_optimize == 0
1725 || ((prev_pinfo & INSN_WRITE_COND_CODE)
1726 && (pinfo & INSN_READ_COND_CODE)))
1727 nops += 2;
1728 else if (pinfo & INSN_COP)
1729 ++nops;
1730 }
1731 }
1732 else if (! mips_opts.mips16
252b5132
RH
1733 && (prev_pinfo & INSN_WRITE_COND_CODE)
1734 && ! cop_interlocks)
1735 {
1736 /* The previous instruction sets the coprocessor condition
1737 codes, but does not require a general coprocessor delay
1738 (this means it is a floating point comparison
1739 instruction). If this instruction uses the condition
1740 codes, we need to insert a single NOP. */
beae10d5 1741 /* Itbl support may require additional care here. */
252b5132
RH
1742 if (mips_optimize == 0
1743 || (pinfo & INSN_READ_COND_CODE))
1744 ++nops;
1745 }
6b76fefe
CM
1746
1747 /* If we're fixing up mfhi/mflo for the r7000 and the
1748 previous insn was an mfhi/mflo and the current insn
1749 reads the register that the mfhi/mflo wrote to, then
1750 insert two nops. */
1751
1752 else if (mips_7000_hilo_fix
1753 && MF_HILO_INSN (prev_pinfo)
1754 && insn_uses_reg (ip, ((prev_insn.insn_opcode >> OP_SH_RD)
beae10d5
KH
1755 & OP_MASK_RD),
1756 MIPS_GR_REG))
6b76fefe
CM
1757 {
1758 nops += 2;
1759 }
1760
1761 /* If we're fixing up mfhi/mflo for the r7000 and the
1762 2nd previous insn was an mfhi/mflo and the current insn
1763 reads the register that the mfhi/mflo wrote to, then
1764 insert one nop. */
1765
1766 else if (mips_7000_hilo_fix
1767 && MF_HILO_INSN (prev_prev_insn.insn_opcode)
1768 && insn_uses_reg (ip, ((prev_prev_insn.insn_opcode >> OP_SH_RD)
1769 & OP_MASK_RD),
1770 MIPS_GR_REG))
bdaaa2e1 1771
6b76fefe 1772 {
f9419b05 1773 ++nops;
6b76fefe 1774 }
bdaaa2e1 1775
252b5132
RH
1776 else if (prev_pinfo & INSN_READ_LO)
1777 {
1778 /* The previous instruction reads the LO register; if the
1779 current instruction writes to the LO register, we must
bdaaa2e1 1780 insert two NOPS. Some newer processors have interlocks.
67c1ffbe
KH
1781 Also the tx39's multiply instructions can be executed
1782 immediately after a read from HI/LO (without the delay),
bdaaa2e1
KH
1783 though the tx39's divide insns still do require the
1784 delay. */
252b5132 1785 if (! (hilo_interlocks
048cdf86 1786 || (mips_opts.arch == CPU_R3900 && (pinfo & INSN_MULT)))
252b5132
RH
1787 && (mips_optimize == 0
1788 || (pinfo & INSN_WRITE_LO)))
1789 nops += 2;
1790 /* Most mips16 branch insns don't have a delay slot.
1791 If a read from LO is immediately followed by a branch
1792 to a write to LO we have a read followed by a write
1793 less than 2 insns away. We assume the target of
1794 a branch might be a write to LO, and insert a nop
bdaaa2e1 1795 between a read and an immediately following branch. */
252b5132
RH
1796 else if (mips_opts.mips16
1797 && (mips_optimize == 0
1798 || (pinfo & MIPS16_INSN_BRANCH)))
f9419b05 1799 ++nops;
252b5132
RH
1800 }
1801 else if (prev_insn.insn_mo->pinfo & INSN_READ_HI)
1802 {
1803 /* The previous instruction reads the HI register; if the
1804 current instruction writes to the HI register, we must
1805 insert a NOP. Some newer processors have interlocks.
bdaaa2e1 1806 Also the note tx39's multiply above. */
252b5132 1807 if (! (hilo_interlocks
048cdf86 1808 || (mips_opts.arch == CPU_R3900 && (pinfo & INSN_MULT)))
252b5132
RH
1809 && (mips_optimize == 0
1810 || (pinfo & INSN_WRITE_HI)))
1811 nops += 2;
1812 /* Most mips16 branch insns don't have a delay slot.
1813 If a read from HI is immediately followed by a branch
1814 to a write to HI we have a read followed by a write
1815 less than 2 insns away. We assume the target of
1816 a branch might be a write to HI, and insert a nop
bdaaa2e1 1817 between a read and an immediately following branch. */
252b5132
RH
1818 else if (mips_opts.mips16
1819 && (mips_optimize == 0
1820 || (pinfo & MIPS16_INSN_BRANCH)))
f9419b05 1821 ++nops;
252b5132
RH
1822 }
1823
1824 /* If the previous instruction was in a noreorder section, then
1825 we don't want to insert the nop after all. */
bdaaa2e1 1826 /* Itbl support may require additional care here. */
252b5132
RH
1827 if (prev_insn_unreordered)
1828 nops = 0;
1829
1830 /* There are two cases which require two intervening
1831 instructions: 1) setting the condition codes using a move to
1832 coprocessor instruction which requires a general coprocessor
1833 delay and then reading the condition codes 2) reading the HI
1834 or LO register and then writing to it (except on processors
1835 which have interlocks). If we are not already emitting a NOP
1836 instruction, we must check for these cases compared to the
1837 instruction previous to the previous instruction. */
1838 if ((! mips_opts.mips16
252b5132
RH
1839 && (prev_prev_insn.insn_mo->pinfo & INSN_COPROC_MOVE_DELAY)
1840 && (prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
1841 && (pinfo & INSN_READ_COND_CODE)
1842 && ! cop_interlocks)
1843 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_LO)
1844 && (pinfo & INSN_WRITE_LO)
1845 && ! (hilo_interlocks
048cdf86 1846 || (mips_opts.arch == CPU_R3900 && (pinfo & INSN_MULT))))
252b5132
RH
1847 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
1848 && (pinfo & INSN_WRITE_HI)
1849 && ! (hilo_interlocks
048cdf86 1850 || (mips_opts.arch == CPU_R3900 && (pinfo & INSN_MULT)))))
252b5132
RH
1851 prev_prev_nop = 1;
1852 else
1853 prev_prev_nop = 0;
1854
1855 if (prev_prev_insn_unreordered)
1856 prev_prev_nop = 0;
1857
1858 if (prev_prev_nop && nops == 0)
1859 ++nops;
1860
d766e8ec 1861 if (mips_fix_vr4120 && prev_insn.insn_mo->name)
60b63b72
RS
1862 {
1863 /* We're out of bits in pinfo, so we must resort to string
1864 ops here. Shortcuts are selected based on opcodes being
d766e8ec 1865 limited to the VR4120 instruction set. */
60b63b72
RS
1866 int min_nops = 0;
1867 const char *pn = prev_insn.insn_mo->name;
1868 const char *tn = ip->insn_mo->name;
532c738a
RS
1869 if (strncmp (pn, "macc", 4) == 0
1870 || strncmp (pn, "dmacc", 5) == 0)
60b63b72
RS
1871 {
1872 /* Errata 21 - [D]DIV[U] after [D]MACC */
1873 if (strstr (tn, "div"))
532c738a 1874 min_nops = 1;
60b63b72 1875
532c738a
RS
1876 /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
1877 instruction is executed immediately after a MACC or
1878 DMACC instruction, the result of [either instruction]
1879 is incorrect." */
1880 if (strncmp (tn, "mult", 4) == 0
1881 || strncmp (tn, "dmult", 5) == 0)
1882 min_nops = 1;
1883
1884 /* Errata 23 - Continuous DMULT[U]/DMACC instructions.
1885 Applies on top of VR4181A MD(1) errata. */
1886 if (pn[0] == 'd' && strncmp (tn, "dmacc", 5) == 0)
1887 min_nops = 1;
60b63b72
RS
1888
1889 /* Errata 24 - MT{LO,HI} after [D]MACC */
1890 if (strcmp (tn, "mtlo") == 0
1891 || strcmp (tn, "mthi") == 0)
532c738a 1892 min_nops = 1;
60b63b72 1893 }
532c738a
RS
1894 else if (strncmp (pn, "dmult", 5) == 0
1895 && (strncmp (tn, "dmult", 5) == 0
1896 || strncmp (tn, "dmacc", 5) == 0))
60b63b72
RS
1897 {
1898 /* Here is the rest of errata 23. */
1899 min_nops = 1;
1900 }
532c738a
RS
1901 else if ((strncmp (pn, "dmult", 5) == 0 || strstr (pn, "div"))
1902 && (strncmp (tn, "macc", 4) == 0
1903 || strncmp (tn, "dmacc", 5) == 0))
1904 {
1905 /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
1906 executed immediately after a DMULT, DMULTU, DIV, DIVU,
1907 DDIV or DDIVU instruction, the result of the MACC or
1908 DMACC instruction is incorrect.". This partly overlaps
1909 the workaround for errata 23. */
1910 min_nops = 1;
1911 }
60b63b72
RS
1912 if (nops < min_nops)
1913 nops = min_nops;
1914 }
1915
252b5132
RH
1916 /* If we are being given a nop instruction, don't bother with
1917 one of the nops we would otherwise output. This will only
1918 happen when a nop instruction is used with mips_optimize set
1919 to 0. */
1920 if (nops > 0
1921 && ! mips_opts.noreorder
156c2f8b 1922 && ip->insn_opcode == (unsigned) (mips_opts.mips16 ? 0x6500 : 0))
252b5132
RH
1923 --nops;
1924
1925 /* Now emit the right number of NOP instructions. */
1926 if (nops > 0 && ! mips_opts.noreorder)
1927 {
1928 fragS *old_frag;
1929 unsigned long old_frag_offset;
1930 int i;
1931 struct insn_label_list *l;
1932
1933 old_frag = frag_now;
1934 old_frag_offset = frag_now_fix ();
1935
1936 for (i = 0; i < nops; i++)
1937 emit_nop ();
1938
1939 if (listing)
1940 {
1941 listing_prev_line ();
1942 /* We may be at the start of a variant frag. In case we
1943 are, make sure there is enough space for the frag
1944 after the frags created by listing_prev_line. The
1945 argument to frag_grow here must be at least as large
1946 as the argument to all other calls to frag_grow in
1947 this file. We don't have to worry about being in the
1948 middle of a variant frag, because the variants insert
1949 all needed nop instructions themselves. */
1950 frag_grow (40);
1951 }
1952
1953 for (l = insn_labels; l != NULL; l = l->next)
1954 {
98aa84af
AM
1955 valueT val;
1956
252b5132 1957 assert (S_GET_SEGMENT (l->label) == now_seg);
49309057 1958 symbol_set_frag (l->label, frag_now);
98aa84af 1959 val = (valueT) frag_now_fix ();
252b5132
RH
1960 /* mips16 text labels are stored as odd. */
1961 if (mips_opts.mips16)
f9419b05 1962 ++val;
98aa84af 1963 S_SET_VALUE (l->label, val);
252b5132
RH
1964 }
1965
1966#ifndef NO_ECOFF_DEBUGGING
1967 if (ECOFF_DEBUGGING)
1968 ecoff_fix_loc (old_frag, old_frag_offset);
1969#endif
1970 }
1971 else if (prev_nop_frag != NULL)
1972 {
1973 /* We have a frag holding nops we may be able to remove. If
1974 we don't need any nops, we can decrease the size of
1975 prev_nop_frag by the size of one instruction. If we do
bdaaa2e1 1976 need some nops, we count them in prev_nops_required. */
252b5132
RH
1977 if (prev_nop_frag_since == 0)
1978 {
1979 if (nops == 0)
1980 {
1981 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1982 --prev_nop_frag_holds;
1983 }
1984 else
1985 prev_nop_frag_required += nops;
1986 }
1987 else
1988 {
1989 if (prev_prev_nop == 0)
1990 {
1991 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1992 --prev_nop_frag_holds;
1993 }
1994 else
1995 ++prev_nop_frag_required;
1996 }
1997
1998 if (prev_nop_frag_holds <= prev_nop_frag_required)
1999 prev_nop_frag = NULL;
2000
2001 ++prev_nop_frag_since;
2002
2003 /* Sanity check: by the time we reach the second instruction
2004 after prev_nop_frag, we should have used up all the nops
2005 one way or another. */
2006 assert (prev_nop_frag_since <= 1 || prev_nop_frag == NULL);
2007 }
2008 }
2009
895921c9
MR
2010 /* Record the frag type before frag_var. */
2011 if (prev_insn_frag)
2012 prev_insn_frag_type = prev_insn_frag->fr_type;
2013
4d7206a2 2014 if (address_expr
0b25d3e6 2015 && *reloc_type == BFD_RELOC_16_PCREL_S2
4a6a3df4
AO
2016 && (pinfo & INSN_UNCOND_BRANCH_DELAY || pinfo & INSN_COND_BRANCH_DELAY
2017 || pinfo & INSN_COND_BRANCH_LIKELY)
2018 && mips_relax_branch
2019 /* Don't try branch relaxation within .set nomacro, or within
2020 .set noat if we use $at for PIC computations. If it turns
2021 out that the branch was out-of-range, we'll get an error. */
2022 && !mips_opts.warn_about_macros
2023 && !(mips_opts.noat && mips_pic != NO_PIC)
2024 && !mips_opts.mips16)
2025 {
895921c9 2026 relaxed_branch = TRUE;
4a6a3df4
AO
2027 f = frag_var (rs_machine_dependent,
2028 relaxed_branch_length
2029 (NULL, NULL,
2030 (pinfo & INSN_UNCOND_BRANCH_DELAY) ? -1
2031 : (pinfo & INSN_COND_BRANCH_LIKELY) ? 1 : 0), 4,
2032 RELAX_BRANCH_ENCODE
af6ae2ad 2033 (pinfo & INSN_UNCOND_BRANCH_DELAY,
4a6a3df4
AO
2034 pinfo & INSN_COND_BRANCH_LIKELY,
2035 pinfo & INSN_WRITE_GPR_31,
2036 0),
2037 address_expr->X_add_symbol,
2038 address_expr->X_add_number,
2039 0);
2040 *reloc_type = BFD_RELOC_UNUSED;
2041 }
2042 else if (*reloc_type > BFD_RELOC_UNUSED)
252b5132
RH
2043 {
2044 /* We need to set up a variant frag. */
2045 assert (mips_opts.mips16 && address_expr != NULL);
2046 f = frag_var (rs_machine_dependent, 4, 0,
f6688943 2047 RELAX_MIPS16_ENCODE (*reloc_type - BFD_RELOC_UNUSED,
252b5132
RH
2048 mips16_small, mips16_ext,
2049 (prev_pinfo
2050 & INSN_UNCOND_BRANCH_DELAY),
f6688943 2051 (*prev_insn_reloc_type
252b5132 2052 == BFD_RELOC_MIPS16_JMP)),
c4e7957c 2053 make_expr_symbol (address_expr), 0, NULL);
252b5132 2054 }
252b5132
RH
2055 else if (mips_opts.mips16
2056 && ! ip->use_extend
f6688943 2057 && *reloc_type != BFD_RELOC_MIPS16_JMP)
252b5132
RH
2058 {
2059 /* Make sure there is enough room to swap this instruction with
2060 a following jump instruction. */
2061 frag_grow (6);
2062 f = frag_more (2);
2063 }
2064 else
2065 {
2066 if (mips_opts.mips16
2067 && mips_opts.noreorder
2068 && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
2069 as_warn (_("extended instruction in delay slot"));
2070
4d7206a2
RS
2071 if (mips_relax.sequence)
2072 {
2073 /* If we've reached the end of this frag, turn it into a variant
2074 frag and record the information for the instructions we've
2075 written so far. */
2076 if (frag_room () < 4)
2077 relax_close_frag ();
2078 mips_relax.sizes[mips_relax.sequence - 1] += 4;
2079 }
2080
584892a6
RS
2081 if (mips_relax.sequence != 2)
2082 mips_macro_warning.sizes[0] += 4;
2083 if (mips_relax.sequence != 1)
2084 mips_macro_warning.sizes[1] += 4;
2085
252b5132
RH
2086 f = frag_more (4);
2087 }
2088
f6688943 2089 fixp[0] = fixp[1] = fixp[2] = NULL;
01a3f561 2090 if (address_expr != NULL && *reloc_type <= BFD_RELOC_UNUSED)
252b5132
RH
2091 {
2092 if (address_expr->X_op == O_constant)
2093 {
4db1a35d 2094 valueT tmp;
f6688943
TS
2095
2096 switch (*reloc_type)
252b5132
RH
2097 {
2098 case BFD_RELOC_32:
2099 ip->insn_opcode |= address_expr->X_add_number;
2100 break;
2101
f6688943 2102 case BFD_RELOC_MIPS_HIGHEST:
6344efa4
AM
2103 tmp = (address_expr->X_add_number
2104 + ((valueT) 0x8000 << 32) + 0x80008000) >> 16;
f6688943
TS
2105 tmp >>= 16;
2106 ip->insn_opcode |= (tmp >> 16) & 0xffff;
2107 break;
2108
2109 case BFD_RELOC_MIPS_HIGHER:
2110 tmp = (address_expr->X_add_number + 0x80008000) >> 16;
2111 ip->insn_opcode |= (tmp >> 16) & 0xffff;
2112 break;
2113
2114 case BFD_RELOC_HI16_S:
2115 ip->insn_opcode |= ((address_expr->X_add_number + 0x8000)
2116 >> 16) & 0xffff;
2117 break;
2118
2119 case BFD_RELOC_HI16:
2120 ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
2121 break;
2122
01a3f561 2123 case BFD_RELOC_UNUSED:
252b5132 2124 case BFD_RELOC_LO16:
ed6fb7bd 2125 case BFD_RELOC_MIPS_GOT_DISP:
252b5132
RH
2126 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2127 break;
2128
2129 case BFD_RELOC_MIPS_JMP:
2130 if ((address_expr->X_add_number & 3) != 0)
2131 as_bad (_("jump to misaligned address (0x%lx)"),
2132 (unsigned long) address_expr->X_add_number);
f3c0ec86 2133 if (address_expr->X_add_number & ~0xfffffff)
7496292d
TS
2134 as_bad (_("jump address range overflow (0x%lx)"),
2135 (unsigned long) address_expr->X_add_number);
252b5132
RH
2136 ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
2137 break;
2138
2139 case BFD_RELOC_MIPS16_JMP:
2140 if ((address_expr->X_add_number & 3) != 0)
2141 as_bad (_("jump to misaligned address (0x%lx)"),
2142 (unsigned long) address_expr->X_add_number);
f3c0ec86 2143 if (address_expr->X_add_number & ~0xfffffff)
7496292d
TS
2144 as_bad (_("jump address range overflow (0x%lx)"),
2145 (unsigned long) address_expr->X_add_number);
252b5132
RH
2146 ip->insn_opcode |=
2147 (((address_expr->X_add_number & 0x7c0000) << 3)
2148 | ((address_expr->X_add_number & 0xf800000) >> 7)
2149 | ((address_expr->X_add_number & 0x3fffc) >> 2));
2150 break;
2151
252b5132
RH
2152 case BFD_RELOC_16_PCREL_S2:
2153 goto need_reloc;
2154
2155 default:
2156 internalError ();
2157 }
2158 }
01a3f561 2159 else if (*reloc_type < BFD_RELOC_UNUSED)
252b5132 2160 need_reloc:
4d7206a2
RS
2161 {
2162 reloc_howto_type *howto;
2163 int i;
34ce925e 2164
4d7206a2
RS
2165 /* In a compound relocation, it is the final (outermost)
2166 operator that determines the relocated field. */
2167 for (i = 1; i < 3; i++)
2168 if (reloc_type[i] == BFD_RELOC_UNUSED)
2169 break;
34ce925e 2170
4d7206a2
RS
2171 howto = bfd_reloc_type_lookup (stdoutput, reloc_type[i - 1]);
2172 fixp[0] = fix_new_exp (frag_now, f - frag_now->fr_literal,
2173 bfd_get_reloc_size(howto),
2174 address_expr,
2175 reloc_type[0] == BFD_RELOC_16_PCREL_S2,
2176 reloc_type[0]);
2177
2178 /* These relocations can have an addend that won't fit in
2179 4 octets for 64bit assembly. */
2180 if (HAVE_64BIT_GPRS
2181 && ! howto->partial_inplace
2182 && (reloc_type[0] == BFD_RELOC_16
2183 || reloc_type[0] == BFD_RELOC_32
2184 || reloc_type[0] == BFD_RELOC_MIPS_JMP
2185 || reloc_type[0] == BFD_RELOC_HI16_S
2186 || reloc_type[0] == BFD_RELOC_LO16
2187 || reloc_type[0] == BFD_RELOC_GPREL16
2188 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
2189 || reloc_type[0] == BFD_RELOC_GPREL32
2190 || reloc_type[0] == BFD_RELOC_64
2191 || reloc_type[0] == BFD_RELOC_CTOR
2192 || reloc_type[0] == BFD_RELOC_MIPS_SUB
2193 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
2194 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
2195 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
2196 || reloc_type[0] == BFD_RELOC_MIPS_REL16
2197 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT))
2198 fixp[0]->fx_no_overflow = 1;
2199
2200 if (mips_relax.sequence)
2201 {
2202 if (mips_relax.first_fixup == 0)
2203 mips_relax.first_fixup = fixp[0];
2204 }
2205 else if (reloc_needs_lo_p (*reloc_type))
2206 {
2207 struct mips_hi_fixup *hi_fixup;
252b5132 2208
4d7206a2
RS
2209 /* Reuse the last entry if it already has a matching %lo. */
2210 hi_fixup = mips_hi_fixup_list;
2211 if (hi_fixup == 0
2212 || !fixup_has_matching_lo_p (hi_fixup->fixp))
2213 {
2214 hi_fixup = ((struct mips_hi_fixup *)
2215 xmalloc (sizeof (struct mips_hi_fixup)));
2216 hi_fixup->next = mips_hi_fixup_list;
2217 mips_hi_fixup_list = hi_fixup;
252b5132 2218 }
4d7206a2
RS
2219 hi_fixup->fixp = fixp[0];
2220 hi_fixup->seg = now_seg;
2221 }
f6688943 2222
4d7206a2
RS
2223 /* Add fixups for the second and third relocations, if given.
2224 Note that the ABI allows the second relocation to be
2225 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the
2226 moment we only use RSS_UNDEF, but we could add support
2227 for the others if it ever becomes necessary. */
2228 for (i = 1; i < 3; i++)
2229 if (reloc_type[i] != BFD_RELOC_UNUSED)
2230 {
6e1304d8
RS
2231 fixp[i] = fix_new (frag_now, fixp[0]->fx_where,
2232 fixp[0]->fx_size, NULL, 0,
2233 FALSE, reloc_type[i]);
b1dca8ee
RS
2234
2235 /* Use fx_tcbit to mark compound relocs. */
2236 fixp[0]->fx_tcbit = 1;
2237 fixp[i]->fx_tcbit = 1;
4d7206a2 2238 }
252b5132
RH
2239 }
2240 }
2241
2242 if (! mips_opts.mips16)
c5dd6aab
DJ
2243 {
2244 md_number_to_chars (f, ip->insn_opcode, 4);
2245#ifdef OBJ_ELF
2246 dwarf2_emit_insn (4);
2247#endif
2248 }
f6688943 2249 else if (*reloc_type == BFD_RELOC_MIPS16_JMP)
252b5132
RH
2250 {
2251 md_number_to_chars (f, ip->insn_opcode >> 16, 2);
2252 md_number_to_chars (f + 2, ip->insn_opcode & 0xffff, 2);
c5dd6aab 2253#ifdef OBJ_ELF
20e56c33
RS
2254 /* The value passed to dwarf2_emit_insn is the distance between
2255 the end of the current instruction and the address that should
2256 be recorded in the debug tables. Since we want to use ISA-encoded
2257 addresses in MIPS16 debug info, the value is one byte less than
2258 the real instruction length. */
2259 dwarf2_emit_insn (3);
c5dd6aab 2260#endif
252b5132
RH
2261 }
2262 else
2263 {
2264 if (ip->use_extend)
2265 {
2266 md_number_to_chars (f, 0xf000 | ip->extend, 2);
2267 f += 2;
2268 }
2269 md_number_to_chars (f, ip->insn_opcode, 2);
c5dd6aab 2270#ifdef OBJ_ELF
20e56c33 2271 dwarf2_emit_insn (ip->use_extend ? 3 : 1);
c5dd6aab 2272#endif
252b5132
RH
2273 }
2274
2275 /* Update the register mask information. */
2276 if (! mips_opts.mips16)
2277 {
2278 if (pinfo & INSN_WRITE_GPR_D)
2279 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
2280 if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2281 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
2282 if (pinfo & INSN_READ_GPR_S)
2283 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
2284 if (pinfo & INSN_WRITE_GPR_31)
f9419b05 2285 mips_gprmask |= 1 << RA;
252b5132
RH
2286 if (pinfo & INSN_WRITE_FPR_D)
2287 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
2288 if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2289 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
2290 if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2291 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
2292 if ((pinfo & INSN_READ_FPR_R) != 0)
2293 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
2294 if (pinfo & INSN_COP)
2295 {
bdaaa2e1
KH
2296 /* We don't keep enough information to sort these cases out.
2297 The itbl support does keep this information however, although
2298 we currently don't support itbl fprmats as part of the cop
2299 instruction. May want to add this support in the future. */
252b5132
RH
2300 }
2301 /* Never set the bit for $0, which is always zero. */
beae10d5 2302 mips_gprmask &= ~1 << 0;
252b5132
RH
2303 }
2304 else
2305 {
2306 if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2307 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RX)
2308 & MIPS16OP_MASK_RX);
2309 if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2310 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RY)
2311 & MIPS16OP_MASK_RY);
2312 if (pinfo & MIPS16_INSN_WRITE_Z)
2313 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RZ)
2314 & MIPS16OP_MASK_RZ);
2315 if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2316 mips_gprmask |= 1 << TREG;
2317 if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2318 mips_gprmask |= 1 << SP;
2319 if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2320 mips_gprmask |= 1 << RA;
2321 if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2322 mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2323 if (pinfo & MIPS16_INSN_READ_Z)
2324 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
2325 & MIPS16OP_MASK_MOVE32Z);
2326 if (pinfo & MIPS16_INSN_READ_GPR_X)
2327 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
2328 & MIPS16OP_MASK_REGR32);
2329 }
2330
4d7206a2 2331 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
252b5132
RH
2332 {
2333 /* Filling the branch delay slot is more complex. We try to
2334 switch the branch with the previous instruction, which we can
2335 do if the previous instruction does not set up a condition
2336 that the branch tests and if the branch is not itself the
2337 target of any branch. */
2338 if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2339 || (pinfo & INSN_COND_BRANCH_DELAY))
2340 {
2341 if (mips_optimize < 2
2342 /* If we have seen .set volatile or .set nomove, don't
2343 optimize. */
2344 || mips_opts.nomove != 0
2345 /* If we had to emit any NOP instructions, then we
2346 already know we can not swap. */
2347 || nops != 0
2348 /* If we don't even know the previous insn, we can not
bdaaa2e1 2349 swap. */
252b5132
RH
2350 || ! prev_insn_valid
2351 /* If the previous insn is already in a branch delay
2352 slot, then we can not swap. */
2353 || prev_insn_is_delay_slot
2354 /* If the previous previous insn was in a .set
2355 noreorder, we can't swap. Actually, the MIPS
2356 assembler will swap in this situation. However, gcc
2357 configured -with-gnu-as will generate code like
2358 .set noreorder
2359 lw $4,XXX
2360 .set reorder
2361 INSN
2362 bne $4,$0,foo
2363 in which we can not swap the bne and INSN. If gcc is
2364 not configured -with-gnu-as, it does not output the
2365 .set pseudo-ops. We don't have to check
2366 prev_insn_unreordered, because prev_insn_valid will
2367 be 0 in that case. We don't want to use
2368 prev_prev_insn_valid, because we do want to be able
2369 to swap at the start of a function. */
2370 || prev_prev_insn_unreordered
2371 /* If the branch is itself the target of a branch, we
2372 can not swap. We cheat on this; all we check for is
2373 whether there is a label on this instruction. If
2374 there are any branches to anything other than a
2375 label, users must use .set noreorder. */
2376 || insn_labels != NULL
895921c9
MR
2377 /* If the previous instruction is in a variant frag
2378 other than this branch's one, we cannot do the swap.
2379 This does not apply to the mips16, which uses variant
2380 frags for different purposes. */
252b5132 2381 || (! mips_opts.mips16
895921c9 2382 && prev_insn_frag_type == rs_machine_dependent)
252b5132
RH
2383 /* If the branch reads the condition codes, we don't
2384 even try to swap, because in the sequence
2385 ctc1 $X,$31
2386 INSN
2387 INSN
2388 bc1t LABEL
2389 we can not swap, and I don't feel like handling that
2390 case. */
2391 || (! mips_opts.mips16
81912461
ILT
2392 && (pinfo & INSN_READ_COND_CODE)
2393 && ! cop_interlocks)
252b5132 2394 /* We can not swap with an instruction that requires a
67c1ffbe 2395 delay slot, because the target of the branch might
252b5132
RH
2396 interfere with that instruction. */
2397 || (! mips_opts.mips16
252b5132 2398 && (prev_pinfo
bdaaa2e1 2399 /* Itbl support may require additional care here. */
252b5132
RH
2400 & (INSN_LOAD_COPROC_DELAY
2401 | INSN_COPROC_MOVE_DELAY
81912461
ILT
2402 | INSN_WRITE_COND_CODE))
2403 && ! cop_interlocks)
252b5132 2404 || (! (hilo_interlocks
048cdf86 2405 || (mips_opts.arch == CPU_R3900 && (pinfo & INSN_MULT)))
252b5132
RH
2406 && (prev_pinfo
2407 & (INSN_READ_LO
2408 | INSN_READ_HI)))
2409 || (! mips_opts.mips16
81912461
ILT
2410 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY)
2411 && ! gpr_interlocks)
252b5132 2412 || (! mips_opts.mips16
bdaaa2e1 2413 /* Itbl support may require additional care here. */
81912461
ILT
2414 && (prev_pinfo & INSN_COPROC_MEMORY_DELAY)
2415 && ! cop_mem_interlocks)
252b5132
RH
2416 /* We can not swap with a branch instruction. */
2417 || (prev_pinfo
2418 & (INSN_UNCOND_BRANCH_DELAY
2419 | INSN_COND_BRANCH_DELAY
2420 | INSN_COND_BRANCH_LIKELY))
2421 /* We do not swap with a trap instruction, since it
2422 complicates trap handlers to have the trap
2423 instruction be in a delay slot. */
2424 || (prev_pinfo & INSN_TRAP)
2425 /* If the branch reads a register that the previous
2426 instruction sets, we can not swap. */
2427 || (! mips_opts.mips16
2428 && (prev_pinfo & INSN_WRITE_GPR_T)
2429 && insn_uses_reg (ip,
2430 ((prev_insn.insn_opcode >> OP_SH_RT)
2431 & OP_MASK_RT),
2432 MIPS_GR_REG))
2433 || (! mips_opts.mips16
2434 && (prev_pinfo & INSN_WRITE_GPR_D)
2435 && insn_uses_reg (ip,
2436 ((prev_insn.insn_opcode >> OP_SH_RD)
2437 & OP_MASK_RD),
2438 MIPS_GR_REG))
2439 || (mips_opts.mips16
2440 && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2441 && insn_uses_reg (ip,
2442 ((prev_insn.insn_opcode
2443 >> MIPS16OP_SH_RX)
2444 & MIPS16OP_MASK_RX),
2445 MIPS16_REG))
2446 || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2447 && insn_uses_reg (ip,
2448 ((prev_insn.insn_opcode
2449 >> MIPS16OP_SH_RY)
2450 & MIPS16OP_MASK_RY),
2451 MIPS16_REG))
2452 || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2453 && insn_uses_reg (ip,
2454 ((prev_insn.insn_opcode
2455 >> MIPS16OP_SH_RZ)
2456 & MIPS16OP_MASK_RZ),
2457 MIPS16_REG))
2458 || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2459 && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2460 || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2461 && insn_uses_reg (ip, RA, MIPS_GR_REG))
2462 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2463 && insn_uses_reg (ip,
2464 MIPS16OP_EXTRACT_REG32R (prev_insn.
2465 insn_opcode),
2466 MIPS_GR_REG))))
2467 /* If the branch writes a register that the previous
2468 instruction sets, we can not swap (we know that
2469 branches write only to RD or to $31). */
2470 || (! mips_opts.mips16
2471 && (prev_pinfo & INSN_WRITE_GPR_T)
2472 && (((pinfo & INSN_WRITE_GPR_D)
2473 && (((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT)
2474 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2475 || ((pinfo & INSN_WRITE_GPR_31)
2476 && (((prev_insn.insn_opcode >> OP_SH_RT)
2477 & OP_MASK_RT)
f9419b05 2478 == RA))))
252b5132
RH
2479 || (! mips_opts.mips16
2480 && (prev_pinfo & INSN_WRITE_GPR_D)
2481 && (((pinfo & INSN_WRITE_GPR_D)
2482 && (((prev_insn.insn_opcode >> OP_SH_RD) & OP_MASK_RD)
2483 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2484 || ((pinfo & INSN_WRITE_GPR_31)
2485 && (((prev_insn.insn_opcode >> OP_SH_RD)
2486 & OP_MASK_RD)
f9419b05 2487 == RA))))
252b5132
RH
2488 || (mips_opts.mips16
2489 && (pinfo & MIPS16_INSN_WRITE_31)
2490 && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2491 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2492 && (MIPS16OP_EXTRACT_REG32R (prev_insn.insn_opcode)
2493 == RA))))
2494 /* If the branch writes a register that the previous
2495 instruction reads, we can not swap (we know that
2496 branches only write to RD or to $31). */
2497 || (! mips_opts.mips16
2498 && (pinfo & INSN_WRITE_GPR_D)
2499 && insn_uses_reg (&prev_insn,
2500 ((ip->insn_opcode >> OP_SH_RD)
2501 & OP_MASK_RD),
2502 MIPS_GR_REG))
2503 || (! mips_opts.mips16
2504 && (pinfo & INSN_WRITE_GPR_31)
f9419b05 2505 && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
252b5132
RH
2506 || (mips_opts.mips16
2507 && (pinfo & MIPS16_INSN_WRITE_31)
2508 && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
252b5132
RH
2509 /* If the previous previous instruction has a load
2510 delay, and sets a register that the branch reads, we
2511 can not swap. */
2512 || (! mips_opts.mips16
bdaaa2e1 2513 /* Itbl support may require additional care here. */
81912461
ILT
2514 && (((prev_prev_insn.insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
2515 && ! cop_interlocks)
2516 || ((prev_prev_insn.insn_mo->pinfo
2517 & INSN_LOAD_MEMORY_DELAY)
2518 && ! gpr_interlocks))
252b5132
RH
2519 && insn_uses_reg (ip,
2520 ((prev_prev_insn.insn_opcode >> OP_SH_RT)
2521 & OP_MASK_RT),
2522 MIPS_GR_REG))
2523 /* If one instruction sets a condition code and the
2524 other one uses a condition code, we can not swap. */
2525 || ((pinfo & INSN_READ_COND_CODE)
2526 && (prev_pinfo & INSN_WRITE_COND_CODE))
2527 || ((pinfo & INSN_WRITE_COND_CODE)
2528 && (prev_pinfo & INSN_READ_COND_CODE))
2529 /* If the previous instruction uses the PC, we can not
2530 swap. */
2531 || (mips_opts.mips16
2532 && (prev_pinfo & MIPS16_INSN_READ_PC))
2533 /* If the previous instruction was extended, we can not
2534 swap. */
2535 || (mips_opts.mips16 && prev_insn_extended)
2536 /* If the previous instruction had a fixup in mips16
2537 mode, we can not swap. This normally means that the
2538 previous instruction was a 4 byte branch anyhow. */
f6688943 2539 || (mips_opts.mips16 && prev_insn_fixp[0])
bdaaa2e1
KH
2540 /* If the previous instruction is a sync, sync.l, or
2541 sync.p, we can not swap. */
f173e82e 2542 || (prev_pinfo & INSN_SYNC))
252b5132
RH
2543 {
2544 /* We could do even better for unconditional branches to
2545 portions of this object file; we could pick up the
2546 instruction at the destination, put it in the delay
2547 slot, and bump the destination address. */
2548 emit_nop ();
dd22970f
ILT
2549 if (mips_relax.sequence)
2550 mips_relax.sizes[mips_relax.sequence - 1] += 4;
252b5132
RH
2551 /* Update the previous insn information. */
2552 prev_prev_insn = *ip;
2553 prev_insn.insn_mo = &dummy_opcode;
2554 }
2555 else
2556 {
2557 /* It looks like we can actually do the swap. */
2558 if (! mips_opts.mips16)
2559 {
2560 char *prev_f;
2561 char temp[4];
2562
2563 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
895921c9
MR
2564 if (!relaxed_branch)
2565 {
2566 /* If this is not a relaxed branch, then just
2567 swap the instructions. */
2568 memcpy (temp, prev_f, 4);
2569 memcpy (prev_f, f, 4);
2570 memcpy (f, temp, 4);
2571 }
2572 else
2573 {
2574 /* If this is a relaxed branch, then we move the
2575 instruction to be placed in the delay slot to
2576 the current frag, shrinking the fixed part of
2577 the originating frag. If the branch occupies
2578 the tail of the latter, we move it backwards,
2579 into the space freed by the moved instruction. */
2580 f = frag_more (4);
2581 memcpy (f, prev_f, 4);
2582 prev_insn_frag->fr_fix -= 4;
2583 if (prev_insn_frag->fr_type == rs_machine_dependent)
2584 memmove (prev_f, prev_f + 4, prev_insn_frag->fr_var);
2585 }
2586
f6688943
TS
2587 if (prev_insn_fixp[0])
2588 {
2589 prev_insn_fixp[0]->fx_frag = frag_now;
2590 prev_insn_fixp[0]->fx_where = f - frag_now->fr_literal;
2591 }
2592 if (prev_insn_fixp[1])
2593 {
2594 prev_insn_fixp[1]->fx_frag = frag_now;
2595 prev_insn_fixp[1]->fx_where = f - frag_now->fr_literal;
2596 }
2597 if (prev_insn_fixp[2])
252b5132 2598 {
f6688943
TS
2599 prev_insn_fixp[2]->fx_frag = frag_now;
2600 prev_insn_fixp[2]->fx_where = f - frag_now->fr_literal;
252b5132 2601 }
f5040a92
AO
2602 if (prev_insn_fixp[0] && HAVE_NEWABI
2603 && prev_insn_frag != frag_now
2604 && (prev_insn_fixp[0]->fx_r_type
2605 == BFD_RELOC_MIPS_GOT_DISP
2606 || (prev_insn_fixp[0]->fx_r_type
2607 == BFD_RELOC_MIPS_CALL16)))
2608 {
2609 /* To avoid confusion in tc_gen_reloc, we must
2610 ensure that this does not become a variant
2611 frag. */
2612 force_new_frag = TRUE;
2613 }
895921c9
MR
2614
2615 if (!relaxed_branch)
f6688943 2616 {
895921c9
MR
2617 if (fixp[0])
2618 {
2619 fixp[0]->fx_frag = prev_insn_frag;
2620 fixp[0]->fx_where = prev_insn_where;
2621 }
2622 if (fixp[1])
2623 {
2624 fixp[1]->fx_frag = prev_insn_frag;
2625 fixp[1]->fx_where = prev_insn_where;
2626 }
2627 if (fixp[2])
2628 {
2629 fixp[2]->fx_frag = prev_insn_frag;
2630 fixp[2]->fx_where = prev_insn_where;
2631 }
f6688943 2632 }
895921c9 2633 else if (prev_insn_frag->fr_type == rs_machine_dependent)
f6688943 2634 {
895921c9
MR
2635 if (fixp[0])
2636 fixp[0]->fx_where -= 4;
2637 if (fixp[1])
2638 fixp[1]->fx_where -= 4;
2639 if (fixp[2])
2640 fixp[2]->fx_where -= 4;
252b5132
RH
2641 }
2642 }
2643 else
2644 {
2645 char *prev_f;
2646 char temp[2];
2647
f6688943
TS
2648 assert (prev_insn_fixp[0] == NULL);
2649 assert (prev_insn_fixp[1] == NULL);
2650 assert (prev_insn_fixp[2] == NULL);
252b5132
RH
2651 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2652 memcpy (temp, prev_f, 2);
2653 memcpy (prev_f, f, 2);
f6688943 2654 if (*reloc_type != BFD_RELOC_MIPS16_JMP)
252b5132 2655 {
f6688943 2656 assert (*reloc_type == BFD_RELOC_UNUSED);
252b5132
RH
2657 memcpy (f, temp, 2);
2658 }
2659 else
2660 {
2661 memcpy (f, f + 2, 2);
2662 memcpy (f + 2, temp, 2);
2663 }
f6688943
TS
2664 if (fixp[0])
2665 {
2666 fixp[0]->fx_frag = prev_insn_frag;
2667 fixp[0]->fx_where = prev_insn_where;
2668 }
2669 if (fixp[1])
2670 {
2671 fixp[1]->fx_frag = prev_insn_frag;
2672 fixp[1]->fx_where = prev_insn_where;
2673 }
2674 if (fixp[2])
252b5132 2675 {
f6688943
TS
2676 fixp[2]->fx_frag = prev_insn_frag;
2677 fixp[2]->fx_where = prev_insn_where;
252b5132
RH
2678 }
2679 }
2680
2681 /* Update the previous insn information; leave prev_insn
2682 unchanged. */
2683 prev_prev_insn = *ip;
2684 }
2685 prev_insn_is_delay_slot = 1;
2686
2687 /* If that was an unconditional branch, forget the previous
2688 insn information. */
2689 if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2690 {
2691 prev_prev_insn.insn_mo = &dummy_opcode;
2692 prev_insn.insn_mo = &dummy_opcode;
2693 }
2694
f6688943
TS
2695 prev_insn_fixp[0] = NULL;
2696 prev_insn_fixp[1] = NULL;
2697 prev_insn_fixp[2] = NULL;
2698 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2699 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2700 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
252b5132
RH
2701 prev_insn_extended = 0;
2702 }
2703 else if (pinfo & INSN_COND_BRANCH_LIKELY)
2704 {
2705 /* We don't yet optimize a branch likely. What we should do
2706 is look at the target, copy the instruction found there
2707 into the delay slot, and increment the branch to jump to
2708 the next instruction. */
2709 emit_nop ();
2710 /* Update the previous insn information. */
2711 prev_prev_insn = *ip;
2712 prev_insn.insn_mo = &dummy_opcode;
f6688943
TS
2713 prev_insn_fixp[0] = NULL;
2714 prev_insn_fixp[1] = NULL;
2715 prev_insn_fixp[2] = NULL;
2716 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2717 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2718 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
252b5132 2719 prev_insn_extended = 0;
f7870c8d 2720 prev_insn_is_delay_slot = 1;
252b5132
RH
2721 }
2722 else
2723 {
2724 /* Update the previous insn information. */
2725 if (nops > 0)
2726 prev_prev_insn.insn_mo = &dummy_opcode;
2727 else
2728 prev_prev_insn = prev_insn;
2729 prev_insn = *ip;
2730
2731 /* Any time we see a branch, we always fill the delay slot
2732 immediately; since this insn is not a branch, we know it
2733 is not in a delay slot. */
2734 prev_insn_is_delay_slot = 0;
2735
f6688943
TS
2736 prev_insn_fixp[0] = fixp[0];
2737 prev_insn_fixp[1] = fixp[1];
2738 prev_insn_fixp[2] = fixp[2];
2739 prev_insn_reloc_type[0] = reloc_type[0];
2740 prev_insn_reloc_type[1] = reloc_type[1];
2741 prev_insn_reloc_type[2] = reloc_type[2];
252b5132
RH
2742 if (mips_opts.mips16)
2743 prev_insn_extended = (ip->use_extend
f6688943 2744 || *reloc_type > BFD_RELOC_UNUSED);
252b5132
RH
2745 }
2746
2747 prev_prev_insn_unreordered = prev_insn_unreordered;
2748 prev_insn_unreordered = 0;
2749 prev_insn_frag = frag_now;
2750 prev_insn_where = f - frag_now->fr_literal;
2751 prev_insn_valid = 1;
2752 }
4d7206a2 2753 else if (mips_relax.sequence != 2)
252b5132
RH
2754 {
2755 /* We need to record a bit of information even when we are not
2756 reordering, in order to determine the base address for mips16
2757 PC relative relocs. */
2758 prev_prev_insn = prev_insn;
2759 prev_insn = *ip;
f6688943
TS
2760 prev_insn_reloc_type[0] = reloc_type[0];
2761 prev_insn_reloc_type[1] = reloc_type[1];
2762 prev_insn_reloc_type[2] = reloc_type[2];
252b5132
RH
2763 prev_prev_insn_unreordered = prev_insn_unreordered;
2764 prev_insn_unreordered = 1;
2765 }
2766
2767 /* We just output an insn, so the next one doesn't have a label. */
2768 mips_clear_insn_labels ();
252b5132
RH
2769}
2770
2771/* This function forgets that there was any previous instruction or
2772 label. If PRESERVE is non-zero, it remembers enough information to
bdaaa2e1 2773 know whether nops are needed before a noreorder section. */
252b5132
RH
2774
2775static void
17a2f251 2776mips_no_prev_insn (int preserve)
252b5132
RH
2777{
2778 if (! preserve)
2779 {
2780 prev_insn.insn_mo = &dummy_opcode;
2781 prev_prev_insn.insn_mo = &dummy_opcode;
2782 prev_nop_frag = NULL;
2783 prev_nop_frag_holds = 0;
2784 prev_nop_frag_required = 0;
2785 prev_nop_frag_since = 0;
2786 }
2787 prev_insn_valid = 0;
2788 prev_insn_is_delay_slot = 0;
2789 prev_insn_unreordered = 0;
2790 prev_insn_extended = 0;
f6688943
TS
2791 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2792 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2793 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
252b5132
RH
2794 prev_prev_insn_unreordered = 0;
2795 mips_clear_insn_labels ();
2796}
2797
2798/* This function must be called whenever we turn on noreorder or emit
2799 something other than instructions. It inserts any NOPS which might
2800 be needed by the previous instruction, and clears the information
2801 kept for the previous instructions. The INSNS parameter is true if
bdaaa2e1 2802 instructions are to follow. */
252b5132
RH
2803
2804static void
17a2f251 2805mips_emit_delays (bfd_boolean insns)
252b5132
RH
2806{
2807 if (! mips_opts.noreorder)
2808 {
2809 int nops;
2810
2811 nops = 0;
2812 if ((! mips_opts.mips16
81912461
ILT
2813 && ((prev_insn.insn_mo->pinfo
2814 & (INSN_LOAD_COPROC_DELAY
2815 | INSN_COPROC_MOVE_DELAY
2816 | INSN_WRITE_COND_CODE))
2817 && ! cop_interlocks))
252b5132
RH
2818 || (! hilo_interlocks
2819 && (prev_insn.insn_mo->pinfo
2820 & (INSN_READ_LO
2821 | INSN_READ_HI)))
2822 || (! mips_opts.mips16
81912461
ILT
2823 && (prev_insn.insn_mo->pinfo & INSN_LOAD_MEMORY_DELAY)
2824 && ! gpr_interlocks)
252b5132 2825 || (! mips_opts.mips16
81912461
ILT
2826 && (prev_insn.insn_mo->pinfo & INSN_COPROC_MEMORY_DELAY)
2827 && ! cop_mem_interlocks))
252b5132 2828 {
beae10d5 2829 /* Itbl support may require additional care here. */
252b5132
RH
2830 ++nops;
2831 if ((! mips_opts.mips16
81912461
ILT
2832 && ((prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
2833 && ! cop_interlocks))
252b5132
RH
2834 || (! hilo_interlocks
2835 && ((prev_insn.insn_mo->pinfo & INSN_READ_HI)
2836 || (prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2837 ++nops;
2838
2839 if (prev_insn_unreordered)
2840 nops = 0;
2841 }
2842 else if ((! mips_opts.mips16
81912461
ILT
2843 && ((prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
2844 && ! cop_interlocks))
252b5132
RH
2845 || (! hilo_interlocks
2846 && ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
2847 || (prev_prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2848 {
beae10d5 2849 /* Itbl support may require additional care here. */
252b5132
RH
2850 if (! prev_prev_insn_unreordered)
2851 ++nops;
2852 }
2853
d766e8ec 2854 if (mips_fix_vr4120 && prev_insn.insn_mo->name)
60b63b72
RS
2855 {
2856 int min_nops = 0;
2857 const char *pn = prev_insn.insn_mo->name;
532c738a
RS
2858 if (strncmp (pn, "macc", 4) == 0
2859 || strncmp (pn, "dmacc", 5) == 0
2860 || strncmp (pn, "dmult", 5) == 0
2861 || strstr (pn, "div"))
2862 min_nops = 1;
60b63b72
RS
2863 if (nops < min_nops)
2864 nops = min_nops;
2865 }
2866
252b5132
RH
2867 if (nops > 0)
2868 {
2869 struct insn_label_list *l;
2870
2871 if (insns)
2872 {
2873 /* Record the frag which holds the nop instructions, so
2874 that we can remove them if we don't need them. */
2875 frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2876 prev_nop_frag = frag_now;
2877 prev_nop_frag_holds = nops;
2878 prev_nop_frag_required = 0;
2879 prev_nop_frag_since = 0;
2880 }
2881
2882 for (; nops > 0; --nops)
2883 emit_nop ();
2884
2885 if (insns)
2886 {
2887 /* Move on to a new frag, so that it is safe to simply
bdaaa2e1 2888 decrease the size of prev_nop_frag. */
252b5132
RH
2889 frag_wane (frag_now);
2890 frag_new (0);
2891 }
2892
2893 for (l = insn_labels; l != NULL; l = l->next)
2894 {
98aa84af
AM
2895 valueT val;
2896
252b5132 2897 assert (S_GET_SEGMENT (l->label) == now_seg);
49309057 2898 symbol_set_frag (l->label, frag_now);
98aa84af 2899 val = (valueT) frag_now_fix ();
252b5132
RH
2900 /* mips16 text labels are stored as odd. */
2901 if (mips_opts.mips16)
f9419b05 2902 ++val;
98aa84af 2903 S_SET_VALUE (l->label, val);
252b5132
RH
2904 }
2905 }
2906 }
2907
2908 /* Mark instruction labels in mips16 mode. */
f9419b05 2909 if (insns)
252b5132
RH
2910 mips16_mark_labels ();
2911
2912 mips_no_prev_insn (insns);
2913}
2914
584892a6
RS
2915/* Set up global variables for the start of a new macro. */
2916
2917static void
2918macro_start (void)
2919{
2920 memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
2921 mips_macro_warning.delay_slot_p = (mips_opts.noreorder
2922 && (prev_insn.insn_mo->pinfo
2923 & (INSN_UNCOND_BRANCH_DELAY
2924 | INSN_COND_BRANCH_DELAY
2925 | INSN_COND_BRANCH_LIKELY)) != 0);
2926}
2927
2928/* Given that a macro is longer than 4 bytes, return the appropriate warning
2929 for it. Return null if no warning is needed. SUBTYPE is a bitmask of
2930 RELAX_DELAY_SLOT and RELAX_NOMACRO. */
2931
2932static const char *
2933macro_warning (relax_substateT subtype)
2934{
2935 if (subtype & RELAX_DELAY_SLOT)
2936 return _("Macro instruction expanded into multiple instructions"
2937 " in a branch delay slot");
2938 else if (subtype & RELAX_NOMACRO)
2939 return _("Macro instruction expanded into multiple instructions");
2940 else
2941 return 0;
2942}
2943
2944/* Finish up a macro. Emit warnings as appropriate. */
2945
2946static void
2947macro_end (void)
2948{
2949 if (mips_macro_warning.sizes[0] > 4 || mips_macro_warning.sizes[1] > 4)
2950 {
2951 relax_substateT subtype;
2952
2953 /* Set up the relaxation warning flags. */
2954 subtype = 0;
2955 if (mips_macro_warning.sizes[1] > mips_macro_warning.sizes[0])
2956 subtype |= RELAX_SECOND_LONGER;
2957 if (mips_opts.warn_about_macros)
2958 subtype |= RELAX_NOMACRO;
2959 if (mips_macro_warning.delay_slot_p)
2960 subtype |= RELAX_DELAY_SLOT;
2961
2962 if (mips_macro_warning.sizes[0] > 4 && mips_macro_warning.sizes[1] > 4)
2963 {
2964 /* Either the macro has a single implementation or both
2965 implementations are longer than 4 bytes. Emit the
2966 warning now. */
2967 const char *msg = macro_warning (subtype);
2968 if (msg != 0)
2969 as_warn (msg);
2970 }
2971 else
2972 {
2973 /* One implementation might need a warning but the other
2974 definitely doesn't. */
2975 mips_macro_warning.first_frag->fr_subtype |= subtype;
2976 }
2977 }
2978}
2979
6e1304d8
RS
2980/* Read a macro's relocation codes from *ARGS and store them in *R.
2981 The first argument in *ARGS will be either the code for a single
2982 relocation or -1 followed by the three codes that make up a
2983 composite relocation. */
2984
2985static void
2986macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
2987{
2988 int i, next;
2989
2990 next = va_arg (*args, int);
2991 if (next >= 0)
2992 r[0] = (bfd_reloc_code_real_type) next;
2993 else
2994 for (i = 0; i < 3; i++)
2995 r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
2996}
2997
252b5132
RH
2998/* Build an instruction created by a macro expansion. This is passed
2999 a pointer to the count of instructions created so far, an
3000 expression, the name of the instruction to build, an operand format
3001 string, and corresponding arguments. */
3002
252b5132 3003static void
67c0d1eb 3004macro_build (expressionS *ep, const char *name, const char *fmt, ...)
252b5132
RH
3005{
3006 struct mips_cl_insn insn;
f6688943 3007 bfd_reloc_code_real_type r[3];
252b5132 3008 va_list args;
252b5132 3009
252b5132 3010 va_start (args, fmt);
252b5132 3011
252b5132
RH
3012 if (mips_opts.mips16)
3013 {
67c0d1eb 3014 mips16_macro_build (ep, name, fmt, args);
252b5132
RH
3015 va_end (args);
3016 return;
3017 }
3018
f6688943
TS
3019 r[0] = BFD_RELOC_UNUSED;
3020 r[1] = BFD_RELOC_UNUSED;
3021 r[2] = BFD_RELOC_UNUSED;
252b5132
RH
3022 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3023 assert (insn.insn_mo);
3024 assert (strcmp (name, insn.insn_mo->name) == 0);
3025
3026 /* Search until we get a match for NAME. */
3027 while (1)
3028 {
b34976b6 3029 /* It is assumed here that macros will never generate
deec1734 3030 MDMX or MIPS-3D instructions. */
252b5132
RH
3031 if (strcmp (fmt, insn.insn_mo->args) == 0
3032 && insn.insn_mo->pinfo != INSN_MACRO
aec421e0
TS
3033 && OPCODE_IS_MEMBER (insn.insn_mo,
3034 (mips_opts.isa
3396de36 3035 | (file_ase_mips16 ? INSN_MIPS16 : 0)),
fef14a42
TS
3036 mips_opts.arch)
3037 && (mips_opts.arch != CPU_R4650 || (insn.insn_mo->pinfo & FP_D) == 0))
252b5132
RH
3038 break;
3039
3040 ++insn.insn_mo;
3041 assert (insn.insn_mo->name);
3042 assert (strcmp (name, insn.insn_mo->name) == 0);
3043 }
3044
3045 insn.insn_opcode = insn.insn_mo->match;
3046 for (;;)
3047 {
3048 switch (*fmt++)
3049 {
3050 case '\0':
3051 break;
3052
3053 case ',':
3054 case '(':
3055 case ')':
3056 continue;
3057
5f74bc13
CD
3058 case '+':
3059 switch (*fmt++)
3060 {
3061 case 'A':
3062 case 'E':
3063 insn.insn_opcode |= (va_arg (args, int)
3064 & OP_MASK_SHAMT) << OP_SH_SHAMT;
3065 continue;
3066
3067 case 'B':
3068 case 'F':
3069 /* Note that in the macro case, these arguments are already
3070 in MSB form. (When handling the instruction in the
3071 non-macro case, these arguments are sizes from which
3072 MSB values must be calculated.) */
3073 insn.insn_opcode |= (va_arg (args, int)
3074 & OP_MASK_INSMSB) << OP_SH_INSMSB;
3075 continue;
3076
3077 case 'C':
3078 case 'G':
3079 case 'H':
3080 /* Note that in the macro case, these arguments are already
3081 in MSBD form. (When handling the instruction in the
3082 non-macro case, these arguments are sizes from which
3083 MSBD values must be calculated.) */
3084 insn.insn_opcode |= (va_arg (args, int)
3085 & OP_MASK_EXTMSBD) << OP_SH_EXTMSBD;
3086 continue;
3087
3088 default:
3089 internalError ();
3090 }
3091 continue;
3092
252b5132
RH
3093 case 't':
3094 case 'w':
3095 case 'E':
38487616 3096 insn.insn_opcode |= va_arg (args, int) << OP_SH_RT;
252b5132
RH
3097 continue;
3098
3099 case 'c':
38487616
TS
3100 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE;
3101 continue;
3102
252b5132
RH
3103 case 'T':
3104 case 'W':
38487616 3105 insn.insn_opcode |= va_arg (args, int) << OP_SH_FT;
252b5132
RH
3106 continue;
3107
3108 case 'd':
3109 case 'G':
af7ee8bf 3110 case 'K':
38487616 3111 insn.insn_opcode |= va_arg (args, int) << OP_SH_RD;
252b5132
RH
3112 continue;
3113
4372b673
NC
3114 case 'U':
3115 {
3116 int tmp = va_arg (args, int);
3117
38487616
TS
3118 insn.insn_opcode |= tmp << OP_SH_RT;
3119 insn.insn_opcode |= tmp << OP_SH_RD;
beae10d5 3120 continue;
4372b673
NC
3121 }
3122
252b5132
RH
3123 case 'V':
3124 case 'S':
38487616 3125 insn.insn_opcode |= va_arg (args, int) << OP_SH_FS;
252b5132
RH
3126 continue;
3127
3128 case 'z':
3129 continue;
3130
3131 case '<':
38487616 3132 insn.insn_opcode |= va_arg (args, int) << OP_SH_SHAMT;
252b5132
RH
3133 continue;
3134
3135 case 'D':
38487616 3136 insn.insn_opcode |= va_arg (args, int) << OP_SH_FD;
252b5132
RH
3137 continue;
3138
3139 case 'B':
38487616 3140 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE20;
252b5132
RH
3141 continue;
3142
4372b673 3143 case 'J':
38487616 3144 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE19;
4372b673
NC
3145 continue;
3146
252b5132 3147 case 'q':
38487616 3148 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE2;
252b5132
RH
3149 continue;
3150
3151 case 'b':
3152 case 's':
3153 case 'r':
3154 case 'v':
38487616 3155 insn.insn_opcode |= va_arg (args, int) << OP_SH_RS;
252b5132
RH
3156 continue;
3157
3158 case 'i':
3159 case 'j':
3160 case 'o':
6e1304d8 3161 macro_read_relocs (&args, r);
cdf6fd85 3162 assert (*r == BFD_RELOC_GPREL16
f6688943
TS
3163 || *r == BFD_RELOC_MIPS_LITERAL
3164 || *r == BFD_RELOC_MIPS_HIGHER
3165 || *r == BFD_RELOC_HI16_S
3166 || *r == BFD_RELOC_LO16
3167 || *r == BFD_RELOC_MIPS_GOT16
3168 || *r == BFD_RELOC_MIPS_CALL16
438c16b8
TS
3169 || *r == BFD_RELOC_MIPS_GOT_DISP
3170 || *r == BFD_RELOC_MIPS_GOT_PAGE
3171 || *r == BFD_RELOC_MIPS_GOT_OFST
f6688943 3172 || *r == BFD_RELOC_MIPS_GOT_LO16
3e722fb5 3173 || *r == BFD_RELOC_MIPS_CALL_LO16);
252b5132
RH
3174 continue;
3175
3176 case 'u':
6e1304d8 3177 macro_read_relocs (&args, r);
252b5132
RH
3178 assert (ep != NULL
3179 && (ep->X_op == O_constant
3180 || (ep->X_op == O_symbol
f6688943
TS
3181 && (*r == BFD_RELOC_MIPS_HIGHEST
3182 || *r == BFD_RELOC_HI16_S
3183 || *r == BFD_RELOC_HI16
3184 || *r == BFD_RELOC_GPREL16
3185 || *r == BFD_RELOC_MIPS_GOT_HI16
3e722fb5 3186 || *r == BFD_RELOC_MIPS_CALL_HI16))));
252b5132
RH
3187 continue;
3188
3189 case 'p':
3190 assert (ep != NULL);
3191 /*
3192 * This allows macro() to pass an immediate expression for
3193 * creating short branches without creating a symbol.
0b25d3e6
AO
3194 * Note that the expression still might come from the assembly
3195 * input, in which case the value is not checked for range nor
3196 * is a relocation entry generated (yuck).
252b5132
RH
3197 */
3198 if (ep->X_op == O_constant)
3199 {
3200 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
3201 ep = NULL;
3202 }
3203 else
0b25d3e6 3204 *r = BFD_RELOC_16_PCREL_S2;
252b5132
RH
3205 continue;
3206
3207 case 'a':
3208 assert (ep != NULL);
f6688943 3209 *r = BFD_RELOC_MIPS_JMP;
252b5132
RH
3210 continue;
3211
3212 case 'C':
3213 insn.insn_opcode |= va_arg (args, unsigned long);
3214 continue;
3215
3216 default:
3217 internalError ();
3218 }
3219 break;
3220 }
3221 va_end (args);
f6688943 3222 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 3223
4d7206a2 3224 append_insn (&insn, ep, r);
252b5132
RH
3225}
3226
3227static void
67c0d1eb 3228mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
17a2f251 3229 va_list args)
252b5132
RH
3230{
3231 struct mips_cl_insn insn;
f6688943
TS
3232 bfd_reloc_code_real_type r[3]
3233 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 3234
252b5132
RH
3235 insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
3236 assert (insn.insn_mo);
3237 assert (strcmp (name, insn.insn_mo->name) == 0);
3238
3239 while (strcmp (fmt, insn.insn_mo->args) != 0
3240 || insn.insn_mo->pinfo == INSN_MACRO)
3241 {
3242 ++insn.insn_mo;
3243 assert (insn.insn_mo->name);
3244 assert (strcmp (name, insn.insn_mo->name) == 0);
3245 }
3246
3247 insn.insn_opcode = insn.insn_mo->match;
b34976b6 3248 insn.use_extend = FALSE;
252b5132
RH
3249
3250 for (;;)
3251 {
3252 int c;
3253
3254 c = *fmt++;
3255 switch (c)
3256 {
3257 case '\0':
3258 break;
3259
3260 case ',':
3261 case '(':
3262 case ')':
3263 continue;
3264
3265 case 'y':
3266 case 'w':
3267 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
3268 continue;
3269
3270 case 'x':
3271 case 'v':
3272 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
3273 continue;
3274
3275 case 'z':
3276 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
3277 continue;
3278
3279 case 'Z':
3280 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
3281 continue;
3282
3283 case '0':
3284 case 'S':
3285 case 'P':
3286 case 'R':
3287 continue;
3288
3289 case 'X':
3290 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
3291 continue;
3292
3293 case 'Y':
3294 {
3295 int regno;
3296
3297 regno = va_arg (args, int);
3298 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
3299 insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
3300 }
3301 continue;
3302
3303 case '<':
3304 case '>':
3305 case '4':
3306 case '5':
3307 case 'H':
3308 case 'W':
3309 case 'D':
3310 case 'j':
3311 case '8':
3312 case 'V':
3313 case 'C':
3314 case 'U':
3315 case 'k':
3316 case 'K':
3317 case 'p':
3318 case 'q':
3319 {
3320 assert (ep != NULL);
3321
3322 if (ep->X_op != O_constant)
874e8986 3323 *r = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
3324 else
3325 {
b34976b6
AM
3326 mips16_immed (NULL, 0, c, ep->X_add_number, FALSE, FALSE,
3327 FALSE, &insn.insn_opcode, &insn.use_extend,
c4e7957c 3328 &insn.extend);
252b5132 3329 ep = NULL;
f6688943 3330 *r = BFD_RELOC_UNUSED;
252b5132
RH
3331 }
3332 }
3333 continue;
3334
3335 case '6':
3336 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
3337 continue;
3338 }
3339
3340 break;
3341 }
3342
f6688943 3343 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 3344
4d7206a2 3345 append_insn (&insn, ep, r);
252b5132
RH
3346}
3347
438c16b8
TS
3348/*
3349 * Generate a "jalr" instruction with a relocation hint to the called
3350 * function. This occurs in NewABI PIC code.
3351 */
3352static void
67c0d1eb 3353macro_build_jalr (expressionS *ep)
438c16b8 3354{
685736be 3355 char *f = NULL;
b34976b6 3356
438c16b8 3357 if (HAVE_NEWABI)
f21f8242 3358 {
cc3d92a5 3359 frag_grow (8);
f21f8242
AO
3360 f = frag_more (0);
3361 }
67c0d1eb 3362 macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
438c16b8 3363 if (HAVE_NEWABI)
f21f8242 3364 fix_new_exp (frag_now, f - frag_now->fr_literal,
a105a300 3365 4, ep, FALSE, BFD_RELOC_MIPS_JALR);
438c16b8
TS
3366}
3367
252b5132
RH
3368/*
3369 * Generate a "lui" instruction.
3370 */
3371static void
67c0d1eb 3372macro_build_lui (expressionS *ep, int regnum)
252b5132
RH
3373{
3374 expressionS high_expr;
3375 struct mips_cl_insn insn;
f6688943
TS
3376 bfd_reloc_code_real_type r[3]
3377 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
5a38dc70
AM
3378 const char *name = "lui";
3379 const char *fmt = "t,u";
252b5132
RH
3380
3381 assert (! mips_opts.mips16);
3382
4d7206a2 3383 high_expr = *ep;
252b5132
RH
3384
3385 if (high_expr.X_op == O_constant)
3386 {
3387 /* we can compute the instruction now without a relocation entry */
e7d556df
TS
3388 high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3389 >> 16) & 0xffff;
f6688943 3390 *r = BFD_RELOC_UNUSED;
252b5132 3391 }
78e1bb40 3392 else
252b5132
RH
3393 {
3394 assert (ep->X_op == O_symbol);
aa6975fb
ILT
3395 /* _gp_disp is a special case, used from s_cpload. _gp is used
3396 if mips_no_shared. */
252b5132 3397 assert (mips_pic == NO_PIC
78e1bb40 3398 || (! HAVE_NEWABI
aa6975fb
ILT
3399 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
3400 || (! mips_in_shared
3401 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp") == 0));
f6688943 3402 *r = BFD_RELOC_HI16_S;
252b5132
RH
3403 }
3404
252b5132
RH
3405 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3406 assert (insn.insn_mo);
3407 assert (strcmp (name, insn.insn_mo->name) == 0);
3408 assert (strcmp (fmt, insn.insn_mo->args) == 0);
3409
3410 insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
f6688943 3411 if (*r == BFD_RELOC_UNUSED)
252b5132
RH
3412 {
3413 insn.insn_opcode |= high_expr.X_add_number;
4d7206a2 3414 append_insn (&insn, NULL, r);
252b5132
RH
3415 }
3416 else
4d7206a2 3417 append_insn (&insn, &high_expr, r);
252b5132
RH
3418}
3419
885add95
CD
3420/* Generate a sequence of instructions to do a load or store from a constant
3421 offset off of a base register (breg) into/from a target register (treg),
3422 using AT if necessary. */
3423static void
67c0d1eb
RS
3424macro_build_ldst_constoffset (expressionS *ep, const char *op,
3425 int treg, int breg, int dbl)
885add95
CD
3426{
3427 assert (ep->X_op == O_constant);
3428
256ab948 3429 /* Sign-extending 32-bit constants makes their handling easier. */
d17e7bce
TS
3430 if (! dbl && ! ((ep->X_add_number & ~((bfd_vma) 0x7fffffff))
3431 == ~((bfd_vma) 0x7fffffff)))
ae826530 3432 {
d17e7bce 3433 if (ep->X_add_number & ~((bfd_vma) 0xffffffff))
1b8e29e5 3434 as_bad (_("constant too large"));
ae826530 3435
1b8e29e5
TS
3436 ep->X_add_number = (((ep->X_add_number & 0xffffffff) ^ 0x80000000)
3437 - 0x80000000);
ae826530 3438 }
256ab948 3439
67c1ffbe 3440 /* Right now, this routine can only handle signed 32-bit constants. */
ecd13cd3 3441 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
885add95
CD
3442 as_warn (_("operand overflow"));
3443
3444 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
3445 {
3446 /* Signed 16-bit offset will fit in the op. Easy! */
67c0d1eb 3447 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
885add95
CD
3448 }
3449 else
3450 {
3451 /* 32-bit offset, need multiple instructions and AT, like:
3452 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
3453 addu $tempreg,$tempreg,$breg
3454 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
3455 to handle the complete offset. */
67c0d1eb
RS
3456 macro_build_lui (ep, AT);
3457 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
3458 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
885add95
CD
3459
3460 if (mips_opts.noat)
3461 as_warn (_("Macro used $at after \".set noat\""));
3462 }
3463}
3464
252b5132
RH
3465/* set_at()
3466 * Generates code to set the $at register to true (one)
3467 * if reg is less than the immediate expression.
3468 */
3469static void
67c0d1eb 3470set_at (int reg, int unsignedp)
252b5132
RH
3471{
3472 if (imm_expr.X_op == O_constant
3473 && imm_expr.X_add_number >= -0x8000
3474 && imm_expr.X_add_number < 0x8000)
67c0d1eb
RS
3475 macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
3476 AT, reg, BFD_RELOC_LO16);
252b5132
RH
3477 else
3478 {
67c0d1eb
RS
3479 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
3480 macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
252b5132
RH
3481 }
3482}
3483
13757d0c
TS
3484static void
3485normalize_constant_expr (expressionS *ex)
3486{
3487 if (ex->X_op == O_constant && HAVE_32BIT_GPRS)
3488 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
3489 - 0x80000000);
3490}
3491
252b5132
RH
3492/* Warn if an expression is not a constant. */
3493
3494static void
17a2f251 3495check_absolute_expr (struct mips_cl_insn *ip, expressionS *ex)
252b5132
RH
3496{
3497 if (ex->X_op == O_big)
3498 as_bad (_("unsupported large constant"));
3499 else if (ex->X_op != O_constant)
3500 as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
13757d0c
TS
3501
3502 normalize_constant_expr (ex);
252b5132
RH
3503}
3504
3505/* Count the leading zeroes by performing a binary chop. This is a
3506 bulky bit of source, but performance is a LOT better for the
3507 majority of values than a simple loop to count the bits:
3508 for (lcnt = 0; (lcnt < 32); lcnt++)
3509 if ((v) & (1 << (31 - lcnt)))
3510 break;
3511 However it is not code size friendly, and the gain will drop a bit
3512 on certain cached systems.
3513*/
3514#define COUNT_TOP_ZEROES(v) \
3515 (((v) & ~0xffff) == 0 \
3516 ? ((v) & ~0xff) == 0 \
3517 ? ((v) & ~0xf) == 0 \
3518 ? ((v) & ~0x3) == 0 \
3519 ? ((v) & ~0x1) == 0 \
3520 ? !(v) \
3521 ? 32 \
3522 : 31 \
3523 : 30 \
3524 : ((v) & ~0x7) == 0 \
3525 ? 29 \
3526 : 28 \
3527 : ((v) & ~0x3f) == 0 \
3528 ? ((v) & ~0x1f) == 0 \
3529 ? 27 \
3530 : 26 \
3531 : ((v) & ~0x7f) == 0 \
3532 ? 25 \
3533 : 24 \
3534 : ((v) & ~0xfff) == 0 \
3535 ? ((v) & ~0x3ff) == 0 \
3536 ? ((v) & ~0x1ff) == 0 \
3537 ? 23 \
3538 : 22 \
3539 : ((v) & ~0x7ff) == 0 \
3540 ? 21 \
3541 : 20 \
3542 : ((v) & ~0x3fff) == 0 \
3543 ? ((v) & ~0x1fff) == 0 \
3544 ? 19 \
3545 : 18 \
3546 : ((v) & ~0x7fff) == 0 \
3547 ? 17 \
3548 : 16 \
3549 : ((v) & ~0xffffff) == 0 \
3550 ? ((v) & ~0xfffff) == 0 \
3551 ? ((v) & ~0x3ffff) == 0 \
3552 ? ((v) & ~0x1ffff) == 0 \
3553 ? 15 \
3554 : 14 \
3555 : ((v) & ~0x7ffff) == 0 \
3556 ? 13 \
3557 : 12 \
3558 : ((v) & ~0x3fffff) == 0 \
3559 ? ((v) & ~0x1fffff) == 0 \
3560 ? 11 \
3561 : 10 \
3562 : ((v) & ~0x7fffff) == 0 \
3563 ? 9 \
3564 : 8 \
3565 : ((v) & ~0xfffffff) == 0 \
3566 ? ((v) & ~0x3ffffff) == 0 \
3567 ? ((v) & ~0x1ffffff) == 0 \
3568 ? 7 \
3569 : 6 \
3570 : ((v) & ~0x7ffffff) == 0 \
3571 ? 5 \
3572 : 4 \
3573 : ((v) & ~0x3fffffff) == 0 \
3574 ? ((v) & ~0x1fffffff) == 0 \
3575 ? 3 \
3576 : 2 \
3577 : ((v) & ~0x7fffffff) == 0 \
3578 ? 1 \
3579 : 0)
3580
3581/* load_register()
67c1ffbe 3582 * This routine generates the least number of instructions necessary to load
252b5132
RH
3583 * an absolute expression value into a register.
3584 */
3585static void
67c0d1eb 3586load_register (int reg, expressionS *ep, int dbl)
252b5132
RH
3587{
3588 int freg;
3589 expressionS hi32, lo32;
3590
3591 if (ep->X_op != O_big)
3592 {
3593 assert (ep->X_op == O_constant);
256ab948
TS
3594
3595 /* Sign-extending 32-bit constants makes their handling easier. */
d17e7bce
TS
3596 if (! dbl && ! ((ep->X_add_number & ~((bfd_vma) 0x7fffffff))
3597 == ~((bfd_vma) 0x7fffffff)))
ae826530 3598 {
d17e7bce 3599 if (ep->X_add_number & ~((bfd_vma) 0xffffffff))
1b8e29e5 3600 as_bad (_("constant too large"));
ae826530 3601
1b8e29e5
TS
3602 ep->X_add_number = (((ep->X_add_number & 0xffffffff) ^ 0x80000000)
3603 - 0x80000000);
ae826530 3604 }
256ab948
TS
3605
3606 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
252b5132
RH
3607 {
3608 /* We can handle 16 bit signed values with an addiu to
3609 $zero. No need to ever use daddiu here, since $zero and
3610 the result are always correct in 32 bit mode. */
67c0d1eb 3611 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
3612 return;
3613 }
3614 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3615 {
3616 /* We can handle 16 bit unsigned values with an ori to
3617 $zero. */
67c0d1eb 3618 macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
252b5132
RH
3619 return;
3620 }
256ab948 3621 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
252b5132
RH
3622 {
3623 /* 32 bit values require an lui. */
67c0d1eb 3624 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_HI16);
252b5132 3625 if ((ep->X_add_number & 0xffff) != 0)
67c0d1eb 3626 macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
252b5132
RH
3627 return;
3628 }
3629 }
3630
3631 /* The value is larger than 32 bits. */
3632
ca4e0257 3633 if (HAVE_32BIT_GPRS)
252b5132 3634 {
956cd1d6
TS
3635 as_bad (_("Number (0x%lx) larger than 32 bits"),
3636 (unsigned long) ep->X_add_number);
67c0d1eb 3637 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
3638 return;
3639 }
3640
3641 if (ep->X_op != O_big)
3642 {
3643 hi32 = *ep;
3644 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3645 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3646 hi32.X_add_number &= 0xffffffff;
3647 lo32 = *ep;
3648 lo32.X_add_number &= 0xffffffff;
3649 }
3650 else
3651 {
3652 assert (ep->X_add_number > 2);
3653 if (ep->X_add_number == 3)
3654 generic_bignum[3] = 0;
3655 else if (ep->X_add_number > 4)
3656 as_bad (_("Number larger than 64 bits"));
3657 lo32.X_op = O_constant;
3658 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3659 hi32.X_op = O_constant;
3660 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3661 }
3662
3663 if (hi32.X_add_number == 0)
3664 freg = 0;
3665 else
3666 {
3667 int shift, bit;
3668 unsigned long hi, lo;
3669
956cd1d6 3670 if (hi32.X_add_number == (offsetT) 0xffffffff)
beae10d5
KH
3671 {
3672 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3673 {
67c0d1eb 3674 macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
3675 return;
3676 }
3677 if (lo32.X_add_number & 0x80000000)
3678 {
67c0d1eb 3679 macro_build (&lo32, "lui", "t,u", reg, BFD_RELOC_HI16);
252b5132 3680 if (lo32.X_add_number & 0xffff)
67c0d1eb 3681 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
beae10d5
KH
3682 return;
3683 }
3684 }
252b5132
RH
3685
3686 /* Check for 16bit shifted constant. We know that hi32 is
3687 non-zero, so start the mask on the first bit of the hi32
3688 value. */
3689 shift = 17;
3690 do
beae10d5
KH
3691 {
3692 unsigned long himask, lomask;
3693
3694 if (shift < 32)
3695 {
3696 himask = 0xffff >> (32 - shift);
3697 lomask = (0xffff << shift) & 0xffffffff;
3698 }
3699 else
3700 {
3701 himask = 0xffff << (shift - 32);
3702 lomask = 0;
3703 }
3704 if ((hi32.X_add_number & ~(offsetT) himask) == 0
3705 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3706 {
3707 expressionS tmp;
3708
3709 tmp.X_op = O_constant;
3710 if (shift < 32)
3711 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3712 | (lo32.X_add_number >> shift));
3713 else
3714 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
67c0d1eb
RS
3715 macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
3716 macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", "d,w,<",
3717 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
3718 return;
3719 }
f9419b05 3720 ++shift;
beae10d5
KH
3721 }
3722 while (shift <= (64 - 16));
252b5132
RH
3723
3724 /* Find the bit number of the lowest one bit, and store the
3725 shifted value in hi/lo. */
3726 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3727 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3728 if (lo != 0)
3729 {
3730 bit = 0;
3731 while ((lo & 1) == 0)
3732 {
3733 lo >>= 1;
3734 ++bit;
3735 }
3736 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3737 hi >>= bit;
3738 }
3739 else
3740 {
3741 bit = 32;
3742 while ((hi & 1) == 0)
3743 {
3744 hi >>= 1;
3745 ++bit;
3746 }
3747 lo = hi;
3748 hi = 0;
3749 }
3750
3751 /* Optimize if the shifted value is a (power of 2) - 1. */
3752 if ((hi == 0 && ((lo + 1) & lo) == 0)
3753 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
beae10d5
KH
3754 {
3755 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
252b5132 3756 if (shift != 0)
beae10d5 3757 {
252b5132
RH
3758 expressionS tmp;
3759
3760 /* This instruction will set the register to be all
3761 ones. */
beae10d5
KH
3762 tmp.X_op = O_constant;
3763 tmp.X_add_number = (offsetT) -1;
67c0d1eb 3764 macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
3765 if (bit != 0)
3766 {
3767 bit += shift;
67c0d1eb
RS
3768 macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", "d,w,<",
3769 reg, reg, (bit >= 32) ? bit - 32 : bit);
beae10d5 3770 }
67c0d1eb
RS
3771 macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", "d,w,<",
3772 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
3773 return;
3774 }
3775 }
252b5132
RH
3776
3777 /* Sign extend hi32 before calling load_register, because we can
3778 generally get better code when we load a sign extended value. */
3779 if ((hi32.X_add_number & 0x80000000) != 0)
beae10d5 3780 hi32.X_add_number |= ~(offsetT) 0xffffffff;
67c0d1eb 3781 load_register (reg, &hi32, 0);
252b5132
RH
3782 freg = reg;
3783 }
3784 if ((lo32.X_add_number & 0xffff0000) == 0)
3785 {
3786 if (freg != 0)
3787 {
67c0d1eb 3788 macro_build (NULL, "dsll32", "d,w,<", reg, freg, 0);
252b5132
RH
3789 freg = reg;
3790 }
3791 }
3792 else
3793 {
3794 expressionS mid16;
3795
956cd1d6 3796 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
beae10d5 3797 {
67c0d1eb
RS
3798 macro_build (&lo32, "lui", "t,u", reg, BFD_RELOC_HI16);
3799 macro_build (NULL, "dsrl32", "d,w,<", reg, reg, 0);
beae10d5
KH
3800 return;
3801 }
252b5132
RH
3802
3803 if (freg != 0)
3804 {
67c0d1eb 3805 macro_build (NULL, "dsll", "d,w,<", reg, freg, 16);
252b5132
RH
3806 freg = reg;
3807 }
3808 mid16 = lo32;
3809 mid16.X_add_number >>= 16;
67c0d1eb
RS
3810 macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
3811 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
252b5132
RH
3812 freg = reg;
3813 }
3814 if ((lo32.X_add_number & 0xffff) != 0)
67c0d1eb 3815 macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
252b5132
RH
3816}
3817
269137b2
TS
3818static inline void
3819load_delay_nop (void)
3820{
3821 if (!gpr_interlocks)
3822 macro_build (NULL, "nop", "");
3823}
3824
252b5132
RH
3825/* Load an address into a register. */
3826
3827static void
67c0d1eb 3828load_address (int reg, expressionS *ep, int *used_at)
252b5132 3829{
252b5132
RH
3830 if (ep->X_op != O_constant
3831 && ep->X_op != O_symbol)
3832 {
3833 as_bad (_("expression too complex"));
3834 ep->X_op = O_constant;
3835 }
3836
3837 if (ep->X_op == O_constant)
3838 {
67c0d1eb 3839 load_register (reg, ep, HAVE_64BIT_ADDRESSES);
252b5132
RH
3840 return;
3841 }
3842
3843 if (mips_pic == NO_PIC)
3844 {
3845 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 3846 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
3847 Otherwise we want
3848 lui $reg,<sym> (BFD_RELOC_HI16_S)
3849 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
d6bc6245 3850 If we have an addend, we always use the latter form.
76b3015f 3851
d6bc6245
TS
3852 With 64bit address space and a usable $at we want
3853 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3854 lui $at,<sym> (BFD_RELOC_HI16_S)
3855 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3856 daddiu $at,<sym> (BFD_RELOC_LO16)
3857 dsll32 $reg,0
3a482fd5 3858 daddu $reg,$reg,$at
76b3015f 3859
c03099e6 3860 If $at is already in use, we use a path which is suboptimal
d6bc6245
TS
3861 on superscalar processors.
3862 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3863 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3864 dsll $reg,16
3865 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
3866 dsll $reg,16
3867 daddiu $reg,<sym> (BFD_RELOC_LO16)
3868 */
c9914766 3869 if (HAVE_64BIT_ADDRESSES)
d6bc6245 3870 {
e864ceca
RS
3871 /* ??? We don't provide a GP-relative alternative for these macros.
3872 It used not to be possible with the original relaxation code,
3873 but it could be done now. */
d6bc6245 3874
460597ba 3875 if (*used_at == 0 && ! mips_opts.noat)
d6bc6245 3876 {
67c0d1eb
RS
3877 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_HIGHEST);
3878 macro_build (ep, "lui", "t,u", AT, BFD_RELOC_HI16_S);
3879 macro_build (ep, "daddiu", "t,r,j", reg, reg,
3880 BFD_RELOC_MIPS_HIGHER);
3881 macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
3882 macro_build (NULL, "dsll32", "d,w,<", reg, reg, 0);
3883 macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
d6bc6245
TS
3884 *used_at = 1;
3885 }
3886 else
3887 {
67c0d1eb
RS
3888 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_HIGHEST);
3889 macro_build (ep, "daddiu", "t,r,j", reg, reg,
3890 BFD_RELOC_MIPS_HIGHER);
3891 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
3892 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
3893 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
3894 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
d6bc6245
TS
3895 }
3896 }
252b5132
RH
3897 else
3898 {
d6bc6245
TS
3899 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3900 && ! nopic_need_relax (ep->X_add_symbol, 1))
3901 {
4d7206a2 3902 relax_start (ep->X_add_symbol);
67c0d1eb 3903 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
17a2f251 3904 mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 3905 relax_switch ();
d6bc6245 3906 }
67c0d1eb
RS
3907 macro_build_lui (ep, reg);
3908 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
3909 reg, reg, BFD_RELOC_LO16);
4d7206a2
RS
3910 if (mips_relax.sequence)
3911 relax_end ();
d6bc6245 3912 }
252b5132
RH
3913 }
3914 else if (mips_pic == SVR4_PIC && ! mips_big_got)
3915 {
3916 expressionS ex;
3917
3918 /* If this is a reference to an external symbol, we want
3919 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3920 Otherwise we want
3921 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3922 nop
3923 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
f5040a92
AO
3924 If there is a constant, it must be added in after.
3925
ed6fb7bd 3926 If we have NewABI, we want
f5040a92
AO
3927 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
3928 unless we're referencing a global symbol with a non-zero
3929 offset, in which case cst must be added separately. */
ed6fb7bd
SC
3930 if (HAVE_NEWABI)
3931 {
f5040a92
AO
3932 if (ep->X_add_number)
3933 {
4d7206a2 3934 ex.X_add_number = ep->X_add_number;
f5040a92 3935 ep->X_add_number = 0;
4d7206a2 3936 relax_start (ep->X_add_symbol);
67c0d1eb
RS
3937 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3938 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
3939 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3940 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3941 ex.X_op = O_constant;
67c0d1eb 3942 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 3943 reg, reg, BFD_RELOC_LO16);
f5040a92 3944 ep->X_add_number = ex.X_add_number;
4d7206a2 3945 relax_switch ();
f5040a92 3946 }
67c0d1eb 3947 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 3948 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2
RS
3949 if (mips_relax.sequence)
3950 relax_end ();
ed6fb7bd
SC
3951 }
3952 else
3953 {
f5040a92
AO
3954 ex.X_add_number = ep->X_add_number;
3955 ep->X_add_number = 0;
67c0d1eb
RS
3956 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3957 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 3958 load_delay_nop ();
4d7206a2
RS
3959 relax_start (ep->X_add_symbol);
3960 relax_switch ();
67c0d1eb 3961 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 3962 BFD_RELOC_LO16);
4d7206a2 3963 relax_end ();
ed6fb7bd 3964
f5040a92
AO
3965 if (ex.X_add_number != 0)
3966 {
3967 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3968 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3969 ex.X_op = O_constant;
67c0d1eb 3970 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 3971 reg, reg, BFD_RELOC_LO16);
f5040a92 3972 }
252b5132
RH
3973 }
3974 }
3975 else if (mips_pic == SVR4_PIC)
3976 {
3977 expressionS ex;
252b5132
RH
3978
3979 /* This is the large GOT case. If this is a reference to an
3980 external symbol, we want
3981 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
3982 addu $reg,$reg,$gp
3983 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
f5040a92
AO
3984
3985 Otherwise, for a reference to a local symbol in old ABI, we want
252b5132
RH
3986 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3987 nop
3988 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
684022ea 3989 If there is a constant, it must be added in after.
f5040a92
AO
3990
3991 In the NewABI, for local symbols, with or without offsets, we want:
438c16b8
TS
3992 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
3993 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
f5040a92 3994 */
438c16b8
TS
3995 if (HAVE_NEWABI)
3996 {
4d7206a2 3997 ex.X_add_number = ep->X_add_number;
f5040a92 3998 ep->X_add_number = 0;
4d7206a2 3999 relax_start (ep->X_add_symbol);
67c0d1eb
RS
4000 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_GOT_HI16);
4001 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
4002 reg, reg, mips_gp_register);
4003 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
4004 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
f5040a92
AO
4005 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4006 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4007 else if (ex.X_add_number)
4008 {
4009 ex.X_op = O_constant;
67c0d1eb
RS
4010 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
4011 BFD_RELOC_LO16);
f5040a92
AO
4012 }
4013
4014 ep->X_add_number = ex.X_add_number;
4d7206a2 4015 relax_switch ();
67c0d1eb 4016 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 4017 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
67c0d1eb
RS
4018 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
4019 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 4020 relax_end ();
438c16b8 4021 }
252b5132 4022 else
438c16b8 4023 {
f5040a92
AO
4024 ex.X_add_number = ep->X_add_number;
4025 ep->X_add_number = 0;
4d7206a2 4026 relax_start (ep->X_add_symbol);
67c0d1eb
RS
4027 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_GOT_HI16);
4028 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
4029 reg, reg, mips_gp_register);
4030 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
4031 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
4d7206a2
RS
4032 relax_switch ();
4033 if (reg_needs_delay (mips_gp_register))
438c16b8
TS
4034 {
4035 /* We need a nop before loading from $gp. This special
4036 check is required because the lui which starts the main
4037 instruction stream does not refer to $gp, and so will not
4038 insert the nop which may be required. */
67c0d1eb 4039 macro_build (NULL, "nop", "");
438c16b8 4040 }
67c0d1eb 4041 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 4042 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 4043 load_delay_nop ();
67c0d1eb 4044 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 4045 BFD_RELOC_LO16);
4d7206a2 4046 relax_end ();
438c16b8 4047
f5040a92
AO
4048 if (ex.X_add_number != 0)
4049 {
4050 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4051 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4052 ex.X_op = O_constant;
67c0d1eb
RS
4053 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
4054 BFD_RELOC_LO16);
f5040a92 4055 }
252b5132
RH
4056 }
4057 }
252b5132
RH
4058 else
4059 abort ();
4060}
4061
ea1fb5dc
RS
4062/* Move the contents of register SOURCE into register DEST. */
4063
4064static void
67c0d1eb 4065move_register (int dest, int source)
ea1fb5dc 4066{
67c0d1eb
RS
4067 macro_build (NULL, HAVE_32BIT_GPRS ? "addu" : "daddu", "d,v,t",
4068 dest, source, 0);
ea1fb5dc
RS
4069}
4070
4d7206a2 4071/* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
f6a22291
MR
4072 LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
4073 The two alternatives are:
4d7206a2
RS
4074
4075 Global symbol Local sybmol
4076 ------------- ------------
4077 lw DEST,%got(SYMBOL) lw DEST,%got(SYMBOL + OFFSET)
4078 ... ...
4079 addiu DEST,DEST,OFFSET addiu DEST,DEST,%lo(SYMBOL + OFFSET)
4080
4081 load_got_offset emits the first instruction and add_got_offset
f6a22291
MR
4082 emits the second for a 16-bit offset or add_got_offset_hilo emits
4083 a sequence to add a 32-bit offset using a scratch register. */
4d7206a2
RS
4084
4085static void
67c0d1eb 4086load_got_offset (int dest, expressionS *local)
4d7206a2
RS
4087{
4088 expressionS global;
4089
4090 global = *local;
4091 global.X_add_number = 0;
4092
4093 relax_start (local->X_add_symbol);
67c0d1eb
RS
4094 macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
4095 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2 4096 relax_switch ();
67c0d1eb
RS
4097 macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
4098 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2
RS
4099 relax_end ();
4100}
4101
4102static void
67c0d1eb 4103add_got_offset (int dest, expressionS *local)
4d7206a2
RS
4104{
4105 expressionS global;
4106
4107 global.X_op = O_constant;
4108 global.X_op_symbol = NULL;
4109 global.X_add_symbol = NULL;
4110 global.X_add_number = local->X_add_number;
4111
4112 relax_start (local->X_add_symbol);
67c0d1eb 4113 macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
4d7206a2
RS
4114 dest, dest, BFD_RELOC_LO16);
4115 relax_switch ();
67c0d1eb 4116 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
4d7206a2
RS
4117 relax_end ();
4118}
4119
f6a22291
MR
4120static void
4121add_got_offset_hilo (int dest, expressionS *local, int tmp)
4122{
4123 expressionS global;
4124 int hold_mips_optimize;
4125
4126 global.X_op = O_constant;
4127 global.X_op_symbol = NULL;
4128 global.X_add_symbol = NULL;
4129 global.X_add_number = local->X_add_number;
4130
4131 relax_start (local->X_add_symbol);
4132 load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
4133 relax_switch ();
4134 /* Set mips_optimize around the lui instruction to avoid
4135 inserting an unnecessary nop after the lw. */
4136 hold_mips_optimize = mips_optimize;
4137 mips_optimize = 2;
4138 macro_build_lui (&global, tmp);
4139 mips_optimize = hold_mips_optimize;
4140 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
4141 relax_end ();
4142
4143 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
4144}
4145
252b5132
RH
4146/*
4147 * Build macros
4148 * This routine implements the seemingly endless macro or synthesized
4149 * instructions and addressing modes in the mips assembly language. Many
4150 * of these macros are simple and are similar to each other. These could
67c1ffbe 4151 * probably be handled by some kind of table or grammar approach instead of
252b5132
RH
4152 * this verbose method. Others are not simple macros but are more like
4153 * optimizing code generation.
4154 * One interesting optimization is when several store macros appear
67c1ffbe 4155 * consecutively that would load AT with the upper half of the same address.
252b5132
RH
4156 * The ensuing load upper instructions are ommited. This implies some kind
4157 * of global optimization. We currently only optimize within a single macro.
4158 * For many of the load and store macros if the address is specified as a
4159 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
4160 * first load register 'at' with zero and use it as the base register. The
4161 * mips assembler simply uses register $zero. Just one tiny optimization
4162 * we're missing.
4163 */
4164static void
17a2f251 4165macro (struct mips_cl_insn *ip)
252b5132
RH
4166{
4167 register int treg, sreg, dreg, breg;
4168 int tempreg;
4169 int mask;
43841e91 4170 int used_at = 0;
252b5132
RH
4171 expressionS expr1;
4172 const char *s;
4173 const char *s2;
4174 const char *fmt;
4175 int likely = 0;
4176 int dbl = 0;
4177 int coproc = 0;
4178 int lr = 0;
4179 int imm = 0;
1abe91b1 4180 int call = 0;
252b5132 4181 int off;
67c0d1eb 4182 offsetT maxnum;
252b5132 4183 bfd_reloc_code_real_type r;
252b5132
RH
4184 int hold_mips_optimize;
4185
4186 assert (! mips_opts.mips16);
4187
4188 treg = (ip->insn_opcode >> 16) & 0x1f;
4189 dreg = (ip->insn_opcode >> 11) & 0x1f;
4190 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
4191 mask = ip->insn_mo->mask;
4192
4193 expr1.X_op = O_constant;
4194 expr1.X_op_symbol = NULL;
4195 expr1.X_add_symbol = NULL;
4196 expr1.X_add_number = 1;
4197
4198 switch (mask)
4199 {
4200 case M_DABS:
4201 dbl = 1;
4202 case M_ABS:
4203 /* bgez $a0,.+12
4204 move v0,$a0
4205 sub v0,$zero,$a0
4206 */
4207
b34976b6 4208 mips_emit_delays (TRUE);
252b5132
RH
4209 ++mips_opts.noreorder;
4210 mips_any_noreorder = 1;
4211
4212 expr1.X_add_number = 8;
67c0d1eb 4213 macro_build (&expr1, "bgez", "s,p", sreg);
252b5132 4214 if (dreg == sreg)
67c0d1eb 4215 macro_build (NULL, "nop", "", 0);
252b5132 4216 else
67c0d1eb
RS
4217 move_register (dreg, sreg);
4218 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
252b5132
RH
4219
4220 --mips_opts.noreorder;
4221 return;
4222
4223 case M_ADD_I:
4224 s = "addi";
4225 s2 = "add";
4226 goto do_addi;
4227 case M_ADDU_I:
4228 s = "addiu";
4229 s2 = "addu";
4230 goto do_addi;
4231 case M_DADD_I:
4232 dbl = 1;
4233 s = "daddi";
4234 s2 = "dadd";
4235 goto do_addi;
4236 case M_DADDU_I:
4237 dbl = 1;
4238 s = "daddiu";
4239 s2 = "daddu";
4240 do_addi:
4241 if (imm_expr.X_op == O_constant
4242 && imm_expr.X_add_number >= -0x8000
4243 && imm_expr.X_add_number < 0x8000)
4244 {
67c0d1eb 4245 macro_build (&imm_expr, s, "t,r,j", treg, sreg, BFD_RELOC_LO16);
252b5132
RH
4246 return;
4247 }
67c0d1eb
RS
4248 load_register (AT, &imm_expr, dbl);
4249 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
252b5132
RH
4250 break;
4251
4252 case M_AND_I:
4253 s = "andi";
4254 s2 = "and";
4255 goto do_bit;
4256 case M_OR_I:
4257 s = "ori";
4258 s2 = "or";
4259 goto do_bit;
4260 case M_NOR_I:
4261 s = "";
4262 s2 = "nor";
4263 goto do_bit;
4264 case M_XOR_I:
4265 s = "xori";
4266 s2 = "xor";
4267 do_bit:
4268 if (imm_expr.X_op == O_constant
4269 && imm_expr.X_add_number >= 0
4270 && imm_expr.X_add_number < 0x10000)
4271 {
4272 if (mask != M_NOR_I)
67c0d1eb 4273 macro_build (&imm_expr, s, "t,r,i", treg, sreg, BFD_RELOC_LO16);
252b5132
RH
4274 else
4275 {
67c0d1eb
RS
4276 macro_build (&imm_expr, "ori", "t,r,i",
4277 treg, sreg, BFD_RELOC_LO16);
4278 macro_build (NULL, "nor", "d,v,t", treg, treg, 0);
252b5132
RH
4279 }
4280 return;
4281 }
4282
67c0d1eb
RS
4283 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
4284 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
252b5132
RH
4285 break;
4286
4287 case M_BEQ_I:
4288 s = "beq";
4289 goto beq_i;
4290 case M_BEQL_I:
4291 s = "beql";
4292 likely = 1;
4293 goto beq_i;
4294 case M_BNE_I:
4295 s = "bne";
4296 goto beq_i;
4297 case M_BNEL_I:
4298 s = "bnel";
4299 likely = 1;
4300 beq_i:
4301 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4302 {
67c0d1eb 4303 macro_build (&offset_expr, s, "s,t,p", sreg, 0);
252b5132
RH
4304 return;
4305 }
67c0d1eb
RS
4306 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
4307 macro_build (&offset_expr, s, "s,t,p", sreg, AT);
252b5132
RH
4308 break;
4309
4310 case M_BGEL:
4311 likely = 1;
4312 case M_BGE:
4313 if (treg == 0)
4314 {
67c0d1eb 4315 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", sreg);
252b5132
RH
4316 return;
4317 }
4318 if (sreg == 0)
4319 {
67c0d1eb 4320 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", treg);
252b5132
RH
4321 return;
4322 }
67c0d1eb
RS
4323 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
4324 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4325 break;
4326
4327 case M_BGTL_I:
4328 likely = 1;
4329 case M_BGT_I:
4330 /* check for > max integer */
4331 maxnum = 0x7fffffff;
ca4e0257 4332 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
4333 {
4334 maxnum <<= 16;
4335 maxnum |= 0xffff;
4336 maxnum <<= 16;
4337 maxnum |= 0xffff;
4338 }
4339 if (imm_expr.X_op == O_constant
4340 && imm_expr.X_add_number >= maxnum
ca4e0257 4341 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
4342 {
4343 do_false:
4344 /* result is always false */
4345 if (! likely)
67c0d1eb 4346 macro_build (NULL, "nop", "", 0);
252b5132 4347 else
67c0d1eb 4348 macro_build (&offset_expr, "bnel", "s,t,p", 0, 0);
252b5132
RH
4349 return;
4350 }
4351 if (imm_expr.X_op != O_constant)
4352 as_bad (_("Unsupported large constant"));
f9419b05 4353 ++imm_expr.X_add_number;
252b5132
RH
4354 /* FALLTHROUGH */
4355 case M_BGE_I:
4356 case M_BGEL_I:
4357 if (mask == M_BGEL_I)
4358 likely = 1;
4359 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4360 {
67c0d1eb 4361 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", sreg);
252b5132
RH
4362 return;
4363 }
4364 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4365 {
67c0d1eb 4366 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", sreg);
252b5132
RH
4367 return;
4368 }
4369 maxnum = 0x7fffffff;
ca4e0257 4370 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
4371 {
4372 maxnum <<= 16;
4373 maxnum |= 0xffff;
4374 maxnum <<= 16;
4375 maxnum |= 0xffff;
4376 }
4377 maxnum = - maxnum - 1;
4378 if (imm_expr.X_op == O_constant
4379 && imm_expr.X_add_number <= maxnum
ca4e0257 4380 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
4381 {
4382 do_true:
4383 /* result is always true */
4384 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
67c0d1eb 4385 macro_build (&offset_expr, "b", "p");
252b5132
RH
4386 return;
4387 }
67c0d1eb
RS
4388 set_at (sreg, 0);
4389 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4390 break;
4391
4392 case M_BGEUL:
4393 likely = 1;
4394 case M_BGEU:
4395 if (treg == 0)
4396 goto do_true;
4397 if (sreg == 0)
4398 {
67c0d1eb 4399 macro_build (&offset_expr, likely ? "beql" : "beq",
17a2f251 4400 "s,t,p", 0, treg);
252b5132
RH
4401 return;
4402 }
67c0d1eb
RS
4403 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
4404 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4405 break;
4406
4407 case M_BGTUL_I:
4408 likely = 1;
4409 case M_BGTU_I:
4410 if (sreg == 0
ca4e0257 4411 || (HAVE_32BIT_GPRS
252b5132 4412 && imm_expr.X_op == O_constant
956cd1d6 4413 && imm_expr.X_add_number == (offsetT) 0xffffffff))
252b5132
RH
4414 goto do_false;
4415 if (imm_expr.X_op != O_constant)
4416 as_bad (_("Unsupported large constant"));
f9419b05 4417 ++imm_expr.X_add_number;
252b5132
RH
4418 /* FALLTHROUGH */
4419 case M_BGEU_I:
4420 case M_BGEUL_I:
4421 if (mask == M_BGEUL_I)
4422 likely = 1;
4423 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4424 goto do_true;
4425 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4426 {
67c0d1eb 4427 macro_build (&offset_expr, likely ? "bnel" : "bne",
17a2f251 4428 "s,t,p", sreg, 0);
252b5132
RH
4429 return;
4430 }
67c0d1eb
RS
4431 set_at (sreg, 1);
4432 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4433 break;
4434
4435 case M_BGTL:
4436 likely = 1;
4437 case M_BGT:
4438 if (treg == 0)
4439 {
67c0d1eb 4440 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", sreg);
252b5132
RH
4441 return;
4442 }
4443 if (sreg == 0)
4444 {
67c0d1eb 4445 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", treg);
252b5132
RH
4446 return;
4447 }
67c0d1eb
RS
4448 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
4449 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4450 break;
4451
4452 case M_BGTUL:
4453 likely = 1;
4454 case M_BGTU:
4455 if (treg == 0)
4456 {
67c0d1eb 4457 macro_build (&offset_expr, likely ? "bnel" : "bne",
17a2f251 4458 "s,t,p", sreg, 0);
252b5132
RH
4459 return;
4460 }
4461 if (sreg == 0)
4462 goto do_false;
67c0d1eb
RS
4463 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
4464 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4465 break;
4466
4467 case M_BLEL:
4468 likely = 1;
4469 case M_BLE:
4470 if (treg == 0)
4471 {
67c0d1eb 4472 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", sreg);
252b5132
RH
4473 return;
4474 }
4475 if (sreg == 0)
4476 {
67c0d1eb 4477 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", treg);
252b5132
RH
4478 return;
4479 }
67c0d1eb
RS
4480 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
4481 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4482 break;
4483
4484 case M_BLEL_I:
4485 likely = 1;
4486 case M_BLE_I:
4487 maxnum = 0x7fffffff;
ca4e0257 4488 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
4489 {
4490 maxnum <<= 16;
4491 maxnum |= 0xffff;
4492 maxnum <<= 16;
4493 maxnum |= 0xffff;
4494 }
4495 if (imm_expr.X_op == O_constant
4496 && imm_expr.X_add_number >= maxnum
ca4e0257 4497 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
4498 goto do_true;
4499 if (imm_expr.X_op != O_constant)
4500 as_bad (_("Unsupported large constant"));
f9419b05 4501 ++imm_expr.X_add_number;
252b5132
RH
4502 /* FALLTHROUGH */
4503 case M_BLT_I:
4504 case M_BLTL_I:
4505 if (mask == M_BLTL_I)
4506 likely = 1;
4507 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4508 {
67c0d1eb 4509 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", sreg);
252b5132
RH
4510 return;
4511 }
4512 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4513 {
67c0d1eb 4514 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", sreg);
252b5132
RH
4515 return;
4516 }
67c0d1eb
RS
4517 set_at (sreg, 0);
4518 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4519 break;
4520
4521 case M_BLEUL:
4522 likely = 1;
4523 case M_BLEU:
4524 if (treg == 0)
4525 {
67c0d1eb 4526 macro_build (&offset_expr, likely ? "beql" : "beq",
17a2f251 4527 "s,t,p", sreg, 0);
252b5132
RH
4528 return;
4529 }
4530 if (sreg == 0)
4531 goto do_true;
67c0d1eb
RS
4532 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
4533 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4534 break;
4535
4536 case M_BLEUL_I:
4537 likely = 1;
4538 case M_BLEU_I:
4539 if (sreg == 0
ca4e0257 4540 || (HAVE_32BIT_GPRS
252b5132 4541 && imm_expr.X_op == O_constant
956cd1d6 4542 && imm_expr.X_add_number == (offsetT) 0xffffffff))
252b5132
RH
4543 goto do_true;
4544 if (imm_expr.X_op != O_constant)
4545 as_bad (_("Unsupported large constant"));
f9419b05 4546 ++imm_expr.X_add_number;
252b5132
RH
4547 /* FALLTHROUGH */
4548 case M_BLTU_I:
4549 case M_BLTUL_I:
4550 if (mask == M_BLTUL_I)
4551 likely = 1;
4552 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4553 goto do_false;
4554 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4555 {
67c0d1eb 4556 macro_build (&offset_expr, likely ? "beql" : "beq",
252b5132
RH
4557 "s,t,p", sreg, 0);
4558 return;
4559 }
67c0d1eb
RS
4560 set_at (sreg, 1);
4561 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4562 break;
4563
4564 case M_BLTL:
4565 likely = 1;
4566 case M_BLT:
4567 if (treg == 0)
4568 {
67c0d1eb 4569 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", sreg);
252b5132
RH
4570 return;
4571 }
4572 if (sreg == 0)
4573 {
67c0d1eb 4574 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", treg);
252b5132
RH
4575 return;
4576 }
67c0d1eb
RS
4577 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
4578 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4579 break;
4580
4581 case M_BLTUL:
4582 likely = 1;
4583 case M_BLTU:
4584 if (treg == 0)
4585 goto do_false;
4586 if (sreg == 0)
4587 {
67c0d1eb 4588 macro_build (&offset_expr, likely ? "bnel" : "bne",
17a2f251 4589 "s,t,p", 0, treg);
252b5132
RH
4590 return;
4591 }
67c0d1eb
RS
4592 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
4593 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4594 break;
4595
5f74bc13
CD
4596 case M_DEXT:
4597 {
4598 unsigned long pos;
4599 unsigned long size;
4600
4601 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
4602 {
4603 as_bad (_("Unsupported large constant"));
4604 pos = size = 1;
4605 }
4606 else
4607 {
4608 pos = (unsigned long) imm_expr.X_add_number;
4609 size = (unsigned long) imm2_expr.X_add_number;
4610 }
4611
4612 if (pos > 63)
4613 {
4614 as_bad (_("Improper position (%lu)"), pos);
4615 pos = 1;
4616 }
4617 if (size == 0 || size > 64
4618 || (pos + size - 1) > 63)
4619 {
4620 as_bad (_("Improper extract size (%lu, position %lu)"),
4621 size, pos);
4622 size = 1;
4623 }
4624
4625 if (size <= 32 && pos < 32)
4626 {
4627 s = "dext";
4628 fmt = "t,r,+A,+C";
4629 }
4630 else if (size <= 32)
4631 {
4632 s = "dextu";
4633 fmt = "t,r,+E,+H";
4634 }
4635 else
4636 {
4637 s = "dextm";
4638 fmt = "t,r,+A,+G";
4639 }
67c0d1eb 4640 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, pos, size - 1);
5f74bc13
CD
4641 }
4642 return;
4643
4644 case M_DINS:
4645 {
4646 unsigned long pos;
4647 unsigned long size;
4648
4649 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
4650 {
4651 as_bad (_("Unsupported large constant"));
4652 pos = size = 1;
4653 }
4654 else
4655 {
4656 pos = (unsigned long) imm_expr.X_add_number;
4657 size = (unsigned long) imm2_expr.X_add_number;
4658 }
4659
4660 if (pos > 63)
4661 {
4662 as_bad (_("Improper position (%lu)"), pos);
4663 pos = 1;
4664 }
4665 if (size == 0 || size > 64
4666 || (pos + size - 1) > 63)
4667 {
4668 as_bad (_("Improper insert size (%lu, position %lu)"),
4669 size, pos);
4670 size = 1;
4671 }
4672
4673 if (pos < 32 && (pos + size - 1) < 32)
4674 {
4675 s = "dins";
4676 fmt = "t,r,+A,+B";
4677 }
4678 else if (pos >= 32)
4679 {
4680 s = "dinsu";
4681 fmt = "t,r,+E,+F";
4682 }
4683 else
4684 {
4685 s = "dinsm";
4686 fmt = "t,r,+A,+F";
4687 }
67c0d1eb
RS
4688 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, pos,
4689 pos + size - 1);
5f74bc13
CD
4690 }
4691 return;
4692
252b5132
RH
4693 case M_DDIV_3:
4694 dbl = 1;
4695 case M_DIV_3:
4696 s = "mflo";
4697 goto do_div3;
4698 case M_DREM_3:
4699 dbl = 1;
4700 case M_REM_3:
4701 s = "mfhi";
4702 do_div3:
4703 if (treg == 0)
4704 {
4705 as_warn (_("Divide by zero."));
4706 if (mips_trap)
67c0d1eb 4707 macro_build (NULL, "teq", "s,t,q", 0, 0, 7);
252b5132 4708 else
67c0d1eb 4709 macro_build (NULL, "break", "c", 7);
252b5132
RH
4710 return;
4711 }
4712
b34976b6 4713 mips_emit_delays (TRUE);
252b5132
RH
4714 ++mips_opts.noreorder;
4715 mips_any_noreorder = 1;
4716 if (mips_trap)
4717 {
67c0d1eb
RS
4718 macro_build (NULL, "teq", "s,t,q", treg, 0, 7);
4719 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
252b5132
RH
4720 }
4721 else
4722 {
4723 expr1.X_add_number = 8;
67c0d1eb
RS
4724 macro_build (&expr1, "bne", "s,t,p", treg, 0);
4725 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4726 macro_build (NULL, "break", "c", 7);
252b5132
RH
4727 }
4728 expr1.X_add_number = -1;
f6a22291 4729 load_register (AT, &expr1, dbl);
252b5132 4730 expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
67c0d1eb 4731 macro_build (&expr1, "bne", "s,t,p", treg, AT);
252b5132
RH
4732 if (dbl)
4733 {
4734 expr1.X_add_number = 1;
f6a22291 4735 load_register (AT, &expr1, dbl);
67c0d1eb 4736 macro_build (NULL, "dsll32", "d,w,<", AT, AT, 31);
252b5132
RH
4737 }
4738 else
4739 {
4740 expr1.X_add_number = 0x80000000;
67c0d1eb 4741 macro_build (&expr1, "lui", "t,u", AT, BFD_RELOC_HI16);
252b5132
RH
4742 }
4743 if (mips_trap)
4744 {
67c0d1eb 4745 macro_build (NULL, "teq", "s,t,q", sreg, AT, 6);
252b5132
RH
4746 /* We want to close the noreorder block as soon as possible, so
4747 that later insns are available for delay slot filling. */
4748 --mips_opts.noreorder;
4749 }
4750 else
4751 {
4752 expr1.X_add_number = 8;
67c0d1eb
RS
4753 macro_build (&expr1, "bne", "s,t,p", sreg, AT);
4754 macro_build (NULL, "nop", "", 0);
252b5132
RH
4755
4756 /* We want to close the noreorder block as soon as possible, so
4757 that later insns are available for delay slot filling. */
4758 --mips_opts.noreorder;
4759
67c0d1eb 4760 macro_build (NULL, "break", "c", 6);
252b5132 4761 }
67c0d1eb 4762 macro_build (NULL, s, "d", dreg);
252b5132
RH
4763 break;
4764
4765 case M_DIV_3I:
4766 s = "div";
4767 s2 = "mflo";
4768 goto do_divi;
4769 case M_DIVU_3I:
4770 s = "divu";
4771 s2 = "mflo";
4772 goto do_divi;
4773 case M_REM_3I:
4774 s = "div";
4775 s2 = "mfhi";
4776 goto do_divi;
4777 case M_REMU_3I:
4778 s = "divu";
4779 s2 = "mfhi";
4780 goto do_divi;
4781 case M_DDIV_3I:
4782 dbl = 1;
4783 s = "ddiv";
4784 s2 = "mflo";
4785 goto do_divi;
4786 case M_DDIVU_3I:
4787 dbl = 1;
4788 s = "ddivu";
4789 s2 = "mflo";
4790 goto do_divi;
4791 case M_DREM_3I:
4792 dbl = 1;
4793 s = "ddiv";
4794 s2 = "mfhi";
4795 goto do_divi;
4796 case M_DREMU_3I:
4797 dbl = 1;
4798 s = "ddivu";
4799 s2 = "mfhi";
4800 do_divi:
4801 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4802 {
4803 as_warn (_("Divide by zero."));
4804 if (mips_trap)
67c0d1eb 4805 macro_build (NULL, "teq", "s,t,q", 0, 0, 7);
252b5132 4806 else
67c0d1eb 4807 macro_build (NULL, "break", "c", 7);
252b5132
RH
4808 return;
4809 }
4810 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4811 {
4812 if (strcmp (s2, "mflo") == 0)
67c0d1eb 4813 move_register (dreg, sreg);
252b5132 4814 else
67c0d1eb 4815 move_register (dreg, 0);
252b5132
RH
4816 return;
4817 }
4818 if (imm_expr.X_op == O_constant
4819 && imm_expr.X_add_number == -1
4820 && s[strlen (s) - 1] != 'u')
4821 {
4822 if (strcmp (s2, "mflo") == 0)
4823 {
67c0d1eb 4824 macro_build (NULL, dbl ? "dneg" : "neg", "d,w", dreg, sreg);
252b5132
RH
4825 }
4826 else
67c0d1eb 4827 move_register (dreg, 0);
252b5132
RH
4828 return;
4829 }
4830
67c0d1eb
RS
4831 load_register (AT, &imm_expr, dbl);
4832 macro_build (NULL, s, "z,s,t", sreg, AT);
4833 macro_build (NULL, s2, "d", dreg);
252b5132
RH
4834 break;
4835
4836 case M_DIVU_3:
4837 s = "divu";
4838 s2 = "mflo";
4839 goto do_divu3;
4840 case M_REMU_3:
4841 s = "divu";
4842 s2 = "mfhi";
4843 goto do_divu3;
4844 case M_DDIVU_3:
4845 s = "ddivu";
4846 s2 = "mflo";
4847 goto do_divu3;
4848 case M_DREMU_3:
4849 s = "ddivu";
4850 s2 = "mfhi";
4851 do_divu3:
b34976b6 4852 mips_emit_delays (TRUE);
252b5132
RH
4853 ++mips_opts.noreorder;
4854 mips_any_noreorder = 1;
4855 if (mips_trap)
4856 {
67c0d1eb
RS
4857 macro_build (NULL, "teq", "s,t,q", treg, 0, 7);
4858 macro_build (NULL, s, "z,s,t", sreg, treg);
252b5132
RH
4859 /* We want to close the noreorder block as soon as possible, so
4860 that later insns are available for delay slot filling. */
4861 --mips_opts.noreorder;
4862 }
4863 else
4864 {
4865 expr1.X_add_number = 8;
67c0d1eb
RS
4866 macro_build (&expr1, "bne", "s,t,p", treg, 0);
4867 macro_build (NULL, s, "z,s,t", sreg, treg);
252b5132
RH
4868
4869 /* We want to close the noreorder block as soon as possible, so
4870 that later insns are available for delay slot filling. */
4871 --mips_opts.noreorder;
67c0d1eb 4872 macro_build (NULL, "break", "c", 7);
252b5132 4873 }
67c0d1eb 4874 macro_build (NULL, s2, "d", dreg);
252b5132
RH
4875 return;
4876
1abe91b1
MR
4877 case M_DLCA_AB:
4878 dbl = 1;
4879 case M_LCA_AB:
4880 call = 1;
4881 goto do_la;
252b5132
RH
4882 case M_DLA_AB:
4883 dbl = 1;
4884 case M_LA_AB:
1abe91b1 4885 do_la:
252b5132
RH
4886 /* Load the address of a symbol into a register. If breg is not
4887 zero, we then add a base register to it. */
4888
3bec30a8
TS
4889 if (dbl && HAVE_32BIT_GPRS)
4890 as_warn (_("dla used to load 32-bit register"));
4891
c90bbe5b 4892 if (! dbl && HAVE_64BIT_OBJECTS)
3bec30a8
TS
4893 as_warn (_("la used to load 64-bit address"));
4894
0c11417f
MR
4895 if (offset_expr.X_op == O_constant
4896 && offset_expr.X_add_number >= -0x8000
4897 && offset_expr.X_add_number < 0x8000)
4898 {
67c0d1eb 4899 macro_build (&offset_expr,
b7c7d6c1 4900 (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
17a2f251 4901 "t,r,j", treg, sreg, BFD_RELOC_LO16);
0c11417f
MR
4902 return;
4903 }
4904
afdbd6d0
CD
4905 if (treg == breg)
4906 {
4907 tempreg = AT;
4908 used_at = 1;
4909 }
4910 else
4911 {
4912 tempreg = treg;
4913 used_at = 0;
4914 }
4915
252b5132
RH
4916 if (offset_expr.X_op != O_symbol
4917 && offset_expr.X_op != O_constant)
4918 {
4919 as_bad (_("expression too complex"));
4920 offset_expr.X_op = O_constant;
4921 }
4922
252b5132 4923 if (offset_expr.X_op == O_constant)
67c0d1eb 4924 load_register (tempreg, &offset_expr,
3e722fb5 4925 (mips_pic == NO_PIC
4d34fb5f
TS
4926 ? (dbl || HAVE_64BIT_ADDRESSES)
4927 : HAVE_64BIT_ADDRESSES));
252b5132
RH
4928 else if (mips_pic == NO_PIC)
4929 {
d6bc6245 4930 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 4931 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
4932 Otherwise we want
4933 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
4934 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4935 If we have a constant, we need two instructions anyhow,
d6bc6245 4936 so we may as well always use the latter form.
76b3015f 4937
d6bc6245
TS
4938 With 64bit address space and a usable $at we want
4939 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4940 lui $at,<sym> (BFD_RELOC_HI16_S)
4941 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4942 daddiu $at,<sym> (BFD_RELOC_LO16)
4943 dsll32 $tempreg,0
3a482fd5 4944 daddu $tempreg,$tempreg,$at
76b3015f 4945
c03099e6 4946 If $at is already in use, we use a path which is suboptimal
d6bc6245
TS
4947 on superscalar processors.
4948 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4949 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4950 dsll $tempreg,16
4951 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
4952 dsll $tempreg,16
4953 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
4954 */
d6bc6245 4955 if (HAVE_64BIT_ADDRESSES)
252b5132 4956 {
e864ceca
RS
4957 /* ??? We don't provide a GP-relative alternative for
4958 these macros. It used not to be possible with the
4959 original relaxation code, but it could be done now. */
d6bc6245 4960
460597ba 4961 if (used_at == 0 && ! mips_opts.noat)
98d3f06f 4962 {
67c0d1eb 4963 macro_build (&offset_expr, "lui", "t,u",
17a2f251 4964 tempreg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb 4965 macro_build (&offset_expr, "lui", "t,u",
17a2f251 4966 AT, BFD_RELOC_HI16_S);
67c0d1eb 4967 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 4968 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
67c0d1eb 4969 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 4970 AT, AT, BFD_RELOC_LO16);
67c0d1eb
RS
4971 macro_build (NULL, "dsll32", "d,w,<", tempreg, tempreg, 0);
4972 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
98d3f06f
KH
4973 used_at = 1;
4974 }
4975 else
4976 {
67c0d1eb 4977 macro_build (&offset_expr, "lui", "t,u",
17a2f251 4978 tempreg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb 4979 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 4980 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
67c0d1eb
RS
4981 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
4982 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 4983 tempreg, tempreg, BFD_RELOC_HI16_S);
67c0d1eb
RS
4984 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
4985 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 4986 tempreg, tempreg, BFD_RELOC_LO16);
98d3f06f
KH
4987 }
4988 }
4989 else
4990 {
4991 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4992 && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
4993 {
4d7206a2 4994 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
4995 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
4996 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 4997 relax_switch ();
98d3f06f 4998 }
67c0d1eb
RS
4999 macro_build_lui (&offset_expr, tempreg);
5000 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5001 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2
RS
5002 if (mips_relax.sequence)
5003 relax_end ();
98d3f06f 5004 }
252b5132 5005 }
f5040a92 5006 else if (mips_pic == SVR4_PIC && ! mips_big_got && ! HAVE_NEWABI)
252b5132 5007 {
9117d219
NC
5008 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5009
252b5132
RH
5010 /* If this is a reference to an external symbol, and there
5011 is no constant, we want
5012 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
1abe91b1 5013 or for lca or if tempreg is PIC_CALL_REG
9117d219 5014 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
252b5132
RH
5015 For a local symbol, we want
5016 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5017 nop
5018 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5019
5020 If we have a small constant, and this is a reference to
5021 an external symbol, we want
5022 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5023 nop
5024 addiu $tempreg,$tempreg,<constant>
5025 For a local symbol, we want the same instruction
5026 sequence, but we output a BFD_RELOC_LO16 reloc on the
5027 addiu instruction.
5028
5029 If we have a large constant, and this is a reference to
5030 an external symbol, we want
5031 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5032 lui $at,<hiconstant>
5033 addiu $at,$at,<loconstant>
5034 addu $tempreg,$tempreg,$at
5035 For a local symbol, we want the same instruction
5036 sequence, but we output a BFD_RELOC_LO16 reloc on the
ed6fb7bd 5037 addiu instruction.
ed6fb7bd
SC
5038 */
5039
4d7206a2 5040 if (offset_expr.X_add_number == 0)
252b5132 5041 {
4d7206a2
RS
5042 if (breg == 0 && (call || tempreg == PIC_CALL_REG))
5043 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
5044
5045 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
5046 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5047 lw_reloc_type, mips_gp_register);
4d7206a2 5048 if (breg != 0)
252b5132
RH
5049 {
5050 /* We're going to put in an addu instruction using
5051 tempreg, so we may as well insert the nop right
5052 now. */
269137b2 5053 load_delay_nop ();
252b5132 5054 }
4d7206a2 5055 relax_switch ();
67c0d1eb
RS
5056 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5057 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 5058 load_delay_nop ();
67c0d1eb
RS
5059 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5060 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2 5061 relax_end ();
252b5132
RH
5062 /* FIXME: If breg == 0, and the next instruction uses
5063 $tempreg, then if this variant case is used an extra
5064 nop will be generated. */
5065 }
4d7206a2
RS
5066 else if (offset_expr.X_add_number >= -0x8000
5067 && offset_expr.X_add_number < 0x8000)
252b5132 5068 {
67c0d1eb 5069 load_got_offset (tempreg, &offset_expr);
269137b2 5070 load_delay_nop ();
67c0d1eb 5071 add_got_offset (tempreg, &offset_expr);
252b5132
RH
5072 }
5073 else
5074 {
4d7206a2
RS
5075 expr1.X_add_number = offset_expr.X_add_number;
5076 offset_expr.X_add_number =
5077 ((offset_expr.X_add_number + 0x8000) & 0xffff) - 0x8000;
67c0d1eb 5078 load_got_offset (tempreg, &offset_expr);
f6a22291 5079 offset_expr.X_add_number = expr1.X_add_number;
252b5132
RH
5080 /* If we are going to add in a base register, and the
5081 target register and the base register are the same,
5082 then we are using AT as a temporary register. Since
5083 we want to load the constant into AT, we add our
5084 current AT (from the global offset table) and the
5085 register into the register now, and pretend we were
5086 not using a base register. */
67c0d1eb 5087 if (breg == treg)
252b5132 5088 {
269137b2 5089 load_delay_nop ();
67c0d1eb 5090 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5091 treg, AT, breg);
252b5132
RH
5092 breg = 0;
5093 tempreg = treg;
252b5132 5094 }
f6a22291 5095 add_got_offset_hilo (tempreg, &offset_expr, AT);
252b5132
RH
5096 used_at = 1;
5097 }
5098 }
f5040a92
AO
5099 else if (mips_pic == SVR4_PIC && ! mips_big_got && HAVE_NEWABI)
5100 {
67c0d1eb 5101 int add_breg_early = 0;
f5040a92
AO
5102
5103 /* If this is a reference to an external, and there is no
5104 constant, or local symbol (*), with or without a
5105 constant, we want
5106 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
1abe91b1 5107 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
5108 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5109
5110 If we have a small constant, and this is a reference to
5111 an external symbol, we want
5112 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5113 addiu $tempreg,$tempreg,<constant>
5114
5115 If we have a large constant, and this is a reference to
5116 an external symbol, we want
5117 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5118 lui $at,<hiconstant>
5119 addiu $at,$at,<loconstant>
5120 addu $tempreg,$tempreg,$at
5121
5122 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
5123 local symbols, even though it introduces an additional
5124 instruction. */
5125
f5040a92
AO
5126 if (offset_expr.X_add_number)
5127 {
4d7206a2 5128 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
5129 offset_expr.X_add_number = 0;
5130
4d7206a2 5131 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
5132 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5133 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
5134
5135 if (expr1.X_add_number >= -0x8000
5136 && expr1.X_add_number < 0x8000)
5137 {
67c0d1eb
RS
5138 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
5139 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 5140 }
ecd13cd3 5141 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92
AO
5142 {
5143 int dreg;
5144
5145 /* If we are going to add in a base register, and the
5146 target register and the base register are the same,
5147 then we are using AT as a temporary register. Since
5148 we want to load the constant into AT, we add our
5149 current AT (from the global offset table) and the
5150 register into the register now, and pretend we were
5151 not using a base register. */
5152 if (breg != treg)
5153 dreg = tempreg;
5154 else
5155 {
5156 assert (tempreg == AT);
67c0d1eb
RS
5157 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5158 treg, AT, breg);
f5040a92 5159 dreg = treg;
67c0d1eb 5160 add_breg_early = 1;
f5040a92
AO
5161 }
5162
f6a22291 5163 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 5164 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5165 dreg, dreg, AT);
f5040a92 5166
f5040a92
AO
5167 used_at = 1;
5168 }
5169 else
5170 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5171
4d7206a2 5172 relax_switch ();
f5040a92
AO
5173 offset_expr.X_add_number = expr1.X_add_number;
5174
67c0d1eb
RS
5175 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5176 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5177 if (add_breg_early)
f5040a92 5178 {
67c0d1eb 5179 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
f899b4b8 5180 treg, tempreg, breg);
f5040a92
AO
5181 breg = 0;
5182 tempreg = treg;
5183 }
4d7206a2 5184 relax_end ();
f5040a92 5185 }
4d7206a2 5186 else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
f5040a92 5187 {
4d7206a2 5188 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
5189 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5190 BFD_RELOC_MIPS_CALL16, mips_gp_register);
4d7206a2 5191 relax_switch ();
67c0d1eb
RS
5192 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5193 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2 5194 relax_end ();
f5040a92 5195 }
4d7206a2 5196 else
f5040a92 5197 {
67c0d1eb
RS
5198 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5199 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
5200 }
5201 }
5202 else if (mips_pic == SVR4_PIC && ! HAVE_NEWABI)
252b5132 5203 {
67c0d1eb 5204 int gpdelay;
9117d219
NC
5205 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5206 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
ed6fb7bd 5207 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
252b5132
RH
5208
5209 /* This is the large GOT case. If this is a reference to an
5210 external symbol, and there is no constant, we want
5211 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5212 addu $tempreg,$tempreg,$gp
5213 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 5214 or for lca or if tempreg is PIC_CALL_REG
9117d219
NC
5215 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5216 addu $tempreg,$tempreg,$gp
5217 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
252b5132
RH
5218 For a local symbol, we want
5219 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5220 nop
5221 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5222
5223 If we have a small constant, and this is a reference to
5224 an external symbol, we want
5225 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5226 addu $tempreg,$tempreg,$gp
5227 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5228 nop
5229 addiu $tempreg,$tempreg,<constant>
5230 For a local symbol, we want
5231 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5232 nop
5233 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
5234
5235 If we have a large constant, and this is a reference to
5236 an external symbol, we want
5237 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5238 addu $tempreg,$tempreg,$gp
5239 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5240 lui $at,<hiconstant>
5241 addiu $at,$at,<loconstant>
5242 addu $tempreg,$tempreg,$at
5243 For a local symbol, we want
5244 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5245 lui $at,<hiconstant>
5246 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
5247 addu $tempreg,$tempreg,$at
f5040a92 5248 */
438c16b8 5249
252b5132
RH
5250 expr1.X_add_number = offset_expr.X_add_number;
5251 offset_expr.X_add_number = 0;
4d7206a2 5252 relax_start (offset_expr.X_add_symbol);
67c0d1eb 5253 gpdelay = reg_needs_delay (mips_gp_register);
1abe91b1
MR
5254 if (expr1.X_add_number == 0 && breg == 0
5255 && (call || tempreg == PIC_CALL_REG))
9117d219
NC
5256 {
5257 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5258 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5259 }
67c0d1eb
RS
5260 macro_build (&offset_expr, "lui", "t,u", tempreg, lui_reloc_type);
5261 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5262 tempreg, tempreg, mips_gp_register);
67c0d1eb 5263 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 5264 tempreg, lw_reloc_type, tempreg);
252b5132
RH
5265 if (expr1.X_add_number == 0)
5266 {
67c0d1eb 5267 if (breg != 0)
252b5132
RH
5268 {
5269 /* We're going to put in an addu instruction using
5270 tempreg, so we may as well insert the nop right
5271 now. */
269137b2 5272 load_delay_nop ();
252b5132 5273 }
252b5132
RH
5274 }
5275 else if (expr1.X_add_number >= -0x8000
5276 && expr1.X_add_number < 0x8000)
5277 {
269137b2 5278 load_delay_nop ();
67c0d1eb 5279 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 5280 tempreg, tempreg, BFD_RELOC_LO16);
252b5132
RH
5281 }
5282 else
5283 {
67c0d1eb 5284 int dreg;
252b5132
RH
5285
5286 /* If we are going to add in a base register, and the
5287 target register and the base register are the same,
5288 then we are using AT as a temporary register. Since
5289 we want to load the constant into AT, we add our
5290 current AT (from the global offset table) and the
5291 register into the register now, and pretend we were
5292 not using a base register. */
5293 if (breg != treg)
67c0d1eb 5294 dreg = tempreg;
252b5132
RH
5295 else
5296 {
5297 assert (tempreg == AT);
269137b2 5298 load_delay_nop ();
67c0d1eb 5299 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5300 treg, AT, breg);
252b5132 5301 dreg = treg;
252b5132
RH
5302 }
5303
f6a22291 5304 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 5305 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
252b5132 5306
252b5132
RH
5307 used_at = 1;
5308 }
4d7206a2
RS
5309 offset_expr.X_add_number =
5310 ((expr1.X_add_number + 0x8000) & 0xffff) - 0x8000;
5311 relax_switch ();
252b5132 5312
67c0d1eb 5313 if (gpdelay)
252b5132
RH
5314 {
5315 /* This is needed because this instruction uses $gp, but
f5040a92 5316 the first instruction on the main stream does not. */
67c0d1eb 5317 macro_build (NULL, "nop", "");
252b5132 5318 }
ed6fb7bd 5319
67c0d1eb
RS
5320 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5321 local_reloc_type, mips_gp_register);
f5040a92 5322 if (expr1.X_add_number >= -0x8000
252b5132
RH
5323 && expr1.X_add_number < 0x8000)
5324 {
269137b2 5325 load_delay_nop ();
67c0d1eb
RS
5326 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5327 tempreg, tempreg, BFD_RELOC_LO16);
252b5132 5328 /* FIXME: If add_number is 0, and there was no base
f5040a92
AO
5329 register, the external symbol case ended with a load,
5330 so if the symbol turns out to not be external, and
5331 the next instruction uses tempreg, an unnecessary nop
5332 will be inserted. */
252b5132
RH
5333 }
5334 else
5335 {
5336 if (breg == treg)
5337 {
5338 /* We must add in the base register now, as in the
f5040a92 5339 external symbol case. */
252b5132 5340 assert (tempreg == AT);
269137b2 5341 load_delay_nop ();
67c0d1eb 5342 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5343 treg, AT, breg);
252b5132
RH
5344 tempreg = treg;
5345 /* We set breg to 0 because we have arranged to add
f5040a92 5346 it in in both cases. */
252b5132
RH
5347 breg = 0;
5348 }
5349
67c0d1eb
RS
5350 macro_build_lui (&expr1, AT);
5351 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 5352 AT, AT, BFD_RELOC_LO16);
67c0d1eb 5353 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5354 tempreg, tempreg, AT);
252b5132 5355 }
4d7206a2 5356 relax_end ();
252b5132 5357 }
f5040a92
AO
5358 else if (mips_pic == SVR4_PIC && HAVE_NEWABI)
5359 {
f5040a92
AO
5360 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5361 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
67c0d1eb 5362 int add_breg_early = 0;
f5040a92
AO
5363
5364 /* This is the large GOT case. If this is a reference to an
5365 external symbol, and there is no constant, we want
5366 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5367 add $tempreg,$tempreg,$gp
5368 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 5369 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
5370 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5371 add $tempreg,$tempreg,$gp
5372 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5373
5374 If we have a small constant, and this is a reference to
5375 an external symbol, we want
5376 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5377 add $tempreg,$tempreg,$gp
5378 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5379 addi $tempreg,$tempreg,<constant>
5380
5381 If we have a large constant, and this is a reference to
5382 an external symbol, we want
5383 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5384 addu $tempreg,$tempreg,$gp
5385 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5386 lui $at,<hiconstant>
5387 addi $at,$at,<loconstant>
5388 add $tempreg,$tempreg,$at
5389
5390 If we have NewABI, and we know it's a local symbol, we want
5391 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
5392 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
5393 otherwise we have to resort to GOT_HI16/GOT_LO16. */
5394
4d7206a2 5395 relax_start (offset_expr.X_add_symbol);
f5040a92 5396
4d7206a2 5397 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
5398 offset_expr.X_add_number = 0;
5399
1abe91b1
MR
5400 if (expr1.X_add_number == 0 && breg == 0
5401 && (call || tempreg == PIC_CALL_REG))
f5040a92
AO
5402 {
5403 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5404 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5405 }
67c0d1eb
RS
5406 macro_build (&offset_expr, "lui", "t,u", tempreg, lui_reloc_type);
5407 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5408 tempreg, tempreg, mips_gp_register);
67c0d1eb
RS
5409 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5410 tempreg, lw_reloc_type, tempreg);
f5040a92
AO
5411
5412 if (expr1.X_add_number == 0)
4d7206a2 5413 ;
f5040a92
AO
5414 else if (expr1.X_add_number >= -0x8000
5415 && expr1.X_add_number < 0x8000)
5416 {
67c0d1eb 5417 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 5418 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 5419 }
ecd13cd3 5420 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92
AO
5421 {
5422 int dreg;
5423
5424 /* If we are going to add in a base register, and the
5425 target register and the base register are the same,
5426 then we are using AT as a temporary register. Since
5427 we want to load the constant into AT, we add our
5428 current AT (from the global offset table) and the
5429 register into the register now, and pretend we were
5430 not using a base register. */
5431 if (breg != treg)
5432 dreg = tempreg;
5433 else
5434 {
5435 assert (tempreg == AT);
67c0d1eb 5436 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5437 treg, AT, breg);
f5040a92 5438 dreg = treg;
67c0d1eb 5439 add_breg_early = 1;
f5040a92
AO
5440 }
5441
f6a22291 5442 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 5443 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
f5040a92 5444
f5040a92
AO
5445 used_at = 1;
5446 }
5447 else
5448 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5449
4d7206a2 5450 relax_switch ();
f5040a92 5451 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
5452 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5453 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
5454 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
5455 tempreg, BFD_RELOC_MIPS_GOT_OFST);
5456 if (add_breg_early)
f5040a92 5457 {
67c0d1eb 5458 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5459 treg, tempreg, breg);
f5040a92
AO
5460 breg = 0;
5461 tempreg = treg;
5462 }
4d7206a2 5463 relax_end ();
f5040a92 5464 }
252b5132
RH
5465 else
5466 abort ();
5467
5468 if (breg != 0)
4d34fb5f
TS
5469 {
5470 char *s;
5471
3e722fb5 5472 if (mips_pic == NO_PIC)
b7c7d6c1 5473 s = (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu";
4d34fb5f 5474 else
f899b4b8 5475 s = ADDRESS_ADD_INSN;
4d34fb5f 5476
67c0d1eb 5477 macro_build (NULL, s, "d,v,t", treg, tempreg, breg);
4d34fb5f 5478 }
252b5132
RH
5479
5480 if (! used_at)
5481 return;
5482
5483 break;
5484
5485 case M_J_A:
5486 /* The j instruction may not be used in PIC code, since it
5487 requires an absolute address. We convert it to a b
5488 instruction. */
5489 if (mips_pic == NO_PIC)
67c0d1eb 5490 macro_build (&offset_expr, "j", "a");
252b5132 5491 else
67c0d1eb 5492 macro_build (&offset_expr, "b", "p");
252b5132
RH
5493 return;
5494
5495 /* The jal instructions must be handled as macros because when
5496 generating PIC code they expand to multi-instruction
5497 sequences. Normally they are simple instructions. */
5498 case M_JAL_1:
5499 dreg = RA;
5500 /* Fall through. */
5501 case M_JAL_2:
3e722fb5 5502 if (mips_pic == NO_PIC)
67c0d1eb 5503 macro_build (NULL, "jalr", "d,s", dreg, sreg);
252b5132
RH
5504 else if (mips_pic == SVR4_PIC)
5505 {
5506 if (sreg != PIC_CALL_REG)
5507 as_warn (_("MIPS PIC call to register other than $25"));
bdaaa2e1 5508
67c0d1eb 5509 macro_build (NULL, "jalr", "d,s", dreg, sreg);
6478892d 5510 if (! HAVE_NEWABI)
252b5132 5511 {
6478892d
TS
5512 if (mips_cprestore_offset < 0)
5513 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5514 else
5515 {
7a621144
DJ
5516 if (! mips_frame_reg_valid)
5517 {
5518 as_warn (_("No .frame pseudo-op used in PIC code"));
5519 /* Quiet this warning. */
5520 mips_frame_reg_valid = 1;
5521 }
5522 if (! mips_cprestore_valid)
5523 {
5524 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5525 /* Quiet this warning. */
5526 mips_cprestore_valid = 1;
5527 }
6478892d 5528 expr1.X_add_number = mips_cprestore_offset;
67c0d1eb 5529 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 5530 mips_gp_register,
256ab948
TS
5531 mips_frame_reg,
5532 HAVE_64BIT_ADDRESSES);
6478892d 5533 }
252b5132
RH
5534 }
5535 }
5536 else
5537 abort ();
5538
5539 return;
5540
5541 case M_JAL_A:
5542 if (mips_pic == NO_PIC)
67c0d1eb 5543 macro_build (&offset_expr, "jal", "a");
252b5132
RH
5544 else if (mips_pic == SVR4_PIC)
5545 {
5546 /* If this is a reference to an external symbol, and we are
5547 using a small GOT, we want
5548 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5549 nop
f9419b05 5550 jalr $ra,$25
252b5132
RH
5551 nop
5552 lw $gp,cprestore($sp)
5553 The cprestore value is set using the .cprestore
5554 pseudo-op. If we are using a big GOT, we want
5555 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5556 addu $25,$25,$gp
5557 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
5558 nop
f9419b05 5559 jalr $ra,$25
252b5132
RH
5560 nop
5561 lw $gp,cprestore($sp)
5562 If the symbol is not external, we want
5563 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5564 nop
5565 addiu $25,$25,<sym> (BFD_RELOC_LO16)
f9419b05 5566 jalr $ra,$25
252b5132 5567 nop
438c16b8 5568 lw $gp,cprestore($sp)
f5040a92
AO
5569
5570 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
5571 sequences above, minus nops, unless the symbol is local,
5572 which enables us to use GOT_PAGE/GOT_OFST (big got) or
5573 GOT_DISP. */
438c16b8 5574 if (HAVE_NEWABI)
252b5132 5575 {
f5040a92
AO
5576 if (! mips_big_got)
5577 {
4d7206a2 5578 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
5579 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5580 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
f5040a92 5581 mips_gp_register);
4d7206a2 5582 relax_switch ();
67c0d1eb
RS
5583 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5584 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
4d7206a2
RS
5585 mips_gp_register);
5586 relax_end ();
f5040a92
AO
5587 }
5588 else
5589 {
4d7206a2 5590 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
5591 macro_build (&offset_expr, "lui", "t,u", PIC_CALL_REG,
5592 BFD_RELOC_MIPS_CALL_HI16);
5593 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
5594 PIC_CALL_REG, mips_gp_register);
5595 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5596 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
5597 PIC_CALL_REG);
4d7206a2 5598 relax_switch ();
67c0d1eb
RS
5599 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5600 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
5601 mips_gp_register);
5602 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5603 PIC_CALL_REG, PIC_CALL_REG,
17a2f251 5604 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 5605 relax_end ();
f5040a92 5606 }
684022ea 5607
67c0d1eb 5608 macro_build_jalr (&offset_expr);
252b5132
RH
5609 }
5610 else
5611 {
4d7206a2 5612 relax_start (offset_expr.X_add_symbol);
438c16b8
TS
5613 if (! mips_big_got)
5614 {
67c0d1eb
RS
5615 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5616 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
17a2f251 5617 mips_gp_register);
269137b2 5618 load_delay_nop ();
4d7206a2 5619 relax_switch ();
438c16b8 5620 }
252b5132 5621 else
252b5132 5622 {
67c0d1eb
RS
5623 int gpdelay;
5624
5625 gpdelay = reg_needs_delay (mips_gp_register);
5626 macro_build (&offset_expr, "lui", "t,u", PIC_CALL_REG,
5627 BFD_RELOC_MIPS_CALL_HI16);
5628 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
5629 PIC_CALL_REG, mips_gp_register);
5630 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5631 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
5632 PIC_CALL_REG);
269137b2 5633 load_delay_nop ();
4d7206a2 5634 relax_switch ();
67c0d1eb
RS
5635 if (gpdelay)
5636 macro_build (NULL, "nop", "");
252b5132 5637 }
67c0d1eb
RS
5638 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5639 PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
4d7206a2 5640 mips_gp_register);
269137b2 5641 load_delay_nop ();
67c0d1eb
RS
5642 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5643 PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
4d7206a2 5644 relax_end ();
67c0d1eb 5645 macro_build_jalr (&offset_expr);
438c16b8 5646
6478892d
TS
5647 if (mips_cprestore_offset < 0)
5648 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5649 else
5650 {
7a621144
DJ
5651 if (! mips_frame_reg_valid)
5652 {
5653 as_warn (_("No .frame pseudo-op used in PIC code"));
5654 /* Quiet this warning. */
5655 mips_frame_reg_valid = 1;
5656 }
5657 if (! mips_cprestore_valid)
5658 {
5659 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5660 /* Quiet this warning. */
5661 mips_cprestore_valid = 1;
5662 }
6478892d 5663 if (mips_opts.noreorder)
67c0d1eb 5664 macro_build (NULL, "nop", "");
6478892d 5665 expr1.X_add_number = mips_cprestore_offset;
67c0d1eb 5666 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 5667 mips_gp_register,
256ab948
TS
5668 mips_frame_reg,
5669 HAVE_64BIT_ADDRESSES);
6478892d 5670 }
252b5132
RH
5671 }
5672 }
252b5132
RH
5673 else
5674 abort ();
5675
5676 return;
5677
5678 case M_LB_AB:
5679 s = "lb";
5680 goto ld;
5681 case M_LBU_AB:
5682 s = "lbu";
5683 goto ld;
5684 case M_LH_AB:
5685 s = "lh";
5686 goto ld;
5687 case M_LHU_AB:
5688 s = "lhu";
5689 goto ld;
5690 case M_LW_AB:
5691 s = "lw";
5692 goto ld;
5693 case M_LWC0_AB:
5694 s = "lwc0";
bdaaa2e1 5695 /* Itbl support may require additional care here. */
252b5132
RH
5696 coproc = 1;
5697 goto ld;
5698 case M_LWC1_AB:
5699 s = "lwc1";
bdaaa2e1 5700 /* Itbl support may require additional care here. */
252b5132
RH
5701 coproc = 1;
5702 goto ld;
5703 case M_LWC2_AB:
5704 s = "lwc2";
bdaaa2e1 5705 /* Itbl support may require additional care here. */
252b5132
RH
5706 coproc = 1;
5707 goto ld;
5708 case M_LWC3_AB:
5709 s = "lwc3";
bdaaa2e1 5710 /* Itbl support may require additional care here. */
252b5132
RH
5711 coproc = 1;
5712 goto ld;
5713 case M_LWL_AB:
5714 s = "lwl";
5715 lr = 1;
5716 goto ld;
5717 case M_LWR_AB:
5718 s = "lwr";
5719 lr = 1;
5720 goto ld;
5721 case M_LDC1_AB:
fef14a42 5722 if (mips_opts.arch == CPU_R4650)
252b5132
RH
5723 {
5724 as_bad (_("opcode not supported on this processor"));
5725 return;
5726 }
5727 s = "ldc1";
bdaaa2e1 5728 /* Itbl support may require additional care here. */
252b5132
RH
5729 coproc = 1;
5730 goto ld;
5731 case M_LDC2_AB:
5732 s = "ldc2";
bdaaa2e1 5733 /* Itbl support may require additional care here. */
252b5132
RH
5734 coproc = 1;
5735 goto ld;
5736 case M_LDC3_AB:
5737 s = "ldc3";
bdaaa2e1 5738 /* Itbl support may require additional care here. */
252b5132
RH
5739 coproc = 1;
5740 goto ld;
5741 case M_LDL_AB:
5742 s = "ldl";
5743 lr = 1;
5744 goto ld;
5745 case M_LDR_AB:
5746 s = "ldr";
5747 lr = 1;
5748 goto ld;
5749 case M_LL_AB:
5750 s = "ll";
5751 goto ld;
5752 case M_LLD_AB:
5753 s = "lld";
5754 goto ld;
5755 case M_LWU_AB:
5756 s = "lwu";
5757 ld:
5758 if (breg == treg || coproc || lr)
5759 {
5760 tempreg = AT;
5761 used_at = 1;
5762 }
5763 else
5764 {
5765 tempreg = treg;
5766 used_at = 0;
5767 }
5768 goto ld_st;
5769 case M_SB_AB:
5770 s = "sb";
5771 goto st;
5772 case M_SH_AB:
5773 s = "sh";
5774 goto st;
5775 case M_SW_AB:
5776 s = "sw";
5777 goto st;
5778 case M_SWC0_AB:
5779 s = "swc0";
bdaaa2e1 5780 /* Itbl support may require additional care here. */
252b5132
RH
5781 coproc = 1;
5782 goto st;
5783 case M_SWC1_AB:
5784 s = "swc1";
bdaaa2e1 5785 /* Itbl support may require additional care here. */
252b5132
RH
5786 coproc = 1;
5787 goto st;
5788 case M_SWC2_AB:
5789 s = "swc2";
bdaaa2e1 5790 /* Itbl support may require additional care here. */
252b5132
RH
5791 coproc = 1;
5792 goto st;
5793 case M_SWC3_AB:
5794 s = "swc3";
bdaaa2e1 5795 /* Itbl support may require additional care here. */
252b5132
RH
5796 coproc = 1;
5797 goto st;
5798 case M_SWL_AB:
5799 s = "swl";
5800 goto st;
5801 case M_SWR_AB:
5802 s = "swr";
5803 goto st;
5804 case M_SC_AB:
5805 s = "sc";
5806 goto st;
5807 case M_SCD_AB:
5808 s = "scd";
5809 goto st;
5810 case M_SDC1_AB:
fef14a42 5811 if (mips_opts.arch == CPU_R4650)
252b5132
RH
5812 {
5813 as_bad (_("opcode not supported on this processor"));
5814 return;
5815 }
5816 s = "sdc1";
5817 coproc = 1;
bdaaa2e1 5818 /* Itbl support may require additional care here. */
252b5132
RH
5819 goto st;
5820 case M_SDC2_AB:
5821 s = "sdc2";
bdaaa2e1 5822 /* Itbl support may require additional care here. */
252b5132
RH
5823 coproc = 1;
5824 goto st;
5825 case M_SDC3_AB:
5826 s = "sdc3";
bdaaa2e1 5827 /* Itbl support may require additional care here. */
252b5132
RH
5828 coproc = 1;
5829 goto st;
5830 case M_SDL_AB:
5831 s = "sdl";
5832 goto st;
5833 case M_SDR_AB:
5834 s = "sdr";
5835 st:
5836 tempreg = AT;
5837 used_at = 1;
5838 ld_st:
bdaaa2e1 5839 /* Itbl support may require additional care here. */
252b5132
RH
5840 if (mask == M_LWC1_AB
5841 || mask == M_SWC1_AB
5842 || mask == M_LDC1_AB
5843 || mask == M_SDC1_AB
5844 || mask == M_L_DAB
5845 || mask == M_S_DAB)
5846 fmt = "T,o(b)";
5847 else if (coproc)
5848 fmt = "E,o(b)";
5849 else
5850 fmt = "t,o(b)";
5851
256ab948
TS
5852 /* Sign-extending 32-bit constants makes their handling easier.
5853 The HAVE_64BIT_GPRS... part is due to the linux kernel hack
5854 described below. */
5855 if ((! HAVE_64BIT_ADDRESSES
5856 && (! HAVE_64BIT_GPRS && offset_expr.X_op == O_constant))
d17e7bce
TS
5857 && (offset_expr.X_op == O_constant)
5858 && ! ((offset_expr.X_add_number & ~((bfd_vma) 0x7fffffff))
5859 == ~((bfd_vma) 0x7fffffff)))
ae826530 5860 {
d17e7bce 5861 if (offset_expr.X_add_number & ~((bfd_vma) 0xffffffff))
1b8e29e5 5862 as_bad (_("constant too large"));
ae826530 5863
1b8e29e5
TS
5864 offset_expr.X_add_number = (((offset_expr.X_add_number & 0xffffffff)
5865 ^ 0x80000000) - 0x80000000);
ae826530 5866 }
256ab948 5867
252b5132
RH
5868 if (offset_expr.X_op != O_constant
5869 && offset_expr.X_op != O_symbol)
5870 {
5871 as_bad (_("expression too complex"));
5872 offset_expr.X_op = O_constant;
5873 }
5874
5875 /* A constant expression in PIC code can be handled just as it
5876 is in non PIC code. */
5877 if (mips_pic == NO_PIC
5878 || offset_expr.X_op == O_constant)
5879 {
5880 /* If this is a reference to a GP relative symbol, and there
5881 is no base register, we want
cdf6fd85 5882 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
5883 Otherwise, if there is no base register, we want
5884 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5885 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5886 If we have a constant, we need two instructions anyhow,
5887 so we always use the latter form.
5888
5889 If we have a base register, and this is a reference to a
5890 GP relative symbol, we want
5891 addu $tempreg,$breg,$gp
cdf6fd85 5892 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
5893 Otherwise we want
5894 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5895 addu $tempreg,$tempreg,$breg
5896 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245 5897 With a constant we always use the latter case.
76b3015f 5898
d6bc6245
TS
5899 With 64bit address space and no base register and $at usable,
5900 we want
5901 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5902 lui $at,<sym> (BFD_RELOC_HI16_S)
5903 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5904 dsll32 $tempreg,0
5905 daddu $tempreg,$at
5906 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5907 If we have a base register, we want
5908 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5909 lui $at,<sym> (BFD_RELOC_HI16_S)
5910 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5911 daddu $at,$breg
5912 dsll32 $tempreg,0
5913 daddu $tempreg,$at
5914 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5915
5916 Without $at we can't generate the optimal path for superscalar
5917 processors here since this would require two temporary registers.
5918 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5919 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5920 dsll $tempreg,16
5921 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5922 dsll $tempreg,16
5923 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5924 If we have a base register, we want
5925 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5926 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5927 dsll $tempreg,16
5928 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5929 dsll $tempreg,16
5930 daddu $tempreg,$tempreg,$breg
5931 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6373ee54
CD
5932
5933 If we have 64-bit addresses, as an optimization, for
5934 addresses which are 32-bit constants (e.g. kseg0/kseg1
5935 addresses) we fall back to the 32-bit address generation
78d32a17
MR
5936 mechanism since it is more efficient. Note that due to
5937 the signed offset used by memory operations, the 32-bit
5938 range is shifted down by 32768 here. This code should
6373ee54
CD
5939 probably attempt to generate 64-bit constants more
5940 efficiently in general.
9214dd3b
AO
5941
5942 As an extension for architectures with 64-bit registers,
5943 we don't truncate 64-bit addresses given as literal
5944 constants down to 32 bits, to support existing practice
5945 in the mips64 Linux (the kernel), that compiles source
5946 files with -mabi=64, assembling them as o32 or n32 (with
5947 -Wa,-32 or -Wa,-n32). This is not beautiful, but since
5948 the whole kernel is loaded into a memory region that is
67c1ffbe 5949 addressable with sign-extended 32-bit addresses, it is
9214dd3b
AO
5950 wasteful to compute the upper 32 bits of every
5951 non-literal address, that takes more space and time.
5952 Some day this should probably be implemented as an
5953 assembler option, such that the kernel doesn't have to
5954 use such ugly hacks, even though it will still have to
5955 end up converting the binary to ELF32 for a number of
5956 platforms whose boot loaders don't support ELF64
5957 binaries. */
256ab948
TS
5958 if ((HAVE_64BIT_ADDRESSES
5959 && ! (offset_expr.X_op == O_constant
5960 && IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000)))
5961 || (HAVE_64BIT_GPRS
5962 && offset_expr.X_op == O_constant
5963 && ! IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000)))
d6bc6245 5964 {
e864ceca
RS
5965 /* ??? We don't provide a GP-relative alternative for
5966 these macros. It used not to be possible with the
5967 original relaxation code, but it could be done now. */
d6bc6245 5968
460597ba 5969 if (used_at == 0 && ! mips_opts.noat)
d6bc6245 5970 {
67c0d1eb
RS
5971 macro_build (&offset_expr, "lui", "t,u", tempreg,
5972 BFD_RELOC_MIPS_HIGHEST);
5973 macro_build (&offset_expr, "lui", "t,u", AT,
5974 BFD_RELOC_HI16_S);
5975 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
5976 tempreg, BFD_RELOC_MIPS_HIGHER);
d6bc6245 5977 if (breg != 0)
67c0d1eb
RS
5978 macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
5979 macro_build (NULL, "dsll32", "d,w,<", tempreg, tempreg, 0);
5980 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
5981 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16,
5982 tempreg);
d6bc6245
TS
5983 used_at = 1;
5984 }
5985 else
5986 {
67c0d1eb
RS
5987 macro_build (&offset_expr, "lui", "t,u", tempreg,
5988 BFD_RELOC_MIPS_HIGHEST);
5989 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
5990 tempreg, BFD_RELOC_MIPS_HIGHER);
5991 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
5992 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
5993 tempreg, BFD_RELOC_HI16_S);
5994 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
d6bc6245 5995 if (breg != 0)
67c0d1eb 5996 macro_build (NULL, "daddu", "d,v,t",
17a2f251 5997 tempreg, tempreg, breg);
67c0d1eb 5998 macro_build (&offset_expr, s, fmt, treg,
17a2f251 5999 BFD_RELOC_LO16, tempreg);
d6bc6245
TS
6000 }
6001
6002 return;
6003 }
256ab948
TS
6004
6005 if (offset_expr.X_op == O_constant
ecd13cd3 6006 && ! IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000))
5a7a0b7b 6007 as_bad (_("load/store address overflow (max 32 bits)"));
76b3015f 6008
252b5132
RH
6009 if (breg == 0)
6010 {
67c0d1eb
RS
6011 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6012 && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 6013 {
4d7206a2 6014 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
6015 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_GPREL16,
6016 mips_gp_register);
4d7206a2 6017 relax_switch ();
252b5132
RH
6018 used_at = 0;
6019 }
67c0d1eb
RS
6020 macro_build_lui (&offset_expr, tempreg);
6021 macro_build (&offset_expr, s, fmt, treg,
17a2f251 6022 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
6023 if (mips_relax.sequence)
6024 relax_end ();
252b5132
RH
6025 }
6026 else
6027 {
67c0d1eb
RS
6028 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6029 && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 6030 {
4d7206a2 6031 relax_start (offset_expr.X_add_symbol);
67c0d1eb 6032 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6033 tempreg, breg, mips_gp_register);
67c0d1eb 6034 macro_build (&offset_expr, s, fmt, treg,
17a2f251 6035 BFD_RELOC_GPREL16, tempreg);
4d7206a2 6036 relax_switch ();
252b5132 6037 }
67c0d1eb
RS
6038 macro_build_lui (&offset_expr, tempreg);
6039 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6040 tempreg, tempreg, breg);
67c0d1eb 6041 macro_build (&offset_expr, s, fmt, treg,
17a2f251 6042 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
6043 if (mips_relax.sequence)
6044 relax_end ();
252b5132
RH
6045 }
6046 }
6047 else if (mips_pic == SVR4_PIC && ! mips_big_got)
6048 {
ed6fb7bd 6049 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
f9419b05 6050
252b5132
RH
6051 /* If this is a reference to an external symbol, we want
6052 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6053 nop
6054 <op> $treg,0($tempreg)
6055 Otherwise we want
6056 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6057 nop
6058 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6059 <op> $treg,0($tempreg)
f5040a92
AO
6060
6061 For NewABI, we want
6062 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6063 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
6064
252b5132
RH
6065 If there is a base register, we add it to $tempreg before
6066 the <op>. If there is a constant, we stick it in the
6067 <op> instruction. We don't handle constants larger than
6068 16 bits, because we have no way to load the upper 16 bits
6069 (actually, we could handle them for the subset of cases
6070 in which we are not using $at). */
6071 assert (offset_expr.X_op == O_symbol);
f5040a92
AO
6072 if (HAVE_NEWABI)
6073 {
67c0d1eb
RS
6074 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6075 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 6076 if (breg != 0)
67c0d1eb 6077 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6078 tempreg, tempreg, breg);
67c0d1eb 6079 macro_build (&offset_expr, s, fmt, treg,
17a2f251 6080 BFD_RELOC_MIPS_GOT_OFST, tempreg);
f5040a92
AO
6081
6082 if (! used_at)
6083 return;
6084
6085 break;
6086 }
252b5132
RH
6087 expr1.X_add_number = offset_expr.X_add_number;
6088 offset_expr.X_add_number = 0;
6089 if (expr1.X_add_number < -0x8000
6090 || expr1.X_add_number >= 0x8000)
6091 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb
RS
6092 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6093 lw_reloc_type, mips_gp_register);
269137b2 6094 load_delay_nop ();
4d7206a2
RS
6095 relax_start (offset_expr.X_add_symbol);
6096 relax_switch ();
67c0d1eb
RS
6097 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
6098 tempreg, BFD_RELOC_LO16);
4d7206a2 6099 relax_end ();
252b5132 6100 if (breg != 0)
67c0d1eb 6101 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6102 tempreg, tempreg, breg);
67c0d1eb 6103 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
252b5132 6104 }
f5040a92 6105 else if (mips_pic == SVR4_PIC && ! HAVE_NEWABI)
252b5132 6106 {
67c0d1eb 6107 int gpdelay;
252b5132
RH
6108
6109 /* If this is a reference to an external symbol, we want
6110 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6111 addu $tempreg,$tempreg,$gp
6112 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6113 <op> $treg,0($tempreg)
6114 Otherwise we want
6115 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6116 nop
6117 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6118 <op> $treg,0($tempreg)
6119 If there is a base register, we add it to $tempreg before
6120 the <op>. If there is a constant, we stick it in the
6121 <op> instruction. We don't handle constants larger than
6122 16 bits, because we have no way to load the upper 16 bits
6123 (actually, we could handle them for the subset of cases
f5040a92 6124 in which we are not using $at). */
252b5132
RH
6125 assert (offset_expr.X_op == O_symbol);
6126 expr1.X_add_number = offset_expr.X_add_number;
6127 offset_expr.X_add_number = 0;
6128 if (expr1.X_add_number < -0x8000
6129 || expr1.X_add_number >= 0x8000)
6130 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 6131 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 6132 relax_start (offset_expr.X_add_symbol);
67c0d1eb 6133 macro_build (&offset_expr, "lui", "t,u", tempreg,
17a2f251 6134 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
6135 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
6136 mips_gp_register);
6137 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6138 BFD_RELOC_MIPS_GOT_LO16, tempreg);
4d7206a2 6139 relax_switch ();
67c0d1eb
RS
6140 if (gpdelay)
6141 macro_build (NULL, "nop", "");
6142 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6143 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 6144 load_delay_nop ();
67c0d1eb
RS
6145 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
6146 tempreg, BFD_RELOC_LO16);
4d7206a2
RS
6147 relax_end ();
6148
252b5132 6149 if (breg != 0)
67c0d1eb 6150 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6151 tempreg, tempreg, breg);
67c0d1eb 6152 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
252b5132 6153 }
f5040a92
AO
6154 else if (mips_pic == SVR4_PIC && HAVE_NEWABI)
6155 {
f5040a92
AO
6156 /* If this is a reference to an external symbol, we want
6157 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6158 add $tempreg,$tempreg,$gp
6159 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6160 <op> $treg,<ofst>($tempreg)
6161 Otherwise, for local symbols, we want:
6162 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6163 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
6164 assert (offset_expr.X_op == O_symbol);
4d7206a2 6165 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
6166 offset_expr.X_add_number = 0;
6167 if (expr1.X_add_number < -0x8000
6168 || expr1.X_add_number >= 0x8000)
6169 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4d7206a2 6170 relax_start (offset_expr.X_add_symbol);
67c0d1eb 6171 macro_build (&offset_expr, "lui", "t,u", tempreg,
17a2f251 6172 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
6173 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
6174 mips_gp_register);
6175 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6176 BFD_RELOC_MIPS_GOT_LO16, tempreg);
f5040a92 6177 if (breg != 0)
67c0d1eb 6178 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6179 tempreg, tempreg, breg);
67c0d1eb 6180 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
684022ea 6181
4d7206a2 6182 relax_switch ();
f5040a92 6183 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
6184 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6185 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 6186 if (breg != 0)
67c0d1eb 6187 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6188 tempreg, tempreg, breg);
67c0d1eb 6189 macro_build (&offset_expr, s, fmt, treg,
17a2f251 6190 BFD_RELOC_MIPS_GOT_OFST, tempreg);
4d7206a2 6191 relax_end ();
f5040a92 6192 }
252b5132
RH
6193 else
6194 abort ();
6195
6196 if (! used_at)
6197 return;
6198
6199 break;
6200
6201 case M_LI:
6202 case M_LI_S:
67c0d1eb 6203 load_register (treg, &imm_expr, 0);
252b5132
RH
6204 return;
6205
6206 case M_DLI:
67c0d1eb 6207 load_register (treg, &imm_expr, 1);
252b5132
RH
6208 return;
6209
6210 case M_LI_SS:
6211 if (imm_expr.X_op == O_constant)
6212 {
67c0d1eb
RS
6213 load_register (AT, &imm_expr, 0);
6214 macro_build (NULL, "mtc1", "t,G", AT, treg);
252b5132
RH
6215 break;
6216 }
6217 else
6218 {
6219 assert (offset_expr.X_op == O_symbol
6220 && strcmp (segment_name (S_GET_SEGMENT
6221 (offset_expr.X_add_symbol)),
6222 ".lit4") == 0
6223 && offset_expr.X_add_number == 0);
67c0d1eb 6224 macro_build (&offset_expr, "lwc1", "T,o(b)", treg,
17a2f251 6225 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
252b5132
RH
6226 return;
6227 }
6228
6229 case M_LI_D:
ca4e0257
RS
6230 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
6231 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
6232 order 32 bits of the value and the low order 32 bits are either
6233 zero or in OFFSET_EXPR. */
252b5132
RH
6234 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6235 {
ca4e0257 6236 if (HAVE_64BIT_GPRS)
67c0d1eb 6237 load_register (treg, &imm_expr, 1);
252b5132
RH
6238 else
6239 {
6240 int hreg, lreg;
6241
6242 if (target_big_endian)
6243 {
6244 hreg = treg;
6245 lreg = treg + 1;
6246 }
6247 else
6248 {
6249 hreg = treg + 1;
6250 lreg = treg;
6251 }
6252
6253 if (hreg <= 31)
67c0d1eb 6254 load_register (hreg, &imm_expr, 0);
252b5132
RH
6255 if (lreg <= 31)
6256 {
6257 if (offset_expr.X_op == O_absent)
67c0d1eb 6258 move_register (lreg, 0);
252b5132
RH
6259 else
6260 {
6261 assert (offset_expr.X_op == O_constant);
67c0d1eb 6262 load_register (lreg, &offset_expr, 0);
252b5132
RH
6263 }
6264 }
6265 }
6266 return;
6267 }
6268
6269 /* We know that sym is in the .rdata section. First we get the
6270 upper 16 bits of the address. */
6271 if (mips_pic == NO_PIC)
6272 {
67c0d1eb 6273 macro_build_lui (&offset_expr, AT);
252b5132
RH
6274 }
6275 else if (mips_pic == SVR4_PIC)
6276 {
67c0d1eb
RS
6277 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6278 BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132 6279 }
252b5132
RH
6280 else
6281 abort ();
bdaaa2e1 6282
252b5132 6283 /* Now we load the register(s). */
ca4e0257 6284 if (HAVE_64BIT_GPRS)
67c0d1eb 6285 macro_build (&offset_expr, "ld", "t,o(b)", treg, BFD_RELOC_LO16, AT);
252b5132
RH
6286 else
6287 {
67c0d1eb 6288 macro_build (&offset_expr, "lw", "t,o(b)", treg, BFD_RELOC_LO16, AT);
f9419b05 6289 if (treg != RA)
252b5132
RH
6290 {
6291 /* FIXME: How in the world do we deal with the possible
6292 overflow here? */
6293 offset_expr.X_add_number += 4;
67c0d1eb 6294 macro_build (&offset_expr, "lw", "t,o(b)",
17a2f251 6295 treg + 1, BFD_RELOC_LO16, AT);
252b5132
RH
6296 }
6297 }
252b5132
RH
6298 break;
6299
6300 case M_LI_DD:
ca4e0257
RS
6301 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
6302 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
6303 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
6304 the value and the low order 32 bits are either zero or in
6305 OFFSET_EXPR. */
252b5132
RH
6306 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6307 {
67c0d1eb 6308 load_register (AT, &imm_expr, HAVE_64BIT_FPRS);
ca4e0257
RS
6309 if (HAVE_64BIT_FPRS)
6310 {
6311 assert (HAVE_64BIT_GPRS);
67c0d1eb 6312 macro_build (NULL, "dmtc1", "t,S", AT, treg);
ca4e0257 6313 }
252b5132
RH
6314 else
6315 {
67c0d1eb 6316 macro_build (NULL, "mtc1", "t,G", AT, treg + 1);
252b5132 6317 if (offset_expr.X_op == O_absent)
67c0d1eb 6318 macro_build (NULL, "mtc1", "t,G", 0, treg);
252b5132
RH
6319 else
6320 {
6321 assert (offset_expr.X_op == O_constant);
67c0d1eb
RS
6322 load_register (AT, &offset_expr, 0);
6323 macro_build (NULL, "mtc1", "t,G", AT, treg);
252b5132
RH
6324 }
6325 }
6326 break;
6327 }
6328
6329 assert (offset_expr.X_op == O_symbol
6330 && offset_expr.X_add_number == 0);
6331 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
6332 if (strcmp (s, ".lit8") == 0)
6333 {
e7af610e 6334 if (mips_opts.isa != ISA_MIPS1)
252b5132 6335 {
67c0d1eb 6336 macro_build (&offset_expr, "ldc1", "T,o(b)", treg,
17a2f251 6337 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
252b5132
RH
6338 return;
6339 }
c9914766 6340 breg = mips_gp_register;
252b5132
RH
6341 r = BFD_RELOC_MIPS_LITERAL;
6342 goto dob;
6343 }
6344 else
6345 {
6346 assert (strcmp (s, RDATA_SECTION_NAME) == 0);
6347 if (mips_pic == SVR4_PIC)
67c0d1eb
RS
6348 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6349 BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132
RH
6350 else
6351 {
6352 /* FIXME: This won't work for a 64 bit address. */
67c0d1eb 6353 macro_build_lui (&offset_expr, AT);
252b5132 6354 }
bdaaa2e1 6355
e7af610e 6356 if (mips_opts.isa != ISA_MIPS1)
252b5132 6357 {
67c0d1eb
RS
6358 macro_build (&offset_expr, "ldc1", "T,o(b)",
6359 treg, BFD_RELOC_LO16, AT);
252b5132
RH
6360 break;
6361 }
6362 breg = AT;
6363 r = BFD_RELOC_LO16;
6364 goto dob;
6365 }
6366
6367 case M_L_DOB:
fef14a42 6368 if (mips_opts.arch == CPU_R4650)
252b5132
RH
6369 {
6370 as_bad (_("opcode not supported on this processor"));
6371 return;
6372 }
6373 /* Even on a big endian machine $fn comes before $fn+1. We have
6374 to adjust when loading from memory. */
6375 r = BFD_RELOC_LO16;
6376 dob:
e7af610e 6377 assert (mips_opts.isa == ISA_MIPS1);
67c0d1eb 6378 macro_build (&offset_expr, "lwc1", "T,o(b)",
17a2f251 6379 target_big_endian ? treg + 1 : treg, r, breg);
252b5132
RH
6380 /* FIXME: A possible overflow which I don't know how to deal
6381 with. */
6382 offset_expr.X_add_number += 4;
67c0d1eb 6383 macro_build (&offset_expr, "lwc1", "T,o(b)",
17a2f251 6384 target_big_endian ? treg : treg + 1, r, breg);
252b5132 6385
252b5132
RH
6386 if (breg != AT)
6387 return;
6388 break;
6389
6390 case M_L_DAB:
6391 /*
6392 * The MIPS assembler seems to check for X_add_number not
6393 * being double aligned and generating:
6394 * lui at,%hi(foo+1)
6395 * addu at,at,v1
6396 * addiu at,at,%lo(foo+1)
6397 * lwc1 f2,0(at)
6398 * lwc1 f3,4(at)
6399 * But, the resulting address is the same after relocation so why
6400 * generate the extra instruction?
6401 */
fef14a42 6402 if (mips_opts.arch == CPU_R4650)
252b5132
RH
6403 {
6404 as_bad (_("opcode not supported on this processor"));
6405 return;
6406 }
bdaaa2e1 6407 /* Itbl support may require additional care here. */
252b5132 6408 coproc = 1;
e7af610e 6409 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
6410 {
6411 s = "ldc1";
6412 goto ld;
6413 }
6414
6415 s = "lwc1";
6416 fmt = "T,o(b)";
6417 goto ldd_std;
6418
6419 case M_S_DAB:
fef14a42 6420 if (mips_opts.arch == CPU_R4650)
252b5132
RH
6421 {
6422 as_bad (_("opcode not supported on this processor"));
6423 return;
6424 }
6425
e7af610e 6426 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
6427 {
6428 s = "sdc1";
6429 goto st;
6430 }
6431
6432 s = "swc1";
6433 fmt = "T,o(b)";
bdaaa2e1 6434 /* Itbl support may require additional care here. */
252b5132
RH
6435 coproc = 1;
6436 goto ldd_std;
6437
6438 case M_LD_AB:
ca4e0257 6439 if (HAVE_64BIT_GPRS)
252b5132
RH
6440 {
6441 s = "ld";
6442 goto ld;
6443 }
6444
6445 s = "lw";
6446 fmt = "t,o(b)";
6447 goto ldd_std;
6448
6449 case M_SD_AB:
ca4e0257 6450 if (HAVE_64BIT_GPRS)
252b5132
RH
6451 {
6452 s = "sd";
6453 goto st;
6454 }
6455
6456 s = "sw";
6457 fmt = "t,o(b)";
6458
6459 ldd_std:
6460 if (offset_expr.X_op != O_symbol
6461 && offset_expr.X_op != O_constant)
6462 {
6463 as_bad (_("expression too complex"));
6464 offset_expr.X_op = O_constant;
6465 }
6466
6467 /* Even on a big endian machine $fn comes before $fn+1. We have
6468 to adjust when loading from memory. We set coproc if we must
6469 load $fn+1 first. */
bdaaa2e1 6470 /* Itbl support may require additional care here. */
252b5132
RH
6471 if (! target_big_endian)
6472 coproc = 0;
6473
6474 if (mips_pic == NO_PIC
6475 || offset_expr.X_op == O_constant)
6476 {
6477 /* If this is a reference to a GP relative symbol, we want
cdf6fd85
TS
6478 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6479 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
6480 If we have a base register, we use this
6481 addu $at,$breg,$gp
cdf6fd85
TS
6482 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6483 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
6484 If this is not a GP relative symbol, we want
6485 lui $at,<sym> (BFD_RELOC_HI16_S)
6486 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6487 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6488 If there is a base register, we add it to $at after the
6489 lui instruction. If there is a constant, we always use
6490 the last case. */
e7d556df 6491 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
252b5132 6492 || nopic_need_relax (offset_expr.X_add_symbol, 1))
67c0d1eb 6493 used_at = 1;
252b5132
RH
6494 else
6495 {
4d7206a2 6496 relax_start (offset_expr.X_add_symbol);
252b5132
RH
6497 if (breg == 0)
6498 {
c9914766 6499 tempreg = mips_gp_register;
252b5132
RH
6500 used_at = 0;
6501 }
6502 else
6503 {
67c0d1eb 6504 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6505 AT, breg, mips_gp_register);
252b5132 6506 tempreg = AT;
252b5132
RH
6507 used_at = 1;
6508 }
6509
beae10d5 6510 /* Itbl support may require additional care here. */
67c0d1eb 6511 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
17a2f251 6512 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6513 offset_expr.X_add_number += 4;
6514
6515 /* Set mips_optimize to 2 to avoid inserting an
6516 undesired nop. */
6517 hold_mips_optimize = mips_optimize;
6518 mips_optimize = 2;
beae10d5 6519 /* Itbl support may require additional care here. */
67c0d1eb 6520 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
17a2f251 6521 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6522 mips_optimize = hold_mips_optimize;
6523
4d7206a2 6524 relax_switch ();
252b5132
RH
6525
6526 /* We just generated two relocs. When tc_gen_reloc
6527 handles this case, it will skip the first reloc and
6528 handle the second. The second reloc already has an
6529 extra addend of 4, which we added above. We must
6530 subtract it out, and then subtract another 4 to make
6531 the first reloc come out right. The second reloc
6532 will come out right because we are going to add 4 to
6533 offset_expr when we build its instruction below.
6534
6535 If we have a symbol, then we don't want to include
6536 the offset, because it will wind up being included
6537 when we generate the reloc. */
6538
6539 if (offset_expr.X_op == O_constant)
6540 offset_expr.X_add_number -= 8;
6541 else
6542 {
6543 offset_expr.X_add_number = -4;
6544 offset_expr.X_op = O_constant;
6545 }
6546 }
67c0d1eb 6547 macro_build_lui (&offset_expr, AT);
252b5132 6548 if (breg != 0)
67c0d1eb 6549 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 6550 /* Itbl support may require additional care here. */
67c0d1eb 6551 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
17a2f251 6552 BFD_RELOC_LO16, AT);
252b5132
RH
6553 /* FIXME: How do we handle overflow here? */
6554 offset_expr.X_add_number += 4;
beae10d5 6555 /* Itbl support may require additional care here. */
67c0d1eb 6556 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
17a2f251 6557 BFD_RELOC_LO16, AT);
4d7206a2
RS
6558 if (mips_relax.sequence)
6559 relax_end ();
bdaaa2e1 6560 }
252b5132
RH
6561 else if (mips_pic == SVR4_PIC && ! mips_big_got)
6562 {
252b5132
RH
6563 /* If this is a reference to an external symbol, we want
6564 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6565 nop
6566 <op> $treg,0($at)
6567 <op> $treg+1,4($at)
6568 Otherwise we want
6569 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6570 nop
6571 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6572 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6573 If there is a base register we add it to $at before the
6574 lwc1 instructions. If there is a constant we include it
6575 in the lwc1 instructions. */
6576 used_at = 1;
6577 expr1.X_add_number = offset_expr.X_add_number;
252b5132
RH
6578 if (expr1.X_add_number < -0x8000
6579 || expr1.X_add_number >= 0x8000 - 4)
6580 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 6581 load_got_offset (AT, &offset_expr);
269137b2 6582 load_delay_nop ();
252b5132 6583 if (breg != 0)
67c0d1eb 6584 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
252b5132
RH
6585
6586 /* Set mips_optimize to 2 to avoid inserting an undesired
6587 nop. */
6588 hold_mips_optimize = mips_optimize;
6589 mips_optimize = 2;
4d7206a2 6590
beae10d5 6591 /* Itbl support may require additional care here. */
4d7206a2 6592 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
6593 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
6594 BFD_RELOC_LO16, AT);
4d7206a2 6595 expr1.X_add_number += 4;
67c0d1eb
RS
6596 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
6597 BFD_RELOC_LO16, AT);
4d7206a2 6598 relax_switch ();
67c0d1eb
RS
6599 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6600 BFD_RELOC_LO16, AT);
4d7206a2 6601 offset_expr.X_add_number += 4;
67c0d1eb
RS
6602 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6603 BFD_RELOC_LO16, AT);
4d7206a2 6604 relax_end ();
252b5132 6605
4d7206a2 6606 mips_optimize = hold_mips_optimize;
252b5132
RH
6607 }
6608 else if (mips_pic == SVR4_PIC)
6609 {
67c0d1eb 6610 int gpdelay;
252b5132
RH
6611
6612 /* If this is a reference to an external symbol, we want
6613 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6614 addu $at,$at,$gp
6615 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
6616 nop
6617 <op> $treg,0($at)
6618 <op> $treg+1,4($at)
6619 Otherwise we want
6620 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6621 nop
6622 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6623 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6624 If there is a base register we add it to $at before the
6625 lwc1 instructions. If there is a constant we include it
6626 in the lwc1 instructions. */
6627 used_at = 1;
6628 expr1.X_add_number = offset_expr.X_add_number;
6629 offset_expr.X_add_number = 0;
6630 if (expr1.X_add_number < -0x8000
6631 || expr1.X_add_number >= 0x8000 - 4)
6632 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 6633 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 6634 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
6635 macro_build (&offset_expr, "lui", "t,u",
6636 AT, BFD_RELOC_MIPS_GOT_HI16);
6637 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6638 AT, AT, mips_gp_register);
67c0d1eb 6639 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 6640 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
269137b2 6641 load_delay_nop ();
252b5132 6642 if (breg != 0)
67c0d1eb 6643 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 6644 /* Itbl support may require additional care here. */
67c0d1eb 6645 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
17a2f251 6646 BFD_RELOC_LO16, AT);
252b5132
RH
6647 expr1.X_add_number += 4;
6648
6649 /* Set mips_optimize to 2 to avoid inserting an undesired
6650 nop. */
6651 hold_mips_optimize = mips_optimize;
6652 mips_optimize = 2;
beae10d5 6653 /* Itbl support may require additional care here. */
67c0d1eb 6654 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
17a2f251 6655 BFD_RELOC_LO16, AT);
252b5132
RH
6656 mips_optimize = hold_mips_optimize;
6657 expr1.X_add_number -= 4;
6658
4d7206a2
RS
6659 relax_switch ();
6660 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
6661 if (gpdelay)
6662 macro_build (NULL, "nop", "");
6663 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6664 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 6665 load_delay_nop ();
252b5132 6666 if (breg != 0)
67c0d1eb 6667 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 6668 /* Itbl support may require additional care here. */
67c0d1eb
RS
6669 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6670 BFD_RELOC_LO16, AT);
4d7206a2 6671 offset_expr.X_add_number += 4;
252b5132
RH
6672
6673 /* Set mips_optimize to 2 to avoid inserting an undesired
6674 nop. */
6675 hold_mips_optimize = mips_optimize;
6676 mips_optimize = 2;
beae10d5 6677 /* Itbl support may require additional care here. */
67c0d1eb
RS
6678 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6679 BFD_RELOC_LO16, AT);
252b5132 6680 mips_optimize = hold_mips_optimize;
4d7206a2 6681 relax_end ();
252b5132 6682 }
252b5132
RH
6683 else
6684 abort ();
6685
6686 if (! used_at)
6687 return;
6688
6689 break;
6690
6691 case M_LD_OB:
6692 s = "lw";
6693 goto sd_ob;
6694 case M_SD_OB:
6695 s = "sw";
6696 sd_ob:
ca4e0257 6697 assert (HAVE_32BIT_ADDRESSES);
67c0d1eb 6698 macro_build (&offset_expr, s, "t,o(b)", treg, BFD_RELOC_LO16, breg);
252b5132 6699 offset_expr.X_add_number += 4;
67c0d1eb 6700 macro_build (&offset_expr, s, "t,o(b)", treg + 1, BFD_RELOC_LO16, breg);
252b5132
RH
6701 return;
6702
6703 /* New code added to support COPZ instructions.
6704 This code builds table entries out of the macros in mip_opcodes.
6705 R4000 uses interlocks to handle coproc delays.
6706 Other chips (like the R3000) require nops to be inserted for delays.
6707
f72c8c98 6708 FIXME: Currently, we require that the user handle delays.
252b5132
RH
6709 In order to fill delay slots for non-interlocked chips,
6710 we must have a way to specify delays based on the coprocessor.
6711 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
6712 What are the side-effects of the cop instruction?
6713 What cache support might we have and what are its effects?
6714 Both coprocessor & memory require delays. how long???
bdaaa2e1 6715 What registers are read/set/modified?
252b5132
RH
6716
6717 If an itbl is provided to interpret cop instructions,
bdaaa2e1 6718 this knowledge can be encoded in the itbl spec. */
252b5132
RH
6719
6720 case M_COP0:
6721 s = "c0";
6722 goto copz;
6723 case M_COP1:
6724 s = "c1";
6725 goto copz;
6726 case M_COP2:
6727 s = "c2";
6728 goto copz;
6729 case M_COP3:
6730 s = "c3";
6731 copz:
6732 /* For now we just do C (same as Cz). The parameter will be
6733 stored in insn_opcode by mips_ip. */
67c0d1eb 6734 macro_build (NULL, s, "C", ip->insn_opcode);
252b5132
RH
6735 return;
6736
ea1fb5dc 6737 case M_MOVE:
67c0d1eb 6738 move_register (dreg, sreg);
ea1fb5dc
RS
6739 return;
6740
252b5132
RH
6741#ifdef LOSING_COMPILER
6742 default:
6743 /* Try and see if this is a new itbl instruction.
6744 This code builds table entries out of the macros in mip_opcodes.
6745 FIXME: For now we just assemble the expression and pass it's
6746 value along as a 32-bit immediate.
bdaaa2e1 6747 We may want to have the assembler assemble this value,
252b5132
RH
6748 so that we gain the assembler's knowledge of delay slots,
6749 symbols, etc.
6750 Would it be more efficient to use mask (id) here? */
bdaaa2e1 6751 if (itbl_have_entries
252b5132 6752 && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
beae10d5 6753 {
252b5132
RH
6754 s = ip->insn_mo->name;
6755 s2 = "cop3";
6756 coproc = ITBL_DECODE_PNUM (immed_expr);;
67c0d1eb 6757 macro_build (&immed_expr, s, "C");
252b5132 6758 return;
beae10d5 6759 }
252b5132
RH
6760 macro2 (ip);
6761 return;
6762 }
6763 if (mips_opts.noat)
6764 as_warn (_("Macro used $at after \".set noat\""));
6765}
bdaaa2e1 6766
252b5132 6767static void
17a2f251 6768macro2 (struct mips_cl_insn *ip)
252b5132
RH
6769{
6770 register int treg, sreg, dreg, breg;
6771 int tempreg;
6772 int mask;
252b5132
RH
6773 int used_at;
6774 expressionS expr1;
6775 const char *s;
6776 const char *s2;
6777 const char *fmt;
6778 int likely = 0;
6779 int dbl = 0;
6780 int coproc = 0;
6781 int lr = 0;
6782 int imm = 0;
6783 int off;
6784 offsetT maxnum;
6785 bfd_reloc_code_real_type r;
bdaaa2e1 6786
252b5132
RH
6787 treg = (ip->insn_opcode >> 16) & 0x1f;
6788 dreg = (ip->insn_opcode >> 11) & 0x1f;
6789 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
6790 mask = ip->insn_mo->mask;
bdaaa2e1 6791
252b5132
RH
6792 expr1.X_op = O_constant;
6793 expr1.X_op_symbol = NULL;
6794 expr1.X_add_symbol = NULL;
6795 expr1.X_add_number = 1;
bdaaa2e1 6796
252b5132
RH
6797 switch (mask)
6798 {
6799#endif /* LOSING_COMPILER */
6800
6801 case M_DMUL:
6802 dbl = 1;
6803 case M_MUL:
67c0d1eb
RS
6804 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", sreg, treg);
6805 macro_build (NULL, "mflo", "d", dreg);
252b5132
RH
6806 return;
6807
6808 case M_DMUL_I:
6809 dbl = 1;
6810 case M_MUL_I:
6811 /* The MIPS assembler some times generates shifts and adds. I'm
6812 not trying to be that fancy. GCC should do this for us
6813 anyway. */
67c0d1eb
RS
6814 load_register (AT, &imm_expr, dbl);
6815 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, AT);
6816 macro_build (NULL, "mflo", "d", dreg);
252b5132
RH
6817 break;
6818
6819 case M_DMULO_I:
6820 dbl = 1;
6821 case M_MULO_I:
6822 imm = 1;
6823 goto do_mulo;
6824
6825 case M_DMULO:
6826 dbl = 1;
6827 case M_MULO:
6828 do_mulo:
b34976b6 6829 mips_emit_delays (TRUE);
252b5132
RH
6830 ++mips_opts.noreorder;
6831 mips_any_noreorder = 1;
6832 if (imm)
67c0d1eb
RS
6833 load_register (AT, &imm_expr, dbl);
6834 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
6835 macro_build (NULL, "mflo", "d", dreg);
6836 macro_build (NULL, dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, RA);
6837 macro_build (NULL, "mfhi", "d", AT);
252b5132 6838 if (mips_trap)
67c0d1eb 6839 macro_build (NULL, "tne", "s,t,q", dreg, AT, 6);
252b5132
RH
6840 else
6841 {
6842 expr1.X_add_number = 8;
67c0d1eb
RS
6843 macro_build (&expr1, "beq", "s,t,p", dreg, AT);
6844 macro_build (NULL, "nop", "", 0);
6845 macro_build (NULL, "break", "c", 6);
252b5132
RH
6846 }
6847 --mips_opts.noreorder;
67c0d1eb 6848 macro_build (NULL, "mflo", "d", dreg);
252b5132
RH
6849 break;
6850
6851 case M_DMULOU_I:
6852 dbl = 1;
6853 case M_MULOU_I:
6854 imm = 1;
6855 goto do_mulou;
6856
6857 case M_DMULOU:
6858 dbl = 1;
6859 case M_MULOU:
6860 do_mulou:
b34976b6 6861 mips_emit_delays (TRUE);
252b5132
RH
6862 ++mips_opts.noreorder;
6863 mips_any_noreorder = 1;
6864 if (imm)
67c0d1eb
RS
6865 load_register (AT, &imm_expr, dbl);
6866 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
17a2f251 6867 sreg, imm ? AT : treg);
67c0d1eb
RS
6868 macro_build (NULL, "mfhi", "d", AT);
6869 macro_build (NULL, "mflo", "d", dreg);
252b5132 6870 if (mips_trap)
67c0d1eb 6871 macro_build (NULL, "tne", "s,t,q", AT, 0, 6);
252b5132
RH
6872 else
6873 {
6874 expr1.X_add_number = 8;
67c0d1eb
RS
6875 macro_build (&expr1, "beq", "s,t,p", AT, 0);
6876 macro_build (NULL, "nop", "", 0);
6877 macro_build (NULL, "break", "c", 6);
252b5132
RH
6878 }
6879 --mips_opts.noreorder;
6880 break;
6881
771c7ce4 6882 case M_DROL:
fef14a42 6883 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097
CD
6884 {
6885 if (dreg == sreg)
6886 {
6887 tempreg = AT;
6888 used_at = 1;
6889 }
6890 else
6891 {
6892 tempreg = dreg;
6893 used_at = 0;
6894 }
67c0d1eb
RS
6895 macro_build (NULL, "dnegu", "d,w", tempreg, treg);
6896 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, tempreg);
82dd0097
CD
6897 if (used_at)
6898 break;
6899 return;
6900 }
67c0d1eb
RS
6901 macro_build (NULL, "dsubu", "d,v,t", AT, 0, treg);
6902 macro_build (NULL, "dsrlv", "d,t,s", AT, sreg, AT);
6903 macro_build (NULL, "dsllv", "d,t,s", dreg, sreg, treg);
6904 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
6905 break;
6906
252b5132 6907 case M_ROL:
fef14a42 6908 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097
CD
6909 {
6910 if (dreg == sreg)
6911 {
6912 tempreg = AT;
6913 used_at = 1;
6914 }
6915 else
6916 {
6917 tempreg = dreg;
6918 used_at = 0;
6919 }
67c0d1eb
RS
6920 macro_build (NULL, "negu", "d,w", tempreg, treg);
6921 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, tempreg);
82dd0097
CD
6922 if (used_at)
6923 break;
6924 return;
6925 }
67c0d1eb
RS
6926 macro_build (NULL, "subu", "d,v,t", AT, 0, treg);
6927 macro_build (NULL, "srlv", "d,t,s", AT, sreg, AT);
6928 macro_build (NULL, "sllv", "d,t,s", dreg, sreg, treg);
6929 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
6930 break;
6931
771c7ce4
TS
6932 case M_DROL_I:
6933 {
6934 unsigned int rot;
82dd0097 6935 char *l, *r;
771c7ce4
TS
6936
6937 if (imm_expr.X_op != O_constant)
82dd0097 6938 as_bad (_("Improper rotate count"));
771c7ce4 6939 rot = imm_expr.X_add_number & 0x3f;
fef14a42 6940 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
60b63b72
RS
6941 {
6942 rot = (64 - rot) & 0x3f;
6943 if (rot >= 32)
67c0d1eb 6944 macro_build (NULL, "dror32", "d,w,<", dreg, sreg, rot - 32);
60b63b72 6945 else
67c0d1eb 6946 macro_build (NULL, "dror", "d,w,<", dreg, sreg, rot);
82dd0097 6947 return;
60b63b72 6948 }
483fc7cd 6949 if (rot == 0)
483fc7cd 6950 {
67c0d1eb 6951 macro_build (NULL, "dsrl", "d,w,<", dreg, sreg, 0);
82dd0097 6952 return;
483fc7cd 6953 }
82dd0097
CD
6954 l = (rot < 0x20) ? "dsll" : "dsll32";
6955 r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
6956 rot &= 0x1f;
67c0d1eb
RS
6957 macro_build (NULL, l, "d,w,<", AT, sreg, rot);
6958 macro_build (NULL, r, "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6959 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
6960 }
6961 break;
6962
252b5132 6963 case M_ROL_I:
771c7ce4
TS
6964 {
6965 unsigned int rot;
6966
6967 if (imm_expr.X_op != O_constant)
82dd0097 6968 as_bad (_("Improper rotate count"));
771c7ce4 6969 rot = imm_expr.X_add_number & 0x1f;
fef14a42 6970 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
60b63b72 6971 {
67c0d1eb 6972 macro_build (NULL, "ror", "d,w,<", dreg, sreg, (32 - rot) & 0x1f);
82dd0097 6973 return;
60b63b72 6974 }
483fc7cd 6975 if (rot == 0)
483fc7cd 6976 {
67c0d1eb 6977 macro_build (NULL, "srl", "d,w,<", dreg, sreg, 0);
82dd0097 6978 return;
483fc7cd 6979 }
67c0d1eb
RS
6980 macro_build (NULL, "sll", "d,w,<", AT, sreg, rot);
6981 macro_build (NULL, "srl", "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6982 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
6983 }
6984 break;
6985
6986 case M_DROR:
fef14a42 6987 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097 6988 {
67c0d1eb 6989 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, treg);
82dd0097
CD
6990 return;
6991 }
67c0d1eb
RS
6992 macro_build (NULL, "dsubu", "d,v,t", AT, 0, treg);
6993 macro_build (NULL, "dsllv", "d,t,s", AT, sreg, AT);
6994 macro_build (NULL, "dsrlv", "d,t,s", dreg, sreg, treg);
6995 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
6996 break;
6997
6998 case M_ROR:
fef14a42 6999 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 7000 {
67c0d1eb 7001 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, treg);
82dd0097
CD
7002 return;
7003 }
67c0d1eb
RS
7004 macro_build (NULL, "subu", "d,v,t", AT, 0, treg);
7005 macro_build (NULL, "sllv", "d,t,s", AT, sreg, AT);
7006 macro_build (NULL, "srlv", "d,t,s", dreg, sreg, treg);
7007 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
7008 break;
7009
771c7ce4
TS
7010 case M_DROR_I:
7011 {
7012 unsigned int rot;
82dd0097 7013 char *l, *r;
771c7ce4
TS
7014
7015 if (imm_expr.X_op != O_constant)
82dd0097 7016 as_bad (_("Improper rotate count"));
771c7ce4 7017 rot = imm_expr.X_add_number & 0x3f;
fef14a42 7018 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097
CD
7019 {
7020 if (rot >= 32)
67c0d1eb 7021 macro_build (NULL, "dror32", "d,w,<", dreg, sreg, rot - 32);
82dd0097 7022 else
67c0d1eb 7023 macro_build (NULL, "dror", "d,w,<", dreg, sreg, rot);
82dd0097
CD
7024 return;
7025 }
483fc7cd 7026 if (rot == 0)
483fc7cd 7027 {
67c0d1eb 7028 macro_build (NULL, "dsrl", "d,w,<", dreg, sreg, 0);
82dd0097 7029 return;
483fc7cd 7030 }
82dd0097
CD
7031 r = (rot < 0x20) ? "dsrl" : "dsrl32";
7032 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
7033 rot &= 0x1f;
67c0d1eb
RS
7034 macro_build (NULL, r, "d,w,<", AT, sreg, rot);
7035 macro_build (NULL, l, "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7036 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
7037 }
7038 break;
7039
252b5132 7040 case M_ROR_I:
771c7ce4
TS
7041 {
7042 unsigned int rot;
7043
7044 if (imm_expr.X_op != O_constant)
82dd0097 7045 as_bad (_("Improper rotate count"));
771c7ce4 7046 rot = imm_expr.X_add_number & 0x1f;
fef14a42 7047 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 7048 {
67c0d1eb 7049 macro_build (NULL, "ror", "d,w,<", dreg, sreg, rot);
82dd0097
CD
7050 return;
7051 }
483fc7cd 7052 if (rot == 0)
483fc7cd 7053 {
67c0d1eb 7054 macro_build (NULL, "srl", "d,w,<", dreg, sreg, 0);
82dd0097 7055 return;
483fc7cd 7056 }
67c0d1eb
RS
7057 macro_build (NULL, "srl", "d,w,<", AT, sreg, rot);
7058 macro_build (NULL, "sll", "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7059 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4 7060 }
252b5132
RH
7061 break;
7062
7063 case M_S_DOB:
fef14a42 7064 if (mips_opts.arch == CPU_R4650)
252b5132
RH
7065 {
7066 as_bad (_("opcode not supported on this processor"));
7067 return;
7068 }
e7af610e 7069 assert (mips_opts.isa == ISA_MIPS1);
252b5132
RH
7070 /* Even on a big endian machine $fn comes before $fn+1. We have
7071 to adjust when storing to memory. */
67c0d1eb
RS
7072 macro_build (&offset_expr, "swc1", "T,o(b)",
7073 target_big_endian ? treg + 1 : treg, BFD_RELOC_LO16, breg);
252b5132 7074 offset_expr.X_add_number += 4;
67c0d1eb
RS
7075 macro_build (&offset_expr, "swc1", "T,o(b)",
7076 target_big_endian ? treg : treg + 1, BFD_RELOC_LO16, breg);
252b5132
RH
7077 return;
7078
7079 case M_SEQ:
7080 if (sreg == 0)
67c0d1eb 7081 macro_build (&expr1, "sltiu", "t,r,j", dreg, treg, BFD_RELOC_LO16);
252b5132 7082 else if (treg == 0)
67c0d1eb 7083 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7084 else
7085 {
67c0d1eb
RS
7086 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
7087 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
252b5132
RH
7088 }
7089 return;
7090
7091 case M_SEQ_I:
7092 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7093 {
67c0d1eb 7094 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7095 return;
7096 }
7097 if (sreg == 0)
7098 {
7099 as_warn (_("Instruction %s: result is always false"),
7100 ip->insn_mo->name);
67c0d1eb 7101 move_register (dreg, 0);
252b5132
RH
7102 return;
7103 }
7104 if (imm_expr.X_op == O_constant
7105 && imm_expr.X_add_number >= 0
7106 && imm_expr.X_add_number < 0x10000)
7107 {
67c0d1eb 7108 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7109 used_at = 0;
7110 }
7111 else if (imm_expr.X_op == O_constant
7112 && imm_expr.X_add_number > -0x8000
7113 && imm_expr.X_add_number < 0)
7114 {
7115 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 7116 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
17a2f251 7117 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7118 used_at = 0;
7119 }
7120 else
7121 {
67c0d1eb
RS
7122 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7123 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
252b5132
RH
7124 used_at = 1;
7125 }
67c0d1eb 7126 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
252b5132
RH
7127 if (used_at)
7128 break;
7129 return;
7130
7131 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
7132 s = "slt";
7133 goto sge;
7134 case M_SGEU:
7135 s = "sltu";
7136 sge:
67c0d1eb
RS
7137 macro_build (NULL, s, "d,v,t", dreg, sreg, treg);
7138 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
252b5132
RH
7139 return;
7140
7141 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
7142 case M_SGEU_I:
7143 if (imm_expr.X_op == O_constant
7144 && imm_expr.X_add_number >= -0x8000
7145 && imm_expr.X_add_number < 0x8000)
7146 {
67c0d1eb
RS
7147 macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
7148 dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7149 used_at = 0;
7150 }
7151 else
7152 {
67c0d1eb
RS
7153 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7154 macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
7155 dreg, sreg, AT);
252b5132
RH
7156 used_at = 1;
7157 }
67c0d1eb 7158 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
252b5132
RH
7159 if (used_at)
7160 break;
7161 return;
7162
7163 case M_SGT: /* sreg > treg <==> treg < sreg */
7164 s = "slt";
7165 goto sgt;
7166 case M_SGTU:
7167 s = "sltu";
7168 sgt:
67c0d1eb 7169 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
252b5132
RH
7170 return;
7171
7172 case M_SGT_I: /* sreg > I <==> I < sreg */
7173 s = "slt";
7174 goto sgti;
7175 case M_SGTU_I:
7176 s = "sltu";
7177 sgti:
67c0d1eb
RS
7178 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7179 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
252b5132
RH
7180 break;
7181
2396cfb9 7182 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
252b5132
RH
7183 s = "slt";
7184 goto sle;
7185 case M_SLEU:
7186 s = "sltu";
7187 sle:
67c0d1eb
RS
7188 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
7189 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
252b5132
RH
7190 return;
7191
2396cfb9 7192 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
252b5132
RH
7193 s = "slt";
7194 goto slei;
7195 case M_SLEU_I:
7196 s = "sltu";
7197 slei:
67c0d1eb
RS
7198 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7199 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
7200 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
252b5132
RH
7201 break;
7202
7203 case M_SLT_I:
7204 if (imm_expr.X_op == O_constant
7205 && imm_expr.X_add_number >= -0x8000
7206 && imm_expr.X_add_number < 0x8000)
7207 {
67c0d1eb 7208 macro_build (&imm_expr, "slti", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7209 return;
7210 }
67c0d1eb
RS
7211 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7212 macro_build (NULL, "slt", "d,v,t", dreg, sreg, AT);
252b5132
RH
7213 break;
7214
7215 case M_SLTU_I:
7216 if (imm_expr.X_op == O_constant
7217 && imm_expr.X_add_number >= -0x8000
7218 && imm_expr.X_add_number < 0x8000)
7219 {
67c0d1eb 7220 macro_build (&imm_expr, "sltiu", "t,r,j", dreg, sreg,
17a2f251 7221 BFD_RELOC_LO16);
252b5132
RH
7222 return;
7223 }
67c0d1eb
RS
7224 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7225 macro_build (NULL, "sltu", "d,v,t", dreg, sreg, AT);
252b5132
RH
7226 break;
7227
7228 case M_SNE:
7229 if (sreg == 0)
67c0d1eb 7230 macro_build (NULL, "sltu", "d,v,t", dreg, 0, treg);
252b5132 7231 else if (treg == 0)
67c0d1eb 7232 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
252b5132
RH
7233 else
7234 {
67c0d1eb
RS
7235 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
7236 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
252b5132
RH
7237 }
7238 return;
7239
7240 case M_SNE_I:
7241 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7242 {
67c0d1eb 7243 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
252b5132
RH
7244 return;
7245 }
7246 if (sreg == 0)
7247 {
7248 as_warn (_("Instruction %s: result is always true"),
7249 ip->insn_mo->name);
67c0d1eb
RS
7250 macro_build (&expr1, HAVE_32BIT_GPRS ? "addiu" : "daddiu", "t,r,j",
7251 dreg, 0, BFD_RELOC_LO16);
252b5132
RH
7252 return;
7253 }
7254 if (imm_expr.X_op == O_constant
7255 && imm_expr.X_add_number >= 0
7256 && imm_expr.X_add_number < 0x10000)
7257 {
67c0d1eb 7258 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7259 used_at = 0;
7260 }
7261 else if (imm_expr.X_op == O_constant
7262 && imm_expr.X_add_number > -0x8000
7263 && imm_expr.X_add_number < 0)
7264 {
7265 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 7266 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
17a2f251 7267 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7268 used_at = 0;
7269 }
7270 else
7271 {
67c0d1eb
RS
7272 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7273 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
252b5132
RH
7274 used_at = 1;
7275 }
67c0d1eb 7276 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
252b5132
RH
7277 if (used_at)
7278 break;
7279 return;
7280
7281 case M_DSUB_I:
7282 dbl = 1;
7283 case M_SUB_I:
7284 if (imm_expr.X_op == O_constant
7285 && imm_expr.X_add_number > -0x8000
7286 && imm_expr.X_add_number <= 0x8000)
7287 {
7288 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb
RS
7289 macro_build (&imm_expr, dbl ? "daddi" : "addi", "t,r,j",
7290 dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7291 return;
7292 }
67c0d1eb
RS
7293 load_register (AT, &imm_expr, dbl);
7294 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
252b5132
RH
7295 break;
7296
7297 case M_DSUBU_I:
7298 dbl = 1;
7299 case M_SUBU_I:
7300 if (imm_expr.X_op == O_constant
7301 && imm_expr.X_add_number > -0x8000
7302 && imm_expr.X_add_number <= 0x8000)
7303 {
7304 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb
RS
7305 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "t,r,j",
7306 dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7307 return;
7308 }
67c0d1eb
RS
7309 load_register (AT, &imm_expr, dbl);
7310 macro_build (NULL, dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
252b5132
RH
7311 break;
7312
7313 case M_TEQ_I:
7314 s = "teq";
7315 goto trap;
7316 case M_TGE_I:
7317 s = "tge";
7318 goto trap;
7319 case M_TGEU_I:
7320 s = "tgeu";
7321 goto trap;
7322 case M_TLT_I:
7323 s = "tlt";
7324 goto trap;
7325 case M_TLTU_I:
7326 s = "tltu";
7327 goto trap;
7328 case M_TNE_I:
7329 s = "tne";
7330 trap:
67c0d1eb
RS
7331 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7332 macro_build (NULL, s, "s,t", sreg, AT);
252b5132
RH
7333 break;
7334
252b5132 7335 case M_TRUNCWS:
43841e91 7336 case M_TRUNCWD:
e7af610e 7337 assert (mips_opts.isa == ISA_MIPS1);
252b5132
RH
7338 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
7339 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
7340
7341 /*
7342 * Is the double cfc1 instruction a bug in the mips assembler;
7343 * or is there a reason for it?
7344 */
b34976b6 7345 mips_emit_delays (TRUE);
252b5132
RH
7346 ++mips_opts.noreorder;
7347 mips_any_noreorder = 1;
67c0d1eb
RS
7348 macro_build (NULL, "cfc1", "t,G", treg, RA);
7349 macro_build (NULL, "cfc1", "t,G", treg, RA);
7350 macro_build (NULL, "nop", "");
252b5132 7351 expr1.X_add_number = 3;
67c0d1eb 7352 macro_build (&expr1, "ori", "t,r,i", AT, treg, BFD_RELOC_LO16);
252b5132 7353 expr1.X_add_number = 2;
67c0d1eb
RS
7354 macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
7355 macro_build (NULL, "ctc1", "t,G", AT, RA);
7356 macro_build (NULL, "nop", "");
7357 macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
7358 dreg, sreg);
7359 macro_build (NULL, "ctc1", "t,G", treg, RA);
7360 macro_build (NULL, "nop", "");
252b5132
RH
7361 --mips_opts.noreorder;
7362 break;
7363
7364 case M_ULH:
7365 s = "lb";
7366 goto ulh;
7367 case M_ULHU:
7368 s = "lbu";
7369 ulh:
7370 if (offset_expr.X_add_number >= 0x7fff)
7371 as_bad (_("operand overflow"));
252b5132 7372 if (! target_big_endian)
f9419b05 7373 ++offset_expr.X_add_number;
67c0d1eb 7374 macro_build (&offset_expr, s, "t,o(b)", AT, BFD_RELOC_LO16, breg);
252b5132 7375 if (! target_big_endian)
f9419b05 7376 --offset_expr.X_add_number;
252b5132 7377 else
f9419b05 7378 ++offset_expr.X_add_number;
67c0d1eb
RS
7379 macro_build (&offset_expr, "lbu", "t,o(b)", treg, BFD_RELOC_LO16, breg);
7380 macro_build (NULL, "sll", "d,w,<", AT, AT, 8);
7381 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
252b5132
RH
7382 break;
7383
7384 case M_ULD:
7385 s = "ldl";
7386 s2 = "ldr";
7387 off = 7;
7388 goto ulw;
7389 case M_ULW:
7390 s = "lwl";
7391 s2 = "lwr";
7392 off = 3;
7393 ulw:
7394 if (offset_expr.X_add_number >= 0x8000 - off)
7395 as_bad (_("operand overflow"));
af22f5b2
CD
7396 if (treg != breg)
7397 tempreg = treg;
7398 else
7399 tempreg = AT;
252b5132
RH
7400 if (! target_big_endian)
7401 offset_expr.X_add_number += off;
67c0d1eb 7402 macro_build (&offset_expr, s, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
252b5132
RH
7403 if (! target_big_endian)
7404 offset_expr.X_add_number -= off;
7405 else
7406 offset_expr.X_add_number += off;
67c0d1eb 7407 macro_build (&offset_expr, s2, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
af22f5b2
CD
7408
7409 /* If necessary, move the result in tempreg the final destination. */
7410 if (treg == tempreg)
7411 return;
7412 /* Protect second load's delay slot. */
017315e4 7413 load_delay_nop ();
67c0d1eb 7414 move_register (treg, tempreg);
af22f5b2 7415 break;
252b5132
RH
7416
7417 case M_ULD_A:
7418 s = "ldl";
7419 s2 = "ldr";
7420 off = 7;
7421 goto ulwa;
7422 case M_ULW_A:
7423 s = "lwl";
7424 s2 = "lwr";
7425 off = 3;
7426 ulwa:
d6bc6245 7427 used_at = 1;
67c0d1eb 7428 load_address (AT, &offset_expr, &used_at);
252b5132 7429 if (breg != 0)
67c0d1eb 7430 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
252b5132
RH
7431 if (! target_big_endian)
7432 expr1.X_add_number = off;
7433 else
7434 expr1.X_add_number = 0;
67c0d1eb 7435 macro_build (&expr1, s, "t,o(b)", treg, BFD_RELOC_LO16, AT);
252b5132
RH
7436 if (! target_big_endian)
7437 expr1.X_add_number = 0;
7438 else
7439 expr1.X_add_number = off;
67c0d1eb 7440 macro_build (&expr1, s2, "t,o(b)", treg, BFD_RELOC_LO16, AT);
252b5132
RH
7441 break;
7442
7443 case M_ULH_A:
7444 case M_ULHU_A:
d6bc6245 7445 used_at = 1;
67c0d1eb 7446 load_address (AT, &offset_expr, &used_at);
252b5132 7447 if (breg != 0)
67c0d1eb 7448 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
252b5132
RH
7449 if (target_big_endian)
7450 expr1.X_add_number = 0;
67c0d1eb 7451 macro_build (&expr1, mask == M_ULH_A ? "lb" : "lbu", "t,o(b)",
17a2f251 7452 treg, BFD_RELOC_LO16, AT);
252b5132
RH
7453 if (target_big_endian)
7454 expr1.X_add_number = 1;
7455 else
7456 expr1.X_add_number = 0;
67c0d1eb
RS
7457 macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
7458 macro_build (NULL, "sll", "d,w,<", treg, treg, 8);
7459 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
252b5132
RH
7460 break;
7461
7462 case M_USH:
7463 if (offset_expr.X_add_number >= 0x7fff)
7464 as_bad (_("operand overflow"));
7465 if (target_big_endian)
f9419b05 7466 ++offset_expr.X_add_number;
67c0d1eb
RS
7467 macro_build (&offset_expr, "sb", "t,o(b)", treg, BFD_RELOC_LO16, breg);
7468 macro_build (NULL, "srl", "d,w,<", AT, treg, 8);
252b5132 7469 if (target_big_endian)
f9419b05 7470 --offset_expr.X_add_number;
252b5132 7471 else
f9419b05 7472 ++offset_expr.X_add_number;
67c0d1eb 7473 macro_build (&offset_expr, "sb", "t,o(b)", AT, BFD_RELOC_LO16, breg);
252b5132
RH
7474 break;
7475
7476 case M_USD:
7477 s = "sdl";
7478 s2 = "sdr";
7479 off = 7;
7480 goto usw;
7481 case M_USW:
7482 s = "swl";
7483 s2 = "swr";
7484 off = 3;
7485 usw:
7486 if (offset_expr.X_add_number >= 0x8000 - off)
7487 as_bad (_("operand overflow"));
7488 if (! target_big_endian)
7489 offset_expr.X_add_number += off;
67c0d1eb 7490 macro_build (&offset_expr, s, "t,o(b)", treg, BFD_RELOC_LO16, breg);
252b5132
RH
7491 if (! target_big_endian)
7492 offset_expr.X_add_number -= off;
7493 else
7494 offset_expr.X_add_number += off;
67c0d1eb 7495 macro_build (&offset_expr, s2, "t,o(b)", treg, BFD_RELOC_LO16, breg);
252b5132
RH
7496 return;
7497
7498 case M_USD_A:
7499 s = "sdl";
7500 s2 = "sdr";
7501 off = 7;
7502 goto uswa;
7503 case M_USW_A:
7504 s = "swl";
7505 s2 = "swr";
7506 off = 3;
7507 uswa:
d6bc6245 7508 used_at = 1;
67c0d1eb 7509 load_address (AT, &offset_expr, &used_at);
252b5132 7510 if (breg != 0)
67c0d1eb 7511 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
252b5132
RH
7512 if (! target_big_endian)
7513 expr1.X_add_number = off;
7514 else
7515 expr1.X_add_number = 0;
67c0d1eb 7516 macro_build (&expr1, s, "t,o(b)", treg, BFD_RELOC_LO16, AT);
252b5132
RH
7517 if (! target_big_endian)
7518 expr1.X_add_number = 0;
7519 else
7520 expr1.X_add_number = off;
67c0d1eb 7521 macro_build (&expr1, s2, "t,o(b)", treg, BFD_RELOC_LO16, AT);
252b5132
RH
7522 break;
7523
7524 case M_USH_A:
d6bc6245 7525 used_at = 1;
67c0d1eb 7526 load_address (AT, &offset_expr, &used_at);
252b5132 7527 if (breg != 0)
67c0d1eb 7528 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
252b5132
RH
7529 if (! target_big_endian)
7530 expr1.X_add_number = 0;
67c0d1eb
RS
7531 macro_build (&expr1, "sb", "t,o(b)", treg, BFD_RELOC_LO16, AT);
7532 macro_build (NULL, "srl", "d,w,<", treg, treg, 8);
252b5132
RH
7533 if (! target_big_endian)
7534 expr1.X_add_number = 1;
7535 else
7536 expr1.X_add_number = 0;
67c0d1eb 7537 macro_build (&expr1, "sb", "t,o(b)", treg, BFD_RELOC_LO16, AT);
252b5132
RH
7538 if (! target_big_endian)
7539 expr1.X_add_number = 0;
7540 else
7541 expr1.X_add_number = 1;
67c0d1eb
RS
7542 macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
7543 macro_build (NULL, "sll", "d,w,<", treg, treg, 8);
7544 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
252b5132
RH
7545 break;
7546
7547 default:
7548 /* FIXME: Check if this is one of the itbl macros, since they
bdaaa2e1 7549 are added dynamically. */
252b5132
RH
7550 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7551 break;
7552 }
7553 if (mips_opts.noat)
7554 as_warn (_("Macro used $at after \".set noat\""));
7555}
7556
7557/* Implement macros in mips16 mode. */
7558
7559static void
17a2f251 7560mips16_macro (struct mips_cl_insn *ip)
252b5132
RH
7561{
7562 int mask;
7563 int xreg, yreg, zreg, tmp;
252b5132
RH
7564 expressionS expr1;
7565 int dbl;
7566 const char *s, *s2, *s3;
7567
7568 mask = ip->insn_mo->mask;
7569
7570 xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
7571 yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
7572 zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
7573
252b5132
RH
7574 expr1.X_op = O_constant;
7575 expr1.X_op_symbol = NULL;
7576 expr1.X_add_symbol = NULL;
7577 expr1.X_add_number = 1;
7578
7579 dbl = 0;
7580
7581 switch (mask)
7582 {
7583 default:
7584 internalError ();
7585
7586 case M_DDIV_3:
7587 dbl = 1;
7588 case M_DIV_3:
7589 s = "mflo";
7590 goto do_div3;
7591 case M_DREM_3:
7592 dbl = 1;
7593 case M_REM_3:
7594 s = "mfhi";
7595 do_div3:
b34976b6 7596 mips_emit_delays (TRUE);
252b5132
RH
7597 ++mips_opts.noreorder;
7598 mips_any_noreorder = 1;
67c0d1eb 7599 macro_build (NULL, dbl ? "ddiv" : "div", "0,x,y", xreg, yreg);
252b5132 7600 expr1.X_add_number = 2;
67c0d1eb
RS
7601 macro_build (&expr1, "bnez", "x,p", yreg);
7602 macro_build (NULL, "break", "6", 7);
bdaaa2e1 7603
252b5132
RH
7604 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7605 since that causes an overflow. We should do that as well,
7606 but I don't see how to do the comparisons without a temporary
7607 register. */
7608 --mips_opts.noreorder;
67c0d1eb 7609 macro_build (NULL, s, "x", zreg);
252b5132
RH
7610 break;
7611
7612 case M_DIVU_3:
7613 s = "divu";
7614 s2 = "mflo";
7615 goto do_divu3;
7616 case M_REMU_3:
7617 s = "divu";
7618 s2 = "mfhi";
7619 goto do_divu3;
7620 case M_DDIVU_3:
7621 s = "ddivu";
7622 s2 = "mflo";
7623 goto do_divu3;
7624 case M_DREMU_3:
7625 s = "ddivu";
7626 s2 = "mfhi";
7627 do_divu3:
b34976b6 7628 mips_emit_delays (TRUE);
252b5132
RH
7629 ++mips_opts.noreorder;
7630 mips_any_noreorder = 1;
67c0d1eb 7631 macro_build (NULL, s, "0,x,y", xreg, yreg);
252b5132 7632 expr1.X_add_number = 2;
67c0d1eb
RS
7633 macro_build (&expr1, "bnez", "x,p", yreg);
7634 macro_build (NULL, "break", "6", 7);
252b5132 7635 --mips_opts.noreorder;
67c0d1eb 7636 macro_build (NULL, s2, "x", zreg);
252b5132
RH
7637 break;
7638
7639 case M_DMUL:
7640 dbl = 1;
7641 case M_MUL:
67c0d1eb
RS
7642 macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
7643 macro_build (NULL, "mflo", "x", zreg);
252b5132
RH
7644 return;
7645
7646 case M_DSUBU_I:
7647 dbl = 1;
7648 goto do_subu;
7649 case M_SUBU_I:
7650 do_subu:
7651 if (imm_expr.X_op != O_constant)
7652 as_bad (_("Unsupported large constant"));
7653 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 7654 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
252b5132
RH
7655 break;
7656
7657 case M_SUBU_I_2:
7658 if (imm_expr.X_op != O_constant)
7659 as_bad (_("Unsupported large constant"));
7660 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 7661 macro_build (&imm_expr, "addiu", "x,k", xreg);
252b5132
RH
7662 break;
7663
7664 case M_DSUBU_I_2:
7665 if (imm_expr.X_op != O_constant)
7666 as_bad (_("Unsupported large constant"));
7667 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 7668 macro_build (&imm_expr, "daddiu", "y,j", yreg);
252b5132
RH
7669 break;
7670
7671 case M_BEQ:
7672 s = "cmp";
7673 s2 = "bteqz";
7674 goto do_branch;
7675 case M_BNE:
7676 s = "cmp";
7677 s2 = "btnez";
7678 goto do_branch;
7679 case M_BLT:
7680 s = "slt";
7681 s2 = "btnez";
7682 goto do_branch;
7683 case M_BLTU:
7684 s = "sltu";
7685 s2 = "btnez";
7686 goto do_branch;
7687 case M_BLE:
7688 s = "slt";
7689 s2 = "bteqz";
7690 goto do_reverse_branch;
7691 case M_BLEU:
7692 s = "sltu";
7693 s2 = "bteqz";
7694 goto do_reverse_branch;
7695 case M_BGE:
7696 s = "slt";
7697 s2 = "bteqz";
7698 goto do_branch;
7699 case M_BGEU:
7700 s = "sltu";
7701 s2 = "bteqz";
7702 goto do_branch;
7703 case M_BGT:
7704 s = "slt";
7705 s2 = "btnez";
7706 goto do_reverse_branch;
7707 case M_BGTU:
7708 s = "sltu";
7709 s2 = "btnez";
7710
7711 do_reverse_branch:
7712 tmp = xreg;
7713 xreg = yreg;
7714 yreg = tmp;
7715
7716 do_branch:
67c0d1eb
RS
7717 macro_build (NULL, s, "x,y", xreg, yreg);
7718 macro_build (&offset_expr, s2, "p");
252b5132
RH
7719 break;
7720
7721 case M_BEQ_I:
7722 s = "cmpi";
7723 s2 = "bteqz";
7724 s3 = "x,U";
7725 goto do_branch_i;
7726 case M_BNE_I:
7727 s = "cmpi";
7728 s2 = "btnez";
7729 s3 = "x,U";
7730 goto do_branch_i;
7731 case M_BLT_I:
7732 s = "slti";
7733 s2 = "btnez";
7734 s3 = "x,8";
7735 goto do_branch_i;
7736 case M_BLTU_I:
7737 s = "sltiu";
7738 s2 = "btnez";
7739 s3 = "x,8";
7740 goto do_branch_i;
7741 case M_BLE_I:
7742 s = "slti";
7743 s2 = "btnez";
7744 s3 = "x,8";
7745 goto do_addone_branch_i;
7746 case M_BLEU_I:
7747 s = "sltiu";
7748 s2 = "btnez";
7749 s3 = "x,8";
7750 goto do_addone_branch_i;
7751 case M_BGE_I:
7752 s = "slti";
7753 s2 = "bteqz";
7754 s3 = "x,8";
7755 goto do_branch_i;
7756 case M_BGEU_I:
7757 s = "sltiu";
7758 s2 = "bteqz";
7759 s3 = "x,8";
7760 goto do_branch_i;
7761 case M_BGT_I:
7762 s = "slti";
7763 s2 = "bteqz";
7764 s3 = "x,8";
7765 goto do_addone_branch_i;
7766 case M_BGTU_I:
7767 s = "sltiu";
7768 s2 = "bteqz";
7769 s3 = "x,8";
7770
7771 do_addone_branch_i:
7772 if (imm_expr.X_op != O_constant)
7773 as_bad (_("Unsupported large constant"));
7774 ++imm_expr.X_add_number;
7775
7776 do_branch_i:
67c0d1eb
RS
7777 macro_build (&imm_expr, s, s3, xreg);
7778 macro_build (&offset_expr, s2, "p");
252b5132
RH
7779 break;
7780
7781 case M_ABS:
7782 expr1.X_add_number = 0;
67c0d1eb 7783 macro_build (&expr1, "slti", "x,8", yreg);
252b5132 7784 if (xreg != yreg)
67c0d1eb 7785 move_register (xreg, yreg);
252b5132 7786 expr1.X_add_number = 2;
67c0d1eb
RS
7787 macro_build (&expr1, "bteqz", "p");
7788 macro_build (NULL, "neg", "x,w", xreg, xreg);
252b5132
RH
7789 }
7790}
7791
7792/* For consistency checking, verify that all bits are specified either
7793 by the match/mask part of the instruction definition, or by the
7794 operand list. */
7795static int
17a2f251 7796validate_mips_insn (const struct mips_opcode *opc)
252b5132
RH
7797{
7798 const char *p = opc->args;
7799 char c;
7800 unsigned long used_bits = opc->mask;
7801
7802 if ((used_bits & opc->match) != opc->match)
7803 {
7804 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
7805 opc->name, opc->args);
7806 return 0;
7807 }
7808#define USE_BITS(mask,shift) (used_bits |= ((mask) << (shift)))
7809 while (*p)
7810 switch (c = *p++)
7811 {
7812 case ',': break;
7813 case '(': break;
7814 case ')': break;
af7ee8bf
CD
7815 case '+':
7816 switch (c = *p++)
7817 {
7818 case 'A': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7819 case 'B': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
7820 case 'C': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
bbcc0807
CD
7821 case 'D': USE_BITS (OP_MASK_RD, OP_SH_RD);
7822 USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
5f74bc13
CD
7823 case 'E': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7824 case 'F': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
7825 case 'G': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
7826 case 'H': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
7827 case 'I': break;
af7ee8bf
CD
7828 default:
7829 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
7830 c, opc->name, opc->args);
7831 return 0;
7832 }
7833 break;
252b5132
RH
7834 case '<': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7835 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7836 case 'A': break;
4372b673 7837 case 'B': USE_BITS (OP_MASK_CODE20, OP_SH_CODE20); break;
252b5132
RH
7838 case 'C': USE_BITS (OP_MASK_COPZ, OP_SH_COPZ); break;
7839 case 'D': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
7840 case 'E': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7841 case 'F': break;
7842 case 'G': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
156c2f8b 7843 case 'H': USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
252b5132 7844 case 'I': break;
e972090a 7845 case 'J': USE_BITS (OP_MASK_CODE19, OP_SH_CODE19); break;
af7ee8bf 7846 case 'K': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
252b5132
RH
7847 case 'L': break;
7848 case 'M': USE_BITS (OP_MASK_CCC, OP_SH_CCC); break;
7849 case 'N': USE_BITS (OP_MASK_BCC, OP_SH_BCC); break;
deec1734
CD
7850 case 'O': USE_BITS (OP_MASK_ALN, OP_SH_ALN); break;
7851 case 'Q': USE_BITS (OP_MASK_VSEL, OP_SH_VSEL);
7852 USE_BITS (OP_MASK_FT, OP_SH_FT); break;
252b5132
RH
7853 case 'R': USE_BITS (OP_MASK_FR, OP_SH_FR); break;
7854 case 'S': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7855 case 'T': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7856 case 'V': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7857 case 'W': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
deec1734
CD
7858 case 'X': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
7859 case 'Y': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7860 case 'Z': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
252b5132
RH
7861 case 'a': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break;
7862 case 'b': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7863 case 'c': USE_BITS (OP_MASK_CODE, OP_SH_CODE); break;
7864 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
7865 case 'f': break;
7866 case 'h': USE_BITS (OP_MASK_PREFX, OP_SH_PREFX); break;
7867 case 'i': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
7868 case 'j': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7869 case 'k': USE_BITS (OP_MASK_CACHE, OP_SH_CACHE); break;
7870 case 'l': break;
7871 case 'o': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7872 case 'p': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7873 case 'q': USE_BITS (OP_MASK_CODE2, OP_SH_CODE2); break;
7874 case 'r': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7875 case 's': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7876 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7877 case 'u': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
7878 case 'v': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7879 case 'w': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7880 case 'x': break;
7881 case 'z': break;
7882 case 'P': USE_BITS (OP_MASK_PERFREG, OP_SH_PERFREG); break;
4372b673
NC
7883 case 'U': USE_BITS (OP_MASK_RD, OP_SH_RD);
7884 USE_BITS (OP_MASK_RT, OP_SH_RT); break;
60b63b72
RS
7885 case 'e': USE_BITS (OP_MASK_VECBYTE, OP_SH_VECBYTE); break;
7886 case '%': USE_BITS (OP_MASK_VECALIGN, OP_SH_VECALIGN); break;
7887 case '[': break;
7888 case ']': break;
252b5132
RH
7889 default:
7890 as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
7891 c, opc->name, opc->args);
7892 return 0;
7893 }
7894#undef USE_BITS
7895 if (used_bits != 0xffffffff)
7896 {
7897 as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
7898 ~used_bits & 0xffffffff, opc->name, opc->args);
7899 return 0;
7900 }
7901 return 1;
7902}
7903
7904/* This routine assembles an instruction into its binary format. As a
7905 side effect, it sets one of the global variables imm_reloc or
7906 offset_reloc to the type of relocation to do if one of the operands
7907 is an address expression. */
7908
7909static void
17a2f251 7910mips_ip (char *str, struct mips_cl_insn *ip)
252b5132
RH
7911{
7912 char *s;
7913 const char *args;
43841e91 7914 char c = 0;
252b5132
RH
7915 struct mips_opcode *insn;
7916 char *argsStart;
7917 unsigned int regno;
7918 unsigned int lastregno = 0;
af7ee8bf 7919 unsigned int lastpos = 0;
071742cf 7920 unsigned int limlo, limhi;
252b5132
RH
7921 char *s_reset;
7922 char save_c = 0;
252b5132
RH
7923
7924 insn_error = NULL;
7925
7926 /* If the instruction contains a '.', we first try to match an instruction
7927 including the '.'. Then we try again without the '.'. */
7928 insn = NULL;
3882b010 7929 for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
252b5132
RH
7930 continue;
7931
7932 /* If we stopped on whitespace, then replace the whitespace with null for
7933 the call to hash_find. Save the character we replaced just in case we
7934 have to re-parse the instruction. */
3882b010 7935 if (ISSPACE (*s))
252b5132
RH
7936 {
7937 save_c = *s;
7938 *s++ = '\0';
7939 }
bdaaa2e1 7940
252b5132
RH
7941 insn = (struct mips_opcode *) hash_find (op_hash, str);
7942
7943 /* If we didn't find the instruction in the opcode table, try again, but
7944 this time with just the instruction up to, but not including the
7945 first '.'. */
7946 if (insn == NULL)
7947 {
bdaaa2e1 7948 /* Restore the character we overwrite above (if any). */
252b5132
RH
7949 if (save_c)
7950 *(--s) = save_c;
7951
7952 /* Scan up to the first '.' or whitespace. */
3882b010
L
7953 for (s = str;
7954 *s != '\0' && *s != '.' && !ISSPACE (*s);
7955 ++s)
252b5132
RH
7956 continue;
7957
7958 /* If we did not find a '.', then we can quit now. */
7959 if (*s != '.')
7960 {
7961 insn_error = "unrecognized opcode";
7962 return;
7963 }
7964
7965 /* Lookup the instruction in the hash table. */
7966 *s++ = '\0';
7967 if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
7968 {
7969 insn_error = "unrecognized opcode";
7970 return;
7971 }
252b5132
RH
7972 }
7973
7974 argsStart = s;
7975 for (;;)
7976 {
b34976b6 7977 bfd_boolean ok;
252b5132
RH
7978
7979 assert (strcmp (insn->name, str) == 0);
7980
1f25f5d3
CD
7981 if (OPCODE_IS_MEMBER (insn,
7982 (mips_opts.isa
3396de36 7983 | (file_ase_mips16 ? INSN_MIPS16 : 0)
deec1734 7984 | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
98d3f06f 7985 | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
fef14a42 7986 mips_opts.arch))
b34976b6 7987 ok = TRUE;
bdaaa2e1 7988 else
b34976b6 7989 ok = FALSE;
bdaaa2e1 7990
252b5132
RH
7991 if (insn->pinfo != INSN_MACRO)
7992 {
fef14a42 7993 if (mips_opts.arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
b34976b6 7994 ok = FALSE;
252b5132
RH
7995 }
7996
7997 if (! ok)
7998 {
7999 if (insn + 1 < &mips_opcodes[NUMOPCODES]
8000 && strcmp (insn->name, insn[1].name) == 0)
8001 {
8002 ++insn;
8003 continue;
8004 }
252b5132 8005 else
beae10d5 8006 {
268f6bed
L
8007 if (!insn_error)
8008 {
8009 static char buf[100];
fef14a42
TS
8010 sprintf (buf,
8011 _("opcode not supported on this processor: %s (%s)"),
8012 mips_cpu_info_from_arch (mips_opts.arch)->name,
8013 mips_cpu_info_from_isa (mips_opts.isa)->name);
268f6bed
L
8014 insn_error = buf;
8015 }
8016 if (save_c)
8017 *(--s) = save_c;
2bd7f1f3 8018 return;
252b5132 8019 }
252b5132
RH
8020 }
8021
8022 ip->insn_mo = insn;
8023 ip->insn_opcode = insn->match;
268f6bed 8024 insn_error = NULL;
252b5132
RH
8025 for (args = insn->args;; ++args)
8026 {
deec1734
CD
8027 int is_mdmx;
8028
ad8d3bb3 8029 s += strspn (s, " \t");
deec1734 8030 is_mdmx = 0;
252b5132
RH
8031 switch (*args)
8032 {
8033 case '\0': /* end of args */
8034 if (*s == '\0')
8035 return;
8036 break;
8037
8038 case ',':
8039 if (*s++ == *args)
8040 continue;
8041 s--;
8042 switch (*++args)
8043 {
8044 case 'r':
8045 case 'v':
38487616 8046 ip->insn_opcode |= lastregno << OP_SH_RS;
252b5132
RH
8047 continue;
8048
8049 case 'w':
38487616
TS
8050 ip->insn_opcode |= lastregno << OP_SH_RT;
8051 continue;
8052
252b5132 8053 case 'W':
38487616 8054 ip->insn_opcode |= lastregno << OP_SH_FT;
252b5132
RH
8055 continue;
8056
8057 case 'V':
38487616 8058 ip->insn_opcode |= lastregno << OP_SH_FS;
252b5132
RH
8059 continue;
8060 }
8061 break;
8062
8063 case '(':
8064 /* Handle optional base register.
8065 Either the base register is omitted or
bdaaa2e1 8066 we must have a left paren. */
252b5132
RH
8067 /* This is dependent on the next operand specifier
8068 is a base register specification. */
8069 assert (args[1] == 'b' || args[1] == '5'
8070 || args[1] == '-' || args[1] == '4');
8071 if (*s == '\0')
8072 return;
8073
8074 case ')': /* these must match exactly */
60b63b72
RS
8075 case '[':
8076 case ']':
252b5132
RH
8077 if (*s++ == *args)
8078 continue;
8079 break;
8080
af7ee8bf
CD
8081 case '+': /* Opcode extension character. */
8082 switch (*++args)
8083 {
071742cf
CD
8084 case 'A': /* ins/ext position, becomes LSB. */
8085 limlo = 0;
8086 limhi = 31;
5f74bc13
CD
8087 goto do_lsb;
8088 case 'E':
8089 limlo = 32;
8090 limhi = 63;
8091 goto do_lsb;
8092do_lsb:
071742cf
CD
8093 my_getExpression (&imm_expr, s);
8094 check_absolute_expr (ip, &imm_expr);
8095 if ((unsigned long) imm_expr.X_add_number < limlo
8096 || (unsigned long) imm_expr.X_add_number > limhi)
8097 {
8098 as_bad (_("Improper position (%lu)"),
8099 (unsigned long) imm_expr.X_add_number);
8100 imm_expr.X_add_number = limlo;
8101 }
8102 lastpos = imm_expr.X_add_number;
8103 ip->insn_opcode |= (imm_expr.X_add_number
8104 & OP_MASK_SHAMT) << OP_SH_SHAMT;
8105 imm_expr.X_op = O_absent;
8106 s = expr_end;
8107 continue;
8108
8109 case 'B': /* ins size, becomes MSB. */
8110 limlo = 1;
8111 limhi = 32;
5f74bc13
CD
8112 goto do_msb;
8113 case 'F':
8114 limlo = 33;
8115 limhi = 64;
8116 goto do_msb;
8117do_msb:
071742cf
CD
8118 my_getExpression (&imm_expr, s);
8119 check_absolute_expr (ip, &imm_expr);
8120 /* Check for negative input so that small negative numbers
8121 will not succeed incorrectly. The checks against
8122 (pos+size) transitively check "size" itself,
8123 assuming that "pos" is reasonable. */
8124 if ((long) imm_expr.X_add_number < 0
8125 || ((unsigned long) imm_expr.X_add_number
8126 + lastpos) < limlo
8127 || ((unsigned long) imm_expr.X_add_number
8128 + lastpos) > limhi)
8129 {
8130 as_bad (_("Improper insert size (%lu, position %lu)"),
8131 (unsigned long) imm_expr.X_add_number,
8132 (unsigned long) lastpos);
8133 imm_expr.X_add_number = limlo - lastpos;
8134 }
8135 ip->insn_opcode |= ((lastpos + imm_expr.X_add_number - 1)
8136 & OP_MASK_INSMSB) << OP_SH_INSMSB;
8137 imm_expr.X_op = O_absent;
8138 s = expr_end;
8139 continue;
8140
8141 case 'C': /* ext size, becomes MSBD. */
8142 limlo = 1;
8143 limhi = 32;
5f74bc13
CD
8144 goto do_msbd;
8145 case 'G':
8146 limlo = 33;
8147 limhi = 64;
8148 goto do_msbd;
8149 case 'H':
8150 limlo = 33;
8151 limhi = 64;
8152 goto do_msbd;
8153do_msbd:
071742cf
CD
8154 my_getExpression (&imm_expr, s);
8155 check_absolute_expr (ip, &imm_expr);
8156 /* Check for negative input so that small negative numbers
8157 will not succeed incorrectly. The checks against
8158 (pos+size) transitively check "size" itself,
8159 assuming that "pos" is reasonable. */
8160 if ((long) imm_expr.X_add_number < 0
8161 || ((unsigned long) imm_expr.X_add_number
8162 + lastpos) < limlo
8163 || ((unsigned long) imm_expr.X_add_number
8164 + lastpos) > limhi)
8165 {
8166 as_bad (_("Improper extract size (%lu, position %lu)"),
8167 (unsigned long) imm_expr.X_add_number,
8168 (unsigned long) lastpos);
8169 imm_expr.X_add_number = limlo - lastpos;
8170 }
8171 ip->insn_opcode |= ((imm_expr.X_add_number - 1)
8172 & OP_MASK_EXTMSBD) << OP_SH_EXTMSBD;
8173 imm_expr.X_op = O_absent;
8174 s = expr_end;
8175 continue;
af7ee8bf 8176
bbcc0807
CD
8177 case 'D':
8178 /* +D is for disassembly only; never match. */
8179 break;
8180
5f74bc13
CD
8181 case 'I':
8182 /* "+I" is like "I", except that imm2_expr is used. */
8183 my_getExpression (&imm2_expr, s);
8184 if (imm2_expr.X_op != O_big
8185 && imm2_expr.X_op != O_constant)
8186 insn_error = _("absolute expression required");
13757d0c 8187 normalize_constant_expr (&imm2_expr);
5f74bc13
CD
8188 s = expr_end;
8189 continue;
8190
af7ee8bf
CD
8191 default:
8192 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8193 *args, insn->name, insn->args);
8194 /* Further processing is fruitless. */
8195 return;
8196 }
8197 break;
8198
252b5132
RH
8199 case '<': /* must be at least one digit */
8200 /*
8201 * According to the manual, if the shift amount is greater
b6ff326e
KH
8202 * than 31 or less than 0, then the shift amount should be
8203 * mod 32. In reality the mips assembler issues an error.
252b5132
RH
8204 * We issue a warning and mask out all but the low 5 bits.
8205 */
8206 my_getExpression (&imm_expr, s);
8207 check_absolute_expr (ip, &imm_expr);
8208 if ((unsigned long) imm_expr.X_add_number > 31)
8209 {
793b27f4
TS
8210 as_warn (_("Improper shift amount (%lu)"),
8211 (unsigned long) imm_expr.X_add_number);
38487616 8212 imm_expr.X_add_number &= OP_MASK_SHAMT;
252b5132 8213 }
38487616 8214 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SHAMT;
252b5132
RH
8215 imm_expr.X_op = O_absent;
8216 s = expr_end;
8217 continue;
8218
8219 case '>': /* shift amount minus 32 */
8220 my_getExpression (&imm_expr, s);
8221 check_absolute_expr (ip, &imm_expr);
8222 if ((unsigned long) imm_expr.X_add_number < 32
8223 || (unsigned long) imm_expr.X_add_number > 63)
8224 break;
38487616 8225 ip->insn_opcode |= (imm_expr.X_add_number - 32) << OP_SH_SHAMT;
252b5132
RH
8226 imm_expr.X_op = O_absent;
8227 s = expr_end;
8228 continue;
8229
252b5132
RH
8230 case 'k': /* cache code */
8231 case 'h': /* prefx code */
8232 my_getExpression (&imm_expr, s);
8233 check_absolute_expr (ip, &imm_expr);
8234 if ((unsigned long) imm_expr.X_add_number > 31)
8235 {
8236 as_warn (_("Invalid value for `%s' (%lu)"),
8237 ip->insn_mo->name,
8238 (unsigned long) imm_expr.X_add_number);
8239 imm_expr.X_add_number &= 0x1f;
8240 }
8241 if (*args == 'k')
8242 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
8243 else
8244 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
8245 imm_expr.X_op = O_absent;
8246 s = expr_end;
8247 continue;
8248
8249 case 'c': /* break code */
8250 my_getExpression (&imm_expr, s);
8251 check_absolute_expr (ip, &imm_expr);
793b27f4 8252 if ((unsigned long) imm_expr.X_add_number > 1023)
252b5132 8253 {
793b27f4
TS
8254 as_warn (_("Illegal break code (%lu)"),
8255 (unsigned long) imm_expr.X_add_number);
38487616 8256 imm_expr.X_add_number &= OP_MASK_CODE;
252b5132 8257 }
38487616 8258 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE;
252b5132
RH
8259 imm_expr.X_op = O_absent;
8260 s = expr_end;
8261 continue;
8262
8263 case 'q': /* lower break code */
8264 my_getExpression (&imm_expr, s);
8265 check_absolute_expr (ip, &imm_expr);
793b27f4 8266 if ((unsigned long) imm_expr.X_add_number > 1023)
252b5132 8267 {
793b27f4
TS
8268 as_warn (_("Illegal lower break code (%lu)"),
8269 (unsigned long) imm_expr.X_add_number);
38487616 8270 imm_expr.X_add_number &= OP_MASK_CODE2;
252b5132 8271 }
38487616 8272 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE2;
252b5132
RH
8273 imm_expr.X_op = O_absent;
8274 s = expr_end;
8275 continue;
8276
4372b673 8277 case 'B': /* 20-bit syscall/break code. */
156c2f8b 8278 my_getExpression (&imm_expr, s);
156c2f8b 8279 check_absolute_expr (ip, &imm_expr);
793b27f4
TS
8280 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
8281 as_warn (_("Illegal 20-bit code (%lu)"),
8282 (unsigned long) imm_expr.X_add_number);
38487616 8283 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE20;
252b5132
RH
8284 imm_expr.X_op = O_absent;
8285 s = expr_end;
8286 continue;
8287
98d3f06f 8288 case 'C': /* Coprocessor code */
beae10d5 8289 my_getExpression (&imm_expr, s);
252b5132 8290 check_absolute_expr (ip, &imm_expr);
98d3f06f 8291 if ((unsigned long) imm_expr.X_add_number >= (1 << 25))
252b5132 8292 {
793b27f4
TS
8293 as_warn (_("Coproccesor code > 25 bits (%lu)"),
8294 (unsigned long) imm_expr.X_add_number);
98d3f06f 8295 imm_expr.X_add_number &= ((1 << 25) - 1);
252b5132 8296 }
beae10d5
KH
8297 ip->insn_opcode |= imm_expr.X_add_number;
8298 imm_expr.X_op = O_absent;
8299 s = expr_end;
8300 continue;
252b5132 8301
4372b673
NC
8302 case 'J': /* 19-bit wait code. */
8303 my_getExpression (&imm_expr, s);
8304 check_absolute_expr (ip, &imm_expr);
793b27f4
TS
8305 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
8306 as_warn (_("Illegal 19-bit code (%lu)"),
8307 (unsigned long) imm_expr.X_add_number);
38487616 8308 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE19;
4372b673
NC
8309 imm_expr.X_op = O_absent;
8310 s = expr_end;
8311 continue;
8312
252b5132 8313 case 'P': /* Performance register */
beae10d5 8314 my_getExpression (&imm_expr, s);
252b5132 8315 check_absolute_expr (ip, &imm_expr);
beae10d5 8316 if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
252b5132 8317 {
793b27f4
TS
8318 as_warn (_("Invalid performance register (%lu)"),
8319 (unsigned long) imm_expr.X_add_number);
38487616 8320 imm_expr.X_add_number &= OP_MASK_PERFREG;
252b5132 8321 }
38487616 8322 ip->insn_opcode |= (imm_expr.X_add_number << OP_SH_PERFREG);
beae10d5
KH
8323 imm_expr.X_op = O_absent;
8324 s = expr_end;
8325 continue;
252b5132
RH
8326
8327 case 'b': /* base register */
8328 case 'd': /* destination register */
8329 case 's': /* source register */
8330 case 't': /* target register */
8331 case 'r': /* both target and source */
8332 case 'v': /* both dest and source */
8333 case 'w': /* both dest and target */
8334 case 'E': /* coprocessor target register */
8335 case 'G': /* coprocessor destination register */
af7ee8bf 8336 case 'K': /* 'rdhwr' destination register */
252b5132
RH
8337 case 'x': /* ignore register name */
8338 case 'z': /* must be zero register */
4372b673 8339 case 'U': /* destination register (clo/clz). */
252b5132
RH
8340 s_reset = s;
8341 if (s[0] == '$')
8342 {
8343
3882b010 8344 if (ISDIGIT (s[1]))
252b5132
RH
8345 {
8346 ++s;
8347 regno = 0;
8348 do
8349 {
8350 regno *= 10;
8351 regno += *s - '0';
8352 ++s;
8353 }
3882b010 8354 while (ISDIGIT (*s));
252b5132
RH
8355 if (regno > 31)
8356 as_bad (_("Invalid register number (%d)"), regno);
8357 }
af7ee8bf 8358 else if (*args == 'E' || *args == 'G' || *args == 'K')
252b5132
RH
8359 goto notreg;
8360 else
8361 {
76db943d
TS
8362 if (s[1] == 'r' && s[2] == 'a')
8363 {
8364 s += 3;
8365 regno = RA;
8366 }
8367 else if (s[1] == 'f' && s[2] == 'p')
252b5132
RH
8368 {
8369 s += 3;
8370 regno = FP;
8371 }
8372 else if (s[1] == 's' && s[2] == 'p')
8373 {
8374 s += 3;
8375 regno = SP;
8376 }
8377 else if (s[1] == 'g' && s[2] == 'p')
8378 {
8379 s += 3;
8380 regno = GP;
8381 }
8382 else if (s[1] == 'a' && s[2] == 't')
8383 {
8384 s += 3;
8385 regno = AT;
8386 }
8387 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8388 {
8389 s += 4;
8390 regno = KT0;
8391 }
8392 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8393 {
8394 s += 4;
8395 regno = KT1;
8396 }
85b51719
TS
8397 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
8398 {
8399 s += 5;
8400 regno = ZERO;
8401 }
252b5132
RH
8402 else if (itbl_have_entries)
8403 {
8404 char *p, *n;
d7ba4a77 8405 unsigned long r;
252b5132 8406
d7ba4a77 8407 p = s + 1; /* advance past '$' */
252b5132
RH
8408 n = itbl_get_field (&p); /* n is name */
8409
d7ba4a77
ILT
8410 /* See if this is a register defined in an
8411 itbl entry. */
8412 if (itbl_get_reg_val (n, &r))
252b5132
RH
8413 {
8414 /* Get_field advances to the start of
8415 the next field, so we need to back
d7ba4a77 8416 rack to the end of the last field. */
bdaaa2e1 8417 if (p)
252b5132 8418 s = p - 1;
bdaaa2e1 8419 else
d7ba4a77 8420 s = strchr (s, '\0');
252b5132
RH
8421 regno = r;
8422 }
8423 else
8424 goto notreg;
beae10d5 8425 }
252b5132
RH
8426 else
8427 goto notreg;
8428 }
8429 if (regno == AT
8430 && ! mips_opts.noat
8431 && *args != 'E'
af7ee8bf
CD
8432 && *args != 'G'
8433 && *args != 'K')
252b5132
RH
8434 as_warn (_("Used $at without \".set noat\""));
8435 c = *args;
8436 if (*s == ' ')
f9419b05 8437 ++s;
252b5132
RH
8438 if (args[1] != *s)
8439 {
8440 if (c == 'r' || c == 'v' || c == 'w')
8441 {
8442 regno = lastregno;
8443 s = s_reset;
f9419b05 8444 ++args;
252b5132
RH
8445 }
8446 }
8447 /* 'z' only matches $0. */
8448 if (c == 'z' && regno != 0)
8449 break;
8450
bdaaa2e1
KH
8451 /* Now that we have assembled one operand, we use the args string
8452 * to figure out where it goes in the instruction. */
252b5132
RH
8453 switch (c)
8454 {
8455 case 'r':
8456 case 's':
8457 case 'v':
8458 case 'b':
38487616 8459 ip->insn_opcode |= regno << OP_SH_RS;
252b5132
RH
8460 break;
8461 case 'd':
8462 case 'G':
af7ee8bf 8463 case 'K':
38487616 8464 ip->insn_opcode |= regno << OP_SH_RD;
252b5132 8465 break;
4372b673 8466 case 'U':
38487616
TS
8467 ip->insn_opcode |= regno << OP_SH_RD;
8468 ip->insn_opcode |= regno << OP_SH_RT;
4372b673 8469 break;
252b5132
RH
8470 case 'w':
8471 case 't':
8472 case 'E':
38487616 8473 ip->insn_opcode |= regno << OP_SH_RT;
252b5132
RH
8474 break;
8475 case 'x':
8476 /* This case exists because on the r3000 trunc
8477 expands into a macro which requires a gp
8478 register. On the r6000 or r4000 it is
8479 assembled into a single instruction which
8480 ignores the register. Thus the insn version
8481 is MIPS_ISA2 and uses 'x', and the macro
8482 version is MIPS_ISA1 and uses 't'. */
8483 break;
8484 case 'z':
8485 /* This case is for the div instruction, which
8486 acts differently if the destination argument
8487 is $0. This only matches $0, and is checked
8488 outside the switch. */
8489 break;
8490 case 'D':
8491 /* Itbl operand; not yet implemented. FIXME ?? */
8492 break;
8493 /* What about all other operands like 'i', which
8494 can be specified in the opcode table? */
8495 }
8496 lastregno = regno;
8497 continue;
8498 }
8499 notreg:
8500 switch (*args++)
8501 {
8502 case 'r':
8503 case 'v':
38487616 8504 ip->insn_opcode |= lastregno << OP_SH_RS;
252b5132
RH
8505 continue;
8506 case 'w':
38487616 8507 ip->insn_opcode |= lastregno << OP_SH_RT;
252b5132
RH
8508 continue;
8509 }
8510 break;
8511
deec1734
CD
8512 case 'O': /* MDMX alignment immediate constant. */
8513 my_getExpression (&imm_expr, s);
8514 check_absolute_expr (ip, &imm_expr);
8515 if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
8516 {
8517 as_warn ("Improper align amount (%ld), using low bits",
8518 (long) imm_expr.X_add_number);
8519 imm_expr.X_add_number &= OP_MASK_ALN;
8520 }
8521 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_ALN;
8522 imm_expr.X_op = O_absent;
8523 s = expr_end;
8524 continue;
8525
8526 case 'Q': /* MDMX vector, element sel, or const. */
8527 if (s[0] != '$')
8528 {
8529 /* MDMX Immediate. */
8530 my_getExpression (&imm_expr, s);
8531 check_absolute_expr (ip, &imm_expr);
8532 if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
8533 {
8534 as_warn (_("Invalid MDMX Immediate (%ld)"),
8535 (long) imm_expr.X_add_number);
8536 imm_expr.X_add_number &= OP_MASK_FT;
8537 }
8538 imm_expr.X_add_number &= OP_MASK_FT;
8539 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8540 ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
8541 else
8542 ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
8543 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_FT;
8544 imm_expr.X_op = O_absent;
8545 s = expr_end;
8546 continue;
8547 }
8548 /* Not MDMX Immediate. Fall through. */
8549 case 'X': /* MDMX destination register. */
8550 case 'Y': /* MDMX source register. */
8551 case 'Z': /* MDMX target register. */
8552 is_mdmx = 1;
252b5132
RH
8553 case 'D': /* floating point destination register */
8554 case 'S': /* floating point source register */
8555 case 'T': /* floating point target register */
8556 case 'R': /* floating point source register */
8557 case 'V':
8558 case 'W':
8559 s_reset = s;
deec1734
CD
8560 /* Accept $fN for FP and MDMX register numbers, and in
8561 addition accept $vN for MDMX register numbers. */
8562 if ((s[0] == '$' && s[1] == 'f' && ISDIGIT (s[2]))
8563 || (is_mdmx != 0 && s[0] == '$' && s[1] == 'v'
8564 && ISDIGIT (s[2])))
252b5132
RH
8565 {
8566 s += 2;
8567 regno = 0;
8568 do
8569 {
8570 regno *= 10;
8571 regno += *s - '0';
8572 ++s;
8573 }
3882b010 8574 while (ISDIGIT (*s));
252b5132
RH
8575
8576 if (regno > 31)
8577 as_bad (_("Invalid float register number (%d)"), regno);
8578
8579 if ((regno & 1) != 0
ca4e0257 8580 && HAVE_32BIT_FPRS
252b5132
RH
8581 && ! (strcmp (str, "mtc1") == 0
8582 || strcmp (str, "mfc1") == 0
8583 || strcmp (str, "lwc1") == 0
8584 || strcmp (str, "swc1") == 0
8585 || strcmp (str, "l.s") == 0
8586 || strcmp (str, "s.s") == 0))
8587 as_warn (_("Float register should be even, was %d"),
8588 regno);
8589
8590 c = *args;
8591 if (*s == ' ')
f9419b05 8592 ++s;
252b5132
RH
8593 if (args[1] != *s)
8594 {
8595 if (c == 'V' || c == 'W')
8596 {
8597 regno = lastregno;
8598 s = s_reset;
f9419b05 8599 ++args;
252b5132
RH
8600 }
8601 }
8602 switch (c)
8603 {
8604 case 'D':
deec1734 8605 case 'X':
38487616 8606 ip->insn_opcode |= regno << OP_SH_FD;
252b5132
RH
8607 break;
8608 case 'V':
8609 case 'S':
deec1734 8610 case 'Y':
38487616 8611 ip->insn_opcode |= regno << OP_SH_FS;
252b5132 8612 break;
deec1734
CD
8613 case 'Q':
8614 /* This is like 'Z', but also needs to fix the MDMX
8615 vector/scalar select bits. Note that the
8616 scalar immediate case is handled above. */
8617 if (*s == '[')
8618 {
8619 int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
8620 int max_el = (is_qh ? 3 : 7);
8621 s++;
8622 my_getExpression(&imm_expr, s);
8623 check_absolute_expr (ip, &imm_expr);
8624 s = expr_end;
8625 if (imm_expr.X_add_number > max_el)
8626 as_bad(_("Bad element selector %ld"),
8627 (long) imm_expr.X_add_number);
8628 imm_expr.X_add_number &= max_el;
8629 ip->insn_opcode |= (imm_expr.X_add_number
8630 << (OP_SH_VSEL +
8631 (is_qh ? 2 : 1)));
01a3f561 8632 imm_expr.X_op = O_absent;
deec1734
CD
8633 if (*s != ']')
8634 as_warn(_("Expecting ']' found '%s'"), s);
8635 else
8636 s++;
8637 }
8638 else
8639 {
8640 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8641 ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
8642 << OP_SH_VSEL);
8643 else
8644 ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
8645 OP_SH_VSEL);
8646 }
8647 /* Fall through */
252b5132
RH
8648 case 'W':
8649 case 'T':
deec1734 8650 case 'Z':
38487616 8651 ip->insn_opcode |= regno << OP_SH_FT;
252b5132
RH
8652 break;
8653 case 'R':
38487616 8654 ip->insn_opcode |= regno << OP_SH_FR;
252b5132
RH
8655 break;
8656 }
8657 lastregno = regno;
8658 continue;
8659 }
8660
252b5132
RH
8661 switch (*args++)
8662 {
8663 case 'V':
38487616 8664 ip->insn_opcode |= lastregno << OP_SH_FS;
252b5132
RH
8665 continue;
8666 case 'W':
38487616 8667 ip->insn_opcode |= lastregno << OP_SH_FT;
252b5132
RH
8668 continue;
8669 }
8670 break;
8671
8672 case 'I':
8673 my_getExpression (&imm_expr, s);
8674 if (imm_expr.X_op != O_big
8675 && imm_expr.X_op != O_constant)
8676 insn_error = _("absolute expression required");
13757d0c 8677 normalize_constant_expr (&imm_expr);
252b5132
RH
8678 s = expr_end;
8679 continue;
8680
8681 case 'A':
8682 my_getExpression (&offset_expr, s);
f6688943 8683 *imm_reloc = BFD_RELOC_32;
252b5132
RH
8684 s = expr_end;
8685 continue;
8686
8687 case 'F':
8688 case 'L':
8689 case 'f':
8690 case 'l':
8691 {
8692 int f64;
ca4e0257 8693 int using_gprs;
252b5132
RH
8694 char *save_in;
8695 char *err;
8696 unsigned char temp[8];
8697 int len;
8698 unsigned int length;
8699 segT seg;
8700 subsegT subseg;
8701 char *p;
8702
8703 /* These only appear as the last operand in an
8704 instruction, and every instruction that accepts
8705 them in any variant accepts them in all variants.
8706 This means we don't have to worry about backing out
8707 any changes if the instruction does not match.
8708
8709 The difference between them is the size of the
8710 floating point constant and where it goes. For 'F'
8711 and 'L' the constant is 64 bits; for 'f' and 'l' it
8712 is 32 bits. Where the constant is placed is based
8713 on how the MIPS assembler does things:
8714 F -- .rdata
8715 L -- .lit8
8716 f -- immediate value
8717 l -- .lit4
8718
8719 The .lit4 and .lit8 sections are only used if
8720 permitted by the -G argument.
8721
ca4e0257
RS
8722 The code below needs to know whether the target register
8723 is 32 or 64 bits wide. It relies on the fact 'f' and
8724 'F' are used with GPR-based instructions and 'l' and
8725 'L' are used with FPR-based instructions. */
252b5132
RH
8726
8727 f64 = *args == 'F' || *args == 'L';
ca4e0257 8728 using_gprs = *args == 'F' || *args == 'f';
252b5132
RH
8729
8730 save_in = input_line_pointer;
8731 input_line_pointer = s;
8732 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
8733 length = len;
8734 s = input_line_pointer;
8735 input_line_pointer = save_in;
8736 if (err != NULL && *err != '\0')
8737 {
8738 as_bad (_("Bad floating point constant: %s"), err);
8739 memset (temp, '\0', sizeof temp);
8740 length = f64 ? 8 : 4;
8741 }
8742
156c2f8b 8743 assert (length == (unsigned) (f64 ? 8 : 4));
252b5132
RH
8744
8745 if (*args == 'f'
8746 || (*args == 'l'
3e722fb5 8747 && (g_switch_value < 4
252b5132
RH
8748 || (temp[0] == 0 && temp[1] == 0)
8749 || (temp[2] == 0 && temp[3] == 0))))
8750 {
8751 imm_expr.X_op = O_constant;
8752 if (! target_big_endian)
8753 imm_expr.X_add_number = bfd_getl32 (temp);
8754 else
8755 imm_expr.X_add_number = bfd_getb32 (temp);
8756 }
8757 else if (length > 4
119d663a 8758 && ! mips_disable_float_construction
ca4e0257
RS
8759 /* Constants can only be constructed in GPRs and
8760 copied to FPRs if the GPRs are at least as wide
8761 as the FPRs. Force the constant into memory if
8762 we are using 64-bit FPRs but the GPRs are only
8763 32 bits wide. */
8764 && (using_gprs
8765 || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
252b5132
RH
8766 && ((temp[0] == 0 && temp[1] == 0)
8767 || (temp[2] == 0 && temp[3] == 0))
8768 && ((temp[4] == 0 && temp[5] == 0)
8769 || (temp[6] == 0 && temp[7] == 0)))
8770 {
ca4e0257
RS
8771 /* The value is simple enough to load with a couple of
8772 instructions. If using 32-bit registers, set
8773 imm_expr to the high order 32 bits and offset_expr to
8774 the low order 32 bits. Otherwise, set imm_expr to
8775 the entire 64 bit constant. */
8776 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
252b5132
RH
8777 {
8778 imm_expr.X_op = O_constant;
8779 offset_expr.X_op = O_constant;
8780 if (! target_big_endian)
8781 {
8782 imm_expr.X_add_number = bfd_getl32 (temp + 4);
8783 offset_expr.X_add_number = bfd_getl32 (temp);
8784 }
8785 else
8786 {
8787 imm_expr.X_add_number = bfd_getb32 (temp);
8788 offset_expr.X_add_number = bfd_getb32 (temp + 4);
8789 }
8790 if (offset_expr.X_add_number == 0)
8791 offset_expr.X_op = O_absent;
8792 }
8793 else if (sizeof (imm_expr.X_add_number) > 4)
8794 {
8795 imm_expr.X_op = O_constant;
8796 if (! target_big_endian)
8797 imm_expr.X_add_number = bfd_getl64 (temp);
8798 else
8799 imm_expr.X_add_number = bfd_getb64 (temp);
8800 }
8801 else
8802 {
8803 imm_expr.X_op = O_big;
8804 imm_expr.X_add_number = 4;
8805 if (! target_big_endian)
8806 {
8807 generic_bignum[0] = bfd_getl16 (temp);
8808 generic_bignum[1] = bfd_getl16 (temp + 2);
8809 generic_bignum[2] = bfd_getl16 (temp + 4);
8810 generic_bignum[3] = bfd_getl16 (temp + 6);
8811 }
8812 else
8813 {
8814 generic_bignum[0] = bfd_getb16 (temp + 6);
8815 generic_bignum[1] = bfd_getb16 (temp + 4);
8816 generic_bignum[2] = bfd_getb16 (temp + 2);
8817 generic_bignum[3] = bfd_getb16 (temp);
8818 }
8819 }
8820 }
8821 else
8822 {
8823 const char *newname;
8824 segT new_seg;
8825
8826 /* Switch to the right section. */
8827 seg = now_seg;
8828 subseg = now_subseg;
8829 switch (*args)
8830 {
8831 default: /* unused default case avoids warnings. */
8832 case 'L':
8833 newname = RDATA_SECTION_NAME;
3e722fb5 8834 if (g_switch_value >= 8)
252b5132
RH
8835 newname = ".lit8";
8836 break;
8837 case 'F':
3e722fb5 8838 newname = RDATA_SECTION_NAME;
252b5132
RH
8839 break;
8840 case 'l':
4d0d148d 8841 assert (g_switch_value >= 4);
252b5132
RH
8842 newname = ".lit4";
8843 break;
8844 }
8845 new_seg = subseg_new (newname, (subsegT) 0);
8846 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
8847 bfd_set_section_flags (stdoutput, new_seg,
8848 (SEC_ALLOC
8849 | SEC_LOAD
8850 | SEC_READONLY
8851 | SEC_DATA));
8852 frag_align (*args == 'l' ? 2 : 3, 0, 0);
8853 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
8854 && strcmp (TARGET_OS, "elf") != 0)
8855 record_alignment (new_seg, 4);
8856 else
8857 record_alignment (new_seg, *args == 'l' ? 2 : 3);
8858 if (seg == now_seg)
8859 as_bad (_("Can't use floating point insn in this section"));
8860
8861 /* Set the argument to the current address in the
8862 section. */
8863 offset_expr.X_op = O_symbol;
8864 offset_expr.X_add_symbol =
8865 symbol_new ("L0\001", now_seg,
8866 (valueT) frag_now_fix (), frag_now);
8867 offset_expr.X_add_number = 0;
8868
8869 /* Put the floating point number into the section. */
8870 p = frag_more ((int) length);
8871 memcpy (p, temp, length);
8872
8873 /* Switch back to the original section. */
8874 subseg_set (seg, subseg);
8875 }
8876 }
8877 continue;
8878
8879 case 'i': /* 16 bit unsigned immediate */
8880 case 'j': /* 16 bit signed immediate */
f6688943 8881 *imm_reloc = BFD_RELOC_LO16;
5e0116d5 8882 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0)
252b5132
RH
8883 {
8884 int more;
5e0116d5
RS
8885 offsetT minval, maxval;
8886
8887 more = (insn + 1 < &mips_opcodes[NUMOPCODES]
8888 && strcmp (insn->name, insn[1].name) == 0);
8889
8890 /* If the expression was written as an unsigned number,
8891 only treat it as signed if there are no more
8892 alternatives. */
8893 if (more
8894 && *args == 'j'
8895 && sizeof (imm_expr.X_add_number) <= 4
8896 && imm_expr.X_op == O_constant
8897 && imm_expr.X_add_number < 0
8898 && imm_expr.X_unsigned
8899 && HAVE_64BIT_GPRS)
8900 break;
8901
8902 /* For compatibility with older assemblers, we accept
8903 0x8000-0xffff as signed 16-bit numbers when only
8904 signed numbers are allowed. */
8905 if (*args == 'i')
8906 minval = 0, maxval = 0xffff;
8907 else if (more)
8908 minval = -0x8000, maxval = 0x7fff;
252b5132 8909 else
5e0116d5
RS
8910 minval = -0x8000, maxval = 0xffff;
8911
8912 if (imm_expr.X_op != O_constant
8913 || imm_expr.X_add_number < minval
8914 || imm_expr.X_add_number > maxval)
252b5132
RH
8915 {
8916 if (more)
8917 break;
2ae7e77b
AH
8918 if (imm_expr.X_op == O_constant
8919 || imm_expr.X_op == O_big)
5e0116d5 8920 as_bad (_("expression out of range"));
252b5132
RH
8921 }
8922 }
8923 s = expr_end;
8924 continue;
8925
8926 case 'o': /* 16 bit offset */
5e0116d5
RS
8927 /* Check whether there is only a single bracketed expression
8928 left. If so, it must be the base register and the
8929 constant must be zero. */
8930 if (*s == '(' && strchr (s + 1, '(') == 0)
8931 {
8932 offset_expr.X_op = O_constant;
8933 offset_expr.X_add_number = 0;
8934 continue;
8935 }
252b5132
RH
8936
8937 /* If this value won't fit into a 16 bit offset, then go
8938 find a macro that will generate the 32 bit offset
afdbd6d0 8939 code pattern. */
5e0116d5 8940 if (my_getSmallExpression (&offset_expr, offset_reloc, s) == 0
252b5132
RH
8941 && (offset_expr.X_op != O_constant
8942 || offset_expr.X_add_number >= 0x8000
afdbd6d0 8943 || offset_expr.X_add_number < -0x8000))
252b5132
RH
8944 break;
8945
252b5132
RH
8946 s = expr_end;
8947 continue;
8948
8949 case 'p': /* pc relative offset */
0b25d3e6 8950 *offset_reloc = BFD_RELOC_16_PCREL_S2;
252b5132
RH
8951 my_getExpression (&offset_expr, s);
8952 s = expr_end;
8953 continue;
8954
8955 case 'u': /* upper 16 bits */
5e0116d5
RS
8956 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0
8957 && imm_expr.X_op == O_constant
8958 && (imm_expr.X_add_number < 0
8959 || imm_expr.X_add_number >= 0x10000))
252b5132
RH
8960 as_bad (_("lui expression not in range 0..65535"));
8961 s = expr_end;
8962 continue;
8963
8964 case 'a': /* 26 bit address */
8965 my_getExpression (&offset_expr, s);
8966 s = expr_end;
f6688943 8967 *offset_reloc = BFD_RELOC_MIPS_JMP;
252b5132
RH
8968 continue;
8969
8970 case 'N': /* 3 bit branch condition code */
8971 case 'M': /* 3 bit compare condition code */
8972 if (strncmp (s, "$fcc", 4) != 0)
8973 break;
8974 s += 4;
8975 regno = 0;
8976 do
8977 {
8978 regno *= 10;
8979 regno += *s - '0';
8980 ++s;
8981 }
3882b010 8982 while (ISDIGIT (*s));
252b5132 8983 if (regno > 7)
30c378fd
CD
8984 as_bad (_("Invalid condition code register $fcc%d"), regno);
8985 if ((strcmp(str + strlen(str) - 3, ".ps") == 0
8986 || strcmp(str + strlen(str) - 5, "any2f") == 0
8987 || strcmp(str + strlen(str) - 5, "any2t") == 0)
8988 && (regno & 1) != 0)
8989 as_warn(_("Condition code register should be even for %s, was %d"),
8990 str, regno);
8991 if ((strcmp(str + strlen(str) - 5, "any4f") == 0
8992 || strcmp(str + strlen(str) - 5, "any4t") == 0)
8993 && (regno & 3) != 0)
8994 as_warn(_("Condition code register should be 0 or 4 for %s, was %d"),
8995 str, regno);
252b5132
RH
8996 if (*args == 'N')
8997 ip->insn_opcode |= regno << OP_SH_BCC;
8998 else
8999 ip->insn_opcode |= regno << OP_SH_CCC;
beae10d5 9000 continue;
252b5132 9001
156c2f8b
NC
9002 case 'H':
9003 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
9004 s += 2;
3882b010 9005 if (ISDIGIT (*s))
156c2f8b
NC
9006 {
9007 c = 0;
9008 do
9009 {
9010 c *= 10;
9011 c += *s - '0';
9012 ++s;
9013 }
3882b010 9014 while (ISDIGIT (*s));
156c2f8b
NC
9015 }
9016 else
9017 c = 8; /* Invalid sel value. */
9018
9019 if (c > 7)
9020 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
9021 ip->insn_opcode |= c;
9022 continue;
9023
60b63b72
RS
9024 case 'e':
9025 /* Must be at least one digit. */
9026 my_getExpression (&imm_expr, s);
9027 check_absolute_expr (ip, &imm_expr);
9028
9029 if ((unsigned long) imm_expr.X_add_number
9030 > (unsigned long) OP_MASK_VECBYTE)
9031 {
9032 as_bad (_("bad byte vector index (%ld)"),
9033 (long) imm_expr.X_add_number);
9034 imm_expr.X_add_number = 0;
9035 }
9036
9037 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECBYTE;
9038 imm_expr.X_op = O_absent;
9039 s = expr_end;
9040 continue;
9041
9042 case '%':
9043 my_getExpression (&imm_expr, s);
9044 check_absolute_expr (ip, &imm_expr);
9045
9046 if ((unsigned long) imm_expr.X_add_number
9047 > (unsigned long) OP_MASK_VECALIGN)
9048 {
9049 as_bad (_("bad byte vector index (%ld)"),
9050 (long) imm_expr.X_add_number);
9051 imm_expr.X_add_number = 0;
9052 }
9053
9054 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECALIGN;
9055 imm_expr.X_op = O_absent;
9056 s = expr_end;
9057 continue;
9058
252b5132
RH
9059 default:
9060 as_bad (_("bad char = '%c'\n"), *args);
9061 internalError ();
9062 }
9063 break;
9064 }
9065 /* Args don't match. */
9066 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
9067 !strcmp (insn->name, insn[1].name))
9068 {
9069 ++insn;
9070 s = argsStart;
268f6bed 9071 insn_error = _("illegal operands");
252b5132
RH
9072 continue;
9073 }
268f6bed
L
9074 if (save_c)
9075 *(--s) = save_c;
252b5132
RH
9076 insn_error = _("illegal operands");
9077 return;
9078 }
9079}
9080
9081/* This routine assembles an instruction into its binary format when
9082 assembling for the mips16. As a side effect, it sets one of the
9083 global variables imm_reloc or offset_reloc to the type of
9084 relocation to do if one of the operands is an address expression.
9085 It also sets mips16_small and mips16_ext if the user explicitly
9086 requested a small or extended instruction. */
9087
9088static void
17a2f251 9089mips16_ip (char *str, struct mips_cl_insn *ip)
252b5132
RH
9090{
9091 char *s;
9092 const char *args;
9093 struct mips_opcode *insn;
9094 char *argsstart;
9095 unsigned int regno;
9096 unsigned int lastregno = 0;
9097 char *s_reset;
9098
9099 insn_error = NULL;
9100
b34976b6
AM
9101 mips16_small = FALSE;
9102 mips16_ext = FALSE;
252b5132 9103
3882b010 9104 for (s = str; ISLOWER (*s); ++s)
252b5132
RH
9105 ;
9106 switch (*s)
9107 {
9108 case '\0':
9109 break;
9110
9111 case ' ':
9112 *s++ = '\0';
9113 break;
9114
9115 case '.':
9116 if (s[1] == 't' && s[2] == ' ')
9117 {
9118 *s = '\0';
b34976b6 9119 mips16_small = TRUE;
252b5132
RH
9120 s += 3;
9121 break;
9122 }
9123 else if (s[1] == 'e' && s[2] == ' ')
9124 {
9125 *s = '\0';
b34976b6 9126 mips16_ext = TRUE;
252b5132
RH
9127 s += 3;
9128 break;
9129 }
9130 /* Fall through. */
9131 default:
9132 insn_error = _("unknown opcode");
9133 return;
9134 }
9135
9136 if (mips_opts.noautoextend && ! mips16_ext)
b34976b6 9137 mips16_small = TRUE;
252b5132
RH
9138
9139 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
9140 {
9141 insn_error = _("unrecognized opcode");
9142 return;
9143 }
9144
9145 argsstart = s;
9146 for (;;)
9147 {
9148 assert (strcmp (insn->name, str) == 0);
9149
9150 ip->insn_mo = insn;
9151 ip->insn_opcode = insn->match;
b34976b6 9152 ip->use_extend = FALSE;
252b5132 9153 imm_expr.X_op = O_absent;
f6688943
TS
9154 imm_reloc[0] = BFD_RELOC_UNUSED;
9155 imm_reloc[1] = BFD_RELOC_UNUSED;
9156 imm_reloc[2] = BFD_RELOC_UNUSED;
5f74bc13 9157 imm2_expr.X_op = O_absent;
252b5132 9158 offset_expr.X_op = O_absent;
f6688943
TS
9159 offset_reloc[0] = BFD_RELOC_UNUSED;
9160 offset_reloc[1] = BFD_RELOC_UNUSED;
9161 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
9162 for (args = insn->args; 1; ++args)
9163 {
9164 int c;
9165
9166 if (*s == ' ')
9167 ++s;
9168
9169 /* In this switch statement we call break if we did not find
9170 a match, continue if we did find a match, or return if we
9171 are done. */
9172
9173 c = *args;
9174 switch (c)
9175 {
9176 case '\0':
9177 if (*s == '\0')
9178 {
9179 /* Stuff the immediate value in now, if we can. */
9180 if (imm_expr.X_op == O_constant
f6688943 9181 && *imm_reloc > BFD_RELOC_UNUSED
252b5132
RH
9182 && insn->pinfo != INSN_MACRO)
9183 {
c4e7957c 9184 mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
b34976b6 9185 imm_expr.X_add_number, TRUE, mips16_small,
252b5132
RH
9186 mips16_ext, &ip->insn_opcode,
9187 &ip->use_extend, &ip->extend);
9188 imm_expr.X_op = O_absent;
f6688943 9189 *imm_reloc = BFD_RELOC_UNUSED;
252b5132
RH
9190 }
9191
9192 return;
9193 }
9194 break;
9195
9196 case ',':
9197 if (*s++ == c)
9198 continue;
9199 s--;
9200 switch (*++args)
9201 {
9202 case 'v':
9203 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9204 continue;
9205 case 'w':
9206 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9207 continue;
9208 }
9209 break;
9210
9211 case '(':
9212 case ')':
9213 if (*s++ == c)
9214 continue;
9215 break;
9216
9217 case 'v':
9218 case 'w':
9219 if (s[0] != '$')
9220 {
9221 if (c == 'v')
9222 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9223 else
9224 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9225 ++args;
9226 continue;
9227 }
9228 /* Fall through. */
9229 case 'x':
9230 case 'y':
9231 case 'z':
9232 case 'Z':
9233 case '0':
9234 case 'S':
9235 case 'R':
9236 case 'X':
9237 case 'Y':
9238 if (s[0] != '$')
9239 break;
9240 s_reset = s;
3882b010 9241 if (ISDIGIT (s[1]))
252b5132
RH
9242 {
9243 ++s;
9244 regno = 0;
9245 do
9246 {
9247 regno *= 10;
9248 regno += *s - '0';
9249 ++s;
9250 }
3882b010 9251 while (ISDIGIT (*s));
252b5132
RH
9252 if (regno > 31)
9253 {
9254 as_bad (_("invalid register number (%d)"), regno);
9255 regno = 2;
9256 }
9257 }
9258 else
9259 {
76db943d
TS
9260 if (s[1] == 'r' && s[2] == 'a')
9261 {
9262 s += 3;
9263 regno = RA;
9264 }
9265 else if (s[1] == 'f' && s[2] == 'p')
252b5132
RH
9266 {
9267 s += 3;
9268 regno = FP;
9269 }
9270 else if (s[1] == 's' && s[2] == 'p')
9271 {
9272 s += 3;
9273 regno = SP;
9274 }
9275 else if (s[1] == 'g' && s[2] == 'p')
9276 {
9277 s += 3;
9278 regno = GP;
9279 }
9280 else if (s[1] == 'a' && s[2] == 't')
9281 {
9282 s += 3;
9283 regno = AT;
9284 }
9285 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
9286 {
9287 s += 4;
9288 regno = KT0;
9289 }
9290 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
9291 {
9292 s += 4;
9293 regno = KT1;
9294 }
85b51719
TS
9295 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
9296 {
9297 s += 5;
9298 regno = ZERO;
9299 }
252b5132
RH
9300 else
9301 break;
9302 }
9303
9304 if (*s == ' ')
9305 ++s;
9306 if (args[1] != *s)
9307 {
9308 if (c == 'v' || c == 'w')
9309 {
9310 regno = mips16_to_32_reg_map[lastregno];
9311 s = s_reset;
f9419b05 9312 ++args;
252b5132
RH
9313 }
9314 }
9315
9316 switch (c)
9317 {
9318 case 'x':
9319 case 'y':
9320 case 'z':
9321 case 'v':
9322 case 'w':
9323 case 'Z':
9324 regno = mips32_to_16_reg_map[regno];
9325 break;
9326
9327 case '0':
9328 if (regno != 0)
9329 regno = ILLEGAL_REG;
9330 break;
9331
9332 case 'S':
9333 if (regno != SP)
9334 regno = ILLEGAL_REG;
9335 break;
9336
9337 case 'R':
9338 if (regno != RA)
9339 regno = ILLEGAL_REG;
9340 break;
9341
9342 case 'X':
9343 case 'Y':
9344 if (regno == AT && ! mips_opts.noat)
9345 as_warn (_("used $at without \".set noat\""));
9346 break;
9347
9348 default:
9349 internalError ();
9350 }
9351
9352 if (regno == ILLEGAL_REG)
9353 break;
9354
9355 switch (c)
9356 {
9357 case 'x':
9358 case 'v':
9359 ip->insn_opcode |= regno << MIPS16OP_SH_RX;
9360 break;
9361 case 'y':
9362 case 'w':
9363 ip->insn_opcode |= regno << MIPS16OP_SH_RY;
9364 break;
9365 case 'z':
9366 ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
9367 break;
9368 case 'Z':
9369 ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
9370 case '0':
9371 case 'S':
9372 case 'R':
9373 break;
9374 case 'X':
9375 ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
9376 break;
9377 case 'Y':
9378 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
9379 ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
9380 break;
9381 default:
9382 internalError ();
9383 }
9384
9385 lastregno = regno;
9386 continue;
9387
9388 case 'P':
9389 if (strncmp (s, "$pc", 3) == 0)
9390 {
9391 s += 3;
9392 continue;
9393 }
9394 break;
9395
9396 case '<':
9397 case '>':
9398 case '[':
9399 case ']':
9400 case '4':
9401 case '5':
9402 case 'H':
9403 case 'W':
9404 case 'D':
9405 case 'j':
9406 case '8':
9407 case 'V':
9408 case 'C':
9409 case 'U':
9410 case 'k':
9411 case 'K':
9412 if (s[0] == '%'
9413 && strncmp (s + 1, "gprel(", sizeof "gprel(" - 1) == 0)
9414 {
9415 /* This is %gprel(SYMBOL). We need to read SYMBOL,
9416 and generate the appropriate reloc. If the text
9417 inside %gprel is not a symbol name with an
9418 optional offset, then we generate a normal reloc
9419 and will probably fail later. */
9420 my_getExpression (&imm_expr, s + sizeof "%gprel" - 1);
9421 if (imm_expr.X_op == O_symbol)
9422 {
b34976b6 9423 mips16_ext = TRUE;
f6688943 9424 *imm_reloc = BFD_RELOC_MIPS16_GPREL;
252b5132 9425 s = expr_end;
b34976b6 9426 ip->use_extend = TRUE;
252b5132
RH
9427 ip->extend = 0;
9428 continue;
9429 }
9430 }
9431 else
9432 {
9433 /* Just pick up a normal expression. */
9434 my_getExpression (&imm_expr, s);
9435 }
9436
9437 if (imm_expr.X_op == O_register)
9438 {
9439 /* What we thought was an expression turned out to
9440 be a register. */
9441
9442 if (s[0] == '(' && args[1] == '(')
9443 {
9444 /* It looks like the expression was omitted
9445 before a register indirection, which means
9446 that the expression is implicitly zero. We
9447 still set up imm_expr, so that we handle
9448 explicit extensions correctly. */
9449 imm_expr.X_op = O_constant;
9450 imm_expr.X_add_number = 0;
f6688943 9451 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
9452 continue;
9453 }
9454
9455 break;
9456 }
9457
9458 /* We need to relax this instruction. */
f6688943 9459 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
9460 s = expr_end;
9461 continue;
9462
9463 case 'p':
9464 case 'q':
9465 case 'A':
9466 case 'B':
9467 case 'E':
9468 /* We use offset_reloc rather than imm_reloc for the PC
9469 relative operands. This lets macros with both
9470 immediate and address operands work correctly. */
9471 my_getExpression (&offset_expr, s);
9472
9473 if (offset_expr.X_op == O_register)
9474 break;
9475
9476 /* We need to relax this instruction. */
f6688943 9477 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
9478 s = expr_end;
9479 continue;
9480
9481 case '6': /* break code */
9482 my_getExpression (&imm_expr, s);
9483 check_absolute_expr (ip, &imm_expr);
9484 if ((unsigned long) imm_expr.X_add_number > 63)
9485 {
9486 as_warn (_("Invalid value for `%s' (%lu)"),
9487 ip->insn_mo->name,
9488 (unsigned long) imm_expr.X_add_number);
9489 imm_expr.X_add_number &= 0x3f;
9490 }
9491 ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
9492 imm_expr.X_op = O_absent;
9493 s = expr_end;
9494 continue;
9495
9496 case 'a': /* 26 bit address */
9497 my_getExpression (&offset_expr, s);
9498 s = expr_end;
f6688943 9499 *offset_reloc = BFD_RELOC_MIPS16_JMP;
252b5132
RH
9500 ip->insn_opcode <<= 16;
9501 continue;
9502
9503 case 'l': /* register list for entry macro */
9504 case 'L': /* register list for exit macro */
9505 {
9506 int mask;
9507
9508 if (c == 'l')
9509 mask = 0;
9510 else
9511 mask = 7 << 3;
9512 while (*s != '\0')
9513 {
9514 int freg, reg1, reg2;
9515
9516 while (*s == ' ' || *s == ',')
9517 ++s;
9518 if (*s != '$')
9519 {
9520 as_bad (_("can't parse register list"));
9521 break;
9522 }
9523 ++s;
9524 if (*s != 'f')
9525 freg = 0;
9526 else
9527 {
9528 freg = 1;
9529 ++s;
9530 }
9531 reg1 = 0;
3882b010 9532 while (ISDIGIT (*s))
252b5132
RH
9533 {
9534 reg1 *= 10;
9535 reg1 += *s - '0';
9536 ++s;
9537 }
9538 if (*s == ' ')
9539 ++s;
9540 if (*s != '-')
9541 reg2 = reg1;
9542 else
9543 {
9544 ++s;
9545 if (*s != '$')
9546 break;
9547 ++s;
9548 if (freg)
9549 {
9550 if (*s == 'f')
9551 ++s;
9552 else
9553 {
9554 as_bad (_("invalid register list"));
9555 break;
9556 }
9557 }
9558 reg2 = 0;
3882b010 9559 while (ISDIGIT (*s))
252b5132
RH
9560 {
9561 reg2 *= 10;
9562 reg2 += *s - '0';
9563 ++s;
9564 }
9565 }
9566 if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
9567 {
9568 mask &= ~ (7 << 3);
9569 mask |= 5 << 3;
9570 }
9571 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
9572 {
9573 mask &= ~ (7 << 3);
9574 mask |= 6 << 3;
9575 }
9576 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
9577 mask |= (reg2 - 3) << 3;
9578 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
9579 mask |= (reg2 - 15) << 1;
f9419b05 9580 else if (reg1 == RA && reg2 == RA)
252b5132
RH
9581 mask |= 1;
9582 else
9583 {
9584 as_bad (_("invalid register list"));
9585 break;
9586 }
9587 }
9588 /* The mask is filled in in the opcode table for the
9589 benefit of the disassembler. We remove it before
9590 applying the actual mask. */
9591 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
9592 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
9593 }
9594 continue;
9595
9596 case 'e': /* extend code */
9597 my_getExpression (&imm_expr, s);
9598 check_absolute_expr (ip, &imm_expr);
9599 if ((unsigned long) imm_expr.X_add_number > 0x7ff)
9600 {
9601 as_warn (_("Invalid value for `%s' (%lu)"),
9602 ip->insn_mo->name,
9603 (unsigned long) imm_expr.X_add_number);
9604 imm_expr.X_add_number &= 0x7ff;
9605 }
9606 ip->insn_opcode |= imm_expr.X_add_number;
9607 imm_expr.X_op = O_absent;
9608 s = expr_end;
9609 continue;
9610
9611 default:
9612 internalError ();
9613 }
9614 break;
9615 }
9616
9617 /* Args don't match. */
9618 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
9619 strcmp (insn->name, insn[1].name) == 0)
9620 {
9621 ++insn;
9622 s = argsstart;
9623 continue;
9624 }
9625
9626 insn_error = _("illegal operands");
9627
9628 return;
9629 }
9630}
9631
9632/* This structure holds information we know about a mips16 immediate
9633 argument type. */
9634
e972090a
NC
9635struct mips16_immed_operand
9636{
252b5132
RH
9637 /* The type code used in the argument string in the opcode table. */
9638 int type;
9639 /* The number of bits in the short form of the opcode. */
9640 int nbits;
9641 /* The number of bits in the extended form of the opcode. */
9642 int extbits;
9643 /* The amount by which the short form is shifted when it is used;
9644 for example, the sw instruction has a shift count of 2. */
9645 int shift;
9646 /* The amount by which the short form is shifted when it is stored
9647 into the instruction code. */
9648 int op_shift;
9649 /* Non-zero if the short form is unsigned. */
9650 int unsp;
9651 /* Non-zero if the extended form is unsigned. */
9652 int extu;
9653 /* Non-zero if the value is PC relative. */
9654 int pcrel;
9655};
9656
9657/* The mips16 immediate operand types. */
9658
9659static const struct mips16_immed_operand mips16_immed_operands[] =
9660{
9661 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9662 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9663 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9664 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9665 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
9666 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
9667 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
9668 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
9669 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
9670 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
9671 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
9672 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
9673 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
9674 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
9675 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
9676 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
9677 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9678 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9679 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
9680 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
9681 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
9682};
9683
9684#define MIPS16_NUM_IMMED \
9685 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
9686
9687/* Handle a mips16 instruction with an immediate value. This or's the
9688 small immediate value into *INSN. It sets *USE_EXTEND to indicate
9689 whether an extended value is needed; if one is needed, it sets
9690 *EXTEND to the value. The argument type is TYPE. The value is VAL.
9691 If SMALL is true, an unextended opcode was explicitly requested.
9692 If EXT is true, an extended opcode was explicitly requested. If
9693 WARN is true, warn if EXT does not match reality. */
9694
9695static void
17a2f251
TS
9696mips16_immed (char *file, unsigned int line, int type, offsetT val,
9697 bfd_boolean warn, bfd_boolean small, bfd_boolean ext,
9698 unsigned long *insn, bfd_boolean *use_extend,
9699 unsigned short *extend)
252b5132
RH
9700{
9701 register const struct mips16_immed_operand *op;
9702 int mintiny, maxtiny;
b34976b6 9703 bfd_boolean needext;
252b5132
RH
9704
9705 op = mips16_immed_operands;
9706 while (op->type != type)
9707 {
9708 ++op;
9709 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
9710 }
9711
9712 if (op->unsp)
9713 {
9714 if (type == '<' || type == '>' || type == '[' || type == ']')
9715 {
9716 mintiny = 1;
9717 maxtiny = 1 << op->nbits;
9718 }
9719 else
9720 {
9721 mintiny = 0;
9722 maxtiny = (1 << op->nbits) - 1;
9723 }
9724 }
9725 else
9726 {
9727 mintiny = - (1 << (op->nbits - 1));
9728 maxtiny = (1 << (op->nbits - 1)) - 1;
9729 }
9730
9731 /* Branch offsets have an implicit 0 in the lowest bit. */
9732 if (type == 'p' || type == 'q')
9733 val /= 2;
9734
9735 if ((val & ((1 << op->shift) - 1)) != 0
9736 || val < (mintiny << op->shift)
9737 || val > (maxtiny << op->shift))
b34976b6 9738 needext = TRUE;
252b5132 9739 else
b34976b6 9740 needext = FALSE;
252b5132
RH
9741
9742 if (warn && ext && ! needext)
beae10d5
KH
9743 as_warn_where (file, line,
9744 _("extended operand requested but not required"));
252b5132
RH
9745 if (small && needext)
9746 as_bad_where (file, line, _("invalid unextended operand value"));
9747
9748 if (small || (! ext && ! needext))
9749 {
9750 int insnval;
9751
b34976b6 9752 *use_extend = FALSE;
252b5132
RH
9753 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
9754 insnval <<= op->op_shift;
9755 *insn |= insnval;
9756 }
9757 else
9758 {
9759 long minext, maxext;
9760 int extval;
9761
9762 if (op->extu)
9763 {
9764 minext = 0;
9765 maxext = (1 << op->extbits) - 1;
9766 }
9767 else
9768 {
9769 minext = - (1 << (op->extbits - 1));
9770 maxext = (1 << (op->extbits - 1)) - 1;
9771 }
9772 if (val < minext || val > maxext)
9773 as_bad_where (file, line,
9774 _("operand value out of range for instruction"));
9775
b34976b6 9776 *use_extend = TRUE;
252b5132
RH
9777 if (op->extbits == 16)
9778 {
9779 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
9780 val &= 0x1f;
9781 }
9782 else if (op->extbits == 15)
9783 {
9784 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
9785 val &= 0xf;
9786 }
9787 else
9788 {
9789 extval = ((val & 0x1f) << 6) | (val & 0x20);
9790 val = 0;
9791 }
9792
9793 *extend = (unsigned short) extval;
9794 *insn |= val;
9795 }
9796}
9797\f
5e0116d5 9798static const struct percent_op_match
ad8d3bb3 9799{
5e0116d5
RS
9800 const char *str;
9801 bfd_reloc_code_real_type reloc;
ad8d3bb3
TS
9802} percent_op[] =
9803{
5e0116d5 9804 {"%lo", BFD_RELOC_LO16},
ad8d3bb3 9805#ifdef OBJ_ELF
5e0116d5
RS
9806 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
9807 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
9808 {"%call16", BFD_RELOC_MIPS_CALL16},
9809 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
9810 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
9811 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
9812 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
9813 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
9814 {"%got", BFD_RELOC_MIPS_GOT16},
9815 {"%gp_rel", BFD_RELOC_GPREL16},
9816 {"%half", BFD_RELOC_16},
9817 {"%highest", BFD_RELOC_MIPS_HIGHEST},
9818 {"%higher", BFD_RELOC_MIPS_HIGHER},
9819 {"%neg", BFD_RELOC_MIPS_SUB},
ad8d3bb3 9820#endif
5e0116d5 9821 {"%hi", BFD_RELOC_HI16_S}
ad8d3bb3
TS
9822};
9823
252b5132 9824
5e0116d5
RS
9825/* Return true if *STR points to a relocation operator. When returning true,
9826 move *STR over the operator and store its relocation code in *RELOC.
9827 Leave both *STR and *RELOC alone when returning false. */
9828
9829static bfd_boolean
17a2f251 9830parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
252b5132 9831{
5e0116d5 9832 size_t i;
76b3015f 9833
5e0116d5
RS
9834 for (i = 0; i < ARRAY_SIZE (percent_op); i++)
9835 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
394f9b3a 9836 {
5e0116d5
RS
9837 *str += strlen (percent_op[i].str);
9838 *reloc = percent_op[i].reloc;
394f9b3a 9839
5e0116d5
RS
9840 /* Check whether the output BFD supports this relocation.
9841 If not, issue an error and fall back on something safe. */
9842 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
394f9b3a 9843 {
5e0116d5
RS
9844 as_bad ("relocation %s isn't supported by the current ABI",
9845 percent_op[i].str);
01a3f561 9846 *reloc = BFD_RELOC_UNUSED;
394f9b3a 9847 }
5e0116d5 9848 return TRUE;
394f9b3a 9849 }
5e0116d5 9850 return FALSE;
394f9b3a 9851}
ad8d3bb3 9852
ad8d3bb3 9853
5e0116d5
RS
9854/* Parse string STR as a 16-bit relocatable operand. Store the
9855 expression in *EP and the relocations in the array starting
9856 at RELOC. Return the number of relocation operators used.
ad8d3bb3 9857
01a3f561 9858 On exit, EXPR_END points to the first character after the expression. */
ad8d3bb3 9859
5e0116d5 9860static size_t
17a2f251
TS
9861my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
9862 char *str)
ad8d3bb3 9863{
5e0116d5
RS
9864 bfd_reloc_code_real_type reversed_reloc[3];
9865 size_t reloc_index, i;
09b8f35a
RS
9866 int crux_depth, str_depth;
9867 char *crux;
5e0116d5
RS
9868
9869 /* Search for the start of the main expression, recoding relocations
09b8f35a
RS
9870 in REVERSED_RELOC. End the loop with CRUX pointing to the start
9871 of the main expression and with CRUX_DEPTH containing the number
9872 of open brackets at that point. */
9873 reloc_index = -1;
9874 str_depth = 0;
9875 do
fb1b3232 9876 {
09b8f35a
RS
9877 reloc_index++;
9878 crux = str;
9879 crux_depth = str_depth;
9880
9881 /* Skip over whitespace and brackets, keeping count of the number
9882 of brackets. */
9883 while (*str == ' ' || *str == '\t' || *str == '(')
9884 if (*str++ == '(')
9885 str_depth++;
5e0116d5 9886 }
09b8f35a
RS
9887 while (*str == '%'
9888 && reloc_index < (HAVE_NEWABI ? 3 : 1)
9889 && parse_relocation (&str, &reversed_reloc[reloc_index]));
ad8d3bb3 9890
09b8f35a 9891 my_getExpression (ep, crux);
5e0116d5 9892 str = expr_end;
394f9b3a 9893
5e0116d5 9894 /* Match every open bracket. */
09b8f35a 9895 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
5e0116d5 9896 if (*str++ == ')')
09b8f35a 9897 crux_depth--;
394f9b3a 9898
09b8f35a 9899 if (crux_depth > 0)
5e0116d5 9900 as_bad ("unclosed '('");
394f9b3a 9901
5e0116d5 9902 expr_end = str;
252b5132 9903
01a3f561 9904 if (reloc_index != 0)
64bdfcaf
RS
9905 {
9906 prev_reloc_op_frag = frag_now;
9907 for (i = 0; i < reloc_index; i++)
9908 reloc[i] = reversed_reloc[reloc_index - 1 - i];
9909 }
fb1b3232 9910
5e0116d5 9911 return reloc_index;
252b5132
RH
9912}
9913
9914static void
17a2f251 9915my_getExpression (expressionS *ep, char *str)
252b5132
RH
9916{
9917 char *save_in;
98aa84af 9918 valueT val;
252b5132
RH
9919
9920 save_in = input_line_pointer;
9921 input_line_pointer = str;
9922 expression (ep);
9923 expr_end = input_line_pointer;
9924 input_line_pointer = save_in;
9925
9926 /* If we are in mips16 mode, and this is an expression based on `.',
9927 then we bump the value of the symbol by 1 since that is how other
9928 text symbols are handled. We don't bother to handle complex
9929 expressions, just `.' plus or minus a constant. */
9930 if (mips_opts.mips16
9931 && ep->X_op == O_symbol
9932 && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
9933 && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
49309057
ILT
9934 && symbol_get_frag (ep->X_add_symbol) == frag_now
9935 && symbol_constant_p (ep->X_add_symbol)
98aa84af
AM
9936 && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
9937 S_SET_VALUE (ep->X_add_symbol, val + 1);
252b5132
RH
9938}
9939
9940/* Turn a string in input_line_pointer into a floating point constant
bc0d738a
NC
9941 of type TYPE, and store the appropriate bytes in *LITP. The number
9942 of LITTLENUMS emitted is stored in *SIZEP. An error message is
252b5132
RH
9943 returned, or NULL on OK. */
9944
9945char *
17a2f251 9946md_atof (int type, char *litP, int *sizeP)
252b5132
RH
9947{
9948 int prec;
9949 LITTLENUM_TYPE words[4];
9950 char *t;
9951 int i;
9952
9953 switch (type)
9954 {
9955 case 'f':
9956 prec = 2;
9957 break;
9958
9959 case 'd':
9960 prec = 4;
9961 break;
9962
9963 default:
9964 *sizeP = 0;
9965 return _("bad call to md_atof");
9966 }
9967
9968 t = atof_ieee (input_line_pointer, type, words);
9969 if (t)
9970 input_line_pointer = t;
9971
9972 *sizeP = prec * 2;
9973
9974 if (! target_big_endian)
9975 {
9976 for (i = prec - 1; i >= 0; i--)
9977 {
17a2f251 9978 md_number_to_chars (litP, words[i], 2);
252b5132
RH
9979 litP += 2;
9980 }
9981 }
9982 else
9983 {
9984 for (i = 0; i < prec; i++)
9985 {
17a2f251 9986 md_number_to_chars (litP, words[i], 2);
252b5132
RH
9987 litP += 2;
9988 }
9989 }
bdaaa2e1 9990
252b5132
RH
9991 return NULL;
9992}
9993
9994void
17a2f251 9995md_number_to_chars (char *buf, valueT val, int n)
252b5132
RH
9996{
9997 if (target_big_endian)
9998 number_to_chars_bigendian (buf, val, n);
9999 else
10000 number_to_chars_littleendian (buf, val, n);
10001}
10002\f
ae948b86 10003#ifdef OBJ_ELF
e013f690
TS
10004static int support_64bit_objects(void)
10005{
10006 const char **list, **l;
aa3d8fdf 10007 int yes;
e013f690
TS
10008
10009 list = bfd_target_list ();
10010 for (l = list; *l != NULL; l++)
10011#ifdef TE_TMIPS
10012 /* This is traditional mips */
10013 if (strcmp (*l, "elf64-tradbigmips") == 0
10014 || strcmp (*l, "elf64-tradlittlemips") == 0)
10015#else
10016 if (strcmp (*l, "elf64-bigmips") == 0
10017 || strcmp (*l, "elf64-littlemips") == 0)
10018#endif
10019 break;
aa3d8fdf 10020 yes = (*l != NULL);
e013f690 10021 free (list);
aa3d8fdf 10022 return yes;
e013f690 10023}
ae948b86 10024#endif /* OBJ_ELF */
e013f690 10025
78849248 10026const char *md_shortopts = "O::g::G:";
252b5132 10027
e972090a
NC
10028struct option md_longopts[] =
10029{
f9b4148d
CD
10030 /* Options which specify architecture. */
10031#define OPTION_ARCH_BASE (OPTION_MD_BASE)
10032#define OPTION_MARCH (OPTION_ARCH_BASE + 0)
10033 {"march", required_argument, NULL, OPTION_MARCH},
10034#define OPTION_MTUNE (OPTION_ARCH_BASE + 1)
10035 {"mtune", required_argument, NULL, OPTION_MTUNE},
10036#define OPTION_MIPS1 (OPTION_ARCH_BASE + 2)
252b5132
RH
10037 {"mips0", no_argument, NULL, OPTION_MIPS1},
10038 {"mips1", no_argument, NULL, OPTION_MIPS1},
f9b4148d 10039#define OPTION_MIPS2 (OPTION_ARCH_BASE + 3)
252b5132 10040 {"mips2", no_argument, NULL, OPTION_MIPS2},
f9b4148d 10041#define OPTION_MIPS3 (OPTION_ARCH_BASE + 4)
252b5132 10042 {"mips3", no_argument, NULL, OPTION_MIPS3},
f9b4148d 10043#define OPTION_MIPS4 (OPTION_ARCH_BASE + 5)
252b5132 10044 {"mips4", no_argument, NULL, OPTION_MIPS4},
f9b4148d 10045#define OPTION_MIPS5 (OPTION_ARCH_BASE + 6)
ae948b86 10046 {"mips5", no_argument, NULL, OPTION_MIPS5},
f9b4148d 10047#define OPTION_MIPS32 (OPTION_ARCH_BASE + 7)
ae948b86 10048 {"mips32", no_argument, NULL, OPTION_MIPS32},
f9b4148d 10049#define OPTION_MIPS64 (OPTION_ARCH_BASE + 8)
ae948b86 10050 {"mips64", no_argument, NULL, OPTION_MIPS64},
f9b4148d
CD
10051#define OPTION_MIPS32R2 (OPTION_ARCH_BASE + 9)
10052 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
5f74bc13
CD
10053#define OPTION_MIPS64R2 (OPTION_ARCH_BASE + 10)
10054 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
f9b4148d
CD
10055
10056 /* Options which specify Application Specific Extensions (ASEs). */
5f74bc13 10057#define OPTION_ASE_BASE (OPTION_ARCH_BASE + 11)
f9b4148d
CD
10058#define OPTION_MIPS16 (OPTION_ASE_BASE + 0)
10059 {"mips16", no_argument, NULL, OPTION_MIPS16},
10060#define OPTION_NO_MIPS16 (OPTION_ASE_BASE + 1)
10061 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
10062#define OPTION_MIPS3D (OPTION_ASE_BASE + 2)
10063 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
10064#define OPTION_NO_MIPS3D (OPTION_ASE_BASE + 3)
10065 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
10066#define OPTION_MDMX (OPTION_ASE_BASE + 4)
10067 {"mdmx", no_argument, NULL, OPTION_MDMX},
10068#define OPTION_NO_MDMX (OPTION_ASE_BASE + 5)
10069 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
10070
10071 /* Old-style architecture options. Don't add more of these. */
10072#define OPTION_COMPAT_ARCH_BASE (OPTION_ASE_BASE + 6)
10073#define OPTION_M4650 (OPTION_COMPAT_ARCH_BASE + 0)
10074 {"m4650", no_argument, NULL, OPTION_M4650},
10075#define OPTION_NO_M4650 (OPTION_COMPAT_ARCH_BASE + 1)
10076 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
10077#define OPTION_M4010 (OPTION_COMPAT_ARCH_BASE + 2)
10078 {"m4010", no_argument, NULL, OPTION_M4010},
10079#define OPTION_NO_M4010 (OPTION_COMPAT_ARCH_BASE + 3)
10080 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
10081#define OPTION_M4100 (OPTION_COMPAT_ARCH_BASE + 4)
10082 {"m4100", no_argument, NULL, OPTION_M4100},
10083#define OPTION_NO_M4100 (OPTION_COMPAT_ARCH_BASE + 5)
10084 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
10085#define OPTION_M3900 (OPTION_COMPAT_ARCH_BASE + 6)
10086 {"m3900", no_argument, NULL, OPTION_M3900},
10087#define OPTION_NO_M3900 (OPTION_COMPAT_ARCH_BASE + 7)
10088 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
10089
10090 /* Options which enable bug fixes. */
10091#define OPTION_FIX_BASE (OPTION_COMPAT_ARCH_BASE + 8)
10092#define OPTION_M7000_HILO_FIX (OPTION_FIX_BASE + 0)
10093 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
10094#define OPTION_MNO_7000_HILO_FIX (OPTION_FIX_BASE + 1)
10095 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10096 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
d766e8ec
RS
10097#define OPTION_FIX_VR4120 (OPTION_FIX_BASE + 2)
10098#define OPTION_NO_FIX_VR4120 (OPTION_FIX_BASE + 3)
10099 {"mfix-vr4120", no_argument, NULL, OPTION_FIX_VR4120},
10100 {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
f9b4148d
CD
10101
10102 /* Miscellaneous options. */
10103#define OPTION_MISC_BASE (OPTION_FIX_BASE + 4)
1ffcab4b 10104#define OPTION_TRAP (OPTION_MISC_BASE + 0)
252b5132
RH
10105 {"trap", no_argument, NULL, OPTION_TRAP},
10106 {"no-break", no_argument, NULL, OPTION_TRAP},
1ffcab4b 10107#define OPTION_BREAK (OPTION_MISC_BASE + 1)
252b5132
RH
10108 {"break", no_argument, NULL, OPTION_BREAK},
10109 {"no-trap", no_argument, NULL, OPTION_BREAK},
1ffcab4b 10110#define OPTION_EB (OPTION_MISC_BASE + 2)
252b5132 10111 {"EB", no_argument, NULL, OPTION_EB},
1ffcab4b 10112#define OPTION_EL (OPTION_MISC_BASE + 3)
252b5132 10113 {"EL", no_argument, NULL, OPTION_EL},
1ffcab4b 10114#define OPTION_FP32 (OPTION_MISC_BASE + 4)
ae948b86 10115 {"mfp32", no_argument, NULL, OPTION_FP32},
1ffcab4b 10116#define OPTION_GP32 (OPTION_MISC_BASE + 5)
c97ef257 10117 {"mgp32", no_argument, NULL, OPTION_GP32},
1ffcab4b 10118#define OPTION_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 6)
119d663a 10119 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
1ffcab4b 10120#define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 7)
119d663a 10121 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
1ffcab4b 10122#define OPTION_FP64 (OPTION_MISC_BASE + 8)
316f5878 10123 {"mfp64", no_argument, NULL, OPTION_FP64},
1ffcab4b 10124#define OPTION_GP64 (OPTION_MISC_BASE + 9)
ae948b86 10125 {"mgp64", no_argument, NULL, OPTION_GP64},
1ffcab4b
CD
10126#define OPTION_RELAX_BRANCH (OPTION_MISC_BASE + 10)
10127#define OPTION_NO_RELAX_BRANCH (OPTION_MISC_BASE + 11)
4a6a3df4
AO
10128 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
10129 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
aa6975fb
ILT
10130#define OPTION_MSHARED (OPTION_MISC_BASE + 12)
10131#define OPTION_MNO_SHARED (OPTION_MISC_BASE + 13)
10132 {"mshared", no_argument, NULL, OPTION_MSHARED},
10133 {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
f9b4148d
CD
10134
10135 /* ELF-specific options. */
156c2f8b 10136#ifdef OBJ_ELF
aa6975fb 10137#define OPTION_ELF_BASE (OPTION_MISC_BASE + 14)
156c2f8b 10138#define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
156c2f8b
NC
10139 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
10140 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
ae948b86 10141#define OPTION_NON_SHARED (OPTION_ELF_BASE + 1)
156c2f8b 10142 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
ae948b86 10143#define OPTION_XGOT (OPTION_ELF_BASE + 2)
156c2f8b 10144 {"xgot", no_argument, NULL, OPTION_XGOT},
ae948b86
TS
10145#define OPTION_MABI (OPTION_ELF_BASE + 3)
10146 {"mabi", required_argument, NULL, OPTION_MABI},
10147#define OPTION_32 (OPTION_ELF_BASE + 4)
156c2f8b 10148 {"32", no_argument, NULL, OPTION_32},
ae948b86 10149#define OPTION_N32 (OPTION_ELF_BASE + 5)
e013f690 10150 {"n32", no_argument, NULL, OPTION_N32},
ae948b86 10151#define OPTION_64 (OPTION_ELF_BASE + 6)
156c2f8b 10152 {"64", no_argument, NULL, OPTION_64},
ecb4347a
DJ
10153#define OPTION_MDEBUG (OPTION_ELF_BASE + 7)
10154 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
10155#define OPTION_NO_MDEBUG (OPTION_ELF_BASE + 8)
10156 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
dcd410fe
RO
10157#define OPTION_PDR (OPTION_ELF_BASE + 9)
10158 {"mpdr", no_argument, NULL, OPTION_PDR},
10159#define OPTION_NO_PDR (OPTION_ELF_BASE + 10)
10160 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
ae948b86 10161#endif /* OBJ_ELF */
f9b4148d 10162
252b5132
RH
10163 {NULL, no_argument, NULL, 0}
10164};
156c2f8b 10165size_t md_longopts_size = sizeof (md_longopts);
252b5132 10166
316f5878
RS
10167/* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
10168 NEW_VALUE. Warn if another value was already specified. Note:
10169 we have to defer parsing the -march and -mtune arguments in order
10170 to handle 'from-abi' correctly, since the ABI might be specified
10171 in a later argument. */
10172
10173static void
17a2f251 10174mips_set_option_string (const char **string_ptr, const char *new_value)
316f5878
RS
10175{
10176 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
10177 as_warn (_("A different %s was already specified, is now %s"),
10178 string_ptr == &mips_arch_string ? "-march" : "-mtune",
10179 new_value);
10180
10181 *string_ptr = new_value;
10182}
10183
252b5132 10184int
17a2f251 10185md_parse_option (int c, char *arg)
252b5132
RH
10186{
10187 switch (c)
10188 {
119d663a
NC
10189 case OPTION_CONSTRUCT_FLOATS:
10190 mips_disable_float_construction = 0;
10191 break;
bdaaa2e1 10192
119d663a
NC
10193 case OPTION_NO_CONSTRUCT_FLOATS:
10194 mips_disable_float_construction = 1;
10195 break;
bdaaa2e1 10196
252b5132
RH
10197 case OPTION_TRAP:
10198 mips_trap = 1;
10199 break;
10200
10201 case OPTION_BREAK:
10202 mips_trap = 0;
10203 break;
10204
10205 case OPTION_EB:
10206 target_big_endian = 1;
10207 break;
10208
10209 case OPTION_EL:
10210 target_big_endian = 0;
10211 break;
10212
10213 case 'O':
10214 if (arg && arg[1] == '0')
10215 mips_optimize = 1;
10216 else
10217 mips_optimize = 2;
10218 break;
10219
10220 case 'g':
10221 if (arg == NULL)
10222 mips_debug = 2;
10223 else
10224 mips_debug = atoi (arg);
10225 /* When the MIPS assembler sees -g or -g2, it does not do
10226 optimizations which limit full symbolic debugging. We take
10227 that to be equivalent to -O0. */
10228 if (mips_debug == 2)
10229 mips_optimize = 1;
10230 break;
10231
10232 case OPTION_MIPS1:
316f5878 10233 file_mips_isa = ISA_MIPS1;
252b5132
RH
10234 break;
10235
10236 case OPTION_MIPS2:
316f5878 10237 file_mips_isa = ISA_MIPS2;
252b5132
RH
10238 break;
10239
10240 case OPTION_MIPS3:
316f5878 10241 file_mips_isa = ISA_MIPS3;
252b5132
RH
10242 break;
10243
10244 case OPTION_MIPS4:
316f5878 10245 file_mips_isa = ISA_MIPS4;
e7af610e
NC
10246 break;
10247
84ea6cf2 10248 case OPTION_MIPS5:
316f5878 10249 file_mips_isa = ISA_MIPS5;
84ea6cf2
NC
10250 break;
10251
e7af610e 10252 case OPTION_MIPS32:
316f5878 10253 file_mips_isa = ISA_MIPS32;
252b5132
RH
10254 break;
10255
af7ee8bf
CD
10256 case OPTION_MIPS32R2:
10257 file_mips_isa = ISA_MIPS32R2;
10258 break;
10259
5f74bc13
CD
10260 case OPTION_MIPS64R2:
10261 file_mips_isa = ISA_MIPS64R2;
10262 break;
10263
84ea6cf2 10264 case OPTION_MIPS64:
316f5878 10265 file_mips_isa = ISA_MIPS64;
84ea6cf2
NC
10266 break;
10267
ec68c924 10268 case OPTION_MTUNE:
316f5878
RS
10269 mips_set_option_string (&mips_tune_string, arg);
10270 break;
ec68c924 10271
316f5878
RS
10272 case OPTION_MARCH:
10273 mips_set_option_string (&mips_arch_string, arg);
252b5132
RH
10274 break;
10275
10276 case OPTION_M4650:
316f5878
RS
10277 mips_set_option_string (&mips_arch_string, "4650");
10278 mips_set_option_string (&mips_tune_string, "4650");
252b5132
RH
10279 break;
10280
10281 case OPTION_NO_M4650:
10282 break;
10283
10284 case OPTION_M4010:
316f5878
RS
10285 mips_set_option_string (&mips_arch_string, "4010");
10286 mips_set_option_string (&mips_tune_string, "4010");
252b5132
RH
10287 break;
10288
10289 case OPTION_NO_M4010:
10290 break;
10291
10292 case OPTION_M4100:
316f5878
RS
10293 mips_set_option_string (&mips_arch_string, "4100");
10294 mips_set_option_string (&mips_tune_string, "4100");
252b5132
RH
10295 break;
10296
10297 case OPTION_NO_M4100:
10298 break;
10299
252b5132 10300 case OPTION_M3900:
316f5878
RS
10301 mips_set_option_string (&mips_arch_string, "3900");
10302 mips_set_option_string (&mips_tune_string, "3900");
252b5132 10303 break;
bdaaa2e1 10304
252b5132
RH
10305 case OPTION_NO_M3900:
10306 break;
10307
deec1734
CD
10308 case OPTION_MDMX:
10309 mips_opts.ase_mdmx = 1;
10310 break;
10311
10312 case OPTION_NO_MDMX:
10313 mips_opts.ase_mdmx = 0;
10314 break;
10315
252b5132
RH
10316 case OPTION_MIPS16:
10317 mips_opts.mips16 = 1;
b34976b6 10318 mips_no_prev_insn (FALSE);
252b5132
RH
10319 break;
10320
10321 case OPTION_NO_MIPS16:
10322 mips_opts.mips16 = 0;
b34976b6 10323 mips_no_prev_insn (FALSE);
252b5132
RH
10324 break;
10325
1f25f5d3
CD
10326 case OPTION_MIPS3D:
10327 mips_opts.ase_mips3d = 1;
10328 break;
10329
10330 case OPTION_NO_MIPS3D:
10331 mips_opts.ase_mips3d = 0;
10332 break;
10333
d766e8ec
RS
10334 case OPTION_FIX_VR4120:
10335 mips_fix_vr4120 = 1;
60b63b72
RS
10336 break;
10337
d766e8ec
RS
10338 case OPTION_NO_FIX_VR4120:
10339 mips_fix_vr4120 = 0;
60b63b72
RS
10340 break;
10341
4a6a3df4
AO
10342 case OPTION_RELAX_BRANCH:
10343 mips_relax_branch = 1;
10344 break;
10345
10346 case OPTION_NO_RELAX_BRANCH:
10347 mips_relax_branch = 0;
10348 break;
10349
aa6975fb
ILT
10350 case OPTION_MSHARED:
10351 mips_in_shared = TRUE;
10352 break;
10353
10354 case OPTION_MNO_SHARED:
10355 mips_in_shared = FALSE;
10356 break;
10357
0f074f60 10358#ifdef OBJ_ELF
252b5132
RH
10359 /* When generating ELF code, we permit -KPIC and -call_shared to
10360 select SVR4_PIC, and -non_shared to select no PIC. This is
10361 intended to be compatible with Irix 5. */
10362 case OPTION_CALL_SHARED:
10363 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10364 {
10365 as_bad (_("-call_shared is supported only for ELF format"));
10366 return 0;
10367 }
10368 mips_pic = SVR4_PIC;
143d77c5 10369 mips_abicalls = TRUE;
252b5132
RH
10370 if (g_switch_seen && g_switch_value != 0)
10371 {
10372 as_bad (_("-G may not be used with SVR4 PIC code"));
10373 return 0;
10374 }
10375 g_switch_value = 0;
10376 break;
10377
10378 case OPTION_NON_SHARED:
10379 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10380 {
10381 as_bad (_("-non_shared is supported only for ELF format"));
10382 return 0;
10383 }
10384 mips_pic = NO_PIC;
143d77c5 10385 mips_abicalls = FALSE;
252b5132
RH
10386 break;
10387
10388 /* The -xgot option tells the assembler to use 32 offsets when
10389 accessing the got in SVR4_PIC mode. It is for Irix
10390 compatibility. */
10391 case OPTION_XGOT:
10392 mips_big_got = 1;
10393 break;
0f074f60 10394#endif /* OBJ_ELF */
252b5132
RH
10395
10396 case 'G':
3e722fb5 10397 if (mips_pic == SVR4_PIC)
252b5132 10398 {
3e722fb5 10399 as_bad (_("-G may not be used with SVR4 PIC code"));
252b5132
RH
10400 return 0;
10401 }
10402 else
10403 g_switch_value = atoi (arg);
10404 g_switch_seen = 1;
10405 break;
10406
0f074f60 10407#ifdef OBJ_ELF
34ba82a8
TS
10408 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
10409 and -mabi=64. */
252b5132 10410 case OPTION_32:
34ba82a8
TS
10411 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10412 {
10413 as_bad (_("-32 is supported for ELF format only"));
10414 return 0;
10415 }
316f5878 10416 mips_abi = O32_ABI;
252b5132
RH
10417 break;
10418
e013f690 10419 case OPTION_N32:
34ba82a8
TS
10420 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10421 {
10422 as_bad (_("-n32 is supported for ELF format only"));
10423 return 0;
10424 }
316f5878 10425 mips_abi = N32_ABI;
e013f690 10426 break;
252b5132 10427
e013f690 10428 case OPTION_64:
34ba82a8
TS
10429 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10430 {
10431 as_bad (_("-64 is supported for ELF format only"));
10432 return 0;
10433 }
316f5878 10434 mips_abi = N64_ABI;
e013f690
TS
10435 if (! support_64bit_objects())
10436 as_fatal (_("No compiled in support for 64 bit object file format"));
252b5132 10437 break;
ae948b86 10438#endif /* OBJ_ELF */
252b5132 10439
c97ef257 10440 case OPTION_GP32:
a325df1d 10441 file_mips_gp32 = 1;
c97ef257
AH
10442 break;
10443
10444 case OPTION_GP64:
a325df1d 10445 file_mips_gp32 = 0;
c97ef257 10446 break;
252b5132 10447
ca4e0257 10448 case OPTION_FP32:
a325df1d 10449 file_mips_fp32 = 1;
316f5878
RS
10450 break;
10451
10452 case OPTION_FP64:
10453 file_mips_fp32 = 0;
ca4e0257
RS
10454 break;
10455
ae948b86 10456#ifdef OBJ_ELF
252b5132 10457 case OPTION_MABI:
34ba82a8
TS
10458 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10459 {
10460 as_bad (_("-mabi is supported for ELF format only"));
10461 return 0;
10462 }
e013f690 10463 if (strcmp (arg, "32") == 0)
316f5878 10464 mips_abi = O32_ABI;
e013f690 10465 else if (strcmp (arg, "o64") == 0)
316f5878 10466 mips_abi = O64_ABI;
e013f690 10467 else if (strcmp (arg, "n32") == 0)
316f5878 10468 mips_abi = N32_ABI;
e013f690
TS
10469 else if (strcmp (arg, "64") == 0)
10470 {
316f5878 10471 mips_abi = N64_ABI;
e013f690
TS
10472 if (! support_64bit_objects())
10473 as_fatal (_("No compiled in support for 64 bit object file "
10474 "format"));
10475 }
10476 else if (strcmp (arg, "eabi") == 0)
316f5878 10477 mips_abi = EABI_ABI;
e013f690 10478 else
da0e507f
TS
10479 {
10480 as_fatal (_("invalid abi -mabi=%s"), arg);
10481 return 0;
10482 }
252b5132 10483 break;
e013f690 10484#endif /* OBJ_ELF */
252b5132 10485
6b76fefe 10486 case OPTION_M7000_HILO_FIX:
b34976b6 10487 mips_7000_hilo_fix = TRUE;
6b76fefe
CM
10488 break;
10489
9ee72ff1 10490 case OPTION_MNO_7000_HILO_FIX:
b34976b6 10491 mips_7000_hilo_fix = FALSE;
6b76fefe
CM
10492 break;
10493
ecb4347a
DJ
10494#ifdef OBJ_ELF
10495 case OPTION_MDEBUG:
b34976b6 10496 mips_flag_mdebug = TRUE;
ecb4347a
DJ
10497 break;
10498
10499 case OPTION_NO_MDEBUG:
b34976b6 10500 mips_flag_mdebug = FALSE;
ecb4347a 10501 break;
dcd410fe
RO
10502
10503 case OPTION_PDR:
10504 mips_flag_pdr = TRUE;
10505 break;
10506
10507 case OPTION_NO_PDR:
10508 mips_flag_pdr = FALSE;
10509 break;
ecb4347a
DJ
10510#endif /* OBJ_ELF */
10511
252b5132
RH
10512 default:
10513 return 0;
10514 }
10515
10516 return 1;
10517}
316f5878
RS
10518\f
10519/* Set up globals to generate code for the ISA or processor
10520 described by INFO. */
252b5132 10521
252b5132 10522static void
17a2f251 10523mips_set_architecture (const struct mips_cpu_info *info)
252b5132 10524{
316f5878 10525 if (info != 0)
252b5132 10526 {
fef14a42
TS
10527 file_mips_arch = info->cpu;
10528 mips_opts.arch = info->cpu;
316f5878 10529 mips_opts.isa = info->isa;
252b5132 10530 }
252b5132
RH
10531}
10532
252b5132 10533
316f5878 10534/* Likewise for tuning. */
252b5132 10535
316f5878 10536static void
17a2f251 10537mips_set_tune (const struct mips_cpu_info *info)
316f5878
RS
10538{
10539 if (info != 0)
fef14a42 10540 mips_tune = info->cpu;
316f5878 10541}
80cc45a5 10542
34ba82a8 10543
252b5132 10544void
17a2f251 10545mips_after_parse_args (void)
e9670677 10546{
fef14a42
TS
10547 const struct mips_cpu_info *arch_info = 0;
10548 const struct mips_cpu_info *tune_info = 0;
10549
e9670677
MR
10550 /* GP relative stuff not working for PE */
10551 if (strncmp (TARGET_OS, "pe", 2) == 0
10552 && g_switch_value != 0)
10553 {
10554 if (g_switch_seen)
10555 as_bad (_("-G not supported in this configuration."));
10556 g_switch_value = 0;
10557 }
10558
cac012d6
AO
10559 if (mips_abi == NO_ABI)
10560 mips_abi = MIPS_DEFAULT_ABI;
10561
22923709
RS
10562 /* The following code determines the architecture and register size.
10563 Similar code was added to GCC 3.3 (see override_options() in
10564 config/mips/mips.c). The GAS and GCC code should be kept in sync
10565 as much as possible. */
e9670677 10566
316f5878 10567 if (mips_arch_string != 0)
fef14a42 10568 arch_info = mips_parse_cpu ("-march", mips_arch_string);
e9670677 10569
316f5878 10570 if (file_mips_isa != ISA_UNKNOWN)
e9670677 10571 {
316f5878 10572 /* Handle -mipsN. At this point, file_mips_isa contains the
fef14a42 10573 ISA level specified by -mipsN, while arch_info->isa contains
316f5878 10574 the -march selection (if any). */
fef14a42 10575 if (arch_info != 0)
e9670677 10576 {
316f5878
RS
10577 /* -march takes precedence over -mipsN, since it is more descriptive.
10578 There's no harm in specifying both as long as the ISA levels
10579 are the same. */
fef14a42 10580 if (file_mips_isa != arch_info->isa)
316f5878
RS
10581 as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
10582 mips_cpu_info_from_isa (file_mips_isa)->name,
fef14a42 10583 mips_cpu_info_from_isa (arch_info->isa)->name);
e9670677 10584 }
316f5878 10585 else
fef14a42 10586 arch_info = mips_cpu_info_from_isa (file_mips_isa);
e9670677
MR
10587 }
10588
fef14a42
TS
10589 if (arch_info == 0)
10590 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
e9670677 10591
fef14a42 10592 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
316f5878 10593 as_bad ("-march=%s is not compatible with the selected ABI",
fef14a42
TS
10594 arch_info->name);
10595
10596 mips_set_architecture (arch_info);
10597
10598 /* Optimize for file_mips_arch, unless -mtune selects a different processor. */
10599 if (mips_tune_string != 0)
10600 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
e9670677 10601
fef14a42
TS
10602 if (tune_info == 0)
10603 mips_set_tune (arch_info);
10604 else
10605 mips_set_tune (tune_info);
e9670677 10606
316f5878 10607 if (file_mips_gp32 >= 0)
e9670677 10608 {
316f5878
RS
10609 /* The user specified the size of the integer registers. Make sure
10610 it agrees with the ABI and ISA. */
10611 if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
10612 as_bad (_("-mgp64 used with a 32-bit processor"));
10613 else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
10614 as_bad (_("-mgp32 used with a 64-bit ABI"));
10615 else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
10616 as_bad (_("-mgp64 used with a 32-bit ABI"));
e9670677
MR
10617 }
10618 else
10619 {
316f5878
RS
10620 /* Infer the integer register size from the ABI and processor.
10621 Restrict ourselves to 32-bit registers if that's all the
10622 processor has, or if the ABI cannot handle 64-bit registers. */
10623 file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
10624 || !ISA_HAS_64BIT_REGS (mips_opts.isa));
e9670677
MR
10625 }
10626
316f5878
RS
10627 /* ??? GAS treats single-float processors as though they had 64-bit
10628 float registers (although it complains when double-precision
10629 instructions are used). As things stand, saying they have 32-bit
10630 registers would lead to spurious "register must be even" messages.
10631 So here we assume float registers are always the same size as
10632 integer ones, unless the user says otherwise. */
10633 if (file_mips_fp32 < 0)
10634 file_mips_fp32 = file_mips_gp32;
e9670677 10635
316f5878 10636 /* End of GCC-shared inference code. */
e9670677 10637
17a2f251
TS
10638 /* This flag is set when we have a 64-bit capable CPU but use only
10639 32-bit wide registers. Note that EABI does not use it. */
10640 if (ISA_HAS_64BIT_REGS (mips_opts.isa)
10641 && ((mips_abi == NO_ABI && file_mips_gp32 == 1)
10642 || mips_abi == O32_ABI))
316f5878 10643 mips_32bitmode = 1;
e9670677
MR
10644
10645 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
10646 as_bad (_("trap exception not supported at ISA 1"));
10647
e9670677
MR
10648 /* If the selected architecture includes support for ASEs, enable
10649 generation of code for them. */
a4672219 10650 if (mips_opts.mips16 == -1)
fef14a42 10651 mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_arch)) ? 1 : 0;
ffdefa66 10652 if (mips_opts.ase_mips3d == -1)
fef14a42 10653 mips_opts.ase_mips3d = (CPU_HAS_MIPS3D (file_mips_arch)) ? 1 : 0;
ffdefa66 10654 if (mips_opts.ase_mdmx == -1)
fef14a42 10655 mips_opts.ase_mdmx = (CPU_HAS_MDMX (file_mips_arch)) ? 1 : 0;
e9670677 10656
e9670677 10657 file_mips_isa = mips_opts.isa;
a4672219 10658 file_ase_mips16 = mips_opts.mips16;
e9670677
MR
10659 file_ase_mips3d = mips_opts.ase_mips3d;
10660 file_ase_mdmx = mips_opts.ase_mdmx;
10661 mips_opts.gp32 = file_mips_gp32;
10662 mips_opts.fp32 = file_mips_fp32;
10663
ecb4347a
DJ
10664 if (mips_flag_mdebug < 0)
10665 {
10666#ifdef OBJ_MAYBE_ECOFF
10667 if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
10668 mips_flag_mdebug = 1;
10669 else
10670#endif /* OBJ_MAYBE_ECOFF */
10671 mips_flag_mdebug = 0;
10672 }
e9670677
MR
10673}
10674\f
10675void
17a2f251 10676mips_init_after_args (void)
252b5132
RH
10677{
10678 /* initialize opcodes */
10679 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
beae10d5 10680 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
252b5132
RH
10681}
10682
10683long
17a2f251 10684md_pcrel_from (fixS *fixP)
252b5132 10685{
a7ebbfdf
TS
10686 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
10687 switch (fixP->fx_r_type)
10688 {
10689 case BFD_RELOC_16_PCREL_S2:
10690 case BFD_RELOC_MIPS_JMP:
10691 /* Return the address of the delay slot. */
10692 return addr + 4;
10693 default:
10694 return addr;
10695 }
252b5132
RH
10696}
10697
252b5132
RH
10698/* This is called before the symbol table is processed. In order to
10699 work with gcc when using mips-tfile, we must keep all local labels.
10700 However, in other cases, we want to discard them. If we were
10701 called with -g, but we didn't see any debugging information, it may
10702 mean that gcc is smuggling debugging information through to
10703 mips-tfile, in which case we must generate all local labels. */
10704
10705void
17a2f251 10706mips_frob_file_before_adjust (void)
252b5132
RH
10707{
10708#ifndef NO_ECOFF_DEBUGGING
10709 if (ECOFF_DEBUGGING
10710 && mips_debug != 0
10711 && ! ecoff_debugging_seen)
10712 flag_keep_locals = 1;
10713#endif
10714}
10715
3b91255e
RS
10716/* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
10717 the corresponding LO16 reloc. This is called before md_apply_fix3 and
10718 tc_gen_reloc. Unmatched relocs can only be generated by use of explicit
10719 relocation operators.
10720
10721 For our purposes, a %lo() expression matches a %got() or %hi()
10722 expression if:
10723
10724 (a) it refers to the same symbol; and
10725 (b) the offset applied in the %lo() expression is no lower than
10726 the offset applied in the %got() or %hi().
10727
10728 (b) allows us to cope with code like:
10729
10730 lui $4,%hi(foo)
10731 lh $4,%lo(foo+2)($4)
10732
10733 ...which is legal on RELA targets, and has a well-defined behaviour
10734 if the user knows that adding 2 to "foo" will not induce a carry to
10735 the high 16 bits.
10736
10737 When several %lo()s match a particular %got() or %hi(), we use the
10738 following rules to distinguish them:
10739
10740 (1) %lo()s with smaller offsets are a better match than %lo()s with
10741 higher offsets.
10742
10743 (2) %lo()s with no matching %got() or %hi() are better than those
10744 that already have a matching %got() or %hi().
10745
10746 (3) later %lo()s are better than earlier %lo()s.
10747
10748 These rules are applied in order.
10749
10750 (1) means, among other things, that %lo()s with identical offsets are
10751 chosen if they exist.
10752
10753 (2) means that we won't associate several high-part relocations with
10754 the same low-part relocation unless there's no alternative. Having
10755 several high parts for the same low part is a GNU extension; this rule
10756 allows careful users to avoid it.
10757
10758 (3) is purely cosmetic. mips_hi_fixup_list is is in reverse order,
10759 with the last high-part relocation being at the front of the list.
10760 It therefore makes sense to choose the last matching low-part
10761 relocation, all other things being equal. It's also easier
10762 to code that way. */
252b5132
RH
10763
10764void
17a2f251 10765mips_frob_file (void)
252b5132
RH
10766{
10767 struct mips_hi_fixup *l;
10768
10769 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
10770 {
10771 segment_info_type *seginfo;
3b91255e
RS
10772 bfd_boolean matched_lo_p;
10773 fixS **hi_pos, **lo_pos, **pos;
252b5132 10774
5919d012 10775 assert (reloc_needs_lo_p (l->fixp->fx_r_type));
252b5132 10776
5919d012
RS
10777 /* If a GOT16 relocation turns out to be against a global symbol,
10778 there isn't supposed to be a matching LO. */
10779 if (l->fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
10780 && !pic_need_relax (l->fixp->fx_addsy, l->seg))
10781 continue;
10782
10783 /* Check quickly whether the next fixup happens to be a matching %lo. */
10784 if (fixup_has_matching_lo_p (l->fixp))
252b5132
RH
10785 continue;
10786
252b5132 10787 seginfo = seg_info (l->seg);
252b5132 10788
3b91255e
RS
10789 /* Set HI_POS to the position of this relocation in the chain.
10790 Set LO_POS to the position of the chosen low-part relocation.
10791 MATCHED_LO_P is true on entry to the loop if *POS is a low-part
10792 relocation that matches an immediately-preceding high-part
10793 relocation. */
10794 hi_pos = NULL;
10795 lo_pos = NULL;
10796 matched_lo_p = FALSE;
10797 for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
10798 {
10799 if (*pos == l->fixp)
10800 hi_pos = pos;
10801
10802 if ((*pos)->fx_r_type == BFD_RELOC_LO16
10803 && (*pos)->fx_addsy == l->fixp->fx_addsy
10804 && (*pos)->fx_offset >= l->fixp->fx_offset
10805 && (lo_pos == NULL
10806 || (*pos)->fx_offset < (*lo_pos)->fx_offset
10807 || (!matched_lo_p
10808 && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
10809 lo_pos = pos;
10810
10811 matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
10812 && fixup_has_matching_lo_p (*pos));
10813 }
10814
10815 /* If we found a match, remove the high-part relocation from its
10816 current position and insert it before the low-part relocation.
10817 Make the offsets match so that fixup_has_matching_lo_p()
10818 will return true.
10819
10820 We don't warn about unmatched high-part relocations since some
10821 versions of gcc have been known to emit dead "lui ...%hi(...)"
10822 instructions. */
10823 if (lo_pos != NULL)
10824 {
10825 l->fixp->fx_offset = (*lo_pos)->fx_offset;
10826 if (l->fixp->fx_next != *lo_pos)
252b5132 10827 {
3b91255e
RS
10828 *hi_pos = l->fixp->fx_next;
10829 l->fixp->fx_next = *lo_pos;
10830 *lo_pos = l->fixp;
252b5132 10831 }
252b5132
RH
10832 }
10833 }
10834}
10835
3e722fb5 10836/* We may have combined relocations without symbols in the N32/N64 ABI.
f6688943 10837 We have to prevent gas from dropping them. */
252b5132 10838
252b5132 10839int
17a2f251 10840mips_force_relocation (fixS *fixp)
252b5132 10841{
ae6063d4 10842 if (generic_force_reloc (fixp))
252b5132
RH
10843 return 1;
10844
f6688943
TS
10845 if (HAVE_NEWABI
10846 && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
10847 && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
10848 || fixp->fx_r_type == BFD_RELOC_HI16_S
10849 || fixp->fx_r_type == BFD_RELOC_LO16))
10850 return 1;
10851
3e722fb5 10852 return 0;
252b5132
RH
10853}
10854
45f8dfe8
AO
10855/* This hook is called before a fix is simplified. We don't really
10856 decide whether to skip a fix here. Rather, we turn global symbols
10857 used as branch targets into local symbols, such that they undergo
10858 simplification. We can only do this if the symbol is defined and
10859 it is in the same section as the branch. If this doesn't hold, we
10860 emit a better error message than just saying the relocation is not
10861 valid for the selected object format.
10862
10863 FIXP is the fix-up we're going to try to simplify, SEG is the
10864 segment in which the fix up occurs. The return value should be
10865 non-zero to indicate the fix-up is valid for further
10866 simplifications. */
10867
10868int
17a2f251 10869mips_validate_fix (struct fix *fixP, asection *seg)
45f8dfe8
AO
10870{
10871 /* There's a lot of discussion on whether it should be possible to
10872 use R_MIPS_PC16 to represent branch relocations. The outcome
10873 seems to be that it can, but gas/bfd are very broken in creating
10874 RELA relocations for this, so for now we only accept branches to
10875 symbols in the same section. Anything else is of dubious value,
10876 since there's no guarantee that at link time the symbol would be
10877 in range. Even for branches to local symbols this is arguably
10878 wrong, since it we assume the symbol is not going to be
10879 overridden, which should be possible per ELF library semantics,
10880 but then, there isn't a dynamic relocation that could be used to
10881 this effect, and the target would likely be out of range as well.
10882
10883 Unfortunately, it seems that there is too much code out there
10884 that relies on branches to symbols that are global to be resolved
10885 as if they were local, like the IRIX tools do, so we do it as
10886 well, but with a warning so that people are reminded to fix their
10887 code. If we ever get back to using R_MIPS_PC16 for branch
10888 targets, this entire block should go away (and probably the
10889 whole function). */
10890
10891 if (fixP->fx_r_type == BFD_RELOC_16_PCREL_S2
3e722fb5
CD
10892 && ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
10893 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
45f8dfe8
AO
10894 || bfd_reloc_type_lookup (stdoutput, BFD_RELOC_16_PCREL_S2) == NULL)
10895 && fixP->fx_addsy)
10896 {
10897 if (! S_IS_DEFINED (fixP->fx_addsy))
10898 {
10899 as_bad_where (fixP->fx_file, fixP->fx_line,
10900 _("Cannot branch to undefined symbol."));
10901 /* Avoid any further errors about this fixup. */
10902 fixP->fx_done = 1;
10903 }
10904 else if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
10905 {
10906 as_bad_where (fixP->fx_file, fixP->fx_line,
10907 _("Cannot branch to symbol in another section."));
10908 fixP->fx_done = 1;
10909 }
10910 else if (S_IS_EXTERNAL (fixP->fx_addsy))
10911 {
10912 symbolS *sym = fixP->fx_addsy;
10913
115695a8
CD
10914 if (mips_pic == SVR4_PIC)
10915 as_warn_where (fixP->fx_file, fixP->fx_line,
10916 _("Pretending global symbol used as branch target is local."));
45f8dfe8
AO
10917
10918 fixP->fx_addsy = symbol_create (S_GET_NAME (sym),
10919 S_GET_SEGMENT (sym),
10920 S_GET_VALUE (sym),
10921 symbol_get_frag (sym));
10922 copy_symbol_attributes (fixP->fx_addsy, sym);
10923 S_CLEAR_EXTERNAL (fixP->fx_addsy);
10924 assert (symbol_resolved_p (sym));
10925 symbol_mark_resolved (fixP->fx_addsy);
10926 }
10927 }
10928
10929 return 1;
10930}
10931
252b5132
RH
10932/* Apply a fixup to the object file. */
10933
94f592af 10934void
17a2f251 10935md_apply_fix3 (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 10936{
874e8986 10937 bfd_byte *buf;
98aa84af 10938 long insn;
a7ebbfdf 10939 reloc_howto_type *howto;
252b5132 10940
a7ebbfdf
TS
10941 /* We ignore generic BFD relocations we don't know about. */
10942 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
10943 if (! howto)
10944 return;
65551fa4 10945
252b5132
RH
10946 assert (fixP->fx_size == 4
10947 || fixP->fx_r_type == BFD_RELOC_16
10948 || fixP->fx_r_type == BFD_RELOC_64
f6688943
TS
10949 || fixP->fx_r_type == BFD_RELOC_CTOR
10950 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
252b5132 10951 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
a7ebbfdf 10952 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY);
252b5132 10953
a7ebbfdf 10954 buf = (bfd_byte *) (fixP->fx_frag->fr_literal + fixP->fx_where);
252b5132 10955
3e722fb5 10956 assert (! fixP->fx_pcrel);
b1dca8ee
RS
10957
10958 /* Don't treat parts of a composite relocation as done. There are two
10959 reasons for this:
10960
10961 (1) The second and third parts will be against 0 (RSS_UNDEF) but
10962 should nevertheless be emitted if the first part is.
10963
10964 (2) In normal usage, composite relocations are never assembly-time
10965 constants. The easiest way of dealing with the pathological
10966 exceptions is to generate a relocation against STN_UNDEF and
10967 leave everything up to the linker. */
10968 if (fixP->fx_addsy == NULL && fixP->fx_tcbit == 0)
252b5132
RH
10969 fixP->fx_done = 1;
10970
10971 switch (fixP->fx_r_type)
10972 {
10973 case BFD_RELOC_MIPS_JMP:
e369bcce
TS
10974 case BFD_RELOC_MIPS_SHIFT5:
10975 case BFD_RELOC_MIPS_SHIFT6:
10976 case BFD_RELOC_MIPS_GOT_DISP:
10977 case BFD_RELOC_MIPS_GOT_PAGE:
10978 case BFD_RELOC_MIPS_GOT_OFST:
10979 case BFD_RELOC_MIPS_SUB:
10980 case BFD_RELOC_MIPS_INSERT_A:
10981 case BFD_RELOC_MIPS_INSERT_B:
10982 case BFD_RELOC_MIPS_DELETE:
10983 case BFD_RELOC_MIPS_HIGHEST:
10984 case BFD_RELOC_MIPS_HIGHER:
10985 case BFD_RELOC_MIPS_SCN_DISP:
10986 case BFD_RELOC_MIPS_REL16:
10987 case BFD_RELOC_MIPS_RELGOT:
10988 case BFD_RELOC_MIPS_JALR:
252b5132
RH
10989 case BFD_RELOC_HI16:
10990 case BFD_RELOC_HI16_S:
cdf6fd85 10991 case BFD_RELOC_GPREL16:
252b5132
RH
10992 case BFD_RELOC_MIPS_LITERAL:
10993 case BFD_RELOC_MIPS_CALL16:
10994 case BFD_RELOC_MIPS_GOT16:
cdf6fd85 10995 case BFD_RELOC_GPREL32:
252b5132
RH
10996 case BFD_RELOC_MIPS_GOT_HI16:
10997 case BFD_RELOC_MIPS_GOT_LO16:
10998 case BFD_RELOC_MIPS_CALL_HI16:
10999 case BFD_RELOC_MIPS_CALL_LO16:
11000 case BFD_RELOC_MIPS16_GPREL:
3e722fb5 11001 assert (! fixP->fx_pcrel);
252b5132
RH
11002 /* Nothing needed to do. The value comes from the reloc entry */
11003 break;
11004
11005 case BFD_RELOC_MIPS16_JMP:
11006 /* We currently always generate a reloc against a symbol, which
11007 means that we don't want an addend even if the symbol is
11008 defined. */
a7ebbfdf 11009 *valP = 0;
252b5132
RH
11010 break;
11011
252b5132
RH
11012 case BFD_RELOC_64:
11013 /* This is handled like BFD_RELOC_32, but we output a sign
11014 extended value if we are only 32 bits. */
3e722fb5 11015 if (fixP->fx_done)
252b5132
RH
11016 {
11017 if (8 <= sizeof (valueT))
a7ebbfdf 11018 md_number_to_chars (buf, *valP, 8);
252b5132
RH
11019 else
11020 {
a7ebbfdf 11021 valueT hiv;
252b5132 11022
a7ebbfdf 11023 if ((*valP & 0x80000000) != 0)
252b5132
RH
11024 hiv = 0xffffffff;
11025 else
11026 hiv = 0;
a7ebbfdf
TS
11027 md_number_to_chars ((char *)(buf + target_big_endian ? 4 : 0),
11028 *valP, 4);
11029 md_number_to_chars ((char *)(buf + target_big_endian ? 0 : 4),
11030 hiv, 4);
252b5132
RH
11031 }
11032 }
11033 break;
11034
056350c6 11035 case BFD_RELOC_RVA:
252b5132
RH
11036 case BFD_RELOC_32:
11037 /* If we are deleting this reloc entry, we must fill in the
11038 value now. This can happen if we have a .word which is not
3e722fb5
CD
11039 resolved when it appears but is later defined. */
11040 if (fixP->fx_done)
a7ebbfdf 11041 md_number_to_chars (buf, *valP, 4);
252b5132
RH
11042 break;
11043
11044 case BFD_RELOC_16:
11045 /* If we are deleting this reloc entry, we must fill in the
11046 value now. */
252b5132 11047 if (fixP->fx_done)
a7ebbfdf 11048 md_number_to_chars (buf, *valP, 2);
252b5132
RH
11049 break;
11050
11051 case BFD_RELOC_LO16:
3e722fb5
CD
11052 /* FIXME: Now that embedded-PIC is gone, some of this code/comment
11053 may be safe to remove, but if so it's not obvious. */
252b5132
RH
11054 /* When handling an embedded PIC switch statement, we can wind
11055 up deleting a LO16 reloc. See the 'o' case in mips_ip. */
11056 if (fixP->fx_done)
11057 {
a7ebbfdf 11058 if (*valP + 0x8000 > 0xffff)
252b5132
RH
11059 as_bad_where (fixP->fx_file, fixP->fx_line,
11060 _("relocation overflow"));
252b5132
RH
11061 if (target_big_endian)
11062 buf += 2;
17a2f251 11063 md_number_to_chars (buf, *valP, 2);
252b5132
RH
11064 }
11065 break;
11066
11067 case BFD_RELOC_16_PCREL_S2:
a7ebbfdf 11068 if ((*valP & 0x3) != 0)
cb56d3d3 11069 as_bad_where (fixP->fx_file, fixP->fx_line,
a7ebbfdf 11070 _("Branch to odd address (%lx)"), (long) *valP);
cb56d3d3 11071
252b5132
RH
11072 /*
11073 * We need to save the bits in the instruction since fixup_segment()
11074 * might be deleting the relocation entry (i.e., a branch within
11075 * the current segment).
11076 */
a7ebbfdf 11077 if (! fixP->fx_done)
bb2d6cd7 11078 break;
252b5132
RH
11079
11080 /* update old instruction data */
252b5132
RH
11081 if (target_big_endian)
11082 insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
11083 else
11084 insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
11085
a7ebbfdf
TS
11086 if (*valP + 0x20000 <= 0x3ffff)
11087 {
11088 insn |= (*valP >> 2) & 0xffff;
17a2f251 11089 md_number_to_chars (buf, insn, 4);
a7ebbfdf
TS
11090 }
11091 else if (mips_pic == NO_PIC
11092 && fixP->fx_done
11093 && fixP->fx_frag->fr_address >= text_section->vma
11094 && (fixP->fx_frag->fr_address
587aac4e 11095 < text_section->vma + bfd_get_section_size (text_section))
a7ebbfdf
TS
11096 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
11097 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
11098 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
252b5132
RH
11099 {
11100 /* The branch offset is too large. If this is an
11101 unconditional branch, and we are not generating PIC code,
11102 we can convert it to an absolute jump instruction. */
a7ebbfdf
TS
11103 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
11104 insn = 0x0c000000; /* jal */
252b5132 11105 else
a7ebbfdf
TS
11106 insn = 0x08000000; /* j */
11107 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
11108 fixP->fx_done = 0;
11109 fixP->fx_addsy = section_symbol (text_section);
11110 *valP += md_pcrel_from (fixP);
17a2f251 11111 md_number_to_chars (buf, insn, 4);
a7ebbfdf
TS
11112 }
11113 else
11114 {
11115 /* If we got here, we have branch-relaxation disabled,
11116 and there's nothing we can do to fix this instruction
11117 without turning it into a longer sequence. */
11118 as_bad_where (fixP->fx_file, fixP->fx_line,
11119 _("Branch out of range"));
252b5132 11120 }
252b5132
RH
11121 break;
11122
11123 case BFD_RELOC_VTABLE_INHERIT:
11124 fixP->fx_done = 0;
11125 if (fixP->fx_addsy
11126 && !S_IS_DEFINED (fixP->fx_addsy)
11127 && !S_IS_WEAK (fixP->fx_addsy))
11128 S_SET_WEAK (fixP->fx_addsy);
11129 break;
11130
11131 case BFD_RELOC_VTABLE_ENTRY:
11132 fixP->fx_done = 0;
11133 break;
11134
11135 default:
11136 internalError ();
11137 }
a7ebbfdf
TS
11138
11139 /* Remember value for tc_gen_reloc. */
11140 fixP->fx_addnumber = *valP;
252b5132
RH
11141}
11142
11143#if 0
11144void
17a2f251 11145printInsn (unsigned long oc)
252b5132
RH
11146{
11147 const struct mips_opcode *p;
11148 int treg, sreg, dreg, shamt;
11149 short imm;
11150 const char *args;
11151 int i;
11152
11153 for (i = 0; i < NUMOPCODES; ++i)
11154 {
11155 p = &mips_opcodes[i];
11156 if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
11157 {
11158 printf ("%08lx %s\t", oc, p->name);
11159 treg = (oc >> 16) & 0x1f;
11160 sreg = (oc >> 21) & 0x1f;
11161 dreg = (oc >> 11) & 0x1f;
11162 shamt = (oc >> 6) & 0x1f;
11163 imm = oc;
11164 for (args = p->args;; ++args)
11165 {
11166 switch (*args)
11167 {
11168 case '\0':
11169 printf ("\n");
11170 break;
11171
11172 case ',':
11173 case '(':
11174 case ')':
11175 printf ("%c", *args);
11176 continue;
11177
11178 case 'r':
11179 assert (treg == sreg);
11180 printf ("$%d,$%d", treg, sreg);
11181 continue;
11182
11183 case 'd':
11184 case 'G':
11185 printf ("$%d", dreg);
11186 continue;
11187
11188 case 't':
11189 case 'E':
11190 printf ("$%d", treg);
11191 continue;
11192
11193 case 'k':
11194 printf ("0x%x", treg);
11195 continue;
11196
11197 case 'b':
11198 case 's':
11199 printf ("$%d", sreg);
11200 continue;
11201
11202 case 'a':
11203 printf ("0x%08lx", oc & 0x1ffffff);
11204 continue;
11205
11206 case 'i':
11207 case 'j':
11208 case 'o':
11209 case 'u':
11210 printf ("%d", imm);
11211 continue;
11212
11213 case '<':
11214 case '>':
11215 printf ("$%d", shamt);
11216 continue;
11217
11218 default:
11219 internalError ();
11220 }
11221 break;
11222 }
11223 return;
11224 }
11225 }
11226 printf (_("%08lx UNDEFINED\n"), oc);
11227}
11228#endif
11229
11230static symbolS *
17a2f251 11231get_symbol (void)
252b5132
RH
11232{
11233 int c;
11234 char *name;
11235 symbolS *p;
11236
11237 name = input_line_pointer;
11238 c = get_symbol_end ();
11239 p = (symbolS *) symbol_find_or_make (name);
11240 *input_line_pointer = c;
11241 return p;
11242}
11243
11244/* Align the current frag to a given power of two. The MIPS assembler
11245 also automatically adjusts any preceding label. */
11246
11247static void
17a2f251 11248mips_align (int to, int fill, symbolS *label)
252b5132 11249{
b34976b6 11250 mips_emit_delays (FALSE);
252b5132
RH
11251 frag_align (to, fill, 0);
11252 record_alignment (now_seg, to);
11253 if (label != NULL)
11254 {
11255 assert (S_GET_SEGMENT (label) == now_seg);
49309057 11256 symbol_set_frag (label, frag_now);
252b5132
RH
11257 S_SET_VALUE (label, (valueT) frag_now_fix ());
11258 }
11259}
11260
11261/* Align to a given power of two. .align 0 turns off the automatic
11262 alignment used by the data creating pseudo-ops. */
11263
11264static void
17a2f251 11265s_align (int x ATTRIBUTE_UNUSED)
252b5132
RH
11266{
11267 register int temp;
11268 register long temp_fill;
11269 long max_alignment = 15;
11270
11271 /*
11272
67c1ffbe 11273 o Note that the assembler pulls down any immediately preceding label
252b5132
RH
11274 to the aligned address.
11275 o It's not documented but auto alignment is reinstated by
11276 a .align pseudo instruction.
11277 o Note also that after auto alignment is turned off the mips assembler
11278 issues an error on attempt to assemble an improperly aligned data item.
11279 We don't.
11280
11281 */
11282
11283 temp = get_absolute_expression ();
11284 if (temp > max_alignment)
11285 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
11286 else if (temp < 0)
11287 {
11288 as_warn (_("Alignment negative: 0 assumed."));
11289 temp = 0;
11290 }
11291 if (*input_line_pointer == ',')
11292 {
f9419b05 11293 ++input_line_pointer;
252b5132
RH
11294 temp_fill = get_absolute_expression ();
11295 }
11296 else
11297 temp_fill = 0;
11298 if (temp)
11299 {
11300 auto_align = 1;
11301 mips_align (temp, (int) temp_fill,
11302 insn_labels != NULL ? insn_labels->label : NULL);
11303 }
11304 else
11305 {
11306 auto_align = 0;
11307 }
11308
11309 demand_empty_rest_of_line ();
11310}
11311
11312void
17a2f251 11313mips_flush_pending_output (void)
252b5132 11314{
b34976b6 11315 mips_emit_delays (FALSE);
252b5132
RH
11316 mips_clear_insn_labels ();
11317}
11318
11319static void
17a2f251 11320s_change_sec (int sec)
252b5132
RH
11321{
11322 segT seg;
11323
252b5132
RH
11324#ifdef OBJ_ELF
11325 /* The ELF backend needs to know that we are changing sections, so
11326 that .previous works correctly. We could do something like check
b6ff326e 11327 for an obj_section_change_hook macro, but that might be confusing
252b5132
RH
11328 as it would not be appropriate to use it in the section changing
11329 functions in read.c, since obj-elf.c intercepts those. FIXME:
11330 This should be cleaner, somehow. */
11331 obj_elf_section_change_hook ();
11332#endif
11333
b34976b6 11334 mips_emit_delays (FALSE);
252b5132
RH
11335 switch (sec)
11336 {
11337 case 't':
11338 s_text (0);
11339 break;
11340 case 'd':
11341 s_data (0);
11342 break;
11343 case 'b':
11344 subseg_set (bss_section, (subsegT) get_absolute_expression ());
11345 demand_empty_rest_of_line ();
11346 break;
11347
11348 case 'r':
4d0d148d
TS
11349 seg = subseg_new (RDATA_SECTION_NAME,
11350 (subsegT) get_absolute_expression ());
11351 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
252b5132 11352 {
4d0d148d
TS
11353 bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
11354 | SEC_READONLY | SEC_RELOC
11355 | SEC_DATA));
11356 if (strcmp (TARGET_OS, "elf") != 0)
11357 record_alignment (seg, 4);
252b5132 11358 }
4d0d148d 11359 demand_empty_rest_of_line ();
252b5132
RH
11360 break;
11361
11362 case 's':
4d0d148d
TS
11363 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
11364 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
252b5132 11365 {
4d0d148d
TS
11366 bfd_set_section_flags (stdoutput, seg,
11367 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
11368 if (strcmp (TARGET_OS, "elf") != 0)
11369 record_alignment (seg, 4);
252b5132 11370 }
4d0d148d
TS
11371 demand_empty_rest_of_line ();
11372 break;
252b5132
RH
11373 }
11374
11375 auto_align = 1;
11376}
b34976b6 11377
cca86cc8 11378void
17a2f251 11379s_change_section (int ignore ATTRIBUTE_UNUSED)
cca86cc8 11380{
7ed4a06a 11381#ifdef OBJ_ELF
cca86cc8
SC
11382 char *section_name;
11383 char c;
684022ea 11384 char next_c = 0;
cca86cc8
SC
11385 int section_type;
11386 int section_flag;
11387 int section_entry_size;
11388 int section_alignment;
b34976b6 11389
7ed4a06a
TS
11390 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11391 return;
11392
cca86cc8
SC
11393 section_name = input_line_pointer;
11394 c = get_symbol_end ();
a816d1ed
AO
11395 if (c)
11396 next_c = *(input_line_pointer + 1);
cca86cc8 11397
4cf0dd0d
TS
11398 /* Do we have .section Name<,"flags">? */
11399 if (c != ',' || (c == ',' && next_c == '"'))
cca86cc8 11400 {
4cf0dd0d
TS
11401 /* just after name is now '\0'. */
11402 *input_line_pointer = c;
cca86cc8
SC
11403 input_line_pointer = section_name;
11404 obj_elf_section (ignore);
11405 return;
11406 }
11407 input_line_pointer++;
11408
11409 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
11410 if (c == ',')
11411 section_type = get_absolute_expression ();
11412 else
11413 section_type = 0;
11414 if (*input_line_pointer++ == ',')
11415 section_flag = get_absolute_expression ();
11416 else
11417 section_flag = 0;
11418 if (*input_line_pointer++ == ',')
11419 section_entry_size = get_absolute_expression ();
11420 else
11421 section_entry_size = 0;
11422 if (*input_line_pointer++ == ',')
11423 section_alignment = get_absolute_expression ();
11424 else
11425 section_alignment = 0;
11426
a816d1ed
AO
11427 section_name = xstrdup (section_name);
11428
8ab8a5c8
RS
11429 /* When using the generic form of .section (as implemented by obj-elf.c),
11430 there's no way to set the section type to SHT_MIPS_DWARF. Users have
11431 traditionally had to fall back on the more common @progbits instead.
11432
11433 There's nothing really harmful in this, since bfd will correct
11434 SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file. But it
11435 means that, for backwards compatibiltiy, the special_section entries
11436 for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
11437
11438 Even so, we shouldn't force users of the MIPS .section syntax to
11439 incorrectly label the sections as SHT_PROGBITS. The best compromise
11440 seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
11441 generic type-checking code. */
11442 if (section_type == SHT_MIPS_DWARF)
11443 section_type = SHT_PROGBITS;
11444
cca86cc8
SC
11445 obj_elf_change_section (section_name, section_type, section_flag,
11446 section_entry_size, 0, 0, 0);
a816d1ed
AO
11447
11448 if (now_seg->name != section_name)
11449 free (section_name);
7ed4a06a 11450#endif /* OBJ_ELF */
cca86cc8 11451}
252b5132
RH
11452
11453void
17a2f251 11454mips_enable_auto_align (void)
252b5132
RH
11455{
11456 auto_align = 1;
11457}
11458
11459static void
17a2f251 11460s_cons (int log_size)
252b5132
RH
11461{
11462 symbolS *label;
11463
11464 label = insn_labels != NULL ? insn_labels->label : NULL;
b34976b6 11465 mips_emit_delays (FALSE);
252b5132
RH
11466 if (log_size > 0 && auto_align)
11467 mips_align (log_size, 0, label);
11468 mips_clear_insn_labels ();
11469 cons (1 << log_size);
11470}
11471
11472static void
17a2f251 11473s_float_cons (int type)
252b5132
RH
11474{
11475 symbolS *label;
11476
11477 label = insn_labels != NULL ? insn_labels->label : NULL;
11478
b34976b6 11479 mips_emit_delays (FALSE);
252b5132
RH
11480
11481 if (auto_align)
49309057
ILT
11482 {
11483 if (type == 'd')
11484 mips_align (3, 0, label);
11485 else
11486 mips_align (2, 0, label);
11487 }
252b5132
RH
11488
11489 mips_clear_insn_labels ();
11490
11491 float_cons (type);
11492}
11493
11494/* Handle .globl. We need to override it because on Irix 5 you are
11495 permitted to say
11496 .globl foo .text
11497 where foo is an undefined symbol, to mean that foo should be
11498 considered to be the address of a function. */
11499
11500static void
17a2f251 11501s_mips_globl (int x ATTRIBUTE_UNUSED)
252b5132
RH
11502{
11503 char *name;
11504 int c;
11505 symbolS *symbolP;
11506 flagword flag;
11507
11508 name = input_line_pointer;
11509 c = get_symbol_end ();
11510 symbolP = symbol_find_or_make (name);
11511 *input_line_pointer = c;
11512 SKIP_WHITESPACE ();
11513
11514 /* On Irix 5, every global symbol that is not explicitly labelled as
11515 being a function is apparently labelled as being an object. */
11516 flag = BSF_OBJECT;
11517
11518 if (! is_end_of_line[(unsigned char) *input_line_pointer])
11519 {
11520 char *secname;
11521 asection *sec;
11522
11523 secname = input_line_pointer;
11524 c = get_symbol_end ();
11525 sec = bfd_get_section_by_name (stdoutput, secname);
11526 if (sec == NULL)
11527 as_bad (_("%s: no such section"), secname);
11528 *input_line_pointer = c;
11529
11530 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
11531 flag = BSF_FUNCTION;
11532 }
11533
49309057 11534 symbol_get_bfdsym (symbolP)->flags |= flag;
252b5132
RH
11535
11536 S_SET_EXTERNAL (symbolP);
11537 demand_empty_rest_of_line ();
11538}
11539
11540static void
17a2f251 11541s_option (int x ATTRIBUTE_UNUSED)
252b5132
RH
11542{
11543 char *opt;
11544 char c;
11545
11546 opt = input_line_pointer;
11547 c = get_symbol_end ();
11548
11549 if (*opt == 'O')
11550 {
11551 /* FIXME: What does this mean? */
11552 }
11553 else if (strncmp (opt, "pic", 3) == 0)
11554 {
11555 int i;
11556
11557 i = atoi (opt + 3);
11558 if (i == 0)
11559 mips_pic = NO_PIC;
11560 else if (i == 2)
143d77c5 11561 {
252b5132 11562 mips_pic = SVR4_PIC;
143d77c5
EC
11563 mips_abicalls = TRUE;
11564 }
252b5132
RH
11565 else
11566 as_bad (_(".option pic%d not supported"), i);
11567
4d0d148d 11568 if (mips_pic == SVR4_PIC)
252b5132
RH
11569 {
11570 if (g_switch_seen && g_switch_value != 0)
11571 as_warn (_("-G may not be used with SVR4 PIC code"));
11572 g_switch_value = 0;
11573 bfd_set_gp_size (stdoutput, 0);
11574 }
11575 }
11576 else
11577 as_warn (_("Unrecognized option \"%s\""), opt);
11578
11579 *input_line_pointer = c;
11580 demand_empty_rest_of_line ();
11581}
11582
11583/* This structure is used to hold a stack of .set values. */
11584
e972090a
NC
11585struct mips_option_stack
11586{
252b5132
RH
11587 struct mips_option_stack *next;
11588 struct mips_set_options options;
11589};
11590
11591static struct mips_option_stack *mips_opts_stack;
11592
11593/* Handle the .set pseudo-op. */
11594
11595static void
17a2f251 11596s_mipsset (int x ATTRIBUTE_UNUSED)
252b5132
RH
11597{
11598 char *name = input_line_pointer, ch;
11599
11600 while (!is_end_of_line[(unsigned char) *input_line_pointer])
f9419b05 11601 ++input_line_pointer;
252b5132
RH
11602 ch = *input_line_pointer;
11603 *input_line_pointer = '\0';
11604
11605 if (strcmp (name, "reorder") == 0)
11606 {
11607 if (mips_opts.noreorder && prev_nop_frag != NULL)
11608 {
11609 /* If we still have pending nops, we can discard them. The
11610 usual nop handling will insert any that are still
bdaaa2e1 11611 needed. */
252b5132
RH
11612 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11613 * (mips_opts.mips16 ? 2 : 4));
11614 prev_nop_frag = NULL;
11615 }
11616 mips_opts.noreorder = 0;
11617 }
11618 else if (strcmp (name, "noreorder") == 0)
11619 {
b34976b6 11620 mips_emit_delays (TRUE);
252b5132
RH
11621 mips_opts.noreorder = 1;
11622 mips_any_noreorder = 1;
11623 }
11624 else if (strcmp (name, "at") == 0)
11625 {
11626 mips_opts.noat = 0;
11627 }
11628 else if (strcmp (name, "noat") == 0)
11629 {
11630 mips_opts.noat = 1;
11631 }
11632 else if (strcmp (name, "macro") == 0)
11633 {
11634 mips_opts.warn_about_macros = 0;
11635 }
11636 else if (strcmp (name, "nomacro") == 0)
11637 {
11638 if (mips_opts.noreorder == 0)
11639 as_bad (_("`noreorder' must be set before `nomacro'"));
11640 mips_opts.warn_about_macros = 1;
11641 }
11642 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
11643 {
11644 mips_opts.nomove = 0;
11645 }
11646 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
11647 {
11648 mips_opts.nomove = 1;
11649 }
11650 else if (strcmp (name, "bopt") == 0)
11651 {
11652 mips_opts.nobopt = 0;
11653 }
11654 else if (strcmp (name, "nobopt") == 0)
11655 {
11656 mips_opts.nobopt = 1;
11657 }
11658 else if (strcmp (name, "mips16") == 0
11659 || strcmp (name, "MIPS-16") == 0)
11660 mips_opts.mips16 = 1;
11661 else if (strcmp (name, "nomips16") == 0
11662 || strcmp (name, "noMIPS-16") == 0)
11663 mips_opts.mips16 = 0;
1f25f5d3
CD
11664 else if (strcmp (name, "mips3d") == 0)
11665 mips_opts.ase_mips3d = 1;
11666 else if (strcmp (name, "nomips3d") == 0)
11667 mips_opts.ase_mips3d = 0;
a4672219
TS
11668 else if (strcmp (name, "mdmx") == 0)
11669 mips_opts.ase_mdmx = 1;
11670 else if (strcmp (name, "nomdmx") == 0)
11671 mips_opts.ase_mdmx = 0;
1a2c1fad 11672 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
252b5132 11673 {
af7ee8bf 11674 int reset = 0;
252b5132 11675
1a2c1fad
CD
11676 /* Permit the user to change the ISA and architecture on the fly.
11677 Needless to say, misuse can cause serious problems. */
81a21e38 11678 if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
af7ee8bf
CD
11679 {
11680 reset = 1;
11681 mips_opts.isa = file_mips_isa;
1a2c1fad 11682 mips_opts.arch = file_mips_arch;
1a2c1fad
CD
11683 }
11684 else if (strncmp (name, "arch=", 5) == 0)
11685 {
11686 const struct mips_cpu_info *p;
11687
11688 p = mips_parse_cpu("internal use", name + 5);
11689 if (!p)
11690 as_bad (_("unknown architecture %s"), name + 5);
11691 else
11692 {
11693 mips_opts.arch = p->cpu;
11694 mips_opts.isa = p->isa;
11695 }
11696 }
81a21e38
TS
11697 else if (strncmp (name, "mips", 4) == 0)
11698 {
11699 const struct mips_cpu_info *p;
11700
11701 p = mips_parse_cpu("internal use", name);
11702 if (!p)
11703 as_bad (_("unknown ISA level %s"), name + 4);
11704 else
11705 {
11706 mips_opts.arch = p->cpu;
11707 mips_opts.isa = p->isa;
11708 }
11709 }
af7ee8bf 11710 else
81a21e38 11711 as_bad (_("unknown ISA or architecture %s"), name);
af7ee8bf
CD
11712
11713 switch (mips_opts.isa)
98d3f06f
KH
11714 {
11715 case 0:
98d3f06f 11716 break;
af7ee8bf
CD
11717 case ISA_MIPS1:
11718 case ISA_MIPS2:
11719 case ISA_MIPS32:
11720 case ISA_MIPS32R2:
98d3f06f
KH
11721 mips_opts.gp32 = 1;
11722 mips_opts.fp32 = 1;
11723 break;
af7ee8bf
CD
11724 case ISA_MIPS3:
11725 case ISA_MIPS4:
11726 case ISA_MIPS5:
11727 case ISA_MIPS64:
5f74bc13 11728 case ISA_MIPS64R2:
98d3f06f
KH
11729 mips_opts.gp32 = 0;
11730 mips_opts.fp32 = 0;
11731 break;
11732 default:
11733 as_bad (_("unknown ISA level %s"), name + 4);
11734 break;
11735 }
af7ee8bf 11736 if (reset)
98d3f06f 11737 {
af7ee8bf
CD
11738 mips_opts.gp32 = file_mips_gp32;
11739 mips_opts.fp32 = file_mips_fp32;
98d3f06f 11740 }
252b5132
RH
11741 }
11742 else if (strcmp (name, "autoextend") == 0)
11743 mips_opts.noautoextend = 0;
11744 else if (strcmp (name, "noautoextend") == 0)
11745 mips_opts.noautoextend = 1;
11746 else if (strcmp (name, "push") == 0)
11747 {
11748 struct mips_option_stack *s;
11749
11750 s = (struct mips_option_stack *) xmalloc (sizeof *s);
11751 s->next = mips_opts_stack;
11752 s->options = mips_opts;
11753 mips_opts_stack = s;
11754 }
11755 else if (strcmp (name, "pop") == 0)
11756 {
11757 struct mips_option_stack *s;
11758
11759 s = mips_opts_stack;
11760 if (s == NULL)
11761 as_bad (_(".set pop with no .set push"));
11762 else
11763 {
11764 /* If we're changing the reorder mode we need to handle
11765 delay slots correctly. */
11766 if (s->options.noreorder && ! mips_opts.noreorder)
b34976b6 11767 mips_emit_delays (TRUE);
252b5132
RH
11768 else if (! s->options.noreorder && mips_opts.noreorder)
11769 {
11770 if (prev_nop_frag != NULL)
11771 {
11772 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11773 * (mips_opts.mips16 ? 2 : 4));
11774 prev_nop_frag = NULL;
11775 }
11776 }
11777
11778 mips_opts = s->options;
11779 mips_opts_stack = s->next;
11780 free (s);
11781 }
11782 }
11783 else
11784 {
11785 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
11786 }
11787 *input_line_pointer = ch;
11788 demand_empty_rest_of_line ();
11789}
11790
11791/* Handle the .abicalls pseudo-op. I believe this is equivalent to
11792 .option pic2. It means to generate SVR4 PIC calls. */
11793
11794static void
17a2f251 11795s_abicalls (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
11796{
11797 mips_pic = SVR4_PIC;
143d77c5 11798 mips_abicalls = TRUE;
4d0d148d
TS
11799
11800 if (g_switch_seen && g_switch_value != 0)
11801 as_warn (_("-G may not be used with SVR4 PIC code"));
11802 g_switch_value = 0;
11803
252b5132
RH
11804 bfd_set_gp_size (stdoutput, 0);
11805 demand_empty_rest_of_line ();
11806}
11807
11808/* Handle the .cpload pseudo-op. This is used when generating SVR4
11809 PIC code. It sets the $gp register for the function based on the
11810 function address, which is in the register named in the argument.
11811 This uses a relocation against _gp_disp, which is handled specially
11812 by the linker. The result is:
11813 lui $gp,%hi(_gp_disp)
11814 addiu $gp,$gp,%lo(_gp_disp)
11815 addu $gp,$gp,.cpload argument
aa6975fb
ILT
11816 The .cpload argument is normally $25 == $t9.
11817
11818 The -mno-shared option changes this to:
11819 lui $gp,%hi(_gp)
11820 addiu $gp,$gp,%lo(_gp)
11821 and the argument is ignored. This saves an instruction, but the
11822 resulting code is not position independent; it uses an absolute
11823 address for _gp. Thus code assembled with -mno-shared can go into
11824 an ordinary executable, but not into a shared library. */
252b5132
RH
11825
11826static void
17a2f251 11827s_cpload (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
11828{
11829 expressionS ex;
aa6975fb
ILT
11830 int reg;
11831 int in_shared;
252b5132 11832
6478892d
TS
11833 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
11834 .cpload is ignored. */
11835 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
11836 {
11837 s_ignore (0);
11838 return;
11839 }
11840
d3ecfc59 11841 /* .cpload should be in a .set noreorder section. */
252b5132
RH
11842 if (mips_opts.noreorder == 0)
11843 as_warn (_(".cpload not in noreorder section"));
11844
aa6975fb
ILT
11845 reg = tc_get_register (0);
11846
11847 /* If we need to produce a 64-bit address, we are better off using
11848 the default instruction sequence. */
11849 in_shared = mips_in_shared || HAVE_64BIT_ADDRESSES;
11850
252b5132 11851 ex.X_op = O_symbol;
aa6975fb 11852 ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" : "_gp");
252b5132
RH
11853 ex.X_op_symbol = NULL;
11854 ex.X_add_number = 0;
11855
11856 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
49309057 11857 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
252b5132 11858
584892a6 11859 macro_start ();
67c0d1eb
RS
11860 macro_build_lui (&ex, mips_gp_register);
11861 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
17a2f251 11862 mips_gp_register, BFD_RELOC_LO16);
aa6975fb
ILT
11863 if (in_shared)
11864 macro_build (NULL, "addu", "d,v,t", mips_gp_register,
11865 mips_gp_register, reg);
584892a6 11866 macro_end ();
252b5132
RH
11867
11868 demand_empty_rest_of_line ();
11869}
11870
6478892d
TS
11871/* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
11872 .cpsetup $reg1, offset|$reg2, label
11873
11874 If offset is given, this results in:
11875 sd $gp, offset($sp)
956cd1d6 11876 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
11877 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
11878 daddu $gp, $gp, $reg1
6478892d
TS
11879
11880 If $reg2 is given, this results in:
11881 daddu $reg2, $gp, $0
956cd1d6 11882 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
11883 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
11884 daddu $gp, $gp, $reg1
aa6975fb
ILT
11885 $reg1 is normally $25 == $t9.
11886
11887 The -mno-shared option replaces the last three instructions with
11888 lui $gp,%hi(_gp)
11889 addiu $gp,$gp,%lo(_gp)
11890 */
11891
6478892d 11892static void
17a2f251 11893s_cpsetup (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
11894{
11895 expressionS ex_off;
11896 expressionS ex_sym;
11897 int reg1;
6478892d 11898
8586fc66 11899 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
6478892d
TS
11900 We also need NewABI support. */
11901 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11902 {
11903 s_ignore (0);
11904 return;
11905 }
11906
11907 reg1 = tc_get_register (0);
11908 SKIP_WHITESPACE ();
11909 if (*input_line_pointer != ',')
11910 {
11911 as_bad (_("missing argument separator ',' for .cpsetup"));
11912 return;
11913 }
11914 else
80245285 11915 ++input_line_pointer;
6478892d
TS
11916 SKIP_WHITESPACE ();
11917 if (*input_line_pointer == '$')
80245285
TS
11918 {
11919 mips_cpreturn_register = tc_get_register (0);
11920 mips_cpreturn_offset = -1;
11921 }
6478892d 11922 else
80245285
TS
11923 {
11924 mips_cpreturn_offset = get_absolute_expression ();
11925 mips_cpreturn_register = -1;
11926 }
6478892d
TS
11927 SKIP_WHITESPACE ();
11928 if (*input_line_pointer != ',')
11929 {
11930 as_bad (_("missing argument separator ',' for .cpsetup"));
11931 return;
11932 }
11933 else
f9419b05 11934 ++input_line_pointer;
6478892d 11935 SKIP_WHITESPACE ();
f21f8242 11936 expression (&ex_sym);
6478892d 11937
584892a6 11938 macro_start ();
6478892d
TS
11939 if (mips_cpreturn_register == -1)
11940 {
11941 ex_off.X_op = O_constant;
11942 ex_off.X_add_symbol = NULL;
11943 ex_off.X_op_symbol = NULL;
11944 ex_off.X_add_number = mips_cpreturn_offset;
11945
67c0d1eb 11946 macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
17a2f251 11947 BFD_RELOC_LO16, SP);
6478892d
TS
11948 }
11949 else
67c0d1eb 11950 macro_build (NULL, "daddu", "d,v,t", mips_cpreturn_register,
17a2f251 11951 mips_gp_register, 0);
6478892d 11952
aa6975fb
ILT
11953 if (mips_in_shared || HAVE_64BIT_ADDRESSES)
11954 {
11955 macro_build (&ex_sym, "lui", "t,u", mips_gp_register,
11956 -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
11957 BFD_RELOC_HI16_S);
11958
11959 macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
11960 mips_gp_register, -1, BFD_RELOC_GPREL16,
11961 BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
11962
11963 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
11964 mips_gp_register, reg1);
11965 }
11966 else
11967 {
11968 expressionS ex;
11969
11970 ex.X_op = O_symbol;
11971 ex.X_add_symbol = symbol_find_or_make ("_gp");
11972 ex.X_op_symbol = NULL;
11973 ex.X_add_number = 0;
6e1304d8 11974
aa6975fb
ILT
11975 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
11976 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
11977
11978 macro_build_lui (&ex, mips_gp_register);
11979 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
11980 mips_gp_register, BFD_RELOC_LO16);
11981 }
f21f8242 11982
584892a6 11983 macro_end ();
6478892d
TS
11984
11985 demand_empty_rest_of_line ();
11986}
11987
11988static void
17a2f251 11989s_cplocal (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
11990{
11991 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
11992 .cplocal is ignored. */
11993 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11994 {
11995 s_ignore (0);
11996 return;
11997 }
11998
11999 mips_gp_register = tc_get_register (0);
85b51719 12000 demand_empty_rest_of_line ();
6478892d
TS
12001}
12002
252b5132
RH
12003/* Handle the .cprestore pseudo-op. This stores $gp into a given
12004 offset from $sp. The offset is remembered, and after making a PIC
12005 call $gp is restored from that location. */
12006
12007static void
17a2f251 12008s_cprestore (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
12009{
12010 expressionS ex;
252b5132 12011
6478892d 12012 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
c9914766 12013 .cprestore is ignored. */
6478892d 12014 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
12015 {
12016 s_ignore (0);
12017 return;
12018 }
12019
12020 mips_cprestore_offset = get_absolute_expression ();
7a621144 12021 mips_cprestore_valid = 1;
252b5132
RH
12022
12023 ex.X_op = O_constant;
12024 ex.X_add_symbol = NULL;
12025 ex.X_op_symbol = NULL;
12026 ex.X_add_number = mips_cprestore_offset;
12027
584892a6 12028 macro_start ();
67c0d1eb
RS
12029 macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
12030 SP, HAVE_64BIT_ADDRESSES);
584892a6 12031 macro_end ();
252b5132
RH
12032
12033 demand_empty_rest_of_line ();
12034}
12035
6478892d 12036/* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
67c1ffbe 12037 was given in the preceding .cpsetup, it results in:
6478892d 12038 ld $gp, offset($sp)
76b3015f 12039
6478892d 12040 If a register $reg2 was given there, it results in:
609f23f4 12041 daddu $gp, $reg2, $0
6478892d
TS
12042 */
12043static void
17a2f251 12044s_cpreturn (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
12045{
12046 expressionS ex;
6478892d
TS
12047
12048 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
12049 We also need NewABI support. */
12050 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12051 {
12052 s_ignore (0);
12053 return;
12054 }
12055
584892a6 12056 macro_start ();
6478892d
TS
12057 if (mips_cpreturn_register == -1)
12058 {
12059 ex.X_op = O_constant;
12060 ex.X_add_symbol = NULL;
12061 ex.X_op_symbol = NULL;
12062 ex.X_add_number = mips_cpreturn_offset;
12063
67c0d1eb 12064 macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
6478892d
TS
12065 }
12066 else
67c0d1eb 12067 macro_build (NULL, "daddu", "d,v,t", mips_gp_register,
17a2f251 12068 mips_cpreturn_register, 0);
584892a6 12069 macro_end ();
6478892d
TS
12070
12071 demand_empty_rest_of_line ();
12072}
12073
12074/* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
12075 code. It sets the offset to use in gp_rel relocations. */
12076
12077static void
17a2f251 12078s_gpvalue (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
12079{
12080 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
12081 We also need NewABI support. */
12082 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12083 {
12084 s_ignore (0);
12085 return;
12086 }
12087
def2e0dd 12088 mips_gprel_offset = get_absolute_expression ();
6478892d
TS
12089
12090 demand_empty_rest_of_line ();
12091}
12092
252b5132
RH
12093/* Handle the .gpword pseudo-op. This is used when generating PIC
12094 code. It generates a 32 bit GP relative reloc. */
12095
12096static void
17a2f251 12097s_gpword (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
12098{
12099 symbolS *label;
12100 expressionS ex;
12101 char *p;
12102
12103 /* When not generating PIC code, this is treated as .word. */
12104 if (mips_pic != SVR4_PIC)
12105 {
12106 s_cons (2);
12107 return;
12108 }
12109
12110 label = insn_labels != NULL ? insn_labels->label : NULL;
b34976b6 12111 mips_emit_delays (TRUE);
252b5132
RH
12112 if (auto_align)
12113 mips_align (2, 0, label);
12114 mips_clear_insn_labels ();
12115
12116 expression (&ex);
12117
12118 if (ex.X_op != O_symbol || ex.X_add_number != 0)
12119 {
12120 as_bad (_("Unsupported use of .gpword"));
12121 ignore_rest_of_line ();
12122 }
12123
12124 p = frag_more (4);
17a2f251 12125 md_number_to_chars (p, 0, 4);
b34976b6 12126 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
cdf6fd85 12127 BFD_RELOC_GPREL32);
252b5132
RH
12128
12129 demand_empty_rest_of_line ();
12130}
12131
10181a0d 12132static void
17a2f251 12133s_gpdword (int ignore ATTRIBUTE_UNUSED)
10181a0d
AO
12134{
12135 symbolS *label;
12136 expressionS ex;
12137 char *p;
12138
12139 /* When not generating PIC code, this is treated as .dword. */
12140 if (mips_pic != SVR4_PIC)
12141 {
12142 s_cons (3);
12143 return;
12144 }
12145
12146 label = insn_labels != NULL ? insn_labels->label : NULL;
b34976b6 12147 mips_emit_delays (TRUE);
10181a0d
AO
12148 if (auto_align)
12149 mips_align (3, 0, label);
12150 mips_clear_insn_labels ();
12151
12152 expression (&ex);
12153
12154 if (ex.X_op != O_symbol || ex.X_add_number != 0)
12155 {
12156 as_bad (_("Unsupported use of .gpdword"));
12157 ignore_rest_of_line ();
12158 }
12159
12160 p = frag_more (8);
17a2f251 12161 md_number_to_chars (p, 0, 8);
a105a300 12162 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
6e1304d8 12163 BFD_RELOC_GPREL32)->fx_tcbit = 1;
10181a0d
AO
12164
12165 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
6e1304d8
RS
12166 fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
12167 FALSE, BFD_RELOC_64)->fx_tcbit = 1;
10181a0d
AO
12168
12169 demand_empty_rest_of_line ();
12170}
12171
252b5132
RH
12172/* Handle the .cpadd pseudo-op. This is used when dealing with switch
12173 tables in SVR4 PIC code. */
12174
12175static void
17a2f251 12176s_cpadd (int ignore ATTRIBUTE_UNUSED)
252b5132 12177{
252b5132
RH
12178 int reg;
12179
10181a0d
AO
12180 /* This is ignored when not generating SVR4 PIC code. */
12181 if (mips_pic != SVR4_PIC)
252b5132
RH
12182 {
12183 s_ignore (0);
12184 return;
12185 }
12186
12187 /* Add $gp to the register named as an argument. */
584892a6 12188 macro_start ();
252b5132 12189 reg = tc_get_register (0);
67c0d1eb 12190 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
584892a6 12191 macro_end ();
252b5132 12192
bdaaa2e1 12193 demand_empty_rest_of_line ();
252b5132
RH
12194}
12195
12196/* Handle the .insn pseudo-op. This marks instruction labels in
12197 mips16 mode. This permits the linker to handle them specially,
12198 such as generating jalx instructions when needed. We also make
12199 them odd for the duration of the assembly, in order to generate the
12200 right sort of code. We will make them even in the adjust_symtab
12201 routine, while leaving them marked. This is convenient for the
12202 debugger and the disassembler. The linker knows to make them odd
12203 again. */
12204
12205static void
17a2f251 12206s_insn (int ignore ATTRIBUTE_UNUSED)
252b5132 12207{
f9419b05 12208 mips16_mark_labels ();
252b5132
RH
12209
12210 demand_empty_rest_of_line ();
12211}
12212
12213/* Handle a .stabn directive. We need these in order to mark a label
12214 as being a mips16 text label correctly. Sometimes the compiler
12215 will emit a label, followed by a .stabn, and then switch sections.
12216 If the label and .stabn are in mips16 mode, then the label is
12217 really a mips16 text label. */
12218
12219static void
17a2f251 12220s_mips_stab (int type)
252b5132 12221{
f9419b05 12222 if (type == 'n')
252b5132
RH
12223 mips16_mark_labels ();
12224
12225 s_stab (type);
12226}
12227
12228/* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
12229 */
12230
12231static void
17a2f251 12232s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
12233{
12234 char *name;
12235 int c;
12236 symbolS *symbolP;
12237 expressionS exp;
12238
12239 name = input_line_pointer;
12240 c = get_symbol_end ();
12241 symbolP = symbol_find_or_make (name);
12242 S_SET_WEAK (symbolP);
12243 *input_line_pointer = c;
12244
12245 SKIP_WHITESPACE ();
12246
12247 if (! is_end_of_line[(unsigned char) *input_line_pointer])
12248 {
12249 if (S_IS_DEFINED (symbolP))
12250 {
956cd1d6 12251 as_bad ("ignoring attempt to redefine symbol %s",
252b5132
RH
12252 S_GET_NAME (symbolP));
12253 ignore_rest_of_line ();
12254 return;
12255 }
bdaaa2e1 12256
252b5132
RH
12257 if (*input_line_pointer == ',')
12258 {
12259 ++input_line_pointer;
12260 SKIP_WHITESPACE ();
12261 }
bdaaa2e1 12262
252b5132
RH
12263 expression (&exp);
12264 if (exp.X_op != O_symbol)
12265 {
12266 as_bad ("bad .weakext directive");
98d3f06f 12267 ignore_rest_of_line ();
252b5132
RH
12268 return;
12269 }
49309057 12270 symbol_set_value_expression (symbolP, &exp);
252b5132
RH
12271 }
12272
12273 demand_empty_rest_of_line ();
12274}
12275
12276/* Parse a register string into a number. Called from the ECOFF code
12277 to parse .frame. The argument is non-zero if this is the frame
12278 register, so that we can record it in mips_frame_reg. */
12279
12280int
17a2f251 12281tc_get_register (int frame)
252b5132
RH
12282{
12283 int reg;
12284
12285 SKIP_WHITESPACE ();
12286 if (*input_line_pointer++ != '$')
12287 {
12288 as_warn (_("expected `$'"));
85b51719 12289 reg = ZERO;
252b5132 12290 }
3882b010 12291 else if (ISDIGIT (*input_line_pointer))
252b5132
RH
12292 {
12293 reg = get_absolute_expression ();
12294 if (reg < 0 || reg >= 32)
12295 {
12296 as_warn (_("Bad register number"));
85b51719 12297 reg = ZERO;
252b5132
RH
12298 }
12299 }
12300 else
12301 {
76db943d 12302 if (strncmp (input_line_pointer, "ra", 2) == 0)
85b51719
TS
12303 {
12304 reg = RA;
12305 input_line_pointer += 2;
12306 }
76db943d 12307 else if (strncmp (input_line_pointer, "fp", 2) == 0)
85b51719
TS
12308 {
12309 reg = FP;
12310 input_line_pointer += 2;
12311 }
252b5132 12312 else if (strncmp (input_line_pointer, "sp", 2) == 0)
85b51719
TS
12313 {
12314 reg = SP;
12315 input_line_pointer += 2;
12316 }
252b5132 12317 else if (strncmp (input_line_pointer, "gp", 2) == 0)
85b51719
TS
12318 {
12319 reg = GP;
12320 input_line_pointer += 2;
12321 }
252b5132 12322 else if (strncmp (input_line_pointer, "at", 2) == 0)
85b51719
TS
12323 {
12324 reg = AT;
12325 input_line_pointer += 2;
12326 }
12327 else if (strncmp (input_line_pointer, "kt0", 3) == 0)
12328 {
12329 reg = KT0;
12330 input_line_pointer += 3;
12331 }
12332 else if (strncmp (input_line_pointer, "kt1", 3) == 0)
12333 {
12334 reg = KT1;
12335 input_line_pointer += 3;
12336 }
12337 else if (strncmp (input_line_pointer, "zero", 4) == 0)
12338 {
12339 reg = ZERO;
12340 input_line_pointer += 4;
12341 }
252b5132
RH
12342 else
12343 {
12344 as_warn (_("Unrecognized register name"));
85b51719
TS
12345 reg = ZERO;
12346 while (ISALNUM(*input_line_pointer))
12347 input_line_pointer++;
252b5132 12348 }
252b5132
RH
12349 }
12350 if (frame)
7a621144
DJ
12351 {
12352 mips_frame_reg = reg != 0 ? reg : SP;
12353 mips_frame_reg_valid = 1;
12354 mips_cprestore_valid = 0;
12355 }
252b5132
RH
12356 return reg;
12357}
12358
12359valueT
17a2f251 12360md_section_align (asection *seg, valueT addr)
252b5132
RH
12361{
12362 int align = bfd_get_section_alignment (stdoutput, seg);
12363
12364#ifdef OBJ_ELF
12365 /* We don't need to align ELF sections to the full alignment.
12366 However, Irix 5 may prefer that we align them at least to a 16
12367 byte boundary. We don't bother to align the sections if we are
12368 targeted for an embedded system. */
12369 if (strcmp (TARGET_OS, "elf") == 0)
12370 return addr;
12371 if (align > 4)
12372 align = 4;
12373#endif
12374
12375 return ((addr + (1 << align) - 1) & (-1 << align));
12376}
12377
12378/* Utility routine, called from above as well. If called while the
12379 input file is still being read, it's only an approximation. (For
12380 example, a symbol may later become defined which appeared to be
12381 undefined earlier.) */
12382
12383static int
17a2f251 12384nopic_need_relax (symbolS *sym, int before_relaxing)
252b5132
RH
12385{
12386 if (sym == 0)
12387 return 0;
12388
4d0d148d 12389 if (g_switch_value > 0)
252b5132
RH
12390 {
12391 const char *symname;
12392 int change;
12393
c9914766 12394 /* Find out whether this symbol can be referenced off the $gp
252b5132
RH
12395 register. It can be if it is smaller than the -G size or if
12396 it is in the .sdata or .sbss section. Certain symbols can
c9914766 12397 not be referenced off the $gp, although it appears as though
252b5132
RH
12398 they can. */
12399 symname = S_GET_NAME (sym);
12400 if (symname != (const char *) NULL
12401 && (strcmp (symname, "eprol") == 0
12402 || strcmp (symname, "etext") == 0
12403 || strcmp (symname, "_gp") == 0
12404 || strcmp (symname, "edata") == 0
12405 || strcmp (symname, "_fbss") == 0
12406 || strcmp (symname, "_fdata") == 0
12407 || strcmp (symname, "_ftext") == 0
12408 || strcmp (symname, "end") == 0
12409 || strcmp (symname, "_gp_disp") == 0))
12410 change = 1;
12411 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
12412 && (0
12413#ifndef NO_ECOFF_DEBUGGING
49309057
ILT
12414 || (symbol_get_obj (sym)->ecoff_extern_size != 0
12415 && (symbol_get_obj (sym)->ecoff_extern_size
12416 <= g_switch_value))
252b5132
RH
12417#endif
12418 /* We must defer this decision until after the whole
12419 file has been read, since there might be a .extern
12420 after the first use of this symbol. */
12421 || (before_relaxing
12422#ifndef NO_ECOFF_DEBUGGING
49309057 12423 && symbol_get_obj (sym)->ecoff_extern_size == 0
252b5132
RH
12424#endif
12425 && S_GET_VALUE (sym) == 0)
12426 || (S_GET_VALUE (sym) != 0
12427 && S_GET_VALUE (sym) <= g_switch_value)))
12428 change = 0;
12429 else
12430 {
12431 const char *segname;
12432
12433 segname = segment_name (S_GET_SEGMENT (sym));
12434 assert (strcmp (segname, ".lit8") != 0
12435 && strcmp (segname, ".lit4") != 0);
12436 change = (strcmp (segname, ".sdata") != 0
fba2b7f9
GK
12437 && strcmp (segname, ".sbss") != 0
12438 && strncmp (segname, ".sdata.", 7) != 0
12439 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
252b5132
RH
12440 }
12441 return change;
12442 }
12443 else
c9914766 12444 /* We are not optimizing for the $gp register. */
252b5132
RH
12445 return 1;
12446}
12447
5919d012
RS
12448
12449/* Return true if the given symbol should be considered local for SVR4 PIC. */
12450
12451static bfd_boolean
17a2f251 12452pic_need_relax (symbolS *sym, asection *segtype)
5919d012
RS
12453{
12454 asection *symsec;
12455 bfd_boolean linkonce;
12456
12457 /* Handle the case of a symbol equated to another symbol. */
12458 while (symbol_equated_reloc_p (sym))
12459 {
12460 symbolS *n;
12461
12462 /* It's possible to get a loop here in a badly written
12463 program. */
12464 n = symbol_get_value_expression (sym)->X_add_symbol;
12465 if (n == sym)
12466 break;
12467 sym = n;
12468 }
12469
12470 symsec = S_GET_SEGMENT (sym);
12471
12472 /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
12473 linkonce = FALSE;
12474 if (symsec != segtype && ! S_IS_LOCAL (sym))
12475 {
12476 if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
12477 != 0)
12478 linkonce = TRUE;
12479
12480 /* The GNU toolchain uses an extension for ELF: a section
12481 beginning with the magic string .gnu.linkonce is a linkonce
12482 section. */
12483 if (strncmp (segment_name (symsec), ".gnu.linkonce",
12484 sizeof ".gnu.linkonce" - 1) == 0)
12485 linkonce = TRUE;
12486 }
12487
12488 /* This must duplicate the test in adjust_reloc_syms. */
12489 return (symsec != &bfd_und_section
12490 && symsec != &bfd_abs_section
12491 && ! bfd_is_com_section (symsec)
12492 && !linkonce
12493#ifdef OBJ_ELF
12494 /* A global or weak symbol is treated as external. */
12495 && (OUTPUT_FLAVOR != bfd_target_elf_flavour
3e722fb5 12496 || (! S_IS_WEAK (sym) && ! S_IS_EXTERNAL (sym)))
5919d012
RS
12497#endif
12498 );
12499}
12500
12501
252b5132
RH
12502/* Given a mips16 variant frag FRAGP, return non-zero if it needs an
12503 extended opcode. SEC is the section the frag is in. */
12504
12505static int
17a2f251 12506mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
252b5132
RH
12507{
12508 int type;
12509 register const struct mips16_immed_operand *op;
12510 offsetT val;
12511 int mintiny, maxtiny;
12512 segT symsec;
98aa84af 12513 fragS *sym_frag;
252b5132
RH
12514
12515 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
12516 return 0;
12517 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
12518 return 1;
12519
12520 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12521 op = mips16_immed_operands;
12522 while (op->type != type)
12523 {
12524 ++op;
12525 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
12526 }
12527
12528 if (op->unsp)
12529 {
12530 if (type == '<' || type == '>' || type == '[' || type == ']')
12531 {
12532 mintiny = 1;
12533 maxtiny = 1 << op->nbits;
12534 }
12535 else
12536 {
12537 mintiny = 0;
12538 maxtiny = (1 << op->nbits) - 1;
12539 }
12540 }
12541 else
12542 {
12543 mintiny = - (1 << (op->nbits - 1));
12544 maxtiny = (1 << (op->nbits - 1)) - 1;
12545 }
12546
98aa84af 12547 sym_frag = symbol_get_frag (fragp->fr_symbol);
ac62c346 12548 val = S_GET_VALUE (fragp->fr_symbol);
98aa84af 12549 symsec = S_GET_SEGMENT (fragp->fr_symbol);
252b5132
RH
12550
12551 if (op->pcrel)
12552 {
12553 addressT addr;
12554
12555 /* We won't have the section when we are called from
12556 mips_relax_frag. However, we will always have been called
12557 from md_estimate_size_before_relax first. If this is a
12558 branch to a different section, we mark it as such. If SEC is
12559 NULL, and the frag is not marked, then it must be a branch to
12560 the same section. */
12561 if (sec == NULL)
12562 {
12563 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
12564 return 1;
12565 }
12566 else
12567 {
98aa84af 12568 /* Must have been called from md_estimate_size_before_relax. */
252b5132
RH
12569 if (symsec != sec)
12570 {
12571 fragp->fr_subtype =
12572 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12573
12574 /* FIXME: We should support this, and let the linker
12575 catch branches and loads that are out of range. */
12576 as_bad_where (fragp->fr_file, fragp->fr_line,
12577 _("unsupported PC relative reference to different section"));
12578
12579 return 1;
12580 }
98aa84af
AM
12581 if (fragp != sym_frag && sym_frag->fr_address == 0)
12582 /* Assume non-extended on the first relaxation pass.
12583 The address we have calculated will be bogus if this is
12584 a forward branch to another frag, as the forward frag
12585 will have fr_address == 0. */
12586 return 0;
252b5132
RH
12587 }
12588
12589 /* In this case, we know for sure that the symbol fragment is in
98aa84af
AM
12590 the same section. If the relax_marker of the symbol fragment
12591 differs from the relax_marker of this fragment, we have not
12592 yet adjusted the symbol fragment fr_address. We want to add
252b5132
RH
12593 in STRETCH in order to get a better estimate of the address.
12594 This particularly matters because of the shift bits. */
12595 if (stretch != 0
98aa84af 12596 && sym_frag->relax_marker != fragp->relax_marker)
252b5132
RH
12597 {
12598 fragS *f;
12599
12600 /* Adjust stretch for any alignment frag. Note that if have
12601 been expanding the earlier code, the symbol may be
12602 defined in what appears to be an earlier frag. FIXME:
12603 This doesn't handle the fr_subtype field, which specifies
12604 a maximum number of bytes to skip when doing an
12605 alignment. */
98aa84af 12606 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
252b5132
RH
12607 {
12608 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
12609 {
12610 if (stretch < 0)
12611 stretch = - ((- stretch)
12612 & ~ ((1 << (int) f->fr_offset) - 1));
12613 else
12614 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
12615 if (stretch == 0)
12616 break;
12617 }
12618 }
12619 if (f != NULL)
12620 val += stretch;
12621 }
12622
12623 addr = fragp->fr_address + fragp->fr_fix;
12624
12625 /* The base address rules are complicated. The base address of
12626 a branch is the following instruction. The base address of a
12627 PC relative load or add is the instruction itself, but if it
12628 is in a delay slot (in which case it can not be extended) use
12629 the address of the instruction whose delay slot it is in. */
12630 if (type == 'p' || type == 'q')
12631 {
12632 addr += 2;
12633
12634 /* If we are currently assuming that this frag should be
12635 extended, then, the current address is two bytes
bdaaa2e1 12636 higher. */
252b5132
RH
12637 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12638 addr += 2;
12639
12640 /* Ignore the low bit in the target, since it will be set
12641 for a text label. */
12642 if ((val & 1) != 0)
12643 --val;
12644 }
12645 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12646 addr -= 4;
12647 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12648 addr -= 2;
12649
12650 val -= addr & ~ ((1 << op->shift) - 1);
12651
12652 /* Branch offsets have an implicit 0 in the lowest bit. */
12653 if (type == 'p' || type == 'q')
12654 val /= 2;
12655
12656 /* If any of the shifted bits are set, we must use an extended
12657 opcode. If the address depends on the size of this
12658 instruction, this can lead to a loop, so we arrange to always
12659 use an extended opcode. We only check this when we are in
12660 the main relaxation loop, when SEC is NULL. */
12661 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
12662 {
12663 fragp->fr_subtype =
12664 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12665 return 1;
12666 }
12667
12668 /* If we are about to mark a frag as extended because the value
12669 is precisely maxtiny + 1, then there is a chance of an
12670 infinite loop as in the following code:
12671 la $4,foo
12672 .skip 1020
12673 .align 2
12674 foo:
12675 In this case when the la is extended, foo is 0x3fc bytes
12676 away, so the la can be shrunk, but then foo is 0x400 away, so
12677 the la must be extended. To avoid this loop, we mark the
12678 frag as extended if it was small, and is about to become
12679 extended with a value of maxtiny + 1. */
12680 if (val == ((maxtiny + 1) << op->shift)
12681 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
12682 && sec == NULL)
12683 {
12684 fragp->fr_subtype =
12685 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12686 return 1;
12687 }
12688 }
12689 else if (symsec != absolute_section && sec != NULL)
12690 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
12691
12692 if ((val & ((1 << op->shift) - 1)) != 0
12693 || val < (mintiny << op->shift)
12694 || val > (maxtiny << op->shift))
12695 return 1;
12696 else
12697 return 0;
12698}
12699
4a6a3df4
AO
12700/* Compute the length of a branch sequence, and adjust the
12701 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
12702 worst-case length is computed, with UPDATE being used to indicate
12703 whether an unconditional (-1), branch-likely (+1) or regular (0)
12704 branch is to be computed. */
12705static int
17a2f251 12706relaxed_branch_length (fragS *fragp, asection *sec, int update)
4a6a3df4 12707{
b34976b6 12708 bfd_boolean toofar;
4a6a3df4
AO
12709 int length;
12710
12711 if (fragp
12712 && S_IS_DEFINED (fragp->fr_symbol)
12713 && sec == S_GET_SEGMENT (fragp->fr_symbol))
12714 {
12715 addressT addr;
12716 offsetT val;
12717
12718 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
12719
12720 addr = fragp->fr_address + fragp->fr_fix + 4;
12721
12722 val -= addr;
12723
12724 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
12725 }
12726 else if (fragp)
12727 /* If the symbol is not defined or it's in a different segment,
12728 assume the user knows what's going on and emit a short
12729 branch. */
b34976b6 12730 toofar = FALSE;
4a6a3df4 12731 else
b34976b6 12732 toofar = TRUE;
4a6a3df4
AO
12733
12734 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
12735 fragp->fr_subtype
af6ae2ad 12736 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_UNCOND (fragp->fr_subtype),
4a6a3df4
AO
12737 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
12738 RELAX_BRANCH_LINK (fragp->fr_subtype),
12739 toofar);
12740
12741 length = 4;
12742 if (toofar)
12743 {
12744 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
12745 length += 8;
12746
12747 if (mips_pic != NO_PIC)
12748 {
12749 /* Additional space for PIC loading of target address. */
12750 length += 8;
12751 if (mips_opts.isa == ISA_MIPS1)
12752 /* Additional space for $at-stabilizing nop. */
12753 length += 4;
12754 }
12755
12756 /* If branch is conditional. */
12757 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
12758 length += 8;
12759 }
b34976b6 12760
4a6a3df4
AO
12761 return length;
12762}
12763
252b5132
RH
12764/* Estimate the size of a frag before relaxing. Unless this is the
12765 mips16, we are not really relaxing here, and the final size is
12766 encoded in the subtype information. For the mips16, we have to
12767 decide whether we are using an extended opcode or not. */
12768
252b5132 12769int
17a2f251 12770md_estimate_size_before_relax (fragS *fragp, asection *segtype)
252b5132 12771{
5919d012 12772 int change;
252b5132 12773
4a6a3df4
AO
12774 if (RELAX_BRANCH_P (fragp->fr_subtype))
12775 {
12776
b34976b6
AM
12777 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
12778
4a6a3df4
AO
12779 return fragp->fr_var;
12780 }
12781
252b5132 12782 if (RELAX_MIPS16_P (fragp->fr_subtype))
177b4a6a
AO
12783 /* We don't want to modify the EXTENDED bit here; it might get us
12784 into infinite loops. We change it only in mips_relax_frag(). */
12785 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
252b5132
RH
12786
12787 if (mips_pic == NO_PIC)
5919d012 12788 change = nopic_need_relax (fragp->fr_symbol, 0);
252b5132 12789 else if (mips_pic == SVR4_PIC)
5919d012 12790 change = pic_need_relax (fragp->fr_symbol, segtype);
252b5132
RH
12791 else
12792 abort ();
12793
12794 if (change)
12795 {
4d7206a2 12796 fragp->fr_subtype |= RELAX_USE_SECOND;
4d7206a2 12797 return -RELAX_FIRST (fragp->fr_subtype);
252b5132 12798 }
4d7206a2
RS
12799 else
12800 return -RELAX_SECOND (fragp->fr_subtype);
252b5132
RH
12801}
12802
12803/* This is called to see whether a reloc against a defined symbol
de7e6852 12804 should be converted into a reloc against a section. */
252b5132
RH
12805
12806int
17a2f251 12807mips_fix_adjustable (fixS *fixp)
252b5132 12808{
de7e6852
RS
12809 /* Don't adjust MIPS16 jump relocations, so we don't have to worry
12810 about the format of the offset in the .o file. */
252b5132
RH
12811 if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
12812 return 0;
a161fe53 12813
252b5132
RH
12814 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
12815 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12816 return 0;
a161fe53 12817
252b5132
RH
12818 if (fixp->fx_addsy == NULL)
12819 return 1;
a161fe53 12820
de7e6852
RS
12821 /* If symbol SYM is in a mergeable section, relocations of the form
12822 SYM + 0 can usually be made section-relative. The mergeable data
12823 is then identified by the section offset rather than by the symbol.
12824
12825 However, if we're generating REL LO16 relocations, the offset is split
12826 between the LO16 and parterning high part relocation. The linker will
12827 need to recalculate the complete offset in order to correctly identify
12828 the merge data.
12829
12830 The linker has traditionally not looked for the parterning high part
12831 relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
12832 placed anywhere. Rather than break backwards compatibility by changing
12833 this, it seems better not to force the issue, and instead keep the
12834 original symbol. This will work with either linker behavior. */
12835 if ((fixp->fx_r_type == BFD_RELOC_LO16 || reloc_needs_lo_p (fixp->fx_r_type))
12836 && HAVE_IN_PLACE_ADDENDS
12837 && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
12838 return 0;
12839
252b5132 12840#ifdef OBJ_ELF
de7e6852
RS
12841 /* Don't adjust relocations against mips16 symbols, so that the linker
12842 can find them if it needs to set up a stub. */
252b5132
RH
12843 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
12844 && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
12845 && fixp->fx_subsy == NULL)
12846 return 0;
12847#endif
a161fe53 12848
252b5132
RH
12849 return 1;
12850}
12851
12852/* Translate internal representation of relocation info to BFD target
12853 format. */
12854
12855arelent **
17a2f251 12856tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
12857{
12858 static arelent *retval[4];
12859 arelent *reloc;
12860 bfd_reloc_code_real_type code;
12861
4b0cff4e
TS
12862 memset (retval, 0, sizeof(retval));
12863 reloc = retval[0] = (arelent *) xcalloc (1, sizeof (arelent));
49309057
ILT
12864 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
12865 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
12866 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
12867
3e722fb5
CD
12868 assert (! fixp->fx_pcrel);
12869 reloc->addend = fixp->fx_addnumber;
252b5132 12870
438c16b8
TS
12871 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
12872 entry to be used in the relocation's section offset. */
12873 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
252b5132
RH
12874 {
12875 reloc->address = reloc->addend;
12876 reloc->addend = 0;
12877 }
12878
252b5132 12879 code = fixp->fx_r_type;
252b5132 12880
3e722fb5
CD
12881 /* To support a PC relative reloc, we used a Cygnus extension.
12882 We check for that here to make sure that we don't let such a
12883 reloc escape normally. (FIXME: This was formerly used by
12884 embedded-PIC support, but is now used by branch handling in
12885 general. That probably should be fixed.) */
0b25d3e6
AO
12886 if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
12887 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
3e722fb5 12888 && code == BFD_RELOC_16_PCREL_S2)
0b25d3e6
AO
12889 reloc->howto = NULL;
12890 else
12891 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
12892
252b5132
RH
12893 if (reloc->howto == NULL)
12894 {
12895 as_bad_where (fixp->fx_file, fixp->fx_line,
12896 _("Can not represent %s relocation in this object file format"),
12897 bfd_get_reloc_code_name (code));
12898 retval[0] = NULL;
12899 }
12900
12901 return retval;
12902}
12903
12904/* Relax a machine dependent frag. This returns the amount by which
12905 the current size of the frag should change. */
12906
12907int
17a2f251 12908mips_relax_frag (asection *sec, fragS *fragp, long stretch)
252b5132 12909{
4a6a3df4
AO
12910 if (RELAX_BRANCH_P (fragp->fr_subtype))
12911 {
12912 offsetT old_var = fragp->fr_var;
b34976b6
AM
12913
12914 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
4a6a3df4
AO
12915
12916 return fragp->fr_var - old_var;
12917 }
12918
252b5132
RH
12919 if (! RELAX_MIPS16_P (fragp->fr_subtype))
12920 return 0;
12921
c4e7957c 12922 if (mips16_extended_frag (fragp, NULL, stretch))
252b5132
RH
12923 {
12924 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12925 return 0;
12926 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
12927 return 2;
12928 }
12929 else
12930 {
12931 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12932 return 0;
12933 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
12934 return -2;
12935 }
12936
12937 return 0;
12938}
12939
12940/* Convert a machine dependent frag. */
12941
12942void
17a2f251 12943md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
252b5132 12944{
4a6a3df4
AO
12945 if (RELAX_BRANCH_P (fragp->fr_subtype))
12946 {
12947 bfd_byte *buf;
12948 unsigned long insn;
12949 expressionS exp;
12950 fixS *fixp;
b34976b6 12951
4a6a3df4
AO
12952 buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
12953
12954 if (target_big_endian)
12955 insn = bfd_getb32 (buf);
12956 else
12957 insn = bfd_getl32 (buf);
b34976b6 12958
4a6a3df4
AO
12959 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
12960 {
12961 /* We generate a fixup instead of applying it right now
12962 because, if there are linker relaxations, we're going to
12963 need the relocations. */
12964 exp.X_op = O_symbol;
12965 exp.X_add_symbol = fragp->fr_symbol;
12966 exp.X_add_number = fragp->fr_offset;
12967
12968 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
0b25d3e6
AO
12969 4, &exp, 1,
12970 BFD_RELOC_16_PCREL_S2);
4a6a3df4
AO
12971 fixp->fx_file = fragp->fr_file;
12972 fixp->fx_line = fragp->fr_line;
b34976b6 12973
17a2f251 12974 md_number_to_chars (buf, insn, 4);
4a6a3df4
AO
12975 buf += 4;
12976 }
12977 else
12978 {
12979 int i;
12980
12981 as_warn_where (fragp->fr_file, fragp->fr_line,
12982 _("relaxed out-of-range branch into a jump"));
12983
12984 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
12985 goto uncond;
12986
12987 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
12988 {
12989 /* Reverse the branch. */
12990 switch ((insn >> 28) & 0xf)
12991 {
12992 case 4:
12993 /* bc[0-3][tf]l? and bc1any[24][ft] instructions can
12994 have the condition reversed by tweaking a single
12995 bit, and their opcodes all have 0x4???????. */
12996 assert ((insn & 0xf1000000) == 0x41000000);
12997 insn ^= 0x00010000;
12998 break;
12999
13000 case 0:
13001 /* bltz 0x04000000 bgez 0x04010000
13002 bltzal 0x04100000 bgezal 0x04110000 */
13003 assert ((insn & 0xfc0e0000) == 0x04000000);
13004 insn ^= 0x00010000;
13005 break;
b34976b6 13006
4a6a3df4
AO
13007 case 1:
13008 /* beq 0x10000000 bne 0x14000000
13009 blez 0x18000000 bgtz 0x1c000000 */
13010 insn ^= 0x04000000;
13011 break;
13012
13013 default:
13014 abort ();
13015 }
13016 }
13017
13018 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13019 {
13020 /* Clear the and-link bit. */
13021 assert ((insn & 0xfc1c0000) == 0x04100000);
13022
13023 /* bltzal 0x04100000 bgezal 0x04110000
13024 bltzall 0x04120000 bgezall 0x04130000 */
13025 insn &= ~0x00100000;
13026 }
13027
13028 /* Branch over the branch (if the branch was likely) or the
13029 full jump (not likely case). Compute the offset from the
13030 current instruction to branch to. */
13031 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13032 i = 16;
13033 else
13034 {
13035 /* How many bytes in instructions we've already emitted? */
13036 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13037 /* How many bytes in instructions from here to the end? */
13038 i = fragp->fr_var - i;
13039 }
13040 /* Convert to instruction count. */
13041 i >>= 2;
13042 /* Branch counts from the next instruction. */
b34976b6 13043 i--;
4a6a3df4
AO
13044 insn |= i;
13045 /* Branch over the jump. */
17a2f251 13046 md_number_to_chars (buf, insn, 4);
4a6a3df4
AO
13047 buf += 4;
13048
13049 /* Nop */
17a2f251 13050 md_number_to_chars (buf, 0, 4);
4a6a3df4
AO
13051 buf += 4;
13052
13053 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13054 {
13055 /* beql $0, $0, 2f */
13056 insn = 0x50000000;
13057 /* Compute the PC offset from the current instruction to
13058 the end of the variable frag. */
13059 /* How many bytes in instructions we've already emitted? */
13060 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13061 /* How many bytes in instructions from here to the end? */
13062 i = fragp->fr_var - i;
13063 /* Convert to instruction count. */
13064 i >>= 2;
13065 /* Don't decrement i, because we want to branch over the
13066 delay slot. */
13067
13068 insn |= i;
17a2f251 13069 md_number_to_chars (buf, insn, 4);
4a6a3df4
AO
13070 buf += 4;
13071
17a2f251 13072 md_number_to_chars (buf, 0, 4);
4a6a3df4
AO
13073 buf += 4;
13074 }
13075
13076 uncond:
13077 if (mips_pic == NO_PIC)
13078 {
13079 /* j or jal. */
13080 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
13081 ? 0x0c000000 : 0x08000000);
13082 exp.X_op = O_symbol;
13083 exp.X_add_symbol = fragp->fr_symbol;
13084 exp.X_add_number = fragp->fr_offset;
13085
13086 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13087 4, &exp, 0, BFD_RELOC_MIPS_JMP);
13088 fixp->fx_file = fragp->fr_file;
13089 fixp->fx_line = fragp->fr_line;
13090
17a2f251 13091 md_number_to_chars (buf, insn, 4);
4a6a3df4
AO
13092 buf += 4;
13093 }
13094 else
13095 {
13096 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
13097 insn = HAVE_64BIT_ADDRESSES ? 0xdf810000 : 0x8f810000;
13098 exp.X_op = O_symbol;
13099 exp.X_add_symbol = fragp->fr_symbol;
13100 exp.X_add_number = fragp->fr_offset;
13101
13102 if (fragp->fr_offset)
13103 {
13104 exp.X_add_symbol = make_expr_symbol (&exp);
13105 exp.X_add_number = 0;
13106 }
13107
13108 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13109 4, &exp, 0, BFD_RELOC_MIPS_GOT16);
13110 fixp->fx_file = fragp->fr_file;
13111 fixp->fx_line = fragp->fr_line;
13112
17a2f251 13113 md_number_to_chars (buf, insn, 4);
4a6a3df4 13114 buf += 4;
b34976b6 13115
4a6a3df4
AO
13116 if (mips_opts.isa == ISA_MIPS1)
13117 {
13118 /* nop */
17a2f251 13119 md_number_to_chars (buf, 0, 4);
4a6a3df4
AO
13120 buf += 4;
13121 }
13122
13123 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
13124 insn = HAVE_64BIT_ADDRESSES ? 0x64210000 : 0x24210000;
13125
13126 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13127 4, &exp, 0, BFD_RELOC_LO16);
13128 fixp->fx_file = fragp->fr_file;
13129 fixp->fx_line = fragp->fr_line;
b34976b6 13130
17a2f251 13131 md_number_to_chars (buf, insn, 4);
4a6a3df4
AO
13132 buf += 4;
13133
13134 /* j(al)r $at. */
13135 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13136 insn = 0x0020f809;
13137 else
13138 insn = 0x00200008;
13139
17a2f251 13140 md_number_to_chars (buf, insn, 4);
4a6a3df4
AO
13141 buf += 4;
13142 }
13143 }
13144
13145 assert (buf == (bfd_byte *)fragp->fr_literal
13146 + fragp->fr_fix + fragp->fr_var);
13147
13148 fragp->fr_fix += fragp->fr_var;
13149
13150 return;
13151 }
13152
252b5132
RH
13153 if (RELAX_MIPS16_P (fragp->fr_subtype))
13154 {
13155 int type;
13156 register const struct mips16_immed_operand *op;
b34976b6 13157 bfd_boolean small, ext;
252b5132
RH
13158 offsetT val;
13159 bfd_byte *buf;
13160 unsigned long insn;
b34976b6 13161 bfd_boolean use_extend;
252b5132
RH
13162 unsigned short extend;
13163
13164 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
13165 op = mips16_immed_operands;
13166 while (op->type != type)
13167 ++op;
13168
13169 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13170 {
b34976b6
AM
13171 small = FALSE;
13172 ext = TRUE;
252b5132
RH
13173 }
13174 else
13175 {
b34976b6
AM
13176 small = TRUE;
13177 ext = FALSE;
252b5132
RH
13178 }
13179
6386f3a7 13180 resolve_symbol_value (fragp->fr_symbol);
252b5132
RH
13181 val = S_GET_VALUE (fragp->fr_symbol);
13182 if (op->pcrel)
13183 {
13184 addressT addr;
13185
13186 addr = fragp->fr_address + fragp->fr_fix;
13187
13188 /* The rules for the base address of a PC relative reloc are
13189 complicated; see mips16_extended_frag. */
13190 if (type == 'p' || type == 'q')
13191 {
13192 addr += 2;
13193 if (ext)
13194 addr += 2;
13195 /* Ignore the low bit in the target, since it will be
13196 set for a text label. */
13197 if ((val & 1) != 0)
13198 --val;
13199 }
13200 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13201 addr -= 4;
13202 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13203 addr -= 2;
13204
13205 addr &= ~ (addressT) ((1 << op->shift) - 1);
13206 val -= addr;
13207
13208 /* Make sure the section winds up with the alignment we have
13209 assumed. */
13210 if (op->shift > 0)
13211 record_alignment (asec, op->shift);
13212 }
13213
13214 if (ext
13215 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
13216 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
13217 as_warn_where (fragp->fr_file, fragp->fr_line,
13218 _("extended instruction in delay slot"));
13219
13220 buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
13221
13222 if (target_big_endian)
13223 insn = bfd_getb16 (buf);
13224 else
13225 insn = bfd_getl16 (buf);
13226
13227 mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
13228 RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
13229 small, ext, &insn, &use_extend, &extend);
13230
13231 if (use_extend)
13232 {
17a2f251 13233 md_number_to_chars (buf, 0xf000 | extend, 2);
252b5132
RH
13234 fragp->fr_fix += 2;
13235 buf += 2;
13236 }
13237
17a2f251 13238 md_number_to_chars (buf, insn, 2);
252b5132
RH
13239 fragp->fr_fix += 2;
13240 buf += 2;
13241 }
13242 else
13243 {
4d7206a2
RS
13244 int first, second;
13245 fixS *fixp;
252b5132 13246
4d7206a2
RS
13247 first = RELAX_FIRST (fragp->fr_subtype);
13248 second = RELAX_SECOND (fragp->fr_subtype);
13249 fixp = (fixS *) fragp->fr_opcode;
252b5132 13250
584892a6
RS
13251 /* Possibly emit a warning if we've chosen the longer option. */
13252 if (((fragp->fr_subtype & RELAX_USE_SECOND) != 0)
13253 == ((fragp->fr_subtype & RELAX_SECOND_LONGER) != 0))
13254 {
13255 const char *msg = macro_warning (fragp->fr_subtype);
13256 if (msg != 0)
13257 as_warn_where (fragp->fr_file, fragp->fr_line, msg);
13258 }
13259
4d7206a2
RS
13260 /* Go through all the fixups for the first sequence. Disable them
13261 (by marking them as done) if we're going to use the second
13262 sequence instead. */
13263 while (fixp
13264 && fixp->fx_frag == fragp
13265 && fixp->fx_where < fragp->fr_fix - second)
13266 {
13267 if (fragp->fr_subtype & RELAX_USE_SECOND)
13268 fixp->fx_done = 1;
13269 fixp = fixp->fx_next;
13270 }
252b5132 13271
4d7206a2
RS
13272 /* Go through the fixups for the second sequence. Disable them if
13273 we're going to use the first sequence, otherwise adjust their
13274 addresses to account for the relaxation. */
13275 while (fixp && fixp->fx_frag == fragp)
13276 {
13277 if (fragp->fr_subtype & RELAX_USE_SECOND)
13278 fixp->fx_where -= first;
13279 else
13280 fixp->fx_done = 1;
13281 fixp = fixp->fx_next;
13282 }
13283
13284 /* Now modify the frag contents. */
13285 if (fragp->fr_subtype & RELAX_USE_SECOND)
13286 {
13287 char *start;
13288
13289 start = fragp->fr_literal + fragp->fr_fix - first - second;
13290 memmove (start, start + first, second);
13291 fragp->fr_fix -= first;
13292 }
13293 else
13294 fragp->fr_fix -= second;
252b5132
RH
13295 }
13296}
13297
13298#ifdef OBJ_ELF
13299
13300/* This function is called after the relocs have been generated.
13301 We've been storing mips16 text labels as odd. Here we convert them
13302 back to even for the convenience of the debugger. */
13303
13304void
17a2f251 13305mips_frob_file_after_relocs (void)
252b5132
RH
13306{
13307 asymbol **syms;
13308 unsigned int count, i;
13309
13310 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
13311 return;
13312
13313 syms = bfd_get_outsymbols (stdoutput);
13314 count = bfd_get_symcount (stdoutput);
13315 for (i = 0; i < count; i++, syms++)
13316 {
13317 if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
13318 && ((*syms)->value & 1) != 0)
13319 {
13320 (*syms)->value &= ~1;
13321 /* If the symbol has an odd size, it was probably computed
13322 incorrectly, so adjust that as well. */
13323 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
13324 ++elf_symbol (*syms)->internal_elf_sym.st_size;
13325 }
13326 }
13327}
13328
13329#endif
13330
13331/* This function is called whenever a label is defined. It is used
13332 when handling branch delays; if a branch has a label, we assume we
13333 can not move it. */
13334
13335void
17a2f251 13336mips_define_label (symbolS *sym)
252b5132
RH
13337{
13338 struct insn_label_list *l;
13339
13340 if (free_insn_labels == NULL)
13341 l = (struct insn_label_list *) xmalloc (sizeof *l);
13342 else
13343 {
13344 l = free_insn_labels;
13345 free_insn_labels = l->next;
13346 }
13347
13348 l->label = sym;
13349 l->next = insn_labels;
13350 insn_labels = l;
13351}
13352\f
13353#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13354
13355/* Some special processing for a MIPS ELF file. */
13356
13357void
17a2f251 13358mips_elf_final_processing (void)
252b5132
RH
13359{
13360 /* Write out the register information. */
316f5878 13361 if (mips_abi != N64_ABI)
252b5132
RH
13362 {
13363 Elf32_RegInfo s;
13364
13365 s.ri_gprmask = mips_gprmask;
13366 s.ri_cprmask[0] = mips_cprmask[0];
13367 s.ri_cprmask[1] = mips_cprmask[1];
13368 s.ri_cprmask[2] = mips_cprmask[2];
13369 s.ri_cprmask[3] = mips_cprmask[3];
13370 /* The gp_value field is set by the MIPS ELF backend. */
13371
13372 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
13373 ((Elf32_External_RegInfo *)
13374 mips_regmask_frag));
13375 }
13376 else
13377 {
13378 Elf64_Internal_RegInfo s;
13379
13380 s.ri_gprmask = mips_gprmask;
13381 s.ri_pad = 0;
13382 s.ri_cprmask[0] = mips_cprmask[0];
13383 s.ri_cprmask[1] = mips_cprmask[1];
13384 s.ri_cprmask[2] = mips_cprmask[2];
13385 s.ri_cprmask[3] = mips_cprmask[3];
13386 /* The gp_value field is set by the MIPS ELF backend. */
13387
13388 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
13389 ((Elf64_External_RegInfo *)
13390 mips_regmask_frag));
13391 }
13392
13393 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
13394 sort of BFD interface for this. */
13395 if (mips_any_noreorder)
13396 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
13397 if (mips_pic != NO_PIC)
143d77c5 13398 {
252b5132 13399 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
143d77c5
EC
13400 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
13401 }
13402 if (mips_abicalls)
13403 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
252b5132 13404
98d3f06f 13405 /* Set MIPS ELF flags for ASEs. */
a4672219
TS
13406 if (file_ase_mips16)
13407 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
1f25f5d3
CD
13408#if 0 /* XXX FIXME */
13409 if (file_ase_mips3d)
13410 elf_elfheader (stdoutput)->e_flags |= ???;
13411#endif
deec1734
CD
13412 if (file_ase_mdmx)
13413 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
1f25f5d3 13414
bdaaa2e1 13415 /* Set the MIPS ELF ABI flags. */
316f5878 13416 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
252b5132 13417 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
316f5878 13418 else if (mips_abi == O64_ABI)
252b5132 13419 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
316f5878 13420 else if (mips_abi == EABI_ABI)
252b5132 13421 {
316f5878 13422 if (!file_mips_gp32)
252b5132
RH
13423 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
13424 else
13425 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
13426 }
316f5878 13427 else if (mips_abi == N32_ABI)
be00bddd
TS
13428 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
13429
c9914766 13430 /* Nothing to do for N64_ABI. */
252b5132
RH
13431
13432 if (mips_32bitmode)
13433 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
13434}
13435
13436#endif /* OBJ_ELF || OBJ_MAYBE_ELF */
13437\f
beae10d5
KH
13438typedef struct proc {
13439 symbolS *isym;
13440 unsigned long reg_mask;
13441 unsigned long reg_offset;
13442 unsigned long fpreg_mask;
13443 unsigned long fpreg_offset;
13444 unsigned long frame_offset;
13445 unsigned long frame_reg;
13446 unsigned long pc_reg;
13447} procS;
252b5132
RH
13448
13449static procS cur_proc;
13450static procS *cur_proc_ptr;
13451static int numprocs;
13452
0a9ef439 13453/* Fill in an rs_align_code fragment. */
a19d8eb0 13454
0a9ef439 13455void
17a2f251 13456mips_handle_align (fragS *fragp)
a19d8eb0 13457{
0a9ef439
RH
13458 if (fragp->fr_type != rs_align_code)
13459 return;
13460
13461 if (mips_opts.mips16)
a19d8eb0
CP
13462 {
13463 static const unsigned char be_nop[] = { 0x65, 0x00 };
13464 static const unsigned char le_nop[] = { 0x00, 0x65 };
13465
0a9ef439
RH
13466 int bytes;
13467 char *p;
a19d8eb0 13468
0a9ef439
RH
13469 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
13470 p = fragp->fr_literal + fragp->fr_fix;
13471
13472 if (bytes & 1)
13473 {
13474 *p++ = 0;
f9419b05 13475 fragp->fr_fix++;
0a9ef439
RH
13476 }
13477
13478 memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
13479 fragp->fr_var = 2;
a19d8eb0
CP
13480 }
13481
0a9ef439 13482 /* For mips32, a nop is a zero, which we trivially get by doing nothing. */
a19d8eb0
CP
13483}
13484
252b5132 13485static void
17a2f251 13486md_obj_begin (void)
252b5132
RH
13487{
13488}
13489
13490static void
17a2f251 13491md_obj_end (void)
252b5132
RH
13492{
13493 /* check for premature end, nesting errors, etc */
13494 if (cur_proc_ptr)
9a41af64 13495 as_warn (_("missing .end at end of assembly"));
252b5132
RH
13496}
13497
13498static long
17a2f251 13499get_number (void)
252b5132
RH
13500{
13501 int negative = 0;
13502 long val = 0;
13503
13504 if (*input_line_pointer == '-')
13505 {
13506 ++input_line_pointer;
13507 negative = 1;
13508 }
3882b010 13509 if (!ISDIGIT (*input_line_pointer))
956cd1d6 13510 as_bad (_("expected simple number"));
252b5132
RH
13511 if (input_line_pointer[0] == '0')
13512 {
13513 if (input_line_pointer[1] == 'x')
13514 {
13515 input_line_pointer += 2;
3882b010 13516 while (ISXDIGIT (*input_line_pointer))
252b5132
RH
13517 {
13518 val <<= 4;
13519 val |= hex_value (*input_line_pointer++);
13520 }
13521 return negative ? -val : val;
13522 }
13523 else
13524 {
13525 ++input_line_pointer;
3882b010 13526 while (ISDIGIT (*input_line_pointer))
252b5132
RH
13527 {
13528 val <<= 3;
13529 val |= *input_line_pointer++ - '0';
13530 }
13531 return negative ? -val : val;
13532 }
13533 }
3882b010 13534 if (!ISDIGIT (*input_line_pointer))
252b5132
RH
13535 {
13536 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
13537 *input_line_pointer, *input_line_pointer);
956cd1d6 13538 as_warn (_("invalid number"));
252b5132
RH
13539 return -1;
13540 }
3882b010 13541 while (ISDIGIT (*input_line_pointer))
252b5132
RH
13542 {
13543 val *= 10;
13544 val += *input_line_pointer++ - '0';
13545 }
13546 return negative ? -val : val;
13547}
13548
13549/* The .file directive; just like the usual .file directive, but there
c5dd6aab
DJ
13550 is an initial number which is the ECOFF file index. In the non-ECOFF
13551 case .file implies DWARF-2. */
13552
13553static void
17a2f251 13554s_mips_file (int x ATTRIBUTE_UNUSED)
c5dd6aab 13555{
ecb4347a
DJ
13556 static int first_file_directive = 0;
13557
c5dd6aab
DJ
13558 if (ECOFF_DEBUGGING)
13559 {
13560 get_number ();
13561 s_app_file (0);
13562 }
13563 else
ecb4347a
DJ
13564 {
13565 char *filename;
13566
13567 filename = dwarf2_directive_file (0);
13568
13569 /* Versions of GCC up to 3.1 start files with a ".file"
13570 directive even for stabs output. Make sure that this
13571 ".file" is handled. Note that you need a version of GCC
13572 after 3.1 in order to support DWARF-2 on MIPS. */
13573 if (filename != NULL && ! first_file_directive)
13574 {
13575 (void) new_logical_line (filename, -1);
c04f5787 13576 s_app_file_string (filename, 0);
ecb4347a
DJ
13577 }
13578 first_file_directive = 1;
13579 }
c5dd6aab
DJ
13580}
13581
13582/* The .loc directive, implying DWARF-2. */
252b5132
RH
13583
13584static void
17a2f251 13585s_mips_loc (int x ATTRIBUTE_UNUSED)
252b5132 13586{
c5dd6aab
DJ
13587 if (!ECOFF_DEBUGGING)
13588 dwarf2_directive_loc (0);
252b5132
RH
13589}
13590
252b5132
RH
13591/* The .end directive. */
13592
13593static void
17a2f251 13594s_mips_end (int x ATTRIBUTE_UNUSED)
252b5132
RH
13595{
13596 symbolS *p;
252b5132 13597
7a621144
DJ
13598 /* Following functions need their own .frame and .cprestore directives. */
13599 mips_frame_reg_valid = 0;
13600 mips_cprestore_valid = 0;
13601
252b5132
RH
13602 if (!is_end_of_line[(unsigned char) *input_line_pointer])
13603 {
13604 p = get_symbol ();
13605 demand_empty_rest_of_line ();
13606 }
13607 else
13608 p = NULL;
13609
14949570 13610 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
13611 as_warn (_(".end not in text section"));
13612
13613 if (!cur_proc_ptr)
13614 {
13615 as_warn (_(".end directive without a preceding .ent directive."));
13616 demand_empty_rest_of_line ();
13617 return;
13618 }
13619
13620 if (p != NULL)
13621 {
13622 assert (S_GET_NAME (p));
13623 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->isym)))
13624 as_warn (_(".end symbol does not match .ent symbol."));
ecb4347a
DJ
13625
13626 if (debug_type == DEBUG_STABS)
13627 stabs_generate_asm_endfunc (S_GET_NAME (p),
13628 S_GET_NAME (p));
252b5132
RH
13629 }
13630 else
13631 as_warn (_(".end directive missing or unknown symbol"));
13632
ecb4347a
DJ
13633#ifdef OBJ_ELF
13634 /* Generate a .pdr section. */
dcd410fe
RO
13635 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING
13636 && mips_flag_pdr)
ecb4347a
DJ
13637 {
13638 segT saved_seg = now_seg;
13639 subsegT saved_subseg = now_subseg;
13640 valueT dot;
13641 expressionS exp;
13642 char *fragp;
252b5132 13643
ecb4347a 13644 dot = frag_now_fix ();
252b5132
RH
13645
13646#ifdef md_flush_pending_output
ecb4347a 13647 md_flush_pending_output ();
252b5132
RH
13648#endif
13649
ecb4347a
DJ
13650 assert (pdr_seg);
13651 subseg_set (pdr_seg, 0);
252b5132 13652
ecb4347a
DJ
13653 /* Write the symbol. */
13654 exp.X_op = O_symbol;
13655 exp.X_add_symbol = p;
13656 exp.X_add_number = 0;
13657 emit_expr (&exp, 4);
252b5132 13658
ecb4347a 13659 fragp = frag_more (7 * 4);
252b5132 13660
17a2f251
TS
13661 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
13662 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
13663 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
13664 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
13665 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
13666 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
13667 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
252b5132 13668
ecb4347a
DJ
13669 subseg_set (saved_seg, saved_subseg);
13670 }
13671#endif /* OBJ_ELF */
252b5132
RH
13672
13673 cur_proc_ptr = NULL;
13674}
13675
13676/* The .aent and .ent directives. */
13677
13678static void
17a2f251 13679s_mips_ent (int aent)
252b5132 13680{
252b5132 13681 symbolS *symbolP;
252b5132
RH
13682
13683 symbolP = get_symbol ();
13684 if (*input_line_pointer == ',')
f9419b05 13685 ++input_line_pointer;
252b5132 13686 SKIP_WHITESPACE ();
3882b010 13687 if (ISDIGIT (*input_line_pointer)
d9a62219 13688 || *input_line_pointer == '-')
874e8986 13689 get_number ();
252b5132 13690
14949570 13691 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
13692 as_warn (_(".ent or .aent not in text section."));
13693
13694 if (!aent && cur_proc_ptr)
9a41af64 13695 as_warn (_("missing .end"));
252b5132
RH
13696
13697 if (!aent)
13698 {
7a621144
DJ
13699 /* This function needs its own .frame and .cprestore directives. */
13700 mips_frame_reg_valid = 0;
13701 mips_cprestore_valid = 0;
13702
252b5132
RH
13703 cur_proc_ptr = &cur_proc;
13704 memset (cur_proc_ptr, '\0', sizeof (procS));
13705
13706 cur_proc_ptr->isym = symbolP;
13707
49309057 13708 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
252b5132 13709
f9419b05 13710 ++numprocs;
ecb4347a
DJ
13711
13712 if (debug_type == DEBUG_STABS)
13713 stabs_generate_asm_func (S_GET_NAME (symbolP),
13714 S_GET_NAME (symbolP));
252b5132
RH
13715 }
13716
13717 demand_empty_rest_of_line ();
13718}
13719
13720/* The .frame directive. If the mdebug section is present (IRIX 5 native)
bdaaa2e1 13721 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
252b5132 13722 s_mips_frame is used so that we can set the PDR information correctly.
bdaaa2e1 13723 We can't use the ecoff routines because they make reference to the ecoff
252b5132
RH
13724 symbol table (in the mdebug section). */
13725
13726static void
17a2f251 13727s_mips_frame (int ignore ATTRIBUTE_UNUSED)
252b5132 13728{
ecb4347a
DJ
13729#ifdef OBJ_ELF
13730 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
13731 {
13732 long val;
252b5132 13733
ecb4347a
DJ
13734 if (cur_proc_ptr == (procS *) NULL)
13735 {
13736 as_warn (_(".frame outside of .ent"));
13737 demand_empty_rest_of_line ();
13738 return;
13739 }
252b5132 13740
ecb4347a
DJ
13741 cur_proc_ptr->frame_reg = tc_get_register (1);
13742
13743 SKIP_WHITESPACE ();
13744 if (*input_line_pointer++ != ','
13745 || get_absolute_expression_and_terminator (&val) != ',')
13746 {
13747 as_warn (_("Bad .frame directive"));
13748 --input_line_pointer;
13749 demand_empty_rest_of_line ();
13750 return;
13751 }
252b5132 13752
ecb4347a
DJ
13753 cur_proc_ptr->frame_offset = val;
13754 cur_proc_ptr->pc_reg = tc_get_register (0);
252b5132 13755
252b5132 13756 demand_empty_rest_of_line ();
252b5132 13757 }
ecb4347a
DJ
13758 else
13759#endif /* OBJ_ELF */
13760 s_ignore (ignore);
252b5132
RH
13761}
13762
bdaaa2e1
KH
13763/* The .fmask and .mask directives. If the mdebug section is present
13764 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
252b5132 13765 embedded targets, s_mips_mask is used so that we can set the PDR
bdaaa2e1 13766 information correctly. We can't use the ecoff routines because they
252b5132
RH
13767 make reference to the ecoff symbol table (in the mdebug section). */
13768
13769static void
17a2f251 13770s_mips_mask (int reg_type)
252b5132 13771{
ecb4347a
DJ
13772#ifdef OBJ_ELF
13773 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
252b5132 13774 {
ecb4347a 13775 long mask, off;
252b5132 13776
ecb4347a
DJ
13777 if (cur_proc_ptr == (procS *) NULL)
13778 {
13779 as_warn (_(".mask/.fmask outside of .ent"));
13780 demand_empty_rest_of_line ();
13781 return;
13782 }
252b5132 13783
ecb4347a
DJ
13784 if (get_absolute_expression_and_terminator (&mask) != ',')
13785 {
13786 as_warn (_("Bad .mask/.fmask directive"));
13787 --input_line_pointer;
13788 demand_empty_rest_of_line ();
13789 return;
13790 }
252b5132 13791
ecb4347a
DJ
13792 off = get_absolute_expression ();
13793
13794 if (reg_type == 'F')
13795 {
13796 cur_proc_ptr->fpreg_mask = mask;
13797 cur_proc_ptr->fpreg_offset = off;
13798 }
13799 else
13800 {
13801 cur_proc_ptr->reg_mask = mask;
13802 cur_proc_ptr->reg_offset = off;
13803 }
13804
13805 demand_empty_rest_of_line ();
252b5132
RH
13806 }
13807 else
ecb4347a
DJ
13808#endif /* OBJ_ELF */
13809 s_ignore (reg_type);
252b5132
RH
13810}
13811
13812/* The .loc directive. */
13813
13814#if 0
13815static void
17a2f251 13816s_loc (int x)
252b5132
RH
13817{
13818 symbolS *symbolP;
13819 int lineno;
13820 int addroff;
13821
13822 assert (now_seg == text_section);
13823
13824 lineno = get_number ();
13825 addroff = frag_now_fix ();
13826
13827 symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
13828 S_SET_TYPE (symbolP, N_SLINE);
13829 S_SET_OTHER (symbolP, 0);
13830 S_SET_DESC (symbolP, lineno);
13831 symbolP->sy_segment = now_seg;
13832}
13833#endif
e7af610e 13834
316f5878
RS
13835/* A table describing all the processors gas knows about. Names are
13836 matched in the order listed.
e7af610e 13837
316f5878
RS
13838 To ease comparison, please keep this table in the same order as
13839 gcc's mips_cpu_info_table[]. */
e972090a
NC
13840static const struct mips_cpu_info mips_cpu_info_table[] =
13841{
316f5878
RS
13842 /* Entries for generic ISAs */
13843 { "mips1", 1, ISA_MIPS1, CPU_R3000 },
13844 { "mips2", 1, ISA_MIPS2, CPU_R6000 },
13845 { "mips3", 1, ISA_MIPS3, CPU_R4000 },
13846 { "mips4", 1, ISA_MIPS4, CPU_R8000 },
13847 { "mips5", 1, ISA_MIPS5, CPU_MIPS5 },
13848 { "mips32", 1, ISA_MIPS32, CPU_MIPS32 },
af7ee8bf 13849 { "mips32r2", 1, ISA_MIPS32R2, CPU_MIPS32R2 },
316f5878 13850 { "mips64", 1, ISA_MIPS64, CPU_MIPS64 },
5f74bc13 13851 { "mips64r2", 1, ISA_MIPS64R2, CPU_MIPS64R2 },
316f5878
RS
13852
13853 /* MIPS I */
13854 { "r3000", 0, ISA_MIPS1, CPU_R3000 },
13855 { "r2000", 0, ISA_MIPS1, CPU_R3000 },
13856 { "r3900", 0, ISA_MIPS1, CPU_R3900 },
13857
13858 /* MIPS II */
13859 { "r6000", 0, ISA_MIPS2, CPU_R6000 },
13860
13861 /* MIPS III */
13862 { "r4000", 0, ISA_MIPS3, CPU_R4000 },
13863 { "r4010", 0, ISA_MIPS2, CPU_R4010 },
13864 { "vr4100", 0, ISA_MIPS3, CPU_VR4100 },
13865 { "vr4111", 0, ISA_MIPS3, CPU_R4111 },
60b63b72
RS
13866 { "vr4120", 0, ISA_MIPS3, CPU_VR4120 },
13867 { "vr4130", 0, ISA_MIPS3, CPU_VR4120 },
13868 { "vr4181", 0, ISA_MIPS3, CPU_R4111 },
316f5878
RS
13869 { "vr4300", 0, ISA_MIPS3, CPU_R4300 },
13870 { "r4400", 0, ISA_MIPS3, CPU_R4400 },
13871 { "r4600", 0, ISA_MIPS3, CPU_R4600 },
13872 { "orion", 0, ISA_MIPS3, CPU_R4600 },
13873 { "r4650", 0, ISA_MIPS3, CPU_R4650 },
13874
13875 /* MIPS IV */
13876 { "r8000", 0, ISA_MIPS4, CPU_R8000 },
13877 { "r10000", 0, ISA_MIPS4, CPU_R10000 },
13878 { "r12000", 0, ISA_MIPS4, CPU_R12000 },
13879 { "vr5000", 0, ISA_MIPS4, CPU_R5000 },
60b63b72
RS
13880 { "vr5400", 0, ISA_MIPS4, CPU_VR5400 },
13881 { "vr5500", 0, ISA_MIPS4, CPU_VR5500 },
316f5878
RS
13882 { "rm5200", 0, ISA_MIPS4, CPU_R5000 },
13883 { "rm5230", 0, ISA_MIPS4, CPU_R5000 },
13884 { "rm5231", 0, ISA_MIPS4, CPU_R5000 },
13885 { "rm5261", 0, ISA_MIPS4, CPU_R5000 },
13886 { "rm5721", 0, ISA_MIPS4, CPU_R5000 },
5a7ea749 13887 { "rm7000", 0, ISA_MIPS4, CPU_RM7000 },
9a92f48d 13888 { "rm9000", 0, ISA_MIPS4, CPU_RM9000 },
316f5878
RS
13889
13890 /* MIPS 32 */
fef14a42 13891 { "4kc", 0, ISA_MIPS32, CPU_MIPS32 },
316f5878
RS
13892 { "4km", 0, ISA_MIPS32, CPU_MIPS32 },
13893 { "4kp", 0, ISA_MIPS32, CPU_MIPS32 },
e7af610e 13894
316f5878
RS
13895 /* MIPS 64 */
13896 { "5kc", 0, ISA_MIPS64, CPU_MIPS64 },
13897 { "20kc", 0, ISA_MIPS64, CPU_MIPS64 },
e7af610e 13898
c7a23324 13899 /* Broadcom SB-1 CPU core */
316f5878 13900 { "sb1", 0, ISA_MIPS64, CPU_SB1 },
e7af610e 13901
316f5878
RS
13902 /* End marker */
13903 { NULL, 0, 0, 0 }
13904};
e7af610e 13905
84ea6cf2 13906
316f5878
RS
13907/* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
13908 with a final "000" replaced by "k". Ignore case.
e7af610e 13909
316f5878 13910 Note: this function is shared between GCC and GAS. */
c6c98b38 13911
b34976b6 13912static bfd_boolean
17a2f251 13913mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
13914{
13915 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
13916 given++, canonical++;
13917
13918 return ((*given == 0 && *canonical == 0)
13919 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
13920}
13921
13922
13923/* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
13924 CPU name. We've traditionally allowed a lot of variation here.
13925
13926 Note: this function is shared between GCC and GAS. */
13927
b34976b6 13928static bfd_boolean
17a2f251 13929mips_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
13930{
13931 /* First see if the name matches exactly, or with a final "000"
13932 turned into "k". */
13933 if (mips_strict_matching_cpu_name_p (canonical, given))
b34976b6 13934 return TRUE;
316f5878
RS
13935
13936 /* If not, try comparing based on numerical designation alone.
13937 See if GIVEN is an unadorned number, or 'r' followed by a number. */
13938 if (TOLOWER (*given) == 'r')
13939 given++;
13940 if (!ISDIGIT (*given))
b34976b6 13941 return FALSE;
316f5878
RS
13942
13943 /* Skip over some well-known prefixes in the canonical name,
13944 hoping to find a number there too. */
13945 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
13946 canonical += 2;
13947 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
13948 canonical += 2;
13949 else if (TOLOWER (canonical[0]) == 'r')
13950 canonical += 1;
13951
13952 return mips_strict_matching_cpu_name_p (canonical, given);
13953}
13954
13955
13956/* Parse an option that takes the name of a processor as its argument.
13957 OPTION is the name of the option and CPU_STRING is the argument.
13958 Return the corresponding processor enumeration if the CPU_STRING is
13959 recognized, otherwise report an error and return null.
13960
13961 A similar function exists in GCC. */
e7af610e
NC
13962
13963static const struct mips_cpu_info *
17a2f251 13964mips_parse_cpu (const char *option, const char *cpu_string)
e7af610e 13965{
316f5878 13966 const struct mips_cpu_info *p;
e7af610e 13967
316f5878
RS
13968 /* 'from-abi' selects the most compatible architecture for the given
13969 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
13970 EABIs, we have to decide whether we're using the 32-bit or 64-bit
13971 version. Look first at the -mgp options, if given, otherwise base
13972 the choice on MIPS_DEFAULT_64BIT.
e7af610e 13973
316f5878
RS
13974 Treat NO_ABI like the EABIs. One reason to do this is that the
13975 plain 'mips' and 'mips64' configs have 'from-abi' as their default
13976 architecture. This code picks MIPS I for 'mips' and MIPS III for
13977 'mips64', just as we did in the days before 'from-abi'. */
13978 if (strcasecmp (cpu_string, "from-abi") == 0)
13979 {
13980 if (ABI_NEEDS_32BIT_REGS (mips_abi))
13981 return mips_cpu_info_from_isa (ISA_MIPS1);
13982
13983 if (ABI_NEEDS_64BIT_REGS (mips_abi))
13984 return mips_cpu_info_from_isa (ISA_MIPS3);
13985
13986 if (file_mips_gp32 >= 0)
13987 return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
13988
13989 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
13990 ? ISA_MIPS3
13991 : ISA_MIPS1);
13992 }
13993
13994 /* 'default' has traditionally been a no-op. Probably not very useful. */
13995 if (strcasecmp (cpu_string, "default") == 0)
13996 return 0;
13997
13998 for (p = mips_cpu_info_table; p->name != 0; p++)
13999 if (mips_matching_cpu_name_p (p->name, cpu_string))
14000 return p;
14001
14002 as_bad ("Bad value (%s) for %s", cpu_string, option);
14003 return 0;
e7af610e
NC
14004}
14005
316f5878
RS
14006/* Return the canonical processor information for ISA (a member of the
14007 ISA_MIPS* enumeration). */
14008
e7af610e 14009static const struct mips_cpu_info *
17a2f251 14010mips_cpu_info_from_isa (int isa)
e7af610e
NC
14011{
14012 int i;
14013
14014 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14015 if (mips_cpu_info_table[i].is_isa
316f5878 14016 && isa == mips_cpu_info_table[i].isa)
e7af610e
NC
14017 return (&mips_cpu_info_table[i]);
14018
e972090a 14019 return NULL;
e7af610e 14020}
fef14a42
TS
14021
14022static const struct mips_cpu_info *
17a2f251 14023mips_cpu_info_from_arch (int arch)
fef14a42
TS
14024{
14025 int i;
14026
14027 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14028 if (arch == mips_cpu_info_table[i].cpu)
14029 return (&mips_cpu_info_table[i]);
14030
14031 return NULL;
14032}
316f5878
RS
14033\f
14034static void
17a2f251 14035show (FILE *stream, const char *string, int *col_p, int *first_p)
316f5878
RS
14036{
14037 if (*first_p)
14038 {
14039 fprintf (stream, "%24s", "");
14040 *col_p = 24;
14041 }
14042 else
14043 {
14044 fprintf (stream, ", ");
14045 *col_p += 2;
14046 }
e7af610e 14047
316f5878
RS
14048 if (*col_p + strlen (string) > 72)
14049 {
14050 fprintf (stream, "\n%24s", "");
14051 *col_p = 24;
14052 }
14053
14054 fprintf (stream, "%s", string);
14055 *col_p += strlen (string);
14056
14057 *first_p = 0;
14058}
14059
14060void
17a2f251 14061md_show_usage (FILE *stream)
e7af610e 14062{
316f5878
RS
14063 int column, first;
14064 size_t i;
14065
14066 fprintf (stream, _("\
14067MIPS options:\n\
316f5878
RS
14068-EB generate big endian output\n\
14069-EL generate little endian output\n\
14070-g, -g2 do not remove unneeded NOPs or swap branches\n\
14071-G NUM allow referencing objects up to NUM bytes\n\
14072 implicitly with the gp register [default 8]\n"));
14073 fprintf (stream, _("\
14074-mips1 generate MIPS ISA I instructions\n\
14075-mips2 generate MIPS ISA II instructions\n\
14076-mips3 generate MIPS ISA III instructions\n\
14077-mips4 generate MIPS ISA IV instructions\n\
14078-mips5 generate MIPS ISA V instructions\n\
14079-mips32 generate MIPS32 ISA instructions\n\
af7ee8bf 14080-mips32r2 generate MIPS32 release 2 ISA instructions\n\
316f5878 14081-mips64 generate MIPS64 ISA instructions\n\
5f74bc13 14082-mips64r2 generate MIPS64 release 2 ISA instructions\n\
316f5878
RS
14083-march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
14084
14085 first = 1;
e7af610e
NC
14086
14087 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
316f5878
RS
14088 show (stream, mips_cpu_info_table[i].name, &column, &first);
14089 show (stream, "from-abi", &column, &first);
14090 fputc ('\n', stream);
e7af610e 14091
316f5878
RS
14092 fprintf (stream, _("\
14093-mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
14094-no-mCPU don't generate code specific to CPU.\n\
14095 For -mCPU and -no-mCPU, CPU must be one of:\n"));
14096
14097 first = 1;
14098
14099 show (stream, "3900", &column, &first);
14100 show (stream, "4010", &column, &first);
14101 show (stream, "4100", &column, &first);
14102 show (stream, "4650", &column, &first);
14103 fputc ('\n', stream);
14104
14105 fprintf (stream, _("\
14106-mips16 generate mips16 instructions\n\
14107-no-mips16 do not generate mips16 instructions\n"));
14108 fprintf (stream, _("\
d766e8ec 14109-mfix-vr4120 work around certain VR4120 errata\n\
316f5878
RS
14110-mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
14111-mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
14112-O0 remove unneeded NOPs, do not swap branches\n\
14113-O remove unneeded NOPs and swap branches\n\
316f5878
RS
14114--[no-]construct-floats [dis]allow floating point values to be constructed\n\
14115--trap, --no-break trap exception on div by 0 and mult overflow\n\
14116--break, --no-trap break exception on div by 0 and mult overflow\n"));
14117#ifdef OBJ_ELF
14118 fprintf (stream, _("\
14119-KPIC, -call_shared generate SVR4 position independent code\n\
14120-non_shared do not generate position independent code\n\
14121-xgot assume a 32 bit GOT\n\
dcd410fe 14122-mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
316f5878
RS
14123-mabi=ABI create ABI conformant object file for:\n"));
14124
14125 first = 1;
14126
14127 show (stream, "32", &column, &first);
14128 show (stream, "o64", &column, &first);
14129 show (stream, "n32", &column, &first);
14130 show (stream, "64", &column, &first);
14131 show (stream, "eabi", &column, &first);
14132
14133 fputc ('\n', stream);
14134
14135 fprintf (stream, _("\
14136-32 create o32 ABI object file (default)\n\
14137-n32 create n32 ABI object file\n\
14138-64 create 64 ABI object file\n"));
14139#endif
e7af610e 14140}
14e777e0
KB
14141
14142enum dwarf2_format
17a2f251 14143mips_dwarf2_format (void)
14e777e0
KB
14144{
14145 if (mips_abi == N64_ABI)
1de5b6a1
AO
14146 {
14147#ifdef TE_IRIX
14148 return dwarf2_format_64bit_irix;
14149#else
14150 return dwarf2_format_64bit;
14151#endif
14152 }
14e777e0
KB
14153 else
14154 return dwarf2_format_32bit;
14155}
73369e65
EC
14156
14157int
14158mips_dwarf2_addr_size (void)
14159{
14160 if (mips_abi == N64_ABI)
14161 return 8;
73369e65
EC
14162 else
14163 return 4;
14164}