* config/tc-mips.c (macro_build_ldst_constoffset,load_register,macro):
[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
6478892d
TS
471/* Similiar for NewABI PIC code, where $gp is callee-saved. NewABI has some
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
KH
1710 insert two NOPS. Some newer processors have interlocks.
1711 Also the tx39's multiply instructions can be exectuted
252b5132 1712 immediatly 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
2291 delay slot, becase the target of the branch might
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))
ae826530
TS
3274 as_bad (_("too large constant specified"));
3275
256ab948
TS
3276 ep->X_add_number = (((ep->X_add_number & 0xffffffff) ^ 0x80000000)
3277 - 0x80000000);
ae826530 3278 }
256ab948 3279
885add95 3280 /* Right now, this routine can only handle signed 32-bit contants. */
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
3332/* Warn if an expression is not a constant. */
3333
3334static void
17a2f251 3335check_absolute_expr (struct mips_cl_insn *ip, expressionS *ex)
252b5132
RH
3336{
3337 if (ex->X_op == O_big)
3338 as_bad (_("unsupported large constant"));
3339 else if (ex->X_op != O_constant)
3340 as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
3341}
3342
3343/* Count the leading zeroes by performing a binary chop. This is a
3344 bulky bit of source, but performance is a LOT better for the
3345 majority of values than a simple loop to count the bits:
3346 for (lcnt = 0; (lcnt < 32); lcnt++)
3347 if ((v) & (1 << (31 - lcnt)))
3348 break;
3349 However it is not code size friendly, and the gain will drop a bit
3350 on certain cached systems.
3351*/
3352#define COUNT_TOP_ZEROES(v) \
3353 (((v) & ~0xffff) == 0 \
3354 ? ((v) & ~0xff) == 0 \
3355 ? ((v) & ~0xf) == 0 \
3356 ? ((v) & ~0x3) == 0 \
3357 ? ((v) & ~0x1) == 0 \
3358 ? !(v) \
3359 ? 32 \
3360 : 31 \
3361 : 30 \
3362 : ((v) & ~0x7) == 0 \
3363 ? 29 \
3364 : 28 \
3365 : ((v) & ~0x3f) == 0 \
3366 ? ((v) & ~0x1f) == 0 \
3367 ? 27 \
3368 : 26 \
3369 : ((v) & ~0x7f) == 0 \
3370 ? 25 \
3371 : 24 \
3372 : ((v) & ~0xfff) == 0 \
3373 ? ((v) & ~0x3ff) == 0 \
3374 ? ((v) & ~0x1ff) == 0 \
3375 ? 23 \
3376 : 22 \
3377 : ((v) & ~0x7ff) == 0 \
3378 ? 21 \
3379 : 20 \
3380 : ((v) & ~0x3fff) == 0 \
3381 ? ((v) & ~0x1fff) == 0 \
3382 ? 19 \
3383 : 18 \
3384 : ((v) & ~0x7fff) == 0 \
3385 ? 17 \
3386 : 16 \
3387 : ((v) & ~0xffffff) == 0 \
3388 ? ((v) & ~0xfffff) == 0 \
3389 ? ((v) & ~0x3ffff) == 0 \
3390 ? ((v) & ~0x1ffff) == 0 \
3391 ? 15 \
3392 : 14 \
3393 : ((v) & ~0x7ffff) == 0 \
3394 ? 13 \
3395 : 12 \
3396 : ((v) & ~0x3fffff) == 0 \
3397 ? ((v) & ~0x1fffff) == 0 \
3398 ? 11 \
3399 : 10 \
3400 : ((v) & ~0x7fffff) == 0 \
3401 ? 9 \
3402 : 8 \
3403 : ((v) & ~0xfffffff) == 0 \
3404 ? ((v) & ~0x3ffffff) == 0 \
3405 ? ((v) & ~0x1ffffff) == 0 \
3406 ? 7 \
3407 : 6 \
3408 : ((v) & ~0x7ffffff) == 0 \
3409 ? 5 \
3410 : 4 \
3411 : ((v) & ~0x3fffffff) == 0 \
3412 ? ((v) & ~0x1fffffff) == 0 \
3413 ? 3 \
3414 : 2 \
3415 : ((v) & ~0x7fffffff) == 0 \
3416 ? 1 \
3417 : 0)
3418
3419/* load_register()
3420 * This routine generates the least number of instructions neccessary to load
3421 * an absolute expression value into a register.
3422 */
3423static void
17a2f251 3424load_register (int *counter, int reg, expressionS *ep, int dbl)
252b5132
RH
3425{
3426 int freg;
3427 expressionS hi32, lo32;
3428
3429 if (ep->X_op != O_big)
3430 {
3431 assert (ep->X_op == O_constant);
256ab948
TS
3432
3433 /* Sign-extending 32-bit constants makes their handling easier. */
d17e7bce
TS
3434 if (! dbl && ! ((ep->X_add_number & ~((bfd_vma) 0x7fffffff))
3435 == ~((bfd_vma) 0x7fffffff)))
ae826530 3436 {
d17e7bce 3437 if (ep->X_add_number & ~((bfd_vma) 0xffffffff))
ae826530
TS
3438 as_bad (_("too large constant specified"));
3439
256ab948
TS
3440 ep->X_add_number = (((ep->X_add_number & 0xffffffff) ^ 0x80000000)
3441 - 0x80000000);
ae826530 3442 }
256ab948
TS
3443
3444 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
252b5132
RH
3445 {
3446 /* We can handle 16 bit signed values with an addiu to
3447 $zero. No need to ever use daddiu here, since $zero and
3448 the result are always correct in 32 bit mode. */
17a2f251
TS
3449 macro_build (NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3450 BFD_RELOC_LO16);
252b5132
RH
3451 return;
3452 }
3453 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3454 {
3455 /* We can handle 16 bit unsigned values with an ori to
3456 $zero. */
17a2f251
TS
3457 macro_build (NULL, counter, ep, "ori", "t,r,i", reg, 0,
3458 BFD_RELOC_LO16);
252b5132
RH
3459 return;
3460 }
256ab948 3461 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
252b5132
RH
3462 {
3463 /* 32 bit values require an lui. */
17a2f251 3464 macro_build (NULL, counter, ep, "lui", "t,u", reg, BFD_RELOC_HI16);
252b5132 3465 if ((ep->X_add_number & 0xffff) != 0)
17a2f251
TS
3466 macro_build (NULL, counter, ep, "ori", "t,r,i", reg, reg,
3467 BFD_RELOC_LO16);
252b5132
RH
3468 return;
3469 }
3470 }
3471
3472 /* The value is larger than 32 bits. */
3473
ca4e0257 3474 if (HAVE_32BIT_GPRS)
252b5132 3475 {
956cd1d6
TS
3476 as_bad (_("Number (0x%lx) larger than 32 bits"),
3477 (unsigned long) ep->X_add_number);
17a2f251
TS
3478 macro_build (NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3479 BFD_RELOC_LO16);
252b5132
RH
3480 return;
3481 }
3482
3483 if (ep->X_op != O_big)
3484 {
3485 hi32 = *ep;
3486 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3487 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3488 hi32.X_add_number &= 0xffffffff;
3489 lo32 = *ep;
3490 lo32.X_add_number &= 0xffffffff;
3491 }
3492 else
3493 {
3494 assert (ep->X_add_number > 2);
3495 if (ep->X_add_number == 3)
3496 generic_bignum[3] = 0;
3497 else if (ep->X_add_number > 4)
3498 as_bad (_("Number larger than 64 bits"));
3499 lo32.X_op = O_constant;
3500 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3501 hi32.X_op = O_constant;
3502 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3503 }
3504
3505 if (hi32.X_add_number == 0)
3506 freg = 0;
3507 else
3508 {
3509 int shift, bit;
3510 unsigned long hi, lo;
3511
956cd1d6 3512 if (hi32.X_add_number == (offsetT) 0xffffffff)
beae10d5
KH
3513 {
3514 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3515 {
17a2f251
TS
3516 macro_build (NULL, counter, &lo32, "addiu", "t,r,j", reg, 0,
3517 BFD_RELOC_LO16);
beae10d5
KH
3518 return;
3519 }
3520 if (lo32.X_add_number & 0x80000000)
3521 {
17a2f251
TS
3522 macro_build (NULL, counter, &lo32, "lui", "t,u", reg,
3523 BFD_RELOC_HI16);
252b5132 3524 if (lo32.X_add_number & 0xffff)
17a2f251
TS
3525 macro_build (NULL, counter, &lo32, "ori", "t,r,i", reg, reg,
3526 BFD_RELOC_LO16);
beae10d5
KH
3527 return;
3528 }
3529 }
252b5132
RH
3530
3531 /* Check for 16bit shifted constant. We know that hi32 is
3532 non-zero, so start the mask on the first bit of the hi32
3533 value. */
3534 shift = 17;
3535 do
beae10d5
KH
3536 {
3537 unsigned long himask, lomask;
3538
3539 if (shift < 32)
3540 {
3541 himask = 0xffff >> (32 - shift);
3542 lomask = (0xffff << shift) & 0xffffffff;
3543 }
3544 else
3545 {
3546 himask = 0xffff << (shift - 32);
3547 lomask = 0;
3548 }
3549 if ((hi32.X_add_number & ~(offsetT) himask) == 0
3550 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3551 {
3552 expressionS tmp;
3553
3554 tmp.X_op = O_constant;
3555 if (shift < 32)
3556 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3557 | (lo32.X_add_number >> shift));
3558 else
3559 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
17a2f251
TS
3560 macro_build (NULL, counter, &tmp, "ori", "t,r,i", reg, 0,
3561 BFD_RELOC_LO16);
3562 macro_build (NULL, counter, NULL,
beae10d5
KH
3563 (shift >= 32) ? "dsll32" : "dsll",
3564 "d,w,<", reg, reg,
3565 (shift >= 32) ? shift - 32 : shift);
3566 return;
3567 }
f9419b05 3568 ++shift;
beae10d5
KH
3569 }
3570 while (shift <= (64 - 16));
252b5132
RH
3571
3572 /* Find the bit number of the lowest one bit, and store the
3573 shifted value in hi/lo. */
3574 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3575 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3576 if (lo != 0)
3577 {
3578 bit = 0;
3579 while ((lo & 1) == 0)
3580 {
3581 lo >>= 1;
3582 ++bit;
3583 }
3584 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3585 hi >>= bit;
3586 }
3587 else
3588 {
3589 bit = 32;
3590 while ((hi & 1) == 0)
3591 {
3592 hi >>= 1;
3593 ++bit;
3594 }
3595 lo = hi;
3596 hi = 0;
3597 }
3598
3599 /* Optimize if the shifted value is a (power of 2) - 1. */
3600 if ((hi == 0 && ((lo + 1) & lo) == 0)
3601 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
beae10d5
KH
3602 {
3603 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
252b5132 3604 if (shift != 0)
beae10d5 3605 {
252b5132
RH
3606 expressionS tmp;
3607
3608 /* This instruction will set the register to be all
3609 ones. */
beae10d5
KH
3610 tmp.X_op = O_constant;
3611 tmp.X_add_number = (offsetT) -1;
17a2f251
TS
3612 macro_build (NULL, counter, &tmp, "addiu", "t,r,j", reg, 0,
3613 BFD_RELOC_LO16);
beae10d5
KH
3614 if (bit != 0)
3615 {
3616 bit += shift;
17a2f251 3617 macro_build (NULL, counter, NULL,
beae10d5
KH
3618 (bit >= 32) ? "dsll32" : "dsll",
3619 "d,w,<", reg, reg,
3620 (bit >= 32) ? bit - 32 : bit);
3621 }
17a2f251 3622 macro_build (NULL, counter, NULL,
252b5132 3623 (shift >= 32) ? "dsrl32" : "dsrl",
beae10d5 3624 "d,w,<", reg, reg,
252b5132 3625 (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
3626 return;
3627 }
3628 }
252b5132
RH
3629
3630 /* Sign extend hi32 before calling load_register, because we can
3631 generally get better code when we load a sign extended value. */
3632 if ((hi32.X_add_number & 0x80000000) != 0)
beae10d5 3633 hi32.X_add_number |= ~(offsetT) 0xffffffff;
252b5132
RH
3634 load_register (counter, reg, &hi32, 0);
3635 freg = reg;
3636 }
3637 if ((lo32.X_add_number & 0xffff0000) == 0)
3638 {
3639 if (freg != 0)
3640 {
17a2f251 3641 macro_build (NULL, counter, NULL, "dsll32", "d,w,<", reg, freg, 0);
252b5132
RH
3642 freg = reg;
3643 }
3644 }
3645 else
3646 {
3647 expressionS mid16;
3648
956cd1d6 3649 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
beae10d5 3650 {
17a2f251
TS
3651 macro_build (NULL, counter, &lo32, "lui", "t,u", reg,
3652 BFD_RELOC_HI16);
3653 macro_build (NULL, counter, NULL, "dsrl32", "d,w,<", reg, reg, 0);
beae10d5
KH
3654 return;
3655 }
252b5132
RH
3656
3657 if (freg != 0)
3658 {
17a2f251 3659 macro_build (NULL, counter, NULL, "dsll", "d,w,<", reg, freg, 16);
252b5132
RH
3660 freg = reg;
3661 }
3662 mid16 = lo32;
3663 mid16.X_add_number >>= 16;
17a2f251
TS
3664 macro_build (NULL, counter, &mid16, "ori", "t,r,i", reg, freg,
3665 BFD_RELOC_LO16);
3666 macro_build (NULL, counter, NULL, "dsll", "d,w,<", reg, reg, 16);
252b5132
RH
3667 freg = reg;
3668 }
3669 if ((lo32.X_add_number & 0xffff) != 0)
17a2f251
TS
3670 macro_build (NULL, counter, &lo32, "ori", "t,r,i", reg, freg,
3671 BFD_RELOC_LO16);
252b5132
RH
3672}
3673
3674/* Load an address into a register. */
3675
3676static void
17a2f251 3677load_address (int *counter, int reg, expressionS *ep, int *used_at)
252b5132 3678{
f9419b05 3679 char *p = NULL;
252b5132
RH
3680
3681 if (ep->X_op != O_constant
3682 && ep->X_op != O_symbol)
3683 {
3684 as_bad (_("expression too complex"));
3685 ep->X_op = O_constant;
3686 }
3687
3688 if (ep->X_op == O_constant)
3689 {
c9914766 3690 load_register (counter, reg, ep, HAVE_64BIT_ADDRESSES);
252b5132
RH
3691 return;
3692 }
3693
3694 if (mips_pic == NO_PIC)
3695 {
3696 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 3697 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
3698 Otherwise we want
3699 lui $reg,<sym> (BFD_RELOC_HI16_S)
3700 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
d6bc6245 3701 If we have an addend, we always use the latter form.
76b3015f 3702
d6bc6245
TS
3703 With 64bit address space and a usable $at we want
3704 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3705 lui $at,<sym> (BFD_RELOC_HI16_S)
3706 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3707 daddiu $at,<sym> (BFD_RELOC_LO16)
3708 dsll32 $reg,0
3a482fd5 3709 daddu $reg,$reg,$at
76b3015f 3710
c03099e6 3711 If $at is already in use, we use a path which is suboptimal
d6bc6245
TS
3712 on superscalar processors.
3713 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3714 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3715 dsll $reg,16
3716 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
3717 dsll $reg,16
3718 daddiu $reg,<sym> (BFD_RELOC_LO16)
3719 */
c9914766 3720 if (HAVE_64BIT_ADDRESSES)
d6bc6245 3721 {
d6bc6245
TS
3722 /* We don't do GP optimization for now because RELAX_ENCODE can't
3723 hold the data for such large chunks. */
3724
460597ba 3725 if (*used_at == 0 && ! mips_opts.noat)
d6bc6245
TS
3726 {
3727 macro_build (p, counter, ep, "lui", "t,u",
17a2f251 3728 reg, BFD_RELOC_MIPS_HIGHEST);
d6bc6245 3729 macro_build (p, counter, ep, "lui", "t,u",
17a2f251 3730 AT, BFD_RELOC_HI16_S);
d6bc6245 3731 macro_build (p, counter, ep, "daddiu", "t,r,j",
17a2f251 3732 reg, reg, BFD_RELOC_MIPS_HIGHER);
d6bc6245 3733 macro_build (p, counter, ep, "daddiu", "t,r,j",
17a2f251
TS
3734 AT, AT, BFD_RELOC_LO16);
3735 macro_build (p, counter, NULL, "dsll32", "d,w,<", reg, reg, 0);
3736 macro_build (p, counter, NULL, "daddu", "d,v,t", reg, reg, AT);
d6bc6245
TS
3737 *used_at = 1;
3738 }
3739 else
3740 {
3741 macro_build (p, counter, ep, "lui", "t,u",
17a2f251 3742 reg, BFD_RELOC_MIPS_HIGHEST);
d6bc6245 3743 macro_build (p, counter, ep, "daddiu", "t,r,j",
17a2f251
TS
3744 reg, reg, BFD_RELOC_MIPS_HIGHER);
3745 macro_build (p, counter, NULL, "dsll", "d,w,<", reg, reg, 16);
d6bc6245 3746 macro_build (p, counter, ep, "daddiu", "t,r,j",
17a2f251
TS
3747 reg, reg, BFD_RELOC_HI16_S);
3748 macro_build (p, counter, NULL, "dsll", "d,w,<", reg, reg, 16);
d6bc6245 3749 macro_build (p, counter, ep, "daddiu", "t,r,j",
17a2f251 3750 reg, reg, BFD_RELOC_LO16);
d6bc6245
TS
3751 }
3752 }
252b5132
RH
3753 else
3754 {
d6bc6245
TS
3755 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3756 && ! nopic_need_relax (ep->X_add_symbol, 1))
3757 {
3758 frag_grow (20);
17a2f251
TS
3759 macro_build (NULL, counter, ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
3760 mips_gp_register, BFD_RELOC_GPREL16);
d6bc6245
TS
3761 p = frag_var (rs_machine_dependent, 8, 0,
3762 RELAX_ENCODE (4, 8, 0, 4, 0,
3763 mips_opts.warn_about_macros),
956cd1d6 3764 ep->X_add_symbol, 0, NULL);
d6bc6245
TS
3765 }
3766 macro_build_lui (p, counter, ep, reg);
3767 if (p != NULL)
3768 p += 4;
17a2f251
TS
3769 macro_build (p, counter, ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
3770 BFD_RELOC_LO16);
d6bc6245 3771 }
252b5132
RH
3772 }
3773 else if (mips_pic == SVR4_PIC && ! mips_big_got)
3774 {
3775 expressionS ex;
3776
3777 /* If this is a reference to an external symbol, we want
3778 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3779 Otherwise we want
3780 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3781 nop
3782 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
f5040a92
AO
3783 If there is a constant, it must be added in after.
3784
ed6fb7bd 3785 If we have NewABI, we want
f5040a92
AO
3786 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
3787 unless we're referencing a global symbol with a non-zero
3788 offset, in which case cst must be added separately. */
ed6fb7bd
SC
3789 if (HAVE_NEWABI)
3790 {
f5040a92 3791 frag_grow (12);
684022ea 3792
f5040a92
AO
3793 if (ep->X_add_number)
3794 {
3795 frag_now->tc_frag_data.tc_fr_offset =
3796 ex.X_add_number = ep->X_add_number;
3797 ep->X_add_number = 0;
17a2f251
TS
3798 macro_build (NULL, counter, ep, ADDRESS_LOAD_INSN, "t,o(b)",
3799 reg, BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
3800 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3801 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3802 ex.X_op = O_constant;
17a2f251
TS
3803 macro_build (NULL, counter, &ex, ADDRESS_ADDI_INSN, "t,r,j",
3804 reg, reg, BFD_RELOC_LO16);
f5040a92
AO
3805 p = frag_var (rs_machine_dependent, 8, 0,
3806 RELAX_ENCODE (8, 4, 0, 0, 0,
3807 mips_opts.warn_about_macros),
17a2f251 3808 ep->X_add_symbol, 0, NULL);
f5040a92
AO
3809 ep->X_add_number = ex.X_add_number;
3810 }
3811
f899b4b8 3812 macro_build (p, counter, ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 3813 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
3814
3815 if (! p)
3816 {
3817 /* To avoid confusion in tc_gen_reloc, we must ensure
3818 that this does not become a variant frag. */
3819 frag_wane (frag_now);
3820 frag_new (0);
3821 }
ed6fb7bd
SC
3822 }
3823 else
3824 {
f5040a92
AO
3825 ex.X_add_number = ep->X_add_number;
3826 ep->X_add_number = 0;
3827 frag_grow (20);
17a2f251
TS
3828 macro_build (NULL, counter, ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3829 BFD_RELOC_MIPS_GOT16,
f899b4b8 3830 mips_gp_register);
17a2f251 3831 macro_build (NULL, counter, NULL, "nop", "");
ed6fb7bd
SC
3832 p = frag_var (rs_machine_dependent, 4, 0,
3833 RELAX_ENCODE (0, 4, -8, 0, 0, mips_opts.warn_about_macros),
17a2f251
TS
3834 ep->X_add_symbol, 0, NULL);
3835 macro_build (p, counter, ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
3836 BFD_RELOC_LO16);
ed6fb7bd 3837
f5040a92
AO
3838 if (ex.X_add_number != 0)
3839 {
3840 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3841 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3842 ex.X_op = O_constant;
17a2f251
TS
3843 macro_build (NULL, counter, &ex, ADDRESS_ADDI_INSN, "t,r,j",
3844 reg, reg, BFD_RELOC_LO16);
f5040a92 3845 }
252b5132
RH
3846 }
3847 }
3848 else if (mips_pic == SVR4_PIC)
3849 {
3850 expressionS ex;
3851 int off;
3852
3853 /* This is the large GOT case. If this is a reference to an
3854 external symbol, we want
3855 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
3856 addu $reg,$reg,$gp
3857 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
f5040a92
AO
3858
3859 Otherwise, for a reference to a local symbol in old ABI, we want
252b5132
RH
3860 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3861 nop
3862 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
684022ea 3863 If there is a constant, it must be added in after.
f5040a92
AO
3864
3865 In the NewABI, for local symbols, with or without offsets, we want:
438c16b8
TS
3866 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
3867 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
f5040a92 3868 */
438c16b8
TS
3869 if (HAVE_NEWABI)
3870 {
f5040a92
AO
3871 frag_grow (24);
3872
3873 frag_now->tc_frag_data.tc_fr_offset =
3874 ex.X_add_number = ep->X_add_number;
3875 ep->X_add_number = 0;
17a2f251
TS
3876 macro_build (NULL, counter, ep, "lui", "t,u", reg,
3877 BFD_RELOC_MIPS_GOT_HI16);
3878 macro_build (NULL, counter, NULL, ADDRESS_ADD_INSN, "d,v,t", reg,
3879 reg, mips_gp_register);
3880 macro_build (NULL, counter, ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3881 BFD_RELOC_MIPS_GOT_LO16, reg);
f5040a92
AO
3882 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3883 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3884 else if (ex.X_add_number)
3885 {
3886 ex.X_op = O_constant;
17a2f251
TS
3887 macro_build (NULL, counter, &ex, ADDRESS_ADDI_INSN, "t,r,j",
3888 reg, reg, BFD_RELOC_LO16);
f5040a92
AO
3889 }
3890
3891 ep->X_add_number = ex.X_add_number;
3892 p = frag_var (rs_machine_dependent, 8, 0,
3893 RELAX_ENCODE (ex.X_add_number ? 16 : 12, 8, 0, 4, 0,
3894 mips_opts.warn_about_macros),
17a2f251 3895 ep->X_add_symbol, 0, NULL);
f899b4b8 3896 macro_build (p, counter, ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251
TS
3897 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
3898 macro_build (p + 4, counter, ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
3899 reg, BFD_RELOC_MIPS_GOT_OFST);
438c16b8 3900 }
252b5132 3901 else
438c16b8 3902 {
f5040a92
AO
3903 ex.X_add_number = ep->X_add_number;
3904 ep->X_add_number = 0;
438c16b8
TS
3905 if (reg_needs_delay (mips_gp_register))
3906 off = 4;
3907 else
3908 off = 0;
3909 frag_grow (32);
17a2f251
TS
3910 macro_build (NULL, counter, ep, "lui", "t,u", reg,
3911 BFD_RELOC_MIPS_GOT_HI16);
3912 macro_build (NULL, counter, NULL, ADDRESS_ADD_INSN, "d,v,t", reg,
3913 reg, mips_gp_register);
3914 macro_build (NULL, counter, ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3915 BFD_RELOC_MIPS_GOT_LO16, reg);
438c16b8
TS
3916 p = frag_var (rs_machine_dependent, 12 + off, 0,
3917 RELAX_ENCODE (12, 12 + off, off, 8 + off, 0,
3918 mips_opts.warn_about_macros),
3919 ep->X_add_symbol, 0, NULL);
3920 if (off > 0)
3921 {
3922 /* We need a nop before loading from $gp. This special
3923 check is required because the lui which starts the main
3924 instruction stream does not refer to $gp, and so will not
3925 insert the nop which may be required. */
17a2f251 3926 macro_build (p, counter, NULL, "nop", "");
438c16b8
TS
3927 p += 4;
3928 }
f899b4b8 3929 macro_build (p, counter, ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 3930 BFD_RELOC_MIPS_GOT16, mips_gp_register);
438c16b8 3931 p += 4;
17a2f251 3932 macro_build (p, counter, NULL, "nop", "");
252b5132 3933 p += 4;
17a2f251
TS
3934 macro_build (p, counter, ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
3935 BFD_RELOC_LO16);
438c16b8 3936
f5040a92
AO
3937 if (ex.X_add_number != 0)
3938 {
3939 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3940 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3941 ex.X_op = O_constant;
17a2f251
TS
3942 macro_build (NULL, counter, &ex, ADDRESS_ADDI_INSN, "t,r,j",
3943 reg, reg, BFD_RELOC_LO16);
f5040a92 3944 }
252b5132
RH
3945 }
3946 }
3947 else if (mips_pic == EMBEDDED_PIC)
3948 {
3949 /* We always do
cdf6fd85 3950 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
c9914766 3951 */
17a2f251
TS
3952 macro_build (NULL, counter, ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
3953 mips_gp_register, BFD_RELOC_GPREL16);
252b5132
RH
3954 }
3955 else
3956 abort ();
3957}
3958
ea1fb5dc
RS
3959/* Move the contents of register SOURCE into register DEST. */
3960
3961static void
17a2f251 3962move_register (int *counter, int dest, int source)
ea1fb5dc 3963{
17a2f251 3964 macro_build (NULL, counter, NULL, HAVE_32BIT_GPRS ? "addu" : "daddu",
ea1fb5dc
RS
3965 "d,v,t", dest, source, 0);
3966}
3967
252b5132
RH
3968/*
3969 * Build macros
3970 * This routine implements the seemingly endless macro or synthesized
3971 * instructions and addressing modes in the mips assembly language. Many
3972 * of these macros are simple and are similar to each other. These could
3973 * probably be handled by some kind of table or grammer aproach instead of
3974 * this verbose method. Others are not simple macros but are more like
3975 * optimizing code generation.
3976 * One interesting optimization is when several store macros appear
3977 * consecutivly that would load AT with the upper half of the same address.
3978 * The ensuing load upper instructions are ommited. This implies some kind
3979 * of global optimization. We currently only optimize within a single macro.
3980 * For many of the load and store macros if the address is specified as a
3981 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
3982 * first load register 'at' with zero and use it as the base register. The
3983 * mips assembler simply uses register $zero. Just one tiny optimization
3984 * we're missing.
3985 */
3986static void
17a2f251 3987macro (struct mips_cl_insn *ip)
252b5132
RH
3988{
3989 register int treg, sreg, dreg, breg;
3990 int tempreg;
3991 int mask;
3992 int icnt = 0;
43841e91 3993 int used_at = 0;
252b5132
RH
3994 expressionS expr1;
3995 const char *s;
3996 const char *s2;
3997 const char *fmt;
3998 int likely = 0;
3999 int dbl = 0;
4000 int coproc = 0;
4001 int lr = 0;
4002 int imm = 0;
4003 offsetT maxnum;
4004 int off;
4005 bfd_reloc_code_real_type r;
252b5132
RH
4006 int hold_mips_optimize;
4007
4008 assert (! mips_opts.mips16);
4009
4010 treg = (ip->insn_opcode >> 16) & 0x1f;
4011 dreg = (ip->insn_opcode >> 11) & 0x1f;
4012 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
4013 mask = ip->insn_mo->mask;
4014
4015 expr1.X_op = O_constant;
4016 expr1.X_op_symbol = NULL;
4017 expr1.X_add_symbol = NULL;
4018 expr1.X_add_number = 1;
4019
5919d012
RS
4020 /* Umatched fixups should not be put in the same frag as a relaxable
4021 macro. For example, suppose we have:
4022
4023 lui $4,%hi(l1) # 1
4024 la $5,l2 # 2
4025 addiu $4,$4,%lo(l1) # 3
4026
4027 If instructions 1 and 2 were put in the same frag, md_frob_file would
4028 move the fixup for #1 after the fixups for the "unrelaxed" version of
4029 #2. This would confuse tc_gen_reloc, which expects the relocations
4030 for #2 to be the last for that frag.
4031
64bdfcaf
RS
4032 Also, if tc_gen_reloc sees certain relocations in a variant frag,
4033 it assumes that they belong to a relaxable macro. We mustn't put
4034 other uses of such relocations into a variant frag.
4035
4036 To avoid both problems, finish the current frag it contains a
4037 %reloc() operator. The macro then goes into a new frag. */
4038 if (prev_reloc_op_frag == frag_now)
5919d012
RS
4039 {
4040 frag_wane (frag_now);
4041 frag_new (0);
4042 }
4043
252b5132
RH
4044 switch (mask)
4045 {
4046 case M_DABS:
4047 dbl = 1;
4048 case M_ABS:
4049 /* bgez $a0,.+12
4050 move v0,$a0
4051 sub v0,$zero,$a0
4052 */
4053
b34976b6 4054 mips_emit_delays (TRUE);
252b5132
RH
4055 ++mips_opts.noreorder;
4056 mips_any_noreorder = 1;
4057
4058 expr1.X_add_number = 8;
17a2f251 4059 macro_build (NULL, &icnt, &expr1, "bgez", "s,p", sreg);
252b5132 4060 if (dreg == sreg)
17a2f251 4061 macro_build (NULL, &icnt, NULL, "nop", "", 0);
252b5132 4062 else
ea1fb5dc 4063 move_register (&icnt, dreg, sreg);
17a2f251
TS
4064 macro_build (NULL, &icnt, NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, 0,
4065 sreg);
252b5132
RH
4066
4067 --mips_opts.noreorder;
4068 return;
4069
4070 case M_ADD_I:
4071 s = "addi";
4072 s2 = "add";
4073 goto do_addi;
4074 case M_ADDU_I:
4075 s = "addiu";
4076 s2 = "addu";
4077 goto do_addi;
4078 case M_DADD_I:
4079 dbl = 1;
4080 s = "daddi";
4081 s2 = "dadd";
4082 goto do_addi;
4083 case M_DADDU_I:
4084 dbl = 1;
4085 s = "daddiu";
4086 s2 = "daddu";
4087 do_addi:
4088 if (imm_expr.X_op == O_constant
4089 && imm_expr.X_add_number >= -0x8000
4090 && imm_expr.X_add_number < 0x8000)
4091 {
17a2f251
TS
4092 macro_build (NULL, &icnt, &imm_expr, s, "t,r,j", treg, sreg,
4093 BFD_RELOC_LO16);
252b5132
RH
4094 return;
4095 }
4096 load_register (&icnt, AT, &imm_expr, dbl);
17a2f251 4097 macro_build (NULL, &icnt, NULL, s2, "d,v,t", treg, sreg, AT);
252b5132
RH
4098 break;
4099
4100 case M_AND_I:
4101 s = "andi";
4102 s2 = "and";
4103 goto do_bit;
4104 case M_OR_I:
4105 s = "ori";
4106 s2 = "or";
4107 goto do_bit;
4108 case M_NOR_I:
4109 s = "";
4110 s2 = "nor";
4111 goto do_bit;
4112 case M_XOR_I:
4113 s = "xori";
4114 s2 = "xor";
4115 do_bit:
4116 if (imm_expr.X_op == O_constant
4117 && imm_expr.X_add_number >= 0
4118 && imm_expr.X_add_number < 0x10000)
4119 {
4120 if (mask != M_NOR_I)
17a2f251
TS
4121 macro_build (NULL, &icnt, &imm_expr, s, "t,r,i", treg, sreg,
4122 BFD_RELOC_LO16);
252b5132
RH
4123 else
4124 {
17a2f251
TS
4125 macro_build (NULL, &icnt, &imm_expr, "ori", "t,r,i", treg, sreg,
4126 BFD_RELOC_LO16);
4127 macro_build (NULL, &icnt, NULL, "nor", "d,v,t", treg, treg, 0);
252b5132
RH
4128 }
4129 return;
4130 }
4131
d6bc6245 4132 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
17a2f251 4133 macro_build (NULL, &icnt, NULL, s2, "d,v,t", treg, sreg, AT);
252b5132
RH
4134 break;
4135
4136 case M_BEQ_I:
4137 s = "beq";
4138 goto beq_i;
4139 case M_BEQL_I:
4140 s = "beql";
4141 likely = 1;
4142 goto beq_i;
4143 case M_BNE_I:
4144 s = "bne";
4145 goto beq_i;
4146 case M_BNEL_I:
4147 s = "bnel";
4148 likely = 1;
4149 beq_i:
4150 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4151 {
17a2f251 4152 macro_build (NULL, &icnt, &offset_expr, s, "s,t,p", sreg, 0);
252b5132
RH
4153 return;
4154 }
4d34fb5f 4155 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
17a2f251 4156 macro_build (NULL, &icnt, &offset_expr, s, "s,t,p", sreg, AT);
252b5132
RH
4157 break;
4158
4159 case M_BGEL:
4160 likely = 1;
4161 case M_BGE:
4162 if (treg == 0)
4163 {
17a2f251
TS
4164 macro_build (NULL, &icnt, &offset_expr, likely ? "bgezl" : "bgez",
4165 "s,p", sreg);
252b5132
RH
4166 return;
4167 }
4168 if (sreg == 0)
4169 {
17a2f251
TS
4170 macro_build (NULL, &icnt, &offset_expr, likely ? "blezl" : "blez",
4171 "s,p", treg);
252b5132
RH
4172 return;
4173 }
17a2f251
TS
4174 macro_build (NULL, &icnt, NULL, "slt", "d,v,t", AT, sreg, treg);
4175 macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4176 "s,t,p", AT, 0);
252b5132
RH
4177 break;
4178
4179 case M_BGTL_I:
4180 likely = 1;
4181 case M_BGT_I:
4182 /* check for > max integer */
4183 maxnum = 0x7fffffff;
ca4e0257 4184 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
4185 {
4186 maxnum <<= 16;
4187 maxnum |= 0xffff;
4188 maxnum <<= 16;
4189 maxnum |= 0xffff;
4190 }
4191 if (imm_expr.X_op == O_constant
4192 && imm_expr.X_add_number >= maxnum
ca4e0257 4193 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
4194 {
4195 do_false:
4196 /* result is always false */
4197 if (! likely)
4198 {
39c0a331
L
4199 if (warn_nops)
4200 as_warn (_("Branch %s is always false (nop)"),
4201 ip->insn_mo->name);
17a2f251 4202 macro_build (NULL, &icnt, NULL, "nop", "", 0);
252b5132
RH
4203 }
4204 else
4205 {
39c0a331
L
4206 if (warn_nops)
4207 as_warn (_("Branch likely %s is always false"),
4208 ip->insn_mo->name);
17a2f251 4209 macro_build (NULL, &icnt, &offset_expr, "bnel", "s,t,p", 0, 0);
252b5132
RH
4210 }
4211 return;
4212 }
4213 if (imm_expr.X_op != O_constant)
4214 as_bad (_("Unsupported large constant"));
f9419b05 4215 ++imm_expr.X_add_number;
252b5132
RH
4216 /* FALLTHROUGH */
4217 case M_BGE_I:
4218 case M_BGEL_I:
4219 if (mask == M_BGEL_I)
4220 likely = 1;
4221 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4222 {
17a2f251
TS
4223 macro_build (NULL, &icnt, &offset_expr, likely ? "bgezl" : "bgez",
4224 "s,p", sreg);
252b5132
RH
4225 return;
4226 }
4227 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4228 {
17a2f251
TS
4229 macro_build (NULL, &icnt, &offset_expr, likely ? "bgtzl" : "bgtz",
4230 "s,p", sreg);
252b5132
RH
4231 return;
4232 }
4233 maxnum = 0x7fffffff;
ca4e0257 4234 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
4235 {
4236 maxnum <<= 16;
4237 maxnum |= 0xffff;
4238 maxnum <<= 16;
4239 maxnum |= 0xffff;
4240 }
4241 maxnum = - maxnum - 1;
4242 if (imm_expr.X_op == O_constant
4243 && imm_expr.X_add_number <= maxnum
ca4e0257 4244 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
4245 {
4246 do_true:
4247 /* result is always true */
4248 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
17a2f251 4249 macro_build (NULL, &icnt, &offset_expr, "b", "p");
252b5132
RH
4250 return;
4251 }
4252 set_at (&icnt, sreg, 0);
17a2f251
TS
4253 macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4254 "s,t,p", AT, 0);
252b5132
RH
4255 break;
4256
4257 case M_BGEUL:
4258 likely = 1;
4259 case M_BGEU:
4260 if (treg == 0)
4261 goto do_true;
4262 if (sreg == 0)
4263 {
17a2f251
TS
4264 macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4265 "s,t,p", 0, treg);
252b5132
RH
4266 return;
4267 }
17a2f251
TS
4268 macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", AT, sreg, treg);
4269 macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4270 "s,t,p", AT, 0);
252b5132
RH
4271 break;
4272
4273 case M_BGTUL_I:
4274 likely = 1;
4275 case M_BGTU_I:
4276 if (sreg == 0
ca4e0257 4277 || (HAVE_32BIT_GPRS
252b5132 4278 && imm_expr.X_op == O_constant
956cd1d6 4279 && imm_expr.X_add_number == (offsetT) 0xffffffff))
252b5132
RH
4280 goto do_false;
4281 if (imm_expr.X_op != O_constant)
4282 as_bad (_("Unsupported large constant"));
f9419b05 4283 ++imm_expr.X_add_number;
252b5132
RH
4284 /* FALLTHROUGH */
4285 case M_BGEU_I:
4286 case M_BGEUL_I:
4287 if (mask == M_BGEUL_I)
4288 likely = 1;
4289 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4290 goto do_true;
4291 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4292 {
17a2f251
TS
4293 macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4294 "s,t,p", sreg, 0);
252b5132
RH
4295 return;
4296 }
4297 set_at (&icnt, sreg, 1);
17a2f251
TS
4298 macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4299 "s,t,p", AT, 0);
252b5132
RH
4300 break;
4301
4302 case M_BGTL:
4303 likely = 1;
4304 case M_BGT:
4305 if (treg == 0)
4306 {
17a2f251
TS
4307 macro_build (NULL, &icnt, &offset_expr, likely ? "bgtzl" : "bgtz",
4308 "s,p", sreg);
252b5132
RH
4309 return;
4310 }
4311 if (sreg == 0)
4312 {
17a2f251
TS
4313 macro_build (NULL, &icnt, &offset_expr, likely ? "bltzl" : "bltz",
4314 "s,p", treg);
252b5132
RH
4315 return;
4316 }
17a2f251
TS
4317 macro_build (NULL, &icnt, NULL, "slt", "d,v,t", AT, treg, sreg);
4318 macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4319 "s,t,p", AT, 0);
252b5132
RH
4320 break;
4321
4322 case M_BGTUL:
4323 likely = 1;
4324 case M_BGTU:
4325 if (treg == 0)
4326 {
17a2f251
TS
4327 macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4328 "s,t,p", sreg, 0);
252b5132
RH
4329 return;
4330 }
4331 if (sreg == 0)
4332 goto do_false;
17a2f251
TS
4333 macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", AT, treg, sreg);
4334 macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4335 "s,t,p", AT, 0);
252b5132
RH
4336 break;
4337
4338 case M_BLEL:
4339 likely = 1;
4340 case M_BLE:
4341 if (treg == 0)
4342 {
17a2f251
TS
4343 macro_build (NULL, &icnt, &offset_expr, likely ? "blezl" : "blez",
4344 "s,p", sreg);
252b5132
RH
4345 return;
4346 }
4347 if (sreg == 0)
4348 {
17a2f251
TS
4349 macro_build (NULL, &icnt, &offset_expr, likely ? "bgezl" : "bgez",
4350 "s,p", treg);
252b5132
RH
4351 return;
4352 }
17a2f251
TS
4353 macro_build (NULL, &icnt, NULL, "slt", "d,v,t", AT, treg, sreg);
4354 macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4355 "s,t,p", AT, 0);
252b5132
RH
4356 break;
4357
4358 case M_BLEL_I:
4359 likely = 1;
4360 case M_BLE_I:
4361 maxnum = 0x7fffffff;
ca4e0257 4362 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
4363 {
4364 maxnum <<= 16;
4365 maxnum |= 0xffff;
4366 maxnum <<= 16;
4367 maxnum |= 0xffff;
4368 }
4369 if (imm_expr.X_op == O_constant
4370 && imm_expr.X_add_number >= maxnum
ca4e0257 4371 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
4372 goto do_true;
4373 if (imm_expr.X_op != O_constant)
4374 as_bad (_("Unsupported large constant"));
f9419b05 4375 ++imm_expr.X_add_number;
252b5132
RH
4376 /* FALLTHROUGH */
4377 case M_BLT_I:
4378 case M_BLTL_I:
4379 if (mask == M_BLTL_I)
4380 likely = 1;
4381 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4382 {
17a2f251
TS
4383 macro_build (NULL, &icnt, &offset_expr, likely ? "bltzl" : "bltz",
4384 "s,p", sreg);
252b5132
RH
4385 return;
4386 }
4387 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4388 {
17a2f251
TS
4389 macro_build (NULL, &icnt, &offset_expr, likely ? "blezl" : "blez",
4390 "s,p", sreg);
252b5132
RH
4391 return;
4392 }
4393 set_at (&icnt, sreg, 0);
17a2f251
TS
4394 macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4395 "s,t,p", AT, 0);
252b5132
RH
4396 break;
4397
4398 case M_BLEUL:
4399 likely = 1;
4400 case M_BLEU:
4401 if (treg == 0)
4402 {
17a2f251
TS
4403 macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4404 "s,t,p", sreg, 0);
252b5132
RH
4405 return;
4406 }
4407 if (sreg == 0)
4408 goto do_true;
17a2f251
TS
4409 macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", AT, treg, sreg);
4410 macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4411 "s,t,p", AT, 0);
252b5132
RH
4412 break;
4413
4414 case M_BLEUL_I:
4415 likely = 1;
4416 case M_BLEU_I:
4417 if (sreg == 0
ca4e0257 4418 || (HAVE_32BIT_GPRS
252b5132 4419 && imm_expr.X_op == O_constant
956cd1d6 4420 && imm_expr.X_add_number == (offsetT) 0xffffffff))
252b5132
RH
4421 goto do_true;
4422 if (imm_expr.X_op != O_constant)
4423 as_bad (_("Unsupported large constant"));
f9419b05 4424 ++imm_expr.X_add_number;
252b5132
RH
4425 /* FALLTHROUGH */
4426 case M_BLTU_I:
4427 case M_BLTUL_I:
4428 if (mask == M_BLTUL_I)
4429 likely = 1;
4430 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4431 goto do_false;
4432 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4433 {
17a2f251 4434 macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
252b5132
RH
4435 "s,t,p", sreg, 0);
4436 return;
4437 }
4438 set_at (&icnt, sreg, 1);
17a2f251
TS
4439 macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4440 "s,t,p", AT, 0);
252b5132
RH
4441 break;
4442
4443 case M_BLTL:
4444 likely = 1;
4445 case M_BLT:
4446 if (treg == 0)
4447 {
17a2f251
TS
4448 macro_build (NULL, &icnt, &offset_expr, likely ? "bltzl" : "bltz",
4449 "s,p", sreg);
252b5132
RH
4450 return;
4451 }
4452 if (sreg == 0)
4453 {
17a2f251
TS
4454 macro_build (NULL, &icnt, &offset_expr, likely ? "bgtzl" : "bgtz",
4455 "s,p", treg);
252b5132
RH
4456 return;
4457 }
17a2f251
TS
4458 macro_build (NULL, &icnt, NULL, "slt", "d,v,t", AT, sreg, treg);
4459 macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4460 "s,t,p", AT, 0);
252b5132
RH
4461 break;
4462
4463 case M_BLTUL:
4464 likely = 1;
4465 case M_BLTU:
4466 if (treg == 0)
4467 goto do_false;
4468 if (sreg == 0)
4469 {
17a2f251
TS
4470 macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4471 "s,t,p", 0, treg);
252b5132
RH
4472 return;
4473 }
17a2f251
TS
4474 macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", AT, sreg, treg);
4475 macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4476 "s,t,p", AT, 0);
252b5132
RH
4477 break;
4478
5f74bc13
CD
4479 case M_DEXT:
4480 {
4481 unsigned long pos;
4482 unsigned long size;
4483
4484 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
4485 {
4486 as_bad (_("Unsupported large constant"));
4487 pos = size = 1;
4488 }
4489 else
4490 {
4491 pos = (unsigned long) imm_expr.X_add_number;
4492 size = (unsigned long) imm2_expr.X_add_number;
4493 }
4494
4495 if (pos > 63)
4496 {
4497 as_bad (_("Improper position (%lu)"), pos);
4498 pos = 1;
4499 }
4500 if (size == 0 || size > 64
4501 || (pos + size - 1) > 63)
4502 {
4503 as_bad (_("Improper extract size (%lu, position %lu)"),
4504 size, pos);
4505 size = 1;
4506 }
4507
4508 if (size <= 32 && pos < 32)
4509 {
4510 s = "dext";
4511 fmt = "t,r,+A,+C";
4512 }
4513 else if (size <= 32)
4514 {
4515 s = "dextu";
4516 fmt = "t,r,+E,+H";
4517 }
4518 else
4519 {
4520 s = "dextm";
4521 fmt = "t,r,+A,+G";
4522 }
4523 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s,
4524 fmt, treg, sreg, pos, size - 1);
4525 }
4526 return;
4527
4528 case M_DINS:
4529 {
4530 unsigned long pos;
4531 unsigned long size;
4532
4533 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
4534 {
4535 as_bad (_("Unsupported large constant"));
4536 pos = size = 1;
4537 }
4538 else
4539 {
4540 pos = (unsigned long) imm_expr.X_add_number;
4541 size = (unsigned long) imm2_expr.X_add_number;
4542 }
4543
4544 if (pos > 63)
4545 {
4546 as_bad (_("Improper position (%lu)"), pos);
4547 pos = 1;
4548 }
4549 if (size == 0 || size > 64
4550 || (pos + size - 1) > 63)
4551 {
4552 as_bad (_("Improper insert size (%lu, position %lu)"),
4553 size, pos);
4554 size = 1;
4555 }
4556
4557 if (pos < 32 && (pos + size - 1) < 32)
4558 {
4559 s = "dins";
4560 fmt = "t,r,+A,+B";
4561 }
4562 else if (pos >= 32)
4563 {
4564 s = "dinsu";
4565 fmt = "t,r,+E,+F";
4566 }
4567 else
4568 {
4569 s = "dinsm";
4570 fmt = "t,r,+A,+F";
4571 }
4572 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s,
4573 fmt, treg, sreg, pos, pos + size - 1);
4574 }
4575 return;
4576
252b5132
RH
4577 case M_DDIV_3:
4578 dbl = 1;
4579 case M_DIV_3:
4580 s = "mflo";
4581 goto do_div3;
4582 case M_DREM_3:
4583 dbl = 1;
4584 case M_REM_3:
4585 s = "mfhi";
4586 do_div3:
4587 if (treg == 0)
4588 {
4589 as_warn (_("Divide by zero."));
4590 if (mips_trap)
17a2f251 4591 macro_build (NULL, &icnt, NULL, "teq", "s,t,q", 0, 0, 7);
252b5132 4592 else
17a2f251 4593 macro_build (NULL, &icnt, NULL, "break", "c", 7);
252b5132
RH
4594 return;
4595 }
4596
b34976b6 4597 mips_emit_delays (TRUE);
252b5132
RH
4598 ++mips_opts.noreorder;
4599 mips_any_noreorder = 1;
4600 if (mips_trap)
4601 {
17a2f251
TS
4602 macro_build (NULL, &icnt, NULL, "teq", "s,t,q", treg, 0, 7);
4603 macro_build (NULL, &icnt, NULL, dbl ? "ddiv" : "div", "z,s,t",
4604 sreg, treg);
252b5132
RH
4605 }
4606 else
4607 {
4608 expr1.X_add_number = 8;
17a2f251
TS
4609 macro_build (NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4610 macro_build (NULL, &icnt, NULL, dbl ? "ddiv" : "div", "z,s,t",
4611 sreg, treg);
4612 macro_build (NULL, &icnt,NULL, "break", "c", 7);
252b5132
RH
4613 }
4614 expr1.X_add_number = -1;
17a2f251
TS
4615 macro_build (NULL, &icnt, &expr1, dbl ? "daddiu" : "addiu", "t,r,j",
4616 AT, 0, BFD_RELOC_LO16);
252b5132 4617 expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
17a2f251 4618 macro_build (NULL, &icnt, &expr1, "bne", "s,t,p", treg, AT);
252b5132
RH
4619 if (dbl)
4620 {
4621 expr1.X_add_number = 1;
17a2f251
TS
4622 macro_build (NULL, &icnt, &expr1, "daddiu", "t,r,j", AT, 0,
4623 BFD_RELOC_LO16);
4624 macro_build (NULL, &icnt, NULL, "dsll32", "d,w,<", AT, AT, 31);
252b5132
RH
4625 }
4626 else
4627 {
4628 expr1.X_add_number = 0x80000000;
17a2f251
TS
4629 macro_build (NULL, &icnt, &expr1, "lui", "t,u", AT,
4630 BFD_RELOC_HI16);
252b5132
RH
4631 }
4632 if (mips_trap)
4633 {
17a2f251 4634 macro_build (NULL, &icnt, NULL, "teq", "s,t,q", sreg, AT, 6);
252b5132
RH
4635 /* We want to close the noreorder block as soon as possible, so
4636 that later insns are available for delay slot filling. */
4637 --mips_opts.noreorder;
4638 }
4639 else
4640 {
4641 expr1.X_add_number = 8;
17a2f251
TS
4642 macro_build (NULL, &icnt, &expr1, "bne", "s,t,p", sreg, AT);
4643 macro_build (NULL, &icnt, NULL, "nop", "", 0);
252b5132
RH
4644
4645 /* We want to close the noreorder block as soon as possible, so
4646 that later insns are available for delay slot filling. */
4647 --mips_opts.noreorder;
4648
17a2f251 4649 macro_build (NULL, &icnt, NULL, "break", "c", 6);
252b5132 4650 }
17a2f251 4651 macro_build (NULL, &icnt, NULL, s, "d", dreg);
252b5132
RH
4652 break;
4653
4654 case M_DIV_3I:
4655 s = "div";
4656 s2 = "mflo";
4657 goto do_divi;
4658 case M_DIVU_3I:
4659 s = "divu";
4660 s2 = "mflo";
4661 goto do_divi;
4662 case M_REM_3I:
4663 s = "div";
4664 s2 = "mfhi";
4665 goto do_divi;
4666 case M_REMU_3I:
4667 s = "divu";
4668 s2 = "mfhi";
4669 goto do_divi;
4670 case M_DDIV_3I:
4671 dbl = 1;
4672 s = "ddiv";
4673 s2 = "mflo";
4674 goto do_divi;
4675 case M_DDIVU_3I:
4676 dbl = 1;
4677 s = "ddivu";
4678 s2 = "mflo";
4679 goto do_divi;
4680 case M_DREM_3I:
4681 dbl = 1;
4682 s = "ddiv";
4683 s2 = "mfhi";
4684 goto do_divi;
4685 case M_DREMU_3I:
4686 dbl = 1;
4687 s = "ddivu";
4688 s2 = "mfhi";
4689 do_divi:
4690 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4691 {
4692 as_warn (_("Divide by zero."));
4693 if (mips_trap)
17a2f251 4694 macro_build (NULL, &icnt, NULL, "teq", "s,t,q", 0, 0, 7);
252b5132 4695 else
17a2f251 4696 macro_build (NULL, &icnt, NULL, "break", "c", 7);
252b5132
RH
4697 return;
4698 }
4699 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4700 {
4701 if (strcmp (s2, "mflo") == 0)
ea1fb5dc 4702 move_register (&icnt, dreg, sreg);
252b5132 4703 else
ea1fb5dc 4704 move_register (&icnt, dreg, 0);
252b5132
RH
4705 return;
4706 }
4707 if (imm_expr.X_op == O_constant
4708 && imm_expr.X_add_number == -1
4709 && s[strlen (s) - 1] != 'u')
4710 {
4711 if (strcmp (s2, "mflo") == 0)
4712 {
17a2f251
TS
4713 macro_build (NULL, &icnt, NULL, dbl ? "dneg" : "neg", "d,w",
4714 dreg, sreg);
252b5132
RH
4715 }
4716 else
ea1fb5dc 4717 move_register (&icnt, dreg, 0);
252b5132
RH
4718 return;
4719 }
4720
4721 load_register (&icnt, AT, &imm_expr, dbl);
17a2f251
TS
4722 macro_build (NULL, &icnt, NULL, s, "z,s,t", sreg, AT);
4723 macro_build (NULL, &icnt, NULL, s2, "d", dreg);
252b5132
RH
4724 break;
4725
4726 case M_DIVU_3:
4727 s = "divu";
4728 s2 = "mflo";
4729 goto do_divu3;
4730 case M_REMU_3:
4731 s = "divu";
4732 s2 = "mfhi";
4733 goto do_divu3;
4734 case M_DDIVU_3:
4735 s = "ddivu";
4736 s2 = "mflo";
4737 goto do_divu3;
4738 case M_DREMU_3:
4739 s = "ddivu";
4740 s2 = "mfhi";
4741 do_divu3:
b34976b6 4742 mips_emit_delays (TRUE);
252b5132
RH
4743 ++mips_opts.noreorder;
4744 mips_any_noreorder = 1;
4745 if (mips_trap)
4746 {
17a2f251
TS
4747 macro_build (NULL, &icnt, NULL, "teq", "s,t,q", treg, 0, 7);
4748 macro_build (NULL, &icnt, NULL, s, "z,s,t", sreg, treg);
252b5132
RH
4749 /* We want to close the noreorder block as soon as possible, so
4750 that later insns are available for delay slot filling. */
4751 --mips_opts.noreorder;
4752 }
4753 else
4754 {
4755 expr1.X_add_number = 8;
17a2f251
TS
4756 macro_build (NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4757 macro_build (NULL, &icnt, NULL, s, "z,s,t", sreg, treg);
252b5132
RH
4758
4759 /* We want to close the noreorder block as soon as possible, so
4760 that later insns are available for delay slot filling. */
4761 --mips_opts.noreorder;
17a2f251 4762 macro_build (NULL, &icnt, NULL, "break", "c", 7);
252b5132 4763 }
17a2f251 4764 macro_build (NULL, &icnt, NULL, s2, "d", dreg);
252b5132
RH
4765 return;
4766
4767 case M_DLA_AB:
4768 dbl = 1;
4769 case M_LA_AB:
4770 /* Load the address of a symbol into a register. If breg is not
4771 zero, we then add a base register to it. */
4772
3bec30a8
TS
4773 if (dbl && HAVE_32BIT_GPRS)
4774 as_warn (_("dla used to load 32-bit register"));
4775
c90bbe5b 4776 if (! dbl && HAVE_64BIT_OBJECTS)
3bec30a8
TS
4777 as_warn (_("la used to load 64-bit address"));
4778
0c11417f
MR
4779 if (offset_expr.X_op == O_constant
4780 && offset_expr.X_add_number >= -0x8000
4781 && offset_expr.X_add_number < 0x8000)
4782 {
17a2f251 4783 macro_build (NULL, &icnt, &offset_expr,
b7c7d6c1 4784 (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
17a2f251 4785 "t,r,j", treg, sreg, BFD_RELOC_LO16);
0c11417f
MR
4786 return;
4787 }
4788
afdbd6d0
CD
4789 if (treg == breg)
4790 {
4791 tempreg = AT;
4792 used_at = 1;
4793 }
4794 else
4795 {
4796 tempreg = treg;
4797 used_at = 0;
4798 }
4799
252b5132
RH
4800 /* When generating embedded PIC code, we permit expressions of
4801 the form
afdbd6d0
CD
4802 la $treg,foo-bar
4803 la $treg,foo-bar($breg)
bb2d6cd7 4804 where bar is an address in the current section. These are used
252b5132
RH
4805 when getting the addresses of functions. We don't permit
4806 X_add_number to be non-zero, because if the symbol is
4807 external the relaxing code needs to know that any addend is
4808 purely the offset to X_op_symbol. */
4809 if (mips_pic == EMBEDDED_PIC
4810 && offset_expr.X_op == O_subtract
49309057 4811 && (symbol_constant_p (offset_expr.X_op_symbol)
bb2d6cd7 4812 ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
49309057
ILT
4813 : (symbol_equated_p (offset_expr.X_op_symbol)
4814 && (S_GET_SEGMENT
4815 (symbol_get_value_expression (offset_expr.X_op_symbol)
4816 ->X_add_symbol)
bb2d6cd7 4817 == now_seg)))
bb2d6cd7
GK
4818 && (offset_expr.X_add_number == 0
4819 || OUTPUT_FLAVOR == bfd_target_elf_flavour))
252b5132 4820 {
afdbd6d0
CD
4821 if (breg == 0)
4822 {
4823 tempreg = treg;
4824 used_at = 0;
17a2f251
TS
4825 macro_build (NULL, &icnt, &offset_expr, "lui", "t,u", tempreg,
4826 BFD_RELOC_PCREL_HI16_S);
afdbd6d0
CD
4827 }
4828 else
4829 {
17a2f251
TS
4830 macro_build (NULL, &icnt, &offset_expr, "lui", "t,u", tempreg,
4831 BFD_RELOC_PCREL_HI16_S);
4832 macro_build (NULL, &icnt, NULL,
4d34fb5f 4833 (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu",
afdbd6d0
CD
4834 "d,v,t", tempreg, tempreg, breg);
4835 }
17a2f251 4836 macro_build (NULL, &icnt, &offset_expr,
4d34fb5f 4837 (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
17a2f251 4838 "t,r,j", treg, tempreg, BFD_RELOC_PCREL_LO16);
afdbd6d0
CD
4839 if (! used_at)
4840 return;
4841 break;
252b5132
RH
4842 }
4843
4844 if (offset_expr.X_op != O_symbol
4845 && offset_expr.X_op != O_constant)
4846 {
4847 as_bad (_("expression too complex"));
4848 offset_expr.X_op = O_constant;
4849 }
4850
252b5132 4851 if (offset_expr.X_op == O_constant)
4d34fb5f
TS
4852 load_register (&icnt, tempreg, &offset_expr,
4853 ((mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
4854 ? (dbl || HAVE_64BIT_ADDRESSES)
4855 : HAVE_64BIT_ADDRESSES));
252b5132
RH
4856 else if (mips_pic == NO_PIC)
4857 {
d6bc6245 4858 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 4859 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
4860 Otherwise we want
4861 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
4862 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4863 If we have a constant, we need two instructions anyhow,
d6bc6245 4864 so we may as well always use the latter form.
76b3015f 4865
d6bc6245
TS
4866 With 64bit address space and a usable $at we want
4867 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4868 lui $at,<sym> (BFD_RELOC_HI16_S)
4869 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4870 daddiu $at,<sym> (BFD_RELOC_LO16)
4871 dsll32 $tempreg,0
3a482fd5 4872 daddu $tempreg,$tempreg,$at
76b3015f 4873
c03099e6 4874 If $at is already in use, we use a path which is suboptimal
d6bc6245
TS
4875 on superscalar processors.
4876 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4877 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4878 dsll $tempreg,16
4879 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
4880 dsll $tempreg,16
4881 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
4882 */
f9419b05 4883 char *p = NULL;
d6bc6245 4884 if (HAVE_64BIT_ADDRESSES)
252b5132 4885 {
d6bc6245
TS
4886 /* We don't do GP optimization for now because RELAX_ENCODE can't
4887 hold the data for such large chunks. */
4888
460597ba 4889 if (used_at == 0 && ! mips_opts.noat)
98d3f06f
KH
4890 {
4891 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
17a2f251 4892 tempreg, BFD_RELOC_MIPS_HIGHEST);
98d3f06f 4893 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
17a2f251 4894 AT, BFD_RELOC_HI16_S);
98d3f06f 4895 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
17a2f251 4896 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
98d3f06f 4897 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
17a2f251
TS
4898 AT, AT, BFD_RELOC_LO16);
4899 macro_build (p, &icnt, NULL, "dsll32", "d,w,<",
4900 tempreg, tempreg, 0);
4901 macro_build (p, &icnt, NULL, "daddu", "d,v,t",
4902 tempreg, tempreg, AT);
98d3f06f
KH
4903 used_at = 1;
4904 }
4905 else
4906 {
4907 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
17a2f251 4908 tempreg, BFD_RELOC_MIPS_HIGHEST);
98d3f06f 4909 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
17a2f251
TS
4910 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
4911 macro_build (p, &icnt, NULL, "dsll", "d,w,<",
98d3f06f
KH
4912 tempreg, tempreg, 16);
4913 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
17a2f251
TS
4914 tempreg, tempreg, BFD_RELOC_HI16_S);
4915 macro_build (p, &icnt, NULL, "dsll", "d,w,<",
98d3f06f
KH
4916 tempreg, tempreg, 16);
4917 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
17a2f251 4918 tempreg, tempreg, BFD_RELOC_LO16);
98d3f06f
KH
4919 }
4920 }
4921 else
4922 {
4923 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4924 && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
4925 {
4926 frag_grow (20);
17a2f251
TS
4927 macro_build (NULL, &icnt, &offset_expr, ADDRESS_ADDI_INSN,
4928 "t,r,j", tempreg, mips_gp_register,
4929 BFD_RELOC_GPREL16);
98d3f06f
KH
4930 p = frag_var (rs_machine_dependent, 8, 0,
4931 RELAX_ENCODE (4, 8, 0, 4, 0,
4932 mips_opts.warn_about_macros),
4933 offset_expr.X_add_symbol, 0, NULL);
4934 }
4935 macro_build_lui (p, &icnt, &offset_expr, tempreg);
4936 if (p != NULL)
4937 p += 4;
f899b4b8 4938 macro_build (p, &icnt, &offset_expr, ADDRESS_ADDI_INSN,
17a2f251 4939 "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
98d3f06f 4940 }
252b5132 4941 }
f5040a92 4942 else if (mips_pic == SVR4_PIC && ! mips_big_got && ! HAVE_NEWABI)
252b5132 4943 {
9117d219
NC
4944 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
4945
252b5132
RH
4946 /* If this is a reference to an external symbol, and there
4947 is no constant, we want
4948 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9117d219
NC
4949 or if tempreg is PIC_CALL_REG
4950 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
252b5132
RH
4951 For a local symbol, we want
4952 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4953 nop
4954 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4955
4956 If we have a small constant, and this is a reference to
4957 an external symbol, we want
4958 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4959 nop
4960 addiu $tempreg,$tempreg,<constant>
4961 For a local symbol, we want the same instruction
4962 sequence, but we output a BFD_RELOC_LO16 reloc on the
4963 addiu instruction.
4964
4965 If we have a large constant, and this is a reference to
4966 an external symbol, we want
4967 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4968 lui $at,<hiconstant>
4969 addiu $at,$at,<loconstant>
4970 addu $tempreg,$tempreg,$at
4971 For a local symbol, we want the same instruction
4972 sequence, but we output a BFD_RELOC_LO16 reloc on the
ed6fb7bd 4973 addiu instruction.
ed6fb7bd
SC
4974 */
4975
252b5132
RH
4976 expr1.X_add_number = offset_expr.X_add_number;
4977 offset_expr.X_add_number = 0;
4978 frag_grow (32);
9117d219
NC
4979 if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4980 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
17a2f251
TS
4981 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
4982 tempreg, lw_reloc_type, mips_gp_register);
252b5132
RH
4983 if (expr1.X_add_number == 0)
4984 {
4985 int off;
f9419b05 4986 char *p;
252b5132
RH
4987
4988 if (breg == 0)
4989 off = 0;
4990 else
4991 {
4992 /* We're going to put in an addu instruction using
4993 tempreg, so we may as well insert the nop right
4994 now. */
17a2f251 4995 macro_build (NULL, &icnt, NULL, "nop", "");
252b5132
RH
4996 off = 4;
4997 }
4998 p = frag_var (rs_machine_dependent, 8 - off, 0,
4999 RELAX_ENCODE (0, 8 - off, -4 - off, 4 - off, 0,
5000 (breg == 0
5001 ? mips_opts.warn_about_macros
5002 : 0)),
c4e7957c 5003 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5004 if (breg == 0)
5005 {
17a2f251 5006 macro_build (p, &icnt, NULL, "nop", "");
252b5132
RH
5007 p += 4;
5008 }
f899b4b8 5009 macro_build (p, &icnt, &expr1, ADDRESS_ADDI_INSN,
17a2f251 5010 "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
252b5132
RH
5011 /* FIXME: If breg == 0, and the next instruction uses
5012 $tempreg, then if this variant case is used an extra
5013 nop will be generated. */
5014 }
5015 else if (expr1.X_add_number >= -0x8000
5016 && expr1.X_add_number < 0x8000)
5017 {
17a2f251
TS
5018 macro_build (NULL, &icnt, NULL, "nop", "");
5019 macro_build (NULL, &icnt, &expr1, ADDRESS_ADDI_INSN,
5020 "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
c4e7957c
TS
5021 frag_var (rs_machine_dependent, 0, 0,
5022 RELAX_ENCODE (0, 0, -12, -4, 0, 0),
5023 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5024 }
5025 else
5026 {
5027 int off1;
5028
5029 /* If we are going to add in a base register, and the
5030 target register and the base register are the same,
5031 then we are using AT as a temporary register. Since
5032 we want to load the constant into AT, we add our
5033 current AT (from the global offset table) and the
5034 register into the register now, and pretend we were
5035 not using a base register. */
5036 if (breg != treg)
5037 off1 = 0;
5038 else
5039 {
17a2f251
TS
5040 macro_build (NULL, &icnt, NULL, "nop", "");
5041 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5042 treg, AT, breg);
252b5132
RH
5043 breg = 0;
5044 tempreg = treg;
5045 off1 = -8;
5046 }
5047
5048 /* Set mips_optimize around the lui instruction to avoid
5049 inserting an unnecessary nop after the lw. */
5050 hold_mips_optimize = mips_optimize;
5051 mips_optimize = 2;
c4e7957c 5052 macro_build_lui (NULL, &icnt, &expr1, AT);
252b5132
RH
5053 mips_optimize = hold_mips_optimize;
5054
17a2f251
TS
5055 macro_build (NULL, &icnt, &expr1, ADDRESS_ADDI_INSN, "t,r,j",
5056 AT, AT, BFD_RELOC_LO16);
5057 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5058 tempreg, tempreg, AT);
c4e7957c
TS
5059 frag_var (rs_machine_dependent, 0, 0,
5060 RELAX_ENCODE (0, 0, -16 + off1, -8, 0, 0),
5061 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5062 used_at = 1;
5063 }
5064 }
f5040a92
AO
5065 else if (mips_pic == SVR4_PIC && ! mips_big_got && HAVE_NEWABI)
5066 {
5067 char *p = NULL;
5068 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_DISP;
5069 int adj = 0;
5070
5071 /* If this is a reference to an external, and there is no
5072 constant, or local symbol (*), with or without a
5073 constant, we want
5074 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5075 or if tempreg is PIC_CALL_REG
5076 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5077
5078 If we have a small constant, and this is a reference to
5079 an external symbol, we want
5080 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5081 addiu $tempreg,$tempreg,<constant>
5082
5083 If we have a large constant, and this is a reference to
5084 an external symbol, we want
5085 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5086 lui $at,<hiconstant>
5087 addiu $at,$at,<loconstant>
5088 addu $tempreg,$tempreg,$at
5089
5090 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
5091 local symbols, even though it introduces an additional
5092 instruction. */
5093
5094 frag_grow (28);
5095 if (offset_expr.X_add_number == 0 && tempreg == PIC_CALL_REG)
5096 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
5097 if (offset_expr.X_add_number)
5098 {
5099 frag_now->tc_frag_data.tc_fr_offset =
5100 expr1.X_add_number = offset_expr.X_add_number;
5101 offset_expr.X_add_number = 0;
5102
17a2f251
TS
5103 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5104 "t,o(b)", tempreg, lw_reloc_type,
5105 mips_gp_register);
f5040a92
AO
5106
5107 if (expr1.X_add_number >= -0x8000
5108 && expr1.X_add_number < 0x8000)
5109 {
17a2f251
TS
5110 macro_build (NULL, &icnt, &expr1, ADDRESS_ADDI_INSN,
5111 "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
f5040a92
AO
5112 p = frag_var (rs_machine_dependent, 4, 0,
5113 RELAX_ENCODE (8, 4, 0, 0, 0, 0),
5114 offset_expr.X_add_symbol, 0, NULL);
5115 }
ecd13cd3 5116 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92
AO
5117 {
5118 int dreg;
5119
5120 /* If we are going to add in a base register, and the
5121 target register and the base register are the same,
5122 then we are using AT as a temporary register. Since
5123 we want to load the constant into AT, we add our
5124 current AT (from the global offset table) and the
5125 register into the register now, and pretend we were
5126 not using a base register. */
5127 if (breg != treg)
5128 dreg = tempreg;
5129 else
5130 {
5131 assert (tempreg == AT);
17a2f251
TS
5132 macro_build (NULL, &icnt,NULL, ADDRESS_ADD_INSN,
5133 "d,v,t", treg, AT, breg);
f5040a92
AO
5134 dreg = treg;
5135 adj = 4;
5136 }
5137
17a2f251
TS
5138 macro_build_lui (NULL, &icnt, &expr1, AT);
5139 macro_build (NULL, &icnt, &expr1, ADDRESS_ADDI_INSN,
5140 "t,r,j", AT, AT, BFD_RELOC_LO16);
5141 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5142 dreg, dreg, AT);
f5040a92
AO
5143
5144 p = frag_var (rs_machine_dependent, 4 + adj, 0,
5145 RELAX_ENCODE (16 + adj, 4 + adj,
5146 0, 0, 0, 0),
5147 offset_expr.X_add_symbol, 0, NULL);
5148
5149 used_at = 1;
5150 }
5151 else
5152 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5153
5154 offset_expr.X_add_number = expr1.X_add_number;
5155
f899b4b8 5156 macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
17a2f251 5157 "t,o(b)", tempreg, BFD_RELOC_MIPS_GOT_DISP,
f5040a92
AO
5158 mips_gp_register);
5159 if (adj)
5160 {
17a2f251 5161 macro_build (p + 4, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
f899b4b8 5162 treg, tempreg, breg);
f5040a92
AO
5163 breg = 0;
5164 tempreg = treg;
5165 }
5166 }
5167 else
5168 {
17a2f251
TS
5169 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5170 "t,o(b)", tempreg, lw_reloc_type,
5171 mips_gp_register);
f5040a92
AO
5172 if (lw_reloc_type != BFD_RELOC_MIPS_GOT_DISP)
5173 p = frag_var (rs_machine_dependent, 0, 0,
5174 RELAX_ENCODE (0, 0, -4, 0, 0, 0),
5175 offset_expr.X_add_symbol, 0, NULL);
5176 }
5177
5178 if (! p)
5179 {
5180 /* To avoid confusion in tc_gen_reloc, we must ensure
5181 that this does not become a variant frag. */
5182 frag_wane (frag_now);
5183 frag_new (0);
5184 }
5185 }
5186 else if (mips_pic == SVR4_PIC && ! HAVE_NEWABI)
252b5132
RH
5187 {
5188 int gpdel;
f9419b05 5189 char *p;
9117d219
NC
5190 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5191 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
ed6fb7bd 5192 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
252b5132
RH
5193
5194 /* This is the large GOT case. If this is a reference to an
5195 external symbol, and there is no constant, we want
5196 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5197 addu $tempreg,$tempreg,$gp
5198 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
9117d219
NC
5199 or if tempreg is PIC_CALL_REG
5200 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5201 addu $tempreg,$tempreg,$gp
5202 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
252b5132
RH
5203 For a local symbol, we want
5204 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5205 nop
5206 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5207
5208 If we have a small constant, and this is a reference to
5209 an external symbol, we want
5210 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5211 addu $tempreg,$tempreg,$gp
5212 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5213 nop
5214 addiu $tempreg,$tempreg,<constant>
5215 For a local symbol, we want
5216 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5217 nop
5218 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
5219
5220 If we have a large constant, and this is a reference to
5221 an external symbol, we want
5222 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5223 addu $tempreg,$tempreg,$gp
5224 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5225 lui $at,<hiconstant>
5226 addiu $at,$at,<loconstant>
5227 addu $tempreg,$tempreg,$at
5228 For a local symbol, we want
5229 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5230 lui $at,<hiconstant>
5231 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
5232 addu $tempreg,$tempreg,$at
f5040a92 5233 */
438c16b8 5234
252b5132
RH
5235 expr1.X_add_number = offset_expr.X_add_number;
5236 offset_expr.X_add_number = 0;
5237 frag_grow (52);
f7ea7ef2 5238 if (reg_needs_delay (mips_gp_register))
252b5132
RH
5239 gpdel = 4;
5240 else
5241 gpdel = 0;
9117d219
NC
5242 if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
5243 {
5244 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5245 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5246 }
17a2f251 5247 macro_build (NULL, &icnt, &offset_expr, "lui", "t,u",
9117d219 5248 tempreg, lui_reloc_type);
17a2f251
TS
5249 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5250 tempreg, tempreg, mips_gp_register);
5251 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5252 tempreg, lw_reloc_type, tempreg);
252b5132
RH
5253 if (expr1.X_add_number == 0)
5254 {
5255 int off;
5256
5257 if (breg == 0)
5258 off = 0;
5259 else
5260 {
5261 /* We're going to put in an addu instruction using
5262 tempreg, so we may as well insert the nop right
5263 now. */
17a2f251 5264 macro_build (NULL, &icnt, NULL, "nop", "");
252b5132
RH
5265 off = 4;
5266 }
5267
5268 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5269 RELAX_ENCODE (12 + off, 12 + gpdel, gpdel,
5270 8 + gpdel, 0,
5271 (breg == 0
5272 ? mips_opts.warn_about_macros
5273 : 0)),
c4e7957c 5274 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5275 }
5276 else if (expr1.X_add_number >= -0x8000
5277 && expr1.X_add_number < 0x8000)
5278 {
17a2f251
TS
5279 macro_build (NULL, &icnt, NULL, "nop", "");
5280 macro_build (NULL, &icnt, &expr1, ADDRESS_ADDI_INSN, "t,r,j",
5281 tempreg, tempreg, BFD_RELOC_LO16);
252b5132
RH
5282
5283 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5284 RELAX_ENCODE (20, 12 + gpdel, gpdel, 8 + gpdel, 0,
5285 (breg == 0
5286 ? mips_opts.warn_about_macros
5287 : 0)),
c4e7957c 5288 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5289 }
5290 else
5291 {
5292 int adj, dreg;
5293
5294 /* If we are going to add in a base register, and the
5295 target register and the base register are the same,
5296 then we are using AT as a temporary register. Since
5297 we want to load the constant into AT, we add our
5298 current AT (from the global offset table) and the
5299 register into the register now, and pretend we were
5300 not using a base register. */
5301 if (breg != treg)
5302 {
5303 adj = 0;
5304 dreg = tempreg;
5305 }
5306 else
5307 {
5308 assert (tempreg == AT);
17a2f251
TS
5309 macro_build (NULL, &icnt, NULL, "nop", "");
5310 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5311 treg, AT, breg);
252b5132
RH
5312 dreg = treg;
5313 adj = 8;
5314 }
5315
5316 /* Set mips_optimize around the lui instruction to avoid
5317 inserting an unnecessary nop after the lw. */
5318 hold_mips_optimize = mips_optimize;
5319 mips_optimize = 2;
c4e7957c 5320 macro_build_lui (NULL, &icnt, &expr1, AT);
252b5132
RH
5321 mips_optimize = hold_mips_optimize;
5322
17a2f251
TS
5323 macro_build (NULL, &icnt, &expr1, ADDRESS_ADDI_INSN, "t,r,j",
5324 AT, AT, BFD_RELOC_LO16);
5325 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5326 dreg, dreg, AT);
252b5132
RH
5327
5328 p = frag_var (rs_machine_dependent, 16 + gpdel + adj, 0,
5329 RELAX_ENCODE (24 + adj, 16 + gpdel + adj, gpdel,
5330 8 + gpdel, 0,
5331 (breg == 0
5332 ? mips_opts.warn_about_macros
5333 : 0)),
c4e7957c 5334 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5335
5336 used_at = 1;
5337 }
5338
5339 if (gpdel > 0)
5340 {
5341 /* This is needed because this instruction uses $gp, but
f5040a92 5342 the first instruction on the main stream does not. */
17a2f251 5343 macro_build (p, &icnt, NULL, "nop", "");
252b5132
RH
5344 p += 4;
5345 }
ed6fb7bd 5346
f899b4b8
TS
5347 macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5348 tempreg, local_reloc_type, mips_gp_register);
252b5132 5349 p += 4;
f5040a92 5350 if (expr1.X_add_number >= -0x8000
252b5132
RH
5351 && expr1.X_add_number < 0x8000)
5352 {
17a2f251 5353 macro_build (p, &icnt, NULL, "nop", "");
252b5132 5354 p += 4;
f899b4b8 5355 macro_build (p, &icnt, &expr1, ADDRESS_ADDI_INSN,
17a2f251 5356 "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
252b5132 5357 /* FIXME: If add_number is 0, and there was no base
f5040a92
AO
5358 register, the external symbol case ended with a load,
5359 so if the symbol turns out to not be external, and
5360 the next instruction uses tempreg, an unnecessary nop
5361 will be inserted. */
252b5132
RH
5362 }
5363 else
5364 {
5365 if (breg == treg)
5366 {
5367 /* We must add in the base register now, as in the
f5040a92 5368 external symbol case. */
252b5132 5369 assert (tempreg == AT);
17a2f251 5370 macro_build (p, &icnt, NULL, "nop", "");
252b5132 5371 p += 4;
17a2f251
TS
5372 macro_build (p, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5373 treg, AT, breg);
252b5132
RH
5374 p += 4;
5375 tempreg = treg;
5376 /* We set breg to 0 because we have arranged to add
f5040a92 5377 it in in both cases. */
252b5132
RH
5378 breg = 0;
5379 }
5380
5381 macro_build_lui (p, &icnt, &expr1, AT);
5382 p += 4;
17a2f251
TS
5383 macro_build (p, &icnt, &expr1, ADDRESS_ADDI_INSN, "t,r,j",
5384 AT, AT, BFD_RELOC_LO16);
252b5132 5385 p += 4;
17a2f251
TS
5386 macro_build (p, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5387 tempreg, tempreg, AT);
252b5132
RH
5388 p += 4;
5389 }
5390 }
f5040a92
AO
5391 else if (mips_pic == SVR4_PIC && HAVE_NEWABI)
5392 {
5393 char *p = NULL;
5394 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5395 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
5396 int adj = 0;
5397
5398 /* This is the large GOT case. If this is a reference to an
5399 external symbol, and there is no constant, we want
5400 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5401 add $tempreg,$tempreg,$gp
5402 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5403 or if tempreg is PIC_CALL_REG
5404 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5405 add $tempreg,$tempreg,$gp
5406 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5407
5408 If we have a small constant, and this is a reference to
5409 an external symbol, we want
5410 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5411 add $tempreg,$tempreg,$gp
5412 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5413 addi $tempreg,$tempreg,<constant>
5414
5415 If we have a large constant, and this is a reference to
5416 an external symbol, we want
5417 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5418 addu $tempreg,$tempreg,$gp
5419 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5420 lui $at,<hiconstant>
5421 addi $at,$at,<loconstant>
5422 add $tempreg,$tempreg,$at
5423
5424 If we have NewABI, and we know it's a local symbol, we want
5425 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
5426 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
5427 otherwise we have to resort to GOT_HI16/GOT_LO16. */
5428
5429 frag_grow (40);
5430
5431 frag_now->tc_frag_data.tc_fr_offset =
5432 expr1.X_add_number = offset_expr.X_add_number;
5433 offset_expr.X_add_number = 0;
5434
5435 if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
5436 {
5437 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5438 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5439 }
17a2f251 5440 macro_build (NULL, &icnt, &offset_expr, "lui", "t,u",
f5040a92 5441 tempreg, lui_reloc_type);
17a2f251
TS
5442 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5443 tempreg, tempreg, mips_gp_register);
5444 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
f5040a92
AO
5445 "t,o(b)", tempreg, lw_reloc_type, tempreg);
5446
5447 if (expr1.X_add_number == 0)
5448 {
5449 p = frag_var (rs_machine_dependent, 8, 0,
5450 RELAX_ENCODE (12, 8, 0, 4, 0,
5451 mips_opts.warn_about_macros),
5452 offset_expr.X_add_symbol, 0, NULL);
684022ea 5453 }
f5040a92
AO
5454 else if (expr1.X_add_number >= -0x8000
5455 && expr1.X_add_number < 0x8000)
5456 {
17a2f251
TS
5457 macro_build (NULL, &icnt, &expr1, ADDRESS_ADDI_INSN, "t,r,j",
5458 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92
AO
5459 p = frag_var (rs_machine_dependent, 8, 0,
5460 RELAX_ENCODE (16, 8, 0, 4, 0,
5461 mips_opts.warn_about_macros),
5462 offset_expr.X_add_symbol, 0, NULL);
5463 }
ecd13cd3 5464 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92
AO
5465 {
5466 int dreg;
5467
5468 /* If we are going to add in a base register, and the
5469 target register and the base register are the same,
5470 then we are using AT as a temporary register. Since
5471 we want to load the constant into AT, we add our
5472 current AT (from the global offset table) and the
5473 register into the register now, and pretend we were
5474 not using a base register. */
5475 if (breg != treg)
5476 dreg = tempreg;
5477 else
5478 {
5479 assert (tempreg == AT);
17a2f251
TS
5480 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5481 treg, AT, breg);
f5040a92
AO
5482 dreg = treg;
5483 adj = 4;
5484 }
5485
5486 /* Set mips_optimize around the lui instruction to avoid
5487 inserting an unnecessary nop after the lw. */
17a2f251
TS
5488 macro_build_lui (NULL, &icnt, &expr1, AT);
5489 macro_build (NULL, &icnt, &expr1, ADDRESS_ADDI_INSN,
5490 "t,r,j", AT, AT, BFD_RELOC_LO16);
5491 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5492 dreg, dreg, AT);
f5040a92
AO
5493
5494 p = frag_var (rs_machine_dependent, 8 + adj, 0,
5495 RELAX_ENCODE (24 + adj, 8 + adj,
5496 0, 4, 0,
5497 (breg == 0
5498 ? mips_opts.warn_about_macros
5499 : 0)),
5500 offset_expr.X_add_symbol, 0, NULL);
5501
5502 used_at = 1;
5503 }
5504 else
5505 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5506
5507 offset_expr.X_add_number = expr1.X_add_number;
f899b4b8 5508 macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 5509 tempreg, BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f899b4b8 5510 macro_build (p + 4, &icnt, &offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 5511 tempreg, tempreg, BFD_RELOC_MIPS_GOT_OFST);
f5040a92
AO
5512 if (adj)
5513 {
17a2f251
TS
5514 macro_build (p + 8, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5515 treg, tempreg, breg);
f5040a92
AO
5516 breg = 0;
5517 tempreg = treg;
5518 }
5519 }
252b5132
RH
5520 else if (mips_pic == EMBEDDED_PIC)
5521 {
5522 /* We use
cdf6fd85 5523 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132 5524 */
17a2f251
TS
5525 macro_build (NULL, &icnt, &offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5526 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
252b5132
RH
5527 }
5528 else
5529 abort ();
5530
5531 if (breg != 0)
4d34fb5f
TS
5532 {
5533 char *s;
5534
5535 if (mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
b7c7d6c1 5536 s = (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu";
4d34fb5f 5537 else
f899b4b8 5538 s = ADDRESS_ADD_INSN;
4d34fb5f 5539
17a2f251 5540 macro_build (NULL, &icnt, NULL, s, "d,v,t", treg, tempreg, breg);
4d34fb5f 5541 }
252b5132
RH
5542
5543 if (! used_at)
5544 return;
5545
5546 break;
5547
5548 case M_J_A:
5549 /* The j instruction may not be used in PIC code, since it
5550 requires an absolute address. We convert it to a b
5551 instruction. */
5552 if (mips_pic == NO_PIC)
17a2f251 5553 macro_build (NULL, &icnt, &offset_expr, "j", "a");
252b5132 5554 else
17a2f251 5555 macro_build (NULL, &icnt, &offset_expr, "b", "p");
252b5132
RH
5556 return;
5557
5558 /* The jal instructions must be handled as macros because when
5559 generating PIC code they expand to multi-instruction
5560 sequences. Normally they are simple instructions. */
5561 case M_JAL_1:
5562 dreg = RA;
5563 /* Fall through. */
5564 case M_JAL_2:
5565 if (mips_pic == NO_PIC
5566 || mips_pic == EMBEDDED_PIC)
17a2f251 5567 macro_build (NULL, &icnt, NULL, "jalr", "d,s", dreg, sreg);
252b5132
RH
5568 else if (mips_pic == SVR4_PIC)
5569 {
5570 if (sreg != PIC_CALL_REG)
5571 as_warn (_("MIPS PIC call to register other than $25"));
bdaaa2e1 5572
17a2f251 5573 macro_build (NULL, &icnt, NULL, "jalr", "d,s", dreg, sreg);
6478892d 5574 if (! HAVE_NEWABI)
252b5132 5575 {
6478892d
TS
5576 if (mips_cprestore_offset < 0)
5577 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5578 else
5579 {
7a621144
DJ
5580 if (! mips_frame_reg_valid)
5581 {
5582 as_warn (_("No .frame pseudo-op used in PIC code"));
5583 /* Quiet this warning. */
5584 mips_frame_reg_valid = 1;
5585 }
5586 if (! mips_cprestore_valid)
5587 {
5588 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5589 /* Quiet this warning. */
5590 mips_cprestore_valid = 1;
5591 }
6478892d 5592 expr1.X_add_number = mips_cprestore_offset;
17a2f251 5593 macro_build_ldst_constoffset (NULL, &icnt, &expr1,
f899b4b8
TS
5594 ADDRESS_LOAD_INSN,
5595 mips_gp_register,
256ab948
TS
5596 mips_frame_reg,
5597 HAVE_64BIT_ADDRESSES);
6478892d 5598 }
252b5132
RH
5599 }
5600 }
5601 else
5602 abort ();
5603
5604 return;
5605
5606 case M_JAL_A:
5607 if (mips_pic == NO_PIC)
17a2f251 5608 macro_build (NULL, &icnt, &offset_expr, "jal", "a");
252b5132
RH
5609 else if (mips_pic == SVR4_PIC)
5610 {
f9419b05
TS
5611 char *p;
5612
252b5132
RH
5613 /* If this is a reference to an external symbol, and we are
5614 using a small GOT, we want
5615 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5616 nop
f9419b05 5617 jalr $ra,$25
252b5132
RH
5618 nop
5619 lw $gp,cprestore($sp)
5620 The cprestore value is set using the .cprestore
5621 pseudo-op. If we are using a big GOT, we want
5622 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5623 addu $25,$25,$gp
5624 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
5625 nop
f9419b05 5626 jalr $ra,$25
252b5132
RH
5627 nop
5628 lw $gp,cprestore($sp)
5629 If the symbol is not external, we want
5630 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5631 nop
5632 addiu $25,$25,<sym> (BFD_RELOC_LO16)
f9419b05 5633 jalr $ra,$25
252b5132 5634 nop
438c16b8 5635 lw $gp,cprestore($sp)
f5040a92
AO
5636
5637 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
5638 sequences above, minus nops, unless the symbol is local,
5639 which enables us to use GOT_PAGE/GOT_OFST (big got) or
5640 GOT_DISP. */
438c16b8 5641 if (HAVE_NEWABI)
252b5132 5642 {
f5040a92
AO
5643 if (! mips_big_got)
5644 {
5645 frag_grow (4);
17a2f251
TS
5646 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5647 "t,o(b)", PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
f5040a92
AO
5648 mips_gp_register);
5649 frag_var (rs_machine_dependent, 0, 0,
5650 RELAX_ENCODE (0, 0, -4, 0, 0, 0),
5651 offset_expr.X_add_symbol, 0, NULL);
5652 }
5653 else
5654 {
5655 frag_grow (20);
17a2f251
TS
5656 macro_build (NULL, &icnt, &offset_expr, "lui", "t,u",
5657 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_HI16);
5658 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5659 PIC_CALL_REG, PIC_CALL_REG, mips_gp_register);
5660 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5661 "t,o(b)", PIC_CALL_REG,
5662 BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
f5040a92
AO
5663 p = frag_var (rs_machine_dependent, 8, 0,
5664 RELAX_ENCODE (12, 8, 0, 4, 0, 0),
5665 offset_expr.X_add_symbol, 0, NULL);
f899b4b8
TS
5666 macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5667 "t,o(b)", PIC_CALL_REG,
17a2f251 5668 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f899b4b8 5669 macro_build (p + 4, &icnt, &offset_expr, ADDRESS_ADDI_INSN,
dc9461f6 5670 "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
17a2f251 5671 BFD_RELOC_MIPS_GOT_OFST);
f5040a92 5672 }
684022ea 5673
438c16b8 5674 macro_build_jalr (icnt, &offset_expr);
252b5132
RH
5675 }
5676 else
5677 {
438c16b8
TS
5678 frag_grow (40);
5679 if (! mips_big_got)
5680 {
17a2f251
TS
5681 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5682 "t,o(b)", PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
5683 mips_gp_register);
5684 macro_build (NULL, &icnt, NULL, "nop", "");
438c16b8
TS
5685 p = frag_var (rs_machine_dependent, 4, 0,
5686 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5687 offset_expr.X_add_symbol, 0, NULL);
5688 }
252b5132 5689 else
252b5132 5690 {
438c16b8
TS
5691 int gpdel;
5692
5693 if (reg_needs_delay (mips_gp_register))
5694 gpdel = 4;
5695 else
5696 gpdel = 0;
17a2f251
TS
5697 macro_build (NULL, &icnt, &offset_expr, "lui", "t,u",
5698 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_HI16);
5699 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5700 PIC_CALL_REG, PIC_CALL_REG, mips_gp_register);
5701 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5702 "t,o(b)", PIC_CALL_REG,
5703 BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
5704 macro_build (NULL, &icnt, NULL, "nop", "");
438c16b8
TS
5705 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5706 RELAX_ENCODE (16, 12 + gpdel, gpdel,
5707 8 + gpdel, 0, 0),
5708 offset_expr.X_add_symbol, 0, NULL);
5709 if (gpdel > 0)
5710 {
17a2f251 5711 macro_build (p, &icnt, NULL, "nop", "");
438c16b8
TS
5712 p += 4;
5713 }
f899b4b8 5714 macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
17a2f251
TS
5715 "t,o(b)", PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
5716 mips_gp_register);
438c16b8 5717 p += 4;
17a2f251 5718 macro_build (p, &icnt, NULL, "nop", "");
252b5132
RH
5719 p += 4;
5720 }
f899b4b8 5721 macro_build (p, &icnt, &offset_expr, ADDRESS_ADDI_INSN,
438c16b8 5722 "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
17a2f251 5723 BFD_RELOC_LO16);
438c16b8
TS
5724 macro_build_jalr (icnt, &offset_expr);
5725
6478892d
TS
5726 if (mips_cprestore_offset < 0)
5727 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5728 else
5729 {
7a621144
DJ
5730 if (! mips_frame_reg_valid)
5731 {
5732 as_warn (_("No .frame pseudo-op used in PIC code"));
5733 /* Quiet this warning. */
5734 mips_frame_reg_valid = 1;
5735 }
5736 if (! mips_cprestore_valid)
5737 {
5738 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5739 /* Quiet this warning. */
5740 mips_cprestore_valid = 1;
5741 }
6478892d 5742 if (mips_opts.noreorder)
17a2f251 5743 macro_build (NULL, &icnt, NULL, "nop", "");
6478892d 5744 expr1.X_add_number = mips_cprestore_offset;
17a2f251 5745 macro_build_ldst_constoffset (NULL, &icnt, &expr1,
f899b4b8
TS
5746 ADDRESS_LOAD_INSN,
5747 mips_gp_register,
256ab948
TS
5748 mips_frame_reg,
5749 HAVE_64BIT_ADDRESSES);
6478892d 5750 }
252b5132
RH
5751 }
5752 }
5753 else if (mips_pic == EMBEDDED_PIC)
5754 {
17a2f251 5755 macro_build (NULL, &icnt, &offset_expr, "bal", "p");
252b5132
RH
5756 /* The linker may expand the call to a longer sequence which
5757 uses $at, so we must break rather than return. */
5758 break;
5759 }
5760 else
5761 abort ();
5762
5763 return;
5764
5765 case M_LB_AB:
5766 s = "lb";
5767 goto ld;
5768 case M_LBU_AB:
5769 s = "lbu";
5770 goto ld;
5771 case M_LH_AB:
5772 s = "lh";
5773 goto ld;
5774 case M_LHU_AB:
5775 s = "lhu";
5776 goto ld;
5777 case M_LW_AB:
5778 s = "lw";
5779 goto ld;
5780 case M_LWC0_AB:
5781 s = "lwc0";
bdaaa2e1 5782 /* Itbl support may require additional care here. */
252b5132
RH
5783 coproc = 1;
5784 goto ld;
5785 case M_LWC1_AB:
5786 s = "lwc1";
bdaaa2e1 5787 /* Itbl support may require additional care here. */
252b5132
RH
5788 coproc = 1;
5789 goto ld;
5790 case M_LWC2_AB:
5791 s = "lwc2";
bdaaa2e1 5792 /* Itbl support may require additional care here. */
252b5132
RH
5793 coproc = 1;
5794 goto ld;
5795 case M_LWC3_AB:
5796 s = "lwc3";
bdaaa2e1 5797 /* Itbl support may require additional care here. */
252b5132
RH
5798 coproc = 1;
5799 goto ld;
5800 case M_LWL_AB:
5801 s = "lwl";
5802 lr = 1;
5803 goto ld;
5804 case M_LWR_AB:
5805 s = "lwr";
5806 lr = 1;
5807 goto ld;
5808 case M_LDC1_AB:
fef14a42 5809 if (mips_opts.arch == CPU_R4650)
252b5132
RH
5810 {
5811 as_bad (_("opcode not supported on this processor"));
5812 return;
5813 }
5814 s = "ldc1";
bdaaa2e1 5815 /* Itbl support may require additional care here. */
252b5132
RH
5816 coproc = 1;
5817 goto ld;
5818 case M_LDC2_AB:
5819 s = "ldc2";
bdaaa2e1 5820 /* Itbl support may require additional care here. */
252b5132
RH
5821 coproc = 1;
5822 goto ld;
5823 case M_LDC3_AB:
5824 s = "ldc3";
bdaaa2e1 5825 /* Itbl support may require additional care here. */
252b5132
RH
5826 coproc = 1;
5827 goto ld;
5828 case M_LDL_AB:
5829 s = "ldl";
5830 lr = 1;
5831 goto ld;
5832 case M_LDR_AB:
5833 s = "ldr";
5834 lr = 1;
5835 goto ld;
5836 case M_LL_AB:
5837 s = "ll";
5838 goto ld;
5839 case M_LLD_AB:
5840 s = "lld";
5841 goto ld;
5842 case M_LWU_AB:
5843 s = "lwu";
5844 ld:
5845 if (breg == treg || coproc || lr)
5846 {
5847 tempreg = AT;
5848 used_at = 1;
5849 }
5850 else
5851 {
5852 tempreg = treg;
5853 used_at = 0;
5854 }
5855 goto ld_st;
5856 case M_SB_AB:
5857 s = "sb";
5858 goto st;
5859 case M_SH_AB:
5860 s = "sh";
5861 goto st;
5862 case M_SW_AB:
5863 s = "sw";
5864 goto st;
5865 case M_SWC0_AB:
5866 s = "swc0";
bdaaa2e1 5867 /* Itbl support may require additional care here. */
252b5132
RH
5868 coproc = 1;
5869 goto st;
5870 case M_SWC1_AB:
5871 s = "swc1";
bdaaa2e1 5872 /* Itbl support may require additional care here. */
252b5132
RH
5873 coproc = 1;
5874 goto st;
5875 case M_SWC2_AB:
5876 s = "swc2";
bdaaa2e1 5877 /* Itbl support may require additional care here. */
252b5132
RH
5878 coproc = 1;
5879 goto st;
5880 case M_SWC3_AB:
5881 s = "swc3";
bdaaa2e1 5882 /* Itbl support may require additional care here. */
252b5132
RH
5883 coproc = 1;
5884 goto st;
5885 case M_SWL_AB:
5886 s = "swl";
5887 goto st;
5888 case M_SWR_AB:
5889 s = "swr";
5890 goto st;
5891 case M_SC_AB:
5892 s = "sc";
5893 goto st;
5894 case M_SCD_AB:
5895 s = "scd";
5896 goto st;
5897 case M_SDC1_AB:
fef14a42 5898 if (mips_opts.arch == CPU_R4650)
252b5132
RH
5899 {
5900 as_bad (_("opcode not supported on this processor"));
5901 return;
5902 }
5903 s = "sdc1";
5904 coproc = 1;
bdaaa2e1 5905 /* Itbl support may require additional care here. */
252b5132
RH
5906 goto st;
5907 case M_SDC2_AB:
5908 s = "sdc2";
bdaaa2e1 5909 /* Itbl support may require additional care here. */
252b5132
RH
5910 coproc = 1;
5911 goto st;
5912 case M_SDC3_AB:
5913 s = "sdc3";
bdaaa2e1 5914 /* Itbl support may require additional care here. */
252b5132
RH
5915 coproc = 1;
5916 goto st;
5917 case M_SDL_AB:
5918 s = "sdl";
5919 goto st;
5920 case M_SDR_AB:
5921 s = "sdr";
5922 st:
5923 tempreg = AT;
5924 used_at = 1;
5925 ld_st:
bdaaa2e1 5926 /* Itbl support may require additional care here. */
252b5132
RH
5927 if (mask == M_LWC1_AB
5928 || mask == M_SWC1_AB
5929 || mask == M_LDC1_AB
5930 || mask == M_SDC1_AB
5931 || mask == M_L_DAB
5932 || mask == M_S_DAB)
5933 fmt = "T,o(b)";
5934 else if (coproc)
5935 fmt = "E,o(b)";
5936 else
5937 fmt = "t,o(b)";
5938
256ab948
TS
5939 /* Sign-extending 32-bit constants makes their handling easier.
5940 The HAVE_64BIT_GPRS... part is due to the linux kernel hack
5941 described below. */
5942 if ((! HAVE_64BIT_ADDRESSES
5943 && (! HAVE_64BIT_GPRS && offset_expr.X_op == O_constant))
d17e7bce
TS
5944 && (offset_expr.X_op == O_constant)
5945 && ! ((offset_expr.X_add_number & ~((bfd_vma) 0x7fffffff))
5946 == ~((bfd_vma) 0x7fffffff)))
ae826530 5947 {
d17e7bce 5948 if (offset_expr.X_add_number & ~((bfd_vma) 0xffffffff))
ae826530
TS
5949 as_bad (_("too large constant specified"));
5950
256ab948
TS
5951 offset_expr.X_add_number = (((offset_expr.X_add_number & 0xffffffff)
5952 ^ 0x80000000) - 0x80000000);
ae826530 5953 }
256ab948 5954
afdbd6d0
CD
5955 /* For embedded PIC, we allow loads where the offset is calculated
5956 by subtracting a symbol in the current segment from an unknown
5957 symbol, relative to a base register, e.g.:
5958 <op> $treg, <sym>-<localsym>($breg)
5959 This is used by the compiler for switch statements. */
76b3015f 5960 if (mips_pic == EMBEDDED_PIC
afdbd6d0
CD
5961 && offset_expr.X_op == O_subtract
5962 && (symbol_constant_p (offset_expr.X_op_symbol)
5963 ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
5964 : (symbol_equated_p (offset_expr.X_op_symbol)
5965 && (S_GET_SEGMENT
5966 (symbol_get_value_expression (offset_expr.X_op_symbol)
5967 ->X_add_symbol)
5968 == now_seg)))
5969 && breg != 0
5970 && (offset_expr.X_add_number == 0
5971 || OUTPUT_FLAVOR == bfd_target_elf_flavour))
5972 {
5973 /* For this case, we output the instructions:
5974 lui $tempreg,<sym> (BFD_RELOC_PCREL_HI16_S)
5975 addiu $tempreg,$tempreg,$breg
5976 <op> $treg,<sym>($tempreg) (BFD_RELOC_PCREL_LO16)
5977 If the relocation would fit entirely in 16 bits, it would be
5978 nice to emit:
5979 <op> $treg,<sym>($breg) (BFD_RELOC_PCREL_LO16)
5980 instead, but that seems quite difficult. */
17a2f251
TS
5981 macro_build (NULL, &icnt, &offset_expr, "lui", "t,u", tempreg,
5982 BFD_RELOC_PCREL_HI16_S);
5983 macro_build (NULL, &icnt, NULL,
afdbd6d0
CD
5984 ((bfd_arch_bits_per_address (stdoutput) == 32
5985 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
b7c7d6c1 5986 ? "addu" : "daddu"),
afdbd6d0 5987 "d,v,t", tempreg, tempreg, breg);
17a2f251
TS
5988 macro_build (NULL, &icnt, &offset_expr, s, fmt, treg,
5989 BFD_RELOC_PCREL_LO16, tempreg);
afdbd6d0
CD
5990 if (! used_at)
5991 return;
5992 break;
5993 }
5994
252b5132
RH
5995 if (offset_expr.X_op != O_constant
5996 && offset_expr.X_op != O_symbol)
5997 {
5998 as_bad (_("expression too complex"));
5999 offset_expr.X_op = O_constant;
6000 }
6001
6002 /* A constant expression in PIC code can be handled just as it
6003 is in non PIC code. */
6004 if (mips_pic == NO_PIC
6005 || offset_expr.X_op == O_constant)
6006 {
f9419b05
TS
6007 char *p;
6008
252b5132
RH
6009 /* If this is a reference to a GP relative symbol, and there
6010 is no base register, we want
cdf6fd85 6011 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
6012 Otherwise, if there is no base register, we want
6013 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
6014 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6015 If we have a constant, we need two instructions anyhow,
6016 so we always use the latter form.
6017
6018 If we have a base register, and this is a reference to a
6019 GP relative symbol, we want
6020 addu $tempreg,$breg,$gp
cdf6fd85 6021 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
6022 Otherwise we want
6023 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
6024 addu $tempreg,$tempreg,$breg
6025 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245 6026 With a constant we always use the latter case.
76b3015f 6027
d6bc6245
TS
6028 With 64bit address space and no base register and $at usable,
6029 we want
6030 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
6031 lui $at,<sym> (BFD_RELOC_HI16_S)
6032 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
6033 dsll32 $tempreg,0
6034 daddu $tempreg,$at
6035 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6036 If we have a base register, we want
6037 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
6038 lui $at,<sym> (BFD_RELOC_HI16_S)
6039 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
6040 daddu $at,$breg
6041 dsll32 $tempreg,0
6042 daddu $tempreg,$at
6043 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6044
6045 Without $at we can't generate the optimal path for superscalar
6046 processors here since this would require two temporary registers.
6047 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
6048 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
6049 dsll $tempreg,16
6050 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
6051 dsll $tempreg,16
6052 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6053 If we have a base register, we want
6054 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
6055 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
6056 dsll $tempreg,16
6057 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
6058 dsll $tempreg,16
6059 daddu $tempreg,$tempreg,$breg
6060 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6373ee54
CD
6061
6062 If we have 64-bit addresses, as an optimization, for
6063 addresses which are 32-bit constants (e.g. kseg0/kseg1
6064 addresses) we fall back to the 32-bit address generation
78d32a17
MR
6065 mechanism since it is more efficient. Note that due to
6066 the signed offset used by memory operations, the 32-bit
6067 range is shifted down by 32768 here. This code should
6373ee54
CD
6068 probably attempt to generate 64-bit constants more
6069 efficiently in general.
9214dd3b
AO
6070
6071 As an extension for architectures with 64-bit registers,
6072 we don't truncate 64-bit addresses given as literal
6073 constants down to 32 bits, to support existing practice
6074 in the mips64 Linux (the kernel), that compiles source
6075 files with -mabi=64, assembling them as o32 or n32 (with
6076 -Wa,-32 or -Wa,-n32). This is not beautiful, but since
6077 the whole kernel is loaded into a memory region that is
6078 addressible with sign-extended 32-bit addresses, it is
6079 wasteful to compute the upper 32 bits of every
6080 non-literal address, that takes more space and time.
6081 Some day this should probably be implemented as an
6082 assembler option, such that the kernel doesn't have to
6083 use such ugly hacks, even though it will still have to
6084 end up converting the binary to ELF32 for a number of
6085 platforms whose boot loaders don't support ELF64
6086 binaries. */
256ab948
TS
6087 if ((HAVE_64BIT_ADDRESSES
6088 && ! (offset_expr.X_op == O_constant
6089 && IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000)))
6090 || (HAVE_64BIT_GPRS
6091 && offset_expr.X_op == O_constant
6092 && ! IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000)))
d6bc6245
TS
6093 {
6094 p = NULL;
6095
6096 /* We don't do GP optimization for now because RELAX_ENCODE can't
6097 hold the data for such large chunks. */
6098
460597ba 6099 if (used_at == 0 && ! mips_opts.noat)
d6bc6245
TS
6100 {
6101 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
17a2f251 6102 tempreg, BFD_RELOC_MIPS_HIGHEST);
d6bc6245 6103 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
17a2f251 6104 AT, BFD_RELOC_HI16_S);
d6bc6245 6105 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
17a2f251 6106 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
d6bc6245 6107 if (breg != 0)
17a2f251
TS
6108 macro_build (p, &icnt, NULL, "daddu", "d,v,t",
6109 AT, AT, breg);
6110 macro_build (p, &icnt, NULL, "dsll32", "d,w,<",
6111 tempreg, tempreg, 0);
6112 macro_build (p, &icnt, NULL, "daddu", "d,v,t",
6113 tempreg, tempreg, AT);
6114 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
6115 BFD_RELOC_LO16, tempreg);
d6bc6245
TS
6116 used_at = 1;
6117 }
6118 else
6119 {
6120 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
17a2f251 6121 tempreg, BFD_RELOC_MIPS_HIGHEST);
d6bc6245 6122 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
17a2f251
TS
6123 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
6124 macro_build (p, &icnt, NULL, "dsll", "d,w,<",
6125 tempreg, tempreg, 16);
d6bc6245 6126 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
17a2f251
TS
6127 tempreg, tempreg, BFD_RELOC_HI16_S);
6128 macro_build (p, &icnt, NULL, "dsll", "d,w,<",
6129 tempreg, tempreg, 16);
d6bc6245 6130 if (breg != 0)
17a2f251
TS
6131 macro_build (p, &icnt, NULL, "daddu", "d,v,t",
6132 tempreg, tempreg, breg);
6133 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
6134 BFD_RELOC_LO16, tempreg);
d6bc6245
TS
6135 }
6136
6137 return;
6138 }
256ab948
TS
6139
6140 if (offset_expr.X_op == O_constant
ecd13cd3 6141 && ! IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000))
5a7a0b7b 6142 as_bad (_("load/store address overflow (max 32 bits)"));
76b3015f 6143
252b5132
RH
6144 if (breg == 0)
6145 {
e7d556df 6146 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
252b5132
RH
6147 || nopic_need_relax (offset_expr.X_add_symbol, 1))
6148 p = NULL;
6149 else
6150 {
6151 frag_grow (20);
17a2f251
TS
6152 macro_build (NULL, &icnt, &offset_expr, s, fmt, treg,
6153 BFD_RELOC_GPREL16, mips_gp_register);
252b5132
RH
6154 p = frag_var (rs_machine_dependent, 8, 0,
6155 RELAX_ENCODE (4, 8, 0, 4, 0,
6156 (mips_opts.warn_about_macros
6157 || (used_at
6158 && mips_opts.noat))),
956cd1d6 6159 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
6160 used_at = 0;
6161 }
6162 macro_build_lui (p, &icnt, &offset_expr, tempreg);
6163 if (p != NULL)
6164 p += 4;
6165 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
17a2f251 6166 BFD_RELOC_LO16, tempreg);
252b5132
RH
6167 }
6168 else
6169 {
e7d556df 6170 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
252b5132
RH
6171 || nopic_need_relax (offset_expr.X_add_symbol, 1))
6172 p = NULL;
6173 else
6174 {
6175 frag_grow (28);
17a2f251
TS
6176 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6177 tempreg, breg, mips_gp_register);
6178 macro_build (NULL, &icnt, &offset_expr, s, fmt, treg,
6179 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6180 p = frag_var (rs_machine_dependent, 12, 0,
6181 RELAX_ENCODE (8, 12, 0, 8, 0, 0),
956cd1d6 6182 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
6183 }
6184 macro_build_lui (p, &icnt, &offset_expr, tempreg);
6185 if (p != NULL)
6186 p += 4;
17a2f251
TS
6187 macro_build (p, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6188 tempreg, tempreg, breg);
252b5132
RH
6189 if (p != NULL)
6190 p += 4;
6191 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
17a2f251 6192 BFD_RELOC_LO16, tempreg);
252b5132
RH
6193 }
6194 }
6195 else if (mips_pic == SVR4_PIC && ! mips_big_got)
6196 {
f9419b05 6197 char *p;
ed6fb7bd 6198 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
f9419b05 6199
252b5132
RH
6200 /* If this is a reference to an external symbol, we want
6201 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6202 nop
6203 <op> $treg,0($tempreg)
6204 Otherwise we want
6205 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6206 nop
6207 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6208 <op> $treg,0($tempreg)
f5040a92
AO
6209
6210 For NewABI, we want
6211 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6212 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
6213
252b5132
RH
6214 If there is a base register, we add it to $tempreg before
6215 the <op>. If there is a constant, we stick it in the
6216 <op> instruction. We don't handle constants larger than
6217 16 bits, because we have no way to load the upper 16 bits
6218 (actually, we could handle them for the subset of cases
6219 in which we are not using $at). */
6220 assert (offset_expr.X_op == O_symbol);
f5040a92
AO
6221 if (HAVE_NEWABI)
6222 {
17a2f251
TS
6223 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
6224 "t,o(b)", tempreg, BFD_RELOC_MIPS_GOT_PAGE,
6225 mips_gp_register);
f5040a92 6226 if (breg != 0)
17a2f251
TS
6227 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6228 tempreg, tempreg, breg);
6229 macro_build (NULL, &icnt, &offset_expr, s, fmt, treg,
6230 BFD_RELOC_MIPS_GOT_OFST, tempreg);
f5040a92
AO
6231
6232 if (! used_at)
6233 return;
6234
6235 break;
6236 }
252b5132
RH
6237 expr1.X_add_number = offset_expr.X_add_number;
6238 offset_expr.X_add_number = 0;
6239 if (expr1.X_add_number < -0x8000
6240 || expr1.X_add_number >= 0x8000)
6241 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6242 frag_grow (20);
17a2f251
TS
6243 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6244 tempreg, lw_reloc_type, mips_gp_register);
6245 macro_build (NULL, &icnt, NULL, "nop", "");
bdaaa2e1 6246 p = frag_var (rs_machine_dependent, 4, 0,
252b5132 6247 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
c4e7957c 6248 offset_expr.X_add_symbol, 0, NULL);
f899b4b8 6249 macro_build (p, &icnt, &offset_expr, ADDRESS_ADDI_INSN,
17a2f251 6250 "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
252b5132 6251 if (breg != 0)
17a2f251
TS
6252 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6253 tempreg, tempreg, breg);
6254 macro_build (NULL, &icnt, &expr1, s, fmt, treg, BFD_RELOC_LO16,
6255 tempreg);
252b5132 6256 }
f5040a92 6257 else if (mips_pic == SVR4_PIC && ! HAVE_NEWABI)
252b5132
RH
6258 {
6259 int gpdel;
f9419b05 6260 char *p;
252b5132
RH
6261
6262 /* If this is a reference to an external symbol, we want
6263 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6264 addu $tempreg,$tempreg,$gp
6265 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6266 <op> $treg,0($tempreg)
6267 Otherwise we want
6268 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6269 nop
6270 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6271 <op> $treg,0($tempreg)
6272 If there is a base register, we add it to $tempreg before
6273 the <op>. If there is a constant, we stick it in the
6274 <op> instruction. We don't handle constants larger than
6275 16 bits, because we have no way to load the upper 16 bits
6276 (actually, we could handle them for the subset of cases
f5040a92 6277 in which we are not using $at). */
252b5132
RH
6278 assert (offset_expr.X_op == O_symbol);
6279 expr1.X_add_number = offset_expr.X_add_number;
6280 offset_expr.X_add_number = 0;
6281 if (expr1.X_add_number < -0x8000
6282 || expr1.X_add_number >= 0x8000)
6283 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
f7ea7ef2 6284 if (reg_needs_delay (mips_gp_register))
252b5132
RH
6285 gpdel = 4;
6286 else
6287 gpdel = 0;
6288 frag_grow (36);
17a2f251
TS
6289 macro_build (NULL, &icnt, &offset_expr, "lui", "t,u", tempreg,
6290 BFD_RELOC_MIPS_GOT_HI16);
6291 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6292 tempreg, tempreg, mips_gp_register);
6293 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6294 tempreg, BFD_RELOC_MIPS_GOT_LO16, tempreg);
252b5132
RH
6295 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
6296 RELAX_ENCODE (12, 12 + gpdel, gpdel, 8 + gpdel, 0, 0),
c4e7957c 6297 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
6298 if (gpdel > 0)
6299 {
17a2f251 6300 macro_build (p, &icnt, NULL, "nop", "");
252b5132
RH
6301 p += 4;
6302 }
17a2f251
TS
6303 macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6304 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132 6305 p += 4;
17a2f251 6306 macro_build (p, &icnt, NULL, "nop", "");
252b5132 6307 p += 4;
17a2f251
TS
6308 macro_build (p, &icnt, &offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
6309 tempreg, tempreg, BFD_RELOC_LO16);
252b5132 6310 if (breg != 0)
17a2f251
TS
6311 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6312 tempreg, tempreg, breg);
6313 macro_build (NULL, &icnt, &expr1, s, fmt, treg, BFD_RELOC_LO16,
6314 tempreg);
252b5132 6315 }
f5040a92
AO
6316 else if (mips_pic == SVR4_PIC && HAVE_NEWABI)
6317 {
6318 char *p;
6319 int bregsz = breg != 0 ? 4 : 0;
6320
6321 /* If this is a reference to an external symbol, we want
6322 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6323 add $tempreg,$tempreg,$gp
6324 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6325 <op> $treg,<ofst>($tempreg)
6326 Otherwise, for local symbols, we want:
6327 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6328 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
6329 assert (offset_expr.X_op == O_symbol);
6330 frag_now->tc_frag_data.tc_fr_offset =
6331 expr1.X_add_number = offset_expr.X_add_number;
6332 offset_expr.X_add_number = 0;
6333 if (expr1.X_add_number < -0x8000
6334 || expr1.X_add_number >= 0x8000)
6335 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6336 frag_grow (36);
17a2f251
TS
6337 macro_build (NULL, &icnt, &offset_expr, "lui", "t,u", tempreg,
6338 BFD_RELOC_MIPS_GOT_HI16);
6339 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6340 tempreg, tempreg, mips_gp_register);
6341 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6342 tempreg, BFD_RELOC_MIPS_GOT_LO16, tempreg);
f5040a92 6343 if (breg != 0)
17a2f251
TS
6344 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6345 tempreg, tempreg, breg);
6346 macro_build (NULL, &icnt, &expr1, s, fmt, treg, BFD_RELOC_LO16,
6347 tempreg);
684022ea 6348
f5040a92
AO
6349 offset_expr.X_add_number = expr1.X_add_number;
6350 p = frag_var (rs_machine_dependent, 12 + bregsz, 0,
6351 RELAX_ENCODE (16 + bregsz, 8 + bregsz,
6352 0, 4 + bregsz, 0, 0),
6353 offset_expr.X_add_symbol, 0, NULL);
f899b4b8 6354 macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 6355 tempreg, BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 6356 if (breg != 0)
17a2f251
TS
6357 macro_build (p + 4, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6358 tempreg, tempreg, breg);
f5040a92 6359 macro_build (p + 4 + bregsz, &icnt, &offset_expr, s, fmt, treg,
17a2f251 6360 BFD_RELOC_MIPS_GOT_OFST, tempreg);
f5040a92 6361 }
252b5132
RH
6362 else if (mips_pic == EMBEDDED_PIC)
6363 {
6364 /* If there is no base register, we want
cdf6fd85 6365 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
6366 If there is a base register, we want
6367 addu $tempreg,$breg,$gp
cdf6fd85 6368 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
6369 */
6370 assert (offset_expr.X_op == O_symbol);
6371 if (breg == 0)
6372 {
17a2f251
TS
6373 macro_build (NULL, &icnt, &offset_expr, s, fmt, treg,
6374 BFD_RELOC_GPREL16, mips_gp_register);
252b5132
RH
6375 used_at = 0;
6376 }
6377 else
6378 {
17a2f251
TS
6379 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6380 tempreg, breg, mips_gp_register);
6381 macro_build (NULL, &icnt, &offset_expr, s, fmt, treg,
6382 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6383 }
6384 }
6385 else
6386 abort ();
6387
6388 if (! used_at)
6389 return;
6390
6391 break;
6392
6393 case M_LI:
6394 case M_LI_S:
6395 load_register (&icnt, treg, &imm_expr, 0);
6396 return;
6397
6398 case M_DLI:
6399 load_register (&icnt, treg, &imm_expr, 1);
6400 return;
6401
6402 case M_LI_SS:
6403 if (imm_expr.X_op == O_constant)
6404 {
6405 load_register (&icnt, AT, &imm_expr, 0);
17a2f251 6406 macro_build (NULL, &icnt, NULL, "mtc1", "t,G", AT, treg);
252b5132
RH
6407 break;
6408 }
6409 else
6410 {
6411 assert (offset_expr.X_op == O_symbol
6412 && strcmp (segment_name (S_GET_SEGMENT
6413 (offset_expr.X_add_symbol)),
6414 ".lit4") == 0
6415 && offset_expr.X_add_number == 0);
17a2f251
TS
6416 macro_build (NULL, &icnt, &offset_expr, "lwc1", "T,o(b)", treg,
6417 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
252b5132
RH
6418 return;
6419 }
6420
6421 case M_LI_D:
ca4e0257
RS
6422 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
6423 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
6424 order 32 bits of the value and the low order 32 bits are either
6425 zero or in OFFSET_EXPR. */
252b5132
RH
6426 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6427 {
ca4e0257 6428 if (HAVE_64BIT_GPRS)
252b5132
RH
6429 load_register (&icnt, treg, &imm_expr, 1);
6430 else
6431 {
6432 int hreg, lreg;
6433
6434 if (target_big_endian)
6435 {
6436 hreg = treg;
6437 lreg = treg + 1;
6438 }
6439 else
6440 {
6441 hreg = treg + 1;
6442 lreg = treg;
6443 }
6444
6445 if (hreg <= 31)
6446 load_register (&icnt, hreg, &imm_expr, 0);
6447 if (lreg <= 31)
6448 {
6449 if (offset_expr.X_op == O_absent)
ea1fb5dc 6450 move_register (&icnt, lreg, 0);
252b5132
RH
6451 else
6452 {
6453 assert (offset_expr.X_op == O_constant);
6454 load_register (&icnt, lreg, &offset_expr, 0);
6455 }
6456 }
6457 }
6458 return;
6459 }
6460
6461 /* We know that sym is in the .rdata section. First we get the
6462 upper 16 bits of the address. */
6463 if (mips_pic == NO_PIC)
6464 {
956cd1d6 6465 macro_build_lui (NULL, &icnt, &offset_expr, AT);
252b5132
RH
6466 }
6467 else if (mips_pic == SVR4_PIC)
6468 {
17a2f251
TS
6469 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6470 AT, BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132
RH
6471 }
6472 else if (mips_pic == EMBEDDED_PIC)
6473 {
6474 /* For embedded PIC we pick up the entire address off $gp in
6475 a single instruction. */
17a2f251
TS
6476 macro_build (NULL, &icnt, &offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
6477 AT, mips_gp_register, BFD_RELOC_GPREL16);
252b5132
RH
6478 offset_expr.X_op = O_constant;
6479 offset_expr.X_add_number = 0;
6480 }
6481 else
6482 abort ();
bdaaa2e1 6483
252b5132 6484 /* Now we load the register(s). */
ca4e0257 6485 if (HAVE_64BIT_GPRS)
17a2f251
TS
6486 macro_build (NULL, &icnt, &offset_expr, "ld", "t,o(b)", treg,
6487 BFD_RELOC_LO16, AT);
252b5132
RH
6488 else
6489 {
17a2f251
TS
6490 macro_build (NULL, &icnt, &offset_expr, "lw", "t,o(b)", treg,
6491 BFD_RELOC_LO16, AT);
f9419b05 6492 if (treg != RA)
252b5132
RH
6493 {
6494 /* FIXME: How in the world do we deal with the possible
6495 overflow here? */
6496 offset_expr.X_add_number += 4;
17a2f251
TS
6497 macro_build (NULL, &icnt, &offset_expr, "lw", "t,o(b)",
6498 treg + 1, BFD_RELOC_LO16, AT);
252b5132
RH
6499 }
6500 }
6501
6502 /* To avoid confusion in tc_gen_reloc, we must ensure that this
6503 does not become a variant frag. */
6504 frag_wane (frag_now);
6505 frag_new (0);
6506
6507 break;
6508
6509 case M_LI_DD:
ca4e0257
RS
6510 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
6511 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
6512 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
6513 the value and the low order 32 bits are either zero or in
6514 OFFSET_EXPR. */
252b5132
RH
6515 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6516 {
ca4e0257
RS
6517 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_FPRS);
6518 if (HAVE_64BIT_FPRS)
6519 {
6520 assert (HAVE_64BIT_GPRS);
17a2f251 6521 macro_build (NULL, &icnt, NULL, "dmtc1", "t,S", AT, treg);
ca4e0257 6522 }
252b5132
RH
6523 else
6524 {
17a2f251 6525 macro_build (NULL, &icnt, NULL, "mtc1", "t,G", AT, treg + 1);
252b5132 6526 if (offset_expr.X_op == O_absent)
17a2f251 6527 macro_build (NULL, &icnt, NULL, "mtc1", "t,G", 0, treg);
252b5132
RH
6528 else
6529 {
6530 assert (offset_expr.X_op == O_constant);
6531 load_register (&icnt, AT, &offset_expr, 0);
17a2f251 6532 macro_build (NULL, &icnt, NULL, "mtc1", "t,G", AT, treg);
252b5132
RH
6533 }
6534 }
6535 break;
6536 }
6537
6538 assert (offset_expr.X_op == O_symbol
6539 && offset_expr.X_add_number == 0);
6540 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
6541 if (strcmp (s, ".lit8") == 0)
6542 {
e7af610e 6543 if (mips_opts.isa != ISA_MIPS1)
252b5132 6544 {
17a2f251
TS
6545 macro_build (NULL, &icnt, &offset_expr, "ldc1", "T,o(b)", treg,
6546 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
252b5132
RH
6547 return;
6548 }
c9914766 6549 breg = mips_gp_register;
252b5132
RH
6550 r = BFD_RELOC_MIPS_LITERAL;
6551 goto dob;
6552 }
6553 else
6554 {
6555 assert (strcmp (s, RDATA_SECTION_NAME) == 0);
6556 if (mips_pic == SVR4_PIC)
17a2f251
TS
6557 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
6558 "t,o(b)", AT, BFD_RELOC_MIPS_GOT16,
6559 mips_gp_register);
252b5132
RH
6560 else
6561 {
6562 /* FIXME: This won't work for a 64 bit address. */
956cd1d6 6563 macro_build_lui (NULL, &icnt, &offset_expr, AT);
252b5132 6564 }
bdaaa2e1 6565
e7af610e 6566 if (mips_opts.isa != ISA_MIPS1)
252b5132 6567 {
17a2f251
TS
6568 macro_build (NULL, &icnt, &offset_expr, "ldc1", "T,o(b)", treg,
6569 BFD_RELOC_LO16, AT);
252b5132
RH
6570
6571 /* To avoid confusion in tc_gen_reloc, we must ensure
6572 that this does not become a variant frag. */
6573 frag_wane (frag_now);
6574 frag_new (0);
6575
6576 break;
6577 }
6578 breg = AT;
6579 r = BFD_RELOC_LO16;
6580 goto dob;
6581 }
6582
6583 case M_L_DOB:
fef14a42 6584 if (mips_opts.arch == CPU_R4650)
252b5132
RH
6585 {
6586 as_bad (_("opcode not supported on this processor"));
6587 return;
6588 }
6589 /* Even on a big endian machine $fn comes before $fn+1. We have
6590 to adjust when loading from memory. */
6591 r = BFD_RELOC_LO16;
6592 dob:
e7af610e 6593 assert (mips_opts.isa == ISA_MIPS1);
17a2f251
TS
6594 macro_build (NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6595 target_big_endian ? treg + 1 : treg, r, breg);
252b5132
RH
6596 /* FIXME: A possible overflow which I don't know how to deal
6597 with. */
6598 offset_expr.X_add_number += 4;
17a2f251
TS
6599 macro_build (NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6600 target_big_endian ? treg : treg + 1, r, breg);
252b5132
RH
6601
6602 /* To avoid confusion in tc_gen_reloc, we must ensure that this
6603 does not become a variant frag. */
6604 frag_wane (frag_now);
6605 frag_new (0);
6606
6607 if (breg != AT)
6608 return;
6609 break;
6610
6611 case M_L_DAB:
6612 /*
6613 * The MIPS assembler seems to check for X_add_number not
6614 * being double aligned and generating:
6615 * lui at,%hi(foo+1)
6616 * addu at,at,v1
6617 * addiu at,at,%lo(foo+1)
6618 * lwc1 f2,0(at)
6619 * lwc1 f3,4(at)
6620 * But, the resulting address is the same after relocation so why
6621 * generate the extra instruction?
6622 */
fef14a42 6623 if (mips_opts.arch == CPU_R4650)
252b5132
RH
6624 {
6625 as_bad (_("opcode not supported on this processor"));
6626 return;
6627 }
bdaaa2e1 6628 /* Itbl support may require additional care here. */
252b5132 6629 coproc = 1;
e7af610e 6630 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
6631 {
6632 s = "ldc1";
6633 goto ld;
6634 }
6635
6636 s = "lwc1";
6637 fmt = "T,o(b)";
6638 goto ldd_std;
6639
6640 case M_S_DAB:
fef14a42 6641 if (mips_opts.arch == CPU_R4650)
252b5132
RH
6642 {
6643 as_bad (_("opcode not supported on this processor"));
6644 return;
6645 }
6646
e7af610e 6647 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
6648 {
6649 s = "sdc1";
6650 goto st;
6651 }
6652
6653 s = "swc1";
6654 fmt = "T,o(b)";
bdaaa2e1 6655 /* Itbl support may require additional care here. */
252b5132
RH
6656 coproc = 1;
6657 goto ldd_std;
6658
6659 case M_LD_AB:
ca4e0257 6660 if (HAVE_64BIT_GPRS)
252b5132
RH
6661 {
6662 s = "ld";
6663 goto ld;
6664 }
6665
6666 s = "lw";
6667 fmt = "t,o(b)";
6668 goto ldd_std;
6669
6670 case M_SD_AB:
ca4e0257 6671 if (HAVE_64BIT_GPRS)
252b5132
RH
6672 {
6673 s = "sd";
6674 goto st;
6675 }
6676
6677 s = "sw";
6678 fmt = "t,o(b)";
6679
6680 ldd_std:
afdbd6d0
CD
6681 /* We do _not_ bother to allow embedded PIC (symbol-local_symbol)
6682 loads for the case of doing a pair of loads to simulate an 'ld'.
6683 This is not currently done by the compiler, and assembly coders
6684 writing embedded-pic code can cope. */
6685
252b5132
RH
6686 if (offset_expr.X_op != O_symbol
6687 && offset_expr.X_op != O_constant)
6688 {
6689 as_bad (_("expression too complex"));
6690 offset_expr.X_op = O_constant;
6691 }
6692
6693 /* Even on a big endian machine $fn comes before $fn+1. We have
6694 to adjust when loading from memory. We set coproc if we must
6695 load $fn+1 first. */
bdaaa2e1 6696 /* Itbl support may require additional care here. */
252b5132
RH
6697 if (! target_big_endian)
6698 coproc = 0;
6699
6700 if (mips_pic == NO_PIC
6701 || offset_expr.X_op == O_constant)
6702 {
f9419b05
TS
6703 char *p;
6704
252b5132 6705 /* If this is a reference to a GP relative symbol, we want
cdf6fd85
TS
6706 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6707 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
6708 If we have a base register, we use this
6709 addu $at,$breg,$gp
cdf6fd85
TS
6710 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6711 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
6712 If this is not a GP relative symbol, we want
6713 lui $at,<sym> (BFD_RELOC_HI16_S)
6714 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6715 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6716 If there is a base register, we add it to $at after the
6717 lui instruction. If there is a constant, we always use
6718 the last case. */
e7d556df 6719 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
252b5132
RH
6720 || nopic_need_relax (offset_expr.X_add_symbol, 1))
6721 {
6722 p = NULL;
6723 used_at = 1;
6724 }
6725 else
6726 {
6727 int off;
6728
6729 if (breg == 0)
6730 {
6731 frag_grow (28);
c9914766 6732 tempreg = mips_gp_register;
252b5132
RH
6733 off = 0;
6734 used_at = 0;
6735 }
6736 else
6737 {
6738 frag_grow (36);
17a2f251
TS
6739 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6740 AT, breg, mips_gp_register);
252b5132
RH
6741 tempreg = AT;
6742 off = 4;
6743 used_at = 1;
6744 }
6745
beae10d5 6746 /* Itbl support may require additional care here. */
17a2f251 6747 macro_build (NULL, &icnt, &offset_expr, s, fmt,
252b5132 6748 coproc ? treg + 1 : treg,
17a2f251 6749 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6750 offset_expr.X_add_number += 4;
6751
6752 /* Set mips_optimize to 2 to avoid inserting an
6753 undesired nop. */
6754 hold_mips_optimize = mips_optimize;
6755 mips_optimize = 2;
beae10d5 6756 /* Itbl support may require additional care here. */
17a2f251 6757 macro_build (NULL, &icnt, &offset_expr, s, fmt,
252b5132 6758 coproc ? treg : treg + 1,
17a2f251 6759 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6760 mips_optimize = hold_mips_optimize;
6761
6762 p = frag_var (rs_machine_dependent, 12 + off, 0,
6763 RELAX_ENCODE (8 + off, 12 + off, 0, 4 + off, 1,
6764 used_at && mips_opts.noat),
956cd1d6 6765 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
6766
6767 /* We just generated two relocs. When tc_gen_reloc
6768 handles this case, it will skip the first reloc and
6769 handle the second. The second reloc already has an
6770 extra addend of 4, which we added above. We must
6771 subtract it out, and then subtract another 4 to make
6772 the first reloc come out right. The second reloc
6773 will come out right because we are going to add 4 to
6774 offset_expr when we build its instruction below.
6775
6776 If we have a symbol, then we don't want to include
6777 the offset, because it will wind up being included
6778 when we generate the reloc. */
6779
6780 if (offset_expr.X_op == O_constant)
6781 offset_expr.X_add_number -= 8;
6782 else
6783 {
6784 offset_expr.X_add_number = -4;
6785 offset_expr.X_op = O_constant;
6786 }
6787 }
6788 macro_build_lui (p, &icnt, &offset_expr, AT);
6789 if (p != NULL)
6790 p += 4;
6791 if (breg != 0)
6792 {
17a2f251
TS
6793 macro_build (p, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6794 AT, breg, AT);
252b5132
RH
6795 if (p != NULL)
6796 p += 4;
6797 }
beae10d5 6798 /* Itbl support may require additional care here. */
252b5132
RH
6799 macro_build (p, &icnt, &offset_expr, s, fmt,
6800 coproc ? treg + 1 : treg,
17a2f251 6801 BFD_RELOC_LO16, AT);
252b5132
RH
6802 if (p != NULL)
6803 p += 4;
6804 /* FIXME: How do we handle overflow here? */
6805 offset_expr.X_add_number += 4;
beae10d5 6806 /* Itbl support may require additional care here. */
252b5132
RH
6807 macro_build (p, &icnt, &offset_expr, s, fmt,
6808 coproc ? treg : treg + 1,
17a2f251 6809 BFD_RELOC_LO16, AT);
bdaaa2e1 6810 }
252b5132
RH
6811 else if (mips_pic == SVR4_PIC && ! mips_big_got)
6812 {
6813 int off;
6814
6815 /* If this is a reference to an external symbol, we want
6816 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6817 nop
6818 <op> $treg,0($at)
6819 <op> $treg+1,4($at)
6820 Otherwise we want
6821 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6822 nop
6823 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6824 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6825 If there is a base register we add it to $at before the
6826 lwc1 instructions. If there is a constant we include it
6827 in the lwc1 instructions. */
6828 used_at = 1;
6829 expr1.X_add_number = offset_expr.X_add_number;
6830 offset_expr.X_add_number = 0;
6831 if (expr1.X_add_number < -0x8000
6832 || expr1.X_add_number >= 0x8000 - 4)
6833 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6834 if (breg == 0)
6835 off = 0;
6836 else
6837 off = 4;
6838 frag_grow (24 + off);
17a2f251
TS
6839 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6840 AT, BFD_RELOC_MIPS_GOT16, mips_gp_register);
6841 macro_build (NULL, &icnt, NULL, "nop", "");
252b5132 6842 if (breg != 0)
17a2f251
TS
6843 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6844 AT, breg, AT);
beae10d5 6845 /* Itbl support may require additional care here. */
17a2f251
TS
6846 macro_build (NULL, &icnt, &expr1, s, fmt, coproc ? treg + 1 : treg,
6847 BFD_RELOC_LO16, AT);
252b5132
RH
6848 expr1.X_add_number += 4;
6849
6850 /* Set mips_optimize to 2 to avoid inserting an undesired
6851 nop. */
6852 hold_mips_optimize = mips_optimize;
6853 mips_optimize = 2;
beae10d5 6854 /* Itbl support may require additional care here. */
17a2f251
TS
6855 macro_build (NULL, &icnt, &expr1, s, fmt, coproc ? treg : treg + 1,
6856 BFD_RELOC_LO16, AT);
252b5132
RH
6857 mips_optimize = hold_mips_optimize;
6858
6859 (void) frag_var (rs_machine_dependent, 0, 0,
6860 RELAX_ENCODE (0, 0, -16 - off, -8, 1, 0),
c4e7957c 6861 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
6862 }
6863 else if (mips_pic == SVR4_PIC)
6864 {
6865 int gpdel, off;
f9419b05 6866 char *p;
252b5132
RH
6867
6868 /* If this is a reference to an external symbol, we want
6869 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6870 addu $at,$at,$gp
6871 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
6872 nop
6873 <op> $treg,0($at)
6874 <op> $treg+1,4($at)
6875 Otherwise we want
6876 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6877 nop
6878 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6879 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6880 If there is a base register we add it to $at before the
6881 lwc1 instructions. If there is a constant we include it
6882 in the lwc1 instructions. */
6883 used_at = 1;
6884 expr1.X_add_number = offset_expr.X_add_number;
6885 offset_expr.X_add_number = 0;
6886 if (expr1.X_add_number < -0x8000
6887 || expr1.X_add_number >= 0x8000 - 4)
6888 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
c9914766 6889 if (reg_needs_delay (mips_gp_register))
252b5132
RH
6890 gpdel = 4;
6891 else
6892 gpdel = 0;
6893 if (breg == 0)
6894 off = 0;
6895 else
6896 off = 4;
6897 frag_grow (56);
17a2f251
TS
6898 macro_build (NULL, &icnt, &offset_expr, "lui", "t,u", AT,
6899 BFD_RELOC_MIPS_GOT_HI16);
6900 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6901 AT, AT, mips_gp_register);
6902 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6903 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
6904 macro_build (NULL, &icnt, NULL, "nop", "");
252b5132 6905 if (breg != 0)
17a2f251
TS
6906 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6907 AT, breg, AT);
beae10d5 6908 /* Itbl support may require additional care here. */
17a2f251
TS
6909 macro_build (NULL, &icnt, &expr1, s, fmt, coproc ? treg + 1 : treg,
6910 BFD_RELOC_LO16, AT);
252b5132
RH
6911 expr1.X_add_number += 4;
6912
6913 /* Set mips_optimize to 2 to avoid inserting an undesired
6914 nop. */
6915 hold_mips_optimize = mips_optimize;
6916 mips_optimize = 2;
beae10d5 6917 /* Itbl support may require additional care here. */
17a2f251
TS
6918 macro_build (NULL, &icnt, &expr1, s, fmt, coproc ? treg : treg + 1,
6919 BFD_RELOC_LO16, AT);
252b5132
RH
6920 mips_optimize = hold_mips_optimize;
6921 expr1.X_add_number -= 4;
6922
6923 p = frag_var (rs_machine_dependent, 16 + gpdel + off, 0,
6924 RELAX_ENCODE (24 + off, 16 + gpdel + off, gpdel,
6925 8 + gpdel + off, 1, 0),
c4e7957c 6926 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
6927 if (gpdel > 0)
6928 {
17a2f251 6929 macro_build (p, &icnt, NULL, "nop", "");
252b5132
RH
6930 p += 4;
6931 }
17a2f251
TS
6932 macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6933 AT, BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132 6934 p += 4;
17a2f251 6935 macro_build (p, &icnt, NULL, "nop", "");
252b5132
RH
6936 p += 4;
6937 if (breg != 0)
6938 {
17a2f251
TS
6939 macro_build (p, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6940 AT, breg, AT);
252b5132
RH
6941 p += 4;
6942 }
beae10d5 6943 /* Itbl support may require additional care here. */
17a2f251
TS
6944 macro_build (p, &icnt, &expr1, s, fmt, coproc ? treg + 1 : treg,
6945 BFD_RELOC_LO16, AT);
252b5132
RH
6946 p += 4;
6947 expr1.X_add_number += 4;
6948
6949 /* Set mips_optimize to 2 to avoid inserting an undesired
6950 nop. */
6951 hold_mips_optimize = mips_optimize;
6952 mips_optimize = 2;
beae10d5 6953 /* Itbl support may require additional care here. */
17a2f251
TS
6954 macro_build (p, &icnt, &expr1, s, fmt, coproc ? treg : treg + 1,
6955 BFD_RELOC_LO16, AT);
252b5132
RH
6956 mips_optimize = hold_mips_optimize;
6957 }
6958 else if (mips_pic == EMBEDDED_PIC)
6959 {
6960 /* If there is no base register, we use
cdf6fd85
TS
6961 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6962 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
6963 If we have a base register, we use
6964 addu $at,$breg,$gp
cdf6fd85
TS
6965 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6966 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
6967 */
6968 if (breg == 0)
6969 {
c9914766 6970 tempreg = mips_gp_register;
252b5132
RH
6971 used_at = 0;
6972 }
6973 else
6974 {
17a2f251
TS
6975 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6976 AT, breg, mips_gp_register);
252b5132
RH
6977 tempreg = AT;
6978 used_at = 1;
6979 }
6980
beae10d5 6981 /* Itbl support may require additional care here. */
17a2f251 6982 macro_build (NULL, &icnt, &offset_expr, s, fmt,
252b5132 6983 coproc ? treg + 1 : treg,
17a2f251 6984 BFD_RELOC_GPREL16, tempreg);
252b5132 6985 offset_expr.X_add_number += 4;
beae10d5 6986 /* Itbl support may require additional care here. */
17a2f251 6987 macro_build (NULL, &icnt, &offset_expr, s, fmt,
252b5132 6988 coproc ? treg : treg + 1,
17a2f251 6989 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6990 }
6991 else
6992 abort ();
6993
6994 if (! used_at)
6995 return;
6996
6997 break;
6998
6999 case M_LD_OB:
7000 s = "lw";
7001 goto sd_ob;
7002 case M_SD_OB:
7003 s = "sw";
7004 sd_ob:
ca4e0257 7005 assert (HAVE_32BIT_ADDRESSES);
17a2f251
TS
7006 macro_build (NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7007 BFD_RELOC_LO16, breg);
252b5132 7008 offset_expr.X_add_number += 4;
17a2f251
TS
7009 macro_build (NULL, &icnt, &offset_expr, s, "t,o(b)", treg + 1,
7010 BFD_RELOC_LO16, breg);
252b5132
RH
7011 return;
7012
7013 /* New code added to support COPZ instructions.
7014 This code builds table entries out of the macros in mip_opcodes.
7015 R4000 uses interlocks to handle coproc delays.
7016 Other chips (like the R3000) require nops to be inserted for delays.
7017
f72c8c98 7018 FIXME: Currently, we require that the user handle delays.
252b5132
RH
7019 In order to fill delay slots for non-interlocked chips,
7020 we must have a way to specify delays based on the coprocessor.
7021 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
7022 What are the side-effects of the cop instruction?
7023 What cache support might we have and what are its effects?
7024 Both coprocessor & memory require delays. how long???
bdaaa2e1 7025 What registers are read/set/modified?
252b5132
RH
7026
7027 If an itbl is provided to interpret cop instructions,
bdaaa2e1 7028 this knowledge can be encoded in the itbl spec. */
252b5132
RH
7029
7030 case M_COP0:
7031 s = "c0";
7032 goto copz;
7033 case M_COP1:
7034 s = "c1";
7035 goto copz;
7036 case M_COP2:
7037 s = "c2";
7038 goto copz;
7039 case M_COP3:
7040 s = "c3";
7041 copz:
7042 /* For now we just do C (same as Cz). The parameter will be
7043 stored in insn_opcode by mips_ip. */
17a2f251 7044 macro_build (NULL, &icnt, NULL, s, "C", ip->insn_opcode);
252b5132
RH
7045 return;
7046
ea1fb5dc
RS
7047 case M_MOVE:
7048 move_register (&icnt, dreg, sreg);
7049 return;
7050
252b5132
RH
7051#ifdef LOSING_COMPILER
7052 default:
7053 /* Try and see if this is a new itbl instruction.
7054 This code builds table entries out of the macros in mip_opcodes.
7055 FIXME: For now we just assemble the expression and pass it's
7056 value along as a 32-bit immediate.
bdaaa2e1 7057 We may want to have the assembler assemble this value,
252b5132
RH
7058 so that we gain the assembler's knowledge of delay slots,
7059 symbols, etc.
7060 Would it be more efficient to use mask (id) here? */
bdaaa2e1 7061 if (itbl_have_entries
252b5132 7062 && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
beae10d5 7063 {
252b5132
RH
7064 s = ip->insn_mo->name;
7065 s2 = "cop3";
7066 coproc = ITBL_DECODE_PNUM (immed_expr);;
17a2f251 7067 macro_build (NULL, &icnt, &immed_expr, s, "C");
252b5132 7068 return;
beae10d5 7069 }
252b5132
RH
7070 macro2 (ip);
7071 return;
7072 }
7073 if (mips_opts.noat)
7074 as_warn (_("Macro used $at after \".set noat\""));
7075}
bdaaa2e1 7076
252b5132 7077static void
17a2f251 7078macro2 (struct mips_cl_insn *ip)
252b5132
RH
7079{
7080 register int treg, sreg, dreg, breg;
7081 int tempreg;
7082 int mask;
7083 int icnt = 0;
7084 int used_at;
7085 expressionS expr1;
7086 const char *s;
7087 const char *s2;
7088 const char *fmt;
7089 int likely = 0;
7090 int dbl = 0;
7091 int coproc = 0;
7092 int lr = 0;
7093 int imm = 0;
7094 int off;
7095 offsetT maxnum;
7096 bfd_reloc_code_real_type r;
7097 char *p;
bdaaa2e1 7098
252b5132
RH
7099 treg = (ip->insn_opcode >> 16) & 0x1f;
7100 dreg = (ip->insn_opcode >> 11) & 0x1f;
7101 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
7102 mask = ip->insn_mo->mask;
bdaaa2e1 7103
252b5132
RH
7104 expr1.X_op = O_constant;
7105 expr1.X_op_symbol = NULL;
7106 expr1.X_add_symbol = NULL;
7107 expr1.X_add_number = 1;
bdaaa2e1 7108
252b5132
RH
7109 switch (mask)
7110 {
7111#endif /* LOSING_COMPILER */
7112
7113 case M_DMUL:
7114 dbl = 1;
7115 case M_MUL:
17a2f251
TS
7116 macro_build (NULL, &icnt, NULL, dbl ? "dmultu" : "multu", "s,t",
7117 sreg, treg);
7118 macro_build (NULL, &icnt, NULL, "mflo", "d", dreg);
252b5132
RH
7119 return;
7120
7121 case M_DMUL_I:
7122 dbl = 1;
7123 case M_MUL_I:
7124 /* The MIPS assembler some times generates shifts and adds. I'm
7125 not trying to be that fancy. GCC should do this for us
7126 anyway. */
7127 load_register (&icnt, AT, &imm_expr, dbl);
17a2f251
TS
7128 macro_build (NULL, &icnt, NULL, dbl ? "dmult" : "mult", "s,t",
7129 sreg, AT);
7130 macro_build (NULL, &icnt, NULL, "mflo", "d", dreg);
252b5132
RH
7131 break;
7132
7133 case M_DMULO_I:
7134 dbl = 1;
7135 case M_MULO_I:
7136 imm = 1;
7137 goto do_mulo;
7138
7139 case M_DMULO:
7140 dbl = 1;
7141 case M_MULO:
7142 do_mulo:
b34976b6 7143 mips_emit_delays (TRUE);
252b5132
RH
7144 ++mips_opts.noreorder;
7145 mips_any_noreorder = 1;
7146 if (imm)
7147 load_register (&icnt, AT, &imm_expr, dbl);
17a2f251
TS
7148 macro_build (NULL, &icnt, NULL, dbl ? "dmult" : "mult", "s,t",
7149 sreg, imm ? AT : treg);
7150 macro_build (NULL, &icnt, NULL, "mflo", "d", dreg);
7151 macro_build (NULL, &icnt, NULL, dbl ? "dsra32" : "sra", "d,w,<",
7152 dreg, dreg, RA);
7153 macro_build (NULL, &icnt, NULL, "mfhi", "d", AT);
252b5132 7154 if (mips_trap)
17a2f251 7155 macro_build (NULL, &icnt, NULL, "tne", "s,t,q", dreg, AT, 6);
252b5132
RH
7156 else
7157 {
7158 expr1.X_add_number = 8;
17a2f251
TS
7159 macro_build (NULL, &icnt, &expr1, "beq", "s,t,p", dreg, AT);
7160 macro_build (NULL, &icnt, NULL, "nop", "", 0);
7161 macro_build (NULL, &icnt, NULL, "break", "c", 6);
252b5132
RH
7162 }
7163 --mips_opts.noreorder;
17a2f251 7164 macro_build (NULL, &icnt, NULL, "mflo", "d", dreg);
252b5132
RH
7165 break;
7166
7167 case M_DMULOU_I:
7168 dbl = 1;
7169 case M_MULOU_I:
7170 imm = 1;
7171 goto do_mulou;
7172
7173 case M_DMULOU:
7174 dbl = 1;
7175 case M_MULOU:
7176 do_mulou:
b34976b6 7177 mips_emit_delays (TRUE);
252b5132
RH
7178 ++mips_opts.noreorder;
7179 mips_any_noreorder = 1;
7180 if (imm)
7181 load_register (&icnt, AT, &imm_expr, dbl);
17a2f251
TS
7182 macro_build (NULL, &icnt, NULL, dbl ? "dmultu" : "multu", "s,t",
7183 sreg, imm ? AT : treg);
7184 macro_build (NULL, &icnt, NULL, "mfhi", "d", AT);
7185 macro_build (NULL, &icnt, NULL, "mflo", "d", dreg);
252b5132 7186 if (mips_trap)
17a2f251 7187 macro_build (NULL, &icnt, NULL, "tne", "s,t,q", AT, 0, 6);
252b5132
RH
7188 else
7189 {
7190 expr1.X_add_number = 8;
17a2f251
TS
7191 macro_build (NULL, &icnt, &expr1, "beq", "s,t,p", AT, 0);
7192 macro_build (NULL, &icnt, NULL, "nop", "", 0);
7193 macro_build (NULL, &icnt, NULL, "break", "c", 6);
252b5132
RH
7194 }
7195 --mips_opts.noreorder;
7196 break;
7197
771c7ce4 7198 case M_DROL:
fef14a42 7199 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097
CD
7200 {
7201 if (dreg == sreg)
7202 {
7203 tempreg = AT;
7204 used_at = 1;
7205 }
7206 else
7207 {
7208 tempreg = dreg;
7209 used_at = 0;
7210 }
17a2f251
TS
7211 macro_build (NULL, &icnt, NULL, "dnegu", "d,w", tempreg, treg);
7212 macro_build (NULL, &icnt, NULL, "drorv", "d,t,s", dreg, sreg,
7213 tempreg);
82dd0097
CD
7214 if (used_at)
7215 break;
7216 return;
7217 }
17a2f251
TS
7218 macro_build (NULL, &icnt, NULL, "dsubu", "d,v,t", AT, 0, treg);
7219 macro_build (NULL, &icnt, NULL, "dsrlv", "d,t,s", AT, sreg, AT);
7220 macro_build (NULL, &icnt, NULL, "dsllv", "d,t,s", dreg, sreg, treg);
7221 macro_build (NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
7222 break;
7223
252b5132 7224 case M_ROL:
fef14a42 7225 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097
CD
7226 {
7227 if (dreg == sreg)
7228 {
7229 tempreg = AT;
7230 used_at = 1;
7231 }
7232 else
7233 {
7234 tempreg = dreg;
7235 used_at = 0;
7236 }
17a2f251
TS
7237 macro_build (NULL, &icnt, NULL, "negu", "d,w", tempreg, treg);
7238 macro_build (NULL, &icnt, NULL, "rorv", "d,t,s", dreg, sreg,
7239 tempreg);
82dd0097
CD
7240 if (used_at)
7241 break;
7242 return;
7243 }
17a2f251
TS
7244 macro_build (NULL, &icnt, NULL, "subu", "d,v,t", AT, 0, treg);
7245 macro_build (NULL, &icnt, NULL, "srlv", "d,t,s", AT, sreg, AT);
7246 macro_build (NULL, &icnt, NULL, "sllv", "d,t,s", dreg, sreg, treg);
7247 macro_build (NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
7248 break;
7249
771c7ce4
TS
7250 case M_DROL_I:
7251 {
7252 unsigned int rot;
82dd0097 7253 char *l, *r;
771c7ce4
TS
7254
7255 if (imm_expr.X_op != O_constant)
82dd0097 7256 as_bad (_("Improper rotate count"));
771c7ce4 7257 rot = imm_expr.X_add_number & 0x3f;
fef14a42 7258 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
60b63b72
RS
7259 {
7260 rot = (64 - rot) & 0x3f;
7261 if (rot >= 32)
17a2f251
TS
7262 macro_build (NULL, &icnt, NULL, "dror32", "d,w,<",
7263 dreg, sreg, rot - 32);
60b63b72 7264 else
17a2f251
TS
7265 macro_build (NULL, &icnt, NULL, "dror", "d,w,<",
7266 dreg, sreg, rot);
82dd0097 7267 return;
60b63b72 7268 }
483fc7cd 7269 if (rot == 0)
483fc7cd 7270 {
17a2f251 7271 macro_build (NULL, &icnt, NULL, "dsrl", "d,w,<", dreg, sreg, 0);
82dd0097 7272 return;
483fc7cd 7273 }
82dd0097
CD
7274 l = (rot < 0x20) ? "dsll" : "dsll32";
7275 r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
7276 rot &= 0x1f;
17a2f251
TS
7277 macro_build (NULL, &icnt, NULL, l, "d,w,<", AT, sreg, rot);
7278 macro_build (NULL, &icnt, NULL, r, "d,w,<", dreg, sreg,
7279 (0x20 - rot) & 0x1f);
7280 macro_build (NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
7281 }
7282 break;
7283
252b5132 7284 case M_ROL_I:
771c7ce4
TS
7285 {
7286 unsigned int rot;
7287
7288 if (imm_expr.X_op != O_constant)
82dd0097 7289 as_bad (_("Improper rotate count"));
771c7ce4 7290 rot = imm_expr.X_add_number & 0x1f;
fef14a42 7291 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
60b63b72 7292 {
17a2f251
TS
7293 macro_build (NULL, &icnt, NULL, "ror", "d,w,<", dreg, sreg,
7294 (32 - rot) & 0x1f);
82dd0097 7295 return;
60b63b72 7296 }
483fc7cd 7297 if (rot == 0)
483fc7cd 7298 {
17a2f251 7299 macro_build (NULL, &icnt, NULL, "srl", "d,w,<", dreg, sreg, 0);
82dd0097 7300 return;
483fc7cd 7301 }
17a2f251
TS
7302 macro_build (NULL, &icnt, NULL, "sll", "d,w,<", AT, sreg, rot);
7303 macro_build (NULL, &icnt, NULL, "srl", "d,w,<", dreg, sreg,
7304 (0x20 - rot) & 0x1f);
7305 macro_build (NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
7306 }
7307 break;
7308
7309 case M_DROR:
fef14a42 7310 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097 7311 {
17a2f251 7312 macro_build (NULL, &icnt, NULL, "drorv", "d,t,s", dreg, sreg, treg);
82dd0097
CD
7313 return;
7314 }
17a2f251
TS
7315 macro_build (NULL, &icnt,NULL, "dsubu", "d,v,t", AT, 0, treg);
7316 macro_build (NULL, &icnt, NULL, "dsllv", "d,t,s", AT, sreg, AT);
7317 macro_build (NULL, &icnt, NULL, "dsrlv", "d,t,s", dreg, sreg, treg);
7318 macro_build (NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
7319 break;
7320
7321 case M_ROR:
fef14a42 7322 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 7323 {
17a2f251 7324 macro_build (NULL, &icnt, NULL, "rorv", "d,t,s", dreg, sreg, treg);
82dd0097
CD
7325 return;
7326 }
17a2f251
TS
7327 macro_build (NULL, &icnt, NULL, "subu", "d,v,t", AT, 0, treg);
7328 macro_build (NULL, &icnt, NULL, "sllv", "d,t,s", AT, sreg, AT);
7329 macro_build (NULL, &icnt, NULL, "srlv", "d,t,s", dreg, sreg, treg);
7330 macro_build (NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
7331 break;
7332
771c7ce4
TS
7333 case M_DROR_I:
7334 {
7335 unsigned int rot;
82dd0097 7336 char *l, *r;
771c7ce4
TS
7337
7338 if (imm_expr.X_op != O_constant)
82dd0097 7339 as_bad (_("Improper rotate count"));
771c7ce4 7340 rot = imm_expr.X_add_number & 0x3f;
fef14a42 7341 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097
CD
7342 {
7343 if (rot >= 32)
17a2f251
TS
7344 macro_build (NULL, &icnt, NULL, "dror32", "d,w,<",
7345 dreg, sreg, rot - 32);
82dd0097 7346 else
17a2f251
TS
7347 macro_build (NULL, &icnt, NULL, "dror", "d,w,<",
7348 dreg, sreg, rot);
82dd0097
CD
7349 return;
7350 }
483fc7cd 7351 if (rot == 0)
483fc7cd 7352 {
17a2f251 7353 macro_build (NULL, &icnt, NULL, "dsrl", "d,w,<", dreg, sreg, 0);
82dd0097 7354 return;
483fc7cd 7355 }
82dd0097
CD
7356 r = (rot < 0x20) ? "dsrl" : "dsrl32";
7357 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
7358 rot &= 0x1f;
17a2f251
TS
7359 macro_build ( NULL, &icnt,NULL, r, "d,w,<", AT, sreg, rot);
7360 macro_build (NULL, &icnt, NULL, l, "d,w,<", dreg, sreg,
7361 (0x20 - rot) & 0x1f);
7362 macro_build (NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
7363 }
7364 break;
7365
252b5132 7366 case M_ROR_I:
771c7ce4
TS
7367 {
7368 unsigned int rot;
7369
7370 if (imm_expr.X_op != O_constant)
82dd0097 7371 as_bad (_("Improper rotate count"));
771c7ce4 7372 rot = imm_expr.X_add_number & 0x1f;
fef14a42 7373 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 7374 {
17a2f251 7375 macro_build (NULL, &icnt, NULL, "ror", "d,w,<", dreg, sreg, rot);
82dd0097
CD
7376 return;
7377 }
483fc7cd 7378 if (rot == 0)
483fc7cd 7379 {
17a2f251 7380 macro_build (NULL, &icnt, NULL, "srl", "d,w,<", dreg, sreg, 0);
82dd0097 7381 return;
483fc7cd 7382 }
17a2f251
TS
7383 macro_build (NULL, &icnt, NULL, "srl", "d,w,<", AT, sreg, rot);
7384 macro_build (NULL, &icnt, NULL, "sll", "d,w,<", dreg, sreg,
7385 (0x20 - rot) & 0x1f);
7386 macro_build (NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4 7387 }
252b5132
RH
7388 break;
7389
7390 case M_S_DOB:
fef14a42 7391 if (mips_opts.arch == CPU_R4650)
252b5132
RH
7392 {
7393 as_bad (_("opcode not supported on this processor"));
7394 return;
7395 }
e7af610e 7396 assert (mips_opts.isa == ISA_MIPS1);
252b5132
RH
7397 /* Even on a big endian machine $fn comes before $fn+1. We have
7398 to adjust when storing to memory. */
17a2f251 7399 macro_build (NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
252b5132 7400 target_big_endian ? treg + 1 : treg,
17a2f251 7401 BFD_RELOC_LO16, breg);
252b5132 7402 offset_expr.X_add_number += 4;
17a2f251 7403 macro_build (NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
252b5132 7404 target_big_endian ? treg : treg + 1,
17a2f251 7405 BFD_RELOC_LO16, breg);
252b5132
RH
7406 return;
7407
7408 case M_SEQ:
7409 if (sreg == 0)
17a2f251
TS
7410 macro_build (NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, treg,
7411 BFD_RELOC_LO16);
252b5132 7412 else if (treg == 0)
17a2f251
TS
7413 macro_build (NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, sreg,
7414 BFD_RELOC_LO16);
252b5132
RH
7415 else
7416 {
17a2f251
TS
7417 macro_build (NULL, &icnt, NULL, "xor", "d,v,t", dreg, sreg, treg);
7418 macro_build (NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
7419 BFD_RELOC_LO16);
252b5132
RH
7420 }
7421 return;
7422
7423 case M_SEQ_I:
7424 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7425 {
17a2f251
TS
7426 macro_build (NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, sreg,
7427 BFD_RELOC_LO16);
252b5132
RH
7428 return;
7429 }
7430 if (sreg == 0)
7431 {
7432 as_warn (_("Instruction %s: result is always false"),
7433 ip->insn_mo->name);
ea1fb5dc 7434 move_register (&icnt, dreg, 0);
252b5132
RH
7435 return;
7436 }
7437 if (imm_expr.X_op == O_constant
7438 && imm_expr.X_add_number >= 0
7439 && imm_expr.X_add_number < 0x10000)
7440 {
17a2f251
TS
7441 macro_build (NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg, sreg,
7442 BFD_RELOC_LO16);
252b5132
RH
7443 used_at = 0;
7444 }
7445 else if (imm_expr.X_op == O_constant
7446 && imm_expr.X_add_number > -0x8000
7447 && imm_expr.X_add_number < 0)
7448 {
7449 imm_expr.X_add_number = -imm_expr.X_add_number;
17a2f251 7450 macro_build (NULL, &icnt, &imm_expr,
ca4e0257 7451 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
17a2f251 7452 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7453 used_at = 0;
7454 }
7455 else
7456 {
4d34fb5f 7457 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
17a2f251 7458 macro_build (NULL, &icnt, NULL, "xor", "d,v,t", dreg, sreg, AT);
252b5132
RH
7459 used_at = 1;
7460 }
17a2f251
TS
7461 macro_build (NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
7462 BFD_RELOC_LO16);
252b5132
RH
7463 if (used_at)
7464 break;
7465 return;
7466
7467 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
7468 s = "slt";
7469 goto sge;
7470 case M_SGEU:
7471 s = "sltu";
7472 sge:
17a2f251
TS
7473 macro_build (NULL, &icnt, NULL, s, "d,v,t", dreg, sreg, treg);
7474 macro_build (NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7475 BFD_RELOC_LO16);
252b5132
RH
7476 return;
7477
7478 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
7479 case M_SGEU_I:
7480 if (imm_expr.X_op == O_constant
7481 && imm_expr.X_add_number >= -0x8000
7482 && imm_expr.X_add_number < 0x8000)
7483 {
17a2f251 7484 macro_build (NULL, &icnt, &imm_expr,
252b5132 7485 mask == M_SGE_I ? "slti" : "sltiu",
17a2f251 7486 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7487 used_at = 0;
7488 }
7489 else
7490 {
4d34fb5f 7491 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
17a2f251
TS
7492 macro_build (NULL, &icnt, NULL, mask == M_SGE_I ? "slt" : "sltu",
7493 "d,v,t", dreg, sreg, AT);
252b5132
RH
7494 used_at = 1;
7495 }
17a2f251
TS
7496 macro_build (NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7497 BFD_RELOC_LO16);
252b5132
RH
7498 if (used_at)
7499 break;
7500 return;
7501
7502 case M_SGT: /* sreg > treg <==> treg < sreg */
7503 s = "slt";
7504 goto sgt;
7505 case M_SGTU:
7506 s = "sltu";
7507 sgt:
17a2f251 7508 macro_build (NULL, &icnt, NULL, s, "d,v,t", dreg, treg, sreg);
252b5132
RH
7509 return;
7510
7511 case M_SGT_I: /* sreg > I <==> I < sreg */
7512 s = "slt";
7513 goto sgti;
7514 case M_SGTU_I:
7515 s = "sltu";
7516 sgti:
4d34fb5f 7517 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
17a2f251 7518 macro_build (NULL, &icnt, NULL, s, "d,v,t", dreg, AT, sreg);
252b5132
RH
7519 break;
7520
2396cfb9 7521 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
252b5132
RH
7522 s = "slt";
7523 goto sle;
7524 case M_SLEU:
7525 s = "sltu";
7526 sle:
17a2f251
TS
7527 macro_build (NULL, &icnt, NULL, s, "d,v,t", dreg, treg, sreg);
7528 macro_build (NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7529 BFD_RELOC_LO16);
252b5132
RH
7530 return;
7531
2396cfb9 7532 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
252b5132
RH
7533 s = "slt";
7534 goto slei;
7535 case M_SLEU_I:
7536 s = "sltu";
7537 slei:
4d34fb5f 7538 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
17a2f251
TS
7539 macro_build (NULL, &icnt, NULL, s, "d,v,t", dreg, AT, sreg);
7540 macro_build (NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7541 BFD_RELOC_LO16);
252b5132
RH
7542 break;
7543
7544 case M_SLT_I:
7545 if (imm_expr.X_op == O_constant
7546 && imm_expr.X_add_number >= -0x8000
7547 && imm_expr.X_add_number < 0x8000)
7548 {
17a2f251
TS
7549 macro_build (NULL, &icnt, &imm_expr, "slti", "t,r,j", dreg, sreg,
7550 BFD_RELOC_LO16);
252b5132
RH
7551 return;
7552 }
4d34fb5f 7553 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
17a2f251 7554 macro_build (NULL, &icnt, NULL, "slt", "d,v,t", dreg, sreg, AT);
252b5132
RH
7555 break;
7556
7557 case M_SLTU_I:
7558 if (imm_expr.X_op == O_constant
7559 && imm_expr.X_add_number >= -0x8000
7560 && imm_expr.X_add_number < 0x8000)
7561 {
17a2f251
TS
7562 macro_build (NULL, &icnt, &imm_expr, "sltiu", "t,r,j", dreg, sreg,
7563 BFD_RELOC_LO16);
252b5132
RH
7564 return;
7565 }
4d34fb5f 7566 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
17a2f251 7567 macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", dreg, sreg, AT);
252b5132
RH
7568 break;
7569
7570 case M_SNE:
7571 if (sreg == 0)
17a2f251 7572 macro_build (NULL, &icnt,NULL, "sltu","d,v,t", dreg, 0, treg);
252b5132 7573 else if (treg == 0)
17a2f251 7574 macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0, sreg);
252b5132
RH
7575 else
7576 {
17a2f251
TS
7577 macro_build (NULL, &icnt, NULL, "xor", "d,v,t", dreg, sreg, treg);
7578 macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0, dreg);
252b5132
RH
7579 }
7580 return;
7581
7582 case M_SNE_I:
7583 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7584 {
17a2f251 7585 macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0, sreg);
252b5132
RH
7586 return;
7587 }
7588 if (sreg == 0)
7589 {
7590 as_warn (_("Instruction %s: result is always true"),
7591 ip->insn_mo->name);
17a2f251 7592 macro_build (NULL, &icnt, &expr1,
ca4e0257 7593 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
17a2f251 7594 "t,r,j", dreg, 0, BFD_RELOC_LO16);
252b5132
RH
7595 return;
7596 }
7597 if (imm_expr.X_op == O_constant
7598 && imm_expr.X_add_number >= 0
7599 && imm_expr.X_add_number < 0x10000)
7600 {
17a2f251
TS
7601 macro_build (NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg, sreg,
7602 BFD_RELOC_LO16);
252b5132
RH
7603 used_at = 0;
7604 }
7605 else if (imm_expr.X_op == O_constant
7606 && imm_expr.X_add_number > -0x8000
7607 && imm_expr.X_add_number < 0)
7608 {
7609 imm_expr.X_add_number = -imm_expr.X_add_number;
17a2f251 7610 macro_build (NULL, &icnt, &imm_expr,
ca4e0257 7611 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
17a2f251 7612 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7613 used_at = 0;
7614 }
7615 else
7616 {
4d34fb5f 7617 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
17a2f251 7618 macro_build (NULL, &icnt, NULL, "xor", "d,v,t", dreg, sreg, AT);
252b5132
RH
7619 used_at = 1;
7620 }
17a2f251 7621 macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0, dreg);
252b5132
RH
7622 if (used_at)
7623 break;
7624 return;
7625
7626 case M_DSUB_I:
7627 dbl = 1;
7628 case M_SUB_I:
7629 if (imm_expr.X_op == O_constant
7630 && imm_expr.X_add_number > -0x8000
7631 && imm_expr.X_add_number <= 0x8000)
7632 {
7633 imm_expr.X_add_number = -imm_expr.X_add_number;
17a2f251
TS
7634 macro_build (NULL, &icnt, &imm_expr, dbl ? "daddi" : "addi",
7635 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7636 return;
7637 }
7638 load_register (&icnt, AT, &imm_expr, dbl);
17a2f251
TS
7639 macro_build (NULL, &icnt, NULL, dbl ? "dsub" : "sub", "d,v,t",
7640 dreg, sreg, AT);
252b5132
RH
7641 break;
7642
7643 case M_DSUBU_I:
7644 dbl = 1;
7645 case M_SUBU_I:
7646 if (imm_expr.X_op == O_constant
7647 && imm_expr.X_add_number > -0x8000
7648 && imm_expr.X_add_number <= 0x8000)
7649 {
7650 imm_expr.X_add_number = -imm_expr.X_add_number;
17a2f251
TS
7651 macro_build (NULL, &icnt, &imm_expr, dbl ? "daddiu" : "addiu",
7652 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7653 return;
7654 }
7655 load_register (&icnt, AT, &imm_expr, dbl);
17a2f251
TS
7656 macro_build (NULL, &icnt, NULL, dbl ? "dsubu" : "subu", "d,v,t",
7657 dreg, sreg, AT);
252b5132
RH
7658 break;
7659
7660 case M_TEQ_I:
7661 s = "teq";
7662 goto trap;
7663 case M_TGE_I:
7664 s = "tge";
7665 goto trap;
7666 case M_TGEU_I:
7667 s = "tgeu";
7668 goto trap;
7669 case M_TLT_I:
7670 s = "tlt";
7671 goto trap;
7672 case M_TLTU_I:
7673 s = "tltu";
7674 goto trap;
7675 case M_TNE_I:
7676 s = "tne";
7677 trap:
4d34fb5f 7678 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
17a2f251 7679 macro_build (NULL, &icnt, NULL, s, "s,t", sreg, AT);
252b5132
RH
7680 break;
7681
252b5132 7682 case M_TRUNCWS:
43841e91 7683 case M_TRUNCWD:
e7af610e 7684 assert (mips_opts.isa == ISA_MIPS1);
252b5132
RH
7685 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
7686 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
7687
7688 /*
7689 * Is the double cfc1 instruction a bug in the mips assembler;
7690 * or is there a reason for it?
7691 */
b34976b6 7692 mips_emit_delays (TRUE);
252b5132
RH
7693 ++mips_opts.noreorder;
7694 mips_any_noreorder = 1;
17a2f251
TS
7695 macro_build (NULL, &icnt, NULL, "cfc1", "t,G", treg, RA);
7696 macro_build (NULL, &icnt, NULL, "cfc1", "t,G", treg, RA);
7697 macro_build (NULL, &icnt, NULL, "nop", "");
252b5132 7698 expr1.X_add_number = 3;
17a2f251
TS
7699 macro_build (NULL, &icnt, &expr1, "ori", "t,r,i", AT, treg,
7700 BFD_RELOC_LO16);
252b5132 7701 expr1.X_add_number = 2;
17a2f251
TS
7702 macro_build (NULL, &icnt, &expr1, "xori", "t,r,i", AT, AT,
7703 BFD_RELOC_LO16);
7704 macro_build (NULL, &icnt, NULL, "ctc1", "t,G", AT, RA);
7705 macro_build (NULL, &icnt, NULL, "nop", "");
7706 macro_build (NULL, &icnt, NULL,
7707 mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s",
7708 "D,S", dreg, sreg);
7709 macro_build (NULL, &icnt, NULL, "ctc1", "t,G", treg, RA);
7710 macro_build (NULL, &icnt, NULL, "nop", "");
252b5132
RH
7711 --mips_opts.noreorder;
7712 break;
7713
7714 case M_ULH:
7715 s = "lb";
7716 goto ulh;
7717 case M_ULHU:
7718 s = "lbu";
7719 ulh:
7720 if (offset_expr.X_add_number >= 0x7fff)
7721 as_bad (_("operand overflow"));
252b5132 7722 if (! target_big_endian)
f9419b05 7723 ++offset_expr.X_add_number;
17a2f251
TS
7724 macro_build (NULL, &icnt, &offset_expr, s, "t,o(b)", AT,
7725 BFD_RELOC_LO16, breg);
252b5132 7726 if (! target_big_endian)
f9419b05 7727 --offset_expr.X_add_number;
252b5132 7728 else
f9419b05 7729 ++offset_expr.X_add_number;
17a2f251
TS
7730 macro_build (NULL, &icnt, &offset_expr, "lbu", "t,o(b)", treg,
7731 BFD_RELOC_LO16, breg);
7732 macro_build (NULL, &icnt, NULL, "sll", "d,w,<", AT, AT, 8);
7733 macro_build (NULL, &icnt, NULL, "or", "d,v,t", treg, treg, AT);
252b5132
RH
7734 break;
7735
7736 case M_ULD:
7737 s = "ldl";
7738 s2 = "ldr";
7739 off = 7;
7740 goto ulw;
7741 case M_ULW:
7742 s = "lwl";
7743 s2 = "lwr";
7744 off = 3;
7745 ulw:
7746 if (offset_expr.X_add_number >= 0x8000 - off)
7747 as_bad (_("operand overflow"));
af22f5b2
CD
7748 if (treg != breg)
7749 tempreg = treg;
7750 else
7751 tempreg = AT;
252b5132
RH
7752 if (! target_big_endian)
7753 offset_expr.X_add_number += off;
17a2f251
TS
7754 macro_build (NULL, &icnt, &offset_expr, s, "t,o(b)", tempreg,
7755 BFD_RELOC_LO16, breg);
252b5132
RH
7756 if (! target_big_endian)
7757 offset_expr.X_add_number -= off;
7758 else
7759 offset_expr.X_add_number += off;
17a2f251
TS
7760 macro_build (NULL, &icnt, &offset_expr, s2, "t,o(b)", tempreg,
7761 BFD_RELOC_LO16, breg);
af22f5b2
CD
7762
7763 /* If necessary, move the result in tempreg the final destination. */
7764 if (treg == tempreg)
7765 return;
7766 /* Protect second load's delay slot. */
7767 if (!gpr_interlocks)
17a2f251 7768 macro_build (NULL, &icnt, NULL, "nop", "");
af22f5b2
CD
7769 move_register (&icnt, treg, tempreg);
7770 break;
252b5132
RH
7771
7772 case M_ULD_A:
7773 s = "ldl";
7774 s2 = "ldr";
7775 off = 7;
7776 goto ulwa;
7777 case M_ULW_A:
7778 s = "lwl";
7779 s2 = "lwr";
7780 off = 3;
7781 ulwa:
d6bc6245 7782 used_at = 1;
c9914766 7783 load_address (&icnt, AT, &offset_expr, &used_at);
252b5132 7784 if (breg != 0)
17a2f251
TS
7785 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
7786 AT, AT, breg);
252b5132
RH
7787 if (! target_big_endian)
7788 expr1.X_add_number = off;
7789 else
7790 expr1.X_add_number = 0;
17a2f251
TS
7791 macro_build (NULL, &icnt, &expr1, s, "t,o(b)", treg,
7792 BFD_RELOC_LO16, AT);
252b5132
RH
7793 if (! target_big_endian)
7794 expr1.X_add_number = 0;
7795 else
7796 expr1.X_add_number = off;
17a2f251
TS
7797 macro_build (NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7798 BFD_RELOC_LO16, AT);
252b5132
RH
7799 break;
7800
7801 case M_ULH_A:
7802 case M_ULHU_A:
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 = 0;
17a2f251
TS
7810 macro_build (NULL, &icnt, &expr1,
7811 mask == M_ULH_A ? "lb" : "lbu", "t,o(b)",
7812 treg, BFD_RELOC_LO16, AT);
252b5132
RH
7813 if (target_big_endian)
7814 expr1.X_add_number = 1;
7815 else
7816 expr1.X_add_number = 0;
17a2f251
TS
7817 macro_build (NULL, &icnt, &expr1, "lbu", "t,o(b)",
7818 AT, BFD_RELOC_LO16, AT);
7819 macro_build (NULL, &icnt, NULL, "sll", "d,w,<", treg, treg, 8);
7820 macro_build (NULL, &icnt, NULL, "or", "d,v,t", treg, treg, AT);
252b5132
RH
7821 break;
7822
7823 case M_USH:
7824 if (offset_expr.X_add_number >= 0x7fff)
7825 as_bad (_("operand overflow"));
7826 if (target_big_endian)
f9419b05 7827 ++offset_expr.X_add_number;
17a2f251
TS
7828 macro_build (NULL, &icnt, &offset_expr, "sb", "t,o(b)", treg,
7829 BFD_RELOC_LO16, breg);
7830 macro_build (NULL, &icnt, NULL, "srl", "d,w,<", AT, treg, 8);
252b5132 7831 if (target_big_endian)
f9419b05 7832 --offset_expr.X_add_number;
252b5132 7833 else
f9419b05 7834 ++offset_expr.X_add_number;
17a2f251
TS
7835 macro_build (NULL, &icnt, &offset_expr, "sb", "t,o(b)", AT,
7836 BFD_RELOC_LO16, breg);
252b5132
RH
7837 break;
7838
7839 case M_USD:
7840 s = "sdl";
7841 s2 = "sdr";
7842 off = 7;
7843 goto usw;
7844 case M_USW:
7845 s = "swl";
7846 s2 = "swr";
7847 off = 3;
7848 usw:
7849 if (offset_expr.X_add_number >= 0x8000 - off)
7850 as_bad (_("operand overflow"));
7851 if (! target_big_endian)
7852 offset_expr.X_add_number += off;
17a2f251
TS
7853 macro_build (NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7854 BFD_RELOC_LO16, breg);
252b5132
RH
7855 if (! target_big_endian)
7856 offset_expr.X_add_number -= off;
7857 else
7858 offset_expr.X_add_number += off;
17a2f251
TS
7859 macro_build (NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7860 BFD_RELOC_LO16, breg);
252b5132
RH
7861 return;
7862
7863 case M_USD_A:
7864 s = "sdl";
7865 s2 = "sdr";
7866 off = 7;
7867 goto uswa;
7868 case M_USW_A:
7869 s = "swl";
7870 s2 = "swr";
7871 off = 3;
7872 uswa:
d6bc6245 7873 used_at = 1;
c9914766 7874 load_address (&icnt, AT, &offset_expr, &used_at);
252b5132 7875 if (breg != 0)
17a2f251
TS
7876 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
7877 AT, AT, breg);
252b5132
RH
7878 if (! target_big_endian)
7879 expr1.X_add_number = off;
7880 else
7881 expr1.X_add_number = 0;
17a2f251
TS
7882 macro_build (NULL, &icnt, &expr1, s, "t,o(b)", treg,
7883 BFD_RELOC_LO16, AT);
252b5132
RH
7884 if (! target_big_endian)
7885 expr1.X_add_number = 0;
7886 else
7887 expr1.X_add_number = off;
17a2f251
TS
7888 macro_build (NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7889 BFD_RELOC_LO16, AT);
252b5132
RH
7890 break;
7891
7892 case M_USH_A:
d6bc6245 7893 used_at = 1;
c9914766 7894 load_address (&icnt, AT, &offset_expr, &used_at);
252b5132 7895 if (breg != 0)
17a2f251
TS
7896 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
7897 AT, AT, breg);
252b5132
RH
7898 if (! target_big_endian)
7899 expr1.X_add_number = 0;
17a2f251
TS
7900 macro_build (NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7901 BFD_RELOC_LO16, AT);
7902 macro_build (NULL, &icnt, NULL, "srl", "d,w,<", treg, treg, 8);
252b5132
RH
7903 if (! target_big_endian)
7904 expr1.X_add_number = 1;
7905 else
7906 expr1.X_add_number = 0;
17a2f251
TS
7907 macro_build (NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7908 BFD_RELOC_LO16, AT);
252b5132
RH
7909 if (! target_big_endian)
7910 expr1.X_add_number = 0;
7911 else
7912 expr1.X_add_number = 1;
17a2f251
TS
7913 macro_build (NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7914 BFD_RELOC_LO16, AT);
7915 macro_build (NULL, &icnt, NULL, "sll", "d,w,<", treg, treg, 8);
7916 macro_build (NULL, &icnt, NULL, "or", "d,v,t", treg, treg, AT);
252b5132
RH
7917 break;
7918
7919 default:
7920 /* FIXME: Check if this is one of the itbl macros, since they
bdaaa2e1 7921 are added dynamically. */
252b5132
RH
7922 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7923 break;
7924 }
7925 if (mips_opts.noat)
7926 as_warn (_("Macro used $at after \".set noat\""));
7927}
7928
7929/* Implement macros in mips16 mode. */
7930
7931static void
17a2f251 7932mips16_macro (struct mips_cl_insn *ip)
252b5132
RH
7933{
7934 int mask;
7935 int xreg, yreg, zreg, tmp;
7936 int icnt;
7937 expressionS expr1;
7938 int dbl;
7939 const char *s, *s2, *s3;
7940
7941 mask = ip->insn_mo->mask;
7942
7943 xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
7944 yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
7945 zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
7946
7947 icnt = 0;
7948
7949 expr1.X_op = O_constant;
7950 expr1.X_op_symbol = NULL;
7951 expr1.X_add_symbol = NULL;
7952 expr1.X_add_number = 1;
7953
7954 dbl = 0;
7955
7956 switch (mask)
7957 {
7958 default:
7959 internalError ();
7960
7961 case M_DDIV_3:
7962 dbl = 1;
7963 case M_DIV_3:
7964 s = "mflo";
7965 goto do_div3;
7966 case M_DREM_3:
7967 dbl = 1;
7968 case M_REM_3:
7969 s = "mfhi";
7970 do_div3:
b34976b6 7971 mips_emit_delays (TRUE);
252b5132
RH
7972 ++mips_opts.noreorder;
7973 mips_any_noreorder = 1;
17a2f251
TS
7974 macro_build (NULL, &icnt, NULL, dbl ? "ddiv" : "div", "0,x,y",
7975 xreg, yreg);
252b5132 7976 expr1.X_add_number = 2;
17a2f251
TS
7977 macro_build (NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7978 macro_build (NULL, &icnt, NULL, "break", "6", 7);
bdaaa2e1 7979
252b5132
RH
7980 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7981 since that causes an overflow. We should do that as well,
7982 but I don't see how to do the comparisons without a temporary
7983 register. */
7984 --mips_opts.noreorder;
17a2f251 7985 macro_build (NULL, &icnt, NULL, s, "x", zreg);
252b5132
RH
7986 break;
7987
7988 case M_DIVU_3:
7989 s = "divu";
7990 s2 = "mflo";
7991 goto do_divu3;
7992 case M_REMU_3:
7993 s = "divu";
7994 s2 = "mfhi";
7995 goto do_divu3;
7996 case M_DDIVU_3:
7997 s = "ddivu";
7998 s2 = "mflo";
7999 goto do_divu3;
8000 case M_DREMU_3:
8001 s = "ddivu";
8002 s2 = "mfhi";
8003 do_divu3:
b34976b6 8004 mips_emit_delays (TRUE);
252b5132
RH
8005 ++mips_opts.noreorder;
8006 mips_any_noreorder = 1;
17a2f251 8007 macro_build (NULL, &icnt, NULL, s, "0,x,y", xreg, yreg);
252b5132 8008 expr1.X_add_number = 2;
17a2f251
TS
8009 macro_build (NULL, &icnt, &expr1, "bnez", "x,p", yreg);
8010 macro_build (NULL, &icnt, NULL, "break", "6", 7);
252b5132 8011 --mips_opts.noreorder;
17a2f251 8012 macro_build (NULL, &icnt, NULL, s2, "x", zreg);
252b5132
RH
8013 break;
8014
8015 case M_DMUL:
8016 dbl = 1;
8017 case M_MUL:
17a2f251
TS
8018 macro_build (NULL, &icnt, NULL, dbl ? "dmultu" : "multu", "x,y",
8019 xreg, yreg);
8020 macro_build (NULL, &icnt, NULL, "mflo", "x", zreg);
252b5132
RH
8021 return;
8022
8023 case M_DSUBU_I:
8024 dbl = 1;
8025 goto do_subu;
8026 case M_SUBU_I:
8027 do_subu:
8028 if (imm_expr.X_op != O_constant)
8029 as_bad (_("Unsupported large constant"));
8030 imm_expr.X_add_number = -imm_expr.X_add_number;
17a2f251
TS
8031 macro_build (NULL, &icnt, &imm_expr, dbl ? "daddiu" : "addiu", "y,x,4",
8032 yreg, xreg);
252b5132
RH
8033 break;
8034
8035 case M_SUBU_I_2:
8036 if (imm_expr.X_op != O_constant)
8037 as_bad (_("Unsupported large constant"));
8038 imm_expr.X_add_number = -imm_expr.X_add_number;
17a2f251 8039 macro_build (NULL, &icnt, &imm_expr, "addiu", "x,k", xreg);
252b5132
RH
8040 break;
8041
8042 case M_DSUBU_I_2:
8043 if (imm_expr.X_op != O_constant)
8044 as_bad (_("Unsupported large constant"));
8045 imm_expr.X_add_number = -imm_expr.X_add_number;
17a2f251 8046 macro_build (NULL, &icnt, &imm_expr, "daddiu", "y,j", yreg);
252b5132
RH
8047 break;
8048
8049 case M_BEQ:
8050 s = "cmp";
8051 s2 = "bteqz";
8052 goto do_branch;
8053 case M_BNE:
8054 s = "cmp";
8055 s2 = "btnez";
8056 goto do_branch;
8057 case M_BLT:
8058 s = "slt";
8059 s2 = "btnez";
8060 goto do_branch;
8061 case M_BLTU:
8062 s = "sltu";
8063 s2 = "btnez";
8064 goto do_branch;
8065 case M_BLE:
8066 s = "slt";
8067 s2 = "bteqz";
8068 goto do_reverse_branch;
8069 case M_BLEU:
8070 s = "sltu";
8071 s2 = "bteqz";
8072 goto do_reverse_branch;
8073 case M_BGE:
8074 s = "slt";
8075 s2 = "bteqz";
8076 goto do_branch;
8077 case M_BGEU:
8078 s = "sltu";
8079 s2 = "bteqz";
8080 goto do_branch;
8081 case M_BGT:
8082 s = "slt";
8083 s2 = "btnez";
8084 goto do_reverse_branch;
8085 case M_BGTU:
8086 s = "sltu";
8087 s2 = "btnez";
8088
8089 do_reverse_branch:
8090 tmp = xreg;
8091 xreg = yreg;
8092 yreg = tmp;
8093
8094 do_branch:
17a2f251
TS
8095 macro_build (NULL, &icnt, NULL, s, "x,y", xreg, yreg);
8096 macro_build (NULL, &icnt, &offset_expr, s2, "p");
252b5132
RH
8097 break;
8098
8099 case M_BEQ_I:
8100 s = "cmpi";
8101 s2 = "bteqz";
8102 s3 = "x,U";
8103 goto do_branch_i;
8104 case M_BNE_I:
8105 s = "cmpi";
8106 s2 = "btnez";
8107 s3 = "x,U";
8108 goto do_branch_i;
8109 case M_BLT_I:
8110 s = "slti";
8111 s2 = "btnez";
8112 s3 = "x,8";
8113 goto do_branch_i;
8114 case M_BLTU_I:
8115 s = "sltiu";
8116 s2 = "btnez";
8117 s3 = "x,8";
8118 goto do_branch_i;
8119 case M_BLE_I:
8120 s = "slti";
8121 s2 = "btnez";
8122 s3 = "x,8";
8123 goto do_addone_branch_i;
8124 case M_BLEU_I:
8125 s = "sltiu";
8126 s2 = "btnez";
8127 s3 = "x,8";
8128 goto do_addone_branch_i;
8129 case M_BGE_I:
8130 s = "slti";
8131 s2 = "bteqz";
8132 s3 = "x,8";
8133 goto do_branch_i;
8134 case M_BGEU_I:
8135 s = "sltiu";
8136 s2 = "bteqz";
8137 s3 = "x,8";
8138 goto do_branch_i;
8139 case M_BGT_I:
8140 s = "slti";
8141 s2 = "bteqz";
8142 s3 = "x,8";
8143 goto do_addone_branch_i;
8144 case M_BGTU_I:
8145 s = "sltiu";
8146 s2 = "bteqz";
8147 s3 = "x,8";
8148
8149 do_addone_branch_i:
8150 if (imm_expr.X_op != O_constant)
8151 as_bad (_("Unsupported large constant"));
8152 ++imm_expr.X_add_number;
8153
8154 do_branch_i:
17a2f251
TS
8155 macro_build (NULL, &icnt, &imm_expr, s, s3, xreg);
8156 macro_build (NULL, &icnt, &offset_expr, s2, "p");
252b5132
RH
8157 break;
8158
8159 case M_ABS:
8160 expr1.X_add_number = 0;
17a2f251 8161 macro_build (NULL, &icnt, &expr1, "slti", "x,8", yreg);
252b5132 8162 if (xreg != yreg)
ea1fb5dc 8163 move_register (&icnt, xreg, yreg);
252b5132 8164 expr1.X_add_number = 2;
17a2f251
TS
8165 macro_build (NULL, &icnt, &expr1, "bteqz", "p");
8166 macro_build (NULL, &icnt, NULL, "neg", "x,w", xreg, xreg);
252b5132
RH
8167 }
8168}
8169
8170/* For consistency checking, verify that all bits are specified either
8171 by the match/mask part of the instruction definition, or by the
8172 operand list. */
8173static int
17a2f251 8174validate_mips_insn (const struct mips_opcode *opc)
252b5132
RH
8175{
8176 const char *p = opc->args;
8177 char c;
8178 unsigned long used_bits = opc->mask;
8179
8180 if ((used_bits & opc->match) != opc->match)
8181 {
8182 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
8183 opc->name, opc->args);
8184 return 0;
8185 }
8186#define USE_BITS(mask,shift) (used_bits |= ((mask) << (shift)))
8187 while (*p)
8188 switch (c = *p++)
8189 {
8190 case ',': break;
8191 case '(': break;
8192 case ')': break;
af7ee8bf
CD
8193 case '+':
8194 switch (c = *p++)
8195 {
8196 case 'A': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
8197 case 'B': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
8198 case 'C': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
bbcc0807
CD
8199 case 'D': USE_BITS (OP_MASK_RD, OP_SH_RD);
8200 USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
5f74bc13
CD
8201 case 'E': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
8202 case 'F': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
8203 case 'G': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
8204 case 'H': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
8205 case 'I': break;
af7ee8bf
CD
8206 default:
8207 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8208 c, opc->name, opc->args);
8209 return 0;
8210 }
8211 break;
252b5132
RH
8212 case '<': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
8213 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
8214 case 'A': break;
4372b673 8215 case 'B': USE_BITS (OP_MASK_CODE20, OP_SH_CODE20); break;
252b5132
RH
8216 case 'C': USE_BITS (OP_MASK_COPZ, OP_SH_COPZ); break;
8217 case 'D': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
8218 case 'E': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8219 case 'F': break;
8220 case 'G': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
156c2f8b 8221 case 'H': USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
252b5132 8222 case 'I': break;
e972090a 8223 case 'J': USE_BITS (OP_MASK_CODE19, OP_SH_CODE19); break;
af7ee8bf 8224 case 'K': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
252b5132
RH
8225 case 'L': break;
8226 case 'M': USE_BITS (OP_MASK_CCC, OP_SH_CCC); break;
8227 case 'N': USE_BITS (OP_MASK_BCC, OP_SH_BCC); break;
deec1734
CD
8228 case 'O': USE_BITS (OP_MASK_ALN, OP_SH_ALN); break;
8229 case 'Q': USE_BITS (OP_MASK_VSEL, OP_SH_VSEL);
8230 USE_BITS (OP_MASK_FT, OP_SH_FT); break;
252b5132
RH
8231 case 'R': USE_BITS (OP_MASK_FR, OP_SH_FR); break;
8232 case 'S': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
8233 case 'T': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
8234 case 'V': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
8235 case 'W': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
deec1734
CD
8236 case 'X': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
8237 case 'Y': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
8238 case 'Z': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
252b5132
RH
8239 case 'a': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break;
8240 case 'b': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8241 case 'c': USE_BITS (OP_MASK_CODE, OP_SH_CODE); break;
8242 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
8243 case 'f': break;
8244 case 'h': USE_BITS (OP_MASK_PREFX, OP_SH_PREFX); break;
8245 case 'i': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
8246 case 'j': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
8247 case 'k': USE_BITS (OP_MASK_CACHE, OP_SH_CACHE); break;
8248 case 'l': break;
8249 case 'o': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
8250 case 'p': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
8251 case 'q': USE_BITS (OP_MASK_CODE2, OP_SH_CODE2); break;
8252 case 'r': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8253 case 's': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8254 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8255 case 'u': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
8256 case 'v': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8257 case 'w': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8258 case 'x': break;
8259 case 'z': break;
8260 case 'P': USE_BITS (OP_MASK_PERFREG, OP_SH_PERFREG); break;
4372b673
NC
8261 case 'U': USE_BITS (OP_MASK_RD, OP_SH_RD);
8262 USE_BITS (OP_MASK_RT, OP_SH_RT); break;
60b63b72
RS
8263 case 'e': USE_BITS (OP_MASK_VECBYTE, OP_SH_VECBYTE); break;
8264 case '%': USE_BITS (OP_MASK_VECALIGN, OP_SH_VECALIGN); break;
8265 case '[': break;
8266 case ']': break;
252b5132
RH
8267 default:
8268 as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
8269 c, opc->name, opc->args);
8270 return 0;
8271 }
8272#undef USE_BITS
8273 if (used_bits != 0xffffffff)
8274 {
8275 as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
8276 ~used_bits & 0xffffffff, opc->name, opc->args);
8277 return 0;
8278 }
8279 return 1;
8280}
8281
8282/* This routine assembles an instruction into its binary format. As a
8283 side effect, it sets one of the global variables imm_reloc or
8284 offset_reloc to the type of relocation to do if one of the operands
8285 is an address expression. */
8286
8287static void
17a2f251 8288mips_ip (char *str, struct mips_cl_insn *ip)
252b5132
RH
8289{
8290 char *s;
8291 const char *args;
43841e91 8292 char c = 0;
252b5132
RH
8293 struct mips_opcode *insn;
8294 char *argsStart;
8295 unsigned int regno;
8296 unsigned int lastregno = 0;
af7ee8bf 8297 unsigned int lastpos = 0;
071742cf 8298 unsigned int limlo, limhi;
252b5132
RH
8299 char *s_reset;
8300 char save_c = 0;
252b5132
RH
8301
8302 insn_error = NULL;
8303
8304 /* If the instruction contains a '.', we first try to match an instruction
8305 including the '.'. Then we try again without the '.'. */
8306 insn = NULL;
3882b010 8307 for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
252b5132
RH
8308 continue;
8309
8310 /* If we stopped on whitespace, then replace the whitespace with null for
8311 the call to hash_find. Save the character we replaced just in case we
8312 have to re-parse the instruction. */
3882b010 8313 if (ISSPACE (*s))
252b5132
RH
8314 {
8315 save_c = *s;
8316 *s++ = '\0';
8317 }
bdaaa2e1 8318
252b5132
RH
8319 insn = (struct mips_opcode *) hash_find (op_hash, str);
8320
8321 /* If we didn't find the instruction in the opcode table, try again, but
8322 this time with just the instruction up to, but not including the
8323 first '.'. */
8324 if (insn == NULL)
8325 {
bdaaa2e1 8326 /* Restore the character we overwrite above (if any). */
252b5132
RH
8327 if (save_c)
8328 *(--s) = save_c;
8329
8330 /* Scan up to the first '.' or whitespace. */
3882b010
L
8331 for (s = str;
8332 *s != '\0' && *s != '.' && !ISSPACE (*s);
8333 ++s)
252b5132
RH
8334 continue;
8335
8336 /* If we did not find a '.', then we can quit now. */
8337 if (*s != '.')
8338 {
8339 insn_error = "unrecognized opcode";
8340 return;
8341 }
8342
8343 /* Lookup the instruction in the hash table. */
8344 *s++ = '\0';
8345 if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
8346 {
8347 insn_error = "unrecognized opcode";
8348 return;
8349 }
252b5132
RH
8350 }
8351
8352 argsStart = s;
8353 for (;;)
8354 {
b34976b6 8355 bfd_boolean ok;
252b5132
RH
8356
8357 assert (strcmp (insn->name, str) == 0);
8358
1f25f5d3
CD
8359 if (OPCODE_IS_MEMBER (insn,
8360 (mips_opts.isa
3396de36 8361 | (file_ase_mips16 ? INSN_MIPS16 : 0)
deec1734 8362 | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
98d3f06f 8363 | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
fef14a42 8364 mips_opts.arch))
b34976b6 8365 ok = TRUE;
bdaaa2e1 8366 else
b34976b6 8367 ok = FALSE;
bdaaa2e1 8368
252b5132
RH
8369 if (insn->pinfo != INSN_MACRO)
8370 {
fef14a42 8371 if (mips_opts.arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
b34976b6 8372 ok = FALSE;
252b5132
RH
8373 }
8374
8375 if (! ok)
8376 {
8377 if (insn + 1 < &mips_opcodes[NUMOPCODES]
8378 && strcmp (insn->name, insn[1].name) == 0)
8379 {
8380 ++insn;
8381 continue;
8382 }
252b5132 8383 else
beae10d5 8384 {
268f6bed
L
8385 if (!insn_error)
8386 {
8387 static char buf[100];
fef14a42
TS
8388 sprintf (buf,
8389 _("opcode not supported on this processor: %s (%s)"),
8390 mips_cpu_info_from_arch (mips_opts.arch)->name,
8391 mips_cpu_info_from_isa (mips_opts.isa)->name);
268f6bed
L
8392 insn_error = buf;
8393 }
8394 if (save_c)
8395 *(--s) = save_c;
2bd7f1f3 8396 return;
252b5132 8397 }
252b5132
RH
8398 }
8399
8400 ip->insn_mo = insn;
8401 ip->insn_opcode = insn->match;
268f6bed 8402 insn_error = NULL;
252b5132
RH
8403 for (args = insn->args;; ++args)
8404 {
deec1734
CD
8405 int is_mdmx;
8406
ad8d3bb3 8407 s += strspn (s, " \t");
deec1734 8408 is_mdmx = 0;
252b5132
RH
8409 switch (*args)
8410 {
8411 case '\0': /* end of args */
8412 if (*s == '\0')
8413 return;
8414 break;
8415
8416 case ',':
8417 if (*s++ == *args)
8418 continue;
8419 s--;
8420 switch (*++args)
8421 {
8422 case 'r':
8423 case 'v':
38487616 8424 ip->insn_opcode |= lastregno << OP_SH_RS;
252b5132
RH
8425 continue;
8426
8427 case 'w':
38487616
TS
8428 ip->insn_opcode |= lastregno << OP_SH_RT;
8429 continue;
8430
252b5132 8431 case 'W':
38487616 8432 ip->insn_opcode |= lastregno << OP_SH_FT;
252b5132
RH
8433 continue;
8434
8435 case 'V':
38487616 8436 ip->insn_opcode |= lastregno << OP_SH_FS;
252b5132
RH
8437 continue;
8438 }
8439 break;
8440
8441 case '(':
8442 /* Handle optional base register.
8443 Either the base register is omitted or
bdaaa2e1 8444 we must have a left paren. */
252b5132
RH
8445 /* This is dependent on the next operand specifier
8446 is a base register specification. */
8447 assert (args[1] == 'b' || args[1] == '5'
8448 || args[1] == '-' || args[1] == '4');
8449 if (*s == '\0')
8450 return;
8451
8452 case ')': /* these must match exactly */
60b63b72
RS
8453 case '[':
8454 case ']':
252b5132
RH
8455 if (*s++ == *args)
8456 continue;
8457 break;
8458
af7ee8bf
CD
8459 case '+': /* Opcode extension character. */
8460 switch (*++args)
8461 {
071742cf
CD
8462 case 'A': /* ins/ext position, becomes LSB. */
8463 limlo = 0;
8464 limhi = 31;
5f74bc13
CD
8465 goto do_lsb;
8466 case 'E':
8467 limlo = 32;
8468 limhi = 63;
8469 goto do_lsb;
8470do_lsb:
071742cf
CD
8471 my_getExpression (&imm_expr, s);
8472 check_absolute_expr (ip, &imm_expr);
8473 if ((unsigned long) imm_expr.X_add_number < limlo
8474 || (unsigned long) imm_expr.X_add_number > limhi)
8475 {
8476 as_bad (_("Improper position (%lu)"),
8477 (unsigned long) imm_expr.X_add_number);
8478 imm_expr.X_add_number = limlo;
8479 }
8480 lastpos = imm_expr.X_add_number;
8481 ip->insn_opcode |= (imm_expr.X_add_number
8482 & OP_MASK_SHAMT) << OP_SH_SHAMT;
8483 imm_expr.X_op = O_absent;
8484 s = expr_end;
8485 continue;
8486
8487 case 'B': /* ins size, becomes MSB. */
8488 limlo = 1;
8489 limhi = 32;
5f74bc13
CD
8490 goto do_msb;
8491 case 'F':
8492 limlo = 33;
8493 limhi = 64;
8494 goto do_msb;
8495do_msb:
071742cf
CD
8496 my_getExpression (&imm_expr, s);
8497 check_absolute_expr (ip, &imm_expr);
8498 /* Check for negative input so that small negative numbers
8499 will not succeed incorrectly. The checks against
8500 (pos+size) transitively check "size" itself,
8501 assuming that "pos" is reasonable. */
8502 if ((long) imm_expr.X_add_number < 0
8503 || ((unsigned long) imm_expr.X_add_number
8504 + lastpos) < limlo
8505 || ((unsigned long) imm_expr.X_add_number
8506 + lastpos) > limhi)
8507 {
8508 as_bad (_("Improper insert size (%lu, position %lu)"),
8509 (unsigned long) imm_expr.X_add_number,
8510 (unsigned long) lastpos);
8511 imm_expr.X_add_number = limlo - lastpos;
8512 }
8513 ip->insn_opcode |= ((lastpos + imm_expr.X_add_number - 1)
8514 & OP_MASK_INSMSB) << OP_SH_INSMSB;
8515 imm_expr.X_op = O_absent;
8516 s = expr_end;
8517 continue;
8518
8519 case 'C': /* ext size, becomes MSBD. */
8520 limlo = 1;
8521 limhi = 32;
5f74bc13
CD
8522 goto do_msbd;
8523 case 'G':
8524 limlo = 33;
8525 limhi = 64;
8526 goto do_msbd;
8527 case 'H':
8528 limlo = 33;
8529 limhi = 64;
8530 goto do_msbd;
8531do_msbd:
071742cf
CD
8532 my_getExpression (&imm_expr, s);
8533 check_absolute_expr (ip, &imm_expr);
8534 /* Check for negative input so that small negative numbers
8535 will not succeed incorrectly. The checks against
8536 (pos+size) transitively check "size" itself,
8537 assuming that "pos" is reasonable. */
8538 if ((long) imm_expr.X_add_number < 0
8539 || ((unsigned long) imm_expr.X_add_number
8540 + lastpos) < limlo
8541 || ((unsigned long) imm_expr.X_add_number
8542 + lastpos) > limhi)
8543 {
8544 as_bad (_("Improper extract size (%lu, position %lu)"),
8545 (unsigned long) imm_expr.X_add_number,
8546 (unsigned long) lastpos);
8547 imm_expr.X_add_number = limlo - lastpos;
8548 }
8549 ip->insn_opcode |= ((imm_expr.X_add_number - 1)
8550 & OP_MASK_EXTMSBD) << OP_SH_EXTMSBD;
8551 imm_expr.X_op = O_absent;
8552 s = expr_end;
8553 continue;
af7ee8bf 8554
bbcc0807
CD
8555 case 'D':
8556 /* +D is for disassembly only; never match. */
8557 break;
8558
5f74bc13
CD
8559 case 'I':
8560 /* "+I" is like "I", except that imm2_expr is used. */
8561 my_getExpression (&imm2_expr, s);
8562 if (imm2_expr.X_op != O_big
8563 && imm2_expr.X_op != O_constant)
8564 insn_error = _("absolute expression required");
8565 s = expr_end;
8566 continue;
8567
af7ee8bf
CD
8568 default:
8569 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8570 *args, insn->name, insn->args);
8571 /* Further processing is fruitless. */
8572 return;
8573 }
8574 break;
8575
252b5132
RH
8576 case '<': /* must be at least one digit */
8577 /*
8578 * According to the manual, if the shift amount is greater
b6ff326e
KH
8579 * than 31 or less than 0, then the shift amount should be
8580 * mod 32. In reality the mips assembler issues an error.
252b5132
RH
8581 * We issue a warning and mask out all but the low 5 bits.
8582 */
8583 my_getExpression (&imm_expr, s);
8584 check_absolute_expr (ip, &imm_expr);
8585 if ((unsigned long) imm_expr.X_add_number > 31)
8586 {
793b27f4
TS
8587 as_warn (_("Improper shift amount (%lu)"),
8588 (unsigned long) imm_expr.X_add_number);
38487616 8589 imm_expr.X_add_number &= OP_MASK_SHAMT;
252b5132 8590 }
38487616 8591 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SHAMT;
252b5132
RH
8592 imm_expr.X_op = O_absent;
8593 s = expr_end;
8594 continue;
8595
8596 case '>': /* shift amount minus 32 */
8597 my_getExpression (&imm_expr, s);
8598 check_absolute_expr (ip, &imm_expr);
8599 if ((unsigned long) imm_expr.X_add_number < 32
8600 || (unsigned long) imm_expr.X_add_number > 63)
8601 break;
38487616 8602 ip->insn_opcode |= (imm_expr.X_add_number - 32) << OP_SH_SHAMT;
252b5132
RH
8603 imm_expr.X_op = O_absent;
8604 s = expr_end;
8605 continue;
8606
252b5132
RH
8607 case 'k': /* cache code */
8608 case 'h': /* prefx code */
8609 my_getExpression (&imm_expr, s);
8610 check_absolute_expr (ip, &imm_expr);
8611 if ((unsigned long) imm_expr.X_add_number > 31)
8612 {
8613 as_warn (_("Invalid value for `%s' (%lu)"),
8614 ip->insn_mo->name,
8615 (unsigned long) imm_expr.X_add_number);
8616 imm_expr.X_add_number &= 0x1f;
8617 }
8618 if (*args == 'k')
8619 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
8620 else
8621 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
8622 imm_expr.X_op = O_absent;
8623 s = expr_end;
8624 continue;
8625
8626 case 'c': /* break code */
8627 my_getExpression (&imm_expr, s);
8628 check_absolute_expr (ip, &imm_expr);
793b27f4 8629 if ((unsigned long) imm_expr.X_add_number > 1023)
252b5132 8630 {
793b27f4
TS
8631 as_warn (_("Illegal break code (%lu)"),
8632 (unsigned long) imm_expr.X_add_number);
38487616 8633 imm_expr.X_add_number &= OP_MASK_CODE;
252b5132 8634 }
38487616 8635 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE;
252b5132
RH
8636 imm_expr.X_op = O_absent;
8637 s = expr_end;
8638 continue;
8639
8640 case 'q': /* lower break code */
8641 my_getExpression (&imm_expr, s);
8642 check_absolute_expr (ip, &imm_expr);
793b27f4 8643 if ((unsigned long) imm_expr.X_add_number > 1023)
252b5132 8644 {
793b27f4
TS
8645 as_warn (_("Illegal lower break code (%lu)"),
8646 (unsigned long) imm_expr.X_add_number);
38487616 8647 imm_expr.X_add_number &= OP_MASK_CODE2;
252b5132 8648 }
38487616 8649 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE2;
252b5132
RH
8650 imm_expr.X_op = O_absent;
8651 s = expr_end;
8652 continue;
8653
4372b673 8654 case 'B': /* 20-bit syscall/break code. */
156c2f8b 8655 my_getExpression (&imm_expr, s);
156c2f8b 8656 check_absolute_expr (ip, &imm_expr);
793b27f4
TS
8657 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
8658 as_warn (_("Illegal 20-bit code (%lu)"),
8659 (unsigned long) imm_expr.X_add_number);
38487616 8660 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE20;
252b5132
RH
8661 imm_expr.X_op = O_absent;
8662 s = expr_end;
8663 continue;
8664
98d3f06f 8665 case 'C': /* Coprocessor code */
beae10d5 8666 my_getExpression (&imm_expr, s);
252b5132 8667 check_absolute_expr (ip, &imm_expr);
98d3f06f 8668 if ((unsigned long) imm_expr.X_add_number >= (1 << 25))
252b5132 8669 {
793b27f4
TS
8670 as_warn (_("Coproccesor code > 25 bits (%lu)"),
8671 (unsigned long) imm_expr.X_add_number);
98d3f06f 8672 imm_expr.X_add_number &= ((1 << 25) - 1);
252b5132 8673 }
beae10d5
KH
8674 ip->insn_opcode |= imm_expr.X_add_number;
8675 imm_expr.X_op = O_absent;
8676 s = expr_end;
8677 continue;
252b5132 8678
4372b673
NC
8679 case 'J': /* 19-bit wait code. */
8680 my_getExpression (&imm_expr, s);
8681 check_absolute_expr (ip, &imm_expr);
793b27f4
TS
8682 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
8683 as_warn (_("Illegal 19-bit code (%lu)"),
8684 (unsigned long) imm_expr.X_add_number);
38487616 8685 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE19;
4372b673
NC
8686 imm_expr.X_op = O_absent;
8687 s = expr_end;
8688 continue;
8689
252b5132 8690 case 'P': /* Performance register */
beae10d5 8691 my_getExpression (&imm_expr, s);
252b5132 8692 check_absolute_expr (ip, &imm_expr);
beae10d5 8693 if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
252b5132 8694 {
793b27f4
TS
8695 as_warn (_("Invalid performance register (%lu)"),
8696 (unsigned long) imm_expr.X_add_number);
38487616 8697 imm_expr.X_add_number &= OP_MASK_PERFREG;
252b5132 8698 }
38487616 8699 ip->insn_opcode |= (imm_expr.X_add_number << OP_SH_PERFREG);
beae10d5
KH
8700 imm_expr.X_op = O_absent;
8701 s = expr_end;
8702 continue;
252b5132
RH
8703
8704 case 'b': /* base register */
8705 case 'd': /* destination register */
8706 case 's': /* source register */
8707 case 't': /* target register */
8708 case 'r': /* both target and source */
8709 case 'v': /* both dest and source */
8710 case 'w': /* both dest and target */
8711 case 'E': /* coprocessor target register */
8712 case 'G': /* coprocessor destination register */
af7ee8bf 8713 case 'K': /* 'rdhwr' destination register */
252b5132
RH
8714 case 'x': /* ignore register name */
8715 case 'z': /* must be zero register */
4372b673 8716 case 'U': /* destination register (clo/clz). */
252b5132
RH
8717 s_reset = s;
8718 if (s[0] == '$')
8719 {
8720
3882b010 8721 if (ISDIGIT (s[1]))
252b5132
RH
8722 {
8723 ++s;
8724 regno = 0;
8725 do
8726 {
8727 regno *= 10;
8728 regno += *s - '0';
8729 ++s;
8730 }
3882b010 8731 while (ISDIGIT (*s));
252b5132
RH
8732 if (regno > 31)
8733 as_bad (_("Invalid register number (%d)"), regno);
8734 }
af7ee8bf 8735 else if (*args == 'E' || *args == 'G' || *args == 'K')
252b5132
RH
8736 goto notreg;
8737 else
8738 {
76db943d
TS
8739 if (s[1] == 'r' && s[2] == 'a')
8740 {
8741 s += 3;
8742 regno = RA;
8743 }
8744 else if (s[1] == 'f' && s[2] == 'p')
252b5132
RH
8745 {
8746 s += 3;
8747 regno = FP;
8748 }
8749 else if (s[1] == 's' && s[2] == 'p')
8750 {
8751 s += 3;
8752 regno = SP;
8753 }
8754 else if (s[1] == 'g' && s[2] == 'p')
8755 {
8756 s += 3;
8757 regno = GP;
8758 }
8759 else if (s[1] == 'a' && s[2] == 't')
8760 {
8761 s += 3;
8762 regno = AT;
8763 }
8764 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8765 {
8766 s += 4;
8767 regno = KT0;
8768 }
8769 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8770 {
8771 s += 4;
8772 regno = KT1;
8773 }
85b51719
TS
8774 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
8775 {
8776 s += 5;
8777 regno = ZERO;
8778 }
252b5132
RH
8779 else if (itbl_have_entries)
8780 {
8781 char *p, *n;
d7ba4a77 8782 unsigned long r;
252b5132 8783
d7ba4a77 8784 p = s + 1; /* advance past '$' */
252b5132
RH
8785 n = itbl_get_field (&p); /* n is name */
8786
d7ba4a77
ILT
8787 /* See if this is a register defined in an
8788 itbl entry. */
8789 if (itbl_get_reg_val (n, &r))
252b5132
RH
8790 {
8791 /* Get_field advances to the start of
8792 the next field, so we need to back
d7ba4a77 8793 rack to the end of the last field. */
bdaaa2e1 8794 if (p)
252b5132 8795 s = p - 1;
bdaaa2e1 8796 else
d7ba4a77 8797 s = strchr (s, '\0');
252b5132
RH
8798 regno = r;
8799 }
8800 else
8801 goto notreg;
beae10d5 8802 }
252b5132
RH
8803 else
8804 goto notreg;
8805 }
8806 if (regno == AT
8807 && ! mips_opts.noat
8808 && *args != 'E'
af7ee8bf
CD
8809 && *args != 'G'
8810 && *args != 'K')
252b5132
RH
8811 as_warn (_("Used $at without \".set noat\""));
8812 c = *args;
8813 if (*s == ' ')
f9419b05 8814 ++s;
252b5132
RH
8815 if (args[1] != *s)
8816 {
8817 if (c == 'r' || c == 'v' || c == 'w')
8818 {
8819 regno = lastregno;
8820 s = s_reset;
f9419b05 8821 ++args;
252b5132
RH
8822 }
8823 }
8824 /* 'z' only matches $0. */
8825 if (c == 'z' && regno != 0)
8826 break;
8827
bdaaa2e1
KH
8828 /* Now that we have assembled one operand, we use the args string
8829 * to figure out where it goes in the instruction. */
252b5132
RH
8830 switch (c)
8831 {
8832 case 'r':
8833 case 's':
8834 case 'v':
8835 case 'b':
38487616 8836 ip->insn_opcode |= regno << OP_SH_RS;
252b5132
RH
8837 break;
8838 case 'd':
8839 case 'G':
af7ee8bf 8840 case 'K':
38487616 8841 ip->insn_opcode |= regno << OP_SH_RD;
252b5132 8842 break;
4372b673 8843 case 'U':
38487616
TS
8844 ip->insn_opcode |= regno << OP_SH_RD;
8845 ip->insn_opcode |= regno << OP_SH_RT;
4372b673 8846 break;
252b5132
RH
8847 case 'w':
8848 case 't':
8849 case 'E':
38487616 8850 ip->insn_opcode |= regno << OP_SH_RT;
252b5132
RH
8851 break;
8852 case 'x':
8853 /* This case exists because on the r3000 trunc
8854 expands into a macro which requires a gp
8855 register. On the r6000 or r4000 it is
8856 assembled into a single instruction which
8857 ignores the register. Thus the insn version
8858 is MIPS_ISA2 and uses 'x', and the macro
8859 version is MIPS_ISA1 and uses 't'. */
8860 break;
8861 case 'z':
8862 /* This case is for the div instruction, which
8863 acts differently if the destination argument
8864 is $0. This only matches $0, and is checked
8865 outside the switch. */
8866 break;
8867 case 'D':
8868 /* Itbl operand; not yet implemented. FIXME ?? */
8869 break;
8870 /* What about all other operands like 'i', which
8871 can be specified in the opcode table? */
8872 }
8873 lastregno = regno;
8874 continue;
8875 }
8876 notreg:
8877 switch (*args++)
8878 {
8879 case 'r':
8880 case 'v':
38487616 8881 ip->insn_opcode |= lastregno << OP_SH_RS;
252b5132
RH
8882 continue;
8883 case 'w':
38487616 8884 ip->insn_opcode |= lastregno << OP_SH_RT;
252b5132
RH
8885 continue;
8886 }
8887 break;
8888
deec1734
CD
8889 case 'O': /* MDMX alignment immediate constant. */
8890 my_getExpression (&imm_expr, s);
8891 check_absolute_expr (ip, &imm_expr);
8892 if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
8893 {
8894 as_warn ("Improper align amount (%ld), using low bits",
8895 (long) imm_expr.X_add_number);
8896 imm_expr.X_add_number &= OP_MASK_ALN;
8897 }
8898 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_ALN;
8899 imm_expr.X_op = O_absent;
8900 s = expr_end;
8901 continue;
8902
8903 case 'Q': /* MDMX vector, element sel, or const. */
8904 if (s[0] != '$')
8905 {
8906 /* MDMX Immediate. */
8907 my_getExpression (&imm_expr, s);
8908 check_absolute_expr (ip, &imm_expr);
8909 if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
8910 {
8911 as_warn (_("Invalid MDMX Immediate (%ld)"),
8912 (long) imm_expr.X_add_number);
8913 imm_expr.X_add_number &= OP_MASK_FT;
8914 }
8915 imm_expr.X_add_number &= OP_MASK_FT;
8916 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8917 ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
8918 else
8919 ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
8920 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_FT;
8921 imm_expr.X_op = O_absent;
8922 s = expr_end;
8923 continue;
8924 }
8925 /* Not MDMX Immediate. Fall through. */
8926 case 'X': /* MDMX destination register. */
8927 case 'Y': /* MDMX source register. */
8928 case 'Z': /* MDMX target register. */
8929 is_mdmx = 1;
252b5132
RH
8930 case 'D': /* floating point destination register */
8931 case 'S': /* floating point source register */
8932 case 'T': /* floating point target register */
8933 case 'R': /* floating point source register */
8934 case 'V':
8935 case 'W':
8936 s_reset = s;
deec1734
CD
8937 /* Accept $fN for FP and MDMX register numbers, and in
8938 addition accept $vN for MDMX register numbers. */
8939 if ((s[0] == '$' && s[1] == 'f' && ISDIGIT (s[2]))
8940 || (is_mdmx != 0 && s[0] == '$' && s[1] == 'v'
8941 && ISDIGIT (s[2])))
252b5132
RH
8942 {
8943 s += 2;
8944 regno = 0;
8945 do
8946 {
8947 regno *= 10;
8948 regno += *s - '0';
8949 ++s;
8950 }
3882b010 8951 while (ISDIGIT (*s));
252b5132
RH
8952
8953 if (regno > 31)
8954 as_bad (_("Invalid float register number (%d)"), regno);
8955
8956 if ((regno & 1) != 0
ca4e0257 8957 && HAVE_32BIT_FPRS
252b5132
RH
8958 && ! (strcmp (str, "mtc1") == 0
8959 || strcmp (str, "mfc1") == 0
8960 || strcmp (str, "lwc1") == 0
8961 || strcmp (str, "swc1") == 0
8962 || strcmp (str, "l.s") == 0
8963 || strcmp (str, "s.s") == 0))
8964 as_warn (_("Float register should be even, was %d"),
8965 regno);
8966
8967 c = *args;
8968 if (*s == ' ')
f9419b05 8969 ++s;
252b5132
RH
8970 if (args[1] != *s)
8971 {
8972 if (c == 'V' || c == 'W')
8973 {
8974 regno = lastregno;
8975 s = s_reset;
f9419b05 8976 ++args;
252b5132
RH
8977 }
8978 }
8979 switch (c)
8980 {
8981 case 'D':
deec1734 8982 case 'X':
38487616 8983 ip->insn_opcode |= regno << OP_SH_FD;
252b5132
RH
8984 break;
8985 case 'V':
8986 case 'S':
deec1734 8987 case 'Y':
38487616 8988 ip->insn_opcode |= regno << OP_SH_FS;
252b5132 8989 break;
deec1734
CD
8990 case 'Q':
8991 /* This is like 'Z', but also needs to fix the MDMX
8992 vector/scalar select bits. Note that the
8993 scalar immediate case is handled above. */
8994 if (*s == '[')
8995 {
8996 int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
8997 int max_el = (is_qh ? 3 : 7);
8998 s++;
8999 my_getExpression(&imm_expr, s);
9000 check_absolute_expr (ip, &imm_expr);
9001 s = expr_end;
9002 if (imm_expr.X_add_number > max_el)
9003 as_bad(_("Bad element selector %ld"),
9004 (long) imm_expr.X_add_number);
9005 imm_expr.X_add_number &= max_el;
9006 ip->insn_opcode |= (imm_expr.X_add_number
9007 << (OP_SH_VSEL +
9008 (is_qh ? 2 : 1)));
9009 if (*s != ']')
9010 as_warn(_("Expecting ']' found '%s'"), s);
9011 else
9012 s++;
9013 }
9014 else
9015 {
9016 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
9017 ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
9018 << OP_SH_VSEL);
9019 else
9020 ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
9021 OP_SH_VSEL);
9022 }
9023 /* Fall through */
252b5132
RH
9024 case 'W':
9025 case 'T':
deec1734 9026 case 'Z':
38487616 9027 ip->insn_opcode |= regno << OP_SH_FT;
252b5132
RH
9028 break;
9029 case 'R':
38487616 9030 ip->insn_opcode |= regno << OP_SH_FR;
252b5132
RH
9031 break;
9032 }
9033 lastregno = regno;
9034 continue;
9035 }
9036
252b5132
RH
9037 switch (*args++)
9038 {
9039 case 'V':
38487616 9040 ip->insn_opcode |= lastregno << OP_SH_FS;
252b5132
RH
9041 continue;
9042 case 'W':
38487616 9043 ip->insn_opcode |= lastregno << OP_SH_FT;
252b5132
RH
9044 continue;
9045 }
9046 break;
9047
9048 case 'I':
9049 my_getExpression (&imm_expr, s);
9050 if (imm_expr.X_op != O_big
9051 && imm_expr.X_op != O_constant)
9052 insn_error = _("absolute expression required");
9053 s = expr_end;
9054 continue;
9055
9056 case 'A':
9057 my_getExpression (&offset_expr, s);
f6688943 9058 *imm_reloc = BFD_RELOC_32;
252b5132
RH
9059 s = expr_end;
9060 continue;
9061
9062 case 'F':
9063 case 'L':
9064 case 'f':
9065 case 'l':
9066 {
9067 int f64;
ca4e0257 9068 int using_gprs;
252b5132
RH
9069 char *save_in;
9070 char *err;
9071 unsigned char temp[8];
9072 int len;
9073 unsigned int length;
9074 segT seg;
9075 subsegT subseg;
9076 char *p;
9077
9078 /* These only appear as the last operand in an
9079 instruction, and every instruction that accepts
9080 them in any variant accepts them in all variants.
9081 This means we don't have to worry about backing out
9082 any changes if the instruction does not match.
9083
9084 The difference between them is the size of the
9085 floating point constant and where it goes. For 'F'
9086 and 'L' the constant is 64 bits; for 'f' and 'l' it
9087 is 32 bits. Where the constant is placed is based
9088 on how the MIPS assembler does things:
9089 F -- .rdata
9090 L -- .lit8
9091 f -- immediate value
9092 l -- .lit4
9093
9094 The .lit4 and .lit8 sections are only used if
9095 permitted by the -G argument.
9096
9097 When generating embedded PIC code, we use the
9098 .lit8 section but not the .lit4 section (we can do
9099 .lit4 inline easily; we need to put .lit8
9100 somewhere in the data segment, and using .lit8
9101 permits the linker to eventually combine identical
ca4e0257
RS
9102 .lit8 entries).
9103
9104 The code below needs to know whether the target register
9105 is 32 or 64 bits wide. It relies on the fact 'f' and
9106 'F' are used with GPR-based instructions and 'l' and
9107 'L' are used with FPR-based instructions. */
252b5132
RH
9108
9109 f64 = *args == 'F' || *args == 'L';
ca4e0257 9110 using_gprs = *args == 'F' || *args == 'f';
252b5132
RH
9111
9112 save_in = input_line_pointer;
9113 input_line_pointer = s;
9114 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
9115 length = len;
9116 s = input_line_pointer;
9117 input_line_pointer = save_in;
9118 if (err != NULL && *err != '\0')
9119 {
9120 as_bad (_("Bad floating point constant: %s"), err);
9121 memset (temp, '\0', sizeof temp);
9122 length = f64 ? 8 : 4;
9123 }
9124
156c2f8b 9125 assert (length == (unsigned) (f64 ? 8 : 4));
252b5132
RH
9126
9127 if (*args == 'f'
9128 || (*args == 'l'
9129 && (! USE_GLOBAL_POINTER_OPT
9130 || mips_pic == EMBEDDED_PIC
9131 || g_switch_value < 4
9132 || (temp[0] == 0 && temp[1] == 0)
9133 || (temp[2] == 0 && temp[3] == 0))))
9134 {
9135 imm_expr.X_op = O_constant;
9136 if (! target_big_endian)
9137 imm_expr.X_add_number = bfd_getl32 (temp);
9138 else
9139 imm_expr.X_add_number = bfd_getb32 (temp);
9140 }
9141 else if (length > 4
119d663a 9142 && ! mips_disable_float_construction
ca4e0257
RS
9143 /* Constants can only be constructed in GPRs and
9144 copied to FPRs if the GPRs are at least as wide
9145 as the FPRs. Force the constant into memory if
9146 we are using 64-bit FPRs but the GPRs are only
9147 32 bits wide. */
9148 && (using_gprs
9149 || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
252b5132
RH
9150 && ((temp[0] == 0 && temp[1] == 0)
9151 || (temp[2] == 0 && temp[3] == 0))
9152 && ((temp[4] == 0 && temp[5] == 0)
9153 || (temp[6] == 0 && temp[7] == 0)))
9154 {
ca4e0257
RS
9155 /* The value is simple enough to load with a couple of
9156 instructions. If using 32-bit registers, set
9157 imm_expr to the high order 32 bits and offset_expr to
9158 the low order 32 bits. Otherwise, set imm_expr to
9159 the entire 64 bit constant. */
9160 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
252b5132
RH
9161 {
9162 imm_expr.X_op = O_constant;
9163 offset_expr.X_op = O_constant;
9164 if (! target_big_endian)
9165 {
9166 imm_expr.X_add_number = bfd_getl32 (temp + 4);
9167 offset_expr.X_add_number = bfd_getl32 (temp);
9168 }
9169 else
9170 {
9171 imm_expr.X_add_number = bfd_getb32 (temp);
9172 offset_expr.X_add_number = bfd_getb32 (temp + 4);
9173 }
9174 if (offset_expr.X_add_number == 0)
9175 offset_expr.X_op = O_absent;
9176 }
9177 else if (sizeof (imm_expr.X_add_number) > 4)
9178 {
9179 imm_expr.X_op = O_constant;
9180 if (! target_big_endian)
9181 imm_expr.X_add_number = bfd_getl64 (temp);
9182 else
9183 imm_expr.X_add_number = bfd_getb64 (temp);
9184 }
9185 else
9186 {
9187 imm_expr.X_op = O_big;
9188 imm_expr.X_add_number = 4;
9189 if (! target_big_endian)
9190 {
9191 generic_bignum[0] = bfd_getl16 (temp);
9192 generic_bignum[1] = bfd_getl16 (temp + 2);
9193 generic_bignum[2] = bfd_getl16 (temp + 4);
9194 generic_bignum[3] = bfd_getl16 (temp + 6);
9195 }
9196 else
9197 {
9198 generic_bignum[0] = bfd_getb16 (temp + 6);
9199 generic_bignum[1] = bfd_getb16 (temp + 4);
9200 generic_bignum[2] = bfd_getb16 (temp + 2);
9201 generic_bignum[3] = bfd_getb16 (temp);
9202 }
9203 }
9204 }
9205 else
9206 {
9207 const char *newname;
9208 segT new_seg;
9209
9210 /* Switch to the right section. */
9211 seg = now_seg;
9212 subseg = now_subseg;
9213 switch (*args)
9214 {
9215 default: /* unused default case avoids warnings. */
9216 case 'L':
9217 newname = RDATA_SECTION_NAME;
bb2d6cd7
GK
9218 if ((USE_GLOBAL_POINTER_OPT && g_switch_value >= 8)
9219 || mips_pic == EMBEDDED_PIC)
252b5132
RH
9220 newname = ".lit8";
9221 break;
9222 case 'F':
bb2d6cd7
GK
9223 if (mips_pic == EMBEDDED_PIC)
9224 newname = ".lit8";
9225 else
9226 newname = RDATA_SECTION_NAME;
252b5132
RH
9227 break;
9228 case 'l':
9229 assert (!USE_GLOBAL_POINTER_OPT
9230 || g_switch_value >= 4);
9231 newname = ".lit4";
9232 break;
9233 }
9234 new_seg = subseg_new (newname, (subsegT) 0);
9235 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
9236 bfd_set_section_flags (stdoutput, new_seg,
9237 (SEC_ALLOC
9238 | SEC_LOAD
9239 | SEC_READONLY
9240 | SEC_DATA));
9241 frag_align (*args == 'l' ? 2 : 3, 0, 0);
9242 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
9243 && strcmp (TARGET_OS, "elf") != 0)
9244 record_alignment (new_seg, 4);
9245 else
9246 record_alignment (new_seg, *args == 'l' ? 2 : 3);
9247 if (seg == now_seg)
9248 as_bad (_("Can't use floating point insn in this section"));
9249
9250 /* Set the argument to the current address in the
9251 section. */
9252 offset_expr.X_op = O_symbol;
9253 offset_expr.X_add_symbol =
9254 symbol_new ("L0\001", now_seg,
9255 (valueT) frag_now_fix (), frag_now);
9256 offset_expr.X_add_number = 0;
9257
9258 /* Put the floating point number into the section. */
9259 p = frag_more ((int) length);
9260 memcpy (p, temp, length);
9261
9262 /* Switch back to the original section. */
9263 subseg_set (seg, subseg);
9264 }
9265 }
9266 continue;
9267
9268 case 'i': /* 16 bit unsigned immediate */
9269 case 'j': /* 16 bit signed immediate */
f6688943 9270 *imm_reloc = BFD_RELOC_LO16;
5e0116d5 9271 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0)
252b5132
RH
9272 {
9273 int more;
5e0116d5
RS
9274 offsetT minval, maxval;
9275
9276 more = (insn + 1 < &mips_opcodes[NUMOPCODES]
9277 && strcmp (insn->name, insn[1].name) == 0);
9278
9279 /* If the expression was written as an unsigned number,
9280 only treat it as signed if there are no more
9281 alternatives. */
9282 if (more
9283 && *args == 'j'
9284 && sizeof (imm_expr.X_add_number) <= 4
9285 && imm_expr.X_op == O_constant
9286 && imm_expr.X_add_number < 0
9287 && imm_expr.X_unsigned
9288 && HAVE_64BIT_GPRS)
9289 break;
9290
9291 /* For compatibility with older assemblers, we accept
9292 0x8000-0xffff as signed 16-bit numbers when only
9293 signed numbers are allowed. */
9294 if (*args == 'i')
9295 minval = 0, maxval = 0xffff;
9296 else if (more)
9297 minval = -0x8000, maxval = 0x7fff;
252b5132 9298 else
5e0116d5
RS
9299 minval = -0x8000, maxval = 0xffff;
9300
9301 if (imm_expr.X_op != O_constant
9302 || imm_expr.X_add_number < minval
9303 || imm_expr.X_add_number > maxval)
252b5132
RH
9304 {
9305 if (more)
9306 break;
2ae7e77b
AH
9307 if (imm_expr.X_op == O_constant
9308 || imm_expr.X_op == O_big)
5e0116d5 9309 as_bad (_("expression out of range"));
252b5132
RH
9310 }
9311 }
9312 s = expr_end;
9313 continue;
9314
9315 case 'o': /* 16 bit offset */
5e0116d5
RS
9316 /* Check whether there is only a single bracketed expression
9317 left. If so, it must be the base register and the
9318 constant must be zero. */
9319 if (*s == '(' && strchr (s + 1, '(') == 0)
9320 {
9321 offset_expr.X_op = O_constant;
9322 offset_expr.X_add_number = 0;
9323 continue;
9324 }
252b5132
RH
9325
9326 /* If this value won't fit into a 16 bit offset, then go
9327 find a macro that will generate the 32 bit offset
afdbd6d0 9328 code pattern. */
5e0116d5 9329 if (my_getSmallExpression (&offset_expr, offset_reloc, s) == 0
252b5132
RH
9330 && (offset_expr.X_op != O_constant
9331 || offset_expr.X_add_number >= 0x8000
afdbd6d0 9332 || offset_expr.X_add_number < -0x8000))
252b5132
RH
9333 break;
9334
252b5132
RH
9335 s = expr_end;
9336 continue;
9337
9338 case 'p': /* pc relative offset */
0b25d3e6 9339 *offset_reloc = BFD_RELOC_16_PCREL_S2;
252b5132
RH
9340 my_getExpression (&offset_expr, s);
9341 s = expr_end;
9342 continue;
9343
9344 case 'u': /* upper 16 bits */
5e0116d5
RS
9345 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0
9346 && imm_expr.X_op == O_constant
9347 && (imm_expr.X_add_number < 0
9348 || imm_expr.X_add_number >= 0x10000))
252b5132
RH
9349 as_bad (_("lui expression not in range 0..65535"));
9350 s = expr_end;
9351 continue;
9352
9353 case 'a': /* 26 bit address */
9354 my_getExpression (&offset_expr, s);
9355 s = expr_end;
f6688943 9356 *offset_reloc = BFD_RELOC_MIPS_JMP;
252b5132
RH
9357 continue;
9358
9359 case 'N': /* 3 bit branch condition code */
9360 case 'M': /* 3 bit compare condition code */
9361 if (strncmp (s, "$fcc", 4) != 0)
9362 break;
9363 s += 4;
9364 regno = 0;
9365 do
9366 {
9367 regno *= 10;
9368 regno += *s - '0';
9369 ++s;
9370 }
3882b010 9371 while (ISDIGIT (*s));
252b5132 9372 if (regno > 7)
30c378fd
CD
9373 as_bad (_("Invalid condition code register $fcc%d"), regno);
9374 if ((strcmp(str + strlen(str) - 3, ".ps") == 0
9375 || strcmp(str + strlen(str) - 5, "any2f") == 0
9376 || strcmp(str + strlen(str) - 5, "any2t") == 0)
9377 && (regno & 1) != 0)
9378 as_warn(_("Condition code register should be even for %s, was %d"),
9379 str, regno);
9380 if ((strcmp(str + strlen(str) - 5, "any4f") == 0
9381 || strcmp(str + strlen(str) - 5, "any4t") == 0)
9382 && (regno & 3) != 0)
9383 as_warn(_("Condition code register should be 0 or 4 for %s, was %d"),
9384 str, regno);
252b5132
RH
9385 if (*args == 'N')
9386 ip->insn_opcode |= regno << OP_SH_BCC;
9387 else
9388 ip->insn_opcode |= regno << OP_SH_CCC;
beae10d5 9389 continue;
252b5132 9390
156c2f8b
NC
9391 case 'H':
9392 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
9393 s += 2;
3882b010 9394 if (ISDIGIT (*s))
156c2f8b
NC
9395 {
9396 c = 0;
9397 do
9398 {
9399 c *= 10;
9400 c += *s - '0';
9401 ++s;
9402 }
3882b010 9403 while (ISDIGIT (*s));
156c2f8b
NC
9404 }
9405 else
9406 c = 8; /* Invalid sel value. */
9407
9408 if (c > 7)
9409 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
9410 ip->insn_opcode |= c;
9411 continue;
9412
60b63b72
RS
9413 case 'e':
9414 /* Must be at least one digit. */
9415 my_getExpression (&imm_expr, s);
9416 check_absolute_expr (ip, &imm_expr);
9417
9418 if ((unsigned long) imm_expr.X_add_number
9419 > (unsigned long) OP_MASK_VECBYTE)
9420 {
9421 as_bad (_("bad byte vector index (%ld)"),
9422 (long) imm_expr.X_add_number);
9423 imm_expr.X_add_number = 0;
9424 }
9425
9426 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECBYTE;
9427 imm_expr.X_op = O_absent;
9428 s = expr_end;
9429 continue;
9430
9431 case '%':
9432 my_getExpression (&imm_expr, s);
9433 check_absolute_expr (ip, &imm_expr);
9434
9435 if ((unsigned long) imm_expr.X_add_number
9436 > (unsigned long) OP_MASK_VECALIGN)
9437 {
9438 as_bad (_("bad byte vector index (%ld)"),
9439 (long) imm_expr.X_add_number);
9440 imm_expr.X_add_number = 0;
9441 }
9442
9443 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECALIGN;
9444 imm_expr.X_op = O_absent;
9445 s = expr_end;
9446 continue;
9447
252b5132
RH
9448 default:
9449 as_bad (_("bad char = '%c'\n"), *args);
9450 internalError ();
9451 }
9452 break;
9453 }
9454 /* Args don't match. */
9455 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
9456 !strcmp (insn->name, insn[1].name))
9457 {
9458 ++insn;
9459 s = argsStart;
268f6bed 9460 insn_error = _("illegal operands");
252b5132
RH
9461 continue;
9462 }
268f6bed
L
9463 if (save_c)
9464 *(--s) = save_c;
252b5132
RH
9465 insn_error = _("illegal operands");
9466 return;
9467 }
9468}
9469
9470/* This routine assembles an instruction into its binary format when
9471 assembling for the mips16. As a side effect, it sets one of the
9472 global variables imm_reloc or offset_reloc to the type of
9473 relocation to do if one of the operands is an address expression.
9474 It also sets mips16_small and mips16_ext if the user explicitly
9475 requested a small or extended instruction. */
9476
9477static void
17a2f251 9478mips16_ip (char *str, struct mips_cl_insn *ip)
252b5132
RH
9479{
9480 char *s;
9481 const char *args;
9482 struct mips_opcode *insn;
9483 char *argsstart;
9484 unsigned int regno;
9485 unsigned int lastregno = 0;
9486 char *s_reset;
9487
9488 insn_error = NULL;
9489
b34976b6
AM
9490 mips16_small = FALSE;
9491 mips16_ext = FALSE;
252b5132 9492
3882b010 9493 for (s = str; ISLOWER (*s); ++s)
252b5132
RH
9494 ;
9495 switch (*s)
9496 {
9497 case '\0':
9498 break;
9499
9500 case ' ':
9501 *s++ = '\0';
9502 break;
9503
9504 case '.':
9505 if (s[1] == 't' && s[2] == ' ')
9506 {
9507 *s = '\0';
b34976b6 9508 mips16_small = TRUE;
252b5132
RH
9509 s += 3;
9510 break;
9511 }
9512 else if (s[1] == 'e' && s[2] == ' ')
9513 {
9514 *s = '\0';
b34976b6 9515 mips16_ext = TRUE;
252b5132
RH
9516 s += 3;
9517 break;
9518 }
9519 /* Fall through. */
9520 default:
9521 insn_error = _("unknown opcode");
9522 return;
9523 }
9524
9525 if (mips_opts.noautoextend && ! mips16_ext)
b34976b6 9526 mips16_small = TRUE;
252b5132
RH
9527
9528 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
9529 {
9530 insn_error = _("unrecognized opcode");
9531 return;
9532 }
9533
9534 argsstart = s;
9535 for (;;)
9536 {
9537 assert (strcmp (insn->name, str) == 0);
9538
9539 ip->insn_mo = insn;
9540 ip->insn_opcode = insn->match;
b34976b6 9541 ip->use_extend = FALSE;
252b5132 9542 imm_expr.X_op = O_absent;
f6688943
TS
9543 imm_reloc[0] = BFD_RELOC_UNUSED;
9544 imm_reloc[1] = BFD_RELOC_UNUSED;
9545 imm_reloc[2] = BFD_RELOC_UNUSED;
5f74bc13 9546 imm2_expr.X_op = O_absent;
252b5132 9547 offset_expr.X_op = O_absent;
f6688943
TS
9548 offset_reloc[0] = BFD_RELOC_UNUSED;
9549 offset_reloc[1] = BFD_RELOC_UNUSED;
9550 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
9551 for (args = insn->args; 1; ++args)
9552 {
9553 int c;
9554
9555 if (*s == ' ')
9556 ++s;
9557
9558 /* In this switch statement we call break if we did not find
9559 a match, continue if we did find a match, or return if we
9560 are done. */
9561
9562 c = *args;
9563 switch (c)
9564 {
9565 case '\0':
9566 if (*s == '\0')
9567 {
9568 /* Stuff the immediate value in now, if we can. */
9569 if (imm_expr.X_op == O_constant
f6688943 9570 && *imm_reloc > BFD_RELOC_UNUSED
252b5132
RH
9571 && insn->pinfo != INSN_MACRO)
9572 {
c4e7957c 9573 mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
b34976b6 9574 imm_expr.X_add_number, TRUE, mips16_small,
252b5132
RH
9575 mips16_ext, &ip->insn_opcode,
9576 &ip->use_extend, &ip->extend);
9577 imm_expr.X_op = O_absent;
f6688943 9578 *imm_reloc = BFD_RELOC_UNUSED;
252b5132
RH
9579 }
9580
9581 return;
9582 }
9583 break;
9584
9585 case ',':
9586 if (*s++ == c)
9587 continue;
9588 s--;
9589 switch (*++args)
9590 {
9591 case 'v':
9592 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9593 continue;
9594 case 'w':
9595 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9596 continue;
9597 }
9598 break;
9599
9600 case '(':
9601 case ')':
9602 if (*s++ == c)
9603 continue;
9604 break;
9605
9606 case 'v':
9607 case 'w':
9608 if (s[0] != '$')
9609 {
9610 if (c == 'v')
9611 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9612 else
9613 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9614 ++args;
9615 continue;
9616 }
9617 /* Fall through. */
9618 case 'x':
9619 case 'y':
9620 case 'z':
9621 case 'Z':
9622 case '0':
9623 case 'S':
9624 case 'R':
9625 case 'X':
9626 case 'Y':
9627 if (s[0] != '$')
9628 break;
9629 s_reset = s;
3882b010 9630 if (ISDIGIT (s[1]))
252b5132
RH
9631 {
9632 ++s;
9633 regno = 0;
9634 do
9635 {
9636 regno *= 10;
9637 regno += *s - '0';
9638 ++s;
9639 }
3882b010 9640 while (ISDIGIT (*s));
252b5132
RH
9641 if (regno > 31)
9642 {
9643 as_bad (_("invalid register number (%d)"), regno);
9644 regno = 2;
9645 }
9646 }
9647 else
9648 {
76db943d
TS
9649 if (s[1] == 'r' && s[2] == 'a')
9650 {
9651 s += 3;
9652 regno = RA;
9653 }
9654 else if (s[1] == 'f' && s[2] == 'p')
252b5132
RH
9655 {
9656 s += 3;
9657 regno = FP;
9658 }
9659 else if (s[1] == 's' && s[2] == 'p')
9660 {
9661 s += 3;
9662 regno = SP;
9663 }
9664 else if (s[1] == 'g' && s[2] == 'p')
9665 {
9666 s += 3;
9667 regno = GP;
9668 }
9669 else if (s[1] == 'a' && s[2] == 't')
9670 {
9671 s += 3;
9672 regno = AT;
9673 }
9674 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
9675 {
9676 s += 4;
9677 regno = KT0;
9678 }
9679 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
9680 {
9681 s += 4;
9682 regno = KT1;
9683 }
85b51719
TS
9684 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
9685 {
9686 s += 5;
9687 regno = ZERO;
9688 }
252b5132
RH
9689 else
9690 break;
9691 }
9692
9693 if (*s == ' ')
9694 ++s;
9695 if (args[1] != *s)
9696 {
9697 if (c == 'v' || c == 'w')
9698 {
9699 regno = mips16_to_32_reg_map[lastregno];
9700 s = s_reset;
f9419b05 9701 ++args;
252b5132
RH
9702 }
9703 }
9704
9705 switch (c)
9706 {
9707 case 'x':
9708 case 'y':
9709 case 'z':
9710 case 'v':
9711 case 'w':
9712 case 'Z':
9713 regno = mips32_to_16_reg_map[regno];
9714 break;
9715
9716 case '0':
9717 if (regno != 0)
9718 regno = ILLEGAL_REG;
9719 break;
9720
9721 case 'S':
9722 if (regno != SP)
9723 regno = ILLEGAL_REG;
9724 break;
9725
9726 case 'R':
9727 if (regno != RA)
9728 regno = ILLEGAL_REG;
9729 break;
9730
9731 case 'X':
9732 case 'Y':
9733 if (regno == AT && ! mips_opts.noat)
9734 as_warn (_("used $at without \".set noat\""));
9735 break;
9736
9737 default:
9738 internalError ();
9739 }
9740
9741 if (regno == ILLEGAL_REG)
9742 break;
9743
9744 switch (c)
9745 {
9746 case 'x':
9747 case 'v':
9748 ip->insn_opcode |= regno << MIPS16OP_SH_RX;
9749 break;
9750 case 'y':
9751 case 'w':
9752 ip->insn_opcode |= regno << MIPS16OP_SH_RY;
9753 break;
9754 case 'z':
9755 ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
9756 break;
9757 case 'Z':
9758 ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
9759 case '0':
9760 case 'S':
9761 case 'R':
9762 break;
9763 case 'X':
9764 ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
9765 break;
9766 case 'Y':
9767 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
9768 ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
9769 break;
9770 default:
9771 internalError ();
9772 }
9773
9774 lastregno = regno;
9775 continue;
9776
9777 case 'P':
9778 if (strncmp (s, "$pc", 3) == 0)
9779 {
9780 s += 3;
9781 continue;
9782 }
9783 break;
9784
9785 case '<':
9786 case '>':
9787 case '[':
9788 case ']':
9789 case '4':
9790 case '5':
9791 case 'H':
9792 case 'W':
9793 case 'D':
9794 case 'j':
9795 case '8':
9796 case 'V':
9797 case 'C':
9798 case 'U':
9799 case 'k':
9800 case 'K':
9801 if (s[0] == '%'
9802 && strncmp (s + 1, "gprel(", sizeof "gprel(" - 1) == 0)
9803 {
9804 /* This is %gprel(SYMBOL). We need to read SYMBOL,
9805 and generate the appropriate reloc. If the text
9806 inside %gprel is not a symbol name with an
9807 optional offset, then we generate a normal reloc
9808 and will probably fail later. */
9809 my_getExpression (&imm_expr, s + sizeof "%gprel" - 1);
9810 if (imm_expr.X_op == O_symbol)
9811 {
b34976b6 9812 mips16_ext = TRUE;
f6688943 9813 *imm_reloc = BFD_RELOC_MIPS16_GPREL;
252b5132 9814 s = expr_end;
b34976b6 9815 ip->use_extend = TRUE;
252b5132
RH
9816 ip->extend = 0;
9817 continue;
9818 }
9819 }
9820 else
9821 {
9822 /* Just pick up a normal expression. */
9823 my_getExpression (&imm_expr, s);
9824 }
9825
9826 if (imm_expr.X_op == O_register)
9827 {
9828 /* What we thought was an expression turned out to
9829 be a register. */
9830
9831 if (s[0] == '(' && args[1] == '(')
9832 {
9833 /* It looks like the expression was omitted
9834 before a register indirection, which means
9835 that the expression is implicitly zero. We
9836 still set up imm_expr, so that we handle
9837 explicit extensions correctly. */
9838 imm_expr.X_op = O_constant;
9839 imm_expr.X_add_number = 0;
f6688943 9840 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
9841 continue;
9842 }
9843
9844 break;
9845 }
9846
9847 /* We need to relax this instruction. */
f6688943 9848 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
9849 s = expr_end;
9850 continue;
9851
9852 case 'p':
9853 case 'q':
9854 case 'A':
9855 case 'B':
9856 case 'E':
9857 /* We use offset_reloc rather than imm_reloc for the PC
9858 relative operands. This lets macros with both
9859 immediate and address operands work correctly. */
9860 my_getExpression (&offset_expr, s);
9861
9862 if (offset_expr.X_op == O_register)
9863 break;
9864
9865 /* We need to relax this instruction. */
f6688943 9866 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
9867 s = expr_end;
9868 continue;
9869
9870 case '6': /* break code */
9871 my_getExpression (&imm_expr, s);
9872 check_absolute_expr (ip, &imm_expr);
9873 if ((unsigned long) imm_expr.X_add_number > 63)
9874 {
9875 as_warn (_("Invalid value for `%s' (%lu)"),
9876 ip->insn_mo->name,
9877 (unsigned long) imm_expr.X_add_number);
9878 imm_expr.X_add_number &= 0x3f;
9879 }
9880 ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
9881 imm_expr.X_op = O_absent;
9882 s = expr_end;
9883 continue;
9884
9885 case 'a': /* 26 bit address */
9886 my_getExpression (&offset_expr, s);
9887 s = expr_end;
f6688943 9888 *offset_reloc = BFD_RELOC_MIPS16_JMP;
252b5132
RH
9889 ip->insn_opcode <<= 16;
9890 continue;
9891
9892 case 'l': /* register list for entry macro */
9893 case 'L': /* register list for exit macro */
9894 {
9895 int mask;
9896
9897 if (c == 'l')
9898 mask = 0;
9899 else
9900 mask = 7 << 3;
9901 while (*s != '\0')
9902 {
9903 int freg, reg1, reg2;
9904
9905 while (*s == ' ' || *s == ',')
9906 ++s;
9907 if (*s != '$')
9908 {
9909 as_bad (_("can't parse register list"));
9910 break;
9911 }
9912 ++s;
9913 if (*s != 'f')
9914 freg = 0;
9915 else
9916 {
9917 freg = 1;
9918 ++s;
9919 }
9920 reg1 = 0;
3882b010 9921 while (ISDIGIT (*s))
252b5132
RH
9922 {
9923 reg1 *= 10;
9924 reg1 += *s - '0';
9925 ++s;
9926 }
9927 if (*s == ' ')
9928 ++s;
9929 if (*s != '-')
9930 reg2 = reg1;
9931 else
9932 {
9933 ++s;
9934 if (*s != '$')
9935 break;
9936 ++s;
9937 if (freg)
9938 {
9939 if (*s == 'f')
9940 ++s;
9941 else
9942 {
9943 as_bad (_("invalid register list"));
9944 break;
9945 }
9946 }
9947 reg2 = 0;
3882b010 9948 while (ISDIGIT (*s))
252b5132
RH
9949 {
9950 reg2 *= 10;
9951 reg2 += *s - '0';
9952 ++s;
9953 }
9954 }
9955 if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
9956 {
9957 mask &= ~ (7 << 3);
9958 mask |= 5 << 3;
9959 }
9960 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
9961 {
9962 mask &= ~ (7 << 3);
9963 mask |= 6 << 3;
9964 }
9965 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
9966 mask |= (reg2 - 3) << 3;
9967 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
9968 mask |= (reg2 - 15) << 1;
f9419b05 9969 else if (reg1 == RA && reg2 == RA)
252b5132
RH
9970 mask |= 1;
9971 else
9972 {
9973 as_bad (_("invalid register list"));
9974 break;
9975 }
9976 }
9977 /* The mask is filled in in the opcode table for the
9978 benefit of the disassembler. We remove it before
9979 applying the actual mask. */
9980 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
9981 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
9982 }
9983 continue;
9984
9985 case 'e': /* extend code */
9986 my_getExpression (&imm_expr, s);
9987 check_absolute_expr (ip, &imm_expr);
9988 if ((unsigned long) imm_expr.X_add_number > 0x7ff)
9989 {
9990 as_warn (_("Invalid value for `%s' (%lu)"),
9991 ip->insn_mo->name,
9992 (unsigned long) imm_expr.X_add_number);
9993 imm_expr.X_add_number &= 0x7ff;
9994 }
9995 ip->insn_opcode |= imm_expr.X_add_number;
9996 imm_expr.X_op = O_absent;
9997 s = expr_end;
9998 continue;
9999
10000 default:
10001 internalError ();
10002 }
10003 break;
10004 }
10005
10006 /* Args don't match. */
10007 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
10008 strcmp (insn->name, insn[1].name) == 0)
10009 {
10010 ++insn;
10011 s = argsstart;
10012 continue;
10013 }
10014
10015 insn_error = _("illegal operands");
10016
10017 return;
10018 }
10019}
10020
10021/* This structure holds information we know about a mips16 immediate
10022 argument type. */
10023
e972090a
NC
10024struct mips16_immed_operand
10025{
252b5132
RH
10026 /* The type code used in the argument string in the opcode table. */
10027 int type;
10028 /* The number of bits in the short form of the opcode. */
10029 int nbits;
10030 /* The number of bits in the extended form of the opcode. */
10031 int extbits;
10032 /* The amount by which the short form is shifted when it is used;
10033 for example, the sw instruction has a shift count of 2. */
10034 int shift;
10035 /* The amount by which the short form is shifted when it is stored
10036 into the instruction code. */
10037 int op_shift;
10038 /* Non-zero if the short form is unsigned. */
10039 int unsp;
10040 /* Non-zero if the extended form is unsigned. */
10041 int extu;
10042 /* Non-zero if the value is PC relative. */
10043 int pcrel;
10044};
10045
10046/* The mips16 immediate operand types. */
10047
10048static const struct mips16_immed_operand mips16_immed_operands[] =
10049{
10050 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
10051 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 },
10052 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
10053 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 },
10054 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
10055 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
10056 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
10057 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
10058 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
10059 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
10060 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
10061 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
10062 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
10063 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
10064 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
10065 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
10066 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
10067 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
10068 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
10069 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
10070 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
10071};
10072
10073#define MIPS16_NUM_IMMED \
10074 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
10075
10076/* Handle a mips16 instruction with an immediate value. This or's the
10077 small immediate value into *INSN. It sets *USE_EXTEND to indicate
10078 whether an extended value is needed; if one is needed, it sets
10079 *EXTEND to the value. The argument type is TYPE. The value is VAL.
10080 If SMALL is true, an unextended opcode was explicitly requested.
10081 If EXT is true, an extended opcode was explicitly requested. If
10082 WARN is true, warn if EXT does not match reality. */
10083
10084static void
17a2f251
TS
10085mips16_immed (char *file, unsigned int line, int type, offsetT val,
10086 bfd_boolean warn, bfd_boolean small, bfd_boolean ext,
10087 unsigned long *insn, bfd_boolean *use_extend,
10088 unsigned short *extend)
252b5132
RH
10089{
10090 register const struct mips16_immed_operand *op;
10091 int mintiny, maxtiny;
b34976b6 10092 bfd_boolean needext;
252b5132
RH
10093
10094 op = mips16_immed_operands;
10095 while (op->type != type)
10096 {
10097 ++op;
10098 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
10099 }
10100
10101 if (op->unsp)
10102 {
10103 if (type == '<' || type == '>' || type == '[' || type == ']')
10104 {
10105 mintiny = 1;
10106 maxtiny = 1 << op->nbits;
10107 }
10108 else
10109 {
10110 mintiny = 0;
10111 maxtiny = (1 << op->nbits) - 1;
10112 }
10113 }
10114 else
10115 {
10116 mintiny = - (1 << (op->nbits - 1));
10117 maxtiny = (1 << (op->nbits - 1)) - 1;
10118 }
10119
10120 /* Branch offsets have an implicit 0 in the lowest bit. */
10121 if (type == 'p' || type == 'q')
10122 val /= 2;
10123
10124 if ((val & ((1 << op->shift) - 1)) != 0
10125 || val < (mintiny << op->shift)
10126 || val > (maxtiny << op->shift))
b34976b6 10127 needext = TRUE;
252b5132 10128 else
b34976b6 10129 needext = FALSE;
252b5132
RH
10130
10131 if (warn && ext && ! needext)
beae10d5
KH
10132 as_warn_where (file, line,
10133 _("extended operand requested but not required"));
252b5132
RH
10134 if (small && needext)
10135 as_bad_where (file, line, _("invalid unextended operand value"));
10136
10137 if (small || (! ext && ! needext))
10138 {
10139 int insnval;
10140
b34976b6 10141 *use_extend = FALSE;
252b5132
RH
10142 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
10143 insnval <<= op->op_shift;
10144 *insn |= insnval;
10145 }
10146 else
10147 {
10148 long minext, maxext;
10149 int extval;
10150
10151 if (op->extu)
10152 {
10153 minext = 0;
10154 maxext = (1 << op->extbits) - 1;
10155 }
10156 else
10157 {
10158 minext = - (1 << (op->extbits - 1));
10159 maxext = (1 << (op->extbits - 1)) - 1;
10160 }
10161 if (val < minext || val > maxext)
10162 as_bad_where (file, line,
10163 _("operand value out of range for instruction"));
10164
b34976b6 10165 *use_extend = TRUE;
252b5132
RH
10166 if (op->extbits == 16)
10167 {
10168 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
10169 val &= 0x1f;
10170 }
10171 else if (op->extbits == 15)
10172 {
10173 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
10174 val &= 0xf;
10175 }
10176 else
10177 {
10178 extval = ((val & 0x1f) << 6) | (val & 0x20);
10179 val = 0;
10180 }
10181
10182 *extend = (unsigned short) extval;
10183 *insn |= val;
10184 }
10185}
10186\f
5e0116d5 10187static const struct percent_op_match
ad8d3bb3 10188{
5e0116d5
RS
10189 const char *str;
10190 bfd_reloc_code_real_type reloc;
ad8d3bb3
TS
10191} percent_op[] =
10192{
5e0116d5 10193 {"%lo", BFD_RELOC_LO16},
ad8d3bb3 10194#ifdef OBJ_ELF
5e0116d5
RS
10195 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
10196 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
10197 {"%call16", BFD_RELOC_MIPS_CALL16},
10198 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
10199 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
10200 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
10201 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
10202 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
10203 {"%got", BFD_RELOC_MIPS_GOT16},
10204 {"%gp_rel", BFD_RELOC_GPREL16},
10205 {"%half", BFD_RELOC_16},
10206 {"%highest", BFD_RELOC_MIPS_HIGHEST},
10207 {"%higher", BFD_RELOC_MIPS_HIGHER},
10208 {"%neg", BFD_RELOC_MIPS_SUB},
ad8d3bb3 10209#endif
5e0116d5 10210 {"%hi", BFD_RELOC_HI16_S}
ad8d3bb3
TS
10211};
10212
252b5132 10213
5e0116d5
RS
10214/* Return true if *STR points to a relocation operator. When returning true,
10215 move *STR over the operator and store its relocation code in *RELOC.
10216 Leave both *STR and *RELOC alone when returning false. */
10217
10218static bfd_boolean
17a2f251 10219parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
252b5132 10220{
5e0116d5 10221 size_t i;
76b3015f 10222
5e0116d5
RS
10223 for (i = 0; i < ARRAY_SIZE (percent_op); i++)
10224 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
394f9b3a 10225 {
5e0116d5
RS
10226 *str += strlen (percent_op[i].str);
10227 *reloc = percent_op[i].reloc;
394f9b3a 10228
5e0116d5
RS
10229 /* Check whether the output BFD supports this relocation.
10230 If not, issue an error and fall back on something safe. */
10231 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
394f9b3a 10232 {
5e0116d5
RS
10233 as_bad ("relocation %s isn't supported by the current ABI",
10234 percent_op[i].str);
10235 *reloc = BFD_RELOC_LO16;
394f9b3a 10236 }
5e0116d5 10237 return TRUE;
394f9b3a 10238 }
5e0116d5 10239 return FALSE;
394f9b3a 10240}
ad8d3bb3 10241
ad8d3bb3 10242
5e0116d5
RS
10243/* Parse string STR as a 16-bit relocatable operand. Store the
10244 expression in *EP and the relocations in the array starting
10245 at RELOC. Return the number of relocation operators used.
ad8d3bb3 10246
5e0116d5
RS
10247 On exit, EXPR_END points to the first character after the expression.
10248 If no relocation operators are used, RELOC[0] is set to BFD_RELOC_LO16. */
ad8d3bb3 10249
5e0116d5 10250static size_t
17a2f251
TS
10251my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
10252 char *str)
ad8d3bb3 10253{
5e0116d5
RS
10254 bfd_reloc_code_real_type reversed_reloc[3];
10255 size_t reloc_index, i;
09b8f35a
RS
10256 int crux_depth, str_depth;
10257 char *crux;
5e0116d5
RS
10258
10259 /* Search for the start of the main expression, recoding relocations
09b8f35a
RS
10260 in REVERSED_RELOC. End the loop with CRUX pointing to the start
10261 of the main expression and with CRUX_DEPTH containing the number
10262 of open brackets at that point. */
10263 reloc_index = -1;
10264 str_depth = 0;
10265 do
fb1b3232 10266 {
09b8f35a
RS
10267 reloc_index++;
10268 crux = str;
10269 crux_depth = str_depth;
10270
10271 /* Skip over whitespace and brackets, keeping count of the number
10272 of brackets. */
10273 while (*str == ' ' || *str == '\t' || *str == '(')
10274 if (*str++ == '(')
10275 str_depth++;
5e0116d5 10276 }
09b8f35a
RS
10277 while (*str == '%'
10278 && reloc_index < (HAVE_NEWABI ? 3 : 1)
10279 && parse_relocation (&str, &reversed_reloc[reloc_index]));
ad8d3bb3 10280
09b8f35a 10281 my_getExpression (ep, crux);
5e0116d5 10282 str = expr_end;
394f9b3a 10283
5e0116d5 10284 /* Match every open bracket. */
09b8f35a 10285 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
5e0116d5 10286 if (*str++ == ')')
09b8f35a 10287 crux_depth--;
394f9b3a 10288
09b8f35a 10289 if (crux_depth > 0)
5e0116d5 10290 as_bad ("unclosed '('");
394f9b3a 10291
5e0116d5 10292 expr_end = str;
252b5132 10293
64bdfcaf
RS
10294 if (reloc_index == 0)
10295 reloc[0] = BFD_RELOC_LO16;
10296 else
10297 {
10298 prev_reloc_op_frag = frag_now;
10299 for (i = 0; i < reloc_index; i++)
10300 reloc[i] = reversed_reloc[reloc_index - 1 - i];
10301 }
fb1b3232 10302
5e0116d5 10303 return reloc_index;
252b5132
RH
10304}
10305
10306static void
17a2f251 10307my_getExpression (expressionS *ep, char *str)
252b5132
RH
10308{
10309 char *save_in;
98aa84af 10310 valueT val;
252b5132
RH
10311
10312 save_in = input_line_pointer;
10313 input_line_pointer = str;
10314 expression (ep);
10315 expr_end = input_line_pointer;
10316 input_line_pointer = save_in;
10317
10318 /* If we are in mips16 mode, and this is an expression based on `.',
10319 then we bump the value of the symbol by 1 since that is how other
10320 text symbols are handled. We don't bother to handle complex
10321 expressions, just `.' plus or minus a constant. */
10322 if (mips_opts.mips16
10323 && ep->X_op == O_symbol
10324 && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
10325 && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
49309057
ILT
10326 && symbol_get_frag (ep->X_add_symbol) == frag_now
10327 && symbol_constant_p (ep->X_add_symbol)
98aa84af
AM
10328 && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
10329 S_SET_VALUE (ep->X_add_symbol, val + 1);
252b5132
RH
10330}
10331
10332/* Turn a string in input_line_pointer into a floating point constant
bc0d738a
NC
10333 of type TYPE, and store the appropriate bytes in *LITP. The number
10334 of LITTLENUMS emitted is stored in *SIZEP. An error message is
252b5132
RH
10335 returned, or NULL on OK. */
10336
10337char *
17a2f251 10338md_atof (int type, char *litP, int *sizeP)
252b5132
RH
10339{
10340 int prec;
10341 LITTLENUM_TYPE words[4];
10342 char *t;
10343 int i;
10344
10345 switch (type)
10346 {
10347 case 'f':
10348 prec = 2;
10349 break;
10350
10351 case 'd':
10352 prec = 4;
10353 break;
10354
10355 default:
10356 *sizeP = 0;
10357 return _("bad call to md_atof");
10358 }
10359
10360 t = atof_ieee (input_line_pointer, type, words);
10361 if (t)
10362 input_line_pointer = t;
10363
10364 *sizeP = prec * 2;
10365
10366 if (! target_big_endian)
10367 {
10368 for (i = prec - 1; i >= 0; i--)
10369 {
17a2f251 10370 md_number_to_chars (litP, words[i], 2);
252b5132
RH
10371 litP += 2;
10372 }
10373 }
10374 else
10375 {
10376 for (i = 0; i < prec; i++)
10377 {
17a2f251 10378 md_number_to_chars (litP, words[i], 2);
252b5132
RH
10379 litP += 2;
10380 }
10381 }
bdaaa2e1 10382
252b5132
RH
10383 return NULL;
10384}
10385
10386void
17a2f251 10387md_number_to_chars (char *buf, valueT val, int n)
252b5132
RH
10388{
10389 if (target_big_endian)
10390 number_to_chars_bigendian (buf, val, n);
10391 else
10392 number_to_chars_littleendian (buf, val, n);
10393}
10394\f
ae948b86 10395#ifdef OBJ_ELF
e013f690
TS
10396static int support_64bit_objects(void)
10397{
10398 const char **list, **l;
aa3d8fdf 10399 int yes;
e013f690
TS
10400
10401 list = bfd_target_list ();
10402 for (l = list; *l != NULL; l++)
10403#ifdef TE_TMIPS
10404 /* This is traditional mips */
10405 if (strcmp (*l, "elf64-tradbigmips") == 0
10406 || strcmp (*l, "elf64-tradlittlemips") == 0)
10407#else
10408 if (strcmp (*l, "elf64-bigmips") == 0
10409 || strcmp (*l, "elf64-littlemips") == 0)
10410#endif
10411 break;
aa3d8fdf 10412 yes = (*l != NULL);
e013f690 10413 free (list);
aa3d8fdf 10414 return yes;
e013f690 10415}
ae948b86 10416#endif /* OBJ_ELF */
e013f690 10417
5a38dc70 10418const char *md_shortopts = "nO::g::G:";
252b5132 10419
e972090a
NC
10420struct option md_longopts[] =
10421{
f9b4148d
CD
10422 /* Options which specify architecture. */
10423#define OPTION_ARCH_BASE (OPTION_MD_BASE)
10424#define OPTION_MARCH (OPTION_ARCH_BASE + 0)
10425 {"march", required_argument, NULL, OPTION_MARCH},
10426#define OPTION_MTUNE (OPTION_ARCH_BASE + 1)
10427 {"mtune", required_argument, NULL, OPTION_MTUNE},
10428#define OPTION_MIPS1 (OPTION_ARCH_BASE + 2)
252b5132
RH
10429 {"mips0", no_argument, NULL, OPTION_MIPS1},
10430 {"mips1", no_argument, NULL, OPTION_MIPS1},
f9b4148d 10431#define OPTION_MIPS2 (OPTION_ARCH_BASE + 3)
252b5132 10432 {"mips2", no_argument, NULL, OPTION_MIPS2},
f9b4148d 10433#define OPTION_MIPS3 (OPTION_ARCH_BASE + 4)
252b5132 10434 {"mips3", no_argument, NULL, OPTION_MIPS3},
f9b4148d 10435#define OPTION_MIPS4 (OPTION_ARCH_BASE + 5)
252b5132 10436 {"mips4", no_argument, NULL, OPTION_MIPS4},
f9b4148d 10437#define OPTION_MIPS5 (OPTION_ARCH_BASE + 6)
ae948b86 10438 {"mips5", no_argument, NULL, OPTION_MIPS5},
f9b4148d 10439#define OPTION_MIPS32 (OPTION_ARCH_BASE + 7)
ae948b86 10440 {"mips32", no_argument, NULL, OPTION_MIPS32},
f9b4148d 10441#define OPTION_MIPS64 (OPTION_ARCH_BASE + 8)
ae948b86 10442 {"mips64", no_argument, NULL, OPTION_MIPS64},
f9b4148d
CD
10443#define OPTION_MIPS32R2 (OPTION_ARCH_BASE + 9)
10444 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
5f74bc13
CD
10445#define OPTION_MIPS64R2 (OPTION_ARCH_BASE + 10)
10446 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
f9b4148d
CD
10447
10448 /* Options which specify Application Specific Extensions (ASEs). */
5f74bc13 10449#define OPTION_ASE_BASE (OPTION_ARCH_BASE + 11)
f9b4148d
CD
10450#define OPTION_MIPS16 (OPTION_ASE_BASE + 0)
10451 {"mips16", no_argument, NULL, OPTION_MIPS16},
10452#define OPTION_NO_MIPS16 (OPTION_ASE_BASE + 1)
10453 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
10454#define OPTION_MIPS3D (OPTION_ASE_BASE + 2)
10455 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
10456#define OPTION_NO_MIPS3D (OPTION_ASE_BASE + 3)
10457 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
10458#define OPTION_MDMX (OPTION_ASE_BASE + 4)
10459 {"mdmx", no_argument, NULL, OPTION_MDMX},
10460#define OPTION_NO_MDMX (OPTION_ASE_BASE + 5)
10461 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
10462
10463 /* Old-style architecture options. Don't add more of these. */
10464#define OPTION_COMPAT_ARCH_BASE (OPTION_ASE_BASE + 6)
10465#define OPTION_M4650 (OPTION_COMPAT_ARCH_BASE + 0)
10466 {"m4650", no_argument, NULL, OPTION_M4650},
10467#define OPTION_NO_M4650 (OPTION_COMPAT_ARCH_BASE + 1)
10468 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
10469#define OPTION_M4010 (OPTION_COMPAT_ARCH_BASE + 2)
10470 {"m4010", no_argument, NULL, OPTION_M4010},
10471#define OPTION_NO_M4010 (OPTION_COMPAT_ARCH_BASE + 3)
10472 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
10473#define OPTION_M4100 (OPTION_COMPAT_ARCH_BASE + 4)
10474 {"m4100", no_argument, NULL, OPTION_M4100},
10475#define OPTION_NO_M4100 (OPTION_COMPAT_ARCH_BASE + 5)
10476 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
10477#define OPTION_M3900 (OPTION_COMPAT_ARCH_BASE + 6)
10478 {"m3900", no_argument, NULL, OPTION_M3900},
10479#define OPTION_NO_M3900 (OPTION_COMPAT_ARCH_BASE + 7)
10480 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
10481
10482 /* Options which enable bug fixes. */
10483#define OPTION_FIX_BASE (OPTION_COMPAT_ARCH_BASE + 8)
10484#define OPTION_M7000_HILO_FIX (OPTION_FIX_BASE + 0)
10485 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
10486#define OPTION_MNO_7000_HILO_FIX (OPTION_FIX_BASE + 1)
10487 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10488 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10489#define OPTION_FIX_VR4122 (OPTION_FIX_BASE + 2)
10490#define OPTION_NO_FIX_VR4122 (OPTION_FIX_BASE + 3)
10491 {"mfix-vr4122-bugs", no_argument, NULL, OPTION_FIX_VR4122},
10492 {"no-mfix-vr4122-bugs", no_argument, NULL, OPTION_NO_FIX_VR4122},
10493
10494 /* Miscellaneous options. */
10495#define OPTION_MISC_BASE (OPTION_FIX_BASE + 4)
10496#define OPTION_MEMBEDDED_PIC (OPTION_MISC_BASE + 0)
252b5132 10497 {"membedded-pic", no_argument, NULL, OPTION_MEMBEDDED_PIC},
f9b4148d 10498#define OPTION_TRAP (OPTION_MISC_BASE + 1)
252b5132
RH
10499 {"trap", no_argument, NULL, OPTION_TRAP},
10500 {"no-break", no_argument, NULL, OPTION_TRAP},
f9b4148d 10501#define OPTION_BREAK (OPTION_MISC_BASE + 2)
252b5132
RH
10502 {"break", no_argument, NULL, OPTION_BREAK},
10503 {"no-trap", no_argument, NULL, OPTION_BREAK},
f9b4148d 10504#define OPTION_EB (OPTION_MISC_BASE + 3)
252b5132 10505 {"EB", no_argument, NULL, OPTION_EB},
f9b4148d 10506#define OPTION_EL (OPTION_MISC_BASE + 4)
252b5132 10507 {"EL", no_argument, NULL, OPTION_EL},
f9b4148d 10508#define OPTION_FP32 (OPTION_MISC_BASE + 5)
ae948b86 10509 {"mfp32", no_argument, NULL, OPTION_FP32},
f9b4148d 10510#define OPTION_GP32 (OPTION_MISC_BASE + 6)
c97ef257 10511 {"mgp32", no_argument, NULL, OPTION_GP32},
f9b4148d 10512#define OPTION_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 7)
119d663a 10513 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
f9b4148d 10514#define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 8)
119d663a 10515 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
f9b4148d 10516#define OPTION_FP64 (OPTION_MISC_BASE + 9)
316f5878 10517 {"mfp64", no_argument, NULL, OPTION_FP64},
f9b4148d 10518#define OPTION_GP64 (OPTION_MISC_BASE + 10)
ae948b86 10519 {"mgp64", no_argument, NULL, OPTION_GP64},
f9b4148d
CD
10520#define OPTION_RELAX_BRANCH (OPTION_MISC_BASE + 11)
10521#define OPTION_NO_RELAX_BRANCH (OPTION_MISC_BASE + 12)
4a6a3df4
AO
10522 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
10523 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
f9b4148d
CD
10524
10525 /* ELF-specific options. */
156c2f8b 10526#ifdef OBJ_ELF
f9b4148d 10527#define OPTION_ELF_BASE (OPTION_MISC_BASE + 13)
156c2f8b 10528#define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
156c2f8b
NC
10529 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
10530 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
ae948b86 10531#define OPTION_NON_SHARED (OPTION_ELF_BASE + 1)
156c2f8b 10532 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
ae948b86 10533#define OPTION_XGOT (OPTION_ELF_BASE + 2)
156c2f8b 10534 {"xgot", no_argument, NULL, OPTION_XGOT},
ae948b86
TS
10535#define OPTION_MABI (OPTION_ELF_BASE + 3)
10536 {"mabi", required_argument, NULL, OPTION_MABI},
10537#define OPTION_32 (OPTION_ELF_BASE + 4)
156c2f8b 10538 {"32", no_argument, NULL, OPTION_32},
ae948b86 10539#define OPTION_N32 (OPTION_ELF_BASE + 5)
e013f690 10540 {"n32", no_argument, NULL, OPTION_N32},
ae948b86 10541#define OPTION_64 (OPTION_ELF_BASE + 6)
156c2f8b 10542 {"64", no_argument, NULL, OPTION_64},
ecb4347a
DJ
10543#define OPTION_MDEBUG (OPTION_ELF_BASE + 7)
10544 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
10545#define OPTION_NO_MDEBUG (OPTION_ELF_BASE + 8)
10546 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
dcd410fe
RO
10547#define OPTION_PDR (OPTION_ELF_BASE + 9)
10548 {"mpdr", no_argument, NULL, OPTION_PDR},
10549#define OPTION_NO_PDR (OPTION_ELF_BASE + 10)
10550 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
ae948b86 10551#endif /* OBJ_ELF */
f9b4148d 10552
252b5132
RH
10553 {NULL, no_argument, NULL, 0}
10554};
156c2f8b 10555size_t md_longopts_size = sizeof (md_longopts);
252b5132 10556
316f5878
RS
10557/* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
10558 NEW_VALUE. Warn if another value was already specified. Note:
10559 we have to defer parsing the -march and -mtune arguments in order
10560 to handle 'from-abi' correctly, since the ABI might be specified
10561 in a later argument. */
10562
10563static void
17a2f251 10564mips_set_option_string (const char **string_ptr, const char *new_value)
316f5878
RS
10565{
10566 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
10567 as_warn (_("A different %s was already specified, is now %s"),
10568 string_ptr == &mips_arch_string ? "-march" : "-mtune",
10569 new_value);
10570
10571 *string_ptr = new_value;
10572}
10573
252b5132 10574int
17a2f251 10575md_parse_option (int c, char *arg)
252b5132
RH
10576{
10577 switch (c)
10578 {
119d663a
NC
10579 case OPTION_CONSTRUCT_FLOATS:
10580 mips_disable_float_construction = 0;
10581 break;
bdaaa2e1 10582
119d663a
NC
10583 case OPTION_NO_CONSTRUCT_FLOATS:
10584 mips_disable_float_construction = 1;
10585 break;
bdaaa2e1 10586
252b5132
RH
10587 case OPTION_TRAP:
10588 mips_trap = 1;
10589 break;
10590
10591 case OPTION_BREAK:
10592 mips_trap = 0;
10593 break;
10594
10595 case OPTION_EB:
10596 target_big_endian = 1;
10597 break;
10598
10599 case OPTION_EL:
10600 target_big_endian = 0;
10601 break;
10602
39c0a331
L
10603 case 'n':
10604 warn_nops = 1;
10605 break;
10606
252b5132
RH
10607 case 'O':
10608 if (arg && arg[1] == '0')
10609 mips_optimize = 1;
10610 else
10611 mips_optimize = 2;
10612 break;
10613
10614 case 'g':
10615 if (arg == NULL)
10616 mips_debug = 2;
10617 else
10618 mips_debug = atoi (arg);
10619 /* When the MIPS assembler sees -g or -g2, it does not do
10620 optimizations which limit full symbolic debugging. We take
10621 that to be equivalent to -O0. */
10622 if (mips_debug == 2)
10623 mips_optimize = 1;
10624 break;
10625
10626 case OPTION_MIPS1:
316f5878 10627 file_mips_isa = ISA_MIPS1;
252b5132
RH
10628 break;
10629
10630 case OPTION_MIPS2:
316f5878 10631 file_mips_isa = ISA_MIPS2;
252b5132
RH
10632 break;
10633
10634 case OPTION_MIPS3:
316f5878 10635 file_mips_isa = ISA_MIPS3;
252b5132
RH
10636 break;
10637
10638 case OPTION_MIPS4:
316f5878 10639 file_mips_isa = ISA_MIPS4;
e7af610e
NC
10640 break;
10641
84ea6cf2 10642 case OPTION_MIPS5:
316f5878 10643 file_mips_isa = ISA_MIPS5;
84ea6cf2
NC
10644 break;
10645
e7af610e 10646 case OPTION_MIPS32:
316f5878 10647 file_mips_isa = ISA_MIPS32;
252b5132
RH
10648 break;
10649
af7ee8bf
CD
10650 case OPTION_MIPS32R2:
10651 file_mips_isa = ISA_MIPS32R2;
10652 break;
10653
5f74bc13
CD
10654 case OPTION_MIPS64R2:
10655 file_mips_isa = ISA_MIPS64R2;
10656 break;
10657
84ea6cf2 10658 case OPTION_MIPS64:
316f5878 10659 file_mips_isa = ISA_MIPS64;
84ea6cf2
NC
10660 break;
10661
ec68c924 10662 case OPTION_MTUNE:
316f5878
RS
10663 mips_set_option_string (&mips_tune_string, arg);
10664 break;
ec68c924 10665
316f5878
RS
10666 case OPTION_MARCH:
10667 mips_set_option_string (&mips_arch_string, arg);
252b5132
RH
10668 break;
10669
10670 case OPTION_M4650:
316f5878
RS
10671 mips_set_option_string (&mips_arch_string, "4650");
10672 mips_set_option_string (&mips_tune_string, "4650");
252b5132
RH
10673 break;
10674
10675 case OPTION_NO_M4650:
10676 break;
10677
10678 case OPTION_M4010:
316f5878
RS
10679 mips_set_option_string (&mips_arch_string, "4010");
10680 mips_set_option_string (&mips_tune_string, "4010");
252b5132
RH
10681 break;
10682
10683 case OPTION_NO_M4010:
10684 break;
10685
10686 case OPTION_M4100:
316f5878
RS
10687 mips_set_option_string (&mips_arch_string, "4100");
10688 mips_set_option_string (&mips_tune_string, "4100");
252b5132
RH
10689 break;
10690
10691 case OPTION_NO_M4100:
10692 break;
10693
252b5132 10694 case OPTION_M3900:
316f5878
RS
10695 mips_set_option_string (&mips_arch_string, "3900");
10696 mips_set_option_string (&mips_tune_string, "3900");
252b5132 10697 break;
bdaaa2e1 10698
252b5132
RH
10699 case OPTION_NO_M3900:
10700 break;
10701
deec1734
CD
10702 case OPTION_MDMX:
10703 mips_opts.ase_mdmx = 1;
10704 break;
10705
10706 case OPTION_NO_MDMX:
10707 mips_opts.ase_mdmx = 0;
10708 break;
10709
252b5132
RH
10710 case OPTION_MIPS16:
10711 mips_opts.mips16 = 1;
b34976b6 10712 mips_no_prev_insn (FALSE);
252b5132
RH
10713 break;
10714
10715 case OPTION_NO_MIPS16:
10716 mips_opts.mips16 = 0;
b34976b6 10717 mips_no_prev_insn (FALSE);
252b5132
RH
10718 break;
10719
1f25f5d3
CD
10720 case OPTION_MIPS3D:
10721 mips_opts.ase_mips3d = 1;
10722 break;
10723
10724 case OPTION_NO_MIPS3D:
10725 mips_opts.ase_mips3d = 0;
10726 break;
10727
252b5132
RH
10728 case OPTION_MEMBEDDED_PIC:
10729 mips_pic = EMBEDDED_PIC;
10730 if (USE_GLOBAL_POINTER_OPT && g_switch_seen)
10731 {
10732 as_bad (_("-G may not be used with embedded PIC code"));
10733 return 0;
10734 }
10735 g_switch_value = 0x7fffffff;
10736 break;
10737
60b63b72
RS
10738 case OPTION_FIX_VR4122:
10739 mips_fix_4122_bugs = 1;
10740 break;
10741
10742 case OPTION_NO_FIX_VR4122:
10743 mips_fix_4122_bugs = 0;
10744 break;
10745
4a6a3df4
AO
10746 case OPTION_RELAX_BRANCH:
10747 mips_relax_branch = 1;
10748 break;
10749
10750 case OPTION_NO_RELAX_BRANCH:
10751 mips_relax_branch = 0;
10752 break;
10753
0f074f60 10754#ifdef OBJ_ELF
252b5132
RH
10755 /* When generating ELF code, we permit -KPIC and -call_shared to
10756 select SVR4_PIC, and -non_shared to select no PIC. This is
10757 intended to be compatible with Irix 5. */
10758 case OPTION_CALL_SHARED:
10759 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10760 {
10761 as_bad (_("-call_shared is supported only for ELF format"));
10762 return 0;
10763 }
10764 mips_pic = SVR4_PIC;
143d77c5 10765 mips_abicalls = TRUE;
252b5132
RH
10766 if (g_switch_seen && g_switch_value != 0)
10767 {
10768 as_bad (_("-G may not be used with SVR4 PIC code"));
10769 return 0;
10770 }
10771 g_switch_value = 0;
10772 break;
10773
10774 case OPTION_NON_SHARED:
10775 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10776 {
10777 as_bad (_("-non_shared is supported only for ELF format"));
10778 return 0;
10779 }
10780 mips_pic = NO_PIC;
143d77c5 10781 mips_abicalls = FALSE;
252b5132
RH
10782 break;
10783
10784 /* The -xgot option tells the assembler to use 32 offsets when
10785 accessing the got in SVR4_PIC mode. It is for Irix
10786 compatibility. */
10787 case OPTION_XGOT:
10788 mips_big_got = 1;
10789 break;
0f074f60 10790#endif /* OBJ_ELF */
252b5132
RH
10791
10792 case 'G':
10793 if (! USE_GLOBAL_POINTER_OPT)
10794 {
10795 as_bad (_("-G is not supported for this configuration"));
10796 return 0;
10797 }
10798 else if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
10799 {
10800 as_bad (_("-G may not be used with SVR4 or embedded PIC code"));
10801 return 0;
10802 }
10803 else
10804 g_switch_value = atoi (arg);
10805 g_switch_seen = 1;
10806 break;
10807
0f074f60 10808#ifdef OBJ_ELF
34ba82a8
TS
10809 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
10810 and -mabi=64. */
252b5132 10811 case OPTION_32:
34ba82a8
TS
10812 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10813 {
10814 as_bad (_("-32 is supported for ELF format only"));
10815 return 0;
10816 }
316f5878 10817 mips_abi = O32_ABI;
252b5132
RH
10818 break;
10819
e013f690 10820 case OPTION_N32:
34ba82a8
TS
10821 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10822 {
10823 as_bad (_("-n32 is supported for ELF format only"));
10824 return 0;
10825 }
316f5878 10826 mips_abi = N32_ABI;
e013f690 10827 break;
252b5132 10828
e013f690 10829 case OPTION_64:
34ba82a8
TS
10830 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10831 {
10832 as_bad (_("-64 is supported for ELF format only"));
10833 return 0;
10834 }
316f5878 10835 mips_abi = N64_ABI;
e013f690
TS
10836 if (! support_64bit_objects())
10837 as_fatal (_("No compiled in support for 64 bit object file format"));
252b5132 10838 break;
ae948b86 10839#endif /* OBJ_ELF */
252b5132 10840
c97ef257 10841 case OPTION_GP32:
a325df1d 10842 file_mips_gp32 = 1;
c97ef257
AH
10843 break;
10844
10845 case OPTION_GP64:
a325df1d 10846 file_mips_gp32 = 0;
c97ef257 10847 break;
252b5132 10848
ca4e0257 10849 case OPTION_FP32:
a325df1d 10850 file_mips_fp32 = 1;
316f5878
RS
10851 break;
10852
10853 case OPTION_FP64:
10854 file_mips_fp32 = 0;
ca4e0257
RS
10855 break;
10856
ae948b86 10857#ifdef OBJ_ELF
252b5132 10858 case OPTION_MABI:
34ba82a8
TS
10859 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10860 {
10861 as_bad (_("-mabi is supported for ELF format only"));
10862 return 0;
10863 }
e013f690 10864 if (strcmp (arg, "32") == 0)
316f5878 10865 mips_abi = O32_ABI;
e013f690 10866 else if (strcmp (arg, "o64") == 0)
316f5878 10867 mips_abi = O64_ABI;
e013f690 10868 else if (strcmp (arg, "n32") == 0)
316f5878 10869 mips_abi = N32_ABI;
e013f690
TS
10870 else if (strcmp (arg, "64") == 0)
10871 {
316f5878 10872 mips_abi = N64_ABI;
e013f690
TS
10873 if (! support_64bit_objects())
10874 as_fatal (_("No compiled in support for 64 bit object file "
10875 "format"));
10876 }
10877 else if (strcmp (arg, "eabi") == 0)
316f5878 10878 mips_abi = EABI_ABI;
e013f690 10879 else
da0e507f
TS
10880 {
10881 as_fatal (_("invalid abi -mabi=%s"), arg);
10882 return 0;
10883 }
252b5132 10884 break;
e013f690 10885#endif /* OBJ_ELF */
252b5132 10886
6b76fefe 10887 case OPTION_M7000_HILO_FIX:
b34976b6 10888 mips_7000_hilo_fix = TRUE;
6b76fefe
CM
10889 break;
10890
9ee72ff1 10891 case OPTION_MNO_7000_HILO_FIX:
b34976b6 10892 mips_7000_hilo_fix = FALSE;
6b76fefe
CM
10893 break;
10894
ecb4347a
DJ
10895#ifdef OBJ_ELF
10896 case OPTION_MDEBUG:
b34976b6 10897 mips_flag_mdebug = TRUE;
ecb4347a
DJ
10898 break;
10899
10900 case OPTION_NO_MDEBUG:
b34976b6 10901 mips_flag_mdebug = FALSE;
ecb4347a 10902 break;
dcd410fe
RO
10903
10904 case OPTION_PDR:
10905 mips_flag_pdr = TRUE;
10906 break;
10907
10908 case OPTION_NO_PDR:
10909 mips_flag_pdr = FALSE;
10910 break;
ecb4347a
DJ
10911#endif /* OBJ_ELF */
10912
252b5132
RH
10913 default:
10914 return 0;
10915 }
10916
10917 return 1;
10918}
316f5878
RS
10919\f
10920/* Set up globals to generate code for the ISA or processor
10921 described by INFO. */
252b5132 10922
252b5132 10923static void
17a2f251 10924mips_set_architecture (const struct mips_cpu_info *info)
252b5132 10925{
316f5878 10926 if (info != 0)
252b5132 10927 {
fef14a42
TS
10928 file_mips_arch = info->cpu;
10929 mips_opts.arch = info->cpu;
316f5878 10930 mips_opts.isa = info->isa;
252b5132 10931 }
252b5132
RH
10932}
10933
252b5132 10934
316f5878 10935/* Likewise for tuning. */
252b5132 10936
316f5878 10937static void
17a2f251 10938mips_set_tune (const struct mips_cpu_info *info)
316f5878
RS
10939{
10940 if (info != 0)
fef14a42 10941 mips_tune = info->cpu;
316f5878 10942}
80cc45a5 10943
34ba82a8 10944
252b5132 10945void
17a2f251 10946mips_after_parse_args (void)
e9670677 10947{
fef14a42
TS
10948 const struct mips_cpu_info *arch_info = 0;
10949 const struct mips_cpu_info *tune_info = 0;
10950
e9670677
MR
10951 /* GP relative stuff not working for PE */
10952 if (strncmp (TARGET_OS, "pe", 2) == 0
10953 && g_switch_value != 0)
10954 {
10955 if (g_switch_seen)
10956 as_bad (_("-G not supported in this configuration."));
10957 g_switch_value = 0;
10958 }
10959
cac012d6
AO
10960 if (mips_abi == NO_ABI)
10961 mips_abi = MIPS_DEFAULT_ABI;
10962
22923709
RS
10963 /* The following code determines the architecture and register size.
10964 Similar code was added to GCC 3.3 (see override_options() in
10965 config/mips/mips.c). The GAS and GCC code should be kept in sync
10966 as much as possible. */
e9670677 10967
316f5878 10968 if (mips_arch_string != 0)
fef14a42 10969 arch_info = mips_parse_cpu ("-march", mips_arch_string);
e9670677 10970
316f5878 10971 if (file_mips_isa != ISA_UNKNOWN)
e9670677 10972 {
316f5878 10973 /* Handle -mipsN. At this point, file_mips_isa contains the
fef14a42 10974 ISA level specified by -mipsN, while arch_info->isa contains
316f5878 10975 the -march selection (if any). */
fef14a42 10976 if (arch_info != 0)
e9670677 10977 {
316f5878
RS
10978 /* -march takes precedence over -mipsN, since it is more descriptive.
10979 There's no harm in specifying both as long as the ISA levels
10980 are the same. */
fef14a42 10981 if (file_mips_isa != arch_info->isa)
316f5878
RS
10982 as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
10983 mips_cpu_info_from_isa (file_mips_isa)->name,
fef14a42 10984 mips_cpu_info_from_isa (arch_info->isa)->name);
e9670677 10985 }
316f5878 10986 else
fef14a42 10987 arch_info = mips_cpu_info_from_isa (file_mips_isa);
e9670677
MR
10988 }
10989
fef14a42
TS
10990 if (arch_info == 0)
10991 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
e9670677 10992
fef14a42 10993 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
316f5878 10994 as_bad ("-march=%s is not compatible with the selected ABI",
fef14a42
TS
10995 arch_info->name);
10996
10997 mips_set_architecture (arch_info);
10998
10999 /* Optimize for file_mips_arch, unless -mtune selects a different processor. */
11000 if (mips_tune_string != 0)
11001 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
e9670677 11002
fef14a42
TS
11003 if (tune_info == 0)
11004 mips_set_tune (arch_info);
11005 else
11006 mips_set_tune (tune_info);
e9670677 11007
316f5878 11008 if (file_mips_gp32 >= 0)
e9670677 11009 {
316f5878
RS
11010 /* The user specified the size of the integer registers. Make sure
11011 it agrees with the ABI and ISA. */
11012 if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
11013 as_bad (_("-mgp64 used with a 32-bit processor"));
11014 else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
11015 as_bad (_("-mgp32 used with a 64-bit ABI"));
11016 else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
11017 as_bad (_("-mgp64 used with a 32-bit ABI"));
e9670677
MR
11018 }
11019 else
11020 {
316f5878
RS
11021 /* Infer the integer register size from the ABI and processor.
11022 Restrict ourselves to 32-bit registers if that's all the
11023 processor has, or if the ABI cannot handle 64-bit registers. */
11024 file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
11025 || !ISA_HAS_64BIT_REGS (mips_opts.isa));
e9670677
MR
11026 }
11027
316f5878
RS
11028 /* ??? GAS treats single-float processors as though they had 64-bit
11029 float registers (although it complains when double-precision
11030 instructions are used). As things stand, saying they have 32-bit
11031 registers would lead to spurious "register must be even" messages.
11032 So here we assume float registers are always the same size as
11033 integer ones, unless the user says otherwise. */
11034 if (file_mips_fp32 < 0)
11035 file_mips_fp32 = file_mips_gp32;
e9670677 11036
316f5878 11037 /* End of GCC-shared inference code. */
e9670677 11038
17a2f251
TS
11039 /* This flag is set when we have a 64-bit capable CPU but use only
11040 32-bit wide registers. Note that EABI does not use it. */
11041 if (ISA_HAS_64BIT_REGS (mips_opts.isa)
11042 && ((mips_abi == NO_ABI && file_mips_gp32 == 1)
11043 || mips_abi == O32_ABI))
316f5878 11044 mips_32bitmode = 1;
e9670677
MR
11045
11046 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
11047 as_bad (_("trap exception not supported at ISA 1"));
11048
e9670677
MR
11049 /* If the selected architecture includes support for ASEs, enable
11050 generation of code for them. */
a4672219 11051 if (mips_opts.mips16 == -1)
fef14a42 11052 mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_arch)) ? 1 : 0;
ffdefa66 11053 if (mips_opts.ase_mips3d == -1)
fef14a42 11054 mips_opts.ase_mips3d = (CPU_HAS_MIPS3D (file_mips_arch)) ? 1 : 0;
ffdefa66 11055 if (mips_opts.ase_mdmx == -1)
fef14a42 11056 mips_opts.ase_mdmx = (CPU_HAS_MDMX (file_mips_arch)) ? 1 : 0;
e9670677 11057
e9670677 11058 file_mips_isa = mips_opts.isa;
a4672219 11059 file_ase_mips16 = mips_opts.mips16;
e9670677
MR
11060 file_ase_mips3d = mips_opts.ase_mips3d;
11061 file_ase_mdmx = mips_opts.ase_mdmx;
11062 mips_opts.gp32 = file_mips_gp32;
11063 mips_opts.fp32 = file_mips_fp32;
11064
ecb4347a
DJ
11065 if (mips_flag_mdebug < 0)
11066 {
11067#ifdef OBJ_MAYBE_ECOFF
11068 if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
11069 mips_flag_mdebug = 1;
11070 else
11071#endif /* OBJ_MAYBE_ECOFF */
11072 mips_flag_mdebug = 0;
11073 }
e9670677
MR
11074}
11075\f
11076void
17a2f251 11077mips_init_after_args (void)
252b5132
RH
11078{
11079 /* initialize opcodes */
11080 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
beae10d5 11081 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
252b5132
RH
11082}
11083
11084long
17a2f251 11085md_pcrel_from (fixS *fixP)
252b5132 11086{
a7ebbfdf
TS
11087 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
11088 switch (fixP->fx_r_type)
11089 {
11090 case BFD_RELOC_16_PCREL_S2:
11091 case BFD_RELOC_MIPS_JMP:
11092 /* Return the address of the delay slot. */
11093 return addr + 4;
11094 default:
11095 return addr;
11096 }
252b5132
RH
11097}
11098
252b5132
RH
11099/* This is called before the symbol table is processed. In order to
11100 work with gcc when using mips-tfile, we must keep all local labels.
11101 However, in other cases, we want to discard them. If we were
11102 called with -g, but we didn't see any debugging information, it may
11103 mean that gcc is smuggling debugging information through to
11104 mips-tfile, in which case we must generate all local labels. */
11105
11106void
17a2f251 11107mips_frob_file_before_adjust (void)
252b5132
RH
11108{
11109#ifndef NO_ECOFF_DEBUGGING
11110 if (ECOFF_DEBUGGING
11111 && mips_debug != 0
11112 && ! ecoff_debugging_seen)
11113 flag_keep_locals = 1;
11114#endif
11115}
11116
11117/* Sort any unmatched HI16_S relocs so that they immediately precede
94f592af 11118 the corresponding LO reloc. This is called before md_apply_fix3 and
252b5132
RH
11119 tc_gen_reloc. Unmatched HI16_S relocs can only be generated by
11120 explicit use of the %hi modifier. */
11121
11122void
17a2f251 11123mips_frob_file (void)
252b5132
RH
11124{
11125 struct mips_hi_fixup *l;
11126
11127 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
11128 {
11129 segment_info_type *seginfo;
11130 int pass;
11131
5919d012 11132 assert (reloc_needs_lo_p (l->fixp->fx_r_type));
252b5132 11133
5919d012
RS
11134 /* If a GOT16 relocation turns out to be against a global symbol,
11135 there isn't supposed to be a matching LO. */
11136 if (l->fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
11137 && !pic_need_relax (l->fixp->fx_addsy, l->seg))
11138 continue;
11139
11140 /* Check quickly whether the next fixup happens to be a matching %lo. */
11141 if (fixup_has_matching_lo_p (l->fixp))
252b5132
RH
11142 continue;
11143
11144 /* Look through the fixups for this segment for a matching %lo.
11145 When we find one, move the %hi just in front of it. We do
11146 this in two passes. In the first pass, we try to find a
11147 unique %lo. In the second pass, we permit multiple %hi
11148 relocs for a single %lo (this is a GNU extension). */
11149 seginfo = seg_info (l->seg);
11150 for (pass = 0; pass < 2; pass++)
11151 {
11152 fixS *f, *prev;
11153
11154 prev = NULL;
11155 for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
11156 {
11157 /* Check whether this is a %lo fixup which matches l->fixp. */
11158 if (f->fx_r_type == BFD_RELOC_LO16
11159 && f->fx_addsy == l->fixp->fx_addsy
11160 && f->fx_offset == l->fixp->fx_offset
11161 && (pass == 1
11162 || prev == NULL
5919d012
RS
11163 || !reloc_needs_lo_p (prev->fx_r_type)
11164 || !fixup_has_matching_lo_p (prev)))
252b5132
RH
11165 {
11166 fixS **pf;
11167
11168 /* Move l->fixp before f. */
11169 for (pf = &seginfo->fix_root;
11170 *pf != l->fixp;
11171 pf = &(*pf)->fx_next)
11172 assert (*pf != NULL);
11173
11174 *pf = l->fixp->fx_next;
11175
11176 l->fixp->fx_next = f;
11177 if (prev == NULL)
11178 seginfo->fix_root = l->fixp;
11179 else
11180 prev->fx_next = l->fixp;
11181
11182 break;
11183 }
11184
11185 prev = f;
11186 }
11187
11188 if (f != NULL)
11189 break;
11190
11191#if 0 /* GCC code motion plus incomplete dead code elimination
11192 can leave a %hi without a %lo. */
11193 if (pass == 1)
11194 as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
11195 _("Unmatched %%hi reloc"));
11196#endif
11197 }
11198 }
11199}
11200
11201/* When generating embedded PIC code we need to use a special
11202 relocation to represent the difference of two symbols in the .text
11203 section (switch tables use a difference of this sort). See
11204 include/coff/mips.h for details. This macro checks whether this
11205 fixup requires the special reloc. */
11206#define SWITCH_TABLE(fixp) \
11207 ((fixp)->fx_r_type == BFD_RELOC_32 \
bb2d6cd7 11208 && OUTPUT_FLAVOR != bfd_target_elf_flavour \
252b5132
RH
11209 && (fixp)->fx_addsy != NULL \
11210 && (fixp)->fx_subsy != NULL \
11211 && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
11212 && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
11213
11214/* When generating embedded PIC code we must keep all PC relative
11215 relocations, in case the linker has to relax a call. We also need
f6688943
TS
11216 to keep relocations for switch table entries.
11217
11218 We may have combined relocations without symbols in the N32/N64 ABI.
11219 We have to prevent gas from dropping them. */
252b5132 11220
252b5132 11221int
17a2f251 11222mips_force_relocation (fixS *fixp)
252b5132 11223{
ae6063d4 11224 if (generic_force_reloc (fixp))
252b5132
RH
11225 return 1;
11226
f6688943
TS
11227 if (HAVE_NEWABI
11228 && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
11229 && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
11230 || fixp->fx_r_type == BFD_RELOC_HI16_S
11231 || fixp->fx_r_type == BFD_RELOC_LO16))
11232 return 1;
11233
252b5132
RH
11234 return (mips_pic == EMBEDDED_PIC
11235 && (fixp->fx_pcrel
11236 || SWITCH_TABLE (fixp)
11237 || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
11238 || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
11239}
11240
45f8dfe8
AO
11241/* This hook is called before a fix is simplified. We don't really
11242 decide whether to skip a fix here. Rather, we turn global symbols
11243 used as branch targets into local symbols, such that they undergo
11244 simplification. We can only do this if the symbol is defined and
11245 it is in the same section as the branch. If this doesn't hold, we
11246 emit a better error message than just saying the relocation is not
11247 valid for the selected object format.
11248
11249 FIXP is the fix-up we're going to try to simplify, SEG is the
11250 segment in which the fix up occurs. The return value should be
11251 non-zero to indicate the fix-up is valid for further
11252 simplifications. */
11253
11254int
17a2f251 11255mips_validate_fix (struct fix *fixP, asection *seg)
45f8dfe8
AO
11256{
11257 /* There's a lot of discussion on whether it should be possible to
11258 use R_MIPS_PC16 to represent branch relocations. The outcome
11259 seems to be that it can, but gas/bfd are very broken in creating
11260 RELA relocations for this, so for now we only accept branches to
11261 symbols in the same section. Anything else is of dubious value,
11262 since there's no guarantee that at link time the symbol would be
11263 in range. Even for branches to local symbols this is arguably
11264 wrong, since it we assume the symbol is not going to be
11265 overridden, which should be possible per ELF library semantics,
11266 but then, there isn't a dynamic relocation that could be used to
11267 this effect, and the target would likely be out of range as well.
11268
11269 Unfortunately, it seems that there is too much code out there
11270 that relies on branches to symbols that are global to be resolved
11271 as if they were local, like the IRIX tools do, so we do it as
11272 well, but with a warning so that people are reminded to fix their
11273 code. If we ever get back to using R_MIPS_PC16 for branch
11274 targets, this entire block should go away (and probably the
11275 whole function). */
11276
11277 if (fixP->fx_r_type == BFD_RELOC_16_PCREL_S2
11278 && (((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
11279 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
11280 && mips_pic != EMBEDDED_PIC)
11281 || bfd_reloc_type_lookup (stdoutput, BFD_RELOC_16_PCREL_S2) == NULL)
11282 && fixP->fx_addsy)
11283 {
11284 if (! S_IS_DEFINED (fixP->fx_addsy))
11285 {
11286 as_bad_where (fixP->fx_file, fixP->fx_line,
11287 _("Cannot branch to undefined symbol."));
11288 /* Avoid any further errors about this fixup. */
11289 fixP->fx_done = 1;
11290 }
11291 else if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
11292 {
11293 as_bad_where (fixP->fx_file, fixP->fx_line,
11294 _("Cannot branch to symbol in another section."));
11295 fixP->fx_done = 1;
11296 }
11297 else if (S_IS_EXTERNAL (fixP->fx_addsy))
11298 {
11299 symbolS *sym = fixP->fx_addsy;
11300
115695a8
CD
11301 if (mips_pic == SVR4_PIC)
11302 as_warn_where (fixP->fx_file, fixP->fx_line,
11303 _("Pretending global symbol used as branch target is local."));
45f8dfe8
AO
11304
11305 fixP->fx_addsy = symbol_create (S_GET_NAME (sym),
11306 S_GET_SEGMENT (sym),
11307 S_GET_VALUE (sym),
11308 symbol_get_frag (sym));
11309 copy_symbol_attributes (fixP->fx_addsy, sym);
11310 S_CLEAR_EXTERNAL (fixP->fx_addsy);
11311 assert (symbol_resolved_p (sym));
11312 symbol_mark_resolved (fixP->fx_addsy);
11313 }
11314 }
11315
11316 return 1;
11317}
11318
add55e1f
RS
11319#ifdef OBJ_ELF
11320static int
17a2f251 11321mips_need_elf_addend_fixup (fixS *fixP)
add55e1f 11322{
2d2bf3e0
CD
11323 if (S_GET_OTHER (fixP->fx_addsy) == STO_MIPS16)
11324 return 1;
b25a253c
CD
11325 if (mips_pic == EMBEDDED_PIC
11326 && S_IS_WEAK (fixP->fx_addsy))
11327 return 1;
11328 if (mips_pic != EMBEDDED_PIC
11329 && (S_IS_WEAK (fixP->fx_addsy)
bad9ca53 11330 || S_IS_EXTERNAL (fixP->fx_addsy))
2d2bf3e0
CD
11331 && !S_IS_COMMON (fixP->fx_addsy))
11332 return 1;
9204e615
DJ
11333 if (((bfd_get_section_flags (stdoutput,
11334 S_GET_SEGMENT (fixP->fx_addsy))
11335 & (SEC_LINK_ONCE | SEC_MERGE)) != 0)
11336 || !strncmp (segment_name (S_GET_SEGMENT (fixP->fx_addsy)),
11337 ".gnu.linkonce",
11338 sizeof (".gnu.linkonce") - 1))
2d2bf3e0
CD
11339 return 1;
11340 return 0;
add55e1f
RS
11341}
11342#endif
11343
252b5132
RH
11344/* Apply a fixup to the object file. */
11345
94f592af 11346void
17a2f251 11347md_apply_fix3 (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 11348{
874e8986 11349 bfd_byte *buf;
98aa84af 11350 long insn;
ed6fb7bd 11351 static int previous_fx_r_type = 0;
a7ebbfdf 11352 reloc_howto_type *howto;
252b5132 11353
a7ebbfdf
TS
11354 /* We ignore generic BFD relocations we don't know about. */
11355 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
11356 if (! howto)
11357 return;
65551fa4 11358
252b5132
RH
11359 assert (fixP->fx_size == 4
11360 || fixP->fx_r_type == BFD_RELOC_16
11361 || fixP->fx_r_type == BFD_RELOC_64
f6688943
TS
11362 || fixP->fx_r_type == BFD_RELOC_CTOR
11363 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
252b5132 11364 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
a7ebbfdf 11365 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY);
252b5132 11366
a7ebbfdf 11367 buf = (bfd_byte *) (fixP->fx_frag->fr_literal + fixP->fx_where);
252b5132
RH
11368
11369 /* If we aren't adjusting this fixup to be against the section
11370 symbol, we need to adjust the value. */
11371#ifdef OBJ_ELF
11372 if (fixP->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour)
bb2d6cd7 11373 {
a7ebbfdf
TS
11374 if (mips_need_elf_addend_fixup (fixP)
11375 && howto->partial_inplace
11376 && fixP->fx_r_type != BFD_RELOC_GPREL16
11377 && fixP->fx_r_type != BFD_RELOC_GPREL32
11378 && fixP->fx_r_type != BFD_RELOC_MIPS16_GPREL)
11379 {
11380 /* In this case, the bfd_install_relocation routine will
11381 incorrectly add the symbol value back in. We just want
11382 the addend to appear in the object file.
11383
11384 The condition above used to include
11385 "&& (! fixP->fx_pcrel || howto->pcrel_offset)".
11386
11387 However, howto can't be trusted here, because we
11388 might change the reloc type in tc_gen_reloc. We can
11389 check howto->partial_inplace because that conversion
11390 happens to preserve howto->partial_inplace; but it
11391 does not preserve howto->pcrel_offset. I've just
11392 eliminated the check, because all MIPS PC-relative
11393 relocations are marked howto->pcrel_offset.
11394
11395 howto->pcrel_offset was originally added for
11396 R_MIPS_PC16, which is generated for code like
11397
11398 globl g1 .text
11399 .text
11400 .space 20
11401 g1:
11402 x:
11403 bal g1
11404 */
11405 *valP -= S_GET_VALUE (fixP->fx_addsy);
98aa84af 11406 }
252b5132 11407
bb2d6cd7
GK
11408 /* This code was generated using trial and error and so is
11409 fragile and not trustworthy. If you change it, you should
11410 rerun the elf-rel, elf-rel2, and empic testcases and ensure
11411 they still pass. */
a7ebbfdf 11412 if (fixP->fx_pcrel)
bb2d6cd7 11413 {
a7ebbfdf 11414 *valP += fixP->fx_frag->fr_address + fixP->fx_where;
bb2d6cd7
GK
11415
11416 /* BFD's REL handling, for MIPS, is _very_ weird.
11417 This gives the right results, but it can't possibly
11418 be the way things are supposed to work. */
a7ebbfdf 11419 *valP += fixP->fx_frag->fr_address + fixP->fx_where;
bb2d6cd7
GK
11420 }
11421 }
11422#endif
252b5132 11423
ed6fb7bd
SC
11424 /* We are not done if this is a composite relocation to set up gp. */
11425 if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel
11426 && !(fixP->fx_r_type == BFD_RELOC_MIPS_SUB
10181a0d
AO
11427 || (fixP->fx_r_type == BFD_RELOC_64
11428 && (previous_fx_r_type == BFD_RELOC_GPREL32
11429 || previous_fx_r_type == BFD_RELOC_GPREL16))
ed6fb7bd
SC
11430 || (previous_fx_r_type == BFD_RELOC_MIPS_SUB
11431 && (fixP->fx_r_type == BFD_RELOC_HI16_S
11432 || fixP->fx_r_type == BFD_RELOC_LO16))))
252b5132 11433 fixP->fx_done = 1;
ed6fb7bd 11434 previous_fx_r_type = fixP->fx_r_type;
252b5132
RH
11435
11436 switch (fixP->fx_r_type)
11437 {
11438 case BFD_RELOC_MIPS_JMP:
e369bcce
TS
11439 case BFD_RELOC_MIPS_SHIFT5:
11440 case BFD_RELOC_MIPS_SHIFT6:
11441 case BFD_RELOC_MIPS_GOT_DISP:
11442 case BFD_RELOC_MIPS_GOT_PAGE:
11443 case BFD_RELOC_MIPS_GOT_OFST:
11444 case BFD_RELOC_MIPS_SUB:
11445 case BFD_RELOC_MIPS_INSERT_A:
11446 case BFD_RELOC_MIPS_INSERT_B:
11447 case BFD_RELOC_MIPS_DELETE:
11448 case BFD_RELOC_MIPS_HIGHEST:
11449 case BFD_RELOC_MIPS_HIGHER:
11450 case BFD_RELOC_MIPS_SCN_DISP:
11451 case BFD_RELOC_MIPS_REL16:
11452 case BFD_RELOC_MIPS_RELGOT:
11453 case BFD_RELOC_MIPS_JALR:
252b5132
RH
11454 case BFD_RELOC_HI16:
11455 case BFD_RELOC_HI16_S:
cdf6fd85 11456 case BFD_RELOC_GPREL16:
252b5132
RH
11457 case BFD_RELOC_MIPS_LITERAL:
11458 case BFD_RELOC_MIPS_CALL16:
11459 case BFD_RELOC_MIPS_GOT16:
cdf6fd85 11460 case BFD_RELOC_GPREL32:
252b5132
RH
11461 case BFD_RELOC_MIPS_GOT_HI16:
11462 case BFD_RELOC_MIPS_GOT_LO16:
11463 case BFD_RELOC_MIPS_CALL_HI16:
11464 case BFD_RELOC_MIPS_CALL_LO16:
11465 case BFD_RELOC_MIPS16_GPREL:
11466 if (fixP->fx_pcrel)
11467 as_bad_where (fixP->fx_file, fixP->fx_line,
11468 _("Invalid PC relative reloc"));
11469 /* Nothing needed to do. The value comes from the reloc entry */
11470 break;
11471
11472 case BFD_RELOC_MIPS16_JMP:
11473 /* We currently always generate a reloc against a symbol, which
11474 means that we don't want an addend even if the symbol is
11475 defined. */
a7ebbfdf 11476 *valP = 0;
252b5132
RH
11477 break;
11478
11479 case BFD_RELOC_PCREL_HI16_S:
11480 /* The addend for this is tricky if it is internal, so we just
11481 do everything here rather than in bfd_install_relocation. */
a7ebbfdf 11482 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && !fixP->fx_done)
bb2d6cd7
GK
11483 break;
11484 if (fixP->fx_addsy
11485 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
252b5132
RH
11486 {
11487 /* For an external symbol adjust by the address to make it
11488 pcrel_offset. We use the address of the RELLO reloc
11489 which follows this one. */
a7ebbfdf 11490 *valP += (fixP->fx_next->fx_frag->fr_address
252b5132
RH
11491 + fixP->fx_next->fx_where);
11492 }
a7ebbfdf 11493 *valP = ((*valP + 0x8000) >> 16) & 0xffff;
252b5132
RH
11494 if (target_big_endian)
11495 buf += 2;
17a2f251 11496 md_number_to_chars (buf, *valP, 2);
252b5132
RH
11497 break;
11498
11499 case BFD_RELOC_PCREL_LO16:
11500 /* The addend for this is tricky if it is internal, so we just
11501 do everything here rather than in bfd_install_relocation. */
a7ebbfdf 11502 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && !fixP->fx_done)
bb2d6cd7
GK
11503 break;
11504 if (fixP->fx_addsy
11505 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
a7ebbfdf 11506 *valP += fixP->fx_frag->fr_address + fixP->fx_where;
252b5132
RH
11507 if (target_big_endian)
11508 buf += 2;
17a2f251 11509 md_number_to_chars (buf, *valP, 2);
252b5132
RH
11510 break;
11511
11512 case BFD_RELOC_64:
11513 /* This is handled like BFD_RELOC_32, but we output a sign
11514 extended value if we are only 32 bits. */
11515 if (fixP->fx_done
11516 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11517 {
11518 if (8 <= sizeof (valueT))
a7ebbfdf 11519 md_number_to_chars (buf, *valP, 8);
252b5132
RH
11520 else
11521 {
a7ebbfdf 11522 valueT hiv;
252b5132 11523
a7ebbfdf 11524 if ((*valP & 0x80000000) != 0)
252b5132
RH
11525 hiv = 0xffffffff;
11526 else
11527 hiv = 0;
a7ebbfdf
TS
11528 md_number_to_chars ((char *)(buf + target_big_endian ? 4 : 0),
11529 *valP, 4);
11530 md_number_to_chars ((char *)(buf + target_big_endian ? 0 : 4),
11531 hiv, 4);
252b5132
RH
11532 }
11533 }
11534 break;
11535
056350c6 11536 case BFD_RELOC_RVA:
252b5132
RH
11537 case BFD_RELOC_32:
11538 /* If we are deleting this reloc entry, we must fill in the
11539 value now. This can happen if we have a .word which is not
11540 resolved when it appears but is later defined. We also need
11541 to fill in the value if this is an embedded PIC switch table
11542 entry. */
11543 if (fixP->fx_done
11544 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
a7ebbfdf 11545 md_number_to_chars (buf, *valP, 4);
252b5132
RH
11546 break;
11547
11548 case BFD_RELOC_16:
11549 /* If we are deleting this reloc entry, we must fill in the
11550 value now. */
11551 assert (fixP->fx_size == 2);
11552 if (fixP->fx_done)
a7ebbfdf 11553 md_number_to_chars (buf, *valP, 2);
252b5132
RH
11554 break;
11555
11556 case BFD_RELOC_LO16:
11557 /* When handling an embedded PIC switch statement, we can wind
11558 up deleting a LO16 reloc. See the 'o' case in mips_ip. */
11559 if (fixP->fx_done)
11560 {
a7ebbfdf 11561 if (*valP + 0x8000 > 0xffff)
252b5132
RH
11562 as_bad_where (fixP->fx_file, fixP->fx_line,
11563 _("relocation overflow"));
252b5132
RH
11564 if (target_big_endian)
11565 buf += 2;
17a2f251 11566 md_number_to_chars (buf, *valP, 2);
252b5132
RH
11567 }
11568 break;
11569
11570 case BFD_RELOC_16_PCREL_S2:
a7ebbfdf 11571 if ((*valP & 0x3) != 0)
cb56d3d3 11572 as_bad_where (fixP->fx_file, fixP->fx_line,
a7ebbfdf 11573 _("Branch to odd address (%lx)"), (long) *valP);
cb56d3d3 11574
252b5132
RH
11575 /*
11576 * We need to save the bits in the instruction since fixup_segment()
11577 * might be deleting the relocation entry (i.e., a branch within
11578 * the current segment).
11579 */
a7ebbfdf 11580 if (! fixP->fx_done)
bb2d6cd7 11581 break;
252b5132
RH
11582
11583 /* update old instruction data */
252b5132
RH
11584 if (target_big_endian)
11585 insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
11586 else
11587 insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
11588
a7ebbfdf
TS
11589 if (*valP + 0x20000 <= 0x3ffff)
11590 {
11591 insn |= (*valP >> 2) & 0xffff;
17a2f251 11592 md_number_to_chars (buf, insn, 4);
a7ebbfdf
TS
11593 }
11594 else if (mips_pic == NO_PIC
11595 && fixP->fx_done
11596 && fixP->fx_frag->fr_address >= text_section->vma
11597 && (fixP->fx_frag->fr_address
11598 < text_section->vma + text_section->_raw_size)
11599 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
11600 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
11601 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
252b5132
RH
11602 {
11603 /* The branch offset is too large. If this is an
11604 unconditional branch, and we are not generating PIC code,
11605 we can convert it to an absolute jump instruction. */
a7ebbfdf
TS
11606 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
11607 insn = 0x0c000000; /* jal */
252b5132 11608 else
a7ebbfdf
TS
11609 insn = 0x08000000; /* j */
11610 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
11611 fixP->fx_done = 0;
11612 fixP->fx_addsy = section_symbol (text_section);
11613 *valP += md_pcrel_from (fixP);
17a2f251 11614 md_number_to_chars (buf, insn, 4);
a7ebbfdf
TS
11615 }
11616 else
11617 {
11618 /* If we got here, we have branch-relaxation disabled,
11619 and there's nothing we can do to fix this instruction
11620 without turning it into a longer sequence. */
11621 as_bad_where (fixP->fx_file, fixP->fx_line,
11622 _("Branch out of range"));
252b5132 11623 }
252b5132
RH
11624 break;
11625
11626 case BFD_RELOC_VTABLE_INHERIT:
11627 fixP->fx_done = 0;
11628 if (fixP->fx_addsy
11629 && !S_IS_DEFINED (fixP->fx_addsy)
11630 && !S_IS_WEAK (fixP->fx_addsy))
11631 S_SET_WEAK (fixP->fx_addsy);
11632 break;
11633
11634 case BFD_RELOC_VTABLE_ENTRY:
11635 fixP->fx_done = 0;
11636 break;
11637
11638 default:
11639 internalError ();
11640 }
a7ebbfdf
TS
11641
11642 /* Remember value for tc_gen_reloc. */
11643 fixP->fx_addnumber = *valP;
252b5132
RH
11644}
11645
11646#if 0
11647void
17a2f251 11648printInsn (unsigned long oc)
252b5132
RH
11649{
11650 const struct mips_opcode *p;
11651 int treg, sreg, dreg, shamt;
11652 short imm;
11653 const char *args;
11654 int i;
11655
11656 for (i = 0; i < NUMOPCODES; ++i)
11657 {
11658 p = &mips_opcodes[i];
11659 if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
11660 {
11661 printf ("%08lx %s\t", oc, p->name);
11662 treg = (oc >> 16) & 0x1f;
11663 sreg = (oc >> 21) & 0x1f;
11664 dreg = (oc >> 11) & 0x1f;
11665 shamt = (oc >> 6) & 0x1f;
11666 imm = oc;
11667 for (args = p->args;; ++args)
11668 {
11669 switch (*args)
11670 {
11671 case '\0':
11672 printf ("\n");
11673 break;
11674
11675 case ',':
11676 case '(':
11677 case ')':
11678 printf ("%c", *args);
11679 continue;
11680
11681 case 'r':
11682 assert (treg == sreg);
11683 printf ("$%d,$%d", treg, sreg);
11684 continue;
11685
11686 case 'd':
11687 case 'G':
11688 printf ("$%d", dreg);
11689 continue;
11690
11691 case 't':
11692 case 'E':
11693 printf ("$%d", treg);
11694 continue;
11695
11696 case 'k':
11697 printf ("0x%x", treg);
11698 continue;
11699
11700 case 'b':
11701 case 's':
11702 printf ("$%d", sreg);
11703 continue;
11704
11705 case 'a':
11706 printf ("0x%08lx", oc & 0x1ffffff);
11707 continue;
11708
11709 case 'i':
11710 case 'j':
11711 case 'o':
11712 case 'u':
11713 printf ("%d", imm);
11714 continue;
11715
11716 case '<':
11717 case '>':
11718 printf ("$%d", shamt);
11719 continue;
11720
11721 default:
11722 internalError ();
11723 }
11724 break;
11725 }
11726 return;
11727 }
11728 }
11729 printf (_("%08lx UNDEFINED\n"), oc);
11730}
11731#endif
11732
11733static symbolS *
17a2f251 11734get_symbol (void)
252b5132
RH
11735{
11736 int c;
11737 char *name;
11738 symbolS *p;
11739
11740 name = input_line_pointer;
11741 c = get_symbol_end ();
11742 p = (symbolS *) symbol_find_or_make (name);
11743 *input_line_pointer = c;
11744 return p;
11745}
11746
11747/* Align the current frag to a given power of two. The MIPS assembler
11748 also automatically adjusts any preceding label. */
11749
11750static void
17a2f251 11751mips_align (int to, int fill, symbolS *label)
252b5132 11752{
b34976b6 11753 mips_emit_delays (FALSE);
252b5132
RH
11754 frag_align (to, fill, 0);
11755 record_alignment (now_seg, to);
11756 if (label != NULL)
11757 {
11758 assert (S_GET_SEGMENT (label) == now_seg);
49309057 11759 symbol_set_frag (label, frag_now);
252b5132
RH
11760 S_SET_VALUE (label, (valueT) frag_now_fix ());
11761 }
11762}
11763
11764/* Align to a given power of two. .align 0 turns off the automatic
11765 alignment used by the data creating pseudo-ops. */
11766
11767static void
17a2f251 11768s_align (int x ATTRIBUTE_UNUSED)
252b5132
RH
11769{
11770 register int temp;
11771 register long temp_fill;
11772 long max_alignment = 15;
11773
11774 /*
11775
11776 o Note that the assembler pulls down any immediately preceeding label
11777 to the aligned address.
11778 o It's not documented but auto alignment is reinstated by
11779 a .align pseudo instruction.
11780 o Note also that after auto alignment is turned off the mips assembler
11781 issues an error on attempt to assemble an improperly aligned data item.
11782 We don't.
11783
11784 */
11785
11786 temp = get_absolute_expression ();
11787 if (temp > max_alignment)
11788 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
11789 else if (temp < 0)
11790 {
11791 as_warn (_("Alignment negative: 0 assumed."));
11792 temp = 0;
11793 }
11794 if (*input_line_pointer == ',')
11795 {
f9419b05 11796 ++input_line_pointer;
252b5132
RH
11797 temp_fill = get_absolute_expression ();
11798 }
11799 else
11800 temp_fill = 0;
11801 if (temp)
11802 {
11803 auto_align = 1;
11804 mips_align (temp, (int) temp_fill,
11805 insn_labels != NULL ? insn_labels->label : NULL);
11806 }
11807 else
11808 {
11809 auto_align = 0;
11810 }
11811
11812 demand_empty_rest_of_line ();
11813}
11814
11815void
17a2f251 11816mips_flush_pending_output (void)
252b5132 11817{
b34976b6 11818 mips_emit_delays (FALSE);
252b5132
RH
11819 mips_clear_insn_labels ();
11820}
11821
11822static void
17a2f251 11823s_change_sec (int sec)
252b5132
RH
11824{
11825 segT seg;
11826
11827 /* When generating embedded PIC code, we only use the .text, .lit8,
11828 .sdata and .sbss sections. We change the .data and .rdata
11829 pseudo-ops to use .sdata. */
11830 if (mips_pic == EMBEDDED_PIC
11831 && (sec == 'd' || sec == 'r'))
11832 sec = 's';
11833
11834#ifdef OBJ_ELF
11835 /* The ELF backend needs to know that we are changing sections, so
11836 that .previous works correctly. We could do something like check
b6ff326e 11837 for an obj_section_change_hook macro, but that might be confusing
252b5132
RH
11838 as it would not be appropriate to use it in the section changing
11839 functions in read.c, since obj-elf.c intercepts those. FIXME:
11840 This should be cleaner, somehow. */
11841 obj_elf_section_change_hook ();
11842#endif
11843
b34976b6 11844 mips_emit_delays (FALSE);
252b5132
RH
11845 switch (sec)
11846 {
11847 case 't':
11848 s_text (0);
11849 break;
11850 case 'd':
11851 s_data (0);
11852 break;
11853 case 'b':
11854 subseg_set (bss_section, (subsegT) get_absolute_expression ());
11855 demand_empty_rest_of_line ();
11856 break;
11857
11858 case 'r':
11859 if (USE_GLOBAL_POINTER_OPT)
11860 {
11861 seg = subseg_new (RDATA_SECTION_NAME,
11862 (subsegT) get_absolute_expression ());
11863 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11864 {
11865 bfd_set_section_flags (stdoutput, seg,
11866 (SEC_ALLOC
11867 | SEC_LOAD
11868 | SEC_READONLY
11869 | SEC_RELOC
11870 | SEC_DATA));
11871 if (strcmp (TARGET_OS, "elf") != 0)
e799a695 11872 record_alignment (seg, 4);
252b5132
RH
11873 }
11874 demand_empty_rest_of_line ();
11875 }
11876 else
11877 {
11878 as_bad (_("No read only data section in this object file format"));
11879 demand_empty_rest_of_line ();
11880 return;
11881 }
11882 break;
11883
11884 case 's':
11885 if (USE_GLOBAL_POINTER_OPT)
11886 {
11887 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
11888 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11889 {
11890 bfd_set_section_flags (stdoutput, seg,
11891 SEC_ALLOC | SEC_LOAD | SEC_RELOC
11892 | SEC_DATA);
11893 if (strcmp (TARGET_OS, "elf") != 0)
e799a695 11894 record_alignment (seg, 4);
252b5132
RH
11895 }
11896 demand_empty_rest_of_line ();
11897 break;
11898 }
11899 else
11900 {
11901 as_bad (_("Global pointers not supported; recompile -G 0"));
11902 demand_empty_rest_of_line ();
11903 return;
11904 }
11905 }
11906
11907 auto_align = 1;
11908}
b34976b6 11909
cca86cc8 11910void
17a2f251 11911s_change_section (int ignore ATTRIBUTE_UNUSED)
cca86cc8 11912{
7ed4a06a 11913#ifdef OBJ_ELF
cca86cc8
SC
11914 char *section_name;
11915 char c;
684022ea 11916 char next_c = 0;
cca86cc8
SC
11917 int section_type;
11918 int section_flag;
11919 int section_entry_size;
11920 int section_alignment;
b34976b6 11921
7ed4a06a
TS
11922 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11923 return;
11924
cca86cc8
SC
11925 section_name = input_line_pointer;
11926 c = get_symbol_end ();
a816d1ed
AO
11927 if (c)
11928 next_c = *(input_line_pointer + 1);
cca86cc8 11929
4cf0dd0d
TS
11930 /* Do we have .section Name<,"flags">? */
11931 if (c != ',' || (c == ',' && next_c == '"'))
cca86cc8 11932 {
4cf0dd0d
TS
11933 /* just after name is now '\0'. */
11934 *input_line_pointer = c;
cca86cc8
SC
11935 input_line_pointer = section_name;
11936 obj_elf_section (ignore);
11937 return;
11938 }
11939 input_line_pointer++;
11940
11941 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
11942 if (c == ',')
11943 section_type = get_absolute_expression ();
11944 else
11945 section_type = 0;
11946 if (*input_line_pointer++ == ',')
11947 section_flag = get_absolute_expression ();
11948 else
11949 section_flag = 0;
11950 if (*input_line_pointer++ == ',')
11951 section_entry_size = get_absolute_expression ();
11952 else
11953 section_entry_size = 0;
11954 if (*input_line_pointer++ == ',')
11955 section_alignment = get_absolute_expression ();
11956 else
11957 section_alignment = 0;
11958
a816d1ed
AO
11959 section_name = xstrdup (section_name);
11960
cca86cc8
SC
11961 obj_elf_change_section (section_name, section_type, section_flag,
11962 section_entry_size, 0, 0, 0);
a816d1ed
AO
11963
11964 if (now_seg->name != section_name)
11965 free (section_name);
7ed4a06a 11966#endif /* OBJ_ELF */
cca86cc8 11967}
252b5132
RH
11968
11969void
17a2f251 11970mips_enable_auto_align (void)
252b5132
RH
11971{
11972 auto_align = 1;
11973}
11974
11975static void
17a2f251 11976s_cons (int log_size)
252b5132
RH
11977{
11978 symbolS *label;
11979
11980 label = insn_labels != NULL ? insn_labels->label : NULL;
b34976b6 11981 mips_emit_delays (FALSE);
252b5132
RH
11982 if (log_size > 0 && auto_align)
11983 mips_align (log_size, 0, label);
11984 mips_clear_insn_labels ();
11985 cons (1 << log_size);
11986}
11987
11988static void
17a2f251 11989s_float_cons (int type)
252b5132
RH
11990{
11991 symbolS *label;
11992
11993 label = insn_labels != NULL ? insn_labels->label : NULL;
11994
b34976b6 11995 mips_emit_delays (FALSE);
252b5132
RH
11996
11997 if (auto_align)
49309057
ILT
11998 {
11999 if (type == 'd')
12000 mips_align (3, 0, label);
12001 else
12002 mips_align (2, 0, label);
12003 }
252b5132
RH
12004
12005 mips_clear_insn_labels ();
12006
12007 float_cons (type);
12008}
12009
12010/* Handle .globl. We need to override it because on Irix 5 you are
12011 permitted to say
12012 .globl foo .text
12013 where foo is an undefined symbol, to mean that foo should be
12014 considered to be the address of a function. */
12015
12016static void
17a2f251 12017s_mips_globl (int x ATTRIBUTE_UNUSED)
252b5132
RH
12018{
12019 char *name;
12020 int c;
12021 symbolS *symbolP;
12022 flagword flag;
12023
12024 name = input_line_pointer;
12025 c = get_symbol_end ();
12026 symbolP = symbol_find_or_make (name);
12027 *input_line_pointer = c;
12028 SKIP_WHITESPACE ();
12029
12030 /* On Irix 5, every global symbol that is not explicitly labelled as
12031 being a function is apparently labelled as being an object. */
12032 flag = BSF_OBJECT;
12033
12034 if (! is_end_of_line[(unsigned char) *input_line_pointer])
12035 {
12036 char *secname;
12037 asection *sec;
12038
12039 secname = input_line_pointer;
12040 c = get_symbol_end ();
12041 sec = bfd_get_section_by_name (stdoutput, secname);
12042 if (sec == NULL)
12043 as_bad (_("%s: no such section"), secname);
12044 *input_line_pointer = c;
12045
12046 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
12047 flag = BSF_FUNCTION;
12048 }
12049
49309057 12050 symbol_get_bfdsym (symbolP)->flags |= flag;
252b5132
RH
12051
12052 S_SET_EXTERNAL (symbolP);
12053 demand_empty_rest_of_line ();
12054}
12055
12056static void
17a2f251 12057s_option (int x ATTRIBUTE_UNUSED)
252b5132
RH
12058{
12059 char *opt;
12060 char c;
12061
12062 opt = input_line_pointer;
12063 c = get_symbol_end ();
12064
12065 if (*opt == 'O')
12066 {
12067 /* FIXME: What does this mean? */
12068 }
12069 else if (strncmp (opt, "pic", 3) == 0)
12070 {
12071 int i;
12072
12073 i = atoi (opt + 3);
12074 if (i == 0)
12075 mips_pic = NO_PIC;
12076 else if (i == 2)
143d77c5 12077 {
252b5132 12078 mips_pic = SVR4_PIC;
143d77c5
EC
12079 mips_abicalls = TRUE;
12080 }
252b5132
RH
12081 else
12082 as_bad (_(".option pic%d not supported"), i);
12083
12084 if (USE_GLOBAL_POINTER_OPT && mips_pic == SVR4_PIC)
12085 {
12086 if (g_switch_seen && g_switch_value != 0)
12087 as_warn (_("-G may not be used with SVR4 PIC code"));
12088 g_switch_value = 0;
12089 bfd_set_gp_size (stdoutput, 0);
12090 }
12091 }
12092 else
12093 as_warn (_("Unrecognized option \"%s\""), opt);
12094
12095 *input_line_pointer = c;
12096 demand_empty_rest_of_line ();
12097}
12098
12099/* This structure is used to hold a stack of .set values. */
12100
e972090a
NC
12101struct mips_option_stack
12102{
252b5132
RH
12103 struct mips_option_stack *next;
12104 struct mips_set_options options;
12105};
12106
12107static struct mips_option_stack *mips_opts_stack;
12108
12109/* Handle the .set pseudo-op. */
12110
12111static void
17a2f251 12112s_mipsset (int x ATTRIBUTE_UNUSED)
252b5132
RH
12113{
12114 char *name = input_line_pointer, ch;
12115
12116 while (!is_end_of_line[(unsigned char) *input_line_pointer])
f9419b05 12117 ++input_line_pointer;
252b5132
RH
12118 ch = *input_line_pointer;
12119 *input_line_pointer = '\0';
12120
12121 if (strcmp (name, "reorder") == 0)
12122 {
12123 if (mips_opts.noreorder && prev_nop_frag != NULL)
12124 {
12125 /* If we still have pending nops, we can discard them. The
12126 usual nop handling will insert any that are still
bdaaa2e1 12127 needed. */
252b5132
RH
12128 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
12129 * (mips_opts.mips16 ? 2 : 4));
12130 prev_nop_frag = NULL;
12131 }
12132 mips_opts.noreorder = 0;
12133 }
12134 else if (strcmp (name, "noreorder") == 0)
12135 {
b34976b6 12136 mips_emit_delays (TRUE);
252b5132
RH
12137 mips_opts.noreorder = 1;
12138 mips_any_noreorder = 1;
12139 }
12140 else if (strcmp (name, "at") == 0)
12141 {
12142 mips_opts.noat = 0;
12143 }
12144 else if (strcmp (name, "noat") == 0)
12145 {
12146 mips_opts.noat = 1;
12147 }
12148 else if (strcmp (name, "macro") == 0)
12149 {
12150 mips_opts.warn_about_macros = 0;
12151 }
12152 else if (strcmp (name, "nomacro") == 0)
12153 {
12154 if (mips_opts.noreorder == 0)
12155 as_bad (_("`noreorder' must be set before `nomacro'"));
12156 mips_opts.warn_about_macros = 1;
12157 }
12158 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
12159 {
12160 mips_opts.nomove = 0;
12161 }
12162 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
12163 {
12164 mips_opts.nomove = 1;
12165 }
12166 else if (strcmp (name, "bopt") == 0)
12167 {
12168 mips_opts.nobopt = 0;
12169 }
12170 else if (strcmp (name, "nobopt") == 0)
12171 {
12172 mips_opts.nobopt = 1;
12173 }
12174 else if (strcmp (name, "mips16") == 0
12175 || strcmp (name, "MIPS-16") == 0)
12176 mips_opts.mips16 = 1;
12177 else if (strcmp (name, "nomips16") == 0
12178 || strcmp (name, "noMIPS-16") == 0)
12179 mips_opts.mips16 = 0;
1f25f5d3
CD
12180 else if (strcmp (name, "mips3d") == 0)
12181 mips_opts.ase_mips3d = 1;
12182 else if (strcmp (name, "nomips3d") == 0)
12183 mips_opts.ase_mips3d = 0;
a4672219
TS
12184 else if (strcmp (name, "mdmx") == 0)
12185 mips_opts.ase_mdmx = 1;
12186 else if (strcmp (name, "nomdmx") == 0)
12187 mips_opts.ase_mdmx = 0;
1a2c1fad 12188 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
252b5132 12189 {
af7ee8bf 12190 int reset = 0;
252b5132 12191
1a2c1fad
CD
12192 /* Permit the user to change the ISA and architecture on the fly.
12193 Needless to say, misuse can cause serious problems. */
af7ee8bf
CD
12194 if (strcmp (name, "mips0") == 0)
12195 {
12196 reset = 1;
12197 mips_opts.isa = file_mips_isa;
12198 }
12199 else if (strcmp (name, "mips1") == 0)
12200 mips_opts.isa = ISA_MIPS1;
12201 else if (strcmp (name, "mips2") == 0)
12202 mips_opts.isa = ISA_MIPS2;
12203 else if (strcmp (name, "mips3") == 0)
12204 mips_opts.isa = ISA_MIPS3;
12205 else if (strcmp (name, "mips4") == 0)
12206 mips_opts.isa = ISA_MIPS4;
12207 else if (strcmp (name, "mips5") == 0)
12208 mips_opts.isa = ISA_MIPS5;
12209 else if (strcmp (name, "mips32") == 0)
12210 mips_opts.isa = ISA_MIPS32;
12211 else if (strcmp (name, "mips32r2") == 0)
12212 mips_opts.isa = ISA_MIPS32R2;
12213 else if (strcmp (name, "mips64") == 0)
12214 mips_opts.isa = ISA_MIPS64;
5f74bc13
CD
12215 else if (strcmp (name, "mips64r2") == 0)
12216 mips_opts.isa = ISA_MIPS64R2;
1a2c1fad
CD
12217 else if (strcmp (name, "arch=default") == 0)
12218 {
12219 reset = 1;
12220 mips_opts.arch = file_mips_arch;
12221 mips_opts.isa = file_mips_isa;
12222 }
12223 else if (strncmp (name, "arch=", 5) == 0)
12224 {
12225 const struct mips_cpu_info *p;
12226
12227 p = mips_parse_cpu("internal use", name + 5);
12228 if (!p)
12229 as_bad (_("unknown architecture %s"), name + 5);
12230 else
12231 {
12232 mips_opts.arch = p->cpu;
12233 mips_opts.isa = p->isa;
12234 }
12235 }
af7ee8bf
CD
12236 else
12237 as_bad (_("unknown ISA level %s"), name + 4);
12238
12239 switch (mips_opts.isa)
98d3f06f
KH
12240 {
12241 case 0:
98d3f06f 12242 break;
af7ee8bf
CD
12243 case ISA_MIPS1:
12244 case ISA_MIPS2:
12245 case ISA_MIPS32:
12246 case ISA_MIPS32R2:
98d3f06f
KH
12247 mips_opts.gp32 = 1;
12248 mips_opts.fp32 = 1;
12249 break;
af7ee8bf
CD
12250 case ISA_MIPS3:
12251 case ISA_MIPS4:
12252 case ISA_MIPS5:
12253 case ISA_MIPS64:
5f74bc13 12254 case ISA_MIPS64R2:
98d3f06f
KH
12255 mips_opts.gp32 = 0;
12256 mips_opts.fp32 = 0;
12257 break;
12258 default:
12259 as_bad (_("unknown ISA level %s"), name + 4);
12260 break;
12261 }
af7ee8bf 12262 if (reset)
98d3f06f 12263 {
af7ee8bf
CD
12264 mips_opts.gp32 = file_mips_gp32;
12265 mips_opts.fp32 = file_mips_fp32;
98d3f06f 12266 }
252b5132
RH
12267 }
12268 else if (strcmp (name, "autoextend") == 0)
12269 mips_opts.noautoextend = 0;
12270 else if (strcmp (name, "noautoextend") == 0)
12271 mips_opts.noautoextend = 1;
12272 else if (strcmp (name, "push") == 0)
12273 {
12274 struct mips_option_stack *s;
12275
12276 s = (struct mips_option_stack *) xmalloc (sizeof *s);
12277 s->next = mips_opts_stack;
12278 s->options = mips_opts;
12279 mips_opts_stack = s;
12280 }
12281 else if (strcmp (name, "pop") == 0)
12282 {
12283 struct mips_option_stack *s;
12284
12285 s = mips_opts_stack;
12286 if (s == NULL)
12287 as_bad (_(".set pop with no .set push"));
12288 else
12289 {
12290 /* If we're changing the reorder mode we need to handle
12291 delay slots correctly. */
12292 if (s->options.noreorder && ! mips_opts.noreorder)
b34976b6 12293 mips_emit_delays (TRUE);
252b5132
RH
12294 else if (! s->options.noreorder && mips_opts.noreorder)
12295 {
12296 if (prev_nop_frag != NULL)
12297 {
12298 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
12299 * (mips_opts.mips16 ? 2 : 4));
12300 prev_nop_frag = NULL;
12301 }
12302 }
12303
12304 mips_opts = s->options;
12305 mips_opts_stack = s->next;
12306 free (s);
12307 }
12308 }
12309 else
12310 {
12311 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
12312 }
12313 *input_line_pointer = ch;
12314 demand_empty_rest_of_line ();
12315}
12316
12317/* Handle the .abicalls pseudo-op. I believe this is equivalent to
12318 .option pic2. It means to generate SVR4 PIC calls. */
12319
12320static void
17a2f251 12321s_abicalls (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
12322{
12323 mips_pic = SVR4_PIC;
143d77c5 12324 mips_abicalls = TRUE;
252b5132
RH
12325 if (USE_GLOBAL_POINTER_OPT)
12326 {
12327 if (g_switch_seen && g_switch_value != 0)
12328 as_warn (_("-G may not be used with SVR4 PIC code"));
12329 g_switch_value = 0;
12330 }
12331 bfd_set_gp_size (stdoutput, 0);
12332 demand_empty_rest_of_line ();
12333}
12334
12335/* Handle the .cpload pseudo-op. This is used when generating SVR4
12336 PIC code. It sets the $gp register for the function based on the
12337 function address, which is in the register named in the argument.
12338 This uses a relocation against _gp_disp, which is handled specially
12339 by the linker. The result is:
12340 lui $gp,%hi(_gp_disp)
12341 addiu $gp,$gp,%lo(_gp_disp)
12342 addu $gp,$gp,.cpload argument
12343 The .cpload argument is normally $25 == $t9. */
12344
12345static void
17a2f251 12346s_cpload (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
12347{
12348 expressionS ex;
12349 int icnt = 0;
12350
6478892d
TS
12351 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12352 .cpload is ignored. */
12353 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
12354 {
12355 s_ignore (0);
12356 return;
12357 }
12358
d3ecfc59 12359 /* .cpload should be in a .set noreorder section. */
252b5132
RH
12360 if (mips_opts.noreorder == 0)
12361 as_warn (_(".cpload not in noreorder section"));
12362
12363 ex.X_op = O_symbol;
12364 ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
12365 ex.X_op_symbol = NULL;
12366 ex.X_add_number = 0;
12367
12368 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
49309057 12369 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
252b5132 12370
c9914766 12371 macro_build_lui (NULL, &icnt, &ex, mips_gp_register);
17a2f251
TS
12372 macro_build (NULL, &icnt, &ex, "addiu", "t,r,j", mips_gp_register,
12373 mips_gp_register, BFD_RELOC_LO16);
252b5132 12374
17a2f251
TS
12375 macro_build (NULL, &icnt, NULL, "addu", "d,v,t", mips_gp_register,
12376 mips_gp_register, tc_get_register (0));
252b5132
RH
12377
12378 demand_empty_rest_of_line ();
12379}
12380
6478892d
TS
12381/* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
12382 .cpsetup $reg1, offset|$reg2, label
12383
12384 If offset is given, this results in:
12385 sd $gp, offset($sp)
956cd1d6 12386 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
12387 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
12388 daddu $gp, $gp, $reg1
6478892d
TS
12389
12390 If $reg2 is given, this results in:
12391 daddu $reg2, $gp, $0
956cd1d6 12392 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
12393 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
12394 daddu $gp, $gp, $reg1
12395 $reg1 is normally $25 == $t9. */
6478892d 12396static void
17a2f251 12397s_cpsetup (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
12398{
12399 expressionS ex_off;
12400 expressionS ex_sym;
12401 int reg1;
12402 int icnt = 0;
f21f8242 12403 char *f;
6478892d 12404
8586fc66 12405 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
6478892d
TS
12406 We also need NewABI support. */
12407 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12408 {
12409 s_ignore (0);
12410 return;
12411 }
12412
12413 reg1 = tc_get_register (0);
12414 SKIP_WHITESPACE ();
12415 if (*input_line_pointer != ',')
12416 {
12417 as_bad (_("missing argument separator ',' for .cpsetup"));
12418 return;
12419 }
12420 else
80245285 12421 ++input_line_pointer;
6478892d
TS
12422 SKIP_WHITESPACE ();
12423 if (*input_line_pointer == '$')
80245285
TS
12424 {
12425 mips_cpreturn_register = tc_get_register (0);
12426 mips_cpreturn_offset = -1;
12427 }
6478892d 12428 else
80245285
TS
12429 {
12430 mips_cpreturn_offset = get_absolute_expression ();
12431 mips_cpreturn_register = -1;
12432 }
6478892d
TS
12433 SKIP_WHITESPACE ();
12434 if (*input_line_pointer != ',')
12435 {
12436 as_bad (_("missing argument separator ',' for .cpsetup"));
12437 return;
12438 }
12439 else
f9419b05 12440 ++input_line_pointer;
6478892d 12441 SKIP_WHITESPACE ();
f21f8242 12442 expression (&ex_sym);
6478892d
TS
12443
12444 if (mips_cpreturn_register == -1)
12445 {
12446 ex_off.X_op = O_constant;
12447 ex_off.X_add_symbol = NULL;
12448 ex_off.X_op_symbol = NULL;
12449 ex_off.X_add_number = mips_cpreturn_offset;
12450
17a2f251
TS
12451 macro_build (NULL, &icnt, &ex_off, "sd", "t,o(b)", mips_gp_register,
12452 BFD_RELOC_LO16, SP);
6478892d
TS
12453 }
12454 else
17a2f251
TS
12455 macro_build (NULL, &icnt, NULL, "daddu", "d,v,t", mips_cpreturn_register,
12456 mips_gp_register, 0);
6478892d 12457
f21f8242
AO
12458 /* Ensure there's room for the next two instructions, so that `f'
12459 doesn't end up with an address in the wrong frag. */
12460 frag_grow (8);
12461 f = frag_more (0);
17a2f251
TS
12462 macro_build (NULL, &icnt, &ex_sym, "lui", "t,u", mips_gp_register,
12463 BFD_RELOC_GPREL16);
f21f8242 12464 fix_new (frag_now, f - frag_now->fr_literal,
a105a300 12465 8, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
f21f8242 12466 fix_new (frag_now, f - frag_now->fr_literal,
a105a300 12467 4, NULL, 0, 0, BFD_RELOC_HI16_S);
f21f8242
AO
12468
12469 f = frag_more (0);
17a2f251
TS
12470 macro_build (NULL, &icnt, &ex_sym, "addiu", "t,r,j", mips_gp_register,
12471 mips_gp_register, BFD_RELOC_GPREL16);
f21f8242 12472 fix_new (frag_now, f - frag_now->fr_literal,
a105a300 12473 8, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
f21f8242 12474 fix_new (frag_now, f - frag_now->fr_literal,
a105a300 12475 4, NULL, 0, 0, BFD_RELOC_LO16);
f21f8242 12476
17a2f251
TS
12477 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
12478 mips_gp_register, reg1);
6478892d
TS
12479
12480 demand_empty_rest_of_line ();
12481}
12482
12483static void
17a2f251 12484s_cplocal (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
12485{
12486 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
12487 .cplocal is ignored. */
12488 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12489 {
12490 s_ignore (0);
12491 return;
12492 }
12493
12494 mips_gp_register = tc_get_register (0);
85b51719 12495 demand_empty_rest_of_line ();
6478892d
TS
12496}
12497
252b5132
RH
12498/* Handle the .cprestore pseudo-op. This stores $gp into a given
12499 offset from $sp. The offset is remembered, and after making a PIC
12500 call $gp is restored from that location. */
12501
12502static void
17a2f251 12503s_cprestore (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
12504{
12505 expressionS ex;
12506 int icnt = 0;
12507
6478892d 12508 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
c9914766 12509 .cprestore is ignored. */
6478892d 12510 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
12511 {
12512 s_ignore (0);
12513 return;
12514 }
12515
12516 mips_cprestore_offset = get_absolute_expression ();
7a621144 12517 mips_cprestore_valid = 1;
252b5132
RH
12518
12519 ex.X_op = O_constant;
12520 ex.X_add_symbol = NULL;
12521 ex.X_op_symbol = NULL;
12522 ex.X_add_number = mips_cprestore_offset;
12523
17a2f251 12524 macro_build_ldst_constoffset (NULL, &icnt, &ex, ADDRESS_STORE_INSN,
256ab948 12525 mips_gp_register, SP, HAVE_64BIT_ADDRESSES);
252b5132
RH
12526
12527 demand_empty_rest_of_line ();
12528}
12529
6478892d 12530/* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
609f23f4 12531 was given in the preceeding .cpsetup, it results in:
6478892d 12532 ld $gp, offset($sp)
76b3015f 12533
6478892d 12534 If a register $reg2 was given there, it results in:
609f23f4 12535 daddu $gp, $reg2, $0
6478892d
TS
12536 */
12537static void
17a2f251 12538s_cpreturn (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
12539{
12540 expressionS ex;
12541 int icnt = 0;
12542
12543 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
12544 We also need NewABI support. */
12545 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12546 {
12547 s_ignore (0);
12548 return;
12549 }
12550
12551 if (mips_cpreturn_register == -1)
12552 {
12553 ex.X_op = O_constant;
12554 ex.X_add_symbol = NULL;
12555 ex.X_op_symbol = NULL;
12556 ex.X_add_number = mips_cpreturn_offset;
12557
17a2f251
TS
12558 macro_build (NULL, &icnt, &ex, "ld", "t,o(b)", mips_gp_register,
12559 BFD_RELOC_LO16, SP);
6478892d
TS
12560 }
12561 else
17a2f251
TS
12562 macro_build (NULL, &icnt, NULL, "daddu", "d,v,t", mips_gp_register,
12563 mips_cpreturn_register, 0);
6478892d
TS
12564
12565 demand_empty_rest_of_line ();
12566}
12567
12568/* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
12569 code. It sets the offset to use in gp_rel relocations. */
12570
12571static void
17a2f251 12572s_gpvalue (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
12573{
12574 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
12575 We also need NewABI support. */
12576 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12577 {
12578 s_ignore (0);
12579 return;
12580 }
12581
def2e0dd 12582 mips_gprel_offset = get_absolute_expression ();
6478892d
TS
12583
12584 demand_empty_rest_of_line ();
12585}
12586
252b5132
RH
12587/* Handle the .gpword pseudo-op. This is used when generating PIC
12588 code. It generates a 32 bit GP relative reloc. */
12589
12590static void
17a2f251 12591s_gpword (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
12592{
12593 symbolS *label;
12594 expressionS ex;
12595 char *p;
12596
12597 /* When not generating PIC code, this is treated as .word. */
12598 if (mips_pic != SVR4_PIC)
12599 {
12600 s_cons (2);
12601 return;
12602 }
12603
12604 label = insn_labels != NULL ? insn_labels->label : NULL;
b34976b6 12605 mips_emit_delays (TRUE);
252b5132
RH
12606 if (auto_align)
12607 mips_align (2, 0, label);
12608 mips_clear_insn_labels ();
12609
12610 expression (&ex);
12611
12612 if (ex.X_op != O_symbol || ex.X_add_number != 0)
12613 {
12614 as_bad (_("Unsupported use of .gpword"));
12615 ignore_rest_of_line ();
12616 }
12617
12618 p = frag_more (4);
17a2f251 12619 md_number_to_chars (p, 0, 4);
b34976b6 12620 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
cdf6fd85 12621 BFD_RELOC_GPREL32);
252b5132
RH
12622
12623 demand_empty_rest_of_line ();
12624}
12625
10181a0d 12626static void
17a2f251 12627s_gpdword (int ignore ATTRIBUTE_UNUSED)
10181a0d
AO
12628{
12629 symbolS *label;
12630 expressionS ex;
12631 char *p;
12632
12633 /* When not generating PIC code, this is treated as .dword. */
12634 if (mips_pic != SVR4_PIC)
12635 {
12636 s_cons (3);
12637 return;
12638 }
12639
12640 label = insn_labels != NULL ? insn_labels->label : NULL;
b34976b6 12641 mips_emit_delays (TRUE);
10181a0d
AO
12642 if (auto_align)
12643 mips_align (3, 0, label);
12644 mips_clear_insn_labels ();
12645
12646 expression (&ex);
12647
12648 if (ex.X_op != O_symbol || ex.X_add_number != 0)
12649 {
12650 as_bad (_("Unsupported use of .gpdword"));
12651 ignore_rest_of_line ();
12652 }
12653
12654 p = frag_more (8);
17a2f251 12655 md_number_to_chars (p, 0, 8);
a105a300 12656 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
10181a0d
AO
12657 BFD_RELOC_GPREL32);
12658
12659 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
12660 ex.X_op = O_absent;
12661 ex.X_add_symbol = 0;
12662 ex.X_add_number = 0;
b34976b6 12663 fix_new_exp (frag_now, p - frag_now->fr_literal, 8, &ex, FALSE,
10181a0d
AO
12664 BFD_RELOC_64);
12665
12666 demand_empty_rest_of_line ();
12667}
12668
252b5132
RH
12669/* Handle the .cpadd pseudo-op. This is used when dealing with switch
12670 tables in SVR4 PIC code. */
12671
12672static void
17a2f251 12673s_cpadd (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
12674{
12675 int icnt = 0;
12676 int reg;
12677
10181a0d
AO
12678 /* This is ignored when not generating SVR4 PIC code. */
12679 if (mips_pic != SVR4_PIC)
252b5132
RH
12680 {
12681 s_ignore (0);
12682 return;
12683 }
12684
12685 /* Add $gp to the register named as an argument. */
12686 reg = tc_get_register (0);
17a2f251
TS
12687 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
12688 reg, reg, mips_gp_register);
252b5132 12689
bdaaa2e1 12690 demand_empty_rest_of_line ();
252b5132
RH
12691}
12692
12693/* Handle the .insn pseudo-op. This marks instruction labels in
12694 mips16 mode. This permits the linker to handle them specially,
12695 such as generating jalx instructions when needed. We also make
12696 them odd for the duration of the assembly, in order to generate the
12697 right sort of code. We will make them even in the adjust_symtab
12698 routine, while leaving them marked. This is convenient for the
12699 debugger and the disassembler. The linker knows to make them odd
12700 again. */
12701
12702static void
17a2f251 12703s_insn (int ignore ATTRIBUTE_UNUSED)
252b5132 12704{
f9419b05 12705 mips16_mark_labels ();
252b5132
RH
12706
12707 demand_empty_rest_of_line ();
12708}
12709
12710/* Handle a .stabn directive. We need these in order to mark a label
12711 as being a mips16 text label correctly. Sometimes the compiler
12712 will emit a label, followed by a .stabn, and then switch sections.
12713 If the label and .stabn are in mips16 mode, then the label is
12714 really a mips16 text label. */
12715
12716static void
17a2f251 12717s_mips_stab (int type)
252b5132 12718{
f9419b05 12719 if (type == 'n')
252b5132
RH
12720 mips16_mark_labels ();
12721
12722 s_stab (type);
12723}
12724
12725/* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
12726 */
12727
12728static void
17a2f251 12729s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
12730{
12731 char *name;
12732 int c;
12733 symbolS *symbolP;
12734 expressionS exp;
12735
12736 name = input_line_pointer;
12737 c = get_symbol_end ();
12738 symbolP = symbol_find_or_make (name);
12739 S_SET_WEAK (symbolP);
12740 *input_line_pointer = c;
12741
12742 SKIP_WHITESPACE ();
12743
12744 if (! is_end_of_line[(unsigned char) *input_line_pointer])
12745 {
12746 if (S_IS_DEFINED (symbolP))
12747 {
956cd1d6 12748 as_bad ("ignoring attempt to redefine symbol %s",
252b5132
RH
12749 S_GET_NAME (symbolP));
12750 ignore_rest_of_line ();
12751 return;
12752 }
bdaaa2e1 12753
252b5132
RH
12754 if (*input_line_pointer == ',')
12755 {
12756 ++input_line_pointer;
12757 SKIP_WHITESPACE ();
12758 }
bdaaa2e1 12759
252b5132
RH
12760 expression (&exp);
12761 if (exp.X_op != O_symbol)
12762 {
12763 as_bad ("bad .weakext directive");
98d3f06f 12764 ignore_rest_of_line ();
252b5132
RH
12765 return;
12766 }
49309057 12767 symbol_set_value_expression (symbolP, &exp);
252b5132
RH
12768 }
12769
12770 demand_empty_rest_of_line ();
12771}
12772
12773/* Parse a register string into a number. Called from the ECOFF code
12774 to parse .frame. The argument is non-zero if this is the frame
12775 register, so that we can record it in mips_frame_reg. */
12776
12777int
17a2f251 12778tc_get_register (int frame)
252b5132
RH
12779{
12780 int reg;
12781
12782 SKIP_WHITESPACE ();
12783 if (*input_line_pointer++ != '$')
12784 {
12785 as_warn (_("expected `$'"));
85b51719 12786 reg = ZERO;
252b5132 12787 }
3882b010 12788 else if (ISDIGIT (*input_line_pointer))
252b5132
RH
12789 {
12790 reg = get_absolute_expression ();
12791 if (reg < 0 || reg >= 32)
12792 {
12793 as_warn (_("Bad register number"));
85b51719 12794 reg = ZERO;
252b5132
RH
12795 }
12796 }
12797 else
12798 {
76db943d 12799 if (strncmp (input_line_pointer, "ra", 2) == 0)
85b51719
TS
12800 {
12801 reg = RA;
12802 input_line_pointer += 2;
12803 }
76db943d 12804 else if (strncmp (input_line_pointer, "fp", 2) == 0)
85b51719
TS
12805 {
12806 reg = FP;
12807 input_line_pointer += 2;
12808 }
252b5132 12809 else if (strncmp (input_line_pointer, "sp", 2) == 0)
85b51719
TS
12810 {
12811 reg = SP;
12812 input_line_pointer += 2;
12813 }
252b5132 12814 else if (strncmp (input_line_pointer, "gp", 2) == 0)
85b51719
TS
12815 {
12816 reg = GP;
12817 input_line_pointer += 2;
12818 }
252b5132 12819 else if (strncmp (input_line_pointer, "at", 2) == 0)
85b51719
TS
12820 {
12821 reg = AT;
12822 input_line_pointer += 2;
12823 }
12824 else if (strncmp (input_line_pointer, "kt0", 3) == 0)
12825 {
12826 reg = KT0;
12827 input_line_pointer += 3;
12828 }
12829 else if (strncmp (input_line_pointer, "kt1", 3) == 0)
12830 {
12831 reg = KT1;
12832 input_line_pointer += 3;
12833 }
12834 else if (strncmp (input_line_pointer, "zero", 4) == 0)
12835 {
12836 reg = ZERO;
12837 input_line_pointer += 4;
12838 }
252b5132
RH
12839 else
12840 {
12841 as_warn (_("Unrecognized register name"));
85b51719
TS
12842 reg = ZERO;
12843 while (ISALNUM(*input_line_pointer))
12844 input_line_pointer++;
252b5132 12845 }
252b5132
RH
12846 }
12847 if (frame)
7a621144
DJ
12848 {
12849 mips_frame_reg = reg != 0 ? reg : SP;
12850 mips_frame_reg_valid = 1;
12851 mips_cprestore_valid = 0;
12852 }
252b5132
RH
12853 return reg;
12854}
12855
12856valueT
17a2f251 12857md_section_align (asection *seg, valueT addr)
252b5132
RH
12858{
12859 int align = bfd_get_section_alignment (stdoutput, seg);
12860
12861#ifdef OBJ_ELF
12862 /* We don't need to align ELF sections to the full alignment.
12863 However, Irix 5 may prefer that we align them at least to a 16
12864 byte boundary. We don't bother to align the sections if we are
12865 targeted for an embedded system. */
12866 if (strcmp (TARGET_OS, "elf") == 0)
12867 return addr;
12868 if (align > 4)
12869 align = 4;
12870#endif
12871
12872 return ((addr + (1 << align) - 1) & (-1 << align));
12873}
12874
12875/* Utility routine, called from above as well. If called while the
12876 input file is still being read, it's only an approximation. (For
12877 example, a symbol may later become defined which appeared to be
12878 undefined earlier.) */
12879
12880static int
17a2f251 12881nopic_need_relax (symbolS *sym, int before_relaxing)
252b5132
RH
12882{
12883 if (sym == 0)
12884 return 0;
12885
6478892d 12886 if (USE_GLOBAL_POINTER_OPT && g_switch_value > 0)
252b5132
RH
12887 {
12888 const char *symname;
12889 int change;
12890
c9914766 12891 /* Find out whether this symbol can be referenced off the $gp
252b5132
RH
12892 register. It can be if it is smaller than the -G size or if
12893 it is in the .sdata or .sbss section. Certain symbols can
c9914766 12894 not be referenced off the $gp, although it appears as though
252b5132
RH
12895 they can. */
12896 symname = S_GET_NAME (sym);
12897 if (symname != (const char *) NULL
12898 && (strcmp (symname, "eprol") == 0
12899 || strcmp (symname, "etext") == 0
12900 || strcmp (symname, "_gp") == 0
12901 || strcmp (symname, "edata") == 0
12902 || strcmp (symname, "_fbss") == 0
12903 || strcmp (symname, "_fdata") == 0
12904 || strcmp (symname, "_ftext") == 0
12905 || strcmp (symname, "end") == 0
12906 || strcmp (symname, "_gp_disp") == 0))
12907 change = 1;
12908 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
12909 && (0
12910#ifndef NO_ECOFF_DEBUGGING
49309057
ILT
12911 || (symbol_get_obj (sym)->ecoff_extern_size != 0
12912 && (symbol_get_obj (sym)->ecoff_extern_size
12913 <= g_switch_value))
252b5132
RH
12914#endif
12915 /* We must defer this decision until after the whole
12916 file has been read, since there might be a .extern
12917 after the first use of this symbol. */
12918 || (before_relaxing
12919#ifndef NO_ECOFF_DEBUGGING
49309057 12920 && symbol_get_obj (sym)->ecoff_extern_size == 0
252b5132
RH
12921#endif
12922 && S_GET_VALUE (sym) == 0)
12923 || (S_GET_VALUE (sym) != 0
12924 && S_GET_VALUE (sym) <= g_switch_value)))
12925 change = 0;
12926 else
12927 {
12928 const char *segname;
12929
12930 segname = segment_name (S_GET_SEGMENT (sym));
12931 assert (strcmp (segname, ".lit8") != 0
12932 && strcmp (segname, ".lit4") != 0);
12933 change = (strcmp (segname, ".sdata") != 0
fba2b7f9
GK
12934 && strcmp (segname, ".sbss") != 0
12935 && strncmp (segname, ".sdata.", 7) != 0
12936 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
252b5132
RH
12937 }
12938 return change;
12939 }
12940 else
c9914766 12941 /* We are not optimizing for the $gp register. */
252b5132
RH
12942 return 1;
12943}
12944
5919d012
RS
12945
12946/* Return true if the given symbol should be considered local for SVR4 PIC. */
12947
12948static bfd_boolean
17a2f251 12949pic_need_relax (symbolS *sym, asection *segtype)
5919d012
RS
12950{
12951 asection *symsec;
12952 bfd_boolean linkonce;
12953
12954 /* Handle the case of a symbol equated to another symbol. */
12955 while (symbol_equated_reloc_p (sym))
12956 {
12957 symbolS *n;
12958
12959 /* It's possible to get a loop here in a badly written
12960 program. */
12961 n = symbol_get_value_expression (sym)->X_add_symbol;
12962 if (n == sym)
12963 break;
12964 sym = n;
12965 }
12966
12967 symsec = S_GET_SEGMENT (sym);
12968
12969 /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
12970 linkonce = FALSE;
12971 if (symsec != segtype && ! S_IS_LOCAL (sym))
12972 {
12973 if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
12974 != 0)
12975 linkonce = TRUE;
12976
12977 /* The GNU toolchain uses an extension for ELF: a section
12978 beginning with the magic string .gnu.linkonce is a linkonce
12979 section. */
12980 if (strncmp (segment_name (symsec), ".gnu.linkonce",
12981 sizeof ".gnu.linkonce" - 1) == 0)
12982 linkonce = TRUE;
12983 }
12984
12985 /* This must duplicate the test in adjust_reloc_syms. */
12986 return (symsec != &bfd_und_section
12987 && symsec != &bfd_abs_section
12988 && ! bfd_is_com_section (symsec)
12989 && !linkonce
12990#ifdef OBJ_ELF
12991 /* A global or weak symbol is treated as external. */
12992 && (OUTPUT_FLAVOR != bfd_target_elf_flavour
12993 || (! S_IS_WEAK (sym)
12994 && (! S_IS_EXTERNAL (sym)
12995 || mips_pic == EMBEDDED_PIC)))
12996#endif
12997 );
12998}
12999
13000
252b5132
RH
13001/* Given a mips16 variant frag FRAGP, return non-zero if it needs an
13002 extended opcode. SEC is the section the frag is in. */
13003
13004static int
17a2f251 13005mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
252b5132
RH
13006{
13007 int type;
13008 register const struct mips16_immed_operand *op;
13009 offsetT val;
13010 int mintiny, maxtiny;
13011 segT symsec;
98aa84af 13012 fragS *sym_frag;
252b5132
RH
13013
13014 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
13015 return 0;
13016 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
13017 return 1;
13018
13019 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
13020 op = mips16_immed_operands;
13021 while (op->type != type)
13022 {
13023 ++op;
13024 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
13025 }
13026
13027 if (op->unsp)
13028 {
13029 if (type == '<' || type == '>' || type == '[' || type == ']')
13030 {
13031 mintiny = 1;
13032 maxtiny = 1 << op->nbits;
13033 }
13034 else
13035 {
13036 mintiny = 0;
13037 maxtiny = (1 << op->nbits) - 1;
13038 }
13039 }
13040 else
13041 {
13042 mintiny = - (1 << (op->nbits - 1));
13043 maxtiny = (1 << (op->nbits - 1)) - 1;
13044 }
13045
98aa84af 13046 sym_frag = symbol_get_frag (fragp->fr_symbol);
ac62c346 13047 val = S_GET_VALUE (fragp->fr_symbol);
98aa84af 13048 symsec = S_GET_SEGMENT (fragp->fr_symbol);
252b5132
RH
13049
13050 if (op->pcrel)
13051 {
13052 addressT addr;
13053
13054 /* We won't have the section when we are called from
13055 mips_relax_frag. However, we will always have been called
13056 from md_estimate_size_before_relax first. If this is a
13057 branch to a different section, we mark it as such. If SEC is
13058 NULL, and the frag is not marked, then it must be a branch to
13059 the same section. */
13060 if (sec == NULL)
13061 {
13062 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
13063 return 1;
13064 }
13065 else
13066 {
98aa84af 13067 /* Must have been called from md_estimate_size_before_relax. */
252b5132
RH
13068 if (symsec != sec)
13069 {
13070 fragp->fr_subtype =
13071 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13072
13073 /* FIXME: We should support this, and let the linker
13074 catch branches and loads that are out of range. */
13075 as_bad_where (fragp->fr_file, fragp->fr_line,
13076 _("unsupported PC relative reference to different section"));
13077
13078 return 1;
13079 }
98aa84af
AM
13080 if (fragp != sym_frag && sym_frag->fr_address == 0)
13081 /* Assume non-extended on the first relaxation pass.
13082 The address we have calculated will be bogus if this is
13083 a forward branch to another frag, as the forward frag
13084 will have fr_address == 0. */
13085 return 0;
252b5132
RH
13086 }
13087
13088 /* In this case, we know for sure that the symbol fragment is in
98aa84af
AM
13089 the same section. If the relax_marker of the symbol fragment
13090 differs from the relax_marker of this fragment, we have not
13091 yet adjusted the symbol fragment fr_address. We want to add
252b5132
RH
13092 in STRETCH in order to get a better estimate of the address.
13093 This particularly matters because of the shift bits. */
13094 if (stretch != 0
98aa84af 13095 && sym_frag->relax_marker != fragp->relax_marker)
252b5132
RH
13096 {
13097 fragS *f;
13098
13099 /* Adjust stretch for any alignment frag. Note that if have
13100 been expanding the earlier code, the symbol may be
13101 defined in what appears to be an earlier frag. FIXME:
13102 This doesn't handle the fr_subtype field, which specifies
13103 a maximum number of bytes to skip when doing an
13104 alignment. */
98aa84af 13105 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
252b5132
RH
13106 {
13107 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
13108 {
13109 if (stretch < 0)
13110 stretch = - ((- stretch)
13111 & ~ ((1 << (int) f->fr_offset) - 1));
13112 else
13113 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
13114 if (stretch == 0)
13115 break;
13116 }
13117 }
13118 if (f != NULL)
13119 val += stretch;
13120 }
13121
13122 addr = fragp->fr_address + fragp->fr_fix;
13123
13124 /* The base address rules are complicated. The base address of
13125 a branch is the following instruction. The base address of a
13126 PC relative load or add is the instruction itself, but if it
13127 is in a delay slot (in which case it can not be extended) use
13128 the address of the instruction whose delay slot it is in. */
13129 if (type == 'p' || type == 'q')
13130 {
13131 addr += 2;
13132
13133 /* If we are currently assuming that this frag should be
13134 extended, then, the current address is two bytes
bdaaa2e1 13135 higher. */
252b5132
RH
13136 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13137 addr += 2;
13138
13139 /* Ignore the low bit in the target, since it will be set
13140 for a text label. */
13141 if ((val & 1) != 0)
13142 --val;
13143 }
13144 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13145 addr -= 4;
13146 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13147 addr -= 2;
13148
13149 val -= addr & ~ ((1 << op->shift) - 1);
13150
13151 /* Branch offsets have an implicit 0 in the lowest bit. */
13152 if (type == 'p' || type == 'q')
13153 val /= 2;
13154
13155 /* If any of the shifted bits are set, we must use an extended
13156 opcode. If the address depends on the size of this
13157 instruction, this can lead to a loop, so we arrange to always
13158 use an extended opcode. We only check this when we are in
13159 the main relaxation loop, when SEC is NULL. */
13160 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
13161 {
13162 fragp->fr_subtype =
13163 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13164 return 1;
13165 }
13166
13167 /* If we are about to mark a frag as extended because the value
13168 is precisely maxtiny + 1, then there is a chance of an
13169 infinite loop as in the following code:
13170 la $4,foo
13171 .skip 1020
13172 .align 2
13173 foo:
13174 In this case when the la is extended, foo is 0x3fc bytes
13175 away, so the la can be shrunk, but then foo is 0x400 away, so
13176 the la must be extended. To avoid this loop, we mark the
13177 frag as extended if it was small, and is about to become
13178 extended with a value of maxtiny + 1. */
13179 if (val == ((maxtiny + 1) << op->shift)
13180 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
13181 && sec == NULL)
13182 {
13183 fragp->fr_subtype =
13184 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13185 return 1;
13186 }
13187 }
13188 else if (symsec != absolute_section && sec != NULL)
13189 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
13190
13191 if ((val & ((1 << op->shift) - 1)) != 0
13192 || val < (mintiny << op->shift)
13193 || val > (maxtiny << op->shift))
13194 return 1;
13195 else
13196 return 0;
13197}
13198
4a6a3df4
AO
13199/* Compute the length of a branch sequence, and adjust the
13200 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
13201 worst-case length is computed, with UPDATE being used to indicate
13202 whether an unconditional (-1), branch-likely (+1) or regular (0)
13203 branch is to be computed. */
13204static int
17a2f251 13205relaxed_branch_length (fragS *fragp, asection *sec, int update)
4a6a3df4 13206{
b34976b6 13207 bfd_boolean toofar;
4a6a3df4
AO
13208 int length;
13209
13210 if (fragp
13211 && S_IS_DEFINED (fragp->fr_symbol)
13212 && sec == S_GET_SEGMENT (fragp->fr_symbol))
13213 {
13214 addressT addr;
13215 offsetT val;
13216
13217 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
13218
13219 addr = fragp->fr_address + fragp->fr_fix + 4;
13220
13221 val -= addr;
13222
13223 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
13224 }
13225 else if (fragp)
13226 /* If the symbol is not defined or it's in a different segment,
13227 assume the user knows what's going on and emit a short
13228 branch. */
b34976b6 13229 toofar = FALSE;
4a6a3df4 13230 else
b34976b6 13231 toofar = TRUE;
4a6a3df4
AO
13232
13233 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13234 fragp->fr_subtype
af6ae2ad 13235 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_UNCOND (fragp->fr_subtype),
4a6a3df4
AO
13236 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
13237 RELAX_BRANCH_LINK (fragp->fr_subtype),
13238 toofar);
13239
13240 length = 4;
13241 if (toofar)
13242 {
13243 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
13244 length += 8;
13245
13246 if (mips_pic != NO_PIC)
13247 {
13248 /* Additional space for PIC loading of target address. */
13249 length += 8;
13250 if (mips_opts.isa == ISA_MIPS1)
13251 /* Additional space for $at-stabilizing nop. */
13252 length += 4;
13253 }
13254
13255 /* If branch is conditional. */
13256 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
13257 length += 8;
13258 }
b34976b6 13259
4a6a3df4
AO
13260 return length;
13261}
13262
252b5132
RH
13263/* Estimate the size of a frag before relaxing. Unless this is the
13264 mips16, we are not really relaxing here, and the final size is
13265 encoded in the subtype information. For the mips16, we have to
13266 decide whether we are using an extended opcode or not. */
13267
252b5132 13268int
17a2f251 13269md_estimate_size_before_relax (fragS *fragp, asection *segtype)
252b5132 13270{
5919d012 13271 int change;
252b5132 13272
4a6a3df4
AO
13273 if (RELAX_BRANCH_P (fragp->fr_subtype))
13274 {
13275
b34976b6
AM
13276 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
13277
4a6a3df4
AO
13278 return fragp->fr_var;
13279 }
13280
252b5132 13281 if (RELAX_MIPS16_P (fragp->fr_subtype))
177b4a6a
AO
13282 /* We don't want to modify the EXTENDED bit here; it might get us
13283 into infinite loops. We change it only in mips_relax_frag(). */
13284 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
252b5132
RH
13285
13286 if (mips_pic == NO_PIC)
5919d012 13287 change = nopic_need_relax (fragp->fr_symbol, 0);
252b5132 13288 else if (mips_pic == SVR4_PIC)
5919d012 13289 change = pic_need_relax (fragp->fr_symbol, segtype);
252b5132
RH
13290 else
13291 abort ();
13292
13293 if (change)
13294 {
13295 /* Record the offset to the first reloc in the fr_opcode field.
13296 This lets md_convert_frag and tc_gen_reloc know that the code
13297 must be expanded. */
13298 fragp->fr_opcode = (fragp->fr_literal
13299 + fragp->fr_fix
13300 - RELAX_OLD (fragp->fr_subtype)
13301 + RELAX_RELOC1 (fragp->fr_subtype));
13302 /* FIXME: This really needs as_warn_where. */
13303 if (RELAX_WARN (fragp->fr_subtype))
9a41af64
TS
13304 as_warn (_("AT used after \".set noat\" or macro used after "
13305 "\".set nomacro\""));
13306
13307 return RELAX_NEW (fragp->fr_subtype) - RELAX_OLD (fragp->fr_subtype);
252b5132
RH
13308 }
13309
9a41af64 13310 return 0;
252b5132
RH
13311}
13312
13313/* This is called to see whether a reloc against a defined symbol
13314 should be converted into a reloc against a section. Don't adjust
13315 MIPS16 jump relocations, so we don't have to worry about the format
13316 of the offset in the .o file. Don't adjust relocations against
13317 mips16 symbols, so that the linker can find them if it needs to set
13318 up a stub. */
13319
13320int
17a2f251 13321mips_fix_adjustable (fixS *fixp)
252b5132
RH
13322{
13323 if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
13324 return 0;
a161fe53 13325
252b5132
RH
13326 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
13327 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13328 return 0;
a161fe53 13329
252b5132
RH
13330 if (fixp->fx_addsy == NULL)
13331 return 1;
a161fe53 13332
252b5132
RH
13333#ifdef OBJ_ELF
13334 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
13335 && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
13336 && fixp->fx_subsy == NULL)
13337 return 0;
13338#endif
a161fe53 13339
252b5132
RH
13340 return 1;
13341}
13342
13343/* Translate internal representation of relocation info to BFD target
13344 format. */
13345
13346arelent **
17a2f251 13347tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
13348{
13349 static arelent *retval[4];
13350 arelent *reloc;
13351 bfd_reloc_code_real_type code;
13352
4b0cff4e
TS
13353 memset (retval, 0, sizeof(retval));
13354 reloc = retval[0] = (arelent *) xcalloc (1, sizeof (arelent));
49309057
ILT
13355 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13356 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
13357 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
13358
13359 if (mips_pic == EMBEDDED_PIC
13360 && SWITCH_TABLE (fixp))
13361 {
13362 /* For a switch table entry we use a special reloc. The addend
13363 is actually the difference between the reloc address and the
13364 subtrahend. */
13365 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
13366 if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
13367 as_fatal (_("Double check fx_r_type in tc-mips.c:tc_gen_reloc"));
13368 fixp->fx_r_type = BFD_RELOC_GPREL32;
13369 }
13370 else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16)
13371 {
4514d474
CD
13372 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
13373 reloc->addend = fixp->fx_addnumber;
252b5132 13374 else
4514d474
CD
13375 {
13376 /* We use a special addend for an internal RELLO reloc. */
13377 if (symbol_section_p (fixp->fx_addsy))
13378 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
13379 else
13380 reloc->addend = fixp->fx_addnumber + reloc->address;
13381 }
252b5132
RH
13382 }
13383 else if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
13384 {
13385 assert (fixp->fx_next != NULL
13386 && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
4514d474
CD
13387
13388 /* The reloc is relative to the RELLO; adjust the addend
252b5132 13389 accordingly. */
4514d474
CD
13390 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
13391 reloc->addend = fixp->fx_next->fx_addnumber;
252b5132 13392 else
4514d474
CD
13393 {
13394 /* We use a special addend for an internal RELHI reloc. */
13395 if (symbol_section_p (fixp->fx_addsy))
13396 reloc->addend = (fixp->fx_next->fx_frag->fr_address
13397 + fixp->fx_next->fx_where
13398 - S_GET_VALUE (fixp->fx_subsy));
13399 else
13400 reloc->addend = (fixp->fx_addnumber
13401 + fixp->fx_next->fx_frag->fr_address
13402 + fixp->fx_next->fx_where);
13403 }
252b5132 13404 }
4514d474
CD
13405 else if (fixp->fx_pcrel == 0 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
13406 reloc->addend = fixp->fx_addnumber;
252b5132
RH
13407 else
13408 {
13409 if (OUTPUT_FLAVOR != bfd_target_aout_flavour)
13410 /* A gruesome hack which is a result of the gruesome gas reloc
13411 handling. */
13412 reloc->addend = reloc->address;
13413 else
13414 reloc->addend = -reloc->address;
13415 }
13416
13417 /* If this is a variant frag, we may need to adjust the existing
13418 reloc and generate a new one. */
13419 if (fixp->fx_frag->fr_opcode != NULL
ed6fb7bd
SC
13420 && ((fixp->fx_r_type == BFD_RELOC_GPREL16
13421 && ! HAVE_NEWABI)
f5040a92
AO
13422 || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_DISP
13423 && HAVE_NEWABI)
252b5132
RH
13424 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
13425 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL16
13426 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
13427 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16
13428 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
6478892d 13429 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_LO16)
ed6fb7bd 13430 )
252b5132
RH
13431 {
13432 arelent *reloc2;
13433
13434 assert (! RELAX_MIPS16_P (fixp->fx_frag->fr_subtype));
13435
13436 /* If this is not the last reloc in this frag, then we have two
13437 GPREL relocs, or a GOT_HI16/GOT_LO16 pair, or a
13438 CALL_HI16/CALL_LO16, both of which are being replaced. Let
13439 the second one handle all of them. */
13440 if (fixp->fx_next != NULL
13441 && fixp->fx_frag == fixp->fx_next->fx_frag)
13442 {
cdf6fd85
TS
13443 assert ((fixp->fx_r_type == BFD_RELOC_GPREL16
13444 && fixp->fx_next->fx_r_type == BFD_RELOC_GPREL16)
252b5132
RH
13445 || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
13446 && (fixp->fx_next->fx_r_type
13447 == BFD_RELOC_MIPS_GOT_LO16))
13448 || (fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
13449 && (fixp->fx_next->fx_r_type
13450 == BFD_RELOC_MIPS_CALL_LO16)));
13451 retval[0] = NULL;
13452 return retval;
13453 }
13454
13455 fixp->fx_where = fixp->fx_frag->fr_opcode - fixp->fx_frag->fr_literal;
13456 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
f5040a92 13457 reloc->addend += fixp->fx_frag->tc_frag_data.tc_fr_offset;
252b5132 13458 reloc2 = retval[1] = (arelent *) xmalloc (sizeof (arelent));
49309057
ILT
13459 reloc2->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13460 *reloc2->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
13461 reloc2->address = (reloc->address
13462 + (RELAX_RELOC2 (fixp->fx_frag->fr_subtype)
13463 - RELAX_RELOC1 (fixp->fx_frag->fr_subtype)));
a7ebbfdf 13464 reloc2->addend = fixp->fx_addnumber - S_GET_VALUE (fixp->fx_addsy)
f5040a92 13465 + fixp->fx_frag->tc_frag_data.tc_fr_offset;
252b5132
RH
13466 reloc2->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO16);
13467 assert (reloc2->howto != NULL);
13468
13469 if (RELAX_RELOC3 (fixp->fx_frag->fr_subtype))
13470 {
13471 arelent *reloc3;
13472
13473 reloc3 = retval[2] = (arelent *) xmalloc (sizeof (arelent));
252b5132
RH
13474 *reloc3 = *reloc2;
13475 reloc3->address += 4;
13476 }
13477
13478 if (mips_pic == NO_PIC)
13479 {
cdf6fd85 13480 assert (fixp->fx_r_type == BFD_RELOC_GPREL16);
252b5132
RH
13481 fixp->fx_r_type = BFD_RELOC_HI16_S;
13482 }
13483 else if (mips_pic == SVR4_PIC)
13484 {
13485 switch (fixp->fx_r_type)
13486 {
13487 default:
13488 abort ();
13489 case BFD_RELOC_MIPS_GOT16:
13490 break;
252b5132
RH
13491 case BFD_RELOC_MIPS_GOT_LO16:
13492 case BFD_RELOC_MIPS_CALL_LO16:
ed6fb7bd
SC
13493 if (HAVE_NEWABI)
13494 {
ed6fb7bd
SC
13495 fixp->fx_r_type = BFD_RELOC_MIPS_GOT_PAGE;
13496 reloc2->howto = bfd_reloc_type_lookup
13497 (stdoutput, BFD_RELOC_MIPS_GOT_OFST);
13498 }
13499 else
13500 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
13501 break;
f5040a92
AO
13502 case BFD_RELOC_MIPS_CALL16:
13503 case BFD_RELOC_MIPS_GOT_OFST:
13504 case BFD_RELOC_MIPS_GOT_DISP:
13505 if (HAVE_NEWABI)
13506 {
13507 /* It may seem nonsensical to relax GOT_DISP to
13508 GOT_DISP, but we're actually turning a GOT_DISP
13509 without offset into a GOT_DISP with an offset,
13510 getting rid of the separate addition, which we can
13511 do when the symbol is found to be local. */
13512 fixp->fx_r_type = BFD_RELOC_MIPS_GOT_DISP;
13513 retval[1] = NULL;
13514 }
13515 else
13516 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
13517 break;
252b5132
RH
13518 }
13519 }
13520 else
13521 abort ();
13522 }
13523
438c16b8
TS
13524 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
13525 entry to be used in the relocation's section offset. */
13526 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
252b5132
RH
13527 {
13528 reloc->address = reloc->addend;
13529 reloc->addend = 0;
13530 }
13531
13532 /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
13533 fixup_segment converted a non-PC relative reloc into a PC
13534 relative reloc. In such a case, we need to convert the reloc
13535 code. */
13536 code = fixp->fx_r_type;
13537 if (fixp->fx_pcrel)
13538 {
13539 switch (code)
13540 {
13541 case BFD_RELOC_8:
13542 code = BFD_RELOC_8_PCREL;
13543 break;
13544 case BFD_RELOC_16:
13545 code = BFD_RELOC_16_PCREL;
13546 break;
13547 case BFD_RELOC_32:
13548 code = BFD_RELOC_32_PCREL;
13549 break;
13550 case BFD_RELOC_64:
13551 code = BFD_RELOC_64_PCREL;
13552 break;
13553 case BFD_RELOC_8_PCREL:
13554 case BFD_RELOC_16_PCREL:
13555 case BFD_RELOC_32_PCREL:
13556 case BFD_RELOC_64_PCREL:
13557 case BFD_RELOC_16_PCREL_S2:
13558 case BFD_RELOC_PCREL_HI16_S:
13559 case BFD_RELOC_PCREL_LO16:
13560 break;
13561 default:
13562 as_bad_where (fixp->fx_file, fixp->fx_line,
13563 _("Cannot make %s relocation PC relative"),
13564 bfd_get_reloc_code_name (code));
13565 }
13566 }
13567
0b25d3e6
AO
13568 /* To support a PC relative reloc when generating embedded PIC code
13569 for ECOFF, we use a Cygnus extension. We check for that here to
13570 make sure that we don't let such a reloc escape normally. */
13571 if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
13572 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
13573 && code == BFD_RELOC_16_PCREL_S2
13574 && mips_pic != EMBEDDED_PIC)
13575 reloc->howto = NULL;
13576 else
13577 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
13578
252b5132
RH
13579 if (reloc->howto == NULL)
13580 {
13581 as_bad_where (fixp->fx_file, fixp->fx_line,
13582 _("Can not represent %s relocation in this object file format"),
13583 bfd_get_reloc_code_name (code));
13584 retval[0] = NULL;
13585 }
13586
13587 return retval;
13588}
13589
13590/* Relax a machine dependent frag. This returns the amount by which
13591 the current size of the frag should change. */
13592
13593int
17a2f251 13594mips_relax_frag (asection *sec, fragS *fragp, long stretch)
252b5132 13595{
4a6a3df4
AO
13596 if (RELAX_BRANCH_P (fragp->fr_subtype))
13597 {
13598 offsetT old_var = fragp->fr_var;
b34976b6
AM
13599
13600 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
4a6a3df4
AO
13601
13602 return fragp->fr_var - old_var;
13603 }
13604
252b5132
RH
13605 if (! RELAX_MIPS16_P (fragp->fr_subtype))
13606 return 0;
13607
c4e7957c 13608 if (mips16_extended_frag (fragp, NULL, stretch))
252b5132
RH
13609 {
13610 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13611 return 0;
13612 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
13613 return 2;
13614 }
13615 else
13616 {
13617 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13618 return 0;
13619 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
13620 return -2;
13621 }
13622
13623 return 0;
13624}
13625
13626/* Convert a machine dependent frag. */
13627
13628void
17a2f251 13629md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
252b5132
RH
13630{
13631 int old, new;
13632 char *fixptr;
13633
4a6a3df4
AO
13634 if (RELAX_BRANCH_P (fragp->fr_subtype))
13635 {
13636 bfd_byte *buf;
13637 unsigned long insn;
13638 expressionS exp;
13639 fixS *fixp;
b34976b6 13640
4a6a3df4
AO
13641 buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
13642
13643 if (target_big_endian)
13644 insn = bfd_getb32 (buf);
13645 else
13646 insn = bfd_getl32 (buf);
b34976b6 13647
4a6a3df4
AO
13648 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13649 {
13650 /* We generate a fixup instead of applying it right now
13651 because, if there are linker relaxations, we're going to
13652 need the relocations. */
13653 exp.X_op = O_symbol;
13654 exp.X_add_symbol = fragp->fr_symbol;
13655 exp.X_add_number = fragp->fr_offset;
13656
13657 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
0b25d3e6
AO
13658 4, &exp, 1,
13659 BFD_RELOC_16_PCREL_S2);
4a6a3df4
AO
13660 fixp->fx_file = fragp->fr_file;
13661 fixp->fx_line = fragp->fr_line;
b34976b6 13662
17a2f251 13663 md_number_to_chars (buf, insn, 4);
4a6a3df4
AO
13664 buf += 4;
13665 }
13666 else
13667 {
13668 int i;
13669
13670 as_warn_where (fragp->fr_file, fragp->fr_line,
13671 _("relaxed out-of-range branch into a jump"));
13672
13673 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
13674 goto uncond;
13675
13676 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13677 {
13678 /* Reverse the branch. */
13679 switch ((insn >> 28) & 0xf)
13680 {
13681 case 4:
13682 /* bc[0-3][tf]l? and bc1any[24][ft] instructions can
13683 have the condition reversed by tweaking a single
13684 bit, and their opcodes all have 0x4???????. */
13685 assert ((insn & 0xf1000000) == 0x41000000);
13686 insn ^= 0x00010000;
13687 break;
13688
13689 case 0:
13690 /* bltz 0x04000000 bgez 0x04010000
13691 bltzal 0x04100000 bgezal 0x04110000 */
13692 assert ((insn & 0xfc0e0000) == 0x04000000);
13693 insn ^= 0x00010000;
13694 break;
b34976b6 13695
4a6a3df4
AO
13696 case 1:
13697 /* beq 0x10000000 bne 0x14000000
13698 blez 0x18000000 bgtz 0x1c000000 */
13699 insn ^= 0x04000000;
13700 break;
13701
13702 default:
13703 abort ();
13704 }
13705 }
13706
13707 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13708 {
13709 /* Clear the and-link bit. */
13710 assert ((insn & 0xfc1c0000) == 0x04100000);
13711
13712 /* bltzal 0x04100000 bgezal 0x04110000
13713 bltzall 0x04120000 bgezall 0x04130000 */
13714 insn &= ~0x00100000;
13715 }
13716
13717 /* Branch over the branch (if the branch was likely) or the
13718 full jump (not likely case). Compute the offset from the
13719 current instruction to branch to. */
13720 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13721 i = 16;
13722 else
13723 {
13724 /* How many bytes in instructions we've already emitted? */
13725 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13726 /* How many bytes in instructions from here to the end? */
13727 i = fragp->fr_var - i;
13728 }
13729 /* Convert to instruction count. */
13730 i >>= 2;
13731 /* Branch counts from the next instruction. */
b34976b6 13732 i--;
4a6a3df4
AO
13733 insn |= i;
13734 /* Branch over the jump. */
17a2f251 13735 md_number_to_chars (buf, insn, 4);
4a6a3df4
AO
13736 buf += 4;
13737
13738 /* Nop */
17a2f251 13739 md_number_to_chars (buf, 0, 4);
4a6a3df4
AO
13740 buf += 4;
13741
13742 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13743 {
13744 /* beql $0, $0, 2f */
13745 insn = 0x50000000;
13746 /* Compute the PC offset from the current instruction to
13747 the end of the variable frag. */
13748 /* How many bytes in instructions we've already emitted? */
13749 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13750 /* How many bytes in instructions from here to the end? */
13751 i = fragp->fr_var - i;
13752 /* Convert to instruction count. */
13753 i >>= 2;
13754 /* Don't decrement i, because we want to branch over the
13755 delay slot. */
13756
13757 insn |= i;
17a2f251 13758 md_number_to_chars (buf, insn, 4);
4a6a3df4
AO
13759 buf += 4;
13760
17a2f251 13761 md_number_to_chars (buf, 0, 4);
4a6a3df4
AO
13762 buf += 4;
13763 }
13764
13765 uncond:
13766 if (mips_pic == NO_PIC)
13767 {
13768 /* j or jal. */
13769 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
13770 ? 0x0c000000 : 0x08000000);
13771 exp.X_op = O_symbol;
13772 exp.X_add_symbol = fragp->fr_symbol;
13773 exp.X_add_number = fragp->fr_offset;
13774
13775 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13776 4, &exp, 0, BFD_RELOC_MIPS_JMP);
13777 fixp->fx_file = fragp->fr_file;
13778 fixp->fx_line = fragp->fr_line;
13779
17a2f251 13780 md_number_to_chars (buf, insn, 4);
4a6a3df4
AO
13781 buf += 4;
13782 }
13783 else
13784 {
13785 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
13786 insn = HAVE_64BIT_ADDRESSES ? 0xdf810000 : 0x8f810000;
13787 exp.X_op = O_symbol;
13788 exp.X_add_symbol = fragp->fr_symbol;
13789 exp.X_add_number = fragp->fr_offset;
13790
13791 if (fragp->fr_offset)
13792 {
13793 exp.X_add_symbol = make_expr_symbol (&exp);
13794 exp.X_add_number = 0;
13795 }
13796
13797 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13798 4, &exp, 0, BFD_RELOC_MIPS_GOT16);
13799 fixp->fx_file = fragp->fr_file;
13800 fixp->fx_line = fragp->fr_line;
13801
17a2f251 13802 md_number_to_chars (buf, insn, 4);
4a6a3df4 13803 buf += 4;
b34976b6 13804
4a6a3df4
AO
13805 if (mips_opts.isa == ISA_MIPS1)
13806 {
13807 /* nop */
17a2f251 13808 md_number_to_chars (buf, 0, 4);
4a6a3df4
AO
13809 buf += 4;
13810 }
13811
13812 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
13813 insn = HAVE_64BIT_ADDRESSES ? 0x64210000 : 0x24210000;
13814
13815 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13816 4, &exp, 0, BFD_RELOC_LO16);
13817 fixp->fx_file = fragp->fr_file;
13818 fixp->fx_line = fragp->fr_line;
b34976b6 13819
17a2f251 13820 md_number_to_chars (buf, insn, 4);
4a6a3df4
AO
13821 buf += 4;
13822
13823 /* j(al)r $at. */
13824 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13825 insn = 0x0020f809;
13826 else
13827 insn = 0x00200008;
13828
17a2f251 13829 md_number_to_chars (buf, insn, 4);
4a6a3df4
AO
13830 buf += 4;
13831 }
13832 }
13833
13834 assert (buf == (bfd_byte *)fragp->fr_literal
13835 + fragp->fr_fix + fragp->fr_var);
13836
13837 fragp->fr_fix += fragp->fr_var;
13838
13839 return;
13840 }
13841
252b5132
RH
13842 if (RELAX_MIPS16_P (fragp->fr_subtype))
13843 {
13844 int type;
13845 register const struct mips16_immed_operand *op;
b34976b6 13846 bfd_boolean small, ext;
252b5132
RH
13847 offsetT val;
13848 bfd_byte *buf;
13849 unsigned long insn;
b34976b6 13850 bfd_boolean use_extend;
252b5132
RH
13851 unsigned short extend;
13852
13853 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
13854 op = mips16_immed_operands;
13855 while (op->type != type)
13856 ++op;
13857
13858 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13859 {
b34976b6
AM
13860 small = FALSE;
13861 ext = TRUE;
252b5132
RH
13862 }
13863 else
13864 {
b34976b6
AM
13865 small = TRUE;
13866 ext = FALSE;
252b5132
RH
13867 }
13868
6386f3a7 13869 resolve_symbol_value (fragp->fr_symbol);
252b5132
RH
13870 val = S_GET_VALUE (fragp->fr_symbol);
13871 if (op->pcrel)
13872 {
13873 addressT addr;
13874
13875 addr = fragp->fr_address + fragp->fr_fix;
13876
13877 /* The rules for the base address of a PC relative reloc are
13878 complicated; see mips16_extended_frag. */
13879 if (type == 'p' || type == 'q')
13880 {
13881 addr += 2;
13882 if (ext)
13883 addr += 2;
13884 /* Ignore the low bit in the target, since it will be
13885 set for a text label. */
13886 if ((val & 1) != 0)
13887 --val;
13888 }
13889 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13890 addr -= 4;
13891 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13892 addr -= 2;
13893
13894 addr &= ~ (addressT) ((1 << op->shift) - 1);
13895 val -= addr;
13896
13897 /* Make sure the section winds up with the alignment we have
13898 assumed. */
13899 if (op->shift > 0)
13900 record_alignment (asec, op->shift);
13901 }
13902
13903 if (ext
13904 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
13905 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
13906 as_warn_where (fragp->fr_file, fragp->fr_line,
13907 _("extended instruction in delay slot"));
13908
13909 buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
13910
13911 if (target_big_endian)
13912 insn = bfd_getb16 (buf);
13913 else
13914 insn = bfd_getl16 (buf);
13915
13916 mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
13917 RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
13918 small, ext, &insn, &use_extend, &extend);
13919
13920 if (use_extend)
13921 {
17a2f251 13922 md_number_to_chars (buf, 0xf000 | extend, 2);
252b5132
RH
13923 fragp->fr_fix += 2;
13924 buf += 2;
13925 }
13926
17a2f251 13927 md_number_to_chars (buf, insn, 2);
252b5132
RH
13928 fragp->fr_fix += 2;
13929 buf += 2;
13930 }
13931 else
13932 {
13933 if (fragp->fr_opcode == NULL)
13934 return;
13935
13936 old = RELAX_OLD (fragp->fr_subtype);
13937 new = RELAX_NEW (fragp->fr_subtype);
13938 fixptr = fragp->fr_literal + fragp->fr_fix;
13939
13940 if (new > 0)
f5040a92 13941 memmove (fixptr - old, fixptr, new);
252b5132
RH
13942
13943 fragp->fr_fix += new - old;
13944 }
13945}
13946
13947#ifdef OBJ_ELF
13948
13949/* This function is called after the relocs have been generated.
13950 We've been storing mips16 text labels as odd. Here we convert them
13951 back to even for the convenience of the debugger. */
13952
13953void
17a2f251 13954mips_frob_file_after_relocs (void)
252b5132
RH
13955{
13956 asymbol **syms;
13957 unsigned int count, i;
13958
13959 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
13960 return;
13961
13962 syms = bfd_get_outsymbols (stdoutput);
13963 count = bfd_get_symcount (stdoutput);
13964 for (i = 0; i < count; i++, syms++)
13965 {
13966 if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
13967 && ((*syms)->value & 1) != 0)
13968 {
13969 (*syms)->value &= ~1;
13970 /* If the symbol has an odd size, it was probably computed
13971 incorrectly, so adjust that as well. */
13972 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
13973 ++elf_symbol (*syms)->internal_elf_sym.st_size;
13974 }
13975 }
13976}
13977
13978#endif
13979
13980/* This function is called whenever a label is defined. It is used
13981 when handling branch delays; if a branch has a label, we assume we
13982 can not move it. */
13983
13984void
17a2f251 13985mips_define_label (symbolS *sym)
252b5132
RH
13986{
13987 struct insn_label_list *l;
13988
13989 if (free_insn_labels == NULL)
13990 l = (struct insn_label_list *) xmalloc (sizeof *l);
13991 else
13992 {
13993 l = free_insn_labels;
13994 free_insn_labels = l->next;
13995 }
13996
13997 l->label = sym;
13998 l->next = insn_labels;
13999 insn_labels = l;
14000}
14001\f
14002#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14003
14004/* Some special processing for a MIPS ELF file. */
14005
14006void
17a2f251 14007mips_elf_final_processing (void)
252b5132
RH
14008{
14009 /* Write out the register information. */
316f5878 14010 if (mips_abi != N64_ABI)
252b5132
RH
14011 {
14012 Elf32_RegInfo s;
14013
14014 s.ri_gprmask = mips_gprmask;
14015 s.ri_cprmask[0] = mips_cprmask[0];
14016 s.ri_cprmask[1] = mips_cprmask[1];
14017 s.ri_cprmask[2] = mips_cprmask[2];
14018 s.ri_cprmask[3] = mips_cprmask[3];
14019 /* The gp_value field is set by the MIPS ELF backend. */
14020
14021 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
14022 ((Elf32_External_RegInfo *)
14023 mips_regmask_frag));
14024 }
14025 else
14026 {
14027 Elf64_Internal_RegInfo s;
14028
14029 s.ri_gprmask = mips_gprmask;
14030 s.ri_pad = 0;
14031 s.ri_cprmask[0] = mips_cprmask[0];
14032 s.ri_cprmask[1] = mips_cprmask[1];
14033 s.ri_cprmask[2] = mips_cprmask[2];
14034 s.ri_cprmask[3] = mips_cprmask[3];
14035 /* The gp_value field is set by the MIPS ELF backend. */
14036
14037 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
14038 ((Elf64_External_RegInfo *)
14039 mips_regmask_frag));
14040 }
14041
14042 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
14043 sort of BFD interface for this. */
14044 if (mips_any_noreorder)
14045 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
14046 if (mips_pic != NO_PIC)
143d77c5 14047 {
252b5132 14048 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
143d77c5
EC
14049 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
14050 }
14051 if (mips_abicalls)
14052 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
252b5132 14053
98d3f06f 14054 /* Set MIPS ELF flags for ASEs. */
a4672219
TS
14055 if (file_ase_mips16)
14056 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
1f25f5d3
CD
14057#if 0 /* XXX FIXME */
14058 if (file_ase_mips3d)
14059 elf_elfheader (stdoutput)->e_flags |= ???;
14060#endif
deec1734
CD
14061 if (file_ase_mdmx)
14062 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
1f25f5d3 14063
bdaaa2e1 14064 /* Set the MIPS ELF ABI flags. */
316f5878 14065 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
252b5132 14066 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
316f5878 14067 else if (mips_abi == O64_ABI)
252b5132 14068 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
316f5878 14069 else if (mips_abi == EABI_ABI)
252b5132 14070 {
316f5878 14071 if (!file_mips_gp32)
252b5132
RH
14072 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
14073 else
14074 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
14075 }
316f5878 14076 else if (mips_abi == N32_ABI)
be00bddd
TS
14077 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
14078
c9914766 14079 /* Nothing to do for N64_ABI. */
252b5132
RH
14080
14081 if (mips_32bitmode)
14082 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
14083}
14084
14085#endif /* OBJ_ELF || OBJ_MAYBE_ELF */
14086\f
beae10d5
KH
14087typedef struct proc {
14088 symbolS *isym;
14089 unsigned long reg_mask;
14090 unsigned long reg_offset;
14091 unsigned long fpreg_mask;
14092 unsigned long fpreg_offset;
14093 unsigned long frame_offset;
14094 unsigned long frame_reg;
14095 unsigned long pc_reg;
14096} procS;
252b5132
RH
14097
14098static procS cur_proc;
14099static procS *cur_proc_ptr;
14100static int numprocs;
14101
0a9ef439 14102/* Fill in an rs_align_code fragment. */
a19d8eb0 14103
0a9ef439 14104void
17a2f251 14105mips_handle_align (fragS *fragp)
a19d8eb0 14106{
0a9ef439
RH
14107 if (fragp->fr_type != rs_align_code)
14108 return;
14109
14110 if (mips_opts.mips16)
a19d8eb0
CP
14111 {
14112 static const unsigned char be_nop[] = { 0x65, 0x00 };
14113 static const unsigned char le_nop[] = { 0x00, 0x65 };
14114
0a9ef439
RH
14115 int bytes;
14116 char *p;
a19d8eb0 14117
0a9ef439
RH
14118 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
14119 p = fragp->fr_literal + fragp->fr_fix;
14120
14121 if (bytes & 1)
14122 {
14123 *p++ = 0;
f9419b05 14124 fragp->fr_fix++;
0a9ef439
RH
14125 }
14126
14127 memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
14128 fragp->fr_var = 2;
a19d8eb0
CP
14129 }
14130
0a9ef439 14131 /* For mips32, a nop is a zero, which we trivially get by doing nothing. */
a19d8eb0
CP
14132}
14133
252b5132 14134static void
17a2f251 14135md_obj_begin (void)
252b5132
RH
14136{
14137}
14138
14139static void
17a2f251 14140md_obj_end (void)
252b5132
RH
14141{
14142 /* check for premature end, nesting errors, etc */
14143 if (cur_proc_ptr)
9a41af64 14144 as_warn (_("missing .end at end of assembly"));
252b5132
RH
14145}
14146
14147static long
17a2f251 14148get_number (void)
252b5132
RH
14149{
14150 int negative = 0;
14151 long val = 0;
14152
14153 if (*input_line_pointer == '-')
14154 {
14155 ++input_line_pointer;
14156 negative = 1;
14157 }
3882b010 14158 if (!ISDIGIT (*input_line_pointer))
956cd1d6 14159 as_bad (_("expected simple number"));
252b5132
RH
14160 if (input_line_pointer[0] == '0')
14161 {
14162 if (input_line_pointer[1] == 'x')
14163 {
14164 input_line_pointer += 2;
3882b010 14165 while (ISXDIGIT (*input_line_pointer))
252b5132
RH
14166 {
14167 val <<= 4;
14168 val |= hex_value (*input_line_pointer++);
14169 }
14170 return negative ? -val : val;
14171 }
14172 else
14173 {
14174 ++input_line_pointer;
3882b010 14175 while (ISDIGIT (*input_line_pointer))
252b5132
RH
14176 {
14177 val <<= 3;
14178 val |= *input_line_pointer++ - '0';
14179 }
14180 return negative ? -val : val;
14181 }
14182 }
3882b010 14183 if (!ISDIGIT (*input_line_pointer))
252b5132
RH
14184 {
14185 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
14186 *input_line_pointer, *input_line_pointer);
956cd1d6 14187 as_warn (_("invalid number"));
252b5132
RH
14188 return -1;
14189 }
3882b010 14190 while (ISDIGIT (*input_line_pointer))
252b5132
RH
14191 {
14192 val *= 10;
14193 val += *input_line_pointer++ - '0';
14194 }
14195 return negative ? -val : val;
14196}
14197
14198/* The .file directive; just like the usual .file directive, but there
c5dd6aab
DJ
14199 is an initial number which is the ECOFF file index. In the non-ECOFF
14200 case .file implies DWARF-2. */
14201
14202static void
17a2f251 14203s_mips_file (int x ATTRIBUTE_UNUSED)
c5dd6aab 14204{
ecb4347a
DJ
14205 static int first_file_directive = 0;
14206
c5dd6aab
DJ
14207 if (ECOFF_DEBUGGING)
14208 {
14209 get_number ();
14210 s_app_file (0);
14211 }
14212 else
ecb4347a
DJ
14213 {
14214 char *filename;
14215
14216 filename = dwarf2_directive_file (0);
14217
14218 /* Versions of GCC up to 3.1 start files with a ".file"
14219 directive even for stabs output. Make sure that this
14220 ".file" is handled. Note that you need a version of GCC
14221 after 3.1 in order to support DWARF-2 on MIPS. */
14222 if (filename != NULL && ! first_file_directive)
14223 {
14224 (void) new_logical_line (filename, -1);
14225 s_app_file_string (filename);
14226 }
14227 first_file_directive = 1;
14228 }
c5dd6aab
DJ
14229}
14230
14231/* The .loc directive, implying DWARF-2. */
252b5132
RH
14232
14233static void
17a2f251 14234s_mips_loc (int x ATTRIBUTE_UNUSED)
252b5132 14235{
c5dd6aab
DJ
14236 if (!ECOFF_DEBUGGING)
14237 dwarf2_directive_loc (0);
252b5132
RH
14238}
14239
252b5132
RH
14240/* The .end directive. */
14241
14242static void
17a2f251 14243s_mips_end (int x ATTRIBUTE_UNUSED)
252b5132
RH
14244{
14245 symbolS *p;
252b5132 14246
7a621144
DJ
14247 /* Following functions need their own .frame and .cprestore directives. */
14248 mips_frame_reg_valid = 0;
14249 mips_cprestore_valid = 0;
14250
252b5132
RH
14251 if (!is_end_of_line[(unsigned char) *input_line_pointer])
14252 {
14253 p = get_symbol ();
14254 demand_empty_rest_of_line ();
14255 }
14256 else
14257 p = NULL;
14258
14949570 14259 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
14260 as_warn (_(".end not in text section"));
14261
14262 if (!cur_proc_ptr)
14263 {
14264 as_warn (_(".end directive without a preceding .ent directive."));
14265 demand_empty_rest_of_line ();
14266 return;
14267 }
14268
14269 if (p != NULL)
14270 {
14271 assert (S_GET_NAME (p));
14272 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->isym)))
14273 as_warn (_(".end symbol does not match .ent symbol."));
ecb4347a
DJ
14274
14275 if (debug_type == DEBUG_STABS)
14276 stabs_generate_asm_endfunc (S_GET_NAME (p),
14277 S_GET_NAME (p));
252b5132
RH
14278 }
14279 else
14280 as_warn (_(".end directive missing or unknown symbol"));
14281
ecb4347a
DJ
14282#ifdef OBJ_ELF
14283 /* Generate a .pdr section. */
dcd410fe
RO
14284 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING
14285 && mips_flag_pdr)
ecb4347a
DJ
14286 {
14287 segT saved_seg = now_seg;
14288 subsegT saved_subseg = now_subseg;
14289 valueT dot;
14290 expressionS exp;
14291 char *fragp;
252b5132 14292
ecb4347a 14293 dot = frag_now_fix ();
252b5132
RH
14294
14295#ifdef md_flush_pending_output
ecb4347a 14296 md_flush_pending_output ();
252b5132
RH
14297#endif
14298
ecb4347a
DJ
14299 assert (pdr_seg);
14300 subseg_set (pdr_seg, 0);
252b5132 14301
ecb4347a
DJ
14302 /* Write the symbol. */
14303 exp.X_op = O_symbol;
14304 exp.X_add_symbol = p;
14305 exp.X_add_number = 0;
14306 emit_expr (&exp, 4);
252b5132 14307
ecb4347a 14308 fragp = frag_more (7 * 4);
252b5132 14309
17a2f251
TS
14310 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
14311 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
14312 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
14313 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
14314 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
14315 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
14316 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
252b5132 14317
ecb4347a
DJ
14318 subseg_set (saved_seg, saved_subseg);
14319 }
14320#endif /* OBJ_ELF */
252b5132
RH
14321
14322 cur_proc_ptr = NULL;
14323}
14324
14325/* The .aent and .ent directives. */
14326
14327static void
17a2f251 14328s_mips_ent (int aent)
252b5132 14329{
252b5132 14330 symbolS *symbolP;
252b5132
RH
14331
14332 symbolP = get_symbol ();
14333 if (*input_line_pointer == ',')
f9419b05 14334 ++input_line_pointer;
252b5132 14335 SKIP_WHITESPACE ();
3882b010 14336 if (ISDIGIT (*input_line_pointer)
d9a62219 14337 || *input_line_pointer == '-')
874e8986 14338 get_number ();
252b5132 14339
14949570 14340 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
14341 as_warn (_(".ent or .aent not in text section."));
14342
14343 if (!aent && cur_proc_ptr)
9a41af64 14344 as_warn (_("missing .end"));
252b5132
RH
14345
14346 if (!aent)
14347 {
7a621144
DJ
14348 /* This function needs its own .frame and .cprestore directives. */
14349 mips_frame_reg_valid = 0;
14350 mips_cprestore_valid = 0;
14351
252b5132
RH
14352 cur_proc_ptr = &cur_proc;
14353 memset (cur_proc_ptr, '\0', sizeof (procS));
14354
14355 cur_proc_ptr->isym = symbolP;
14356
49309057 14357 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
252b5132 14358
f9419b05 14359 ++numprocs;
ecb4347a
DJ
14360
14361 if (debug_type == DEBUG_STABS)
14362 stabs_generate_asm_func (S_GET_NAME (symbolP),
14363 S_GET_NAME (symbolP));
252b5132
RH
14364 }
14365
14366 demand_empty_rest_of_line ();
14367}
14368
14369/* The .frame directive. If the mdebug section is present (IRIX 5 native)
bdaaa2e1 14370 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
252b5132 14371 s_mips_frame is used so that we can set the PDR information correctly.
bdaaa2e1 14372 We can't use the ecoff routines because they make reference to the ecoff
252b5132
RH
14373 symbol table (in the mdebug section). */
14374
14375static void
17a2f251 14376s_mips_frame (int ignore ATTRIBUTE_UNUSED)
252b5132 14377{
ecb4347a
DJ
14378#ifdef OBJ_ELF
14379 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14380 {
14381 long val;
252b5132 14382
ecb4347a
DJ
14383 if (cur_proc_ptr == (procS *) NULL)
14384 {
14385 as_warn (_(".frame outside of .ent"));
14386 demand_empty_rest_of_line ();
14387 return;
14388 }
252b5132 14389
ecb4347a
DJ
14390 cur_proc_ptr->frame_reg = tc_get_register (1);
14391
14392 SKIP_WHITESPACE ();
14393 if (*input_line_pointer++ != ','
14394 || get_absolute_expression_and_terminator (&val) != ',')
14395 {
14396 as_warn (_("Bad .frame directive"));
14397 --input_line_pointer;
14398 demand_empty_rest_of_line ();
14399 return;
14400 }
252b5132 14401
ecb4347a
DJ
14402 cur_proc_ptr->frame_offset = val;
14403 cur_proc_ptr->pc_reg = tc_get_register (0);
252b5132 14404
252b5132 14405 demand_empty_rest_of_line ();
252b5132 14406 }
ecb4347a
DJ
14407 else
14408#endif /* OBJ_ELF */
14409 s_ignore (ignore);
252b5132
RH
14410}
14411
bdaaa2e1
KH
14412/* The .fmask and .mask directives. If the mdebug section is present
14413 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
252b5132 14414 embedded targets, s_mips_mask is used so that we can set the PDR
bdaaa2e1 14415 information correctly. We can't use the ecoff routines because they
252b5132
RH
14416 make reference to the ecoff symbol table (in the mdebug section). */
14417
14418static void
17a2f251 14419s_mips_mask (int reg_type)
252b5132 14420{
ecb4347a
DJ
14421#ifdef OBJ_ELF
14422 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
252b5132 14423 {
ecb4347a 14424 long mask, off;
252b5132 14425
ecb4347a
DJ
14426 if (cur_proc_ptr == (procS *) NULL)
14427 {
14428 as_warn (_(".mask/.fmask outside of .ent"));
14429 demand_empty_rest_of_line ();
14430 return;
14431 }
252b5132 14432
ecb4347a
DJ
14433 if (get_absolute_expression_and_terminator (&mask) != ',')
14434 {
14435 as_warn (_("Bad .mask/.fmask directive"));
14436 --input_line_pointer;
14437 demand_empty_rest_of_line ();
14438 return;
14439 }
252b5132 14440
ecb4347a
DJ
14441 off = get_absolute_expression ();
14442
14443 if (reg_type == 'F')
14444 {
14445 cur_proc_ptr->fpreg_mask = mask;
14446 cur_proc_ptr->fpreg_offset = off;
14447 }
14448 else
14449 {
14450 cur_proc_ptr->reg_mask = mask;
14451 cur_proc_ptr->reg_offset = off;
14452 }
14453
14454 demand_empty_rest_of_line ();
252b5132
RH
14455 }
14456 else
ecb4347a
DJ
14457#endif /* OBJ_ELF */
14458 s_ignore (reg_type);
252b5132
RH
14459}
14460
14461/* The .loc directive. */
14462
14463#if 0
14464static void
17a2f251 14465s_loc (int x)
252b5132
RH
14466{
14467 symbolS *symbolP;
14468 int lineno;
14469 int addroff;
14470
14471 assert (now_seg == text_section);
14472
14473 lineno = get_number ();
14474 addroff = frag_now_fix ();
14475
14476 symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
14477 S_SET_TYPE (symbolP, N_SLINE);
14478 S_SET_OTHER (symbolP, 0);
14479 S_SET_DESC (symbolP, lineno);
14480 symbolP->sy_segment = now_seg;
14481}
14482#endif
e7af610e 14483
316f5878
RS
14484/* A table describing all the processors gas knows about. Names are
14485 matched in the order listed.
e7af610e 14486
316f5878
RS
14487 To ease comparison, please keep this table in the same order as
14488 gcc's mips_cpu_info_table[]. */
e972090a
NC
14489static const struct mips_cpu_info mips_cpu_info_table[] =
14490{
316f5878
RS
14491 /* Entries for generic ISAs */
14492 { "mips1", 1, ISA_MIPS1, CPU_R3000 },
14493 { "mips2", 1, ISA_MIPS2, CPU_R6000 },
14494 { "mips3", 1, ISA_MIPS3, CPU_R4000 },
14495 { "mips4", 1, ISA_MIPS4, CPU_R8000 },
14496 { "mips5", 1, ISA_MIPS5, CPU_MIPS5 },
14497 { "mips32", 1, ISA_MIPS32, CPU_MIPS32 },
af7ee8bf 14498 { "mips32r2", 1, ISA_MIPS32R2, CPU_MIPS32R2 },
316f5878 14499 { "mips64", 1, ISA_MIPS64, CPU_MIPS64 },
5f74bc13 14500 { "mips64r2", 1, ISA_MIPS64R2, CPU_MIPS64R2 },
316f5878
RS
14501
14502 /* MIPS I */
14503 { "r3000", 0, ISA_MIPS1, CPU_R3000 },
14504 { "r2000", 0, ISA_MIPS1, CPU_R3000 },
14505 { "r3900", 0, ISA_MIPS1, CPU_R3900 },
14506
14507 /* MIPS II */
14508 { "r6000", 0, ISA_MIPS2, CPU_R6000 },
14509
14510 /* MIPS III */
14511 { "r4000", 0, ISA_MIPS3, CPU_R4000 },
14512 { "r4010", 0, ISA_MIPS2, CPU_R4010 },
14513 { "vr4100", 0, ISA_MIPS3, CPU_VR4100 },
14514 { "vr4111", 0, ISA_MIPS3, CPU_R4111 },
60b63b72
RS
14515 { "vr4120", 0, ISA_MIPS3, CPU_VR4120 },
14516 { "vr4130", 0, ISA_MIPS3, CPU_VR4120 },
14517 { "vr4181", 0, ISA_MIPS3, CPU_R4111 },
316f5878
RS
14518 { "vr4300", 0, ISA_MIPS3, CPU_R4300 },
14519 { "r4400", 0, ISA_MIPS3, CPU_R4400 },
14520 { "r4600", 0, ISA_MIPS3, CPU_R4600 },
14521 { "orion", 0, ISA_MIPS3, CPU_R4600 },
14522 { "r4650", 0, ISA_MIPS3, CPU_R4650 },
14523
14524 /* MIPS IV */
14525 { "r8000", 0, ISA_MIPS4, CPU_R8000 },
14526 { "r10000", 0, ISA_MIPS4, CPU_R10000 },
14527 { "r12000", 0, ISA_MIPS4, CPU_R12000 },
14528 { "vr5000", 0, ISA_MIPS4, CPU_R5000 },
60b63b72
RS
14529 { "vr5400", 0, ISA_MIPS4, CPU_VR5400 },
14530 { "vr5500", 0, ISA_MIPS4, CPU_VR5500 },
316f5878
RS
14531 { "rm5200", 0, ISA_MIPS4, CPU_R5000 },
14532 { "rm5230", 0, ISA_MIPS4, CPU_R5000 },
14533 { "rm5231", 0, ISA_MIPS4, CPU_R5000 },
14534 { "rm5261", 0, ISA_MIPS4, CPU_R5000 },
14535 { "rm5721", 0, ISA_MIPS4, CPU_R5000 },
5a7ea749
RS
14536 { "rm7000", 0, ISA_MIPS4, CPU_RM7000 },
14537 { "rm9000", 0, ISA_MIPS4, CPU_RM7000 },
316f5878
RS
14538
14539 /* MIPS 32 */
fef14a42 14540 { "4kc", 0, ISA_MIPS32, CPU_MIPS32 },
316f5878
RS
14541 { "4km", 0, ISA_MIPS32, CPU_MIPS32 },
14542 { "4kp", 0, ISA_MIPS32, CPU_MIPS32 },
e7af610e 14543
316f5878
RS
14544 /* MIPS 64 */
14545 { "5kc", 0, ISA_MIPS64, CPU_MIPS64 },
14546 { "20kc", 0, ISA_MIPS64, CPU_MIPS64 },
e7af610e 14547
c7a23324 14548 /* Broadcom SB-1 CPU core */
316f5878 14549 { "sb1", 0, ISA_MIPS64, CPU_SB1 },
e7af610e 14550
316f5878
RS
14551 /* End marker */
14552 { NULL, 0, 0, 0 }
14553};
e7af610e 14554
84ea6cf2 14555
316f5878
RS
14556/* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
14557 with a final "000" replaced by "k". Ignore case.
e7af610e 14558
316f5878 14559 Note: this function is shared between GCC and GAS. */
c6c98b38 14560
b34976b6 14561static bfd_boolean
17a2f251 14562mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
14563{
14564 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
14565 given++, canonical++;
14566
14567 return ((*given == 0 && *canonical == 0)
14568 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
14569}
14570
14571
14572/* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
14573 CPU name. We've traditionally allowed a lot of variation here.
14574
14575 Note: this function is shared between GCC and GAS. */
14576
b34976b6 14577static bfd_boolean
17a2f251 14578mips_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
14579{
14580 /* First see if the name matches exactly, or with a final "000"
14581 turned into "k". */
14582 if (mips_strict_matching_cpu_name_p (canonical, given))
b34976b6 14583 return TRUE;
316f5878
RS
14584
14585 /* If not, try comparing based on numerical designation alone.
14586 See if GIVEN is an unadorned number, or 'r' followed by a number. */
14587 if (TOLOWER (*given) == 'r')
14588 given++;
14589 if (!ISDIGIT (*given))
b34976b6 14590 return FALSE;
316f5878
RS
14591
14592 /* Skip over some well-known prefixes in the canonical name,
14593 hoping to find a number there too. */
14594 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
14595 canonical += 2;
14596 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
14597 canonical += 2;
14598 else if (TOLOWER (canonical[0]) == 'r')
14599 canonical += 1;
14600
14601 return mips_strict_matching_cpu_name_p (canonical, given);
14602}
14603
14604
14605/* Parse an option that takes the name of a processor as its argument.
14606 OPTION is the name of the option and CPU_STRING is the argument.
14607 Return the corresponding processor enumeration if the CPU_STRING is
14608 recognized, otherwise report an error and return null.
14609
14610 A similar function exists in GCC. */
e7af610e
NC
14611
14612static const struct mips_cpu_info *
17a2f251 14613mips_parse_cpu (const char *option, const char *cpu_string)
e7af610e 14614{
316f5878 14615 const struct mips_cpu_info *p;
e7af610e 14616
316f5878
RS
14617 /* 'from-abi' selects the most compatible architecture for the given
14618 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
14619 EABIs, we have to decide whether we're using the 32-bit or 64-bit
14620 version. Look first at the -mgp options, if given, otherwise base
14621 the choice on MIPS_DEFAULT_64BIT.
e7af610e 14622
316f5878
RS
14623 Treat NO_ABI like the EABIs. One reason to do this is that the
14624 plain 'mips' and 'mips64' configs have 'from-abi' as their default
14625 architecture. This code picks MIPS I for 'mips' and MIPS III for
14626 'mips64', just as we did in the days before 'from-abi'. */
14627 if (strcasecmp (cpu_string, "from-abi") == 0)
14628 {
14629 if (ABI_NEEDS_32BIT_REGS (mips_abi))
14630 return mips_cpu_info_from_isa (ISA_MIPS1);
14631
14632 if (ABI_NEEDS_64BIT_REGS (mips_abi))
14633 return mips_cpu_info_from_isa (ISA_MIPS3);
14634
14635 if (file_mips_gp32 >= 0)
14636 return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
14637
14638 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
14639 ? ISA_MIPS3
14640 : ISA_MIPS1);
14641 }
14642
14643 /* 'default' has traditionally been a no-op. Probably not very useful. */
14644 if (strcasecmp (cpu_string, "default") == 0)
14645 return 0;
14646
14647 for (p = mips_cpu_info_table; p->name != 0; p++)
14648 if (mips_matching_cpu_name_p (p->name, cpu_string))
14649 return p;
14650
14651 as_bad ("Bad value (%s) for %s", cpu_string, option);
14652 return 0;
e7af610e
NC
14653}
14654
316f5878
RS
14655/* Return the canonical processor information for ISA (a member of the
14656 ISA_MIPS* enumeration). */
14657
e7af610e 14658static const struct mips_cpu_info *
17a2f251 14659mips_cpu_info_from_isa (int isa)
e7af610e
NC
14660{
14661 int i;
14662
14663 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14664 if (mips_cpu_info_table[i].is_isa
316f5878 14665 && isa == mips_cpu_info_table[i].isa)
e7af610e
NC
14666 return (&mips_cpu_info_table[i]);
14667
e972090a 14668 return NULL;
e7af610e 14669}
fef14a42
TS
14670
14671static const struct mips_cpu_info *
17a2f251 14672mips_cpu_info_from_arch (int arch)
fef14a42
TS
14673{
14674 int i;
14675
14676 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14677 if (arch == mips_cpu_info_table[i].cpu)
14678 return (&mips_cpu_info_table[i]);
14679
14680 return NULL;
14681}
316f5878
RS
14682\f
14683static void
17a2f251 14684show (FILE *stream, const char *string, int *col_p, int *first_p)
316f5878
RS
14685{
14686 if (*first_p)
14687 {
14688 fprintf (stream, "%24s", "");
14689 *col_p = 24;
14690 }
14691 else
14692 {
14693 fprintf (stream, ", ");
14694 *col_p += 2;
14695 }
e7af610e 14696
316f5878
RS
14697 if (*col_p + strlen (string) > 72)
14698 {
14699 fprintf (stream, "\n%24s", "");
14700 *col_p = 24;
14701 }
14702
14703 fprintf (stream, "%s", string);
14704 *col_p += strlen (string);
14705
14706 *first_p = 0;
14707}
14708
14709void
17a2f251 14710md_show_usage (FILE *stream)
e7af610e 14711{
316f5878
RS
14712 int column, first;
14713 size_t i;
14714
14715 fprintf (stream, _("\
14716MIPS options:\n\
14717-membedded-pic generate embedded position independent code\n\
14718-EB generate big endian output\n\
14719-EL generate little endian output\n\
14720-g, -g2 do not remove unneeded NOPs or swap branches\n\
14721-G NUM allow referencing objects up to NUM bytes\n\
14722 implicitly with the gp register [default 8]\n"));
14723 fprintf (stream, _("\
14724-mips1 generate MIPS ISA I instructions\n\
14725-mips2 generate MIPS ISA II instructions\n\
14726-mips3 generate MIPS ISA III instructions\n\
14727-mips4 generate MIPS ISA IV instructions\n\
14728-mips5 generate MIPS ISA V instructions\n\
14729-mips32 generate MIPS32 ISA instructions\n\
af7ee8bf 14730-mips32r2 generate MIPS32 release 2 ISA instructions\n\
316f5878 14731-mips64 generate MIPS64 ISA instructions\n\
5f74bc13 14732-mips64r2 generate MIPS64 release 2 ISA instructions\n\
316f5878
RS
14733-march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
14734
14735 first = 1;
e7af610e
NC
14736
14737 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
316f5878
RS
14738 show (stream, mips_cpu_info_table[i].name, &column, &first);
14739 show (stream, "from-abi", &column, &first);
14740 fputc ('\n', stream);
e7af610e 14741
316f5878
RS
14742 fprintf (stream, _("\
14743-mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
14744-no-mCPU don't generate code specific to CPU.\n\
14745 For -mCPU and -no-mCPU, CPU must be one of:\n"));
14746
14747 first = 1;
14748
14749 show (stream, "3900", &column, &first);
14750 show (stream, "4010", &column, &first);
14751 show (stream, "4100", &column, &first);
14752 show (stream, "4650", &column, &first);
14753 fputc ('\n', stream);
14754
14755 fprintf (stream, _("\
14756-mips16 generate mips16 instructions\n\
14757-no-mips16 do not generate mips16 instructions\n"));
14758 fprintf (stream, _("\
14759-mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
14760-mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
14761-O0 remove unneeded NOPs, do not swap branches\n\
14762-O remove unneeded NOPs and swap branches\n\
14763-n warn about NOPs generated from macros\n\
14764--[no-]construct-floats [dis]allow floating point values to be constructed\n\
14765--trap, --no-break trap exception on div by 0 and mult overflow\n\
14766--break, --no-trap break exception on div by 0 and mult overflow\n"));
14767#ifdef OBJ_ELF
14768 fprintf (stream, _("\
14769-KPIC, -call_shared generate SVR4 position independent code\n\
14770-non_shared do not generate position independent code\n\
14771-xgot assume a 32 bit GOT\n\
dcd410fe 14772-mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
316f5878
RS
14773-mabi=ABI create ABI conformant object file for:\n"));
14774
14775 first = 1;
14776
14777 show (stream, "32", &column, &first);
14778 show (stream, "o64", &column, &first);
14779 show (stream, "n32", &column, &first);
14780 show (stream, "64", &column, &first);
14781 show (stream, "eabi", &column, &first);
14782
14783 fputc ('\n', stream);
14784
14785 fprintf (stream, _("\
14786-32 create o32 ABI object file (default)\n\
14787-n32 create n32 ABI object file\n\
14788-64 create 64 ABI object file\n"));
14789#endif
e7af610e 14790}
14e777e0
KB
14791
14792enum dwarf2_format
17a2f251 14793mips_dwarf2_format (void)
14e777e0
KB
14794{
14795 if (mips_abi == N64_ABI)
1de5b6a1
AO
14796 {
14797#ifdef TE_IRIX
14798 return dwarf2_format_64bit_irix;
14799#else
14800 return dwarf2_format_64bit;
14801#endif
14802 }
14e777e0
KB
14803 else
14804 return dwarf2_format_32bit;
14805}
This page took 1.755307 seconds and 4 git commands to generate.