2003-12-16 Michael Chastain <mec.gnu@mindspring.com>
[deliverable/binutils-gdb.git] / gas / config / tc-mips.c
CommitLineData
252b5132 1/* tc-mips.c -- assemble code for a MIPS chip.
071742cf 2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
82efde3a 3 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
9ce8a5dd
GRK
240/* Some ISA's have delay slots for instructions which read or write
241 from a coprocessor (eg. mips1-mips3); some don't (eg mips4).
bdaaa2e1 242 Return true if instructions marked INSN_LOAD_COPROC_DELAY,
9ce8a5dd
GRK
243 INSN_COPROC_MOVE_DELAY, or INSN_WRITE_COND_CODE actually have a
244 delay slot in this ISA. The uses of this macro assume that any
245 ISA that has delay slots for one of these, has them for all. They
246 also assume that ISAs which don't have delays for these insns, don't
bdaaa2e1 247 have delays for the INSN_LOAD_MEMORY_DELAY instructions either. */
9ce8a5dd 248#define ISA_HAS_COPROC_DELAYS(ISA) ( \
e7af610e
NC
249 (ISA) == ISA_MIPS1 \
250 || (ISA) == ISA_MIPS2 \
251 || (ISA) == ISA_MIPS3 \
9ce8a5dd
GRK
252 )
253
316f5878
RS
254/* True if the given ABI requires 32-bit registers. */
255#define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
256
257/* Likewise 64-bit registers. */
258#define ABI_NEEDS_64BIT_REGS(ABI) \
259 ((ABI) == N32_ABI \
260 || (ABI) == N64_ABI \
261 || (ABI) == O64_ABI)
262
bdaaa2e1 263/* Return true if ISA supports 64 bit gp register instructions. */
9ce8a5dd 264#define ISA_HAS_64BIT_REGS(ISA) ( \
e7af610e
NC
265 (ISA) == ISA_MIPS3 \
266 || (ISA) == ISA_MIPS4 \
84ea6cf2 267 || (ISA) == ISA_MIPS5 \
d1cf510e 268 || (ISA) == ISA_MIPS64 \
5f74bc13 269 || (ISA) == ISA_MIPS64R2 \
9ce8a5dd
GRK
270 )
271
af7ee8bf
CD
272/* Return true if ISA supports 64-bit right rotate (dror et al.)
273 instructions. */
274#define ISA_HAS_DROR(ISA) ( \
5f74bc13 275 (ISA) == ISA_MIPS64R2 \
af7ee8bf
CD
276 )
277
278/* Return true if ISA supports 32-bit right rotate (ror et al.)
279 instructions. */
280#define ISA_HAS_ROR(ISA) ( \
281 (ISA) == ISA_MIPS32R2 \
5f74bc13 282 || (ISA) == ISA_MIPS64R2 \
af7ee8bf
CD
283 )
284
e013f690 285#define HAVE_32BIT_GPRS \
316f5878 286 (mips_opts.gp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
ca4e0257 287
e013f690 288#define HAVE_32BIT_FPRS \
316f5878 289 (mips_opts.fp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
ca4e0257
RS
290
291#define HAVE_64BIT_GPRS (! HAVE_32BIT_GPRS)
292#define HAVE_64BIT_FPRS (! HAVE_32BIT_FPRS)
293
316f5878 294#define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
e013f690 295
316f5878 296#define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
e013f690
TS
297
298/* We can only have 64bit addresses if the object file format
299 supports it. */
afdbd6d0
CD
300#define HAVE_32BIT_ADDRESSES \
301 (HAVE_32BIT_GPRS \
302 || ((bfd_arch_bits_per_address (stdoutput) == 32 \
303 || ! HAVE_64BIT_OBJECTS) \
304 && mips_pic != EMBEDDED_PIC))
e013f690
TS
305
306#define HAVE_64BIT_ADDRESSES (! HAVE_32BIT_ADDRESSES)
ca4e0257 307
b7c7d6c1
TS
308/* Addresses are loaded in different ways, depending on the address size
309 in use. The n32 ABI Documentation also mandates the use of additions
310 with overflow checking, but existing implementations don't follow it. */
f899b4b8 311#define ADDRESS_ADD_INSN \
b7c7d6c1 312 (HAVE_32BIT_ADDRESSES ? "addu" : "daddu")
f899b4b8
TS
313
314#define ADDRESS_ADDI_INSN \
b7c7d6c1 315 (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu")
f899b4b8
TS
316
317#define ADDRESS_LOAD_INSN \
318 (HAVE_32BIT_ADDRESSES ? "lw" : "ld")
319
320#define ADDRESS_STORE_INSN \
321 (HAVE_32BIT_ADDRESSES ? "sw" : "sd")
322
a4672219 323/* Return true if the given CPU supports the MIPS16 ASE. */
3396de36
TS
324#define CPU_HAS_MIPS16(cpu) \
325 (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0 \
326 || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
a4672219 327
1f25f5d3
CD
328/* Return true if the given CPU supports the MIPS3D ASE. */
329#define CPU_HAS_MIPS3D(cpu) ((cpu) == CPU_SB1 \
330 )
331
deec1734 332/* Return true if the given CPU supports the MDMX ASE. */
b34976b6 333#define CPU_HAS_MDMX(cpu) (FALSE \
deec1734
CD
334 )
335
60b63b72
RS
336/* True if CPU has a dror instruction. */
337#define CPU_HAS_DROR(CPU) ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
338
339/* True if CPU has a ror instruction. */
340#define CPU_HAS_ROR(CPU) CPU_HAS_DROR (CPU)
341
bdaaa2e1 342/* Whether the processor uses hardware interlocks to protect
252b5132 343 reads from the HI and LO registers, and thus does not
ec68c924 344 require nops to be inserted. */
252b5132 345
fef14a42
TS
346#define hilo_interlocks (mips_opts.arch == CPU_R4010 \
347 || mips_opts.arch == CPU_VR5500 \
5a7ea749 348 || mips_opts.arch == CPU_RM7000 \
fef14a42 349 || mips_opts.arch == CPU_SB1 \
252b5132
RH
350 )
351
352/* Whether the processor uses hardware interlocks to protect reads
353 from the GPRs, and thus does not require nops to be inserted. */
354#define gpr_interlocks \
e7af610e 355 (mips_opts.isa != ISA_MIPS1 \
fef14a42
TS
356 || mips_opts.arch == CPU_VR5400 \
357 || mips_opts.arch == CPU_VR5500 \
358 || mips_opts.arch == CPU_R3900)
252b5132
RH
359
360/* As with other "interlocks" this is used by hardware that has FP
361 (co-processor) interlocks. */
bdaaa2e1 362/* Itbl support may require additional care here. */
fef14a42
TS
363#define cop_interlocks (mips_opts.arch == CPU_R4300 \
364 || mips_opts.arch == CPU_VR5400 \
365 || mips_opts.arch == CPU_VR5500 \
366 || mips_opts.arch == CPU_SB1 \
252b5132
RH
367 )
368
6b76fefe
CM
369/* Is this a mfhi or mflo instruction? */
370#define MF_HILO_INSN(PINFO) \
371 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
372
252b5132
RH
373/* MIPS PIC level. */
374
a161fe53 375enum mips_pic_level mips_pic;
252b5132 376
39c0a331
L
377/* Warn about all NOPS that the assembler generates. */
378static int warn_nops = 0;
379
c9914766 380/* 1 if we should generate 32 bit offsets from the $gp register in
252b5132 381 SVR4_PIC mode. Currently has no meaning in other modes. */
c9914766 382static int mips_big_got = 0;
252b5132
RH
383
384/* 1 if trap instructions should used for overflow rather than break
385 instructions. */
c9914766 386static int mips_trap = 0;
252b5132 387
119d663a 388/* 1 if double width floating point constants should not be constructed
b6ff326e 389 by assembling two single width halves into two single width floating
119d663a
NC
390 point registers which just happen to alias the double width destination
391 register. On some architectures this aliasing can be disabled by a bit
d547a75e 392 in the status register, and the setting of this bit cannot be determined
119d663a
NC
393 automatically at assemble time. */
394static int mips_disable_float_construction;
395
252b5132
RH
396/* Non-zero if any .set noreorder directives were used. */
397
398static int mips_any_noreorder;
399
6b76fefe
CM
400/* Non-zero if nops should be inserted when the register referenced in
401 an mfhi/mflo instruction is read in the next two instructions. */
402static int mips_7000_hilo_fix;
403
252b5132 404/* The size of the small data section. */
156c2f8b 405static unsigned int g_switch_value = 8;
252b5132
RH
406/* Whether the -G option was used. */
407static int g_switch_seen = 0;
408
409#define N_RMASK 0xc4
410#define N_VFP 0xd4
411
412/* If we can determine in advance that GP optimization won't be
413 possible, we can skip the relaxation stuff that tries to produce
414 GP-relative references. This makes delay slot optimization work
415 better.
416
417 This function can only provide a guess, but it seems to work for
fba2b7f9
GK
418 gcc output. It needs to guess right for gcc, otherwise gcc
419 will put what it thinks is a GP-relative instruction in a branch
420 delay slot.
252b5132
RH
421
422 I don't know if a fix is needed for the SVR4_PIC mode. I've only
423 fixed it for the non-PIC mode. KR 95/04/07 */
17a2f251 424static int nopic_need_relax (symbolS *, int);
252b5132
RH
425
426/* handle of the OPCODE hash table */
427static struct hash_control *op_hash = NULL;
428
429/* The opcode hash table we use for the mips16. */
430static struct hash_control *mips16_op_hash = NULL;
431
432/* This array holds the chars that always start a comment. If the
433 pre-processor is disabled, these aren't very useful */
434const char comment_chars[] = "#";
435
436/* This array holds the chars that only start a comment at the beginning of
437 a line. If the line seems to have the form '# 123 filename'
438 .line and .file directives will appear in the pre-processed output */
439/* Note that input_file.c hand checks for '#' at the beginning of the
440 first line of the input file. This is because the compiler outputs
bdaaa2e1 441 #NO_APP at the beginning of its output. */
252b5132
RH
442/* Also note that C style comments are always supported. */
443const char line_comment_chars[] = "#";
444
bdaaa2e1 445/* This array holds machine specific line separator characters. */
63a0b638 446const char line_separator_chars[] = ";";
252b5132
RH
447
448/* Chars that can be used to separate mant from exp in floating point nums */
449const char EXP_CHARS[] = "eE";
450
451/* Chars that mean this number is a floating point constant */
452/* As in 0f12.456 */
453/* or 0d1.2345e12 */
454const char FLT_CHARS[] = "rRsSfFdDxXpP";
455
456/* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
457 changed in read.c . Ideally it shouldn't have to know about it at all,
458 but nothing is ideal around here.
459 */
460
461static char *insn_error;
462
463static int auto_align = 1;
464
465/* When outputting SVR4 PIC code, the assembler needs to know the
466 offset in the stack frame from which to restore the $gp register.
467 This is set by the .cprestore pseudo-op, and saved in this
468 variable. */
469static offsetT mips_cprestore_offset = -1;
470
67c1ffbe 471/* Similar for NewABI PIC code, where $gp is callee-saved. NewABI has some
6478892d 472 more optimizations, it can use a register value instead of a memory-saved
956cd1d6 473 offset and even an other register than $gp as global pointer. */
6478892d
TS
474static offsetT mips_cpreturn_offset = -1;
475static int mips_cpreturn_register = -1;
476static int mips_gp_register = GP;
def2e0dd 477static int mips_gprel_offset = 0;
6478892d 478
7a621144
DJ
479/* Whether mips_cprestore_offset has been set in the current function
480 (or whether it has already been warned about, if not). */
481static int mips_cprestore_valid = 0;
482
252b5132
RH
483/* This is the register which holds the stack frame, as set by the
484 .frame pseudo-op. This is needed to implement .cprestore. */
485static int mips_frame_reg = SP;
486
7a621144
DJ
487/* Whether mips_frame_reg has been set in the current function
488 (or whether it has already been warned about, if not). */
489static int mips_frame_reg_valid = 0;
490
252b5132
RH
491/* To output NOP instructions correctly, we need to keep information
492 about the previous two instructions. */
493
494/* Whether we are optimizing. The default value of 2 means to remove
495 unneeded NOPs and swap branch instructions when possible. A value
496 of 1 means to not swap branches. A value of 0 means to always
497 insert NOPs. */
498static int mips_optimize = 2;
499
500/* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
501 equivalent to seeing no -g option at all. */
502static int mips_debug = 0;
503
504/* The previous instruction. */
505static struct mips_cl_insn prev_insn;
506
507/* The instruction before prev_insn. */
508static struct mips_cl_insn prev_prev_insn;
509
510/* If we don't want information for prev_insn or prev_prev_insn, we
511 point the insn_mo field at this dummy integer. */
43841e91 512static const struct mips_opcode dummy_opcode = { NULL, NULL, 0, 0, 0, 0 };
252b5132
RH
513
514/* Non-zero if prev_insn is valid. */
515static int prev_insn_valid;
516
517/* The frag for the previous instruction. */
518static struct frag *prev_insn_frag;
519
520/* The offset into prev_insn_frag for the previous instruction. */
521static long prev_insn_where;
522
523/* The reloc type for the previous instruction, if any. */
f6688943 524static bfd_reloc_code_real_type prev_insn_reloc_type[3];
252b5132
RH
525
526/* The reloc for the previous instruction, if any. */
f6688943 527static fixS *prev_insn_fixp[3];
252b5132
RH
528
529/* Non-zero if the previous instruction was in a delay slot. */
530static int prev_insn_is_delay_slot;
531
532/* Non-zero if the previous instruction was in a .set noreorder. */
533static int prev_insn_unreordered;
534
535/* Non-zero if the previous instruction uses an extend opcode (if
536 mips16). */
537static int prev_insn_extended;
538
539/* Non-zero if the previous previous instruction was in a .set
540 noreorder. */
541static int prev_prev_insn_unreordered;
542
543/* If this is set, it points to a frag holding nop instructions which
544 were inserted before the start of a noreorder section. If those
545 nops turn out to be unnecessary, the size of the frag can be
546 decreased. */
547static fragS *prev_nop_frag;
548
549/* The number of nop instructions we created in prev_nop_frag. */
550static int prev_nop_frag_holds;
551
552/* The number of nop instructions that we know we need in
bdaaa2e1 553 prev_nop_frag. */
252b5132
RH
554static int prev_nop_frag_required;
555
556/* The number of instructions we've seen since prev_nop_frag. */
557static int prev_nop_frag_since;
558
559/* For ECOFF and ELF, relocations against symbols are done in two
560 parts, with a HI relocation and a LO relocation. Each relocation
561 has only 16 bits of space to store an addend. This means that in
562 order for the linker to handle carries correctly, it must be able
563 to locate both the HI and the LO relocation. This means that the
564 relocations must appear in order in the relocation table.
565
566 In order to implement this, we keep track of each unmatched HI
567 relocation. We then sort them so that they immediately precede the
bdaaa2e1 568 corresponding LO relocation. */
252b5132 569
e972090a
NC
570struct mips_hi_fixup
571{
252b5132
RH
572 /* Next HI fixup. */
573 struct mips_hi_fixup *next;
574 /* This fixup. */
575 fixS *fixp;
576 /* The section this fixup is in. */
577 segT seg;
578};
579
580/* The list of unmatched HI relocs. */
581
582static struct mips_hi_fixup *mips_hi_fixup_list;
583
64bdfcaf
RS
584/* The frag containing the last explicit relocation operator.
585 Null if explicit relocations have not been used. */
586
587static fragS *prev_reloc_op_frag;
588
252b5132
RH
589/* Map normal MIPS register numbers to mips16 register numbers. */
590
591#define X ILLEGAL_REG
e972090a
NC
592static const int mips32_to_16_reg_map[] =
593{
252b5132
RH
594 X, X, 2, 3, 4, 5, 6, 7,
595 X, X, X, X, X, X, X, X,
596 0, 1, X, X, X, X, X, X,
597 X, X, X, X, X, X, X, X
598};
599#undef X
600
601/* Map mips16 register numbers to normal MIPS register numbers. */
602
e972090a
NC
603static const unsigned int mips16_to_32_reg_map[] =
604{
252b5132
RH
605 16, 17, 2, 3, 4, 5, 6, 7
606};
60b63b72
RS
607
608static int mips_fix_4122_bugs;
4a6a3df4
AO
609
610/* We don't relax branches by default, since this causes us to expand
611 `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
612 fail to compute the offset before expanding the macro to the most
613 efficient expansion. */
614
615static int mips_relax_branch;
252b5132
RH
616\f
617/* Since the MIPS does not have multiple forms of PC relative
618 instructions, we do not have to do relaxing as is done on other
619 platforms. However, we do have to handle GP relative addressing
620 correctly, which turns out to be a similar problem.
621
622 Every macro that refers to a symbol can occur in (at least) two
623 forms, one with GP relative addressing and one without. For
624 example, loading a global variable into a register generally uses
625 a macro instruction like this:
626 lw $4,i
627 If i can be addressed off the GP register (this is true if it is in
628 the .sbss or .sdata section, or if it is known to be smaller than
629 the -G argument) this will generate the following instruction:
630 lw $4,i($gp)
631 This instruction will use a GPREL reloc. If i can not be addressed
632 off the GP register, the following instruction sequence will be used:
633 lui $at,i
634 lw $4,i($at)
635 In this case the first instruction will have a HI16 reloc, and the
636 second reloc will have a LO16 reloc. Both relocs will be against
637 the symbol i.
638
639 The issue here is that we may not know whether i is GP addressable
640 until after we see the instruction that uses it. Therefore, we
641 want to be able to choose the final instruction sequence only at
642 the end of the assembly. This is similar to the way other
643 platforms choose the size of a PC relative instruction only at the
644 end of assembly.
645
646 When generating position independent code we do not use GP
647 addressing in quite the same way, but the issue still arises as
648 external symbols and local symbols must be handled differently.
649
650 We handle these issues by actually generating both possible
651 instruction sequences. The longer one is put in a frag_var with
652 type rs_machine_dependent. We encode what to do with the frag in
653 the subtype field. We encode (1) the number of existing bytes to
654 replace, (2) the number of new bytes to use, (3) the offset from
655 the start of the existing bytes to the first reloc we must generate
656 (that is, the offset is applied from the start of the existing
657 bytes after they are replaced by the new bytes, if any), (4) the
658 offset from the start of the existing bytes to the second reloc,
659 (5) whether a third reloc is needed (the third reloc is always four
660 bytes after the second reloc), and (6) whether to warn if this
661 variant is used (this is sometimes needed if .set nomacro or .set
662 noat is in effect). All these numbers are reasonably small.
663
664 Generating two instruction sequences must be handled carefully to
665 ensure that delay slots are handled correctly. Fortunately, there
666 are a limited number of cases. When the second instruction
667 sequence is generated, append_insn is directed to maintain the
668 existing delay slot information, so it continues to apply to any
669 code after the second instruction sequence. This means that the
670 second instruction sequence must not impose any requirements not
671 required by the first instruction sequence.
672
673 These variant frags are then handled in functions called by the
674 machine independent code. md_estimate_size_before_relax returns
675 the final size of the frag. md_convert_frag sets up the final form
676 of the frag. tc_gen_reloc adjust the first reloc and adds a second
677 one if needed. */
678#define RELAX_ENCODE(old, new, reloc1, reloc2, reloc3, warn) \
679 ((relax_substateT) \
680 (((old) << 23) \
681 | ((new) << 16) \
682 | (((reloc1) + 64) << 9) \
683 | (((reloc2) + 64) << 2) \
684 | ((reloc3) ? (1 << 1) : 0) \
685 | ((warn) ? 1 : 0)))
686#define RELAX_OLD(i) (((i) >> 23) & 0x7f)
687#define RELAX_NEW(i) (((i) >> 16) & 0x7f)
9a41af64
TS
688#define RELAX_RELOC1(i) ((valueT) (((i) >> 9) & 0x7f) - 64)
689#define RELAX_RELOC2(i) ((valueT) (((i) >> 2) & 0x7f) - 64)
252b5132
RH
690#define RELAX_RELOC3(i) (((i) >> 1) & 1)
691#define RELAX_WARN(i) ((i) & 1)
692
4a6a3df4
AO
693/* Branch without likely bit. If label is out of range, we turn:
694
695 beq reg1, reg2, label
696 delay slot
697
698 into
699
700 bne reg1, reg2, 0f
701 nop
702 j label
703 0: delay slot
704
705 with the following opcode replacements:
706
707 beq <-> bne
708 blez <-> bgtz
709 bltz <-> bgez
710 bc1f <-> bc1t
711
712 bltzal <-> bgezal (with jal label instead of j label)
713
714 Even though keeping the delay slot instruction in the delay slot of
715 the branch would be more efficient, it would be very tricky to do
716 correctly, because we'd have to introduce a variable frag *after*
717 the delay slot instruction, and expand that instead. Let's do it
718 the easy way for now, even if the branch-not-taken case now costs
719 one additional instruction. Out-of-range branches are not supposed
720 to be common, anyway.
721
722 Branch likely. If label is out of range, we turn:
723
724 beql reg1, reg2, label
725 delay slot (annulled if branch not taken)
726
727 into
728
729 beql reg1, reg2, 1f
730 nop
731 beql $0, $0, 2f
732 nop
733 1: j[al] label
734 delay slot (executed only if branch taken)
735 2:
736
737 It would be possible to generate a shorter sequence by losing the
738 likely bit, generating something like:
b34976b6 739
4a6a3df4
AO
740 bne reg1, reg2, 0f
741 nop
742 j[al] label
743 delay slot (executed only if branch taken)
744 0:
745
746 beql -> bne
747 bnel -> beq
748 blezl -> bgtz
749 bgtzl -> blez
750 bltzl -> bgez
751 bgezl -> bltz
752 bc1fl -> bc1t
753 bc1tl -> bc1f
754
755 bltzall -> bgezal (with jal label instead of j label)
756 bgezall -> bltzal (ditto)
757
758
759 but it's not clear that it would actually improve performance. */
af6ae2ad 760#define RELAX_BRANCH_ENCODE(uncond, likely, link, toofar) \
4a6a3df4
AO
761 ((relax_substateT) \
762 (0xc0000000 \
763 | ((toofar) ? 1 : 0) \
764 | ((link) ? 2 : 0) \
765 | ((likely) ? 4 : 0) \
af6ae2ad 766 | ((uncond) ? 8 : 0)))
4a6a3df4 767#define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
4a6a3df4
AO
768#define RELAX_BRANCH_UNCOND(i) (((i) & 8) != 0)
769#define RELAX_BRANCH_LIKELY(i) (((i) & 4) != 0)
770#define RELAX_BRANCH_LINK(i) (((i) & 2) != 0)
ae6063d4 771#define RELAX_BRANCH_TOOFAR(i) (((i) & 1) != 0)
4a6a3df4 772
252b5132
RH
773/* For mips16 code, we use an entirely different form of relaxation.
774 mips16 supports two versions of most instructions which take
775 immediate values: a small one which takes some small value, and a
776 larger one which takes a 16 bit value. Since branches also follow
777 this pattern, relaxing these values is required.
778
779 We can assemble both mips16 and normal MIPS code in a single
780 object. Therefore, we need to support this type of relaxation at
781 the same time that we support the relaxation described above. We
782 use the high bit of the subtype field to distinguish these cases.
783
784 The information we store for this type of relaxation is the
785 argument code found in the opcode file for this relocation, whether
786 the user explicitly requested a small or extended form, and whether
787 the relocation is in a jump or jal delay slot. That tells us the
788 size of the value, and how it should be stored. We also store
789 whether the fragment is considered to be extended or not. We also
790 store whether this is known to be a branch to a different section,
791 whether we have tried to relax this frag yet, and whether we have
792 ever extended a PC relative fragment because of a shift count. */
793#define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
794 (0x80000000 \
795 | ((type) & 0xff) \
796 | ((small) ? 0x100 : 0) \
797 | ((ext) ? 0x200 : 0) \
798 | ((dslot) ? 0x400 : 0) \
799 | ((jal_dslot) ? 0x800 : 0))
4a6a3df4 800#define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
252b5132
RH
801#define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
802#define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
803#define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
804#define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
805#define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
806#define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
807#define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
808#define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
809#define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
810#define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
811#define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
885add95
CD
812
813/* Is the given value a sign-extended 32-bit value? */
814#define IS_SEXT_32BIT_NUM(x) \
815 (((x) &~ (offsetT) 0x7fffffff) == 0 \
816 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
817
818/* Is the given value a sign-extended 16-bit value? */
819#define IS_SEXT_16BIT_NUM(x) \
820 (((x) &~ (offsetT) 0x7fff) == 0 \
821 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
822
252b5132
RH
823\f
824/* Prototypes for static functions. */
825
17a2f251 826#define internalError() \
252b5132 827 as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
252b5132
RH
828
829enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
830
b34976b6 831static void append_insn
17a2f251
TS
832 (char *place, struct mips_cl_insn *ip, expressionS *p,
833 bfd_reloc_code_real_type *r);
834static void mips_no_prev_insn (int);
b34976b6 835static void mips16_macro_build
17a2f251
TS
836 (char *, int *, expressionS *, const char *, const char *, va_list);
837static void load_register (int *, int, expressionS *, int);
838static void macro (struct mips_cl_insn * ip);
839static void mips16_macro (struct mips_cl_insn * ip);
252b5132 840#ifdef LOSING_COMPILER
17a2f251 841static void macro2 (struct mips_cl_insn * ip);
252b5132 842#endif
17a2f251
TS
843static void mips_ip (char *str, struct mips_cl_insn * ip);
844static void mips16_ip (char *str, struct mips_cl_insn * ip);
b34976b6 845static void mips16_immed
17a2f251
TS
846 (char *, unsigned int, int, offsetT, bfd_boolean, bfd_boolean, bfd_boolean,
847 unsigned long *, bfd_boolean *, unsigned short *);
5e0116d5 848static size_t my_getSmallExpression
17a2f251
TS
849 (expressionS *, bfd_reloc_code_real_type *, char *);
850static void my_getExpression (expressionS *, char *);
851static void s_align (int);
852static void s_change_sec (int);
853static void s_change_section (int);
854static void s_cons (int);
855static void s_float_cons (int);
856static void s_mips_globl (int);
857static void s_option (int);
858static void s_mipsset (int);
859static void s_abicalls (int);
860static void s_cpload (int);
861static void s_cpsetup (int);
862static void s_cplocal (int);
863static void s_cprestore (int);
864static void s_cpreturn (int);
865static void s_gpvalue (int);
866static void s_gpword (int);
867static void s_gpdword (int);
868static void s_cpadd (int);
869static void s_insn (int);
870static void md_obj_begin (void);
871static void md_obj_end (void);
872static void s_mips_ent (int);
873static void s_mips_end (int);
874static void s_mips_frame (int);
875static void s_mips_mask (int reg_type);
876static void s_mips_stab (int);
877static void s_mips_weakext (int);
878static void s_mips_file (int);
879static void s_mips_loc (int);
880static bfd_boolean pic_need_relax (symbolS *, asection *);
4a6a3df4 881static int relaxed_branch_length (fragS *, asection *, int);
17a2f251 882static int validate_mips_insn (const struct mips_opcode *);
e7af610e
NC
883
884/* Table and functions used to map between CPU/ISA names, and
885 ISA levels, and CPU numbers. */
886
e972090a
NC
887struct mips_cpu_info
888{
e7af610e
NC
889 const char *name; /* CPU or ISA name. */
890 int is_isa; /* Is this an ISA? (If 0, a CPU.) */
891 int isa; /* ISA level. */
892 int cpu; /* CPU number (default CPU if ISA). */
893};
894
17a2f251
TS
895static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
896static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
897static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
252b5132
RH
898\f
899/* Pseudo-op table.
900
901 The following pseudo-ops from the Kane and Heinrich MIPS book
902 should be defined here, but are currently unsupported: .alias,
903 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
904
905 The following pseudo-ops from the Kane and Heinrich MIPS book are
906 specific to the type of debugging information being generated, and
907 should be defined by the object format: .aent, .begin, .bend,
908 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
909 .vreg.
910
911 The following pseudo-ops from the Kane and Heinrich MIPS book are
912 not MIPS CPU specific, but are also not specific to the object file
913 format. This file is probably the best place to define them, but
914 they are not currently supported: .asm0, .endr, .lab, .repeat,
915 .struct. */
916
e972090a
NC
917static const pseudo_typeS mips_pseudo_table[] =
918{
beae10d5 919 /* MIPS specific pseudo-ops. */
252b5132
RH
920 {"option", s_option, 0},
921 {"set", s_mipsset, 0},
922 {"rdata", s_change_sec, 'r'},
923 {"sdata", s_change_sec, 's'},
924 {"livereg", s_ignore, 0},
925 {"abicalls", s_abicalls, 0},
926 {"cpload", s_cpload, 0},
6478892d
TS
927 {"cpsetup", s_cpsetup, 0},
928 {"cplocal", s_cplocal, 0},
252b5132 929 {"cprestore", s_cprestore, 0},
6478892d
TS
930 {"cpreturn", s_cpreturn, 0},
931 {"gpvalue", s_gpvalue, 0},
252b5132 932 {"gpword", s_gpword, 0},
10181a0d 933 {"gpdword", s_gpdword, 0},
252b5132
RH
934 {"cpadd", s_cpadd, 0},
935 {"insn", s_insn, 0},
936
beae10d5 937 /* Relatively generic pseudo-ops that happen to be used on MIPS
252b5132
RH
938 chips. */
939 {"asciiz", stringer, 1},
940 {"bss", s_change_sec, 'b'},
941 {"err", s_err, 0},
942 {"half", s_cons, 1},
943 {"dword", s_cons, 3},
944 {"weakext", s_mips_weakext, 0},
945
beae10d5 946 /* These pseudo-ops are defined in read.c, but must be overridden
252b5132
RH
947 here for one reason or another. */
948 {"align", s_align, 0},
949 {"byte", s_cons, 0},
950 {"data", s_change_sec, 'd'},
951 {"double", s_float_cons, 'd'},
952 {"float", s_float_cons, 'f'},
953 {"globl", s_mips_globl, 0},
954 {"global", s_mips_globl, 0},
955 {"hword", s_cons, 1},
956 {"int", s_cons, 2},
957 {"long", s_cons, 2},
958 {"octa", s_cons, 4},
959 {"quad", s_cons, 3},
cca86cc8 960 {"section", s_change_section, 0},
252b5132
RH
961 {"short", s_cons, 1},
962 {"single", s_float_cons, 'f'},
963 {"stabn", s_mips_stab, 'n'},
964 {"text", s_change_sec, 't'},
965 {"word", s_cons, 2},
add56521 966
add56521 967 { "extern", ecoff_directive_extern, 0},
add56521 968
43841e91 969 { NULL, NULL, 0 },
252b5132
RH
970};
971
e972090a
NC
972static const pseudo_typeS mips_nonecoff_pseudo_table[] =
973{
beae10d5
KH
974 /* These pseudo-ops should be defined by the object file format.
975 However, a.out doesn't support them, so we have versions here. */
252b5132
RH
976 {"aent", s_mips_ent, 1},
977 {"bgnb", s_ignore, 0},
978 {"end", s_mips_end, 0},
979 {"endb", s_ignore, 0},
980 {"ent", s_mips_ent, 0},
c5dd6aab 981 {"file", s_mips_file, 0},
252b5132
RH
982 {"fmask", s_mips_mask, 'F'},
983 {"frame", s_mips_frame, 0},
c5dd6aab 984 {"loc", s_mips_loc, 0},
252b5132
RH
985 {"mask", s_mips_mask, 'R'},
986 {"verstamp", s_ignore, 0},
43841e91 987 { NULL, NULL, 0 },
252b5132
RH
988};
989
17a2f251 990extern void pop_insert (const pseudo_typeS *);
252b5132
RH
991
992void
17a2f251 993mips_pop_insert (void)
252b5132
RH
994{
995 pop_insert (mips_pseudo_table);
996 if (! ECOFF_DEBUGGING)
997 pop_insert (mips_nonecoff_pseudo_table);
998}
999\f
1000/* Symbols labelling the current insn. */
1001
e972090a
NC
1002struct insn_label_list
1003{
252b5132
RH
1004 struct insn_label_list *next;
1005 symbolS *label;
1006};
1007
1008static struct insn_label_list *insn_labels;
1009static struct insn_label_list *free_insn_labels;
1010
17a2f251 1011static void mips_clear_insn_labels (void);
252b5132
RH
1012
1013static inline void
17a2f251 1014mips_clear_insn_labels (void)
252b5132
RH
1015{
1016 register struct insn_label_list **pl;
1017
1018 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1019 ;
1020 *pl = insn_labels;
1021 insn_labels = NULL;
1022}
1023\f
1024static char *expr_end;
1025
1026/* Expressions which appear in instructions. These are set by
1027 mips_ip. */
1028
1029static expressionS imm_expr;
5f74bc13 1030static expressionS imm2_expr;
252b5132
RH
1031static expressionS offset_expr;
1032
1033/* Relocs associated with imm_expr and offset_expr. */
1034
f6688943
TS
1035static bfd_reloc_code_real_type imm_reloc[3]
1036 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1037static bfd_reloc_code_real_type offset_reloc[3]
1038 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 1039
252b5132
RH
1040/* These are set by mips16_ip if an explicit extension is used. */
1041
b34976b6 1042static bfd_boolean mips16_small, mips16_ext;
252b5132 1043
7ed4a06a 1044#ifdef OBJ_ELF
ecb4347a
DJ
1045/* The pdr segment for per procedure frame/regmask info. Not used for
1046 ECOFF debugging. */
252b5132
RH
1047
1048static segT pdr_seg;
7ed4a06a 1049#endif
252b5132 1050
e013f690
TS
1051/* The default target format to use. */
1052
1053const char *
17a2f251 1054mips_target_format (void)
e013f690
TS
1055{
1056 switch (OUTPUT_FLAVOR)
1057 {
1058 case bfd_target_aout_flavour:
1059 return target_big_endian ? "a.out-mips-big" : "a.out-mips-little";
1060 case bfd_target_ecoff_flavour:
1061 return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
1062 case bfd_target_coff_flavour:
1063 return "pe-mips";
1064 case bfd_target_elf_flavour:
1065#ifdef TE_TMIPS
cfe86eaa 1066 /* This is traditional mips. */
e013f690 1067 return (target_big_endian
cfe86eaa
TS
1068 ? (HAVE_64BIT_OBJECTS
1069 ? "elf64-tradbigmips"
1070 : (HAVE_NEWABI
1071 ? "elf32-ntradbigmips" : "elf32-tradbigmips"))
1072 : (HAVE_64BIT_OBJECTS
1073 ? "elf64-tradlittlemips"
1074 : (HAVE_NEWABI
1075 ? "elf32-ntradlittlemips" : "elf32-tradlittlemips")));
e013f690
TS
1076#else
1077 return (target_big_endian
cfe86eaa
TS
1078 ? (HAVE_64BIT_OBJECTS
1079 ? "elf64-bigmips"
1080 : (HAVE_NEWABI
1081 ? "elf32-nbigmips" : "elf32-bigmips"))
1082 : (HAVE_64BIT_OBJECTS
1083 ? "elf64-littlemips"
1084 : (HAVE_NEWABI
1085 ? "elf32-nlittlemips" : "elf32-littlemips")));
e013f690
TS
1086#endif
1087 default:
1088 abort ();
1089 return NULL;
1090 }
1091}
1092
156c2f8b
NC
1093/* This function is called once, at assembler startup time. It should
1094 set up all the tables, etc. that the MD part of the assembler will need. */
1095
252b5132 1096void
17a2f251 1097md_begin (void)
252b5132 1098{
252b5132 1099 register const char *retval = NULL;
156c2f8b 1100 int i = 0;
252b5132 1101 int broken = 0;
1f25f5d3 1102
fef14a42 1103 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_arch))
252b5132
RH
1104 as_warn (_("Could not set architecture and machine"));
1105
252b5132
RH
1106 op_hash = hash_new ();
1107
1108 for (i = 0; i < NUMOPCODES;)
1109 {
1110 const char *name = mips_opcodes[i].name;
1111
17a2f251 1112 retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
252b5132
RH
1113 if (retval != NULL)
1114 {
1115 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1116 mips_opcodes[i].name, retval);
1117 /* Probably a memory allocation problem? Give up now. */
1118 as_fatal (_("Broken assembler. No assembly attempted."));
1119 }
1120 do
1121 {
1122 if (mips_opcodes[i].pinfo != INSN_MACRO)
1123 {
1124 if (!validate_mips_insn (&mips_opcodes[i]))
1125 broken = 1;
1126 }
1127 ++i;
1128 }
1129 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1130 }
1131
1132 mips16_op_hash = hash_new ();
1133
1134 i = 0;
1135 while (i < bfd_mips16_num_opcodes)
1136 {
1137 const char *name = mips16_opcodes[i].name;
1138
17a2f251 1139 retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
252b5132
RH
1140 if (retval != NULL)
1141 as_fatal (_("internal: can't hash `%s': %s"),
1142 mips16_opcodes[i].name, retval);
1143 do
1144 {
1145 if (mips16_opcodes[i].pinfo != INSN_MACRO
1146 && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1147 != mips16_opcodes[i].match))
1148 {
1149 fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1150 mips16_opcodes[i].name, mips16_opcodes[i].args);
1151 broken = 1;
1152 }
1153 ++i;
1154 }
1155 while (i < bfd_mips16_num_opcodes
1156 && strcmp (mips16_opcodes[i].name, name) == 0);
1157 }
1158
1159 if (broken)
1160 as_fatal (_("Broken assembler. No assembly attempted."));
1161
1162 /* We add all the general register names to the symbol table. This
1163 helps us detect invalid uses of them. */
1164 for (i = 0; i < 32; i++)
1165 {
1166 char buf[5];
1167
1168 sprintf (buf, "$%d", i);
1169 symbol_table_insert (symbol_new (buf, reg_section, i,
1170 &zero_address_frag));
1171 }
76db943d
TS
1172 symbol_table_insert (symbol_new ("$ra", reg_section, RA,
1173 &zero_address_frag));
252b5132
RH
1174 symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1175 &zero_address_frag));
1176 symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1177 &zero_address_frag));
1178 symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1179 &zero_address_frag));
1180 symbol_table_insert (symbol_new ("$at", reg_section, AT,
1181 &zero_address_frag));
1182 symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1183 &zero_address_frag));
1184 symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1185 &zero_address_frag));
85b51719
TS
1186 symbol_table_insert (symbol_new ("$zero", reg_section, ZERO,
1187 &zero_address_frag));
252b5132
RH
1188 symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1189 &zero_address_frag));
1190
6047c971
AO
1191 /* If we don't add these register names to the symbol table, they
1192 may end up being added as regular symbols by operand(), and then
1193 make it to the object file as undefined in case they're not
1194 regarded as local symbols. They're local in o32, since `$' is a
1195 local symbol prefix, but not in n32 or n64. */
1196 for (i = 0; i < 8; i++)
1197 {
1198 char buf[6];
1199
1200 sprintf (buf, "$fcc%i", i);
1201 symbol_table_insert (symbol_new (buf, reg_section, -1,
1202 &zero_address_frag));
1203 }
1204
b34976b6 1205 mips_no_prev_insn (FALSE);
252b5132
RH
1206
1207 mips_gprmask = 0;
1208 mips_cprmask[0] = 0;
1209 mips_cprmask[1] = 0;
1210 mips_cprmask[2] = 0;
1211 mips_cprmask[3] = 0;
1212
1213 /* set the default alignment for the text section (2**2) */
1214 record_alignment (text_section, 2);
1215
1216 if (USE_GLOBAL_POINTER_OPT)
1217 bfd_set_gp_size (stdoutput, g_switch_value);
1218
1219 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1220 {
1221 /* On a native system, sections must be aligned to 16 byte
1222 boundaries. When configured for an embedded ELF target, we
1223 don't bother. */
1224 if (strcmp (TARGET_OS, "elf") != 0)
1225 {
1226 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1227 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1228 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1229 }
1230
1231 /* Create a .reginfo section for register masks and a .mdebug
1232 section for debugging information. */
1233 {
1234 segT seg;
1235 subsegT subseg;
1236 flagword flags;
1237 segT sec;
1238
1239 seg = now_seg;
1240 subseg = now_subseg;
1241
1242 /* The ABI says this section should be loaded so that the
1243 running program can access it. However, we don't load it
1244 if we are configured for an embedded target */
1245 flags = SEC_READONLY | SEC_DATA;
1246 if (strcmp (TARGET_OS, "elf") != 0)
1247 flags |= SEC_ALLOC | SEC_LOAD;
1248
316f5878 1249 if (mips_abi != N64_ABI)
252b5132
RH
1250 {
1251 sec = subseg_new (".reginfo", (subsegT) 0);
1252
195325d2
TS
1253 bfd_set_section_flags (stdoutput, sec, flags);
1254 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
bdaaa2e1 1255
252b5132
RH
1256#ifdef OBJ_ELF
1257 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1258#endif
1259 }
1260 else
1261 {
1262 /* The 64-bit ABI uses a .MIPS.options section rather than
1263 .reginfo section. */
1264 sec = subseg_new (".MIPS.options", (subsegT) 0);
195325d2
TS
1265 bfd_set_section_flags (stdoutput, sec, flags);
1266 bfd_set_section_alignment (stdoutput, sec, 3);
252b5132
RH
1267
1268#ifdef OBJ_ELF
1269 /* Set up the option header. */
1270 {
1271 Elf_Internal_Options opthdr;
1272 char *f;
1273
1274 opthdr.kind = ODK_REGINFO;
1275 opthdr.size = (sizeof (Elf_External_Options)
1276 + sizeof (Elf64_External_RegInfo));
1277 opthdr.section = 0;
1278 opthdr.info = 0;
1279 f = frag_more (sizeof (Elf_External_Options));
1280 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1281 (Elf_External_Options *) f);
1282
1283 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1284 }
1285#endif
1286 }
1287
1288 if (ECOFF_DEBUGGING)
1289 {
1290 sec = subseg_new (".mdebug", (subsegT) 0);
1291 (void) bfd_set_section_flags (stdoutput, sec,
1292 SEC_HAS_CONTENTS | SEC_READONLY);
1293 (void) bfd_set_section_alignment (stdoutput, sec, 2);
1294 }
ecb4347a 1295#ifdef OBJ_ELF
dcd410fe 1296 else if (OUTPUT_FLAVOR == bfd_target_elf_flavour && mips_flag_pdr)
ecb4347a
DJ
1297 {
1298 pdr_seg = subseg_new (".pdr", (subsegT) 0);
1299 (void) bfd_set_section_flags (stdoutput, pdr_seg,
1300 SEC_READONLY | SEC_RELOC
1301 | SEC_DEBUGGING);
1302 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1303 }
252b5132
RH
1304#endif
1305
1306 subseg_set (seg, subseg);
1307 }
1308 }
1309
1310 if (! ECOFF_DEBUGGING)
1311 md_obj_begin ();
1312}
1313
1314void
17a2f251 1315md_mips_end (void)
252b5132
RH
1316{
1317 if (! ECOFF_DEBUGGING)
1318 md_obj_end ();
1319}
1320
1321void
17a2f251 1322md_assemble (char *str)
252b5132
RH
1323{
1324 struct mips_cl_insn insn;
f6688943
TS
1325 bfd_reloc_code_real_type unused_reloc[3]
1326 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132
RH
1327
1328 imm_expr.X_op = O_absent;
5f74bc13 1329 imm2_expr.X_op = O_absent;
252b5132 1330 offset_expr.X_op = O_absent;
f6688943
TS
1331 imm_reloc[0] = BFD_RELOC_UNUSED;
1332 imm_reloc[1] = BFD_RELOC_UNUSED;
1333 imm_reloc[2] = BFD_RELOC_UNUSED;
1334 offset_reloc[0] = BFD_RELOC_UNUSED;
1335 offset_reloc[1] = BFD_RELOC_UNUSED;
1336 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
1337
1338 if (mips_opts.mips16)
1339 mips16_ip (str, &insn);
1340 else
1341 {
1342 mips_ip (str, &insn);
beae10d5
KH
1343 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1344 str, insn.insn_opcode));
252b5132
RH
1345 }
1346
1347 if (insn_error)
1348 {
1349 as_bad ("%s `%s'", insn_error, str);
1350 return;
1351 }
1352
1353 if (insn.insn_mo->pinfo == INSN_MACRO)
1354 {
1355 if (mips_opts.mips16)
1356 mips16_macro (&insn);
1357 else
1358 macro (&insn);
1359 }
1360 else
1361 {
1362 if (imm_expr.X_op != O_absent)
5e0116d5 1363 append_insn (NULL, &insn, &imm_expr, imm_reloc);
252b5132 1364 else if (offset_expr.X_op != O_absent)
5e0116d5 1365 append_insn (NULL, &insn, &offset_expr, offset_reloc);
252b5132 1366 else
5e0116d5 1367 append_insn (NULL, &insn, NULL, unused_reloc);
252b5132
RH
1368 }
1369}
1370
5919d012
RS
1371/* Return true if the given relocation might need a matching %lo().
1372 Note that R_MIPS_GOT16 relocations only need a matching %lo() when
1373 applied to local symbols. */
1374
1375static inline bfd_boolean
17a2f251 1376reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
5919d012
RS
1377{
1378 return (reloc == BFD_RELOC_HI16_S
1379 || reloc == BFD_RELOC_MIPS_GOT16);
1380}
1381
1382/* Return true if the given fixup is followed by a matching R_MIPS_LO16
1383 relocation. */
1384
1385static inline bfd_boolean
17a2f251 1386fixup_has_matching_lo_p (fixS *fixp)
5919d012
RS
1387{
1388 return (fixp->fx_next != NULL
1389 && fixp->fx_next->fx_r_type == BFD_RELOC_LO16
1390 && fixp->fx_addsy == fixp->fx_next->fx_addsy
1391 && fixp->fx_offset == fixp->fx_next->fx_offset);
1392}
1393
252b5132
RH
1394/* See whether instruction IP reads register REG. CLASS is the type
1395 of register. */
1396
1397static int
17a2f251
TS
1398insn_uses_reg (struct mips_cl_insn *ip, unsigned int reg,
1399 enum mips_regclass class)
252b5132
RH
1400{
1401 if (class == MIPS16_REG)
1402 {
1403 assert (mips_opts.mips16);
1404 reg = mips16_to_32_reg_map[reg];
1405 class = MIPS_GR_REG;
1406 }
1407
85b51719
TS
1408 /* Don't report on general register ZERO, since it never changes. */
1409 if (class == MIPS_GR_REG && reg == ZERO)
252b5132
RH
1410 return 0;
1411
1412 if (class == MIPS_FP_REG)
1413 {
1414 assert (! mips_opts.mips16);
1415 /* If we are called with either $f0 or $f1, we must check $f0.
1416 This is not optimal, because it will introduce an unnecessary
1417 NOP between "lwc1 $f0" and "swc1 $f1". To fix this we would
1418 need to distinguish reading both $f0 and $f1 or just one of
1419 them. Note that we don't have to check the other way,
1420 because there is no instruction that sets both $f0 and $f1
1421 and requires a delay. */
1422 if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1423 && ((((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS) &~(unsigned)1)
1424 == (reg &~ (unsigned) 1)))
1425 return 1;
1426 if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1427 && ((((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT) &~(unsigned)1)
1428 == (reg &~ (unsigned) 1)))
1429 return 1;
1430 }
1431 else if (! mips_opts.mips16)
1432 {
1433 if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1434 && ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == reg)
1435 return 1;
1436 if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1437 && ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT) == reg)
1438 return 1;
1439 }
1440 else
1441 {
1442 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1443 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RX)
1444 & MIPS16OP_MASK_RX)]
1445 == reg))
1446 return 1;
1447 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1448 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RY)
1449 & MIPS16OP_MASK_RY)]
1450 == reg))
1451 return 1;
1452 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1453 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
1454 & MIPS16OP_MASK_MOVE32Z)]
1455 == reg))
1456 return 1;
1457 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1458 return 1;
1459 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1460 return 1;
1461 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1462 return 1;
1463 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1464 && ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
1465 & MIPS16OP_MASK_REGR32) == reg)
1466 return 1;
1467 }
1468
1469 return 0;
1470}
1471
1472/* This function returns true if modifying a register requires a
1473 delay. */
1474
1475static int
17a2f251 1476reg_needs_delay (unsigned int reg)
252b5132
RH
1477{
1478 unsigned long prev_pinfo;
1479
1480 prev_pinfo = prev_insn.insn_mo->pinfo;
1481 if (! mips_opts.noreorder
9ce8a5dd 1482 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
1483 && ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1484 || (! gpr_interlocks
1485 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1486 {
1487 /* A load from a coprocessor or from memory. All load
1488 delays delay the use of general register rt for one
1489 instruction on the r3000. The r6000 and r4000 use
1490 interlocks. */
bdaaa2e1 1491 /* Itbl support may require additional care here. */
252b5132
RH
1492 know (prev_pinfo & INSN_WRITE_GPR_T);
1493 if (reg == ((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT))
1494 return 1;
1495 }
1496
1497 return 0;
1498}
1499
1500/* Mark instruction labels in mips16 mode. This permits the linker to
1501 handle them specially, such as generating jalx instructions when
1502 needed. We also make them odd for the duration of the assembly, in
1503 order to generate the right sort of code. We will make them even
1504 in the adjust_symtab routine, while leaving them marked. This is
1505 convenient for the debugger and the disassembler. The linker knows
1506 to make them odd again. */
1507
1508static void
17a2f251 1509mips16_mark_labels (void)
252b5132
RH
1510{
1511 if (mips_opts.mips16)
1512 {
1513 struct insn_label_list *l;
98aa84af 1514 valueT val;
252b5132
RH
1515
1516 for (l = insn_labels; l != NULL; l = l->next)
1517 {
1518#ifdef OBJ_ELF
1519 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1520 S_SET_OTHER (l->label, STO_MIPS16);
1521#endif
98aa84af
AM
1522 val = S_GET_VALUE (l->label);
1523 if ((val & 1) == 0)
1524 S_SET_VALUE (l->label, val + 1);
252b5132
RH
1525 }
1526 }
1527}
1528
1529/* Output an instruction. PLACE is where to put the instruction; if
1530 it is NULL, this uses frag_more to get room. IP is the instruction
1531 information. ADDRESS_EXPR is an operand of the instruction to be
1532 used with RELOC_TYPE. */
1533
1534static void
17a2f251
TS
1535append_insn (char *place, struct mips_cl_insn *ip, expressionS *address_expr,
1536 bfd_reloc_code_real_type *reloc_type)
252b5132
RH
1537{
1538 register unsigned long prev_pinfo, pinfo;
1539 char *f;
f6688943 1540 fixS *fixp[3];
252b5132 1541 int nops = 0;
f5040a92 1542 bfd_boolean force_new_frag = FALSE;
252b5132
RH
1543
1544 /* Mark instruction labels in mips16 mode. */
f9419b05 1545 mips16_mark_labels ();
252b5132
RH
1546
1547 prev_pinfo = prev_insn.insn_mo->pinfo;
1548 pinfo = ip->insn_mo->pinfo;
1549
1550 if (place == NULL && (! mips_opts.noreorder || prev_nop_frag != NULL))
1551 {
1552 int prev_prev_nop;
1553
1554 /* If the previous insn required any delay slots, see if we need
1555 to insert a NOP or two. There are eight kinds of possible
1556 hazards, of which an instruction can have at most one type.
1557 (1) a load from memory delay
1558 (2) a load from a coprocessor delay
1559 (3) an unconditional branch delay
1560 (4) a conditional branch delay
1561 (5) a move to coprocessor register delay
1562 (6) a load coprocessor register from memory delay
1563 (7) a coprocessor condition code delay
1564 (8) a HI/LO special register delay
1565
1566 There are a lot of optimizations we could do that we don't.
1567 In particular, we do not, in general, reorder instructions.
1568 If you use gcc with optimization, it will reorder
1569 instructions and generally do much more optimization then we
1570 do here; repeating all that work in the assembler would only
1571 benefit hand written assembly code, and does not seem worth
1572 it. */
1573
1574 /* This is how a NOP is emitted. */
1575#define emit_nop() \
1576 (mips_opts.mips16 \
1577 ? md_number_to_chars (frag_more (2), 0x6500, 2) \
1578 : md_number_to_chars (frag_more (4), 0, 4))
1579
1580 /* The previous insn might require a delay slot, depending upon
1581 the contents of the current insn. */
1582 if (! mips_opts.mips16
9ce8a5dd 1583 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
1584 && (((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1585 && ! cop_interlocks)
1586 || (! gpr_interlocks
1587 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1588 {
1589 /* A load from a coprocessor or from memory. All load
1590 delays delay the use of general register rt for one
1591 instruction on the r3000. The r6000 and r4000 use
1592 interlocks. */
beae10d5 1593 /* Itbl support may require additional care here. */
252b5132
RH
1594 know (prev_pinfo & INSN_WRITE_GPR_T);
1595 if (mips_optimize == 0
1596 || insn_uses_reg (ip,
1597 ((prev_insn.insn_opcode >> OP_SH_RT)
1598 & OP_MASK_RT),
1599 MIPS_GR_REG))
1600 ++nops;
1601 }
1602 else if (! mips_opts.mips16
9ce8a5dd 1603 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132 1604 && (((prev_pinfo & INSN_COPROC_MOVE_DELAY)
beae10d5 1605 && ! cop_interlocks)
e7af610e 1606 || (mips_opts.isa == ISA_MIPS1
252b5132
RH
1607 && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))))
1608 {
1609 /* A generic coprocessor delay. The previous instruction
1610 modified a coprocessor general or control register. If
1611 it modified a control register, we need to avoid any
1612 coprocessor instruction (this is probably not always
1613 required, but it sometimes is). If it modified a general
1614 register, we avoid using that register.
1615
1616 On the r6000 and r4000 loading a coprocessor register
1617 from memory is interlocked, and does not require a delay.
1618
1619 This case is not handled very well. There is no special
1620 knowledge of CP0 handling, and the coprocessors other
1621 than the floating point unit are not distinguished at
1622 all. */
1623 /* Itbl support may require additional care here. FIXME!
bdaaa2e1 1624 Need to modify this to include knowledge about
252b5132
RH
1625 user specified delays! */
1626 if (prev_pinfo & INSN_WRITE_FPR_T)
1627 {
1628 if (mips_optimize == 0
1629 || insn_uses_reg (ip,
1630 ((prev_insn.insn_opcode >> OP_SH_FT)
1631 & OP_MASK_FT),
1632 MIPS_FP_REG))
1633 ++nops;
1634 }
1635 else if (prev_pinfo & INSN_WRITE_FPR_S)
1636 {
1637 if (mips_optimize == 0
1638 || insn_uses_reg (ip,
1639 ((prev_insn.insn_opcode >> OP_SH_FS)
1640 & OP_MASK_FS),
1641 MIPS_FP_REG))
1642 ++nops;
1643 }
1644 else
1645 {
1646 /* We don't know exactly what the previous instruction
1647 does. If the current instruction uses a coprocessor
1648 register, we must insert a NOP. If previous
1649 instruction may set the condition codes, and the
1650 current instruction uses them, we must insert two
1651 NOPS. */
bdaaa2e1 1652 /* Itbl support may require additional care here. */
252b5132
RH
1653 if (mips_optimize == 0
1654 || ((prev_pinfo & INSN_WRITE_COND_CODE)
1655 && (pinfo & INSN_READ_COND_CODE)))
1656 nops += 2;
1657 else if (pinfo & INSN_COP)
1658 ++nops;
1659 }
1660 }
1661 else if (! mips_opts.mips16
9ce8a5dd 1662 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
1663 && (prev_pinfo & INSN_WRITE_COND_CODE)
1664 && ! cop_interlocks)
1665 {
1666 /* The previous instruction sets the coprocessor condition
1667 codes, but does not require a general coprocessor delay
1668 (this means it is a floating point comparison
1669 instruction). If this instruction uses the condition
1670 codes, we need to insert a single NOP. */
beae10d5 1671 /* Itbl support may require additional care here. */
252b5132
RH
1672 if (mips_optimize == 0
1673 || (pinfo & INSN_READ_COND_CODE))
1674 ++nops;
1675 }
6b76fefe
CM
1676
1677 /* If we're fixing up mfhi/mflo for the r7000 and the
1678 previous insn was an mfhi/mflo and the current insn
1679 reads the register that the mfhi/mflo wrote to, then
1680 insert two nops. */
1681
1682 else if (mips_7000_hilo_fix
1683 && MF_HILO_INSN (prev_pinfo)
1684 && insn_uses_reg (ip, ((prev_insn.insn_opcode >> OP_SH_RD)
beae10d5
KH
1685 & OP_MASK_RD),
1686 MIPS_GR_REG))
6b76fefe
CM
1687 {
1688 nops += 2;
1689 }
1690
1691 /* If we're fixing up mfhi/mflo for the r7000 and the
1692 2nd previous insn was an mfhi/mflo and the current insn
1693 reads the register that the mfhi/mflo wrote to, then
1694 insert one nop. */
1695
1696 else if (mips_7000_hilo_fix
1697 && MF_HILO_INSN (prev_prev_insn.insn_opcode)
1698 && insn_uses_reg (ip, ((prev_prev_insn.insn_opcode >> OP_SH_RD)
1699 & OP_MASK_RD),
1700 MIPS_GR_REG))
bdaaa2e1 1701
6b76fefe 1702 {
f9419b05 1703 ++nops;
6b76fefe 1704 }
bdaaa2e1 1705
252b5132
RH
1706 else if (prev_pinfo & INSN_READ_LO)
1707 {
1708 /* The previous instruction reads the LO register; if the
1709 current instruction writes to the LO register, we must
bdaaa2e1 1710 insert two NOPS. Some newer processors have interlocks.
67c1ffbe
KH
1711 Also the tx39's multiply instructions can be executed
1712 immediately after a read from HI/LO (without the delay),
bdaaa2e1
KH
1713 though the tx39's divide insns still do require the
1714 delay. */
252b5132 1715 if (! (hilo_interlocks
ec68c924 1716 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
252b5132
RH
1717 && (mips_optimize == 0
1718 || (pinfo & INSN_WRITE_LO)))
1719 nops += 2;
1720 /* Most mips16 branch insns don't have a delay slot.
1721 If a read from LO is immediately followed by a branch
1722 to a write to LO we have a read followed by a write
1723 less than 2 insns away. We assume the target of
1724 a branch might be a write to LO, and insert a nop
bdaaa2e1 1725 between a read and an immediately following branch. */
252b5132
RH
1726 else if (mips_opts.mips16
1727 && (mips_optimize == 0
1728 || (pinfo & MIPS16_INSN_BRANCH)))
f9419b05 1729 ++nops;
252b5132
RH
1730 }
1731 else if (prev_insn.insn_mo->pinfo & INSN_READ_HI)
1732 {
1733 /* The previous instruction reads the HI register; if the
1734 current instruction writes to the HI register, we must
1735 insert a NOP. Some newer processors have interlocks.
bdaaa2e1 1736 Also the note tx39's multiply above. */
252b5132 1737 if (! (hilo_interlocks
ec68c924 1738 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
252b5132
RH
1739 && (mips_optimize == 0
1740 || (pinfo & INSN_WRITE_HI)))
1741 nops += 2;
1742 /* Most mips16 branch insns don't have a delay slot.
1743 If a read from HI is immediately followed by a branch
1744 to a write to HI we have a read followed by a write
1745 less than 2 insns away. We assume the target of
1746 a branch might be a write to HI, and insert a nop
bdaaa2e1 1747 between a read and an immediately following branch. */
252b5132
RH
1748 else if (mips_opts.mips16
1749 && (mips_optimize == 0
1750 || (pinfo & MIPS16_INSN_BRANCH)))
f9419b05 1751 ++nops;
252b5132
RH
1752 }
1753
1754 /* If the previous instruction was in a noreorder section, then
1755 we don't want to insert the nop after all. */
bdaaa2e1 1756 /* Itbl support may require additional care here. */
252b5132
RH
1757 if (prev_insn_unreordered)
1758 nops = 0;
1759
1760 /* There are two cases which require two intervening
1761 instructions: 1) setting the condition codes using a move to
1762 coprocessor instruction which requires a general coprocessor
1763 delay and then reading the condition codes 2) reading the HI
1764 or LO register and then writing to it (except on processors
1765 which have interlocks). If we are not already emitting a NOP
1766 instruction, we must check for these cases compared to the
1767 instruction previous to the previous instruction. */
1768 if ((! mips_opts.mips16
9ce8a5dd 1769 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
1770 && (prev_prev_insn.insn_mo->pinfo & INSN_COPROC_MOVE_DELAY)
1771 && (prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
1772 && (pinfo & INSN_READ_COND_CODE)
1773 && ! cop_interlocks)
1774 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_LO)
1775 && (pinfo & INSN_WRITE_LO)
1776 && ! (hilo_interlocks
ec68c924 1777 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT))))
252b5132
RH
1778 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
1779 && (pinfo & INSN_WRITE_HI)
1780 && ! (hilo_interlocks
ec68c924 1781 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))))
252b5132
RH
1782 prev_prev_nop = 1;
1783 else
1784 prev_prev_nop = 0;
1785
1786 if (prev_prev_insn_unreordered)
1787 prev_prev_nop = 0;
1788
1789 if (prev_prev_nop && nops == 0)
1790 ++nops;
1791
60b63b72
RS
1792 if (mips_fix_4122_bugs && prev_insn.insn_mo->name)
1793 {
1794 /* We're out of bits in pinfo, so we must resort to string
1795 ops here. Shortcuts are selected based on opcodes being
1796 limited to the VR4122 instruction set. */
1797 int min_nops = 0;
1798 const char *pn = prev_insn.insn_mo->name;
1799 const char *tn = ip->insn_mo->name;
1800 if (strncmp(pn, "macc", 4) == 0
1801 || strncmp(pn, "dmacc", 5) == 0)
1802 {
1803 /* Errata 21 - [D]DIV[U] after [D]MACC */
1804 if (strstr (tn, "div"))
1805 {
1806 min_nops = 1;
1807 }
1808
1809 /* Errata 23 - Continuous DMULT[U]/DMACC instructions */
1810 if (pn[0] == 'd' /* dmacc */
1811 && (strncmp(tn, "dmult", 5) == 0
1812 || strncmp(tn, "dmacc", 5) == 0))
1813 {
1814 min_nops = 1;
1815 }
1816
1817 /* Errata 24 - MT{LO,HI} after [D]MACC */
1818 if (strcmp (tn, "mtlo") == 0
1819 || strcmp (tn, "mthi") == 0)
1820 {
1821 min_nops = 1;
1822 }
1823
1824 }
1825 else if (strncmp(pn, "dmult", 5) == 0
1826 && (strncmp(tn, "dmult", 5) == 0
1827 || strncmp(tn, "dmacc", 5) == 0))
1828 {
1829 /* Here is the rest of errata 23. */
1830 min_nops = 1;
1831 }
1832 if (nops < min_nops)
1833 nops = min_nops;
1834 }
1835
252b5132
RH
1836 /* If we are being given a nop instruction, don't bother with
1837 one of the nops we would otherwise output. This will only
1838 happen when a nop instruction is used with mips_optimize set
1839 to 0. */
1840 if (nops > 0
1841 && ! mips_opts.noreorder
156c2f8b 1842 && ip->insn_opcode == (unsigned) (mips_opts.mips16 ? 0x6500 : 0))
252b5132
RH
1843 --nops;
1844
1845 /* Now emit the right number of NOP instructions. */
1846 if (nops > 0 && ! mips_opts.noreorder)
1847 {
1848 fragS *old_frag;
1849 unsigned long old_frag_offset;
1850 int i;
1851 struct insn_label_list *l;
1852
1853 old_frag = frag_now;
1854 old_frag_offset = frag_now_fix ();
1855
1856 for (i = 0; i < nops; i++)
1857 emit_nop ();
1858
1859 if (listing)
1860 {
1861 listing_prev_line ();
1862 /* We may be at the start of a variant frag. In case we
1863 are, make sure there is enough space for the frag
1864 after the frags created by listing_prev_line. The
1865 argument to frag_grow here must be at least as large
1866 as the argument to all other calls to frag_grow in
1867 this file. We don't have to worry about being in the
1868 middle of a variant frag, because the variants insert
1869 all needed nop instructions themselves. */
1870 frag_grow (40);
1871 }
1872
1873 for (l = insn_labels; l != NULL; l = l->next)
1874 {
98aa84af
AM
1875 valueT val;
1876
252b5132 1877 assert (S_GET_SEGMENT (l->label) == now_seg);
49309057 1878 symbol_set_frag (l->label, frag_now);
98aa84af 1879 val = (valueT) frag_now_fix ();
252b5132
RH
1880 /* mips16 text labels are stored as odd. */
1881 if (mips_opts.mips16)
f9419b05 1882 ++val;
98aa84af 1883 S_SET_VALUE (l->label, val);
252b5132
RH
1884 }
1885
1886#ifndef NO_ECOFF_DEBUGGING
1887 if (ECOFF_DEBUGGING)
1888 ecoff_fix_loc (old_frag, old_frag_offset);
1889#endif
1890 }
1891 else if (prev_nop_frag != NULL)
1892 {
1893 /* We have a frag holding nops we may be able to remove. If
1894 we don't need any nops, we can decrease the size of
1895 prev_nop_frag by the size of one instruction. If we do
bdaaa2e1 1896 need some nops, we count them in prev_nops_required. */
252b5132
RH
1897 if (prev_nop_frag_since == 0)
1898 {
1899 if (nops == 0)
1900 {
1901 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1902 --prev_nop_frag_holds;
1903 }
1904 else
1905 prev_nop_frag_required += nops;
1906 }
1907 else
1908 {
1909 if (prev_prev_nop == 0)
1910 {
1911 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1912 --prev_nop_frag_holds;
1913 }
1914 else
1915 ++prev_nop_frag_required;
1916 }
1917
1918 if (prev_nop_frag_holds <= prev_nop_frag_required)
1919 prev_nop_frag = NULL;
1920
1921 ++prev_nop_frag_since;
1922
1923 /* Sanity check: by the time we reach the second instruction
1924 after prev_nop_frag, we should have used up all the nops
1925 one way or another. */
1926 assert (prev_nop_frag_since <= 1 || prev_nop_frag == NULL);
1927 }
1928 }
1929
4a6a3df4
AO
1930 if (place == NULL
1931 && address_expr
0b25d3e6 1932 && *reloc_type == BFD_RELOC_16_PCREL_S2
4a6a3df4
AO
1933 && (pinfo & INSN_UNCOND_BRANCH_DELAY || pinfo & INSN_COND_BRANCH_DELAY
1934 || pinfo & INSN_COND_BRANCH_LIKELY)
1935 && mips_relax_branch
1936 /* Don't try branch relaxation within .set nomacro, or within
1937 .set noat if we use $at for PIC computations. If it turns
1938 out that the branch was out-of-range, we'll get an error. */
1939 && !mips_opts.warn_about_macros
1940 && !(mips_opts.noat && mips_pic != NO_PIC)
1941 && !mips_opts.mips16)
1942 {
1943 f = frag_var (rs_machine_dependent,
1944 relaxed_branch_length
1945 (NULL, NULL,
1946 (pinfo & INSN_UNCOND_BRANCH_DELAY) ? -1
1947 : (pinfo & INSN_COND_BRANCH_LIKELY) ? 1 : 0), 4,
1948 RELAX_BRANCH_ENCODE
af6ae2ad 1949 (pinfo & INSN_UNCOND_BRANCH_DELAY,
4a6a3df4
AO
1950 pinfo & INSN_COND_BRANCH_LIKELY,
1951 pinfo & INSN_WRITE_GPR_31,
1952 0),
1953 address_expr->X_add_symbol,
1954 address_expr->X_add_number,
1955 0);
1956 *reloc_type = BFD_RELOC_UNUSED;
1957 }
1958 else if (*reloc_type > BFD_RELOC_UNUSED)
252b5132
RH
1959 {
1960 /* We need to set up a variant frag. */
1961 assert (mips_opts.mips16 && address_expr != NULL);
1962 f = frag_var (rs_machine_dependent, 4, 0,
f6688943 1963 RELAX_MIPS16_ENCODE (*reloc_type - BFD_RELOC_UNUSED,
252b5132
RH
1964 mips16_small, mips16_ext,
1965 (prev_pinfo
1966 & INSN_UNCOND_BRANCH_DELAY),
f6688943 1967 (*prev_insn_reloc_type
252b5132 1968 == BFD_RELOC_MIPS16_JMP)),
c4e7957c 1969 make_expr_symbol (address_expr), 0, NULL);
252b5132
RH
1970 }
1971 else if (place != NULL)
1972 f = place;
1973 else if (mips_opts.mips16
1974 && ! ip->use_extend
f6688943 1975 && *reloc_type != BFD_RELOC_MIPS16_JMP)
252b5132
RH
1976 {
1977 /* Make sure there is enough room to swap this instruction with
1978 a following jump instruction. */
1979 frag_grow (6);
1980 f = frag_more (2);
1981 }
1982 else
1983 {
1984 if (mips_opts.mips16
1985 && mips_opts.noreorder
1986 && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
1987 as_warn (_("extended instruction in delay slot"));
1988
1989 f = frag_more (4);
1990 }
1991
f6688943
TS
1992 fixp[0] = fixp[1] = fixp[2] = NULL;
1993 if (address_expr != NULL && *reloc_type < BFD_RELOC_UNUSED)
252b5132
RH
1994 {
1995 if (address_expr->X_op == O_constant)
1996 {
4db1a35d 1997 valueT tmp;
f6688943
TS
1998
1999 switch (*reloc_type)
252b5132
RH
2000 {
2001 case BFD_RELOC_32:
2002 ip->insn_opcode |= address_expr->X_add_number;
2003 break;
2004
f6688943 2005 case BFD_RELOC_MIPS_HIGHEST:
6344efa4
AM
2006 tmp = (address_expr->X_add_number
2007 + ((valueT) 0x8000 << 32) + 0x80008000) >> 16;
f6688943
TS
2008 tmp >>= 16;
2009 ip->insn_opcode |= (tmp >> 16) & 0xffff;
2010 break;
2011
2012 case BFD_RELOC_MIPS_HIGHER:
2013 tmp = (address_expr->X_add_number + 0x80008000) >> 16;
2014 ip->insn_opcode |= (tmp >> 16) & 0xffff;
2015 break;
2016
2017 case BFD_RELOC_HI16_S:
2018 ip->insn_opcode |= ((address_expr->X_add_number + 0x8000)
2019 >> 16) & 0xffff;
2020 break;
2021
2022 case BFD_RELOC_HI16:
2023 ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
2024 break;
2025
252b5132 2026 case BFD_RELOC_LO16:
ed6fb7bd 2027 case BFD_RELOC_MIPS_GOT_DISP:
252b5132
RH
2028 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2029 break;
2030
2031 case BFD_RELOC_MIPS_JMP:
2032 if ((address_expr->X_add_number & 3) != 0)
2033 as_bad (_("jump to misaligned address (0x%lx)"),
2034 (unsigned long) address_expr->X_add_number);
f3c0ec86 2035 if (address_expr->X_add_number & ~0xfffffff)
7496292d
TS
2036 as_bad (_("jump address range overflow (0x%lx)"),
2037 (unsigned long) address_expr->X_add_number);
252b5132
RH
2038 ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
2039 break;
2040
2041 case BFD_RELOC_MIPS16_JMP:
2042 if ((address_expr->X_add_number & 3) != 0)
2043 as_bad (_("jump to misaligned address (0x%lx)"),
2044 (unsigned long) address_expr->X_add_number);
f3c0ec86 2045 if (address_expr->X_add_number & ~0xfffffff)
7496292d
TS
2046 as_bad (_("jump address range overflow (0x%lx)"),
2047 (unsigned long) address_expr->X_add_number);
252b5132
RH
2048 ip->insn_opcode |=
2049 (((address_expr->X_add_number & 0x7c0000) << 3)
2050 | ((address_expr->X_add_number & 0xf800000) >> 7)
2051 | ((address_expr->X_add_number & 0x3fffc) >> 2));
2052 break;
2053
252b5132
RH
2054 case BFD_RELOC_16_PCREL_S2:
2055 goto need_reloc;
2056
2057 default:
2058 internalError ();
2059 }
2060 }
2061 else
2062 {
2063 need_reloc:
f6688943 2064 /* Don't generate a reloc if we are writing into a variant frag. */
252b5132
RH
2065 if (place == NULL)
2066 {
34ce925e
RS
2067 reloc_howto_type *howto;
2068 int i;
2069
2070 /* In a compound relocation, it is the final (outermost)
2071 operator that determines the relocated field. */
2072 for (i = 1; i < 3; i++)
2073 if (reloc_type[i] == BFD_RELOC_UNUSED)
2074 break;
2075
2076 howto = bfd_reloc_type_lookup (stdoutput, reloc_type[i - 1]);
a105a300
TS
2077 fixp[0] = fix_new_exp (frag_now, f - frag_now->fr_literal,
2078 bfd_get_reloc_size(howto),
f6688943 2079 address_expr,
a105a300 2080 reloc_type[0] == BFD_RELOC_16_PCREL_S2,
f6688943
TS
2081 reloc_type[0]);
2082
b6ff326e 2083 /* These relocations can have an addend that won't fit in
f6688943 2084 4 octets for 64bit assembly. */
a105a300
TS
2085 if (HAVE_64BIT_GPRS
2086 && ! howto->partial_inplace
2087 && (reloc_type[0] == BFD_RELOC_16
2088 || reloc_type[0] == BFD_RELOC_32
2089 || reloc_type[0] == BFD_RELOC_MIPS_JMP
2090 || reloc_type[0] == BFD_RELOC_HI16_S
2091 || reloc_type[0] == BFD_RELOC_LO16
2092 || reloc_type[0] == BFD_RELOC_GPREL16
2093 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
2094 || reloc_type[0] == BFD_RELOC_GPREL32
2095 || reloc_type[0] == BFD_RELOC_64
2096 || reloc_type[0] == BFD_RELOC_CTOR
2097 || reloc_type[0] == BFD_RELOC_MIPS_SUB
2098 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
2099 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
2100 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
2101 || reloc_type[0] == BFD_RELOC_MIPS_REL16
2102 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT))
f6688943
TS
2103 fixp[0]->fx_no_overflow = 1;
2104
5919d012 2105 if (reloc_needs_lo_p (*reloc_type))
252b5132
RH
2106 {
2107 struct mips_hi_fixup *hi_fixup;
2108
5919d012
RS
2109 /* Reuse the last entry if it already has a matching %lo. */
2110 hi_fixup = mips_hi_fixup_list;
2111 if (hi_fixup == 0
2112 || !fixup_has_matching_lo_p (hi_fixup->fixp))
2113 {
2114 hi_fixup = ((struct mips_hi_fixup *)
2115 xmalloc (sizeof (struct mips_hi_fixup)));
2116 hi_fixup->next = mips_hi_fixup_list;
2117 mips_hi_fixup_list = hi_fixup;
2118 }
f6688943 2119 hi_fixup->fixp = fixp[0];
252b5132 2120 hi_fixup->seg = now_seg;
252b5132 2121 }
f6688943 2122
34ce925e
RS
2123 /* Add fixups for the second and third relocations, if given.
2124 Note that the ABI allows the second relocation to be
2125 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the
2126 moment we only use RSS_UNDEF, but we could add support
2127 for the others if it ever becomes necessary. */
2128 for (i = 1; i < 3; i++)
2129 if (reloc_type[i] != BFD_RELOC_UNUSED)
2130 {
2131 address_expr->X_op = O_absent;
2132 address_expr->X_add_symbol = 0;
2133 address_expr->X_add_number = 0;
2134
2135 fixp[i] = fix_new_exp (frag_now, fixp[0]->fx_where,
2136 fixp[0]->fx_size, address_expr,
2137 FALSE, reloc_type[i]);
2138 }
252b5132
RH
2139 }
2140 }
2141 }
2142
2143 if (! mips_opts.mips16)
c5dd6aab
DJ
2144 {
2145 md_number_to_chars (f, ip->insn_opcode, 4);
2146#ifdef OBJ_ELF
2147 dwarf2_emit_insn (4);
2148#endif
2149 }
f6688943 2150 else if (*reloc_type == BFD_RELOC_MIPS16_JMP)
252b5132
RH
2151 {
2152 md_number_to_chars (f, ip->insn_opcode >> 16, 2);
2153 md_number_to_chars (f + 2, ip->insn_opcode & 0xffff, 2);
c5dd6aab
DJ
2154#ifdef OBJ_ELF
2155 dwarf2_emit_insn (4);
2156#endif
252b5132
RH
2157 }
2158 else
2159 {
2160 if (ip->use_extend)
2161 {
2162 md_number_to_chars (f, 0xf000 | ip->extend, 2);
2163 f += 2;
2164 }
2165 md_number_to_chars (f, ip->insn_opcode, 2);
c5dd6aab
DJ
2166#ifdef OBJ_ELF
2167 dwarf2_emit_insn (ip->use_extend ? 4 : 2);
2168#endif
252b5132
RH
2169 }
2170
2171 /* Update the register mask information. */
2172 if (! mips_opts.mips16)
2173 {
2174 if (pinfo & INSN_WRITE_GPR_D)
2175 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
2176 if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2177 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
2178 if (pinfo & INSN_READ_GPR_S)
2179 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
2180 if (pinfo & INSN_WRITE_GPR_31)
f9419b05 2181 mips_gprmask |= 1 << RA;
252b5132
RH
2182 if (pinfo & INSN_WRITE_FPR_D)
2183 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
2184 if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2185 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
2186 if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2187 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
2188 if ((pinfo & INSN_READ_FPR_R) != 0)
2189 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
2190 if (pinfo & INSN_COP)
2191 {
bdaaa2e1
KH
2192 /* We don't keep enough information to sort these cases out.
2193 The itbl support does keep this information however, although
2194 we currently don't support itbl fprmats as part of the cop
2195 instruction. May want to add this support in the future. */
252b5132
RH
2196 }
2197 /* Never set the bit for $0, which is always zero. */
beae10d5 2198 mips_gprmask &= ~1 << 0;
252b5132
RH
2199 }
2200 else
2201 {
2202 if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2203 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RX)
2204 & MIPS16OP_MASK_RX);
2205 if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2206 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RY)
2207 & MIPS16OP_MASK_RY);
2208 if (pinfo & MIPS16_INSN_WRITE_Z)
2209 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RZ)
2210 & MIPS16OP_MASK_RZ);
2211 if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2212 mips_gprmask |= 1 << TREG;
2213 if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2214 mips_gprmask |= 1 << SP;
2215 if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2216 mips_gprmask |= 1 << RA;
2217 if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2218 mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2219 if (pinfo & MIPS16_INSN_READ_Z)
2220 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
2221 & MIPS16OP_MASK_MOVE32Z);
2222 if (pinfo & MIPS16_INSN_READ_GPR_X)
2223 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
2224 & MIPS16OP_MASK_REGR32);
2225 }
2226
2227 if (place == NULL && ! mips_opts.noreorder)
2228 {
2229 /* Filling the branch delay slot is more complex. We try to
2230 switch the branch with the previous instruction, which we can
2231 do if the previous instruction does not set up a condition
2232 that the branch tests and if the branch is not itself the
2233 target of any branch. */
2234 if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2235 || (pinfo & INSN_COND_BRANCH_DELAY))
2236 {
2237 if (mips_optimize < 2
2238 /* If we have seen .set volatile or .set nomove, don't
2239 optimize. */
2240 || mips_opts.nomove != 0
2241 /* If we had to emit any NOP instructions, then we
2242 already know we can not swap. */
2243 || nops != 0
2244 /* If we don't even know the previous insn, we can not
bdaaa2e1 2245 swap. */
252b5132
RH
2246 || ! prev_insn_valid
2247 /* If the previous insn is already in a branch delay
2248 slot, then we can not swap. */
2249 || prev_insn_is_delay_slot
2250 /* If the previous previous insn was in a .set
2251 noreorder, we can't swap. Actually, the MIPS
2252 assembler will swap in this situation. However, gcc
2253 configured -with-gnu-as will generate code like
2254 .set noreorder
2255 lw $4,XXX
2256 .set reorder
2257 INSN
2258 bne $4,$0,foo
2259 in which we can not swap the bne and INSN. If gcc is
2260 not configured -with-gnu-as, it does not output the
2261 .set pseudo-ops. We don't have to check
2262 prev_insn_unreordered, because prev_insn_valid will
2263 be 0 in that case. We don't want to use
2264 prev_prev_insn_valid, because we do want to be able
2265 to swap at the start of a function. */
2266 || prev_prev_insn_unreordered
2267 /* If the branch is itself the target of a branch, we
2268 can not swap. We cheat on this; all we check for is
2269 whether there is a label on this instruction. If
2270 there are any branches to anything other than a
2271 label, users must use .set noreorder. */
2272 || insn_labels != NULL
2273 /* If the previous instruction is in a variant frag, we
2274 can not do the swap. This does not apply to the
2275 mips16, which uses variant frags for different
2276 purposes. */
2277 || (! mips_opts.mips16
2278 && prev_insn_frag->fr_type == rs_machine_dependent)
2279 /* If the branch reads the condition codes, we don't
2280 even try to swap, because in the sequence
2281 ctc1 $X,$31
2282 INSN
2283 INSN
2284 bc1t LABEL
2285 we can not swap, and I don't feel like handling that
2286 case. */
2287 || (! mips_opts.mips16
9ce8a5dd 2288 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
2289 && (pinfo & INSN_READ_COND_CODE))
2290 /* We can not swap with an instruction that requires a
67c1ffbe 2291 delay slot, because the target of the branch might
252b5132
RH
2292 interfere with that instruction. */
2293 || (! mips_opts.mips16
9ce8a5dd 2294 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132 2295 && (prev_pinfo
bdaaa2e1 2296 /* Itbl support may require additional care here. */
252b5132
RH
2297 & (INSN_LOAD_COPROC_DELAY
2298 | INSN_COPROC_MOVE_DELAY
2299 | INSN_WRITE_COND_CODE)))
2300 || (! (hilo_interlocks
ec68c924 2301 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
252b5132
RH
2302 && (prev_pinfo
2303 & (INSN_READ_LO
2304 | INSN_READ_HI)))
2305 || (! mips_opts.mips16
2306 && ! gpr_interlocks
2307 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))
2308 || (! mips_opts.mips16
e7af610e 2309 && mips_opts.isa == ISA_MIPS1
bdaaa2e1 2310 /* Itbl support may require additional care here. */
252b5132
RH
2311 && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))
2312 /* We can not swap with a branch instruction. */
2313 || (prev_pinfo
2314 & (INSN_UNCOND_BRANCH_DELAY
2315 | INSN_COND_BRANCH_DELAY
2316 | INSN_COND_BRANCH_LIKELY))
2317 /* We do not swap with a trap instruction, since it
2318 complicates trap handlers to have the trap
2319 instruction be in a delay slot. */
2320 || (prev_pinfo & INSN_TRAP)
2321 /* If the branch reads a register that the previous
2322 instruction sets, we can not swap. */
2323 || (! mips_opts.mips16
2324 && (prev_pinfo & INSN_WRITE_GPR_T)
2325 && insn_uses_reg (ip,
2326 ((prev_insn.insn_opcode >> OP_SH_RT)
2327 & OP_MASK_RT),
2328 MIPS_GR_REG))
2329 || (! mips_opts.mips16
2330 && (prev_pinfo & INSN_WRITE_GPR_D)
2331 && insn_uses_reg (ip,
2332 ((prev_insn.insn_opcode >> OP_SH_RD)
2333 & OP_MASK_RD),
2334 MIPS_GR_REG))
2335 || (mips_opts.mips16
2336 && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2337 && insn_uses_reg (ip,
2338 ((prev_insn.insn_opcode
2339 >> MIPS16OP_SH_RX)
2340 & MIPS16OP_MASK_RX),
2341 MIPS16_REG))
2342 || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2343 && insn_uses_reg (ip,
2344 ((prev_insn.insn_opcode
2345 >> MIPS16OP_SH_RY)
2346 & MIPS16OP_MASK_RY),
2347 MIPS16_REG))
2348 || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2349 && insn_uses_reg (ip,
2350 ((prev_insn.insn_opcode
2351 >> MIPS16OP_SH_RZ)
2352 & MIPS16OP_MASK_RZ),
2353 MIPS16_REG))
2354 || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2355 && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2356 || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2357 && insn_uses_reg (ip, RA, MIPS_GR_REG))
2358 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2359 && insn_uses_reg (ip,
2360 MIPS16OP_EXTRACT_REG32R (prev_insn.
2361 insn_opcode),
2362 MIPS_GR_REG))))
2363 /* If the branch writes a register that the previous
2364 instruction sets, we can not swap (we know that
2365 branches write only to RD or to $31). */
2366 || (! mips_opts.mips16
2367 && (prev_pinfo & INSN_WRITE_GPR_T)
2368 && (((pinfo & INSN_WRITE_GPR_D)
2369 && (((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT)
2370 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2371 || ((pinfo & INSN_WRITE_GPR_31)
2372 && (((prev_insn.insn_opcode >> OP_SH_RT)
2373 & OP_MASK_RT)
f9419b05 2374 == RA))))
252b5132
RH
2375 || (! mips_opts.mips16
2376 && (prev_pinfo & INSN_WRITE_GPR_D)
2377 && (((pinfo & INSN_WRITE_GPR_D)
2378 && (((prev_insn.insn_opcode >> OP_SH_RD) & OP_MASK_RD)
2379 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2380 || ((pinfo & INSN_WRITE_GPR_31)
2381 && (((prev_insn.insn_opcode >> OP_SH_RD)
2382 & OP_MASK_RD)
f9419b05 2383 == RA))))
252b5132
RH
2384 || (mips_opts.mips16
2385 && (pinfo & MIPS16_INSN_WRITE_31)
2386 && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2387 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2388 && (MIPS16OP_EXTRACT_REG32R (prev_insn.insn_opcode)
2389 == RA))))
2390 /* If the branch writes a register that the previous
2391 instruction reads, we can not swap (we know that
2392 branches only write to RD or to $31). */
2393 || (! mips_opts.mips16
2394 && (pinfo & INSN_WRITE_GPR_D)
2395 && insn_uses_reg (&prev_insn,
2396 ((ip->insn_opcode >> OP_SH_RD)
2397 & OP_MASK_RD),
2398 MIPS_GR_REG))
2399 || (! mips_opts.mips16
2400 && (pinfo & INSN_WRITE_GPR_31)
f9419b05 2401 && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
252b5132
RH
2402 || (mips_opts.mips16
2403 && (pinfo & MIPS16_INSN_WRITE_31)
2404 && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2405 /* If we are generating embedded PIC code, the branch
2406 might be expanded into a sequence which uses $at, so
2407 we can't swap with an instruction which reads it. */
2408 || (mips_pic == EMBEDDED_PIC
2409 && insn_uses_reg (&prev_insn, AT, MIPS_GR_REG))
2410 /* If the previous previous instruction has a load
2411 delay, and sets a register that the branch reads, we
2412 can not swap. */
2413 || (! mips_opts.mips16
9ce8a5dd 2414 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
bdaaa2e1 2415 /* Itbl support may require additional care here. */
252b5132
RH
2416 && ((prev_prev_insn.insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
2417 || (! gpr_interlocks
2418 && (prev_prev_insn.insn_mo->pinfo
2419 & INSN_LOAD_MEMORY_DELAY)))
2420 && insn_uses_reg (ip,
2421 ((prev_prev_insn.insn_opcode >> OP_SH_RT)
2422 & OP_MASK_RT),
2423 MIPS_GR_REG))
2424 /* If one instruction sets a condition code and the
2425 other one uses a condition code, we can not swap. */
2426 || ((pinfo & INSN_READ_COND_CODE)
2427 && (prev_pinfo & INSN_WRITE_COND_CODE))
2428 || ((pinfo & INSN_WRITE_COND_CODE)
2429 && (prev_pinfo & INSN_READ_COND_CODE))
2430 /* If the previous instruction uses the PC, we can not
2431 swap. */
2432 || (mips_opts.mips16
2433 && (prev_pinfo & MIPS16_INSN_READ_PC))
2434 /* If the previous instruction was extended, we can not
2435 swap. */
2436 || (mips_opts.mips16 && prev_insn_extended)
2437 /* If the previous instruction had a fixup in mips16
2438 mode, we can not swap. This normally means that the
2439 previous instruction was a 4 byte branch anyhow. */
f6688943 2440 || (mips_opts.mips16 && prev_insn_fixp[0])
bdaaa2e1
KH
2441 /* If the previous instruction is a sync, sync.l, or
2442 sync.p, we can not swap. */
f173e82e 2443 || (prev_pinfo & INSN_SYNC))
252b5132
RH
2444 {
2445 /* We could do even better for unconditional branches to
2446 portions of this object file; we could pick up the
2447 instruction at the destination, put it in the delay
2448 slot, and bump the destination address. */
2449 emit_nop ();
2450 /* Update the previous insn information. */
2451 prev_prev_insn = *ip;
2452 prev_insn.insn_mo = &dummy_opcode;
2453 }
2454 else
2455 {
2456 /* It looks like we can actually do the swap. */
2457 if (! mips_opts.mips16)
2458 {
2459 char *prev_f;
2460 char temp[4];
2461
2462 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2463 memcpy (temp, prev_f, 4);
2464 memcpy (prev_f, f, 4);
2465 memcpy (f, temp, 4);
f6688943
TS
2466 if (prev_insn_fixp[0])
2467 {
2468 prev_insn_fixp[0]->fx_frag = frag_now;
2469 prev_insn_fixp[0]->fx_where = f - frag_now->fr_literal;
2470 }
2471 if (prev_insn_fixp[1])
2472 {
2473 prev_insn_fixp[1]->fx_frag = frag_now;
2474 prev_insn_fixp[1]->fx_where = f - frag_now->fr_literal;
2475 }
2476 if (prev_insn_fixp[2])
252b5132 2477 {
f6688943
TS
2478 prev_insn_fixp[2]->fx_frag = frag_now;
2479 prev_insn_fixp[2]->fx_where = f - frag_now->fr_literal;
252b5132 2480 }
f5040a92
AO
2481 if (prev_insn_fixp[0] && HAVE_NEWABI
2482 && prev_insn_frag != frag_now
2483 && (prev_insn_fixp[0]->fx_r_type
2484 == BFD_RELOC_MIPS_GOT_DISP
2485 || (prev_insn_fixp[0]->fx_r_type
2486 == BFD_RELOC_MIPS_CALL16)))
2487 {
2488 /* To avoid confusion in tc_gen_reloc, we must
2489 ensure that this does not become a variant
2490 frag. */
2491 force_new_frag = TRUE;
2492 }
f6688943 2493 if (fixp[0])
252b5132 2494 {
f6688943
TS
2495 fixp[0]->fx_frag = prev_insn_frag;
2496 fixp[0]->fx_where = prev_insn_where;
2497 }
2498 if (fixp[1])
2499 {
2500 fixp[1]->fx_frag = prev_insn_frag;
2501 fixp[1]->fx_where = prev_insn_where;
2502 }
2503 if (fixp[2])
2504 {
2505 fixp[2]->fx_frag = prev_insn_frag;
2506 fixp[2]->fx_where = prev_insn_where;
252b5132
RH
2507 }
2508 }
2509 else
2510 {
2511 char *prev_f;
2512 char temp[2];
2513
f6688943
TS
2514 assert (prev_insn_fixp[0] == NULL);
2515 assert (prev_insn_fixp[1] == NULL);
2516 assert (prev_insn_fixp[2] == NULL);
252b5132
RH
2517 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2518 memcpy (temp, prev_f, 2);
2519 memcpy (prev_f, f, 2);
f6688943 2520 if (*reloc_type != BFD_RELOC_MIPS16_JMP)
252b5132 2521 {
f6688943 2522 assert (*reloc_type == BFD_RELOC_UNUSED);
252b5132
RH
2523 memcpy (f, temp, 2);
2524 }
2525 else
2526 {
2527 memcpy (f, f + 2, 2);
2528 memcpy (f + 2, temp, 2);
2529 }
f6688943
TS
2530 if (fixp[0])
2531 {
2532 fixp[0]->fx_frag = prev_insn_frag;
2533 fixp[0]->fx_where = prev_insn_where;
2534 }
2535 if (fixp[1])
2536 {
2537 fixp[1]->fx_frag = prev_insn_frag;
2538 fixp[1]->fx_where = prev_insn_where;
2539 }
2540 if (fixp[2])
252b5132 2541 {
f6688943
TS
2542 fixp[2]->fx_frag = prev_insn_frag;
2543 fixp[2]->fx_where = prev_insn_where;
252b5132
RH
2544 }
2545 }
2546
2547 /* Update the previous insn information; leave prev_insn
2548 unchanged. */
2549 prev_prev_insn = *ip;
2550 }
2551 prev_insn_is_delay_slot = 1;
2552
2553 /* If that was an unconditional branch, forget the previous
2554 insn information. */
2555 if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2556 {
2557 prev_prev_insn.insn_mo = &dummy_opcode;
2558 prev_insn.insn_mo = &dummy_opcode;
2559 }
2560
f6688943
TS
2561 prev_insn_fixp[0] = NULL;
2562 prev_insn_fixp[1] = NULL;
2563 prev_insn_fixp[2] = NULL;
2564 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2565 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2566 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
252b5132
RH
2567 prev_insn_extended = 0;
2568 }
2569 else if (pinfo & INSN_COND_BRANCH_LIKELY)
2570 {
2571 /* We don't yet optimize a branch likely. What we should do
2572 is look at the target, copy the instruction found there
2573 into the delay slot, and increment the branch to jump to
2574 the next instruction. */
2575 emit_nop ();
2576 /* Update the previous insn information. */
2577 prev_prev_insn = *ip;
2578 prev_insn.insn_mo = &dummy_opcode;
f6688943
TS
2579 prev_insn_fixp[0] = NULL;
2580 prev_insn_fixp[1] = NULL;
2581 prev_insn_fixp[2] = NULL;
2582 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2583 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2584 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
252b5132
RH
2585 prev_insn_extended = 0;
2586 }
2587 else
2588 {
2589 /* Update the previous insn information. */
2590 if (nops > 0)
2591 prev_prev_insn.insn_mo = &dummy_opcode;
2592 else
2593 prev_prev_insn = prev_insn;
2594 prev_insn = *ip;
2595
2596 /* Any time we see a branch, we always fill the delay slot
2597 immediately; since this insn is not a branch, we know it
2598 is not in a delay slot. */
2599 prev_insn_is_delay_slot = 0;
2600
f6688943
TS
2601 prev_insn_fixp[0] = fixp[0];
2602 prev_insn_fixp[1] = fixp[1];
2603 prev_insn_fixp[2] = fixp[2];
2604 prev_insn_reloc_type[0] = reloc_type[0];
2605 prev_insn_reloc_type[1] = reloc_type[1];
2606 prev_insn_reloc_type[2] = reloc_type[2];
252b5132
RH
2607 if (mips_opts.mips16)
2608 prev_insn_extended = (ip->use_extend
f6688943 2609 || *reloc_type > BFD_RELOC_UNUSED);
252b5132
RH
2610 }
2611
2612 prev_prev_insn_unreordered = prev_insn_unreordered;
2613 prev_insn_unreordered = 0;
2614 prev_insn_frag = frag_now;
2615 prev_insn_where = f - frag_now->fr_literal;
2616 prev_insn_valid = 1;
2617 }
2618 else if (place == NULL)
2619 {
2620 /* We need to record a bit of information even when we are not
2621 reordering, in order to determine the base address for mips16
2622 PC relative relocs. */
2623 prev_prev_insn = prev_insn;
2624 prev_insn = *ip;
f6688943
TS
2625 prev_insn_reloc_type[0] = reloc_type[0];
2626 prev_insn_reloc_type[1] = reloc_type[1];
2627 prev_insn_reloc_type[2] = reloc_type[2];
252b5132
RH
2628 prev_prev_insn_unreordered = prev_insn_unreordered;
2629 prev_insn_unreordered = 1;
2630 }
2631
2632 /* We just output an insn, so the next one doesn't have a label. */
2633 mips_clear_insn_labels ();
f5040a92
AO
2634
2635 /* We must ensure that the frag to which an instruction that was
2636 moved from a non-variant frag doesn't become a variant frag,
2637 otherwise tc_gen_reloc may get confused. */
2638 if (force_new_frag)
2639 {
2640 frag_wane (frag_now);
2641 frag_new (0);
2642 }
252b5132
RH
2643}
2644
2645/* This function forgets that there was any previous instruction or
2646 label. If PRESERVE is non-zero, it remembers enough information to
bdaaa2e1 2647 know whether nops are needed before a noreorder section. */
252b5132
RH
2648
2649static void
17a2f251 2650mips_no_prev_insn (int preserve)
252b5132
RH
2651{
2652 if (! preserve)
2653 {
2654 prev_insn.insn_mo = &dummy_opcode;
2655 prev_prev_insn.insn_mo = &dummy_opcode;
2656 prev_nop_frag = NULL;
2657 prev_nop_frag_holds = 0;
2658 prev_nop_frag_required = 0;
2659 prev_nop_frag_since = 0;
2660 }
2661 prev_insn_valid = 0;
2662 prev_insn_is_delay_slot = 0;
2663 prev_insn_unreordered = 0;
2664 prev_insn_extended = 0;
f6688943
TS
2665 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2666 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2667 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
252b5132
RH
2668 prev_prev_insn_unreordered = 0;
2669 mips_clear_insn_labels ();
2670}
2671
2672/* This function must be called whenever we turn on noreorder or emit
2673 something other than instructions. It inserts any NOPS which might
2674 be needed by the previous instruction, and clears the information
2675 kept for the previous instructions. The INSNS parameter is true if
bdaaa2e1 2676 instructions are to follow. */
252b5132
RH
2677
2678static void
17a2f251 2679mips_emit_delays (bfd_boolean insns)
252b5132
RH
2680{
2681 if (! mips_opts.noreorder)
2682 {
2683 int nops;
2684
2685 nops = 0;
2686 if ((! mips_opts.mips16
9ce8a5dd 2687 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
2688 && (! cop_interlocks
2689 && (prev_insn.insn_mo->pinfo
2690 & (INSN_LOAD_COPROC_DELAY
2691 | INSN_COPROC_MOVE_DELAY
2692 | INSN_WRITE_COND_CODE))))
2693 || (! hilo_interlocks
2694 && (prev_insn.insn_mo->pinfo
2695 & (INSN_READ_LO
2696 | INSN_READ_HI)))
2697 || (! mips_opts.mips16
2698 && ! gpr_interlocks
bdaaa2e1 2699 && (prev_insn.insn_mo->pinfo
252b5132
RH
2700 & INSN_LOAD_MEMORY_DELAY))
2701 || (! mips_opts.mips16
e7af610e 2702 && mips_opts.isa == ISA_MIPS1
252b5132
RH
2703 && (prev_insn.insn_mo->pinfo
2704 & INSN_COPROC_MEMORY_DELAY)))
2705 {
beae10d5 2706 /* Itbl support may require additional care here. */
252b5132
RH
2707 ++nops;
2708 if ((! mips_opts.mips16
9ce8a5dd 2709 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
2710 && (! cop_interlocks
2711 && prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2712 || (! hilo_interlocks
2713 && ((prev_insn.insn_mo->pinfo & INSN_READ_HI)
2714 || (prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2715 ++nops;
2716
2717 if (prev_insn_unreordered)
2718 nops = 0;
2719 }
2720 else if ((! mips_opts.mips16
9ce8a5dd 2721 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
2722 && (! cop_interlocks
2723 && prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2724 || (! hilo_interlocks
2725 && ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
2726 || (prev_prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2727 {
beae10d5 2728 /* Itbl support may require additional care here. */
252b5132
RH
2729 if (! prev_prev_insn_unreordered)
2730 ++nops;
2731 }
2732
60b63b72
RS
2733 if (mips_fix_4122_bugs && prev_insn.insn_mo->name)
2734 {
2735 int min_nops = 0;
2736 const char *pn = prev_insn.insn_mo->name;
2737 if (strncmp(pn, "macc", 4) == 0
2738 || strncmp(pn, "dmacc", 5) == 0
2739 || strncmp(pn, "dmult", 5) == 0)
2740 {
2741 min_nops = 1;
2742 }
2743 if (nops < min_nops)
2744 nops = min_nops;
2745 }
2746
252b5132
RH
2747 if (nops > 0)
2748 {
2749 struct insn_label_list *l;
2750
2751 if (insns)
2752 {
2753 /* Record the frag which holds the nop instructions, so
2754 that we can remove them if we don't need them. */
2755 frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2756 prev_nop_frag = frag_now;
2757 prev_nop_frag_holds = nops;
2758 prev_nop_frag_required = 0;
2759 prev_nop_frag_since = 0;
2760 }
2761
2762 for (; nops > 0; --nops)
2763 emit_nop ();
2764
2765 if (insns)
2766 {
2767 /* Move on to a new frag, so that it is safe to simply
bdaaa2e1 2768 decrease the size of prev_nop_frag. */
252b5132
RH
2769 frag_wane (frag_now);
2770 frag_new (0);
2771 }
2772
2773 for (l = insn_labels; l != NULL; l = l->next)
2774 {
98aa84af
AM
2775 valueT val;
2776
252b5132 2777 assert (S_GET_SEGMENT (l->label) == now_seg);
49309057 2778 symbol_set_frag (l->label, frag_now);
98aa84af 2779 val = (valueT) frag_now_fix ();
252b5132
RH
2780 /* mips16 text labels are stored as odd. */
2781 if (mips_opts.mips16)
f9419b05 2782 ++val;
98aa84af 2783 S_SET_VALUE (l->label, val);
252b5132
RH
2784 }
2785 }
2786 }
2787
2788 /* Mark instruction labels in mips16 mode. */
f9419b05 2789 if (insns)
252b5132
RH
2790 mips16_mark_labels ();
2791
2792 mips_no_prev_insn (insns);
2793}
2794
2795/* Build an instruction created by a macro expansion. This is passed
2796 a pointer to the count of instructions created so far, an
2797 expression, the name of the instruction to build, an operand format
2798 string, and corresponding arguments. */
2799
252b5132 2800static void
17a2f251
TS
2801macro_build (char *place, int *counter, expressionS *ep, const char *name,
2802 const char *fmt, ...)
252b5132
RH
2803{
2804 struct mips_cl_insn insn;
f6688943 2805 bfd_reloc_code_real_type r[3];
252b5132 2806 va_list args;
252b5132 2807
252b5132 2808 va_start (args, fmt);
252b5132
RH
2809
2810 /*
2811 * If the macro is about to expand into a second instruction,
2812 * print a warning if needed. We need to pass ip as a parameter
2813 * to generate a better warning message here...
2814 */
2815 if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
2816 as_warn (_("Macro instruction expanded into multiple instructions"));
2817
80cc45a5
EC
2818 /*
2819 * If the macro is about to expand into a second instruction,
2820 * and it is in a delay slot, print a warning.
2821 */
2822 if (place == NULL
2823 && *counter == 1
2824 && mips_opts.noreorder
2825 && (prev_prev_insn.insn_mo->pinfo
2826 & (INSN_UNCOND_BRANCH_DELAY | INSN_COND_BRANCH_DELAY
2b2e39bf 2827 | INSN_COND_BRANCH_LIKELY)) != 0)
80cc45a5
EC
2828 as_warn (_("Macro instruction expanded into multiple instructions in a branch delay slot"));
2829
252b5132 2830 if (place == NULL)
f9419b05 2831 ++*counter; /* bump instruction counter */
252b5132
RH
2832
2833 if (mips_opts.mips16)
2834 {
2835 mips16_macro_build (place, counter, ep, name, fmt, args);
2836 va_end (args);
2837 return;
2838 }
2839
f6688943
TS
2840 r[0] = BFD_RELOC_UNUSED;
2841 r[1] = BFD_RELOC_UNUSED;
2842 r[2] = BFD_RELOC_UNUSED;
252b5132
RH
2843 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
2844 assert (insn.insn_mo);
2845 assert (strcmp (name, insn.insn_mo->name) == 0);
2846
2847 /* Search until we get a match for NAME. */
2848 while (1)
2849 {
b34976b6 2850 /* It is assumed here that macros will never generate
deec1734 2851 MDMX or MIPS-3D instructions. */
252b5132
RH
2852 if (strcmp (fmt, insn.insn_mo->args) == 0
2853 && insn.insn_mo->pinfo != INSN_MACRO
aec421e0
TS
2854 && OPCODE_IS_MEMBER (insn.insn_mo,
2855 (mips_opts.isa
3396de36 2856 | (file_ase_mips16 ? INSN_MIPS16 : 0)),
fef14a42
TS
2857 mips_opts.arch)
2858 && (mips_opts.arch != CPU_R4650 || (insn.insn_mo->pinfo & FP_D) == 0))
252b5132
RH
2859 break;
2860
2861 ++insn.insn_mo;
2862 assert (insn.insn_mo->name);
2863 assert (strcmp (name, insn.insn_mo->name) == 0);
2864 }
2865
2866 insn.insn_opcode = insn.insn_mo->match;
2867 for (;;)
2868 {
2869 switch (*fmt++)
2870 {
2871 case '\0':
2872 break;
2873
2874 case ',':
2875 case '(':
2876 case ')':
2877 continue;
2878
5f74bc13
CD
2879 case '+':
2880 switch (*fmt++)
2881 {
2882 case 'A':
2883 case 'E':
2884 insn.insn_opcode |= (va_arg (args, int)
2885 & OP_MASK_SHAMT) << OP_SH_SHAMT;
2886 continue;
2887
2888 case 'B':
2889 case 'F':
2890 /* Note that in the macro case, these arguments are already
2891 in MSB form. (When handling the instruction in the
2892 non-macro case, these arguments are sizes from which
2893 MSB values must be calculated.) */
2894 insn.insn_opcode |= (va_arg (args, int)
2895 & OP_MASK_INSMSB) << OP_SH_INSMSB;
2896 continue;
2897
2898 case 'C':
2899 case 'G':
2900 case 'H':
2901 /* Note that in the macro case, these arguments are already
2902 in MSBD form. (When handling the instruction in the
2903 non-macro case, these arguments are sizes from which
2904 MSBD values must be calculated.) */
2905 insn.insn_opcode |= (va_arg (args, int)
2906 & OP_MASK_EXTMSBD) << OP_SH_EXTMSBD;
2907 continue;
2908
2909 default:
2910 internalError ();
2911 }
2912 continue;
2913
252b5132
RH
2914 case 't':
2915 case 'w':
2916 case 'E':
38487616 2917 insn.insn_opcode |= va_arg (args, int) << OP_SH_RT;
252b5132
RH
2918 continue;
2919
2920 case 'c':
38487616
TS
2921 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE;
2922 continue;
2923
252b5132
RH
2924 case 'T':
2925 case 'W':
38487616 2926 insn.insn_opcode |= va_arg (args, int) << OP_SH_FT;
252b5132
RH
2927 continue;
2928
2929 case 'd':
2930 case 'G':
af7ee8bf 2931 case 'K':
38487616 2932 insn.insn_opcode |= va_arg (args, int) << OP_SH_RD;
252b5132
RH
2933 continue;
2934
4372b673
NC
2935 case 'U':
2936 {
2937 int tmp = va_arg (args, int);
2938
38487616
TS
2939 insn.insn_opcode |= tmp << OP_SH_RT;
2940 insn.insn_opcode |= tmp << OP_SH_RD;
beae10d5 2941 continue;
4372b673
NC
2942 }
2943
252b5132
RH
2944 case 'V':
2945 case 'S':
38487616 2946 insn.insn_opcode |= va_arg (args, int) << OP_SH_FS;
252b5132
RH
2947 continue;
2948
2949 case 'z':
2950 continue;
2951
2952 case '<':
38487616 2953 insn.insn_opcode |= va_arg (args, int) << OP_SH_SHAMT;
252b5132
RH
2954 continue;
2955
2956 case 'D':
38487616 2957 insn.insn_opcode |= va_arg (args, int) << OP_SH_FD;
252b5132
RH
2958 continue;
2959
2960 case 'B':
38487616 2961 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE20;
252b5132
RH
2962 continue;
2963
4372b673 2964 case 'J':
38487616 2965 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE19;
4372b673
NC
2966 continue;
2967
252b5132 2968 case 'q':
38487616 2969 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE2;
252b5132
RH
2970 continue;
2971
2972 case 'b':
2973 case 's':
2974 case 'r':
2975 case 'v':
38487616 2976 insn.insn_opcode |= va_arg (args, int) << OP_SH_RS;
252b5132
RH
2977 continue;
2978
2979 case 'i':
2980 case 'j':
2981 case 'o':
f6688943 2982 *r = (bfd_reloc_code_real_type) va_arg (args, int);
cdf6fd85 2983 assert (*r == BFD_RELOC_GPREL16
f6688943
TS
2984 || *r == BFD_RELOC_MIPS_LITERAL
2985 || *r == BFD_RELOC_MIPS_HIGHER
2986 || *r == BFD_RELOC_HI16_S
2987 || *r == BFD_RELOC_LO16
2988 || *r == BFD_RELOC_MIPS_GOT16
2989 || *r == BFD_RELOC_MIPS_CALL16
438c16b8
TS
2990 || *r == BFD_RELOC_MIPS_GOT_DISP
2991 || *r == BFD_RELOC_MIPS_GOT_PAGE
2992 || *r == BFD_RELOC_MIPS_GOT_OFST
f6688943
TS
2993 || *r == BFD_RELOC_MIPS_GOT_LO16
2994 || *r == BFD_RELOC_MIPS_CALL_LO16
252b5132 2995 || (ep->X_op == O_subtract
f6688943 2996 && *r == BFD_RELOC_PCREL_LO16));
252b5132
RH
2997 continue;
2998
2999 case 'u':
f6688943 3000 *r = (bfd_reloc_code_real_type) va_arg (args, int);
252b5132
RH
3001 assert (ep != NULL
3002 && (ep->X_op == O_constant
3003 || (ep->X_op == O_symbol
f6688943
TS
3004 && (*r == BFD_RELOC_MIPS_HIGHEST
3005 || *r == BFD_RELOC_HI16_S
3006 || *r == BFD_RELOC_HI16
3007 || *r == BFD_RELOC_GPREL16
3008 || *r == BFD_RELOC_MIPS_GOT_HI16
3009 || *r == BFD_RELOC_MIPS_CALL_HI16))
252b5132 3010 || (ep->X_op == O_subtract
f6688943 3011 && *r == BFD_RELOC_PCREL_HI16_S)));
252b5132
RH
3012 continue;
3013
3014 case 'p':
3015 assert (ep != NULL);
3016 /*
3017 * This allows macro() to pass an immediate expression for
3018 * creating short branches without creating a symbol.
0b25d3e6
AO
3019 * Note that the expression still might come from the assembly
3020 * input, in which case the value is not checked for range nor
3021 * is a relocation entry generated (yuck).
252b5132
RH
3022 */
3023 if (ep->X_op == O_constant)
3024 {
3025 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
3026 ep = NULL;
3027 }
3028 else
0b25d3e6 3029 *r = BFD_RELOC_16_PCREL_S2;
252b5132
RH
3030 continue;
3031
3032 case 'a':
3033 assert (ep != NULL);
f6688943 3034 *r = BFD_RELOC_MIPS_JMP;
252b5132
RH
3035 continue;
3036
3037 case 'C':
3038 insn.insn_opcode |= va_arg (args, unsigned long);
3039 continue;
3040
3041 default:
3042 internalError ();
3043 }
3044 break;
3045 }
3046 va_end (args);
f6688943 3047 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 3048
5e0116d5 3049 append_insn (place, &insn, ep, r);
252b5132
RH
3050}
3051
3052static void
17a2f251
TS
3053mips16_macro_build (char *place, int *counter ATTRIBUTE_UNUSED,
3054 expressionS *ep, const char *name, const char *fmt,
3055 va_list args)
252b5132
RH
3056{
3057 struct mips_cl_insn insn;
f6688943
TS
3058 bfd_reloc_code_real_type r[3]
3059 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 3060
252b5132
RH
3061 insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
3062 assert (insn.insn_mo);
3063 assert (strcmp (name, insn.insn_mo->name) == 0);
3064
3065 while (strcmp (fmt, insn.insn_mo->args) != 0
3066 || insn.insn_mo->pinfo == INSN_MACRO)
3067 {
3068 ++insn.insn_mo;
3069 assert (insn.insn_mo->name);
3070 assert (strcmp (name, insn.insn_mo->name) == 0);
3071 }
3072
3073 insn.insn_opcode = insn.insn_mo->match;
b34976b6 3074 insn.use_extend = FALSE;
252b5132
RH
3075
3076 for (;;)
3077 {
3078 int c;
3079
3080 c = *fmt++;
3081 switch (c)
3082 {
3083 case '\0':
3084 break;
3085
3086 case ',':
3087 case '(':
3088 case ')':
3089 continue;
3090
3091 case 'y':
3092 case 'w':
3093 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
3094 continue;
3095
3096 case 'x':
3097 case 'v':
3098 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
3099 continue;
3100
3101 case 'z':
3102 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
3103 continue;
3104
3105 case 'Z':
3106 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
3107 continue;
3108
3109 case '0':
3110 case 'S':
3111 case 'P':
3112 case 'R':
3113 continue;
3114
3115 case 'X':
3116 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
3117 continue;
3118
3119 case 'Y':
3120 {
3121 int regno;
3122
3123 regno = va_arg (args, int);
3124 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
3125 insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
3126 }
3127 continue;
3128
3129 case '<':
3130 case '>':
3131 case '4':
3132 case '5':
3133 case 'H':
3134 case 'W':
3135 case 'D':
3136 case 'j':
3137 case '8':
3138 case 'V':
3139 case 'C':
3140 case 'U':
3141 case 'k':
3142 case 'K':
3143 case 'p':
3144 case 'q':
3145 {
3146 assert (ep != NULL);
3147
3148 if (ep->X_op != O_constant)
874e8986 3149 *r = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
3150 else
3151 {
b34976b6
AM
3152 mips16_immed (NULL, 0, c, ep->X_add_number, FALSE, FALSE,
3153 FALSE, &insn.insn_opcode, &insn.use_extend,
c4e7957c 3154 &insn.extend);
252b5132 3155 ep = NULL;
f6688943 3156 *r = BFD_RELOC_UNUSED;
252b5132
RH
3157 }
3158 }
3159 continue;
3160
3161 case '6':
3162 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
3163 continue;
3164 }
3165
3166 break;
3167 }
3168
f6688943 3169 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 3170
5e0116d5 3171 append_insn (place, &insn, ep, r);
252b5132
RH
3172}
3173
438c16b8
TS
3174/*
3175 * Generate a "jalr" instruction with a relocation hint to the called
3176 * function. This occurs in NewABI PIC code.
3177 */
3178static void
17a2f251 3179macro_build_jalr (int icnt, expressionS *ep)
438c16b8 3180{
685736be 3181 char *f = NULL;
b34976b6 3182
438c16b8 3183 if (HAVE_NEWABI)
f21f8242
AO
3184 {
3185 frag_grow (4);
3186 f = frag_more (0);
3187 }
17a2f251 3188 macro_build (NULL, &icnt, NULL, "jalr", "d,s", RA, PIC_CALL_REG);
438c16b8 3189 if (HAVE_NEWABI)
f21f8242 3190 fix_new_exp (frag_now, f - frag_now->fr_literal,
a105a300 3191 4, ep, FALSE, BFD_RELOC_MIPS_JALR);
438c16b8
TS
3192}
3193
252b5132
RH
3194/*
3195 * Generate a "lui" instruction.
3196 */
3197static void
17a2f251 3198macro_build_lui (char *place, int *counter, expressionS *ep, int regnum)
252b5132
RH
3199{
3200 expressionS high_expr;
3201 struct mips_cl_insn insn;
f6688943
TS
3202 bfd_reloc_code_real_type r[3]
3203 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
5a38dc70
AM
3204 const char *name = "lui";
3205 const char *fmt = "t,u";
252b5132
RH
3206
3207 assert (! mips_opts.mips16);
3208
3209 if (place == NULL)
3210 high_expr = *ep;
3211 else
3212 {
3213 high_expr.X_op = O_constant;
3214 high_expr.X_add_number = ep->X_add_number;
3215 }
3216
3217 if (high_expr.X_op == O_constant)
3218 {
3219 /* we can compute the instruction now without a relocation entry */
e7d556df
TS
3220 high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3221 >> 16) & 0xffff;
f6688943 3222 *r = BFD_RELOC_UNUSED;
252b5132 3223 }
78e1bb40 3224 else
252b5132
RH
3225 {
3226 assert (ep->X_op == O_symbol);
3227 /* _gp_disp is a special case, used from s_cpload. */
3228 assert (mips_pic == NO_PIC
78e1bb40
AO
3229 || (! HAVE_NEWABI
3230 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0));
f6688943 3231 *r = BFD_RELOC_HI16_S;
252b5132
RH
3232 }
3233
3234 /*
3235 * If the macro is about to expand into a second instruction,
3236 * print a warning if needed. We need to pass ip as a parameter
3237 * to generate a better warning message here...
3238 */
3239 if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
3240 as_warn (_("Macro instruction expanded into multiple instructions"));
3241
3242 if (place == NULL)
f9419b05 3243 ++*counter; /* bump instruction counter */
252b5132
RH
3244
3245 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3246 assert (insn.insn_mo);
3247 assert (strcmp (name, insn.insn_mo->name) == 0);
3248 assert (strcmp (fmt, insn.insn_mo->args) == 0);
3249
3250 insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
f6688943 3251 if (*r == BFD_RELOC_UNUSED)
252b5132
RH
3252 {
3253 insn.insn_opcode |= high_expr.X_add_number;
5e0116d5 3254 append_insn (place, &insn, NULL, r);
252b5132
RH
3255 }
3256 else
5e0116d5 3257 append_insn (place, &insn, &high_expr, r);
252b5132
RH
3258}
3259
885add95
CD
3260/* Generate a sequence of instructions to do a load or store from a constant
3261 offset off of a base register (breg) into/from a target register (treg),
3262 using AT if necessary. */
3263static void
17a2f251 3264macro_build_ldst_constoffset (char *place, int *counter, expressionS *ep,
256ab948 3265 const char *op, int treg, int breg, int dbl)
885add95
CD
3266{
3267 assert (ep->X_op == O_constant);
3268
256ab948 3269 /* Sign-extending 32-bit constants makes their handling easier. */
d17e7bce
TS
3270 if (! dbl && ! ((ep->X_add_number & ~((bfd_vma) 0x7fffffff))
3271 == ~((bfd_vma) 0x7fffffff)))
ae826530 3272 {
d17e7bce 3273 if (ep->X_add_number & ~((bfd_vma) 0xffffffff))
1b8e29e5 3274 as_bad (_("constant too large"));
ae826530 3275
1b8e29e5
TS
3276 ep->X_add_number = (((ep->X_add_number & 0xffffffff) ^ 0x80000000)
3277 - 0x80000000);
ae826530 3278 }
256ab948 3279
67c1ffbe 3280 /* Right now, this routine can only handle signed 32-bit constants. */
ecd13cd3 3281 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
885add95
CD
3282 as_warn (_("operand overflow"));
3283
3284 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
3285 {
3286 /* Signed 16-bit offset will fit in the op. Easy! */
17a2f251
TS
3287 macro_build (place, counter, ep, op, "t,o(b)", treg, BFD_RELOC_LO16,
3288 breg);
885add95
CD
3289 }
3290 else
3291 {
3292 /* 32-bit offset, need multiple instructions and AT, like:
3293 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
3294 addu $tempreg,$tempreg,$breg
3295 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
3296 to handle the complete offset. */
3297 macro_build_lui (place, counter, ep, AT);
3298 if (place != NULL)
3299 place += 4;
17a2f251
TS
3300 macro_build (place, counter, NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT,
3301 breg);
885add95
CD
3302 if (place != NULL)
3303 place += 4;
17a2f251
TS
3304 macro_build (place, counter, ep, op, "t,o(b)", treg, BFD_RELOC_LO16,
3305 AT);
885add95
CD
3306
3307 if (mips_opts.noat)
3308 as_warn (_("Macro used $at after \".set noat\""));
3309 }
3310}
3311
252b5132
RH
3312/* set_at()
3313 * Generates code to set the $at register to true (one)
3314 * if reg is less than the immediate expression.
3315 */
3316static void
17a2f251 3317set_at (int *counter, int reg, int unsignedp)
252b5132
RH
3318{
3319 if (imm_expr.X_op == O_constant
3320 && imm_expr.X_add_number >= -0x8000
3321 && imm_expr.X_add_number < 0x8000)
17a2f251
TS
3322 macro_build (NULL, counter, &imm_expr, unsignedp ? "sltiu" : "slti",
3323 "t,r,j", AT, reg, BFD_RELOC_LO16);
252b5132
RH
3324 else
3325 {
4d34fb5f 3326 load_register (counter, AT, &imm_expr, HAVE_64BIT_GPRS);
17a2f251 3327 macro_build (NULL, counter, NULL, unsignedp ? "sltu" : "slt",
252b5132
RH
3328 "d,v,t", AT, reg, AT);
3329 }
3330}
3331
13757d0c
TS
3332static void
3333normalize_constant_expr (expressionS *ex)
3334{
3335 if (ex->X_op == O_constant && HAVE_32BIT_GPRS)
3336 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
3337 - 0x80000000);
3338}
3339
252b5132
RH
3340/* Warn if an expression is not a constant. */
3341
3342static void
17a2f251 3343check_absolute_expr (struct mips_cl_insn *ip, expressionS *ex)
252b5132
RH
3344{
3345 if (ex->X_op == O_big)
3346 as_bad (_("unsupported large constant"));
3347 else if (ex->X_op != O_constant)
3348 as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
13757d0c
TS
3349
3350 normalize_constant_expr (ex);
252b5132
RH
3351}
3352
3353/* Count the leading zeroes by performing a binary chop. This is a
3354 bulky bit of source, but performance is a LOT better for the
3355 majority of values than a simple loop to count the bits:
3356 for (lcnt = 0; (lcnt < 32); lcnt++)
3357 if ((v) & (1 << (31 - lcnt)))
3358 break;
3359 However it is not code size friendly, and the gain will drop a bit
3360 on certain cached systems.
3361*/
3362#define COUNT_TOP_ZEROES(v) \
3363 (((v) & ~0xffff) == 0 \
3364 ? ((v) & ~0xff) == 0 \
3365 ? ((v) & ~0xf) == 0 \
3366 ? ((v) & ~0x3) == 0 \
3367 ? ((v) & ~0x1) == 0 \
3368 ? !(v) \
3369 ? 32 \
3370 : 31 \
3371 : 30 \
3372 : ((v) & ~0x7) == 0 \
3373 ? 29 \
3374 : 28 \
3375 : ((v) & ~0x3f) == 0 \
3376 ? ((v) & ~0x1f) == 0 \
3377 ? 27 \
3378 : 26 \
3379 : ((v) & ~0x7f) == 0 \
3380 ? 25 \
3381 : 24 \
3382 : ((v) & ~0xfff) == 0 \
3383 ? ((v) & ~0x3ff) == 0 \
3384 ? ((v) & ~0x1ff) == 0 \
3385 ? 23 \
3386 : 22 \
3387 : ((v) & ~0x7ff) == 0 \
3388 ? 21 \
3389 : 20 \
3390 : ((v) & ~0x3fff) == 0 \
3391 ? ((v) & ~0x1fff) == 0 \
3392 ? 19 \
3393 : 18 \
3394 : ((v) & ~0x7fff) == 0 \
3395 ? 17 \
3396 : 16 \
3397 : ((v) & ~0xffffff) == 0 \
3398 ? ((v) & ~0xfffff) == 0 \
3399 ? ((v) & ~0x3ffff) == 0 \
3400 ? ((v) & ~0x1ffff) == 0 \
3401 ? 15 \
3402 : 14 \
3403 : ((v) & ~0x7ffff) == 0 \
3404 ? 13 \
3405 : 12 \
3406 : ((v) & ~0x3fffff) == 0 \
3407 ? ((v) & ~0x1fffff) == 0 \
3408 ? 11 \
3409 : 10 \
3410 : ((v) & ~0x7fffff) == 0 \
3411 ? 9 \
3412 : 8 \
3413 : ((v) & ~0xfffffff) == 0 \
3414 ? ((v) & ~0x3ffffff) == 0 \
3415 ? ((v) & ~0x1ffffff) == 0 \
3416 ? 7 \
3417 : 6 \
3418 : ((v) & ~0x7ffffff) == 0 \
3419 ? 5 \
3420 : 4 \
3421 : ((v) & ~0x3fffffff) == 0 \
3422 ? ((v) & ~0x1fffffff) == 0 \
3423 ? 3 \
3424 : 2 \
3425 : ((v) & ~0x7fffffff) == 0 \
3426 ? 1 \
3427 : 0)
3428
3429/* load_register()
67c1ffbe 3430 * This routine generates the least number of instructions necessary to load
252b5132
RH
3431 * an absolute expression value into a register.
3432 */
3433static void
17a2f251 3434load_register (int *counter, int reg, expressionS *ep, int dbl)
252b5132
RH
3435{
3436 int freg;
3437 expressionS hi32, lo32;
3438
3439 if (ep->X_op != O_big)
3440 {
3441 assert (ep->X_op == O_constant);
256ab948
TS
3442
3443 /* Sign-extending 32-bit constants makes their handling easier. */
d17e7bce
TS
3444 if (! dbl && ! ((ep->X_add_number & ~((bfd_vma) 0x7fffffff))
3445 == ~((bfd_vma) 0x7fffffff)))
ae826530 3446 {
d17e7bce 3447 if (ep->X_add_number & ~((bfd_vma) 0xffffffff))
1b8e29e5 3448 as_bad (_("constant too large"));
ae826530 3449
1b8e29e5
TS
3450 ep->X_add_number = (((ep->X_add_number & 0xffffffff) ^ 0x80000000)
3451 - 0x80000000);
ae826530 3452 }
256ab948
TS
3453
3454 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
252b5132
RH
3455 {
3456 /* We can handle 16 bit signed values with an addiu to
3457 $zero. No need to ever use daddiu here, since $zero and
3458 the result are always correct in 32 bit mode. */
17a2f251
TS
3459 macro_build (NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3460 BFD_RELOC_LO16);
252b5132
RH
3461 return;
3462 }
3463 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3464 {
3465 /* We can handle 16 bit unsigned values with an ori to
3466 $zero. */
17a2f251
TS
3467 macro_build (NULL, counter, ep, "ori", "t,r,i", reg, 0,
3468 BFD_RELOC_LO16);
252b5132
RH
3469 return;
3470 }
256ab948 3471 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
252b5132
RH
3472 {
3473 /* 32 bit values require an lui. */
17a2f251 3474 macro_build (NULL, counter, ep, "lui", "t,u", reg, BFD_RELOC_HI16);
252b5132 3475 if ((ep->X_add_number & 0xffff) != 0)
17a2f251
TS
3476 macro_build (NULL, counter, ep, "ori", "t,r,i", reg, reg,
3477 BFD_RELOC_LO16);
252b5132
RH
3478 return;
3479 }
3480 }
3481
3482 /* The value is larger than 32 bits. */
3483
ca4e0257 3484 if (HAVE_32BIT_GPRS)
252b5132 3485 {
956cd1d6
TS
3486 as_bad (_("Number (0x%lx) larger than 32 bits"),
3487 (unsigned long) ep->X_add_number);
17a2f251
TS
3488 macro_build (NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3489 BFD_RELOC_LO16);
252b5132
RH
3490 return;
3491 }
3492
3493 if (ep->X_op != O_big)
3494 {
3495 hi32 = *ep;
3496 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3497 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3498 hi32.X_add_number &= 0xffffffff;
3499 lo32 = *ep;
3500 lo32.X_add_number &= 0xffffffff;
3501 }
3502 else
3503 {
3504 assert (ep->X_add_number > 2);
3505 if (ep->X_add_number == 3)
3506 generic_bignum[3] = 0;
3507 else if (ep->X_add_number > 4)
3508 as_bad (_("Number larger than 64 bits"));
3509 lo32.X_op = O_constant;
3510 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3511 hi32.X_op = O_constant;
3512 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3513 }
3514
3515 if (hi32.X_add_number == 0)
3516 freg = 0;
3517 else
3518 {
3519 int shift, bit;
3520 unsigned long hi, lo;
3521
956cd1d6 3522 if (hi32.X_add_number == (offsetT) 0xffffffff)
beae10d5
KH
3523 {
3524 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3525 {
17a2f251
TS
3526 macro_build (NULL, counter, &lo32, "addiu", "t,r,j", reg, 0,
3527 BFD_RELOC_LO16);
beae10d5
KH
3528 return;
3529 }
3530 if (lo32.X_add_number & 0x80000000)
3531 {
17a2f251
TS
3532 macro_build (NULL, counter, &lo32, "lui", "t,u", reg,
3533 BFD_RELOC_HI16);
252b5132 3534 if (lo32.X_add_number & 0xffff)
17a2f251
TS
3535 macro_build (NULL, counter, &lo32, "ori", "t,r,i", reg, reg,
3536 BFD_RELOC_LO16);
beae10d5
KH
3537 return;
3538 }
3539 }
252b5132
RH
3540
3541 /* Check for 16bit shifted constant. We know that hi32 is
3542 non-zero, so start the mask on the first bit of the hi32
3543 value. */
3544 shift = 17;
3545 do
beae10d5
KH
3546 {
3547 unsigned long himask, lomask;
3548
3549 if (shift < 32)
3550 {
3551 himask = 0xffff >> (32 - shift);
3552 lomask = (0xffff << shift) & 0xffffffff;
3553 }
3554 else
3555 {
3556 himask = 0xffff << (shift - 32);
3557 lomask = 0;
3558 }
3559 if ((hi32.X_add_number & ~(offsetT) himask) == 0
3560 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3561 {
3562 expressionS tmp;
3563
3564 tmp.X_op = O_constant;
3565 if (shift < 32)
3566 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3567 | (lo32.X_add_number >> shift));
3568 else
3569 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
17a2f251
TS
3570 macro_build (NULL, counter, &tmp, "ori", "t,r,i", reg, 0,
3571 BFD_RELOC_LO16);
3572 macro_build (NULL, counter, NULL,
beae10d5
KH
3573 (shift >= 32) ? "dsll32" : "dsll",
3574 "d,w,<", reg, reg,
3575 (shift >= 32) ? shift - 32 : shift);
3576 return;
3577 }
f9419b05 3578 ++shift;
beae10d5
KH
3579 }
3580 while (shift <= (64 - 16));
252b5132
RH
3581
3582 /* Find the bit number of the lowest one bit, and store the
3583 shifted value in hi/lo. */
3584 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3585 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3586 if (lo != 0)
3587 {
3588 bit = 0;
3589 while ((lo & 1) == 0)
3590 {
3591 lo >>= 1;
3592 ++bit;
3593 }
3594 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3595 hi >>= bit;
3596 }
3597 else
3598 {
3599 bit = 32;
3600 while ((hi & 1) == 0)
3601 {
3602 hi >>= 1;
3603 ++bit;
3604 }
3605 lo = hi;
3606 hi = 0;
3607 }
3608
3609 /* Optimize if the shifted value is a (power of 2) - 1. */
3610 if ((hi == 0 && ((lo + 1) & lo) == 0)
3611 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
beae10d5
KH
3612 {
3613 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
252b5132 3614 if (shift != 0)
beae10d5 3615 {
252b5132
RH
3616 expressionS tmp;
3617
3618 /* This instruction will set the register to be all
3619 ones. */
beae10d5
KH
3620 tmp.X_op = O_constant;
3621 tmp.X_add_number = (offsetT) -1;
17a2f251
TS
3622 macro_build (NULL, counter, &tmp, "addiu", "t,r,j", reg, 0,
3623 BFD_RELOC_LO16);
beae10d5
KH
3624 if (bit != 0)
3625 {
3626 bit += shift;
17a2f251 3627 macro_build (NULL, counter, NULL,
beae10d5
KH
3628 (bit >= 32) ? "dsll32" : "dsll",
3629 "d,w,<", reg, reg,
3630 (bit >= 32) ? bit - 32 : bit);
3631 }
17a2f251 3632 macro_build (NULL, counter, NULL,
252b5132 3633 (shift >= 32) ? "dsrl32" : "dsrl",
beae10d5 3634 "d,w,<", reg, reg,
252b5132 3635 (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
3636 return;
3637 }
3638 }
252b5132
RH
3639
3640 /* Sign extend hi32 before calling load_register, because we can
3641 generally get better code when we load a sign extended value. */
3642 if ((hi32.X_add_number & 0x80000000) != 0)
beae10d5 3643 hi32.X_add_number |= ~(offsetT) 0xffffffff;
252b5132
RH
3644 load_register (counter, reg, &hi32, 0);
3645 freg = reg;
3646 }
3647 if ((lo32.X_add_number & 0xffff0000) == 0)
3648 {
3649 if (freg != 0)
3650 {
17a2f251 3651 macro_build (NULL, counter, NULL, "dsll32", "d,w,<", reg, freg, 0);
252b5132
RH
3652 freg = reg;
3653 }
3654 }
3655 else
3656 {
3657 expressionS mid16;
3658
956cd1d6 3659 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
beae10d5 3660 {
17a2f251
TS
3661 macro_build (NULL, counter, &lo32, "lui", "t,u", reg,
3662 BFD_RELOC_HI16);
3663 macro_build (NULL, counter, NULL, "dsrl32", "d,w,<", reg, reg, 0);
beae10d5
KH
3664 return;
3665 }
252b5132
RH
3666
3667 if (freg != 0)
3668 {
17a2f251 3669 macro_build (NULL, counter, NULL, "dsll", "d,w,<", reg, freg, 16);
252b5132
RH
3670 freg = reg;
3671 }
3672 mid16 = lo32;
3673 mid16.X_add_number >>= 16;
17a2f251
TS
3674 macro_build (NULL, counter, &mid16, "ori", "t,r,i", reg, freg,
3675 BFD_RELOC_LO16);
3676 macro_build (NULL, counter, NULL, "dsll", "d,w,<", reg, reg, 16);
252b5132
RH
3677 freg = reg;
3678 }
3679 if ((lo32.X_add_number & 0xffff) != 0)
17a2f251
TS
3680 macro_build (NULL, counter, &lo32, "ori", "t,r,i", reg, freg,
3681 BFD_RELOC_LO16);
252b5132
RH
3682}
3683
3684/* Load an address into a register. */
3685
3686static void
17a2f251 3687load_address (int *counter, int reg, expressionS *ep, int *used_at)
252b5132 3688{
f9419b05 3689 char *p = NULL;
252b5132
RH
3690
3691 if (ep->X_op != O_constant
3692 && ep->X_op != O_symbol)
3693 {
3694 as_bad (_("expression too complex"));
3695 ep->X_op = O_constant;
3696 }
3697
3698 if (ep->X_op == O_constant)
3699 {
c9914766 3700 load_register (counter, reg, ep, HAVE_64BIT_ADDRESSES);
252b5132
RH
3701 return;
3702 }
3703
3704 if (mips_pic == NO_PIC)
3705 {
3706 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 3707 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
3708 Otherwise we want
3709 lui $reg,<sym> (BFD_RELOC_HI16_S)
3710 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
d6bc6245 3711 If we have an addend, we always use the latter form.
76b3015f 3712
d6bc6245
TS
3713 With 64bit address space and a usable $at we want
3714 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3715 lui $at,<sym> (BFD_RELOC_HI16_S)
3716 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3717 daddiu $at,<sym> (BFD_RELOC_LO16)
3718 dsll32 $reg,0
3a482fd5 3719 daddu $reg,$reg,$at
76b3015f 3720
c03099e6 3721 If $at is already in use, we use a path which is suboptimal
d6bc6245
TS
3722 on superscalar processors.
3723 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3724 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3725 dsll $reg,16
3726 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
3727 dsll $reg,16
3728 daddiu $reg,<sym> (BFD_RELOC_LO16)
3729 */
c9914766 3730 if (HAVE_64BIT_ADDRESSES)
d6bc6245 3731 {
d6bc6245
TS
3732 /* We don't do GP optimization for now because RELAX_ENCODE can't
3733 hold the data for such large chunks. */
3734
460597ba 3735 if (*used_at == 0 && ! mips_opts.noat)
d6bc6245
TS
3736 {
3737 macro_build (p, counter, ep, "lui", "t,u",
17a2f251 3738 reg, BFD_RELOC_MIPS_HIGHEST);
d6bc6245 3739 macro_build (p, counter, ep, "lui", "t,u",
17a2f251 3740 AT, BFD_RELOC_HI16_S);
d6bc6245 3741 macro_build (p, counter, ep, "daddiu", "t,r,j",
17a2f251 3742 reg, reg, BFD_RELOC_MIPS_HIGHER);
d6bc6245 3743 macro_build (p, counter, ep, "daddiu", "t,r,j",
17a2f251
TS
3744 AT, AT, BFD_RELOC_LO16);
3745 macro_build (p, counter, NULL, "dsll32", "d,w,<", reg, reg, 0);
3746 macro_build (p, counter, NULL, "daddu", "d,v,t", reg, reg, AT);
d6bc6245
TS
3747 *used_at = 1;
3748 }
3749 else
3750 {
3751 macro_build (p, counter, ep, "lui", "t,u",
17a2f251 3752 reg, BFD_RELOC_MIPS_HIGHEST);
d6bc6245 3753 macro_build (p, counter, ep, "daddiu", "t,r,j",
17a2f251
TS
3754 reg, reg, BFD_RELOC_MIPS_HIGHER);
3755 macro_build (p, counter, NULL, "dsll", "d,w,<", reg, reg, 16);
d6bc6245 3756 macro_build (p, counter, ep, "daddiu", "t,r,j",
17a2f251
TS
3757 reg, reg, BFD_RELOC_HI16_S);
3758 macro_build (p, counter, NULL, "dsll", "d,w,<", reg, reg, 16);
d6bc6245 3759 macro_build (p, counter, ep, "daddiu", "t,r,j",
17a2f251 3760 reg, reg, BFD_RELOC_LO16);
d6bc6245
TS
3761 }
3762 }
252b5132
RH
3763 else
3764 {
d6bc6245
TS
3765 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3766 && ! nopic_need_relax (ep->X_add_symbol, 1))
3767 {
3768 frag_grow (20);
17a2f251
TS
3769 macro_build (NULL, counter, ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
3770 mips_gp_register, BFD_RELOC_GPREL16);
d6bc6245
TS
3771 p = frag_var (rs_machine_dependent, 8, 0,
3772 RELAX_ENCODE (4, 8, 0, 4, 0,
3773 mips_opts.warn_about_macros),
956cd1d6 3774 ep->X_add_symbol, 0, NULL);
d6bc6245
TS
3775 }
3776 macro_build_lui (p, counter, ep, reg);
3777 if (p != NULL)
3778 p += 4;
17a2f251
TS
3779 macro_build (p, counter, ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
3780 BFD_RELOC_LO16);
d6bc6245 3781 }
252b5132
RH
3782 }
3783 else if (mips_pic == SVR4_PIC && ! mips_big_got)
3784 {
3785 expressionS ex;
3786
3787 /* If this is a reference to an external symbol, we want
3788 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3789 Otherwise we want
3790 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3791 nop
3792 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
f5040a92
AO
3793 If there is a constant, it must be added in after.
3794
ed6fb7bd 3795 If we have NewABI, we want
f5040a92
AO
3796 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
3797 unless we're referencing a global symbol with a non-zero
3798 offset, in which case cst must be added separately. */
ed6fb7bd
SC
3799 if (HAVE_NEWABI)
3800 {
f5040a92 3801 frag_grow (12);
684022ea 3802
f5040a92
AO
3803 if (ep->X_add_number)
3804 {
3805 frag_now->tc_frag_data.tc_fr_offset =
3806 ex.X_add_number = ep->X_add_number;
3807 ep->X_add_number = 0;
17a2f251
TS
3808 macro_build (NULL, counter, ep, ADDRESS_LOAD_INSN, "t,o(b)",
3809 reg, BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
3810 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3811 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3812 ex.X_op = O_constant;
17a2f251
TS
3813 macro_build (NULL, counter, &ex, ADDRESS_ADDI_INSN, "t,r,j",
3814 reg, reg, BFD_RELOC_LO16);
f5040a92
AO
3815 p = frag_var (rs_machine_dependent, 8, 0,
3816 RELAX_ENCODE (8, 4, 0, 0, 0,
3817 mips_opts.warn_about_macros),
17a2f251 3818 ep->X_add_symbol, 0, NULL);
f5040a92
AO
3819 ep->X_add_number = ex.X_add_number;
3820 }
3821
f899b4b8 3822 macro_build (p, counter, ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 3823 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
3824
3825 if (! p)
3826 {
3827 /* To avoid confusion in tc_gen_reloc, we must ensure
3828 that this does not become a variant frag. */
3829 frag_wane (frag_now);
3830 frag_new (0);
3831 }
ed6fb7bd
SC
3832 }
3833 else
3834 {
f5040a92
AO
3835 ex.X_add_number = ep->X_add_number;
3836 ep->X_add_number = 0;
3837 frag_grow (20);
17a2f251
TS
3838 macro_build (NULL, counter, ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3839 BFD_RELOC_MIPS_GOT16,
f899b4b8 3840 mips_gp_register);
17a2f251 3841 macro_build (NULL, counter, NULL, "nop", "");
ed6fb7bd
SC
3842 p = frag_var (rs_machine_dependent, 4, 0,
3843 RELAX_ENCODE (0, 4, -8, 0, 0, mips_opts.warn_about_macros),
17a2f251
TS
3844 ep->X_add_symbol, 0, NULL);
3845 macro_build (p, counter, ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
3846 BFD_RELOC_LO16);
ed6fb7bd 3847
f5040a92
AO
3848 if (ex.X_add_number != 0)
3849 {
3850 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3851 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3852 ex.X_op = O_constant;
17a2f251
TS
3853 macro_build (NULL, counter, &ex, ADDRESS_ADDI_INSN, "t,r,j",
3854 reg, reg, BFD_RELOC_LO16);
f5040a92 3855 }
252b5132
RH
3856 }
3857 }
3858 else if (mips_pic == SVR4_PIC)
3859 {
3860 expressionS ex;
3861 int off;
3862
3863 /* This is the large GOT case. If this is a reference to an
3864 external symbol, we want
3865 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
3866 addu $reg,$reg,$gp
3867 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
f5040a92
AO
3868
3869 Otherwise, for a reference to a local symbol in old ABI, we want
252b5132
RH
3870 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3871 nop
3872 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
684022ea 3873 If there is a constant, it must be added in after.
f5040a92
AO
3874
3875 In the NewABI, for local symbols, with or without offsets, we want:
438c16b8
TS
3876 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
3877 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
f5040a92 3878 */
438c16b8
TS
3879 if (HAVE_NEWABI)
3880 {
f5040a92
AO
3881 frag_grow (24);
3882
3883 frag_now->tc_frag_data.tc_fr_offset =
3884 ex.X_add_number = ep->X_add_number;
3885 ep->X_add_number = 0;
17a2f251
TS
3886 macro_build (NULL, counter, ep, "lui", "t,u", reg,
3887 BFD_RELOC_MIPS_GOT_HI16);
3888 macro_build (NULL, counter, NULL, ADDRESS_ADD_INSN, "d,v,t", reg,
3889 reg, mips_gp_register);
3890 macro_build (NULL, counter, ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3891 BFD_RELOC_MIPS_GOT_LO16, reg);
f5040a92
AO
3892 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3893 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3894 else if (ex.X_add_number)
3895 {
3896 ex.X_op = O_constant;
17a2f251
TS
3897 macro_build (NULL, counter, &ex, ADDRESS_ADDI_INSN, "t,r,j",
3898 reg, reg, BFD_RELOC_LO16);
f5040a92
AO
3899 }
3900
3901 ep->X_add_number = ex.X_add_number;
3902 p = frag_var (rs_machine_dependent, 8, 0,
3903 RELAX_ENCODE (ex.X_add_number ? 16 : 12, 8, 0, 4, 0,
3904 mips_opts.warn_about_macros),
17a2f251 3905 ep->X_add_symbol, 0, NULL);
f899b4b8 3906 macro_build (p, counter, ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251
TS
3907 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
3908 macro_build (p + 4, counter, ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
3909 reg, BFD_RELOC_MIPS_GOT_OFST);
438c16b8 3910 }
252b5132 3911 else
438c16b8 3912 {
f5040a92
AO
3913 ex.X_add_number = ep->X_add_number;
3914 ep->X_add_number = 0;
438c16b8
TS
3915 if (reg_needs_delay (mips_gp_register))
3916 off = 4;
3917 else
3918 off = 0;
3919 frag_grow (32);
17a2f251
TS
3920 macro_build (NULL, counter, ep, "lui", "t,u", reg,
3921 BFD_RELOC_MIPS_GOT_HI16);
3922 macro_build (NULL, counter, NULL, ADDRESS_ADD_INSN, "d,v,t", reg,
3923 reg, mips_gp_register);
3924 macro_build (NULL, counter, ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3925 BFD_RELOC_MIPS_GOT_LO16, reg);
438c16b8
TS
3926 p = frag_var (rs_machine_dependent, 12 + off, 0,
3927 RELAX_ENCODE (12, 12 + off, off, 8 + off, 0,
3928 mips_opts.warn_about_macros),
3929 ep->X_add_symbol, 0, NULL);
3930 if (off > 0)
3931 {
3932 /* We need a nop before loading from $gp. This special
3933 check is required because the lui which starts the main
3934 instruction stream does not refer to $gp, and so will not
3935 insert the nop which may be required. */
17a2f251 3936 macro_build (p, counter, NULL, "nop", "");
438c16b8
TS
3937 p += 4;
3938 }
f899b4b8 3939 macro_build (p, counter, ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 3940 BFD_RELOC_MIPS_GOT16, mips_gp_register);
438c16b8 3941 p += 4;
17a2f251 3942 macro_build (p, counter, NULL, "nop", "");
252b5132 3943 p += 4;
17a2f251
TS
3944 macro_build (p, counter, ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
3945 BFD_RELOC_LO16);
438c16b8 3946
f5040a92
AO
3947 if (ex.X_add_number != 0)
3948 {
3949 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3950 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3951 ex.X_op = O_constant;
17a2f251
TS
3952 macro_build (NULL, counter, &ex, ADDRESS_ADDI_INSN, "t,r,j",
3953 reg, reg, BFD_RELOC_LO16);
f5040a92 3954 }
252b5132
RH
3955 }
3956 }
3957 else if (mips_pic == EMBEDDED_PIC)
3958 {
3959 /* We always do
cdf6fd85 3960 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
c9914766 3961 */
17a2f251
TS
3962 macro_build (NULL, counter, ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
3963 mips_gp_register, BFD_RELOC_GPREL16);
252b5132
RH
3964 }
3965 else
3966 abort ();
3967}
3968
ea1fb5dc
RS
3969/* Move the contents of register SOURCE into register DEST. */
3970
3971static void
17a2f251 3972move_register (int *counter, int dest, int source)
ea1fb5dc 3973{
17a2f251 3974 macro_build (NULL, counter, NULL, HAVE_32BIT_GPRS ? "addu" : "daddu",
ea1fb5dc
RS
3975 "d,v,t", dest, source, 0);
3976}
3977
252b5132
RH
3978/*
3979 * Build macros
3980 * This routine implements the seemingly endless macro or synthesized
3981 * instructions and addressing modes in the mips assembly language. Many
3982 * of these macros are simple and are similar to each other. These could
67c1ffbe 3983 * probably be handled by some kind of table or grammar approach instead of
252b5132
RH
3984 * this verbose method. Others are not simple macros but are more like
3985 * optimizing code generation.
3986 * One interesting optimization is when several store macros appear
67c1ffbe 3987 * consecutively that would load AT with the upper half of the same address.
252b5132
RH
3988 * The ensuing load upper instructions are ommited. This implies some kind
3989 * of global optimization. We currently only optimize within a single macro.
3990 * For many of the load and store macros if the address is specified as a
3991 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
3992 * first load register 'at' with zero and use it as the base register. The
3993 * mips assembler simply uses register $zero. Just one tiny optimization
3994 * we're missing.
3995 */
3996static void
17a2f251 3997macro (struct mips_cl_insn *ip)
252b5132
RH
3998{
3999 register int treg, sreg, dreg, breg;
4000 int tempreg;
4001 int mask;
4002 int icnt = 0;
43841e91 4003 int used_at = 0;
252b5132
RH
4004 expressionS expr1;
4005 const char *s;
4006 const char *s2;
4007 const char *fmt;
4008 int likely = 0;
4009 int dbl = 0;
4010 int coproc = 0;
4011 int lr = 0;
4012 int imm = 0;
1abe91b1 4013 int call = 0;
252b5132
RH
4014 offsetT maxnum;
4015 int off;
4016 bfd_reloc_code_real_type r;
252b5132
RH
4017 int hold_mips_optimize;
4018
4019 assert (! mips_opts.mips16);
4020
4021 treg = (ip->insn_opcode >> 16) & 0x1f;
4022 dreg = (ip->insn_opcode >> 11) & 0x1f;
4023 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
4024 mask = ip->insn_mo->mask;
4025
4026 expr1.X_op = O_constant;
4027 expr1.X_op_symbol = NULL;
4028 expr1.X_add_symbol = NULL;
4029 expr1.X_add_number = 1;
4030
67c1ffbe 4031 /* Unmatched fixups should not be put in the same frag as a relaxable
5919d012
RS
4032 macro. For example, suppose we have:
4033
4034 lui $4,%hi(l1) # 1
4035 la $5,l2 # 2
4036 addiu $4,$4,%lo(l1) # 3
4037
4038 If instructions 1 and 2 were put in the same frag, md_frob_file would
4039 move the fixup for #1 after the fixups for the "unrelaxed" version of
4040 #2. This would confuse tc_gen_reloc, which expects the relocations
4041 for #2 to be the last for that frag.
4042
64bdfcaf
RS
4043 Also, if tc_gen_reloc sees certain relocations in a variant frag,
4044 it assumes that they belong to a relaxable macro. We mustn't put
4045 other uses of such relocations into a variant frag.
4046
4047 To avoid both problems, finish the current frag it contains a
4048 %reloc() operator. The macro then goes into a new frag. */
4049 if (prev_reloc_op_frag == frag_now)
5919d012
RS
4050 {
4051 frag_wane (frag_now);
4052 frag_new (0);
4053 }
4054
252b5132
RH
4055 switch (mask)
4056 {
4057 case M_DABS:
4058 dbl = 1;
4059 case M_ABS:
4060 /* bgez $a0,.+12
4061 move v0,$a0
4062 sub v0,$zero,$a0
4063 */
4064
b34976b6 4065 mips_emit_delays (TRUE);
252b5132
RH
4066 ++mips_opts.noreorder;
4067 mips_any_noreorder = 1;
4068
4069 expr1.X_add_number = 8;
17a2f251 4070 macro_build (NULL, &icnt, &expr1, "bgez", "s,p", sreg);
252b5132 4071 if (dreg == sreg)
17a2f251 4072 macro_build (NULL, &icnt, NULL, "nop", "", 0);
252b5132 4073 else
ea1fb5dc 4074 move_register (&icnt, dreg, sreg);
17a2f251
TS
4075 macro_build (NULL, &icnt, NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, 0,
4076 sreg);
252b5132
RH
4077
4078 --mips_opts.noreorder;
4079 return;
4080
4081 case M_ADD_I:
4082 s = "addi";
4083 s2 = "add";
4084 goto do_addi;
4085 case M_ADDU_I:
4086 s = "addiu";
4087 s2 = "addu";
4088 goto do_addi;
4089 case M_DADD_I:
4090 dbl = 1;
4091 s = "daddi";
4092 s2 = "dadd";
4093 goto do_addi;
4094 case M_DADDU_I:
4095 dbl = 1;
4096 s = "daddiu";
4097 s2 = "daddu";
4098 do_addi:
4099 if (imm_expr.X_op == O_constant
4100 && imm_expr.X_add_number >= -0x8000
4101 && imm_expr.X_add_number < 0x8000)
4102 {
17a2f251
TS
4103 macro_build (NULL, &icnt, &imm_expr, s, "t,r,j", treg, sreg,
4104 BFD_RELOC_LO16);
252b5132
RH
4105 return;
4106 }
4107 load_register (&icnt, AT, &imm_expr, dbl);
17a2f251 4108 macro_build (NULL, &icnt, NULL, s2, "d,v,t", treg, sreg, AT);
252b5132
RH
4109 break;
4110
4111 case M_AND_I:
4112 s = "andi";
4113 s2 = "and";
4114 goto do_bit;
4115 case M_OR_I:
4116 s = "ori";
4117 s2 = "or";
4118 goto do_bit;
4119 case M_NOR_I:
4120 s = "";
4121 s2 = "nor";
4122 goto do_bit;
4123 case M_XOR_I:
4124 s = "xori";
4125 s2 = "xor";
4126 do_bit:
4127 if (imm_expr.X_op == O_constant
4128 && imm_expr.X_add_number >= 0
4129 && imm_expr.X_add_number < 0x10000)
4130 {
4131 if (mask != M_NOR_I)
17a2f251
TS
4132 macro_build (NULL, &icnt, &imm_expr, s, "t,r,i", treg, sreg,
4133 BFD_RELOC_LO16);
252b5132
RH
4134 else
4135 {
17a2f251
TS
4136 macro_build (NULL, &icnt, &imm_expr, "ori", "t,r,i", treg, sreg,
4137 BFD_RELOC_LO16);
4138 macro_build (NULL, &icnt, NULL, "nor", "d,v,t", treg, treg, 0);
252b5132
RH
4139 }
4140 return;
4141 }
4142
d6bc6245 4143 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
17a2f251 4144 macro_build (NULL, &icnt, NULL, s2, "d,v,t", treg, sreg, AT);
252b5132
RH
4145 break;
4146
4147 case M_BEQ_I:
4148 s = "beq";
4149 goto beq_i;
4150 case M_BEQL_I:
4151 s = "beql";
4152 likely = 1;
4153 goto beq_i;
4154 case M_BNE_I:
4155 s = "bne";
4156 goto beq_i;
4157 case M_BNEL_I:
4158 s = "bnel";
4159 likely = 1;
4160 beq_i:
4161 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4162 {
17a2f251 4163 macro_build (NULL, &icnt, &offset_expr, s, "s,t,p", sreg, 0);
252b5132
RH
4164 return;
4165 }
4d34fb5f 4166 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
17a2f251 4167 macro_build (NULL, &icnt, &offset_expr, s, "s,t,p", sreg, AT);
252b5132
RH
4168 break;
4169
4170 case M_BGEL:
4171 likely = 1;
4172 case M_BGE:
4173 if (treg == 0)
4174 {
17a2f251
TS
4175 macro_build (NULL, &icnt, &offset_expr, likely ? "bgezl" : "bgez",
4176 "s,p", sreg);
252b5132
RH
4177 return;
4178 }
4179 if (sreg == 0)
4180 {
17a2f251
TS
4181 macro_build (NULL, &icnt, &offset_expr, likely ? "blezl" : "blez",
4182 "s,p", treg);
252b5132
RH
4183 return;
4184 }
17a2f251
TS
4185 macro_build (NULL, &icnt, NULL, "slt", "d,v,t", AT, sreg, treg);
4186 macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4187 "s,t,p", AT, 0);
252b5132
RH
4188 break;
4189
4190 case M_BGTL_I:
4191 likely = 1;
4192 case M_BGT_I:
4193 /* check for > max integer */
4194 maxnum = 0x7fffffff;
ca4e0257 4195 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
4196 {
4197 maxnum <<= 16;
4198 maxnum |= 0xffff;
4199 maxnum <<= 16;
4200 maxnum |= 0xffff;
4201 }
4202 if (imm_expr.X_op == O_constant
4203 && imm_expr.X_add_number >= maxnum
ca4e0257 4204 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
4205 {
4206 do_false:
4207 /* result is always false */
4208 if (! likely)
4209 {
39c0a331
L
4210 if (warn_nops)
4211 as_warn (_("Branch %s is always false (nop)"),
4212 ip->insn_mo->name);
17a2f251 4213 macro_build (NULL, &icnt, NULL, "nop", "", 0);
252b5132
RH
4214 }
4215 else
4216 {
39c0a331
L
4217 if (warn_nops)
4218 as_warn (_("Branch likely %s is always false"),
4219 ip->insn_mo->name);
17a2f251 4220 macro_build (NULL, &icnt, &offset_expr, "bnel", "s,t,p", 0, 0);
252b5132
RH
4221 }
4222 return;
4223 }
4224 if (imm_expr.X_op != O_constant)
4225 as_bad (_("Unsupported large constant"));
f9419b05 4226 ++imm_expr.X_add_number;
252b5132
RH
4227 /* FALLTHROUGH */
4228 case M_BGE_I:
4229 case M_BGEL_I:
4230 if (mask == M_BGEL_I)
4231 likely = 1;
4232 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4233 {
17a2f251
TS
4234 macro_build (NULL, &icnt, &offset_expr, likely ? "bgezl" : "bgez",
4235 "s,p", sreg);
252b5132
RH
4236 return;
4237 }
4238 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4239 {
17a2f251
TS
4240 macro_build (NULL, &icnt, &offset_expr, likely ? "bgtzl" : "bgtz",
4241 "s,p", sreg);
252b5132
RH
4242 return;
4243 }
4244 maxnum = 0x7fffffff;
ca4e0257 4245 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
4246 {
4247 maxnum <<= 16;
4248 maxnum |= 0xffff;
4249 maxnum <<= 16;
4250 maxnum |= 0xffff;
4251 }
4252 maxnum = - maxnum - 1;
4253 if (imm_expr.X_op == O_constant
4254 && imm_expr.X_add_number <= maxnum
ca4e0257 4255 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
4256 {
4257 do_true:
4258 /* result is always true */
4259 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
17a2f251 4260 macro_build (NULL, &icnt, &offset_expr, "b", "p");
252b5132
RH
4261 return;
4262 }
4263 set_at (&icnt, sreg, 0);
17a2f251
TS
4264 macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4265 "s,t,p", AT, 0);
252b5132
RH
4266 break;
4267
4268 case M_BGEUL:
4269 likely = 1;
4270 case M_BGEU:
4271 if (treg == 0)
4272 goto do_true;
4273 if (sreg == 0)
4274 {
17a2f251
TS
4275 macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4276 "s,t,p", 0, treg);
252b5132
RH
4277 return;
4278 }
17a2f251
TS
4279 macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", AT, sreg, treg);
4280 macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4281 "s,t,p", AT, 0);
252b5132
RH
4282 break;
4283
4284 case M_BGTUL_I:
4285 likely = 1;
4286 case M_BGTU_I:
4287 if (sreg == 0
ca4e0257 4288 || (HAVE_32BIT_GPRS
252b5132 4289 && imm_expr.X_op == O_constant
956cd1d6 4290 && imm_expr.X_add_number == (offsetT) 0xffffffff))
252b5132
RH
4291 goto do_false;
4292 if (imm_expr.X_op != O_constant)
4293 as_bad (_("Unsupported large constant"));
f9419b05 4294 ++imm_expr.X_add_number;
252b5132
RH
4295 /* FALLTHROUGH */
4296 case M_BGEU_I:
4297 case M_BGEUL_I:
4298 if (mask == M_BGEUL_I)
4299 likely = 1;
4300 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4301 goto do_true;
4302 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4303 {
17a2f251
TS
4304 macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4305 "s,t,p", sreg, 0);
252b5132
RH
4306 return;
4307 }
4308 set_at (&icnt, sreg, 1);
17a2f251
TS
4309 macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4310 "s,t,p", AT, 0);
252b5132
RH
4311 break;
4312
4313 case M_BGTL:
4314 likely = 1;
4315 case M_BGT:
4316 if (treg == 0)
4317 {
17a2f251
TS
4318 macro_build (NULL, &icnt, &offset_expr, likely ? "bgtzl" : "bgtz",
4319 "s,p", sreg);
252b5132
RH
4320 return;
4321 }
4322 if (sreg == 0)
4323 {
17a2f251
TS
4324 macro_build (NULL, &icnt, &offset_expr, likely ? "bltzl" : "bltz",
4325 "s,p", treg);
252b5132
RH
4326 return;
4327 }
17a2f251
TS
4328 macro_build (NULL, &icnt, NULL, "slt", "d,v,t", AT, treg, sreg);
4329 macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4330 "s,t,p", AT, 0);
252b5132
RH
4331 break;
4332
4333 case M_BGTUL:
4334 likely = 1;
4335 case M_BGTU:
4336 if (treg == 0)
4337 {
17a2f251
TS
4338 macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4339 "s,t,p", sreg, 0);
252b5132
RH
4340 return;
4341 }
4342 if (sreg == 0)
4343 goto do_false;
17a2f251
TS
4344 macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", AT, treg, sreg);
4345 macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4346 "s,t,p", AT, 0);
252b5132
RH
4347 break;
4348
4349 case M_BLEL:
4350 likely = 1;
4351 case M_BLE:
4352 if (treg == 0)
4353 {
17a2f251
TS
4354 macro_build (NULL, &icnt, &offset_expr, likely ? "blezl" : "blez",
4355 "s,p", sreg);
252b5132
RH
4356 return;
4357 }
4358 if (sreg == 0)
4359 {
17a2f251
TS
4360 macro_build (NULL, &icnt, &offset_expr, likely ? "bgezl" : "bgez",
4361 "s,p", treg);
252b5132
RH
4362 return;
4363 }
17a2f251
TS
4364 macro_build (NULL, &icnt, NULL, "slt", "d,v,t", AT, treg, sreg);
4365 macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4366 "s,t,p", AT, 0);
252b5132
RH
4367 break;
4368
4369 case M_BLEL_I:
4370 likely = 1;
4371 case M_BLE_I:
4372 maxnum = 0x7fffffff;
ca4e0257 4373 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
4374 {
4375 maxnum <<= 16;
4376 maxnum |= 0xffff;
4377 maxnum <<= 16;
4378 maxnum |= 0xffff;
4379 }
4380 if (imm_expr.X_op == O_constant
4381 && imm_expr.X_add_number >= maxnum
ca4e0257 4382 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
4383 goto do_true;
4384 if (imm_expr.X_op != O_constant)
4385 as_bad (_("Unsupported large constant"));
f9419b05 4386 ++imm_expr.X_add_number;
252b5132
RH
4387 /* FALLTHROUGH */
4388 case M_BLT_I:
4389 case M_BLTL_I:
4390 if (mask == M_BLTL_I)
4391 likely = 1;
4392 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4393 {
17a2f251
TS
4394 macro_build (NULL, &icnt, &offset_expr, likely ? "bltzl" : "bltz",
4395 "s,p", sreg);
252b5132
RH
4396 return;
4397 }
4398 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4399 {
17a2f251
TS
4400 macro_build (NULL, &icnt, &offset_expr, likely ? "blezl" : "blez",
4401 "s,p", sreg);
252b5132
RH
4402 return;
4403 }
4404 set_at (&icnt, sreg, 0);
17a2f251
TS
4405 macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4406 "s,t,p", AT, 0);
252b5132
RH
4407 break;
4408
4409 case M_BLEUL:
4410 likely = 1;
4411 case M_BLEU:
4412 if (treg == 0)
4413 {
17a2f251
TS
4414 macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4415 "s,t,p", sreg, 0);
252b5132
RH
4416 return;
4417 }
4418 if (sreg == 0)
4419 goto do_true;
17a2f251
TS
4420 macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", AT, treg, sreg);
4421 macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4422 "s,t,p", AT, 0);
252b5132
RH
4423 break;
4424
4425 case M_BLEUL_I:
4426 likely = 1;
4427 case M_BLEU_I:
4428 if (sreg == 0
ca4e0257 4429 || (HAVE_32BIT_GPRS
252b5132 4430 && imm_expr.X_op == O_constant
956cd1d6 4431 && imm_expr.X_add_number == (offsetT) 0xffffffff))
252b5132
RH
4432 goto do_true;
4433 if (imm_expr.X_op != O_constant)
4434 as_bad (_("Unsupported large constant"));
f9419b05 4435 ++imm_expr.X_add_number;
252b5132
RH
4436 /* FALLTHROUGH */
4437 case M_BLTU_I:
4438 case M_BLTUL_I:
4439 if (mask == M_BLTUL_I)
4440 likely = 1;
4441 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4442 goto do_false;
4443 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4444 {
17a2f251 4445 macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
252b5132
RH
4446 "s,t,p", sreg, 0);
4447 return;
4448 }
4449 set_at (&icnt, sreg, 1);
17a2f251
TS
4450 macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4451 "s,t,p", AT, 0);
252b5132
RH
4452 break;
4453
4454 case M_BLTL:
4455 likely = 1;
4456 case M_BLT:
4457 if (treg == 0)
4458 {
17a2f251
TS
4459 macro_build (NULL, &icnt, &offset_expr, likely ? "bltzl" : "bltz",
4460 "s,p", sreg);
252b5132
RH
4461 return;
4462 }
4463 if (sreg == 0)
4464 {
17a2f251
TS
4465 macro_build (NULL, &icnt, &offset_expr, likely ? "bgtzl" : "bgtz",
4466 "s,p", treg);
252b5132
RH
4467 return;
4468 }
17a2f251
TS
4469 macro_build (NULL, &icnt, NULL, "slt", "d,v,t", AT, sreg, treg);
4470 macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4471 "s,t,p", AT, 0);
252b5132
RH
4472 break;
4473
4474 case M_BLTUL:
4475 likely = 1;
4476 case M_BLTU:
4477 if (treg == 0)
4478 goto do_false;
4479 if (sreg == 0)
4480 {
17a2f251
TS
4481 macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4482 "s,t,p", 0, treg);
252b5132
RH
4483 return;
4484 }
17a2f251
TS
4485 macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", AT, sreg, treg);
4486 macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4487 "s,t,p", AT, 0);
252b5132
RH
4488 break;
4489
5f74bc13
CD
4490 case M_DEXT:
4491 {
4492 unsigned long pos;
4493 unsigned long size;
4494
4495 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
4496 {
4497 as_bad (_("Unsupported large constant"));
4498 pos = size = 1;
4499 }
4500 else
4501 {
4502 pos = (unsigned long) imm_expr.X_add_number;
4503 size = (unsigned long) imm2_expr.X_add_number;
4504 }
4505
4506 if (pos > 63)
4507 {
4508 as_bad (_("Improper position (%lu)"), pos);
4509 pos = 1;
4510 }
4511 if (size == 0 || size > 64
4512 || (pos + size - 1) > 63)
4513 {
4514 as_bad (_("Improper extract size (%lu, position %lu)"),
4515 size, pos);
4516 size = 1;
4517 }
4518
4519 if (size <= 32 && pos < 32)
4520 {
4521 s = "dext";
4522 fmt = "t,r,+A,+C";
4523 }
4524 else if (size <= 32)
4525 {
4526 s = "dextu";
4527 fmt = "t,r,+E,+H";
4528 }
4529 else
4530 {
4531 s = "dextm";
4532 fmt = "t,r,+A,+G";
4533 }
4534 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s,
4535 fmt, treg, sreg, pos, size - 1);
4536 }
4537 return;
4538
4539 case M_DINS:
4540 {
4541 unsigned long pos;
4542 unsigned long size;
4543
4544 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
4545 {
4546 as_bad (_("Unsupported large constant"));
4547 pos = size = 1;
4548 }
4549 else
4550 {
4551 pos = (unsigned long) imm_expr.X_add_number;
4552 size = (unsigned long) imm2_expr.X_add_number;
4553 }
4554
4555 if (pos > 63)
4556 {
4557 as_bad (_("Improper position (%lu)"), pos);
4558 pos = 1;
4559 }
4560 if (size == 0 || size > 64
4561 || (pos + size - 1) > 63)
4562 {
4563 as_bad (_("Improper insert size (%lu, position %lu)"),
4564 size, pos);
4565 size = 1;
4566 }
4567
4568 if (pos < 32 && (pos + size - 1) < 32)
4569 {
4570 s = "dins";
4571 fmt = "t,r,+A,+B";
4572 }
4573 else if (pos >= 32)
4574 {
4575 s = "dinsu";
4576 fmt = "t,r,+E,+F";
4577 }
4578 else
4579 {
4580 s = "dinsm";
4581 fmt = "t,r,+A,+F";
4582 }
4583 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s,
4584 fmt, treg, sreg, pos, pos + size - 1);
4585 }
4586 return;
4587
252b5132
RH
4588 case M_DDIV_3:
4589 dbl = 1;
4590 case M_DIV_3:
4591 s = "mflo";
4592 goto do_div3;
4593 case M_DREM_3:
4594 dbl = 1;
4595 case M_REM_3:
4596 s = "mfhi";
4597 do_div3:
4598 if (treg == 0)
4599 {
4600 as_warn (_("Divide by zero."));
4601 if (mips_trap)
17a2f251 4602 macro_build (NULL, &icnt, NULL, "teq", "s,t,q", 0, 0, 7);
252b5132 4603 else
17a2f251 4604 macro_build (NULL, &icnt, NULL, "break", "c", 7);
252b5132
RH
4605 return;
4606 }
4607
b34976b6 4608 mips_emit_delays (TRUE);
252b5132
RH
4609 ++mips_opts.noreorder;
4610 mips_any_noreorder = 1;
4611 if (mips_trap)
4612 {
17a2f251
TS
4613 macro_build (NULL, &icnt, NULL, "teq", "s,t,q", treg, 0, 7);
4614 macro_build (NULL, &icnt, NULL, dbl ? "ddiv" : "div", "z,s,t",
4615 sreg, treg);
252b5132
RH
4616 }
4617 else
4618 {
4619 expr1.X_add_number = 8;
17a2f251
TS
4620 macro_build (NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4621 macro_build (NULL, &icnt, NULL, dbl ? "ddiv" : "div", "z,s,t",
4622 sreg, treg);
bf151ce7 4623 macro_build (NULL, &icnt, NULL, "break", "c", 7);
252b5132
RH
4624 }
4625 expr1.X_add_number = -1;
17a2f251
TS
4626 macro_build (NULL, &icnt, &expr1, dbl ? "daddiu" : "addiu", "t,r,j",
4627 AT, 0, BFD_RELOC_LO16);
252b5132 4628 expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
17a2f251 4629 macro_build (NULL, &icnt, &expr1, "bne", "s,t,p", treg, AT);
252b5132
RH
4630 if (dbl)
4631 {
4632 expr1.X_add_number = 1;
17a2f251
TS
4633 macro_build (NULL, &icnt, &expr1, "daddiu", "t,r,j", AT, 0,
4634 BFD_RELOC_LO16);
4635 macro_build (NULL, &icnt, NULL, "dsll32", "d,w,<", AT, AT, 31);
252b5132
RH
4636 }
4637 else
4638 {
4639 expr1.X_add_number = 0x80000000;
17a2f251
TS
4640 macro_build (NULL, &icnt, &expr1, "lui", "t,u", AT,
4641 BFD_RELOC_HI16);
252b5132
RH
4642 }
4643 if (mips_trap)
4644 {
17a2f251 4645 macro_build (NULL, &icnt, NULL, "teq", "s,t,q", sreg, AT, 6);
252b5132
RH
4646 /* We want to close the noreorder block as soon as possible, so
4647 that later insns are available for delay slot filling. */
4648 --mips_opts.noreorder;
4649 }
4650 else
4651 {
4652 expr1.X_add_number = 8;
17a2f251
TS
4653 macro_build (NULL, &icnt, &expr1, "bne", "s,t,p", sreg, AT);
4654 macro_build (NULL, &icnt, NULL, "nop", "", 0);
252b5132
RH
4655
4656 /* We want to close the noreorder block as soon as possible, so
4657 that later insns are available for delay slot filling. */
4658 --mips_opts.noreorder;
4659
17a2f251 4660 macro_build (NULL, &icnt, NULL, "break", "c", 6);
252b5132 4661 }
17a2f251 4662 macro_build (NULL, &icnt, NULL, s, "d", dreg);
252b5132
RH
4663 break;
4664
4665 case M_DIV_3I:
4666 s = "div";
4667 s2 = "mflo";
4668 goto do_divi;
4669 case M_DIVU_3I:
4670 s = "divu";
4671 s2 = "mflo";
4672 goto do_divi;
4673 case M_REM_3I:
4674 s = "div";
4675 s2 = "mfhi";
4676 goto do_divi;
4677 case M_REMU_3I:
4678 s = "divu";
4679 s2 = "mfhi";
4680 goto do_divi;
4681 case M_DDIV_3I:
4682 dbl = 1;
4683 s = "ddiv";
4684 s2 = "mflo";
4685 goto do_divi;
4686 case M_DDIVU_3I:
4687 dbl = 1;
4688 s = "ddivu";
4689 s2 = "mflo";
4690 goto do_divi;
4691 case M_DREM_3I:
4692 dbl = 1;
4693 s = "ddiv";
4694 s2 = "mfhi";
4695 goto do_divi;
4696 case M_DREMU_3I:
4697 dbl = 1;
4698 s = "ddivu";
4699 s2 = "mfhi";
4700 do_divi:
4701 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4702 {
4703 as_warn (_("Divide by zero."));
4704 if (mips_trap)
17a2f251 4705 macro_build (NULL, &icnt, NULL, "teq", "s,t,q", 0, 0, 7);
252b5132 4706 else
17a2f251 4707 macro_build (NULL, &icnt, NULL, "break", "c", 7);
252b5132
RH
4708 return;
4709 }
4710 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4711 {
4712 if (strcmp (s2, "mflo") == 0)
ea1fb5dc 4713 move_register (&icnt, dreg, sreg);
252b5132 4714 else
ea1fb5dc 4715 move_register (&icnt, dreg, 0);
252b5132
RH
4716 return;
4717 }
4718 if (imm_expr.X_op == O_constant
4719 && imm_expr.X_add_number == -1
4720 && s[strlen (s) - 1] != 'u')
4721 {
4722 if (strcmp (s2, "mflo") == 0)
4723 {
17a2f251
TS
4724 macro_build (NULL, &icnt, NULL, dbl ? "dneg" : "neg", "d,w",
4725 dreg, sreg);
252b5132
RH
4726 }
4727 else
ea1fb5dc 4728 move_register (&icnt, dreg, 0);
252b5132
RH
4729 return;
4730 }
4731
4732 load_register (&icnt, AT, &imm_expr, dbl);
17a2f251
TS
4733 macro_build (NULL, &icnt, NULL, s, "z,s,t", sreg, AT);
4734 macro_build (NULL, &icnt, NULL, s2, "d", dreg);
252b5132
RH
4735 break;
4736
4737 case M_DIVU_3:
4738 s = "divu";
4739 s2 = "mflo";
4740 goto do_divu3;
4741 case M_REMU_3:
4742 s = "divu";
4743 s2 = "mfhi";
4744 goto do_divu3;
4745 case M_DDIVU_3:
4746 s = "ddivu";
4747 s2 = "mflo";
4748 goto do_divu3;
4749 case M_DREMU_3:
4750 s = "ddivu";
4751 s2 = "mfhi";
4752 do_divu3:
b34976b6 4753 mips_emit_delays (TRUE);
252b5132
RH
4754 ++mips_opts.noreorder;
4755 mips_any_noreorder = 1;
4756 if (mips_trap)
4757 {
17a2f251
TS
4758 macro_build (NULL, &icnt, NULL, "teq", "s,t,q", treg, 0, 7);
4759 macro_build (NULL, &icnt, NULL, s, "z,s,t", sreg, treg);
252b5132
RH
4760 /* We want to close the noreorder block as soon as possible, so
4761 that later insns are available for delay slot filling. */
4762 --mips_opts.noreorder;
4763 }
4764 else
4765 {
4766 expr1.X_add_number = 8;
17a2f251
TS
4767 macro_build (NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4768 macro_build (NULL, &icnt, NULL, s, "z,s,t", sreg, treg);
252b5132
RH
4769
4770 /* We want to close the noreorder block as soon as possible, so
4771 that later insns are available for delay slot filling. */
4772 --mips_opts.noreorder;
17a2f251 4773 macro_build (NULL, &icnt, NULL, "break", "c", 7);
252b5132 4774 }
17a2f251 4775 macro_build (NULL, &icnt, NULL, s2, "d", dreg);
252b5132
RH
4776 return;
4777
1abe91b1
MR
4778 case M_DLCA_AB:
4779 dbl = 1;
4780 case M_LCA_AB:
4781 call = 1;
4782 goto do_la;
252b5132
RH
4783 case M_DLA_AB:
4784 dbl = 1;
4785 case M_LA_AB:
1abe91b1 4786 do_la:
252b5132
RH
4787 /* Load the address of a symbol into a register. If breg is not
4788 zero, we then add a base register to it. */
4789
3bec30a8
TS
4790 if (dbl && HAVE_32BIT_GPRS)
4791 as_warn (_("dla used to load 32-bit register"));
4792
c90bbe5b 4793 if (! dbl && HAVE_64BIT_OBJECTS)
3bec30a8
TS
4794 as_warn (_("la used to load 64-bit address"));
4795
0c11417f
MR
4796 if (offset_expr.X_op == O_constant
4797 && offset_expr.X_add_number >= -0x8000
4798 && offset_expr.X_add_number < 0x8000)
4799 {
17a2f251 4800 macro_build (NULL, &icnt, &offset_expr,
b7c7d6c1 4801 (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
17a2f251 4802 "t,r,j", treg, sreg, BFD_RELOC_LO16);
0c11417f
MR
4803 return;
4804 }
4805
afdbd6d0
CD
4806 if (treg == breg)
4807 {
4808 tempreg = AT;
4809 used_at = 1;
4810 }
4811 else
4812 {
4813 tempreg = treg;
4814 used_at = 0;
4815 }
4816
252b5132
RH
4817 /* When generating embedded PIC code, we permit expressions of
4818 the form
afdbd6d0
CD
4819 la $treg,foo-bar
4820 la $treg,foo-bar($breg)
bb2d6cd7 4821 where bar is an address in the current section. These are used
252b5132
RH
4822 when getting the addresses of functions. We don't permit
4823 X_add_number to be non-zero, because if the symbol is
4824 external the relaxing code needs to know that any addend is
4825 purely the offset to X_op_symbol. */
4826 if (mips_pic == EMBEDDED_PIC
4827 && offset_expr.X_op == O_subtract
49309057 4828 && (symbol_constant_p (offset_expr.X_op_symbol)
bb2d6cd7 4829 ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
49309057
ILT
4830 : (symbol_equated_p (offset_expr.X_op_symbol)
4831 && (S_GET_SEGMENT
4832 (symbol_get_value_expression (offset_expr.X_op_symbol)
4833 ->X_add_symbol)
bb2d6cd7 4834 == now_seg)))
bb2d6cd7
GK
4835 && (offset_expr.X_add_number == 0
4836 || OUTPUT_FLAVOR == bfd_target_elf_flavour))
252b5132 4837 {
afdbd6d0
CD
4838 if (breg == 0)
4839 {
4840 tempreg = treg;
4841 used_at = 0;
17a2f251
TS
4842 macro_build (NULL, &icnt, &offset_expr, "lui", "t,u", tempreg,
4843 BFD_RELOC_PCREL_HI16_S);
afdbd6d0
CD
4844 }
4845 else
4846 {
17a2f251
TS
4847 macro_build (NULL, &icnt, &offset_expr, "lui", "t,u", tempreg,
4848 BFD_RELOC_PCREL_HI16_S);
4849 macro_build (NULL, &icnt, NULL,
4d34fb5f 4850 (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu",
afdbd6d0
CD
4851 "d,v,t", tempreg, tempreg, breg);
4852 }
17a2f251 4853 macro_build (NULL, &icnt, &offset_expr,
4d34fb5f 4854 (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
17a2f251 4855 "t,r,j", treg, tempreg, BFD_RELOC_PCREL_LO16);
afdbd6d0
CD
4856 if (! used_at)
4857 return;
4858 break;
252b5132
RH
4859 }
4860
4861 if (offset_expr.X_op != O_symbol
4862 && offset_expr.X_op != O_constant)
4863 {
4864 as_bad (_("expression too complex"));
4865 offset_expr.X_op = O_constant;
4866 }
4867
252b5132 4868 if (offset_expr.X_op == O_constant)
4d34fb5f
TS
4869 load_register (&icnt, tempreg, &offset_expr,
4870 ((mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
4871 ? (dbl || HAVE_64BIT_ADDRESSES)
4872 : HAVE_64BIT_ADDRESSES));
252b5132
RH
4873 else if (mips_pic == NO_PIC)
4874 {
d6bc6245 4875 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 4876 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
4877 Otherwise we want
4878 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
4879 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4880 If we have a constant, we need two instructions anyhow,
d6bc6245 4881 so we may as well always use the latter form.
76b3015f 4882
d6bc6245
TS
4883 With 64bit address space and a usable $at we want
4884 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4885 lui $at,<sym> (BFD_RELOC_HI16_S)
4886 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4887 daddiu $at,<sym> (BFD_RELOC_LO16)
4888 dsll32 $tempreg,0
3a482fd5 4889 daddu $tempreg,$tempreg,$at
76b3015f 4890
c03099e6 4891 If $at is already in use, we use a path which is suboptimal
d6bc6245
TS
4892 on superscalar processors.
4893 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4894 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4895 dsll $tempreg,16
4896 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
4897 dsll $tempreg,16
4898 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
4899 */
f9419b05 4900 char *p = NULL;
d6bc6245 4901 if (HAVE_64BIT_ADDRESSES)
252b5132 4902 {
d6bc6245
TS
4903 /* We don't do GP optimization for now because RELAX_ENCODE can't
4904 hold the data for such large chunks. */
4905
460597ba 4906 if (used_at == 0 && ! mips_opts.noat)
98d3f06f
KH
4907 {
4908 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
17a2f251 4909 tempreg, BFD_RELOC_MIPS_HIGHEST);
98d3f06f 4910 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
17a2f251 4911 AT, BFD_RELOC_HI16_S);
98d3f06f 4912 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
17a2f251 4913 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
98d3f06f 4914 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
17a2f251
TS
4915 AT, AT, BFD_RELOC_LO16);
4916 macro_build (p, &icnt, NULL, "dsll32", "d,w,<",
4917 tempreg, tempreg, 0);
4918 macro_build (p, &icnt, NULL, "daddu", "d,v,t",
4919 tempreg, tempreg, AT);
98d3f06f
KH
4920 used_at = 1;
4921 }
4922 else
4923 {
4924 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
17a2f251 4925 tempreg, BFD_RELOC_MIPS_HIGHEST);
98d3f06f 4926 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
17a2f251
TS
4927 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
4928 macro_build (p, &icnt, NULL, "dsll", "d,w,<",
98d3f06f
KH
4929 tempreg, tempreg, 16);
4930 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
17a2f251
TS
4931 tempreg, tempreg, BFD_RELOC_HI16_S);
4932 macro_build (p, &icnt, NULL, "dsll", "d,w,<",
98d3f06f
KH
4933 tempreg, tempreg, 16);
4934 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
17a2f251 4935 tempreg, tempreg, BFD_RELOC_LO16);
98d3f06f
KH
4936 }
4937 }
4938 else
4939 {
4940 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4941 && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
4942 {
4943 frag_grow (20);
17a2f251
TS
4944 macro_build (NULL, &icnt, &offset_expr, ADDRESS_ADDI_INSN,
4945 "t,r,j", tempreg, mips_gp_register,
4946 BFD_RELOC_GPREL16);
98d3f06f
KH
4947 p = frag_var (rs_machine_dependent, 8, 0,
4948 RELAX_ENCODE (4, 8, 0, 4, 0,
4949 mips_opts.warn_about_macros),
4950 offset_expr.X_add_symbol, 0, NULL);
4951 }
4952 macro_build_lui (p, &icnt, &offset_expr, tempreg);
4953 if (p != NULL)
4954 p += 4;
f899b4b8 4955 macro_build (p, &icnt, &offset_expr, ADDRESS_ADDI_INSN,
17a2f251 4956 "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
98d3f06f 4957 }
252b5132 4958 }
f5040a92 4959 else if (mips_pic == SVR4_PIC && ! mips_big_got && ! HAVE_NEWABI)
252b5132 4960 {
9117d219
NC
4961 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
4962
252b5132
RH
4963 /* If this is a reference to an external symbol, and there
4964 is no constant, we want
4965 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
1abe91b1 4966 or for lca or if tempreg is PIC_CALL_REG
9117d219 4967 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
252b5132
RH
4968 For a local symbol, we want
4969 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4970 nop
4971 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4972
4973 If we have a small constant, and this is a reference to
4974 an external symbol, we want
4975 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4976 nop
4977 addiu $tempreg,$tempreg,<constant>
4978 For a local symbol, we want the same instruction
4979 sequence, but we output a BFD_RELOC_LO16 reloc on the
4980 addiu instruction.
4981
4982 If we have a large constant, and this is a reference to
4983 an external symbol, we want
4984 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4985 lui $at,<hiconstant>
4986 addiu $at,$at,<loconstant>
4987 addu $tempreg,$tempreg,$at
4988 For a local symbol, we want the same instruction
4989 sequence, but we output a BFD_RELOC_LO16 reloc on the
ed6fb7bd 4990 addiu instruction.
ed6fb7bd
SC
4991 */
4992
252b5132
RH
4993 expr1.X_add_number = offset_expr.X_add_number;
4994 offset_expr.X_add_number = 0;
4995 frag_grow (32);
1abe91b1
MR
4996 if (expr1.X_add_number == 0 && breg == 0
4997 && (call || tempreg == PIC_CALL_REG))
9117d219 4998 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
17a2f251
TS
4999 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5000 tempreg, lw_reloc_type, mips_gp_register);
252b5132
RH
5001 if (expr1.X_add_number == 0)
5002 {
5003 int off;
f9419b05 5004 char *p;
252b5132
RH
5005
5006 if (breg == 0)
5007 off = 0;
5008 else
5009 {
5010 /* We're going to put in an addu instruction using
5011 tempreg, so we may as well insert the nop right
5012 now. */
17a2f251 5013 macro_build (NULL, &icnt, NULL, "nop", "");
252b5132
RH
5014 off = 4;
5015 }
5016 p = frag_var (rs_machine_dependent, 8 - off, 0,
5017 RELAX_ENCODE (0, 8 - off, -4 - off, 4 - off, 0,
5018 (breg == 0
5019 ? mips_opts.warn_about_macros
5020 : 0)),
c4e7957c 5021 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5022 if (breg == 0)
5023 {
17a2f251 5024 macro_build (p, &icnt, NULL, "nop", "");
252b5132
RH
5025 p += 4;
5026 }
f899b4b8 5027 macro_build (p, &icnt, &expr1, ADDRESS_ADDI_INSN,
17a2f251 5028 "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
252b5132
RH
5029 /* FIXME: If breg == 0, and the next instruction uses
5030 $tempreg, then if this variant case is used an extra
5031 nop will be generated. */
5032 }
5033 else if (expr1.X_add_number >= -0x8000
5034 && expr1.X_add_number < 0x8000)
5035 {
17a2f251
TS
5036 macro_build (NULL, &icnt, NULL, "nop", "");
5037 macro_build (NULL, &icnt, &expr1, ADDRESS_ADDI_INSN,
5038 "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
c4e7957c
TS
5039 frag_var (rs_machine_dependent, 0, 0,
5040 RELAX_ENCODE (0, 0, -12, -4, 0, 0),
5041 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5042 }
5043 else
5044 {
5045 int off1;
5046
5047 /* If we are going to add in a base register, and the
5048 target register and the base register are the same,
5049 then we are using AT as a temporary register. Since
5050 we want to load the constant into AT, we add our
5051 current AT (from the global offset table) and the
5052 register into the register now, and pretend we were
5053 not using a base register. */
5054 if (breg != treg)
5055 off1 = 0;
5056 else
5057 {
17a2f251
TS
5058 macro_build (NULL, &icnt, NULL, "nop", "");
5059 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5060 treg, AT, breg);
252b5132
RH
5061 breg = 0;
5062 tempreg = treg;
5063 off1 = -8;
5064 }
5065
5066 /* Set mips_optimize around the lui instruction to avoid
5067 inserting an unnecessary nop after the lw. */
5068 hold_mips_optimize = mips_optimize;
5069 mips_optimize = 2;
c4e7957c 5070 macro_build_lui (NULL, &icnt, &expr1, AT);
252b5132
RH
5071 mips_optimize = hold_mips_optimize;
5072
17a2f251
TS
5073 macro_build (NULL, &icnt, &expr1, ADDRESS_ADDI_INSN, "t,r,j",
5074 AT, AT, BFD_RELOC_LO16);
5075 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5076 tempreg, tempreg, AT);
c4e7957c
TS
5077 frag_var (rs_machine_dependent, 0, 0,
5078 RELAX_ENCODE (0, 0, -16 + off1, -8, 0, 0),
5079 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5080 used_at = 1;
5081 }
5082 }
f5040a92
AO
5083 else if (mips_pic == SVR4_PIC && ! mips_big_got && HAVE_NEWABI)
5084 {
5085 char *p = NULL;
5086 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_DISP;
5087 int adj = 0;
5088
5089 /* If this is a reference to an external, and there is no
5090 constant, or local symbol (*), with or without a
5091 constant, we want
5092 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
1abe91b1 5093 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
5094 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5095
5096 If we have a small constant, and this is a reference to
5097 an external symbol, we want
5098 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5099 addiu $tempreg,$tempreg,<constant>
5100
5101 If we have a large constant, and this is a reference to
5102 an external symbol, we want
5103 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5104 lui $at,<hiconstant>
5105 addiu $at,$at,<loconstant>
5106 addu $tempreg,$tempreg,$at
5107
5108 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
5109 local symbols, even though it introduces an additional
5110 instruction. */
5111
5112 frag_grow (28);
1abe91b1
MR
5113 if (offset_expr.X_add_number == 0 && breg == 0
5114 && (call || tempreg == PIC_CALL_REG))
f5040a92
AO
5115 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
5116 if (offset_expr.X_add_number)
5117 {
5118 frag_now->tc_frag_data.tc_fr_offset =
5119 expr1.X_add_number = offset_expr.X_add_number;
5120 offset_expr.X_add_number = 0;
5121
17a2f251
TS
5122 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5123 "t,o(b)", tempreg, lw_reloc_type,
5124 mips_gp_register);
f5040a92
AO
5125
5126 if (expr1.X_add_number >= -0x8000
5127 && expr1.X_add_number < 0x8000)
5128 {
17a2f251
TS
5129 macro_build (NULL, &icnt, &expr1, ADDRESS_ADDI_INSN,
5130 "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
f5040a92
AO
5131 p = frag_var (rs_machine_dependent, 4, 0,
5132 RELAX_ENCODE (8, 4, 0, 0, 0, 0),
5133 offset_expr.X_add_symbol, 0, NULL);
5134 }
ecd13cd3 5135 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92
AO
5136 {
5137 int dreg;
5138
5139 /* If we are going to add in a base register, and the
5140 target register and the base register are the same,
5141 then we are using AT as a temporary register. Since
5142 we want to load the constant into AT, we add our
5143 current AT (from the global offset table) and the
5144 register into the register now, and pretend we were
5145 not using a base register. */
5146 if (breg != treg)
5147 dreg = tempreg;
5148 else
5149 {
5150 assert (tempreg == AT);
bf151ce7 5151 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN,
17a2f251 5152 "d,v,t", treg, AT, breg);
f5040a92
AO
5153 dreg = treg;
5154 adj = 4;
5155 }
5156
17a2f251
TS
5157 macro_build_lui (NULL, &icnt, &expr1, AT);
5158 macro_build (NULL, &icnt, &expr1, ADDRESS_ADDI_INSN,
5159 "t,r,j", AT, AT, BFD_RELOC_LO16);
5160 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5161 dreg, dreg, AT);
f5040a92
AO
5162
5163 p = frag_var (rs_machine_dependent, 4 + adj, 0,
5164 RELAX_ENCODE (16 + adj, 4 + adj,
5165 0, 0, 0, 0),
5166 offset_expr.X_add_symbol, 0, NULL);
5167
5168 used_at = 1;
5169 }
5170 else
5171 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5172
5173 offset_expr.X_add_number = expr1.X_add_number;
5174
f899b4b8 5175 macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
17a2f251 5176 "t,o(b)", tempreg, BFD_RELOC_MIPS_GOT_DISP,
f5040a92
AO
5177 mips_gp_register);
5178 if (adj)
5179 {
17a2f251 5180 macro_build (p + 4, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
f899b4b8 5181 treg, tempreg, breg);
f5040a92
AO
5182 breg = 0;
5183 tempreg = treg;
5184 }
5185 }
5186 else
5187 {
17a2f251
TS
5188 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5189 "t,o(b)", tempreg, lw_reloc_type,
5190 mips_gp_register);
f5040a92
AO
5191 if (lw_reloc_type != BFD_RELOC_MIPS_GOT_DISP)
5192 p = frag_var (rs_machine_dependent, 0, 0,
5193 RELAX_ENCODE (0, 0, -4, 0, 0, 0),
5194 offset_expr.X_add_symbol, 0, NULL);
5195 }
5196
5197 if (! p)
5198 {
5199 /* To avoid confusion in tc_gen_reloc, we must ensure
5200 that this does not become a variant frag. */
5201 frag_wane (frag_now);
5202 frag_new (0);
5203 }
5204 }
5205 else if (mips_pic == SVR4_PIC && ! HAVE_NEWABI)
252b5132
RH
5206 {
5207 int gpdel;
f9419b05 5208 char *p;
9117d219
NC
5209 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5210 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
ed6fb7bd 5211 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
252b5132
RH
5212
5213 /* This is the large GOT case. If this is a reference to an
5214 external symbol, and there is no constant, we want
5215 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5216 addu $tempreg,$tempreg,$gp
5217 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 5218 or for lca or if tempreg is PIC_CALL_REG
9117d219
NC
5219 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5220 addu $tempreg,$tempreg,$gp
5221 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
252b5132
RH
5222 For a local symbol, we want
5223 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5224 nop
5225 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5226
5227 If we have a small constant, and this is a reference to
5228 an external symbol, we want
5229 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5230 addu $tempreg,$tempreg,$gp
5231 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5232 nop
5233 addiu $tempreg,$tempreg,<constant>
5234 For a local symbol, we want
5235 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5236 nop
5237 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
5238
5239 If we have a large constant, and this is a reference to
5240 an external symbol, we want
5241 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5242 addu $tempreg,$tempreg,$gp
5243 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5244 lui $at,<hiconstant>
5245 addiu $at,$at,<loconstant>
5246 addu $tempreg,$tempreg,$at
5247 For a local symbol, we want
5248 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5249 lui $at,<hiconstant>
5250 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
5251 addu $tempreg,$tempreg,$at
f5040a92 5252 */
438c16b8 5253
252b5132
RH
5254 expr1.X_add_number = offset_expr.X_add_number;
5255 offset_expr.X_add_number = 0;
5256 frag_grow (52);
f7ea7ef2 5257 if (reg_needs_delay (mips_gp_register))
252b5132
RH
5258 gpdel = 4;
5259 else
5260 gpdel = 0;
1abe91b1
MR
5261 if (expr1.X_add_number == 0 && breg == 0
5262 && (call || tempreg == PIC_CALL_REG))
9117d219
NC
5263 {
5264 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5265 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5266 }
17a2f251 5267 macro_build (NULL, &icnt, &offset_expr, "lui", "t,u",
9117d219 5268 tempreg, lui_reloc_type);
17a2f251
TS
5269 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5270 tempreg, tempreg, mips_gp_register);
5271 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5272 tempreg, lw_reloc_type, tempreg);
252b5132
RH
5273 if (expr1.X_add_number == 0)
5274 {
5275 int off;
5276
5277 if (breg == 0)
5278 off = 0;
5279 else
5280 {
5281 /* We're going to put in an addu instruction using
5282 tempreg, so we may as well insert the nop right
5283 now. */
17a2f251 5284 macro_build (NULL, &icnt, NULL, "nop", "");
252b5132
RH
5285 off = 4;
5286 }
5287
5288 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5289 RELAX_ENCODE (12 + off, 12 + gpdel, gpdel,
5290 8 + gpdel, 0,
5291 (breg == 0
5292 ? mips_opts.warn_about_macros
5293 : 0)),
c4e7957c 5294 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5295 }
5296 else if (expr1.X_add_number >= -0x8000
5297 && expr1.X_add_number < 0x8000)
5298 {
17a2f251
TS
5299 macro_build (NULL, &icnt, NULL, "nop", "");
5300 macro_build (NULL, &icnt, &expr1, ADDRESS_ADDI_INSN, "t,r,j",
5301 tempreg, tempreg, BFD_RELOC_LO16);
252b5132
RH
5302
5303 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5304 RELAX_ENCODE (20, 12 + gpdel, gpdel, 8 + gpdel, 0,
5305 (breg == 0
5306 ? mips_opts.warn_about_macros
5307 : 0)),
c4e7957c 5308 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5309 }
5310 else
5311 {
5312 int adj, dreg;
5313
5314 /* If we are going to add in a base register, and the
5315 target register and the base register are the same,
5316 then we are using AT as a temporary register. Since
5317 we want to load the constant into AT, we add our
5318 current AT (from the global offset table) and the
5319 register into the register now, and pretend we were
5320 not using a base register. */
5321 if (breg != treg)
5322 {
5323 adj = 0;
5324 dreg = tempreg;
5325 }
5326 else
5327 {
5328 assert (tempreg == AT);
17a2f251
TS
5329 macro_build (NULL, &icnt, NULL, "nop", "");
5330 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5331 treg, AT, breg);
252b5132
RH
5332 dreg = treg;
5333 adj = 8;
5334 }
5335
5336 /* Set mips_optimize around the lui instruction to avoid
5337 inserting an unnecessary nop after the lw. */
5338 hold_mips_optimize = mips_optimize;
5339 mips_optimize = 2;
c4e7957c 5340 macro_build_lui (NULL, &icnt, &expr1, AT);
252b5132
RH
5341 mips_optimize = hold_mips_optimize;
5342
17a2f251
TS
5343 macro_build (NULL, &icnt, &expr1, ADDRESS_ADDI_INSN, "t,r,j",
5344 AT, AT, BFD_RELOC_LO16);
5345 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5346 dreg, dreg, AT);
252b5132
RH
5347
5348 p = frag_var (rs_machine_dependent, 16 + gpdel + adj, 0,
5349 RELAX_ENCODE (24 + adj, 16 + gpdel + adj, gpdel,
5350 8 + gpdel, 0,
5351 (breg == 0
5352 ? mips_opts.warn_about_macros
5353 : 0)),
c4e7957c 5354 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5355
5356 used_at = 1;
5357 }
5358
5359 if (gpdel > 0)
5360 {
5361 /* This is needed because this instruction uses $gp, but
f5040a92 5362 the first instruction on the main stream does not. */
17a2f251 5363 macro_build (p, &icnt, NULL, "nop", "");
252b5132
RH
5364 p += 4;
5365 }
ed6fb7bd 5366
f899b4b8
TS
5367 macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5368 tempreg, local_reloc_type, mips_gp_register);
252b5132 5369 p += 4;
f5040a92 5370 if (expr1.X_add_number >= -0x8000
252b5132
RH
5371 && expr1.X_add_number < 0x8000)
5372 {
17a2f251 5373 macro_build (p, &icnt, NULL, "nop", "");
252b5132 5374 p += 4;
f899b4b8 5375 macro_build (p, &icnt, &expr1, ADDRESS_ADDI_INSN,
17a2f251 5376 "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
252b5132 5377 /* FIXME: If add_number is 0, and there was no base
f5040a92
AO
5378 register, the external symbol case ended with a load,
5379 so if the symbol turns out to not be external, and
5380 the next instruction uses tempreg, an unnecessary nop
5381 will be inserted. */
252b5132
RH
5382 }
5383 else
5384 {
5385 if (breg == treg)
5386 {
5387 /* We must add in the base register now, as in the
f5040a92 5388 external symbol case. */
252b5132 5389 assert (tempreg == AT);
17a2f251 5390 macro_build (p, &icnt, NULL, "nop", "");
252b5132 5391 p += 4;
17a2f251
TS
5392 macro_build (p, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5393 treg, AT, breg);
252b5132
RH
5394 p += 4;
5395 tempreg = treg;
5396 /* We set breg to 0 because we have arranged to add
f5040a92 5397 it in in both cases. */
252b5132
RH
5398 breg = 0;
5399 }
5400
5401 macro_build_lui (p, &icnt, &expr1, AT);
5402 p += 4;
17a2f251
TS
5403 macro_build (p, &icnt, &expr1, ADDRESS_ADDI_INSN, "t,r,j",
5404 AT, AT, BFD_RELOC_LO16);
252b5132 5405 p += 4;
17a2f251
TS
5406 macro_build (p, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5407 tempreg, tempreg, AT);
252b5132
RH
5408 p += 4;
5409 }
5410 }
f5040a92
AO
5411 else if (mips_pic == SVR4_PIC && HAVE_NEWABI)
5412 {
5413 char *p = NULL;
5414 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5415 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
5416 int adj = 0;
5417
5418 /* This is the large GOT case. If this is a reference to an
5419 external symbol, and there is no constant, we want
5420 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5421 add $tempreg,$tempreg,$gp
5422 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 5423 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
5424 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5425 add $tempreg,$tempreg,$gp
5426 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5427
5428 If we have a small constant, and this is a reference to
5429 an external symbol, we want
5430 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5431 add $tempreg,$tempreg,$gp
5432 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5433 addi $tempreg,$tempreg,<constant>
5434
5435 If we have a large constant, and this is a reference to
5436 an external symbol, we want
5437 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5438 addu $tempreg,$tempreg,$gp
5439 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5440 lui $at,<hiconstant>
5441 addi $at,$at,<loconstant>
5442 add $tempreg,$tempreg,$at
5443
5444 If we have NewABI, and we know it's a local symbol, we want
5445 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
5446 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
5447 otherwise we have to resort to GOT_HI16/GOT_LO16. */
5448
5449 frag_grow (40);
5450
5451 frag_now->tc_frag_data.tc_fr_offset =
5452 expr1.X_add_number = offset_expr.X_add_number;
5453 offset_expr.X_add_number = 0;
5454
1abe91b1
MR
5455 if (expr1.X_add_number == 0 && breg == 0
5456 && (call || tempreg == PIC_CALL_REG))
f5040a92
AO
5457 {
5458 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5459 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5460 }
17a2f251 5461 macro_build (NULL, &icnt, &offset_expr, "lui", "t,u",
f5040a92 5462 tempreg, lui_reloc_type);
17a2f251
TS
5463 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5464 tempreg, tempreg, mips_gp_register);
5465 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
f5040a92
AO
5466 "t,o(b)", tempreg, lw_reloc_type, tempreg);
5467
5468 if (expr1.X_add_number == 0)
5469 {
5470 p = frag_var (rs_machine_dependent, 8, 0,
5471 RELAX_ENCODE (12, 8, 0, 4, 0,
5472 mips_opts.warn_about_macros),
5473 offset_expr.X_add_symbol, 0, NULL);
684022ea 5474 }
f5040a92
AO
5475 else if (expr1.X_add_number >= -0x8000
5476 && expr1.X_add_number < 0x8000)
5477 {
17a2f251
TS
5478 macro_build (NULL, &icnt, &expr1, ADDRESS_ADDI_INSN, "t,r,j",
5479 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92
AO
5480 p = frag_var (rs_machine_dependent, 8, 0,
5481 RELAX_ENCODE (16, 8, 0, 4, 0,
5482 mips_opts.warn_about_macros),
5483 offset_expr.X_add_symbol, 0, NULL);
5484 }
ecd13cd3 5485 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92
AO
5486 {
5487 int dreg;
5488
5489 /* If we are going to add in a base register, and the
5490 target register and the base register are the same,
5491 then we are using AT as a temporary register. Since
5492 we want to load the constant into AT, we add our
5493 current AT (from the global offset table) and the
5494 register into the register now, and pretend we were
5495 not using a base register. */
5496 if (breg != treg)
5497 dreg = tempreg;
5498 else
5499 {
5500 assert (tempreg == AT);
17a2f251
TS
5501 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5502 treg, AT, breg);
f5040a92
AO
5503 dreg = treg;
5504 adj = 4;
5505 }
5506
5507 /* Set mips_optimize around the lui instruction to avoid
5508 inserting an unnecessary nop after the lw. */
17a2f251
TS
5509 macro_build_lui (NULL, &icnt, &expr1, AT);
5510 macro_build (NULL, &icnt, &expr1, ADDRESS_ADDI_INSN,
5511 "t,r,j", AT, AT, BFD_RELOC_LO16);
5512 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5513 dreg, dreg, AT);
f5040a92
AO
5514
5515 p = frag_var (rs_machine_dependent, 8 + adj, 0,
5516 RELAX_ENCODE (24 + adj, 8 + adj,
5517 0, 4, 0,
5518 (breg == 0
5519 ? mips_opts.warn_about_macros
5520 : 0)),
5521 offset_expr.X_add_symbol, 0, NULL);
5522
5523 used_at = 1;
5524 }
5525 else
5526 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5527
5528 offset_expr.X_add_number = expr1.X_add_number;
f899b4b8 5529 macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 5530 tempreg, BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f899b4b8 5531 macro_build (p + 4, &icnt, &offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 5532 tempreg, tempreg, BFD_RELOC_MIPS_GOT_OFST);
f5040a92
AO
5533 if (adj)
5534 {
17a2f251
TS
5535 macro_build (p + 8, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5536 treg, tempreg, breg);
f5040a92
AO
5537 breg = 0;
5538 tempreg = treg;
5539 }
5540 }
252b5132
RH
5541 else if (mips_pic == EMBEDDED_PIC)
5542 {
5543 /* We use
cdf6fd85 5544 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132 5545 */
17a2f251
TS
5546 macro_build (NULL, &icnt, &offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5547 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
252b5132
RH
5548 }
5549 else
5550 abort ();
5551
5552 if (breg != 0)
4d34fb5f
TS
5553 {
5554 char *s;
5555
5556 if (mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
b7c7d6c1 5557 s = (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu";
4d34fb5f 5558 else
f899b4b8 5559 s = ADDRESS_ADD_INSN;
4d34fb5f 5560
17a2f251 5561 macro_build (NULL, &icnt, NULL, s, "d,v,t", treg, tempreg, breg);
4d34fb5f 5562 }
252b5132
RH
5563
5564 if (! used_at)
5565 return;
5566
5567 break;
5568
5569 case M_J_A:
5570 /* The j instruction may not be used in PIC code, since it
5571 requires an absolute address. We convert it to a b
5572 instruction. */
5573 if (mips_pic == NO_PIC)
17a2f251 5574 macro_build (NULL, &icnt, &offset_expr, "j", "a");
252b5132 5575 else
17a2f251 5576 macro_build (NULL, &icnt, &offset_expr, "b", "p");
252b5132
RH
5577 return;
5578
5579 /* The jal instructions must be handled as macros because when
5580 generating PIC code they expand to multi-instruction
5581 sequences. Normally they are simple instructions. */
5582 case M_JAL_1:
5583 dreg = RA;
5584 /* Fall through. */
5585 case M_JAL_2:
5586 if (mips_pic == NO_PIC
5587 || mips_pic == EMBEDDED_PIC)
17a2f251 5588 macro_build (NULL, &icnt, NULL, "jalr", "d,s", dreg, sreg);
252b5132
RH
5589 else if (mips_pic == SVR4_PIC)
5590 {
5591 if (sreg != PIC_CALL_REG)
5592 as_warn (_("MIPS PIC call to register other than $25"));
bdaaa2e1 5593
17a2f251 5594 macro_build (NULL, &icnt, NULL, "jalr", "d,s", dreg, sreg);
6478892d 5595 if (! HAVE_NEWABI)
252b5132 5596 {
6478892d
TS
5597 if (mips_cprestore_offset < 0)
5598 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5599 else
5600 {
7a621144
DJ
5601 if (! mips_frame_reg_valid)
5602 {
5603 as_warn (_("No .frame pseudo-op used in PIC code"));
5604 /* Quiet this warning. */
5605 mips_frame_reg_valid = 1;
5606 }
5607 if (! mips_cprestore_valid)
5608 {
5609 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5610 /* Quiet this warning. */
5611 mips_cprestore_valid = 1;
5612 }
6478892d 5613 expr1.X_add_number = mips_cprestore_offset;
17a2f251 5614 macro_build_ldst_constoffset (NULL, &icnt, &expr1,
f899b4b8
TS
5615 ADDRESS_LOAD_INSN,
5616 mips_gp_register,
256ab948
TS
5617 mips_frame_reg,
5618 HAVE_64BIT_ADDRESSES);
6478892d 5619 }
252b5132
RH
5620 }
5621 }
5622 else
5623 abort ();
5624
5625 return;
5626
5627 case M_JAL_A:
5628 if (mips_pic == NO_PIC)
17a2f251 5629 macro_build (NULL, &icnt, &offset_expr, "jal", "a");
252b5132
RH
5630 else if (mips_pic == SVR4_PIC)
5631 {
f9419b05
TS
5632 char *p;
5633
252b5132
RH
5634 /* If this is a reference to an external symbol, and we are
5635 using a small GOT, we want
5636 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5637 nop
f9419b05 5638 jalr $ra,$25
252b5132
RH
5639 nop
5640 lw $gp,cprestore($sp)
5641 The cprestore value is set using the .cprestore
5642 pseudo-op. If we are using a big GOT, we want
5643 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5644 addu $25,$25,$gp
5645 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
5646 nop
f9419b05 5647 jalr $ra,$25
252b5132
RH
5648 nop
5649 lw $gp,cprestore($sp)
5650 If the symbol is not external, we want
5651 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5652 nop
5653 addiu $25,$25,<sym> (BFD_RELOC_LO16)
f9419b05 5654 jalr $ra,$25
252b5132 5655 nop
438c16b8 5656 lw $gp,cprestore($sp)
f5040a92
AO
5657
5658 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
5659 sequences above, minus nops, unless the symbol is local,
5660 which enables us to use GOT_PAGE/GOT_OFST (big got) or
5661 GOT_DISP. */
438c16b8 5662 if (HAVE_NEWABI)
252b5132 5663 {
f5040a92
AO
5664 if (! mips_big_got)
5665 {
5666 frag_grow (4);
17a2f251
TS
5667 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5668 "t,o(b)", PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
f5040a92
AO
5669 mips_gp_register);
5670 frag_var (rs_machine_dependent, 0, 0,
5671 RELAX_ENCODE (0, 0, -4, 0, 0, 0),
5672 offset_expr.X_add_symbol, 0, NULL);
5673 }
5674 else
5675 {
5676 frag_grow (20);
17a2f251
TS
5677 macro_build (NULL, &icnt, &offset_expr, "lui", "t,u",
5678 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_HI16);
5679 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5680 PIC_CALL_REG, PIC_CALL_REG, mips_gp_register);
5681 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5682 "t,o(b)", PIC_CALL_REG,
5683 BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
f5040a92
AO
5684 p = frag_var (rs_machine_dependent, 8, 0,
5685 RELAX_ENCODE (12, 8, 0, 4, 0, 0),
5686 offset_expr.X_add_symbol, 0, NULL);
f899b4b8
TS
5687 macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5688 "t,o(b)", PIC_CALL_REG,
17a2f251 5689 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f899b4b8 5690 macro_build (p + 4, &icnt, &offset_expr, ADDRESS_ADDI_INSN,
dc9461f6 5691 "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
17a2f251 5692 BFD_RELOC_MIPS_GOT_OFST);
f5040a92 5693 }
684022ea 5694
438c16b8 5695 macro_build_jalr (icnt, &offset_expr);
252b5132
RH
5696 }
5697 else
5698 {
438c16b8
TS
5699 frag_grow (40);
5700 if (! mips_big_got)
5701 {
17a2f251
TS
5702 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5703 "t,o(b)", PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
5704 mips_gp_register);
5705 macro_build (NULL, &icnt, NULL, "nop", "");
438c16b8
TS
5706 p = frag_var (rs_machine_dependent, 4, 0,
5707 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5708 offset_expr.X_add_symbol, 0, NULL);
5709 }
252b5132 5710 else
252b5132 5711 {
438c16b8
TS
5712 int gpdel;
5713
5714 if (reg_needs_delay (mips_gp_register))
5715 gpdel = 4;
5716 else
5717 gpdel = 0;
17a2f251
TS
5718 macro_build (NULL, &icnt, &offset_expr, "lui", "t,u",
5719 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_HI16);
5720 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5721 PIC_CALL_REG, PIC_CALL_REG, mips_gp_register);
5722 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5723 "t,o(b)", PIC_CALL_REG,
5724 BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
5725 macro_build (NULL, &icnt, NULL, "nop", "");
438c16b8
TS
5726 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5727 RELAX_ENCODE (16, 12 + gpdel, gpdel,
5728 8 + gpdel, 0, 0),
5729 offset_expr.X_add_symbol, 0, NULL);
5730 if (gpdel > 0)
5731 {
17a2f251 5732 macro_build (p, &icnt, NULL, "nop", "");
438c16b8
TS
5733 p += 4;
5734 }
f899b4b8 5735 macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
17a2f251
TS
5736 "t,o(b)", PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
5737 mips_gp_register);
438c16b8 5738 p += 4;
17a2f251 5739 macro_build (p, &icnt, NULL, "nop", "");
252b5132
RH
5740 p += 4;
5741 }
f899b4b8 5742 macro_build (p, &icnt, &offset_expr, ADDRESS_ADDI_INSN,
438c16b8 5743 "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
17a2f251 5744 BFD_RELOC_LO16);
438c16b8
TS
5745 macro_build_jalr (icnt, &offset_expr);
5746
6478892d
TS
5747 if (mips_cprestore_offset < 0)
5748 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5749 else
5750 {
7a621144
DJ
5751 if (! mips_frame_reg_valid)
5752 {
5753 as_warn (_("No .frame pseudo-op used in PIC code"));
5754 /* Quiet this warning. */
5755 mips_frame_reg_valid = 1;
5756 }
5757 if (! mips_cprestore_valid)
5758 {
5759 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5760 /* Quiet this warning. */
5761 mips_cprestore_valid = 1;
5762 }
6478892d 5763 if (mips_opts.noreorder)
17a2f251 5764 macro_build (NULL, &icnt, NULL, "nop", "");
6478892d 5765 expr1.X_add_number = mips_cprestore_offset;
17a2f251 5766 macro_build_ldst_constoffset (NULL, &icnt, &expr1,
f899b4b8
TS
5767 ADDRESS_LOAD_INSN,
5768 mips_gp_register,
256ab948
TS
5769 mips_frame_reg,
5770 HAVE_64BIT_ADDRESSES);
6478892d 5771 }
252b5132
RH
5772 }
5773 }
5774 else if (mips_pic == EMBEDDED_PIC)
5775 {
17a2f251 5776 macro_build (NULL, &icnt, &offset_expr, "bal", "p");
252b5132
RH
5777 /* The linker may expand the call to a longer sequence which
5778 uses $at, so we must break rather than return. */
5779 break;
5780 }
5781 else
5782 abort ();
5783
5784 return;
5785
5786 case M_LB_AB:
5787 s = "lb";
5788 goto ld;
5789 case M_LBU_AB:
5790 s = "lbu";
5791 goto ld;
5792 case M_LH_AB:
5793 s = "lh";
5794 goto ld;
5795 case M_LHU_AB:
5796 s = "lhu";
5797 goto ld;
5798 case M_LW_AB:
5799 s = "lw";
5800 goto ld;
5801 case M_LWC0_AB:
5802 s = "lwc0";
bdaaa2e1 5803 /* Itbl support may require additional care here. */
252b5132
RH
5804 coproc = 1;
5805 goto ld;
5806 case M_LWC1_AB:
5807 s = "lwc1";
bdaaa2e1 5808 /* Itbl support may require additional care here. */
252b5132
RH
5809 coproc = 1;
5810 goto ld;
5811 case M_LWC2_AB:
5812 s = "lwc2";
bdaaa2e1 5813 /* Itbl support may require additional care here. */
252b5132
RH
5814 coproc = 1;
5815 goto ld;
5816 case M_LWC3_AB:
5817 s = "lwc3";
bdaaa2e1 5818 /* Itbl support may require additional care here. */
252b5132
RH
5819 coproc = 1;
5820 goto ld;
5821 case M_LWL_AB:
5822 s = "lwl";
5823 lr = 1;
5824 goto ld;
5825 case M_LWR_AB:
5826 s = "lwr";
5827 lr = 1;
5828 goto ld;
5829 case M_LDC1_AB:
fef14a42 5830 if (mips_opts.arch == CPU_R4650)
252b5132
RH
5831 {
5832 as_bad (_("opcode not supported on this processor"));
5833 return;
5834 }
5835 s = "ldc1";
bdaaa2e1 5836 /* Itbl support may require additional care here. */
252b5132
RH
5837 coproc = 1;
5838 goto ld;
5839 case M_LDC2_AB:
5840 s = "ldc2";
bdaaa2e1 5841 /* Itbl support may require additional care here. */
252b5132
RH
5842 coproc = 1;
5843 goto ld;
5844 case M_LDC3_AB:
5845 s = "ldc3";
bdaaa2e1 5846 /* Itbl support may require additional care here. */
252b5132
RH
5847 coproc = 1;
5848 goto ld;
5849 case M_LDL_AB:
5850 s = "ldl";
5851 lr = 1;
5852 goto ld;
5853 case M_LDR_AB:
5854 s = "ldr";
5855 lr = 1;
5856 goto ld;
5857 case M_LL_AB:
5858 s = "ll";
5859 goto ld;
5860 case M_LLD_AB:
5861 s = "lld";
5862 goto ld;
5863 case M_LWU_AB:
5864 s = "lwu";
5865 ld:
5866 if (breg == treg || coproc || lr)
5867 {
5868 tempreg = AT;
5869 used_at = 1;
5870 }
5871 else
5872 {
5873 tempreg = treg;
5874 used_at = 0;
5875 }
5876 goto ld_st;
5877 case M_SB_AB:
5878 s = "sb";
5879 goto st;
5880 case M_SH_AB:
5881 s = "sh";
5882 goto st;
5883 case M_SW_AB:
5884 s = "sw";
5885 goto st;
5886 case M_SWC0_AB:
5887 s = "swc0";
bdaaa2e1 5888 /* Itbl support may require additional care here. */
252b5132
RH
5889 coproc = 1;
5890 goto st;
5891 case M_SWC1_AB:
5892 s = "swc1";
bdaaa2e1 5893 /* Itbl support may require additional care here. */
252b5132
RH
5894 coproc = 1;
5895 goto st;
5896 case M_SWC2_AB:
5897 s = "swc2";
bdaaa2e1 5898 /* Itbl support may require additional care here. */
252b5132
RH
5899 coproc = 1;
5900 goto st;
5901 case M_SWC3_AB:
5902 s = "swc3";
bdaaa2e1 5903 /* Itbl support may require additional care here. */
252b5132
RH
5904 coproc = 1;
5905 goto st;
5906 case M_SWL_AB:
5907 s = "swl";
5908 goto st;
5909 case M_SWR_AB:
5910 s = "swr";
5911 goto st;
5912 case M_SC_AB:
5913 s = "sc";
5914 goto st;
5915 case M_SCD_AB:
5916 s = "scd";
5917 goto st;
5918 case M_SDC1_AB:
fef14a42 5919 if (mips_opts.arch == CPU_R4650)
252b5132
RH
5920 {
5921 as_bad (_("opcode not supported on this processor"));
5922 return;
5923 }
5924 s = "sdc1";
5925 coproc = 1;
bdaaa2e1 5926 /* Itbl support may require additional care here. */
252b5132
RH
5927 goto st;
5928 case M_SDC2_AB:
5929 s = "sdc2";
bdaaa2e1 5930 /* Itbl support may require additional care here. */
252b5132
RH
5931 coproc = 1;
5932 goto st;
5933 case M_SDC3_AB:
5934 s = "sdc3";
bdaaa2e1 5935 /* Itbl support may require additional care here. */
252b5132
RH
5936 coproc = 1;
5937 goto st;
5938 case M_SDL_AB:
5939 s = "sdl";
5940 goto st;
5941 case M_SDR_AB:
5942 s = "sdr";
5943 st:
5944 tempreg = AT;
5945 used_at = 1;
5946 ld_st:
bdaaa2e1 5947 /* Itbl support may require additional care here. */
252b5132
RH
5948 if (mask == M_LWC1_AB
5949 || mask == M_SWC1_AB
5950 || mask == M_LDC1_AB
5951 || mask == M_SDC1_AB
5952 || mask == M_L_DAB
5953 || mask == M_S_DAB)
5954 fmt = "T,o(b)";
5955 else if (coproc)
5956 fmt = "E,o(b)";
5957 else
5958 fmt = "t,o(b)";
5959
256ab948
TS
5960 /* Sign-extending 32-bit constants makes their handling easier.
5961 The HAVE_64BIT_GPRS... part is due to the linux kernel hack
5962 described below. */
5963 if ((! HAVE_64BIT_ADDRESSES
5964 && (! HAVE_64BIT_GPRS && offset_expr.X_op == O_constant))
d17e7bce
TS
5965 && (offset_expr.X_op == O_constant)
5966 && ! ((offset_expr.X_add_number & ~((bfd_vma) 0x7fffffff))
5967 == ~((bfd_vma) 0x7fffffff)))
ae826530 5968 {
d17e7bce 5969 if (offset_expr.X_add_number & ~((bfd_vma) 0xffffffff))
1b8e29e5 5970 as_bad (_("constant too large"));
ae826530 5971
1b8e29e5
TS
5972 offset_expr.X_add_number = (((offset_expr.X_add_number & 0xffffffff)
5973 ^ 0x80000000) - 0x80000000);
ae826530 5974 }
256ab948 5975
afdbd6d0
CD
5976 /* For embedded PIC, we allow loads where the offset is calculated
5977 by subtracting a symbol in the current segment from an unknown
5978 symbol, relative to a base register, e.g.:
5979 <op> $treg, <sym>-<localsym>($breg)
5980 This is used by the compiler for switch statements. */
76b3015f 5981 if (mips_pic == EMBEDDED_PIC
afdbd6d0
CD
5982 && offset_expr.X_op == O_subtract
5983 && (symbol_constant_p (offset_expr.X_op_symbol)
5984 ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
5985 : (symbol_equated_p (offset_expr.X_op_symbol)
5986 && (S_GET_SEGMENT
5987 (symbol_get_value_expression (offset_expr.X_op_symbol)
5988 ->X_add_symbol)
5989 == now_seg)))
5990 && breg != 0
5991 && (offset_expr.X_add_number == 0
5992 || OUTPUT_FLAVOR == bfd_target_elf_flavour))
5993 {
5994 /* For this case, we output the instructions:
5995 lui $tempreg,<sym> (BFD_RELOC_PCREL_HI16_S)
5996 addiu $tempreg,$tempreg,$breg
5997 <op> $treg,<sym>($tempreg) (BFD_RELOC_PCREL_LO16)
5998 If the relocation would fit entirely in 16 bits, it would be
5999 nice to emit:
6000 <op> $treg,<sym>($breg) (BFD_RELOC_PCREL_LO16)
6001 instead, but that seems quite difficult. */
17a2f251
TS
6002 macro_build (NULL, &icnt, &offset_expr, "lui", "t,u", tempreg,
6003 BFD_RELOC_PCREL_HI16_S);
6004 macro_build (NULL, &icnt, NULL,
afdbd6d0
CD
6005 ((bfd_arch_bits_per_address (stdoutput) == 32
6006 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
b7c7d6c1 6007 ? "addu" : "daddu"),
afdbd6d0 6008 "d,v,t", tempreg, tempreg, breg);
17a2f251
TS
6009 macro_build (NULL, &icnt, &offset_expr, s, fmt, treg,
6010 BFD_RELOC_PCREL_LO16, tempreg);
afdbd6d0
CD
6011 if (! used_at)
6012 return;
6013 break;
6014 }
6015
252b5132
RH
6016 if (offset_expr.X_op != O_constant
6017 && offset_expr.X_op != O_symbol)
6018 {
6019 as_bad (_("expression too complex"));
6020 offset_expr.X_op = O_constant;
6021 }
6022
6023 /* A constant expression in PIC code can be handled just as it
6024 is in non PIC code. */
6025 if (mips_pic == NO_PIC
6026 || offset_expr.X_op == O_constant)
6027 {
f9419b05
TS
6028 char *p;
6029
252b5132
RH
6030 /* If this is a reference to a GP relative symbol, and there
6031 is no base register, we want
cdf6fd85 6032 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
6033 Otherwise, if there is no base register, we want
6034 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
6035 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6036 If we have a constant, we need two instructions anyhow,
6037 so we always use the latter form.
6038
6039 If we have a base register, and this is a reference to a
6040 GP relative symbol, we want
6041 addu $tempreg,$breg,$gp
cdf6fd85 6042 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
6043 Otherwise we want
6044 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
6045 addu $tempreg,$tempreg,$breg
6046 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245 6047 With a constant we always use the latter case.
76b3015f 6048
d6bc6245
TS
6049 With 64bit address space and no base register and $at usable,
6050 we want
6051 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
6052 lui $at,<sym> (BFD_RELOC_HI16_S)
6053 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
6054 dsll32 $tempreg,0
6055 daddu $tempreg,$at
6056 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6057 If we have a base register, we want
6058 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
6059 lui $at,<sym> (BFD_RELOC_HI16_S)
6060 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
6061 daddu $at,$breg
6062 dsll32 $tempreg,0
6063 daddu $tempreg,$at
6064 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6065
6066 Without $at we can't generate the optimal path for superscalar
6067 processors here since this would require two temporary registers.
6068 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
6069 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
6070 dsll $tempreg,16
6071 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
6072 dsll $tempreg,16
6073 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6074 If we have a base register, we want
6075 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
6076 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
6077 dsll $tempreg,16
6078 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
6079 dsll $tempreg,16
6080 daddu $tempreg,$tempreg,$breg
6081 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6373ee54
CD
6082
6083 If we have 64-bit addresses, as an optimization, for
6084 addresses which are 32-bit constants (e.g. kseg0/kseg1
6085 addresses) we fall back to the 32-bit address generation
78d32a17
MR
6086 mechanism since it is more efficient. Note that due to
6087 the signed offset used by memory operations, the 32-bit
6088 range is shifted down by 32768 here. This code should
6373ee54
CD
6089 probably attempt to generate 64-bit constants more
6090 efficiently in general.
9214dd3b
AO
6091
6092 As an extension for architectures with 64-bit registers,
6093 we don't truncate 64-bit addresses given as literal
6094 constants down to 32 bits, to support existing practice
6095 in the mips64 Linux (the kernel), that compiles source
6096 files with -mabi=64, assembling them as o32 or n32 (with
6097 -Wa,-32 or -Wa,-n32). This is not beautiful, but since
6098 the whole kernel is loaded into a memory region that is
67c1ffbe 6099 addressable with sign-extended 32-bit addresses, it is
9214dd3b
AO
6100 wasteful to compute the upper 32 bits of every
6101 non-literal address, that takes more space and time.
6102 Some day this should probably be implemented as an
6103 assembler option, such that the kernel doesn't have to
6104 use such ugly hacks, even though it will still have to
6105 end up converting the binary to ELF32 for a number of
6106 platforms whose boot loaders don't support ELF64
6107 binaries. */
256ab948
TS
6108 if ((HAVE_64BIT_ADDRESSES
6109 && ! (offset_expr.X_op == O_constant
6110 && IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000)))
6111 || (HAVE_64BIT_GPRS
6112 && offset_expr.X_op == O_constant
6113 && ! IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000)))
d6bc6245
TS
6114 {
6115 p = NULL;
6116
6117 /* We don't do GP optimization for now because RELAX_ENCODE can't
6118 hold the data for such large chunks. */
6119
460597ba 6120 if (used_at == 0 && ! mips_opts.noat)
d6bc6245
TS
6121 {
6122 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
17a2f251 6123 tempreg, BFD_RELOC_MIPS_HIGHEST);
d6bc6245 6124 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
17a2f251 6125 AT, BFD_RELOC_HI16_S);
d6bc6245 6126 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
17a2f251 6127 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
d6bc6245 6128 if (breg != 0)
17a2f251
TS
6129 macro_build (p, &icnt, NULL, "daddu", "d,v,t",
6130 AT, AT, breg);
6131 macro_build (p, &icnt, NULL, "dsll32", "d,w,<",
6132 tempreg, tempreg, 0);
6133 macro_build (p, &icnt, NULL, "daddu", "d,v,t",
6134 tempreg, tempreg, AT);
6135 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
6136 BFD_RELOC_LO16, tempreg);
d6bc6245
TS
6137 used_at = 1;
6138 }
6139 else
6140 {
6141 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
17a2f251 6142 tempreg, BFD_RELOC_MIPS_HIGHEST);
d6bc6245 6143 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
17a2f251
TS
6144 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
6145 macro_build (p, &icnt, NULL, "dsll", "d,w,<",
6146 tempreg, tempreg, 16);
d6bc6245 6147 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
17a2f251
TS
6148 tempreg, tempreg, BFD_RELOC_HI16_S);
6149 macro_build (p, &icnt, NULL, "dsll", "d,w,<",
6150 tempreg, tempreg, 16);
d6bc6245 6151 if (breg != 0)
17a2f251
TS
6152 macro_build (p, &icnt, NULL, "daddu", "d,v,t",
6153 tempreg, tempreg, breg);
6154 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
6155 BFD_RELOC_LO16, tempreg);
d6bc6245
TS
6156 }
6157
6158 return;
6159 }
256ab948
TS
6160
6161 if (offset_expr.X_op == O_constant
ecd13cd3 6162 && ! IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000))
5a7a0b7b 6163 as_bad (_("load/store address overflow (max 32 bits)"));
76b3015f 6164
252b5132
RH
6165 if (breg == 0)
6166 {
e7d556df 6167 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
252b5132
RH
6168 || nopic_need_relax (offset_expr.X_add_symbol, 1))
6169 p = NULL;
6170 else
6171 {
6172 frag_grow (20);
17a2f251
TS
6173 macro_build (NULL, &icnt, &offset_expr, s, fmt, treg,
6174 BFD_RELOC_GPREL16, mips_gp_register);
252b5132
RH
6175 p = frag_var (rs_machine_dependent, 8, 0,
6176 RELAX_ENCODE (4, 8, 0, 4, 0,
6177 (mips_opts.warn_about_macros
6178 || (used_at
6179 && mips_opts.noat))),
956cd1d6 6180 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
6181 used_at = 0;
6182 }
6183 macro_build_lui (p, &icnt, &offset_expr, tempreg);
6184 if (p != NULL)
6185 p += 4;
6186 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
17a2f251 6187 BFD_RELOC_LO16, tempreg);
252b5132
RH
6188 }
6189 else
6190 {
e7d556df 6191 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
252b5132
RH
6192 || nopic_need_relax (offset_expr.X_add_symbol, 1))
6193 p = NULL;
6194 else
6195 {
6196 frag_grow (28);
17a2f251
TS
6197 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6198 tempreg, breg, mips_gp_register);
6199 macro_build (NULL, &icnt, &offset_expr, s, fmt, treg,
6200 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6201 p = frag_var (rs_machine_dependent, 12, 0,
6202 RELAX_ENCODE (8, 12, 0, 8, 0, 0),
956cd1d6 6203 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
6204 }
6205 macro_build_lui (p, &icnt, &offset_expr, tempreg);
6206 if (p != NULL)
6207 p += 4;
17a2f251
TS
6208 macro_build (p, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6209 tempreg, tempreg, breg);
252b5132
RH
6210 if (p != NULL)
6211 p += 4;
6212 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
17a2f251 6213 BFD_RELOC_LO16, tempreg);
252b5132
RH
6214 }
6215 }
6216 else if (mips_pic == SVR4_PIC && ! mips_big_got)
6217 {
f9419b05 6218 char *p;
ed6fb7bd 6219 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
f9419b05 6220
252b5132
RH
6221 /* If this is a reference to an external symbol, we want
6222 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6223 nop
6224 <op> $treg,0($tempreg)
6225 Otherwise we want
6226 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6227 nop
6228 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6229 <op> $treg,0($tempreg)
f5040a92
AO
6230
6231 For NewABI, we want
6232 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6233 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
6234
252b5132
RH
6235 If there is a base register, we add it to $tempreg before
6236 the <op>. If there is a constant, we stick it in the
6237 <op> instruction. We don't handle constants larger than
6238 16 bits, because we have no way to load the upper 16 bits
6239 (actually, we could handle them for the subset of cases
6240 in which we are not using $at). */
6241 assert (offset_expr.X_op == O_symbol);
f5040a92
AO
6242 if (HAVE_NEWABI)
6243 {
17a2f251
TS
6244 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
6245 "t,o(b)", tempreg, BFD_RELOC_MIPS_GOT_PAGE,
6246 mips_gp_register);
f5040a92 6247 if (breg != 0)
17a2f251
TS
6248 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6249 tempreg, tempreg, breg);
6250 macro_build (NULL, &icnt, &offset_expr, s, fmt, treg,
6251 BFD_RELOC_MIPS_GOT_OFST, tempreg);
f5040a92
AO
6252
6253 if (! used_at)
6254 return;
6255
6256 break;
6257 }
252b5132
RH
6258 expr1.X_add_number = offset_expr.X_add_number;
6259 offset_expr.X_add_number = 0;
6260 if (expr1.X_add_number < -0x8000
6261 || expr1.X_add_number >= 0x8000)
6262 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6263 frag_grow (20);
17a2f251
TS
6264 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6265 tempreg, lw_reloc_type, mips_gp_register);
6266 macro_build (NULL, &icnt, NULL, "nop", "");
bdaaa2e1 6267 p = frag_var (rs_machine_dependent, 4, 0,
252b5132 6268 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
c4e7957c 6269 offset_expr.X_add_symbol, 0, NULL);
f899b4b8 6270 macro_build (p, &icnt, &offset_expr, ADDRESS_ADDI_INSN,
17a2f251 6271 "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
252b5132 6272 if (breg != 0)
17a2f251
TS
6273 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6274 tempreg, tempreg, breg);
6275 macro_build (NULL, &icnt, &expr1, s, fmt, treg, BFD_RELOC_LO16,
6276 tempreg);
252b5132 6277 }
f5040a92 6278 else if (mips_pic == SVR4_PIC && ! HAVE_NEWABI)
252b5132
RH
6279 {
6280 int gpdel;
f9419b05 6281 char *p;
252b5132
RH
6282
6283 /* If this is a reference to an external symbol, we want
6284 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6285 addu $tempreg,$tempreg,$gp
6286 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6287 <op> $treg,0($tempreg)
6288 Otherwise we want
6289 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6290 nop
6291 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6292 <op> $treg,0($tempreg)
6293 If there is a base register, we add it to $tempreg before
6294 the <op>. If there is a constant, we stick it in the
6295 <op> instruction. We don't handle constants larger than
6296 16 bits, because we have no way to load the upper 16 bits
6297 (actually, we could handle them for the subset of cases
f5040a92 6298 in which we are not using $at). */
252b5132
RH
6299 assert (offset_expr.X_op == O_symbol);
6300 expr1.X_add_number = offset_expr.X_add_number;
6301 offset_expr.X_add_number = 0;
6302 if (expr1.X_add_number < -0x8000
6303 || expr1.X_add_number >= 0x8000)
6304 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
f7ea7ef2 6305 if (reg_needs_delay (mips_gp_register))
252b5132
RH
6306 gpdel = 4;
6307 else
6308 gpdel = 0;
6309 frag_grow (36);
17a2f251
TS
6310 macro_build (NULL, &icnt, &offset_expr, "lui", "t,u", tempreg,
6311 BFD_RELOC_MIPS_GOT_HI16);
6312 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6313 tempreg, tempreg, mips_gp_register);
6314 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6315 tempreg, BFD_RELOC_MIPS_GOT_LO16, tempreg);
252b5132
RH
6316 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
6317 RELAX_ENCODE (12, 12 + gpdel, gpdel, 8 + gpdel, 0, 0),
c4e7957c 6318 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
6319 if (gpdel > 0)
6320 {
17a2f251 6321 macro_build (p, &icnt, NULL, "nop", "");
252b5132
RH
6322 p += 4;
6323 }
17a2f251
TS
6324 macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6325 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132 6326 p += 4;
17a2f251 6327 macro_build (p, &icnt, NULL, "nop", "");
252b5132 6328 p += 4;
17a2f251
TS
6329 macro_build (p, &icnt, &offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
6330 tempreg, tempreg, BFD_RELOC_LO16);
252b5132 6331 if (breg != 0)
17a2f251
TS
6332 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6333 tempreg, tempreg, breg);
6334 macro_build (NULL, &icnt, &expr1, s, fmt, treg, BFD_RELOC_LO16,
6335 tempreg);
252b5132 6336 }
f5040a92
AO
6337 else if (mips_pic == SVR4_PIC && HAVE_NEWABI)
6338 {
6339 char *p;
6340 int bregsz = breg != 0 ? 4 : 0;
6341
6342 /* If this is a reference to an external symbol, we want
6343 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6344 add $tempreg,$tempreg,$gp
6345 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6346 <op> $treg,<ofst>($tempreg)
6347 Otherwise, for local symbols, we want:
6348 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6349 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
6350 assert (offset_expr.X_op == O_symbol);
0a6ace1e 6351 frag_grow (36);
f5040a92
AO
6352 frag_now->tc_frag_data.tc_fr_offset =
6353 expr1.X_add_number = offset_expr.X_add_number;
6354 offset_expr.X_add_number = 0;
6355 if (expr1.X_add_number < -0x8000
6356 || expr1.X_add_number >= 0x8000)
6357 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
17a2f251
TS
6358 macro_build (NULL, &icnt, &offset_expr, "lui", "t,u", tempreg,
6359 BFD_RELOC_MIPS_GOT_HI16);
6360 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6361 tempreg, tempreg, mips_gp_register);
6362 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6363 tempreg, BFD_RELOC_MIPS_GOT_LO16, tempreg);
f5040a92 6364 if (breg != 0)
17a2f251
TS
6365 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6366 tempreg, tempreg, breg);
6367 macro_build (NULL, &icnt, &expr1, s, fmt, treg, BFD_RELOC_LO16,
6368 tempreg);
684022ea 6369
f5040a92
AO
6370 offset_expr.X_add_number = expr1.X_add_number;
6371 p = frag_var (rs_machine_dependent, 12 + bregsz, 0,
6372 RELAX_ENCODE (16 + bregsz, 8 + bregsz,
6373 0, 4 + bregsz, 0, 0),
6374 offset_expr.X_add_symbol, 0, NULL);
f899b4b8 6375 macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 6376 tempreg, BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 6377 if (breg != 0)
17a2f251
TS
6378 macro_build (p + 4, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6379 tempreg, tempreg, breg);
f5040a92 6380 macro_build (p + 4 + bregsz, &icnt, &offset_expr, s, fmt, treg,
17a2f251 6381 BFD_RELOC_MIPS_GOT_OFST, tempreg);
f5040a92 6382 }
252b5132
RH
6383 else if (mips_pic == EMBEDDED_PIC)
6384 {
6385 /* If there is no base register, we want
cdf6fd85 6386 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
6387 If there is a base register, we want
6388 addu $tempreg,$breg,$gp
cdf6fd85 6389 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
6390 */
6391 assert (offset_expr.X_op == O_symbol);
6392 if (breg == 0)
6393 {
17a2f251
TS
6394 macro_build (NULL, &icnt, &offset_expr, s, fmt, treg,
6395 BFD_RELOC_GPREL16, mips_gp_register);
252b5132
RH
6396 used_at = 0;
6397 }
6398 else
6399 {
17a2f251
TS
6400 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6401 tempreg, breg, mips_gp_register);
6402 macro_build (NULL, &icnt, &offset_expr, s, fmt, treg,
6403 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6404 }
6405 }
6406 else
6407 abort ();
6408
6409 if (! used_at)
6410 return;
6411
6412 break;
6413
6414 case M_LI:
6415 case M_LI_S:
6416 load_register (&icnt, treg, &imm_expr, 0);
6417 return;
6418
6419 case M_DLI:
6420 load_register (&icnt, treg, &imm_expr, 1);
6421 return;
6422
6423 case M_LI_SS:
6424 if (imm_expr.X_op == O_constant)
6425 {
6426 load_register (&icnt, AT, &imm_expr, 0);
17a2f251 6427 macro_build (NULL, &icnt, NULL, "mtc1", "t,G", AT, treg);
252b5132
RH
6428 break;
6429 }
6430 else
6431 {
6432 assert (offset_expr.X_op == O_symbol
6433 && strcmp (segment_name (S_GET_SEGMENT
6434 (offset_expr.X_add_symbol)),
6435 ".lit4") == 0
6436 && offset_expr.X_add_number == 0);
17a2f251
TS
6437 macro_build (NULL, &icnt, &offset_expr, "lwc1", "T,o(b)", treg,
6438 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
252b5132
RH
6439 return;
6440 }
6441
6442 case M_LI_D:
ca4e0257
RS
6443 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
6444 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
6445 order 32 bits of the value and the low order 32 bits are either
6446 zero or in OFFSET_EXPR. */
252b5132
RH
6447 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6448 {
ca4e0257 6449 if (HAVE_64BIT_GPRS)
252b5132
RH
6450 load_register (&icnt, treg, &imm_expr, 1);
6451 else
6452 {
6453 int hreg, lreg;
6454
6455 if (target_big_endian)
6456 {
6457 hreg = treg;
6458 lreg = treg + 1;
6459 }
6460 else
6461 {
6462 hreg = treg + 1;
6463 lreg = treg;
6464 }
6465
6466 if (hreg <= 31)
6467 load_register (&icnt, hreg, &imm_expr, 0);
6468 if (lreg <= 31)
6469 {
6470 if (offset_expr.X_op == O_absent)
ea1fb5dc 6471 move_register (&icnt, lreg, 0);
252b5132
RH
6472 else
6473 {
6474 assert (offset_expr.X_op == O_constant);
6475 load_register (&icnt, lreg, &offset_expr, 0);
6476 }
6477 }
6478 }
6479 return;
6480 }
6481
6482 /* We know that sym is in the .rdata section. First we get the
6483 upper 16 bits of the address. */
6484 if (mips_pic == NO_PIC)
6485 {
956cd1d6 6486 macro_build_lui (NULL, &icnt, &offset_expr, AT);
252b5132
RH
6487 }
6488 else if (mips_pic == SVR4_PIC)
6489 {
17a2f251
TS
6490 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6491 AT, BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132
RH
6492 }
6493 else if (mips_pic == EMBEDDED_PIC)
6494 {
6495 /* For embedded PIC we pick up the entire address off $gp in
6496 a single instruction. */
17a2f251
TS
6497 macro_build (NULL, &icnt, &offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
6498 AT, mips_gp_register, BFD_RELOC_GPREL16);
252b5132
RH
6499 offset_expr.X_op = O_constant;
6500 offset_expr.X_add_number = 0;
6501 }
6502 else
6503 abort ();
bdaaa2e1 6504
252b5132 6505 /* Now we load the register(s). */
ca4e0257 6506 if (HAVE_64BIT_GPRS)
17a2f251
TS
6507 macro_build (NULL, &icnt, &offset_expr, "ld", "t,o(b)", treg,
6508 BFD_RELOC_LO16, AT);
252b5132
RH
6509 else
6510 {
17a2f251
TS
6511 macro_build (NULL, &icnt, &offset_expr, "lw", "t,o(b)", treg,
6512 BFD_RELOC_LO16, AT);
f9419b05 6513 if (treg != RA)
252b5132
RH
6514 {
6515 /* FIXME: How in the world do we deal with the possible
6516 overflow here? */
6517 offset_expr.X_add_number += 4;
17a2f251
TS
6518 macro_build (NULL, &icnt, &offset_expr, "lw", "t,o(b)",
6519 treg + 1, BFD_RELOC_LO16, AT);
252b5132
RH
6520 }
6521 }
6522
6523 /* To avoid confusion in tc_gen_reloc, we must ensure that this
6524 does not become a variant frag. */
6525 frag_wane (frag_now);
6526 frag_new (0);
6527
6528 break;
6529
6530 case M_LI_DD:
ca4e0257
RS
6531 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
6532 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
6533 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
6534 the value and the low order 32 bits are either zero or in
6535 OFFSET_EXPR. */
252b5132
RH
6536 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6537 {
ca4e0257
RS
6538 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_FPRS);
6539 if (HAVE_64BIT_FPRS)
6540 {
6541 assert (HAVE_64BIT_GPRS);
17a2f251 6542 macro_build (NULL, &icnt, NULL, "dmtc1", "t,S", AT, treg);
ca4e0257 6543 }
252b5132
RH
6544 else
6545 {
17a2f251 6546 macro_build (NULL, &icnt, NULL, "mtc1", "t,G", AT, treg + 1);
252b5132 6547 if (offset_expr.X_op == O_absent)
17a2f251 6548 macro_build (NULL, &icnt, NULL, "mtc1", "t,G", 0, treg);
252b5132
RH
6549 else
6550 {
6551 assert (offset_expr.X_op == O_constant);
6552 load_register (&icnt, AT, &offset_expr, 0);
17a2f251 6553 macro_build (NULL, &icnt, NULL, "mtc1", "t,G", AT, treg);
252b5132
RH
6554 }
6555 }
6556 break;
6557 }
6558
6559 assert (offset_expr.X_op == O_symbol
6560 && offset_expr.X_add_number == 0);
6561 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
6562 if (strcmp (s, ".lit8") == 0)
6563 {
e7af610e 6564 if (mips_opts.isa != ISA_MIPS1)
252b5132 6565 {
17a2f251
TS
6566 macro_build (NULL, &icnt, &offset_expr, "ldc1", "T,o(b)", treg,
6567 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
252b5132
RH
6568 return;
6569 }
c9914766 6570 breg = mips_gp_register;
252b5132
RH
6571 r = BFD_RELOC_MIPS_LITERAL;
6572 goto dob;
6573 }
6574 else
6575 {
6576 assert (strcmp (s, RDATA_SECTION_NAME) == 0);
6577 if (mips_pic == SVR4_PIC)
17a2f251
TS
6578 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
6579 "t,o(b)", AT, BFD_RELOC_MIPS_GOT16,
6580 mips_gp_register);
252b5132
RH
6581 else
6582 {
6583 /* FIXME: This won't work for a 64 bit address. */
956cd1d6 6584 macro_build_lui (NULL, &icnt, &offset_expr, AT);
252b5132 6585 }
bdaaa2e1 6586
e7af610e 6587 if (mips_opts.isa != ISA_MIPS1)
252b5132 6588 {
17a2f251
TS
6589 macro_build (NULL, &icnt, &offset_expr, "ldc1", "T,o(b)", treg,
6590 BFD_RELOC_LO16, AT);
252b5132
RH
6591
6592 /* To avoid confusion in tc_gen_reloc, we must ensure
6593 that this does not become a variant frag. */
6594 frag_wane (frag_now);
6595 frag_new (0);
6596
6597 break;
6598 }
6599 breg = AT;
6600 r = BFD_RELOC_LO16;
6601 goto dob;
6602 }
6603
6604 case M_L_DOB:
fef14a42 6605 if (mips_opts.arch == CPU_R4650)
252b5132
RH
6606 {
6607 as_bad (_("opcode not supported on this processor"));
6608 return;
6609 }
6610 /* Even on a big endian machine $fn comes before $fn+1. We have
6611 to adjust when loading from memory. */
6612 r = BFD_RELOC_LO16;
6613 dob:
e7af610e 6614 assert (mips_opts.isa == ISA_MIPS1);
17a2f251
TS
6615 macro_build (NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6616 target_big_endian ? treg + 1 : treg, r, breg);
252b5132
RH
6617 /* FIXME: A possible overflow which I don't know how to deal
6618 with. */
6619 offset_expr.X_add_number += 4;
17a2f251
TS
6620 macro_build (NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6621 target_big_endian ? treg : treg + 1, r, breg);
252b5132
RH
6622
6623 /* To avoid confusion in tc_gen_reloc, we must ensure that this
6624 does not become a variant frag. */
6625 frag_wane (frag_now);
6626 frag_new (0);
6627
6628 if (breg != AT)
6629 return;
6630 break;
6631
6632 case M_L_DAB:
6633 /*
6634 * The MIPS assembler seems to check for X_add_number not
6635 * being double aligned and generating:
6636 * lui at,%hi(foo+1)
6637 * addu at,at,v1
6638 * addiu at,at,%lo(foo+1)
6639 * lwc1 f2,0(at)
6640 * lwc1 f3,4(at)
6641 * But, the resulting address is the same after relocation so why
6642 * generate the extra instruction?
6643 */
fef14a42 6644 if (mips_opts.arch == CPU_R4650)
252b5132
RH
6645 {
6646 as_bad (_("opcode not supported on this processor"));
6647 return;
6648 }
bdaaa2e1 6649 /* Itbl support may require additional care here. */
252b5132 6650 coproc = 1;
e7af610e 6651 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
6652 {
6653 s = "ldc1";
6654 goto ld;
6655 }
6656
6657 s = "lwc1";
6658 fmt = "T,o(b)";
6659 goto ldd_std;
6660
6661 case M_S_DAB:
fef14a42 6662 if (mips_opts.arch == CPU_R4650)
252b5132
RH
6663 {
6664 as_bad (_("opcode not supported on this processor"));
6665 return;
6666 }
6667
e7af610e 6668 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
6669 {
6670 s = "sdc1";
6671 goto st;
6672 }
6673
6674 s = "swc1";
6675 fmt = "T,o(b)";
bdaaa2e1 6676 /* Itbl support may require additional care here. */
252b5132
RH
6677 coproc = 1;
6678 goto ldd_std;
6679
6680 case M_LD_AB:
ca4e0257 6681 if (HAVE_64BIT_GPRS)
252b5132
RH
6682 {
6683 s = "ld";
6684 goto ld;
6685 }
6686
6687 s = "lw";
6688 fmt = "t,o(b)";
6689 goto ldd_std;
6690
6691 case M_SD_AB:
ca4e0257 6692 if (HAVE_64BIT_GPRS)
252b5132
RH
6693 {
6694 s = "sd";
6695 goto st;
6696 }
6697
6698 s = "sw";
6699 fmt = "t,o(b)";
6700
6701 ldd_std:
afdbd6d0
CD
6702 /* We do _not_ bother to allow embedded PIC (symbol-local_symbol)
6703 loads for the case of doing a pair of loads to simulate an 'ld'.
6704 This is not currently done by the compiler, and assembly coders
6705 writing embedded-pic code can cope. */
6706
252b5132
RH
6707 if (offset_expr.X_op != O_symbol
6708 && offset_expr.X_op != O_constant)
6709 {
6710 as_bad (_("expression too complex"));
6711 offset_expr.X_op = O_constant;
6712 }
6713
6714 /* Even on a big endian machine $fn comes before $fn+1. We have
6715 to adjust when loading from memory. We set coproc if we must
6716 load $fn+1 first. */
bdaaa2e1 6717 /* Itbl support may require additional care here. */
252b5132
RH
6718 if (! target_big_endian)
6719 coproc = 0;
6720
6721 if (mips_pic == NO_PIC
6722 || offset_expr.X_op == O_constant)
6723 {
f9419b05
TS
6724 char *p;
6725
252b5132 6726 /* If this is a reference to a GP relative symbol, we want
cdf6fd85
TS
6727 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6728 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
6729 If we have a base register, we use this
6730 addu $at,$breg,$gp
cdf6fd85
TS
6731 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6732 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
6733 If this is not a GP relative symbol, we want
6734 lui $at,<sym> (BFD_RELOC_HI16_S)
6735 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6736 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6737 If there is a base register, we add it to $at after the
6738 lui instruction. If there is a constant, we always use
6739 the last case. */
e7d556df 6740 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
252b5132
RH
6741 || nopic_need_relax (offset_expr.X_add_symbol, 1))
6742 {
6743 p = NULL;
6744 used_at = 1;
6745 }
6746 else
6747 {
6748 int off;
6749
6750 if (breg == 0)
6751 {
6752 frag_grow (28);
c9914766 6753 tempreg = mips_gp_register;
252b5132
RH
6754 off = 0;
6755 used_at = 0;
6756 }
6757 else
6758 {
6759 frag_grow (36);
17a2f251
TS
6760 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6761 AT, breg, mips_gp_register);
252b5132
RH
6762 tempreg = AT;
6763 off = 4;
6764 used_at = 1;
6765 }
6766
beae10d5 6767 /* Itbl support may require additional care here. */
17a2f251 6768 macro_build (NULL, &icnt, &offset_expr, s, fmt,
252b5132 6769 coproc ? treg + 1 : treg,
17a2f251 6770 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6771 offset_expr.X_add_number += 4;
6772
6773 /* Set mips_optimize to 2 to avoid inserting an
6774 undesired nop. */
6775 hold_mips_optimize = mips_optimize;
6776 mips_optimize = 2;
beae10d5 6777 /* Itbl support may require additional care here. */
17a2f251 6778 macro_build (NULL, &icnt, &offset_expr, s, fmt,
252b5132 6779 coproc ? treg : treg + 1,
17a2f251 6780 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6781 mips_optimize = hold_mips_optimize;
6782
6783 p = frag_var (rs_machine_dependent, 12 + off, 0,
6784 RELAX_ENCODE (8 + off, 12 + off, 0, 4 + off, 1,
6785 used_at && mips_opts.noat),
956cd1d6 6786 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
6787
6788 /* We just generated two relocs. When tc_gen_reloc
6789 handles this case, it will skip the first reloc and
6790 handle the second. The second reloc already has an
6791 extra addend of 4, which we added above. We must
6792 subtract it out, and then subtract another 4 to make
6793 the first reloc come out right. The second reloc
6794 will come out right because we are going to add 4 to
6795 offset_expr when we build its instruction below.
6796
6797 If we have a symbol, then we don't want to include
6798 the offset, because it will wind up being included
6799 when we generate the reloc. */
6800
6801 if (offset_expr.X_op == O_constant)
6802 offset_expr.X_add_number -= 8;
6803 else
6804 {
6805 offset_expr.X_add_number = -4;
6806 offset_expr.X_op = O_constant;
6807 }
6808 }
6809 macro_build_lui (p, &icnt, &offset_expr, AT);
6810 if (p != NULL)
6811 p += 4;
6812 if (breg != 0)
6813 {
17a2f251
TS
6814 macro_build (p, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6815 AT, breg, AT);
252b5132
RH
6816 if (p != NULL)
6817 p += 4;
6818 }
beae10d5 6819 /* Itbl support may require additional care here. */
252b5132
RH
6820 macro_build (p, &icnt, &offset_expr, s, fmt,
6821 coproc ? treg + 1 : treg,
17a2f251 6822 BFD_RELOC_LO16, AT);
252b5132
RH
6823 if (p != NULL)
6824 p += 4;
6825 /* FIXME: How do we handle overflow here? */
6826 offset_expr.X_add_number += 4;
beae10d5 6827 /* Itbl support may require additional care here. */
252b5132
RH
6828 macro_build (p, &icnt, &offset_expr, s, fmt,
6829 coproc ? treg : treg + 1,
17a2f251 6830 BFD_RELOC_LO16, AT);
bdaaa2e1 6831 }
252b5132
RH
6832 else if (mips_pic == SVR4_PIC && ! mips_big_got)
6833 {
6834 int off;
6835
6836 /* If this is a reference to an external symbol, we want
6837 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6838 nop
6839 <op> $treg,0($at)
6840 <op> $treg+1,4($at)
6841 Otherwise we want
6842 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6843 nop
6844 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6845 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6846 If there is a base register we add it to $at before the
6847 lwc1 instructions. If there is a constant we include it
6848 in the lwc1 instructions. */
6849 used_at = 1;
6850 expr1.X_add_number = offset_expr.X_add_number;
6851 offset_expr.X_add_number = 0;
6852 if (expr1.X_add_number < -0x8000
6853 || expr1.X_add_number >= 0x8000 - 4)
6854 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6855 if (breg == 0)
6856 off = 0;
6857 else
6858 off = 4;
6859 frag_grow (24 + off);
17a2f251
TS
6860 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6861 AT, BFD_RELOC_MIPS_GOT16, mips_gp_register);
6862 macro_build (NULL, &icnt, NULL, "nop", "");
252b5132 6863 if (breg != 0)
17a2f251
TS
6864 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6865 AT, breg, AT);
beae10d5 6866 /* Itbl support may require additional care here. */
17a2f251
TS
6867 macro_build (NULL, &icnt, &expr1, s, fmt, coproc ? treg + 1 : treg,
6868 BFD_RELOC_LO16, AT);
252b5132
RH
6869 expr1.X_add_number += 4;
6870
6871 /* Set mips_optimize to 2 to avoid inserting an undesired
6872 nop. */
6873 hold_mips_optimize = mips_optimize;
6874 mips_optimize = 2;
beae10d5 6875 /* Itbl support may require additional care here. */
17a2f251
TS
6876 macro_build (NULL, &icnt, &expr1, s, fmt, coproc ? treg : treg + 1,
6877 BFD_RELOC_LO16, AT);
252b5132
RH
6878 mips_optimize = hold_mips_optimize;
6879
6880 (void) frag_var (rs_machine_dependent, 0, 0,
6881 RELAX_ENCODE (0, 0, -16 - off, -8, 1, 0),
c4e7957c 6882 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
6883 }
6884 else if (mips_pic == SVR4_PIC)
6885 {
6886 int gpdel, off;
f9419b05 6887 char *p;
252b5132
RH
6888
6889 /* If this is a reference to an external symbol, we want
6890 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6891 addu $at,$at,$gp
6892 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
6893 nop
6894 <op> $treg,0($at)
6895 <op> $treg+1,4($at)
6896 Otherwise we want
6897 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6898 nop
6899 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6900 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6901 If there is a base register we add it to $at before the
6902 lwc1 instructions. If there is a constant we include it
6903 in the lwc1 instructions. */
6904 used_at = 1;
6905 expr1.X_add_number = offset_expr.X_add_number;
6906 offset_expr.X_add_number = 0;
6907 if (expr1.X_add_number < -0x8000
6908 || expr1.X_add_number >= 0x8000 - 4)
6909 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
c9914766 6910 if (reg_needs_delay (mips_gp_register))
252b5132
RH
6911 gpdel = 4;
6912 else
6913 gpdel = 0;
6914 if (breg == 0)
6915 off = 0;
6916 else
6917 off = 4;
6918 frag_grow (56);
17a2f251
TS
6919 macro_build (NULL, &icnt, &offset_expr, "lui", "t,u", AT,
6920 BFD_RELOC_MIPS_GOT_HI16);
6921 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6922 AT, AT, mips_gp_register);
6923 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6924 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
6925 macro_build (NULL, &icnt, NULL, "nop", "");
252b5132 6926 if (breg != 0)
17a2f251
TS
6927 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6928 AT, breg, AT);
beae10d5 6929 /* Itbl support may require additional care here. */
17a2f251
TS
6930 macro_build (NULL, &icnt, &expr1, s, fmt, coproc ? treg + 1 : treg,
6931 BFD_RELOC_LO16, AT);
252b5132
RH
6932 expr1.X_add_number += 4;
6933
6934 /* Set mips_optimize to 2 to avoid inserting an undesired
6935 nop. */
6936 hold_mips_optimize = mips_optimize;
6937 mips_optimize = 2;
beae10d5 6938 /* Itbl support may require additional care here. */
17a2f251
TS
6939 macro_build (NULL, &icnt, &expr1, s, fmt, coproc ? treg : treg + 1,
6940 BFD_RELOC_LO16, AT);
252b5132
RH
6941 mips_optimize = hold_mips_optimize;
6942 expr1.X_add_number -= 4;
6943
6944 p = frag_var (rs_machine_dependent, 16 + gpdel + off, 0,
6945 RELAX_ENCODE (24 + off, 16 + gpdel + off, gpdel,
6946 8 + gpdel + off, 1, 0),
c4e7957c 6947 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
6948 if (gpdel > 0)
6949 {
17a2f251 6950 macro_build (p, &icnt, NULL, "nop", "");
252b5132
RH
6951 p += 4;
6952 }
17a2f251
TS
6953 macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6954 AT, BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132 6955 p += 4;
17a2f251 6956 macro_build (p, &icnt, NULL, "nop", "");
252b5132
RH
6957 p += 4;
6958 if (breg != 0)
6959 {
17a2f251
TS
6960 macro_build (p, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6961 AT, breg, AT);
252b5132
RH
6962 p += 4;
6963 }
beae10d5 6964 /* Itbl support may require additional care here. */
17a2f251
TS
6965 macro_build (p, &icnt, &expr1, s, fmt, coproc ? treg + 1 : treg,
6966 BFD_RELOC_LO16, AT);
252b5132
RH
6967 p += 4;
6968 expr1.X_add_number += 4;
6969
6970 /* Set mips_optimize to 2 to avoid inserting an undesired
6971 nop. */
6972 hold_mips_optimize = mips_optimize;
6973 mips_optimize = 2;
beae10d5 6974 /* Itbl support may require additional care here. */
17a2f251
TS
6975 macro_build (p, &icnt, &expr1, s, fmt, coproc ? treg : treg + 1,
6976 BFD_RELOC_LO16, AT);
252b5132
RH
6977 mips_optimize = hold_mips_optimize;
6978 }
6979 else if (mips_pic == EMBEDDED_PIC)
6980 {
6981 /* If there is no base register, we use
cdf6fd85
TS
6982 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6983 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
6984 If we have a base register, we use
6985 addu $at,$breg,$gp
cdf6fd85
TS
6986 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6987 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
6988 */
6989 if (breg == 0)
6990 {
c9914766 6991 tempreg = mips_gp_register;
252b5132
RH
6992 used_at = 0;
6993 }
6994 else
6995 {
17a2f251
TS
6996 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6997 AT, breg, mips_gp_register);
252b5132
RH
6998 tempreg = AT;
6999 used_at = 1;
7000 }
7001
beae10d5 7002 /* Itbl support may require additional care here. */
17a2f251 7003 macro_build (NULL, &icnt, &offset_expr, s, fmt,
252b5132 7004 coproc ? treg + 1 : treg,
17a2f251 7005 BFD_RELOC_GPREL16, tempreg);
252b5132 7006 offset_expr.X_add_number += 4;
beae10d5 7007 /* Itbl support may require additional care here. */
17a2f251 7008 macro_build (NULL, &icnt, &offset_expr, s, fmt,
252b5132 7009 coproc ? treg : treg + 1,
17a2f251 7010 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
7011 }
7012 else
7013 abort ();
7014
7015 if (! used_at)
7016 return;
7017
7018 break;
7019
7020 case M_LD_OB:
7021 s = "lw";
7022 goto sd_ob;
7023 case M_SD_OB:
7024 s = "sw";
7025 sd_ob:
ca4e0257 7026 assert (HAVE_32BIT_ADDRESSES);
17a2f251
TS
7027 macro_build (NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7028 BFD_RELOC_LO16, breg);
252b5132 7029 offset_expr.X_add_number += 4;
17a2f251
TS
7030 macro_build (NULL, &icnt, &offset_expr, s, "t,o(b)", treg + 1,
7031 BFD_RELOC_LO16, breg);
252b5132
RH
7032 return;
7033
7034 /* New code added to support COPZ instructions.
7035 This code builds table entries out of the macros in mip_opcodes.
7036 R4000 uses interlocks to handle coproc delays.
7037 Other chips (like the R3000) require nops to be inserted for delays.
7038
f72c8c98 7039 FIXME: Currently, we require that the user handle delays.
252b5132
RH
7040 In order to fill delay slots for non-interlocked chips,
7041 we must have a way to specify delays based on the coprocessor.
7042 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
7043 What are the side-effects of the cop instruction?
7044 What cache support might we have and what are its effects?
7045 Both coprocessor & memory require delays. how long???
bdaaa2e1 7046 What registers are read/set/modified?
252b5132
RH
7047
7048 If an itbl is provided to interpret cop instructions,
bdaaa2e1 7049 this knowledge can be encoded in the itbl spec. */
252b5132
RH
7050
7051 case M_COP0:
7052 s = "c0";
7053 goto copz;
7054 case M_COP1:
7055 s = "c1";
7056 goto copz;
7057 case M_COP2:
7058 s = "c2";
7059 goto copz;
7060 case M_COP3:
7061 s = "c3";
7062 copz:
7063 /* For now we just do C (same as Cz). The parameter will be
7064 stored in insn_opcode by mips_ip. */
17a2f251 7065 macro_build (NULL, &icnt, NULL, s, "C", ip->insn_opcode);
252b5132
RH
7066 return;
7067
ea1fb5dc
RS
7068 case M_MOVE:
7069 move_register (&icnt, dreg, sreg);
7070 return;
7071
252b5132
RH
7072#ifdef LOSING_COMPILER
7073 default:
7074 /* Try and see if this is a new itbl instruction.
7075 This code builds table entries out of the macros in mip_opcodes.
7076 FIXME: For now we just assemble the expression and pass it's
7077 value along as a 32-bit immediate.
bdaaa2e1 7078 We may want to have the assembler assemble this value,
252b5132
RH
7079 so that we gain the assembler's knowledge of delay slots,
7080 symbols, etc.
7081 Would it be more efficient to use mask (id) here? */
bdaaa2e1 7082 if (itbl_have_entries
252b5132 7083 && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
beae10d5 7084 {
252b5132
RH
7085 s = ip->insn_mo->name;
7086 s2 = "cop3";
7087 coproc = ITBL_DECODE_PNUM (immed_expr);;
17a2f251 7088 macro_build (NULL, &icnt, &immed_expr, s, "C");
252b5132 7089 return;
beae10d5 7090 }
252b5132
RH
7091 macro2 (ip);
7092 return;
7093 }
7094 if (mips_opts.noat)
7095 as_warn (_("Macro used $at after \".set noat\""));
7096}
bdaaa2e1 7097
252b5132 7098static void
17a2f251 7099macro2 (struct mips_cl_insn *ip)
252b5132
RH
7100{
7101 register int treg, sreg, dreg, breg;
7102 int tempreg;
7103 int mask;
7104 int icnt = 0;
7105 int used_at;
7106 expressionS expr1;
7107 const char *s;
7108 const char *s2;
7109 const char *fmt;
7110 int likely = 0;
7111 int dbl = 0;
7112 int coproc = 0;
7113 int lr = 0;
7114 int imm = 0;
7115 int off;
7116 offsetT maxnum;
7117 bfd_reloc_code_real_type r;
7118 char *p;
bdaaa2e1 7119
252b5132
RH
7120 treg = (ip->insn_opcode >> 16) & 0x1f;
7121 dreg = (ip->insn_opcode >> 11) & 0x1f;
7122 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
7123 mask = ip->insn_mo->mask;
bdaaa2e1 7124
252b5132
RH
7125 expr1.X_op = O_constant;
7126 expr1.X_op_symbol = NULL;
7127 expr1.X_add_symbol = NULL;
7128 expr1.X_add_number = 1;
bdaaa2e1 7129
252b5132
RH
7130 switch (mask)
7131 {
7132#endif /* LOSING_COMPILER */
7133
7134 case M_DMUL:
7135 dbl = 1;
7136 case M_MUL:
17a2f251
TS
7137 macro_build (NULL, &icnt, NULL, dbl ? "dmultu" : "multu", "s,t",
7138 sreg, treg);
7139 macro_build (NULL, &icnt, NULL, "mflo", "d", dreg);
252b5132
RH
7140 return;
7141
7142 case M_DMUL_I:
7143 dbl = 1;
7144 case M_MUL_I:
7145 /* The MIPS assembler some times generates shifts and adds. I'm
7146 not trying to be that fancy. GCC should do this for us
7147 anyway. */
7148 load_register (&icnt, AT, &imm_expr, dbl);
17a2f251
TS
7149 macro_build (NULL, &icnt, NULL, dbl ? "dmult" : "mult", "s,t",
7150 sreg, AT);
7151 macro_build (NULL, &icnt, NULL, "mflo", "d", dreg);
252b5132
RH
7152 break;
7153
7154 case M_DMULO_I:
7155 dbl = 1;
7156 case M_MULO_I:
7157 imm = 1;
7158 goto do_mulo;
7159
7160 case M_DMULO:
7161 dbl = 1;
7162 case M_MULO:
7163 do_mulo:
b34976b6 7164 mips_emit_delays (TRUE);
252b5132
RH
7165 ++mips_opts.noreorder;
7166 mips_any_noreorder = 1;
7167 if (imm)
7168 load_register (&icnt, AT, &imm_expr, dbl);
17a2f251
TS
7169 macro_build (NULL, &icnt, NULL, dbl ? "dmult" : "mult", "s,t",
7170 sreg, imm ? AT : treg);
7171 macro_build (NULL, &icnt, NULL, "mflo", "d", dreg);
7172 macro_build (NULL, &icnt, NULL, dbl ? "dsra32" : "sra", "d,w,<",
7173 dreg, dreg, RA);
7174 macro_build (NULL, &icnt, NULL, "mfhi", "d", AT);
252b5132 7175 if (mips_trap)
17a2f251 7176 macro_build (NULL, &icnt, NULL, "tne", "s,t,q", dreg, AT, 6);
252b5132
RH
7177 else
7178 {
7179 expr1.X_add_number = 8;
17a2f251
TS
7180 macro_build (NULL, &icnt, &expr1, "beq", "s,t,p", dreg, AT);
7181 macro_build (NULL, &icnt, NULL, "nop", "", 0);
7182 macro_build (NULL, &icnt, NULL, "break", "c", 6);
252b5132
RH
7183 }
7184 --mips_opts.noreorder;
17a2f251 7185 macro_build (NULL, &icnt, NULL, "mflo", "d", dreg);
252b5132
RH
7186 break;
7187
7188 case M_DMULOU_I:
7189 dbl = 1;
7190 case M_MULOU_I:
7191 imm = 1;
7192 goto do_mulou;
7193
7194 case M_DMULOU:
7195 dbl = 1;
7196 case M_MULOU:
7197 do_mulou:
b34976b6 7198 mips_emit_delays (TRUE);
252b5132
RH
7199 ++mips_opts.noreorder;
7200 mips_any_noreorder = 1;
7201 if (imm)
7202 load_register (&icnt, AT, &imm_expr, dbl);
17a2f251
TS
7203 macro_build (NULL, &icnt, NULL, dbl ? "dmultu" : "multu", "s,t",
7204 sreg, imm ? AT : treg);
7205 macro_build (NULL, &icnt, NULL, "mfhi", "d", AT);
7206 macro_build (NULL, &icnt, NULL, "mflo", "d", dreg);
252b5132 7207 if (mips_trap)
17a2f251 7208 macro_build (NULL, &icnt, NULL, "tne", "s,t,q", AT, 0, 6);
252b5132
RH
7209 else
7210 {
7211 expr1.X_add_number = 8;
17a2f251
TS
7212 macro_build (NULL, &icnt, &expr1, "beq", "s,t,p", AT, 0);
7213 macro_build (NULL, &icnt, NULL, "nop", "", 0);
7214 macro_build (NULL, &icnt, NULL, "break", "c", 6);
252b5132
RH
7215 }
7216 --mips_opts.noreorder;
7217 break;
7218
771c7ce4 7219 case M_DROL:
fef14a42 7220 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097
CD
7221 {
7222 if (dreg == sreg)
7223 {
7224 tempreg = AT;
7225 used_at = 1;
7226 }
7227 else
7228 {
7229 tempreg = dreg;
7230 used_at = 0;
7231 }
17a2f251
TS
7232 macro_build (NULL, &icnt, NULL, "dnegu", "d,w", tempreg, treg);
7233 macro_build (NULL, &icnt, NULL, "drorv", "d,t,s", dreg, sreg,
7234 tempreg);
82dd0097
CD
7235 if (used_at)
7236 break;
7237 return;
7238 }
17a2f251
TS
7239 macro_build (NULL, &icnt, NULL, "dsubu", "d,v,t", AT, 0, treg);
7240 macro_build (NULL, &icnt, NULL, "dsrlv", "d,t,s", AT, sreg, AT);
7241 macro_build (NULL, &icnt, NULL, "dsllv", "d,t,s", dreg, sreg, treg);
7242 macro_build (NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
7243 break;
7244
252b5132 7245 case M_ROL:
fef14a42 7246 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097
CD
7247 {
7248 if (dreg == sreg)
7249 {
7250 tempreg = AT;
7251 used_at = 1;
7252 }
7253 else
7254 {
7255 tempreg = dreg;
7256 used_at = 0;
7257 }
17a2f251
TS
7258 macro_build (NULL, &icnt, NULL, "negu", "d,w", tempreg, treg);
7259 macro_build (NULL, &icnt, NULL, "rorv", "d,t,s", dreg, sreg,
7260 tempreg);
82dd0097
CD
7261 if (used_at)
7262 break;
7263 return;
7264 }
17a2f251
TS
7265 macro_build (NULL, &icnt, NULL, "subu", "d,v,t", AT, 0, treg);
7266 macro_build (NULL, &icnt, NULL, "srlv", "d,t,s", AT, sreg, AT);
7267 macro_build (NULL, &icnt, NULL, "sllv", "d,t,s", dreg, sreg, treg);
7268 macro_build (NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
7269 break;
7270
771c7ce4
TS
7271 case M_DROL_I:
7272 {
7273 unsigned int rot;
82dd0097 7274 char *l, *r;
771c7ce4
TS
7275
7276 if (imm_expr.X_op != O_constant)
82dd0097 7277 as_bad (_("Improper rotate count"));
771c7ce4 7278 rot = imm_expr.X_add_number & 0x3f;
fef14a42 7279 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
60b63b72
RS
7280 {
7281 rot = (64 - rot) & 0x3f;
7282 if (rot >= 32)
17a2f251
TS
7283 macro_build (NULL, &icnt, NULL, "dror32", "d,w,<",
7284 dreg, sreg, rot - 32);
60b63b72 7285 else
17a2f251
TS
7286 macro_build (NULL, &icnt, NULL, "dror", "d,w,<",
7287 dreg, sreg, rot);
82dd0097 7288 return;
60b63b72 7289 }
483fc7cd 7290 if (rot == 0)
483fc7cd 7291 {
17a2f251 7292 macro_build (NULL, &icnt, NULL, "dsrl", "d,w,<", dreg, sreg, 0);
82dd0097 7293 return;
483fc7cd 7294 }
82dd0097
CD
7295 l = (rot < 0x20) ? "dsll" : "dsll32";
7296 r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
7297 rot &= 0x1f;
17a2f251
TS
7298 macro_build (NULL, &icnt, NULL, l, "d,w,<", AT, sreg, rot);
7299 macro_build (NULL, &icnt, NULL, r, "d,w,<", dreg, sreg,
7300 (0x20 - rot) & 0x1f);
7301 macro_build (NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
7302 }
7303 break;
7304
252b5132 7305 case M_ROL_I:
771c7ce4
TS
7306 {
7307 unsigned int rot;
7308
7309 if (imm_expr.X_op != O_constant)
82dd0097 7310 as_bad (_("Improper rotate count"));
771c7ce4 7311 rot = imm_expr.X_add_number & 0x1f;
fef14a42 7312 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
60b63b72 7313 {
17a2f251
TS
7314 macro_build (NULL, &icnt, NULL, "ror", "d,w,<", dreg, sreg,
7315 (32 - rot) & 0x1f);
82dd0097 7316 return;
60b63b72 7317 }
483fc7cd 7318 if (rot == 0)
483fc7cd 7319 {
17a2f251 7320 macro_build (NULL, &icnt, NULL, "srl", "d,w,<", dreg, sreg, 0);
82dd0097 7321 return;
483fc7cd 7322 }
17a2f251
TS
7323 macro_build (NULL, &icnt, NULL, "sll", "d,w,<", AT, sreg, rot);
7324 macro_build (NULL, &icnt, NULL, "srl", "d,w,<", dreg, sreg,
7325 (0x20 - rot) & 0x1f);
7326 macro_build (NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
7327 }
7328 break;
7329
7330 case M_DROR:
fef14a42 7331 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097 7332 {
17a2f251 7333 macro_build (NULL, &icnt, NULL, "drorv", "d,t,s", dreg, sreg, treg);
82dd0097
CD
7334 return;
7335 }
bf151ce7 7336 macro_build (NULL, &icnt, NULL, "dsubu", "d,v,t", AT, 0, treg);
17a2f251
TS
7337 macro_build (NULL, &icnt, NULL, "dsllv", "d,t,s", AT, sreg, AT);
7338 macro_build (NULL, &icnt, NULL, "dsrlv", "d,t,s", dreg, sreg, treg);
7339 macro_build (NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
7340 break;
7341
7342 case M_ROR:
fef14a42 7343 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 7344 {
17a2f251 7345 macro_build (NULL, &icnt, NULL, "rorv", "d,t,s", dreg, sreg, treg);
82dd0097
CD
7346 return;
7347 }
17a2f251
TS
7348 macro_build (NULL, &icnt, NULL, "subu", "d,v,t", AT, 0, treg);
7349 macro_build (NULL, &icnt, NULL, "sllv", "d,t,s", AT, sreg, AT);
7350 macro_build (NULL, &icnt, NULL, "srlv", "d,t,s", dreg, sreg, treg);
7351 macro_build (NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
7352 break;
7353
771c7ce4
TS
7354 case M_DROR_I:
7355 {
7356 unsigned int rot;
82dd0097 7357 char *l, *r;
771c7ce4
TS
7358
7359 if (imm_expr.X_op != O_constant)
82dd0097 7360 as_bad (_("Improper rotate count"));
771c7ce4 7361 rot = imm_expr.X_add_number & 0x3f;
fef14a42 7362 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097
CD
7363 {
7364 if (rot >= 32)
17a2f251
TS
7365 macro_build (NULL, &icnt, NULL, "dror32", "d,w,<",
7366 dreg, sreg, rot - 32);
82dd0097 7367 else
17a2f251
TS
7368 macro_build (NULL, &icnt, NULL, "dror", "d,w,<",
7369 dreg, sreg, rot);
82dd0097
CD
7370 return;
7371 }
483fc7cd 7372 if (rot == 0)
483fc7cd 7373 {
17a2f251 7374 macro_build (NULL, &icnt, NULL, "dsrl", "d,w,<", dreg, sreg, 0);
82dd0097 7375 return;
483fc7cd 7376 }
82dd0097
CD
7377 r = (rot < 0x20) ? "dsrl" : "dsrl32";
7378 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
7379 rot &= 0x1f;
bf151ce7 7380 macro_build (NULL, &icnt, NULL, r, "d,w,<", AT, sreg, rot);
17a2f251
TS
7381 macro_build (NULL, &icnt, NULL, l, "d,w,<", dreg, sreg,
7382 (0x20 - rot) & 0x1f);
7383 macro_build (NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
7384 }
7385 break;
7386
252b5132 7387 case M_ROR_I:
771c7ce4
TS
7388 {
7389 unsigned int rot;
7390
7391 if (imm_expr.X_op != O_constant)
82dd0097 7392 as_bad (_("Improper rotate count"));
771c7ce4 7393 rot = imm_expr.X_add_number & 0x1f;
fef14a42 7394 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 7395 {
17a2f251 7396 macro_build (NULL, &icnt, NULL, "ror", "d,w,<", dreg, sreg, rot);
82dd0097
CD
7397 return;
7398 }
483fc7cd 7399 if (rot == 0)
483fc7cd 7400 {
17a2f251 7401 macro_build (NULL, &icnt, NULL, "srl", "d,w,<", dreg, sreg, 0);
82dd0097 7402 return;
483fc7cd 7403 }
17a2f251
TS
7404 macro_build (NULL, &icnt, NULL, "srl", "d,w,<", AT, sreg, rot);
7405 macro_build (NULL, &icnt, NULL, "sll", "d,w,<", dreg, sreg,
7406 (0x20 - rot) & 0x1f);
7407 macro_build (NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4 7408 }
252b5132
RH
7409 break;
7410
7411 case M_S_DOB:
fef14a42 7412 if (mips_opts.arch == CPU_R4650)
252b5132
RH
7413 {
7414 as_bad (_("opcode not supported on this processor"));
7415 return;
7416 }
e7af610e 7417 assert (mips_opts.isa == ISA_MIPS1);
252b5132
RH
7418 /* Even on a big endian machine $fn comes before $fn+1. We have
7419 to adjust when storing to memory. */
17a2f251 7420 macro_build (NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
252b5132 7421 target_big_endian ? treg + 1 : treg,
17a2f251 7422 BFD_RELOC_LO16, breg);
252b5132 7423 offset_expr.X_add_number += 4;
17a2f251 7424 macro_build (NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
252b5132 7425 target_big_endian ? treg : treg + 1,
17a2f251 7426 BFD_RELOC_LO16, breg);
252b5132
RH
7427 return;
7428
7429 case M_SEQ:
7430 if (sreg == 0)
17a2f251
TS
7431 macro_build (NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, treg,
7432 BFD_RELOC_LO16);
252b5132 7433 else if (treg == 0)
17a2f251
TS
7434 macro_build (NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, sreg,
7435 BFD_RELOC_LO16);
252b5132
RH
7436 else
7437 {
17a2f251
TS
7438 macro_build (NULL, &icnt, NULL, "xor", "d,v,t", dreg, sreg, treg);
7439 macro_build (NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
7440 BFD_RELOC_LO16);
252b5132
RH
7441 }
7442 return;
7443
7444 case M_SEQ_I:
7445 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7446 {
17a2f251
TS
7447 macro_build (NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, sreg,
7448 BFD_RELOC_LO16);
252b5132
RH
7449 return;
7450 }
7451 if (sreg == 0)
7452 {
7453 as_warn (_("Instruction %s: result is always false"),
7454 ip->insn_mo->name);
ea1fb5dc 7455 move_register (&icnt, dreg, 0);
252b5132
RH
7456 return;
7457 }
7458 if (imm_expr.X_op == O_constant
7459 && imm_expr.X_add_number >= 0
7460 && imm_expr.X_add_number < 0x10000)
7461 {
17a2f251
TS
7462 macro_build (NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg, sreg,
7463 BFD_RELOC_LO16);
252b5132
RH
7464 used_at = 0;
7465 }
7466 else if (imm_expr.X_op == O_constant
7467 && imm_expr.X_add_number > -0x8000
7468 && imm_expr.X_add_number < 0)
7469 {
7470 imm_expr.X_add_number = -imm_expr.X_add_number;
17a2f251 7471 macro_build (NULL, &icnt, &imm_expr,
ca4e0257 7472 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
17a2f251 7473 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7474 used_at = 0;
7475 }
7476 else
7477 {
4d34fb5f 7478 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
17a2f251 7479 macro_build (NULL, &icnt, NULL, "xor", "d,v,t", dreg, sreg, AT);
252b5132
RH
7480 used_at = 1;
7481 }
17a2f251
TS
7482 macro_build (NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
7483 BFD_RELOC_LO16);
252b5132
RH
7484 if (used_at)
7485 break;
7486 return;
7487
7488 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
7489 s = "slt";
7490 goto sge;
7491 case M_SGEU:
7492 s = "sltu";
7493 sge:
17a2f251
TS
7494 macro_build (NULL, &icnt, NULL, s, "d,v,t", dreg, sreg, treg);
7495 macro_build (NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7496 BFD_RELOC_LO16);
252b5132
RH
7497 return;
7498
7499 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
7500 case M_SGEU_I:
7501 if (imm_expr.X_op == O_constant
7502 && imm_expr.X_add_number >= -0x8000
7503 && imm_expr.X_add_number < 0x8000)
7504 {
17a2f251 7505 macro_build (NULL, &icnt, &imm_expr,
252b5132 7506 mask == M_SGE_I ? "slti" : "sltiu",
17a2f251 7507 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7508 used_at = 0;
7509 }
7510 else
7511 {
4d34fb5f 7512 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
17a2f251
TS
7513 macro_build (NULL, &icnt, NULL, mask == M_SGE_I ? "slt" : "sltu",
7514 "d,v,t", dreg, sreg, AT);
252b5132
RH
7515 used_at = 1;
7516 }
17a2f251
TS
7517 macro_build (NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7518 BFD_RELOC_LO16);
252b5132
RH
7519 if (used_at)
7520 break;
7521 return;
7522
7523 case M_SGT: /* sreg > treg <==> treg < sreg */
7524 s = "slt";
7525 goto sgt;
7526 case M_SGTU:
7527 s = "sltu";
7528 sgt:
17a2f251 7529 macro_build (NULL, &icnt, NULL, s, "d,v,t", dreg, treg, sreg);
252b5132
RH
7530 return;
7531
7532 case M_SGT_I: /* sreg > I <==> I < sreg */
7533 s = "slt";
7534 goto sgti;
7535 case M_SGTU_I:
7536 s = "sltu";
7537 sgti:
4d34fb5f 7538 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
17a2f251 7539 macro_build (NULL, &icnt, NULL, s, "d,v,t", dreg, AT, sreg);
252b5132
RH
7540 break;
7541
2396cfb9 7542 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
252b5132
RH
7543 s = "slt";
7544 goto sle;
7545 case M_SLEU:
7546 s = "sltu";
7547 sle:
17a2f251
TS
7548 macro_build (NULL, &icnt, NULL, s, "d,v,t", dreg, treg, sreg);
7549 macro_build (NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7550 BFD_RELOC_LO16);
252b5132
RH
7551 return;
7552
2396cfb9 7553 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
252b5132
RH
7554 s = "slt";
7555 goto slei;
7556 case M_SLEU_I:
7557 s = "sltu";
7558 slei:
4d34fb5f 7559 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
17a2f251
TS
7560 macro_build (NULL, &icnt, NULL, s, "d,v,t", dreg, AT, sreg);
7561 macro_build (NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7562 BFD_RELOC_LO16);
252b5132
RH
7563 break;
7564
7565 case M_SLT_I:
7566 if (imm_expr.X_op == O_constant
7567 && imm_expr.X_add_number >= -0x8000
7568 && imm_expr.X_add_number < 0x8000)
7569 {
17a2f251
TS
7570 macro_build (NULL, &icnt, &imm_expr, "slti", "t,r,j", dreg, sreg,
7571 BFD_RELOC_LO16);
252b5132
RH
7572 return;
7573 }
4d34fb5f 7574 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
17a2f251 7575 macro_build (NULL, &icnt, NULL, "slt", "d,v,t", dreg, sreg, AT);
252b5132
RH
7576 break;
7577
7578 case M_SLTU_I:
7579 if (imm_expr.X_op == O_constant
7580 && imm_expr.X_add_number >= -0x8000
7581 && imm_expr.X_add_number < 0x8000)
7582 {
17a2f251
TS
7583 macro_build (NULL, &icnt, &imm_expr, "sltiu", "t,r,j", dreg, sreg,
7584 BFD_RELOC_LO16);
252b5132
RH
7585 return;
7586 }
4d34fb5f 7587 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
17a2f251 7588 macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", dreg, sreg, AT);
252b5132
RH
7589 break;
7590
7591 case M_SNE:
7592 if (sreg == 0)
bf151ce7 7593 macro_build (NULL, &icnt, NULL, "sltu","d,v,t", dreg, 0, treg);
252b5132 7594 else if (treg == 0)
17a2f251 7595 macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0, sreg);
252b5132
RH
7596 else
7597 {
17a2f251
TS
7598 macro_build (NULL, &icnt, NULL, "xor", "d,v,t", dreg, sreg, treg);
7599 macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0, dreg);
252b5132
RH
7600 }
7601 return;
7602
7603 case M_SNE_I:
7604 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7605 {
17a2f251 7606 macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0, sreg);
252b5132
RH
7607 return;
7608 }
7609 if (sreg == 0)
7610 {
7611 as_warn (_("Instruction %s: result is always true"),
7612 ip->insn_mo->name);
17a2f251 7613 macro_build (NULL, &icnt, &expr1,
ca4e0257 7614 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
17a2f251 7615 "t,r,j", dreg, 0, BFD_RELOC_LO16);
252b5132
RH
7616 return;
7617 }
7618 if (imm_expr.X_op == O_constant
7619 && imm_expr.X_add_number >= 0
7620 && imm_expr.X_add_number < 0x10000)
7621 {
17a2f251
TS
7622 macro_build (NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg, sreg,
7623 BFD_RELOC_LO16);
252b5132
RH
7624 used_at = 0;
7625 }
7626 else if (imm_expr.X_op == O_constant
7627 && imm_expr.X_add_number > -0x8000
7628 && imm_expr.X_add_number < 0)
7629 {
7630 imm_expr.X_add_number = -imm_expr.X_add_number;
17a2f251 7631 macro_build (NULL, &icnt, &imm_expr,
ca4e0257 7632 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
17a2f251 7633 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7634 used_at = 0;
7635 }
7636 else
7637 {
4d34fb5f 7638 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
17a2f251 7639 macro_build (NULL, &icnt, NULL, "xor", "d,v,t", dreg, sreg, AT);
252b5132
RH
7640 used_at = 1;
7641 }
17a2f251 7642 macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0, dreg);
252b5132
RH
7643 if (used_at)
7644 break;
7645 return;
7646
7647 case M_DSUB_I:
7648 dbl = 1;
7649 case M_SUB_I:
7650 if (imm_expr.X_op == O_constant
7651 && imm_expr.X_add_number > -0x8000
7652 && imm_expr.X_add_number <= 0x8000)
7653 {
7654 imm_expr.X_add_number = -imm_expr.X_add_number;
17a2f251
TS
7655 macro_build (NULL, &icnt, &imm_expr, dbl ? "daddi" : "addi",
7656 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7657 return;
7658 }
7659 load_register (&icnt, AT, &imm_expr, dbl);
17a2f251
TS
7660 macro_build (NULL, &icnt, NULL, dbl ? "dsub" : "sub", "d,v,t",
7661 dreg, sreg, AT);
252b5132
RH
7662 break;
7663
7664 case M_DSUBU_I:
7665 dbl = 1;
7666 case M_SUBU_I:
7667 if (imm_expr.X_op == O_constant
7668 && imm_expr.X_add_number > -0x8000
7669 && imm_expr.X_add_number <= 0x8000)
7670 {
7671 imm_expr.X_add_number = -imm_expr.X_add_number;
17a2f251
TS
7672 macro_build (NULL, &icnt, &imm_expr, dbl ? "daddiu" : "addiu",
7673 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7674 return;
7675 }
7676 load_register (&icnt, AT, &imm_expr, dbl);
17a2f251
TS
7677 macro_build (NULL, &icnt, NULL, dbl ? "dsubu" : "subu", "d,v,t",
7678 dreg, sreg, AT);
252b5132
RH
7679 break;
7680
7681 case M_TEQ_I:
7682 s = "teq";
7683 goto trap;
7684 case M_TGE_I:
7685 s = "tge";
7686 goto trap;
7687 case M_TGEU_I:
7688 s = "tgeu";
7689 goto trap;
7690 case M_TLT_I:
7691 s = "tlt";
7692 goto trap;
7693 case M_TLTU_I:
7694 s = "tltu";
7695 goto trap;
7696 case M_TNE_I:
7697 s = "tne";
7698 trap:
4d34fb5f 7699 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
17a2f251 7700 macro_build (NULL, &icnt, NULL, s, "s,t", sreg, AT);
252b5132
RH
7701 break;
7702
252b5132 7703 case M_TRUNCWS:
43841e91 7704 case M_TRUNCWD:
e7af610e 7705 assert (mips_opts.isa == ISA_MIPS1);
252b5132
RH
7706 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
7707 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
7708
7709 /*
7710 * Is the double cfc1 instruction a bug in the mips assembler;
7711 * or is there a reason for it?
7712 */
b34976b6 7713 mips_emit_delays (TRUE);
252b5132
RH
7714 ++mips_opts.noreorder;
7715 mips_any_noreorder = 1;
17a2f251
TS
7716 macro_build (NULL, &icnt, NULL, "cfc1", "t,G", treg, RA);
7717 macro_build (NULL, &icnt, NULL, "cfc1", "t,G", treg, RA);
7718 macro_build (NULL, &icnt, NULL, "nop", "");
252b5132 7719 expr1.X_add_number = 3;
17a2f251
TS
7720 macro_build (NULL, &icnt, &expr1, "ori", "t,r,i", AT, treg,
7721 BFD_RELOC_LO16);
252b5132 7722 expr1.X_add_number = 2;
17a2f251
TS
7723 macro_build (NULL, &icnt, &expr1, "xori", "t,r,i", AT, AT,
7724 BFD_RELOC_LO16);
7725 macro_build (NULL, &icnt, NULL, "ctc1", "t,G", AT, RA);
7726 macro_build (NULL, &icnt, NULL, "nop", "");
7727 macro_build (NULL, &icnt, NULL,
7728 mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s",
7729 "D,S", dreg, sreg);
7730 macro_build (NULL, &icnt, NULL, "ctc1", "t,G", treg, RA);
7731 macro_build (NULL, &icnt, NULL, "nop", "");
252b5132
RH
7732 --mips_opts.noreorder;
7733 break;
7734
7735 case M_ULH:
7736 s = "lb";
7737 goto ulh;
7738 case M_ULHU:
7739 s = "lbu";
7740 ulh:
7741 if (offset_expr.X_add_number >= 0x7fff)
7742 as_bad (_("operand overflow"));
252b5132 7743 if (! target_big_endian)
f9419b05 7744 ++offset_expr.X_add_number;
17a2f251
TS
7745 macro_build (NULL, &icnt, &offset_expr, s, "t,o(b)", AT,
7746 BFD_RELOC_LO16, breg);
252b5132 7747 if (! target_big_endian)
f9419b05 7748 --offset_expr.X_add_number;
252b5132 7749 else
f9419b05 7750 ++offset_expr.X_add_number;
17a2f251
TS
7751 macro_build (NULL, &icnt, &offset_expr, "lbu", "t,o(b)", treg,
7752 BFD_RELOC_LO16, breg);
7753 macro_build (NULL, &icnt, NULL, "sll", "d,w,<", AT, AT, 8);
7754 macro_build (NULL, &icnt, NULL, "or", "d,v,t", treg, treg, AT);
252b5132
RH
7755 break;
7756
7757 case M_ULD:
7758 s = "ldl";
7759 s2 = "ldr";
7760 off = 7;
7761 goto ulw;
7762 case M_ULW:
7763 s = "lwl";
7764 s2 = "lwr";
7765 off = 3;
7766 ulw:
7767 if (offset_expr.X_add_number >= 0x8000 - off)
7768 as_bad (_("operand overflow"));
af22f5b2
CD
7769 if (treg != breg)
7770 tempreg = treg;
7771 else
7772 tempreg = AT;
252b5132
RH
7773 if (! target_big_endian)
7774 offset_expr.X_add_number += off;
17a2f251
TS
7775 macro_build (NULL, &icnt, &offset_expr, s, "t,o(b)", tempreg,
7776 BFD_RELOC_LO16, breg);
252b5132
RH
7777 if (! target_big_endian)
7778 offset_expr.X_add_number -= off;
7779 else
7780 offset_expr.X_add_number += off;
17a2f251
TS
7781 macro_build (NULL, &icnt, &offset_expr, s2, "t,o(b)", tempreg,
7782 BFD_RELOC_LO16, breg);
af22f5b2
CD
7783
7784 /* If necessary, move the result in tempreg the final destination. */
7785 if (treg == tempreg)
7786 return;
7787 /* Protect second load's delay slot. */
7788 if (!gpr_interlocks)
17a2f251 7789 macro_build (NULL, &icnt, NULL, "nop", "");
af22f5b2
CD
7790 move_register (&icnt, treg, tempreg);
7791 break;
252b5132
RH
7792
7793 case M_ULD_A:
7794 s = "ldl";
7795 s2 = "ldr";
7796 off = 7;
7797 goto ulwa;
7798 case M_ULW_A:
7799 s = "lwl";
7800 s2 = "lwr";
7801 off = 3;
7802 ulwa:
d6bc6245 7803 used_at = 1;
c9914766 7804 load_address (&icnt, AT, &offset_expr, &used_at);
252b5132 7805 if (breg != 0)
17a2f251
TS
7806 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
7807 AT, AT, breg);
252b5132
RH
7808 if (! target_big_endian)
7809 expr1.X_add_number = off;
7810 else
7811 expr1.X_add_number = 0;
17a2f251
TS
7812 macro_build (NULL, &icnt, &expr1, s, "t,o(b)", treg,
7813 BFD_RELOC_LO16, AT);
252b5132
RH
7814 if (! target_big_endian)
7815 expr1.X_add_number = 0;
7816 else
7817 expr1.X_add_number = off;
17a2f251
TS
7818 macro_build (NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7819 BFD_RELOC_LO16, AT);
252b5132
RH
7820 break;
7821
7822 case M_ULH_A:
7823 case M_ULHU_A:
d6bc6245 7824 used_at = 1;
c9914766 7825 load_address (&icnt, AT, &offset_expr, &used_at);
252b5132 7826 if (breg != 0)
17a2f251
TS
7827 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
7828 AT, AT, breg);
252b5132
RH
7829 if (target_big_endian)
7830 expr1.X_add_number = 0;
17a2f251
TS
7831 macro_build (NULL, &icnt, &expr1,
7832 mask == M_ULH_A ? "lb" : "lbu", "t,o(b)",
7833 treg, BFD_RELOC_LO16, AT);
252b5132
RH
7834 if (target_big_endian)
7835 expr1.X_add_number = 1;
7836 else
7837 expr1.X_add_number = 0;
17a2f251
TS
7838 macro_build (NULL, &icnt, &expr1, "lbu", "t,o(b)",
7839 AT, BFD_RELOC_LO16, AT);
7840 macro_build (NULL, &icnt, NULL, "sll", "d,w,<", treg, treg, 8);
7841 macro_build (NULL, &icnt, NULL, "or", "d,v,t", treg, treg, AT);
252b5132
RH
7842 break;
7843
7844 case M_USH:
7845 if (offset_expr.X_add_number >= 0x7fff)
7846 as_bad (_("operand overflow"));
7847 if (target_big_endian)
f9419b05 7848 ++offset_expr.X_add_number;
17a2f251
TS
7849 macro_build (NULL, &icnt, &offset_expr, "sb", "t,o(b)", treg,
7850 BFD_RELOC_LO16, breg);
7851 macro_build (NULL, &icnt, NULL, "srl", "d,w,<", AT, treg, 8);
252b5132 7852 if (target_big_endian)
f9419b05 7853 --offset_expr.X_add_number;
252b5132 7854 else
f9419b05 7855 ++offset_expr.X_add_number;
17a2f251
TS
7856 macro_build (NULL, &icnt, &offset_expr, "sb", "t,o(b)", AT,
7857 BFD_RELOC_LO16, breg);
252b5132
RH
7858 break;
7859
7860 case M_USD:
7861 s = "sdl";
7862 s2 = "sdr";
7863 off = 7;
7864 goto usw;
7865 case M_USW:
7866 s = "swl";
7867 s2 = "swr";
7868 off = 3;
7869 usw:
7870 if (offset_expr.X_add_number >= 0x8000 - off)
7871 as_bad (_("operand overflow"));
7872 if (! target_big_endian)
7873 offset_expr.X_add_number += off;
17a2f251
TS
7874 macro_build (NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7875 BFD_RELOC_LO16, breg);
252b5132
RH
7876 if (! target_big_endian)
7877 offset_expr.X_add_number -= off;
7878 else
7879 offset_expr.X_add_number += off;
17a2f251
TS
7880 macro_build (NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7881 BFD_RELOC_LO16, breg);
252b5132
RH
7882 return;
7883
7884 case M_USD_A:
7885 s = "sdl";
7886 s2 = "sdr";
7887 off = 7;
7888 goto uswa;
7889 case M_USW_A:
7890 s = "swl";
7891 s2 = "swr";
7892 off = 3;
7893 uswa:
d6bc6245 7894 used_at = 1;
c9914766 7895 load_address (&icnt, AT, &offset_expr, &used_at);
252b5132 7896 if (breg != 0)
17a2f251
TS
7897 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
7898 AT, AT, breg);
252b5132
RH
7899 if (! target_big_endian)
7900 expr1.X_add_number = off;
7901 else
7902 expr1.X_add_number = 0;
17a2f251
TS
7903 macro_build (NULL, &icnt, &expr1, s, "t,o(b)", treg,
7904 BFD_RELOC_LO16, AT);
252b5132
RH
7905 if (! target_big_endian)
7906 expr1.X_add_number = 0;
7907 else
7908 expr1.X_add_number = off;
17a2f251
TS
7909 macro_build (NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7910 BFD_RELOC_LO16, AT);
252b5132
RH
7911 break;
7912
7913 case M_USH_A:
d6bc6245 7914 used_at = 1;
c9914766 7915 load_address (&icnt, AT, &offset_expr, &used_at);
252b5132 7916 if (breg != 0)
17a2f251
TS
7917 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
7918 AT, AT, breg);
252b5132
RH
7919 if (! target_big_endian)
7920 expr1.X_add_number = 0;
17a2f251
TS
7921 macro_build (NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7922 BFD_RELOC_LO16, AT);
7923 macro_build (NULL, &icnt, NULL, "srl", "d,w,<", treg, treg, 8);
252b5132
RH
7924 if (! target_big_endian)
7925 expr1.X_add_number = 1;
7926 else
7927 expr1.X_add_number = 0;
17a2f251
TS
7928 macro_build (NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7929 BFD_RELOC_LO16, AT);
252b5132
RH
7930 if (! target_big_endian)
7931 expr1.X_add_number = 0;
7932 else
7933 expr1.X_add_number = 1;
17a2f251
TS
7934 macro_build (NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7935 BFD_RELOC_LO16, AT);
7936 macro_build (NULL, &icnt, NULL, "sll", "d,w,<", treg, treg, 8);
7937 macro_build (NULL, &icnt, NULL, "or", "d,v,t", treg, treg, AT);
252b5132
RH
7938 break;
7939
7940 default:
7941 /* FIXME: Check if this is one of the itbl macros, since they
bdaaa2e1 7942 are added dynamically. */
252b5132
RH
7943 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7944 break;
7945 }
7946 if (mips_opts.noat)
7947 as_warn (_("Macro used $at after \".set noat\""));
7948}
7949
7950/* Implement macros in mips16 mode. */
7951
7952static void
17a2f251 7953mips16_macro (struct mips_cl_insn *ip)
252b5132
RH
7954{
7955 int mask;
7956 int xreg, yreg, zreg, tmp;
7957 int icnt;
7958 expressionS expr1;
7959 int dbl;
7960 const char *s, *s2, *s3;
7961
7962 mask = ip->insn_mo->mask;
7963
7964 xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
7965 yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
7966 zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
7967
7968 icnt = 0;
7969
7970 expr1.X_op = O_constant;
7971 expr1.X_op_symbol = NULL;
7972 expr1.X_add_symbol = NULL;
7973 expr1.X_add_number = 1;
7974
7975 dbl = 0;
7976
7977 switch (mask)
7978 {
7979 default:
7980 internalError ();
7981
7982 case M_DDIV_3:
7983 dbl = 1;
7984 case M_DIV_3:
7985 s = "mflo";
7986 goto do_div3;
7987 case M_DREM_3:
7988 dbl = 1;
7989 case M_REM_3:
7990 s = "mfhi";
7991 do_div3:
b34976b6 7992 mips_emit_delays (TRUE);
252b5132
RH
7993 ++mips_opts.noreorder;
7994 mips_any_noreorder = 1;
17a2f251
TS
7995 macro_build (NULL, &icnt, NULL, dbl ? "ddiv" : "div", "0,x,y",
7996 xreg, yreg);
252b5132 7997 expr1.X_add_number = 2;
17a2f251
TS
7998 macro_build (NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7999 macro_build (NULL, &icnt, NULL, "break", "6", 7);
bdaaa2e1 8000
252b5132
RH
8001 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
8002 since that causes an overflow. We should do that as well,
8003 but I don't see how to do the comparisons without a temporary
8004 register. */
8005 --mips_opts.noreorder;
17a2f251 8006 macro_build (NULL, &icnt, NULL, s, "x", zreg);
252b5132
RH
8007 break;
8008
8009 case M_DIVU_3:
8010 s = "divu";
8011 s2 = "mflo";
8012 goto do_divu3;
8013 case M_REMU_3:
8014 s = "divu";
8015 s2 = "mfhi";
8016 goto do_divu3;
8017 case M_DDIVU_3:
8018 s = "ddivu";
8019 s2 = "mflo";
8020 goto do_divu3;
8021 case M_DREMU_3:
8022 s = "ddivu";
8023 s2 = "mfhi";
8024 do_divu3:
b34976b6 8025 mips_emit_delays (TRUE);
252b5132
RH
8026 ++mips_opts.noreorder;
8027 mips_any_noreorder = 1;
17a2f251 8028 macro_build (NULL, &icnt, NULL, s, "0,x,y", xreg, yreg);
252b5132 8029 expr1.X_add_number = 2;
17a2f251
TS
8030 macro_build (NULL, &icnt, &expr1, "bnez", "x,p", yreg);
8031 macro_build (NULL, &icnt, NULL, "break", "6", 7);
252b5132 8032 --mips_opts.noreorder;
17a2f251 8033 macro_build (NULL, &icnt, NULL, s2, "x", zreg);
252b5132
RH
8034 break;
8035
8036 case M_DMUL:
8037 dbl = 1;
8038 case M_MUL:
17a2f251
TS
8039 macro_build (NULL, &icnt, NULL, dbl ? "dmultu" : "multu", "x,y",
8040 xreg, yreg);
8041 macro_build (NULL, &icnt, NULL, "mflo", "x", zreg);
252b5132
RH
8042 return;
8043
8044 case M_DSUBU_I:
8045 dbl = 1;
8046 goto do_subu;
8047 case M_SUBU_I:
8048 do_subu:
8049 if (imm_expr.X_op != O_constant)
8050 as_bad (_("Unsupported large constant"));
8051 imm_expr.X_add_number = -imm_expr.X_add_number;
17a2f251
TS
8052 macro_build (NULL, &icnt, &imm_expr, dbl ? "daddiu" : "addiu", "y,x,4",
8053 yreg, xreg);
252b5132
RH
8054 break;
8055
8056 case M_SUBU_I_2:
8057 if (imm_expr.X_op != O_constant)
8058 as_bad (_("Unsupported large constant"));
8059 imm_expr.X_add_number = -imm_expr.X_add_number;
17a2f251 8060 macro_build (NULL, &icnt, &imm_expr, "addiu", "x,k", xreg);
252b5132
RH
8061 break;
8062
8063 case M_DSUBU_I_2:
8064 if (imm_expr.X_op != O_constant)
8065 as_bad (_("Unsupported large constant"));
8066 imm_expr.X_add_number = -imm_expr.X_add_number;
17a2f251 8067 macro_build (NULL, &icnt, &imm_expr, "daddiu", "y,j", yreg);
252b5132
RH
8068 break;
8069
8070 case M_BEQ:
8071 s = "cmp";
8072 s2 = "bteqz";
8073 goto do_branch;
8074 case M_BNE:
8075 s = "cmp";
8076 s2 = "btnez";
8077 goto do_branch;
8078 case M_BLT:
8079 s = "slt";
8080 s2 = "btnez";
8081 goto do_branch;
8082 case M_BLTU:
8083 s = "sltu";
8084 s2 = "btnez";
8085 goto do_branch;
8086 case M_BLE:
8087 s = "slt";
8088 s2 = "bteqz";
8089 goto do_reverse_branch;
8090 case M_BLEU:
8091 s = "sltu";
8092 s2 = "bteqz";
8093 goto do_reverse_branch;
8094 case M_BGE:
8095 s = "slt";
8096 s2 = "bteqz";
8097 goto do_branch;
8098 case M_BGEU:
8099 s = "sltu";
8100 s2 = "bteqz";
8101 goto do_branch;
8102 case M_BGT:
8103 s = "slt";
8104 s2 = "btnez";
8105 goto do_reverse_branch;
8106 case M_BGTU:
8107 s = "sltu";
8108 s2 = "btnez";
8109
8110 do_reverse_branch:
8111 tmp = xreg;
8112 xreg = yreg;
8113 yreg = tmp;
8114
8115 do_branch:
17a2f251
TS
8116 macro_build (NULL, &icnt, NULL, s, "x,y", xreg, yreg);
8117 macro_build (NULL, &icnt, &offset_expr, s2, "p");
252b5132
RH
8118 break;
8119
8120 case M_BEQ_I:
8121 s = "cmpi";
8122 s2 = "bteqz";
8123 s3 = "x,U";
8124 goto do_branch_i;
8125 case M_BNE_I:
8126 s = "cmpi";
8127 s2 = "btnez";
8128 s3 = "x,U";
8129 goto do_branch_i;
8130 case M_BLT_I:
8131 s = "slti";
8132 s2 = "btnez";
8133 s3 = "x,8";
8134 goto do_branch_i;
8135 case M_BLTU_I:
8136 s = "sltiu";
8137 s2 = "btnez";
8138 s3 = "x,8";
8139 goto do_branch_i;
8140 case M_BLE_I:
8141 s = "slti";
8142 s2 = "btnez";
8143 s3 = "x,8";
8144 goto do_addone_branch_i;
8145 case M_BLEU_I:
8146 s = "sltiu";
8147 s2 = "btnez";
8148 s3 = "x,8";
8149 goto do_addone_branch_i;
8150 case M_BGE_I:
8151 s = "slti";
8152 s2 = "bteqz";
8153 s3 = "x,8";
8154 goto do_branch_i;
8155 case M_BGEU_I:
8156 s = "sltiu";
8157 s2 = "bteqz";
8158 s3 = "x,8";
8159 goto do_branch_i;
8160 case M_BGT_I:
8161 s = "slti";
8162 s2 = "bteqz";
8163 s3 = "x,8";
8164 goto do_addone_branch_i;
8165 case M_BGTU_I:
8166 s = "sltiu";
8167 s2 = "bteqz";
8168 s3 = "x,8";
8169
8170 do_addone_branch_i:
8171 if (imm_expr.X_op != O_constant)
8172 as_bad (_("Unsupported large constant"));
8173 ++imm_expr.X_add_number;
8174
8175 do_branch_i:
17a2f251
TS
8176 macro_build (NULL, &icnt, &imm_expr, s, s3, xreg);
8177 macro_build (NULL, &icnt, &offset_expr, s2, "p");
252b5132
RH
8178 break;
8179
8180 case M_ABS:
8181 expr1.X_add_number = 0;
17a2f251 8182 macro_build (NULL, &icnt, &expr1, "slti", "x,8", yreg);
252b5132 8183 if (xreg != yreg)
ea1fb5dc 8184 move_register (&icnt, xreg, yreg);
252b5132 8185 expr1.X_add_number = 2;
17a2f251
TS
8186 macro_build (NULL, &icnt, &expr1, "bteqz", "p");
8187 macro_build (NULL, &icnt, NULL, "neg", "x,w", xreg, xreg);
252b5132
RH
8188 }
8189}
8190
8191/* For consistency checking, verify that all bits are specified either
8192 by the match/mask part of the instruction definition, or by the
8193 operand list. */
8194static int
17a2f251 8195validate_mips_insn (const struct mips_opcode *opc)
252b5132
RH
8196{
8197 const char *p = opc->args;
8198 char c;
8199 unsigned long used_bits = opc->mask;
8200
8201 if ((used_bits & opc->match) != opc->match)
8202 {
8203 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
8204 opc->name, opc->args);
8205 return 0;
8206 }
8207#define USE_BITS(mask,shift) (used_bits |= ((mask) << (shift)))
8208 while (*p)
8209 switch (c = *p++)
8210 {
8211 case ',': break;
8212 case '(': break;
8213 case ')': break;
af7ee8bf
CD
8214 case '+':
8215 switch (c = *p++)
8216 {
8217 case 'A': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
8218 case 'B': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
8219 case 'C': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
bbcc0807
CD
8220 case 'D': USE_BITS (OP_MASK_RD, OP_SH_RD);
8221 USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
5f74bc13
CD
8222 case 'E': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
8223 case 'F': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
8224 case 'G': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
8225 case 'H': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
8226 case 'I': break;
af7ee8bf
CD
8227 default:
8228 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8229 c, opc->name, opc->args);
8230 return 0;
8231 }
8232 break;
252b5132
RH
8233 case '<': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
8234 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
8235 case 'A': break;
4372b673 8236 case 'B': USE_BITS (OP_MASK_CODE20, OP_SH_CODE20); break;
252b5132
RH
8237 case 'C': USE_BITS (OP_MASK_COPZ, OP_SH_COPZ); break;
8238 case 'D': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
8239 case 'E': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8240 case 'F': break;
8241 case 'G': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
156c2f8b 8242 case 'H': USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
252b5132 8243 case 'I': break;
e972090a 8244 case 'J': USE_BITS (OP_MASK_CODE19, OP_SH_CODE19); break;
af7ee8bf 8245 case 'K': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
252b5132
RH
8246 case 'L': break;
8247 case 'M': USE_BITS (OP_MASK_CCC, OP_SH_CCC); break;
8248 case 'N': USE_BITS (OP_MASK_BCC, OP_SH_BCC); break;
deec1734
CD
8249 case 'O': USE_BITS (OP_MASK_ALN, OP_SH_ALN); break;
8250 case 'Q': USE_BITS (OP_MASK_VSEL, OP_SH_VSEL);
8251 USE_BITS (OP_MASK_FT, OP_SH_FT); break;
252b5132
RH
8252 case 'R': USE_BITS (OP_MASK_FR, OP_SH_FR); break;
8253 case 'S': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
8254 case 'T': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
8255 case 'V': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
8256 case 'W': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
deec1734
CD
8257 case 'X': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
8258 case 'Y': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
8259 case 'Z': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
252b5132
RH
8260 case 'a': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break;
8261 case 'b': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8262 case 'c': USE_BITS (OP_MASK_CODE, OP_SH_CODE); break;
8263 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
8264 case 'f': break;
8265 case 'h': USE_BITS (OP_MASK_PREFX, OP_SH_PREFX); break;
8266 case 'i': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
8267 case 'j': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
8268 case 'k': USE_BITS (OP_MASK_CACHE, OP_SH_CACHE); break;
8269 case 'l': break;
8270 case 'o': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
8271 case 'p': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
8272 case 'q': USE_BITS (OP_MASK_CODE2, OP_SH_CODE2); break;
8273 case 'r': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8274 case 's': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8275 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8276 case 'u': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
8277 case 'v': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8278 case 'w': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8279 case 'x': break;
8280 case 'z': break;
8281 case 'P': USE_BITS (OP_MASK_PERFREG, OP_SH_PERFREG); break;
4372b673
NC
8282 case 'U': USE_BITS (OP_MASK_RD, OP_SH_RD);
8283 USE_BITS (OP_MASK_RT, OP_SH_RT); break;
60b63b72
RS
8284 case 'e': USE_BITS (OP_MASK_VECBYTE, OP_SH_VECBYTE); break;
8285 case '%': USE_BITS (OP_MASK_VECALIGN, OP_SH_VECALIGN); break;
8286 case '[': break;
8287 case ']': break;
252b5132
RH
8288 default:
8289 as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
8290 c, opc->name, opc->args);
8291 return 0;
8292 }
8293#undef USE_BITS
8294 if (used_bits != 0xffffffff)
8295 {
8296 as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
8297 ~used_bits & 0xffffffff, opc->name, opc->args);
8298 return 0;
8299 }
8300 return 1;
8301}
8302
8303/* This routine assembles an instruction into its binary format. As a
8304 side effect, it sets one of the global variables imm_reloc or
8305 offset_reloc to the type of relocation to do if one of the operands
8306 is an address expression. */
8307
8308static void
17a2f251 8309mips_ip (char *str, struct mips_cl_insn *ip)
252b5132
RH
8310{
8311 char *s;
8312 const char *args;
43841e91 8313 char c = 0;
252b5132
RH
8314 struct mips_opcode *insn;
8315 char *argsStart;
8316 unsigned int regno;
8317 unsigned int lastregno = 0;
af7ee8bf 8318 unsigned int lastpos = 0;
071742cf 8319 unsigned int limlo, limhi;
252b5132
RH
8320 char *s_reset;
8321 char save_c = 0;
252b5132
RH
8322
8323 insn_error = NULL;
8324
8325 /* If the instruction contains a '.', we first try to match an instruction
8326 including the '.'. Then we try again without the '.'. */
8327 insn = NULL;
3882b010 8328 for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
252b5132
RH
8329 continue;
8330
8331 /* If we stopped on whitespace, then replace the whitespace with null for
8332 the call to hash_find. Save the character we replaced just in case we
8333 have to re-parse the instruction. */
3882b010 8334 if (ISSPACE (*s))
252b5132
RH
8335 {
8336 save_c = *s;
8337 *s++ = '\0';
8338 }
bdaaa2e1 8339
252b5132
RH
8340 insn = (struct mips_opcode *) hash_find (op_hash, str);
8341
8342 /* If we didn't find the instruction in the opcode table, try again, but
8343 this time with just the instruction up to, but not including the
8344 first '.'. */
8345 if (insn == NULL)
8346 {
bdaaa2e1 8347 /* Restore the character we overwrite above (if any). */
252b5132
RH
8348 if (save_c)
8349 *(--s) = save_c;
8350
8351 /* Scan up to the first '.' or whitespace. */
3882b010
L
8352 for (s = str;
8353 *s != '\0' && *s != '.' && !ISSPACE (*s);
8354 ++s)
252b5132
RH
8355 continue;
8356
8357 /* If we did not find a '.', then we can quit now. */
8358 if (*s != '.')
8359 {
8360 insn_error = "unrecognized opcode";
8361 return;
8362 }
8363
8364 /* Lookup the instruction in the hash table. */
8365 *s++ = '\0';
8366 if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
8367 {
8368 insn_error = "unrecognized opcode";
8369 return;
8370 }
252b5132
RH
8371 }
8372
8373 argsStart = s;
8374 for (;;)
8375 {
b34976b6 8376 bfd_boolean ok;
252b5132
RH
8377
8378 assert (strcmp (insn->name, str) == 0);
8379
1f25f5d3
CD
8380 if (OPCODE_IS_MEMBER (insn,
8381 (mips_opts.isa
3396de36 8382 | (file_ase_mips16 ? INSN_MIPS16 : 0)
deec1734 8383 | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
98d3f06f 8384 | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
fef14a42 8385 mips_opts.arch))
b34976b6 8386 ok = TRUE;
bdaaa2e1 8387 else
b34976b6 8388 ok = FALSE;
bdaaa2e1 8389
252b5132
RH
8390 if (insn->pinfo != INSN_MACRO)
8391 {
fef14a42 8392 if (mips_opts.arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
b34976b6 8393 ok = FALSE;
252b5132
RH
8394 }
8395
8396 if (! ok)
8397 {
8398 if (insn + 1 < &mips_opcodes[NUMOPCODES]
8399 && strcmp (insn->name, insn[1].name) == 0)
8400 {
8401 ++insn;
8402 continue;
8403 }
252b5132 8404 else
beae10d5 8405 {
268f6bed
L
8406 if (!insn_error)
8407 {
8408 static char buf[100];
fef14a42
TS
8409 sprintf (buf,
8410 _("opcode not supported on this processor: %s (%s)"),
8411 mips_cpu_info_from_arch (mips_opts.arch)->name,
8412 mips_cpu_info_from_isa (mips_opts.isa)->name);
268f6bed
L
8413 insn_error = buf;
8414 }
8415 if (save_c)
8416 *(--s) = save_c;
2bd7f1f3 8417 return;
252b5132 8418 }
252b5132
RH
8419 }
8420
8421 ip->insn_mo = insn;
8422 ip->insn_opcode = insn->match;
268f6bed 8423 insn_error = NULL;
252b5132
RH
8424 for (args = insn->args;; ++args)
8425 {
deec1734
CD
8426 int is_mdmx;
8427
ad8d3bb3 8428 s += strspn (s, " \t");
deec1734 8429 is_mdmx = 0;
252b5132
RH
8430 switch (*args)
8431 {
8432 case '\0': /* end of args */
8433 if (*s == '\0')
8434 return;
8435 break;
8436
8437 case ',':
8438 if (*s++ == *args)
8439 continue;
8440 s--;
8441 switch (*++args)
8442 {
8443 case 'r':
8444 case 'v':
38487616 8445 ip->insn_opcode |= lastregno << OP_SH_RS;
252b5132
RH
8446 continue;
8447
8448 case 'w':
38487616
TS
8449 ip->insn_opcode |= lastregno << OP_SH_RT;
8450 continue;
8451
252b5132 8452 case 'W':
38487616 8453 ip->insn_opcode |= lastregno << OP_SH_FT;
252b5132
RH
8454 continue;
8455
8456 case 'V':
38487616 8457 ip->insn_opcode |= lastregno << OP_SH_FS;
252b5132
RH
8458 continue;
8459 }
8460 break;
8461
8462 case '(':
8463 /* Handle optional base register.
8464 Either the base register is omitted or
bdaaa2e1 8465 we must have a left paren. */
252b5132
RH
8466 /* This is dependent on the next operand specifier
8467 is a base register specification. */
8468 assert (args[1] == 'b' || args[1] == '5'
8469 || args[1] == '-' || args[1] == '4');
8470 if (*s == '\0')
8471 return;
8472
8473 case ')': /* these must match exactly */
60b63b72
RS
8474 case '[':
8475 case ']':
252b5132
RH
8476 if (*s++ == *args)
8477 continue;
8478 break;
8479
af7ee8bf
CD
8480 case '+': /* Opcode extension character. */
8481 switch (*++args)
8482 {
071742cf
CD
8483 case 'A': /* ins/ext position, becomes LSB. */
8484 limlo = 0;
8485 limhi = 31;
5f74bc13
CD
8486 goto do_lsb;
8487 case 'E':
8488 limlo = 32;
8489 limhi = 63;
8490 goto do_lsb;
8491do_lsb:
071742cf
CD
8492 my_getExpression (&imm_expr, s);
8493 check_absolute_expr (ip, &imm_expr);
8494 if ((unsigned long) imm_expr.X_add_number < limlo
8495 || (unsigned long) imm_expr.X_add_number > limhi)
8496 {
8497 as_bad (_("Improper position (%lu)"),
8498 (unsigned long) imm_expr.X_add_number);
8499 imm_expr.X_add_number = limlo;
8500 }
8501 lastpos = imm_expr.X_add_number;
8502 ip->insn_opcode |= (imm_expr.X_add_number
8503 & OP_MASK_SHAMT) << OP_SH_SHAMT;
8504 imm_expr.X_op = O_absent;
8505 s = expr_end;
8506 continue;
8507
8508 case 'B': /* ins size, becomes MSB. */
8509 limlo = 1;
8510 limhi = 32;
5f74bc13
CD
8511 goto do_msb;
8512 case 'F':
8513 limlo = 33;
8514 limhi = 64;
8515 goto do_msb;
8516do_msb:
071742cf
CD
8517 my_getExpression (&imm_expr, s);
8518 check_absolute_expr (ip, &imm_expr);
8519 /* Check for negative input so that small negative numbers
8520 will not succeed incorrectly. The checks against
8521 (pos+size) transitively check "size" itself,
8522 assuming that "pos" is reasonable. */
8523 if ((long) imm_expr.X_add_number < 0
8524 || ((unsigned long) imm_expr.X_add_number
8525 + lastpos) < limlo
8526 || ((unsigned long) imm_expr.X_add_number
8527 + lastpos) > limhi)
8528 {
8529 as_bad (_("Improper insert size (%lu, position %lu)"),
8530 (unsigned long) imm_expr.X_add_number,
8531 (unsigned long) lastpos);
8532 imm_expr.X_add_number = limlo - lastpos;
8533 }
8534 ip->insn_opcode |= ((lastpos + imm_expr.X_add_number - 1)
8535 & OP_MASK_INSMSB) << OP_SH_INSMSB;
8536 imm_expr.X_op = O_absent;
8537 s = expr_end;
8538 continue;
8539
8540 case 'C': /* ext size, becomes MSBD. */
8541 limlo = 1;
8542 limhi = 32;
5f74bc13
CD
8543 goto do_msbd;
8544 case 'G':
8545 limlo = 33;
8546 limhi = 64;
8547 goto do_msbd;
8548 case 'H':
8549 limlo = 33;
8550 limhi = 64;
8551 goto do_msbd;
8552do_msbd:
071742cf
CD
8553 my_getExpression (&imm_expr, s);
8554 check_absolute_expr (ip, &imm_expr);
8555 /* Check for negative input so that small negative numbers
8556 will not succeed incorrectly. The checks against
8557 (pos+size) transitively check "size" itself,
8558 assuming that "pos" is reasonable. */
8559 if ((long) imm_expr.X_add_number < 0
8560 || ((unsigned long) imm_expr.X_add_number
8561 + lastpos) < limlo
8562 || ((unsigned long) imm_expr.X_add_number
8563 + lastpos) > limhi)
8564 {
8565 as_bad (_("Improper extract size (%lu, position %lu)"),
8566 (unsigned long) imm_expr.X_add_number,
8567 (unsigned long) lastpos);
8568 imm_expr.X_add_number = limlo - lastpos;
8569 }
8570 ip->insn_opcode |= ((imm_expr.X_add_number - 1)
8571 & OP_MASK_EXTMSBD) << OP_SH_EXTMSBD;
8572 imm_expr.X_op = O_absent;
8573 s = expr_end;
8574 continue;
af7ee8bf 8575
bbcc0807
CD
8576 case 'D':
8577 /* +D is for disassembly only; never match. */
8578 break;
8579
5f74bc13
CD
8580 case 'I':
8581 /* "+I" is like "I", except that imm2_expr is used. */
8582 my_getExpression (&imm2_expr, s);
8583 if (imm2_expr.X_op != O_big
8584 && imm2_expr.X_op != O_constant)
8585 insn_error = _("absolute expression required");
13757d0c 8586 normalize_constant_expr (&imm2_expr);
5f74bc13
CD
8587 s = expr_end;
8588 continue;
8589
af7ee8bf
CD
8590 default:
8591 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8592 *args, insn->name, insn->args);
8593 /* Further processing is fruitless. */
8594 return;
8595 }
8596 break;
8597
252b5132
RH
8598 case '<': /* must be at least one digit */
8599 /*
8600 * According to the manual, if the shift amount is greater
b6ff326e
KH
8601 * than 31 or less than 0, then the shift amount should be
8602 * mod 32. In reality the mips assembler issues an error.
252b5132
RH
8603 * We issue a warning and mask out all but the low 5 bits.
8604 */
8605 my_getExpression (&imm_expr, s);
8606 check_absolute_expr (ip, &imm_expr);
8607 if ((unsigned long) imm_expr.X_add_number > 31)
8608 {
793b27f4
TS
8609 as_warn (_("Improper shift amount (%lu)"),
8610 (unsigned long) imm_expr.X_add_number);
38487616 8611 imm_expr.X_add_number &= OP_MASK_SHAMT;
252b5132 8612 }
38487616 8613 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SHAMT;
252b5132
RH
8614 imm_expr.X_op = O_absent;
8615 s = expr_end;
8616 continue;
8617
8618 case '>': /* shift amount minus 32 */
8619 my_getExpression (&imm_expr, s);
8620 check_absolute_expr (ip, &imm_expr);
8621 if ((unsigned long) imm_expr.X_add_number < 32
8622 || (unsigned long) imm_expr.X_add_number > 63)
8623 break;
38487616 8624 ip->insn_opcode |= (imm_expr.X_add_number - 32) << OP_SH_SHAMT;
252b5132
RH
8625 imm_expr.X_op = O_absent;
8626 s = expr_end;
8627 continue;
8628
252b5132
RH
8629 case 'k': /* cache code */
8630 case 'h': /* prefx code */
8631 my_getExpression (&imm_expr, s);
8632 check_absolute_expr (ip, &imm_expr);
8633 if ((unsigned long) imm_expr.X_add_number > 31)
8634 {
8635 as_warn (_("Invalid value for `%s' (%lu)"),
8636 ip->insn_mo->name,
8637 (unsigned long) imm_expr.X_add_number);
8638 imm_expr.X_add_number &= 0x1f;
8639 }
8640 if (*args == 'k')
8641 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
8642 else
8643 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
8644 imm_expr.X_op = O_absent;
8645 s = expr_end;
8646 continue;
8647
8648 case 'c': /* break code */
8649 my_getExpression (&imm_expr, s);
8650 check_absolute_expr (ip, &imm_expr);
793b27f4 8651 if ((unsigned long) imm_expr.X_add_number > 1023)
252b5132 8652 {
793b27f4
TS
8653 as_warn (_("Illegal break code (%lu)"),
8654 (unsigned long) imm_expr.X_add_number);
38487616 8655 imm_expr.X_add_number &= OP_MASK_CODE;
252b5132 8656 }
38487616 8657 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE;
252b5132
RH
8658 imm_expr.X_op = O_absent;
8659 s = expr_end;
8660 continue;
8661
8662 case 'q': /* lower break code */
8663 my_getExpression (&imm_expr, s);
8664 check_absolute_expr (ip, &imm_expr);
793b27f4 8665 if ((unsigned long) imm_expr.X_add_number > 1023)
252b5132 8666 {
793b27f4
TS
8667 as_warn (_("Illegal lower break code (%lu)"),
8668 (unsigned long) imm_expr.X_add_number);
38487616 8669 imm_expr.X_add_number &= OP_MASK_CODE2;
252b5132 8670 }
38487616 8671 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE2;
252b5132
RH
8672 imm_expr.X_op = O_absent;
8673 s = expr_end;
8674 continue;
8675
4372b673 8676 case 'B': /* 20-bit syscall/break code. */
156c2f8b 8677 my_getExpression (&imm_expr, s);
156c2f8b 8678 check_absolute_expr (ip, &imm_expr);
793b27f4
TS
8679 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
8680 as_warn (_("Illegal 20-bit code (%lu)"),
8681 (unsigned long) imm_expr.X_add_number);
38487616 8682 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE20;
252b5132
RH
8683 imm_expr.X_op = O_absent;
8684 s = expr_end;
8685 continue;
8686
98d3f06f 8687 case 'C': /* Coprocessor code */
beae10d5 8688 my_getExpression (&imm_expr, s);
252b5132 8689 check_absolute_expr (ip, &imm_expr);
98d3f06f 8690 if ((unsigned long) imm_expr.X_add_number >= (1 << 25))
252b5132 8691 {
793b27f4
TS
8692 as_warn (_("Coproccesor code > 25 bits (%lu)"),
8693 (unsigned long) imm_expr.X_add_number);
98d3f06f 8694 imm_expr.X_add_number &= ((1 << 25) - 1);
252b5132 8695 }
beae10d5
KH
8696 ip->insn_opcode |= imm_expr.X_add_number;
8697 imm_expr.X_op = O_absent;
8698 s = expr_end;
8699 continue;
252b5132 8700
4372b673
NC
8701 case 'J': /* 19-bit wait code. */
8702 my_getExpression (&imm_expr, s);
8703 check_absolute_expr (ip, &imm_expr);
793b27f4
TS
8704 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
8705 as_warn (_("Illegal 19-bit code (%lu)"),
8706 (unsigned long) imm_expr.X_add_number);
38487616 8707 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE19;
4372b673
NC
8708 imm_expr.X_op = O_absent;
8709 s = expr_end;
8710 continue;
8711
252b5132 8712 case 'P': /* Performance register */
beae10d5 8713 my_getExpression (&imm_expr, s);
252b5132 8714 check_absolute_expr (ip, &imm_expr);
beae10d5 8715 if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
252b5132 8716 {
793b27f4
TS
8717 as_warn (_("Invalid performance register (%lu)"),
8718 (unsigned long) imm_expr.X_add_number);
38487616 8719 imm_expr.X_add_number &= OP_MASK_PERFREG;
252b5132 8720 }
38487616 8721 ip->insn_opcode |= (imm_expr.X_add_number << OP_SH_PERFREG);
beae10d5
KH
8722 imm_expr.X_op = O_absent;
8723 s = expr_end;
8724 continue;
252b5132
RH
8725
8726 case 'b': /* base register */
8727 case 'd': /* destination register */
8728 case 's': /* source register */
8729 case 't': /* target register */
8730 case 'r': /* both target and source */
8731 case 'v': /* both dest and source */
8732 case 'w': /* both dest and target */
8733 case 'E': /* coprocessor target register */
8734 case 'G': /* coprocessor destination register */
af7ee8bf 8735 case 'K': /* 'rdhwr' destination register */
252b5132
RH
8736 case 'x': /* ignore register name */
8737 case 'z': /* must be zero register */
4372b673 8738 case 'U': /* destination register (clo/clz). */
252b5132
RH
8739 s_reset = s;
8740 if (s[0] == '$')
8741 {
8742
3882b010 8743 if (ISDIGIT (s[1]))
252b5132
RH
8744 {
8745 ++s;
8746 regno = 0;
8747 do
8748 {
8749 regno *= 10;
8750 regno += *s - '0';
8751 ++s;
8752 }
3882b010 8753 while (ISDIGIT (*s));
252b5132
RH
8754 if (regno > 31)
8755 as_bad (_("Invalid register number (%d)"), regno);
8756 }
af7ee8bf 8757 else if (*args == 'E' || *args == 'G' || *args == 'K')
252b5132
RH
8758 goto notreg;
8759 else
8760 {
76db943d
TS
8761 if (s[1] == 'r' && s[2] == 'a')
8762 {
8763 s += 3;
8764 regno = RA;
8765 }
8766 else if (s[1] == 'f' && s[2] == 'p')
252b5132
RH
8767 {
8768 s += 3;
8769 regno = FP;
8770 }
8771 else if (s[1] == 's' && s[2] == 'p')
8772 {
8773 s += 3;
8774 regno = SP;
8775 }
8776 else if (s[1] == 'g' && s[2] == 'p')
8777 {
8778 s += 3;
8779 regno = GP;
8780 }
8781 else if (s[1] == 'a' && s[2] == 't')
8782 {
8783 s += 3;
8784 regno = AT;
8785 }
8786 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8787 {
8788 s += 4;
8789 regno = KT0;
8790 }
8791 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8792 {
8793 s += 4;
8794 regno = KT1;
8795 }
85b51719
TS
8796 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
8797 {
8798 s += 5;
8799 regno = ZERO;
8800 }
252b5132
RH
8801 else if (itbl_have_entries)
8802 {
8803 char *p, *n;
d7ba4a77 8804 unsigned long r;
252b5132 8805
d7ba4a77 8806 p = s + 1; /* advance past '$' */
252b5132
RH
8807 n = itbl_get_field (&p); /* n is name */
8808
d7ba4a77
ILT
8809 /* See if this is a register defined in an
8810 itbl entry. */
8811 if (itbl_get_reg_val (n, &r))
252b5132
RH
8812 {
8813 /* Get_field advances to the start of
8814 the next field, so we need to back
d7ba4a77 8815 rack to the end of the last field. */
bdaaa2e1 8816 if (p)
252b5132 8817 s = p - 1;
bdaaa2e1 8818 else
d7ba4a77 8819 s = strchr (s, '\0');
252b5132
RH
8820 regno = r;
8821 }
8822 else
8823 goto notreg;
beae10d5 8824 }
252b5132
RH
8825 else
8826 goto notreg;
8827 }
8828 if (regno == AT
8829 && ! mips_opts.noat
8830 && *args != 'E'
af7ee8bf
CD
8831 && *args != 'G'
8832 && *args != 'K')
252b5132
RH
8833 as_warn (_("Used $at without \".set noat\""));
8834 c = *args;
8835 if (*s == ' ')
f9419b05 8836 ++s;
252b5132
RH
8837 if (args[1] != *s)
8838 {
8839 if (c == 'r' || c == 'v' || c == 'w')
8840 {
8841 regno = lastregno;
8842 s = s_reset;
f9419b05 8843 ++args;
252b5132
RH
8844 }
8845 }
8846 /* 'z' only matches $0. */
8847 if (c == 'z' && regno != 0)
8848 break;
8849
bdaaa2e1
KH
8850 /* Now that we have assembled one operand, we use the args string
8851 * to figure out where it goes in the instruction. */
252b5132
RH
8852 switch (c)
8853 {
8854 case 'r':
8855 case 's':
8856 case 'v':
8857 case 'b':
38487616 8858 ip->insn_opcode |= regno << OP_SH_RS;
252b5132
RH
8859 break;
8860 case 'd':
8861 case 'G':
af7ee8bf 8862 case 'K':
38487616 8863 ip->insn_opcode |= regno << OP_SH_RD;
252b5132 8864 break;
4372b673 8865 case 'U':
38487616
TS
8866 ip->insn_opcode |= regno << OP_SH_RD;
8867 ip->insn_opcode |= regno << OP_SH_RT;
4372b673 8868 break;
252b5132
RH
8869 case 'w':
8870 case 't':
8871 case 'E':
38487616 8872 ip->insn_opcode |= regno << OP_SH_RT;
252b5132
RH
8873 break;
8874 case 'x':
8875 /* This case exists because on the r3000 trunc
8876 expands into a macro which requires a gp
8877 register. On the r6000 or r4000 it is
8878 assembled into a single instruction which
8879 ignores the register. Thus the insn version
8880 is MIPS_ISA2 and uses 'x', and the macro
8881 version is MIPS_ISA1 and uses 't'. */
8882 break;
8883 case 'z':
8884 /* This case is for the div instruction, which
8885 acts differently if the destination argument
8886 is $0. This only matches $0, and is checked
8887 outside the switch. */
8888 break;
8889 case 'D':
8890 /* Itbl operand; not yet implemented. FIXME ?? */
8891 break;
8892 /* What about all other operands like 'i', which
8893 can be specified in the opcode table? */
8894 }
8895 lastregno = regno;
8896 continue;
8897 }
8898 notreg:
8899 switch (*args++)
8900 {
8901 case 'r':
8902 case 'v':
38487616 8903 ip->insn_opcode |= lastregno << OP_SH_RS;
252b5132
RH
8904 continue;
8905 case 'w':
38487616 8906 ip->insn_opcode |= lastregno << OP_SH_RT;
252b5132
RH
8907 continue;
8908 }
8909 break;
8910
deec1734
CD
8911 case 'O': /* MDMX alignment immediate constant. */
8912 my_getExpression (&imm_expr, s);
8913 check_absolute_expr (ip, &imm_expr);
8914 if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
8915 {
8916 as_warn ("Improper align amount (%ld), using low bits",
8917 (long) imm_expr.X_add_number);
8918 imm_expr.X_add_number &= OP_MASK_ALN;
8919 }
8920 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_ALN;
8921 imm_expr.X_op = O_absent;
8922 s = expr_end;
8923 continue;
8924
8925 case 'Q': /* MDMX vector, element sel, or const. */
8926 if (s[0] != '$')
8927 {
8928 /* MDMX Immediate. */
8929 my_getExpression (&imm_expr, s);
8930 check_absolute_expr (ip, &imm_expr);
8931 if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
8932 {
8933 as_warn (_("Invalid MDMX Immediate (%ld)"),
8934 (long) imm_expr.X_add_number);
8935 imm_expr.X_add_number &= OP_MASK_FT;
8936 }
8937 imm_expr.X_add_number &= OP_MASK_FT;
8938 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8939 ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
8940 else
8941 ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
8942 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_FT;
8943 imm_expr.X_op = O_absent;
8944 s = expr_end;
8945 continue;
8946 }
8947 /* Not MDMX Immediate. Fall through. */
8948 case 'X': /* MDMX destination register. */
8949 case 'Y': /* MDMX source register. */
8950 case 'Z': /* MDMX target register. */
8951 is_mdmx = 1;
252b5132
RH
8952 case 'D': /* floating point destination register */
8953 case 'S': /* floating point source register */
8954 case 'T': /* floating point target register */
8955 case 'R': /* floating point source register */
8956 case 'V':
8957 case 'W':
8958 s_reset = s;
deec1734
CD
8959 /* Accept $fN for FP and MDMX register numbers, and in
8960 addition accept $vN for MDMX register numbers. */
8961 if ((s[0] == '$' && s[1] == 'f' && ISDIGIT (s[2]))
8962 || (is_mdmx != 0 && s[0] == '$' && s[1] == 'v'
8963 && ISDIGIT (s[2])))
252b5132
RH
8964 {
8965 s += 2;
8966 regno = 0;
8967 do
8968 {
8969 regno *= 10;
8970 regno += *s - '0';
8971 ++s;
8972 }
3882b010 8973 while (ISDIGIT (*s));
252b5132
RH
8974
8975 if (regno > 31)
8976 as_bad (_("Invalid float register number (%d)"), regno);
8977
8978 if ((regno & 1) != 0
ca4e0257 8979 && HAVE_32BIT_FPRS
252b5132
RH
8980 && ! (strcmp (str, "mtc1") == 0
8981 || strcmp (str, "mfc1") == 0
8982 || strcmp (str, "lwc1") == 0
8983 || strcmp (str, "swc1") == 0
8984 || strcmp (str, "l.s") == 0
8985 || strcmp (str, "s.s") == 0))
8986 as_warn (_("Float register should be even, was %d"),
8987 regno);
8988
8989 c = *args;
8990 if (*s == ' ')
f9419b05 8991 ++s;
252b5132
RH
8992 if (args[1] != *s)
8993 {
8994 if (c == 'V' || c == 'W')
8995 {
8996 regno = lastregno;
8997 s = s_reset;
f9419b05 8998 ++args;
252b5132
RH
8999 }
9000 }
9001 switch (c)
9002 {
9003 case 'D':
deec1734 9004 case 'X':
38487616 9005 ip->insn_opcode |= regno << OP_SH_FD;
252b5132
RH
9006 break;
9007 case 'V':
9008 case 'S':
deec1734 9009 case 'Y':
38487616 9010 ip->insn_opcode |= regno << OP_SH_FS;
252b5132 9011 break;
deec1734
CD
9012 case 'Q':
9013 /* This is like 'Z', but also needs to fix the MDMX
9014 vector/scalar select bits. Note that the
9015 scalar immediate case is handled above. */
9016 if (*s == '[')
9017 {
9018 int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
9019 int max_el = (is_qh ? 3 : 7);
9020 s++;
9021 my_getExpression(&imm_expr, s);
9022 check_absolute_expr (ip, &imm_expr);
9023 s = expr_end;
9024 if (imm_expr.X_add_number > max_el)
9025 as_bad(_("Bad element selector %ld"),
9026 (long) imm_expr.X_add_number);
9027 imm_expr.X_add_number &= max_el;
9028 ip->insn_opcode |= (imm_expr.X_add_number
9029 << (OP_SH_VSEL +
9030 (is_qh ? 2 : 1)));
9031 if (*s != ']')
9032 as_warn(_("Expecting ']' found '%s'"), s);
9033 else
9034 s++;
9035 }
9036 else
9037 {
9038 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
9039 ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
9040 << OP_SH_VSEL);
9041 else
9042 ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
9043 OP_SH_VSEL);
9044 }
9045 /* Fall through */
252b5132
RH
9046 case 'W':
9047 case 'T':
deec1734 9048 case 'Z':
38487616 9049 ip->insn_opcode |= regno << OP_SH_FT;
252b5132
RH
9050 break;
9051 case 'R':
38487616 9052 ip->insn_opcode |= regno << OP_SH_FR;
252b5132
RH
9053 break;
9054 }
9055 lastregno = regno;
9056 continue;
9057 }
9058
252b5132
RH
9059 switch (*args++)
9060 {
9061 case 'V':
38487616 9062 ip->insn_opcode |= lastregno << OP_SH_FS;
252b5132
RH
9063 continue;
9064 case 'W':
38487616 9065 ip->insn_opcode |= lastregno << OP_SH_FT;
252b5132
RH
9066 continue;
9067 }
9068 break;
9069
9070 case 'I':
9071 my_getExpression (&imm_expr, s);
9072 if (imm_expr.X_op != O_big
9073 && imm_expr.X_op != O_constant)
9074 insn_error = _("absolute expression required");
13757d0c 9075 normalize_constant_expr (&imm_expr);
252b5132
RH
9076 s = expr_end;
9077 continue;
9078
9079 case 'A':
9080 my_getExpression (&offset_expr, s);
f6688943 9081 *imm_reloc = BFD_RELOC_32;
252b5132
RH
9082 s = expr_end;
9083 continue;
9084
9085 case 'F':
9086 case 'L':
9087 case 'f':
9088 case 'l':
9089 {
9090 int f64;
ca4e0257 9091 int using_gprs;
252b5132
RH
9092 char *save_in;
9093 char *err;
9094 unsigned char temp[8];
9095 int len;
9096 unsigned int length;
9097 segT seg;
9098 subsegT subseg;
9099 char *p;
9100
9101 /* These only appear as the last operand in an
9102 instruction, and every instruction that accepts
9103 them in any variant accepts them in all variants.
9104 This means we don't have to worry about backing out
9105 any changes if the instruction does not match.
9106
9107 The difference between them is the size of the
9108 floating point constant and where it goes. For 'F'
9109 and 'L' the constant is 64 bits; for 'f' and 'l' it
9110 is 32 bits. Where the constant is placed is based
9111 on how the MIPS assembler does things:
9112 F -- .rdata
9113 L -- .lit8
9114 f -- immediate value
9115 l -- .lit4
9116
9117 The .lit4 and .lit8 sections are only used if
9118 permitted by the -G argument.
9119
9120 When generating embedded PIC code, we use the
9121 .lit8 section but not the .lit4 section (we can do
9122 .lit4 inline easily; we need to put .lit8
9123 somewhere in the data segment, and using .lit8
9124 permits the linker to eventually combine identical
ca4e0257
RS
9125 .lit8 entries).
9126
9127 The code below needs to know whether the target register
9128 is 32 or 64 bits wide. It relies on the fact 'f' and
9129 'F' are used with GPR-based instructions and 'l' and
9130 'L' are used with FPR-based instructions. */
252b5132
RH
9131
9132 f64 = *args == 'F' || *args == 'L';
ca4e0257 9133 using_gprs = *args == 'F' || *args == 'f';
252b5132
RH
9134
9135 save_in = input_line_pointer;
9136 input_line_pointer = s;
9137 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
9138 length = len;
9139 s = input_line_pointer;
9140 input_line_pointer = save_in;
9141 if (err != NULL && *err != '\0')
9142 {
9143 as_bad (_("Bad floating point constant: %s"), err);
9144 memset (temp, '\0', sizeof temp);
9145 length = f64 ? 8 : 4;
9146 }
9147
156c2f8b 9148 assert (length == (unsigned) (f64 ? 8 : 4));
252b5132
RH
9149
9150 if (*args == 'f'
9151 || (*args == 'l'
9152 && (! USE_GLOBAL_POINTER_OPT
9153 || mips_pic == EMBEDDED_PIC
9154 || g_switch_value < 4
9155 || (temp[0] == 0 && temp[1] == 0)
9156 || (temp[2] == 0 && temp[3] == 0))))
9157 {
9158 imm_expr.X_op = O_constant;
9159 if (! target_big_endian)
9160 imm_expr.X_add_number = bfd_getl32 (temp);
9161 else
9162 imm_expr.X_add_number = bfd_getb32 (temp);
9163 }
9164 else if (length > 4
119d663a 9165 && ! mips_disable_float_construction
ca4e0257
RS
9166 /* Constants can only be constructed in GPRs and
9167 copied to FPRs if the GPRs are at least as wide
9168 as the FPRs. Force the constant into memory if
9169 we are using 64-bit FPRs but the GPRs are only
9170 32 bits wide. */
9171 && (using_gprs
9172 || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
252b5132
RH
9173 && ((temp[0] == 0 && temp[1] == 0)
9174 || (temp[2] == 0 && temp[3] == 0))
9175 && ((temp[4] == 0 && temp[5] == 0)
9176 || (temp[6] == 0 && temp[7] == 0)))
9177 {
ca4e0257
RS
9178 /* The value is simple enough to load with a couple of
9179 instructions. If using 32-bit registers, set
9180 imm_expr to the high order 32 bits and offset_expr to
9181 the low order 32 bits. Otherwise, set imm_expr to
9182 the entire 64 bit constant. */
9183 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
252b5132
RH
9184 {
9185 imm_expr.X_op = O_constant;
9186 offset_expr.X_op = O_constant;
9187 if (! target_big_endian)
9188 {
9189 imm_expr.X_add_number = bfd_getl32 (temp + 4);
9190 offset_expr.X_add_number = bfd_getl32 (temp);
9191 }
9192 else
9193 {
9194 imm_expr.X_add_number = bfd_getb32 (temp);
9195 offset_expr.X_add_number = bfd_getb32 (temp + 4);
9196 }
9197 if (offset_expr.X_add_number == 0)
9198 offset_expr.X_op = O_absent;
9199 }
9200 else if (sizeof (imm_expr.X_add_number) > 4)
9201 {
9202 imm_expr.X_op = O_constant;
9203 if (! target_big_endian)
9204 imm_expr.X_add_number = bfd_getl64 (temp);
9205 else
9206 imm_expr.X_add_number = bfd_getb64 (temp);
9207 }
9208 else
9209 {
9210 imm_expr.X_op = O_big;
9211 imm_expr.X_add_number = 4;
9212 if (! target_big_endian)
9213 {
9214 generic_bignum[0] = bfd_getl16 (temp);
9215 generic_bignum[1] = bfd_getl16 (temp + 2);
9216 generic_bignum[2] = bfd_getl16 (temp + 4);
9217 generic_bignum[3] = bfd_getl16 (temp + 6);
9218 }
9219 else
9220 {
9221 generic_bignum[0] = bfd_getb16 (temp + 6);
9222 generic_bignum[1] = bfd_getb16 (temp + 4);
9223 generic_bignum[2] = bfd_getb16 (temp + 2);
9224 generic_bignum[3] = bfd_getb16 (temp);
9225 }
9226 }
9227 }
9228 else
9229 {
9230 const char *newname;
9231 segT new_seg;
9232
9233 /* Switch to the right section. */
9234 seg = now_seg;
9235 subseg = now_subseg;
9236 switch (*args)
9237 {
9238 default: /* unused default case avoids warnings. */
9239 case 'L':
9240 newname = RDATA_SECTION_NAME;
bb2d6cd7
GK
9241 if ((USE_GLOBAL_POINTER_OPT && g_switch_value >= 8)
9242 || mips_pic == EMBEDDED_PIC)
252b5132
RH
9243 newname = ".lit8";
9244 break;
9245 case 'F':
bb2d6cd7
GK
9246 if (mips_pic == EMBEDDED_PIC)
9247 newname = ".lit8";
9248 else
9249 newname = RDATA_SECTION_NAME;
252b5132
RH
9250 break;
9251 case 'l':
9252 assert (!USE_GLOBAL_POINTER_OPT
9253 || g_switch_value >= 4);
9254 newname = ".lit4";
9255 break;
9256 }
9257 new_seg = subseg_new (newname, (subsegT) 0);
9258 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
9259 bfd_set_section_flags (stdoutput, new_seg,
9260 (SEC_ALLOC
9261 | SEC_LOAD
9262 | SEC_READONLY
9263 | SEC_DATA));
9264 frag_align (*args == 'l' ? 2 : 3, 0, 0);
9265 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
9266 && strcmp (TARGET_OS, "elf") != 0)
9267 record_alignment (new_seg, 4);
9268 else
9269 record_alignment (new_seg, *args == 'l' ? 2 : 3);
9270 if (seg == now_seg)
9271 as_bad (_("Can't use floating point insn in this section"));
9272
9273 /* Set the argument to the current address in the
9274 section. */
9275 offset_expr.X_op = O_symbol;
9276 offset_expr.X_add_symbol =
9277 symbol_new ("L0\001", now_seg,
9278 (valueT) frag_now_fix (), frag_now);
9279 offset_expr.X_add_number = 0;
9280
9281 /* Put the floating point number into the section. */
9282 p = frag_more ((int) length);
9283 memcpy (p, temp, length);
9284
9285 /* Switch back to the original section. */
9286 subseg_set (seg, subseg);
9287 }
9288 }
9289 continue;
9290
9291 case 'i': /* 16 bit unsigned immediate */
9292 case 'j': /* 16 bit signed immediate */
f6688943 9293 *imm_reloc = BFD_RELOC_LO16;
5e0116d5 9294 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0)
252b5132
RH
9295 {
9296 int more;
5e0116d5
RS
9297 offsetT minval, maxval;
9298
9299 more = (insn + 1 < &mips_opcodes[NUMOPCODES]
9300 && strcmp (insn->name, insn[1].name) == 0);
9301
9302 /* If the expression was written as an unsigned number,
9303 only treat it as signed if there are no more
9304 alternatives. */
9305 if (more
9306 && *args == 'j'
9307 && sizeof (imm_expr.X_add_number) <= 4
9308 && imm_expr.X_op == O_constant
9309 && imm_expr.X_add_number < 0
9310 && imm_expr.X_unsigned
9311 && HAVE_64BIT_GPRS)
9312 break;
9313
9314 /* For compatibility with older assemblers, we accept
9315 0x8000-0xffff as signed 16-bit numbers when only
9316 signed numbers are allowed. */
9317 if (*args == 'i')
9318 minval = 0, maxval = 0xffff;
9319 else if (more)
9320 minval = -0x8000, maxval = 0x7fff;
252b5132 9321 else
5e0116d5
RS
9322 minval = -0x8000, maxval = 0xffff;
9323
9324 if (imm_expr.X_op != O_constant
9325 || imm_expr.X_add_number < minval
9326 || imm_expr.X_add_number > maxval)
252b5132
RH
9327 {
9328 if (more)
9329 break;
2ae7e77b
AH
9330 if (imm_expr.X_op == O_constant
9331 || imm_expr.X_op == O_big)
5e0116d5 9332 as_bad (_("expression out of range"));
252b5132
RH
9333 }
9334 }
9335 s = expr_end;
9336 continue;
9337
9338 case 'o': /* 16 bit offset */
5e0116d5
RS
9339 /* Check whether there is only a single bracketed expression
9340 left. If so, it must be the base register and the
9341 constant must be zero. */
9342 if (*s == '(' && strchr (s + 1, '(') == 0)
9343 {
9344 offset_expr.X_op = O_constant;
9345 offset_expr.X_add_number = 0;
9346 continue;
9347 }
252b5132
RH
9348
9349 /* If this value won't fit into a 16 bit offset, then go
9350 find a macro that will generate the 32 bit offset
afdbd6d0 9351 code pattern. */
5e0116d5 9352 if (my_getSmallExpression (&offset_expr, offset_reloc, s) == 0
252b5132
RH
9353 && (offset_expr.X_op != O_constant
9354 || offset_expr.X_add_number >= 0x8000
afdbd6d0 9355 || offset_expr.X_add_number < -0x8000))
252b5132
RH
9356 break;
9357
252b5132
RH
9358 s = expr_end;
9359 continue;
9360
9361 case 'p': /* pc relative offset */
0b25d3e6 9362 *offset_reloc = BFD_RELOC_16_PCREL_S2;
252b5132
RH
9363 my_getExpression (&offset_expr, s);
9364 s = expr_end;
9365 continue;
9366
9367 case 'u': /* upper 16 bits */
5e0116d5
RS
9368 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0
9369 && imm_expr.X_op == O_constant
9370 && (imm_expr.X_add_number < 0
9371 || imm_expr.X_add_number >= 0x10000))
252b5132
RH
9372 as_bad (_("lui expression not in range 0..65535"));
9373 s = expr_end;
9374 continue;
9375
9376 case 'a': /* 26 bit address */
9377 my_getExpression (&offset_expr, s);
9378 s = expr_end;
f6688943 9379 *offset_reloc = BFD_RELOC_MIPS_JMP;
252b5132
RH
9380 continue;
9381
9382 case 'N': /* 3 bit branch condition code */
9383 case 'M': /* 3 bit compare condition code */
9384 if (strncmp (s, "$fcc", 4) != 0)
9385 break;
9386 s += 4;
9387 regno = 0;
9388 do
9389 {
9390 regno *= 10;
9391 regno += *s - '0';
9392 ++s;
9393 }
3882b010 9394 while (ISDIGIT (*s));
252b5132 9395 if (regno > 7)
30c378fd
CD
9396 as_bad (_("Invalid condition code register $fcc%d"), regno);
9397 if ((strcmp(str + strlen(str) - 3, ".ps") == 0
9398 || strcmp(str + strlen(str) - 5, "any2f") == 0
9399 || strcmp(str + strlen(str) - 5, "any2t") == 0)
9400 && (regno & 1) != 0)
9401 as_warn(_("Condition code register should be even for %s, was %d"),
9402 str, regno);
9403 if ((strcmp(str + strlen(str) - 5, "any4f") == 0
9404 || strcmp(str + strlen(str) - 5, "any4t") == 0)
9405 && (regno & 3) != 0)
9406 as_warn(_("Condition code register should be 0 or 4 for %s, was %d"),
9407 str, regno);
252b5132
RH
9408 if (*args == 'N')
9409 ip->insn_opcode |= regno << OP_SH_BCC;
9410 else
9411 ip->insn_opcode |= regno << OP_SH_CCC;
beae10d5 9412 continue;
252b5132 9413
156c2f8b
NC
9414 case 'H':
9415 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
9416 s += 2;
3882b010 9417 if (ISDIGIT (*s))
156c2f8b
NC
9418 {
9419 c = 0;
9420 do
9421 {
9422 c *= 10;
9423 c += *s - '0';
9424 ++s;
9425 }
3882b010 9426 while (ISDIGIT (*s));
156c2f8b
NC
9427 }
9428 else
9429 c = 8; /* Invalid sel value. */
9430
9431 if (c > 7)
9432 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
9433 ip->insn_opcode |= c;
9434 continue;
9435
60b63b72
RS
9436 case 'e':
9437 /* Must be at least one digit. */
9438 my_getExpression (&imm_expr, s);
9439 check_absolute_expr (ip, &imm_expr);
9440
9441 if ((unsigned long) imm_expr.X_add_number
9442 > (unsigned long) OP_MASK_VECBYTE)
9443 {
9444 as_bad (_("bad byte vector index (%ld)"),
9445 (long) imm_expr.X_add_number);
9446 imm_expr.X_add_number = 0;
9447 }
9448
9449 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECBYTE;
9450 imm_expr.X_op = O_absent;
9451 s = expr_end;
9452 continue;
9453
9454 case '%':
9455 my_getExpression (&imm_expr, s);
9456 check_absolute_expr (ip, &imm_expr);
9457
9458 if ((unsigned long) imm_expr.X_add_number
9459 > (unsigned long) OP_MASK_VECALIGN)
9460 {
9461 as_bad (_("bad byte vector index (%ld)"),
9462 (long) imm_expr.X_add_number);
9463 imm_expr.X_add_number = 0;
9464 }
9465
9466 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECALIGN;
9467 imm_expr.X_op = O_absent;
9468 s = expr_end;
9469 continue;
9470
252b5132
RH
9471 default:
9472 as_bad (_("bad char = '%c'\n"), *args);
9473 internalError ();
9474 }
9475 break;
9476 }
9477 /* Args don't match. */
9478 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
9479 !strcmp (insn->name, insn[1].name))
9480 {
9481 ++insn;
9482 s = argsStart;
268f6bed 9483 insn_error = _("illegal operands");
252b5132
RH
9484 continue;
9485 }
268f6bed
L
9486 if (save_c)
9487 *(--s) = save_c;
252b5132
RH
9488 insn_error = _("illegal operands");
9489 return;
9490 }
9491}
9492
9493/* This routine assembles an instruction into its binary format when
9494 assembling for the mips16. As a side effect, it sets one of the
9495 global variables imm_reloc or offset_reloc to the type of
9496 relocation to do if one of the operands is an address expression.
9497 It also sets mips16_small and mips16_ext if the user explicitly
9498 requested a small or extended instruction. */
9499
9500static void
17a2f251 9501mips16_ip (char *str, struct mips_cl_insn *ip)
252b5132
RH
9502{
9503 char *s;
9504 const char *args;
9505 struct mips_opcode *insn;
9506 char *argsstart;
9507 unsigned int regno;
9508 unsigned int lastregno = 0;
9509 char *s_reset;
9510
9511 insn_error = NULL;
9512
b34976b6
AM
9513 mips16_small = FALSE;
9514 mips16_ext = FALSE;
252b5132 9515
3882b010 9516 for (s = str; ISLOWER (*s); ++s)
252b5132
RH
9517 ;
9518 switch (*s)
9519 {
9520 case '\0':
9521 break;
9522
9523 case ' ':
9524 *s++ = '\0';
9525 break;
9526
9527 case '.':
9528 if (s[1] == 't' && s[2] == ' ')
9529 {
9530 *s = '\0';
b34976b6 9531 mips16_small = TRUE;
252b5132
RH
9532 s += 3;
9533 break;
9534 }
9535 else if (s[1] == 'e' && s[2] == ' ')
9536 {
9537 *s = '\0';
b34976b6 9538 mips16_ext = TRUE;
252b5132
RH
9539 s += 3;
9540 break;
9541 }
9542 /* Fall through. */
9543 default:
9544 insn_error = _("unknown opcode");
9545 return;
9546 }
9547
9548 if (mips_opts.noautoextend && ! mips16_ext)
b34976b6 9549 mips16_small = TRUE;
252b5132
RH
9550
9551 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
9552 {
9553 insn_error = _("unrecognized opcode");
9554 return;
9555 }
9556
9557 argsstart = s;
9558 for (;;)
9559 {
9560 assert (strcmp (insn->name, str) == 0);
9561
9562 ip->insn_mo = insn;
9563 ip->insn_opcode = insn->match;
b34976b6 9564 ip->use_extend = FALSE;
252b5132 9565 imm_expr.X_op = O_absent;
f6688943
TS
9566 imm_reloc[0] = BFD_RELOC_UNUSED;
9567 imm_reloc[1] = BFD_RELOC_UNUSED;
9568 imm_reloc[2] = BFD_RELOC_UNUSED;
5f74bc13 9569 imm2_expr.X_op = O_absent;
252b5132 9570 offset_expr.X_op = O_absent;
f6688943
TS
9571 offset_reloc[0] = BFD_RELOC_UNUSED;
9572 offset_reloc[1] = BFD_RELOC_UNUSED;
9573 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
9574 for (args = insn->args; 1; ++args)
9575 {
9576 int c;
9577
9578 if (*s == ' ')
9579 ++s;
9580
9581 /* In this switch statement we call break if we did not find
9582 a match, continue if we did find a match, or return if we
9583 are done. */
9584
9585 c = *args;
9586 switch (c)
9587 {
9588 case '\0':
9589 if (*s == '\0')
9590 {
9591 /* Stuff the immediate value in now, if we can. */
9592 if (imm_expr.X_op == O_constant
f6688943 9593 && *imm_reloc > BFD_RELOC_UNUSED
252b5132
RH
9594 && insn->pinfo != INSN_MACRO)
9595 {
c4e7957c 9596 mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
b34976b6 9597 imm_expr.X_add_number, TRUE, mips16_small,
252b5132
RH
9598 mips16_ext, &ip->insn_opcode,
9599 &ip->use_extend, &ip->extend);
9600 imm_expr.X_op = O_absent;
f6688943 9601 *imm_reloc = BFD_RELOC_UNUSED;
252b5132
RH
9602 }
9603
9604 return;
9605 }
9606 break;
9607
9608 case ',':
9609 if (*s++ == c)
9610 continue;
9611 s--;
9612 switch (*++args)
9613 {
9614 case 'v':
9615 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9616 continue;
9617 case 'w':
9618 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9619 continue;
9620 }
9621 break;
9622
9623 case '(':
9624 case ')':
9625 if (*s++ == c)
9626 continue;
9627 break;
9628
9629 case 'v':
9630 case 'w':
9631 if (s[0] != '$')
9632 {
9633 if (c == 'v')
9634 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9635 else
9636 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9637 ++args;
9638 continue;
9639 }
9640 /* Fall through. */
9641 case 'x':
9642 case 'y':
9643 case 'z':
9644 case 'Z':
9645 case '0':
9646 case 'S':
9647 case 'R':
9648 case 'X':
9649 case 'Y':
9650 if (s[0] != '$')
9651 break;
9652 s_reset = s;
3882b010 9653 if (ISDIGIT (s[1]))
252b5132
RH
9654 {
9655 ++s;
9656 regno = 0;
9657 do
9658 {
9659 regno *= 10;
9660 regno += *s - '0';
9661 ++s;
9662 }
3882b010 9663 while (ISDIGIT (*s));
252b5132
RH
9664 if (regno > 31)
9665 {
9666 as_bad (_("invalid register number (%d)"), regno);
9667 regno = 2;
9668 }
9669 }
9670 else
9671 {
76db943d
TS
9672 if (s[1] == 'r' && s[2] == 'a')
9673 {
9674 s += 3;
9675 regno = RA;
9676 }
9677 else if (s[1] == 'f' && s[2] == 'p')
252b5132
RH
9678 {
9679 s += 3;
9680 regno = FP;
9681 }
9682 else if (s[1] == 's' && s[2] == 'p')
9683 {
9684 s += 3;
9685 regno = SP;
9686 }
9687 else if (s[1] == 'g' && s[2] == 'p')
9688 {
9689 s += 3;
9690 regno = GP;
9691 }
9692 else if (s[1] == 'a' && s[2] == 't')
9693 {
9694 s += 3;
9695 regno = AT;
9696 }
9697 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
9698 {
9699 s += 4;
9700 regno = KT0;
9701 }
9702 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
9703 {
9704 s += 4;
9705 regno = KT1;
9706 }
85b51719
TS
9707 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
9708 {
9709 s += 5;
9710 regno = ZERO;
9711 }
252b5132
RH
9712 else
9713 break;
9714 }
9715
9716 if (*s == ' ')
9717 ++s;
9718 if (args[1] != *s)
9719 {
9720 if (c == 'v' || c == 'w')
9721 {
9722 regno = mips16_to_32_reg_map[lastregno];
9723 s = s_reset;
f9419b05 9724 ++args;
252b5132
RH
9725 }
9726 }
9727
9728 switch (c)
9729 {
9730 case 'x':
9731 case 'y':
9732 case 'z':
9733 case 'v':
9734 case 'w':
9735 case 'Z':
9736 regno = mips32_to_16_reg_map[regno];
9737 break;
9738
9739 case '0':
9740 if (regno != 0)
9741 regno = ILLEGAL_REG;
9742 break;
9743
9744 case 'S':
9745 if (regno != SP)
9746 regno = ILLEGAL_REG;
9747 break;
9748
9749 case 'R':
9750 if (regno != RA)
9751 regno = ILLEGAL_REG;
9752 break;
9753
9754 case 'X':
9755 case 'Y':
9756 if (regno == AT && ! mips_opts.noat)
9757 as_warn (_("used $at without \".set noat\""));
9758 break;
9759
9760 default:
9761 internalError ();
9762 }
9763
9764 if (regno == ILLEGAL_REG)
9765 break;
9766
9767 switch (c)
9768 {
9769 case 'x':
9770 case 'v':
9771 ip->insn_opcode |= regno << MIPS16OP_SH_RX;
9772 break;
9773 case 'y':
9774 case 'w':
9775 ip->insn_opcode |= regno << MIPS16OP_SH_RY;
9776 break;
9777 case 'z':
9778 ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
9779 break;
9780 case 'Z':
9781 ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
9782 case '0':
9783 case 'S':
9784 case 'R':
9785 break;
9786 case 'X':
9787 ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
9788 break;
9789 case 'Y':
9790 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
9791 ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
9792 break;
9793 default:
9794 internalError ();
9795 }
9796
9797 lastregno = regno;
9798 continue;
9799
9800 case 'P':
9801 if (strncmp (s, "$pc", 3) == 0)
9802 {
9803 s += 3;
9804 continue;
9805 }
9806 break;
9807
9808 case '<':
9809 case '>':
9810 case '[':
9811 case ']':
9812 case '4':
9813 case '5':
9814 case 'H':
9815 case 'W':
9816 case 'D':
9817 case 'j':
9818 case '8':
9819 case 'V':
9820 case 'C':
9821 case 'U':
9822 case 'k':
9823 case 'K':
9824 if (s[0] == '%'
9825 && strncmp (s + 1, "gprel(", sizeof "gprel(" - 1) == 0)
9826 {
9827 /* This is %gprel(SYMBOL). We need to read SYMBOL,
9828 and generate the appropriate reloc. If the text
9829 inside %gprel is not a symbol name with an
9830 optional offset, then we generate a normal reloc
9831 and will probably fail later. */
9832 my_getExpression (&imm_expr, s + sizeof "%gprel" - 1);
9833 if (imm_expr.X_op == O_symbol)
9834 {
b34976b6 9835 mips16_ext = TRUE;
f6688943 9836 *imm_reloc = BFD_RELOC_MIPS16_GPREL;
252b5132 9837 s = expr_end;
b34976b6 9838 ip->use_extend = TRUE;
252b5132
RH
9839 ip->extend = 0;
9840 continue;
9841 }
9842 }
9843 else
9844 {
9845 /* Just pick up a normal expression. */
9846 my_getExpression (&imm_expr, s);
9847 }
9848
9849 if (imm_expr.X_op == O_register)
9850 {
9851 /* What we thought was an expression turned out to
9852 be a register. */
9853
9854 if (s[0] == '(' && args[1] == '(')
9855 {
9856 /* It looks like the expression was omitted
9857 before a register indirection, which means
9858 that the expression is implicitly zero. We
9859 still set up imm_expr, so that we handle
9860 explicit extensions correctly. */
9861 imm_expr.X_op = O_constant;
9862 imm_expr.X_add_number = 0;
f6688943 9863 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
9864 continue;
9865 }
9866
9867 break;
9868 }
9869
9870 /* We need to relax this instruction. */
f6688943 9871 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
9872 s = expr_end;
9873 continue;
9874
9875 case 'p':
9876 case 'q':
9877 case 'A':
9878 case 'B':
9879 case 'E':
9880 /* We use offset_reloc rather than imm_reloc for the PC
9881 relative operands. This lets macros with both
9882 immediate and address operands work correctly. */
9883 my_getExpression (&offset_expr, s);
9884
9885 if (offset_expr.X_op == O_register)
9886 break;
9887
9888 /* We need to relax this instruction. */
f6688943 9889 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
9890 s = expr_end;
9891 continue;
9892
9893 case '6': /* break code */
9894 my_getExpression (&imm_expr, s);
9895 check_absolute_expr (ip, &imm_expr);
9896 if ((unsigned long) imm_expr.X_add_number > 63)
9897 {
9898 as_warn (_("Invalid value for `%s' (%lu)"),
9899 ip->insn_mo->name,
9900 (unsigned long) imm_expr.X_add_number);
9901 imm_expr.X_add_number &= 0x3f;
9902 }
9903 ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
9904 imm_expr.X_op = O_absent;
9905 s = expr_end;
9906 continue;
9907
9908 case 'a': /* 26 bit address */
9909 my_getExpression (&offset_expr, s);
9910 s = expr_end;
f6688943 9911 *offset_reloc = BFD_RELOC_MIPS16_JMP;
252b5132
RH
9912 ip->insn_opcode <<= 16;
9913 continue;
9914
9915 case 'l': /* register list for entry macro */
9916 case 'L': /* register list for exit macro */
9917 {
9918 int mask;
9919
9920 if (c == 'l')
9921 mask = 0;
9922 else
9923 mask = 7 << 3;
9924 while (*s != '\0')
9925 {
9926 int freg, reg1, reg2;
9927
9928 while (*s == ' ' || *s == ',')
9929 ++s;
9930 if (*s != '$')
9931 {
9932 as_bad (_("can't parse register list"));
9933 break;
9934 }
9935 ++s;
9936 if (*s != 'f')
9937 freg = 0;
9938 else
9939 {
9940 freg = 1;
9941 ++s;
9942 }
9943 reg1 = 0;
3882b010 9944 while (ISDIGIT (*s))
252b5132
RH
9945 {
9946 reg1 *= 10;
9947 reg1 += *s - '0';
9948 ++s;
9949 }
9950 if (*s == ' ')
9951 ++s;
9952 if (*s != '-')
9953 reg2 = reg1;
9954 else
9955 {
9956 ++s;
9957 if (*s != '$')
9958 break;
9959 ++s;
9960 if (freg)
9961 {
9962 if (*s == 'f')
9963 ++s;
9964 else
9965 {
9966 as_bad (_("invalid register list"));
9967 break;
9968 }
9969 }
9970 reg2 = 0;
3882b010 9971 while (ISDIGIT (*s))
252b5132
RH
9972 {
9973 reg2 *= 10;
9974 reg2 += *s - '0';
9975 ++s;
9976 }
9977 }
9978 if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
9979 {
9980 mask &= ~ (7 << 3);
9981 mask |= 5 << 3;
9982 }
9983 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
9984 {
9985 mask &= ~ (7 << 3);
9986 mask |= 6 << 3;
9987 }
9988 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
9989 mask |= (reg2 - 3) << 3;
9990 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
9991 mask |= (reg2 - 15) << 1;
f9419b05 9992 else if (reg1 == RA && reg2 == RA)
252b5132
RH
9993 mask |= 1;
9994 else
9995 {
9996 as_bad (_("invalid register list"));
9997 break;
9998 }
9999 }
10000 /* The mask is filled in in the opcode table for the
10001 benefit of the disassembler. We remove it before
10002 applying the actual mask. */
10003 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
10004 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
10005 }
10006 continue;
10007
10008 case 'e': /* extend code */
10009 my_getExpression (&imm_expr, s);
10010 check_absolute_expr (ip, &imm_expr);
10011 if ((unsigned long) imm_expr.X_add_number > 0x7ff)
10012 {
10013 as_warn (_("Invalid value for `%s' (%lu)"),
10014 ip->insn_mo->name,
10015 (unsigned long) imm_expr.X_add_number);
10016 imm_expr.X_add_number &= 0x7ff;
10017 }
10018 ip->insn_opcode |= imm_expr.X_add_number;
10019 imm_expr.X_op = O_absent;
10020 s = expr_end;
10021 continue;
10022
10023 default:
10024 internalError ();
10025 }
10026 break;
10027 }
10028
10029 /* Args don't match. */
10030 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
10031 strcmp (insn->name, insn[1].name) == 0)
10032 {
10033 ++insn;
10034 s = argsstart;
10035 continue;
10036 }
10037
10038 insn_error = _("illegal operands");
10039
10040 return;
10041 }
10042}
10043
10044/* This structure holds information we know about a mips16 immediate
10045 argument type. */
10046
e972090a
NC
10047struct mips16_immed_operand
10048{
252b5132
RH
10049 /* The type code used in the argument string in the opcode table. */
10050 int type;
10051 /* The number of bits in the short form of the opcode. */
10052 int nbits;
10053 /* The number of bits in the extended form of the opcode. */
10054 int extbits;
10055 /* The amount by which the short form is shifted when it is used;
10056 for example, the sw instruction has a shift count of 2. */
10057 int shift;
10058 /* The amount by which the short form is shifted when it is stored
10059 into the instruction code. */
10060 int op_shift;
10061 /* Non-zero if the short form is unsigned. */
10062 int unsp;
10063 /* Non-zero if the extended form is unsigned. */
10064 int extu;
10065 /* Non-zero if the value is PC relative. */
10066 int pcrel;
10067};
10068
10069/* The mips16 immediate operand types. */
10070
10071static const struct mips16_immed_operand mips16_immed_operands[] =
10072{
10073 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
10074 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 },
10075 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
10076 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 },
10077 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
10078 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
10079 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
10080 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
10081 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
10082 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
10083 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
10084 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
10085 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
10086 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
10087 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
10088 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
10089 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
10090 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
10091 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
10092 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
10093 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
10094};
10095
10096#define MIPS16_NUM_IMMED \
10097 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
10098
10099/* Handle a mips16 instruction with an immediate value. This or's the
10100 small immediate value into *INSN. It sets *USE_EXTEND to indicate
10101 whether an extended value is needed; if one is needed, it sets
10102 *EXTEND to the value. The argument type is TYPE. The value is VAL.
10103 If SMALL is true, an unextended opcode was explicitly requested.
10104 If EXT is true, an extended opcode was explicitly requested. If
10105 WARN is true, warn if EXT does not match reality. */
10106
10107static void
17a2f251
TS
10108mips16_immed (char *file, unsigned int line, int type, offsetT val,
10109 bfd_boolean warn, bfd_boolean small, bfd_boolean ext,
10110 unsigned long *insn, bfd_boolean *use_extend,
10111 unsigned short *extend)
252b5132
RH
10112{
10113 register const struct mips16_immed_operand *op;
10114 int mintiny, maxtiny;
b34976b6 10115 bfd_boolean needext;
252b5132
RH
10116
10117 op = mips16_immed_operands;
10118 while (op->type != type)
10119 {
10120 ++op;
10121 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
10122 }
10123
10124 if (op->unsp)
10125 {
10126 if (type == '<' || type == '>' || type == '[' || type == ']')
10127 {
10128 mintiny = 1;
10129 maxtiny = 1 << op->nbits;
10130 }
10131 else
10132 {
10133 mintiny = 0;
10134 maxtiny = (1 << op->nbits) - 1;
10135 }
10136 }
10137 else
10138 {
10139 mintiny = - (1 << (op->nbits - 1));
10140 maxtiny = (1 << (op->nbits - 1)) - 1;
10141 }
10142
10143 /* Branch offsets have an implicit 0 in the lowest bit. */
10144 if (type == 'p' || type == 'q')
10145 val /= 2;
10146
10147 if ((val & ((1 << op->shift) - 1)) != 0
10148 || val < (mintiny << op->shift)
10149 || val > (maxtiny << op->shift))
b34976b6 10150 needext = TRUE;
252b5132 10151 else
b34976b6 10152 needext = FALSE;
252b5132
RH
10153
10154 if (warn && ext && ! needext)
beae10d5
KH
10155 as_warn_where (file, line,
10156 _("extended operand requested but not required"));
252b5132
RH
10157 if (small && needext)
10158 as_bad_where (file, line, _("invalid unextended operand value"));
10159
10160 if (small || (! ext && ! needext))
10161 {
10162 int insnval;
10163
b34976b6 10164 *use_extend = FALSE;
252b5132
RH
10165 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
10166 insnval <<= op->op_shift;
10167 *insn |= insnval;
10168 }
10169 else
10170 {
10171 long minext, maxext;
10172 int extval;
10173
10174 if (op->extu)
10175 {
10176 minext = 0;
10177 maxext = (1 << op->extbits) - 1;
10178 }
10179 else
10180 {
10181 minext = - (1 << (op->extbits - 1));
10182 maxext = (1 << (op->extbits - 1)) - 1;
10183 }
10184 if (val < minext || val > maxext)
10185 as_bad_where (file, line,
10186 _("operand value out of range for instruction"));
10187
b34976b6 10188 *use_extend = TRUE;
252b5132
RH
10189 if (op->extbits == 16)
10190 {
10191 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
10192 val &= 0x1f;
10193 }
10194 else if (op->extbits == 15)
10195 {
10196 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
10197 val &= 0xf;
10198 }
10199 else
10200 {
10201 extval = ((val & 0x1f) << 6) | (val & 0x20);
10202 val = 0;
10203 }
10204
10205 *extend = (unsigned short) extval;
10206 *insn |= val;
10207 }
10208}
10209\f
5e0116d5 10210static const struct percent_op_match
ad8d3bb3 10211{
5e0116d5
RS
10212 const char *str;
10213 bfd_reloc_code_real_type reloc;
ad8d3bb3
TS
10214} percent_op[] =
10215{
5e0116d5 10216 {"%lo", BFD_RELOC_LO16},
ad8d3bb3 10217#ifdef OBJ_ELF
5e0116d5
RS
10218 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
10219 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
10220 {"%call16", BFD_RELOC_MIPS_CALL16},
10221 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
10222 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
10223 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
10224 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
10225 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
10226 {"%got", BFD_RELOC_MIPS_GOT16},
10227 {"%gp_rel", BFD_RELOC_GPREL16},
10228 {"%half", BFD_RELOC_16},
10229 {"%highest", BFD_RELOC_MIPS_HIGHEST},
10230 {"%higher", BFD_RELOC_MIPS_HIGHER},
10231 {"%neg", BFD_RELOC_MIPS_SUB},
ad8d3bb3 10232#endif
5e0116d5 10233 {"%hi", BFD_RELOC_HI16_S}
ad8d3bb3
TS
10234};
10235
252b5132 10236
5e0116d5
RS
10237/* Return true if *STR points to a relocation operator. When returning true,
10238 move *STR over the operator and store its relocation code in *RELOC.
10239 Leave both *STR and *RELOC alone when returning false. */
10240
10241static bfd_boolean
17a2f251 10242parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
252b5132 10243{
5e0116d5 10244 size_t i;
76b3015f 10245
5e0116d5
RS
10246 for (i = 0; i < ARRAY_SIZE (percent_op); i++)
10247 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
394f9b3a 10248 {
5e0116d5
RS
10249 *str += strlen (percent_op[i].str);
10250 *reloc = percent_op[i].reloc;
394f9b3a 10251
5e0116d5
RS
10252 /* Check whether the output BFD supports this relocation.
10253 If not, issue an error and fall back on something safe. */
10254 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
394f9b3a 10255 {
5e0116d5
RS
10256 as_bad ("relocation %s isn't supported by the current ABI",
10257 percent_op[i].str);
10258 *reloc = BFD_RELOC_LO16;
394f9b3a 10259 }
5e0116d5 10260 return TRUE;
394f9b3a 10261 }
5e0116d5 10262 return FALSE;
394f9b3a 10263}
ad8d3bb3 10264
ad8d3bb3 10265
5e0116d5
RS
10266/* Parse string STR as a 16-bit relocatable operand. Store the
10267 expression in *EP and the relocations in the array starting
10268 at RELOC. Return the number of relocation operators used.
ad8d3bb3 10269
5e0116d5
RS
10270 On exit, EXPR_END points to the first character after the expression.
10271 If no relocation operators are used, RELOC[0] is set to BFD_RELOC_LO16. */
ad8d3bb3 10272
5e0116d5 10273static size_t
17a2f251
TS
10274my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
10275 char *str)
ad8d3bb3 10276{
5e0116d5
RS
10277 bfd_reloc_code_real_type reversed_reloc[3];
10278 size_t reloc_index, i;
09b8f35a
RS
10279 int crux_depth, str_depth;
10280 char *crux;
5e0116d5
RS
10281
10282 /* Search for the start of the main expression, recoding relocations
09b8f35a
RS
10283 in REVERSED_RELOC. End the loop with CRUX pointing to the start
10284 of the main expression and with CRUX_DEPTH containing the number
10285 of open brackets at that point. */
10286 reloc_index = -1;
10287 str_depth = 0;
10288 do
fb1b3232 10289 {
09b8f35a
RS
10290 reloc_index++;
10291 crux = str;
10292 crux_depth = str_depth;
10293
10294 /* Skip over whitespace and brackets, keeping count of the number
10295 of brackets. */
10296 while (*str == ' ' || *str == '\t' || *str == '(')
10297 if (*str++ == '(')
10298 str_depth++;
5e0116d5 10299 }
09b8f35a
RS
10300 while (*str == '%'
10301 && reloc_index < (HAVE_NEWABI ? 3 : 1)
10302 && parse_relocation (&str, &reversed_reloc[reloc_index]));
ad8d3bb3 10303
09b8f35a 10304 my_getExpression (ep, crux);
5e0116d5 10305 str = expr_end;
394f9b3a 10306
5e0116d5 10307 /* Match every open bracket. */
09b8f35a 10308 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
5e0116d5 10309 if (*str++ == ')')
09b8f35a 10310 crux_depth--;
394f9b3a 10311
09b8f35a 10312 if (crux_depth > 0)
5e0116d5 10313 as_bad ("unclosed '('");
394f9b3a 10314
5e0116d5 10315 expr_end = str;
252b5132 10316
64bdfcaf
RS
10317 if (reloc_index == 0)
10318 reloc[0] = BFD_RELOC_LO16;
10319 else
10320 {
10321 prev_reloc_op_frag = frag_now;
10322 for (i = 0; i < reloc_index; i++)
10323 reloc[i] = reversed_reloc[reloc_index - 1 - i];
10324 }
fb1b3232 10325
5e0116d5 10326 return reloc_index;
252b5132
RH
10327}
10328
10329static void
17a2f251 10330my_getExpression (expressionS *ep, char *str)
252b5132
RH
10331{
10332 char *save_in;
98aa84af 10333 valueT val;
252b5132
RH
10334
10335 save_in = input_line_pointer;
10336 input_line_pointer = str;
10337 expression (ep);
10338 expr_end = input_line_pointer;
10339 input_line_pointer = save_in;
10340
10341 /* If we are in mips16 mode, and this is an expression based on `.',
10342 then we bump the value of the symbol by 1 since that is how other
10343 text symbols are handled. We don't bother to handle complex
10344 expressions, just `.' plus or minus a constant. */
10345 if (mips_opts.mips16
10346 && ep->X_op == O_symbol
10347 && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
10348 && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
49309057
ILT
10349 && symbol_get_frag (ep->X_add_symbol) == frag_now
10350 && symbol_constant_p (ep->X_add_symbol)
98aa84af
AM
10351 && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
10352 S_SET_VALUE (ep->X_add_symbol, val + 1);
252b5132
RH
10353}
10354
10355/* Turn a string in input_line_pointer into a floating point constant
bc0d738a
NC
10356 of type TYPE, and store the appropriate bytes in *LITP. The number
10357 of LITTLENUMS emitted is stored in *SIZEP. An error message is
252b5132
RH
10358 returned, or NULL on OK. */
10359
10360char *
17a2f251 10361md_atof (int type, char *litP, int *sizeP)
252b5132
RH
10362{
10363 int prec;
10364 LITTLENUM_TYPE words[4];
10365 char *t;
10366 int i;
10367
10368 switch (type)
10369 {
10370 case 'f':
10371 prec = 2;
10372 break;
10373
10374 case 'd':
10375 prec = 4;
10376 break;
10377
10378 default:
10379 *sizeP = 0;
10380 return _("bad call to md_atof");
10381 }
10382
10383 t = atof_ieee (input_line_pointer, type, words);
10384 if (t)
10385 input_line_pointer = t;
10386
10387 *sizeP = prec * 2;
10388
10389 if (! target_big_endian)
10390 {
10391 for (i = prec - 1; i >= 0; i--)
10392 {
17a2f251 10393 md_number_to_chars (litP, words[i], 2);
252b5132
RH
10394 litP += 2;
10395 }
10396 }
10397 else
10398 {
10399 for (i = 0; i < prec; i++)
10400 {
17a2f251 10401 md_number_to_chars (litP, words[i], 2);
252b5132
RH
10402 litP += 2;
10403 }
10404 }
bdaaa2e1 10405
252b5132
RH
10406 return NULL;
10407}
10408
10409void
17a2f251 10410md_number_to_chars (char *buf, valueT val, int n)
252b5132
RH
10411{
10412 if (target_big_endian)
10413 number_to_chars_bigendian (buf, val, n);
10414 else
10415 number_to_chars_littleendian (buf, val, n);
10416}
10417\f
ae948b86 10418#ifdef OBJ_ELF
e013f690
TS
10419static int support_64bit_objects(void)
10420{
10421 const char **list, **l;
aa3d8fdf 10422 int yes;
e013f690
TS
10423
10424 list = bfd_target_list ();
10425 for (l = list; *l != NULL; l++)
10426#ifdef TE_TMIPS
10427 /* This is traditional mips */
10428 if (strcmp (*l, "elf64-tradbigmips") == 0
10429 || strcmp (*l, "elf64-tradlittlemips") == 0)
10430#else
10431 if (strcmp (*l, "elf64-bigmips") == 0
10432 || strcmp (*l, "elf64-littlemips") == 0)
10433#endif
10434 break;
aa3d8fdf 10435 yes = (*l != NULL);
e013f690 10436 free (list);
aa3d8fdf 10437 return yes;
e013f690 10438}
ae948b86 10439#endif /* OBJ_ELF */
e013f690 10440
5a38dc70 10441const char *md_shortopts = "nO::g::G:";
252b5132 10442
e972090a
NC
10443struct option md_longopts[] =
10444{
f9b4148d
CD
10445 /* Options which specify architecture. */
10446#define OPTION_ARCH_BASE (OPTION_MD_BASE)
10447#define OPTION_MARCH (OPTION_ARCH_BASE + 0)
10448 {"march", required_argument, NULL, OPTION_MARCH},
10449#define OPTION_MTUNE (OPTION_ARCH_BASE + 1)
10450 {"mtune", required_argument, NULL, OPTION_MTUNE},
10451#define OPTION_MIPS1 (OPTION_ARCH_BASE + 2)
252b5132
RH
10452 {"mips0", no_argument, NULL, OPTION_MIPS1},
10453 {"mips1", no_argument, NULL, OPTION_MIPS1},
f9b4148d 10454#define OPTION_MIPS2 (OPTION_ARCH_BASE + 3)
252b5132 10455 {"mips2", no_argument, NULL, OPTION_MIPS2},
f9b4148d 10456#define OPTION_MIPS3 (OPTION_ARCH_BASE + 4)
252b5132 10457 {"mips3", no_argument, NULL, OPTION_MIPS3},
f9b4148d 10458#define OPTION_MIPS4 (OPTION_ARCH_BASE + 5)
252b5132 10459 {"mips4", no_argument, NULL, OPTION_MIPS4},
f9b4148d 10460#define OPTION_MIPS5 (OPTION_ARCH_BASE + 6)
ae948b86 10461 {"mips5", no_argument, NULL, OPTION_MIPS5},
f9b4148d 10462#define OPTION_MIPS32 (OPTION_ARCH_BASE + 7)
ae948b86 10463 {"mips32", no_argument, NULL, OPTION_MIPS32},
f9b4148d 10464#define OPTION_MIPS64 (OPTION_ARCH_BASE + 8)
ae948b86 10465 {"mips64", no_argument, NULL, OPTION_MIPS64},
f9b4148d
CD
10466#define OPTION_MIPS32R2 (OPTION_ARCH_BASE + 9)
10467 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
5f74bc13
CD
10468#define OPTION_MIPS64R2 (OPTION_ARCH_BASE + 10)
10469 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
f9b4148d
CD
10470
10471 /* Options which specify Application Specific Extensions (ASEs). */
5f74bc13 10472#define OPTION_ASE_BASE (OPTION_ARCH_BASE + 11)
f9b4148d
CD
10473#define OPTION_MIPS16 (OPTION_ASE_BASE + 0)
10474 {"mips16", no_argument, NULL, OPTION_MIPS16},
10475#define OPTION_NO_MIPS16 (OPTION_ASE_BASE + 1)
10476 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
10477#define OPTION_MIPS3D (OPTION_ASE_BASE + 2)
10478 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
10479#define OPTION_NO_MIPS3D (OPTION_ASE_BASE + 3)
10480 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
10481#define OPTION_MDMX (OPTION_ASE_BASE + 4)
10482 {"mdmx", no_argument, NULL, OPTION_MDMX},
10483#define OPTION_NO_MDMX (OPTION_ASE_BASE + 5)
10484 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
10485
10486 /* Old-style architecture options. Don't add more of these. */
10487#define OPTION_COMPAT_ARCH_BASE (OPTION_ASE_BASE + 6)
10488#define OPTION_M4650 (OPTION_COMPAT_ARCH_BASE + 0)
10489 {"m4650", no_argument, NULL, OPTION_M4650},
10490#define OPTION_NO_M4650 (OPTION_COMPAT_ARCH_BASE + 1)
10491 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
10492#define OPTION_M4010 (OPTION_COMPAT_ARCH_BASE + 2)
10493 {"m4010", no_argument, NULL, OPTION_M4010},
10494#define OPTION_NO_M4010 (OPTION_COMPAT_ARCH_BASE + 3)
10495 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
10496#define OPTION_M4100 (OPTION_COMPAT_ARCH_BASE + 4)
10497 {"m4100", no_argument, NULL, OPTION_M4100},
10498#define OPTION_NO_M4100 (OPTION_COMPAT_ARCH_BASE + 5)
10499 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
10500#define OPTION_M3900 (OPTION_COMPAT_ARCH_BASE + 6)
10501 {"m3900", no_argument, NULL, OPTION_M3900},
10502#define OPTION_NO_M3900 (OPTION_COMPAT_ARCH_BASE + 7)
10503 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
10504
10505 /* Options which enable bug fixes. */
10506#define OPTION_FIX_BASE (OPTION_COMPAT_ARCH_BASE + 8)
10507#define OPTION_M7000_HILO_FIX (OPTION_FIX_BASE + 0)
10508 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
10509#define OPTION_MNO_7000_HILO_FIX (OPTION_FIX_BASE + 1)
10510 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10511 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10512#define OPTION_FIX_VR4122 (OPTION_FIX_BASE + 2)
10513#define OPTION_NO_FIX_VR4122 (OPTION_FIX_BASE + 3)
10514 {"mfix-vr4122-bugs", no_argument, NULL, OPTION_FIX_VR4122},
10515 {"no-mfix-vr4122-bugs", no_argument, NULL, OPTION_NO_FIX_VR4122},
10516
10517 /* Miscellaneous options. */
10518#define OPTION_MISC_BASE (OPTION_FIX_BASE + 4)
10519#define OPTION_MEMBEDDED_PIC (OPTION_MISC_BASE + 0)
252b5132 10520 {"membedded-pic", no_argument, NULL, OPTION_MEMBEDDED_PIC},
f9b4148d 10521#define OPTION_TRAP (OPTION_MISC_BASE + 1)
252b5132
RH
10522 {"trap", no_argument, NULL, OPTION_TRAP},
10523 {"no-break", no_argument, NULL, OPTION_TRAP},
f9b4148d 10524#define OPTION_BREAK (OPTION_MISC_BASE + 2)
252b5132
RH
10525 {"break", no_argument, NULL, OPTION_BREAK},
10526 {"no-trap", no_argument, NULL, OPTION_BREAK},
f9b4148d 10527#define OPTION_EB (OPTION_MISC_BASE + 3)
252b5132 10528 {"EB", no_argument, NULL, OPTION_EB},
f9b4148d 10529#define OPTION_EL (OPTION_MISC_BASE + 4)
252b5132 10530 {"EL", no_argument, NULL, OPTION_EL},
f9b4148d 10531#define OPTION_FP32 (OPTION_MISC_BASE + 5)
ae948b86 10532 {"mfp32", no_argument, NULL, OPTION_FP32},
f9b4148d 10533#define OPTION_GP32 (OPTION_MISC_BASE + 6)
c97ef257 10534 {"mgp32", no_argument, NULL, OPTION_GP32},
f9b4148d 10535#define OPTION_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 7)
119d663a 10536 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
f9b4148d 10537#define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 8)
119d663a 10538 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
f9b4148d 10539#define OPTION_FP64 (OPTION_MISC_BASE + 9)
316f5878 10540 {"mfp64", no_argument, NULL, OPTION_FP64},
f9b4148d 10541#define OPTION_GP64 (OPTION_MISC_BASE + 10)
ae948b86 10542 {"mgp64", no_argument, NULL, OPTION_GP64},
f9b4148d
CD
10543#define OPTION_RELAX_BRANCH (OPTION_MISC_BASE + 11)
10544#define OPTION_NO_RELAX_BRANCH (OPTION_MISC_BASE + 12)
4a6a3df4
AO
10545 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
10546 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
f9b4148d
CD
10547
10548 /* ELF-specific options. */
156c2f8b 10549#ifdef OBJ_ELF
f9b4148d 10550#define OPTION_ELF_BASE (OPTION_MISC_BASE + 13)
156c2f8b 10551#define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
156c2f8b
NC
10552 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
10553 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
ae948b86 10554#define OPTION_NON_SHARED (OPTION_ELF_BASE + 1)
156c2f8b 10555 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
ae948b86 10556#define OPTION_XGOT (OPTION_ELF_BASE + 2)
156c2f8b 10557 {"xgot", no_argument, NULL, OPTION_XGOT},
ae948b86
TS
10558#define OPTION_MABI (OPTION_ELF_BASE + 3)
10559 {"mabi", required_argument, NULL, OPTION_MABI},
10560#define OPTION_32 (OPTION_ELF_BASE + 4)
156c2f8b 10561 {"32", no_argument, NULL, OPTION_32},
ae948b86 10562#define OPTION_N32 (OPTION_ELF_BASE + 5)
e013f690 10563 {"n32", no_argument, NULL, OPTION_N32},
ae948b86 10564#define OPTION_64 (OPTION_ELF_BASE + 6)
156c2f8b 10565 {"64", no_argument, NULL, OPTION_64},
ecb4347a
DJ
10566#define OPTION_MDEBUG (OPTION_ELF_BASE + 7)
10567 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
10568#define OPTION_NO_MDEBUG (OPTION_ELF_BASE + 8)
10569 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
dcd410fe
RO
10570#define OPTION_PDR (OPTION_ELF_BASE + 9)
10571 {"mpdr", no_argument, NULL, OPTION_PDR},
10572#define OPTION_NO_PDR (OPTION_ELF_BASE + 10)
10573 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
ae948b86 10574#endif /* OBJ_ELF */
f9b4148d 10575
252b5132
RH
10576 {NULL, no_argument, NULL, 0}
10577};
156c2f8b 10578size_t md_longopts_size = sizeof (md_longopts);
252b5132 10579
316f5878
RS
10580/* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
10581 NEW_VALUE. Warn if another value was already specified. Note:
10582 we have to defer parsing the -march and -mtune arguments in order
10583 to handle 'from-abi' correctly, since the ABI might be specified
10584 in a later argument. */
10585
10586static void
17a2f251 10587mips_set_option_string (const char **string_ptr, const char *new_value)
316f5878
RS
10588{
10589 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
10590 as_warn (_("A different %s was already specified, is now %s"),
10591 string_ptr == &mips_arch_string ? "-march" : "-mtune",
10592 new_value);
10593
10594 *string_ptr = new_value;
10595}
10596
252b5132 10597int
17a2f251 10598md_parse_option (int c, char *arg)
252b5132
RH
10599{
10600 switch (c)
10601 {
119d663a
NC
10602 case OPTION_CONSTRUCT_FLOATS:
10603 mips_disable_float_construction = 0;
10604 break;
bdaaa2e1 10605
119d663a
NC
10606 case OPTION_NO_CONSTRUCT_FLOATS:
10607 mips_disable_float_construction = 1;
10608 break;
bdaaa2e1 10609
252b5132
RH
10610 case OPTION_TRAP:
10611 mips_trap = 1;
10612 break;
10613
10614 case OPTION_BREAK:
10615 mips_trap = 0;
10616 break;
10617
10618 case OPTION_EB:
10619 target_big_endian = 1;
10620 break;
10621
10622 case OPTION_EL:
10623 target_big_endian = 0;
10624 break;
10625
39c0a331
L
10626 case 'n':
10627 warn_nops = 1;
10628 break;
10629
252b5132
RH
10630 case 'O':
10631 if (arg && arg[1] == '0')
10632 mips_optimize = 1;
10633 else
10634 mips_optimize = 2;
10635 break;
10636
10637 case 'g':
10638 if (arg == NULL)
10639 mips_debug = 2;
10640 else
10641 mips_debug = atoi (arg);
10642 /* When the MIPS assembler sees -g or -g2, it does not do
10643 optimizations which limit full symbolic debugging. We take
10644 that to be equivalent to -O0. */
10645 if (mips_debug == 2)
10646 mips_optimize = 1;
10647 break;
10648
10649 case OPTION_MIPS1:
316f5878 10650 file_mips_isa = ISA_MIPS1;
252b5132
RH
10651 break;
10652
10653 case OPTION_MIPS2:
316f5878 10654 file_mips_isa = ISA_MIPS2;
252b5132
RH
10655 break;
10656
10657 case OPTION_MIPS3:
316f5878 10658 file_mips_isa = ISA_MIPS3;
252b5132
RH
10659 break;
10660
10661 case OPTION_MIPS4:
316f5878 10662 file_mips_isa = ISA_MIPS4;
e7af610e
NC
10663 break;
10664
84ea6cf2 10665 case OPTION_MIPS5:
316f5878 10666 file_mips_isa = ISA_MIPS5;
84ea6cf2
NC
10667 break;
10668
e7af610e 10669 case OPTION_MIPS32:
316f5878 10670 file_mips_isa = ISA_MIPS32;
252b5132
RH
10671 break;
10672
af7ee8bf
CD
10673 case OPTION_MIPS32R2:
10674 file_mips_isa = ISA_MIPS32R2;
10675 break;
10676
5f74bc13
CD
10677 case OPTION_MIPS64R2:
10678 file_mips_isa = ISA_MIPS64R2;
10679 break;
10680
84ea6cf2 10681 case OPTION_MIPS64:
316f5878 10682 file_mips_isa = ISA_MIPS64;
84ea6cf2
NC
10683 break;
10684
ec68c924 10685 case OPTION_MTUNE:
316f5878
RS
10686 mips_set_option_string (&mips_tune_string, arg);
10687 break;
ec68c924 10688
316f5878
RS
10689 case OPTION_MARCH:
10690 mips_set_option_string (&mips_arch_string, arg);
252b5132
RH
10691 break;
10692
10693 case OPTION_M4650:
316f5878
RS
10694 mips_set_option_string (&mips_arch_string, "4650");
10695 mips_set_option_string (&mips_tune_string, "4650");
252b5132
RH
10696 break;
10697
10698 case OPTION_NO_M4650:
10699 break;
10700
10701 case OPTION_M4010:
316f5878
RS
10702 mips_set_option_string (&mips_arch_string, "4010");
10703 mips_set_option_string (&mips_tune_string, "4010");
252b5132
RH
10704 break;
10705
10706 case OPTION_NO_M4010:
10707 break;
10708
10709 case OPTION_M4100:
316f5878
RS
10710 mips_set_option_string (&mips_arch_string, "4100");
10711 mips_set_option_string (&mips_tune_string, "4100");
252b5132
RH
10712 break;
10713
10714 case OPTION_NO_M4100:
10715 break;
10716
252b5132 10717 case OPTION_M3900:
316f5878
RS
10718 mips_set_option_string (&mips_arch_string, "3900");
10719 mips_set_option_string (&mips_tune_string, "3900");
252b5132 10720 break;
bdaaa2e1 10721
252b5132
RH
10722 case OPTION_NO_M3900:
10723 break;
10724
deec1734
CD
10725 case OPTION_MDMX:
10726 mips_opts.ase_mdmx = 1;
10727 break;
10728
10729 case OPTION_NO_MDMX:
10730 mips_opts.ase_mdmx = 0;
10731 break;
10732
252b5132
RH
10733 case OPTION_MIPS16:
10734 mips_opts.mips16 = 1;
b34976b6 10735 mips_no_prev_insn (FALSE);
252b5132
RH
10736 break;
10737
10738 case OPTION_NO_MIPS16:
10739 mips_opts.mips16 = 0;
b34976b6 10740 mips_no_prev_insn (FALSE);
252b5132
RH
10741 break;
10742
1f25f5d3
CD
10743 case OPTION_MIPS3D:
10744 mips_opts.ase_mips3d = 1;
10745 break;
10746
10747 case OPTION_NO_MIPS3D:
10748 mips_opts.ase_mips3d = 0;
10749 break;
10750
252b5132
RH
10751 case OPTION_MEMBEDDED_PIC:
10752 mips_pic = EMBEDDED_PIC;
10753 if (USE_GLOBAL_POINTER_OPT && g_switch_seen)
10754 {
10755 as_bad (_("-G may not be used with embedded PIC code"));
10756 return 0;
10757 }
10758 g_switch_value = 0x7fffffff;
10759 break;
10760
60b63b72
RS
10761 case OPTION_FIX_VR4122:
10762 mips_fix_4122_bugs = 1;
10763 break;
10764
10765 case OPTION_NO_FIX_VR4122:
10766 mips_fix_4122_bugs = 0;
10767 break;
10768
4a6a3df4
AO
10769 case OPTION_RELAX_BRANCH:
10770 mips_relax_branch = 1;
10771 break;
10772
10773 case OPTION_NO_RELAX_BRANCH:
10774 mips_relax_branch = 0;
10775 break;
10776
0f074f60 10777#ifdef OBJ_ELF
252b5132
RH
10778 /* When generating ELF code, we permit -KPIC and -call_shared to
10779 select SVR4_PIC, and -non_shared to select no PIC. This is
10780 intended to be compatible with Irix 5. */
10781 case OPTION_CALL_SHARED:
10782 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10783 {
10784 as_bad (_("-call_shared is supported only for ELF format"));
10785 return 0;
10786 }
10787 mips_pic = SVR4_PIC;
143d77c5 10788 mips_abicalls = TRUE;
252b5132
RH
10789 if (g_switch_seen && g_switch_value != 0)
10790 {
10791 as_bad (_("-G may not be used with SVR4 PIC code"));
10792 return 0;
10793 }
10794 g_switch_value = 0;
10795 break;
10796
10797 case OPTION_NON_SHARED:
10798 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10799 {
10800 as_bad (_("-non_shared is supported only for ELF format"));
10801 return 0;
10802 }
10803 mips_pic = NO_PIC;
143d77c5 10804 mips_abicalls = FALSE;
252b5132
RH
10805 break;
10806
10807 /* The -xgot option tells the assembler to use 32 offsets when
10808 accessing the got in SVR4_PIC mode. It is for Irix
10809 compatibility. */
10810 case OPTION_XGOT:
10811 mips_big_got = 1;
10812 break;
0f074f60 10813#endif /* OBJ_ELF */
252b5132
RH
10814
10815 case 'G':
10816 if (! USE_GLOBAL_POINTER_OPT)
10817 {
10818 as_bad (_("-G is not supported for this configuration"));
10819 return 0;
10820 }
10821 else if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
10822 {
10823 as_bad (_("-G may not be used with SVR4 or embedded PIC code"));
10824 return 0;
10825 }
10826 else
10827 g_switch_value = atoi (arg);
10828 g_switch_seen = 1;
10829 break;
10830
0f074f60 10831#ifdef OBJ_ELF
34ba82a8
TS
10832 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
10833 and -mabi=64. */
252b5132 10834 case OPTION_32:
34ba82a8
TS
10835 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10836 {
10837 as_bad (_("-32 is supported for ELF format only"));
10838 return 0;
10839 }
316f5878 10840 mips_abi = O32_ABI;
252b5132
RH
10841 break;
10842
e013f690 10843 case OPTION_N32:
34ba82a8
TS
10844 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10845 {
10846 as_bad (_("-n32 is supported for ELF format only"));
10847 return 0;
10848 }
316f5878 10849 mips_abi = N32_ABI;
e013f690 10850 break;
252b5132 10851
e013f690 10852 case OPTION_64:
34ba82a8
TS
10853 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10854 {
10855 as_bad (_("-64 is supported for ELF format only"));
10856 return 0;
10857 }
316f5878 10858 mips_abi = N64_ABI;
e013f690
TS
10859 if (! support_64bit_objects())
10860 as_fatal (_("No compiled in support for 64 bit object file format"));
252b5132 10861 break;
ae948b86 10862#endif /* OBJ_ELF */
252b5132 10863
c97ef257 10864 case OPTION_GP32:
a325df1d 10865 file_mips_gp32 = 1;
c97ef257
AH
10866 break;
10867
10868 case OPTION_GP64:
a325df1d 10869 file_mips_gp32 = 0;
c97ef257 10870 break;
252b5132 10871
ca4e0257 10872 case OPTION_FP32:
a325df1d 10873 file_mips_fp32 = 1;
316f5878
RS
10874 break;
10875
10876 case OPTION_FP64:
10877 file_mips_fp32 = 0;
ca4e0257
RS
10878 break;
10879
ae948b86 10880#ifdef OBJ_ELF
252b5132 10881 case OPTION_MABI:
34ba82a8
TS
10882 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10883 {
10884 as_bad (_("-mabi is supported for ELF format only"));
10885 return 0;
10886 }
e013f690 10887 if (strcmp (arg, "32") == 0)
316f5878 10888 mips_abi = O32_ABI;
e013f690 10889 else if (strcmp (arg, "o64") == 0)
316f5878 10890 mips_abi = O64_ABI;
e013f690 10891 else if (strcmp (arg, "n32") == 0)
316f5878 10892 mips_abi = N32_ABI;
e013f690
TS
10893 else if (strcmp (arg, "64") == 0)
10894 {
316f5878 10895 mips_abi = N64_ABI;
e013f690
TS
10896 if (! support_64bit_objects())
10897 as_fatal (_("No compiled in support for 64 bit object file "
10898 "format"));
10899 }
10900 else if (strcmp (arg, "eabi") == 0)
316f5878 10901 mips_abi = EABI_ABI;
e013f690 10902 else
da0e507f
TS
10903 {
10904 as_fatal (_("invalid abi -mabi=%s"), arg);
10905 return 0;
10906 }
252b5132 10907 break;
e013f690 10908#endif /* OBJ_ELF */
252b5132 10909
6b76fefe 10910 case OPTION_M7000_HILO_FIX:
b34976b6 10911 mips_7000_hilo_fix = TRUE;
6b76fefe
CM
10912 break;
10913
9ee72ff1 10914 case OPTION_MNO_7000_HILO_FIX:
b34976b6 10915 mips_7000_hilo_fix = FALSE;
6b76fefe
CM
10916 break;
10917
ecb4347a
DJ
10918#ifdef OBJ_ELF
10919 case OPTION_MDEBUG:
b34976b6 10920 mips_flag_mdebug = TRUE;
ecb4347a
DJ
10921 break;
10922
10923 case OPTION_NO_MDEBUG:
b34976b6 10924 mips_flag_mdebug = FALSE;
ecb4347a 10925 break;
dcd410fe
RO
10926
10927 case OPTION_PDR:
10928 mips_flag_pdr = TRUE;
10929 break;
10930
10931 case OPTION_NO_PDR:
10932 mips_flag_pdr = FALSE;
10933 break;
ecb4347a
DJ
10934#endif /* OBJ_ELF */
10935
252b5132
RH
10936 default:
10937 return 0;
10938 }
10939
10940 return 1;
10941}
316f5878
RS
10942\f
10943/* Set up globals to generate code for the ISA or processor
10944 described by INFO. */
252b5132 10945
252b5132 10946static void
17a2f251 10947mips_set_architecture (const struct mips_cpu_info *info)
252b5132 10948{
316f5878 10949 if (info != 0)
252b5132 10950 {
fef14a42
TS
10951 file_mips_arch = info->cpu;
10952 mips_opts.arch = info->cpu;
316f5878 10953 mips_opts.isa = info->isa;
252b5132 10954 }
252b5132
RH
10955}
10956
252b5132 10957
316f5878 10958/* Likewise for tuning. */
252b5132 10959
316f5878 10960static void
17a2f251 10961mips_set_tune (const struct mips_cpu_info *info)
316f5878
RS
10962{
10963 if (info != 0)
fef14a42 10964 mips_tune = info->cpu;
316f5878 10965}
80cc45a5 10966
34ba82a8 10967
252b5132 10968void
17a2f251 10969mips_after_parse_args (void)
e9670677 10970{
fef14a42
TS
10971 const struct mips_cpu_info *arch_info = 0;
10972 const struct mips_cpu_info *tune_info = 0;
10973
e9670677
MR
10974 /* GP relative stuff not working for PE */
10975 if (strncmp (TARGET_OS, "pe", 2) == 0
10976 && g_switch_value != 0)
10977 {
10978 if (g_switch_seen)
10979 as_bad (_("-G not supported in this configuration."));
10980 g_switch_value = 0;
10981 }
10982
cac012d6
AO
10983 if (mips_abi == NO_ABI)
10984 mips_abi = MIPS_DEFAULT_ABI;
10985
22923709
RS
10986 /* The following code determines the architecture and register size.
10987 Similar code was added to GCC 3.3 (see override_options() in
10988 config/mips/mips.c). The GAS and GCC code should be kept in sync
10989 as much as possible. */
e9670677 10990
316f5878 10991 if (mips_arch_string != 0)
fef14a42 10992 arch_info = mips_parse_cpu ("-march", mips_arch_string);
e9670677 10993
316f5878 10994 if (file_mips_isa != ISA_UNKNOWN)
e9670677 10995 {
316f5878 10996 /* Handle -mipsN. At this point, file_mips_isa contains the
fef14a42 10997 ISA level specified by -mipsN, while arch_info->isa contains
316f5878 10998 the -march selection (if any). */
fef14a42 10999 if (arch_info != 0)
e9670677 11000 {
316f5878
RS
11001 /* -march takes precedence over -mipsN, since it is more descriptive.
11002 There's no harm in specifying both as long as the ISA levels
11003 are the same. */
fef14a42 11004 if (file_mips_isa != arch_info->isa)
316f5878
RS
11005 as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
11006 mips_cpu_info_from_isa (file_mips_isa)->name,
fef14a42 11007 mips_cpu_info_from_isa (arch_info->isa)->name);
e9670677 11008 }
316f5878 11009 else
fef14a42 11010 arch_info = mips_cpu_info_from_isa (file_mips_isa);
e9670677
MR
11011 }
11012
fef14a42
TS
11013 if (arch_info == 0)
11014 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
e9670677 11015
fef14a42 11016 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
316f5878 11017 as_bad ("-march=%s is not compatible with the selected ABI",
fef14a42
TS
11018 arch_info->name);
11019
11020 mips_set_architecture (arch_info);
11021
11022 /* Optimize for file_mips_arch, unless -mtune selects a different processor. */
11023 if (mips_tune_string != 0)
11024 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
e9670677 11025
fef14a42
TS
11026 if (tune_info == 0)
11027 mips_set_tune (arch_info);
11028 else
11029 mips_set_tune (tune_info);
e9670677 11030
316f5878 11031 if (file_mips_gp32 >= 0)
e9670677 11032 {
316f5878
RS
11033 /* The user specified the size of the integer registers. Make sure
11034 it agrees with the ABI and ISA. */
11035 if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
11036 as_bad (_("-mgp64 used with a 32-bit processor"));
11037 else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
11038 as_bad (_("-mgp32 used with a 64-bit ABI"));
11039 else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
11040 as_bad (_("-mgp64 used with a 32-bit ABI"));
e9670677
MR
11041 }
11042 else
11043 {
316f5878
RS
11044 /* Infer the integer register size from the ABI and processor.
11045 Restrict ourselves to 32-bit registers if that's all the
11046 processor has, or if the ABI cannot handle 64-bit registers. */
11047 file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
11048 || !ISA_HAS_64BIT_REGS (mips_opts.isa));
e9670677
MR
11049 }
11050
316f5878
RS
11051 /* ??? GAS treats single-float processors as though they had 64-bit
11052 float registers (although it complains when double-precision
11053 instructions are used). As things stand, saying they have 32-bit
11054 registers would lead to spurious "register must be even" messages.
11055 So here we assume float registers are always the same size as
11056 integer ones, unless the user says otherwise. */
11057 if (file_mips_fp32 < 0)
11058 file_mips_fp32 = file_mips_gp32;
e9670677 11059
316f5878 11060 /* End of GCC-shared inference code. */
e9670677 11061
17a2f251
TS
11062 /* This flag is set when we have a 64-bit capable CPU but use only
11063 32-bit wide registers. Note that EABI does not use it. */
11064 if (ISA_HAS_64BIT_REGS (mips_opts.isa)
11065 && ((mips_abi == NO_ABI && file_mips_gp32 == 1)
11066 || mips_abi == O32_ABI))
316f5878 11067 mips_32bitmode = 1;
e9670677
MR
11068
11069 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
11070 as_bad (_("trap exception not supported at ISA 1"));
11071
e9670677
MR
11072 /* If the selected architecture includes support for ASEs, enable
11073 generation of code for them. */
a4672219 11074 if (mips_opts.mips16 == -1)
fef14a42 11075 mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_arch)) ? 1 : 0;
ffdefa66 11076 if (mips_opts.ase_mips3d == -1)
fef14a42 11077 mips_opts.ase_mips3d = (CPU_HAS_MIPS3D (file_mips_arch)) ? 1 : 0;
ffdefa66 11078 if (mips_opts.ase_mdmx == -1)
fef14a42 11079 mips_opts.ase_mdmx = (CPU_HAS_MDMX (file_mips_arch)) ? 1 : 0;
e9670677 11080
e9670677 11081 file_mips_isa = mips_opts.isa;
a4672219 11082 file_ase_mips16 = mips_opts.mips16;
e9670677
MR
11083 file_ase_mips3d = mips_opts.ase_mips3d;
11084 file_ase_mdmx = mips_opts.ase_mdmx;
11085 mips_opts.gp32 = file_mips_gp32;
11086 mips_opts.fp32 = file_mips_fp32;
11087
ecb4347a
DJ
11088 if (mips_flag_mdebug < 0)
11089 {
11090#ifdef OBJ_MAYBE_ECOFF
11091 if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
11092 mips_flag_mdebug = 1;
11093 else
11094#endif /* OBJ_MAYBE_ECOFF */
11095 mips_flag_mdebug = 0;
11096 }
e9670677
MR
11097}
11098\f
11099void
17a2f251 11100mips_init_after_args (void)
252b5132
RH
11101{
11102 /* initialize opcodes */
11103 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
beae10d5 11104 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
252b5132
RH
11105}
11106
11107long
17a2f251 11108md_pcrel_from (fixS *fixP)
252b5132 11109{
a7ebbfdf
TS
11110 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
11111 switch (fixP->fx_r_type)
11112 {
11113 case BFD_RELOC_16_PCREL_S2:
11114 case BFD_RELOC_MIPS_JMP:
11115 /* Return the address of the delay slot. */
11116 return addr + 4;
11117 default:
11118 return addr;
11119 }
252b5132
RH
11120}
11121
252b5132
RH
11122/* This is called before the symbol table is processed. In order to
11123 work with gcc when using mips-tfile, we must keep all local labels.
11124 However, in other cases, we want to discard them. If we were
11125 called with -g, but we didn't see any debugging information, it may
11126 mean that gcc is smuggling debugging information through to
11127 mips-tfile, in which case we must generate all local labels. */
11128
11129void
17a2f251 11130mips_frob_file_before_adjust (void)
252b5132
RH
11131{
11132#ifndef NO_ECOFF_DEBUGGING
11133 if (ECOFF_DEBUGGING
11134 && mips_debug != 0
11135 && ! ecoff_debugging_seen)
11136 flag_keep_locals = 1;
11137#endif
11138}
11139
11140/* Sort any unmatched HI16_S relocs so that they immediately precede
94f592af 11141 the corresponding LO reloc. This is called before md_apply_fix3 and
252b5132
RH
11142 tc_gen_reloc. Unmatched HI16_S relocs can only be generated by
11143 explicit use of the %hi modifier. */
11144
11145void
17a2f251 11146mips_frob_file (void)
252b5132
RH
11147{
11148 struct mips_hi_fixup *l;
11149
11150 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
11151 {
11152 segment_info_type *seginfo;
11153 int pass;
11154
5919d012 11155 assert (reloc_needs_lo_p (l->fixp->fx_r_type));
252b5132 11156
5919d012
RS
11157 /* If a GOT16 relocation turns out to be against a global symbol,
11158 there isn't supposed to be a matching LO. */
11159 if (l->fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
11160 && !pic_need_relax (l->fixp->fx_addsy, l->seg))
11161 continue;
11162
11163 /* Check quickly whether the next fixup happens to be a matching %lo. */
11164 if (fixup_has_matching_lo_p (l->fixp))
252b5132
RH
11165 continue;
11166
11167 /* Look through the fixups for this segment for a matching %lo.
11168 When we find one, move the %hi just in front of it. We do
11169 this in two passes. In the first pass, we try to find a
11170 unique %lo. In the second pass, we permit multiple %hi
11171 relocs for a single %lo (this is a GNU extension). */
11172 seginfo = seg_info (l->seg);
11173 for (pass = 0; pass < 2; pass++)
11174 {
11175 fixS *f, *prev;
11176
11177 prev = NULL;
11178 for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
11179 {
11180 /* Check whether this is a %lo fixup which matches l->fixp. */
11181 if (f->fx_r_type == BFD_RELOC_LO16
11182 && f->fx_addsy == l->fixp->fx_addsy
11183 && f->fx_offset == l->fixp->fx_offset
11184 && (pass == 1
11185 || prev == NULL
5919d012
RS
11186 || !reloc_needs_lo_p (prev->fx_r_type)
11187 || !fixup_has_matching_lo_p (prev)))
252b5132
RH
11188 {
11189 fixS **pf;
11190
11191 /* Move l->fixp before f. */
11192 for (pf = &seginfo->fix_root;
11193 *pf != l->fixp;
11194 pf = &(*pf)->fx_next)
11195 assert (*pf != NULL);
11196
11197 *pf = l->fixp->fx_next;
11198
11199 l->fixp->fx_next = f;
11200 if (prev == NULL)
11201 seginfo->fix_root = l->fixp;
11202 else
11203 prev->fx_next = l->fixp;
11204
11205 break;
11206 }
11207
11208 prev = f;
11209 }
11210
11211 if (f != NULL)
11212 break;
11213
11214#if 0 /* GCC code motion plus incomplete dead code elimination
11215 can leave a %hi without a %lo. */
11216 if (pass == 1)
11217 as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
11218 _("Unmatched %%hi reloc"));
11219#endif
11220 }
11221 }
11222}
11223
11224/* When generating embedded PIC code we need to use a special
11225 relocation to represent the difference of two symbols in the .text
11226 section (switch tables use a difference of this sort). See
11227 include/coff/mips.h for details. This macro checks whether this
11228 fixup requires the special reloc. */
11229#define SWITCH_TABLE(fixp) \
11230 ((fixp)->fx_r_type == BFD_RELOC_32 \
bb2d6cd7 11231 && OUTPUT_FLAVOR != bfd_target_elf_flavour \
252b5132
RH
11232 && (fixp)->fx_addsy != NULL \
11233 && (fixp)->fx_subsy != NULL \
11234 && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
11235 && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
11236
11237/* When generating embedded PIC code we must keep all PC relative
11238 relocations, in case the linker has to relax a call. We also need
f6688943
TS
11239 to keep relocations for switch table entries.
11240
11241 We may have combined relocations without symbols in the N32/N64 ABI.
11242 We have to prevent gas from dropping them. */
252b5132 11243
252b5132 11244int
17a2f251 11245mips_force_relocation (fixS *fixp)
252b5132 11246{
ae6063d4 11247 if (generic_force_reloc (fixp))
252b5132
RH
11248 return 1;
11249
f6688943
TS
11250 if (HAVE_NEWABI
11251 && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
11252 && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
11253 || fixp->fx_r_type == BFD_RELOC_HI16_S
11254 || fixp->fx_r_type == BFD_RELOC_LO16))
11255 return 1;
11256
252b5132
RH
11257 return (mips_pic == EMBEDDED_PIC
11258 && (fixp->fx_pcrel
11259 || SWITCH_TABLE (fixp)
11260 || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
11261 || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
11262}
11263
45f8dfe8
AO
11264/* This hook is called before a fix is simplified. We don't really
11265 decide whether to skip a fix here. Rather, we turn global symbols
11266 used as branch targets into local symbols, such that they undergo
11267 simplification. We can only do this if the symbol is defined and
11268 it is in the same section as the branch. If this doesn't hold, we
11269 emit a better error message than just saying the relocation is not
11270 valid for the selected object format.
11271
11272 FIXP is the fix-up we're going to try to simplify, SEG is the
11273 segment in which the fix up occurs. The return value should be
11274 non-zero to indicate the fix-up is valid for further
11275 simplifications. */
11276
11277int
17a2f251 11278mips_validate_fix (struct fix *fixP, asection *seg)
45f8dfe8
AO
11279{
11280 /* There's a lot of discussion on whether it should be possible to
11281 use R_MIPS_PC16 to represent branch relocations. The outcome
11282 seems to be that it can, but gas/bfd are very broken in creating
11283 RELA relocations for this, so for now we only accept branches to
11284 symbols in the same section. Anything else is of dubious value,
11285 since there's no guarantee that at link time the symbol would be
11286 in range. Even for branches to local symbols this is arguably
11287 wrong, since it we assume the symbol is not going to be
11288 overridden, which should be possible per ELF library semantics,
11289 but then, there isn't a dynamic relocation that could be used to
11290 this effect, and the target would likely be out of range as well.
11291
11292 Unfortunately, it seems that there is too much code out there
11293 that relies on branches to symbols that are global to be resolved
11294 as if they were local, like the IRIX tools do, so we do it as
11295 well, but with a warning so that people are reminded to fix their
11296 code. If we ever get back to using R_MIPS_PC16 for branch
11297 targets, this entire block should go away (and probably the
11298 whole function). */
11299
11300 if (fixP->fx_r_type == BFD_RELOC_16_PCREL_S2
11301 && (((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
11302 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
11303 && mips_pic != EMBEDDED_PIC)
11304 || bfd_reloc_type_lookup (stdoutput, BFD_RELOC_16_PCREL_S2) == NULL)
11305 && fixP->fx_addsy)
11306 {
11307 if (! S_IS_DEFINED (fixP->fx_addsy))
11308 {
11309 as_bad_where (fixP->fx_file, fixP->fx_line,
11310 _("Cannot branch to undefined symbol."));
11311 /* Avoid any further errors about this fixup. */
11312 fixP->fx_done = 1;
11313 }
11314 else if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
11315 {
11316 as_bad_where (fixP->fx_file, fixP->fx_line,
11317 _("Cannot branch to symbol in another section."));
11318 fixP->fx_done = 1;
11319 }
11320 else if (S_IS_EXTERNAL (fixP->fx_addsy))
11321 {
11322 symbolS *sym = fixP->fx_addsy;
11323
115695a8
CD
11324 if (mips_pic == SVR4_PIC)
11325 as_warn_where (fixP->fx_file, fixP->fx_line,
11326 _("Pretending global symbol used as branch target is local."));
45f8dfe8
AO
11327
11328 fixP->fx_addsy = symbol_create (S_GET_NAME (sym),
11329 S_GET_SEGMENT (sym),
11330 S_GET_VALUE (sym),
11331 symbol_get_frag (sym));
11332 copy_symbol_attributes (fixP->fx_addsy, sym);
11333 S_CLEAR_EXTERNAL (fixP->fx_addsy);
11334 assert (symbol_resolved_p (sym));
11335 symbol_mark_resolved (fixP->fx_addsy);
11336 }
11337 }
11338
11339 return 1;
11340}
11341
add55e1f
RS
11342#ifdef OBJ_ELF
11343static int
17a2f251 11344mips_need_elf_addend_fixup (fixS *fixP)
add55e1f 11345{
2d2bf3e0
CD
11346 if (S_GET_OTHER (fixP->fx_addsy) == STO_MIPS16)
11347 return 1;
b25a253c
CD
11348 if (mips_pic == EMBEDDED_PIC
11349 && S_IS_WEAK (fixP->fx_addsy))
11350 return 1;
11351 if (mips_pic != EMBEDDED_PIC
11352 && (S_IS_WEAK (fixP->fx_addsy)
bad9ca53 11353 || S_IS_EXTERNAL (fixP->fx_addsy))
2d2bf3e0
CD
11354 && !S_IS_COMMON (fixP->fx_addsy))
11355 return 1;
9204e615
DJ
11356 if (((bfd_get_section_flags (stdoutput,
11357 S_GET_SEGMENT (fixP->fx_addsy))
11358 & (SEC_LINK_ONCE | SEC_MERGE)) != 0)
11359 || !strncmp (segment_name (S_GET_SEGMENT (fixP->fx_addsy)),
11360 ".gnu.linkonce",
11361 sizeof (".gnu.linkonce") - 1))
2d2bf3e0
CD
11362 return 1;
11363 return 0;
add55e1f
RS
11364}
11365#endif
11366
252b5132
RH
11367/* Apply a fixup to the object file. */
11368
94f592af 11369void
17a2f251 11370md_apply_fix3 (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 11371{
874e8986 11372 bfd_byte *buf;
98aa84af 11373 long insn;
ed6fb7bd 11374 static int previous_fx_r_type = 0;
a7ebbfdf 11375 reloc_howto_type *howto;
252b5132 11376
a7ebbfdf
TS
11377 /* We ignore generic BFD relocations we don't know about. */
11378 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
11379 if (! howto)
11380 return;
65551fa4 11381
252b5132
RH
11382 assert (fixP->fx_size == 4
11383 || fixP->fx_r_type == BFD_RELOC_16
11384 || fixP->fx_r_type == BFD_RELOC_64
f6688943
TS
11385 || fixP->fx_r_type == BFD_RELOC_CTOR
11386 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
252b5132 11387 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
a7ebbfdf 11388 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY);
252b5132 11389
a7ebbfdf 11390 buf = (bfd_byte *) (fixP->fx_frag->fr_literal + fixP->fx_where);
252b5132
RH
11391
11392 /* If we aren't adjusting this fixup to be against the section
11393 symbol, we need to adjust the value. */
11394#ifdef OBJ_ELF
11395 if (fixP->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour)
bb2d6cd7 11396 {
a7ebbfdf
TS
11397 if (mips_need_elf_addend_fixup (fixP)
11398 && howto->partial_inplace
11399 && fixP->fx_r_type != BFD_RELOC_GPREL16
11400 && fixP->fx_r_type != BFD_RELOC_GPREL32
11401 && fixP->fx_r_type != BFD_RELOC_MIPS16_GPREL)
11402 {
11403 /* In this case, the bfd_install_relocation routine will
11404 incorrectly add the symbol value back in. We just want
11405 the addend to appear in the object file.
11406
11407 The condition above used to include
11408 "&& (! fixP->fx_pcrel || howto->pcrel_offset)".
11409
11410 However, howto can't be trusted here, because we
11411 might change the reloc type in tc_gen_reloc. We can
11412 check howto->partial_inplace because that conversion
11413 happens to preserve howto->partial_inplace; but it
11414 does not preserve howto->pcrel_offset. I've just
11415 eliminated the check, because all MIPS PC-relative
11416 relocations are marked howto->pcrel_offset.
11417
11418 howto->pcrel_offset was originally added for
11419 R_MIPS_PC16, which is generated for code like
11420
11421 globl g1 .text
11422 .text
11423 .space 20
11424 g1:
11425 x:
11426 bal g1
11427 */
11428 *valP -= S_GET_VALUE (fixP->fx_addsy);
98aa84af 11429 }
252b5132 11430
bb2d6cd7
GK
11431 /* This code was generated using trial and error and so is
11432 fragile and not trustworthy. If you change it, you should
11433 rerun the elf-rel, elf-rel2, and empic testcases and ensure
11434 they still pass. */
a7ebbfdf 11435 if (fixP->fx_pcrel)
bb2d6cd7 11436 {
a7ebbfdf 11437 *valP += fixP->fx_frag->fr_address + fixP->fx_where;
bb2d6cd7
GK
11438
11439 /* BFD's REL handling, for MIPS, is _very_ weird.
11440 This gives the right results, but it can't possibly
11441 be the way things are supposed to work. */
a7ebbfdf 11442 *valP += fixP->fx_frag->fr_address + fixP->fx_where;
bb2d6cd7
GK
11443 }
11444 }
11445#endif
252b5132 11446
ed6fb7bd
SC
11447 /* We are not done if this is a composite relocation to set up gp. */
11448 if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel
11449 && !(fixP->fx_r_type == BFD_RELOC_MIPS_SUB
10181a0d
AO
11450 || (fixP->fx_r_type == BFD_RELOC_64
11451 && (previous_fx_r_type == BFD_RELOC_GPREL32
11452 || previous_fx_r_type == BFD_RELOC_GPREL16))
ed6fb7bd
SC
11453 || (previous_fx_r_type == BFD_RELOC_MIPS_SUB
11454 && (fixP->fx_r_type == BFD_RELOC_HI16_S
11455 || fixP->fx_r_type == BFD_RELOC_LO16))))
252b5132 11456 fixP->fx_done = 1;
ed6fb7bd 11457 previous_fx_r_type = fixP->fx_r_type;
252b5132
RH
11458
11459 switch (fixP->fx_r_type)
11460 {
11461 case BFD_RELOC_MIPS_JMP:
e369bcce
TS
11462 case BFD_RELOC_MIPS_SHIFT5:
11463 case BFD_RELOC_MIPS_SHIFT6:
11464 case BFD_RELOC_MIPS_GOT_DISP:
11465 case BFD_RELOC_MIPS_GOT_PAGE:
11466 case BFD_RELOC_MIPS_GOT_OFST:
11467 case BFD_RELOC_MIPS_SUB:
11468 case BFD_RELOC_MIPS_INSERT_A:
11469 case BFD_RELOC_MIPS_INSERT_B:
11470 case BFD_RELOC_MIPS_DELETE:
11471 case BFD_RELOC_MIPS_HIGHEST:
11472 case BFD_RELOC_MIPS_HIGHER:
11473 case BFD_RELOC_MIPS_SCN_DISP:
11474 case BFD_RELOC_MIPS_REL16:
11475 case BFD_RELOC_MIPS_RELGOT:
11476 case BFD_RELOC_MIPS_JALR:
252b5132
RH
11477 case BFD_RELOC_HI16:
11478 case BFD_RELOC_HI16_S:
cdf6fd85 11479 case BFD_RELOC_GPREL16:
252b5132
RH
11480 case BFD_RELOC_MIPS_LITERAL:
11481 case BFD_RELOC_MIPS_CALL16:
11482 case BFD_RELOC_MIPS_GOT16:
cdf6fd85 11483 case BFD_RELOC_GPREL32:
252b5132
RH
11484 case BFD_RELOC_MIPS_GOT_HI16:
11485 case BFD_RELOC_MIPS_GOT_LO16:
11486 case BFD_RELOC_MIPS_CALL_HI16:
11487 case BFD_RELOC_MIPS_CALL_LO16:
11488 case BFD_RELOC_MIPS16_GPREL:
11489 if (fixP->fx_pcrel)
11490 as_bad_where (fixP->fx_file, fixP->fx_line,
11491 _("Invalid PC relative reloc"));
11492 /* Nothing needed to do. The value comes from the reloc entry */
11493 break;
11494
11495 case BFD_RELOC_MIPS16_JMP:
11496 /* We currently always generate a reloc against a symbol, which
11497 means that we don't want an addend even if the symbol is
11498 defined. */
a7ebbfdf 11499 *valP = 0;
252b5132
RH
11500 break;
11501
11502 case BFD_RELOC_PCREL_HI16_S:
11503 /* The addend for this is tricky if it is internal, so we just
11504 do everything here rather than in bfd_install_relocation. */
a7ebbfdf 11505 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && !fixP->fx_done)
bb2d6cd7
GK
11506 break;
11507 if (fixP->fx_addsy
11508 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
252b5132
RH
11509 {
11510 /* For an external symbol adjust by the address to make it
11511 pcrel_offset. We use the address of the RELLO reloc
11512 which follows this one. */
a7ebbfdf 11513 *valP += (fixP->fx_next->fx_frag->fr_address
252b5132
RH
11514 + fixP->fx_next->fx_where);
11515 }
a7ebbfdf 11516 *valP = ((*valP + 0x8000) >> 16) & 0xffff;
252b5132
RH
11517 if (target_big_endian)
11518 buf += 2;
17a2f251 11519 md_number_to_chars (buf, *valP, 2);
252b5132
RH
11520 break;
11521
11522 case BFD_RELOC_PCREL_LO16:
11523 /* The addend for this is tricky if it is internal, so we just
11524 do everything here rather than in bfd_install_relocation. */
a7ebbfdf 11525 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && !fixP->fx_done)
bb2d6cd7
GK
11526 break;
11527 if (fixP->fx_addsy
11528 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
a7ebbfdf 11529 *valP += fixP->fx_frag->fr_address + fixP->fx_where;
252b5132
RH
11530 if (target_big_endian)
11531 buf += 2;
17a2f251 11532 md_number_to_chars (buf, *valP, 2);
252b5132
RH
11533 break;
11534
11535 case BFD_RELOC_64:
11536 /* This is handled like BFD_RELOC_32, but we output a sign
11537 extended value if we are only 32 bits. */
11538 if (fixP->fx_done
11539 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11540 {
11541 if (8 <= sizeof (valueT))
a7ebbfdf 11542 md_number_to_chars (buf, *valP, 8);
252b5132
RH
11543 else
11544 {
a7ebbfdf 11545 valueT hiv;
252b5132 11546
a7ebbfdf 11547 if ((*valP & 0x80000000) != 0)
252b5132
RH
11548 hiv = 0xffffffff;
11549 else
11550 hiv = 0;
a7ebbfdf
TS
11551 md_number_to_chars ((char *)(buf + target_big_endian ? 4 : 0),
11552 *valP, 4);
11553 md_number_to_chars ((char *)(buf + target_big_endian ? 0 : 4),
11554 hiv, 4);
252b5132
RH
11555 }
11556 }
11557 break;
11558
056350c6 11559 case BFD_RELOC_RVA:
252b5132
RH
11560 case BFD_RELOC_32:
11561 /* If we are deleting this reloc entry, we must fill in the
11562 value now. This can happen if we have a .word which is not
11563 resolved when it appears but is later defined. We also need
11564 to fill in the value if this is an embedded PIC switch table
11565 entry. */
11566 if (fixP->fx_done
11567 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
a7ebbfdf 11568 md_number_to_chars (buf, *valP, 4);
252b5132
RH
11569 break;
11570
11571 case BFD_RELOC_16:
11572 /* If we are deleting this reloc entry, we must fill in the
11573 value now. */
11574 assert (fixP->fx_size == 2);
11575 if (fixP->fx_done)
a7ebbfdf 11576 md_number_to_chars (buf, *valP, 2);
252b5132
RH
11577 break;
11578
11579 case BFD_RELOC_LO16:
11580 /* When handling an embedded PIC switch statement, we can wind
11581 up deleting a LO16 reloc. See the 'o' case in mips_ip. */
11582 if (fixP->fx_done)
11583 {
a7ebbfdf 11584 if (*valP + 0x8000 > 0xffff)
252b5132
RH
11585 as_bad_where (fixP->fx_file, fixP->fx_line,
11586 _("relocation overflow"));
252b5132
RH
11587 if (target_big_endian)
11588 buf += 2;
17a2f251 11589 md_number_to_chars (buf, *valP, 2);
252b5132
RH
11590 }
11591 break;
11592
11593 case BFD_RELOC_16_PCREL_S2:
a7ebbfdf 11594 if ((*valP & 0x3) != 0)
cb56d3d3 11595 as_bad_where (fixP->fx_file, fixP->fx_line,
a7ebbfdf 11596 _("Branch to odd address (%lx)"), (long) *valP);
cb56d3d3 11597
252b5132
RH
11598 /*
11599 * We need to save the bits in the instruction since fixup_segment()
11600 * might be deleting the relocation entry (i.e., a branch within
11601 * the current segment).
11602 */
a7ebbfdf 11603 if (! fixP->fx_done)
bb2d6cd7 11604 break;
252b5132
RH
11605
11606 /* update old instruction data */
252b5132
RH
11607 if (target_big_endian)
11608 insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
11609 else
11610 insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
11611
a7ebbfdf
TS
11612 if (*valP + 0x20000 <= 0x3ffff)
11613 {
11614 insn |= (*valP >> 2) & 0xffff;
17a2f251 11615 md_number_to_chars (buf, insn, 4);
a7ebbfdf
TS
11616 }
11617 else if (mips_pic == NO_PIC
11618 && fixP->fx_done
11619 && fixP->fx_frag->fr_address >= text_section->vma
11620 && (fixP->fx_frag->fr_address
11621 < text_section->vma + text_section->_raw_size)
11622 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
11623 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
11624 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
252b5132
RH
11625 {
11626 /* The branch offset is too large. If this is an
11627 unconditional branch, and we are not generating PIC code,
11628 we can convert it to an absolute jump instruction. */
a7ebbfdf
TS
11629 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
11630 insn = 0x0c000000; /* jal */
252b5132 11631 else
a7ebbfdf
TS
11632 insn = 0x08000000; /* j */
11633 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
11634 fixP->fx_done = 0;
11635 fixP->fx_addsy = section_symbol (text_section);
11636 *valP += md_pcrel_from (fixP);
17a2f251 11637 md_number_to_chars (buf, insn, 4);
a7ebbfdf
TS
11638 }
11639 else
11640 {
11641 /* If we got here, we have branch-relaxation disabled,
11642 and there's nothing we can do to fix this instruction
11643 without turning it into a longer sequence. */
11644 as_bad_where (fixP->fx_file, fixP->fx_line,
11645 _("Branch out of range"));
252b5132 11646 }
252b5132
RH
11647 break;
11648
11649 case BFD_RELOC_VTABLE_INHERIT:
11650 fixP->fx_done = 0;
11651 if (fixP->fx_addsy
11652 && !S_IS_DEFINED (fixP->fx_addsy)
11653 && !S_IS_WEAK (fixP->fx_addsy))
11654 S_SET_WEAK (fixP->fx_addsy);
11655 break;
11656
11657 case BFD_RELOC_VTABLE_ENTRY:
11658 fixP->fx_done = 0;
11659 break;
11660
11661 default:
11662 internalError ();
11663 }
a7ebbfdf
TS
11664
11665 /* Remember value for tc_gen_reloc. */
11666 fixP->fx_addnumber = *valP;
252b5132
RH
11667}
11668
11669#if 0
11670void
17a2f251 11671printInsn (unsigned long oc)
252b5132
RH
11672{
11673 const struct mips_opcode *p;
11674 int treg, sreg, dreg, shamt;
11675 short imm;
11676 const char *args;
11677 int i;
11678
11679 for (i = 0; i < NUMOPCODES; ++i)
11680 {
11681 p = &mips_opcodes[i];
11682 if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
11683 {
11684 printf ("%08lx %s\t", oc, p->name);
11685 treg = (oc >> 16) & 0x1f;
11686 sreg = (oc >> 21) & 0x1f;
11687 dreg = (oc >> 11) & 0x1f;
11688 shamt = (oc >> 6) & 0x1f;
11689 imm = oc;
11690 for (args = p->args;; ++args)
11691 {
11692 switch (*args)
11693 {
11694 case '\0':
11695 printf ("\n");
11696 break;
11697
11698 case ',':
11699 case '(':
11700 case ')':
11701 printf ("%c", *args);
11702 continue;
11703
11704 case 'r':
11705 assert (treg == sreg);
11706 printf ("$%d,$%d", treg, sreg);
11707 continue;
11708
11709 case 'd':
11710 case 'G':
11711 printf ("$%d", dreg);
11712 continue;
11713
11714 case 't':
11715 case 'E':
11716 printf ("$%d", treg);
11717 continue;
11718
11719 case 'k':
11720 printf ("0x%x", treg);
11721 continue;
11722
11723 case 'b':
11724 case 's':
11725 printf ("$%d", sreg);
11726 continue;
11727
11728 case 'a':
11729 printf ("0x%08lx", oc & 0x1ffffff);
11730 continue;
11731
11732 case 'i':
11733 case 'j':
11734 case 'o':
11735 case 'u':
11736 printf ("%d", imm);
11737 continue;
11738
11739 case '<':
11740 case '>':
11741 printf ("$%d", shamt);
11742 continue;
11743
11744 default:
11745 internalError ();
11746 }
11747 break;
11748 }
11749 return;
11750 }
11751 }
11752 printf (_("%08lx UNDEFINED\n"), oc);
11753}
11754#endif
11755
11756static symbolS *
17a2f251 11757get_symbol (void)
252b5132
RH
11758{
11759 int c;
11760 char *name;
11761 symbolS *p;
11762
11763 name = input_line_pointer;
11764 c = get_symbol_end ();
11765 p = (symbolS *) symbol_find_or_make (name);
11766 *input_line_pointer = c;
11767 return p;
11768}
11769
11770/* Align the current frag to a given power of two. The MIPS assembler
11771 also automatically adjusts any preceding label. */
11772
11773static void
17a2f251 11774mips_align (int to, int fill, symbolS *label)
252b5132 11775{
b34976b6 11776 mips_emit_delays (FALSE);
252b5132
RH
11777 frag_align (to, fill, 0);
11778 record_alignment (now_seg, to);
11779 if (label != NULL)
11780 {
11781 assert (S_GET_SEGMENT (label) == now_seg);
49309057 11782 symbol_set_frag (label, frag_now);
252b5132
RH
11783 S_SET_VALUE (label, (valueT) frag_now_fix ());
11784 }
11785}
11786
11787/* Align to a given power of two. .align 0 turns off the automatic
11788 alignment used by the data creating pseudo-ops. */
11789
11790static void
17a2f251 11791s_align (int x ATTRIBUTE_UNUSED)
252b5132
RH
11792{
11793 register int temp;
11794 register long temp_fill;
11795 long max_alignment = 15;
11796
11797 /*
11798
67c1ffbe 11799 o Note that the assembler pulls down any immediately preceding label
252b5132
RH
11800 to the aligned address.
11801 o It's not documented but auto alignment is reinstated by
11802 a .align pseudo instruction.
11803 o Note also that after auto alignment is turned off the mips assembler
11804 issues an error on attempt to assemble an improperly aligned data item.
11805 We don't.
11806
11807 */
11808
11809 temp = get_absolute_expression ();
11810 if (temp > max_alignment)
11811 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
11812 else if (temp < 0)
11813 {
11814 as_warn (_("Alignment negative: 0 assumed."));
11815 temp = 0;
11816 }
11817 if (*input_line_pointer == ',')
11818 {
f9419b05 11819 ++input_line_pointer;
252b5132
RH
11820 temp_fill = get_absolute_expression ();
11821 }
11822 else
11823 temp_fill = 0;
11824 if (temp)
11825 {
11826 auto_align = 1;
11827 mips_align (temp, (int) temp_fill,
11828 insn_labels != NULL ? insn_labels->label : NULL);
11829 }
11830 else
11831 {
11832 auto_align = 0;
11833 }
11834
11835 demand_empty_rest_of_line ();
11836}
11837
11838void
17a2f251 11839mips_flush_pending_output (void)
252b5132 11840{
b34976b6 11841 mips_emit_delays (FALSE);
252b5132
RH
11842 mips_clear_insn_labels ();
11843}
11844
11845static void
17a2f251 11846s_change_sec (int sec)
252b5132
RH
11847{
11848 segT seg;
11849
11850 /* When generating embedded PIC code, we only use the .text, .lit8,
11851 .sdata and .sbss sections. We change the .data and .rdata
11852 pseudo-ops to use .sdata. */
11853 if (mips_pic == EMBEDDED_PIC
11854 && (sec == 'd' || sec == 'r'))
11855 sec = 's';
11856
11857#ifdef OBJ_ELF
11858 /* The ELF backend needs to know that we are changing sections, so
11859 that .previous works correctly. We could do something like check
b6ff326e 11860 for an obj_section_change_hook macro, but that might be confusing
252b5132
RH
11861 as it would not be appropriate to use it in the section changing
11862 functions in read.c, since obj-elf.c intercepts those. FIXME:
11863 This should be cleaner, somehow. */
11864 obj_elf_section_change_hook ();
11865#endif
11866
b34976b6 11867 mips_emit_delays (FALSE);
252b5132
RH
11868 switch (sec)
11869 {
11870 case 't':
11871 s_text (0);
11872 break;
11873 case 'd':
11874 s_data (0);
11875 break;
11876 case 'b':
11877 subseg_set (bss_section, (subsegT) get_absolute_expression ());
11878 demand_empty_rest_of_line ();
11879 break;
11880
11881 case 'r':
11882 if (USE_GLOBAL_POINTER_OPT)
11883 {
11884 seg = subseg_new (RDATA_SECTION_NAME,
11885 (subsegT) get_absolute_expression ());
11886 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11887 {
11888 bfd_set_section_flags (stdoutput, seg,
11889 (SEC_ALLOC
11890 | SEC_LOAD
11891 | SEC_READONLY
11892 | SEC_RELOC
11893 | SEC_DATA));
11894 if (strcmp (TARGET_OS, "elf") != 0)
e799a695 11895 record_alignment (seg, 4);
252b5132
RH
11896 }
11897 demand_empty_rest_of_line ();
11898 }
11899 else
11900 {
11901 as_bad (_("No read only data section in this object file format"));
11902 demand_empty_rest_of_line ();
11903 return;
11904 }
11905 break;
11906
11907 case 's':
11908 if (USE_GLOBAL_POINTER_OPT)
11909 {
11910 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
11911 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11912 {
11913 bfd_set_section_flags (stdoutput, seg,
11914 SEC_ALLOC | SEC_LOAD | SEC_RELOC
11915 | SEC_DATA);
11916 if (strcmp (TARGET_OS, "elf") != 0)
e799a695 11917 record_alignment (seg, 4);
252b5132
RH
11918 }
11919 demand_empty_rest_of_line ();
11920 break;
11921 }
11922 else
11923 {
11924 as_bad (_("Global pointers not supported; recompile -G 0"));
11925 demand_empty_rest_of_line ();
11926 return;
11927 }
11928 }
11929
11930 auto_align = 1;
11931}
b34976b6 11932
cca86cc8 11933void
17a2f251 11934s_change_section (int ignore ATTRIBUTE_UNUSED)
cca86cc8 11935{
7ed4a06a 11936#ifdef OBJ_ELF
cca86cc8
SC
11937 char *section_name;
11938 char c;
684022ea 11939 char next_c = 0;
cca86cc8
SC
11940 int section_type;
11941 int section_flag;
11942 int section_entry_size;
11943 int section_alignment;
b34976b6 11944
7ed4a06a
TS
11945 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11946 return;
11947
cca86cc8
SC
11948 section_name = input_line_pointer;
11949 c = get_symbol_end ();
a816d1ed
AO
11950 if (c)
11951 next_c = *(input_line_pointer + 1);
cca86cc8 11952
4cf0dd0d
TS
11953 /* Do we have .section Name<,"flags">? */
11954 if (c != ',' || (c == ',' && next_c == '"'))
cca86cc8 11955 {
4cf0dd0d
TS
11956 /* just after name is now '\0'. */
11957 *input_line_pointer = c;
cca86cc8
SC
11958 input_line_pointer = section_name;
11959 obj_elf_section (ignore);
11960 return;
11961 }
11962 input_line_pointer++;
11963
11964 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
11965 if (c == ',')
11966 section_type = get_absolute_expression ();
11967 else
11968 section_type = 0;
11969 if (*input_line_pointer++ == ',')
11970 section_flag = get_absolute_expression ();
11971 else
11972 section_flag = 0;
11973 if (*input_line_pointer++ == ',')
11974 section_entry_size = get_absolute_expression ();
11975 else
11976 section_entry_size = 0;
11977 if (*input_line_pointer++ == ',')
11978 section_alignment = get_absolute_expression ();
11979 else
11980 section_alignment = 0;
11981
a816d1ed
AO
11982 section_name = xstrdup (section_name);
11983
cca86cc8
SC
11984 obj_elf_change_section (section_name, section_type, section_flag,
11985 section_entry_size, 0, 0, 0);
a816d1ed
AO
11986
11987 if (now_seg->name != section_name)
11988 free (section_name);
7ed4a06a 11989#endif /* OBJ_ELF */
cca86cc8 11990}
252b5132
RH
11991
11992void
17a2f251 11993mips_enable_auto_align (void)
252b5132
RH
11994{
11995 auto_align = 1;
11996}
11997
11998static void
17a2f251 11999s_cons (int log_size)
252b5132
RH
12000{
12001 symbolS *label;
12002
12003 label = insn_labels != NULL ? insn_labels->label : NULL;
b34976b6 12004 mips_emit_delays (FALSE);
252b5132
RH
12005 if (log_size > 0 && auto_align)
12006 mips_align (log_size, 0, label);
12007 mips_clear_insn_labels ();
12008 cons (1 << log_size);
12009}
12010
12011static void
17a2f251 12012s_float_cons (int type)
252b5132
RH
12013{
12014 symbolS *label;
12015
12016 label = insn_labels != NULL ? insn_labels->label : NULL;
12017
b34976b6 12018 mips_emit_delays (FALSE);
252b5132
RH
12019
12020 if (auto_align)
49309057
ILT
12021 {
12022 if (type == 'd')
12023 mips_align (3, 0, label);
12024 else
12025 mips_align (2, 0, label);
12026 }
252b5132
RH
12027
12028 mips_clear_insn_labels ();
12029
12030 float_cons (type);
12031}
12032
12033/* Handle .globl. We need to override it because on Irix 5 you are
12034 permitted to say
12035 .globl foo .text
12036 where foo is an undefined symbol, to mean that foo should be
12037 considered to be the address of a function. */
12038
12039static void
17a2f251 12040s_mips_globl (int x ATTRIBUTE_UNUSED)
252b5132
RH
12041{
12042 char *name;
12043 int c;
12044 symbolS *symbolP;
12045 flagword flag;
12046
12047 name = input_line_pointer;
12048 c = get_symbol_end ();
12049 symbolP = symbol_find_or_make (name);
12050 *input_line_pointer = c;
12051 SKIP_WHITESPACE ();
12052
12053 /* On Irix 5, every global symbol that is not explicitly labelled as
12054 being a function is apparently labelled as being an object. */
12055 flag = BSF_OBJECT;
12056
12057 if (! is_end_of_line[(unsigned char) *input_line_pointer])
12058 {
12059 char *secname;
12060 asection *sec;
12061
12062 secname = input_line_pointer;
12063 c = get_symbol_end ();
12064 sec = bfd_get_section_by_name (stdoutput, secname);
12065 if (sec == NULL)
12066 as_bad (_("%s: no such section"), secname);
12067 *input_line_pointer = c;
12068
12069 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
12070 flag = BSF_FUNCTION;
12071 }
12072
49309057 12073 symbol_get_bfdsym (symbolP)->flags |= flag;
252b5132
RH
12074
12075 S_SET_EXTERNAL (symbolP);
12076 demand_empty_rest_of_line ();
12077}
12078
12079static void
17a2f251 12080s_option (int x ATTRIBUTE_UNUSED)
252b5132
RH
12081{
12082 char *opt;
12083 char c;
12084
12085 opt = input_line_pointer;
12086 c = get_symbol_end ();
12087
12088 if (*opt == 'O')
12089 {
12090 /* FIXME: What does this mean? */
12091 }
12092 else if (strncmp (opt, "pic", 3) == 0)
12093 {
12094 int i;
12095
12096 i = atoi (opt + 3);
12097 if (i == 0)
12098 mips_pic = NO_PIC;
12099 else if (i == 2)
143d77c5 12100 {
252b5132 12101 mips_pic = SVR4_PIC;
143d77c5
EC
12102 mips_abicalls = TRUE;
12103 }
252b5132
RH
12104 else
12105 as_bad (_(".option pic%d not supported"), i);
12106
12107 if (USE_GLOBAL_POINTER_OPT && mips_pic == SVR4_PIC)
12108 {
12109 if (g_switch_seen && g_switch_value != 0)
12110 as_warn (_("-G may not be used with SVR4 PIC code"));
12111 g_switch_value = 0;
12112 bfd_set_gp_size (stdoutput, 0);
12113 }
12114 }
12115 else
12116 as_warn (_("Unrecognized option \"%s\""), opt);
12117
12118 *input_line_pointer = c;
12119 demand_empty_rest_of_line ();
12120}
12121
12122/* This structure is used to hold a stack of .set values. */
12123
e972090a
NC
12124struct mips_option_stack
12125{
252b5132
RH
12126 struct mips_option_stack *next;
12127 struct mips_set_options options;
12128};
12129
12130static struct mips_option_stack *mips_opts_stack;
12131
12132/* Handle the .set pseudo-op. */
12133
12134static void
17a2f251 12135s_mipsset (int x ATTRIBUTE_UNUSED)
252b5132
RH
12136{
12137 char *name = input_line_pointer, ch;
12138
12139 while (!is_end_of_line[(unsigned char) *input_line_pointer])
f9419b05 12140 ++input_line_pointer;
252b5132
RH
12141 ch = *input_line_pointer;
12142 *input_line_pointer = '\0';
12143
12144 if (strcmp (name, "reorder") == 0)
12145 {
12146 if (mips_opts.noreorder && prev_nop_frag != NULL)
12147 {
12148 /* If we still have pending nops, we can discard them. The
12149 usual nop handling will insert any that are still
bdaaa2e1 12150 needed. */
252b5132
RH
12151 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
12152 * (mips_opts.mips16 ? 2 : 4));
12153 prev_nop_frag = NULL;
12154 }
12155 mips_opts.noreorder = 0;
12156 }
12157 else if (strcmp (name, "noreorder") == 0)
12158 {
b34976b6 12159 mips_emit_delays (TRUE);
252b5132
RH
12160 mips_opts.noreorder = 1;
12161 mips_any_noreorder = 1;
12162 }
12163 else if (strcmp (name, "at") == 0)
12164 {
12165 mips_opts.noat = 0;
12166 }
12167 else if (strcmp (name, "noat") == 0)
12168 {
12169 mips_opts.noat = 1;
12170 }
12171 else if (strcmp (name, "macro") == 0)
12172 {
12173 mips_opts.warn_about_macros = 0;
12174 }
12175 else if (strcmp (name, "nomacro") == 0)
12176 {
12177 if (mips_opts.noreorder == 0)
12178 as_bad (_("`noreorder' must be set before `nomacro'"));
12179 mips_opts.warn_about_macros = 1;
12180 }
12181 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
12182 {
12183 mips_opts.nomove = 0;
12184 }
12185 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
12186 {
12187 mips_opts.nomove = 1;
12188 }
12189 else if (strcmp (name, "bopt") == 0)
12190 {
12191 mips_opts.nobopt = 0;
12192 }
12193 else if (strcmp (name, "nobopt") == 0)
12194 {
12195 mips_opts.nobopt = 1;
12196 }
12197 else if (strcmp (name, "mips16") == 0
12198 || strcmp (name, "MIPS-16") == 0)
12199 mips_opts.mips16 = 1;
12200 else if (strcmp (name, "nomips16") == 0
12201 || strcmp (name, "noMIPS-16") == 0)
12202 mips_opts.mips16 = 0;
1f25f5d3
CD
12203 else if (strcmp (name, "mips3d") == 0)
12204 mips_opts.ase_mips3d = 1;
12205 else if (strcmp (name, "nomips3d") == 0)
12206 mips_opts.ase_mips3d = 0;
a4672219
TS
12207 else if (strcmp (name, "mdmx") == 0)
12208 mips_opts.ase_mdmx = 1;
12209 else if (strcmp (name, "nomdmx") == 0)
12210 mips_opts.ase_mdmx = 0;
1a2c1fad 12211 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
252b5132 12212 {
af7ee8bf 12213 int reset = 0;
252b5132 12214
1a2c1fad
CD
12215 /* Permit the user to change the ISA and architecture on the fly.
12216 Needless to say, misuse can cause serious problems. */
af7ee8bf
CD
12217 if (strcmp (name, "mips0") == 0)
12218 {
12219 reset = 1;
12220 mips_opts.isa = file_mips_isa;
12221 }
12222 else if (strcmp (name, "mips1") == 0)
12223 mips_opts.isa = ISA_MIPS1;
12224 else if (strcmp (name, "mips2") == 0)
12225 mips_opts.isa = ISA_MIPS2;
12226 else if (strcmp (name, "mips3") == 0)
12227 mips_opts.isa = ISA_MIPS3;
12228 else if (strcmp (name, "mips4") == 0)
12229 mips_opts.isa = ISA_MIPS4;
12230 else if (strcmp (name, "mips5") == 0)
12231 mips_opts.isa = ISA_MIPS5;
12232 else if (strcmp (name, "mips32") == 0)
12233 mips_opts.isa = ISA_MIPS32;
12234 else if (strcmp (name, "mips32r2") == 0)
12235 mips_opts.isa = ISA_MIPS32R2;
12236 else if (strcmp (name, "mips64") == 0)
12237 mips_opts.isa = ISA_MIPS64;
5f74bc13
CD
12238 else if (strcmp (name, "mips64r2") == 0)
12239 mips_opts.isa = ISA_MIPS64R2;
1a2c1fad
CD
12240 else if (strcmp (name, "arch=default") == 0)
12241 {
12242 reset = 1;
12243 mips_opts.arch = file_mips_arch;
12244 mips_opts.isa = file_mips_isa;
12245 }
12246 else if (strncmp (name, "arch=", 5) == 0)
12247 {
12248 const struct mips_cpu_info *p;
12249
12250 p = mips_parse_cpu("internal use", name + 5);
12251 if (!p)
12252 as_bad (_("unknown architecture %s"), name + 5);
12253 else
12254 {
12255 mips_opts.arch = p->cpu;
12256 mips_opts.isa = p->isa;
12257 }
12258 }
af7ee8bf
CD
12259 else
12260 as_bad (_("unknown ISA level %s"), name + 4);
12261
12262 switch (mips_opts.isa)
98d3f06f
KH
12263 {
12264 case 0:
98d3f06f 12265 break;
af7ee8bf
CD
12266 case ISA_MIPS1:
12267 case ISA_MIPS2:
12268 case ISA_MIPS32:
12269 case ISA_MIPS32R2:
98d3f06f
KH
12270 mips_opts.gp32 = 1;
12271 mips_opts.fp32 = 1;
12272 break;
af7ee8bf
CD
12273 case ISA_MIPS3:
12274 case ISA_MIPS4:
12275 case ISA_MIPS5:
12276 case ISA_MIPS64:
5f74bc13 12277 case ISA_MIPS64R2:
98d3f06f
KH
12278 mips_opts.gp32 = 0;
12279 mips_opts.fp32 = 0;
12280 break;
12281 default:
12282 as_bad (_("unknown ISA level %s"), name + 4);
12283 break;
12284 }
af7ee8bf 12285 if (reset)
98d3f06f 12286 {
af7ee8bf
CD
12287 mips_opts.gp32 = file_mips_gp32;
12288 mips_opts.fp32 = file_mips_fp32;
98d3f06f 12289 }
252b5132
RH
12290 }
12291 else if (strcmp (name, "autoextend") == 0)
12292 mips_opts.noautoextend = 0;
12293 else if (strcmp (name, "noautoextend") == 0)
12294 mips_opts.noautoextend = 1;
12295 else if (strcmp (name, "push") == 0)
12296 {
12297 struct mips_option_stack *s;
12298
12299 s = (struct mips_option_stack *) xmalloc (sizeof *s);
12300 s->next = mips_opts_stack;
12301 s->options = mips_opts;
12302 mips_opts_stack = s;
12303 }
12304 else if (strcmp (name, "pop") == 0)
12305 {
12306 struct mips_option_stack *s;
12307
12308 s = mips_opts_stack;
12309 if (s == NULL)
12310 as_bad (_(".set pop with no .set push"));
12311 else
12312 {
12313 /* If we're changing the reorder mode we need to handle
12314 delay slots correctly. */
12315 if (s->options.noreorder && ! mips_opts.noreorder)
b34976b6 12316 mips_emit_delays (TRUE);
252b5132
RH
12317 else if (! s->options.noreorder && mips_opts.noreorder)
12318 {
12319 if (prev_nop_frag != NULL)
12320 {
12321 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
12322 * (mips_opts.mips16 ? 2 : 4));
12323 prev_nop_frag = NULL;
12324 }
12325 }
12326
12327 mips_opts = s->options;
12328 mips_opts_stack = s->next;
12329 free (s);
12330 }
12331 }
12332 else
12333 {
12334 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
12335 }
12336 *input_line_pointer = ch;
12337 demand_empty_rest_of_line ();
12338}
12339
12340/* Handle the .abicalls pseudo-op. I believe this is equivalent to
12341 .option pic2. It means to generate SVR4 PIC calls. */
12342
12343static void
17a2f251 12344s_abicalls (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
12345{
12346 mips_pic = SVR4_PIC;
143d77c5 12347 mips_abicalls = TRUE;
252b5132
RH
12348 if (USE_GLOBAL_POINTER_OPT)
12349 {
12350 if (g_switch_seen && g_switch_value != 0)
12351 as_warn (_("-G may not be used with SVR4 PIC code"));
12352 g_switch_value = 0;
12353 }
12354 bfd_set_gp_size (stdoutput, 0);
12355 demand_empty_rest_of_line ();
12356}
12357
12358/* Handle the .cpload pseudo-op. This is used when generating SVR4
12359 PIC code. It sets the $gp register for the function based on the
12360 function address, which is in the register named in the argument.
12361 This uses a relocation against _gp_disp, which is handled specially
12362 by the linker. The result is:
12363 lui $gp,%hi(_gp_disp)
12364 addiu $gp,$gp,%lo(_gp_disp)
12365 addu $gp,$gp,.cpload argument
12366 The .cpload argument is normally $25 == $t9. */
12367
12368static void
17a2f251 12369s_cpload (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
12370{
12371 expressionS ex;
12372 int icnt = 0;
12373
6478892d
TS
12374 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12375 .cpload is ignored. */
12376 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
12377 {
12378 s_ignore (0);
12379 return;
12380 }
12381
d3ecfc59 12382 /* .cpload should be in a .set noreorder section. */
252b5132
RH
12383 if (mips_opts.noreorder == 0)
12384 as_warn (_(".cpload not in noreorder section"));
12385
12386 ex.X_op = O_symbol;
12387 ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
12388 ex.X_op_symbol = NULL;
12389 ex.X_add_number = 0;
12390
12391 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
49309057 12392 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
252b5132 12393
c9914766 12394 macro_build_lui (NULL, &icnt, &ex, mips_gp_register);
17a2f251
TS
12395 macro_build (NULL, &icnt, &ex, "addiu", "t,r,j", mips_gp_register,
12396 mips_gp_register, BFD_RELOC_LO16);
252b5132 12397
17a2f251
TS
12398 macro_build (NULL, &icnt, NULL, "addu", "d,v,t", mips_gp_register,
12399 mips_gp_register, tc_get_register (0));
252b5132
RH
12400
12401 demand_empty_rest_of_line ();
12402}
12403
6478892d
TS
12404/* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
12405 .cpsetup $reg1, offset|$reg2, label
12406
12407 If offset is given, this results in:
12408 sd $gp, offset($sp)
956cd1d6 12409 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
12410 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
12411 daddu $gp, $gp, $reg1
6478892d
TS
12412
12413 If $reg2 is given, this results in:
12414 daddu $reg2, $gp, $0
956cd1d6 12415 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
12416 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
12417 daddu $gp, $gp, $reg1
12418 $reg1 is normally $25 == $t9. */
6478892d 12419static void
17a2f251 12420s_cpsetup (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
12421{
12422 expressionS ex_off;
12423 expressionS ex_sym;
12424 int reg1;
12425 int icnt = 0;
f21f8242 12426 char *f;
6478892d 12427
8586fc66 12428 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
6478892d
TS
12429 We also need NewABI support. */
12430 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12431 {
12432 s_ignore (0);
12433 return;
12434 }
12435
12436 reg1 = tc_get_register (0);
12437 SKIP_WHITESPACE ();
12438 if (*input_line_pointer != ',')
12439 {
12440 as_bad (_("missing argument separator ',' for .cpsetup"));
12441 return;
12442 }
12443 else
80245285 12444 ++input_line_pointer;
6478892d
TS
12445 SKIP_WHITESPACE ();
12446 if (*input_line_pointer == '$')
80245285
TS
12447 {
12448 mips_cpreturn_register = tc_get_register (0);
12449 mips_cpreturn_offset = -1;
12450 }
6478892d 12451 else
80245285
TS
12452 {
12453 mips_cpreturn_offset = get_absolute_expression ();
12454 mips_cpreturn_register = -1;
12455 }
6478892d
TS
12456 SKIP_WHITESPACE ();
12457 if (*input_line_pointer != ',')
12458 {
12459 as_bad (_("missing argument separator ',' for .cpsetup"));
12460 return;
12461 }
12462 else
f9419b05 12463 ++input_line_pointer;
6478892d 12464 SKIP_WHITESPACE ();
f21f8242 12465 expression (&ex_sym);
6478892d
TS
12466
12467 if (mips_cpreturn_register == -1)
12468 {
12469 ex_off.X_op = O_constant;
12470 ex_off.X_add_symbol = NULL;
12471 ex_off.X_op_symbol = NULL;
12472 ex_off.X_add_number = mips_cpreturn_offset;
12473
17a2f251
TS
12474 macro_build (NULL, &icnt, &ex_off, "sd", "t,o(b)", mips_gp_register,
12475 BFD_RELOC_LO16, SP);
6478892d
TS
12476 }
12477 else
17a2f251
TS
12478 macro_build (NULL, &icnt, NULL, "daddu", "d,v,t", mips_cpreturn_register,
12479 mips_gp_register, 0);
6478892d 12480
f21f8242
AO
12481 /* Ensure there's room for the next two instructions, so that `f'
12482 doesn't end up with an address in the wrong frag. */
12483 frag_grow (8);
12484 f = frag_more (0);
17a2f251
TS
12485 macro_build (NULL, &icnt, &ex_sym, "lui", "t,u", mips_gp_register,
12486 BFD_RELOC_GPREL16);
f21f8242 12487 fix_new (frag_now, f - frag_now->fr_literal,
a105a300 12488 8, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
f21f8242 12489 fix_new (frag_now, f - frag_now->fr_literal,
a105a300 12490 4, NULL, 0, 0, BFD_RELOC_HI16_S);
f21f8242
AO
12491
12492 f = frag_more (0);
17a2f251
TS
12493 macro_build (NULL, &icnt, &ex_sym, "addiu", "t,r,j", mips_gp_register,
12494 mips_gp_register, BFD_RELOC_GPREL16);
f21f8242 12495 fix_new (frag_now, f - frag_now->fr_literal,
a105a300 12496 8, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
f21f8242 12497 fix_new (frag_now, f - frag_now->fr_literal,
a105a300 12498 4, NULL, 0, 0, BFD_RELOC_LO16);
f21f8242 12499
17a2f251
TS
12500 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
12501 mips_gp_register, reg1);
6478892d
TS
12502
12503 demand_empty_rest_of_line ();
12504}
12505
12506static void
17a2f251 12507s_cplocal (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
12508{
12509 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
12510 .cplocal is ignored. */
12511 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12512 {
12513 s_ignore (0);
12514 return;
12515 }
12516
12517 mips_gp_register = tc_get_register (0);
85b51719 12518 demand_empty_rest_of_line ();
6478892d
TS
12519}
12520
252b5132
RH
12521/* Handle the .cprestore pseudo-op. This stores $gp into a given
12522 offset from $sp. The offset is remembered, and after making a PIC
12523 call $gp is restored from that location. */
12524
12525static void
17a2f251 12526s_cprestore (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
12527{
12528 expressionS ex;
12529 int icnt = 0;
12530
6478892d 12531 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
c9914766 12532 .cprestore is ignored. */
6478892d 12533 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
12534 {
12535 s_ignore (0);
12536 return;
12537 }
12538
12539 mips_cprestore_offset = get_absolute_expression ();
7a621144 12540 mips_cprestore_valid = 1;
252b5132
RH
12541
12542 ex.X_op = O_constant;
12543 ex.X_add_symbol = NULL;
12544 ex.X_op_symbol = NULL;
12545 ex.X_add_number = mips_cprestore_offset;
12546
17a2f251 12547 macro_build_ldst_constoffset (NULL, &icnt, &ex, ADDRESS_STORE_INSN,
256ab948 12548 mips_gp_register, SP, HAVE_64BIT_ADDRESSES);
252b5132
RH
12549
12550 demand_empty_rest_of_line ();
12551}
12552
6478892d 12553/* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
67c1ffbe 12554 was given in the preceding .cpsetup, it results in:
6478892d 12555 ld $gp, offset($sp)
76b3015f 12556
6478892d 12557 If a register $reg2 was given there, it results in:
609f23f4 12558 daddu $gp, $reg2, $0
6478892d
TS
12559 */
12560static void
17a2f251 12561s_cpreturn (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
12562{
12563 expressionS ex;
12564 int icnt = 0;
12565
12566 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
12567 We also need NewABI support. */
12568 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12569 {
12570 s_ignore (0);
12571 return;
12572 }
12573
12574 if (mips_cpreturn_register == -1)
12575 {
12576 ex.X_op = O_constant;
12577 ex.X_add_symbol = NULL;
12578 ex.X_op_symbol = NULL;
12579 ex.X_add_number = mips_cpreturn_offset;
12580
17a2f251
TS
12581 macro_build (NULL, &icnt, &ex, "ld", "t,o(b)", mips_gp_register,
12582 BFD_RELOC_LO16, SP);
6478892d
TS
12583 }
12584 else
17a2f251
TS
12585 macro_build (NULL, &icnt, NULL, "daddu", "d,v,t", mips_gp_register,
12586 mips_cpreturn_register, 0);
6478892d
TS
12587
12588 demand_empty_rest_of_line ();
12589}
12590
12591/* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
12592 code. It sets the offset to use in gp_rel relocations. */
12593
12594static void
17a2f251 12595s_gpvalue (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
12596{
12597 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
12598 We also need NewABI support. */
12599 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12600 {
12601 s_ignore (0);
12602 return;
12603 }
12604
def2e0dd 12605 mips_gprel_offset = get_absolute_expression ();
6478892d
TS
12606
12607 demand_empty_rest_of_line ();
12608}
12609
252b5132
RH
12610/* Handle the .gpword pseudo-op. This is used when generating PIC
12611 code. It generates a 32 bit GP relative reloc. */
12612
12613static void
17a2f251 12614s_gpword (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
12615{
12616 symbolS *label;
12617 expressionS ex;
12618 char *p;
12619
12620 /* When not generating PIC code, this is treated as .word. */
12621 if (mips_pic != SVR4_PIC)
12622 {
12623 s_cons (2);
12624 return;
12625 }
12626
12627 label = insn_labels != NULL ? insn_labels->label : NULL;
b34976b6 12628 mips_emit_delays (TRUE);
252b5132
RH
12629 if (auto_align)
12630 mips_align (2, 0, label);
12631 mips_clear_insn_labels ();
12632
12633 expression (&ex);
12634
12635 if (ex.X_op != O_symbol || ex.X_add_number != 0)
12636 {
12637 as_bad (_("Unsupported use of .gpword"));
12638 ignore_rest_of_line ();
12639 }
12640
12641 p = frag_more (4);
17a2f251 12642 md_number_to_chars (p, 0, 4);
b34976b6 12643 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
cdf6fd85 12644 BFD_RELOC_GPREL32);
252b5132
RH
12645
12646 demand_empty_rest_of_line ();
12647}
12648
10181a0d 12649static void
17a2f251 12650s_gpdword (int ignore ATTRIBUTE_UNUSED)
10181a0d
AO
12651{
12652 symbolS *label;
12653 expressionS ex;
12654 char *p;
12655
12656 /* When not generating PIC code, this is treated as .dword. */
12657 if (mips_pic != SVR4_PIC)
12658 {
12659 s_cons (3);
12660 return;
12661 }
12662
12663 label = insn_labels != NULL ? insn_labels->label : NULL;
b34976b6 12664 mips_emit_delays (TRUE);
10181a0d
AO
12665 if (auto_align)
12666 mips_align (3, 0, label);
12667 mips_clear_insn_labels ();
12668
12669 expression (&ex);
12670
12671 if (ex.X_op != O_symbol || ex.X_add_number != 0)
12672 {
12673 as_bad (_("Unsupported use of .gpdword"));
12674 ignore_rest_of_line ();
12675 }
12676
12677 p = frag_more (8);
17a2f251 12678 md_number_to_chars (p, 0, 8);
a105a300 12679 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
10181a0d
AO
12680 BFD_RELOC_GPREL32);
12681
12682 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
12683 ex.X_op = O_absent;
12684 ex.X_add_symbol = 0;
12685 ex.X_add_number = 0;
b34976b6 12686 fix_new_exp (frag_now, p - frag_now->fr_literal, 8, &ex, FALSE,
10181a0d
AO
12687 BFD_RELOC_64);
12688
12689 demand_empty_rest_of_line ();
12690}
12691
252b5132
RH
12692/* Handle the .cpadd pseudo-op. This is used when dealing with switch
12693 tables in SVR4 PIC code. */
12694
12695static void
17a2f251 12696s_cpadd (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
12697{
12698 int icnt = 0;
12699 int reg;
12700
10181a0d
AO
12701 /* This is ignored when not generating SVR4 PIC code. */
12702 if (mips_pic != SVR4_PIC)
252b5132
RH
12703 {
12704 s_ignore (0);
12705 return;
12706 }
12707
12708 /* Add $gp to the register named as an argument. */
12709 reg = tc_get_register (0);
17a2f251
TS
12710 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
12711 reg, reg, mips_gp_register);
252b5132 12712
bdaaa2e1 12713 demand_empty_rest_of_line ();
252b5132
RH
12714}
12715
12716/* Handle the .insn pseudo-op. This marks instruction labels in
12717 mips16 mode. This permits the linker to handle them specially,
12718 such as generating jalx instructions when needed. We also make
12719 them odd for the duration of the assembly, in order to generate the
12720 right sort of code. We will make them even in the adjust_symtab
12721 routine, while leaving them marked. This is convenient for the
12722 debugger and the disassembler. The linker knows to make them odd
12723 again. */
12724
12725static void
17a2f251 12726s_insn (int ignore ATTRIBUTE_UNUSED)
252b5132 12727{
f9419b05 12728 mips16_mark_labels ();
252b5132
RH
12729
12730 demand_empty_rest_of_line ();
12731}
12732
12733/* Handle a .stabn directive. We need these in order to mark a label
12734 as being a mips16 text label correctly. Sometimes the compiler
12735 will emit a label, followed by a .stabn, and then switch sections.
12736 If the label and .stabn are in mips16 mode, then the label is
12737 really a mips16 text label. */
12738
12739static void
17a2f251 12740s_mips_stab (int type)
252b5132 12741{
f9419b05 12742 if (type == 'n')
252b5132
RH
12743 mips16_mark_labels ();
12744
12745 s_stab (type);
12746}
12747
12748/* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
12749 */
12750
12751static void
17a2f251 12752s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
12753{
12754 char *name;
12755 int c;
12756 symbolS *symbolP;
12757 expressionS exp;
12758
12759 name = input_line_pointer;
12760 c = get_symbol_end ();
12761 symbolP = symbol_find_or_make (name);
12762 S_SET_WEAK (symbolP);
12763 *input_line_pointer = c;
12764
12765 SKIP_WHITESPACE ();
12766
12767 if (! is_end_of_line[(unsigned char) *input_line_pointer])
12768 {
12769 if (S_IS_DEFINED (symbolP))
12770 {
956cd1d6 12771 as_bad ("ignoring attempt to redefine symbol %s",
252b5132
RH
12772 S_GET_NAME (symbolP));
12773 ignore_rest_of_line ();
12774 return;
12775 }
bdaaa2e1 12776
252b5132
RH
12777 if (*input_line_pointer == ',')
12778 {
12779 ++input_line_pointer;
12780 SKIP_WHITESPACE ();
12781 }
bdaaa2e1 12782
252b5132
RH
12783 expression (&exp);
12784 if (exp.X_op != O_symbol)
12785 {
12786 as_bad ("bad .weakext directive");
98d3f06f 12787 ignore_rest_of_line ();
252b5132
RH
12788 return;
12789 }
49309057 12790 symbol_set_value_expression (symbolP, &exp);
252b5132
RH
12791 }
12792
12793 demand_empty_rest_of_line ();
12794}
12795
12796/* Parse a register string into a number. Called from the ECOFF code
12797 to parse .frame. The argument is non-zero if this is the frame
12798 register, so that we can record it in mips_frame_reg. */
12799
12800int
17a2f251 12801tc_get_register (int frame)
252b5132
RH
12802{
12803 int reg;
12804
12805 SKIP_WHITESPACE ();
12806 if (*input_line_pointer++ != '$')
12807 {
12808 as_warn (_("expected `$'"));
85b51719 12809 reg = ZERO;
252b5132 12810 }
3882b010 12811 else if (ISDIGIT (*input_line_pointer))
252b5132
RH
12812 {
12813 reg = get_absolute_expression ();
12814 if (reg < 0 || reg >= 32)
12815 {
12816 as_warn (_("Bad register number"));
85b51719 12817 reg = ZERO;
252b5132
RH
12818 }
12819 }
12820 else
12821 {
76db943d 12822 if (strncmp (input_line_pointer, "ra", 2) == 0)
85b51719
TS
12823 {
12824 reg = RA;
12825 input_line_pointer += 2;
12826 }
76db943d 12827 else if (strncmp (input_line_pointer, "fp", 2) == 0)
85b51719
TS
12828 {
12829 reg = FP;
12830 input_line_pointer += 2;
12831 }
252b5132 12832 else if (strncmp (input_line_pointer, "sp", 2) == 0)
85b51719
TS
12833 {
12834 reg = SP;
12835 input_line_pointer += 2;
12836 }
252b5132 12837 else if (strncmp (input_line_pointer, "gp", 2) == 0)
85b51719
TS
12838 {
12839 reg = GP;
12840 input_line_pointer += 2;
12841 }
252b5132 12842 else if (strncmp (input_line_pointer, "at", 2) == 0)
85b51719
TS
12843 {
12844 reg = AT;
12845 input_line_pointer += 2;
12846 }
12847 else if (strncmp (input_line_pointer, "kt0", 3) == 0)
12848 {
12849 reg = KT0;
12850 input_line_pointer += 3;
12851 }
12852 else if (strncmp (input_line_pointer, "kt1", 3) == 0)
12853 {
12854 reg = KT1;
12855 input_line_pointer += 3;
12856 }
12857 else if (strncmp (input_line_pointer, "zero", 4) == 0)
12858 {
12859 reg = ZERO;
12860 input_line_pointer += 4;
12861 }
252b5132
RH
12862 else
12863 {
12864 as_warn (_("Unrecognized register name"));
85b51719
TS
12865 reg = ZERO;
12866 while (ISALNUM(*input_line_pointer))
12867 input_line_pointer++;
252b5132 12868 }
252b5132
RH
12869 }
12870 if (frame)
7a621144
DJ
12871 {
12872 mips_frame_reg = reg != 0 ? reg : SP;
12873 mips_frame_reg_valid = 1;
12874 mips_cprestore_valid = 0;
12875 }
252b5132
RH
12876 return reg;
12877}
12878
12879valueT
17a2f251 12880md_section_align (asection *seg, valueT addr)
252b5132
RH
12881{
12882 int align = bfd_get_section_alignment (stdoutput, seg);
12883
12884#ifdef OBJ_ELF
12885 /* We don't need to align ELF sections to the full alignment.
12886 However, Irix 5 may prefer that we align them at least to a 16
12887 byte boundary. We don't bother to align the sections if we are
12888 targeted for an embedded system. */
12889 if (strcmp (TARGET_OS, "elf") == 0)
12890 return addr;
12891 if (align > 4)
12892 align = 4;
12893#endif
12894
12895 return ((addr + (1 << align) - 1) & (-1 << align));
12896}
12897
12898/* Utility routine, called from above as well. If called while the
12899 input file is still being read, it's only an approximation. (For
12900 example, a symbol may later become defined which appeared to be
12901 undefined earlier.) */
12902
12903static int
17a2f251 12904nopic_need_relax (symbolS *sym, int before_relaxing)
252b5132
RH
12905{
12906 if (sym == 0)
12907 return 0;
12908
6478892d 12909 if (USE_GLOBAL_POINTER_OPT && g_switch_value > 0)
252b5132
RH
12910 {
12911 const char *symname;
12912 int change;
12913
c9914766 12914 /* Find out whether this symbol can be referenced off the $gp
252b5132
RH
12915 register. It can be if it is smaller than the -G size or if
12916 it is in the .sdata or .sbss section. Certain symbols can
c9914766 12917 not be referenced off the $gp, although it appears as though
252b5132
RH
12918 they can. */
12919 symname = S_GET_NAME (sym);
12920 if (symname != (const char *) NULL
12921 && (strcmp (symname, "eprol") == 0
12922 || strcmp (symname, "etext") == 0
12923 || strcmp (symname, "_gp") == 0
12924 || strcmp (symname, "edata") == 0
12925 || strcmp (symname, "_fbss") == 0
12926 || strcmp (symname, "_fdata") == 0
12927 || strcmp (symname, "_ftext") == 0
12928 || strcmp (symname, "end") == 0
12929 || strcmp (symname, "_gp_disp") == 0))
12930 change = 1;
12931 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
12932 && (0
12933#ifndef NO_ECOFF_DEBUGGING
49309057
ILT
12934 || (symbol_get_obj (sym)->ecoff_extern_size != 0
12935 && (symbol_get_obj (sym)->ecoff_extern_size
12936 <= g_switch_value))
252b5132
RH
12937#endif
12938 /* We must defer this decision until after the whole
12939 file has been read, since there might be a .extern
12940 after the first use of this symbol. */
12941 || (before_relaxing
12942#ifndef NO_ECOFF_DEBUGGING
49309057 12943 && symbol_get_obj (sym)->ecoff_extern_size == 0
252b5132
RH
12944#endif
12945 && S_GET_VALUE (sym) == 0)
12946 || (S_GET_VALUE (sym) != 0
12947 && S_GET_VALUE (sym) <= g_switch_value)))
12948 change = 0;
12949 else
12950 {
12951 const char *segname;
12952
12953 segname = segment_name (S_GET_SEGMENT (sym));
12954 assert (strcmp (segname, ".lit8") != 0
12955 && strcmp (segname, ".lit4") != 0);
12956 change = (strcmp (segname, ".sdata") != 0
fba2b7f9
GK
12957 && strcmp (segname, ".sbss") != 0
12958 && strncmp (segname, ".sdata.", 7) != 0
12959 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
252b5132
RH
12960 }
12961 return change;
12962 }
12963 else
c9914766 12964 /* We are not optimizing for the $gp register. */
252b5132
RH
12965 return 1;
12966}
12967
5919d012
RS
12968
12969/* Return true if the given symbol should be considered local for SVR4 PIC. */
12970
12971static bfd_boolean
17a2f251 12972pic_need_relax (symbolS *sym, asection *segtype)
5919d012
RS
12973{
12974 asection *symsec;
12975 bfd_boolean linkonce;
12976
12977 /* Handle the case of a symbol equated to another symbol. */
12978 while (symbol_equated_reloc_p (sym))
12979 {
12980 symbolS *n;
12981
12982 /* It's possible to get a loop here in a badly written
12983 program. */
12984 n = symbol_get_value_expression (sym)->X_add_symbol;
12985 if (n == sym)
12986 break;
12987 sym = n;
12988 }
12989
12990 symsec = S_GET_SEGMENT (sym);
12991
12992 /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
12993 linkonce = FALSE;
12994 if (symsec != segtype && ! S_IS_LOCAL (sym))
12995 {
12996 if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
12997 != 0)
12998 linkonce = TRUE;
12999
13000 /* The GNU toolchain uses an extension for ELF: a section
13001 beginning with the magic string .gnu.linkonce is a linkonce
13002 section. */
13003 if (strncmp (segment_name (symsec), ".gnu.linkonce",
13004 sizeof ".gnu.linkonce" - 1) == 0)
13005 linkonce = TRUE;
13006 }
13007
13008 /* This must duplicate the test in adjust_reloc_syms. */
13009 return (symsec != &bfd_und_section
13010 && symsec != &bfd_abs_section
13011 && ! bfd_is_com_section (symsec)
13012 && !linkonce
13013#ifdef OBJ_ELF
13014 /* A global or weak symbol is treated as external. */
13015 && (OUTPUT_FLAVOR != bfd_target_elf_flavour
13016 || (! S_IS_WEAK (sym)
13017 && (! S_IS_EXTERNAL (sym)
13018 || mips_pic == EMBEDDED_PIC)))
13019#endif
13020 );
13021}
13022
13023
252b5132
RH
13024/* Given a mips16 variant frag FRAGP, return non-zero if it needs an
13025 extended opcode. SEC is the section the frag is in. */
13026
13027static int
17a2f251 13028mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
252b5132
RH
13029{
13030 int type;
13031 register const struct mips16_immed_operand *op;
13032 offsetT val;
13033 int mintiny, maxtiny;
13034 segT symsec;
98aa84af 13035 fragS *sym_frag;
252b5132
RH
13036
13037 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
13038 return 0;
13039 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
13040 return 1;
13041
13042 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
13043 op = mips16_immed_operands;
13044 while (op->type != type)
13045 {
13046 ++op;
13047 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
13048 }
13049
13050 if (op->unsp)
13051 {
13052 if (type == '<' || type == '>' || type == '[' || type == ']')
13053 {
13054 mintiny = 1;
13055 maxtiny = 1 << op->nbits;
13056 }
13057 else
13058 {
13059 mintiny = 0;
13060 maxtiny = (1 << op->nbits) - 1;
13061 }
13062 }
13063 else
13064 {
13065 mintiny = - (1 << (op->nbits - 1));
13066 maxtiny = (1 << (op->nbits - 1)) - 1;
13067 }
13068
98aa84af 13069 sym_frag = symbol_get_frag (fragp->fr_symbol);
ac62c346 13070 val = S_GET_VALUE (fragp->fr_symbol);
98aa84af 13071 symsec = S_GET_SEGMENT (fragp->fr_symbol);
252b5132
RH
13072
13073 if (op->pcrel)
13074 {
13075 addressT addr;
13076
13077 /* We won't have the section when we are called from
13078 mips_relax_frag. However, we will always have been called
13079 from md_estimate_size_before_relax first. If this is a
13080 branch to a different section, we mark it as such. If SEC is
13081 NULL, and the frag is not marked, then it must be a branch to
13082 the same section. */
13083 if (sec == NULL)
13084 {
13085 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
13086 return 1;
13087 }
13088 else
13089 {
98aa84af 13090 /* Must have been called from md_estimate_size_before_relax. */
252b5132
RH
13091 if (symsec != sec)
13092 {
13093 fragp->fr_subtype =
13094 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13095
13096 /* FIXME: We should support this, and let the linker
13097 catch branches and loads that are out of range. */
13098 as_bad_where (fragp->fr_file, fragp->fr_line,
13099 _("unsupported PC relative reference to different section"));
13100
13101 return 1;
13102 }
98aa84af
AM
13103 if (fragp != sym_frag && sym_frag->fr_address == 0)
13104 /* Assume non-extended on the first relaxation pass.
13105 The address we have calculated will be bogus if this is
13106 a forward branch to another frag, as the forward frag
13107 will have fr_address == 0. */
13108 return 0;
252b5132
RH
13109 }
13110
13111 /* In this case, we know for sure that the symbol fragment is in
98aa84af
AM
13112 the same section. If the relax_marker of the symbol fragment
13113 differs from the relax_marker of this fragment, we have not
13114 yet adjusted the symbol fragment fr_address. We want to add
252b5132
RH
13115 in STRETCH in order to get a better estimate of the address.
13116 This particularly matters because of the shift bits. */
13117 if (stretch != 0
98aa84af 13118 && sym_frag->relax_marker != fragp->relax_marker)
252b5132
RH
13119 {
13120 fragS *f;
13121
13122 /* Adjust stretch for any alignment frag. Note that if have
13123 been expanding the earlier code, the symbol may be
13124 defined in what appears to be an earlier frag. FIXME:
13125 This doesn't handle the fr_subtype field, which specifies
13126 a maximum number of bytes to skip when doing an
13127 alignment. */
98aa84af 13128 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
252b5132
RH
13129 {
13130 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
13131 {
13132 if (stretch < 0)
13133 stretch = - ((- stretch)
13134 & ~ ((1 << (int) f->fr_offset) - 1));
13135 else
13136 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
13137 if (stretch == 0)
13138 break;
13139 }
13140 }
13141 if (f != NULL)
13142 val += stretch;
13143 }
13144
13145 addr = fragp->fr_address + fragp->fr_fix;
13146
13147 /* The base address rules are complicated. The base address of
13148 a branch is the following instruction. The base address of a
13149 PC relative load or add is the instruction itself, but if it
13150 is in a delay slot (in which case it can not be extended) use
13151 the address of the instruction whose delay slot it is in. */
13152 if (type == 'p' || type == 'q')
13153 {
13154 addr += 2;
13155
13156 /* If we are currently assuming that this frag should be
13157 extended, then, the current address is two bytes
bdaaa2e1 13158 higher. */
252b5132
RH
13159 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13160 addr += 2;
13161
13162 /* Ignore the low bit in the target, since it will be set
13163 for a text label. */
13164 if ((val & 1) != 0)
13165 --val;
13166 }
13167 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13168 addr -= 4;
13169 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13170 addr -= 2;
13171
13172 val -= addr & ~ ((1 << op->shift) - 1);
13173
13174 /* Branch offsets have an implicit 0 in the lowest bit. */
13175 if (type == 'p' || type == 'q')
13176 val /= 2;
13177
13178 /* If any of the shifted bits are set, we must use an extended
13179 opcode. If the address depends on the size of this
13180 instruction, this can lead to a loop, so we arrange to always
13181 use an extended opcode. We only check this when we are in
13182 the main relaxation loop, when SEC is NULL. */
13183 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
13184 {
13185 fragp->fr_subtype =
13186 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13187 return 1;
13188 }
13189
13190 /* If we are about to mark a frag as extended because the value
13191 is precisely maxtiny + 1, then there is a chance of an
13192 infinite loop as in the following code:
13193 la $4,foo
13194 .skip 1020
13195 .align 2
13196 foo:
13197 In this case when the la is extended, foo is 0x3fc bytes
13198 away, so the la can be shrunk, but then foo is 0x400 away, so
13199 the la must be extended. To avoid this loop, we mark the
13200 frag as extended if it was small, and is about to become
13201 extended with a value of maxtiny + 1. */
13202 if (val == ((maxtiny + 1) << op->shift)
13203 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
13204 && sec == NULL)
13205 {
13206 fragp->fr_subtype =
13207 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13208 return 1;
13209 }
13210 }
13211 else if (symsec != absolute_section && sec != NULL)
13212 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
13213
13214 if ((val & ((1 << op->shift) - 1)) != 0
13215 || val < (mintiny << op->shift)
13216 || val > (maxtiny << op->shift))
13217 return 1;
13218 else
13219 return 0;
13220}
13221
4a6a3df4
AO
13222/* Compute the length of a branch sequence, and adjust the
13223 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
13224 worst-case length is computed, with UPDATE being used to indicate
13225 whether an unconditional (-1), branch-likely (+1) or regular (0)
13226 branch is to be computed. */
13227static int
17a2f251 13228relaxed_branch_length (fragS *fragp, asection *sec, int update)
4a6a3df4 13229{
b34976b6 13230 bfd_boolean toofar;
4a6a3df4
AO
13231 int length;
13232
13233 if (fragp
13234 && S_IS_DEFINED (fragp->fr_symbol)
13235 && sec == S_GET_SEGMENT (fragp->fr_symbol))
13236 {
13237 addressT addr;
13238 offsetT val;
13239
13240 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
13241
13242 addr = fragp->fr_address + fragp->fr_fix + 4;
13243
13244 val -= addr;
13245
13246 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
13247 }
13248 else if (fragp)
13249 /* If the symbol is not defined or it's in a different segment,
13250 assume the user knows what's going on and emit a short
13251 branch. */
b34976b6 13252 toofar = FALSE;
4a6a3df4 13253 else
b34976b6 13254 toofar = TRUE;
4a6a3df4
AO
13255
13256 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13257 fragp->fr_subtype
af6ae2ad 13258 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_UNCOND (fragp->fr_subtype),
4a6a3df4
AO
13259 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
13260 RELAX_BRANCH_LINK (fragp->fr_subtype),
13261 toofar);
13262
13263 length = 4;
13264 if (toofar)
13265 {
13266 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
13267 length += 8;
13268
13269 if (mips_pic != NO_PIC)
13270 {
13271 /* Additional space for PIC loading of target address. */
13272 length += 8;
13273 if (mips_opts.isa == ISA_MIPS1)
13274 /* Additional space for $at-stabilizing nop. */
13275 length += 4;
13276 }
13277
13278 /* If branch is conditional. */
13279 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
13280 length += 8;
13281 }
b34976b6 13282
4a6a3df4
AO
13283 return length;
13284}
13285
252b5132
RH
13286/* Estimate the size of a frag before relaxing. Unless this is the
13287 mips16, we are not really relaxing here, and the final size is
13288 encoded in the subtype information. For the mips16, we have to
13289 decide whether we are using an extended opcode or not. */
13290
252b5132 13291int
17a2f251 13292md_estimate_size_before_relax (fragS *fragp, asection *segtype)
252b5132 13293{
5919d012 13294 int change;
252b5132 13295
4a6a3df4
AO
13296 if (RELAX_BRANCH_P (fragp->fr_subtype))
13297 {
13298
b34976b6
AM
13299 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
13300
4a6a3df4
AO
13301 return fragp->fr_var;
13302 }
13303
252b5132 13304 if (RELAX_MIPS16_P (fragp->fr_subtype))
177b4a6a
AO
13305 /* We don't want to modify the EXTENDED bit here; it might get us
13306 into infinite loops. We change it only in mips_relax_frag(). */
13307 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
252b5132
RH
13308
13309 if (mips_pic == NO_PIC)
5919d012 13310 change = nopic_need_relax (fragp->fr_symbol, 0);
252b5132 13311 else if (mips_pic == SVR4_PIC)
5919d012 13312 change = pic_need_relax (fragp->fr_symbol, segtype);
252b5132
RH
13313 else
13314 abort ();
13315
13316 if (change)
13317 {
13318 /* Record the offset to the first reloc in the fr_opcode field.
13319 This lets md_convert_frag and tc_gen_reloc know that the code
13320 must be expanded. */
13321 fragp->fr_opcode = (fragp->fr_literal
13322 + fragp->fr_fix
13323 - RELAX_OLD (fragp->fr_subtype)
13324 + RELAX_RELOC1 (fragp->fr_subtype));
13325 /* FIXME: This really needs as_warn_where. */
13326 if (RELAX_WARN (fragp->fr_subtype))
9a41af64
TS
13327 as_warn (_("AT used after \".set noat\" or macro used after "
13328 "\".set nomacro\""));
13329
13330 return RELAX_NEW (fragp->fr_subtype) - RELAX_OLD (fragp->fr_subtype);
252b5132
RH
13331 }
13332
9a41af64 13333 return 0;
252b5132
RH
13334}
13335
13336/* This is called to see whether a reloc against a defined symbol
13337 should be converted into a reloc against a section. Don't adjust
13338 MIPS16 jump relocations, so we don't have to worry about the format
13339 of the offset in the .o file. Don't adjust relocations against
13340 mips16 symbols, so that the linker can find them if it needs to set
13341 up a stub. */
13342
13343int
17a2f251 13344mips_fix_adjustable (fixS *fixp)
252b5132
RH
13345{
13346 if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
13347 return 0;
a161fe53 13348
252b5132
RH
13349 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
13350 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13351 return 0;
a161fe53 13352
252b5132
RH
13353 if (fixp->fx_addsy == NULL)
13354 return 1;
a161fe53 13355
252b5132
RH
13356#ifdef OBJ_ELF
13357 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
13358 && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
13359 && fixp->fx_subsy == NULL)
13360 return 0;
13361#endif
a161fe53 13362
252b5132
RH
13363 return 1;
13364}
13365
13366/* Translate internal representation of relocation info to BFD target
13367 format. */
13368
13369arelent **
17a2f251 13370tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
13371{
13372 static arelent *retval[4];
13373 arelent *reloc;
13374 bfd_reloc_code_real_type code;
13375
4b0cff4e
TS
13376 memset (retval, 0, sizeof(retval));
13377 reloc = retval[0] = (arelent *) xcalloc (1, sizeof (arelent));
49309057
ILT
13378 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13379 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
13380 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
13381
13382 if (mips_pic == EMBEDDED_PIC
13383 && SWITCH_TABLE (fixp))
13384 {
13385 /* For a switch table entry we use a special reloc. The addend
13386 is actually the difference between the reloc address and the
13387 subtrahend. */
13388 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
13389 if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
13390 as_fatal (_("Double check fx_r_type in tc-mips.c:tc_gen_reloc"));
13391 fixp->fx_r_type = BFD_RELOC_GPREL32;
13392 }
13393 else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16)
13394 {
4514d474
CD
13395 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
13396 reloc->addend = fixp->fx_addnumber;
252b5132 13397 else
4514d474
CD
13398 {
13399 /* We use a special addend for an internal RELLO reloc. */
13400 if (symbol_section_p (fixp->fx_addsy))
13401 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
13402 else
13403 reloc->addend = fixp->fx_addnumber + reloc->address;
13404 }
252b5132
RH
13405 }
13406 else if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
13407 {
13408 assert (fixp->fx_next != NULL
13409 && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
4514d474
CD
13410
13411 /* The reloc is relative to the RELLO; adjust the addend
252b5132 13412 accordingly. */
4514d474
CD
13413 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
13414 reloc->addend = fixp->fx_next->fx_addnumber;
252b5132 13415 else
4514d474
CD
13416 {
13417 /* We use a special addend for an internal RELHI reloc. */
13418 if (symbol_section_p (fixp->fx_addsy))
13419 reloc->addend = (fixp->fx_next->fx_frag->fr_address
13420 + fixp->fx_next->fx_where
13421 - S_GET_VALUE (fixp->fx_subsy));
13422 else
13423 reloc->addend = (fixp->fx_addnumber
13424 + fixp->fx_next->fx_frag->fr_address
13425 + fixp->fx_next->fx_where);
13426 }
252b5132 13427 }
4514d474
CD
13428 else if (fixp->fx_pcrel == 0 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
13429 reloc->addend = fixp->fx_addnumber;
252b5132
RH
13430 else
13431 {
13432 if (OUTPUT_FLAVOR != bfd_target_aout_flavour)
13433 /* A gruesome hack which is a result of the gruesome gas reloc
13434 handling. */
13435 reloc->addend = reloc->address;
13436 else
13437 reloc->addend = -reloc->address;
13438 }
13439
13440 /* If this is a variant frag, we may need to adjust the existing
13441 reloc and generate a new one. */
13442 if (fixp->fx_frag->fr_opcode != NULL
ed6fb7bd
SC
13443 && ((fixp->fx_r_type == BFD_RELOC_GPREL16
13444 && ! HAVE_NEWABI)
f5040a92
AO
13445 || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_DISP
13446 && HAVE_NEWABI)
252b5132
RH
13447 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
13448 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL16
13449 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
13450 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16
13451 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
6478892d 13452 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_LO16)
ed6fb7bd 13453 )
252b5132
RH
13454 {
13455 arelent *reloc2;
13456
13457 assert (! RELAX_MIPS16_P (fixp->fx_frag->fr_subtype));
13458
13459 /* If this is not the last reloc in this frag, then we have two
13460 GPREL relocs, or a GOT_HI16/GOT_LO16 pair, or a
13461 CALL_HI16/CALL_LO16, both of which are being replaced. Let
13462 the second one handle all of them. */
13463 if (fixp->fx_next != NULL
13464 && fixp->fx_frag == fixp->fx_next->fx_frag)
13465 {
cdf6fd85
TS
13466 assert ((fixp->fx_r_type == BFD_RELOC_GPREL16
13467 && fixp->fx_next->fx_r_type == BFD_RELOC_GPREL16)
252b5132
RH
13468 || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
13469 && (fixp->fx_next->fx_r_type
13470 == BFD_RELOC_MIPS_GOT_LO16))
13471 || (fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
13472 && (fixp->fx_next->fx_r_type
13473 == BFD_RELOC_MIPS_CALL_LO16)));
13474 retval[0] = NULL;
13475 return retval;
13476 }
13477
13478 fixp->fx_where = fixp->fx_frag->fr_opcode - fixp->fx_frag->fr_literal;
13479 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
f5040a92 13480 reloc->addend += fixp->fx_frag->tc_frag_data.tc_fr_offset;
252b5132 13481 reloc2 = retval[1] = (arelent *) xmalloc (sizeof (arelent));
49309057
ILT
13482 reloc2->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13483 *reloc2->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
13484 reloc2->address = (reloc->address
13485 + (RELAX_RELOC2 (fixp->fx_frag->fr_subtype)
13486 - RELAX_RELOC1 (fixp->fx_frag->fr_subtype)));
a7ebbfdf 13487 reloc2->addend = fixp->fx_addnumber - S_GET_VALUE (fixp->fx_addsy)
f5040a92 13488 + fixp->fx_frag->tc_frag_data.tc_fr_offset;
252b5132
RH
13489 reloc2->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO16);
13490 assert (reloc2->howto != NULL);
13491
13492 if (RELAX_RELOC3 (fixp->fx_frag->fr_subtype))
13493 {
13494 arelent *reloc3;
13495
13496 reloc3 = retval[2] = (arelent *) xmalloc (sizeof (arelent));
252b5132
RH
13497 *reloc3 = *reloc2;
13498 reloc3->address += 4;
13499 }
13500
13501 if (mips_pic == NO_PIC)
13502 {
cdf6fd85 13503 assert (fixp->fx_r_type == BFD_RELOC_GPREL16);
252b5132
RH
13504 fixp->fx_r_type = BFD_RELOC_HI16_S;
13505 }
13506 else if (mips_pic == SVR4_PIC)
13507 {
13508 switch (fixp->fx_r_type)
13509 {
13510 default:
13511 abort ();
13512 case BFD_RELOC_MIPS_GOT16:
13513 break;
252b5132
RH
13514 case BFD_RELOC_MIPS_GOT_LO16:
13515 case BFD_RELOC_MIPS_CALL_LO16:
ed6fb7bd
SC
13516 if (HAVE_NEWABI)
13517 {
ed6fb7bd
SC
13518 fixp->fx_r_type = BFD_RELOC_MIPS_GOT_PAGE;
13519 reloc2->howto = bfd_reloc_type_lookup
13520 (stdoutput, BFD_RELOC_MIPS_GOT_OFST);
13521 }
13522 else
13523 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
13524 break;
f5040a92
AO
13525 case BFD_RELOC_MIPS_CALL16:
13526 case BFD_RELOC_MIPS_GOT_OFST:
13527 case BFD_RELOC_MIPS_GOT_DISP:
13528 if (HAVE_NEWABI)
13529 {
13530 /* It may seem nonsensical to relax GOT_DISP to
13531 GOT_DISP, but we're actually turning a GOT_DISP
13532 without offset into a GOT_DISP with an offset,
13533 getting rid of the separate addition, which we can
13534 do when the symbol is found to be local. */
13535 fixp->fx_r_type = BFD_RELOC_MIPS_GOT_DISP;
13536 retval[1] = NULL;
13537 }
13538 else
13539 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
13540 break;
252b5132
RH
13541 }
13542 }
13543 else
13544 abort ();
13545 }
13546
438c16b8
TS
13547 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
13548 entry to be used in the relocation's section offset. */
13549 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
252b5132
RH
13550 {
13551 reloc->address = reloc->addend;
13552 reloc->addend = 0;
13553 }
13554
13555 /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
13556 fixup_segment converted a non-PC relative reloc into a PC
13557 relative reloc. In such a case, we need to convert the reloc
13558 code. */
13559 code = fixp->fx_r_type;
13560 if (fixp->fx_pcrel)
13561 {
13562 switch (code)
13563 {
13564 case BFD_RELOC_8:
13565 code = BFD_RELOC_8_PCREL;
13566 break;
13567 case BFD_RELOC_16:
13568 code = BFD_RELOC_16_PCREL;
13569 break;
13570 case BFD_RELOC_32:
13571 code = BFD_RELOC_32_PCREL;
13572 break;
13573 case BFD_RELOC_64:
13574 code = BFD_RELOC_64_PCREL;
13575 break;
13576 case BFD_RELOC_8_PCREL:
13577 case BFD_RELOC_16_PCREL:
13578 case BFD_RELOC_32_PCREL:
13579 case BFD_RELOC_64_PCREL:
13580 case BFD_RELOC_16_PCREL_S2:
13581 case BFD_RELOC_PCREL_HI16_S:
13582 case BFD_RELOC_PCREL_LO16:
13583 break;
13584 default:
13585 as_bad_where (fixp->fx_file, fixp->fx_line,
13586 _("Cannot make %s relocation PC relative"),
13587 bfd_get_reloc_code_name (code));
13588 }
13589 }
13590
0b25d3e6
AO
13591 /* To support a PC relative reloc when generating embedded PIC code
13592 for ECOFF, we use a Cygnus extension. We check for that here to
13593 make sure that we don't let such a reloc escape normally. */
13594 if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
13595 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
13596 && code == BFD_RELOC_16_PCREL_S2
13597 && mips_pic != EMBEDDED_PIC)
13598 reloc->howto = NULL;
13599 else
13600 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
13601
252b5132
RH
13602 if (reloc->howto == NULL)
13603 {
13604 as_bad_where (fixp->fx_file, fixp->fx_line,
13605 _("Can not represent %s relocation in this object file format"),
13606 bfd_get_reloc_code_name (code));
13607 retval[0] = NULL;
13608 }
13609
13610 return retval;
13611}
13612
13613/* Relax a machine dependent frag. This returns the amount by which
13614 the current size of the frag should change. */
13615
13616int
17a2f251 13617mips_relax_frag (asection *sec, fragS *fragp, long stretch)
252b5132 13618{
4a6a3df4
AO
13619 if (RELAX_BRANCH_P (fragp->fr_subtype))
13620 {
13621 offsetT old_var = fragp->fr_var;
b34976b6
AM
13622
13623 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
4a6a3df4
AO
13624
13625 return fragp->fr_var - old_var;
13626 }
13627
252b5132
RH
13628 if (! RELAX_MIPS16_P (fragp->fr_subtype))
13629 return 0;
13630
c4e7957c 13631 if (mips16_extended_frag (fragp, NULL, stretch))
252b5132
RH
13632 {
13633 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13634 return 0;
13635 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
13636 return 2;
13637 }
13638 else
13639 {
13640 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13641 return 0;
13642 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
13643 return -2;
13644 }
13645
13646 return 0;
13647}
13648
13649/* Convert a machine dependent frag. */
13650
13651void
17a2f251 13652md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
252b5132
RH
13653{
13654 int old, new;
13655 char *fixptr;
13656
4a6a3df4
AO
13657 if (RELAX_BRANCH_P (fragp->fr_subtype))
13658 {
13659 bfd_byte *buf;
13660 unsigned long insn;
13661 expressionS exp;
13662 fixS *fixp;
b34976b6 13663
4a6a3df4
AO
13664 buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
13665
13666 if (target_big_endian)
13667 insn = bfd_getb32 (buf);
13668 else
13669 insn = bfd_getl32 (buf);
b34976b6 13670
4a6a3df4
AO
13671 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13672 {
13673 /* We generate a fixup instead of applying it right now
13674 because, if there are linker relaxations, we're going to
13675 need the relocations. */
13676 exp.X_op = O_symbol;
13677 exp.X_add_symbol = fragp->fr_symbol;
13678 exp.X_add_number = fragp->fr_offset;
13679
13680 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
0b25d3e6
AO
13681 4, &exp, 1,
13682 BFD_RELOC_16_PCREL_S2);
4a6a3df4
AO
13683 fixp->fx_file = fragp->fr_file;
13684 fixp->fx_line = fragp->fr_line;
b34976b6 13685
17a2f251 13686 md_number_to_chars (buf, insn, 4);
4a6a3df4
AO
13687 buf += 4;
13688 }
13689 else
13690 {
13691 int i;
13692
13693 as_warn_where (fragp->fr_file, fragp->fr_line,
13694 _("relaxed out-of-range branch into a jump"));
13695
13696 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
13697 goto uncond;
13698
13699 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13700 {
13701 /* Reverse the branch. */
13702 switch ((insn >> 28) & 0xf)
13703 {
13704 case 4:
13705 /* bc[0-3][tf]l? and bc1any[24][ft] instructions can
13706 have the condition reversed by tweaking a single
13707 bit, and their opcodes all have 0x4???????. */
13708 assert ((insn & 0xf1000000) == 0x41000000);
13709 insn ^= 0x00010000;
13710 break;
13711
13712 case 0:
13713 /* bltz 0x04000000 bgez 0x04010000
13714 bltzal 0x04100000 bgezal 0x04110000 */
13715 assert ((insn & 0xfc0e0000) == 0x04000000);
13716 insn ^= 0x00010000;
13717 break;
b34976b6 13718
4a6a3df4
AO
13719 case 1:
13720 /* beq 0x10000000 bne 0x14000000
13721 blez 0x18000000 bgtz 0x1c000000 */
13722 insn ^= 0x04000000;
13723 break;
13724
13725 default:
13726 abort ();
13727 }
13728 }
13729
13730 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13731 {
13732 /* Clear the and-link bit. */
13733 assert ((insn & 0xfc1c0000) == 0x04100000);
13734
13735 /* bltzal 0x04100000 bgezal 0x04110000
13736 bltzall 0x04120000 bgezall 0x04130000 */
13737 insn &= ~0x00100000;
13738 }
13739
13740 /* Branch over the branch (if the branch was likely) or the
13741 full jump (not likely case). Compute the offset from the
13742 current instruction to branch to. */
13743 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13744 i = 16;
13745 else
13746 {
13747 /* How many bytes in instructions we've already emitted? */
13748 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13749 /* How many bytes in instructions from here to the end? */
13750 i = fragp->fr_var - i;
13751 }
13752 /* Convert to instruction count. */
13753 i >>= 2;
13754 /* Branch counts from the next instruction. */
b34976b6 13755 i--;
4a6a3df4
AO
13756 insn |= i;
13757 /* Branch over the jump. */
17a2f251 13758 md_number_to_chars (buf, insn, 4);
4a6a3df4
AO
13759 buf += 4;
13760
13761 /* Nop */
17a2f251 13762 md_number_to_chars (buf, 0, 4);
4a6a3df4
AO
13763 buf += 4;
13764
13765 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13766 {
13767 /* beql $0, $0, 2f */
13768 insn = 0x50000000;
13769 /* Compute the PC offset from the current instruction to
13770 the end of the variable frag. */
13771 /* How many bytes in instructions we've already emitted? */
13772 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13773 /* How many bytes in instructions from here to the end? */
13774 i = fragp->fr_var - i;
13775 /* Convert to instruction count. */
13776 i >>= 2;
13777 /* Don't decrement i, because we want to branch over the
13778 delay slot. */
13779
13780 insn |= i;
17a2f251 13781 md_number_to_chars (buf, insn, 4);
4a6a3df4
AO
13782 buf += 4;
13783
17a2f251 13784 md_number_to_chars (buf, 0, 4);
4a6a3df4
AO
13785 buf += 4;
13786 }
13787
13788 uncond:
13789 if (mips_pic == NO_PIC)
13790 {
13791 /* j or jal. */
13792 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
13793 ? 0x0c000000 : 0x08000000);
13794 exp.X_op = O_symbol;
13795 exp.X_add_symbol = fragp->fr_symbol;
13796 exp.X_add_number = fragp->fr_offset;
13797
13798 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13799 4, &exp, 0, BFD_RELOC_MIPS_JMP);
13800 fixp->fx_file = fragp->fr_file;
13801 fixp->fx_line = fragp->fr_line;
13802
17a2f251 13803 md_number_to_chars (buf, insn, 4);
4a6a3df4
AO
13804 buf += 4;
13805 }
13806 else
13807 {
13808 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
13809 insn = HAVE_64BIT_ADDRESSES ? 0xdf810000 : 0x8f810000;
13810 exp.X_op = O_symbol;
13811 exp.X_add_symbol = fragp->fr_symbol;
13812 exp.X_add_number = fragp->fr_offset;
13813
13814 if (fragp->fr_offset)
13815 {
13816 exp.X_add_symbol = make_expr_symbol (&exp);
13817 exp.X_add_number = 0;
13818 }
13819
13820 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13821 4, &exp, 0, BFD_RELOC_MIPS_GOT16);
13822 fixp->fx_file = fragp->fr_file;
13823 fixp->fx_line = fragp->fr_line;
13824
17a2f251 13825 md_number_to_chars (buf, insn, 4);
4a6a3df4 13826 buf += 4;
b34976b6 13827
4a6a3df4
AO
13828 if (mips_opts.isa == ISA_MIPS1)
13829 {
13830 /* nop */
17a2f251 13831 md_number_to_chars (buf, 0, 4);
4a6a3df4
AO
13832 buf += 4;
13833 }
13834
13835 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
13836 insn = HAVE_64BIT_ADDRESSES ? 0x64210000 : 0x24210000;
13837
13838 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13839 4, &exp, 0, BFD_RELOC_LO16);
13840 fixp->fx_file = fragp->fr_file;
13841 fixp->fx_line = fragp->fr_line;
b34976b6 13842
17a2f251 13843 md_number_to_chars (buf, insn, 4);
4a6a3df4
AO
13844 buf += 4;
13845
13846 /* j(al)r $at. */
13847 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13848 insn = 0x0020f809;
13849 else
13850 insn = 0x00200008;
13851
17a2f251 13852 md_number_to_chars (buf, insn, 4);
4a6a3df4
AO
13853 buf += 4;
13854 }
13855 }
13856
13857 assert (buf == (bfd_byte *)fragp->fr_literal
13858 + fragp->fr_fix + fragp->fr_var);
13859
13860 fragp->fr_fix += fragp->fr_var;
13861
13862 return;
13863 }
13864
252b5132
RH
13865 if (RELAX_MIPS16_P (fragp->fr_subtype))
13866 {
13867 int type;
13868 register const struct mips16_immed_operand *op;
b34976b6 13869 bfd_boolean small, ext;
252b5132
RH
13870 offsetT val;
13871 bfd_byte *buf;
13872 unsigned long insn;
b34976b6 13873 bfd_boolean use_extend;
252b5132
RH
13874 unsigned short extend;
13875
13876 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
13877 op = mips16_immed_operands;
13878 while (op->type != type)
13879 ++op;
13880
13881 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13882 {
b34976b6
AM
13883 small = FALSE;
13884 ext = TRUE;
252b5132
RH
13885 }
13886 else
13887 {
b34976b6
AM
13888 small = TRUE;
13889 ext = FALSE;
252b5132
RH
13890 }
13891
6386f3a7 13892 resolve_symbol_value (fragp->fr_symbol);
252b5132
RH
13893 val = S_GET_VALUE (fragp->fr_symbol);
13894 if (op->pcrel)
13895 {
13896 addressT addr;
13897
13898 addr = fragp->fr_address + fragp->fr_fix;
13899
13900 /* The rules for the base address of a PC relative reloc are
13901 complicated; see mips16_extended_frag. */
13902 if (type == 'p' || type == 'q')
13903 {
13904 addr += 2;
13905 if (ext)
13906 addr += 2;
13907 /* Ignore the low bit in the target, since it will be
13908 set for a text label. */
13909 if ((val & 1) != 0)
13910 --val;
13911 }
13912 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13913 addr -= 4;
13914 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13915 addr -= 2;
13916
13917 addr &= ~ (addressT) ((1 << op->shift) - 1);
13918 val -= addr;
13919
13920 /* Make sure the section winds up with the alignment we have
13921 assumed. */
13922 if (op->shift > 0)
13923 record_alignment (asec, op->shift);
13924 }
13925
13926 if (ext
13927 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
13928 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
13929 as_warn_where (fragp->fr_file, fragp->fr_line,
13930 _("extended instruction in delay slot"));
13931
13932 buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
13933
13934 if (target_big_endian)
13935 insn = bfd_getb16 (buf);
13936 else
13937 insn = bfd_getl16 (buf);
13938
13939 mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
13940 RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
13941 small, ext, &insn, &use_extend, &extend);
13942
13943 if (use_extend)
13944 {
17a2f251 13945 md_number_to_chars (buf, 0xf000 | extend, 2);
252b5132
RH
13946 fragp->fr_fix += 2;
13947 buf += 2;
13948 }
13949
17a2f251 13950 md_number_to_chars (buf, insn, 2);
252b5132
RH
13951 fragp->fr_fix += 2;
13952 buf += 2;
13953 }
13954 else
13955 {
13956 if (fragp->fr_opcode == NULL)
13957 return;
13958
13959 old = RELAX_OLD (fragp->fr_subtype);
13960 new = RELAX_NEW (fragp->fr_subtype);
13961 fixptr = fragp->fr_literal + fragp->fr_fix;
13962
13963 if (new > 0)
f5040a92 13964 memmove (fixptr - old, fixptr, new);
252b5132
RH
13965
13966 fragp->fr_fix += new - old;
13967 }
13968}
13969
13970#ifdef OBJ_ELF
13971
13972/* This function is called after the relocs have been generated.
13973 We've been storing mips16 text labels as odd. Here we convert them
13974 back to even for the convenience of the debugger. */
13975
13976void
17a2f251 13977mips_frob_file_after_relocs (void)
252b5132
RH
13978{
13979 asymbol **syms;
13980 unsigned int count, i;
13981
13982 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
13983 return;
13984
13985 syms = bfd_get_outsymbols (stdoutput);
13986 count = bfd_get_symcount (stdoutput);
13987 for (i = 0; i < count; i++, syms++)
13988 {
13989 if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
13990 && ((*syms)->value & 1) != 0)
13991 {
13992 (*syms)->value &= ~1;
13993 /* If the symbol has an odd size, it was probably computed
13994 incorrectly, so adjust that as well. */
13995 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
13996 ++elf_symbol (*syms)->internal_elf_sym.st_size;
13997 }
13998 }
13999}
14000
14001#endif
14002
14003/* This function is called whenever a label is defined. It is used
14004 when handling branch delays; if a branch has a label, we assume we
14005 can not move it. */
14006
14007void
17a2f251 14008mips_define_label (symbolS *sym)
252b5132
RH
14009{
14010 struct insn_label_list *l;
14011
14012 if (free_insn_labels == NULL)
14013 l = (struct insn_label_list *) xmalloc (sizeof *l);
14014 else
14015 {
14016 l = free_insn_labels;
14017 free_insn_labels = l->next;
14018 }
14019
14020 l->label = sym;
14021 l->next = insn_labels;
14022 insn_labels = l;
14023}
14024\f
14025#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14026
14027/* Some special processing for a MIPS ELF file. */
14028
14029void
17a2f251 14030mips_elf_final_processing (void)
252b5132
RH
14031{
14032 /* Write out the register information. */
316f5878 14033 if (mips_abi != N64_ABI)
252b5132
RH
14034 {
14035 Elf32_RegInfo s;
14036
14037 s.ri_gprmask = mips_gprmask;
14038 s.ri_cprmask[0] = mips_cprmask[0];
14039 s.ri_cprmask[1] = mips_cprmask[1];
14040 s.ri_cprmask[2] = mips_cprmask[2];
14041 s.ri_cprmask[3] = mips_cprmask[3];
14042 /* The gp_value field is set by the MIPS ELF backend. */
14043
14044 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
14045 ((Elf32_External_RegInfo *)
14046 mips_regmask_frag));
14047 }
14048 else
14049 {
14050 Elf64_Internal_RegInfo s;
14051
14052 s.ri_gprmask = mips_gprmask;
14053 s.ri_pad = 0;
14054 s.ri_cprmask[0] = mips_cprmask[0];
14055 s.ri_cprmask[1] = mips_cprmask[1];
14056 s.ri_cprmask[2] = mips_cprmask[2];
14057 s.ri_cprmask[3] = mips_cprmask[3];
14058 /* The gp_value field is set by the MIPS ELF backend. */
14059
14060 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
14061 ((Elf64_External_RegInfo *)
14062 mips_regmask_frag));
14063 }
14064
14065 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
14066 sort of BFD interface for this. */
14067 if (mips_any_noreorder)
14068 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
14069 if (mips_pic != NO_PIC)
143d77c5 14070 {
252b5132 14071 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
143d77c5
EC
14072 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
14073 }
14074 if (mips_abicalls)
14075 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
252b5132 14076
98d3f06f 14077 /* Set MIPS ELF flags for ASEs. */
a4672219
TS
14078 if (file_ase_mips16)
14079 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
1f25f5d3
CD
14080#if 0 /* XXX FIXME */
14081 if (file_ase_mips3d)
14082 elf_elfheader (stdoutput)->e_flags |= ???;
14083#endif
deec1734
CD
14084 if (file_ase_mdmx)
14085 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
1f25f5d3 14086
bdaaa2e1 14087 /* Set the MIPS ELF ABI flags. */
316f5878 14088 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
252b5132 14089 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
316f5878 14090 else if (mips_abi == O64_ABI)
252b5132 14091 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
316f5878 14092 else if (mips_abi == EABI_ABI)
252b5132 14093 {
316f5878 14094 if (!file_mips_gp32)
252b5132
RH
14095 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
14096 else
14097 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
14098 }
316f5878 14099 else if (mips_abi == N32_ABI)
be00bddd
TS
14100 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
14101
c9914766 14102 /* Nothing to do for N64_ABI. */
252b5132
RH
14103
14104 if (mips_32bitmode)
14105 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
14106}
14107
14108#endif /* OBJ_ELF || OBJ_MAYBE_ELF */
14109\f
beae10d5
KH
14110typedef struct proc {
14111 symbolS *isym;
14112 unsigned long reg_mask;
14113 unsigned long reg_offset;
14114 unsigned long fpreg_mask;
14115 unsigned long fpreg_offset;
14116 unsigned long frame_offset;
14117 unsigned long frame_reg;
14118 unsigned long pc_reg;
14119} procS;
252b5132
RH
14120
14121static procS cur_proc;
14122static procS *cur_proc_ptr;
14123static int numprocs;
14124
0a9ef439 14125/* Fill in an rs_align_code fragment. */
a19d8eb0 14126
0a9ef439 14127void
17a2f251 14128mips_handle_align (fragS *fragp)
a19d8eb0 14129{
0a9ef439
RH
14130 if (fragp->fr_type != rs_align_code)
14131 return;
14132
14133 if (mips_opts.mips16)
a19d8eb0
CP
14134 {
14135 static const unsigned char be_nop[] = { 0x65, 0x00 };
14136 static const unsigned char le_nop[] = { 0x00, 0x65 };
14137
0a9ef439
RH
14138 int bytes;
14139 char *p;
a19d8eb0 14140
0a9ef439
RH
14141 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
14142 p = fragp->fr_literal + fragp->fr_fix;
14143
14144 if (bytes & 1)
14145 {
14146 *p++ = 0;
f9419b05 14147 fragp->fr_fix++;
0a9ef439
RH
14148 }
14149
14150 memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
14151 fragp->fr_var = 2;
a19d8eb0
CP
14152 }
14153
0a9ef439 14154 /* For mips32, a nop is a zero, which we trivially get by doing nothing. */
a19d8eb0
CP
14155}
14156
252b5132 14157static void
17a2f251 14158md_obj_begin (void)
252b5132
RH
14159{
14160}
14161
14162static void
17a2f251 14163md_obj_end (void)
252b5132
RH
14164{
14165 /* check for premature end, nesting errors, etc */
14166 if (cur_proc_ptr)
9a41af64 14167 as_warn (_("missing .end at end of assembly"));
252b5132
RH
14168}
14169
14170static long
17a2f251 14171get_number (void)
252b5132
RH
14172{
14173 int negative = 0;
14174 long val = 0;
14175
14176 if (*input_line_pointer == '-')
14177 {
14178 ++input_line_pointer;
14179 negative = 1;
14180 }
3882b010 14181 if (!ISDIGIT (*input_line_pointer))
956cd1d6 14182 as_bad (_("expected simple number"));
252b5132
RH
14183 if (input_line_pointer[0] == '0')
14184 {
14185 if (input_line_pointer[1] == 'x')
14186 {
14187 input_line_pointer += 2;
3882b010 14188 while (ISXDIGIT (*input_line_pointer))
252b5132
RH
14189 {
14190 val <<= 4;
14191 val |= hex_value (*input_line_pointer++);
14192 }
14193 return negative ? -val : val;
14194 }
14195 else
14196 {
14197 ++input_line_pointer;
3882b010 14198 while (ISDIGIT (*input_line_pointer))
252b5132
RH
14199 {
14200 val <<= 3;
14201 val |= *input_line_pointer++ - '0';
14202 }
14203 return negative ? -val : val;
14204 }
14205 }
3882b010 14206 if (!ISDIGIT (*input_line_pointer))
252b5132
RH
14207 {
14208 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
14209 *input_line_pointer, *input_line_pointer);
956cd1d6 14210 as_warn (_("invalid number"));
252b5132
RH
14211 return -1;
14212 }
3882b010 14213 while (ISDIGIT (*input_line_pointer))
252b5132
RH
14214 {
14215 val *= 10;
14216 val += *input_line_pointer++ - '0';
14217 }
14218 return negative ? -val : val;
14219}
14220
14221/* The .file directive; just like the usual .file directive, but there
c5dd6aab
DJ
14222 is an initial number which is the ECOFF file index. In the non-ECOFF
14223 case .file implies DWARF-2. */
14224
14225static void
17a2f251 14226s_mips_file (int x ATTRIBUTE_UNUSED)
c5dd6aab 14227{
ecb4347a
DJ
14228 static int first_file_directive = 0;
14229
c5dd6aab
DJ
14230 if (ECOFF_DEBUGGING)
14231 {
14232 get_number ();
14233 s_app_file (0);
14234 }
14235 else
ecb4347a
DJ
14236 {
14237 char *filename;
14238
14239 filename = dwarf2_directive_file (0);
14240
14241 /* Versions of GCC up to 3.1 start files with a ".file"
14242 directive even for stabs output. Make sure that this
14243 ".file" is handled. Note that you need a version of GCC
14244 after 3.1 in order to support DWARF-2 on MIPS. */
14245 if (filename != NULL && ! first_file_directive)
14246 {
14247 (void) new_logical_line (filename, -1);
14248 s_app_file_string (filename);
14249 }
14250 first_file_directive = 1;
14251 }
c5dd6aab
DJ
14252}
14253
14254/* The .loc directive, implying DWARF-2. */
252b5132
RH
14255
14256static void
17a2f251 14257s_mips_loc (int x ATTRIBUTE_UNUSED)
252b5132 14258{
c5dd6aab
DJ
14259 if (!ECOFF_DEBUGGING)
14260 dwarf2_directive_loc (0);
252b5132
RH
14261}
14262
252b5132
RH
14263/* The .end directive. */
14264
14265static void
17a2f251 14266s_mips_end (int x ATTRIBUTE_UNUSED)
252b5132
RH
14267{
14268 symbolS *p;
252b5132 14269
7a621144
DJ
14270 /* Following functions need their own .frame and .cprestore directives. */
14271 mips_frame_reg_valid = 0;
14272 mips_cprestore_valid = 0;
14273
252b5132
RH
14274 if (!is_end_of_line[(unsigned char) *input_line_pointer])
14275 {
14276 p = get_symbol ();
14277 demand_empty_rest_of_line ();
14278 }
14279 else
14280 p = NULL;
14281
14949570 14282 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
14283 as_warn (_(".end not in text section"));
14284
14285 if (!cur_proc_ptr)
14286 {
14287 as_warn (_(".end directive without a preceding .ent directive."));
14288 demand_empty_rest_of_line ();
14289 return;
14290 }
14291
14292 if (p != NULL)
14293 {
14294 assert (S_GET_NAME (p));
14295 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->isym)))
14296 as_warn (_(".end symbol does not match .ent symbol."));
ecb4347a
DJ
14297
14298 if (debug_type == DEBUG_STABS)
14299 stabs_generate_asm_endfunc (S_GET_NAME (p),
14300 S_GET_NAME (p));
252b5132
RH
14301 }
14302 else
14303 as_warn (_(".end directive missing or unknown symbol"));
14304
ecb4347a
DJ
14305#ifdef OBJ_ELF
14306 /* Generate a .pdr section. */
dcd410fe
RO
14307 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING
14308 && mips_flag_pdr)
ecb4347a
DJ
14309 {
14310 segT saved_seg = now_seg;
14311 subsegT saved_subseg = now_subseg;
14312 valueT dot;
14313 expressionS exp;
14314 char *fragp;
252b5132 14315
ecb4347a 14316 dot = frag_now_fix ();
252b5132
RH
14317
14318#ifdef md_flush_pending_output
ecb4347a 14319 md_flush_pending_output ();
252b5132
RH
14320#endif
14321
ecb4347a
DJ
14322 assert (pdr_seg);
14323 subseg_set (pdr_seg, 0);
252b5132 14324
ecb4347a
DJ
14325 /* Write the symbol. */
14326 exp.X_op = O_symbol;
14327 exp.X_add_symbol = p;
14328 exp.X_add_number = 0;
14329 emit_expr (&exp, 4);
252b5132 14330
ecb4347a 14331 fragp = frag_more (7 * 4);
252b5132 14332
17a2f251
TS
14333 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
14334 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
14335 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
14336 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
14337 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
14338 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
14339 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
252b5132 14340
ecb4347a
DJ
14341 subseg_set (saved_seg, saved_subseg);
14342 }
14343#endif /* OBJ_ELF */
252b5132
RH
14344
14345 cur_proc_ptr = NULL;
14346}
14347
14348/* The .aent and .ent directives. */
14349
14350static void
17a2f251 14351s_mips_ent (int aent)
252b5132 14352{
252b5132 14353 symbolS *symbolP;
252b5132
RH
14354
14355 symbolP = get_symbol ();
14356 if (*input_line_pointer == ',')
f9419b05 14357 ++input_line_pointer;
252b5132 14358 SKIP_WHITESPACE ();
3882b010 14359 if (ISDIGIT (*input_line_pointer)
d9a62219 14360 || *input_line_pointer == '-')
874e8986 14361 get_number ();
252b5132 14362
14949570 14363 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
14364 as_warn (_(".ent or .aent not in text section."));
14365
14366 if (!aent && cur_proc_ptr)
9a41af64 14367 as_warn (_("missing .end"));
252b5132
RH
14368
14369 if (!aent)
14370 {
7a621144
DJ
14371 /* This function needs its own .frame and .cprestore directives. */
14372 mips_frame_reg_valid = 0;
14373 mips_cprestore_valid = 0;
14374
252b5132
RH
14375 cur_proc_ptr = &cur_proc;
14376 memset (cur_proc_ptr, '\0', sizeof (procS));
14377
14378 cur_proc_ptr->isym = symbolP;
14379
49309057 14380 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
252b5132 14381
f9419b05 14382 ++numprocs;
ecb4347a
DJ
14383
14384 if (debug_type == DEBUG_STABS)
14385 stabs_generate_asm_func (S_GET_NAME (symbolP),
14386 S_GET_NAME (symbolP));
252b5132
RH
14387 }
14388
14389 demand_empty_rest_of_line ();
14390}
14391
14392/* The .frame directive. If the mdebug section is present (IRIX 5 native)
bdaaa2e1 14393 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
252b5132 14394 s_mips_frame is used so that we can set the PDR information correctly.
bdaaa2e1 14395 We can't use the ecoff routines because they make reference to the ecoff
252b5132
RH
14396 symbol table (in the mdebug section). */
14397
14398static void
17a2f251 14399s_mips_frame (int ignore ATTRIBUTE_UNUSED)
252b5132 14400{
ecb4347a
DJ
14401#ifdef OBJ_ELF
14402 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14403 {
14404 long val;
252b5132 14405
ecb4347a
DJ
14406 if (cur_proc_ptr == (procS *) NULL)
14407 {
14408 as_warn (_(".frame outside of .ent"));
14409 demand_empty_rest_of_line ();
14410 return;
14411 }
252b5132 14412
ecb4347a
DJ
14413 cur_proc_ptr->frame_reg = tc_get_register (1);
14414
14415 SKIP_WHITESPACE ();
14416 if (*input_line_pointer++ != ','
14417 || get_absolute_expression_and_terminator (&val) != ',')
14418 {
14419 as_warn (_("Bad .frame directive"));
14420 --input_line_pointer;
14421 demand_empty_rest_of_line ();
14422 return;
14423 }
252b5132 14424
ecb4347a
DJ
14425 cur_proc_ptr->frame_offset = val;
14426 cur_proc_ptr->pc_reg = tc_get_register (0);
252b5132 14427
252b5132 14428 demand_empty_rest_of_line ();
252b5132 14429 }
ecb4347a
DJ
14430 else
14431#endif /* OBJ_ELF */
14432 s_ignore (ignore);
252b5132
RH
14433}
14434
bdaaa2e1
KH
14435/* The .fmask and .mask directives. If the mdebug section is present
14436 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
252b5132 14437 embedded targets, s_mips_mask is used so that we can set the PDR
bdaaa2e1 14438 information correctly. We can't use the ecoff routines because they
252b5132
RH
14439 make reference to the ecoff symbol table (in the mdebug section). */
14440
14441static void
17a2f251 14442s_mips_mask (int reg_type)
252b5132 14443{
ecb4347a
DJ
14444#ifdef OBJ_ELF
14445 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
252b5132 14446 {
ecb4347a 14447 long mask, off;
252b5132 14448
ecb4347a
DJ
14449 if (cur_proc_ptr == (procS *) NULL)
14450 {
14451 as_warn (_(".mask/.fmask outside of .ent"));
14452 demand_empty_rest_of_line ();
14453 return;
14454 }
252b5132 14455
ecb4347a
DJ
14456 if (get_absolute_expression_and_terminator (&mask) != ',')
14457 {
14458 as_warn (_("Bad .mask/.fmask directive"));
14459 --input_line_pointer;
14460 demand_empty_rest_of_line ();
14461 return;
14462 }
252b5132 14463
ecb4347a
DJ
14464 off = get_absolute_expression ();
14465
14466 if (reg_type == 'F')
14467 {
14468 cur_proc_ptr->fpreg_mask = mask;
14469 cur_proc_ptr->fpreg_offset = off;
14470 }
14471 else
14472 {
14473 cur_proc_ptr->reg_mask = mask;
14474 cur_proc_ptr->reg_offset = off;
14475 }
14476
14477 demand_empty_rest_of_line ();
252b5132
RH
14478 }
14479 else
ecb4347a
DJ
14480#endif /* OBJ_ELF */
14481 s_ignore (reg_type);
252b5132
RH
14482}
14483
14484/* The .loc directive. */
14485
14486#if 0
14487static void
17a2f251 14488s_loc (int x)
252b5132
RH
14489{
14490 symbolS *symbolP;
14491 int lineno;
14492 int addroff;
14493
14494 assert (now_seg == text_section);
14495
14496 lineno = get_number ();
14497 addroff = frag_now_fix ();
14498
14499 symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
14500 S_SET_TYPE (symbolP, N_SLINE);
14501 S_SET_OTHER (symbolP, 0);
14502 S_SET_DESC (symbolP, lineno);
14503 symbolP->sy_segment = now_seg;
14504}
14505#endif
e7af610e 14506
316f5878
RS
14507/* A table describing all the processors gas knows about. Names are
14508 matched in the order listed.
e7af610e 14509
316f5878
RS
14510 To ease comparison, please keep this table in the same order as
14511 gcc's mips_cpu_info_table[]. */
e972090a
NC
14512static const struct mips_cpu_info mips_cpu_info_table[] =
14513{
316f5878
RS
14514 /* Entries for generic ISAs */
14515 { "mips1", 1, ISA_MIPS1, CPU_R3000 },
14516 { "mips2", 1, ISA_MIPS2, CPU_R6000 },
14517 { "mips3", 1, ISA_MIPS3, CPU_R4000 },
14518 { "mips4", 1, ISA_MIPS4, CPU_R8000 },
14519 { "mips5", 1, ISA_MIPS5, CPU_MIPS5 },
14520 { "mips32", 1, ISA_MIPS32, CPU_MIPS32 },
af7ee8bf 14521 { "mips32r2", 1, ISA_MIPS32R2, CPU_MIPS32R2 },
316f5878 14522 { "mips64", 1, ISA_MIPS64, CPU_MIPS64 },
5f74bc13 14523 { "mips64r2", 1, ISA_MIPS64R2, CPU_MIPS64R2 },
316f5878
RS
14524
14525 /* MIPS I */
14526 { "r3000", 0, ISA_MIPS1, CPU_R3000 },
14527 { "r2000", 0, ISA_MIPS1, CPU_R3000 },
14528 { "r3900", 0, ISA_MIPS1, CPU_R3900 },
14529
14530 /* MIPS II */
14531 { "r6000", 0, ISA_MIPS2, CPU_R6000 },
14532
14533 /* MIPS III */
14534 { "r4000", 0, ISA_MIPS3, CPU_R4000 },
14535 { "r4010", 0, ISA_MIPS2, CPU_R4010 },
14536 { "vr4100", 0, ISA_MIPS3, CPU_VR4100 },
14537 { "vr4111", 0, ISA_MIPS3, CPU_R4111 },
60b63b72
RS
14538 { "vr4120", 0, ISA_MIPS3, CPU_VR4120 },
14539 { "vr4130", 0, ISA_MIPS3, CPU_VR4120 },
14540 { "vr4181", 0, ISA_MIPS3, CPU_R4111 },
316f5878
RS
14541 { "vr4300", 0, ISA_MIPS3, CPU_R4300 },
14542 { "r4400", 0, ISA_MIPS3, CPU_R4400 },
14543 { "r4600", 0, ISA_MIPS3, CPU_R4600 },
14544 { "orion", 0, ISA_MIPS3, CPU_R4600 },
14545 { "r4650", 0, ISA_MIPS3, CPU_R4650 },
14546
14547 /* MIPS IV */
14548 { "r8000", 0, ISA_MIPS4, CPU_R8000 },
14549 { "r10000", 0, ISA_MIPS4, CPU_R10000 },
14550 { "r12000", 0, ISA_MIPS4, CPU_R12000 },
14551 { "vr5000", 0, ISA_MIPS4, CPU_R5000 },
60b63b72
RS
14552 { "vr5400", 0, ISA_MIPS4, CPU_VR5400 },
14553 { "vr5500", 0, ISA_MIPS4, CPU_VR5500 },
316f5878
RS
14554 { "rm5200", 0, ISA_MIPS4, CPU_R5000 },
14555 { "rm5230", 0, ISA_MIPS4, CPU_R5000 },
14556 { "rm5231", 0, ISA_MIPS4, CPU_R5000 },
14557 { "rm5261", 0, ISA_MIPS4, CPU_R5000 },
14558 { "rm5721", 0, ISA_MIPS4, CPU_R5000 },
5a7ea749
RS
14559 { "rm7000", 0, ISA_MIPS4, CPU_RM7000 },
14560 { "rm9000", 0, ISA_MIPS4, CPU_RM7000 },
316f5878
RS
14561
14562 /* MIPS 32 */
fef14a42 14563 { "4kc", 0, ISA_MIPS32, CPU_MIPS32 },
316f5878
RS
14564 { "4km", 0, ISA_MIPS32, CPU_MIPS32 },
14565 { "4kp", 0, ISA_MIPS32, CPU_MIPS32 },
e7af610e 14566
316f5878
RS
14567 /* MIPS 64 */
14568 { "5kc", 0, ISA_MIPS64, CPU_MIPS64 },
14569 { "20kc", 0, ISA_MIPS64, CPU_MIPS64 },
e7af610e 14570
c7a23324 14571 /* Broadcom SB-1 CPU core */
316f5878 14572 { "sb1", 0, ISA_MIPS64, CPU_SB1 },
e7af610e 14573
316f5878
RS
14574 /* End marker */
14575 { NULL, 0, 0, 0 }
14576};
e7af610e 14577
84ea6cf2 14578
316f5878
RS
14579/* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
14580 with a final "000" replaced by "k". Ignore case.
e7af610e 14581
316f5878 14582 Note: this function is shared between GCC and GAS. */
c6c98b38 14583
b34976b6 14584static bfd_boolean
17a2f251 14585mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
14586{
14587 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
14588 given++, canonical++;
14589
14590 return ((*given == 0 && *canonical == 0)
14591 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
14592}
14593
14594
14595/* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
14596 CPU name. We've traditionally allowed a lot of variation here.
14597
14598 Note: this function is shared between GCC and GAS. */
14599
b34976b6 14600static bfd_boolean
17a2f251 14601mips_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
14602{
14603 /* First see if the name matches exactly, or with a final "000"
14604 turned into "k". */
14605 if (mips_strict_matching_cpu_name_p (canonical, given))
b34976b6 14606 return TRUE;
316f5878
RS
14607
14608 /* If not, try comparing based on numerical designation alone.
14609 See if GIVEN is an unadorned number, or 'r' followed by a number. */
14610 if (TOLOWER (*given) == 'r')
14611 given++;
14612 if (!ISDIGIT (*given))
b34976b6 14613 return FALSE;
316f5878
RS
14614
14615 /* Skip over some well-known prefixes in the canonical name,
14616 hoping to find a number there too. */
14617 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
14618 canonical += 2;
14619 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
14620 canonical += 2;
14621 else if (TOLOWER (canonical[0]) == 'r')
14622 canonical += 1;
14623
14624 return mips_strict_matching_cpu_name_p (canonical, given);
14625}
14626
14627
14628/* Parse an option that takes the name of a processor as its argument.
14629 OPTION is the name of the option and CPU_STRING is the argument.
14630 Return the corresponding processor enumeration if the CPU_STRING is
14631 recognized, otherwise report an error and return null.
14632
14633 A similar function exists in GCC. */
e7af610e
NC
14634
14635static const struct mips_cpu_info *
17a2f251 14636mips_parse_cpu (const char *option, const char *cpu_string)
e7af610e 14637{
316f5878 14638 const struct mips_cpu_info *p;
e7af610e 14639
316f5878
RS
14640 /* 'from-abi' selects the most compatible architecture for the given
14641 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
14642 EABIs, we have to decide whether we're using the 32-bit or 64-bit
14643 version. Look first at the -mgp options, if given, otherwise base
14644 the choice on MIPS_DEFAULT_64BIT.
e7af610e 14645
316f5878
RS
14646 Treat NO_ABI like the EABIs. One reason to do this is that the
14647 plain 'mips' and 'mips64' configs have 'from-abi' as their default
14648 architecture. This code picks MIPS I for 'mips' and MIPS III for
14649 'mips64', just as we did in the days before 'from-abi'. */
14650 if (strcasecmp (cpu_string, "from-abi") == 0)
14651 {
14652 if (ABI_NEEDS_32BIT_REGS (mips_abi))
14653 return mips_cpu_info_from_isa (ISA_MIPS1);
14654
14655 if (ABI_NEEDS_64BIT_REGS (mips_abi))
14656 return mips_cpu_info_from_isa (ISA_MIPS3);
14657
14658 if (file_mips_gp32 >= 0)
14659 return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
14660
14661 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
14662 ? ISA_MIPS3
14663 : ISA_MIPS1);
14664 }
14665
14666 /* 'default' has traditionally been a no-op. Probably not very useful. */
14667 if (strcasecmp (cpu_string, "default") == 0)
14668 return 0;
14669
14670 for (p = mips_cpu_info_table; p->name != 0; p++)
14671 if (mips_matching_cpu_name_p (p->name, cpu_string))
14672 return p;
14673
14674 as_bad ("Bad value (%s) for %s", cpu_string, option);
14675 return 0;
e7af610e
NC
14676}
14677
316f5878
RS
14678/* Return the canonical processor information for ISA (a member of the
14679 ISA_MIPS* enumeration). */
14680
e7af610e 14681static const struct mips_cpu_info *
17a2f251 14682mips_cpu_info_from_isa (int isa)
e7af610e
NC
14683{
14684 int i;
14685
14686 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14687 if (mips_cpu_info_table[i].is_isa
316f5878 14688 && isa == mips_cpu_info_table[i].isa)
e7af610e
NC
14689 return (&mips_cpu_info_table[i]);
14690
e972090a 14691 return NULL;
e7af610e 14692}
fef14a42
TS
14693
14694static const struct mips_cpu_info *
17a2f251 14695mips_cpu_info_from_arch (int arch)
fef14a42
TS
14696{
14697 int i;
14698
14699 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14700 if (arch == mips_cpu_info_table[i].cpu)
14701 return (&mips_cpu_info_table[i]);
14702
14703 return NULL;
14704}
316f5878
RS
14705\f
14706static void
17a2f251 14707show (FILE *stream, const char *string, int *col_p, int *first_p)
316f5878
RS
14708{
14709 if (*first_p)
14710 {
14711 fprintf (stream, "%24s", "");
14712 *col_p = 24;
14713 }
14714 else
14715 {
14716 fprintf (stream, ", ");
14717 *col_p += 2;
14718 }
e7af610e 14719
316f5878
RS
14720 if (*col_p + strlen (string) > 72)
14721 {
14722 fprintf (stream, "\n%24s", "");
14723 *col_p = 24;
14724 }
14725
14726 fprintf (stream, "%s", string);
14727 *col_p += strlen (string);
14728
14729 *first_p = 0;
14730}
14731
14732void
17a2f251 14733md_show_usage (FILE *stream)
e7af610e 14734{
316f5878
RS
14735 int column, first;
14736 size_t i;
14737
14738 fprintf (stream, _("\
14739MIPS options:\n\
14740-membedded-pic generate embedded position independent code\n\
14741-EB generate big endian output\n\
14742-EL generate little endian output\n\
14743-g, -g2 do not remove unneeded NOPs or swap branches\n\
14744-G NUM allow referencing objects up to NUM bytes\n\
14745 implicitly with the gp register [default 8]\n"));
14746 fprintf (stream, _("\
14747-mips1 generate MIPS ISA I instructions\n\
14748-mips2 generate MIPS ISA II instructions\n\
14749-mips3 generate MIPS ISA III instructions\n\
14750-mips4 generate MIPS ISA IV instructions\n\
14751-mips5 generate MIPS ISA V instructions\n\
14752-mips32 generate MIPS32 ISA instructions\n\
af7ee8bf 14753-mips32r2 generate MIPS32 release 2 ISA instructions\n\
316f5878 14754-mips64 generate MIPS64 ISA instructions\n\
5f74bc13 14755-mips64r2 generate MIPS64 release 2 ISA instructions\n\
316f5878
RS
14756-march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
14757
14758 first = 1;
e7af610e
NC
14759
14760 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
316f5878
RS
14761 show (stream, mips_cpu_info_table[i].name, &column, &first);
14762 show (stream, "from-abi", &column, &first);
14763 fputc ('\n', stream);
e7af610e 14764
316f5878
RS
14765 fprintf (stream, _("\
14766-mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
14767-no-mCPU don't generate code specific to CPU.\n\
14768 For -mCPU and -no-mCPU, CPU must be one of:\n"));
14769
14770 first = 1;
14771
14772 show (stream, "3900", &column, &first);
14773 show (stream, "4010", &column, &first);
14774 show (stream, "4100", &column, &first);
14775 show (stream, "4650", &column, &first);
14776 fputc ('\n', stream);
14777
14778 fprintf (stream, _("\
14779-mips16 generate mips16 instructions\n\
14780-no-mips16 do not generate mips16 instructions\n"));
14781 fprintf (stream, _("\
14782-mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
14783-mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
14784-O0 remove unneeded NOPs, do not swap branches\n\
14785-O remove unneeded NOPs and swap branches\n\
14786-n warn about NOPs generated from macros\n\
14787--[no-]construct-floats [dis]allow floating point values to be constructed\n\
14788--trap, --no-break trap exception on div by 0 and mult overflow\n\
14789--break, --no-trap break exception on div by 0 and mult overflow\n"));
14790#ifdef OBJ_ELF
14791 fprintf (stream, _("\
14792-KPIC, -call_shared generate SVR4 position independent code\n\
14793-non_shared do not generate position independent code\n\
14794-xgot assume a 32 bit GOT\n\
dcd410fe 14795-mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
316f5878
RS
14796-mabi=ABI create ABI conformant object file for:\n"));
14797
14798 first = 1;
14799
14800 show (stream, "32", &column, &first);
14801 show (stream, "o64", &column, &first);
14802 show (stream, "n32", &column, &first);
14803 show (stream, "64", &column, &first);
14804 show (stream, "eabi", &column, &first);
14805
14806 fputc ('\n', stream);
14807
14808 fprintf (stream, _("\
14809-32 create o32 ABI object file (default)\n\
14810-n32 create n32 ABI object file\n\
14811-64 create 64 ABI object file\n"));
14812#endif
e7af610e 14813}
14e777e0
KB
14814
14815enum dwarf2_format
17a2f251 14816mips_dwarf2_format (void)
14e777e0
KB
14817{
14818 if (mips_abi == N64_ABI)
1de5b6a1
AO
14819 {
14820#ifdef TE_IRIX
14821 return dwarf2_format_64bit_irix;
14822#else
14823 return dwarf2_format_64bit;
14824#endif
14825 }
14e777e0
KB
14826 else
14827 return dwarf2_format_32bit;
14828}
This page took 1.280114 seconds and 4 git commands to generate.