2004-02-26 Andrew Cagney <cagney@redhat.com>
[deliverable/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
RH
113/* The name of the readonly data section. */
114#define RDATA_SECTION_NAME (OUTPUT_FLAVOR == bfd_target_aout_flavour \
115 ? ".data" \
116 : OUTPUT_FLAVOR == bfd_target_ecoff_flavour \
117 ? ".rdata" \
056350c6
NC
118 : OUTPUT_FLAVOR == bfd_target_coff_flavour \
119 ? ".rdata" \
252b5132
RH
120 : OUTPUT_FLAVOR == bfd_target_elf_flavour \
121 ? ".rodata" \
122 : (abort (), ""))
123
a325df1d
TS
124/* The ABI to use. */
125enum mips_abi_level
126{
127 NO_ABI = 0,
128 O32_ABI,
129 O64_ABI,
130 N32_ABI,
131 N64_ABI,
132 EABI_ABI
133};
134
135/* MIPS ABI we are using for this output file. */
316f5878 136static enum mips_abi_level mips_abi = NO_ABI;
a325df1d 137
143d77c5
EC
138/* Whether or not we have code that can call pic code. */
139int mips_abicalls = FALSE;
140
252b5132
RH
141/* This is the set of options which may be modified by the .set
142 pseudo-op. We use a struct so that .set push and .set pop are more
143 reliable. */
144
e972090a
NC
145struct mips_set_options
146{
252b5132
RH
147 /* MIPS ISA (Instruction Set Architecture) level. This is set to -1
148 if it has not been initialized. Changed by `.set mipsN', and the
149 -mipsN command line option, and the default CPU. */
150 int isa;
1f25f5d3
CD
151 /* Enabled Application Specific Extensions (ASEs). These are set to -1
152 if they have not been initialized. Changed by `.set <asename>', by
153 command line options, and based on the default architecture. */
154 int ase_mips3d;
deec1734 155 int ase_mdmx;
252b5132
RH
156 /* Whether we are assembling for the mips16 processor. 0 if we are
157 not, 1 if we are, and -1 if the value has not been initialized.
158 Changed by `.set mips16' and `.set nomips16', and the -mips16 and
159 -nomips16 command line options, and the default CPU. */
160 int mips16;
161 /* Non-zero if we should not reorder instructions. Changed by `.set
162 reorder' and `.set noreorder'. */
163 int noreorder;
164 /* Non-zero if we should not permit the $at ($1) register to be used
165 in instructions. Changed by `.set at' and `.set noat'. */
166 int noat;
167 /* Non-zero if we should warn when a macro instruction expands into
168 more than one machine instruction. Changed by `.set nomacro' and
169 `.set macro'. */
170 int warn_about_macros;
171 /* Non-zero if we should not move instructions. Changed by `.set
172 move', `.set volatile', `.set nomove', and `.set novolatile'. */
173 int nomove;
174 /* Non-zero if we should not optimize branches by moving the target
175 of the branch into the delay slot. Actually, we don't perform
176 this optimization anyhow. Changed by `.set bopt' and `.set
177 nobopt'. */
178 int nobopt;
179 /* Non-zero if we should not autoextend mips16 instructions.
180 Changed by `.set autoextend' and `.set noautoextend'. */
181 int noautoextend;
a325df1d
TS
182 /* Restrict general purpose registers and floating point registers
183 to 32 bit. This is initially determined when -mgp32 or -mfp32
184 is passed but can changed if the assembler code uses .set mipsN. */
185 int gp32;
186 int fp32;
fef14a42
TS
187 /* MIPS architecture (CPU) type. Changed by .set arch=FOO, the -march
188 command line option, and the default CPU. */
189 int arch;
252b5132
RH
190};
191
a325df1d 192/* True if -mgp32 was passed. */
a8e8e863 193static int file_mips_gp32 = -1;
a325df1d
TS
194
195/* True if -mfp32 was passed. */
a8e8e863 196static int file_mips_fp32 = -1;
a325df1d 197
252b5132 198/* This is the struct we use to hold the current set of options. Note
a4672219 199 that we must set the isa field to ISA_UNKNOWN and the ASE fields to
e7af610e 200 -1 to indicate that they have not been initialized. */
252b5132 201
e972090a
NC
202static struct mips_set_options mips_opts =
203{
fef14a42 204 ISA_UNKNOWN, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, CPU_UNKNOWN
e7af610e 205};
252b5132
RH
206
207/* These variables are filled in with the masks of registers used.
208 The object format code reads them and puts them in the appropriate
209 place. */
210unsigned long mips_gprmask;
211unsigned long mips_cprmask[4];
212
213/* MIPS ISA we are using for this output file. */
e7af610e 214static int file_mips_isa = ISA_UNKNOWN;
252b5132 215
a4672219
TS
216/* True if -mips16 was passed or implied by arguments passed on the
217 command line (e.g., by -march). */
218static int file_ase_mips16;
219
1f25f5d3
CD
220/* True if -mips3d was passed or implied by arguments passed on the
221 command line (e.g., by -march). */
222static int file_ase_mips3d;
223
deec1734
CD
224/* True if -mdmx was passed or implied by arguments passed on the
225 command line (e.g., by -march). */
226static int file_ase_mdmx;
227
ec68c924 228/* The argument of the -march= flag. The architecture we are assembling. */
fef14a42 229static int file_mips_arch = CPU_UNKNOWN;
316f5878 230static const char *mips_arch_string;
ec68c924
EC
231
232/* The argument of the -mtune= flag. The architecture for which we
233 are optimizing. */
234static int mips_tune = CPU_UNKNOWN;
316f5878 235static const char *mips_tune_string;
ec68c924 236
316f5878 237/* True when generating 32-bit code for a 64-bit processor. */
252b5132
RH
238static int mips_32bitmode = 0;
239
316f5878
RS
240/* True if the given ABI requires 32-bit registers. */
241#define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
242
243/* Likewise 64-bit registers. */
244#define ABI_NEEDS_64BIT_REGS(ABI) \
245 ((ABI) == N32_ABI \
246 || (ABI) == N64_ABI \
247 || (ABI) == O64_ABI)
248
bdaaa2e1 249/* Return true if ISA supports 64 bit gp register instructions. */
9ce8a5dd 250#define ISA_HAS_64BIT_REGS(ISA) ( \
e7af610e
NC
251 (ISA) == ISA_MIPS3 \
252 || (ISA) == ISA_MIPS4 \
84ea6cf2 253 || (ISA) == ISA_MIPS5 \
d1cf510e 254 || (ISA) == ISA_MIPS64 \
5f74bc13 255 || (ISA) == ISA_MIPS64R2 \
9ce8a5dd
GRK
256 )
257
af7ee8bf
CD
258/* Return true if ISA supports 64-bit right rotate (dror et al.)
259 instructions. */
260#define ISA_HAS_DROR(ISA) ( \
5f74bc13 261 (ISA) == ISA_MIPS64R2 \
af7ee8bf
CD
262 )
263
264/* Return true if ISA supports 32-bit right rotate (ror et al.)
265 instructions. */
266#define ISA_HAS_ROR(ISA) ( \
267 (ISA) == ISA_MIPS32R2 \
5f74bc13 268 || (ISA) == ISA_MIPS64R2 \
af7ee8bf
CD
269 )
270
e013f690 271#define HAVE_32BIT_GPRS \
316f5878 272 (mips_opts.gp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
ca4e0257 273
e013f690 274#define HAVE_32BIT_FPRS \
316f5878 275 (mips_opts.fp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
ca4e0257
RS
276
277#define HAVE_64BIT_GPRS (! HAVE_32BIT_GPRS)
278#define HAVE_64BIT_FPRS (! HAVE_32BIT_FPRS)
279
316f5878 280#define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
e013f690 281
316f5878 282#define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
e013f690
TS
283
284/* We can only have 64bit addresses if the object file format
285 supports it. */
afdbd6d0
CD
286#define HAVE_32BIT_ADDRESSES \
287 (HAVE_32BIT_GPRS \
288 || ((bfd_arch_bits_per_address (stdoutput) == 32 \
289 || ! HAVE_64BIT_OBJECTS) \
290 && mips_pic != EMBEDDED_PIC))
e013f690
TS
291
292#define HAVE_64BIT_ADDRESSES (! HAVE_32BIT_ADDRESSES)
ca4e0257 293
b7c7d6c1
TS
294/* Addresses are loaded in different ways, depending on the address size
295 in use. The n32 ABI Documentation also mandates the use of additions
296 with overflow checking, but existing implementations don't follow it. */
f899b4b8 297#define ADDRESS_ADD_INSN \
b7c7d6c1 298 (HAVE_32BIT_ADDRESSES ? "addu" : "daddu")
f899b4b8
TS
299
300#define ADDRESS_ADDI_INSN \
b7c7d6c1 301 (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu")
f899b4b8
TS
302
303#define ADDRESS_LOAD_INSN \
304 (HAVE_32BIT_ADDRESSES ? "lw" : "ld")
305
306#define ADDRESS_STORE_INSN \
307 (HAVE_32BIT_ADDRESSES ? "sw" : "sd")
308
a4672219 309/* Return true if the given CPU supports the MIPS16 ASE. */
3396de36
TS
310#define CPU_HAS_MIPS16(cpu) \
311 (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0 \
312 || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
a4672219 313
1f25f5d3
CD
314/* Return true if the given CPU supports the MIPS3D ASE. */
315#define CPU_HAS_MIPS3D(cpu) ((cpu) == CPU_SB1 \
316 )
317
deec1734 318/* Return true if the given CPU supports the MDMX ASE. */
b34976b6 319#define CPU_HAS_MDMX(cpu) (FALSE \
deec1734
CD
320 )
321
60b63b72
RS
322/* True if CPU has a dror instruction. */
323#define CPU_HAS_DROR(CPU) ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
324
325/* True if CPU has a ror instruction. */
326#define CPU_HAS_ROR(CPU) CPU_HAS_DROR (CPU)
327
c8978940
CD
328/* True if mflo and mfhi can be immediately followed by instructions
329 which write to the HI and LO registers.
330
331 According to MIPS specifications, MIPS ISAs I, II, and III need
332 (at least) two instructions between the reads of HI/LO and
333 instructions which write them, and later ISAs do not. Contradicting
334 the MIPS specifications, some MIPS IV processor user manuals (e.g.
335 the UM for the NEC Vr5000) document needing the instructions between
336 HI/LO reads and writes, as well. Therefore, we declare only MIPS32,
337 MIPS64 and later ISAs to have the interlocks, plus any specific
338 earlier-ISA CPUs for which CPU documentation declares that the
339 instructions are really interlocked. */
340#define hilo_interlocks \
341 (mips_opts.isa == ISA_MIPS32 \
342 || mips_opts.isa == ISA_MIPS32R2 \
343 || mips_opts.isa == ISA_MIPS64 \
344 || mips_opts.isa == ISA_MIPS64R2 \
345 || mips_opts.arch == CPU_R4010 \
346 || mips_opts.arch == CPU_R10000 \
347 || mips_opts.arch == CPU_R12000 \
348 || mips_opts.arch == CPU_RM7000 \
349 || mips_opts.arch == CPU_SB1 \
350 || mips_opts.arch == CPU_VR5500 \
351 )
252b5132
RH
352
353/* Whether the processor uses hardware interlocks to protect reads
81912461
ILT
354 from the GPRs after they are loaded from memory, and thus does not
355 require nops to be inserted. This applies to instructions marked
356 INSN_LOAD_MEMORY_DELAY. These nops are only required at MIPS ISA
357 level I. */
252b5132 358#define gpr_interlocks \
e7af610e 359 (mips_opts.isa != ISA_MIPS1 \
fef14a42
TS
360 || mips_opts.arch == CPU_VR5400 \
361 || mips_opts.arch == CPU_VR5500 \
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 \
377 || mips_opts.arch == CPU_VR5400 \
378 || mips_opts.arch == CPU_VR5500 \
379 || mips_opts.arch == CPU_SB1 \
380 )
381
382/* Whether the processor uses hardware interlocks to protect reads
383 from coprocessor registers after they are loaded from memory, and
384 thus does not require nops to be inserted. This applies to
385 instructions marked INSN_COPROC_MEMORY_DELAY. These nops are only
386 requires at MIPS ISA level I. */
387#define cop_mem_interlocks (mips_opts.isa != ISA_MIPS1)
252b5132 388
6b76fefe
CM
389/* Is this a mfhi or mflo instruction? */
390#define MF_HILO_INSN(PINFO) \
391 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
392
252b5132
RH
393/* MIPS PIC level. */
394
a161fe53 395enum mips_pic_level mips_pic;
252b5132 396
c9914766 397/* 1 if we should generate 32 bit offsets from the $gp register in
252b5132 398 SVR4_PIC mode. Currently has no meaning in other modes. */
c9914766 399static int mips_big_got = 0;
252b5132
RH
400
401/* 1 if trap instructions should used for overflow rather than break
402 instructions. */
c9914766 403static int mips_trap = 0;
252b5132 404
119d663a 405/* 1 if double width floating point constants should not be constructed
b6ff326e 406 by assembling two single width halves into two single width floating
119d663a
NC
407 point registers which just happen to alias the double width destination
408 register. On some architectures this aliasing can be disabled by a bit
d547a75e 409 in the status register, and the setting of this bit cannot be determined
119d663a
NC
410 automatically at assemble time. */
411static int mips_disable_float_construction;
412
252b5132
RH
413/* Non-zero if any .set noreorder directives were used. */
414
415static int mips_any_noreorder;
416
6b76fefe
CM
417/* Non-zero if nops should be inserted when the register referenced in
418 an mfhi/mflo instruction is read in the next two instructions. */
419static int mips_7000_hilo_fix;
420
252b5132 421/* The size of the small data section. */
156c2f8b 422static unsigned int g_switch_value = 8;
252b5132
RH
423/* Whether the -G option was used. */
424static int g_switch_seen = 0;
425
426#define N_RMASK 0xc4
427#define N_VFP 0xd4
428
429/* If we can determine in advance that GP optimization won't be
430 possible, we can skip the relaxation stuff that tries to produce
431 GP-relative references. This makes delay slot optimization work
432 better.
433
434 This function can only provide a guess, but it seems to work for
fba2b7f9
GK
435 gcc output. It needs to guess right for gcc, otherwise gcc
436 will put what it thinks is a GP-relative instruction in a branch
437 delay slot.
252b5132
RH
438
439 I don't know if a fix is needed for the SVR4_PIC mode. I've only
440 fixed it for the non-PIC mode. KR 95/04/07 */
17a2f251 441static int nopic_need_relax (symbolS *, int);
252b5132
RH
442
443/* handle of the OPCODE hash table */
444static struct hash_control *op_hash = NULL;
445
446/* The opcode hash table we use for the mips16. */
447static struct hash_control *mips16_op_hash = NULL;
448
449/* This array holds the chars that always start a comment. If the
450 pre-processor is disabled, these aren't very useful */
451const char comment_chars[] = "#";
452
453/* This array holds the chars that only start a comment at the beginning of
454 a line. If the line seems to have the form '# 123 filename'
455 .line and .file directives will appear in the pre-processed output */
456/* Note that input_file.c hand checks for '#' at the beginning of the
457 first line of the input file. This is because the compiler outputs
bdaaa2e1 458 #NO_APP at the beginning of its output. */
252b5132
RH
459/* Also note that C style comments are always supported. */
460const char line_comment_chars[] = "#";
461
bdaaa2e1 462/* This array holds machine specific line separator characters. */
63a0b638 463const char line_separator_chars[] = ";";
252b5132
RH
464
465/* Chars that can be used to separate mant from exp in floating point nums */
466const char EXP_CHARS[] = "eE";
467
468/* Chars that mean this number is a floating point constant */
469/* As in 0f12.456 */
470/* or 0d1.2345e12 */
471const char FLT_CHARS[] = "rRsSfFdDxXpP";
472
473/* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
474 changed in read.c . Ideally it shouldn't have to know about it at all,
475 but nothing is ideal around here.
476 */
477
478static char *insn_error;
479
480static int auto_align = 1;
481
482/* When outputting SVR4 PIC code, the assembler needs to know the
483 offset in the stack frame from which to restore the $gp register.
484 This is set by the .cprestore pseudo-op, and saved in this
485 variable. */
486static offsetT mips_cprestore_offset = -1;
487
67c1ffbe 488/* Similar for NewABI PIC code, where $gp is callee-saved. NewABI has some
6478892d 489 more optimizations, it can use a register value instead of a memory-saved
956cd1d6 490 offset and even an other register than $gp as global pointer. */
6478892d
TS
491static offsetT mips_cpreturn_offset = -1;
492static int mips_cpreturn_register = -1;
493static int mips_gp_register = GP;
def2e0dd 494static int mips_gprel_offset = 0;
6478892d 495
7a621144
DJ
496/* Whether mips_cprestore_offset has been set in the current function
497 (or whether it has already been warned about, if not). */
498static int mips_cprestore_valid = 0;
499
252b5132
RH
500/* This is the register which holds the stack frame, as set by the
501 .frame pseudo-op. This is needed to implement .cprestore. */
502static int mips_frame_reg = SP;
503
7a621144
DJ
504/* Whether mips_frame_reg has been set in the current function
505 (or whether it has already been warned about, if not). */
506static int mips_frame_reg_valid = 0;
507
252b5132
RH
508/* To output NOP instructions correctly, we need to keep information
509 about the previous two instructions. */
510
511/* Whether we are optimizing. The default value of 2 means to remove
512 unneeded NOPs and swap branch instructions when possible. A value
513 of 1 means to not swap branches. A value of 0 means to always
514 insert NOPs. */
515static int mips_optimize = 2;
516
517/* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
518 equivalent to seeing no -g option at all. */
519static int mips_debug = 0;
520
521/* The previous instruction. */
522static struct mips_cl_insn prev_insn;
523
524/* The instruction before prev_insn. */
525static struct mips_cl_insn prev_prev_insn;
526
527/* If we don't want information for prev_insn or prev_prev_insn, we
528 point the insn_mo field at this dummy integer. */
43841e91 529static const struct mips_opcode dummy_opcode = { NULL, NULL, 0, 0, 0, 0 };
252b5132
RH
530
531/* Non-zero if prev_insn is valid. */
532static int prev_insn_valid;
533
534/* The frag for the previous instruction. */
535static struct frag *prev_insn_frag;
536
537/* The offset into prev_insn_frag for the previous instruction. */
538static long prev_insn_where;
539
540/* The reloc type for the previous instruction, if any. */
f6688943 541static bfd_reloc_code_real_type prev_insn_reloc_type[3];
252b5132
RH
542
543/* The reloc for the previous instruction, if any. */
f6688943 544static fixS *prev_insn_fixp[3];
252b5132
RH
545
546/* Non-zero if the previous instruction was in a delay slot. */
547static int prev_insn_is_delay_slot;
548
549/* Non-zero if the previous instruction was in a .set noreorder. */
550static int prev_insn_unreordered;
551
552/* Non-zero if the previous instruction uses an extend opcode (if
553 mips16). */
554static int prev_insn_extended;
555
556/* Non-zero if the previous previous instruction was in a .set
557 noreorder. */
558static int prev_prev_insn_unreordered;
559
560/* If this is set, it points to a frag holding nop instructions which
561 were inserted before the start of a noreorder section. If those
562 nops turn out to be unnecessary, the size of the frag can be
563 decreased. */
564static fragS *prev_nop_frag;
565
566/* The number of nop instructions we created in prev_nop_frag. */
567static int prev_nop_frag_holds;
568
569/* The number of nop instructions that we know we need in
bdaaa2e1 570 prev_nop_frag. */
252b5132
RH
571static int prev_nop_frag_required;
572
573/* The number of instructions we've seen since prev_nop_frag. */
574static int prev_nop_frag_since;
575
576/* For ECOFF and ELF, relocations against symbols are done in two
577 parts, with a HI relocation and a LO relocation. Each relocation
578 has only 16 bits of space to store an addend. This means that in
579 order for the linker to handle carries correctly, it must be able
580 to locate both the HI and the LO relocation. This means that the
581 relocations must appear in order in the relocation table.
582
583 In order to implement this, we keep track of each unmatched HI
584 relocation. We then sort them so that they immediately precede the
bdaaa2e1 585 corresponding LO relocation. */
252b5132 586
e972090a
NC
587struct mips_hi_fixup
588{
252b5132
RH
589 /* Next HI fixup. */
590 struct mips_hi_fixup *next;
591 /* This fixup. */
592 fixS *fixp;
593 /* The section this fixup is in. */
594 segT seg;
595};
596
597/* The list of unmatched HI relocs. */
598
599static struct mips_hi_fixup *mips_hi_fixup_list;
600
64bdfcaf
RS
601/* The frag containing the last explicit relocation operator.
602 Null if explicit relocations have not been used. */
603
604static fragS *prev_reloc_op_frag;
605
252b5132
RH
606/* Map normal MIPS register numbers to mips16 register numbers. */
607
608#define X ILLEGAL_REG
e972090a
NC
609static const int mips32_to_16_reg_map[] =
610{
252b5132
RH
611 X, X, 2, 3, 4, 5, 6, 7,
612 X, X, X, X, X, X, X, X,
613 0, 1, X, X, X, X, X, X,
614 X, X, X, X, X, X, X, X
615};
616#undef X
617
618/* Map mips16 register numbers to normal MIPS register numbers. */
619
e972090a
NC
620static const unsigned int mips16_to_32_reg_map[] =
621{
252b5132
RH
622 16, 17, 2, 3, 4, 5, 6, 7
623};
60b63b72
RS
624
625static int mips_fix_4122_bugs;
4a6a3df4
AO
626
627/* We don't relax branches by default, since this causes us to expand
628 `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
629 fail to compute the offset before expanding the macro to the most
630 efficient expansion. */
631
632static int mips_relax_branch;
252b5132 633\f
4d7206a2
RS
634/* The expansion of many macros depends on the type of symbol that
635 they refer to. For example, when generating position-dependent code,
636 a macro that refers to a symbol may have two different expansions,
637 one which uses GP-relative addresses and one which uses absolute
638 addresses. When generating SVR4-style PIC, a macro may have
639 different expansions for local and global symbols.
640
641 We handle these situations by generating both sequences and putting
642 them in variant frags. In position-dependent code, the first sequence
643 will be the GP-relative one and the second sequence will be the
644 absolute one. In SVR4 PIC, the first sequence will be for global
645 symbols and the second will be for local symbols.
646
584892a6
RS
647 The frag's "subtype" is RELAX_ENCODE (FIRST, SECOND), where FIRST and
648 SECOND are the lengths of the two sequences in bytes. These fields
649 can be extracted using RELAX_FIRST() and RELAX_SECOND(). In addition,
650 the subtype has the following flags:
4d7206a2 651
584892a6
RS
652 RELAX_USE_SECOND
653 Set if it has been decided that we should use the second
654 sequence instead of the first.
655
656 RELAX_SECOND_LONGER
657 Set in the first variant frag if the macro's second implementation
658 is longer than its first. This refers to the macro as a whole,
659 not an individual relaxation.
660
661 RELAX_NOMACRO
662 Set in the first variant frag if the macro appeared in a .set nomacro
663 block and if one alternative requires a warning but the other does not.
664
665 RELAX_DELAY_SLOT
666 Like RELAX_NOMACRO, but indicates that the macro appears in a branch
667 delay slot.
4d7206a2
RS
668
669 The frag's "opcode" points to the first fixup for relaxable code.
670
671 Relaxable macros are generated using a sequence such as:
672
673 relax_start (SYMBOL);
674 ... generate first expansion ...
675 relax_switch ();
676 ... generate second expansion ...
677 relax_end ();
678
679 The code and fixups for the unwanted alternative are discarded
680 by md_convert_frag. */
584892a6 681#define RELAX_ENCODE(FIRST, SECOND) (((FIRST) << 8) | (SECOND))
4d7206a2 682
584892a6
RS
683#define RELAX_FIRST(X) (((X) >> 8) & 0xff)
684#define RELAX_SECOND(X) ((X) & 0xff)
685#define RELAX_USE_SECOND 0x10000
686#define RELAX_SECOND_LONGER 0x20000
687#define RELAX_NOMACRO 0x40000
688#define RELAX_DELAY_SLOT 0x80000
252b5132 689
4a6a3df4
AO
690/* Branch without likely bit. If label is out of range, we turn:
691
692 beq reg1, reg2, label
693 delay slot
694
695 into
696
697 bne reg1, reg2, 0f
698 nop
699 j label
700 0: delay slot
701
702 with the following opcode replacements:
703
704 beq <-> bne
705 blez <-> bgtz
706 bltz <-> bgez
707 bc1f <-> bc1t
708
709 bltzal <-> bgezal (with jal label instead of j label)
710
711 Even though keeping the delay slot instruction in the delay slot of
712 the branch would be more efficient, it would be very tricky to do
713 correctly, because we'd have to introduce a variable frag *after*
714 the delay slot instruction, and expand that instead. Let's do it
715 the easy way for now, even if the branch-not-taken case now costs
716 one additional instruction. Out-of-range branches are not supposed
717 to be common, anyway.
718
719 Branch likely. If label is out of range, we turn:
720
721 beql reg1, reg2, label
722 delay slot (annulled if branch not taken)
723
724 into
725
726 beql reg1, reg2, 1f
727 nop
728 beql $0, $0, 2f
729 nop
730 1: j[al] label
731 delay slot (executed only if branch taken)
732 2:
733
734 It would be possible to generate a shorter sequence by losing the
735 likely bit, generating something like:
b34976b6 736
4a6a3df4
AO
737 bne reg1, reg2, 0f
738 nop
739 j[al] label
740 delay slot (executed only if branch taken)
741 0:
742
743 beql -> bne
744 bnel -> beq
745 blezl -> bgtz
746 bgtzl -> blez
747 bltzl -> bgez
748 bgezl -> bltz
749 bc1fl -> bc1t
750 bc1tl -> bc1f
751
752 bltzall -> bgezal (with jal label instead of j label)
753 bgezall -> bltzal (ditto)
754
755
756 but it's not clear that it would actually improve performance. */
af6ae2ad 757#define RELAX_BRANCH_ENCODE(uncond, likely, link, toofar) \
4a6a3df4
AO
758 ((relax_substateT) \
759 (0xc0000000 \
760 | ((toofar) ? 1 : 0) \
761 | ((link) ? 2 : 0) \
762 | ((likely) ? 4 : 0) \
af6ae2ad 763 | ((uncond) ? 8 : 0)))
4a6a3df4 764#define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
4a6a3df4
AO
765#define RELAX_BRANCH_UNCOND(i) (((i) & 8) != 0)
766#define RELAX_BRANCH_LIKELY(i) (((i) & 4) != 0)
767#define RELAX_BRANCH_LINK(i) (((i) & 2) != 0)
ae6063d4 768#define RELAX_BRANCH_TOOFAR(i) (((i) & 1) != 0)
4a6a3df4 769
252b5132
RH
770/* For mips16 code, we use an entirely different form of relaxation.
771 mips16 supports two versions of most instructions which take
772 immediate values: a small one which takes some small value, and a
773 larger one which takes a 16 bit value. Since branches also follow
774 this pattern, relaxing these values is required.
775
776 We can assemble both mips16 and normal MIPS code in a single
777 object. Therefore, we need to support this type of relaxation at
778 the same time that we support the relaxation described above. We
779 use the high bit of the subtype field to distinguish these cases.
780
781 The information we store for this type of relaxation is the
782 argument code found in the opcode file for this relocation, whether
783 the user explicitly requested a small or extended form, and whether
784 the relocation is in a jump or jal delay slot. That tells us the
785 size of the value, and how it should be stored. We also store
786 whether the fragment is considered to be extended or not. We also
787 store whether this is known to be a branch to a different section,
788 whether we have tried to relax this frag yet, and whether we have
789 ever extended a PC relative fragment because of a shift count. */
790#define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
791 (0x80000000 \
792 | ((type) & 0xff) \
793 | ((small) ? 0x100 : 0) \
794 | ((ext) ? 0x200 : 0) \
795 | ((dslot) ? 0x400 : 0) \
796 | ((jal_dslot) ? 0x800 : 0))
4a6a3df4 797#define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
252b5132
RH
798#define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
799#define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
800#define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
801#define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
802#define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
803#define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
804#define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
805#define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
806#define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
807#define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
808#define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
885add95
CD
809
810/* Is the given value a sign-extended 32-bit value? */
811#define IS_SEXT_32BIT_NUM(x) \
812 (((x) &~ (offsetT) 0x7fffffff) == 0 \
813 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
814
815/* Is the given value a sign-extended 16-bit value? */
816#define IS_SEXT_16BIT_NUM(x) \
817 (((x) &~ (offsetT) 0x7fff) == 0 \
818 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
819
4d7206a2
RS
820\f
821/* Global variables used when generating relaxable macros. See the
822 comment above RELAX_ENCODE for more details about how relaxation
823 is used. */
824static struct {
825 /* 0 if we're not emitting a relaxable macro.
826 1 if we're emitting the first of the two relaxation alternatives.
827 2 if we're emitting the second alternative. */
828 int sequence;
829
830 /* The first relaxable fixup in the current frag. (In other words,
831 the first fixup that refers to relaxable code.) */
832 fixS *first_fixup;
833
834 /* sizes[0] says how many bytes of the first alternative are stored in
835 the current frag. Likewise sizes[1] for the second alternative. */
836 unsigned int sizes[2];
837
838 /* The symbol on which the choice of sequence depends. */
839 symbolS *symbol;
840} mips_relax;
252b5132 841\f
584892a6
RS
842/* Global variables used to decide whether a macro needs a warning. */
843static struct {
844 /* True if the macro is in a branch delay slot. */
845 bfd_boolean delay_slot_p;
846
847 /* For relaxable macros, sizes[0] is the length of the first alternative
848 in bytes and sizes[1] is the length of the second alternative.
849 For non-relaxable macros, both elements give the length of the
850 macro in bytes. */
851 unsigned int sizes[2];
852
853 /* The first variant frag for this macro. */
854 fragS *first_frag;
855} mips_macro_warning;
856\f
252b5132
RH
857/* Prototypes for static functions. */
858
17a2f251 859#define internalError() \
252b5132 860 as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
252b5132
RH
861
862enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
863
b34976b6 864static void append_insn
4d7206a2 865 (struct mips_cl_insn *ip, expressionS *p, bfd_reloc_code_real_type *r);
17a2f251 866static void mips_no_prev_insn (int);
b34976b6 867static void mips16_macro_build
67c0d1eb
RS
868 (expressionS *, const char *, const char *, va_list);
869static void load_register (int, expressionS *, int);
584892a6
RS
870static void macro_start (void);
871static void macro_end (void);
17a2f251
TS
872static void macro (struct mips_cl_insn * ip);
873static void mips16_macro (struct mips_cl_insn * ip);
252b5132 874#ifdef LOSING_COMPILER
17a2f251 875static void macro2 (struct mips_cl_insn * ip);
252b5132 876#endif
17a2f251
TS
877static void mips_ip (char *str, struct mips_cl_insn * ip);
878static void mips16_ip (char *str, struct mips_cl_insn * ip);
b34976b6 879static void mips16_immed
17a2f251
TS
880 (char *, unsigned int, int, offsetT, bfd_boolean, bfd_boolean, bfd_boolean,
881 unsigned long *, bfd_boolean *, unsigned short *);
5e0116d5 882static size_t my_getSmallExpression
17a2f251
TS
883 (expressionS *, bfd_reloc_code_real_type *, char *);
884static void my_getExpression (expressionS *, char *);
885static void s_align (int);
886static void s_change_sec (int);
887static void s_change_section (int);
888static void s_cons (int);
889static void s_float_cons (int);
890static void s_mips_globl (int);
891static void s_option (int);
892static void s_mipsset (int);
893static void s_abicalls (int);
894static void s_cpload (int);
895static void s_cpsetup (int);
896static void s_cplocal (int);
897static void s_cprestore (int);
898static void s_cpreturn (int);
899static void s_gpvalue (int);
900static void s_gpword (int);
901static void s_gpdword (int);
902static void s_cpadd (int);
903static void s_insn (int);
904static void md_obj_begin (void);
905static void md_obj_end (void);
906static void s_mips_ent (int);
907static void s_mips_end (int);
908static void s_mips_frame (int);
909static void s_mips_mask (int reg_type);
910static void s_mips_stab (int);
911static void s_mips_weakext (int);
912static void s_mips_file (int);
913static void s_mips_loc (int);
914static bfd_boolean pic_need_relax (symbolS *, asection *);
4a6a3df4 915static int relaxed_branch_length (fragS *, asection *, int);
17a2f251 916static int validate_mips_insn (const struct mips_opcode *);
e7af610e
NC
917
918/* Table and functions used to map between CPU/ISA names, and
919 ISA levels, and CPU numbers. */
920
e972090a
NC
921struct mips_cpu_info
922{
e7af610e
NC
923 const char *name; /* CPU or ISA name. */
924 int is_isa; /* Is this an ISA? (If 0, a CPU.) */
925 int isa; /* ISA level. */
926 int cpu; /* CPU number (default CPU if ISA). */
927};
928
17a2f251
TS
929static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
930static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
931static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
252b5132
RH
932\f
933/* Pseudo-op table.
934
935 The following pseudo-ops from the Kane and Heinrich MIPS book
936 should be defined here, but are currently unsupported: .alias,
937 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
938
939 The following pseudo-ops from the Kane and Heinrich MIPS book are
940 specific to the type of debugging information being generated, and
941 should be defined by the object format: .aent, .begin, .bend,
942 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
943 .vreg.
944
945 The following pseudo-ops from the Kane and Heinrich MIPS book are
946 not MIPS CPU specific, but are also not specific to the object file
947 format. This file is probably the best place to define them, but
948 they are not currently supported: .asm0, .endr, .lab, .repeat,
949 .struct. */
950
e972090a
NC
951static const pseudo_typeS mips_pseudo_table[] =
952{
beae10d5 953 /* MIPS specific pseudo-ops. */
252b5132
RH
954 {"option", s_option, 0},
955 {"set", s_mipsset, 0},
956 {"rdata", s_change_sec, 'r'},
957 {"sdata", s_change_sec, 's'},
958 {"livereg", s_ignore, 0},
959 {"abicalls", s_abicalls, 0},
960 {"cpload", s_cpload, 0},
6478892d
TS
961 {"cpsetup", s_cpsetup, 0},
962 {"cplocal", s_cplocal, 0},
252b5132 963 {"cprestore", s_cprestore, 0},
6478892d
TS
964 {"cpreturn", s_cpreturn, 0},
965 {"gpvalue", s_gpvalue, 0},
252b5132 966 {"gpword", s_gpword, 0},
10181a0d 967 {"gpdword", s_gpdword, 0},
252b5132
RH
968 {"cpadd", s_cpadd, 0},
969 {"insn", s_insn, 0},
970
beae10d5 971 /* Relatively generic pseudo-ops that happen to be used on MIPS
252b5132
RH
972 chips. */
973 {"asciiz", stringer, 1},
974 {"bss", s_change_sec, 'b'},
975 {"err", s_err, 0},
976 {"half", s_cons, 1},
977 {"dword", s_cons, 3},
978 {"weakext", s_mips_weakext, 0},
979
beae10d5 980 /* These pseudo-ops are defined in read.c, but must be overridden
252b5132
RH
981 here for one reason or another. */
982 {"align", s_align, 0},
983 {"byte", s_cons, 0},
984 {"data", s_change_sec, 'd'},
985 {"double", s_float_cons, 'd'},
986 {"float", s_float_cons, 'f'},
987 {"globl", s_mips_globl, 0},
988 {"global", s_mips_globl, 0},
989 {"hword", s_cons, 1},
990 {"int", s_cons, 2},
991 {"long", s_cons, 2},
992 {"octa", s_cons, 4},
993 {"quad", s_cons, 3},
cca86cc8 994 {"section", s_change_section, 0},
252b5132
RH
995 {"short", s_cons, 1},
996 {"single", s_float_cons, 'f'},
997 {"stabn", s_mips_stab, 'n'},
998 {"text", s_change_sec, 't'},
999 {"word", s_cons, 2},
add56521 1000
add56521 1001 { "extern", ecoff_directive_extern, 0},
add56521 1002
43841e91 1003 { NULL, NULL, 0 },
252b5132
RH
1004};
1005
e972090a
NC
1006static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1007{
beae10d5
KH
1008 /* These pseudo-ops should be defined by the object file format.
1009 However, a.out doesn't support them, so we have versions here. */
252b5132
RH
1010 {"aent", s_mips_ent, 1},
1011 {"bgnb", s_ignore, 0},
1012 {"end", s_mips_end, 0},
1013 {"endb", s_ignore, 0},
1014 {"ent", s_mips_ent, 0},
c5dd6aab 1015 {"file", s_mips_file, 0},
252b5132
RH
1016 {"fmask", s_mips_mask, 'F'},
1017 {"frame", s_mips_frame, 0},
c5dd6aab 1018 {"loc", s_mips_loc, 0},
252b5132
RH
1019 {"mask", s_mips_mask, 'R'},
1020 {"verstamp", s_ignore, 0},
43841e91 1021 { NULL, NULL, 0 },
252b5132
RH
1022};
1023
17a2f251 1024extern void pop_insert (const pseudo_typeS *);
252b5132
RH
1025
1026void
17a2f251 1027mips_pop_insert (void)
252b5132
RH
1028{
1029 pop_insert (mips_pseudo_table);
1030 if (! ECOFF_DEBUGGING)
1031 pop_insert (mips_nonecoff_pseudo_table);
1032}
1033\f
1034/* Symbols labelling the current insn. */
1035
e972090a
NC
1036struct insn_label_list
1037{
252b5132
RH
1038 struct insn_label_list *next;
1039 symbolS *label;
1040};
1041
1042static struct insn_label_list *insn_labels;
1043static struct insn_label_list *free_insn_labels;
1044
17a2f251 1045static void mips_clear_insn_labels (void);
252b5132
RH
1046
1047static inline void
17a2f251 1048mips_clear_insn_labels (void)
252b5132
RH
1049{
1050 register struct insn_label_list **pl;
1051
1052 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1053 ;
1054 *pl = insn_labels;
1055 insn_labels = NULL;
1056}
1057\f
1058static char *expr_end;
1059
1060/* Expressions which appear in instructions. These are set by
1061 mips_ip. */
1062
1063static expressionS imm_expr;
5f74bc13 1064static expressionS imm2_expr;
252b5132
RH
1065static expressionS offset_expr;
1066
1067/* Relocs associated with imm_expr and offset_expr. */
1068
f6688943
TS
1069static bfd_reloc_code_real_type imm_reloc[3]
1070 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1071static bfd_reloc_code_real_type offset_reloc[3]
1072 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 1073
252b5132
RH
1074/* These are set by mips16_ip if an explicit extension is used. */
1075
b34976b6 1076static bfd_boolean mips16_small, mips16_ext;
252b5132 1077
7ed4a06a 1078#ifdef OBJ_ELF
ecb4347a
DJ
1079/* The pdr segment for per procedure frame/regmask info. Not used for
1080 ECOFF debugging. */
252b5132
RH
1081
1082static segT pdr_seg;
7ed4a06a 1083#endif
252b5132 1084
e013f690
TS
1085/* The default target format to use. */
1086
1087const char *
17a2f251 1088mips_target_format (void)
e013f690
TS
1089{
1090 switch (OUTPUT_FLAVOR)
1091 {
1092 case bfd_target_aout_flavour:
1093 return target_big_endian ? "a.out-mips-big" : "a.out-mips-little";
1094 case bfd_target_ecoff_flavour:
1095 return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
1096 case bfd_target_coff_flavour:
1097 return "pe-mips";
1098 case bfd_target_elf_flavour:
1099#ifdef TE_TMIPS
cfe86eaa 1100 /* This is traditional mips. */
e013f690 1101 return (target_big_endian
cfe86eaa
TS
1102 ? (HAVE_64BIT_OBJECTS
1103 ? "elf64-tradbigmips"
1104 : (HAVE_NEWABI
1105 ? "elf32-ntradbigmips" : "elf32-tradbigmips"))
1106 : (HAVE_64BIT_OBJECTS
1107 ? "elf64-tradlittlemips"
1108 : (HAVE_NEWABI
1109 ? "elf32-ntradlittlemips" : "elf32-tradlittlemips")));
e013f690
TS
1110#else
1111 return (target_big_endian
cfe86eaa
TS
1112 ? (HAVE_64BIT_OBJECTS
1113 ? "elf64-bigmips"
1114 : (HAVE_NEWABI
1115 ? "elf32-nbigmips" : "elf32-bigmips"))
1116 : (HAVE_64BIT_OBJECTS
1117 ? "elf64-littlemips"
1118 : (HAVE_NEWABI
1119 ? "elf32-nlittlemips" : "elf32-littlemips")));
e013f690
TS
1120#endif
1121 default:
1122 abort ();
1123 return NULL;
1124 }
1125}
1126
156c2f8b
NC
1127/* This function is called once, at assembler startup time. It should
1128 set up all the tables, etc. that the MD part of the assembler will need. */
1129
252b5132 1130void
17a2f251 1131md_begin (void)
252b5132 1132{
252b5132 1133 register const char *retval = NULL;
156c2f8b 1134 int i = 0;
252b5132 1135 int broken = 0;
1f25f5d3 1136
fef14a42 1137 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_arch))
252b5132
RH
1138 as_warn (_("Could not set architecture and machine"));
1139
252b5132
RH
1140 op_hash = hash_new ();
1141
1142 for (i = 0; i < NUMOPCODES;)
1143 {
1144 const char *name = mips_opcodes[i].name;
1145
17a2f251 1146 retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
252b5132
RH
1147 if (retval != NULL)
1148 {
1149 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1150 mips_opcodes[i].name, retval);
1151 /* Probably a memory allocation problem? Give up now. */
1152 as_fatal (_("Broken assembler. No assembly attempted."));
1153 }
1154 do
1155 {
1156 if (mips_opcodes[i].pinfo != INSN_MACRO)
1157 {
1158 if (!validate_mips_insn (&mips_opcodes[i]))
1159 broken = 1;
1160 }
1161 ++i;
1162 }
1163 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1164 }
1165
1166 mips16_op_hash = hash_new ();
1167
1168 i = 0;
1169 while (i < bfd_mips16_num_opcodes)
1170 {
1171 const char *name = mips16_opcodes[i].name;
1172
17a2f251 1173 retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
252b5132
RH
1174 if (retval != NULL)
1175 as_fatal (_("internal: can't hash `%s': %s"),
1176 mips16_opcodes[i].name, retval);
1177 do
1178 {
1179 if (mips16_opcodes[i].pinfo != INSN_MACRO
1180 && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1181 != mips16_opcodes[i].match))
1182 {
1183 fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1184 mips16_opcodes[i].name, mips16_opcodes[i].args);
1185 broken = 1;
1186 }
1187 ++i;
1188 }
1189 while (i < bfd_mips16_num_opcodes
1190 && strcmp (mips16_opcodes[i].name, name) == 0);
1191 }
1192
1193 if (broken)
1194 as_fatal (_("Broken assembler. No assembly attempted."));
1195
1196 /* We add all the general register names to the symbol table. This
1197 helps us detect invalid uses of them. */
1198 for (i = 0; i < 32; i++)
1199 {
1200 char buf[5];
1201
1202 sprintf (buf, "$%d", i);
1203 symbol_table_insert (symbol_new (buf, reg_section, i,
1204 &zero_address_frag));
1205 }
76db943d
TS
1206 symbol_table_insert (symbol_new ("$ra", reg_section, RA,
1207 &zero_address_frag));
252b5132
RH
1208 symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1209 &zero_address_frag));
1210 symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1211 &zero_address_frag));
1212 symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1213 &zero_address_frag));
1214 symbol_table_insert (symbol_new ("$at", reg_section, AT,
1215 &zero_address_frag));
1216 symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1217 &zero_address_frag));
1218 symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1219 &zero_address_frag));
85b51719
TS
1220 symbol_table_insert (symbol_new ("$zero", reg_section, ZERO,
1221 &zero_address_frag));
252b5132
RH
1222 symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1223 &zero_address_frag));
1224
6047c971
AO
1225 /* If we don't add these register names to the symbol table, they
1226 may end up being added as regular symbols by operand(), and then
1227 make it to the object file as undefined in case they're not
1228 regarded as local symbols. They're local in o32, since `$' is a
1229 local symbol prefix, but not in n32 or n64. */
1230 for (i = 0; i < 8; i++)
1231 {
1232 char buf[6];
1233
1234 sprintf (buf, "$fcc%i", i);
1235 symbol_table_insert (symbol_new (buf, reg_section, -1,
1236 &zero_address_frag));
1237 }
1238
b34976b6 1239 mips_no_prev_insn (FALSE);
252b5132
RH
1240
1241 mips_gprmask = 0;
1242 mips_cprmask[0] = 0;
1243 mips_cprmask[1] = 0;
1244 mips_cprmask[2] = 0;
1245 mips_cprmask[3] = 0;
1246
1247 /* set the default alignment for the text section (2**2) */
1248 record_alignment (text_section, 2);
1249
1250 if (USE_GLOBAL_POINTER_OPT)
1251 bfd_set_gp_size (stdoutput, g_switch_value);
1252
1253 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1254 {
1255 /* On a native system, sections must be aligned to 16 byte
1256 boundaries. When configured for an embedded ELF target, we
1257 don't bother. */
1258 if (strcmp (TARGET_OS, "elf") != 0)
1259 {
1260 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1261 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1262 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1263 }
1264
1265 /* Create a .reginfo section for register masks and a .mdebug
1266 section for debugging information. */
1267 {
1268 segT seg;
1269 subsegT subseg;
1270 flagword flags;
1271 segT sec;
1272
1273 seg = now_seg;
1274 subseg = now_subseg;
1275
1276 /* The ABI says this section should be loaded so that the
1277 running program can access it. However, we don't load it
1278 if we are configured for an embedded target */
1279 flags = SEC_READONLY | SEC_DATA;
1280 if (strcmp (TARGET_OS, "elf") != 0)
1281 flags |= SEC_ALLOC | SEC_LOAD;
1282
316f5878 1283 if (mips_abi != N64_ABI)
252b5132
RH
1284 {
1285 sec = subseg_new (".reginfo", (subsegT) 0);
1286
195325d2
TS
1287 bfd_set_section_flags (stdoutput, sec, flags);
1288 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
bdaaa2e1 1289
252b5132
RH
1290#ifdef OBJ_ELF
1291 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1292#endif
1293 }
1294 else
1295 {
1296 /* The 64-bit ABI uses a .MIPS.options section rather than
1297 .reginfo section. */
1298 sec = subseg_new (".MIPS.options", (subsegT) 0);
195325d2
TS
1299 bfd_set_section_flags (stdoutput, sec, flags);
1300 bfd_set_section_alignment (stdoutput, sec, 3);
252b5132
RH
1301
1302#ifdef OBJ_ELF
1303 /* Set up the option header. */
1304 {
1305 Elf_Internal_Options opthdr;
1306 char *f;
1307
1308 opthdr.kind = ODK_REGINFO;
1309 opthdr.size = (sizeof (Elf_External_Options)
1310 + sizeof (Elf64_External_RegInfo));
1311 opthdr.section = 0;
1312 opthdr.info = 0;
1313 f = frag_more (sizeof (Elf_External_Options));
1314 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1315 (Elf_External_Options *) f);
1316
1317 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1318 }
1319#endif
1320 }
1321
1322 if (ECOFF_DEBUGGING)
1323 {
1324 sec = subseg_new (".mdebug", (subsegT) 0);
1325 (void) bfd_set_section_flags (stdoutput, sec,
1326 SEC_HAS_CONTENTS | SEC_READONLY);
1327 (void) bfd_set_section_alignment (stdoutput, sec, 2);
1328 }
ecb4347a 1329#ifdef OBJ_ELF
dcd410fe 1330 else if (OUTPUT_FLAVOR == bfd_target_elf_flavour && mips_flag_pdr)
ecb4347a
DJ
1331 {
1332 pdr_seg = subseg_new (".pdr", (subsegT) 0);
1333 (void) bfd_set_section_flags (stdoutput, pdr_seg,
1334 SEC_READONLY | SEC_RELOC
1335 | SEC_DEBUGGING);
1336 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1337 }
252b5132
RH
1338#endif
1339
1340 subseg_set (seg, subseg);
1341 }
1342 }
1343
1344 if (! ECOFF_DEBUGGING)
1345 md_obj_begin ();
1346}
1347
1348void
17a2f251 1349md_mips_end (void)
252b5132
RH
1350{
1351 if (! ECOFF_DEBUGGING)
1352 md_obj_end ();
1353}
1354
1355void
17a2f251 1356md_assemble (char *str)
252b5132
RH
1357{
1358 struct mips_cl_insn insn;
f6688943
TS
1359 bfd_reloc_code_real_type unused_reloc[3]
1360 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132
RH
1361
1362 imm_expr.X_op = O_absent;
5f74bc13 1363 imm2_expr.X_op = O_absent;
252b5132 1364 offset_expr.X_op = O_absent;
f6688943
TS
1365 imm_reloc[0] = BFD_RELOC_UNUSED;
1366 imm_reloc[1] = BFD_RELOC_UNUSED;
1367 imm_reloc[2] = BFD_RELOC_UNUSED;
1368 offset_reloc[0] = BFD_RELOC_UNUSED;
1369 offset_reloc[1] = BFD_RELOC_UNUSED;
1370 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
1371
1372 if (mips_opts.mips16)
1373 mips16_ip (str, &insn);
1374 else
1375 {
1376 mips_ip (str, &insn);
beae10d5
KH
1377 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1378 str, insn.insn_opcode));
252b5132
RH
1379 }
1380
1381 if (insn_error)
1382 {
1383 as_bad ("%s `%s'", insn_error, str);
1384 return;
1385 }
1386
1387 if (insn.insn_mo->pinfo == INSN_MACRO)
1388 {
584892a6 1389 macro_start ();
252b5132
RH
1390 if (mips_opts.mips16)
1391 mips16_macro (&insn);
1392 else
1393 macro (&insn);
584892a6 1394 macro_end ();
252b5132
RH
1395 }
1396 else
1397 {
1398 if (imm_expr.X_op != O_absent)
4d7206a2 1399 append_insn (&insn, &imm_expr, imm_reloc);
252b5132 1400 else if (offset_expr.X_op != O_absent)
4d7206a2 1401 append_insn (&insn, &offset_expr, offset_reloc);
252b5132 1402 else
4d7206a2 1403 append_insn (&insn, NULL, unused_reloc);
252b5132
RH
1404 }
1405}
1406
5919d012
RS
1407/* Return true if the given relocation might need a matching %lo().
1408 Note that R_MIPS_GOT16 relocations only need a matching %lo() when
1409 applied to local symbols. */
1410
1411static inline bfd_boolean
17a2f251 1412reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
5919d012
RS
1413{
1414 return (reloc == BFD_RELOC_HI16_S
1415 || reloc == BFD_RELOC_MIPS_GOT16);
1416}
1417
1418/* Return true if the given fixup is followed by a matching R_MIPS_LO16
1419 relocation. */
1420
1421static inline bfd_boolean
17a2f251 1422fixup_has_matching_lo_p (fixS *fixp)
5919d012
RS
1423{
1424 return (fixp->fx_next != NULL
1425 && fixp->fx_next->fx_r_type == BFD_RELOC_LO16
1426 && fixp->fx_addsy == fixp->fx_next->fx_addsy
1427 && fixp->fx_offset == fixp->fx_next->fx_offset);
1428}
1429
252b5132
RH
1430/* See whether instruction IP reads register REG. CLASS is the type
1431 of register. */
1432
1433static int
17a2f251
TS
1434insn_uses_reg (struct mips_cl_insn *ip, unsigned int reg,
1435 enum mips_regclass class)
252b5132
RH
1436{
1437 if (class == MIPS16_REG)
1438 {
1439 assert (mips_opts.mips16);
1440 reg = mips16_to_32_reg_map[reg];
1441 class = MIPS_GR_REG;
1442 }
1443
85b51719
TS
1444 /* Don't report on general register ZERO, since it never changes. */
1445 if (class == MIPS_GR_REG && reg == ZERO)
252b5132
RH
1446 return 0;
1447
1448 if (class == MIPS_FP_REG)
1449 {
1450 assert (! mips_opts.mips16);
1451 /* If we are called with either $f0 or $f1, we must check $f0.
1452 This is not optimal, because it will introduce an unnecessary
1453 NOP between "lwc1 $f0" and "swc1 $f1". To fix this we would
1454 need to distinguish reading both $f0 and $f1 or just one of
1455 them. Note that we don't have to check the other way,
1456 because there is no instruction that sets both $f0 and $f1
1457 and requires a delay. */
1458 if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1459 && ((((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS) &~(unsigned)1)
1460 == (reg &~ (unsigned) 1)))
1461 return 1;
1462 if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1463 && ((((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT) &~(unsigned)1)
1464 == (reg &~ (unsigned) 1)))
1465 return 1;
1466 }
1467 else if (! mips_opts.mips16)
1468 {
1469 if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1470 && ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == reg)
1471 return 1;
1472 if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1473 && ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT) == reg)
1474 return 1;
1475 }
1476 else
1477 {
1478 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1479 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RX)
1480 & MIPS16OP_MASK_RX)]
1481 == reg))
1482 return 1;
1483 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1484 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RY)
1485 & MIPS16OP_MASK_RY)]
1486 == reg))
1487 return 1;
1488 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1489 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
1490 & MIPS16OP_MASK_MOVE32Z)]
1491 == reg))
1492 return 1;
1493 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1494 return 1;
1495 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1496 return 1;
1497 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1498 return 1;
1499 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1500 && ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
1501 & MIPS16OP_MASK_REGR32) == reg)
1502 return 1;
1503 }
1504
1505 return 0;
1506}
1507
1508/* This function returns true if modifying a register requires a
1509 delay. */
1510
1511static int
17a2f251 1512reg_needs_delay (unsigned int reg)
252b5132
RH
1513{
1514 unsigned long prev_pinfo;
1515
1516 prev_pinfo = prev_insn.insn_mo->pinfo;
1517 if (! mips_opts.noreorder
81912461
ILT
1518 && (((prev_pinfo & INSN_LOAD_MEMORY_DELAY)
1519 && ! gpr_interlocks)
1520 || ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1521 && ! cop_interlocks)))
252b5132 1522 {
81912461
ILT
1523 /* A load from a coprocessor or from memory. All load delays
1524 delay the use of general register rt for one instruction. */
bdaaa2e1 1525 /* Itbl support may require additional care here. */
252b5132
RH
1526 know (prev_pinfo & INSN_WRITE_GPR_T);
1527 if (reg == ((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT))
1528 return 1;
1529 }
1530
1531 return 0;
1532}
1533
1534/* Mark instruction labels in mips16 mode. This permits the linker to
1535 handle them specially, such as generating jalx instructions when
1536 needed. We also make them odd for the duration of the assembly, in
1537 order to generate the right sort of code. We will make them even
1538 in the adjust_symtab routine, while leaving them marked. This is
1539 convenient for the debugger and the disassembler. The linker knows
1540 to make them odd again. */
1541
1542static void
17a2f251 1543mips16_mark_labels (void)
252b5132
RH
1544{
1545 if (mips_opts.mips16)
1546 {
1547 struct insn_label_list *l;
98aa84af 1548 valueT val;
252b5132
RH
1549
1550 for (l = insn_labels; l != NULL; l = l->next)
1551 {
1552#ifdef OBJ_ELF
1553 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1554 S_SET_OTHER (l->label, STO_MIPS16);
1555#endif
98aa84af
AM
1556 val = S_GET_VALUE (l->label);
1557 if ((val & 1) == 0)
1558 S_SET_VALUE (l->label, val + 1);
252b5132
RH
1559 }
1560 }
1561}
1562
4d7206a2
RS
1563/* End the current frag. Make it a variant frag and record the
1564 relaxation info. */
1565
1566static void
1567relax_close_frag (void)
1568{
584892a6 1569 mips_macro_warning.first_frag = frag_now;
4d7206a2 1570 frag_var (rs_machine_dependent, 0, 0,
584892a6 1571 RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1]),
4d7206a2
RS
1572 mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
1573
1574 memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
1575 mips_relax.first_fixup = 0;
1576}
1577
1578/* Start a new relaxation sequence whose expansion depends on SYMBOL.
1579 See the comment above RELAX_ENCODE for more details. */
1580
1581static void
1582relax_start (symbolS *symbol)
1583{
1584 assert (mips_relax.sequence == 0);
1585 mips_relax.sequence = 1;
1586 mips_relax.symbol = symbol;
1587}
1588
1589/* Start generating the second version of a relaxable sequence.
1590 See the comment above RELAX_ENCODE for more details. */
252b5132
RH
1591
1592static void
4d7206a2
RS
1593relax_switch (void)
1594{
1595 assert (mips_relax.sequence == 1);
1596 mips_relax.sequence = 2;
1597}
1598
1599/* End the current relaxable sequence. */
1600
1601static void
1602relax_end (void)
1603{
1604 assert (mips_relax.sequence == 2);
1605 relax_close_frag ();
1606 mips_relax.sequence = 0;
1607}
1608
1609/* Output an instruction. IP is the instruction information.
1610 ADDRESS_EXPR is an operand of the instruction to be used with
1611 RELOC_TYPE. */
1612
1613static void
1614append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
17a2f251 1615 bfd_reloc_code_real_type *reloc_type)
252b5132
RH
1616{
1617 register unsigned long prev_pinfo, pinfo;
1618 char *f;
f6688943 1619 fixS *fixp[3];
252b5132 1620 int nops = 0;
895921c9
MR
1621 relax_stateT prev_insn_frag_type = 0;
1622 bfd_boolean relaxed_branch = FALSE;
f5040a92 1623 bfd_boolean force_new_frag = FALSE;
252b5132
RH
1624
1625 /* Mark instruction labels in mips16 mode. */
f9419b05 1626 mips16_mark_labels ();
252b5132
RH
1627
1628 prev_pinfo = prev_insn.insn_mo->pinfo;
1629 pinfo = ip->insn_mo->pinfo;
1630
4d7206a2
RS
1631 if (mips_relax.sequence != 2
1632 && (!mips_opts.noreorder || prev_nop_frag != NULL))
252b5132
RH
1633 {
1634 int prev_prev_nop;
1635
1636 /* If the previous insn required any delay slots, see if we need
1637 to insert a NOP or two. There are eight kinds of possible
1638 hazards, of which an instruction can have at most one type.
1639 (1) a load from memory delay
1640 (2) a load from a coprocessor delay
1641 (3) an unconditional branch delay
1642 (4) a conditional branch delay
1643 (5) a move to coprocessor register delay
1644 (6) a load coprocessor register from memory delay
1645 (7) a coprocessor condition code delay
1646 (8) a HI/LO special register delay
1647
1648 There are a lot of optimizations we could do that we don't.
1649 In particular, we do not, in general, reorder instructions.
1650 If you use gcc with optimization, it will reorder
1651 instructions and generally do much more optimization then we
1652 do here; repeating all that work in the assembler would only
1653 benefit hand written assembly code, and does not seem worth
1654 it. */
1655
1656 /* This is how a NOP is emitted. */
1657#define emit_nop() \
1658 (mips_opts.mips16 \
1659 ? md_number_to_chars (frag_more (2), 0x6500, 2) \
1660 : md_number_to_chars (frag_more (4), 0, 4))
1661
1662 /* The previous insn might require a delay slot, depending upon
1663 the contents of the current insn. */
1664 if (! mips_opts.mips16
81912461
ILT
1665 && (((prev_pinfo & INSN_LOAD_MEMORY_DELAY)
1666 && ! gpr_interlocks)
1667 || ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1668 && ! cop_interlocks)))
252b5132
RH
1669 {
1670 /* A load from a coprocessor or from memory. All load
1671 delays delay the use of general register rt for one
81912461 1672 instruction. */
beae10d5 1673 /* Itbl support may require additional care here. */
252b5132
RH
1674 know (prev_pinfo & INSN_WRITE_GPR_T);
1675 if (mips_optimize == 0
1676 || insn_uses_reg (ip,
1677 ((prev_insn.insn_opcode >> OP_SH_RT)
1678 & OP_MASK_RT),
1679 MIPS_GR_REG))
1680 ++nops;
1681 }
1682 else if (! mips_opts.mips16
252b5132 1683 && (((prev_pinfo & INSN_COPROC_MOVE_DELAY)
beae10d5 1684 && ! cop_interlocks)
81912461
ILT
1685 || ((prev_pinfo & INSN_COPROC_MEMORY_DELAY)
1686 && ! cop_mem_interlocks)))
252b5132
RH
1687 {
1688 /* A generic coprocessor delay. The previous instruction
1689 modified a coprocessor general or control register. If
1690 it modified a control register, we need to avoid any
1691 coprocessor instruction (this is probably not always
1692 required, but it sometimes is). If it modified a general
1693 register, we avoid using that register.
1694
252b5132
RH
1695 This case is not handled very well. There is no special
1696 knowledge of CP0 handling, and the coprocessors other
1697 than the floating point unit are not distinguished at
1698 all. */
1699 /* Itbl support may require additional care here. FIXME!
bdaaa2e1 1700 Need to modify this to include knowledge about
252b5132
RH
1701 user specified delays! */
1702 if (prev_pinfo & INSN_WRITE_FPR_T)
1703 {
1704 if (mips_optimize == 0
1705 || insn_uses_reg (ip,
1706 ((prev_insn.insn_opcode >> OP_SH_FT)
1707 & OP_MASK_FT),
1708 MIPS_FP_REG))
1709 ++nops;
1710 }
1711 else if (prev_pinfo & INSN_WRITE_FPR_S)
1712 {
1713 if (mips_optimize == 0
1714 || insn_uses_reg (ip,
1715 ((prev_insn.insn_opcode >> OP_SH_FS)
1716 & OP_MASK_FS),
1717 MIPS_FP_REG))
1718 ++nops;
1719 }
1720 else
1721 {
1722 /* We don't know exactly what the previous instruction
1723 does. If the current instruction uses a coprocessor
1724 register, we must insert a NOP. If previous
1725 instruction may set the condition codes, and the
1726 current instruction uses them, we must insert two
1727 NOPS. */
bdaaa2e1 1728 /* Itbl support may require additional care here. */
252b5132
RH
1729 if (mips_optimize == 0
1730 || ((prev_pinfo & INSN_WRITE_COND_CODE)
1731 && (pinfo & INSN_READ_COND_CODE)))
1732 nops += 2;
1733 else if (pinfo & INSN_COP)
1734 ++nops;
1735 }
1736 }
1737 else if (! mips_opts.mips16
252b5132
RH
1738 && (prev_pinfo & INSN_WRITE_COND_CODE)
1739 && ! cop_interlocks)
1740 {
1741 /* The previous instruction sets the coprocessor condition
1742 codes, but does not require a general coprocessor delay
1743 (this means it is a floating point comparison
1744 instruction). If this instruction uses the condition
1745 codes, we need to insert a single NOP. */
beae10d5 1746 /* Itbl support may require additional care here. */
252b5132
RH
1747 if (mips_optimize == 0
1748 || (pinfo & INSN_READ_COND_CODE))
1749 ++nops;
1750 }
6b76fefe
CM
1751
1752 /* If we're fixing up mfhi/mflo for the r7000 and the
1753 previous insn was an mfhi/mflo and the current insn
1754 reads the register that the mfhi/mflo wrote to, then
1755 insert two nops. */
1756
1757 else if (mips_7000_hilo_fix
1758 && MF_HILO_INSN (prev_pinfo)
1759 && insn_uses_reg (ip, ((prev_insn.insn_opcode >> OP_SH_RD)
beae10d5
KH
1760 & OP_MASK_RD),
1761 MIPS_GR_REG))
6b76fefe
CM
1762 {
1763 nops += 2;
1764 }
1765
1766 /* If we're fixing up mfhi/mflo for the r7000 and the
1767 2nd previous insn was an mfhi/mflo and the current insn
1768 reads the register that the mfhi/mflo wrote to, then
1769 insert one nop. */
1770
1771 else if (mips_7000_hilo_fix
1772 && MF_HILO_INSN (prev_prev_insn.insn_opcode)
1773 && insn_uses_reg (ip, ((prev_prev_insn.insn_opcode >> OP_SH_RD)
1774 & OP_MASK_RD),
1775 MIPS_GR_REG))
bdaaa2e1 1776
6b76fefe 1777 {
f9419b05 1778 ++nops;
6b76fefe 1779 }
bdaaa2e1 1780
252b5132
RH
1781 else if (prev_pinfo & INSN_READ_LO)
1782 {
1783 /* The previous instruction reads the LO register; if the
1784 current instruction writes to the LO register, we must
bdaaa2e1 1785 insert two NOPS. Some newer processors have interlocks.
67c1ffbe
KH
1786 Also the tx39's multiply instructions can be executed
1787 immediately after a read from HI/LO (without the delay),
bdaaa2e1
KH
1788 though the tx39's divide insns still do require the
1789 delay. */
252b5132 1790 if (! (hilo_interlocks
048cdf86 1791 || (mips_opts.arch == CPU_R3900 && (pinfo & INSN_MULT)))
252b5132
RH
1792 && (mips_optimize == 0
1793 || (pinfo & INSN_WRITE_LO)))
1794 nops += 2;
1795 /* Most mips16 branch insns don't have a delay slot.
1796 If a read from LO is immediately followed by a branch
1797 to a write to LO we have a read followed by a write
1798 less than 2 insns away. We assume the target of
1799 a branch might be a write to LO, and insert a nop
bdaaa2e1 1800 between a read and an immediately following branch. */
252b5132
RH
1801 else if (mips_opts.mips16
1802 && (mips_optimize == 0
1803 || (pinfo & MIPS16_INSN_BRANCH)))
f9419b05 1804 ++nops;
252b5132
RH
1805 }
1806 else if (prev_insn.insn_mo->pinfo & INSN_READ_HI)
1807 {
1808 /* The previous instruction reads the HI register; if the
1809 current instruction writes to the HI register, we must
1810 insert a NOP. Some newer processors have interlocks.
bdaaa2e1 1811 Also the note tx39's multiply above. */
252b5132 1812 if (! (hilo_interlocks
048cdf86 1813 || (mips_opts.arch == CPU_R3900 && (pinfo & INSN_MULT)))
252b5132
RH
1814 && (mips_optimize == 0
1815 || (pinfo & INSN_WRITE_HI)))
1816 nops += 2;
1817 /* Most mips16 branch insns don't have a delay slot.
1818 If a read from HI is immediately followed by a branch
1819 to a write to HI we have a read followed by a write
1820 less than 2 insns away. We assume the target of
1821 a branch might be a write to HI, and insert a nop
bdaaa2e1 1822 between a read and an immediately following branch. */
252b5132
RH
1823 else if (mips_opts.mips16
1824 && (mips_optimize == 0
1825 || (pinfo & MIPS16_INSN_BRANCH)))
f9419b05 1826 ++nops;
252b5132
RH
1827 }
1828
1829 /* If the previous instruction was in a noreorder section, then
1830 we don't want to insert the nop after all. */
bdaaa2e1 1831 /* Itbl support may require additional care here. */
252b5132
RH
1832 if (prev_insn_unreordered)
1833 nops = 0;
1834
1835 /* There are two cases which require two intervening
1836 instructions: 1) setting the condition codes using a move to
1837 coprocessor instruction which requires a general coprocessor
1838 delay and then reading the condition codes 2) reading the HI
1839 or LO register and then writing to it (except on processors
1840 which have interlocks). If we are not already emitting a NOP
1841 instruction, we must check for these cases compared to the
1842 instruction previous to the previous instruction. */
1843 if ((! mips_opts.mips16
252b5132
RH
1844 && (prev_prev_insn.insn_mo->pinfo & INSN_COPROC_MOVE_DELAY)
1845 && (prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
1846 && (pinfo & INSN_READ_COND_CODE)
1847 && ! cop_interlocks)
1848 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_LO)
1849 && (pinfo & INSN_WRITE_LO)
1850 && ! (hilo_interlocks
048cdf86 1851 || (mips_opts.arch == CPU_R3900 && (pinfo & INSN_MULT))))
252b5132
RH
1852 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
1853 && (pinfo & INSN_WRITE_HI)
1854 && ! (hilo_interlocks
048cdf86 1855 || (mips_opts.arch == CPU_R3900 && (pinfo & INSN_MULT)))))
252b5132
RH
1856 prev_prev_nop = 1;
1857 else
1858 prev_prev_nop = 0;
1859
1860 if (prev_prev_insn_unreordered)
1861 prev_prev_nop = 0;
1862
1863 if (prev_prev_nop && nops == 0)
1864 ++nops;
1865
60b63b72
RS
1866 if (mips_fix_4122_bugs && prev_insn.insn_mo->name)
1867 {
1868 /* We're out of bits in pinfo, so we must resort to string
1869 ops here. Shortcuts are selected based on opcodes being
1870 limited to the VR4122 instruction set. */
1871 int min_nops = 0;
1872 const char *pn = prev_insn.insn_mo->name;
1873 const char *tn = ip->insn_mo->name;
1874 if (strncmp(pn, "macc", 4) == 0
1875 || strncmp(pn, "dmacc", 5) == 0)
1876 {
1877 /* Errata 21 - [D]DIV[U] after [D]MACC */
1878 if (strstr (tn, "div"))
1879 {
1880 min_nops = 1;
1881 }
1882
1883 /* Errata 23 - Continuous DMULT[U]/DMACC instructions */
1884 if (pn[0] == 'd' /* dmacc */
1885 && (strncmp(tn, "dmult", 5) == 0
1886 || strncmp(tn, "dmacc", 5) == 0))
1887 {
1888 min_nops = 1;
1889 }
1890
1891 /* Errata 24 - MT{LO,HI} after [D]MACC */
1892 if (strcmp (tn, "mtlo") == 0
1893 || strcmp (tn, "mthi") == 0)
1894 {
1895 min_nops = 1;
1896 }
1897
1898 }
1899 else if (strncmp(pn, "dmult", 5) == 0
1900 && (strncmp(tn, "dmult", 5) == 0
1901 || strncmp(tn, "dmacc", 5) == 0))
1902 {
1903 /* Here is the rest of errata 23. */
1904 min_nops = 1;
1905 }
1906 if (nops < min_nops)
1907 nops = min_nops;
1908 }
1909
252b5132
RH
1910 /* If we are being given a nop instruction, don't bother with
1911 one of the nops we would otherwise output. This will only
1912 happen when a nop instruction is used with mips_optimize set
1913 to 0. */
1914 if (nops > 0
1915 && ! mips_opts.noreorder
156c2f8b 1916 && ip->insn_opcode == (unsigned) (mips_opts.mips16 ? 0x6500 : 0))
252b5132
RH
1917 --nops;
1918
1919 /* Now emit the right number of NOP instructions. */
1920 if (nops > 0 && ! mips_opts.noreorder)
1921 {
1922 fragS *old_frag;
1923 unsigned long old_frag_offset;
1924 int i;
1925 struct insn_label_list *l;
1926
1927 old_frag = frag_now;
1928 old_frag_offset = frag_now_fix ();
1929
1930 for (i = 0; i < nops; i++)
1931 emit_nop ();
1932
1933 if (listing)
1934 {
1935 listing_prev_line ();
1936 /* We may be at the start of a variant frag. In case we
1937 are, make sure there is enough space for the frag
1938 after the frags created by listing_prev_line. The
1939 argument to frag_grow here must be at least as large
1940 as the argument to all other calls to frag_grow in
1941 this file. We don't have to worry about being in the
1942 middle of a variant frag, because the variants insert
1943 all needed nop instructions themselves. */
1944 frag_grow (40);
1945 }
1946
1947 for (l = insn_labels; l != NULL; l = l->next)
1948 {
98aa84af
AM
1949 valueT val;
1950
252b5132 1951 assert (S_GET_SEGMENT (l->label) == now_seg);
49309057 1952 symbol_set_frag (l->label, frag_now);
98aa84af 1953 val = (valueT) frag_now_fix ();
252b5132
RH
1954 /* mips16 text labels are stored as odd. */
1955 if (mips_opts.mips16)
f9419b05 1956 ++val;
98aa84af 1957 S_SET_VALUE (l->label, val);
252b5132
RH
1958 }
1959
1960#ifndef NO_ECOFF_DEBUGGING
1961 if (ECOFF_DEBUGGING)
1962 ecoff_fix_loc (old_frag, old_frag_offset);
1963#endif
1964 }
1965 else if (prev_nop_frag != NULL)
1966 {
1967 /* We have a frag holding nops we may be able to remove. If
1968 we don't need any nops, we can decrease the size of
1969 prev_nop_frag by the size of one instruction. If we do
bdaaa2e1 1970 need some nops, we count them in prev_nops_required. */
252b5132
RH
1971 if (prev_nop_frag_since == 0)
1972 {
1973 if (nops == 0)
1974 {
1975 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1976 --prev_nop_frag_holds;
1977 }
1978 else
1979 prev_nop_frag_required += nops;
1980 }
1981 else
1982 {
1983 if (prev_prev_nop == 0)
1984 {
1985 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1986 --prev_nop_frag_holds;
1987 }
1988 else
1989 ++prev_nop_frag_required;
1990 }
1991
1992 if (prev_nop_frag_holds <= prev_nop_frag_required)
1993 prev_nop_frag = NULL;
1994
1995 ++prev_nop_frag_since;
1996
1997 /* Sanity check: by the time we reach the second instruction
1998 after prev_nop_frag, we should have used up all the nops
1999 one way or another. */
2000 assert (prev_nop_frag_since <= 1 || prev_nop_frag == NULL);
2001 }
2002 }
2003
895921c9
MR
2004 /* Record the frag type before frag_var. */
2005 if (prev_insn_frag)
2006 prev_insn_frag_type = prev_insn_frag->fr_type;
2007
4d7206a2 2008 if (address_expr
0b25d3e6 2009 && *reloc_type == BFD_RELOC_16_PCREL_S2
4a6a3df4
AO
2010 && (pinfo & INSN_UNCOND_BRANCH_DELAY || pinfo & INSN_COND_BRANCH_DELAY
2011 || pinfo & INSN_COND_BRANCH_LIKELY)
2012 && mips_relax_branch
2013 /* Don't try branch relaxation within .set nomacro, or within
2014 .set noat if we use $at for PIC computations. If it turns
2015 out that the branch was out-of-range, we'll get an error. */
2016 && !mips_opts.warn_about_macros
2017 && !(mips_opts.noat && mips_pic != NO_PIC)
2018 && !mips_opts.mips16)
2019 {
895921c9 2020 relaxed_branch = TRUE;
4a6a3df4
AO
2021 f = frag_var (rs_machine_dependent,
2022 relaxed_branch_length
2023 (NULL, NULL,
2024 (pinfo & INSN_UNCOND_BRANCH_DELAY) ? -1
2025 : (pinfo & INSN_COND_BRANCH_LIKELY) ? 1 : 0), 4,
2026 RELAX_BRANCH_ENCODE
af6ae2ad 2027 (pinfo & INSN_UNCOND_BRANCH_DELAY,
4a6a3df4
AO
2028 pinfo & INSN_COND_BRANCH_LIKELY,
2029 pinfo & INSN_WRITE_GPR_31,
2030 0),
2031 address_expr->X_add_symbol,
2032 address_expr->X_add_number,
2033 0);
2034 *reloc_type = BFD_RELOC_UNUSED;
2035 }
2036 else if (*reloc_type > BFD_RELOC_UNUSED)
252b5132
RH
2037 {
2038 /* We need to set up a variant frag. */
2039 assert (mips_opts.mips16 && address_expr != NULL);
2040 f = frag_var (rs_machine_dependent, 4, 0,
f6688943 2041 RELAX_MIPS16_ENCODE (*reloc_type - BFD_RELOC_UNUSED,
252b5132
RH
2042 mips16_small, mips16_ext,
2043 (prev_pinfo
2044 & INSN_UNCOND_BRANCH_DELAY),
f6688943 2045 (*prev_insn_reloc_type
252b5132 2046 == BFD_RELOC_MIPS16_JMP)),
c4e7957c 2047 make_expr_symbol (address_expr), 0, NULL);
252b5132 2048 }
252b5132
RH
2049 else if (mips_opts.mips16
2050 && ! ip->use_extend
f6688943 2051 && *reloc_type != BFD_RELOC_MIPS16_JMP)
252b5132
RH
2052 {
2053 /* Make sure there is enough room to swap this instruction with
2054 a following jump instruction. */
2055 frag_grow (6);
2056 f = frag_more (2);
2057 }
2058 else
2059 {
2060 if (mips_opts.mips16
2061 && mips_opts.noreorder
2062 && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
2063 as_warn (_("extended instruction in delay slot"));
2064
4d7206a2
RS
2065 if (mips_relax.sequence)
2066 {
2067 /* If we've reached the end of this frag, turn it into a variant
2068 frag and record the information for the instructions we've
2069 written so far. */
2070 if (frag_room () < 4)
2071 relax_close_frag ();
2072 mips_relax.sizes[mips_relax.sequence - 1] += 4;
2073 }
2074
584892a6
RS
2075 if (mips_relax.sequence != 2)
2076 mips_macro_warning.sizes[0] += 4;
2077 if (mips_relax.sequence != 1)
2078 mips_macro_warning.sizes[1] += 4;
2079
252b5132
RH
2080 f = frag_more (4);
2081 }
2082
f6688943
TS
2083 fixp[0] = fixp[1] = fixp[2] = NULL;
2084 if (address_expr != NULL && *reloc_type < BFD_RELOC_UNUSED)
252b5132
RH
2085 {
2086 if (address_expr->X_op == O_constant)
2087 {
4db1a35d 2088 valueT tmp;
f6688943
TS
2089
2090 switch (*reloc_type)
252b5132
RH
2091 {
2092 case BFD_RELOC_32:
2093 ip->insn_opcode |= address_expr->X_add_number;
2094 break;
2095
f6688943 2096 case BFD_RELOC_MIPS_HIGHEST:
6344efa4
AM
2097 tmp = (address_expr->X_add_number
2098 + ((valueT) 0x8000 << 32) + 0x80008000) >> 16;
f6688943
TS
2099 tmp >>= 16;
2100 ip->insn_opcode |= (tmp >> 16) & 0xffff;
2101 break;
2102
2103 case BFD_RELOC_MIPS_HIGHER:
2104 tmp = (address_expr->X_add_number + 0x80008000) >> 16;
2105 ip->insn_opcode |= (tmp >> 16) & 0xffff;
2106 break;
2107
2108 case BFD_RELOC_HI16_S:
2109 ip->insn_opcode |= ((address_expr->X_add_number + 0x8000)
2110 >> 16) & 0xffff;
2111 break;
2112
2113 case BFD_RELOC_HI16:
2114 ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
2115 break;
2116
252b5132 2117 case BFD_RELOC_LO16:
ed6fb7bd 2118 case BFD_RELOC_MIPS_GOT_DISP:
252b5132
RH
2119 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2120 break;
2121
2122 case BFD_RELOC_MIPS_JMP:
2123 if ((address_expr->X_add_number & 3) != 0)
2124 as_bad (_("jump to misaligned address (0x%lx)"),
2125 (unsigned long) address_expr->X_add_number);
f3c0ec86 2126 if (address_expr->X_add_number & ~0xfffffff)
7496292d
TS
2127 as_bad (_("jump address range overflow (0x%lx)"),
2128 (unsigned long) address_expr->X_add_number);
252b5132
RH
2129 ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
2130 break;
2131
2132 case BFD_RELOC_MIPS16_JMP:
2133 if ((address_expr->X_add_number & 3) != 0)
2134 as_bad (_("jump to misaligned address (0x%lx)"),
2135 (unsigned long) address_expr->X_add_number);
f3c0ec86 2136 if (address_expr->X_add_number & ~0xfffffff)
7496292d
TS
2137 as_bad (_("jump address range overflow (0x%lx)"),
2138 (unsigned long) address_expr->X_add_number);
252b5132
RH
2139 ip->insn_opcode |=
2140 (((address_expr->X_add_number & 0x7c0000) << 3)
2141 | ((address_expr->X_add_number & 0xf800000) >> 7)
2142 | ((address_expr->X_add_number & 0x3fffc) >> 2));
2143 break;
2144
252b5132
RH
2145 case BFD_RELOC_16_PCREL_S2:
2146 goto need_reloc;
2147
2148 default:
2149 internalError ();
2150 }
2151 }
2152 else
252b5132 2153 need_reloc:
4d7206a2
RS
2154 {
2155 reloc_howto_type *howto;
2156 int i;
34ce925e 2157
4d7206a2
RS
2158 /* In a compound relocation, it is the final (outermost)
2159 operator that determines the relocated field. */
2160 for (i = 1; i < 3; i++)
2161 if (reloc_type[i] == BFD_RELOC_UNUSED)
2162 break;
34ce925e 2163
4d7206a2
RS
2164 howto = bfd_reloc_type_lookup (stdoutput, reloc_type[i - 1]);
2165 fixp[0] = fix_new_exp (frag_now, f - frag_now->fr_literal,
2166 bfd_get_reloc_size(howto),
2167 address_expr,
2168 reloc_type[0] == BFD_RELOC_16_PCREL_S2,
2169 reloc_type[0]);
2170
2171 /* These relocations can have an addend that won't fit in
2172 4 octets for 64bit assembly. */
2173 if (HAVE_64BIT_GPRS
2174 && ! howto->partial_inplace
2175 && (reloc_type[0] == BFD_RELOC_16
2176 || reloc_type[0] == BFD_RELOC_32
2177 || reloc_type[0] == BFD_RELOC_MIPS_JMP
2178 || reloc_type[0] == BFD_RELOC_HI16_S
2179 || reloc_type[0] == BFD_RELOC_LO16
2180 || reloc_type[0] == BFD_RELOC_GPREL16
2181 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
2182 || reloc_type[0] == BFD_RELOC_GPREL32
2183 || reloc_type[0] == BFD_RELOC_64
2184 || reloc_type[0] == BFD_RELOC_CTOR
2185 || reloc_type[0] == BFD_RELOC_MIPS_SUB
2186 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
2187 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
2188 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
2189 || reloc_type[0] == BFD_RELOC_MIPS_REL16
2190 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT))
2191 fixp[0]->fx_no_overflow = 1;
2192
2193 if (mips_relax.sequence)
2194 {
2195 if (mips_relax.first_fixup == 0)
2196 mips_relax.first_fixup = fixp[0];
2197 }
2198 else if (reloc_needs_lo_p (*reloc_type))
2199 {
2200 struct mips_hi_fixup *hi_fixup;
252b5132 2201
4d7206a2
RS
2202 /* Reuse the last entry if it already has a matching %lo. */
2203 hi_fixup = mips_hi_fixup_list;
2204 if (hi_fixup == 0
2205 || !fixup_has_matching_lo_p (hi_fixup->fixp))
2206 {
2207 hi_fixup = ((struct mips_hi_fixup *)
2208 xmalloc (sizeof (struct mips_hi_fixup)));
2209 hi_fixup->next = mips_hi_fixup_list;
2210 mips_hi_fixup_list = hi_fixup;
252b5132 2211 }
4d7206a2
RS
2212 hi_fixup->fixp = fixp[0];
2213 hi_fixup->seg = now_seg;
2214 }
f6688943 2215
4d7206a2
RS
2216 /* Add fixups for the second and third relocations, if given.
2217 Note that the ABI allows the second relocation to be
2218 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the
2219 moment we only use RSS_UNDEF, but we could add support
2220 for the others if it ever becomes necessary. */
2221 for (i = 1; i < 3; i++)
2222 if (reloc_type[i] != BFD_RELOC_UNUSED)
2223 {
2224 address_expr->X_op = O_absent;
2225 address_expr->X_add_symbol = 0;
2226 address_expr->X_add_number = 0;
34ce925e 2227
4d7206a2
RS
2228 fixp[i] = fix_new_exp (frag_now, fixp[0]->fx_where,
2229 fixp[0]->fx_size, address_expr,
2230 FALSE, reloc_type[i]);
2231 }
252b5132
RH
2232 }
2233 }
2234
2235 if (! mips_opts.mips16)
c5dd6aab
DJ
2236 {
2237 md_number_to_chars (f, ip->insn_opcode, 4);
2238#ifdef OBJ_ELF
2239 dwarf2_emit_insn (4);
2240#endif
2241 }
f6688943 2242 else if (*reloc_type == BFD_RELOC_MIPS16_JMP)
252b5132
RH
2243 {
2244 md_number_to_chars (f, ip->insn_opcode >> 16, 2);
2245 md_number_to_chars (f + 2, ip->insn_opcode & 0xffff, 2);
c5dd6aab
DJ
2246#ifdef OBJ_ELF
2247 dwarf2_emit_insn (4);
2248#endif
252b5132
RH
2249 }
2250 else
2251 {
2252 if (ip->use_extend)
2253 {
2254 md_number_to_chars (f, 0xf000 | ip->extend, 2);
2255 f += 2;
2256 }
2257 md_number_to_chars (f, ip->insn_opcode, 2);
c5dd6aab
DJ
2258#ifdef OBJ_ELF
2259 dwarf2_emit_insn (ip->use_extend ? 4 : 2);
2260#endif
252b5132
RH
2261 }
2262
2263 /* Update the register mask information. */
2264 if (! mips_opts.mips16)
2265 {
2266 if (pinfo & INSN_WRITE_GPR_D)
2267 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
2268 if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2269 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
2270 if (pinfo & INSN_READ_GPR_S)
2271 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
2272 if (pinfo & INSN_WRITE_GPR_31)
f9419b05 2273 mips_gprmask |= 1 << RA;
252b5132
RH
2274 if (pinfo & INSN_WRITE_FPR_D)
2275 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
2276 if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2277 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
2278 if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2279 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
2280 if ((pinfo & INSN_READ_FPR_R) != 0)
2281 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
2282 if (pinfo & INSN_COP)
2283 {
bdaaa2e1
KH
2284 /* We don't keep enough information to sort these cases out.
2285 The itbl support does keep this information however, although
2286 we currently don't support itbl fprmats as part of the cop
2287 instruction. May want to add this support in the future. */
252b5132
RH
2288 }
2289 /* Never set the bit for $0, which is always zero. */
beae10d5 2290 mips_gprmask &= ~1 << 0;
252b5132
RH
2291 }
2292 else
2293 {
2294 if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2295 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RX)
2296 & MIPS16OP_MASK_RX);
2297 if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2298 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RY)
2299 & MIPS16OP_MASK_RY);
2300 if (pinfo & MIPS16_INSN_WRITE_Z)
2301 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RZ)
2302 & MIPS16OP_MASK_RZ);
2303 if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2304 mips_gprmask |= 1 << TREG;
2305 if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2306 mips_gprmask |= 1 << SP;
2307 if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2308 mips_gprmask |= 1 << RA;
2309 if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2310 mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2311 if (pinfo & MIPS16_INSN_READ_Z)
2312 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
2313 & MIPS16OP_MASK_MOVE32Z);
2314 if (pinfo & MIPS16_INSN_READ_GPR_X)
2315 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
2316 & MIPS16OP_MASK_REGR32);
2317 }
2318
4d7206a2 2319 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
252b5132
RH
2320 {
2321 /* Filling the branch delay slot is more complex. We try to
2322 switch the branch with the previous instruction, which we can
2323 do if the previous instruction does not set up a condition
2324 that the branch tests and if the branch is not itself the
2325 target of any branch. */
2326 if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2327 || (pinfo & INSN_COND_BRANCH_DELAY))
2328 {
2329 if (mips_optimize < 2
2330 /* If we have seen .set volatile or .set nomove, don't
2331 optimize. */
2332 || mips_opts.nomove != 0
2333 /* If we had to emit any NOP instructions, then we
2334 already know we can not swap. */
2335 || nops != 0
2336 /* If we don't even know the previous insn, we can not
bdaaa2e1 2337 swap. */
252b5132
RH
2338 || ! prev_insn_valid
2339 /* If the previous insn is already in a branch delay
2340 slot, then we can not swap. */
2341 || prev_insn_is_delay_slot
2342 /* If the previous previous insn was in a .set
2343 noreorder, we can't swap. Actually, the MIPS
2344 assembler will swap in this situation. However, gcc
2345 configured -with-gnu-as will generate code like
2346 .set noreorder
2347 lw $4,XXX
2348 .set reorder
2349 INSN
2350 bne $4,$0,foo
2351 in which we can not swap the bne and INSN. If gcc is
2352 not configured -with-gnu-as, it does not output the
2353 .set pseudo-ops. We don't have to check
2354 prev_insn_unreordered, because prev_insn_valid will
2355 be 0 in that case. We don't want to use
2356 prev_prev_insn_valid, because we do want to be able
2357 to swap at the start of a function. */
2358 || prev_prev_insn_unreordered
2359 /* If the branch is itself the target of a branch, we
2360 can not swap. We cheat on this; all we check for is
2361 whether there is a label on this instruction. If
2362 there are any branches to anything other than a
2363 label, users must use .set noreorder. */
2364 || insn_labels != NULL
895921c9
MR
2365 /* If the previous instruction is in a variant frag
2366 other than this branch's one, we cannot do the swap.
2367 This does not apply to the mips16, which uses variant
2368 frags for different purposes. */
252b5132 2369 || (! mips_opts.mips16
895921c9 2370 && prev_insn_frag_type == rs_machine_dependent)
252b5132
RH
2371 /* If the branch reads the condition codes, we don't
2372 even try to swap, because in the sequence
2373 ctc1 $X,$31
2374 INSN
2375 INSN
2376 bc1t LABEL
2377 we can not swap, and I don't feel like handling that
2378 case. */
2379 || (! mips_opts.mips16
81912461
ILT
2380 && (pinfo & INSN_READ_COND_CODE)
2381 && ! cop_interlocks)
252b5132 2382 /* We can not swap with an instruction that requires a
67c1ffbe 2383 delay slot, because the target of the branch might
252b5132
RH
2384 interfere with that instruction. */
2385 || (! mips_opts.mips16
252b5132 2386 && (prev_pinfo
bdaaa2e1 2387 /* Itbl support may require additional care here. */
252b5132
RH
2388 & (INSN_LOAD_COPROC_DELAY
2389 | INSN_COPROC_MOVE_DELAY
81912461
ILT
2390 | INSN_WRITE_COND_CODE))
2391 && ! cop_interlocks)
252b5132 2392 || (! (hilo_interlocks
048cdf86 2393 || (mips_opts.arch == CPU_R3900 && (pinfo & INSN_MULT)))
252b5132
RH
2394 && (prev_pinfo
2395 & (INSN_READ_LO
2396 | INSN_READ_HI)))
2397 || (! mips_opts.mips16
81912461
ILT
2398 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY)
2399 && ! gpr_interlocks)
252b5132 2400 || (! mips_opts.mips16
bdaaa2e1 2401 /* Itbl support may require additional care here. */
81912461
ILT
2402 && (prev_pinfo & INSN_COPROC_MEMORY_DELAY)
2403 && ! cop_mem_interlocks)
252b5132
RH
2404 /* We can not swap with a branch instruction. */
2405 || (prev_pinfo
2406 & (INSN_UNCOND_BRANCH_DELAY
2407 | INSN_COND_BRANCH_DELAY
2408 | INSN_COND_BRANCH_LIKELY))
2409 /* We do not swap with a trap instruction, since it
2410 complicates trap handlers to have the trap
2411 instruction be in a delay slot. */
2412 || (prev_pinfo & INSN_TRAP)
2413 /* If the branch reads a register that the previous
2414 instruction sets, we can not swap. */
2415 || (! mips_opts.mips16
2416 && (prev_pinfo & INSN_WRITE_GPR_T)
2417 && insn_uses_reg (ip,
2418 ((prev_insn.insn_opcode >> OP_SH_RT)
2419 & OP_MASK_RT),
2420 MIPS_GR_REG))
2421 || (! mips_opts.mips16
2422 && (prev_pinfo & INSN_WRITE_GPR_D)
2423 && insn_uses_reg (ip,
2424 ((prev_insn.insn_opcode >> OP_SH_RD)
2425 & OP_MASK_RD),
2426 MIPS_GR_REG))
2427 || (mips_opts.mips16
2428 && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2429 && insn_uses_reg (ip,
2430 ((prev_insn.insn_opcode
2431 >> MIPS16OP_SH_RX)
2432 & MIPS16OP_MASK_RX),
2433 MIPS16_REG))
2434 || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2435 && insn_uses_reg (ip,
2436 ((prev_insn.insn_opcode
2437 >> MIPS16OP_SH_RY)
2438 & MIPS16OP_MASK_RY),
2439 MIPS16_REG))
2440 || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2441 && insn_uses_reg (ip,
2442 ((prev_insn.insn_opcode
2443 >> MIPS16OP_SH_RZ)
2444 & MIPS16OP_MASK_RZ),
2445 MIPS16_REG))
2446 || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2447 && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2448 || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2449 && insn_uses_reg (ip, RA, MIPS_GR_REG))
2450 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2451 && insn_uses_reg (ip,
2452 MIPS16OP_EXTRACT_REG32R (prev_insn.
2453 insn_opcode),
2454 MIPS_GR_REG))))
2455 /* If the branch writes a register that the previous
2456 instruction sets, we can not swap (we know that
2457 branches write only to RD or to $31). */
2458 || (! mips_opts.mips16
2459 && (prev_pinfo & INSN_WRITE_GPR_T)
2460 && (((pinfo & INSN_WRITE_GPR_D)
2461 && (((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT)
2462 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2463 || ((pinfo & INSN_WRITE_GPR_31)
2464 && (((prev_insn.insn_opcode >> OP_SH_RT)
2465 & OP_MASK_RT)
f9419b05 2466 == RA))))
252b5132
RH
2467 || (! mips_opts.mips16
2468 && (prev_pinfo & INSN_WRITE_GPR_D)
2469 && (((pinfo & INSN_WRITE_GPR_D)
2470 && (((prev_insn.insn_opcode >> OP_SH_RD) & OP_MASK_RD)
2471 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2472 || ((pinfo & INSN_WRITE_GPR_31)
2473 && (((prev_insn.insn_opcode >> OP_SH_RD)
2474 & OP_MASK_RD)
f9419b05 2475 == RA))))
252b5132
RH
2476 || (mips_opts.mips16
2477 && (pinfo & MIPS16_INSN_WRITE_31)
2478 && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2479 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2480 && (MIPS16OP_EXTRACT_REG32R (prev_insn.insn_opcode)
2481 == RA))))
2482 /* If the branch writes a register that the previous
2483 instruction reads, we can not swap (we know that
2484 branches only write to RD or to $31). */
2485 || (! mips_opts.mips16
2486 && (pinfo & INSN_WRITE_GPR_D)
2487 && insn_uses_reg (&prev_insn,
2488 ((ip->insn_opcode >> OP_SH_RD)
2489 & OP_MASK_RD),
2490 MIPS_GR_REG))
2491 || (! mips_opts.mips16
2492 && (pinfo & INSN_WRITE_GPR_31)
f9419b05 2493 && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
252b5132
RH
2494 || (mips_opts.mips16
2495 && (pinfo & MIPS16_INSN_WRITE_31)
2496 && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2497 /* If we are generating embedded PIC code, the branch
2498 might be expanded into a sequence which uses $at, so
2499 we can't swap with an instruction which reads it. */
2500 || (mips_pic == EMBEDDED_PIC
2501 && insn_uses_reg (&prev_insn, AT, MIPS_GR_REG))
2502 /* If the previous previous instruction has a load
2503 delay, and sets a register that the branch reads, we
2504 can not swap. */
2505 || (! mips_opts.mips16
bdaaa2e1 2506 /* Itbl support may require additional care here. */
81912461
ILT
2507 && (((prev_prev_insn.insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
2508 && ! cop_interlocks)
2509 || ((prev_prev_insn.insn_mo->pinfo
2510 & INSN_LOAD_MEMORY_DELAY)
2511 && ! gpr_interlocks))
252b5132
RH
2512 && insn_uses_reg (ip,
2513 ((prev_prev_insn.insn_opcode >> OP_SH_RT)
2514 & OP_MASK_RT),
2515 MIPS_GR_REG))
2516 /* If one instruction sets a condition code and the
2517 other one uses a condition code, we can not swap. */
2518 || ((pinfo & INSN_READ_COND_CODE)
2519 && (prev_pinfo & INSN_WRITE_COND_CODE))
2520 || ((pinfo & INSN_WRITE_COND_CODE)
2521 && (prev_pinfo & INSN_READ_COND_CODE))
2522 /* If the previous instruction uses the PC, we can not
2523 swap. */
2524 || (mips_opts.mips16
2525 && (prev_pinfo & MIPS16_INSN_READ_PC))
2526 /* If the previous instruction was extended, we can not
2527 swap. */
2528 || (mips_opts.mips16 && prev_insn_extended)
2529 /* If the previous instruction had a fixup in mips16
2530 mode, we can not swap. This normally means that the
2531 previous instruction was a 4 byte branch anyhow. */
f6688943 2532 || (mips_opts.mips16 && prev_insn_fixp[0])
bdaaa2e1
KH
2533 /* If the previous instruction is a sync, sync.l, or
2534 sync.p, we can not swap. */
f173e82e 2535 || (prev_pinfo & INSN_SYNC))
252b5132
RH
2536 {
2537 /* We could do even better for unconditional branches to
2538 portions of this object file; we could pick up the
2539 instruction at the destination, put it in the delay
2540 slot, and bump the destination address. */
2541 emit_nop ();
2542 /* Update the previous insn information. */
2543 prev_prev_insn = *ip;
2544 prev_insn.insn_mo = &dummy_opcode;
2545 }
2546 else
2547 {
2548 /* It looks like we can actually do the swap. */
2549 if (! mips_opts.mips16)
2550 {
2551 char *prev_f;
2552 char temp[4];
2553
2554 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
895921c9
MR
2555 if (!relaxed_branch)
2556 {
2557 /* If this is not a relaxed branch, then just
2558 swap the instructions. */
2559 memcpy (temp, prev_f, 4);
2560 memcpy (prev_f, f, 4);
2561 memcpy (f, temp, 4);
2562 }
2563 else
2564 {
2565 /* If this is a relaxed branch, then we move the
2566 instruction to be placed in the delay slot to
2567 the current frag, shrinking the fixed part of
2568 the originating frag. If the branch occupies
2569 the tail of the latter, we move it backwards,
2570 into the space freed by the moved instruction. */
2571 f = frag_more (4);
2572 memcpy (f, prev_f, 4);
2573 prev_insn_frag->fr_fix -= 4;
2574 if (prev_insn_frag->fr_type == rs_machine_dependent)
2575 memmove (prev_f, prev_f + 4, prev_insn_frag->fr_var);
2576 }
2577
f6688943
TS
2578 if (prev_insn_fixp[0])
2579 {
2580 prev_insn_fixp[0]->fx_frag = frag_now;
2581 prev_insn_fixp[0]->fx_where = f - frag_now->fr_literal;
2582 }
2583 if (prev_insn_fixp[1])
2584 {
2585 prev_insn_fixp[1]->fx_frag = frag_now;
2586 prev_insn_fixp[1]->fx_where = f - frag_now->fr_literal;
2587 }
2588 if (prev_insn_fixp[2])
252b5132 2589 {
f6688943
TS
2590 prev_insn_fixp[2]->fx_frag = frag_now;
2591 prev_insn_fixp[2]->fx_where = f - frag_now->fr_literal;
252b5132 2592 }
f5040a92
AO
2593 if (prev_insn_fixp[0] && HAVE_NEWABI
2594 && prev_insn_frag != frag_now
2595 && (prev_insn_fixp[0]->fx_r_type
2596 == BFD_RELOC_MIPS_GOT_DISP
2597 || (prev_insn_fixp[0]->fx_r_type
2598 == BFD_RELOC_MIPS_CALL16)))
2599 {
2600 /* To avoid confusion in tc_gen_reloc, we must
2601 ensure that this does not become a variant
2602 frag. */
2603 force_new_frag = TRUE;
2604 }
895921c9
MR
2605
2606 if (!relaxed_branch)
f6688943 2607 {
895921c9
MR
2608 if (fixp[0])
2609 {
2610 fixp[0]->fx_frag = prev_insn_frag;
2611 fixp[0]->fx_where = prev_insn_where;
2612 }
2613 if (fixp[1])
2614 {
2615 fixp[1]->fx_frag = prev_insn_frag;
2616 fixp[1]->fx_where = prev_insn_where;
2617 }
2618 if (fixp[2])
2619 {
2620 fixp[2]->fx_frag = prev_insn_frag;
2621 fixp[2]->fx_where = prev_insn_where;
2622 }
f6688943 2623 }
895921c9 2624 else if (prev_insn_frag->fr_type == rs_machine_dependent)
f6688943 2625 {
895921c9
MR
2626 if (fixp[0])
2627 fixp[0]->fx_where -= 4;
2628 if (fixp[1])
2629 fixp[1]->fx_where -= 4;
2630 if (fixp[2])
2631 fixp[2]->fx_where -= 4;
252b5132
RH
2632 }
2633 }
2634 else
2635 {
2636 char *prev_f;
2637 char temp[2];
2638
f6688943
TS
2639 assert (prev_insn_fixp[0] == NULL);
2640 assert (prev_insn_fixp[1] == NULL);
2641 assert (prev_insn_fixp[2] == NULL);
252b5132
RH
2642 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2643 memcpy (temp, prev_f, 2);
2644 memcpy (prev_f, f, 2);
f6688943 2645 if (*reloc_type != BFD_RELOC_MIPS16_JMP)
252b5132 2646 {
f6688943 2647 assert (*reloc_type == BFD_RELOC_UNUSED);
252b5132
RH
2648 memcpy (f, temp, 2);
2649 }
2650 else
2651 {
2652 memcpy (f, f + 2, 2);
2653 memcpy (f + 2, temp, 2);
2654 }
f6688943
TS
2655 if (fixp[0])
2656 {
2657 fixp[0]->fx_frag = prev_insn_frag;
2658 fixp[0]->fx_where = prev_insn_where;
2659 }
2660 if (fixp[1])
2661 {
2662 fixp[1]->fx_frag = prev_insn_frag;
2663 fixp[1]->fx_where = prev_insn_where;
2664 }
2665 if (fixp[2])
252b5132 2666 {
f6688943
TS
2667 fixp[2]->fx_frag = prev_insn_frag;
2668 fixp[2]->fx_where = prev_insn_where;
252b5132
RH
2669 }
2670 }
2671
2672 /* Update the previous insn information; leave prev_insn
2673 unchanged. */
2674 prev_prev_insn = *ip;
2675 }
2676 prev_insn_is_delay_slot = 1;
2677
2678 /* If that was an unconditional branch, forget the previous
2679 insn information. */
2680 if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2681 {
2682 prev_prev_insn.insn_mo = &dummy_opcode;
2683 prev_insn.insn_mo = &dummy_opcode;
2684 }
2685
f6688943
TS
2686 prev_insn_fixp[0] = NULL;
2687 prev_insn_fixp[1] = NULL;
2688 prev_insn_fixp[2] = NULL;
2689 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2690 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2691 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
252b5132
RH
2692 prev_insn_extended = 0;
2693 }
2694 else if (pinfo & INSN_COND_BRANCH_LIKELY)
2695 {
2696 /* We don't yet optimize a branch likely. What we should do
2697 is look at the target, copy the instruction found there
2698 into the delay slot, and increment the branch to jump to
2699 the next instruction. */
2700 emit_nop ();
2701 /* Update the previous insn information. */
2702 prev_prev_insn = *ip;
2703 prev_insn.insn_mo = &dummy_opcode;
f6688943
TS
2704 prev_insn_fixp[0] = NULL;
2705 prev_insn_fixp[1] = NULL;
2706 prev_insn_fixp[2] = NULL;
2707 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2708 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2709 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
252b5132
RH
2710 prev_insn_extended = 0;
2711 }
2712 else
2713 {
2714 /* Update the previous insn information. */
2715 if (nops > 0)
2716 prev_prev_insn.insn_mo = &dummy_opcode;
2717 else
2718 prev_prev_insn = prev_insn;
2719 prev_insn = *ip;
2720
2721 /* Any time we see a branch, we always fill the delay slot
2722 immediately; since this insn is not a branch, we know it
2723 is not in a delay slot. */
2724 prev_insn_is_delay_slot = 0;
2725
f6688943
TS
2726 prev_insn_fixp[0] = fixp[0];
2727 prev_insn_fixp[1] = fixp[1];
2728 prev_insn_fixp[2] = fixp[2];
2729 prev_insn_reloc_type[0] = reloc_type[0];
2730 prev_insn_reloc_type[1] = reloc_type[1];
2731 prev_insn_reloc_type[2] = reloc_type[2];
252b5132
RH
2732 if (mips_opts.mips16)
2733 prev_insn_extended = (ip->use_extend
f6688943 2734 || *reloc_type > BFD_RELOC_UNUSED);
252b5132
RH
2735 }
2736
2737 prev_prev_insn_unreordered = prev_insn_unreordered;
2738 prev_insn_unreordered = 0;
2739 prev_insn_frag = frag_now;
2740 prev_insn_where = f - frag_now->fr_literal;
2741 prev_insn_valid = 1;
2742 }
4d7206a2 2743 else if (mips_relax.sequence != 2)
252b5132
RH
2744 {
2745 /* We need to record a bit of information even when we are not
2746 reordering, in order to determine the base address for mips16
2747 PC relative relocs. */
2748 prev_prev_insn = prev_insn;
2749 prev_insn = *ip;
f6688943
TS
2750 prev_insn_reloc_type[0] = reloc_type[0];
2751 prev_insn_reloc_type[1] = reloc_type[1];
2752 prev_insn_reloc_type[2] = reloc_type[2];
252b5132
RH
2753 prev_prev_insn_unreordered = prev_insn_unreordered;
2754 prev_insn_unreordered = 1;
2755 }
2756
2757 /* We just output an insn, so the next one doesn't have a label. */
2758 mips_clear_insn_labels ();
252b5132
RH
2759}
2760
2761/* This function forgets that there was any previous instruction or
2762 label. If PRESERVE is non-zero, it remembers enough information to
bdaaa2e1 2763 know whether nops are needed before a noreorder section. */
252b5132
RH
2764
2765static void
17a2f251 2766mips_no_prev_insn (int preserve)
252b5132
RH
2767{
2768 if (! preserve)
2769 {
2770 prev_insn.insn_mo = &dummy_opcode;
2771 prev_prev_insn.insn_mo = &dummy_opcode;
2772 prev_nop_frag = NULL;
2773 prev_nop_frag_holds = 0;
2774 prev_nop_frag_required = 0;
2775 prev_nop_frag_since = 0;
2776 }
2777 prev_insn_valid = 0;
2778 prev_insn_is_delay_slot = 0;
2779 prev_insn_unreordered = 0;
2780 prev_insn_extended = 0;
f6688943
TS
2781 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2782 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2783 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
252b5132
RH
2784 prev_prev_insn_unreordered = 0;
2785 mips_clear_insn_labels ();
2786}
2787
2788/* This function must be called whenever we turn on noreorder or emit
2789 something other than instructions. It inserts any NOPS which might
2790 be needed by the previous instruction, and clears the information
2791 kept for the previous instructions. The INSNS parameter is true if
bdaaa2e1 2792 instructions are to follow. */
252b5132
RH
2793
2794static void
17a2f251 2795mips_emit_delays (bfd_boolean insns)
252b5132
RH
2796{
2797 if (! mips_opts.noreorder)
2798 {
2799 int nops;
2800
2801 nops = 0;
2802 if ((! mips_opts.mips16
81912461
ILT
2803 && ((prev_insn.insn_mo->pinfo
2804 & (INSN_LOAD_COPROC_DELAY
2805 | INSN_COPROC_MOVE_DELAY
2806 | INSN_WRITE_COND_CODE))
2807 && ! cop_interlocks))
252b5132
RH
2808 || (! hilo_interlocks
2809 && (prev_insn.insn_mo->pinfo
2810 & (INSN_READ_LO
2811 | INSN_READ_HI)))
2812 || (! mips_opts.mips16
81912461
ILT
2813 && (prev_insn.insn_mo->pinfo & INSN_LOAD_MEMORY_DELAY)
2814 && ! gpr_interlocks)
252b5132 2815 || (! mips_opts.mips16
81912461
ILT
2816 && (prev_insn.insn_mo->pinfo & INSN_COPROC_MEMORY_DELAY)
2817 && ! cop_mem_interlocks))
252b5132 2818 {
beae10d5 2819 /* Itbl support may require additional care here. */
252b5132
RH
2820 ++nops;
2821 if ((! mips_opts.mips16
81912461
ILT
2822 && ((prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
2823 && ! cop_interlocks))
252b5132
RH
2824 || (! hilo_interlocks
2825 && ((prev_insn.insn_mo->pinfo & INSN_READ_HI)
2826 || (prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2827 ++nops;
2828
2829 if (prev_insn_unreordered)
2830 nops = 0;
2831 }
2832 else if ((! mips_opts.mips16
81912461
ILT
2833 && ((prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
2834 && ! cop_interlocks))
252b5132
RH
2835 || (! hilo_interlocks
2836 && ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
2837 || (prev_prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2838 {
beae10d5 2839 /* Itbl support may require additional care here. */
252b5132
RH
2840 if (! prev_prev_insn_unreordered)
2841 ++nops;
2842 }
2843
60b63b72
RS
2844 if (mips_fix_4122_bugs && prev_insn.insn_mo->name)
2845 {
2846 int min_nops = 0;
2847 const char *pn = prev_insn.insn_mo->name;
2848 if (strncmp(pn, "macc", 4) == 0
2849 || strncmp(pn, "dmacc", 5) == 0
2850 || strncmp(pn, "dmult", 5) == 0)
2851 {
2852 min_nops = 1;
2853 }
2854 if (nops < min_nops)
2855 nops = min_nops;
2856 }
2857
252b5132
RH
2858 if (nops > 0)
2859 {
2860 struct insn_label_list *l;
2861
2862 if (insns)
2863 {
2864 /* Record the frag which holds the nop instructions, so
2865 that we can remove them if we don't need them. */
2866 frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2867 prev_nop_frag = frag_now;
2868 prev_nop_frag_holds = nops;
2869 prev_nop_frag_required = 0;
2870 prev_nop_frag_since = 0;
2871 }
2872
2873 for (; nops > 0; --nops)
2874 emit_nop ();
2875
2876 if (insns)
2877 {
2878 /* Move on to a new frag, so that it is safe to simply
bdaaa2e1 2879 decrease the size of prev_nop_frag. */
252b5132
RH
2880 frag_wane (frag_now);
2881 frag_new (0);
2882 }
2883
2884 for (l = insn_labels; l != NULL; l = l->next)
2885 {
98aa84af
AM
2886 valueT val;
2887
252b5132 2888 assert (S_GET_SEGMENT (l->label) == now_seg);
49309057 2889 symbol_set_frag (l->label, frag_now);
98aa84af 2890 val = (valueT) frag_now_fix ();
252b5132
RH
2891 /* mips16 text labels are stored as odd. */
2892 if (mips_opts.mips16)
f9419b05 2893 ++val;
98aa84af 2894 S_SET_VALUE (l->label, val);
252b5132
RH
2895 }
2896 }
2897 }
2898
2899 /* Mark instruction labels in mips16 mode. */
f9419b05 2900 if (insns)
252b5132
RH
2901 mips16_mark_labels ();
2902
2903 mips_no_prev_insn (insns);
2904}
2905
584892a6
RS
2906/* Set up global variables for the start of a new macro. */
2907
2908static void
2909macro_start (void)
2910{
2911 memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
2912 mips_macro_warning.delay_slot_p = (mips_opts.noreorder
2913 && (prev_insn.insn_mo->pinfo
2914 & (INSN_UNCOND_BRANCH_DELAY
2915 | INSN_COND_BRANCH_DELAY
2916 | INSN_COND_BRANCH_LIKELY)) != 0);
2917}
2918
2919/* Given that a macro is longer than 4 bytes, return the appropriate warning
2920 for it. Return null if no warning is needed. SUBTYPE is a bitmask of
2921 RELAX_DELAY_SLOT and RELAX_NOMACRO. */
2922
2923static const char *
2924macro_warning (relax_substateT subtype)
2925{
2926 if (subtype & RELAX_DELAY_SLOT)
2927 return _("Macro instruction expanded into multiple instructions"
2928 " in a branch delay slot");
2929 else if (subtype & RELAX_NOMACRO)
2930 return _("Macro instruction expanded into multiple instructions");
2931 else
2932 return 0;
2933}
2934
2935/* Finish up a macro. Emit warnings as appropriate. */
2936
2937static void
2938macro_end (void)
2939{
2940 if (mips_macro_warning.sizes[0] > 4 || mips_macro_warning.sizes[1] > 4)
2941 {
2942 relax_substateT subtype;
2943
2944 /* Set up the relaxation warning flags. */
2945 subtype = 0;
2946 if (mips_macro_warning.sizes[1] > mips_macro_warning.sizes[0])
2947 subtype |= RELAX_SECOND_LONGER;
2948 if (mips_opts.warn_about_macros)
2949 subtype |= RELAX_NOMACRO;
2950 if (mips_macro_warning.delay_slot_p)
2951 subtype |= RELAX_DELAY_SLOT;
2952
2953 if (mips_macro_warning.sizes[0] > 4 && mips_macro_warning.sizes[1] > 4)
2954 {
2955 /* Either the macro has a single implementation or both
2956 implementations are longer than 4 bytes. Emit the
2957 warning now. */
2958 const char *msg = macro_warning (subtype);
2959 if (msg != 0)
2960 as_warn (msg);
2961 }
2962 else
2963 {
2964 /* One implementation might need a warning but the other
2965 definitely doesn't. */
2966 mips_macro_warning.first_frag->fr_subtype |= subtype;
2967 }
2968 }
2969}
2970
252b5132
RH
2971/* Build an instruction created by a macro expansion. This is passed
2972 a pointer to the count of instructions created so far, an
2973 expression, the name of the instruction to build, an operand format
2974 string, and corresponding arguments. */
2975
252b5132 2976static void
67c0d1eb 2977macro_build (expressionS *ep, const char *name, const char *fmt, ...)
252b5132
RH
2978{
2979 struct mips_cl_insn insn;
f6688943 2980 bfd_reloc_code_real_type r[3];
252b5132 2981 va_list args;
252b5132 2982
252b5132 2983 va_start (args, fmt);
252b5132 2984
252b5132
RH
2985 if (mips_opts.mips16)
2986 {
67c0d1eb 2987 mips16_macro_build (ep, name, fmt, args);
252b5132
RH
2988 va_end (args);
2989 return;
2990 }
2991
f6688943
TS
2992 r[0] = BFD_RELOC_UNUSED;
2993 r[1] = BFD_RELOC_UNUSED;
2994 r[2] = BFD_RELOC_UNUSED;
252b5132
RH
2995 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
2996 assert (insn.insn_mo);
2997 assert (strcmp (name, insn.insn_mo->name) == 0);
2998
2999 /* Search until we get a match for NAME. */
3000 while (1)
3001 {
b34976b6 3002 /* It is assumed here that macros will never generate
deec1734 3003 MDMX or MIPS-3D instructions. */
252b5132
RH
3004 if (strcmp (fmt, insn.insn_mo->args) == 0
3005 && insn.insn_mo->pinfo != INSN_MACRO
aec421e0
TS
3006 && OPCODE_IS_MEMBER (insn.insn_mo,
3007 (mips_opts.isa
3396de36 3008 | (file_ase_mips16 ? INSN_MIPS16 : 0)),
fef14a42
TS
3009 mips_opts.arch)
3010 && (mips_opts.arch != CPU_R4650 || (insn.insn_mo->pinfo & FP_D) == 0))
252b5132
RH
3011 break;
3012
3013 ++insn.insn_mo;
3014 assert (insn.insn_mo->name);
3015 assert (strcmp (name, insn.insn_mo->name) == 0);
3016 }
3017
3018 insn.insn_opcode = insn.insn_mo->match;
3019 for (;;)
3020 {
3021 switch (*fmt++)
3022 {
3023 case '\0':
3024 break;
3025
3026 case ',':
3027 case '(':
3028 case ')':
3029 continue;
3030
5f74bc13
CD
3031 case '+':
3032 switch (*fmt++)
3033 {
3034 case 'A':
3035 case 'E':
3036 insn.insn_opcode |= (va_arg (args, int)
3037 & OP_MASK_SHAMT) << OP_SH_SHAMT;
3038 continue;
3039
3040 case 'B':
3041 case 'F':
3042 /* Note that in the macro case, these arguments are already
3043 in MSB form. (When handling the instruction in the
3044 non-macro case, these arguments are sizes from which
3045 MSB values must be calculated.) */
3046 insn.insn_opcode |= (va_arg (args, int)
3047 & OP_MASK_INSMSB) << OP_SH_INSMSB;
3048 continue;
3049
3050 case 'C':
3051 case 'G':
3052 case 'H':
3053 /* Note that in the macro case, these arguments are already
3054 in MSBD form. (When handling the instruction in the
3055 non-macro case, these arguments are sizes from which
3056 MSBD values must be calculated.) */
3057 insn.insn_opcode |= (va_arg (args, int)
3058 & OP_MASK_EXTMSBD) << OP_SH_EXTMSBD;
3059 continue;
3060
3061 default:
3062 internalError ();
3063 }
3064 continue;
3065
252b5132
RH
3066 case 't':
3067 case 'w':
3068 case 'E':
38487616 3069 insn.insn_opcode |= va_arg (args, int) << OP_SH_RT;
252b5132
RH
3070 continue;
3071
3072 case 'c':
38487616
TS
3073 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE;
3074 continue;
3075
252b5132
RH
3076 case 'T':
3077 case 'W':
38487616 3078 insn.insn_opcode |= va_arg (args, int) << OP_SH_FT;
252b5132
RH
3079 continue;
3080
3081 case 'd':
3082 case 'G':
af7ee8bf 3083 case 'K':
38487616 3084 insn.insn_opcode |= va_arg (args, int) << OP_SH_RD;
252b5132
RH
3085 continue;
3086
4372b673
NC
3087 case 'U':
3088 {
3089 int tmp = va_arg (args, int);
3090
38487616
TS
3091 insn.insn_opcode |= tmp << OP_SH_RT;
3092 insn.insn_opcode |= tmp << OP_SH_RD;
beae10d5 3093 continue;
4372b673
NC
3094 }
3095
252b5132
RH
3096 case 'V':
3097 case 'S':
38487616 3098 insn.insn_opcode |= va_arg (args, int) << OP_SH_FS;
252b5132
RH
3099 continue;
3100
3101 case 'z':
3102 continue;
3103
3104 case '<':
38487616 3105 insn.insn_opcode |= va_arg (args, int) << OP_SH_SHAMT;
252b5132
RH
3106 continue;
3107
3108 case 'D':
38487616 3109 insn.insn_opcode |= va_arg (args, int) << OP_SH_FD;
252b5132
RH
3110 continue;
3111
3112 case 'B':
38487616 3113 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE20;
252b5132
RH
3114 continue;
3115
4372b673 3116 case 'J':
38487616 3117 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE19;
4372b673
NC
3118 continue;
3119
252b5132 3120 case 'q':
38487616 3121 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE2;
252b5132
RH
3122 continue;
3123
3124 case 'b':
3125 case 's':
3126 case 'r':
3127 case 'v':
38487616 3128 insn.insn_opcode |= va_arg (args, int) << OP_SH_RS;
252b5132
RH
3129 continue;
3130
3131 case 'i':
3132 case 'j':
3133 case 'o':
f6688943 3134 *r = (bfd_reloc_code_real_type) va_arg (args, int);
cdf6fd85 3135 assert (*r == BFD_RELOC_GPREL16
f6688943
TS
3136 || *r == BFD_RELOC_MIPS_LITERAL
3137 || *r == BFD_RELOC_MIPS_HIGHER
3138 || *r == BFD_RELOC_HI16_S
3139 || *r == BFD_RELOC_LO16
3140 || *r == BFD_RELOC_MIPS_GOT16
3141 || *r == BFD_RELOC_MIPS_CALL16
438c16b8
TS
3142 || *r == BFD_RELOC_MIPS_GOT_DISP
3143 || *r == BFD_RELOC_MIPS_GOT_PAGE
3144 || *r == BFD_RELOC_MIPS_GOT_OFST
f6688943
TS
3145 || *r == BFD_RELOC_MIPS_GOT_LO16
3146 || *r == BFD_RELOC_MIPS_CALL_LO16
252b5132 3147 || (ep->X_op == O_subtract
f6688943 3148 && *r == BFD_RELOC_PCREL_LO16));
252b5132
RH
3149 continue;
3150
3151 case 'u':
f6688943 3152 *r = (bfd_reloc_code_real_type) va_arg (args, int);
252b5132
RH
3153 assert (ep != NULL
3154 && (ep->X_op == O_constant
3155 || (ep->X_op == O_symbol
f6688943
TS
3156 && (*r == BFD_RELOC_MIPS_HIGHEST
3157 || *r == BFD_RELOC_HI16_S
3158 || *r == BFD_RELOC_HI16
3159 || *r == BFD_RELOC_GPREL16
3160 || *r == BFD_RELOC_MIPS_GOT_HI16
3161 || *r == BFD_RELOC_MIPS_CALL_HI16))
252b5132 3162 || (ep->X_op == O_subtract
f6688943 3163 && *r == BFD_RELOC_PCREL_HI16_S)));
252b5132
RH
3164 continue;
3165
3166 case 'p':
3167 assert (ep != NULL);
3168 /*
3169 * This allows macro() to pass an immediate expression for
3170 * creating short branches without creating a symbol.
0b25d3e6
AO
3171 * Note that the expression still might come from the assembly
3172 * input, in which case the value is not checked for range nor
3173 * is a relocation entry generated (yuck).
252b5132
RH
3174 */
3175 if (ep->X_op == O_constant)
3176 {
3177 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
3178 ep = NULL;
3179 }
3180 else
0b25d3e6 3181 *r = BFD_RELOC_16_PCREL_S2;
252b5132
RH
3182 continue;
3183
3184 case 'a':
3185 assert (ep != NULL);
f6688943 3186 *r = BFD_RELOC_MIPS_JMP;
252b5132
RH
3187 continue;
3188
3189 case 'C':
3190 insn.insn_opcode |= va_arg (args, unsigned long);
3191 continue;
3192
3193 default:
3194 internalError ();
3195 }
3196 break;
3197 }
3198 va_end (args);
f6688943 3199 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 3200
4d7206a2 3201 append_insn (&insn, ep, r);
252b5132
RH
3202}
3203
3204static void
67c0d1eb 3205mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
17a2f251 3206 va_list args)
252b5132
RH
3207{
3208 struct mips_cl_insn insn;
f6688943
TS
3209 bfd_reloc_code_real_type r[3]
3210 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 3211
252b5132
RH
3212 insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
3213 assert (insn.insn_mo);
3214 assert (strcmp (name, insn.insn_mo->name) == 0);
3215
3216 while (strcmp (fmt, insn.insn_mo->args) != 0
3217 || insn.insn_mo->pinfo == INSN_MACRO)
3218 {
3219 ++insn.insn_mo;
3220 assert (insn.insn_mo->name);
3221 assert (strcmp (name, insn.insn_mo->name) == 0);
3222 }
3223
3224 insn.insn_opcode = insn.insn_mo->match;
b34976b6 3225 insn.use_extend = FALSE;
252b5132
RH
3226
3227 for (;;)
3228 {
3229 int c;
3230
3231 c = *fmt++;
3232 switch (c)
3233 {
3234 case '\0':
3235 break;
3236
3237 case ',':
3238 case '(':
3239 case ')':
3240 continue;
3241
3242 case 'y':
3243 case 'w':
3244 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
3245 continue;
3246
3247 case 'x':
3248 case 'v':
3249 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
3250 continue;
3251
3252 case 'z':
3253 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
3254 continue;
3255
3256 case 'Z':
3257 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
3258 continue;
3259
3260 case '0':
3261 case 'S':
3262 case 'P':
3263 case 'R':
3264 continue;
3265
3266 case 'X':
3267 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
3268 continue;
3269
3270 case 'Y':
3271 {
3272 int regno;
3273
3274 regno = va_arg (args, int);
3275 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
3276 insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
3277 }
3278 continue;
3279
3280 case '<':
3281 case '>':
3282 case '4':
3283 case '5':
3284 case 'H':
3285 case 'W':
3286 case 'D':
3287 case 'j':
3288 case '8':
3289 case 'V':
3290 case 'C':
3291 case 'U':
3292 case 'k':
3293 case 'K':
3294 case 'p':
3295 case 'q':
3296 {
3297 assert (ep != NULL);
3298
3299 if (ep->X_op != O_constant)
874e8986 3300 *r = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
3301 else
3302 {
b34976b6
AM
3303 mips16_immed (NULL, 0, c, ep->X_add_number, FALSE, FALSE,
3304 FALSE, &insn.insn_opcode, &insn.use_extend,
c4e7957c 3305 &insn.extend);
252b5132 3306 ep = NULL;
f6688943 3307 *r = BFD_RELOC_UNUSED;
252b5132
RH
3308 }
3309 }
3310 continue;
3311
3312 case '6':
3313 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
3314 continue;
3315 }
3316
3317 break;
3318 }
3319
f6688943 3320 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 3321
4d7206a2 3322 append_insn (&insn, ep, r);
252b5132
RH
3323}
3324
438c16b8
TS
3325/*
3326 * Generate a "jalr" instruction with a relocation hint to the called
3327 * function. This occurs in NewABI PIC code.
3328 */
3329static void
67c0d1eb 3330macro_build_jalr (expressionS *ep)
438c16b8 3331{
685736be 3332 char *f = NULL;
b34976b6 3333
438c16b8 3334 if (HAVE_NEWABI)
f21f8242 3335 {
cc3d92a5 3336 frag_grow (8);
f21f8242
AO
3337 f = frag_more (0);
3338 }
67c0d1eb 3339 macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
438c16b8 3340 if (HAVE_NEWABI)
f21f8242 3341 fix_new_exp (frag_now, f - frag_now->fr_literal,
a105a300 3342 4, ep, FALSE, BFD_RELOC_MIPS_JALR);
438c16b8
TS
3343}
3344
252b5132
RH
3345/*
3346 * Generate a "lui" instruction.
3347 */
3348static void
67c0d1eb 3349macro_build_lui (expressionS *ep, int regnum)
252b5132
RH
3350{
3351 expressionS high_expr;
3352 struct mips_cl_insn insn;
f6688943
TS
3353 bfd_reloc_code_real_type r[3]
3354 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
5a38dc70
AM
3355 const char *name = "lui";
3356 const char *fmt = "t,u";
252b5132
RH
3357
3358 assert (! mips_opts.mips16);
3359
4d7206a2 3360 high_expr = *ep;
252b5132
RH
3361
3362 if (high_expr.X_op == O_constant)
3363 {
3364 /* we can compute the instruction now without a relocation entry */
e7d556df
TS
3365 high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3366 >> 16) & 0xffff;
f6688943 3367 *r = BFD_RELOC_UNUSED;
252b5132 3368 }
78e1bb40 3369 else
252b5132
RH
3370 {
3371 assert (ep->X_op == O_symbol);
3372 /* _gp_disp is a special case, used from s_cpload. */
3373 assert (mips_pic == NO_PIC
78e1bb40
AO
3374 || (! HAVE_NEWABI
3375 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0));
f6688943 3376 *r = BFD_RELOC_HI16_S;
252b5132
RH
3377 }
3378
252b5132
RH
3379 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3380 assert (insn.insn_mo);
3381 assert (strcmp (name, insn.insn_mo->name) == 0);
3382 assert (strcmp (fmt, insn.insn_mo->args) == 0);
3383
3384 insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
f6688943 3385 if (*r == BFD_RELOC_UNUSED)
252b5132
RH
3386 {
3387 insn.insn_opcode |= high_expr.X_add_number;
4d7206a2 3388 append_insn (&insn, NULL, r);
252b5132
RH
3389 }
3390 else
4d7206a2 3391 append_insn (&insn, &high_expr, r);
252b5132
RH
3392}
3393
885add95
CD
3394/* Generate a sequence of instructions to do a load or store from a constant
3395 offset off of a base register (breg) into/from a target register (treg),
3396 using AT if necessary. */
3397static void
67c0d1eb
RS
3398macro_build_ldst_constoffset (expressionS *ep, const char *op,
3399 int treg, int breg, int dbl)
885add95
CD
3400{
3401 assert (ep->X_op == O_constant);
3402
256ab948 3403 /* Sign-extending 32-bit constants makes their handling easier. */
d17e7bce
TS
3404 if (! dbl && ! ((ep->X_add_number & ~((bfd_vma) 0x7fffffff))
3405 == ~((bfd_vma) 0x7fffffff)))
ae826530 3406 {
d17e7bce 3407 if (ep->X_add_number & ~((bfd_vma) 0xffffffff))
1b8e29e5 3408 as_bad (_("constant too large"));
ae826530 3409
1b8e29e5
TS
3410 ep->X_add_number = (((ep->X_add_number & 0xffffffff) ^ 0x80000000)
3411 - 0x80000000);
ae826530 3412 }
256ab948 3413
67c1ffbe 3414 /* Right now, this routine can only handle signed 32-bit constants. */
ecd13cd3 3415 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
885add95
CD
3416 as_warn (_("operand overflow"));
3417
3418 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
3419 {
3420 /* Signed 16-bit offset will fit in the op. Easy! */
67c0d1eb 3421 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
885add95
CD
3422 }
3423 else
3424 {
3425 /* 32-bit offset, need multiple instructions and AT, like:
3426 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
3427 addu $tempreg,$tempreg,$breg
3428 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
3429 to handle the complete offset. */
67c0d1eb
RS
3430 macro_build_lui (ep, AT);
3431 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
3432 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
885add95
CD
3433
3434 if (mips_opts.noat)
3435 as_warn (_("Macro used $at after \".set noat\""));
3436 }
3437}
3438
252b5132
RH
3439/* set_at()
3440 * Generates code to set the $at register to true (one)
3441 * if reg is less than the immediate expression.
3442 */
3443static void
67c0d1eb 3444set_at (int reg, int unsignedp)
252b5132
RH
3445{
3446 if (imm_expr.X_op == O_constant
3447 && imm_expr.X_add_number >= -0x8000
3448 && imm_expr.X_add_number < 0x8000)
67c0d1eb
RS
3449 macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
3450 AT, reg, BFD_RELOC_LO16);
252b5132
RH
3451 else
3452 {
67c0d1eb
RS
3453 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
3454 macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
252b5132
RH
3455 }
3456}
3457
13757d0c
TS
3458static void
3459normalize_constant_expr (expressionS *ex)
3460{
3461 if (ex->X_op == O_constant && HAVE_32BIT_GPRS)
3462 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
3463 - 0x80000000);
3464}
3465
252b5132
RH
3466/* Warn if an expression is not a constant. */
3467
3468static void
17a2f251 3469check_absolute_expr (struct mips_cl_insn *ip, expressionS *ex)
252b5132
RH
3470{
3471 if (ex->X_op == O_big)
3472 as_bad (_("unsupported large constant"));
3473 else if (ex->X_op != O_constant)
3474 as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
13757d0c
TS
3475
3476 normalize_constant_expr (ex);
252b5132
RH
3477}
3478
3479/* Count the leading zeroes by performing a binary chop. This is a
3480 bulky bit of source, but performance is a LOT better for the
3481 majority of values than a simple loop to count the bits:
3482 for (lcnt = 0; (lcnt < 32); lcnt++)
3483 if ((v) & (1 << (31 - lcnt)))
3484 break;
3485 However it is not code size friendly, and the gain will drop a bit
3486 on certain cached systems.
3487*/
3488#define COUNT_TOP_ZEROES(v) \
3489 (((v) & ~0xffff) == 0 \
3490 ? ((v) & ~0xff) == 0 \
3491 ? ((v) & ~0xf) == 0 \
3492 ? ((v) & ~0x3) == 0 \
3493 ? ((v) & ~0x1) == 0 \
3494 ? !(v) \
3495 ? 32 \
3496 : 31 \
3497 : 30 \
3498 : ((v) & ~0x7) == 0 \
3499 ? 29 \
3500 : 28 \
3501 : ((v) & ~0x3f) == 0 \
3502 ? ((v) & ~0x1f) == 0 \
3503 ? 27 \
3504 : 26 \
3505 : ((v) & ~0x7f) == 0 \
3506 ? 25 \
3507 : 24 \
3508 : ((v) & ~0xfff) == 0 \
3509 ? ((v) & ~0x3ff) == 0 \
3510 ? ((v) & ~0x1ff) == 0 \
3511 ? 23 \
3512 : 22 \
3513 : ((v) & ~0x7ff) == 0 \
3514 ? 21 \
3515 : 20 \
3516 : ((v) & ~0x3fff) == 0 \
3517 ? ((v) & ~0x1fff) == 0 \
3518 ? 19 \
3519 : 18 \
3520 : ((v) & ~0x7fff) == 0 \
3521 ? 17 \
3522 : 16 \
3523 : ((v) & ~0xffffff) == 0 \
3524 ? ((v) & ~0xfffff) == 0 \
3525 ? ((v) & ~0x3ffff) == 0 \
3526 ? ((v) & ~0x1ffff) == 0 \
3527 ? 15 \
3528 : 14 \
3529 : ((v) & ~0x7ffff) == 0 \
3530 ? 13 \
3531 : 12 \
3532 : ((v) & ~0x3fffff) == 0 \
3533 ? ((v) & ~0x1fffff) == 0 \
3534 ? 11 \
3535 : 10 \
3536 : ((v) & ~0x7fffff) == 0 \
3537 ? 9 \
3538 : 8 \
3539 : ((v) & ~0xfffffff) == 0 \
3540 ? ((v) & ~0x3ffffff) == 0 \
3541 ? ((v) & ~0x1ffffff) == 0 \
3542 ? 7 \
3543 : 6 \
3544 : ((v) & ~0x7ffffff) == 0 \
3545 ? 5 \
3546 : 4 \
3547 : ((v) & ~0x3fffffff) == 0 \
3548 ? ((v) & ~0x1fffffff) == 0 \
3549 ? 3 \
3550 : 2 \
3551 : ((v) & ~0x7fffffff) == 0 \
3552 ? 1 \
3553 : 0)
3554
3555/* load_register()
67c1ffbe 3556 * This routine generates the least number of instructions necessary to load
252b5132
RH
3557 * an absolute expression value into a register.
3558 */
3559static void
67c0d1eb 3560load_register (int reg, expressionS *ep, int dbl)
252b5132
RH
3561{
3562 int freg;
3563 expressionS hi32, lo32;
3564
3565 if (ep->X_op != O_big)
3566 {
3567 assert (ep->X_op == O_constant);
256ab948
TS
3568
3569 /* Sign-extending 32-bit constants makes their handling easier. */
d17e7bce
TS
3570 if (! dbl && ! ((ep->X_add_number & ~((bfd_vma) 0x7fffffff))
3571 == ~((bfd_vma) 0x7fffffff)))
ae826530 3572 {
d17e7bce 3573 if (ep->X_add_number & ~((bfd_vma) 0xffffffff))
1b8e29e5 3574 as_bad (_("constant too large"));
ae826530 3575
1b8e29e5
TS
3576 ep->X_add_number = (((ep->X_add_number & 0xffffffff) ^ 0x80000000)
3577 - 0x80000000);
ae826530 3578 }
256ab948
TS
3579
3580 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
252b5132
RH
3581 {
3582 /* We can handle 16 bit signed values with an addiu to
3583 $zero. No need to ever use daddiu here, since $zero and
3584 the result are always correct in 32 bit mode. */
67c0d1eb 3585 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
3586 return;
3587 }
3588 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3589 {
3590 /* We can handle 16 bit unsigned values with an ori to
3591 $zero. */
67c0d1eb 3592 macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
252b5132
RH
3593 return;
3594 }
256ab948 3595 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
252b5132
RH
3596 {
3597 /* 32 bit values require an lui. */
67c0d1eb 3598 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_HI16);
252b5132 3599 if ((ep->X_add_number & 0xffff) != 0)
67c0d1eb 3600 macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
252b5132
RH
3601 return;
3602 }
3603 }
3604
3605 /* The value is larger than 32 bits. */
3606
ca4e0257 3607 if (HAVE_32BIT_GPRS)
252b5132 3608 {
956cd1d6
TS
3609 as_bad (_("Number (0x%lx) larger than 32 bits"),
3610 (unsigned long) ep->X_add_number);
67c0d1eb 3611 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
3612 return;
3613 }
3614
3615 if (ep->X_op != O_big)
3616 {
3617 hi32 = *ep;
3618 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3619 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3620 hi32.X_add_number &= 0xffffffff;
3621 lo32 = *ep;
3622 lo32.X_add_number &= 0xffffffff;
3623 }
3624 else
3625 {
3626 assert (ep->X_add_number > 2);
3627 if (ep->X_add_number == 3)
3628 generic_bignum[3] = 0;
3629 else if (ep->X_add_number > 4)
3630 as_bad (_("Number larger than 64 bits"));
3631 lo32.X_op = O_constant;
3632 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3633 hi32.X_op = O_constant;
3634 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3635 }
3636
3637 if (hi32.X_add_number == 0)
3638 freg = 0;
3639 else
3640 {
3641 int shift, bit;
3642 unsigned long hi, lo;
3643
956cd1d6 3644 if (hi32.X_add_number == (offsetT) 0xffffffff)
beae10d5
KH
3645 {
3646 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3647 {
67c0d1eb 3648 macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
3649 return;
3650 }
3651 if (lo32.X_add_number & 0x80000000)
3652 {
67c0d1eb 3653 macro_build (&lo32, "lui", "t,u", reg, BFD_RELOC_HI16);
252b5132 3654 if (lo32.X_add_number & 0xffff)
67c0d1eb 3655 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
beae10d5
KH
3656 return;
3657 }
3658 }
252b5132
RH
3659
3660 /* Check for 16bit shifted constant. We know that hi32 is
3661 non-zero, so start the mask on the first bit of the hi32
3662 value. */
3663 shift = 17;
3664 do
beae10d5
KH
3665 {
3666 unsigned long himask, lomask;
3667
3668 if (shift < 32)
3669 {
3670 himask = 0xffff >> (32 - shift);
3671 lomask = (0xffff << shift) & 0xffffffff;
3672 }
3673 else
3674 {
3675 himask = 0xffff << (shift - 32);
3676 lomask = 0;
3677 }
3678 if ((hi32.X_add_number & ~(offsetT) himask) == 0
3679 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3680 {
3681 expressionS tmp;
3682
3683 tmp.X_op = O_constant;
3684 if (shift < 32)
3685 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3686 | (lo32.X_add_number >> shift));
3687 else
3688 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
67c0d1eb
RS
3689 macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
3690 macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", "d,w,<",
3691 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
3692 return;
3693 }
f9419b05 3694 ++shift;
beae10d5
KH
3695 }
3696 while (shift <= (64 - 16));
252b5132
RH
3697
3698 /* Find the bit number of the lowest one bit, and store the
3699 shifted value in hi/lo. */
3700 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3701 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3702 if (lo != 0)
3703 {
3704 bit = 0;
3705 while ((lo & 1) == 0)
3706 {
3707 lo >>= 1;
3708 ++bit;
3709 }
3710 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3711 hi >>= bit;
3712 }
3713 else
3714 {
3715 bit = 32;
3716 while ((hi & 1) == 0)
3717 {
3718 hi >>= 1;
3719 ++bit;
3720 }
3721 lo = hi;
3722 hi = 0;
3723 }
3724
3725 /* Optimize if the shifted value is a (power of 2) - 1. */
3726 if ((hi == 0 && ((lo + 1) & lo) == 0)
3727 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
beae10d5
KH
3728 {
3729 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
252b5132 3730 if (shift != 0)
beae10d5 3731 {
252b5132
RH
3732 expressionS tmp;
3733
3734 /* This instruction will set the register to be all
3735 ones. */
beae10d5
KH
3736 tmp.X_op = O_constant;
3737 tmp.X_add_number = (offsetT) -1;
67c0d1eb 3738 macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
3739 if (bit != 0)
3740 {
3741 bit += shift;
67c0d1eb
RS
3742 macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", "d,w,<",
3743 reg, reg, (bit >= 32) ? bit - 32 : bit);
beae10d5 3744 }
67c0d1eb
RS
3745 macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", "d,w,<",
3746 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
3747 return;
3748 }
3749 }
252b5132
RH
3750
3751 /* Sign extend hi32 before calling load_register, because we can
3752 generally get better code when we load a sign extended value. */
3753 if ((hi32.X_add_number & 0x80000000) != 0)
beae10d5 3754 hi32.X_add_number |= ~(offsetT) 0xffffffff;
67c0d1eb 3755 load_register (reg, &hi32, 0);
252b5132
RH
3756 freg = reg;
3757 }
3758 if ((lo32.X_add_number & 0xffff0000) == 0)
3759 {
3760 if (freg != 0)
3761 {
67c0d1eb 3762 macro_build (NULL, "dsll32", "d,w,<", reg, freg, 0);
252b5132
RH
3763 freg = reg;
3764 }
3765 }
3766 else
3767 {
3768 expressionS mid16;
3769
956cd1d6 3770 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
beae10d5 3771 {
67c0d1eb
RS
3772 macro_build (&lo32, "lui", "t,u", reg, BFD_RELOC_HI16);
3773 macro_build (NULL, "dsrl32", "d,w,<", reg, reg, 0);
beae10d5
KH
3774 return;
3775 }
252b5132
RH
3776
3777 if (freg != 0)
3778 {
67c0d1eb 3779 macro_build (NULL, "dsll", "d,w,<", reg, freg, 16);
252b5132
RH
3780 freg = reg;
3781 }
3782 mid16 = lo32;
3783 mid16.X_add_number >>= 16;
67c0d1eb
RS
3784 macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
3785 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
252b5132
RH
3786 freg = reg;
3787 }
3788 if ((lo32.X_add_number & 0xffff) != 0)
67c0d1eb 3789 macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
252b5132
RH
3790}
3791
3792/* Load an address into a register. */
3793
3794static void
67c0d1eb 3795load_address (int reg, expressionS *ep, int *used_at)
252b5132 3796{
252b5132
RH
3797 if (ep->X_op != O_constant
3798 && ep->X_op != O_symbol)
3799 {
3800 as_bad (_("expression too complex"));
3801 ep->X_op = O_constant;
3802 }
3803
3804 if (ep->X_op == O_constant)
3805 {
67c0d1eb 3806 load_register (reg, ep, HAVE_64BIT_ADDRESSES);
252b5132
RH
3807 return;
3808 }
3809
3810 if (mips_pic == NO_PIC)
3811 {
3812 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 3813 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
3814 Otherwise we want
3815 lui $reg,<sym> (BFD_RELOC_HI16_S)
3816 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
d6bc6245 3817 If we have an addend, we always use the latter form.
76b3015f 3818
d6bc6245
TS
3819 With 64bit address space and a usable $at we want
3820 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3821 lui $at,<sym> (BFD_RELOC_HI16_S)
3822 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3823 daddiu $at,<sym> (BFD_RELOC_LO16)
3824 dsll32 $reg,0
3a482fd5 3825 daddu $reg,$reg,$at
76b3015f 3826
c03099e6 3827 If $at is already in use, we use a path which is suboptimal
d6bc6245
TS
3828 on superscalar processors.
3829 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3830 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3831 dsll $reg,16
3832 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
3833 dsll $reg,16
3834 daddiu $reg,<sym> (BFD_RELOC_LO16)
3835 */
c9914766 3836 if (HAVE_64BIT_ADDRESSES)
d6bc6245 3837 {
e864ceca
RS
3838 /* ??? We don't provide a GP-relative alternative for these macros.
3839 It used not to be possible with the original relaxation code,
3840 but it could be done now. */
d6bc6245 3841
460597ba 3842 if (*used_at == 0 && ! mips_opts.noat)
d6bc6245 3843 {
67c0d1eb
RS
3844 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_HIGHEST);
3845 macro_build (ep, "lui", "t,u", AT, BFD_RELOC_HI16_S);
3846 macro_build (ep, "daddiu", "t,r,j", reg, reg,
3847 BFD_RELOC_MIPS_HIGHER);
3848 macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
3849 macro_build (NULL, "dsll32", "d,w,<", reg, reg, 0);
3850 macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
d6bc6245
TS
3851 *used_at = 1;
3852 }
3853 else
3854 {
67c0d1eb
RS
3855 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_HIGHEST);
3856 macro_build (ep, "daddiu", "t,r,j", reg, reg,
3857 BFD_RELOC_MIPS_HIGHER);
3858 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
3859 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
3860 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
3861 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
d6bc6245
TS
3862 }
3863 }
252b5132
RH
3864 else
3865 {
d6bc6245
TS
3866 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3867 && ! nopic_need_relax (ep->X_add_symbol, 1))
3868 {
4d7206a2 3869 relax_start (ep->X_add_symbol);
67c0d1eb 3870 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
17a2f251 3871 mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 3872 relax_switch ();
d6bc6245 3873 }
67c0d1eb
RS
3874 macro_build_lui (ep, reg);
3875 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
3876 reg, reg, BFD_RELOC_LO16);
4d7206a2
RS
3877 if (mips_relax.sequence)
3878 relax_end ();
d6bc6245 3879 }
252b5132
RH
3880 }
3881 else if (mips_pic == SVR4_PIC && ! mips_big_got)
3882 {
3883 expressionS ex;
3884
3885 /* If this is a reference to an external symbol, we want
3886 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3887 Otherwise we want
3888 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3889 nop
3890 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
f5040a92
AO
3891 If there is a constant, it must be added in after.
3892
ed6fb7bd 3893 If we have NewABI, we want
f5040a92
AO
3894 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
3895 unless we're referencing a global symbol with a non-zero
3896 offset, in which case cst must be added separately. */
ed6fb7bd
SC
3897 if (HAVE_NEWABI)
3898 {
f5040a92
AO
3899 if (ep->X_add_number)
3900 {
4d7206a2 3901 ex.X_add_number = ep->X_add_number;
f5040a92 3902 ep->X_add_number = 0;
4d7206a2 3903 relax_start (ep->X_add_symbol);
67c0d1eb
RS
3904 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3905 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
3906 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3907 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3908 ex.X_op = O_constant;
67c0d1eb 3909 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 3910 reg, reg, BFD_RELOC_LO16);
f5040a92 3911 ep->X_add_number = ex.X_add_number;
4d7206a2 3912 relax_switch ();
f5040a92 3913 }
67c0d1eb 3914 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 3915 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2
RS
3916 if (mips_relax.sequence)
3917 relax_end ();
ed6fb7bd
SC
3918 }
3919 else
3920 {
f5040a92
AO
3921 ex.X_add_number = ep->X_add_number;
3922 ep->X_add_number = 0;
67c0d1eb
RS
3923 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3924 BFD_RELOC_MIPS_GOT16, mips_gp_register);
3925 macro_build (NULL, "nop", "");
4d7206a2
RS
3926 relax_start (ep->X_add_symbol);
3927 relax_switch ();
67c0d1eb 3928 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 3929 BFD_RELOC_LO16);
4d7206a2 3930 relax_end ();
ed6fb7bd 3931
f5040a92
AO
3932 if (ex.X_add_number != 0)
3933 {
3934 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3935 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3936 ex.X_op = O_constant;
67c0d1eb 3937 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 3938 reg, reg, BFD_RELOC_LO16);
f5040a92 3939 }
252b5132
RH
3940 }
3941 }
3942 else if (mips_pic == SVR4_PIC)
3943 {
3944 expressionS ex;
252b5132
RH
3945
3946 /* This is the large GOT case. If this is a reference to an
3947 external symbol, we want
3948 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
3949 addu $reg,$reg,$gp
3950 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
f5040a92
AO
3951
3952 Otherwise, for a reference to a local symbol in old ABI, we want
252b5132
RH
3953 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3954 nop
3955 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
684022ea 3956 If there is a constant, it must be added in after.
f5040a92
AO
3957
3958 In the NewABI, for local symbols, with or without offsets, we want:
438c16b8
TS
3959 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
3960 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
f5040a92 3961 */
438c16b8
TS
3962 if (HAVE_NEWABI)
3963 {
4d7206a2 3964 ex.X_add_number = ep->X_add_number;
f5040a92 3965 ep->X_add_number = 0;
4d7206a2 3966 relax_start (ep->X_add_symbol);
67c0d1eb
RS
3967 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_GOT_HI16);
3968 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
3969 reg, reg, mips_gp_register);
3970 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
3971 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
f5040a92
AO
3972 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3973 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3974 else if (ex.X_add_number)
3975 {
3976 ex.X_op = O_constant;
67c0d1eb
RS
3977 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
3978 BFD_RELOC_LO16);
f5040a92
AO
3979 }
3980
3981 ep->X_add_number = ex.X_add_number;
4d7206a2 3982 relax_switch ();
67c0d1eb 3983 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 3984 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
67c0d1eb
RS
3985 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
3986 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 3987 relax_end ();
438c16b8 3988 }
252b5132 3989 else
438c16b8 3990 {
f5040a92
AO
3991 ex.X_add_number = ep->X_add_number;
3992 ep->X_add_number = 0;
4d7206a2 3993 relax_start (ep->X_add_symbol);
67c0d1eb
RS
3994 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_GOT_HI16);
3995 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
3996 reg, reg, mips_gp_register);
3997 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
3998 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
4d7206a2
RS
3999 relax_switch ();
4000 if (reg_needs_delay (mips_gp_register))
438c16b8
TS
4001 {
4002 /* We need a nop before loading from $gp. This special
4003 check is required because the lui which starts the main
4004 instruction stream does not refer to $gp, and so will not
4005 insert the nop which may be required. */
67c0d1eb 4006 macro_build (NULL, "nop", "");
438c16b8 4007 }
67c0d1eb 4008 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 4009 BFD_RELOC_MIPS_GOT16, mips_gp_register);
67c0d1eb
RS
4010 macro_build (NULL, "nop", "");
4011 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 4012 BFD_RELOC_LO16);
4d7206a2 4013 relax_end ();
438c16b8 4014
f5040a92
AO
4015 if (ex.X_add_number != 0)
4016 {
4017 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4018 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4019 ex.X_op = O_constant;
67c0d1eb
RS
4020 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
4021 BFD_RELOC_LO16);
f5040a92 4022 }
252b5132
RH
4023 }
4024 }
4025 else if (mips_pic == EMBEDDED_PIC)
4026 {
4027 /* We always do
cdf6fd85 4028 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
c9914766 4029 */
67c0d1eb
RS
4030 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
4031 reg, mips_gp_register, BFD_RELOC_GPREL16);
252b5132
RH
4032 }
4033 else
4034 abort ();
4035}
4036
ea1fb5dc
RS
4037/* Move the contents of register SOURCE into register DEST. */
4038
4039static void
67c0d1eb 4040move_register (int dest, int source)
ea1fb5dc 4041{
67c0d1eb
RS
4042 macro_build (NULL, HAVE_32BIT_GPRS ? "addu" : "daddu", "d,v,t",
4043 dest, source, 0);
ea1fb5dc
RS
4044}
4045
4d7206a2 4046/* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
f6a22291
MR
4047 LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
4048 The two alternatives are:
4d7206a2
RS
4049
4050 Global symbol Local sybmol
4051 ------------- ------------
4052 lw DEST,%got(SYMBOL) lw DEST,%got(SYMBOL + OFFSET)
4053 ... ...
4054 addiu DEST,DEST,OFFSET addiu DEST,DEST,%lo(SYMBOL + OFFSET)
4055
4056 load_got_offset emits the first instruction and add_got_offset
f6a22291
MR
4057 emits the second for a 16-bit offset or add_got_offset_hilo emits
4058 a sequence to add a 32-bit offset using a scratch register. */
4d7206a2
RS
4059
4060static void
67c0d1eb 4061load_got_offset (int dest, expressionS *local)
4d7206a2
RS
4062{
4063 expressionS global;
4064
4065 global = *local;
4066 global.X_add_number = 0;
4067
4068 relax_start (local->X_add_symbol);
67c0d1eb
RS
4069 macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
4070 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2 4071 relax_switch ();
67c0d1eb
RS
4072 macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
4073 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2
RS
4074 relax_end ();
4075}
4076
4077static void
67c0d1eb 4078add_got_offset (int dest, expressionS *local)
4d7206a2
RS
4079{
4080 expressionS global;
4081
4082 global.X_op = O_constant;
4083 global.X_op_symbol = NULL;
4084 global.X_add_symbol = NULL;
4085 global.X_add_number = local->X_add_number;
4086
4087 relax_start (local->X_add_symbol);
67c0d1eb 4088 macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
4d7206a2
RS
4089 dest, dest, BFD_RELOC_LO16);
4090 relax_switch ();
67c0d1eb 4091 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
4d7206a2
RS
4092 relax_end ();
4093}
4094
f6a22291
MR
4095static void
4096add_got_offset_hilo (int dest, expressionS *local, int tmp)
4097{
4098 expressionS global;
4099 int hold_mips_optimize;
4100
4101 global.X_op = O_constant;
4102 global.X_op_symbol = NULL;
4103 global.X_add_symbol = NULL;
4104 global.X_add_number = local->X_add_number;
4105
4106 relax_start (local->X_add_symbol);
4107 load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
4108 relax_switch ();
4109 /* Set mips_optimize around the lui instruction to avoid
4110 inserting an unnecessary nop after the lw. */
4111 hold_mips_optimize = mips_optimize;
4112 mips_optimize = 2;
4113 macro_build_lui (&global, tmp);
4114 mips_optimize = hold_mips_optimize;
4115 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
4116 relax_end ();
4117
4118 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
4119}
4120
252b5132
RH
4121/*
4122 * Build macros
4123 * This routine implements the seemingly endless macro or synthesized
4124 * instructions and addressing modes in the mips assembly language. Many
4125 * of these macros are simple and are similar to each other. These could
67c1ffbe 4126 * probably be handled by some kind of table or grammar approach instead of
252b5132
RH
4127 * this verbose method. Others are not simple macros but are more like
4128 * optimizing code generation.
4129 * One interesting optimization is when several store macros appear
67c1ffbe 4130 * consecutively that would load AT with the upper half of the same address.
252b5132
RH
4131 * The ensuing load upper instructions are ommited. This implies some kind
4132 * of global optimization. We currently only optimize within a single macro.
4133 * For many of the load and store macros if the address is specified as a
4134 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
4135 * first load register 'at' with zero and use it as the base register. The
4136 * mips assembler simply uses register $zero. Just one tiny optimization
4137 * we're missing.
4138 */
4139static void
17a2f251 4140macro (struct mips_cl_insn *ip)
252b5132
RH
4141{
4142 register int treg, sreg, dreg, breg;
4143 int tempreg;
4144 int mask;
43841e91 4145 int used_at = 0;
252b5132
RH
4146 expressionS expr1;
4147 const char *s;
4148 const char *s2;
4149 const char *fmt;
4150 int likely = 0;
4151 int dbl = 0;
4152 int coproc = 0;
4153 int lr = 0;
4154 int imm = 0;
1abe91b1 4155 int call = 0;
252b5132 4156 int off;
67c0d1eb 4157 offsetT maxnum;
252b5132 4158 bfd_reloc_code_real_type r;
252b5132
RH
4159 int hold_mips_optimize;
4160
4161 assert (! mips_opts.mips16);
4162
4163 treg = (ip->insn_opcode >> 16) & 0x1f;
4164 dreg = (ip->insn_opcode >> 11) & 0x1f;
4165 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
4166 mask = ip->insn_mo->mask;
4167
4168 expr1.X_op = O_constant;
4169 expr1.X_op_symbol = NULL;
4170 expr1.X_add_symbol = NULL;
4171 expr1.X_add_number = 1;
4172
4173 switch (mask)
4174 {
4175 case M_DABS:
4176 dbl = 1;
4177 case M_ABS:
4178 /* bgez $a0,.+12
4179 move v0,$a0
4180 sub v0,$zero,$a0
4181 */
4182
b34976b6 4183 mips_emit_delays (TRUE);
252b5132
RH
4184 ++mips_opts.noreorder;
4185 mips_any_noreorder = 1;
4186
4187 expr1.X_add_number = 8;
67c0d1eb 4188 macro_build (&expr1, "bgez", "s,p", sreg);
252b5132 4189 if (dreg == sreg)
67c0d1eb 4190 macro_build (NULL, "nop", "", 0);
252b5132 4191 else
67c0d1eb
RS
4192 move_register (dreg, sreg);
4193 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
252b5132
RH
4194
4195 --mips_opts.noreorder;
4196 return;
4197
4198 case M_ADD_I:
4199 s = "addi";
4200 s2 = "add";
4201 goto do_addi;
4202 case M_ADDU_I:
4203 s = "addiu";
4204 s2 = "addu";
4205 goto do_addi;
4206 case M_DADD_I:
4207 dbl = 1;
4208 s = "daddi";
4209 s2 = "dadd";
4210 goto do_addi;
4211 case M_DADDU_I:
4212 dbl = 1;
4213 s = "daddiu";
4214 s2 = "daddu";
4215 do_addi:
4216 if (imm_expr.X_op == O_constant
4217 && imm_expr.X_add_number >= -0x8000
4218 && imm_expr.X_add_number < 0x8000)
4219 {
67c0d1eb 4220 macro_build (&imm_expr, s, "t,r,j", treg, sreg, BFD_RELOC_LO16);
252b5132
RH
4221 return;
4222 }
67c0d1eb
RS
4223 load_register (AT, &imm_expr, dbl);
4224 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
252b5132
RH
4225 break;
4226
4227 case M_AND_I:
4228 s = "andi";
4229 s2 = "and";
4230 goto do_bit;
4231 case M_OR_I:
4232 s = "ori";
4233 s2 = "or";
4234 goto do_bit;
4235 case M_NOR_I:
4236 s = "";
4237 s2 = "nor";
4238 goto do_bit;
4239 case M_XOR_I:
4240 s = "xori";
4241 s2 = "xor";
4242 do_bit:
4243 if (imm_expr.X_op == O_constant
4244 && imm_expr.X_add_number >= 0
4245 && imm_expr.X_add_number < 0x10000)
4246 {
4247 if (mask != M_NOR_I)
67c0d1eb 4248 macro_build (&imm_expr, s, "t,r,i", treg, sreg, BFD_RELOC_LO16);
252b5132
RH
4249 else
4250 {
67c0d1eb
RS
4251 macro_build (&imm_expr, "ori", "t,r,i",
4252 treg, sreg, BFD_RELOC_LO16);
4253 macro_build (NULL, "nor", "d,v,t", treg, treg, 0);
252b5132
RH
4254 }
4255 return;
4256 }
4257
67c0d1eb
RS
4258 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
4259 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
252b5132
RH
4260 break;
4261
4262 case M_BEQ_I:
4263 s = "beq";
4264 goto beq_i;
4265 case M_BEQL_I:
4266 s = "beql";
4267 likely = 1;
4268 goto beq_i;
4269 case M_BNE_I:
4270 s = "bne";
4271 goto beq_i;
4272 case M_BNEL_I:
4273 s = "bnel";
4274 likely = 1;
4275 beq_i:
4276 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4277 {
67c0d1eb 4278 macro_build (&offset_expr, s, "s,t,p", sreg, 0);
252b5132
RH
4279 return;
4280 }
67c0d1eb
RS
4281 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
4282 macro_build (&offset_expr, s, "s,t,p", sreg, AT);
252b5132
RH
4283 break;
4284
4285 case M_BGEL:
4286 likely = 1;
4287 case M_BGE:
4288 if (treg == 0)
4289 {
67c0d1eb 4290 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", sreg);
252b5132
RH
4291 return;
4292 }
4293 if (sreg == 0)
4294 {
67c0d1eb 4295 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", treg);
252b5132
RH
4296 return;
4297 }
67c0d1eb
RS
4298 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
4299 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4300 break;
4301
4302 case M_BGTL_I:
4303 likely = 1;
4304 case M_BGT_I:
4305 /* check for > max integer */
4306 maxnum = 0x7fffffff;
ca4e0257 4307 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
4308 {
4309 maxnum <<= 16;
4310 maxnum |= 0xffff;
4311 maxnum <<= 16;
4312 maxnum |= 0xffff;
4313 }
4314 if (imm_expr.X_op == O_constant
4315 && imm_expr.X_add_number >= maxnum
ca4e0257 4316 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
4317 {
4318 do_false:
4319 /* result is always false */
4320 if (! likely)
67c0d1eb 4321 macro_build (NULL, "nop", "", 0);
252b5132 4322 else
67c0d1eb 4323 macro_build (&offset_expr, "bnel", "s,t,p", 0, 0);
252b5132
RH
4324 return;
4325 }
4326 if (imm_expr.X_op != O_constant)
4327 as_bad (_("Unsupported large constant"));
f9419b05 4328 ++imm_expr.X_add_number;
252b5132
RH
4329 /* FALLTHROUGH */
4330 case M_BGE_I:
4331 case M_BGEL_I:
4332 if (mask == M_BGEL_I)
4333 likely = 1;
4334 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4335 {
67c0d1eb 4336 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", sreg);
252b5132
RH
4337 return;
4338 }
4339 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4340 {
67c0d1eb 4341 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", sreg);
252b5132
RH
4342 return;
4343 }
4344 maxnum = 0x7fffffff;
ca4e0257 4345 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
4346 {
4347 maxnum <<= 16;
4348 maxnum |= 0xffff;
4349 maxnum <<= 16;
4350 maxnum |= 0xffff;
4351 }
4352 maxnum = - maxnum - 1;
4353 if (imm_expr.X_op == O_constant
4354 && imm_expr.X_add_number <= maxnum
ca4e0257 4355 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
4356 {
4357 do_true:
4358 /* result is always true */
4359 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
67c0d1eb 4360 macro_build (&offset_expr, "b", "p");
252b5132
RH
4361 return;
4362 }
67c0d1eb
RS
4363 set_at (sreg, 0);
4364 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4365 break;
4366
4367 case M_BGEUL:
4368 likely = 1;
4369 case M_BGEU:
4370 if (treg == 0)
4371 goto do_true;
4372 if (sreg == 0)
4373 {
67c0d1eb 4374 macro_build (&offset_expr, likely ? "beql" : "beq",
17a2f251 4375 "s,t,p", 0, treg);
252b5132
RH
4376 return;
4377 }
67c0d1eb
RS
4378 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
4379 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4380 break;
4381
4382 case M_BGTUL_I:
4383 likely = 1;
4384 case M_BGTU_I:
4385 if (sreg == 0
ca4e0257 4386 || (HAVE_32BIT_GPRS
252b5132 4387 && imm_expr.X_op == O_constant
956cd1d6 4388 && imm_expr.X_add_number == (offsetT) 0xffffffff))
252b5132
RH
4389 goto do_false;
4390 if (imm_expr.X_op != O_constant)
4391 as_bad (_("Unsupported large constant"));
f9419b05 4392 ++imm_expr.X_add_number;
252b5132
RH
4393 /* FALLTHROUGH */
4394 case M_BGEU_I:
4395 case M_BGEUL_I:
4396 if (mask == M_BGEUL_I)
4397 likely = 1;
4398 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4399 goto do_true;
4400 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4401 {
67c0d1eb 4402 macro_build (&offset_expr, likely ? "bnel" : "bne",
17a2f251 4403 "s,t,p", sreg, 0);
252b5132
RH
4404 return;
4405 }
67c0d1eb
RS
4406 set_at (sreg, 1);
4407 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4408 break;
4409
4410 case M_BGTL:
4411 likely = 1;
4412 case M_BGT:
4413 if (treg == 0)
4414 {
67c0d1eb 4415 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", sreg);
252b5132
RH
4416 return;
4417 }
4418 if (sreg == 0)
4419 {
67c0d1eb 4420 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", treg);
252b5132
RH
4421 return;
4422 }
67c0d1eb
RS
4423 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
4424 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4425 break;
4426
4427 case M_BGTUL:
4428 likely = 1;
4429 case M_BGTU:
4430 if (treg == 0)
4431 {
67c0d1eb 4432 macro_build (&offset_expr, likely ? "bnel" : "bne",
17a2f251 4433 "s,t,p", sreg, 0);
252b5132
RH
4434 return;
4435 }
4436 if (sreg == 0)
4437 goto do_false;
67c0d1eb
RS
4438 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
4439 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4440 break;
4441
4442 case M_BLEL:
4443 likely = 1;
4444 case M_BLE:
4445 if (treg == 0)
4446 {
67c0d1eb 4447 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", sreg);
252b5132
RH
4448 return;
4449 }
4450 if (sreg == 0)
4451 {
67c0d1eb 4452 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", treg);
252b5132
RH
4453 return;
4454 }
67c0d1eb
RS
4455 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
4456 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4457 break;
4458
4459 case M_BLEL_I:
4460 likely = 1;
4461 case M_BLE_I:
4462 maxnum = 0x7fffffff;
ca4e0257 4463 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
4464 {
4465 maxnum <<= 16;
4466 maxnum |= 0xffff;
4467 maxnum <<= 16;
4468 maxnum |= 0xffff;
4469 }
4470 if (imm_expr.X_op == O_constant
4471 && imm_expr.X_add_number >= maxnum
ca4e0257 4472 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
4473 goto do_true;
4474 if (imm_expr.X_op != O_constant)
4475 as_bad (_("Unsupported large constant"));
f9419b05 4476 ++imm_expr.X_add_number;
252b5132
RH
4477 /* FALLTHROUGH */
4478 case M_BLT_I:
4479 case M_BLTL_I:
4480 if (mask == M_BLTL_I)
4481 likely = 1;
4482 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4483 {
67c0d1eb 4484 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", sreg);
252b5132
RH
4485 return;
4486 }
4487 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4488 {
67c0d1eb 4489 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", sreg);
252b5132
RH
4490 return;
4491 }
67c0d1eb
RS
4492 set_at (sreg, 0);
4493 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4494 break;
4495
4496 case M_BLEUL:
4497 likely = 1;
4498 case M_BLEU:
4499 if (treg == 0)
4500 {
67c0d1eb 4501 macro_build (&offset_expr, likely ? "beql" : "beq",
17a2f251 4502 "s,t,p", sreg, 0);
252b5132
RH
4503 return;
4504 }
4505 if (sreg == 0)
4506 goto do_true;
67c0d1eb
RS
4507 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
4508 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4509 break;
4510
4511 case M_BLEUL_I:
4512 likely = 1;
4513 case M_BLEU_I:
4514 if (sreg == 0
ca4e0257 4515 || (HAVE_32BIT_GPRS
252b5132 4516 && imm_expr.X_op == O_constant
956cd1d6 4517 && imm_expr.X_add_number == (offsetT) 0xffffffff))
252b5132
RH
4518 goto do_true;
4519 if (imm_expr.X_op != O_constant)
4520 as_bad (_("Unsupported large constant"));
f9419b05 4521 ++imm_expr.X_add_number;
252b5132
RH
4522 /* FALLTHROUGH */
4523 case M_BLTU_I:
4524 case M_BLTUL_I:
4525 if (mask == M_BLTUL_I)
4526 likely = 1;
4527 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4528 goto do_false;
4529 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4530 {
67c0d1eb 4531 macro_build (&offset_expr, likely ? "beql" : "beq",
252b5132
RH
4532 "s,t,p", sreg, 0);
4533 return;
4534 }
67c0d1eb
RS
4535 set_at (sreg, 1);
4536 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4537 break;
4538
4539 case M_BLTL:
4540 likely = 1;
4541 case M_BLT:
4542 if (treg == 0)
4543 {
67c0d1eb 4544 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", sreg);
252b5132
RH
4545 return;
4546 }
4547 if (sreg == 0)
4548 {
67c0d1eb 4549 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", treg);
252b5132
RH
4550 return;
4551 }
67c0d1eb
RS
4552 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
4553 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4554 break;
4555
4556 case M_BLTUL:
4557 likely = 1;
4558 case M_BLTU:
4559 if (treg == 0)
4560 goto do_false;
4561 if (sreg == 0)
4562 {
67c0d1eb 4563 macro_build (&offset_expr, likely ? "bnel" : "bne",
17a2f251 4564 "s,t,p", 0, treg);
252b5132
RH
4565 return;
4566 }
67c0d1eb
RS
4567 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
4568 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4569 break;
4570
5f74bc13
CD
4571 case M_DEXT:
4572 {
4573 unsigned long pos;
4574 unsigned long size;
4575
4576 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
4577 {
4578 as_bad (_("Unsupported large constant"));
4579 pos = size = 1;
4580 }
4581 else
4582 {
4583 pos = (unsigned long) imm_expr.X_add_number;
4584 size = (unsigned long) imm2_expr.X_add_number;
4585 }
4586
4587 if (pos > 63)
4588 {
4589 as_bad (_("Improper position (%lu)"), pos);
4590 pos = 1;
4591 }
4592 if (size == 0 || size > 64
4593 || (pos + size - 1) > 63)
4594 {
4595 as_bad (_("Improper extract size (%lu, position %lu)"),
4596 size, pos);
4597 size = 1;
4598 }
4599
4600 if (size <= 32 && pos < 32)
4601 {
4602 s = "dext";
4603 fmt = "t,r,+A,+C";
4604 }
4605 else if (size <= 32)
4606 {
4607 s = "dextu";
4608 fmt = "t,r,+E,+H";
4609 }
4610 else
4611 {
4612 s = "dextm";
4613 fmt = "t,r,+A,+G";
4614 }
67c0d1eb 4615 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, pos, size - 1);
5f74bc13
CD
4616 }
4617 return;
4618
4619 case M_DINS:
4620 {
4621 unsigned long pos;
4622 unsigned long size;
4623
4624 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
4625 {
4626 as_bad (_("Unsupported large constant"));
4627 pos = size = 1;
4628 }
4629 else
4630 {
4631 pos = (unsigned long) imm_expr.X_add_number;
4632 size = (unsigned long) imm2_expr.X_add_number;
4633 }
4634
4635 if (pos > 63)
4636 {
4637 as_bad (_("Improper position (%lu)"), pos);
4638 pos = 1;
4639 }
4640 if (size == 0 || size > 64
4641 || (pos + size - 1) > 63)
4642 {
4643 as_bad (_("Improper insert size (%lu, position %lu)"),
4644 size, pos);
4645 size = 1;
4646 }
4647
4648 if (pos < 32 && (pos + size - 1) < 32)
4649 {
4650 s = "dins";
4651 fmt = "t,r,+A,+B";
4652 }
4653 else if (pos >= 32)
4654 {
4655 s = "dinsu";
4656 fmt = "t,r,+E,+F";
4657 }
4658 else
4659 {
4660 s = "dinsm";
4661 fmt = "t,r,+A,+F";
4662 }
67c0d1eb
RS
4663 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, pos,
4664 pos + size - 1);
5f74bc13
CD
4665 }
4666 return;
4667
252b5132
RH
4668 case M_DDIV_3:
4669 dbl = 1;
4670 case M_DIV_3:
4671 s = "mflo";
4672 goto do_div3;
4673 case M_DREM_3:
4674 dbl = 1;
4675 case M_REM_3:
4676 s = "mfhi";
4677 do_div3:
4678 if (treg == 0)
4679 {
4680 as_warn (_("Divide by zero."));
4681 if (mips_trap)
67c0d1eb 4682 macro_build (NULL, "teq", "s,t,q", 0, 0, 7);
252b5132 4683 else
67c0d1eb 4684 macro_build (NULL, "break", "c", 7);
252b5132
RH
4685 return;
4686 }
4687
b34976b6 4688 mips_emit_delays (TRUE);
252b5132
RH
4689 ++mips_opts.noreorder;
4690 mips_any_noreorder = 1;
4691 if (mips_trap)
4692 {
67c0d1eb
RS
4693 macro_build (NULL, "teq", "s,t,q", treg, 0, 7);
4694 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
252b5132
RH
4695 }
4696 else
4697 {
4698 expr1.X_add_number = 8;
67c0d1eb
RS
4699 macro_build (&expr1, "bne", "s,t,p", treg, 0);
4700 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4701 macro_build (NULL, "break", "c", 7);
252b5132
RH
4702 }
4703 expr1.X_add_number = -1;
f6a22291 4704 load_register (AT, &expr1, dbl);
252b5132 4705 expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
67c0d1eb 4706 macro_build (&expr1, "bne", "s,t,p", treg, AT);
252b5132
RH
4707 if (dbl)
4708 {
4709 expr1.X_add_number = 1;
f6a22291 4710 load_register (AT, &expr1, dbl);
67c0d1eb 4711 macro_build (NULL, "dsll32", "d,w,<", AT, AT, 31);
252b5132
RH
4712 }
4713 else
4714 {
4715 expr1.X_add_number = 0x80000000;
67c0d1eb 4716 macro_build (&expr1, "lui", "t,u", AT, BFD_RELOC_HI16);
252b5132
RH
4717 }
4718 if (mips_trap)
4719 {
67c0d1eb 4720 macro_build (NULL, "teq", "s,t,q", sreg, AT, 6);
252b5132
RH
4721 /* We want to close the noreorder block as soon as possible, so
4722 that later insns are available for delay slot filling. */
4723 --mips_opts.noreorder;
4724 }
4725 else
4726 {
4727 expr1.X_add_number = 8;
67c0d1eb
RS
4728 macro_build (&expr1, "bne", "s,t,p", sreg, AT);
4729 macro_build (NULL, "nop", "", 0);
252b5132
RH
4730
4731 /* We want to close the noreorder block as soon as possible, so
4732 that later insns are available for delay slot filling. */
4733 --mips_opts.noreorder;
4734
67c0d1eb 4735 macro_build (NULL, "break", "c", 6);
252b5132 4736 }
67c0d1eb 4737 macro_build (NULL, s, "d", dreg);
252b5132
RH
4738 break;
4739
4740 case M_DIV_3I:
4741 s = "div";
4742 s2 = "mflo";
4743 goto do_divi;
4744 case M_DIVU_3I:
4745 s = "divu";
4746 s2 = "mflo";
4747 goto do_divi;
4748 case M_REM_3I:
4749 s = "div";
4750 s2 = "mfhi";
4751 goto do_divi;
4752 case M_REMU_3I:
4753 s = "divu";
4754 s2 = "mfhi";
4755 goto do_divi;
4756 case M_DDIV_3I:
4757 dbl = 1;
4758 s = "ddiv";
4759 s2 = "mflo";
4760 goto do_divi;
4761 case M_DDIVU_3I:
4762 dbl = 1;
4763 s = "ddivu";
4764 s2 = "mflo";
4765 goto do_divi;
4766 case M_DREM_3I:
4767 dbl = 1;
4768 s = "ddiv";
4769 s2 = "mfhi";
4770 goto do_divi;
4771 case M_DREMU_3I:
4772 dbl = 1;
4773 s = "ddivu";
4774 s2 = "mfhi";
4775 do_divi:
4776 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4777 {
4778 as_warn (_("Divide by zero."));
4779 if (mips_trap)
67c0d1eb 4780 macro_build (NULL, "teq", "s,t,q", 0, 0, 7);
252b5132 4781 else
67c0d1eb 4782 macro_build (NULL, "break", "c", 7);
252b5132
RH
4783 return;
4784 }
4785 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4786 {
4787 if (strcmp (s2, "mflo") == 0)
67c0d1eb 4788 move_register (dreg, sreg);
252b5132 4789 else
67c0d1eb 4790 move_register (dreg, 0);
252b5132
RH
4791 return;
4792 }
4793 if (imm_expr.X_op == O_constant
4794 && imm_expr.X_add_number == -1
4795 && s[strlen (s) - 1] != 'u')
4796 {
4797 if (strcmp (s2, "mflo") == 0)
4798 {
67c0d1eb 4799 macro_build (NULL, dbl ? "dneg" : "neg", "d,w", dreg, sreg);
252b5132
RH
4800 }
4801 else
67c0d1eb 4802 move_register (dreg, 0);
252b5132
RH
4803 return;
4804 }
4805
67c0d1eb
RS
4806 load_register (AT, &imm_expr, dbl);
4807 macro_build (NULL, s, "z,s,t", sreg, AT);
4808 macro_build (NULL, s2, "d", dreg);
252b5132
RH
4809 break;
4810
4811 case M_DIVU_3:
4812 s = "divu";
4813 s2 = "mflo";
4814 goto do_divu3;
4815 case M_REMU_3:
4816 s = "divu";
4817 s2 = "mfhi";
4818 goto do_divu3;
4819 case M_DDIVU_3:
4820 s = "ddivu";
4821 s2 = "mflo";
4822 goto do_divu3;
4823 case M_DREMU_3:
4824 s = "ddivu";
4825 s2 = "mfhi";
4826 do_divu3:
b34976b6 4827 mips_emit_delays (TRUE);
252b5132
RH
4828 ++mips_opts.noreorder;
4829 mips_any_noreorder = 1;
4830 if (mips_trap)
4831 {
67c0d1eb
RS
4832 macro_build (NULL, "teq", "s,t,q", treg, 0, 7);
4833 macro_build (NULL, s, "z,s,t", sreg, treg);
252b5132
RH
4834 /* We want to close the noreorder block as soon as possible, so
4835 that later insns are available for delay slot filling. */
4836 --mips_opts.noreorder;
4837 }
4838 else
4839 {
4840 expr1.X_add_number = 8;
67c0d1eb
RS
4841 macro_build (&expr1, "bne", "s,t,p", treg, 0);
4842 macro_build (NULL, s, "z,s,t", sreg, treg);
252b5132
RH
4843
4844 /* We want to close the noreorder block as soon as possible, so
4845 that later insns are available for delay slot filling. */
4846 --mips_opts.noreorder;
67c0d1eb 4847 macro_build (NULL, "break", "c", 7);
252b5132 4848 }
67c0d1eb 4849 macro_build (NULL, s2, "d", dreg);
252b5132
RH
4850 return;
4851
1abe91b1
MR
4852 case M_DLCA_AB:
4853 dbl = 1;
4854 case M_LCA_AB:
4855 call = 1;
4856 goto do_la;
252b5132
RH
4857 case M_DLA_AB:
4858 dbl = 1;
4859 case M_LA_AB:
1abe91b1 4860 do_la:
252b5132
RH
4861 /* Load the address of a symbol into a register. If breg is not
4862 zero, we then add a base register to it. */
4863
3bec30a8
TS
4864 if (dbl && HAVE_32BIT_GPRS)
4865 as_warn (_("dla used to load 32-bit register"));
4866
c90bbe5b 4867 if (! dbl && HAVE_64BIT_OBJECTS)
3bec30a8
TS
4868 as_warn (_("la used to load 64-bit address"));
4869
0c11417f
MR
4870 if (offset_expr.X_op == O_constant
4871 && offset_expr.X_add_number >= -0x8000
4872 && offset_expr.X_add_number < 0x8000)
4873 {
67c0d1eb 4874 macro_build (&offset_expr,
b7c7d6c1 4875 (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
17a2f251 4876 "t,r,j", treg, sreg, BFD_RELOC_LO16);
0c11417f
MR
4877 return;
4878 }
4879
afdbd6d0
CD
4880 if (treg == breg)
4881 {
4882 tempreg = AT;
4883 used_at = 1;
4884 }
4885 else
4886 {
4887 tempreg = treg;
4888 used_at = 0;
4889 }
4890
252b5132
RH
4891 /* When generating embedded PIC code, we permit expressions of
4892 the form
afdbd6d0
CD
4893 la $treg,foo-bar
4894 la $treg,foo-bar($breg)
bb2d6cd7 4895 where bar is an address in the current section. These are used
252b5132
RH
4896 when getting the addresses of functions. We don't permit
4897 X_add_number to be non-zero, because if the symbol is
4898 external the relaxing code needs to know that any addend is
4899 purely the offset to X_op_symbol. */
4900 if (mips_pic == EMBEDDED_PIC
4901 && offset_expr.X_op == O_subtract
49309057 4902 && (symbol_constant_p (offset_expr.X_op_symbol)
bb2d6cd7 4903 ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
49309057
ILT
4904 : (symbol_equated_p (offset_expr.X_op_symbol)
4905 && (S_GET_SEGMENT
4906 (symbol_get_value_expression (offset_expr.X_op_symbol)
4907 ->X_add_symbol)
bb2d6cd7 4908 == now_seg)))
bb2d6cd7
GK
4909 && (offset_expr.X_add_number == 0
4910 || OUTPUT_FLAVOR == bfd_target_elf_flavour))
252b5132 4911 {
afdbd6d0
CD
4912 if (breg == 0)
4913 {
4914 tempreg = treg;
4915 used_at = 0;
67c0d1eb
RS
4916 macro_build (&offset_expr, "lui", "t,u",
4917 tempreg, BFD_RELOC_PCREL_HI16_S);
afdbd6d0
CD
4918 }
4919 else
4920 {
67c0d1eb
RS
4921 macro_build (&offset_expr, "lui", "t,u",
4922 tempreg, BFD_RELOC_PCREL_HI16_S);
4923 macro_build (NULL,
4d34fb5f 4924 (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu",
afdbd6d0
CD
4925 "d,v,t", tempreg, tempreg, breg);
4926 }
67c0d1eb 4927 macro_build (&offset_expr,
4d34fb5f 4928 (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
17a2f251 4929 "t,r,j", treg, tempreg, BFD_RELOC_PCREL_LO16);
afdbd6d0
CD
4930 if (! used_at)
4931 return;
4932 break;
252b5132
RH
4933 }
4934
4935 if (offset_expr.X_op != O_symbol
4936 && offset_expr.X_op != O_constant)
4937 {
4938 as_bad (_("expression too complex"));
4939 offset_expr.X_op = O_constant;
4940 }
4941
252b5132 4942 if (offset_expr.X_op == O_constant)
67c0d1eb 4943 load_register (tempreg, &offset_expr,
4d34fb5f
TS
4944 ((mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
4945 ? (dbl || HAVE_64BIT_ADDRESSES)
4946 : HAVE_64BIT_ADDRESSES));
252b5132
RH
4947 else if (mips_pic == NO_PIC)
4948 {
d6bc6245 4949 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 4950 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
4951 Otherwise we want
4952 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
4953 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4954 If we have a constant, we need two instructions anyhow,
d6bc6245 4955 so we may as well always use the latter form.
76b3015f 4956
d6bc6245
TS
4957 With 64bit address space and a usable $at we want
4958 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4959 lui $at,<sym> (BFD_RELOC_HI16_S)
4960 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4961 daddiu $at,<sym> (BFD_RELOC_LO16)
4962 dsll32 $tempreg,0
3a482fd5 4963 daddu $tempreg,$tempreg,$at
76b3015f 4964
c03099e6 4965 If $at is already in use, we use a path which is suboptimal
d6bc6245
TS
4966 on superscalar processors.
4967 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4968 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4969 dsll $tempreg,16
4970 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
4971 dsll $tempreg,16
4972 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
4973 */
d6bc6245 4974 if (HAVE_64BIT_ADDRESSES)
252b5132 4975 {
e864ceca
RS
4976 /* ??? We don't provide a GP-relative alternative for
4977 these macros. It used not to be possible with the
4978 original relaxation code, but it could be done now. */
d6bc6245 4979
460597ba 4980 if (used_at == 0 && ! mips_opts.noat)
98d3f06f 4981 {
67c0d1eb 4982 macro_build (&offset_expr, "lui", "t,u",
17a2f251 4983 tempreg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb 4984 macro_build (&offset_expr, "lui", "t,u",
17a2f251 4985 AT, BFD_RELOC_HI16_S);
67c0d1eb 4986 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 4987 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
67c0d1eb 4988 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 4989 AT, AT, BFD_RELOC_LO16);
67c0d1eb
RS
4990 macro_build (NULL, "dsll32", "d,w,<", tempreg, tempreg, 0);
4991 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
98d3f06f
KH
4992 used_at = 1;
4993 }
4994 else
4995 {
67c0d1eb 4996 macro_build (&offset_expr, "lui", "t,u",
17a2f251 4997 tempreg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb 4998 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 4999 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
67c0d1eb
RS
5000 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
5001 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 5002 tempreg, tempreg, BFD_RELOC_HI16_S);
67c0d1eb
RS
5003 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
5004 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 5005 tempreg, tempreg, BFD_RELOC_LO16);
98d3f06f
KH
5006 }
5007 }
5008 else
5009 {
5010 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
5011 && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
5012 {
4d7206a2 5013 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
5014 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5015 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 5016 relax_switch ();
98d3f06f 5017 }
67c0d1eb
RS
5018 macro_build_lui (&offset_expr, tempreg);
5019 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5020 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2
RS
5021 if (mips_relax.sequence)
5022 relax_end ();
98d3f06f 5023 }
252b5132 5024 }
f5040a92 5025 else if (mips_pic == SVR4_PIC && ! mips_big_got && ! HAVE_NEWABI)
252b5132 5026 {
9117d219
NC
5027 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5028
252b5132
RH
5029 /* If this is a reference to an external symbol, and there
5030 is no constant, we want
5031 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
1abe91b1 5032 or for lca or if tempreg is PIC_CALL_REG
9117d219 5033 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
252b5132
RH
5034 For a local symbol, we want
5035 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5036 nop
5037 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5038
5039 If we have a small constant, and this is a reference to
5040 an external symbol, we want
5041 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5042 nop
5043 addiu $tempreg,$tempreg,<constant>
5044 For a local symbol, we want the same instruction
5045 sequence, but we output a BFD_RELOC_LO16 reloc on the
5046 addiu instruction.
5047
5048 If we have a large constant, and this is a reference to
5049 an external symbol, we want
5050 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5051 lui $at,<hiconstant>
5052 addiu $at,$at,<loconstant>
5053 addu $tempreg,$tempreg,$at
5054 For a local symbol, we want the same instruction
5055 sequence, but we output a BFD_RELOC_LO16 reloc on the
ed6fb7bd 5056 addiu instruction.
ed6fb7bd
SC
5057 */
5058
4d7206a2 5059 if (offset_expr.X_add_number == 0)
252b5132 5060 {
4d7206a2
RS
5061 if (breg == 0 && (call || tempreg == PIC_CALL_REG))
5062 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
5063
5064 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
5065 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5066 lw_reloc_type, mips_gp_register);
4d7206a2 5067 if (breg != 0)
252b5132
RH
5068 {
5069 /* We're going to put in an addu instruction using
5070 tempreg, so we may as well insert the nop right
5071 now. */
67c0d1eb 5072 macro_build (NULL, "nop", "");
252b5132 5073 }
4d7206a2 5074 relax_switch ();
67c0d1eb
RS
5075 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5076 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
5077 macro_build (NULL, "nop", "");
5078 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5079 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2 5080 relax_end ();
252b5132
RH
5081 /* FIXME: If breg == 0, and the next instruction uses
5082 $tempreg, then if this variant case is used an extra
5083 nop will be generated. */
5084 }
4d7206a2
RS
5085 else if (offset_expr.X_add_number >= -0x8000
5086 && offset_expr.X_add_number < 0x8000)
252b5132 5087 {
67c0d1eb
RS
5088 load_got_offset (tempreg, &offset_expr);
5089 macro_build (NULL, "nop", "");
5090 add_got_offset (tempreg, &offset_expr);
252b5132
RH
5091 }
5092 else
5093 {
4d7206a2
RS
5094 expr1.X_add_number = offset_expr.X_add_number;
5095 offset_expr.X_add_number =
5096 ((offset_expr.X_add_number + 0x8000) & 0xffff) - 0x8000;
67c0d1eb 5097 load_got_offset (tempreg, &offset_expr);
f6a22291 5098 offset_expr.X_add_number = expr1.X_add_number;
252b5132
RH
5099 /* If we are going to add in a base register, and the
5100 target register and the base register are the same,
5101 then we are using AT as a temporary register. Since
5102 we want to load the constant into AT, we add our
5103 current AT (from the global offset table) and the
5104 register into the register now, and pretend we were
5105 not using a base register. */
67c0d1eb 5106 if (breg == treg)
252b5132 5107 {
67c0d1eb
RS
5108 macro_build (NULL, "nop", "");
5109 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5110 treg, AT, breg);
252b5132
RH
5111 breg = 0;
5112 tempreg = treg;
252b5132 5113 }
f6a22291 5114 add_got_offset_hilo (tempreg, &offset_expr, AT);
252b5132
RH
5115 used_at = 1;
5116 }
5117 }
f5040a92
AO
5118 else if (mips_pic == SVR4_PIC && ! mips_big_got && HAVE_NEWABI)
5119 {
67c0d1eb 5120 int add_breg_early = 0;
f5040a92
AO
5121
5122 /* If this is a reference to an external, and there is no
5123 constant, or local symbol (*), with or without a
5124 constant, we want
5125 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
1abe91b1 5126 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
5127 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5128
5129 If we have a small constant, and this is a reference to
5130 an external symbol, we want
5131 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5132 addiu $tempreg,$tempreg,<constant>
5133
5134 If we have a large constant, and this is a reference to
5135 an external symbol, we want
5136 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5137 lui $at,<hiconstant>
5138 addiu $at,$at,<loconstant>
5139 addu $tempreg,$tempreg,$at
5140
5141 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
5142 local symbols, even though it introduces an additional
5143 instruction. */
5144
f5040a92
AO
5145 if (offset_expr.X_add_number)
5146 {
4d7206a2 5147 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
5148 offset_expr.X_add_number = 0;
5149
4d7206a2 5150 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
5151 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5152 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
5153
5154 if (expr1.X_add_number >= -0x8000
5155 && expr1.X_add_number < 0x8000)
5156 {
67c0d1eb
RS
5157 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
5158 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 5159 }
ecd13cd3 5160 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92
AO
5161 {
5162 int dreg;
5163
5164 /* If we are going to add in a base register, and the
5165 target register and the base register are the same,
5166 then we are using AT as a temporary register. Since
5167 we want to load the constant into AT, we add our
5168 current AT (from the global offset table) and the
5169 register into the register now, and pretend we were
5170 not using a base register. */
5171 if (breg != treg)
5172 dreg = tempreg;
5173 else
5174 {
5175 assert (tempreg == AT);
67c0d1eb
RS
5176 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5177 treg, AT, breg);
f5040a92 5178 dreg = treg;
67c0d1eb 5179 add_breg_early = 1;
f5040a92
AO
5180 }
5181
f6a22291 5182 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 5183 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5184 dreg, dreg, AT);
f5040a92 5185
f5040a92
AO
5186 used_at = 1;
5187 }
5188 else
5189 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5190
4d7206a2 5191 relax_switch ();
f5040a92
AO
5192 offset_expr.X_add_number = expr1.X_add_number;
5193
67c0d1eb
RS
5194 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5195 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5196 if (add_breg_early)
f5040a92 5197 {
67c0d1eb 5198 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
f899b4b8 5199 treg, tempreg, breg);
f5040a92
AO
5200 breg = 0;
5201 tempreg = treg;
5202 }
4d7206a2 5203 relax_end ();
f5040a92 5204 }
4d7206a2 5205 else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
f5040a92 5206 {
4d7206a2 5207 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
5208 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5209 BFD_RELOC_MIPS_CALL16, mips_gp_register);
4d7206a2 5210 relax_switch ();
67c0d1eb
RS
5211 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5212 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2 5213 relax_end ();
f5040a92 5214 }
4d7206a2 5215 else
f5040a92 5216 {
67c0d1eb
RS
5217 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5218 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
5219 }
5220 }
5221 else if (mips_pic == SVR4_PIC && ! HAVE_NEWABI)
252b5132 5222 {
67c0d1eb 5223 int gpdelay;
9117d219
NC
5224 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5225 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
ed6fb7bd 5226 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
252b5132
RH
5227
5228 /* This is the large GOT case. If this is a reference to an
5229 external symbol, and there is no constant, we want
5230 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5231 addu $tempreg,$tempreg,$gp
5232 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 5233 or for lca or if tempreg is PIC_CALL_REG
9117d219
NC
5234 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5235 addu $tempreg,$tempreg,$gp
5236 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
252b5132
RH
5237 For a local symbol, we want
5238 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5239 nop
5240 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5241
5242 If we have a small constant, and this is a reference to
5243 an external symbol, we want
5244 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5245 addu $tempreg,$tempreg,$gp
5246 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5247 nop
5248 addiu $tempreg,$tempreg,<constant>
5249 For a local symbol, we want
5250 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5251 nop
5252 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
5253
5254 If we have a large constant, and this is a reference to
5255 an external symbol, we want
5256 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5257 addu $tempreg,$tempreg,$gp
5258 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5259 lui $at,<hiconstant>
5260 addiu $at,$at,<loconstant>
5261 addu $tempreg,$tempreg,$at
5262 For a local symbol, we want
5263 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5264 lui $at,<hiconstant>
5265 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
5266 addu $tempreg,$tempreg,$at
f5040a92 5267 */
438c16b8 5268
252b5132
RH
5269 expr1.X_add_number = offset_expr.X_add_number;
5270 offset_expr.X_add_number = 0;
4d7206a2 5271 relax_start (offset_expr.X_add_symbol);
67c0d1eb 5272 gpdelay = reg_needs_delay (mips_gp_register);
1abe91b1
MR
5273 if (expr1.X_add_number == 0 && breg == 0
5274 && (call || tempreg == PIC_CALL_REG))
9117d219
NC
5275 {
5276 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5277 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5278 }
67c0d1eb
RS
5279 macro_build (&offset_expr, "lui", "t,u", tempreg, lui_reloc_type);
5280 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5281 tempreg, tempreg, mips_gp_register);
67c0d1eb 5282 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 5283 tempreg, lw_reloc_type, tempreg);
252b5132
RH
5284 if (expr1.X_add_number == 0)
5285 {
67c0d1eb 5286 if (breg != 0)
252b5132
RH
5287 {
5288 /* We're going to put in an addu instruction using
5289 tempreg, so we may as well insert the nop right
5290 now. */
67c0d1eb 5291 macro_build (NULL, "nop", "");
252b5132 5292 }
252b5132
RH
5293 }
5294 else if (expr1.X_add_number >= -0x8000
5295 && expr1.X_add_number < 0x8000)
5296 {
67c0d1eb
RS
5297 macro_build (NULL, "nop", "");
5298 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 5299 tempreg, tempreg, BFD_RELOC_LO16);
252b5132
RH
5300 }
5301 else
5302 {
67c0d1eb 5303 int dreg;
252b5132
RH
5304
5305 /* If we are going to add in a base register, and the
5306 target register and the base register are the same,
5307 then we are using AT as a temporary register. Since
5308 we want to load the constant into AT, we add our
5309 current AT (from the global offset table) and the
5310 register into the register now, and pretend we were
5311 not using a base register. */
5312 if (breg != treg)
67c0d1eb 5313 dreg = tempreg;
252b5132
RH
5314 else
5315 {
5316 assert (tempreg == AT);
67c0d1eb
RS
5317 macro_build (NULL, "nop", "");
5318 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5319 treg, AT, breg);
252b5132 5320 dreg = treg;
252b5132
RH
5321 }
5322
f6a22291 5323 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 5324 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
252b5132 5325
252b5132
RH
5326 used_at = 1;
5327 }
4d7206a2
RS
5328 offset_expr.X_add_number =
5329 ((expr1.X_add_number + 0x8000) & 0xffff) - 0x8000;
5330 relax_switch ();
252b5132 5331
67c0d1eb 5332 if (gpdelay)
252b5132
RH
5333 {
5334 /* This is needed because this instruction uses $gp, but
f5040a92 5335 the first instruction on the main stream does not. */
67c0d1eb 5336 macro_build (NULL, "nop", "");
252b5132 5337 }
ed6fb7bd 5338
67c0d1eb
RS
5339 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5340 local_reloc_type, mips_gp_register);
f5040a92 5341 if (expr1.X_add_number >= -0x8000
252b5132
RH
5342 && expr1.X_add_number < 0x8000)
5343 {
67c0d1eb
RS
5344 macro_build (NULL, "nop", "");
5345 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5346 tempreg, tempreg, BFD_RELOC_LO16);
252b5132 5347 /* FIXME: If add_number is 0, and there was no base
f5040a92
AO
5348 register, the external symbol case ended with a load,
5349 so if the symbol turns out to not be external, and
5350 the next instruction uses tempreg, an unnecessary nop
5351 will be inserted. */
252b5132
RH
5352 }
5353 else
5354 {
5355 if (breg == treg)
5356 {
5357 /* We must add in the base register now, as in the
f5040a92 5358 external symbol case. */
252b5132 5359 assert (tempreg == AT);
67c0d1eb
RS
5360 macro_build (NULL, "nop", "");
5361 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5362 treg, AT, breg);
252b5132
RH
5363 tempreg = treg;
5364 /* We set breg to 0 because we have arranged to add
f5040a92 5365 it in in both cases. */
252b5132
RH
5366 breg = 0;
5367 }
5368
67c0d1eb
RS
5369 macro_build_lui (&expr1, AT);
5370 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 5371 AT, AT, BFD_RELOC_LO16);
67c0d1eb 5372 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5373 tempreg, tempreg, AT);
252b5132 5374 }
4d7206a2 5375 relax_end ();
252b5132 5376 }
f5040a92
AO
5377 else if (mips_pic == SVR4_PIC && HAVE_NEWABI)
5378 {
f5040a92
AO
5379 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5380 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
67c0d1eb 5381 int add_breg_early = 0;
f5040a92
AO
5382
5383 /* This is the large GOT case. If this is a reference to an
5384 external symbol, and there is no constant, we want
5385 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5386 add $tempreg,$tempreg,$gp
5387 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 5388 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
5389 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5390 add $tempreg,$tempreg,$gp
5391 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5392
5393 If we have a small constant, and this is a reference to
5394 an external symbol, we want
5395 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5396 add $tempreg,$tempreg,$gp
5397 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5398 addi $tempreg,$tempreg,<constant>
5399
5400 If we have a large constant, and this is a reference to
5401 an external symbol, we want
5402 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5403 addu $tempreg,$tempreg,$gp
5404 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5405 lui $at,<hiconstant>
5406 addi $at,$at,<loconstant>
5407 add $tempreg,$tempreg,$at
5408
5409 If we have NewABI, and we know it's a local symbol, we want
5410 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
5411 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
5412 otherwise we have to resort to GOT_HI16/GOT_LO16. */
5413
4d7206a2 5414 relax_start (offset_expr.X_add_symbol);
f5040a92 5415
4d7206a2 5416 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
5417 offset_expr.X_add_number = 0;
5418
1abe91b1
MR
5419 if (expr1.X_add_number == 0 && breg == 0
5420 && (call || tempreg == PIC_CALL_REG))
f5040a92
AO
5421 {
5422 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5423 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5424 }
67c0d1eb
RS
5425 macro_build (&offset_expr, "lui", "t,u", tempreg, lui_reloc_type);
5426 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5427 tempreg, tempreg, mips_gp_register);
67c0d1eb
RS
5428 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5429 tempreg, lw_reloc_type, tempreg);
f5040a92
AO
5430
5431 if (expr1.X_add_number == 0)
4d7206a2 5432 ;
f5040a92
AO
5433 else if (expr1.X_add_number >= -0x8000
5434 && expr1.X_add_number < 0x8000)
5435 {
67c0d1eb 5436 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 5437 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 5438 }
ecd13cd3 5439 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92
AO
5440 {
5441 int dreg;
5442
5443 /* If we are going to add in a base register, and the
5444 target register and the base register are the same,
5445 then we are using AT as a temporary register. Since
5446 we want to load the constant into AT, we add our
5447 current AT (from the global offset table) and the
5448 register into the register now, and pretend we were
5449 not using a base register. */
5450 if (breg != treg)
5451 dreg = tempreg;
5452 else
5453 {
5454 assert (tempreg == AT);
67c0d1eb 5455 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5456 treg, AT, breg);
f5040a92 5457 dreg = treg;
67c0d1eb 5458 add_breg_early = 1;
f5040a92
AO
5459 }
5460
f6a22291 5461 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 5462 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
f5040a92 5463
f5040a92
AO
5464 used_at = 1;
5465 }
5466 else
5467 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5468
4d7206a2 5469 relax_switch ();
f5040a92 5470 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
5471 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5472 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
5473 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
5474 tempreg, BFD_RELOC_MIPS_GOT_OFST);
5475 if (add_breg_early)
f5040a92 5476 {
67c0d1eb 5477 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5478 treg, tempreg, breg);
f5040a92
AO
5479 breg = 0;
5480 tempreg = treg;
5481 }
4d7206a2 5482 relax_end ();
f5040a92 5483 }
252b5132
RH
5484 else if (mips_pic == EMBEDDED_PIC)
5485 {
5486 /* We use
cdf6fd85 5487 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132 5488 */
67c0d1eb
RS
5489 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
5490 mips_gp_register, BFD_RELOC_GPREL16);
252b5132
RH
5491 }
5492 else
5493 abort ();
5494
5495 if (breg != 0)
4d34fb5f
TS
5496 {
5497 char *s;
5498
5499 if (mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
b7c7d6c1 5500 s = (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu";
4d34fb5f 5501 else
f899b4b8 5502 s = ADDRESS_ADD_INSN;
4d34fb5f 5503
67c0d1eb 5504 macro_build (NULL, s, "d,v,t", treg, tempreg, breg);
4d34fb5f 5505 }
252b5132
RH
5506
5507 if (! used_at)
5508 return;
5509
5510 break;
5511
5512 case M_J_A:
5513 /* The j instruction may not be used in PIC code, since it
5514 requires an absolute address. We convert it to a b
5515 instruction. */
5516 if (mips_pic == NO_PIC)
67c0d1eb 5517 macro_build (&offset_expr, "j", "a");
252b5132 5518 else
67c0d1eb 5519 macro_build (&offset_expr, "b", "p");
252b5132
RH
5520 return;
5521
5522 /* The jal instructions must be handled as macros because when
5523 generating PIC code they expand to multi-instruction
5524 sequences. Normally they are simple instructions. */
5525 case M_JAL_1:
5526 dreg = RA;
5527 /* Fall through. */
5528 case M_JAL_2:
5529 if (mips_pic == NO_PIC
5530 || mips_pic == EMBEDDED_PIC)
67c0d1eb 5531 macro_build (NULL, "jalr", "d,s", dreg, sreg);
252b5132
RH
5532 else if (mips_pic == SVR4_PIC)
5533 {
5534 if (sreg != PIC_CALL_REG)
5535 as_warn (_("MIPS PIC call to register other than $25"));
bdaaa2e1 5536
67c0d1eb 5537 macro_build (NULL, "jalr", "d,s", dreg, sreg);
6478892d 5538 if (! HAVE_NEWABI)
252b5132 5539 {
6478892d
TS
5540 if (mips_cprestore_offset < 0)
5541 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5542 else
5543 {
7a621144
DJ
5544 if (! mips_frame_reg_valid)
5545 {
5546 as_warn (_("No .frame pseudo-op used in PIC code"));
5547 /* Quiet this warning. */
5548 mips_frame_reg_valid = 1;
5549 }
5550 if (! mips_cprestore_valid)
5551 {
5552 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5553 /* Quiet this warning. */
5554 mips_cprestore_valid = 1;
5555 }
6478892d 5556 expr1.X_add_number = mips_cprestore_offset;
67c0d1eb 5557 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 5558 mips_gp_register,
256ab948
TS
5559 mips_frame_reg,
5560 HAVE_64BIT_ADDRESSES);
6478892d 5561 }
252b5132
RH
5562 }
5563 }
5564 else
5565 abort ();
5566
5567 return;
5568
5569 case M_JAL_A:
5570 if (mips_pic == NO_PIC)
67c0d1eb 5571 macro_build (&offset_expr, "jal", "a");
252b5132
RH
5572 else if (mips_pic == SVR4_PIC)
5573 {
5574 /* If this is a reference to an external symbol, and we are
5575 using a small GOT, we want
5576 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5577 nop
f9419b05 5578 jalr $ra,$25
252b5132
RH
5579 nop
5580 lw $gp,cprestore($sp)
5581 The cprestore value is set using the .cprestore
5582 pseudo-op. If we are using a big GOT, we want
5583 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5584 addu $25,$25,$gp
5585 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
5586 nop
f9419b05 5587 jalr $ra,$25
252b5132
RH
5588 nop
5589 lw $gp,cprestore($sp)
5590 If the symbol is not external, we want
5591 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5592 nop
5593 addiu $25,$25,<sym> (BFD_RELOC_LO16)
f9419b05 5594 jalr $ra,$25
252b5132 5595 nop
438c16b8 5596 lw $gp,cprestore($sp)
f5040a92
AO
5597
5598 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
5599 sequences above, minus nops, unless the symbol is local,
5600 which enables us to use GOT_PAGE/GOT_OFST (big got) or
5601 GOT_DISP. */
438c16b8 5602 if (HAVE_NEWABI)
252b5132 5603 {
f5040a92
AO
5604 if (! mips_big_got)
5605 {
4d7206a2 5606 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
5607 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5608 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
f5040a92 5609 mips_gp_register);
4d7206a2 5610 relax_switch ();
67c0d1eb
RS
5611 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5612 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
4d7206a2
RS
5613 mips_gp_register);
5614 relax_end ();
f5040a92
AO
5615 }
5616 else
5617 {
4d7206a2 5618 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
5619 macro_build (&offset_expr, "lui", "t,u", PIC_CALL_REG,
5620 BFD_RELOC_MIPS_CALL_HI16);
5621 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
5622 PIC_CALL_REG, mips_gp_register);
5623 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5624 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
5625 PIC_CALL_REG);
4d7206a2 5626 relax_switch ();
67c0d1eb
RS
5627 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5628 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
5629 mips_gp_register);
5630 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5631 PIC_CALL_REG, PIC_CALL_REG,
17a2f251 5632 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 5633 relax_end ();
f5040a92 5634 }
684022ea 5635
67c0d1eb 5636 macro_build_jalr (&offset_expr);
252b5132
RH
5637 }
5638 else
5639 {
4d7206a2 5640 relax_start (offset_expr.X_add_symbol);
438c16b8
TS
5641 if (! mips_big_got)
5642 {
67c0d1eb
RS
5643 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5644 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
17a2f251 5645 mips_gp_register);
67c0d1eb 5646 macro_build (NULL, "nop", "");
4d7206a2 5647 relax_switch ();
438c16b8 5648 }
252b5132 5649 else
252b5132 5650 {
67c0d1eb
RS
5651 int gpdelay;
5652
5653 gpdelay = reg_needs_delay (mips_gp_register);
5654 macro_build (&offset_expr, "lui", "t,u", PIC_CALL_REG,
5655 BFD_RELOC_MIPS_CALL_HI16);
5656 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
5657 PIC_CALL_REG, mips_gp_register);
5658 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5659 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
5660 PIC_CALL_REG);
5661 macro_build (NULL, "nop", "");
4d7206a2 5662 relax_switch ();
67c0d1eb
RS
5663 if (gpdelay)
5664 macro_build (NULL, "nop", "");
252b5132 5665 }
67c0d1eb
RS
5666 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5667 PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
4d7206a2 5668 mips_gp_register);
67c0d1eb
RS
5669 macro_build (NULL, "nop", "");
5670 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5671 PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
4d7206a2 5672 relax_end ();
67c0d1eb 5673 macro_build_jalr (&offset_expr);
438c16b8 5674
6478892d
TS
5675 if (mips_cprestore_offset < 0)
5676 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5677 else
5678 {
7a621144
DJ
5679 if (! mips_frame_reg_valid)
5680 {
5681 as_warn (_("No .frame pseudo-op used in PIC code"));
5682 /* Quiet this warning. */
5683 mips_frame_reg_valid = 1;
5684 }
5685 if (! mips_cprestore_valid)
5686 {
5687 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5688 /* Quiet this warning. */
5689 mips_cprestore_valid = 1;
5690 }
6478892d 5691 if (mips_opts.noreorder)
67c0d1eb 5692 macro_build (NULL, "nop", "");
6478892d 5693 expr1.X_add_number = mips_cprestore_offset;
67c0d1eb 5694 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 5695 mips_gp_register,
256ab948
TS
5696 mips_frame_reg,
5697 HAVE_64BIT_ADDRESSES);
6478892d 5698 }
252b5132
RH
5699 }
5700 }
5701 else if (mips_pic == EMBEDDED_PIC)
5702 {
67c0d1eb 5703 macro_build (&offset_expr, "bal", "p");
252b5132
RH
5704 /* The linker may expand the call to a longer sequence which
5705 uses $at, so we must break rather than return. */
5706 break;
5707 }
5708 else
5709 abort ();
5710
5711 return;
5712
5713 case M_LB_AB:
5714 s = "lb";
5715 goto ld;
5716 case M_LBU_AB:
5717 s = "lbu";
5718 goto ld;
5719 case M_LH_AB:
5720 s = "lh";
5721 goto ld;
5722 case M_LHU_AB:
5723 s = "lhu";
5724 goto ld;
5725 case M_LW_AB:
5726 s = "lw";
5727 goto ld;
5728 case M_LWC0_AB:
5729 s = "lwc0";
bdaaa2e1 5730 /* Itbl support may require additional care here. */
252b5132
RH
5731 coproc = 1;
5732 goto ld;
5733 case M_LWC1_AB:
5734 s = "lwc1";
bdaaa2e1 5735 /* Itbl support may require additional care here. */
252b5132
RH
5736 coproc = 1;
5737 goto ld;
5738 case M_LWC2_AB:
5739 s = "lwc2";
bdaaa2e1 5740 /* Itbl support may require additional care here. */
252b5132
RH
5741 coproc = 1;
5742 goto ld;
5743 case M_LWC3_AB:
5744 s = "lwc3";
bdaaa2e1 5745 /* Itbl support may require additional care here. */
252b5132
RH
5746 coproc = 1;
5747 goto ld;
5748 case M_LWL_AB:
5749 s = "lwl";
5750 lr = 1;
5751 goto ld;
5752 case M_LWR_AB:
5753 s = "lwr";
5754 lr = 1;
5755 goto ld;
5756 case M_LDC1_AB:
fef14a42 5757 if (mips_opts.arch == CPU_R4650)
252b5132
RH
5758 {
5759 as_bad (_("opcode not supported on this processor"));
5760 return;
5761 }
5762 s = "ldc1";
bdaaa2e1 5763 /* Itbl support may require additional care here. */
252b5132
RH
5764 coproc = 1;
5765 goto ld;
5766 case M_LDC2_AB:
5767 s = "ldc2";
bdaaa2e1 5768 /* Itbl support may require additional care here. */
252b5132
RH
5769 coproc = 1;
5770 goto ld;
5771 case M_LDC3_AB:
5772 s = "ldc3";
bdaaa2e1 5773 /* Itbl support may require additional care here. */
252b5132
RH
5774 coproc = 1;
5775 goto ld;
5776 case M_LDL_AB:
5777 s = "ldl";
5778 lr = 1;
5779 goto ld;
5780 case M_LDR_AB:
5781 s = "ldr";
5782 lr = 1;
5783 goto ld;
5784 case M_LL_AB:
5785 s = "ll";
5786 goto ld;
5787 case M_LLD_AB:
5788 s = "lld";
5789 goto ld;
5790 case M_LWU_AB:
5791 s = "lwu";
5792 ld:
5793 if (breg == treg || coproc || lr)
5794 {
5795 tempreg = AT;
5796 used_at = 1;
5797 }
5798 else
5799 {
5800 tempreg = treg;
5801 used_at = 0;
5802 }
5803 goto ld_st;
5804 case M_SB_AB:
5805 s = "sb";
5806 goto st;
5807 case M_SH_AB:
5808 s = "sh";
5809 goto st;
5810 case M_SW_AB:
5811 s = "sw";
5812 goto st;
5813 case M_SWC0_AB:
5814 s = "swc0";
bdaaa2e1 5815 /* Itbl support may require additional care here. */
252b5132
RH
5816 coproc = 1;
5817 goto st;
5818 case M_SWC1_AB:
5819 s = "swc1";
bdaaa2e1 5820 /* Itbl support may require additional care here. */
252b5132
RH
5821 coproc = 1;
5822 goto st;
5823 case M_SWC2_AB:
5824 s = "swc2";
bdaaa2e1 5825 /* Itbl support may require additional care here. */
252b5132
RH
5826 coproc = 1;
5827 goto st;
5828 case M_SWC3_AB:
5829 s = "swc3";
bdaaa2e1 5830 /* Itbl support may require additional care here. */
252b5132
RH
5831 coproc = 1;
5832 goto st;
5833 case M_SWL_AB:
5834 s = "swl";
5835 goto st;
5836 case M_SWR_AB:
5837 s = "swr";
5838 goto st;
5839 case M_SC_AB:
5840 s = "sc";
5841 goto st;
5842 case M_SCD_AB:
5843 s = "scd";
5844 goto st;
5845 case M_SDC1_AB:
fef14a42 5846 if (mips_opts.arch == CPU_R4650)
252b5132
RH
5847 {
5848 as_bad (_("opcode not supported on this processor"));
5849 return;
5850 }
5851 s = "sdc1";
5852 coproc = 1;
bdaaa2e1 5853 /* Itbl support may require additional care here. */
252b5132
RH
5854 goto st;
5855 case M_SDC2_AB:
5856 s = "sdc2";
bdaaa2e1 5857 /* Itbl support may require additional care here. */
252b5132
RH
5858 coproc = 1;
5859 goto st;
5860 case M_SDC3_AB:
5861 s = "sdc3";
bdaaa2e1 5862 /* Itbl support may require additional care here. */
252b5132
RH
5863 coproc = 1;
5864 goto st;
5865 case M_SDL_AB:
5866 s = "sdl";
5867 goto st;
5868 case M_SDR_AB:
5869 s = "sdr";
5870 st:
5871 tempreg = AT;
5872 used_at = 1;
5873 ld_st:
bdaaa2e1 5874 /* Itbl support may require additional care here. */
252b5132
RH
5875 if (mask == M_LWC1_AB
5876 || mask == M_SWC1_AB
5877 || mask == M_LDC1_AB
5878 || mask == M_SDC1_AB
5879 || mask == M_L_DAB
5880 || mask == M_S_DAB)
5881 fmt = "T,o(b)";
5882 else if (coproc)
5883 fmt = "E,o(b)";
5884 else
5885 fmt = "t,o(b)";
5886
256ab948
TS
5887 /* Sign-extending 32-bit constants makes their handling easier.
5888 The HAVE_64BIT_GPRS... part is due to the linux kernel hack
5889 described below. */
5890 if ((! HAVE_64BIT_ADDRESSES
5891 && (! HAVE_64BIT_GPRS && offset_expr.X_op == O_constant))
d17e7bce
TS
5892 && (offset_expr.X_op == O_constant)
5893 && ! ((offset_expr.X_add_number & ~((bfd_vma) 0x7fffffff))
5894 == ~((bfd_vma) 0x7fffffff)))
ae826530 5895 {
d17e7bce 5896 if (offset_expr.X_add_number & ~((bfd_vma) 0xffffffff))
1b8e29e5 5897 as_bad (_("constant too large"));
ae826530 5898
1b8e29e5
TS
5899 offset_expr.X_add_number = (((offset_expr.X_add_number & 0xffffffff)
5900 ^ 0x80000000) - 0x80000000);
ae826530 5901 }
256ab948 5902
afdbd6d0
CD
5903 /* For embedded PIC, we allow loads where the offset is calculated
5904 by subtracting a symbol in the current segment from an unknown
5905 symbol, relative to a base register, e.g.:
5906 <op> $treg, <sym>-<localsym>($breg)
5907 This is used by the compiler for switch statements. */
76b3015f 5908 if (mips_pic == EMBEDDED_PIC
afdbd6d0
CD
5909 && offset_expr.X_op == O_subtract
5910 && (symbol_constant_p (offset_expr.X_op_symbol)
5911 ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
5912 : (symbol_equated_p (offset_expr.X_op_symbol)
5913 && (S_GET_SEGMENT
5914 (symbol_get_value_expression (offset_expr.X_op_symbol)
5915 ->X_add_symbol)
5916 == now_seg)))
5917 && breg != 0
5918 && (offset_expr.X_add_number == 0
5919 || OUTPUT_FLAVOR == bfd_target_elf_flavour))
5920 {
5921 /* For this case, we output the instructions:
5922 lui $tempreg,<sym> (BFD_RELOC_PCREL_HI16_S)
5923 addiu $tempreg,$tempreg,$breg
5924 <op> $treg,<sym>($tempreg) (BFD_RELOC_PCREL_LO16)
5925 If the relocation would fit entirely in 16 bits, it would be
5926 nice to emit:
5927 <op> $treg,<sym>($breg) (BFD_RELOC_PCREL_LO16)
5928 instead, but that seems quite difficult. */
67c0d1eb 5929 macro_build (&offset_expr, "lui", "t,u", tempreg,
17a2f251 5930 BFD_RELOC_PCREL_HI16_S);
67c0d1eb
RS
5931 macro_build (NULL,
5932 ((bfd_arch_bits_per_address (stdoutput) == 32
5933 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
5934 ? "addu" : "daddu"),
5935 "d,v,t", tempreg, tempreg, breg);
5936 macro_build (&offset_expr, s, fmt, treg,
5937 BFD_RELOC_PCREL_LO16, tempreg);
afdbd6d0
CD
5938 if (! used_at)
5939 return;
5940 break;
5941 }
5942
252b5132
RH
5943 if (offset_expr.X_op != O_constant
5944 && offset_expr.X_op != O_symbol)
5945 {
5946 as_bad (_("expression too complex"));
5947 offset_expr.X_op = O_constant;
5948 }
5949
5950 /* A constant expression in PIC code can be handled just as it
5951 is in non PIC code. */
5952 if (mips_pic == NO_PIC
5953 || offset_expr.X_op == O_constant)
5954 {
5955 /* If this is a reference to a GP relative symbol, and there
5956 is no base register, we want
cdf6fd85 5957 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
5958 Otherwise, if there is no base register, we want
5959 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5960 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5961 If we have a constant, we need two instructions anyhow,
5962 so we always use the latter form.
5963
5964 If we have a base register, and this is a reference to a
5965 GP relative symbol, we want
5966 addu $tempreg,$breg,$gp
cdf6fd85 5967 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
5968 Otherwise we want
5969 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5970 addu $tempreg,$tempreg,$breg
5971 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245 5972 With a constant we always use the latter case.
76b3015f 5973
d6bc6245
TS
5974 With 64bit address space and no base register and $at usable,
5975 we want
5976 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5977 lui $at,<sym> (BFD_RELOC_HI16_S)
5978 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5979 dsll32 $tempreg,0
5980 daddu $tempreg,$at
5981 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5982 If we have a base register, we want
5983 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5984 lui $at,<sym> (BFD_RELOC_HI16_S)
5985 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5986 daddu $at,$breg
5987 dsll32 $tempreg,0
5988 daddu $tempreg,$at
5989 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5990
5991 Without $at we can't generate the optimal path for superscalar
5992 processors here since this would require two temporary registers.
5993 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5994 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5995 dsll $tempreg,16
5996 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5997 dsll $tempreg,16
5998 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5999 If we have a base register, we want
6000 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
6001 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
6002 dsll $tempreg,16
6003 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
6004 dsll $tempreg,16
6005 daddu $tempreg,$tempreg,$breg
6006 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6373ee54
CD
6007
6008 If we have 64-bit addresses, as an optimization, for
6009 addresses which are 32-bit constants (e.g. kseg0/kseg1
6010 addresses) we fall back to the 32-bit address generation
78d32a17
MR
6011 mechanism since it is more efficient. Note that due to
6012 the signed offset used by memory operations, the 32-bit
6013 range is shifted down by 32768 here. This code should
6373ee54
CD
6014 probably attempt to generate 64-bit constants more
6015 efficiently in general.
9214dd3b
AO
6016
6017 As an extension for architectures with 64-bit registers,
6018 we don't truncate 64-bit addresses given as literal
6019 constants down to 32 bits, to support existing practice
6020 in the mips64 Linux (the kernel), that compiles source
6021 files with -mabi=64, assembling them as o32 or n32 (with
6022 -Wa,-32 or -Wa,-n32). This is not beautiful, but since
6023 the whole kernel is loaded into a memory region that is
67c1ffbe 6024 addressable with sign-extended 32-bit addresses, it is
9214dd3b
AO
6025 wasteful to compute the upper 32 bits of every
6026 non-literal address, that takes more space and time.
6027 Some day this should probably be implemented as an
6028 assembler option, such that the kernel doesn't have to
6029 use such ugly hacks, even though it will still have to
6030 end up converting the binary to ELF32 for a number of
6031 platforms whose boot loaders don't support ELF64
6032 binaries. */
256ab948
TS
6033 if ((HAVE_64BIT_ADDRESSES
6034 && ! (offset_expr.X_op == O_constant
6035 && IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000)))
6036 || (HAVE_64BIT_GPRS
6037 && offset_expr.X_op == O_constant
6038 && ! IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000)))
d6bc6245 6039 {
e864ceca
RS
6040 /* ??? We don't provide a GP-relative alternative for
6041 these macros. It used not to be possible with the
6042 original relaxation code, but it could be done now. */
d6bc6245 6043
460597ba 6044 if (used_at == 0 && ! mips_opts.noat)
d6bc6245 6045 {
67c0d1eb
RS
6046 macro_build (&offset_expr, "lui", "t,u", tempreg,
6047 BFD_RELOC_MIPS_HIGHEST);
6048 macro_build (&offset_expr, "lui", "t,u", AT,
6049 BFD_RELOC_HI16_S);
6050 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
6051 tempreg, BFD_RELOC_MIPS_HIGHER);
d6bc6245 6052 if (breg != 0)
67c0d1eb
RS
6053 macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
6054 macro_build (NULL, "dsll32", "d,w,<", tempreg, tempreg, 0);
6055 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
6056 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16,
6057 tempreg);
d6bc6245
TS
6058 used_at = 1;
6059 }
6060 else
6061 {
67c0d1eb
RS
6062 macro_build (&offset_expr, "lui", "t,u", tempreg,
6063 BFD_RELOC_MIPS_HIGHEST);
6064 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
6065 tempreg, BFD_RELOC_MIPS_HIGHER);
6066 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
6067 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
6068 tempreg, BFD_RELOC_HI16_S);
6069 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
d6bc6245 6070 if (breg != 0)
67c0d1eb 6071 macro_build (NULL, "daddu", "d,v,t",
17a2f251 6072 tempreg, tempreg, breg);
67c0d1eb 6073 macro_build (&offset_expr, s, fmt, treg,
17a2f251 6074 BFD_RELOC_LO16, tempreg);
d6bc6245
TS
6075 }
6076
6077 return;
6078 }
256ab948
TS
6079
6080 if (offset_expr.X_op == O_constant
ecd13cd3 6081 && ! IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000))
5a7a0b7b 6082 as_bad (_("load/store address overflow (max 32 bits)"));
76b3015f 6083
252b5132
RH
6084 if (breg == 0)
6085 {
67c0d1eb
RS
6086 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6087 && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 6088 {
4d7206a2 6089 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
6090 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_GPREL16,
6091 mips_gp_register);
4d7206a2 6092 relax_switch ();
252b5132
RH
6093 used_at = 0;
6094 }
67c0d1eb
RS
6095 macro_build_lui (&offset_expr, tempreg);
6096 macro_build (&offset_expr, s, fmt, treg,
17a2f251 6097 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
6098 if (mips_relax.sequence)
6099 relax_end ();
252b5132
RH
6100 }
6101 else
6102 {
67c0d1eb
RS
6103 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6104 && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 6105 {
4d7206a2 6106 relax_start (offset_expr.X_add_symbol);
67c0d1eb 6107 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6108 tempreg, breg, mips_gp_register);
67c0d1eb 6109 macro_build (&offset_expr, s, fmt, treg,
17a2f251 6110 BFD_RELOC_GPREL16, tempreg);
4d7206a2 6111 relax_switch ();
252b5132 6112 }
67c0d1eb
RS
6113 macro_build_lui (&offset_expr, tempreg);
6114 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6115 tempreg, tempreg, breg);
67c0d1eb 6116 macro_build (&offset_expr, s, fmt, treg,
17a2f251 6117 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
6118 if (mips_relax.sequence)
6119 relax_end ();
252b5132
RH
6120 }
6121 }
6122 else if (mips_pic == SVR4_PIC && ! mips_big_got)
6123 {
ed6fb7bd 6124 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
f9419b05 6125
252b5132
RH
6126 /* If this is a reference to an external symbol, we want
6127 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6128 nop
6129 <op> $treg,0($tempreg)
6130 Otherwise we want
6131 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6132 nop
6133 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6134 <op> $treg,0($tempreg)
f5040a92
AO
6135
6136 For NewABI, we want
6137 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6138 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
6139
252b5132
RH
6140 If there is a base register, we add it to $tempreg before
6141 the <op>. If there is a constant, we stick it in the
6142 <op> instruction. We don't handle constants larger than
6143 16 bits, because we have no way to load the upper 16 bits
6144 (actually, we could handle them for the subset of cases
6145 in which we are not using $at). */
6146 assert (offset_expr.X_op == O_symbol);
f5040a92
AO
6147 if (HAVE_NEWABI)
6148 {
67c0d1eb
RS
6149 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6150 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 6151 if (breg != 0)
67c0d1eb 6152 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6153 tempreg, tempreg, breg);
67c0d1eb 6154 macro_build (&offset_expr, s, fmt, treg,
17a2f251 6155 BFD_RELOC_MIPS_GOT_OFST, tempreg);
f5040a92
AO
6156
6157 if (! used_at)
6158 return;
6159
6160 break;
6161 }
252b5132
RH
6162 expr1.X_add_number = offset_expr.X_add_number;
6163 offset_expr.X_add_number = 0;
6164 if (expr1.X_add_number < -0x8000
6165 || expr1.X_add_number >= 0x8000)
6166 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb
RS
6167 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6168 lw_reloc_type, mips_gp_register);
6169 macro_build (NULL, "nop", "");
4d7206a2
RS
6170 relax_start (offset_expr.X_add_symbol);
6171 relax_switch ();
67c0d1eb
RS
6172 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
6173 tempreg, BFD_RELOC_LO16);
4d7206a2 6174 relax_end ();
252b5132 6175 if (breg != 0)
67c0d1eb 6176 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6177 tempreg, tempreg, breg);
67c0d1eb 6178 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
252b5132 6179 }
f5040a92 6180 else if (mips_pic == SVR4_PIC && ! HAVE_NEWABI)
252b5132 6181 {
67c0d1eb 6182 int gpdelay;
252b5132
RH
6183
6184 /* If this is a reference to an external symbol, we want
6185 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6186 addu $tempreg,$tempreg,$gp
6187 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6188 <op> $treg,0($tempreg)
6189 Otherwise we want
6190 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6191 nop
6192 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6193 <op> $treg,0($tempreg)
6194 If there is a base register, we add it to $tempreg before
6195 the <op>. If there is a constant, we stick it in the
6196 <op> instruction. We don't handle constants larger than
6197 16 bits, because we have no way to load the upper 16 bits
6198 (actually, we could handle them for the subset of cases
f5040a92 6199 in which we are not using $at). */
252b5132
RH
6200 assert (offset_expr.X_op == O_symbol);
6201 expr1.X_add_number = offset_expr.X_add_number;
6202 offset_expr.X_add_number = 0;
6203 if (expr1.X_add_number < -0x8000
6204 || expr1.X_add_number >= 0x8000)
6205 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 6206 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 6207 relax_start (offset_expr.X_add_symbol);
67c0d1eb 6208 macro_build (&offset_expr, "lui", "t,u", tempreg,
17a2f251 6209 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
6210 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
6211 mips_gp_register);
6212 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6213 BFD_RELOC_MIPS_GOT_LO16, tempreg);
4d7206a2 6214 relax_switch ();
67c0d1eb
RS
6215 if (gpdelay)
6216 macro_build (NULL, "nop", "");
6217 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6218 BFD_RELOC_MIPS_GOT16, mips_gp_register);
6219 macro_build (NULL, "nop", "");
6220 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
6221 tempreg, BFD_RELOC_LO16);
4d7206a2
RS
6222 relax_end ();
6223
252b5132 6224 if (breg != 0)
67c0d1eb 6225 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6226 tempreg, tempreg, breg);
67c0d1eb 6227 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
252b5132 6228 }
f5040a92
AO
6229 else if (mips_pic == SVR4_PIC && HAVE_NEWABI)
6230 {
f5040a92
AO
6231 /* If this is a reference to an external symbol, we want
6232 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6233 add $tempreg,$tempreg,$gp
6234 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6235 <op> $treg,<ofst>($tempreg)
6236 Otherwise, for local symbols, we want:
6237 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6238 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
6239 assert (offset_expr.X_op == O_symbol);
4d7206a2 6240 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
6241 offset_expr.X_add_number = 0;
6242 if (expr1.X_add_number < -0x8000
6243 || expr1.X_add_number >= 0x8000)
6244 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4d7206a2 6245 relax_start (offset_expr.X_add_symbol);
67c0d1eb 6246 macro_build (&offset_expr, "lui", "t,u", tempreg,
17a2f251 6247 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
6248 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
6249 mips_gp_register);
6250 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6251 BFD_RELOC_MIPS_GOT_LO16, tempreg);
f5040a92 6252 if (breg != 0)
67c0d1eb 6253 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6254 tempreg, tempreg, breg);
67c0d1eb 6255 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
684022ea 6256
4d7206a2 6257 relax_switch ();
f5040a92 6258 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
6259 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6260 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 6261 if (breg != 0)
67c0d1eb 6262 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6263 tempreg, tempreg, breg);
67c0d1eb 6264 macro_build (&offset_expr, s, fmt, treg,
17a2f251 6265 BFD_RELOC_MIPS_GOT_OFST, tempreg);
4d7206a2 6266 relax_end ();
f5040a92 6267 }
252b5132
RH
6268 else if (mips_pic == EMBEDDED_PIC)
6269 {
6270 /* If there is no base register, we want
cdf6fd85 6271 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
6272 If there is a base register, we want
6273 addu $tempreg,$breg,$gp
cdf6fd85 6274 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
6275 */
6276 assert (offset_expr.X_op == O_symbol);
6277 if (breg == 0)
6278 {
67c0d1eb
RS
6279 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_GPREL16,
6280 mips_gp_register);
252b5132
RH
6281 used_at = 0;
6282 }
6283 else
6284 {
67c0d1eb 6285 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6286 tempreg, breg, mips_gp_register);
67c0d1eb 6287 macro_build (&offset_expr, s, fmt, treg,
17a2f251 6288 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6289 }
6290 }
6291 else
6292 abort ();
6293
6294 if (! used_at)
6295 return;
6296
6297 break;
6298
6299 case M_LI:
6300 case M_LI_S:
67c0d1eb 6301 load_register (treg, &imm_expr, 0);
252b5132
RH
6302 return;
6303
6304 case M_DLI:
67c0d1eb 6305 load_register (treg, &imm_expr, 1);
252b5132
RH
6306 return;
6307
6308 case M_LI_SS:
6309 if (imm_expr.X_op == O_constant)
6310 {
67c0d1eb
RS
6311 load_register (AT, &imm_expr, 0);
6312 macro_build (NULL, "mtc1", "t,G", AT, treg);
252b5132
RH
6313 break;
6314 }
6315 else
6316 {
6317 assert (offset_expr.X_op == O_symbol
6318 && strcmp (segment_name (S_GET_SEGMENT
6319 (offset_expr.X_add_symbol)),
6320 ".lit4") == 0
6321 && offset_expr.X_add_number == 0);
67c0d1eb 6322 macro_build (&offset_expr, "lwc1", "T,o(b)", treg,
17a2f251 6323 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
252b5132
RH
6324 return;
6325 }
6326
6327 case M_LI_D:
ca4e0257
RS
6328 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
6329 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
6330 order 32 bits of the value and the low order 32 bits are either
6331 zero or in OFFSET_EXPR. */
252b5132
RH
6332 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6333 {
ca4e0257 6334 if (HAVE_64BIT_GPRS)
67c0d1eb 6335 load_register (treg, &imm_expr, 1);
252b5132
RH
6336 else
6337 {
6338 int hreg, lreg;
6339
6340 if (target_big_endian)
6341 {
6342 hreg = treg;
6343 lreg = treg + 1;
6344 }
6345 else
6346 {
6347 hreg = treg + 1;
6348 lreg = treg;
6349 }
6350
6351 if (hreg <= 31)
67c0d1eb 6352 load_register (hreg, &imm_expr, 0);
252b5132
RH
6353 if (lreg <= 31)
6354 {
6355 if (offset_expr.X_op == O_absent)
67c0d1eb 6356 move_register (lreg, 0);
252b5132
RH
6357 else
6358 {
6359 assert (offset_expr.X_op == O_constant);
67c0d1eb 6360 load_register (lreg, &offset_expr, 0);
252b5132
RH
6361 }
6362 }
6363 }
6364 return;
6365 }
6366
6367 /* We know that sym is in the .rdata section. First we get the
6368 upper 16 bits of the address. */
6369 if (mips_pic == NO_PIC)
6370 {
67c0d1eb 6371 macro_build_lui (&offset_expr, AT);
252b5132
RH
6372 }
6373 else if (mips_pic == SVR4_PIC)
6374 {
67c0d1eb
RS
6375 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6376 BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132
RH
6377 }
6378 else if (mips_pic == EMBEDDED_PIC)
6379 {
6380 /* For embedded PIC we pick up the entire address off $gp in
6381 a single instruction. */
67c0d1eb
RS
6382 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", AT,
6383 mips_gp_register, BFD_RELOC_GPREL16);
252b5132
RH
6384 offset_expr.X_op = O_constant;
6385 offset_expr.X_add_number = 0;
6386 }
6387 else
6388 abort ();
bdaaa2e1 6389
252b5132 6390 /* Now we load the register(s). */
ca4e0257 6391 if (HAVE_64BIT_GPRS)
67c0d1eb 6392 macro_build (&offset_expr, "ld", "t,o(b)", treg, BFD_RELOC_LO16, AT);
252b5132
RH
6393 else
6394 {
67c0d1eb 6395 macro_build (&offset_expr, "lw", "t,o(b)", treg, BFD_RELOC_LO16, AT);
f9419b05 6396 if (treg != RA)
252b5132
RH
6397 {
6398 /* FIXME: How in the world do we deal with the possible
6399 overflow here? */
6400 offset_expr.X_add_number += 4;
67c0d1eb 6401 macro_build (&offset_expr, "lw", "t,o(b)",
17a2f251 6402 treg + 1, BFD_RELOC_LO16, AT);
252b5132
RH
6403 }
6404 }
252b5132
RH
6405 break;
6406
6407 case M_LI_DD:
ca4e0257
RS
6408 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
6409 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
6410 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
6411 the value and the low order 32 bits are either zero or in
6412 OFFSET_EXPR. */
252b5132
RH
6413 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6414 {
67c0d1eb 6415 load_register (AT, &imm_expr, HAVE_64BIT_FPRS);
ca4e0257
RS
6416 if (HAVE_64BIT_FPRS)
6417 {
6418 assert (HAVE_64BIT_GPRS);
67c0d1eb 6419 macro_build (NULL, "dmtc1", "t,S", AT, treg);
ca4e0257 6420 }
252b5132
RH
6421 else
6422 {
67c0d1eb 6423 macro_build (NULL, "mtc1", "t,G", AT, treg + 1);
252b5132 6424 if (offset_expr.X_op == O_absent)
67c0d1eb 6425 macro_build (NULL, "mtc1", "t,G", 0, treg);
252b5132
RH
6426 else
6427 {
6428 assert (offset_expr.X_op == O_constant);
67c0d1eb
RS
6429 load_register (AT, &offset_expr, 0);
6430 macro_build (NULL, "mtc1", "t,G", AT, treg);
252b5132
RH
6431 }
6432 }
6433 break;
6434 }
6435
6436 assert (offset_expr.X_op == O_symbol
6437 && offset_expr.X_add_number == 0);
6438 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
6439 if (strcmp (s, ".lit8") == 0)
6440 {
e7af610e 6441 if (mips_opts.isa != ISA_MIPS1)
252b5132 6442 {
67c0d1eb 6443 macro_build (&offset_expr, "ldc1", "T,o(b)", treg,
17a2f251 6444 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
252b5132
RH
6445 return;
6446 }
c9914766 6447 breg = mips_gp_register;
252b5132
RH
6448 r = BFD_RELOC_MIPS_LITERAL;
6449 goto dob;
6450 }
6451 else
6452 {
6453 assert (strcmp (s, RDATA_SECTION_NAME) == 0);
6454 if (mips_pic == SVR4_PIC)
67c0d1eb
RS
6455 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6456 BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132
RH
6457 else
6458 {
6459 /* FIXME: This won't work for a 64 bit address. */
67c0d1eb 6460 macro_build_lui (&offset_expr, AT);
252b5132 6461 }
bdaaa2e1 6462
e7af610e 6463 if (mips_opts.isa != ISA_MIPS1)
252b5132 6464 {
67c0d1eb
RS
6465 macro_build (&offset_expr, "ldc1", "T,o(b)",
6466 treg, BFD_RELOC_LO16, AT);
252b5132
RH
6467 break;
6468 }
6469 breg = AT;
6470 r = BFD_RELOC_LO16;
6471 goto dob;
6472 }
6473
6474 case M_L_DOB:
fef14a42 6475 if (mips_opts.arch == CPU_R4650)
252b5132
RH
6476 {
6477 as_bad (_("opcode not supported on this processor"));
6478 return;
6479 }
6480 /* Even on a big endian machine $fn comes before $fn+1. We have
6481 to adjust when loading from memory. */
6482 r = BFD_RELOC_LO16;
6483 dob:
e7af610e 6484 assert (mips_opts.isa == ISA_MIPS1);
67c0d1eb 6485 macro_build (&offset_expr, "lwc1", "T,o(b)",
17a2f251 6486 target_big_endian ? treg + 1 : treg, r, breg);
252b5132
RH
6487 /* FIXME: A possible overflow which I don't know how to deal
6488 with. */
6489 offset_expr.X_add_number += 4;
67c0d1eb 6490 macro_build (&offset_expr, "lwc1", "T,o(b)",
17a2f251 6491 target_big_endian ? treg : treg + 1, r, breg);
252b5132 6492
252b5132
RH
6493 if (breg != AT)
6494 return;
6495 break;
6496
6497 case M_L_DAB:
6498 /*
6499 * The MIPS assembler seems to check for X_add_number not
6500 * being double aligned and generating:
6501 * lui at,%hi(foo+1)
6502 * addu at,at,v1
6503 * addiu at,at,%lo(foo+1)
6504 * lwc1 f2,0(at)
6505 * lwc1 f3,4(at)
6506 * But, the resulting address is the same after relocation so why
6507 * generate the extra instruction?
6508 */
fef14a42 6509 if (mips_opts.arch == CPU_R4650)
252b5132
RH
6510 {
6511 as_bad (_("opcode not supported on this processor"));
6512 return;
6513 }
bdaaa2e1 6514 /* Itbl support may require additional care here. */
252b5132 6515 coproc = 1;
e7af610e 6516 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
6517 {
6518 s = "ldc1";
6519 goto ld;
6520 }
6521
6522 s = "lwc1";
6523 fmt = "T,o(b)";
6524 goto ldd_std;
6525
6526 case M_S_DAB:
fef14a42 6527 if (mips_opts.arch == CPU_R4650)
252b5132
RH
6528 {
6529 as_bad (_("opcode not supported on this processor"));
6530 return;
6531 }
6532
e7af610e 6533 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
6534 {
6535 s = "sdc1";
6536 goto st;
6537 }
6538
6539 s = "swc1";
6540 fmt = "T,o(b)";
bdaaa2e1 6541 /* Itbl support may require additional care here. */
252b5132
RH
6542 coproc = 1;
6543 goto ldd_std;
6544
6545 case M_LD_AB:
ca4e0257 6546 if (HAVE_64BIT_GPRS)
252b5132
RH
6547 {
6548 s = "ld";
6549 goto ld;
6550 }
6551
6552 s = "lw";
6553 fmt = "t,o(b)";
6554 goto ldd_std;
6555
6556 case M_SD_AB:
ca4e0257 6557 if (HAVE_64BIT_GPRS)
252b5132
RH
6558 {
6559 s = "sd";
6560 goto st;
6561 }
6562
6563 s = "sw";
6564 fmt = "t,o(b)";
6565
6566 ldd_std:
afdbd6d0
CD
6567 /* We do _not_ bother to allow embedded PIC (symbol-local_symbol)
6568 loads for the case of doing a pair of loads to simulate an 'ld'.
6569 This is not currently done by the compiler, and assembly coders
6570 writing embedded-pic code can cope. */
6571
252b5132
RH
6572 if (offset_expr.X_op != O_symbol
6573 && offset_expr.X_op != O_constant)
6574 {
6575 as_bad (_("expression too complex"));
6576 offset_expr.X_op = O_constant;
6577 }
6578
6579 /* Even on a big endian machine $fn comes before $fn+1. We have
6580 to adjust when loading from memory. We set coproc if we must
6581 load $fn+1 first. */
bdaaa2e1 6582 /* Itbl support may require additional care here. */
252b5132
RH
6583 if (! target_big_endian)
6584 coproc = 0;
6585
6586 if (mips_pic == NO_PIC
6587 || offset_expr.X_op == O_constant)
6588 {
6589 /* If this is a reference to a GP relative symbol, we want
cdf6fd85
TS
6590 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6591 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
6592 If we have a base register, we use this
6593 addu $at,$breg,$gp
cdf6fd85
TS
6594 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6595 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
6596 If this is not a GP relative symbol, we want
6597 lui $at,<sym> (BFD_RELOC_HI16_S)
6598 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6599 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6600 If there is a base register, we add it to $at after the
6601 lui instruction. If there is a constant, we always use
6602 the last case. */
e7d556df 6603 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
252b5132 6604 || nopic_need_relax (offset_expr.X_add_symbol, 1))
67c0d1eb 6605 used_at = 1;
252b5132
RH
6606 else
6607 {
4d7206a2 6608 relax_start (offset_expr.X_add_symbol);
252b5132
RH
6609 if (breg == 0)
6610 {
c9914766 6611 tempreg = mips_gp_register;
252b5132
RH
6612 used_at = 0;
6613 }
6614 else
6615 {
67c0d1eb 6616 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6617 AT, breg, mips_gp_register);
252b5132 6618 tempreg = AT;
252b5132
RH
6619 used_at = 1;
6620 }
6621
beae10d5 6622 /* Itbl support may require additional care here. */
67c0d1eb 6623 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
17a2f251 6624 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6625 offset_expr.X_add_number += 4;
6626
6627 /* Set mips_optimize to 2 to avoid inserting an
6628 undesired nop. */
6629 hold_mips_optimize = mips_optimize;
6630 mips_optimize = 2;
beae10d5 6631 /* Itbl support may require additional care here. */
67c0d1eb 6632 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
17a2f251 6633 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6634 mips_optimize = hold_mips_optimize;
6635
4d7206a2 6636 relax_switch ();
252b5132
RH
6637
6638 /* We just generated two relocs. When tc_gen_reloc
6639 handles this case, it will skip the first reloc and
6640 handle the second. The second reloc already has an
6641 extra addend of 4, which we added above. We must
6642 subtract it out, and then subtract another 4 to make
6643 the first reloc come out right. The second reloc
6644 will come out right because we are going to add 4 to
6645 offset_expr when we build its instruction below.
6646
6647 If we have a symbol, then we don't want to include
6648 the offset, because it will wind up being included
6649 when we generate the reloc. */
6650
6651 if (offset_expr.X_op == O_constant)
6652 offset_expr.X_add_number -= 8;
6653 else
6654 {
6655 offset_expr.X_add_number = -4;
6656 offset_expr.X_op = O_constant;
6657 }
6658 }
67c0d1eb 6659 macro_build_lui (&offset_expr, AT);
252b5132 6660 if (breg != 0)
67c0d1eb 6661 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 6662 /* Itbl support may require additional care here. */
67c0d1eb 6663 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
17a2f251 6664 BFD_RELOC_LO16, AT);
252b5132
RH
6665 /* FIXME: How do we handle overflow here? */
6666 offset_expr.X_add_number += 4;
beae10d5 6667 /* Itbl support may require additional care here. */
67c0d1eb 6668 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
17a2f251 6669 BFD_RELOC_LO16, AT);
4d7206a2
RS
6670 if (mips_relax.sequence)
6671 relax_end ();
bdaaa2e1 6672 }
252b5132
RH
6673 else if (mips_pic == SVR4_PIC && ! mips_big_got)
6674 {
252b5132
RH
6675 /* If this is a reference to an external symbol, we want
6676 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6677 nop
6678 <op> $treg,0($at)
6679 <op> $treg+1,4($at)
6680 Otherwise we want
6681 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6682 nop
6683 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6684 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6685 If there is a base register we add it to $at before the
6686 lwc1 instructions. If there is a constant we include it
6687 in the lwc1 instructions. */
6688 used_at = 1;
6689 expr1.X_add_number = offset_expr.X_add_number;
252b5132
RH
6690 if (expr1.X_add_number < -0x8000
6691 || expr1.X_add_number >= 0x8000 - 4)
6692 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb
RS
6693 load_got_offset (AT, &offset_expr);
6694 macro_build (NULL, "nop", "");
252b5132 6695 if (breg != 0)
67c0d1eb 6696 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
252b5132
RH
6697
6698 /* Set mips_optimize to 2 to avoid inserting an undesired
6699 nop. */
6700 hold_mips_optimize = mips_optimize;
6701 mips_optimize = 2;
4d7206a2 6702
beae10d5 6703 /* Itbl support may require additional care here. */
4d7206a2 6704 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
6705 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
6706 BFD_RELOC_LO16, AT);
4d7206a2 6707 expr1.X_add_number += 4;
67c0d1eb
RS
6708 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
6709 BFD_RELOC_LO16, AT);
4d7206a2 6710 relax_switch ();
67c0d1eb
RS
6711 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6712 BFD_RELOC_LO16, AT);
4d7206a2 6713 offset_expr.X_add_number += 4;
67c0d1eb
RS
6714 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6715 BFD_RELOC_LO16, AT);
4d7206a2 6716 relax_end ();
252b5132 6717
4d7206a2 6718 mips_optimize = hold_mips_optimize;
252b5132
RH
6719 }
6720 else if (mips_pic == SVR4_PIC)
6721 {
67c0d1eb 6722 int gpdelay;
252b5132
RH
6723
6724 /* If this is a reference to an external symbol, we want
6725 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6726 addu $at,$at,$gp
6727 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
6728 nop
6729 <op> $treg,0($at)
6730 <op> $treg+1,4($at)
6731 Otherwise we want
6732 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6733 nop
6734 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6735 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6736 If there is a base register we add it to $at before the
6737 lwc1 instructions. If there is a constant we include it
6738 in the lwc1 instructions. */
6739 used_at = 1;
6740 expr1.X_add_number = offset_expr.X_add_number;
6741 offset_expr.X_add_number = 0;
6742 if (expr1.X_add_number < -0x8000
6743 || expr1.X_add_number >= 0x8000 - 4)
6744 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 6745 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 6746 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
6747 macro_build (&offset_expr, "lui", "t,u",
6748 AT, BFD_RELOC_MIPS_GOT_HI16);
6749 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6750 AT, AT, mips_gp_register);
67c0d1eb 6751 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 6752 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
67c0d1eb 6753 macro_build (NULL, "nop", "");
252b5132 6754 if (breg != 0)
67c0d1eb 6755 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 6756 /* Itbl support may require additional care here. */
67c0d1eb 6757 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
17a2f251 6758 BFD_RELOC_LO16, AT);
252b5132
RH
6759 expr1.X_add_number += 4;
6760
6761 /* Set mips_optimize to 2 to avoid inserting an undesired
6762 nop. */
6763 hold_mips_optimize = mips_optimize;
6764 mips_optimize = 2;
beae10d5 6765 /* Itbl support may require additional care here. */
67c0d1eb 6766 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
17a2f251 6767 BFD_RELOC_LO16, AT);
252b5132
RH
6768 mips_optimize = hold_mips_optimize;
6769 expr1.X_add_number -= 4;
6770
4d7206a2
RS
6771 relax_switch ();
6772 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
6773 if (gpdelay)
6774 macro_build (NULL, "nop", "");
6775 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6776 BFD_RELOC_MIPS_GOT16, mips_gp_register);
6777 macro_build (NULL, "nop", "");
252b5132 6778 if (breg != 0)
67c0d1eb 6779 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 6780 /* Itbl support may require additional care here. */
67c0d1eb
RS
6781 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6782 BFD_RELOC_LO16, AT);
4d7206a2 6783 offset_expr.X_add_number += 4;
252b5132
RH
6784
6785 /* Set mips_optimize to 2 to avoid inserting an undesired
6786 nop. */
6787 hold_mips_optimize = mips_optimize;
6788 mips_optimize = 2;
beae10d5 6789 /* Itbl support may require additional care here. */
67c0d1eb
RS
6790 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6791 BFD_RELOC_LO16, AT);
252b5132 6792 mips_optimize = hold_mips_optimize;
4d7206a2 6793 relax_end ();
252b5132
RH
6794 }
6795 else if (mips_pic == EMBEDDED_PIC)
6796 {
6797 /* If there is no base register, we use
cdf6fd85
TS
6798 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6799 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
6800 If we have a base register, we use
6801 addu $at,$breg,$gp
cdf6fd85
TS
6802 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6803 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
6804 */
6805 if (breg == 0)
6806 {
c9914766 6807 tempreg = mips_gp_register;
252b5132
RH
6808 used_at = 0;
6809 }
6810 else
6811 {
67c0d1eb 6812 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6813 AT, breg, mips_gp_register);
252b5132
RH
6814 tempreg = AT;
6815 used_at = 1;
6816 }
6817
beae10d5 6818 /* Itbl support may require additional care here. */
67c0d1eb 6819 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
17a2f251 6820 BFD_RELOC_GPREL16, tempreg);
252b5132 6821 offset_expr.X_add_number += 4;
beae10d5 6822 /* Itbl support may require additional care here. */
67c0d1eb 6823 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
17a2f251 6824 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6825 }
6826 else
6827 abort ();
6828
6829 if (! used_at)
6830 return;
6831
6832 break;
6833
6834 case M_LD_OB:
6835 s = "lw";
6836 goto sd_ob;
6837 case M_SD_OB:
6838 s = "sw";
6839 sd_ob:
ca4e0257 6840 assert (HAVE_32BIT_ADDRESSES);
67c0d1eb 6841 macro_build (&offset_expr, s, "t,o(b)", treg, BFD_RELOC_LO16, breg);
252b5132 6842 offset_expr.X_add_number += 4;
67c0d1eb 6843 macro_build (&offset_expr, s, "t,o(b)", treg + 1, BFD_RELOC_LO16, breg);
252b5132
RH
6844 return;
6845
6846 /* New code added to support COPZ instructions.
6847 This code builds table entries out of the macros in mip_opcodes.
6848 R4000 uses interlocks to handle coproc delays.
6849 Other chips (like the R3000) require nops to be inserted for delays.
6850
f72c8c98 6851 FIXME: Currently, we require that the user handle delays.
252b5132
RH
6852 In order to fill delay slots for non-interlocked chips,
6853 we must have a way to specify delays based on the coprocessor.
6854 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
6855 What are the side-effects of the cop instruction?
6856 What cache support might we have and what are its effects?
6857 Both coprocessor & memory require delays. how long???
bdaaa2e1 6858 What registers are read/set/modified?
252b5132
RH
6859
6860 If an itbl is provided to interpret cop instructions,
bdaaa2e1 6861 this knowledge can be encoded in the itbl spec. */
252b5132
RH
6862
6863 case M_COP0:
6864 s = "c0";
6865 goto copz;
6866 case M_COP1:
6867 s = "c1";
6868 goto copz;
6869 case M_COP2:
6870 s = "c2";
6871 goto copz;
6872 case M_COP3:
6873 s = "c3";
6874 copz:
6875 /* For now we just do C (same as Cz). The parameter will be
6876 stored in insn_opcode by mips_ip. */
67c0d1eb 6877 macro_build (NULL, s, "C", ip->insn_opcode);
252b5132
RH
6878 return;
6879
ea1fb5dc 6880 case M_MOVE:
67c0d1eb 6881 move_register (dreg, sreg);
ea1fb5dc
RS
6882 return;
6883
252b5132
RH
6884#ifdef LOSING_COMPILER
6885 default:
6886 /* Try and see if this is a new itbl instruction.
6887 This code builds table entries out of the macros in mip_opcodes.
6888 FIXME: For now we just assemble the expression and pass it's
6889 value along as a 32-bit immediate.
bdaaa2e1 6890 We may want to have the assembler assemble this value,
252b5132
RH
6891 so that we gain the assembler's knowledge of delay slots,
6892 symbols, etc.
6893 Would it be more efficient to use mask (id) here? */
bdaaa2e1 6894 if (itbl_have_entries
252b5132 6895 && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
beae10d5 6896 {
252b5132
RH
6897 s = ip->insn_mo->name;
6898 s2 = "cop3";
6899 coproc = ITBL_DECODE_PNUM (immed_expr);;
67c0d1eb 6900 macro_build (&immed_expr, s, "C");
252b5132 6901 return;
beae10d5 6902 }
252b5132
RH
6903 macro2 (ip);
6904 return;
6905 }
6906 if (mips_opts.noat)
6907 as_warn (_("Macro used $at after \".set noat\""));
6908}
bdaaa2e1 6909
252b5132 6910static void
17a2f251 6911macro2 (struct mips_cl_insn *ip)
252b5132
RH
6912{
6913 register int treg, sreg, dreg, breg;
6914 int tempreg;
6915 int mask;
252b5132
RH
6916 int used_at;
6917 expressionS expr1;
6918 const char *s;
6919 const char *s2;
6920 const char *fmt;
6921 int likely = 0;
6922 int dbl = 0;
6923 int coproc = 0;
6924 int lr = 0;
6925 int imm = 0;
6926 int off;
6927 offsetT maxnum;
6928 bfd_reloc_code_real_type r;
bdaaa2e1 6929
252b5132
RH
6930 treg = (ip->insn_opcode >> 16) & 0x1f;
6931 dreg = (ip->insn_opcode >> 11) & 0x1f;
6932 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
6933 mask = ip->insn_mo->mask;
bdaaa2e1 6934
252b5132
RH
6935 expr1.X_op = O_constant;
6936 expr1.X_op_symbol = NULL;
6937 expr1.X_add_symbol = NULL;
6938 expr1.X_add_number = 1;
bdaaa2e1 6939
252b5132
RH
6940 switch (mask)
6941 {
6942#endif /* LOSING_COMPILER */
6943
6944 case M_DMUL:
6945 dbl = 1;
6946 case M_MUL:
67c0d1eb
RS
6947 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", sreg, treg);
6948 macro_build (NULL, "mflo", "d", dreg);
252b5132
RH
6949 return;
6950
6951 case M_DMUL_I:
6952 dbl = 1;
6953 case M_MUL_I:
6954 /* The MIPS assembler some times generates shifts and adds. I'm
6955 not trying to be that fancy. GCC should do this for us
6956 anyway. */
67c0d1eb
RS
6957 load_register (AT, &imm_expr, dbl);
6958 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, AT);
6959 macro_build (NULL, "mflo", "d", dreg);
252b5132
RH
6960 break;
6961
6962 case M_DMULO_I:
6963 dbl = 1;
6964 case M_MULO_I:
6965 imm = 1;
6966 goto do_mulo;
6967
6968 case M_DMULO:
6969 dbl = 1;
6970 case M_MULO:
6971 do_mulo:
b34976b6 6972 mips_emit_delays (TRUE);
252b5132
RH
6973 ++mips_opts.noreorder;
6974 mips_any_noreorder = 1;
6975 if (imm)
67c0d1eb
RS
6976 load_register (AT, &imm_expr, dbl);
6977 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
6978 macro_build (NULL, "mflo", "d", dreg);
6979 macro_build (NULL, dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, RA);
6980 macro_build (NULL, "mfhi", "d", AT);
252b5132 6981 if (mips_trap)
67c0d1eb 6982 macro_build (NULL, "tne", "s,t,q", dreg, AT, 6);
252b5132
RH
6983 else
6984 {
6985 expr1.X_add_number = 8;
67c0d1eb
RS
6986 macro_build (&expr1, "beq", "s,t,p", dreg, AT);
6987 macro_build (NULL, "nop", "", 0);
6988 macro_build (NULL, "break", "c", 6);
252b5132
RH
6989 }
6990 --mips_opts.noreorder;
67c0d1eb 6991 macro_build (NULL, "mflo", "d", dreg);
252b5132
RH
6992 break;
6993
6994 case M_DMULOU_I:
6995 dbl = 1;
6996 case M_MULOU_I:
6997 imm = 1;
6998 goto do_mulou;
6999
7000 case M_DMULOU:
7001 dbl = 1;
7002 case M_MULOU:
7003 do_mulou:
b34976b6 7004 mips_emit_delays (TRUE);
252b5132
RH
7005 ++mips_opts.noreorder;
7006 mips_any_noreorder = 1;
7007 if (imm)
67c0d1eb
RS
7008 load_register (AT, &imm_expr, dbl);
7009 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
17a2f251 7010 sreg, imm ? AT : treg);
67c0d1eb
RS
7011 macro_build (NULL, "mfhi", "d", AT);
7012 macro_build (NULL, "mflo", "d", dreg);
252b5132 7013 if (mips_trap)
67c0d1eb 7014 macro_build (NULL, "tne", "s,t,q", AT, 0, 6);
252b5132
RH
7015 else
7016 {
7017 expr1.X_add_number = 8;
67c0d1eb
RS
7018 macro_build (&expr1, "beq", "s,t,p", AT, 0);
7019 macro_build (NULL, "nop", "", 0);
7020 macro_build (NULL, "break", "c", 6);
252b5132
RH
7021 }
7022 --mips_opts.noreorder;
7023 break;
7024
771c7ce4 7025 case M_DROL:
fef14a42 7026 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097
CD
7027 {
7028 if (dreg == sreg)
7029 {
7030 tempreg = AT;
7031 used_at = 1;
7032 }
7033 else
7034 {
7035 tempreg = dreg;
7036 used_at = 0;
7037 }
67c0d1eb
RS
7038 macro_build (NULL, "dnegu", "d,w", tempreg, treg);
7039 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, tempreg);
82dd0097
CD
7040 if (used_at)
7041 break;
7042 return;
7043 }
67c0d1eb
RS
7044 macro_build (NULL, "dsubu", "d,v,t", AT, 0, treg);
7045 macro_build (NULL, "dsrlv", "d,t,s", AT, sreg, AT);
7046 macro_build (NULL, "dsllv", "d,t,s", dreg, sreg, treg);
7047 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
7048 break;
7049
252b5132 7050 case M_ROL:
fef14a42 7051 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097
CD
7052 {
7053 if (dreg == sreg)
7054 {
7055 tempreg = AT;
7056 used_at = 1;
7057 }
7058 else
7059 {
7060 tempreg = dreg;
7061 used_at = 0;
7062 }
67c0d1eb
RS
7063 macro_build (NULL, "negu", "d,w", tempreg, treg);
7064 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, tempreg);
82dd0097
CD
7065 if (used_at)
7066 break;
7067 return;
7068 }
67c0d1eb
RS
7069 macro_build (NULL, "subu", "d,v,t", AT, 0, treg);
7070 macro_build (NULL, "srlv", "d,t,s", AT, sreg, AT);
7071 macro_build (NULL, "sllv", "d,t,s", dreg, sreg, treg);
7072 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
7073 break;
7074
771c7ce4
TS
7075 case M_DROL_I:
7076 {
7077 unsigned int rot;
82dd0097 7078 char *l, *r;
771c7ce4
TS
7079
7080 if (imm_expr.X_op != O_constant)
82dd0097 7081 as_bad (_("Improper rotate count"));
771c7ce4 7082 rot = imm_expr.X_add_number & 0x3f;
fef14a42 7083 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
60b63b72
RS
7084 {
7085 rot = (64 - rot) & 0x3f;
7086 if (rot >= 32)
67c0d1eb 7087 macro_build (NULL, "dror32", "d,w,<", dreg, sreg, rot - 32);
60b63b72 7088 else
67c0d1eb 7089 macro_build (NULL, "dror", "d,w,<", dreg, sreg, rot);
82dd0097 7090 return;
60b63b72 7091 }
483fc7cd 7092 if (rot == 0)
483fc7cd 7093 {
67c0d1eb 7094 macro_build (NULL, "dsrl", "d,w,<", dreg, sreg, 0);
82dd0097 7095 return;
483fc7cd 7096 }
82dd0097
CD
7097 l = (rot < 0x20) ? "dsll" : "dsll32";
7098 r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
7099 rot &= 0x1f;
67c0d1eb
RS
7100 macro_build (NULL, l, "d,w,<", AT, sreg, rot);
7101 macro_build (NULL, r, "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7102 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
7103 }
7104 break;
7105
252b5132 7106 case M_ROL_I:
771c7ce4
TS
7107 {
7108 unsigned int rot;
7109
7110 if (imm_expr.X_op != O_constant)
82dd0097 7111 as_bad (_("Improper rotate count"));
771c7ce4 7112 rot = imm_expr.X_add_number & 0x1f;
fef14a42 7113 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
60b63b72 7114 {
67c0d1eb 7115 macro_build (NULL, "ror", "d,w,<", dreg, sreg, (32 - rot) & 0x1f);
82dd0097 7116 return;
60b63b72 7117 }
483fc7cd 7118 if (rot == 0)
483fc7cd 7119 {
67c0d1eb 7120 macro_build (NULL, "srl", "d,w,<", dreg, sreg, 0);
82dd0097 7121 return;
483fc7cd 7122 }
67c0d1eb
RS
7123 macro_build (NULL, "sll", "d,w,<", AT, sreg, rot);
7124 macro_build (NULL, "srl", "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7125 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
7126 }
7127 break;
7128
7129 case M_DROR:
fef14a42 7130 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097 7131 {
67c0d1eb 7132 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, treg);
82dd0097
CD
7133 return;
7134 }
67c0d1eb
RS
7135 macro_build (NULL, "dsubu", "d,v,t", AT, 0, treg);
7136 macro_build (NULL, "dsllv", "d,t,s", AT, sreg, AT);
7137 macro_build (NULL, "dsrlv", "d,t,s", dreg, sreg, treg);
7138 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
7139 break;
7140
7141 case M_ROR:
fef14a42 7142 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 7143 {
67c0d1eb 7144 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, treg);
82dd0097
CD
7145 return;
7146 }
67c0d1eb
RS
7147 macro_build (NULL, "subu", "d,v,t", AT, 0, treg);
7148 macro_build (NULL, "sllv", "d,t,s", AT, sreg, AT);
7149 macro_build (NULL, "srlv", "d,t,s", dreg, sreg, treg);
7150 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
7151 break;
7152
771c7ce4
TS
7153 case M_DROR_I:
7154 {
7155 unsigned int rot;
82dd0097 7156 char *l, *r;
771c7ce4
TS
7157
7158 if (imm_expr.X_op != O_constant)
82dd0097 7159 as_bad (_("Improper rotate count"));
771c7ce4 7160 rot = imm_expr.X_add_number & 0x3f;
fef14a42 7161 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097
CD
7162 {
7163 if (rot >= 32)
67c0d1eb 7164 macro_build (NULL, "dror32", "d,w,<", dreg, sreg, rot - 32);
82dd0097 7165 else
67c0d1eb 7166 macro_build (NULL, "dror", "d,w,<", dreg, sreg, rot);
82dd0097
CD
7167 return;
7168 }
483fc7cd 7169 if (rot == 0)
483fc7cd 7170 {
67c0d1eb 7171 macro_build (NULL, "dsrl", "d,w,<", dreg, sreg, 0);
82dd0097 7172 return;
483fc7cd 7173 }
82dd0097
CD
7174 r = (rot < 0x20) ? "dsrl" : "dsrl32";
7175 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
7176 rot &= 0x1f;
67c0d1eb
RS
7177 macro_build (NULL, r, "d,w,<", AT, sreg, rot);
7178 macro_build (NULL, l, "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7179 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
7180 }
7181 break;
7182
252b5132 7183 case M_ROR_I:
771c7ce4
TS
7184 {
7185 unsigned int rot;
7186
7187 if (imm_expr.X_op != O_constant)
82dd0097 7188 as_bad (_("Improper rotate count"));
771c7ce4 7189 rot = imm_expr.X_add_number & 0x1f;
fef14a42 7190 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 7191 {
67c0d1eb 7192 macro_build (NULL, "ror", "d,w,<", dreg, sreg, rot);
82dd0097
CD
7193 return;
7194 }
483fc7cd 7195 if (rot == 0)
483fc7cd 7196 {
67c0d1eb 7197 macro_build (NULL, "srl", "d,w,<", dreg, sreg, 0);
82dd0097 7198 return;
483fc7cd 7199 }
67c0d1eb
RS
7200 macro_build (NULL, "srl", "d,w,<", AT, sreg, rot);
7201 macro_build (NULL, "sll", "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7202 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4 7203 }
252b5132
RH
7204 break;
7205
7206 case M_S_DOB:
fef14a42 7207 if (mips_opts.arch == CPU_R4650)
252b5132
RH
7208 {
7209 as_bad (_("opcode not supported on this processor"));
7210 return;
7211 }
e7af610e 7212 assert (mips_opts.isa == ISA_MIPS1);
252b5132
RH
7213 /* Even on a big endian machine $fn comes before $fn+1. We have
7214 to adjust when storing to memory. */
67c0d1eb
RS
7215 macro_build (&offset_expr, "swc1", "T,o(b)",
7216 target_big_endian ? treg + 1 : treg, BFD_RELOC_LO16, breg);
252b5132 7217 offset_expr.X_add_number += 4;
67c0d1eb
RS
7218 macro_build (&offset_expr, "swc1", "T,o(b)",
7219 target_big_endian ? treg : treg + 1, BFD_RELOC_LO16, breg);
252b5132
RH
7220 return;
7221
7222 case M_SEQ:
7223 if (sreg == 0)
67c0d1eb 7224 macro_build (&expr1, "sltiu", "t,r,j", dreg, treg, BFD_RELOC_LO16);
252b5132 7225 else if (treg == 0)
67c0d1eb 7226 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7227 else
7228 {
67c0d1eb
RS
7229 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
7230 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
252b5132
RH
7231 }
7232 return;
7233
7234 case M_SEQ_I:
7235 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7236 {
67c0d1eb 7237 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7238 return;
7239 }
7240 if (sreg == 0)
7241 {
7242 as_warn (_("Instruction %s: result is always false"),
7243 ip->insn_mo->name);
67c0d1eb 7244 move_register (dreg, 0);
252b5132
RH
7245 return;
7246 }
7247 if (imm_expr.X_op == O_constant
7248 && imm_expr.X_add_number >= 0
7249 && imm_expr.X_add_number < 0x10000)
7250 {
67c0d1eb 7251 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7252 used_at = 0;
7253 }
7254 else if (imm_expr.X_op == O_constant
7255 && imm_expr.X_add_number > -0x8000
7256 && imm_expr.X_add_number < 0)
7257 {
7258 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 7259 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
17a2f251 7260 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7261 used_at = 0;
7262 }
7263 else
7264 {
67c0d1eb
RS
7265 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7266 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
252b5132
RH
7267 used_at = 1;
7268 }
67c0d1eb 7269 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
252b5132
RH
7270 if (used_at)
7271 break;
7272 return;
7273
7274 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
7275 s = "slt";
7276 goto sge;
7277 case M_SGEU:
7278 s = "sltu";
7279 sge:
67c0d1eb
RS
7280 macro_build (NULL, s, "d,v,t", dreg, sreg, treg);
7281 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
252b5132
RH
7282 return;
7283
7284 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
7285 case M_SGEU_I:
7286 if (imm_expr.X_op == O_constant
7287 && imm_expr.X_add_number >= -0x8000
7288 && imm_expr.X_add_number < 0x8000)
7289 {
67c0d1eb
RS
7290 macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
7291 dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7292 used_at = 0;
7293 }
7294 else
7295 {
67c0d1eb
RS
7296 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7297 macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
7298 dreg, sreg, AT);
252b5132
RH
7299 used_at = 1;
7300 }
67c0d1eb 7301 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
252b5132
RH
7302 if (used_at)
7303 break;
7304 return;
7305
7306 case M_SGT: /* sreg > treg <==> treg < sreg */
7307 s = "slt";
7308 goto sgt;
7309 case M_SGTU:
7310 s = "sltu";
7311 sgt:
67c0d1eb 7312 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
252b5132
RH
7313 return;
7314
7315 case M_SGT_I: /* sreg > I <==> I < sreg */
7316 s = "slt";
7317 goto sgti;
7318 case M_SGTU_I:
7319 s = "sltu";
7320 sgti:
67c0d1eb
RS
7321 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7322 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
252b5132
RH
7323 break;
7324
2396cfb9 7325 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
252b5132
RH
7326 s = "slt";
7327 goto sle;
7328 case M_SLEU:
7329 s = "sltu";
7330 sle:
67c0d1eb
RS
7331 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
7332 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
252b5132
RH
7333 return;
7334
2396cfb9 7335 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
252b5132
RH
7336 s = "slt";
7337 goto slei;
7338 case M_SLEU_I:
7339 s = "sltu";
7340 slei:
67c0d1eb
RS
7341 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7342 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
7343 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
252b5132
RH
7344 break;
7345
7346 case M_SLT_I:
7347 if (imm_expr.X_op == O_constant
7348 && imm_expr.X_add_number >= -0x8000
7349 && imm_expr.X_add_number < 0x8000)
7350 {
67c0d1eb 7351 macro_build (&imm_expr, "slti", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7352 return;
7353 }
67c0d1eb
RS
7354 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7355 macro_build (NULL, "slt", "d,v,t", dreg, sreg, AT);
252b5132
RH
7356 break;
7357
7358 case M_SLTU_I:
7359 if (imm_expr.X_op == O_constant
7360 && imm_expr.X_add_number >= -0x8000
7361 && imm_expr.X_add_number < 0x8000)
7362 {
67c0d1eb 7363 macro_build (&imm_expr, "sltiu", "t,r,j", dreg, sreg,
17a2f251 7364 BFD_RELOC_LO16);
252b5132
RH
7365 return;
7366 }
67c0d1eb
RS
7367 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7368 macro_build (NULL, "sltu", "d,v,t", dreg, sreg, AT);
252b5132
RH
7369 break;
7370
7371 case M_SNE:
7372 if (sreg == 0)
67c0d1eb 7373 macro_build (NULL, "sltu", "d,v,t", dreg, 0, treg);
252b5132 7374 else if (treg == 0)
67c0d1eb 7375 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
252b5132
RH
7376 else
7377 {
67c0d1eb
RS
7378 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
7379 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
252b5132
RH
7380 }
7381 return;
7382
7383 case M_SNE_I:
7384 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7385 {
67c0d1eb 7386 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
252b5132
RH
7387 return;
7388 }
7389 if (sreg == 0)
7390 {
7391 as_warn (_("Instruction %s: result is always true"),
7392 ip->insn_mo->name);
67c0d1eb
RS
7393 macro_build (&expr1, HAVE_32BIT_GPRS ? "addiu" : "daddiu", "t,r,j",
7394 dreg, 0, BFD_RELOC_LO16);
252b5132
RH
7395 return;
7396 }
7397 if (imm_expr.X_op == O_constant
7398 && imm_expr.X_add_number >= 0
7399 && imm_expr.X_add_number < 0x10000)
7400 {
67c0d1eb 7401 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7402 used_at = 0;
7403 }
7404 else if (imm_expr.X_op == O_constant
7405 && imm_expr.X_add_number > -0x8000
7406 && imm_expr.X_add_number < 0)
7407 {
7408 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 7409 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
17a2f251 7410 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7411 used_at = 0;
7412 }
7413 else
7414 {
67c0d1eb
RS
7415 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7416 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
252b5132
RH
7417 used_at = 1;
7418 }
67c0d1eb 7419 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
252b5132
RH
7420 if (used_at)
7421 break;
7422 return;
7423
7424 case M_DSUB_I:
7425 dbl = 1;
7426 case M_SUB_I:
7427 if (imm_expr.X_op == O_constant
7428 && imm_expr.X_add_number > -0x8000
7429 && imm_expr.X_add_number <= 0x8000)
7430 {
7431 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb
RS
7432 macro_build (&imm_expr, dbl ? "daddi" : "addi", "t,r,j",
7433 dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7434 return;
7435 }
67c0d1eb
RS
7436 load_register (AT, &imm_expr, dbl);
7437 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
252b5132
RH
7438 break;
7439
7440 case M_DSUBU_I:
7441 dbl = 1;
7442 case M_SUBU_I:
7443 if (imm_expr.X_op == O_constant
7444 && imm_expr.X_add_number > -0x8000
7445 && imm_expr.X_add_number <= 0x8000)
7446 {
7447 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb
RS
7448 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "t,r,j",
7449 dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7450 return;
7451 }
67c0d1eb
RS
7452 load_register (AT, &imm_expr, dbl);
7453 macro_build (NULL, dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
252b5132
RH
7454 break;
7455
7456 case M_TEQ_I:
7457 s = "teq";
7458 goto trap;
7459 case M_TGE_I:
7460 s = "tge";
7461 goto trap;
7462 case M_TGEU_I:
7463 s = "tgeu";
7464 goto trap;
7465 case M_TLT_I:
7466 s = "tlt";
7467 goto trap;
7468 case M_TLTU_I:
7469 s = "tltu";
7470 goto trap;
7471 case M_TNE_I:
7472 s = "tne";
7473 trap:
67c0d1eb
RS
7474 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7475 macro_build (NULL, s, "s,t", sreg, AT);
252b5132
RH
7476 break;
7477
252b5132 7478 case M_TRUNCWS:
43841e91 7479 case M_TRUNCWD:
e7af610e 7480 assert (mips_opts.isa == ISA_MIPS1);
252b5132
RH
7481 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
7482 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
7483
7484 /*
7485 * Is the double cfc1 instruction a bug in the mips assembler;
7486 * or is there a reason for it?
7487 */
b34976b6 7488 mips_emit_delays (TRUE);
252b5132
RH
7489 ++mips_opts.noreorder;
7490 mips_any_noreorder = 1;
67c0d1eb
RS
7491 macro_build (NULL, "cfc1", "t,G", treg, RA);
7492 macro_build (NULL, "cfc1", "t,G", treg, RA);
7493 macro_build (NULL, "nop", "");
252b5132 7494 expr1.X_add_number = 3;
67c0d1eb 7495 macro_build (&expr1, "ori", "t,r,i", AT, treg, BFD_RELOC_LO16);
252b5132 7496 expr1.X_add_number = 2;
67c0d1eb
RS
7497 macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
7498 macro_build (NULL, "ctc1", "t,G", AT, RA);
7499 macro_build (NULL, "nop", "");
7500 macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
7501 dreg, sreg);
7502 macro_build (NULL, "ctc1", "t,G", treg, RA);
7503 macro_build (NULL, "nop", "");
252b5132
RH
7504 --mips_opts.noreorder;
7505 break;
7506
7507 case M_ULH:
7508 s = "lb";
7509 goto ulh;
7510 case M_ULHU:
7511 s = "lbu";
7512 ulh:
7513 if (offset_expr.X_add_number >= 0x7fff)
7514 as_bad (_("operand overflow"));
252b5132 7515 if (! target_big_endian)
f9419b05 7516 ++offset_expr.X_add_number;
67c0d1eb 7517 macro_build (&offset_expr, s, "t,o(b)", AT, BFD_RELOC_LO16, breg);
252b5132 7518 if (! target_big_endian)
f9419b05 7519 --offset_expr.X_add_number;
252b5132 7520 else
f9419b05 7521 ++offset_expr.X_add_number;
67c0d1eb
RS
7522 macro_build (&offset_expr, "lbu", "t,o(b)", treg, BFD_RELOC_LO16, breg);
7523 macro_build (NULL, "sll", "d,w,<", AT, AT, 8);
7524 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
252b5132
RH
7525 break;
7526
7527 case M_ULD:
7528 s = "ldl";
7529 s2 = "ldr";
7530 off = 7;
7531 goto ulw;
7532 case M_ULW:
7533 s = "lwl";
7534 s2 = "lwr";
7535 off = 3;
7536 ulw:
7537 if (offset_expr.X_add_number >= 0x8000 - off)
7538 as_bad (_("operand overflow"));
af22f5b2
CD
7539 if (treg != breg)
7540 tempreg = treg;
7541 else
7542 tempreg = AT;
252b5132
RH
7543 if (! target_big_endian)
7544 offset_expr.X_add_number += off;
67c0d1eb 7545 macro_build (&offset_expr, s, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
252b5132
RH
7546 if (! target_big_endian)
7547 offset_expr.X_add_number -= off;
7548 else
7549 offset_expr.X_add_number += off;
67c0d1eb 7550 macro_build (&offset_expr, s2, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
af22f5b2
CD
7551
7552 /* If necessary, move the result in tempreg the final destination. */
7553 if (treg == tempreg)
7554 return;
7555 /* Protect second load's delay slot. */
7556 if (!gpr_interlocks)
67c0d1eb
RS
7557 macro_build (NULL, "nop", "");
7558 move_register (treg, tempreg);
af22f5b2 7559 break;
252b5132
RH
7560
7561 case M_ULD_A:
7562 s = "ldl";
7563 s2 = "ldr";
7564 off = 7;
7565 goto ulwa;
7566 case M_ULW_A:
7567 s = "lwl";
7568 s2 = "lwr";
7569 off = 3;
7570 ulwa:
d6bc6245 7571 used_at = 1;
67c0d1eb 7572 load_address (AT, &offset_expr, &used_at);
252b5132 7573 if (breg != 0)
67c0d1eb 7574 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
252b5132
RH
7575 if (! target_big_endian)
7576 expr1.X_add_number = off;
7577 else
7578 expr1.X_add_number = 0;
67c0d1eb 7579 macro_build (&expr1, s, "t,o(b)", treg, BFD_RELOC_LO16, AT);
252b5132
RH
7580 if (! target_big_endian)
7581 expr1.X_add_number = 0;
7582 else
7583 expr1.X_add_number = off;
67c0d1eb 7584 macro_build (&expr1, s2, "t,o(b)", treg, BFD_RELOC_LO16, AT);
252b5132
RH
7585 break;
7586
7587 case M_ULH_A:
7588 case M_ULHU_A:
d6bc6245 7589 used_at = 1;
67c0d1eb 7590 load_address (AT, &offset_expr, &used_at);
252b5132 7591 if (breg != 0)
67c0d1eb 7592 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
252b5132
RH
7593 if (target_big_endian)
7594 expr1.X_add_number = 0;
67c0d1eb 7595 macro_build (&expr1, mask == M_ULH_A ? "lb" : "lbu", "t,o(b)",
17a2f251 7596 treg, BFD_RELOC_LO16, AT);
252b5132
RH
7597 if (target_big_endian)
7598 expr1.X_add_number = 1;
7599 else
7600 expr1.X_add_number = 0;
67c0d1eb
RS
7601 macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
7602 macro_build (NULL, "sll", "d,w,<", treg, treg, 8);
7603 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
252b5132
RH
7604 break;
7605
7606 case M_USH:
7607 if (offset_expr.X_add_number >= 0x7fff)
7608 as_bad (_("operand overflow"));
7609 if (target_big_endian)
f9419b05 7610 ++offset_expr.X_add_number;
67c0d1eb
RS
7611 macro_build (&offset_expr, "sb", "t,o(b)", treg, BFD_RELOC_LO16, breg);
7612 macro_build (NULL, "srl", "d,w,<", AT, treg, 8);
252b5132 7613 if (target_big_endian)
f9419b05 7614 --offset_expr.X_add_number;
252b5132 7615 else
f9419b05 7616 ++offset_expr.X_add_number;
67c0d1eb 7617 macro_build (&offset_expr, "sb", "t,o(b)", AT, BFD_RELOC_LO16, breg);
252b5132
RH
7618 break;
7619
7620 case M_USD:
7621 s = "sdl";
7622 s2 = "sdr";
7623 off = 7;
7624 goto usw;
7625 case M_USW:
7626 s = "swl";
7627 s2 = "swr";
7628 off = 3;
7629 usw:
7630 if (offset_expr.X_add_number >= 0x8000 - off)
7631 as_bad (_("operand overflow"));
7632 if (! target_big_endian)
7633 offset_expr.X_add_number += off;
67c0d1eb 7634 macro_build (&offset_expr, s, "t,o(b)", treg, BFD_RELOC_LO16, breg);
252b5132
RH
7635 if (! target_big_endian)
7636 offset_expr.X_add_number -= off;
7637 else
7638 offset_expr.X_add_number += off;
67c0d1eb 7639 macro_build (&offset_expr, s2, "t,o(b)", treg, BFD_RELOC_LO16, breg);
252b5132
RH
7640 return;
7641
7642 case M_USD_A:
7643 s = "sdl";
7644 s2 = "sdr";
7645 off = 7;
7646 goto uswa;
7647 case M_USW_A:
7648 s = "swl";
7649 s2 = "swr";
7650 off = 3;
7651 uswa:
d6bc6245 7652 used_at = 1;
67c0d1eb 7653 load_address (AT, &offset_expr, &used_at);
252b5132 7654 if (breg != 0)
67c0d1eb 7655 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
252b5132
RH
7656 if (! target_big_endian)
7657 expr1.X_add_number = off;
7658 else
7659 expr1.X_add_number = 0;
67c0d1eb 7660 macro_build (&expr1, s, "t,o(b)", treg, BFD_RELOC_LO16, AT);
252b5132
RH
7661 if (! target_big_endian)
7662 expr1.X_add_number = 0;
7663 else
7664 expr1.X_add_number = off;
67c0d1eb 7665 macro_build (&expr1, s2, "t,o(b)", treg, BFD_RELOC_LO16, AT);
252b5132
RH
7666 break;
7667
7668 case M_USH_A:
d6bc6245 7669 used_at = 1;
67c0d1eb 7670 load_address (AT, &offset_expr, &used_at);
252b5132 7671 if (breg != 0)
67c0d1eb 7672 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
252b5132
RH
7673 if (! target_big_endian)
7674 expr1.X_add_number = 0;
67c0d1eb
RS
7675 macro_build (&expr1, "sb", "t,o(b)", treg, BFD_RELOC_LO16, AT);
7676 macro_build (NULL, "srl", "d,w,<", treg, treg, 8);
252b5132
RH
7677 if (! target_big_endian)
7678 expr1.X_add_number = 1;
7679 else
7680 expr1.X_add_number = 0;
67c0d1eb 7681 macro_build (&expr1, "sb", "t,o(b)", treg, BFD_RELOC_LO16, AT);
252b5132
RH
7682 if (! target_big_endian)
7683 expr1.X_add_number = 0;
7684 else
7685 expr1.X_add_number = 1;
67c0d1eb
RS
7686 macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
7687 macro_build (NULL, "sll", "d,w,<", treg, treg, 8);
7688 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
252b5132
RH
7689 break;
7690
7691 default:
7692 /* FIXME: Check if this is one of the itbl macros, since they
bdaaa2e1 7693 are added dynamically. */
252b5132
RH
7694 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7695 break;
7696 }
7697 if (mips_opts.noat)
7698 as_warn (_("Macro used $at after \".set noat\""));
7699}
7700
7701/* Implement macros in mips16 mode. */
7702
7703static void
17a2f251 7704mips16_macro (struct mips_cl_insn *ip)
252b5132
RH
7705{
7706 int mask;
7707 int xreg, yreg, zreg, tmp;
252b5132
RH
7708 expressionS expr1;
7709 int dbl;
7710 const char *s, *s2, *s3;
7711
7712 mask = ip->insn_mo->mask;
7713
7714 xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
7715 yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
7716 zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
7717
252b5132
RH
7718 expr1.X_op = O_constant;
7719 expr1.X_op_symbol = NULL;
7720 expr1.X_add_symbol = NULL;
7721 expr1.X_add_number = 1;
7722
7723 dbl = 0;
7724
7725 switch (mask)
7726 {
7727 default:
7728 internalError ();
7729
7730 case M_DDIV_3:
7731 dbl = 1;
7732 case M_DIV_3:
7733 s = "mflo";
7734 goto do_div3;
7735 case M_DREM_3:
7736 dbl = 1;
7737 case M_REM_3:
7738 s = "mfhi";
7739 do_div3:
b34976b6 7740 mips_emit_delays (TRUE);
252b5132
RH
7741 ++mips_opts.noreorder;
7742 mips_any_noreorder = 1;
67c0d1eb 7743 macro_build (NULL, dbl ? "ddiv" : "div", "0,x,y", xreg, yreg);
252b5132 7744 expr1.X_add_number = 2;
67c0d1eb
RS
7745 macro_build (&expr1, "bnez", "x,p", yreg);
7746 macro_build (NULL, "break", "6", 7);
bdaaa2e1 7747
252b5132
RH
7748 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7749 since that causes an overflow. We should do that as well,
7750 but I don't see how to do the comparisons without a temporary
7751 register. */
7752 --mips_opts.noreorder;
67c0d1eb 7753 macro_build (NULL, s, "x", zreg);
252b5132
RH
7754 break;
7755
7756 case M_DIVU_3:
7757 s = "divu";
7758 s2 = "mflo";
7759 goto do_divu3;
7760 case M_REMU_3:
7761 s = "divu";
7762 s2 = "mfhi";
7763 goto do_divu3;
7764 case M_DDIVU_3:
7765 s = "ddivu";
7766 s2 = "mflo";
7767 goto do_divu3;
7768 case M_DREMU_3:
7769 s = "ddivu";
7770 s2 = "mfhi";
7771 do_divu3:
b34976b6 7772 mips_emit_delays (TRUE);
252b5132
RH
7773 ++mips_opts.noreorder;
7774 mips_any_noreorder = 1;
67c0d1eb 7775 macro_build (NULL, s, "0,x,y", xreg, yreg);
252b5132 7776 expr1.X_add_number = 2;
67c0d1eb
RS
7777 macro_build (&expr1, "bnez", "x,p", yreg);
7778 macro_build (NULL, "break", "6", 7);
252b5132 7779 --mips_opts.noreorder;
67c0d1eb 7780 macro_build (NULL, s2, "x", zreg);
252b5132
RH
7781 break;
7782
7783 case M_DMUL:
7784 dbl = 1;
7785 case M_MUL:
67c0d1eb
RS
7786 macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
7787 macro_build (NULL, "mflo", "x", zreg);
252b5132
RH
7788 return;
7789
7790 case M_DSUBU_I:
7791 dbl = 1;
7792 goto do_subu;
7793 case M_SUBU_I:
7794 do_subu:
7795 if (imm_expr.X_op != O_constant)
7796 as_bad (_("Unsupported large constant"));
7797 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 7798 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
252b5132
RH
7799 break;
7800
7801 case M_SUBU_I_2:
7802 if (imm_expr.X_op != O_constant)
7803 as_bad (_("Unsupported large constant"));
7804 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 7805 macro_build (&imm_expr, "addiu", "x,k", xreg);
252b5132
RH
7806 break;
7807
7808 case M_DSUBU_I_2:
7809 if (imm_expr.X_op != O_constant)
7810 as_bad (_("Unsupported large constant"));
7811 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 7812 macro_build (&imm_expr, "daddiu", "y,j", yreg);
252b5132
RH
7813 break;
7814
7815 case M_BEQ:
7816 s = "cmp";
7817 s2 = "bteqz";
7818 goto do_branch;
7819 case M_BNE:
7820 s = "cmp";
7821 s2 = "btnez";
7822 goto do_branch;
7823 case M_BLT:
7824 s = "slt";
7825 s2 = "btnez";
7826 goto do_branch;
7827 case M_BLTU:
7828 s = "sltu";
7829 s2 = "btnez";
7830 goto do_branch;
7831 case M_BLE:
7832 s = "slt";
7833 s2 = "bteqz";
7834 goto do_reverse_branch;
7835 case M_BLEU:
7836 s = "sltu";
7837 s2 = "bteqz";
7838 goto do_reverse_branch;
7839 case M_BGE:
7840 s = "slt";
7841 s2 = "bteqz";
7842 goto do_branch;
7843 case M_BGEU:
7844 s = "sltu";
7845 s2 = "bteqz";
7846 goto do_branch;
7847 case M_BGT:
7848 s = "slt";
7849 s2 = "btnez";
7850 goto do_reverse_branch;
7851 case M_BGTU:
7852 s = "sltu";
7853 s2 = "btnez";
7854
7855 do_reverse_branch:
7856 tmp = xreg;
7857 xreg = yreg;
7858 yreg = tmp;
7859
7860 do_branch:
67c0d1eb
RS
7861 macro_build (NULL, s, "x,y", xreg, yreg);
7862 macro_build (&offset_expr, s2, "p");
252b5132
RH
7863 break;
7864
7865 case M_BEQ_I:
7866 s = "cmpi";
7867 s2 = "bteqz";
7868 s3 = "x,U";
7869 goto do_branch_i;
7870 case M_BNE_I:
7871 s = "cmpi";
7872 s2 = "btnez";
7873 s3 = "x,U";
7874 goto do_branch_i;
7875 case M_BLT_I:
7876 s = "slti";
7877 s2 = "btnez";
7878 s3 = "x,8";
7879 goto do_branch_i;
7880 case M_BLTU_I:
7881 s = "sltiu";
7882 s2 = "btnez";
7883 s3 = "x,8";
7884 goto do_branch_i;
7885 case M_BLE_I:
7886 s = "slti";
7887 s2 = "btnez";
7888 s3 = "x,8";
7889 goto do_addone_branch_i;
7890 case M_BLEU_I:
7891 s = "sltiu";
7892 s2 = "btnez";
7893 s3 = "x,8";
7894 goto do_addone_branch_i;
7895 case M_BGE_I:
7896 s = "slti";
7897 s2 = "bteqz";
7898 s3 = "x,8";
7899 goto do_branch_i;
7900 case M_BGEU_I:
7901 s = "sltiu";
7902 s2 = "bteqz";
7903 s3 = "x,8";
7904 goto do_branch_i;
7905 case M_BGT_I:
7906 s = "slti";
7907 s2 = "bteqz";
7908 s3 = "x,8";
7909 goto do_addone_branch_i;
7910 case M_BGTU_I:
7911 s = "sltiu";
7912 s2 = "bteqz";
7913 s3 = "x,8";
7914
7915 do_addone_branch_i:
7916 if (imm_expr.X_op != O_constant)
7917 as_bad (_("Unsupported large constant"));
7918 ++imm_expr.X_add_number;
7919
7920 do_branch_i:
67c0d1eb
RS
7921 macro_build (&imm_expr, s, s3, xreg);
7922 macro_build (&offset_expr, s2, "p");
252b5132
RH
7923 break;
7924
7925 case M_ABS:
7926 expr1.X_add_number = 0;
67c0d1eb 7927 macro_build (&expr1, "slti", "x,8", yreg);
252b5132 7928 if (xreg != yreg)
67c0d1eb 7929 move_register (xreg, yreg);
252b5132 7930 expr1.X_add_number = 2;
67c0d1eb
RS
7931 macro_build (&expr1, "bteqz", "p");
7932 macro_build (NULL, "neg", "x,w", xreg, xreg);
252b5132
RH
7933 }
7934}
7935
7936/* For consistency checking, verify that all bits are specified either
7937 by the match/mask part of the instruction definition, or by the
7938 operand list. */
7939static int
17a2f251 7940validate_mips_insn (const struct mips_opcode *opc)
252b5132
RH
7941{
7942 const char *p = opc->args;
7943 char c;
7944 unsigned long used_bits = opc->mask;
7945
7946 if ((used_bits & opc->match) != opc->match)
7947 {
7948 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
7949 opc->name, opc->args);
7950 return 0;
7951 }
7952#define USE_BITS(mask,shift) (used_bits |= ((mask) << (shift)))
7953 while (*p)
7954 switch (c = *p++)
7955 {
7956 case ',': break;
7957 case '(': break;
7958 case ')': break;
af7ee8bf
CD
7959 case '+':
7960 switch (c = *p++)
7961 {
7962 case 'A': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7963 case 'B': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
7964 case 'C': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
bbcc0807
CD
7965 case 'D': USE_BITS (OP_MASK_RD, OP_SH_RD);
7966 USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
5f74bc13
CD
7967 case 'E': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7968 case 'F': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
7969 case 'G': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
7970 case 'H': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
7971 case 'I': break;
af7ee8bf
CD
7972 default:
7973 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
7974 c, opc->name, opc->args);
7975 return 0;
7976 }
7977 break;
252b5132
RH
7978 case '<': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7979 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7980 case 'A': break;
4372b673 7981 case 'B': USE_BITS (OP_MASK_CODE20, OP_SH_CODE20); break;
252b5132
RH
7982 case 'C': USE_BITS (OP_MASK_COPZ, OP_SH_COPZ); break;
7983 case 'D': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
7984 case 'E': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7985 case 'F': break;
7986 case 'G': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
156c2f8b 7987 case 'H': USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
252b5132 7988 case 'I': break;
e972090a 7989 case 'J': USE_BITS (OP_MASK_CODE19, OP_SH_CODE19); break;
af7ee8bf 7990 case 'K': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
252b5132
RH
7991 case 'L': break;
7992 case 'M': USE_BITS (OP_MASK_CCC, OP_SH_CCC); break;
7993 case 'N': USE_BITS (OP_MASK_BCC, OP_SH_BCC); break;
deec1734
CD
7994 case 'O': USE_BITS (OP_MASK_ALN, OP_SH_ALN); break;
7995 case 'Q': USE_BITS (OP_MASK_VSEL, OP_SH_VSEL);
7996 USE_BITS (OP_MASK_FT, OP_SH_FT); break;
252b5132
RH
7997 case 'R': USE_BITS (OP_MASK_FR, OP_SH_FR); break;
7998 case 'S': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7999 case 'T': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
8000 case 'V': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
8001 case 'W': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
deec1734
CD
8002 case 'X': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
8003 case 'Y': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
8004 case 'Z': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
252b5132
RH
8005 case 'a': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break;
8006 case 'b': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8007 case 'c': USE_BITS (OP_MASK_CODE, OP_SH_CODE); break;
8008 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
8009 case 'f': break;
8010 case 'h': USE_BITS (OP_MASK_PREFX, OP_SH_PREFX); break;
8011 case 'i': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
8012 case 'j': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
8013 case 'k': USE_BITS (OP_MASK_CACHE, OP_SH_CACHE); break;
8014 case 'l': break;
8015 case 'o': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
8016 case 'p': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
8017 case 'q': USE_BITS (OP_MASK_CODE2, OP_SH_CODE2); break;
8018 case 'r': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8019 case 's': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8020 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8021 case 'u': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
8022 case 'v': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8023 case 'w': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8024 case 'x': break;
8025 case 'z': break;
8026 case 'P': USE_BITS (OP_MASK_PERFREG, OP_SH_PERFREG); break;
4372b673
NC
8027 case 'U': USE_BITS (OP_MASK_RD, OP_SH_RD);
8028 USE_BITS (OP_MASK_RT, OP_SH_RT); break;
60b63b72
RS
8029 case 'e': USE_BITS (OP_MASK_VECBYTE, OP_SH_VECBYTE); break;
8030 case '%': USE_BITS (OP_MASK_VECALIGN, OP_SH_VECALIGN); break;
8031 case '[': break;
8032 case ']': break;
252b5132
RH
8033 default:
8034 as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
8035 c, opc->name, opc->args);
8036 return 0;
8037 }
8038#undef USE_BITS
8039 if (used_bits != 0xffffffff)
8040 {
8041 as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
8042 ~used_bits & 0xffffffff, opc->name, opc->args);
8043 return 0;
8044 }
8045 return 1;
8046}
8047
8048/* This routine assembles an instruction into its binary format. As a
8049 side effect, it sets one of the global variables imm_reloc or
8050 offset_reloc to the type of relocation to do if one of the operands
8051 is an address expression. */
8052
8053static void
17a2f251 8054mips_ip (char *str, struct mips_cl_insn *ip)
252b5132
RH
8055{
8056 char *s;
8057 const char *args;
43841e91 8058 char c = 0;
252b5132
RH
8059 struct mips_opcode *insn;
8060 char *argsStart;
8061 unsigned int regno;
8062 unsigned int lastregno = 0;
af7ee8bf 8063 unsigned int lastpos = 0;
071742cf 8064 unsigned int limlo, limhi;
252b5132
RH
8065 char *s_reset;
8066 char save_c = 0;
252b5132
RH
8067
8068 insn_error = NULL;
8069
8070 /* If the instruction contains a '.', we first try to match an instruction
8071 including the '.'. Then we try again without the '.'. */
8072 insn = NULL;
3882b010 8073 for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
252b5132
RH
8074 continue;
8075
8076 /* If we stopped on whitespace, then replace the whitespace with null for
8077 the call to hash_find. Save the character we replaced just in case we
8078 have to re-parse the instruction. */
3882b010 8079 if (ISSPACE (*s))
252b5132
RH
8080 {
8081 save_c = *s;
8082 *s++ = '\0';
8083 }
bdaaa2e1 8084
252b5132
RH
8085 insn = (struct mips_opcode *) hash_find (op_hash, str);
8086
8087 /* If we didn't find the instruction in the opcode table, try again, but
8088 this time with just the instruction up to, but not including the
8089 first '.'. */
8090 if (insn == NULL)
8091 {
bdaaa2e1 8092 /* Restore the character we overwrite above (if any). */
252b5132
RH
8093 if (save_c)
8094 *(--s) = save_c;
8095
8096 /* Scan up to the first '.' or whitespace. */
3882b010
L
8097 for (s = str;
8098 *s != '\0' && *s != '.' && !ISSPACE (*s);
8099 ++s)
252b5132
RH
8100 continue;
8101
8102 /* If we did not find a '.', then we can quit now. */
8103 if (*s != '.')
8104 {
8105 insn_error = "unrecognized opcode";
8106 return;
8107 }
8108
8109 /* Lookup the instruction in the hash table. */
8110 *s++ = '\0';
8111 if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
8112 {
8113 insn_error = "unrecognized opcode";
8114 return;
8115 }
252b5132
RH
8116 }
8117
8118 argsStart = s;
8119 for (;;)
8120 {
b34976b6 8121 bfd_boolean ok;
252b5132
RH
8122
8123 assert (strcmp (insn->name, str) == 0);
8124
1f25f5d3
CD
8125 if (OPCODE_IS_MEMBER (insn,
8126 (mips_opts.isa
3396de36 8127 | (file_ase_mips16 ? INSN_MIPS16 : 0)
deec1734 8128 | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
98d3f06f 8129 | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
fef14a42 8130 mips_opts.arch))
b34976b6 8131 ok = TRUE;
bdaaa2e1 8132 else
b34976b6 8133 ok = FALSE;
bdaaa2e1 8134
252b5132
RH
8135 if (insn->pinfo != INSN_MACRO)
8136 {
fef14a42 8137 if (mips_opts.arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
b34976b6 8138 ok = FALSE;
252b5132
RH
8139 }
8140
8141 if (! ok)
8142 {
8143 if (insn + 1 < &mips_opcodes[NUMOPCODES]
8144 && strcmp (insn->name, insn[1].name) == 0)
8145 {
8146 ++insn;
8147 continue;
8148 }
252b5132 8149 else
beae10d5 8150 {
268f6bed
L
8151 if (!insn_error)
8152 {
8153 static char buf[100];
fef14a42
TS
8154 sprintf (buf,
8155 _("opcode not supported on this processor: %s (%s)"),
8156 mips_cpu_info_from_arch (mips_opts.arch)->name,
8157 mips_cpu_info_from_isa (mips_opts.isa)->name);
268f6bed
L
8158 insn_error = buf;
8159 }
8160 if (save_c)
8161 *(--s) = save_c;
2bd7f1f3 8162 return;
252b5132 8163 }
252b5132
RH
8164 }
8165
8166 ip->insn_mo = insn;
8167 ip->insn_opcode = insn->match;
268f6bed 8168 insn_error = NULL;
252b5132
RH
8169 for (args = insn->args;; ++args)
8170 {
deec1734
CD
8171 int is_mdmx;
8172
ad8d3bb3 8173 s += strspn (s, " \t");
deec1734 8174 is_mdmx = 0;
252b5132
RH
8175 switch (*args)
8176 {
8177 case '\0': /* end of args */
8178 if (*s == '\0')
8179 return;
8180 break;
8181
8182 case ',':
8183 if (*s++ == *args)
8184 continue;
8185 s--;
8186 switch (*++args)
8187 {
8188 case 'r':
8189 case 'v':
38487616 8190 ip->insn_opcode |= lastregno << OP_SH_RS;
252b5132
RH
8191 continue;
8192
8193 case 'w':
38487616
TS
8194 ip->insn_opcode |= lastregno << OP_SH_RT;
8195 continue;
8196
252b5132 8197 case 'W':
38487616 8198 ip->insn_opcode |= lastregno << OP_SH_FT;
252b5132
RH
8199 continue;
8200
8201 case 'V':
38487616 8202 ip->insn_opcode |= lastregno << OP_SH_FS;
252b5132
RH
8203 continue;
8204 }
8205 break;
8206
8207 case '(':
8208 /* Handle optional base register.
8209 Either the base register is omitted or
bdaaa2e1 8210 we must have a left paren. */
252b5132
RH
8211 /* This is dependent on the next operand specifier
8212 is a base register specification. */
8213 assert (args[1] == 'b' || args[1] == '5'
8214 || args[1] == '-' || args[1] == '4');
8215 if (*s == '\0')
8216 return;
8217
8218 case ')': /* these must match exactly */
60b63b72
RS
8219 case '[':
8220 case ']':
252b5132
RH
8221 if (*s++ == *args)
8222 continue;
8223 break;
8224
af7ee8bf
CD
8225 case '+': /* Opcode extension character. */
8226 switch (*++args)
8227 {
071742cf
CD
8228 case 'A': /* ins/ext position, becomes LSB. */
8229 limlo = 0;
8230 limhi = 31;
5f74bc13
CD
8231 goto do_lsb;
8232 case 'E':
8233 limlo = 32;
8234 limhi = 63;
8235 goto do_lsb;
8236do_lsb:
071742cf
CD
8237 my_getExpression (&imm_expr, s);
8238 check_absolute_expr (ip, &imm_expr);
8239 if ((unsigned long) imm_expr.X_add_number < limlo
8240 || (unsigned long) imm_expr.X_add_number > limhi)
8241 {
8242 as_bad (_("Improper position (%lu)"),
8243 (unsigned long) imm_expr.X_add_number);
8244 imm_expr.X_add_number = limlo;
8245 }
8246 lastpos = imm_expr.X_add_number;
8247 ip->insn_opcode |= (imm_expr.X_add_number
8248 & OP_MASK_SHAMT) << OP_SH_SHAMT;
8249 imm_expr.X_op = O_absent;
8250 s = expr_end;
8251 continue;
8252
8253 case 'B': /* ins size, becomes MSB. */
8254 limlo = 1;
8255 limhi = 32;
5f74bc13
CD
8256 goto do_msb;
8257 case 'F':
8258 limlo = 33;
8259 limhi = 64;
8260 goto do_msb;
8261do_msb:
071742cf
CD
8262 my_getExpression (&imm_expr, s);
8263 check_absolute_expr (ip, &imm_expr);
8264 /* Check for negative input so that small negative numbers
8265 will not succeed incorrectly. The checks against
8266 (pos+size) transitively check "size" itself,
8267 assuming that "pos" is reasonable. */
8268 if ((long) imm_expr.X_add_number < 0
8269 || ((unsigned long) imm_expr.X_add_number
8270 + lastpos) < limlo
8271 || ((unsigned long) imm_expr.X_add_number
8272 + lastpos) > limhi)
8273 {
8274 as_bad (_("Improper insert size (%lu, position %lu)"),
8275 (unsigned long) imm_expr.X_add_number,
8276 (unsigned long) lastpos);
8277 imm_expr.X_add_number = limlo - lastpos;
8278 }
8279 ip->insn_opcode |= ((lastpos + imm_expr.X_add_number - 1)
8280 & OP_MASK_INSMSB) << OP_SH_INSMSB;
8281 imm_expr.X_op = O_absent;
8282 s = expr_end;
8283 continue;
8284
8285 case 'C': /* ext size, becomes MSBD. */
8286 limlo = 1;
8287 limhi = 32;
5f74bc13
CD
8288 goto do_msbd;
8289 case 'G':
8290 limlo = 33;
8291 limhi = 64;
8292 goto do_msbd;
8293 case 'H':
8294 limlo = 33;
8295 limhi = 64;
8296 goto do_msbd;
8297do_msbd:
071742cf
CD
8298 my_getExpression (&imm_expr, s);
8299 check_absolute_expr (ip, &imm_expr);
8300 /* Check for negative input so that small negative numbers
8301 will not succeed incorrectly. The checks against
8302 (pos+size) transitively check "size" itself,
8303 assuming that "pos" is reasonable. */
8304 if ((long) imm_expr.X_add_number < 0
8305 || ((unsigned long) imm_expr.X_add_number
8306 + lastpos) < limlo
8307 || ((unsigned long) imm_expr.X_add_number
8308 + lastpos) > limhi)
8309 {
8310 as_bad (_("Improper extract size (%lu, position %lu)"),
8311 (unsigned long) imm_expr.X_add_number,
8312 (unsigned long) lastpos);
8313 imm_expr.X_add_number = limlo - lastpos;
8314 }
8315 ip->insn_opcode |= ((imm_expr.X_add_number - 1)
8316 & OP_MASK_EXTMSBD) << OP_SH_EXTMSBD;
8317 imm_expr.X_op = O_absent;
8318 s = expr_end;
8319 continue;
af7ee8bf 8320
bbcc0807
CD
8321 case 'D':
8322 /* +D is for disassembly only; never match. */
8323 break;
8324
5f74bc13
CD
8325 case 'I':
8326 /* "+I" is like "I", except that imm2_expr is used. */
8327 my_getExpression (&imm2_expr, s);
8328 if (imm2_expr.X_op != O_big
8329 && imm2_expr.X_op != O_constant)
8330 insn_error = _("absolute expression required");
13757d0c 8331 normalize_constant_expr (&imm2_expr);
5f74bc13
CD
8332 s = expr_end;
8333 continue;
8334
af7ee8bf
CD
8335 default:
8336 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8337 *args, insn->name, insn->args);
8338 /* Further processing is fruitless. */
8339 return;
8340 }
8341 break;
8342
252b5132
RH
8343 case '<': /* must be at least one digit */
8344 /*
8345 * According to the manual, if the shift amount is greater
b6ff326e
KH
8346 * than 31 or less than 0, then the shift amount should be
8347 * mod 32. In reality the mips assembler issues an error.
252b5132
RH
8348 * We issue a warning and mask out all but the low 5 bits.
8349 */
8350 my_getExpression (&imm_expr, s);
8351 check_absolute_expr (ip, &imm_expr);
8352 if ((unsigned long) imm_expr.X_add_number > 31)
8353 {
793b27f4
TS
8354 as_warn (_("Improper shift amount (%lu)"),
8355 (unsigned long) imm_expr.X_add_number);
38487616 8356 imm_expr.X_add_number &= OP_MASK_SHAMT;
252b5132 8357 }
38487616 8358 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SHAMT;
252b5132
RH
8359 imm_expr.X_op = O_absent;
8360 s = expr_end;
8361 continue;
8362
8363 case '>': /* shift amount minus 32 */
8364 my_getExpression (&imm_expr, s);
8365 check_absolute_expr (ip, &imm_expr);
8366 if ((unsigned long) imm_expr.X_add_number < 32
8367 || (unsigned long) imm_expr.X_add_number > 63)
8368 break;
38487616 8369 ip->insn_opcode |= (imm_expr.X_add_number - 32) << OP_SH_SHAMT;
252b5132
RH
8370 imm_expr.X_op = O_absent;
8371 s = expr_end;
8372 continue;
8373
252b5132
RH
8374 case 'k': /* cache code */
8375 case 'h': /* prefx code */
8376 my_getExpression (&imm_expr, s);
8377 check_absolute_expr (ip, &imm_expr);
8378 if ((unsigned long) imm_expr.X_add_number > 31)
8379 {
8380 as_warn (_("Invalid value for `%s' (%lu)"),
8381 ip->insn_mo->name,
8382 (unsigned long) imm_expr.X_add_number);
8383 imm_expr.X_add_number &= 0x1f;
8384 }
8385 if (*args == 'k')
8386 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
8387 else
8388 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
8389 imm_expr.X_op = O_absent;
8390 s = expr_end;
8391 continue;
8392
8393 case 'c': /* break code */
8394 my_getExpression (&imm_expr, s);
8395 check_absolute_expr (ip, &imm_expr);
793b27f4 8396 if ((unsigned long) imm_expr.X_add_number > 1023)
252b5132 8397 {
793b27f4
TS
8398 as_warn (_("Illegal break code (%lu)"),
8399 (unsigned long) imm_expr.X_add_number);
38487616 8400 imm_expr.X_add_number &= OP_MASK_CODE;
252b5132 8401 }
38487616 8402 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE;
252b5132
RH
8403 imm_expr.X_op = O_absent;
8404 s = expr_end;
8405 continue;
8406
8407 case 'q': /* lower break code */
8408 my_getExpression (&imm_expr, s);
8409 check_absolute_expr (ip, &imm_expr);
793b27f4 8410 if ((unsigned long) imm_expr.X_add_number > 1023)
252b5132 8411 {
793b27f4
TS
8412 as_warn (_("Illegal lower break code (%lu)"),
8413 (unsigned long) imm_expr.X_add_number);
38487616 8414 imm_expr.X_add_number &= OP_MASK_CODE2;
252b5132 8415 }
38487616 8416 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE2;
252b5132
RH
8417 imm_expr.X_op = O_absent;
8418 s = expr_end;
8419 continue;
8420
4372b673 8421 case 'B': /* 20-bit syscall/break code. */
156c2f8b 8422 my_getExpression (&imm_expr, s);
156c2f8b 8423 check_absolute_expr (ip, &imm_expr);
793b27f4
TS
8424 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
8425 as_warn (_("Illegal 20-bit code (%lu)"),
8426 (unsigned long) imm_expr.X_add_number);
38487616 8427 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE20;
252b5132
RH
8428 imm_expr.X_op = O_absent;
8429 s = expr_end;
8430 continue;
8431
98d3f06f 8432 case 'C': /* Coprocessor code */
beae10d5 8433 my_getExpression (&imm_expr, s);
252b5132 8434 check_absolute_expr (ip, &imm_expr);
98d3f06f 8435 if ((unsigned long) imm_expr.X_add_number >= (1 << 25))
252b5132 8436 {
793b27f4
TS
8437 as_warn (_("Coproccesor code > 25 bits (%lu)"),
8438 (unsigned long) imm_expr.X_add_number);
98d3f06f 8439 imm_expr.X_add_number &= ((1 << 25) - 1);
252b5132 8440 }
beae10d5
KH
8441 ip->insn_opcode |= imm_expr.X_add_number;
8442 imm_expr.X_op = O_absent;
8443 s = expr_end;
8444 continue;
252b5132 8445
4372b673
NC
8446 case 'J': /* 19-bit wait code. */
8447 my_getExpression (&imm_expr, s);
8448 check_absolute_expr (ip, &imm_expr);
793b27f4
TS
8449 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
8450 as_warn (_("Illegal 19-bit code (%lu)"),
8451 (unsigned long) imm_expr.X_add_number);
38487616 8452 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE19;
4372b673
NC
8453 imm_expr.X_op = O_absent;
8454 s = expr_end;
8455 continue;
8456
252b5132 8457 case 'P': /* Performance register */
beae10d5 8458 my_getExpression (&imm_expr, s);
252b5132 8459 check_absolute_expr (ip, &imm_expr);
beae10d5 8460 if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
252b5132 8461 {
793b27f4
TS
8462 as_warn (_("Invalid performance register (%lu)"),
8463 (unsigned long) imm_expr.X_add_number);
38487616 8464 imm_expr.X_add_number &= OP_MASK_PERFREG;
252b5132 8465 }
38487616 8466 ip->insn_opcode |= (imm_expr.X_add_number << OP_SH_PERFREG);
beae10d5
KH
8467 imm_expr.X_op = O_absent;
8468 s = expr_end;
8469 continue;
252b5132
RH
8470
8471 case 'b': /* base register */
8472 case 'd': /* destination register */
8473 case 's': /* source register */
8474 case 't': /* target register */
8475 case 'r': /* both target and source */
8476 case 'v': /* both dest and source */
8477 case 'w': /* both dest and target */
8478 case 'E': /* coprocessor target register */
8479 case 'G': /* coprocessor destination register */
af7ee8bf 8480 case 'K': /* 'rdhwr' destination register */
252b5132
RH
8481 case 'x': /* ignore register name */
8482 case 'z': /* must be zero register */
4372b673 8483 case 'U': /* destination register (clo/clz). */
252b5132
RH
8484 s_reset = s;
8485 if (s[0] == '$')
8486 {
8487
3882b010 8488 if (ISDIGIT (s[1]))
252b5132
RH
8489 {
8490 ++s;
8491 regno = 0;
8492 do
8493 {
8494 regno *= 10;
8495 regno += *s - '0';
8496 ++s;
8497 }
3882b010 8498 while (ISDIGIT (*s));
252b5132
RH
8499 if (regno > 31)
8500 as_bad (_("Invalid register number (%d)"), regno);
8501 }
af7ee8bf 8502 else if (*args == 'E' || *args == 'G' || *args == 'K')
252b5132
RH
8503 goto notreg;
8504 else
8505 {
76db943d
TS
8506 if (s[1] == 'r' && s[2] == 'a')
8507 {
8508 s += 3;
8509 regno = RA;
8510 }
8511 else if (s[1] == 'f' && s[2] == 'p')
252b5132
RH
8512 {
8513 s += 3;
8514 regno = FP;
8515 }
8516 else if (s[1] == 's' && s[2] == 'p')
8517 {
8518 s += 3;
8519 regno = SP;
8520 }
8521 else if (s[1] == 'g' && s[2] == 'p')
8522 {
8523 s += 3;
8524 regno = GP;
8525 }
8526 else if (s[1] == 'a' && s[2] == 't')
8527 {
8528 s += 3;
8529 regno = AT;
8530 }
8531 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8532 {
8533 s += 4;
8534 regno = KT0;
8535 }
8536 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8537 {
8538 s += 4;
8539 regno = KT1;
8540 }
85b51719
TS
8541 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
8542 {
8543 s += 5;
8544 regno = ZERO;
8545 }
252b5132
RH
8546 else if (itbl_have_entries)
8547 {
8548 char *p, *n;
d7ba4a77 8549 unsigned long r;
252b5132 8550
d7ba4a77 8551 p = s + 1; /* advance past '$' */
252b5132
RH
8552 n = itbl_get_field (&p); /* n is name */
8553
d7ba4a77
ILT
8554 /* See if this is a register defined in an
8555 itbl entry. */
8556 if (itbl_get_reg_val (n, &r))
252b5132
RH
8557 {
8558 /* Get_field advances to the start of
8559 the next field, so we need to back
d7ba4a77 8560 rack to the end of the last field. */
bdaaa2e1 8561 if (p)
252b5132 8562 s = p - 1;
bdaaa2e1 8563 else
d7ba4a77 8564 s = strchr (s, '\0');
252b5132
RH
8565 regno = r;
8566 }
8567 else
8568 goto notreg;
beae10d5 8569 }
252b5132
RH
8570 else
8571 goto notreg;
8572 }
8573 if (regno == AT
8574 && ! mips_opts.noat
8575 && *args != 'E'
af7ee8bf
CD
8576 && *args != 'G'
8577 && *args != 'K')
252b5132
RH
8578 as_warn (_("Used $at without \".set noat\""));
8579 c = *args;
8580 if (*s == ' ')
f9419b05 8581 ++s;
252b5132
RH
8582 if (args[1] != *s)
8583 {
8584 if (c == 'r' || c == 'v' || c == 'w')
8585 {
8586 regno = lastregno;
8587 s = s_reset;
f9419b05 8588 ++args;
252b5132
RH
8589 }
8590 }
8591 /* 'z' only matches $0. */
8592 if (c == 'z' && regno != 0)
8593 break;
8594
bdaaa2e1
KH
8595 /* Now that we have assembled one operand, we use the args string
8596 * to figure out where it goes in the instruction. */
252b5132
RH
8597 switch (c)
8598 {
8599 case 'r':
8600 case 's':
8601 case 'v':
8602 case 'b':
38487616 8603 ip->insn_opcode |= regno << OP_SH_RS;
252b5132
RH
8604 break;
8605 case 'd':
8606 case 'G':
af7ee8bf 8607 case 'K':
38487616 8608 ip->insn_opcode |= regno << OP_SH_RD;
252b5132 8609 break;
4372b673 8610 case 'U':
38487616
TS
8611 ip->insn_opcode |= regno << OP_SH_RD;
8612 ip->insn_opcode |= regno << OP_SH_RT;
4372b673 8613 break;
252b5132
RH
8614 case 'w':
8615 case 't':
8616 case 'E':
38487616 8617 ip->insn_opcode |= regno << OP_SH_RT;
252b5132
RH
8618 break;
8619 case 'x':
8620 /* This case exists because on the r3000 trunc
8621 expands into a macro which requires a gp
8622 register. On the r6000 or r4000 it is
8623 assembled into a single instruction which
8624 ignores the register. Thus the insn version
8625 is MIPS_ISA2 and uses 'x', and the macro
8626 version is MIPS_ISA1 and uses 't'. */
8627 break;
8628 case 'z':
8629 /* This case is for the div instruction, which
8630 acts differently if the destination argument
8631 is $0. This only matches $0, and is checked
8632 outside the switch. */
8633 break;
8634 case 'D':
8635 /* Itbl operand; not yet implemented. FIXME ?? */
8636 break;
8637 /* What about all other operands like 'i', which
8638 can be specified in the opcode table? */
8639 }
8640 lastregno = regno;
8641 continue;
8642 }
8643 notreg:
8644 switch (*args++)
8645 {
8646 case 'r':
8647 case 'v':
38487616 8648 ip->insn_opcode |= lastregno << OP_SH_RS;
252b5132
RH
8649 continue;
8650 case 'w':
38487616 8651 ip->insn_opcode |= lastregno << OP_SH_RT;
252b5132
RH
8652 continue;
8653 }
8654 break;
8655
deec1734
CD
8656 case 'O': /* MDMX alignment immediate constant. */
8657 my_getExpression (&imm_expr, s);
8658 check_absolute_expr (ip, &imm_expr);
8659 if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
8660 {
8661 as_warn ("Improper align amount (%ld), using low bits",
8662 (long) imm_expr.X_add_number);
8663 imm_expr.X_add_number &= OP_MASK_ALN;
8664 }
8665 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_ALN;
8666 imm_expr.X_op = O_absent;
8667 s = expr_end;
8668 continue;
8669
8670 case 'Q': /* MDMX vector, element sel, or const. */
8671 if (s[0] != '$')
8672 {
8673 /* MDMX Immediate. */
8674 my_getExpression (&imm_expr, s);
8675 check_absolute_expr (ip, &imm_expr);
8676 if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
8677 {
8678 as_warn (_("Invalid MDMX Immediate (%ld)"),
8679 (long) imm_expr.X_add_number);
8680 imm_expr.X_add_number &= OP_MASK_FT;
8681 }
8682 imm_expr.X_add_number &= OP_MASK_FT;
8683 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8684 ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
8685 else
8686 ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
8687 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_FT;
8688 imm_expr.X_op = O_absent;
8689 s = expr_end;
8690 continue;
8691 }
8692 /* Not MDMX Immediate. Fall through. */
8693 case 'X': /* MDMX destination register. */
8694 case 'Y': /* MDMX source register. */
8695 case 'Z': /* MDMX target register. */
8696 is_mdmx = 1;
252b5132
RH
8697 case 'D': /* floating point destination register */
8698 case 'S': /* floating point source register */
8699 case 'T': /* floating point target register */
8700 case 'R': /* floating point source register */
8701 case 'V':
8702 case 'W':
8703 s_reset = s;
deec1734
CD
8704 /* Accept $fN for FP and MDMX register numbers, and in
8705 addition accept $vN for MDMX register numbers. */
8706 if ((s[0] == '$' && s[1] == 'f' && ISDIGIT (s[2]))
8707 || (is_mdmx != 0 && s[0] == '$' && s[1] == 'v'
8708 && ISDIGIT (s[2])))
252b5132
RH
8709 {
8710 s += 2;
8711 regno = 0;
8712 do
8713 {
8714 regno *= 10;
8715 regno += *s - '0';
8716 ++s;
8717 }
3882b010 8718 while (ISDIGIT (*s));
252b5132
RH
8719
8720 if (regno > 31)
8721 as_bad (_("Invalid float register number (%d)"), regno);
8722
8723 if ((regno & 1) != 0
ca4e0257 8724 && HAVE_32BIT_FPRS
252b5132
RH
8725 && ! (strcmp (str, "mtc1") == 0
8726 || strcmp (str, "mfc1") == 0
8727 || strcmp (str, "lwc1") == 0
8728 || strcmp (str, "swc1") == 0
8729 || strcmp (str, "l.s") == 0
8730 || strcmp (str, "s.s") == 0))
8731 as_warn (_("Float register should be even, was %d"),
8732 regno);
8733
8734 c = *args;
8735 if (*s == ' ')
f9419b05 8736 ++s;
252b5132
RH
8737 if (args[1] != *s)
8738 {
8739 if (c == 'V' || c == 'W')
8740 {
8741 regno = lastregno;
8742 s = s_reset;
f9419b05 8743 ++args;
252b5132
RH
8744 }
8745 }
8746 switch (c)
8747 {
8748 case 'D':
deec1734 8749 case 'X':
38487616 8750 ip->insn_opcode |= regno << OP_SH_FD;
252b5132
RH
8751 break;
8752 case 'V':
8753 case 'S':
deec1734 8754 case 'Y':
38487616 8755 ip->insn_opcode |= regno << OP_SH_FS;
252b5132 8756 break;
deec1734
CD
8757 case 'Q':
8758 /* This is like 'Z', but also needs to fix the MDMX
8759 vector/scalar select bits. Note that the
8760 scalar immediate case is handled above. */
8761 if (*s == '[')
8762 {
8763 int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
8764 int max_el = (is_qh ? 3 : 7);
8765 s++;
8766 my_getExpression(&imm_expr, s);
8767 check_absolute_expr (ip, &imm_expr);
8768 s = expr_end;
8769 if (imm_expr.X_add_number > max_el)
8770 as_bad(_("Bad element selector %ld"),
8771 (long) imm_expr.X_add_number);
8772 imm_expr.X_add_number &= max_el;
8773 ip->insn_opcode |= (imm_expr.X_add_number
8774 << (OP_SH_VSEL +
8775 (is_qh ? 2 : 1)));
8776 if (*s != ']')
8777 as_warn(_("Expecting ']' found '%s'"), s);
8778 else
8779 s++;
8780 }
8781 else
8782 {
8783 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8784 ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
8785 << OP_SH_VSEL);
8786 else
8787 ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
8788 OP_SH_VSEL);
8789 }
8790 /* Fall through */
252b5132
RH
8791 case 'W':
8792 case 'T':
deec1734 8793 case 'Z':
38487616 8794 ip->insn_opcode |= regno << OP_SH_FT;
252b5132
RH
8795 break;
8796 case 'R':
38487616 8797 ip->insn_opcode |= regno << OP_SH_FR;
252b5132
RH
8798 break;
8799 }
8800 lastregno = regno;
8801 continue;
8802 }
8803
252b5132
RH
8804 switch (*args++)
8805 {
8806 case 'V':
38487616 8807 ip->insn_opcode |= lastregno << OP_SH_FS;
252b5132
RH
8808 continue;
8809 case 'W':
38487616 8810 ip->insn_opcode |= lastregno << OP_SH_FT;
252b5132
RH
8811 continue;
8812 }
8813 break;
8814
8815 case 'I':
8816 my_getExpression (&imm_expr, s);
8817 if (imm_expr.X_op != O_big
8818 && imm_expr.X_op != O_constant)
8819 insn_error = _("absolute expression required");
13757d0c 8820 normalize_constant_expr (&imm_expr);
252b5132
RH
8821 s = expr_end;
8822 continue;
8823
8824 case 'A':
8825 my_getExpression (&offset_expr, s);
f6688943 8826 *imm_reloc = BFD_RELOC_32;
252b5132
RH
8827 s = expr_end;
8828 continue;
8829
8830 case 'F':
8831 case 'L':
8832 case 'f':
8833 case 'l':
8834 {
8835 int f64;
ca4e0257 8836 int using_gprs;
252b5132
RH
8837 char *save_in;
8838 char *err;
8839 unsigned char temp[8];
8840 int len;
8841 unsigned int length;
8842 segT seg;
8843 subsegT subseg;
8844 char *p;
8845
8846 /* These only appear as the last operand in an
8847 instruction, and every instruction that accepts
8848 them in any variant accepts them in all variants.
8849 This means we don't have to worry about backing out
8850 any changes if the instruction does not match.
8851
8852 The difference between them is the size of the
8853 floating point constant and where it goes. For 'F'
8854 and 'L' the constant is 64 bits; for 'f' and 'l' it
8855 is 32 bits. Where the constant is placed is based
8856 on how the MIPS assembler does things:
8857 F -- .rdata
8858 L -- .lit8
8859 f -- immediate value
8860 l -- .lit4
8861
8862 The .lit4 and .lit8 sections are only used if
8863 permitted by the -G argument.
8864
8865 When generating embedded PIC code, we use the
8866 .lit8 section but not the .lit4 section (we can do
8867 .lit4 inline easily; we need to put .lit8
8868 somewhere in the data segment, and using .lit8
8869 permits the linker to eventually combine identical
ca4e0257
RS
8870 .lit8 entries).
8871
8872 The code below needs to know whether the target register
8873 is 32 or 64 bits wide. It relies on the fact 'f' and
8874 'F' are used with GPR-based instructions and 'l' and
8875 'L' are used with FPR-based instructions. */
252b5132
RH
8876
8877 f64 = *args == 'F' || *args == 'L';
ca4e0257 8878 using_gprs = *args == 'F' || *args == 'f';
252b5132
RH
8879
8880 save_in = input_line_pointer;
8881 input_line_pointer = s;
8882 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
8883 length = len;
8884 s = input_line_pointer;
8885 input_line_pointer = save_in;
8886 if (err != NULL && *err != '\0')
8887 {
8888 as_bad (_("Bad floating point constant: %s"), err);
8889 memset (temp, '\0', sizeof temp);
8890 length = f64 ? 8 : 4;
8891 }
8892
156c2f8b 8893 assert (length == (unsigned) (f64 ? 8 : 4));
252b5132
RH
8894
8895 if (*args == 'f'
8896 || (*args == 'l'
8897 && (! USE_GLOBAL_POINTER_OPT
8898 || mips_pic == EMBEDDED_PIC
8899 || g_switch_value < 4
8900 || (temp[0] == 0 && temp[1] == 0)
8901 || (temp[2] == 0 && temp[3] == 0))))
8902 {
8903 imm_expr.X_op = O_constant;
8904 if (! target_big_endian)
8905 imm_expr.X_add_number = bfd_getl32 (temp);
8906 else
8907 imm_expr.X_add_number = bfd_getb32 (temp);
8908 }
8909 else if (length > 4
119d663a 8910 && ! mips_disable_float_construction
ca4e0257
RS
8911 /* Constants can only be constructed in GPRs and
8912 copied to FPRs if the GPRs are at least as wide
8913 as the FPRs. Force the constant into memory if
8914 we are using 64-bit FPRs but the GPRs are only
8915 32 bits wide. */
8916 && (using_gprs
8917 || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
252b5132
RH
8918 && ((temp[0] == 0 && temp[1] == 0)
8919 || (temp[2] == 0 && temp[3] == 0))
8920 && ((temp[4] == 0 && temp[5] == 0)
8921 || (temp[6] == 0 && temp[7] == 0)))
8922 {
ca4e0257
RS
8923 /* The value is simple enough to load with a couple of
8924 instructions. If using 32-bit registers, set
8925 imm_expr to the high order 32 bits and offset_expr to
8926 the low order 32 bits. Otherwise, set imm_expr to
8927 the entire 64 bit constant. */
8928 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
252b5132
RH
8929 {
8930 imm_expr.X_op = O_constant;
8931 offset_expr.X_op = O_constant;
8932 if (! target_big_endian)
8933 {
8934 imm_expr.X_add_number = bfd_getl32 (temp + 4);
8935 offset_expr.X_add_number = bfd_getl32 (temp);
8936 }
8937 else
8938 {
8939 imm_expr.X_add_number = bfd_getb32 (temp);
8940 offset_expr.X_add_number = bfd_getb32 (temp + 4);
8941 }
8942 if (offset_expr.X_add_number == 0)
8943 offset_expr.X_op = O_absent;
8944 }
8945 else if (sizeof (imm_expr.X_add_number) > 4)
8946 {
8947 imm_expr.X_op = O_constant;
8948 if (! target_big_endian)
8949 imm_expr.X_add_number = bfd_getl64 (temp);
8950 else
8951 imm_expr.X_add_number = bfd_getb64 (temp);
8952 }
8953 else
8954 {
8955 imm_expr.X_op = O_big;
8956 imm_expr.X_add_number = 4;
8957 if (! target_big_endian)
8958 {
8959 generic_bignum[0] = bfd_getl16 (temp);
8960 generic_bignum[1] = bfd_getl16 (temp + 2);
8961 generic_bignum[2] = bfd_getl16 (temp + 4);
8962 generic_bignum[3] = bfd_getl16 (temp + 6);
8963 }
8964 else
8965 {
8966 generic_bignum[0] = bfd_getb16 (temp + 6);
8967 generic_bignum[1] = bfd_getb16 (temp + 4);
8968 generic_bignum[2] = bfd_getb16 (temp + 2);
8969 generic_bignum[3] = bfd_getb16 (temp);
8970 }
8971 }
8972 }
8973 else
8974 {
8975 const char *newname;
8976 segT new_seg;
8977
8978 /* Switch to the right section. */
8979 seg = now_seg;
8980 subseg = now_subseg;
8981 switch (*args)
8982 {
8983 default: /* unused default case avoids warnings. */
8984 case 'L':
8985 newname = RDATA_SECTION_NAME;
bb2d6cd7
GK
8986 if ((USE_GLOBAL_POINTER_OPT && g_switch_value >= 8)
8987 || mips_pic == EMBEDDED_PIC)
252b5132
RH
8988 newname = ".lit8";
8989 break;
8990 case 'F':
bb2d6cd7
GK
8991 if (mips_pic == EMBEDDED_PIC)
8992 newname = ".lit8";
8993 else
8994 newname = RDATA_SECTION_NAME;
252b5132
RH
8995 break;
8996 case 'l':
8997 assert (!USE_GLOBAL_POINTER_OPT
8998 || g_switch_value >= 4);
8999 newname = ".lit4";
9000 break;
9001 }
9002 new_seg = subseg_new (newname, (subsegT) 0);
9003 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
9004 bfd_set_section_flags (stdoutput, new_seg,
9005 (SEC_ALLOC
9006 | SEC_LOAD
9007 | SEC_READONLY
9008 | SEC_DATA));
9009 frag_align (*args == 'l' ? 2 : 3, 0, 0);
9010 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
9011 && strcmp (TARGET_OS, "elf") != 0)
9012 record_alignment (new_seg, 4);
9013 else
9014 record_alignment (new_seg, *args == 'l' ? 2 : 3);
9015 if (seg == now_seg)
9016 as_bad (_("Can't use floating point insn in this section"));
9017
9018 /* Set the argument to the current address in the
9019 section. */
9020 offset_expr.X_op = O_symbol;
9021 offset_expr.X_add_symbol =
9022 symbol_new ("L0\001", now_seg,
9023 (valueT) frag_now_fix (), frag_now);
9024 offset_expr.X_add_number = 0;
9025
9026 /* Put the floating point number into the section. */
9027 p = frag_more ((int) length);
9028 memcpy (p, temp, length);
9029
9030 /* Switch back to the original section. */
9031 subseg_set (seg, subseg);
9032 }
9033 }
9034 continue;
9035
9036 case 'i': /* 16 bit unsigned immediate */
9037 case 'j': /* 16 bit signed immediate */
f6688943 9038 *imm_reloc = BFD_RELOC_LO16;
5e0116d5 9039 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0)
252b5132
RH
9040 {
9041 int more;
5e0116d5
RS
9042 offsetT minval, maxval;
9043
9044 more = (insn + 1 < &mips_opcodes[NUMOPCODES]
9045 && strcmp (insn->name, insn[1].name) == 0);
9046
9047 /* If the expression was written as an unsigned number,
9048 only treat it as signed if there are no more
9049 alternatives. */
9050 if (more
9051 && *args == 'j'
9052 && sizeof (imm_expr.X_add_number) <= 4
9053 && imm_expr.X_op == O_constant
9054 && imm_expr.X_add_number < 0
9055 && imm_expr.X_unsigned
9056 && HAVE_64BIT_GPRS)
9057 break;
9058
9059 /* For compatibility with older assemblers, we accept
9060 0x8000-0xffff as signed 16-bit numbers when only
9061 signed numbers are allowed. */
9062 if (*args == 'i')
9063 minval = 0, maxval = 0xffff;
9064 else if (more)
9065 minval = -0x8000, maxval = 0x7fff;
252b5132 9066 else
5e0116d5
RS
9067 minval = -0x8000, maxval = 0xffff;
9068
9069 if (imm_expr.X_op != O_constant
9070 || imm_expr.X_add_number < minval
9071 || imm_expr.X_add_number > maxval)
252b5132
RH
9072 {
9073 if (more)
9074 break;
2ae7e77b
AH
9075 if (imm_expr.X_op == O_constant
9076 || imm_expr.X_op == O_big)
5e0116d5 9077 as_bad (_("expression out of range"));
252b5132
RH
9078 }
9079 }
9080 s = expr_end;
9081 continue;
9082
9083 case 'o': /* 16 bit offset */
5e0116d5
RS
9084 /* Check whether there is only a single bracketed expression
9085 left. If so, it must be the base register and the
9086 constant must be zero. */
9087 if (*s == '(' && strchr (s + 1, '(') == 0)
9088 {
9089 offset_expr.X_op = O_constant;
9090 offset_expr.X_add_number = 0;
9091 continue;
9092 }
252b5132
RH
9093
9094 /* If this value won't fit into a 16 bit offset, then go
9095 find a macro that will generate the 32 bit offset
afdbd6d0 9096 code pattern. */
5e0116d5 9097 if (my_getSmallExpression (&offset_expr, offset_reloc, s) == 0
252b5132
RH
9098 && (offset_expr.X_op != O_constant
9099 || offset_expr.X_add_number >= 0x8000
afdbd6d0 9100 || offset_expr.X_add_number < -0x8000))
252b5132
RH
9101 break;
9102
252b5132
RH
9103 s = expr_end;
9104 continue;
9105
9106 case 'p': /* pc relative offset */
0b25d3e6 9107 *offset_reloc = BFD_RELOC_16_PCREL_S2;
252b5132
RH
9108 my_getExpression (&offset_expr, s);
9109 s = expr_end;
9110 continue;
9111
9112 case 'u': /* upper 16 bits */
5e0116d5
RS
9113 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0
9114 && imm_expr.X_op == O_constant
9115 && (imm_expr.X_add_number < 0
9116 || imm_expr.X_add_number >= 0x10000))
252b5132
RH
9117 as_bad (_("lui expression not in range 0..65535"));
9118 s = expr_end;
9119 continue;
9120
9121 case 'a': /* 26 bit address */
9122 my_getExpression (&offset_expr, s);
9123 s = expr_end;
f6688943 9124 *offset_reloc = BFD_RELOC_MIPS_JMP;
252b5132
RH
9125 continue;
9126
9127 case 'N': /* 3 bit branch condition code */
9128 case 'M': /* 3 bit compare condition code */
9129 if (strncmp (s, "$fcc", 4) != 0)
9130 break;
9131 s += 4;
9132 regno = 0;
9133 do
9134 {
9135 regno *= 10;
9136 regno += *s - '0';
9137 ++s;
9138 }
3882b010 9139 while (ISDIGIT (*s));
252b5132 9140 if (regno > 7)
30c378fd
CD
9141 as_bad (_("Invalid condition code register $fcc%d"), regno);
9142 if ((strcmp(str + strlen(str) - 3, ".ps") == 0
9143 || strcmp(str + strlen(str) - 5, "any2f") == 0
9144 || strcmp(str + strlen(str) - 5, "any2t") == 0)
9145 && (regno & 1) != 0)
9146 as_warn(_("Condition code register should be even for %s, was %d"),
9147 str, regno);
9148 if ((strcmp(str + strlen(str) - 5, "any4f") == 0
9149 || strcmp(str + strlen(str) - 5, "any4t") == 0)
9150 && (regno & 3) != 0)
9151 as_warn(_("Condition code register should be 0 or 4 for %s, was %d"),
9152 str, regno);
252b5132
RH
9153 if (*args == 'N')
9154 ip->insn_opcode |= regno << OP_SH_BCC;
9155 else
9156 ip->insn_opcode |= regno << OP_SH_CCC;
beae10d5 9157 continue;
252b5132 9158
156c2f8b
NC
9159 case 'H':
9160 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
9161 s += 2;
3882b010 9162 if (ISDIGIT (*s))
156c2f8b
NC
9163 {
9164 c = 0;
9165 do
9166 {
9167 c *= 10;
9168 c += *s - '0';
9169 ++s;
9170 }
3882b010 9171 while (ISDIGIT (*s));
156c2f8b
NC
9172 }
9173 else
9174 c = 8; /* Invalid sel value. */
9175
9176 if (c > 7)
9177 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
9178 ip->insn_opcode |= c;
9179 continue;
9180
60b63b72
RS
9181 case 'e':
9182 /* Must be at least one digit. */
9183 my_getExpression (&imm_expr, s);
9184 check_absolute_expr (ip, &imm_expr);
9185
9186 if ((unsigned long) imm_expr.X_add_number
9187 > (unsigned long) OP_MASK_VECBYTE)
9188 {
9189 as_bad (_("bad byte vector index (%ld)"),
9190 (long) imm_expr.X_add_number);
9191 imm_expr.X_add_number = 0;
9192 }
9193
9194 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECBYTE;
9195 imm_expr.X_op = O_absent;
9196 s = expr_end;
9197 continue;
9198
9199 case '%':
9200 my_getExpression (&imm_expr, s);
9201 check_absolute_expr (ip, &imm_expr);
9202
9203 if ((unsigned long) imm_expr.X_add_number
9204 > (unsigned long) OP_MASK_VECALIGN)
9205 {
9206 as_bad (_("bad byte vector index (%ld)"),
9207 (long) imm_expr.X_add_number);
9208 imm_expr.X_add_number = 0;
9209 }
9210
9211 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECALIGN;
9212 imm_expr.X_op = O_absent;
9213 s = expr_end;
9214 continue;
9215
252b5132
RH
9216 default:
9217 as_bad (_("bad char = '%c'\n"), *args);
9218 internalError ();
9219 }
9220 break;
9221 }
9222 /* Args don't match. */
9223 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
9224 !strcmp (insn->name, insn[1].name))
9225 {
9226 ++insn;
9227 s = argsStart;
268f6bed 9228 insn_error = _("illegal operands");
252b5132
RH
9229 continue;
9230 }
268f6bed
L
9231 if (save_c)
9232 *(--s) = save_c;
252b5132
RH
9233 insn_error = _("illegal operands");
9234 return;
9235 }
9236}
9237
9238/* This routine assembles an instruction into its binary format when
9239 assembling for the mips16. As a side effect, it sets one of the
9240 global variables imm_reloc or offset_reloc to the type of
9241 relocation to do if one of the operands is an address expression.
9242 It also sets mips16_small and mips16_ext if the user explicitly
9243 requested a small or extended instruction. */
9244
9245static void
17a2f251 9246mips16_ip (char *str, struct mips_cl_insn *ip)
252b5132
RH
9247{
9248 char *s;
9249 const char *args;
9250 struct mips_opcode *insn;
9251 char *argsstart;
9252 unsigned int regno;
9253 unsigned int lastregno = 0;
9254 char *s_reset;
9255
9256 insn_error = NULL;
9257
b34976b6
AM
9258 mips16_small = FALSE;
9259 mips16_ext = FALSE;
252b5132 9260
3882b010 9261 for (s = str; ISLOWER (*s); ++s)
252b5132
RH
9262 ;
9263 switch (*s)
9264 {
9265 case '\0':
9266 break;
9267
9268 case ' ':
9269 *s++ = '\0';
9270 break;
9271
9272 case '.':
9273 if (s[1] == 't' && s[2] == ' ')
9274 {
9275 *s = '\0';
b34976b6 9276 mips16_small = TRUE;
252b5132
RH
9277 s += 3;
9278 break;
9279 }
9280 else if (s[1] == 'e' && s[2] == ' ')
9281 {
9282 *s = '\0';
b34976b6 9283 mips16_ext = TRUE;
252b5132
RH
9284 s += 3;
9285 break;
9286 }
9287 /* Fall through. */
9288 default:
9289 insn_error = _("unknown opcode");
9290 return;
9291 }
9292
9293 if (mips_opts.noautoextend && ! mips16_ext)
b34976b6 9294 mips16_small = TRUE;
252b5132
RH
9295
9296 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
9297 {
9298 insn_error = _("unrecognized opcode");
9299 return;
9300 }
9301
9302 argsstart = s;
9303 for (;;)
9304 {
9305 assert (strcmp (insn->name, str) == 0);
9306
9307 ip->insn_mo = insn;
9308 ip->insn_opcode = insn->match;
b34976b6 9309 ip->use_extend = FALSE;
252b5132 9310 imm_expr.X_op = O_absent;
f6688943
TS
9311 imm_reloc[0] = BFD_RELOC_UNUSED;
9312 imm_reloc[1] = BFD_RELOC_UNUSED;
9313 imm_reloc[2] = BFD_RELOC_UNUSED;
5f74bc13 9314 imm2_expr.X_op = O_absent;
252b5132 9315 offset_expr.X_op = O_absent;
f6688943
TS
9316 offset_reloc[0] = BFD_RELOC_UNUSED;
9317 offset_reloc[1] = BFD_RELOC_UNUSED;
9318 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
9319 for (args = insn->args; 1; ++args)
9320 {
9321 int c;
9322
9323 if (*s == ' ')
9324 ++s;
9325
9326 /* In this switch statement we call break if we did not find
9327 a match, continue if we did find a match, or return if we
9328 are done. */
9329
9330 c = *args;
9331 switch (c)
9332 {
9333 case '\0':
9334 if (*s == '\0')
9335 {
9336 /* Stuff the immediate value in now, if we can. */
9337 if (imm_expr.X_op == O_constant
f6688943 9338 && *imm_reloc > BFD_RELOC_UNUSED
252b5132
RH
9339 && insn->pinfo != INSN_MACRO)
9340 {
c4e7957c 9341 mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
b34976b6 9342 imm_expr.X_add_number, TRUE, mips16_small,
252b5132
RH
9343 mips16_ext, &ip->insn_opcode,
9344 &ip->use_extend, &ip->extend);
9345 imm_expr.X_op = O_absent;
f6688943 9346 *imm_reloc = BFD_RELOC_UNUSED;
252b5132
RH
9347 }
9348
9349 return;
9350 }
9351 break;
9352
9353 case ',':
9354 if (*s++ == c)
9355 continue;
9356 s--;
9357 switch (*++args)
9358 {
9359 case 'v':
9360 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9361 continue;
9362 case 'w':
9363 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9364 continue;
9365 }
9366 break;
9367
9368 case '(':
9369 case ')':
9370 if (*s++ == c)
9371 continue;
9372 break;
9373
9374 case 'v':
9375 case 'w':
9376 if (s[0] != '$')
9377 {
9378 if (c == 'v')
9379 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9380 else
9381 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9382 ++args;
9383 continue;
9384 }
9385 /* Fall through. */
9386 case 'x':
9387 case 'y':
9388 case 'z':
9389 case 'Z':
9390 case '0':
9391 case 'S':
9392 case 'R':
9393 case 'X':
9394 case 'Y':
9395 if (s[0] != '$')
9396 break;
9397 s_reset = s;
3882b010 9398 if (ISDIGIT (s[1]))
252b5132
RH
9399 {
9400 ++s;
9401 regno = 0;
9402 do
9403 {
9404 regno *= 10;
9405 regno += *s - '0';
9406 ++s;
9407 }
3882b010 9408 while (ISDIGIT (*s));
252b5132
RH
9409 if (regno > 31)
9410 {
9411 as_bad (_("invalid register number (%d)"), regno);
9412 regno = 2;
9413 }
9414 }
9415 else
9416 {
76db943d
TS
9417 if (s[1] == 'r' && s[2] == 'a')
9418 {
9419 s += 3;
9420 regno = RA;
9421 }
9422 else if (s[1] == 'f' && s[2] == 'p')
252b5132
RH
9423 {
9424 s += 3;
9425 regno = FP;
9426 }
9427 else if (s[1] == 's' && s[2] == 'p')
9428 {
9429 s += 3;
9430 regno = SP;
9431 }
9432 else if (s[1] == 'g' && s[2] == 'p')
9433 {
9434 s += 3;
9435 regno = GP;
9436 }
9437 else if (s[1] == 'a' && s[2] == 't')
9438 {
9439 s += 3;
9440 regno = AT;
9441 }
9442 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
9443 {
9444 s += 4;
9445 regno = KT0;
9446 }
9447 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
9448 {
9449 s += 4;
9450 regno = KT1;
9451 }
85b51719
TS
9452 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
9453 {
9454 s += 5;
9455 regno = ZERO;
9456 }
252b5132
RH
9457 else
9458 break;
9459 }
9460
9461 if (*s == ' ')
9462 ++s;
9463 if (args[1] != *s)
9464 {
9465 if (c == 'v' || c == 'w')
9466 {
9467 regno = mips16_to_32_reg_map[lastregno];
9468 s = s_reset;
f9419b05 9469 ++args;
252b5132
RH
9470 }
9471 }
9472
9473 switch (c)
9474 {
9475 case 'x':
9476 case 'y':
9477 case 'z':
9478 case 'v':
9479 case 'w':
9480 case 'Z':
9481 regno = mips32_to_16_reg_map[regno];
9482 break;
9483
9484 case '0':
9485 if (regno != 0)
9486 regno = ILLEGAL_REG;
9487 break;
9488
9489 case 'S':
9490 if (regno != SP)
9491 regno = ILLEGAL_REG;
9492 break;
9493
9494 case 'R':
9495 if (regno != RA)
9496 regno = ILLEGAL_REG;
9497 break;
9498
9499 case 'X':
9500 case 'Y':
9501 if (regno == AT && ! mips_opts.noat)
9502 as_warn (_("used $at without \".set noat\""));
9503 break;
9504
9505 default:
9506 internalError ();
9507 }
9508
9509 if (regno == ILLEGAL_REG)
9510 break;
9511
9512 switch (c)
9513 {
9514 case 'x':
9515 case 'v':
9516 ip->insn_opcode |= regno << MIPS16OP_SH_RX;
9517 break;
9518 case 'y':
9519 case 'w':
9520 ip->insn_opcode |= regno << MIPS16OP_SH_RY;
9521 break;
9522 case 'z':
9523 ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
9524 break;
9525 case 'Z':
9526 ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
9527 case '0':
9528 case 'S':
9529 case 'R':
9530 break;
9531 case 'X':
9532 ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
9533 break;
9534 case 'Y':
9535 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
9536 ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
9537 break;
9538 default:
9539 internalError ();
9540 }
9541
9542 lastregno = regno;
9543 continue;
9544
9545 case 'P':
9546 if (strncmp (s, "$pc", 3) == 0)
9547 {
9548 s += 3;
9549 continue;
9550 }
9551 break;
9552
9553 case '<':
9554 case '>':
9555 case '[':
9556 case ']':
9557 case '4':
9558 case '5':
9559 case 'H':
9560 case 'W':
9561 case 'D':
9562 case 'j':
9563 case '8':
9564 case 'V':
9565 case 'C':
9566 case 'U':
9567 case 'k':
9568 case 'K':
9569 if (s[0] == '%'
9570 && strncmp (s + 1, "gprel(", sizeof "gprel(" - 1) == 0)
9571 {
9572 /* This is %gprel(SYMBOL). We need to read SYMBOL,
9573 and generate the appropriate reloc. If the text
9574 inside %gprel is not a symbol name with an
9575 optional offset, then we generate a normal reloc
9576 and will probably fail later. */
9577 my_getExpression (&imm_expr, s + sizeof "%gprel" - 1);
9578 if (imm_expr.X_op == O_symbol)
9579 {
b34976b6 9580 mips16_ext = TRUE;
f6688943 9581 *imm_reloc = BFD_RELOC_MIPS16_GPREL;
252b5132 9582 s = expr_end;
b34976b6 9583 ip->use_extend = TRUE;
252b5132
RH
9584 ip->extend = 0;
9585 continue;
9586 }
9587 }
9588 else
9589 {
9590 /* Just pick up a normal expression. */
9591 my_getExpression (&imm_expr, s);
9592 }
9593
9594 if (imm_expr.X_op == O_register)
9595 {
9596 /* What we thought was an expression turned out to
9597 be a register. */
9598
9599 if (s[0] == '(' && args[1] == '(')
9600 {
9601 /* It looks like the expression was omitted
9602 before a register indirection, which means
9603 that the expression is implicitly zero. We
9604 still set up imm_expr, so that we handle
9605 explicit extensions correctly. */
9606 imm_expr.X_op = O_constant;
9607 imm_expr.X_add_number = 0;
f6688943 9608 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
9609 continue;
9610 }
9611
9612 break;
9613 }
9614
9615 /* We need to relax this instruction. */
f6688943 9616 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
9617 s = expr_end;
9618 continue;
9619
9620 case 'p':
9621 case 'q':
9622 case 'A':
9623 case 'B':
9624 case 'E':
9625 /* We use offset_reloc rather than imm_reloc for the PC
9626 relative operands. This lets macros with both
9627 immediate and address operands work correctly. */
9628 my_getExpression (&offset_expr, s);
9629
9630 if (offset_expr.X_op == O_register)
9631 break;
9632
9633 /* We need to relax this instruction. */
f6688943 9634 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
9635 s = expr_end;
9636 continue;
9637
9638 case '6': /* break code */
9639 my_getExpression (&imm_expr, s);
9640 check_absolute_expr (ip, &imm_expr);
9641 if ((unsigned long) imm_expr.X_add_number > 63)
9642 {
9643 as_warn (_("Invalid value for `%s' (%lu)"),
9644 ip->insn_mo->name,
9645 (unsigned long) imm_expr.X_add_number);
9646 imm_expr.X_add_number &= 0x3f;
9647 }
9648 ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
9649 imm_expr.X_op = O_absent;
9650 s = expr_end;
9651 continue;
9652
9653 case 'a': /* 26 bit address */
9654 my_getExpression (&offset_expr, s);
9655 s = expr_end;
f6688943 9656 *offset_reloc = BFD_RELOC_MIPS16_JMP;
252b5132
RH
9657 ip->insn_opcode <<= 16;
9658 continue;
9659
9660 case 'l': /* register list for entry macro */
9661 case 'L': /* register list for exit macro */
9662 {
9663 int mask;
9664
9665 if (c == 'l')
9666 mask = 0;
9667 else
9668 mask = 7 << 3;
9669 while (*s != '\0')
9670 {
9671 int freg, reg1, reg2;
9672
9673 while (*s == ' ' || *s == ',')
9674 ++s;
9675 if (*s != '$')
9676 {
9677 as_bad (_("can't parse register list"));
9678 break;
9679 }
9680 ++s;
9681 if (*s != 'f')
9682 freg = 0;
9683 else
9684 {
9685 freg = 1;
9686 ++s;
9687 }
9688 reg1 = 0;
3882b010 9689 while (ISDIGIT (*s))
252b5132
RH
9690 {
9691 reg1 *= 10;
9692 reg1 += *s - '0';
9693 ++s;
9694 }
9695 if (*s == ' ')
9696 ++s;
9697 if (*s != '-')
9698 reg2 = reg1;
9699 else
9700 {
9701 ++s;
9702 if (*s != '$')
9703 break;
9704 ++s;
9705 if (freg)
9706 {
9707 if (*s == 'f')
9708 ++s;
9709 else
9710 {
9711 as_bad (_("invalid register list"));
9712 break;
9713 }
9714 }
9715 reg2 = 0;
3882b010 9716 while (ISDIGIT (*s))
252b5132
RH
9717 {
9718 reg2 *= 10;
9719 reg2 += *s - '0';
9720 ++s;
9721 }
9722 }
9723 if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
9724 {
9725 mask &= ~ (7 << 3);
9726 mask |= 5 << 3;
9727 }
9728 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
9729 {
9730 mask &= ~ (7 << 3);
9731 mask |= 6 << 3;
9732 }
9733 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
9734 mask |= (reg2 - 3) << 3;
9735 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
9736 mask |= (reg2 - 15) << 1;
f9419b05 9737 else if (reg1 == RA && reg2 == RA)
252b5132
RH
9738 mask |= 1;
9739 else
9740 {
9741 as_bad (_("invalid register list"));
9742 break;
9743 }
9744 }
9745 /* The mask is filled in in the opcode table for the
9746 benefit of the disassembler. We remove it before
9747 applying the actual mask. */
9748 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
9749 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
9750 }
9751 continue;
9752
9753 case 'e': /* extend code */
9754 my_getExpression (&imm_expr, s);
9755 check_absolute_expr (ip, &imm_expr);
9756 if ((unsigned long) imm_expr.X_add_number > 0x7ff)
9757 {
9758 as_warn (_("Invalid value for `%s' (%lu)"),
9759 ip->insn_mo->name,
9760 (unsigned long) imm_expr.X_add_number);
9761 imm_expr.X_add_number &= 0x7ff;
9762 }
9763 ip->insn_opcode |= imm_expr.X_add_number;
9764 imm_expr.X_op = O_absent;
9765 s = expr_end;
9766 continue;
9767
9768 default:
9769 internalError ();
9770 }
9771 break;
9772 }
9773
9774 /* Args don't match. */
9775 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
9776 strcmp (insn->name, insn[1].name) == 0)
9777 {
9778 ++insn;
9779 s = argsstart;
9780 continue;
9781 }
9782
9783 insn_error = _("illegal operands");
9784
9785 return;
9786 }
9787}
9788
9789/* This structure holds information we know about a mips16 immediate
9790 argument type. */
9791
e972090a
NC
9792struct mips16_immed_operand
9793{
252b5132
RH
9794 /* The type code used in the argument string in the opcode table. */
9795 int type;
9796 /* The number of bits in the short form of the opcode. */
9797 int nbits;
9798 /* The number of bits in the extended form of the opcode. */
9799 int extbits;
9800 /* The amount by which the short form is shifted when it is used;
9801 for example, the sw instruction has a shift count of 2. */
9802 int shift;
9803 /* The amount by which the short form is shifted when it is stored
9804 into the instruction code. */
9805 int op_shift;
9806 /* Non-zero if the short form is unsigned. */
9807 int unsp;
9808 /* Non-zero if the extended form is unsigned. */
9809 int extu;
9810 /* Non-zero if the value is PC relative. */
9811 int pcrel;
9812};
9813
9814/* The mips16 immediate operand types. */
9815
9816static const struct mips16_immed_operand mips16_immed_operands[] =
9817{
9818 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9819 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9820 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9821 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9822 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
9823 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
9824 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
9825 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
9826 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
9827 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
9828 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
9829 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
9830 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
9831 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
9832 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
9833 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
9834 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9835 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9836 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
9837 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
9838 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
9839};
9840
9841#define MIPS16_NUM_IMMED \
9842 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
9843
9844/* Handle a mips16 instruction with an immediate value. This or's the
9845 small immediate value into *INSN. It sets *USE_EXTEND to indicate
9846 whether an extended value is needed; if one is needed, it sets
9847 *EXTEND to the value. The argument type is TYPE. The value is VAL.
9848 If SMALL is true, an unextended opcode was explicitly requested.
9849 If EXT is true, an extended opcode was explicitly requested. If
9850 WARN is true, warn if EXT does not match reality. */
9851
9852static void
17a2f251
TS
9853mips16_immed (char *file, unsigned int line, int type, offsetT val,
9854 bfd_boolean warn, bfd_boolean small, bfd_boolean ext,
9855 unsigned long *insn, bfd_boolean *use_extend,
9856 unsigned short *extend)
252b5132
RH
9857{
9858 register const struct mips16_immed_operand *op;
9859 int mintiny, maxtiny;
b34976b6 9860 bfd_boolean needext;
252b5132
RH
9861
9862 op = mips16_immed_operands;
9863 while (op->type != type)
9864 {
9865 ++op;
9866 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
9867 }
9868
9869 if (op->unsp)
9870 {
9871 if (type == '<' || type == '>' || type == '[' || type == ']')
9872 {
9873 mintiny = 1;
9874 maxtiny = 1 << op->nbits;
9875 }
9876 else
9877 {
9878 mintiny = 0;
9879 maxtiny = (1 << op->nbits) - 1;
9880 }
9881 }
9882 else
9883 {
9884 mintiny = - (1 << (op->nbits - 1));
9885 maxtiny = (1 << (op->nbits - 1)) - 1;
9886 }
9887
9888 /* Branch offsets have an implicit 0 in the lowest bit. */
9889 if (type == 'p' || type == 'q')
9890 val /= 2;
9891
9892 if ((val & ((1 << op->shift) - 1)) != 0
9893 || val < (mintiny << op->shift)
9894 || val > (maxtiny << op->shift))
b34976b6 9895 needext = TRUE;
252b5132 9896 else
b34976b6 9897 needext = FALSE;
252b5132
RH
9898
9899 if (warn && ext && ! needext)
beae10d5
KH
9900 as_warn_where (file, line,
9901 _("extended operand requested but not required"));
252b5132
RH
9902 if (small && needext)
9903 as_bad_where (file, line, _("invalid unextended operand value"));
9904
9905 if (small || (! ext && ! needext))
9906 {
9907 int insnval;
9908
b34976b6 9909 *use_extend = FALSE;
252b5132
RH
9910 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
9911 insnval <<= op->op_shift;
9912 *insn |= insnval;
9913 }
9914 else
9915 {
9916 long minext, maxext;
9917 int extval;
9918
9919 if (op->extu)
9920 {
9921 minext = 0;
9922 maxext = (1 << op->extbits) - 1;
9923 }
9924 else
9925 {
9926 minext = - (1 << (op->extbits - 1));
9927 maxext = (1 << (op->extbits - 1)) - 1;
9928 }
9929 if (val < minext || val > maxext)
9930 as_bad_where (file, line,
9931 _("operand value out of range for instruction"));
9932
b34976b6 9933 *use_extend = TRUE;
252b5132
RH
9934 if (op->extbits == 16)
9935 {
9936 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
9937 val &= 0x1f;
9938 }
9939 else if (op->extbits == 15)
9940 {
9941 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
9942 val &= 0xf;
9943 }
9944 else
9945 {
9946 extval = ((val & 0x1f) << 6) | (val & 0x20);
9947 val = 0;
9948 }
9949
9950 *extend = (unsigned short) extval;
9951 *insn |= val;
9952 }
9953}
9954\f
5e0116d5 9955static const struct percent_op_match
ad8d3bb3 9956{
5e0116d5
RS
9957 const char *str;
9958 bfd_reloc_code_real_type reloc;
ad8d3bb3
TS
9959} percent_op[] =
9960{
5e0116d5 9961 {"%lo", BFD_RELOC_LO16},
ad8d3bb3 9962#ifdef OBJ_ELF
5e0116d5
RS
9963 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
9964 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
9965 {"%call16", BFD_RELOC_MIPS_CALL16},
9966 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
9967 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
9968 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
9969 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
9970 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
9971 {"%got", BFD_RELOC_MIPS_GOT16},
9972 {"%gp_rel", BFD_RELOC_GPREL16},
9973 {"%half", BFD_RELOC_16},
9974 {"%highest", BFD_RELOC_MIPS_HIGHEST},
9975 {"%higher", BFD_RELOC_MIPS_HIGHER},
9976 {"%neg", BFD_RELOC_MIPS_SUB},
ad8d3bb3 9977#endif
5e0116d5 9978 {"%hi", BFD_RELOC_HI16_S}
ad8d3bb3
TS
9979};
9980
252b5132 9981
5e0116d5
RS
9982/* Return true if *STR points to a relocation operator. When returning true,
9983 move *STR over the operator and store its relocation code in *RELOC.
9984 Leave both *STR and *RELOC alone when returning false. */
9985
9986static bfd_boolean
17a2f251 9987parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
252b5132 9988{
5e0116d5 9989 size_t i;
76b3015f 9990
5e0116d5
RS
9991 for (i = 0; i < ARRAY_SIZE (percent_op); i++)
9992 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
394f9b3a 9993 {
5e0116d5
RS
9994 *str += strlen (percent_op[i].str);
9995 *reloc = percent_op[i].reloc;
394f9b3a 9996
5e0116d5
RS
9997 /* Check whether the output BFD supports this relocation.
9998 If not, issue an error and fall back on something safe. */
9999 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
394f9b3a 10000 {
5e0116d5
RS
10001 as_bad ("relocation %s isn't supported by the current ABI",
10002 percent_op[i].str);
10003 *reloc = BFD_RELOC_LO16;
394f9b3a 10004 }
5e0116d5 10005 return TRUE;
394f9b3a 10006 }
5e0116d5 10007 return FALSE;
394f9b3a 10008}
ad8d3bb3 10009
ad8d3bb3 10010
5e0116d5
RS
10011/* Parse string STR as a 16-bit relocatable operand. Store the
10012 expression in *EP and the relocations in the array starting
10013 at RELOC. Return the number of relocation operators used.
ad8d3bb3 10014
5e0116d5
RS
10015 On exit, EXPR_END points to the first character after the expression.
10016 If no relocation operators are used, RELOC[0] is set to BFD_RELOC_LO16. */
ad8d3bb3 10017
5e0116d5 10018static size_t
17a2f251
TS
10019my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
10020 char *str)
ad8d3bb3 10021{
5e0116d5
RS
10022 bfd_reloc_code_real_type reversed_reloc[3];
10023 size_t reloc_index, i;
09b8f35a
RS
10024 int crux_depth, str_depth;
10025 char *crux;
5e0116d5
RS
10026
10027 /* Search for the start of the main expression, recoding relocations
09b8f35a
RS
10028 in REVERSED_RELOC. End the loop with CRUX pointing to the start
10029 of the main expression and with CRUX_DEPTH containing the number
10030 of open brackets at that point. */
10031 reloc_index = -1;
10032 str_depth = 0;
10033 do
fb1b3232 10034 {
09b8f35a
RS
10035 reloc_index++;
10036 crux = str;
10037 crux_depth = str_depth;
10038
10039 /* Skip over whitespace and brackets, keeping count of the number
10040 of brackets. */
10041 while (*str == ' ' || *str == '\t' || *str == '(')
10042 if (*str++ == '(')
10043 str_depth++;
5e0116d5 10044 }
09b8f35a
RS
10045 while (*str == '%'
10046 && reloc_index < (HAVE_NEWABI ? 3 : 1)
10047 && parse_relocation (&str, &reversed_reloc[reloc_index]));
ad8d3bb3 10048
09b8f35a 10049 my_getExpression (ep, crux);
5e0116d5 10050 str = expr_end;
394f9b3a 10051
5e0116d5 10052 /* Match every open bracket. */
09b8f35a 10053 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
5e0116d5 10054 if (*str++ == ')')
09b8f35a 10055 crux_depth--;
394f9b3a 10056
09b8f35a 10057 if (crux_depth > 0)
5e0116d5 10058 as_bad ("unclosed '('");
394f9b3a 10059
5e0116d5 10060 expr_end = str;
252b5132 10061
64bdfcaf
RS
10062 if (reloc_index == 0)
10063 reloc[0] = BFD_RELOC_LO16;
10064 else
10065 {
10066 prev_reloc_op_frag = frag_now;
10067 for (i = 0; i < reloc_index; i++)
10068 reloc[i] = reversed_reloc[reloc_index - 1 - i];
10069 }
fb1b3232 10070
5e0116d5 10071 return reloc_index;
252b5132
RH
10072}
10073
10074static void
17a2f251 10075my_getExpression (expressionS *ep, char *str)
252b5132
RH
10076{
10077 char *save_in;
98aa84af 10078 valueT val;
252b5132
RH
10079
10080 save_in = input_line_pointer;
10081 input_line_pointer = str;
10082 expression (ep);
10083 expr_end = input_line_pointer;
10084 input_line_pointer = save_in;
10085
10086 /* If we are in mips16 mode, and this is an expression based on `.',
10087 then we bump the value of the symbol by 1 since that is how other
10088 text symbols are handled. We don't bother to handle complex
10089 expressions, just `.' plus or minus a constant. */
10090 if (mips_opts.mips16
10091 && ep->X_op == O_symbol
10092 && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
10093 && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
49309057
ILT
10094 && symbol_get_frag (ep->X_add_symbol) == frag_now
10095 && symbol_constant_p (ep->X_add_symbol)
98aa84af
AM
10096 && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
10097 S_SET_VALUE (ep->X_add_symbol, val + 1);
252b5132
RH
10098}
10099
10100/* Turn a string in input_line_pointer into a floating point constant
bc0d738a
NC
10101 of type TYPE, and store the appropriate bytes in *LITP. The number
10102 of LITTLENUMS emitted is stored in *SIZEP. An error message is
252b5132
RH
10103 returned, or NULL on OK. */
10104
10105char *
17a2f251 10106md_atof (int type, char *litP, int *sizeP)
252b5132
RH
10107{
10108 int prec;
10109 LITTLENUM_TYPE words[4];
10110 char *t;
10111 int i;
10112
10113 switch (type)
10114 {
10115 case 'f':
10116 prec = 2;
10117 break;
10118
10119 case 'd':
10120 prec = 4;
10121 break;
10122
10123 default:
10124 *sizeP = 0;
10125 return _("bad call to md_atof");
10126 }
10127
10128 t = atof_ieee (input_line_pointer, type, words);
10129 if (t)
10130 input_line_pointer = t;
10131
10132 *sizeP = prec * 2;
10133
10134 if (! target_big_endian)
10135 {
10136 for (i = prec - 1; i >= 0; i--)
10137 {
17a2f251 10138 md_number_to_chars (litP, words[i], 2);
252b5132
RH
10139 litP += 2;
10140 }
10141 }
10142 else
10143 {
10144 for (i = 0; i < prec; i++)
10145 {
17a2f251 10146 md_number_to_chars (litP, words[i], 2);
252b5132
RH
10147 litP += 2;
10148 }
10149 }
bdaaa2e1 10150
252b5132
RH
10151 return NULL;
10152}
10153
10154void
17a2f251 10155md_number_to_chars (char *buf, valueT val, int n)
252b5132
RH
10156{
10157 if (target_big_endian)
10158 number_to_chars_bigendian (buf, val, n);
10159 else
10160 number_to_chars_littleendian (buf, val, n);
10161}
10162\f
ae948b86 10163#ifdef OBJ_ELF
e013f690
TS
10164static int support_64bit_objects(void)
10165{
10166 const char **list, **l;
aa3d8fdf 10167 int yes;
e013f690
TS
10168
10169 list = bfd_target_list ();
10170 for (l = list; *l != NULL; l++)
10171#ifdef TE_TMIPS
10172 /* This is traditional mips */
10173 if (strcmp (*l, "elf64-tradbigmips") == 0
10174 || strcmp (*l, "elf64-tradlittlemips") == 0)
10175#else
10176 if (strcmp (*l, "elf64-bigmips") == 0
10177 || strcmp (*l, "elf64-littlemips") == 0)
10178#endif
10179 break;
aa3d8fdf 10180 yes = (*l != NULL);
e013f690 10181 free (list);
aa3d8fdf 10182 return yes;
e013f690 10183}
ae948b86 10184#endif /* OBJ_ELF */
e013f690 10185
78849248 10186const char *md_shortopts = "O::g::G:";
252b5132 10187
e972090a
NC
10188struct option md_longopts[] =
10189{
f9b4148d
CD
10190 /* Options which specify architecture. */
10191#define OPTION_ARCH_BASE (OPTION_MD_BASE)
10192#define OPTION_MARCH (OPTION_ARCH_BASE + 0)
10193 {"march", required_argument, NULL, OPTION_MARCH},
10194#define OPTION_MTUNE (OPTION_ARCH_BASE + 1)
10195 {"mtune", required_argument, NULL, OPTION_MTUNE},
10196#define OPTION_MIPS1 (OPTION_ARCH_BASE + 2)
252b5132
RH
10197 {"mips0", no_argument, NULL, OPTION_MIPS1},
10198 {"mips1", no_argument, NULL, OPTION_MIPS1},
f9b4148d 10199#define OPTION_MIPS2 (OPTION_ARCH_BASE + 3)
252b5132 10200 {"mips2", no_argument, NULL, OPTION_MIPS2},
f9b4148d 10201#define OPTION_MIPS3 (OPTION_ARCH_BASE + 4)
252b5132 10202 {"mips3", no_argument, NULL, OPTION_MIPS3},
f9b4148d 10203#define OPTION_MIPS4 (OPTION_ARCH_BASE + 5)
252b5132 10204 {"mips4", no_argument, NULL, OPTION_MIPS4},
f9b4148d 10205#define OPTION_MIPS5 (OPTION_ARCH_BASE + 6)
ae948b86 10206 {"mips5", no_argument, NULL, OPTION_MIPS5},
f9b4148d 10207#define OPTION_MIPS32 (OPTION_ARCH_BASE + 7)
ae948b86 10208 {"mips32", no_argument, NULL, OPTION_MIPS32},
f9b4148d 10209#define OPTION_MIPS64 (OPTION_ARCH_BASE + 8)
ae948b86 10210 {"mips64", no_argument, NULL, OPTION_MIPS64},
f9b4148d
CD
10211#define OPTION_MIPS32R2 (OPTION_ARCH_BASE + 9)
10212 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
5f74bc13
CD
10213#define OPTION_MIPS64R2 (OPTION_ARCH_BASE + 10)
10214 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
f9b4148d
CD
10215
10216 /* Options which specify Application Specific Extensions (ASEs). */
5f74bc13 10217#define OPTION_ASE_BASE (OPTION_ARCH_BASE + 11)
f9b4148d
CD
10218#define OPTION_MIPS16 (OPTION_ASE_BASE + 0)
10219 {"mips16", no_argument, NULL, OPTION_MIPS16},
10220#define OPTION_NO_MIPS16 (OPTION_ASE_BASE + 1)
10221 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
10222#define OPTION_MIPS3D (OPTION_ASE_BASE + 2)
10223 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
10224#define OPTION_NO_MIPS3D (OPTION_ASE_BASE + 3)
10225 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
10226#define OPTION_MDMX (OPTION_ASE_BASE + 4)
10227 {"mdmx", no_argument, NULL, OPTION_MDMX},
10228#define OPTION_NO_MDMX (OPTION_ASE_BASE + 5)
10229 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
10230
10231 /* Old-style architecture options. Don't add more of these. */
10232#define OPTION_COMPAT_ARCH_BASE (OPTION_ASE_BASE + 6)
10233#define OPTION_M4650 (OPTION_COMPAT_ARCH_BASE + 0)
10234 {"m4650", no_argument, NULL, OPTION_M4650},
10235#define OPTION_NO_M4650 (OPTION_COMPAT_ARCH_BASE + 1)
10236 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
10237#define OPTION_M4010 (OPTION_COMPAT_ARCH_BASE + 2)
10238 {"m4010", no_argument, NULL, OPTION_M4010},
10239#define OPTION_NO_M4010 (OPTION_COMPAT_ARCH_BASE + 3)
10240 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
10241#define OPTION_M4100 (OPTION_COMPAT_ARCH_BASE + 4)
10242 {"m4100", no_argument, NULL, OPTION_M4100},
10243#define OPTION_NO_M4100 (OPTION_COMPAT_ARCH_BASE + 5)
10244 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
10245#define OPTION_M3900 (OPTION_COMPAT_ARCH_BASE + 6)
10246 {"m3900", no_argument, NULL, OPTION_M3900},
10247#define OPTION_NO_M3900 (OPTION_COMPAT_ARCH_BASE + 7)
10248 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
10249
10250 /* Options which enable bug fixes. */
10251#define OPTION_FIX_BASE (OPTION_COMPAT_ARCH_BASE + 8)
10252#define OPTION_M7000_HILO_FIX (OPTION_FIX_BASE + 0)
10253 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
10254#define OPTION_MNO_7000_HILO_FIX (OPTION_FIX_BASE + 1)
10255 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10256 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10257#define OPTION_FIX_VR4122 (OPTION_FIX_BASE + 2)
10258#define OPTION_NO_FIX_VR4122 (OPTION_FIX_BASE + 3)
10259 {"mfix-vr4122-bugs", no_argument, NULL, OPTION_FIX_VR4122},
10260 {"no-mfix-vr4122-bugs", no_argument, NULL, OPTION_NO_FIX_VR4122},
10261
10262 /* Miscellaneous options. */
10263#define OPTION_MISC_BASE (OPTION_FIX_BASE + 4)
10264#define OPTION_MEMBEDDED_PIC (OPTION_MISC_BASE + 0)
252b5132 10265 {"membedded-pic", no_argument, NULL, OPTION_MEMBEDDED_PIC},
f9b4148d 10266#define OPTION_TRAP (OPTION_MISC_BASE + 1)
252b5132
RH
10267 {"trap", no_argument, NULL, OPTION_TRAP},
10268 {"no-break", no_argument, NULL, OPTION_TRAP},
f9b4148d 10269#define OPTION_BREAK (OPTION_MISC_BASE + 2)
252b5132
RH
10270 {"break", no_argument, NULL, OPTION_BREAK},
10271 {"no-trap", no_argument, NULL, OPTION_BREAK},
f9b4148d 10272#define OPTION_EB (OPTION_MISC_BASE + 3)
252b5132 10273 {"EB", no_argument, NULL, OPTION_EB},
f9b4148d 10274#define OPTION_EL (OPTION_MISC_BASE + 4)
252b5132 10275 {"EL", no_argument, NULL, OPTION_EL},
f9b4148d 10276#define OPTION_FP32 (OPTION_MISC_BASE + 5)
ae948b86 10277 {"mfp32", no_argument, NULL, OPTION_FP32},
f9b4148d 10278#define OPTION_GP32 (OPTION_MISC_BASE + 6)
c97ef257 10279 {"mgp32", no_argument, NULL, OPTION_GP32},
f9b4148d 10280#define OPTION_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 7)
119d663a 10281 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
f9b4148d 10282#define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 8)
119d663a 10283 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
f9b4148d 10284#define OPTION_FP64 (OPTION_MISC_BASE + 9)
316f5878 10285 {"mfp64", no_argument, NULL, OPTION_FP64},
f9b4148d 10286#define OPTION_GP64 (OPTION_MISC_BASE + 10)
ae948b86 10287 {"mgp64", no_argument, NULL, OPTION_GP64},
f9b4148d
CD
10288#define OPTION_RELAX_BRANCH (OPTION_MISC_BASE + 11)
10289#define OPTION_NO_RELAX_BRANCH (OPTION_MISC_BASE + 12)
4a6a3df4
AO
10290 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
10291 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
f9b4148d
CD
10292
10293 /* ELF-specific options. */
156c2f8b 10294#ifdef OBJ_ELF
f9b4148d 10295#define OPTION_ELF_BASE (OPTION_MISC_BASE + 13)
156c2f8b 10296#define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
156c2f8b
NC
10297 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
10298 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
ae948b86 10299#define OPTION_NON_SHARED (OPTION_ELF_BASE + 1)
156c2f8b 10300 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
ae948b86 10301#define OPTION_XGOT (OPTION_ELF_BASE + 2)
156c2f8b 10302 {"xgot", no_argument, NULL, OPTION_XGOT},
ae948b86
TS
10303#define OPTION_MABI (OPTION_ELF_BASE + 3)
10304 {"mabi", required_argument, NULL, OPTION_MABI},
10305#define OPTION_32 (OPTION_ELF_BASE + 4)
156c2f8b 10306 {"32", no_argument, NULL, OPTION_32},
ae948b86 10307#define OPTION_N32 (OPTION_ELF_BASE + 5)
e013f690 10308 {"n32", no_argument, NULL, OPTION_N32},
ae948b86 10309#define OPTION_64 (OPTION_ELF_BASE + 6)
156c2f8b 10310 {"64", no_argument, NULL, OPTION_64},
ecb4347a
DJ
10311#define OPTION_MDEBUG (OPTION_ELF_BASE + 7)
10312 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
10313#define OPTION_NO_MDEBUG (OPTION_ELF_BASE + 8)
10314 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
dcd410fe
RO
10315#define OPTION_PDR (OPTION_ELF_BASE + 9)
10316 {"mpdr", no_argument, NULL, OPTION_PDR},
10317#define OPTION_NO_PDR (OPTION_ELF_BASE + 10)
10318 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
ae948b86 10319#endif /* OBJ_ELF */
f9b4148d 10320
252b5132
RH
10321 {NULL, no_argument, NULL, 0}
10322};
156c2f8b 10323size_t md_longopts_size = sizeof (md_longopts);
252b5132 10324
316f5878
RS
10325/* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
10326 NEW_VALUE. Warn if another value was already specified. Note:
10327 we have to defer parsing the -march and -mtune arguments in order
10328 to handle 'from-abi' correctly, since the ABI might be specified
10329 in a later argument. */
10330
10331static void
17a2f251 10332mips_set_option_string (const char **string_ptr, const char *new_value)
316f5878
RS
10333{
10334 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
10335 as_warn (_("A different %s was already specified, is now %s"),
10336 string_ptr == &mips_arch_string ? "-march" : "-mtune",
10337 new_value);
10338
10339 *string_ptr = new_value;
10340}
10341
252b5132 10342int
17a2f251 10343md_parse_option (int c, char *arg)
252b5132
RH
10344{
10345 switch (c)
10346 {
119d663a
NC
10347 case OPTION_CONSTRUCT_FLOATS:
10348 mips_disable_float_construction = 0;
10349 break;
bdaaa2e1 10350
119d663a
NC
10351 case OPTION_NO_CONSTRUCT_FLOATS:
10352 mips_disable_float_construction = 1;
10353 break;
bdaaa2e1 10354
252b5132
RH
10355 case OPTION_TRAP:
10356 mips_trap = 1;
10357 break;
10358
10359 case OPTION_BREAK:
10360 mips_trap = 0;
10361 break;
10362
10363 case OPTION_EB:
10364 target_big_endian = 1;
10365 break;
10366
10367 case OPTION_EL:
10368 target_big_endian = 0;
10369 break;
10370
10371 case 'O':
10372 if (arg && arg[1] == '0')
10373 mips_optimize = 1;
10374 else
10375 mips_optimize = 2;
10376 break;
10377
10378 case 'g':
10379 if (arg == NULL)
10380 mips_debug = 2;
10381 else
10382 mips_debug = atoi (arg);
10383 /* When the MIPS assembler sees -g or -g2, it does not do
10384 optimizations which limit full symbolic debugging. We take
10385 that to be equivalent to -O0. */
10386 if (mips_debug == 2)
10387 mips_optimize = 1;
10388 break;
10389
10390 case OPTION_MIPS1:
316f5878 10391 file_mips_isa = ISA_MIPS1;
252b5132
RH
10392 break;
10393
10394 case OPTION_MIPS2:
316f5878 10395 file_mips_isa = ISA_MIPS2;
252b5132
RH
10396 break;
10397
10398 case OPTION_MIPS3:
316f5878 10399 file_mips_isa = ISA_MIPS3;
252b5132
RH
10400 break;
10401
10402 case OPTION_MIPS4:
316f5878 10403 file_mips_isa = ISA_MIPS4;
e7af610e
NC
10404 break;
10405
84ea6cf2 10406 case OPTION_MIPS5:
316f5878 10407 file_mips_isa = ISA_MIPS5;
84ea6cf2
NC
10408 break;
10409
e7af610e 10410 case OPTION_MIPS32:
316f5878 10411 file_mips_isa = ISA_MIPS32;
252b5132
RH
10412 break;
10413
af7ee8bf
CD
10414 case OPTION_MIPS32R2:
10415 file_mips_isa = ISA_MIPS32R2;
10416 break;
10417
5f74bc13
CD
10418 case OPTION_MIPS64R2:
10419 file_mips_isa = ISA_MIPS64R2;
10420 break;
10421
84ea6cf2 10422 case OPTION_MIPS64:
316f5878 10423 file_mips_isa = ISA_MIPS64;
84ea6cf2
NC
10424 break;
10425
ec68c924 10426 case OPTION_MTUNE:
316f5878
RS
10427 mips_set_option_string (&mips_tune_string, arg);
10428 break;
ec68c924 10429
316f5878
RS
10430 case OPTION_MARCH:
10431 mips_set_option_string (&mips_arch_string, arg);
252b5132
RH
10432 break;
10433
10434 case OPTION_M4650:
316f5878
RS
10435 mips_set_option_string (&mips_arch_string, "4650");
10436 mips_set_option_string (&mips_tune_string, "4650");
252b5132
RH
10437 break;
10438
10439 case OPTION_NO_M4650:
10440 break;
10441
10442 case OPTION_M4010:
316f5878
RS
10443 mips_set_option_string (&mips_arch_string, "4010");
10444 mips_set_option_string (&mips_tune_string, "4010");
252b5132
RH
10445 break;
10446
10447 case OPTION_NO_M4010:
10448 break;
10449
10450 case OPTION_M4100:
316f5878
RS
10451 mips_set_option_string (&mips_arch_string, "4100");
10452 mips_set_option_string (&mips_tune_string, "4100");
252b5132
RH
10453 break;
10454
10455 case OPTION_NO_M4100:
10456 break;
10457
252b5132 10458 case OPTION_M3900:
316f5878
RS
10459 mips_set_option_string (&mips_arch_string, "3900");
10460 mips_set_option_string (&mips_tune_string, "3900");
252b5132 10461 break;
bdaaa2e1 10462
252b5132
RH
10463 case OPTION_NO_M3900:
10464 break;
10465
deec1734
CD
10466 case OPTION_MDMX:
10467 mips_opts.ase_mdmx = 1;
10468 break;
10469
10470 case OPTION_NO_MDMX:
10471 mips_opts.ase_mdmx = 0;
10472 break;
10473
252b5132
RH
10474 case OPTION_MIPS16:
10475 mips_opts.mips16 = 1;
b34976b6 10476 mips_no_prev_insn (FALSE);
252b5132
RH
10477 break;
10478
10479 case OPTION_NO_MIPS16:
10480 mips_opts.mips16 = 0;
b34976b6 10481 mips_no_prev_insn (FALSE);
252b5132
RH
10482 break;
10483
1f25f5d3
CD
10484 case OPTION_MIPS3D:
10485 mips_opts.ase_mips3d = 1;
10486 break;
10487
10488 case OPTION_NO_MIPS3D:
10489 mips_opts.ase_mips3d = 0;
10490 break;
10491
252b5132
RH
10492 case OPTION_MEMBEDDED_PIC:
10493 mips_pic = EMBEDDED_PIC;
10494 if (USE_GLOBAL_POINTER_OPT && g_switch_seen)
10495 {
10496 as_bad (_("-G may not be used with embedded PIC code"));
10497 return 0;
10498 }
10499 g_switch_value = 0x7fffffff;
10500 break;
10501
60b63b72
RS
10502 case OPTION_FIX_VR4122:
10503 mips_fix_4122_bugs = 1;
10504 break;
10505
10506 case OPTION_NO_FIX_VR4122:
10507 mips_fix_4122_bugs = 0;
10508 break;
10509
4a6a3df4
AO
10510 case OPTION_RELAX_BRANCH:
10511 mips_relax_branch = 1;
10512 break;
10513
10514 case OPTION_NO_RELAX_BRANCH:
10515 mips_relax_branch = 0;
10516 break;
10517
0f074f60 10518#ifdef OBJ_ELF
252b5132
RH
10519 /* When generating ELF code, we permit -KPIC and -call_shared to
10520 select SVR4_PIC, and -non_shared to select no PIC. This is
10521 intended to be compatible with Irix 5. */
10522 case OPTION_CALL_SHARED:
10523 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10524 {
10525 as_bad (_("-call_shared is supported only for ELF format"));
10526 return 0;
10527 }
10528 mips_pic = SVR4_PIC;
143d77c5 10529 mips_abicalls = TRUE;
252b5132
RH
10530 if (g_switch_seen && g_switch_value != 0)
10531 {
10532 as_bad (_("-G may not be used with SVR4 PIC code"));
10533 return 0;
10534 }
10535 g_switch_value = 0;
10536 break;
10537
10538 case OPTION_NON_SHARED:
10539 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10540 {
10541 as_bad (_("-non_shared is supported only for ELF format"));
10542 return 0;
10543 }
10544 mips_pic = NO_PIC;
143d77c5 10545 mips_abicalls = FALSE;
252b5132
RH
10546 break;
10547
10548 /* The -xgot option tells the assembler to use 32 offsets when
10549 accessing the got in SVR4_PIC mode. It is for Irix
10550 compatibility. */
10551 case OPTION_XGOT:
10552 mips_big_got = 1;
10553 break;
0f074f60 10554#endif /* OBJ_ELF */
252b5132
RH
10555
10556 case 'G':
10557 if (! USE_GLOBAL_POINTER_OPT)
10558 {
10559 as_bad (_("-G is not supported for this configuration"));
10560 return 0;
10561 }
10562 else if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
10563 {
10564 as_bad (_("-G may not be used with SVR4 or embedded PIC code"));
10565 return 0;
10566 }
10567 else
10568 g_switch_value = atoi (arg);
10569 g_switch_seen = 1;
10570 break;
10571
0f074f60 10572#ifdef OBJ_ELF
34ba82a8
TS
10573 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
10574 and -mabi=64. */
252b5132 10575 case OPTION_32:
34ba82a8
TS
10576 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10577 {
10578 as_bad (_("-32 is supported for ELF format only"));
10579 return 0;
10580 }
316f5878 10581 mips_abi = O32_ABI;
252b5132
RH
10582 break;
10583
e013f690 10584 case OPTION_N32:
34ba82a8
TS
10585 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10586 {
10587 as_bad (_("-n32 is supported for ELF format only"));
10588 return 0;
10589 }
316f5878 10590 mips_abi = N32_ABI;
e013f690 10591 break;
252b5132 10592
e013f690 10593 case OPTION_64:
34ba82a8
TS
10594 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10595 {
10596 as_bad (_("-64 is supported for ELF format only"));
10597 return 0;
10598 }
316f5878 10599 mips_abi = N64_ABI;
e013f690
TS
10600 if (! support_64bit_objects())
10601 as_fatal (_("No compiled in support for 64 bit object file format"));
252b5132 10602 break;
ae948b86 10603#endif /* OBJ_ELF */
252b5132 10604
c97ef257 10605 case OPTION_GP32:
a325df1d 10606 file_mips_gp32 = 1;
c97ef257
AH
10607 break;
10608
10609 case OPTION_GP64:
a325df1d 10610 file_mips_gp32 = 0;
c97ef257 10611 break;
252b5132 10612
ca4e0257 10613 case OPTION_FP32:
a325df1d 10614 file_mips_fp32 = 1;
316f5878
RS
10615 break;
10616
10617 case OPTION_FP64:
10618 file_mips_fp32 = 0;
ca4e0257
RS
10619 break;
10620
ae948b86 10621#ifdef OBJ_ELF
252b5132 10622 case OPTION_MABI:
34ba82a8
TS
10623 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10624 {
10625 as_bad (_("-mabi is supported for ELF format only"));
10626 return 0;
10627 }
e013f690 10628 if (strcmp (arg, "32") == 0)
316f5878 10629 mips_abi = O32_ABI;
e013f690 10630 else if (strcmp (arg, "o64") == 0)
316f5878 10631 mips_abi = O64_ABI;
e013f690 10632 else if (strcmp (arg, "n32") == 0)
316f5878 10633 mips_abi = N32_ABI;
e013f690
TS
10634 else if (strcmp (arg, "64") == 0)
10635 {
316f5878 10636 mips_abi = N64_ABI;
e013f690
TS
10637 if (! support_64bit_objects())
10638 as_fatal (_("No compiled in support for 64 bit object file "
10639 "format"));
10640 }
10641 else if (strcmp (arg, "eabi") == 0)
316f5878 10642 mips_abi = EABI_ABI;
e013f690 10643 else
da0e507f
TS
10644 {
10645 as_fatal (_("invalid abi -mabi=%s"), arg);
10646 return 0;
10647 }
252b5132 10648 break;
e013f690 10649#endif /* OBJ_ELF */
252b5132 10650
6b76fefe 10651 case OPTION_M7000_HILO_FIX:
b34976b6 10652 mips_7000_hilo_fix = TRUE;
6b76fefe
CM
10653 break;
10654
9ee72ff1 10655 case OPTION_MNO_7000_HILO_FIX:
b34976b6 10656 mips_7000_hilo_fix = FALSE;
6b76fefe
CM
10657 break;
10658
ecb4347a
DJ
10659#ifdef OBJ_ELF
10660 case OPTION_MDEBUG:
b34976b6 10661 mips_flag_mdebug = TRUE;
ecb4347a
DJ
10662 break;
10663
10664 case OPTION_NO_MDEBUG:
b34976b6 10665 mips_flag_mdebug = FALSE;
ecb4347a 10666 break;
dcd410fe
RO
10667
10668 case OPTION_PDR:
10669 mips_flag_pdr = TRUE;
10670 break;
10671
10672 case OPTION_NO_PDR:
10673 mips_flag_pdr = FALSE;
10674 break;
ecb4347a
DJ
10675#endif /* OBJ_ELF */
10676
252b5132
RH
10677 default:
10678 return 0;
10679 }
10680
10681 return 1;
10682}
316f5878
RS
10683\f
10684/* Set up globals to generate code for the ISA or processor
10685 described by INFO. */
252b5132 10686
252b5132 10687static void
17a2f251 10688mips_set_architecture (const struct mips_cpu_info *info)
252b5132 10689{
316f5878 10690 if (info != 0)
252b5132 10691 {
fef14a42
TS
10692 file_mips_arch = info->cpu;
10693 mips_opts.arch = info->cpu;
316f5878 10694 mips_opts.isa = info->isa;
252b5132 10695 }
252b5132
RH
10696}
10697
252b5132 10698
316f5878 10699/* Likewise for tuning. */
252b5132 10700
316f5878 10701static void
17a2f251 10702mips_set_tune (const struct mips_cpu_info *info)
316f5878
RS
10703{
10704 if (info != 0)
fef14a42 10705 mips_tune = info->cpu;
316f5878 10706}
80cc45a5 10707
34ba82a8 10708
252b5132 10709void
17a2f251 10710mips_after_parse_args (void)
e9670677 10711{
fef14a42
TS
10712 const struct mips_cpu_info *arch_info = 0;
10713 const struct mips_cpu_info *tune_info = 0;
10714
e9670677
MR
10715 /* GP relative stuff not working for PE */
10716 if (strncmp (TARGET_OS, "pe", 2) == 0
10717 && g_switch_value != 0)
10718 {
10719 if (g_switch_seen)
10720 as_bad (_("-G not supported in this configuration."));
10721 g_switch_value = 0;
10722 }
10723
cac012d6
AO
10724 if (mips_abi == NO_ABI)
10725 mips_abi = MIPS_DEFAULT_ABI;
10726
22923709
RS
10727 /* The following code determines the architecture and register size.
10728 Similar code was added to GCC 3.3 (see override_options() in
10729 config/mips/mips.c). The GAS and GCC code should be kept in sync
10730 as much as possible. */
e9670677 10731
316f5878 10732 if (mips_arch_string != 0)
fef14a42 10733 arch_info = mips_parse_cpu ("-march", mips_arch_string);
e9670677 10734
316f5878 10735 if (file_mips_isa != ISA_UNKNOWN)
e9670677 10736 {
316f5878 10737 /* Handle -mipsN. At this point, file_mips_isa contains the
fef14a42 10738 ISA level specified by -mipsN, while arch_info->isa contains
316f5878 10739 the -march selection (if any). */
fef14a42 10740 if (arch_info != 0)
e9670677 10741 {
316f5878
RS
10742 /* -march takes precedence over -mipsN, since it is more descriptive.
10743 There's no harm in specifying both as long as the ISA levels
10744 are the same. */
fef14a42 10745 if (file_mips_isa != arch_info->isa)
316f5878
RS
10746 as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
10747 mips_cpu_info_from_isa (file_mips_isa)->name,
fef14a42 10748 mips_cpu_info_from_isa (arch_info->isa)->name);
e9670677 10749 }
316f5878 10750 else
fef14a42 10751 arch_info = mips_cpu_info_from_isa (file_mips_isa);
e9670677
MR
10752 }
10753
fef14a42
TS
10754 if (arch_info == 0)
10755 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
e9670677 10756
fef14a42 10757 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
316f5878 10758 as_bad ("-march=%s is not compatible with the selected ABI",
fef14a42
TS
10759 arch_info->name);
10760
10761 mips_set_architecture (arch_info);
10762
10763 /* Optimize for file_mips_arch, unless -mtune selects a different processor. */
10764 if (mips_tune_string != 0)
10765 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
e9670677 10766
fef14a42
TS
10767 if (tune_info == 0)
10768 mips_set_tune (arch_info);
10769 else
10770 mips_set_tune (tune_info);
e9670677 10771
316f5878 10772 if (file_mips_gp32 >= 0)
e9670677 10773 {
316f5878
RS
10774 /* The user specified the size of the integer registers. Make sure
10775 it agrees with the ABI and ISA. */
10776 if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
10777 as_bad (_("-mgp64 used with a 32-bit processor"));
10778 else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
10779 as_bad (_("-mgp32 used with a 64-bit ABI"));
10780 else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
10781 as_bad (_("-mgp64 used with a 32-bit ABI"));
e9670677
MR
10782 }
10783 else
10784 {
316f5878
RS
10785 /* Infer the integer register size from the ABI and processor.
10786 Restrict ourselves to 32-bit registers if that's all the
10787 processor has, or if the ABI cannot handle 64-bit registers. */
10788 file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
10789 || !ISA_HAS_64BIT_REGS (mips_opts.isa));
e9670677
MR
10790 }
10791
316f5878
RS
10792 /* ??? GAS treats single-float processors as though they had 64-bit
10793 float registers (although it complains when double-precision
10794 instructions are used). As things stand, saying they have 32-bit
10795 registers would lead to spurious "register must be even" messages.
10796 So here we assume float registers are always the same size as
10797 integer ones, unless the user says otherwise. */
10798 if (file_mips_fp32 < 0)
10799 file_mips_fp32 = file_mips_gp32;
e9670677 10800
316f5878 10801 /* End of GCC-shared inference code. */
e9670677 10802
17a2f251
TS
10803 /* This flag is set when we have a 64-bit capable CPU but use only
10804 32-bit wide registers. Note that EABI does not use it. */
10805 if (ISA_HAS_64BIT_REGS (mips_opts.isa)
10806 && ((mips_abi == NO_ABI && file_mips_gp32 == 1)
10807 || mips_abi == O32_ABI))
316f5878 10808 mips_32bitmode = 1;
e9670677
MR
10809
10810 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
10811 as_bad (_("trap exception not supported at ISA 1"));
10812
e9670677
MR
10813 /* If the selected architecture includes support for ASEs, enable
10814 generation of code for them. */
a4672219 10815 if (mips_opts.mips16 == -1)
fef14a42 10816 mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_arch)) ? 1 : 0;
ffdefa66 10817 if (mips_opts.ase_mips3d == -1)
fef14a42 10818 mips_opts.ase_mips3d = (CPU_HAS_MIPS3D (file_mips_arch)) ? 1 : 0;
ffdefa66 10819 if (mips_opts.ase_mdmx == -1)
fef14a42 10820 mips_opts.ase_mdmx = (CPU_HAS_MDMX (file_mips_arch)) ? 1 : 0;
e9670677 10821
e9670677 10822 file_mips_isa = mips_opts.isa;
a4672219 10823 file_ase_mips16 = mips_opts.mips16;
e9670677
MR
10824 file_ase_mips3d = mips_opts.ase_mips3d;
10825 file_ase_mdmx = mips_opts.ase_mdmx;
10826 mips_opts.gp32 = file_mips_gp32;
10827 mips_opts.fp32 = file_mips_fp32;
10828
ecb4347a
DJ
10829 if (mips_flag_mdebug < 0)
10830 {
10831#ifdef OBJ_MAYBE_ECOFF
10832 if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
10833 mips_flag_mdebug = 1;
10834 else
10835#endif /* OBJ_MAYBE_ECOFF */
10836 mips_flag_mdebug = 0;
10837 }
e9670677
MR
10838}
10839\f
10840void
17a2f251 10841mips_init_after_args (void)
252b5132
RH
10842{
10843 /* initialize opcodes */
10844 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
beae10d5 10845 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
252b5132
RH
10846}
10847
10848long
17a2f251 10849md_pcrel_from (fixS *fixP)
252b5132 10850{
a7ebbfdf
TS
10851 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
10852 switch (fixP->fx_r_type)
10853 {
10854 case BFD_RELOC_16_PCREL_S2:
10855 case BFD_RELOC_MIPS_JMP:
10856 /* Return the address of the delay slot. */
10857 return addr + 4;
10858 default:
10859 return addr;
10860 }
252b5132
RH
10861}
10862
252b5132
RH
10863/* This is called before the symbol table is processed. In order to
10864 work with gcc when using mips-tfile, we must keep all local labels.
10865 However, in other cases, we want to discard them. If we were
10866 called with -g, but we didn't see any debugging information, it may
10867 mean that gcc is smuggling debugging information through to
10868 mips-tfile, in which case we must generate all local labels. */
10869
10870void
17a2f251 10871mips_frob_file_before_adjust (void)
252b5132
RH
10872{
10873#ifndef NO_ECOFF_DEBUGGING
10874 if (ECOFF_DEBUGGING
10875 && mips_debug != 0
10876 && ! ecoff_debugging_seen)
10877 flag_keep_locals = 1;
10878#endif
10879}
10880
10881/* Sort any unmatched HI16_S relocs so that they immediately precede
94f592af 10882 the corresponding LO reloc. This is called before md_apply_fix3 and
252b5132
RH
10883 tc_gen_reloc. Unmatched HI16_S relocs can only be generated by
10884 explicit use of the %hi modifier. */
10885
10886void
17a2f251 10887mips_frob_file (void)
252b5132
RH
10888{
10889 struct mips_hi_fixup *l;
10890
10891 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
10892 {
10893 segment_info_type *seginfo;
10894 int pass;
10895
5919d012 10896 assert (reloc_needs_lo_p (l->fixp->fx_r_type));
252b5132 10897
5919d012
RS
10898 /* If a GOT16 relocation turns out to be against a global symbol,
10899 there isn't supposed to be a matching LO. */
10900 if (l->fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
10901 && !pic_need_relax (l->fixp->fx_addsy, l->seg))
10902 continue;
10903
10904 /* Check quickly whether the next fixup happens to be a matching %lo. */
10905 if (fixup_has_matching_lo_p (l->fixp))
252b5132
RH
10906 continue;
10907
10908 /* Look through the fixups for this segment for a matching %lo.
10909 When we find one, move the %hi just in front of it. We do
10910 this in two passes. In the first pass, we try to find a
10911 unique %lo. In the second pass, we permit multiple %hi
10912 relocs for a single %lo (this is a GNU extension). */
10913 seginfo = seg_info (l->seg);
10914 for (pass = 0; pass < 2; pass++)
10915 {
10916 fixS *f, *prev;
10917
10918 prev = NULL;
10919 for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
10920 {
10921 /* Check whether this is a %lo fixup which matches l->fixp. */
10922 if (f->fx_r_type == BFD_RELOC_LO16
10923 && f->fx_addsy == l->fixp->fx_addsy
10924 && f->fx_offset == l->fixp->fx_offset
10925 && (pass == 1
10926 || prev == NULL
5919d012
RS
10927 || !reloc_needs_lo_p (prev->fx_r_type)
10928 || !fixup_has_matching_lo_p (prev)))
252b5132
RH
10929 {
10930 fixS **pf;
10931
10932 /* Move l->fixp before f. */
10933 for (pf = &seginfo->fix_root;
10934 *pf != l->fixp;
10935 pf = &(*pf)->fx_next)
10936 assert (*pf != NULL);
10937
10938 *pf = l->fixp->fx_next;
10939
10940 l->fixp->fx_next = f;
10941 if (prev == NULL)
10942 seginfo->fix_root = l->fixp;
10943 else
10944 prev->fx_next = l->fixp;
10945
10946 break;
10947 }
10948
10949 prev = f;
10950 }
10951
10952 if (f != NULL)
10953 break;
10954
10955#if 0 /* GCC code motion plus incomplete dead code elimination
10956 can leave a %hi without a %lo. */
10957 if (pass == 1)
10958 as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
10959 _("Unmatched %%hi reloc"));
10960#endif
10961 }
10962 }
10963}
10964
10965/* When generating embedded PIC code we need to use a special
10966 relocation to represent the difference of two symbols in the .text
10967 section (switch tables use a difference of this sort). See
10968 include/coff/mips.h for details. This macro checks whether this
10969 fixup requires the special reloc. */
10970#define SWITCH_TABLE(fixp) \
10971 ((fixp)->fx_r_type == BFD_RELOC_32 \
bb2d6cd7 10972 && OUTPUT_FLAVOR != bfd_target_elf_flavour \
252b5132
RH
10973 && (fixp)->fx_addsy != NULL \
10974 && (fixp)->fx_subsy != NULL \
10975 && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
10976 && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
10977
10978/* When generating embedded PIC code we must keep all PC relative
10979 relocations, in case the linker has to relax a call. We also need
f6688943
TS
10980 to keep relocations for switch table entries.
10981
10982 We may have combined relocations without symbols in the N32/N64 ABI.
10983 We have to prevent gas from dropping them. */
252b5132 10984
252b5132 10985int
17a2f251 10986mips_force_relocation (fixS *fixp)
252b5132 10987{
ae6063d4 10988 if (generic_force_reloc (fixp))
252b5132
RH
10989 return 1;
10990
f6688943
TS
10991 if (HAVE_NEWABI
10992 && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
10993 && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
10994 || fixp->fx_r_type == BFD_RELOC_HI16_S
10995 || fixp->fx_r_type == BFD_RELOC_LO16))
10996 return 1;
10997
252b5132
RH
10998 return (mips_pic == EMBEDDED_PIC
10999 && (fixp->fx_pcrel
11000 || SWITCH_TABLE (fixp)
11001 || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
11002 || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
11003}
11004
45f8dfe8
AO
11005/* This hook is called before a fix is simplified. We don't really
11006 decide whether to skip a fix here. Rather, we turn global symbols
11007 used as branch targets into local symbols, such that they undergo
11008 simplification. We can only do this if the symbol is defined and
11009 it is in the same section as the branch. If this doesn't hold, we
11010 emit a better error message than just saying the relocation is not
11011 valid for the selected object format.
11012
11013 FIXP is the fix-up we're going to try to simplify, SEG is the
11014 segment in which the fix up occurs. The return value should be
11015 non-zero to indicate the fix-up is valid for further
11016 simplifications. */
11017
11018int
17a2f251 11019mips_validate_fix (struct fix *fixP, asection *seg)
45f8dfe8
AO
11020{
11021 /* There's a lot of discussion on whether it should be possible to
11022 use R_MIPS_PC16 to represent branch relocations. The outcome
11023 seems to be that it can, but gas/bfd are very broken in creating
11024 RELA relocations for this, so for now we only accept branches to
11025 symbols in the same section. Anything else is of dubious value,
11026 since there's no guarantee that at link time the symbol would be
11027 in range. Even for branches to local symbols this is arguably
11028 wrong, since it we assume the symbol is not going to be
11029 overridden, which should be possible per ELF library semantics,
11030 but then, there isn't a dynamic relocation that could be used to
11031 this effect, and the target would likely be out of range as well.
11032
11033 Unfortunately, it seems that there is too much code out there
11034 that relies on branches to symbols that are global to be resolved
11035 as if they were local, like the IRIX tools do, so we do it as
11036 well, but with a warning so that people are reminded to fix their
11037 code. If we ever get back to using R_MIPS_PC16 for branch
11038 targets, this entire block should go away (and probably the
11039 whole function). */
11040
11041 if (fixP->fx_r_type == BFD_RELOC_16_PCREL_S2
11042 && (((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
11043 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
11044 && mips_pic != EMBEDDED_PIC)
11045 || bfd_reloc_type_lookup (stdoutput, BFD_RELOC_16_PCREL_S2) == NULL)
11046 && fixP->fx_addsy)
11047 {
11048 if (! S_IS_DEFINED (fixP->fx_addsy))
11049 {
11050 as_bad_where (fixP->fx_file, fixP->fx_line,
11051 _("Cannot branch to undefined symbol."));
11052 /* Avoid any further errors about this fixup. */
11053 fixP->fx_done = 1;
11054 }
11055 else if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
11056 {
11057 as_bad_where (fixP->fx_file, fixP->fx_line,
11058 _("Cannot branch to symbol in another section."));
11059 fixP->fx_done = 1;
11060 }
11061 else if (S_IS_EXTERNAL (fixP->fx_addsy))
11062 {
11063 symbolS *sym = fixP->fx_addsy;
11064
115695a8
CD
11065 if (mips_pic == SVR4_PIC)
11066 as_warn_where (fixP->fx_file, fixP->fx_line,
11067 _("Pretending global symbol used as branch target is local."));
45f8dfe8
AO
11068
11069 fixP->fx_addsy = symbol_create (S_GET_NAME (sym),
11070 S_GET_SEGMENT (sym),
11071 S_GET_VALUE (sym),
11072 symbol_get_frag (sym));
11073 copy_symbol_attributes (fixP->fx_addsy, sym);
11074 S_CLEAR_EXTERNAL (fixP->fx_addsy);
11075 assert (symbol_resolved_p (sym));
11076 symbol_mark_resolved (fixP->fx_addsy);
11077 }
11078 }
11079
11080 return 1;
11081}
11082
252b5132
RH
11083/* Apply a fixup to the object file. */
11084
94f592af 11085void
17a2f251 11086md_apply_fix3 (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 11087{
874e8986 11088 bfd_byte *buf;
98aa84af 11089 long insn;
ed6fb7bd 11090 static int previous_fx_r_type = 0;
a7ebbfdf 11091 reloc_howto_type *howto;
252b5132 11092
a7ebbfdf
TS
11093 /* We ignore generic BFD relocations we don't know about. */
11094 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
11095 if (! howto)
11096 return;
65551fa4 11097
252b5132
RH
11098 assert (fixP->fx_size == 4
11099 || fixP->fx_r_type == BFD_RELOC_16
11100 || fixP->fx_r_type == BFD_RELOC_64
f6688943
TS
11101 || fixP->fx_r_type == BFD_RELOC_CTOR
11102 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
252b5132 11103 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
a7ebbfdf 11104 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY);
252b5132 11105
a7ebbfdf 11106 buf = (bfd_byte *) (fixP->fx_frag->fr_literal + fixP->fx_where);
252b5132 11107
ed6fb7bd
SC
11108 /* We are not done if this is a composite relocation to set up gp. */
11109 if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel
11110 && !(fixP->fx_r_type == BFD_RELOC_MIPS_SUB
10181a0d
AO
11111 || (fixP->fx_r_type == BFD_RELOC_64
11112 && (previous_fx_r_type == BFD_RELOC_GPREL32
11113 || previous_fx_r_type == BFD_RELOC_GPREL16))
ed6fb7bd
SC
11114 || (previous_fx_r_type == BFD_RELOC_MIPS_SUB
11115 && (fixP->fx_r_type == BFD_RELOC_HI16_S
11116 || fixP->fx_r_type == BFD_RELOC_LO16))))
252b5132 11117 fixP->fx_done = 1;
ed6fb7bd 11118 previous_fx_r_type = fixP->fx_r_type;
252b5132
RH
11119
11120 switch (fixP->fx_r_type)
11121 {
11122 case BFD_RELOC_MIPS_JMP:
e369bcce
TS
11123 case BFD_RELOC_MIPS_SHIFT5:
11124 case BFD_RELOC_MIPS_SHIFT6:
11125 case BFD_RELOC_MIPS_GOT_DISP:
11126 case BFD_RELOC_MIPS_GOT_PAGE:
11127 case BFD_RELOC_MIPS_GOT_OFST:
11128 case BFD_RELOC_MIPS_SUB:
11129 case BFD_RELOC_MIPS_INSERT_A:
11130 case BFD_RELOC_MIPS_INSERT_B:
11131 case BFD_RELOC_MIPS_DELETE:
11132 case BFD_RELOC_MIPS_HIGHEST:
11133 case BFD_RELOC_MIPS_HIGHER:
11134 case BFD_RELOC_MIPS_SCN_DISP:
11135 case BFD_RELOC_MIPS_REL16:
11136 case BFD_RELOC_MIPS_RELGOT:
11137 case BFD_RELOC_MIPS_JALR:
252b5132
RH
11138 case BFD_RELOC_HI16:
11139 case BFD_RELOC_HI16_S:
cdf6fd85 11140 case BFD_RELOC_GPREL16:
252b5132
RH
11141 case BFD_RELOC_MIPS_LITERAL:
11142 case BFD_RELOC_MIPS_CALL16:
11143 case BFD_RELOC_MIPS_GOT16:
cdf6fd85 11144 case BFD_RELOC_GPREL32:
252b5132
RH
11145 case BFD_RELOC_MIPS_GOT_HI16:
11146 case BFD_RELOC_MIPS_GOT_LO16:
11147 case BFD_RELOC_MIPS_CALL_HI16:
11148 case BFD_RELOC_MIPS_CALL_LO16:
11149 case BFD_RELOC_MIPS16_GPREL:
11150 if (fixP->fx_pcrel)
11151 as_bad_where (fixP->fx_file, fixP->fx_line,
11152 _("Invalid PC relative reloc"));
11153 /* Nothing needed to do. The value comes from the reloc entry */
11154 break;
11155
11156 case BFD_RELOC_MIPS16_JMP:
11157 /* We currently always generate a reloc against a symbol, which
11158 means that we don't want an addend even if the symbol is
11159 defined. */
a7ebbfdf 11160 *valP = 0;
252b5132
RH
11161 break;
11162
11163 case BFD_RELOC_PCREL_HI16_S:
11164 /* The addend for this is tricky if it is internal, so we just
11165 do everything here rather than in bfd_install_relocation. */
a7ebbfdf 11166 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && !fixP->fx_done)
bb2d6cd7
GK
11167 break;
11168 if (fixP->fx_addsy
11169 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
252b5132
RH
11170 {
11171 /* For an external symbol adjust by the address to make it
11172 pcrel_offset. We use the address of the RELLO reloc
11173 which follows this one. */
a7ebbfdf 11174 *valP += (fixP->fx_next->fx_frag->fr_address
252b5132
RH
11175 + fixP->fx_next->fx_where);
11176 }
a7ebbfdf 11177 *valP = ((*valP + 0x8000) >> 16) & 0xffff;
252b5132
RH
11178 if (target_big_endian)
11179 buf += 2;
17a2f251 11180 md_number_to_chars (buf, *valP, 2);
252b5132
RH
11181 break;
11182
11183 case BFD_RELOC_PCREL_LO16:
11184 /* The addend for this is tricky if it is internal, so we just
11185 do everything here rather than in bfd_install_relocation. */
a7ebbfdf 11186 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && !fixP->fx_done)
bb2d6cd7
GK
11187 break;
11188 if (fixP->fx_addsy
11189 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
a7ebbfdf 11190 *valP += fixP->fx_frag->fr_address + fixP->fx_where;
252b5132
RH
11191 if (target_big_endian)
11192 buf += 2;
17a2f251 11193 md_number_to_chars (buf, *valP, 2);
252b5132
RH
11194 break;
11195
11196 case BFD_RELOC_64:
11197 /* This is handled like BFD_RELOC_32, but we output a sign
11198 extended value if we are only 32 bits. */
11199 if (fixP->fx_done
11200 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11201 {
11202 if (8 <= sizeof (valueT))
a7ebbfdf 11203 md_number_to_chars (buf, *valP, 8);
252b5132
RH
11204 else
11205 {
a7ebbfdf 11206 valueT hiv;
252b5132 11207
a7ebbfdf 11208 if ((*valP & 0x80000000) != 0)
252b5132
RH
11209 hiv = 0xffffffff;
11210 else
11211 hiv = 0;
a7ebbfdf
TS
11212 md_number_to_chars ((char *)(buf + target_big_endian ? 4 : 0),
11213 *valP, 4);
11214 md_number_to_chars ((char *)(buf + target_big_endian ? 0 : 4),
11215 hiv, 4);
252b5132
RH
11216 }
11217 }
11218 break;
11219
056350c6 11220 case BFD_RELOC_RVA:
252b5132
RH
11221 case BFD_RELOC_32:
11222 /* If we are deleting this reloc entry, we must fill in the
11223 value now. This can happen if we have a .word which is not
11224 resolved when it appears but is later defined. We also need
11225 to fill in the value if this is an embedded PIC switch table
11226 entry. */
11227 if (fixP->fx_done
11228 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
a7ebbfdf 11229 md_number_to_chars (buf, *valP, 4);
252b5132
RH
11230 break;
11231
11232 case BFD_RELOC_16:
11233 /* If we are deleting this reloc entry, we must fill in the
11234 value now. */
11235 assert (fixP->fx_size == 2);
11236 if (fixP->fx_done)
a7ebbfdf 11237 md_number_to_chars (buf, *valP, 2);
252b5132
RH
11238 break;
11239
11240 case BFD_RELOC_LO16:
11241 /* When handling an embedded PIC switch statement, we can wind
11242 up deleting a LO16 reloc. See the 'o' case in mips_ip. */
11243 if (fixP->fx_done)
11244 {
a7ebbfdf 11245 if (*valP + 0x8000 > 0xffff)
252b5132
RH
11246 as_bad_where (fixP->fx_file, fixP->fx_line,
11247 _("relocation overflow"));
252b5132
RH
11248 if (target_big_endian)
11249 buf += 2;
17a2f251 11250 md_number_to_chars (buf, *valP, 2);
252b5132
RH
11251 }
11252 break;
11253
11254 case BFD_RELOC_16_PCREL_S2:
a7ebbfdf 11255 if ((*valP & 0x3) != 0)
cb56d3d3 11256 as_bad_where (fixP->fx_file, fixP->fx_line,
a7ebbfdf 11257 _("Branch to odd address (%lx)"), (long) *valP);
cb56d3d3 11258
252b5132
RH
11259 /*
11260 * We need to save the bits in the instruction since fixup_segment()
11261 * might be deleting the relocation entry (i.e., a branch within
11262 * the current segment).
11263 */
a7ebbfdf 11264 if (! fixP->fx_done)
bb2d6cd7 11265 break;
252b5132
RH
11266
11267 /* update old instruction data */
252b5132
RH
11268 if (target_big_endian)
11269 insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
11270 else
11271 insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
11272
a7ebbfdf
TS
11273 if (*valP + 0x20000 <= 0x3ffff)
11274 {
11275 insn |= (*valP >> 2) & 0xffff;
17a2f251 11276 md_number_to_chars (buf, insn, 4);
a7ebbfdf
TS
11277 }
11278 else if (mips_pic == NO_PIC
11279 && fixP->fx_done
11280 && fixP->fx_frag->fr_address >= text_section->vma
11281 && (fixP->fx_frag->fr_address
11282 < text_section->vma + text_section->_raw_size)
11283 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
11284 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
11285 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
252b5132
RH
11286 {
11287 /* The branch offset is too large. If this is an
11288 unconditional branch, and we are not generating PIC code,
11289 we can convert it to an absolute jump instruction. */
a7ebbfdf
TS
11290 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
11291 insn = 0x0c000000; /* jal */
252b5132 11292 else
a7ebbfdf
TS
11293 insn = 0x08000000; /* j */
11294 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
11295 fixP->fx_done = 0;
11296 fixP->fx_addsy = section_symbol (text_section);
11297 *valP += md_pcrel_from (fixP);
17a2f251 11298 md_number_to_chars (buf, insn, 4);
a7ebbfdf
TS
11299 }
11300 else
11301 {
11302 /* If we got here, we have branch-relaxation disabled,
11303 and there's nothing we can do to fix this instruction
11304 without turning it into a longer sequence. */
11305 as_bad_where (fixP->fx_file, fixP->fx_line,
11306 _("Branch out of range"));
252b5132 11307 }
252b5132
RH
11308 break;
11309
11310 case BFD_RELOC_VTABLE_INHERIT:
11311 fixP->fx_done = 0;
11312 if (fixP->fx_addsy
11313 && !S_IS_DEFINED (fixP->fx_addsy)
11314 && !S_IS_WEAK (fixP->fx_addsy))
11315 S_SET_WEAK (fixP->fx_addsy);
11316 break;
11317
11318 case BFD_RELOC_VTABLE_ENTRY:
11319 fixP->fx_done = 0;
11320 break;
11321
11322 default:
11323 internalError ();
11324 }
a7ebbfdf
TS
11325
11326 /* Remember value for tc_gen_reloc. */
11327 fixP->fx_addnumber = *valP;
252b5132
RH
11328}
11329
11330#if 0
11331void
17a2f251 11332printInsn (unsigned long oc)
252b5132
RH
11333{
11334 const struct mips_opcode *p;
11335 int treg, sreg, dreg, shamt;
11336 short imm;
11337 const char *args;
11338 int i;
11339
11340 for (i = 0; i < NUMOPCODES; ++i)
11341 {
11342 p = &mips_opcodes[i];
11343 if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
11344 {
11345 printf ("%08lx %s\t", oc, p->name);
11346 treg = (oc >> 16) & 0x1f;
11347 sreg = (oc >> 21) & 0x1f;
11348 dreg = (oc >> 11) & 0x1f;
11349 shamt = (oc >> 6) & 0x1f;
11350 imm = oc;
11351 for (args = p->args;; ++args)
11352 {
11353 switch (*args)
11354 {
11355 case '\0':
11356 printf ("\n");
11357 break;
11358
11359 case ',':
11360 case '(':
11361 case ')':
11362 printf ("%c", *args);
11363 continue;
11364
11365 case 'r':
11366 assert (treg == sreg);
11367 printf ("$%d,$%d", treg, sreg);
11368 continue;
11369
11370 case 'd':
11371 case 'G':
11372 printf ("$%d", dreg);
11373 continue;
11374
11375 case 't':
11376 case 'E':
11377 printf ("$%d", treg);
11378 continue;
11379
11380 case 'k':
11381 printf ("0x%x", treg);
11382 continue;
11383
11384 case 'b':
11385 case 's':
11386 printf ("$%d", sreg);
11387 continue;
11388
11389 case 'a':
11390 printf ("0x%08lx", oc & 0x1ffffff);
11391 continue;
11392
11393 case 'i':
11394 case 'j':
11395 case 'o':
11396 case 'u':
11397 printf ("%d", imm);
11398 continue;
11399
11400 case '<':
11401 case '>':
11402 printf ("$%d", shamt);
11403 continue;
11404
11405 default:
11406 internalError ();
11407 }
11408 break;
11409 }
11410 return;
11411 }
11412 }
11413 printf (_("%08lx UNDEFINED\n"), oc);
11414}
11415#endif
11416
11417static symbolS *
17a2f251 11418get_symbol (void)
252b5132
RH
11419{
11420 int c;
11421 char *name;
11422 symbolS *p;
11423
11424 name = input_line_pointer;
11425 c = get_symbol_end ();
11426 p = (symbolS *) symbol_find_or_make (name);
11427 *input_line_pointer = c;
11428 return p;
11429}
11430
11431/* Align the current frag to a given power of two. The MIPS assembler
11432 also automatically adjusts any preceding label. */
11433
11434static void
17a2f251 11435mips_align (int to, int fill, symbolS *label)
252b5132 11436{
b34976b6 11437 mips_emit_delays (FALSE);
252b5132
RH
11438 frag_align (to, fill, 0);
11439 record_alignment (now_seg, to);
11440 if (label != NULL)
11441 {
11442 assert (S_GET_SEGMENT (label) == now_seg);
49309057 11443 symbol_set_frag (label, frag_now);
252b5132
RH
11444 S_SET_VALUE (label, (valueT) frag_now_fix ());
11445 }
11446}
11447
11448/* Align to a given power of two. .align 0 turns off the automatic
11449 alignment used by the data creating pseudo-ops. */
11450
11451static void
17a2f251 11452s_align (int x ATTRIBUTE_UNUSED)
252b5132
RH
11453{
11454 register int temp;
11455 register long temp_fill;
11456 long max_alignment = 15;
11457
11458 /*
11459
67c1ffbe 11460 o Note that the assembler pulls down any immediately preceding label
252b5132
RH
11461 to the aligned address.
11462 o It's not documented but auto alignment is reinstated by
11463 a .align pseudo instruction.
11464 o Note also that after auto alignment is turned off the mips assembler
11465 issues an error on attempt to assemble an improperly aligned data item.
11466 We don't.
11467
11468 */
11469
11470 temp = get_absolute_expression ();
11471 if (temp > max_alignment)
11472 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
11473 else if (temp < 0)
11474 {
11475 as_warn (_("Alignment negative: 0 assumed."));
11476 temp = 0;
11477 }
11478 if (*input_line_pointer == ',')
11479 {
f9419b05 11480 ++input_line_pointer;
252b5132
RH
11481 temp_fill = get_absolute_expression ();
11482 }
11483 else
11484 temp_fill = 0;
11485 if (temp)
11486 {
11487 auto_align = 1;
11488 mips_align (temp, (int) temp_fill,
11489 insn_labels != NULL ? insn_labels->label : NULL);
11490 }
11491 else
11492 {
11493 auto_align = 0;
11494 }
11495
11496 demand_empty_rest_of_line ();
11497}
11498
11499void
17a2f251 11500mips_flush_pending_output (void)
252b5132 11501{
b34976b6 11502 mips_emit_delays (FALSE);
252b5132
RH
11503 mips_clear_insn_labels ();
11504}
11505
11506static void
17a2f251 11507s_change_sec (int sec)
252b5132
RH
11508{
11509 segT seg;
11510
11511 /* When generating embedded PIC code, we only use the .text, .lit8,
11512 .sdata and .sbss sections. We change the .data and .rdata
11513 pseudo-ops to use .sdata. */
11514 if (mips_pic == EMBEDDED_PIC
11515 && (sec == 'd' || sec == 'r'))
11516 sec = 's';
11517
11518#ifdef OBJ_ELF
11519 /* The ELF backend needs to know that we are changing sections, so
11520 that .previous works correctly. We could do something like check
b6ff326e 11521 for an obj_section_change_hook macro, but that might be confusing
252b5132
RH
11522 as it would not be appropriate to use it in the section changing
11523 functions in read.c, since obj-elf.c intercepts those. FIXME:
11524 This should be cleaner, somehow. */
11525 obj_elf_section_change_hook ();
11526#endif
11527
b34976b6 11528 mips_emit_delays (FALSE);
252b5132
RH
11529 switch (sec)
11530 {
11531 case 't':
11532 s_text (0);
11533 break;
11534 case 'd':
11535 s_data (0);
11536 break;
11537 case 'b':
11538 subseg_set (bss_section, (subsegT) get_absolute_expression ());
11539 demand_empty_rest_of_line ();
11540 break;
11541
11542 case 'r':
11543 if (USE_GLOBAL_POINTER_OPT)
11544 {
11545 seg = subseg_new (RDATA_SECTION_NAME,
11546 (subsegT) get_absolute_expression ());
11547 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11548 {
11549 bfd_set_section_flags (stdoutput, seg,
11550 (SEC_ALLOC
11551 | SEC_LOAD
11552 | SEC_READONLY
11553 | SEC_RELOC
11554 | SEC_DATA));
11555 if (strcmp (TARGET_OS, "elf") != 0)
e799a695 11556 record_alignment (seg, 4);
252b5132
RH
11557 }
11558 demand_empty_rest_of_line ();
11559 }
11560 else
11561 {
11562 as_bad (_("No read only data section in this object file format"));
11563 demand_empty_rest_of_line ();
11564 return;
11565 }
11566 break;
11567
11568 case 's':
11569 if (USE_GLOBAL_POINTER_OPT)
11570 {
11571 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
11572 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11573 {
11574 bfd_set_section_flags (stdoutput, seg,
11575 SEC_ALLOC | SEC_LOAD | SEC_RELOC
11576 | SEC_DATA);
11577 if (strcmp (TARGET_OS, "elf") != 0)
e799a695 11578 record_alignment (seg, 4);
252b5132
RH
11579 }
11580 demand_empty_rest_of_line ();
11581 break;
11582 }
11583 else
11584 {
11585 as_bad (_("Global pointers not supported; recompile -G 0"));
11586 demand_empty_rest_of_line ();
11587 return;
11588 }
11589 }
11590
11591 auto_align = 1;
11592}
b34976b6 11593
cca86cc8 11594void
17a2f251 11595s_change_section (int ignore ATTRIBUTE_UNUSED)
cca86cc8 11596{
7ed4a06a 11597#ifdef OBJ_ELF
cca86cc8
SC
11598 char *section_name;
11599 char c;
684022ea 11600 char next_c = 0;
cca86cc8
SC
11601 int section_type;
11602 int section_flag;
11603 int section_entry_size;
11604 int section_alignment;
b34976b6 11605
7ed4a06a
TS
11606 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11607 return;
11608
cca86cc8
SC
11609 section_name = input_line_pointer;
11610 c = get_symbol_end ();
a816d1ed
AO
11611 if (c)
11612 next_c = *(input_line_pointer + 1);
cca86cc8 11613
4cf0dd0d
TS
11614 /* Do we have .section Name<,"flags">? */
11615 if (c != ',' || (c == ',' && next_c == '"'))
cca86cc8 11616 {
4cf0dd0d
TS
11617 /* just after name is now '\0'. */
11618 *input_line_pointer = c;
cca86cc8
SC
11619 input_line_pointer = section_name;
11620 obj_elf_section (ignore);
11621 return;
11622 }
11623 input_line_pointer++;
11624
11625 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
11626 if (c == ',')
11627 section_type = get_absolute_expression ();
11628 else
11629 section_type = 0;
11630 if (*input_line_pointer++ == ',')
11631 section_flag = get_absolute_expression ();
11632 else
11633 section_flag = 0;
11634 if (*input_line_pointer++ == ',')
11635 section_entry_size = get_absolute_expression ();
11636 else
11637 section_entry_size = 0;
11638 if (*input_line_pointer++ == ',')
11639 section_alignment = get_absolute_expression ();
11640 else
11641 section_alignment = 0;
11642
a816d1ed
AO
11643 section_name = xstrdup (section_name);
11644
8ab8a5c8
RS
11645 /* When using the generic form of .section (as implemented by obj-elf.c),
11646 there's no way to set the section type to SHT_MIPS_DWARF. Users have
11647 traditionally had to fall back on the more common @progbits instead.
11648
11649 There's nothing really harmful in this, since bfd will correct
11650 SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file. But it
11651 means that, for backwards compatibiltiy, the special_section entries
11652 for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
11653
11654 Even so, we shouldn't force users of the MIPS .section syntax to
11655 incorrectly label the sections as SHT_PROGBITS. The best compromise
11656 seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
11657 generic type-checking code. */
11658 if (section_type == SHT_MIPS_DWARF)
11659 section_type = SHT_PROGBITS;
11660
cca86cc8
SC
11661 obj_elf_change_section (section_name, section_type, section_flag,
11662 section_entry_size, 0, 0, 0);
a816d1ed
AO
11663
11664 if (now_seg->name != section_name)
11665 free (section_name);
7ed4a06a 11666#endif /* OBJ_ELF */
cca86cc8 11667}
252b5132
RH
11668
11669void
17a2f251 11670mips_enable_auto_align (void)
252b5132
RH
11671{
11672 auto_align = 1;
11673}
11674
11675static void
17a2f251 11676s_cons (int log_size)
252b5132
RH
11677{
11678 symbolS *label;
11679
11680 label = insn_labels != NULL ? insn_labels->label : NULL;
b34976b6 11681 mips_emit_delays (FALSE);
252b5132
RH
11682 if (log_size > 0 && auto_align)
11683 mips_align (log_size, 0, label);
11684 mips_clear_insn_labels ();
11685 cons (1 << log_size);
11686}
11687
11688static void
17a2f251 11689s_float_cons (int type)
252b5132
RH
11690{
11691 symbolS *label;
11692
11693 label = insn_labels != NULL ? insn_labels->label : NULL;
11694
b34976b6 11695 mips_emit_delays (FALSE);
252b5132
RH
11696
11697 if (auto_align)
49309057
ILT
11698 {
11699 if (type == 'd')
11700 mips_align (3, 0, label);
11701 else
11702 mips_align (2, 0, label);
11703 }
252b5132
RH
11704
11705 mips_clear_insn_labels ();
11706
11707 float_cons (type);
11708}
11709
11710/* Handle .globl. We need to override it because on Irix 5 you are
11711 permitted to say
11712 .globl foo .text
11713 where foo is an undefined symbol, to mean that foo should be
11714 considered to be the address of a function. */
11715
11716static void
17a2f251 11717s_mips_globl (int x ATTRIBUTE_UNUSED)
252b5132
RH
11718{
11719 char *name;
11720 int c;
11721 symbolS *symbolP;
11722 flagword flag;
11723
11724 name = input_line_pointer;
11725 c = get_symbol_end ();
11726 symbolP = symbol_find_or_make (name);
11727 *input_line_pointer = c;
11728 SKIP_WHITESPACE ();
11729
11730 /* On Irix 5, every global symbol that is not explicitly labelled as
11731 being a function is apparently labelled as being an object. */
11732 flag = BSF_OBJECT;
11733
11734 if (! is_end_of_line[(unsigned char) *input_line_pointer])
11735 {
11736 char *secname;
11737 asection *sec;
11738
11739 secname = input_line_pointer;
11740 c = get_symbol_end ();
11741 sec = bfd_get_section_by_name (stdoutput, secname);
11742 if (sec == NULL)
11743 as_bad (_("%s: no such section"), secname);
11744 *input_line_pointer = c;
11745
11746 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
11747 flag = BSF_FUNCTION;
11748 }
11749
49309057 11750 symbol_get_bfdsym (symbolP)->flags |= flag;
252b5132
RH
11751
11752 S_SET_EXTERNAL (symbolP);
11753 demand_empty_rest_of_line ();
11754}
11755
11756static void
17a2f251 11757s_option (int x ATTRIBUTE_UNUSED)
252b5132
RH
11758{
11759 char *opt;
11760 char c;
11761
11762 opt = input_line_pointer;
11763 c = get_symbol_end ();
11764
11765 if (*opt == 'O')
11766 {
11767 /* FIXME: What does this mean? */
11768 }
11769 else if (strncmp (opt, "pic", 3) == 0)
11770 {
11771 int i;
11772
11773 i = atoi (opt + 3);
11774 if (i == 0)
11775 mips_pic = NO_PIC;
11776 else if (i == 2)
143d77c5 11777 {
252b5132 11778 mips_pic = SVR4_PIC;
143d77c5
EC
11779 mips_abicalls = TRUE;
11780 }
252b5132
RH
11781 else
11782 as_bad (_(".option pic%d not supported"), i);
11783
11784 if (USE_GLOBAL_POINTER_OPT && mips_pic == SVR4_PIC)
11785 {
11786 if (g_switch_seen && g_switch_value != 0)
11787 as_warn (_("-G may not be used with SVR4 PIC code"));
11788 g_switch_value = 0;
11789 bfd_set_gp_size (stdoutput, 0);
11790 }
11791 }
11792 else
11793 as_warn (_("Unrecognized option \"%s\""), opt);
11794
11795 *input_line_pointer = c;
11796 demand_empty_rest_of_line ();
11797}
11798
11799/* This structure is used to hold a stack of .set values. */
11800
e972090a
NC
11801struct mips_option_stack
11802{
252b5132
RH
11803 struct mips_option_stack *next;
11804 struct mips_set_options options;
11805};
11806
11807static struct mips_option_stack *mips_opts_stack;
11808
11809/* Handle the .set pseudo-op. */
11810
11811static void
17a2f251 11812s_mipsset (int x ATTRIBUTE_UNUSED)
252b5132
RH
11813{
11814 char *name = input_line_pointer, ch;
11815
11816 while (!is_end_of_line[(unsigned char) *input_line_pointer])
f9419b05 11817 ++input_line_pointer;
252b5132
RH
11818 ch = *input_line_pointer;
11819 *input_line_pointer = '\0';
11820
11821 if (strcmp (name, "reorder") == 0)
11822 {
11823 if (mips_opts.noreorder && prev_nop_frag != NULL)
11824 {
11825 /* If we still have pending nops, we can discard them. The
11826 usual nop handling will insert any that are still
bdaaa2e1 11827 needed. */
252b5132
RH
11828 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11829 * (mips_opts.mips16 ? 2 : 4));
11830 prev_nop_frag = NULL;
11831 }
11832 mips_opts.noreorder = 0;
11833 }
11834 else if (strcmp (name, "noreorder") == 0)
11835 {
b34976b6 11836 mips_emit_delays (TRUE);
252b5132
RH
11837 mips_opts.noreorder = 1;
11838 mips_any_noreorder = 1;
11839 }
11840 else if (strcmp (name, "at") == 0)
11841 {
11842 mips_opts.noat = 0;
11843 }
11844 else if (strcmp (name, "noat") == 0)
11845 {
11846 mips_opts.noat = 1;
11847 }
11848 else if (strcmp (name, "macro") == 0)
11849 {
11850 mips_opts.warn_about_macros = 0;
11851 }
11852 else if (strcmp (name, "nomacro") == 0)
11853 {
11854 if (mips_opts.noreorder == 0)
11855 as_bad (_("`noreorder' must be set before `nomacro'"));
11856 mips_opts.warn_about_macros = 1;
11857 }
11858 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
11859 {
11860 mips_opts.nomove = 0;
11861 }
11862 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
11863 {
11864 mips_opts.nomove = 1;
11865 }
11866 else if (strcmp (name, "bopt") == 0)
11867 {
11868 mips_opts.nobopt = 0;
11869 }
11870 else if (strcmp (name, "nobopt") == 0)
11871 {
11872 mips_opts.nobopt = 1;
11873 }
11874 else if (strcmp (name, "mips16") == 0
11875 || strcmp (name, "MIPS-16") == 0)
11876 mips_opts.mips16 = 1;
11877 else if (strcmp (name, "nomips16") == 0
11878 || strcmp (name, "noMIPS-16") == 0)
11879 mips_opts.mips16 = 0;
1f25f5d3
CD
11880 else if (strcmp (name, "mips3d") == 0)
11881 mips_opts.ase_mips3d = 1;
11882 else if (strcmp (name, "nomips3d") == 0)
11883 mips_opts.ase_mips3d = 0;
a4672219
TS
11884 else if (strcmp (name, "mdmx") == 0)
11885 mips_opts.ase_mdmx = 1;
11886 else if (strcmp (name, "nomdmx") == 0)
11887 mips_opts.ase_mdmx = 0;
1a2c1fad 11888 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
252b5132 11889 {
af7ee8bf 11890 int reset = 0;
252b5132 11891
1a2c1fad
CD
11892 /* Permit the user to change the ISA and architecture on the fly.
11893 Needless to say, misuse can cause serious problems. */
af7ee8bf
CD
11894 if (strcmp (name, "mips0") == 0)
11895 {
11896 reset = 1;
11897 mips_opts.isa = file_mips_isa;
11898 }
11899 else if (strcmp (name, "mips1") == 0)
11900 mips_opts.isa = ISA_MIPS1;
11901 else if (strcmp (name, "mips2") == 0)
11902 mips_opts.isa = ISA_MIPS2;
11903 else if (strcmp (name, "mips3") == 0)
11904 mips_opts.isa = ISA_MIPS3;
11905 else if (strcmp (name, "mips4") == 0)
11906 mips_opts.isa = ISA_MIPS4;
11907 else if (strcmp (name, "mips5") == 0)
11908 mips_opts.isa = ISA_MIPS5;
11909 else if (strcmp (name, "mips32") == 0)
11910 mips_opts.isa = ISA_MIPS32;
11911 else if (strcmp (name, "mips32r2") == 0)
11912 mips_opts.isa = ISA_MIPS32R2;
11913 else if (strcmp (name, "mips64") == 0)
11914 mips_opts.isa = ISA_MIPS64;
5f74bc13
CD
11915 else if (strcmp (name, "mips64r2") == 0)
11916 mips_opts.isa = ISA_MIPS64R2;
1a2c1fad
CD
11917 else if (strcmp (name, "arch=default") == 0)
11918 {
11919 reset = 1;
11920 mips_opts.arch = file_mips_arch;
11921 mips_opts.isa = file_mips_isa;
11922 }
11923 else if (strncmp (name, "arch=", 5) == 0)
11924 {
11925 const struct mips_cpu_info *p;
11926
11927 p = mips_parse_cpu("internal use", name + 5);
11928 if (!p)
11929 as_bad (_("unknown architecture %s"), name + 5);
11930 else
11931 {
11932 mips_opts.arch = p->cpu;
11933 mips_opts.isa = p->isa;
11934 }
11935 }
af7ee8bf
CD
11936 else
11937 as_bad (_("unknown ISA level %s"), name + 4);
11938
11939 switch (mips_opts.isa)
98d3f06f
KH
11940 {
11941 case 0:
98d3f06f 11942 break;
af7ee8bf
CD
11943 case ISA_MIPS1:
11944 case ISA_MIPS2:
11945 case ISA_MIPS32:
11946 case ISA_MIPS32R2:
98d3f06f
KH
11947 mips_opts.gp32 = 1;
11948 mips_opts.fp32 = 1;
11949 break;
af7ee8bf
CD
11950 case ISA_MIPS3:
11951 case ISA_MIPS4:
11952 case ISA_MIPS5:
11953 case ISA_MIPS64:
5f74bc13 11954 case ISA_MIPS64R2:
98d3f06f
KH
11955 mips_opts.gp32 = 0;
11956 mips_opts.fp32 = 0;
11957 break;
11958 default:
11959 as_bad (_("unknown ISA level %s"), name + 4);
11960 break;
11961 }
af7ee8bf 11962 if (reset)
98d3f06f 11963 {
af7ee8bf
CD
11964 mips_opts.gp32 = file_mips_gp32;
11965 mips_opts.fp32 = file_mips_fp32;
98d3f06f 11966 }
252b5132
RH
11967 }
11968 else if (strcmp (name, "autoextend") == 0)
11969 mips_opts.noautoextend = 0;
11970 else if (strcmp (name, "noautoextend") == 0)
11971 mips_opts.noautoextend = 1;
11972 else if (strcmp (name, "push") == 0)
11973 {
11974 struct mips_option_stack *s;
11975
11976 s = (struct mips_option_stack *) xmalloc (sizeof *s);
11977 s->next = mips_opts_stack;
11978 s->options = mips_opts;
11979 mips_opts_stack = s;
11980 }
11981 else if (strcmp (name, "pop") == 0)
11982 {
11983 struct mips_option_stack *s;
11984
11985 s = mips_opts_stack;
11986 if (s == NULL)
11987 as_bad (_(".set pop with no .set push"));
11988 else
11989 {
11990 /* If we're changing the reorder mode we need to handle
11991 delay slots correctly. */
11992 if (s->options.noreorder && ! mips_opts.noreorder)
b34976b6 11993 mips_emit_delays (TRUE);
252b5132
RH
11994 else if (! s->options.noreorder && mips_opts.noreorder)
11995 {
11996 if (prev_nop_frag != NULL)
11997 {
11998 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11999 * (mips_opts.mips16 ? 2 : 4));
12000 prev_nop_frag = NULL;
12001 }
12002 }
12003
12004 mips_opts = s->options;
12005 mips_opts_stack = s->next;
12006 free (s);
12007 }
12008 }
12009 else
12010 {
12011 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
12012 }
12013 *input_line_pointer = ch;
12014 demand_empty_rest_of_line ();
12015}
12016
12017/* Handle the .abicalls pseudo-op. I believe this is equivalent to
12018 .option pic2. It means to generate SVR4 PIC calls. */
12019
12020static void
17a2f251 12021s_abicalls (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
12022{
12023 mips_pic = SVR4_PIC;
143d77c5 12024 mips_abicalls = TRUE;
252b5132
RH
12025 if (USE_GLOBAL_POINTER_OPT)
12026 {
12027 if (g_switch_seen && g_switch_value != 0)
12028 as_warn (_("-G may not be used with SVR4 PIC code"));
12029 g_switch_value = 0;
12030 }
12031 bfd_set_gp_size (stdoutput, 0);
12032 demand_empty_rest_of_line ();
12033}
12034
12035/* Handle the .cpload pseudo-op. This is used when generating SVR4
12036 PIC code. It sets the $gp register for the function based on the
12037 function address, which is in the register named in the argument.
12038 This uses a relocation against _gp_disp, which is handled specially
12039 by the linker. The result is:
12040 lui $gp,%hi(_gp_disp)
12041 addiu $gp,$gp,%lo(_gp_disp)
12042 addu $gp,$gp,.cpload argument
12043 The .cpload argument is normally $25 == $t9. */
12044
12045static void
17a2f251 12046s_cpload (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
12047{
12048 expressionS ex;
252b5132 12049
6478892d
TS
12050 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12051 .cpload is ignored. */
12052 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
12053 {
12054 s_ignore (0);
12055 return;
12056 }
12057
d3ecfc59 12058 /* .cpload should be in a .set noreorder section. */
252b5132
RH
12059 if (mips_opts.noreorder == 0)
12060 as_warn (_(".cpload not in noreorder section"));
12061
12062 ex.X_op = O_symbol;
12063 ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
12064 ex.X_op_symbol = NULL;
12065 ex.X_add_number = 0;
12066
12067 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
49309057 12068 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
252b5132 12069
584892a6 12070 macro_start ();
67c0d1eb
RS
12071 macro_build_lui (&ex, mips_gp_register);
12072 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
17a2f251 12073 mips_gp_register, BFD_RELOC_LO16);
67c0d1eb 12074 macro_build (NULL, "addu", "d,v,t", mips_gp_register,
17a2f251 12075 mips_gp_register, tc_get_register (0));
584892a6 12076 macro_end ();
252b5132
RH
12077
12078 demand_empty_rest_of_line ();
12079}
12080
6478892d
TS
12081/* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
12082 .cpsetup $reg1, offset|$reg2, label
12083
12084 If offset is given, this results in:
12085 sd $gp, offset($sp)
956cd1d6 12086 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
12087 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
12088 daddu $gp, $gp, $reg1
6478892d
TS
12089
12090 If $reg2 is given, this results in:
12091 daddu $reg2, $gp, $0
956cd1d6 12092 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
12093 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
12094 daddu $gp, $gp, $reg1
12095 $reg1 is normally $25 == $t9. */
6478892d 12096static void
17a2f251 12097s_cpsetup (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
12098{
12099 expressionS ex_off;
12100 expressionS ex_sym;
12101 int reg1;
f21f8242 12102 char *f;
6478892d 12103
8586fc66 12104 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
6478892d
TS
12105 We also need NewABI support. */
12106 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12107 {
12108 s_ignore (0);
12109 return;
12110 }
12111
12112 reg1 = tc_get_register (0);
12113 SKIP_WHITESPACE ();
12114 if (*input_line_pointer != ',')
12115 {
12116 as_bad (_("missing argument separator ',' for .cpsetup"));
12117 return;
12118 }
12119 else
80245285 12120 ++input_line_pointer;
6478892d
TS
12121 SKIP_WHITESPACE ();
12122 if (*input_line_pointer == '$')
80245285
TS
12123 {
12124 mips_cpreturn_register = tc_get_register (0);
12125 mips_cpreturn_offset = -1;
12126 }
6478892d 12127 else
80245285
TS
12128 {
12129 mips_cpreturn_offset = get_absolute_expression ();
12130 mips_cpreturn_register = -1;
12131 }
6478892d
TS
12132 SKIP_WHITESPACE ();
12133 if (*input_line_pointer != ',')
12134 {
12135 as_bad (_("missing argument separator ',' for .cpsetup"));
12136 return;
12137 }
12138 else
f9419b05 12139 ++input_line_pointer;
6478892d 12140 SKIP_WHITESPACE ();
f21f8242 12141 expression (&ex_sym);
6478892d 12142
584892a6 12143 macro_start ();
6478892d
TS
12144 if (mips_cpreturn_register == -1)
12145 {
12146 ex_off.X_op = O_constant;
12147 ex_off.X_add_symbol = NULL;
12148 ex_off.X_op_symbol = NULL;
12149 ex_off.X_add_number = mips_cpreturn_offset;
12150
67c0d1eb 12151 macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
17a2f251 12152 BFD_RELOC_LO16, SP);
6478892d
TS
12153 }
12154 else
67c0d1eb 12155 macro_build (NULL, "daddu", "d,v,t", mips_cpreturn_register,
17a2f251 12156 mips_gp_register, 0);
6478892d 12157
f21f8242
AO
12158 /* Ensure there's room for the next two instructions, so that `f'
12159 doesn't end up with an address in the wrong frag. */
12160 frag_grow (8);
12161 f = frag_more (0);
67c0d1eb 12162 macro_build (&ex_sym, "lui", "t,u", mips_gp_register, BFD_RELOC_GPREL16);
f21f8242 12163 fix_new (frag_now, f - frag_now->fr_literal,
a105a300 12164 8, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
f21f8242 12165 fix_new (frag_now, f - frag_now->fr_literal,
a105a300 12166 4, NULL, 0, 0, BFD_RELOC_HI16_S);
f21f8242
AO
12167
12168 f = frag_more (0);
67c0d1eb 12169 macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
17a2f251 12170 mips_gp_register, BFD_RELOC_GPREL16);
f21f8242 12171 fix_new (frag_now, f - frag_now->fr_literal,
a105a300 12172 8, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
f21f8242 12173 fix_new (frag_now, f - frag_now->fr_literal,
a105a300 12174 4, NULL, 0, 0, BFD_RELOC_LO16);
f21f8242 12175
67c0d1eb 12176 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
17a2f251 12177 mips_gp_register, reg1);
584892a6 12178 macro_end ();
6478892d
TS
12179
12180 demand_empty_rest_of_line ();
12181}
12182
12183static void
17a2f251 12184s_cplocal (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
12185{
12186 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
12187 .cplocal is ignored. */
12188 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12189 {
12190 s_ignore (0);
12191 return;
12192 }
12193
12194 mips_gp_register = tc_get_register (0);
85b51719 12195 demand_empty_rest_of_line ();
6478892d
TS
12196}
12197
252b5132
RH
12198/* Handle the .cprestore pseudo-op. This stores $gp into a given
12199 offset from $sp. The offset is remembered, and after making a PIC
12200 call $gp is restored from that location. */
12201
12202static void
17a2f251 12203s_cprestore (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
12204{
12205 expressionS ex;
252b5132 12206
6478892d 12207 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
c9914766 12208 .cprestore is ignored. */
6478892d 12209 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
12210 {
12211 s_ignore (0);
12212 return;
12213 }
12214
12215 mips_cprestore_offset = get_absolute_expression ();
7a621144 12216 mips_cprestore_valid = 1;
252b5132
RH
12217
12218 ex.X_op = O_constant;
12219 ex.X_add_symbol = NULL;
12220 ex.X_op_symbol = NULL;
12221 ex.X_add_number = mips_cprestore_offset;
12222
584892a6 12223 macro_start ();
67c0d1eb
RS
12224 macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
12225 SP, HAVE_64BIT_ADDRESSES);
584892a6 12226 macro_end ();
252b5132
RH
12227
12228 demand_empty_rest_of_line ();
12229}
12230
6478892d 12231/* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
67c1ffbe 12232 was given in the preceding .cpsetup, it results in:
6478892d 12233 ld $gp, offset($sp)
76b3015f 12234
6478892d 12235 If a register $reg2 was given there, it results in:
609f23f4 12236 daddu $gp, $reg2, $0
6478892d
TS
12237 */
12238static void
17a2f251 12239s_cpreturn (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
12240{
12241 expressionS ex;
6478892d
TS
12242
12243 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
12244 We also need NewABI support. */
12245 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12246 {
12247 s_ignore (0);
12248 return;
12249 }
12250
584892a6 12251 macro_start ();
6478892d
TS
12252 if (mips_cpreturn_register == -1)
12253 {
12254 ex.X_op = O_constant;
12255 ex.X_add_symbol = NULL;
12256 ex.X_op_symbol = NULL;
12257 ex.X_add_number = mips_cpreturn_offset;
12258
67c0d1eb 12259 macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
6478892d
TS
12260 }
12261 else
67c0d1eb 12262 macro_build (NULL, "daddu", "d,v,t", mips_gp_register,
17a2f251 12263 mips_cpreturn_register, 0);
584892a6 12264 macro_end ();
6478892d
TS
12265
12266 demand_empty_rest_of_line ();
12267}
12268
12269/* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
12270 code. It sets the offset to use in gp_rel relocations. */
12271
12272static void
17a2f251 12273s_gpvalue (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
12274{
12275 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
12276 We also need NewABI support. */
12277 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12278 {
12279 s_ignore (0);
12280 return;
12281 }
12282
def2e0dd 12283 mips_gprel_offset = get_absolute_expression ();
6478892d
TS
12284
12285 demand_empty_rest_of_line ();
12286}
12287
252b5132
RH
12288/* Handle the .gpword pseudo-op. This is used when generating PIC
12289 code. It generates a 32 bit GP relative reloc. */
12290
12291static void
17a2f251 12292s_gpword (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
12293{
12294 symbolS *label;
12295 expressionS ex;
12296 char *p;
12297
12298 /* When not generating PIC code, this is treated as .word. */
12299 if (mips_pic != SVR4_PIC)
12300 {
12301 s_cons (2);
12302 return;
12303 }
12304
12305 label = insn_labels != NULL ? insn_labels->label : NULL;
b34976b6 12306 mips_emit_delays (TRUE);
252b5132
RH
12307 if (auto_align)
12308 mips_align (2, 0, label);
12309 mips_clear_insn_labels ();
12310
12311 expression (&ex);
12312
12313 if (ex.X_op != O_symbol || ex.X_add_number != 0)
12314 {
12315 as_bad (_("Unsupported use of .gpword"));
12316 ignore_rest_of_line ();
12317 }
12318
12319 p = frag_more (4);
17a2f251 12320 md_number_to_chars (p, 0, 4);
b34976b6 12321 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
cdf6fd85 12322 BFD_RELOC_GPREL32);
252b5132
RH
12323
12324 demand_empty_rest_of_line ();
12325}
12326
10181a0d 12327static void
17a2f251 12328s_gpdword (int ignore ATTRIBUTE_UNUSED)
10181a0d
AO
12329{
12330 symbolS *label;
12331 expressionS ex;
12332 char *p;
12333
12334 /* When not generating PIC code, this is treated as .dword. */
12335 if (mips_pic != SVR4_PIC)
12336 {
12337 s_cons (3);
12338 return;
12339 }
12340
12341 label = insn_labels != NULL ? insn_labels->label : NULL;
b34976b6 12342 mips_emit_delays (TRUE);
10181a0d
AO
12343 if (auto_align)
12344 mips_align (3, 0, label);
12345 mips_clear_insn_labels ();
12346
12347 expression (&ex);
12348
12349 if (ex.X_op != O_symbol || ex.X_add_number != 0)
12350 {
12351 as_bad (_("Unsupported use of .gpdword"));
12352 ignore_rest_of_line ();
12353 }
12354
12355 p = frag_more (8);
17a2f251 12356 md_number_to_chars (p, 0, 8);
a105a300 12357 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
10181a0d
AO
12358 BFD_RELOC_GPREL32);
12359
12360 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
12361 ex.X_op = O_absent;
12362 ex.X_add_symbol = 0;
12363 ex.X_add_number = 0;
b34976b6 12364 fix_new_exp (frag_now, p - frag_now->fr_literal, 8, &ex, FALSE,
10181a0d
AO
12365 BFD_RELOC_64);
12366
12367 demand_empty_rest_of_line ();
12368}
12369
252b5132
RH
12370/* Handle the .cpadd pseudo-op. This is used when dealing with switch
12371 tables in SVR4 PIC code. */
12372
12373static void
17a2f251 12374s_cpadd (int ignore ATTRIBUTE_UNUSED)
252b5132 12375{
252b5132
RH
12376 int reg;
12377
10181a0d
AO
12378 /* This is ignored when not generating SVR4 PIC code. */
12379 if (mips_pic != SVR4_PIC)
252b5132
RH
12380 {
12381 s_ignore (0);
12382 return;
12383 }
12384
12385 /* Add $gp to the register named as an argument. */
584892a6 12386 macro_start ();
252b5132 12387 reg = tc_get_register (0);
67c0d1eb 12388 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
584892a6 12389 macro_end ();
252b5132 12390
bdaaa2e1 12391 demand_empty_rest_of_line ();
252b5132
RH
12392}
12393
12394/* Handle the .insn pseudo-op. This marks instruction labels in
12395 mips16 mode. This permits the linker to handle them specially,
12396 such as generating jalx instructions when needed. We also make
12397 them odd for the duration of the assembly, in order to generate the
12398 right sort of code. We will make them even in the adjust_symtab
12399 routine, while leaving them marked. This is convenient for the
12400 debugger and the disassembler. The linker knows to make them odd
12401 again. */
12402
12403static void
17a2f251 12404s_insn (int ignore ATTRIBUTE_UNUSED)
252b5132 12405{
f9419b05 12406 mips16_mark_labels ();
252b5132
RH
12407
12408 demand_empty_rest_of_line ();
12409}
12410
12411/* Handle a .stabn directive. We need these in order to mark a label
12412 as being a mips16 text label correctly. Sometimes the compiler
12413 will emit a label, followed by a .stabn, and then switch sections.
12414 If the label and .stabn are in mips16 mode, then the label is
12415 really a mips16 text label. */
12416
12417static void
17a2f251 12418s_mips_stab (int type)
252b5132 12419{
f9419b05 12420 if (type == 'n')
252b5132
RH
12421 mips16_mark_labels ();
12422
12423 s_stab (type);
12424}
12425
12426/* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
12427 */
12428
12429static void
17a2f251 12430s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
12431{
12432 char *name;
12433 int c;
12434 symbolS *symbolP;
12435 expressionS exp;
12436
12437 name = input_line_pointer;
12438 c = get_symbol_end ();
12439 symbolP = symbol_find_or_make (name);
12440 S_SET_WEAK (symbolP);
12441 *input_line_pointer = c;
12442
12443 SKIP_WHITESPACE ();
12444
12445 if (! is_end_of_line[(unsigned char) *input_line_pointer])
12446 {
12447 if (S_IS_DEFINED (symbolP))
12448 {
956cd1d6 12449 as_bad ("ignoring attempt to redefine symbol %s",
252b5132
RH
12450 S_GET_NAME (symbolP));
12451 ignore_rest_of_line ();
12452 return;
12453 }
bdaaa2e1 12454
252b5132
RH
12455 if (*input_line_pointer == ',')
12456 {
12457 ++input_line_pointer;
12458 SKIP_WHITESPACE ();
12459 }
bdaaa2e1 12460
252b5132
RH
12461 expression (&exp);
12462 if (exp.X_op != O_symbol)
12463 {
12464 as_bad ("bad .weakext directive");
98d3f06f 12465 ignore_rest_of_line ();
252b5132
RH
12466 return;
12467 }
49309057 12468 symbol_set_value_expression (symbolP, &exp);
252b5132
RH
12469 }
12470
12471 demand_empty_rest_of_line ();
12472}
12473
12474/* Parse a register string into a number. Called from the ECOFF code
12475 to parse .frame. The argument is non-zero if this is the frame
12476 register, so that we can record it in mips_frame_reg. */
12477
12478int
17a2f251 12479tc_get_register (int frame)
252b5132
RH
12480{
12481 int reg;
12482
12483 SKIP_WHITESPACE ();
12484 if (*input_line_pointer++ != '$')
12485 {
12486 as_warn (_("expected `$'"));
85b51719 12487 reg = ZERO;
252b5132 12488 }
3882b010 12489 else if (ISDIGIT (*input_line_pointer))
252b5132
RH
12490 {
12491 reg = get_absolute_expression ();
12492 if (reg < 0 || reg >= 32)
12493 {
12494 as_warn (_("Bad register number"));
85b51719 12495 reg = ZERO;
252b5132
RH
12496 }
12497 }
12498 else
12499 {
76db943d 12500 if (strncmp (input_line_pointer, "ra", 2) == 0)
85b51719
TS
12501 {
12502 reg = RA;
12503 input_line_pointer += 2;
12504 }
76db943d 12505 else if (strncmp (input_line_pointer, "fp", 2) == 0)
85b51719
TS
12506 {
12507 reg = FP;
12508 input_line_pointer += 2;
12509 }
252b5132 12510 else if (strncmp (input_line_pointer, "sp", 2) == 0)
85b51719
TS
12511 {
12512 reg = SP;
12513 input_line_pointer += 2;
12514 }
252b5132 12515 else if (strncmp (input_line_pointer, "gp", 2) == 0)
85b51719
TS
12516 {
12517 reg = GP;
12518 input_line_pointer += 2;
12519 }
252b5132 12520 else if (strncmp (input_line_pointer, "at", 2) == 0)
85b51719
TS
12521 {
12522 reg = AT;
12523 input_line_pointer += 2;
12524 }
12525 else if (strncmp (input_line_pointer, "kt0", 3) == 0)
12526 {
12527 reg = KT0;
12528 input_line_pointer += 3;
12529 }
12530 else if (strncmp (input_line_pointer, "kt1", 3) == 0)
12531 {
12532 reg = KT1;
12533 input_line_pointer += 3;
12534 }
12535 else if (strncmp (input_line_pointer, "zero", 4) == 0)
12536 {
12537 reg = ZERO;
12538 input_line_pointer += 4;
12539 }
252b5132
RH
12540 else
12541 {
12542 as_warn (_("Unrecognized register name"));
85b51719
TS
12543 reg = ZERO;
12544 while (ISALNUM(*input_line_pointer))
12545 input_line_pointer++;
252b5132 12546 }
252b5132
RH
12547 }
12548 if (frame)
7a621144
DJ
12549 {
12550 mips_frame_reg = reg != 0 ? reg : SP;
12551 mips_frame_reg_valid = 1;
12552 mips_cprestore_valid = 0;
12553 }
252b5132
RH
12554 return reg;
12555}
12556
12557valueT
17a2f251 12558md_section_align (asection *seg, valueT addr)
252b5132
RH
12559{
12560 int align = bfd_get_section_alignment (stdoutput, seg);
12561
12562#ifdef OBJ_ELF
12563 /* We don't need to align ELF sections to the full alignment.
12564 However, Irix 5 may prefer that we align them at least to a 16
12565 byte boundary. We don't bother to align the sections if we are
12566 targeted for an embedded system. */
12567 if (strcmp (TARGET_OS, "elf") == 0)
12568 return addr;
12569 if (align > 4)
12570 align = 4;
12571#endif
12572
12573 return ((addr + (1 << align) - 1) & (-1 << align));
12574}
12575
12576/* Utility routine, called from above as well. If called while the
12577 input file is still being read, it's only an approximation. (For
12578 example, a symbol may later become defined which appeared to be
12579 undefined earlier.) */
12580
12581static int
17a2f251 12582nopic_need_relax (symbolS *sym, int before_relaxing)
252b5132
RH
12583{
12584 if (sym == 0)
12585 return 0;
12586
6478892d 12587 if (USE_GLOBAL_POINTER_OPT && g_switch_value > 0)
252b5132
RH
12588 {
12589 const char *symname;
12590 int change;
12591
c9914766 12592 /* Find out whether this symbol can be referenced off the $gp
252b5132
RH
12593 register. It can be if it is smaller than the -G size or if
12594 it is in the .sdata or .sbss section. Certain symbols can
c9914766 12595 not be referenced off the $gp, although it appears as though
252b5132
RH
12596 they can. */
12597 symname = S_GET_NAME (sym);
12598 if (symname != (const char *) NULL
12599 && (strcmp (symname, "eprol") == 0
12600 || strcmp (symname, "etext") == 0
12601 || strcmp (symname, "_gp") == 0
12602 || strcmp (symname, "edata") == 0
12603 || strcmp (symname, "_fbss") == 0
12604 || strcmp (symname, "_fdata") == 0
12605 || strcmp (symname, "_ftext") == 0
12606 || strcmp (symname, "end") == 0
12607 || strcmp (symname, "_gp_disp") == 0))
12608 change = 1;
12609 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
12610 && (0
12611#ifndef NO_ECOFF_DEBUGGING
49309057
ILT
12612 || (symbol_get_obj (sym)->ecoff_extern_size != 0
12613 && (symbol_get_obj (sym)->ecoff_extern_size
12614 <= g_switch_value))
252b5132
RH
12615#endif
12616 /* We must defer this decision until after the whole
12617 file has been read, since there might be a .extern
12618 after the first use of this symbol. */
12619 || (before_relaxing
12620#ifndef NO_ECOFF_DEBUGGING
49309057 12621 && symbol_get_obj (sym)->ecoff_extern_size == 0
252b5132
RH
12622#endif
12623 && S_GET_VALUE (sym) == 0)
12624 || (S_GET_VALUE (sym) != 0
12625 && S_GET_VALUE (sym) <= g_switch_value)))
12626 change = 0;
12627 else
12628 {
12629 const char *segname;
12630
12631 segname = segment_name (S_GET_SEGMENT (sym));
12632 assert (strcmp (segname, ".lit8") != 0
12633 && strcmp (segname, ".lit4") != 0);
12634 change = (strcmp (segname, ".sdata") != 0
fba2b7f9
GK
12635 && strcmp (segname, ".sbss") != 0
12636 && strncmp (segname, ".sdata.", 7) != 0
12637 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
252b5132
RH
12638 }
12639 return change;
12640 }
12641 else
c9914766 12642 /* We are not optimizing for the $gp register. */
252b5132
RH
12643 return 1;
12644}
12645
5919d012
RS
12646
12647/* Return true if the given symbol should be considered local for SVR4 PIC. */
12648
12649static bfd_boolean
17a2f251 12650pic_need_relax (symbolS *sym, asection *segtype)
5919d012
RS
12651{
12652 asection *symsec;
12653 bfd_boolean linkonce;
12654
12655 /* Handle the case of a symbol equated to another symbol. */
12656 while (symbol_equated_reloc_p (sym))
12657 {
12658 symbolS *n;
12659
12660 /* It's possible to get a loop here in a badly written
12661 program. */
12662 n = symbol_get_value_expression (sym)->X_add_symbol;
12663 if (n == sym)
12664 break;
12665 sym = n;
12666 }
12667
12668 symsec = S_GET_SEGMENT (sym);
12669
12670 /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
12671 linkonce = FALSE;
12672 if (symsec != segtype && ! S_IS_LOCAL (sym))
12673 {
12674 if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
12675 != 0)
12676 linkonce = TRUE;
12677
12678 /* The GNU toolchain uses an extension for ELF: a section
12679 beginning with the magic string .gnu.linkonce is a linkonce
12680 section. */
12681 if (strncmp (segment_name (symsec), ".gnu.linkonce",
12682 sizeof ".gnu.linkonce" - 1) == 0)
12683 linkonce = TRUE;
12684 }
12685
12686 /* This must duplicate the test in adjust_reloc_syms. */
12687 return (symsec != &bfd_und_section
12688 && symsec != &bfd_abs_section
12689 && ! bfd_is_com_section (symsec)
12690 && !linkonce
12691#ifdef OBJ_ELF
12692 /* A global or weak symbol is treated as external. */
12693 && (OUTPUT_FLAVOR != bfd_target_elf_flavour
12694 || (! S_IS_WEAK (sym)
12695 && (! S_IS_EXTERNAL (sym)
12696 || mips_pic == EMBEDDED_PIC)))
12697#endif
12698 );
12699}
12700
12701
252b5132
RH
12702/* Given a mips16 variant frag FRAGP, return non-zero if it needs an
12703 extended opcode. SEC is the section the frag is in. */
12704
12705static int
17a2f251 12706mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
252b5132
RH
12707{
12708 int type;
12709 register const struct mips16_immed_operand *op;
12710 offsetT val;
12711 int mintiny, maxtiny;
12712 segT symsec;
98aa84af 12713 fragS *sym_frag;
252b5132
RH
12714
12715 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
12716 return 0;
12717 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
12718 return 1;
12719
12720 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12721 op = mips16_immed_operands;
12722 while (op->type != type)
12723 {
12724 ++op;
12725 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
12726 }
12727
12728 if (op->unsp)
12729 {
12730 if (type == '<' || type == '>' || type == '[' || type == ']')
12731 {
12732 mintiny = 1;
12733 maxtiny = 1 << op->nbits;
12734 }
12735 else
12736 {
12737 mintiny = 0;
12738 maxtiny = (1 << op->nbits) - 1;
12739 }
12740 }
12741 else
12742 {
12743 mintiny = - (1 << (op->nbits - 1));
12744 maxtiny = (1 << (op->nbits - 1)) - 1;
12745 }
12746
98aa84af 12747 sym_frag = symbol_get_frag (fragp->fr_symbol);
ac62c346 12748 val = S_GET_VALUE (fragp->fr_symbol);
98aa84af 12749 symsec = S_GET_SEGMENT (fragp->fr_symbol);
252b5132
RH
12750
12751 if (op->pcrel)
12752 {
12753 addressT addr;
12754
12755 /* We won't have the section when we are called from
12756 mips_relax_frag. However, we will always have been called
12757 from md_estimate_size_before_relax first. If this is a
12758 branch to a different section, we mark it as such. If SEC is
12759 NULL, and the frag is not marked, then it must be a branch to
12760 the same section. */
12761 if (sec == NULL)
12762 {
12763 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
12764 return 1;
12765 }
12766 else
12767 {
98aa84af 12768 /* Must have been called from md_estimate_size_before_relax. */
252b5132
RH
12769 if (symsec != sec)
12770 {
12771 fragp->fr_subtype =
12772 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12773
12774 /* FIXME: We should support this, and let the linker
12775 catch branches and loads that are out of range. */
12776 as_bad_where (fragp->fr_file, fragp->fr_line,
12777 _("unsupported PC relative reference to different section"));
12778
12779 return 1;
12780 }
98aa84af
AM
12781 if (fragp != sym_frag && sym_frag->fr_address == 0)
12782 /* Assume non-extended on the first relaxation pass.
12783 The address we have calculated will be bogus if this is
12784 a forward branch to another frag, as the forward frag
12785 will have fr_address == 0. */
12786 return 0;
252b5132
RH
12787 }
12788
12789 /* In this case, we know for sure that the symbol fragment is in
98aa84af
AM
12790 the same section. If the relax_marker of the symbol fragment
12791 differs from the relax_marker of this fragment, we have not
12792 yet adjusted the symbol fragment fr_address. We want to add
252b5132
RH
12793 in STRETCH in order to get a better estimate of the address.
12794 This particularly matters because of the shift bits. */
12795 if (stretch != 0
98aa84af 12796 && sym_frag->relax_marker != fragp->relax_marker)
252b5132
RH
12797 {
12798 fragS *f;
12799
12800 /* Adjust stretch for any alignment frag. Note that if have
12801 been expanding the earlier code, the symbol may be
12802 defined in what appears to be an earlier frag. FIXME:
12803 This doesn't handle the fr_subtype field, which specifies
12804 a maximum number of bytes to skip when doing an
12805 alignment. */
98aa84af 12806 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
252b5132
RH
12807 {
12808 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
12809 {
12810 if (stretch < 0)
12811 stretch = - ((- stretch)
12812 & ~ ((1 << (int) f->fr_offset) - 1));
12813 else
12814 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
12815 if (stretch == 0)
12816 break;
12817 }
12818 }
12819 if (f != NULL)
12820 val += stretch;
12821 }
12822
12823 addr = fragp->fr_address + fragp->fr_fix;
12824
12825 /* The base address rules are complicated. The base address of
12826 a branch is the following instruction. The base address of a
12827 PC relative load or add is the instruction itself, but if it
12828 is in a delay slot (in which case it can not be extended) use
12829 the address of the instruction whose delay slot it is in. */
12830 if (type == 'p' || type == 'q')
12831 {
12832 addr += 2;
12833
12834 /* If we are currently assuming that this frag should be
12835 extended, then, the current address is two bytes
bdaaa2e1 12836 higher. */
252b5132
RH
12837 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12838 addr += 2;
12839
12840 /* Ignore the low bit in the target, since it will be set
12841 for a text label. */
12842 if ((val & 1) != 0)
12843 --val;
12844 }
12845 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12846 addr -= 4;
12847 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12848 addr -= 2;
12849
12850 val -= addr & ~ ((1 << op->shift) - 1);
12851
12852 /* Branch offsets have an implicit 0 in the lowest bit. */
12853 if (type == 'p' || type == 'q')
12854 val /= 2;
12855
12856 /* If any of the shifted bits are set, we must use an extended
12857 opcode. If the address depends on the size of this
12858 instruction, this can lead to a loop, so we arrange to always
12859 use an extended opcode. We only check this when we are in
12860 the main relaxation loop, when SEC is NULL. */
12861 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
12862 {
12863 fragp->fr_subtype =
12864 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12865 return 1;
12866 }
12867
12868 /* If we are about to mark a frag as extended because the value
12869 is precisely maxtiny + 1, then there is a chance of an
12870 infinite loop as in the following code:
12871 la $4,foo
12872 .skip 1020
12873 .align 2
12874 foo:
12875 In this case when the la is extended, foo is 0x3fc bytes
12876 away, so the la can be shrunk, but then foo is 0x400 away, so
12877 the la must be extended. To avoid this loop, we mark the
12878 frag as extended if it was small, and is about to become
12879 extended with a value of maxtiny + 1. */
12880 if (val == ((maxtiny + 1) << op->shift)
12881 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
12882 && sec == NULL)
12883 {
12884 fragp->fr_subtype =
12885 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12886 return 1;
12887 }
12888 }
12889 else if (symsec != absolute_section && sec != NULL)
12890 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
12891
12892 if ((val & ((1 << op->shift) - 1)) != 0
12893 || val < (mintiny << op->shift)
12894 || val > (maxtiny << op->shift))
12895 return 1;
12896 else
12897 return 0;
12898}
12899
4a6a3df4
AO
12900/* Compute the length of a branch sequence, and adjust the
12901 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
12902 worst-case length is computed, with UPDATE being used to indicate
12903 whether an unconditional (-1), branch-likely (+1) or regular (0)
12904 branch is to be computed. */
12905static int
17a2f251 12906relaxed_branch_length (fragS *fragp, asection *sec, int update)
4a6a3df4 12907{
b34976b6 12908 bfd_boolean toofar;
4a6a3df4
AO
12909 int length;
12910
12911 if (fragp
12912 && S_IS_DEFINED (fragp->fr_symbol)
12913 && sec == S_GET_SEGMENT (fragp->fr_symbol))
12914 {
12915 addressT addr;
12916 offsetT val;
12917
12918 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
12919
12920 addr = fragp->fr_address + fragp->fr_fix + 4;
12921
12922 val -= addr;
12923
12924 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
12925 }
12926 else if (fragp)
12927 /* If the symbol is not defined or it's in a different segment,
12928 assume the user knows what's going on and emit a short
12929 branch. */
b34976b6 12930 toofar = FALSE;
4a6a3df4 12931 else
b34976b6 12932 toofar = TRUE;
4a6a3df4
AO
12933
12934 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
12935 fragp->fr_subtype
af6ae2ad 12936 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_UNCOND (fragp->fr_subtype),
4a6a3df4
AO
12937 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
12938 RELAX_BRANCH_LINK (fragp->fr_subtype),
12939 toofar);
12940
12941 length = 4;
12942 if (toofar)
12943 {
12944 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
12945 length += 8;
12946
12947 if (mips_pic != NO_PIC)
12948 {
12949 /* Additional space for PIC loading of target address. */
12950 length += 8;
12951 if (mips_opts.isa == ISA_MIPS1)
12952 /* Additional space for $at-stabilizing nop. */
12953 length += 4;
12954 }
12955
12956 /* If branch is conditional. */
12957 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
12958 length += 8;
12959 }
b34976b6 12960
4a6a3df4
AO
12961 return length;
12962}
12963
252b5132
RH
12964/* Estimate the size of a frag before relaxing. Unless this is the
12965 mips16, we are not really relaxing here, and the final size is
12966 encoded in the subtype information. For the mips16, we have to
12967 decide whether we are using an extended opcode or not. */
12968
252b5132 12969int
17a2f251 12970md_estimate_size_before_relax (fragS *fragp, asection *segtype)
252b5132 12971{
5919d012 12972 int change;
252b5132 12973
4a6a3df4
AO
12974 if (RELAX_BRANCH_P (fragp->fr_subtype))
12975 {
12976
b34976b6
AM
12977 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
12978
4a6a3df4
AO
12979 return fragp->fr_var;
12980 }
12981
252b5132 12982 if (RELAX_MIPS16_P (fragp->fr_subtype))
177b4a6a
AO
12983 /* We don't want to modify the EXTENDED bit here; it might get us
12984 into infinite loops. We change it only in mips_relax_frag(). */
12985 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
252b5132
RH
12986
12987 if (mips_pic == NO_PIC)
5919d012 12988 change = nopic_need_relax (fragp->fr_symbol, 0);
252b5132 12989 else if (mips_pic == SVR4_PIC)
5919d012 12990 change = pic_need_relax (fragp->fr_symbol, segtype);
252b5132
RH
12991 else
12992 abort ();
12993
12994 if (change)
12995 {
4d7206a2 12996 fragp->fr_subtype |= RELAX_USE_SECOND;
4d7206a2 12997 return -RELAX_FIRST (fragp->fr_subtype);
252b5132 12998 }
4d7206a2
RS
12999 else
13000 return -RELAX_SECOND (fragp->fr_subtype);
252b5132
RH
13001}
13002
13003/* This is called to see whether a reloc against a defined symbol
13004 should be converted into a reloc against a section. Don't adjust
13005 MIPS16 jump relocations, so we don't have to worry about the format
13006 of the offset in the .o file. Don't adjust relocations against
13007 mips16 symbols, so that the linker can find them if it needs to set
13008 up a stub. */
13009
13010int
17a2f251 13011mips_fix_adjustable (fixS *fixp)
252b5132
RH
13012{
13013 if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
13014 return 0;
a161fe53 13015
252b5132
RH
13016 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
13017 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13018 return 0;
a161fe53 13019
252b5132
RH
13020 if (fixp->fx_addsy == NULL)
13021 return 1;
a161fe53 13022
252b5132
RH
13023#ifdef OBJ_ELF
13024 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
13025 && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
13026 && fixp->fx_subsy == NULL)
13027 return 0;
13028#endif
a161fe53 13029
252b5132
RH
13030 return 1;
13031}
13032
13033/* Translate internal representation of relocation info to BFD target
13034 format. */
13035
13036arelent **
17a2f251 13037tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
13038{
13039 static arelent *retval[4];
13040 arelent *reloc;
13041 bfd_reloc_code_real_type code;
13042
4b0cff4e
TS
13043 memset (retval, 0, sizeof(retval));
13044 reloc = retval[0] = (arelent *) xcalloc (1, sizeof (arelent));
49309057
ILT
13045 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13046 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
13047 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
13048
13049 if (mips_pic == EMBEDDED_PIC
13050 && SWITCH_TABLE (fixp))
13051 {
13052 /* For a switch table entry we use a special reloc. The addend
13053 is actually the difference between the reloc address and the
13054 subtrahend. */
13055 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
13056 if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
13057 as_fatal (_("Double check fx_r_type in tc-mips.c:tc_gen_reloc"));
13058 fixp->fx_r_type = BFD_RELOC_GPREL32;
13059 }
30ac9238 13060 else if (fixp->fx_pcrel)
252b5132 13061 {
30ac9238
RS
13062 bfd_vma pcrel_address;
13063
13064 /* Set PCREL_ADDRESS to this relocation's "PC". The PC for high
13065 high-part relocs is the address of the low-part reloc. */
13066 if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
13067 {
13068 assert (fixp->fx_next != NULL
13069 && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
13070 pcrel_address = (fixp->fx_next->fx_where
13071 + fixp->fx_next->fx_frag->fr_address);
13072 }
252b5132 13073 else
30ac9238
RS
13074 pcrel_address = reloc->address;
13075
13076 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
13077 {
13078 /* At this point, fx_addnumber is "symbol offset - pcrel_address".
13079 Relocations want only the symbol offset. */
13080 reloc->addend = fixp->fx_addnumber + pcrel_address;
13081 }
13082 else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16
13083 || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
4514d474 13084 {
30ac9238 13085 /* We use a special addend for an internal RELLO or RELHI reloc. */
4514d474 13086 if (symbol_section_p (fixp->fx_addsy))
30ac9238 13087 reloc->addend = pcrel_address - S_GET_VALUE (fixp->fx_subsy);
4514d474 13088 else
30ac9238 13089 reloc->addend = fixp->fx_addnumber + pcrel_address;
4514d474 13090 }
252b5132 13091 else
4514d474 13092 {
30ac9238
RS
13093 if (OUTPUT_FLAVOR != bfd_target_aout_flavour)
13094 /* A gruesome hack which is a result of the gruesome gas reloc
13095 handling. */
13096 reloc->addend = pcrel_address;
4514d474 13097 else
30ac9238 13098 reloc->addend = -pcrel_address;
4514d474 13099 }
252b5132 13100 }
252b5132 13101 else
30ac9238 13102 reloc->addend = fixp->fx_addnumber;
252b5132 13103
438c16b8
TS
13104 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
13105 entry to be used in the relocation's section offset. */
13106 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
252b5132
RH
13107 {
13108 reloc->address = reloc->addend;
13109 reloc->addend = 0;
13110 }
13111
13112 /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
13113 fixup_segment converted a non-PC relative reloc into a PC
13114 relative reloc. In such a case, we need to convert the reloc
13115 code. */
13116 code = fixp->fx_r_type;
13117 if (fixp->fx_pcrel)
13118 {
13119 switch (code)
13120 {
13121 case BFD_RELOC_8:
13122 code = BFD_RELOC_8_PCREL;
13123 break;
13124 case BFD_RELOC_16:
13125 code = BFD_RELOC_16_PCREL;
13126 break;
13127 case BFD_RELOC_32:
13128 code = BFD_RELOC_32_PCREL;
13129 break;
13130 case BFD_RELOC_64:
13131 code = BFD_RELOC_64_PCREL;
13132 break;
13133 case BFD_RELOC_8_PCREL:
13134 case BFD_RELOC_16_PCREL:
13135 case BFD_RELOC_32_PCREL:
13136 case BFD_RELOC_64_PCREL:
13137 case BFD_RELOC_16_PCREL_S2:
13138 case BFD_RELOC_PCREL_HI16_S:
13139 case BFD_RELOC_PCREL_LO16:
13140 break;
13141 default:
13142 as_bad_where (fixp->fx_file, fixp->fx_line,
13143 _("Cannot make %s relocation PC relative"),
13144 bfd_get_reloc_code_name (code));
13145 }
13146 }
13147
0b25d3e6
AO
13148 /* To support a PC relative reloc when generating embedded PIC code
13149 for ECOFF, we use a Cygnus extension. We check for that here to
13150 make sure that we don't let such a reloc escape normally. */
13151 if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
13152 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
13153 && code == BFD_RELOC_16_PCREL_S2
13154 && mips_pic != EMBEDDED_PIC)
13155 reloc->howto = NULL;
13156 else
13157 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
13158
252b5132
RH
13159 if (reloc->howto == NULL)
13160 {
13161 as_bad_where (fixp->fx_file, fixp->fx_line,
13162 _("Can not represent %s relocation in this object file format"),
13163 bfd_get_reloc_code_name (code));
13164 retval[0] = NULL;
13165 }
13166
13167 return retval;
13168}
13169
13170/* Relax a machine dependent frag. This returns the amount by which
13171 the current size of the frag should change. */
13172
13173int
17a2f251 13174mips_relax_frag (asection *sec, fragS *fragp, long stretch)
252b5132 13175{
4a6a3df4
AO
13176 if (RELAX_BRANCH_P (fragp->fr_subtype))
13177 {
13178 offsetT old_var = fragp->fr_var;
b34976b6
AM
13179
13180 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
4a6a3df4
AO
13181
13182 return fragp->fr_var - old_var;
13183 }
13184
252b5132
RH
13185 if (! RELAX_MIPS16_P (fragp->fr_subtype))
13186 return 0;
13187
c4e7957c 13188 if (mips16_extended_frag (fragp, NULL, stretch))
252b5132
RH
13189 {
13190 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13191 return 0;
13192 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
13193 return 2;
13194 }
13195 else
13196 {
13197 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13198 return 0;
13199 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
13200 return -2;
13201 }
13202
13203 return 0;
13204}
13205
13206/* Convert a machine dependent frag. */
13207
13208void
17a2f251 13209md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
252b5132 13210{
4a6a3df4
AO
13211 if (RELAX_BRANCH_P (fragp->fr_subtype))
13212 {
13213 bfd_byte *buf;
13214 unsigned long insn;
13215 expressionS exp;
13216 fixS *fixp;
b34976b6 13217
4a6a3df4
AO
13218 buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
13219
13220 if (target_big_endian)
13221 insn = bfd_getb32 (buf);
13222 else
13223 insn = bfd_getl32 (buf);
b34976b6 13224
4a6a3df4
AO
13225 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13226 {
13227 /* We generate a fixup instead of applying it right now
13228 because, if there are linker relaxations, we're going to
13229 need the relocations. */
13230 exp.X_op = O_symbol;
13231 exp.X_add_symbol = fragp->fr_symbol;
13232 exp.X_add_number = fragp->fr_offset;
13233
13234 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
0b25d3e6
AO
13235 4, &exp, 1,
13236 BFD_RELOC_16_PCREL_S2);
4a6a3df4
AO
13237 fixp->fx_file = fragp->fr_file;
13238 fixp->fx_line = fragp->fr_line;
b34976b6 13239
17a2f251 13240 md_number_to_chars (buf, insn, 4);
4a6a3df4
AO
13241 buf += 4;
13242 }
13243 else
13244 {
13245 int i;
13246
13247 as_warn_where (fragp->fr_file, fragp->fr_line,
13248 _("relaxed out-of-range branch into a jump"));
13249
13250 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
13251 goto uncond;
13252
13253 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13254 {
13255 /* Reverse the branch. */
13256 switch ((insn >> 28) & 0xf)
13257 {
13258 case 4:
13259 /* bc[0-3][tf]l? and bc1any[24][ft] instructions can
13260 have the condition reversed by tweaking a single
13261 bit, and their opcodes all have 0x4???????. */
13262 assert ((insn & 0xf1000000) == 0x41000000);
13263 insn ^= 0x00010000;
13264 break;
13265
13266 case 0:
13267 /* bltz 0x04000000 bgez 0x04010000
13268 bltzal 0x04100000 bgezal 0x04110000 */
13269 assert ((insn & 0xfc0e0000) == 0x04000000);
13270 insn ^= 0x00010000;
13271 break;
b34976b6 13272
4a6a3df4
AO
13273 case 1:
13274 /* beq 0x10000000 bne 0x14000000
13275 blez 0x18000000 bgtz 0x1c000000 */
13276 insn ^= 0x04000000;
13277 break;
13278
13279 default:
13280 abort ();
13281 }
13282 }
13283
13284 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13285 {
13286 /* Clear the and-link bit. */
13287 assert ((insn & 0xfc1c0000) == 0x04100000);
13288
13289 /* bltzal 0x04100000 bgezal 0x04110000
13290 bltzall 0x04120000 bgezall 0x04130000 */
13291 insn &= ~0x00100000;
13292 }
13293
13294 /* Branch over the branch (if the branch was likely) or the
13295 full jump (not likely case). Compute the offset from the
13296 current instruction to branch to. */
13297 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13298 i = 16;
13299 else
13300 {
13301 /* How many bytes in instructions we've already emitted? */
13302 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13303 /* How many bytes in instructions from here to the end? */
13304 i = fragp->fr_var - i;
13305 }
13306 /* Convert to instruction count. */
13307 i >>= 2;
13308 /* Branch counts from the next instruction. */
b34976b6 13309 i--;
4a6a3df4
AO
13310 insn |= i;
13311 /* Branch over the jump. */
17a2f251 13312 md_number_to_chars (buf, insn, 4);
4a6a3df4
AO
13313 buf += 4;
13314
13315 /* Nop */
17a2f251 13316 md_number_to_chars (buf, 0, 4);
4a6a3df4
AO
13317 buf += 4;
13318
13319 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13320 {
13321 /* beql $0, $0, 2f */
13322 insn = 0x50000000;
13323 /* Compute the PC offset from the current instruction to
13324 the end of the variable frag. */
13325 /* How many bytes in instructions we've already emitted? */
13326 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13327 /* How many bytes in instructions from here to the end? */
13328 i = fragp->fr_var - i;
13329 /* Convert to instruction count. */
13330 i >>= 2;
13331 /* Don't decrement i, because we want to branch over the
13332 delay slot. */
13333
13334 insn |= i;
17a2f251 13335 md_number_to_chars (buf, insn, 4);
4a6a3df4
AO
13336 buf += 4;
13337
17a2f251 13338 md_number_to_chars (buf, 0, 4);
4a6a3df4
AO
13339 buf += 4;
13340 }
13341
13342 uncond:
13343 if (mips_pic == NO_PIC)
13344 {
13345 /* j or jal. */
13346 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
13347 ? 0x0c000000 : 0x08000000);
13348 exp.X_op = O_symbol;
13349 exp.X_add_symbol = fragp->fr_symbol;
13350 exp.X_add_number = fragp->fr_offset;
13351
13352 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13353 4, &exp, 0, BFD_RELOC_MIPS_JMP);
13354 fixp->fx_file = fragp->fr_file;
13355 fixp->fx_line = fragp->fr_line;
13356
17a2f251 13357 md_number_to_chars (buf, insn, 4);
4a6a3df4
AO
13358 buf += 4;
13359 }
13360 else
13361 {
13362 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
13363 insn = HAVE_64BIT_ADDRESSES ? 0xdf810000 : 0x8f810000;
13364 exp.X_op = O_symbol;
13365 exp.X_add_symbol = fragp->fr_symbol;
13366 exp.X_add_number = fragp->fr_offset;
13367
13368 if (fragp->fr_offset)
13369 {
13370 exp.X_add_symbol = make_expr_symbol (&exp);
13371 exp.X_add_number = 0;
13372 }
13373
13374 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13375 4, &exp, 0, BFD_RELOC_MIPS_GOT16);
13376 fixp->fx_file = fragp->fr_file;
13377 fixp->fx_line = fragp->fr_line;
13378
17a2f251 13379 md_number_to_chars (buf, insn, 4);
4a6a3df4 13380 buf += 4;
b34976b6 13381
4a6a3df4
AO
13382 if (mips_opts.isa == ISA_MIPS1)
13383 {
13384 /* nop */
17a2f251 13385 md_number_to_chars (buf, 0, 4);
4a6a3df4
AO
13386 buf += 4;
13387 }
13388
13389 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
13390 insn = HAVE_64BIT_ADDRESSES ? 0x64210000 : 0x24210000;
13391
13392 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13393 4, &exp, 0, BFD_RELOC_LO16);
13394 fixp->fx_file = fragp->fr_file;
13395 fixp->fx_line = fragp->fr_line;
b34976b6 13396
17a2f251 13397 md_number_to_chars (buf, insn, 4);
4a6a3df4
AO
13398 buf += 4;
13399
13400 /* j(al)r $at. */
13401 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13402 insn = 0x0020f809;
13403 else
13404 insn = 0x00200008;
13405
17a2f251 13406 md_number_to_chars (buf, insn, 4);
4a6a3df4
AO
13407 buf += 4;
13408 }
13409 }
13410
13411 assert (buf == (bfd_byte *)fragp->fr_literal
13412 + fragp->fr_fix + fragp->fr_var);
13413
13414 fragp->fr_fix += fragp->fr_var;
13415
13416 return;
13417 }
13418
252b5132
RH
13419 if (RELAX_MIPS16_P (fragp->fr_subtype))
13420 {
13421 int type;
13422 register const struct mips16_immed_operand *op;
b34976b6 13423 bfd_boolean small, ext;
252b5132
RH
13424 offsetT val;
13425 bfd_byte *buf;
13426 unsigned long insn;
b34976b6 13427 bfd_boolean use_extend;
252b5132
RH
13428 unsigned short extend;
13429
13430 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
13431 op = mips16_immed_operands;
13432 while (op->type != type)
13433 ++op;
13434
13435 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13436 {
b34976b6
AM
13437 small = FALSE;
13438 ext = TRUE;
252b5132
RH
13439 }
13440 else
13441 {
b34976b6
AM
13442 small = TRUE;
13443 ext = FALSE;
252b5132
RH
13444 }
13445
6386f3a7 13446 resolve_symbol_value (fragp->fr_symbol);
252b5132
RH
13447 val = S_GET_VALUE (fragp->fr_symbol);
13448 if (op->pcrel)
13449 {
13450 addressT addr;
13451
13452 addr = fragp->fr_address + fragp->fr_fix;
13453
13454 /* The rules for the base address of a PC relative reloc are
13455 complicated; see mips16_extended_frag. */
13456 if (type == 'p' || type == 'q')
13457 {
13458 addr += 2;
13459 if (ext)
13460 addr += 2;
13461 /* Ignore the low bit in the target, since it will be
13462 set for a text label. */
13463 if ((val & 1) != 0)
13464 --val;
13465 }
13466 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13467 addr -= 4;
13468 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13469 addr -= 2;
13470
13471 addr &= ~ (addressT) ((1 << op->shift) - 1);
13472 val -= addr;
13473
13474 /* Make sure the section winds up with the alignment we have
13475 assumed. */
13476 if (op->shift > 0)
13477 record_alignment (asec, op->shift);
13478 }
13479
13480 if (ext
13481 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
13482 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
13483 as_warn_where (fragp->fr_file, fragp->fr_line,
13484 _("extended instruction in delay slot"));
13485
13486 buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
13487
13488 if (target_big_endian)
13489 insn = bfd_getb16 (buf);
13490 else
13491 insn = bfd_getl16 (buf);
13492
13493 mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
13494 RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
13495 small, ext, &insn, &use_extend, &extend);
13496
13497 if (use_extend)
13498 {
17a2f251 13499 md_number_to_chars (buf, 0xf000 | extend, 2);
252b5132
RH
13500 fragp->fr_fix += 2;
13501 buf += 2;
13502 }
13503
17a2f251 13504 md_number_to_chars (buf, insn, 2);
252b5132
RH
13505 fragp->fr_fix += 2;
13506 buf += 2;
13507 }
13508 else
13509 {
4d7206a2
RS
13510 int first, second;
13511 fixS *fixp;
252b5132 13512
4d7206a2
RS
13513 first = RELAX_FIRST (fragp->fr_subtype);
13514 second = RELAX_SECOND (fragp->fr_subtype);
13515 fixp = (fixS *) fragp->fr_opcode;
252b5132 13516
584892a6
RS
13517 /* Possibly emit a warning if we've chosen the longer option. */
13518 if (((fragp->fr_subtype & RELAX_USE_SECOND) != 0)
13519 == ((fragp->fr_subtype & RELAX_SECOND_LONGER) != 0))
13520 {
13521 const char *msg = macro_warning (fragp->fr_subtype);
13522 if (msg != 0)
13523 as_warn_where (fragp->fr_file, fragp->fr_line, msg);
13524 }
13525
4d7206a2
RS
13526 /* Go through all the fixups for the first sequence. Disable them
13527 (by marking them as done) if we're going to use the second
13528 sequence instead. */
13529 while (fixp
13530 && fixp->fx_frag == fragp
13531 && fixp->fx_where < fragp->fr_fix - second)
13532 {
13533 if (fragp->fr_subtype & RELAX_USE_SECOND)
13534 fixp->fx_done = 1;
13535 fixp = fixp->fx_next;
13536 }
252b5132 13537
4d7206a2
RS
13538 /* Go through the fixups for the second sequence. Disable them if
13539 we're going to use the first sequence, otherwise adjust their
13540 addresses to account for the relaxation. */
13541 while (fixp && fixp->fx_frag == fragp)
13542 {
13543 if (fragp->fr_subtype & RELAX_USE_SECOND)
13544 fixp->fx_where -= first;
13545 else
13546 fixp->fx_done = 1;
13547 fixp = fixp->fx_next;
13548 }
13549
13550 /* Now modify the frag contents. */
13551 if (fragp->fr_subtype & RELAX_USE_SECOND)
13552 {
13553 char *start;
13554
13555 start = fragp->fr_literal + fragp->fr_fix - first - second;
13556 memmove (start, start + first, second);
13557 fragp->fr_fix -= first;
13558 }
13559 else
13560 fragp->fr_fix -= second;
252b5132
RH
13561 }
13562}
13563
13564#ifdef OBJ_ELF
13565
13566/* This function is called after the relocs have been generated.
13567 We've been storing mips16 text labels as odd. Here we convert them
13568 back to even for the convenience of the debugger. */
13569
13570void
17a2f251 13571mips_frob_file_after_relocs (void)
252b5132
RH
13572{
13573 asymbol **syms;
13574 unsigned int count, i;
13575
13576 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
13577 return;
13578
13579 syms = bfd_get_outsymbols (stdoutput);
13580 count = bfd_get_symcount (stdoutput);
13581 for (i = 0; i < count; i++, syms++)
13582 {
13583 if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
13584 && ((*syms)->value & 1) != 0)
13585 {
13586 (*syms)->value &= ~1;
13587 /* If the symbol has an odd size, it was probably computed
13588 incorrectly, so adjust that as well. */
13589 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
13590 ++elf_symbol (*syms)->internal_elf_sym.st_size;
13591 }
13592 }
13593}
13594
13595#endif
13596
13597/* This function is called whenever a label is defined. It is used
13598 when handling branch delays; if a branch has a label, we assume we
13599 can not move it. */
13600
13601void
17a2f251 13602mips_define_label (symbolS *sym)
252b5132
RH
13603{
13604 struct insn_label_list *l;
13605
13606 if (free_insn_labels == NULL)
13607 l = (struct insn_label_list *) xmalloc (sizeof *l);
13608 else
13609 {
13610 l = free_insn_labels;
13611 free_insn_labels = l->next;
13612 }
13613
13614 l->label = sym;
13615 l->next = insn_labels;
13616 insn_labels = l;
13617}
13618\f
13619#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13620
13621/* Some special processing for a MIPS ELF file. */
13622
13623void
17a2f251 13624mips_elf_final_processing (void)
252b5132
RH
13625{
13626 /* Write out the register information. */
316f5878 13627 if (mips_abi != N64_ABI)
252b5132
RH
13628 {
13629 Elf32_RegInfo s;
13630
13631 s.ri_gprmask = mips_gprmask;
13632 s.ri_cprmask[0] = mips_cprmask[0];
13633 s.ri_cprmask[1] = mips_cprmask[1];
13634 s.ri_cprmask[2] = mips_cprmask[2];
13635 s.ri_cprmask[3] = mips_cprmask[3];
13636 /* The gp_value field is set by the MIPS ELF backend. */
13637
13638 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
13639 ((Elf32_External_RegInfo *)
13640 mips_regmask_frag));
13641 }
13642 else
13643 {
13644 Elf64_Internal_RegInfo s;
13645
13646 s.ri_gprmask = mips_gprmask;
13647 s.ri_pad = 0;
13648 s.ri_cprmask[0] = mips_cprmask[0];
13649 s.ri_cprmask[1] = mips_cprmask[1];
13650 s.ri_cprmask[2] = mips_cprmask[2];
13651 s.ri_cprmask[3] = mips_cprmask[3];
13652 /* The gp_value field is set by the MIPS ELF backend. */
13653
13654 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
13655 ((Elf64_External_RegInfo *)
13656 mips_regmask_frag));
13657 }
13658
13659 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
13660 sort of BFD interface for this. */
13661 if (mips_any_noreorder)
13662 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
13663 if (mips_pic != NO_PIC)
143d77c5 13664 {
252b5132 13665 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
143d77c5
EC
13666 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
13667 }
13668 if (mips_abicalls)
13669 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
252b5132 13670
98d3f06f 13671 /* Set MIPS ELF flags for ASEs. */
a4672219
TS
13672 if (file_ase_mips16)
13673 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
1f25f5d3
CD
13674#if 0 /* XXX FIXME */
13675 if (file_ase_mips3d)
13676 elf_elfheader (stdoutput)->e_flags |= ???;
13677#endif
deec1734
CD
13678 if (file_ase_mdmx)
13679 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
1f25f5d3 13680
bdaaa2e1 13681 /* Set the MIPS ELF ABI flags. */
316f5878 13682 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
252b5132 13683 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
316f5878 13684 else if (mips_abi == O64_ABI)
252b5132 13685 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
316f5878 13686 else if (mips_abi == EABI_ABI)
252b5132 13687 {
316f5878 13688 if (!file_mips_gp32)
252b5132
RH
13689 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
13690 else
13691 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
13692 }
316f5878 13693 else if (mips_abi == N32_ABI)
be00bddd
TS
13694 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
13695
c9914766 13696 /* Nothing to do for N64_ABI. */
252b5132
RH
13697
13698 if (mips_32bitmode)
13699 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
13700}
13701
13702#endif /* OBJ_ELF || OBJ_MAYBE_ELF */
13703\f
beae10d5
KH
13704typedef struct proc {
13705 symbolS *isym;
13706 unsigned long reg_mask;
13707 unsigned long reg_offset;
13708 unsigned long fpreg_mask;
13709 unsigned long fpreg_offset;
13710 unsigned long frame_offset;
13711 unsigned long frame_reg;
13712 unsigned long pc_reg;
13713} procS;
252b5132
RH
13714
13715static procS cur_proc;
13716static procS *cur_proc_ptr;
13717static int numprocs;
13718
0a9ef439 13719/* Fill in an rs_align_code fragment. */
a19d8eb0 13720
0a9ef439 13721void
17a2f251 13722mips_handle_align (fragS *fragp)
a19d8eb0 13723{
0a9ef439
RH
13724 if (fragp->fr_type != rs_align_code)
13725 return;
13726
13727 if (mips_opts.mips16)
a19d8eb0
CP
13728 {
13729 static const unsigned char be_nop[] = { 0x65, 0x00 };
13730 static const unsigned char le_nop[] = { 0x00, 0x65 };
13731
0a9ef439
RH
13732 int bytes;
13733 char *p;
a19d8eb0 13734
0a9ef439
RH
13735 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
13736 p = fragp->fr_literal + fragp->fr_fix;
13737
13738 if (bytes & 1)
13739 {
13740 *p++ = 0;
f9419b05 13741 fragp->fr_fix++;
0a9ef439
RH
13742 }
13743
13744 memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
13745 fragp->fr_var = 2;
a19d8eb0
CP
13746 }
13747
0a9ef439 13748 /* For mips32, a nop is a zero, which we trivially get by doing nothing. */
a19d8eb0
CP
13749}
13750
252b5132 13751static void
17a2f251 13752md_obj_begin (void)
252b5132
RH
13753{
13754}
13755
13756static void
17a2f251 13757md_obj_end (void)
252b5132
RH
13758{
13759 /* check for premature end, nesting errors, etc */
13760 if (cur_proc_ptr)
9a41af64 13761 as_warn (_("missing .end at end of assembly"));
252b5132
RH
13762}
13763
13764static long
17a2f251 13765get_number (void)
252b5132
RH
13766{
13767 int negative = 0;
13768 long val = 0;
13769
13770 if (*input_line_pointer == '-')
13771 {
13772 ++input_line_pointer;
13773 negative = 1;
13774 }
3882b010 13775 if (!ISDIGIT (*input_line_pointer))
956cd1d6 13776 as_bad (_("expected simple number"));
252b5132
RH
13777 if (input_line_pointer[0] == '0')
13778 {
13779 if (input_line_pointer[1] == 'x')
13780 {
13781 input_line_pointer += 2;
3882b010 13782 while (ISXDIGIT (*input_line_pointer))
252b5132
RH
13783 {
13784 val <<= 4;
13785 val |= hex_value (*input_line_pointer++);
13786 }
13787 return negative ? -val : val;
13788 }
13789 else
13790 {
13791 ++input_line_pointer;
3882b010 13792 while (ISDIGIT (*input_line_pointer))
252b5132
RH
13793 {
13794 val <<= 3;
13795 val |= *input_line_pointer++ - '0';
13796 }
13797 return negative ? -val : val;
13798 }
13799 }
3882b010 13800 if (!ISDIGIT (*input_line_pointer))
252b5132
RH
13801 {
13802 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
13803 *input_line_pointer, *input_line_pointer);
956cd1d6 13804 as_warn (_("invalid number"));
252b5132
RH
13805 return -1;
13806 }
3882b010 13807 while (ISDIGIT (*input_line_pointer))
252b5132
RH
13808 {
13809 val *= 10;
13810 val += *input_line_pointer++ - '0';
13811 }
13812 return negative ? -val : val;
13813}
13814
13815/* The .file directive; just like the usual .file directive, but there
c5dd6aab
DJ
13816 is an initial number which is the ECOFF file index. In the non-ECOFF
13817 case .file implies DWARF-2. */
13818
13819static void
17a2f251 13820s_mips_file (int x ATTRIBUTE_UNUSED)
c5dd6aab 13821{
ecb4347a
DJ
13822 static int first_file_directive = 0;
13823
c5dd6aab
DJ
13824 if (ECOFF_DEBUGGING)
13825 {
13826 get_number ();
13827 s_app_file (0);
13828 }
13829 else
ecb4347a
DJ
13830 {
13831 char *filename;
13832
13833 filename = dwarf2_directive_file (0);
13834
13835 /* Versions of GCC up to 3.1 start files with a ".file"
13836 directive even for stabs output. Make sure that this
13837 ".file" is handled. Note that you need a version of GCC
13838 after 3.1 in order to support DWARF-2 on MIPS. */
13839 if (filename != NULL && ! first_file_directive)
13840 {
13841 (void) new_logical_line (filename, -1);
13842 s_app_file_string (filename);
13843 }
13844 first_file_directive = 1;
13845 }
c5dd6aab
DJ
13846}
13847
13848/* The .loc directive, implying DWARF-2. */
252b5132
RH
13849
13850static void
17a2f251 13851s_mips_loc (int x ATTRIBUTE_UNUSED)
252b5132 13852{
c5dd6aab
DJ
13853 if (!ECOFF_DEBUGGING)
13854 dwarf2_directive_loc (0);
252b5132
RH
13855}
13856
252b5132
RH
13857/* The .end directive. */
13858
13859static void
17a2f251 13860s_mips_end (int x ATTRIBUTE_UNUSED)
252b5132
RH
13861{
13862 symbolS *p;
252b5132 13863
7a621144
DJ
13864 /* Following functions need their own .frame and .cprestore directives. */
13865 mips_frame_reg_valid = 0;
13866 mips_cprestore_valid = 0;
13867
252b5132
RH
13868 if (!is_end_of_line[(unsigned char) *input_line_pointer])
13869 {
13870 p = get_symbol ();
13871 demand_empty_rest_of_line ();
13872 }
13873 else
13874 p = NULL;
13875
14949570 13876 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
13877 as_warn (_(".end not in text section"));
13878
13879 if (!cur_proc_ptr)
13880 {
13881 as_warn (_(".end directive without a preceding .ent directive."));
13882 demand_empty_rest_of_line ();
13883 return;
13884 }
13885
13886 if (p != NULL)
13887 {
13888 assert (S_GET_NAME (p));
13889 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->isym)))
13890 as_warn (_(".end symbol does not match .ent symbol."));
ecb4347a
DJ
13891
13892 if (debug_type == DEBUG_STABS)
13893 stabs_generate_asm_endfunc (S_GET_NAME (p),
13894 S_GET_NAME (p));
252b5132
RH
13895 }
13896 else
13897 as_warn (_(".end directive missing or unknown symbol"));
13898
ecb4347a
DJ
13899#ifdef OBJ_ELF
13900 /* Generate a .pdr section. */
dcd410fe
RO
13901 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING
13902 && mips_flag_pdr)
ecb4347a
DJ
13903 {
13904 segT saved_seg = now_seg;
13905 subsegT saved_subseg = now_subseg;
13906 valueT dot;
13907 expressionS exp;
13908 char *fragp;
252b5132 13909
ecb4347a 13910 dot = frag_now_fix ();
252b5132
RH
13911
13912#ifdef md_flush_pending_output
ecb4347a 13913 md_flush_pending_output ();
252b5132
RH
13914#endif
13915
ecb4347a
DJ
13916 assert (pdr_seg);
13917 subseg_set (pdr_seg, 0);
252b5132 13918
ecb4347a
DJ
13919 /* Write the symbol. */
13920 exp.X_op = O_symbol;
13921 exp.X_add_symbol = p;
13922 exp.X_add_number = 0;
13923 emit_expr (&exp, 4);
252b5132 13924
ecb4347a 13925 fragp = frag_more (7 * 4);
252b5132 13926
17a2f251
TS
13927 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
13928 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
13929 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
13930 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
13931 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
13932 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
13933 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
252b5132 13934
ecb4347a
DJ
13935 subseg_set (saved_seg, saved_subseg);
13936 }
13937#endif /* OBJ_ELF */
252b5132
RH
13938
13939 cur_proc_ptr = NULL;
13940}
13941
13942/* The .aent and .ent directives. */
13943
13944static void
17a2f251 13945s_mips_ent (int aent)
252b5132 13946{
252b5132 13947 symbolS *symbolP;
252b5132
RH
13948
13949 symbolP = get_symbol ();
13950 if (*input_line_pointer == ',')
f9419b05 13951 ++input_line_pointer;
252b5132 13952 SKIP_WHITESPACE ();
3882b010 13953 if (ISDIGIT (*input_line_pointer)
d9a62219 13954 || *input_line_pointer == '-')
874e8986 13955 get_number ();
252b5132 13956
14949570 13957 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
13958 as_warn (_(".ent or .aent not in text section."));
13959
13960 if (!aent && cur_proc_ptr)
9a41af64 13961 as_warn (_("missing .end"));
252b5132
RH
13962
13963 if (!aent)
13964 {
7a621144
DJ
13965 /* This function needs its own .frame and .cprestore directives. */
13966 mips_frame_reg_valid = 0;
13967 mips_cprestore_valid = 0;
13968
252b5132
RH
13969 cur_proc_ptr = &cur_proc;
13970 memset (cur_proc_ptr, '\0', sizeof (procS));
13971
13972 cur_proc_ptr->isym = symbolP;
13973
49309057 13974 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
252b5132 13975
f9419b05 13976 ++numprocs;
ecb4347a
DJ
13977
13978 if (debug_type == DEBUG_STABS)
13979 stabs_generate_asm_func (S_GET_NAME (symbolP),
13980 S_GET_NAME (symbolP));
252b5132
RH
13981 }
13982
13983 demand_empty_rest_of_line ();
13984}
13985
13986/* The .frame directive. If the mdebug section is present (IRIX 5 native)
bdaaa2e1 13987 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
252b5132 13988 s_mips_frame is used so that we can set the PDR information correctly.
bdaaa2e1 13989 We can't use the ecoff routines because they make reference to the ecoff
252b5132
RH
13990 symbol table (in the mdebug section). */
13991
13992static void
17a2f251 13993s_mips_frame (int ignore ATTRIBUTE_UNUSED)
252b5132 13994{
ecb4347a
DJ
13995#ifdef OBJ_ELF
13996 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
13997 {
13998 long val;
252b5132 13999
ecb4347a
DJ
14000 if (cur_proc_ptr == (procS *) NULL)
14001 {
14002 as_warn (_(".frame outside of .ent"));
14003 demand_empty_rest_of_line ();
14004 return;
14005 }
252b5132 14006
ecb4347a
DJ
14007 cur_proc_ptr->frame_reg = tc_get_register (1);
14008
14009 SKIP_WHITESPACE ();
14010 if (*input_line_pointer++ != ','
14011 || get_absolute_expression_and_terminator (&val) != ',')
14012 {
14013 as_warn (_("Bad .frame directive"));
14014 --input_line_pointer;
14015 demand_empty_rest_of_line ();
14016 return;
14017 }
252b5132 14018
ecb4347a
DJ
14019 cur_proc_ptr->frame_offset = val;
14020 cur_proc_ptr->pc_reg = tc_get_register (0);
252b5132 14021
252b5132 14022 demand_empty_rest_of_line ();
252b5132 14023 }
ecb4347a
DJ
14024 else
14025#endif /* OBJ_ELF */
14026 s_ignore (ignore);
252b5132
RH
14027}
14028
bdaaa2e1
KH
14029/* The .fmask and .mask directives. If the mdebug section is present
14030 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
252b5132 14031 embedded targets, s_mips_mask is used so that we can set the PDR
bdaaa2e1 14032 information correctly. We can't use the ecoff routines because they
252b5132
RH
14033 make reference to the ecoff symbol table (in the mdebug section). */
14034
14035static void
17a2f251 14036s_mips_mask (int reg_type)
252b5132 14037{
ecb4347a
DJ
14038#ifdef OBJ_ELF
14039 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
252b5132 14040 {
ecb4347a 14041 long mask, off;
252b5132 14042
ecb4347a
DJ
14043 if (cur_proc_ptr == (procS *) NULL)
14044 {
14045 as_warn (_(".mask/.fmask outside of .ent"));
14046 demand_empty_rest_of_line ();
14047 return;
14048 }
252b5132 14049
ecb4347a
DJ
14050 if (get_absolute_expression_and_terminator (&mask) != ',')
14051 {
14052 as_warn (_("Bad .mask/.fmask directive"));
14053 --input_line_pointer;
14054 demand_empty_rest_of_line ();
14055 return;
14056 }
252b5132 14057
ecb4347a
DJ
14058 off = get_absolute_expression ();
14059
14060 if (reg_type == 'F')
14061 {
14062 cur_proc_ptr->fpreg_mask = mask;
14063 cur_proc_ptr->fpreg_offset = off;
14064 }
14065 else
14066 {
14067 cur_proc_ptr->reg_mask = mask;
14068 cur_proc_ptr->reg_offset = off;
14069 }
14070
14071 demand_empty_rest_of_line ();
252b5132
RH
14072 }
14073 else
ecb4347a
DJ
14074#endif /* OBJ_ELF */
14075 s_ignore (reg_type);
252b5132
RH
14076}
14077
14078/* The .loc directive. */
14079
14080#if 0
14081static void
17a2f251 14082s_loc (int x)
252b5132
RH
14083{
14084 symbolS *symbolP;
14085 int lineno;
14086 int addroff;
14087
14088 assert (now_seg == text_section);
14089
14090 lineno = get_number ();
14091 addroff = frag_now_fix ();
14092
14093 symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
14094 S_SET_TYPE (symbolP, N_SLINE);
14095 S_SET_OTHER (symbolP, 0);
14096 S_SET_DESC (symbolP, lineno);
14097 symbolP->sy_segment = now_seg;
14098}
14099#endif
e7af610e 14100
316f5878
RS
14101/* A table describing all the processors gas knows about. Names are
14102 matched in the order listed.
e7af610e 14103
316f5878
RS
14104 To ease comparison, please keep this table in the same order as
14105 gcc's mips_cpu_info_table[]. */
e972090a
NC
14106static const struct mips_cpu_info mips_cpu_info_table[] =
14107{
316f5878
RS
14108 /* Entries for generic ISAs */
14109 { "mips1", 1, ISA_MIPS1, CPU_R3000 },
14110 { "mips2", 1, ISA_MIPS2, CPU_R6000 },
14111 { "mips3", 1, ISA_MIPS3, CPU_R4000 },
14112 { "mips4", 1, ISA_MIPS4, CPU_R8000 },
14113 { "mips5", 1, ISA_MIPS5, CPU_MIPS5 },
14114 { "mips32", 1, ISA_MIPS32, CPU_MIPS32 },
af7ee8bf 14115 { "mips32r2", 1, ISA_MIPS32R2, CPU_MIPS32R2 },
316f5878 14116 { "mips64", 1, ISA_MIPS64, CPU_MIPS64 },
5f74bc13 14117 { "mips64r2", 1, ISA_MIPS64R2, CPU_MIPS64R2 },
316f5878
RS
14118
14119 /* MIPS I */
14120 { "r3000", 0, ISA_MIPS1, CPU_R3000 },
14121 { "r2000", 0, ISA_MIPS1, CPU_R3000 },
14122 { "r3900", 0, ISA_MIPS1, CPU_R3900 },
14123
14124 /* MIPS II */
14125 { "r6000", 0, ISA_MIPS2, CPU_R6000 },
14126
14127 /* MIPS III */
14128 { "r4000", 0, ISA_MIPS3, CPU_R4000 },
14129 { "r4010", 0, ISA_MIPS2, CPU_R4010 },
14130 { "vr4100", 0, ISA_MIPS3, CPU_VR4100 },
14131 { "vr4111", 0, ISA_MIPS3, CPU_R4111 },
60b63b72
RS
14132 { "vr4120", 0, ISA_MIPS3, CPU_VR4120 },
14133 { "vr4130", 0, ISA_MIPS3, CPU_VR4120 },
14134 { "vr4181", 0, ISA_MIPS3, CPU_R4111 },
316f5878
RS
14135 { "vr4300", 0, ISA_MIPS3, CPU_R4300 },
14136 { "r4400", 0, ISA_MIPS3, CPU_R4400 },
14137 { "r4600", 0, ISA_MIPS3, CPU_R4600 },
14138 { "orion", 0, ISA_MIPS3, CPU_R4600 },
14139 { "r4650", 0, ISA_MIPS3, CPU_R4650 },
14140
14141 /* MIPS IV */
14142 { "r8000", 0, ISA_MIPS4, CPU_R8000 },
14143 { "r10000", 0, ISA_MIPS4, CPU_R10000 },
14144 { "r12000", 0, ISA_MIPS4, CPU_R12000 },
14145 { "vr5000", 0, ISA_MIPS4, CPU_R5000 },
60b63b72
RS
14146 { "vr5400", 0, ISA_MIPS4, CPU_VR5400 },
14147 { "vr5500", 0, ISA_MIPS4, CPU_VR5500 },
316f5878
RS
14148 { "rm5200", 0, ISA_MIPS4, CPU_R5000 },
14149 { "rm5230", 0, ISA_MIPS4, CPU_R5000 },
14150 { "rm5231", 0, ISA_MIPS4, CPU_R5000 },
14151 { "rm5261", 0, ISA_MIPS4, CPU_R5000 },
14152 { "rm5721", 0, ISA_MIPS4, CPU_R5000 },
5a7ea749
RS
14153 { "rm7000", 0, ISA_MIPS4, CPU_RM7000 },
14154 { "rm9000", 0, ISA_MIPS4, CPU_RM7000 },
316f5878
RS
14155
14156 /* MIPS 32 */
fef14a42 14157 { "4kc", 0, ISA_MIPS32, CPU_MIPS32 },
316f5878
RS
14158 { "4km", 0, ISA_MIPS32, CPU_MIPS32 },
14159 { "4kp", 0, ISA_MIPS32, CPU_MIPS32 },
e7af610e 14160
316f5878
RS
14161 /* MIPS 64 */
14162 { "5kc", 0, ISA_MIPS64, CPU_MIPS64 },
14163 { "20kc", 0, ISA_MIPS64, CPU_MIPS64 },
e7af610e 14164
c7a23324 14165 /* Broadcom SB-1 CPU core */
316f5878 14166 { "sb1", 0, ISA_MIPS64, CPU_SB1 },
e7af610e 14167
316f5878
RS
14168 /* End marker */
14169 { NULL, 0, 0, 0 }
14170};
e7af610e 14171
84ea6cf2 14172
316f5878
RS
14173/* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
14174 with a final "000" replaced by "k". Ignore case.
e7af610e 14175
316f5878 14176 Note: this function is shared between GCC and GAS. */
c6c98b38 14177
b34976b6 14178static bfd_boolean
17a2f251 14179mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
14180{
14181 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
14182 given++, canonical++;
14183
14184 return ((*given == 0 && *canonical == 0)
14185 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
14186}
14187
14188
14189/* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
14190 CPU name. We've traditionally allowed a lot of variation here.
14191
14192 Note: this function is shared between GCC and GAS. */
14193
b34976b6 14194static bfd_boolean
17a2f251 14195mips_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
14196{
14197 /* First see if the name matches exactly, or with a final "000"
14198 turned into "k". */
14199 if (mips_strict_matching_cpu_name_p (canonical, given))
b34976b6 14200 return TRUE;
316f5878
RS
14201
14202 /* If not, try comparing based on numerical designation alone.
14203 See if GIVEN is an unadorned number, or 'r' followed by a number. */
14204 if (TOLOWER (*given) == 'r')
14205 given++;
14206 if (!ISDIGIT (*given))
b34976b6 14207 return FALSE;
316f5878
RS
14208
14209 /* Skip over some well-known prefixes in the canonical name,
14210 hoping to find a number there too. */
14211 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
14212 canonical += 2;
14213 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
14214 canonical += 2;
14215 else if (TOLOWER (canonical[0]) == 'r')
14216 canonical += 1;
14217
14218 return mips_strict_matching_cpu_name_p (canonical, given);
14219}
14220
14221
14222/* Parse an option that takes the name of a processor as its argument.
14223 OPTION is the name of the option and CPU_STRING is the argument.
14224 Return the corresponding processor enumeration if the CPU_STRING is
14225 recognized, otherwise report an error and return null.
14226
14227 A similar function exists in GCC. */
e7af610e
NC
14228
14229static const struct mips_cpu_info *
17a2f251 14230mips_parse_cpu (const char *option, const char *cpu_string)
e7af610e 14231{
316f5878 14232 const struct mips_cpu_info *p;
e7af610e 14233
316f5878
RS
14234 /* 'from-abi' selects the most compatible architecture for the given
14235 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
14236 EABIs, we have to decide whether we're using the 32-bit or 64-bit
14237 version. Look first at the -mgp options, if given, otherwise base
14238 the choice on MIPS_DEFAULT_64BIT.
e7af610e 14239
316f5878
RS
14240 Treat NO_ABI like the EABIs. One reason to do this is that the
14241 plain 'mips' and 'mips64' configs have 'from-abi' as their default
14242 architecture. This code picks MIPS I for 'mips' and MIPS III for
14243 'mips64', just as we did in the days before 'from-abi'. */
14244 if (strcasecmp (cpu_string, "from-abi") == 0)
14245 {
14246 if (ABI_NEEDS_32BIT_REGS (mips_abi))
14247 return mips_cpu_info_from_isa (ISA_MIPS1);
14248
14249 if (ABI_NEEDS_64BIT_REGS (mips_abi))
14250 return mips_cpu_info_from_isa (ISA_MIPS3);
14251
14252 if (file_mips_gp32 >= 0)
14253 return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
14254
14255 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
14256 ? ISA_MIPS3
14257 : ISA_MIPS1);
14258 }
14259
14260 /* 'default' has traditionally been a no-op. Probably not very useful. */
14261 if (strcasecmp (cpu_string, "default") == 0)
14262 return 0;
14263
14264 for (p = mips_cpu_info_table; p->name != 0; p++)
14265 if (mips_matching_cpu_name_p (p->name, cpu_string))
14266 return p;
14267
14268 as_bad ("Bad value (%s) for %s", cpu_string, option);
14269 return 0;
e7af610e
NC
14270}
14271
316f5878
RS
14272/* Return the canonical processor information for ISA (a member of the
14273 ISA_MIPS* enumeration). */
14274
e7af610e 14275static const struct mips_cpu_info *
17a2f251 14276mips_cpu_info_from_isa (int isa)
e7af610e
NC
14277{
14278 int i;
14279
14280 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14281 if (mips_cpu_info_table[i].is_isa
316f5878 14282 && isa == mips_cpu_info_table[i].isa)
e7af610e
NC
14283 return (&mips_cpu_info_table[i]);
14284
e972090a 14285 return NULL;
e7af610e 14286}
fef14a42
TS
14287
14288static const struct mips_cpu_info *
17a2f251 14289mips_cpu_info_from_arch (int arch)
fef14a42
TS
14290{
14291 int i;
14292
14293 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14294 if (arch == mips_cpu_info_table[i].cpu)
14295 return (&mips_cpu_info_table[i]);
14296
14297 return NULL;
14298}
316f5878
RS
14299\f
14300static void
17a2f251 14301show (FILE *stream, const char *string, int *col_p, int *first_p)
316f5878
RS
14302{
14303 if (*first_p)
14304 {
14305 fprintf (stream, "%24s", "");
14306 *col_p = 24;
14307 }
14308 else
14309 {
14310 fprintf (stream, ", ");
14311 *col_p += 2;
14312 }
e7af610e 14313
316f5878
RS
14314 if (*col_p + strlen (string) > 72)
14315 {
14316 fprintf (stream, "\n%24s", "");
14317 *col_p = 24;
14318 }
14319
14320 fprintf (stream, "%s", string);
14321 *col_p += strlen (string);
14322
14323 *first_p = 0;
14324}
14325
14326void
17a2f251 14327md_show_usage (FILE *stream)
e7af610e 14328{
316f5878
RS
14329 int column, first;
14330 size_t i;
14331
14332 fprintf (stream, _("\
14333MIPS options:\n\
14334-membedded-pic generate embedded position independent code\n\
14335-EB generate big endian output\n\
14336-EL generate little endian output\n\
14337-g, -g2 do not remove unneeded NOPs or swap branches\n\
14338-G NUM allow referencing objects up to NUM bytes\n\
14339 implicitly with the gp register [default 8]\n"));
14340 fprintf (stream, _("\
14341-mips1 generate MIPS ISA I instructions\n\
14342-mips2 generate MIPS ISA II instructions\n\
14343-mips3 generate MIPS ISA III instructions\n\
14344-mips4 generate MIPS ISA IV instructions\n\
14345-mips5 generate MIPS ISA V instructions\n\
14346-mips32 generate MIPS32 ISA instructions\n\
af7ee8bf 14347-mips32r2 generate MIPS32 release 2 ISA instructions\n\
316f5878 14348-mips64 generate MIPS64 ISA instructions\n\
5f74bc13 14349-mips64r2 generate MIPS64 release 2 ISA instructions\n\
316f5878
RS
14350-march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
14351
14352 first = 1;
e7af610e
NC
14353
14354 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
316f5878
RS
14355 show (stream, mips_cpu_info_table[i].name, &column, &first);
14356 show (stream, "from-abi", &column, &first);
14357 fputc ('\n', stream);
e7af610e 14358
316f5878
RS
14359 fprintf (stream, _("\
14360-mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
14361-no-mCPU don't generate code specific to CPU.\n\
14362 For -mCPU and -no-mCPU, CPU must be one of:\n"));
14363
14364 first = 1;
14365
14366 show (stream, "3900", &column, &first);
14367 show (stream, "4010", &column, &first);
14368 show (stream, "4100", &column, &first);
14369 show (stream, "4650", &column, &first);
14370 fputc ('\n', stream);
14371
14372 fprintf (stream, _("\
14373-mips16 generate mips16 instructions\n\
14374-no-mips16 do not generate mips16 instructions\n"));
14375 fprintf (stream, _("\
14376-mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
14377-mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
14378-O0 remove unneeded NOPs, do not swap branches\n\
14379-O remove unneeded NOPs and swap branches\n\
316f5878
RS
14380--[no-]construct-floats [dis]allow floating point values to be constructed\n\
14381--trap, --no-break trap exception on div by 0 and mult overflow\n\
14382--break, --no-trap break exception on div by 0 and mult overflow\n"));
14383#ifdef OBJ_ELF
14384 fprintf (stream, _("\
14385-KPIC, -call_shared generate SVR4 position independent code\n\
14386-non_shared do not generate position independent code\n\
14387-xgot assume a 32 bit GOT\n\
dcd410fe 14388-mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
316f5878
RS
14389-mabi=ABI create ABI conformant object file for:\n"));
14390
14391 first = 1;
14392
14393 show (stream, "32", &column, &first);
14394 show (stream, "o64", &column, &first);
14395 show (stream, "n32", &column, &first);
14396 show (stream, "64", &column, &first);
14397 show (stream, "eabi", &column, &first);
14398
14399 fputc ('\n', stream);
14400
14401 fprintf (stream, _("\
14402-32 create o32 ABI object file (default)\n\
14403-n32 create n32 ABI object file\n\
14404-64 create 64 ABI object file\n"));
14405#endif
e7af610e 14406}
14e777e0
KB
14407
14408enum dwarf2_format
17a2f251 14409mips_dwarf2_format (void)
14e777e0
KB
14410{
14411 if (mips_abi == N64_ABI)
1de5b6a1
AO
14412 {
14413#ifdef TE_IRIX
14414 return dwarf2_format_64bit_irix;
14415#else
14416 return dwarf2_format_64bit;
14417#endif
14418 }
14e777e0
KB
14419 else
14420 return dwarf2_format_32bit;
14421}
This page took 1.279321 seconds and 4 git commands to generate.