]> Git Repo - binutils.git/blame - gas/config/tc-mips.c
2004-04-22 Randolph Chung <[email protected]>
[binutils.git] / gas / config / tc-mips.c
CommitLineData
252b5132 1/* tc-mips.c -- assemble code for a MIPS chip.
81912461
ILT
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3 2003, 2004 Free Software Foundation, Inc.
252b5132
RH
4 Contributed by the OSF and Ralph Campbell.
5 Written by Keith Knowles and Ralph Campbell, working independently.
6 Modified for ECOFF and R4000 support by Ian Lance Taylor of Cygnus
7 Support.
8
9 This file is part of GAS.
10
11 GAS is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2, or (at your option)
14 any later version.
15
16 GAS is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with GAS; see the file COPYING. If not, write to the Free
23 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 02111-1307, USA. */
25
26#include "as.h"
27#include "config.h"
28#include "subsegs.h"
3882b010 29#include "safe-ctype.h"
252b5132 30
252b5132 31#include <stdarg.h>
252b5132
RH
32
33#include "opcode/mips.h"
34#include "itbl-ops.h"
c5dd6aab 35#include "dwarf2dbg.h"
252b5132
RH
36
37#ifdef DEBUG
38#define DBG(x) printf x
39#else
40#define DBG(x)
41#endif
42
43#ifdef OBJ_MAYBE_ELF
44/* Clean up namespace so we can include obj-elf.h too. */
17a2f251
TS
45static int mips_output_flavor (void);
46static int mips_output_flavor (void) { return OUTPUT_FLAVOR; }
252b5132
RH
47#undef OBJ_PROCESS_STAB
48#undef OUTPUT_FLAVOR
49#undef S_GET_ALIGN
50#undef S_GET_SIZE
51#undef S_SET_ALIGN
52#undef S_SET_SIZE
252b5132
RH
53#undef obj_frob_file
54#undef obj_frob_file_after_relocs
55#undef obj_frob_symbol
56#undef obj_pop_insert
57#undef obj_sec_sym_ok_for_reloc
58#undef OBJ_COPY_SYMBOL_ATTRIBUTES
59
60#include "obj-elf.h"
61/* Fix any of them that we actually care about. */
62#undef OUTPUT_FLAVOR
63#define OUTPUT_FLAVOR mips_output_flavor()
64#endif
65
66#if defined (OBJ_ELF)
67#include "elf/mips.h"
68#endif
69
70#ifndef ECOFF_DEBUGGING
71#define NO_ECOFF_DEBUGGING
72#define ECOFF_DEBUGGING 0
73#endif
74
ecb4347a
DJ
75int mips_flag_mdebug = -1;
76
dcd410fe
RO
77/* Control generation of .pdr sections. Off by default on IRIX: the native
78 linker doesn't know about and discards them, but relocations against them
79 remain, leading to rld crashes. */
80#ifdef TE_IRIX
81int mips_flag_pdr = FALSE;
82#else
83int mips_flag_pdr = TRUE;
84#endif
85
252b5132
RH
86#include "ecoff.h"
87
88#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
89static char *mips_regmask_frag;
90#endif
91
85b51719 92#define ZERO 0
252b5132
RH
93#define AT 1
94#define TREG 24
95#define PIC_CALL_REG 25
96#define KT0 26
97#define KT1 27
98#define GP 28
99#define SP 29
100#define FP 30
101#define RA 31
102
103#define ILLEGAL_REG (32)
104
105/* Allow override of standard little-endian ECOFF format. */
106
107#ifndef ECOFF_LITTLE_FORMAT
108#define ECOFF_LITTLE_FORMAT "ecoff-littlemips"
109#endif
110
111extern int target_big_endian;
112
252b5132 113/* The name of the readonly data section. */
4d0d148d 114#define RDATA_SECTION_NAME (OUTPUT_FLAVOR == bfd_target_ecoff_flavour \
252b5132 115 ? ".rdata" \
056350c6
NC
116 : OUTPUT_FLAVOR == bfd_target_coff_flavour \
117 ? ".rdata" \
252b5132
RH
118 : OUTPUT_FLAVOR == bfd_target_elf_flavour \
119 ? ".rodata" \
120 : (abort (), ""))
121
a325df1d
TS
122/* The ABI to use. */
123enum mips_abi_level
124{
125 NO_ABI = 0,
126 O32_ABI,
127 O64_ABI,
128 N32_ABI,
129 N64_ABI,
130 EABI_ABI
131};
132
133/* MIPS ABI we are using for this output file. */
316f5878 134static enum mips_abi_level mips_abi = NO_ABI;
a325df1d 135
143d77c5
EC
136/* Whether or not we have code that can call pic code. */
137int mips_abicalls = FALSE;
138
252b5132
RH
139/* This is the set of options which may be modified by the .set
140 pseudo-op. We use a struct so that .set push and .set pop are more
141 reliable. */
142
e972090a
NC
143struct mips_set_options
144{
252b5132
RH
145 /* MIPS ISA (Instruction Set Architecture) level. This is set to -1
146 if it has not been initialized. Changed by `.set mipsN', and the
147 -mipsN command line option, and the default CPU. */
148 int isa;
1f25f5d3
CD
149 /* Enabled Application Specific Extensions (ASEs). These are set to -1
150 if they have not been initialized. Changed by `.set <asename>', by
151 command line options, and based on the default architecture. */
152 int ase_mips3d;
deec1734 153 int ase_mdmx;
252b5132
RH
154 /* Whether we are assembling for the mips16 processor. 0 if we are
155 not, 1 if we are, and -1 if the value has not been initialized.
156 Changed by `.set mips16' and `.set nomips16', and the -mips16 and
157 -nomips16 command line options, and the default CPU. */
158 int mips16;
159 /* Non-zero if we should not reorder instructions. Changed by `.set
160 reorder' and `.set noreorder'. */
161 int noreorder;
162 /* Non-zero if we should not permit the $at ($1) register to be used
163 in instructions. Changed by `.set at' and `.set noat'. */
164 int noat;
165 /* Non-zero if we should warn when a macro instruction expands into
166 more than one machine instruction. Changed by `.set nomacro' and
167 `.set macro'. */
168 int warn_about_macros;
169 /* Non-zero if we should not move instructions. Changed by `.set
170 move', `.set volatile', `.set nomove', and `.set novolatile'. */
171 int nomove;
172 /* Non-zero if we should not optimize branches by moving the target
173 of the branch into the delay slot. Actually, we don't perform
174 this optimization anyhow. Changed by `.set bopt' and `.set
175 nobopt'. */
176 int nobopt;
177 /* Non-zero if we should not autoextend mips16 instructions.
178 Changed by `.set autoextend' and `.set noautoextend'. */
179 int noautoextend;
a325df1d
TS
180 /* Restrict general purpose registers and floating point registers
181 to 32 bit. This is initially determined when -mgp32 or -mfp32
182 is passed but can changed if the assembler code uses .set mipsN. */
183 int gp32;
184 int fp32;
fef14a42
TS
185 /* MIPS architecture (CPU) type. Changed by .set arch=FOO, the -march
186 command line option, and the default CPU. */
187 int arch;
252b5132
RH
188};
189
a325df1d 190/* True if -mgp32 was passed. */
a8e8e863 191static int file_mips_gp32 = -1;
a325df1d
TS
192
193/* True if -mfp32 was passed. */
a8e8e863 194static int file_mips_fp32 = -1;
a325df1d 195
252b5132 196/* This is the struct we use to hold the current set of options. Note
a4672219 197 that we must set the isa field to ISA_UNKNOWN and the ASE fields to
e7af610e 198 -1 to indicate that they have not been initialized. */
252b5132 199
e972090a
NC
200static struct mips_set_options mips_opts =
201{
fef14a42 202 ISA_UNKNOWN, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, CPU_UNKNOWN
e7af610e 203};
252b5132
RH
204
205/* These variables are filled in with the masks of registers used.
206 The object format code reads them and puts them in the appropriate
207 place. */
208unsigned long mips_gprmask;
209unsigned long mips_cprmask[4];
210
211/* MIPS ISA we are using for this output file. */
e7af610e 212static int file_mips_isa = ISA_UNKNOWN;
252b5132 213
a4672219
TS
214/* True if -mips16 was passed or implied by arguments passed on the
215 command line (e.g., by -march). */
216static int file_ase_mips16;
217
1f25f5d3
CD
218/* True if -mips3d was passed or implied by arguments passed on the
219 command line (e.g., by -march). */
220static int file_ase_mips3d;
221
deec1734
CD
222/* True if -mdmx was passed or implied by arguments passed on the
223 command line (e.g., by -march). */
224static int file_ase_mdmx;
225
ec68c924 226/* The argument of the -march= flag. The architecture we are assembling. */
fef14a42 227static int file_mips_arch = CPU_UNKNOWN;
316f5878 228static const char *mips_arch_string;
ec68c924
EC
229
230/* The argument of the -mtune= flag. The architecture for which we
231 are optimizing. */
232static int mips_tune = CPU_UNKNOWN;
316f5878 233static const char *mips_tune_string;
ec68c924 234
316f5878 235/* True when generating 32-bit code for a 64-bit processor. */
252b5132
RH
236static int mips_32bitmode = 0;
237
316f5878
RS
238/* True if the given ABI requires 32-bit registers. */
239#define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
240
241/* Likewise 64-bit registers. */
242#define ABI_NEEDS_64BIT_REGS(ABI) \
243 ((ABI) == N32_ABI \
244 || (ABI) == N64_ABI \
245 || (ABI) == O64_ABI)
246
bdaaa2e1 247/* Return true if ISA supports 64 bit gp register instructions. */
9ce8a5dd 248#define ISA_HAS_64BIT_REGS(ISA) ( \
e7af610e
NC
249 (ISA) == ISA_MIPS3 \
250 || (ISA) == ISA_MIPS4 \
84ea6cf2 251 || (ISA) == ISA_MIPS5 \
d1cf510e 252 || (ISA) == ISA_MIPS64 \
5f74bc13 253 || (ISA) == ISA_MIPS64R2 \
9ce8a5dd
GRK
254 )
255
af7ee8bf
CD
256/* Return true if ISA supports 64-bit right rotate (dror et al.)
257 instructions. */
258#define ISA_HAS_DROR(ISA) ( \
5f74bc13 259 (ISA) == ISA_MIPS64R2 \
af7ee8bf
CD
260 )
261
262/* Return true if ISA supports 32-bit right rotate (ror et al.)
263 instructions. */
264#define ISA_HAS_ROR(ISA) ( \
265 (ISA) == ISA_MIPS32R2 \
5f74bc13 266 || (ISA) == ISA_MIPS64R2 \
af7ee8bf
CD
267 )
268
e013f690 269#define HAVE_32BIT_GPRS \
316f5878 270 (mips_opts.gp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
ca4e0257 271
e013f690 272#define HAVE_32BIT_FPRS \
316f5878 273 (mips_opts.fp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
ca4e0257
RS
274
275#define HAVE_64BIT_GPRS (! HAVE_32BIT_GPRS)
276#define HAVE_64BIT_FPRS (! HAVE_32BIT_FPRS)
277
316f5878 278#define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
e013f690 279
316f5878 280#define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
e013f690
TS
281
282/* We can only have 64bit addresses if the object file format
283 supports it. */
afdbd6d0
CD
284#define HAVE_32BIT_ADDRESSES \
285 (HAVE_32BIT_GPRS \
286 || ((bfd_arch_bits_per_address (stdoutput) == 32 \
287 || ! HAVE_64BIT_OBJECTS) \
288 && mips_pic != EMBEDDED_PIC))
e013f690
TS
289
290#define HAVE_64BIT_ADDRESSES (! HAVE_32BIT_ADDRESSES)
ca4e0257 291
b7c7d6c1
TS
292/* Addresses are loaded in different ways, depending on the address size
293 in use. The n32 ABI Documentation also mandates the use of additions
294 with overflow checking, but existing implementations don't follow it. */
f899b4b8 295#define ADDRESS_ADD_INSN \
b7c7d6c1 296 (HAVE_32BIT_ADDRESSES ? "addu" : "daddu")
f899b4b8
TS
297
298#define ADDRESS_ADDI_INSN \
b7c7d6c1 299 (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu")
f899b4b8
TS
300
301#define ADDRESS_LOAD_INSN \
302 (HAVE_32BIT_ADDRESSES ? "lw" : "ld")
303
304#define ADDRESS_STORE_INSN \
305 (HAVE_32BIT_ADDRESSES ? "sw" : "sd")
306
a4672219 307/* Return true if the given CPU supports the MIPS16 ASE. */
3396de36
TS
308#define CPU_HAS_MIPS16(cpu) \
309 (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0 \
310 || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
a4672219 311
1f25f5d3
CD
312/* Return true if the given CPU supports the MIPS3D ASE. */
313#define CPU_HAS_MIPS3D(cpu) ((cpu) == CPU_SB1 \
314 )
315
deec1734 316/* Return true if the given CPU supports the MDMX ASE. */
b34976b6 317#define CPU_HAS_MDMX(cpu) (FALSE \
deec1734
CD
318 )
319
60b63b72
RS
320/* True if CPU has a dror instruction. */
321#define CPU_HAS_DROR(CPU) ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
322
323/* True if CPU has a ror instruction. */
324#define CPU_HAS_ROR(CPU) CPU_HAS_DROR (CPU)
325
c8978940
CD
326/* True if mflo and mfhi can be immediately followed by instructions
327 which write to the HI and LO registers.
328
329 According to MIPS specifications, MIPS ISAs I, II, and III need
330 (at least) two instructions between the reads of HI/LO and
331 instructions which write them, and later ISAs do not. Contradicting
332 the MIPS specifications, some MIPS IV processor user manuals (e.g.
333 the UM for the NEC Vr5000) document needing the instructions between
334 HI/LO reads and writes, as well. Therefore, we declare only MIPS32,
335 MIPS64 and later ISAs to have the interlocks, plus any specific
336 earlier-ISA CPUs for which CPU documentation declares that the
337 instructions are really interlocked. */
338#define hilo_interlocks \
339 (mips_opts.isa == ISA_MIPS32 \
340 || mips_opts.isa == ISA_MIPS32R2 \
341 || mips_opts.isa == ISA_MIPS64 \
342 || mips_opts.isa == ISA_MIPS64R2 \
343 || mips_opts.arch == CPU_R4010 \
344 || mips_opts.arch == CPU_R10000 \
345 || mips_opts.arch == CPU_R12000 \
346 || mips_opts.arch == CPU_RM7000 \
c8978940
CD
347 || mips_opts.arch == CPU_VR5500 \
348 )
252b5132
RH
349
350/* Whether the processor uses hardware interlocks to protect reads
81912461
ILT
351 from the GPRs after they are loaded from memory, and thus does not
352 require nops to be inserted. This applies to instructions marked
353 INSN_LOAD_MEMORY_DELAY. These nops are only required at MIPS ISA
354 level I. */
252b5132 355#define gpr_interlocks \
e7af610e 356 (mips_opts.isa != ISA_MIPS1 \
fef14a42 357 || mips_opts.arch == CPU_R3900)
252b5132 358
81912461
ILT
359/* Whether the processor uses hardware interlocks to avoid delays
360 required by coprocessor instructions, and thus does not require
361 nops to be inserted. This applies to instructions marked
362 INSN_LOAD_COPROC_DELAY, INSN_COPROC_MOVE_DELAY, and to delays
363 between instructions marked INSN_WRITE_COND_CODE and ones marked
364 INSN_READ_COND_CODE. These nops are only required at MIPS ISA
365 levels I, II, and III. */
bdaaa2e1 366/* Itbl support may require additional care here. */
81912461
ILT
367#define cop_interlocks \
368 ((mips_opts.isa != ISA_MIPS1 \
369 && mips_opts.isa != ISA_MIPS2 \
370 && mips_opts.isa != ISA_MIPS3) \
371 || mips_opts.arch == CPU_R4300 \
81912461
ILT
372 )
373
374/* Whether the processor uses hardware interlocks to protect reads
375 from coprocessor registers after they are loaded from memory, and
376 thus does not require nops to be inserted. This applies to
377 instructions marked INSN_COPROC_MEMORY_DELAY. These nops are only
378 requires at MIPS ISA level I. */
379#define cop_mem_interlocks (mips_opts.isa != ISA_MIPS1)
252b5132 380
6b76fefe
CM
381/* Is this a mfhi or mflo instruction? */
382#define MF_HILO_INSN(PINFO) \
383 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
384
252b5132
RH
385/* MIPS PIC level. */
386
a161fe53 387enum mips_pic_level mips_pic;
252b5132 388
c9914766 389/* 1 if we should generate 32 bit offsets from the $gp register in
252b5132 390 SVR4_PIC mode. Currently has no meaning in other modes. */
c9914766 391static int mips_big_got = 0;
252b5132
RH
392
393/* 1 if trap instructions should used for overflow rather than break
394 instructions. */
c9914766 395static int mips_trap = 0;
252b5132 396
119d663a 397/* 1 if double width floating point constants should not be constructed
b6ff326e 398 by assembling two single width halves into two single width floating
119d663a
NC
399 point registers which just happen to alias the double width destination
400 register. On some architectures this aliasing can be disabled by a bit
d547a75e 401 in the status register, and the setting of this bit cannot be determined
119d663a
NC
402 automatically at assemble time. */
403static int mips_disable_float_construction;
404
252b5132
RH
405/* Non-zero if any .set noreorder directives were used. */
406
407static int mips_any_noreorder;
408
6b76fefe
CM
409/* Non-zero if nops should be inserted when the register referenced in
410 an mfhi/mflo instruction is read in the next two instructions. */
411static int mips_7000_hilo_fix;
412
252b5132 413/* The size of the small data section. */
156c2f8b 414static unsigned int g_switch_value = 8;
252b5132
RH
415/* Whether the -G option was used. */
416static int g_switch_seen = 0;
417
418#define N_RMASK 0xc4
419#define N_VFP 0xd4
420
421/* If we can determine in advance that GP optimization won't be
422 possible, we can skip the relaxation stuff that tries to produce
423 GP-relative references. This makes delay slot optimization work
424 better.
425
426 This function can only provide a guess, but it seems to work for
fba2b7f9
GK
427 gcc output. It needs to guess right for gcc, otherwise gcc
428 will put what it thinks is a GP-relative instruction in a branch
429 delay slot.
252b5132
RH
430
431 I don't know if a fix is needed for the SVR4_PIC mode. I've only
432 fixed it for the non-PIC mode. KR 95/04/07 */
17a2f251 433static int nopic_need_relax (symbolS *, int);
252b5132
RH
434
435/* handle of the OPCODE hash table */
436static struct hash_control *op_hash = NULL;
437
438/* The opcode hash table we use for the mips16. */
439static struct hash_control *mips16_op_hash = NULL;
440
441/* This array holds the chars that always start a comment. If the
442 pre-processor is disabled, these aren't very useful */
443const char comment_chars[] = "#";
444
445/* This array holds the chars that only start a comment at the beginning of
446 a line. If the line seems to have the form '# 123 filename'
447 .line and .file directives will appear in the pre-processed output */
448/* Note that input_file.c hand checks for '#' at the beginning of the
449 first line of the input file. This is because the compiler outputs
bdaaa2e1 450 #NO_APP at the beginning of its output. */
252b5132
RH
451/* Also note that C style comments are always supported. */
452const char line_comment_chars[] = "#";
453
bdaaa2e1 454/* This array holds machine specific line separator characters. */
63a0b638 455const char line_separator_chars[] = ";";
252b5132
RH
456
457/* Chars that can be used to separate mant from exp in floating point nums */
458const char EXP_CHARS[] = "eE";
459
460/* Chars that mean this number is a floating point constant */
461/* As in 0f12.456 */
462/* or 0d1.2345e12 */
463const char FLT_CHARS[] = "rRsSfFdDxXpP";
464
465/* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
466 changed in read.c . Ideally it shouldn't have to know about it at all,
467 but nothing is ideal around here.
468 */
469
470static char *insn_error;
471
472static int auto_align = 1;
473
474/* When outputting SVR4 PIC code, the assembler needs to know the
475 offset in the stack frame from which to restore the $gp register.
476 This is set by the .cprestore pseudo-op, and saved in this
477 variable. */
478static offsetT mips_cprestore_offset = -1;
479
67c1ffbe 480/* Similar for NewABI PIC code, where $gp is callee-saved. NewABI has some
6478892d 481 more optimizations, it can use a register value instead of a memory-saved
956cd1d6 482 offset and even an other register than $gp as global pointer. */
6478892d
TS
483static offsetT mips_cpreturn_offset = -1;
484static int mips_cpreturn_register = -1;
485static int mips_gp_register = GP;
def2e0dd 486static int mips_gprel_offset = 0;
6478892d 487
7a621144
DJ
488/* Whether mips_cprestore_offset has been set in the current function
489 (or whether it has already been warned about, if not). */
490static int mips_cprestore_valid = 0;
491
252b5132
RH
492/* This is the register which holds the stack frame, as set by the
493 .frame pseudo-op. This is needed to implement .cprestore. */
494static int mips_frame_reg = SP;
495
7a621144
DJ
496/* Whether mips_frame_reg has been set in the current function
497 (or whether it has already been warned about, if not). */
498static int mips_frame_reg_valid = 0;
499
252b5132
RH
500/* To output NOP instructions correctly, we need to keep information
501 about the previous two instructions. */
502
503/* Whether we are optimizing. The default value of 2 means to remove
504 unneeded NOPs and swap branch instructions when possible. A value
505 of 1 means to not swap branches. A value of 0 means to always
506 insert NOPs. */
507static int mips_optimize = 2;
508
509/* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
510 equivalent to seeing no -g option at all. */
511static int mips_debug = 0;
512
513/* The previous instruction. */
514static struct mips_cl_insn prev_insn;
515
516/* The instruction before prev_insn. */
517static struct mips_cl_insn prev_prev_insn;
518
519/* If we don't want information for prev_insn or prev_prev_insn, we
520 point the insn_mo field at this dummy integer. */
43841e91 521static const struct mips_opcode dummy_opcode = { NULL, NULL, 0, 0, 0, 0 };
252b5132
RH
522
523/* Non-zero if prev_insn is valid. */
524static int prev_insn_valid;
525
526/* The frag for the previous instruction. */
527static struct frag *prev_insn_frag;
528
529/* The offset into prev_insn_frag for the previous instruction. */
530static long prev_insn_where;
531
532/* The reloc type for the previous instruction, if any. */
f6688943 533static bfd_reloc_code_real_type prev_insn_reloc_type[3];
252b5132
RH
534
535/* The reloc for the previous instruction, if any. */
f6688943 536static fixS *prev_insn_fixp[3];
252b5132
RH
537
538/* Non-zero if the previous instruction was in a delay slot. */
539static int prev_insn_is_delay_slot;
540
541/* Non-zero if the previous instruction was in a .set noreorder. */
542static int prev_insn_unreordered;
543
544/* Non-zero if the previous instruction uses an extend opcode (if
545 mips16). */
546static int prev_insn_extended;
547
548/* Non-zero if the previous previous instruction was in a .set
549 noreorder. */
550static int prev_prev_insn_unreordered;
551
552/* If this is set, it points to a frag holding nop instructions which
553 were inserted before the start of a noreorder section. If those
554 nops turn out to be unnecessary, the size of the frag can be
555 decreased. */
556static fragS *prev_nop_frag;
557
558/* The number of nop instructions we created in prev_nop_frag. */
559static int prev_nop_frag_holds;
560
561/* The number of nop instructions that we know we need in
bdaaa2e1 562 prev_nop_frag. */
252b5132
RH
563static int prev_nop_frag_required;
564
565/* The number of instructions we've seen since prev_nop_frag. */
566static int prev_nop_frag_since;
567
568/* For ECOFF and ELF, relocations against symbols are done in two
569 parts, with a HI relocation and a LO relocation. Each relocation
570 has only 16 bits of space to store an addend. This means that in
571 order for the linker to handle carries correctly, it must be able
572 to locate both the HI and the LO relocation. This means that the
573 relocations must appear in order in the relocation table.
574
575 In order to implement this, we keep track of each unmatched HI
576 relocation. We then sort them so that they immediately precede the
bdaaa2e1 577 corresponding LO relocation. */
252b5132 578
e972090a
NC
579struct mips_hi_fixup
580{
252b5132
RH
581 /* Next HI fixup. */
582 struct mips_hi_fixup *next;
583 /* This fixup. */
584 fixS *fixp;
585 /* The section this fixup is in. */
586 segT seg;
587};
588
589/* The list of unmatched HI relocs. */
590
591static struct mips_hi_fixup *mips_hi_fixup_list;
592
64bdfcaf
RS
593/* The frag containing the last explicit relocation operator.
594 Null if explicit relocations have not been used. */
595
596static fragS *prev_reloc_op_frag;
597
252b5132
RH
598/* Map normal MIPS register numbers to mips16 register numbers. */
599
600#define X ILLEGAL_REG
e972090a
NC
601static const int mips32_to_16_reg_map[] =
602{
252b5132
RH
603 X, X, 2, 3, 4, 5, 6, 7,
604 X, X, X, X, X, X, X, X,
605 0, 1, X, X, X, X, X, X,
606 X, X, X, X, X, X, X, X
607};
608#undef X
609
610/* Map mips16 register numbers to normal MIPS register numbers. */
611
e972090a
NC
612static const unsigned int mips16_to_32_reg_map[] =
613{
252b5132
RH
614 16, 17, 2, 3, 4, 5, 6, 7
615};
60b63b72 616
d766e8ec 617static int mips_fix_vr4120;
4a6a3df4
AO
618
619/* We don't relax branches by default, since this causes us to expand
620 `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
621 fail to compute the offset before expanding the macro to the most
622 efficient expansion. */
623
624static int mips_relax_branch;
252b5132 625\f
4d7206a2
RS
626/* The expansion of many macros depends on the type of symbol that
627 they refer to. For example, when generating position-dependent code,
628 a macro that refers to a symbol may have two different expansions,
629 one which uses GP-relative addresses and one which uses absolute
630 addresses. When generating SVR4-style PIC, a macro may have
631 different expansions for local and global symbols.
632
633 We handle these situations by generating both sequences and putting
634 them in variant frags. In position-dependent code, the first sequence
635 will be the GP-relative one and the second sequence will be the
636 absolute one. In SVR4 PIC, the first sequence will be for global
637 symbols and the second will be for local symbols.
638
584892a6
RS
639 The frag's "subtype" is RELAX_ENCODE (FIRST, SECOND), where FIRST and
640 SECOND are the lengths of the two sequences in bytes. These fields
641 can be extracted using RELAX_FIRST() and RELAX_SECOND(). In addition,
642 the subtype has the following flags:
4d7206a2 643
584892a6
RS
644 RELAX_USE_SECOND
645 Set if it has been decided that we should use the second
646 sequence instead of the first.
647
648 RELAX_SECOND_LONGER
649 Set in the first variant frag if the macro's second implementation
650 is longer than its first. This refers to the macro as a whole,
651 not an individual relaxation.
652
653 RELAX_NOMACRO
654 Set in the first variant frag if the macro appeared in a .set nomacro
655 block and if one alternative requires a warning but the other does not.
656
657 RELAX_DELAY_SLOT
658 Like RELAX_NOMACRO, but indicates that the macro appears in a branch
659 delay slot.
4d7206a2
RS
660
661 The frag's "opcode" points to the first fixup for relaxable code.
662
663 Relaxable macros are generated using a sequence such as:
664
665 relax_start (SYMBOL);
666 ... generate first expansion ...
667 relax_switch ();
668 ... generate second expansion ...
669 relax_end ();
670
671 The code and fixups for the unwanted alternative are discarded
672 by md_convert_frag. */
584892a6 673#define RELAX_ENCODE(FIRST, SECOND) (((FIRST) << 8) | (SECOND))
4d7206a2 674
584892a6
RS
675#define RELAX_FIRST(X) (((X) >> 8) & 0xff)
676#define RELAX_SECOND(X) ((X) & 0xff)
677#define RELAX_USE_SECOND 0x10000
678#define RELAX_SECOND_LONGER 0x20000
679#define RELAX_NOMACRO 0x40000
680#define RELAX_DELAY_SLOT 0x80000
252b5132 681
4a6a3df4
AO
682/* Branch without likely bit. If label is out of range, we turn:
683
684 beq reg1, reg2, label
685 delay slot
686
687 into
688
689 bne reg1, reg2, 0f
690 nop
691 j label
692 0: delay slot
693
694 with the following opcode replacements:
695
696 beq <-> bne
697 blez <-> bgtz
698 bltz <-> bgez
699 bc1f <-> bc1t
700
701 bltzal <-> bgezal (with jal label instead of j label)
702
703 Even though keeping the delay slot instruction in the delay slot of
704 the branch would be more efficient, it would be very tricky to do
705 correctly, because we'd have to introduce a variable frag *after*
706 the delay slot instruction, and expand that instead. Let's do it
707 the easy way for now, even if the branch-not-taken case now costs
708 one additional instruction. Out-of-range branches are not supposed
709 to be common, anyway.
710
711 Branch likely. If label is out of range, we turn:
712
713 beql reg1, reg2, label
714 delay slot (annulled if branch not taken)
715
716 into
717
718 beql reg1, reg2, 1f
719 nop
720 beql $0, $0, 2f
721 nop
722 1: j[al] label
723 delay slot (executed only if branch taken)
724 2:
725
726 It would be possible to generate a shorter sequence by losing the
727 likely bit, generating something like:
b34976b6 728
4a6a3df4
AO
729 bne reg1, reg2, 0f
730 nop
731 j[al] label
732 delay slot (executed only if branch taken)
733 0:
734
735 beql -> bne
736 bnel -> beq
737 blezl -> bgtz
738 bgtzl -> blez
739 bltzl -> bgez
740 bgezl -> bltz
741 bc1fl -> bc1t
742 bc1tl -> bc1f
743
744 bltzall -> bgezal (with jal label instead of j label)
745 bgezall -> bltzal (ditto)
746
747
748 but it's not clear that it would actually improve performance. */
af6ae2ad 749#define RELAX_BRANCH_ENCODE(uncond, likely, link, toofar) \
4a6a3df4
AO
750 ((relax_substateT) \
751 (0xc0000000 \
752 | ((toofar) ? 1 : 0) \
753 | ((link) ? 2 : 0) \
754 | ((likely) ? 4 : 0) \
af6ae2ad 755 | ((uncond) ? 8 : 0)))
4a6a3df4 756#define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
4a6a3df4
AO
757#define RELAX_BRANCH_UNCOND(i) (((i) & 8) != 0)
758#define RELAX_BRANCH_LIKELY(i) (((i) & 4) != 0)
759#define RELAX_BRANCH_LINK(i) (((i) & 2) != 0)
ae6063d4 760#define RELAX_BRANCH_TOOFAR(i) (((i) & 1) != 0)
4a6a3df4 761
252b5132
RH
762/* For mips16 code, we use an entirely different form of relaxation.
763 mips16 supports two versions of most instructions which take
764 immediate values: a small one which takes some small value, and a
765 larger one which takes a 16 bit value. Since branches also follow
766 this pattern, relaxing these values is required.
767
768 We can assemble both mips16 and normal MIPS code in a single
769 object. Therefore, we need to support this type of relaxation at
770 the same time that we support the relaxation described above. We
771 use the high bit of the subtype field to distinguish these cases.
772
773 The information we store for this type of relaxation is the
774 argument code found in the opcode file for this relocation, whether
775 the user explicitly requested a small or extended form, and whether
776 the relocation is in a jump or jal delay slot. That tells us the
777 size of the value, and how it should be stored. We also store
778 whether the fragment is considered to be extended or not. We also
779 store whether this is known to be a branch to a different section,
780 whether we have tried to relax this frag yet, and whether we have
781 ever extended a PC relative fragment because of a shift count. */
782#define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
783 (0x80000000 \
784 | ((type) & 0xff) \
785 | ((small) ? 0x100 : 0) \
786 | ((ext) ? 0x200 : 0) \
787 | ((dslot) ? 0x400 : 0) \
788 | ((jal_dslot) ? 0x800 : 0))
4a6a3df4 789#define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
252b5132
RH
790#define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
791#define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
792#define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
793#define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
794#define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
795#define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
796#define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
797#define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
798#define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
799#define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
800#define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
885add95
CD
801
802/* Is the given value a sign-extended 32-bit value? */
803#define IS_SEXT_32BIT_NUM(x) \
804 (((x) &~ (offsetT) 0x7fffffff) == 0 \
805 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
806
807/* Is the given value a sign-extended 16-bit value? */
808#define IS_SEXT_16BIT_NUM(x) \
809 (((x) &~ (offsetT) 0x7fff) == 0 \
810 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
811
4d7206a2
RS
812\f
813/* Global variables used when generating relaxable macros. See the
814 comment above RELAX_ENCODE for more details about how relaxation
815 is used. */
816static struct {
817 /* 0 if we're not emitting a relaxable macro.
818 1 if we're emitting the first of the two relaxation alternatives.
819 2 if we're emitting the second alternative. */
820 int sequence;
821
822 /* The first relaxable fixup in the current frag. (In other words,
823 the first fixup that refers to relaxable code.) */
824 fixS *first_fixup;
825
826 /* sizes[0] says how many bytes of the first alternative are stored in
827 the current frag. Likewise sizes[1] for the second alternative. */
828 unsigned int sizes[2];
829
830 /* The symbol on which the choice of sequence depends. */
831 symbolS *symbol;
832} mips_relax;
252b5132 833\f
584892a6
RS
834/* Global variables used to decide whether a macro needs a warning. */
835static struct {
836 /* True if the macro is in a branch delay slot. */
837 bfd_boolean delay_slot_p;
838
839 /* For relaxable macros, sizes[0] is the length of the first alternative
840 in bytes and sizes[1] is the length of the second alternative.
841 For non-relaxable macros, both elements give the length of the
842 macro in bytes. */
843 unsigned int sizes[2];
844
845 /* The first variant frag for this macro. */
846 fragS *first_frag;
847} mips_macro_warning;
848\f
252b5132
RH
849/* Prototypes for static functions. */
850
17a2f251 851#define internalError() \
252b5132 852 as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
252b5132
RH
853
854enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
855
b34976b6 856static void append_insn
4d7206a2 857 (struct mips_cl_insn *ip, expressionS *p, bfd_reloc_code_real_type *r);
17a2f251 858static void mips_no_prev_insn (int);
b34976b6 859static void mips16_macro_build
67c0d1eb
RS
860 (expressionS *, const char *, const char *, va_list);
861static void load_register (int, expressionS *, int);
584892a6
RS
862static void macro_start (void);
863static void macro_end (void);
17a2f251
TS
864static void macro (struct mips_cl_insn * ip);
865static void mips16_macro (struct mips_cl_insn * ip);
252b5132 866#ifdef LOSING_COMPILER
17a2f251 867static void macro2 (struct mips_cl_insn * ip);
252b5132 868#endif
17a2f251
TS
869static void mips_ip (char *str, struct mips_cl_insn * ip);
870static void mips16_ip (char *str, struct mips_cl_insn * ip);
b34976b6 871static void mips16_immed
17a2f251
TS
872 (char *, unsigned int, int, offsetT, bfd_boolean, bfd_boolean, bfd_boolean,
873 unsigned long *, bfd_boolean *, unsigned short *);
5e0116d5 874static size_t my_getSmallExpression
17a2f251
TS
875 (expressionS *, bfd_reloc_code_real_type *, char *);
876static void my_getExpression (expressionS *, char *);
877static void s_align (int);
878static void s_change_sec (int);
879static void s_change_section (int);
880static void s_cons (int);
881static void s_float_cons (int);
882static void s_mips_globl (int);
883static void s_option (int);
884static void s_mipsset (int);
885static void s_abicalls (int);
886static void s_cpload (int);
887static void s_cpsetup (int);
888static void s_cplocal (int);
889static void s_cprestore (int);
890static void s_cpreturn (int);
891static void s_gpvalue (int);
892static void s_gpword (int);
893static void s_gpdword (int);
894static void s_cpadd (int);
895static void s_insn (int);
896static void md_obj_begin (void);
897static void md_obj_end (void);
898static void s_mips_ent (int);
899static void s_mips_end (int);
900static void s_mips_frame (int);
901static void s_mips_mask (int reg_type);
902static void s_mips_stab (int);
903static void s_mips_weakext (int);
904static void s_mips_file (int);
905static void s_mips_loc (int);
906static bfd_boolean pic_need_relax (symbolS *, asection *);
4a6a3df4 907static int relaxed_branch_length (fragS *, asection *, int);
17a2f251 908static int validate_mips_insn (const struct mips_opcode *);
e7af610e
NC
909
910/* Table and functions used to map between CPU/ISA names, and
911 ISA levels, and CPU numbers. */
912
e972090a
NC
913struct mips_cpu_info
914{
e7af610e
NC
915 const char *name; /* CPU or ISA name. */
916 int is_isa; /* Is this an ISA? (If 0, a CPU.) */
917 int isa; /* ISA level. */
918 int cpu; /* CPU number (default CPU if ISA). */
919};
920
17a2f251
TS
921static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
922static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
923static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
252b5132
RH
924\f
925/* Pseudo-op table.
926
927 The following pseudo-ops from the Kane and Heinrich MIPS book
928 should be defined here, but are currently unsupported: .alias,
929 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
930
931 The following pseudo-ops from the Kane and Heinrich MIPS book are
932 specific to the type of debugging information being generated, and
933 should be defined by the object format: .aent, .begin, .bend,
934 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
935 .vreg.
936
937 The following pseudo-ops from the Kane and Heinrich MIPS book are
938 not MIPS CPU specific, but are also not specific to the object file
939 format. This file is probably the best place to define them, but
940 they are not currently supported: .asm0, .endr, .lab, .repeat,
941 .struct. */
942
e972090a
NC
943static const pseudo_typeS mips_pseudo_table[] =
944{
beae10d5 945 /* MIPS specific pseudo-ops. */
252b5132
RH
946 {"option", s_option, 0},
947 {"set", s_mipsset, 0},
948 {"rdata", s_change_sec, 'r'},
949 {"sdata", s_change_sec, 's'},
950 {"livereg", s_ignore, 0},
951 {"abicalls", s_abicalls, 0},
952 {"cpload", s_cpload, 0},
6478892d
TS
953 {"cpsetup", s_cpsetup, 0},
954 {"cplocal", s_cplocal, 0},
252b5132 955 {"cprestore", s_cprestore, 0},
6478892d
TS
956 {"cpreturn", s_cpreturn, 0},
957 {"gpvalue", s_gpvalue, 0},
252b5132 958 {"gpword", s_gpword, 0},
10181a0d 959 {"gpdword", s_gpdword, 0},
252b5132
RH
960 {"cpadd", s_cpadd, 0},
961 {"insn", s_insn, 0},
962
beae10d5 963 /* Relatively generic pseudo-ops that happen to be used on MIPS
252b5132
RH
964 chips. */
965 {"asciiz", stringer, 1},
966 {"bss", s_change_sec, 'b'},
967 {"err", s_err, 0},
968 {"half", s_cons, 1},
969 {"dword", s_cons, 3},
970 {"weakext", s_mips_weakext, 0},
971
beae10d5 972 /* These pseudo-ops are defined in read.c, but must be overridden
252b5132
RH
973 here for one reason or another. */
974 {"align", s_align, 0},
975 {"byte", s_cons, 0},
976 {"data", s_change_sec, 'd'},
977 {"double", s_float_cons, 'd'},
978 {"float", s_float_cons, 'f'},
979 {"globl", s_mips_globl, 0},
980 {"global", s_mips_globl, 0},
981 {"hword", s_cons, 1},
982 {"int", s_cons, 2},
983 {"long", s_cons, 2},
984 {"octa", s_cons, 4},
985 {"quad", s_cons, 3},
cca86cc8 986 {"section", s_change_section, 0},
252b5132
RH
987 {"short", s_cons, 1},
988 {"single", s_float_cons, 'f'},
989 {"stabn", s_mips_stab, 'n'},
990 {"text", s_change_sec, 't'},
991 {"word", s_cons, 2},
add56521 992
add56521 993 { "extern", ecoff_directive_extern, 0},
add56521 994
43841e91 995 { NULL, NULL, 0 },
252b5132
RH
996};
997
e972090a
NC
998static const pseudo_typeS mips_nonecoff_pseudo_table[] =
999{
beae10d5
KH
1000 /* These pseudo-ops should be defined by the object file format.
1001 However, a.out doesn't support them, so we have versions here. */
252b5132
RH
1002 {"aent", s_mips_ent, 1},
1003 {"bgnb", s_ignore, 0},
1004 {"end", s_mips_end, 0},
1005 {"endb", s_ignore, 0},
1006 {"ent", s_mips_ent, 0},
c5dd6aab 1007 {"file", s_mips_file, 0},
252b5132
RH
1008 {"fmask", s_mips_mask, 'F'},
1009 {"frame", s_mips_frame, 0},
c5dd6aab 1010 {"loc", s_mips_loc, 0},
252b5132
RH
1011 {"mask", s_mips_mask, 'R'},
1012 {"verstamp", s_ignore, 0},
43841e91 1013 { NULL, NULL, 0 },
252b5132
RH
1014};
1015
17a2f251 1016extern void pop_insert (const pseudo_typeS *);
252b5132
RH
1017
1018void
17a2f251 1019mips_pop_insert (void)
252b5132
RH
1020{
1021 pop_insert (mips_pseudo_table);
1022 if (! ECOFF_DEBUGGING)
1023 pop_insert (mips_nonecoff_pseudo_table);
1024}
1025\f
1026/* Symbols labelling the current insn. */
1027
e972090a
NC
1028struct insn_label_list
1029{
252b5132
RH
1030 struct insn_label_list *next;
1031 symbolS *label;
1032};
1033
1034static struct insn_label_list *insn_labels;
1035static struct insn_label_list *free_insn_labels;
1036
17a2f251 1037static void mips_clear_insn_labels (void);
252b5132
RH
1038
1039static inline void
17a2f251 1040mips_clear_insn_labels (void)
252b5132
RH
1041{
1042 register struct insn_label_list **pl;
1043
1044 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1045 ;
1046 *pl = insn_labels;
1047 insn_labels = NULL;
1048}
1049\f
1050static char *expr_end;
1051
1052/* Expressions which appear in instructions. These are set by
1053 mips_ip. */
1054
1055static expressionS imm_expr;
5f74bc13 1056static expressionS imm2_expr;
252b5132
RH
1057static expressionS offset_expr;
1058
1059/* Relocs associated with imm_expr and offset_expr. */
1060
f6688943
TS
1061static bfd_reloc_code_real_type imm_reloc[3]
1062 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1063static bfd_reloc_code_real_type offset_reloc[3]
1064 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 1065
252b5132
RH
1066/* These are set by mips16_ip if an explicit extension is used. */
1067
b34976b6 1068static bfd_boolean mips16_small, mips16_ext;
252b5132 1069
7ed4a06a 1070#ifdef OBJ_ELF
ecb4347a
DJ
1071/* The pdr segment for per procedure frame/regmask info. Not used for
1072 ECOFF debugging. */
252b5132
RH
1073
1074static segT pdr_seg;
7ed4a06a 1075#endif
252b5132 1076
e013f690
TS
1077/* The default target format to use. */
1078
1079const char *
17a2f251 1080mips_target_format (void)
e013f690
TS
1081{
1082 switch (OUTPUT_FLAVOR)
1083 {
e013f690
TS
1084 case bfd_target_ecoff_flavour:
1085 return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
1086 case bfd_target_coff_flavour:
1087 return "pe-mips";
1088 case bfd_target_elf_flavour:
1089#ifdef TE_TMIPS
cfe86eaa 1090 /* This is traditional mips. */
e013f690 1091 return (target_big_endian
cfe86eaa
TS
1092 ? (HAVE_64BIT_OBJECTS
1093 ? "elf64-tradbigmips"
1094 : (HAVE_NEWABI
1095 ? "elf32-ntradbigmips" : "elf32-tradbigmips"))
1096 : (HAVE_64BIT_OBJECTS
1097 ? "elf64-tradlittlemips"
1098 : (HAVE_NEWABI
1099 ? "elf32-ntradlittlemips" : "elf32-tradlittlemips")));
e013f690
TS
1100#else
1101 return (target_big_endian
cfe86eaa
TS
1102 ? (HAVE_64BIT_OBJECTS
1103 ? "elf64-bigmips"
1104 : (HAVE_NEWABI
1105 ? "elf32-nbigmips" : "elf32-bigmips"))
1106 : (HAVE_64BIT_OBJECTS
1107 ? "elf64-littlemips"
1108 : (HAVE_NEWABI
1109 ? "elf32-nlittlemips" : "elf32-littlemips")));
e013f690
TS
1110#endif
1111 default:
1112 abort ();
1113 return NULL;
1114 }
1115}
1116
156c2f8b
NC
1117/* This function is called once, at assembler startup time. It should
1118 set up all the tables, etc. that the MD part of the assembler will need. */
1119
252b5132 1120void
17a2f251 1121md_begin (void)
252b5132 1122{
252b5132 1123 register const char *retval = NULL;
156c2f8b 1124 int i = 0;
252b5132 1125 int broken = 0;
1f25f5d3 1126
fef14a42 1127 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_arch))
252b5132
RH
1128 as_warn (_("Could not set architecture and machine"));
1129
252b5132
RH
1130 op_hash = hash_new ();
1131
1132 for (i = 0; i < NUMOPCODES;)
1133 {
1134 const char *name = mips_opcodes[i].name;
1135
17a2f251 1136 retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
252b5132
RH
1137 if (retval != NULL)
1138 {
1139 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1140 mips_opcodes[i].name, retval);
1141 /* Probably a memory allocation problem? Give up now. */
1142 as_fatal (_("Broken assembler. No assembly attempted."));
1143 }
1144 do
1145 {
1146 if (mips_opcodes[i].pinfo != INSN_MACRO)
1147 {
1148 if (!validate_mips_insn (&mips_opcodes[i]))
1149 broken = 1;
1150 }
1151 ++i;
1152 }
1153 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1154 }
1155
1156 mips16_op_hash = hash_new ();
1157
1158 i = 0;
1159 while (i < bfd_mips16_num_opcodes)
1160 {
1161 const char *name = mips16_opcodes[i].name;
1162
17a2f251 1163 retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
252b5132
RH
1164 if (retval != NULL)
1165 as_fatal (_("internal: can't hash `%s': %s"),
1166 mips16_opcodes[i].name, retval);
1167 do
1168 {
1169 if (mips16_opcodes[i].pinfo != INSN_MACRO
1170 && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1171 != mips16_opcodes[i].match))
1172 {
1173 fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1174 mips16_opcodes[i].name, mips16_opcodes[i].args);
1175 broken = 1;
1176 }
1177 ++i;
1178 }
1179 while (i < bfd_mips16_num_opcodes
1180 && strcmp (mips16_opcodes[i].name, name) == 0);
1181 }
1182
1183 if (broken)
1184 as_fatal (_("Broken assembler. No assembly attempted."));
1185
1186 /* We add all the general register names to the symbol table. This
1187 helps us detect invalid uses of them. */
1188 for (i = 0; i < 32; i++)
1189 {
1190 char buf[5];
1191
1192 sprintf (buf, "$%d", i);
1193 symbol_table_insert (symbol_new (buf, reg_section, i,
1194 &zero_address_frag));
1195 }
76db943d
TS
1196 symbol_table_insert (symbol_new ("$ra", reg_section, RA,
1197 &zero_address_frag));
252b5132
RH
1198 symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1199 &zero_address_frag));
1200 symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1201 &zero_address_frag));
1202 symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1203 &zero_address_frag));
1204 symbol_table_insert (symbol_new ("$at", reg_section, AT,
1205 &zero_address_frag));
1206 symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1207 &zero_address_frag));
1208 symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1209 &zero_address_frag));
85b51719
TS
1210 symbol_table_insert (symbol_new ("$zero", reg_section, ZERO,
1211 &zero_address_frag));
252b5132
RH
1212 symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1213 &zero_address_frag));
1214
6047c971
AO
1215 /* If we don't add these register names to the symbol table, they
1216 may end up being added as regular symbols by operand(), and then
1217 make it to the object file as undefined in case they're not
1218 regarded as local symbols. They're local in o32, since `$' is a
1219 local symbol prefix, but not in n32 or n64. */
1220 for (i = 0; i < 8; i++)
1221 {
1222 char buf[6];
1223
1224 sprintf (buf, "$fcc%i", i);
1225 symbol_table_insert (symbol_new (buf, reg_section, -1,
1226 &zero_address_frag));
1227 }
1228
b34976b6 1229 mips_no_prev_insn (FALSE);
252b5132
RH
1230
1231 mips_gprmask = 0;
1232 mips_cprmask[0] = 0;
1233 mips_cprmask[1] = 0;
1234 mips_cprmask[2] = 0;
1235 mips_cprmask[3] = 0;
1236
1237 /* set the default alignment for the text section (2**2) */
1238 record_alignment (text_section, 2);
1239
4d0d148d 1240 bfd_set_gp_size (stdoutput, g_switch_value);
252b5132
RH
1241
1242 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1243 {
1244 /* On a native system, sections must be aligned to 16 byte
1245 boundaries. When configured for an embedded ELF target, we
1246 don't bother. */
1247 if (strcmp (TARGET_OS, "elf") != 0)
1248 {
1249 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1250 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1251 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1252 }
1253
1254 /* Create a .reginfo section for register masks and a .mdebug
1255 section for debugging information. */
1256 {
1257 segT seg;
1258 subsegT subseg;
1259 flagword flags;
1260 segT sec;
1261
1262 seg = now_seg;
1263 subseg = now_subseg;
1264
1265 /* The ABI says this section should be loaded so that the
1266 running program can access it. However, we don't load it
1267 if we are configured for an embedded target */
1268 flags = SEC_READONLY | SEC_DATA;
1269 if (strcmp (TARGET_OS, "elf") != 0)
1270 flags |= SEC_ALLOC | SEC_LOAD;
1271
316f5878 1272 if (mips_abi != N64_ABI)
252b5132
RH
1273 {
1274 sec = subseg_new (".reginfo", (subsegT) 0);
1275
195325d2
TS
1276 bfd_set_section_flags (stdoutput, sec, flags);
1277 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
bdaaa2e1 1278
252b5132
RH
1279#ifdef OBJ_ELF
1280 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1281#endif
1282 }
1283 else
1284 {
1285 /* The 64-bit ABI uses a .MIPS.options section rather than
1286 .reginfo section. */
1287 sec = subseg_new (".MIPS.options", (subsegT) 0);
195325d2
TS
1288 bfd_set_section_flags (stdoutput, sec, flags);
1289 bfd_set_section_alignment (stdoutput, sec, 3);
252b5132
RH
1290
1291#ifdef OBJ_ELF
1292 /* Set up the option header. */
1293 {
1294 Elf_Internal_Options opthdr;
1295 char *f;
1296
1297 opthdr.kind = ODK_REGINFO;
1298 opthdr.size = (sizeof (Elf_External_Options)
1299 + sizeof (Elf64_External_RegInfo));
1300 opthdr.section = 0;
1301 opthdr.info = 0;
1302 f = frag_more (sizeof (Elf_External_Options));
1303 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1304 (Elf_External_Options *) f);
1305
1306 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1307 }
1308#endif
1309 }
1310
1311 if (ECOFF_DEBUGGING)
1312 {
1313 sec = subseg_new (".mdebug", (subsegT) 0);
1314 (void) bfd_set_section_flags (stdoutput, sec,
1315 SEC_HAS_CONTENTS | SEC_READONLY);
1316 (void) bfd_set_section_alignment (stdoutput, sec, 2);
1317 }
ecb4347a 1318#ifdef OBJ_ELF
dcd410fe 1319 else if (OUTPUT_FLAVOR == bfd_target_elf_flavour && mips_flag_pdr)
ecb4347a
DJ
1320 {
1321 pdr_seg = subseg_new (".pdr", (subsegT) 0);
1322 (void) bfd_set_section_flags (stdoutput, pdr_seg,
1323 SEC_READONLY | SEC_RELOC
1324 | SEC_DEBUGGING);
1325 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1326 }
252b5132
RH
1327#endif
1328
1329 subseg_set (seg, subseg);
1330 }
1331 }
1332
1333 if (! ECOFF_DEBUGGING)
1334 md_obj_begin ();
1335}
1336
1337void
17a2f251 1338md_mips_end (void)
252b5132
RH
1339{
1340 if (! ECOFF_DEBUGGING)
1341 md_obj_end ();
1342}
1343
1344void
17a2f251 1345md_assemble (char *str)
252b5132
RH
1346{
1347 struct mips_cl_insn insn;
f6688943
TS
1348 bfd_reloc_code_real_type unused_reloc[3]
1349 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132
RH
1350
1351 imm_expr.X_op = O_absent;
5f74bc13 1352 imm2_expr.X_op = O_absent;
252b5132 1353 offset_expr.X_op = O_absent;
f6688943
TS
1354 imm_reloc[0] = BFD_RELOC_UNUSED;
1355 imm_reloc[1] = BFD_RELOC_UNUSED;
1356 imm_reloc[2] = BFD_RELOC_UNUSED;
1357 offset_reloc[0] = BFD_RELOC_UNUSED;
1358 offset_reloc[1] = BFD_RELOC_UNUSED;
1359 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
1360
1361 if (mips_opts.mips16)
1362 mips16_ip (str, &insn);
1363 else
1364 {
1365 mips_ip (str, &insn);
beae10d5
KH
1366 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1367 str, insn.insn_opcode));
252b5132
RH
1368 }
1369
1370 if (insn_error)
1371 {
1372 as_bad ("%s `%s'", insn_error, str);
1373 return;
1374 }
1375
1376 if (insn.insn_mo->pinfo == INSN_MACRO)
1377 {
584892a6 1378 macro_start ();
252b5132
RH
1379 if (mips_opts.mips16)
1380 mips16_macro (&insn);
1381 else
1382 macro (&insn);
584892a6 1383 macro_end ();
252b5132
RH
1384 }
1385 else
1386 {
1387 if (imm_expr.X_op != O_absent)
4d7206a2 1388 append_insn (&insn, &imm_expr, imm_reloc);
252b5132 1389 else if (offset_expr.X_op != O_absent)
4d7206a2 1390 append_insn (&insn, &offset_expr, offset_reloc);
252b5132 1391 else
4d7206a2 1392 append_insn (&insn, NULL, unused_reloc);
252b5132
RH
1393 }
1394}
1395
5919d012
RS
1396/* Return true if the given relocation might need a matching %lo().
1397 Note that R_MIPS_GOT16 relocations only need a matching %lo() when
1398 applied to local symbols. */
1399
1400static inline bfd_boolean
17a2f251 1401reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
5919d012
RS
1402{
1403 return (reloc == BFD_RELOC_HI16_S
1404 || reloc == BFD_RELOC_MIPS_GOT16);
1405}
1406
1407/* Return true if the given fixup is followed by a matching R_MIPS_LO16
1408 relocation. */
1409
1410static inline bfd_boolean
17a2f251 1411fixup_has_matching_lo_p (fixS *fixp)
5919d012
RS
1412{
1413 return (fixp->fx_next != NULL
1414 && fixp->fx_next->fx_r_type == BFD_RELOC_LO16
1415 && fixp->fx_addsy == fixp->fx_next->fx_addsy
1416 && fixp->fx_offset == fixp->fx_next->fx_offset);
1417}
1418
252b5132
RH
1419/* See whether instruction IP reads register REG. CLASS is the type
1420 of register. */
1421
1422static int
17a2f251
TS
1423insn_uses_reg (struct mips_cl_insn *ip, unsigned int reg,
1424 enum mips_regclass class)
252b5132
RH
1425{
1426 if (class == MIPS16_REG)
1427 {
1428 assert (mips_opts.mips16);
1429 reg = mips16_to_32_reg_map[reg];
1430 class = MIPS_GR_REG;
1431 }
1432
85b51719
TS
1433 /* Don't report on general register ZERO, since it never changes. */
1434 if (class == MIPS_GR_REG && reg == ZERO)
252b5132
RH
1435 return 0;
1436
1437 if (class == MIPS_FP_REG)
1438 {
1439 assert (! mips_opts.mips16);
1440 /* If we are called with either $f0 or $f1, we must check $f0.
1441 This is not optimal, because it will introduce an unnecessary
1442 NOP between "lwc1 $f0" and "swc1 $f1". To fix this we would
1443 need to distinguish reading both $f0 and $f1 or just one of
1444 them. Note that we don't have to check the other way,
1445 because there is no instruction that sets both $f0 and $f1
1446 and requires a delay. */
1447 if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1448 && ((((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS) &~(unsigned)1)
1449 == (reg &~ (unsigned) 1)))
1450 return 1;
1451 if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1452 && ((((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT) &~(unsigned)1)
1453 == (reg &~ (unsigned) 1)))
1454 return 1;
1455 }
1456 else if (! mips_opts.mips16)
1457 {
1458 if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1459 && ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == reg)
1460 return 1;
1461 if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1462 && ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT) == reg)
1463 return 1;
1464 }
1465 else
1466 {
1467 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1468 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RX)
1469 & MIPS16OP_MASK_RX)]
1470 == reg))
1471 return 1;
1472 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1473 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RY)
1474 & MIPS16OP_MASK_RY)]
1475 == reg))
1476 return 1;
1477 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1478 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
1479 & MIPS16OP_MASK_MOVE32Z)]
1480 == reg))
1481 return 1;
1482 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1483 return 1;
1484 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1485 return 1;
1486 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1487 return 1;
1488 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1489 && ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
1490 & MIPS16OP_MASK_REGR32) == reg)
1491 return 1;
1492 }
1493
1494 return 0;
1495}
1496
1497/* This function returns true if modifying a register requires a
1498 delay. */
1499
1500static int
17a2f251 1501reg_needs_delay (unsigned int reg)
252b5132
RH
1502{
1503 unsigned long prev_pinfo;
1504
1505 prev_pinfo = prev_insn.insn_mo->pinfo;
1506 if (! mips_opts.noreorder
81912461
ILT
1507 && (((prev_pinfo & INSN_LOAD_MEMORY_DELAY)
1508 && ! gpr_interlocks)
1509 || ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1510 && ! cop_interlocks)))
252b5132 1511 {
81912461
ILT
1512 /* A load from a coprocessor or from memory. All load delays
1513 delay the use of general register rt for one instruction. */
bdaaa2e1 1514 /* Itbl support may require additional care here. */
252b5132
RH
1515 know (prev_pinfo & INSN_WRITE_GPR_T);
1516 if (reg == ((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT))
1517 return 1;
1518 }
1519
1520 return 0;
1521}
1522
1523/* Mark instruction labels in mips16 mode. This permits the linker to
1524 handle them specially, such as generating jalx instructions when
1525 needed. We also make them odd for the duration of the assembly, in
1526 order to generate the right sort of code. We will make them even
1527 in the adjust_symtab routine, while leaving them marked. This is
1528 convenient for the debugger and the disassembler. The linker knows
1529 to make them odd again. */
1530
1531static void
17a2f251 1532mips16_mark_labels (void)
252b5132
RH
1533{
1534 if (mips_opts.mips16)
1535 {
1536 struct insn_label_list *l;
98aa84af 1537 valueT val;
252b5132
RH
1538
1539 for (l = insn_labels; l != NULL; l = l->next)
1540 {
1541#ifdef OBJ_ELF
1542 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1543 S_SET_OTHER (l->label, STO_MIPS16);
1544#endif
98aa84af
AM
1545 val = S_GET_VALUE (l->label);
1546 if ((val & 1) == 0)
1547 S_SET_VALUE (l->label, val + 1);
252b5132
RH
1548 }
1549 }
1550}
1551
4d7206a2
RS
1552/* End the current frag. Make it a variant frag and record the
1553 relaxation info. */
1554
1555static void
1556relax_close_frag (void)
1557{
584892a6 1558 mips_macro_warning.first_frag = frag_now;
4d7206a2 1559 frag_var (rs_machine_dependent, 0, 0,
584892a6 1560 RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1]),
4d7206a2
RS
1561 mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
1562
1563 memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
1564 mips_relax.first_fixup = 0;
1565}
1566
1567/* Start a new relaxation sequence whose expansion depends on SYMBOL.
1568 See the comment above RELAX_ENCODE for more details. */
1569
1570static void
1571relax_start (symbolS *symbol)
1572{
1573 assert (mips_relax.sequence == 0);
1574 mips_relax.sequence = 1;
1575 mips_relax.symbol = symbol;
1576}
1577
1578/* Start generating the second version of a relaxable sequence.
1579 See the comment above RELAX_ENCODE for more details. */
252b5132
RH
1580
1581static void
4d7206a2
RS
1582relax_switch (void)
1583{
1584 assert (mips_relax.sequence == 1);
1585 mips_relax.sequence = 2;
1586}
1587
1588/* End the current relaxable sequence. */
1589
1590static void
1591relax_end (void)
1592{
1593 assert (mips_relax.sequence == 2);
1594 relax_close_frag ();
1595 mips_relax.sequence = 0;
1596}
1597
1598/* Output an instruction. IP is the instruction information.
1599 ADDRESS_EXPR is an operand of the instruction to be used with
1600 RELOC_TYPE. */
1601
1602static void
1603append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
17a2f251 1604 bfd_reloc_code_real_type *reloc_type)
252b5132
RH
1605{
1606 register unsigned long prev_pinfo, pinfo;
1607 char *f;
f6688943 1608 fixS *fixp[3];
252b5132 1609 int nops = 0;
895921c9
MR
1610 relax_stateT prev_insn_frag_type = 0;
1611 bfd_boolean relaxed_branch = FALSE;
f5040a92 1612 bfd_boolean force_new_frag = FALSE;
252b5132
RH
1613
1614 /* Mark instruction labels in mips16 mode. */
f9419b05 1615 mips16_mark_labels ();
252b5132
RH
1616
1617 prev_pinfo = prev_insn.insn_mo->pinfo;
1618 pinfo = ip->insn_mo->pinfo;
1619
4d7206a2
RS
1620 if (mips_relax.sequence != 2
1621 && (!mips_opts.noreorder || prev_nop_frag != NULL))
252b5132
RH
1622 {
1623 int prev_prev_nop;
1624
1625 /* If the previous insn required any delay slots, see if we need
1626 to insert a NOP or two. There are eight kinds of possible
1627 hazards, of which an instruction can have at most one type.
1628 (1) a load from memory delay
1629 (2) a load from a coprocessor delay
1630 (3) an unconditional branch delay
1631 (4) a conditional branch delay
1632 (5) a move to coprocessor register delay
1633 (6) a load coprocessor register from memory delay
1634 (7) a coprocessor condition code delay
1635 (8) a HI/LO special register delay
1636
1637 There are a lot of optimizations we could do that we don't.
1638 In particular, we do not, in general, reorder instructions.
1639 If you use gcc with optimization, it will reorder
1640 instructions and generally do much more optimization then we
1641 do here; repeating all that work in the assembler would only
1642 benefit hand written assembly code, and does not seem worth
1643 it. */
1644
1645 /* This is how a NOP is emitted. */
1646#define emit_nop() \
1647 (mips_opts.mips16 \
1648 ? md_number_to_chars (frag_more (2), 0x6500, 2) \
1649 : md_number_to_chars (frag_more (4), 0, 4))
1650
1651 /* The previous insn might require a delay slot, depending upon
1652 the contents of the current insn. */
1653 if (! mips_opts.mips16
81912461
ILT
1654 && (((prev_pinfo & INSN_LOAD_MEMORY_DELAY)
1655 && ! gpr_interlocks)
1656 || ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1657 && ! cop_interlocks)))
252b5132
RH
1658 {
1659 /* A load from a coprocessor or from memory. All load
1660 delays delay the use of general register rt for one
81912461 1661 instruction. */
beae10d5 1662 /* Itbl support may require additional care here. */
252b5132
RH
1663 know (prev_pinfo & INSN_WRITE_GPR_T);
1664 if (mips_optimize == 0
1665 || insn_uses_reg (ip,
1666 ((prev_insn.insn_opcode >> OP_SH_RT)
1667 & OP_MASK_RT),
1668 MIPS_GR_REG))
1669 ++nops;
1670 }
1671 else if (! mips_opts.mips16
252b5132 1672 && (((prev_pinfo & INSN_COPROC_MOVE_DELAY)
beae10d5 1673 && ! cop_interlocks)
81912461
ILT
1674 || ((prev_pinfo & INSN_COPROC_MEMORY_DELAY)
1675 && ! cop_mem_interlocks)))
252b5132
RH
1676 {
1677 /* A generic coprocessor delay. The previous instruction
1678 modified a coprocessor general or control register. If
1679 it modified a control register, we need to avoid any
1680 coprocessor instruction (this is probably not always
1681 required, but it sometimes is). If it modified a general
1682 register, we avoid using that register.
1683
252b5132
RH
1684 This case is not handled very well. There is no special
1685 knowledge of CP0 handling, and the coprocessors other
1686 than the floating point unit are not distinguished at
1687 all. */
1688 /* Itbl support may require additional care here. FIXME!
bdaaa2e1 1689 Need to modify this to include knowledge about
252b5132
RH
1690 user specified delays! */
1691 if (prev_pinfo & INSN_WRITE_FPR_T)
1692 {
1693 if (mips_optimize == 0
1694 || insn_uses_reg (ip,
1695 ((prev_insn.insn_opcode >> OP_SH_FT)
1696 & OP_MASK_FT),
1697 MIPS_FP_REG))
1698 ++nops;
1699 }
1700 else if (prev_pinfo & INSN_WRITE_FPR_S)
1701 {
1702 if (mips_optimize == 0
1703 || insn_uses_reg (ip,
1704 ((prev_insn.insn_opcode >> OP_SH_FS)
1705 & OP_MASK_FS),
1706 MIPS_FP_REG))
1707 ++nops;
1708 }
1709 else
1710 {
1711 /* We don't know exactly what the previous instruction
1712 does. If the current instruction uses a coprocessor
1713 register, we must insert a NOP. If previous
1714 instruction may set the condition codes, and the
1715 current instruction uses them, we must insert two
1716 NOPS. */
bdaaa2e1 1717 /* Itbl support may require additional care here. */
252b5132
RH
1718 if (mips_optimize == 0
1719 || ((prev_pinfo & INSN_WRITE_COND_CODE)
1720 && (pinfo & INSN_READ_COND_CODE)))
1721 nops += 2;
1722 else if (pinfo & INSN_COP)
1723 ++nops;
1724 }
1725 }
1726 else if (! mips_opts.mips16
252b5132
RH
1727 && (prev_pinfo & INSN_WRITE_COND_CODE)
1728 && ! cop_interlocks)
1729 {
1730 /* The previous instruction sets the coprocessor condition
1731 codes, but does not require a general coprocessor delay
1732 (this means it is a floating point comparison
1733 instruction). If this instruction uses the condition
1734 codes, we need to insert a single NOP. */
beae10d5 1735 /* Itbl support may require additional care here. */
252b5132
RH
1736 if (mips_optimize == 0
1737 || (pinfo & INSN_READ_COND_CODE))
1738 ++nops;
1739 }
6b76fefe
CM
1740
1741 /* If we're fixing up mfhi/mflo for the r7000 and the
1742 previous insn was an mfhi/mflo and the current insn
1743 reads the register that the mfhi/mflo wrote to, then
1744 insert two nops. */
1745
1746 else if (mips_7000_hilo_fix
1747 && MF_HILO_INSN (prev_pinfo)
1748 && insn_uses_reg (ip, ((prev_insn.insn_opcode >> OP_SH_RD)
beae10d5
KH
1749 & OP_MASK_RD),
1750 MIPS_GR_REG))
6b76fefe
CM
1751 {
1752 nops += 2;
1753 }
1754
1755 /* If we're fixing up mfhi/mflo for the r7000 and the
1756 2nd previous insn was an mfhi/mflo and the current insn
1757 reads the register that the mfhi/mflo wrote to, then
1758 insert one nop. */
1759
1760 else if (mips_7000_hilo_fix
1761 && MF_HILO_INSN (prev_prev_insn.insn_opcode)
1762 && insn_uses_reg (ip, ((prev_prev_insn.insn_opcode >> OP_SH_RD)
1763 & OP_MASK_RD),
1764 MIPS_GR_REG))
bdaaa2e1 1765
6b76fefe 1766 {
f9419b05 1767 ++nops;
6b76fefe 1768 }
bdaaa2e1 1769
252b5132
RH
1770 else if (prev_pinfo & INSN_READ_LO)
1771 {
1772 /* The previous instruction reads the LO register; if the
1773 current instruction writes to the LO register, we must
bdaaa2e1 1774 insert two NOPS. Some newer processors have interlocks.
67c1ffbe
KH
1775 Also the tx39's multiply instructions can be executed
1776 immediately after a read from HI/LO (without the delay),
bdaaa2e1
KH
1777 though the tx39's divide insns still do require the
1778 delay. */
252b5132 1779 if (! (hilo_interlocks
048cdf86 1780 || (mips_opts.arch == CPU_R3900 && (pinfo & INSN_MULT)))
252b5132
RH
1781 && (mips_optimize == 0
1782 || (pinfo & INSN_WRITE_LO)))
1783 nops += 2;
1784 /* Most mips16 branch insns don't have a delay slot.
1785 If a read from LO is immediately followed by a branch
1786 to a write to LO we have a read followed by a write
1787 less than 2 insns away. We assume the target of
1788 a branch might be a write to LO, and insert a nop
bdaaa2e1 1789 between a read and an immediately following branch. */
252b5132
RH
1790 else if (mips_opts.mips16
1791 && (mips_optimize == 0
1792 || (pinfo & MIPS16_INSN_BRANCH)))
f9419b05 1793 ++nops;
252b5132
RH
1794 }
1795 else if (prev_insn.insn_mo->pinfo & INSN_READ_HI)
1796 {
1797 /* The previous instruction reads the HI register; if the
1798 current instruction writes to the HI register, we must
1799 insert a NOP. Some newer processors have interlocks.
bdaaa2e1 1800 Also the note tx39's multiply above. */
252b5132 1801 if (! (hilo_interlocks
048cdf86 1802 || (mips_opts.arch == CPU_R3900 && (pinfo & INSN_MULT)))
252b5132
RH
1803 && (mips_optimize == 0
1804 || (pinfo & INSN_WRITE_HI)))
1805 nops += 2;
1806 /* Most mips16 branch insns don't have a delay slot.
1807 If a read from HI is immediately followed by a branch
1808 to a write to HI we have a read followed by a write
1809 less than 2 insns away. We assume the target of
1810 a branch might be a write to HI, and insert a nop
bdaaa2e1 1811 between a read and an immediately following branch. */
252b5132
RH
1812 else if (mips_opts.mips16
1813 && (mips_optimize == 0
1814 || (pinfo & MIPS16_INSN_BRANCH)))
f9419b05 1815 ++nops;
252b5132
RH
1816 }
1817
1818 /* If the previous instruction was in a noreorder section, then
1819 we don't want to insert the nop after all. */
bdaaa2e1 1820 /* Itbl support may require additional care here. */
252b5132
RH
1821 if (prev_insn_unreordered)
1822 nops = 0;
1823
1824 /* There are two cases which require two intervening
1825 instructions: 1) setting the condition codes using a move to
1826 coprocessor instruction which requires a general coprocessor
1827 delay and then reading the condition codes 2) reading the HI
1828 or LO register and then writing to it (except on processors
1829 which have interlocks). If we are not already emitting a NOP
1830 instruction, we must check for these cases compared to the
1831 instruction previous to the previous instruction. */
1832 if ((! mips_opts.mips16
252b5132
RH
1833 && (prev_prev_insn.insn_mo->pinfo & INSN_COPROC_MOVE_DELAY)
1834 && (prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
1835 && (pinfo & INSN_READ_COND_CODE)
1836 && ! cop_interlocks)
1837 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_LO)
1838 && (pinfo & INSN_WRITE_LO)
1839 && ! (hilo_interlocks
048cdf86 1840 || (mips_opts.arch == CPU_R3900 && (pinfo & INSN_MULT))))
252b5132
RH
1841 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
1842 && (pinfo & INSN_WRITE_HI)
1843 && ! (hilo_interlocks
048cdf86 1844 || (mips_opts.arch == CPU_R3900 && (pinfo & INSN_MULT)))))
252b5132
RH
1845 prev_prev_nop = 1;
1846 else
1847 prev_prev_nop = 0;
1848
1849 if (prev_prev_insn_unreordered)
1850 prev_prev_nop = 0;
1851
1852 if (prev_prev_nop && nops == 0)
1853 ++nops;
1854
d766e8ec 1855 if (mips_fix_vr4120 && prev_insn.insn_mo->name)
60b63b72
RS
1856 {
1857 /* We're out of bits in pinfo, so we must resort to string
1858 ops here. Shortcuts are selected based on opcodes being
d766e8ec 1859 limited to the VR4120 instruction set. */
60b63b72
RS
1860 int min_nops = 0;
1861 const char *pn = prev_insn.insn_mo->name;
1862 const char *tn = ip->insn_mo->name;
1863 if (strncmp(pn, "macc", 4) == 0
1864 || strncmp(pn, "dmacc", 5) == 0)
1865 {
1866 /* Errata 21 - [D]DIV[U] after [D]MACC */
1867 if (strstr (tn, "div"))
1868 {
1869 min_nops = 1;
1870 }
1871
1872 /* Errata 23 - Continuous DMULT[U]/DMACC instructions */
1873 if (pn[0] == 'd' /* dmacc */
1874 && (strncmp(tn, "dmult", 5) == 0
1875 || strncmp(tn, "dmacc", 5) == 0))
1876 {
1877 min_nops = 1;
1878 }
1879
1880 /* Errata 24 - MT{LO,HI} after [D]MACC */
1881 if (strcmp (tn, "mtlo") == 0
1882 || strcmp (tn, "mthi") == 0)
1883 {
1884 min_nops = 1;
1885 }
1886
1887 }
1888 else if (strncmp(pn, "dmult", 5) == 0
1889 && (strncmp(tn, "dmult", 5) == 0
1890 || strncmp(tn, "dmacc", 5) == 0))
1891 {
1892 /* Here is the rest of errata 23. */
1893 min_nops = 1;
1894 }
1895 if (nops < min_nops)
1896 nops = min_nops;
1897 }
1898
252b5132
RH
1899 /* If we are being given a nop instruction, don't bother with
1900 one of the nops we would otherwise output. This will only
1901 happen when a nop instruction is used with mips_optimize set
1902 to 0. */
1903 if (nops > 0
1904 && ! mips_opts.noreorder
156c2f8b 1905 && ip->insn_opcode == (unsigned) (mips_opts.mips16 ? 0x6500 : 0))
252b5132
RH
1906 --nops;
1907
1908 /* Now emit the right number of NOP instructions. */
1909 if (nops > 0 && ! mips_opts.noreorder)
1910 {
1911 fragS *old_frag;
1912 unsigned long old_frag_offset;
1913 int i;
1914 struct insn_label_list *l;
1915
1916 old_frag = frag_now;
1917 old_frag_offset = frag_now_fix ();
1918
1919 for (i = 0; i < nops; i++)
1920 emit_nop ();
1921
1922 if (listing)
1923 {
1924 listing_prev_line ();
1925 /* We may be at the start of a variant frag. In case we
1926 are, make sure there is enough space for the frag
1927 after the frags created by listing_prev_line. The
1928 argument to frag_grow here must be at least as large
1929 as the argument to all other calls to frag_grow in
1930 this file. We don't have to worry about being in the
1931 middle of a variant frag, because the variants insert
1932 all needed nop instructions themselves. */
1933 frag_grow (40);
1934 }
1935
1936 for (l = insn_labels; l != NULL; l = l->next)
1937 {
98aa84af
AM
1938 valueT val;
1939
252b5132 1940 assert (S_GET_SEGMENT (l->label) == now_seg);
49309057 1941 symbol_set_frag (l->label, frag_now);
98aa84af 1942 val = (valueT) frag_now_fix ();
252b5132
RH
1943 /* mips16 text labels are stored as odd. */
1944 if (mips_opts.mips16)
f9419b05 1945 ++val;
98aa84af 1946 S_SET_VALUE (l->label, val);
252b5132
RH
1947 }
1948
1949#ifndef NO_ECOFF_DEBUGGING
1950 if (ECOFF_DEBUGGING)
1951 ecoff_fix_loc (old_frag, old_frag_offset);
1952#endif
1953 }
1954 else if (prev_nop_frag != NULL)
1955 {
1956 /* We have a frag holding nops we may be able to remove. If
1957 we don't need any nops, we can decrease the size of
1958 prev_nop_frag by the size of one instruction. If we do
bdaaa2e1 1959 need some nops, we count them in prev_nops_required. */
252b5132
RH
1960 if (prev_nop_frag_since == 0)
1961 {
1962 if (nops == 0)
1963 {
1964 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1965 --prev_nop_frag_holds;
1966 }
1967 else
1968 prev_nop_frag_required += nops;
1969 }
1970 else
1971 {
1972 if (prev_prev_nop == 0)
1973 {
1974 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1975 --prev_nop_frag_holds;
1976 }
1977 else
1978 ++prev_nop_frag_required;
1979 }
1980
1981 if (prev_nop_frag_holds <= prev_nop_frag_required)
1982 prev_nop_frag = NULL;
1983
1984 ++prev_nop_frag_since;
1985
1986 /* Sanity check: by the time we reach the second instruction
1987 after prev_nop_frag, we should have used up all the nops
1988 one way or another. */
1989 assert (prev_nop_frag_since <= 1 || prev_nop_frag == NULL);
1990 }
1991 }
1992
895921c9
MR
1993 /* Record the frag type before frag_var. */
1994 if (prev_insn_frag)
1995 prev_insn_frag_type = prev_insn_frag->fr_type;
1996
4d7206a2 1997 if (address_expr
0b25d3e6 1998 && *reloc_type == BFD_RELOC_16_PCREL_S2
4a6a3df4
AO
1999 && (pinfo & INSN_UNCOND_BRANCH_DELAY || pinfo & INSN_COND_BRANCH_DELAY
2000 || pinfo & INSN_COND_BRANCH_LIKELY)
2001 && mips_relax_branch
2002 /* Don't try branch relaxation within .set nomacro, or within
2003 .set noat if we use $at for PIC computations. If it turns
2004 out that the branch was out-of-range, we'll get an error. */
2005 && !mips_opts.warn_about_macros
2006 && !(mips_opts.noat && mips_pic != NO_PIC)
2007 && !mips_opts.mips16)
2008 {
895921c9 2009 relaxed_branch = TRUE;
4a6a3df4
AO
2010 f = frag_var (rs_machine_dependent,
2011 relaxed_branch_length
2012 (NULL, NULL,
2013 (pinfo & INSN_UNCOND_BRANCH_DELAY) ? -1
2014 : (pinfo & INSN_COND_BRANCH_LIKELY) ? 1 : 0), 4,
2015 RELAX_BRANCH_ENCODE
af6ae2ad 2016 (pinfo & INSN_UNCOND_BRANCH_DELAY,
4a6a3df4
AO
2017 pinfo & INSN_COND_BRANCH_LIKELY,
2018 pinfo & INSN_WRITE_GPR_31,
2019 0),
2020 address_expr->X_add_symbol,
2021 address_expr->X_add_number,
2022 0);
2023 *reloc_type = BFD_RELOC_UNUSED;
2024 }
2025 else if (*reloc_type > BFD_RELOC_UNUSED)
252b5132
RH
2026 {
2027 /* We need to set up a variant frag. */
2028 assert (mips_opts.mips16 && address_expr != NULL);
2029 f = frag_var (rs_machine_dependent, 4, 0,
f6688943 2030 RELAX_MIPS16_ENCODE (*reloc_type - BFD_RELOC_UNUSED,
252b5132
RH
2031 mips16_small, mips16_ext,
2032 (prev_pinfo
2033 & INSN_UNCOND_BRANCH_DELAY),
f6688943 2034 (*prev_insn_reloc_type
252b5132 2035 == BFD_RELOC_MIPS16_JMP)),
c4e7957c 2036 make_expr_symbol (address_expr), 0, NULL);
252b5132 2037 }
252b5132
RH
2038 else if (mips_opts.mips16
2039 && ! ip->use_extend
f6688943 2040 && *reloc_type != BFD_RELOC_MIPS16_JMP)
252b5132
RH
2041 {
2042 /* Make sure there is enough room to swap this instruction with
2043 a following jump instruction. */
2044 frag_grow (6);
2045 f = frag_more (2);
2046 }
2047 else
2048 {
2049 if (mips_opts.mips16
2050 && mips_opts.noreorder
2051 && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
2052 as_warn (_("extended instruction in delay slot"));
2053
4d7206a2
RS
2054 if (mips_relax.sequence)
2055 {
2056 /* If we've reached the end of this frag, turn it into a variant
2057 frag and record the information for the instructions we've
2058 written so far. */
2059 if (frag_room () < 4)
2060 relax_close_frag ();
2061 mips_relax.sizes[mips_relax.sequence - 1] += 4;
2062 }
2063
584892a6
RS
2064 if (mips_relax.sequence != 2)
2065 mips_macro_warning.sizes[0] += 4;
2066 if (mips_relax.sequence != 1)
2067 mips_macro_warning.sizes[1] += 4;
2068
252b5132
RH
2069 f = frag_more (4);
2070 }
2071
f6688943
TS
2072 fixp[0] = fixp[1] = fixp[2] = NULL;
2073 if (address_expr != NULL && *reloc_type < BFD_RELOC_UNUSED)
252b5132
RH
2074 {
2075 if (address_expr->X_op == O_constant)
2076 {
4db1a35d 2077 valueT tmp;
f6688943
TS
2078
2079 switch (*reloc_type)
252b5132
RH
2080 {
2081 case BFD_RELOC_32:
2082 ip->insn_opcode |= address_expr->X_add_number;
2083 break;
2084
f6688943 2085 case BFD_RELOC_MIPS_HIGHEST:
6344efa4
AM
2086 tmp = (address_expr->X_add_number
2087 + ((valueT) 0x8000 << 32) + 0x80008000) >> 16;
f6688943
TS
2088 tmp >>= 16;
2089 ip->insn_opcode |= (tmp >> 16) & 0xffff;
2090 break;
2091
2092 case BFD_RELOC_MIPS_HIGHER:
2093 tmp = (address_expr->X_add_number + 0x80008000) >> 16;
2094 ip->insn_opcode |= (tmp >> 16) & 0xffff;
2095 break;
2096
2097 case BFD_RELOC_HI16_S:
2098 ip->insn_opcode |= ((address_expr->X_add_number + 0x8000)
2099 >> 16) & 0xffff;
2100 break;
2101
2102 case BFD_RELOC_HI16:
2103 ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
2104 break;
2105
252b5132 2106 case BFD_RELOC_LO16:
ed6fb7bd 2107 case BFD_RELOC_MIPS_GOT_DISP:
252b5132
RH
2108 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2109 break;
2110
2111 case BFD_RELOC_MIPS_JMP:
2112 if ((address_expr->X_add_number & 3) != 0)
2113 as_bad (_("jump to misaligned address (0x%lx)"),
2114 (unsigned long) address_expr->X_add_number);
f3c0ec86 2115 if (address_expr->X_add_number & ~0xfffffff)
7496292d
TS
2116 as_bad (_("jump address range overflow (0x%lx)"),
2117 (unsigned long) address_expr->X_add_number);
252b5132
RH
2118 ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
2119 break;
2120
2121 case BFD_RELOC_MIPS16_JMP:
2122 if ((address_expr->X_add_number & 3) != 0)
2123 as_bad (_("jump to misaligned address (0x%lx)"),
2124 (unsigned long) address_expr->X_add_number);
f3c0ec86 2125 if (address_expr->X_add_number & ~0xfffffff)
7496292d
TS
2126 as_bad (_("jump address range overflow (0x%lx)"),
2127 (unsigned long) address_expr->X_add_number);
252b5132
RH
2128 ip->insn_opcode |=
2129 (((address_expr->X_add_number & 0x7c0000) << 3)
2130 | ((address_expr->X_add_number & 0xf800000) >> 7)
2131 | ((address_expr->X_add_number & 0x3fffc) >> 2));
2132 break;
2133
252b5132
RH
2134 case BFD_RELOC_16_PCREL_S2:
2135 goto need_reloc;
2136
2137 default:
2138 internalError ();
2139 }
2140 }
2141 else
252b5132 2142 need_reloc:
4d7206a2
RS
2143 {
2144 reloc_howto_type *howto;
2145 int i;
34ce925e 2146
4d7206a2
RS
2147 /* In a compound relocation, it is the final (outermost)
2148 operator that determines the relocated field. */
2149 for (i = 1; i < 3; i++)
2150 if (reloc_type[i] == BFD_RELOC_UNUSED)
2151 break;
34ce925e 2152
4d7206a2
RS
2153 howto = bfd_reloc_type_lookup (stdoutput, reloc_type[i - 1]);
2154 fixp[0] = fix_new_exp (frag_now, f - frag_now->fr_literal,
2155 bfd_get_reloc_size(howto),
2156 address_expr,
2157 reloc_type[0] == BFD_RELOC_16_PCREL_S2,
2158 reloc_type[0]);
2159
2160 /* These relocations can have an addend that won't fit in
2161 4 octets for 64bit assembly. */
2162 if (HAVE_64BIT_GPRS
2163 && ! howto->partial_inplace
2164 && (reloc_type[0] == BFD_RELOC_16
2165 || reloc_type[0] == BFD_RELOC_32
2166 || reloc_type[0] == BFD_RELOC_MIPS_JMP
2167 || reloc_type[0] == BFD_RELOC_HI16_S
2168 || reloc_type[0] == BFD_RELOC_LO16
2169 || reloc_type[0] == BFD_RELOC_GPREL16
2170 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
2171 || reloc_type[0] == BFD_RELOC_GPREL32
2172 || reloc_type[0] == BFD_RELOC_64
2173 || reloc_type[0] == BFD_RELOC_CTOR
2174 || reloc_type[0] == BFD_RELOC_MIPS_SUB
2175 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
2176 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
2177 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
2178 || reloc_type[0] == BFD_RELOC_MIPS_REL16
2179 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT))
2180 fixp[0]->fx_no_overflow = 1;
2181
2182 if (mips_relax.sequence)
2183 {
2184 if (mips_relax.first_fixup == 0)
2185 mips_relax.first_fixup = fixp[0];
2186 }
2187 else if (reloc_needs_lo_p (*reloc_type))
2188 {
2189 struct mips_hi_fixup *hi_fixup;
252b5132 2190
4d7206a2
RS
2191 /* Reuse the last entry if it already has a matching %lo. */
2192 hi_fixup = mips_hi_fixup_list;
2193 if (hi_fixup == 0
2194 || !fixup_has_matching_lo_p (hi_fixup->fixp))
2195 {
2196 hi_fixup = ((struct mips_hi_fixup *)
2197 xmalloc (sizeof (struct mips_hi_fixup)));
2198 hi_fixup->next = mips_hi_fixup_list;
2199 mips_hi_fixup_list = hi_fixup;
252b5132 2200 }
4d7206a2
RS
2201 hi_fixup->fixp = fixp[0];
2202 hi_fixup->seg = now_seg;
2203 }
f6688943 2204
4d7206a2
RS
2205 /* Add fixups for the second and third relocations, if given.
2206 Note that the ABI allows the second relocation to be
2207 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the
2208 moment we only use RSS_UNDEF, but we could add support
2209 for the others if it ever becomes necessary. */
2210 for (i = 1; i < 3; i++)
2211 if (reloc_type[i] != BFD_RELOC_UNUSED)
2212 {
2213 address_expr->X_op = O_absent;
2214 address_expr->X_add_symbol = 0;
2215 address_expr->X_add_number = 0;
34ce925e 2216
4d7206a2
RS
2217 fixp[i] = fix_new_exp (frag_now, fixp[0]->fx_where,
2218 fixp[0]->fx_size, address_expr,
2219 FALSE, reloc_type[i]);
2220 }
252b5132
RH
2221 }
2222 }
2223
2224 if (! mips_opts.mips16)
c5dd6aab
DJ
2225 {
2226 md_number_to_chars (f, ip->insn_opcode, 4);
2227#ifdef OBJ_ELF
2228 dwarf2_emit_insn (4);
2229#endif
2230 }
f6688943 2231 else if (*reloc_type == BFD_RELOC_MIPS16_JMP)
252b5132
RH
2232 {
2233 md_number_to_chars (f, ip->insn_opcode >> 16, 2);
2234 md_number_to_chars (f + 2, ip->insn_opcode & 0xffff, 2);
c5dd6aab
DJ
2235#ifdef OBJ_ELF
2236 dwarf2_emit_insn (4);
2237#endif
252b5132
RH
2238 }
2239 else
2240 {
2241 if (ip->use_extend)
2242 {
2243 md_number_to_chars (f, 0xf000 | ip->extend, 2);
2244 f += 2;
2245 }
2246 md_number_to_chars (f, ip->insn_opcode, 2);
c5dd6aab
DJ
2247#ifdef OBJ_ELF
2248 dwarf2_emit_insn (ip->use_extend ? 4 : 2);
2249#endif
252b5132
RH
2250 }
2251
2252 /* Update the register mask information. */
2253 if (! mips_opts.mips16)
2254 {
2255 if (pinfo & INSN_WRITE_GPR_D)
2256 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
2257 if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2258 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
2259 if (pinfo & INSN_READ_GPR_S)
2260 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
2261 if (pinfo & INSN_WRITE_GPR_31)
f9419b05 2262 mips_gprmask |= 1 << RA;
252b5132
RH
2263 if (pinfo & INSN_WRITE_FPR_D)
2264 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
2265 if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2266 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
2267 if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2268 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
2269 if ((pinfo & INSN_READ_FPR_R) != 0)
2270 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
2271 if (pinfo & INSN_COP)
2272 {
bdaaa2e1
KH
2273 /* We don't keep enough information to sort these cases out.
2274 The itbl support does keep this information however, although
2275 we currently don't support itbl fprmats as part of the cop
2276 instruction. May want to add this support in the future. */
252b5132
RH
2277 }
2278 /* Never set the bit for $0, which is always zero. */
beae10d5 2279 mips_gprmask &= ~1 << 0;
252b5132
RH
2280 }
2281 else
2282 {
2283 if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2284 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RX)
2285 & MIPS16OP_MASK_RX);
2286 if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2287 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RY)
2288 & MIPS16OP_MASK_RY);
2289 if (pinfo & MIPS16_INSN_WRITE_Z)
2290 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RZ)
2291 & MIPS16OP_MASK_RZ);
2292 if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2293 mips_gprmask |= 1 << TREG;
2294 if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2295 mips_gprmask |= 1 << SP;
2296 if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2297 mips_gprmask |= 1 << RA;
2298 if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2299 mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2300 if (pinfo & MIPS16_INSN_READ_Z)
2301 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
2302 & MIPS16OP_MASK_MOVE32Z);
2303 if (pinfo & MIPS16_INSN_READ_GPR_X)
2304 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
2305 & MIPS16OP_MASK_REGR32);
2306 }
2307
4d7206a2 2308 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
252b5132
RH
2309 {
2310 /* Filling the branch delay slot is more complex. We try to
2311 switch the branch with the previous instruction, which we can
2312 do if the previous instruction does not set up a condition
2313 that the branch tests and if the branch is not itself the
2314 target of any branch. */
2315 if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2316 || (pinfo & INSN_COND_BRANCH_DELAY))
2317 {
2318 if (mips_optimize < 2
2319 /* If we have seen .set volatile or .set nomove, don't
2320 optimize. */
2321 || mips_opts.nomove != 0
2322 /* If we had to emit any NOP instructions, then we
2323 already know we can not swap. */
2324 || nops != 0
2325 /* If we don't even know the previous insn, we can not
bdaaa2e1 2326 swap. */
252b5132
RH
2327 || ! prev_insn_valid
2328 /* If the previous insn is already in a branch delay
2329 slot, then we can not swap. */
2330 || prev_insn_is_delay_slot
2331 /* If the previous previous insn was in a .set
2332 noreorder, we can't swap. Actually, the MIPS
2333 assembler will swap in this situation. However, gcc
2334 configured -with-gnu-as will generate code like
2335 .set noreorder
2336 lw $4,XXX
2337 .set reorder
2338 INSN
2339 bne $4,$0,foo
2340 in which we can not swap the bne and INSN. If gcc is
2341 not configured -with-gnu-as, it does not output the
2342 .set pseudo-ops. We don't have to check
2343 prev_insn_unreordered, because prev_insn_valid will
2344 be 0 in that case. We don't want to use
2345 prev_prev_insn_valid, because we do want to be able
2346 to swap at the start of a function. */
2347 || prev_prev_insn_unreordered
2348 /* If the branch is itself the target of a branch, we
2349 can not swap. We cheat on this; all we check for is
2350 whether there is a label on this instruction. If
2351 there are any branches to anything other than a
2352 label, users must use .set noreorder. */
2353 || insn_labels != NULL
895921c9
MR
2354 /* If the previous instruction is in a variant frag
2355 other than this branch's one, we cannot do the swap.
2356 This does not apply to the mips16, which uses variant
2357 frags for different purposes. */
252b5132 2358 || (! mips_opts.mips16
895921c9 2359 && prev_insn_frag_type == rs_machine_dependent)
252b5132
RH
2360 /* If the branch reads the condition codes, we don't
2361 even try to swap, because in the sequence
2362 ctc1 $X,$31
2363 INSN
2364 INSN
2365 bc1t LABEL
2366 we can not swap, and I don't feel like handling that
2367 case. */
2368 || (! mips_opts.mips16
81912461
ILT
2369 && (pinfo & INSN_READ_COND_CODE)
2370 && ! cop_interlocks)
252b5132 2371 /* We can not swap with an instruction that requires a
67c1ffbe 2372 delay slot, because the target of the branch might
252b5132
RH
2373 interfere with that instruction. */
2374 || (! mips_opts.mips16
252b5132 2375 && (prev_pinfo
bdaaa2e1 2376 /* Itbl support may require additional care here. */
252b5132
RH
2377 & (INSN_LOAD_COPROC_DELAY
2378 | INSN_COPROC_MOVE_DELAY
81912461
ILT
2379 | INSN_WRITE_COND_CODE))
2380 && ! cop_interlocks)
252b5132 2381 || (! (hilo_interlocks
048cdf86 2382 || (mips_opts.arch == CPU_R3900 && (pinfo & INSN_MULT)))
252b5132
RH
2383 && (prev_pinfo
2384 & (INSN_READ_LO
2385 | INSN_READ_HI)))
2386 || (! mips_opts.mips16
81912461
ILT
2387 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY)
2388 && ! gpr_interlocks)
252b5132 2389 || (! mips_opts.mips16
bdaaa2e1 2390 /* Itbl support may require additional care here. */
81912461
ILT
2391 && (prev_pinfo & INSN_COPROC_MEMORY_DELAY)
2392 && ! cop_mem_interlocks)
252b5132
RH
2393 /* We can not swap with a branch instruction. */
2394 || (prev_pinfo
2395 & (INSN_UNCOND_BRANCH_DELAY
2396 | INSN_COND_BRANCH_DELAY
2397 | INSN_COND_BRANCH_LIKELY))
2398 /* We do not swap with a trap instruction, since it
2399 complicates trap handlers to have the trap
2400 instruction be in a delay slot. */
2401 || (prev_pinfo & INSN_TRAP)
2402 /* If the branch reads a register that the previous
2403 instruction sets, we can not swap. */
2404 || (! mips_opts.mips16
2405 && (prev_pinfo & INSN_WRITE_GPR_T)
2406 && insn_uses_reg (ip,
2407 ((prev_insn.insn_opcode >> OP_SH_RT)
2408 & OP_MASK_RT),
2409 MIPS_GR_REG))
2410 || (! mips_opts.mips16
2411 && (prev_pinfo & INSN_WRITE_GPR_D)
2412 && insn_uses_reg (ip,
2413 ((prev_insn.insn_opcode >> OP_SH_RD)
2414 & OP_MASK_RD),
2415 MIPS_GR_REG))
2416 || (mips_opts.mips16
2417 && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2418 && insn_uses_reg (ip,
2419 ((prev_insn.insn_opcode
2420 >> MIPS16OP_SH_RX)
2421 & MIPS16OP_MASK_RX),
2422 MIPS16_REG))
2423 || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2424 && insn_uses_reg (ip,
2425 ((prev_insn.insn_opcode
2426 >> MIPS16OP_SH_RY)
2427 & MIPS16OP_MASK_RY),
2428 MIPS16_REG))
2429 || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2430 && insn_uses_reg (ip,
2431 ((prev_insn.insn_opcode
2432 >> MIPS16OP_SH_RZ)
2433 & MIPS16OP_MASK_RZ),
2434 MIPS16_REG))
2435 || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2436 && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2437 || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2438 && insn_uses_reg (ip, RA, MIPS_GR_REG))
2439 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2440 && insn_uses_reg (ip,
2441 MIPS16OP_EXTRACT_REG32R (prev_insn.
2442 insn_opcode),
2443 MIPS_GR_REG))))
2444 /* If the branch writes a register that the previous
2445 instruction sets, we can not swap (we know that
2446 branches write only to RD or to $31). */
2447 || (! mips_opts.mips16
2448 && (prev_pinfo & INSN_WRITE_GPR_T)
2449 && (((pinfo & INSN_WRITE_GPR_D)
2450 && (((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT)
2451 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2452 || ((pinfo & INSN_WRITE_GPR_31)
2453 && (((prev_insn.insn_opcode >> OP_SH_RT)
2454 & OP_MASK_RT)
f9419b05 2455 == RA))))
252b5132
RH
2456 || (! mips_opts.mips16
2457 && (prev_pinfo & INSN_WRITE_GPR_D)
2458 && (((pinfo & INSN_WRITE_GPR_D)
2459 && (((prev_insn.insn_opcode >> OP_SH_RD) & OP_MASK_RD)
2460 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2461 || ((pinfo & INSN_WRITE_GPR_31)
2462 && (((prev_insn.insn_opcode >> OP_SH_RD)
2463 & OP_MASK_RD)
f9419b05 2464 == RA))))
252b5132
RH
2465 || (mips_opts.mips16
2466 && (pinfo & MIPS16_INSN_WRITE_31)
2467 && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2468 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2469 && (MIPS16OP_EXTRACT_REG32R (prev_insn.insn_opcode)
2470 == RA))))
2471 /* If the branch writes a register that the previous
2472 instruction reads, we can not swap (we know that
2473 branches only write to RD or to $31). */
2474 || (! mips_opts.mips16
2475 && (pinfo & INSN_WRITE_GPR_D)
2476 && insn_uses_reg (&prev_insn,
2477 ((ip->insn_opcode >> OP_SH_RD)
2478 & OP_MASK_RD),
2479 MIPS_GR_REG))
2480 || (! mips_opts.mips16
2481 && (pinfo & INSN_WRITE_GPR_31)
f9419b05 2482 && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
252b5132
RH
2483 || (mips_opts.mips16
2484 && (pinfo & MIPS16_INSN_WRITE_31)
2485 && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2486 /* If we are generating embedded PIC code, the branch
2487 might be expanded into a sequence which uses $at, so
2488 we can't swap with an instruction which reads it. */
2489 || (mips_pic == EMBEDDED_PIC
2490 && insn_uses_reg (&prev_insn, AT, MIPS_GR_REG))
2491 /* If the previous previous instruction has a load
2492 delay, and sets a register that the branch reads, we
2493 can not swap. */
2494 || (! mips_opts.mips16
bdaaa2e1 2495 /* Itbl support may require additional care here. */
81912461
ILT
2496 && (((prev_prev_insn.insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
2497 && ! cop_interlocks)
2498 || ((prev_prev_insn.insn_mo->pinfo
2499 & INSN_LOAD_MEMORY_DELAY)
2500 && ! gpr_interlocks))
252b5132
RH
2501 && insn_uses_reg (ip,
2502 ((prev_prev_insn.insn_opcode >> OP_SH_RT)
2503 & OP_MASK_RT),
2504 MIPS_GR_REG))
2505 /* If one instruction sets a condition code and the
2506 other one uses a condition code, we can not swap. */
2507 || ((pinfo & INSN_READ_COND_CODE)
2508 && (prev_pinfo & INSN_WRITE_COND_CODE))
2509 || ((pinfo & INSN_WRITE_COND_CODE)
2510 && (prev_pinfo & INSN_READ_COND_CODE))
2511 /* If the previous instruction uses the PC, we can not
2512 swap. */
2513 || (mips_opts.mips16
2514 && (prev_pinfo & MIPS16_INSN_READ_PC))
2515 /* If the previous instruction was extended, we can not
2516 swap. */
2517 || (mips_opts.mips16 && prev_insn_extended)
2518 /* If the previous instruction had a fixup in mips16
2519 mode, we can not swap. This normally means that the
2520 previous instruction was a 4 byte branch anyhow. */
f6688943 2521 || (mips_opts.mips16 && prev_insn_fixp[0])
bdaaa2e1
KH
2522 /* If the previous instruction is a sync, sync.l, or
2523 sync.p, we can not swap. */
f173e82e 2524 || (prev_pinfo & INSN_SYNC))
252b5132
RH
2525 {
2526 /* We could do even better for unconditional branches to
2527 portions of this object file; we could pick up the
2528 instruction at the destination, put it in the delay
2529 slot, and bump the destination address. */
2530 emit_nop ();
2531 /* Update the previous insn information. */
2532 prev_prev_insn = *ip;
2533 prev_insn.insn_mo = &dummy_opcode;
2534 }
2535 else
2536 {
2537 /* It looks like we can actually do the swap. */
2538 if (! mips_opts.mips16)
2539 {
2540 char *prev_f;
2541 char temp[4];
2542
2543 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
895921c9
MR
2544 if (!relaxed_branch)
2545 {
2546 /* If this is not a relaxed branch, then just
2547 swap the instructions. */
2548 memcpy (temp, prev_f, 4);
2549 memcpy (prev_f, f, 4);
2550 memcpy (f, temp, 4);
2551 }
2552 else
2553 {
2554 /* If this is a relaxed branch, then we move the
2555 instruction to be placed in the delay slot to
2556 the current frag, shrinking the fixed part of
2557 the originating frag. If the branch occupies
2558 the tail of the latter, we move it backwards,
2559 into the space freed by the moved instruction. */
2560 f = frag_more (4);
2561 memcpy (f, prev_f, 4);
2562 prev_insn_frag->fr_fix -= 4;
2563 if (prev_insn_frag->fr_type == rs_machine_dependent)
2564 memmove (prev_f, prev_f + 4, prev_insn_frag->fr_var);
2565 }
2566
f6688943
TS
2567 if (prev_insn_fixp[0])
2568 {
2569 prev_insn_fixp[0]->fx_frag = frag_now;
2570 prev_insn_fixp[0]->fx_where = f - frag_now->fr_literal;
2571 }
2572 if (prev_insn_fixp[1])
2573 {
2574 prev_insn_fixp[1]->fx_frag = frag_now;
2575 prev_insn_fixp[1]->fx_where = f - frag_now->fr_literal;
2576 }
2577 if (prev_insn_fixp[2])
252b5132 2578 {
f6688943
TS
2579 prev_insn_fixp[2]->fx_frag = frag_now;
2580 prev_insn_fixp[2]->fx_where = f - frag_now->fr_literal;
252b5132 2581 }
f5040a92
AO
2582 if (prev_insn_fixp[0] && HAVE_NEWABI
2583 && prev_insn_frag != frag_now
2584 && (prev_insn_fixp[0]->fx_r_type
2585 == BFD_RELOC_MIPS_GOT_DISP
2586 || (prev_insn_fixp[0]->fx_r_type
2587 == BFD_RELOC_MIPS_CALL16)))
2588 {
2589 /* To avoid confusion in tc_gen_reloc, we must
2590 ensure that this does not become a variant
2591 frag. */
2592 force_new_frag = TRUE;
2593 }
895921c9
MR
2594
2595 if (!relaxed_branch)
f6688943 2596 {
895921c9
MR
2597 if (fixp[0])
2598 {
2599 fixp[0]->fx_frag = prev_insn_frag;
2600 fixp[0]->fx_where = prev_insn_where;
2601 }
2602 if (fixp[1])
2603 {
2604 fixp[1]->fx_frag = prev_insn_frag;
2605 fixp[1]->fx_where = prev_insn_where;
2606 }
2607 if (fixp[2])
2608 {
2609 fixp[2]->fx_frag = prev_insn_frag;
2610 fixp[2]->fx_where = prev_insn_where;
2611 }
f6688943 2612 }
895921c9 2613 else if (prev_insn_frag->fr_type == rs_machine_dependent)
f6688943 2614 {
895921c9
MR
2615 if (fixp[0])
2616 fixp[0]->fx_where -= 4;
2617 if (fixp[1])
2618 fixp[1]->fx_where -= 4;
2619 if (fixp[2])
2620 fixp[2]->fx_where -= 4;
252b5132
RH
2621 }
2622 }
2623 else
2624 {
2625 char *prev_f;
2626 char temp[2];
2627
f6688943
TS
2628 assert (prev_insn_fixp[0] == NULL);
2629 assert (prev_insn_fixp[1] == NULL);
2630 assert (prev_insn_fixp[2] == NULL);
252b5132
RH
2631 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2632 memcpy (temp, prev_f, 2);
2633 memcpy (prev_f, f, 2);
f6688943 2634 if (*reloc_type != BFD_RELOC_MIPS16_JMP)
252b5132 2635 {
f6688943 2636 assert (*reloc_type == BFD_RELOC_UNUSED);
252b5132
RH
2637 memcpy (f, temp, 2);
2638 }
2639 else
2640 {
2641 memcpy (f, f + 2, 2);
2642 memcpy (f + 2, temp, 2);
2643 }
f6688943
TS
2644 if (fixp[0])
2645 {
2646 fixp[0]->fx_frag = prev_insn_frag;
2647 fixp[0]->fx_where = prev_insn_where;
2648 }
2649 if (fixp[1])
2650 {
2651 fixp[1]->fx_frag = prev_insn_frag;
2652 fixp[1]->fx_where = prev_insn_where;
2653 }
2654 if (fixp[2])
252b5132 2655 {
f6688943
TS
2656 fixp[2]->fx_frag = prev_insn_frag;
2657 fixp[2]->fx_where = prev_insn_where;
252b5132
RH
2658 }
2659 }
2660
2661 /* Update the previous insn information; leave prev_insn
2662 unchanged. */
2663 prev_prev_insn = *ip;
2664 }
2665 prev_insn_is_delay_slot = 1;
2666
2667 /* If that was an unconditional branch, forget the previous
2668 insn information. */
2669 if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2670 {
2671 prev_prev_insn.insn_mo = &dummy_opcode;
2672 prev_insn.insn_mo = &dummy_opcode;
2673 }
2674
f6688943
TS
2675 prev_insn_fixp[0] = NULL;
2676 prev_insn_fixp[1] = NULL;
2677 prev_insn_fixp[2] = NULL;
2678 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2679 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2680 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
252b5132
RH
2681 prev_insn_extended = 0;
2682 }
2683 else if (pinfo & INSN_COND_BRANCH_LIKELY)
2684 {
2685 /* We don't yet optimize a branch likely. What we should do
2686 is look at the target, copy the instruction found there
2687 into the delay slot, and increment the branch to jump to
2688 the next instruction. */
2689 emit_nop ();
2690 /* Update the previous insn information. */
2691 prev_prev_insn = *ip;
2692 prev_insn.insn_mo = &dummy_opcode;
f6688943
TS
2693 prev_insn_fixp[0] = NULL;
2694 prev_insn_fixp[1] = NULL;
2695 prev_insn_fixp[2] = NULL;
2696 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2697 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2698 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
252b5132
RH
2699 prev_insn_extended = 0;
2700 }
2701 else
2702 {
2703 /* Update the previous insn information. */
2704 if (nops > 0)
2705 prev_prev_insn.insn_mo = &dummy_opcode;
2706 else
2707 prev_prev_insn = prev_insn;
2708 prev_insn = *ip;
2709
2710 /* Any time we see a branch, we always fill the delay slot
2711 immediately; since this insn is not a branch, we know it
2712 is not in a delay slot. */
2713 prev_insn_is_delay_slot = 0;
2714
f6688943
TS
2715 prev_insn_fixp[0] = fixp[0];
2716 prev_insn_fixp[1] = fixp[1];
2717 prev_insn_fixp[2] = fixp[2];
2718 prev_insn_reloc_type[0] = reloc_type[0];
2719 prev_insn_reloc_type[1] = reloc_type[1];
2720 prev_insn_reloc_type[2] = reloc_type[2];
252b5132
RH
2721 if (mips_opts.mips16)
2722 prev_insn_extended = (ip->use_extend
f6688943 2723 || *reloc_type > BFD_RELOC_UNUSED);
252b5132
RH
2724 }
2725
2726 prev_prev_insn_unreordered = prev_insn_unreordered;
2727 prev_insn_unreordered = 0;
2728 prev_insn_frag = frag_now;
2729 prev_insn_where = f - frag_now->fr_literal;
2730 prev_insn_valid = 1;
2731 }
4d7206a2 2732 else if (mips_relax.sequence != 2)
252b5132
RH
2733 {
2734 /* We need to record a bit of information even when we are not
2735 reordering, in order to determine the base address for mips16
2736 PC relative relocs. */
2737 prev_prev_insn = prev_insn;
2738 prev_insn = *ip;
f6688943
TS
2739 prev_insn_reloc_type[0] = reloc_type[0];
2740 prev_insn_reloc_type[1] = reloc_type[1];
2741 prev_insn_reloc_type[2] = reloc_type[2];
252b5132
RH
2742 prev_prev_insn_unreordered = prev_insn_unreordered;
2743 prev_insn_unreordered = 1;
2744 }
2745
2746 /* We just output an insn, so the next one doesn't have a label. */
2747 mips_clear_insn_labels ();
252b5132
RH
2748}
2749
2750/* This function forgets that there was any previous instruction or
2751 label. If PRESERVE is non-zero, it remembers enough information to
bdaaa2e1 2752 know whether nops are needed before a noreorder section. */
252b5132
RH
2753
2754static void
17a2f251 2755mips_no_prev_insn (int preserve)
252b5132
RH
2756{
2757 if (! preserve)
2758 {
2759 prev_insn.insn_mo = &dummy_opcode;
2760 prev_prev_insn.insn_mo = &dummy_opcode;
2761 prev_nop_frag = NULL;
2762 prev_nop_frag_holds = 0;
2763 prev_nop_frag_required = 0;
2764 prev_nop_frag_since = 0;
2765 }
2766 prev_insn_valid = 0;
2767 prev_insn_is_delay_slot = 0;
2768 prev_insn_unreordered = 0;
2769 prev_insn_extended = 0;
f6688943
TS
2770 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2771 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2772 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
252b5132
RH
2773 prev_prev_insn_unreordered = 0;
2774 mips_clear_insn_labels ();
2775}
2776
2777/* This function must be called whenever we turn on noreorder or emit
2778 something other than instructions. It inserts any NOPS which might
2779 be needed by the previous instruction, and clears the information
2780 kept for the previous instructions. The INSNS parameter is true if
bdaaa2e1 2781 instructions are to follow. */
252b5132
RH
2782
2783static void
17a2f251 2784mips_emit_delays (bfd_boolean insns)
252b5132
RH
2785{
2786 if (! mips_opts.noreorder)
2787 {
2788 int nops;
2789
2790 nops = 0;
2791 if ((! mips_opts.mips16
81912461
ILT
2792 && ((prev_insn.insn_mo->pinfo
2793 & (INSN_LOAD_COPROC_DELAY
2794 | INSN_COPROC_MOVE_DELAY
2795 | INSN_WRITE_COND_CODE))
2796 && ! cop_interlocks))
252b5132
RH
2797 || (! hilo_interlocks
2798 && (prev_insn.insn_mo->pinfo
2799 & (INSN_READ_LO
2800 | INSN_READ_HI)))
2801 || (! mips_opts.mips16
81912461
ILT
2802 && (prev_insn.insn_mo->pinfo & INSN_LOAD_MEMORY_DELAY)
2803 && ! gpr_interlocks)
252b5132 2804 || (! mips_opts.mips16
81912461
ILT
2805 && (prev_insn.insn_mo->pinfo & INSN_COPROC_MEMORY_DELAY)
2806 && ! cop_mem_interlocks))
252b5132 2807 {
beae10d5 2808 /* Itbl support may require additional care here. */
252b5132
RH
2809 ++nops;
2810 if ((! mips_opts.mips16
81912461
ILT
2811 && ((prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
2812 && ! cop_interlocks))
252b5132
RH
2813 || (! hilo_interlocks
2814 && ((prev_insn.insn_mo->pinfo & INSN_READ_HI)
2815 || (prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2816 ++nops;
2817
2818 if (prev_insn_unreordered)
2819 nops = 0;
2820 }
2821 else if ((! mips_opts.mips16
81912461
ILT
2822 && ((prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
2823 && ! cop_interlocks))
252b5132
RH
2824 || (! hilo_interlocks
2825 && ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
2826 || (prev_prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2827 {
beae10d5 2828 /* Itbl support may require additional care here. */
252b5132
RH
2829 if (! prev_prev_insn_unreordered)
2830 ++nops;
2831 }
2832
d766e8ec 2833 if (mips_fix_vr4120 && prev_insn.insn_mo->name)
60b63b72
RS
2834 {
2835 int min_nops = 0;
2836 const char *pn = prev_insn.insn_mo->name;
2837 if (strncmp(pn, "macc", 4) == 0
2838 || strncmp(pn, "dmacc", 5) == 0
2839 || strncmp(pn, "dmult", 5) == 0)
2840 {
2841 min_nops = 1;
2842 }
2843 if (nops < min_nops)
2844 nops = min_nops;
2845 }
2846
252b5132
RH
2847 if (nops > 0)
2848 {
2849 struct insn_label_list *l;
2850
2851 if (insns)
2852 {
2853 /* Record the frag which holds the nop instructions, so
2854 that we can remove them if we don't need them. */
2855 frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2856 prev_nop_frag = frag_now;
2857 prev_nop_frag_holds = nops;
2858 prev_nop_frag_required = 0;
2859 prev_nop_frag_since = 0;
2860 }
2861
2862 for (; nops > 0; --nops)
2863 emit_nop ();
2864
2865 if (insns)
2866 {
2867 /* Move on to a new frag, so that it is safe to simply
bdaaa2e1 2868 decrease the size of prev_nop_frag. */
252b5132
RH
2869 frag_wane (frag_now);
2870 frag_new (0);
2871 }
2872
2873 for (l = insn_labels; l != NULL; l = l->next)
2874 {
98aa84af
AM
2875 valueT val;
2876
252b5132 2877 assert (S_GET_SEGMENT (l->label) == now_seg);
49309057 2878 symbol_set_frag (l->label, frag_now);
98aa84af 2879 val = (valueT) frag_now_fix ();
252b5132
RH
2880 /* mips16 text labels are stored as odd. */
2881 if (mips_opts.mips16)
f9419b05 2882 ++val;
98aa84af 2883 S_SET_VALUE (l->label, val);
252b5132
RH
2884 }
2885 }
2886 }
2887
2888 /* Mark instruction labels in mips16 mode. */
f9419b05 2889 if (insns)
252b5132
RH
2890 mips16_mark_labels ();
2891
2892 mips_no_prev_insn (insns);
2893}
2894
584892a6
RS
2895/* Set up global variables for the start of a new macro. */
2896
2897static void
2898macro_start (void)
2899{
2900 memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
2901 mips_macro_warning.delay_slot_p = (mips_opts.noreorder
2902 && (prev_insn.insn_mo->pinfo
2903 & (INSN_UNCOND_BRANCH_DELAY
2904 | INSN_COND_BRANCH_DELAY
2905 | INSN_COND_BRANCH_LIKELY)) != 0);
2906}
2907
2908/* Given that a macro is longer than 4 bytes, return the appropriate warning
2909 for it. Return null if no warning is needed. SUBTYPE is a bitmask of
2910 RELAX_DELAY_SLOT and RELAX_NOMACRO. */
2911
2912static const char *
2913macro_warning (relax_substateT subtype)
2914{
2915 if (subtype & RELAX_DELAY_SLOT)
2916 return _("Macro instruction expanded into multiple instructions"
2917 " in a branch delay slot");
2918 else if (subtype & RELAX_NOMACRO)
2919 return _("Macro instruction expanded into multiple instructions");
2920 else
2921 return 0;
2922}
2923
2924/* Finish up a macro. Emit warnings as appropriate. */
2925
2926static void
2927macro_end (void)
2928{
2929 if (mips_macro_warning.sizes[0] > 4 || mips_macro_warning.sizes[1] > 4)
2930 {
2931 relax_substateT subtype;
2932
2933 /* Set up the relaxation warning flags. */
2934 subtype = 0;
2935 if (mips_macro_warning.sizes[1] > mips_macro_warning.sizes[0])
2936 subtype |= RELAX_SECOND_LONGER;
2937 if (mips_opts.warn_about_macros)
2938 subtype |= RELAX_NOMACRO;
2939 if (mips_macro_warning.delay_slot_p)
2940 subtype |= RELAX_DELAY_SLOT;
2941
2942 if (mips_macro_warning.sizes[0] > 4 && mips_macro_warning.sizes[1] > 4)
2943 {
2944 /* Either the macro has a single implementation or both
2945 implementations are longer than 4 bytes. Emit the
2946 warning now. */
2947 const char *msg = macro_warning (subtype);
2948 if (msg != 0)
2949 as_warn (msg);
2950 }
2951 else
2952 {
2953 /* One implementation might need a warning but the other
2954 definitely doesn't. */
2955 mips_macro_warning.first_frag->fr_subtype |= subtype;
2956 }
2957 }
2958}
2959
252b5132
RH
2960/* Build an instruction created by a macro expansion. This is passed
2961 a pointer to the count of instructions created so far, an
2962 expression, the name of the instruction to build, an operand format
2963 string, and corresponding arguments. */
2964
252b5132 2965static void
67c0d1eb 2966macro_build (expressionS *ep, const char *name, const char *fmt, ...)
252b5132
RH
2967{
2968 struct mips_cl_insn insn;
f6688943 2969 bfd_reloc_code_real_type r[3];
252b5132 2970 va_list args;
252b5132 2971
252b5132 2972 va_start (args, fmt);
252b5132 2973
252b5132
RH
2974 if (mips_opts.mips16)
2975 {
67c0d1eb 2976 mips16_macro_build (ep, name, fmt, args);
252b5132
RH
2977 va_end (args);
2978 return;
2979 }
2980
f6688943
TS
2981 r[0] = BFD_RELOC_UNUSED;
2982 r[1] = BFD_RELOC_UNUSED;
2983 r[2] = BFD_RELOC_UNUSED;
252b5132
RH
2984 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
2985 assert (insn.insn_mo);
2986 assert (strcmp (name, insn.insn_mo->name) == 0);
2987
2988 /* Search until we get a match for NAME. */
2989 while (1)
2990 {
b34976b6 2991 /* It is assumed here that macros will never generate
deec1734 2992 MDMX or MIPS-3D instructions. */
252b5132
RH
2993 if (strcmp (fmt, insn.insn_mo->args) == 0
2994 && insn.insn_mo->pinfo != INSN_MACRO
aec421e0
TS
2995 && OPCODE_IS_MEMBER (insn.insn_mo,
2996 (mips_opts.isa
3396de36 2997 | (file_ase_mips16 ? INSN_MIPS16 : 0)),
fef14a42
TS
2998 mips_opts.arch)
2999 && (mips_opts.arch != CPU_R4650 || (insn.insn_mo->pinfo & FP_D) == 0))
252b5132
RH
3000 break;
3001
3002 ++insn.insn_mo;
3003 assert (insn.insn_mo->name);
3004 assert (strcmp (name, insn.insn_mo->name) == 0);
3005 }
3006
3007 insn.insn_opcode = insn.insn_mo->match;
3008 for (;;)
3009 {
3010 switch (*fmt++)
3011 {
3012 case '\0':
3013 break;
3014
3015 case ',':
3016 case '(':
3017 case ')':
3018 continue;
3019
5f74bc13
CD
3020 case '+':
3021 switch (*fmt++)
3022 {
3023 case 'A':
3024 case 'E':
3025 insn.insn_opcode |= (va_arg (args, int)
3026 & OP_MASK_SHAMT) << OP_SH_SHAMT;
3027 continue;
3028
3029 case 'B':
3030 case 'F':
3031 /* Note that in the macro case, these arguments are already
3032 in MSB form. (When handling the instruction in the
3033 non-macro case, these arguments are sizes from which
3034 MSB values must be calculated.) */
3035 insn.insn_opcode |= (va_arg (args, int)
3036 & OP_MASK_INSMSB) << OP_SH_INSMSB;
3037 continue;
3038
3039 case 'C':
3040 case 'G':
3041 case 'H':
3042 /* Note that in the macro case, these arguments are already
3043 in MSBD form. (When handling the instruction in the
3044 non-macro case, these arguments are sizes from which
3045 MSBD values must be calculated.) */
3046 insn.insn_opcode |= (va_arg (args, int)
3047 & OP_MASK_EXTMSBD) << OP_SH_EXTMSBD;
3048 continue;
3049
3050 default:
3051 internalError ();
3052 }
3053 continue;
3054
252b5132
RH
3055 case 't':
3056 case 'w':
3057 case 'E':
38487616 3058 insn.insn_opcode |= va_arg (args, int) << OP_SH_RT;
252b5132
RH
3059 continue;
3060
3061 case 'c':
38487616
TS
3062 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE;
3063 continue;
3064
252b5132
RH
3065 case 'T':
3066 case 'W':
38487616 3067 insn.insn_opcode |= va_arg (args, int) << OP_SH_FT;
252b5132
RH
3068 continue;
3069
3070 case 'd':
3071 case 'G':
af7ee8bf 3072 case 'K':
38487616 3073 insn.insn_opcode |= va_arg (args, int) << OP_SH_RD;
252b5132
RH
3074 continue;
3075
4372b673
NC
3076 case 'U':
3077 {
3078 int tmp = va_arg (args, int);
3079
38487616
TS
3080 insn.insn_opcode |= tmp << OP_SH_RT;
3081 insn.insn_opcode |= tmp << OP_SH_RD;
beae10d5 3082 continue;
4372b673
NC
3083 }
3084
252b5132
RH
3085 case 'V':
3086 case 'S':
38487616 3087 insn.insn_opcode |= va_arg (args, int) << OP_SH_FS;
252b5132
RH
3088 continue;
3089
3090 case 'z':
3091 continue;
3092
3093 case '<':
38487616 3094 insn.insn_opcode |= va_arg (args, int) << OP_SH_SHAMT;
252b5132
RH
3095 continue;
3096
3097 case 'D':
38487616 3098 insn.insn_opcode |= va_arg (args, int) << OP_SH_FD;
252b5132
RH
3099 continue;
3100
3101 case 'B':
38487616 3102 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE20;
252b5132
RH
3103 continue;
3104
4372b673 3105 case 'J':
38487616 3106 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE19;
4372b673
NC
3107 continue;
3108
252b5132 3109 case 'q':
38487616 3110 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE2;
252b5132
RH
3111 continue;
3112
3113 case 'b':
3114 case 's':
3115 case 'r':
3116 case 'v':
38487616 3117 insn.insn_opcode |= va_arg (args, int) << OP_SH_RS;
252b5132
RH
3118 continue;
3119
3120 case 'i':
3121 case 'j':
3122 case 'o':
f6688943 3123 *r = (bfd_reloc_code_real_type) va_arg (args, int);
cdf6fd85 3124 assert (*r == BFD_RELOC_GPREL16
f6688943
TS
3125 || *r == BFD_RELOC_MIPS_LITERAL
3126 || *r == BFD_RELOC_MIPS_HIGHER
3127 || *r == BFD_RELOC_HI16_S
3128 || *r == BFD_RELOC_LO16
3129 || *r == BFD_RELOC_MIPS_GOT16
3130 || *r == BFD_RELOC_MIPS_CALL16
438c16b8
TS
3131 || *r == BFD_RELOC_MIPS_GOT_DISP
3132 || *r == BFD_RELOC_MIPS_GOT_PAGE
3133 || *r == BFD_RELOC_MIPS_GOT_OFST
f6688943
TS
3134 || *r == BFD_RELOC_MIPS_GOT_LO16
3135 || *r == BFD_RELOC_MIPS_CALL_LO16
252b5132 3136 || (ep->X_op == O_subtract
f6688943 3137 && *r == BFD_RELOC_PCREL_LO16));
252b5132
RH
3138 continue;
3139
3140 case 'u':
f6688943 3141 *r = (bfd_reloc_code_real_type) va_arg (args, int);
252b5132
RH
3142 assert (ep != NULL
3143 && (ep->X_op == O_constant
3144 || (ep->X_op == O_symbol
f6688943
TS
3145 && (*r == BFD_RELOC_MIPS_HIGHEST
3146 || *r == BFD_RELOC_HI16_S
3147 || *r == BFD_RELOC_HI16
3148 || *r == BFD_RELOC_GPREL16
3149 || *r == BFD_RELOC_MIPS_GOT_HI16
3150 || *r == BFD_RELOC_MIPS_CALL_HI16))
252b5132 3151 || (ep->X_op == O_subtract
f6688943 3152 && *r == BFD_RELOC_PCREL_HI16_S)));
252b5132
RH
3153 continue;
3154
3155 case 'p':
3156 assert (ep != NULL);
3157 /*
3158 * This allows macro() to pass an immediate expression for
3159 * creating short branches without creating a symbol.
0b25d3e6
AO
3160 * Note that the expression still might come from the assembly
3161 * input, in which case the value is not checked for range nor
3162 * is a relocation entry generated (yuck).
252b5132
RH
3163 */
3164 if (ep->X_op == O_constant)
3165 {
3166 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
3167 ep = NULL;
3168 }
3169 else
0b25d3e6 3170 *r = BFD_RELOC_16_PCREL_S2;
252b5132
RH
3171 continue;
3172
3173 case 'a':
3174 assert (ep != NULL);
f6688943 3175 *r = BFD_RELOC_MIPS_JMP;
252b5132
RH
3176 continue;
3177
3178 case 'C':
3179 insn.insn_opcode |= va_arg (args, unsigned long);
3180 continue;
3181
3182 default:
3183 internalError ();
3184 }
3185 break;
3186 }
3187 va_end (args);
f6688943 3188 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 3189
4d7206a2 3190 append_insn (&insn, ep, r);
252b5132
RH
3191}
3192
3193static void
67c0d1eb 3194mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
17a2f251 3195 va_list args)
252b5132
RH
3196{
3197 struct mips_cl_insn insn;
f6688943
TS
3198 bfd_reloc_code_real_type r[3]
3199 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 3200
252b5132
RH
3201 insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
3202 assert (insn.insn_mo);
3203 assert (strcmp (name, insn.insn_mo->name) == 0);
3204
3205 while (strcmp (fmt, insn.insn_mo->args) != 0
3206 || insn.insn_mo->pinfo == INSN_MACRO)
3207 {
3208 ++insn.insn_mo;
3209 assert (insn.insn_mo->name);
3210 assert (strcmp (name, insn.insn_mo->name) == 0);
3211 }
3212
3213 insn.insn_opcode = insn.insn_mo->match;
b34976b6 3214 insn.use_extend = FALSE;
252b5132
RH
3215
3216 for (;;)
3217 {
3218 int c;
3219
3220 c = *fmt++;
3221 switch (c)
3222 {
3223 case '\0':
3224 break;
3225
3226 case ',':
3227 case '(':
3228 case ')':
3229 continue;
3230
3231 case 'y':
3232 case 'w':
3233 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
3234 continue;
3235
3236 case 'x':
3237 case 'v':
3238 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
3239 continue;
3240
3241 case 'z':
3242 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
3243 continue;
3244
3245 case 'Z':
3246 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
3247 continue;
3248
3249 case '0':
3250 case 'S':
3251 case 'P':
3252 case 'R':
3253 continue;
3254
3255 case 'X':
3256 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
3257 continue;
3258
3259 case 'Y':
3260 {
3261 int regno;
3262
3263 regno = va_arg (args, int);
3264 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
3265 insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
3266 }
3267 continue;
3268
3269 case '<':
3270 case '>':
3271 case '4':
3272 case '5':
3273 case 'H':
3274 case 'W':
3275 case 'D':
3276 case 'j':
3277 case '8':
3278 case 'V':
3279 case 'C':
3280 case 'U':
3281 case 'k':
3282 case 'K':
3283 case 'p':
3284 case 'q':
3285 {
3286 assert (ep != NULL);
3287
3288 if (ep->X_op != O_constant)
874e8986 3289 *r = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
3290 else
3291 {
b34976b6
AM
3292 mips16_immed (NULL, 0, c, ep->X_add_number, FALSE, FALSE,
3293 FALSE, &insn.insn_opcode, &insn.use_extend,
c4e7957c 3294 &insn.extend);
252b5132 3295 ep = NULL;
f6688943 3296 *r = BFD_RELOC_UNUSED;
252b5132
RH
3297 }
3298 }
3299 continue;
3300
3301 case '6':
3302 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
3303 continue;
3304 }
3305
3306 break;
3307 }
3308
f6688943 3309 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 3310
4d7206a2 3311 append_insn (&insn, ep, r);
252b5132
RH
3312}
3313
438c16b8
TS
3314/*
3315 * Generate a "jalr" instruction with a relocation hint to the called
3316 * function. This occurs in NewABI PIC code.
3317 */
3318static void
67c0d1eb 3319macro_build_jalr (expressionS *ep)
438c16b8 3320{
685736be 3321 char *f = NULL;
b34976b6 3322
438c16b8 3323 if (HAVE_NEWABI)
f21f8242 3324 {
cc3d92a5 3325 frag_grow (8);
f21f8242
AO
3326 f = frag_more (0);
3327 }
67c0d1eb 3328 macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
438c16b8 3329 if (HAVE_NEWABI)
f21f8242 3330 fix_new_exp (frag_now, f - frag_now->fr_literal,
a105a300 3331 4, ep, FALSE, BFD_RELOC_MIPS_JALR);
438c16b8
TS
3332}
3333
252b5132
RH
3334/*
3335 * Generate a "lui" instruction.
3336 */
3337static void
67c0d1eb 3338macro_build_lui (expressionS *ep, int regnum)
252b5132
RH
3339{
3340 expressionS high_expr;
3341 struct mips_cl_insn insn;
f6688943
TS
3342 bfd_reloc_code_real_type r[3]
3343 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
5a38dc70
AM
3344 const char *name = "lui";
3345 const char *fmt = "t,u";
252b5132
RH
3346
3347 assert (! mips_opts.mips16);
3348
4d7206a2 3349 high_expr = *ep;
252b5132
RH
3350
3351 if (high_expr.X_op == O_constant)
3352 {
3353 /* we can compute the instruction now without a relocation entry */
e7d556df
TS
3354 high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3355 >> 16) & 0xffff;
f6688943 3356 *r = BFD_RELOC_UNUSED;
252b5132 3357 }
78e1bb40 3358 else
252b5132
RH
3359 {
3360 assert (ep->X_op == O_symbol);
3361 /* _gp_disp is a special case, used from s_cpload. */
3362 assert (mips_pic == NO_PIC
78e1bb40
AO
3363 || (! HAVE_NEWABI
3364 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0));
f6688943 3365 *r = BFD_RELOC_HI16_S;
252b5132
RH
3366 }
3367
252b5132
RH
3368 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3369 assert (insn.insn_mo);
3370 assert (strcmp (name, insn.insn_mo->name) == 0);
3371 assert (strcmp (fmt, insn.insn_mo->args) == 0);
3372
3373 insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
f6688943 3374 if (*r == BFD_RELOC_UNUSED)
252b5132
RH
3375 {
3376 insn.insn_opcode |= high_expr.X_add_number;
4d7206a2 3377 append_insn (&insn, NULL, r);
252b5132
RH
3378 }
3379 else
4d7206a2 3380 append_insn (&insn, &high_expr, r);
252b5132
RH
3381}
3382
885add95
CD
3383/* Generate a sequence of instructions to do a load or store from a constant
3384 offset off of a base register (breg) into/from a target register (treg),
3385 using AT if necessary. */
3386static void
67c0d1eb
RS
3387macro_build_ldst_constoffset (expressionS *ep, const char *op,
3388 int treg, int breg, int dbl)
885add95
CD
3389{
3390 assert (ep->X_op == O_constant);
3391
256ab948 3392 /* Sign-extending 32-bit constants makes their handling easier. */
d17e7bce
TS
3393 if (! dbl && ! ((ep->X_add_number & ~((bfd_vma) 0x7fffffff))
3394 == ~((bfd_vma) 0x7fffffff)))
ae826530 3395 {
d17e7bce 3396 if (ep->X_add_number & ~((bfd_vma) 0xffffffff))
1b8e29e5 3397 as_bad (_("constant too large"));
ae826530 3398
1b8e29e5
TS
3399 ep->X_add_number = (((ep->X_add_number & 0xffffffff) ^ 0x80000000)
3400 - 0x80000000);
ae826530 3401 }
256ab948 3402
67c1ffbe 3403 /* Right now, this routine can only handle signed 32-bit constants. */
ecd13cd3 3404 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
885add95
CD
3405 as_warn (_("operand overflow"));
3406
3407 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
3408 {
3409 /* Signed 16-bit offset will fit in the op. Easy! */
67c0d1eb 3410 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
885add95
CD
3411 }
3412 else
3413 {
3414 /* 32-bit offset, need multiple instructions and AT, like:
3415 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
3416 addu $tempreg,$tempreg,$breg
3417 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
3418 to handle the complete offset. */
67c0d1eb
RS
3419 macro_build_lui (ep, AT);
3420 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
3421 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
885add95
CD
3422
3423 if (mips_opts.noat)
3424 as_warn (_("Macro used $at after \".set noat\""));
3425 }
3426}
3427
252b5132
RH
3428/* set_at()
3429 * Generates code to set the $at register to true (one)
3430 * if reg is less than the immediate expression.
3431 */
3432static void
67c0d1eb 3433set_at (int reg, int unsignedp)
252b5132
RH
3434{
3435 if (imm_expr.X_op == O_constant
3436 && imm_expr.X_add_number >= -0x8000
3437 && imm_expr.X_add_number < 0x8000)
67c0d1eb
RS
3438 macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
3439 AT, reg, BFD_RELOC_LO16);
252b5132
RH
3440 else
3441 {
67c0d1eb
RS
3442 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
3443 macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
252b5132
RH
3444 }
3445}
3446
13757d0c
TS
3447static void
3448normalize_constant_expr (expressionS *ex)
3449{
3450 if (ex->X_op == O_constant && HAVE_32BIT_GPRS)
3451 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
3452 - 0x80000000);
3453}
3454
252b5132
RH
3455/* Warn if an expression is not a constant. */
3456
3457static void
17a2f251 3458check_absolute_expr (struct mips_cl_insn *ip, expressionS *ex)
252b5132
RH
3459{
3460 if (ex->X_op == O_big)
3461 as_bad (_("unsupported large constant"));
3462 else if (ex->X_op != O_constant)
3463 as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
13757d0c
TS
3464
3465 normalize_constant_expr (ex);
252b5132
RH
3466}
3467
3468/* Count the leading zeroes by performing a binary chop. This is a
3469 bulky bit of source, but performance is a LOT better for the
3470 majority of values than a simple loop to count the bits:
3471 for (lcnt = 0; (lcnt < 32); lcnt++)
3472 if ((v) & (1 << (31 - lcnt)))
3473 break;
3474 However it is not code size friendly, and the gain will drop a bit
3475 on certain cached systems.
3476*/
3477#define COUNT_TOP_ZEROES(v) \
3478 (((v) & ~0xffff) == 0 \
3479 ? ((v) & ~0xff) == 0 \
3480 ? ((v) & ~0xf) == 0 \
3481 ? ((v) & ~0x3) == 0 \
3482 ? ((v) & ~0x1) == 0 \
3483 ? !(v) \
3484 ? 32 \
3485 : 31 \
3486 : 30 \
3487 : ((v) & ~0x7) == 0 \
3488 ? 29 \
3489 : 28 \
3490 : ((v) & ~0x3f) == 0 \
3491 ? ((v) & ~0x1f) == 0 \
3492 ? 27 \
3493 : 26 \
3494 : ((v) & ~0x7f) == 0 \
3495 ? 25 \
3496 : 24 \
3497 : ((v) & ~0xfff) == 0 \
3498 ? ((v) & ~0x3ff) == 0 \
3499 ? ((v) & ~0x1ff) == 0 \
3500 ? 23 \
3501 : 22 \
3502 : ((v) & ~0x7ff) == 0 \
3503 ? 21 \
3504 : 20 \
3505 : ((v) & ~0x3fff) == 0 \
3506 ? ((v) & ~0x1fff) == 0 \
3507 ? 19 \
3508 : 18 \
3509 : ((v) & ~0x7fff) == 0 \
3510 ? 17 \
3511 : 16 \
3512 : ((v) & ~0xffffff) == 0 \
3513 ? ((v) & ~0xfffff) == 0 \
3514 ? ((v) & ~0x3ffff) == 0 \
3515 ? ((v) & ~0x1ffff) == 0 \
3516 ? 15 \
3517 : 14 \
3518 : ((v) & ~0x7ffff) == 0 \
3519 ? 13 \
3520 : 12 \
3521 : ((v) & ~0x3fffff) == 0 \
3522 ? ((v) & ~0x1fffff) == 0 \
3523 ? 11 \
3524 : 10 \
3525 : ((v) & ~0x7fffff) == 0 \
3526 ? 9 \
3527 : 8 \
3528 : ((v) & ~0xfffffff) == 0 \
3529 ? ((v) & ~0x3ffffff) == 0 \
3530 ? ((v) & ~0x1ffffff) == 0 \
3531 ? 7 \
3532 : 6 \
3533 : ((v) & ~0x7ffffff) == 0 \
3534 ? 5 \
3535 : 4 \
3536 : ((v) & ~0x3fffffff) == 0 \
3537 ? ((v) & ~0x1fffffff) == 0 \
3538 ? 3 \
3539 : 2 \
3540 : ((v) & ~0x7fffffff) == 0 \
3541 ? 1 \
3542 : 0)
3543
3544/* load_register()
67c1ffbe 3545 * This routine generates the least number of instructions necessary to load
252b5132
RH
3546 * an absolute expression value into a register.
3547 */
3548static void
67c0d1eb 3549load_register (int reg, expressionS *ep, int dbl)
252b5132
RH
3550{
3551 int freg;
3552 expressionS hi32, lo32;
3553
3554 if (ep->X_op != O_big)
3555 {
3556 assert (ep->X_op == O_constant);
256ab948
TS
3557
3558 /* Sign-extending 32-bit constants makes their handling easier. */
d17e7bce
TS
3559 if (! dbl && ! ((ep->X_add_number & ~((bfd_vma) 0x7fffffff))
3560 == ~((bfd_vma) 0x7fffffff)))
ae826530 3561 {
d17e7bce 3562 if (ep->X_add_number & ~((bfd_vma) 0xffffffff))
1b8e29e5 3563 as_bad (_("constant too large"));
ae826530 3564
1b8e29e5
TS
3565 ep->X_add_number = (((ep->X_add_number & 0xffffffff) ^ 0x80000000)
3566 - 0x80000000);
ae826530 3567 }
256ab948
TS
3568
3569 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
252b5132
RH
3570 {
3571 /* We can handle 16 bit signed values with an addiu to
3572 $zero. No need to ever use daddiu here, since $zero and
3573 the result are always correct in 32 bit mode. */
67c0d1eb 3574 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
3575 return;
3576 }
3577 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3578 {
3579 /* We can handle 16 bit unsigned values with an ori to
3580 $zero. */
67c0d1eb 3581 macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
252b5132
RH
3582 return;
3583 }
256ab948 3584 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
252b5132
RH
3585 {
3586 /* 32 bit values require an lui. */
67c0d1eb 3587 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_HI16);
252b5132 3588 if ((ep->X_add_number & 0xffff) != 0)
67c0d1eb 3589 macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
252b5132
RH
3590 return;
3591 }
3592 }
3593
3594 /* The value is larger than 32 bits. */
3595
ca4e0257 3596 if (HAVE_32BIT_GPRS)
252b5132 3597 {
956cd1d6
TS
3598 as_bad (_("Number (0x%lx) larger than 32 bits"),
3599 (unsigned long) ep->X_add_number);
67c0d1eb 3600 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
3601 return;
3602 }
3603
3604 if (ep->X_op != O_big)
3605 {
3606 hi32 = *ep;
3607 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3608 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3609 hi32.X_add_number &= 0xffffffff;
3610 lo32 = *ep;
3611 lo32.X_add_number &= 0xffffffff;
3612 }
3613 else
3614 {
3615 assert (ep->X_add_number > 2);
3616 if (ep->X_add_number == 3)
3617 generic_bignum[3] = 0;
3618 else if (ep->X_add_number > 4)
3619 as_bad (_("Number larger than 64 bits"));
3620 lo32.X_op = O_constant;
3621 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3622 hi32.X_op = O_constant;
3623 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3624 }
3625
3626 if (hi32.X_add_number == 0)
3627 freg = 0;
3628 else
3629 {
3630 int shift, bit;
3631 unsigned long hi, lo;
3632
956cd1d6 3633 if (hi32.X_add_number == (offsetT) 0xffffffff)
beae10d5
KH
3634 {
3635 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3636 {
67c0d1eb 3637 macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
3638 return;
3639 }
3640 if (lo32.X_add_number & 0x80000000)
3641 {
67c0d1eb 3642 macro_build (&lo32, "lui", "t,u", reg, BFD_RELOC_HI16);
252b5132 3643 if (lo32.X_add_number & 0xffff)
67c0d1eb 3644 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
beae10d5
KH
3645 return;
3646 }
3647 }
252b5132
RH
3648
3649 /* Check for 16bit shifted constant. We know that hi32 is
3650 non-zero, so start the mask on the first bit of the hi32
3651 value. */
3652 shift = 17;
3653 do
beae10d5
KH
3654 {
3655 unsigned long himask, lomask;
3656
3657 if (shift < 32)
3658 {
3659 himask = 0xffff >> (32 - shift);
3660 lomask = (0xffff << shift) & 0xffffffff;
3661 }
3662 else
3663 {
3664 himask = 0xffff << (shift - 32);
3665 lomask = 0;
3666 }
3667 if ((hi32.X_add_number & ~(offsetT) himask) == 0
3668 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3669 {
3670 expressionS tmp;
3671
3672 tmp.X_op = O_constant;
3673 if (shift < 32)
3674 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3675 | (lo32.X_add_number >> shift));
3676 else
3677 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
67c0d1eb
RS
3678 macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
3679 macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", "d,w,<",
3680 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
3681 return;
3682 }
f9419b05 3683 ++shift;
beae10d5
KH
3684 }
3685 while (shift <= (64 - 16));
252b5132
RH
3686
3687 /* Find the bit number of the lowest one bit, and store the
3688 shifted value in hi/lo. */
3689 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3690 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3691 if (lo != 0)
3692 {
3693 bit = 0;
3694 while ((lo & 1) == 0)
3695 {
3696 lo >>= 1;
3697 ++bit;
3698 }
3699 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3700 hi >>= bit;
3701 }
3702 else
3703 {
3704 bit = 32;
3705 while ((hi & 1) == 0)
3706 {
3707 hi >>= 1;
3708 ++bit;
3709 }
3710 lo = hi;
3711 hi = 0;
3712 }
3713
3714 /* Optimize if the shifted value is a (power of 2) - 1. */
3715 if ((hi == 0 && ((lo + 1) & lo) == 0)
3716 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
beae10d5
KH
3717 {
3718 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
252b5132 3719 if (shift != 0)
beae10d5 3720 {
252b5132
RH
3721 expressionS tmp;
3722
3723 /* This instruction will set the register to be all
3724 ones. */
beae10d5
KH
3725 tmp.X_op = O_constant;
3726 tmp.X_add_number = (offsetT) -1;
67c0d1eb 3727 macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
3728 if (bit != 0)
3729 {
3730 bit += shift;
67c0d1eb
RS
3731 macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", "d,w,<",
3732 reg, reg, (bit >= 32) ? bit - 32 : bit);
beae10d5 3733 }
67c0d1eb
RS
3734 macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", "d,w,<",
3735 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
3736 return;
3737 }
3738 }
252b5132
RH
3739
3740 /* Sign extend hi32 before calling load_register, because we can
3741 generally get better code when we load a sign extended value. */
3742 if ((hi32.X_add_number & 0x80000000) != 0)
beae10d5 3743 hi32.X_add_number |= ~(offsetT) 0xffffffff;
67c0d1eb 3744 load_register (reg, &hi32, 0);
252b5132
RH
3745 freg = reg;
3746 }
3747 if ((lo32.X_add_number & 0xffff0000) == 0)
3748 {
3749 if (freg != 0)
3750 {
67c0d1eb 3751 macro_build (NULL, "dsll32", "d,w,<", reg, freg, 0);
252b5132
RH
3752 freg = reg;
3753 }
3754 }
3755 else
3756 {
3757 expressionS mid16;
3758
956cd1d6 3759 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
beae10d5 3760 {
67c0d1eb
RS
3761 macro_build (&lo32, "lui", "t,u", reg, BFD_RELOC_HI16);
3762 macro_build (NULL, "dsrl32", "d,w,<", reg, reg, 0);
beae10d5
KH
3763 return;
3764 }
252b5132
RH
3765
3766 if (freg != 0)
3767 {
67c0d1eb 3768 macro_build (NULL, "dsll", "d,w,<", reg, freg, 16);
252b5132
RH
3769 freg = reg;
3770 }
3771 mid16 = lo32;
3772 mid16.X_add_number >>= 16;
67c0d1eb
RS
3773 macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
3774 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
252b5132
RH
3775 freg = reg;
3776 }
3777 if ((lo32.X_add_number & 0xffff) != 0)
67c0d1eb 3778 macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
252b5132
RH
3779}
3780
269137b2
TS
3781static inline void
3782load_delay_nop (void)
3783{
3784 if (!gpr_interlocks)
3785 macro_build (NULL, "nop", "");
3786}
3787
252b5132
RH
3788/* Load an address into a register. */
3789
3790static void
67c0d1eb 3791load_address (int reg, expressionS *ep, int *used_at)
252b5132 3792{
252b5132
RH
3793 if (ep->X_op != O_constant
3794 && ep->X_op != O_symbol)
3795 {
3796 as_bad (_("expression too complex"));
3797 ep->X_op = O_constant;
3798 }
3799
3800 if (ep->X_op == O_constant)
3801 {
67c0d1eb 3802 load_register (reg, ep, HAVE_64BIT_ADDRESSES);
252b5132
RH
3803 return;
3804 }
3805
3806 if (mips_pic == NO_PIC)
3807 {
3808 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 3809 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
3810 Otherwise we want
3811 lui $reg,<sym> (BFD_RELOC_HI16_S)
3812 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
d6bc6245 3813 If we have an addend, we always use the latter form.
76b3015f 3814
d6bc6245
TS
3815 With 64bit address space and a usable $at we want
3816 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3817 lui $at,<sym> (BFD_RELOC_HI16_S)
3818 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3819 daddiu $at,<sym> (BFD_RELOC_LO16)
3820 dsll32 $reg,0
3a482fd5 3821 daddu $reg,$reg,$at
76b3015f 3822
c03099e6 3823 If $at is already in use, we use a path which is suboptimal
d6bc6245
TS
3824 on superscalar processors.
3825 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3826 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3827 dsll $reg,16
3828 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
3829 dsll $reg,16
3830 daddiu $reg,<sym> (BFD_RELOC_LO16)
3831 */
c9914766 3832 if (HAVE_64BIT_ADDRESSES)
d6bc6245 3833 {
e864ceca
RS
3834 /* ??? We don't provide a GP-relative alternative for these macros.
3835 It used not to be possible with the original relaxation code,
3836 but it could be done now. */
d6bc6245 3837
460597ba 3838 if (*used_at == 0 && ! mips_opts.noat)
d6bc6245 3839 {
67c0d1eb
RS
3840 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_HIGHEST);
3841 macro_build (ep, "lui", "t,u", AT, BFD_RELOC_HI16_S);
3842 macro_build (ep, "daddiu", "t,r,j", reg, reg,
3843 BFD_RELOC_MIPS_HIGHER);
3844 macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
3845 macro_build (NULL, "dsll32", "d,w,<", reg, reg, 0);
3846 macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
d6bc6245
TS
3847 *used_at = 1;
3848 }
3849 else
3850 {
67c0d1eb
RS
3851 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_HIGHEST);
3852 macro_build (ep, "daddiu", "t,r,j", reg, reg,
3853 BFD_RELOC_MIPS_HIGHER);
3854 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
3855 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
3856 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
3857 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
d6bc6245
TS
3858 }
3859 }
252b5132
RH
3860 else
3861 {
d6bc6245
TS
3862 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3863 && ! nopic_need_relax (ep->X_add_symbol, 1))
3864 {
4d7206a2 3865 relax_start (ep->X_add_symbol);
67c0d1eb 3866 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
17a2f251 3867 mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 3868 relax_switch ();
d6bc6245 3869 }
67c0d1eb
RS
3870 macro_build_lui (ep, reg);
3871 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
3872 reg, reg, BFD_RELOC_LO16);
4d7206a2
RS
3873 if (mips_relax.sequence)
3874 relax_end ();
d6bc6245 3875 }
252b5132
RH
3876 }
3877 else if (mips_pic == SVR4_PIC && ! mips_big_got)
3878 {
3879 expressionS ex;
3880
3881 /* If this is a reference to an external symbol, we want
3882 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3883 Otherwise we want
3884 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3885 nop
3886 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
f5040a92
AO
3887 If there is a constant, it must be added in after.
3888
ed6fb7bd 3889 If we have NewABI, we want
f5040a92
AO
3890 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
3891 unless we're referencing a global symbol with a non-zero
3892 offset, in which case cst must be added separately. */
ed6fb7bd
SC
3893 if (HAVE_NEWABI)
3894 {
f5040a92
AO
3895 if (ep->X_add_number)
3896 {
4d7206a2 3897 ex.X_add_number = ep->X_add_number;
f5040a92 3898 ep->X_add_number = 0;
4d7206a2 3899 relax_start (ep->X_add_symbol);
67c0d1eb
RS
3900 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3901 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
3902 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3903 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3904 ex.X_op = O_constant;
67c0d1eb 3905 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 3906 reg, reg, BFD_RELOC_LO16);
f5040a92 3907 ep->X_add_number = ex.X_add_number;
4d7206a2 3908 relax_switch ();
f5040a92 3909 }
67c0d1eb 3910 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 3911 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2
RS
3912 if (mips_relax.sequence)
3913 relax_end ();
ed6fb7bd
SC
3914 }
3915 else
3916 {
f5040a92
AO
3917 ex.X_add_number = ep->X_add_number;
3918 ep->X_add_number = 0;
67c0d1eb
RS
3919 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3920 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 3921 load_delay_nop ();
4d7206a2
RS
3922 relax_start (ep->X_add_symbol);
3923 relax_switch ();
67c0d1eb 3924 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 3925 BFD_RELOC_LO16);
4d7206a2 3926 relax_end ();
ed6fb7bd 3927
f5040a92
AO
3928 if (ex.X_add_number != 0)
3929 {
3930 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3931 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3932 ex.X_op = O_constant;
67c0d1eb 3933 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 3934 reg, reg, BFD_RELOC_LO16);
f5040a92 3935 }
252b5132
RH
3936 }
3937 }
3938 else if (mips_pic == SVR4_PIC)
3939 {
3940 expressionS ex;
252b5132
RH
3941
3942 /* This is the large GOT case. If this is a reference to an
3943 external symbol, we want
3944 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
3945 addu $reg,$reg,$gp
3946 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
f5040a92
AO
3947
3948 Otherwise, for a reference to a local symbol in old ABI, we want
252b5132
RH
3949 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3950 nop
3951 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
684022ea 3952 If there is a constant, it must be added in after.
f5040a92
AO
3953
3954 In the NewABI, for local symbols, with or without offsets, we want:
438c16b8
TS
3955 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
3956 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
f5040a92 3957 */
438c16b8
TS
3958 if (HAVE_NEWABI)
3959 {
4d7206a2 3960 ex.X_add_number = ep->X_add_number;
f5040a92 3961 ep->X_add_number = 0;
4d7206a2 3962 relax_start (ep->X_add_symbol);
67c0d1eb
RS
3963 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_GOT_HI16);
3964 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
3965 reg, reg, mips_gp_register);
3966 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
3967 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
f5040a92
AO
3968 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3969 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3970 else if (ex.X_add_number)
3971 {
3972 ex.X_op = O_constant;
67c0d1eb
RS
3973 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
3974 BFD_RELOC_LO16);
f5040a92
AO
3975 }
3976
3977 ep->X_add_number = ex.X_add_number;
4d7206a2 3978 relax_switch ();
67c0d1eb 3979 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 3980 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
67c0d1eb
RS
3981 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
3982 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 3983 relax_end ();
438c16b8 3984 }
252b5132 3985 else
438c16b8 3986 {
f5040a92
AO
3987 ex.X_add_number = ep->X_add_number;
3988 ep->X_add_number = 0;
4d7206a2 3989 relax_start (ep->X_add_symbol);
67c0d1eb
RS
3990 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_GOT_HI16);
3991 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
3992 reg, reg, mips_gp_register);
3993 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
3994 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
4d7206a2
RS
3995 relax_switch ();
3996 if (reg_needs_delay (mips_gp_register))
438c16b8
TS
3997 {
3998 /* We need a nop before loading from $gp. This special
3999 check is required because the lui which starts the main
4000 instruction stream does not refer to $gp, and so will not
4001 insert the nop which may be required. */
67c0d1eb 4002 macro_build (NULL, "nop", "");
438c16b8 4003 }
67c0d1eb 4004 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 4005 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 4006 load_delay_nop ();
67c0d1eb 4007 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 4008 BFD_RELOC_LO16);
4d7206a2 4009 relax_end ();
438c16b8 4010
f5040a92
AO
4011 if (ex.X_add_number != 0)
4012 {
4013 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4014 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4015 ex.X_op = O_constant;
67c0d1eb
RS
4016 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
4017 BFD_RELOC_LO16);
f5040a92 4018 }
252b5132
RH
4019 }
4020 }
4021 else if (mips_pic == EMBEDDED_PIC)
4022 {
4023 /* We always do
cdf6fd85 4024 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
c9914766 4025 */
67c0d1eb
RS
4026 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
4027 reg, mips_gp_register, BFD_RELOC_GPREL16);
252b5132
RH
4028 }
4029 else
4030 abort ();
4031}
4032
ea1fb5dc
RS
4033/* Move the contents of register SOURCE into register DEST. */
4034
4035static void
67c0d1eb 4036move_register (int dest, int source)
ea1fb5dc 4037{
67c0d1eb
RS
4038 macro_build (NULL, HAVE_32BIT_GPRS ? "addu" : "daddu", "d,v,t",
4039 dest, source, 0);
ea1fb5dc
RS
4040}
4041
4d7206a2 4042/* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
f6a22291
MR
4043 LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
4044 The two alternatives are:
4d7206a2
RS
4045
4046 Global symbol Local sybmol
4047 ------------- ------------
4048 lw DEST,%got(SYMBOL) lw DEST,%got(SYMBOL + OFFSET)
4049 ... ...
4050 addiu DEST,DEST,OFFSET addiu DEST,DEST,%lo(SYMBOL + OFFSET)
4051
4052 load_got_offset emits the first instruction and add_got_offset
f6a22291
MR
4053 emits the second for a 16-bit offset or add_got_offset_hilo emits
4054 a sequence to add a 32-bit offset using a scratch register. */
4d7206a2
RS
4055
4056static void
67c0d1eb 4057load_got_offset (int dest, expressionS *local)
4d7206a2
RS
4058{
4059 expressionS global;
4060
4061 global = *local;
4062 global.X_add_number = 0;
4063
4064 relax_start (local->X_add_symbol);
67c0d1eb
RS
4065 macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
4066 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2 4067 relax_switch ();
67c0d1eb
RS
4068 macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
4069 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2
RS
4070 relax_end ();
4071}
4072
4073static void
67c0d1eb 4074add_got_offset (int dest, expressionS *local)
4d7206a2
RS
4075{
4076 expressionS global;
4077
4078 global.X_op = O_constant;
4079 global.X_op_symbol = NULL;
4080 global.X_add_symbol = NULL;
4081 global.X_add_number = local->X_add_number;
4082
4083 relax_start (local->X_add_symbol);
67c0d1eb 4084 macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
4d7206a2
RS
4085 dest, dest, BFD_RELOC_LO16);
4086 relax_switch ();
67c0d1eb 4087 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
4d7206a2
RS
4088 relax_end ();
4089}
4090
f6a22291
MR
4091static void
4092add_got_offset_hilo (int dest, expressionS *local, int tmp)
4093{
4094 expressionS global;
4095 int hold_mips_optimize;
4096
4097 global.X_op = O_constant;
4098 global.X_op_symbol = NULL;
4099 global.X_add_symbol = NULL;
4100 global.X_add_number = local->X_add_number;
4101
4102 relax_start (local->X_add_symbol);
4103 load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
4104 relax_switch ();
4105 /* Set mips_optimize around the lui instruction to avoid
4106 inserting an unnecessary nop after the lw. */
4107 hold_mips_optimize = mips_optimize;
4108 mips_optimize = 2;
4109 macro_build_lui (&global, tmp);
4110 mips_optimize = hold_mips_optimize;
4111 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
4112 relax_end ();
4113
4114 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
4115}
4116
252b5132
RH
4117/*
4118 * Build macros
4119 * This routine implements the seemingly endless macro or synthesized
4120 * instructions and addressing modes in the mips assembly language. Many
4121 * of these macros are simple and are similar to each other. These could
67c1ffbe 4122 * probably be handled by some kind of table or grammar approach instead of
252b5132
RH
4123 * this verbose method. Others are not simple macros but are more like
4124 * optimizing code generation.
4125 * One interesting optimization is when several store macros appear
67c1ffbe 4126 * consecutively that would load AT with the upper half of the same address.
252b5132
RH
4127 * The ensuing load upper instructions are ommited. This implies some kind
4128 * of global optimization. We currently only optimize within a single macro.
4129 * For many of the load and store macros if the address is specified as a
4130 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
4131 * first load register 'at' with zero and use it as the base register. The
4132 * mips assembler simply uses register $zero. Just one tiny optimization
4133 * we're missing.
4134 */
4135static void
17a2f251 4136macro (struct mips_cl_insn *ip)
252b5132
RH
4137{
4138 register int treg, sreg, dreg, breg;
4139 int tempreg;
4140 int mask;
43841e91 4141 int used_at = 0;
252b5132
RH
4142 expressionS expr1;
4143 const char *s;
4144 const char *s2;
4145 const char *fmt;
4146 int likely = 0;
4147 int dbl = 0;
4148 int coproc = 0;
4149 int lr = 0;
4150 int imm = 0;
1abe91b1 4151 int call = 0;
252b5132 4152 int off;
67c0d1eb 4153 offsetT maxnum;
252b5132 4154 bfd_reloc_code_real_type r;
252b5132
RH
4155 int hold_mips_optimize;
4156
4157 assert (! mips_opts.mips16);
4158
4159 treg = (ip->insn_opcode >> 16) & 0x1f;
4160 dreg = (ip->insn_opcode >> 11) & 0x1f;
4161 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
4162 mask = ip->insn_mo->mask;
4163
4164 expr1.X_op = O_constant;
4165 expr1.X_op_symbol = NULL;
4166 expr1.X_add_symbol = NULL;
4167 expr1.X_add_number = 1;
4168
4169 switch (mask)
4170 {
4171 case M_DABS:
4172 dbl = 1;
4173 case M_ABS:
4174 /* bgez $a0,.+12
4175 move v0,$a0
4176 sub v0,$zero,$a0
4177 */
4178
b34976b6 4179 mips_emit_delays (TRUE);
252b5132
RH
4180 ++mips_opts.noreorder;
4181 mips_any_noreorder = 1;
4182
4183 expr1.X_add_number = 8;
67c0d1eb 4184 macro_build (&expr1, "bgez", "s,p", sreg);
252b5132 4185 if (dreg == sreg)
67c0d1eb 4186 macro_build (NULL, "nop", "", 0);
252b5132 4187 else
67c0d1eb
RS
4188 move_register (dreg, sreg);
4189 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
252b5132
RH
4190
4191 --mips_opts.noreorder;
4192 return;
4193
4194 case M_ADD_I:
4195 s = "addi";
4196 s2 = "add";
4197 goto do_addi;
4198 case M_ADDU_I:
4199 s = "addiu";
4200 s2 = "addu";
4201 goto do_addi;
4202 case M_DADD_I:
4203 dbl = 1;
4204 s = "daddi";
4205 s2 = "dadd";
4206 goto do_addi;
4207 case M_DADDU_I:
4208 dbl = 1;
4209 s = "daddiu";
4210 s2 = "daddu";
4211 do_addi:
4212 if (imm_expr.X_op == O_constant
4213 && imm_expr.X_add_number >= -0x8000
4214 && imm_expr.X_add_number < 0x8000)
4215 {
67c0d1eb 4216 macro_build (&imm_expr, s, "t,r,j", treg, sreg, BFD_RELOC_LO16);
252b5132
RH
4217 return;
4218 }
67c0d1eb
RS
4219 load_register (AT, &imm_expr, dbl);
4220 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
252b5132
RH
4221 break;
4222
4223 case M_AND_I:
4224 s = "andi";
4225 s2 = "and";
4226 goto do_bit;
4227 case M_OR_I:
4228 s = "ori";
4229 s2 = "or";
4230 goto do_bit;
4231 case M_NOR_I:
4232 s = "";
4233 s2 = "nor";
4234 goto do_bit;
4235 case M_XOR_I:
4236 s = "xori";
4237 s2 = "xor";
4238 do_bit:
4239 if (imm_expr.X_op == O_constant
4240 && imm_expr.X_add_number >= 0
4241 && imm_expr.X_add_number < 0x10000)
4242 {
4243 if (mask != M_NOR_I)
67c0d1eb 4244 macro_build (&imm_expr, s, "t,r,i", treg, sreg, BFD_RELOC_LO16);
252b5132
RH
4245 else
4246 {
67c0d1eb
RS
4247 macro_build (&imm_expr, "ori", "t,r,i",
4248 treg, sreg, BFD_RELOC_LO16);
4249 macro_build (NULL, "nor", "d,v,t", treg, treg, 0);
252b5132
RH
4250 }
4251 return;
4252 }
4253
67c0d1eb
RS
4254 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
4255 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
252b5132
RH
4256 break;
4257
4258 case M_BEQ_I:
4259 s = "beq";
4260 goto beq_i;
4261 case M_BEQL_I:
4262 s = "beql";
4263 likely = 1;
4264 goto beq_i;
4265 case M_BNE_I:
4266 s = "bne";
4267 goto beq_i;
4268 case M_BNEL_I:
4269 s = "bnel";
4270 likely = 1;
4271 beq_i:
4272 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4273 {
67c0d1eb 4274 macro_build (&offset_expr, s, "s,t,p", sreg, 0);
252b5132
RH
4275 return;
4276 }
67c0d1eb
RS
4277 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
4278 macro_build (&offset_expr, s, "s,t,p", sreg, AT);
252b5132
RH
4279 break;
4280
4281 case M_BGEL:
4282 likely = 1;
4283 case M_BGE:
4284 if (treg == 0)
4285 {
67c0d1eb 4286 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", sreg);
252b5132
RH
4287 return;
4288 }
4289 if (sreg == 0)
4290 {
67c0d1eb 4291 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", treg);
252b5132
RH
4292 return;
4293 }
67c0d1eb
RS
4294 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
4295 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4296 break;
4297
4298 case M_BGTL_I:
4299 likely = 1;
4300 case M_BGT_I:
4301 /* check for > max integer */
4302 maxnum = 0x7fffffff;
ca4e0257 4303 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
4304 {
4305 maxnum <<= 16;
4306 maxnum |= 0xffff;
4307 maxnum <<= 16;
4308 maxnum |= 0xffff;
4309 }
4310 if (imm_expr.X_op == O_constant
4311 && imm_expr.X_add_number >= maxnum
ca4e0257 4312 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
4313 {
4314 do_false:
4315 /* result is always false */
4316 if (! likely)
67c0d1eb 4317 macro_build (NULL, "nop", "", 0);
252b5132 4318 else
67c0d1eb 4319 macro_build (&offset_expr, "bnel", "s,t,p", 0, 0);
252b5132
RH
4320 return;
4321 }
4322 if (imm_expr.X_op != O_constant)
4323 as_bad (_("Unsupported large constant"));
f9419b05 4324 ++imm_expr.X_add_number;
252b5132
RH
4325 /* FALLTHROUGH */
4326 case M_BGE_I:
4327 case M_BGEL_I:
4328 if (mask == M_BGEL_I)
4329 likely = 1;
4330 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4331 {
67c0d1eb 4332 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", sreg);
252b5132
RH
4333 return;
4334 }
4335 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4336 {
67c0d1eb 4337 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", sreg);
252b5132
RH
4338 return;
4339 }
4340 maxnum = 0x7fffffff;
ca4e0257 4341 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
4342 {
4343 maxnum <<= 16;
4344 maxnum |= 0xffff;
4345 maxnum <<= 16;
4346 maxnum |= 0xffff;
4347 }
4348 maxnum = - maxnum - 1;
4349 if (imm_expr.X_op == O_constant
4350 && imm_expr.X_add_number <= maxnum
ca4e0257 4351 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
4352 {
4353 do_true:
4354 /* result is always true */
4355 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
67c0d1eb 4356 macro_build (&offset_expr, "b", "p");
252b5132
RH
4357 return;
4358 }
67c0d1eb
RS
4359 set_at (sreg, 0);
4360 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4361 break;
4362
4363 case M_BGEUL:
4364 likely = 1;
4365 case M_BGEU:
4366 if (treg == 0)
4367 goto do_true;
4368 if (sreg == 0)
4369 {
67c0d1eb 4370 macro_build (&offset_expr, likely ? "beql" : "beq",
17a2f251 4371 "s,t,p", 0, treg);
252b5132
RH
4372 return;
4373 }
67c0d1eb
RS
4374 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
4375 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4376 break;
4377
4378 case M_BGTUL_I:
4379 likely = 1;
4380 case M_BGTU_I:
4381 if (sreg == 0
ca4e0257 4382 || (HAVE_32BIT_GPRS
252b5132 4383 && imm_expr.X_op == O_constant
956cd1d6 4384 && imm_expr.X_add_number == (offsetT) 0xffffffff))
252b5132
RH
4385 goto do_false;
4386 if (imm_expr.X_op != O_constant)
4387 as_bad (_("Unsupported large constant"));
f9419b05 4388 ++imm_expr.X_add_number;
252b5132
RH
4389 /* FALLTHROUGH */
4390 case M_BGEU_I:
4391 case M_BGEUL_I:
4392 if (mask == M_BGEUL_I)
4393 likely = 1;
4394 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4395 goto do_true;
4396 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4397 {
67c0d1eb 4398 macro_build (&offset_expr, likely ? "bnel" : "bne",
17a2f251 4399 "s,t,p", sreg, 0);
252b5132
RH
4400 return;
4401 }
67c0d1eb
RS
4402 set_at (sreg, 1);
4403 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4404 break;
4405
4406 case M_BGTL:
4407 likely = 1;
4408 case M_BGT:
4409 if (treg == 0)
4410 {
67c0d1eb 4411 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", sreg);
252b5132
RH
4412 return;
4413 }
4414 if (sreg == 0)
4415 {
67c0d1eb 4416 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", treg);
252b5132
RH
4417 return;
4418 }
67c0d1eb
RS
4419 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
4420 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4421 break;
4422
4423 case M_BGTUL:
4424 likely = 1;
4425 case M_BGTU:
4426 if (treg == 0)
4427 {
67c0d1eb 4428 macro_build (&offset_expr, likely ? "bnel" : "bne",
17a2f251 4429 "s,t,p", sreg, 0);
252b5132
RH
4430 return;
4431 }
4432 if (sreg == 0)
4433 goto do_false;
67c0d1eb
RS
4434 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
4435 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4436 break;
4437
4438 case M_BLEL:
4439 likely = 1;
4440 case M_BLE:
4441 if (treg == 0)
4442 {
67c0d1eb 4443 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", sreg);
252b5132
RH
4444 return;
4445 }
4446 if (sreg == 0)
4447 {
67c0d1eb 4448 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", treg);
252b5132
RH
4449 return;
4450 }
67c0d1eb
RS
4451 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
4452 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4453 break;
4454
4455 case M_BLEL_I:
4456 likely = 1;
4457 case M_BLE_I:
4458 maxnum = 0x7fffffff;
ca4e0257 4459 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
4460 {
4461 maxnum <<= 16;
4462 maxnum |= 0xffff;
4463 maxnum <<= 16;
4464 maxnum |= 0xffff;
4465 }
4466 if (imm_expr.X_op == O_constant
4467 && imm_expr.X_add_number >= maxnum
ca4e0257 4468 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
4469 goto do_true;
4470 if (imm_expr.X_op != O_constant)
4471 as_bad (_("Unsupported large constant"));
f9419b05 4472 ++imm_expr.X_add_number;
252b5132
RH
4473 /* FALLTHROUGH */
4474 case M_BLT_I:
4475 case M_BLTL_I:
4476 if (mask == M_BLTL_I)
4477 likely = 1;
4478 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4479 {
67c0d1eb 4480 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", sreg);
252b5132
RH
4481 return;
4482 }
4483 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4484 {
67c0d1eb 4485 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", sreg);
252b5132
RH
4486 return;
4487 }
67c0d1eb
RS
4488 set_at (sreg, 0);
4489 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4490 break;
4491
4492 case M_BLEUL:
4493 likely = 1;
4494 case M_BLEU:
4495 if (treg == 0)
4496 {
67c0d1eb 4497 macro_build (&offset_expr, likely ? "beql" : "beq",
17a2f251 4498 "s,t,p", sreg, 0);
252b5132
RH
4499 return;
4500 }
4501 if (sreg == 0)
4502 goto do_true;
67c0d1eb
RS
4503 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
4504 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4505 break;
4506
4507 case M_BLEUL_I:
4508 likely = 1;
4509 case M_BLEU_I:
4510 if (sreg == 0
ca4e0257 4511 || (HAVE_32BIT_GPRS
252b5132 4512 && imm_expr.X_op == O_constant
956cd1d6 4513 && imm_expr.X_add_number == (offsetT) 0xffffffff))
252b5132
RH
4514 goto do_true;
4515 if (imm_expr.X_op != O_constant)
4516 as_bad (_("Unsupported large constant"));
f9419b05 4517 ++imm_expr.X_add_number;
252b5132
RH
4518 /* FALLTHROUGH */
4519 case M_BLTU_I:
4520 case M_BLTUL_I:
4521 if (mask == M_BLTUL_I)
4522 likely = 1;
4523 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4524 goto do_false;
4525 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4526 {
67c0d1eb 4527 macro_build (&offset_expr, likely ? "beql" : "beq",
252b5132
RH
4528 "s,t,p", sreg, 0);
4529 return;
4530 }
67c0d1eb
RS
4531 set_at (sreg, 1);
4532 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4533 break;
4534
4535 case M_BLTL:
4536 likely = 1;
4537 case M_BLT:
4538 if (treg == 0)
4539 {
67c0d1eb 4540 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", sreg);
252b5132
RH
4541 return;
4542 }
4543 if (sreg == 0)
4544 {
67c0d1eb 4545 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", treg);
252b5132
RH
4546 return;
4547 }
67c0d1eb
RS
4548 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
4549 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4550 break;
4551
4552 case M_BLTUL:
4553 likely = 1;
4554 case M_BLTU:
4555 if (treg == 0)
4556 goto do_false;
4557 if (sreg == 0)
4558 {
67c0d1eb 4559 macro_build (&offset_expr, likely ? "bnel" : "bne",
17a2f251 4560 "s,t,p", 0, treg);
252b5132
RH
4561 return;
4562 }
67c0d1eb
RS
4563 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
4564 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4565 break;
4566
5f74bc13
CD
4567 case M_DEXT:
4568 {
4569 unsigned long pos;
4570 unsigned long size;
4571
4572 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
4573 {
4574 as_bad (_("Unsupported large constant"));
4575 pos = size = 1;
4576 }
4577 else
4578 {
4579 pos = (unsigned long) imm_expr.X_add_number;
4580 size = (unsigned long) imm2_expr.X_add_number;
4581 }
4582
4583 if (pos > 63)
4584 {
4585 as_bad (_("Improper position (%lu)"), pos);
4586 pos = 1;
4587 }
4588 if (size == 0 || size > 64
4589 || (pos + size - 1) > 63)
4590 {
4591 as_bad (_("Improper extract size (%lu, position %lu)"),
4592 size, pos);
4593 size = 1;
4594 }
4595
4596 if (size <= 32 && pos < 32)
4597 {
4598 s = "dext";
4599 fmt = "t,r,+A,+C";
4600 }
4601 else if (size <= 32)
4602 {
4603 s = "dextu";
4604 fmt = "t,r,+E,+H";
4605 }
4606 else
4607 {
4608 s = "dextm";
4609 fmt = "t,r,+A,+G";
4610 }
67c0d1eb 4611 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, pos, size - 1);
5f74bc13
CD
4612 }
4613 return;
4614
4615 case M_DINS:
4616 {
4617 unsigned long pos;
4618 unsigned long size;
4619
4620 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
4621 {
4622 as_bad (_("Unsupported large constant"));
4623 pos = size = 1;
4624 }
4625 else
4626 {
4627 pos = (unsigned long) imm_expr.X_add_number;
4628 size = (unsigned long) imm2_expr.X_add_number;
4629 }
4630
4631 if (pos > 63)
4632 {
4633 as_bad (_("Improper position (%lu)"), pos);
4634 pos = 1;
4635 }
4636 if (size == 0 || size > 64
4637 || (pos + size - 1) > 63)
4638 {
4639 as_bad (_("Improper insert size (%lu, position %lu)"),
4640 size, pos);
4641 size = 1;
4642 }
4643
4644 if (pos < 32 && (pos + size - 1) < 32)
4645 {
4646 s = "dins";
4647 fmt = "t,r,+A,+B";
4648 }
4649 else if (pos >= 32)
4650 {
4651 s = "dinsu";
4652 fmt = "t,r,+E,+F";
4653 }
4654 else
4655 {
4656 s = "dinsm";
4657 fmt = "t,r,+A,+F";
4658 }
67c0d1eb
RS
4659 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, pos,
4660 pos + size - 1);
5f74bc13
CD
4661 }
4662 return;
4663
252b5132
RH
4664 case M_DDIV_3:
4665 dbl = 1;
4666 case M_DIV_3:
4667 s = "mflo";
4668 goto do_div3;
4669 case M_DREM_3:
4670 dbl = 1;
4671 case M_REM_3:
4672 s = "mfhi";
4673 do_div3:
4674 if (treg == 0)
4675 {
4676 as_warn (_("Divide by zero."));
4677 if (mips_trap)
67c0d1eb 4678 macro_build (NULL, "teq", "s,t,q", 0, 0, 7);
252b5132 4679 else
67c0d1eb 4680 macro_build (NULL, "break", "c", 7);
252b5132
RH
4681 return;
4682 }
4683
b34976b6 4684 mips_emit_delays (TRUE);
252b5132
RH
4685 ++mips_opts.noreorder;
4686 mips_any_noreorder = 1;
4687 if (mips_trap)
4688 {
67c0d1eb
RS
4689 macro_build (NULL, "teq", "s,t,q", treg, 0, 7);
4690 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
252b5132
RH
4691 }
4692 else
4693 {
4694 expr1.X_add_number = 8;
67c0d1eb
RS
4695 macro_build (&expr1, "bne", "s,t,p", treg, 0);
4696 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4697 macro_build (NULL, "break", "c", 7);
252b5132
RH
4698 }
4699 expr1.X_add_number = -1;
f6a22291 4700 load_register (AT, &expr1, dbl);
252b5132 4701 expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
67c0d1eb 4702 macro_build (&expr1, "bne", "s,t,p", treg, AT);
252b5132
RH
4703 if (dbl)
4704 {
4705 expr1.X_add_number = 1;
f6a22291 4706 load_register (AT, &expr1, dbl);
67c0d1eb 4707 macro_build (NULL, "dsll32", "d,w,<", AT, AT, 31);
252b5132
RH
4708 }
4709 else
4710 {
4711 expr1.X_add_number = 0x80000000;
67c0d1eb 4712 macro_build (&expr1, "lui", "t,u", AT, BFD_RELOC_HI16);
252b5132
RH
4713 }
4714 if (mips_trap)
4715 {
67c0d1eb 4716 macro_build (NULL, "teq", "s,t,q", sreg, AT, 6);
252b5132
RH
4717 /* We want to close the noreorder block as soon as possible, so
4718 that later insns are available for delay slot filling. */
4719 --mips_opts.noreorder;
4720 }
4721 else
4722 {
4723 expr1.X_add_number = 8;
67c0d1eb
RS
4724 macro_build (&expr1, "bne", "s,t,p", sreg, AT);
4725 macro_build (NULL, "nop", "", 0);
252b5132
RH
4726
4727 /* We want to close the noreorder block as soon as possible, so
4728 that later insns are available for delay slot filling. */
4729 --mips_opts.noreorder;
4730
67c0d1eb 4731 macro_build (NULL, "break", "c", 6);
252b5132 4732 }
67c0d1eb 4733 macro_build (NULL, s, "d", dreg);
252b5132
RH
4734 break;
4735
4736 case M_DIV_3I:
4737 s = "div";
4738 s2 = "mflo";
4739 goto do_divi;
4740 case M_DIVU_3I:
4741 s = "divu";
4742 s2 = "mflo";
4743 goto do_divi;
4744 case M_REM_3I:
4745 s = "div";
4746 s2 = "mfhi";
4747 goto do_divi;
4748 case M_REMU_3I:
4749 s = "divu";
4750 s2 = "mfhi";
4751 goto do_divi;
4752 case M_DDIV_3I:
4753 dbl = 1;
4754 s = "ddiv";
4755 s2 = "mflo";
4756 goto do_divi;
4757 case M_DDIVU_3I:
4758 dbl = 1;
4759 s = "ddivu";
4760 s2 = "mflo";
4761 goto do_divi;
4762 case M_DREM_3I:
4763 dbl = 1;
4764 s = "ddiv";
4765 s2 = "mfhi";
4766 goto do_divi;
4767 case M_DREMU_3I:
4768 dbl = 1;
4769 s = "ddivu";
4770 s2 = "mfhi";
4771 do_divi:
4772 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4773 {
4774 as_warn (_("Divide by zero."));
4775 if (mips_trap)
67c0d1eb 4776 macro_build (NULL, "teq", "s,t,q", 0, 0, 7);
252b5132 4777 else
67c0d1eb 4778 macro_build (NULL, "break", "c", 7);
252b5132
RH
4779 return;
4780 }
4781 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4782 {
4783 if (strcmp (s2, "mflo") == 0)
67c0d1eb 4784 move_register (dreg, sreg);
252b5132 4785 else
67c0d1eb 4786 move_register (dreg, 0);
252b5132
RH
4787 return;
4788 }
4789 if (imm_expr.X_op == O_constant
4790 && imm_expr.X_add_number == -1
4791 && s[strlen (s) - 1] != 'u')
4792 {
4793 if (strcmp (s2, "mflo") == 0)
4794 {
67c0d1eb 4795 macro_build (NULL, dbl ? "dneg" : "neg", "d,w", dreg, sreg);
252b5132
RH
4796 }
4797 else
67c0d1eb 4798 move_register (dreg, 0);
252b5132
RH
4799 return;
4800 }
4801
67c0d1eb
RS
4802 load_register (AT, &imm_expr, dbl);
4803 macro_build (NULL, s, "z,s,t", sreg, AT);
4804 macro_build (NULL, s2, "d", dreg);
252b5132
RH
4805 break;
4806
4807 case M_DIVU_3:
4808 s = "divu";
4809 s2 = "mflo";
4810 goto do_divu3;
4811 case M_REMU_3:
4812 s = "divu";
4813 s2 = "mfhi";
4814 goto do_divu3;
4815 case M_DDIVU_3:
4816 s = "ddivu";
4817 s2 = "mflo";
4818 goto do_divu3;
4819 case M_DREMU_3:
4820 s = "ddivu";
4821 s2 = "mfhi";
4822 do_divu3:
b34976b6 4823 mips_emit_delays (TRUE);
252b5132
RH
4824 ++mips_opts.noreorder;
4825 mips_any_noreorder = 1;
4826 if (mips_trap)
4827 {
67c0d1eb
RS
4828 macro_build (NULL, "teq", "s,t,q", treg, 0, 7);
4829 macro_build (NULL, s, "z,s,t", sreg, treg);
252b5132
RH
4830 /* We want to close the noreorder block as soon as possible, so
4831 that later insns are available for delay slot filling. */
4832 --mips_opts.noreorder;
4833 }
4834 else
4835 {
4836 expr1.X_add_number = 8;
67c0d1eb
RS
4837 macro_build (&expr1, "bne", "s,t,p", treg, 0);
4838 macro_build (NULL, s, "z,s,t", sreg, treg);
252b5132
RH
4839
4840 /* We want to close the noreorder block as soon as possible, so
4841 that later insns are available for delay slot filling. */
4842 --mips_opts.noreorder;
67c0d1eb 4843 macro_build (NULL, "break", "c", 7);
252b5132 4844 }
67c0d1eb 4845 macro_build (NULL, s2, "d", dreg);
252b5132
RH
4846 return;
4847
1abe91b1
MR
4848 case M_DLCA_AB:
4849 dbl = 1;
4850 case M_LCA_AB:
4851 call = 1;
4852 goto do_la;
252b5132
RH
4853 case M_DLA_AB:
4854 dbl = 1;
4855 case M_LA_AB:
1abe91b1 4856 do_la:
252b5132
RH
4857 /* Load the address of a symbol into a register. If breg is not
4858 zero, we then add a base register to it. */
4859
3bec30a8
TS
4860 if (dbl && HAVE_32BIT_GPRS)
4861 as_warn (_("dla used to load 32-bit register"));
4862
c90bbe5b 4863 if (! dbl && HAVE_64BIT_OBJECTS)
3bec30a8
TS
4864 as_warn (_("la used to load 64-bit address"));
4865
0c11417f
MR
4866 if (offset_expr.X_op == O_constant
4867 && offset_expr.X_add_number >= -0x8000
4868 && offset_expr.X_add_number < 0x8000)
4869 {
67c0d1eb 4870 macro_build (&offset_expr,
b7c7d6c1 4871 (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
17a2f251 4872 "t,r,j", treg, sreg, BFD_RELOC_LO16);
0c11417f
MR
4873 return;
4874 }
4875
afdbd6d0
CD
4876 if (treg == breg)
4877 {
4878 tempreg = AT;
4879 used_at = 1;
4880 }
4881 else
4882 {
4883 tempreg = treg;
4884 used_at = 0;
4885 }
4886
252b5132
RH
4887 /* When generating embedded PIC code, we permit expressions of
4888 the form
afdbd6d0
CD
4889 la $treg,foo-bar
4890 la $treg,foo-bar($breg)
bb2d6cd7 4891 where bar is an address in the current section. These are used
252b5132
RH
4892 when getting the addresses of functions. We don't permit
4893 X_add_number to be non-zero, because if the symbol is
4894 external the relaxing code needs to know that any addend is
4895 purely the offset to X_op_symbol. */
4896 if (mips_pic == EMBEDDED_PIC
4897 && offset_expr.X_op == O_subtract
49309057 4898 && (symbol_constant_p (offset_expr.X_op_symbol)
bb2d6cd7 4899 ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
49309057
ILT
4900 : (symbol_equated_p (offset_expr.X_op_symbol)
4901 && (S_GET_SEGMENT
4902 (symbol_get_value_expression (offset_expr.X_op_symbol)
4903 ->X_add_symbol)
bb2d6cd7 4904 == now_seg)))
bb2d6cd7
GK
4905 && (offset_expr.X_add_number == 0
4906 || OUTPUT_FLAVOR == bfd_target_elf_flavour))
252b5132 4907 {
afdbd6d0
CD
4908 if (breg == 0)
4909 {
4910 tempreg = treg;
4911 used_at = 0;
67c0d1eb
RS
4912 macro_build (&offset_expr, "lui", "t,u",
4913 tempreg, BFD_RELOC_PCREL_HI16_S);
afdbd6d0
CD
4914 }
4915 else
4916 {
67c0d1eb
RS
4917 macro_build (&offset_expr, "lui", "t,u",
4918 tempreg, BFD_RELOC_PCREL_HI16_S);
4919 macro_build (NULL,
4d34fb5f 4920 (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu",
afdbd6d0
CD
4921 "d,v,t", tempreg, tempreg, breg);
4922 }
67c0d1eb 4923 macro_build (&offset_expr,
4d34fb5f 4924 (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
17a2f251 4925 "t,r,j", treg, tempreg, BFD_RELOC_PCREL_LO16);
afdbd6d0
CD
4926 if (! used_at)
4927 return;
4928 break;
252b5132
RH
4929 }
4930
4931 if (offset_expr.X_op != O_symbol
4932 && offset_expr.X_op != O_constant)
4933 {
4934 as_bad (_("expression too complex"));
4935 offset_expr.X_op = O_constant;
4936 }
4937
252b5132 4938 if (offset_expr.X_op == O_constant)
67c0d1eb 4939 load_register (tempreg, &offset_expr,
4d34fb5f
TS
4940 ((mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
4941 ? (dbl || HAVE_64BIT_ADDRESSES)
4942 : HAVE_64BIT_ADDRESSES));
252b5132
RH
4943 else if (mips_pic == NO_PIC)
4944 {
d6bc6245 4945 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 4946 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
4947 Otherwise we want
4948 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
4949 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4950 If we have a constant, we need two instructions anyhow,
d6bc6245 4951 so we may as well always use the latter form.
76b3015f 4952
d6bc6245
TS
4953 With 64bit address space and a usable $at we want
4954 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4955 lui $at,<sym> (BFD_RELOC_HI16_S)
4956 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4957 daddiu $at,<sym> (BFD_RELOC_LO16)
4958 dsll32 $tempreg,0
3a482fd5 4959 daddu $tempreg,$tempreg,$at
76b3015f 4960
c03099e6 4961 If $at is already in use, we use a path which is suboptimal
d6bc6245
TS
4962 on superscalar processors.
4963 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4964 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4965 dsll $tempreg,16
4966 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
4967 dsll $tempreg,16
4968 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
4969 */
d6bc6245 4970 if (HAVE_64BIT_ADDRESSES)
252b5132 4971 {
e864ceca
RS
4972 /* ??? We don't provide a GP-relative alternative for
4973 these macros. It used not to be possible with the
4974 original relaxation code, but it could be done now. */
d6bc6245 4975
460597ba 4976 if (used_at == 0 && ! mips_opts.noat)
98d3f06f 4977 {
67c0d1eb 4978 macro_build (&offset_expr, "lui", "t,u",
17a2f251 4979 tempreg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb 4980 macro_build (&offset_expr, "lui", "t,u",
17a2f251 4981 AT, BFD_RELOC_HI16_S);
67c0d1eb 4982 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 4983 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
67c0d1eb 4984 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 4985 AT, AT, BFD_RELOC_LO16);
67c0d1eb
RS
4986 macro_build (NULL, "dsll32", "d,w,<", tempreg, tempreg, 0);
4987 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
98d3f06f
KH
4988 used_at = 1;
4989 }
4990 else
4991 {
67c0d1eb 4992 macro_build (&offset_expr, "lui", "t,u",
17a2f251 4993 tempreg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb 4994 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 4995 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
67c0d1eb
RS
4996 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
4997 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 4998 tempreg, tempreg, BFD_RELOC_HI16_S);
67c0d1eb
RS
4999 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
5000 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 5001 tempreg, tempreg, BFD_RELOC_LO16);
98d3f06f
KH
5002 }
5003 }
5004 else
5005 {
5006 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
5007 && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
5008 {
4d7206a2 5009 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
5010 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5011 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 5012 relax_switch ();
98d3f06f 5013 }
67c0d1eb
RS
5014 macro_build_lui (&offset_expr, tempreg);
5015 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5016 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2
RS
5017 if (mips_relax.sequence)
5018 relax_end ();
98d3f06f 5019 }
252b5132 5020 }
f5040a92 5021 else if (mips_pic == SVR4_PIC && ! mips_big_got && ! HAVE_NEWABI)
252b5132 5022 {
9117d219
NC
5023 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5024
252b5132
RH
5025 /* If this is a reference to an external symbol, and there
5026 is no constant, we want
5027 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
1abe91b1 5028 or for lca or if tempreg is PIC_CALL_REG
9117d219 5029 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
252b5132
RH
5030 For a local symbol, we want
5031 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5032 nop
5033 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5034
5035 If we have a small constant, and this is a reference to
5036 an external symbol, we want
5037 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5038 nop
5039 addiu $tempreg,$tempreg,<constant>
5040 For a local symbol, we want the same instruction
5041 sequence, but we output a BFD_RELOC_LO16 reloc on the
5042 addiu instruction.
5043
5044 If we have a large constant, and this is a reference to
5045 an external symbol, we want
5046 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5047 lui $at,<hiconstant>
5048 addiu $at,$at,<loconstant>
5049 addu $tempreg,$tempreg,$at
5050 For a local symbol, we want the same instruction
5051 sequence, but we output a BFD_RELOC_LO16 reloc on the
ed6fb7bd 5052 addiu instruction.
ed6fb7bd
SC
5053 */
5054
4d7206a2 5055 if (offset_expr.X_add_number == 0)
252b5132 5056 {
4d7206a2
RS
5057 if (breg == 0 && (call || tempreg == PIC_CALL_REG))
5058 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
5059
5060 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
5061 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5062 lw_reloc_type, mips_gp_register);
4d7206a2 5063 if (breg != 0)
252b5132
RH
5064 {
5065 /* We're going to put in an addu instruction using
5066 tempreg, so we may as well insert the nop right
5067 now. */
269137b2 5068 load_delay_nop ();
252b5132 5069 }
4d7206a2 5070 relax_switch ();
67c0d1eb
RS
5071 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5072 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 5073 load_delay_nop ();
67c0d1eb
RS
5074 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5075 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2 5076 relax_end ();
252b5132
RH
5077 /* FIXME: If breg == 0, and the next instruction uses
5078 $tempreg, then if this variant case is used an extra
5079 nop will be generated. */
5080 }
4d7206a2
RS
5081 else if (offset_expr.X_add_number >= -0x8000
5082 && offset_expr.X_add_number < 0x8000)
252b5132 5083 {
67c0d1eb 5084 load_got_offset (tempreg, &offset_expr);
269137b2 5085 load_delay_nop ();
67c0d1eb 5086 add_got_offset (tempreg, &offset_expr);
252b5132
RH
5087 }
5088 else
5089 {
4d7206a2
RS
5090 expr1.X_add_number = offset_expr.X_add_number;
5091 offset_expr.X_add_number =
5092 ((offset_expr.X_add_number + 0x8000) & 0xffff) - 0x8000;
67c0d1eb 5093 load_got_offset (tempreg, &offset_expr);
f6a22291 5094 offset_expr.X_add_number = expr1.X_add_number;
252b5132
RH
5095 /* If we are going to add in a base register, and the
5096 target register and the base register are the same,
5097 then we are using AT as a temporary register. Since
5098 we want to load the constant into AT, we add our
5099 current AT (from the global offset table) and the
5100 register into the register now, and pretend we were
5101 not using a base register. */
67c0d1eb 5102 if (breg == treg)
252b5132 5103 {
269137b2 5104 load_delay_nop ();
67c0d1eb 5105 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5106 treg, AT, breg);
252b5132
RH
5107 breg = 0;
5108 tempreg = treg;
252b5132 5109 }
f6a22291 5110 add_got_offset_hilo (tempreg, &offset_expr, AT);
252b5132
RH
5111 used_at = 1;
5112 }
5113 }
f5040a92
AO
5114 else if (mips_pic == SVR4_PIC && ! mips_big_got && HAVE_NEWABI)
5115 {
67c0d1eb 5116 int add_breg_early = 0;
f5040a92
AO
5117
5118 /* If this is a reference to an external, and there is no
5119 constant, or local symbol (*), with or without a
5120 constant, we want
5121 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
1abe91b1 5122 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
5123 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5124
5125 If we have a small constant, and this is a reference to
5126 an external symbol, we want
5127 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5128 addiu $tempreg,$tempreg,<constant>
5129
5130 If we have a large constant, and this is a reference to
5131 an external symbol, we want
5132 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5133 lui $at,<hiconstant>
5134 addiu $at,$at,<loconstant>
5135 addu $tempreg,$tempreg,$at
5136
5137 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
5138 local symbols, even though it introduces an additional
5139 instruction. */
5140
f5040a92
AO
5141 if (offset_expr.X_add_number)
5142 {
4d7206a2 5143 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
5144 offset_expr.X_add_number = 0;
5145
4d7206a2 5146 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
5147 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5148 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
5149
5150 if (expr1.X_add_number >= -0x8000
5151 && expr1.X_add_number < 0x8000)
5152 {
67c0d1eb
RS
5153 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
5154 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 5155 }
ecd13cd3 5156 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92
AO
5157 {
5158 int dreg;
5159
5160 /* If we are going to add in a base register, and the
5161 target register and the base register are the same,
5162 then we are using AT as a temporary register. Since
5163 we want to load the constant into AT, we add our
5164 current AT (from the global offset table) and the
5165 register into the register now, and pretend we were
5166 not using a base register. */
5167 if (breg != treg)
5168 dreg = tempreg;
5169 else
5170 {
5171 assert (tempreg == AT);
67c0d1eb
RS
5172 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5173 treg, AT, breg);
f5040a92 5174 dreg = treg;
67c0d1eb 5175 add_breg_early = 1;
f5040a92
AO
5176 }
5177
f6a22291 5178 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 5179 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5180 dreg, dreg, AT);
f5040a92 5181
f5040a92
AO
5182 used_at = 1;
5183 }
5184 else
5185 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5186
4d7206a2 5187 relax_switch ();
f5040a92
AO
5188 offset_expr.X_add_number = expr1.X_add_number;
5189
67c0d1eb
RS
5190 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5191 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5192 if (add_breg_early)
f5040a92 5193 {
67c0d1eb 5194 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
f899b4b8 5195 treg, tempreg, breg);
f5040a92
AO
5196 breg = 0;
5197 tempreg = treg;
5198 }
4d7206a2 5199 relax_end ();
f5040a92 5200 }
4d7206a2 5201 else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
f5040a92 5202 {
4d7206a2 5203 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
5204 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5205 BFD_RELOC_MIPS_CALL16, mips_gp_register);
4d7206a2 5206 relax_switch ();
67c0d1eb
RS
5207 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5208 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2 5209 relax_end ();
f5040a92 5210 }
4d7206a2 5211 else
f5040a92 5212 {
67c0d1eb
RS
5213 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5214 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
5215 }
5216 }
5217 else if (mips_pic == SVR4_PIC && ! HAVE_NEWABI)
252b5132 5218 {
67c0d1eb 5219 int gpdelay;
9117d219
NC
5220 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5221 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
ed6fb7bd 5222 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
252b5132
RH
5223
5224 /* This is the large GOT case. If this is a reference to an
5225 external symbol, and there is no constant, we want
5226 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5227 addu $tempreg,$tempreg,$gp
5228 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 5229 or for lca or if tempreg is PIC_CALL_REG
9117d219
NC
5230 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5231 addu $tempreg,$tempreg,$gp
5232 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
252b5132
RH
5233 For a local symbol, we want
5234 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5235 nop
5236 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5237
5238 If we have a small constant, and this is a reference to
5239 an external symbol, we want
5240 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5241 addu $tempreg,$tempreg,$gp
5242 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5243 nop
5244 addiu $tempreg,$tempreg,<constant>
5245 For a local symbol, we want
5246 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5247 nop
5248 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
5249
5250 If we have a large constant, and this is a reference to
5251 an external symbol, we want
5252 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5253 addu $tempreg,$tempreg,$gp
5254 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5255 lui $at,<hiconstant>
5256 addiu $at,$at,<loconstant>
5257 addu $tempreg,$tempreg,$at
5258 For a local symbol, we want
5259 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5260 lui $at,<hiconstant>
5261 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
5262 addu $tempreg,$tempreg,$at
f5040a92 5263 */
438c16b8 5264
252b5132
RH
5265 expr1.X_add_number = offset_expr.X_add_number;
5266 offset_expr.X_add_number = 0;
4d7206a2 5267 relax_start (offset_expr.X_add_symbol);
67c0d1eb 5268 gpdelay = reg_needs_delay (mips_gp_register);
1abe91b1
MR
5269 if (expr1.X_add_number == 0 && breg == 0
5270 && (call || tempreg == PIC_CALL_REG))
9117d219
NC
5271 {
5272 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5273 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5274 }
67c0d1eb
RS
5275 macro_build (&offset_expr, "lui", "t,u", tempreg, lui_reloc_type);
5276 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5277 tempreg, tempreg, mips_gp_register);
67c0d1eb 5278 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 5279 tempreg, lw_reloc_type, tempreg);
252b5132
RH
5280 if (expr1.X_add_number == 0)
5281 {
67c0d1eb 5282 if (breg != 0)
252b5132
RH
5283 {
5284 /* We're going to put in an addu instruction using
5285 tempreg, so we may as well insert the nop right
5286 now. */
269137b2 5287 load_delay_nop ();
252b5132 5288 }
252b5132
RH
5289 }
5290 else if (expr1.X_add_number >= -0x8000
5291 && expr1.X_add_number < 0x8000)
5292 {
269137b2 5293 load_delay_nop ();
67c0d1eb 5294 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 5295 tempreg, tempreg, BFD_RELOC_LO16);
252b5132
RH
5296 }
5297 else
5298 {
67c0d1eb 5299 int dreg;
252b5132
RH
5300
5301 /* If we are going to add in a base register, and the
5302 target register and the base register are the same,
5303 then we are using AT as a temporary register. Since
5304 we want to load the constant into AT, we add our
5305 current AT (from the global offset table) and the
5306 register into the register now, and pretend we were
5307 not using a base register. */
5308 if (breg != treg)
67c0d1eb 5309 dreg = tempreg;
252b5132
RH
5310 else
5311 {
5312 assert (tempreg == AT);
269137b2 5313 load_delay_nop ();
67c0d1eb 5314 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5315 treg, AT, breg);
252b5132 5316 dreg = treg;
252b5132
RH
5317 }
5318
f6a22291 5319 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 5320 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
252b5132 5321
252b5132
RH
5322 used_at = 1;
5323 }
4d7206a2
RS
5324 offset_expr.X_add_number =
5325 ((expr1.X_add_number + 0x8000) & 0xffff) - 0x8000;
5326 relax_switch ();
252b5132 5327
67c0d1eb 5328 if (gpdelay)
252b5132
RH
5329 {
5330 /* This is needed because this instruction uses $gp, but
f5040a92 5331 the first instruction on the main stream does not. */
67c0d1eb 5332 macro_build (NULL, "nop", "");
252b5132 5333 }
ed6fb7bd 5334
67c0d1eb
RS
5335 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5336 local_reloc_type, mips_gp_register);
f5040a92 5337 if (expr1.X_add_number >= -0x8000
252b5132
RH
5338 && expr1.X_add_number < 0x8000)
5339 {
269137b2 5340 load_delay_nop ();
67c0d1eb
RS
5341 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5342 tempreg, tempreg, BFD_RELOC_LO16);
252b5132 5343 /* FIXME: If add_number is 0, and there was no base
f5040a92
AO
5344 register, the external symbol case ended with a load,
5345 so if the symbol turns out to not be external, and
5346 the next instruction uses tempreg, an unnecessary nop
5347 will be inserted. */
252b5132
RH
5348 }
5349 else
5350 {
5351 if (breg == treg)
5352 {
5353 /* We must add in the base register now, as in the
f5040a92 5354 external symbol case. */
252b5132 5355 assert (tempreg == AT);
269137b2 5356 load_delay_nop ();
67c0d1eb 5357 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5358 treg, AT, breg);
252b5132
RH
5359 tempreg = treg;
5360 /* We set breg to 0 because we have arranged to add
f5040a92 5361 it in in both cases. */
252b5132
RH
5362 breg = 0;
5363 }
5364
67c0d1eb
RS
5365 macro_build_lui (&expr1, AT);
5366 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 5367 AT, AT, BFD_RELOC_LO16);
67c0d1eb 5368 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5369 tempreg, tempreg, AT);
252b5132 5370 }
4d7206a2 5371 relax_end ();
252b5132 5372 }
f5040a92
AO
5373 else if (mips_pic == SVR4_PIC && HAVE_NEWABI)
5374 {
f5040a92
AO
5375 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5376 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
67c0d1eb 5377 int add_breg_early = 0;
f5040a92
AO
5378
5379 /* This is the large GOT case. If this is a reference to an
5380 external symbol, and there is no constant, we want
5381 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5382 add $tempreg,$tempreg,$gp
5383 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 5384 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
5385 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5386 add $tempreg,$tempreg,$gp
5387 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5388
5389 If we have a small constant, and this is a reference to
5390 an external symbol, we want
5391 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5392 add $tempreg,$tempreg,$gp
5393 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5394 addi $tempreg,$tempreg,<constant>
5395
5396 If we have a large constant, and this is a reference to
5397 an external symbol, we want
5398 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5399 addu $tempreg,$tempreg,$gp
5400 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5401 lui $at,<hiconstant>
5402 addi $at,$at,<loconstant>
5403 add $tempreg,$tempreg,$at
5404
5405 If we have NewABI, and we know it's a local symbol, we want
5406 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
5407 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
5408 otherwise we have to resort to GOT_HI16/GOT_LO16. */
5409
4d7206a2 5410 relax_start (offset_expr.X_add_symbol);
f5040a92 5411
4d7206a2 5412 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
5413 offset_expr.X_add_number = 0;
5414
1abe91b1
MR
5415 if (expr1.X_add_number == 0 && breg == 0
5416 && (call || tempreg == PIC_CALL_REG))
f5040a92
AO
5417 {
5418 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5419 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5420 }
67c0d1eb
RS
5421 macro_build (&offset_expr, "lui", "t,u", tempreg, lui_reloc_type);
5422 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5423 tempreg, tempreg, mips_gp_register);
67c0d1eb
RS
5424 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5425 tempreg, lw_reloc_type, tempreg);
f5040a92
AO
5426
5427 if (expr1.X_add_number == 0)
4d7206a2 5428 ;
f5040a92
AO
5429 else if (expr1.X_add_number >= -0x8000
5430 && expr1.X_add_number < 0x8000)
5431 {
67c0d1eb 5432 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 5433 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 5434 }
ecd13cd3 5435 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92
AO
5436 {
5437 int dreg;
5438
5439 /* If we are going to add in a base register, and the
5440 target register and the base register are the same,
5441 then we are using AT as a temporary register. Since
5442 we want to load the constant into AT, we add our
5443 current AT (from the global offset table) and the
5444 register into the register now, and pretend we were
5445 not using a base register. */
5446 if (breg != treg)
5447 dreg = tempreg;
5448 else
5449 {
5450 assert (tempreg == AT);
67c0d1eb 5451 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5452 treg, AT, breg);
f5040a92 5453 dreg = treg;
67c0d1eb 5454 add_breg_early = 1;
f5040a92
AO
5455 }
5456
f6a22291 5457 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 5458 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
f5040a92 5459
f5040a92
AO
5460 used_at = 1;
5461 }
5462 else
5463 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5464
4d7206a2 5465 relax_switch ();
f5040a92 5466 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
5467 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5468 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
5469 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
5470 tempreg, BFD_RELOC_MIPS_GOT_OFST);
5471 if (add_breg_early)
f5040a92 5472 {
67c0d1eb 5473 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5474 treg, tempreg, breg);
f5040a92
AO
5475 breg = 0;
5476 tempreg = treg;
5477 }
4d7206a2 5478 relax_end ();
f5040a92 5479 }
252b5132
RH
5480 else if (mips_pic == EMBEDDED_PIC)
5481 {
5482 /* We use
cdf6fd85 5483 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132 5484 */
67c0d1eb
RS
5485 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
5486 mips_gp_register, BFD_RELOC_GPREL16);
252b5132
RH
5487 }
5488 else
5489 abort ();
5490
5491 if (breg != 0)
4d34fb5f
TS
5492 {
5493 char *s;
5494
5495 if (mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
b7c7d6c1 5496 s = (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu";
4d34fb5f 5497 else
f899b4b8 5498 s = ADDRESS_ADD_INSN;
4d34fb5f 5499
67c0d1eb 5500 macro_build (NULL, s, "d,v,t", treg, tempreg, breg);
4d34fb5f 5501 }
252b5132
RH
5502
5503 if (! used_at)
5504 return;
5505
5506 break;
5507
5508 case M_J_A:
5509 /* The j instruction may not be used in PIC code, since it
5510 requires an absolute address. We convert it to a b
5511 instruction. */
5512 if (mips_pic == NO_PIC)
67c0d1eb 5513 macro_build (&offset_expr, "j", "a");
252b5132 5514 else
67c0d1eb 5515 macro_build (&offset_expr, "b", "p");
252b5132
RH
5516 return;
5517
5518 /* The jal instructions must be handled as macros because when
5519 generating PIC code they expand to multi-instruction
5520 sequences. Normally they are simple instructions. */
5521 case M_JAL_1:
5522 dreg = RA;
5523 /* Fall through. */
5524 case M_JAL_2:
5525 if (mips_pic == NO_PIC
5526 || mips_pic == EMBEDDED_PIC)
67c0d1eb 5527 macro_build (NULL, "jalr", "d,s", dreg, sreg);
252b5132
RH
5528 else if (mips_pic == SVR4_PIC)
5529 {
5530 if (sreg != PIC_CALL_REG)
5531 as_warn (_("MIPS PIC call to register other than $25"));
bdaaa2e1 5532
67c0d1eb 5533 macro_build (NULL, "jalr", "d,s", dreg, sreg);
6478892d 5534 if (! HAVE_NEWABI)
252b5132 5535 {
6478892d
TS
5536 if (mips_cprestore_offset < 0)
5537 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5538 else
5539 {
7a621144
DJ
5540 if (! mips_frame_reg_valid)
5541 {
5542 as_warn (_("No .frame pseudo-op used in PIC code"));
5543 /* Quiet this warning. */
5544 mips_frame_reg_valid = 1;
5545 }
5546 if (! mips_cprestore_valid)
5547 {
5548 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5549 /* Quiet this warning. */
5550 mips_cprestore_valid = 1;
5551 }
6478892d 5552 expr1.X_add_number = mips_cprestore_offset;
67c0d1eb 5553 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 5554 mips_gp_register,
256ab948
TS
5555 mips_frame_reg,
5556 HAVE_64BIT_ADDRESSES);
6478892d 5557 }
252b5132
RH
5558 }
5559 }
5560 else
5561 abort ();
5562
5563 return;
5564
5565 case M_JAL_A:
5566 if (mips_pic == NO_PIC)
67c0d1eb 5567 macro_build (&offset_expr, "jal", "a");
252b5132
RH
5568 else if (mips_pic == SVR4_PIC)
5569 {
5570 /* If this is a reference to an external symbol, and we are
5571 using a small GOT, we want
5572 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5573 nop
f9419b05 5574 jalr $ra,$25
252b5132
RH
5575 nop
5576 lw $gp,cprestore($sp)
5577 The cprestore value is set using the .cprestore
5578 pseudo-op. If we are using a big GOT, we want
5579 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5580 addu $25,$25,$gp
5581 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
5582 nop
f9419b05 5583 jalr $ra,$25
252b5132
RH
5584 nop
5585 lw $gp,cprestore($sp)
5586 If the symbol is not external, we want
5587 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5588 nop
5589 addiu $25,$25,<sym> (BFD_RELOC_LO16)
f9419b05 5590 jalr $ra,$25
252b5132 5591 nop
438c16b8 5592 lw $gp,cprestore($sp)
f5040a92
AO
5593
5594 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
5595 sequences above, minus nops, unless the symbol is local,
5596 which enables us to use GOT_PAGE/GOT_OFST (big got) or
5597 GOT_DISP. */
438c16b8 5598 if (HAVE_NEWABI)
252b5132 5599 {
f5040a92
AO
5600 if (! mips_big_got)
5601 {
4d7206a2 5602 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
5603 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5604 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
f5040a92 5605 mips_gp_register);
4d7206a2 5606 relax_switch ();
67c0d1eb
RS
5607 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5608 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
4d7206a2
RS
5609 mips_gp_register);
5610 relax_end ();
f5040a92
AO
5611 }
5612 else
5613 {
4d7206a2 5614 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
5615 macro_build (&offset_expr, "lui", "t,u", PIC_CALL_REG,
5616 BFD_RELOC_MIPS_CALL_HI16);
5617 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
5618 PIC_CALL_REG, mips_gp_register);
5619 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5620 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
5621 PIC_CALL_REG);
4d7206a2 5622 relax_switch ();
67c0d1eb
RS
5623 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5624 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
5625 mips_gp_register);
5626 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5627 PIC_CALL_REG, PIC_CALL_REG,
17a2f251 5628 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 5629 relax_end ();
f5040a92 5630 }
684022ea 5631
67c0d1eb 5632 macro_build_jalr (&offset_expr);
252b5132
RH
5633 }
5634 else
5635 {
4d7206a2 5636 relax_start (offset_expr.X_add_symbol);
438c16b8
TS
5637 if (! mips_big_got)
5638 {
67c0d1eb
RS
5639 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5640 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
17a2f251 5641 mips_gp_register);
269137b2 5642 load_delay_nop ();
4d7206a2 5643 relax_switch ();
438c16b8 5644 }
252b5132 5645 else
252b5132 5646 {
67c0d1eb
RS
5647 int gpdelay;
5648
5649 gpdelay = reg_needs_delay (mips_gp_register);
5650 macro_build (&offset_expr, "lui", "t,u", PIC_CALL_REG,
5651 BFD_RELOC_MIPS_CALL_HI16);
5652 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
5653 PIC_CALL_REG, mips_gp_register);
5654 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5655 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
5656 PIC_CALL_REG);
269137b2 5657 load_delay_nop ();
4d7206a2 5658 relax_switch ();
67c0d1eb
RS
5659 if (gpdelay)
5660 macro_build (NULL, "nop", "");
252b5132 5661 }
67c0d1eb
RS
5662 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5663 PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
4d7206a2 5664 mips_gp_register);
269137b2 5665 load_delay_nop ();
67c0d1eb
RS
5666 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5667 PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
4d7206a2 5668 relax_end ();
67c0d1eb 5669 macro_build_jalr (&offset_expr);
438c16b8 5670
6478892d
TS
5671 if (mips_cprestore_offset < 0)
5672 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5673 else
5674 {
7a621144
DJ
5675 if (! mips_frame_reg_valid)
5676 {
5677 as_warn (_("No .frame pseudo-op used in PIC code"));
5678 /* Quiet this warning. */
5679 mips_frame_reg_valid = 1;
5680 }
5681 if (! mips_cprestore_valid)
5682 {
5683 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5684 /* Quiet this warning. */
5685 mips_cprestore_valid = 1;
5686 }
6478892d 5687 if (mips_opts.noreorder)
67c0d1eb 5688 macro_build (NULL, "nop", "");
6478892d 5689 expr1.X_add_number = mips_cprestore_offset;
67c0d1eb 5690 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 5691 mips_gp_register,
256ab948
TS
5692 mips_frame_reg,
5693 HAVE_64BIT_ADDRESSES);
6478892d 5694 }
252b5132
RH
5695 }
5696 }
5697 else if (mips_pic == EMBEDDED_PIC)
5698 {
67c0d1eb 5699 macro_build (&offset_expr, "bal", "p");
252b5132
RH
5700 /* The linker may expand the call to a longer sequence which
5701 uses $at, so we must break rather than return. */
5702 break;
5703 }
5704 else
5705 abort ();
5706
5707 return;
5708
5709 case M_LB_AB:
5710 s = "lb";
5711 goto ld;
5712 case M_LBU_AB:
5713 s = "lbu";
5714 goto ld;
5715 case M_LH_AB:
5716 s = "lh";
5717 goto ld;
5718 case M_LHU_AB:
5719 s = "lhu";
5720 goto ld;
5721 case M_LW_AB:
5722 s = "lw";
5723 goto ld;
5724 case M_LWC0_AB:
5725 s = "lwc0";
bdaaa2e1 5726 /* Itbl support may require additional care here. */
252b5132
RH
5727 coproc = 1;
5728 goto ld;
5729 case M_LWC1_AB:
5730 s = "lwc1";
bdaaa2e1 5731 /* Itbl support may require additional care here. */
252b5132
RH
5732 coproc = 1;
5733 goto ld;
5734 case M_LWC2_AB:
5735 s = "lwc2";
bdaaa2e1 5736 /* Itbl support may require additional care here. */
252b5132
RH
5737 coproc = 1;
5738 goto ld;
5739 case M_LWC3_AB:
5740 s = "lwc3";
bdaaa2e1 5741 /* Itbl support may require additional care here. */
252b5132
RH
5742 coproc = 1;
5743 goto ld;
5744 case M_LWL_AB:
5745 s = "lwl";
5746 lr = 1;
5747 goto ld;
5748 case M_LWR_AB:
5749 s = "lwr";
5750 lr = 1;
5751 goto ld;
5752 case M_LDC1_AB:
fef14a42 5753 if (mips_opts.arch == CPU_R4650)
252b5132
RH
5754 {
5755 as_bad (_("opcode not supported on this processor"));
5756 return;
5757 }
5758 s = "ldc1";
bdaaa2e1 5759 /* Itbl support may require additional care here. */
252b5132
RH
5760 coproc = 1;
5761 goto ld;
5762 case M_LDC2_AB:
5763 s = "ldc2";
bdaaa2e1 5764 /* Itbl support may require additional care here. */
252b5132
RH
5765 coproc = 1;
5766 goto ld;
5767 case M_LDC3_AB:
5768 s = "ldc3";
bdaaa2e1 5769 /* Itbl support may require additional care here. */
252b5132
RH
5770 coproc = 1;
5771 goto ld;
5772 case M_LDL_AB:
5773 s = "ldl";
5774 lr = 1;
5775 goto ld;
5776 case M_LDR_AB:
5777 s = "ldr";
5778 lr = 1;
5779 goto ld;
5780 case M_LL_AB:
5781 s = "ll";
5782 goto ld;
5783 case M_LLD_AB:
5784 s = "lld";
5785 goto ld;
5786 case M_LWU_AB:
5787 s = "lwu";
5788 ld:
5789 if (breg == treg || coproc || lr)
5790 {
5791 tempreg = AT;
5792 used_at = 1;
5793 }
5794 else
5795 {
5796 tempreg = treg;
5797 used_at = 0;
5798 }
5799 goto ld_st;
5800 case M_SB_AB:
5801 s = "sb";
5802 goto st;
5803 case M_SH_AB:
5804 s = "sh";
5805 goto st;
5806 case M_SW_AB:
5807 s = "sw";
5808 goto st;
5809 case M_SWC0_AB:
5810 s = "swc0";
bdaaa2e1 5811 /* Itbl support may require additional care here. */
252b5132
RH
5812 coproc = 1;
5813 goto st;
5814 case M_SWC1_AB:
5815 s = "swc1";
bdaaa2e1 5816 /* Itbl support may require additional care here. */
252b5132
RH
5817 coproc = 1;
5818 goto st;
5819 case M_SWC2_AB:
5820 s = "swc2";
bdaaa2e1 5821 /* Itbl support may require additional care here. */
252b5132
RH
5822 coproc = 1;
5823 goto st;
5824 case M_SWC3_AB:
5825 s = "swc3";
bdaaa2e1 5826 /* Itbl support may require additional care here. */
252b5132
RH
5827 coproc = 1;
5828 goto st;
5829 case M_SWL_AB:
5830 s = "swl";
5831 goto st;
5832 case M_SWR_AB:
5833 s = "swr";
5834 goto st;
5835 case M_SC_AB:
5836 s = "sc";
5837 goto st;
5838 case M_SCD_AB:
5839 s = "scd";
5840 goto st;
5841 case M_SDC1_AB:
fef14a42 5842 if (mips_opts.arch == CPU_R4650)
252b5132
RH
5843 {
5844 as_bad (_("opcode not supported on this processor"));
5845 return;
5846 }
5847 s = "sdc1";
5848 coproc = 1;
bdaaa2e1 5849 /* Itbl support may require additional care here. */
252b5132
RH
5850 goto st;
5851 case M_SDC2_AB:
5852 s = "sdc2";
bdaaa2e1 5853 /* Itbl support may require additional care here. */
252b5132
RH
5854 coproc = 1;
5855 goto st;
5856 case M_SDC3_AB:
5857 s = "sdc3";
bdaaa2e1 5858 /* Itbl support may require additional care here. */
252b5132
RH
5859 coproc = 1;
5860 goto st;
5861 case M_SDL_AB:
5862 s = "sdl";
5863 goto st;
5864 case M_SDR_AB:
5865 s = "sdr";
5866 st:
5867 tempreg = AT;
5868 used_at = 1;
5869 ld_st:
bdaaa2e1 5870 /* Itbl support may require additional care here. */
252b5132
RH
5871 if (mask == M_LWC1_AB
5872 || mask == M_SWC1_AB
5873 || mask == M_LDC1_AB
5874 || mask == M_SDC1_AB
5875 || mask == M_L_DAB
5876 || mask == M_S_DAB)
5877 fmt = "T,o(b)";
5878 else if (coproc)
5879 fmt = "E,o(b)";
5880 else
5881 fmt = "t,o(b)";
5882
256ab948
TS
5883 /* Sign-extending 32-bit constants makes their handling easier.
5884 The HAVE_64BIT_GPRS... part is due to the linux kernel hack
5885 described below. */
5886 if ((! HAVE_64BIT_ADDRESSES
5887 && (! HAVE_64BIT_GPRS && offset_expr.X_op == O_constant))
d17e7bce
TS
5888 && (offset_expr.X_op == O_constant)
5889 && ! ((offset_expr.X_add_number & ~((bfd_vma) 0x7fffffff))
5890 == ~((bfd_vma) 0x7fffffff)))
ae826530 5891 {
d17e7bce 5892 if (offset_expr.X_add_number & ~((bfd_vma) 0xffffffff))
1b8e29e5 5893 as_bad (_("constant too large"));
ae826530 5894
1b8e29e5
TS
5895 offset_expr.X_add_number = (((offset_expr.X_add_number & 0xffffffff)
5896 ^ 0x80000000) - 0x80000000);
ae826530 5897 }
256ab948 5898
afdbd6d0
CD
5899 /* For embedded PIC, we allow loads where the offset is calculated
5900 by subtracting a symbol in the current segment from an unknown
5901 symbol, relative to a base register, e.g.:
5902 <op> $treg, <sym>-<localsym>($breg)
5903 This is used by the compiler for switch statements. */
76b3015f 5904 if (mips_pic == EMBEDDED_PIC
afdbd6d0
CD
5905 && offset_expr.X_op == O_subtract
5906 && (symbol_constant_p (offset_expr.X_op_symbol)
5907 ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
5908 : (symbol_equated_p (offset_expr.X_op_symbol)
5909 && (S_GET_SEGMENT
5910 (symbol_get_value_expression (offset_expr.X_op_symbol)
5911 ->X_add_symbol)
5912 == now_seg)))
5913 && breg != 0
5914 && (offset_expr.X_add_number == 0
5915 || OUTPUT_FLAVOR == bfd_target_elf_flavour))
5916 {
5917 /* For this case, we output the instructions:
5918 lui $tempreg,<sym> (BFD_RELOC_PCREL_HI16_S)
5919 addiu $tempreg,$tempreg,$breg
5920 <op> $treg,<sym>($tempreg) (BFD_RELOC_PCREL_LO16)
5921 If the relocation would fit entirely in 16 bits, it would be
5922 nice to emit:
5923 <op> $treg,<sym>($breg) (BFD_RELOC_PCREL_LO16)
5924 instead, but that seems quite difficult. */
67c0d1eb 5925 macro_build (&offset_expr, "lui", "t,u", tempreg,
17a2f251 5926 BFD_RELOC_PCREL_HI16_S);
67c0d1eb
RS
5927 macro_build (NULL,
5928 ((bfd_arch_bits_per_address (stdoutput) == 32
5929 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
5930 ? "addu" : "daddu"),
5931 "d,v,t", tempreg, tempreg, breg);
5932 macro_build (&offset_expr, s, fmt, treg,
5933 BFD_RELOC_PCREL_LO16, tempreg);
afdbd6d0
CD
5934 if (! used_at)
5935 return;
5936 break;
5937 }
5938
252b5132
RH
5939 if (offset_expr.X_op != O_constant
5940 && offset_expr.X_op != O_symbol)
5941 {
5942 as_bad (_("expression too complex"));
5943 offset_expr.X_op = O_constant;
5944 }
5945
5946 /* A constant expression in PIC code can be handled just as it
5947 is in non PIC code. */
5948 if (mips_pic == NO_PIC
5949 || offset_expr.X_op == O_constant)
5950 {
5951 /* If this is a reference to a GP relative symbol, and there
5952 is no base register, we want
cdf6fd85 5953 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
5954 Otherwise, if there is no base register, we want
5955 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5956 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5957 If we have a constant, we need two instructions anyhow,
5958 so we always use the latter form.
5959
5960 If we have a base register, and this is a reference to a
5961 GP relative symbol, we want
5962 addu $tempreg,$breg,$gp
cdf6fd85 5963 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
5964 Otherwise we want
5965 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5966 addu $tempreg,$tempreg,$breg
5967 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245 5968 With a constant we always use the latter case.
76b3015f 5969
d6bc6245
TS
5970 With 64bit address space and no base register and $at usable,
5971 we want
5972 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5973 lui $at,<sym> (BFD_RELOC_HI16_S)
5974 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5975 dsll32 $tempreg,0
5976 daddu $tempreg,$at
5977 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5978 If we have a base register, we want
5979 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5980 lui $at,<sym> (BFD_RELOC_HI16_S)
5981 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5982 daddu $at,$breg
5983 dsll32 $tempreg,0
5984 daddu $tempreg,$at
5985 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5986
5987 Without $at we can't generate the optimal path for superscalar
5988 processors here since this would require two temporary registers.
5989 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5990 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5991 dsll $tempreg,16
5992 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5993 dsll $tempreg,16
5994 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5995 If we have a base register, we want
5996 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5997 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5998 dsll $tempreg,16
5999 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
6000 dsll $tempreg,16
6001 daddu $tempreg,$tempreg,$breg
6002 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6373ee54
CD
6003
6004 If we have 64-bit addresses, as an optimization, for
6005 addresses which are 32-bit constants (e.g. kseg0/kseg1
6006 addresses) we fall back to the 32-bit address generation
78d32a17
MR
6007 mechanism since it is more efficient. Note that due to
6008 the signed offset used by memory operations, the 32-bit
6009 range is shifted down by 32768 here. This code should
6373ee54
CD
6010 probably attempt to generate 64-bit constants more
6011 efficiently in general.
9214dd3b
AO
6012
6013 As an extension for architectures with 64-bit registers,
6014 we don't truncate 64-bit addresses given as literal
6015 constants down to 32 bits, to support existing practice
6016 in the mips64 Linux (the kernel), that compiles source
6017 files with -mabi=64, assembling them as o32 or n32 (with
6018 -Wa,-32 or -Wa,-n32). This is not beautiful, but since
6019 the whole kernel is loaded into a memory region that is
67c1ffbe 6020 addressable with sign-extended 32-bit addresses, it is
9214dd3b
AO
6021 wasteful to compute the upper 32 bits of every
6022 non-literal address, that takes more space and time.
6023 Some day this should probably be implemented as an
6024 assembler option, such that the kernel doesn't have to
6025 use such ugly hacks, even though it will still have to
6026 end up converting the binary to ELF32 for a number of
6027 platforms whose boot loaders don't support ELF64
6028 binaries. */
256ab948
TS
6029 if ((HAVE_64BIT_ADDRESSES
6030 && ! (offset_expr.X_op == O_constant
6031 && IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000)))
6032 || (HAVE_64BIT_GPRS
6033 && offset_expr.X_op == O_constant
6034 && ! IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000)))
d6bc6245 6035 {
e864ceca
RS
6036 /* ??? We don't provide a GP-relative alternative for
6037 these macros. It used not to be possible with the
6038 original relaxation code, but it could be done now. */
d6bc6245 6039
460597ba 6040 if (used_at == 0 && ! mips_opts.noat)
d6bc6245 6041 {
67c0d1eb
RS
6042 macro_build (&offset_expr, "lui", "t,u", tempreg,
6043 BFD_RELOC_MIPS_HIGHEST);
6044 macro_build (&offset_expr, "lui", "t,u", AT,
6045 BFD_RELOC_HI16_S);
6046 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
6047 tempreg, BFD_RELOC_MIPS_HIGHER);
d6bc6245 6048 if (breg != 0)
67c0d1eb
RS
6049 macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
6050 macro_build (NULL, "dsll32", "d,w,<", tempreg, tempreg, 0);
6051 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
6052 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16,
6053 tempreg);
d6bc6245
TS
6054 used_at = 1;
6055 }
6056 else
6057 {
67c0d1eb
RS
6058 macro_build (&offset_expr, "lui", "t,u", tempreg,
6059 BFD_RELOC_MIPS_HIGHEST);
6060 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
6061 tempreg, BFD_RELOC_MIPS_HIGHER);
6062 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
6063 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
6064 tempreg, BFD_RELOC_HI16_S);
6065 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
d6bc6245 6066 if (breg != 0)
67c0d1eb 6067 macro_build (NULL, "daddu", "d,v,t",
17a2f251 6068 tempreg, tempreg, breg);
67c0d1eb 6069 macro_build (&offset_expr, s, fmt, treg,
17a2f251 6070 BFD_RELOC_LO16, tempreg);
d6bc6245
TS
6071 }
6072
6073 return;
6074 }
256ab948
TS
6075
6076 if (offset_expr.X_op == O_constant
ecd13cd3 6077 && ! IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000))
5a7a0b7b 6078 as_bad (_("load/store address overflow (max 32 bits)"));
76b3015f 6079
252b5132
RH
6080 if (breg == 0)
6081 {
67c0d1eb
RS
6082 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6083 && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 6084 {
4d7206a2 6085 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
6086 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_GPREL16,
6087 mips_gp_register);
4d7206a2 6088 relax_switch ();
252b5132
RH
6089 used_at = 0;
6090 }
67c0d1eb
RS
6091 macro_build_lui (&offset_expr, tempreg);
6092 macro_build (&offset_expr, s, fmt, treg,
17a2f251 6093 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
6094 if (mips_relax.sequence)
6095 relax_end ();
252b5132
RH
6096 }
6097 else
6098 {
67c0d1eb
RS
6099 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6100 && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 6101 {
4d7206a2 6102 relax_start (offset_expr.X_add_symbol);
67c0d1eb 6103 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6104 tempreg, breg, mips_gp_register);
67c0d1eb 6105 macro_build (&offset_expr, s, fmt, treg,
17a2f251 6106 BFD_RELOC_GPREL16, tempreg);
4d7206a2 6107 relax_switch ();
252b5132 6108 }
67c0d1eb
RS
6109 macro_build_lui (&offset_expr, tempreg);
6110 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6111 tempreg, tempreg, breg);
67c0d1eb 6112 macro_build (&offset_expr, s, fmt, treg,
17a2f251 6113 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
6114 if (mips_relax.sequence)
6115 relax_end ();
252b5132
RH
6116 }
6117 }
6118 else if (mips_pic == SVR4_PIC && ! mips_big_got)
6119 {
ed6fb7bd 6120 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
f9419b05 6121
252b5132
RH
6122 /* If this is a reference to an external symbol, we want
6123 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6124 nop
6125 <op> $treg,0($tempreg)
6126 Otherwise we want
6127 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6128 nop
6129 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6130 <op> $treg,0($tempreg)
f5040a92
AO
6131
6132 For NewABI, we want
6133 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6134 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
6135
252b5132
RH
6136 If there is a base register, we add it to $tempreg before
6137 the <op>. If there is a constant, we stick it in the
6138 <op> instruction. We don't handle constants larger than
6139 16 bits, because we have no way to load the upper 16 bits
6140 (actually, we could handle them for the subset of cases
6141 in which we are not using $at). */
6142 assert (offset_expr.X_op == O_symbol);
f5040a92
AO
6143 if (HAVE_NEWABI)
6144 {
67c0d1eb
RS
6145 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6146 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 6147 if (breg != 0)
67c0d1eb 6148 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6149 tempreg, tempreg, breg);
67c0d1eb 6150 macro_build (&offset_expr, s, fmt, treg,
17a2f251 6151 BFD_RELOC_MIPS_GOT_OFST, tempreg);
f5040a92
AO
6152
6153 if (! used_at)
6154 return;
6155
6156 break;
6157 }
252b5132
RH
6158 expr1.X_add_number = offset_expr.X_add_number;
6159 offset_expr.X_add_number = 0;
6160 if (expr1.X_add_number < -0x8000
6161 || expr1.X_add_number >= 0x8000)
6162 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb
RS
6163 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6164 lw_reloc_type, mips_gp_register);
269137b2 6165 load_delay_nop ();
4d7206a2
RS
6166 relax_start (offset_expr.X_add_symbol);
6167 relax_switch ();
67c0d1eb
RS
6168 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
6169 tempreg, BFD_RELOC_LO16);
4d7206a2 6170 relax_end ();
252b5132 6171 if (breg != 0)
67c0d1eb 6172 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6173 tempreg, tempreg, breg);
67c0d1eb 6174 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
252b5132 6175 }
f5040a92 6176 else if (mips_pic == SVR4_PIC && ! HAVE_NEWABI)
252b5132 6177 {
67c0d1eb 6178 int gpdelay;
252b5132
RH
6179
6180 /* If this is a reference to an external symbol, we want
6181 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6182 addu $tempreg,$tempreg,$gp
6183 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6184 <op> $treg,0($tempreg)
6185 Otherwise we want
6186 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6187 nop
6188 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6189 <op> $treg,0($tempreg)
6190 If there is a base register, we add it to $tempreg before
6191 the <op>. If there is a constant, we stick it in the
6192 <op> instruction. We don't handle constants larger than
6193 16 bits, because we have no way to load the upper 16 bits
6194 (actually, we could handle them for the subset of cases
f5040a92 6195 in which we are not using $at). */
252b5132
RH
6196 assert (offset_expr.X_op == O_symbol);
6197 expr1.X_add_number = offset_expr.X_add_number;
6198 offset_expr.X_add_number = 0;
6199 if (expr1.X_add_number < -0x8000
6200 || expr1.X_add_number >= 0x8000)
6201 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 6202 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 6203 relax_start (offset_expr.X_add_symbol);
67c0d1eb 6204 macro_build (&offset_expr, "lui", "t,u", tempreg,
17a2f251 6205 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
6206 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
6207 mips_gp_register);
6208 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6209 BFD_RELOC_MIPS_GOT_LO16, tempreg);
4d7206a2 6210 relax_switch ();
67c0d1eb
RS
6211 if (gpdelay)
6212 macro_build (NULL, "nop", "");
6213 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6214 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 6215 load_delay_nop ();
67c0d1eb
RS
6216 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
6217 tempreg, BFD_RELOC_LO16);
4d7206a2
RS
6218 relax_end ();
6219
252b5132 6220 if (breg != 0)
67c0d1eb 6221 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6222 tempreg, tempreg, breg);
67c0d1eb 6223 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
252b5132 6224 }
f5040a92
AO
6225 else if (mips_pic == SVR4_PIC && HAVE_NEWABI)
6226 {
f5040a92
AO
6227 /* If this is a reference to an external symbol, we want
6228 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6229 add $tempreg,$tempreg,$gp
6230 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6231 <op> $treg,<ofst>($tempreg)
6232 Otherwise, for local symbols, we want:
6233 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6234 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
6235 assert (offset_expr.X_op == O_symbol);
4d7206a2 6236 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
6237 offset_expr.X_add_number = 0;
6238 if (expr1.X_add_number < -0x8000
6239 || expr1.X_add_number >= 0x8000)
6240 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4d7206a2 6241 relax_start (offset_expr.X_add_symbol);
67c0d1eb 6242 macro_build (&offset_expr, "lui", "t,u", tempreg,
17a2f251 6243 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
6244 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
6245 mips_gp_register);
6246 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6247 BFD_RELOC_MIPS_GOT_LO16, tempreg);
f5040a92 6248 if (breg != 0)
67c0d1eb 6249 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6250 tempreg, tempreg, breg);
67c0d1eb 6251 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
684022ea 6252
4d7206a2 6253 relax_switch ();
f5040a92 6254 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
6255 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6256 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 6257 if (breg != 0)
67c0d1eb 6258 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6259 tempreg, tempreg, breg);
67c0d1eb 6260 macro_build (&offset_expr, s, fmt, treg,
17a2f251 6261 BFD_RELOC_MIPS_GOT_OFST, tempreg);
4d7206a2 6262 relax_end ();
f5040a92 6263 }
252b5132
RH
6264 else if (mips_pic == EMBEDDED_PIC)
6265 {
6266 /* If there is no base register, we want
cdf6fd85 6267 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
6268 If there is a base register, we want
6269 addu $tempreg,$breg,$gp
cdf6fd85 6270 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
6271 */
6272 assert (offset_expr.X_op == O_symbol);
6273 if (breg == 0)
6274 {
67c0d1eb
RS
6275 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_GPREL16,
6276 mips_gp_register);
252b5132
RH
6277 used_at = 0;
6278 }
6279 else
6280 {
67c0d1eb 6281 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6282 tempreg, breg, mips_gp_register);
67c0d1eb 6283 macro_build (&offset_expr, s, fmt, treg,
17a2f251 6284 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6285 }
6286 }
6287 else
6288 abort ();
6289
6290 if (! used_at)
6291 return;
6292
6293 break;
6294
6295 case M_LI:
6296 case M_LI_S:
67c0d1eb 6297 load_register (treg, &imm_expr, 0);
252b5132
RH
6298 return;
6299
6300 case M_DLI:
67c0d1eb 6301 load_register (treg, &imm_expr, 1);
252b5132
RH
6302 return;
6303
6304 case M_LI_SS:
6305 if (imm_expr.X_op == O_constant)
6306 {
67c0d1eb
RS
6307 load_register (AT, &imm_expr, 0);
6308 macro_build (NULL, "mtc1", "t,G", AT, treg);
252b5132
RH
6309 break;
6310 }
6311 else
6312 {
6313 assert (offset_expr.X_op == O_symbol
6314 && strcmp (segment_name (S_GET_SEGMENT
6315 (offset_expr.X_add_symbol)),
6316 ".lit4") == 0
6317 && offset_expr.X_add_number == 0);
67c0d1eb 6318 macro_build (&offset_expr, "lwc1", "T,o(b)", treg,
17a2f251 6319 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
252b5132
RH
6320 return;
6321 }
6322
6323 case M_LI_D:
ca4e0257
RS
6324 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
6325 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
6326 order 32 bits of the value and the low order 32 bits are either
6327 zero or in OFFSET_EXPR. */
252b5132
RH
6328 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6329 {
ca4e0257 6330 if (HAVE_64BIT_GPRS)
67c0d1eb 6331 load_register (treg, &imm_expr, 1);
252b5132
RH
6332 else
6333 {
6334 int hreg, lreg;
6335
6336 if (target_big_endian)
6337 {
6338 hreg = treg;
6339 lreg = treg + 1;
6340 }
6341 else
6342 {
6343 hreg = treg + 1;
6344 lreg = treg;
6345 }
6346
6347 if (hreg <= 31)
67c0d1eb 6348 load_register (hreg, &imm_expr, 0);
252b5132
RH
6349 if (lreg <= 31)
6350 {
6351 if (offset_expr.X_op == O_absent)
67c0d1eb 6352 move_register (lreg, 0);
252b5132
RH
6353 else
6354 {
6355 assert (offset_expr.X_op == O_constant);
67c0d1eb 6356 load_register (lreg, &offset_expr, 0);
252b5132
RH
6357 }
6358 }
6359 }
6360 return;
6361 }
6362
6363 /* We know that sym is in the .rdata section. First we get the
6364 upper 16 bits of the address. */
6365 if (mips_pic == NO_PIC)
6366 {
67c0d1eb 6367 macro_build_lui (&offset_expr, AT);
252b5132
RH
6368 }
6369 else if (mips_pic == SVR4_PIC)
6370 {
67c0d1eb
RS
6371 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6372 BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132
RH
6373 }
6374 else if (mips_pic == EMBEDDED_PIC)
6375 {
6376 /* For embedded PIC we pick up the entire address off $gp in
6377 a single instruction. */
67c0d1eb
RS
6378 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", AT,
6379 mips_gp_register, BFD_RELOC_GPREL16);
252b5132
RH
6380 offset_expr.X_op = O_constant;
6381 offset_expr.X_add_number = 0;
6382 }
6383 else
6384 abort ();
bdaaa2e1 6385
252b5132 6386 /* Now we load the register(s). */
ca4e0257 6387 if (HAVE_64BIT_GPRS)
67c0d1eb 6388 macro_build (&offset_expr, "ld", "t,o(b)", treg, BFD_RELOC_LO16, AT);
252b5132
RH
6389 else
6390 {
67c0d1eb 6391 macro_build (&offset_expr, "lw", "t,o(b)", treg, BFD_RELOC_LO16, AT);
f9419b05 6392 if (treg != RA)
252b5132
RH
6393 {
6394 /* FIXME: How in the world do we deal with the possible
6395 overflow here? */
6396 offset_expr.X_add_number += 4;
67c0d1eb 6397 macro_build (&offset_expr, "lw", "t,o(b)",
17a2f251 6398 treg + 1, BFD_RELOC_LO16, AT);
252b5132
RH
6399 }
6400 }
252b5132
RH
6401 break;
6402
6403 case M_LI_DD:
ca4e0257
RS
6404 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
6405 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
6406 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
6407 the value and the low order 32 bits are either zero or in
6408 OFFSET_EXPR. */
252b5132
RH
6409 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6410 {
67c0d1eb 6411 load_register (AT, &imm_expr, HAVE_64BIT_FPRS);
ca4e0257
RS
6412 if (HAVE_64BIT_FPRS)
6413 {
6414 assert (HAVE_64BIT_GPRS);
67c0d1eb 6415 macro_build (NULL, "dmtc1", "t,S", AT, treg);
ca4e0257 6416 }
252b5132
RH
6417 else
6418 {
67c0d1eb 6419 macro_build (NULL, "mtc1", "t,G", AT, treg + 1);
252b5132 6420 if (offset_expr.X_op == O_absent)
67c0d1eb 6421 macro_build (NULL, "mtc1", "t,G", 0, treg);
252b5132
RH
6422 else
6423 {
6424 assert (offset_expr.X_op == O_constant);
67c0d1eb
RS
6425 load_register (AT, &offset_expr, 0);
6426 macro_build (NULL, "mtc1", "t,G", AT, treg);
252b5132
RH
6427 }
6428 }
6429 break;
6430 }
6431
6432 assert (offset_expr.X_op == O_symbol
6433 && offset_expr.X_add_number == 0);
6434 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
6435 if (strcmp (s, ".lit8") == 0)
6436 {
e7af610e 6437 if (mips_opts.isa != ISA_MIPS1)
252b5132 6438 {
67c0d1eb 6439 macro_build (&offset_expr, "ldc1", "T,o(b)", treg,
17a2f251 6440 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
252b5132
RH
6441 return;
6442 }
c9914766 6443 breg = mips_gp_register;
252b5132
RH
6444 r = BFD_RELOC_MIPS_LITERAL;
6445 goto dob;
6446 }
6447 else
6448 {
6449 assert (strcmp (s, RDATA_SECTION_NAME) == 0);
6450 if (mips_pic == SVR4_PIC)
67c0d1eb
RS
6451 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6452 BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132
RH
6453 else
6454 {
6455 /* FIXME: This won't work for a 64 bit address. */
67c0d1eb 6456 macro_build_lui (&offset_expr, AT);
252b5132 6457 }
bdaaa2e1 6458
e7af610e 6459 if (mips_opts.isa != ISA_MIPS1)
252b5132 6460 {
67c0d1eb
RS
6461 macro_build (&offset_expr, "ldc1", "T,o(b)",
6462 treg, BFD_RELOC_LO16, AT);
252b5132
RH
6463 break;
6464 }
6465 breg = AT;
6466 r = BFD_RELOC_LO16;
6467 goto dob;
6468 }
6469
6470 case M_L_DOB:
fef14a42 6471 if (mips_opts.arch == CPU_R4650)
252b5132
RH
6472 {
6473 as_bad (_("opcode not supported on this processor"));
6474 return;
6475 }
6476 /* Even on a big endian machine $fn comes before $fn+1. We have
6477 to adjust when loading from memory. */
6478 r = BFD_RELOC_LO16;
6479 dob:
e7af610e 6480 assert (mips_opts.isa == ISA_MIPS1);
67c0d1eb 6481 macro_build (&offset_expr, "lwc1", "T,o(b)",
17a2f251 6482 target_big_endian ? treg + 1 : treg, r, breg);
252b5132
RH
6483 /* FIXME: A possible overflow which I don't know how to deal
6484 with. */
6485 offset_expr.X_add_number += 4;
67c0d1eb 6486 macro_build (&offset_expr, "lwc1", "T,o(b)",
17a2f251 6487 target_big_endian ? treg : treg + 1, r, breg);
252b5132 6488
252b5132
RH
6489 if (breg != AT)
6490 return;
6491 break;
6492
6493 case M_L_DAB:
6494 /*
6495 * The MIPS assembler seems to check for X_add_number not
6496 * being double aligned and generating:
6497 * lui at,%hi(foo+1)
6498 * addu at,at,v1
6499 * addiu at,at,%lo(foo+1)
6500 * lwc1 f2,0(at)
6501 * lwc1 f3,4(at)
6502 * But, the resulting address is the same after relocation so why
6503 * generate the extra instruction?
6504 */
fef14a42 6505 if (mips_opts.arch == CPU_R4650)
252b5132
RH
6506 {
6507 as_bad (_("opcode not supported on this processor"));
6508 return;
6509 }
bdaaa2e1 6510 /* Itbl support may require additional care here. */
252b5132 6511 coproc = 1;
e7af610e 6512 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
6513 {
6514 s = "ldc1";
6515 goto ld;
6516 }
6517
6518 s = "lwc1";
6519 fmt = "T,o(b)";
6520 goto ldd_std;
6521
6522 case M_S_DAB:
fef14a42 6523 if (mips_opts.arch == CPU_R4650)
252b5132
RH
6524 {
6525 as_bad (_("opcode not supported on this processor"));
6526 return;
6527 }
6528
e7af610e 6529 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
6530 {
6531 s = "sdc1";
6532 goto st;
6533 }
6534
6535 s = "swc1";
6536 fmt = "T,o(b)";
bdaaa2e1 6537 /* Itbl support may require additional care here. */
252b5132
RH
6538 coproc = 1;
6539 goto ldd_std;
6540
6541 case M_LD_AB:
ca4e0257 6542 if (HAVE_64BIT_GPRS)
252b5132
RH
6543 {
6544 s = "ld";
6545 goto ld;
6546 }
6547
6548 s = "lw";
6549 fmt = "t,o(b)";
6550 goto ldd_std;
6551
6552 case M_SD_AB:
ca4e0257 6553 if (HAVE_64BIT_GPRS)
252b5132
RH
6554 {
6555 s = "sd";
6556 goto st;
6557 }
6558
6559 s = "sw";
6560 fmt = "t,o(b)";
6561
6562 ldd_std:
afdbd6d0
CD
6563 /* We do _not_ bother to allow embedded PIC (symbol-local_symbol)
6564 loads for the case of doing a pair of loads to simulate an 'ld'.
6565 This is not currently done by the compiler, and assembly coders
6566 writing embedded-pic code can cope. */
6567
252b5132
RH
6568 if (offset_expr.X_op != O_symbol
6569 && offset_expr.X_op != O_constant)
6570 {
6571 as_bad (_("expression too complex"));
6572 offset_expr.X_op = O_constant;
6573 }
6574
6575 /* Even on a big endian machine $fn comes before $fn+1. We have
6576 to adjust when loading from memory. We set coproc if we must
6577 load $fn+1 first. */
bdaaa2e1 6578 /* Itbl support may require additional care here. */
252b5132
RH
6579 if (! target_big_endian)
6580 coproc = 0;
6581
6582 if (mips_pic == NO_PIC
6583 || offset_expr.X_op == O_constant)
6584 {
6585 /* If this is a reference to a GP relative symbol, we want
cdf6fd85
TS
6586 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6587 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
6588 If we have a base register, we use this
6589 addu $at,$breg,$gp
cdf6fd85
TS
6590 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6591 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
6592 If this is not a GP relative symbol, we want
6593 lui $at,<sym> (BFD_RELOC_HI16_S)
6594 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6595 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6596 If there is a base register, we add it to $at after the
6597 lui instruction. If there is a constant, we always use
6598 the last case. */
e7d556df 6599 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
252b5132 6600 || nopic_need_relax (offset_expr.X_add_symbol, 1))
67c0d1eb 6601 used_at = 1;
252b5132
RH
6602 else
6603 {
4d7206a2 6604 relax_start (offset_expr.X_add_symbol);
252b5132
RH
6605 if (breg == 0)
6606 {
c9914766 6607 tempreg = mips_gp_register;
252b5132
RH
6608 used_at = 0;
6609 }
6610 else
6611 {
67c0d1eb 6612 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6613 AT, breg, mips_gp_register);
252b5132 6614 tempreg = AT;
252b5132
RH
6615 used_at = 1;
6616 }
6617
beae10d5 6618 /* Itbl support may require additional care here. */
67c0d1eb 6619 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
17a2f251 6620 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6621 offset_expr.X_add_number += 4;
6622
6623 /* Set mips_optimize to 2 to avoid inserting an
6624 undesired nop. */
6625 hold_mips_optimize = mips_optimize;
6626 mips_optimize = 2;
beae10d5 6627 /* Itbl support may require additional care here. */
67c0d1eb 6628 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
17a2f251 6629 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6630 mips_optimize = hold_mips_optimize;
6631
4d7206a2 6632 relax_switch ();
252b5132
RH
6633
6634 /* We just generated two relocs. When tc_gen_reloc
6635 handles this case, it will skip the first reloc and
6636 handle the second. The second reloc already has an
6637 extra addend of 4, which we added above. We must
6638 subtract it out, and then subtract another 4 to make
6639 the first reloc come out right. The second reloc
6640 will come out right because we are going to add 4 to
6641 offset_expr when we build its instruction below.
6642
6643 If we have a symbol, then we don't want to include
6644 the offset, because it will wind up being included
6645 when we generate the reloc. */
6646
6647 if (offset_expr.X_op == O_constant)
6648 offset_expr.X_add_number -= 8;
6649 else
6650 {
6651 offset_expr.X_add_number = -4;
6652 offset_expr.X_op = O_constant;
6653 }
6654 }
67c0d1eb 6655 macro_build_lui (&offset_expr, AT);
252b5132 6656 if (breg != 0)
67c0d1eb 6657 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 6658 /* Itbl support may require additional care here. */
67c0d1eb 6659 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
17a2f251 6660 BFD_RELOC_LO16, AT);
252b5132
RH
6661 /* FIXME: How do we handle overflow here? */
6662 offset_expr.X_add_number += 4;
beae10d5 6663 /* Itbl support may require additional care here. */
67c0d1eb 6664 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
17a2f251 6665 BFD_RELOC_LO16, AT);
4d7206a2
RS
6666 if (mips_relax.sequence)
6667 relax_end ();
bdaaa2e1 6668 }
252b5132
RH
6669 else if (mips_pic == SVR4_PIC && ! mips_big_got)
6670 {
252b5132
RH
6671 /* If this is a reference to an external symbol, we want
6672 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6673 nop
6674 <op> $treg,0($at)
6675 <op> $treg+1,4($at)
6676 Otherwise we want
6677 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6678 nop
6679 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6680 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6681 If there is a base register we add it to $at before the
6682 lwc1 instructions. If there is a constant we include it
6683 in the lwc1 instructions. */
6684 used_at = 1;
6685 expr1.X_add_number = offset_expr.X_add_number;
252b5132
RH
6686 if (expr1.X_add_number < -0x8000
6687 || expr1.X_add_number >= 0x8000 - 4)
6688 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 6689 load_got_offset (AT, &offset_expr);
269137b2 6690 load_delay_nop ();
252b5132 6691 if (breg != 0)
67c0d1eb 6692 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
252b5132
RH
6693
6694 /* Set mips_optimize to 2 to avoid inserting an undesired
6695 nop. */
6696 hold_mips_optimize = mips_optimize;
6697 mips_optimize = 2;
4d7206a2 6698
beae10d5 6699 /* Itbl support may require additional care here. */
4d7206a2 6700 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
6701 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
6702 BFD_RELOC_LO16, AT);
4d7206a2 6703 expr1.X_add_number += 4;
67c0d1eb
RS
6704 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
6705 BFD_RELOC_LO16, AT);
4d7206a2 6706 relax_switch ();
67c0d1eb
RS
6707 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6708 BFD_RELOC_LO16, AT);
4d7206a2 6709 offset_expr.X_add_number += 4;
67c0d1eb
RS
6710 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6711 BFD_RELOC_LO16, AT);
4d7206a2 6712 relax_end ();
252b5132 6713
4d7206a2 6714 mips_optimize = hold_mips_optimize;
252b5132
RH
6715 }
6716 else if (mips_pic == SVR4_PIC)
6717 {
67c0d1eb 6718 int gpdelay;
252b5132
RH
6719
6720 /* If this is a reference to an external symbol, we want
6721 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6722 addu $at,$at,$gp
6723 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
6724 nop
6725 <op> $treg,0($at)
6726 <op> $treg+1,4($at)
6727 Otherwise we want
6728 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6729 nop
6730 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6731 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6732 If there is a base register we add it to $at before the
6733 lwc1 instructions. If there is a constant we include it
6734 in the lwc1 instructions. */
6735 used_at = 1;
6736 expr1.X_add_number = offset_expr.X_add_number;
6737 offset_expr.X_add_number = 0;
6738 if (expr1.X_add_number < -0x8000
6739 || expr1.X_add_number >= 0x8000 - 4)
6740 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 6741 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 6742 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
6743 macro_build (&offset_expr, "lui", "t,u",
6744 AT, BFD_RELOC_MIPS_GOT_HI16);
6745 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6746 AT, AT, mips_gp_register);
67c0d1eb 6747 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 6748 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
269137b2 6749 load_delay_nop ();
252b5132 6750 if (breg != 0)
67c0d1eb 6751 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 6752 /* Itbl support may require additional care here. */
67c0d1eb 6753 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
17a2f251 6754 BFD_RELOC_LO16, AT);
252b5132
RH
6755 expr1.X_add_number += 4;
6756
6757 /* Set mips_optimize to 2 to avoid inserting an undesired
6758 nop. */
6759 hold_mips_optimize = mips_optimize;
6760 mips_optimize = 2;
beae10d5 6761 /* Itbl support may require additional care here. */
67c0d1eb 6762 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
17a2f251 6763 BFD_RELOC_LO16, AT);
252b5132
RH
6764 mips_optimize = hold_mips_optimize;
6765 expr1.X_add_number -= 4;
6766
4d7206a2
RS
6767 relax_switch ();
6768 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
6769 if (gpdelay)
6770 macro_build (NULL, "nop", "");
6771 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6772 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 6773 load_delay_nop ();
252b5132 6774 if (breg != 0)
67c0d1eb 6775 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 6776 /* Itbl support may require additional care here. */
67c0d1eb
RS
6777 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6778 BFD_RELOC_LO16, AT);
4d7206a2 6779 offset_expr.X_add_number += 4;
252b5132
RH
6780
6781 /* Set mips_optimize to 2 to avoid inserting an undesired
6782 nop. */
6783 hold_mips_optimize = mips_optimize;
6784 mips_optimize = 2;
beae10d5 6785 /* Itbl support may require additional care here. */
67c0d1eb
RS
6786 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6787 BFD_RELOC_LO16, AT);
252b5132 6788 mips_optimize = hold_mips_optimize;
4d7206a2 6789 relax_end ();
252b5132
RH
6790 }
6791 else if (mips_pic == EMBEDDED_PIC)
6792 {
6793 /* If there is no base register, we use
cdf6fd85
TS
6794 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6795 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
6796 If we have a base register, we use
6797 addu $at,$breg,$gp
cdf6fd85
TS
6798 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6799 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
6800 */
6801 if (breg == 0)
6802 {
c9914766 6803 tempreg = mips_gp_register;
252b5132
RH
6804 used_at = 0;
6805 }
6806 else
6807 {
67c0d1eb 6808 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6809 AT, breg, mips_gp_register);
252b5132
RH
6810 tempreg = AT;
6811 used_at = 1;
6812 }
6813
beae10d5 6814 /* Itbl support may require additional care here. */
67c0d1eb 6815 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
17a2f251 6816 BFD_RELOC_GPREL16, tempreg);
252b5132 6817 offset_expr.X_add_number += 4;
beae10d5 6818 /* Itbl support may require additional care here. */
67c0d1eb 6819 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
17a2f251 6820 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6821 }
6822 else
6823 abort ();
6824
6825 if (! used_at)
6826 return;
6827
6828 break;
6829
6830 case M_LD_OB:
6831 s = "lw";
6832 goto sd_ob;
6833 case M_SD_OB:
6834 s = "sw";
6835 sd_ob:
ca4e0257 6836 assert (HAVE_32BIT_ADDRESSES);
67c0d1eb 6837 macro_build (&offset_expr, s, "t,o(b)", treg, BFD_RELOC_LO16, breg);
252b5132 6838 offset_expr.X_add_number += 4;
67c0d1eb 6839 macro_build (&offset_expr, s, "t,o(b)", treg + 1, BFD_RELOC_LO16, breg);
252b5132
RH
6840 return;
6841
6842 /* New code added to support COPZ instructions.
6843 This code builds table entries out of the macros in mip_opcodes.
6844 R4000 uses interlocks to handle coproc delays.
6845 Other chips (like the R3000) require nops to be inserted for delays.
6846
f72c8c98 6847 FIXME: Currently, we require that the user handle delays.
252b5132
RH
6848 In order to fill delay slots for non-interlocked chips,
6849 we must have a way to specify delays based on the coprocessor.
6850 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
6851 What are the side-effects of the cop instruction?
6852 What cache support might we have and what are its effects?
6853 Both coprocessor & memory require delays. how long???
bdaaa2e1 6854 What registers are read/set/modified?
252b5132
RH
6855
6856 If an itbl is provided to interpret cop instructions,
bdaaa2e1 6857 this knowledge can be encoded in the itbl spec. */
252b5132
RH
6858
6859 case M_COP0:
6860 s = "c0";
6861 goto copz;
6862 case M_COP1:
6863 s = "c1";
6864 goto copz;
6865 case M_COP2:
6866 s = "c2";
6867 goto copz;
6868 case M_COP3:
6869 s = "c3";
6870 copz:
6871 /* For now we just do C (same as Cz). The parameter will be
6872 stored in insn_opcode by mips_ip. */
67c0d1eb 6873 macro_build (NULL, s, "C", ip->insn_opcode);
252b5132
RH
6874 return;
6875
ea1fb5dc 6876 case M_MOVE:
67c0d1eb 6877 move_register (dreg, sreg);
ea1fb5dc
RS
6878 return;
6879
252b5132
RH
6880#ifdef LOSING_COMPILER
6881 default:
6882 /* Try and see if this is a new itbl instruction.
6883 This code builds table entries out of the macros in mip_opcodes.
6884 FIXME: For now we just assemble the expression and pass it's
6885 value along as a 32-bit immediate.
bdaaa2e1 6886 We may want to have the assembler assemble this value,
252b5132
RH
6887 so that we gain the assembler's knowledge of delay slots,
6888 symbols, etc.
6889 Would it be more efficient to use mask (id) here? */
bdaaa2e1 6890 if (itbl_have_entries
252b5132 6891 && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
beae10d5 6892 {
252b5132
RH
6893 s = ip->insn_mo->name;
6894 s2 = "cop3";
6895 coproc = ITBL_DECODE_PNUM (immed_expr);;
67c0d1eb 6896 macro_build (&immed_expr, s, "C");
252b5132 6897 return;
beae10d5 6898 }
252b5132
RH
6899 macro2 (ip);
6900 return;
6901 }
6902 if (mips_opts.noat)
6903 as_warn (_("Macro used $at after \".set noat\""));
6904}
bdaaa2e1 6905
252b5132 6906static void
17a2f251 6907macro2 (struct mips_cl_insn *ip)
252b5132
RH
6908{
6909 register int treg, sreg, dreg, breg;
6910 int tempreg;
6911 int mask;
252b5132
RH
6912 int used_at;
6913 expressionS expr1;
6914 const char *s;
6915 const char *s2;
6916 const char *fmt;
6917 int likely = 0;
6918 int dbl = 0;
6919 int coproc = 0;
6920 int lr = 0;
6921 int imm = 0;
6922 int off;
6923 offsetT maxnum;
6924 bfd_reloc_code_real_type r;
bdaaa2e1 6925
252b5132
RH
6926 treg = (ip->insn_opcode >> 16) & 0x1f;
6927 dreg = (ip->insn_opcode >> 11) & 0x1f;
6928 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
6929 mask = ip->insn_mo->mask;
bdaaa2e1 6930
252b5132
RH
6931 expr1.X_op = O_constant;
6932 expr1.X_op_symbol = NULL;
6933 expr1.X_add_symbol = NULL;
6934 expr1.X_add_number = 1;
bdaaa2e1 6935
252b5132
RH
6936 switch (mask)
6937 {
6938#endif /* LOSING_COMPILER */
6939
6940 case M_DMUL:
6941 dbl = 1;
6942 case M_MUL:
67c0d1eb
RS
6943 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", sreg, treg);
6944 macro_build (NULL, "mflo", "d", dreg);
252b5132
RH
6945 return;
6946
6947 case M_DMUL_I:
6948 dbl = 1;
6949 case M_MUL_I:
6950 /* The MIPS assembler some times generates shifts and adds. I'm
6951 not trying to be that fancy. GCC should do this for us
6952 anyway. */
67c0d1eb
RS
6953 load_register (AT, &imm_expr, dbl);
6954 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, AT);
6955 macro_build (NULL, "mflo", "d", dreg);
252b5132
RH
6956 break;
6957
6958 case M_DMULO_I:
6959 dbl = 1;
6960 case M_MULO_I:
6961 imm = 1;
6962 goto do_mulo;
6963
6964 case M_DMULO:
6965 dbl = 1;
6966 case M_MULO:
6967 do_mulo:
b34976b6 6968 mips_emit_delays (TRUE);
252b5132
RH
6969 ++mips_opts.noreorder;
6970 mips_any_noreorder = 1;
6971 if (imm)
67c0d1eb
RS
6972 load_register (AT, &imm_expr, dbl);
6973 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
6974 macro_build (NULL, "mflo", "d", dreg);
6975 macro_build (NULL, dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, RA);
6976 macro_build (NULL, "mfhi", "d", AT);
252b5132 6977 if (mips_trap)
67c0d1eb 6978 macro_build (NULL, "tne", "s,t,q", dreg, AT, 6);
252b5132
RH
6979 else
6980 {
6981 expr1.X_add_number = 8;
67c0d1eb
RS
6982 macro_build (&expr1, "beq", "s,t,p", dreg, AT);
6983 macro_build (NULL, "nop", "", 0);
6984 macro_build (NULL, "break", "c", 6);
252b5132
RH
6985 }
6986 --mips_opts.noreorder;
67c0d1eb 6987 macro_build (NULL, "mflo", "d", dreg);
252b5132
RH
6988 break;
6989
6990 case M_DMULOU_I:
6991 dbl = 1;
6992 case M_MULOU_I:
6993 imm = 1;
6994 goto do_mulou;
6995
6996 case M_DMULOU:
6997 dbl = 1;
6998 case M_MULOU:
6999 do_mulou:
b34976b6 7000 mips_emit_delays (TRUE);
252b5132
RH
7001 ++mips_opts.noreorder;
7002 mips_any_noreorder = 1;
7003 if (imm)
67c0d1eb
RS
7004 load_register (AT, &imm_expr, dbl);
7005 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
17a2f251 7006 sreg, imm ? AT : treg);
67c0d1eb
RS
7007 macro_build (NULL, "mfhi", "d", AT);
7008 macro_build (NULL, "mflo", "d", dreg);
252b5132 7009 if (mips_trap)
67c0d1eb 7010 macro_build (NULL, "tne", "s,t,q", AT, 0, 6);
252b5132
RH
7011 else
7012 {
7013 expr1.X_add_number = 8;
67c0d1eb
RS
7014 macro_build (&expr1, "beq", "s,t,p", AT, 0);
7015 macro_build (NULL, "nop", "", 0);
7016 macro_build (NULL, "break", "c", 6);
252b5132
RH
7017 }
7018 --mips_opts.noreorder;
7019 break;
7020
771c7ce4 7021 case M_DROL:
fef14a42 7022 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097
CD
7023 {
7024 if (dreg == sreg)
7025 {
7026 tempreg = AT;
7027 used_at = 1;
7028 }
7029 else
7030 {
7031 tempreg = dreg;
7032 used_at = 0;
7033 }
67c0d1eb
RS
7034 macro_build (NULL, "dnegu", "d,w", tempreg, treg);
7035 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, tempreg);
82dd0097
CD
7036 if (used_at)
7037 break;
7038 return;
7039 }
67c0d1eb
RS
7040 macro_build (NULL, "dsubu", "d,v,t", AT, 0, treg);
7041 macro_build (NULL, "dsrlv", "d,t,s", AT, sreg, AT);
7042 macro_build (NULL, "dsllv", "d,t,s", dreg, sreg, treg);
7043 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
7044 break;
7045
252b5132 7046 case M_ROL:
fef14a42 7047 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097
CD
7048 {
7049 if (dreg == sreg)
7050 {
7051 tempreg = AT;
7052 used_at = 1;
7053 }
7054 else
7055 {
7056 tempreg = dreg;
7057 used_at = 0;
7058 }
67c0d1eb
RS
7059 macro_build (NULL, "negu", "d,w", tempreg, treg);
7060 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, tempreg);
82dd0097
CD
7061 if (used_at)
7062 break;
7063 return;
7064 }
67c0d1eb
RS
7065 macro_build (NULL, "subu", "d,v,t", AT, 0, treg);
7066 macro_build (NULL, "srlv", "d,t,s", AT, sreg, AT);
7067 macro_build (NULL, "sllv", "d,t,s", dreg, sreg, treg);
7068 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
7069 break;
7070
771c7ce4
TS
7071 case M_DROL_I:
7072 {
7073 unsigned int rot;
82dd0097 7074 char *l, *r;
771c7ce4
TS
7075
7076 if (imm_expr.X_op != O_constant)
82dd0097 7077 as_bad (_("Improper rotate count"));
771c7ce4 7078 rot = imm_expr.X_add_number & 0x3f;
fef14a42 7079 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
60b63b72
RS
7080 {
7081 rot = (64 - rot) & 0x3f;
7082 if (rot >= 32)
67c0d1eb 7083 macro_build (NULL, "dror32", "d,w,<", dreg, sreg, rot - 32);
60b63b72 7084 else
67c0d1eb 7085 macro_build (NULL, "dror", "d,w,<", dreg, sreg, rot);
82dd0097 7086 return;
60b63b72 7087 }
483fc7cd 7088 if (rot == 0)
483fc7cd 7089 {
67c0d1eb 7090 macro_build (NULL, "dsrl", "d,w,<", dreg, sreg, 0);
82dd0097 7091 return;
483fc7cd 7092 }
82dd0097
CD
7093 l = (rot < 0x20) ? "dsll" : "dsll32";
7094 r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
7095 rot &= 0x1f;
67c0d1eb
RS
7096 macro_build (NULL, l, "d,w,<", AT, sreg, rot);
7097 macro_build (NULL, r, "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7098 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
7099 }
7100 break;
7101
252b5132 7102 case M_ROL_I:
771c7ce4
TS
7103 {
7104 unsigned int rot;
7105
7106 if (imm_expr.X_op != O_constant)
82dd0097 7107 as_bad (_("Improper rotate count"));
771c7ce4 7108 rot = imm_expr.X_add_number & 0x1f;
fef14a42 7109 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
60b63b72 7110 {
67c0d1eb 7111 macro_build (NULL, "ror", "d,w,<", dreg, sreg, (32 - rot) & 0x1f);
82dd0097 7112 return;
60b63b72 7113 }
483fc7cd 7114 if (rot == 0)
483fc7cd 7115 {
67c0d1eb 7116 macro_build (NULL, "srl", "d,w,<", dreg, sreg, 0);
82dd0097 7117 return;
483fc7cd 7118 }
67c0d1eb
RS
7119 macro_build (NULL, "sll", "d,w,<", AT, sreg, rot);
7120 macro_build (NULL, "srl", "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7121 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
7122 }
7123 break;
7124
7125 case M_DROR:
fef14a42 7126 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097 7127 {
67c0d1eb 7128 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, treg);
82dd0097
CD
7129 return;
7130 }
67c0d1eb
RS
7131 macro_build (NULL, "dsubu", "d,v,t", AT, 0, treg);
7132 macro_build (NULL, "dsllv", "d,t,s", AT, sreg, AT);
7133 macro_build (NULL, "dsrlv", "d,t,s", dreg, sreg, treg);
7134 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
7135 break;
7136
7137 case M_ROR:
fef14a42 7138 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 7139 {
67c0d1eb 7140 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, treg);
82dd0097
CD
7141 return;
7142 }
67c0d1eb
RS
7143 macro_build (NULL, "subu", "d,v,t", AT, 0, treg);
7144 macro_build (NULL, "sllv", "d,t,s", AT, sreg, AT);
7145 macro_build (NULL, "srlv", "d,t,s", dreg, sreg, treg);
7146 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
7147 break;
7148
771c7ce4
TS
7149 case M_DROR_I:
7150 {
7151 unsigned int rot;
82dd0097 7152 char *l, *r;
771c7ce4
TS
7153
7154 if (imm_expr.X_op != O_constant)
82dd0097 7155 as_bad (_("Improper rotate count"));
771c7ce4 7156 rot = imm_expr.X_add_number & 0x3f;
fef14a42 7157 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097
CD
7158 {
7159 if (rot >= 32)
67c0d1eb 7160 macro_build (NULL, "dror32", "d,w,<", dreg, sreg, rot - 32);
82dd0097 7161 else
67c0d1eb 7162 macro_build (NULL, "dror", "d,w,<", dreg, sreg, rot);
82dd0097
CD
7163 return;
7164 }
483fc7cd 7165 if (rot == 0)
483fc7cd 7166 {
67c0d1eb 7167 macro_build (NULL, "dsrl", "d,w,<", dreg, sreg, 0);
82dd0097 7168 return;
483fc7cd 7169 }
82dd0097
CD
7170 r = (rot < 0x20) ? "dsrl" : "dsrl32";
7171 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
7172 rot &= 0x1f;
67c0d1eb
RS
7173 macro_build (NULL, r, "d,w,<", AT, sreg, rot);
7174 macro_build (NULL, l, "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7175 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
7176 }
7177 break;
7178
252b5132 7179 case M_ROR_I:
771c7ce4
TS
7180 {
7181 unsigned int rot;
7182
7183 if (imm_expr.X_op != O_constant)
82dd0097 7184 as_bad (_("Improper rotate count"));
771c7ce4 7185 rot = imm_expr.X_add_number & 0x1f;
fef14a42 7186 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 7187 {
67c0d1eb 7188 macro_build (NULL, "ror", "d,w,<", dreg, sreg, rot);
82dd0097
CD
7189 return;
7190 }
483fc7cd 7191 if (rot == 0)
483fc7cd 7192 {
67c0d1eb 7193 macro_build (NULL, "srl", "d,w,<", dreg, sreg, 0);
82dd0097 7194 return;
483fc7cd 7195 }
67c0d1eb
RS
7196 macro_build (NULL, "srl", "d,w,<", AT, sreg, rot);
7197 macro_build (NULL, "sll", "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7198 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4 7199 }
252b5132
RH
7200 break;
7201
7202 case M_S_DOB:
fef14a42 7203 if (mips_opts.arch == CPU_R4650)
252b5132
RH
7204 {
7205 as_bad (_("opcode not supported on this processor"));
7206 return;
7207 }
e7af610e 7208 assert (mips_opts.isa == ISA_MIPS1);
252b5132
RH
7209 /* Even on a big endian machine $fn comes before $fn+1. We have
7210 to adjust when storing to memory. */
67c0d1eb
RS
7211 macro_build (&offset_expr, "swc1", "T,o(b)",
7212 target_big_endian ? treg + 1 : treg, BFD_RELOC_LO16, breg);
252b5132 7213 offset_expr.X_add_number += 4;
67c0d1eb
RS
7214 macro_build (&offset_expr, "swc1", "T,o(b)",
7215 target_big_endian ? treg : treg + 1, BFD_RELOC_LO16, breg);
252b5132
RH
7216 return;
7217
7218 case M_SEQ:
7219 if (sreg == 0)
67c0d1eb 7220 macro_build (&expr1, "sltiu", "t,r,j", dreg, treg, BFD_RELOC_LO16);
252b5132 7221 else if (treg == 0)
67c0d1eb 7222 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7223 else
7224 {
67c0d1eb
RS
7225 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
7226 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
252b5132
RH
7227 }
7228 return;
7229
7230 case M_SEQ_I:
7231 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7232 {
67c0d1eb 7233 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7234 return;
7235 }
7236 if (sreg == 0)
7237 {
7238 as_warn (_("Instruction %s: result is always false"),
7239 ip->insn_mo->name);
67c0d1eb 7240 move_register (dreg, 0);
252b5132
RH
7241 return;
7242 }
7243 if (imm_expr.X_op == O_constant
7244 && imm_expr.X_add_number >= 0
7245 && imm_expr.X_add_number < 0x10000)
7246 {
67c0d1eb 7247 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7248 used_at = 0;
7249 }
7250 else if (imm_expr.X_op == O_constant
7251 && imm_expr.X_add_number > -0x8000
7252 && imm_expr.X_add_number < 0)
7253 {
7254 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 7255 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
17a2f251 7256 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7257 used_at = 0;
7258 }
7259 else
7260 {
67c0d1eb
RS
7261 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7262 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
252b5132
RH
7263 used_at = 1;
7264 }
67c0d1eb 7265 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
252b5132
RH
7266 if (used_at)
7267 break;
7268 return;
7269
7270 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
7271 s = "slt";
7272 goto sge;
7273 case M_SGEU:
7274 s = "sltu";
7275 sge:
67c0d1eb
RS
7276 macro_build (NULL, s, "d,v,t", dreg, sreg, treg);
7277 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
252b5132
RH
7278 return;
7279
7280 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
7281 case M_SGEU_I:
7282 if (imm_expr.X_op == O_constant
7283 && imm_expr.X_add_number >= -0x8000
7284 && imm_expr.X_add_number < 0x8000)
7285 {
67c0d1eb
RS
7286 macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
7287 dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7288 used_at = 0;
7289 }
7290 else
7291 {
67c0d1eb
RS
7292 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7293 macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
7294 dreg, sreg, AT);
252b5132
RH
7295 used_at = 1;
7296 }
67c0d1eb 7297 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
252b5132
RH
7298 if (used_at)
7299 break;
7300 return;
7301
7302 case M_SGT: /* sreg > treg <==> treg < sreg */
7303 s = "slt";
7304 goto sgt;
7305 case M_SGTU:
7306 s = "sltu";
7307 sgt:
67c0d1eb 7308 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
252b5132
RH
7309 return;
7310
7311 case M_SGT_I: /* sreg > I <==> I < sreg */
7312 s = "slt";
7313 goto sgti;
7314 case M_SGTU_I:
7315 s = "sltu";
7316 sgti:
67c0d1eb
RS
7317 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7318 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
252b5132
RH
7319 break;
7320
2396cfb9 7321 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
252b5132
RH
7322 s = "slt";
7323 goto sle;
7324 case M_SLEU:
7325 s = "sltu";
7326 sle:
67c0d1eb
RS
7327 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
7328 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
252b5132
RH
7329 return;
7330
2396cfb9 7331 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
252b5132
RH
7332 s = "slt";
7333 goto slei;
7334 case M_SLEU_I:
7335 s = "sltu";
7336 slei:
67c0d1eb
RS
7337 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7338 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
7339 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
252b5132
RH
7340 break;
7341
7342 case M_SLT_I:
7343 if (imm_expr.X_op == O_constant
7344 && imm_expr.X_add_number >= -0x8000
7345 && imm_expr.X_add_number < 0x8000)
7346 {
67c0d1eb 7347 macro_build (&imm_expr, "slti", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7348 return;
7349 }
67c0d1eb
RS
7350 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7351 macro_build (NULL, "slt", "d,v,t", dreg, sreg, AT);
252b5132
RH
7352 break;
7353
7354 case M_SLTU_I:
7355 if (imm_expr.X_op == O_constant
7356 && imm_expr.X_add_number >= -0x8000
7357 && imm_expr.X_add_number < 0x8000)
7358 {
67c0d1eb 7359 macro_build (&imm_expr, "sltiu", "t,r,j", dreg, sreg,
17a2f251 7360 BFD_RELOC_LO16);
252b5132
RH
7361 return;
7362 }
67c0d1eb
RS
7363 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7364 macro_build (NULL, "sltu", "d,v,t", dreg, sreg, AT);
252b5132
RH
7365 break;
7366
7367 case M_SNE:
7368 if (sreg == 0)
67c0d1eb 7369 macro_build (NULL, "sltu", "d,v,t", dreg, 0, treg);
252b5132 7370 else if (treg == 0)
67c0d1eb 7371 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
252b5132
RH
7372 else
7373 {
67c0d1eb
RS
7374 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
7375 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
252b5132
RH
7376 }
7377 return;
7378
7379 case M_SNE_I:
7380 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7381 {
67c0d1eb 7382 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
252b5132
RH
7383 return;
7384 }
7385 if (sreg == 0)
7386 {
7387 as_warn (_("Instruction %s: result is always true"),
7388 ip->insn_mo->name);
67c0d1eb
RS
7389 macro_build (&expr1, HAVE_32BIT_GPRS ? "addiu" : "daddiu", "t,r,j",
7390 dreg, 0, BFD_RELOC_LO16);
252b5132
RH
7391 return;
7392 }
7393 if (imm_expr.X_op == O_constant
7394 && imm_expr.X_add_number >= 0
7395 && imm_expr.X_add_number < 0x10000)
7396 {
67c0d1eb 7397 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7398 used_at = 0;
7399 }
7400 else if (imm_expr.X_op == O_constant
7401 && imm_expr.X_add_number > -0x8000
7402 && imm_expr.X_add_number < 0)
7403 {
7404 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 7405 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
17a2f251 7406 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7407 used_at = 0;
7408 }
7409 else
7410 {
67c0d1eb
RS
7411 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7412 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
252b5132
RH
7413 used_at = 1;
7414 }
67c0d1eb 7415 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
252b5132
RH
7416 if (used_at)
7417 break;
7418 return;
7419
7420 case M_DSUB_I:
7421 dbl = 1;
7422 case M_SUB_I:
7423 if (imm_expr.X_op == O_constant
7424 && imm_expr.X_add_number > -0x8000
7425 && imm_expr.X_add_number <= 0x8000)
7426 {
7427 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb
RS
7428 macro_build (&imm_expr, dbl ? "daddi" : "addi", "t,r,j",
7429 dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7430 return;
7431 }
67c0d1eb
RS
7432 load_register (AT, &imm_expr, dbl);
7433 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
252b5132
RH
7434 break;
7435
7436 case M_DSUBU_I:
7437 dbl = 1;
7438 case M_SUBU_I:
7439 if (imm_expr.X_op == O_constant
7440 && imm_expr.X_add_number > -0x8000
7441 && imm_expr.X_add_number <= 0x8000)
7442 {
7443 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb
RS
7444 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "t,r,j",
7445 dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7446 return;
7447 }
67c0d1eb
RS
7448 load_register (AT, &imm_expr, dbl);
7449 macro_build (NULL, dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
252b5132
RH
7450 break;
7451
7452 case M_TEQ_I:
7453 s = "teq";
7454 goto trap;
7455 case M_TGE_I:
7456 s = "tge";
7457 goto trap;
7458 case M_TGEU_I:
7459 s = "tgeu";
7460 goto trap;
7461 case M_TLT_I:
7462 s = "tlt";
7463 goto trap;
7464 case M_TLTU_I:
7465 s = "tltu";
7466 goto trap;
7467 case M_TNE_I:
7468 s = "tne";
7469 trap:
67c0d1eb
RS
7470 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7471 macro_build (NULL, s, "s,t", sreg, AT);
252b5132
RH
7472 break;
7473
252b5132 7474 case M_TRUNCWS:
43841e91 7475 case M_TRUNCWD:
e7af610e 7476 assert (mips_opts.isa == ISA_MIPS1);
252b5132
RH
7477 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
7478 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
7479
7480 /*
7481 * Is the double cfc1 instruction a bug in the mips assembler;
7482 * or is there a reason for it?
7483 */
b34976b6 7484 mips_emit_delays (TRUE);
252b5132
RH
7485 ++mips_opts.noreorder;
7486 mips_any_noreorder = 1;
67c0d1eb
RS
7487 macro_build (NULL, "cfc1", "t,G", treg, RA);
7488 macro_build (NULL, "cfc1", "t,G", treg, RA);
7489 macro_build (NULL, "nop", "");
252b5132 7490 expr1.X_add_number = 3;
67c0d1eb 7491 macro_build (&expr1, "ori", "t,r,i", AT, treg, BFD_RELOC_LO16);
252b5132 7492 expr1.X_add_number = 2;
67c0d1eb
RS
7493 macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
7494 macro_build (NULL, "ctc1", "t,G", AT, RA);
7495 macro_build (NULL, "nop", "");
7496 macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
7497 dreg, sreg);
7498 macro_build (NULL, "ctc1", "t,G", treg, RA);
7499 macro_build (NULL, "nop", "");
252b5132
RH
7500 --mips_opts.noreorder;
7501 break;
7502
7503 case M_ULH:
7504 s = "lb";
7505 goto ulh;
7506 case M_ULHU:
7507 s = "lbu";
7508 ulh:
7509 if (offset_expr.X_add_number >= 0x7fff)
7510 as_bad (_("operand overflow"));
252b5132 7511 if (! target_big_endian)
f9419b05 7512 ++offset_expr.X_add_number;
67c0d1eb 7513 macro_build (&offset_expr, s, "t,o(b)", AT, BFD_RELOC_LO16, breg);
252b5132 7514 if (! target_big_endian)
f9419b05 7515 --offset_expr.X_add_number;
252b5132 7516 else
f9419b05 7517 ++offset_expr.X_add_number;
67c0d1eb
RS
7518 macro_build (&offset_expr, "lbu", "t,o(b)", treg, BFD_RELOC_LO16, breg);
7519 macro_build (NULL, "sll", "d,w,<", AT, AT, 8);
7520 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
252b5132
RH
7521 break;
7522
7523 case M_ULD:
7524 s = "ldl";
7525 s2 = "ldr";
7526 off = 7;
7527 goto ulw;
7528 case M_ULW:
7529 s = "lwl";
7530 s2 = "lwr";
7531 off = 3;
7532 ulw:
7533 if (offset_expr.X_add_number >= 0x8000 - off)
7534 as_bad (_("operand overflow"));
af22f5b2
CD
7535 if (treg != breg)
7536 tempreg = treg;
7537 else
7538 tempreg = AT;
252b5132
RH
7539 if (! target_big_endian)
7540 offset_expr.X_add_number += off;
67c0d1eb 7541 macro_build (&offset_expr, s, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
252b5132
RH
7542 if (! target_big_endian)
7543 offset_expr.X_add_number -= off;
7544 else
7545 offset_expr.X_add_number += off;
67c0d1eb 7546 macro_build (&offset_expr, s2, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
af22f5b2
CD
7547
7548 /* If necessary, move the result in tempreg the final destination. */
7549 if (treg == tempreg)
7550 return;
7551 /* Protect second load's delay slot. */
017315e4 7552 load_delay_nop ();
67c0d1eb 7553 move_register (treg, tempreg);
af22f5b2 7554 break;
252b5132
RH
7555
7556 case M_ULD_A:
7557 s = "ldl";
7558 s2 = "ldr";
7559 off = 7;
7560 goto ulwa;
7561 case M_ULW_A:
7562 s = "lwl";
7563 s2 = "lwr";
7564 off = 3;
7565 ulwa:
d6bc6245 7566 used_at = 1;
67c0d1eb 7567 load_address (AT, &offset_expr, &used_at);
252b5132 7568 if (breg != 0)
67c0d1eb 7569 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
252b5132
RH
7570 if (! target_big_endian)
7571 expr1.X_add_number = off;
7572 else
7573 expr1.X_add_number = 0;
67c0d1eb 7574 macro_build (&expr1, s, "t,o(b)", treg, BFD_RELOC_LO16, AT);
252b5132
RH
7575 if (! target_big_endian)
7576 expr1.X_add_number = 0;
7577 else
7578 expr1.X_add_number = off;
67c0d1eb 7579 macro_build (&expr1, s2, "t,o(b)", treg, BFD_RELOC_LO16, AT);
252b5132
RH
7580 break;
7581
7582 case M_ULH_A:
7583 case M_ULHU_A:
d6bc6245 7584 used_at = 1;
67c0d1eb 7585 load_address (AT, &offset_expr, &used_at);
252b5132 7586 if (breg != 0)
67c0d1eb 7587 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
252b5132
RH
7588 if (target_big_endian)
7589 expr1.X_add_number = 0;
67c0d1eb 7590 macro_build (&expr1, mask == M_ULH_A ? "lb" : "lbu", "t,o(b)",
17a2f251 7591 treg, BFD_RELOC_LO16, AT);
252b5132
RH
7592 if (target_big_endian)
7593 expr1.X_add_number = 1;
7594 else
7595 expr1.X_add_number = 0;
67c0d1eb
RS
7596 macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
7597 macro_build (NULL, "sll", "d,w,<", treg, treg, 8);
7598 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
252b5132
RH
7599 break;
7600
7601 case M_USH:
7602 if (offset_expr.X_add_number >= 0x7fff)
7603 as_bad (_("operand overflow"));
7604 if (target_big_endian)
f9419b05 7605 ++offset_expr.X_add_number;
67c0d1eb
RS
7606 macro_build (&offset_expr, "sb", "t,o(b)", treg, BFD_RELOC_LO16, breg);
7607 macro_build (NULL, "srl", "d,w,<", AT, treg, 8);
252b5132 7608 if (target_big_endian)
f9419b05 7609 --offset_expr.X_add_number;
252b5132 7610 else
f9419b05 7611 ++offset_expr.X_add_number;
67c0d1eb 7612 macro_build (&offset_expr, "sb", "t,o(b)", AT, BFD_RELOC_LO16, breg);
252b5132
RH
7613 break;
7614
7615 case M_USD:
7616 s = "sdl";
7617 s2 = "sdr";
7618 off = 7;
7619 goto usw;
7620 case M_USW:
7621 s = "swl";
7622 s2 = "swr";
7623 off = 3;
7624 usw:
7625 if (offset_expr.X_add_number >= 0x8000 - off)
7626 as_bad (_("operand overflow"));
7627 if (! target_big_endian)
7628 offset_expr.X_add_number += off;
67c0d1eb 7629 macro_build (&offset_expr, s, "t,o(b)", treg, BFD_RELOC_LO16, breg);
252b5132
RH
7630 if (! target_big_endian)
7631 offset_expr.X_add_number -= off;
7632 else
7633 offset_expr.X_add_number += off;
67c0d1eb 7634 macro_build (&offset_expr, s2, "t,o(b)", treg, BFD_RELOC_LO16, breg);
252b5132
RH
7635 return;
7636
7637 case M_USD_A:
7638 s = "sdl";
7639 s2 = "sdr";
7640 off = 7;
7641 goto uswa;
7642 case M_USW_A:
7643 s = "swl";
7644 s2 = "swr";
7645 off = 3;
7646 uswa:
d6bc6245 7647 used_at = 1;
67c0d1eb 7648 load_address (AT, &offset_expr, &used_at);
252b5132 7649 if (breg != 0)
67c0d1eb 7650 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
252b5132
RH
7651 if (! target_big_endian)
7652 expr1.X_add_number = off;
7653 else
7654 expr1.X_add_number = 0;
67c0d1eb 7655 macro_build (&expr1, s, "t,o(b)", treg, BFD_RELOC_LO16, AT);
252b5132
RH
7656 if (! target_big_endian)
7657 expr1.X_add_number = 0;
7658 else
7659 expr1.X_add_number = off;
67c0d1eb 7660 macro_build (&expr1, s2, "t,o(b)", treg, BFD_RELOC_LO16, AT);
252b5132
RH
7661 break;
7662
7663 case M_USH_A:
d6bc6245 7664 used_at = 1;
67c0d1eb 7665 load_address (AT, &offset_expr, &used_at);
252b5132 7666 if (breg != 0)
67c0d1eb 7667 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
252b5132
RH
7668 if (! target_big_endian)
7669 expr1.X_add_number = 0;
67c0d1eb
RS
7670 macro_build (&expr1, "sb", "t,o(b)", treg, BFD_RELOC_LO16, AT);
7671 macro_build (NULL, "srl", "d,w,<", treg, treg, 8);
252b5132
RH
7672 if (! target_big_endian)
7673 expr1.X_add_number = 1;
7674 else
7675 expr1.X_add_number = 0;
67c0d1eb 7676 macro_build (&expr1, "sb", "t,o(b)", treg, BFD_RELOC_LO16, AT);
252b5132
RH
7677 if (! target_big_endian)
7678 expr1.X_add_number = 0;
7679 else
7680 expr1.X_add_number = 1;
67c0d1eb
RS
7681 macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
7682 macro_build (NULL, "sll", "d,w,<", treg, treg, 8);
7683 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
252b5132
RH
7684 break;
7685
7686 default:
7687 /* FIXME: Check if this is one of the itbl macros, since they
bdaaa2e1 7688 are added dynamically. */
252b5132
RH
7689 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7690 break;
7691 }
7692 if (mips_opts.noat)
7693 as_warn (_("Macro used $at after \".set noat\""));
7694}
7695
7696/* Implement macros in mips16 mode. */
7697
7698static void
17a2f251 7699mips16_macro (struct mips_cl_insn *ip)
252b5132
RH
7700{
7701 int mask;
7702 int xreg, yreg, zreg, tmp;
252b5132
RH
7703 expressionS expr1;
7704 int dbl;
7705 const char *s, *s2, *s3;
7706
7707 mask = ip->insn_mo->mask;
7708
7709 xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
7710 yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
7711 zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
7712
252b5132
RH
7713 expr1.X_op = O_constant;
7714 expr1.X_op_symbol = NULL;
7715 expr1.X_add_symbol = NULL;
7716 expr1.X_add_number = 1;
7717
7718 dbl = 0;
7719
7720 switch (mask)
7721 {
7722 default:
7723 internalError ();
7724
7725 case M_DDIV_3:
7726 dbl = 1;
7727 case M_DIV_3:
7728 s = "mflo";
7729 goto do_div3;
7730 case M_DREM_3:
7731 dbl = 1;
7732 case M_REM_3:
7733 s = "mfhi";
7734 do_div3:
b34976b6 7735 mips_emit_delays (TRUE);
252b5132
RH
7736 ++mips_opts.noreorder;
7737 mips_any_noreorder = 1;
67c0d1eb 7738 macro_build (NULL, dbl ? "ddiv" : "div", "0,x,y", xreg, yreg);
252b5132 7739 expr1.X_add_number = 2;
67c0d1eb
RS
7740 macro_build (&expr1, "bnez", "x,p", yreg);
7741 macro_build (NULL, "break", "6", 7);
bdaaa2e1 7742
252b5132
RH
7743 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7744 since that causes an overflow. We should do that as well,
7745 but I don't see how to do the comparisons without a temporary
7746 register. */
7747 --mips_opts.noreorder;
67c0d1eb 7748 macro_build (NULL, s, "x", zreg);
252b5132
RH
7749 break;
7750
7751 case M_DIVU_3:
7752 s = "divu";
7753 s2 = "mflo";
7754 goto do_divu3;
7755 case M_REMU_3:
7756 s = "divu";
7757 s2 = "mfhi";
7758 goto do_divu3;
7759 case M_DDIVU_3:
7760 s = "ddivu";
7761 s2 = "mflo";
7762 goto do_divu3;
7763 case M_DREMU_3:
7764 s = "ddivu";
7765 s2 = "mfhi";
7766 do_divu3:
b34976b6 7767 mips_emit_delays (TRUE);
252b5132
RH
7768 ++mips_opts.noreorder;
7769 mips_any_noreorder = 1;
67c0d1eb 7770 macro_build (NULL, s, "0,x,y", xreg, yreg);
252b5132 7771 expr1.X_add_number = 2;
67c0d1eb
RS
7772 macro_build (&expr1, "bnez", "x,p", yreg);
7773 macro_build (NULL, "break", "6", 7);
252b5132 7774 --mips_opts.noreorder;
67c0d1eb 7775 macro_build (NULL, s2, "x", zreg);
252b5132
RH
7776 break;
7777
7778 case M_DMUL:
7779 dbl = 1;
7780 case M_MUL:
67c0d1eb
RS
7781 macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
7782 macro_build (NULL, "mflo", "x", zreg);
252b5132
RH
7783 return;
7784
7785 case M_DSUBU_I:
7786 dbl = 1;
7787 goto do_subu;
7788 case M_SUBU_I:
7789 do_subu:
7790 if (imm_expr.X_op != O_constant)
7791 as_bad (_("Unsupported large constant"));
7792 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 7793 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
252b5132
RH
7794 break;
7795
7796 case M_SUBU_I_2:
7797 if (imm_expr.X_op != O_constant)
7798 as_bad (_("Unsupported large constant"));
7799 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 7800 macro_build (&imm_expr, "addiu", "x,k", xreg);
252b5132
RH
7801 break;
7802
7803 case M_DSUBU_I_2:
7804 if (imm_expr.X_op != O_constant)
7805 as_bad (_("Unsupported large constant"));
7806 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 7807 macro_build (&imm_expr, "daddiu", "y,j", yreg);
252b5132
RH
7808 break;
7809
7810 case M_BEQ:
7811 s = "cmp";
7812 s2 = "bteqz";
7813 goto do_branch;
7814 case M_BNE:
7815 s = "cmp";
7816 s2 = "btnez";
7817 goto do_branch;
7818 case M_BLT:
7819 s = "slt";
7820 s2 = "btnez";
7821 goto do_branch;
7822 case M_BLTU:
7823 s = "sltu";
7824 s2 = "btnez";
7825 goto do_branch;
7826 case M_BLE:
7827 s = "slt";
7828 s2 = "bteqz";
7829 goto do_reverse_branch;
7830 case M_BLEU:
7831 s = "sltu";
7832 s2 = "bteqz";
7833 goto do_reverse_branch;
7834 case M_BGE:
7835 s = "slt";
7836 s2 = "bteqz";
7837 goto do_branch;
7838 case M_BGEU:
7839 s = "sltu";
7840 s2 = "bteqz";
7841 goto do_branch;
7842 case M_BGT:
7843 s = "slt";
7844 s2 = "btnez";
7845 goto do_reverse_branch;
7846 case M_BGTU:
7847 s = "sltu";
7848 s2 = "btnez";
7849
7850 do_reverse_branch:
7851 tmp = xreg;
7852 xreg = yreg;
7853 yreg = tmp;
7854
7855 do_branch:
67c0d1eb
RS
7856 macro_build (NULL, s, "x,y", xreg, yreg);
7857 macro_build (&offset_expr, s2, "p");
252b5132
RH
7858 break;
7859
7860 case M_BEQ_I:
7861 s = "cmpi";
7862 s2 = "bteqz";
7863 s3 = "x,U";
7864 goto do_branch_i;
7865 case M_BNE_I:
7866 s = "cmpi";
7867 s2 = "btnez";
7868 s3 = "x,U";
7869 goto do_branch_i;
7870 case M_BLT_I:
7871 s = "slti";
7872 s2 = "btnez";
7873 s3 = "x,8";
7874 goto do_branch_i;
7875 case M_BLTU_I:
7876 s = "sltiu";
7877 s2 = "btnez";
7878 s3 = "x,8";
7879 goto do_branch_i;
7880 case M_BLE_I:
7881 s = "slti";
7882 s2 = "btnez";
7883 s3 = "x,8";
7884 goto do_addone_branch_i;
7885 case M_BLEU_I:
7886 s = "sltiu";
7887 s2 = "btnez";
7888 s3 = "x,8";
7889 goto do_addone_branch_i;
7890 case M_BGE_I:
7891 s = "slti";
7892 s2 = "bteqz";
7893 s3 = "x,8";
7894 goto do_branch_i;
7895 case M_BGEU_I:
7896 s = "sltiu";
7897 s2 = "bteqz";
7898 s3 = "x,8";
7899 goto do_branch_i;
7900 case M_BGT_I:
7901 s = "slti";
7902 s2 = "bteqz";
7903 s3 = "x,8";
7904 goto do_addone_branch_i;
7905 case M_BGTU_I:
7906 s = "sltiu";
7907 s2 = "bteqz";
7908 s3 = "x,8";
7909
7910 do_addone_branch_i:
7911 if (imm_expr.X_op != O_constant)
7912 as_bad (_("Unsupported large constant"));
7913 ++imm_expr.X_add_number;
7914
7915 do_branch_i:
67c0d1eb
RS
7916 macro_build (&imm_expr, s, s3, xreg);
7917 macro_build (&offset_expr, s2, "p");
252b5132
RH
7918 break;
7919
7920 case M_ABS:
7921 expr1.X_add_number = 0;
67c0d1eb 7922 macro_build (&expr1, "slti", "x,8", yreg);
252b5132 7923 if (xreg != yreg)
67c0d1eb 7924 move_register (xreg, yreg);
252b5132 7925 expr1.X_add_number = 2;
67c0d1eb
RS
7926 macro_build (&expr1, "bteqz", "p");
7927 macro_build (NULL, "neg", "x,w", xreg, xreg);
252b5132
RH
7928 }
7929}
7930
7931/* For consistency checking, verify that all bits are specified either
7932 by the match/mask part of the instruction definition, or by the
7933 operand list. */
7934static int
17a2f251 7935validate_mips_insn (const struct mips_opcode *opc)
252b5132
RH
7936{
7937 const char *p = opc->args;
7938 char c;
7939 unsigned long used_bits = opc->mask;
7940
7941 if ((used_bits & opc->match) != opc->match)
7942 {
7943 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
7944 opc->name, opc->args);
7945 return 0;
7946 }
7947#define USE_BITS(mask,shift) (used_bits |= ((mask) << (shift)))
7948 while (*p)
7949 switch (c = *p++)
7950 {
7951 case ',': break;
7952 case '(': break;
7953 case ')': break;
af7ee8bf
CD
7954 case '+':
7955 switch (c = *p++)
7956 {
7957 case 'A': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7958 case 'B': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
7959 case 'C': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
bbcc0807
CD
7960 case 'D': USE_BITS (OP_MASK_RD, OP_SH_RD);
7961 USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
5f74bc13
CD
7962 case 'E': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7963 case 'F': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
7964 case 'G': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
7965 case 'H': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
7966 case 'I': break;
af7ee8bf
CD
7967 default:
7968 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
7969 c, opc->name, opc->args);
7970 return 0;
7971 }
7972 break;
252b5132
RH
7973 case '<': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7974 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7975 case 'A': break;
4372b673 7976 case 'B': USE_BITS (OP_MASK_CODE20, OP_SH_CODE20); break;
252b5132
RH
7977 case 'C': USE_BITS (OP_MASK_COPZ, OP_SH_COPZ); break;
7978 case 'D': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
7979 case 'E': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7980 case 'F': break;
7981 case 'G': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
156c2f8b 7982 case 'H': USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
252b5132 7983 case 'I': break;
e972090a 7984 case 'J': USE_BITS (OP_MASK_CODE19, OP_SH_CODE19); break;
af7ee8bf 7985 case 'K': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
252b5132
RH
7986 case 'L': break;
7987 case 'M': USE_BITS (OP_MASK_CCC, OP_SH_CCC); break;
7988 case 'N': USE_BITS (OP_MASK_BCC, OP_SH_BCC); break;
deec1734
CD
7989 case 'O': USE_BITS (OP_MASK_ALN, OP_SH_ALN); break;
7990 case 'Q': USE_BITS (OP_MASK_VSEL, OP_SH_VSEL);
7991 USE_BITS (OP_MASK_FT, OP_SH_FT); break;
252b5132
RH
7992 case 'R': USE_BITS (OP_MASK_FR, OP_SH_FR); break;
7993 case 'S': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7994 case 'T': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7995 case 'V': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7996 case 'W': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
deec1734
CD
7997 case 'X': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
7998 case 'Y': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7999 case 'Z': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
252b5132
RH
8000 case 'a': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break;
8001 case 'b': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8002 case 'c': USE_BITS (OP_MASK_CODE, OP_SH_CODE); break;
8003 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
8004 case 'f': break;
8005 case 'h': USE_BITS (OP_MASK_PREFX, OP_SH_PREFX); break;
8006 case 'i': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
8007 case 'j': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
8008 case 'k': USE_BITS (OP_MASK_CACHE, OP_SH_CACHE); break;
8009 case 'l': break;
8010 case 'o': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
8011 case 'p': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
8012 case 'q': USE_BITS (OP_MASK_CODE2, OP_SH_CODE2); break;
8013 case 'r': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8014 case 's': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8015 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8016 case 'u': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
8017 case 'v': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8018 case 'w': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8019 case 'x': break;
8020 case 'z': break;
8021 case 'P': USE_BITS (OP_MASK_PERFREG, OP_SH_PERFREG); break;
4372b673
NC
8022 case 'U': USE_BITS (OP_MASK_RD, OP_SH_RD);
8023 USE_BITS (OP_MASK_RT, OP_SH_RT); break;
60b63b72
RS
8024 case 'e': USE_BITS (OP_MASK_VECBYTE, OP_SH_VECBYTE); break;
8025 case '%': USE_BITS (OP_MASK_VECALIGN, OP_SH_VECALIGN); break;
8026 case '[': break;
8027 case ']': break;
252b5132
RH
8028 default:
8029 as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
8030 c, opc->name, opc->args);
8031 return 0;
8032 }
8033#undef USE_BITS
8034 if (used_bits != 0xffffffff)
8035 {
8036 as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
8037 ~used_bits & 0xffffffff, opc->name, opc->args);
8038 return 0;
8039 }
8040 return 1;
8041}
8042
8043/* This routine assembles an instruction into its binary format. As a
8044 side effect, it sets one of the global variables imm_reloc or
8045 offset_reloc to the type of relocation to do if one of the operands
8046 is an address expression. */
8047
8048static void
17a2f251 8049mips_ip (char *str, struct mips_cl_insn *ip)
252b5132
RH
8050{
8051 char *s;
8052 const char *args;
43841e91 8053 char c = 0;
252b5132
RH
8054 struct mips_opcode *insn;
8055 char *argsStart;
8056 unsigned int regno;
8057 unsigned int lastregno = 0;
af7ee8bf 8058 unsigned int lastpos = 0;
071742cf 8059 unsigned int limlo, limhi;
252b5132
RH
8060 char *s_reset;
8061 char save_c = 0;
252b5132
RH
8062
8063 insn_error = NULL;
8064
8065 /* If the instruction contains a '.', we first try to match an instruction
8066 including the '.'. Then we try again without the '.'. */
8067 insn = NULL;
3882b010 8068 for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
252b5132
RH
8069 continue;
8070
8071 /* If we stopped on whitespace, then replace the whitespace with null for
8072 the call to hash_find. Save the character we replaced just in case we
8073 have to re-parse the instruction. */
3882b010 8074 if (ISSPACE (*s))
252b5132
RH
8075 {
8076 save_c = *s;
8077 *s++ = '\0';
8078 }
bdaaa2e1 8079
252b5132
RH
8080 insn = (struct mips_opcode *) hash_find (op_hash, str);
8081
8082 /* If we didn't find the instruction in the opcode table, try again, but
8083 this time with just the instruction up to, but not including the
8084 first '.'. */
8085 if (insn == NULL)
8086 {
bdaaa2e1 8087 /* Restore the character we overwrite above (if any). */
252b5132
RH
8088 if (save_c)
8089 *(--s) = save_c;
8090
8091 /* Scan up to the first '.' or whitespace. */
3882b010
L
8092 for (s = str;
8093 *s != '\0' && *s != '.' && !ISSPACE (*s);
8094 ++s)
252b5132
RH
8095 continue;
8096
8097 /* If we did not find a '.', then we can quit now. */
8098 if (*s != '.')
8099 {
8100 insn_error = "unrecognized opcode";
8101 return;
8102 }
8103
8104 /* Lookup the instruction in the hash table. */
8105 *s++ = '\0';
8106 if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
8107 {
8108 insn_error = "unrecognized opcode";
8109 return;
8110 }
252b5132
RH
8111 }
8112
8113 argsStart = s;
8114 for (;;)
8115 {
b34976b6 8116 bfd_boolean ok;
252b5132
RH
8117
8118 assert (strcmp (insn->name, str) == 0);
8119
1f25f5d3
CD
8120 if (OPCODE_IS_MEMBER (insn,
8121 (mips_opts.isa
3396de36 8122 | (file_ase_mips16 ? INSN_MIPS16 : 0)
deec1734 8123 | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
98d3f06f 8124 | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
fef14a42 8125 mips_opts.arch))
b34976b6 8126 ok = TRUE;
bdaaa2e1 8127 else
b34976b6 8128 ok = FALSE;
bdaaa2e1 8129
252b5132
RH
8130 if (insn->pinfo != INSN_MACRO)
8131 {
fef14a42 8132 if (mips_opts.arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
b34976b6 8133 ok = FALSE;
252b5132
RH
8134 }
8135
8136 if (! ok)
8137 {
8138 if (insn + 1 < &mips_opcodes[NUMOPCODES]
8139 && strcmp (insn->name, insn[1].name) == 0)
8140 {
8141 ++insn;
8142 continue;
8143 }
252b5132 8144 else
beae10d5 8145 {
268f6bed
L
8146 if (!insn_error)
8147 {
8148 static char buf[100];
fef14a42
TS
8149 sprintf (buf,
8150 _("opcode not supported on this processor: %s (%s)"),
8151 mips_cpu_info_from_arch (mips_opts.arch)->name,
8152 mips_cpu_info_from_isa (mips_opts.isa)->name);
268f6bed
L
8153 insn_error = buf;
8154 }
8155 if (save_c)
8156 *(--s) = save_c;
2bd7f1f3 8157 return;
252b5132 8158 }
252b5132
RH
8159 }
8160
8161 ip->insn_mo = insn;
8162 ip->insn_opcode = insn->match;
268f6bed 8163 insn_error = NULL;
252b5132
RH
8164 for (args = insn->args;; ++args)
8165 {
deec1734
CD
8166 int is_mdmx;
8167
ad8d3bb3 8168 s += strspn (s, " \t");
deec1734 8169 is_mdmx = 0;
252b5132
RH
8170 switch (*args)
8171 {
8172 case '\0': /* end of args */
8173 if (*s == '\0')
8174 return;
8175 break;
8176
8177 case ',':
8178 if (*s++ == *args)
8179 continue;
8180 s--;
8181 switch (*++args)
8182 {
8183 case 'r':
8184 case 'v':
38487616 8185 ip->insn_opcode |= lastregno << OP_SH_RS;
252b5132
RH
8186 continue;
8187
8188 case 'w':
38487616
TS
8189 ip->insn_opcode |= lastregno << OP_SH_RT;
8190 continue;
8191
252b5132 8192 case 'W':
38487616 8193 ip->insn_opcode |= lastregno << OP_SH_FT;
252b5132
RH
8194 continue;
8195
8196 case 'V':
38487616 8197 ip->insn_opcode |= lastregno << OP_SH_FS;
252b5132
RH
8198 continue;
8199 }
8200 break;
8201
8202 case '(':
8203 /* Handle optional base register.
8204 Either the base register is omitted or
bdaaa2e1 8205 we must have a left paren. */
252b5132
RH
8206 /* This is dependent on the next operand specifier
8207 is a base register specification. */
8208 assert (args[1] == 'b' || args[1] == '5'
8209 || args[1] == '-' || args[1] == '4');
8210 if (*s == '\0')
8211 return;
8212
8213 case ')': /* these must match exactly */
60b63b72
RS
8214 case '[':
8215 case ']':
252b5132
RH
8216 if (*s++ == *args)
8217 continue;
8218 break;
8219
af7ee8bf
CD
8220 case '+': /* Opcode extension character. */
8221 switch (*++args)
8222 {
071742cf
CD
8223 case 'A': /* ins/ext position, becomes LSB. */
8224 limlo = 0;
8225 limhi = 31;
5f74bc13
CD
8226 goto do_lsb;
8227 case 'E':
8228 limlo = 32;
8229 limhi = 63;
8230 goto do_lsb;
8231do_lsb:
071742cf
CD
8232 my_getExpression (&imm_expr, s);
8233 check_absolute_expr (ip, &imm_expr);
8234 if ((unsigned long) imm_expr.X_add_number < limlo
8235 || (unsigned long) imm_expr.X_add_number > limhi)
8236 {
8237 as_bad (_("Improper position (%lu)"),
8238 (unsigned long) imm_expr.X_add_number);
8239 imm_expr.X_add_number = limlo;
8240 }
8241 lastpos = imm_expr.X_add_number;
8242 ip->insn_opcode |= (imm_expr.X_add_number
8243 & OP_MASK_SHAMT) << OP_SH_SHAMT;
8244 imm_expr.X_op = O_absent;
8245 s = expr_end;
8246 continue;
8247
8248 case 'B': /* ins size, becomes MSB. */
8249 limlo = 1;
8250 limhi = 32;
5f74bc13
CD
8251 goto do_msb;
8252 case 'F':
8253 limlo = 33;
8254 limhi = 64;
8255 goto do_msb;
8256do_msb:
071742cf
CD
8257 my_getExpression (&imm_expr, s);
8258 check_absolute_expr (ip, &imm_expr);
8259 /* Check for negative input so that small negative numbers
8260 will not succeed incorrectly. The checks against
8261 (pos+size) transitively check "size" itself,
8262 assuming that "pos" is reasonable. */
8263 if ((long) imm_expr.X_add_number < 0
8264 || ((unsigned long) imm_expr.X_add_number
8265 + lastpos) < limlo
8266 || ((unsigned long) imm_expr.X_add_number
8267 + lastpos) > limhi)
8268 {
8269 as_bad (_("Improper insert size (%lu, position %lu)"),
8270 (unsigned long) imm_expr.X_add_number,
8271 (unsigned long) lastpos);
8272 imm_expr.X_add_number = limlo - lastpos;
8273 }
8274 ip->insn_opcode |= ((lastpos + imm_expr.X_add_number - 1)
8275 & OP_MASK_INSMSB) << OP_SH_INSMSB;
8276 imm_expr.X_op = O_absent;
8277 s = expr_end;
8278 continue;
8279
8280 case 'C': /* ext size, becomes MSBD. */
8281 limlo = 1;
8282 limhi = 32;
5f74bc13
CD
8283 goto do_msbd;
8284 case 'G':
8285 limlo = 33;
8286 limhi = 64;
8287 goto do_msbd;
8288 case 'H':
8289 limlo = 33;
8290 limhi = 64;
8291 goto do_msbd;
8292do_msbd:
071742cf
CD
8293 my_getExpression (&imm_expr, s);
8294 check_absolute_expr (ip, &imm_expr);
8295 /* Check for negative input so that small negative numbers
8296 will not succeed incorrectly. The checks against
8297 (pos+size) transitively check "size" itself,
8298 assuming that "pos" is reasonable. */
8299 if ((long) imm_expr.X_add_number < 0
8300 || ((unsigned long) imm_expr.X_add_number
8301 + lastpos) < limlo
8302 || ((unsigned long) imm_expr.X_add_number
8303 + lastpos) > limhi)
8304 {
8305 as_bad (_("Improper extract size (%lu, position %lu)"),
8306 (unsigned long) imm_expr.X_add_number,
8307 (unsigned long) lastpos);
8308 imm_expr.X_add_number = limlo - lastpos;
8309 }
8310 ip->insn_opcode |= ((imm_expr.X_add_number - 1)
8311 & OP_MASK_EXTMSBD) << OP_SH_EXTMSBD;
8312 imm_expr.X_op = O_absent;
8313 s = expr_end;
8314 continue;
af7ee8bf 8315
bbcc0807
CD
8316 case 'D':
8317 /* +D is for disassembly only; never match. */
8318 break;
8319
5f74bc13
CD
8320 case 'I':
8321 /* "+I" is like "I", except that imm2_expr is used. */
8322 my_getExpression (&imm2_expr, s);
8323 if (imm2_expr.X_op != O_big
8324 && imm2_expr.X_op != O_constant)
8325 insn_error = _("absolute expression required");
13757d0c 8326 normalize_constant_expr (&imm2_expr);
5f74bc13
CD
8327 s = expr_end;
8328 continue;
8329
af7ee8bf
CD
8330 default:
8331 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8332 *args, insn->name, insn->args);
8333 /* Further processing is fruitless. */
8334 return;
8335 }
8336 break;
8337
252b5132
RH
8338 case '<': /* must be at least one digit */
8339 /*
8340 * According to the manual, if the shift amount is greater
b6ff326e
KH
8341 * than 31 or less than 0, then the shift amount should be
8342 * mod 32. In reality the mips assembler issues an error.
252b5132
RH
8343 * We issue a warning and mask out all but the low 5 bits.
8344 */
8345 my_getExpression (&imm_expr, s);
8346 check_absolute_expr (ip, &imm_expr);
8347 if ((unsigned long) imm_expr.X_add_number > 31)
8348 {
793b27f4
TS
8349 as_warn (_("Improper shift amount (%lu)"),
8350 (unsigned long) imm_expr.X_add_number);
38487616 8351 imm_expr.X_add_number &= OP_MASK_SHAMT;
252b5132 8352 }
38487616 8353 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SHAMT;
252b5132
RH
8354 imm_expr.X_op = O_absent;
8355 s = expr_end;
8356 continue;
8357
8358 case '>': /* shift amount minus 32 */
8359 my_getExpression (&imm_expr, s);
8360 check_absolute_expr (ip, &imm_expr);
8361 if ((unsigned long) imm_expr.X_add_number < 32
8362 || (unsigned long) imm_expr.X_add_number > 63)
8363 break;
38487616 8364 ip->insn_opcode |= (imm_expr.X_add_number - 32) << OP_SH_SHAMT;
252b5132
RH
8365 imm_expr.X_op = O_absent;
8366 s = expr_end;
8367 continue;
8368
252b5132
RH
8369 case 'k': /* cache code */
8370 case 'h': /* prefx code */
8371 my_getExpression (&imm_expr, s);
8372 check_absolute_expr (ip, &imm_expr);
8373 if ((unsigned long) imm_expr.X_add_number > 31)
8374 {
8375 as_warn (_("Invalid value for `%s' (%lu)"),
8376 ip->insn_mo->name,
8377 (unsigned long) imm_expr.X_add_number);
8378 imm_expr.X_add_number &= 0x1f;
8379 }
8380 if (*args == 'k')
8381 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
8382 else
8383 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
8384 imm_expr.X_op = O_absent;
8385 s = expr_end;
8386 continue;
8387
8388 case 'c': /* break code */
8389 my_getExpression (&imm_expr, s);
8390 check_absolute_expr (ip, &imm_expr);
793b27f4 8391 if ((unsigned long) imm_expr.X_add_number > 1023)
252b5132 8392 {
793b27f4
TS
8393 as_warn (_("Illegal break code (%lu)"),
8394 (unsigned long) imm_expr.X_add_number);
38487616 8395 imm_expr.X_add_number &= OP_MASK_CODE;
252b5132 8396 }
38487616 8397 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE;
252b5132
RH
8398 imm_expr.X_op = O_absent;
8399 s = expr_end;
8400 continue;
8401
8402 case 'q': /* lower break code */
8403 my_getExpression (&imm_expr, s);
8404 check_absolute_expr (ip, &imm_expr);
793b27f4 8405 if ((unsigned long) imm_expr.X_add_number > 1023)
252b5132 8406 {
793b27f4
TS
8407 as_warn (_("Illegal lower break code (%lu)"),
8408 (unsigned long) imm_expr.X_add_number);
38487616 8409 imm_expr.X_add_number &= OP_MASK_CODE2;
252b5132 8410 }
38487616 8411 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE2;
252b5132
RH
8412 imm_expr.X_op = O_absent;
8413 s = expr_end;
8414 continue;
8415
4372b673 8416 case 'B': /* 20-bit syscall/break code. */
156c2f8b 8417 my_getExpression (&imm_expr, s);
156c2f8b 8418 check_absolute_expr (ip, &imm_expr);
793b27f4
TS
8419 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
8420 as_warn (_("Illegal 20-bit code (%lu)"),
8421 (unsigned long) imm_expr.X_add_number);
38487616 8422 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE20;
252b5132
RH
8423 imm_expr.X_op = O_absent;
8424 s = expr_end;
8425 continue;
8426
98d3f06f 8427 case 'C': /* Coprocessor code */
beae10d5 8428 my_getExpression (&imm_expr, s);
252b5132 8429 check_absolute_expr (ip, &imm_expr);
98d3f06f 8430 if ((unsigned long) imm_expr.X_add_number >= (1 << 25))
252b5132 8431 {
793b27f4
TS
8432 as_warn (_("Coproccesor code > 25 bits (%lu)"),
8433 (unsigned long) imm_expr.X_add_number);
98d3f06f 8434 imm_expr.X_add_number &= ((1 << 25) - 1);
252b5132 8435 }
beae10d5
KH
8436 ip->insn_opcode |= imm_expr.X_add_number;
8437 imm_expr.X_op = O_absent;
8438 s = expr_end;
8439 continue;
252b5132 8440
4372b673
NC
8441 case 'J': /* 19-bit wait code. */
8442 my_getExpression (&imm_expr, s);
8443 check_absolute_expr (ip, &imm_expr);
793b27f4
TS
8444 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
8445 as_warn (_("Illegal 19-bit code (%lu)"),
8446 (unsigned long) imm_expr.X_add_number);
38487616 8447 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE19;
4372b673
NC
8448 imm_expr.X_op = O_absent;
8449 s = expr_end;
8450 continue;
8451
252b5132 8452 case 'P': /* Performance register */
beae10d5 8453 my_getExpression (&imm_expr, s);
252b5132 8454 check_absolute_expr (ip, &imm_expr);
beae10d5 8455 if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
252b5132 8456 {
793b27f4
TS
8457 as_warn (_("Invalid performance register (%lu)"),
8458 (unsigned long) imm_expr.X_add_number);
38487616 8459 imm_expr.X_add_number &= OP_MASK_PERFREG;
252b5132 8460 }
38487616 8461 ip->insn_opcode |= (imm_expr.X_add_number << OP_SH_PERFREG);
beae10d5
KH
8462 imm_expr.X_op = O_absent;
8463 s = expr_end;
8464 continue;
252b5132
RH
8465
8466 case 'b': /* base register */
8467 case 'd': /* destination register */
8468 case 's': /* source register */
8469 case 't': /* target register */
8470 case 'r': /* both target and source */
8471 case 'v': /* both dest and source */
8472 case 'w': /* both dest and target */
8473 case 'E': /* coprocessor target register */
8474 case 'G': /* coprocessor destination register */
af7ee8bf 8475 case 'K': /* 'rdhwr' destination register */
252b5132
RH
8476 case 'x': /* ignore register name */
8477 case 'z': /* must be zero register */
4372b673 8478 case 'U': /* destination register (clo/clz). */
252b5132
RH
8479 s_reset = s;
8480 if (s[0] == '$')
8481 {
8482
3882b010 8483 if (ISDIGIT (s[1]))
252b5132
RH
8484 {
8485 ++s;
8486 regno = 0;
8487 do
8488 {
8489 regno *= 10;
8490 regno += *s - '0';
8491 ++s;
8492 }
3882b010 8493 while (ISDIGIT (*s));
252b5132
RH
8494 if (regno > 31)
8495 as_bad (_("Invalid register number (%d)"), regno);
8496 }
af7ee8bf 8497 else if (*args == 'E' || *args == 'G' || *args == 'K')
252b5132
RH
8498 goto notreg;
8499 else
8500 {
76db943d
TS
8501 if (s[1] == 'r' && s[2] == 'a')
8502 {
8503 s += 3;
8504 regno = RA;
8505 }
8506 else if (s[1] == 'f' && s[2] == 'p')
252b5132
RH
8507 {
8508 s += 3;
8509 regno = FP;
8510 }
8511 else if (s[1] == 's' && s[2] == 'p')
8512 {
8513 s += 3;
8514 regno = SP;
8515 }
8516 else if (s[1] == 'g' && s[2] == 'p')
8517 {
8518 s += 3;
8519 regno = GP;
8520 }
8521 else if (s[1] == 'a' && s[2] == 't')
8522 {
8523 s += 3;
8524 regno = AT;
8525 }
8526 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8527 {
8528 s += 4;
8529 regno = KT0;
8530 }
8531 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8532 {
8533 s += 4;
8534 regno = KT1;
8535 }
85b51719
TS
8536 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
8537 {
8538 s += 5;
8539 regno = ZERO;
8540 }
252b5132
RH
8541 else if (itbl_have_entries)
8542 {
8543 char *p, *n;
d7ba4a77 8544 unsigned long r;
252b5132 8545
d7ba4a77 8546 p = s + 1; /* advance past '$' */
252b5132
RH
8547 n = itbl_get_field (&p); /* n is name */
8548
d7ba4a77
ILT
8549 /* See if this is a register defined in an
8550 itbl entry. */
8551 if (itbl_get_reg_val (n, &r))
252b5132
RH
8552 {
8553 /* Get_field advances to the start of
8554 the next field, so we need to back
d7ba4a77 8555 rack to the end of the last field. */
bdaaa2e1 8556 if (p)
252b5132 8557 s = p - 1;
bdaaa2e1 8558 else
d7ba4a77 8559 s = strchr (s, '\0');
252b5132
RH
8560 regno = r;
8561 }
8562 else
8563 goto notreg;
beae10d5 8564 }
252b5132
RH
8565 else
8566 goto notreg;
8567 }
8568 if (regno == AT
8569 && ! mips_opts.noat
8570 && *args != 'E'
af7ee8bf
CD
8571 && *args != 'G'
8572 && *args != 'K')
252b5132
RH
8573 as_warn (_("Used $at without \".set noat\""));
8574 c = *args;
8575 if (*s == ' ')
f9419b05 8576 ++s;
252b5132
RH
8577 if (args[1] != *s)
8578 {
8579 if (c == 'r' || c == 'v' || c == 'w')
8580 {
8581 regno = lastregno;
8582 s = s_reset;
f9419b05 8583 ++args;
252b5132
RH
8584 }
8585 }
8586 /* 'z' only matches $0. */
8587 if (c == 'z' && regno != 0)
8588 break;
8589
bdaaa2e1
KH
8590 /* Now that we have assembled one operand, we use the args string
8591 * to figure out where it goes in the instruction. */
252b5132
RH
8592 switch (c)
8593 {
8594 case 'r':
8595 case 's':
8596 case 'v':
8597 case 'b':
38487616 8598 ip->insn_opcode |= regno << OP_SH_RS;
252b5132
RH
8599 break;
8600 case 'd':
8601 case 'G':
af7ee8bf 8602 case 'K':
38487616 8603 ip->insn_opcode |= regno << OP_SH_RD;
252b5132 8604 break;
4372b673 8605 case 'U':
38487616
TS
8606 ip->insn_opcode |= regno << OP_SH_RD;
8607 ip->insn_opcode |= regno << OP_SH_RT;
4372b673 8608 break;
252b5132
RH
8609 case 'w':
8610 case 't':
8611 case 'E':
38487616 8612 ip->insn_opcode |= regno << OP_SH_RT;
252b5132
RH
8613 break;
8614 case 'x':
8615 /* This case exists because on the r3000 trunc
8616 expands into a macro which requires a gp
8617 register. On the r6000 or r4000 it is
8618 assembled into a single instruction which
8619 ignores the register. Thus the insn version
8620 is MIPS_ISA2 and uses 'x', and the macro
8621 version is MIPS_ISA1 and uses 't'. */
8622 break;
8623 case 'z':
8624 /* This case is for the div instruction, which
8625 acts differently if the destination argument
8626 is $0. This only matches $0, and is checked
8627 outside the switch. */
8628 break;
8629 case 'D':
8630 /* Itbl operand; not yet implemented. FIXME ?? */
8631 break;
8632 /* What about all other operands like 'i', which
8633 can be specified in the opcode table? */
8634 }
8635 lastregno = regno;
8636 continue;
8637 }
8638 notreg:
8639 switch (*args++)
8640 {
8641 case 'r':
8642 case 'v':
38487616 8643 ip->insn_opcode |= lastregno << OP_SH_RS;
252b5132
RH
8644 continue;
8645 case 'w':
38487616 8646 ip->insn_opcode |= lastregno << OP_SH_RT;
252b5132
RH
8647 continue;
8648 }
8649 break;
8650
deec1734
CD
8651 case 'O': /* MDMX alignment immediate constant. */
8652 my_getExpression (&imm_expr, s);
8653 check_absolute_expr (ip, &imm_expr);
8654 if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
8655 {
8656 as_warn ("Improper align amount (%ld), using low bits",
8657 (long) imm_expr.X_add_number);
8658 imm_expr.X_add_number &= OP_MASK_ALN;
8659 }
8660 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_ALN;
8661 imm_expr.X_op = O_absent;
8662 s = expr_end;
8663 continue;
8664
8665 case 'Q': /* MDMX vector, element sel, or const. */
8666 if (s[0] != '$')
8667 {
8668 /* MDMX Immediate. */
8669 my_getExpression (&imm_expr, s);
8670 check_absolute_expr (ip, &imm_expr);
8671 if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
8672 {
8673 as_warn (_("Invalid MDMX Immediate (%ld)"),
8674 (long) imm_expr.X_add_number);
8675 imm_expr.X_add_number &= OP_MASK_FT;
8676 }
8677 imm_expr.X_add_number &= OP_MASK_FT;
8678 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8679 ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
8680 else
8681 ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
8682 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_FT;
8683 imm_expr.X_op = O_absent;
8684 s = expr_end;
8685 continue;
8686 }
8687 /* Not MDMX Immediate. Fall through. */
8688 case 'X': /* MDMX destination register. */
8689 case 'Y': /* MDMX source register. */
8690 case 'Z': /* MDMX target register. */
8691 is_mdmx = 1;
252b5132
RH
8692 case 'D': /* floating point destination register */
8693 case 'S': /* floating point source register */
8694 case 'T': /* floating point target register */
8695 case 'R': /* floating point source register */
8696 case 'V':
8697 case 'W':
8698 s_reset = s;
deec1734
CD
8699 /* Accept $fN for FP and MDMX register numbers, and in
8700 addition accept $vN for MDMX register numbers. */
8701 if ((s[0] == '$' && s[1] == 'f' && ISDIGIT (s[2]))
8702 || (is_mdmx != 0 && s[0] == '$' && s[1] == 'v'
8703 && ISDIGIT (s[2])))
252b5132
RH
8704 {
8705 s += 2;
8706 regno = 0;
8707 do
8708 {
8709 regno *= 10;
8710 regno += *s - '0';
8711 ++s;
8712 }
3882b010 8713 while (ISDIGIT (*s));
252b5132
RH
8714
8715 if (regno > 31)
8716 as_bad (_("Invalid float register number (%d)"), regno);
8717
8718 if ((regno & 1) != 0
ca4e0257 8719 && HAVE_32BIT_FPRS
252b5132
RH
8720 && ! (strcmp (str, "mtc1") == 0
8721 || strcmp (str, "mfc1") == 0
8722 || strcmp (str, "lwc1") == 0
8723 || strcmp (str, "swc1") == 0
8724 || strcmp (str, "l.s") == 0
8725 || strcmp (str, "s.s") == 0))
8726 as_warn (_("Float register should be even, was %d"),
8727 regno);
8728
8729 c = *args;
8730 if (*s == ' ')
f9419b05 8731 ++s;
252b5132
RH
8732 if (args[1] != *s)
8733 {
8734 if (c == 'V' || c == 'W')
8735 {
8736 regno = lastregno;
8737 s = s_reset;
f9419b05 8738 ++args;
252b5132
RH
8739 }
8740 }
8741 switch (c)
8742 {
8743 case 'D':
deec1734 8744 case 'X':
38487616 8745 ip->insn_opcode |= regno << OP_SH_FD;
252b5132
RH
8746 break;
8747 case 'V':
8748 case 'S':
deec1734 8749 case 'Y':
38487616 8750 ip->insn_opcode |= regno << OP_SH_FS;
252b5132 8751 break;
deec1734
CD
8752 case 'Q':
8753 /* This is like 'Z', but also needs to fix the MDMX
8754 vector/scalar select bits. Note that the
8755 scalar immediate case is handled above. */
8756 if (*s == '[')
8757 {
8758 int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
8759 int max_el = (is_qh ? 3 : 7);
8760 s++;
8761 my_getExpression(&imm_expr, s);
8762 check_absolute_expr (ip, &imm_expr);
8763 s = expr_end;
8764 if (imm_expr.X_add_number > max_el)
8765 as_bad(_("Bad element selector %ld"),
8766 (long) imm_expr.X_add_number);
8767 imm_expr.X_add_number &= max_el;
8768 ip->insn_opcode |= (imm_expr.X_add_number
8769 << (OP_SH_VSEL +
8770 (is_qh ? 2 : 1)));
8771 if (*s != ']')
8772 as_warn(_("Expecting ']' found '%s'"), s);
8773 else
8774 s++;
8775 }
8776 else
8777 {
8778 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8779 ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
8780 << OP_SH_VSEL);
8781 else
8782 ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
8783 OP_SH_VSEL);
8784 }
8785 /* Fall through */
252b5132
RH
8786 case 'W':
8787 case 'T':
deec1734 8788 case 'Z':
38487616 8789 ip->insn_opcode |= regno << OP_SH_FT;
252b5132
RH
8790 break;
8791 case 'R':
38487616 8792 ip->insn_opcode |= regno << OP_SH_FR;
252b5132
RH
8793 break;
8794 }
8795 lastregno = regno;
8796 continue;
8797 }
8798
252b5132
RH
8799 switch (*args++)
8800 {
8801 case 'V':
38487616 8802 ip->insn_opcode |= lastregno << OP_SH_FS;
252b5132
RH
8803 continue;
8804 case 'W':
38487616 8805 ip->insn_opcode |= lastregno << OP_SH_FT;
252b5132
RH
8806 continue;
8807 }
8808 break;
8809
8810 case 'I':
8811 my_getExpression (&imm_expr, s);
8812 if (imm_expr.X_op != O_big
8813 && imm_expr.X_op != O_constant)
8814 insn_error = _("absolute expression required");
13757d0c 8815 normalize_constant_expr (&imm_expr);
252b5132
RH
8816 s = expr_end;
8817 continue;
8818
8819 case 'A':
8820 my_getExpression (&offset_expr, s);
f6688943 8821 *imm_reloc = BFD_RELOC_32;
252b5132
RH
8822 s = expr_end;
8823 continue;
8824
8825 case 'F':
8826 case 'L':
8827 case 'f':
8828 case 'l':
8829 {
8830 int f64;
ca4e0257 8831 int using_gprs;
252b5132
RH
8832 char *save_in;
8833 char *err;
8834 unsigned char temp[8];
8835 int len;
8836 unsigned int length;
8837 segT seg;
8838 subsegT subseg;
8839 char *p;
8840
8841 /* These only appear as the last operand in an
8842 instruction, and every instruction that accepts
8843 them in any variant accepts them in all variants.
8844 This means we don't have to worry about backing out
8845 any changes if the instruction does not match.
8846
8847 The difference between them is the size of the
8848 floating point constant and where it goes. For 'F'
8849 and 'L' the constant is 64 bits; for 'f' and 'l' it
8850 is 32 bits. Where the constant is placed is based
8851 on how the MIPS assembler does things:
8852 F -- .rdata
8853 L -- .lit8
8854 f -- immediate value
8855 l -- .lit4
8856
8857 The .lit4 and .lit8 sections are only used if
8858 permitted by the -G argument.
8859
8860 When generating embedded PIC code, we use the
8861 .lit8 section but not the .lit4 section (we can do
8862 .lit4 inline easily; we need to put .lit8
8863 somewhere in the data segment, and using .lit8
8864 permits the linker to eventually combine identical
ca4e0257
RS
8865 .lit8 entries).
8866
8867 The code below needs to know whether the target register
8868 is 32 or 64 bits wide. It relies on the fact 'f' and
8869 'F' are used with GPR-based instructions and 'l' and
8870 'L' are used with FPR-based instructions. */
252b5132
RH
8871
8872 f64 = *args == 'F' || *args == 'L';
ca4e0257 8873 using_gprs = *args == 'F' || *args == 'f';
252b5132
RH
8874
8875 save_in = input_line_pointer;
8876 input_line_pointer = s;
8877 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
8878 length = len;
8879 s = input_line_pointer;
8880 input_line_pointer = save_in;
8881 if (err != NULL && *err != '\0')
8882 {
8883 as_bad (_("Bad floating point constant: %s"), err);
8884 memset (temp, '\0', sizeof temp);
8885 length = f64 ? 8 : 4;
8886 }
8887
156c2f8b 8888 assert (length == (unsigned) (f64 ? 8 : 4));
252b5132
RH
8889
8890 if (*args == 'f'
8891 || (*args == 'l'
4d0d148d 8892 && (mips_pic == EMBEDDED_PIC
252b5132
RH
8893 || g_switch_value < 4
8894 || (temp[0] == 0 && temp[1] == 0)
8895 || (temp[2] == 0 && temp[3] == 0))))
8896 {
8897 imm_expr.X_op = O_constant;
8898 if (! target_big_endian)
8899 imm_expr.X_add_number = bfd_getl32 (temp);
8900 else
8901 imm_expr.X_add_number = bfd_getb32 (temp);
8902 }
8903 else if (length > 4
119d663a 8904 && ! mips_disable_float_construction
ca4e0257
RS
8905 /* Constants can only be constructed in GPRs and
8906 copied to FPRs if the GPRs are at least as wide
8907 as the FPRs. Force the constant into memory if
8908 we are using 64-bit FPRs but the GPRs are only
8909 32 bits wide. */
8910 && (using_gprs
8911 || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
252b5132
RH
8912 && ((temp[0] == 0 && temp[1] == 0)
8913 || (temp[2] == 0 && temp[3] == 0))
8914 && ((temp[4] == 0 && temp[5] == 0)
8915 || (temp[6] == 0 && temp[7] == 0)))
8916 {
ca4e0257
RS
8917 /* The value is simple enough to load with a couple of
8918 instructions. If using 32-bit registers, set
8919 imm_expr to the high order 32 bits and offset_expr to
8920 the low order 32 bits. Otherwise, set imm_expr to
8921 the entire 64 bit constant. */
8922 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
252b5132
RH
8923 {
8924 imm_expr.X_op = O_constant;
8925 offset_expr.X_op = O_constant;
8926 if (! target_big_endian)
8927 {
8928 imm_expr.X_add_number = bfd_getl32 (temp + 4);
8929 offset_expr.X_add_number = bfd_getl32 (temp);
8930 }
8931 else
8932 {
8933 imm_expr.X_add_number = bfd_getb32 (temp);
8934 offset_expr.X_add_number = bfd_getb32 (temp + 4);
8935 }
8936 if (offset_expr.X_add_number == 0)
8937 offset_expr.X_op = O_absent;
8938 }
8939 else if (sizeof (imm_expr.X_add_number) > 4)
8940 {
8941 imm_expr.X_op = O_constant;
8942 if (! target_big_endian)
8943 imm_expr.X_add_number = bfd_getl64 (temp);
8944 else
8945 imm_expr.X_add_number = bfd_getb64 (temp);
8946 }
8947 else
8948 {
8949 imm_expr.X_op = O_big;
8950 imm_expr.X_add_number = 4;
8951 if (! target_big_endian)
8952 {
8953 generic_bignum[0] = bfd_getl16 (temp);
8954 generic_bignum[1] = bfd_getl16 (temp + 2);
8955 generic_bignum[2] = bfd_getl16 (temp + 4);
8956 generic_bignum[3] = bfd_getl16 (temp + 6);
8957 }
8958 else
8959 {
8960 generic_bignum[0] = bfd_getb16 (temp + 6);
8961 generic_bignum[1] = bfd_getb16 (temp + 4);
8962 generic_bignum[2] = bfd_getb16 (temp + 2);
8963 generic_bignum[3] = bfd_getb16 (temp);
8964 }
8965 }
8966 }
8967 else
8968 {
8969 const char *newname;
8970 segT new_seg;
8971
8972 /* Switch to the right section. */
8973 seg = now_seg;
8974 subseg = now_subseg;
8975 switch (*args)
8976 {
8977 default: /* unused default case avoids warnings. */
8978 case 'L':
8979 newname = RDATA_SECTION_NAME;
4d0d148d 8980 if ((g_switch_value >= 8)
bb2d6cd7 8981 || mips_pic == EMBEDDED_PIC)
252b5132
RH
8982 newname = ".lit8";
8983 break;
8984 case 'F':
bb2d6cd7
GK
8985 if (mips_pic == EMBEDDED_PIC)
8986 newname = ".lit8";
8987 else
8988 newname = RDATA_SECTION_NAME;
252b5132
RH
8989 break;
8990 case 'l':
4d0d148d 8991 assert (g_switch_value >= 4);
252b5132
RH
8992 newname = ".lit4";
8993 break;
8994 }
8995 new_seg = subseg_new (newname, (subsegT) 0);
8996 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
8997 bfd_set_section_flags (stdoutput, new_seg,
8998 (SEC_ALLOC
8999 | SEC_LOAD
9000 | SEC_READONLY
9001 | SEC_DATA));
9002 frag_align (*args == 'l' ? 2 : 3, 0, 0);
9003 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
9004 && strcmp (TARGET_OS, "elf") != 0)
9005 record_alignment (new_seg, 4);
9006 else
9007 record_alignment (new_seg, *args == 'l' ? 2 : 3);
9008 if (seg == now_seg)
9009 as_bad (_("Can't use floating point insn in this section"));
9010
9011 /* Set the argument to the current address in the
9012 section. */
9013 offset_expr.X_op = O_symbol;
9014 offset_expr.X_add_symbol =
9015 symbol_new ("L0\001", now_seg,
9016 (valueT) frag_now_fix (), frag_now);
9017 offset_expr.X_add_number = 0;
9018
9019 /* Put the floating point number into the section. */
9020 p = frag_more ((int) length);
9021 memcpy (p, temp, length);
9022
9023 /* Switch back to the original section. */
9024 subseg_set (seg, subseg);
9025 }
9026 }
9027 continue;
9028
9029 case 'i': /* 16 bit unsigned immediate */
9030 case 'j': /* 16 bit signed immediate */
f6688943 9031 *imm_reloc = BFD_RELOC_LO16;
5e0116d5 9032 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0)
252b5132
RH
9033 {
9034 int more;
5e0116d5
RS
9035 offsetT minval, maxval;
9036
9037 more = (insn + 1 < &mips_opcodes[NUMOPCODES]
9038 && strcmp (insn->name, insn[1].name) == 0);
9039
9040 /* If the expression was written as an unsigned number,
9041 only treat it as signed if there are no more
9042 alternatives. */
9043 if (more
9044 && *args == 'j'
9045 && sizeof (imm_expr.X_add_number) <= 4
9046 && imm_expr.X_op == O_constant
9047 && imm_expr.X_add_number < 0
9048 && imm_expr.X_unsigned
9049 && HAVE_64BIT_GPRS)
9050 break;
9051
9052 /* For compatibility with older assemblers, we accept
9053 0x8000-0xffff as signed 16-bit numbers when only
9054 signed numbers are allowed. */
9055 if (*args == 'i')
9056 minval = 0, maxval = 0xffff;
9057 else if (more)
9058 minval = -0x8000, maxval = 0x7fff;
252b5132 9059 else
5e0116d5
RS
9060 minval = -0x8000, maxval = 0xffff;
9061
9062 if (imm_expr.X_op != O_constant
9063 || imm_expr.X_add_number < minval
9064 || imm_expr.X_add_number > maxval)
252b5132
RH
9065 {
9066 if (more)
9067 break;
2ae7e77b
AH
9068 if (imm_expr.X_op == O_constant
9069 || imm_expr.X_op == O_big)
5e0116d5 9070 as_bad (_("expression out of range"));
252b5132
RH
9071 }
9072 }
9073 s = expr_end;
9074 continue;
9075
9076 case 'o': /* 16 bit offset */
5e0116d5
RS
9077 /* Check whether there is only a single bracketed expression
9078 left. If so, it must be the base register and the
9079 constant must be zero. */
9080 if (*s == '(' && strchr (s + 1, '(') == 0)
9081 {
9082 offset_expr.X_op = O_constant;
9083 offset_expr.X_add_number = 0;
9084 continue;
9085 }
252b5132
RH
9086
9087 /* If this value won't fit into a 16 bit offset, then go
9088 find a macro that will generate the 32 bit offset
afdbd6d0 9089 code pattern. */
5e0116d5 9090 if (my_getSmallExpression (&offset_expr, offset_reloc, s) == 0
252b5132
RH
9091 && (offset_expr.X_op != O_constant
9092 || offset_expr.X_add_number >= 0x8000
afdbd6d0 9093 || offset_expr.X_add_number < -0x8000))
252b5132
RH
9094 break;
9095
252b5132
RH
9096 s = expr_end;
9097 continue;
9098
9099 case 'p': /* pc relative offset */
0b25d3e6 9100 *offset_reloc = BFD_RELOC_16_PCREL_S2;
252b5132
RH
9101 my_getExpression (&offset_expr, s);
9102 s = expr_end;
9103 continue;
9104
9105 case 'u': /* upper 16 bits */
5e0116d5
RS
9106 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0
9107 && imm_expr.X_op == O_constant
9108 && (imm_expr.X_add_number < 0
9109 || imm_expr.X_add_number >= 0x10000))
252b5132
RH
9110 as_bad (_("lui expression not in range 0..65535"));
9111 s = expr_end;
9112 continue;
9113
9114 case 'a': /* 26 bit address */
9115 my_getExpression (&offset_expr, s);
9116 s = expr_end;
f6688943 9117 *offset_reloc = BFD_RELOC_MIPS_JMP;
252b5132
RH
9118 continue;
9119
9120 case 'N': /* 3 bit branch condition code */
9121 case 'M': /* 3 bit compare condition code */
9122 if (strncmp (s, "$fcc", 4) != 0)
9123 break;
9124 s += 4;
9125 regno = 0;
9126 do
9127 {
9128 regno *= 10;
9129 regno += *s - '0';
9130 ++s;
9131 }
3882b010 9132 while (ISDIGIT (*s));
252b5132 9133 if (regno > 7)
30c378fd
CD
9134 as_bad (_("Invalid condition code register $fcc%d"), regno);
9135 if ((strcmp(str + strlen(str) - 3, ".ps") == 0
9136 || strcmp(str + strlen(str) - 5, "any2f") == 0
9137 || strcmp(str + strlen(str) - 5, "any2t") == 0)
9138 && (regno & 1) != 0)
9139 as_warn(_("Condition code register should be even for %s, was %d"),
9140 str, regno);
9141 if ((strcmp(str + strlen(str) - 5, "any4f") == 0
9142 || strcmp(str + strlen(str) - 5, "any4t") == 0)
9143 && (regno & 3) != 0)
9144 as_warn(_("Condition code register should be 0 or 4 for %s, was %d"),
9145 str, regno);
252b5132
RH
9146 if (*args == 'N')
9147 ip->insn_opcode |= regno << OP_SH_BCC;
9148 else
9149 ip->insn_opcode |= regno << OP_SH_CCC;
beae10d5 9150 continue;
252b5132 9151
156c2f8b
NC
9152 case 'H':
9153 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
9154 s += 2;
3882b010 9155 if (ISDIGIT (*s))
156c2f8b
NC
9156 {
9157 c = 0;
9158 do
9159 {
9160 c *= 10;
9161 c += *s - '0';
9162 ++s;
9163 }
3882b010 9164 while (ISDIGIT (*s));
156c2f8b
NC
9165 }
9166 else
9167 c = 8; /* Invalid sel value. */
9168
9169 if (c > 7)
9170 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
9171 ip->insn_opcode |= c;
9172 continue;
9173
60b63b72
RS
9174 case 'e':
9175 /* Must be at least one digit. */
9176 my_getExpression (&imm_expr, s);
9177 check_absolute_expr (ip, &imm_expr);
9178
9179 if ((unsigned long) imm_expr.X_add_number
9180 > (unsigned long) OP_MASK_VECBYTE)
9181 {
9182 as_bad (_("bad byte vector index (%ld)"),
9183 (long) imm_expr.X_add_number);
9184 imm_expr.X_add_number = 0;
9185 }
9186
9187 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECBYTE;
9188 imm_expr.X_op = O_absent;
9189 s = expr_end;
9190 continue;
9191
9192 case '%':
9193 my_getExpression (&imm_expr, s);
9194 check_absolute_expr (ip, &imm_expr);
9195
9196 if ((unsigned long) imm_expr.X_add_number
9197 > (unsigned long) OP_MASK_VECALIGN)
9198 {
9199 as_bad (_("bad byte vector index (%ld)"),
9200 (long) imm_expr.X_add_number);
9201 imm_expr.X_add_number = 0;
9202 }
9203
9204 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECALIGN;
9205 imm_expr.X_op = O_absent;
9206 s = expr_end;
9207 continue;
9208
252b5132
RH
9209 default:
9210 as_bad (_("bad char = '%c'\n"), *args);
9211 internalError ();
9212 }
9213 break;
9214 }
9215 /* Args don't match. */
9216 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
9217 !strcmp (insn->name, insn[1].name))
9218 {
9219 ++insn;
9220 s = argsStart;
268f6bed 9221 insn_error = _("illegal operands");
252b5132
RH
9222 continue;
9223 }
268f6bed
L
9224 if (save_c)
9225 *(--s) = save_c;
252b5132
RH
9226 insn_error = _("illegal operands");
9227 return;
9228 }
9229}
9230
9231/* This routine assembles an instruction into its binary format when
9232 assembling for the mips16. As a side effect, it sets one of the
9233 global variables imm_reloc or offset_reloc to the type of
9234 relocation to do if one of the operands is an address expression.
9235 It also sets mips16_small and mips16_ext if the user explicitly
9236 requested a small or extended instruction. */
9237
9238static void
17a2f251 9239mips16_ip (char *str, struct mips_cl_insn *ip)
252b5132
RH
9240{
9241 char *s;
9242 const char *args;
9243 struct mips_opcode *insn;
9244 char *argsstart;
9245 unsigned int regno;
9246 unsigned int lastregno = 0;
9247 char *s_reset;
9248
9249 insn_error = NULL;
9250
b34976b6
AM
9251 mips16_small = FALSE;
9252 mips16_ext = FALSE;
252b5132 9253
3882b010 9254 for (s = str; ISLOWER (*s); ++s)
252b5132
RH
9255 ;
9256 switch (*s)
9257 {
9258 case '\0':
9259 break;
9260
9261 case ' ':
9262 *s++ = '\0';
9263 break;
9264
9265 case '.':
9266 if (s[1] == 't' && s[2] == ' ')
9267 {
9268 *s = '\0';
b34976b6 9269 mips16_small = TRUE;
252b5132
RH
9270 s += 3;
9271 break;
9272 }
9273 else if (s[1] == 'e' && s[2] == ' ')
9274 {
9275 *s = '\0';
b34976b6 9276 mips16_ext = TRUE;
252b5132
RH
9277 s += 3;
9278 break;
9279 }
9280 /* Fall through. */
9281 default:
9282 insn_error = _("unknown opcode");
9283 return;
9284 }
9285
9286 if (mips_opts.noautoextend && ! mips16_ext)
b34976b6 9287 mips16_small = TRUE;
252b5132
RH
9288
9289 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
9290 {
9291 insn_error = _("unrecognized opcode");
9292 return;
9293 }
9294
9295 argsstart = s;
9296 for (;;)
9297 {
9298 assert (strcmp (insn->name, str) == 0);
9299
9300 ip->insn_mo = insn;
9301 ip->insn_opcode = insn->match;
b34976b6 9302 ip->use_extend = FALSE;
252b5132 9303 imm_expr.X_op = O_absent;
f6688943
TS
9304 imm_reloc[0] = BFD_RELOC_UNUSED;
9305 imm_reloc[1] = BFD_RELOC_UNUSED;
9306 imm_reloc[2] = BFD_RELOC_UNUSED;
5f74bc13 9307 imm2_expr.X_op = O_absent;
252b5132 9308 offset_expr.X_op = O_absent;
f6688943
TS
9309 offset_reloc[0] = BFD_RELOC_UNUSED;
9310 offset_reloc[1] = BFD_RELOC_UNUSED;
9311 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
9312 for (args = insn->args; 1; ++args)
9313 {
9314 int c;
9315
9316 if (*s == ' ')
9317 ++s;
9318
9319 /* In this switch statement we call break if we did not find
9320 a match, continue if we did find a match, or return if we
9321 are done. */
9322
9323 c = *args;
9324 switch (c)
9325 {
9326 case '\0':
9327 if (*s == '\0')
9328 {
9329 /* Stuff the immediate value in now, if we can. */
9330 if (imm_expr.X_op == O_constant
f6688943 9331 && *imm_reloc > BFD_RELOC_UNUSED
252b5132
RH
9332 && insn->pinfo != INSN_MACRO)
9333 {
c4e7957c 9334 mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
b34976b6 9335 imm_expr.X_add_number, TRUE, mips16_small,
252b5132
RH
9336 mips16_ext, &ip->insn_opcode,
9337 &ip->use_extend, &ip->extend);
9338 imm_expr.X_op = O_absent;
f6688943 9339 *imm_reloc = BFD_RELOC_UNUSED;
252b5132
RH
9340 }
9341
9342 return;
9343 }
9344 break;
9345
9346 case ',':
9347 if (*s++ == c)
9348 continue;
9349 s--;
9350 switch (*++args)
9351 {
9352 case 'v':
9353 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9354 continue;
9355 case 'w':
9356 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9357 continue;
9358 }
9359 break;
9360
9361 case '(':
9362 case ')':
9363 if (*s++ == c)
9364 continue;
9365 break;
9366
9367 case 'v':
9368 case 'w':
9369 if (s[0] != '$')
9370 {
9371 if (c == 'v')
9372 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9373 else
9374 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9375 ++args;
9376 continue;
9377 }
9378 /* Fall through. */
9379 case 'x':
9380 case 'y':
9381 case 'z':
9382 case 'Z':
9383 case '0':
9384 case 'S':
9385 case 'R':
9386 case 'X':
9387 case 'Y':
9388 if (s[0] != '$')
9389 break;
9390 s_reset = s;
3882b010 9391 if (ISDIGIT (s[1]))
252b5132
RH
9392 {
9393 ++s;
9394 regno = 0;
9395 do
9396 {
9397 regno *= 10;
9398 regno += *s - '0';
9399 ++s;
9400 }
3882b010 9401 while (ISDIGIT (*s));
252b5132
RH
9402 if (regno > 31)
9403 {
9404 as_bad (_("invalid register number (%d)"), regno);
9405 regno = 2;
9406 }
9407 }
9408 else
9409 {
76db943d
TS
9410 if (s[1] == 'r' && s[2] == 'a')
9411 {
9412 s += 3;
9413 regno = RA;
9414 }
9415 else if (s[1] == 'f' && s[2] == 'p')
252b5132
RH
9416 {
9417 s += 3;
9418 regno = FP;
9419 }
9420 else if (s[1] == 's' && s[2] == 'p')
9421 {
9422 s += 3;
9423 regno = SP;
9424 }
9425 else if (s[1] == 'g' && s[2] == 'p')
9426 {
9427 s += 3;
9428 regno = GP;
9429 }
9430 else if (s[1] == 'a' && s[2] == 't')
9431 {
9432 s += 3;
9433 regno = AT;
9434 }
9435 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
9436 {
9437 s += 4;
9438 regno = KT0;
9439 }
9440 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
9441 {
9442 s += 4;
9443 regno = KT1;
9444 }
85b51719
TS
9445 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
9446 {
9447 s += 5;
9448 regno = ZERO;
9449 }
252b5132
RH
9450 else
9451 break;
9452 }
9453
9454 if (*s == ' ')
9455 ++s;
9456 if (args[1] != *s)
9457 {
9458 if (c == 'v' || c == 'w')
9459 {
9460 regno = mips16_to_32_reg_map[lastregno];
9461 s = s_reset;
f9419b05 9462 ++args;
252b5132
RH
9463 }
9464 }
9465
9466 switch (c)
9467 {
9468 case 'x':
9469 case 'y':
9470 case 'z':
9471 case 'v':
9472 case 'w':
9473 case 'Z':
9474 regno = mips32_to_16_reg_map[regno];
9475 break;
9476
9477 case '0':
9478 if (regno != 0)
9479 regno = ILLEGAL_REG;
9480 break;
9481
9482 case 'S':
9483 if (regno != SP)
9484 regno = ILLEGAL_REG;
9485 break;
9486
9487 case 'R':
9488 if (regno != RA)
9489 regno = ILLEGAL_REG;
9490 break;
9491
9492 case 'X':
9493 case 'Y':
9494 if (regno == AT && ! mips_opts.noat)
9495 as_warn (_("used $at without \".set noat\""));
9496 break;
9497
9498 default:
9499 internalError ();
9500 }
9501
9502 if (regno == ILLEGAL_REG)
9503 break;
9504
9505 switch (c)
9506 {
9507 case 'x':
9508 case 'v':
9509 ip->insn_opcode |= regno << MIPS16OP_SH_RX;
9510 break;
9511 case 'y':
9512 case 'w':
9513 ip->insn_opcode |= regno << MIPS16OP_SH_RY;
9514 break;
9515 case 'z':
9516 ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
9517 break;
9518 case 'Z':
9519 ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
9520 case '0':
9521 case 'S':
9522 case 'R':
9523 break;
9524 case 'X':
9525 ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
9526 break;
9527 case 'Y':
9528 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
9529 ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
9530 break;
9531 default:
9532 internalError ();
9533 }
9534
9535 lastregno = regno;
9536 continue;
9537
9538 case 'P':
9539 if (strncmp (s, "$pc", 3) == 0)
9540 {
9541 s += 3;
9542 continue;
9543 }
9544 break;
9545
9546 case '<':
9547 case '>':
9548 case '[':
9549 case ']':
9550 case '4':
9551 case '5':
9552 case 'H':
9553 case 'W':
9554 case 'D':
9555 case 'j':
9556 case '8':
9557 case 'V':
9558 case 'C':
9559 case 'U':
9560 case 'k':
9561 case 'K':
9562 if (s[0] == '%'
9563 && strncmp (s + 1, "gprel(", sizeof "gprel(" - 1) == 0)
9564 {
9565 /* This is %gprel(SYMBOL). We need to read SYMBOL,
9566 and generate the appropriate reloc. If the text
9567 inside %gprel is not a symbol name with an
9568 optional offset, then we generate a normal reloc
9569 and will probably fail later. */
9570 my_getExpression (&imm_expr, s + sizeof "%gprel" - 1);
9571 if (imm_expr.X_op == O_symbol)
9572 {
b34976b6 9573 mips16_ext = TRUE;
f6688943 9574 *imm_reloc = BFD_RELOC_MIPS16_GPREL;
252b5132 9575 s = expr_end;
b34976b6 9576 ip->use_extend = TRUE;
252b5132
RH
9577 ip->extend = 0;
9578 continue;
9579 }
9580 }
9581 else
9582 {
9583 /* Just pick up a normal expression. */
9584 my_getExpression (&imm_expr, s);
9585 }
9586
9587 if (imm_expr.X_op == O_register)
9588 {
9589 /* What we thought was an expression turned out to
9590 be a register. */
9591
9592 if (s[0] == '(' && args[1] == '(')
9593 {
9594 /* It looks like the expression was omitted
9595 before a register indirection, which means
9596 that the expression is implicitly zero. We
9597 still set up imm_expr, so that we handle
9598 explicit extensions correctly. */
9599 imm_expr.X_op = O_constant;
9600 imm_expr.X_add_number = 0;
f6688943 9601 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
9602 continue;
9603 }
9604
9605 break;
9606 }
9607
9608 /* We need to relax this instruction. */
f6688943 9609 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
9610 s = expr_end;
9611 continue;
9612
9613 case 'p':
9614 case 'q':
9615 case 'A':
9616 case 'B':
9617 case 'E':
9618 /* We use offset_reloc rather than imm_reloc for the PC
9619 relative operands. This lets macros with both
9620 immediate and address operands work correctly. */
9621 my_getExpression (&offset_expr, s);
9622
9623 if (offset_expr.X_op == O_register)
9624 break;
9625
9626 /* We need to relax this instruction. */
f6688943 9627 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
9628 s = expr_end;
9629 continue;
9630
9631 case '6': /* break code */
9632 my_getExpression (&imm_expr, s);
9633 check_absolute_expr (ip, &imm_expr);
9634 if ((unsigned long) imm_expr.X_add_number > 63)
9635 {
9636 as_warn (_("Invalid value for `%s' (%lu)"),
9637 ip->insn_mo->name,
9638 (unsigned long) imm_expr.X_add_number);
9639 imm_expr.X_add_number &= 0x3f;
9640 }
9641 ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
9642 imm_expr.X_op = O_absent;
9643 s = expr_end;
9644 continue;
9645
9646 case 'a': /* 26 bit address */
9647 my_getExpression (&offset_expr, s);
9648 s = expr_end;
f6688943 9649 *offset_reloc = BFD_RELOC_MIPS16_JMP;
252b5132
RH
9650 ip->insn_opcode <<= 16;
9651 continue;
9652
9653 case 'l': /* register list for entry macro */
9654 case 'L': /* register list for exit macro */
9655 {
9656 int mask;
9657
9658 if (c == 'l')
9659 mask = 0;
9660 else
9661 mask = 7 << 3;
9662 while (*s != '\0')
9663 {
9664 int freg, reg1, reg2;
9665
9666 while (*s == ' ' || *s == ',')
9667 ++s;
9668 if (*s != '$')
9669 {
9670 as_bad (_("can't parse register list"));
9671 break;
9672 }
9673 ++s;
9674 if (*s != 'f')
9675 freg = 0;
9676 else
9677 {
9678 freg = 1;
9679 ++s;
9680 }
9681 reg1 = 0;
3882b010 9682 while (ISDIGIT (*s))
252b5132
RH
9683 {
9684 reg1 *= 10;
9685 reg1 += *s - '0';
9686 ++s;
9687 }
9688 if (*s == ' ')
9689 ++s;
9690 if (*s != '-')
9691 reg2 = reg1;
9692 else
9693 {
9694 ++s;
9695 if (*s != '$')
9696 break;
9697 ++s;
9698 if (freg)
9699 {
9700 if (*s == 'f')
9701 ++s;
9702 else
9703 {
9704 as_bad (_("invalid register list"));
9705 break;
9706 }
9707 }
9708 reg2 = 0;
3882b010 9709 while (ISDIGIT (*s))
252b5132
RH
9710 {
9711 reg2 *= 10;
9712 reg2 += *s - '0';
9713 ++s;
9714 }
9715 }
9716 if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
9717 {
9718 mask &= ~ (7 << 3);
9719 mask |= 5 << 3;
9720 }
9721 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
9722 {
9723 mask &= ~ (7 << 3);
9724 mask |= 6 << 3;
9725 }
9726 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
9727 mask |= (reg2 - 3) << 3;
9728 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
9729 mask |= (reg2 - 15) << 1;
f9419b05 9730 else if (reg1 == RA && reg2 == RA)
252b5132
RH
9731 mask |= 1;
9732 else
9733 {
9734 as_bad (_("invalid register list"));
9735 break;
9736 }
9737 }
9738 /* The mask is filled in in the opcode table for the
9739 benefit of the disassembler. We remove it before
9740 applying the actual mask. */
9741 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
9742 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
9743 }
9744 continue;
9745
9746 case 'e': /* extend code */
9747 my_getExpression (&imm_expr, s);
9748 check_absolute_expr (ip, &imm_expr);
9749 if ((unsigned long) imm_expr.X_add_number > 0x7ff)
9750 {
9751 as_warn (_("Invalid value for `%s' (%lu)"),
9752 ip->insn_mo->name,
9753 (unsigned long) imm_expr.X_add_number);
9754 imm_expr.X_add_number &= 0x7ff;
9755 }
9756 ip->insn_opcode |= imm_expr.X_add_number;
9757 imm_expr.X_op = O_absent;
9758 s = expr_end;
9759 continue;
9760
9761 default:
9762 internalError ();
9763 }
9764 break;
9765 }
9766
9767 /* Args don't match. */
9768 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
9769 strcmp (insn->name, insn[1].name) == 0)
9770 {
9771 ++insn;
9772 s = argsstart;
9773 continue;
9774 }
9775
9776 insn_error = _("illegal operands");
9777
9778 return;
9779 }
9780}
9781
9782/* This structure holds information we know about a mips16 immediate
9783 argument type. */
9784
e972090a
NC
9785struct mips16_immed_operand
9786{
252b5132
RH
9787 /* The type code used in the argument string in the opcode table. */
9788 int type;
9789 /* The number of bits in the short form of the opcode. */
9790 int nbits;
9791 /* The number of bits in the extended form of the opcode. */
9792 int extbits;
9793 /* The amount by which the short form is shifted when it is used;
9794 for example, the sw instruction has a shift count of 2. */
9795 int shift;
9796 /* The amount by which the short form is shifted when it is stored
9797 into the instruction code. */
9798 int op_shift;
9799 /* Non-zero if the short form is unsigned. */
9800 int unsp;
9801 /* Non-zero if the extended form is unsigned. */
9802 int extu;
9803 /* Non-zero if the value is PC relative. */
9804 int pcrel;
9805};
9806
9807/* The mips16 immediate operand types. */
9808
9809static const struct mips16_immed_operand mips16_immed_operands[] =
9810{
9811 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9812 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9813 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9814 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9815 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
9816 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
9817 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
9818 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
9819 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
9820 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
9821 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
9822 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
9823 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
9824 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
9825 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
9826 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
9827 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9828 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9829 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
9830 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
9831 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
9832};
9833
9834#define MIPS16_NUM_IMMED \
9835 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
9836
9837/* Handle a mips16 instruction with an immediate value. This or's the
9838 small immediate value into *INSN. It sets *USE_EXTEND to indicate
9839 whether an extended value is needed; if one is needed, it sets
9840 *EXTEND to the value. The argument type is TYPE. The value is VAL.
9841 If SMALL is true, an unextended opcode was explicitly requested.
9842 If EXT is true, an extended opcode was explicitly requested. If
9843 WARN is true, warn if EXT does not match reality. */
9844
9845static void
17a2f251
TS
9846mips16_immed (char *file, unsigned int line, int type, offsetT val,
9847 bfd_boolean warn, bfd_boolean small, bfd_boolean ext,
9848 unsigned long *insn, bfd_boolean *use_extend,
9849 unsigned short *extend)
252b5132
RH
9850{
9851 register const struct mips16_immed_operand *op;
9852 int mintiny, maxtiny;
b34976b6 9853 bfd_boolean needext;
252b5132
RH
9854
9855 op = mips16_immed_operands;
9856 while (op->type != type)
9857 {
9858 ++op;
9859 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
9860 }
9861
9862 if (op->unsp)
9863 {
9864 if (type == '<' || type == '>' || type == '[' || type == ']')
9865 {
9866 mintiny = 1;
9867 maxtiny = 1 << op->nbits;
9868 }
9869 else
9870 {
9871 mintiny = 0;
9872 maxtiny = (1 << op->nbits) - 1;
9873 }
9874 }
9875 else
9876 {
9877 mintiny = - (1 << (op->nbits - 1));
9878 maxtiny = (1 << (op->nbits - 1)) - 1;
9879 }
9880
9881 /* Branch offsets have an implicit 0 in the lowest bit. */
9882 if (type == 'p' || type == 'q')
9883 val /= 2;
9884
9885 if ((val & ((1 << op->shift) - 1)) != 0
9886 || val < (mintiny << op->shift)
9887 || val > (maxtiny << op->shift))
b34976b6 9888 needext = TRUE;
252b5132 9889 else
b34976b6 9890 needext = FALSE;
252b5132
RH
9891
9892 if (warn && ext && ! needext)
beae10d5
KH
9893 as_warn_where (file, line,
9894 _("extended operand requested but not required"));
252b5132
RH
9895 if (small && needext)
9896 as_bad_where (file, line, _("invalid unextended operand value"));
9897
9898 if (small || (! ext && ! needext))
9899 {
9900 int insnval;
9901
b34976b6 9902 *use_extend = FALSE;
252b5132
RH
9903 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
9904 insnval <<= op->op_shift;
9905 *insn |= insnval;
9906 }
9907 else
9908 {
9909 long minext, maxext;
9910 int extval;
9911
9912 if (op->extu)
9913 {
9914 minext = 0;
9915 maxext = (1 << op->extbits) - 1;
9916 }
9917 else
9918 {
9919 minext = - (1 << (op->extbits - 1));
9920 maxext = (1 << (op->extbits - 1)) - 1;
9921 }
9922 if (val < minext || val > maxext)
9923 as_bad_where (file, line,
9924 _("operand value out of range for instruction"));
9925
b34976b6 9926 *use_extend = TRUE;
252b5132
RH
9927 if (op->extbits == 16)
9928 {
9929 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
9930 val &= 0x1f;
9931 }
9932 else if (op->extbits == 15)
9933 {
9934 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
9935 val &= 0xf;
9936 }
9937 else
9938 {
9939 extval = ((val & 0x1f) << 6) | (val & 0x20);
9940 val = 0;
9941 }
9942
9943 *extend = (unsigned short) extval;
9944 *insn |= val;
9945 }
9946}
9947\f
5e0116d5 9948static const struct percent_op_match
ad8d3bb3 9949{
5e0116d5
RS
9950 const char *str;
9951 bfd_reloc_code_real_type reloc;
ad8d3bb3
TS
9952} percent_op[] =
9953{
5e0116d5 9954 {"%lo", BFD_RELOC_LO16},
ad8d3bb3 9955#ifdef OBJ_ELF
5e0116d5
RS
9956 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
9957 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
9958 {"%call16", BFD_RELOC_MIPS_CALL16},
9959 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
9960 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
9961 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
9962 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
9963 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
9964 {"%got", BFD_RELOC_MIPS_GOT16},
9965 {"%gp_rel", BFD_RELOC_GPREL16},
9966 {"%half", BFD_RELOC_16},
9967 {"%highest", BFD_RELOC_MIPS_HIGHEST},
9968 {"%higher", BFD_RELOC_MIPS_HIGHER},
9969 {"%neg", BFD_RELOC_MIPS_SUB},
ad8d3bb3 9970#endif
5e0116d5 9971 {"%hi", BFD_RELOC_HI16_S}
ad8d3bb3
TS
9972};
9973
252b5132 9974
5e0116d5
RS
9975/* Return true if *STR points to a relocation operator. When returning true,
9976 move *STR over the operator and store its relocation code in *RELOC.
9977 Leave both *STR and *RELOC alone when returning false. */
9978
9979static bfd_boolean
17a2f251 9980parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
252b5132 9981{
5e0116d5 9982 size_t i;
76b3015f 9983
5e0116d5
RS
9984 for (i = 0; i < ARRAY_SIZE (percent_op); i++)
9985 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
394f9b3a 9986 {
5e0116d5
RS
9987 *str += strlen (percent_op[i].str);
9988 *reloc = percent_op[i].reloc;
394f9b3a 9989
5e0116d5
RS
9990 /* Check whether the output BFD supports this relocation.
9991 If not, issue an error and fall back on something safe. */
9992 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
394f9b3a 9993 {
5e0116d5
RS
9994 as_bad ("relocation %s isn't supported by the current ABI",
9995 percent_op[i].str);
9996 *reloc = BFD_RELOC_LO16;
394f9b3a 9997 }
5e0116d5 9998 return TRUE;
394f9b3a 9999 }
5e0116d5 10000 return FALSE;
394f9b3a 10001}
ad8d3bb3 10002
ad8d3bb3 10003
5e0116d5
RS
10004/* Parse string STR as a 16-bit relocatable operand. Store the
10005 expression in *EP and the relocations in the array starting
10006 at RELOC. Return the number of relocation operators used.
ad8d3bb3 10007
5e0116d5
RS
10008 On exit, EXPR_END points to the first character after the expression.
10009 If no relocation operators are used, RELOC[0] is set to BFD_RELOC_LO16. */
ad8d3bb3 10010
5e0116d5 10011static size_t
17a2f251
TS
10012my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
10013 char *str)
ad8d3bb3 10014{
5e0116d5
RS
10015 bfd_reloc_code_real_type reversed_reloc[3];
10016 size_t reloc_index, i;
09b8f35a
RS
10017 int crux_depth, str_depth;
10018 char *crux;
5e0116d5
RS
10019
10020 /* Search for the start of the main expression, recoding relocations
09b8f35a
RS
10021 in REVERSED_RELOC. End the loop with CRUX pointing to the start
10022 of the main expression and with CRUX_DEPTH containing the number
10023 of open brackets at that point. */
10024 reloc_index = -1;
10025 str_depth = 0;
10026 do
fb1b3232 10027 {
09b8f35a
RS
10028 reloc_index++;
10029 crux = str;
10030 crux_depth = str_depth;
10031
10032 /* Skip over whitespace and brackets, keeping count of the number
10033 of brackets. */
10034 while (*str == ' ' || *str == '\t' || *str == '(')
10035 if (*str++ == '(')
10036 str_depth++;
5e0116d5 10037 }
09b8f35a
RS
10038 while (*str == '%'
10039 && reloc_index < (HAVE_NEWABI ? 3 : 1)
10040 && parse_relocation (&str, &reversed_reloc[reloc_index]));
ad8d3bb3 10041
09b8f35a 10042 my_getExpression (ep, crux);
5e0116d5 10043 str = expr_end;
394f9b3a 10044
5e0116d5 10045 /* Match every open bracket. */
09b8f35a 10046 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
5e0116d5 10047 if (*str++ == ')')
09b8f35a 10048 crux_depth--;
394f9b3a 10049
09b8f35a 10050 if (crux_depth > 0)
5e0116d5 10051 as_bad ("unclosed '('");
394f9b3a 10052
5e0116d5 10053 expr_end = str;
252b5132 10054
64bdfcaf
RS
10055 if (reloc_index == 0)
10056 reloc[0] = BFD_RELOC_LO16;
10057 else
10058 {
10059 prev_reloc_op_frag = frag_now;
10060 for (i = 0; i < reloc_index; i++)
10061 reloc[i] = reversed_reloc[reloc_index - 1 - i];
10062 }
fb1b3232 10063
5e0116d5 10064 return reloc_index;
252b5132
RH
10065}
10066
10067static void
17a2f251 10068my_getExpression (expressionS *ep, char *str)
252b5132
RH
10069{
10070 char *save_in;
98aa84af 10071 valueT val;
252b5132
RH
10072
10073 save_in = input_line_pointer;
10074 input_line_pointer = str;
10075 expression (ep);
10076 expr_end = input_line_pointer;
10077 input_line_pointer = save_in;
10078
10079 /* If we are in mips16 mode, and this is an expression based on `.',
10080 then we bump the value of the symbol by 1 since that is how other
10081 text symbols are handled. We don't bother to handle complex
10082 expressions, just `.' plus or minus a constant. */
10083 if (mips_opts.mips16
10084 && ep->X_op == O_symbol
10085 && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
10086 && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
49309057
ILT
10087 && symbol_get_frag (ep->X_add_symbol) == frag_now
10088 && symbol_constant_p (ep->X_add_symbol)
98aa84af
AM
10089 && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
10090 S_SET_VALUE (ep->X_add_symbol, val + 1);
252b5132
RH
10091}
10092
10093/* Turn a string in input_line_pointer into a floating point constant
bc0d738a
NC
10094 of type TYPE, and store the appropriate bytes in *LITP. The number
10095 of LITTLENUMS emitted is stored in *SIZEP. An error message is
252b5132
RH
10096 returned, or NULL on OK. */
10097
10098char *
17a2f251 10099md_atof (int type, char *litP, int *sizeP)
252b5132
RH
10100{
10101 int prec;
10102 LITTLENUM_TYPE words[4];
10103 char *t;
10104 int i;
10105
10106 switch (type)
10107 {
10108 case 'f':
10109 prec = 2;
10110 break;
10111
10112 case 'd':
10113 prec = 4;
10114 break;
10115
10116 default:
10117 *sizeP = 0;
10118 return _("bad call to md_atof");
10119 }
10120
10121 t = atof_ieee (input_line_pointer, type, words);
10122 if (t)
10123 input_line_pointer = t;
10124
10125 *sizeP = prec * 2;
10126
10127 if (! target_big_endian)
10128 {
10129 for (i = prec - 1; i >= 0; i--)
10130 {
17a2f251 10131 md_number_to_chars (litP, words[i], 2);
252b5132
RH
10132 litP += 2;
10133 }
10134 }
10135 else
10136 {
10137 for (i = 0; i < prec; i++)
10138 {
17a2f251 10139 md_number_to_chars (litP, words[i], 2);
252b5132
RH
10140 litP += 2;
10141 }
10142 }
bdaaa2e1 10143
252b5132
RH
10144 return NULL;
10145}
10146
10147void
17a2f251 10148md_number_to_chars (char *buf, valueT val, int n)
252b5132
RH
10149{
10150 if (target_big_endian)
10151 number_to_chars_bigendian (buf, val, n);
10152 else
10153 number_to_chars_littleendian (buf, val, n);
10154}
10155\f
ae948b86 10156#ifdef OBJ_ELF
e013f690
TS
10157static int support_64bit_objects(void)
10158{
10159 const char **list, **l;
aa3d8fdf 10160 int yes;
e013f690
TS
10161
10162 list = bfd_target_list ();
10163 for (l = list; *l != NULL; l++)
10164#ifdef TE_TMIPS
10165 /* This is traditional mips */
10166 if (strcmp (*l, "elf64-tradbigmips") == 0
10167 || strcmp (*l, "elf64-tradlittlemips") == 0)
10168#else
10169 if (strcmp (*l, "elf64-bigmips") == 0
10170 || strcmp (*l, "elf64-littlemips") == 0)
10171#endif
10172 break;
aa3d8fdf 10173 yes = (*l != NULL);
e013f690 10174 free (list);
aa3d8fdf 10175 return yes;
e013f690 10176}
ae948b86 10177#endif /* OBJ_ELF */
e013f690 10178
78849248 10179const char *md_shortopts = "O::g::G:";
252b5132 10180
e972090a
NC
10181struct option md_longopts[] =
10182{
f9b4148d
CD
10183 /* Options which specify architecture. */
10184#define OPTION_ARCH_BASE (OPTION_MD_BASE)
10185#define OPTION_MARCH (OPTION_ARCH_BASE + 0)
10186 {"march", required_argument, NULL, OPTION_MARCH},
10187#define OPTION_MTUNE (OPTION_ARCH_BASE + 1)
10188 {"mtune", required_argument, NULL, OPTION_MTUNE},
10189#define OPTION_MIPS1 (OPTION_ARCH_BASE + 2)
252b5132
RH
10190 {"mips0", no_argument, NULL, OPTION_MIPS1},
10191 {"mips1", no_argument, NULL, OPTION_MIPS1},
f9b4148d 10192#define OPTION_MIPS2 (OPTION_ARCH_BASE + 3)
252b5132 10193 {"mips2", no_argument, NULL, OPTION_MIPS2},
f9b4148d 10194#define OPTION_MIPS3 (OPTION_ARCH_BASE + 4)
252b5132 10195 {"mips3", no_argument, NULL, OPTION_MIPS3},
f9b4148d 10196#define OPTION_MIPS4 (OPTION_ARCH_BASE + 5)
252b5132 10197 {"mips4", no_argument, NULL, OPTION_MIPS4},
f9b4148d 10198#define OPTION_MIPS5 (OPTION_ARCH_BASE + 6)
ae948b86 10199 {"mips5", no_argument, NULL, OPTION_MIPS5},
f9b4148d 10200#define OPTION_MIPS32 (OPTION_ARCH_BASE + 7)
ae948b86 10201 {"mips32", no_argument, NULL, OPTION_MIPS32},
f9b4148d 10202#define OPTION_MIPS64 (OPTION_ARCH_BASE + 8)
ae948b86 10203 {"mips64", no_argument, NULL, OPTION_MIPS64},
f9b4148d
CD
10204#define OPTION_MIPS32R2 (OPTION_ARCH_BASE + 9)
10205 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
5f74bc13
CD
10206#define OPTION_MIPS64R2 (OPTION_ARCH_BASE + 10)
10207 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
f9b4148d
CD
10208
10209 /* Options which specify Application Specific Extensions (ASEs). */
5f74bc13 10210#define OPTION_ASE_BASE (OPTION_ARCH_BASE + 11)
f9b4148d
CD
10211#define OPTION_MIPS16 (OPTION_ASE_BASE + 0)
10212 {"mips16", no_argument, NULL, OPTION_MIPS16},
10213#define OPTION_NO_MIPS16 (OPTION_ASE_BASE + 1)
10214 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
10215#define OPTION_MIPS3D (OPTION_ASE_BASE + 2)
10216 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
10217#define OPTION_NO_MIPS3D (OPTION_ASE_BASE + 3)
10218 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
10219#define OPTION_MDMX (OPTION_ASE_BASE + 4)
10220 {"mdmx", no_argument, NULL, OPTION_MDMX},
10221#define OPTION_NO_MDMX (OPTION_ASE_BASE + 5)
10222 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
10223
10224 /* Old-style architecture options. Don't add more of these. */
10225#define OPTION_COMPAT_ARCH_BASE (OPTION_ASE_BASE + 6)
10226#define OPTION_M4650 (OPTION_COMPAT_ARCH_BASE + 0)
10227 {"m4650", no_argument, NULL, OPTION_M4650},
10228#define OPTION_NO_M4650 (OPTION_COMPAT_ARCH_BASE + 1)
10229 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
10230#define OPTION_M4010 (OPTION_COMPAT_ARCH_BASE + 2)
10231 {"m4010", no_argument, NULL, OPTION_M4010},
10232#define OPTION_NO_M4010 (OPTION_COMPAT_ARCH_BASE + 3)
10233 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
10234#define OPTION_M4100 (OPTION_COMPAT_ARCH_BASE + 4)
10235 {"m4100", no_argument, NULL, OPTION_M4100},
10236#define OPTION_NO_M4100 (OPTION_COMPAT_ARCH_BASE + 5)
10237 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
10238#define OPTION_M3900 (OPTION_COMPAT_ARCH_BASE + 6)
10239 {"m3900", no_argument, NULL, OPTION_M3900},
10240#define OPTION_NO_M3900 (OPTION_COMPAT_ARCH_BASE + 7)
10241 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
10242
10243 /* Options which enable bug fixes. */
10244#define OPTION_FIX_BASE (OPTION_COMPAT_ARCH_BASE + 8)
10245#define OPTION_M7000_HILO_FIX (OPTION_FIX_BASE + 0)
10246 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
10247#define OPTION_MNO_7000_HILO_FIX (OPTION_FIX_BASE + 1)
10248 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10249 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
d766e8ec
RS
10250#define OPTION_FIX_VR4120 (OPTION_FIX_BASE + 2)
10251#define OPTION_NO_FIX_VR4120 (OPTION_FIX_BASE + 3)
10252 {"mfix-vr4120", no_argument, NULL, OPTION_FIX_VR4120},
10253 {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
f9b4148d
CD
10254
10255 /* Miscellaneous options. */
10256#define OPTION_MISC_BASE (OPTION_FIX_BASE + 4)
10257#define OPTION_MEMBEDDED_PIC (OPTION_MISC_BASE + 0)
252b5132 10258 {"membedded-pic", no_argument, NULL, OPTION_MEMBEDDED_PIC},
f9b4148d 10259#define OPTION_TRAP (OPTION_MISC_BASE + 1)
252b5132
RH
10260 {"trap", no_argument, NULL, OPTION_TRAP},
10261 {"no-break", no_argument, NULL, OPTION_TRAP},
f9b4148d 10262#define OPTION_BREAK (OPTION_MISC_BASE + 2)
252b5132
RH
10263 {"break", no_argument, NULL, OPTION_BREAK},
10264 {"no-trap", no_argument, NULL, OPTION_BREAK},
f9b4148d 10265#define OPTION_EB (OPTION_MISC_BASE + 3)
252b5132 10266 {"EB", no_argument, NULL, OPTION_EB},
f9b4148d 10267#define OPTION_EL (OPTION_MISC_BASE + 4)
252b5132 10268 {"EL", no_argument, NULL, OPTION_EL},
f9b4148d 10269#define OPTION_FP32 (OPTION_MISC_BASE + 5)
ae948b86 10270 {"mfp32", no_argument, NULL, OPTION_FP32},
f9b4148d 10271#define OPTION_GP32 (OPTION_MISC_BASE + 6)
c97ef257 10272 {"mgp32", no_argument, NULL, OPTION_GP32},
f9b4148d 10273#define OPTION_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 7)
119d663a 10274 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
f9b4148d 10275#define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 8)
119d663a 10276 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
f9b4148d 10277#define OPTION_FP64 (OPTION_MISC_BASE + 9)
316f5878 10278 {"mfp64", no_argument, NULL, OPTION_FP64},
f9b4148d 10279#define OPTION_GP64 (OPTION_MISC_BASE + 10)
ae948b86 10280 {"mgp64", no_argument, NULL, OPTION_GP64},
f9b4148d
CD
10281#define OPTION_RELAX_BRANCH (OPTION_MISC_BASE + 11)
10282#define OPTION_NO_RELAX_BRANCH (OPTION_MISC_BASE + 12)
4a6a3df4
AO
10283 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
10284 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
f9b4148d
CD
10285
10286 /* ELF-specific options. */
156c2f8b 10287#ifdef OBJ_ELF
f9b4148d 10288#define OPTION_ELF_BASE (OPTION_MISC_BASE + 13)
156c2f8b 10289#define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
156c2f8b
NC
10290 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
10291 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
ae948b86 10292#define OPTION_NON_SHARED (OPTION_ELF_BASE + 1)
156c2f8b 10293 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
ae948b86 10294#define OPTION_XGOT (OPTION_ELF_BASE + 2)
156c2f8b 10295 {"xgot", no_argument, NULL, OPTION_XGOT},
ae948b86
TS
10296#define OPTION_MABI (OPTION_ELF_BASE + 3)
10297 {"mabi", required_argument, NULL, OPTION_MABI},
10298#define OPTION_32 (OPTION_ELF_BASE + 4)
156c2f8b 10299 {"32", no_argument, NULL, OPTION_32},
ae948b86 10300#define OPTION_N32 (OPTION_ELF_BASE + 5)
e013f690 10301 {"n32", no_argument, NULL, OPTION_N32},
ae948b86 10302#define OPTION_64 (OPTION_ELF_BASE + 6)
156c2f8b 10303 {"64", no_argument, NULL, OPTION_64},
ecb4347a
DJ
10304#define OPTION_MDEBUG (OPTION_ELF_BASE + 7)
10305 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
10306#define OPTION_NO_MDEBUG (OPTION_ELF_BASE + 8)
10307 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
dcd410fe
RO
10308#define OPTION_PDR (OPTION_ELF_BASE + 9)
10309 {"mpdr", no_argument, NULL, OPTION_PDR},
10310#define OPTION_NO_PDR (OPTION_ELF_BASE + 10)
10311 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
ae948b86 10312#endif /* OBJ_ELF */
f9b4148d 10313
252b5132
RH
10314 {NULL, no_argument, NULL, 0}
10315};
156c2f8b 10316size_t md_longopts_size = sizeof (md_longopts);
252b5132 10317
316f5878
RS
10318/* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
10319 NEW_VALUE. Warn if another value was already specified. Note:
10320 we have to defer parsing the -march and -mtune arguments in order
10321 to handle 'from-abi' correctly, since the ABI might be specified
10322 in a later argument. */
10323
10324static void
17a2f251 10325mips_set_option_string (const char **string_ptr, const char *new_value)
316f5878
RS
10326{
10327 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
10328 as_warn (_("A different %s was already specified, is now %s"),
10329 string_ptr == &mips_arch_string ? "-march" : "-mtune",
10330 new_value);
10331
10332 *string_ptr = new_value;
10333}
10334
252b5132 10335int
17a2f251 10336md_parse_option (int c, char *arg)
252b5132
RH
10337{
10338 switch (c)
10339 {
119d663a
NC
10340 case OPTION_CONSTRUCT_FLOATS:
10341 mips_disable_float_construction = 0;
10342 break;
bdaaa2e1 10343
119d663a
NC
10344 case OPTION_NO_CONSTRUCT_FLOATS:
10345 mips_disable_float_construction = 1;
10346 break;
bdaaa2e1 10347
252b5132
RH
10348 case OPTION_TRAP:
10349 mips_trap = 1;
10350 break;
10351
10352 case OPTION_BREAK:
10353 mips_trap = 0;
10354 break;
10355
10356 case OPTION_EB:
10357 target_big_endian = 1;
10358 break;
10359
10360 case OPTION_EL:
10361 target_big_endian = 0;
10362 break;
10363
10364 case 'O':
10365 if (arg && arg[1] == '0')
10366 mips_optimize = 1;
10367 else
10368 mips_optimize = 2;
10369 break;
10370
10371 case 'g':
10372 if (arg == NULL)
10373 mips_debug = 2;
10374 else
10375 mips_debug = atoi (arg);
10376 /* When the MIPS assembler sees -g or -g2, it does not do
10377 optimizations which limit full symbolic debugging. We take
10378 that to be equivalent to -O0. */
10379 if (mips_debug == 2)
10380 mips_optimize = 1;
10381 break;
10382
10383 case OPTION_MIPS1:
316f5878 10384 file_mips_isa = ISA_MIPS1;
252b5132
RH
10385 break;
10386
10387 case OPTION_MIPS2:
316f5878 10388 file_mips_isa = ISA_MIPS2;
252b5132
RH
10389 break;
10390
10391 case OPTION_MIPS3:
316f5878 10392 file_mips_isa = ISA_MIPS3;
252b5132
RH
10393 break;
10394
10395 case OPTION_MIPS4:
316f5878 10396 file_mips_isa = ISA_MIPS4;
e7af610e
NC
10397 break;
10398
84ea6cf2 10399 case OPTION_MIPS5:
316f5878 10400 file_mips_isa = ISA_MIPS5;
84ea6cf2
NC
10401 break;
10402
e7af610e 10403 case OPTION_MIPS32:
316f5878 10404 file_mips_isa = ISA_MIPS32;
252b5132
RH
10405 break;
10406
af7ee8bf
CD
10407 case OPTION_MIPS32R2:
10408 file_mips_isa = ISA_MIPS32R2;
10409 break;
10410
5f74bc13
CD
10411 case OPTION_MIPS64R2:
10412 file_mips_isa = ISA_MIPS64R2;
10413 break;
10414
84ea6cf2 10415 case OPTION_MIPS64:
316f5878 10416 file_mips_isa = ISA_MIPS64;
84ea6cf2
NC
10417 break;
10418
ec68c924 10419 case OPTION_MTUNE:
316f5878
RS
10420 mips_set_option_string (&mips_tune_string, arg);
10421 break;
ec68c924 10422
316f5878
RS
10423 case OPTION_MARCH:
10424 mips_set_option_string (&mips_arch_string, arg);
252b5132
RH
10425 break;
10426
10427 case OPTION_M4650:
316f5878
RS
10428 mips_set_option_string (&mips_arch_string, "4650");
10429 mips_set_option_string (&mips_tune_string, "4650");
252b5132
RH
10430 break;
10431
10432 case OPTION_NO_M4650:
10433 break;
10434
10435 case OPTION_M4010:
316f5878
RS
10436 mips_set_option_string (&mips_arch_string, "4010");
10437 mips_set_option_string (&mips_tune_string, "4010");
252b5132
RH
10438 break;
10439
10440 case OPTION_NO_M4010:
10441 break;
10442
10443 case OPTION_M4100:
316f5878
RS
10444 mips_set_option_string (&mips_arch_string, "4100");
10445 mips_set_option_string (&mips_tune_string, "4100");
252b5132
RH
10446 break;
10447
10448 case OPTION_NO_M4100:
10449 break;
10450
252b5132 10451 case OPTION_M3900:
316f5878
RS
10452 mips_set_option_string (&mips_arch_string, "3900");
10453 mips_set_option_string (&mips_tune_string, "3900");
252b5132 10454 break;
bdaaa2e1 10455
252b5132
RH
10456 case OPTION_NO_M3900:
10457 break;
10458
deec1734
CD
10459 case OPTION_MDMX:
10460 mips_opts.ase_mdmx = 1;
10461 break;
10462
10463 case OPTION_NO_MDMX:
10464 mips_opts.ase_mdmx = 0;
10465 break;
10466
252b5132
RH
10467 case OPTION_MIPS16:
10468 mips_opts.mips16 = 1;
b34976b6 10469 mips_no_prev_insn (FALSE);
252b5132
RH
10470 break;
10471
10472 case OPTION_NO_MIPS16:
10473 mips_opts.mips16 = 0;
b34976b6 10474 mips_no_prev_insn (FALSE);
252b5132
RH
10475 break;
10476
1f25f5d3
CD
10477 case OPTION_MIPS3D:
10478 mips_opts.ase_mips3d = 1;
10479 break;
10480
10481 case OPTION_NO_MIPS3D:
10482 mips_opts.ase_mips3d = 0;
10483 break;
10484
252b5132
RH
10485 case OPTION_MEMBEDDED_PIC:
10486 mips_pic = EMBEDDED_PIC;
4d0d148d 10487 if (g_switch_seen)
252b5132
RH
10488 {
10489 as_bad (_("-G may not be used with embedded PIC code"));
10490 return 0;
10491 }
10492 g_switch_value = 0x7fffffff;
10493 break;
10494
d766e8ec
RS
10495 case OPTION_FIX_VR4120:
10496 mips_fix_vr4120 = 1;
60b63b72
RS
10497 break;
10498
d766e8ec
RS
10499 case OPTION_NO_FIX_VR4120:
10500 mips_fix_vr4120 = 0;
60b63b72
RS
10501 break;
10502
4a6a3df4
AO
10503 case OPTION_RELAX_BRANCH:
10504 mips_relax_branch = 1;
10505 break;
10506
10507 case OPTION_NO_RELAX_BRANCH:
10508 mips_relax_branch = 0;
10509 break;
10510
0f074f60 10511#ifdef OBJ_ELF
252b5132
RH
10512 /* When generating ELF code, we permit -KPIC and -call_shared to
10513 select SVR4_PIC, and -non_shared to select no PIC. This is
10514 intended to be compatible with Irix 5. */
10515 case OPTION_CALL_SHARED:
10516 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10517 {
10518 as_bad (_("-call_shared is supported only for ELF format"));
10519 return 0;
10520 }
10521 mips_pic = SVR4_PIC;
143d77c5 10522 mips_abicalls = TRUE;
252b5132
RH
10523 if (g_switch_seen && g_switch_value != 0)
10524 {
10525 as_bad (_("-G may not be used with SVR4 PIC code"));
10526 return 0;
10527 }
10528 g_switch_value = 0;
10529 break;
10530
10531 case OPTION_NON_SHARED:
10532 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10533 {
10534 as_bad (_("-non_shared is supported only for ELF format"));
10535 return 0;
10536 }
10537 mips_pic = NO_PIC;
143d77c5 10538 mips_abicalls = FALSE;
252b5132
RH
10539 break;
10540
10541 /* The -xgot option tells the assembler to use 32 offsets when
10542 accessing the got in SVR4_PIC mode. It is for Irix
10543 compatibility. */
10544 case OPTION_XGOT:
10545 mips_big_got = 1;
10546 break;
0f074f60 10547#endif /* OBJ_ELF */
252b5132
RH
10548
10549 case 'G':
4d0d148d 10550 if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
252b5132
RH
10551 {
10552 as_bad (_("-G may not be used with SVR4 or embedded PIC code"));
10553 return 0;
10554 }
10555 else
10556 g_switch_value = atoi (arg);
10557 g_switch_seen = 1;
10558 break;
10559
0f074f60 10560#ifdef OBJ_ELF
34ba82a8
TS
10561 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
10562 and -mabi=64. */
252b5132 10563 case OPTION_32:
34ba82a8
TS
10564 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10565 {
10566 as_bad (_("-32 is supported for ELF format only"));
10567 return 0;
10568 }
316f5878 10569 mips_abi = O32_ABI;
252b5132
RH
10570 break;
10571
e013f690 10572 case OPTION_N32:
34ba82a8
TS
10573 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10574 {
10575 as_bad (_("-n32 is supported for ELF format only"));
10576 return 0;
10577 }
316f5878 10578 mips_abi = N32_ABI;
e013f690 10579 break;
252b5132 10580
e013f690 10581 case OPTION_64:
34ba82a8
TS
10582 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10583 {
10584 as_bad (_("-64 is supported for ELF format only"));
10585 return 0;
10586 }
316f5878 10587 mips_abi = N64_ABI;
e013f690
TS
10588 if (! support_64bit_objects())
10589 as_fatal (_("No compiled in support for 64 bit object file format"));
252b5132 10590 break;
ae948b86 10591#endif /* OBJ_ELF */
252b5132 10592
c97ef257 10593 case OPTION_GP32:
a325df1d 10594 file_mips_gp32 = 1;
c97ef257
AH
10595 break;
10596
10597 case OPTION_GP64:
a325df1d 10598 file_mips_gp32 = 0;
c97ef257 10599 break;
252b5132 10600
ca4e0257 10601 case OPTION_FP32:
a325df1d 10602 file_mips_fp32 = 1;
316f5878
RS
10603 break;
10604
10605 case OPTION_FP64:
10606 file_mips_fp32 = 0;
ca4e0257
RS
10607 break;
10608
ae948b86 10609#ifdef OBJ_ELF
252b5132 10610 case OPTION_MABI:
34ba82a8
TS
10611 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10612 {
10613 as_bad (_("-mabi is supported for ELF format only"));
10614 return 0;
10615 }
e013f690 10616 if (strcmp (arg, "32") == 0)
316f5878 10617 mips_abi = O32_ABI;
e013f690 10618 else if (strcmp (arg, "o64") == 0)
316f5878 10619 mips_abi = O64_ABI;
e013f690 10620 else if (strcmp (arg, "n32") == 0)
316f5878 10621 mips_abi = N32_ABI;
e013f690
TS
10622 else if (strcmp (arg, "64") == 0)
10623 {
316f5878 10624 mips_abi = N64_ABI;
e013f690
TS
10625 if (! support_64bit_objects())
10626 as_fatal (_("No compiled in support for 64 bit object file "
10627 "format"));
10628 }
10629 else if (strcmp (arg, "eabi") == 0)
316f5878 10630 mips_abi = EABI_ABI;
e013f690 10631 else
da0e507f
TS
10632 {
10633 as_fatal (_("invalid abi -mabi=%s"), arg);
10634 return 0;
10635 }
252b5132 10636 break;
e013f690 10637#endif /* OBJ_ELF */
252b5132 10638
6b76fefe 10639 case OPTION_M7000_HILO_FIX:
b34976b6 10640 mips_7000_hilo_fix = TRUE;
6b76fefe
CM
10641 break;
10642
9ee72ff1 10643 case OPTION_MNO_7000_HILO_FIX:
b34976b6 10644 mips_7000_hilo_fix = FALSE;
6b76fefe
CM
10645 break;
10646
ecb4347a
DJ
10647#ifdef OBJ_ELF
10648 case OPTION_MDEBUG:
b34976b6 10649 mips_flag_mdebug = TRUE;
ecb4347a
DJ
10650 break;
10651
10652 case OPTION_NO_MDEBUG:
b34976b6 10653 mips_flag_mdebug = FALSE;
ecb4347a 10654 break;
dcd410fe
RO
10655
10656 case OPTION_PDR:
10657 mips_flag_pdr = TRUE;
10658 break;
10659
10660 case OPTION_NO_PDR:
10661 mips_flag_pdr = FALSE;
10662 break;
ecb4347a
DJ
10663#endif /* OBJ_ELF */
10664
252b5132
RH
10665 default:
10666 return 0;
10667 }
10668
10669 return 1;
10670}
316f5878
RS
10671\f
10672/* Set up globals to generate code for the ISA or processor
10673 described by INFO. */
252b5132 10674
252b5132 10675static void
17a2f251 10676mips_set_architecture (const struct mips_cpu_info *info)
252b5132 10677{
316f5878 10678 if (info != 0)
252b5132 10679 {
fef14a42
TS
10680 file_mips_arch = info->cpu;
10681 mips_opts.arch = info->cpu;
316f5878 10682 mips_opts.isa = info->isa;
252b5132 10683 }
252b5132
RH
10684}
10685
252b5132 10686
316f5878 10687/* Likewise for tuning. */
252b5132 10688
316f5878 10689static void
17a2f251 10690mips_set_tune (const struct mips_cpu_info *info)
316f5878
RS
10691{
10692 if (info != 0)
fef14a42 10693 mips_tune = info->cpu;
316f5878 10694}
80cc45a5 10695
34ba82a8 10696
252b5132 10697void
17a2f251 10698mips_after_parse_args (void)
e9670677 10699{
fef14a42
TS
10700 const struct mips_cpu_info *arch_info = 0;
10701 const struct mips_cpu_info *tune_info = 0;
10702
e9670677
MR
10703 /* GP relative stuff not working for PE */
10704 if (strncmp (TARGET_OS, "pe", 2) == 0
10705 && g_switch_value != 0)
10706 {
10707 if (g_switch_seen)
10708 as_bad (_("-G not supported in this configuration."));
10709 g_switch_value = 0;
10710 }
10711
cac012d6
AO
10712 if (mips_abi == NO_ABI)
10713 mips_abi = MIPS_DEFAULT_ABI;
10714
22923709
RS
10715 /* The following code determines the architecture and register size.
10716 Similar code was added to GCC 3.3 (see override_options() in
10717 config/mips/mips.c). The GAS and GCC code should be kept in sync
10718 as much as possible. */
e9670677 10719
316f5878 10720 if (mips_arch_string != 0)
fef14a42 10721 arch_info = mips_parse_cpu ("-march", mips_arch_string);
e9670677 10722
316f5878 10723 if (file_mips_isa != ISA_UNKNOWN)
e9670677 10724 {
316f5878 10725 /* Handle -mipsN. At this point, file_mips_isa contains the
fef14a42 10726 ISA level specified by -mipsN, while arch_info->isa contains
316f5878 10727 the -march selection (if any). */
fef14a42 10728 if (arch_info != 0)
e9670677 10729 {
316f5878
RS
10730 /* -march takes precedence over -mipsN, since it is more descriptive.
10731 There's no harm in specifying both as long as the ISA levels
10732 are the same. */
fef14a42 10733 if (file_mips_isa != arch_info->isa)
316f5878
RS
10734 as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
10735 mips_cpu_info_from_isa (file_mips_isa)->name,
fef14a42 10736 mips_cpu_info_from_isa (arch_info->isa)->name);
e9670677 10737 }
316f5878 10738 else
fef14a42 10739 arch_info = mips_cpu_info_from_isa (file_mips_isa);
e9670677
MR
10740 }
10741
fef14a42
TS
10742 if (arch_info == 0)
10743 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
e9670677 10744
fef14a42 10745 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
316f5878 10746 as_bad ("-march=%s is not compatible with the selected ABI",
fef14a42
TS
10747 arch_info->name);
10748
10749 mips_set_architecture (arch_info);
10750
10751 /* Optimize for file_mips_arch, unless -mtune selects a different processor. */
10752 if (mips_tune_string != 0)
10753 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
e9670677 10754
fef14a42
TS
10755 if (tune_info == 0)
10756 mips_set_tune (arch_info);
10757 else
10758 mips_set_tune (tune_info);
e9670677 10759
316f5878 10760 if (file_mips_gp32 >= 0)
e9670677 10761 {
316f5878
RS
10762 /* The user specified the size of the integer registers. Make sure
10763 it agrees with the ABI and ISA. */
10764 if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
10765 as_bad (_("-mgp64 used with a 32-bit processor"));
10766 else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
10767 as_bad (_("-mgp32 used with a 64-bit ABI"));
10768 else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
10769 as_bad (_("-mgp64 used with a 32-bit ABI"));
e9670677
MR
10770 }
10771 else
10772 {
316f5878
RS
10773 /* Infer the integer register size from the ABI and processor.
10774 Restrict ourselves to 32-bit registers if that's all the
10775 processor has, or if the ABI cannot handle 64-bit registers. */
10776 file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
10777 || !ISA_HAS_64BIT_REGS (mips_opts.isa));
e9670677
MR
10778 }
10779
316f5878
RS
10780 /* ??? GAS treats single-float processors as though they had 64-bit
10781 float registers (although it complains when double-precision
10782 instructions are used). As things stand, saying they have 32-bit
10783 registers would lead to spurious "register must be even" messages.
10784 So here we assume float registers are always the same size as
10785 integer ones, unless the user says otherwise. */
10786 if (file_mips_fp32 < 0)
10787 file_mips_fp32 = file_mips_gp32;
e9670677 10788
316f5878 10789 /* End of GCC-shared inference code. */
e9670677 10790
17a2f251
TS
10791 /* This flag is set when we have a 64-bit capable CPU but use only
10792 32-bit wide registers. Note that EABI does not use it. */
10793 if (ISA_HAS_64BIT_REGS (mips_opts.isa)
10794 && ((mips_abi == NO_ABI && file_mips_gp32 == 1)
10795 || mips_abi == O32_ABI))
316f5878 10796 mips_32bitmode = 1;
e9670677
MR
10797
10798 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
10799 as_bad (_("trap exception not supported at ISA 1"));
10800
e9670677
MR
10801 /* If the selected architecture includes support for ASEs, enable
10802 generation of code for them. */
a4672219 10803 if (mips_opts.mips16 == -1)
fef14a42 10804 mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_arch)) ? 1 : 0;
ffdefa66 10805 if (mips_opts.ase_mips3d == -1)
fef14a42 10806 mips_opts.ase_mips3d = (CPU_HAS_MIPS3D (file_mips_arch)) ? 1 : 0;
ffdefa66 10807 if (mips_opts.ase_mdmx == -1)
fef14a42 10808 mips_opts.ase_mdmx = (CPU_HAS_MDMX (file_mips_arch)) ? 1 : 0;
e9670677 10809
e9670677 10810 file_mips_isa = mips_opts.isa;
a4672219 10811 file_ase_mips16 = mips_opts.mips16;
e9670677
MR
10812 file_ase_mips3d = mips_opts.ase_mips3d;
10813 file_ase_mdmx = mips_opts.ase_mdmx;
10814 mips_opts.gp32 = file_mips_gp32;
10815 mips_opts.fp32 = file_mips_fp32;
10816
ecb4347a
DJ
10817 if (mips_flag_mdebug < 0)
10818 {
10819#ifdef OBJ_MAYBE_ECOFF
10820 if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
10821 mips_flag_mdebug = 1;
10822 else
10823#endif /* OBJ_MAYBE_ECOFF */
10824 mips_flag_mdebug = 0;
10825 }
e9670677
MR
10826}
10827\f
10828void
17a2f251 10829mips_init_after_args (void)
252b5132
RH
10830{
10831 /* initialize opcodes */
10832 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
beae10d5 10833 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
252b5132
RH
10834}
10835
10836long
17a2f251 10837md_pcrel_from (fixS *fixP)
252b5132 10838{
a7ebbfdf
TS
10839 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
10840 switch (fixP->fx_r_type)
10841 {
10842 case BFD_RELOC_16_PCREL_S2:
10843 case BFD_RELOC_MIPS_JMP:
10844 /* Return the address of the delay slot. */
10845 return addr + 4;
10846 default:
10847 return addr;
10848 }
252b5132
RH
10849}
10850
252b5132
RH
10851/* This is called before the symbol table is processed. In order to
10852 work with gcc when using mips-tfile, we must keep all local labels.
10853 However, in other cases, we want to discard them. If we were
10854 called with -g, but we didn't see any debugging information, it may
10855 mean that gcc is smuggling debugging information through to
10856 mips-tfile, in which case we must generate all local labels. */
10857
10858void
17a2f251 10859mips_frob_file_before_adjust (void)
252b5132
RH
10860{
10861#ifndef NO_ECOFF_DEBUGGING
10862 if (ECOFF_DEBUGGING
10863 && mips_debug != 0
10864 && ! ecoff_debugging_seen)
10865 flag_keep_locals = 1;
10866#endif
10867}
10868
10869/* Sort any unmatched HI16_S relocs so that they immediately precede
94f592af 10870 the corresponding LO reloc. This is called before md_apply_fix3 and
252b5132
RH
10871 tc_gen_reloc. Unmatched HI16_S relocs can only be generated by
10872 explicit use of the %hi modifier. */
10873
10874void
17a2f251 10875mips_frob_file (void)
252b5132
RH
10876{
10877 struct mips_hi_fixup *l;
10878
10879 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
10880 {
10881 segment_info_type *seginfo;
10882 int pass;
10883
5919d012 10884 assert (reloc_needs_lo_p (l->fixp->fx_r_type));
252b5132 10885
5919d012
RS
10886 /* If a GOT16 relocation turns out to be against a global symbol,
10887 there isn't supposed to be a matching LO. */
10888 if (l->fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
10889 && !pic_need_relax (l->fixp->fx_addsy, l->seg))
10890 continue;
10891
10892 /* Check quickly whether the next fixup happens to be a matching %lo. */
10893 if (fixup_has_matching_lo_p (l->fixp))
252b5132
RH
10894 continue;
10895
10896 /* Look through the fixups for this segment for a matching %lo.
10897 When we find one, move the %hi just in front of it. We do
10898 this in two passes. In the first pass, we try to find a
10899 unique %lo. In the second pass, we permit multiple %hi
10900 relocs for a single %lo (this is a GNU extension). */
10901 seginfo = seg_info (l->seg);
10902 for (pass = 0; pass < 2; pass++)
10903 {
10904 fixS *f, *prev;
10905
10906 prev = NULL;
10907 for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
10908 {
10909 /* Check whether this is a %lo fixup which matches l->fixp. */
10910 if (f->fx_r_type == BFD_RELOC_LO16
10911 && f->fx_addsy == l->fixp->fx_addsy
10912 && f->fx_offset == l->fixp->fx_offset
10913 && (pass == 1
10914 || prev == NULL
5919d012
RS
10915 || !reloc_needs_lo_p (prev->fx_r_type)
10916 || !fixup_has_matching_lo_p (prev)))
252b5132
RH
10917 {
10918 fixS **pf;
10919
10920 /* Move l->fixp before f. */
10921 for (pf = &seginfo->fix_root;
10922 *pf != l->fixp;
10923 pf = &(*pf)->fx_next)
10924 assert (*pf != NULL);
10925
10926 *pf = l->fixp->fx_next;
10927
10928 l->fixp->fx_next = f;
10929 if (prev == NULL)
10930 seginfo->fix_root = l->fixp;
10931 else
10932 prev->fx_next = l->fixp;
10933
10934 break;
10935 }
10936
10937 prev = f;
10938 }
10939
10940 if (f != NULL)
10941 break;
10942
10943#if 0 /* GCC code motion plus incomplete dead code elimination
10944 can leave a %hi without a %lo. */
10945 if (pass == 1)
10946 as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
10947 _("Unmatched %%hi reloc"));
10948#endif
10949 }
10950 }
10951}
10952
10953/* When generating embedded PIC code we need to use a special
10954 relocation to represent the difference of two symbols in the .text
10955 section (switch tables use a difference of this sort). See
10956 include/coff/mips.h for details. This macro checks whether this
10957 fixup requires the special reloc. */
10958#define SWITCH_TABLE(fixp) \
10959 ((fixp)->fx_r_type == BFD_RELOC_32 \
bb2d6cd7 10960 && OUTPUT_FLAVOR != bfd_target_elf_flavour \
252b5132
RH
10961 && (fixp)->fx_addsy != NULL \
10962 && (fixp)->fx_subsy != NULL \
10963 && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
10964 && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
10965
10966/* When generating embedded PIC code we must keep all PC relative
10967 relocations, in case the linker has to relax a call. We also need
f6688943
TS
10968 to keep relocations for switch table entries.
10969
10970 We may have combined relocations without symbols in the N32/N64 ABI.
10971 We have to prevent gas from dropping them. */
252b5132 10972
252b5132 10973int
17a2f251 10974mips_force_relocation (fixS *fixp)
252b5132 10975{
ae6063d4 10976 if (generic_force_reloc (fixp))
252b5132
RH
10977 return 1;
10978
f6688943
TS
10979 if (HAVE_NEWABI
10980 && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
10981 && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
10982 || fixp->fx_r_type == BFD_RELOC_HI16_S
10983 || fixp->fx_r_type == BFD_RELOC_LO16))
10984 return 1;
10985
252b5132
RH
10986 return (mips_pic == EMBEDDED_PIC
10987 && (fixp->fx_pcrel
10988 || SWITCH_TABLE (fixp)
10989 || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
10990 || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
10991}
10992
45f8dfe8
AO
10993/* This hook is called before a fix is simplified. We don't really
10994 decide whether to skip a fix here. Rather, we turn global symbols
10995 used as branch targets into local symbols, such that they undergo
10996 simplification. We can only do this if the symbol is defined and
10997 it is in the same section as the branch. If this doesn't hold, we
10998 emit a better error message than just saying the relocation is not
10999 valid for the selected object format.
11000
11001 FIXP is the fix-up we're going to try to simplify, SEG is the
11002 segment in which the fix up occurs. The return value should be
11003 non-zero to indicate the fix-up is valid for further
11004 simplifications. */
11005
11006int
17a2f251 11007mips_validate_fix (struct fix *fixP, asection *seg)
45f8dfe8
AO
11008{
11009 /* There's a lot of discussion on whether it should be possible to
11010 use R_MIPS_PC16 to represent branch relocations. The outcome
11011 seems to be that it can, but gas/bfd are very broken in creating
11012 RELA relocations for this, so for now we only accept branches to
11013 symbols in the same section. Anything else is of dubious value,
11014 since there's no guarantee that at link time the symbol would be
11015 in range. Even for branches to local symbols this is arguably
11016 wrong, since it we assume the symbol is not going to be
11017 overridden, which should be possible per ELF library semantics,
11018 but then, there isn't a dynamic relocation that could be used to
11019 this effect, and the target would likely be out of range as well.
11020
11021 Unfortunately, it seems that there is too much code out there
11022 that relies on branches to symbols that are global to be resolved
11023 as if they were local, like the IRIX tools do, so we do it as
11024 well, but with a warning so that people are reminded to fix their
11025 code. If we ever get back to using R_MIPS_PC16 for branch
11026 targets, this entire block should go away (and probably the
11027 whole function). */
11028
11029 if (fixP->fx_r_type == BFD_RELOC_16_PCREL_S2
11030 && (((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
11031 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
11032 && mips_pic != EMBEDDED_PIC)
11033 || bfd_reloc_type_lookup (stdoutput, BFD_RELOC_16_PCREL_S2) == NULL)
11034 && fixP->fx_addsy)
11035 {
11036 if (! S_IS_DEFINED (fixP->fx_addsy))
11037 {
11038 as_bad_where (fixP->fx_file, fixP->fx_line,
11039 _("Cannot branch to undefined symbol."));
11040 /* Avoid any further errors about this fixup. */
11041 fixP->fx_done = 1;
11042 }
11043 else if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
11044 {
11045 as_bad_where (fixP->fx_file, fixP->fx_line,
11046 _("Cannot branch to symbol in another section."));
11047 fixP->fx_done = 1;
11048 }
11049 else if (S_IS_EXTERNAL (fixP->fx_addsy))
11050 {
11051 symbolS *sym = fixP->fx_addsy;
11052
115695a8
CD
11053 if (mips_pic == SVR4_PIC)
11054 as_warn_where (fixP->fx_file, fixP->fx_line,
11055 _("Pretending global symbol used as branch target is local."));
45f8dfe8
AO
11056
11057 fixP->fx_addsy = symbol_create (S_GET_NAME (sym),
11058 S_GET_SEGMENT (sym),
11059 S_GET_VALUE (sym),
11060 symbol_get_frag (sym));
11061 copy_symbol_attributes (fixP->fx_addsy, sym);
11062 S_CLEAR_EXTERNAL (fixP->fx_addsy);
11063 assert (symbol_resolved_p (sym));
11064 symbol_mark_resolved (fixP->fx_addsy);
11065 }
11066 }
11067
11068 return 1;
11069}
11070
252b5132
RH
11071/* Apply a fixup to the object file. */
11072
94f592af 11073void
17a2f251 11074md_apply_fix3 (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 11075{
874e8986 11076 bfd_byte *buf;
98aa84af 11077 long insn;
ed6fb7bd 11078 static int previous_fx_r_type = 0;
a7ebbfdf 11079 reloc_howto_type *howto;
252b5132 11080
a7ebbfdf
TS
11081 /* We ignore generic BFD relocations we don't know about. */
11082 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
11083 if (! howto)
11084 return;
65551fa4 11085
252b5132
RH
11086 assert (fixP->fx_size == 4
11087 || fixP->fx_r_type == BFD_RELOC_16
11088 || fixP->fx_r_type == BFD_RELOC_64
f6688943
TS
11089 || fixP->fx_r_type == BFD_RELOC_CTOR
11090 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
252b5132 11091 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
a7ebbfdf 11092 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY);
252b5132 11093
a7ebbfdf 11094 buf = (bfd_byte *) (fixP->fx_frag->fr_literal + fixP->fx_where);
252b5132 11095
ed6fb7bd
SC
11096 /* We are not done if this is a composite relocation to set up gp. */
11097 if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel
11098 && !(fixP->fx_r_type == BFD_RELOC_MIPS_SUB
10181a0d
AO
11099 || (fixP->fx_r_type == BFD_RELOC_64
11100 && (previous_fx_r_type == BFD_RELOC_GPREL32
11101 || previous_fx_r_type == BFD_RELOC_GPREL16))
ed6fb7bd
SC
11102 || (previous_fx_r_type == BFD_RELOC_MIPS_SUB
11103 && (fixP->fx_r_type == BFD_RELOC_HI16_S
11104 || fixP->fx_r_type == BFD_RELOC_LO16))))
252b5132 11105 fixP->fx_done = 1;
ed6fb7bd 11106 previous_fx_r_type = fixP->fx_r_type;
252b5132
RH
11107
11108 switch (fixP->fx_r_type)
11109 {
11110 case BFD_RELOC_MIPS_JMP:
e369bcce
TS
11111 case BFD_RELOC_MIPS_SHIFT5:
11112 case BFD_RELOC_MIPS_SHIFT6:
11113 case BFD_RELOC_MIPS_GOT_DISP:
11114 case BFD_RELOC_MIPS_GOT_PAGE:
11115 case BFD_RELOC_MIPS_GOT_OFST:
11116 case BFD_RELOC_MIPS_SUB:
11117 case BFD_RELOC_MIPS_INSERT_A:
11118 case BFD_RELOC_MIPS_INSERT_B:
11119 case BFD_RELOC_MIPS_DELETE:
11120 case BFD_RELOC_MIPS_HIGHEST:
11121 case BFD_RELOC_MIPS_HIGHER:
11122 case BFD_RELOC_MIPS_SCN_DISP:
11123 case BFD_RELOC_MIPS_REL16:
11124 case BFD_RELOC_MIPS_RELGOT:
11125 case BFD_RELOC_MIPS_JALR:
252b5132
RH
11126 case BFD_RELOC_HI16:
11127 case BFD_RELOC_HI16_S:
cdf6fd85 11128 case BFD_RELOC_GPREL16:
252b5132
RH
11129 case BFD_RELOC_MIPS_LITERAL:
11130 case BFD_RELOC_MIPS_CALL16:
11131 case BFD_RELOC_MIPS_GOT16:
cdf6fd85 11132 case BFD_RELOC_GPREL32:
252b5132
RH
11133 case BFD_RELOC_MIPS_GOT_HI16:
11134 case BFD_RELOC_MIPS_GOT_LO16:
11135 case BFD_RELOC_MIPS_CALL_HI16:
11136 case BFD_RELOC_MIPS_CALL_LO16:
11137 case BFD_RELOC_MIPS16_GPREL:
11138 if (fixP->fx_pcrel)
11139 as_bad_where (fixP->fx_file, fixP->fx_line,
11140 _("Invalid PC relative reloc"));
11141 /* Nothing needed to do. The value comes from the reloc entry */
11142 break;
11143
11144 case BFD_RELOC_MIPS16_JMP:
11145 /* We currently always generate a reloc against a symbol, which
11146 means that we don't want an addend even if the symbol is
11147 defined. */
a7ebbfdf 11148 *valP = 0;
252b5132
RH
11149 break;
11150
11151 case BFD_RELOC_PCREL_HI16_S:
11152 /* The addend for this is tricky if it is internal, so we just
11153 do everything here rather than in bfd_install_relocation. */
a7ebbfdf 11154 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && !fixP->fx_done)
bb2d6cd7
GK
11155 break;
11156 if (fixP->fx_addsy
11157 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
252b5132
RH
11158 {
11159 /* For an external symbol adjust by the address to make it
11160 pcrel_offset. We use the address of the RELLO reloc
11161 which follows this one. */
a7ebbfdf 11162 *valP += (fixP->fx_next->fx_frag->fr_address
252b5132
RH
11163 + fixP->fx_next->fx_where);
11164 }
a7ebbfdf 11165 *valP = ((*valP + 0x8000) >> 16) & 0xffff;
252b5132
RH
11166 if (target_big_endian)
11167 buf += 2;
17a2f251 11168 md_number_to_chars (buf, *valP, 2);
252b5132
RH
11169 break;
11170
11171 case BFD_RELOC_PCREL_LO16:
11172 /* The addend for this is tricky if it is internal, so we just
11173 do everything here rather than in bfd_install_relocation. */
a7ebbfdf 11174 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && !fixP->fx_done)
bb2d6cd7
GK
11175 break;
11176 if (fixP->fx_addsy
11177 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
a7ebbfdf 11178 *valP += fixP->fx_frag->fr_address + fixP->fx_where;
252b5132
RH
11179 if (target_big_endian)
11180 buf += 2;
17a2f251 11181 md_number_to_chars (buf, *valP, 2);
252b5132
RH
11182 break;
11183
11184 case BFD_RELOC_64:
11185 /* This is handled like BFD_RELOC_32, but we output a sign
11186 extended value if we are only 32 bits. */
11187 if (fixP->fx_done
11188 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11189 {
11190 if (8 <= sizeof (valueT))
a7ebbfdf 11191 md_number_to_chars (buf, *valP, 8);
252b5132
RH
11192 else
11193 {
a7ebbfdf 11194 valueT hiv;
252b5132 11195
a7ebbfdf 11196 if ((*valP & 0x80000000) != 0)
252b5132
RH
11197 hiv = 0xffffffff;
11198 else
11199 hiv = 0;
a7ebbfdf
TS
11200 md_number_to_chars ((char *)(buf + target_big_endian ? 4 : 0),
11201 *valP, 4);
11202 md_number_to_chars ((char *)(buf + target_big_endian ? 0 : 4),
11203 hiv, 4);
252b5132
RH
11204 }
11205 }
11206 break;
11207
056350c6 11208 case BFD_RELOC_RVA:
252b5132
RH
11209 case BFD_RELOC_32:
11210 /* If we are deleting this reloc entry, we must fill in the
11211 value now. This can happen if we have a .word which is not
11212 resolved when it appears but is later defined. We also need
11213 to fill in the value if this is an embedded PIC switch table
11214 entry. */
11215 if (fixP->fx_done
11216 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
a7ebbfdf 11217 md_number_to_chars (buf, *valP, 4);
252b5132
RH
11218 break;
11219
11220 case BFD_RELOC_16:
11221 /* If we are deleting this reloc entry, we must fill in the
11222 value now. */
11223 assert (fixP->fx_size == 2);
11224 if (fixP->fx_done)
a7ebbfdf 11225 md_number_to_chars (buf, *valP, 2);
252b5132
RH
11226 break;
11227
11228 case BFD_RELOC_LO16:
11229 /* When handling an embedded PIC switch statement, we can wind
11230 up deleting a LO16 reloc. See the 'o' case in mips_ip. */
11231 if (fixP->fx_done)
11232 {
a7ebbfdf 11233 if (*valP + 0x8000 > 0xffff)
252b5132
RH
11234 as_bad_where (fixP->fx_file, fixP->fx_line,
11235 _("relocation overflow"));
252b5132
RH
11236 if (target_big_endian)
11237 buf += 2;
17a2f251 11238 md_number_to_chars (buf, *valP, 2);
252b5132
RH
11239 }
11240 break;
11241
11242 case BFD_RELOC_16_PCREL_S2:
a7ebbfdf 11243 if ((*valP & 0x3) != 0)
cb56d3d3 11244 as_bad_where (fixP->fx_file, fixP->fx_line,
a7ebbfdf 11245 _("Branch to odd address (%lx)"), (long) *valP);
cb56d3d3 11246
252b5132
RH
11247 /*
11248 * We need to save the bits in the instruction since fixup_segment()
11249 * might be deleting the relocation entry (i.e., a branch within
11250 * the current segment).
11251 */
a7ebbfdf 11252 if (! fixP->fx_done)
bb2d6cd7 11253 break;
252b5132
RH
11254
11255 /* update old instruction data */
252b5132
RH
11256 if (target_big_endian)
11257 insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
11258 else
11259 insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
11260
a7ebbfdf
TS
11261 if (*valP + 0x20000 <= 0x3ffff)
11262 {
11263 insn |= (*valP >> 2) & 0xffff;
17a2f251 11264 md_number_to_chars (buf, insn, 4);
a7ebbfdf
TS
11265 }
11266 else if (mips_pic == NO_PIC
11267 && fixP->fx_done
11268 && fixP->fx_frag->fr_address >= text_section->vma
11269 && (fixP->fx_frag->fr_address
11270 < text_section->vma + text_section->_raw_size)
11271 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
11272 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
11273 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
252b5132
RH
11274 {
11275 /* The branch offset is too large. If this is an
11276 unconditional branch, and we are not generating PIC code,
11277 we can convert it to an absolute jump instruction. */
a7ebbfdf
TS
11278 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
11279 insn = 0x0c000000; /* jal */
252b5132 11280 else
a7ebbfdf
TS
11281 insn = 0x08000000; /* j */
11282 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
11283 fixP->fx_done = 0;
11284 fixP->fx_addsy = section_symbol (text_section);
11285 *valP += md_pcrel_from (fixP);
17a2f251 11286 md_number_to_chars (buf, insn, 4);
a7ebbfdf
TS
11287 }
11288 else
11289 {
11290 /* If we got here, we have branch-relaxation disabled,
11291 and there's nothing we can do to fix this instruction
11292 without turning it into a longer sequence. */
11293 as_bad_where (fixP->fx_file, fixP->fx_line,
11294 _("Branch out of range"));
252b5132 11295 }
252b5132
RH
11296 break;
11297
11298 case BFD_RELOC_VTABLE_INHERIT:
11299 fixP->fx_done = 0;
11300 if (fixP->fx_addsy
11301 && !S_IS_DEFINED (fixP->fx_addsy)
11302 && !S_IS_WEAK (fixP->fx_addsy))
11303 S_SET_WEAK (fixP->fx_addsy);
11304 break;
11305
11306 case BFD_RELOC_VTABLE_ENTRY:
11307 fixP->fx_done = 0;
11308 break;
11309
11310 default:
11311 internalError ();
11312 }
a7ebbfdf
TS
11313
11314 /* Remember value for tc_gen_reloc. */
11315 fixP->fx_addnumber = *valP;
252b5132
RH
11316}
11317
11318#if 0
11319void
17a2f251 11320printInsn (unsigned long oc)
252b5132
RH
11321{
11322 const struct mips_opcode *p;
11323 int treg, sreg, dreg, shamt;
11324 short imm;
11325 const char *args;
11326 int i;
11327
11328 for (i = 0; i < NUMOPCODES; ++i)
11329 {
11330 p = &mips_opcodes[i];
11331 if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
11332 {
11333 printf ("%08lx %s\t", oc, p->name);
11334 treg = (oc >> 16) & 0x1f;
11335 sreg = (oc >> 21) & 0x1f;
11336 dreg = (oc >> 11) & 0x1f;
11337 shamt = (oc >> 6) & 0x1f;
11338 imm = oc;
11339 for (args = p->args;; ++args)
11340 {
11341 switch (*args)
11342 {
11343 case '\0':
11344 printf ("\n");
11345 break;
11346
11347 case ',':
11348 case '(':
11349 case ')':
11350 printf ("%c", *args);
11351 continue;
11352
11353 case 'r':
11354 assert (treg == sreg);
11355 printf ("$%d,$%d", treg, sreg);
11356 continue;
11357
11358 case 'd':
11359 case 'G':
11360 printf ("$%d", dreg);
11361 continue;
11362
11363 case 't':
11364 case 'E':
11365 printf ("$%d", treg);
11366 continue;
11367
11368 case 'k':
11369 printf ("0x%x", treg);
11370 continue;
11371
11372 case 'b':
11373 case 's':
11374 printf ("$%d", sreg);
11375 continue;
11376
11377 case 'a':
11378 printf ("0x%08lx", oc & 0x1ffffff);
11379 continue;
11380
11381 case 'i':
11382 case 'j':
11383 case 'o':
11384 case 'u':
11385 printf ("%d", imm);
11386 continue;
11387
11388 case '<':
11389 case '>':
11390 printf ("$%d", shamt);
11391 continue;
11392
11393 default:
11394 internalError ();
11395 }
11396 break;
11397 }
11398 return;
11399 }
11400 }
11401 printf (_("%08lx UNDEFINED\n"), oc);
11402}
11403#endif
11404
11405static symbolS *
17a2f251 11406get_symbol (void)
252b5132
RH
11407{
11408 int c;
11409 char *name;
11410 symbolS *p;
11411
11412 name = input_line_pointer;
11413 c = get_symbol_end ();
11414 p = (symbolS *) symbol_find_or_make (name);
11415 *input_line_pointer = c;
11416 return p;
11417}
11418
11419/* Align the current frag to a given power of two. The MIPS assembler
11420 also automatically adjusts any preceding label. */
11421
11422static void
17a2f251 11423mips_align (int to, int fill, symbolS *label)
252b5132 11424{
b34976b6 11425 mips_emit_delays (FALSE);
252b5132
RH
11426 frag_align (to, fill, 0);
11427 record_alignment (now_seg, to);
11428 if (label != NULL)
11429 {
11430 assert (S_GET_SEGMENT (label) == now_seg);
49309057 11431 symbol_set_frag (label, frag_now);
252b5132
RH
11432 S_SET_VALUE (label, (valueT) frag_now_fix ());
11433 }
11434}
11435
11436/* Align to a given power of two. .align 0 turns off the automatic
11437 alignment used by the data creating pseudo-ops. */
11438
11439static void
17a2f251 11440s_align (int x ATTRIBUTE_UNUSED)
252b5132
RH
11441{
11442 register int temp;
11443 register long temp_fill;
11444 long max_alignment = 15;
11445
11446 /*
11447
67c1ffbe 11448 o Note that the assembler pulls down any immediately preceding label
252b5132
RH
11449 to the aligned address.
11450 o It's not documented but auto alignment is reinstated by
11451 a .align pseudo instruction.
11452 o Note also that after auto alignment is turned off the mips assembler
11453 issues an error on attempt to assemble an improperly aligned data item.
11454 We don't.
11455
11456 */
11457
11458 temp = get_absolute_expression ();
11459 if (temp > max_alignment)
11460 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
11461 else if (temp < 0)
11462 {
11463 as_warn (_("Alignment negative: 0 assumed."));
11464 temp = 0;
11465 }
11466 if (*input_line_pointer == ',')
11467 {
f9419b05 11468 ++input_line_pointer;
252b5132
RH
11469 temp_fill = get_absolute_expression ();
11470 }
11471 else
11472 temp_fill = 0;
11473 if (temp)
11474 {
11475 auto_align = 1;
11476 mips_align (temp, (int) temp_fill,
11477 insn_labels != NULL ? insn_labels->label : NULL);
11478 }
11479 else
11480 {
11481 auto_align = 0;
11482 }
11483
11484 demand_empty_rest_of_line ();
11485}
11486
11487void
17a2f251 11488mips_flush_pending_output (void)
252b5132 11489{
b34976b6 11490 mips_emit_delays (FALSE);
252b5132
RH
11491 mips_clear_insn_labels ();
11492}
11493
11494static void
17a2f251 11495s_change_sec (int sec)
252b5132
RH
11496{
11497 segT seg;
11498
11499 /* When generating embedded PIC code, we only use the .text, .lit8,
11500 .sdata and .sbss sections. We change the .data and .rdata
11501 pseudo-ops to use .sdata. */
11502 if (mips_pic == EMBEDDED_PIC
11503 && (sec == 'd' || sec == 'r'))
11504 sec = 's';
11505
11506#ifdef OBJ_ELF
11507 /* The ELF backend needs to know that we are changing sections, so
11508 that .previous works correctly. We could do something like check
b6ff326e 11509 for an obj_section_change_hook macro, but that might be confusing
252b5132
RH
11510 as it would not be appropriate to use it in the section changing
11511 functions in read.c, since obj-elf.c intercepts those. FIXME:
11512 This should be cleaner, somehow. */
11513 obj_elf_section_change_hook ();
11514#endif
11515
b34976b6 11516 mips_emit_delays (FALSE);
252b5132
RH
11517 switch (sec)
11518 {
11519 case 't':
11520 s_text (0);
11521 break;
11522 case 'd':
11523 s_data (0);
11524 break;
11525 case 'b':
11526 subseg_set (bss_section, (subsegT) get_absolute_expression ());
11527 demand_empty_rest_of_line ();
11528 break;
11529
11530 case 'r':
4d0d148d
TS
11531 seg = subseg_new (RDATA_SECTION_NAME,
11532 (subsegT) get_absolute_expression ());
11533 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
252b5132 11534 {
4d0d148d
TS
11535 bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
11536 | SEC_READONLY | SEC_RELOC
11537 | SEC_DATA));
11538 if (strcmp (TARGET_OS, "elf") != 0)
11539 record_alignment (seg, 4);
252b5132 11540 }
4d0d148d 11541 demand_empty_rest_of_line ();
252b5132
RH
11542 break;
11543
11544 case 's':
4d0d148d
TS
11545 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
11546 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
252b5132 11547 {
4d0d148d
TS
11548 bfd_set_section_flags (stdoutput, seg,
11549 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
11550 if (strcmp (TARGET_OS, "elf") != 0)
11551 record_alignment (seg, 4);
252b5132 11552 }
4d0d148d
TS
11553 demand_empty_rest_of_line ();
11554 break;
252b5132
RH
11555 }
11556
11557 auto_align = 1;
11558}
b34976b6 11559
cca86cc8 11560void
17a2f251 11561s_change_section (int ignore ATTRIBUTE_UNUSED)
cca86cc8 11562{
7ed4a06a 11563#ifdef OBJ_ELF
cca86cc8
SC
11564 char *section_name;
11565 char c;
684022ea 11566 char next_c = 0;
cca86cc8
SC
11567 int section_type;
11568 int section_flag;
11569 int section_entry_size;
11570 int section_alignment;
b34976b6 11571
7ed4a06a
TS
11572 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11573 return;
11574
cca86cc8
SC
11575 section_name = input_line_pointer;
11576 c = get_symbol_end ();
a816d1ed
AO
11577 if (c)
11578 next_c = *(input_line_pointer + 1);
cca86cc8 11579
4cf0dd0d
TS
11580 /* Do we have .section Name<,"flags">? */
11581 if (c != ',' || (c == ',' && next_c == '"'))
cca86cc8 11582 {
4cf0dd0d
TS
11583 /* just after name is now '\0'. */
11584 *input_line_pointer = c;
cca86cc8
SC
11585 input_line_pointer = section_name;
11586 obj_elf_section (ignore);
11587 return;
11588 }
11589 input_line_pointer++;
11590
11591 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
11592 if (c == ',')
11593 section_type = get_absolute_expression ();
11594 else
11595 section_type = 0;
11596 if (*input_line_pointer++ == ',')
11597 section_flag = get_absolute_expression ();
11598 else
11599 section_flag = 0;
11600 if (*input_line_pointer++ == ',')
11601 section_entry_size = get_absolute_expression ();
11602 else
11603 section_entry_size = 0;
11604 if (*input_line_pointer++ == ',')
11605 section_alignment = get_absolute_expression ();
11606 else
11607 section_alignment = 0;
11608
a816d1ed
AO
11609 section_name = xstrdup (section_name);
11610
8ab8a5c8
RS
11611 /* When using the generic form of .section (as implemented by obj-elf.c),
11612 there's no way to set the section type to SHT_MIPS_DWARF. Users have
11613 traditionally had to fall back on the more common @progbits instead.
11614
11615 There's nothing really harmful in this, since bfd will correct
11616 SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file. But it
11617 means that, for backwards compatibiltiy, the special_section entries
11618 for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
11619
11620 Even so, we shouldn't force users of the MIPS .section syntax to
11621 incorrectly label the sections as SHT_PROGBITS. The best compromise
11622 seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
11623 generic type-checking code. */
11624 if (section_type == SHT_MIPS_DWARF)
11625 section_type = SHT_PROGBITS;
11626
cca86cc8
SC
11627 obj_elf_change_section (section_name, section_type, section_flag,
11628 section_entry_size, 0, 0, 0);
a816d1ed
AO
11629
11630 if (now_seg->name != section_name)
11631 free (section_name);
7ed4a06a 11632#endif /* OBJ_ELF */
cca86cc8 11633}
252b5132
RH
11634
11635void
17a2f251 11636mips_enable_auto_align (void)
252b5132
RH
11637{
11638 auto_align = 1;
11639}
11640
11641static void
17a2f251 11642s_cons (int log_size)
252b5132
RH
11643{
11644 symbolS *label;
11645
11646 label = insn_labels != NULL ? insn_labels->label : NULL;
b34976b6 11647 mips_emit_delays (FALSE);
252b5132
RH
11648 if (log_size > 0 && auto_align)
11649 mips_align (log_size, 0, label);
11650 mips_clear_insn_labels ();
11651 cons (1 << log_size);
11652}
11653
11654static void
17a2f251 11655s_float_cons (int type)
252b5132
RH
11656{
11657 symbolS *label;
11658
11659 label = insn_labels != NULL ? insn_labels->label : NULL;
11660
b34976b6 11661 mips_emit_delays (FALSE);
252b5132
RH
11662
11663 if (auto_align)
49309057
ILT
11664 {
11665 if (type == 'd')
11666 mips_align (3, 0, label);
11667 else
11668 mips_align (2, 0, label);
11669 }
252b5132
RH
11670
11671 mips_clear_insn_labels ();
11672
11673 float_cons (type);
11674}
11675
11676/* Handle .globl. We need to override it because on Irix 5 you are
11677 permitted to say
11678 .globl foo .text
11679 where foo is an undefined symbol, to mean that foo should be
11680 considered to be the address of a function. */
11681
11682static void
17a2f251 11683s_mips_globl (int x ATTRIBUTE_UNUSED)
252b5132
RH
11684{
11685 char *name;
11686 int c;
11687 symbolS *symbolP;
11688 flagword flag;
11689
11690 name = input_line_pointer;
11691 c = get_symbol_end ();
11692 symbolP = symbol_find_or_make (name);
11693 *input_line_pointer = c;
11694 SKIP_WHITESPACE ();
11695
11696 /* On Irix 5, every global symbol that is not explicitly labelled as
11697 being a function is apparently labelled as being an object. */
11698 flag = BSF_OBJECT;
11699
11700 if (! is_end_of_line[(unsigned char) *input_line_pointer])
11701 {
11702 char *secname;
11703 asection *sec;
11704
11705 secname = input_line_pointer;
11706 c = get_symbol_end ();
11707 sec = bfd_get_section_by_name (stdoutput, secname);
11708 if (sec == NULL)
11709 as_bad (_("%s: no such section"), secname);
11710 *input_line_pointer = c;
11711
11712 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
11713 flag = BSF_FUNCTION;
11714 }
11715
49309057 11716 symbol_get_bfdsym (symbolP)->flags |= flag;
252b5132
RH
11717
11718 S_SET_EXTERNAL (symbolP);
11719 demand_empty_rest_of_line ();
11720}
11721
11722static void
17a2f251 11723s_option (int x ATTRIBUTE_UNUSED)
252b5132
RH
11724{
11725 char *opt;
11726 char c;
11727
11728 opt = input_line_pointer;
11729 c = get_symbol_end ();
11730
11731 if (*opt == 'O')
11732 {
11733 /* FIXME: What does this mean? */
11734 }
11735 else if (strncmp (opt, "pic", 3) == 0)
11736 {
11737 int i;
11738
11739 i = atoi (opt + 3);
11740 if (i == 0)
11741 mips_pic = NO_PIC;
11742 else if (i == 2)
143d77c5 11743 {
252b5132 11744 mips_pic = SVR4_PIC;
143d77c5
EC
11745 mips_abicalls = TRUE;
11746 }
252b5132
RH
11747 else
11748 as_bad (_(".option pic%d not supported"), i);
11749
4d0d148d 11750 if (mips_pic == SVR4_PIC)
252b5132
RH
11751 {
11752 if (g_switch_seen && g_switch_value != 0)
11753 as_warn (_("-G may not be used with SVR4 PIC code"));
11754 g_switch_value = 0;
11755 bfd_set_gp_size (stdoutput, 0);
11756 }
11757 }
11758 else
11759 as_warn (_("Unrecognized option \"%s\""), opt);
11760
11761 *input_line_pointer = c;
11762 demand_empty_rest_of_line ();
11763}
11764
11765/* This structure is used to hold a stack of .set values. */
11766
e972090a
NC
11767struct mips_option_stack
11768{
252b5132
RH
11769 struct mips_option_stack *next;
11770 struct mips_set_options options;
11771};
11772
11773static struct mips_option_stack *mips_opts_stack;
11774
11775/* Handle the .set pseudo-op. */
11776
11777static void
17a2f251 11778s_mipsset (int x ATTRIBUTE_UNUSED)
252b5132
RH
11779{
11780 char *name = input_line_pointer, ch;
11781
11782 while (!is_end_of_line[(unsigned char) *input_line_pointer])
f9419b05 11783 ++input_line_pointer;
252b5132
RH
11784 ch = *input_line_pointer;
11785 *input_line_pointer = '\0';
11786
11787 if (strcmp (name, "reorder") == 0)
11788 {
11789 if (mips_opts.noreorder && prev_nop_frag != NULL)
11790 {
11791 /* If we still have pending nops, we can discard them. The
11792 usual nop handling will insert any that are still
bdaaa2e1 11793 needed. */
252b5132
RH
11794 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11795 * (mips_opts.mips16 ? 2 : 4));
11796 prev_nop_frag = NULL;
11797 }
11798 mips_opts.noreorder = 0;
11799 }
11800 else if (strcmp (name, "noreorder") == 0)
11801 {
b34976b6 11802 mips_emit_delays (TRUE);
252b5132
RH
11803 mips_opts.noreorder = 1;
11804 mips_any_noreorder = 1;
11805 }
11806 else if (strcmp (name, "at") == 0)
11807 {
11808 mips_opts.noat = 0;
11809 }
11810 else if (strcmp (name, "noat") == 0)
11811 {
11812 mips_opts.noat = 1;
11813 }
11814 else if (strcmp (name, "macro") == 0)
11815 {
11816 mips_opts.warn_about_macros = 0;
11817 }
11818 else if (strcmp (name, "nomacro") == 0)
11819 {
11820 if (mips_opts.noreorder == 0)
11821 as_bad (_("`noreorder' must be set before `nomacro'"));
11822 mips_opts.warn_about_macros = 1;
11823 }
11824 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
11825 {
11826 mips_opts.nomove = 0;
11827 }
11828 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
11829 {
11830 mips_opts.nomove = 1;
11831 }
11832 else if (strcmp (name, "bopt") == 0)
11833 {
11834 mips_opts.nobopt = 0;
11835 }
11836 else if (strcmp (name, "nobopt") == 0)
11837 {
11838 mips_opts.nobopt = 1;
11839 }
11840 else if (strcmp (name, "mips16") == 0
11841 || strcmp (name, "MIPS-16") == 0)
11842 mips_opts.mips16 = 1;
11843 else if (strcmp (name, "nomips16") == 0
11844 || strcmp (name, "noMIPS-16") == 0)
11845 mips_opts.mips16 = 0;
1f25f5d3
CD
11846 else if (strcmp (name, "mips3d") == 0)
11847 mips_opts.ase_mips3d = 1;
11848 else if (strcmp (name, "nomips3d") == 0)
11849 mips_opts.ase_mips3d = 0;
a4672219
TS
11850 else if (strcmp (name, "mdmx") == 0)
11851 mips_opts.ase_mdmx = 1;
11852 else if (strcmp (name, "nomdmx") == 0)
11853 mips_opts.ase_mdmx = 0;
1a2c1fad 11854 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
252b5132 11855 {
af7ee8bf 11856 int reset = 0;
252b5132 11857
1a2c1fad
CD
11858 /* Permit the user to change the ISA and architecture on the fly.
11859 Needless to say, misuse can cause serious problems. */
af7ee8bf
CD
11860 if (strcmp (name, "mips0") == 0)
11861 {
11862 reset = 1;
11863 mips_opts.isa = file_mips_isa;
11864 }
11865 else if (strcmp (name, "mips1") == 0)
11866 mips_opts.isa = ISA_MIPS1;
11867 else if (strcmp (name, "mips2") == 0)
11868 mips_opts.isa = ISA_MIPS2;
11869 else if (strcmp (name, "mips3") == 0)
11870 mips_opts.isa = ISA_MIPS3;
11871 else if (strcmp (name, "mips4") == 0)
11872 mips_opts.isa = ISA_MIPS4;
11873 else if (strcmp (name, "mips5") == 0)
11874 mips_opts.isa = ISA_MIPS5;
11875 else if (strcmp (name, "mips32") == 0)
11876 mips_opts.isa = ISA_MIPS32;
11877 else if (strcmp (name, "mips32r2") == 0)
11878 mips_opts.isa = ISA_MIPS32R2;
11879 else if (strcmp (name, "mips64") == 0)
11880 mips_opts.isa = ISA_MIPS64;
5f74bc13
CD
11881 else if (strcmp (name, "mips64r2") == 0)
11882 mips_opts.isa = ISA_MIPS64R2;
1a2c1fad
CD
11883 else if (strcmp (name, "arch=default") == 0)
11884 {
11885 reset = 1;
11886 mips_opts.arch = file_mips_arch;
11887 mips_opts.isa = file_mips_isa;
11888 }
11889 else if (strncmp (name, "arch=", 5) == 0)
11890 {
11891 const struct mips_cpu_info *p;
11892
11893 p = mips_parse_cpu("internal use", name + 5);
11894 if (!p)
11895 as_bad (_("unknown architecture %s"), name + 5);
11896 else
11897 {
11898 mips_opts.arch = p->cpu;
11899 mips_opts.isa = p->isa;
11900 }
11901 }
af7ee8bf
CD
11902 else
11903 as_bad (_("unknown ISA level %s"), name + 4);
11904
11905 switch (mips_opts.isa)
98d3f06f
KH
11906 {
11907 case 0:
98d3f06f 11908 break;
af7ee8bf
CD
11909 case ISA_MIPS1:
11910 case ISA_MIPS2:
11911 case ISA_MIPS32:
11912 case ISA_MIPS32R2:
98d3f06f
KH
11913 mips_opts.gp32 = 1;
11914 mips_opts.fp32 = 1;
11915 break;
af7ee8bf
CD
11916 case ISA_MIPS3:
11917 case ISA_MIPS4:
11918 case ISA_MIPS5:
11919 case ISA_MIPS64:
5f74bc13 11920 case ISA_MIPS64R2:
98d3f06f
KH
11921 mips_opts.gp32 = 0;
11922 mips_opts.fp32 = 0;
11923 break;
11924 default:
11925 as_bad (_("unknown ISA level %s"), name + 4);
11926 break;
11927 }
af7ee8bf 11928 if (reset)
98d3f06f 11929 {
af7ee8bf
CD
11930 mips_opts.gp32 = file_mips_gp32;
11931 mips_opts.fp32 = file_mips_fp32;
98d3f06f 11932 }
252b5132
RH
11933 }
11934 else if (strcmp (name, "autoextend") == 0)
11935 mips_opts.noautoextend = 0;
11936 else if (strcmp (name, "noautoextend") == 0)
11937 mips_opts.noautoextend = 1;
11938 else if (strcmp (name, "push") == 0)
11939 {
11940 struct mips_option_stack *s;
11941
11942 s = (struct mips_option_stack *) xmalloc (sizeof *s);
11943 s->next = mips_opts_stack;
11944 s->options = mips_opts;
11945 mips_opts_stack = s;
11946 }
11947 else if (strcmp (name, "pop") == 0)
11948 {
11949 struct mips_option_stack *s;
11950
11951 s = mips_opts_stack;
11952 if (s == NULL)
11953 as_bad (_(".set pop with no .set push"));
11954 else
11955 {
11956 /* If we're changing the reorder mode we need to handle
11957 delay slots correctly. */
11958 if (s->options.noreorder && ! mips_opts.noreorder)
b34976b6 11959 mips_emit_delays (TRUE);
252b5132
RH
11960 else if (! s->options.noreorder && mips_opts.noreorder)
11961 {
11962 if (prev_nop_frag != NULL)
11963 {
11964 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11965 * (mips_opts.mips16 ? 2 : 4));
11966 prev_nop_frag = NULL;
11967 }
11968 }
11969
11970 mips_opts = s->options;
11971 mips_opts_stack = s->next;
11972 free (s);
11973 }
11974 }
11975 else
11976 {
11977 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
11978 }
11979 *input_line_pointer = ch;
11980 demand_empty_rest_of_line ();
11981}
11982
11983/* Handle the .abicalls pseudo-op. I believe this is equivalent to
11984 .option pic2. It means to generate SVR4 PIC calls. */
11985
11986static void
17a2f251 11987s_abicalls (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
11988{
11989 mips_pic = SVR4_PIC;
143d77c5 11990 mips_abicalls = TRUE;
4d0d148d
TS
11991
11992 if (g_switch_seen && g_switch_value != 0)
11993 as_warn (_("-G may not be used with SVR4 PIC code"));
11994 g_switch_value = 0;
11995
252b5132
RH
11996 bfd_set_gp_size (stdoutput, 0);
11997 demand_empty_rest_of_line ();
11998}
11999
12000/* Handle the .cpload pseudo-op. This is used when generating SVR4
12001 PIC code. It sets the $gp register for the function based on the
12002 function address, which is in the register named in the argument.
12003 This uses a relocation against _gp_disp, which is handled specially
12004 by the linker. The result is:
12005 lui $gp,%hi(_gp_disp)
12006 addiu $gp,$gp,%lo(_gp_disp)
12007 addu $gp,$gp,.cpload argument
12008 The .cpload argument is normally $25 == $t9. */
12009
12010static void
17a2f251 12011s_cpload (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
12012{
12013 expressionS ex;
252b5132 12014
6478892d
TS
12015 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12016 .cpload is ignored. */
12017 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
12018 {
12019 s_ignore (0);
12020 return;
12021 }
12022
d3ecfc59 12023 /* .cpload should be in a .set noreorder section. */
252b5132
RH
12024 if (mips_opts.noreorder == 0)
12025 as_warn (_(".cpload not in noreorder section"));
12026
12027 ex.X_op = O_symbol;
12028 ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
12029 ex.X_op_symbol = NULL;
12030 ex.X_add_number = 0;
12031
12032 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
49309057 12033 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
252b5132 12034
584892a6 12035 macro_start ();
67c0d1eb
RS
12036 macro_build_lui (&ex, mips_gp_register);
12037 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
17a2f251 12038 mips_gp_register, BFD_RELOC_LO16);
67c0d1eb 12039 macro_build (NULL, "addu", "d,v,t", mips_gp_register,
17a2f251 12040 mips_gp_register, tc_get_register (0));
584892a6 12041 macro_end ();
252b5132
RH
12042
12043 demand_empty_rest_of_line ();
12044}
12045
6478892d
TS
12046/* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
12047 .cpsetup $reg1, offset|$reg2, label
12048
12049 If offset is given, this results in:
12050 sd $gp, offset($sp)
956cd1d6 12051 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
12052 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
12053 daddu $gp, $gp, $reg1
6478892d
TS
12054
12055 If $reg2 is given, this results in:
12056 daddu $reg2, $gp, $0
956cd1d6 12057 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
12058 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
12059 daddu $gp, $gp, $reg1
12060 $reg1 is normally $25 == $t9. */
6478892d 12061static void
17a2f251 12062s_cpsetup (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
12063{
12064 expressionS ex_off;
12065 expressionS ex_sym;
12066 int reg1;
f21f8242 12067 char *f;
6478892d 12068
8586fc66 12069 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
6478892d
TS
12070 We also need NewABI support. */
12071 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12072 {
12073 s_ignore (0);
12074 return;
12075 }
12076
12077 reg1 = tc_get_register (0);
12078 SKIP_WHITESPACE ();
12079 if (*input_line_pointer != ',')
12080 {
12081 as_bad (_("missing argument separator ',' for .cpsetup"));
12082 return;
12083 }
12084 else
80245285 12085 ++input_line_pointer;
6478892d
TS
12086 SKIP_WHITESPACE ();
12087 if (*input_line_pointer == '$')
80245285
TS
12088 {
12089 mips_cpreturn_register = tc_get_register (0);
12090 mips_cpreturn_offset = -1;
12091 }
6478892d 12092 else
80245285
TS
12093 {
12094 mips_cpreturn_offset = get_absolute_expression ();
12095 mips_cpreturn_register = -1;
12096 }
6478892d
TS
12097 SKIP_WHITESPACE ();
12098 if (*input_line_pointer != ',')
12099 {
12100 as_bad (_("missing argument separator ',' for .cpsetup"));
12101 return;
12102 }
12103 else
f9419b05 12104 ++input_line_pointer;
6478892d 12105 SKIP_WHITESPACE ();
f21f8242 12106 expression (&ex_sym);
6478892d 12107
584892a6 12108 macro_start ();
6478892d
TS
12109 if (mips_cpreturn_register == -1)
12110 {
12111 ex_off.X_op = O_constant;
12112 ex_off.X_add_symbol = NULL;
12113 ex_off.X_op_symbol = NULL;
12114 ex_off.X_add_number = mips_cpreturn_offset;
12115
67c0d1eb 12116 macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
17a2f251 12117 BFD_RELOC_LO16, SP);
6478892d
TS
12118 }
12119 else
67c0d1eb 12120 macro_build (NULL, "daddu", "d,v,t", mips_cpreturn_register,
17a2f251 12121 mips_gp_register, 0);
6478892d 12122
f21f8242
AO
12123 /* Ensure there's room for the next two instructions, so that `f'
12124 doesn't end up with an address in the wrong frag. */
12125 frag_grow (8);
12126 f = frag_more (0);
67c0d1eb 12127 macro_build (&ex_sym, "lui", "t,u", mips_gp_register, BFD_RELOC_GPREL16);
f21f8242 12128 fix_new (frag_now, f - frag_now->fr_literal,
a105a300 12129 8, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
f21f8242 12130 fix_new (frag_now, f - frag_now->fr_literal,
a105a300 12131 4, NULL, 0, 0, BFD_RELOC_HI16_S);
f21f8242
AO
12132
12133 f = frag_more (0);
67c0d1eb 12134 macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
17a2f251 12135 mips_gp_register, BFD_RELOC_GPREL16);
f21f8242 12136 fix_new (frag_now, f - frag_now->fr_literal,
a105a300 12137 8, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
f21f8242 12138 fix_new (frag_now, f - frag_now->fr_literal,
a105a300 12139 4, NULL, 0, 0, BFD_RELOC_LO16);
f21f8242 12140
67c0d1eb 12141 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
17a2f251 12142 mips_gp_register, reg1);
584892a6 12143 macro_end ();
6478892d
TS
12144
12145 demand_empty_rest_of_line ();
12146}
12147
12148static void
17a2f251 12149s_cplocal (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
12150{
12151 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
12152 .cplocal is ignored. */
12153 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12154 {
12155 s_ignore (0);
12156 return;
12157 }
12158
12159 mips_gp_register = tc_get_register (0);
85b51719 12160 demand_empty_rest_of_line ();
6478892d
TS
12161}
12162
252b5132
RH
12163/* Handle the .cprestore pseudo-op. This stores $gp into a given
12164 offset from $sp. The offset is remembered, and after making a PIC
12165 call $gp is restored from that location. */
12166
12167static void
17a2f251 12168s_cprestore (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
12169{
12170 expressionS ex;
252b5132 12171
6478892d 12172 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
c9914766 12173 .cprestore is ignored. */
6478892d 12174 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
12175 {
12176 s_ignore (0);
12177 return;
12178 }
12179
12180 mips_cprestore_offset = get_absolute_expression ();
7a621144 12181 mips_cprestore_valid = 1;
252b5132
RH
12182
12183 ex.X_op = O_constant;
12184 ex.X_add_symbol = NULL;
12185 ex.X_op_symbol = NULL;
12186 ex.X_add_number = mips_cprestore_offset;
12187
584892a6 12188 macro_start ();
67c0d1eb
RS
12189 macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
12190 SP, HAVE_64BIT_ADDRESSES);
584892a6 12191 macro_end ();
252b5132
RH
12192
12193 demand_empty_rest_of_line ();
12194}
12195
6478892d 12196/* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
67c1ffbe 12197 was given in the preceding .cpsetup, it results in:
6478892d 12198 ld $gp, offset($sp)
76b3015f 12199
6478892d 12200 If a register $reg2 was given there, it results in:
609f23f4 12201 daddu $gp, $reg2, $0
6478892d
TS
12202 */
12203static void
17a2f251 12204s_cpreturn (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
12205{
12206 expressionS ex;
6478892d
TS
12207
12208 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
12209 We also need NewABI support. */
12210 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12211 {
12212 s_ignore (0);
12213 return;
12214 }
12215
584892a6 12216 macro_start ();
6478892d
TS
12217 if (mips_cpreturn_register == -1)
12218 {
12219 ex.X_op = O_constant;
12220 ex.X_add_symbol = NULL;
12221 ex.X_op_symbol = NULL;
12222 ex.X_add_number = mips_cpreturn_offset;
12223
67c0d1eb 12224 macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
6478892d
TS
12225 }
12226 else
67c0d1eb 12227 macro_build (NULL, "daddu", "d,v,t", mips_gp_register,
17a2f251 12228 mips_cpreturn_register, 0);
584892a6 12229 macro_end ();
6478892d
TS
12230
12231 demand_empty_rest_of_line ();
12232}
12233
12234/* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
12235 code. It sets the offset to use in gp_rel relocations. */
12236
12237static void
17a2f251 12238s_gpvalue (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
12239{
12240 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
12241 We also need NewABI support. */
12242 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12243 {
12244 s_ignore (0);
12245 return;
12246 }
12247
def2e0dd 12248 mips_gprel_offset = get_absolute_expression ();
6478892d
TS
12249
12250 demand_empty_rest_of_line ();
12251}
12252
252b5132
RH
12253/* Handle the .gpword pseudo-op. This is used when generating PIC
12254 code. It generates a 32 bit GP relative reloc. */
12255
12256static void
17a2f251 12257s_gpword (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
12258{
12259 symbolS *label;
12260 expressionS ex;
12261 char *p;
12262
12263 /* When not generating PIC code, this is treated as .word. */
12264 if (mips_pic != SVR4_PIC)
12265 {
12266 s_cons (2);
12267 return;
12268 }
12269
12270 label = insn_labels != NULL ? insn_labels->label : NULL;
b34976b6 12271 mips_emit_delays (TRUE);
252b5132
RH
12272 if (auto_align)
12273 mips_align (2, 0, label);
12274 mips_clear_insn_labels ();
12275
12276 expression (&ex);
12277
12278 if (ex.X_op != O_symbol || ex.X_add_number != 0)
12279 {
12280 as_bad (_("Unsupported use of .gpword"));
12281 ignore_rest_of_line ();
12282 }
12283
12284 p = frag_more (4);
17a2f251 12285 md_number_to_chars (p, 0, 4);
b34976b6 12286 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
cdf6fd85 12287 BFD_RELOC_GPREL32);
252b5132
RH
12288
12289 demand_empty_rest_of_line ();
12290}
12291
10181a0d 12292static void
17a2f251 12293s_gpdword (int ignore ATTRIBUTE_UNUSED)
10181a0d
AO
12294{
12295 symbolS *label;
12296 expressionS ex;
12297 char *p;
12298
12299 /* When not generating PIC code, this is treated as .dword. */
12300 if (mips_pic != SVR4_PIC)
12301 {
12302 s_cons (3);
12303 return;
12304 }
12305
12306 label = insn_labels != NULL ? insn_labels->label : NULL;
b34976b6 12307 mips_emit_delays (TRUE);
10181a0d
AO
12308 if (auto_align)
12309 mips_align (3, 0, label);
12310 mips_clear_insn_labels ();
12311
12312 expression (&ex);
12313
12314 if (ex.X_op != O_symbol || ex.X_add_number != 0)
12315 {
12316 as_bad (_("Unsupported use of .gpdword"));
12317 ignore_rest_of_line ();
12318 }
12319
12320 p = frag_more (8);
17a2f251 12321 md_number_to_chars (p, 0, 8);
a105a300 12322 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
10181a0d
AO
12323 BFD_RELOC_GPREL32);
12324
12325 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
12326 ex.X_op = O_absent;
12327 ex.X_add_symbol = 0;
12328 ex.X_add_number = 0;
b34976b6 12329 fix_new_exp (frag_now, p - frag_now->fr_literal, 8, &ex, FALSE,
10181a0d
AO
12330 BFD_RELOC_64);
12331
12332 demand_empty_rest_of_line ();
12333}
12334
252b5132
RH
12335/* Handle the .cpadd pseudo-op. This is used when dealing with switch
12336 tables in SVR4 PIC code. */
12337
12338static void
17a2f251 12339s_cpadd (int ignore ATTRIBUTE_UNUSED)
252b5132 12340{
252b5132
RH
12341 int reg;
12342
10181a0d
AO
12343 /* This is ignored when not generating SVR4 PIC code. */
12344 if (mips_pic != SVR4_PIC)
252b5132
RH
12345 {
12346 s_ignore (0);
12347 return;
12348 }
12349
12350 /* Add $gp to the register named as an argument. */
584892a6 12351 macro_start ();
252b5132 12352 reg = tc_get_register (0);
67c0d1eb 12353 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
584892a6 12354 macro_end ();
252b5132 12355
bdaaa2e1 12356 demand_empty_rest_of_line ();
252b5132
RH
12357}
12358
12359/* Handle the .insn pseudo-op. This marks instruction labels in
12360 mips16 mode. This permits the linker to handle them specially,
12361 such as generating jalx instructions when needed. We also make
12362 them odd for the duration of the assembly, in order to generate the
12363 right sort of code. We will make them even in the adjust_symtab
12364 routine, while leaving them marked. This is convenient for the
12365 debugger and the disassembler. The linker knows to make them odd
12366 again. */
12367
12368static void
17a2f251 12369s_insn (int ignore ATTRIBUTE_UNUSED)
252b5132 12370{
f9419b05 12371 mips16_mark_labels ();
252b5132
RH
12372
12373 demand_empty_rest_of_line ();
12374}
12375
12376/* Handle a .stabn directive. We need these in order to mark a label
12377 as being a mips16 text label correctly. Sometimes the compiler
12378 will emit a label, followed by a .stabn, and then switch sections.
12379 If the label and .stabn are in mips16 mode, then the label is
12380 really a mips16 text label. */
12381
12382static void
17a2f251 12383s_mips_stab (int type)
252b5132 12384{
f9419b05 12385 if (type == 'n')
252b5132
RH
12386 mips16_mark_labels ();
12387
12388 s_stab (type);
12389}
12390
12391/* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
12392 */
12393
12394static void
17a2f251 12395s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
12396{
12397 char *name;
12398 int c;
12399 symbolS *symbolP;
12400 expressionS exp;
12401
12402 name = input_line_pointer;
12403 c = get_symbol_end ();
12404 symbolP = symbol_find_or_make (name);
12405 S_SET_WEAK (symbolP);
12406 *input_line_pointer = c;
12407
12408 SKIP_WHITESPACE ();
12409
12410 if (! is_end_of_line[(unsigned char) *input_line_pointer])
12411 {
12412 if (S_IS_DEFINED (symbolP))
12413 {
956cd1d6 12414 as_bad ("ignoring attempt to redefine symbol %s",
252b5132
RH
12415 S_GET_NAME (symbolP));
12416 ignore_rest_of_line ();
12417 return;
12418 }
bdaaa2e1 12419
252b5132
RH
12420 if (*input_line_pointer == ',')
12421 {
12422 ++input_line_pointer;
12423 SKIP_WHITESPACE ();
12424 }
bdaaa2e1 12425
252b5132
RH
12426 expression (&exp);
12427 if (exp.X_op != O_symbol)
12428 {
12429 as_bad ("bad .weakext directive");
98d3f06f 12430 ignore_rest_of_line ();
252b5132
RH
12431 return;
12432 }
49309057 12433 symbol_set_value_expression (symbolP, &exp);
252b5132
RH
12434 }
12435
12436 demand_empty_rest_of_line ();
12437}
12438
12439/* Parse a register string into a number. Called from the ECOFF code
12440 to parse .frame. The argument is non-zero if this is the frame
12441 register, so that we can record it in mips_frame_reg. */
12442
12443int
17a2f251 12444tc_get_register (int frame)
252b5132
RH
12445{
12446 int reg;
12447
12448 SKIP_WHITESPACE ();
12449 if (*input_line_pointer++ != '$')
12450 {
12451 as_warn (_("expected `$'"));
85b51719 12452 reg = ZERO;
252b5132 12453 }
3882b010 12454 else if (ISDIGIT (*input_line_pointer))
252b5132
RH
12455 {
12456 reg = get_absolute_expression ();
12457 if (reg < 0 || reg >= 32)
12458 {
12459 as_warn (_("Bad register number"));
85b51719 12460 reg = ZERO;
252b5132
RH
12461 }
12462 }
12463 else
12464 {
76db943d 12465 if (strncmp (input_line_pointer, "ra", 2) == 0)
85b51719
TS
12466 {
12467 reg = RA;
12468 input_line_pointer += 2;
12469 }
76db943d 12470 else if (strncmp (input_line_pointer, "fp", 2) == 0)
85b51719
TS
12471 {
12472 reg = FP;
12473 input_line_pointer += 2;
12474 }
252b5132 12475 else if (strncmp (input_line_pointer, "sp", 2) == 0)
85b51719
TS
12476 {
12477 reg = SP;
12478 input_line_pointer += 2;
12479 }
252b5132 12480 else if (strncmp (input_line_pointer, "gp", 2) == 0)
85b51719
TS
12481 {
12482 reg = GP;
12483 input_line_pointer += 2;
12484 }
252b5132 12485 else if (strncmp (input_line_pointer, "at", 2) == 0)
85b51719
TS
12486 {
12487 reg = AT;
12488 input_line_pointer += 2;
12489 }
12490 else if (strncmp (input_line_pointer, "kt0", 3) == 0)
12491 {
12492 reg = KT0;
12493 input_line_pointer += 3;
12494 }
12495 else if (strncmp (input_line_pointer, "kt1", 3) == 0)
12496 {
12497 reg = KT1;
12498 input_line_pointer += 3;
12499 }
12500 else if (strncmp (input_line_pointer, "zero", 4) == 0)
12501 {
12502 reg = ZERO;
12503 input_line_pointer += 4;
12504 }
252b5132
RH
12505 else
12506 {
12507 as_warn (_("Unrecognized register name"));
85b51719
TS
12508 reg = ZERO;
12509 while (ISALNUM(*input_line_pointer))
12510 input_line_pointer++;
252b5132 12511 }
252b5132
RH
12512 }
12513 if (frame)
7a621144
DJ
12514 {
12515 mips_frame_reg = reg != 0 ? reg : SP;
12516 mips_frame_reg_valid = 1;
12517 mips_cprestore_valid = 0;
12518 }
252b5132
RH
12519 return reg;
12520}
12521
12522valueT
17a2f251 12523md_section_align (asection *seg, valueT addr)
252b5132
RH
12524{
12525 int align = bfd_get_section_alignment (stdoutput, seg);
12526
12527#ifdef OBJ_ELF
12528 /* We don't need to align ELF sections to the full alignment.
12529 However, Irix 5 may prefer that we align them at least to a 16
12530 byte boundary. We don't bother to align the sections if we are
12531 targeted for an embedded system. */
12532 if (strcmp (TARGET_OS, "elf") == 0)
12533 return addr;
12534 if (align > 4)
12535 align = 4;
12536#endif
12537
12538 return ((addr + (1 << align) - 1) & (-1 << align));
12539}
12540
12541/* Utility routine, called from above as well. If called while the
12542 input file is still being read, it's only an approximation. (For
12543 example, a symbol may later become defined which appeared to be
12544 undefined earlier.) */
12545
12546static int
17a2f251 12547nopic_need_relax (symbolS *sym, int before_relaxing)
252b5132
RH
12548{
12549 if (sym == 0)
12550 return 0;
12551
4d0d148d 12552 if (g_switch_value > 0)
252b5132
RH
12553 {
12554 const char *symname;
12555 int change;
12556
c9914766 12557 /* Find out whether this symbol can be referenced off the $gp
252b5132
RH
12558 register. It can be if it is smaller than the -G size or if
12559 it is in the .sdata or .sbss section. Certain symbols can
c9914766 12560 not be referenced off the $gp, although it appears as though
252b5132
RH
12561 they can. */
12562 symname = S_GET_NAME (sym);
12563 if (symname != (const char *) NULL
12564 && (strcmp (symname, "eprol") == 0
12565 || strcmp (symname, "etext") == 0
12566 || strcmp (symname, "_gp") == 0
12567 || strcmp (symname, "edata") == 0
12568 || strcmp (symname, "_fbss") == 0
12569 || strcmp (symname, "_fdata") == 0
12570 || strcmp (symname, "_ftext") == 0
12571 || strcmp (symname, "end") == 0
12572 || strcmp (symname, "_gp_disp") == 0))
12573 change = 1;
12574 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
12575 && (0
12576#ifndef NO_ECOFF_DEBUGGING
49309057
ILT
12577 || (symbol_get_obj (sym)->ecoff_extern_size != 0
12578 && (symbol_get_obj (sym)->ecoff_extern_size
12579 <= g_switch_value))
252b5132
RH
12580#endif
12581 /* We must defer this decision until after the whole
12582 file has been read, since there might be a .extern
12583 after the first use of this symbol. */
12584 || (before_relaxing
12585#ifndef NO_ECOFF_DEBUGGING
49309057 12586 && symbol_get_obj (sym)->ecoff_extern_size == 0
252b5132
RH
12587#endif
12588 && S_GET_VALUE (sym) == 0)
12589 || (S_GET_VALUE (sym) != 0
12590 && S_GET_VALUE (sym) <= g_switch_value)))
12591 change = 0;
12592 else
12593 {
12594 const char *segname;
12595
12596 segname = segment_name (S_GET_SEGMENT (sym));
12597 assert (strcmp (segname, ".lit8") != 0
12598 && strcmp (segname, ".lit4") != 0);
12599 change = (strcmp (segname, ".sdata") != 0
fba2b7f9
GK
12600 && strcmp (segname, ".sbss") != 0
12601 && strncmp (segname, ".sdata.", 7) != 0
12602 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
252b5132
RH
12603 }
12604 return change;
12605 }
12606 else
c9914766 12607 /* We are not optimizing for the $gp register. */
252b5132
RH
12608 return 1;
12609}
12610
5919d012
RS
12611
12612/* Return true if the given symbol should be considered local for SVR4 PIC. */
12613
12614static bfd_boolean
17a2f251 12615pic_need_relax (symbolS *sym, asection *segtype)
5919d012
RS
12616{
12617 asection *symsec;
12618 bfd_boolean linkonce;
12619
12620 /* Handle the case of a symbol equated to another symbol. */
12621 while (symbol_equated_reloc_p (sym))
12622 {
12623 symbolS *n;
12624
12625 /* It's possible to get a loop here in a badly written
12626 program. */
12627 n = symbol_get_value_expression (sym)->X_add_symbol;
12628 if (n == sym)
12629 break;
12630 sym = n;
12631 }
12632
12633 symsec = S_GET_SEGMENT (sym);
12634
12635 /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
12636 linkonce = FALSE;
12637 if (symsec != segtype && ! S_IS_LOCAL (sym))
12638 {
12639 if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
12640 != 0)
12641 linkonce = TRUE;
12642
12643 /* The GNU toolchain uses an extension for ELF: a section
12644 beginning with the magic string .gnu.linkonce is a linkonce
12645 section. */
12646 if (strncmp (segment_name (symsec), ".gnu.linkonce",
12647 sizeof ".gnu.linkonce" - 1) == 0)
12648 linkonce = TRUE;
12649 }
12650
12651 /* This must duplicate the test in adjust_reloc_syms. */
12652 return (symsec != &bfd_und_section
12653 && symsec != &bfd_abs_section
12654 && ! bfd_is_com_section (symsec)
12655 && !linkonce
12656#ifdef OBJ_ELF
12657 /* A global or weak symbol is treated as external. */
12658 && (OUTPUT_FLAVOR != bfd_target_elf_flavour
12659 || (! S_IS_WEAK (sym)
12660 && (! S_IS_EXTERNAL (sym)
12661 || mips_pic == EMBEDDED_PIC)))
12662#endif
12663 );
12664}
12665
12666
252b5132
RH
12667/* Given a mips16 variant frag FRAGP, return non-zero if it needs an
12668 extended opcode. SEC is the section the frag is in. */
12669
12670static int
17a2f251 12671mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
252b5132
RH
12672{
12673 int type;
12674 register const struct mips16_immed_operand *op;
12675 offsetT val;
12676 int mintiny, maxtiny;
12677 segT symsec;
98aa84af 12678 fragS *sym_frag;
252b5132
RH
12679
12680 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
12681 return 0;
12682 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
12683 return 1;
12684
12685 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12686 op = mips16_immed_operands;
12687 while (op->type != type)
12688 {
12689 ++op;
12690 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
12691 }
12692
12693 if (op->unsp)
12694 {
12695 if (type == '<' || type == '>' || type == '[' || type == ']')
12696 {
12697 mintiny = 1;
12698 maxtiny = 1 << op->nbits;
12699 }
12700 else
12701 {
12702 mintiny = 0;
12703 maxtiny = (1 << op->nbits) - 1;
12704 }
12705 }
12706 else
12707 {
12708 mintiny = - (1 << (op->nbits - 1));
12709 maxtiny = (1 << (op->nbits - 1)) - 1;
12710 }
12711
98aa84af 12712 sym_frag = symbol_get_frag (fragp->fr_symbol);
ac62c346 12713 val = S_GET_VALUE (fragp->fr_symbol);
98aa84af 12714 symsec = S_GET_SEGMENT (fragp->fr_symbol);
252b5132
RH
12715
12716 if (op->pcrel)
12717 {
12718 addressT addr;
12719
12720 /* We won't have the section when we are called from
12721 mips_relax_frag. However, we will always have been called
12722 from md_estimate_size_before_relax first. If this is a
12723 branch to a different section, we mark it as such. If SEC is
12724 NULL, and the frag is not marked, then it must be a branch to
12725 the same section. */
12726 if (sec == NULL)
12727 {
12728 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
12729 return 1;
12730 }
12731 else
12732 {
98aa84af 12733 /* Must have been called from md_estimate_size_before_relax. */
252b5132
RH
12734 if (symsec != sec)
12735 {
12736 fragp->fr_subtype =
12737 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12738
12739 /* FIXME: We should support this, and let the linker
12740 catch branches and loads that are out of range. */
12741 as_bad_where (fragp->fr_file, fragp->fr_line,
12742 _("unsupported PC relative reference to different section"));
12743
12744 return 1;
12745 }
98aa84af
AM
12746 if (fragp != sym_frag && sym_frag->fr_address == 0)
12747 /* Assume non-extended on the first relaxation pass.
12748 The address we have calculated will be bogus if this is
12749 a forward branch to another frag, as the forward frag
12750 will have fr_address == 0. */
12751 return 0;
252b5132
RH
12752 }
12753
12754 /* In this case, we know for sure that the symbol fragment is in
98aa84af
AM
12755 the same section. If the relax_marker of the symbol fragment
12756 differs from the relax_marker of this fragment, we have not
12757 yet adjusted the symbol fragment fr_address. We want to add
252b5132
RH
12758 in STRETCH in order to get a better estimate of the address.
12759 This particularly matters because of the shift bits. */
12760 if (stretch != 0
98aa84af 12761 && sym_frag->relax_marker != fragp->relax_marker)
252b5132
RH
12762 {
12763 fragS *f;
12764
12765 /* Adjust stretch for any alignment frag. Note that if have
12766 been expanding the earlier code, the symbol may be
12767 defined in what appears to be an earlier frag. FIXME:
12768 This doesn't handle the fr_subtype field, which specifies
12769 a maximum number of bytes to skip when doing an
12770 alignment. */
98aa84af 12771 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
252b5132
RH
12772 {
12773 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
12774 {
12775 if (stretch < 0)
12776 stretch = - ((- stretch)
12777 & ~ ((1 << (int) f->fr_offset) - 1));
12778 else
12779 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
12780 if (stretch == 0)
12781 break;
12782 }
12783 }
12784 if (f != NULL)
12785 val += stretch;
12786 }
12787
12788 addr = fragp->fr_address + fragp->fr_fix;
12789
12790 /* The base address rules are complicated. The base address of
12791 a branch is the following instruction. The base address of a
12792 PC relative load or add is the instruction itself, but if it
12793 is in a delay slot (in which case it can not be extended) use
12794 the address of the instruction whose delay slot it is in. */
12795 if (type == 'p' || type == 'q')
12796 {
12797 addr += 2;
12798
12799 /* If we are currently assuming that this frag should be
12800 extended, then, the current address is two bytes
bdaaa2e1 12801 higher. */
252b5132
RH
12802 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12803 addr += 2;
12804
12805 /* Ignore the low bit in the target, since it will be set
12806 for a text label. */
12807 if ((val & 1) != 0)
12808 --val;
12809 }
12810 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12811 addr -= 4;
12812 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12813 addr -= 2;
12814
12815 val -= addr & ~ ((1 << op->shift) - 1);
12816
12817 /* Branch offsets have an implicit 0 in the lowest bit. */
12818 if (type == 'p' || type == 'q')
12819 val /= 2;
12820
12821 /* If any of the shifted bits are set, we must use an extended
12822 opcode. If the address depends on the size of this
12823 instruction, this can lead to a loop, so we arrange to always
12824 use an extended opcode. We only check this when we are in
12825 the main relaxation loop, when SEC is NULL. */
12826 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
12827 {
12828 fragp->fr_subtype =
12829 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12830 return 1;
12831 }
12832
12833 /* If we are about to mark a frag as extended because the value
12834 is precisely maxtiny + 1, then there is a chance of an
12835 infinite loop as in the following code:
12836 la $4,foo
12837 .skip 1020
12838 .align 2
12839 foo:
12840 In this case when the la is extended, foo is 0x3fc bytes
12841 away, so the la can be shrunk, but then foo is 0x400 away, so
12842 the la must be extended. To avoid this loop, we mark the
12843 frag as extended if it was small, and is about to become
12844 extended with a value of maxtiny + 1. */
12845 if (val == ((maxtiny + 1) << op->shift)
12846 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
12847 && sec == NULL)
12848 {
12849 fragp->fr_subtype =
12850 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12851 return 1;
12852 }
12853 }
12854 else if (symsec != absolute_section && sec != NULL)
12855 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
12856
12857 if ((val & ((1 << op->shift) - 1)) != 0
12858 || val < (mintiny << op->shift)
12859 || val > (maxtiny << op->shift))
12860 return 1;
12861 else
12862 return 0;
12863}
12864
4a6a3df4
AO
12865/* Compute the length of a branch sequence, and adjust the
12866 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
12867 worst-case length is computed, with UPDATE being used to indicate
12868 whether an unconditional (-1), branch-likely (+1) or regular (0)
12869 branch is to be computed. */
12870static int
17a2f251 12871relaxed_branch_length (fragS *fragp, asection *sec, int update)
4a6a3df4 12872{
b34976b6 12873 bfd_boolean toofar;
4a6a3df4
AO
12874 int length;
12875
12876 if (fragp
12877 && S_IS_DEFINED (fragp->fr_symbol)
12878 && sec == S_GET_SEGMENT (fragp->fr_symbol))
12879 {
12880 addressT addr;
12881 offsetT val;
12882
12883 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
12884
12885 addr = fragp->fr_address + fragp->fr_fix + 4;
12886
12887 val -= addr;
12888
12889 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
12890 }
12891 else if (fragp)
12892 /* If the symbol is not defined or it's in a different segment,
12893 assume the user knows what's going on and emit a short
12894 branch. */
b34976b6 12895 toofar = FALSE;
4a6a3df4 12896 else
b34976b6 12897 toofar = TRUE;
4a6a3df4
AO
12898
12899 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
12900 fragp->fr_subtype
af6ae2ad 12901 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_UNCOND (fragp->fr_subtype),
4a6a3df4
AO
12902 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
12903 RELAX_BRANCH_LINK (fragp->fr_subtype),
12904 toofar);
12905
12906 length = 4;
12907 if (toofar)
12908 {
12909 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
12910 length += 8;
12911
12912 if (mips_pic != NO_PIC)
12913 {
12914 /* Additional space for PIC loading of target address. */
12915 length += 8;
12916 if (mips_opts.isa == ISA_MIPS1)
12917 /* Additional space for $at-stabilizing nop. */
12918 length += 4;
12919 }
12920
12921 /* If branch is conditional. */
12922 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
12923 length += 8;
12924 }
b34976b6 12925
4a6a3df4
AO
12926 return length;
12927}
12928
252b5132
RH
12929/* Estimate the size of a frag before relaxing. Unless this is the
12930 mips16, we are not really relaxing here, and the final size is
12931 encoded in the subtype information. For the mips16, we have to
12932 decide whether we are using an extended opcode or not. */
12933
252b5132 12934int
17a2f251 12935md_estimate_size_before_relax (fragS *fragp, asection *segtype)
252b5132 12936{
5919d012 12937 int change;
252b5132 12938
4a6a3df4
AO
12939 if (RELAX_BRANCH_P (fragp->fr_subtype))
12940 {
12941
b34976b6
AM
12942 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
12943
4a6a3df4
AO
12944 return fragp->fr_var;
12945 }
12946
252b5132 12947 if (RELAX_MIPS16_P (fragp->fr_subtype))
177b4a6a
AO
12948 /* We don't want to modify the EXTENDED bit here; it might get us
12949 into infinite loops. We change it only in mips_relax_frag(). */
12950 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
252b5132
RH
12951
12952 if (mips_pic == NO_PIC)
5919d012 12953 change = nopic_need_relax (fragp->fr_symbol, 0);
252b5132 12954 else if (mips_pic == SVR4_PIC)
5919d012 12955 change = pic_need_relax (fragp->fr_symbol, segtype);
252b5132
RH
12956 else
12957 abort ();
12958
12959 if (change)
12960 {
4d7206a2 12961 fragp->fr_subtype |= RELAX_USE_SECOND;
4d7206a2 12962 return -RELAX_FIRST (fragp->fr_subtype);
252b5132 12963 }
4d7206a2
RS
12964 else
12965 return -RELAX_SECOND (fragp->fr_subtype);
252b5132
RH
12966}
12967
12968/* This is called to see whether a reloc against a defined symbol
12969 should be converted into a reloc against a section. Don't adjust
12970 MIPS16 jump relocations, so we don't have to worry about the format
12971 of the offset in the .o file. Don't adjust relocations against
12972 mips16 symbols, so that the linker can find them if it needs to set
12973 up a stub. */
12974
12975int
17a2f251 12976mips_fix_adjustable (fixS *fixp)
252b5132
RH
12977{
12978 if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
12979 return 0;
a161fe53 12980
252b5132
RH
12981 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
12982 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12983 return 0;
a161fe53 12984
252b5132
RH
12985 if (fixp->fx_addsy == NULL)
12986 return 1;
a161fe53 12987
252b5132
RH
12988#ifdef OBJ_ELF
12989 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
12990 && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
12991 && fixp->fx_subsy == NULL)
12992 return 0;
12993#endif
a161fe53 12994
252b5132
RH
12995 return 1;
12996}
12997
12998/* Translate internal representation of relocation info to BFD target
12999 format. */
13000
13001arelent **
17a2f251 13002tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
13003{
13004 static arelent *retval[4];
13005 arelent *reloc;
13006 bfd_reloc_code_real_type code;
13007
4b0cff4e
TS
13008 memset (retval, 0, sizeof(retval));
13009 reloc = retval[0] = (arelent *) xcalloc (1, sizeof (arelent));
49309057
ILT
13010 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13011 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
13012 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
13013
13014 if (mips_pic == EMBEDDED_PIC
13015 && SWITCH_TABLE (fixp))
13016 {
13017 /* For a switch table entry we use a special reloc. The addend
13018 is actually the difference between the reloc address and the
13019 subtrahend. */
13020 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
13021 if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
13022 as_fatal (_("Double check fx_r_type in tc-mips.c:tc_gen_reloc"));
13023 fixp->fx_r_type = BFD_RELOC_GPREL32;
13024 }
30ac9238 13025 else if (fixp->fx_pcrel)
252b5132 13026 {
30ac9238
RS
13027 bfd_vma pcrel_address;
13028
13029 /* Set PCREL_ADDRESS to this relocation's "PC". The PC for high
13030 high-part relocs is the address of the low-part reloc. */
13031 if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
13032 {
13033 assert (fixp->fx_next != NULL
13034 && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
13035 pcrel_address = (fixp->fx_next->fx_where
13036 + fixp->fx_next->fx_frag->fr_address);
13037 }
252b5132 13038 else
30ac9238
RS
13039 pcrel_address = reloc->address;
13040
13041 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
13042 {
13043 /* At this point, fx_addnumber is "symbol offset - pcrel_address".
13044 Relocations want only the symbol offset. */
13045 reloc->addend = fixp->fx_addnumber + pcrel_address;
13046 }
13047 else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16
13048 || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
4514d474 13049 {
30ac9238 13050 /* We use a special addend for an internal RELLO or RELHI reloc. */
4514d474 13051 if (symbol_section_p (fixp->fx_addsy))
30ac9238 13052 reloc->addend = pcrel_address - S_GET_VALUE (fixp->fx_subsy);
4514d474 13053 else
30ac9238 13054 reloc->addend = fixp->fx_addnumber + pcrel_address;
4514d474 13055 }
252b5132 13056 else
4514d474 13057 {
4d0d148d
TS
13058 /* A gruesome hack which is a result of the gruesome gas reloc
13059 handling. */
13060 reloc->addend = pcrel_address;
4514d474 13061 }
252b5132 13062 }
252b5132 13063 else
30ac9238 13064 reloc->addend = fixp->fx_addnumber;
252b5132 13065
438c16b8
TS
13066 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
13067 entry to be used in the relocation's section offset. */
13068 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
252b5132
RH
13069 {
13070 reloc->address = reloc->addend;
13071 reloc->addend = 0;
13072 }
13073
13074 /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
13075 fixup_segment converted a non-PC relative reloc into a PC
13076 relative reloc. In such a case, we need to convert the reloc
13077 code. */
13078 code = fixp->fx_r_type;
13079 if (fixp->fx_pcrel)
13080 {
13081 switch (code)
13082 {
13083 case BFD_RELOC_8:
13084 code = BFD_RELOC_8_PCREL;
13085 break;
13086 case BFD_RELOC_16:
13087 code = BFD_RELOC_16_PCREL;
13088 break;
13089 case BFD_RELOC_32:
13090 code = BFD_RELOC_32_PCREL;
13091 break;
13092 case BFD_RELOC_64:
13093 code = BFD_RELOC_64_PCREL;
13094 break;
13095 case BFD_RELOC_8_PCREL:
13096 case BFD_RELOC_16_PCREL:
13097 case BFD_RELOC_32_PCREL:
13098 case BFD_RELOC_64_PCREL:
13099 case BFD_RELOC_16_PCREL_S2:
13100 case BFD_RELOC_PCREL_HI16_S:
13101 case BFD_RELOC_PCREL_LO16:
13102 break;
13103 default:
13104 as_bad_where (fixp->fx_file, fixp->fx_line,
13105 _("Cannot make %s relocation PC relative"),
13106 bfd_get_reloc_code_name (code));
13107 }
13108 }
13109
0b25d3e6
AO
13110 /* To support a PC relative reloc when generating embedded PIC code
13111 for ECOFF, we use a Cygnus extension. We check for that here to
13112 make sure that we don't let such a reloc escape normally. */
13113 if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
13114 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
13115 && code == BFD_RELOC_16_PCREL_S2
13116 && mips_pic != EMBEDDED_PIC)
13117 reloc->howto = NULL;
13118 else
13119 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
13120
252b5132
RH
13121 if (reloc->howto == NULL)
13122 {
13123 as_bad_where (fixp->fx_file, fixp->fx_line,
13124 _("Can not represent %s relocation in this object file format"),
13125 bfd_get_reloc_code_name (code));
13126 retval[0] = NULL;
13127 }
13128
13129 return retval;
13130}
13131
13132/* Relax a machine dependent frag. This returns the amount by which
13133 the current size of the frag should change. */
13134
13135int
17a2f251 13136mips_relax_frag (asection *sec, fragS *fragp, long stretch)
252b5132 13137{
4a6a3df4
AO
13138 if (RELAX_BRANCH_P (fragp->fr_subtype))
13139 {
13140 offsetT old_var = fragp->fr_var;
b34976b6
AM
13141
13142 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
4a6a3df4
AO
13143
13144 return fragp->fr_var - old_var;
13145 }
13146
252b5132
RH
13147 if (! RELAX_MIPS16_P (fragp->fr_subtype))
13148 return 0;
13149
c4e7957c 13150 if (mips16_extended_frag (fragp, NULL, stretch))
252b5132
RH
13151 {
13152 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13153 return 0;
13154 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
13155 return 2;
13156 }
13157 else
13158 {
13159 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13160 return 0;
13161 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
13162 return -2;
13163 }
13164
13165 return 0;
13166}
13167
13168/* Convert a machine dependent frag. */
13169
13170void
17a2f251 13171md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
252b5132 13172{
4a6a3df4
AO
13173 if (RELAX_BRANCH_P (fragp->fr_subtype))
13174 {
13175 bfd_byte *buf;
13176 unsigned long insn;
13177 expressionS exp;
13178 fixS *fixp;
b34976b6 13179
4a6a3df4
AO
13180 buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
13181
13182 if (target_big_endian)
13183 insn = bfd_getb32 (buf);
13184 else
13185 insn = bfd_getl32 (buf);
b34976b6 13186
4a6a3df4
AO
13187 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13188 {
13189 /* We generate a fixup instead of applying it right now
13190 because, if there are linker relaxations, we're going to
13191 need the relocations. */
13192 exp.X_op = O_symbol;
13193 exp.X_add_symbol = fragp->fr_symbol;
13194 exp.X_add_number = fragp->fr_offset;
13195
13196 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
0b25d3e6
AO
13197 4, &exp, 1,
13198 BFD_RELOC_16_PCREL_S2);
4a6a3df4
AO
13199 fixp->fx_file = fragp->fr_file;
13200 fixp->fx_line = fragp->fr_line;
b34976b6 13201
17a2f251 13202 md_number_to_chars (buf, insn, 4);
4a6a3df4
AO
13203 buf += 4;
13204 }
13205 else
13206 {
13207 int i;
13208
13209 as_warn_where (fragp->fr_file, fragp->fr_line,
13210 _("relaxed out-of-range branch into a jump"));
13211
13212 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
13213 goto uncond;
13214
13215 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13216 {
13217 /* Reverse the branch. */
13218 switch ((insn >> 28) & 0xf)
13219 {
13220 case 4:
13221 /* bc[0-3][tf]l? and bc1any[24][ft] instructions can
13222 have the condition reversed by tweaking a single
13223 bit, and their opcodes all have 0x4???????. */
13224 assert ((insn & 0xf1000000) == 0x41000000);
13225 insn ^= 0x00010000;
13226 break;
13227
13228 case 0:
13229 /* bltz 0x04000000 bgez 0x04010000
13230 bltzal 0x04100000 bgezal 0x04110000 */
13231 assert ((insn & 0xfc0e0000) == 0x04000000);
13232 insn ^= 0x00010000;
13233 break;
b34976b6 13234
4a6a3df4
AO
13235 case 1:
13236 /* beq 0x10000000 bne 0x14000000
13237 blez 0x18000000 bgtz 0x1c000000 */
13238 insn ^= 0x04000000;
13239 break;
13240
13241 default:
13242 abort ();
13243 }
13244 }
13245
13246 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13247 {
13248 /* Clear the and-link bit. */
13249 assert ((insn & 0xfc1c0000) == 0x04100000);
13250
13251 /* bltzal 0x04100000 bgezal 0x04110000
13252 bltzall 0x04120000 bgezall 0x04130000 */
13253 insn &= ~0x00100000;
13254 }
13255
13256 /* Branch over the branch (if the branch was likely) or the
13257 full jump (not likely case). Compute the offset from the
13258 current instruction to branch to. */
13259 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13260 i = 16;
13261 else
13262 {
13263 /* How many bytes in instructions we've already emitted? */
13264 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13265 /* How many bytes in instructions from here to the end? */
13266 i = fragp->fr_var - i;
13267 }
13268 /* Convert to instruction count. */
13269 i >>= 2;
13270 /* Branch counts from the next instruction. */
b34976b6 13271 i--;
4a6a3df4
AO
13272 insn |= i;
13273 /* Branch over the jump. */
17a2f251 13274 md_number_to_chars (buf, insn, 4);
4a6a3df4
AO
13275 buf += 4;
13276
13277 /* Nop */
17a2f251 13278 md_number_to_chars (buf, 0, 4);
4a6a3df4
AO
13279 buf += 4;
13280
13281 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13282 {
13283 /* beql $0, $0, 2f */
13284 insn = 0x50000000;
13285 /* Compute the PC offset from the current instruction to
13286 the end of the variable frag. */
13287 /* How many bytes in instructions we've already emitted? */
13288 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13289 /* How many bytes in instructions from here to the end? */
13290 i = fragp->fr_var - i;
13291 /* Convert to instruction count. */
13292 i >>= 2;
13293 /* Don't decrement i, because we want to branch over the
13294 delay slot. */
13295
13296 insn |= i;
17a2f251 13297 md_number_to_chars (buf, insn, 4);
4a6a3df4
AO
13298 buf += 4;
13299
17a2f251 13300 md_number_to_chars (buf, 0, 4);
4a6a3df4
AO
13301 buf += 4;
13302 }
13303
13304 uncond:
13305 if (mips_pic == NO_PIC)
13306 {
13307 /* j or jal. */
13308 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
13309 ? 0x0c000000 : 0x08000000);
13310 exp.X_op = O_symbol;
13311 exp.X_add_symbol = fragp->fr_symbol;
13312 exp.X_add_number = fragp->fr_offset;
13313
13314 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13315 4, &exp, 0, BFD_RELOC_MIPS_JMP);
13316 fixp->fx_file = fragp->fr_file;
13317 fixp->fx_line = fragp->fr_line;
13318
17a2f251 13319 md_number_to_chars (buf, insn, 4);
4a6a3df4
AO
13320 buf += 4;
13321 }
13322 else
13323 {
13324 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
13325 insn = HAVE_64BIT_ADDRESSES ? 0xdf810000 : 0x8f810000;
13326 exp.X_op = O_symbol;
13327 exp.X_add_symbol = fragp->fr_symbol;
13328 exp.X_add_number = fragp->fr_offset;
13329
13330 if (fragp->fr_offset)
13331 {
13332 exp.X_add_symbol = make_expr_symbol (&exp);
13333 exp.X_add_number = 0;
13334 }
13335
13336 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13337 4, &exp, 0, BFD_RELOC_MIPS_GOT16);
13338 fixp->fx_file = fragp->fr_file;
13339 fixp->fx_line = fragp->fr_line;
13340
17a2f251 13341 md_number_to_chars (buf, insn, 4);
4a6a3df4 13342 buf += 4;
b34976b6 13343
4a6a3df4
AO
13344 if (mips_opts.isa == ISA_MIPS1)
13345 {
13346 /* nop */
17a2f251 13347 md_number_to_chars (buf, 0, 4);
4a6a3df4
AO
13348 buf += 4;
13349 }
13350
13351 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
13352 insn = HAVE_64BIT_ADDRESSES ? 0x64210000 : 0x24210000;
13353
13354 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13355 4, &exp, 0, BFD_RELOC_LO16);
13356 fixp->fx_file = fragp->fr_file;
13357 fixp->fx_line = fragp->fr_line;
b34976b6 13358
17a2f251 13359 md_number_to_chars (buf, insn, 4);
4a6a3df4
AO
13360 buf += 4;
13361
13362 /* j(al)r $at. */
13363 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13364 insn = 0x0020f809;
13365 else
13366 insn = 0x00200008;
13367
17a2f251 13368 md_number_to_chars (buf, insn, 4);
4a6a3df4
AO
13369 buf += 4;
13370 }
13371 }
13372
13373 assert (buf == (bfd_byte *)fragp->fr_literal
13374 + fragp->fr_fix + fragp->fr_var);
13375
13376 fragp->fr_fix += fragp->fr_var;
13377
13378 return;
13379 }
13380
252b5132
RH
13381 if (RELAX_MIPS16_P (fragp->fr_subtype))
13382 {
13383 int type;
13384 register const struct mips16_immed_operand *op;
b34976b6 13385 bfd_boolean small, ext;
252b5132
RH
13386 offsetT val;
13387 bfd_byte *buf;
13388 unsigned long insn;
b34976b6 13389 bfd_boolean use_extend;
252b5132
RH
13390 unsigned short extend;
13391
13392 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
13393 op = mips16_immed_operands;
13394 while (op->type != type)
13395 ++op;
13396
13397 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13398 {
b34976b6
AM
13399 small = FALSE;
13400 ext = TRUE;
252b5132
RH
13401 }
13402 else
13403 {
b34976b6
AM
13404 small = TRUE;
13405 ext = FALSE;
252b5132
RH
13406 }
13407
6386f3a7 13408 resolve_symbol_value (fragp->fr_symbol);
252b5132
RH
13409 val = S_GET_VALUE (fragp->fr_symbol);
13410 if (op->pcrel)
13411 {
13412 addressT addr;
13413
13414 addr = fragp->fr_address + fragp->fr_fix;
13415
13416 /* The rules for the base address of a PC relative reloc are
13417 complicated; see mips16_extended_frag. */
13418 if (type == 'p' || type == 'q')
13419 {
13420 addr += 2;
13421 if (ext)
13422 addr += 2;
13423 /* Ignore the low bit in the target, since it will be
13424 set for a text label. */
13425 if ((val & 1) != 0)
13426 --val;
13427 }
13428 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13429 addr -= 4;
13430 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13431 addr -= 2;
13432
13433 addr &= ~ (addressT) ((1 << op->shift) - 1);
13434 val -= addr;
13435
13436 /* Make sure the section winds up with the alignment we have
13437 assumed. */
13438 if (op->shift > 0)
13439 record_alignment (asec, op->shift);
13440 }
13441
13442 if (ext
13443 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
13444 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
13445 as_warn_where (fragp->fr_file, fragp->fr_line,
13446 _("extended instruction in delay slot"));
13447
13448 buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
13449
13450 if (target_big_endian)
13451 insn = bfd_getb16 (buf);
13452 else
13453 insn = bfd_getl16 (buf);
13454
13455 mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
13456 RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
13457 small, ext, &insn, &use_extend, &extend);
13458
13459 if (use_extend)
13460 {
17a2f251 13461 md_number_to_chars (buf, 0xf000 | extend, 2);
252b5132
RH
13462 fragp->fr_fix += 2;
13463 buf += 2;
13464 }
13465
17a2f251 13466 md_number_to_chars (buf, insn, 2);
252b5132
RH
13467 fragp->fr_fix += 2;
13468 buf += 2;
13469 }
13470 else
13471 {
4d7206a2
RS
13472 int first, second;
13473 fixS *fixp;
252b5132 13474
4d7206a2
RS
13475 first = RELAX_FIRST (fragp->fr_subtype);
13476 second = RELAX_SECOND (fragp->fr_subtype);
13477 fixp = (fixS *) fragp->fr_opcode;
252b5132 13478
584892a6
RS
13479 /* Possibly emit a warning if we've chosen the longer option. */
13480 if (((fragp->fr_subtype & RELAX_USE_SECOND) != 0)
13481 == ((fragp->fr_subtype & RELAX_SECOND_LONGER) != 0))
13482 {
13483 const char *msg = macro_warning (fragp->fr_subtype);
13484 if (msg != 0)
13485 as_warn_where (fragp->fr_file, fragp->fr_line, msg);
13486 }
13487
4d7206a2
RS
13488 /* Go through all the fixups for the first sequence. Disable them
13489 (by marking them as done) if we're going to use the second
13490 sequence instead. */
13491 while (fixp
13492 && fixp->fx_frag == fragp
13493 && fixp->fx_where < fragp->fr_fix - second)
13494 {
13495 if (fragp->fr_subtype & RELAX_USE_SECOND)
13496 fixp->fx_done = 1;
13497 fixp = fixp->fx_next;
13498 }
252b5132 13499
4d7206a2
RS
13500 /* Go through the fixups for the second sequence. Disable them if
13501 we're going to use the first sequence, otherwise adjust their
13502 addresses to account for the relaxation. */
13503 while (fixp && fixp->fx_frag == fragp)
13504 {
13505 if (fragp->fr_subtype & RELAX_USE_SECOND)
13506 fixp->fx_where -= first;
13507 else
13508 fixp->fx_done = 1;
13509 fixp = fixp->fx_next;
13510 }
13511
13512 /* Now modify the frag contents. */
13513 if (fragp->fr_subtype & RELAX_USE_SECOND)
13514 {
13515 char *start;
13516
13517 start = fragp->fr_literal + fragp->fr_fix - first - second;
13518 memmove (start, start + first, second);
13519 fragp->fr_fix -= first;
13520 }
13521 else
13522 fragp->fr_fix -= second;
252b5132
RH
13523 }
13524}
13525
13526#ifdef OBJ_ELF
13527
13528/* This function is called after the relocs have been generated.
13529 We've been storing mips16 text labels as odd. Here we convert them
13530 back to even for the convenience of the debugger. */
13531
13532void
17a2f251 13533mips_frob_file_after_relocs (void)
252b5132
RH
13534{
13535 asymbol **syms;
13536 unsigned int count, i;
13537
13538 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
13539 return;
13540
13541 syms = bfd_get_outsymbols (stdoutput);
13542 count = bfd_get_symcount (stdoutput);
13543 for (i = 0; i < count; i++, syms++)
13544 {
13545 if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
13546 && ((*syms)->value & 1) != 0)
13547 {
13548 (*syms)->value &= ~1;
13549 /* If the symbol has an odd size, it was probably computed
13550 incorrectly, so adjust that as well. */
13551 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
13552 ++elf_symbol (*syms)->internal_elf_sym.st_size;
13553 }
13554 }
13555}
13556
13557#endif
13558
13559/* This function is called whenever a label is defined. It is used
13560 when handling branch delays; if a branch has a label, we assume we
13561 can not move it. */
13562
13563void
17a2f251 13564mips_define_label (symbolS *sym)
252b5132
RH
13565{
13566 struct insn_label_list *l;
13567
13568 if (free_insn_labels == NULL)
13569 l = (struct insn_label_list *) xmalloc (sizeof *l);
13570 else
13571 {
13572 l = free_insn_labels;
13573 free_insn_labels = l->next;
13574 }
13575
13576 l->label = sym;
13577 l->next = insn_labels;
13578 insn_labels = l;
13579}
13580\f
13581#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13582
13583/* Some special processing for a MIPS ELF file. */
13584
13585void
17a2f251 13586mips_elf_final_processing (void)
252b5132
RH
13587{
13588 /* Write out the register information. */
316f5878 13589 if (mips_abi != N64_ABI)
252b5132
RH
13590 {
13591 Elf32_RegInfo s;
13592
13593 s.ri_gprmask = mips_gprmask;
13594 s.ri_cprmask[0] = mips_cprmask[0];
13595 s.ri_cprmask[1] = mips_cprmask[1];
13596 s.ri_cprmask[2] = mips_cprmask[2];
13597 s.ri_cprmask[3] = mips_cprmask[3];
13598 /* The gp_value field is set by the MIPS ELF backend. */
13599
13600 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
13601 ((Elf32_External_RegInfo *)
13602 mips_regmask_frag));
13603 }
13604 else
13605 {
13606 Elf64_Internal_RegInfo s;
13607
13608 s.ri_gprmask = mips_gprmask;
13609 s.ri_pad = 0;
13610 s.ri_cprmask[0] = mips_cprmask[0];
13611 s.ri_cprmask[1] = mips_cprmask[1];
13612 s.ri_cprmask[2] = mips_cprmask[2];
13613 s.ri_cprmask[3] = mips_cprmask[3];
13614 /* The gp_value field is set by the MIPS ELF backend. */
13615
13616 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
13617 ((Elf64_External_RegInfo *)
13618 mips_regmask_frag));
13619 }
13620
13621 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
13622 sort of BFD interface for this. */
13623 if (mips_any_noreorder)
13624 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
13625 if (mips_pic != NO_PIC)
143d77c5 13626 {
252b5132 13627 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
143d77c5
EC
13628 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
13629 }
13630 if (mips_abicalls)
13631 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
252b5132 13632
98d3f06f 13633 /* Set MIPS ELF flags for ASEs. */
a4672219
TS
13634 if (file_ase_mips16)
13635 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
1f25f5d3
CD
13636#if 0 /* XXX FIXME */
13637 if (file_ase_mips3d)
13638 elf_elfheader (stdoutput)->e_flags |= ???;
13639#endif
deec1734
CD
13640 if (file_ase_mdmx)
13641 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
1f25f5d3 13642
bdaaa2e1 13643 /* Set the MIPS ELF ABI flags. */
316f5878 13644 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
252b5132 13645 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
316f5878 13646 else if (mips_abi == O64_ABI)
252b5132 13647 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
316f5878 13648 else if (mips_abi == EABI_ABI)
252b5132 13649 {
316f5878 13650 if (!file_mips_gp32)
252b5132
RH
13651 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
13652 else
13653 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
13654 }
316f5878 13655 else if (mips_abi == N32_ABI)
be00bddd
TS
13656 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
13657
c9914766 13658 /* Nothing to do for N64_ABI. */
252b5132
RH
13659
13660 if (mips_32bitmode)
13661 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
13662}
13663
13664#endif /* OBJ_ELF || OBJ_MAYBE_ELF */
13665\f
beae10d5
KH
13666typedef struct proc {
13667 symbolS *isym;
13668 unsigned long reg_mask;
13669 unsigned long reg_offset;
13670 unsigned long fpreg_mask;
13671 unsigned long fpreg_offset;
13672 unsigned long frame_offset;
13673 unsigned long frame_reg;
13674 unsigned long pc_reg;
13675} procS;
252b5132
RH
13676
13677static procS cur_proc;
13678static procS *cur_proc_ptr;
13679static int numprocs;
13680
0a9ef439 13681/* Fill in an rs_align_code fragment. */
a19d8eb0 13682
0a9ef439 13683void
17a2f251 13684mips_handle_align (fragS *fragp)
a19d8eb0 13685{
0a9ef439
RH
13686 if (fragp->fr_type != rs_align_code)
13687 return;
13688
13689 if (mips_opts.mips16)
a19d8eb0
CP
13690 {
13691 static const unsigned char be_nop[] = { 0x65, 0x00 };
13692 static const unsigned char le_nop[] = { 0x00, 0x65 };
13693
0a9ef439
RH
13694 int bytes;
13695 char *p;
a19d8eb0 13696
0a9ef439
RH
13697 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
13698 p = fragp->fr_literal + fragp->fr_fix;
13699
13700 if (bytes & 1)
13701 {
13702 *p++ = 0;
f9419b05 13703 fragp->fr_fix++;
0a9ef439
RH
13704 }
13705
13706 memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
13707 fragp->fr_var = 2;
a19d8eb0
CP
13708 }
13709
0a9ef439 13710 /* For mips32, a nop is a zero, which we trivially get by doing nothing. */
a19d8eb0
CP
13711}
13712
252b5132 13713static void
17a2f251 13714md_obj_begin (void)
252b5132
RH
13715{
13716}
13717
13718static void
17a2f251 13719md_obj_end (void)
252b5132
RH
13720{
13721 /* check for premature end, nesting errors, etc */
13722 if (cur_proc_ptr)
9a41af64 13723 as_warn (_("missing .end at end of assembly"));
252b5132
RH
13724}
13725
13726static long
17a2f251 13727get_number (void)
252b5132
RH
13728{
13729 int negative = 0;
13730 long val = 0;
13731
13732 if (*input_line_pointer == '-')
13733 {
13734 ++input_line_pointer;
13735 negative = 1;
13736 }
3882b010 13737 if (!ISDIGIT (*input_line_pointer))
956cd1d6 13738 as_bad (_("expected simple number"));
252b5132
RH
13739 if (input_line_pointer[0] == '0')
13740 {
13741 if (input_line_pointer[1] == 'x')
13742 {
13743 input_line_pointer += 2;
3882b010 13744 while (ISXDIGIT (*input_line_pointer))
252b5132
RH
13745 {
13746 val <<= 4;
13747 val |= hex_value (*input_line_pointer++);
13748 }
13749 return negative ? -val : val;
13750 }
13751 else
13752 {
13753 ++input_line_pointer;
3882b010 13754 while (ISDIGIT (*input_line_pointer))
252b5132
RH
13755 {
13756 val <<= 3;
13757 val |= *input_line_pointer++ - '0';
13758 }
13759 return negative ? -val : val;
13760 }
13761 }
3882b010 13762 if (!ISDIGIT (*input_line_pointer))
252b5132
RH
13763 {
13764 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
13765 *input_line_pointer, *input_line_pointer);
956cd1d6 13766 as_warn (_("invalid number"));
252b5132
RH
13767 return -1;
13768 }
3882b010 13769 while (ISDIGIT (*input_line_pointer))
252b5132
RH
13770 {
13771 val *= 10;
13772 val += *input_line_pointer++ - '0';
13773 }
13774 return negative ? -val : val;
13775}
13776
13777/* The .file directive; just like the usual .file directive, but there
c5dd6aab
DJ
13778 is an initial number which is the ECOFF file index. In the non-ECOFF
13779 case .file implies DWARF-2. */
13780
13781static void
17a2f251 13782s_mips_file (int x ATTRIBUTE_UNUSED)
c5dd6aab 13783{
ecb4347a
DJ
13784 static int first_file_directive = 0;
13785
c5dd6aab
DJ
13786 if (ECOFF_DEBUGGING)
13787 {
13788 get_number ();
13789 s_app_file (0);
13790 }
13791 else
ecb4347a
DJ
13792 {
13793 char *filename;
13794
13795 filename = dwarf2_directive_file (0);
13796
13797 /* Versions of GCC up to 3.1 start files with a ".file"
13798 directive even for stabs output. Make sure that this
13799 ".file" is handled. Note that you need a version of GCC
13800 after 3.1 in order to support DWARF-2 on MIPS. */
13801 if (filename != NULL && ! first_file_directive)
13802 {
13803 (void) new_logical_line (filename, -1);
13804 s_app_file_string (filename);
13805 }
13806 first_file_directive = 1;
13807 }
c5dd6aab
DJ
13808}
13809
13810/* The .loc directive, implying DWARF-2. */
252b5132
RH
13811
13812static void
17a2f251 13813s_mips_loc (int x ATTRIBUTE_UNUSED)
252b5132 13814{
c5dd6aab
DJ
13815 if (!ECOFF_DEBUGGING)
13816 dwarf2_directive_loc (0);
252b5132
RH
13817}
13818
252b5132
RH
13819/* The .end directive. */
13820
13821static void
17a2f251 13822s_mips_end (int x ATTRIBUTE_UNUSED)
252b5132
RH
13823{
13824 symbolS *p;
252b5132 13825
7a621144
DJ
13826 /* Following functions need their own .frame and .cprestore directives. */
13827 mips_frame_reg_valid = 0;
13828 mips_cprestore_valid = 0;
13829
252b5132
RH
13830 if (!is_end_of_line[(unsigned char) *input_line_pointer])
13831 {
13832 p = get_symbol ();
13833 demand_empty_rest_of_line ();
13834 }
13835 else
13836 p = NULL;
13837
14949570 13838 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
13839 as_warn (_(".end not in text section"));
13840
13841 if (!cur_proc_ptr)
13842 {
13843 as_warn (_(".end directive without a preceding .ent directive."));
13844 demand_empty_rest_of_line ();
13845 return;
13846 }
13847
13848 if (p != NULL)
13849 {
13850 assert (S_GET_NAME (p));
13851 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->isym)))
13852 as_warn (_(".end symbol does not match .ent symbol."));
ecb4347a
DJ
13853
13854 if (debug_type == DEBUG_STABS)
13855 stabs_generate_asm_endfunc (S_GET_NAME (p),
13856 S_GET_NAME (p));
252b5132
RH
13857 }
13858 else
13859 as_warn (_(".end directive missing or unknown symbol"));
13860
ecb4347a
DJ
13861#ifdef OBJ_ELF
13862 /* Generate a .pdr section. */
dcd410fe
RO
13863 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING
13864 && mips_flag_pdr)
ecb4347a
DJ
13865 {
13866 segT saved_seg = now_seg;
13867 subsegT saved_subseg = now_subseg;
13868 valueT dot;
13869 expressionS exp;
13870 char *fragp;
252b5132 13871
ecb4347a 13872 dot = frag_now_fix ();
252b5132
RH
13873
13874#ifdef md_flush_pending_output
ecb4347a 13875 md_flush_pending_output ();
252b5132
RH
13876#endif
13877
ecb4347a
DJ
13878 assert (pdr_seg);
13879 subseg_set (pdr_seg, 0);
252b5132 13880
ecb4347a
DJ
13881 /* Write the symbol. */
13882 exp.X_op = O_symbol;
13883 exp.X_add_symbol = p;
13884 exp.X_add_number = 0;
13885 emit_expr (&exp, 4);
252b5132 13886
ecb4347a 13887 fragp = frag_more (7 * 4);
252b5132 13888
17a2f251
TS
13889 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
13890 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
13891 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
13892 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
13893 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
13894 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
13895 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
252b5132 13896
ecb4347a
DJ
13897 subseg_set (saved_seg, saved_subseg);
13898 }
13899#endif /* OBJ_ELF */
252b5132
RH
13900
13901 cur_proc_ptr = NULL;
13902}
13903
13904/* The .aent and .ent directives. */
13905
13906static void
17a2f251 13907s_mips_ent (int aent)
252b5132 13908{
252b5132 13909 symbolS *symbolP;
252b5132
RH
13910
13911 symbolP = get_symbol ();
13912 if (*input_line_pointer == ',')
f9419b05 13913 ++input_line_pointer;
252b5132 13914 SKIP_WHITESPACE ();
3882b010 13915 if (ISDIGIT (*input_line_pointer)
d9a62219 13916 || *input_line_pointer == '-')
874e8986 13917 get_number ();
252b5132 13918
14949570 13919 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
13920 as_warn (_(".ent or .aent not in text section."));
13921
13922 if (!aent && cur_proc_ptr)
9a41af64 13923 as_warn (_("missing .end"));
252b5132
RH
13924
13925 if (!aent)
13926 {
7a621144
DJ
13927 /* This function needs its own .frame and .cprestore directives. */
13928 mips_frame_reg_valid = 0;
13929 mips_cprestore_valid = 0;
13930
252b5132
RH
13931 cur_proc_ptr = &cur_proc;
13932 memset (cur_proc_ptr, '\0', sizeof (procS));
13933
13934 cur_proc_ptr->isym = symbolP;
13935
49309057 13936 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
252b5132 13937
f9419b05 13938 ++numprocs;
ecb4347a
DJ
13939
13940 if (debug_type == DEBUG_STABS)
13941 stabs_generate_asm_func (S_GET_NAME (symbolP),
13942 S_GET_NAME (symbolP));
252b5132
RH
13943 }
13944
13945 demand_empty_rest_of_line ();
13946}
13947
13948/* The .frame directive. If the mdebug section is present (IRIX 5 native)
bdaaa2e1 13949 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
252b5132 13950 s_mips_frame is used so that we can set the PDR information correctly.
bdaaa2e1 13951 We can't use the ecoff routines because they make reference to the ecoff
252b5132
RH
13952 symbol table (in the mdebug section). */
13953
13954static void
17a2f251 13955s_mips_frame (int ignore ATTRIBUTE_UNUSED)
252b5132 13956{
ecb4347a
DJ
13957#ifdef OBJ_ELF
13958 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
13959 {
13960 long val;
252b5132 13961
ecb4347a
DJ
13962 if (cur_proc_ptr == (procS *) NULL)
13963 {
13964 as_warn (_(".frame outside of .ent"));
13965 demand_empty_rest_of_line ();
13966 return;
13967 }
252b5132 13968
ecb4347a
DJ
13969 cur_proc_ptr->frame_reg = tc_get_register (1);
13970
13971 SKIP_WHITESPACE ();
13972 if (*input_line_pointer++ != ','
13973 || get_absolute_expression_and_terminator (&val) != ',')
13974 {
13975 as_warn (_("Bad .frame directive"));
13976 --input_line_pointer;
13977 demand_empty_rest_of_line ();
13978 return;
13979 }
252b5132 13980
ecb4347a
DJ
13981 cur_proc_ptr->frame_offset = val;
13982 cur_proc_ptr->pc_reg = tc_get_register (0);
252b5132 13983
252b5132 13984 demand_empty_rest_of_line ();
252b5132 13985 }
ecb4347a
DJ
13986 else
13987#endif /* OBJ_ELF */
13988 s_ignore (ignore);
252b5132
RH
13989}
13990
bdaaa2e1
KH
13991/* The .fmask and .mask directives. If the mdebug section is present
13992 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
252b5132 13993 embedded targets, s_mips_mask is used so that we can set the PDR
bdaaa2e1 13994 information correctly. We can't use the ecoff routines because they
252b5132
RH
13995 make reference to the ecoff symbol table (in the mdebug section). */
13996
13997static void
17a2f251 13998s_mips_mask (int reg_type)
252b5132 13999{
ecb4347a
DJ
14000#ifdef OBJ_ELF
14001 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
252b5132 14002 {
ecb4347a 14003 long mask, off;
252b5132 14004
ecb4347a
DJ
14005 if (cur_proc_ptr == (procS *) NULL)
14006 {
14007 as_warn (_(".mask/.fmask outside of .ent"));
14008 demand_empty_rest_of_line ();
14009 return;
14010 }
252b5132 14011
ecb4347a
DJ
14012 if (get_absolute_expression_and_terminator (&mask) != ',')
14013 {
14014 as_warn (_("Bad .mask/.fmask directive"));
14015 --input_line_pointer;
14016 demand_empty_rest_of_line ();
14017 return;
14018 }
252b5132 14019
ecb4347a
DJ
14020 off = get_absolute_expression ();
14021
14022 if (reg_type == 'F')
14023 {
14024 cur_proc_ptr->fpreg_mask = mask;
14025 cur_proc_ptr->fpreg_offset = off;
14026 }
14027 else
14028 {
14029 cur_proc_ptr->reg_mask = mask;
14030 cur_proc_ptr->reg_offset = off;
14031 }
14032
14033 demand_empty_rest_of_line ();
252b5132
RH
14034 }
14035 else
ecb4347a
DJ
14036#endif /* OBJ_ELF */
14037 s_ignore (reg_type);
252b5132
RH
14038}
14039
14040/* The .loc directive. */
14041
14042#if 0
14043static void
17a2f251 14044s_loc (int x)
252b5132
RH
14045{
14046 symbolS *symbolP;
14047 int lineno;
14048 int addroff;
14049
14050 assert (now_seg == text_section);
14051
14052 lineno = get_number ();
14053 addroff = frag_now_fix ();
14054
14055 symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
14056 S_SET_TYPE (symbolP, N_SLINE);
14057 S_SET_OTHER (symbolP, 0);
14058 S_SET_DESC (symbolP, lineno);
14059 symbolP->sy_segment = now_seg;
14060}
14061#endif
e7af610e 14062
316f5878
RS
14063/* A table describing all the processors gas knows about. Names are
14064 matched in the order listed.
e7af610e 14065
316f5878
RS
14066 To ease comparison, please keep this table in the same order as
14067 gcc's mips_cpu_info_table[]. */
e972090a
NC
14068static const struct mips_cpu_info mips_cpu_info_table[] =
14069{
316f5878
RS
14070 /* Entries for generic ISAs */
14071 { "mips1", 1, ISA_MIPS1, CPU_R3000 },
14072 { "mips2", 1, ISA_MIPS2, CPU_R6000 },
14073 { "mips3", 1, ISA_MIPS3, CPU_R4000 },
14074 { "mips4", 1, ISA_MIPS4, CPU_R8000 },
14075 { "mips5", 1, ISA_MIPS5, CPU_MIPS5 },
14076 { "mips32", 1, ISA_MIPS32, CPU_MIPS32 },
af7ee8bf 14077 { "mips32r2", 1, ISA_MIPS32R2, CPU_MIPS32R2 },
316f5878 14078 { "mips64", 1, ISA_MIPS64, CPU_MIPS64 },
5f74bc13 14079 { "mips64r2", 1, ISA_MIPS64R2, CPU_MIPS64R2 },
316f5878
RS
14080
14081 /* MIPS I */
14082 { "r3000", 0, ISA_MIPS1, CPU_R3000 },
14083 { "r2000", 0, ISA_MIPS1, CPU_R3000 },
14084 { "r3900", 0, ISA_MIPS1, CPU_R3900 },
14085
14086 /* MIPS II */
14087 { "r6000", 0, ISA_MIPS2, CPU_R6000 },
14088
14089 /* MIPS III */
14090 { "r4000", 0, ISA_MIPS3, CPU_R4000 },
14091 { "r4010", 0, ISA_MIPS2, CPU_R4010 },
14092 { "vr4100", 0, ISA_MIPS3, CPU_VR4100 },
14093 { "vr4111", 0, ISA_MIPS3, CPU_R4111 },
60b63b72
RS
14094 { "vr4120", 0, ISA_MIPS3, CPU_VR4120 },
14095 { "vr4130", 0, ISA_MIPS3, CPU_VR4120 },
14096 { "vr4181", 0, ISA_MIPS3, CPU_R4111 },
316f5878
RS
14097 { "vr4300", 0, ISA_MIPS3, CPU_R4300 },
14098 { "r4400", 0, ISA_MIPS3, CPU_R4400 },
14099 { "r4600", 0, ISA_MIPS3, CPU_R4600 },
14100 { "orion", 0, ISA_MIPS3, CPU_R4600 },
14101 { "r4650", 0, ISA_MIPS3, CPU_R4650 },
14102
14103 /* MIPS IV */
14104 { "r8000", 0, ISA_MIPS4, CPU_R8000 },
14105 { "r10000", 0, ISA_MIPS4, CPU_R10000 },
14106 { "r12000", 0, ISA_MIPS4, CPU_R12000 },
14107 { "vr5000", 0, ISA_MIPS4, CPU_R5000 },
60b63b72
RS
14108 { "vr5400", 0, ISA_MIPS4, CPU_VR5400 },
14109 { "vr5500", 0, ISA_MIPS4, CPU_VR5500 },
316f5878
RS
14110 { "rm5200", 0, ISA_MIPS4, CPU_R5000 },
14111 { "rm5230", 0, ISA_MIPS4, CPU_R5000 },
14112 { "rm5231", 0, ISA_MIPS4, CPU_R5000 },
14113 { "rm5261", 0, ISA_MIPS4, CPU_R5000 },
14114 { "rm5721", 0, ISA_MIPS4, CPU_R5000 },
5a7ea749
RS
14115 { "rm7000", 0, ISA_MIPS4, CPU_RM7000 },
14116 { "rm9000", 0, ISA_MIPS4, CPU_RM7000 },
316f5878
RS
14117
14118 /* MIPS 32 */
fef14a42 14119 { "4kc", 0, ISA_MIPS32, CPU_MIPS32 },
316f5878
RS
14120 { "4km", 0, ISA_MIPS32, CPU_MIPS32 },
14121 { "4kp", 0, ISA_MIPS32, CPU_MIPS32 },
e7af610e 14122
316f5878
RS
14123 /* MIPS 64 */
14124 { "5kc", 0, ISA_MIPS64, CPU_MIPS64 },
14125 { "20kc", 0, ISA_MIPS64, CPU_MIPS64 },
e7af610e 14126
c7a23324 14127 /* Broadcom SB-1 CPU core */
316f5878 14128 { "sb1", 0, ISA_MIPS64, CPU_SB1 },
e7af610e 14129
316f5878
RS
14130 /* End marker */
14131 { NULL, 0, 0, 0 }
14132};
e7af610e 14133
84ea6cf2 14134
316f5878
RS
14135/* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
14136 with a final "000" replaced by "k". Ignore case.
e7af610e 14137
316f5878 14138 Note: this function is shared between GCC and GAS. */
c6c98b38 14139
b34976b6 14140static bfd_boolean
17a2f251 14141mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
14142{
14143 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
14144 given++, canonical++;
14145
14146 return ((*given == 0 && *canonical == 0)
14147 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
14148}
14149
14150
14151/* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
14152 CPU name. We've traditionally allowed a lot of variation here.
14153
14154 Note: this function is shared between GCC and GAS. */
14155
b34976b6 14156static bfd_boolean
17a2f251 14157mips_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
14158{
14159 /* First see if the name matches exactly, or with a final "000"
14160 turned into "k". */
14161 if (mips_strict_matching_cpu_name_p (canonical, given))
b34976b6 14162 return TRUE;
316f5878
RS
14163
14164 /* If not, try comparing based on numerical designation alone.
14165 See if GIVEN is an unadorned number, or 'r' followed by a number. */
14166 if (TOLOWER (*given) == 'r')
14167 given++;
14168 if (!ISDIGIT (*given))
b34976b6 14169 return FALSE;
316f5878
RS
14170
14171 /* Skip over some well-known prefixes in the canonical name,
14172 hoping to find a number there too. */
14173 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
14174 canonical += 2;
14175 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
14176 canonical += 2;
14177 else if (TOLOWER (canonical[0]) == 'r')
14178 canonical += 1;
14179
14180 return mips_strict_matching_cpu_name_p (canonical, given);
14181}
14182
14183
14184/* Parse an option that takes the name of a processor as its argument.
14185 OPTION is the name of the option and CPU_STRING is the argument.
14186 Return the corresponding processor enumeration if the CPU_STRING is
14187 recognized, otherwise report an error and return null.
14188
14189 A similar function exists in GCC. */
e7af610e
NC
14190
14191static const struct mips_cpu_info *
17a2f251 14192mips_parse_cpu (const char *option, const char *cpu_string)
e7af610e 14193{
316f5878 14194 const struct mips_cpu_info *p;
e7af610e 14195
316f5878
RS
14196 /* 'from-abi' selects the most compatible architecture for the given
14197 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
14198 EABIs, we have to decide whether we're using the 32-bit or 64-bit
14199 version. Look first at the -mgp options, if given, otherwise base
14200 the choice on MIPS_DEFAULT_64BIT.
e7af610e 14201
316f5878
RS
14202 Treat NO_ABI like the EABIs. One reason to do this is that the
14203 plain 'mips' and 'mips64' configs have 'from-abi' as their default
14204 architecture. This code picks MIPS I for 'mips' and MIPS III for
14205 'mips64', just as we did in the days before 'from-abi'. */
14206 if (strcasecmp (cpu_string, "from-abi") == 0)
14207 {
14208 if (ABI_NEEDS_32BIT_REGS (mips_abi))
14209 return mips_cpu_info_from_isa (ISA_MIPS1);
14210
14211 if (ABI_NEEDS_64BIT_REGS (mips_abi))
14212 return mips_cpu_info_from_isa (ISA_MIPS3);
14213
14214 if (file_mips_gp32 >= 0)
14215 return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
14216
14217 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
14218 ? ISA_MIPS3
14219 : ISA_MIPS1);
14220 }
14221
14222 /* 'default' has traditionally been a no-op. Probably not very useful. */
14223 if (strcasecmp (cpu_string, "default") == 0)
14224 return 0;
14225
14226 for (p = mips_cpu_info_table; p->name != 0; p++)
14227 if (mips_matching_cpu_name_p (p->name, cpu_string))
14228 return p;
14229
14230 as_bad ("Bad value (%s) for %s", cpu_string, option);
14231 return 0;
e7af610e
NC
14232}
14233
316f5878
RS
14234/* Return the canonical processor information for ISA (a member of the
14235 ISA_MIPS* enumeration). */
14236
e7af610e 14237static const struct mips_cpu_info *
17a2f251 14238mips_cpu_info_from_isa (int isa)
e7af610e
NC
14239{
14240 int i;
14241
14242 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14243 if (mips_cpu_info_table[i].is_isa
316f5878 14244 && isa == mips_cpu_info_table[i].isa)
e7af610e
NC
14245 return (&mips_cpu_info_table[i]);
14246
e972090a 14247 return NULL;
e7af610e 14248}
fef14a42
TS
14249
14250static const struct mips_cpu_info *
17a2f251 14251mips_cpu_info_from_arch (int arch)
fef14a42
TS
14252{
14253 int i;
14254
14255 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14256 if (arch == mips_cpu_info_table[i].cpu)
14257 return (&mips_cpu_info_table[i]);
14258
14259 return NULL;
14260}
316f5878
RS
14261\f
14262static void
17a2f251 14263show (FILE *stream, const char *string, int *col_p, int *first_p)
316f5878
RS
14264{
14265 if (*first_p)
14266 {
14267 fprintf (stream, "%24s", "");
14268 *col_p = 24;
14269 }
14270 else
14271 {
14272 fprintf (stream, ", ");
14273 *col_p += 2;
14274 }
e7af610e 14275
316f5878
RS
14276 if (*col_p + strlen (string) > 72)
14277 {
14278 fprintf (stream, "\n%24s", "");
14279 *col_p = 24;
14280 }
14281
14282 fprintf (stream, "%s", string);
14283 *col_p += strlen (string);
14284
14285 *first_p = 0;
14286}
14287
14288void
17a2f251 14289md_show_usage (FILE *stream)
e7af610e 14290{
316f5878
RS
14291 int column, first;
14292 size_t i;
14293
14294 fprintf (stream, _("\
14295MIPS options:\n\
14296-membedded-pic generate embedded position independent code\n\
14297-EB generate big endian output\n\
14298-EL generate little endian output\n\
14299-g, -g2 do not remove unneeded NOPs or swap branches\n\
14300-G NUM allow referencing objects up to NUM bytes\n\
14301 implicitly with the gp register [default 8]\n"));
14302 fprintf (stream, _("\
14303-mips1 generate MIPS ISA I instructions\n\
14304-mips2 generate MIPS ISA II instructions\n\
14305-mips3 generate MIPS ISA III instructions\n\
14306-mips4 generate MIPS ISA IV instructions\n\
14307-mips5 generate MIPS ISA V instructions\n\
14308-mips32 generate MIPS32 ISA instructions\n\
af7ee8bf 14309-mips32r2 generate MIPS32 release 2 ISA instructions\n\
316f5878 14310-mips64 generate MIPS64 ISA instructions\n\
5f74bc13 14311-mips64r2 generate MIPS64 release 2 ISA instructions\n\
316f5878
RS
14312-march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
14313
14314 first = 1;
e7af610e
NC
14315
14316 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
316f5878
RS
14317 show (stream, mips_cpu_info_table[i].name, &column, &first);
14318 show (stream, "from-abi", &column, &first);
14319 fputc ('\n', stream);
e7af610e 14320
316f5878
RS
14321 fprintf (stream, _("\
14322-mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
14323-no-mCPU don't generate code specific to CPU.\n\
14324 For -mCPU and -no-mCPU, CPU must be one of:\n"));
14325
14326 first = 1;
14327
14328 show (stream, "3900", &column, &first);
14329 show (stream, "4010", &column, &first);
14330 show (stream, "4100", &column, &first);
14331 show (stream, "4650", &column, &first);
14332 fputc ('\n', stream);
14333
14334 fprintf (stream, _("\
14335-mips16 generate mips16 instructions\n\
14336-no-mips16 do not generate mips16 instructions\n"));
14337 fprintf (stream, _("\
d766e8ec 14338-mfix-vr4120 work around certain VR4120 errata\n\
316f5878
RS
14339-mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
14340-mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
14341-O0 remove unneeded NOPs, do not swap branches\n\
14342-O remove unneeded NOPs and swap branches\n\
316f5878
RS
14343--[no-]construct-floats [dis]allow floating point values to be constructed\n\
14344--trap, --no-break trap exception on div by 0 and mult overflow\n\
14345--break, --no-trap break exception on div by 0 and mult overflow\n"));
14346#ifdef OBJ_ELF
14347 fprintf (stream, _("\
14348-KPIC, -call_shared generate SVR4 position independent code\n\
14349-non_shared do not generate position independent code\n\
14350-xgot assume a 32 bit GOT\n\
dcd410fe 14351-mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
316f5878
RS
14352-mabi=ABI create ABI conformant object file for:\n"));
14353
14354 first = 1;
14355
14356 show (stream, "32", &column, &first);
14357 show (stream, "o64", &column, &first);
14358 show (stream, "n32", &column, &first);
14359 show (stream, "64", &column, &first);
14360 show (stream, "eabi", &column, &first);
14361
14362 fputc ('\n', stream);
14363
14364 fprintf (stream, _("\
14365-32 create o32 ABI object file (default)\n\
14366-n32 create n32 ABI object file\n\
14367-64 create 64 ABI object file\n"));
14368#endif
e7af610e 14369}
14e777e0
KB
14370
14371enum dwarf2_format
17a2f251 14372mips_dwarf2_format (void)
14e777e0
KB
14373{
14374 if (mips_abi == N64_ABI)
1de5b6a1
AO
14375 {
14376#ifdef TE_IRIX
14377 return dwarf2_format_64bit_irix;
14378#else
14379 return dwarf2_format_64bit;
14380#endif
14381 }
14e777e0
KB
14382 else
14383 return dwarf2_format_32bit;
14384}
73369e65
EC
14385
14386int
14387mips_dwarf2_addr_size (void)
14388{
14389 if (mips_abi == N64_ABI)
14390 return 8;
73369e65
EC
14391 else
14392 return 4;
14393}
This page took 2.906768 seconds and 4 git commands to generate.