* config/tc-mips.c (ISA_HAS_COPROC_DELAYS): Remove.
[deliverable/binutils-gdb.git] / gas / config / tc-mips.c
CommitLineData
252b5132 1/* tc-mips.c -- assemble code for a MIPS chip.
81912461
ILT
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3 2003, 2004 Free Software Foundation, Inc.
252b5132
RH
4 Contributed by the OSF and Ralph Campbell.
5 Written by Keith Knowles and Ralph Campbell, working independently.
6 Modified for ECOFF and R4000 support by Ian Lance Taylor of Cygnus
7 Support.
8
9 This file is part of GAS.
10
11 GAS is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2, or (at your option)
14 any later version.
15
16 GAS is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with GAS; see the file COPYING. If not, write to the Free
23 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 02111-1307, USA. */
25
26#include "as.h"
27#include "config.h"
28#include "subsegs.h"
3882b010 29#include "safe-ctype.h"
252b5132 30
252b5132 31#include <stdarg.h>
252b5132
RH
32
33#include "opcode/mips.h"
34#include "itbl-ops.h"
c5dd6aab 35#include "dwarf2dbg.h"
252b5132
RH
36
37#ifdef DEBUG
38#define DBG(x) printf x
39#else
40#define DBG(x)
41#endif
42
43#ifdef OBJ_MAYBE_ELF
44/* Clean up namespace so we can include obj-elf.h too. */
17a2f251
TS
45static int mips_output_flavor (void);
46static int mips_output_flavor (void) { return OUTPUT_FLAVOR; }
252b5132
RH
47#undef OBJ_PROCESS_STAB
48#undef OUTPUT_FLAVOR
49#undef S_GET_ALIGN
50#undef S_GET_SIZE
51#undef S_SET_ALIGN
52#undef S_SET_SIZE
252b5132
RH
53#undef obj_frob_file
54#undef obj_frob_file_after_relocs
55#undef obj_frob_symbol
56#undef obj_pop_insert
57#undef obj_sec_sym_ok_for_reloc
58#undef OBJ_COPY_SYMBOL_ATTRIBUTES
59
60#include "obj-elf.h"
61/* Fix any of them that we actually care about. */
62#undef OUTPUT_FLAVOR
63#define OUTPUT_FLAVOR mips_output_flavor()
64#endif
65
66#if defined (OBJ_ELF)
67#include "elf/mips.h"
68#endif
69
70#ifndef ECOFF_DEBUGGING
71#define NO_ECOFF_DEBUGGING
72#define ECOFF_DEBUGGING 0
73#endif
74
ecb4347a
DJ
75int mips_flag_mdebug = -1;
76
dcd410fe
RO
77/* Control generation of .pdr sections. Off by default on IRIX: the native
78 linker doesn't know about and discards them, but relocations against them
79 remain, leading to rld crashes. */
80#ifdef TE_IRIX
81int mips_flag_pdr = FALSE;
82#else
83int mips_flag_pdr = TRUE;
84#endif
85
252b5132
RH
86#include "ecoff.h"
87
88#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
89static char *mips_regmask_frag;
90#endif
91
85b51719 92#define ZERO 0
252b5132
RH
93#define AT 1
94#define TREG 24
95#define PIC_CALL_REG 25
96#define KT0 26
97#define KT1 27
98#define GP 28
99#define SP 29
100#define FP 30
101#define RA 31
102
103#define ILLEGAL_REG (32)
104
105/* Allow override of standard little-endian ECOFF format. */
106
107#ifndef ECOFF_LITTLE_FORMAT
108#define ECOFF_LITTLE_FORMAT "ecoff-littlemips"
109#endif
110
111extern int target_big_endian;
112
252b5132
RH
113/* The name of the readonly data section. */
114#define RDATA_SECTION_NAME (OUTPUT_FLAVOR == bfd_target_aout_flavour \
115 ? ".data" \
116 : OUTPUT_FLAVOR == bfd_target_ecoff_flavour \
117 ? ".rdata" \
056350c6
NC
118 : OUTPUT_FLAVOR == bfd_target_coff_flavour \
119 ? ".rdata" \
252b5132
RH
120 : OUTPUT_FLAVOR == bfd_target_elf_flavour \
121 ? ".rodata" \
122 : (abort (), ""))
123
a325df1d
TS
124/* The ABI to use. */
125enum mips_abi_level
126{
127 NO_ABI = 0,
128 O32_ABI,
129 O64_ABI,
130 N32_ABI,
131 N64_ABI,
132 EABI_ABI
133};
134
135/* MIPS ABI we are using for this output file. */
316f5878 136static enum mips_abi_level mips_abi = NO_ABI;
a325df1d 137
143d77c5
EC
138/* Whether or not we have code that can call pic code. */
139int mips_abicalls = FALSE;
140
252b5132
RH
141/* This is the set of options which may be modified by the .set
142 pseudo-op. We use a struct so that .set push and .set pop are more
143 reliable. */
144
e972090a
NC
145struct mips_set_options
146{
252b5132
RH
147 /* MIPS ISA (Instruction Set Architecture) level. This is set to -1
148 if it has not been initialized. Changed by `.set mipsN', and the
149 -mipsN command line option, and the default CPU. */
150 int isa;
1f25f5d3
CD
151 /* Enabled Application Specific Extensions (ASEs). These are set to -1
152 if they have not been initialized. Changed by `.set <asename>', by
153 command line options, and based on the default architecture. */
154 int ase_mips3d;
deec1734 155 int ase_mdmx;
252b5132
RH
156 /* Whether we are assembling for the mips16 processor. 0 if we are
157 not, 1 if we are, and -1 if the value has not been initialized.
158 Changed by `.set mips16' and `.set nomips16', and the -mips16 and
159 -nomips16 command line options, and the default CPU. */
160 int mips16;
161 /* Non-zero if we should not reorder instructions. Changed by `.set
162 reorder' and `.set noreorder'. */
163 int noreorder;
164 /* Non-zero if we should not permit the $at ($1) register to be used
165 in instructions. Changed by `.set at' and `.set noat'. */
166 int noat;
167 /* Non-zero if we should warn when a macro instruction expands into
168 more than one machine instruction. Changed by `.set nomacro' and
169 `.set macro'. */
170 int warn_about_macros;
171 /* Non-zero if we should not move instructions. Changed by `.set
172 move', `.set volatile', `.set nomove', and `.set novolatile'. */
173 int nomove;
174 /* Non-zero if we should not optimize branches by moving the target
175 of the branch into the delay slot. Actually, we don't perform
176 this optimization anyhow. Changed by `.set bopt' and `.set
177 nobopt'. */
178 int nobopt;
179 /* Non-zero if we should not autoextend mips16 instructions.
180 Changed by `.set autoextend' and `.set noautoextend'. */
181 int noautoextend;
a325df1d
TS
182 /* Restrict general purpose registers and floating point registers
183 to 32 bit. This is initially determined when -mgp32 or -mfp32
184 is passed but can changed if the assembler code uses .set mipsN. */
185 int gp32;
186 int fp32;
fef14a42
TS
187 /* MIPS architecture (CPU) type. Changed by .set arch=FOO, the -march
188 command line option, and the default CPU. */
189 int arch;
252b5132
RH
190};
191
a325df1d 192/* True if -mgp32 was passed. */
a8e8e863 193static int file_mips_gp32 = -1;
a325df1d
TS
194
195/* True if -mfp32 was passed. */
a8e8e863 196static int file_mips_fp32 = -1;
a325df1d 197
252b5132 198/* This is the struct we use to hold the current set of options. Note
a4672219 199 that we must set the isa field to ISA_UNKNOWN and the ASE fields to
e7af610e 200 -1 to indicate that they have not been initialized. */
252b5132 201
e972090a
NC
202static struct mips_set_options mips_opts =
203{
fef14a42 204 ISA_UNKNOWN, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, CPU_UNKNOWN
e7af610e 205};
252b5132
RH
206
207/* These variables are filled in with the masks of registers used.
208 The object format code reads them and puts them in the appropriate
209 place. */
210unsigned long mips_gprmask;
211unsigned long mips_cprmask[4];
212
213/* MIPS ISA we are using for this output file. */
e7af610e 214static int file_mips_isa = ISA_UNKNOWN;
252b5132 215
a4672219
TS
216/* True if -mips16 was passed or implied by arguments passed on the
217 command line (e.g., by -march). */
218static int file_ase_mips16;
219
1f25f5d3
CD
220/* True if -mips3d was passed or implied by arguments passed on the
221 command line (e.g., by -march). */
222static int file_ase_mips3d;
223
deec1734
CD
224/* True if -mdmx was passed or implied by arguments passed on the
225 command line (e.g., by -march). */
226static int file_ase_mdmx;
227
ec68c924 228/* The argument of the -march= flag. The architecture we are assembling. */
fef14a42 229static int file_mips_arch = CPU_UNKNOWN;
316f5878 230static const char *mips_arch_string;
ec68c924
EC
231
232/* The argument of the -mtune= flag. The architecture for which we
233 are optimizing. */
234static int mips_tune = CPU_UNKNOWN;
316f5878 235static const char *mips_tune_string;
ec68c924 236
316f5878 237/* True when generating 32-bit code for a 64-bit processor. */
252b5132
RH
238static int mips_32bitmode = 0;
239
316f5878
RS
240/* True if the given ABI requires 32-bit registers. */
241#define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
242
243/* Likewise 64-bit registers. */
244#define ABI_NEEDS_64BIT_REGS(ABI) \
245 ((ABI) == N32_ABI \
246 || (ABI) == N64_ABI \
247 || (ABI) == O64_ABI)
248
bdaaa2e1 249/* Return true if ISA supports 64 bit gp register instructions. */
9ce8a5dd 250#define ISA_HAS_64BIT_REGS(ISA) ( \
e7af610e
NC
251 (ISA) == ISA_MIPS3 \
252 || (ISA) == ISA_MIPS4 \
84ea6cf2 253 || (ISA) == ISA_MIPS5 \
d1cf510e 254 || (ISA) == ISA_MIPS64 \
5f74bc13 255 || (ISA) == ISA_MIPS64R2 \
9ce8a5dd
GRK
256 )
257
af7ee8bf
CD
258/* Return true if ISA supports 64-bit right rotate (dror et al.)
259 instructions. */
260#define ISA_HAS_DROR(ISA) ( \
5f74bc13 261 (ISA) == ISA_MIPS64R2 \
af7ee8bf
CD
262 )
263
264/* Return true if ISA supports 32-bit right rotate (ror et al.)
265 instructions. */
266#define ISA_HAS_ROR(ISA) ( \
267 (ISA) == ISA_MIPS32R2 \
5f74bc13 268 || (ISA) == ISA_MIPS64R2 \
af7ee8bf
CD
269 )
270
e013f690 271#define HAVE_32BIT_GPRS \
316f5878 272 (mips_opts.gp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
ca4e0257 273
e013f690 274#define HAVE_32BIT_FPRS \
316f5878 275 (mips_opts.fp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
ca4e0257
RS
276
277#define HAVE_64BIT_GPRS (! HAVE_32BIT_GPRS)
278#define HAVE_64BIT_FPRS (! HAVE_32BIT_FPRS)
279
316f5878 280#define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
e013f690 281
316f5878 282#define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
e013f690
TS
283
284/* We can only have 64bit addresses if the object file format
285 supports it. */
afdbd6d0
CD
286#define HAVE_32BIT_ADDRESSES \
287 (HAVE_32BIT_GPRS \
288 || ((bfd_arch_bits_per_address (stdoutput) == 32 \
289 || ! HAVE_64BIT_OBJECTS) \
290 && mips_pic != EMBEDDED_PIC))
e013f690
TS
291
292#define HAVE_64BIT_ADDRESSES (! HAVE_32BIT_ADDRESSES)
ca4e0257 293
b7c7d6c1
TS
294/* Addresses are loaded in different ways, depending on the address size
295 in use. The n32 ABI Documentation also mandates the use of additions
296 with overflow checking, but existing implementations don't follow it. */
f899b4b8 297#define ADDRESS_ADD_INSN \
b7c7d6c1 298 (HAVE_32BIT_ADDRESSES ? "addu" : "daddu")
f899b4b8
TS
299
300#define ADDRESS_ADDI_INSN \
b7c7d6c1 301 (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu")
f899b4b8
TS
302
303#define ADDRESS_LOAD_INSN \
304 (HAVE_32BIT_ADDRESSES ? "lw" : "ld")
305
306#define ADDRESS_STORE_INSN \
307 (HAVE_32BIT_ADDRESSES ? "sw" : "sd")
308
a4672219 309/* Return true if the given CPU supports the MIPS16 ASE. */
3396de36
TS
310#define CPU_HAS_MIPS16(cpu) \
311 (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0 \
312 || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
a4672219 313
1f25f5d3
CD
314/* Return true if the given CPU supports the MIPS3D ASE. */
315#define CPU_HAS_MIPS3D(cpu) ((cpu) == CPU_SB1 \
316 )
317
deec1734 318/* Return true if the given CPU supports the MDMX ASE. */
b34976b6 319#define CPU_HAS_MDMX(cpu) (FALSE \
deec1734
CD
320 )
321
60b63b72
RS
322/* True if CPU has a dror instruction. */
323#define CPU_HAS_DROR(CPU) ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
324
325/* True if CPU has a ror instruction. */
326#define CPU_HAS_ROR(CPU) CPU_HAS_DROR (CPU)
327
bdaaa2e1 328/* Whether the processor uses hardware interlocks to protect
252b5132 329 reads from the HI and LO registers, and thus does not
ec68c924 330 require nops to be inserted. */
252b5132 331
fef14a42
TS
332#define hilo_interlocks (mips_opts.arch == CPU_R4010 \
333 || mips_opts.arch == CPU_VR5500 \
5a7ea749 334 || mips_opts.arch == CPU_RM7000 \
fef14a42 335 || mips_opts.arch == CPU_SB1 \
252b5132
RH
336 )
337
338/* Whether the processor uses hardware interlocks to protect reads
81912461
ILT
339 from the GPRs after they are loaded from memory, and thus does not
340 require nops to be inserted. This applies to instructions marked
341 INSN_LOAD_MEMORY_DELAY. These nops are only required at MIPS ISA
342 level I. */
252b5132 343#define gpr_interlocks \
e7af610e 344 (mips_opts.isa != ISA_MIPS1 \
fef14a42
TS
345 || mips_opts.arch == CPU_VR5400 \
346 || mips_opts.arch == CPU_VR5500 \
347 || mips_opts.arch == CPU_R3900)
252b5132 348
81912461
ILT
349/* Whether the processor uses hardware interlocks to avoid delays
350 required by coprocessor instructions, and thus does not require
351 nops to be inserted. This applies to instructions marked
352 INSN_LOAD_COPROC_DELAY, INSN_COPROC_MOVE_DELAY, and to delays
353 between instructions marked INSN_WRITE_COND_CODE and ones marked
354 INSN_READ_COND_CODE. These nops are only required at MIPS ISA
355 levels I, II, and III. */
bdaaa2e1 356/* Itbl support may require additional care here. */
81912461
ILT
357#define cop_interlocks \
358 ((mips_opts.isa != ISA_MIPS1 \
359 && mips_opts.isa != ISA_MIPS2 \
360 && mips_opts.isa != ISA_MIPS3) \
361 || mips_opts.arch == CPU_R4300 \
362 || mips_opts.arch == CPU_VR5400 \
363 || mips_opts.arch == CPU_VR5500 \
364 || mips_opts.arch == CPU_SB1 \
365 )
366
367/* Whether the processor uses hardware interlocks to protect reads
368 from coprocessor registers after they are loaded from memory, and
369 thus does not require nops to be inserted. This applies to
370 instructions marked INSN_COPROC_MEMORY_DELAY. These nops are only
371 requires at MIPS ISA level I. */
372#define cop_mem_interlocks (mips_opts.isa != ISA_MIPS1)
252b5132 373
6b76fefe
CM
374/* Is this a mfhi or mflo instruction? */
375#define MF_HILO_INSN(PINFO) \
376 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
377
252b5132
RH
378/* MIPS PIC level. */
379
a161fe53 380enum mips_pic_level mips_pic;
252b5132 381
39c0a331
L
382/* Warn about all NOPS that the assembler generates. */
383static int warn_nops = 0;
384
c9914766 385/* 1 if we should generate 32 bit offsets from the $gp register in
252b5132 386 SVR4_PIC mode. Currently has no meaning in other modes. */
c9914766 387static int mips_big_got = 0;
252b5132
RH
388
389/* 1 if trap instructions should used for overflow rather than break
390 instructions. */
c9914766 391static int mips_trap = 0;
252b5132 392
119d663a 393/* 1 if double width floating point constants should not be constructed
b6ff326e 394 by assembling two single width halves into two single width floating
119d663a
NC
395 point registers which just happen to alias the double width destination
396 register. On some architectures this aliasing can be disabled by a bit
d547a75e 397 in the status register, and the setting of this bit cannot be determined
119d663a
NC
398 automatically at assemble time. */
399static int mips_disable_float_construction;
400
252b5132
RH
401/* Non-zero if any .set noreorder directives were used. */
402
403static int mips_any_noreorder;
404
6b76fefe
CM
405/* Non-zero if nops should be inserted when the register referenced in
406 an mfhi/mflo instruction is read in the next two instructions. */
407static int mips_7000_hilo_fix;
408
252b5132 409/* The size of the small data section. */
156c2f8b 410static unsigned int g_switch_value = 8;
252b5132
RH
411/* Whether the -G option was used. */
412static int g_switch_seen = 0;
413
414#define N_RMASK 0xc4
415#define N_VFP 0xd4
416
417/* If we can determine in advance that GP optimization won't be
418 possible, we can skip the relaxation stuff that tries to produce
419 GP-relative references. This makes delay slot optimization work
420 better.
421
422 This function can only provide a guess, but it seems to work for
fba2b7f9
GK
423 gcc output. It needs to guess right for gcc, otherwise gcc
424 will put what it thinks is a GP-relative instruction in a branch
425 delay slot.
252b5132
RH
426
427 I don't know if a fix is needed for the SVR4_PIC mode. I've only
428 fixed it for the non-PIC mode. KR 95/04/07 */
17a2f251 429static int nopic_need_relax (symbolS *, int);
252b5132
RH
430
431/* handle of the OPCODE hash table */
432static struct hash_control *op_hash = NULL;
433
434/* The opcode hash table we use for the mips16. */
435static struct hash_control *mips16_op_hash = NULL;
436
437/* This array holds the chars that always start a comment. If the
438 pre-processor is disabled, these aren't very useful */
439const char comment_chars[] = "#";
440
441/* This array holds the chars that only start a comment at the beginning of
442 a line. If the line seems to have the form '# 123 filename'
443 .line and .file directives will appear in the pre-processed output */
444/* Note that input_file.c hand checks for '#' at the beginning of the
445 first line of the input file. This is because the compiler outputs
bdaaa2e1 446 #NO_APP at the beginning of its output. */
252b5132
RH
447/* Also note that C style comments are always supported. */
448const char line_comment_chars[] = "#";
449
bdaaa2e1 450/* This array holds machine specific line separator characters. */
63a0b638 451const char line_separator_chars[] = ";";
252b5132
RH
452
453/* Chars that can be used to separate mant from exp in floating point nums */
454const char EXP_CHARS[] = "eE";
455
456/* Chars that mean this number is a floating point constant */
457/* As in 0f12.456 */
458/* or 0d1.2345e12 */
459const char FLT_CHARS[] = "rRsSfFdDxXpP";
460
461/* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
462 changed in read.c . Ideally it shouldn't have to know about it at all,
463 but nothing is ideal around here.
464 */
465
466static char *insn_error;
467
468static int auto_align = 1;
469
470/* When outputting SVR4 PIC code, the assembler needs to know the
471 offset in the stack frame from which to restore the $gp register.
472 This is set by the .cprestore pseudo-op, and saved in this
473 variable. */
474static offsetT mips_cprestore_offset = -1;
475
67c1ffbe 476/* Similar for NewABI PIC code, where $gp is callee-saved. NewABI has some
6478892d 477 more optimizations, it can use a register value instead of a memory-saved
956cd1d6 478 offset and even an other register than $gp as global pointer. */
6478892d
TS
479static offsetT mips_cpreturn_offset = -1;
480static int mips_cpreturn_register = -1;
481static int mips_gp_register = GP;
def2e0dd 482static int mips_gprel_offset = 0;
6478892d 483
7a621144
DJ
484/* Whether mips_cprestore_offset has been set in the current function
485 (or whether it has already been warned about, if not). */
486static int mips_cprestore_valid = 0;
487
252b5132
RH
488/* This is the register which holds the stack frame, as set by the
489 .frame pseudo-op. This is needed to implement .cprestore. */
490static int mips_frame_reg = SP;
491
7a621144
DJ
492/* Whether mips_frame_reg has been set in the current function
493 (or whether it has already been warned about, if not). */
494static int mips_frame_reg_valid = 0;
495
252b5132
RH
496/* To output NOP instructions correctly, we need to keep information
497 about the previous two instructions. */
498
499/* Whether we are optimizing. The default value of 2 means to remove
500 unneeded NOPs and swap branch instructions when possible. A value
501 of 1 means to not swap branches. A value of 0 means to always
502 insert NOPs. */
503static int mips_optimize = 2;
504
505/* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
506 equivalent to seeing no -g option at all. */
507static int mips_debug = 0;
508
509/* The previous instruction. */
510static struct mips_cl_insn prev_insn;
511
512/* The instruction before prev_insn. */
513static struct mips_cl_insn prev_prev_insn;
514
515/* If we don't want information for prev_insn or prev_prev_insn, we
516 point the insn_mo field at this dummy integer. */
43841e91 517static const struct mips_opcode dummy_opcode = { NULL, NULL, 0, 0, 0, 0 };
252b5132
RH
518
519/* Non-zero if prev_insn is valid. */
520static int prev_insn_valid;
521
522/* The frag for the previous instruction. */
523static struct frag *prev_insn_frag;
524
525/* The offset into prev_insn_frag for the previous instruction. */
526static long prev_insn_where;
527
528/* The reloc type for the previous instruction, if any. */
f6688943 529static bfd_reloc_code_real_type prev_insn_reloc_type[3];
252b5132
RH
530
531/* The reloc for the previous instruction, if any. */
f6688943 532static fixS *prev_insn_fixp[3];
252b5132
RH
533
534/* Non-zero if the previous instruction was in a delay slot. */
535static int prev_insn_is_delay_slot;
536
537/* Non-zero if the previous instruction was in a .set noreorder. */
538static int prev_insn_unreordered;
539
540/* Non-zero if the previous instruction uses an extend opcode (if
541 mips16). */
542static int prev_insn_extended;
543
544/* Non-zero if the previous previous instruction was in a .set
545 noreorder. */
546static int prev_prev_insn_unreordered;
547
548/* If this is set, it points to a frag holding nop instructions which
549 were inserted before the start of a noreorder section. If those
550 nops turn out to be unnecessary, the size of the frag can be
551 decreased. */
552static fragS *prev_nop_frag;
553
554/* The number of nop instructions we created in prev_nop_frag. */
555static int prev_nop_frag_holds;
556
557/* The number of nop instructions that we know we need in
bdaaa2e1 558 prev_nop_frag. */
252b5132
RH
559static int prev_nop_frag_required;
560
561/* The number of instructions we've seen since prev_nop_frag. */
562static int prev_nop_frag_since;
563
564/* For ECOFF and ELF, relocations against symbols are done in two
565 parts, with a HI relocation and a LO relocation. Each relocation
566 has only 16 bits of space to store an addend. This means that in
567 order for the linker to handle carries correctly, it must be able
568 to locate both the HI and the LO relocation. This means that the
569 relocations must appear in order in the relocation table.
570
571 In order to implement this, we keep track of each unmatched HI
572 relocation. We then sort them so that they immediately precede the
bdaaa2e1 573 corresponding LO relocation. */
252b5132 574
e972090a
NC
575struct mips_hi_fixup
576{
252b5132
RH
577 /* Next HI fixup. */
578 struct mips_hi_fixup *next;
579 /* This fixup. */
580 fixS *fixp;
581 /* The section this fixup is in. */
582 segT seg;
583};
584
585/* The list of unmatched HI relocs. */
586
587static struct mips_hi_fixup *mips_hi_fixup_list;
588
64bdfcaf
RS
589/* The frag containing the last explicit relocation operator.
590 Null if explicit relocations have not been used. */
591
592static fragS *prev_reloc_op_frag;
593
252b5132
RH
594/* Map normal MIPS register numbers to mips16 register numbers. */
595
596#define X ILLEGAL_REG
e972090a
NC
597static const int mips32_to_16_reg_map[] =
598{
252b5132
RH
599 X, X, 2, 3, 4, 5, 6, 7,
600 X, X, X, X, X, X, X, X,
601 0, 1, X, X, X, X, X, X,
602 X, X, X, X, X, X, X, X
603};
604#undef X
605
606/* Map mips16 register numbers to normal MIPS register numbers. */
607
e972090a
NC
608static const unsigned int mips16_to_32_reg_map[] =
609{
252b5132
RH
610 16, 17, 2, 3, 4, 5, 6, 7
611};
60b63b72
RS
612
613static int mips_fix_4122_bugs;
4a6a3df4
AO
614
615/* We don't relax branches by default, since this causes us to expand
616 `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
617 fail to compute the offset before expanding the macro to the most
618 efficient expansion. */
619
620static int mips_relax_branch;
252b5132
RH
621\f
622/* Since the MIPS does not have multiple forms of PC relative
623 instructions, we do not have to do relaxing as is done on other
624 platforms. However, we do have to handle GP relative addressing
625 correctly, which turns out to be a similar problem.
626
627 Every macro that refers to a symbol can occur in (at least) two
628 forms, one with GP relative addressing and one without. For
629 example, loading a global variable into a register generally uses
630 a macro instruction like this:
631 lw $4,i
632 If i can be addressed off the GP register (this is true if it is in
633 the .sbss or .sdata section, or if it is known to be smaller than
634 the -G argument) this will generate the following instruction:
635 lw $4,i($gp)
636 This instruction will use a GPREL reloc. If i can not be addressed
637 off the GP register, the following instruction sequence will be used:
638 lui $at,i
639 lw $4,i($at)
640 In this case the first instruction will have a HI16 reloc, and the
641 second reloc will have a LO16 reloc. Both relocs will be against
642 the symbol i.
643
644 The issue here is that we may not know whether i is GP addressable
645 until after we see the instruction that uses it. Therefore, we
646 want to be able to choose the final instruction sequence only at
647 the end of the assembly. This is similar to the way other
648 platforms choose the size of a PC relative instruction only at the
649 end of assembly.
650
651 When generating position independent code we do not use GP
652 addressing in quite the same way, but the issue still arises as
653 external symbols and local symbols must be handled differently.
654
655 We handle these issues by actually generating both possible
656 instruction sequences. The longer one is put in a frag_var with
657 type rs_machine_dependent. We encode what to do with the frag in
658 the subtype field. We encode (1) the number of existing bytes to
659 replace, (2) the number of new bytes to use, (3) the offset from
660 the start of the existing bytes to the first reloc we must generate
661 (that is, the offset is applied from the start of the existing
662 bytes after they are replaced by the new bytes, if any), (4) the
663 offset from the start of the existing bytes to the second reloc,
664 (5) whether a third reloc is needed (the third reloc is always four
665 bytes after the second reloc), and (6) whether to warn if this
666 variant is used (this is sometimes needed if .set nomacro or .set
667 noat is in effect). All these numbers are reasonably small.
668
669 Generating two instruction sequences must be handled carefully to
670 ensure that delay slots are handled correctly. Fortunately, there
671 are a limited number of cases. When the second instruction
672 sequence is generated, append_insn is directed to maintain the
673 existing delay slot information, so it continues to apply to any
674 code after the second instruction sequence. This means that the
675 second instruction sequence must not impose any requirements not
676 required by the first instruction sequence.
677
678 These variant frags are then handled in functions called by the
679 machine independent code. md_estimate_size_before_relax returns
680 the final size of the frag. md_convert_frag sets up the final form
681 of the frag. tc_gen_reloc adjust the first reloc and adds a second
682 one if needed. */
683#define RELAX_ENCODE(old, new, reloc1, reloc2, reloc3, warn) \
684 ((relax_substateT) \
685 (((old) << 23) \
686 | ((new) << 16) \
687 | (((reloc1) + 64) << 9) \
688 | (((reloc2) + 64) << 2) \
689 | ((reloc3) ? (1 << 1) : 0) \
690 | ((warn) ? 1 : 0)))
691#define RELAX_OLD(i) (((i) >> 23) & 0x7f)
692#define RELAX_NEW(i) (((i) >> 16) & 0x7f)
9a41af64
TS
693#define RELAX_RELOC1(i) ((valueT) (((i) >> 9) & 0x7f) - 64)
694#define RELAX_RELOC2(i) ((valueT) (((i) >> 2) & 0x7f) - 64)
252b5132
RH
695#define RELAX_RELOC3(i) (((i) >> 1) & 1)
696#define RELAX_WARN(i) ((i) & 1)
697
4a6a3df4
AO
698/* Branch without likely bit. If label is out of range, we turn:
699
700 beq reg1, reg2, label
701 delay slot
702
703 into
704
705 bne reg1, reg2, 0f
706 nop
707 j label
708 0: delay slot
709
710 with the following opcode replacements:
711
712 beq <-> bne
713 blez <-> bgtz
714 bltz <-> bgez
715 bc1f <-> bc1t
716
717 bltzal <-> bgezal (with jal label instead of j label)
718
719 Even though keeping the delay slot instruction in the delay slot of
720 the branch would be more efficient, it would be very tricky to do
721 correctly, because we'd have to introduce a variable frag *after*
722 the delay slot instruction, and expand that instead. Let's do it
723 the easy way for now, even if the branch-not-taken case now costs
724 one additional instruction. Out-of-range branches are not supposed
725 to be common, anyway.
726
727 Branch likely. If label is out of range, we turn:
728
729 beql reg1, reg2, label
730 delay slot (annulled if branch not taken)
731
732 into
733
734 beql reg1, reg2, 1f
735 nop
736 beql $0, $0, 2f
737 nop
738 1: j[al] label
739 delay slot (executed only if branch taken)
740 2:
741
742 It would be possible to generate a shorter sequence by losing the
743 likely bit, generating something like:
b34976b6 744
4a6a3df4
AO
745 bne reg1, reg2, 0f
746 nop
747 j[al] label
748 delay slot (executed only if branch taken)
749 0:
750
751 beql -> bne
752 bnel -> beq
753 blezl -> bgtz
754 bgtzl -> blez
755 bltzl -> bgez
756 bgezl -> bltz
757 bc1fl -> bc1t
758 bc1tl -> bc1f
759
760 bltzall -> bgezal (with jal label instead of j label)
761 bgezall -> bltzal (ditto)
762
763
764 but it's not clear that it would actually improve performance. */
af6ae2ad 765#define RELAX_BRANCH_ENCODE(uncond, likely, link, toofar) \
4a6a3df4
AO
766 ((relax_substateT) \
767 (0xc0000000 \
768 | ((toofar) ? 1 : 0) \
769 | ((link) ? 2 : 0) \
770 | ((likely) ? 4 : 0) \
af6ae2ad 771 | ((uncond) ? 8 : 0)))
4a6a3df4 772#define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
4a6a3df4
AO
773#define RELAX_BRANCH_UNCOND(i) (((i) & 8) != 0)
774#define RELAX_BRANCH_LIKELY(i) (((i) & 4) != 0)
775#define RELAX_BRANCH_LINK(i) (((i) & 2) != 0)
ae6063d4 776#define RELAX_BRANCH_TOOFAR(i) (((i) & 1) != 0)
4a6a3df4 777
252b5132
RH
778/* For mips16 code, we use an entirely different form of relaxation.
779 mips16 supports two versions of most instructions which take
780 immediate values: a small one which takes some small value, and a
781 larger one which takes a 16 bit value. Since branches also follow
782 this pattern, relaxing these values is required.
783
784 We can assemble both mips16 and normal MIPS code in a single
785 object. Therefore, we need to support this type of relaxation at
786 the same time that we support the relaxation described above. We
787 use the high bit of the subtype field to distinguish these cases.
788
789 The information we store for this type of relaxation is the
790 argument code found in the opcode file for this relocation, whether
791 the user explicitly requested a small or extended form, and whether
792 the relocation is in a jump or jal delay slot. That tells us the
793 size of the value, and how it should be stored. We also store
794 whether the fragment is considered to be extended or not. We also
795 store whether this is known to be a branch to a different section,
796 whether we have tried to relax this frag yet, and whether we have
797 ever extended a PC relative fragment because of a shift count. */
798#define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
799 (0x80000000 \
800 | ((type) & 0xff) \
801 | ((small) ? 0x100 : 0) \
802 | ((ext) ? 0x200 : 0) \
803 | ((dslot) ? 0x400 : 0) \
804 | ((jal_dslot) ? 0x800 : 0))
4a6a3df4 805#define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
252b5132
RH
806#define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
807#define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
808#define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
809#define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
810#define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
811#define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
812#define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
813#define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
814#define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
815#define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
816#define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
885add95
CD
817
818/* Is the given value a sign-extended 32-bit value? */
819#define IS_SEXT_32BIT_NUM(x) \
820 (((x) &~ (offsetT) 0x7fffffff) == 0 \
821 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
822
823/* Is the given value a sign-extended 16-bit value? */
824#define IS_SEXT_16BIT_NUM(x) \
825 (((x) &~ (offsetT) 0x7fff) == 0 \
826 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
827
252b5132
RH
828\f
829/* Prototypes for static functions. */
830
17a2f251 831#define internalError() \
252b5132 832 as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
252b5132
RH
833
834enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
835
b34976b6 836static void append_insn
17a2f251
TS
837 (char *place, struct mips_cl_insn *ip, expressionS *p,
838 bfd_reloc_code_real_type *r);
839static void mips_no_prev_insn (int);
b34976b6 840static void mips16_macro_build
17a2f251
TS
841 (char *, int *, expressionS *, const char *, const char *, va_list);
842static void load_register (int *, int, expressionS *, int);
843static void macro (struct mips_cl_insn * ip);
844static void mips16_macro (struct mips_cl_insn * ip);
252b5132 845#ifdef LOSING_COMPILER
17a2f251 846static void macro2 (struct mips_cl_insn * ip);
252b5132 847#endif
17a2f251
TS
848static void mips_ip (char *str, struct mips_cl_insn * ip);
849static void mips16_ip (char *str, struct mips_cl_insn * ip);
b34976b6 850static void mips16_immed
17a2f251
TS
851 (char *, unsigned int, int, offsetT, bfd_boolean, bfd_boolean, bfd_boolean,
852 unsigned long *, bfd_boolean *, unsigned short *);
5e0116d5 853static size_t my_getSmallExpression
17a2f251
TS
854 (expressionS *, bfd_reloc_code_real_type *, char *);
855static void my_getExpression (expressionS *, char *);
856static void s_align (int);
857static void s_change_sec (int);
858static void s_change_section (int);
859static void s_cons (int);
860static void s_float_cons (int);
861static void s_mips_globl (int);
862static void s_option (int);
863static void s_mipsset (int);
864static void s_abicalls (int);
865static void s_cpload (int);
866static void s_cpsetup (int);
867static void s_cplocal (int);
868static void s_cprestore (int);
869static void s_cpreturn (int);
870static void s_gpvalue (int);
871static void s_gpword (int);
872static void s_gpdword (int);
873static void s_cpadd (int);
874static void s_insn (int);
875static void md_obj_begin (void);
876static void md_obj_end (void);
877static void s_mips_ent (int);
878static void s_mips_end (int);
879static void s_mips_frame (int);
880static void s_mips_mask (int reg_type);
881static void s_mips_stab (int);
882static void s_mips_weakext (int);
883static void s_mips_file (int);
884static void s_mips_loc (int);
885static bfd_boolean pic_need_relax (symbolS *, asection *);
4a6a3df4 886static int relaxed_branch_length (fragS *, asection *, int);
17a2f251 887static int validate_mips_insn (const struct mips_opcode *);
e7af610e
NC
888
889/* Table and functions used to map between CPU/ISA names, and
890 ISA levels, and CPU numbers. */
891
e972090a
NC
892struct mips_cpu_info
893{
e7af610e
NC
894 const char *name; /* CPU or ISA name. */
895 int is_isa; /* Is this an ISA? (If 0, a CPU.) */
896 int isa; /* ISA level. */
897 int cpu; /* CPU number (default CPU if ISA). */
898};
899
17a2f251
TS
900static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
901static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
902static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
252b5132
RH
903\f
904/* Pseudo-op table.
905
906 The following pseudo-ops from the Kane and Heinrich MIPS book
907 should be defined here, but are currently unsupported: .alias,
908 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
909
910 The following pseudo-ops from the Kane and Heinrich MIPS book are
911 specific to the type of debugging information being generated, and
912 should be defined by the object format: .aent, .begin, .bend,
913 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
914 .vreg.
915
916 The following pseudo-ops from the Kane and Heinrich MIPS book are
917 not MIPS CPU specific, but are also not specific to the object file
918 format. This file is probably the best place to define them, but
919 they are not currently supported: .asm0, .endr, .lab, .repeat,
920 .struct. */
921
e972090a
NC
922static const pseudo_typeS mips_pseudo_table[] =
923{
beae10d5 924 /* MIPS specific pseudo-ops. */
252b5132
RH
925 {"option", s_option, 0},
926 {"set", s_mipsset, 0},
927 {"rdata", s_change_sec, 'r'},
928 {"sdata", s_change_sec, 's'},
929 {"livereg", s_ignore, 0},
930 {"abicalls", s_abicalls, 0},
931 {"cpload", s_cpload, 0},
6478892d
TS
932 {"cpsetup", s_cpsetup, 0},
933 {"cplocal", s_cplocal, 0},
252b5132 934 {"cprestore", s_cprestore, 0},
6478892d
TS
935 {"cpreturn", s_cpreturn, 0},
936 {"gpvalue", s_gpvalue, 0},
252b5132 937 {"gpword", s_gpword, 0},
10181a0d 938 {"gpdword", s_gpdword, 0},
252b5132
RH
939 {"cpadd", s_cpadd, 0},
940 {"insn", s_insn, 0},
941
beae10d5 942 /* Relatively generic pseudo-ops that happen to be used on MIPS
252b5132
RH
943 chips. */
944 {"asciiz", stringer, 1},
945 {"bss", s_change_sec, 'b'},
946 {"err", s_err, 0},
947 {"half", s_cons, 1},
948 {"dword", s_cons, 3},
949 {"weakext", s_mips_weakext, 0},
950
beae10d5 951 /* These pseudo-ops are defined in read.c, but must be overridden
252b5132
RH
952 here for one reason or another. */
953 {"align", s_align, 0},
954 {"byte", s_cons, 0},
955 {"data", s_change_sec, 'd'},
956 {"double", s_float_cons, 'd'},
957 {"float", s_float_cons, 'f'},
958 {"globl", s_mips_globl, 0},
959 {"global", s_mips_globl, 0},
960 {"hword", s_cons, 1},
961 {"int", s_cons, 2},
962 {"long", s_cons, 2},
963 {"octa", s_cons, 4},
964 {"quad", s_cons, 3},
cca86cc8 965 {"section", s_change_section, 0},
252b5132
RH
966 {"short", s_cons, 1},
967 {"single", s_float_cons, 'f'},
968 {"stabn", s_mips_stab, 'n'},
969 {"text", s_change_sec, 't'},
970 {"word", s_cons, 2},
add56521 971
add56521 972 { "extern", ecoff_directive_extern, 0},
add56521 973
43841e91 974 { NULL, NULL, 0 },
252b5132
RH
975};
976
e972090a
NC
977static const pseudo_typeS mips_nonecoff_pseudo_table[] =
978{
beae10d5
KH
979 /* These pseudo-ops should be defined by the object file format.
980 However, a.out doesn't support them, so we have versions here. */
252b5132
RH
981 {"aent", s_mips_ent, 1},
982 {"bgnb", s_ignore, 0},
983 {"end", s_mips_end, 0},
984 {"endb", s_ignore, 0},
985 {"ent", s_mips_ent, 0},
c5dd6aab 986 {"file", s_mips_file, 0},
252b5132
RH
987 {"fmask", s_mips_mask, 'F'},
988 {"frame", s_mips_frame, 0},
c5dd6aab 989 {"loc", s_mips_loc, 0},
252b5132
RH
990 {"mask", s_mips_mask, 'R'},
991 {"verstamp", s_ignore, 0},
43841e91 992 { NULL, NULL, 0 },
252b5132
RH
993};
994
17a2f251 995extern void pop_insert (const pseudo_typeS *);
252b5132
RH
996
997void
17a2f251 998mips_pop_insert (void)
252b5132
RH
999{
1000 pop_insert (mips_pseudo_table);
1001 if (! ECOFF_DEBUGGING)
1002 pop_insert (mips_nonecoff_pseudo_table);
1003}
1004\f
1005/* Symbols labelling the current insn. */
1006
e972090a
NC
1007struct insn_label_list
1008{
252b5132
RH
1009 struct insn_label_list *next;
1010 symbolS *label;
1011};
1012
1013static struct insn_label_list *insn_labels;
1014static struct insn_label_list *free_insn_labels;
1015
17a2f251 1016static void mips_clear_insn_labels (void);
252b5132
RH
1017
1018static inline void
17a2f251 1019mips_clear_insn_labels (void)
252b5132
RH
1020{
1021 register struct insn_label_list **pl;
1022
1023 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1024 ;
1025 *pl = insn_labels;
1026 insn_labels = NULL;
1027}
1028\f
1029static char *expr_end;
1030
1031/* Expressions which appear in instructions. These are set by
1032 mips_ip. */
1033
1034static expressionS imm_expr;
5f74bc13 1035static expressionS imm2_expr;
252b5132
RH
1036static expressionS offset_expr;
1037
1038/* Relocs associated with imm_expr and offset_expr. */
1039
f6688943
TS
1040static bfd_reloc_code_real_type imm_reloc[3]
1041 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1042static bfd_reloc_code_real_type offset_reloc[3]
1043 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 1044
252b5132
RH
1045/* These are set by mips16_ip if an explicit extension is used. */
1046
b34976b6 1047static bfd_boolean mips16_small, mips16_ext;
252b5132 1048
7ed4a06a 1049#ifdef OBJ_ELF
ecb4347a
DJ
1050/* The pdr segment for per procedure frame/regmask info. Not used for
1051 ECOFF debugging. */
252b5132
RH
1052
1053static segT pdr_seg;
7ed4a06a 1054#endif
252b5132 1055
e013f690
TS
1056/* The default target format to use. */
1057
1058const char *
17a2f251 1059mips_target_format (void)
e013f690
TS
1060{
1061 switch (OUTPUT_FLAVOR)
1062 {
1063 case bfd_target_aout_flavour:
1064 return target_big_endian ? "a.out-mips-big" : "a.out-mips-little";
1065 case bfd_target_ecoff_flavour:
1066 return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
1067 case bfd_target_coff_flavour:
1068 return "pe-mips";
1069 case bfd_target_elf_flavour:
1070#ifdef TE_TMIPS
cfe86eaa 1071 /* This is traditional mips. */
e013f690 1072 return (target_big_endian
cfe86eaa
TS
1073 ? (HAVE_64BIT_OBJECTS
1074 ? "elf64-tradbigmips"
1075 : (HAVE_NEWABI
1076 ? "elf32-ntradbigmips" : "elf32-tradbigmips"))
1077 : (HAVE_64BIT_OBJECTS
1078 ? "elf64-tradlittlemips"
1079 : (HAVE_NEWABI
1080 ? "elf32-ntradlittlemips" : "elf32-tradlittlemips")));
e013f690
TS
1081#else
1082 return (target_big_endian
cfe86eaa
TS
1083 ? (HAVE_64BIT_OBJECTS
1084 ? "elf64-bigmips"
1085 : (HAVE_NEWABI
1086 ? "elf32-nbigmips" : "elf32-bigmips"))
1087 : (HAVE_64BIT_OBJECTS
1088 ? "elf64-littlemips"
1089 : (HAVE_NEWABI
1090 ? "elf32-nlittlemips" : "elf32-littlemips")));
e013f690
TS
1091#endif
1092 default:
1093 abort ();
1094 return NULL;
1095 }
1096}
1097
156c2f8b
NC
1098/* This function is called once, at assembler startup time. It should
1099 set up all the tables, etc. that the MD part of the assembler will need. */
1100
252b5132 1101void
17a2f251 1102md_begin (void)
252b5132 1103{
252b5132 1104 register const char *retval = NULL;
156c2f8b 1105 int i = 0;
252b5132 1106 int broken = 0;
1f25f5d3 1107
fef14a42 1108 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_arch))
252b5132
RH
1109 as_warn (_("Could not set architecture and machine"));
1110
252b5132
RH
1111 op_hash = hash_new ();
1112
1113 for (i = 0; i < NUMOPCODES;)
1114 {
1115 const char *name = mips_opcodes[i].name;
1116
17a2f251 1117 retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
252b5132
RH
1118 if (retval != NULL)
1119 {
1120 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1121 mips_opcodes[i].name, retval);
1122 /* Probably a memory allocation problem? Give up now. */
1123 as_fatal (_("Broken assembler. No assembly attempted."));
1124 }
1125 do
1126 {
1127 if (mips_opcodes[i].pinfo != INSN_MACRO)
1128 {
1129 if (!validate_mips_insn (&mips_opcodes[i]))
1130 broken = 1;
1131 }
1132 ++i;
1133 }
1134 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1135 }
1136
1137 mips16_op_hash = hash_new ();
1138
1139 i = 0;
1140 while (i < bfd_mips16_num_opcodes)
1141 {
1142 const char *name = mips16_opcodes[i].name;
1143
17a2f251 1144 retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
252b5132
RH
1145 if (retval != NULL)
1146 as_fatal (_("internal: can't hash `%s': %s"),
1147 mips16_opcodes[i].name, retval);
1148 do
1149 {
1150 if (mips16_opcodes[i].pinfo != INSN_MACRO
1151 && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1152 != mips16_opcodes[i].match))
1153 {
1154 fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1155 mips16_opcodes[i].name, mips16_opcodes[i].args);
1156 broken = 1;
1157 }
1158 ++i;
1159 }
1160 while (i < bfd_mips16_num_opcodes
1161 && strcmp (mips16_opcodes[i].name, name) == 0);
1162 }
1163
1164 if (broken)
1165 as_fatal (_("Broken assembler. No assembly attempted."));
1166
1167 /* We add all the general register names to the symbol table. This
1168 helps us detect invalid uses of them. */
1169 for (i = 0; i < 32; i++)
1170 {
1171 char buf[5];
1172
1173 sprintf (buf, "$%d", i);
1174 symbol_table_insert (symbol_new (buf, reg_section, i,
1175 &zero_address_frag));
1176 }
76db943d
TS
1177 symbol_table_insert (symbol_new ("$ra", reg_section, RA,
1178 &zero_address_frag));
252b5132
RH
1179 symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1180 &zero_address_frag));
1181 symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1182 &zero_address_frag));
1183 symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1184 &zero_address_frag));
1185 symbol_table_insert (symbol_new ("$at", reg_section, AT,
1186 &zero_address_frag));
1187 symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1188 &zero_address_frag));
1189 symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1190 &zero_address_frag));
85b51719
TS
1191 symbol_table_insert (symbol_new ("$zero", reg_section, ZERO,
1192 &zero_address_frag));
252b5132
RH
1193 symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1194 &zero_address_frag));
1195
6047c971
AO
1196 /* If we don't add these register names to the symbol table, they
1197 may end up being added as regular symbols by operand(), and then
1198 make it to the object file as undefined in case they're not
1199 regarded as local symbols. They're local in o32, since `$' is a
1200 local symbol prefix, but not in n32 or n64. */
1201 for (i = 0; i < 8; i++)
1202 {
1203 char buf[6];
1204
1205 sprintf (buf, "$fcc%i", i);
1206 symbol_table_insert (symbol_new (buf, reg_section, -1,
1207 &zero_address_frag));
1208 }
1209
b34976b6 1210 mips_no_prev_insn (FALSE);
252b5132
RH
1211
1212 mips_gprmask = 0;
1213 mips_cprmask[0] = 0;
1214 mips_cprmask[1] = 0;
1215 mips_cprmask[2] = 0;
1216 mips_cprmask[3] = 0;
1217
1218 /* set the default alignment for the text section (2**2) */
1219 record_alignment (text_section, 2);
1220
1221 if (USE_GLOBAL_POINTER_OPT)
1222 bfd_set_gp_size (stdoutput, g_switch_value);
1223
1224 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1225 {
1226 /* On a native system, sections must be aligned to 16 byte
1227 boundaries. When configured for an embedded ELF target, we
1228 don't bother. */
1229 if (strcmp (TARGET_OS, "elf") != 0)
1230 {
1231 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1232 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1233 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1234 }
1235
1236 /* Create a .reginfo section for register masks and a .mdebug
1237 section for debugging information. */
1238 {
1239 segT seg;
1240 subsegT subseg;
1241 flagword flags;
1242 segT sec;
1243
1244 seg = now_seg;
1245 subseg = now_subseg;
1246
1247 /* The ABI says this section should be loaded so that the
1248 running program can access it. However, we don't load it
1249 if we are configured for an embedded target */
1250 flags = SEC_READONLY | SEC_DATA;
1251 if (strcmp (TARGET_OS, "elf") != 0)
1252 flags |= SEC_ALLOC | SEC_LOAD;
1253
316f5878 1254 if (mips_abi != N64_ABI)
252b5132
RH
1255 {
1256 sec = subseg_new (".reginfo", (subsegT) 0);
1257
195325d2
TS
1258 bfd_set_section_flags (stdoutput, sec, flags);
1259 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
bdaaa2e1 1260
252b5132
RH
1261#ifdef OBJ_ELF
1262 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1263#endif
1264 }
1265 else
1266 {
1267 /* The 64-bit ABI uses a .MIPS.options section rather than
1268 .reginfo section. */
1269 sec = subseg_new (".MIPS.options", (subsegT) 0);
195325d2
TS
1270 bfd_set_section_flags (stdoutput, sec, flags);
1271 bfd_set_section_alignment (stdoutput, sec, 3);
252b5132
RH
1272
1273#ifdef OBJ_ELF
1274 /* Set up the option header. */
1275 {
1276 Elf_Internal_Options opthdr;
1277 char *f;
1278
1279 opthdr.kind = ODK_REGINFO;
1280 opthdr.size = (sizeof (Elf_External_Options)
1281 + sizeof (Elf64_External_RegInfo));
1282 opthdr.section = 0;
1283 opthdr.info = 0;
1284 f = frag_more (sizeof (Elf_External_Options));
1285 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1286 (Elf_External_Options *) f);
1287
1288 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1289 }
1290#endif
1291 }
1292
1293 if (ECOFF_DEBUGGING)
1294 {
1295 sec = subseg_new (".mdebug", (subsegT) 0);
1296 (void) bfd_set_section_flags (stdoutput, sec,
1297 SEC_HAS_CONTENTS | SEC_READONLY);
1298 (void) bfd_set_section_alignment (stdoutput, sec, 2);
1299 }
ecb4347a 1300#ifdef OBJ_ELF
dcd410fe 1301 else if (OUTPUT_FLAVOR == bfd_target_elf_flavour && mips_flag_pdr)
ecb4347a
DJ
1302 {
1303 pdr_seg = subseg_new (".pdr", (subsegT) 0);
1304 (void) bfd_set_section_flags (stdoutput, pdr_seg,
1305 SEC_READONLY | SEC_RELOC
1306 | SEC_DEBUGGING);
1307 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1308 }
252b5132
RH
1309#endif
1310
1311 subseg_set (seg, subseg);
1312 }
1313 }
1314
1315 if (! ECOFF_DEBUGGING)
1316 md_obj_begin ();
1317}
1318
1319void
17a2f251 1320md_mips_end (void)
252b5132
RH
1321{
1322 if (! ECOFF_DEBUGGING)
1323 md_obj_end ();
1324}
1325
1326void
17a2f251 1327md_assemble (char *str)
252b5132
RH
1328{
1329 struct mips_cl_insn insn;
f6688943
TS
1330 bfd_reloc_code_real_type unused_reloc[3]
1331 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132
RH
1332
1333 imm_expr.X_op = O_absent;
5f74bc13 1334 imm2_expr.X_op = O_absent;
252b5132 1335 offset_expr.X_op = O_absent;
f6688943
TS
1336 imm_reloc[0] = BFD_RELOC_UNUSED;
1337 imm_reloc[1] = BFD_RELOC_UNUSED;
1338 imm_reloc[2] = BFD_RELOC_UNUSED;
1339 offset_reloc[0] = BFD_RELOC_UNUSED;
1340 offset_reloc[1] = BFD_RELOC_UNUSED;
1341 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
1342
1343 if (mips_opts.mips16)
1344 mips16_ip (str, &insn);
1345 else
1346 {
1347 mips_ip (str, &insn);
beae10d5
KH
1348 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1349 str, insn.insn_opcode));
252b5132
RH
1350 }
1351
1352 if (insn_error)
1353 {
1354 as_bad ("%s `%s'", insn_error, str);
1355 return;
1356 }
1357
1358 if (insn.insn_mo->pinfo == INSN_MACRO)
1359 {
1360 if (mips_opts.mips16)
1361 mips16_macro (&insn);
1362 else
1363 macro (&insn);
1364 }
1365 else
1366 {
1367 if (imm_expr.X_op != O_absent)
5e0116d5 1368 append_insn (NULL, &insn, &imm_expr, imm_reloc);
252b5132 1369 else if (offset_expr.X_op != O_absent)
5e0116d5 1370 append_insn (NULL, &insn, &offset_expr, offset_reloc);
252b5132 1371 else
5e0116d5 1372 append_insn (NULL, &insn, NULL, unused_reloc);
252b5132
RH
1373 }
1374}
1375
5919d012
RS
1376/* Return true if the given relocation might need a matching %lo().
1377 Note that R_MIPS_GOT16 relocations only need a matching %lo() when
1378 applied to local symbols. */
1379
1380static inline bfd_boolean
17a2f251 1381reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
5919d012
RS
1382{
1383 return (reloc == BFD_RELOC_HI16_S
1384 || reloc == BFD_RELOC_MIPS_GOT16);
1385}
1386
1387/* Return true if the given fixup is followed by a matching R_MIPS_LO16
1388 relocation. */
1389
1390static inline bfd_boolean
17a2f251 1391fixup_has_matching_lo_p (fixS *fixp)
5919d012
RS
1392{
1393 return (fixp->fx_next != NULL
1394 && fixp->fx_next->fx_r_type == BFD_RELOC_LO16
1395 && fixp->fx_addsy == fixp->fx_next->fx_addsy
1396 && fixp->fx_offset == fixp->fx_next->fx_offset);
1397}
1398
252b5132
RH
1399/* See whether instruction IP reads register REG. CLASS is the type
1400 of register. */
1401
1402static int
17a2f251
TS
1403insn_uses_reg (struct mips_cl_insn *ip, unsigned int reg,
1404 enum mips_regclass class)
252b5132
RH
1405{
1406 if (class == MIPS16_REG)
1407 {
1408 assert (mips_opts.mips16);
1409 reg = mips16_to_32_reg_map[reg];
1410 class = MIPS_GR_REG;
1411 }
1412
85b51719
TS
1413 /* Don't report on general register ZERO, since it never changes. */
1414 if (class == MIPS_GR_REG && reg == ZERO)
252b5132
RH
1415 return 0;
1416
1417 if (class == MIPS_FP_REG)
1418 {
1419 assert (! mips_opts.mips16);
1420 /* If we are called with either $f0 or $f1, we must check $f0.
1421 This is not optimal, because it will introduce an unnecessary
1422 NOP between "lwc1 $f0" and "swc1 $f1". To fix this we would
1423 need to distinguish reading both $f0 and $f1 or just one of
1424 them. Note that we don't have to check the other way,
1425 because there is no instruction that sets both $f0 and $f1
1426 and requires a delay. */
1427 if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1428 && ((((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS) &~(unsigned)1)
1429 == (reg &~ (unsigned) 1)))
1430 return 1;
1431 if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1432 && ((((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT) &~(unsigned)1)
1433 == (reg &~ (unsigned) 1)))
1434 return 1;
1435 }
1436 else if (! mips_opts.mips16)
1437 {
1438 if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1439 && ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == reg)
1440 return 1;
1441 if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1442 && ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT) == reg)
1443 return 1;
1444 }
1445 else
1446 {
1447 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1448 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RX)
1449 & MIPS16OP_MASK_RX)]
1450 == reg))
1451 return 1;
1452 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1453 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RY)
1454 & MIPS16OP_MASK_RY)]
1455 == reg))
1456 return 1;
1457 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1458 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
1459 & MIPS16OP_MASK_MOVE32Z)]
1460 == reg))
1461 return 1;
1462 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1463 return 1;
1464 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1465 return 1;
1466 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1467 return 1;
1468 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1469 && ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
1470 & MIPS16OP_MASK_REGR32) == reg)
1471 return 1;
1472 }
1473
1474 return 0;
1475}
1476
1477/* This function returns true if modifying a register requires a
1478 delay. */
1479
1480static int
17a2f251 1481reg_needs_delay (unsigned int reg)
252b5132
RH
1482{
1483 unsigned long prev_pinfo;
1484
1485 prev_pinfo = prev_insn.insn_mo->pinfo;
1486 if (! mips_opts.noreorder
81912461
ILT
1487 && (((prev_pinfo & INSN_LOAD_MEMORY_DELAY)
1488 && ! gpr_interlocks)
1489 || ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1490 && ! cop_interlocks)))
252b5132 1491 {
81912461
ILT
1492 /* A load from a coprocessor or from memory. All load delays
1493 delay the use of general register rt for one instruction. */
bdaaa2e1 1494 /* Itbl support may require additional care here. */
252b5132
RH
1495 know (prev_pinfo & INSN_WRITE_GPR_T);
1496 if (reg == ((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT))
1497 return 1;
1498 }
1499
1500 return 0;
1501}
1502
1503/* Mark instruction labels in mips16 mode. This permits the linker to
1504 handle them specially, such as generating jalx instructions when
1505 needed. We also make them odd for the duration of the assembly, in
1506 order to generate the right sort of code. We will make them even
1507 in the adjust_symtab routine, while leaving them marked. This is
1508 convenient for the debugger and the disassembler. The linker knows
1509 to make them odd again. */
1510
1511static void
17a2f251 1512mips16_mark_labels (void)
252b5132
RH
1513{
1514 if (mips_opts.mips16)
1515 {
1516 struct insn_label_list *l;
98aa84af 1517 valueT val;
252b5132
RH
1518
1519 for (l = insn_labels; l != NULL; l = l->next)
1520 {
1521#ifdef OBJ_ELF
1522 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1523 S_SET_OTHER (l->label, STO_MIPS16);
1524#endif
98aa84af
AM
1525 val = S_GET_VALUE (l->label);
1526 if ((val & 1) == 0)
1527 S_SET_VALUE (l->label, val + 1);
252b5132
RH
1528 }
1529 }
1530}
1531
1532/* Output an instruction. PLACE is where to put the instruction; if
1533 it is NULL, this uses frag_more to get room. IP is the instruction
1534 information. ADDRESS_EXPR is an operand of the instruction to be
1535 used with RELOC_TYPE. */
1536
1537static void
17a2f251
TS
1538append_insn (char *place, struct mips_cl_insn *ip, expressionS *address_expr,
1539 bfd_reloc_code_real_type *reloc_type)
252b5132
RH
1540{
1541 register unsigned long prev_pinfo, pinfo;
1542 char *f;
f6688943 1543 fixS *fixp[3];
252b5132 1544 int nops = 0;
f5040a92 1545 bfd_boolean force_new_frag = FALSE;
252b5132
RH
1546
1547 /* Mark instruction labels in mips16 mode. */
f9419b05 1548 mips16_mark_labels ();
252b5132
RH
1549
1550 prev_pinfo = prev_insn.insn_mo->pinfo;
1551 pinfo = ip->insn_mo->pinfo;
1552
1553 if (place == NULL && (! mips_opts.noreorder || prev_nop_frag != NULL))
1554 {
1555 int prev_prev_nop;
1556
1557 /* If the previous insn required any delay slots, see if we need
1558 to insert a NOP or two. There are eight kinds of possible
1559 hazards, of which an instruction can have at most one type.
1560 (1) a load from memory delay
1561 (2) a load from a coprocessor delay
1562 (3) an unconditional branch delay
1563 (4) a conditional branch delay
1564 (5) a move to coprocessor register delay
1565 (6) a load coprocessor register from memory delay
1566 (7) a coprocessor condition code delay
1567 (8) a HI/LO special register delay
1568
1569 There are a lot of optimizations we could do that we don't.
1570 In particular, we do not, in general, reorder instructions.
1571 If you use gcc with optimization, it will reorder
1572 instructions and generally do much more optimization then we
1573 do here; repeating all that work in the assembler would only
1574 benefit hand written assembly code, and does not seem worth
1575 it. */
1576
1577 /* This is how a NOP is emitted. */
1578#define emit_nop() \
1579 (mips_opts.mips16 \
1580 ? md_number_to_chars (frag_more (2), 0x6500, 2) \
1581 : md_number_to_chars (frag_more (4), 0, 4))
1582
1583 /* The previous insn might require a delay slot, depending upon
1584 the contents of the current insn. */
1585 if (! mips_opts.mips16
81912461
ILT
1586 && (((prev_pinfo & INSN_LOAD_MEMORY_DELAY)
1587 && ! gpr_interlocks)
1588 || ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1589 && ! cop_interlocks)))
252b5132
RH
1590 {
1591 /* A load from a coprocessor or from memory. All load
1592 delays delay the use of general register rt for one
81912461 1593 instruction. */
beae10d5 1594 /* Itbl support may require additional care here. */
252b5132
RH
1595 know (prev_pinfo & INSN_WRITE_GPR_T);
1596 if (mips_optimize == 0
1597 || insn_uses_reg (ip,
1598 ((prev_insn.insn_opcode >> OP_SH_RT)
1599 & OP_MASK_RT),
1600 MIPS_GR_REG))
1601 ++nops;
1602 }
1603 else if (! mips_opts.mips16
252b5132 1604 && (((prev_pinfo & INSN_COPROC_MOVE_DELAY)
beae10d5 1605 && ! cop_interlocks)
81912461
ILT
1606 || ((prev_pinfo & INSN_COPROC_MEMORY_DELAY)
1607 && ! cop_mem_interlocks)))
252b5132
RH
1608 {
1609 /* A generic coprocessor delay. The previous instruction
1610 modified a coprocessor general or control register. If
1611 it modified a control register, we need to avoid any
1612 coprocessor instruction (this is probably not always
1613 required, but it sometimes is). If it modified a general
1614 register, we avoid using that register.
1615
252b5132
RH
1616 This case is not handled very well. There is no special
1617 knowledge of CP0 handling, and the coprocessors other
1618 than the floating point unit are not distinguished at
1619 all. */
1620 /* Itbl support may require additional care here. FIXME!
bdaaa2e1 1621 Need to modify this to include knowledge about
252b5132
RH
1622 user specified delays! */
1623 if (prev_pinfo & INSN_WRITE_FPR_T)
1624 {
1625 if (mips_optimize == 0
1626 || insn_uses_reg (ip,
1627 ((prev_insn.insn_opcode >> OP_SH_FT)
1628 & OP_MASK_FT),
1629 MIPS_FP_REG))
1630 ++nops;
1631 }
1632 else if (prev_pinfo & INSN_WRITE_FPR_S)
1633 {
1634 if (mips_optimize == 0
1635 || insn_uses_reg (ip,
1636 ((prev_insn.insn_opcode >> OP_SH_FS)
1637 & OP_MASK_FS),
1638 MIPS_FP_REG))
1639 ++nops;
1640 }
1641 else
1642 {
1643 /* We don't know exactly what the previous instruction
1644 does. If the current instruction uses a coprocessor
1645 register, we must insert a NOP. If previous
1646 instruction may set the condition codes, and the
1647 current instruction uses them, we must insert two
1648 NOPS. */
bdaaa2e1 1649 /* Itbl support may require additional care here. */
252b5132
RH
1650 if (mips_optimize == 0
1651 || ((prev_pinfo & INSN_WRITE_COND_CODE)
1652 && (pinfo & INSN_READ_COND_CODE)))
1653 nops += 2;
1654 else if (pinfo & INSN_COP)
1655 ++nops;
1656 }
1657 }
1658 else if (! mips_opts.mips16
252b5132
RH
1659 && (prev_pinfo & INSN_WRITE_COND_CODE)
1660 && ! cop_interlocks)
1661 {
1662 /* The previous instruction sets the coprocessor condition
1663 codes, but does not require a general coprocessor delay
1664 (this means it is a floating point comparison
1665 instruction). If this instruction uses the condition
1666 codes, we need to insert a single NOP. */
beae10d5 1667 /* Itbl support may require additional care here. */
252b5132
RH
1668 if (mips_optimize == 0
1669 || (pinfo & INSN_READ_COND_CODE))
1670 ++nops;
1671 }
6b76fefe
CM
1672
1673 /* If we're fixing up mfhi/mflo for the r7000 and the
1674 previous insn was an mfhi/mflo and the current insn
1675 reads the register that the mfhi/mflo wrote to, then
1676 insert two nops. */
1677
1678 else if (mips_7000_hilo_fix
1679 && MF_HILO_INSN (prev_pinfo)
1680 && insn_uses_reg (ip, ((prev_insn.insn_opcode >> OP_SH_RD)
beae10d5
KH
1681 & OP_MASK_RD),
1682 MIPS_GR_REG))
6b76fefe
CM
1683 {
1684 nops += 2;
1685 }
1686
1687 /* If we're fixing up mfhi/mflo for the r7000 and the
1688 2nd previous insn was an mfhi/mflo and the current insn
1689 reads the register that the mfhi/mflo wrote to, then
1690 insert one nop. */
1691
1692 else if (mips_7000_hilo_fix
1693 && MF_HILO_INSN (prev_prev_insn.insn_opcode)
1694 && insn_uses_reg (ip, ((prev_prev_insn.insn_opcode >> OP_SH_RD)
1695 & OP_MASK_RD),
1696 MIPS_GR_REG))
bdaaa2e1 1697
6b76fefe 1698 {
f9419b05 1699 ++nops;
6b76fefe 1700 }
bdaaa2e1 1701
252b5132
RH
1702 else if (prev_pinfo & INSN_READ_LO)
1703 {
1704 /* The previous instruction reads the LO register; if the
1705 current instruction writes to the LO register, we must
bdaaa2e1 1706 insert two NOPS. Some newer processors have interlocks.
67c1ffbe
KH
1707 Also the tx39's multiply instructions can be executed
1708 immediately after a read from HI/LO (without the delay),
bdaaa2e1
KH
1709 though the tx39's divide insns still do require the
1710 delay. */
252b5132 1711 if (! (hilo_interlocks
ec68c924 1712 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
252b5132
RH
1713 && (mips_optimize == 0
1714 || (pinfo & INSN_WRITE_LO)))
1715 nops += 2;
1716 /* Most mips16 branch insns don't have a delay slot.
1717 If a read from LO is immediately followed by a branch
1718 to a write to LO we have a read followed by a write
1719 less than 2 insns away. We assume the target of
1720 a branch might be a write to LO, and insert a nop
bdaaa2e1 1721 between a read and an immediately following branch. */
252b5132
RH
1722 else if (mips_opts.mips16
1723 && (mips_optimize == 0
1724 || (pinfo & MIPS16_INSN_BRANCH)))
f9419b05 1725 ++nops;
252b5132
RH
1726 }
1727 else if (prev_insn.insn_mo->pinfo & INSN_READ_HI)
1728 {
1729 /* The previous instruction reads the HI register; if the
1730 current instruction writes to the HI register, we must
1731 insert a NOP. Some newer processors have interlocks.
bdaaa2e1 1732 Also the note tx39's multiply above. */
252b5132 1733 if (! (hilo_interlocks
ec68c924 1734 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
252b5132
RH
1735 && (mips_optimize == 0
1736 || (pinfo & INSN_WRITE_HI)))
1737 nops += 2;
1738 /* Most mips16 branch insns don't have a delay slot.
1739 If a read from HI is immediately followed by a branch
1740 to a write to HI we have a read followed by a write
1741 less than 2 insns away. We assume the target of
1742 a branch might be a write to HI, and insert a nop
bdaaa2e1 1743 between a read and an immediately following branch. */
252b5132
RH
1744 else if (mips_opts.mips16
1745 && (mips_optimize == 0
1746 || (pinfo & MIPS16_INSN_BRANCH)))
f9419b05 1747 ++nops;
252b5132
RH
1748 }
1749
1750 /* If the previous instruction was in a noreorder section, then
1751 we don't want to insert the nop after all. */
bdaaa2e1 1752 /* Itbl support may require additional care here. */
252b5132
RH
1753 if (prev_insn_unreordered)
1754 nops = 0;
1755
1756 /* There are two cases which require two intervening
1757 instructions: 1) setting the condition codes using a move to
1758 coprocessor instruction which requires a general coprocessor
1759 delay and then reading the condition codes 2) reading the HI
1760 or LO register and then writing to it (except on processors
1761 which have interlocks). If we are not already emitting a NOP
1762 instruction, we must check for these cases compared to the
1763 instruction previous to the previous instruction. */
1764 if ((! mips_opts.mips16
252b5132
RH
1765 && (prev_prev_insn.insn_mo->pinfo & INSN_COPROC_MOVE_DELAY)
1766 && (prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
1767 && (pinfo & INSN_READ_COND_CODE)
1768 && ! cop_interlocks)
1769 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_LO)
1770 && (pinfo & INSN_WRITE_LO)
1771 && ! (hilo_interlocks
ec68c924 1772 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT))))
252b5132
RH
1773 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
1774 && (pinfo & INSN_WRITE_HI)
1775 && ! (hilo_interlocks
ec68c924 1776 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))))
252b5132
RH
1777 prev_prev_nop = 1;
1778 else
1779 prev_prev_nop = 0;
1780
1781 if (prev_prev_insn_unreordered)
1782 prev_prev_nop = 0;
1783
1784 if (prev_prev_nop && nops == 0)
1785 ++nops;
1786
60b63b72
RS
1787 if (mips_fix_4122_bugs && prev_insn.insn_mo->name)
1788 {
1789 /* We're out of bits in pinfo, so we must resort to string
1790 ops here. Shortcuts are selected based on opcodes being
1791 limited to the VR4122 instruction set. */
1792 int min_nops = 0;
1793 const char *pn = prev_insn.insn_mo->name;
1794 const char *tn = ip->insn_mo->name;
1795 if (strncmp(pn, "macc", 4) == 0
1796 || strncmp(pn, "dmacc", 5) == 0)
1797 {
1798 /* Errata 21 - [D]DIV[U] after [D]MACC */
1799 if (strstr (tn, "div"))
1800 {
1801 min_nops = 1;
1802 }
1803
1804 /* Errata 23 - Continuous DMULT[U]/DMACC instructions */
1805 if (pn[0] == 'd' /* dmacc */
1806 && (strncmp(tn, "dmult", 5) == 0
1807 || strncmp(tn, "dmacc", 5) == 0))
1808 {
1809 min_nops = 1;
1810 }
1811
1812 /* Errata 24 - MT{LO,HI} after [D]MACC */
1813 if (strcmp (tn, "mtlo") == 0
1814 || strcmp (tn, "mthi") == 0)
1815 {
1816 min_nops = 1;
1817 }
1818
1819 }
1820 else if (strncmp(pn, "dmult", 5) == 0
1821 && (strncmp(tn, "dmult", 5) == 0
1822 || strncmp(tn, "dmacc", 5) == 0))
1823 {
1824 /* Here is the rest of errata 23. */
1825 min_nops = 1;
1826 }
1827 if (nops < min_nops)
1828 nops = min_nops;
1829 }
1830
252b5132
RH
1831 /* If we are being given a nop instruction, don't bother with
1832 one of the nops we would otherwise output. This will only
1833 happen when a nop instruction is used with mips_optimize set
1834 to 0. */
1835 if (nops > 0
1836 && ! mips_opts.noreorder
156c2f8b 1837 && ip->insn_opcode == (unsigned) (mips_opts.mips16 ? 0x6500 : 0))
252b5132
RH
1838 --nops;
1839
1840 /* Now emit the right number of NOP instructions. */
1841 if (nops > 0 && ! mips_opts.noreorder)
1842 {
1843 fragS *old_frag;
1844 unsigned long old_frag_offset;
1845 int i;
1846 struct insn_label_list *l;
1847
1848 old_frag = frag_now;
1849 old_frag_offset = frag_now_fix ();
1850
1851 for (i = 0; i < nops; i++)
1852 emit_nop ();
1853
1854 if (listing)
1855 {
1856 listing_prev_line ();
1857 /* We may be at the start of a variant frag. In case we
1858 are, make sure there is enough space for the frag
1859 after the frags created by listing_prev_line. The
1860 argument to frag_grow here must be at least as large
1861 as the argument to all other calls to frag_grow in
1862 this file. We don't have to worry about being in the
1863 middle of a variant frag, because the variants insert
1864 all needed nop instructions themselves. */
1865 frag_grow (40);
1866 }
1867
1868 for (l = insn_labels; l != NULL; l = l->next)
1869 {
98aa84af
AM
1870 valueT val;
1871
252b5132 1872 assert (S_GET_SEGMENT (l->label) == now_seg);
49309057 1873 symbol_set_frag (l->label, frag_now);
98aa84af 1874 val = (valueT) frag_now_fix ();
252b5132
RH
1875 /* mips16 text labels are stored as odd. */
1876 if (mips_opts.mips16)
f9419b05 1877 ++val;
98aa84af 1878 S_SET_VALUE (l->label, val);
252b5132
RH
1879 }
1880
1881#ifndef NO_ECOFF_DEBUGGING
1882 if (ECOFF_DEBUGGING)
1883 ecoff_fix_loc (old_frag, old_frag_offset);
1884#endif
1885 }
1886 else if (prev_nop_frag != NULL)
1887 {
1888 /* We have a frag holding nops we may be able to remove. If
1889 we don't need any nops, we can decrease the size of
1890 prev_nop_frag by the size of one instruction. If we do
bdaaa2e1 1891 need some nops, we count them in prev_nops_required. */
252b5132
RH
1892 if (prev_nop_frag_since == 0)
1893 {
1894 if (nops == 0)
1895 {
1896 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1897 --prev_nop_frag_holds;
1898 }
1899 else
1900 prev_nop_frag_required += nops;
1901 }
1902 else
1903 {
1904 if (prev_prev_nop == 0)
1905 {
1906 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1907 --prev_nop_frag_holds;
1908 }
1909 else
1910 ++prev_nop_frag_required;
1911 }
1912
1913 if (prev_nop_frag_holds <= prev_nop_frag_required)
1914 prev_nop_frag = NULL;
1915
1916 ++prev_nop_frag_since;
1917
1918 /* Sanity check: by the time we reach the second instruction
1919 after prev_nop_frag, we should have used up all the nops
1920 one way or another. */
1921 assert (prev_nop_frag_since <= 1 || prev_nop_frag == NULL);
1922 }
1923 }
1924
4a6a3df4
AO
1925 if (place == NULL
1926 && address_expr
0b25d3e6 1927 && *reloc_type == BFD_RELOC_16_PCREL_S2
4a6a3df4
AO
1928 && (pinfo & INSN_UNCOND_BRANCH_DELAY || pinfo & INSN_COND_BRANCH_DELAY
1929 || pinfo & INSN_COND_BRANCH_LIKELY)
1930 && mips_relax_branch
1931 /* Don't try branch relaxation within .set nomacro, or within
1932 .set noat if we use $at for PIC computations. If it turns
1933 out that the branch was out-of-range, we'll get an error. */
1934 && !mips_opts.warn_about_macros
1935 && !(mips_opts.noat && mips_pic != NO_PIC)
1936 && !mips_opts.mips16)
1937 {
1938 f = frag_var (rs_machine_dependent,
1939 relaxed_branch_length
1940 (NULL, NULL,
1941 (pinfo & INSN_UNCOND_BRANCH_DELAY) ? -1
1942 : (pinfo & INSN_COND_BRANCH_LIKELY) ? 1 : 0), 4,
1943 RELAX_BRANCH_ENCODE
af6ae2ad 1944 (pinfo & INSN_UNCOND_BRANCH_DELAY,
4a6a3df4
AO
1945 pinfo & INSN_COND_BRANCH_LIKELY,
1946 pinfo & INSN_WRITE_GPR_31,
1947 0),
1948 address_expr->X_add_symbol,
1949 address_expr->X_add_number,
1950 0);
1951 *reloc_type = BFD_RELOC_UNUSED;
1952 }
1953 else if (*reloc_type > BFD_RELOC_UNUSED)
252b5132
RH
1954 {
1955 /* We need to set up a variant frag. */
1956 assert (mips_opts.mips16 && address_expr != NULL);
1957 f = frag_var (rs_machine_dependent, 4, 0,
f6688943 1958 RELAX_MIPS16_ENCODE (*reloc_type - BFD_RELOC_UNUSED,
252b5132
RH
1959 mips16_small, mips16_ext,
1960 (prev_pinfo
1961 & INSN_UNCOND_BRANCH_DELAY),
f6688943 1962 (*prev_insn_reloc_type
252b5132 1963 == BFD_RELOC_MIPS16_JMP)),
c4e7957c 1964 make_expr_symbol (address_expr), 0, NULL);
252b5132
RH
1965 }
1966 else if (place != NULL)
1967 f = place;
1968 else if (mips_opts.mips16
1969 && ! ip->use_extend
f6688943 1970 && *reloc_type != BFD_RELOC_MIPS16_JMP)
252b5132
RH
1971 {
1972 /* Make sure there is enough room to swap this instruction with
1973 a following jump instruction. */
1974 frag_grow (6);
1975 f = frag_more (2);
1976 }
1977 else
1978 {
1979 if (mips_opts.mips16
1980 && mips_opts.noreorder
1981 && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
1982 as_warn (_("extended instruction in delay slot"));
1983
1984 f = frag_more (4);
1985 }
1986
f6688943
TS
1987 fixp[0] = fixp[1] = fixp[2] = NULL;
1988 if (address_expr != NULL && *reloc_type < BFD_RELOC_UNUSED)
252b5132
RH
1989 {
1990 if (address_expr->X_op == O_constant)
1991 {
4db1a35d 1992 valueT tmp;
f6688943
TS
1993
1994 switch (*reloc_type)
252b5132
RH
1995 {
1996 case BFD_RELOC_32:
1997 ip->insn_opcode |= address_expr->X_add_number;
1998 break;
1999
f6688943 2000 case BFD_RELOC_MIPS_HIGHEST:
6344efa4
AM
2001 tmp = (address_expr->X_add_number
2002 + ((valueT) 0x8000 << 32) + 0x80008000) >> 16;
f6688943
TS
2003 tmp >>= 16;
2004 ip->insn_opcode |= (tmp >> 16) & 0xffff;
2005 break;
2006
2007 case BFD_RELOC_MIPS_HIGHER:
2008 tmp = (address_expr->X_add_number + 0x80008000) >> 16;
2009 ip->insn_opcode |= (tmp >> 16) & 0xffff;
2010 break;
2011
2012 case BFD_RELOC_HI16_S:
2013 ip->insn_opcode |= ((address_expr->X_add_number + 0x8000)
2014 >> 16) & 0xffff;
2015 break;
2016
2017 case BFD_RELOC_HI16:
2018 ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
2019 break;
2020
252b5132 2021 case BFD_RELOC_LO16:
ed6fb7bd 2022 case BFD_RELOC_MIPS_GOT_DISP:
252b5132
RH
2023 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2024 break;
2025
2026 case BFD_RELOC_MIPS_JMP:
2027 if ((address_expr->X_add_number & 3) != 0)
2028 as_bad (_("jump to misaligned address (0x%lx)"),
2029 (unsigned long) address_expr->X_add_number);
f3c0ec86 2030 if (address_expr->X_add_number & ~0xfffffff)
7496292d
TS
2031 as_bad (_("jump address range overflow (0x%lx)"),
2032 (unsigned long) address_expr->X_add_number);
252b5132
RH
2033 ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
2034 break;
2035
2036 case BFD_RELOC_MIPS16_JMP:
2037 if ((address_expr->X_add_number & 3) != 0)
2038 as_bad (_("jump to misaligned address (0x%lx)"),
2039 (unsigned long) address_expr->X_add_number);
f3c0ec86 2040 if (address_expr->X_add_number & ~0xfffffff)
7496292d
TS
2041 as_bad (_("jump address range overflow (0x%lx)"),
2042 (unsigned long) address_expr->X_add_number);
252b5132
RH
2043 ip->insn_opcode |=
2044 (((address_expr->X_add_number & 0x7c0000) << 3)
2045 | ((address_expr->X_add_number & 0xf800000) >> 7)
2046 | ((address_expr->X_add_number & 0x3fffc) >> 2));
2047 break;
2048
252b5132
RH
2049 case BFD_RELOC_16_PCREL_S2:
2050 goto need_reloc;
2051
2052 default:
2053 internalError ();
2054 }
2055 }
2056 else
2057 {
2058 need_reloc:
f6688943 2059 /* Don't generate a reloc if we are writing into a variant frag. */
252b5132
RH
2060 if (place == NULL)
2061 {
34ce925e
RS
2062 reloc_howto_type *howto;
2063 int i;
2064
2065 /* In a compound relocation, it is the final (outermost)
2066 operator that determines the relocated field. */
2067 for (i = 1; i < 3; i++)
2068 if (reloc_type[i] == BFD_RELOC_UNUSED)
2069 break;
2070
2071 howto = bfd_reloc_type_lookup (stdoutput, reloc_type[i - 1]);
a105a300
TS
2072 fixp[0] = fix_new_exp (frag_now, f - frag_now->fr_literal,
2073 bfd_get_reloc_size(howto),
f6688943 2074 address_expr,
a105a300 2075 reloc_type[0] == BFD_RELOC_16_PCREL_S2,
f6688943
TS
2076 reloc_type[0]);
2077
b6ff326e 2078 /* These relocations can have an addend that won't fit in
f6688943 2079 4 octets for 64bit assembly. */
a105a300
TS
2080 if (HAVE_64BIT_GPRS
2081 && ! howto->partial_inplace
2082 && (reloc_type[0] == BFD_RELOC_16
2083 || reloc_type[0] == BFD_RELOC_32
2084 || reloc_type[0] == BFD_RELOC_MIPS_JMP
2085 || reloc_type[0] == BFD_RELOC_HI16_S
2086 || reloc_type[0] == BFD_RELOC_LO16
2087 || reloc_type[0] == BFD_RELOC_GPREL16
2088 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
2089 || reloc_type[0] == BFD_RELOC_GPREL32
2090 || reloc_type[0] == BFD_RELOC_64
2091 || reloc_type[0] == BFD_RELOC_CTOR
2092 || reloc_type[0] == BFD_RELOC_MIPS_SUB
2093 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
2094 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
2095 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
2096 || reloc_type[0] == BFD_RELOC_MIPS_REL16
2097 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT))
f6688943
TS
2098 fixp[0]->fx_no_overflow = 1;
2099
5919d012 2100 if (reloc_needs_lo_p (*reloc_type))
252b5132
RH
2101 {
2102 struct mips_hi_fixup *hi_fixup;
2103
5919d012
RS
2104 /* Reuse the last entry if it already has a matching %lo. */
2105 hi_fixup = mips_hi_fixup_list;
2106 if (hi_fixup == 0
2107 || !fixup_has_matching_lo_p (hi_fixup->fixp))
2108 {
2109 hi_fixup = ((struct mips_hi_fixup *)
2110 xmalloc (sizeof (struct mips_hi_fixup)));
2111 hi_fixup->next = mips_hi_fixup_list;
2112 mips_hi_fixup_list = hi_fixup;
2113 }
f6688943 2114 hi_fixup->fixp = fixp[0];
252b5132 2115 hi_fixup->seg = now_seg;
252b5132 2116 }
f6688943 2117
34ce925e
RS
2118 /* Add fixups for the second and third relocations, if given.
2119 Note that the ABI allows the second relocation to be
2120 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the
2121 moment we only use RSS_UNDEF, but we could add support
2122 for the others if it ever becomes necessary. */
2123 for (i = 1; i < 3; i++)
2124 if (reloc_type[i] != BFD_RELOC_UNUSED)
2125 {
2126 address_expr->X_op = O_absent;
2127 address_expr->X_add_symbol = 0;
2128 address_expr->X_add_number = 0;
2129
2130 fixp[i] = fix_new_exp (frag_now, fixp[0]->fx_where,
2131 fixp[0]->fx_size, address_expr,
2132 FALSE, reloc_type[i]);
2133 }
252b5132
RH
2134 }
2135 }
2136 }
2137
2138 if (! mips_opts.mips16)
c5dd6aab
DJ
2139 {
2140 md_number_to_chars (f, ip->insn_opcode, 4);
2141#ifdef OBJ_ELF
2142 dwarf2_emit_insn (4);
2143#endif
2144 }
f6688943 2145 else if (*reloc_type == BFD_RELOC_MIPS16_JMP)
252b5132
RH
2146 {
2147 md_number_to_chars (f, ip->insn_opcode >> 16, 2);
2148 md_number_to_chars (f + 2, ip->insn_opcode & 0xffff, 2);
c5dd6aab
DJ
2149#ifdef OBJ_ELF
2150 dwarf2_emit_insn (4);
2151#endif
252b5132
RH
2152 }
2153 else
2154 {
2155 if (ip->use_extend)
2156 {
2157 md_number_to_chars (f, 0xf000 | ip->extend, 2);
2158 f += 2;
2159 }
2160 md_number_to_chars (f, ip->insn_opcode, 2);
c5dd6aab
DJ
2161#ifdef OBJ_ELF
2162 dwarf2_emit_insn (ip->use_extend ? 4 : 2);
2163#endif
252b5132
RH
2164 }
2165
2166 /* Update the register mask information. */
2167 if (! mips_opts.mips16)
2168 {
2169 if (pinfo & INSN_WRITE_GPR_D)
2170 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
2171 if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2172 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
2173 if (pinfo & INSN_READ_GPR_S)
2174 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
2175 if (pinfo & INSN_WRITE_GPR_31)
f9419b05 2176 mips_gprmask |= 1 << RA;
252b5132
RH
2177 if (pinfo & INSN_WRITE_FPR_D)
2178 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
2179 if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2180 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
2181 if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2182 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
2183 if ((pinfo & INSN_READ_FPR_R) != 0)
2184 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
2185 if (pinfo & INSN_COP)
2186 {
bdaaa2e1
KH
2187 /* We don't keep enough information to sort these cases out.
2188 The itbl support does keep this information however, although
2189 we currently don't support itbl fprmats as part of the cop
2190 instruction. May want to add this support in the future. */
252b5132
RH
2191 }
2192 /* Never set the bit for $0, which is always zero. */
beae10d5 2193 mips_gprmask &= ~1 << 0;
252b5132
RH
2194 }
2195 else
2196 {
2197 if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2198 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RX)
2199 & MIPS16OP_MASK_RX);
2200 if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2201 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RY)
2202 & MIPS16OP_MASK_RY);
2203 if (pinfo & MIPS16_INSN_WRITE_Z)
2204 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RZ)
2205 & MIPS16OP_MASK_RZ);
2206 if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2207 mips_gprmask |= 1 << TREG;
2208 if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2209 mips_gprmask |= 1 << SP;
2210 if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2211 mips_gprmask |= 1 << RA;
2212 if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2213 mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2214 if (pinfo & MIPS16_INSN_READ_Z)
2215 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
2216 & MIPS16OP_MASK_MOVE32Z);
2217 if (pinfo & MIPS16_INSN_READ_GPR_X)
2218 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
2219 & MIPS16OP_MASK_REGR32);
2220 }
2221
2222 if (place == NULL && ! mips_opts.noreorder)
2223 {
2224 /* Filling the branch delay slot is more complex. We try to
2225 switch the branch with the previous instruction, which we can
2226 do if the previous instruction does not set up a condition
2227 that the branch tests and if the branch is not itself the
2228 target of any branch. */
2229 if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2230 || (pinfo & INSN_COND_BRANCH_DELAY))
2231 {
2232 if (mips_optimize < 2
2233 /* If we have seen .set volatile or .set nomove, don't
2234 optimize. */
2235 || mips_opts.nomove != 0
2236 /* If we had to emit any NOP instructions, then we
2237 already know we can not swap. */
2238 || nops != 0
2239 /* If we don't even know the previous insn, we can not
bdaaa2e1 2240 swap. */
252b5132
RH
2241 || ! prev_insn_valid
2242 /* If the previous insn is already in a branch delay
2243 slot, then we can not swap. */
2244 || prev_insn_is_delay_slot
2245 /* If the previous previous insn was in a .set
2246 noreorder, we can't swap. Actually, the MIPS
2247 assembler will swap in this situation. However, gcc
2248 configured -with-gnu-as will generate code like
2249 .set noreorder
2250 lw $4,XXX
2251 .set reorder
2252 INSN
2253 bne $4,$0,foo
2254 in which we can not swap the bne and INSN. If gcc is
2255 not configured -with-gnu-as, it does not output the
2256 .set pseudo-ops. We don't have to check
2257 prev_insn_unreordered, because prev_insn_valid will
2258 be 0 in that case. We don't want to use
2259 prev_prev_insn_valid, because we do want to be able
2260 to swap at the start of a function. */
2261 || prev_prev_insn_unreordered
2262 /* If the branch is itself the target of a branch, we
2263 can not swap. We cheat on this; all we check for is
2264 whether there is a label on this instruction. If
2265 there are any branches to anything other than a
2266 label, users must use .set noreorder. */
2267 || insn_labels != NULL
2268 /* If the previous instruction is in a variant frag, we
2269 can not do the swap. This does not apply to the
2270 mips16, which uses variant frags for different
2271 purposes. */
2272 || (! mips_opts.mips16
2273 && prev_insn_frag->fr_type == rs_machine_dependent)
2274 /* If the branch reads the condition codes, we don't
2275 even try to swap, because in the sequence
2276 ctc1 $X,$31
2277 INSN
2278 INSN
2279 bc1t LABEL
2280 we can not swap, and I don't feel like handling that
2281 case. */
2282 || (! mips_opts.mips16
81912461
ILT
2283 && (pinfo & INSN_READ_COND_CODE)
2284 && ! cop_interlocks)
252b5132 2285 /* We can not swap with an instruction that requires a
67c1ffbe 2286 delay slot, because the target of the branch might
252b5132
RH
2287 interfere with that instruction. */
2288 || (! mips_opts.mips16
252b5132 2289 && (prev_pinfo
bdaaa2e1 2290 /* Itbl support may require additional care here. */
252b5132
RH
2291 & (INSN_LOAD_COPROC_DELAY
2292 | INSN_COPROC_MOVE_DELAY
81912461
ILT
2293 | INSN_WRITE_COND_CODE))
2294 && ! cop_interlocks)
252b5132 2295 || (! (hilo_interlocks
ec68c924 2296 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
252b5132
RH
2297 && (prev_pinfo
2298 & (INSN_READ_LO
2299 | INSN_READ_HI)))
2300 || (! mips_opts.mips16
81912461
ILT
2301 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY)
2302 && ! gpr_interlocks)
252b5132 2303 || (! mips_opts.mips16
bdaaa2e1 2304 /* Itbl support may require additional care here. */
81912461
ILT
2305 && (prev_pinfo & INSN_COPROC_MEMORY_DELAY)
2306 && ! cop_mem_interlocks)
252b5132
RH
2307 /* We can not swap with a branch instruction. */
2308 || (prev_pinfo
2309 & (INSN_UNCOND_BRANCH_DELAY
2310 | INSN_COND_BRANCH_DELAY
2311 | INSN_COND_BRANCH_LIKELY))
2312 /* We do not swap with a trap instruction, since it
2313 complicates trap handlers to have the trap
2314 instruction be in a delay slot. */
2315 || (prev_pinfo & INSN_TRAP)
2316 /* If the branch reads a register that the previous
2317 instruction sets, we can not swap. */
2318 || (! mips_opts.mips16
2319 && (prev_pinfo & INSN_WRITE_GPR_T)
2320 && insn_uses_reg (ip,
2321 ((prev_insn.insn_opcode >> OP_SH_RT)
2322 & OP_MASK_RT),
2323 MIPS_GR_REG))
2324 || (! mips_opts.mips16
2325 && (prev_pinfo & INSN_WRITE_GPR_D)
2326 && insn_uses_reg (ip,
2327 ((prev_insn.insn_opcode >> OP_SH_RD)
2328 & OP_MASK_RD),
2329 MIPS_GR_REG))
2330 || (mips_opts.mips16
2331 && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2332 && insn_uses_reg (ip,
2333 ((prev_insn.insn_opcode
2334 >> MIPS16OP_SH_RX)
2335 & MIPS16OP_MASK_RX),
2336 MIPS16_REG))
2337 || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2338 && insn_uses_reg (ip,
2339 ((prev_insn.insn_opcode
2340 >> MIPS16OP_SH_RY)
2341 & MIPS16OP_MASK_RY),
2342 MIPS16_REG))
2343 || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2344 && insn_uses_reg (ip,
2345 ((prev_insn.insn_opcode
2346 >> MIPS16OP_SH_RZ)
2347 & MIPS16OP_MASK_RZ),
2348 MIPS16_REG))
2349 || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2350 && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2351 || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2352 && insn_uses_reg (ip, RA, MIPS_GR_REG))
2353 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2354 && insn_uses_reg (ip,
2355 MIPS16OP_EXTRACT_REG32R (prev_insn.
2356 insn_opcode),
2357 MIPS_GR_REG))))
2358 /* If the branch writes a register that the previous
2359 instruction sets, we can not swap (we know that
2360 branches write only to RD or to $31). */
2361 || (! mips_opts.mips16
2362 && (prev_pinfo & INSN_WRITE_GPR_T)
2363 && (((pinfo & INSN_WRITE_GPR_D)
2364 && (((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT)
2365 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2366 || ((pinfo & INSN_WRITE_GPR_31)
2367 && (((prev_insn.insn_opcode >> OP_SH_RT)
2368 & OP_MASK_RT)
f9419b05 2369 == RA))))
252b5132
RH
2370 || (! mips_opts.mips16
2371 && (prev_pinfo & INSN_WRITE_GPR_D)
2372 && (((pinfo & INSN_WRITE_GPR_D)
2373 && (((prev_insn.insn_opcode >> OP_SH_RD) & OP_MASK_RD)
2374 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2375 || ((pinfo & INSN_WRITE_GPR_31)
2376 && (((prev_insn.insn_opcode >> OP_SH_RD)
2377 & OP_MASK_RD)
f9419b05 2378 == RA))))
252b5132
RH
2379 || (mips_opts.mips16
2380 && (pinfo & MIPS16_INSN_WRITE_31)
2381 && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2382 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2383 && (MIPS16OP_EXTRACT_REG32R (prev_insn.insn_opcode)
2384 == RA))))
2385 /* If the branch writes a register that the previous
2386 instruction reads, we can not swap (we know that
2387 branches only write to RD or to $31). */
2388 || (! mips_opts.mips16
2389 && (pinfo & INSN_WRITE_GPR_D)
2390 && insn_uses_reg (&prev_insn,
2391 ((ip->insn_opcode >> OP_SH_RD)
2392 & OP_MASK_RD),
2393 MIPS_GR_REG))
2394 || (! mips_opts.mips16
2395 && (pinfo & INSN_WRITE_GPR_31)
f9419b05 2396 && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
252b5132
RH
2397 || (mips_opts.mips16
2398 && (pinfo & MIPS16_INSN_WRITE_31)
2399 && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2400 /* If we are generating embedded PIC code, the branch
2401 might be expanded into a sequence which uses $at, so
2402 we can't swap with an instruction which reads it. */
2403 || (mips_pic == EMBEDDED_PIC
2404 && insn_uses_reg (&prev_insn, AT, MIPS_GR_REG))
2405 /* If the previous previous instruction has a load
2406 delay, and sets a register that the branch reads, we
2407 can not swap. */
2408 || (! mips_opts.mips16
bdaaa2e1 2409 /* Itbl support may require additional care here. */
81912461
ILT
2410 && (((prev_prev_insn.insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
2411 && ! cop_interlocks)
2412 || ((prev_prev_insn.insn_mo->pinfo
2413 & INSN_LOAD_MEMORY_DELAY)
2414 && ! gpr_interlocks))
252b5132
RH
2415 && insn_uses_reg (ip,
2416 ((prev_prev_insn.insn_opcode >> OP_SH_RT)
2417 & OP_MASK_RT),
2418 MIPS_GR_REG))
2419 /* If one instruction sets a condition code and the
2420 other one uses a condition code, we can not swap. */
2421 || ((pinfo & INSN_READ_COND_CODE)
2422 && (prev_pinfo & INSN_WRITE_COND_CODE))
2423 || ((pinfo & INSN_WRITE_COND_CODE)
2424 && (prev_pinfo & INSN_READ_COND_CODE))
2425 /* If the previous instruction uses the PC, we can not
2426 swap. */
2427 || (mips_opts.mips16
2428 && (prev_pinfo & MIPS16_INSN_READ_PC))
2429 /* If the previous instruction was extended, we can not
2430 swap. */
2431 || (mips_opts.mips16 && prev_insn_extended)
2432 /* If the previous instruction had a fixup in mips16
2433 mode, we can not swap. This normally means that the
2434 previous instruction was a 4 byte branch anyhow. */
f6688943 2435 || (mips_opts.mips16 && prev_insn_fixp[0])
bdaaa2e1
KH
2436 /* If the previous instruction is a sync, sync.l, or
2437 sync.p, we can not swap. */
f173e82e 2438 || (prev_pinfo & INSN_SYNC))
252b5132
RH
2439 {
2440 /* We could do even better for unconditional branches to
2441 portions of this object file; we could pick up the
2442 instruction at the destination, put it in the delay
2443 slot, and bump the destination address. */
2444 emit_nop ();
2445 /* Update the previous insn information. */
2446 prev_prev_insn = *ip;
2447 prev_insn.insn_mo = &dummy_opcode;
2448 }
2449 else
2450 {
2451 /* It looks like we can actually do the swap. */
2452 if (! mips_opts.mips16)
2453 {
2454 char *prev_f;
2455 char temp[4];
2456
2457 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2458 memcpy (temp, prev_f, 4);
2459 memcpy (prev_f, f, 4);
2460 memcpy (f, temp, 4);
f6688943
TS
2461 if (prev_insn_fixp[0])
2462 {
2463 prev_insn_fixp[0]->fx_frag = frag_now;
2464 prev_insn_fixp[0]->fx_where = f - frag_now->fr_literal;
2465 }
2466 if (prev_insn_fixp[1])
2467 {
2468 prev_insn_fixp[1]->fx_frag = frag_now;
2469 prev_insn_fixp[1]->fx_where = f - frag_now->fr_literal;
2470 }
2471 if (prev_insn_fixp[2])
252b5132 2472 {
f6688943
TS
2473 prev_insn_fixp[2]->fx_frag = frag_now;
2474 prev_insn_fixp[2]->fx_where = f - frag_now->fr_literal;
252b5132 2475 }
f5040a92
AO
2476 if (prev_insn_fixp[0] && HAVE_NEWABI
2477 && prev_insn_frag != frag_now
2478 && (prev_insn_fixp[0]->fx_r_type
2479 == BFD_RELOC_MIPS_GOT_DISP
2480 || (prev_insn_fixp[0]->fx_r_type
2481 == BFD_RELOC_MIPS_CALL16)))
2482 {
2483 /* To avoid confusion in tc_gen_reloc, we must
2484 ensure that this does not become a variant
2485 frag. */
2486 force_new_frag = TRUE;
2487 }
f6688943 2488 if (fixp[0])
252b5132 2489 {
f6688943
TS
2490 fixp[0]->fx_frag = prev_insn_frag;
2491 fixp[0]->fx_where = prev_insn_where;
2492 }
2493 if (fixp[1])
2494 {
2495 fixp[1]->fx_frag = prev_insn_frag;
2496 fixp[1]->fx_where = prev_insn_where;
2497 }
2498 if (fixp[2])
2499 {
2500 fixp[2]->fx_frag = prev_insn_frag;
2501 fixp[2]->fx_where = prev_insn_where;
252b5132
RH
2502 }
2503 }
2504 else
2505 {
2506 char *prev_f;
2507 char temp[2];
2508
f6688943
TS
2509 assert (prev_insn_fixp[0] == NULL);
2510 assert (prev_insn_fixp[1] == NULL);
2511 assert (prev_insn_fixp[2] == NULL);
252b5132
RH
2512 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2513 memcpy (temp, prev_f, 2);
2514 memcpy (prev_f, f, 2);
f6688943 2515 if (*reloc_type != BFD_RELOC_MIPS16_JMP)
252b5132 2516 {
f6688943 2517 assert (*reloc_type == BFD_RELOC_UNUSED);
252b5132
RH
2518 memcpy (f, temp, 2);
2519 }
2520 else
2521 {
2522 memcpy (f, f + 2, 2);
2523 memcpy (f + 2, temp, 2);
2524 }
f6688943
TS
2525 if (fixp[0])
2526 {
2527 fixp[0]->fx_frag = prev_insn_frag;
2528 fixp[0]->fx_where = prev_insn_where;
2529 }
2530 if (fixp[1])
2531 {
2532 fixp[1]->fx_frag = prev_insn_frag;
2533 fixp[1]->fx_where = prev_insn_where;
2534 }
2535 if (fixp[2])
252b5132 2536 {
f6688943
TS
2537 fixp[2]->fx_frag = prev_insn_frag;
2538 fixp[2]->fx_where = prev_insn_where;
252b5132
RH
2539 }
2540 }
2541
2542 /* Update the previous insn information; leave prev_insn
2543 unchanged. */
2544 prev_prev_insn = *ip;
2545 }
2546 prev_insn_is_delay_slot = 1;
2547
2548 /* If that was an unconditional branch, forget the previous
2549 insn information. */
2550 if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2551 {
2552 prev_prev_insn.insn_mo = &dummy_opcode;
2553 prev_insn.insn_mo = &dummy_opcode;
2554 }
2555
f6688943
TS
2556 prev_insn_fixp[0] = NULL;
2557 prev_insn_fixp[1] = NULL;
2558 prev_insn_fixp[2] = NULL;
2559 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2560 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2561 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
252b5132
RH
2562 prev_insn_extended = 0;
2563 }
2564 else if (pinfo & INSN_COND_BRANCH_LIKELY)
2565 {
2566 /* We don't yet optimize a branch likely. What we should do
2567 is look at the target, copy the instruction found there
2568 into the delay slot, and increment the branch to jump to
2569 the next instruction. */
2570 emit_nop ();
2571 /* Update the previous insn information. */
2572 prev_prev_insn = *ip;
2573 prev_insn.insn_mo = &dummy_opcode;
f6688943
TS
2574 prev_insn_fixp[0] = NULL;
2575 prev_insn_fixp[1] = NULL;
2576 prev_insn_fixp[2] = NULL;
2577 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2578 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2579 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
252b5132
RH
2580 prev_insn_extended = 0;
2581 }
2582 else
2583 {
2584 /* Update the previous insn information. */
2585 if (nops > 0)
2586 prev_prev_insn.insn_mo = &dummy_opcode;
2587 else
2588 prev_prev_insn = prev_insn;
2589 prev_insn = *ip;
2590
2591 /* Any time we see a branch, we always fill the delay slot
2592 immediately; since this insn is not a branch, we know it
2593 is not in a delay slot. */
2594 prev_insn_is_delay_slot = 0;
2595
f6688943
TS
2596 prev_insn_fixp[0] = fixp[0];
2597 prev_insn_fixp[1] = fixp[1];
2598 prev_insn_fixp[2] = fixp[2];
2599 prev_insn_reloc_type[0] = reloc_type[0];
2600 prev_insn_reloc_type[1] = reloc_type[1];
2601 prev_insn_reloc_type[2] = reloc_type[2];
252b5132
RH
2602 if (mips_opts.mips16)
2603 prev_insn_extended = (ip->use_extend
f6688943 2604 || *reloc_type > BFD_RELOC_UNUSED);
252b5132
RH
2605 }
2606
2607 prev_prev_insn_unreordered = prev_insn_unreordered;
2608 prev_insn_unreordered = 0;
2609 prev_insn_frag = frag_now;
2610 prev_insn_where = f - frag_now->fr_literal;
2611 prev_insn_valid = 1;
2612 }
2613 else if (place == NULL)
2614 {
2615 /* We need to record a bit of information even when we are not
2616 reordering, in order to determine the base address for mips16
2617 PC relative relocs. */
2618 prev_prev_insn = prev_insn;
2619 prev_insn = *ip;
f6688943
TS
2620 prev_insn_reloc_type[0] = reloc_type[0];
2621 prev_insn_reloc_type[1] = reloc_type[1];
2622 prev_insn_reloc_type[2] = reloc_type[2];
252b5132
RH
2623 prev_prev_insn_unreordered = prev_insn_unreordered;
2624 prev_insn_unreordered = 1;
2625 }
2626
2627 /* We just output an insn, so the next one doesn't have a label. */
2628 mips_clear_insn_labels ();
f5040a92
AO
2629
2630 /* We must ensure that the frag to which an instruction that was
2631 moved from a non-variant frag doesn't become a variant frag,
2632 otherwise tc_gen_reloc may get confused. */
2633 if (force_new_frag)
2634 {
2635 frag_wane (frag_now);
2636 frag_new (0);
2637 }
252b5132
RH
2638}
2639
2640/* This function forgets that there was any previous instruction or
2641 label. If PRESERVE is non-zero, it remembers enough information to
bdaaa2e1 2642 know whether nops are needed before a noreorder section. */
252b5132
RH
2643
2644static void
17a2f251 2645mips_no_prev_insn (int preserve)
252b5132
RH
2646{
2647 if (! preserve)
2648 {
2649 prev_insn.insn_mo = &dummy_opcode;
2650 prev_prev_insn.insn_mo = &dummy_opcode;
2651 prev_nop_frag = NULL;
2652 prev_nop_frag_holds = 0;
2653 prev_nop_frag_required = 0;
2654 prev_nop_frag_since = 0;
2655 }
2656 prev_insn_valid = 0;
2657 prev_insn_is_delay_slot = 0;
2658 prev_insn_unreordered = 0;
2659 prev_insn_extended = 0;
f6688943
TS
2660 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2661 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2662 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
252b5132
RH
2663 prev_prev_insn_unreordered = 0;
2664 mips_clear_insn_labels ();
2665}
2666
2667/* This function must be called whenever we turn on noreorder or emit
2668 something other than instructions. It inserts any NOPS which might
2669 be needed by the previous instruction, and clears the information
2670 kept for the previous instructions. The INSNS parameter is true if
bdaaa2e1 2671 instructions are to follow. */
252b5132
RH
2672
2673static void
17a2f251 2674mips_emit_delays (bfd_boolean insns)
252b5132
RH
2675{
2676 if (! mips_opts.noreorder)
2677 {
2678 int nops;
2679
2680 nops = 0;
2681 if ((! mips_opts.mips16
81912461
ILT
2682 && ((prev_insn.insn_mo->pinfo
2683 & (INSN_LOAD_COPROC_DELAY
2684 | INSN_COPROC_MOVE_DELAY
2685 | INSN_WRITE_COND_CODE))
2686 && ! cop_interlocks))
252b5132
RH
2687 || (! hilo_interlocks
2688 && (prev_insn.insn_mo->pinfo
2689 & (INSN_READ_LO
2690 | INSN_READ_HI)))
2691 || (! mips_opts.mips16
81912461
ILT
2692 && (prev_insn.insn_mo->pinfo & INSN_LOAD_MEMORY_DELAY)
2693 && ! gpr_interlocks)
252b5132 2694 || (! mips_opts.mips16
81912461
ILT
2695 && (prev_insn.insn_mo->pinfo & INSN_COPROC_MEMORY_DELAY)
2696 && ! cop_mem_interlocks))
252b5132 2697 {
beae10d5 2698 /* Itbl support may require additional care here. */
252b5132
RH
2699 ++nops;
2700 if ((! mips_opts.mips16
81912461
ILT
2701 && ((prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
2702 && ! cop_interlocks))
252b5132
RH
2703 || (! hilo_interlocks
2704 && ((prev_insn.insn_mo->pinfo & INSN_READ_HI)
2705 || (prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2706 ++nops;
2707
2708 if (prev_insn_unreordered)
2709 nops = 0;
2710 }
2711 else if ((! mips_opts.mips16
81912461
ILT
2712 && ((prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
2713 && ! cop_interlocks))
252b5132
RH
2714 || (! hilo_interlocks
2715 && ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
2716 || (prev_prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2717 {
beae10d5 2718 /* Itbl support may require additional care here. */
252b5132
RH
2719 if (! prev_prev_insn_unreordered)
2720 ++nops;
2721 }
2722
60b63b72
RS
2723 if (mips_fix_4122_bugs && prev_insn.insn_mo->name)
2724 {
2725 int min_nops = 0;
2726 const char *pn = prev_insn.insn_mo->name;
2727 if (strncmp(pn, "macc", 4) == 0
2728 || strncmp(pn, "dmacc", 5) == 0
2729 || strncmp(pn, "dmult", 5) == 0)
2730 {
2731 min_nops = 1;
2732 }
2733 if (nops < min_nops)
2734 nops = min_nops;
2735 }
2736
252b5132
RH
2737 if (nops > 0)
2738 {
2739 struct insn_label_list *l;
2740
2741 if (insns)
2742 {
2743 /* Record the frag which holds the nop instructions, so
2744 that we can remove them if we don't need them. */
2745 frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2746 prev_nop_frag = frag_now;
2747 prev_nop_frag_holds = nops;
2748 prev_nop_frag_required = 0;
2749 prev_nop_frag_since = 0;
2750 }
2751
2752 for (; nops > 0; --nops)
2753 emit_nop ();
2754
2755 if (insns)
2756 {
2757 /* Move on to a new frag, so that it is safe to simply
bdaaa2e1 2758 decrease the size of prev_nop_frag. */
252b5132
RH
2759 frag_wane (frag_now);
2760 frag_new (0);
2761 }
2762
2763 for (l = insn_labels; l != NULL; l = l->next)
2764 {
98aa84af
AM
2765 valueT val;
2766
252b5132 2767 assert (S_GET_SEGMENT (l->label) == now_seg);
49309057 2768 symbol_set_frag (l->label, frag_now);
98aa84af 2769 val = (valueT) frag_now_fix ();
252b5132
RH
2770 /* mips16 text labels are stored as odd. */
2771 if (mips_opts.mips16)
f9419b05 2772 ++val;
98aa84af 2773 S_SET_VALUE (l->label, val);
252b5132
RH
2774 }
2775 }
2776 }
2777
2778 /* Mark instruction labels in mips16 mode. */
f9419b05 2779 if (insns)
252b5132
RH
2780 mips16_mark_labels ();
2781
2782 mips_no_prev_insn (insns);
2783}
2784
2785/* Build an instruction created by a macro expansion. This is passed
2786 a pointer to the count of instructions created so far, an
2787 expression, the name of the instruction to build, an operand format
2788 string, and corresponding arguments. */
2789
252b5132 2790static void
17a2f251
TS
2791macro_build (char *place, int *counter, expressionS *ep, const char *name,
2792 const char *fmt, ...)
252b5132
RH
2793{
2794 struct mips_cl_insn insn;
f6688943 2795 bfd_reloc_code_real_type r[3];
252b5132 2796 va_list args;
252b5132 2797
252b5132 2798 va_start (args, fmt);
252b5132
RH
2799
2800 /*
2801 * If the macro is about to expand into a second instruction,
2802 * print a warning if needed. We need to pass ip as a parameter
2803 * to generate a better warning message here...
2804 */
2805 if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
2806 as_warn (_("Macro instruction expanded into multiple instructions"));
2807
80cc45a5
EC
2808 /*
2809 * If the macro is about to expand into a second instruction,
2810 * and it is in a delay slot, print a warning.
2811 */
2812 if (place == NULL
2813 && *counter == 1
2814 && mips_opts.noreorder
2815 && (prev_prev_insn.insn_mo->pinfo
2816 & (INSN_UNCOND_BRANCH_DELAY | INSN_COND_BRANCH_DELAY
2b2e39bf 2817 | INSN_COND_BRANCH_LIKELY)) != 0)
80cc45a5
EC
2818 as_warn (_("Macro instruction expanded into multiple instructions in a branch delay slot"));
2819
252b5132 2820 if (place == NULL)
f9419b05 2821 ++*counter; /* bump instruction counter */
252b5132
RH
2822
2823 if (mips_opts.mips16)
2824 {
2825 mips16_macro_build (place, counter, ep, name, fmt, args);
2826 va_end (args);
2827 return;
2828 }
2829
f6688943
TS
2830 r[0] = BFD_RELOC_UNUSED;
2831 r[1] = BFD_RELOC_UNUSED;
2832 r[2] = BFD_RELOC_UNUSED;
252b5132
RH
2833 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
2834 assert (insn.insn_mo);
2835 assert (strcmp (name, insn.insn_mo->name) == 0);
2836
2837 /* Search until we get a match for NAME. */
2838 while (1)
2839 {
b34976b6 2840 /* It is assumed here that macros will never generate
deec1734 2841 MDMX or MIPS-3D instructions. */
252b5132
RH
2842 if (strcmp (fmt, insn.insn_mo->args) == 0
2843 && insn.insn_mo->pinfo != INSN_MACRO
aec421e0
TS
2844 && OPCODE_IS_MEMBER (insn.insn_mo,
2845 (mips_opts.isa
3396de36 2846 | (file_ase_mips16 ? INSN_MIPS16 : 0)),
fef14a42
TS
2847 mips_opts.arch)
2848 && (mips_opts.arch != CPU_R4650 || (insn.insn_mo->pinfo & FP_D) == 0))
252b5132
RH
2849 break;
2850
2851 ++insn.insn_mo;
2852 assert (insn.insn_mo->name);
2853 assert (strcmp (name, insn.insn_mo->name) == 0);
2854 }
2855
2856 insn.insn_opcode = insn.insn_mo->match;
2857 for (;;)
2858 {
2859 switch (*fmt++)
2860 {
2861 case '\0':
2862 break;
2863
2864 case ',':
2865 case '(':
2866 case ')':
2867 continue;
2868
5f74bc13
CD
2869 case '+':
2870 switch (*fmt++)
2871 {
2872 case 'A':
2873 case 'E':
2874 insn.insn_opcode |= (va_arg (args, int)
2875 & OP_MASK_SHAMT) << OP_SH_SHAMT;
2876 continue;
2877
2878 case 'B':
2879 case 'F':
2880 /* Note that in the macro case, these arguments are already
2881 in MSB form. (When handling the instruction in the
2882 non-macro case, these arguments are sizes from which
2883 MSB values must be calculated.) */
2884 insn.insn_opcode |= (va_arg (args, int)
2885 & OP_MASK_INSMSB) << OP_SH_INSMSB;
2886 continue;
2887
2888 case 'C':
2889 case 'G':
2890 case 'H':
2891 /* Note that in the macro case, these arguments are already
2892 in MSBD form. (When handling the instruction in the
2893 non-macro case, these arguments are sizes from which
2894 MSBD values must be calculated.) */
2895 insn.insn_opcode |= (va_arg (args, int)
2896 & OP_MASK_EXTMSBD) << OP_SH_EXTMSBD;
2897 continue;
2898
2899 default:
2900 internalError ();
2901 }
2902 continue;
2903
252b5132
RH
2904 case 't':
2905 case 'w':
2906 case 'E':
38487616 2907 insn.insn_opcode |= va_arg (args, int) << OP_SH_RT;
252b5132
RH
2908 continue;
2909
2910 case 'c':
38487616
TS
2911 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE;
2912 continue;
2913
252b5132
RH
2914 case 'T':
2915 case 'W':
38487616 2916 insn.insn_opcode |= va_arg (args, int) << OP_SH_FT;
252b5132
RH
2917 continue;
2918
2919 case 'd':
2920 case 'G':
af7ee8bf 2921 case 'K':
38487616 2922 insn.insn_opcode |= va_arg (args, int) << OP_SH_RD;
252b5132
RH
2923 continue;
2924
4372b673
NC
2925 case 'U':
2926 {
2927 int tmp = va_arg (args, int);
2928
38487616
TS
2929 insn.insn_opcode |= tmp << OP_SH_RT;
2930 insn.insn_opcode |= tmp << OP_SH_RD;
beae10d5 2931 continue;
4372b673
NC
2932 }
2933
252b5132
RH
2934 case 'V':
2935 case 'S':
38487616 2936 insn.insn_opcode |= va_arg (args, int) << OP_SH_FS;
252b5132
RH
2937 continue;
2938
2939 case 'z':
2940 continue;
2941
2942 case '<':
38487616 2943 insn.insn_opcode |= va_arg (args, int) << OP_SH_SHAMT;
252b5132
RH
2944 continue;
2945
2946 case 'D':
38487616 2947 insn.insn_opcode |= va_arg (args, int) << OP_SH_FD;
252b5132
RH
2948 continue;
2949
2950 case 'B':
38487616 2951 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE20;
252b5132
RH
2952 continue;
2953
4372b673 2954 case 'J':
38487616 2955 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE19;
4372b673
NC
2956 continue;
2957
252b5132 2958 case 'q':
38487616 2959 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE2;
252b5132
RH
2960 continue;
2961
2962 case 'b':
2963 case 's':
2964 case 'r':
2965 case 'v':
38487616 2966 insn.insn_opcode |= va_arg (args, int) << OP_SH_RS;
252b5132
RH
2967 continue;
2968
2969 case 'i':
2970 case 'j':
2971 case 'o':
f6688943 2972 *r = (bfd_reloc_code_real_type) va_arg (args, int);
cdf6fd85 2973 assert (*r == BFD_RELOC_GPREL16
f6688943
TS
2974 || *r == BFD_RELOC_MIPS_LITERAL
2975 || *r == BFD_RELOC_MIPS_HIGHER
2976 || *r == BFD_RELOC_HI16_S
2977 || *r == BFD_RELOC_LO16
2978 || *r == BFD_RELOC_MIPS_GOT16
2979 || *r == BFD_RELOC_MIPS_CALL16
438c16b8
TS
2980 || *r == BFD_RELOC_MIPS_GOT_DISP
2981 || *r == BFD_RELOC_MIPS_GOT_PAGE
2982 || *r == BFD_RELOC_MIPS_GOT_OFST
f6688943
TS
2983 || *r == BFD_RELOC_MIPS_GOT_LO16
2984 || *r == BFD_RELOC_MIPS_CALL_LO16
252b5132 2985 || (ep->X_op == O_subtract
f6688943 2986 && *r == BFD_RELOC_PCREL_LO16));
252b5132
RH
2987 continue;
2988
2989 case 'u':
f6688943 2990 *r = (bfd_reloc_code_real_type) va_arg (args, int);
252b5132
RH
2991 assert (ep != NULL
2992 && (ep->X_op == O_constant
2993 || (ep->X_op == O_symbol
f6688943
TS
2994 && (*r == BFD_RELOC_MIPS_HIGHEST
2995 || *r == BFD_RELOC_HI16_S
2996 || *r == BFD_RELOC_HI16
2997 || *r == BFD_RELOC_GPREL16
2998 || *r == BFD_RELOC_MIPS_GOT_HI16
2999 || *r == BFD_RELOC_MIPS_CALL_HI16))
252b5132 3000 || (ep->X_op == O_subtract
f6688943 3001 && *r == BFD_RELOC_PCREL_HI16_S)));
252b5132
RH
3002 continue;
3003
3004 case 'p':
3005 assert (ep != NULL);
3006 /*
3007 * This allows macro() to pass an immediate expression for
3008 * creating short branches without creating a symbol.
0b25d3e6
AO
3009 * Note that the expression still might come from the assembly
3010 * input, in which case the value is not checked for range nor
3011 * is a relocation entry generated (yuck).
252b5132
RH
3012 */
3013 if (ep->X_op == O_constant)
3014 {
3015 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
3016 ep = NULL;
3017 }
3018 else
0b25d3e6 3019 *r = BFD_RELOC_16_PCREL_S2;
252b5132
RH
3020 continue;
3021
3022 case 'a':
3023 assert (ep != NULL);
f6688943 3024 *r = BFD_RELOC_MIPS_JMP;
252b5132
RH
3025 continue;
3026
3027 case 'C':
3028 insn.insn_opcode |= va_arg (args, unsigned long);
3029 continue;
3030
3031 default:
3032 internalError ();
3033 }
3034 break;
3035 }
3036 va_end (args);
f6688943 3037 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 3038
5e0116d5 3039 append_insn (place, &insn, ep, r);
252b5132
RH
3040}
3041
3042static void
17a2f251
TS
3043mips16_macro_build (char *place, int *counter ATTRIBUTE_UNUSED,
3044 expressionS *ep, const char *name, const char *fmt,
3045 va_list args)
252b5132
RH
3046{
3047 struct mips_cl_insn insn;
f6688943
TS
3048 bfd_reloc_code_real_type r[3]
3049 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 3050
252b5132
RH
3051 insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
3052 assert (insn.insn_mo);
3053 assert (strcmp (name, insn.insn_mo->name) == 0);
3054
3055 while (strcmp (fmt, insn.insn_mo->args) != 0
3056 || insn.insn_mo->pinfo == INSN_MACRO)
3057 {
3058 ++insn.insn_mo;
3059 assert (insn.insn_mo->name);
3060 assert (strcmp (name, insn.insn_mo->name) == 0);
3061 }
3062
3063 insn.insn_opcode = insn.insn_mo->match;
b34976b6 3064 insn.use_extend = FALSE;
252b5132
RH
3065
3066 for (;;)
3067 {
3068 int c;
3069
3070 c = *fmt++;
3071 switch (c)
3072 {
3073 case '\0':
3074 break;
3075
3076 case ',':
3077 case '(':
3078 case ')':
3079 continue;
3080
3081 case 'y':
3082 case 'w':
3083 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
3084 continue;
3085
3086 case 'x':
3087 case 'v':
3088 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
3089 continue;
3090
3091 case 'z':
3092 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
3093 continue;
3094
3095 case 'Z':
3096 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
3097 continue;
3098
3099 case '0':
3100 case 'S':
3101 case 'P':
3102 case 'R':
3103 continue;
3104
3105 case 'X':
3106 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
3107 continue;
3108
3109 case 'Y':
3110 {
3111 int regno;
3112
3113 regno = va_arg (args, int);
3114 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
3115 insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
3116 }
3117 continue;
3118
3119 case '<':
3120 case '>':
3121 case '4':
3122 case '5':
3123 case 'H':
3124 case 'W':
3125 case 'D':
3126 case 'j':
3127 case '8':
3128 case 'V':
3129 case 'C':
3130 case 'U':
3131 case 'k':
3132 case 'K':
3133 case 'p':
3134 case 'q':
3135 {
3136 assert (ep != NULL);
3137
3138 if (ep->X_op != O_constant)
874e8986 3139 *r = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
3140 else
3141 {
b34976b6
AM
3142 mips16_immed (NULL, 0, c, ep->X_add_number, FALSE, FALSE,
3143 FALSE, &insn.insn_opcode, &insn.use_extend,
c4e7957c 3144 &insn.extend);
252b5132 3145 ep = NULL;
f6688943 3146 *r = BFD_RELOC_UNUSED;
252b5132
RH
3147 }
3148 }
3149 continue;
3150
3151 case '6':
3152 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
3153 continue;
3154 }
3155
3156 break;
3157 }
3158
f6688943 3159 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 3160
5e0116d5 3161 append_insn (place, &insn, ep, r);
252b5132
RH
3162}
3163
438c16b8
TS
3164/*
3165 * Generate a "jalr" instruction with a relocation hint to the called
3166 * function. This occurs in NewABI PIC code.
3167 */
3168static void
17a2f251 3169macro_build_jalr (int icnt, expressionS *ep)
438c16b8 3170{
685736be 3171 char *f = NULL;
b34976b6 3172
438c16b8 3173 if (HAVE_NEWABI)
f21f8242
AO
3174 {
3175 frag_grow (4);
3176 f = frag_more (0);
3177 }
17a2f251 3178 macro_build (NULL, &icnt, NULL, "jalr", "d,s", RA, PIC_CALL_REG);
438c16b8 3179 if (HAVE_NEWABI)
f21f8242 3180 fix_new_exp (frag_now, f - frag_now->fr_literal,
a105a300 3181 4, ep, FALSE, BFD_RELOC_MIPS_JALR);
438c16b8
TS
3182}
3183
252b5132
RH
3184/*
3185 * Generate a "lui" instruction.
3186 */
3187static void
17a2f251 3188macro_build_lui (char *place, int *counter, expressionS *ep, int regnum)
252b5132
RH
3189{
3190 expressionS high_expr;
3191 struct mips_cl_insn insn;
f6688943
TS
3192 bfd_reloc_code_real_type r[3]
3193 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
5a38dc70
AM
3194 const char *name = "lui";
3195 const char *fmt = "t,u";
252b5132
RH
3196
3197 assert (! mips_opts.mips16);
3198
3199 if (place == NULL)
3200 high_expr = *ep;
3201 else
3202 {
3203 high_expr.X_op = O_constant;
3204 high_expr.X_add_number = ep->X_add_number;
3205 }
3206
3207 if (high_expr.X_op == O_constant)
3208 {
3209 /* we can compute the instruction now without a relocation entry */
e7d556df
TS
3210 high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3211 >> 16) & 0xffff;
f6688943 3212 *r = BFD_RELOC_UNUSED;
252b5132 3213 }
78e1bb40 3214 else
252b5132
RH
3215 {
3216 assert (ep->X_op == O_symbol);
3217 /* _gp_disp is a special case, used from s_cpload. */
3218 assert (mips_pic == NO_PIC
78e1bb40
AO
3219 || (! HAVE_NEWABI
3220 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0));
f6688943 3221 *r = BFD_RELOC_HI16_S;
252b5132
RH
3222 }
3223
3224 /*
3225 * If the macro is about to expand into a second instruction,
3226 * print a warning if needed. We need to pass ip as a parameter
3227 * to generate a better warning message here...
3228 */
3229 if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
3230 as_warn (_("Macro instruction expanded into multiple instructions"));
3231
3232 if (place == NULL)
f9419b05 3233 ++*counter; /* bump instruction counter */
252b5132
RH
3234
3235 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3236 assert (insn.insn_mo);
3237 assert (strcmp (name, insn.insn_mo->name) == 0);
3238 assert (strcmp (fmt, insn.insn_mo->args) == 0);
3239
3240 insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
f6688943 3241 if (*r == BFD_RELOC_UNUSED)
252b5132
RH
3242 {
3243 insn.insn_opcode |= high_expr.X_add_number;
5e0116d5 3244 append_insn (place, &insn, NULL, r);
252b5132
RH
3245 }
3246 else
5e0116d5 3247 append_insn (place, &insn, &high_expr, r);
252b5132
RH
3248}
3249
885add95
CD
3250/* Generate a sequence of instructions to do a load or store from a constant
3251 offset off of a base register (breg) into/from a target register (treg),
3252 using AT if necessary. */
3253static void
17a2f251 3254macro_build_ldst_constoffset (char *place, int *counter, expressionS *ep,
256ab948 3255 const char *op, int treg, int breg, int dbl)
885add95
CD
3256{
3257 assert (ep->X_op == O_constant);
3258
256ab948 3259 /* Sign-extending 32-bit constants makes their handling easier. */
d17e7bce
TS
3260 if (! dbl && ! ((ep->X_add_number & ~((bfd_vma) 0x7fffffff))
3261 == ~((bfd_vma) 0x7fffffff)))
ae826530 3262 {
d17e7bce 3263 if (ep->X_add_number & ~((bfd_vma) 0xffffffff))
1b8e29e5 3264 as_bad (_("constant too large"));
ae826530 3265
1b8e29e5
TS
3266 ep->X_add_number = (((ep->X_add_number & 0xffffffff) ^ 0x80000000)
3267 - 0x80000000);
ae826530 3268 }
256ab948 3269
67c1ffbe 3270 /* Right now, this routine can only handle signed 32-bit constants. */
ecd13cd3 3271 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
885add95
CD
3272 as_warn (_("operand overflow"));
3273
3274 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
3275 {
3276 /* Signed 16-bit offset will fit in the op. Easy! */
17a2f251
TS
3277 macro_build (place, counter, ep, op, "t,o(b)", treg, BFD_RELOC_LO16,
3278 breg);
885add95
CD
3279 }
3280 else
3281 {
3282 /* 32-bit offset, need multiple instructions and AT, like:
3283 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
3284 addu $tempreg,$tempreg,$breg
3285 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
3286 to handle the complete offset. */
3287 macro_build_lui (place, counter, ep, AT);
3288 if (place != NULL)
3289 place += 4;
17a2f251
TS
3290 macro_build (place, counter, NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT,
3291 breg);
885add95
CD
3292 if (place != NULL)
3293 place += 4;
17a2f251
TS
3294 macro_build (place, counter, ep, op, "t,o(b)", treg, BFD_RELOC_LO16,
3295 AT);
885add95
CD
3296
3297 if (mips_opts.noat)
3298 as_warn (_("Macro used $at after \".set noat\""));
3299 }
3300}
3301
252b5132
RH
3302/* set_at()
3303 * Generates code to set the $at register to true (one)
3304 * if reg is less than the immediate expression.
3305 */
3306static void
17a2f251 3307set_at (int *counter, int reg, int unsignedp)
252b5132
RH
3308{
3309 if (imm_expr.X_op == O_constant
3310 && imm_expr.X_add_number >= -0x8000
3311 && imm_expr.X_add_number < 0x8000)
17a2f251
TS
3312 macro_build (NULL, counter, &imm_expr, unsignedp ? "sltiu" : "slti",
3313 "t,r,j", AT, reg, BFD_RELOC_LO16);
252b5132
RH
3314 else
3315 {
4d34fb5f 3316 load_register (counter, AT, &imm_expr, HAVE_64BIT_GPRS);
17a2f251 3317 macro_build (NULL, counter, NULL, unsignedp ? "sltu" : "slt",
252b5132
RH
3318 "d,v,t", AT, reg, AT);
3319 }
3320}
3321
13757d0c
TS
3322static void
3323normalize_constant_expr (expressionS *ex)
3324{
3325 if (ex->X_op == O_constant && HAVE_32BIT_GPRS)
3326 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
3327 - 0x80000000);
3328}
3329
252b5132
RH
3330/* Warn if an expression is not a constant. */
3331
3332static void
17a2f251 3333check_absolute_expr (struct mips_cl_insn *ip, expressionS *ex)
252b5132
RH
3334{
3335 if (ex->X_op == O_big)
3336 as_bad (_("unsupported large constant"));
3337 else if (ex->X_op != O_constant)
3338 as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
13757d0c
TS
3339
3340 normalize_constant_expr (ex);
252b5132
RH
3341}
3342
3343/* Count the leading zeroes by performing a binary chop. This is a
3344 bulky bit of source, but performance is a LOT better for the
3345 majority of values than a simple loop to count the bits:
3346 for (lcnt = 0; (lcnt < 32); lcnt++)
3347 if ((v) & (1 << (31 - lcnt)))
3348 break;
3349 However it is not code size friendly, and the gain will drop a bit
3350 on certain cached systems.
3351*/
3352#define COUNT_TOP_ZEROES(v) \
3353 (((v) & ~0xffff) == 0 \
3354 ? ((v) & ~0xff) == 0 \
3355 ? ((v) & ~0xf) == 0 \
3356 ? ((v) & ~0x3) == 0 \
3357 ? ((v) & ~0x1) == 0 \
3358 ? !(v) \
3359 ? 32 \
3360 : 31 \
3361 : 30 \
3362 : ((v) & ~0x7) == 0 \
3363 ? 29 \
3364 : 28 \
3365 : ((v) & ~0x3f) == 0 \
3366 ? ((v) & ~0x1f) == 0 \
3367 ? 27 \
3368 : 26 \
3369 : ((v) & ~0x7f) == 0 \
3370 ? 25 \
3371 : 24 \
3372 : ((v) & ~0xfff) == 0 \
3373 ? ((v) & ~0x3ff) == 0 \
3374 ? ((v) & ~0x1ff) == 0 \
3375 ? 23 \
3376 : 22 \
3377 : ((v) & ~0x7ff) == 0 \
3378 ? 21 \
3379 : 20 \
3380 : ((v) & ~0x3fff) == 0 \
3381 ? ((v) & ~0x1fff) == 0 \
3382 ? 19 \
3383 : 18 \
3384 : ((v) & ~0x7fff) == 0 \
3385 ? 17 \
3386 : 16 \
3387 : ((v) & ~0xffffff) == 0 \
3388 ? ((v) & ~0xfffff) == 0 \
3389 ? ((v) & ~0x3ffff) == 0 \
3390 ? ((v) & ~0x1ffff) == 0 \
3391 ? 15 \
3392 : 14 \
3393 : ((v) & ~0x7ffff) == 0 \
3394 ? 13 \
3395 : 12 \
3396 : ((v) & ~0x3fffff) == 0 \
3397 ? ((v) & ~0x1fffff) == 0 \
3398 ? 11 \
3399 : 10 \
3400 : ((v) & ~0x7fffff) == 0 \
3401 ? 9 \
3402 : 8 \
3403 : ((v) & ~0xfffffff) == 0 \
3404 ? ((v) & ~0x3ffffff) == 0 \
3405 ? ((v) & ~0x1ffffff) == 0 \
3406 ? 7 \
3407 : 6 \
3408 : ((v) & ~0x7ffffff) == 0 \
3409 ? 5 \
3410 : 4 \
3411 : ((v) & ~0x3fffffff) == 0 \
3412 ? ((v) & ~0x1fffffff) == 0 \
3413 ? 3 \
3414 : 2 \
3415 : ((v) & ~0x7fffffff) == 0 \
3416 ? 1 \
3417 : 0)
3418
3419/* load_register()
67c1ffbe 3420 * This routine generates the least number of instructions necessary to load
252b5132
RH
3421 * an absolute expression value into a register.
3422 */
3423static void
17a2f251 3424load_register (int *counter, int reg, expressionS *ep, int dbl)
252b5132
RH
3425{
3426 int freg;
3427 expressionS hi32, lo32;
3428
3429 if (ep->X_op != O_big)
3430 {
3431 assert (ep->X_op == O_constant);
256ab948
TS
3432
3433 /* Sign-extending 32-bit constants makes their handling easier. */
d17e7bce
TS
3434 if (! dbl && ! ((ep->X_add_number & ~((bfd_vma) 0x7fffffff))
3435 == ~((bfd_vma) 0x7fffffff)))
ae826530 3436 {
d17e7bce 3437 if (ep->X_add_number & ~((bfd_vma) 0xffffffff))
1b8e29e5 3438 as_bad (_("constant too large"));
ae826530 3439
1b8e29e5
TS
3440 ep->X_add_number = (((ep->X_add_number & 0xffffffff) ^ 0x80000000)
3441 - 0x80000000);
ae826530 3442 }
256ab948
TS
3443
3444 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
252b5132
RH
3445 {
3446 /* We can handle 16 bit signed values with an addiu to
3447 $zero. No need to ever use daddiu here, since $zero and
3448 the result are always correct in 32 bit mode. */
17a2f251
TS
3449 macro_build (NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3450 BFD_RELOC_LO16);
252b5132
RH
3451 return;
3452 }
3453 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3454 {
3455 /* We can handle 16 bit unsigned values with an ori to
3456 $zero. */
17a2f251
TS
3457 macro_build (NULL, counter, ep, "ori", "t,r,i", reg, 0,
3458 BFD_RELOC_LO16);
252b5132
RH
3459 return;
3460 }
256ab948 3461 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
252b5132
RH
3462 {
3463 /* 32 bit values require an lui. */
17a2f251 3464 macro_build (NULL, counter, ep, "lui", "t,u", reg, BFD_RELOC_HI16);
252b5132 3465 if ((ep->X_add_number & 0xffff) != 0)
17a2f251
TS
3466 macro_build (NULL, counter, ep, "ori", "t,r,i", reg, reg,
3467 BFD_RELOC_LO16);
252b5132
RH
3468 return;
3469 }
3470 }
3471
3472 /* The value is larger than 32 bits. */
3473
ca4e0257 3474 if (HAVE_32BIT_GPRS)
252b5132 3475 {
956cd1d6
TS
3476 as_bad (_("Number (0x%lx) larger than 32 bits"),
3477 (unsigned long) ep->X_add_number);
17a2f251
TS
3478 macro_build (NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3479 BFD_RELOC_LO16);
252b5132
RH
3480 return;
3481 }
3482
3483 if (ep->X_op != O_big)
3484 {
3485 hi32 = *ep;
3486 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3487 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3488 hi32.X_add_number &= 0xffffffff;
3489 lo32 = *ep;
3490 lo32.X_add_number &= 0xffffffff;
3491 }
3492 else
3493 {
3494 assert (ep->X_add_number > 2);
3495 if (ep->X_add_number == 3)
3496 generic_bignum[3] = 0;
3497 else if (ep->X_add_number > 4)
3498 as_bad (_("Number larger than 64 bits"));
3499 lo32.X_op = O_constant;
3500 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3501 hi32.X_op = O_constant;
3502 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3503 }
3504
3505 if (hi32.X_add_number == 0)
3506 freg = 0;
3507 else
3508 {
3509 int shift, bit;
3510 unsigned long hi, lo;
3511
956cd1d6 3512 if (hi32.X_add_number == (offsetT) 0xffffffff)
beae10d5
KH
3513 {
3514 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3515 {
17a2f251
TS
3516 macro_build (NULL, counter, &lo32, "addiu", "t,r,j", reg, 0,
3517 BFD_RELOC_LO16);
beae10d5
KH
3518 return;
3519 }
3520 if (lo32.X_add_number & 0x80000000)
3521 {
17a2f251
TS
3522 macro_build (NULL, counter, &lo32, "lui", "t,u", reg,
3523 BFD_RELOC_HI16);
252b5132 3524 if (lo32.X_add_number & 0xffff)
17a2f251
TS
3525 macro_build (NULL, counter, &lo32, "ori", "t,r,i", reg, reg,
3526 BFD_RELOC_LO16);
beae10d5
KH
3527 return;
3528 }
3529 }
252b5132
RH
3530
3531 /* Check for 16bit shifted constant. We know that hi32 is
3532 non-zero, so start the mask on the first bit of the hi32
3533 value. */
3534 shift = 17;
3535 do
beae10d5
KH
3536 {
3537 unsigned long himask, lomask;
3538
3539 if (shift < 32)
3540 {
3541 himask = 0xffff >> (32 - shift);
3542 lomask = (0xffff << shift) & 0xffffffff;
3543 }
3544 else
3545 {
3546 himask = 0xffff << (shift - 32);
3547 lomask = 0;
3548 }
3549 if ((hi32.X_add_number & ~(offsetT) himask) == 0
3550 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3551 {
3552 expressionS tmp;
3553
3554 tmp.X_op = O_constant;
3555 if (shift < 32)
3556 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3557 | (lo32.X_add_number >> shift));
3558 else
3559 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
17a2f251
TS
3560 macro_build (NULL, counter, &tmp, "ori", "t,r,i", reg, 0,
3561 BFD_RELOC_LO16);
3562 macro_build (NULL, counter, NULL,
beae10d5
KH
3563 (shift >= 32) ? "dsll32" : "dsll",
3564 "d,w,<", reg, reg,
3565 (shift >= 32) ? shift - 32 : shift);
3566 return;
3567 }
f9419b05 3568 ++shift;
beae10d5
KH
3569 }
3570 while (shift <= (64 - 16));
252b5132
RH
3571
3572 /* Find the bit number of the lowest one bit, and store the
3573 shifted value in hi/lo. */
3574 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3575 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3576 if (lo != 0)
3577 {
3578 bit = 0;
3579 while ((lo & 1) == 0)
3580 {
3581 lo >>= 1;
3582 ++bit;
3583 }
3584 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3585 hi >>= bit;
3586 }
3587 else
3588 {
3589 bit = 32;
3590 while ((hi & 1) == 0)
3591 {
3592 hi >>= 1;
3593 ++bit;
3594 }
3595 lo = hi;
3596 hi = 0;
3597 }
3598
3599 /* Optimize if the shifted value is a (power of 2) - 1. */
3600 if ((hi == 0 && ((lo + 1) & lo) == 0)
3601 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
beae10d5
KH
3602 {
3603 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
252b5132 3604 if (shift != 0)
beae10d5 3605 {
252b5132
RH
3606 expressionS tmp;
3607
3608 /* This instruction will set the register to be all
3609 ones. */
beae10d5
KH
3610 tmp.X_op = O_constant;
3611 tmp.X_add_number = (offsetT) -1;
17a2f251
TS
3612 macro_build (NULL, counter, &tmp, "addiu", "t,r,j", reg, 0,
3613 BFD_RELOC_LO16);
beae10d5
KH
3614 if (bit != 0)
3615 {
3616 bit += shift;
17a2f251 3617 macro_build (NULL, counter, NULL,
beae10d5
KH
3618 (bit >= 32) ? "dsll32" : "dsll",
3619 "d,w,<", reg, reg,
3620 (bit >= 32) ? bit - 32 : bit);
3621 }
17a2f251 3622 macro_build (NULL, counter, NULL,
252b5132 3623 (shift >= 32) ? "dsrl32" : "dsrl",
beae10d5 3624 "d,w,<", reg, reg,
252b5132 3625 (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
3626 return;
3627 }
3628 }
252b5132
RH
3629
3630 /* Sign extend hi32 before calling load_register, because we can
3631 generally get better code when we load a sign extended value. */
3632 if ((hi32.X_add_number & 0x80000000) != 0)
beae10d5 3633 hi32.X_add_number |= ~(offsetT) 0xffffffff;
252b5132
RH
3634 load_register (counter, reg, &hi32, 0);
3635 freg = reg;
3636 }
3637 if ((lo32.X_add_number & 0xffff0000) == 0)
3638 {
3639 if (freg != 0)
3640 {
17a2f251 3641 macro_build (NULL, counter, NULL, "dsll32", "d,w,<", reg, freg, 0);
252b5132
RH
3642 freg = reg;
3643 }
3644 }
3645 else
3646 {
3647 expressionS mid16;
3648
956cd1d6 3649 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
beae10d5 3650 {
17a2f251
TS
3651 macro_build (NULL, counter, &lo32, "lui", "t,u", reg,
3652 BFD_RELOC_HI16);
3653 macro_build (NULL, counter, NULL, "dsrl32", "d,w,<", reg, reg, 0);
beae10d5
KH
3654 return;
3655 }
252b5132
RH
3656
3657 if (freg != 0)
3658 {
17a2f251 3659 macro_build (NULL, counter, NULL, "dsll", "d,w,<", reg, freg, 16);
252b5132
RH
3660 freg = reg;
3661 }
3662 mid16 = lo32;
3663 mid16.X_add_number >>= 16;
17a2f251
TS
3664 macro_build (NULL, counter, &mid16, "ori", "t,r,i", reg, freg,
3665 BFD_RELOC_LO16);
3666 macro_build (NULL, counter, NULL, "dsll", "d,w,<", reg, reg, 16);
252b5132
RH
3667 freg = reg;
3668 }
3669 if ((lo32.X_add_number & 0xffff) != 0)
17a2f251
TS
3670 macro_build (NULL, counter, &lo32, "ori", "t,r,i", reg, freg,
3671 BFD_RELOC_LO16);
252b5132
RH
3672}
3673
3674/* Load an address into a register. */
3675
3676static void
17a2f251 3677load_address (int *counter, int reg, expressionS *ep, int *used_at)
252b5132 3678{
f9419b05 3679 char *p = NULL;
252b5132
RH
3680
3681 if (ep->X_op != O_constant
3682 && ep->X_op != O_symbol)
3683 {
3684 as_bad (_("expression too complex"));
3685 ep->X_op = O_constant;
3686 }
3687
3688 if (ep->X_op == O_constant)
3689 {
c9914766 3690 load_register (counter, reg, ep, HAVE_64BIT_ADDRESSES);
252b5132
RH
3691 return;
3692 }
3693
3694 if (mips_pic == NO_PIC)
3695 {
3696 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 3697 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
3698 Otherwise we want
3699 lui $reg,<sym> (BFD_RELOC_HI16_S)
3700 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
d6bc6245 3701 If we have an addend, we always use the latter form.
76b3015f 3702
d6bc6245
TS
3703 With 64bit address space and a usable $at we want
3704 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3705 lui $at,<sym> (BFD_RELOC_HI16_S)
3706 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3707 daddiu $at,<sym> (BFD_RELOC_LO16)
3708 dsll32 $reg,0
3a482fd5 3709 daddu $reg,$reg,$at
76b3015f 3710
c03099e6 3711 If $at is already in use, we use a path which is suboptimal
d6bc6245
TS
3712 on superscalar processors.
3713 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3714 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3715 dsll $reg,16
3716 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
3717 dsll $reg,16
3718 daddiu $reg,<sym> (BFD_RELOC_LO16)
3719 */
c9914766 3720 if (HAVE_64BIT_ADDRESSES)
d6bc6245 3721 {
d6bc6245
TS
3722 /* We don't do GP optimization for now because RELAX_ENCODE can't
3723 hold the data for such large chunks. */
3724
460597ba 3725 if (*used_at == 0 && ! mips_opts.noat)
d6bc6245
TS
3726 {
3727 macro_build (p, counter, ep, "lui", "t,u",
17a2f251 3728 reg, BFD_RELOC_MIPS_HIGHEST);
d6bc6245 3729 macro_build (p, counter, ep, "lui", "t,u",
17a2f251 3730 AT, BFD_RELOC_HI16_S);
d6bc6245 3731 macro_build (p, counter, ep, "daddiu", "t,r,j",
17a2f251 3732 reg, reg, BFD_RELOC_MIPS_HIGHER);
d6bc6245 3733 macro_build (p, counter, ep, "daddiu", "t,r,j",
17a2f251
TS
3734 AT, AT, BFD_RELOC_LO16);
3735 macro_build (p, counter, NULL, "dsll32", "d,w,<", reg, reg, 0);
3736 macro_build (p, counter, NULL, "daddu", "d,v,t", reg, reg, AT);
d6bc6245
TS
3737 *used_at = 1;
3738 }
3739 else
3740 {
3741 macro_build (p, counter, ep, "lui", "t,u",
17a2f251 3742 reg, BFD_RELOC_MIPS_HIGHEST);
d6bc6245 3743 macro_build (p, counter, ep, "daddiu", "t,r,j",
17a2f251
TS
3744 reg, reg, BFD_RELOC_MIPS_HIGHER);
3745 macro_build (p, counter, NULL, "dsll", "d,w,<", reg, reg, 16);
d6bc6245 3746 macro_build (p, counter, ep, "daddiu", "t,r,j",
17a2f251
TS
3747 reg, reg, BFD_RELOC_HI16_S);
3748 macro_build (p, counter, NULL, "dsll", "d,w,<", reg, reg, 16);
d6bc6245 3749 macro_build (p, counter, ep, "daddiu", "t,r,j",
17a2f251 3750 reg, reg, BFD_RELOC_LO16);
d6bc6245
TS
3751 }
3752 }
252b5132
RH
3753 else
3754 {
d6bc6245
TS
3755 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3756 && ! nopic_need_relax (ep->X_add_symbol, 1))
3757 {
3758 frag_grow (20);
17a2f251
TS
3759 macro_build (NULL, counter, ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
3760 mips_gp_register, BFD_RELOC_GPREL16);
d6bc6245
TS
3761 p = frag_var (rs_machine_dependent, 8, 0,
3762 RELAX_ENCODE (4, 8, 0, 4, 0,
3763 mips_opts.warn_about_macros),
956cd1d6 3764 ep->X_add_symbol, 0, NULL);
d6bc6245
TS
3765 }
3766 macro_build_lui (p, counter, ep, reg);
3767 if (p != NULL)
3768 p += 4;
17a2f251
TS
3769 macro_build (p, counter, ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
3770 BFD_RELOC_LO16);
d6bc6245 3771 }
252b5132
RH
3772 }
3773 else if (mips_pic == SVR4_PIC && ! mips_big_got)
3774 {
3775 expressionS ex;
3776
3777 /* If this is a reference to an external symbol, we want
3778 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3779 Otherwise we want
3780 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3781 nop
3782 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
f5040a92
AO
3783 If there is a constant, it must be added in after.
3784
ed6fb7bd 3785 If we have NewABI, we want
f5040a92
AO
3786 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
3787 unless we're referencing a global symbol with a non-zero
3788 offset, in which case cst must be added separately. */
ed6fb7bd
SC
3789 if (HAVE_NEWABI)
3790 {
f5040a92 3791 frag_grow (12);
684022ea 3792
f5040a92
AO
3793 if (ep->X_add_number)
3794 {
3795 frag_now->tc_frag_data.tc_fr_offset =
3796 ex.X_add_number = ep->X_add_number;
3797 ep->X_add_number = 0;
17a2f251
TS
3798 macro_build (NULL, counter, ep, ADDRESS_LOAD_INSN, "t,o(b)",
3799 reg, BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
3800 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3801 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3802 ex.X_op = O_constant;
17a2f251
TS
3803 macro_build (NULL, counter, &ex, ADDRESS_ADDI_INSN, "t,r,j",
3804 reg, reg, BFD_RELOC_LO16);
f5040a92
AO
3805 p = frag_var (rs_machine_dependent, 8, 0,
3806 RELAX_ENCODE (8, 4, 0, 0, 0,
3807 mips_opts.warn_about_macros),
17a2f251 3808 ep->X_add_symbol, 0, NULL);
f5040a92
AO
3809 ep->X_add_number = ex.X_add_number;
3810 }
3811
f899b4b8 3812 macro_build (p, counter, ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 3813 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
3814
3815 if (! p)
3816 {
3817 /* To avoid confusion in tc_gen_reloc, we must ensure
3818 that this does not become a variant frag. */
3819 frag_wane (frag_now);
3820 frag_new (0);
3821 }
ed6fb7bd
SC
3822 }
3823 else
3824 {
f5040a92
AO
3825 ex.X_add_number = ep->X_add_number;
3826 ep->X_add_number = 0;
3827 frag_grow (20);
17a2f251
TS
3828 macro_build (NULL, counter, ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3829 BFD_RELOC_MIPS_GOT16,
f899b4b8 3830 mips_gp_register);
17a2f251 3831 macro_build (NULL, counter, NULL, "nop", "");
ed6fb7bd
SC
3832 p = frag_var (rs_machine_dependent, 4, 0,
3833 RELAX_ENCODE (0, 4, -8, 0, 0, mips_opts.warn_about_macros),
17a2f251
TS
3834 ep->X_add_symbol, 0, NULL);
3835 macro_build (p, counter, ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
3836 BFD_RELOC_LO16);
ed6fb7bd 3837
f5040a92
AO
3838 if (ex.X_add_number != 0)
3839 {
3840 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3841 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3842 ex.X_op = O_constant;
17a2f251
TS
3843 macro_build (NULL, counter, &ex, ADDRESS_ADDI_INSN, "t,r,j",
3844 reg, reg, BFD_RELOC_LO16);
f5040a92 3845 }
252b5132
RH
3846 }
3847 }
3848 else if (mips_pic == SVR4_PIC)
3849 {
3850 expressionS ex;
3851 int off;
3852
3853 /* This is the large GOT case. If this is a reference to an
3854 external symbol, we want
3855 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
3856 addu $reg,$reg,$gp
3857 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
f5040a92
AO
3858
3859 Otherwise, for a reference to a local symbol in old ABI, we want
252b5132
RH
3860 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3861 nop
3862 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
684022ea 3863 If there is a constant, it must be added in after.
f5040a92
AO
3864
3865 In the NewABI, for local symbols, with or without offsets, we want:
438c16b8
TS
3866 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
3867 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
f5040a92 3868 */
438c16b8
TS
3869 if (HAVE_NEWABI)
3870 {
f5040a92
AO
3871 frag_grow (24);
3872
3873 frag_now->tc_frag_data.tc_fr_offset =
3874 ex.X_add_number = ep->X_add_number;
3875 ep->X_add_number = 0;
17a2f251
TS
3876 macro_build (NULL, counter, ep, "lui", "t,u", reg,
3877 BFD_RELOC_MIPS_GOT_HI16);
3878 macro_build (NULL, counter, NULL, ADDRESS_ADD_INSN, "d,v,t", reg,
3879 reg, mips_gp_register);
3880 macro_build (NULL, counter, ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3881 BFD_RELOC_MIPS_GOT_LO16, reg);
f5040a92
AO
3882 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3883 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3884 else if (ex.X_add_number)
3885 {
3886 ex.X_op = O_constant;
17a2f251
TS
3887 macro_build (NULL, counter, &ex, ADDRESS_ADDI_INSN, "t,r,j",
3888 reg, reg, BFD_RELOC_LO16);
f5040a92
AO
3889 }
3890
3891 ep->X_add_number = ex.X_add_number;
3892 p = frag_var (rs_machine_dependent, 8, 0,
3893 RELAX_ENCODE (ex.X_add_number ? 16 : 12, 8, 0, 4, 0,
3894 mips_opts.warn_about_macros),
17a2f251 3895 ep->X_add_symbol, 0, NULL);
f899b4b8 3896 macro_build (p, counter, ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251
TS
3897 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
3898 macro_build (p + 4, counter, ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
3899 reg, BFD_RELOC_MIPS_GOT_OFST);
438c16b8 3900 }
252b5132 3901 else
438c16b8 3902 {
f5040a92
AO
3903 ex.X_add_number = ep->X_add_number;
3904 ep->X_add_number = 0;
438c16b8
TS
3905 if (reg_needs_delay (mips_gp_register))
3906 off = 4;
3907 else
3908 off = 0;
3909 frag_grow (32);
17a2f251
TS
3910 macro_build (NULL, counter, ep, "lui", "t,u", reg,
3911 BFD_RELOC_MIPS_GOT_HI16);
3912 macro_build (NULL, counter, NULL, ADDRESS_ADD_INSN, "d,v,t", reg,
3913 reg, mips_gp_register);
3914 macro_build (NULL, counter, ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3915 BFD_RELOC_MIPS_GOT_LO16, reg);
438c16b8
TS
3916 p = frag_var (rs_machine_dependent, 12 + off, 0,
3917 RELAX_ENCODE (12, 12 + off, off, 8 + off, 0,
3918 mips_opts.warn_about_macros),
3919 ep->X_add_symbol, 0, NULL);
3920 if (off > 0)
3921 {
3922 /* We need a nop before loading from $gp. This special
3923 check is required because the lui which starts the main
3924 instruction stream does not refer to $gp, and so will not
3925 insert the nop which may be required. */
17a2f251 3926 macro_build (p, counter, NULL, "nop", "");
438c16b8
TS
3927 p += 4;
3928 }
f899b4b8 3929 macro_build (p, counter, ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 3930 BFD_RELOC_MIPS_GOT16, mips_gp_register);
438c16b8 3931 p += 4;
17a2f251 3932 macro_build (p, counter, NULL, "nop", "");
252b5132 3933 p += 4;
17a2f251
TS
3934 macro_build (p, counter, ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
3935 BFD_RELOC_LO16);
438c16b8 3936
f5040a92
AO
3937 if (ex.X_add_number != 0)
3938 {
3939 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3940 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3941 ex.X_op = O_constant;
17a2f251
TS
3942 macro_build (NULL, counter, &ex, ADDRESS_ADDI_INSN, "t,r,j",
3943 reg, reg, BFD_RELOC_LO16);
f5040a92 3944 }
252b5132
RH
3945 }
3946 }
3947 else if (mips_pic == EMBEDDED_PIC)
3948 {
3949 /* We always do
cdf6fd85 3950 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
c9914766 3951 */
17a2f251
TS
3952 macro_build (NULL, counter, ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
3953 mips_gp_register, BFD_RELOC_GPREL16);
252b5132
RH
3954 }
3955 else
3956 abort ();
3957}
3958
ea1fb5dc
RS
3959/* Move the contents of register SOURCE into register DEST. */
3960
3961static void
17a2f251 3962move_register (int *counter, int dest, int source)
ea1fb5dc 3963{
17a2f251 3964 macro_build (NULL, counter, NULL, HAVE_32BIT_GPRS ? "addu" : "daddu",
ea1fb5dc
RS
3965 "d,v,t", dest, source, 0);
3966}
3967
252b5132
RH
3968/*
3969 * Build macros
3970 * This routine implements the seemingly endless macro or synthesized
3971 * instructions and addressing modes in the mips assembly language. Many
3972 * of these macros are simple and are similar to each other. These could
67c1ffbe 3973 * probably be handled by some kind of table or grammar approach instead of
252b5132
RH
3974 * this verbose method. Others are not simple macros but are more like
3975 * optimizing code generation.
3976 * One interesting optimization is when several store macros appear
67c1ffbe 3977 * consecutively that would load AT with the upper half of the same address.
252b5132
RH
3978 * The ensuing load upper instructions are ommited. This implies some kind
3979 * of global optimization. We currently only optimize within a single macro.
3980 * For many of the load and store macros if the address is specified as a
3981 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
3982 * first load register 'at' with zero and use it as the base register. The
3983 * mips assembler simply uses register $zero. Just one tiny optimization
3984 * we're missing.
3985 */
3986static void
17a2f251 3987macro (struct mips_cl_insn *ip)
252b5132
RH
3988{
3989 register int treg, sreg, dreg, breg;
3990 int tempreg;
3991 int mask;
3992 int icnt = 0;
43841e91 3993 int used_at = 0;
252b5132
RH
3994 expressionS expr1;
3995 const char *s;
3996 const char *s2;
3997 const char *fmt;
3998 int likely = 0;
3999 int dbl = 0;
4000 int coproc = 0;
4001 int lr = 0;
4002 int imm = 0;
1abe91b1 4003 int call = 0;
252b5132
RH
4004 offsetT maxnum;
4005 int off;
4006 bfd_reloc_code_real_type r;
252b5132
RH
4007 int hold_mips_optimize;
4008
4009 assert (! mips_opts.mips16);
4010
4011 treg = (ip->insn_opcode >> 16) & 0x1f;
4012 dreg = (ip->insn_opcode >> 11) & 0x1f;
4013 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
4014 mask = ip->insn_mo->mask;
4015
4016 expr1.X_op = O_constant;
4017 expr1.X_op_symbol = NULL;
4018 expr1.X_add_symbol = NULL;
4019 expr1.X_add_number = 1;
4020
67c1ffbe 4021 /* Unmatched fixups should not be put in the same frag as a relaxable
5919d012
RS
4022 macro. For example, suppose we have:
4023
4024 lui $4,%hi(l1) # 1
4025 la $5,l2 # 2
4026 addiu $4,$4,%lo(l1) # 3
4027
4028 If instructions 1 and 2 were put in the same frag, md_frob_file would
4029 move the fixup for #1 after the fixups for the "unrelaxed" version of
4030 #2. This would confuse tc_gen_reloc, which expects the relocations
4031 for #2 to be the last for that frag.
4032
64bdfcaf
RS
4033 Also, if tc_gen_reloc sees certain relocations in a variant frag,
4034 it assumes that they belong to a relaxable macro. We mustn't put
4035 other uses of such relocations into a variant frag.
4036
4037 To avoid both problems, finish the current frag it contains a
4038 %reloc() operator. The macro then goes into a new frag. */
4039 if (prev_reloc_op_frag == frag_now)
5919d012
RS
4040 {
4041 frag_wane (frag_now);
4042 frag_new (0);
4043 }
4044
252b5132
RH
4045 switch (mask)
4046 {
4047 case M_DABS:
4048 dbl = 1;
4049 case M_ABS:
4050 /* bgez $a0,.+12
4051 move v0,$a0
4052 sub v0,$zero,$a0
4053 */
4054
b34976b6 4055 mips_emit_delays (TRUE);
252b5132
RH
4056 ++mips_opts.noreorder;
4057 mips_any_noreorder = 1;
4058
4059 expr1.X_add_number = 8;
17a2f251 4060 macro_build (NULL, &icnt, &expr1, "bgez", "s,p", sreg);
252b5132 4061 if (dreg == sreg)
17a2f251 4062 macro_build (NULL, &icnt, NULL, "nop", "", 0);
252b5132 4063 else
ea1fb5dc 4064 move_register (&icnt, dreg, sreg);
17a2f251
TS
4065 macro_build (NULL, &icnt, NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, 0,
4066 sreg);
252b5132
RH
4067
4068 --mips_opts.noreorder;
4069 return;
4070
4071 case M_ADD_I:
4072 s = "addi";
4073 s2 = "add";
4074 goto do_addi;
4075 case M_ADDU_I:
4076 s = "addiu";
4077 s2 = "addu";
4078 goto do_addi;
4079 case M_DADD_I:
4080 dbl = 1;
4081 s = "daddi";
4082 s2 = "dadd";
4083 goto do_addi;
4084 case M_DADDU_I:
4085 dbl = 1;
4086 s = "daddiu";
4087 s2 = "daddu";
4088 do_addi:
4089 if (imm_expr.X_op == O_constant
4090 && imm_expr.X_add_number >= -0x8000
4091 && imm_expr.X_add_number < 0x8000)
4092 {
17a2f251
TS
4093 macro_build (NULL, &icnt, &imm_expr, s, "t,r,j", treg, sreg,
4094 BFD_RELOC_LO16);
252b5132
RH
4095 return;
4096 }
4097 load_register (&icnt, AT, &imm_expr, dbl);
17a2f251 4098 macro_build (NULL, &icnt, NULL, s2, "d,v,t", treg, sreg, AT);
252b5132
RH
4099 break;
4100
4101 case M_AND_I:
4102 s = "andi";
4103 s2 = "and";
4104 goto do_bit;
4105 case M_OR_I:
4106 s = "ori";
4107 s2 = "or";
4108 goto do_bit;
4109 case M_NOR_I:
4110 s = "";
4111 s2 = "nor";
4112 goto do_bit;
4113 case M_XOR_I:
4114 s = "xori";
4115 s2 = "xor";
4116 do_bit:
4117 if (imm_expr.X_op == O_constant
4118 && imm_expr.X_add_number >= 0
4119 && imm_expr.X_add_number < 0x10000)
4120 {
4121 if (mask != M_NOR_I)
17a2f251
TS
4122 macro_build (NULL, &icnt, &imm_expr, s, "t,r,i", treg, sreg,
4123 BFD_RELOC_LO16);
252b5132
RH
4124 else
4125 {
17a2f251
TS
4126 macro_build (NULL, &icnt, &imm_expr, "ori", "t,r,i", treg, sreg,
4127 BFD_RELOC_LO16);
4128 macro_build (NULL, &icnt, NULL, "nor", "d,v,t", treg, treg, 0);
252b5132
RH
4129 }
4130 return;
4131 }
4132
d6bc6245 4133 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
17a2f251 4134 macro_build (NULL, &icnt, NULL, s2, "d,v,t", treg, sreg, AT);
252b5132
RH
4135 break;
4136
4137 case M_BEQ_I:
4138 s = "beq";
4139 goto beq_i;
4140 case M_BEQL_I:
4141 s = "beql";
4142 likely = 1;
4143 goto beq_i;
4144 case M_BNE_I:
4145 s = "bne";
4146 goto beq_i;
4147 case M_BNEL_I:
4148 s = "bnel";
4149 likely = 1;
4150 beq_i:
4151 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4152 {
17a2f251 4153 macro_build (NULL, &icnt, &offset_expr, s, "s,t,p", sreg, 0);
252b5132
RH
4154 return;
4155 }
4d34fb5f 4156 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
17a2f251 4157 macro_build (NULL, &icnt, &offset_expr, s, "s,t,p", sreg, AT);
252b5132
RH
4158 break;
4159
4160 case M_BGEL:
4161 likely = 1;
4162 case M_BGE:
4163 if (treg == 0)
4164 {
17a2f251
TS
4165 macro_build (NULL, &icnt, &offset_expr, likely ? "bgezl" : "bgez",
4166 "s,p", sreg);
252b5132
RH
4167 return;
4168 }
4169 if (sreg == 0)
4170 {
17a2f251
TS
4171 macro_build (NULL, &icnt, &offset_expr, likely ? "blezl" : "blez",
4172 "s,p", treg);
252b5132
RH
4173 return;
4174 }
17a2f251
TS
4175 macro_build (NULL, &icnt, NULL, "slt", "d,v,t", AT, sreg, treg);
4176 macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4177 "s,t,p", AT, 0);
252b5132
RH
4178 break;
4179
4180 case M_BGTL_I:
4181 likely = 1;
4182 case M_BGT_I:
4183 /* check for > max integer */
4184 maxnum = 0x7fffffff;
ca4e0257 4185 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
4186 {
4187 maxnum <<= 16;
4188 maxnum |= 0xffff;
4189 maxnum <<= 16;
4190 maxnum |= 0xffff;
4191 }
4192 if (imm_expr.X_op == O_constant
4193 && imm_expr.X_add_number >= maxnum
ca4e0257 4194 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
4195 {
4196 do_false:
4197 /* result is always false */
4198 if (! likely)
4199 {
39c0a331
L
4200 if (warn_nops)
4201 as_warn (_("Branch %s is always false (nop)"),
4202 ip->insn_mo->name);
17a2f251 4203 macro_build (NULL, &icnt, NULL, "nop", "", 0);
252b5132
RH
4204 }
4205 else
4206 {
39c0a331
L
4207 if (warn_nops)
4208 as_warn (_("Branch likely %s is always false"),
4209 ip->insn_mo->name);
17a2f251 4210 macro_build (NULL, &icnt, &offset_expr, "bnel", "s,t,p", 0, 0);
252b5132
RH
4211 }
4212 return;
4213 }
4214 if (imm_expr.X_op != O_constant)
4215 as_bad (_("Unsupported large constant"));
f9419b05 4216 ++imm_expr.X_add_number;
252b5132
RH
4217 /* FALLTHROUGH */
4218 case M_BGE_I:
4219 case M_BGEL_I:
4220 if (mask == M_BGEL_I)
4221 likely = 1;
4222 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4223 {
17a2f251
TS
4224 macro_build (NULL, &icnt, &offset_expr, likely ? "bgezl" : "bgez",
4225 "s,p", sreg);
252b5132
RH
4226 return;
4227 }
4228 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4229 {
17a2f251
TS
4230 macro_build (NULL, &icnt, &offset_expr, likely ? "bgtzl" : "bgtz",
4231 "s,p", sreg);
252b5132
RH
4232 return;
4233 }
4234 maxnum = 0x7fffffff;
ca4e0257 4235 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
4236 {
4237 maxnum <<= 16;
4238 maxnum |= 0xffff;
4239 maxnum <<= 16;
4240 maxnum |= 0xffff;
4241 }
4242 maxnum = - maxnum - 1;
4243 if (imm_expr.X_op == O_constant
4244 && imm_expr.X_add_number <= maxnum
ca4e0257 4245 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
4246 {
4247 do_true:
4248 /* result is always true */
4249 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
17a2f251 4250 macro_build (NULL, &icnt, &offset_expr, "b", "p");
252b5132
RH
4251 return;
4252 }
4253 set_at (&icnt, sreg, 0);
17a2f251
TS
4254 macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4255 "s,t,p", AT, 0);
252b5132
RH
4256 break;
4257
4258 case M_BGEUL:
4259 likely = 1;
4260 case M_BGEU:
4261 if (treg == 0)
4262 goto do_true;
4263 if (sreg == 0)
4264 {
17a2f251
TS
4265 macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4266 "s,t,p", 0, treg);
252b5132
RH
4267 return;
4268 }
17a2f251
TS
4269 macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", AT, sreg, treg);
4270 macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4271 "s,t,p", AT, 0);
252b5132
RH
4272 break;
4273
4274 case M_BGTUL_I:
4275 likely = 1;
4276 case M_BGTU_I:
4277 if (sreg == 0
ca4e0257 4278 || (HAVE_32BIT_GPRS
252b5132 4279 && imm_expr.X_op == O_constant
956cd1d6 4280 && imm_expr.X_add_number == (offsetT) 0xffffffff))
252b5132
RH
4281 goto do_false;
4282 if (imm_expr.X_op != O_constant)
4283 as_bad (_("Unsupported large constant"));
f9419b05 4284 ++imm_expr.X_add_number;
252b5132
RH
4285 /* FALLTHROUGH */
4286 case M_BGEU_I:
4287 case M_BGEUL_I:
4288 if (mask == M_BGEUL_I)
4289 likely = 1;
4290 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4291 goto do_true;
4292 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4293 {
17a2f251
TS
4294 macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4295 "s,t,p", sreg, 0);
252b5132
RH
4296 return;
4297 }
4298 set_at (&icnt, sreg, 1);
17a2f251
TS
4299 macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4300 "s,t,p", AT, 0);
252b5132
RH
4301 break;
4302
4303 case M_BGTL:
4304 likely = 1;
4305 case M_BGT:
4306 if (treg == 0)
4307 {
17a2f251
TS
4308 macro_build (NULL, &icnt, &offset_expr, likely ? "bgtzl" : "bgtz",
4309 "s,p", sreg);
252b5132
RH
4310 return;
4311 }
4312 if (sreg == 0)
4313 {
17a2f251
TS
4314 macro_build (NULL, &icnt, &offset_expr, likely ? "bltzl" : "bltz",
4315 "s,p", treg);
252b5132
RH
4316 return;
4317 }
17a2f251
TS
4318 macro_build (NULL, &icnt, NULL, "slt", "d,v,t", AT, treg, sreg);
4319 macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4320 "s,t,p", AT, 0);
252b5132
RH
4321 break;
4322
4323 case M_BGTUL:
4324 likely = 1;
4325 case M_BGTU:
4326 if (treg == 0)
4327 {
17a2f251
TS
4328 macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4329 "s,t,p", sreg, 0);
252b5132
RH
4330 return;
4331 }
4332 if (sreg == 0)
4333 goto do_false;
17a2f251
TS
4334 macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", AT, treg, sreg);
4335 macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4336 "s,t,p", AT, 0);
252b5132
RH
4337 break;
4338
4339 case M_BLEL:
4340 likely = 1;
4341 case M_BLE:
4342 if (treg == 0)
4343 {
17a2f251
TS
4344 macro_build (NULL, &icnt, &offset_expr, likely ? "blezl" : "blez",
4345 "s,p", sreg);
252b5132
RH
4346 return;
4347 }
4348 if (sreg == 0)
4349 {
17a2f251
TS
4350 macro_build (NULL, &icnt, &offset_expr, likely ? "bgezl" : "bgez",
4351 "s,p", treg);
252b5132
RH
4352 return;
4353 }
17a2f251
TS
4354 macro_build (NULL, &icnt, NULL, "slt", "d,v,t", AT, treg, sreg);
4355 macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4356 "s,t,p", AT, 0);
252b5132
RH
4357 break;
4358
4359 case M_BLEL_I:
4360 likely = 1;
4361 case M_BLE_I:
4362 maxnum = 0x7fffffff;
ca4e0257 4363 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
4364 {
4365 maxnum <<= 16;
4366 maxnum |= 0xffff;
4367 maxnum <<= 16;
4368 maxnum |= 0xffff;
4369 }
4370 if (imm_expr.X_op == O_constant
4371 && imm_expr.X_add_number >= maxnum
ca4e0257 4372 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
4373 goto do_true;
4374 if (imm_expr.X_op != O_constant)
4375 as_bad (_("Unsupported large constant"));
f9419b05 4376 ++imm_expr.X_add_number;
252b5132
RH
4377 /* FALLTHROUGH */
4378 case M_BLT_I:
4379 case M_BLTL_I:
4380 if (mask == M_BLTL_I)
4381 likely = 1;
4382 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4383 {
17a2f251
TS
4384 macro_build (NULL, &icnt, &offset_expr, likely ? "bltzl" : "bltz",
4385 "s,p", sreg);
252b5132
RH
4386 return;
4387 }
4388 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4389 {
17a2f251
TS
4390 macro_build (NULL, &icnt, &offset_expr, likely ? "blezl" : "blez",
4391 "s,p", sreg);
252b5132
RH
4392 return;
4393 }
4394 set_at (&icnt, sreg, 0);
17a2f251
TS
4395 macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4396 "s,t,p", AT, 0);
252b5132
RH
4397 break;
4398
4399 case M_BLEUL:
4400 likely = 1;
4401 case M_BLEU:
4402 if (treg == 0)
4403 {
17a2f251
TS
4404 macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4405 "s,t,p", sreg, 0);
252b5132
RH
4406 return;
4407 }
4408 if (sreg == 0)
4409 goto do_true;
17a2f251
TS
4410 macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", AT, treg, sreg);
4411 macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
4412 "s,t,p", AT, 0);
252b5132
RH
4413 break;
4414
4415 case M_BLEUL_I:
4416 likely = 1;
4417 case M_BLEU_I:
4418 if (sreg == 0
ca4e0257 4419 || (HAVE_32BIT_GPRS
252b5132 4420 && imm_expr.X_op == O_constant
956cd1d6 4421 && imm_expr.X_add_number == (offsetT) 0xffffffff))
252b5132
RH
4422 goto do_true;
4423 if (imm_expr.X_op != O_constant)
4424 as_bad (_("Unsupported large constant"));
f9419b05 4425 ++imm_expr.X_add_number;
252b5132
RH
4426 /* FALLTHROUGH */
4427 case M_BLTU_I:
4428 case M_BLTUL_I:
4429 if (mask == M_BLTUL_I)
4430 likely = 1;
4431 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4432 goto do_false;
4433 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4434 {
17a2f251 4435 macro_build (NULL, &icnt, &offset_expr, likely ? "beql" : "beq",
252b5132
RH
4436 "s,t,p", sreg, 0);
4437 return;
4438 }
4439 set_at (&icnt, sreg, 1);
17a2f251
TS
4440 macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4441 "s,t,p", AT, 0);
252b5132
RH
4442 break;
4443
4444 case M_BLTL:
4445 likely = 1;
4446 case M_BLT:
4447 if (treg == 0)
4448 {
17a2f251
TS
4449 macro_build (NULL, &icnt, &offset_expr, likely ? "bltzl" : "bltz",
4450 "s,p", sreg);
252b5132
RH
4451 return;
4452 }
4453 if (sreg == 0)
4454 {
17a2f251
TS
4455 macro_build (NULL, &icnt, &offset_expr, likely ? "bgtzl" : "bgtz",
4456 "s,p", treg);
252b5132
RH
4457 return;
4458 }
17a2f251
TS
4459 macro_build (NULL, &icnt, NULL, "slt", "d,v,t", AT, sreg, treg);
4460 macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4461 "s,t,p", AT, 0);
252b5132
RH
4462 break;
4463
4464 case M_BLTUL:
4465 likely = 1;
4466 case M_BLTU:
4467 if (treg == 0)
4468 goto do_false;
4469 if (sreg == 0)
4470 {
17a2f251
TS
4471 macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4472 "s,t,p", 0, treg);
252b5132
RH
4473 return;
4474 }
17a2f251
TS
4475 macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", AT, sreg, treg);
4476 macro_build (NULL, &icnt, &offset_expr, likely ? "bnel" : "bne",
4477 "s,t,p", AT, 0);
252b5132
RH
4478 break;
4479
5f74bc13
CD
4480 case M_DEXT:
4481 {
4482 unsigned long pos;
4483 unsigned long size;
4484
4485 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
4486 {
4487 as_bad (_("Unsupported large constant"));
4488 pos = size = 1;
4489 }
4490 else
4491 {
4492 pos = (unsigned long) imm_expr.X_add_number;
4493 size = (unsigned long) imm2_expr.X_add_number;
4494 }
4495
4496 if (pos > 63)
4497 {
4498 as_bad (_("Improper position (%lu)"), pos);
4499 pos = 1;
4500 }
4501 if (size == 0 || size > 64
4502 || (pos + size - 1) > 63)
4503 {
4504 as_bad (_("Improper extract size (%lu, position %lu)"),
4505 size, pos);
4506 size = 1;
4507 }
4508
4509 if (size <= 32 && pos < 32)
4510 {
4511 s = "dext";
4512 fmt = "t,r,+A,+C";
4513 }
4514 else if (size <= 32)
4515 {
4516 s = "dextu";
4517 fmt = "t,r,+E,+H";
4518 }
4519 else
4520 {
4521 s = "dextm";
4522 fmt = "t,r,+A,+G";
4523 }
4524 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s,
4525 fmt, treg, sreg, pos, size - 1);
4526 }
4527 return;
4528
4529 case M_DINS:
4530 {
4531 unsigned long pos;
4532 unsigned long size;
4533
4534 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
4535 {
4536 as_bad (_("Unsupported large constant"));
4537 pos = size = 1;
4538 }
4539 else
4540 {
4541 pos = (unsigned long) imm_expr.X_add_number;
4542 size = (unsigned long) imm2_expr.X_add_number;
4543 }
4544
4545 if (pos > 63)
4546 {
4547 as_bad (_("Improper position (%lu)"), pos);
4548 pos = 1;
4549 }
4550 if (size == 0 || size > 64
4551 || (pos + size - 1) > 63)
4552 {
4553 as_bad (_("Improper insert size (%lu, position %lu)"),
4554 size, pos);
4555 size = 1;
4556 }
4557
4558 if (pos < 32 && (pos + size - 1) < 32)
4559 {
4560 s = "dins";
4561 fmt = "t,r,+A,+B";
4562 }
4563 else if (pos >= 32)
4564 {
4565 s = "dinsu";
4566 fmt = "t,r,+E,+F";
4567 }
4568 else
4569 {
4570 s = "dinsm";
4571 fmt = "t,r,+A,+F";
4572 }
4573 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s,
4574 fmt, treg, sreg, pos, pos + size - 1);
4575 }
4576 return;
4577
252b5132
RH
4578 case M_DDIV_3:
4579 dbl = 1;
4580 case M_DIV_3:
4581 s = "mflo";
4582 goto do_div3;
4583 case M_DREM_3:
4584 dbl = 1;
4585 case M_REM_3:
4586 s = "mfhi";
4587 do_div3:
4588 if (treg == 0)
4589 {
4590 as_warn (_("Divide by zero."));
4591 if (mips_trap)
17a2f251 4592 macro_build (NULL, &icnt, NULL, "teq", "s,t,q", 0, 0, 7);
252b5132 4593 else
17a2f251 4594 macro_build (NULL, &icnt, NULL, "break", "c", 7);
252b5132
RH
4595 return;
4596 }
4597
b34976b6 4598 mips_emit_delays (TRUE);
252b5132
RH
4599 ++mips_opts.noreorder;
4600 mips_any_noreorder = 1;
4601 if (mips_trap)
4602 {
17a2f251
TS
4603 macro_build (NULL, &icnt, NULL, "teq", "s,t,q", treg, 0, 7);
4604 macro_build (NULL, &icnt, NULL, dbl ? "ddiv" : "div", "z,s,t",
4605 sreg, treg);
252b5132
RH
4606 }
4607 else
4608 {
4609 expr1.X_add_number = 8;
17a2f251
TS
4610 macro_build (NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4611 macro_build (NULL, &icnt, NULL, dbl ? "ddiv" : "div", "z,s,t",
4612 sreg, treg);
bf151ce7 4613 macro_build (NULL, &icnt, NULL, "break", "c", 7);
252b5132
RH
4614 }
4615 expr1.X_add_number = -1;
17a2f251
TS
4616 macro_build (NULL, &icnt, &expr1, dbl ? "daddiu" : "addiu", "t,r,j",
4617 AT, 0, BFD_RELOC_LO16);
252b5132 4618 expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
17a2f251 4619 macro_build (NULL, &icnt, &expr1, "bne", "s,t,p", treg, AT);
252b5132
RH
4620 if (dbl)
4621 {
4622 expr1.X_add_number = 1;
17a2f251
TS
4623 macro_build (NULL, &icnt, &expr1, "daddiu", "t,r,j", AT, 0,
4624 BFD_RELOC_LO16);
4625 macro_build (NULL, &icnt, NULL, "dsll32", "d,w,<", AT, AT, 31);
252b5132
RH
4626 }
4627 else
4628 {
4629 expr1.X_add_number = 0x80000000;
17a2f251
TS
4630 macro_build (NULL, &icnt, &expr1, "lui", "t,u", AT,
4631 BFD_RELOC_HI16);
252b5132
RH
4632 }
4633 if (mips_trap)
4634 {
17a2f251 4635 macro_build (NULL, &icnt, NULL, "teq", "s,t,q", sreg, AT, 6);
252b5132
RH
4636 /* We want to close the noreorder block as soon as possible, so
4637 that later insns are available for delay slot filling. */
4638 --mips_opts.noreorder;
4639 }
4640 else
4641 {
4642 expr1.X_add_number = 8;
17a2f251
TS
4643 macro_build (NULL, &icnt, &expr1, "bne", "s,t,p", sreg, AT);
4644 macro_build (NULL, &icnt, NULL, "nop", "", 0);
252b5132
RH
4645
4646 /* We want to close the noreorder block as soon as possible, so
4647 that later insns are available for delay slot filling. */
4648 --mips_opts.noreorder;
4649
17a2f251 4650 macro_build (NULL, &icnt, NULL, "break", "c", 6);
252b5132 4651 }
17a2f251 4652 macro_build (NULL, &icnt, NULL, s, "d", dreg);
252b5132
RH
4653 break;
4654
4655 case M_DIV_3I:
4656 s = "div";
4657 s2 = "mflo";
4658 goto do_divi;
4659 case M_DIVU_3I:
4660 s = "divu";
4661 s2 = "mflo";
4662 goto do_divi;
4663 case M_REM_3I:
4664 s = "div";
4665 s2 = "mfhi";
4666 goto do_divi;
4667 case M_REMU_3I:
4668 s = "divu";
4669 s2 = "mfhi";
4670 goto do_divi;
4671 case M_DDIV_3I:
4672 dbl = 1;
4673 s = "ddiv";
4674 s2 = "mflo";
4675 goto do_divi;
4676 case M_DDIVU_3I:
4677 dbl = 1;
4678 s = "ddivu";
4679 s2 = "mflo";
4680 goto do_divi;
4681 case M_DREM_3I:
4682 dbl = 1;
4683 s = "ddiv";
4684 s2 = "mfhi";
4685 goto do_divi;
4686 case M_DREMU_3I:
4687 dbl = 1;
4688 s = "ddivu";
4689 s2 = "mfhi";
4690 do_divi:
4691 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4692 {
4693 as_warn (_("Divide by zero."));
4694 if (mips_trap)
17a2f251 4695 macro_build (NULL, &icnt, NULL, "teq", "s,t,q", 0, 0, 7);
252b5132 4696 else
17a2f251 4697 macro_build (NULL, &icnt, NULL, "break", "c", 7);
252b5132
RH
4698 return;
4699 }
4700 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4701 {
4702 if (strcmp (s2, "mflo") == 0)
ea1fb5dc 4703 move_register (&icnt, dreg, sreg);
252b5132 4704 else
ea1fb5dc 4705 move_register (&icnt, dreg, 0);
252b5132
RH
4706 return;
4707 }
4708 if (imm_expr.X_op == O_constant
4709 && imm_expr.X_add_number == -1
4710 && s[strlen (s) - 1] != 'u')
4711 {
4712 if (strcmp (s2, "mflo") == 0)
4713 {
17a2f251
TS
4714 macro_build (NULL, &icnt, NULL, dbl ? "dneg" : "neg", "d,w",
4715 dreg, sreg);
252b5132
RH
4716 }
4717 else
ea1fb5dc 4718 move_register (&icnt, dreg, 0);
252b5132
RH
4719 return;
4720 }
4721
4722 load_register (&icnt, AT, &imm_expr, dbl);
17a2f251
TS
4723 macro_build (NULL, &icnt, NULL, s, "z,s,t", sreg, AT);
4724 macro_build (NULL, &icnt, NULL, s2, "d", dreg);
252b5132
RH
4725 break;
4726
4727 case M_DIVU_3:
4728 s = "divu";
4729 s2 = "mflo";
4730 goto do_divu3;
4731 case M_REMU_3:
4732 s = "divu";
4733 s2 = "mfhi";
4734 goto do_divu3;
4735 case M_DDIVU_3:
4736 s = "ddivu";
4737 s2 = "mflo";
4738 goto do_divu3;
4739 case M_DREMU_3:
4740 s = "ddivu";
4741 s2 = "mfhi";
4742 do_divu3:
b34976b6 4743 mips_emit_delays (TRUE);
252b5132
RH
4744 ++mips_opts.noreorder;
4745 mips_any_noreorder = 1;
4746 if (mips_trap)
4747 {
17a2f251
TS
4748 macro_build (NULL, &icnt, NULL, "teq", "s,t,q", treg, 0, 7);
4749 macro_build (NULL, &icnt, NULL, s, "z,s,t", sreg, treg);
252b5132
RH
4750 /* We want to close the noreorder block as soon as possible, so
4751 that later insns are available for delay slot filling. */
4752 --mips_opts.noreorder;
4753 }
4754 else
4755 {
4756 expr1.X_add_number = 8;
17a2f251
TS
4757 macro_build (NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
4758 macro_build (NULL, &icnt, NULL, s, "z,s,t", sreg, treg);
252b5132
RH
4759
4760 /* We want to close the noreorder block as soon as possible, so
4761 that later insns are available for delay slot filling. */
4762 --mips_opts.noreorder;
17a2f251 4763 macro_build (NULL, &icnt, NULL, "break", "c", 7);
252b5132 4764 }
17a2f251 4765 macro_build (NULL, &icnt, NULL, s2, "d", dreg);
252b5132
RH
4766 return;
4767
1abe91b1
MR
4768 case M_DLCA_AB:
4769 dbl = 1;
4770 case M_LCA_AB:
4771 call = 1;
4772 goto do_la;
252b5132
RH
4773 case M_DLA_AB:
4774 dbl = 1;
4775 case M_LA_AB:
1abe91b1 4776 do_la:
252b5132
RH
4777 /* Load the address of a symbol into a register. If breg is not
4778 zero, we then add a base register to it. */
4779
3bec30a8
TS
4780 if (dbl && HAVE_32BIT_GPRS)
4781 as_warn (_("dla used to load 32-bit register"));
4782
c90bbe5b 4783 if (! dbl && HAVE_64BIT_OBJECTS)
3bec30a8
TS
4784 as_warn (_("la used to load 64-bit address"));
4785
0c11417f
MR
4786 if (offset_expr.X_op == O_constant
4787 && offset_expr.X_add_number >= -0x8000
4788 && offset_expr.X_add_number < 0x8000)
4789 {
17a2f251 4790 macro_build (NULL, &icnt, &offset_expr,
b7c7d6c1 4791 (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
17a2f251 4792 "t,r,j", treg, sreg, BFD_RELOC_LO16);
0c11417f
MR
4793 return;
4794 }
4795
afdbd6d0
CD
4796 if (treg == breg)
4797 {
4798 tempreg = AT;
4799 used_at = 1;
4800 }
4801 else
4802 {
4803 tempreg = treg;
4804 used_at = 0;
4805 }
4806
252b5132
RH
4807 /* When generating embedded PIC code, we permit expressions of
4808 the form
afdbd6d0
CD
4809 la $treg,foo-bar
4810 la $treg,foo-bar($breg)
bb2d6cd7 4811 where bar is an address in the current section. These are used
252b5132
RH
4812 when getting the addresses of functions. We don't permit
4813 X_add_number to be non-zero, because if the symbol is
4814 external the relaxing code needs to know that any addend is
4815 purely the offset to X_op_symbol. */
4816 if (mips_pic == EMBEDDED_PIC
4817 && offset_expr.X_op == O_subtract
49309057 4818 && (symbol_constant_p (offset_expr.X_op_symbol)
bb2d6cd7 4819 ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
49309057
ILT
4820 : (symbol_equated_p (offset_expr.X_op_symbol)
4821 && (S_GET_SEGMENT
4822 (symbol_get_value_expression (offset_expr.X_op_symbol)
4823 ->X_add_symbol)
bb2d6cd7 4824 == now_seg)))
bb2d6cd7
GK
4825 && (offset_expr.X_add_number == 0
4826 || OUTPUT_FLAVOR == bfd_target_elf_flavour))
252b5132 4827 {
afdbd6d0
CD
4828 if (breg == 0)
4829 {
4830 tempreg = treg;
4831 used_at = 0;
17a2f251
TS
4832 macro_build (NULL, &icnt, &offset_expr, "lui", "t,u", tempreg,
4833 BFD_RELOC_PCREL_HI16_S);
afdbd6d0
CD
4834 }
4835 else
4836 {
17a2f251
TS
4837 macro_build (NULL, &icnt, &offset_expr, "lui", "t,u", tempreg,
4838 BFD_RELOC_PCREL_HI16_S);
4839 macro_build (NULL, &icnt, NULL,
4d34fb5f 4840 (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu",
afdbd6d0
CD
4841 "d,v,t", tempreg, tempreg, breg);
4842 }
17a2f251 4843 macro_build (NULL, &icnt, &offset_expr,
4d34fb5f 4844 (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
17a2f251 4845 "t,r,j", treg, tempreg, BFD_RELOC_PCREL_LO16);
afdbd6d0
CD
4846 if (! used_at)
4847 return;
4848 break;
252b5132
RH
4849 }
4850
4851 if (offset_expr.X_op != O_symbol
4852 && offset_expr.X_op != O_constant)
4853 {
4854 as_bad (_("expression too complex"));
4855 offset_expr.X_op = O_constant;
4856 }
4857
252b5132 4858 if (offset_expr.X_op == O_constant)
4d34fb5f
TS
4859 load_register (&icnt, tempreg, &offset_expr,
4860 ((mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
4861 ? (dbl || HAVE_64BIT_ADDRESSES)
4862 : HAVE_64BIT_ADDRESSES));
252b5132
RH
4863 else if (mips_pic == NO_PIC)
4864 {
d6bc6245 4865 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 4866 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
4867 Otherwise we want
4868 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
4869 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4870 If we have a constant, we need two instructions anyhow,
d6bc6245 4871 so we may as well always use the latter form.
76b3015f 4872
d6bc6245
TS
4873 With 64bit address space and a usable $at we want
4874 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4875 lui $at,<sym> (BFD_RELOC_HI16_S)
4876 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4877 daddiu $at,<sym> (BFD_RELOC_LO16)
4878 dsll32 $tempreg,0
3a482fd5 4879 daddu $tempreg,$tempreg,$at
76b3015f 4880
c03099e6 4881 If $at is already in use, we use a path which is suboptimal
d6bc6245
TS
4882 on superscalar processors.
4883 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4884 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4885 dsll $tempreg,16
4886 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
4887 dsll $tempreg,16
4888 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
4889 */
f9419b05 4890 char *p = NULL;
d6bc6245 4891 if (HAVE_64BIT_ADDRESSES)
252b5132 4892 {
d6bc6245
TS
4893 /* We don't do GP optimization for now because RELAX_ENCODE can't
4894 hold the data for such large chunks. */
4895
460597ba 4896 if (used_at == 0 && ! mips_opts.noat)
98d3f06f
KH
4897 {
4898 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
17a2f251 4899 tempreg, BFD_RELOC_MIPS_HIGHEST);
98d3f06f 4900 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
17a2f251 4901 AT, BFD_RELOC_HI16_S);
98d3f06f 4902 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
17a2f251 4903 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
98d3f06f 4904 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
17a2f251
TS
4905 AT, AT, BFD_RELOC_LO16);
4906 macro_build (p, &icnt, NULL, "dsll32", "d,w,<",
4907 tempreg, tempreg, 0);
4908 macro_build (p, &icnt, NULL, "daddu", "d,v,t",
4909 tempreg, tempreg, AT);
98d3f06f
KH
4910 used_at = 1;
4911 }
4912 else
4913 {
4914 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
17a2f251 4915 tempreg, BFD_RELOC_MIPS_HIGHEST);
98d3f06f 4916 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
17a2f251
TS
4917 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
4918 macro_build (p, &icnt, NULL, "dsll", "d,w,<",
98d3f06f
KH
4919 tempreg, tempreg, 16);
4920 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
17a2f251
TS
4921 tempreg, tempreg, BFD_RELOC_HI16_S);
4922 macro_build (p, &icnt, NULL, "dsll", "d,w,<",
98d3f06f
KH
4923 tempreg, tempreg, 16);
4924 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
17a2f251 4925 tempreg, tempreg, BFD_RELOC_LO16);
98d3f06f
KH
4926 }
4927 }
4928 else
4929 {
4930 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4931 && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
4932 {
4933 frag_grow (20);
17a2f251
TS
4934 macro_build (NULL, &icnt, &offset_expr, ADDRESS_ADDI_INSN,
4935 "t,r,j", tempreg, mips_gp_register,
4936 BFD_RELOC_GPREL16);
98d3f06f
KH
4937 p = frag_var (rs_machine_dependent, 8, 0,
4938 RELAX_ENCODE (4, 8, 0, 4, 0,
4939 mips_opts.warn_about_macros),
4940 offset_expr.X_add_symbol, 0, NULL);
4941 }
4942 macro_build_lui (p, &icnt, &offset_expr, tempreg);
4943 if (p != NULL)
4944 p += 4;
f899b4b8 4945 macro_build (p, &icnt, &offset_expr, ADDRESS_ADDI_INSN,
17a2f251 4946 "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
98d3f06f 4947 }
252b5132 4948 }
f5040a92 4949 else if (mips_pic == SVR4_PIC && ! mips_big_got && ! HAVE_NEWABI)
252b5132 4950 {
9117d219
NC
4951 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
4952
252b5132
RH
4953 /* If this is a reference to an external symbol, and there
4954 is no constant, we want
4955 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
1abe91b1 4956 or for lca or if tempreg is PIC_CALL_REG
9117d219 4957 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
252b5132
RH
4958 For a local symbol, we want
4959 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4960 nop
4961 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4962
4963 If we have a small constant, and this is a reference to
4964 an external symbol, we want
4965 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4966 nop
4967 addiu $tempreg,$tempreg,<constant>
4968 For a local symbol, we want the same instruction
4969 sequence, but we output a BFD_RELOC_LO16 reloc on the
4970 addiu instruction.
4971
4972 If we have a large constant, and this is a reference to
4973 an external symbol, we want
4974 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4975 lui $at,<hiconstant>
4976 addiu $at,$at,<loconstant>
4977 addu $tempreg,$tempreg,$at
4978 For a local symbol, we want the same instruction
4979 sequence, but we output a BFD_RELOC_LO16 reloc on the
ed6fb7bd 4980 addiu instruction.
ed6fb7bd
SC
4981 */
4982
252b5132
RH
4983 expr1.X_add_number = offset_expr.X_add_number;
4984 offset_expr.X_add_number = 0;
4985 frag_grow (32);
1abe91b1
MR
4986 if (expr1.X_add_number == 0 && breg == 0
4987 && (call || tempreg == PIC_CALL_REG))
9117d219 4988 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
17a2f251
TS
4989 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
4990 tempreg, lw_reloc_type, mips_gp_register);
252b5132
RH
4991 if (expr1.X_add_number == 0)
4992 {
4993 int off;
f9419b05 4994 char *p;
252b5132
RH
4995
4996 if (breg == 0)
4997 off = 0;
4998 else
4999 {
5000 /* We're going to put in an addu instruction using
5001 tempreg, so we may as well insert the nop right
5002 now. */
17a2f251 5003 macro_build (NULL, &icnt, NULL, "nop", "");
252b5132
RH
5004 off = 4;
5005 }
5006 p = frag_var (rs_machine_dependent, 8 - off, 0,
5007 RELAX_ENCODE (0, 8 - off, -4 - off, 4 - off, 0,
5008 (breg == 0
5009 ? mips_opts.warn_about_macros
5010 : 0)),
c4e7957c 5011 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5012 if (breg == 0)
5013 {
17a2f251 5014 macro_build (p, &icnt, NULL, "nop", "");
252b5132
RH
5015 p += 4;
5016 }
f899b4b8 5017 macro_build (p, &icnt, &expr1, ADDRESS_ADDI_INSN,
17a2f251 5018 "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
252b5132
RH
5019 /* FIXME: If breg == 0, and the next instruction uses
5020 $tempreg, then if this variant case is used an extra
5021 nop will be generated. */
5022 }
5023 else if (expr1.X_add_number >= -0x8000
5024 && expr1.X_add_number < 0x8000)
5025 {
17a2f251
TS
5026 macro_build (NULL, &icnt, NULL, "nop", "");
5027 macro_build (NULL, &icnt, &expr1, ADDRESS_ADDI_INSN,
5028 "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
c4e7957c
TS
5029 frag_var (rs_machine_dependent, 0, 0,
5030 RELAX_ENCODE (0, 0, -12, -4, 0, 0),
5031 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5032 }
5033 else
5034 {
5035 int off1;
5036
5037 /* If we are going to add in a base register, and the
5038 target register and the base register are the same,
5039 then we are using AT as a temporary register. Since
5040 we want to load the constant into AT, we add our
5041 current AT (from the global offset table) and the
5042 register into the register now, and pretend we were
5043 not using a base register. */
5044 if (breg != treg)
5045 off1 = 0;
5046 else
5047 {
17a2f251
TS
5048 macro_build (NULL, &icnt, NULL, "nop", "");
5049 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5050 treg, AT, breg);
252b5132
RH
5051 breg = 0;
5052 tempreg = treg;
5053 off1 = -8;
5054 }
5055
5056 /* Set mips_optimize around the lui instruction to avoid
5057 inserting an unnecessary nop after the lw. */
5058 hold_mips_optimize = mips_optimize;
5059 mips_optimize = 2;
c4e7957c 5060 macro_build_lui (NULL, &icnt, &expr1, AT);
252b5132
RH
5061 mips_optimize = hold_mips_optimize;
5062
17a2f251
TS
5063 macro_build (NULL, &icnt, &expr1, ADDRESS_ADDI_INSN, "t,r,j",
5064 AT, AT, BFD_RELOC_LO16);
5065 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5066 tempreg, tempreg, AT);
c4e7957c
TS
5067 frag_var (rs_machine_dependent, 0, 0,
5068 RELAX_ENCODE (0, 0, -16 + off1, -8, 0, 0),
5069 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5070 used_at = 1;
5071 }
5072 }
f5040a92
AO
5073 else if (mips_pic == SVR4_PIC && ! mips_big_got && HAVE_NEWABI)
5074 {
5075 char *p = NULL;
5076 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_DISP;
5077 int adj = 0;
5078
5079 /* If this is a reference to an external, and there is no
5080 constant, or local symbol (*), with or without a
5081 constant, we want
5082 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
1abe91b1 5083 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
5084 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5085
5086 If we have a small constant, and this is a reference to
5087 an external symbol, we want
5088 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5089 addiu $tempreg,$tempreg,<constant>
5090
5091 If we have a large constant, and this is a reference to
5092 an external symbol, we want
5093 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5094 lui $at,<hiconstant>
5095 addiu $at,$at,<loconstant>
5096 addu $tempreg,$tempreg,$at
5097
5098 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
5099 local symbols, even though it introduces an additional
5100 instruction. */
5101
5102 frag_grow (28);
1abe91b1
MR
5103 if (offset_expr.X_add_number == 0 && breg == 0
5104 && (call || tempreg == PIC_CALL_REG))
f5040a92
AO
5105 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
5106 if (offset_expr.X_add_number)
5107 {
5108 frag_now->tc_frag_data.tc_fr_offset =
5109 expr1.X_add_number = offset_expr.X_add_number;
5110 offset_expr.X_add_number = 0;
5111
17a2f251
TS
5112 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5113 "t,o(b)", tempreg, lw_reloc_type,
5114 mips_gp_register);
f5040a92
AO
5115
5116 if (expr1.X_add_number >= -0x8000
5117 && expr1.X_add_number < 0x8000)
5118 {
17a2f251
TS
5119 macro_build (NULL, &icnt, &expr1, ADDRESS_ADDI_INSN,
5120 "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
f5040a92
AO
5121 p = frag_var (rs_machine_dependent, 4, 0,
5122 RELAX_ENCODE (8, 4, 0, 0, 0, 0),
5123 offset_expr.X_add_symbol, 0, NULL);
5124 }
ecd13cd3 5125 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92
AO
5126 {
5127 int dreg;
5128
5129 /* If we are going to add in a base register, and the
5130 target register and the base register are the same,
5131 then we are using AT as a temporary register. Since
5132 we want to load the constant into AT, we add our
5133 current AT (from the global offset table) and the
5134 register into the register now, and pretend we were
5135 not using a base register. */
5136 if (breg != treg)
5137 dreg = tempreg;
5138 else
5139 {
5140 assert (tempreg == AT);
bf151ce7 5141 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN,
17a2f251 5142 "d,v,t", treg, AT, breg);
f5040a92
AO
5143 dreg = treg;
5144 adj = 4;
5145 }
5146
17a2f251
TS
5147 macro_build_lui (NULL, &icnt, &expr1, AT);
5148 macro_build (NULL, &icnt, &expr1, ADDRESS_ADDI_INSN,
5149 "t,r,j", AT, AT, BFD_RELOC_LO16);
5150 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5151 dreg, dreg, AT);
f5040a92
AO
5152
5153 p = frag_var (rs_machine_dependent, 4 + adj, 0,
5154 RELAX_ENCODE (16 + adj, 4 + adj,
5155 0, 0, 0, 0),
5156 offset_expr.X_add_symbol, 0, NULL);
5157
5158 used_at = 1;
5159 }
5160 else
5161 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5162
5163 offset_expr.X_add_number = expr1.X_add_number;
5164
f899b4b8 5165 macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
17a2f251 5166 "t,o(b)", tempreg, BFD_RELOC_MIPS_GOT_DISP,
f5040a92
AO
5167 mips_gp_register);
5168 if (adj)
5169 {
17a2f251 5170 macro_build (p + 4, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
f899b4b8 5171 treg, tempreg, breg);
f5040a92
AO
5172 breg = 0;
5173 tempreg = treg;
5174 }
5175 }
5176 else
5177 {
17a2f251
TS
5178 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5179 "t,o(b)", tempreg, lw_reloc_type,
5180 mips_gp_register);
f5040a92
AO
5181 if (lw_reloc_type != BFD_RELOC_MIPS_GOT_DISP)
5182 p = frag_var (rs_machine_dependent, 0, 0,
5183 RELAX_ENCODE (0, 0, -4, 0, 0, 0),
5184 offset_expr.X_add_symbol, 0, NULL);
5185 }
5186
5187 if (! p)
5188 {
5189 /* To avoid confusion in tc_gen_reloc, we must ensure
5190 that this does not become a variant frag. */
5191 frag_wane (frag_now);
5192 frag_new (0);
5193 }
5194 }
5195 else if (mips_pic == SVR4_PIC && ! HAVE_NEWABI)
252b5132
RH
5196 {
5197 int gpdel;
f9419b05 5198 char *p;
9117d219
NC
5199 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5200 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
ed6fb7bd 5201 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
252b5132
RH
5202
5203 /* This is the large GOT case. If this is a reference to an
5204 external symbol, and there is no constant, we want
5205 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5206 addu $tempreg,$tempreg,$gp
5207 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 5208 or for lca or if tempreg is PIC_CALL_REG
9117d219
NC
5209 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5210 addu $tempreg,$tempreg,$gp
5211 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
252b5132
RH
5212 For a local symbol, we want
5213 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5214 nop
5215 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5216
5217 If we have a small constant, and this is a reference to
5218 an external symbol, we want
5219 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5220 addu $tempreg,$tempreg,$gp
5221 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5222 nop
5223 addiu $tempreg,$tempreg,<constant>
5224 For a local symbol, we want
5225 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5226 nop
5227 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
5228
5229 If we have a large constant, and this is a reference to
5230 an external symbol, we want
5231 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5232 addu $tempreg,$tempreg,$gp
5233 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5234 lui $at,<hiconstant>
5235 addiu $at,$at,<loconstant>
5236 addu $tempreg,$tempreg,$at
5237 For a local symbol, we want
5238 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5239 lui $at,<hiconstant>
5240 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
5241 addu $tempreg,$tempreg,$at
f5040a92 5242 */
438c16b8 5243
252b5132
RH
5244 expr1.X_add_number = offset_expr.X_add_number;
5245 offset_expr.X_add_number = 0;
5246 frag_grow (52);
f7ea7ef2 5247 if (reg_needs_delay (mips_gp_register))
252b5132
RH
5248 gpdel = 4;
5249 else
5250 gpdel = 0;
1abe91b1
MR
5251 if (expr1.X_add_number == 0 && breg == 0
5252 && (call || tempreg == PIC_CALL_REG))
9117d219
NC
5253 {
5254 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5255 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5256 }
17a2f251 5257 macro_build (NULL, &icnt, &offset_expr, "lui", "t,u",
9117d219 5258 tempreg, lui_reloc_type);
17a2f251
TS
5259 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5260 tempreg, tempreg, mips_gp_register);
5261 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5262 tempreg, lw_reloc_type, tempreg);
252b5132
RH
5263 if (expr1.X_add_number == 0)
5264 {
5265 int off;
5266
5267 if (breg == 0)
5268 off = 0;
5269 else
5270 {
5271 /* We're going to put in an addu instruction using
5272 tempreg, so we may as well insert the nop right
5273 now. */
17a2f251 5274 macro_build (NULL, &icnt, NULL, "nop", "");
252b5132
RH
5275 off = 4;
5276 }
5277
5278 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5279 RELAX_ENCODE (12 + off, 12 + gpdel, gpdel,
5280 8 + gpdel, 0,
5281 (breg == 0
5282 ? mips_opts.warn_about_macros
5283 : 0)),
c4e7957c 5284 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5285 }
5286 else if (expr1.X_add_number >= -0x8000
5287 && expr1.X_add_number < 0x8000)
5288 {
17a2f251
TS
5289 macro_build (NULL, &icnt, NULL, "nop", "");
5290 macro_build (NULL, &icnt, &expr1, ADDRESS_ADDI_INSN, "t,r,j",
5291 tempreg, tempreg, BFD_RELOC_LO16);
252b5132
RH
5292
5293 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5294 RELAX_ENCODE (20, 12 + gpdel, gpdel, 8 + gpdel, 0,
5295 (breg == 0
5296 ? mips_opts.warn_about_macros
5297 : 0)),
c4e7957c 5298 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5299 }
5300 else
5301 {
5302 int adj, dreg;
5303
5304 /* If we are going to add in a base register, and the
5305 target register and the base register are the same,
5306 then we are using AT as a temporary register. Since
5307 we want to load the constant into AT, we add our
5308 current AT (from the global offset table) and the
5309 register into the register now, and pretend we were
5310 not using a base register. */
5311 if (breg != treg)
5312 {
5313 adj = 0;
5314 dreg = tempreg;
5315 }
5316 else
5317 {
5318 assert (tempreg == AT);
17a2f251
TS
5319 macro_build (NULL, &icnt, NULL, "nop", "");
5320 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5321 treg, AT, breg);
252b5132
RH
5322 dreg = treg;
5323 adj = 8;
5324 }
5325
5326 /* Set mips_optimize around the lui instruction to avoid
5327 inserting an unnecessary nop after the lw. */
5328 hold_mips_optimize = mips_optimize;
5329 mips_optimize = 2;
c4e7957c 5330 macro_build_lui (NULL, &icnt, &expr1, AT);
252b5132
RH
5331 mips_optimize = hold_mips_optimize;
5332
17a2f251
TS
5333 macro_build (NULL, &icnt, &expr1, ADDRESS_ADDI_INSN, "t,r,j",
5334 AT, AT, BFD_RELOC_LO16);
5335 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5336 dreg, dreg, AT);
252b5132
RH
5337
5338 p = frag_var (rs_machine_dependent, 16 + gpdel + adj, 0,
5339 RELAX_ENCODE (24 + adj, 16 + gpdel + adj, gpdel,
5340 8 + gpdel, 0,
5341 (breg == 0
5342 ? mips_opts.warn_about_macros
5343 : 0)),
c4e7957c 5344 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5345
5346 used_at = 1;
5347 }
5348
5349 if (gpdel > 0)
5350 {
5351 /* This is needed because this instruction uses $gp, but
f5040a92 5352 the first instruction on the main stream does not. */
17a2f251 5353 macro_build (p, &icnt, NULL, "nop", "");
252b5132
RH
5354 p += 4;
5355 }
ed6fb7bd 5356
f899b4b8
TS
5357 macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5358 tempreg, local_reloc_type, mips_gp_register);
252b5132 5359 p += 4;
f5040a92 5360 if (expr1.X_add_number >= -0x8000
252b5132
RH
5361 && expr1.X_add_number < 0x8000)
5362 {
17a2f251 5363 macro_build (p, &icnt, NULL, "nop", "");
252b5132 5364 p += 4;
f899b4b8 5365 macro_build (p, &icnt, &expr1, ADDRESS_ADDI_INSN,
17a2f251 5366 "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
252b5132 5367 /* FIXME: If add_number is 0, and there was no base
f5040a92
AO
5368 register, the external symbol case ended with a load,
5369 so if the symbol turns out to not be external, and
5370 the next instruction uses tempreg, an unnecessary nop
5371 will be inserted. */
252b5132
RH
5372 }
5373 else
5374 {
5375 if (breg == treg)
5376 {
5377 /* We must add in the base register now, as in the
f5040a92 5378 external symbol case. */
252b5132 5379 assert (tempreg == AT);
17a2f251 5380 macro_build (p, &icnt, NULL, "nop", "");
252b5132 5381 p += 4;
17a2f251
TS
5382 macro_build (p, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5383 treg, AT, breg);
252b5132
RH
5384 p += 4;
5385 tempreg = treg;
5386 /* We set breg to 0 because we have arranged to add
f5040a92 5387 it in in both cases. */
252b5132
RH
5388 breg = 0;
5389 }
5390
5391 macro_build_lui (p, &icnt, &expr1, AT);
5392 p += 4;
17a2f251
TS
5393 macro_build (p, &icnt, &expr1, ADDRESS_ADDI_INSN, "t,r,j",
5394 AT, AT, BFD_RELOC_LO16);
252b5132 5395 p += 4;
17a2f251
TS
5396 macro_build (p, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5397 tempreg, tempreg, AT);
252b5132
RH
5398 p += 4;
5399 }
5400 }
f5040a92
AO
5401 else if (mips_pic == SVR4_PIC && HAVE_NEWABI)
5402 {
5403 char *p = NULL;
5404 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5405 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
5406 int adj = 0;
5407
5408 /* This is the large GOT case. If this is a reference to an
5409 external symbol, and there is no constant, we want
5410 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5411 add $tempreg,$tempreg,$gp
5412 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 5413 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
5414 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5415 add $tempreg,$tempreg,$gp
5416 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5417
5418 If we have a small constant, and this is a reference to
5419 an external symbol, we want
5420 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5421 add $tempreg,$tempreg,$gp
5422 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5423 addi $tempreg,$tempreg,<constant>
5424
5425 If we have a large constant, and this is a reference to
5426 an external symbol, we want
5427 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5428 addu $tempreg,$tempreg,$gp
5429 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5430 lui $at,<hiconstant>
5431 addi $at,$at,<loconstant>
5432 add $tempreg,$tempreg,$at
5433
5434 If we have NewABI, and we know it's a local symbol, we want
5435 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
5436 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
5437 otherwise we have to resort to GOT_HI16/GOT_LO16. */
5438
5439 frag_grow (40);
5440
5441 frag_now->tc_frag_data.tc_fr_offset =
5442 expr1.X_add_number = offset_expr.X_add_number;
5443 offset_expr.X_add_number = 0;
5444
1abe91b1
MR
5445 if (expr1.X_add_number == 0 && breg == 0
5446 && (call || tempreg == PIC_CALL_REG))
f5040a92
AO
5447 {
5448 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5449 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5450 }
17a2f251 5451 macro_build (NULL, &icnt, &offset_expr, "lui", "t,u",
f5040a92 5452 tempreg, lui_reloc_type);
17a2f251
TS
5453 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5454 tempreg, tempreg, mips_gp_register);
5455 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
f5040a92
AO
5456 "t,o(b)", tempreg, lw_reloc_type, tempreg);
5457
5458 if (expr1.X_add_number == 0)
5459 {
5460 p = frag_var (rs_machine_dependent, 8, 0,
5461 RELAX_ENCODE (12, 8, 0, 4, 0,
5462 mips_opts.warn_about_macros),
5463 offset_expr.X_add_symbol, 0, NULL);
684022ea 5464 }
f5040a92
AO
5465 else if (expr1.X_add_number >= -0x8000
5466 && expr1.X_add_number < 0x8000)
5467 {
17a2f251
TS
5468 macro_build (NULL, &icnt, &expr1, ADDRESS_ADDI_INSN, "t,r,j",
5469 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92
AO
5470 p = frag_var (rs_machine_dependent, 8, 0,
5471 RELAX_ENCODE (16, 8, 0, 4, 0,
5472 mips_opts.warn_about_macros),
5473 offset_expr.X_add_symbol, 0, NULL);
5474 }
ecd13cd3 5475 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92
AO
5476 {
5477 int dreg;
5478
5479 /* If we are going to add in a base register, and the
5480 target register and the base register are the same,
5481 then we are using AT as a temporary register. Since
5482 we want to load the constant into AT, we add our
5483 current AT (from the global offset table) and the
5484 register into the register now, and pretend we were
5485 not using a base register. */
5486 if (breg != treg)
5487 dreg = tempreg;
5488 else
5489 {
5490 assert (tempreg == AT);
17a2f251
TS
5491 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5492 treg, AT, breg);
f5040a92
AO
5493 dreg = treg;
5494 adj = 4;
5495 }
5496
5497 /* Set mips_optimize around the lui instruction to avoid
5498 inserting an unnecessary nop after the lw. */
17a2f251
TS
5499 macro_build_lui (NULL, &icnt, &expr1, AT);
5500 macro_build (NULL, &icnt, &expr1, ADDRESS_ADDI_INSN,
5501 "t,r,j", AT, AT, BFD_RELOC_LO16);
5502 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5503 dreg, dreg, AT);
f5040a92
AO
5504
5505 p = frag_var (rs_machine_dependent, 8 + adj, 0,
5506 RELAX_ENCODE (24 + adj, 8 + adj,
5507 0, 4, 0,
5508 (breg == 0
5509 ? mips_opts.warn_about_macros
5510 : 0)),
5511 offset_expr.X_add_symbol, 0, NULL);
5512
5513 used_at = 1;
5514 }
5515 else
5516 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5517
5518 offset_expr.X_add_number = expr1.X_add_number;
f899b4b8 5519 macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 5520 tempreg, BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f899b4b8 5521 macro_build (p + 4, &icnt, &offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 5522 tempreg, tempreg, BFD_RELOC_MIPS_GOT_OFST);
f5040a92
AO
5523 if (adj)
5524 {
17a2f251
TS
5525 macro_build (p + 8, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5526 treg, tempreg, breg);
f5040a92
AO
5527 breg = 0;
5528 tempreg = treg;
5529 }
5530 }
252b5132
RH
5531 else if (mips_pic == EMBEDDED_PIC)
5532 {
5533 /* We use
cdf6fd85 5534 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132 5535 */
17a2f251
TS
5536 macro_build (NULL, &icnt, &offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5537 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
252b5132
RH
5538 }
5539 else
5540 abort ();
5541
5542 if (breg != 0)
4d34fb5f
TS
5543 {
5544 char *s;
5545
5546 if (mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
b7c7d6c1 5547 s = (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu";
4d34fb5f 5548 else
f899b4b8 5549 s = ADDRESS_ADD_INSN;
4d34fb5f 5550
17a2f251 5551 macro_build (NULL, &icnt, NULL, s, "d,v,t", treg, tempreg, breg);
4d34fb5f 5552 }
252b5132
RH
5553
5554 if (! used_at)
5555 return;
5556
5557 break;
5558
5559 case M_J_A:
5560 /* The j instruction may not be used in PIC code, since it
5561 requires an absolute address. We convert it to a b
5562 instruction. */
5563 if (mips_pic == NO_PIC)
17a2f251 5564 macro_build (NULL, &icnt, &offset_expr, "j", "a");
252b5132 5565 else
17a2f251 5566 macro_build (NULL, &icnt, &offset_expr, "b", "p");
252b5132
RH
5567 return;
5568
5569 /* The jal instructions must be handled as macros because when
5570 generating PIC code they expand to multi-instruction
5571 sequences. Normally they are simple instructions. */
5572 case M_JAL_1:
5573 dreg = RA;
5574 /* Fall through. */
5575 case M_JAL_2:
5576 if (mips_pic == NO_PIC
5577 || mips_pic == EMBEDDED_PIC)
17a2f251 5578 macro_build (NULL, &icnt, NULL, "jalr", "d,s", dreg, sreg);
252b5132
RH
5579 else if (mips_pic == SVR4_PIC)
5580 {
5581 if (sreg != PIC_CALL_REG)
5582 as_warn (_("MIPS PIC call to register other than $25"));
bdaaa2e1 5583
17a2f251 5584 macro_build (NULL, &icnt, NULL, "jalr", "d,s", dreg, sreg);
6478892d 5585 if (! HAVE_NEWABI)
252b5132 5586 {
6478892d
TS
5587 if (mips_cprestore_offset < 0)
5588 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5589 else
5590 {
7a621144
DJ
5591 if (! mips_frame_reg_valid)
5592 {
5593 as_warn (_("No .frame pseudo-op used in PIC code"));
5594 /* Quiet this warning. */
5595 mips_frame_reg_valid = 1;
5596 }
5597 if (! mips_cprestore_valid)
5598 {
5599 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5600 /* Quiet this warning. */
5601 mips_cprestore_valid = 1;
5602 }
6478892d 5603 expr1.X_add_number = mips_cprestore_offset;
17a2f251 5604 macro_build_ldst_constoffset (NULL, &icnt, &expr1,
f899b4b8
TS
5605 ADDRESS_LOAD_INSN,
5606 mips_gp_register,
256ab948
TS
5607 mips_frame_reg,
5608 HAVE_64BIT_ADDRESSES);
6478892d 5609 }
252b5132
RH
5610 }
5611 }
5612 else
5613 abort ();
5614
5615 return;
5616
5617 case M_JAL_A:
5618 if (mips_pic == NO_PIC)
17a2f251 5619 macro_build (NULL, &icnt, &offset_expr, "jal", "a");
252b5132
RH
5620 else if (mips_pic == SVR4_PIC)
5621 {
f9419b05
TS
5622 char *p;
5623
252b5132
RH
5624 /* If this is a reference to an external symbol, and we are
5625 using a small GOT, we want
5626 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5627 nop
f9419b05 5628 jalr $ra,$25
252b5132
RH
5629 nop
5630 lw $gp,cprestore($sp)
5631 The cprestore value is set using the .cprestore
5632 pseudo-op. If we are using a big GOT, we want
5633 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5634 addu $25,$25,$gp
5635 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
5636 nop
f9419b05 5637 jalr $ra,$25
252b5132
RH
5638 nop
5639 lw $gp,cprestore($sp)
5640 If the symbol is not external, we want
5641 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5642 nop
5643 addiu $25,$25,<sym> (BFD_RELOC_LO16)
f9419b05 5644 jalr $ra,$25
252b5132 5645 nop
438c16b8 5646 lw $gp,cprestore($sp)
f5040a92
AO
5647
5648 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
5649 sequences above, minus nops, unless the symbol is local,
5650 which enables us to use GOT_PAGE/GOT_OFST (big got) or
5651 GOT_DISP. */
438c16b8 5652 if (HAVE_NEWABI)
252b5132 5653 {
f5040a92
AO
5654 if (! mips_big_got)
5655 {
5656 frag_grow (4);
17a2f251
TS
5657 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5658 "t,o(b)", PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
f5040a92
AO
5659 mips_gp_register);
5660 frag_var (rs_machine_dependent, 0, 0,
5661 RELAX_ENCODE (0, 0, -4, 0, 0, 0),
5662 offset_expr.X_add_symbol, 0, NULL);
5663 }
5664 else
5665 {
5666 frag_grow (20);
17a2f251
TS
5667 macro_build (NULL, &icnt, &offset_expr, "lui", "t,u",
5668 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_HI16);
5669 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5670 PIC_CALL_REG, PIC_CALL_REG, mips_gp_register);
5671 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5672 "t,o(b)", PIC_CALL_REG,
5673 BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
f5040a92
AO
5674 p = frag_var (rs_machine_dependent, 8, 0,
5675 RELAX_ENCODE (12, 8, 0, 4, 0, 0),
5676 offset_expr.X_add_symbol, 0, NULL);
f899b4b8
TS
5677 macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5678 "t,o(b)", PIC_CALL_REG,
17a2f251 5679 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f899b4b8 5680 macro_build (p + 4, &icnt, &offset_expr, ADDRESS_ADDI_INSN,
dc9461f6 5681 "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
17a2f251 5682 BFD_RELOC_MIPS_GOT_OFST);
f5040a92 5683 }
684022ea 5684
438c16b8 5685 macro_build_jalr (icnt, &offset_expr);
252b5132
RH
5686 }
5687 else
5688 {
438c16b8
TS
5689 frag_grow (40);
5690 if (! mips_big_got)
5691 {
17a2f251
TS
5692 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5693 "t,o(b)", PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
5694 mips_gp_register);
5695 macro_build (NULL, &icnt, NULL, "nop", "");
438c16b8
TS
5696 p = frag_var (rs_machine_dependent, 4, 0,
5697 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5698 offset_expr.X_add_symbol, 0, NULL);
5699 }
252b5132 5700 else
252b5132 5701 {
438c16b8
TS
5702 int gpdel;
5703
5704 if (reg_needs_delay (mips_gp_register))
5705 gpdel = 4;
5706 else
5707 gpdel = 0;
17a2f251
TS
5708 macro_build (NULL, &icnt, &offset_expr, "lui", "t,u",
5709 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_HI16);
5710 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
5711 PIC_CALL_REG, PIC_CALL_REG, mips_gp_register);
5712 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
5713 "t,o(b)", PIC_CALL_REG,
5714 BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
5715 macro_build (NULL, &icnt, NULL, "nop", "");
438c16b8
TS
5716 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5717 RELAX_ENCODE (16, 12 + gpdel, gpdel,
5718 8 + gpdel, 0, 0),
5719 offset_expr.X_add_symbol, 0, NULL);
5720 if (gpdel > 0)
5721 {
17a2f251 5722 macro_build (p, &icnt, NULL, "nop", "");
438c16b8
TS
5723 p += 4;
5724 }
f899b4b8 5725 macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
17a2f251
TS
5726 "t,o(b)", PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
5727 mips_gp_register);
438c16b8 5728 p += 4;
17a2f251 5729 macro_build (p, &icnt, NULL, "nop", "");
252b5132
RH
5730 p += 4;
5731 }
f899b4b8 5732 macro_build (p, &icnt, &offset_expr, ADDRESS_ADDI_INSN,
438c16b8 5733 "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
17a2f251 5734 BFD_RELOC_LO16);
438c16b8
TS
5735 macro_build_jalr (icnt, &offset_expr);
5736
6478892d
TS
5737 if (mips_cprestore_offset < 0)
5738 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5739 else
5740 {
7a621144
DJ
5741 if (! mips_frame_reg_valid)
5742 {
5743 as_warn (_("No .frame pseudo-op used in PIC code"));
5744 /* Quiet this warning. */
5745 mips_frame_reg_valid = 1;
5746 }
5747 if (! mips_cprestore_valid)
5748 {
5749 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5750 /* Quiet this warning. */
5751 mips_cprestore_valid = 1;
5752 }
6478892d 5753 if (mips_opts.noreorder)
17a2f251 5754 macro_build (NULL, &icnt, NULL, "nop", "");
6478892d 5755 expr1.X_add_number = mips_cprestore_offset;
17a2f251 5756 macro_build_ldst_constoffset (NULL, &icnt, &expr1,
f899b4b8
TS
5757 ADDRESS_LOAD_INSN,
5758 mips_gp_register,
256ab948
TS
5759 mips_frame_reg,
5760 HAVE_64BIT_ADDRESSES);
6478892d 5761 }
252b5132
RH
5762 }
5763 }
5764 else if (mips_pic == EMBEDDED_PIC)
5765 {
17a2f251 5766 macro_build (NULL, &icnt, &offset_expr, "bal", "p");
252b5132
RH
5767 /* The linker may expand the call to a longer sequence which
5768 uses $at, so we must break rather than return. */
5769 break;
5770 }
5771 else
5772 abort ();
5773
5774 return;
5775
5776 case M_LB_AB:
5777 s = "lb";
5778 goto ld;
5779 case M_LBU_AB:
5780 s = "lbu";
5781 goto ld;
5782 case M_LH_AB:
5783 s = "lh";
5784 goto ld;
5785 case M_LHU_AB:
5786 s = "lhu";
5787 goto ld;
5788 case M_LW_AB:
5789 s = "lw";
5790 goto ld;
5791 case M_LWC0_AB:
5792 s = "lwc0";
bdaaa2e1 5793 /* Itbl support may require additional care here. */
252b5132
RH
5794 coproc = 1;
5795 goto ld;
5796 case M_LWC1_AB:
5797 s = "lwc1";
bdaaa2e1 5798 /* Itbl support may require additional care here. */
252b5132
RH
5799 coproc = 1;
5800 goto ld;
5801 case M_LWC2_AB:
5802 s = "lwc2";
bdaaa2e1 5803 /* Itbl support may require additional care here. */
252b5132
RH
5804 coproc = 1;
5805 goto ld;
5806 case M_LWC3_AB:
5807 s = "lwc3";
bdaaa2e1 5808 /* Itbl support may require additional care here. */
252b5132
RH
5809 coproc = 1;
5810 goto ld;
5811 case M_LWL_AB:
5812 s = "lwl";
5813 lr = 1;
5814 goto ld;
5815 case M_LWR_AB:
5816 s = "lwr";
5817 lr = 1;
5818 goto ld;
5819 case M_LDC1_AB:
fef14a42 5820 if (mips_opts.arch == CPU_R4650)
252b5132
RH
5821 {
5822 as_bad (_("opcode not supported on this processor"));
5823 return;
5824 }
5825 s = "ldc1";
bdaaa2e1 5826 /* Itbl support may require additional care here. */
252b5132
RH
5827 coproc = 1;
5828 goto ld;
5829 case M_LDC2_AB:
5830 s = "ldc2";
bdaaa2e1 5831 /* Itbl support may require additional care here. */
252b5132
RH
5832 coproc = 1;
5833 goto ld;
5834 case M_LDC3_AB:
5835 s = "ldc3";
bdaaa2e1 5836 /* Itbl support may require additional care here. */
252b5132
RH
5837 coproc = 1;
5838 goto ld;
5839 case M_LDL_AB:
5840 s = "ldl";
5841 lr = 1;
5842 goto ld;
5843 case M_LDR_AB:
5844 s = "ldr";
5845 lr = 1;
5846 goto ld;
5847 case M_LL_AB:
5848 s = "ll";
5849 goto ld;
5850 case M_LLD_AB:
5851 s = "lld";
5852 goto ld;
5853 case M_LWU_AB:
5854 s = "lwu";
5855 ld:
5856 if (breg == treg || coproc || lr)
5857 {
5858 tempreg = AT;
5859 used_at = 1;
5860 }
5861 else
5862 {
5863 tempreg = treg;
5864 used_at = 0;
5865 }
5866 goto ld_st;
5867 case M_SB_AB:
5868 s = "sb";
5869 goto st;
5870 case M_SH_AB:
5871 s = "sh";
5872 goto st;
5873 case M_SW_AB:
5874 s = "sw";
5875 goto st;
5876 case M_SWC0_AB:
5877 s = "swc0";
bdaaa2e1 5878 /* Itbl support may require additional care here. */
252b5132
RH
5879 coproc = 1;
5880 goto st;
5881 case M_SWC1_AB:
5882 s = "swc1";
bdaaa2e1 5883 /* Itbl support may require additional care here. */
252b5132
RH
5884 coproc = 1;
5885 goto st;
5886 case M_SWC2_AB:
5887 s = "swc2";
bdaaa2e1 5888 /* Itbl support may require additional care here. */
252b5132
RH
5889 coproc = 1;
5890 goto st;
5891 case M_SWC3_AB:
5892 s = "swc3";
bdaaa2e1 5893 /* Itbl support may require additional care here. */
252b5132
RH
5894 coproc = 1;
5895 goto st;
5896 case M_SWL_AB:
5897 s = "swl";
5898 goto st;
5899 case M_SWR_AB:
5900 s = "swr";
5901 goto st;
5902 case M_SC_AB:
5903 s = "sc";
5904 goto st;
5905 case M_SCD_AB:
5906 s = "scd";
5907 goto st;
5908 case M_SDC1_AB:
fef14a42 5909 if (mips_opts.arch == CPU_R4650)
252b5132
RH
5910 {
5911 as_bad (_("opcode not supported on this processor"));
5912 return;
5913 }
5914 s = "sdc1";
5915 coproc = 1;
bdaaa2e1 5916 /* Itbl support may require additional care here. */
252b5132
RH
5917 goto st;
5918 case M_SDC2_AB:
5919 s = "sdc2";
bdaaa2e1 5920 /* Itbl support may require additional care here. */
252b5132
RH
5921 coproc = 1;
5922 goto st;
5923 case M_SDC3_AB:
5924 s = "sdc3";
bdaaa2e1 5925 /* Itbl support may require additional care here. */
252b5132
RH
5926 coproc = 1;
5927 goto st;
5928 case M_SDL_AB:
5929 s = "sdl";
5930 goto st;
5931 case M_SDR_AB:
5932 s = "sdr";
5933 st:
5934 tempreg = AT;
5935 used_at = 1;
5936 ld_st:
bdaaa2e1 5937 /* Itbl support may require additional care here. */
252b5132
RH
5938 if (mask == M_LWC1_AB
5939 || mask == M_SWC1_AB
5940 || mask == M_LDC1_AB
5941 || mask == M_SDC1_AB
5942 || mask == M_L_DAB
5943 || mask == M_S_DAB)
5944 fmt = "T,o(b)";
5945 else if (coproc)
5946 fmt = "E,o(b)";
5947 else
5948 fmt = "t,o(b)";
5949
256ab948
TS
5950 /* Sign-extending 32-bit constants makes their handling easier.
5951 The HAVE_64BIT_GPRS... part is due to the linux kernel hack
5952 described below. */
5953 if ((! HAVE_64BIT_ADDRESSES
5954 && (! HAVE_64BIT_GPRS && offset_expr.X_op == O_constant))
d17e7bce
TS
5955 && (offset_expr.X_op == O_constant)
5956 && ! ((offset_expr.X_add_number & ~((bfd_vma) 0x7fffffff))
5957 == ~((bfd_vma) 0x7fffffff)))
ae826530 5958 {
d17e7bce 5959 if (offset_expr.X_add_number & ~((bfd_vma) 0xffffffff))
1b8e29e5 5960 as_bad (_("constant too large"));
ae826530 5961
1b8e29e5
TS
5962 offset_expr.X_add_number = (((offset_expr.X_add_number & 0xffffffff)
5963 ^ 0x80000000) - 0x80000000);
ae826530 5964 }
256ab948 5965
afdbd6d0
CD
5966 /* For embedded PIC, we allow loads where the offset is calculated
5967 by subtracting a symbol in the current segment from an unknown
5968 symbol, relative to a base register, e.g.:
5969 <op> $treg, <sym>-<localsym>($breg)
5970 This is used by the compiler for switch statements. */
76b3015f 5971 if (mips_pic == EMBEDDED_PIC
afdbd6d0
CD
5972 && offset_expr.X_op == O_subtract
5973 && (symbol_constant_p (offset_expr.X_op_symbol)
5974 ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
5975 : (symbol_equated_p (offset_expr.X_op_symbol)
5976 && (S_GET_SEGMENT
5977 (symbol_get_value_expression (offset_expr.X_op_symbol)
5978 ->X_add_symbol)
5979 == now_seg)))
5980 && breg != 0
5981 && (offset_expr.X_add_number == 0
5982 || OUTPUT_FLAVOR == bfd_target_elf_flavour))
5983 {
5984 /* For this case, we output the instructions:
5985 lui $tempreg,<sym> (BFD_RELOC_PCREL_HI16_S)
5986 addiu $tempreg,$tempreg,$breg
5987 <op> $treg,<sym>($tempreg) (BFD_RELOC_PCREL_LO16)
5988 If the relocation would fit entirely in 16 bits, it would be
5989 nice to emit:
5990 <op> $treg,<sym>($breg) (BFD_RELOC_PCREL_LO16)
5991 instead, but that seems quite difficult. */
17a2f251
TS
5992 macro_build (NULL, &icnt, &offset_expr, "lui", "t,u", tempreg,
5993 BFD_RELOC_PCREL_HI16_S);
5994 macro_build (NULL, &icnt, NULL,
afdbd6d0
CD
5995 ((bfd_arch_bits_per_address (stdoutput) == 32
5996 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
b7c7d6c1 5997 ? "addu" : "daddu"),
afdbd6d0 5998 "d,v,t", tempreg, tempreg, breg);
17a2f251
TS
5999 macro_build (NULL, &icnt, &offset_expr, s, fmt, treg,
6000 BFD_RELOC_PCREL_LO16, tempreg);
afdbd6d0
CD
6001 if (! used_at)
6002 return;
6003 break;
6004 }
6005
252b5132
RH
6006 if (offset_expr.X_op != O_constant
6007 && offset_expr.X_op != O_symbol)
6008 {
6009 as_bad (_("expression too complex"));
6010 offset_expr.X_op = O_constant;
6011 }
6012
6013 /* A constant expression in PIC code can be handled just as it
6014 is in non PIC code. */
6015 if (mips_pic == NO_PIC
6016 || offset_expr.X_op == O_constant)
6017 {
f9419b05
TS
6018 char *p;
6019
252b5132
RH
6020 /* If this is a reference to a GP relative symbol, and there
6021 is no base register, we want
cdf6fd85 6022 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
6023 Otherwise, if there is no base register, we want
6024 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
6025 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6026 If we have a constant, we need two instructions anyhow,
6027 so we always use the latter form.
6028
6029 If we have a base register, and this is a reference to a
6030 GP relative symbol, we want
6031 addu $tempreg,$breg,$gp
cdf6fd85 6032 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
6033 Otherwise we want
6034 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
6035 addu $tempreg,$tempreg,$breg
6036 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245 6037 With a constant we always use the latter case.
76b3015f 6038
d6bc6245
TS
6039 With 64bit address space and no base register and $at usable,
6040 we want
6041 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
6042 lui $at,<sym> (BFD_RELOC_HI16_S)
6043 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
6044 dsll32 $tempreg,0
6045 daddu $tempreg,$at
6046 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6047 If we have a base register, we want
6048 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
6049 lui $at,<sym> (BFD_RELOC_HI16_S)
6050 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
6051 daddu $at,$breg
6052 dsll32 $tempreg,0
6053 daddu $tempreg,$at
6054 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6055
6056 Without $at we can't generate the optimal path for superscalar
6057 processors here since this would require two temporary registers.
6058 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
6059 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
6060 dsll $tempreg,16
6061 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
6062 dsll $tempreg,16
6063 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6064 If we have a base register, we want
6065 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
6066 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
6067 dsll $tempreg,16
6068 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
6069 dsll $tempreg,16
6070 daddu $tempreg,$tempreg,$breg
6071 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6373ee54
CD
6072
6073 If we have 64-bit addresses, as an optimization, for
6074 addresses which are 32-bit constants (e.g. kseg0/kseg1
6075 addresses) we fall back to the 32-bit address generation
78d32a17
MR
6076 mechanism since it is more efficient. Note that due to
6077 the signed offset used by memory operations, the 32-bit
6078 range is shifted down by 32768 here. This code should
6373ee54
CD
6079 probably attempt to generate 64-bit constants more
6080 efficiently in general.
9214dd3b
AO
6081
6082 As an extension for architectures with 64-bit registers,
6083 we don't truncate 64-bit addresses given as literal
6084 constants down to 32 bits, to support existing practice
6085 in the mips64 Linux (the kernel), that compiles source
6086 files with -mabi=64, assembling them as o32 or n32 (with
6087 -Wa,-32 or -Wa,-n32). This is not beautiful, but since
6088 the whole kernel is loaded into a memory region that is
67c1ffbe 6089 addressable with sign-extended 32-bit addresses, it is
9214dd3b
AO
6090 wasteful to compute the upper 32 bits of every
6091 non-literal address, that takes more space and time.
6092 Some day this should probably be implemented as an
6093 assembler option, such that the kernel doesn't have to
6094 use such ugly hacks, even though it will still have to
6095 end up converting the binary to ELF32 for a number of
6096 platforms whose boot loaders don't support ELF64
6097 binaries. */
256ab948
TS
6098 if ((HAVE_64BIT_ADDRESSES
6099 && ! (offset_expr.X_op == O_constant
6100 && IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000)))
6101 || (HAVE_64BIT_GPRS
6102 && offset_expr.X_op == O_constant
6103 && ! IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000)))
d6bc6245
TS
6104 {
6105 p = NULL;
6106
6107 /* We don't do GP optimization for now because RELAX_ENCODE can't
6108 hold the data for such large chunks. */
6109
460597ba 6110 if (used_at == 0 && ! mips_opts.noat)
d6bc6245
TS
6111 {
6112 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
17a2f251 6113 tempreg, BFD_RELOC_MIPS_HIGHEST);
d6bc6245 6114 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
17a2f251 6115 AT, BFD_RELOC_HI16_S);
d6bc6245 6116 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
17a2f251 6117 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
d6bc6245 6118 if (breg != 0)
17a2f251
TS
6119 macro_build (p, &icnt, NULL, "daddu", "d,v,t",
6120 AT, AT, breg);
6121 macro_build (p, &icnt, NULL, "dsll32", "d,w,<",
6122 tempreg, tempreg, 0);
6123 macro_build (p, &icnt, NULL, "daddu", "d,v,t",
6124 tempreg, tempreg, AT);
6125 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
6126 BFD_RELOC_LO16, tempreg);
d6bc6245
TS
6127 used_at = 1;
6128 }
6129 else
6130 {
6131 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
17a2f251 6132 tempreg, BFD_RELOC_MIPS_HIGHEST);
d6bc6245 6133 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
17a2f251
TS
6134 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
6135 macro_build (p, &icnt, NULL, "dsll", "d,w,<",
6136 tempreg, tempreg, 16);
d6bc6245 6137 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
17a2f251
TS
6138 tempreg, tempreg, BFD_RELOC_HI16_S);
6139 macro_build (p, &icnt, NULL, "dsll", "d,w,<",
6140 tempreg, tempreg, 16);
d6bc6245 6141 if (breg != 0)
17a2f251
TS
6142 macro_build (p, &icnt, NULL, "daddu", "d,v,t",
6143 tempreg, tempreg, breg);
6144 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
6145 BFD_RELOC_LO16, tempreg);
d6bc6245
TS
6146 }
6147
6148 return;
6149 }
256ab948
TS
6150
6151 if (offset_expr.X_op == O_constant
ecd13cd3 6152 && ! IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000))
5a7a0b7b 6153 as_bad (_("load/store address overflow (max 32 bits)"));
76b3015f 6154
252b5132
RH
6155 if (breg == 0)
6156 {
e7d556df 6157 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
252b5132
RH
6158 || nopic_need_relax (offset_expr.X_add_symbol, 1))
6159 p = NULL;
6160 else
6161 {
6162 frag_grow (20);
17a2f251
TS
6163 macro_build (NULL, &icnt, &offset_expr, s, fmt, treg,
6164 BFD_RELOC_GPREL16, mips_gp_register);
252b5132
RH
6165 p = frag_var (rs_machine_dependent, 8, 0,
6166 RELAX_ENCODE (4, 8, 0, 4, 0,
6167 (mips_opts.warn_about_macros
6168 || (used_at
6169 && mips_opts.noat))),
956cd1d6 6170 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
6171 used_at = 0;
6172 }
6173 macro_build_lui (p, &icnt, &offset_expr, tempreg);
6174 if (p != NULL)
6175 p += 4;
6176 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
17a2f251 6177 BFD_RELOC_LO16, tempreg);
252b5132
RH
6178 }
6179 else
6180 {
e7d556df 6181 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
252b5132
RH
6182 || nopic_need_relax (offset_expr.X_add_symbol, 1))
6183 p = NULL;
6184 else
6185 {
6186 frag_grow (28);
17a2f251
TS
6187 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6188 tempreg, breg, mips_gp_register);
6189 macro_build (NULL, &icnt, &offset_expr, s, fmt, treg,
6190 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6191 p = frag_var (rs_machine_dependent, 12, 0,
6192 RELAX_ENCODE (8, 12, 0, 8, 0, 0),
956cd1d6 6193 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
6194 }
6195 macro_build_lui (p, &icnt, &offset_expr, tempreg);
6196 if (p != NULL)
6197 p += 4;
17a2f251
TS
6198 macro_build (p, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6199 tempreg, tempreg, breg);
252b5132
RH
6200 if (p != NULL)
6201 p += 4;
6202 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
17a2f251 6203 BFD_RELOC_LO16, tempreg);
252b5132
RH
6204 }
6205 }
6206 else if (mips_pic == SVR4_PIC && ! mips_big_got)
6207 {
f9419b05 6208 char *p;
ed6fb7bd 6209 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
f9419b05 6210
252b5132
RH
6211 /* If this is a reference to an external symbol, we want
6212 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6213 nop
6214 <op> $treg,0($tempreg)
6215 Otherwise we want
6216 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6217 nop
6218 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6219 <op> $treg,0($tempreg)
f5040a92
AO
6220
6221 For NewABI, we want
6222 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6223 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
6224
252b5132
RH
6225 If there is a base register, we add it to $tempreg before
6226 the <op>. If there is a constant, we stick it in the
6227 <op> instruction. We don't handle constants larger than
6228 16 bits, because we have no way to load the upper 16 bits
6229 (actually, we could handle them for the subset of cases
6230 in which we are not using $at). */
6231 assert (offset_expr.X_op == O_symbol);
f5040a92
AO
6232 if (HAVE_NEWABI)
6233 {
17a2f251
TS
6234 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
6235 "t,o(b)", tempreg, BFD_RELOC_MIPS_GOT_PAGE,
6236 mips_gp_register);
f5040a92 6237 if (breg != 0)
17a2f251
TS
6238 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6239 tempreg, tempreg, breg);
6240 macro_build (NULL, &icnt, &offset_expr, s, fmt, treg,
6241 BFD_RELOC_MIPS_GOT_OFST, tempreg);
f5040a92
AO
6242
6243 if (! used_at)
6244 return;
6245
6246 break;
6247 }
252b5132
RH
6248 expr1.X_add_number = offset_expr.X_add_number;
6249 offset_expr.X_add_number = 0;
6250 if (expr1.X_add_number < -0x8000
6251 || expr1.X_add_number >= 0x8000)
6252 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6253 frag_grow (20);
17a2f251
TS
6254 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6255 tempreg, lw_reloc_type, mips_gp_register);
6256 macro_build (NULL, &icnt, NULL, "nop", "");
bdaaa2e1 6257 p = frag_var (rs_machine_dependent, 4, 0,
252b5132 6258 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
c4e7957c 6259 offset_expr.X_add_symbol, 0, NULL);
f899b4b8 6260 macro_build (p, &icnt, &offset_expr, ADDRESS_ADDI_INSN,
17a2f251 6261 "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
252b5132 6262 if (breg != 0)
17a2f251
TS
6263 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6264 tempreg, tempreg, breg);
6265 macro_build (NULL, &icnt, &expr1, s, fmt, treg, BFD_RELOC_LO16,
6266 tempreg);
252b5132 6267 }
f5040a92 6268 else if (mips_pic == SVR4_PIC && ! HAVE_NEWABI)
252b5132
RH
6269 {
6270 int gpdel;
f9419b05 6271 char *p;
252b5132
RH
6272
6273 /* If this is a reference to an external symbol, we want
6274 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6275 addu $tempreg,$tempreg,$gp
6276 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6277 <op> $treg,0($tempreg)
6278 Otherwise we want
6279 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6280 nop
6281 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6282 <op> $treg,0($tempreg)
6283 If there is a base register, we add it to $tempreg before
6284 the <op>. If there is a constant, we stick it in the
6285 <op> instruction. We don't handle constants larger than
6286 16 bits, because we have no way to load the upper 16 bits
6287 (actually, we could handle them for the subset of cases
f5040a92 6288 in which we are not using $at). */
252b5132
RH
6289 assert (offset_expr.X_op == O_symbol);
6290 expr1.X_add_number = offset_expr.X_add_number;
6291 offset_expr.X_add_number = 0;
6292 if (expr1.X_add_number < -0x8000
6293 || expr1.X_add_number >= 0x8000)
6294 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
f7ea7ef2 6295 if (reg_needs_delay (mips_gp_register))
252b5132
RH
6296 gpdel = 4;
6297 else
6298 gpdel = 0;
6299 frag_grow (36);
17a2f251
TS
6300 macro_build (NULL, &icnt, &offset_expr, "lui", "t,u", tempreg,
6301 BFD_RELOC_MIPS_GOT_HI16);
6302 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6303 tempreg, tempreg, mips_gp_register);
6304 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6305 tempreg, BFD_RELOC_MIPS_GOT_LO16, tempreg);
252b5132
RH
6306 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
6307 RELAX_ENCODE (12, 12 + gpdel, gpdel, 8 + gpdel, 0, 0),
c4e7957c 6308 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
6309 if (gpdel > 0)
6310 {
17a2f251 6311 macro_build (p, &icnt, NULL, "nop", "");
252b5132
RH
6312 p += 4;
6313 }
17a2f251
TS
6314 macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6315 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132 6316 p += 4;
17a2f251 6317 macro_build (p, &icnt, NULL, "nop", "");
252b5132 6318 p += 4;
17a2f251
TS
6319 macro_build (p, &icnt, &offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
6320 tempreg, tempreg, BFD_RELOC_LO16);
252b5132 6321 if (breg != 0)
17a2f251
TS
6322 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6323 tempreg, tempreg, breg);
6324 macro_build (NULL, &icnt, &expr1, s, fmt, treg, BFD_RELOC_LO16,
6325 tempreg);
252b5132 6326 }
f5040a92
AO
6327 else if (mips_pic == SVR4_PIC && HAVE_NEWABI)
6328 {
6329 char *p;
6330 int bregsz = breg != 0 ? 4 : 0;
6331
6332 /* If this is a reference to an external symbol, we want
6333 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6334 add $tempreg,$tempreg,$gp
6335 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6336 <op> $treg,<ofst>($tempreg)
6337 Otherwise, for local symbols, we want:
6338 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6339 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
6340 assert (offset_expr.X_op == O_symbol);
0a6ace1e 6341 frag_grow (36);
f5040a92
AO
6342 frag_now->tc_frag_data.tc_fr_offset =
6343 expr1.X_add_number = offset_expr.X_add_number;
6344 offset_expr.X_add_number = 0;
6345 if (expr1.X_add_number < -0x8000
6346 || expr1.X_add_number >= 0x8000)
6347 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
17a2f251
TS
6348 macro_build (NULL, &icnt, &offset_expr, "lui", "t,u", tempreg,
6349 BFD_RELOC_MIPS_GOT_HI16);
6350 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6351 tempreg, tempreg, mips_gp_register);
6352 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6353 tempreg, BFD_RELOC_MIPS_GOT_LO16, tempreg);
f5040a92 6354 if (breg != 0)
17a2f251
TS
6355 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6356 tempreg, tempreg, breg);
6357 macro_build (NULL, &icnt, &expr1, s, fmt, treg, BFD_RELOC_LO16,
6358 tempreg);
684022ea 6359
f5040a92
AO
6360 offset_expr.X_add_number = expr1.X_add_number;
6361 p = frag_var (rs_machine_dependent, 12 + bregsz, 0,
6362 RELAX_ENCODE (16 + bregsz, 8 + bregsz,
6363 0, 4 + bregsz, 0, 0),
6364 offset_expr.X_add_symbol, 0, NULL);
f899b4b8 6365 macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 6366 tempreg, BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 6367 if (breg != 0)
17a2f251
TS
6368 macro_build (p + 4, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6369 tempreg, tempreg, breg);
f5040a92 6370 macro_build (p + 4 + bregsz, &icnt, &offset_expr, s, fmt, treg,
17a2f251 6371 BFD_RELOC_MIPS_GOT_OFST, tempreg);
f5040a92 6372 }
252b5132
RH
6373 else if (mips_pic == EMBEDDED_PIC)
6374 {
6375 /* If there is no base register, we want
cdf6fd85 6376 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
6377 If there is a base register, we want
6378 addu $tempreg,$breg,$gp
cdf6fd85 6379 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
6380 */
6381 assert (offset_expr.X_op == O_symbol);
6382 if (breg == 0)
6383 {
17a2f251
TS
6384 macro_build (NULL, &icnt, &offset_expr, s, fmt, treg,
6385 BFD_RELOC_GPREL16, mips_gp_register);
252b5132
RH
6386 used_at = 0;
6387 }
6388 else
6389 {
17a2f251
TS
6390 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6391 tempreg, breg, mips_gp_register);
6392 macro_build (NULL, &icnt, &offset_expr, s, fmt, treg,
6393 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6394 }
6395 }
6396 else
6397 abort ();
6398
6399 if (! used_at)
6400 return;
6401
6402 break;
6403
6404 case M_LI:
6405 case M_LI_S:
6406 load_register (&icnt, treg, &imm_expr, 0);
6407 return;
6408
6409 case M_DLI:
6410 load_register (&icnt, treg, &imm_expr, 1);
6411 return;
6412
6413 case M_LI_SS:
6414 if (imm_expr.X_op == O_constant)
6415 {
6416 load_register (&icnt, AT, &imm_expr, 0);
17a2f251 6417 macro_build (NULL, &icnt, NULL, "mtc1", "t,G", AT, treg);
252b5132
RH
6418 break;
6419 }
6420 else
6421 {
6422 assert (offset_expr.X_op == O_symbol
6423 && strcmp (segment_name (S_GET_SEGMENT
6424 (offset_expr.X_add_symbol)),
6425 ".lit4") == 0
6426 && offset_expr.X_add_number == 0);
17a2f251
TS
6427 macro_build (NULL, &icnt, &offset_expr, "lwc1", "T,o(b)", treg,
6428 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
252b5132
RH
6429 return;
6430 }
6431
6432 case M_LI_D:
ca4e0257
RS
6433 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
6434 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
6435 order 32 bits of the value and the low order 32 bits are either
6436 zero or in OFFSET_EXPR. */
252b5132
RH
6437 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6438 {
ca4e0257 6439 if (HAVE_64BIT_GPRS)
252b5132
RH
6440 load_register (&icnt, treg, &imm_expr, 1);
6441 else
6442 {
6443 int hreg, lreg;
6444
6445 if (target_big_endian)
6446 {
6447 hreg = treg;
6448 lreg = treg + 1;
6449 }
6450 else
6451 {
6452 hreg = treg + 1;
6453 lreg = treg;
6454 }
6455
6456 if (hreg <= 31)
6457 load_register (&icnt, hreg, &imm_expr, 0);
6458 if (lreg <= 31)
6459 {
6460 if (offset_expr.X_op == O_absent)
ea1fb5dc 6461 move_register (&icnt, lreg, 0);
252b5132
RH
6462 else
6463 {
6464 assert (offset_expr.X_op == O_constant);
6465 load_register (&icnt, lreg, &offset_expr, 0);
6466 }
6467 }
6468 }
6469 return;
6470 }
6471
6472 /* We know that sym is in the .rdata section. First we get the
6473 upper 16 bits of the address. */
6474 if (mips_pic == NO_PIC)
6475 {
956cd1d6 6476 macro_build_lui (NULL, &icnt, &offset_expr, AT);
252b5132
RH
6477 }
6478 else if (mips_pic == SVR4_PIC)
6479 {
17a2f251
TS
6480 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6481 AT, BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132
RH
6482 }
6483 else if (mips_pic == EMBEDDED_PIC)
6484 {
6485 /* For embedded PIC we pick up the entire address off $gp in
6486 a single instruction. */
17a2f251
TS
6487 macro_build (NULL, &icnt, &offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
6488 AT, mips_gp_register, BFD_RELOC_GPREL16);
252b5132
RH
6489 offset_expr.X_op = O_constant;
6490 offset_expr.X_add_number = 0;
6491 }
6492 else
6493 abort ();
bdaaa2e1 6494
252b5132 6495 /* Now we load the register(s). */
ca4e0257 6496 if (HAVE_64BIT_GPRS)
17a2f251
TS
6497 macro_build (NULL, &icnt, &offset_expr, "ld", "t,o(b)", treg,
6498 BFD_RELOC_LO16, AT);
252b5132
RH
6499 else
6500 {
17a2f251
TS
6501 macro_build (NULL, &icnt, &offset_expr, "lw", "t,o(b)", treg,
6502 BFD_RELOC_LO16, AT);
f9419b05 6503 if (treg != RA)
252b5132
RH
6504 {
6505 /* FIXME: How in the world do we deal with the possible
6506 overflow here? */
6507 offset_expr.X_add_number += 4;
17a2f251
TS
6508 macro_build (NULL, &icnt, &offset_expr, "lw", "t,o(b)",
6509 treg + 1, BFD_RELOC_LO16, AT);
252b5132
RH
6510 }
6511 }
6512
6513 /* To avoid confusion in tc_gen_reloc, we must ensure that this
6514 does not become a variant frag. */
6515 frag_wane (frag_now);
6516 frag_new (0);
6517
6518 break;
6519
6520 case M_LI_DD:
ca4e0257
RS
6521 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
6522 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
6523 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
6524 the value and the low order 32 bits are either zero or in
6525 OFFSET_EXPR. */
252b5132
RH
6526 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6527 {
ca4e0257
RS
6528 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_FPRS);
6529 if (HAVE_64BIT_FPRS)
6530 {
6531 assert (HAVE_64BIT_GPRS);
17a2f251 6532 macro_build (NULL, &icnt, NULL, "dmtc1", "t,S", AT, treg);
ca4e0257 6533 }
252b5132
RH
6534 else
6535 {
17a2f251 6536 macro_build (NULL, &icnt, NULL, "mtc1", "t,G", AT, treg + 1);
252b5132 6537 if (offset_expr.X_op == O_absent)
17a2f251 6538 macro_build (NULL, &icnt, NULL, "mtc1", "t,G", 0, treg);
252b5132
RH
6539 else
6540 {
6541 assert (offset_expr.X_op == O_constant);
6542 load_register (&icnt, AT, &offset_expr, 0);
17a2f251 6543 macro_build (NULL, &icnt, NULL, "mtc1", "t,G", AT, treg);
252b5132
RH
6544 }
6545 }
6546 break;
6547 }
6548
6549 assert (offset_expr.X_op == O_symbol
6550 && offset_expr.X_add_number == 0);
6551 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
6552 if (strcmp (s, ".lit8") == 0)
6553 {
e7af610e 6554 if (mips_opts.isa != ISA_MIPS1)
252b5132 6555 {
17a2f251
TS
6556 macro_build (NULL, &icnt, &offset_expr, "ldc1", "T,o(b)", treg,
6557 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
252b5132
RH
6558 return;
6559 }
c9914766 6560 breg = mips_gp_register;
252b5132
RH
6561 r = BFD_RELOC_MIPS_LITERAL;
6562 goto dob;
6563 }
6564 else
6565 {
6566 assert (strcmp (s, RDATA_SECTION_NAME) == 0);
6567 if (mips_pic == SVR4_PIC)
17a2f251
TS
6568 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN,
6569 "t,o(b)", AT, BFD_RELOC_MIPS_GOT16,
6570 mips_gp_register);
252b5132
RH
6571 else
6572 {
6573 /* FIXME: This won't work for a 64 bit address. */
956cd1d6 6574 macro_build_lui (NULL, &icnt, &offset_expr, AT);
252b5132 6575 }
bdaaa2e1 6576
e7af610e 6577 if (mips_opts.isa != ISA_MIPS1)
252b5132 6578 {
17a2f251
TS
6579 macro_build (NULL, &icnt, &offset_expr, "ldc1", "T,o(b)", treg,
6580 BFD_RELOC_LO16, AT);
252b5132
RH
6581
6582 /* To avoid confusion in tc_gen_reloc, we must ensure
6583 that this does not become a variant frag. */
6584 frag_wane (frag_now);
6585 frag_new (0);
6586
6587 break;
6588 }
6589 breg = AT;
6590 r = BFD_RELOC_LO16;
6591 goto dob;
6592 }
6593
6594 case M_L_DOB:
fef14a42 6595 if (mips_opts.arch == CPU_R4650)
252b5132
RH
6596 {
6597 as_bad (_("opcode not supported on this processor"));
6598 return;
6599 }
6600 /* Even on a big endian machine $fn comes before $fn+1. We have
6601 to adjust when loading from memory. */
6602 r = BFD_RELOC_LO16;
6603 dob:
e7af610e 6604 assert (mips_opts.isa == ISA_MIPS1);
17a2f251
TS
6605 macro_build (NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6606 target_big_endian ? treg + 1 : treg, r, breg);
252b5132
RH
6607 /* FIXME: A possible overflow which I don't know how to deal
6608 with. */
6609 offset_expr.X_add_number += 4;
17a2f251
TS
6610 macro_build (NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6611 target_big_endian ? treg : treg + 1, r, breg);
252b5132
RH
6612
6613 /* To avoid confusion in tc_gen_reloc, we must ensure that this
6614 does not become a variant frag. */
6615 frag_wane (frag_now);
6616 frag_new (0);
6617
6618 if (breg != AT)
6619 return;
6620 break;
6621
6622 case M_L_DAB:
6623 /*
6624 * The MIPS assembler seems to check for X_add_number not
6625 * being double aligned and generating:
6626 * lui at,%hi(foo+1)
6627 * addu at,at,v1
6628 * addiu at,at,%lo(foo+1)
6629 * lwc1 f2,0(at)
6630 * lwc1 f3,4(at)
6631 * But, the resulting address is the same after relocation so why
6632 * generate the extra instruction?
6633 */
fef14a42 6634 if (mips_opts.arch == CPU_R4650)
252b5132
RH
6635 {
6636 as_bad (_("opcode not supported on this processor"));
6637 return;
6638 }
bdaaa2e1 6639 /* Itbl support may require additional care here. */
252b5132 6640 coproc = 1;
e7af610e 6641 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
6642 {
6643 s = "ldc1";
6644 goto ld;
6645 }
6646
6647 s = "lwc1";
6648 fmt = "T,o(b)";
6649 goto ldd_std;
6650
6651 case M_S_DAB:
fef14a42 6652 if (mips_opts.arch == CPU_R4650)
252b5132
RH
6653 {
6654 as_bad (_("opcode not supported on this processor"));
6655 return;
6656 }
6657
e7af610e 6658 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
6659 {
6660 s = "sdc1";
6661 goto st;
6662 }
6663
6664 s = "swc1";
6665 fmt = "T,o(b)";
bdaaa2e1 6666 /* Itbl support may require additional care here. */
252b5132
RH
6667 coproc = 1;
6668 goto ldd_std;
6669
6670 case M_LD_AB:
ca4e0257 6671 if (HAVE_64BIT_GPRS)
252b5132
RH
6672 {
6673 s = "ld";
6674 goto ld;
6675 }
6676
6677 s = "lw";
6678 fmt = "t,o(b)";
6679 goto ldd_std;
6680
6681 case M_SD_AB:
ca4e0257 6682 if (HAVE_64BIT_GPRS)
252b5132
RH
6683 {
6684 s = "sd";
6685 goto st;
6686 }
6687
6688 s = "sw";
6689 fmt = "t,o(b)";
6690
6691 ldd_std:
afdbd6d0
CD
6692 /* We do _not_ bother to allow embedded PIC (symbol-local_symbol)
6693 loads for the case of doing a pair of loads to simulate an 'ld'.
6694 This is not currently done by the compiler, and assembly coders
6695 writing embedded-pic code can cope. */
6696
252b5132
RH
6697 if (offset_expr.X_op != O_symbol
6698 && offset_expr.X_op != O_constant)
6699 {
6700 as_bad (_("expression too complex"));
6701 offset_expr.X_op = O_constant;
6702 }
6703
6704 /* Even on a big endian machine $fn comes before $fn+1. We have
6705 to adjust when loading from memory. We set coproc if we must
6706 load $fn+1 first. */
bdaaa2e1 6707 /* Itbl support may require additional care here. */
252b5132
RH
6708 if (! target_big_endian)
6709 coproc = 0;
6710
6711 if (mips_pic == NO_PIC
6712 || offset_expr.X_op == O_constant)
6713 {
f9419b05
TS
6714 char *p;
6715
252b5132 6716 /* If this is a reference to a GP relative symbol, we want
cdf6fd85
TS
6717 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6718 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
6719 If we have a base register, we use this
6720 addu $at,$breg,$gp
cdf6fd85
TS
6721 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6722 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
6723 If this is not a GP relative symbol, we want
6724 lui $at,<sym> (BFD_RELOC_HI16_S)
6725 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6726 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6727 If there is a base register, we add it to $at after the
6728 lui instruction. If there is a constant, we always use
6729 the last case. */
e7d556df 6730 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
252b5132
RH
6731 || nopic_need_relax (offset_expr.X_add_symbol, 1))
6732 {
6733 p = NULL;
6734 used_at = 1;
6735 }
6736 else
6737 {
6738 int off;
6739
6740 if (breg == 0)
6741 {
6742 frag_grow (28);
c9914766 6743 tempreg = mips_gp_register;
252b5132
RH
6744 off = 0;
6745 used_at = 0;
6746 }
6747 else
6748 {
6749 frag_grow (36);
17a2f251
TS
6750 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6751 AT, breg, mips_gp_register);
252b5132
RH
6752 tempreg = AT;
6753 off = 4;
6754 used_at = 1;
6755 }
6756
beae10d5 6757 /* Itbl support may require additional care here. */
17a2f251 6758 macro_build (NULL, &icnt, &offset_expr, s, fmt,
252b5132 6759 coproc ? treg + 1 : treg,
17a2f251 6760 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6761 offset_expr.X_add_number += 4;
6762
6763 /* Set mips_optimize to 2 to avoid inserting an
6764 undesired nop. */
6765 hold_mips_optimize = mips_optimize;
6766 mips_optimize = 2;
beae10d5 6767 /* Itbl support may require additional care here. */
17a2f251 6768 macro_build (NULL, &icnt, &offset_expr, s, fmt,
252b5132 6769 coproc ? treg : treg + 1,
17a2f251 6770 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6771 mips_optimize = hold_mips_optimize;
6772
6773 p = frag_var (rs_machine_dependent, 12 + off, 0,
6774 RELAX_ENCODE (8 + off, 12 + off, 0, 4 + off, 1,
6775 used_at && mips_opts.noat),
956cd1d6 6776 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
6777
6778 /* We just generated two relocs. When tc_gen_reloc
6779 handles this case, it will skip the first reloc and
6780 handle the second. The second reloc already has an
6781 extra addend of 4, which we added above. We must
6782 subtract it out, and then subtract another 4 to make
6783 the first reloc come out right. The second reloc
6784 will come out right because we are going to add 4 to
6785 offset_expr when we build its instruction below.
6786
6787 If we have a symbol, then we don't want to include
6788 the offset, because it will wind up being included
6789 when we generate the reloc. */
6790
6791 if (offset_expr.X_op == O_constant)
6792 offset_expr.X_add_number -= 8;
6793 else
6794 {
6795 offset_expr.X_add_number = -4;
6796 offset_expr.X_op = O_constant;
6797 }
6798 }
6799 macro_build_lui (p, &icnt, &offset_expr, AT);
6800 if (p != NULL)
6801 p += 4;
6802 if (breg != 0)
6803 {
17a2f251
TS
6804 macro_build (p, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6805 AT, breg, AT);
252b5132
RH
6806 if (p != NULL)
6807 p += 4;
6808 }
beae10d5 6809 /* Itbl support may require additional care here. */
252b5132
RH
6810 macro_build (p, &icnt, &offset_expr, s, fmt,
6811 coproc ? treg + 1 : treg,
17a2f251 6812 BFD_RELOC_LO16, AT);
252b5132
RH
6813 if (p != NULL)
6814 p += 4;
6815 /* FIXME: How do we handle overflow here? */
6816 offset_expr.X_add_number += 4;
beae10d5 6817 /* Itbl support may require additional care here. */
252b5132
RH
6818 macro_build (p, &icnt, &offset_expr, s, fmt,
6819 coproc ? treg : treg + 1,
17a2f251 6820 BFD_RELOC_LO16, AT);
bdaaa2e1 6821 }
252b5132
RH
6822 else if (mips_pic == SVR4_PIC && ! mips_big_got)
6823 {
6824 int off;
6825
6826 /* If this is a reference to an external symbol, we want
6827 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6828 nop
6829 <op> $treg,0($at)
6830 <op> $treg+1,4($at)
6831 Otherwise we want
6832 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6833 nop
6834 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6835 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6836 If there is a base register we add it to $at before the
6837 lwc1 instructions. If there is a constant we include it
6838 in the lwc1 instructions. */
6839 used_at = 1;
6840 expr1.X_add_number = offset_expr.X_add_number;
6841 offset_expr.X_add_number = 0;
6842 if (expr1.X_add_number < -0x8000
6843 || expr1.X_add_number >= 0x8000 - 4)
6844 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6845 if (breg == 0)
6846 off = 0;
6847 else
6848 off = 4;
6849 frag_grow (24 + off);
17a2f251
TS
6850 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6851 AT, BFD_RELOC_MIPS_GOT16, mips_gp_register);
6852 macro_build (NULL, &icnt, NULL, "nop", "");
252b5132 6853 if (breg != 0)
17a2f251
TS
6854 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6855 AT, breg, AT);
beae10d5 6856 /* Itbl support may require additional care here. */
17a2f251
TS
6857 macro_build (NULL, &icnt, &expr1, s, fmt, coproc ? treg + 1 : treg,
6858 BFD_RELOC_LO16, AT);
252b5132
RH
6859 expr1.X_add_number += 4;
6860
6861 /* Set mips_optimize to 2 to avoid inserting an undesired
6862 nop. */
6863 hold_mips_optimize = mips_optimize;
6864 mips_optimize = 2;
beae10d5 6865 /* Itbl support may require additional care here. */
17a2f251
TS
6866 macro_build (NULL, &icnt, &expr1, s, fmt, coproc ? treg : treg + 1,
6867 BFD_RELOC_LO16, AT);
252b5132
RH
6868 mips_optimize = hold_mips_optimize;
6869
6870 (void) frag_var (rs_machine_dependent, 0, 0,
6871 RELAX_ENCODE (0, 0, -16 - off, -8, 1, 0),
c4e7957c 6872 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
6873 }
6874 else if (mips_pic == SVR4_PIC)
6875 {
6876 int gpdel, off;
f9419b05 6877 char *p;
252b5132
RH
6878
6879 /* If this is a reference to an external symbol, we want
6880 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6881 addu $at,$at,$gp
6882 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
6883 nop
6884 <op> $treg,0($at)
6885 <op> $treg+1,4($at)
6886 Otherwise we want
6887 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6888 nop
6889 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6890 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6891 If there is a base register we add it to $at before the
6892 lwc1 instructions. If there is a constant we include it
6893 in the lwc1 instructions. */
6894 used_at = 1;
6895 expr1.X_add_number = offset_expr.X_add_number;
6896 offset_expr.X_add_number = 0;
6897 if (expr1.X_add_number < -0x8000
6898 || expr1.X_add_number >= 0x8000 - 4)
6899 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
c9914766 6900 if (reg_needs_delay (mips_gp_register))
252b5132
RH
6901 gpdel = 4;
6902 else
6903 gpdel = 0;
6904 if (breg == 0)
6905 off = 0;
6906 else
6907 off = 4;
6908 frag_grow (56);
17a2f251
TS
6909 macro_build (NULL, &icnt, &offset_expr, "lui", "t,u", AT,
6910 BFD_RELOC_MIPS_GOT_HI16);
6911 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6912 AT, AT, mips_gp_register);
6913 macro_build (NULL, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6914 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
6915 macro_build (NULL, &icnt, NULL, "nop", "");
252b5132 6916 if (breg != 0)
17a2f251
TS
6917 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6918 AT, breg, AT);
beae10d5 6919 /* Itbl support may require additional care here. */
17a2f251
TS
6920 macro_build (NULL, &icnt, &expr1, s, fmt, coproc ? treg + 1 : treg,
6921 BFD_RELOC_LO16, AT);
252b5132
RH
6922 expr1.X_add_number += 4;
6923
6924 /* Set mips_optimize to 2 to avoid inserting an undesired
6925 nop. */
6926 hold_mips_optimize = mips_optimize;
6927 mips_optimize = 2;
beae10d5 6928 /* Itbl support may require additional care here. */
17a2f251
TS
6929 macro_build (NULL, &icnt, &expr1, s, fmt, coproc ? treg : treg + 1,
6930 BFD_RELOC_LO16, AT);
252b5132
RH
6931 mips_optimize = hold_mips_optimize;
6932 expr1.X_add_number -= 4;
6933
6934 p = frag_var (rs_machine_dependent, 16 + gpdel + off, 0,
6935 RELAX_ENCODE (24 + off, 16 + gpdel + off, gpdel,
6936 8 + gpdel + off, 1, 0),
c4e7957c 6937 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
6938 if (gpdel > 0)
6939 {
17a2f251 6940 macro_build (p, &icnt, NULL, "nop", "");
252b5132
RH
6941 p += 4;
6942 }
17a2f251
TS
6943 macro_build (p, &icnt, &offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
6944 AT, BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132 6945 p += 4;
17a2f251 6946 macro_build (p, &icnt, NULL, "nop", "");
252b5132
RH
6947 p += 4;
6948 if (breg != 0)
6949 {
17a2f251
TS
6950 macro_build (p, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6951 AT, breg, AT);
252b5132
RH
6952 p += 4;
6953 }
beae10d5 6954 /* Itbl support may require additional care here. */
17a2f251
TS
6955 macro_build (p, &icnt, &expr1, s, fmt, coproc ? treg + 1 : treg,
6956 BFD_RELOC_LO16, AT);
252b5132
RH
6957 p += 4;
6958 expr1.X_add_number += 4;
6959
6960 /* Set mips_optimize to 2 to avoid inserting an undesired
6961 nop. */
6962 hold_mips_optimize = mips_optimize;
6963 mips_optimize = 2;
beae10d5 6964 /* Itbl support may require additional care here. */
17a2f251
TS
6965 macro_build (p, &icnt, &expr1, s, fmt, coproc ? treg : treg + 1,
6966 BFD_RELOC_LO16, AT);
252b5132
RH
6967 mips_optimize = hold_mips_optimize;
6968 }
6969 else if (mips_pic == EMBEDDED_PIC)
6970 {
6971 /* If there is no base register, we use
cdf6fd85
TS
6972 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6973 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
6974 If we have a base register, we use
6975 addu $at,$breg,$gp
cdf6fd85
TS
6976 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6977 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
6978 */
6979 if (breg == 0)
6980 {
c9914766 6981 tempreg = mips_gp_register;
252b5132
RH
6982 used_at = 0;
6983 }
6984 else
6985 {
17a2f251
TS
6986 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
6987 AT, breg, mips_gp_register);
252b5132
RH
6988 tempreg = AT;
6989 used_at = 1;
6990 }
6991
beae10d5 6992 /* Itbl support may require additional care here. */
17a2f251 6993 macro_build (NULL, &icnt, &offset_expr, s, fmt,
252b5132 6994 coproc ? treg + 1 : treg,
17a2f251 6995 BFD_RELOC_GPREL16, tempreg);
252b5132 6996 offset_expr.X_add_number += 4;
beae10d5 6997 /* Itbl support may require additional care here. */
17a2f251 6998 macro_build (NULL, &icnt, &offset_expr, s, fmt,
252b5132 6999 coproc ? treg : treg + 1,
17a2f251 7000 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
7001 }
7002 else
7003 abort ();
7004
7005 if (! used_at)
7006 return;
7007
7008 break;
7009
7010 case M_LD_OB:
7011 s = "lw";
7012 goto sd_ob;
7013 case M_SD_OB:
7014 s = "sw";
7015 sd_ob:
ca4e0257 7016 assert (HAVE_32BIT_ADDRESSES);
17a2f251
TS
7017 macro_build (NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7018 BFD_RELOC_LO16, breg);
252b5132 7019 offset_expr.X_add_number += 4;
17a2f251
TS
7020 macro_build (NULL, &icnt, &offset_expr, s, "t,o(b)", treg + 1,
7021 BFD_RELOC_LO16, breg);
252b5132
RH
7022 return;
7023
7024 /* New code added to support COPZ instructions.
7025 This code builds table entries out of the macros in mip_opcodes.
7026 R4000 uses interlocks to handle coproc delays.
7027 Other chips (like the R3000) require nops to be inserted for delays.
7028
f72c8c98 7029 FIXME: Currently, we require that the user handle delays.
252b5132
RH
7030 In order to fill delay slots for non-interlocked chips,
7031 we must have a way to specify delays based on the coprocessor.
7032 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
7033 What are the side-effects of the cop instruction?
7034 What cache support might we have and what are its effects?
7035 Both coprocessor & memory require delays. how long???
bdaaa2e1 7036 What registers are read/set/modified?
252b5132
RH
7037
7038 If an itbl is provided to interpret cop instructions,
bdaaa2e1 7039 this knowledge can be encoded in the itbl spec. */
252b5132
RH
7040
7041 case M_COP0:
7042 s = "c0";
7043 goto copz;
7044 case M_COP1:
7045 s = "c1";
7046 goto copz;
7047 case M_COP2:
7048 s = "c2";
7049 goto copz;
7050 case M_COP3:
7051 s = "c3";
7052 copz:
7053 /* For now we just do C (same as Cz). The parameter will be
7054 stored in insn_opcode by mips_ip. */
17a2f251 7055 macro_build (NULL, &icnt, NULL, s, "C", ip->insn_opcode);
252b5132
RH
7056 return;
7057
ea1fb5dc
RS
7058 case M_MOVE:
7059 move_register (&icnt, dreg, sreg);
7060 return;
7061
252b5132
RH
7062#ifdef LOSING_COMPILER
7063 default:
7064 /* Try and see if this is a new itbl instruction.
7065 This code builds table entries out of the macros in mip_opcodes.
7066 FIXME: For now we just assemble the expression and pass it's
7067 value along as a 32-bit immediate.
bdaaa2e1 7068 We may want to have the assembler assemble this value,
252b5132
RH
7069 so that we gain the assembler's knowledge of delay slots,
7070 symbols, etc.
7071 Would it be more efficient to use mask (id) here? */
bdaaa2e1 7072 if (itbl_have_entries
252b5132 7073 && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
beae10d5 7074 {
252b5132
RH
7075 s = ip->insn_mo->name;
7076 s2 = "cop3";
7077 coproc = ITBL_DECODE_PNUM (immed_expr);;
17a2f251 7078 macro_build (NULL, &icnt, &immed_expr, s, "C");
252b5132 7079 return;
beae10d5 7080 }
252b5132
RH
7081 macro2 (ip);
7082 return;
7083 }
7084 if (mips_opts.noat)
7085 as_warn (_("Macro used $at after \".set noat\""));
7086}
bdaaa2e1 7087
252b5132 7088static void
17a2f251 7089macro2 (struct mips_cl_insn *ip)
252b5132
RH
7090{
7091 register int treg, sreg, dreg, breg;
7092 int tempreg;
7093 int mask;
7094 int icnt = 0;
7095 int used_at;
7096 expressionS expr1;
7097 const char *s;
7098 const char *s2;
7099 const char *fmt;
7100 int likely = 0;
7101 int dbl = 0;
7102 int coproc = 0;
7103 int lr = 0;
7104 int imm = 0;
7105 int off;
7106 offsetT maxnum;
7107 bfd_reloc_code_real_type r;
7108 char *p;
bdaaa2e1 7109
252b5132
RH
7110 treg = (ip->insn_opcode >> 16) & 0x1f;
7111 dreg = (ip->insn_opcode >> 11) & 0x1f;
7112 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
7113 mask = ip->insn_mo->mask;
bdaaa2e1 7114
252b5132
RH
7115 expr1.X_op = O_constant;
7116 expr1.X_op_symbol = NULL;
7117 expr1.X_add_symbol = NULL;
7118 expr1.X_add_number = 1;
bdaaa2e1 7119
252b5132
RH
7120 switch (mask)
7121 {
7122#endif /* LOSING_COMPILER */
7123
7124 case M_DMUL:
7125 dbl = 1;
7126 case M_MUL:
17a2f251
TS
7127 macro_build (NULL, &icnt, NULL, dbl ? "dmultu" : "multu", "s,t",
7128 sreg, treg);
7129 macro_build (NULL, &icnt, NULL, "mflo", "d", dreg);
252b5132
RH
7130 return;
7131
7132 case M_DMUL_I:
7133 dbl = 1;
7134 case M_MUL_I:
7135 /* The MIPS assembler some times generates shifts and adds. I'm
7136 not trying to be that fancy. GCC should do this for us
7137 anyway. */
7138 load_register (&icnt, AT, &imm_expr, dbl);
17a2f251
TS
7139 macro_build (NULL, &icnt, NULL, dbl ? "dmult" : "mult", "s,t",
7140 sreg, AT);
7141 macro_build (NULL, &icnt, NULL, "mflo", "d", dreg);
252b5132
RH
7142 break;
7143
7144 case M_DMULO_I:
7145 dbl = 1;
7146 case M_MULO_I:
7147 imm = 1;
7148 goto do_mulo;
7149
7150 case M_DMULO:
7151 dbl = 1;
7152 case M_MULO:
7153 do_mulo:
b34976b6 7154 mips_emit_delays (TRUE);
252b5132
RH
7155 ++mips_opts.noreorder;
7156 mips_any_noreorder = 1;
7157 if (imm)
7158 load_register (&icnt, AT, &imm_expr, dbl);
17a2f251
TS
7159 macro_build (NULL, &icnt, NULL, dbl ? "dmult" : "mult", "s,t",
7160 sreg, imm ? AT : treg);
7161 macro_build (NULL, &icnt, NULL, "mflo", "d", dreg);
7162 macro_build (NULL, &icnt, NULL, dbl ? "dsra32" : "sra", "d,w,<",
7163 dreg, dreg, RA);
7164 macro_build (NULL, &icnt, NULL, "mfhi", "d", AT);
252b5132 7165 if (mips_trap)
17a2f251 7166 macro_build (NULL, &icnt, NULL, "tne", "s,t,q", dreg, AT, 6);
252b5132
RH
7167 else
7168 {
7169 expr1.X_add_number = 8;
17a2f251
TS
7170 macro_build (NULL, &icnt, &expr1, "beq", "s,t,p", dreg, AT);
7171 macro_build (NULL, &icnt, NULL, "nop", "", 0);
7172 macro_build (NULL, &icnt, NULL, "break", "c", 6);
252b5132
RH
7173 }
7174 --mips_opts.noreorder;
17a2f251 7175 macro_build (NULL, &icnt, NULL, "mflo", "d", dreg);
252b5132
RH
7176 break;
7177
7178 case M_DMULOU_I:
7179 dbl = 1;
7180 case M_MULOU_I:
7181 imm = 1;
7182 goto do_mulou;
7183
7184 case M_DMULOU:
7185 dbl = 1;
7186 case M_MULOU:
7187 do_mulou:
b34976b6 7188 mips_emit_delays (TRUE);
252b5132
RH
7189 ++mips_opts.noreorder;
7190 mips_any_noreorder = 1;
7191 if (imm)
7192 load_register (&icnt, AT, &imm_expr, dbl);
17a2f251
TS
7193 macro_build (NULL, &icnt, NULL, dbl ? "dmultu" : "multu", "s,t",
7194 sreg, imm ? AT : treg);
7195 macro_build (NULL, &icnt, NULL, "mfhi", "d", AT);
7196 macro_build (NULL, &icnt, NULL, "mflo", "d", dreg);
252b5132 7197 if (mips_trap)
17a2f251 7198 macro_build (NULL, &icnt, NULL, "tne", "s,t,q", AT, 0, 6);
252b5132
RH
7199 else
7200 {
7201 expr1.X_add_number = 8;
17a2f251
TS
7202 macro_build (NULL, &icnt, &expr1, "beq", "s,t,p", AT, 0);
7203 macro_build (NULL, &icnt, NULL, "nop", "", 0);
7204 macro_build (NULL, &icnt, NULL, "break", "c", 6);
252b5132
RH
7205 }
7206 --mips_opts.noreorder;
7207 break;
7208
771c7ce4 7209 case M_DROL:
fef14a42 7210 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097
CD
7211 {
7212 if (dreg == sreg)
7213 {
7214 tempreg = AT;
7215 used_at = 1;
7216 }
7217 else
7218 {
7219 tempreg = dreg;
7220 used_at = 0;
7221 }
17a2f251
TS
7222 macro_build (NULL, &icnt, NULL, "dnegu", "d,w", tempreg, treg);
7223 macro_build (NULL, &icnt, NULL, "drorv", "d,t,s", dreg, sreg,
7224 tempreg);
82dd0097
CD
7225 if (used_at)
7226 break;
7227 return;
7228 }
17a2f251
TS
7229 macro_build (NULL, &icnt, NULL, "dsubu", "d,v,t", AT, 0, treg);
7230 macro_build (NULL, &icnt, NULL, "dsrlv", "d,t,s", AT, sreg, AT);
7231 macro_build (NULL, &icnt, NULL, "dsllv", "d,t,s", dreg, sreg, treg);
7232 macro_build (NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
7233 break;
7234
252b5132 7235 case M_ROL:
fef14a42 7236 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097
CD
7237 {
7238 if (dreg == sreg)
7239 {
7240 tempreg = AT;
7241 used_at = 1;
7242 }
7243 else
7244 {
7245 tempreg = dreg;
7246 used_at = 0;
7247 }
17a2f251
TS
7248 macro_build (NULL, &icnt, NULL, "negu", "d,w", tempreg, treg);
7249 macro_build (NULL, &icnt, NULL, "rorv", "d,t,s", dreg, sreg,
7250 tempreg);
82dd0097
CD
7251 if (used_at)
7252 break;
7253 return;
7254 }
17a2f251
TS
7255 macro_build (NULL, &icnt, NULL, "subu", "d,v,t", AT, 0, treg);
7256 macro_build (NULL, &icnt, NULL, "srlv", "d,t,s", AT, sreg, AT);
7257 macro_build (NULL, &icnt, NULL, "sllv", "d,t,s", dreg, sreg, treg);
7258 macro_build (NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
7259 break;
7260
771c7ce4
TS
7261 case M_DROL_I:
7262 {
7263 unsigned int rot;
82dd0097 7264 char *l, *r;
771c7ce4
TS
7265
7266 if (imm_expr.X_op != O_constant)
82dd0097 7267 as_bad (_("Improper rotate count"));
771c7ce4 7268 rot = imm_expr.X_add_number & 0x3f;
fef14a42 7269 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
60b63b72
RS
7270 {
7271 rot = (64 - rot) & 0x3f;
7272 if (rot >= 32)
17a2f251
TS
7273 macro_build (NULL, &icnt, NULL, "dror32", "d,w,<",
7274 dreg, sreg, rot - 32);
60b63b72 7275 else
17a2f251
TS
7276 macro_build (NULL, &icnt, NULL, "dror", "d,w,<",
7277 dreg, sreg, rot);
82dd0097 7278 return;
60b63b72 7279 }
483fc7cd 7280 if (rot == 0)
483fc7cd 7281 {
17a2f251 7282 macro_build (NULL, &icnt, NULL, "dsrl", "d,w,<", dreg, sreg, 0);
82dd0097 7283 return;
483fc7cd 7284 }
82dd0097
CD
7285 l = (rot < 0x20) ? "dsll" : "dsll32";
7286 r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
7287 rot &= 0x1f;
17a2f251
TS
7288 macro_build (NULL, &icnt, NULL, l, "d,w,<", AT, sreg, rot);
7289 macro_build (NULL, &icnt, NULL, r, "d,w,<", dreg, sreg,
7290 (0x20 - rot) & 0x1f);
7291 macro_build (NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
7292 }
7293 break;
7294
252b5132 7295 case M_ROL_I:
771c7ce4
TS
7296 {
7297 unsigned int rot;
7298
7299 if (imm_expr.X_op != O_constant)
82dd0097 7300 as_bad (_("Improper rotate count"));
771c7ce4 7301 rot = imm_expr.X_add_number & 0x1f;
fef14a42 7302 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
60b63b72 7303 {
17a2f251
TS
7304 macro_build (NULL, &icnt, NULL, "ror", "d,w,<", dreg, sreg,
7305 (32 - rot) & 0x1f);
82dd0097 7306 return;
60b63b72 7307 }
483fc7cd 7308 if (rot == 0)
483fc7cd 7309 {
17a2f251 7310 macro_build (NULL, &icnt, NULL, "srl", "d,w,<", dreg, sreg, 0);
82dd0097 7311 return;
483fc7cd 7312 }
17a2f251
TS
7313 macro_build (NULL, &icnt, NULL, "sll", "d,w,<", AT, sreg, rot);
7314 macro_build (NULL, &icnt, NULL, "srl", "d,w,<", dreg, sreg,
7315 (0x20 - rot) & 0x1f);
7316 macro_build (NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
7317 }
7318 break;
7319
7320 case M_DROR:
fef14a42 7321 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097 7322 {
17a2f251 7323 macro_build (NULL, &icnt, NULL, "drorv", "d,t,s", dreg, sreg, treg);
82dd0097
CD
7324 return;
7325 }
bf151ce7 7326 macro_build (NULL, &icnt, NULL, "dsubu", "d,v,t", AT, 0, treg);
17a2f251
TS
7327 macro_build (NULL, &icnt, NULL, "dsllv", "d,t,s", AT, sreg, AT);
7328 macro_build (NULL, &icnt, NULL, "dsrlv", "d,t,s", dreg, sreg, treg);
7329 macro_build (NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
7330 break;
7331
7332 case M_ROR:
fef14a42 7333 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 7334 {
17a2f251 7335 macro_build (NULL, &icnt, NULL, "rorv", "d,t,s", dreg, sreg, treg);
82dd0097
CD
7336 return;
7337 }
17a2f251
TS
7338 macro_build (NULL, &icnt, NULL, "subu", "d,v,t", AT, 0, treg);
7339 macro_build (NULL, &icnt, NULL, "sllv", "d,t,s", AT, sreg, AT);
7340 macro_build (NULL, &icnt, NULL, "srlv", "d,t,s", dreg, sreg, treg);
7341 macro_build (NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
7342 break;
7343
771c7ce4
TS
7344 case M_DROR_I:
7345 {
7346 unsigned int rot;
82dd0097 7347 char *l, *r;
771c7ce4
TS
7348
7349 if (imm_expr.X_op != O_constant)
82dd0097 7350 as_bad (_("Improper rotate count"));
771c7ce4 7351 rot = imm_expr.X_add_number & 0x3f;
fef14a42 7352 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097
CD
7353 {
7354 if (rot >= 32)
17a2f251
TS
7355 macro_build (NULL, &icnt, NULL, "dror32", "d,w,<",
7356 dreg, sreg, rot - 32);
82dd0097 7357 else
17a2f251
TS
7358 macro_build (NULL, &icnt, NULL, "dror", "d,w,<",
7359 dreg, sreg, rot);
82dd0097
CD
7360 return;
7361 }
483fc7cd 7362 if (rot == 0)
483fc7cd 7363 {
17a2f251 7364 macro_build (NULL, &icnt, NULL, "dsrl", "d,w,<", dreg, sreg, 0);
82dd0097 7365 return;
483fc7cd 7366 }
82dd0097
CD
7367 r = (rot < 0x20) ? "dsrl" : "dsrl32";
7368 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
7369 rot &= 0x1f;
bf151ce7 7370 macro_build (NULL, &icnt, NULL, r, "d,w,<", AT, sreg, rot);
17a2f251
TS
7371 macro_build (NULL, &icnt, NULL, l, "d,w,<", dreg, sreg,
7372 (0x20 - rot) & 0x1f);
7373 macro_build (NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
7374 }
7375 break;
7376
252b5132 7377 case M_ROR_I:
771c7ce4
TS
7378 {
7379 unsigned int rot;
7380
7381 if (imm_expr.X_op != O_constant)
82dd0097 7382 as_bad (_("Improper rotate count"));
771c7ce4 7383 rot = imm_expr.X_add_number & 0x1f;
fef14a42 7384 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 7385 {
17a2f251 7386 macro_build (NULL, &icnt, NULL, "ror", "d,w,<", dreg, sreg, rot);
82dd0097
CD
7387 return;
7388 }
483fc7cd 7389 if (rot == 0)
483fc7cd 7390 {
17a2f251 7391 macro_build (NULL, &icnt, NULL, "srl", "d,w,<", dreg, sreg, 0);
82dd0097 7392 return;
483fc7cd 7393 }
17a2f251
TS
7394 macro_build (NULL, &icnt, NULL, "srl", "d,w,<", AT, sreg, rot);
7395 macro_build (NULL, &icnt, NULL, "sll", "d,w,<", dreg, sreg,
7396 (0x20 - rot) & 0x1f);
7397 macro_build (NULL, &icnt, NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4 7398 }
252b5132
RH
7399 break;
7400
7401 case M_S_DOB:
fef14a42 7402 if (mips_opts.arch == CPU_R4650)
252b5132
RH
7403 {
7404 as_bad (_("opcode not supported on this processor"));
7405 return;
7406 }
e7af610e 7407 assert (mips_opts.isa == ISA_MIPS1);
252b5132
RH
7408 /* Even on a big endian machine $fn comes before $fn+1. We have
7409 to adjust when storing to memory. */
17a2f251 7410 macro_build (NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
252b5132 7411 target_big_endian ? treg + 1 : treg,
17a2f251 7412 BFD_RELOC_LO16, breg);
252b5132 7413 offset_expr.X_add_number += 4;
17a2f251 7414 macro_build (NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
252b5132 7415 target_big_endian ? treg : treg + 1,
17a2f251 7416 BFD_RELOC_LO16, breg);
252b5132
RH
7417 return;
7418
7419 case M_SEQ:
7420 if (sreg == 0)
17a2f251
TS
7421 macro_build (NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, treg,
7422 BFD_RELOC_LO16);
252b5132 7423 else if (treg == 0)
17a2f251
TS
7424 macro_build (NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, sreg,
7425 BFD_RELOC_LO16);
252b5132
RH
7426 else
7427 {
17a2f251
TS
7428 macro_build (NULL, &icnt, NULL, "xor", "d,v,t", dreg, sreg, treg);
7429 macro_build (NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
7430 BFD_RELOC_LO16);
252b5132
RH
7431 }
7432 return;
7433
7434 case M_SEQ_I:
7435 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7436 {
17a2f251
TS
7437 macro_build (NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, sreg,
7438 BFD_RELOC_LO16);
252b5132
RH
7439 return;
7440 }
7441 if (sreg == 0)
7442 {
7443 as_warn (_("Instruction %s: result is always false"),
7444 ip->insn_mo->name);
ea1fb5dc 7445 move_register (&icnt, dreg, 0);
252b5132
RH
7446 return;
7447 }
7448 if (imm_expr.X_op == O_constant
7449 && imm_expr.X_add_number >= 0
7450 && imm_expr.X_add_number < 0x10000)
7451 {
17a2f251
TS
7452 macro_build (NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg, sreg,
7453 BFD_RELOC_LO16);
252b5132
RH
7454 used_at = 0;
7455 }
7456 else if (imm_expr.X_op == O_constant
7457 && imm_expr.X_add_number > -0x8000
7458 && imm_expr.X_add_number < 0)
7459 {
7460 imm_expr.X_add_number = -imm_expr.X_add_number;
17a2f251 7461 macro_build (NULL, &icnt, &imm_expr,
ca4e0257 7462 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
17a2f251 7463 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7464 used_at = 0;
7465 }
7466 else
7467 {
4d34fb5f 7468 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
17a2f251 7469 macro_build (NULL, &icnt, NULL, "xor", "d,v,t", dreg, sreg, AT);
252b5132
RH
7470 used_at = 1;
7471 }
17a2f251
TS
7472 macro_build (NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
7473 BFD_RELOC_LO16);
252b5132
RH
7474 if (used_at)
7475 break;
7476 return;
7477
7478 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
7479 s = "slt";
7480 goto sge;
7481 case M_SGEU:
7482 s = "sltu";
7483 sge:
17a2f251
TS
7484 macro_build (NULL, &icnt, NULL, s, "d,v,t", dreg, sreg, treg);
7485 macro_build (NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7486 BFD_RELOC_LO16);
252b5132
RH
7487 return;
7488
7489 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
7490 case M_SGEU_I:
7491 if (imm_expr.X_op == O_constant
7492 && imm_expr.X_add_number >= -0x8000
7493 && imm_expr.X_add_number < 0x8000)
7494 {
17a2f251 7495 macro_build (NULL, &icnt, &imm_expr,
252b5132 7496 mask == M_SGE_I ? "slti" : "sltiu",
17a2f251 7497 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7498 used_at = 0;
7499 }
7500 else
7501 {
4d34fb5f 7502 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
17a2f251
TS
7503 macro_build (NULL, &icnt, NULL, mask == M_SGE_I ? "slt" : "sltu",
7504 "d,v,t", dreg, sreg, AT);
252b5132
RH
7505 used_at = 1;
7506 }
17a2f251
TS
7507 macro_build (NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7508 BFD_RELOC_LO16);
252b5132
RH
7509 if (used_at)
7510 break;
7511 return;
7512
7513 case M_SGT: /* sreg > treg <==> treg < sreg */
7514 s = "slt";
7515 goto sgt;
7516 case M_SGTU:
7517 s = "sltu";
7518 sgt:
17a2f251 7519 macro_build (NULL, &icnt, NULL, s, "d,v,t", dreg, treg, sreg);
252b5132
RH
7520 return;
7521
7522 case M_SGT_I: /* sreg > I <==> I < sreg */
7523 s = "slt";
7524 goto sgti;
7525 case M_SGTU_I:
7526 s = "sltu";
7527 sgti:
4d34fb5f 7528 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
17a2f251 7529 macro_build (NULL, &icnt, NULL, s, "d,v,t", dreg, AT, sreg);
252b5132
RH
7530 break;
7531
2396cfb9 7532 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
252b5132
RH
7533 s = "slt";
7534 goto sle;
7535 case M_SLEU:
7536 s = "sltu";
7537 sle:
17a2f251
TS
7538 macro_build (NULL, &icnt, NULL, s, "d,v,t", dreg, treg, sreg);
7539 macro_build (NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7540 BFD_RELOC_LO16);
252b5132
RH
7541 return;
7542
2396cfb9 7543 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
252b5132
RH
7544 s = "slt";
7545 goto slei;
7546 case M_SLEU_I:
7547 s = "sltu";
7548 slei:
4d34fb5f 7549 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
17a2f251
TS
7550 macro_build (NULL, &icnt, NULL, s, "d,v,t", dreg, AT, sreg);
7551 macro_build (NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7552 BFD_RELOC_LO16);
252b5132
RH
7553 break;
7554
7555 case M_SLT_I:
7556 if (imm_expr.X_op == O_constant
7557 && imm_expr.X_add_number >= -0x8000
7558 && imm_expr.X_add_number < 0x8000)
7559 {
17a2f251
TS
7560 macro_build (NULL, &icnt, &imm_expr, "slti", "t,r,j", dreg, sreg,
7561 BFD_RELOC_LO16);
252b5132
RH
7562 return;
7563 }
4d34fb5f 7564 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
17a2f251 7565 macro_build (NULL, &icnt, NULL, "slt", "d,v,t", dreg, sreg, AT);
252b5132
RH
7566 break;
7567
7568 case M_SLTU_I:
7569 if (imm_expr.X_op == O_constant
7570 && imm_expr.X_add_number >= -0x8000
7571 && imm_expr.X_add_number < 0x8000)
7572 {
17a2f251
TS
7573 macro_build (NULL, &icnt, &imm_expr, "sltiu", "t,r,j", dreg, sreg,
7574 BFD_RELOC_LO16);
252b5132
RH
7575 return;
7576 }
4d34fb5f 7577 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
17a2f251 7578 macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", dreg, sreg, AT);
252b5132
RH
7579 break;
7580
7581 case M_SNE:
7582 if (sreg == 0)
bf151ce7 7583 macro_build (NULL, &icnt, NULL, "sltu","d,v,t", dreg, 0, treg);
252b5132 7584 else if (treg == 0)
17a2f251 7585 macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0, sreg);
252b5132
RH
7586 else
7587 {
17a2f251
TS
7588 macro_build (NULL, &icnt, NULL, "xor", "d,v,t", dreg, sreg, treg);
7589 macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0, dreg);
252b5132
RH
7590 }
7591 return;
7592
7593 case M_SNE_I:
7594 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7595 {
17a2f251 7596 macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0, sreg);
252b5132
RH
7597 return;
7598 }
7599 if (sreg == 0)
7600 {
7601 as_warn (_("Instruction %s: result is always true"),
7602 ip->insn_mo->name);
17a2f251 7603 macro_build (NULL, &icnt, &expr1,
ca4e0257 7604 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
17a2f251 7605 "t,r,j", dreg, 0, BFD_RELOC_LO16);
252b5132
RH
7606 return;
7607 }
7608 if (imm_expr.X_op == O_constant
7609 && imm_expr.X_add_number >= 0
7610 && imm_expr.X_add_number < 0x10000)
7611 {
17a2f251
TS
7612 macro_build (NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg, sreg,
7613 BFD_RELOC_LO16);
252b5132
RH
7614 used_at = 0;
7615 }
7616 else if (imm_expr.X_op == O_constant
7617 && imm_expr.X_add_number > -0x8000
7618 && imm_expr.X_add_number < 0)
7619 {
7620 imm_expr.X_add_number = -imm_expr.X_add_number;
17a2f251 7621 macro_build (NULL, &icnt, &imm_expr,
ca4e0257 7622 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
17a2f251 7623 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7624 used_at = 0;
7625 }
7626 else
7627 {
4d34fb5f 7628 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
17a2f251 7629 macro_build (NULL, &icnt, NULL, "xor", "d,v,t", dreg, sreg, AT);
252b5132
RH
7630 used_at = 1;
7631 }
17a2f251 7632 macro_build (NULL, &icnt, NULL, "sltu", "d,v,t", dreg, 0, dreg);
252b5132
RH
7633 if (used_at)
7634 break;
7635 return;
7636
7637 case M_DSUB_I:
7638 dbl = 1;
7639 case M_SUB_I:
7640 if (imm_expr.X_op == O_constant
7641 && imm_expr.X_add_number > -0x8000
7642 && imm_expr.X_add_number <= 0x8000)
7643 {
7644 imm_expr.X_add_number = -imm_expr.X_add_number;
17a2f251
TS
7645 macro_build (NULL, &icnt, &imm_expr, dbl ? "daddi" : "addi",
7646 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7647 return;
7648 }
7649 load_register (&icnt, AT, &imm_expr, dbl);
17a2f251
TS
7650 macro_build (NULL, &icnt, NULL, dbl ? "dsub" : "sub", "d,v,t",
7651 dreg, sreg, AT);
252b5132
RH
7652 break;
7653
7654 case M_DSUBU_I:
7655 dbl = 1;
7656 case M_SUBU_I:
7657 if (imm_expr.X_op == O_constant
7658 && imm_expr.X_add_number > -0x8000
7659 && imm_expr.X_add_number <= 0x8000)
7660 {
7661 imm_expr.X_add_number = -imm_expr.X_add_number;
17a2f251
TS
7662 macro_build (NULL, &icnt, &imm_expr, dbl ? "daddiu" : "addiu",
7663 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7664 return;
7665 }
7666 load_register (&icnt, AT, &imm_expr, dbl);
17a2f251
TS
7667 macro_build (NULL, &icnt, NULL, dbl ? "dsubu" : "subu", "d,v,t",
7668 dreg, sreg, AT);
252b5132
RH
7669 break;
7670
7671 case M_TEQ_I:
7672 s = "teq";
7673 goto trap;
7674 case M_TGE_I:
7675 s = "tge";
7676 goto trap;
7677 case M_TGEU_I:
7678 s = "tgeu";
7679 goto trap;
7680 case M_TLT_I:
7681 s = "tlt";
7682 goto trap;
7683 case M_TLTU_I:
7684 s = "tltu";
7685 goto trap;
7686 case M_TNE_I:
7687 s = "tne";
7688 trap:
4d34fb5f 7689 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
17a2f251 7690 macro_build (NULL, &icnt, NULL, s, "s,t", sreg, AT);
252b5132
RH
7691 break;
7692
252b5132 7693 case M_TRUNCWS:
43841e91 7694 case M_TRUNCWD:
e7af610e 7695 assert (mips_opts.isa == ISA_MIPS1);
252b5132
RH
7696 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
7697 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
7698
7699 /*
7700 * Is the double cfc1 instruction a bug in the mips assembler;
7701 * or is there a reason for it?
7702 */
b34976b6 7703 mips_emit_delays (TRUE);
252b5132
RH
7704 ++mips_opts.noreorder;
7705 mips_any_noreorder = 1;
17a2f251
TS
7706 macro_build (NULL, &icnt, NULL, "cfc1", "t,G", treg, RA);
7707 macro_build (NULL, &icnt, NULL, "cfc1", "t,G", treg, RA);
7708 macro_build (NULL, &icnt, NULL, "nop", "");
252b5132 7709 expr1.X_add_number = 3;
17a2f251
TS
7710 macro_build (NULL, &icnt, &expr1, "ori", "t,r,i", AT, treg,
7711 BFD_RELOC_LO16);
252b5132 7712 expr1.X_add_number = 2;
17a2f251
TS
7713 macro_build (NULL, &icnt, &expr1, "xori", "t,r,i", AT, AT,
7714 BFD_RELOC_LO16);
7715 macro_build (NULL, &icnt, NULL, "ctc1", "t,G", AT, RA);
7716 macro_build (NULL, &icnt, NULL, "nop", "");
7717 macro_build (NULL, &icnt, NULL,
7718 mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s",
7719 "D,S", dreg, sreg);
7720 macro_build (NULL, &icnt, NULL, "ctc1", "t,G", treg, RA);
7721 macro_build (NULL, &icnt, NULL, "nop", "");
252b5132
RH
7722 --mips_opts.noreorder;
7723 break;
7724
7725 case M_ULH:
7726 s = "lb";
7727 goto ulh;
7728 case M_ULHU:
7729 s = "lbu";
7730 ulh:
7731 if (offset_expr.X_add_number >= 0x7fff)
7732 as_bad (_("operand overflow"));
252b5132 7733 if (! target_big_endian)
f9419b05 7734 ++offset_expr.X_add_number;
17a2f251
TS
7735 macro_build (NULL, &icnt, &offset_expr, s, "t,o(b)", AT,
7736 BFD_RELOC_LO16, breg);
252b5132 7737 if (! target_big_endian)
f9419b05 7738 --offset_expr.X_add_number;
252b5132 7739 else
f9419b05 7740 ++offset_expr.X_add_number;
17a2f251
TS
7741 macro_build (NULL, &icnt, &offset_expr, "lbu", "t,o(b)", treg,
7742 BFD_RELOC_LO16, breg);
7743 macro_build (NULL, &icnt, NULL, "sll", "d,w,<", AT, AT, 8);
7744 macro_build (NULL, &icnt, NULL, "or", "d,v,t", treg, treg, AT);
252b5132
RH
7745 break;
7746
7747 case M_ULD:
7748 s = "ldl";
7749 s2 = "ldr";
7750 off = 7;
7751 goto ulw;
7752 case M_ULW:
7753 s = "lwl";
7754 s2 = "lwr";
7755 off = 3;
7756 ulw:
7757 if (offset_expr.X_add_number >= 0x8000 - off)
7758 as_bad (_("operand overflow"));
af22f5b2
CD
7759 if (treg != breg)
7760 tempreg = treg;
7761 else
7762 tempreg = AT;
252b5132
RH
7763 if (! target_big_endian)
7764 offset_expr.X_add_number += off;
17a2f251
TS
7765 macro_build (NULL, &icnt, &offset_expr, s, "t,o(b)", tempreg,
7766 BFD_RELOC_LO16, breg);
252b5132
RH
7767 if (! target_big_endian)
7768 offset_expr.X_add_number -= off;
7769 else
7770 offset_expr.X_add_number += off;
17a2f251
TS
7771 macro_build (NULL, &icnt, &offset_expr, s2, "t,o(b)", tempreg,
7772 BFD_RELOC_LO16, breg);
af22f5b2
CD
7773
7774 /* If necessary, move the result in tempreg the final destination. */
7775 if (treg == tempreg)
7776 return;
7777 /* Protect second load's delay slot. */
7778 if (!gpr_interlocks)
17a2f251 7779 macro_build (NULL, &icnt, NULL, "nop", "");
af22f5b2
CD
7780 move_register (&icnt, treg, tempreg);
7781 break;
252b5132
RH
7782
7783 case M_ULD_A:
7784 s = "ldl";
7785 s2 = "ldr";
7786 off = 7;
7787 goto ulwa;
7788 case M_ULW_A:
7789 s = "lwl";
7790 s2 = "lwr";
7791 off = 3;
7792 ulwa:
d6bc6245 7793 used_at = 1;
c9914766 7794 load_address (&icnt, AT, &offset_expr, &used_at);
252b5132 7795 if (breg != 0)
17a2f251
TS
7796 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
7797 AT, AT, breg);
252b5132
RH
7798 if (! target_big_endian)
7799 expr1.X_add_number = off;
7800 else
7801 expr1.X_add_number = 0;
17a2f251
TS
7802 macro_build (NULL, &icnt, &expr1, s, "t,o(b)", treg,
7803 BFD_RELOC_LO16, AT);
252b5132
RH
7804 if (! target_big_endian)
7805 expr1.X_add_number = 0;
7806 else
7807 expr1.X_add_number = off;
17a2f251
TS
7808 macro_build (NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7809 BFD_RELOC_LO16, AT);
252b5132
RH
7810 break;
7811
7812 case M_ULH_A:
7813 case M_ULHU_A:
d6bc6245 7814 used_at = 1;
c9914766 7815 load_address (&icnt, AT, &offset_expr, &used_at);
252b5132 7816 if (breg != 0)
17a2f251
TS
7817 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
7818 AT, AT, breg);
252b5132
RH
7819 if (target_big_endian)
7820 expr1.X_add_number = 0;
17a2f251
TS
7821 macro_build (NULL, &icnt, &expr1,
7822 mask == M_ULH_A ? "lb" : "lbu", "t,o(b)",
7823 treg, BFD_RELOC_LO16, AT);
252b5132
RH
7824 if (target_big_endian)
7825 expr1.X_add_number = 1;
7826 else
7827 expr1.X_add_number = 0;
17a2f251
TS
7828 macro_build (NULL, &icnt, &expr1, "lbu", "t,o(b)",
7829 AT, BFD_RELOC_LO16, AT);
7830 macro_build (NULL, &icnt, NULL, "sll", "d,w,<", treg, treg, 8);
7831 macro_build (NULL, &icnt, NULL, "or", "d,v,t", treg, treg, AT);
252b5132
RH
7832 break;
7833
7834 case M_USH:
7835 if (offset_expr.X_add_number >= 0x7fff)
7836 as_bad (_("operand overflow"));
7837 if (target_big_endian)
f9419b05 7838 ++offset_expr.X_add_number;
17a2f251
TS
7839 macro_build (NULL, &icnt, &offset_expr, "sb", "t,o(b)", treg,
7840 BFD_RELOC_LO16, breg);
7841 macro_build (NULL, &icnt, NULL, "srl", "d,w,<", AT, treg, 8);
252b5132 7842 if (target_big_endian)
f9419b05 7843 --offset_expr.X_add_number;
252b5132 7844 else
f9419b05 7845 ++offset_expr.X_add_number;
17a2f251
TS
7846 macro_build (NULL, &icnt, &offset_expr, "sb", "t,o(b)", AT,
7847 BFD_RELOC_LO16, breg);
252b5132
RH
7848 break;
7849
7850 case M_USD:
7851 s = "sdl";
7852 s2 = "sdr";
7853 off = 7;
7854 goto usw;
7855 case M_USW:
7856 s = "swl";
7857 s2 = "swr";
7858 off = 3;
7859 usw:
7860 if (offset_expr.X_add_number >= 0x8000 - off)
7861 as_bad (_("operand overflow"));
7862 if (! target_big_endian)
7863 offset_expr.X_add_number += off;
17a2f251
TS
7864 macro_build (NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7865 BFD_RELOC_LO16, breg);
252b5132
RH
7866 if (! target_big_endian)
7867 offset_expr.X_add_number -= off;
7868 else
7869 offset_expr.X_add_number += off;
17a2f251
TS
7870 macro_build (NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7871 BFD_RELOC_LO16, breg);
252b5132
RH
7872 return;
7873
7874 case M_USD_A:
7875 s = "sdl";
7876 s2 = "sdr";
7877 off = 7;
7878 goto uswa;
7879 case M_USW_A:
7880 s = "swl";
7881 s2 = "swr";
7882 off = 3;
7883 uswa:
d6bc6245 7884 used_at = 1;
c9914766 7885 load_address (&icnt, AT, &offset_expr, &used_at);
252b5132 7886 if (breg != 0)
17a2f251
TS
7887 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
7888 AT, AT, breg);
252b5132
RH
7889 if (! target_big_endian)
7890 expr1.X_add_number = off;
7891 else
7892 expr1.X_add_number = 0;
17a2f251
TS
7893 macro_build (NULL, &icnt, &expr1, s, "t,o(b)", treg,
7894 BFD_RELOC_LO16, AT);
252b5132
RH
7895 if (! target_big_endian)
7896 expr1.X_add_number = 0;
7897 else
7898 expr1.X_add_number = off;
17a2f251
TS
7899 macro_build (NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7900 BFD_RELOC_LO16, AT);
252b5132
RH
7901 break;
7902
7903 case M_USH_A:
d6bc6245 7904 used_at = 1;
c9914766 7905 load_address (&icnt, AT, &offset_expr, &used_at);
252b5132 7906 if (breg != 0)
17a2f251
TS
7907 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
7908 AT, AT, breg);
252b5132
RH
7909 if (! target_big_endian)
7910 expr1.X_add_number = 0;
17a2f251
TS
7911 macro_build (NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7912 BFD_RELOC_LO16, AT);
7913 macro_build (NULL, &icnt, NULL, "srl", "d,w,<", treg, treg, 8);
252b5132
RH
7914 if (! target_big_endian)
7915 expr1.X_add_number = 1;
7916 else
7917 expr1.X_add_number = 0;
17a2f251
TS
7918 macro_build (NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7919 BFD_RELOC_LO16, AT);
252b5132
RH
7920 if (! target_big_endian)
7921 expr1.X_add_number = 0;
7922 else
7923 expr1.X_add_number = 1;
17a2f251
TS
7924 macro_build (NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7925 BFD_RELOC_LO16, AT);
7926 macro_build (NULL, &icnt, NULL, "sll", "d,w,<", treg, treg, 8);
7927 macro_build (NULL, &icnt, NULL, "or", "d,v,t", treg, treg, AT);
252b5132
RH
7928 break;
7929
7930 default:
7931 /* FIXME: Check if this is one of the itbl macros, since they
bdaaa2e1 7932 are added dynamically. */
252b5132
RH
7933 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7934 break;
7935 }
7936 if (mips_opts.noat)
7937 as_warn (_("Macro used $at after \".set noat\""));
7938}
7939
7940/* Implement macros in mips16 mode. */
7941
7942static void
17a2f251 7943mips16_macro (struct mips_cl_insn *ip)
252b5132
RH
7944{
7945 int mask;
7946 int xreg, yreg, zreg, tmp;
7947 int icnt;
7948 expressionS expr1;
7949 int dbl;
7950 const char *s, *s2, *s3;
7951
7952 mask = ip->insn_mo->mask;
7953
7954 xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
7955 yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
7956 zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
7957
7958 icnt = 0;
7959
7960 expr1.X_op = O_constant;
7961 expr1.X_op_symbol = NULL;
7962 expr1.X_add_symbol = NULL;
7963 expr1.X_add_number = 1;
7964
7965 dbl = 0;
7966
7967 switch (mask)
7968 {
7969 default:
7970 internalError ();
7971
7972 case M_DDIV_3:
7973 dbl = 1;
7974 case M_DIV_3:
7975 s = "mflo";
7976 goto do_div3;
7977 case M_DREM_3:
7978 dbl = 1;
7979 case M_REM_3:
7980 s = "mfhi";
7981 do_div3:
b34976b6 7982 mips_emit_delays (TRUE);
252b5132
RH
7983 ++mips_opts.noreorder;
7984 mips_any_noreorder = 1;
17a2f251
TS
7985 macro_build (NULL, &icnt, NULL, dbl ? "ddiv" : "div", "0,x,y",
7986 xreg, yreg);
252b5132 7987 expr1.X_add_number = 2;
17a2f251
TS
7988 macro_build (NULL, &icnt, &expr1, "bnez", "x,p", yreg);
7989 macro_build (NULL, &icnt, NULL, "break", "6", 7);
bdaaa2e1 7990
252b5132
RH
7991 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7992 since that causes an overflow. We should do that as well,
7993 but I don't see how to do the comparisons without a temporary
7994 register. */
7995 --mips_opts.noreorder;
17a2f251 7996 macro_build (NULL, &icnt, NULL, s, "x", zreg);
252b5132
RH
7997 break;
7998
7999 case M_DIVU_3:
8000 s = "divu";
8001 s2 = "mflo";
8002 goto do_divu3;
8003 case M_REMU_3:
8004 s = "divu";
8005 s2 = "mfhi";
8006 goto do_divu3;
8007 case M_DDIVU_3:
8008 s = "ddivu";
8009 s2 = "mflo";
8010 goto do_divu3;
8011 case M_DREMU_3:
8012 s = "ddivu";
8013 s2 = "mfhi";
8014 do_divu3:
b34976b6 8015 mips_emit_delays (TRUE);
252b5132
RH
8016 ++mips_opts.noreorder;
8017 mips_any_noreorder = 1;
17a2f251 8018 macro_build (NULL, &icnt, NULL, s, "0,x,y", xreg, yreg);
252b5132 8019 expr1.X_add_number = 2;
17a2f251
TS
8020 macro_build (NULL, &icnt, &expr1, "bnez", "x,p", yreg);
8021 macro_build (NULL, &icnt, NULL, "break", "6", 7);
252b5132 8022 --mips_opts.noreorder;
17a2f251 8023 macro_build (NULL, &icnt, NULL, s2, "x", zreg);
252b5132
RH
8024 break;
8025
8026 case M_DMUL:
8027 dbl = 1;
8028 case M_MUL:
17a2f251
TS
8029 macro_build (NULL, &icnt, NULL, dbl ? "dmultu" : "multu", "x,y",
8030 xreg, yreg);
8031 macro_build (NULL, &icnt, NULL, "mflo", "x", zreg);
252b5132
RH
8032 return;
8033
8034 case M_DSUBU_I:
8035 dbl = 1;
8036 goto do_subu;
8037 case M_SUBU_I:
8038 do_subu:
8039 if (imm_expr.X_op != O_constant)
8040 as_bad (_("Unsupported large constant"));
8041 imm_expr.X_add_number = -imm_expr.X_add_number;
17a2f251
TS
8042 macro_build (NULL, &icnt, &imm_expr, dbl ? "daddiu" : "addiu", "y,x,4",
8043 yreg, xreg);
252b5132
RH
8044 break;
8045
8046 case M_SUBU_I_2:
8047 if (imm_expr.X_op != O_constant)
8048 as_bad (_("Unsupported large constant"));
8049 imm_expr.X_add_number = -imm_expr.X_add_number;
17a2f251 8050 macro_build (NULL, &icnt, &imm_expr, "addiu", "x,k", xreg);
252b5132
RH
8051 break;
8052
8053 case M_DSUBU_I_2:
8054 if (imm_expr.X_op != O_constant)
8055 as_bad (_("Unsupported large constant"));
8056 imm_expr.X_add_number = -imm_expr.X_add_number;
17a2f251 8057 macro_build (NULL, &icnt, &imm_expr, "daddiu", "y,j", yreg);
252b5132
RH
8058 break;
8059
8060 case M_BEQ:
8061 s = "cmp";
8062 s2 = "bteqz";
8063 goto do_branch;
8064 case M_BNE:
8065 s = "cmp";
8066 s2 = "btnez";
8067 goto do_branch;
8068 case M_BLT:
8069 s = "slt";
8070 s2 = "btnez";
8071 goto do_branch;
8072 case M_BLTU:
8073 s = "sltu";
8074 s2 = "btnez";
8075 goto do_branch;
8076 case M_BLE:
8077 s = "slt";
8078 s2 = "bteqz";
8079 goto do_reverse_branch;
8080 case M_BLEU:
8081 s = "sltu";
8082 s2 = "bteqz";
8083 goto do_reverse_branch;
8084 case M_BGE:
8085 s = "slt";
8086 s2 = "bteqz";
8087 goto do_branch;
8088 case M_BGEU:
8089 s = "sltu";
8090 s2 = "bteqz";
8091 goto do_branch;
8092 case M_BGT:
8093 s = "slt";
8094 s2 = "btnez";
8095 goto do_reverse_branch;
8096 case M_BGTU:
8097 s = "sltu";
8098 s2 = "btnez";
8099
8100 do_reverse_branch:
8101 tmp = xreg;
8102 xreg = yreg;
8103 yreg = tmp;
8104
8105 do_branch:
17a2f251
TS
8106 macro_build (NULL, &icnt, NULL, s, "x,y", xreg, yreg);
8107 macro_build (NULL, &icnt, &offset_expr, s2, "p");
252b5132
RH
8108 break;
8109
8110 case M_BEQ_I:
8111 s = "cmpi";
8112 s2 = "bteqz";
8113 s3 = "x,U";
8114 goto do_branch_i;
8115 case M_BNE_I:
8116 s = "cmpi";
8117 s2 = "btnez";
8118 s3 = "x,U";
8119 goto do_branch_i;
8120 case M_BLT_I:
8121 s = "slti";
8122 s2 = "btnez";
8123 s3 = "x,8";
8124 goto do_branch_i;
8125 case M_BLTU_I:
8126 s = "sltiu";
8127 s2 = "btnez";
8128 s3 = "x,8";
8129 goto do_branch_i;
8130 case M_BLE_I:
8131 s = "slti";
8132 s2 = "btnez";
8133 s3 = "x,8";
8134 goto do_addone_branch_i;
8135 case M_BLEU_I:
8136 s = "sltiu";
8137 s2 = "btnez";
8138 s3 = "x,8";
8139 goto do_addone_branch_i;
8140 case M_BGE_I:
8141 s = "slti";
8142 s2 = "bteqz";
8143 s3 = "x,8";
8144 goto do_branch_i;
8145 case M_BGEU_I:
8146 s = "sltiu";
8147 s2 = "bteqz";
8148 s3 = "x,8";
8149 goto do_branch_i;
8150 case M_BGT_I:
8151 s = "slti";
8152 s2 = "bteqz";
8153 s3 = "x,8";
8154 goto do_addone_branch_i;
8155 case M_BGTU_I:
8156 s = "sltiu";
8157 s2 = "bteqz";
8158 s3 = "x,8";
8159
8160 do_addone_branch_i:
8161 if (imm_expr.X_op != O_constant)
8162 as_bad (_("Unsupported large constant"));
8163 ++imm_expr.X_add_number;
8164
8165 do_branch_i:
17a2f251
TS
8166 macro_build (NULL, &icnt, &imm_expr, s, s3, xreg);
8167 macro_build (NULL, &icnt, &offset_expr, s2, "p");
252b5132
RH
8168 break;
8169
8170 case M_ABS:
8171 expr1.X_add_number = 0;
17a2f251 8172 macro_build (NULL, &icnt, &expr1, "slti", "x,8", yreg);
252b5132 8173 if (xreg != yreg)
ea1fb5dc 8174 move_register (&icnt, xreg, yreg);
252b5132 8175 expr1.X_add_number = 2;
17a2f251
TS
8176 macro_build (NULL, &icnt, &expr1, "bteqz", "p");
8177 macro_build (NULL, &icnt, NULL, "neg", "x,w", xreg, xreg);
252b5132
RH
8178 }
8179}
8180
8181/* For consistency checking, verify that all bits are specified either
8182 by the match/mask part of the instruction definition, or by the
8183 operand list. */
8184static int
17a2f251 8185validate_mips_insn (const struct mips_opcode *opc)
252b5132
RH
8186{
8187 const char *p = opc->args;
8188 char c;
8189 unsigned long used_bits = opc->mask;
8190
8191 if ((used_bits & opc->match) != opc->match)
8192 {
8193 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
8194 opc->name, opc->args);
8195 return 0;
8196 }
8197#define USE_BITS(mask,shift) (used_bits |= ((mask) << (shift)))
8198 while (*p)
8199 switch (c = *p++)
8200 {
8201 case ',': break;
8202 case '(': break;
8203 case ')': break;
af7ee8bf
CD
8204 case '+':
8205 switch (c = *p++)
8206 {
8207 case 'A': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
8208 case 'B': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
8209 case 'C': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
bbcc0807
CD
8210 case 'D': USE_BITS (OP_MASK_RD, OP_SH_RD);
8211 USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
5f74bc13
CD
8212 case 'E': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
8213 case 'F': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
8214 case 'G': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
8215 case 'H': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
8216 case 'I': break;
af7ee8bf
CD
8217 default:
8218 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8219 c, opc->name, opc->args);
8220 return 0;
8221 }
8222 break;
252b5132
RH
8223 case '<': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
8224 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
8225 case 'A': break;
4372b673 8226 case 'B': USE_BITS (OP_MASK_CODE20, OP_SH_CODE20); break;
252b5132
RH
8227 case 'C': USE_BITS (OP_MASK_COPZ, OP_SH_COPZ); break;
8228 case 'D': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
8229 case 'E': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8230 case 'F': break;
8231 case 'G': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
156c2f8b 8232 case 'H': USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
252b5132 8233 case 'I': break;
e972090a 8234 case 'J': USE_BITS (OP_MASK_CODE19, OP_SH_CODE19); break;
af7ee8bf 8235 case 'K': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
252b5132
RH
8236 case 'L': break;
8237 case 'M': USE_BITS (OP_MASK_CCC, OP_SH_CCC); break;
8238 case 'N': USE_BITS (OP_MASK_BCC, OP_SH_BCC); break;
deec1734
CD
8239 case 'O': USE_BITS (OP_MASK_ALN, OP_SH_ALN); break;
8240 case 'Q': USE_BITS (OP_MASK_VSEL, OP_SH_VSEL);
8241 USE_BITS (OP_MASK_FT, OP_SH_FT); break;
252b5132
RH
8242 case 'R': USE_BITS (OP_MASK_FR, OP_SH_FR); break;
8243 case 'S': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
8244 case 'T': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
8245 case 'V': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
8246 case 'W': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
deec1734
CD
8247 case 'X': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
8248 case 'Y': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
8249 case 'Z': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
252b5132
RH
8250 case 'a': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break;
8251 case 'b': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8252 case 'c': USE_BITS (OP_MASK_CODE, OP_SH_CODE); break;
8253 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
8254 case 'f': break;
8255 case 'h': USE_BITS (OP_MASK_PREFX, OP_SH_PREFX); break;
8256 case 'i': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
8257 case 'j': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
8258 case 'k': USE_BITS (OP_MASK_CACHE, OP_SH_CACHE); break;
8259 case 'l': break;
8260 case 'o': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
8261 case 'p': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
8262 case 'q': USE_BITS (OP_MASK_CODE2, OP_SH_CODE2); break;
8263 case 'r': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8264 case 's': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8265 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8266 case 'u': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
8267 case 'v': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8268 case 'w': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8269 case 'x': break;
8270 case 'z': break;
8271 case 'P': USE_BITS (OP_MASK_PERFREG, OP_SH_PERFREG); break;
4372b673
NC
8272 case 'U': USE_BITS (OP_MASK_RD, OP_SH_RD);
8273 USE_BITS (OP_MASK_RT, OP_SH_RT); break;
60b63b72
RS
8274 case 'e': USE_BITS (OP_MASK_VECBYTE, OP_SH_VECBYTE); break;
8275 case '%': USE_BITS (OP_MASK_VECALIGN, OP_SH_VECALIGN); break;
8276 case '[': break;
8277 case ']': break;
252b5132
RH
8278 default:
8279 as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
8280 c, opc->name, opc->args);
8281 return 0;
8282 }
8283#undef USE_BITS
8284 if (used_bits != 0xffffffff)
8285 {
8286 as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
8287 ~used_bits & 0xffffffff, opc->name, opc->args);
8288 return 0;
8289 }
8290 return 1;
8291}
8292
8293/* This routine assembles an instruction into its binary format. As a
8294 side effect, it sets one of the global variables imm_reloc or
8295 offset_reloc to the type of relocation to do if one of the operands
8296 is an address expression. */
8297
8298static void
17a2f251 8299mips_ip (char *str, struct mips_cl_insn *ip)
252b5132
RH
8300{
8301 char *s;
8302 const char *args;
43841e91 8303 char c = 0;
252b5132
RH
8304 struct mips_opcode *insn;
8305 char *argsStart;
8306 unsigned int regno;
8307 unsigned int lastregno = 0;
af7ee8bf 8308 unsigned int lastpos = 0;
071742cf 8309 unsigned int limlo, limhi;
252b5132
RH
8310 char *s_reset;
8311 char save_c = 0;
252b5132
RH
8312
8313 insn_error = NULL;
8314
8315 /* If the instruction contains a '.', we first try to match an instruction
8316 including the '.'. Then we try again without the '.'. */
8317 insn = NULL;
3882b010 8318 for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
252b5132
RH
8319 continue;
8320
8321 /* If we stopped on whitespace, then replace the whitespace with null for
8322 the call to hash_find. Save the character we replaced just in case we
8323 have to re-parse the instruction. */
3882b010 8324 if (ISSPACE (*s))
252b5132
RH
8325 {
8326 save_c = *s;
8327 *s++ = '\0';
8328 }
bdaaa2e1 8329
252b5132
RH
8330 insn = (struct mips_opcode *) hash_find (op_hash, str);
8331
8332 /* If we didn't find the instruction in the opcode table, try again, but
8333 this time with just the instruction up to, but not including the
8334 first '.'. */
8335 if (insn == NULL)
8336 {
bdaaa2e1 8337 /* Restore the character we overwrite above (if any). */
252b5132
RH
8338 if (save_c)
8339 *(--s) = save_c;
8340
8341 /* Scan up to the first '.' or whitespace. */
3882b010
L
8342 for (s = str;
8343 *s != '\0' && *s != '.' && !ISSPACE (*s);
8344 ++s)
252b5132
RH
8345 continue;
8346
8347 /* If we did not find a '.', then we can quit now. */
8348 if (*s != '.')
8349 {
8350 insn_error = "unrecognized opcode";
8351 return;
8352 }
8353
8354 /* Lookup the instruction in the hash table. */
8355 *s++ = '\0';
8356 if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
8357 {
8358 insn_error = "unrecognized opcode";
8359 return;
8360 }
252b5132
RH
8361 }
8362
8363 argsStart = s;
8364 for (;;)
8365 {
b34976b6 8366 bfd_boolean ok;
252b5132
RH
8367
8368 assert (strcmp (insn->name, str) == 0);
8369
1f25f5d3
CD
8370 if (OPCODE_IS_MEMBER (insn,
8371 (mips_opts.isa
3396de36 8372 | (file_ase_mips16 ? INSN_MIPS16 : 0)
deec1734 8373 | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
98d3f06f 8374 | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
fef14a42 8375 mips_opts.arch))
b34976b6 8376 ok = TRUE;
bdaaa2e1 8377 else
b34976b6 8378 ok = FALSE;
bdaaa2e1 8379
252b5132
RH
8380 if (insn->pinfo != INSN_MACRO)
8381 {
fef14a42 8382 if (mips_opts.arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
b34976b6 8383 ok = FALSE;
252b5132
RH
8384 }
8385
8386 if (! ok)
8387 {
8388 if (insn + 1 < &mips_opcodes[NUMOPCODES]
8389 && strcmp (insn->name, insn[1].name) == 0)
8390 {
8391 ++insn;
8392 continue;
8393 }
252b5132 8394 else
beae10d5 8395 {
268f6bed
L
8396 if (!insn_error)
8397 {
8398 static char buf[100];
fef14a42
TS
8399 sprintf (buf,
8400 _("opcode not supported on this processor: %s (%s)"),
8401 mips_cpu_info_from_arch (mips_opts.arch)->name,
8402 mips_cpu_info_from_isa (mips_opts.isa)->name);
268f6bed
L
8403 insn_error = buf;
8404 }
8405 if (save_c)
8406 *(--s) = save_c;
2bd7f1f3 8407 return;
252b5132 8408 }
252b5132
RH
8409 }
8410
8411 ip->insn_mo = insn;
8412 ip->insn_opcode = insn->match;
268f6bed 8413 insn_error = NULL;
252b5132
RH
8414 for (args = insn->args;; ++args)
8415 {
deec1734
CD
8416 int is_mdmx;
8417
ad8d3bb3 8418 s += strspn (s, " \t");
deec1734 8419 is_mdmx = 0;
252b5132
RH
8420 switch (*args)
8421 {
8422 case '\0': /* end of args */
8423 if (*s == '\0')
8424 return;
8425 break;
8426
8427 case ',':
8428 if (*s++ == *args)
8429 continue;
8430 s--;
8431 switch (*++args)
8432 {
8433 case 'r':
8434 case 'v':
38487616 8435 ip->insn_opcode |= lastregno << OP_SH_RS;
252b5132
RH
8436 continue;
8437
8438 case 'w':
38487616
TS
8439 ip->insn_opcode |= lastregno << OP_SH_RT;
8440 continue;
8441
252b5132 8442 case 'W':
38487616 8443 ip->insn_opcode |= lastregno << OP_SH_FT;
252b5132
RH
8444 continue;
8445
8446 case 'V':
38487616 8447 ip->insn_opcode |= lastregno << OP_SH_FS;
252b5132
RH
8448 continue;
8449 }
8450 break;
8451
8452 case '(':
8453 /* Handle optional base register.
8454 Either the base register is omitted or
bdaaa2e1 8455 we must have a left paren. */
252b5132
RH
8456 /* This is dependent on the next operand specifier
8457 is a base register specification. */
8458 assert (args[1] == 'b' || args[1] == '5'
8459 || args[1] == '-' || args[1] == '4');
8460 if (*s == '\0')
8461 return;
8462
8463 case ')': /* these must match exactly */
60b63b72
RS
8464 case '[':
8465 case ']':
252b5132
RH
8466 if (*s++ == *args)
8467 continue;
8468 break;
8469
af7ee8bf
CD
8470 case '+': /* Opcode extension character. */
8471 switch (*++args)
8472 {
071742cf
CD
8473 case 'A': /* ins/ext position, becomes LSB. */
8474 limlo = 0;
8475 limhi = 31;
5f74bc13
CD
8476 goto do_lsb;
8477 case 'E':
8478 limlo = 32;
8479 limhi = 63;
8480 goto do_lsb;
8481do_lsb:
071742cf
CD
8482 my_getExpression (&imm_expr, s);
8483 check_absolute_expr (ip, &imm_expr);
8484 if ((unsigned long) imm_expr.X_add_number < limlo
8485 || (unsigned long) imm_expr.X_add_number > limhi)
8486 {
8487 as_bad (_("Improper position (%lu)"),
8488 (unsigned long) imm_expr.X_add_number);
8489 imm_expr.X_add_number = limlo;
8490 }
8491 lastpos = imm_expr.X_add_number;
8492 ip->insn_opcode |= (imm_expr.X_add_number
8493 & OP_MASK_SHAMT) << OP_SH_SHAMT;
8494 imm_expr.X_op = O_absent;
8495 s = expr_end;
8496 continue;
8497
8498 case 'B': /* ins size, becomes MSB. */
8499 limlo = 1;
8500 limhi = 32;
5f74bc13
CD
8501 goto do_msb;
8502 case 'F':
8503 limlo = 33;
8504 limhi = 64;
8505 goto do_msb;
8506do_msb:
071742cf
CD
8507 my_getExpression (&imm_expr, s);
8508 check_absolute_expr (ip, &imm_expr);
8509 /* Check for negative input so that small negative numbers
8510 will not succeed incorrectly. The checks against
8511 (pos+size) transitively check "size" itself,
8512 assuming that "pos" is reasonable. */
8513 if ((long) imm_expr.X_add_number < 0
8514 || ((unsigned long) imm_expr.X_add_number
8515 + lastpos) < limlo
8516 || ((unsigned long) imm_expr.X_add_number
8517 + lastpos) > limhi)
8518 {
8519 as_bad (_("Improper insert size (%lu, position %lu)"),
8520 (unsigned long) imm_expr.X_add_number,
8521 (unsigned long) lastpos);
8522 imm_expr.X_add_number = limlo - lastpos;
8523 }
8524 ip->insn_opcode |= ((lastpos + imm_expr.X_add_number - 1)
8525 & OP_MASK_INSMSB) << OP_SH_INSMSB;
8526 imm_expr.X_op = O_absent;
8527 s = expr_end;
8528 continue;
8529
8530 case 'C': /* ext size, becomes MSBD. */
8531 limlo = 1;
8532 limhi = 32;
5f74bc13
CD
8533 goto do_msbd;
8534 case 'G':
8535 limlo = 33;
8536 limhi = 64;
8537 goto do_msbd;
8538 case 'H':
8539 limlo = 33;
8540 limhi = 64;
8541 goto do_msbd;
8542do_msbd:
071742cf
CD
8543 my_getExpression (&imm_expr, s);
8544 check_absolute_expr (ip, &imm_expr);
8545 /* Check for negative input so that small negative numbers
8546 will not succeed incorrectly. The checks against
8547 (pos+size) transitively check "size" itself,
8548 assuming that "pos" is reasonable. */
8549 if ((long) imm_expr.X_add_number < 0
8550 || ((unsigned long) imm_expr.X_add_number
8551 + lastpos) < limlo
8552 || ((unsigned long) imm_expr.X_add_number
8553 + lastpos) > limhi)
8554 {
8555 as_bad (_("Improper extract size (%lu, position %lu)"),
8556 (unsigned long) imm_expr.X_add_number,
8557 (unsigned long) lastpos);
8558 imm_expr.X_add_number = limlo - lastpos;
8559 }
8560 ip->insn_opcode |= ((imm_expr.X_add_number - 1)
8561 & OP_MASK_EXTMSBD) << OP_SH_EXTMSBD;
8562 imm_expr.X_op = O_absent;
8563 s = expr_end;
8564 continue;
af7ee8bf 8565
bbcc0807
CD
8566 case 'D':
8567 /* +D is for disassembly only; never match. */
8568 break;
8569
5f74bc13
CD
8570 case 'I':
8571 /* "+I" is like "I", except that imm2_expr is used. */
8572 my_getExpression (&imm2_expr, s);
8573 if (imm2_expr.X_op != O_big
8574 && imm2_expr.X_op != O_constant)
8575 insn_error = _("absolute expression required");
13757d0c 8576 normalize_constant_expr (&imm2_expr);
5f74bc13
CD
8577 s = expr_end;
8578 continue;
8579
af7ee8bf
CD
8580 default:
8581 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8582 *args, insn->name, insn->args);
8583 /* Further processing is fruitless. */
8584 return;
8585 }
8586 break;
8587
252b5132
RH
8588 case '<': /* must be at least one digit */
8589 /*
8590 * According to the manual, if the shift amount is greater
b6ff326e
KH
8591 * than 31 or less than 0, then the shift amount should be
8592 * mod 32. In reality the mips assembler issues an error.
252b5132
RH
8593 * We issue a warning and mask out all but the low 5 bits.
8594 */
8595 my_getExpression (&imm_expr, s);
8596 check_absolute_expr (ip, &imm_expr);
8597 if ((unsigned long) imm_expr.X_add_number > 31)
8598 {
793b27f4
TS
8599 as_warn (_("Improper shift amount (%lu)"),
8600 (unsigned long) imm_expr.X_add_number);
38487616 8601 imm_expr.X_add_number &= OP_MASK_SHAMT;
252b5132 8602 }
38487616 8603 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SHAMT;
252b5132
RH
8604 imm_expr.X_op = O_absent;
8605 s = expr_end;
8606 continue;
8607
8608 case '>': /* shift amount minus 32 */
8609 my_getExpression (&imm_expr, s);
8610 check_absolute_expr (ip, &imm_expr);
8611 if ((unsigned long) imm_expr.X_add_number < 32
8612 || (unsigned long) imm_expr.X_add_number > 63)
8613 break;
38487616 8614 ip->insn_opcode |= (imm_expr.X_add_number - 32) << OP_SH_SHAMT;
252b5132
RH
8615 imm_expr.X_op = O_absent;
8616 s = expr_end;
8617 continue;
8618
252b5132
RH
8619 case 'k': /* cache code */
8620 case 'h': /* prefx code */
8621 my_getExpression (&imm_expr, s);
8622 check_absolute_expr (ip, &imm_expr);
8623 if ((unsigned long) imm_expr.X_add_number > 31)
8624 {
8625 as_warn (_("Invalid value for `%s' (%lu)"),
8626 ip->insn_mo->name,
8627 (unsigned long) imm_expr.X_add_number);
8628 imm_expr.X_add_number &= 0x1f;
8629 }
8630 if (*args == 'k')
8631 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
8632 else
8633 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
8634 imm_expr.X_op = O_absent;
8635 s = expr_end;
8636 continue;
8637
8638 case 'c': /* break code */
8639 my_getExpression (&imm_expr, s);
8640 check_absolute_expr (ip, &imm_expr);
793b27f4 8641 if ((unsigned long) imm_expr.X_add_number > 1023)
252b5132 8642 {
793b27f4
TS
8643 as_warn (_("Illegal break code (%lu)"),
8644 (unsigned long) imm_expr.X_add_number);
38487616 8645 imm_expr.X_add_number &= OP_MASK_CODE;
252b5132 8646 }
38487616 8647 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE;
252b5132
RH
8648 imm_expr.X_op = O_absent;
8649 s = expr_end;
8650 continue;
8651
8652 case 'q': /* lower break code */
8653 my_getExpression (&imm_expr, s);
8654 check_absolute_expr (ip, &imm_expr);
793b27f4 8655 if ((unsigned long) imm_expr.X_add_number > 1023)
252b5132 8656 {
793b27f4
TS
8657 as_warn (_("Illegal lower break code (%lu)"),
8658 (unsigned long) imm_expr.X_add_number);
38487616 8659 imm_expr.X_add_number &= OP_MASK_CODE2;
252b5132 8660 }
38487616 8661 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE2;
252b5132
RH
8662 imm_expr.X_op = O_absent;
8663 s = expr_end;
8664 continue;
8665
4372b673 8666 case 'B': /* 20-bit syscall/break code. */
156c2f8b 8667 my_getExpression (&imm_expr, s);
156c2f8b 8668 check_absolute_expr (ip, &imm_expr);
793b27f4
TS
8669 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
8670 as_warn (_("Illegal 20-bit code (%lu)"),
8671 (unsigned long) imm_expr.X_add_number);
38487616 8672 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE20;
252b5132
RH
8673 imm_expr.X_op = O_absent;
8674 s = expr_end;
8675 continue;
8676
98d3f06f 8677 case 'C': /* Coprocessor code */
beae10d5 8678 my_getExpression (&imm_expr, s);
252b5132 8679 check_absolute_expr (ip, &imm_expr);
98d3f06f 8680 if ((unsigned long) imm_expr.X_add_number >= (1 << 25))
252b5132 8681 {
793b27f4
TS
8682 as_warn (_("Coproccesor code > 25 bits (%lu)"),
8683 (unsigned long) imm_expr.X_add_number);
98d3f06f 8684 imm_expr.X_add_number &= ((1 << 25) - 1);
252b5132 8685 }
beae10d5
KH
8686 ip->insn_opcode |= imm_expr.X_add_number;
8687 imm_expr.X_op = O_absent;
8688 s = expr_end;
8689 continue;
252b5132 8690
4372b673
NC
8691 case 'J': /* 19-bit wait code. */
8692 my_getExpression (&imm_expr, s);
8693 check_absolute_expr (ip, &imm_expr);
793b27f4
TS
8694 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
8695 as_warn (_("Illegal 19-bit code (%lu)"),
8696 (unsigned long) imm_expr.X_add_number);
38487616 8697 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE19;
4372b673
NC
8698 imm_expr.X_op = O_absent;
8699 s = expr_end;
8700 continue;
8701
252b5132 8702 case 'P': /* Performance register */
beae10d5 8703 my_getExpression (&imm_expr, s);
252b5132 8704 check_absolute_expr (ip, &imm_expr);
beae10d5 8705 if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
252b5132 8706 {
793b27f4
TS
8707 as_warn (_("Invalid performance register (%lu)"),
8708 (unsigned long) imm_expr.X_add_number);
38487616 8709 imm_expr.X_add_number &= OP_MASK_PERFREG;
252b5132 8710 }
38487616 8711 ip->insn_opcode |= (imm_expr.X_add_number << OP_SH_PERFREG);
beae10d5
KH
8712 imm_expr.X_op = O_absent;
8713 s = expr_end;
8714 continue;
252b5132
RH
8715
8716 case 'b': /* base register */
8717 case 'd': /* destination register */
8718 case 's': /* source register */
8719 case 't': /* target register */
8720 case 'r': /* both target and source */
8721 case 'v': /* both dest and source */
8722 case 'w': /* both dest and target */
8723 case 'E': /* coprocessor target register */
8724 case 'G': /* coprocessor destination register */
af7ee8bf 8725 case 'K': /* 'rdhwr' destination register */
252b5132
RH
8726 case 'x': /* ignore register name */
8727 case 'z': /* must be zero register */
4372b673 8728 case 'U': /* destination register (clo/clz). */
252b5132
RH
8729 s_reset = s;
8730 if (s[0] == '$')
8731 {
8732
3882b010 8733 if (ISDIGIT (s[1]))
252b5132
RH
8734 {
8735 ++s;
8736 regno = 0;
8737 do
8738 {
8739 regno *= 10;
8740 regno += *s - '0';
8741 ++s;
8742 }
3882b010 8743 while (ISDIGIT (*s));
252b5132
RH
8744 if (regno > 31)
8745 as_bad (_("Invalid register number (%d)"), regno);
8746 }
af7ee8bf 8747 else if (*args == 'E' || *args == 'G' || *args == 'K')
252b5132
RH
8748 goto notreg;
8749 else
8750 {
76db943d
TS
8751 if (s[1] == 'r' && s[2] == 'a')
8752 {
8753 s += 3;
8754 regno = RA;
8755 }
8756 else if (s[1] == 'f' && s[2] == 'p')
252b5132
RH
8757 {
8758 s += 3;
8759 regno = FP;
8760 }
8761 else if (s[1] == 's' && s[2] == 'p')
8762 {
8763 s += 3;
8764 regno = SP;
8765 }
8766 else if (s[1] == 'g' && s[2] == 'p')
8767 {
8768 s += 3;
8769 regno = GP;
8770 }
8771 else if (s[1] == 'a' && s[2] == 't')
8772 {
8773 s += 3;
8774 regno = AT;
8775 }
8776 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8777 {
8778 s += 4;
8779 regno = KT0;
8780 }
8781 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8782 {
8783 s += 4;
8784 regno = KT1;
8785 }
85b51719
TS
8786 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
8787 {
8788 s += 5;
8789 regno = ZERO;
8790 }
252b5132
RH
8791 else if (itbl_have_entries)
8792 {
8793 char *p, *n;
d7ba4a77 8794 unsigned long r;
252b5132 8795
d7ba4a77 8796 p = s + 1; /* advance past '$' */
252b5132
RH
8797 n = itbl_get_field (&p); /* n is name */
8798
d7ba4a77
ILT
8799 /* See if this is a register defined in an
8800 itbl entry. */
8801 if (itbl_get_reg_val (n, &r))
252b5132
RH
8802 {
8803 /* Get_field advances to the start of
8804 the next field, so we need to back
d7ba4a77 8805 rack to the end of the last field. */
bdaaa2e1 8806 if (p)
252b5132 8807 s = p - 1;
bdaaa2e1 8808 else
d7ba4a77 8809 s = strchr (s, '\0');
252b5132
RH
8810 regno = r;
8811 }
8812 else
8813 goto notreg;
beae10d5 8814 }
252b5132
RH
8815 else
8816 goto notreg;
8817 }
8818 if (regno == AT
8819 && ! mips_opts.noat
8820 && *args != 'E'
af7ee8bf
CD
8821 && *args != 'G'
8822 && *args != 'K')
252b5132
RH
8823 as_warn (_("Used $at without \".set noat\""));
8824 c = *args;
8825 if (*s == ' ')
f9419b05 8826 ++s;
252b5132
RH
8827 if (args[1] != *s)
8828 {
8829 if (c == 'r' || c == 'v' || c == 'w')
8830 {
8831 regno = lastregno;
8832 s = s_reset;
f9419b05 8833 ++args;
252b5132
RH
8834 }
8835 }
8836 /* 'z' only matches $0. */
8837 if (c == 'z' && regno != 0)
8838 break;
8839
bdaaa2e1
KH
8840 /* Now that we have assembled one operand, we use the args string
8841 * to figure out where it goes in the instruction. */
252b5132
RH
8842 switch (c)
8843 {
8844 case 'r':
8845 case 's':
8846 case 'v':
8847 case 'b':
38487616 8848 ip->insn_opcode |= regno << OP_SH_RS;
252b5132
RH
8849 break;
8850 case 'd':
8851 case 'G':
af7ee8bf 8852 case 'K':
38487616 8853 ip->insn_opcode |= regno << OP_SH_RD;
252b5132 8854 break;
4372b673 8855 case 'U':
38487616
TS
8856 ip->insn_opcode |= regno << OP_SH_RD;
8857 ip->insn_opcode |= regno << OP_SH_RT;
4372b673 8858 break;
252b5132
RH
8859 case 'w':
8860 case 't':
8861 case 'E':
38487616 8862 ip->insn_opcode |= regno << OP_SH_RT;
252b5132
RH
8863 break;
8864 case 'x':
8865 /* This case exists because on the r3000 trunc
8866 expands into a macro which requires a gp
8867 register. On the r6000 or r4000 it is
8868 assembled into a single instruction which
8869 ignores the register. Thus the insn version
8870 is MIPS_ISA2 and uses 'x', and the macro
8871 version is MIPS_ISA1 and uses 't'. */
8872 break;
8873 case 'z':
8874 /* This case is for the div instruction, which
8875 acts differently if the destination argument
8876 is $0. This only matches $0, and is checked
8877 outside the switch. */
8878 break;
8879 case 'D':
8880 /* Itbl operand; not yet implemented. FIXME ?? */
8881 break;
8882 /* What about all other operands like 'i', which
8883 can be specified in the opcode table? */
8884 }
8885 lastregno = regno;
8886 continue;
8887 }
8888 notreg:
8889 switch (*args++)
8890 {
8891 case 'r':
8892 case 'v':
38487616 8893 ip->insn_opcode |= lastregno << OP_SH_RS;
252b5132
RH
8894 continue;
8895 case 'w':
38487616 8896 ip->insn_opcode |= lastregno << OP_SH_RT;
252b5132
RH
8897 continue;
8898 }
8899 break;
8900
deec1734
CD
8901 case 'O': /* MDMX alignment immediate constant. */
8902 my_getExpression (&imm_expr, s);
8903 check_absolute_expr (ip, &imm_expr);
8904 if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
8905 {
8906 as_warn ("Improper align amount (%ld), using low bits",
8907 (long) imm_expr.X_add_number);
8908 imm_expr.X_add_number &= OP_MASK_ALN;
8909 }
8910 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_ALN;
8911 imm_expr.X_op = O_absent;
8912 s = expr_end;
8913 continue;
8914
8915 case 'Q': /* MDMX vector, element sel, or const. */
8916 if (s[0] != '$')
8917 {
8918 /* MDMX Immediate. */
8919 my_getExpression (&imm_expr, s);
8920 check_absolute_expr (ip, &imm_expr);
8921 if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
8922 {
8923 as_warn (_("Invalid MDMX Immediate (%ld)"),
8924 (long) imm_expr.X_add_number);
8925 imm_expr.X_add_number &= OP_MASK_FT;
8926 }
8927 imm_expr.X_add_number &= OP_MASK_FT;
8928 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8929 ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
8930 else
8931 ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
8932 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_FT;
8933 imm_expr.X_op = O_absent;
8934 s = expr_end;
8935 continue;
8936 }
8937 /* Not MDMX Immediate. Fall through. */
8938 case 'X': /* MDMX destination register. */
8939 case 'Y': /* MDMX source register. */
8940 case 'Z': /* MDMX target register. */
8941 is_mdmx = 1;
252b5132
RH
8942 case 'D': /* floating point destination register */
8943 case 'S': /* floating point source register */
8944 case 'T': /* floating point target register */
8945 case 'R': /* floating point source register */
8946 case 'V':
8947 case 'W':
8948 s_reset = s;
deec1734
CD
8949 /* Accept $fN for FP and MDMX register numbers, and in
8950 addition accept $vN for MDMX register numbers. */
8951 if ((s[0] == '$' && s[1] == 'f' && ISDIGIT (s[2]))
8952 || (is_mdmx != 0 && s[0] == '$' && s[1] == 'v'
8953 && ISDIGIT (s[2])))
252b5132
RH
8954 {
8955 s += 2;
8956 regno = 0;
8957 do
8958 {
8959 regno *= 10;
8960 regno += *s - '0';
8961 ++s;
8962 }
3882b010 8963 while (ISDIGIT (*s));
252b5132
RH
8964
8965 if (regno > 31)
8966 as_bad (_("Invalid float register number (%d)"), regno);
8967
8968 if ((regno & 1) != 0
ca4e0257 8969 && HAVE_32BIT_FPRS
252b5132
RH
8970 && ! (strcmp (str, "mtc1") == 0
8971 || strcmp (str, "mfc1") == 0
8972 || strcmp (str, "lwc1") == 0
8973 || strcmp (str, "swc1") == 0
8974 || strcmp (str, "l.s") == 0
8975 || strcmp (str, "s.s") == 0))
8976 as_warn (_("Float register should be even, was %d"),
8977 regno);
8978
8979 c = *args;
8980 if (*s == ' ')
f9419b05 8981 ++s;
252b5132
RH
8982 if (args[1] != *s)
8983 {
8984 if (c == 'V' || c == 'W')
8985 {
8986 regno = lastregno;
8987 s = s_reset;
f9419b05 8988 ++args;
252b5132
RH
8989 }
8990 }
8991 switch (c)
8992 {
8993 case 'D':
deec1734 8994 case 'X':
38487616 8995 ip->insn_opcode |= regno << OP_SH_FD;
252b5132
RH
8996 break;
8997 case 'V':
8998 case 'S':
deec1734 8999 case 'Y':
38487616 9000 ip->insn_opcode |= regno << OP_SH_FS;
252b5132 9001 break;
deec1734
CD
9002 case 'Q':
9003 /* This is like 'Z', but also needs to fix the MDMX
9004 vector/scalar select bits. Note that the
9005 scalar immediate case is handled above. */
9006 if (*s == '[')
9007 {
9008 int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
9009 int max_el = (is_qh ? 3 : 7);
9010 s++;
9011 my_getExpression(&imm_expr, s);
9012 check_absolute_expr (ip, &imm_expr);
9013 s = expr_end;
9014 if (imm_expr.X_add_number > max_el)
9015 as_bad(_("Bad element selector %ld"),
9016 (long) imm_expr.X_add_number);
9017 imm_expr.X_add_number &= max_el;
9018 ip->insn_opcode |= (imm_expr.X_add_number
9019 << (OP_SH_VSEL +
9020 (is_qh ? 2 : 1)));
9021 if (*s != ']')
9022 as_warn(_("Expecting ']' found '%s'"), s);
9023 else
9024 s++;
9025 }
9026 else
9027 {
9028 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
9029 ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
9030 << OP_SH_VSEL);
9031 else
9032 ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
9033 OP_SH_VSEL);
9034 }
9035 /* Fall through */
252b5132
RH
9036 case 'W':
9037 case 'T':
deec1734 9038 case 'Z':
38487616 9039 ip->insn_opcode |= regno << OP_SH_FT;
252b5132
RH
9040 break;
9041 case 'R':
38487616 9042 ip->insn_opcode |= regno << OP_SH_FR;
252b5132
RH
9043 break;
9044 }
9045 lastregno = regno;
9046 continue;
9047 }
9048
252b5132
RH
9049 switch (*args++)
9050 {
9051 case 'V':
38487616 9052 ip->insn_opcode |= lastregno << OP_SH_FS;
252b5132
RH
9053 continue;
9054 case 'W':
38487616 9055 ip->insn_opcode |= lastregno << OP_SH_FT;
252b5132
RH
9056 continue;
9057 }
9058 break;
9059
9060 case 'I':
9061 my_getExpression (&imm_expr, s);
9062 if (imm_expr.X_op != O_big
9063 && imm_expr.X_op != O_constant)
9064 insn_error = _("absolute expression required");
13757d0c 9065 normalize_constant_expr (&imm_expr);
252b5132
RH
9066 s = expr_end;
9067 continue;
9068
9069 case 'A':
9070 my_getExpression (&offset_expr, s);
f6688943 9071 *imm_reloc = BFD_RELOC_32;
252b5132
RH
9072 s = expr_end;
9073 continue;
9074
9075 case 'F':
9076 case 'L':
9077 case 'f':
9078 case 'l':
9079 {
9080 int f64;
ca4e0257 9081 int using_gprs;
252b5132
RH
9082 char *save_in;
9083 char *err;
9084 unsigned char temp[8];
9085 int len;
9086 unsigned int length;
9087 segT seg;
9088 subsegT subseg;
9089 char *p;
9090
9091 /* These only appear as the last operand in an
9092 instruction, and every instruction that accepts
9093 them in any variant accepts them in all variants.
9094 This means we don't have to worry about backing out
9095 any changes if the instruction does not match.
9096
9097 The difference between them is the size of the
9098 floating point constant and where it goes. For 'F'
9099 and 'L' the constant is 64 bits; for 'f' and 'l' it
9100 is 32 bits. Where the constant is placed is based
9101 on how the MIPS assembler does things:
9102 F -- .rdata
9103 L -- .lit8
9104 f -- immediate value
9105 l -- .lit4
9106
9107 The .lit4 and .lit8 sections are only used if
9108 permitted by the -G argument.
9109
9110 When generating embedded PIC code, we use the
9111 .lit8 section but not the .lit4 section (we can do
9112 .lit4 inline easily; we need to put .lit8
9113 somewhere in the data segment, and using .lit8
9114 permits the linker to eventually combine identical
ca4e0257
RS
9115 .lit8 entries).
9116
9117 The code below needs to know whether the target register
9118 is 32 or 64 bits wide. It relies on the fact 'f' and
9119 'F' are used with GPR-based instructions and 'l' and
9120 'L' are used with FPR-based instructions. */
252b5132
RH
9121
9122 f64 = *args == 'F' || *args == 'L';
ca4e0257 9123 using_gprs = *args == 'F' || *args == 'f';
252b5132
RH
9124
9125 save_in = input_line_pointer;
9126 input_line_pointer = s;
9127 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
9128 length = len;
9129 s = input_line_pointer;
9130 input_line_pointer = save_in;
9131 if (err != NULL && *err != '\0')
9132 {
9133 as_bad (_("Bad floating point constant: %s"), err);
9134 memset (temp, '\0', sizeof temp);
9135 length = f64 ? 8 : 4;
9136 }
9137
156c2f8b 9138 assert (length == (unsigned) (f64 ? 8 : 4));
252b5132
RH
9139
9140 if (*args == 'f'
9141 || (*args == 'l'
9142 && (! USE_GLOBAL_POINTER_OPT
9143 || mips_pic == EMBEDDED_PIC
9144 || g_switch_value < 4
9145 || (temp[0] == 0 && temp[1] == 0)
9146 || (temp[2] == 0 && temp[3] == 0))))
9147 {
9148 imm_expr.X_op = O_constant;
9149 if (! target_big_endian)
9150 imm_expr.X_add_number = bfd_getl32 (temp);
9151 else
9152 imm_expr.X_add_number = bfd_getb32 (temp);
9153 }
9154 else if (length > 4
119d663a 9155 && ! mips_disable_float_construction
ca4e0257
RS
9156 /* Constants can only be constructed in GPRs and
9157 copied to FPRs if the GPRs are at least as wide
9158 as the FPRs. Force the constant into memory if
9159 we are using 64-bit FPRs but the GPRs are only
9160 32 bits wide. */
9161 && (using_gprs
9162 || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
252b5132
RH
9163 && ((temp[0] == 0 && temp[1] == 0)
9164 || (temp[2] == 0 && temp[3] == 0))
9165 && ((temp[4] == 0 && temp[5] == 0)
9166 || (temp[6] == 0 && temp[7] == 0)))
9167 {
ca4e0257
RS
9168 /* The value is simple enough to load with a couple of
9169 instructions. If using 32-bit registers, set
9170 imm_expr to the high order 32 bits and offset_expr to
9171 the low order 32 bits. Otherwise, set imm_expr to
9172 the entire 64 bit constant. */
9173 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
252b5132
RH
9174 {
9175 imm_expr.X_op = O_constant;
9176 offset_expr.X_op = O_constant;
9177 if (! target_big_endian)
9178 {
9179 imm_expr.X_add_number = bfd_getl32 (temp + 4);
9180 offset_expr.X_add_number = bfd_getl32 (temp);
9181 }
9182 else
9183 {
9184 imm_expr.X_add_number = bfd_getb32 (temp);
9185 offset_expr.X_add_number = bfd_getb32 (temp + 4);
9186 }
9187 if (offset_expr.X_add_number == 0)
9188 offset_expr.X_op = O_absent;
9189 }
9190 else if (sizeof (imm_expr.X_add_number) > 4)
9191 {
9192 imm_expr.X_op = O_constant;
9193 if (! target_big_endian)
9194 imm_expr.X_add_number = bfd_getl64 (temp);
9195 else
9196 imm_expr.X_add_number = bfd_getb64 (temp);
9197 }
9198 else
9199 {
9200 imm_expr.X_op = O_big;
9201 imm_expr.X_add_number = 4;
9202 if (! target_big_endian)
9203 {
9204 generic_bignum[0] = bfd_getl16 (temp);
9205 generic_bignum[1] = bfd_getl16 (temp + 2);
9206 generic_bignum[2] = bfd_getl16 (temp + 4);
9207 generic_bignum[3] = bfd_getl16 (temp + 6);
9208 }
9209 else
9210 {
9211 generic_bignum[0] = bfd_getb16 (temp + 6);
9212 generic_bignum[1] = bfd_getb16 (temp + 4);
9213 generic_bignum[2] = bfd_getb16 (temp + 2);
9214 generic_bignum[3] = bfd_getb16 (temp);
9215 }
9216 }
9217 }
9218 else
9219 {
9220 const char *newname;
9221 segT new_seg;
9222
9223 /* Switch to the right section. */
9224 seg = now_seg;
9225 subseg = now_subseg;
9226 switch (*args)
9227 {
9228 default: /* unused default case avoids warnings. */
9229 case 'L':
9230 newname = RDATA_SECTION_NAME;
bb2d6cd7
GK
9231 if ((USE_GLOBAL_POINTER_OPT && g_switch_value >= 8)
9232 || mips_pic == EMBEDDED_PIC)
252b5132
RH
9233 newname = ".lit8";
9234 break;
9235 case 'F':
bb2d6cd7
GK
9236 if (mips_pic == EMBEDDED_PIC)
9237 newname = ".lit8";
9238 else
9239 newname = RDATA_SECTION_NAME;
252b5132
RH
9240 break;
9241 case 'l':
9242 assert (!USE_GLOBAL_POINTER_OPT
9243 || g_switch_value >= 4);
9244 newname = ".lit4";
9245 break;
9246 }
9247 new_seg = subseg_new (newname, (subsegT) 0);
9248 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
9249 bfd_set_section_flags (stdoutput, new_seg,
9250 (SEC_ALLOC
9251 | SEC_LOAD
9252 | SEC_READONLY
9253 | SEC_DATA));
9254 frag_align (*args == 'l' ? 2 : 3, 0, 0);
9255 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
9256 && strcmp (TARGET_OS, "elf") != 0)
9257 record_alignment (new_seg, 4);
9258 else
9259 record_alignment (new_seg, *args == 'l' ? 2 : 3);
9260 if (seg == now_seg)
9261 as_bad (_("Can't use floating point insn in this section"));
9262
9263 /* Set the argument to the current address in the
9264 section. */
9265 offset_expr.X_op = O_symbol;
9266 offset_expr.X_add_symbol =
9267 symbol_new ("L0\001", now_seg,
9268 (valueT) frag_now_fix (), frag_now);
9269 offset_expr.X_add_number = 0;
9270
9271 /* Put the floating point number into the section. */
9272 p = frag_more ((int) length);
9273 memcpy (p, temp, length);
9274
9275 /* Switch back to the original section. */
9276 subseg_set (seg, subseg);
9277 }
9278 }
9279 continue;
9280
9281 case 'i': /* 16 bit unsigned immediate */
9282 case 'j': /* 16 bit signed immediate */
f6688943 9283 *imm_reloc = BFD_RELOC_LO16;
5e0116d5 9284 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0)
252b5132
RH
9285 {
9286 int more;
5e0116d5
RS
9287 offsetT minval, maxval;
9288
9289 more = (insn + 1 < &mips_opcodes[NUMOPCODES]
9290 && strcmp (insn->name, insn[1].name) == 0);
9291
9292 /* If the expression was written as an unsigned number,
9293 only treat it as signed if there are no more
9294 alternatives. */
9295 if (more
9296 && *args == 'j'
9297 && sizeof (imm_expr.X_add_number) <= 4
9298 && imm_expr.X_op == O_constant
9299 && imm_expr.X_add_number < 0
9300 && imm_expr.X_unsigned
9301 && HAVE_64BIT_GPRS)
9302 break;
9303
9304 /* For compatibility with older assemblers, we accept
9305 0x8000-0xffff as signed 16-bit numbers when only
9306 signed numbers are allowed. */
9307 if (*args == 'i')
9308 minval = 0, maxval = 0xffff;
9309 else if (more)
9310 minval = -0x8000, maxval = 0x7fff;
252b5132 9311 else
5e0116d5
RS
9312 minval = -0x8000, maxval = 0xffff;
9313
9314 if (imm_expr.X_op != O_constant
9315 || imm_expr.X_add_number < minval
9316 || imm_expr.X_add_number > maxval)
252b5132
RH
9317 {
9318 if (more)
9319 break;
2ae7e77b
AH
9320 if (imm_expr.X_op == O_constant
9321 || imm_expr.X_op == O_big)
5e0116d5 9322 as_bad (_("expression out of range"));
252b5132
RH
9323 }
9324 }
9325 s = expr_end;
9326 continue;
9327
9328 case 'o': /* 16 bit offset */
5e0116d5
RS
9329 /* Check whether there is only a single bracketed expression
9330 left. If so, it must be the base register and the
9331 constant must be zero. */
9332 if (*s == '(' && strchr (s + 1, '(') == 0)
9333 {
9334 offset_expr.X_op = O_constant;
9335 offset_expr.X_add_number = 0;
9336 continue;
9337 }
252b5132
RH
9338
9339 /* If this value won't fit into a 16 bit offset, then go
9340 find a macro that will generate the 32 bit offset
afdbd6d0 9341 code pattern. */
5e0116d5 9342 if (my_getSmallExpression (&offset_expr, offset_reloc, s) == 0
252b5132
RH
9343 && (offset_expr.X_op != O_constant
9344 || offset_expr.X_add_number >= 0x8000
afdbd6d0 9345 || offset_expr.X_add_number < -0x8000))
252b5132
RH
9346 break;
9347
252b5132
RH
9348 s = expr_end;
9349 continue;
9350
9351 case 'p': /* pc relative offset */
0b25d3e6 9352 *offset_reloc = BFD_RELOC_16_PCREL_S2;
252b5132
RH
9353 my_getExpression (&offset_expr, s);
9354 s = expr_end;
9355 continue;
9356
9357 case 'u': /* upper 16 bits */
5e0116d5
RS
9358 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0
9359 && imm_expr.X_op == O_constant
9360 && (imm_expr.X_add_number < 0
9361 || imm_expr.X_add_number >= 0x10000))
252b5132
RH
9362 as_bad (_("lui expression not in range 0..65535"));
9363 s = expr_end;
9364 continue;
9365
9366 case 'a': /* 26 bit address */
9367 my_getExpression (&offset_expr, s);
9368 s = expr_end;
f6688943 9369 *offset_reloc = BFD_RELOC_MIPS_JMP;
252b5132
RH
9370 continue;
9371
9372 case 'N': /* 3 bit branch condition code */
9373 case 'M': /* 3 bit compare condition code */
9374 if (strncmp (s, "$fcc", 4) != 0)
9375 break;
9376 s += 4;
9377 regno = 0;
9378 do
9379 {
9380 regno *= 10;
9381 regno += *s - '0';
9382 ++s;
9383 }
3882b010 9384 while (ISDIGIT (*s));
252b5132 9385 if (regno > 7)
30c378fd
CD
9386 as_bad (_("Invalid condition code register $fcc%d"), regno);
9387 if ((strcmp(str + strlen(str) - 3, ".ps") == 0
9388 || strcmp(str + strlen(str) - 5, "any2f") == 0
9389 || strcmp(str + strlen(str) - 5, "any2t") == 0)
9390 && (regno & 1) != 0)
9391 as_warn(_("Condition code register should be even for %s, was %d"),
9392 str, regno);
9393 if ((strcmp(str + strlen(str) - 5, "any4f") == 0
9394 || strcmp(str + strlen(str) - 5, "any4t") == 0)
9395 && (regno & 3) != 0)
9396 as_warn(_("Condition code register should be 0 or 4 for %s, was %d"),
9397 str, regno);
252b5132
RH
9398 if (*args == 'N')
9399 ip->insn_opcode |= regno << OP_SH_BCC;
9400 else
9401 ip->insn_opcode |= regno << OP_SH_CCC;
beae10d5 9402 continue;
252b5132 9403
156c2f8b
NC
9404 case 'H':
9405 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
9406 s += 2;
3882b010 9407 if (ISDIGIT (*s))
156c2f8b
NC
9408 {
9409 c = 0;
9410 do
9411 {
9412 c *= 10;
9413 c += *s - '0';
9414 ++s;
9415 }
3882b010 9416 while (ISDIGIT (*s));
156c2f8b
NC
9417 }
9418 else
9419 c = 8; /* Invalid sel value. */
9420
9421 if (c > 7)
9422 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
9423 ip->insn_opcode |= c;
9424 continue;
9425
60b63b72
RS
9426 case 'e':
9427 /* Must be at least one digit. */
9428 my_getExpression (&imm_expr, s);
9429 check_absolute_expr (ip, &imm_expr);
9430
9431 if ((unsigned long) imm_expr.X_add_number
9432 > (unsigned long) OP_MASK_VECBYTE)
9433 {
9434 as_bad (_("bad byte vector index (%ld)"),
9435 (long) imm_expr.X_add_number);
9436 imm_expr.X_add_number = 0;
9437 }
9438
9439 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECBYTE;
9440 imm_expr.X_op = O_absent;
9441 s = expr_end;
9442 continue;
9443
9444 case '%':
9445 my_getExpression (&imm_expr, s);
9446 check_absolute_expr (ip, &imm_expr);
9447
9448 if ((unsigned long) imm_expr.X_add_number
9449 > (unsigned long) OP_MASK_VECALIGN)
9450 {
9451 as_bad (_("bad byte vector index (%ld)"),
9452 (long) imm_expr.X_add_number);
9453 imm_expr.X_add_number = 0;
9454 }
9455
9456 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECALIGN;
9457 imm_expr.X_op = O_absent;
9458 s = expr_end;
9459 continue;
9460
252b5132
RH
9461 default:
9462 as_bad (_("bad char = '%c'\n"), *args);
9463 internalError ();
9464 }
9465 break;
9466 }
9467 /* Args don't match. */
9468 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
9469 !strcmp (insn->name, insn[1].name))
9470 {
9471 ++insn;
9472 s = argsStart;
268f6bed 9473 insn_error = _("illegal operands");
252b5132
RH
9474 continue;
9475 }
268f6bed
L
9476 if (save_c)
9477 *(--s) = save_c;
252b5132
RH
9478 insn_error = _("illegal operands");
9479 return;
9480 }
9481}
9482
9483/* This routine assembles an instruction into its binary format when
9484 assembling for the mips16. As a side effect, it sets one of the
9485 global variables imm_reloc or offset_reloc to the type of
9486 relocation to do if one of the operands is an address expression.
9487 It also sets mips16_small and mips16_ext if the user explicitly
9488 requested a small or extended instruction. */
9489
9490static void
17a2f251 9491mips16_ip (char *str, struct mips_cl_insn *ip)
252b5132
RH
9492{
9493 char *s;
9494 const char *args;
9495 struct mips_opcode *insn;
9496 char *argsstart;
9497 unsigned int regno;
9498 unsigned int lastregno = 0;
9499 char *s_reset;
9500
9501 insn_error = NULL;
9502
b34976b6
AM
9503 mips16_small = FALSE;
9504 mips16_ext = FALSE;
252b5132 9505
3882b010 9506 for (s = str; ISLOWER (*s); ++s)
252b5132
RH
9507 ;
9508 switch (*s)
9509 {
9510 case '\0':
9511 break;
9512
9513 case ' ':
9514 *s++ = '\0';
9515 break;
9516
9517 case '.':
9518 if (s[1] == 't' && s[2] == ' ')
9519 {
9520 *s = '\0';
b34976b6 9521 mips16_small = TRUE;
252b5132
RH
9522 s += 3;
9523 break;
9524 }
9525 else if (s[1] == 'e' && s[2] == ' ')
9526 {
9527 *s = '\0';
b34976b6 9528 mips16_ext = TRUE;
252b5132
RH
9529 s += 3;
9530 break;
9531 }
9532 /* Fall through. */
9533 default:
9534 insn_error = _("unknown opcode");
9535 return;
9536 }
9537
9538 if (mips_opts.noautoextend && ! mips16_ext)
b34976b6 9539 mips16_small = TRUE;
252b5132
RH
9540
9541 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
9542 {
9543 insn_error = _("unrecognized opcode");
9544 return;
9545 }
9546
9547 argsstart = s;
9548 for (;;)
9549 {
9550 assert (strcmp (insn->name, str) == 0);
9551
9552 ip->insn_mo = insn;
9553 ip->insn_opcode = insn->match;
b34976b6 9554 ip->use_extend = FALSE;
252b5132 9555 imm_expr.X_op = O_absent;
f6688943
TS
9556 imm_reloc[0] = BFD_RELOC_UNUSED;
9557 imm_reloc[1] = BFD_RELOC_UNUSED;
9558 imm_reloc[2] = BFD_RELOC_UNUSED;
5f74bc13 9559 imm2_expr.X_op = O_absent;
252b5132 9560 offset_expr.X_op = O_absent;
f6688943
TS
9561 offset_reloc[0] = BFD_RELOC_UNUSED;
9562 offset_reloc[1] = BFD_RELOC_UNUSED;
9563 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
9564 for (args = insn->args; 1; ++args)
9565 {
9566 int c;
9567
9568 if (*s == ' ')
9569 ++s;
9570
9571 /* In this switch statement we call break if we did not find
9572 a match, continue if we did find a match, or return if we
9573 are done. */
9574
9575 c = *args;
9576 switch (c)
9577 {
9578 case '\0':
9579 if (*s == '\0')
9580 {
9581 /* Stuff the immediate value in now, if we can. */
9582 if (imm_expr.X_op == O_constant
f6688943 9583 && *imm_reloc > BFD_RELOC_UNUSED
252b5132
RH
9584 && insn->pinfo != INSN_MACRO)
9585 {
c4e7957c 9586 mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
b34976b6 9587 imm_expr.X_add_number, TRUE, mips16_small,
252b5132
RH
9588 mips16_ext, &ip->insn_opcode,
9589 &ip->use_extend, &ip->extend);
9590 imm_expr.X_op = O_absent;
f6688943 9591 *imm_reloc = BFD_RELOC_UNUSED;
252b5132
RH
9592 }
9593
9594 return;
9595 }
9596 break;
9597
9598 case ',':
9599 if (*s++ == c)
9600 continue;
9601 s--;
9602 switch (*++args)
9603 {
9604 case 'v':
9605 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9606 continue;
9607 case 'w':
9608 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9609 continue;
9610 }
9611 break;
9612
9613 case '(':
9614 case ')':
9615 if (*s++ == c)
9616 continue;
9617 break;
9618
9619 case 'v':
9620 case 'w':
9621 if (s[0] != '$')
9622 {
9623 if (c == 'v')
9624 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9625 else
9626 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9627 ++args;
9628 continue;
9629 }
9630 /* Fall through. */
9631 case 'x':
9632 case 'y':
9633 case 'z':
9634 case 'Z':
9635 case '0':
9636 case 'S':
9637 case 'R':
9638 case 'X':
9639 case 'Y':
9640 if (s[0] != '$')
9641 break;
9642 s_reset = s;
3882b010 9643 if (ISDIGIT (s[1]))
252b5132
RH
9644 {
9645 ++s;
9646 regno = 0;
9647 do
9648 {
9649 regno *= 10;
9650 regno += *s - '0';
9651 ++s;
9652 }
3882b010 9653 while (ISDIGIT (*s));
252b5132
RH
9654 if (regno > 31)
9655 {
9656 as_bad (_("invalid register number (%d)"), regno);
9657 regno = 2;
9658 }
9659 }
9660 else
9661 {
76db943d
TS
9662 if (s[1] == 'r' && s[2] == 'a')
9663 {
9664 s += 3;
9665 regno = RA;
9666 }
9667 else if (s[1] == 'f' && s[2] == 'p')
252b5132
RH
9668 {
9669 s += 3;
9670 regno = FP;
9671 }
9672 else if (s[1] == 's' && s[2] == 'p')
9673 {
9674 s += 3;
9675 regno = SP;
9676 }
9677 else if (s[1] == 'g' && s[2] == 'p')
9678 {
9679 s += 3;
9680 regno = GP;
9681 }
9682 else if (s[1] == 'a' && s[2] == 't')
9683 {
9684 s += 3;
9685 regno = AT;
9686 }
9687 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
9688 {
9689 s += 4;
9690 regno = KT0;
9691 }
9692 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
9693 {
9694 s += 4;
9695 regno = KT1;
9696 }
85b51719
TS
9697 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
9698 {
9699 s += 5;
9700 regno = ZERO;
9701 }
252b5132
RH
9702 else
9703 break;
9704 }
9705
9706 if (*s == ' ')
9707 ++s;
9708 if (args[1] != *s)
9709 {
9710 if (c == 'v' || c == 'w')
9711 {
9712 regno = mips16_to_32_reg_map[lastregno];
9713 s = s_reset;
f9419b05 9714 ++args;
252b5132
RH
9715 }
9716 }
9717
9718 switch (c)
9719 {
9720 case 'x':
9721 case 'y':
9722 case 'z':
9723 case 'v':
9724 case 'w':
9725 case 'Z':
9726 regno = mips32_to_16_reg_map[regno];
9727 break;
9728
9729 case '0':
9730 if (regno != 0)
9731 regno = ILLEGAL_REG;
9732 break;
9733
9734 case 'S':
9735 if (regno != SP)
9736 regno = ILLEGAL_REG;
9737 break;
9738
9739 case 'R':
9740 if (regno != RA)
9741 regno = ILLEGAL_REG;
9742 break;
9743
9744 case 'X':
9745 case 'Y':
9746 if (regno == AT && ! mips_opts.noat)
9747 as_warn (_("used $at without \".set noat\""));
9748 break;
9749
9750 default:
9751 internalError ();
9752 }
9753
9754 if (regno == ILLEGAL_REG)
9755 break;
9756
9757 switch (c)
9758 {
9759 case 'x':
9760 case 'v':
9761 ip->insn_opcode |= regno << MIPS16OP_SH_RX;
9762 break;
9763 case 'y':
9764 case 'w':
9765 ip->insn_opcode |= regno << MIPS16OP_SH_RY;
9766 break;
9767 case 'z':
9768 ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
9769 break;
9770 case 'Z':
9771 ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
9772 case '0':
9773 case 'S':
9774 case 'R':
9775 break;
9776 case 'X':
9777 ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
9778 break;
9779 case 'Y':
9780 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
9781 ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
9782 break;
9783 default:
9784 internalError ();
9785 }
9786
9787 lastregno = regno;
9788 continue;
9789
9790 case 'P':
9791 if (strncmp (s, "$pc", 3) == 0)
9792 {
9793 s += 3;
9794 continue;
9795 }
9796 break;
9797
9798 case '<':
9799 case '>':
9800 case '[':
9801 case ']':
9802 case '4':
9803 case '5':
9804 case 'H':
9805 case 'W':
9806 case 'D':
9807 case 'j':
9808 case '8':
9809 case 'V':
9810 case 'C':
9811 case 'U':
9812 case 'k':
9813 case 'K':
9814 if (s[0] == '%'
9815 && strncmp (s + 1, "gprel(", sizeof "gprel(" - 1) == 0)
9816 {
9817 /* This is %gprel(SYMBOL). We need to read SYMBOL,
9818 and generate the appropriate reloc. If the text
9819 inside %gprel is not a symbol name with an
9820 optional offset, then we generate a normal reloc
9821 and will probably fail later. */
9822 my_getExpression (&imm_expr, s + sizeof "%gprel" - 1);
9823 if (imm_expr.X_op == O_symbol)
9824 {
b34976b6 9825 mips16_ext = TRUE;
f6688943 9826 *imm_reloc = BFD_RELOC_MIPS16_GPREL;
252b5132 9827 s = expr_end;
b34976b6 9828 ip->use_extend = TRUE;
252b5132
RH
9829 ip->extend = 0;
9830 continue;
9831 }
9832 }
9833 else
9834 {
9835 /* Just pick up a normal expression. */
9836 my_getExpression (&imm_expr, s);
9837 }
9838
9839 if (imm_expr.X_op == O_register)
9840 {
9841 /* What we thought was an expression turned out to
9842 be a register. */
9843
9844 if (s[0] == '(' && args[1] == '(')
9845 {
9846 /* It looks like the expression was omitted
9847 before a register indirection, which means
9848 that the expression is implicitly zero. We
9849 still set up imm_expr, so that we handle
9850 explicit extensions correctly. */
9851 imm_expr.X_op = O_constant;
9852 imm_expr.X_add_number = 0;
f6688943 9853 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
9854 continue;
9855 }
9856
9857 break;
9858 }
9859
9860 /* We need to relax this instruction. */
f6688943 9861 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
9862 s = expr_end;
9863 continue;
9864
9865 case 'p':
9866 case 'q':
9867 case 'A':
9868 case 'B':
9869 case 'E':
9870 /* We use offset_reloc rather than imm_reloc for the PC
9871 relative operands. This lets macros with both
9872 immediate and address operands work correctly. */
9873 my_getExpression (&offset_expr, s);
9874
9875 if (offset_expr.X_op == O_register)
9876 break;
9877
9878 /* We need to relax this instruction. */
f6688943 9879 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
9880 s = expr_end;
9881 continue;
9882
9883 case '6': /* break code */
9884 my_getExpression (&imm_expr, s);
9885 check_absolute_expr (ip, &imm_expr);
9886 if ((unsigned long) imm_expr.X_add_number > 63)
9887 {
9888 as_warn (_("Invalid value for `%s' (%lu)"),
9889 ip->insn_mo->name,
9890 (unsigned long) imm_expr.X_add_number);
9891 imm_expr.X_add_number &= 0x3f;
9892 }
9893 ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
9894 imm_expr.X_op = O_absent;
9895 s = expr_end;
9896 continue;
9897
9898 case 'a': /* 26 bit address */
9899 my_getExpression (&offset_expr, s);
9900 s = expr_end;
f6688943 9901 *offset_reloc = BFD_RELOC_MIPS16_JMP;
252b5132
RH
9902 ip->insn_opcode <<= 16;
9903 continue;
9904
9905 case 'l': /* register list for entry macro */
9906 case 'L': /* register list for exit macro */
9907 {
9908 int mask;
9909
9910 if (c == 'l')
9911 mask = 0;
9912 else
9913 mask = 7 << 3;
9914 while (*s != '\0')
9915 {
9916 int freg, reg1, reg2;
9917
9918 while (*s == ' ' || *s == ',')
9919 ++s;
9920 if (*s != '$')
9921 {
9922 as_bad (_("can't parse register list"));
9923 break;
9924 }
9925 ++s;
9926 if (*s != 'f')
9927 freg = 0;
9928 else
9929 {
9930 freg = 1;
9931 ++s;
9932 }
9933 reg1 = 0;
3882b010 9934 while (ISDIGIT (*s))
252b5132
RH
9935 {
9936 reg1 *= 10;
9937 reg1 += *s - '0';
9938 ++s;
9939 }
9940 if (*s == ' ')
9941 ++s;
9942 if (*s != '-')
9943 reg2 = reg1;
9944 else
9945 {
9946 ++s;
9947 if (*s != '$')
9948 break;
9949 ++s;
9950 if (freg)
9951 {
9952 if (*s == 'f')
9953 ++s;
9954 else
9955 {
9956 as_bad (_("invalid register list"));
9957 break;
9958 }
9959 }
9960 reg2 = 0;
3882b010 9961 while (ISDIGIT (*s))
252b5132
RH
9962 {
9963 reg2 *= 10;
9964 reg2 += *s - '0';
9965 ++s;
9966 }
9967 }
9968 if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
9969 {
9970 mask &= ~ (7 << 3);
9971 mask |= 5 << 3;
9972 }
9973 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
9974 {
9975 mask &= ~ (7 << 3);
9976 mask |= 6 << 3;
9977 }
9978 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
9979 mask |= (reg2 - 3) << 3;
9980 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
9981 mask |= (reg2 - 15) << 1;
f9419b05 9982 else if (reg1 == RA && reg2 == RA)
252b5132
RH
9983 mask |= 1;
9984 else
9985 {
9986 as_bad (_("invalid register list"));
9987 break;
9988 }
9989 }
9990 /* The mask is filled in in the opcode table for the
9991 benefit of the disassembler. We remove it before
9992 applying the actual mask. */
9993 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
9994 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
9995 }
9996 continue;
9997
9998 case 'e': /* extend code */
9999 my_getExpression (&imm_expr, s);
10000 check_absolute_expr (ip, &imm_expr);
10001 if ((unsigned long) imm_expr.X_add_number > 0x7ff)
10002 {
10003 as_warn (_("Invalid value for `%s' (%lu)"),
10004 ip->insn_mo->name,
10005 (unsigned long) imm_expr.X_add_number);
10006 imm_expr.X_add_number &= 0x7ff;
10007 }
10008 ip->insn_opcode |= imm_expr.X_add_number;
10009 imm_expr.X_op = O_absent;
10010 s = expr_end;
10011 continue;
10012
10013 default:
10014 internalError ();
10015 }
10016 break;
10017 }
10018
10019 /* Args don't match. */
10020 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
10021 strcmp (insn->name, insn[1].name) == 0)
10022 {
10023 ++insn;
10024 s = argsstart;
10025 continue;
10026 }
10027
10028 insn_error = _("illegal operands");
10029
10030 return;
10031 }
10032}
10033
10034/* This structure holds information we know about a mips16 immediate
10035 argument type. */
10036
e972090a
NC
10037struct mips16_immed_operand
10038{
252b5132
RH
10039 /* The type code used in the argument string in the opcode table. */
10040 int type;
10041 /* The number of bits in the short form of the opcode. */
10042 int nbits;
10043 /* The number of bits in the extended form of the opcode. */
10044 int extbits;
10045 /* The amount by which the short form is shifted when it is used;
10046 for example, the sw instruction has a shift count of 2. */
10047 int shift;
10048 /* The amount by which the short form is shifted when it is stored
10049 into the instruction code. */
10050 int op_shift;
10051 /* Non-zero if the short form is unsigned. */
10052 int unsp;
10053 /* Non-zero if the extended form is unsigned. */
10054 int extu;
10055 /* Non-zero if the value is PC relative. */
10056 int pcrel;
10057};
10058
10059/* The mips16 immediate operand types. */
10060
10061static const struct mips16_immed_operand mips16_immed_operands[] =
10062{
10063 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
10064 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 },
10065 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
10066 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 },
10067 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
10068 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
10069 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
10070 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
10071 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
10072 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
10073 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
10074 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
10075 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
10076 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
10077 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
10078 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
10079 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
10080 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
10081 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
10082 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
10083 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
10084};
10085
10086#define MIPS16_NUM_IMMED \
10087 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
10088
10089/* Handle a mips16 instruction with an immediate value. This or's the
10090 small immediate value into *INSN. It sets *USE_EXTEND to indicate
10091 whether an extended value is needed; if one is needed, it sets
10092 *EXTEND to the value. The argument type is TYPE. The value is VAL.
10093 If SMALL is true, an unextended opcode was explicitly requested.
10094 If EXT is true, an extended opcode was explicitly requested. If
10095 WARN is true, warn if EXT does not match reality. */
10096
10097static void
17a2f251
TS
10098mips16_immed (char *file, unsigned int line, int type, offsetT val,
10099 bfd_boolean warn, bfd_boolean small, bfd_boolean ext,
10100 unsigned long *insn, bfd_boolean *use_extend,
10101 unsigned short *extend)
252b5132
RH
10102{
10103 register const struct mips16_immed_operand *op;
10104 int mintiny, maxtiny;
b34976b6 10105 bfd_boolean needext;
252b5132
RH
10106
10107 op = mips16_immed_operands;
10108 while (op->type != type)
10109 {
10110 ++op;
10111 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
10112 }
10113
10114 if (op->unsp)
10115 {
10116 if (type == '<' || type == '>' || type == '[' || type == ']')
10117 {
10118 mintiny = 1;
10119 maxtiny = 1 << op->nbits;
10120 }
10121 else
10122 {
10123 mintiny = 0;
10124 maxtiny = (1 << op->nbits) - 1;
10125 }
10126 }
10127 else
10128 {
10129 mintiny = - (1 << (op->nbits - 1));
10130 maxtiny = (1 << (op->nbits - 1)) - 1;
10131 }
10132
10133 /* Branch offsets have an implicit 0 in the lowest bit. */
10134 if (type == 'p' || type == 'q')
10135 val /= 2;
10136
10137 if ((val & ((1 << op->shift) - 1)) != 0
10138 || val < (mintiny << op->shift)
10139 || val > (maxtiny << op->shift))
b34976b6 10140 needext = TRUE;
252b5132 10141 else
b34976b6 10142 needext = FALSE;
252b5132
RH
10143
10144 if (warn && ext && ! needext)
beae10d5
KH
10145 as_warn_where (file, line,
10146 _("extended operand requested but not required"));
252b5132
RH
10147 if (small && needext)
10148 as_bad_where (file, line, _("invalid unextended operand value"));
10149
10150 if (small || (! ext && ! needext))
10151 {
10152 int insnval;
10153
b34976b6 10154 *use_extend = FALSE;
252b5132
RH
10155 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
10156 insnval <<= op->op_shift;
10157 *insn |= insnval;
10158 }
10159 else
10160 {
10161 long minext, maxext;
10162 int extval;
10163
10164 if (op->extu)
10165 {
10166 minext = 0;
10167 maxext = (1 << op->extbits) - 1;
10168 }
10169 else
10170 {
10171 minext = - (1 << (op->extbits - 1));
10172 maxext = (1 << (op->extbits - 1)) - 1;
10173 }
10174 if (val < minext || val > maxext)
10175 as_bad_where (file, line,
10176 _("operand value out of range for instruction"));
10177
b34976b6 10178 *use_extend = TRUE;
252b5132
RH
10179 if (op->extbits == 16)
10180 {
10181 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
10182 val &= 0x1f;
10183 }
10184 else if (op->extbits == 15)
10185 {
10186 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
10187 val &= 0xf;
10188 }
10189 else
10190 {
10191 extval = ((val & 0x1f) << 6) | (val & 0x20);
10192 val = 0;
10193 }
10194
10195 *extend = (unsigned short) extval;
10196 *insn |= val;
10197 }
10198}
10199\f
5e0116d5 10200static const struct percent_op_match
ad8d3bb3 10201{
5e0116d5
RS
10202 const char *str;
10203 bfd_reloc_code_real_type reloc;
ad8d3bb3
TS
10204} percent_op[] =
10205{
5e0116d5 10206 {"%lo", BFD_RELOC_LO16},
ad8d3bb3 10207#ifdef OBJ_ELF
5e0116d5
RS
10208 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
10209 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
10210 {"%call16", BFD_RELOC_MIPS_CALL16},
10211 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
10212 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
10213 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
10214 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
10215 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
10216 {"%got", BFD_RELOC_MIPS_GOT16},
10217 {"%gp_rel", BFD_RELOC_GPREL16},
10218 {"%half", BFD_RELOC_16},
10219 {"%highest", BFD_RELOC_MIPS_HIGHEST},
10220 {"%higher", BFD_RELOC_MIPS_HIGHER},
10221 {"%neg", BFD_RELOC_MIPS_SUB},
ad8d3bb3 10222#endif
5e0116d5 10223 {"%hi", BFD_RELOC_HI16_S}
ad8d3bb3
TS
10224};
10225
252b5132 10226
5e0116d5
RS
10227/* Return true if *STR points to a relocation operator. When returning true,
10228 move *STR over the operator and store its relocation code in *RELOC.
10229 Leave both *STR and *RELOC alone when returning false. */
10230
10231static bfd_boolean
17a2f251 10232parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
252b5132 10233{
5e0116d5 10234 size_t i;
76b3015f 10235
5e0116d5
RS
10236 for (i = 0; i < ARRAY_SIZE (percent_op); i++)
10237 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
394f9b3a 10238 {
5e0116d5
RS
10239 *str += strlen (percent_op[i].str);
10240 *reloc = percent_op[i].reloc;
394f9b3a 10241
5e0116d5
RS
10242 /* Check whether the output BFD supports this relocation.
10243 If not, issue an error and fall back on something safe. */
10244 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
394f9b3a 10245 {
5e0116d5
RS
10246 as_bad ("relocation %s isn't supported by the current ABI",
10247 percent_op[i].str);
10248 *reloc = BFD_RELOC_LO16;
394f9b3a 10249 }
5e0116d5 10250 return TRUE;
394f9b3a 10251 }
5e0116d5 10252 return FALSE;
394f9b3a 10253}
ad8d3bb3 10254
ad8d3bb3 10255
5e0116d5
RS
10256/* Parse string STR as a 16-bit relocatable operand. Store the
10257 expression in *EP and the relocations in the array starting
10258 at RELOC. Return the number of relocation operators used.
ad8d3bb3 10259
5e0116d5
RS
10260 On exit, EXPR_END points to the first character after the expression.
10261 If no relocation operators are used, RELOC[0] is set to BFD_RELOC_LO16. */
ad8d3bb3 10262
5e0116d5 10263static size_t
17a2f251
TS
10264my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
10265 char *str)
ad8d3bb3 10266{
5e0116d5
RS
10267 bfd_reloc_code_real_type reversed_reloc[3];
10268 size_t reloc_index, i;
09b8f35a
RS
10269 int crux_depth, str_depth;
10270 char *crux;
5e0116d5
RS
10271
10272 /* Search for the start of the main expression, recoding relocations
09b8f35a
RS
10273 in REVERSED_RELOC. End the loop with CRUX pointing to the start
10274 of the main expression and with CRUX_DEPTH containing the number
10275 of open brackets at that point. */
10276 reloc_index = -1;
10277 str_depth = 0;
10278 do
fb1b3232 10279 {
09b8f35a
RS
10280 reloc_index++;
10281 crux = str;
10282 crux_depth = str_depth;
10283
10284 /* Skip over whitespace and brackets, keeping count of the number
10285 of brackets. */
10286 while (*str == ' ' || *str == '\t' || *str == '(')
10287 if (*str++ == '(')
10288 str_depth++;
5e0116d5 10289 }
09b8f35a
RS
10290 while (*str == '%'
10291 && reloc_index < (HAVE_NEWABI ? 3 : 1)
10292 && parse_relocation (&str, &reversed_reloc[reloc_index]));
ad8d3bb3 10293
09b8f35a 10294 my_getExpression (ep, crux);
5e0116d5 10295 str = expr_end;
394f9b3a 10296
5e0116d5 10297 /* Match every open bracket. */
09b8f35a 10298 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
5e0116d5 10299 if (*str++ == ')')
09b8f35a 10300 crux_depth--;
394f9b3a 10301
09b8f35a 10302 if (crux_depth > 0)
5e0116d5 10303 as_bad ("unclosed '('");
394f9b3a 10304
5e0116d5 10305 expr_end = str;
252b5132 10306
64bdfcaf
RS
10307 if (reloc_index == 0)
10308 reloc[0] = BFD_RELOC_LO16;
10309 else
10310 {
10311 prev_reloc_op_frag = frag_now;
10312 for (i = 0; i < reloc_index; i++)
10313 reloc[i] = reversed_reloc[reloc_index - 1 - i];
10314 }
fb1b3232 10315
5e0116d5 10316 return reloc_index;
252b5132
RH
10317}
10318
10319static void
17a2f251 10320my_getExpression (expressionS *ep, char *str)
252b5132
RH
10321{
10322 char *save_in;
98aa84af 10323 valueT val;
252b5132
RH
10324
10325 save_in = input_line_pointer;
10326 input_line_pointer = str;
10327 expression (ep);
10328 expr_end = input_line_pointer;
10329 input_line_pointer = save_in;
10330
10331 /* If we are in mips16 mode, and this is an expression based on `.',
10332 then we bump the value of the symbol by 1 since that is how other
10333 text symbols are handled. We don't bother to handle complex
10334 expressions, just `.' plus or minus a constant. */
10335 if (mips_opts.mips16
10336 && ep->X_op == O_symbol
10337 && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
10338 && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
49309057
ILT
10339 && symbol_get_frag (ep->X_add_symbol) == frag_now
10340 && symbol_constant_p (ep->X_add_symbol)
98aa84af
AM
10341 && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
10342 S_SET_VALUE (ep->X_add_symbol, val + 1);
252b5132
RH
10343}
10344
10345/* Turn a string in input_line_pointer into a floating point constant
bc0d738a
NC
10346 of type TYPE, and store the appropriate bytes in *LITP. The number
10347 of LITTLENUMS emitted is stored in *SIZEP. An error message is
252b5132
RH
10348 returned, or NULL on OK. */
10349
10350char *
17a2f251 10351md_atof (int type, char *litP, int *sizeP)
252b5132
RH
10352{
10353 int prec;
10354 LITTLENUM_TYPE words[4];
10355 char *t;
10356 int i;
10357
10358 switch (type)
10359 {
10360 case 'f':
10361 prec = 2;
10362 break;
10363
10364 case 'd':
10365 prec = 4;
10366 break;
10367
10368 default:
10369 *sizeP = 0;
10370 return _("bad call to md_atof");
10371 }
10372
10373 t = atof_ieee (input_line_pointer, type, words);
10374 if (t)
10375 input_line_pointer = t;
10376
10377 *sizeP = prec * 2;
10378
10379 if (! target_big_endian)
10380 {
10381 for (i = prec - 1; i >= 0; i--)
10382 {
17a2f251 10383 md_number_to_chars (litP, words[i], 2);
252b5132
RH
10384 litP += 2;
10385 }
10386 }
10387 else
10388 {
10389 for (i = 0; i < prec; i++)
10390 {
17a2f251 10391 md_number_to_chars (litP, words[i], 2);
252b5132
RH
10392 litP += 2;
10393 }
10394 }
bdaaa2e1 10395
252b5132
RH
10396 return NULL;
10397}
10398
10399void
17a2f251 10400md_number_to_chars (char *buf, valueT val, int n)
252b5132
RH
10401{
10402 if (target_big_endian)
10403 number_to_chars_bigendian (buf, val, n);
10404 else
10405 number_to_chars_littleendian (buf, val, n);
10406}
10407\f
ae948b86 10408#ifdef OBJ_ELF
e013f690
TS
10409static int support_64bit_objects(void)
10410{
10411 const char **list, **l;
aa3d8fdf 10412 int yes;
e013f690
TS
10413
10414 list = bfd_target_list ();
10415 for (l = list; *l != NULL; l++)
10416#ifdef TE_TMIPS
10417 /* This is traditional mips */
10418 if (strcmp (*l, "elf64-tradbigmips") == 0
10419 || strcmp (*l, "elf64-tradlittlemips") == 0)
10420#else
10421 if (strcmp (*l, "elf64-bigmips") == 0
10422 || strcmp (*l, "elf64-littlemips") == 0)
10423#endif
10424 break;
aa3d8fdf 10425 yes = (*l != NULL);
e013f690 10426 free (list);
aa3d8fdf 10427 return yes;
e013f690 10428}
ae948b86 10429#endif /* OBJ_ELF */
e013f690 10430
5a38dc70 10431const char *md_shortopts = "nO::g::G:";
252b5132 10432
e972090a
NC
10433struct option md_longopts[] =
10434{
f9b4148d
CD
10435 /* Options which specify architecture. */
10436#define OPTION_ARCH_BASE (OPTION_MD_BASE)
10437#define OPTION_MARCH (OPTION_ARCH_BASE + 0)
10438 {"march", required_argument, NULL, OPTION_MARCH},
10439#define OPTION_MTUNE (OPTION_ARCH_BASE + 1)
10440 {"mtune", required_argument, NULL, OPTION_MTUNE},
10441#define OPTION_MIPS1 (OPTION_ARCH_BASE + 2)
252b5132
RH
10442 {"mips0", no_argument, NULL, OPTION_MIPS1},
10443 {"mips1", no_argument, NULL, OPTION_MIPS1},
f9b4148d 10444#define OPTION_MIPS2 (OPTION_ARCH_BASE + 3)
252b5132 10445 {"mips2", no_argument, NULL, OPTION_MIPS2},
f9b4148d 10446#define OPTION_MIPS3 (OPTION_ARCH_BASE + 4)
252b5132 10447 {"mips3", no_argument, NULL, OPTION_MIPS3},
f9b4148d 10448#define OPTION_MIPS4 (OPTION_ARCH_BASE + 5)
252b5132 10449 {"mips4", no_argument, NULL, OPTION_MIPS4},
f9b4148d 10450#define OPTION_MIPS5 (OPTION_ARCH_BASE + 6)
ae948b86 10451 {"mips5", no_argument, NULL, OPTION_MIPS5},
f9b4148d 10452#define OPTION_MIPS32 (OPTION_ARCH_BASE + 7)
ae948b86 10453 {"mips32", no_argument, NULL, OPTION_MIPS32},
f9b4148d 10454#define OPTION_MIPS64 (OPTION_ARCH_BASE + 8)
ae948b86 10455 {"mips64", no_argument, NULL, OPTION_MIPS64},
f9b4148d
CD
10456#define OPTION_MIPS32R2 (OPTION_ARCH_BASE + 9)
10457 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
5f74bc13
CD
10458#define OPTION_MIPS64R2 (OPTION_ARCH_BASE + 10)
10459 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
f9b4148d
CD
10460
10461 /* Options which specify Application Specific Extensions (ASEs). */
5f74bc13 10462#define OPTION_ASE_BASE (OPTION_ARCH_BASE + 11)
f9b4148d
CD
10463#define OPTION_MIPS16 (OPTION_ASE_BASE + 0)
10464 {"mips16", no_argument, NULL, OPTION_MIPS16},
10465#define OPTION_NO_MIPS16 (OPTION_ASE_BASE + 1)
10466 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
10467#define OPTION_MIPS3D (OPTION_ASE_BASE + 2)
10468 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
10469#define OPTION_NO_MIPS3D (OPTION_ASE_BASE + 3)
10470 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
10471#define OPTION_MDMX (OPTION_ASE_BASE + 4)
10472 {"mdmx", no_argument, NULL, OPTION_MDMX},
10473#define OPTION_NO_MDMX (OPTION_ASE_BASE + 5)
10474 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
10475
10476 /* Old-style architecture options. Don't add more of these. */
10477#define OPTION_COMPAT_ARCH_BASE (OPTION_ASE_BASE + 6)
10478#define OPTION_M4650 (OPTION_COMPAT_ARCH_BASE + 0)
10479 {"m4650", no_argument, NULL, OPTION_M4650},
10480#define OPTION_NO_M4650 (OPTION_COMPAT_ARCH_BASE + 1)
10481 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
10482#define OPTION_M4010 (OPTION_COMPAT_ARCH_BASE + 2)
10483 {"m4010", no_argument, NULL, OPTION_M4010},
10484#define OPTION_NO_M4010 (OPTION_COMPAT_ARCH_BASE + 3)
10485 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
10486#define OPTION_M4100 (OPTION_COMPAT_ARCH_BASE + 4)
10487 {"m4100", no_argument, NULL, OPTION_M4100},
10488#define OPTION_NO_M4100 (OPTION_COMPAT_ARCH_BASE + 5)
10489 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
10490#define OPTION_M3900 (OPTION_COMPAT_ARCH_BASE + 6)
10491 {"m3900", no_argument, NULL, OPTION_M3900},
10492#define OPTION_NO_M3900 (OPTION_COMPAT_ARCH_BASE + 7)
10493 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
10494
10495 /* Options which enable bug fixes. */
10496#define OPTION_FIX_BASE (OPTION_COMPAT_ARCH_BASE + 8)
10497#define OPTION_M7000_HILO_FIX (OPTION_FIX_BASE + 0)
10498 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
10499#define OPTION_MNO_7000_HILO_FIX (OPTION_FIX_BASE + 1)
10500 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10501 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10502#define OPTION_FIX_VR4122 (OPTION_FIX_BASE + 2)
10503#define OPTION_NO_FIX_VR4122 (OPTION_FIX_BASE + 3)
10504 {"mfix-vr4122-bugs", no_argument, NULL, OPTION_FIX_VR4122},
10505 {"no-mfix-vr4122-bugs", no_argument, NULL, OPTION_NO_FIX_VR4122},
10506
10507 /* Miscellaneous options. */
10508#define OPTION_MISC_BASE (OPTION_FIX_BASE + 4)
10509#define OPTION_MEMBEDDED_PIC (OPTION_MISC_BASE + 0)
252b5132 10510 {"membedded-pic", no_argument, NULL, OPTION_MEMBEDDED_PIC},
f9b4148d 10511#define OPTION_TRAP (OPTION_MISC_BASE + 1)
252b5132
RH
10512 {"trap", no_argument, NULL, OPTION_TRAP},
10513 {"no-break", no_argument, NULL, OPTION_TRAP},
f9b4148d 10514#define OPTION_BREAK (OPTION_MISC_BASE + 2)
252b5132
RH
10515 {"break", no_argument, NULL, OPTION_BREAK},
10516 {"no-trap", no_argument, NULL, OPTION_BREAK},
f9b4148d 10517#define OPTION_EB (OPTION_MISC_BASE + 3)
252b5132 10518 {"EB", no_argument, NULL, OPTION_EB},
f9b4148d 10519#define OPTION_EL (OPTION_MISC_BASE + 4)
252b5132 10520 {"EL", no_argument, NULL, OPTION_EL},
f9b4148d 10521#define OPTION_FP32 (OPTION_MISC_BASE + 5)
ae948b86 10522 {"mfp32", no_argument, NULL, OPTION_FP32},
f9b4148d 10523#define OPTION_GP32 (OPTION_MISC_BASE + 6)
c97ef257 10524 {"mgp32", no_argument, NULL, OPTION_GP32},
f9b4148d 10525#define OPTION_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 7)
119d663a 10526 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
f9b4148d 10527#define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 8)
119d663a 10528 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
f9b4148d 10529#define OPTION_FP64 (OPTION_MISC_BASE + 9)
316f5878 10530 {"mfp64", no_argument, NULL, OPTION_FP64},
f9b4148d 10531#define OPTION_GP64 (OPTION_MISC_BASE + 10)
ae948b86 10532 {"mgp64", no_argument, NULL, OPTION_GP64},
f9b4148d
CD
10533#define OPTION_RELAX_BRANCH (OPTION_MISC_BASE + 11)
10534#define OPTION_NO_RELAX_BRANCH (OPTION_MISC_BASE + 12)
4a6a3df4
AO
10535 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
10536 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
f9b4148d
CD
10537
10538 /* ELF-specific options. */
156c2f8b 10539#ifdef OBJ_ELF
f9b4148d 10540#define OPTION_ELF_BASE (OPTION_MISC_BASE + 13)
156c2f8b 10541#define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
156c2f8b
NC
10542 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
10543 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
ae948b86 10544#define OPTION_NON_SHARED (OPTION_ELF_BASE + 1)
156c2f8b 10545 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
ae948b86 10546#define OPTION_XGOT (OPTION_ELF_BASE + 2)
156c2f8b 10547 {"xgot", no_argument, NULL, OPTION_XGOT},
ae948b86
TS
10548#define OPTION_MABI (OPTION_ELF_BASE + 3)
10549 {"mabi", required_argument, NULL, OPTION_MABI},
10550#define OPTION_32 (OPTION_ELF_BASE + 4)
156c2f8b 10551 {"32", no_argument, NULL, OPTION_32},
ae948b86 10552#define OPTION_N32 (OPTION_ELF_BASE + 5)
e013f690 10553 {"n32", no_argument, NULL, OPTION_N32},
ae948b86 10554#define OPTION_64 (OPTION_ELF_BASE + 6)
156c2f8b 10555 {"64", no_argument, NULL, OPTION_64},
ecb4347a
DJ
10556#define OPTION_MDEBUG (OPTION_ELF_BASE + 7)
10557 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
10558#define OPTION_NO_MDEBUG (OPTION_ELF_BASE + 8)
10559 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
dcd410fe
RO
10560#define OPTION_PDR (OPTION_ELF_BASE + 9)
10561 {"mpdr", no_argument, NULL, OPTION_PDR},
10562#define OPTION_NO_PDR (OPTION_ELF_BASE + 10)
10563 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
ae948b86 10564#endif /* OBJ_ELF */
f9b4148d 10565
252b5132
RH
10566 {NULL, no_argument, NULL, 0}
10567};
156c2f8b 10568size_t md_longopts_size = sizeof (md_longopts);
252b5132 10569
316f5878
RS
10570/* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
10571 NEW_VALUE. Warn if another value was already specified. Note:
10572 we have to defer parsing the -march and -mtune arguments in order
10573 to handle 'from-abi' correctly, since the ABI might be specified
10574 in a later argument. */
10575
10576static void
17a2f251 10577mips_set_option_string (const char **string_ptr, const char *new_value)
316f5878
RS
10578{
10579 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
10580 as_warn (_("A different %s was already specified, is now %s"),
10581 string_ptr == &mips_arch_string ? "-march" : "-mtune",
10582 new_value);
10583
10584 *string_ptr = new_value;
10585}
10586
252b5132 10587int
17a2f251 10588md_parse_option (int c, char *arg)
252b5132
RH
10589{
10590 switch (c)
10591 {
119d663a
NC
10592 case OPTION_CONSTRUCT_FLOATS:
10593 mips_disable_float_construction = 0;
10594 break;
bdaaa2e1 10595
119d663a
NC
10596 case OPTION_NO_CONSTRUCT_FLOATS:
10597 mips_disable_float_construction = 1;
10598 break;
bdaaa2e1 10599
252b5132
RH
10600 case OPTION_TRAP:
10601 mips_trap = 1;
10602 break;
10603
10604 case OPTION_BREAK:
10605 mips_trap = 0;
10606 break;
10607
10608 case OPTION_EB:
10609 target_big_endian = 1;
10610 break;
10611
10612 case OPTION_EL:
10613 target_big_endian = 0;
10614 break;
10615
39c0a331
L
10616 case 'n':
10617 warn_nops = 1;
10618 break;
10619
252b5132
RH
10620 case 'O':
10621 if (arg && arg[1] == '0')
10622 mips_optimize = 1;
10623 else
10624 mips_optimize = 2;
10625 break;
10626
10627 case 'g':
10628 if (arg == NULL)
10629 mips_debug = 2;
10630 else
10631 mips_debug = atoi (arg);
10632 /* When the MIPS assembler sees -g or -g2, it does not do
10633 optimizations which limit full symbolic debugging. We take
10634 that to be equivalent to -O0. */
10635 if (mips_debug == 2)
10636 mips_optimize = 1;
10637 break;
10638
10639 case OPTION_MIPS1:
316f5878 10640 file_mips_isa = ISA_MIPS1;
252b5132
RH
10641 break;
10642
10643 case OPTION_MIPS2:
316f5878 10644 file_mips_isa = ISA_MIPS2;
252b5132
RH
10645 break;
10646
10647 case OPTION_MIPS3:
316f5878 10648 file_mips_isa = ISA_MIPS3;
252b5132
RH
10649 break;
10650
10651 case OPTION_MIPS4:
316f5878 10652 file_mips_isa = ISA_MIPS4;
e7af610e
NC
10653 break;
10654
84ea6cf2 10655 case OPTION_MIPS5:
316f5878 10656 file_mips_isa = ISA_MIPS5;
84ea6cf2
NC
10657 break;
10658
e7af610e 10659 case OPTION_MIPS32:
316f5878 10660 file_mips_isa = ISA_MIPS32;
252b5132
RH
10661 break;
10662
af7ee8bf
CD
10663 case OPTION_MIPS32R2:
10664 file_mips_isa = ISA_MIPS32R2;
10665 break;
10666
5f74bc13
CD
10667 case OPTION_MIPS64R2:
10668 file_mips_isa = ISA_MIPS64R2;
10669 break;
10670
84ea6cf2 10671 case OPTION_MIPS64:
316f5878 10672 file_mips_isa = ISA_MIPS64;
84ea6cf2
NC
10673 break;
10674
ec68c924 10675 case OPTION_MTUNE:
316f5878
RS
10676 mips_set_option_string (&mips_tune_string, arg);
10677 break;
ec68c924 10678
316f5878
RS
10679 case OPTION_MARCH:
10680 mips_set_option_string (&mips_arch_string, arg);
252b5132
RH
10681 break;
10682
10683 case OPTION_M4650:
316f5878
RS
10684 mips_set_option_string (&mips_arch_string, "4650");
10685 mips_set_option_string (&mips_tune_string, "4650");
252b5132
RH
10686 break;
10687
10688 case OPTION_NO_M4650:
10689 break;
10690
10691 case OPTION_M4010:
316f5878
RS
10692 mips_set_option_string (&mips_arch_string, "4010");
10693 mips_set_option_string (&mips_tune_string, "4010");
252b5132
RH
10694 break;
10695
10696 case OPTION_NO_M4010:
10697 break;
10698
10699 case OPTION_M4100:
316f5878
RS
10700 mips_set_option_string (&mips_arch_string, "4100");
10701 mips_set_option_string (&mips_tune_string, "4100");
252b5132
RH
10702 break;
10703
10704 case OPTION_NO_M4100:
10705 break;
10706
252b5132 10707 case OPTION_M3900:
316f5878
RS
10708 mips_set_option_string (&mips_arch_string, "3900");
10709 mips_set_option_string (&mips_tune_string, "3900");
252b5132 10710 break;
bdaaa2e1 10711
252b5132
RH
10712 case OPTION_NO_M3900:
10713 break;
10714
deec1734
CD
10715 case OPTION_MDMX:
10716 mips_opts.ase_mdmx = 1;
10717 break;
10718
10719 case OPTION_NO_MDMX:
10720 mips_opts.ase_mdmx = 0;
10721 break;
10722
252b5132
RH
10723 case OPTION_MIPS16:
10724 mips_opts.mips16 = 1;
b34976b6 10725 mips_no_prev_insn (FALSE);
252b5132
RH
10726 break;
10727
10728 case OPTION_NO_MIPS16:
10729 mips_opts.mips16 = 0;
b34976b6 10730 mips_no_prev_insn (FALSE);
252b5132
RH
10731 break;
10732
1f25f5d3
CD
10733 case OPTION_MIPS3D:
10734 mips_opts.ase_mips3d = 1;
10735 break;
10736
10737 case OPTION_NO_MIPS3D:
10738 mips_opts.ase_mips3d = 0;
10739 break;
10740
252b5132
RH
10741 case OPTION_MEMBEDDED_PIC:
10742 mips_pic = EMBEDDED_PIC;
10743 if (USE_GLOBAL_POINTER_OPT && g_switch_seen)
10744 {
10745 as_bad (_("-G may not be used with embedded PIC code"));
10746 return 0;
10747 }
10748 g_switch_value = 0x7fffffff;
10749 break;
10750
60b63b72
RS
10751 case OPTION_FIX_VR4122:
10752 mips_fix_4122_bugs = 1;
10753 break;
10754
10755 case OPTION_NO_FIX_VR4122:
10756 mips_fix_4122_bugs = 0;
10757 break;
10758
4a6a3df4
AO
10759 case OPTION_RELAX_BRANCH:
10760 mips_relax_branch = 1;
10761 break;
10762
10763 case OPTION_NO_RELAX_BRANCH:
10764 mips_relax_branch = 0;
10765 break;
10766
0f074f60 10767#ifdef OBJ_ELF
252b5132
RH
10768 /* When generating ELF code, we permit -KPIC and -call_shared to
10769 select SVR4_PIC, and -non_shared to select no PIC. This is
10770 intended to be compatible with Irix 5. */
10771 case OPTION_CALL_SHARED:
10772 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10773 {
10774 as_bad (_("-call_shared is supported only for ELF format"));
10775 return 0;
10776 }
10777 mips_pic = SVR4_PIC;
143d77c5 10778 mips_abicalls = TRUE;
252b5132
RH
10779 if (g_switch_seen && g_switch_value != 0)
10780 {
10781 as_bad (_("-G may not be used with SVR4 PIC code"));
10782 return 0;
10783 }
10784 g_switch_value = 0;
10785 break;
10786
10787 case OPTION_NON_SHARED:
10788 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10789 {
10790 as_bad (_("-non_shared is supported only for ELF format"));
10791 return 0;
10792 }
10793 mips_pic = NO_PIC;
143d77c5 10794 mips_abicalls = FALSE;
252b5132
RH
10795 break;
10796
10797 /* The -xgot option tells the assembler to use 32 offsets when
10798 accessing the got in SVR4_PIC mode. It is for Irix
10799 compatibility. */
10800 case OPTION_XGOT:
10801 mips_big_got = 1;
10802 break;
0f074f60 10803#endif /* OBJ_ELF */
252b5132
RH
10804
10805 case 'G':
10806 if (! USE_GLOBAL_POINTER_OPT)
10807 {
10808 as_bad (_("-G is not supported for this configuration"));
10809 return 0;
10810 }
10811 else if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
10812 {
10813 as_bad (_("-G may not be used with SVR4 or embedded PIC code"));
10814 return 0;
10815 }
10816 else
10817 g_switch_value = atoi (arg);
10818 g_switch_seen = 1;
10819 break;
10820
0f074f60 10821#ifdef OBJ_ELF
34ba82a8
TS
10822 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
10823 and -mabi=64. */
252b5132 10824 case OPTION_32:
34ba82a8
TS
10825 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10826 {
10827 as_bad (_("-32 is supported for ELF format only"));
10828 return 0;
10829 }
316f5878 10830 mips_abi = O32_ABI;
252b5132
RH
10831 break;
10832
e013f690 10833 case OPTION_N32:
34ba82a8
TS
10834 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10835 {
10836 as_bad (_("-n32 is supported for ELF format only"));
10837 return 0;
10838 }
316f5878 10839 mips_abi = N32_ABI;
e013f690 10840 break;
252b5132 10841
e013f690 10842 case OPTION_64:
34ba82a8
TS
10843 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10844 {
10845 as_bad (_("-64 is supported for ELF format only"));
10846 return 0;
10847 }
316f5878 10848 mips_abi = N64_ABI;
e013f690
TS
10849 if (! support_64bit_objects())
10850 as_fatal (_("No compiled in support for 64 bit object file format"));
252b5132 10851 break;
ae948b86 10852#endif /* OBJ_ELF */
252b5132 10853
c97ef257 10854 case OPTION_GP32:
a325df1d 10855 file_mips_gp32 = 1;
c97ef257
AH
10856 break;
10857
10858 case OPTION_GP64:
a325df1d 10859 file_mips_gp32 = 0;
c97ef257 10860 break;
252b5132 10861
ca4e0257 10862 case OPTION_FP32:
a325df1d 10863 file_mips_fp32 = 1;
316f5878
RS
10864 break;
10865
10866 case OPTION_FP64:
10867 file_mips_fp32 = 0;
ca4e0257
RS
10868 break;
10869
ae948b86 10870#ifdef OBJ_ELF
252b5132 10871 case OPTION_MABI:
34ba82a8
TS
10872 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10873 {
10874 as_bad (_("-mabi is supported for ELF format only"));
10875 return 0;
10876 }
e013f690 10877 if (strcmp (arg, "32") == 0)
316f5878 10878 mips_abi = O32_ABI;
e013f690 10879 else if (strcmp (arg, "o64") == 0)
316f5878 10880 mips_abi = O64_ABI;
e013f690 10881 else if (strcmp (arg, "n32") == 0)
316f5878 10882 mips_abi = N32_ABI;
e013f690
TS
10883 else if (strcmp (arg, "64") == 0)
10884 {
316f5878 10885 mips_abi = N64_ABI;
e013f690
TS
10886 if (! support_64bit_objects())
10887 as_fatal (_("No compiled in support for 64 bit object file "
10888 "format"));
10889 }
10890 else if (strcmp (arg, "eabi") == 0)
316f5878 10891 mips_abi = EABI_ABI;
e013f690 10892 else
da0e507f
TS
10893 {
10894 as_fatal (_("invalid abi -mabi=%s"), arg);
10895 return 0;
10896 }
252b5132 10897 break;
e013f690 10898#endif /* OBJ_ELF */
252b5132 10899
6b76fefe 10900 case OPTION_M7000_HILO_FIX:
b34976b6 10901 mips_7000_hilo_fix = TRUE;
6b76fefe
CM
10902 break;
10903
9ee72ff1 10904 case OPTION_MNO_7000_HILO_FIX:
b34976b6 10905 mips_7000_hilo_fix = FALSE;
6b76fefe
CM
10906 break;
10907
ecb4347a
DJ
10908#ifdef OBJ_ELF
10909 case OPTION_MDEBUG:
b34976b6 10910 mips_flag_mdebug = TRUE;
ecb4347a
DJ
10911 break;
10912
10913 case OPTION_NO_MDEBUG:
b34976b6 10914 mips_flag_mdebug = FALSE;
ecb4347a 10915 break;
dcd410fe
RO
10916
10917 case OPTION_PDR:
10918 mips_flag_pdr = TRUE;
10919 break;
10920
10921 case OPTION_NO_PDR:
10922 mips_flag_pdr = FALSE;
10923 break;
ecb4347a
DJ
10924#endif /* OBJ_ELF */
10925
252b5132
RH
10926 default:
10927 return 0;
10928 }
10929
10930 return 1;
10931}
316f5878
RS
10932\f
10933/* Set up globals to generate code for the ISA or processor
10934 described by INFO. */
252b5132 10935
252b5132 10936static void
17a2f251 10937mips_set_architecture (const struct mips_cpu_info *info)
252b5132 10938{
316f5878 10939 if (info != 0)
252b5132 10940 {
fef14a42
TS
10941 file_mips_arch = info->cpu;
10942 mips_opts.arch = info->cpu;
316f5878 10943 mips_opts.isa = info->isa;
252b5132 10944 }
252b5132
RH
10945}
10946
252b5132 10947
316f5878 10948/* Likewise for tuning. */
252b5132 10949
316f5878 10950static void
17a2f251 10951mips_set_tune (const struct mips_cpu_info *info)
316f5878
RS
10952{
10953 if (info != 0)
fef14a42 10954 mips_tune = info->cpu;
316f5878 10955}
80cc45a5 10956
34ba82a8 10957
252b5132 10958void
17a2f251 10959mips_after_parse_args (void)
e9670677 10960{
fef14a42
TS
10961 const struct mips_cpu_info *arch_info = 0;
10962 const struct mips_cpu_info *tune_info = 0;
10963
e9670677
MR
10964 /* GP relative stuff not working for PE */
10965 if (strncmp (TARGET_OS, "pe", 2) == 0
10966 && g_switch_value != 0)
10967 {
10968 if (g_switch_seen)
10969 as_bad (_("-G not supported in this configuration."));
10970 g_switch_value = 0;
10971 }
10972
cac012d6
AO
10973 if (mips_abi == NO_ABI)
10974 mips_abi = MIPS_DEFAULT_ABI;
10975
22923709
RS
10976 /* The following code determines the architecture and register size.
10977 Similar code was added to GCC 3.3 (see override_options() in
10978 config/mips/mips.c). The GAS and GCC code should be kept in sync
10979 as much as possible. */
e9670677 10980
316f5878 10981 if (mips_arch_string != 0)
fef14a42 10982 arch_info = mips_parse_cpu ("-march", mips_arch_string);
e9670677 10983
316f5878 10984 if (file_mips_isa != ISA_UNKNOWN)
e9670677 10985 {
316f5878 10986 /* Handle -mipsN. At this point, file_mips_isa contains the
fef14a42 10987 ISA level specified by -mipsN, while arch_info->isa contains
316f5878 10988 the -march selection (if any). */
fef14a42 10989 if (arch_info != 0)
e9670677 10990 {
316f5878
RS
10991 /* -march takes precedence over -mipsN, since it is more descriptive.
10992 There's no harm in specifying both as long as the ISA levels
10993 are the same. */
fef14a42 10994 if (file_mips_isa != arch_info->isa)
316f5878
RS
10995 as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
10996 mips_cpu_info_from_isa (file_mips_isa)->name,
fef14a42 10997 mips_cpu_info_from_isa (arch_info->isa)->name);
e9670677 10998 }
316f5878 10999 else
fef14a42 11000 arch_info = mips_cpu_info_from_isa (file_mips_isa);
e9670677
MR
11001 }
11002
fef14a42
TS
11003 if (arch_info == 0)
11004 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
e9670677 11005
fef14a42 11006 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
316f5878 11007 as_bad ("-march=%s is not compatible with the selected ABI",
fef14a42
TS
11008 arch_info->name);
11009
11010 mips_set_architecture (arch_info);
11011
11012 /* Optimize for file_mips_arch, unless -mtune selects a different processor. */
11013 if (mips_tune_string != 0)
11014 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
e9670677 11015
fef14a42
TS
11016 if (tune_info == 0)
11017 mips_set_tune (arch_info);
11018 else
11019 mips_set_tune (tune_info);
e9670677 11020
316f5878 11021 if (file_mips_gp32 >= 0)
e9670677 11022 {
316f5878
RS
11023 /* The user specified the size of the integer registers. Make sure
11024 it agrees with the ABI and ISA. */
11025 if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
11026 as_bad (_("-mgp64 used with a 32-bit processor"));
11027 else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
11028 as_bad (_("-mgp32 used with a 64-bit ABI"));
11029 else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
11030 as_bad (_("-mgp64 used with a 32-bit ABI"));
e9670677
MR
11031 }
11032 else
11033 {
316f5878
RS
11034 /* Infer the integer register size from the ABI and processor.
11035 Restrict ourselves to 32-bit registers if that's all the
11036 processor has, or if the ABI cannot handle 64-bit registers. */
11037 file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
11038 || !ISA_HAS_64BIT_REGS (mips_opts.isa));
e9670677
MR
11039 }
11040
316f5878
RS
11041 /* ??? GAS treats single-float processors as though they had 64-bit
11042 float registers (although it complains when double-precision
11043 instructions are used). As things stand, saying they have 32-bit
11044 registers would lead to spurious "register must be even" messages.
11045 So here we assume float registers are always the same size as
11046 integer ones, unless the user says otherwise. */
11047 if (file_mips_fp32 < 0)
11048 file_mips_fp32 = file_mips_gp32;
e9670677 11049
316f5878 11050 /* End of GCC-shared inference code. */
e9670677 11051
17a2f251
TS
11052 /* This flag is set when we have a 64-bit capable CPU but use only
11053 32-bit wide registers. Note that EABI does not use it. */
11054 if (ISA_HAS_64BIT_REGS (mips_opts.isa)
11055 && ((mips_abi == NO_ABI && file_mips_gp32 == 1)
11056 || mips_abi == O32_ABI))
316f5878 11057 mips_32bitmode = 1;
e9670677
MR
11058
11059 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
11060 as_bad (_("trap exception not supported at ISA 1"));
11061
e9670677
MR
11062 /* If the selected architecture includes support for ASEs, enable
11063 generation of code for them. */
a4672219 11064 if (mips_opts.mips16 == -1)
fef14a42 11065 mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_arch)) ? 1 : 0;
ffdefa66 11066 if (mips_opts.ase_mips3d == -1)
fef14a42 11067 mips_opts.ase_mips3d = (CPU_HAS_MIPS3D (file_mips_arch)) ? 1 : 0;
ffdefa66 11068 if (mips_opts.ase_mdmx == -1)
fef14a42 11069 mips_opts.ase_mdmx = (CPU_HAS_MDMX (file_mips_arch)) ? 1 : 0;
e9670677 11070
e9670677 11071 file_mips_isa = mips_opts.isa;
a4672219 11072 file_ase_mips16 = mips_opts.mips16;
e9670677
MR
11073 file_ase_mips3d = mips_opts.ase_mips3d;
11074 file_ase_mdmx = mips_opts.ase_mdmx;
11075 mips_opts.gp32 = file_mips_gp32;
11076 mips_opts.fp32 = file_mips_fp32;
11077
ecb4347a
DJ
11078 if (mips_flag_mdebug < 0)
11079 {
11080#ifdef OBJ_MAYBE_ECOFF
11081 if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
11082 mips_flag_mdebug = 1;
11083 else
11084#endif /* OBJ_MAYBE_ECOFF */
11085 mips_flag_mdebug = 0;
11086 }
e9670677
MR
11087}
11088\f
11089void
17a2f251 11090mips_init_after_args (void)
252b5132
RH
11091{
11092 /* initialize opcodes */
11093 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
beae10d5 11094 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
252b5132
RH
11095}
11096
11097long
17a2f251 11098md_pcrel_from (fixS *fixP)
252b5132 11099{
a7ebbfdf
TS
11100 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
11101 switch (fixP->fx_r_type)
11102 {
11103 case BFD_RELOC_16_PCREL_S2:
11104 case BFD_RELOC_MIPS_JMP:
11105 /* Return the address of the delay slot. */
11106 return addr + 4;
11107 default:
11108 return addr;
11109 }
252b5132
RH
11110}
11111
252b5132
RH
11112/* This is called before the symbol table is processed. In order to
11113 work with gcc when using mips-tfile, we must keep all local labels.
11114 However, in other cases, we want to discard them. If we were
11115 called with -g, but we didn't see any debugging information, it may
11116 mean that gcc is smuggling debugging information through to
11117 mips-tfile, in which case we must generate all local labels. */
11118
11119void
17a2f251 11120mips_frob_file_before_adjust (void)
252b5132
RH
11121{
11122#ifndef NO_ECOFF_DEBUGGING
11123 if (ECOFF_DEBUGGING
11124 && mips_debug != 0
11125 && ! ecoff_debugging_seen)
11126 flag_keep_locals = 1;
11127#endif
11128}
11129
11130/* Sort any unmatched HI16_S relocs so that they immediately precede
94f592af 11131 the corresponding LO reloc. This is called before md_apply_fix3 and
252b5132
RH
11132 tc_gen_reloc. Unmatched HI16_S relocs can only be generated by
11133 explicit use of the %hi modifier. */
11134
11135void
17a2f251 11136mips_frob_file (void)
252b5132
RH
11137{
11138 struct mips_hi_fixup *l;
11139
11140 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
11141 {
11142 segment_info_type *seginfo;
11143 int pass;
11144
5919d012 11145 assert (reloc_needs_lo_p (l->fixp->fx_r_type));
252b5132 11146
5919d012
RS
11147 /* If a GOT16 relocation turns out to be against a global symbol,
11148 there isn't supposed to be a matching LO. */
11149 if (l->fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
11150 && !pic_need_relax (l->fixp->fx_addsy, l->seg))
11151 continue;
11152
11153 /* Check quickly whether the next fixup happens to be a matching %lo. */
11154 if (fixup_has_matching_lo_p (l->fixp))
252b5132
RH
11155 continue;
11156
11157 /* Look through the fixups for this segment for a matching %lo.
11158 When we find one, move the %hi just in front of it. We do
11159 this in two passes. In the first pass, we try to find a
11160 unique %lo. In the second pass, we permit multiple %hi
11161 relocs for a single %lo (this is a GNU extension). */
11162 seginfo = seg_info (l->seg);
11163 for (pass = 0; pass < 2; pass++)
11164 {
11165 fixS *f, *prev;
11166
11167 prev = NULL;
11168 for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
11169 {
11170 /* Check whether this is a %lo fixup which matches l->fixp. */
11171 if (f->fx_r_type == BFD_RELOC_LO16
11172 && f->fx_addsy == l->fixp->fx_addsy
11173 && f->fx_offset == l->fixp->fx_offset
11174 && (pass == 1
11175 || prev == NULL
5919d012
RS
11176 || !reloc_needs_lo_p (prev->fx_r_type)
11177 || !fixup_has_matching_lo_p (prev)))
252b5132
RH
11178 {
11179 fixS **pf;
11180
11181 /* Move l->fixp before f. */
11182 for (pf = &seginfo->fix_root;
11183 *pf != l->fixp;
11184 pf = &(*pf)->fx_next)
11185 assert (*pf != NULL);
11186
11187 *pf = l->fixp->fx_next;
11188
11189 l->fixp->fx_next = f;
11190 if (prev == NULL)
11191 seginfo->fix_root = l->fixp;
11192 else
11193 prev->fx_next = l->fixp;
11194
11195 break;
11196 }
11197
11198 prev = f;
11199 }
11200
11201 if (f != NULL)
11202 break;
11203
11204#if 0 /* GCC code motion plus incomplete dead code elimination
11205 can leave a %hi without a %lo. */
11206 if (pass == 1)
11207 as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
11208 _("Unmatched %%hi reloc"));
11209#endif
11210 }
11211 }
11212}
11213
11214/* When generating embedded PIC code we need to use a special
11215 relocation to represent the difference of two symbols in the .text
11216 section (switch tables use a difference of this sort). See
11217 include/coff/mips.h for details. This macro checks whether this
11218 fixup requires the special reloc. */
11219#define SWITCH_TABLE(fixp) \
11220 ((fixp)->fx_r_type == BFD_RELOC_32 \
bb2d6cd7 11221 && OUTPUT_FLAVOR != bfd_target_elf_flavour \
252b5132
RH
11222 && (fixp)->fx_addsy != NULL \
11223 && (fixp)->fx_subsy != NULL \
11224 && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
11225 && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
11226
11227/* When generating embedded PIC code we must keep all PC relative
11228 relocations, in case the linker has to relax a call. We also need
f6688943
TS
11229 to keep relocations for switch table entries.
11230
11231 We may have combined relocations without symbols in the N32/N64 ABI.
11232 We have to prevent gas from dropping them. */
252b5132 11233
252b5132 11234int
17a2f251 11235mips_force_relocation (fixS *fixp)
252b5132 11236{
ae6063d4 11237 if (generic_force_reloc (fixp))
252b5132
RH
11238 return 1;
11239
f6688943
TS
11240 if (HAVE_NEWABI
11241 && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
11242 && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
11243 || fixp->fx_r_type == BFD_RELOC_HI16_S
11244 || fixp->fx_r_type == BFD_RELOC_LO16))
11245 return 1;
11246
252b5132
RH
11247 return (mips_pic == EMBEDDED_PIC
11248 && (fixp->fx_pcrel
11249 || SWITCH_TABLE (fixp)
11250 || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
11251 || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
11252}
11253
45f8dfe8
AO
11254/* This hook is called before a fix is simplified. We don't really
11255 decide whether to skip a fix here. Rather, we turn global symbols
11256 used as branch targets into local symbols, such that they undergo
11257 simplification. We can only do this if the symbol is defined and
11258 it is in the same section as the branch. If this doesn't hold, we
11259 emit a better error message than just saying the relocation is not
11260 valid for the selected object format.
11261
11262 FIXP is the fix-up we're going to try to simplify, SEG is the
11263 segment in which the fix up occurs. The return value should be
11264 non-zero to indicate the fix-up is valid for further
11265 simplifications. */
11266
11267int
17a2f251 11268mips_validate_fix (struct fix *fixP, asection *seg)
45f8dfe8
AO
11269{
11270 /* There's a lot of discussion on whether it should be possible to
11271 use R_MIPS_PC16 to represent branch relocations. The outcome
11272 seems to be that it can, but gas/bfd are very broken in creating
11273 RELA relocations for this, so for now we only accept branches to
11274 symbols in the same section. Anything else is of dubious value,
11275 since there's no guarantee that at link time the symbol would be
11276 in range. Even for branches to local symbols this is arguably
11277 wrong, since it we assume the symbol is not going to be
11278 overridden, which should be possible per ELF library semantics,
11279 but then, there isn't a dynamic relocation that could be used to
11280 this effect, and the target would likely be out of range as well.
11281
11282 Unfortunately, it seems that there is too much code out there
11283 that relies on branches to symbols that are global to be resolved
11284 as if they were local, like the IRIX tools do, so we do it as
11285 well, but with a warning so that people are reminded to fix their
11286 code. If we ever get back to using R_MIPS_PC16 for branch
11287 targets, this entire block should go away (and probably the
11288 whole function). */
11289
11290 if (fixP->fx_r_type == BFD_RELOC_16_PCREL_S2
11291 && (((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
11292 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
11293 && mips_pic != EMBEDDED_PIC)
11294 || bfd_reloc_type_lookup (stdoutput, BFD_RELOC_16_PCREL_S2) == NULL)
11295 && fixP->fx_addsy)
11296 {
11297 if (! S_IS_DEFINED (fixP->fx_addsy))
11298 {
11299 as_bad_where (fixP->fx_file, fixP->fx_line,
11300 _("Cannot branch to undefined symbol."));
11301 /* Avoid any further errors about this fixup. */
11302 fixP->fx_done = 1;
11303 }
11304 else if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
11305 {
11306 as_bad_where (fixP->fx_file, fixP->fx_line,
11307 _("Cannot branch to symbol in another section."));
11308 fixP->fx_done = 1;
11309 }
11310 else if (S_IS_EXTERNAL (fixP->fx_addsy))
11311 {
11312 symbolS *sym = fixP->fx_addsy;
11313
115695a8
CD
11314 if (mips_pic == SVR4_PIC)
11315 as_warn_where (fixP->fx_file, fixP->fx_line,
11316 _("Pretending global symbol used as branch target is local."));
45f8dfe8
AO
11317
11318 fixP->fx_addsy = symbol_create (S_GET_NAME (sym),
11319 S_GET_SEGMENT (sym),
11320 S_GET_VALUE (sym),
11321 symbol_get_frag (sym));
11322 copy_symbol_attributes (fixP->fx_addsy, sym);
11323 S_CLEAR_EXTERNAL (fixP->fx_addsy);
11324 assert (symbol_resolved_p (sym));
11325 symbol_mark_resolved (fixP->fx_addsy);
11326 }
11327 }
11328
11329 return 1;
11330}
11331
252b5132
RH
11332/* Apply a fixup to the object file. */
11333
94f592af 11334void
17a2f251 11335md_apply_fix3 (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 11336{
874e8986 11337 bfd_byte *buf;
98aa84af 11338 long insn;
ed6fb7bd 11339 static int previous_fx_r_type = 0;
a7ebbfdf 11340 reloc_howto_type *howto;
252b5132 11341
a7ebbfdf
TS
11342 /* We ignore generic BFD relocations we don't know about. */
11343 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
11344 if (! howto)
11345 return;
65551fa4 11346
252b5132
RH
11347 assert (fixP->fx_size == 4
11348 || fixP->fx_r_type == BFD_RELOC_16
11349 || fixP->fx_r_type == BFD_RELOC_64
f6688943
TS
11350 || fixP->fx_r_type == BFD_RELOC_CTOR
11351 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
252b5132 11352 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
a7ebbfdf 11353 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY);
252b5132 11354
a7ebbfdf 11355 buf = (bfd_byte *) (fixP->fx_frag->fr_literal + fixP->fx_where);
252b5132 11356
ed6fb7bd
SC
11357 /* We are not done if this is a composite relocation to set up gp. */
11358 if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel
11359 && !(fixP->fx_r_type == BFD_RELOC_MIPS_SUB
10181a0d
AO
11360 || (fixP->fx_r_type == BFD_RELOC_64
11361 && (previous_fx_r_type == BFD_RELOC_GPREL32
11362 || previous_fx_r_type == BFD_RELOC_GPREL16))
ed6fb7bd
SC
11363 || (previous_fx_r_type == BFD_RELOC_MIPS_SUB
11364 && (fixP->fx_r_type == BFD_RELOC_HI16_S
11365 || fixP->fx_r_type == BFD_RELOC_LO16))))
252b5132 11366 fixP->fx_done = 1;
ed6fb7bd 11367 previous_fx_r_type = fixP->fx_r_type;
252b5132
RH
11368
11369 switch (fixP->fx_r_type)
11370 {
11371 case BFD_RELOC_MIPS_JMP:
e369bcce
TS
11372 case BFD_RELOC_MIPS_SHIFT5:
11373 case BFD_RELOC_MIPS_SHIFT6:
11374 case BFD_RELOC_MIPS_GOT_DISP:
11375 case BFD_RELOC_MIPS_GOT_PAGE:
11376 case BFD_RELOC_MIPS_GOT_OFST:
11377 case BFD_RELOC_MIPS_SUB:
11378 case BFD_RELOC_MIPS_INSERT_A:
11379 case BFD_RELOC_MIPS_INSERT_B:
11380 case BFD_RELOC_MIPS_DELETE:
11381 case BFD_RELOC_MIPS_HIGHEST:
11382 case BFD_RELOC_MIPS_HIGHER:
11383 case BFD_RELOC_MIPS_SCN_DISP:
11384 case BFD_RELOC_MIPS_REL16:
11385 case BFD_RELOC_MIPS_RELGOT:
11386 case BFD_RELOC_MIPS_JALR:
252b5132
RH
11387 case BFD_RELOC_HI16:
11388 case BFD_RELOC_HI16_S:
cdf6fd85 11389 case BFD_RELOC_GPREL16:
252b5132
RH
11390 case BFD_RELOC_MIPS_LITERAL:
11391 case BFD_RELOC_MIPS_CALL16:
11392 case BFD_RELOC_MIPS_GOT16:
cdf6fd85 11393 case BFD_RELOC_GPREL32:
252b5132
RH
11394 case BFD_RELOC_MIPS_GOT_HI16:
11395 case BFD_RELOC_MIPS_GOT_LO16:
11396 case BFD_RELOC_MIPS_CALL_HI16:
11397 case BFD_RELOC_MIPS_CALL_LO16:
11398 case BFD_RELOC_MIPS16_GPREL:
11399 if (fixP->fx_pcrel)
11400 as_bad_where (fixP->fx_file, fixP->fx_line,
11401 _("Invalid PC relative reloc"));
11402 /* Nothing needed to do. The value comes from the reloc entry */
11403 break;
11404
11405 case BFD_RELOC_MIPS16_JMP:
11406 /* We currently always generate a reloc against a symbol, which
11407 means that we don't want an addend even if the symbol is
11408 defined. */
a7ebbfdf 11409 *valP = 0;
252b5132
RH
11410 break;
11411
11412 case BFD_RELOC_PCREL_HI16_S:
11413 /* The addend for this is tricky if it is internal, so we just
11414 do everything here rather than in bfd_install_relocation. */
a7ebbfdf 11415 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && !fixP->fx_done)
bb2d6cd7
GK
11416 break;
11417 if (fixP->fx_addsy
11418 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
252b5132
RH
11419 {
11420 /* For an external symbol adjust by the address to make it
11421 pcrel_offset. We use the address of the RELLO reloc
11422 which follows this one. */
a7ebbfdf 11423 *valP += (fixP->fx_next->fx_frag->fr_address
252b5132
RH
11424 + fixP->fx_next->fx_where);
11425 }
a7ebbfdf 11426 *valP = ((*valP + 0x8000) >> 16) & 0xffff;
252b5132
RH
11427 if (target_big_endian)
11428 buf += 2;
17a2f251 11429 md_number_to_chars (buf, *valP, 2);
252b5132
RH
11430 break;
11431
11432 case BFD_RELOC_PCREL_LO16:
11433 /* The addend for this is tricky if it is internal, so we just
11434 do everything here rather than in bfd_install_relocation. */
a7ebbfdf 11435 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && !fixP->fx_done)
bb2d6cd7
GK
11436 break;
11437 if (fixP->fx_addsy
11438 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
a7ebbfdf 11439 *valP += fixP->fx_frag->fr_address + fixP->fx_where;
252b5132
RH
11440 if (target_big_endian)
11441 buf += 2;
17a2f251 11442 md_number_to_chars (buf, *valP, 2);
252b5132
RH
11443 break;
11444
11445 case BFD_RELOC_64:
11446 /* This is handled like BFD_RELOC_32, but we output a sign
11447 extended value if we are only 32 bits. */
11448 if (fixP->fx_done
11449 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11450 {
11451 if (8 <= sizeof (valueT))
a7ebbfdf 11452 md_number_to_chars (buf, *valP, 8);
252b5132
RH
11453 else
11454 {
a7ebbfdf 11455 valueT hiv;
252b5132 11456
a7ebbfdf 11457 if ((*valP & 0x80000000) != 0)
252b5132
RH
11458 hiv = 0xffffffff;
11459 else
11460 hiv = 0;
a7ebbfdf
TS
11461 md_number_to_chars ((char *)(buf + target_big_endian ? 4 : 0),
11462 *valP, 4);
11463 md_number_to_chars ((char *)(buf + target_big_endian ? 0 : 4),
11464 hiv, 4);
252b5132
RH
11465 }
11466 }
11467 break;
11468
056350c6 11469 case BFD_RELOC_RVA:
252b5132
RH
11470 case BFD_RELOC_32:
11471 /* If we are deleting this reloc entry, we must fill in the
11472 value now. This can happen if we have a .word which is not
11473 resolved when it appears but is later defined. We also need
11474 to fill in the value if this is an embedded PIC switch table
11475 entry. */
11476 if (fixP->fx_done
11477 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
a7ebbfdf 11478 md_number_to_chars (buf, *valP, 4);
252b5132
RH
11479 break;
11480
11481 case BFD_RELOC_16:
11482 /* If we are deleting this reloc entry, we must fill in the
11483 value now. */
11484 assert (fixP->fx_size == 2);
11485 if (fixP->fx_done)
a7ebbfdf 11486 md_number_to_chars (buf, *valP, 2);
252b5132
RH
11487 break;
11488
11489 case BFD_RELOC_LO16:
11490 /* When handling an embedded PIC switch statement, we can wind
11491 up deleting a LO16 reloc. See the 'o' case in mips_ip. */
11492 if (fixP->fx_done)
11493 {
a7ebbfdf 11494 if (*valP + 0x8000 > 0xffff)
252b5132
RH
11495 as_bad_where (fixP->fx_file, fixP->fx_line,
11496 _("relocation overflow"));
252b5132
RH
11497 if (target_big_endian)
11498 buf += 2;
17a2f251 11499 md_number_to_chars (buf, *valP, 2);
252b5132
RH
11500 }
11501 break;
11502
11503 case BFD_RELOC_16_PCREL_S2:
a7ebbfdf 11504 if ((*valP & 0x3) != 0)
cb56d3d3 11505 as_bad_where (fixP->fx_file, fixP->fx_line,
a7ebbfdf 11506 _("Branch to odd address (%lx)"), (long) *valP);
cb56d3d3 11507
252b5132
RH
11508 /*
11509 * We need to save the bits in the instruction since fixup_segment()
11510 * might be deleting the relocation entry (i.e., a branch within
11511 * the current segment).
11512 */
a7ebbfdf 11513 if (! fixP->fx_done)
bb2d6cd7 11514 break;
252b5132
RH
11515
11516 /* update old instruction data */
252b5132
RH
11517 if (target_big_endian)
11518 insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
11519 else
11520 insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
11521
a7ebbfdf
TS
11522 if (*valP + 0x20000 <= 0x3ffff)
11523 {
11524 insn |= (*valP >> 2) & 0xffff;
17a2f251 11525 md_number_to_chars (buf, insn, 4);
a7ebbfdf
TS
11526 }
11527 else if (mips_pic == NO_PIC
11528 && fixP->fx_done
11529 && fixP->fx_frag->fr_address >= text_section->vma
11530 && (fixP->fx_frag->fr_address
11531 < text_section->vma + text_section->_raw_size)
11532 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
11533 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
11534 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
252b5132
RH
11535 {
11536 /* The branch offset is too large. If this is an
11537 unconditional branch, and we are not generating PIC code,
11538 we can convert it to an absolute jump instruction. */
a7ebbfdf
TS
11539 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
11540 insn = 0x0c000000; /* jal */
252b5132 11541 else
a7ebbfdf
TS
11542 insn = 0x08000000; /* j */
11543 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
11544 fixP->fx_done = 0;
11545 fixP->fx_addsy = section_symbol (text_section);
11546 *valP += md_pcrel_from (fixP);
17a2f251 11547 md_number_to_chars (buf, insn, 4);
a7ebbfdf
TS
11548 }
11549 else
11550 {
11551 /* If we got here, we have branch-relaxation disabled,
11552 and there's nothing we can do to fix this instruction
11553 without turning it into a longer sequence. */
11554 as_bad_where (fixP->fx_file, fixP->fx_line,
11555 _("Branch out of range"));
252b5132 11556 }
252b5132
RH
11557 break;
11558
11559 case BFD_RELOC_VTABLE_INHERIT:
11560 fixP->fx_done = 0;
11561 if (fixP->fx_addsy
11562 && !S_IS_DEFINED (fixP->fx_addsy)
11563 && !S_IS_WEAK (fixP->fx_addsy))
11564 S_SET_WEAK (fixP->fx_addsy);
11565 break;
11566
11567 case BFD_RELOC_VTABLE_ENTRY:
11568 fixP->fx_done = 0;
11569 break;
11570
11571 default:
11572 internalError ();
11573 }
a7ebbfdf
TS
11574
11575 /* Remember value for tc_gen_reloc. */
11576 fixP->fx_addnumber = *valP;
252b5132
RH
11577}
11578
11579#if 0
11580void
17a2f251 11581printInsn (unsigned long oc)
252b5132
RH
11582{
11583 const struct mips_opcode *p;
11584 int treg, sreg, dreg, shamt;
11585 short imm;
11586 const char *args;
11587 int i;
11588
11589 for (i = 0; i < NUMOPCODES; ++i)
11590 {
11591 p = &mips_opcodes[i];
11592 if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
11593 {
11594 printf ("%08lx %s\t", oc, p->name);
11595 treg = (oc >> 16) & 0x1f;
11596 sreg = (oc >> 21) & 0x1f;
11597 dreg = (oc >> 11) & 0x1f;
11598 shamt = (oc >> 6) & 0x1f;
11599 imm = oc;
11600 for (args = p->args;; ++args)
11601 {
11602 switch (*args)
11603 {
11604 case '\0':
11605 printf ("\n");
11606 break;
11607
11608 case ',':
11609 case '(':
11610 case ')':
11611 printf ("%c", *args);
11612 continue;
11613
11614 case 'r':
11615 assert (treg == sreg);
11616 printf ("$%d,$%d", treg, sreg);
11617 continue;
11618
11619 case 'd':
11620 case 'G':
11621 printf ("$%d", dreg);
11622 continue;
11623
11624 case 't':
11625 case 'E':
11626 printf ("$%d", treg);
11627 continue;
11628
11629 case 'k':
11630 printf ("0x%x", treg);
11631 continue;
11632
11633 case 'b':
11634 case 's':
11635 printf ("$%d", sreg);
11636 continue;
11637
11638 case 'a':
11639 printf ("0x%08lx", oc & 0x1ffffff);
11640 continue;
11641
11642 case 'i':
11643 case 'j':
11644 case 'o':
11645 case 'u':
11646 printf ("%d", imm);
11647 continue;
11648
11649 case '<':
11650 case '>':
11651 printf ("$%d", shamt);
11652 continue;
11653
11654 default:
11655 internalError ();
11656 }
11657 break;
11658 }
11659 return;
11660 }
11661 }
11662 printf (_("%08lx UNDEFINED\n"), oc);
11663}
11664#endif
11665
11666static symbolS *
17a2f251 11667get_symbol (void)
252b5132
RH
11668{
11669 int c;
11670 char *name;
11671 symbolS *p;
11672
11673 name = input_line_pointer;
11674 c = get_symbol_end ();
11675 p = (symbolS *) symbol_find_or_make (name);
11676 *input_line_pointer = c;
11677 return p;
11678}
11679
11680/* Align the current frag to a given power of two. The MIPS assembler
11681 also automatically adjusts any preceding label. */
11682
11683static void
17a2f251 11684mips_align (int to, int fill, symbolS *label)
252b5132 11685{
b34976b6 11686 mips_emit_delays (FALSE);
252b5132
RH
11687 frag_align (to, fill, 0);
11688 record_alignment (now_seg, to);
11689 if (label != NULL)
11690 {
11691 assert (S_GET_SEGMENT (label) == now_seg);
49309057 11692 symbol_set_frag (label, frag_now);
252b5132
RH
11693 S_SET_VALUE (label, (valueT) frag_now_fix ());
11694 }
11695}
11696
11697/* Align to a given power of two. .align 0 turns off the automatic
11698 alignment used by the data creating pseudo-ops. */
11699
11700static void
17a2f251 11701s_align (int x ATTRIBUTE_UNUSED)
252b5132
RH
11702{
11703 register int temp;
11704 register long temp_fill;
11705 long max_alignment = 15;
11706
11707 /*
11708
67c1ffbe 11709 o Note that the assembler pulls down any immediately preceding label
252b5132
RH
11710 to the aligned address.
11711 o It's not documented but auto alignment is reinstated by
11712 a .align pseudo instruction.
11713 o Note also that after auto alignment is turned off the mips assembler
11714 issues an error on attempt to assemble an improperly aligned data item.
11715 We don't.
11716
11717 */
11718
11719 temp = get_absolute_expression ();
11720 if (temp > max_alignment)
11721 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
11722 else if (temp < 0)
11723 {
11724 as_warn (_("Alignment negative: 0 assumed."));
11725 temp = 0;
11726 }
11727 if (*input_line_pointer == ',')
11728 {
f9419b05 11729 ++input_line_pointer;
252b5132
RH
11730 temp_fill = get_absolute_expression ();
11731 }
11732 else
11733 temp_fill = 0;
11734 if (temp)
11735 {
11736 auto_align = 1;
11737 mips_align (temp, (int) temp_fill,
11738 insn_labels != NULL ? insn_labels->label : NULL);
11739 }
11740 else
11741 {
11742 auto_align = 0;
11743 }
11744
11745 demand_empty_rest_of_line ();
11746}
11747
11748void
17a2f251 11749mips_flush_pending_output (void)
252b5132 11750{
b34976b6 11751 mips_emit_delays (FALSE);
252b5132
RH
11752 mips_clear_insn_labels ();
11753}
11754
11755static void
17a2f251 11756s_change_sec (int sec)
252b5132
RH
11757{
11758 segT seg;
11759
11760 /* When generating embedded PIC code, we only use the .text, .lit8,
11761 .sdata and .sbss sections. We change the .data and .rdata
11762 pseudo-ops to use .sdata. */
11763 if (mips_pic == EMBEDDED_PIC
11764 && (sec == 'd' || sec == 'r'))
11765 sec = 's';
11766
11767#ifdef OBJ_ELF
11768 /* The ELF backend needs to know that we are changing sections, so
11769 that .previous works correctly. We could do something like check
b6ff326e 11770 for an obj_section_change_hook macro, but that might be confusing
252b5132
RH
11771 as it would not be appropriate to use it in the section changing
11772 functions in read.c, since obj-elf.c intercepts those. FIXME:
11773 This should be cleaner, somehow. */
11774 obj_elf_section_change_hook ();
11775#endif
11776
b34976b6 11777 mips_emit_delays (FALSE);
252b5132
RH
11778 switch (sec)
11779 {
11780 case 't':
11781 s_text (0);
11782 break;
11783 case 'd':
11784 s_data (0);
11785 break;
11786 case 'b':
11787 subseg_set (bss_section, (subsegT) get_absolute_expression ());
11788 demand_empty_rest_of_line ();
11789 break;
11790
11791 case 'r':
11792 if (USE_GLOBAL_POINTER_OPT)
11793 {
11794 seg = subseg_new (RDATA_SECTION_NAME,
11795 (subsegT) get_absolute_expression ());
11796 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11797 {
11798 bfd_set_section_flags (stdoutput, seg,
11799 (SEC_ALLOC
11800 | SEC_LOAD
11801 | SEC_READONLY
11802 | SEC_RELOC
11803 | SEC_DATA));
11804 if (strcmp (TARGET_OS, "elf") != 0)
e799a695 11805 record_alignment (seg, 4);
252b5132
RH
11806 }
11807 demand_empty_rest_of_line ();
11808 }
11809 else
11810 {
11811 as_bad (_("No read only data section in this object file format"));
11812 demand_empty_rest_of_line ();
11813 return;
11814 }
11815 break;
11816
11817 case 's':
11818 if (USE_GLOBAL_POINTER_OPT)
11819 {
11820 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
11821 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11822 {
11823 bfd_set_section_flags (stdoutput, seg,
11824 SEC_ALLOC | SEC_LOAD | SEC_RELOC
11825 | SEC_DATA);
11826 if (strcmp (TARGET_OS, "elf") != 0)
e799a695 11827 record_alignment (seg, 4);
252b5132
RH
11828 }
11829 demand_empty_rest_of_line ();
11830 break;
11831 }
11832 else
11833 {
11834 as_bad (_("Global pointers not supported; recompile -G 0"));
11835 demand_empty_rest_of_line ();
11836 return;
11837 }
11838 }
11839
11840 auto_align = 1;
11841}
b34976b6 11842
cca86cc8 11843void
17a2f251 11844s_change_section (int ignore ATTRIBUTE_UNUSED)
cca86cc8 11845{
7ed4a06a 11846#ifdef OBJ_ELF
cca86cc8
SC
11847 char *section_name;
11848 char c;
684022ea 11849 char next_c = 0;
cca86cc8
SC
11850 int section_type;
11851 int section_flag;
11852 int section_entry_size;
11853 int section_alignment;
b34976b6 11854
7ed4a06a
TS
11855 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11856 return;
11857
cca86cc8
SC
11858 section_name = input_line_pointer;
11859 c = get_symbol_end ();
a816d1ed
AO
11860 if (c)
11861 next_c = *(input_line_pointer + 1);
cca86cc8 11862
4cf0dd0d
TS
11863 /* Do we have .section Name<,"flags">? */
11864 if (c != ',' || (c == ',' && next_c == '"'))
cca86cc8 11865 {
4cf0dd0d
TS
11866 /* just after name is now '\0'. */
11867 *input_line_pointer = c;
cca86cc8
SC
11868 input_line_pointer = section_name;
11869 obj_elf_section (ignore);
11870 return;
11871 }
11872 input_line_pointer++;
11873
11874 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
11875 if (c == ',')
11876 section_type = get_absolute_expression ();
11877 else
11878 section_type = 0;
11879 if (*input_line_pointer++ == ',')
11880 section_flag = get_absolute_expression ();
11881 else
11882 section_flag = 0;
11883 if (*input_line_pointer++ == ',')
11884 section_entry_size = get_absolute_expression ();
11885 else
11886 section_entry_size = 0;
11887 if (*input_line_pointer++ == ',')
11888 section_alignment = get_absolute_expression ();
11889 else
11890 section_alignment = 0;
11891
a816d1ed
AO
11892 section_name = xstrdup (section_name);
11893
8ab8a5c8
RS
11894 /* When using the generic form of .section (as implemented by obj-elf.c),
11895 there's no way to set the section type to SHT_MIPS_DWARF. Users have
11896 traditionally had to fall back on the more common @progbits instead.
11897
11898 There's nothing really harmful in this, since bfd will correct
11899 SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file. But it
11900 means that, for backwards compatibiltiy, the special_section entries
11901 for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
11902
11903 Even so, we shouldn't force users of the MIPS .section syntax to
11904 incorrectly label the sections as SHT_PROGBITS. The best compromise
11905 seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
11906 generic type-checking code. */
11907 if (section_type == SHT_MIPS_DWARF)
11908 section_type = SHT_PROGBITS;
11909
cca86cc8
SC
11910 obj_elf_change_section (section_name, section_type, section_flag,
11911 section_entry_size, 0, 0, 0);
a816d1ed
AO
11912
11913 if (now_seg->name != section_name)
11914 free (section_name);
7ed4a06a 11915#endif /* OBJ_ELF */
cca86cc8 11916}
252b5132
RH
11917
11918void
17a2f251 11919mips_enable_auto_align (void)
252b5132
RH
11920{
11921 auto_align = 1;
11922}
11923
11924static void
17a2f251 11925s_cons (int log_size)
252b5132
RH
11926{
11927 symbolS *label;
11928
11929 label = insn_labels != NULL ? insn_labels->label : NULL;
b34976b6 11930 mips_emit_delays (FALSE);
252b5132
RH
11931 if (log_size > 0 && auto_align)
11932 mips_align (log_size, 0, label);
11933 mips_clear_insn_labels ();
11934 cons (1 << log_size);
11935}
11936
11937static void
17a2f251 11938s_float_cons (int type)
252b5132
RH
11939{
11940 symbolS *label;
11941
11942 label = insn_labels != NULL ? insn_labels->label : NULL;
11943
b34976b6 11944 mips_emit_delays (FALSE);
252b5132
RH
11945
11946 if (auto_align)
49309057
ILT
11947 {
11948 if (type == 'd')
11949 mips_align (3, 0, label);
11950 else
11951 mips_align (2, 0, label);
11952 }
252b5132
RH
11953
11954 mips_clear_insn_labels ();
11955
11956 float_cons (type);
11957}
11958
11959/* Handle .globl. We need to override it because on Irix 5 you are
11960 permitted to say
11961 .globl foo .text
11962 where foo is an undefined symbol, to mean that foo should be
11963 considered to be the address of a function. */
11964
11965static void
17a2f251 11966s_mips_globl (int x ATTRIBUTE_UNUSED)
252b5132
RH
11967{
11968 char *name;
11969 int c;
11970 symbolS *symbolP;
11971 flagword flag;
11972
11973 name = input_line_pointer;
11974 c = get_symbol_end ();
11975 symbolP = symbol_find_or_make (name);
11976 *input_line_pointer = c;
11977 SKIP_WHITESPACE ();
11978
11979 /* On Irix 5, every global symbol that is not explicitly labelled as
11980 being a function is apparently labelled as being an object. */
11981 flag = BSF_OBJECT;
11982
11983 if (! is_end_of_line[(unsigned char) *input_line_pointer])
11984 {
11985 char *secname;
11986 asection *sec;
11987
11988 secname = input_line_pointer;
11989 c = get_symbol_end ();
11990 sec = bfd_get_section_by_name (stdoutput, secname);
11991 if (sec == NULL)
11992 as_bad (_("%s: no such section"), secname);
11993 *input_line_pointer = c;
11994
11995 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
11996 flag = BSF_FUNCTION;
11997 }
11998
49309057 11999 symbol_get_bfdsym (symbolP)->flags |= flag;
252b5132
RH
12000
12001 S_SET_EXTERNAL (symbolP);
12002 demand_empty_rest_of_line ();
12003}
12004
12005static void
17a2f251 12006s_option (int x ATTRIBUTE_UNUSED)
252b5132
RH
12007{
12008 char *opt;
12009 char c;
12010
12011 opt = input_line_pointer;
12012 c = get_symbol_end ();
12013
12014 if (*opt == 'O')
12015 {
12016 /* FIXME: What does this mean? */
12017 }
12018 else if (strncmp (opt, "pic", 3) == 0)
12019 {
12020 int i;
12021
12022 i = atoi (opt + 3);
12023 if (i == 0)
12024 mips_pic = NO_PIC;
12025 else if (i == 2)
143d77c5 12026 {
252b5132 12027 mips_pic = SVR4_PIC;
143d77c5
EC
12028 mips_abicalls = TRUE;
12029 }
252b5132
RH
12030 else
12031 as_bad (_(".option pic%d not supported"), i);
12032
12033 if (USE_GLOBAL_POINTER_OPT && mips_pic == SVR4_PIC)
12034 {
12035 if (g_switch_seen && g_switch_value != 0)
12036 as_warn (_("-G may not be used with SVR4 PIC code"));
12037 g_switch_value = 0;
12038 bfd_set_gp_size (stdoutput, 0);
12039 }
12040 }
12041 else
12042 as_warn (_("Unrecognized option \"%s\""), opt);
12043
12044 *input_line_pointer = c;
12045 demand_empty_rest_of_line ();
12046}
12047
12048/* This structure is used to hold a stack of .set values. */
12049
e972090a
NC
12050struct mips_option_stack
12051{
252b5132
RH
12052 struct mips_option_stack *next;
12053 struct mips_set_options options;
12054};
12055
12056static struct mips_option_stack *mips_opts_stack;
12057
12058/* Handle the .set pseudo-op. */
12059
12060static void
17a2f251 12061s_mipsset (int x ATTRIBUTE_UNUSED)
252b5132
RH
12062{
12063 char *name = input_line_pointer, ch;
12064
12065 while (!is_end_of_line[(unsigned char) *input_line_pointer])
f9419b05 12066 ++input_line_pointer;
252b5132
RH
12067 ch = *input_line_pointer;
12068 *input_line_pointer = '\0';
12069
12070 if (strcmp (name, "reorder") == 0)
12071 {
12072 if (mips_opts.noreorder && prev_nop_frag != NULL)
12073 {
12074 /* If we still have pending nops, we can discard them. The
12075 usual nop handling will insert any that are still
bdaaa2e1 12076 needed. */
252b5132
RH
12077 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
12078 * (mips_opts.mips16 ? 2 : 4));
12079 prev_nop_frag = NULL;
12080 }
12081 mips_opts.noreorder = 0;
12082 }
12083 else if (strcmp (name, "noreorder") == 0)
12084 {
b34976b6 12085 mips_emit_delays (TRUE);
252b5132
RH
12086 mips_opts.noreorder = 1;
12087 mips_any_noreorder = 1;
12088 }
12089 else if (strcmp (name, "at") == 0)
12090 {
12091 mips_opts.noat = 0;
12092 }
12093 else if (strcmp (name, "noat") == 0)
12094 {
12095 mips_opts.noat = 1;
12096 }
12097 else if (strcmp (name, "macro") == 0)
12098 {
12099 mips_opts.warn_about_macros = 0;
12100 }
12101 else if (strcmp (name, "nomacro") == 0)
12102 {
12103 if (mips_opts.noreorder == 0)
12104 as_bad (_("`noreorder' must be set before `nomacro'"));
12105 mips_opts.warn_about_macros = 1;
12106 }
12107 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
12108 {
12109 mips_opts.nomove = 0;
12110 }
12111 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
12112 {
12113 mips_opts.nomove = 1;
12114 }
12115 else if (strcmp (name, "bopt") == 0)
12116 {
12117 mips_opts.nobopt = 0;
12118 }
12119 else if (strcmp (name, "nobopt") == 0)
12120 {
12121 mips_opts.nobopt = 1;
12122 }
12123 else if (strcmp (name, "mips16") == 0
12124 || strcmp (name, "MIPS-16") == 0)
12125 mips_opts.mips16 = 1;
12126 else if (strcmp (name, "nomips16") == 0
12127 || strcmp (name, "noMIPS-16") == 0)
12128 mips_opts.mips16 = 0;
1f25f5d3
CD
12129 else if (strcmp (name, "mips3d") == 0)
12130 mips_opts.ase_mips3d = 1;
12131 else if (strcmp (name, "nomips3d") == 0)
12132 mips_opts.ase_mips3d = 0;
a4672219
TS
12133 else if (strcmp (name, "mdmx") == 0)
12134 mips_opts.ase_mdmx = 1;
12135 else if (strcmp (name, "nomdmx") == 0)
12136 mips_opts.ase_mdmx = 0;
1a2c1fad 12137 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
252b5132 12138 {
af7ee8bf 12139 int reset = 0;
252b5132 12140
1a2c1fad
CD
12141 /* Permit the user to change the ISA and architecture on the fly.
12142 Needless to say, misuse can cause serious problems. */
af7ee8bf
CD
12143 if (strcmp (name, "mips0") == 0)
12144 {
12145 reset = 1;
12146 mips_opts.isa = file_mips_isa;
12147 }
12148 else if (strcmp (name, "mips1") == 0)
12149 mips_opts.isa = ISA_MIPS1;
12150 else if (strcmp (name, "mips2") == 0)
12151 mips_opts.isa = ISA_MIPS2;
12152 else if (strcmp (name, "mips3") == 0)
12153 mips_opts.isa = ISA_MIPS3;
12154 else if (strcmp (name, "mips4") == 0)
12155 mips_opts.isa = ISA_MIPS4;
12156 else if (strcmp (name, "mips5") == 0)
12157 mips_opts.isa = ISA_MIPS5;
12158 else if (strcmp (name, "mips32") == 0)
12159 mips_opts.isa = ISA_MIPS32;
12160 else if (strcmp (name, "mips32r2") == 0)
12161 mips_opts.isa = ISA_MIPS32R2;
12162 else if (strcmp (name, "mips64") == 0)
12163 mips_opts.isa = ISA_MIPS64;
5f74bc13
CD
12164 else if (strcmp (name, "mips64r2") == 0)
12165 mips_opts.isa = ISA_MIPS64R2;
1a2c1fad
CD
12166 else if (strcmp (name, "arch=default") == 0)
12167 {
12168 reset = 1;
12169 mips_opts.arch = file_mips_arch;
12170 mips_opts.isa = file_mips_isa;
12171 }
12172 else if (strncmp (name, "arch=", 5) == 0)
12173 {
12174 const struct mips_cpu_info *p;
12175
12176 p = mips_parse_cpu("internal use", name + 5);
12177 if (!p)
12178 as_bad (_("unknown architecture %s"), name + 5);
12179 else
12180 {
12181 mips_opts.arch = p->cpu;
12182 mips_opts.isa = p->isa;
12183 }
12184 }
af7ee8bf
CD
12185 else
12186 as_bad (_("unknown ISA level %s"), name + 4);
12187
12188 switch (mips_opts.isa)
98d3f06f
KH
12189 {
12190 case 0:
98d3f06f 12191 break;
af7ee8bf
CD
12192 case ISA_MIPS1:
12193 case ISA_MIPS2:
12194 case ISA_MIPS32:
12195 case ISA_MIPS32R2:
98d3f06f
KH
12196 mips_opts.gp32 = 1;
12197 mips_opts.fp32 = 1;
12198 break;
af7ee8bf
CD
12199 case ISA_MIPS3:
12200 case ISA_MIPS4:
12201 case ISA_MIPS5:
12202 case ISA_MIPS64:
5f74bc13 12203 case ISA_MIPS64R2:
98d3f06f
KH
12204 mips_opts.gp32 = 0;
12205 mips_opts.fp32 = 0;
12206 break;
12207 default:
12208 as_bad (_("unknown ISA level %s"), name + 4);
12209 break;
12210 }
af7ee8bf 12211 if (reset)
98d3f06f 12212 {
af7ee8bf
CD
12213 mips_opts.gp32 = file_mips_gp32;
12214 mips_opts.fp32 = file_mips_fp32;
98d3f06f 12215 }
252b5132
RH
12216 }
12217 else if (strcmp (name, "autoextend") == 0)
12218 mips_opts.noautoextend = 0;
12219 else if (strcmp (name, "noautoextend") == 0)
12220 mips_opts.noautoextend = 1;
12221 else if (strcmp (name, "push") == 0)
12222 {
12223 struct mips_option_stack *s;
12224
12225 s = (struct mips_option_stack *) xmalloc (sizeof *s);
12226 s->next = mips_opts_stack;
12227 s->options = mips_opts;
12228 mips_opts_stack = s;
12229 }
12230 else if (strcmp (name, "pop") == 0)
12231 {
12232 struct mips_option_stack *s;
12233
12234 s = mips_opts_stack;
12235 if (s == NULL)
12236 as_bad (_(".set pop with no .set push"));
12237 else
12238 {
12239 /* If we're changing the reorder mode we need to handle
12240 delay slots correctly. */
12241 if (s->options.noreorder && ! mips_opts.noreorder)
b34976b6 12242 mips_emit_delays (TRUE);
252b5132
RH
12243 else if (! s->options.noreorder && mips_opts.noreorder)
12244 {
12245 if (prev_nop_frag != NULL)
12246 {
12247 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
12248 * (mips_opts.mips16 ? 2 : 4));
12249 prev_nop_frag = NULL;
12250 }
12251 }
12252
12253 mips_opts = s->options;
12254 mips_opts_stack = s->next;
12255 free (s);
12256 }
12257 }
12258 else
12259 {
12260 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
12261 }
12262 *input_line_pointer = ch;
12263 demand_empty_rest_of_line ();
12264}
12265
12266/* Handle the .abicalls pseudo-op. I believe this is equivalent to
12267 .option pic2. It means to generate SVR4 PIC calls. */
12268
12269static void
17a2f251 12270s_abicalls (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
12271{
12272 mips_pic = SVR4_PIC;
143d77c5 12273 mips_abicalls = TRUE;
252b5132
RH
12274 if (USE_GLOBAL_POINTER_OPT)
12275 {
12276 if (g_switch_seen && g_switch_value != 0)
12277 as_warn (_("-G may not be used with SVR4 PIC code"));
12278 g_switch_value = 0;
12279 }
12280 bfd_set_gp_size (stdoutput, 0);
12281 demand_empty_rest_of_line ();
12282}
12283
12284/* Handle the .cpload pseudo-op. This is used when generating SVR4
12285 PIC code. It sets the $gp register for the function based on the
12286 function address, which is in the register named in the argument.
12287 This uses a relocation against _gp_disp, which is handled specially
12288 by the linker. The result is:
12289 lui $gp,%hi(_gp_disp)
12290 addiu $gp,$gp,%lo(_gp_disp)
12291 addu $gp,$gp,.cpload argument
12292 The .cpload argument is normally $25 == $t9. */
12293
12294static void
17a2f251 12295s_cpload (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
12296{
12297 expressionS ex;
12298 int icnt = 0;
12299
6478892d
TS
12300 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12301 .cpload is ignored. */
12302 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
12303 {
12304 s_ignore (0);
12305 return;
12306 }
12307
d3ecfc59 12308 /* .cpload should be in a .set noreorder section. */
252b5132
RH
12309 if (mips_opts.noreorder == 0)
12310 as_warn (_(".cpload not in noreorder section"));
12311
12312 ex.X_op = O_symbol;
12313 ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
12314 ex.X_op_symbol = NULL;
12315 ex.X_add_number = 0;
12316
12317 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
49309057 12318 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
252b5132 12319
c9914766 12320 macro_build_lui (NULL, &icnt, &ex, mips_gp_register);
17a2f251
TS
12321 macro_build (NULL, &icnt, &ex, "addiu", "t,r,j", mips_gp_register,
12322 mips_gp_register, BFD_RELOC_LO16);
252b5132 12323
17a2f251
TS
12324 macro_build (NULL, &icnt, NULL, "addu", "d,v,t", mips_gp_register,
12325 mips_gp_register, tc_get_register (0));
252b5132
RH
12326
12327 demand_empty_rest_of_line ();
12328}
12329
6478892d
TS
12330/* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
12331 .cpsetup $reg1, offset|$reg2, label
12332
12333 If offset is given, this results in:
12334 sd $gp, offset($sp)
956cd1d6 12335 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
12336 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
12337 daddu $gp, $gp, $reg1
6478892d
TS
12338
12339 If $reg2 is given, this results in:
12340 daddu $reg2, $gp, $0
956cd1d6 12341 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
12342 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
12343 daddu $gp, $gp, $reg1
12344 $reg1 is normally $25 == $t9. */
6478892d 12345static void
17a2f251 12346s_cpsetup (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
12347{
12348 expressionS ex_off;
12349 expressionS ex_sym;
12350 int reg1;
12351 int icnt = 0;
f21f8242 12352 char *f;
6478892d 12353
8586fc66 12354 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
6478892d
TS
12355 We also need NewABI support. */
12356 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12357 {
12358 s_ignore (0);
12359 return;
12360 }
12361
12362 reg1 = tc_get_register (0);
12363 SKIP_WHITESPACE ();
12364 if (*input_line_pointer != ',')
12365 {
12366 as_bad (_("missing argument separator ',' for .cpsetup"));
12367 return;
12368 }
12369 else
80245285 12370 ++input_line_pointer;
6478892d
TS
12371 SKIP_WHITESPACE ();
12372 if (*input_line_pointer == '$')
80245285
TS
12373 {
12374 mips_cpreturn_register = tc_get_register (0);
12375 mips_cpreturn_offset = -1;
12376 }
6478892d 12377 else
80245285
TS
12378 {
12379 mips_cpreturn_offset = get_absolute_expression ();
12380 mips_cpreturn_register = -1;
12381 }
6478892d
TS
12382 SKIP_WHITESPACE ();
12383 if (*input_line_pointer != ',')
12384 {
12385 as_bad (_("missing argument separator ',' for .cpsetup"));
12386 return;
12387 }
12388 else
f9419b05 12389 ++input_line_pointer;
6478892d 12390 SKIP_WHITESPACE ();
f21f8242 12391 expression (&ex_sym);
6478892d
TS
12392
12393 if (mips_cpreturn_register == -1)
12394 {
12395 ex_off.X_op = O_constant;
12396 ex_off.X_add_symbol = NULL;
12397 ex_off.X_op_symbol = NULL;
12398 ex_off.X_add_number = mips_cpreturn_offset;
12399
17a2f251
TS
12400 macro_build (NULL, &icnt, &ex_off, "sd", "t,o(b)", mips_gp_register,
12401 BFD_RELOC_LO16, SP);
6478892d
TS
12402 }
12403 else
17a2f251
TS
12404 macro_build (NULL, &icnt, NULL, "daddu", "d,v,t", mips_cpreturn_register,
12405 mips_gp_register, 0);
6478892d 12406
f21f8242
AO
12407 /* Ensure there's room for the next two instructions, so that `f'
12408 doesn't end up with an address in the wrong frag. */
12409 frag_grow (8);
12410 f = frag_more (0);
17a2f251
TS
12411 macro_build (NULL, &icnt, &ex_sym, "lui", "t,u", mips_gp_register,
12412 BFD_RELOC_GPREL16);
f21f8242 12413 fix_new (frag_now, f - frag_now->fr_literal,
a105a300 12414 8, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
f21f8242 12415 fix_new (frag_now, f - frag_now->fr_literal,
a105a300 12416 4, NULL, 0, 0, BFD_RELOC_HI16_S);
f21f8242
AO
12417
12418 f = frag_more (0);
17a2f251
TS
12419 macro_build (NULL, &icnt, &ex_sym, "addiu", "t,r,j", mips_gp_register,
12420 mips_gp_register, BFD_RELOC_GPREL16);
f21f8242 12421 fix_new (frag_now, f - frag_now->fr_literal,
a105a300 12422 8, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
f21f8242 12423 fix_new (frag_now, f - frag_now->fr_literal,
a105a300 12424 4, NULL, 0, 0, BFD_RELOC_LO16);
f21f8242 12425
17a2f251
TS
12426 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
12427 mips_gp_register, reg1);
6478892d
TS
12428
12429 demand_empty_rest_of_line ();
12430}
12431
12432static void
17a2f251 12433s_cplocal (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
12434{
12435 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
12436 .cplocal is ignored. */
12437 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12438 {
12439 s_ignore (0);
12440 return;
12441 }
12442
12443 mips_gp_register = tc_get_register (0);
85b51719 12444 demand_empty_rest_of_line ();
6478892d
TS
12445}
12446
252b5132
RH
12447/* Handle the .cprestore pseudo-op. This stores $gp into a given
12448 offset from $sp. The offset is remembered, and after making a PIC
12449 call $gp is restored from that location. */
12450
12451static void
17a2f251 12452s_cprestore (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
12453{
12454 expressionS ex;
12455 int icnt = 0;
12456
6478892d 12457 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
c9914766 12458 .cprestore is ignored. */
6478892d 12459 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
12460 {
12461 s_ignore (0);
12462 return;
12463 }
12464
12465 mips_cprestore_offset = get_absolute_expression ();
7a621144 12466 mips_cprestore_valid = 1;
252b5132
RH
12467
12468 ex.X_op = O_constant;
12469 ex.X_add_symbol = NULL;
12470 ex.X_op_symbol = NULL;
12471 ex.X_add_number = mips_cprestore_offset;
12472
17a2f251 12473 macro_build_ldst_constoffset (NULL, &icnt, &ex, ADDRESS_STORE_INSN,
256ab948 12474 mips_gp_register, SP, HAVE_64BIT_ADDRESSES);
252b5132
RH
12475
12476 demand_empty_rest_of_line ();
12477}
12478
6478892d 12479/* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
67c1ffbe 12480 was given in the preceding .cpsetup, it results in:
6478892d 12481 ld $gp, offset($sp)
76b3015f 12482
6478892d 12483 If a register $reg2 was given there, it results in:
609f23f4 12484 daddu $gp, $reg2, $0
6478892d
TS
12485 */
12486static void
17a2f251 12487s_cpreturn (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
12488{
12489 expressionS ex;
12490 int icnt = 0;
12491
12492 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
12493 We also need NewABI support. */
12494 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12495 {
12496 s_ignore (0);
12497 return;
12498 }
12499
12500 if (mips_cpreturn_register == -1)
12501 {
12502 ex.X_op = O_constant;
12503 ex.X_add_symbol = NULL;
12504 ex.X_op_symbol = NULL;
12505 ex.X_add_number = mips_cpreturn_offset;
12506
17a2f251
TS
12507 macro_build (NULL, &icnt, &ex, "ld", "t,o(b)", mips_gp_register,
12508 BFD_RELOC_LO16, SP);
6478892d
TS
12509 }
12510 else
17a2f251
TS
12511 macro_build (NULL, &icnt, NULL, "daddu", "d,v,t", mips_gp_register,
12512 mips_cpreturn_register, 0);
6478892d
TS
12513
12514 demand_empty_rest_of_line ();
12515}
12516
12517/* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
12518 code. It sets the offset to use in gp_rel relocations. */
12519
12520static void
17a2f251 12521s_gpvalue (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
12522{
12523 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
12524 We also need NewABI support. */
12525 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12526 {
12527 s_ignore (0);
12528 return;
12529 }
12530
def2e0dd 12531 mips_gprel_offset = get_absolute_expression ();
6478892d
TS
12532
12533 demand_empty_rest_of_line ();
12534}
12535
252b5132
RH
12536/* Handle the .gpword pseudo-op. This is used when generating PIC
12537 code. It generates a 32 bit GP relative reloc. */
12538
12539static void
17a2f251 12540s_gpword (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
12541{
12542 symbolS *label;
12543 expressionS ex;
12544 char *p;
12545
12546 /* When not generating PIC code, this is treated as .word. */
12547 if (mips_pic != SVR4_PIC)
12548 {
12549 s_cons (2);
12550 return;
12551 }
12552
12553 label = insn_labels != NULL ? insn_labels->label : NULL;
b34976b6 12554 mips_emit_delays (TRUE);
252b5132
RH
12555 if (auto_align)
12556 mips_align (2, 0, label);
12557 mips_clear_insn_labels ();
12558
12559 expression (&ex);
12560
12561 if (ex.X_op != O_symbol || ex.X_add_number != 0)
12562 {
12563 as_bad (_("Unsupported use of .gpword"));
12564 ignore_rest_of_line ();
12565 }
12566
12567 p = frag_more (4);
17a2f251 12568 md_number_to_chars (p, 0, 4);
b34976b6 12569 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
cdf6fd85 12570 BFD_RELOC_GPREL32);
252b5132
RH
12571
12572 demand_empty_rest_of_line ();
12573}
12574
10181a0d 12575static void
17a2f251 12576s_gpdword (int ignore ATTRIBUTE_UNUSED)
10181a0d
AO
12577{
12578 symbolS *label;
12579 expressionS ex;
12580 char *p;
12581
12582 /* When not generating PIC code, this is treated as .dword. */
12583 if (mips_pic != SVR4_PIC)
12584 {
12585 s_cons (3);
12586 return;
12587 }
12588
12589 label = insn_labels != NULL ? insn_labels->label : NULL;
b34976b6 12590 mips_emit_delays (TRUE);
10181a0d
AO
12591 if (auto_align)
12592 mips_align (3, 0, label);
12593 mips_clear_insn_labels ();
12594
12595 expression (&ex);
12596
12597 if (ex.X_op != O_symbol || ex.X_add_number != 0)
12598 {
12599 as_bad (_("Unsupported use of .gpdword"));
12600 ignore_rest_of_line ();
12601 }
12602
12603 p = frag_more (8);
17a2f251 12604 md_number_to_chars (p, 0, 8);
a105a300 12605 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
10181a0d
AO
12606 BFD_RELOC_GPREL32);
12607
12608 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
12609 ex.X_op = O_absent;
12610 ex.X_add_symbol = 0;
12611 ex.X_add_number = 0;
b34976b6 12612 fix_new_exp (frag_now, p - frag_now->fr_literal, 8, &ex, FALSE,
10181a0d
AO
12613 BFD_RELOC_64);
12614
12615 demand_empty_rest_of_line ();
12616}
12617
252b5132
RH
12618/* Handle the .cpadd pseudo-op. This is used when dealing with switch
12619 tables in SVR4 PIC code. */
12620
12621static void
17a2f251 12622s_cpadd (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
12623{
12624 int icnt = 0;
12625 int reg;
12626
10181a0d
AO
12627 /* This is ignored when not generating SVR4 PIC code. */
12628 if (mips_pic != SVR4_PIC)
252b5132
RH
12629 {
12630 s_ignore (0);
12631 return;
12632 }
12633
12634 /* Add $gp to the register named as an argument. */
12635 reg = tc_get_register (0);
17a2f251
TS
12636 macro_build (NULL, &icnt, NULL, ADDRESS_ADD_INSN, "d,v,t",
12637 reg, reg, mips_gp_register);
252b5132 12638
bdaaa2e1 12639 demand_empty_rest_of_line ();
252b5132
RH
12640}
12641
12642/* Handle the .insn pseudo-op. This marks instruction labels in
12643 mips16 mode. This permits the linker to handle them specially,
12644 such as generating jalx instructions when needed. We also make
12645 them odd for the duration of the assembly, in order to generate the
12646 right sort of code. We will make them even in the adjust_symtab
12647 routine, while leaving them marked. This is convenient for the
12648 debugger and the disassembler. The linker knows to make them odd
12649 again. */
12650
12651static void
17a2f251 12652s_insn (int ignore ATTRIBUTE_UNUSED)
252b5132 12653{
f9419b05 12654 mips16_mark_labels ();
252b5132
RH
12655
12656 demand_empty_rest_of_line ();
12657}
12658
12659/* Handle a .stabn directive. We need these in order to mark a label
12660 as being a mips16 text label correctly. Sometimes the compiler
12661 will emit a label, followed by a .stabn, and then switch sections.
12662 If the label and .stabn are in mips16 mode, then the label is
12663 really a mips16 text label. */
12664
12665static void
17a2f251 12666s_mips_stab (int type)
252b5132 12667{
f9419b05 12668 if (type == 'n')
252b5132
RH
12669 mips16_mark_labels ();
12670
12671 s_stab (type);
12672}
12673
12674/* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
12675 */
12676
12677static void
17a2f251 12678s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
12679{
12680 char *name;
12681 int c;
12682 symbolS *symbolP;
12683 expressionS exp;
12684
12685 name = input_line_pointer;
12686 c = get_symbol_end ();
12687 symbolP = symbol_find_or_make (name);
12688 S_SET_WEAK (symbolP);
12689 *input_line_pointer = c;
12690
12691 SKIP_WHITESPACE ();
12692
12693 if (! is_end_of_line[(unsigned char) *input_line_pointer])
12694 {
12695 if (S_IS_DEFINED (symbolP))
12696 {
956cd1d6 12697 as_bad ("ignoring attempt to redefine symbol %s",
252b5132
RH
12698 S_GET_NAME (symbolP));
12699 ignore_rest_of_line ();
12700 return;
12701 }
bdaaa2e1 12702
252b5132
RH
12703 if (*input_line_pointer == ',')
12704 {
12705 ++input_line_pointer;
12706 SKIP_WHITESPACE ();
12707 }
bdaaa2e1 12708
252b5132
RH
12709 expression (&exp);
12710 if (exp.X_op != O_symbol)
12711 {
12712 as_bad ("bad .weakext directive");
98d3f06f 12713 ignore_rest_of_line ();
252b5132
RH
12714 return;
12715 }
49309057 12716 symbol_set_value_expression (symbolP, &exp);
252b5132
RH
12717 }
12718
12719 demand_empty_rest_of_line ();
12720}
12721
12722/* Parse a register string into a number. Called from the ECOFF code
12723 to parse .frame. The argument is non-zero if this is the frame
12724 register, so that we can record it in mips_frame_reg. */
12725
12726int
17a2f251 12727tc_get_register (int frame)
252b5132
RH
12728{
12729 int reg;
12730
12731 SKIP_WHITESPACE ();
12732 if (*input_line_pointer++ != '$')
12733 {
12734 as_warn (_("expected `$'"));
85b51719 12735 reg = ZERO;
252b5132 12736 }
3882b010 12737 else if (ISDIGIT (*input_line_pointer))
252b5132
RH
12738 {
12739 reg = get_absolute_expression ();
12740 if (reg < 0 || reg >= 32)
12741 {
12742 as_warn (_("Bad register number"));
85b51719 12743 reg = ZERO;
252b5132
RH
12744 }
12745 }
12746 else
12747 {
76db943d 12748 if (strncmp (input_line_pointer, "ra", 2) == 0)
85b51719
TS
12749 {
12750 reg = RA;
12751 input_line_pointer += 2;
12752 }
76db943d 12753 else if (strncmp (input_line_pointer, "fp", 2) == 0)
85b51719
TS
12754 {
12755 reg = FP;
12756 input_line_pointer += 2;
12757 }
252b5132 12758 else if (strncmp (input_line_pointer, "sp", 2) == 0)
85b51719
TS
12759 {
12760 reg = SP;
12761 input_line_pointer += 2;
12762 }
252b5132 12763 else if (strncmp (input_line_pointer, "gp", 2) == 0)
85b51719
TS
12764 {
12765 reg = GP;
12766 input_line_pointer += 2;
12767 }
252b5132 12768 else if (strncmp (input_line_pointer, "at", 2) == 0)
85b51719
TS
12769 {
12770 reg = AT;
12771 input_line_pointer += 2;
12772 }
12773 else if (strncmp (input_line_pointer, "kt0", 3) == 0)
12774 {
12775 reg = KT0;
12776 input_line_pointer += 3;
12777 }
12778 else if (strncmp (input_line_pointer, "kt1", 3) == 0)
12779 {
12780 reg = KT1;
12781 input_line_pointer += 3;
12782 }
12783 else if (strncmp (input_line_pointer, "zero", 4) == 0)
12784 {
12785 reg = ZERO;
12786 input_line_pointer += 4;
12787 }
252b5132
RH
12788 else
12789 {
12790 as_warn (_("Unrecognized register name"));
85b51719
TS
12791 reg = ZERO;
12792 while (ISALNUM(*input_line_pointer))
12793 input_line_pointer++;
252b5132 12794 }
252b5132
RH
12795 }
12796 if (frame)
7a621144
DJ
12797 {
12798 mips_frame_reg = reg != 0 ? reg : SP;
12799 mips_frame_reg_valid = 1;
12800 mips_cprestore_valid = 0;
12801 }
252b5132
RH
12802 return reg;
12803}
12804
12805valueT
17a2f251 12806md_section_align (asection *seg, valueT addr)
252b5132
RH
12807{
12808 int align = bfd_get_section_alignment (stdoutput, seg);
12809
12810#ifdef OBJ_ELF
12811 /* We don't need to align ELF sections to the full alignment.
12812 However, Irix 5 may prefer that we align them at least to a 16
12813 byte boundary. We don't bother to align the sections if we are
12814 targeted for an embedded system. */
12815 if (strcmp (TARGET_OS, "elf") == 0)
12816 return addr;
12817 if (align > 4)
12818 align = 4;
12819#endif
12820
12821 return ((addr + (1 << align) - 1) & (-1 << align));
12822}
12823
12824/* Utility routine, called from above as well. If called while the
12825 input file is still being read, it's only an approximation. (For
12826 example, a symbol may later become defined which appeared to be
12827 undefined earlier.) */
12828
12829static int
17a2f251 12830nopic_need_relax (symbolS *sym, int before_relaxing)
252b5132
RH
12831{
12832 if (sym == 0)
12833 return 0;
12834
6478892d 12835 if (USE_GLOBAL_POINTER_OPT && g_switch_value > 0)
252b5132
RH
12836 {
12837 const char *symname;
12838 int change;
12839
c9914766 12840 /* Find out whether this symbol can be referenced off the $gp
252b5132
RH
12841 register. It can be if it is smaller than the -G size or if
12842 it is in the .sdata or .sbss section. Certain symbols can
c9914766 12843 not be referenced off the $gp, although it appears as though
252b5132
RH
12844 they can. */
12845 symname = S_GET_NAME (sym);
12846 if (symname != (const char *) NULL
12847 && (strcmp (symname, "eprol") == 0
12848 || strcmp (symname, "etext") == 0
12849 || strcmp (symname, "_gp") == 0
12850 || strcmp (symname, "edata") == 0
12851 || strcmp (symname, "_fbss") == 0
12852 || strcmp (symname, "_fdata") == 0
12853 || strcmp (symname, "_ftext") == 0
12854 || strcmp (symname, "end") == 0
12855 || strcmp (symname, "_gp_disp") == 0))
12856 change = 1;
12857 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
12858 && (0
12859#ifndef NO_ECOFF_DEBUGGING
49309057
ILT
12860 || (symbol_get_obj (sym)->ecoff_extern_size != 0
12861 && (symbol_get_obj (sym)->ecoff_extern_size
12862 <= g_switch_value))
252b5132
RH
12863#endif
12864 /* We must defer this decision until after the whole
12865 file has been read, since there might be a .extern
12866 after the first use of this symbol. */
12867 || (before_relaxing
12868#ifndef NO_ECOFF_DEBUGGING
49309057 12869 && symbol_get_obj (sym)->ecoff_extern_size == 0
252b5132
RH
12870#endif
12871 && S_GET_VALUE (sym) == 0)
12872 || (S_GET_VALUE (sym) != 0
12873 && S_GET_VALUE (sym) <= g_switch_value)))
12874 change = 0;
12875 else
12876 {
12877 const char *segname;
12878
12879 segname = segment_name (S_GET_SEGMENT (sym));
12880 assert (strcmp (segname, ".lit8") != 0
12881 && strcmp (segname, ".lit4") != 0);
12882 change = (strcmp (segname, ".sdata") != 0
fba2b7f9
GK
12883 && strcmp (segname, ".sbss") != 0
12884 && strncmp (segname, ".sdata.", 7) != 0
12885 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
252b5132
RH
12886 }
12887 return change;
12888 }
12889 else
c9914766 12890 /* We are not optimizing for the $gp register. */
252b5132
RH
12891 return 1;
12892}
12893
5919d012
RS
12894
12895/* Return true if the given symbol should be considered local for SVR4 PIC. */
12896
12897static bfd_boolean
17a2f251 12898pic_need_relax (symbolS *sym, asection *segtype)
5919d012
RS
12899{
12900 asection *symsec;
12901 bfd_boolean linkonce;
12902
12903 /* Handle the case of a symbol equated to another symbol. */
12904 while (symbol_equated_reloc_p (sym))
12905 {
12906 symbolS *n;
12907
12908 /* It's possible to get a loop here in a badly written
12909 program. */
12910 n = symbol_get_value_expression (sym)->X_add_symbol;
12911 if (n == sym)
12912 break;
12913 sym = n;
12914 }
12915
12916 symsec = S_GET_SEGMENT (sym);
12917
12918 /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
12919 linkonce = FALSE;
12920 if (symsec != segtype && ! S_IS_LOCAL (sym))
12921 {
12922 if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
12923 != 0)
12924 linkonce = TRUE;
12925
12926 /* The GNU toolchain uses an extension for ELF: a section
12927 beginning with the magic string .gnu.linkonce is a linkonce
12928 section. */
12929 if (strncmp (segment_name (symsec), ".gnu.linkonce",
12930 sizeof ".gnu.linkonce" - 1) == 0)
12931 linkonce = TRUE;
12932 }
12933
12934 /* This must duplicate the test in adjust_reloc_syms. */
12935 return (symsec != &bfd_und_section
12936 && symsec != &bfd_abs_section
12937 && ! bfd_is_com_section (symsec)
12938 && !linkonce
12939#ifdef OBJ_ELF
12940 /* A global or weak symbol is treated as external. */
12941 && (OUTPUT_FLAVOR != bfd_target_elf_flavour
12942 || (! S_IS_WEAK (sym)
12943 && (! S_IS_EXTERNAL (sym)
12944 || mips_pic == EMBEDDED_PIC)))
12945#endif
12946 );
12947}
12948
12949
252b5132
RH
12950/* Given a mips16 variant frag FRAGP, return non-zero if it needs an
12951 extended opcode. SEC is the section the frag is in. */
12952
12953static int
17a2f251 12954mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
252b5132
RH
12955{
12956 int type;
12957 register const struct mips16_immed_operand *op;
12958 offsetT val;
12959 int mintiny, maxtiny;
12960 segT symsec;
98aa84af 12961 fragS *sym_frag;
252b5132
RH
12962
12963 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
12964 return 0;
12965 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
12966 return 1;
12967
12968 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12969 op = mips16_immed_operands;
12970 while (op->type != type)
12971 {
12972 ++op;
12973 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
12974 }
12975
12976 if (op->unsp)
12977 {
12978 if (type == '<' || type == '>' || type == '[' || type == ']')
12979 {
12980 mintiny = 1;
12981 maxtiny = 1 << op->nbits;
12982 }
12983 else
12984 {
12985 mintiny = 0;
12986 maxtiny = (1 << op->nbits) - 1;
12987 }
12988 }
12989 else
12990 {
12991 mintiny = - (1 << (op->nbits - 1));
12992 maxtiny = (1 << (op->nbits - 1)) - 1;
12993 }
12994
98aa84af 12995 sym_frag = symbol_get_frag (fragp->fr_symbol);
ac62c346 12996 val = S_GET_VALUE (fragp->fr_symbol);
98aa84af 12997 symsec = S_GET_SEGMENT (fragp->fr_symbol);
252b5132
RH
12998
12999 if (op->pcrel)
13000 {
13001 addressT addr;
13002
13003 /* We won't have the section when we are called from
13004 mips_relax_frag. However, we will always have been called
13005 from md_estimate_size_before_relax first. If this is a
13006 branch to a different section, we mark it as such. If SEC is
13007 NULL, and the frag is not marked, then it must be a branch to
13008 the same section. */
13009 if (sec == NULL)
13010 {
13011 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
13012 return 1;
13013 }
13014 else
13015 {
98aa84af 13016 /* Must have been called from md_estimate_size_before_relax. */
252b5132
RH
13017 if (symsec != sec)
13018 {
13019 fragp->fr_subtype =
13020 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13021
13022 /* FIXME: We should support this, and let the linker
13023 catch branches and loads that are out of range. */
13024 as_bad_where (fragp->fr_file, fragp->fr_line,
13025 _("unsupported PC relative reference to different section"));
13026
13027 return 1;
13028 }
98aa84af
AM
13029 if (fragp != sym_frag && sym_frag->fr_address == 0)
13030 /* Assume non-extended on the first relaxation pass.
13031 The address we have calculated will be bogus if this is
13032 a forward branch to another frag, as the forward frag
13033 will have fr_address == 0. */
13034 return 0;
252b5132
RH
13035 }
13036
13037 /* In this case, we know for sure that the symbol fragment is in
98aa84af
AM
13038 the same section. If the relax_marker of the symbol fragment
13039 differs from the relax_marker of this fragment, we have not
13040 yet adjusted the symbol fragment fr_address. We want to add
252b5132
RH
13041 in STRETCH in order to get a better estimate of the address.
13042 This particularly matters because of the shift bits. */
13043 if (stretch != 0
98aa84af 13044 && sym_frag->relax_marker != fragp->relax_marker)
252b5132
RH
13045 {
13046 fragS *f;
13047
13048 /* Adjust stretch for any alignment frag. Note that if have
13049 been expanding the earlier code, the symbol may be
13050 defined in what appears to be an earlier frag. FIXME:
13051 This doesn't handle the fr_subtype field, which specifies
13052 a maximum number of bytes to skip when doing an
13053 alignment. */
98aa84af 13054 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
252b5132
RH
13055 {
13056 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
13057 {
13058 if (stretch < 0)
13059 stretch = - ((- stretch)
13060 & ~ ((1 << (int) f->fr_offset) - 1));
13061 else
13062 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
13063 if (stretch == 0)
13064 break;
13065 }
13066 }
13067 if (f != NULL)
13068 val += stretch;
13069 }
13070
13071 addr = fragp->fr_address + fragp->fr_fix;
13072
13073 /* The base address rules are complicated. The base address of
13074 a branch is the following instruction. The base address of a
13075 PC relative load or add is the instruction itself, but if it
13076 is in a delay slot (in which case it can not be extended) use
13077 the address of the instruction whose delay slot it is in. */
13078 if (type == 'p' || type == 'q')
13079 {
13080 addr += 2;
13081
13082 /* If we are currently assuming that this frag should be
13083 extended, then, the current address is two bytes
bdaaa2e1 13084 higher. */
252b5132
RH
13085 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13086 addr += 2;
13087
13088 /* Ignore the low bit in the target, since it will be set
13089 for a text label. */
13090 if ((val & 1) != 0)
13091 --val;
13092 }
13093 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13094 addr -= 4;
13095 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13096 addr -= 2;
13097
13098 val -= addr & ~ ((1 << op->shift) - 1);
13099
13100 /* Branch offsets have an implicit 0 in the lowest bit. */
13101 if (type == 'p' || type == 'q')
13102 val /= 2;
13103
13104 /* If any of the shifted bits are set, we must use an extended
13105 opcode. If the address depends on the size of this
13106 instruction, this can lead to a loop, so we arrange to always
13107 use an extended opcode. We only check this when we are in
13108 the main relaxation loop, when SEC is NULL. */
13109 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
13110 {
13111 fragp->fr_subtype =
13112 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13113 return 1;
13114 }
13115
13116 /* If we are about to mark a frag as extended because the value
13117 is precisely maxtiny + 1, then there is a chance of an
13118 infinite loop as in the following code:
13119 la $4,foo
13120 .skip 1020
13121 .align 2
13122 foo:
13123 In this case when the la is extended, foo is 0x3fc bytes
13124 away, so the la can be shrunk, but then foo is 0x400 away, so
13125 the la must be extended. To avoid this loop, we mark the
13126 frag as extended if it was small, and is about to become
13127 extended with a value of maxtiny + 1. */
13128 if (val == ((maxtiny + 1) << op->shift)
13129 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
13130 && sec == NULL)
13131 {
13132 fragp->fr_subtype =
13133 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13134 return 1;
13135 }
13136 }
13137 else if (symsec != absolute_section && sec != NULL)
13138 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
13139
13140 if ((val & ((1 << op->shift) - 1)) != 0
13141 || val < (mintiny << op->shift)
13142 || val > (maxtiny << op->shift))
13143 return 1;
13144 else
13145 return 0;
13146}
13147
4a6a3df4
AO
13148/* Compute the length of a branch sequence, and adjust the
13149 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
13150 worst-case length is computed, with UPDATE being used to indicate
13151 whether an unconditional (-1), branch-likely (+1) or regular (0)
13152 branch is to be computed. */
13153static int
17a2f251 13154relaxed_branch_length (fragS *fragp, asection *sec, int update)
4a6a3df4 13155{
b34976b6 13156 bfd_boolean toofar;
4a6a3df4
AO
13157 int length;
13158
13159 if (fragp
13160 && S_IS_DEFINED (fragp->fr_symbol)
13161 && sec == S_GET_SEGMENT (fragp->fr_symbol))
13162 {
13163 addressT addr;
13164 offsetT val;
13165
13166 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
13167
13168 addr = fragp->fr_address + fragp->fr_fix + 4;
13169
13170 val -= addr;
13171
13172 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
13173 }
13174 else if (fragp)
13175 /* If the symbol is not defined or it's in a different segment,
13176 assume the user knows what's going on and emit a short
13177 branch. */
b34976b6 13178 toofar = FALSE;
4a6a3df4 13179 else
b34976b6 13180 toofar = TRUE;
4a6a3df4
AO
13181
13182 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13183 fragp->fr_subtype
af6ae2ad 13184 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_UNCOND (fragp->fr_subtype),
4a6a3df4
AO
13185 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
13186 RELAX_BRANCH_LINK (fragp->fr_subtype),
13187 toofar);
13188
13189 length = 4;
13190 if (toofar)
13191 {
13192 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
13193 length += 8;
13194
13195 if (mips_pic != NO_PIC)
13196 {
13197 /* Additional space for PIC loading of target address. */
13198 length += 8;
13199 if (mips_opts.isa == ISA_MIPS1)
13200 /* Additional space for $at-stabilizing nop. */
13201 length += 4;
13202 }
13203
13204 /* If branch is conditional. */
13205 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
13206 length += 8;
13207 }
b34976b6 13208
4a6a3df4
AO
13209 return length;
13210}
13211
252b5132
RH
13212/* Estimate the size of a frag before relaxing. Unless this is the
13213 mips16, we are not really relaxing here, and the final size is
13214 encoded in the subtype information. For the mips16, we have to
13215 decide whether we are using an extended opcode or not. */
13216
252b5132 13217int
17a2f251 13218md_estimate_size_before_relax (fragS *fragp, asection *segtype)
252b5132 13219{
5919d012 13220 int change;
252b5132 13221
4a6a3df4
AO
13222 if (RELAX_BRANCH_P (fragp->fr_subtype))
13223 {
13224
b34976b6
AM
13225 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
13226
4a6a3df4
AO
13227 return fragp->fr_var;
13228 }
13229
252b5132 13230 if (RELAX_MIPS16_P (fragp->fr_subtype))
177b4a6a
AO
13231 /* We don't want to modify the EXTENDED bit here; it might get us
13232 into infinite loops. We change it only in mips_relax_frag(). */
13233 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
252b5132
RH
13234
13235 if (mips_pic == NO_PIC)
5919d012 13236 change = nopic_need_relax (fragp->fr_symbol, 0);
252b5132 13237 else if (mips_pic == SVR4_PIC)
5919d012 13238 change = pic_need_relax (fragp->fr_symbol, segtype);
252b5132
RH
13239 else
13240 abort ();
13241
13242 if (change)
13243 {
13244 /* Record the offset to the first reloc in the fr_opcode field.
13245 This lets md_convert_frag and tc_gen_reloc know that the code
13246 must be expanded. */
13247 fragp->fr_opcode = (fragp->fr_literal
13248 + fragp->fr_fix
13249 - RELAX_OLD (fragp->fr_subtype)
13250 + RELAX_RELOC1 (fragp->fr_subtype));
13251 /* FIXME: This really needs as_warn_where. */
13252 if (RELAX_WARN (fragp->fr_subtype))
9a41af64
TS
13253 as_warn (_("AT used after \".set noat\" or macro used after "
13254 "\".set nomacro\""));
13255
13256 return RELAX_NEW (fragp->fr_subtype) - RELAX_OLD (fragp->fr_subtype);
252b5132
RH
13257 }
13258
9a41af64 13259 return 0;
252b5132
RH
13260}
13261
13262/* This is called to see whether a reloc against a defined symbol
13263 should be converted into a reloc against a section. Don't adjust
13264 MIPS16 jump relocations, so we don't have to worry about the format
13265 of the offset in the .o file. Don't adjust relocations against
13266 mips16 symbols, so that the linker can find them if it needs to set
13267 up a stub. */
13268
13269int
17a2f251 13270mips_fix_adjustable (fixS *fixp)
252b5132
RH
13271{
13272 if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
13273 return 0;
a161fe53 13274
252b5132
RH
13275 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
13276 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13277 return 0;
a161fe53 13278
252b5132
RH
13279 if (fixp->fx_addsy == NULL)
13280 return 1;
a161fe53 13281
252b5132
RH
13282#ifdef OBJ_ELF
13283 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
13284 && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
13285 && fixp->fx_subsy == NULL)
13286 return 0;
13287#endif
a161fe53 13288
252b5132
RH
13289 return 1;
13290}
13291
13292/* Translate internal representation of relocation info to BFD target
13293 format. */
13294
13295arelent **
17a2f251 13296tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
13297{
13298 static arelent *retval[4];
13299 arelent *reloc;
13300 bfd_reloc_code_real_type code;
13301
4b0cff4e
TS
13302 memset (retval, 0, sizeof(retval));
13303 reloc = retval[0] = (arelent *) xcalloc (1, sizeof (arelent));
49309057
ILT
13304 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13305 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
13306 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
13307
13308 if (mips_pic == EMBEDDED_PIC
13309 && SWITCH_TABLE (fixp))
13310 {
13311 /* For a switch table entry we use a special reloc. The addend
13312 is actually the difference between the reloc address and the
13313 subtrahend. */
13314 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
13315 if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
13316 as_fatal (_("Double check fx_r_type in tc-mips.c:tc_gen_reloc"));
13317 fixp->fx_r_type = BFD_RELOC_GPREL32;
13318 }
30ac9238 13319 else if (fixp->fx_pcrel)
252b5132 13320 {
30ac9238
RS
13321 bfd_vma pcrel_address;
13322
13323 /* Set PCREL_ADDRESS to this relocation's "PC". The PC for high
13324 high-part relocs is the address of the low-part reloc. */
13325 if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
13326 {
13327 assert (fixp->fx_next != NULL
13328 && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
13329 pcrel_address = (fixp->fx_next->fx_where
13330 + fixp->fx_next->fx_frag->fr_address);
13331 }
252b5132 13332 else
30ac9238
RS
13333 pcrel_address = reloc->address;
13334
13335 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
13336 {
13337 /* At this point, fx_addnumber is "symbol offset - pcrel_address".
13338 Relocations want only the symbol offset. */
13339 reloc->addend = fixp->fx_addnumber + pcrel_address;
13340 }
13341 else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16
13342 || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
4514d474 13343 {
30ac9238 13344 /* We use a special addend for an internal RELLO or RELHI reloc. */
4514d474 13345 if (symbol_section_p (fixp->fx_addsy))
30ac9238 13346 reloc->addend = pcrel_address - S_GET_VALUE (fixp->fx_subsy);
4514d474 13347 else
30ac9238 13348 reloc->addend = fixp->fx_addnumber + pcrel_address;
4514d474 13349 }
252b5132 13350 else
4514d474 13351 {
30ac9238
RS
13352 if (OUTPUT_FLAVOR != bfd_target_aout_flavour)
13353 /* A gruesome hack which is a result of the gruesome gas reloc
13354 handling. */
13355 reloc->addend = pcrel_address;
4514d474 13356 else
30ac9238 13357 reloc->addend = -pcrel_address;
4514d474 13358 }
252b5132 13359 }
252b5132 13360 else
30ac9238 13361 reloc->addend = fixp->fx_addnumber;
252b5132
RH
13362
13363 /* If this is a variant frag, we may need to adjust the existing
13364 reloc and generate a new one. */
13365 if (fixp->fx_frag->fr_opcode != NULL
ed6fb7bd
SC
13366 && ((fixp->fx_r_type == BFD_RELOC_GPREL16
13367 && ! HAVE_NEWABI)
f5040a92
AO
13368 || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_DISP
13369 && HAVE_NEWABI)
252b5132
RH
13370 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
13371 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL16
13372 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
13373 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16
13374 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
6478892d 13375 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_LO16)
ed6fb7bd 13376 )
252b5132
RH
13377 {
13378 arelent *reloc2;
13379
13380 assert (! RELAX_MIPS16_P (fixp->fx_frag->fr_subtype));
13381
13382 /* If this is not the last reloc in this frag, then we have two
13383 GPREL relocs, or a GOT_HI16/GOT_LO16 pair, or a
13384 CALL_HI16/CALL_LO16, both of which are being replaced. Let
13385 the second one handle all of them. */
13386 if (fixp->fx_next != NULL
13387 && fixp->fx_frag == fixp->fx_next->fx_frag)
13388 {
cdf6fd85
TS
13389 assert ((fixp->fx_r_type == BFD_RELOC_GPREL16
13390 && fixp->fx_next->fx_r_type == BFD_RELOC_GPREL16)
252b5132
RH
13391 || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
13392 && (fixp->fx_next->fx_r_type
13393 == BFD_RELOC_MIPS_GOT_LO16))
13394 || (fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
13395 && (fixp->fx_next->fx_r_type
13396 == BFD_RELOC_MIPS_CALL_LO16)));
13397 retval[0] = NULL;
13398 return retval;
13399 }
13400
13401 fixp->fx_where = fixp->fx_frag->fr_opcode - fixp->fx_frag->fr_literal;
13402 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
f5040a92 13403 reloc->addend += fixp->fx_frag->tc_frag_data.tc_fr_offset;
252b5132 13404 reloc2 = retval[1] = (arelent *) xmalloc (sizeof (arelent));
49309057
ILT
13405 reloc2->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13406 *reloc2->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
13407 reloc2->address = (reloc->address
13408 + (RELAX_RELOC2 (fixp->fx_frag->fr_subtype)
13409 - RELAX_RELOC1 (fixp->fx_frag->fr_subtype)));
30ac9238 13410 reloc2->addend = reloc->addend;
252b5132
RH
13411 reloc2->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO16);
13412 assert (reloc2->howto != NULL);
13413
13414 if (RELAX_RELOC3 (fixp->fx_frag->fr_subtype))
13415 {
13416 arelent *reloc3;
13417
13418 reloc3 = retval[2] = (arelent *) xmalloc (sizeof (arelent));
252b5132
RH
13419 *reloc3 = *reloc2;
13420 reloc3->address += 4;
13421 }
13422
13423 if (mips_pic == NO_PIC)
13424 {
cdf6fd85 13425 assert (fixp->fx_r_type == BFD_RELOC_GPREL16);
252b5132
RH
13426 fixp->fx_r_type = BFD_RELOC_HI16_S;
13427 }
13428 else if (mips_pic == SVR4_PIC)
13429 {
13430 switch (fixp->fx_r_type)
13431 {
13432 default:
13433 abort ();
13434 case BFD_RELOC_MIPS_GOT16:
13435 break;
252b5132
RH
13436 case BFD_RELOC_MIPS_GOT_LO16:
13437 case BFD_RELOC_MIPS_CALL_LO16:
ed6fb7bd
SC
13438 if (HAVE_NEWABI)
13439 {
ed6fb7bd
SC
13440 fixp->fx_r_type = BFD_RELOC_MIPS_GOT_PAGE;
13441 reloc2->howto = bfd_reloc_type_lookup
13442 (stdoutput, BFD_RELOC_MIPS_GOT_OFST);
13443 }
13444 else
13445 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
13446 break;
f5040a92
AO
13447 case BFD_RELOC_MIPS_CALL16:
13448 case BFD_RELOC_MIPS_GOT_OFST:
13449 case BFD_RELOC_MIPS_GOT_DISP:
13450 if (HAVE_NEWABI)
13451 {
13452 /* It may seem nonsensical to relax GOT_DISP to
13453 GOT_DISP, but we're actually turning a GOT_DISP
13454 without offset into a GOT_DISP with an offset,
13455 getting rid of the separate addition, which we can
13456 do when the symbol is found to be local. */
13457 fixp->fx_r_type = BFD_RELOC_MIPS_GOT_DISP;
13458 retval[1] = NULL;
13459 }
13460 else
13461 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
13462 break;
252b5132
RH
13463 }
13464 }
13465 else
13466 abort ();
13467 }
13468
438c16b8
TS
13469 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
13470 entry to be used in the relocation's section offset. */
13471 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
252b5132
RH
13472 {
13473 reloc->address = reloc->addend;
13474 reloc->addend = 0;
13475 }
13476
13477 /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
13478 fixup_segment converted a non-PC relative reloc into a PC
13479 relative reloc. In such a case, we need to convert the reloc
13480 code. */
13481 code = fixp->fx_r_type;
13482 if (fixp->fx_pcrel)
13483 {
13484 switch (code)
13485 {
13486 case BFD_RELOC_8:
13487 code = BFD_RELOC_8_PCREL;
13488 break;
13489 case BFD_RELOC_16:
13490 code = BFD_RELOC_16_PCREL;
13491 break;
13492 case BFD_RELOC_32:
13493 code = BFD_RELOC_32_PCREL;
13494 break;
13495 case BFD_RELOC_64:
13496 code = BFD_RELOC_64_PCREL;
13497 break;
13498 case BFD_RELOC_8_PCREL:
13499 case BFD_RELOC_16_PCREL:
13500 case BFD_RELOC_32_PCREL:
13501 case BFD_RELOC_64_PCREL:
13502 case BFD_RELOC_16_PCREL_S2:
13503 case BFD_RELOC_PCREL_HI16_S:
13504 case BFD_RELOC_PCREL_LO16:
13505 break;
13506 default:
13507 as_bad_where (fixp->fx_file, fixp->fx_line,
13508 _("Cannot make %s relocation PC relative"),
13509 bfd_get_reloc_code_name (code));
13510 }
13511 }
13512
0b25d3e6
AO
13513 /* To support a PC relative reloc when generating embedded PIC code
13514 for ECOFF, we use a Cygnus extension. We check for that here to
13515 make sure that we don't let such a reloc escape normally. */
13516 if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
13517 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
13518 && code == BFD_RELOC_16_PCREL_S2
13519 && mips_pic != EMBEDDED_PIC)
13520 reloc->howto = NULL;
13521 else
13522 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
13523
252b5132
RH
13524 if (reloc->howto == NULL)
13525 {
13526 as_bad_where (fixp->fx_file, fixp->fx_line,
13527 _("Can not represent %s relocation in this object file format"),
13528 bfd_get_reloc_code_name (code));
13529 retval[0] = NULL;
13530 }
13531
13532 return retval;
13533}
13534
13535/* Relax a machine dependent frag. This returns the amount by which
13536 the current size of the frag should change. */
13537
13538int
17a2f251 13539mips_relax_frag (asection *sec, fragS *fragp, long stretch)
252b5132 13540{
4a6a3df4
AO
13541 if (RELAX_BRANCH_P (fragp->fr_subtype))
13542 {
13543 offsetT old_var = fragp->fr_var;
b34976b6
AM
13544
13545 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
4a6a3df4
AO
13546
13547 return fragp->fr_var - old_var;
13548 }
13549
252b5132
RH
13550 if (! RELAX_MIPS16_P (fragp->fr_subtype))
13551 return 0;
13552
c4e7957c 13553 if (mips16_extended_frag (fragp, NULL, stretch))
252b5132
RH
13554 {
13555 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13556 return 0;
13557 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
13558 return 2;
13559 }
13560 else
13561 {
13562 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13563 return 0;
13564 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
13565 return -2;
13566 }
13567
13568 return 0;
13569}
13570
13571/* Convert a machine dependent frag. */
13572
13573void
17a2f251 13574md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
252b5132
RH
13575{
13576 int old, new;
13577 char *fixptr;
13578
4a6a3df4
AO
13579 if (RELAX_BRANCH_P (fragp->fr_subtype))
13580 {
13581 bfd_byte *buf;
13582 unsigned long insn;
13583 expressionS exp;
13584 fixS *fixp;
b34976b6 13585
4a6a3df4
AO
13586 buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
13587
13588 if (target_big_endian)
13589 insn = bfd_getb32 (buf);
13590 else
13591 insn = bfd_getl32 (buf);
b34976b6 13592
4a6a3df4
AO
13593 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13594 {
13595 /* We generate a fixup instead of applying it right now
13596 because, if there are linker relaxations, we're going to
13597 need the relocations. */
13598 exp.X_op = O_symbol;
13599 exp.X_add_symbol = fragp->fr_symbol;
13600 exp.X_add_number = fragp->fr_offset;
13601
13602 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
0b25d3e6
AO
13603 4, &exp, 1,
13604 BFD_RELOC_16_PCREL_S2);
4a6a3df4
AO
13605 fixp->fx_file = fragp->fr_file;
13606 fixp->fx_line = fragp->fr_line;
b34976b6 13607
17a2f251 13608 md_number_to_chars (buf, insn, 4);
4a6a3df4
AO
13609 buf += 4;
13610 }
13611 else
13612 {
13613 int i;
13614
13615 as_warn_where (fragp->fr_file, fragp->fr_line,
13616 _("relaxed out-of-range branch into a jump"));
13617
13618 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
13619 goto uncond;
13620
13621 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13622 {
13623 /* Reverse the branch. */
13624 switch ((insn >> 28) & 0xf)
13625 {
13626 case 4:
13627 /* bc[0-3][tf]l? and bc1any[24][ft] instructions can
13628 have the condition reversed by tweaking a single
13629 bit, and their opcodes all have 0x4???????. */
13630 assert ((insn & 0xf1000000) == 0x41000000);
13631 insn ^= 0x00010000;
13632 break;
13633
13634 case 0:
13635 /* bltz 0x04000000 bgez 0x04010000
13636 bltzal 0x04100000 bgezal 0x04110000 */
13637 assert ((insn & 0xfc0e0000) == 0x04000000);
13638 insn ^= 0x00010000;
13639 break;
b34976b6 13640
4a6a3df4
AO
13641 case 1:
13642 /* beq 0x10000000 bne 0x14000000
13643 blez 0x18000000 bgtz 0x1c000000 */
13644 insn ^= 0x04000000;
13645 break;
13646
13647 default:
13648 abort ();
13649 }
13650 }
13651
13652 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13653 {
13654 /* Clear the and-link bit. */
13655 assert ((insn & 0xfc1c0000) == 0x04100000);
13656
13657 /* bltzal 0x04100000 bgezal 0x04110000
13658 bltzall 0x04120000 bgezall 0x04130000 */
13659 insn &= ~0x00100000;
13660 }
13661
13662 /* Branch over the branch (if the branch was likely) or the
13663 full jump (not likely case). Compute the offset from the
13664 current instruction to branch to. */
13665 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13666 i = 16;
13667 else
13668 {
13669 /* How many bytes in instructions we've already emitted? */
13670 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13671 /* How many bytes in instructions from here to the end? */
13672 i = fragp->fr_var - i;
13673 }
13674 /* Convert to instruction count. */
13675 i >>= 2;
13676 /* Branch counts from the next instruction. */
b34976b6 13677 i--;
4a6a3df4
AO
13678 insn |= i;
13679 /* Branch over the jump. */
17a2f251 13680 md_number_to_chars (buf, insn, 4);
4a6a3df4
AO
13681 buf += 4;
13682
13683 /* Nop */
17a2f251 13684 md_number_to_chars (buf, 0, 4);
4a6a3df4
AO
13685 buf += 4;
13686
13687 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13688 {
13689 /* beql $0, $0, 2f */
13690 insn = 0x50000000;
13691 /* Compute the PC offset from the current instruction to
13692 the end of the variable frag. */
13693 /* How many bytes in instructions we've already emitted? */
13694 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13695 /* How many bytes in instructions from here to the end? */
13696 i = fragp->fr_var - i;
13697 /* Convert to instruction count. */
13698 i >>= 2;
13699 /* Don't decrement i, because we want to branch over the
13700 delay slot. */
13701
13702 insn |= i;
17a2f251 13703 md_number_to_chars (buf, insn, 4);
4a6a3df4
AO
13704 buf += 4;
13705
17a2f251 13706 md_number_to_chars (buf, 0, 4);
4a6a3df4
AO
13707 buf += 4;
13708 }
13709
13710 uncond:
13711 if (mips_pic == NO_PIC)
13712 {
13713 /* j or jal. */
13714 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
13715 ? 0x0c000000 : 0x08000000);
13716 exp.X_op = O_symbol;
13717 exp.X_add_symbol = fragp->fr_symbol;
13718 exp.X_add_number = fragp->fr_offset;
13719
13720 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13721 4, &exp, 0, BFD_RELOC_MIPS_JMP);
13722 fixp->fx_file = fragp->fr_file;
13723 fixp->fx_line = fragp->fr_line;
13724
17a2f251 13725 md_number_to_chars (buf, insn, 4);
4a6a3df4
AO
13726 buf += 4;
13727 }
13728 else
13729 {
13730 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
13731 insn = HAVE_64BIT_ADDRESSES ? 0xdf810000 : 0x8f810000;
13732 exp.X_op = O_symbol;
13733 exp.X_add_symbol = fragp->fr_symbol;
13734 exp.X_add_number = fragp->fr_offset;
13735
13736 if (fragp->fr_offset)
13737 {
13738 exp.X_add_symbol = make_expr_symbol (&exp);
13739 exp.X_add_number = 0;
13740 }
13741
13742 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13743 4, &exp, 0, BFD_RELOC_MIPS_GOT16);
13744 fixp->fx_file = fragp->fr_file;
13745 fixp->fx_line = fragp->fr_line;
13746
17a2f251 13747 md_number_to_chars (buf, insn, 4);
4a6a3df4 13748 buf += 4;
b34976b6 13749
4a6a3df4
AO
13750 if (mips_opts.isa == ISA_MIPS1)
13751 {
13752 /* nop */
17a2f251 13753 md_number_to_chars (buf, 0, 4);
4a6a3df4
AO
13754 buf += 4;
13755 }
13756
13757 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
13758 insn = HAVE_64BIT_ADDRESSES ? 0x64210000 : 0x24210000;
13759
13760 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13761 4, &exp, 0, BFD_RELOC_LO16);
13762 fixp->fx_file = fragp->fr_file;
13763 fixp->fx_line = fragp->fr_line;
b34976b6 13764
17a2f251 13765 md_number_to_chars (buf, insn, 4);
4a6a3df4
AO
13766 buf += 4;
13767
13768 /* j(al)r $at. */
13769 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13770 insn = 0x0020f809;
13771 else
13772 insn = 0x00200008;
13773
17a2f251 13774 md_number_to_chars (buf, insn, 4);
4a6a3df4
AO
13775 buf += 4;
13776 }
13777 }
13778
13779 assert (buf == (bfd_byte *)fragp->fr_literal
13780 + fragp->fr_fix + fragp->fr_var);
13781
13782 fragp->fr_fix += fragp->fr_var;
13783
13784 return;
13785 }
13786
252b5132
RH
13787 if (RELAX_MIPS16_P (fragp->fr_subtype))
13788 {
13789 int type;
13790 register const struct mips16_immed_operand *op;
b34976b6 13791 bfd_boolean small, ext;
252b5132
RH
13792 offsetT val;
13793 bfd_byte *buf;
13794 unsigned long insn;
b34976b6 13795 bfd_boolean use_extend;
252b5132
RH
13796 unsigned short extend;
13797
13798 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
13799 op = mips16_immed_operands;
13800 while (op->type != type)
13801 ++op;
13802
13803 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13804 {
b34976b6
AM
13805 small = FALSE;
13806 ext = TRUE;
252b5132
RH
13807 }
13808 else
13809 {
b34976b6
AM
13810 small = TRUE;
13811 ext = FALSE;
252b5132
RH
13812 }
13813
6386f3a7 13814 resolve_symbol_value (fragp->fr_symbol);
252b5132
RH
13815 val = S_GET_VALUE (fragp->fr_symbol);
13816 if (op->pcrel)
13817 {
13818 addressT addr;
13819
13820 addr = fragp->fr_address + fragp->fr_fix;
13821
13822 /* The rules for the base address of a PC relative reloc are
13823 complicated; see mips16_extended_frag. */
13824 if (type == 'p' || type == 'q')
13825 {
13826 addr += 2;
13827 if (ext)
13828 addr += 2;
13829 /* Ignore the low bit in the target, since it will be
13830 set for a text label. */
13831 if ((val & 1) != 0)
13832 --val;
13833 }
13834 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13835 addr -= 4;
13836 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13837 addr -= 2;
13838
13839 addr &= ~ (addressT) ((1 << op->shift) - 1);
13840 val -= addr;
13841
13842 /* Make sure the section winds up with the alignment we have
13843 assumed. */
13844 if (op->shift > 0)
13845 record_alignment (asec, op->shift);
13846 }
13847
13848 if (ext
13849 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
13850 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
13851 as_warn_where (fragp->fr_file, fragp->fr_line,
13852 _("extended instruction in delay slot"));
13853
13854 buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
13855
13856 if (target_big_endian)
13857 insn = bfd_getb16 (buf);
13858 else
13859 insn = bfd_getl16 (buf);
13860
13861 mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
13862 RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
13863 small, ext, &insn, &use_extend, &extend);
13864
13865 if (use_extend)
13866 {
17a2f251 13867 md_number_to_chars (buf, 0xf000 | extend, 2);
252b5132
RH
13868 fragp->fr_fix += 2;
13869 buf += 2;
13870 }
13871
17a2f251 13872 md_number_to_chars (buf, insn, 2);
252b5132
RH
13873 fragp->fr_fix += 2;
13874 buf += 2;
13875 }
13876 else
13877 {
13878 if (fragp->fr_opcode == NULL)
13879 return;
13880
13881 old = RELAX_OLD (fragp->fr_subtype);
13882 new = RELAX_NEW (fragp->fr_subtype);
13883 fixptr = fragp->fr_literal + fragp->fr_fix;
13884
13885 if (new > 0)
f5040a92 13886 memmove (fixptr - old, fixptr, new);
252b5132
RH
13887
13888 fragp->fr_fix += new - old;
13889 }
13890}
13891
13892#ifdef OBJ_ELF
13893
13894/* This function is called after the relocs have been generated.
13895 We've been storing mips16 text labels as odd. Here we convert them
13896 back to even for the convenience of the debugger. */
13897
13898void
17a2f251 13899mips_frob_file_after_relocs (void)
252b5132
RH
13900{
13901 asymbol **syms;
13902 unsigned int count, i;
13903
13904 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
13905 return;
13906
13907 syms = bfd_get_outsymbols (stdoutput);
13908 count = bfd_get_symcount (stdoutput);
13909 for (i = 0; i < count; i++, syms++)
13910 {
13911 if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
13912 && ((*syms)->value & 1) != 0)
13913 {
13914 (*syms)->value &= ~1;
13915 /* If the symbol has an odd size, it was probably computed
13916 incorrectly, so adjust that as well. */
13917 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
13918 ++elf_symbol (*syms)->internal_elf_sym.st_size;
13919 }
13920 }
13921}
13922
13923#endif
13924
13925/* This function is called whenever a label is defined. It is used
13926 when handling branch delays; if a branch has a label, we assume we
13927 can not move it. */
13928
13929void
17a2f251 13930mips_define_label (symbolS *sym)
252b5132
RH
13931{
13932 struct insn_label_list *l;
13933
13934 if (free_insn_labels == NULL)
13935 l = (struct insn_label_list *) xmalloc (sizeof *l);
13936 else
13937 {
13938 l = free_insn_labels;
13939 free_insn_labels = l->next;
13940 }
13941
13942 l->label = sym;
13943 l->next = insn_labels;
13944 insn_labels = l;
13945}
13946\f
13947#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13948
13949/* Some special processing for a MIPS ELF file. */
13950
13951void
17a2f251 13952mips_elf_final_processing (void)
252b5132
RH
13953{
13954 /* Write out the register information. */
316f5878 13955 if (mips_abi != N64_ABI)
252b5132
RH
13956 {
13957 Elf32_RegInfo s;
13958
13959 s.ri_gprmask = mips_gprmask;
13960 s.ri_cprmask[0] = mips_cprmask[0];
13961 s.ri_cprmask[1] = mips_cprmask[1];
13962 s.ri_cprmask[2] = mips_cprmask[2];
13963 s.ri_cprmask[3] = mips_cprmask[3];
13964 /* The gp_value field is set by the MIPS ELF backend. */
13965
13966 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
13967 ((Elf32_External_RegInfo *)
13968 mips_regmask_frag));
13969 }
13970 else
13971 {
13972 Elf64_Internal_RegInfo s;
13973
13974 s.ri_gprmask = mips_gprmask;
13975 s.ri_pad = 0;
13976 s.ri_cprmask[0] = mips_cprmask[0];
13977 s.ri_cprmask[1] = mips_cprmask[1];
13978 s.ri_cprmask[2] = mips_cprmask[2];
13979 s.ri_cprmask[3] = mips_cprmask[3];
13980 /* The gp_value field is set by the MIPS ELF backend. */
13981
13982 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
13983 ((Elf64_External_RegInfo *)
13984 mips_regmask_frag));
13985 }
13986
13987 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
13988 sort of BFD interface for this. */
13989 if (mips_any_noreorder)
13990 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
13991 if (mips_pic != NO_PIC)
143d77c5 13992 {
252b5132 13993 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
143d77c5
EC
13994 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
13995 }
13996 if (mips_abicalls)
13997 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
252b5132 13998
98d3f06f 13999 /* Set MIPS ELF flags for ASEs. */
a4672219
TS
14000 if (file_ase_mips16)
14001 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
1f25f5d3
CD
14002#if 0 /* XXX FIXME */
14003 if (file_ase_mips3d)
14004 elf_elfheader (stdoutput)->e_flags |= ???;
14005#endif
deec1734
CD
14006 if (file_ase_mdmx)
14007 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
1f25f5d3 14008
bdaaa2e1 14009 /* Set the MIPS ELF ABI flags. */
316f5878 14010 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
252b5132 14011 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
316f5878 14012 else if (mips_abi == O64_ABI)
252b5132 14013 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
316f5878 14014 else if (mips_abi == EABI_ABI)
252b5132 14015 {
316f5878 14016 if (!file_mips_gp32)
252b5132
RH
14017 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
14018 else
14019 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
14020 }
316f5878 14021 else if (mips_abi == N32_ABI)
be00bddd
TS
14022 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
14023
c9914766 14024 /* Nothing to do for N64_ABI. */
252b5132
RH
14025
14026 if (mips_32bitmode)
14027 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
14028}
14029
14030#endif /* OBJ_ELF || OBJ_MAYBE_ELF */
14031\f
beae10d5
KH
14032typedef struct proc {
14033 symbolS *isym;
14034 unsigned long reg_mask;
14035 unsigned long reg_offset;
14036 unsigned long fpreg_mask;
14037 unsigned long fpreg_offset;
14038 unsigned long frame_offset;
14039 unsigned long frame_reg;
14040 unsigned long pc_reg;
14041} procS;
252b5132
RH
14042
14043static procS cur_proc;
14044static procS *cur_proc_ptr;
14045static int numprocs;
14046
0a9ef439 14047/* Fill in an rs_align_code fragment. */
a19d8eb0 14048
0a9ef439 14049void
17a2f251 14050mips_handle_align (fragS *fragp)
a19d8eb0 14051{
0a9ef439
RH
14052 if (fragp->fr_type != rs_align_code)
14053 return;
14054
14055 if (mips_opts.mips16)
a19d8eb0
CP
14056 {
14057 static const unsigned char be_nop[] = { 0x65, 0x00 };
14058 static const unsigned char le_nop[] = { 0x00, 0x65 };
14059
0a9ef439
RH
14060 int bytes;
14061 char *p;
a19d8eb0 14062
0a9ef439
RH
14063 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
14064 p = fragp->fr_literal + fragp->fr_fix;
14065
14066 if (bytes & 1)
14067 {
14068 *p++ = 0;
f9419b05 14069 fragp->fr_fix++;
0a9ef439
RH
14070 }
14071
14072 memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
14073 fragp->fr_var = 2;
a19d8eb0
CP
14074 }
14075
0a9ef439 14076 /* For mips32, a nop is a zero, which we trivially get by doing nothing. */
a19d8eb0
CP
14077}
14078
252b5132 14079static void
17a2f251 14080md_obj_begin (void)
252b5132
RH
14081{
14082}
14083
14084static void
17a2f251 14085md_obj_end (void)
252b5132
RH
14086{
14087 /* check for premature end, nesting errors, etc */
14088 if (cur_proc_ptr)
9a41af64 14089 as_warn (_("missing .end at end of assembly"));
252b5132
RH
14090}
14091
14092static long
17a2f251 14093get_number (void)
252b5132
RH
14094{
14095 int negative = 0;
14096 long val = 0;
14097
14098 if (*input_line_pointer == '-')
14099 {
14100 ++input_line_pointer;
14101 negative = 1;
14102 }
3882b010 14103 if (!ISDIGIT (*input_line_pointer))
956cd1d6 14104 as_bad (_("expected simple number"));
252b5132
RH
14105 if (input_line_pointer[0] == '0')
14106 {
14107 if (input_line_pointer[1] == 'x')
14108 {
14109 input_line_pointer += 2;
3882b010 14110 while (ISXDIGIT (*input_line_pointer))
252b5132
RH
14111 {
14112 val <<= 4;
14113 val |= hex_value (*input_line_pointer++);
14114 }
14115 return negative ? -val : val;
14116 }
14117 else
14118 {
14119 ++input_line_pointer;
3882b010 14120 while (ISDIGIT (*input_line_pointer))
252b5132
RH
14121 {
14122 val <<= 3;
14123 val |= *input_line_pointer++ - '0';
14124 }
14125 return negative ? -val : val;
14126 }
14127 }
3882b010 14128 if (!ISDIGIT (*input_line_pointer))
252b5132
RH
14129 {
14130 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
14131 *input_line_pointer, *input_line_pointer);
956cd1d6 14132 as_warn (_("invalid number"));
252b5132
RH
14133 return -1;
14134 }
3882b010 14135 while (ISDIGIT (*input_line_pointer))
252b5132
RH
14136 {
14137 val *= 10;
14138 val += *input_line_pointer++ - '0';
14139 }
14140 return negative ? -val : val;
14141}
14142
14143/* The .file directive; just like the usual .file directive, but there
c5dd6aab
DJ
14144 is an initial number which is the ECOFF file index. In the non-ECOFF
14145 case .file implies DWARF-2. */
14146
14147static void
17a2f251 14148s_mips_file (int x ATTRIBUTE_UNUSED)
c5dd6aab 14149{
ecb4347a
DJ
14150 static int first_file_directive = 0;
14151
c5dd6aab
DJ
14152 if (ECOFF_DEBUGGING)
14153 {
14154 get_number ();
14155 s_app_file (0);
14156 }
14157 else
ecb4347a
DJ
14158 {
14159 char *filename;
14160
14161 filename = dwarf2_directive_file (0);
14162
14163 /* Versions of GCC up to 3.1 start files with a ".file"
14164 directive even for stabs output. Make sure that this
14165 ".file" is handled. Note that you need a version of GCC
14166 after 3.1 in order to support DWARF-2 on MIPS. */
14167 if (filename != NULL && ! first_file_directive)
14168 {
14169 (void) new_logical_line (filename, -1);
14170 s_app_file_string (filename);
14171 }
14172 first_file_directive = 1;
14173 }
c5dd6aab
DJ
14174}
14175
14176/* The .loc directive, implying DWARF-2. */
252b5132
RH
14177
14178static void
17a2f251 14179s_mips_loc (int x ATTRIBUTE_UNUSED)
252b5132 14180{
c5dd6aab
DJ
14181 if (!ECOFF_DEBUGGING)
14182 dwarf2_directive_loc (0);
252b5132
RH
14183}
14184
252b5132
RH
14185/* The .end directive. */
14186
14187static void
17a2f251 14188s_mips_end (int x ATTRIBUTE_UNUSED)
252b5132
RH
14189{
14190 symbolS *p;
252b5132 14191
7a621144
DJ
14192 /* Following functions need their own .frame and .cprestore directives. */
14193 mips_frame_reg_valid = 0;
14194 mips_cprestore_valid = 0;
14195
252b5132
RH
14196 if (!is_end_of_line[(unsigned char) *input_line_pointer])
14197 {
14198 p = get_symbol ();
14199 demand_empty_rest_of_line ();
14200 }
14201 else
14202 p = NULL;
14203
14949570 14204 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
14205 as_warn (_(".end not in text section"));
14206
14207 if (!cur_proc_ptr)
14208 {
14209 as_warn (_(".end directive without a preceding .ent directive."));
14210 demand_empty_rest_of_line ();
14211 return;
14212 }
14213
14214 if (p != NULL)
14215 {
14216 assert (S_GET_NAME (p));
14217 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->isym)))
14218 as_warn (_(".end symbol does not match .ent symbol."));
ecb4347a
DJ
14219
14220 if (debug_type == DEBUG_STABS)
14221 stabs_generate_asm_endfunc (S_GET_NAME (p),
14222 S_GET_NAME (p));
252b5132
RH
14223 }
14224 else
14225 as_warn (_(".end directive missing or unknown symbol"));
14226
ecb4347a
DJ
14227#ifdef OBJ_ELF
14228 /* Generate a .pdr section. */
dcd410fe
RO
14229 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING
14230 && mips_flag_pdr)
ecb4347a
DJ
14231 {
14232 segT saved_seg = now_seg;
14233 subsegT saved_subseg = now_subseg;
14234 valueT dot;
14235 expressionS exp;
14236 char *fragp;
252b5132 14237
ecb4347a 14238 dot = frag_now_fix ();
252b5132
RH
14239
14240#ifdef md_flush_pending_output
ecb4347a 14241 md_flush_pending_output ();
252b5132
RH
14242#endif
14243
ecb4347a
DJ
14244 assert (pdr_seg);
14245 subseg_set (pdr_seg, 0);
252b5132 14246
ecb4347a
DJ
14247 /* Write the symbol. */
14248 exp.X_op = O_symbol;
14249 exp.X_add_symbol = p;
14250 exp.X_add_number = 0;
14251 emit_expr (&exp, 4);
252b5132 14252
ecb4347a 14253 fragp = frag_more (7 * 4);
252b5132 14254
17a2f251
TS
14255 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
14256 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
14257 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
14258 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
14259 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
14260 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
14261 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
252b5132 14262
ecb4347a
DJ
14263 subseg_set (saved_seg, saved_subseg);
14264 }
14265#endif /* OBJ_ELF */
252b5132
RH
14266
14267 cur_proc_ptr = NULL;
14268}
14269
14270/* The .aent and .ent directives. */
14271
14272static void
17a2f251 14273s_mips_ent (int aent)
252b5132 14274{
252b5132 14275 symbolS *symbolP;
252b5132
RH
14276
14277 symbolP = get_symbol ();
14278 if (*input_line_pointer == ',')
f9419b05 14279 ++input_line_pointer;
252b5132 14280 SKIP_WHITESPACE ();
3882b010 14281 if (ISDIGIT (*input_line_pointer)
d9a62219 14282 || *input_line_pointer == '-')
874e8986 14283 get_number ();
252b5132 14284
14949570 14285 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
14286 as_warn (_(".ent or .aent not in text section."));
14287
14288 if (!aent && cur_proc_ptr)
9a41af64 14289 as_warn (_("missing .end"));
252b5132
RH
14290
14291 if (!aent)
14292 {
7a621144
DJ
14293 /* This function needs its own .frame and .cprestore directives. */
14294 mips_frame_reg_valid = 0;
14295 mips_cprestore_valid = 0;
14296
252b5132
RH
14297 cur_proc_ptr = &cur_proc;
14298 memset (cur_proc_ptr, '\0', sizeof (procS));
14299
14300 cur_proc_ptr->isym = symbolP;
14301
49309057 14302 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
252b5132 14303
f9419b05 14304 ++numprocs;
ecb4347a
DJ
14305
14306 if (debug_type == DEBUG_STABS)
14307 stabs_generate_asm_func (S_GET_NAME (symbolP),
14308 S_GET_NAME (symbolP));
252b5132
RH
14309 }
14310
14311 demand_empty_rest_of_line ();
14312}
14313
14314/* The .frame directive. If the mdebug section is present (IRIX 5 native)
bdaaa2e1 14315 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
252b5132 14316 s_mips_frame is used so that we can set the PDR information correctly.
bdaaa2e1 14317 We can't use the ecoff routines because they make reference to the ecoff
252b5132
RH
14318 symbol table (in the mdebug section). */
14319
14320static void
17a2f251 14321s_mips_frame (int ignore ATTRIBUTE_UNUSED)
252b5132 14322{
ecb4347a
DJ
14323#ifdef OBJ_ELF
14324 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14325 {
14326 long val;
252b5132 14327
ecb4347a
DJ
14328 if (cur_proc_ptr == (procS *) NULL)
14329 {
14330 as_warn (_(".frame outside of .ent"));
14331 demand_empty_rest_of_line ();
14332 return;
14333 }
252b5132 14334
ecb4347a
DJ
14335 cur_proc_ptr->frame_reg = tc_get_register (1);
14336
14337 SKIP_WHITESPACE ();
14338 if (*input_line_pointer++ != ','
14339 || get_absolute_expression_and_terminator (&val) != ',')
14340 {
14341 as_warn (_("Bad .frame directive"));
14342 --input_line_pointer;
14343 demand_empty_rest_of_line ();
14344 return;
14345 }
252b5132 14346
ecb4347a
DJ
14347 cur_proc_ptr->frame_offset = val;
14348 cur_proc_ptr->pc_reg = tc_get_register (0);
252b5132 14349
252b5132 14350 demand_empty_rest_of_line ();
252b5132 14351 }
ecb4347a
DJ
14352 else
14353#endif /* OBJ_ELF */
14354 s_ignore (ignore);
252b5132
RH
14355}
14356
bdaaa2e1
KH
14357/* The .fmask and .mask directives. If the mdebug section is present
14358 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
252b5132 14359 embedded targets, s_mips_mask is used so that we can set the PDR
bdaaa2e1 14360 information correctly. We can't use the ecoff routines because they
252b5132
RH
14361 make reference to the ecoff symbol table (in the mdebug section). */
14362
14363static void
17a2f251 14364s_mips_mask (int reg_type)
252b5132 14365{
ecb4347a
DJ
14366#ifdef OBJ_ELF
14367 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
252b5132 14368 {
ecb4347a 14369 long mask, off;
252b5132 14370
ecb4347a
DJ
14371 if (cur_proc_ptr == (procS *) NULL)
14372 {
14373 as_warn (_(".mask/.fmask outside of .ent"));
14374 demand_empty_rest_of_line ();
14375 return;
14376 }
252b5132 14377
ecb4347a
DJ
14378 if (get_absolute_expression_and_terminator (&mask) != ',')
14379 {
14380 as_warn (_("Bad .mask/.fmask directive"));
14381 --input_line_pointer;
14382 demand_empty_rest_of_line ();
14383 return;
14384 }
252b5132 14385
ecb4347a
DJ
14386 off = get_absolute_expression ();
14387
14388 if (reg_type == 'F')
14389 {
14390 cur_proc_ptr->fpreg_mask = mask;
14391 cur_proc_ptr->fpreg_offset = off;
14392 }
14393 else
14394 {
14395 cur_proc_ptr->reg_mask = mask;
14396 cur_proc_ptr->reg_offset = off;
14397 }
14398
14399 demand_empty_rest_of_line ();
252b5132
RH
14400 }
14401 else
ecb4347a
DJ
14402#endif /* OBJ_ELF */
14403 s_ignore (reg_type);
252b5132
RH
14404}
14405
14406/* The .loc directive. */
14407
14408#if 0
14409static void
17a2f251 14410s_loc (int x)
252b5132
RH
14411{
14412 symbolS *symbolP;
14413 int lineno;
14414 int addroff;
14415
14416 assert (now_seg == text_section);
14417
14418 lineno = get_number ();
14419 addroff = frag_now_fix ();
14420
14421 symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
14422 S_SET_TYPE (symbolP, N_SLINE);
14423 S_SET_OTHER (symbolP, 0);
14424 S_SET_DESC (symbolP, lineno);
14425 symbolP->sy_segment = now_seg;
14426}
14427#endif
e7af610e 14428
316f5878
RS
14429/* A table describing all the processors gas knows about. Names are
14430 matched in the order listed.
e7af610e 14431
316f5878
RS
14432 To ease comparison, please keep this table in the same order as
14433 gcc's mips_cpu_info_table[]. */
e972090a
NC
14434static const struct mips_cpu_info mips_cpu_info_table[] =
14435{
316f5878
RS
14436 /* Entries for generic ISAs */
14437 { "mips1", 1, ISA_MIPS1, CPU_R3000 },
14438 { "mips2", 1, ISA_MIPS2, CPU_R6000 },
14439 { "mips3", 1, ISA_MIPS3, CPU_R4000 },
14440 { "mips4", 1, ISA_MIPS4, CPU_R8000 },
14441 { "mips5", 1, ISA_MIPS5, CPU_MIPS5 },
14442 { "mips32", 1, ISA_MIPS32, CPU_MIPS32 },
af7ee8bf 14443 { "mips32r2", 1, ISA_MIPS32R2, CPU_MIPS32R2 },
316f5878 14444 { "mips64", 1, ISA_MIPS64, CPU_MIPS64 },
5f74bc13 14445 { "mips64r2", 1, ISA_MIPS64R2, CPU_MIPS64R2 },
316f5878
RS
14446
14447 /* MIPS I */
14448 { "r3000", 0, ISA_MIPS1, CPU_R3000 },
14449 { "r2000", 0, ISA_MIPS1, CPU_R3000 },
14450 { "r3900", 0, ISA_MIPS1, CPU_R3900 },
14451
14452 /* MIPS II */
14453 { "r6000", 0, ISA_MIPS2, CPU_R6000 },
14454
14455 /* MIPS III */
14456 { "r4000", 0, ISA_MIPS3, CPU_R4000 },
14457 { "r4010", 0, ISA_MIPS2, CPU_R4010 },
14458 { "vr4100", 0, ISA_MIPS3, CPU_VR4100 },
14459 { "vr4111", 0, ISA_MIPS3, CPU_R4111 },
60b63b72
RS
14460 { "vr4120", 0, ISA_MIPS3, CPU_VR4120 },
14461 { "vr4130", 0, ISA_MIPS3, CPU_VR4120 },
14462 { "vr4181", 0, ISA_MIPS3, CPU_R4111 },
316f5878
RS
14463 { "vr4300", 0, ISA_MIPS3, CPU_R4300 },
14464 { "r4400", 0, ISA_MIPS3, CPU_R4400 },
14465 { "r4600", 0, ISA_MIPS3, CPU_R4600 },
14466 { "orion", 0, ISA_MIPS3, CPU_R4600 },
14467 { "r4650", 0, ISA_MIPS3, CPU_R4650 },
14468
14469 /* MIPS IV */
14470 { "r8000", 0, ISA_MIPS4, CPU_R8000 },
14471 { "r10000", 0, ISA_MIPS4, CPU_R10000 },
14472 { "r12000", 0, ISA_MIPS4, CPU_R12000 },
14473 { "vr5000", 0, ISA_MIPS4, CPU_R5000 },
60b63b72
RS
14474 { "vr5400", 0, ISA_MIPS4, CPU_VR5400 },
14475 { "vr5500", 0, ISA_MIPS4, CPU_VR5500 },
316f5878
RS
14476 { "rm5200", 0, ISA_MIPS4, CPU_R5000 },
14477 { "rm5230", 0, ISA_MIPS4, CPU_R5000 },
14478 { "rm5231", 0, ISA_MIPS4, CPU_R5000 },
14479 { "rm5261", 0, ISA_MIPS4, CPU_R5000 },
14480 { "rm5721", 0, ISA_MIPS4, CPU_R5000 },
5a7ea749
RS
14481 { "rm7000", 0, ISA_MIPS4, CPU_RM7000 },
14482 { "rm9000", 0, ISA_MIPS4, CPU_RM7000 },
316f5878
RS
14483
14484 /* MIPS 32 */
fef14a42 14485 { "4kc", 0, ISA_MIPS32, CPU_MIPS32 },
316f5878
RS
14486 { "4km", 0, ISA_MIPS32, CPU_MIPS32 },
14487 { "4kp", 0, ISA_MIPS32, CPU_MIPS32 },
e7af610e 14488
316f5878
RS
14489 /* MIPS 64 */
14490 { "5kc", 0, ISA_MIPS64, CPU_MIPS64 },
14491 { "20kc", 0, ISA_MIPS64, CPU_MIPS64 },
e7af610e 14492
c7a23324 14493 /* Broadcom SB-1 CPU core */
316f5878 14494 { "sb1", 0, ISA_MIPS64, CPU_SB1 },
e7af610e 14495
316f5878
RS
14496 /* End marker */
14497 { NULL, 0, 0, 0 }
14498};
e7af610e 14499
84ea6cf2 14500
316f5878
RS
14501/* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
14502 with a final "000" replaced by "k". Ignore case.
e7af610e 14503
316f5878 14504 Note: this function is shared between GCC and GAS. */
c6c98b38 14505
b34976b6 14506static bfd_boolean
17a2f251 14507mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
14508{
14509 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
14510 given++, canonical++;
14511
14512 return ((*given == 0 && *canonical == 0)
14513 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
14514}
14515
14516
14517/* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
14518 CPU name. We've traditionally allowed a lot of variation here.
14519
14520 Note: this function is shared between GCC and GAS. */
14521
b34976b6 14522static bfd_boolean
17a2f251 14523mips_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
14524{
14525 /* First see if the name matches exactly, or with a final "000"
14526 turned into "k". */
14527 if (mips_strict_matching_cpu_name_p (canonical, given))
b34976b6 14528 return TRUE;
316f5878
RS
14529
14530 /* If not, try comparing based on numerical designation alone.
14531 See if GIVEN is an unadorned number, or 'r' followed by a number. */
14532 if (TOLOWER (*given) == 'r')
14533 given++;
14534 if (!ISDIGIT (*given))
b34976b6 14535 return FALSE;
316f5878
RS
14536
14537 /* Skip over some well-known prefixes in the canonical name,
14538 hoping to find a number there too. */
14539 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
14540 canonical += 2;
14541 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
14542 canonical += 2;
14543 else if (TOLOWER (canonical[0]) == 'r')
14544 canonical += 1;
14545
14546 return mips_strict_matching_cpu_name_p (canonical, given);
14547}
14548
14549
14550/* Parse an option that takes the name of a processor as its argument.
14551 OPTION is the name of the option and CPU_STRING is the argument.
14552 Return the corresponding processor enumeration if the CPU_STRING is
14553 recognized, otherwise report an error and return null.
14554
14555 A similar function exists in GCC. */
e7af610e
NC
14556
14557static const struct mips_cpu_info *
17a2f251 14558mips_parse_cpu (const char *option, const char *cpu_string)
e7af610e 14559{
316f5878 14560 const struct mips_cpu_info *p;
e7af610e 14561
316f5878
RS
14562 /* 'from-abi' selects the most compatible architecture for the given
14563 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
14564 EABIs, we have to decide whether we're using the 32-bit or 64-bit
14565 version. Look first at the -mgp options, if given, otherwise base
14566 the choice on MIPS_DEFAULT_64BIT.
e7af610e 14567
316f5878
RS
14568 Treat NO_ABI like the EABIs. One reason to do this is that the
14569 plain 'mips' and 'mips64' configs have 'from-abi' as their default
14570 architecture. This code picks MIPS I for 'mips' and MIPS III for
14571 'mips64', just as we did in the days before 'from-abi'. */
14572 if (strcasecmp (cpu_string, "from-abi") == 0)
14573 {
14574 if (ABI_NEEDS_32BIT_REGS (mips_abi))
14575 return mips_cpu_info_from_isa (ISA_MIPS1);
14576
14577 if (ABI_NEEDS_64BIT_REGS (mips_abi))
14578 return mips_cpu_info_from_isa (ISA_MIPS3);
14579
14580 if (file_mips_gp32 >= 0)
14581 return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
14582
14583 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
14584 ? ISA_MIPS3
14585 : ISA_MIPS1);
14586 }
14587
14588 /* 'default' has traditionally been a no-op. Probably not very useful. */
14589 if (strcasecmp (cpu_string, "default") == 0)
14590 return 0;
14591
14592 for (p = mips_cpu_info_table; p->name != 0; p++)
14593 if (mips_matching_cpu_name_p (p->name, cpu_string))
14594 return p;
14595
14596 as_bad ("Bad value (%s) for %s", cpu_string, option);
14597 return 0;
e7af610e
NC
14598}
14599
316f5878
RS
14600/* Return the canonical processor information for ISA (a member of the
14601 ISA_MIPS* enumeration). */
14602
e7af610e 14603static const struct mips_cpu_info *
17a2f251 14604mips_cpu_info_from_isa (int isa)
e7af610e
NC
14605{
14606 int i;
14607
14608 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14609 if (mips_cpu_info_table[i].is_isa
316f5878 14610 && isa == mips_cpu_info_table[i].isa)
e7af610e
NC
14611 return (&mips_cpu_info_table[i]);
14612
e972090a 14613 return NULL;
e7af610e 14614}
fef14a42
TS
14615
14616static const struct mips_cpu_info *
17a2f251 14617mips_cpu_info_from_arch (int arch)
fef14a42
TS
14618{
14619 int i;
14620
14621 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14622 if (arch == mips_cpu_info_table[i].cpu)
14623 return (&mips_cpu_info_table[i]);
14624
14625 return NULL;
14626}
316f5878
RS
14627\f
14628static void
17a2f251 14629show (FILE *stream, const char *string, int *col_p, int *first_p)
316f5878
RS
14630{
14631 if (*first_p)
14632 {
14633 fprintf (stream, "%24s", "");
14634 *col_p = 24;
14635 }
14636 else
14637 {
14638 fprintf (stream, ", ");
14639 *col_p += 2;
14640 }
e7af610e 14641
316f5878
RS
14642 if (*col_p + strlen (string) > 72)
14643 {
14644 fprintf (stream, "\n%24s", "");
14645 *col_p = 24;
14646 }
14647
14648 fprintf (stream, "%s", string);
14649 *col_p += strlen (string);
14650
14651 *first_p = 0;
14652}
14653
14654void
17a2f251 14655md_show_usage (FILE *stream)
e7af610e 14656{
316f5878
RS
14657 int column, first;
14658 size_t i;
14659
14660 fprintf (stream, _("\
14661MIPS options:\n\
14662-membedded-pic generate embedded position independent code\n\
14663-EB generate big endian output\n\
14664-EL generate little endian output\n\
14665-g, -g2 do not remove unneeded NOPs or swap branches\n\
14666-G NUM allow referencing objects up to NUM bytes\n\
14667 implicitly with the gp register [default 8]\n"));
14668 fprintf (stream, _("\
14669-mips1 generate MIPS ISA I instructions\n\
14670-mips2 generate MIPS ISA II instructions\n\
14671-mips3 generate MIPS ISA III instructions\n\
14672-mips4 generate MIPS ISA IV instructions\n\
14673-mips5 generate MIPS ISA V instructions\n\
14674-mips32 generate MIPS32 ISA instructions\n\
af7ee8bf 14675-mips32r2 generate MIPS32 release 2 ISA instructions\n\
316f5878 14676-mips64 generate MIPS64 ISA instructions\n\
5f74bc13 14677-mips64r2 generate MIPS64 release 2 ISA instructions\n\
316f5878
RS
14678-march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
14679
14680 first = 1;
e7af610e
NC
14681
14682 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
316f5878
RS
14683 show (stream, mips_cpu_info_table[i].name, &column, &first);
14684 show (stream, "from-abi", &column, &first);
14685 fputc ('\n', stream);
e7af610e 14686
316f5878
RS
14687 fprintf (stream, _("\
14688-mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
14689-no-mCPU don't generate code specific to CPU.\n\
14690 For -mCPU and -no-mCPU, CPU must be one of:\n"));
14691
14692 first = 1;
14693
14694 show (stream, "3900", &column, &first);
14695 show (stream, "4010", &column, &first);
14696 show (stream, "4100", &column, &first);
14697 show (stream, "4650", &column, &first);
14698 fputc ('\n', stream);
14699
14700 fprintf (stream, _("\
14701-mips16 generate mips16 instructions\n\
14702-no-mips16 do not generate mips16 instructions\n"));
14703 fprintf (stream, _("\
14704-mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
14705-mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
14706-O0 remove unneeded NOPs, do not swap branches\n\
14707-O remove unneeded NOPs and swap branches\n\
14708-n warn about NOPs generated from macros\n\
14709--[no-]construct-floats [dis]allow floating point values to be constructed\n\
14710--trap, --no-break trap exception on div by 0 and mult overflow\n\
14711--break, --no-trap break exception on div by 0 and mult overflow\n"));
14712#ifdef OBJ_ELF
14713 fprintf (stream, _("\
14714-KPIC, -call_shared generate SVR4 position independent code\n\
14715-non_shared do not generate position independent code\n\
14716-xgot assume a 32 bit GOT\n\
dcd410fe 14717-mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
316f5878
RS
14718-mabi=ABI create ABI conformant object file for:\n"));
14719
14720 first = 1;
14721
14722 show (stream, "32", &column, &first);
14723 show (stream, "o64", &column, &first);
14724 show (stream, "n32", &column, &first);
14725 show (stream, "64", &column, &first);
14726 show (stream, "eabi", &column, &first);
14727
14728 fputc ('\n', stream);
14729
14730 fprintf (stream, _("\
14731-32 create o32 ABI object file (default)\n\
14732-n32 create n32 ABI object file\n\
14733-64 create 64 ABI object file\n"));
14734#endif
e7af610e 14735}
14e777e0
KB
14736
14737enum dwarf2_format
17a2f251 14738mips_dwarf2_format (void)
14e777e0
KB
14739{
14740 if (mips_abi == N64_ABI)
1de5b6a1
AO
14741 {
14742#ifdef TE_IRIX
14743 return dwarf2_format_64bit_irix;
14744#else
14745 return dwarf2_format_64bit;
14746#endif
14747 }
14e777e0
KB
14748 else
14749 return dwarf2_format_32bit;
14750}
This page took 1.292435 seconds and 4 git commands to generate.