* som.c (struct som_misc_symbol_info): Add is_comdat, is_common and
[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 113/* The name of the readonly data section. */
4d0d148d 114#define RDATA_SECTION_NAME (OUTPUT_FLAVOR == bfd_target_ecoff_flavour \
252b5132 115 ? ".rdata" \
056350c6
NC
116 : OUTPUT_FLAVOR == bfd_target_coff_flavour \
117 ? ".rdata" \
252b5132
RH
118 : OUTPUT_FLAVOR == bfd_target_elf_flavour \
119 ? ".rodata" \
120 : (abort (), ""))
121
a325df1d
TS
122/* The ABI to use. */
123enum mips_abi_level
124{
125 NO_ABI = 0,
126 O32_ABI,
127 O64_ABI,
128 N32_ABI,
129 N64_ABI,
130 EABI_ABI
131};
132
133/* MIPS ABI we are using for this output file. */
316f5878 134static enum mips_abi_level mips_abi = NO_ABI;
a325df1d 135
143d77c5
EC
136/* Whether or not we have code that can call pic code. */
137int mips_abicalls = FALSE;
138
252b5132
RH
139/* This is the set of options which may be modified by the .set
140 pseudo-op. We use a struct so that .set push and .set pop are more
141 reliable. */
142
e972090a
NC
143struct mips_set_options
144{
252b5132
RH
145 /* MIPS ISA (Instruction Set Architecture) level. This is set to -1
146 if it has not been initialized. Changed by `.set mipsN', and the
147 -mipsN command line option, and the default CPU. */
148 int isa;
1f25f5d3
CD
149 /* Enabled Application Specific Extensions (ASEs). These are set to -1
150 if they have not been initialized. Changed by `.set <asename>', by
151 command line options, and based on the default architecture. */
152 int ase_mips3d;
deec1734 153 int ase_mdmx;
252b5132
RH
154 /* Whether we are assembling for the mips16 processor. 0 if we are
155 not, 1 if we are, and -1 if the value has not been initialized.
156 Changed by `.set mips16' and `.set nomips16', and the -mips16 and
157 -nomips16 command line options, and the default CPU. */
158 int mips16;
159 /* Non-zero if we should not reorder instructions. Changed by `.set
160 reorder' and `.set noreorder'. */
161 int noreorder;
162 /* Non-zero if we should not permit the $at ($1) register to be used
163 in instructions. Changed by `.set at' and `.set noat'. */
164 int noat;
165 /* Non-zero if we should warn when a macro instruction expands into
166 more than one machine instruction. Changed by `.set nomacro' and
167 `.set macro'. */
168 int warn_about_macros;
169 /* Non-zero if we should not move instructions. Changed by `.set
170 move', `.set volatile', `.set nomove', and `.set novolatile'. */
171 int nomove;
172 /* Non-zero if we should not optimize branches by moving the target
173 of the branch into the delay slot. Actually, we don't perform
174 this optimization anyhow. Changed by `.set bopt' and `.set
175 nobopt'. */
176 int nobopt;
177 /* Non-zero if we should not autoextend mips16 instructions.
178 Changed by `.set autoextend' and `.set noautoextend'. */
179 int noautoextend;
a325df1d
TS
180 /* Restrict general purpose registers and floating point registers
181 to 32 bit. This is initially determined when -mgp32 or -mfp32
182 is passed but can changed if the assembler code uses .set mipsN. */
183 int gp32;
184 int fp32;
fef14a42
TS
185 /* MIPS architecture (CPU) type. Changed by .set arch=FOO, the -march
186 command line option, and the default CPU. */
187 int arch;
252b5132
RH
188};
189
a325df1d 190/* True if -mgp32 was passed. */
a8e8e863 191static int file_mips_gp32 = -1;
a325df1d
TS
192
193/* True if -mfp32 was passed. */
a8e8e863 194static int file_mips_fp32 = -1;
a325df1d 195
252b5132 196/* This is the struct we use to hold the current set of options. Note
a4672219 197 that we must set the isa field to ISA_UNKNOWN and the ASE fields to
e7af610e 198 -1 to indicate that they have not been initialized. */
252b5132 199
e972090a
NC
200static struct mips_set_options mips_opts =
201{
fef14a42 202 ISA_UNKNOWN, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, CPU_UNKNOWN
e7af610e 203};
252b5132
RH
204
205/* These variables are filled in with the masks of registers used.
206 The object format code reads them and puts them in the appropriate
207 place. */
208unsigned long mips_gprmask;
209unsigned long mips_cprmask[4];
210
211/* MIPS ISA we are using for this output file. */
e7af610e 212static int file_mips_isa = ISA_UNKNOWN;
252b5132 213
a4672219
TS
214/* True if -mips16 was passed or implied by arguments passed on the
215 command line (e.g., by -march). */
216static int file_ase_mips16;
217
1f25f5d3
CD
218/* True if -mips3d was passed or implied by arguments passed on the
219 command line (e.g., by -march). */
220static int file_ase_mips3d;
221
deec1734
CD
222/* True if -mdmx was passed or implied by arguments passed on the
223 command line (e.g., by -march). */
224static int file_ase_mdmx;
225
ec68c924 226/* The argument of the -march= flag. The architecture we are assembling. */
fef14a42 227static int file_mips_arch = CPU_UNKNOWN;
316f5878 228static const char *mips_arch_string;
ec68c924
EC
229
230/* The argument of the -mtune= flag. The architecture for which we
231 are optimizing. */
232static int mips_tune = CPU_UNKNOWN;
316f5878 233static const char *mips_tune_string;
ec68c924 234
316f5878 235/* True when generating 32-bit code for a 64-bit processor. */
252b5132
RH
236static int mips_32bitmode = 0;
237
316f5878
RS
238/* True if the given ABI requires 32-bit registers. */
239#define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
240
241/* Likewise 64-bit registers. */
242#define ABI_NEEDS_64BIT_REGS(ABI) \
243 ((ABI) == N32_ABI \
244 || (ABI) == N64_ABI \
245 || (ABI) == O64_ABI)
246
bdaaa2e1 247/* Return true if ISA supports 64 bit gp register instructions. */
9ce8a5dd 248#define ISA_HAS_64BIT_REGS(ISA) ( \
e7af610e
NC
249 (ISA) == ISA_MIPS3 \
250 || (ISA) == ISA_MIPS4 \
84ea6cf2 251 || (ISA) == ISA_MIPS5 \
d1cf510e 252 || (ISA) == ISA_MIPS64 \
5f74bc13 253 || (ISA) == ISA_MIPS64R2 \
9ce8a5dd
GRK
254 )
255
af7ee8bf
CD
256/* Return true if ISA supports 64-bit right rotate (dror et al.)
257 instructions. */
258#define ISA_HAS_DROR(ISA) ( \
5f74bc13 259 (ISA) == ISA_MIPS64R2 \
af7ee8bf
CD
260 )
261
262/* Return true if ISA supports 32-bit right rotate (ror et al.)
263 instructions. */
264#define ISA_HAS_ROR(ISA) ( \
265 (ISA) == ISA_MIPS32R2 \
5f74bc13 266 || (ISA) == ISA_MIPS64R2 \
af7ee8bf
CD
267 )
268
e013f690 269#define HAVE_32BIT_GPRS \
316f5878 270 (mips_opts.gp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
ca4e0257 271
e013f690 272#define HAVE_32BIT_FPRS \
316f5878 273 (mips_opts.fp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
ca4e0257
RS
274
275#define HAVE_64BIT_GPRS (! HAVE_32BIT_GPRS)
276#define HAVE_64BIT_FPRS (! HAVE_32BIT_FPRS)
277
316f5878 278#define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
e013f690 279
316f5878 280#define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
e013f690
TS
281
282/* We can only have 64bit addresses if the object file format
283 supports it. */
afdbd6d0
CD
284#define HAVE_32BIT_ADDRESSES \
285 (HAVE_32BIT_GPRS \
286 || ((bfd_arch_bits_per_address (stdoutput) == 32 \
287 || ! HAVE_64BIT_OBJECTS) \
288 && mips_pic != EMBEDDED_PIC))
e013f690
TS
289
290#define HAVE_64BIT_ADDRESSES (! HAVE_32BIT_ADDRESSES)
ca4e0257 291
b7c7d6c1
TS
292/* Addresses are loaded in different ways, depending on the address size
293 in use. The n32 ABI Documentation also mandates the use of additions
294 with overflow checking, but existing implementations don't follow it. */
f899b4b8 295#define ADDRESS_ADD_INSN \
b7c7d6c1 296 (HAVE_32BIT_ADDRESSES ? "addu" : "daddu")
f899b4b8
TS
297
298#define ADDRESS_ADDI_INSN \
b7c7d6c1 299 (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu")
f899b4b8
TS
300
301#define ADDRESS_LOAD_INSN \
302 (HAVE_32BIT_ADDRESSES ? "lw" : "ld")
303
304#define ADDRESS_STORE_INSN \
305 (HAVE_32BIT_ADDRESSES ? "sw" : "sd")
306
a4672219 307/* Return true if the given CPU supports the MIPS16 ASE. */
3396de36
TS
308#define CPU_HAS_MIPS16(cpu) \
309 (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0 \
310 || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
a4672219 311
1f25f5d3
CD
312/* Return true if the given CPU supports the MIPS3D ASE. */
313#define CPU_HAS_MIPS3D(cpu) ((cpu) == CPU_SB1 \
314 )
315
deec1734 316/* Return true if the given CPU supports the MDMX ASE. */
b34976b6 317#define CPU_HAS_MDMX(cpu) (FALSE \
deec1734
CD
318 )
319
60b63b72
RS
320/* True if CPU has a dror instruction. */
321#define CPU_HAS_DROR(CPU) ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
322
323/* True if CPU has a ror instruction. */
324#define CPU_HAS_ROR(CPU) CPU_HAS_DROR (CPU)
325
c8978940
CD
326/* True if mflo and mfhi can be immediately followed by instructions
327 which write to the HI and LO registers.
328
329 According to MIPS specifications, MIPS ISAs I, II, and III need
330 (at least) two instructions between the reads of HI/LO and
331 instructions which write them, and later ISAs do not. Contradicting
332 the MIPS specifications, some MIPS IV processor user manuals (e.g.
333 the UM for the NEC Vr5000) document needing the instructions between
334 HI/LO reads and writes, as well. Therefore, we declare only MIPS32,
335 MIPS64 and later ISAs to have the interlocks, plus any specific
336 earlier-ISA CPUs for which CPU documentation declares that the
337 instructions are really interlocked. */
338#define hilo_interlocks \
339 (mips_opts.isa == ISA_MIPS32 \
340 || mips_opts.isa == ISA_MIPS32R2 \
341 || mips_opts.isa == ISA_MIPS64 \
342 || mips_opts.isa == ISA_MIPS64R2 \
343 || mips_opts.arch == CPU_R4010 \
344 || mips_opts.arch == CPU_R10000 \
345 || mips_opts.arch == CPU_R12000 \
346 || mips_opts.arch == CPU_RM7000 \
c8978940
CD
347 || mips_opts.arch == CPU_VR5500 \
348 )
252b5132
RH
349
350/* Whether the processor uses hardware interlocks to protect reads
81912461
ILT
351 from the GPRs after they are loaded from memory, and thus does not
352 require nops to be inserted. This applies to instructions marked
353 INSN_LOAD_MEMORY_DELAY. These nops are only required at MIPS ISA
354 level I. */
252b5132 355#define gpr_interlocks \
e7af610e 356 (mips_opts.isa != ISA_MIPS1 \
fef14a42 357 || mips_opts.arch == CPU_R3900)
252b5132 358
81912461
ILT
359/* Whether the processor uses hardware interlocks to avoid delays
360 required by coprocessor instructions, and thus does not require
361 nops to be inserted. This applies to instructions marked
362 INSN_LOAD_COPROC_DELAY, INSN_COPROC_MOVE_DELAY, and to delays
363 between instructions marked INSN_WRITE_COND_CODE and ones marked
364 INSN_READ_COND_CODE. These nops are only required at MIPS ISA
365 levels I, II, and III. */
bdaaa2e1 366/* Itbl support may require additional care here. */
81912461
ILT
367#define cop_interlocks \
368 ((mips_opts.isa != ISA_MIPS1 \
369 && mips_opts.isa != ISA_MIPS2 \
370 && mips_opts.isa != ISA_MIPS3) \
371 || mips_opts.arch == CPU_R4300 \
81912461
ILT
372 )
373
374/* Whether the processor uses hardware interlocks to protect reads
375 from coprocessor registers after they are loaded from memory, and
376 thus does not require nops to be inserted. This applies to
377 instructions marked INSN_COPROC_MEMORY_DELAY. These nops are only
378 requires at MIPS ISA level I. */
379#define cop_mem_interlocks (mips_opts.isa != ISA_MIPS1)
252b5132 380
6b76fefe
CM
381/* Is this a mfhi or mflo instruction? */
382#define MF_HILO_INSN(PINFO) \
383 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
384
252b5132
RH
385/* MIPS PIC level. */
386
a161fe53 387enum mips_pic_level mips_pic;
252b5132 388
c9914766 389/* 1 if we should generate 32 bit offsets from the $gp register in
252b5132 390 SVR4_PIC mode. Currently has no meaning in other modes. */
c9914766 391static int mips_big_got = 0;
252b5132
RH
392
393/* 1 if trap instructions should used for overflow rather than break
394 instructions. */
c9914766 395static int mips_trap = 0;
252b5132 396
119d663a 397/* 1 if double width floating point constants should not be constructed
b6ff326e 398 by assembling two single width halves into two single width floating
119d663a
NC
399 point registers which just happen to alias the double width destination
400 register. On some architectures this aliasing can be disabled by a bit
d547a75e 401 in the status register, and the setting of this bit cannot be determined
119d663a
NC
402 automatically at assemble time. */
403static int mips_disable_float_construction;
404
252b5132
RH
405/* Non-zero if any .set noreorder directives were used. */
406
407static int mips_any_noreorder;
408
6b76fefe
CM
409/* Non-zero if nops should be inserted when the register referenced in
410 an mfhi/mflo instruction is read in the next two instructions. */
411static int mips_7000_hilo_fix;
412
252b5132 413/* The size of the small data section. */
156c2f8b 414static unsigned int g_switch_value = 8;
252b5132
RH
415/* Whether the -G option was used. */
416static int g_switch_seen = 0;
417
418#define N_RMASK 0xc4
419#define N_VFP 0xd4
420
421/* If we can determine in advance that GP optimization won't be
422 possible, we can skip the relaxation stuff that tries to produce
423 GP-relative references. This makes delay slot optimization work
424 better.
425
426 This function can only provide a guess, but it seems to work for
fba2b7f9
GK
427 gcc output. It needs to guess right for gcc, otherwise gcc
428 will put what it thinks is a GP-relative instruction in a branch
429 delay slot.
252b5132
RH
430
431 I don't know if a fix is needed for the SVR4_PIC mode. I've only
432 fixed it for the non-PIC mode. KR 95/04/07 */
17a2f251 433static int nopic_need_relax (symbolS *, int);
252b5132
RH
434
435/* handle of the OPCODE hash table */
436static struct hash_control *op_hash = NULL;
437
438/* The opcode hash table we use for the mips16. */
439static struct hash_control *mips16_op_hash = NULL;
440
441/* This array holds the chars that always start a comment. If the
442 pre-processor is disabled, these aren't very useful */
443const char comment_chars[] = "#";
444
445/* This array holds the chars that only start a comment at the beginning of
446 a line. If the line seems to have the form '# 123 filename'
447 .line and .file directives will appear in the pre-processed output */
448/* Note that input_file.c hand checks for '#' at the beginning of the
449 first line of the input file. This is because the compiler outputs
bdaaa2e1 450 #NO_APP at the beginning of its output. */
252b5132
RH
451/* Also note that C style comments are always supported. */
452const char line_comment_chars[] = "#";
453
bdaaa2e1 454/* This array holds machine specific line separator characters. */
63a0b638 455const char line_separator_chars[] = ";";
252b5132
RH
456
457/* Chars that can be used to separate mant from exp in floating point nums */
458const char EXP_CHARS[] = "eE";
459
460/* Chars that mean this number is a floating point constant */
461/* As in 0f12.456 */
462/* or 0d1.2345e12 */
463const char FLT_CHARS[] = "rRsSfFdDxXpP";
464
465/* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
466 changed in read.c . Ideally it shouldn't have to know about it at all,
467 but nothing is ideal around here.
468 */
469
470static char *insn_error;
471
472static int auto_align = 1;
473
474/* When outputting SVR4 PIC code, the assembler needs to know the
475 offset in the stack frame from which to restore the $gp register.
476 This is set by the .cprestore pseudo-op, and saved in this
477 variable. */
478static offsetT mips_cprestore_offset = -1;
479
67c1ffbe 480/* Similar for NewABI PIC code, where $gp is callee-saved. NewABI has some
6478892d 481 more optimizations, it can use a register value instead of a memory-saved
956cd1d6 482 offset and even an other register than $gp as global pointer. */
6478892d
TS
483static offsetT mips_cpreturn_offset = -1;
484static int mips_cpreturn_register = -1;
485static int mips_gp_register = GP;
def2e0dd 486static int mips_gprel_offset = 0;
6478892d 487
7a621144
DJ
488/* Whether mips_cprestore_offset has been set in the current function
489 (or whether it has already been warned about, if not). */
490static int mips_cprestore_valid = 0;
491
252b5132
RH
492/* This is the register which holds the stack frame, as set by the
493 .frame pseudo-op. This is needed to implement .cprestore. */
494static int mips_frame_reg = SP;
495
7a621144
DJ
496/* Whether mips_frame_reg has been set in the current function
497 (or whether it has already been warned about, if not). */
498static int mips_frame_reg_valid = 0;
499
252b5132
RH
500/* To output NOP instructions correctly, we need to keep information
501 about the previous two instructions. */
502
503/* Whether we are optimizing. The default value of 2 means to remove
504 unneeded NOPs and swap branch instructions when possible. A value
505 of 1 means to not swap branches. A value of 0 means to always
506 insert NOPs. */
507static int mips_optimize = 2;
508
509/* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
510 equivalent to seeing no -g option at all. */
511static int mips_debug = 0;
512
513/* The previous instruction. */
514static struct mips_cl_insn prev_insn;
515
516/* The instruction before prev_insn. */
517static struct mips_cl_insn prev_prev_insn;
518
519/* If we don't want information for prev_insn or prev_prev_insn, we
520 point the insn_mo field at this dummy integer. */
43841e91 521static const struct mips_opcode dummy_opcode = { NULL, NULL, 0, 0, 0, 0 };
252b5132
RH
522
523/* Non-zero if prev_insn is valid. */
524static int prev_insn_valid;
525
526/* The frag for the previous instruction. */
527static struct frag *prev_insn_frag;
528
529/* The offset into prev_insn_frag for the previous instruction. */
530static long prev_insn_where;
531
532/* The reloc type for the previous instruction, if any. */
f6688943 533static bfd_reloc_code_real_type prev_insn_reloc_type[3];
252b5132
RH
534
535/* The reloc for the previous instruction, if any. */
f6688943 536static fixS *prev_insn_fixp[3];
252b5132
RH
537
538/* Non-zero if the previous instruction was in a delay slot. */
539static int prev_insn_is_delay_slot;
540
541/* Non-zero if the previous instruction was in a .set noreorder. */
542static int prev_insn_unreordered;
543
544/* Non-zero if the previous instruction uses an extend opcode (if
545 mips16). */
546static int prev_insn_extended;
547
548/* Non-zero if the previous previous instruction was in a .set
549 noreorder. */
550static int prev_prev_insn_unreordered;
551
552/* If this is set, it points to a frag holding nop instructions which
553 were inserted before the start of a noreorder section. If those
554 nops turn out to be unnecessary, the size of the frag can be
555 decreased. */
556static fragS *prev_nop_frag;
557
558/* The number of nop instructions we created in prev_nop_frag. */
559static int prev_nop_frag_holds;
560
561/* The number of nop instructions that we know we need in
bdaaa2e1 562 prev_nop_frag. */
252b5132
RH
563static int prev_nop_frag_required;
564
565/* The number of instructions we've seen since prev_nop_frag. */
566static int prev_nop_frag_since;
567
568/* For ECOFF and ELF, relocations against symbols are done in two
569 parts, with a HI relocation and a LO relocation. Each relocation
570 has only 16 bits of space to store an addend. This means that in
571 order for the linker to handle carries correctly, it must be able
572 to locate both the HI and the LO relocation. This means that the
573 relocations must appear in order in the relocation table.
574
575 In order to implement this, we keep track of each unmatched HI
576 relocation. We then sort them so that they immediately precede the
bdaaa2e1 577 corresponding LO relocation. */
252b5132 578
e972090a
NC
579struct mips_hi_fixup
580{
252b5132
RH
581 /* Next HI fixup. */
582 struct mips_hi_fixup *next;
583 /* This fixup. */
584 fixS *fixp;
585 /* The section this fixup is in. */
586 segT seg;
587};
588
589/* The list of unmatched HI relocs. */
590
591static struct mips_hi_fixup *mips_hi_fixup_list;
592
64bdfcaf
RS
593/* The frag containing the last explicit relocation operator.
594 Null if explicit relocations have not been used. */
595
596static fragS *prev_reloc_op_frag;
597
252b5132
RH
598/* Map normal MIPS register numbers to mips16 register numbers. */
599
600#define X ILLEGAL_REG
e972090a
NC
601static const int mips32_to_16_reg_map[] =
602{
252b5132
RH
603 X, X, 2, 3, 4, 5, 6, 7,
604 X, X, X, X, X, X, X, X,
605 0, 1, X, X, X, X, X, X,
606 X, X, X, X, X, X, X, X
607};
608#undef X
609
610/* Map mips16 register numbers to normal MIPS register numbers. */
611
e972090a
NC
612static const unsigned int mips16_to_32_reg_map[] =
613{
252b5132
RH
614 16, 17, 2, 3, 4, 5, 6, 7
615};
60b63b72 616
d766e8ec 617static int mips_fix_vr4120;
4a6a3df4
AO
618
619/* We don't relax branches by default, since this causes us to expand
620 `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
621 fail to compute the offset before expanding the macro to the most
622 efficient expansion. */
623
624static int mips_relax_branch;
252b5132 625\f
4d7206a2
RS
626/* The expansion of many macros depends on the type of symbol that
627 they refer to. For example, when generating position-dependent code,
628 a macro that refers to a symbol may have two different expansions,
629 one which uses GP-relative addresses and one which uses absolute
630 addresses. When generating SVR4-style PIC, a macro may have
631 different expansions for local and global symbols.
632
633 We handle these situations by generating both sequences and putting
634 them in variant frags. In position-dependent code, the first sequence
635 will be the GP-relative one and the second sequence will be the
636 absolute one. In SVR4 PIC, the first sequence will be for global
637 symbols and the second will be for local symbols.
638
584892a6
RS
639 The frag's "subtype" is RELAX_ENCODE (FIRST, SECOND), where FIRST and
640 SECOND are the lengths of the two sequences in bytes. These fields
641 can be extracted using RELAX_FIRST() and RELAX_SECOND(). In addition,
642 the subtype has the following flags:
4d7206a2 643
584892a6
RS
644 RELAX_USE_SECOND
645 Set if it has been decided that we should use the second
646 sequence instead of the first.
647
648 RELAX_SECOND_LONGER
649 Set in the first variant frag if the macro's second implementation
650 is longer than its first. This refers to the macro as a whole,
651 not an individual relaxation.
652
653 RELAX_NOMACRO
654 Set in the first variant frag if the macro appeared in a .set nomacro
655 block and if one alternative requires a warning but the other does not.
656
657 RELAX_DELAY_SLOT
658 Like RELAX_NOMACRO, but indicates that the macro appears in a branch
659 delay slot.
4d7206a2
RS
660
661 The frag's "opcode" points to the first fixup for relaxable code.
662
663 Relaxable macros are generated using a sequence such as:
664
665 relax_start (SYMBOL);
666 ... generate first expansion ...
667 relax_switch ();
668 ... generate second expansion ...
669 relax_end ();
670
671 The code and fixups for the unwanted alternative are discarded
672 by md_convert_frag. */
584892a6 673#define RELAX_ENCODE(FIRST, SECOND) (((FIRST) << 8) | (SECOND))
4d7206a2 674
584892a6
RS
675#define RELAX_FIRST(X) (((X) >> 8) & 0xff)
676#define RELAX_SECOND(X) ((X) & 0xff)
677#define RELAX_USE_SECOND 0x10000
678#define RELAX_SECOND_LONGER 0x20000
679#define RELAX_NOMACRO 0x40000
680#define RELAX_DELAY_SLOT 0x80000
252b5132 681
4a6a3df4
AO
682/* Branch without likely bit. If label is out of range, we turn:
683
684 beq reg1, reg2, label
685 delay slot
686
687 into
688
689 bne reg1, reg2, 0f
690 nop
691 j label
692 0: delay slot
693
694 with the following opcode replacements:
695
696 beq <-> bne
697 blez <-> bgtz
698 bltz <-> bgez
699 bc1f <-> bc1t
700
701 bltzal <-> bgezal (with jal label instead of j label)
702
703 Even though keeping the delay slot instruction in the delay slot of
704 the branch would be more efficient, it would be very tricky to do
705 correctly, because we'd have to introduce a variable frag *after*
706 the delay slot instruction, and expand that instead. Let's do it
707 the easy way for now, even if the branch-not-taken case now costs
708 one additional instruction. Out-of-range branches are not supposed
709 to be common, anyway.
710
711 Branch likely. If label is out of range, we turn:
712
713 beql reg1, reg2, label
714 delay slot (annulled if branch not taken)
715
716 into
717
718 beql reg1, reg2, 1f
719 nop
720 beql $0, $0, 2f
721 nop
722 1: j[al] label
723 delay slot (executed only if branch taken)
724 2:
725
726 It would be possible to generate a shorter sequence by losing the
727 likely bit, generating something like:
b34976b6 728
4a6a3df4
AO
729 bne reg1, reg2, 0f
730 nop
731 j[al] label
732 delay slot (executed only if branch taken)
733 0:
734
735 beql -> bne
736 bnel -> beq
737 blezl -> bgtz
738 bgtzl -> blez
739 bltzl -> bgez
740 bgezl -> bltz
741 bc1fl -> bc1t
742 bc1tl -> bc1f
743
744 bltzall -> bgezal (with jal label instead of j label)
745 bgezall -> bltzal (ditto)
746
747
748 but it's not clear that it would actually improve performance. */
af6ae2ad 749#define RELAX_BRANCH_ENCODE(uncond, likely, link, toofar) \
4a6a3df4
AO
750 ((relax_substateT) \
751 (0xc0000000 \
752 | ((toofar) ? 1 : 0) \
753 | ((link) ? 2 : 0) \
754 | ((likely) ? 4 : 0) \
af6ae2ad 755 | ((uncond) ? 8 : 0)))
4a6a3df4 756#define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
4a6a3df4
AO
757#define RELAX_BRANCH_UNCOND(i) (((i) & 8) != 0)
758#define RELAX_BRANCH_LIKELY(i) (((i) & 4) != 0)
759#define RELAX_BRANCH_LINK(i) (((i) & 2) != 0)
ae6063d4 760#define RELAX_BRANCH_TOOFAR(i) (((i) & 1) != 0)
4a6a3df4 761
252b5132
RH
762/* For mips16 code, we use an entirely different form of relaxation.
763 mips16 supports two versions of most instructions which take
764 immediate values: a small one which takes some small value, and a
765 larger one which takes a 16 bit value. Since branches also follow
766 this pattern, relaxing these values is required.
767
768 We can assemble both mips16 and normal MIPS code in a single
769 object. Therefore, we need to support this type of relaxation at
770 the same time that we support the relaxation described above. We
771 use the high bit of the subtype field to distinguish these cases.
772
773 The information we store for this type of relaxation is the
774 argument code found in the opcode file for this relocation, whether
775 the user explicitly requested a small or extended form, and whether
776 the relocation is in a jump or jal delay slot. That tells us the
777 size of the value, and how it should be stored. We also store
778 whether the fragment is considered to be extended or not. We also
779 store whether this is known to be a branch to a different section,
780 whether we have tried to relax this frag yet, and whether we have
781 ever extended a PC relative fragment because of a shift count. */
782#define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
783 (0x80000000 \
784 | ((type) & 0xff) \
785 | ((small) ? 0x100 : 0) \
786 | ((ext) ? 0x200 : 0) \
787 | ((dslot) ? 0x400 : 0) \
788 | ((jal_dslot) ? 0x800 : 0))
4a6a3df4 789#define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
252b5132
RH
790#define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
791#define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
792#define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
793#define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
794#define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
795#define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
796#define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
797#define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
798#define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
799#define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
800#define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
885add95
CD
801
802/* Is the given value a sign-extended 32-bit value? */
803#define IS_SEXT_32BIT_NUM(x) \
804 (((x) &~ (offsetT) 0x7fffffff) == 0 \
805 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
806
807/* Is the given value a sign-extended 16-bit value? */
808#define IS_SEXT_16BIT_NUM(x) \
809 (((x) &~ (offsetT) 0x7fff) == 0 \
810 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
811
4d7206a2
RS
812\f
813/* Global variables used when generating relaxable macros. See the
814 comment above RELAX_ENCODE for more details about how relaxation
815 is used. */
816static struct {
817 /* 0 if we're not emitting a relaxable macro.
818 1 if we're emitting the first of the two relaxation alternatives.
819 2 if we're emitting the second alternative. */
820 int sequence;
821
822 /* The first relaxable fixup in the current frag. (In other words,
823 the first fixup that refers to relaxable code.) */
824 fixS *first_fixup;
825
826 /* sizes[0] says how many bytes of the first alternative are stored in
827 the current frag. Likewise sizes[1] for the second alternative. */
828 unsigned int sizes[2];
829
830 /* The symbol on which the choice of sequence depends. */
831 symbolS *symbol;
832} mips_relax;
252b5132 833\f
584892a6
RS
834/* Global variables used to decide whether a macro needs a warning. */
835static struct {
836 /* True if the macro is in a branch delay slot. */
837 bfd_boolean delay_slot_p;
838
839 /* For relaxable macros, sizes[0] is the length of the first alternative
840 in bytes and sizes[1] is the length of the second alternative.
841 For non-relaxable macros, both elements give the length of the
842 macro in bytes. */
843 unsigned int sizes[2];
844
845 /* The first variant frag for this macro. */
846 fragS *first_frag;
847} mips_macro_warning;
848\f
252b5132
RH
849/* Prototypes for static functions. */
850
17a2f251 851#define internalError() \
252b5132 852 as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
252b5132
RH
853
854enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
855
b34976b6 856static void append_insn
4d7206a2 857 (struct mips_cl_insn *ip, expressionS *p, bfd_reloc_code_real_type *r);
17a2f251 858static void mips_no_prev_insn (int);
b34976b6 859static void mips16_macro_build
67c0d1eb
RS
860 (expressionS *, const char *, const char *, va_list);
861static void load_register (int, expressionS *, int);
584892a6
RS
862static void macro_start (void);
863static void macro_end (void);
17a2f251
TS
864static void macro (struct mips_cl_insn * ip);
865static void mips16_macro (struct mips_cl_insn * ip);
252b5132 866#ifdef LOSING_COMPILER
17a2f251 867static void macro2 (struct mips_cl_insn * ip);
252b5132 868#endif
17a2f251
TS
869static void mips_ip (char *str, struct mips_cl_insn * ip);
870static void mips16_ip (char *str, struct mips_cl_insn * ip);
b34976b6 871static void mips16_immed
17a2f251
TS
872 (char *, unsigned int, int, offsetT, bfd_boolean, bfd_boolean, bfd_boolean,
873 unsigned long *, bfd_boolean *, unsigned short *);
5e0116d5 874static size_t my_getSmallExpression
17a2f251
TS
875 (expressionS *, bfd_reloc_code_real_type *, char *);
876static void my_getExpression (expressionS *, char *);
877static void s_align (int);
878static void s_change_sec (int);
879static void s_change_section (int);
880static void s_cons (int);
881static void s_float_cons (int);
882static void s_mips_globl (int);
883static void s_option (int);
884static void s_mipsset (int);
885static void s_abicalls (int);
886static void s_cpload (int);
887static void s_cpsetup (int);
888static void s_cplocal (int);
889static void s_cprestore (int);
890static void s_cpreturn (int);
891static void s_gpvalue (int);
892static void s_gpword (int);
893static void s_gpdword (int);
894static void s_cpadd (int);
895static void s_insn (int);
896static void md_obj_begin (void);
897static void md_obj_end (void);
898static void s_mips_ent (int);
899static void s_mips_end (int);
900static void s_mips_frame (int);
901static void s_mips_mask (int reg_type);
902static void s_mips_stab (int);
903static void s_mips_weakext (int);
904static void s_mips_file (int);
905static void s_mips_loc (int);
906static bfd_boolean pic_need_relax (symbolS *, asection *);
4a6a3df4 907static int relaxed_branch_length (fragS *, asection *, int);
17a2f251 908static int validate_mips_insn (const struct mips_opcode *);
e7af610e
NC
909
910/* Table and functions used to map between CPU/ISA names, and
911 ISA levels, and CPU numbers. */
912
e972090a
NC
913struct mips_cpu_info
914{
e7af610e
NC
915 const char *name; /* CPU or ISA name. */
916 int is_isa; /* Is this an ISA? (If 0, a CPU.) */
917 int isa; /* ISA level. */
918 int cpu; /* CPU number (default CPU if ISA). */
919};
920
17a2f251
TS
921static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
922static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
923static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
252b5132
RH
924\f
925/* Pseudo-op table.
926
927 The following pseudo-ops from the Kane and Heinrich MIPS book
928 should be defined here, but are currently unsupported: .alias,
929 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
930
931 The following pseudo-ops from the Kane and Heinrich MIPS book are
932 specific to the type of debugging information being generated, and
933 should be defined by the object format: .aent, .begin, .bend,
934 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
935 .vreg.
936
937 The following pseudo-ops from the Kane and Heinrich MIPS book are
938 not MIPS CPU specific, but are also not specific to the object file
939 format. This file is probably the best place to define them, but
940 they are not currently supported: .asm0, .endr, .lab, .repeat,
941 .struct. */
942
e972090a
NC
943static const pseudo_typeS mips_pseudo_table[] =
944{
beae10d5 945 /* MIPS specific pseudo-ops. */
252b5132
RH
946 {"option", s_option, 0},
947 {"set", s_mipsset, 0},
948 {"rdata", s_change_sec, 'r'},
949 {"sdata", s_change_sec, 's'},
950 {"livereg", s_ignore, 0},
951 {"abicalls", s_abicalls, 0},
952 {"cpload", s_cpload, 0},
6478892d
TS
953 {"cpsetup", s_cpsetup, 0},
954 {"cplocal", s_cplocal, 0},
252b5132 955 {"cprestore", s_cprestore, 0},
6478892d
TS
956 {"cpreturn", s_cpreturn, 0},
957 {"gpvalue", s_gpvalue, 0},
252b5132 958 {"gpword", s_gpword, 0},
10181a0d 959 {"gpdword", s_gpdword, 0},
252b5132
RH
960 {"cpadd", s_cpadd, 0},
961 {"insn", s_insn, 0},
962
beae10d5 963 /* Relatively generic pseudo-ops that happen to be used on MIPS
252b5132
RH
964 chips. */
965 {"asciiz", stringer, 1},
966 {"bss", s_change_sec, 'b'},
967 {"err", s_err, 0},
968 {"half", s_cons, 1},
969 {"dword", s_cons, 3},
970 {"weakext", s_mips_weakext, 0},
971
beae10d5 972 /* These pseudo-ops are defined in read.c, but must be overridden
252b5132
RH
973 here for one reason or another. */
974 {"align", s_align, 0},
975 {"byte", s_cons, 0},
976 {"data", s_change_sec, 'd'},
977 {"double", s_float_cons, 'd'},
978 {"float", s_float_cons, 'f'},
979 {"globl", s_mips_globl, 0},
980 {"global", s_mips_globl, 0},
981 {"hword", s_cons, 1},
982 {"int", s_cons, 2},
983 {"long", s_cons, 2},
984 {"octa", s_cons, 4},
985 {"quad", s_cons, 3},
cca86cc8 986 {"section", s_change_section, 0},
252b5132
RH
987 {"short", s_cons, 1},
988 {"single", s_float_cons, 'f'},
989 {"stabn", s_mips_stab, 'n'},
990 {"text", s_change_sec, 't'},
991 {"word", s_cons, 2},
add56521 992
add56521 993 { "extern", ecoff_directive_extern, 0},
add56521 994
43841e91 995 { NULL, NULL, 0 },
252b5132
RH
996};
997
e972090a
NC
998static const pseudo_typeS mips_nonecoff_pseudo_table[] =
999{
beae10d5
KH
1000 /* These pseudo-ops should be defined by the object file format.
1001 However, a.out doesn't support them, so we have versions here. */
252b5132
RH
1002 {"aent", s_mips_ent, 1},
1003 {"bgnb", s_ignore, 0},
1004 {"end", s_mips_end, 0},
1005 {"endb", s_ignore, 0},
1006 {"ent", s_mips_ent, 0},
c5dd6aab 1007 {"file", s_mips_file, 0},
252b5132
RH
1008 {"fmask", s_mips_mask, 'F'},
1009 {"frame", s_mips_frame, 0},
c5dd6aab 1010 {"loc", s_mips_loc, 0},
252b5132
RH
1011 {"mask", s_mips_mask, 'R'},
1012 {"verstamp", s_ignore, 0},
43841e91 1013 { NULL, NULL, 0 },
252b5132
RH
1014};
1015
17a2f251 1016extern void pop_insert (const pseudo_typeS *);
252b5132
RH
1017
1018void
17a2f251 1019mips_pop_insert (void)
252b5132
RH
1020{
1021 pop_insert (mips_pseudo_table);
1022 if (! ECOFF_DEBUGGING)
1023 pop_insert (mips_nonecoff_pseudo_table);
1024}
1025\f
1026/* Symbols labelling the current insn. */
1027
e972090a
NC
1028struct insn_label_list
1029{
252b5132
RH
1030 struct insn_label_list *next;
1031 symbolS *label;
1032};
1033
1034static struct insn_label_list *insn_labels;
1035static struct insn_label_list *free_insn_labels;
1036
17a2f251 1037static void mips_clear_insn_labels (void);
252b5132
RH
1038
1039static inline void
17a2f251 1040mips_clear_insn_labels (void)
252b5132
RH
1041{
1042 register struct insn_label_list **pl;
1043
1044 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1045 ;
1046 *pl = insn_labels;
1047 insn_labels = NULL;
1048}
1049\f
1050static char *expr_end;
1051
1052/* Expressions which appear in instructions. These are set by
1053 mips_ip. */
1054
1055static expressionS imm_expr;
5f74bc13 1056static expressionS imm2_expr;
252b5132
RH
1057static expressionS offset_expr;
1058
1059/* Relocs associated with imm_expr and offset_expr. */
1060
f6688943
TS
1061static bfd_reloc_code_real_type imm_reloc[3]
1062 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1063static bfd_reloc_code_real_type offset_reloc[3]
1064 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 1065
252b5132
RH
1066/* These are set by mips16_ip if an explicit extension is used. */
1067
b34976b6 1068static bfd_boolean mips16_small, mips16_ext;
252b5132 1069
7ed4a06a 1070#ifdef OBJ_ELF
ecb4347a
DJ
1071/* The pdr segment for per procedure frame/regmask info. Not used for
1072 ECOFF debugging. */
252b5132
RH
1073
1074static segT pdr_seg;
7ed4a06a 1075#endif
252b5132 1076
e013f690
TS
1077/* The default target format to use. */
1078
1079const char *
17a2f251 1080mips_target_format (void)
e013f690
TS
1081{
1082 switch (OUTPUT_FLAVOR)
1083 {
e013f690
TS
1084 case bfd_target_ecoff_flavour:
1085 return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
1086 case bfd_target_coff_flavour:
1087 return "pe-mips";
1088 case bfd_target_elf_flavour:
1089#ifdef TE_TMIPS
cfe86eaa 1090 /* This is traditional mips. */
e013f690 1091 return (target_big_endian
cfe86eaa
TS
1092 ? (HAVE_64BIT_OBJECTS
1093 ? "elf64-tradbigmips"
1094 : (HAVE_NEWABI
1095 ? "elf32-ntradbigmips" : "elf32-tradbigmips"))
1096 : (HAVE_64BIT_OBJECTS
1097 ? "elf64-tradlittlemips"
1098 : (HAVE_NEWABI
1099 ? "elf32-ntradlittlemips" : "elf32-tradlittlemips")));
e013f690
TS
1100#else
1101 return (target_big_endian
cfe86eaa
TS
1102 ? (HAVE_64BIT_OBJECTS
1103 ? "elf64-bigmips"
1104 : (HAVE_NEWABI
1105 ? "elf32-nbigmips" : "elf32-bigmips"))
1106 : (HAVE_64BIT_OBJECTS
1107 ? "elf64-littlemips"
1108 : (HAVE_NEWABI
1109 ? "elf32-nlittlemips" : "elf32-littlemips")));
e013f690
TS
1110#endif
1111 default:
1112 abort ();
1113 return NULL;
1114 }
1115}
1116
156c2f8b
NC
1117/* This function is called once, at assembler startup time. It should
1118 set up all the tables, etc. that the MD part of the assembler will need. */
1119
252b5132 1120void
17a2f251 1121md_begin (void)
252b5132 1122{
252b5132 1123 register const char *retval = NULL;
156c2f8b 1124 int i = 0;
252b5132 1125 int broken = 0;
1f25f5d3 1126
fef14a42 1127 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_arch))
252b5132
RH
1128 as_warn (_("Could not set architecture and machine"));
1129
252b5132
RH
1130 op_hash = hash_new ();
1131
1132 for (i = 0; i < NUMOPCODES;)
1133 {
1134 const char *name = mips_opcodes[i].name;
1135
17a2f251 1136 retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
252b5132
RH
1137 if (retval != NULL)
1138 {
1139 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1140 mips_opcodes[i].name, retval);
1141 /* Probably a memory allocation problem? Give up now. */
1142 as_fatal (_("Broken assembler. No assembly attempted."));
1143 }
1144 do
1145 {
1146 if (mips_opcodes[i].pinfo != INSN_MACRO)
1147 {
1148 if (!validate_mips_insn (&mips_opcodes[i]))
1149 broken = 1;
1150 }
1151 ++i;
1152 }
1153 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1154 }
1155
1156 mips16_op_hash = hash_new ();
1157
1158 i = 0;
1159 while (i < bfd_mips16_num_opcodes)
1160 {
1161 const char *name = mips16_opcodes[i].name;
1162
17a2f251 1163 retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
252b5132
RH
1164 if (retval != NULL)
1165 as_fatal (_("internal: can't hash `%s': %s"),
1166 mips16_opcodes[i].name, retval);
1167 do
1168 {
1169 if (mips16_opcodes[i].pinfo != INSN_MACRO
1170 && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1171 != mips16_opcodes[i].match))
1172 {
1173 fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1174 mips16_opcodes[i].name, mips16_opcodes[i].args);
1175 broken = 1;
1176 }
1177 ++i;
1178 }
1179 while (i < bfd_mips16_num_opcodes
1180 && strcmp (mips16_opcodes[i].name, name) == 0);
1181 }
1182
1183 if (broken)
1184 as_fatal (_("Broken assembler. No assembly attempted."));
1185
1186 /* We add all the general register names to the symbol table. This
1187 helps us detect invalid uses of them. */
1188 for (i = 0; i < 32; i++)
1189 {
1190 char buf[5];
1191
1192 sprintf (buf, "$%d", i);
1193 symbol_table_insert (symbol_new (buf, reg_section, i,
1194 &zero_address_frag));
1195 }
76db943d
TS
1196 symbol_table_insert (symbol_new ("$ra", reg_section, RA,
1197 &zero_address_frag));
252b5132
RH
1198 symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1199 &zero_address_frag));
1200 symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1201 &zero_address_frag));
1202 symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1203 &zero_address_frag));
1204 symbol_table_insert (symbol_new ("$at", reg_section, AT,
1205 &zero_address_frag));
1206 symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1207 &zero_address_frag));
1208 symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1209 &zero_address_frag));
85b51719
TS
1210 symbol_table_insert (symbol_new ("$zero", reg_section, ZERO,
1211 &zero_address_frag));
252b5132
RH
1212 symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1213 &zero_address_frag));
1214
6047c971
AO
1215 /* If we don't add these register names to the symbol table, they
1216 may end up being added as regular symbols by operand(), and then
1217 make it to the object file as undefined in case they're not
1218 regarded as local symbols. They're local in o32, since `$' is a
1219 local symbol prefix, but not in n32 or n64. */
1220 for (i = 0; i < 8; i++)
1221 {
1222 char buf[6];
1223
1224 sprintf (buf, "$fcc%i", i);
1225 symbol_table_insert (symbol_new (buf, reg_section, -1,
1226 &zero_address_frag));
1227 }
1228
b34976b6 1229 mips_no_prev_insn (FALSE);
252b5132
RH
1230
1231 mips_gprmask = 0;
1232 mips_cprmask[0] = 0;
1233 mips_cprmask[1] = 0;
1234 mips_cprmask[2] = 0;
1235 mips_cprmask[3] = 0;
1236
1237 /* set the default alignment for the text section (2**2) */
1238 record_alignment (text_section, 2);
1239
4d0d148d 1240 bfd_set_gp_size (stdoutput, g_switch_value);
252b5132
RH
1241
1242 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1243 {
1244 /* On a native system, sections must be aligned to 16 byte
1245 boundaries. When configured for an embedded ELF target, we
1246 don't bother. */
1247 if (strcmp (TARGET_OS, "elf") != 0)
1248 {
1249 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1250 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1251 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1252 }
1253
1254 /* Create a .reginfo section for register masks and a .mdebug
1255 section for debugging information. */
1256 {
1257 segT seg;
1258 subsegT subseg;
1259 flagword flags;
1260 segT sec;
1261
1262 seg = now_seg;
1263 subseg = now_subseg;
1264
1265 /* The ABI says this section should be loaded so that the
1266 running program can access it. However, we don't load it
1267 if we are configured for an embedded target */
1268 flags = SEC_READONLY | SEC_DATA;
1269 if (strcmp (TARGET_OS, "elf") != 0)
1270 flags |= SEC_ALLOC | SEC_LOAD;
1271
316f5878 1272 if (mips_abi != N64_ABI)
252b5132
RH
1273 {
1274 sec = subseg_new (".reginfo", (subsegT) 0);
1275
195325d2
TS
1276 bfd_set_section_flags (stdoutput, sec, flags);
1277 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
bdaaa2e1 1278
252b5132
RH
1279#ifdef OBJ_ELF
1280 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1281#endif
1282 }
1283 else
1284 {
1285 /* The 64-bit ABI uses a .MIPS.options section rather than
1286 .reginfo section. */
1287 sec = subseg_new (".MIPS.options", (subsegT) 0);
195325d2
TS
1288 bfd_set_section_flags (stdoutput, sec, flags);
1289 bfd_set_section_alignment (stdoutput, sec, 3);
252b5132
RH
1290
1291#ifdef OBJ_ELF
1292 /* Set up the option header. */
1293 {
1294 Elf_Internal_Options opthdr;
1295 char *f;
1296
1297 opthdr.kind = ODK_REGINFO;
1298 opthdr.size = (sizeof (Elf_External_Options)
1299 + sizeof (Elf64_External_RegInfo));
1300 opthdr.section = 0;
1301 opthdr.info = 0;
1302 f = frag_more (sizeof (Elf_External_Options));
1303 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1304 (Elf_External_Options *) f);
1305
1306 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1307 }
1308#endif
1309 }
1310
1311 if (ECOFF_DEBUGGING)
1312 {
1313 sec = subseg_new (".mdebug", (subsegT) 0);
1314 (void) bfd_set_section_flags (stdoutput, sec,
1315 SEC_HAS_CONTENTS | SEC_READONLY);
1316 (void) bfd_set_section_alignment (stdoutput, sec, 2);
1317 }
ecb4347a 1318#ifdef OBJ_ELF
dcd410fe 1319 else if (OUTPUT_FLAVOR == bfd_target_elf_flavour && mips_flag_pdr)
ecb4347a
DJ
1320 {
1321 pdr_seg = subseg_new (".pdr", (subsegT) 0);
1322 (void) bfd_set_section_flags (stdoutput, pdr_seg,
1323 SEC_READONLY | SEC_RELOC
1324 | SEC_DEBUGGING);
1325 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1326 }
252b5132
RH
1327#endif
1328
1329 subseg_set (seg, subseg);
1330 }
1331 }
1332
1333 if (! ECOFF_DEBUGGING)
1334 md_obj_begin ();
1335}
1336
1337void
17a2f251 1338md_mips_end (void)
252b5132
RH
1339{
1340 if (! ECOFF_DEBUGGING)
1341 md_obj_end ();
1342}
1343
1344void
17a2f251 1345md_assemble (char *str)
252b5132
RH
1346{
1347 struct mips_cl_insn insn;
f6688943
TS
1348 bfd_reloc_code_real_type unused_reloc[3]
1349 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132
RH
1350
1351 imm_expr.X_op = O_absent;
5f74bc13 1352 imm2_expr.X_op = O_absent;
252b5132 1353 offset_expr.X_op = O_absent;
f6688943
TS
1354 imm_reloc[0] = BFD_RELOC_UNUSED;
1355 imm_reloc[1] = BFD_RELOC_UNUSED;
1356 imm_reloc[2] = BFD_RELOC_UNUSED;
1357 offset_reloc[0] = BFD_RELOC_UNUSED;
1358 offset_reloc[1] = BFD_RELOC_UNUSED;
1359 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
1360
1361 if (mips_opts.mips16)
1362 mips16_ip (str, &insn);
1363 else
1364 {
1365 mips_ip (str, &insn);
beae10d5
KH
1366 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1367 str, insn.insn_opcode));
252b5132
RH
1368 }
1369
1370 if (insn_error)
1371 {
1372 as_bad ("%s `%s'", insn_error, str);
1373 return;
1374 }
1375
1376 if (insn.insn_mo->pinfo == INSN_MACRO)
1377 {
584892a6 1378 macro_start ();
252b5132
RH
1379 if (mips_opts.mips16)
1380 mips16_macro (&insn);
1381 else
1382 macro (&insn);
584892a6 1383 macro_end ();
252b5132
RH
1384 }
1385 else
1386 {
1387 if (imm_expr.X_op != O_absent)
4d7206a2 1388 append_insn (&insn, &imm_expr, imm_reloc);
252b5132 1389 else if (offset_expr.X_op != O_absent)
4d7206a2 1390 append_insn (&insn, &offset_expr, offset_reloc);
252b5132 1391 else
4d7206a2 1392 append_insn (&insn, NULL, unused_reloc);
252b5132
RH
1393 }
1394}
1395
5919d012
RS
1396/* Return true if the given relocation might need a matching %lo().
1397 Note that R_MIPS_GOT16 relocations only need a matching %lo() when
1398 applied to local symbols. */
1399
1400static inline bfd_boolean
17a2f251 1401reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
5919d012
RS
1402{
1403 return (reloc == BFD_RELOC_HI16_S
1404 || reloc == BFD_RELOC_MIPS_GOT16);
1405}
1406
1407/* Return true if the given fixup is followed by a matching R_MIPS_LO16
1408 relocation. */
1409
1410static inline bfd_boolean
17a2f251 1411fixup_has_matching_lo_p (fixS *fixp)
5919d012
RS
1412{
1413 return (fixp->fx_next != NULL
1414 && fixp->fx_next->fx_r_type == BFD_RELOC_LO16
1415 && fixp->fx_addsy == fixp->fx_next->fx_addsy
1416 && fixp->fx_offset == fixp->fx_next->fx_offset);
1417}
1418
252b5132
RH
1419/* See whether instruction IP reads register REG. CLASS is the type
1420 of register. */
1421
1422static int
17a2f251
TS
1423insn_uses_reg (struct mips_cl_insn *ip, unsigned int reg,
1424 enum mips_regclass class)
252b5132
RH
1425{
1426 if (class == MIPS16_REG)
1427 {
1428 assert (mips_opts.mips16);
1429 reg = mips16_to_32_reg_map[reg];
1430 class = MIPS_GR_REG;
1431 }
1432
85b51719
TS
1433 /* Don't report on general register ZERO, since it never changes. */
1434 if (class == MIPS_GR_REG && reg == ZERO)
252b5132
RH
1435 return 0;
1436
1437 if (class == MIPS_FP_REG)
1438 {
1439 assert (! mips_opts.mips16);
1440 /* If we are called with either $f0 or $f1, we must check $f0.
1441 This is not optimal, because it will introduce an unnecessary
1442 NOP between "lwc1 $f0" and "swc1 $f1". To fix this we would
1443 need to distinguish reading both $f0 and $f1 or just one of
1444 them. Note that we don't have to check the other way,
1445 because there is no instruction that sets both $f0 and $f1
1446 and requires a delay. */
1447 if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1448 && ((((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS) &~(unsigned)1)
1449 == (reg &~ (unsigned) 1)))
1450 return 1;
1451 if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1452 && ((((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT) &~(unsigned)1)
1453 == (reg &~ (unsigned) 1)))
1454 return 1;
1455 }
1456 else if (! mips_opts.mips16)
1457 {
1458 if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1459 && ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == reg)
1460 return 1;
1461 if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1462 && ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT) == reg)
1463 return 1;
1464 }
1465 else
1466 {
1467 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1468 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RX)
1469 & MIPS16OP_MASK_RX)]
1470 == reg))
1471 return 1;
1472 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1473 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RY)
1474 & MIPS16OP_MASK_RY)]
1475 == reg))
1476 return 1;
1477 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1478 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
1479 & MIPS16OP_MASK_MOVE32Z)]
1480 == reg))
1481 return 1;
1482 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1483 return 1;
1484 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1485 return 1;
1486 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1487 return 1;
1488 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1489 && ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
1490 & MIPS16OP_MASK_REGR32) == reg)
1491 return 1;
1492 }
1493
1494 return 0;
1495}
1496
1497/* This function returns true if modifying a register requires a
1498 delay. */
1499
1500static int
17a2f251 1501reg_needs_delay (unsigned int reg)
252b5132
RH
1502{
1503 unsigned long prev_pinfo;
1504
1505 prev_pinfo = prev_insn.insn_mo->pinfo;
1506 if (! mips_opts.noreorder
81912461
ILT
1507 && (((prev_pinfo & INSN_LOAD_MEMORY_DELAY)
1508 && ! gpr_interlocks)
1509 || ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1510 && ! cop_interlocks)))
252b5132 1511 {
81912461
ILT
1512 /* A load from a coprocessor or from memory. All load delays
1513 delay the use of general register rt for one instruction. */
bdaaa2e1 1514 /* Itbl support may require additional care here. */
252b5132
RH
1515 know (prev_pinfo & INSN_WRITE_GPR_T);
1516 if (reg == ((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT))
1517 return 1;
1518 }
1519
1520 return 0;
1521}
1522
1523/* Mark instruction labels in mips16 mode. This permits the linker to
1524 handle them specially, such as generating jalx instructions when
1525 needed. We also make them odd for the duration of the assembly, in
1526 order to generate the right sort of code. We will make them even
1527 in the adjust_symtab routine, while leaving them marked. This is
1528 convenient for the debugger and the disassembler. The linker knows
1529 to make them odd again. */
1530
1531static void
17a2f251 1532mips16_mark_labels (void)
252b5132
RH
1533{
1534 if (mips_opts.mips16)
1535 {
1536 struct insn_label_list *l;
98aa84af 1537 valueT val;
252b5132
RH
1538
1539 for (l = insn_labels; l != NULL; l = l->next)
1540 {
1541#ifdef OBJ_ELF
1542 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1543 S_SET_OTHER (l->label, STO_MIPS16);
1544#endif
98aa84af
AM
1545 val = S_GET_VALUE (l->label);
1546 if ((val & 1) == 0)
1547 S_SET_VALUE (l->label, val + 1);
252b5132
RH
1548 }
1549 }
1550}
1551
4d7206a2
RS
1552/* End the current frag. Make it a variant frag and record the
1553 relaxation info. */
1554
1555static void
1556relax_close_frag (void)
1557{
584892a6 1558 mips_macro_warning.first_frag = frag_now;
4d7206a2 1559 frag_var (rs_machine_dependent, 0, 0,
584892a6 1560 RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1]),
4d7206a2
RS
1561 mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
1562
1563 memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
1564 mips_relax.first_fixup = 0;
1565}
1566
1567/* Start a new relaxation sequence whose expansion depends on SYMBOL.
1568 See the comment above RELAX_ENCODE for more details. */
1569
1570static void
1571relax_start (symbolS *symbol)
1572{
1573 assert (mips_relax.sequence == 0);
1574 mips_relax.sequence = 1;
1575 mips_relax.symbol = symbol;
1576}
1577
1578/* Start generating the second version of a relaxable sequence.
1579 See the comment above RELAX_ENCODE for more details. */
252b5132
RH
1580
1581static void
4d7206a2
RS
1582relax_switch (void)
1583{
1584 assert (mips_relax.sequence == 1);
1585 mips_relax.sequence = 2;
1586}
1587
1588/* End the current relaxable sequence. */
1589
1590static void
1591relax_end (void)
1592{
1593 assert (mips_relax.sequence == 2);
1594 relax_close_frag ();
1595 mips_relax.sequence = 0;
1596}
1597
1598/* Output an instruction. IP is the instruction information.
1599 ADDRESS_EXPR is an operand of the instruction to be used with
1600 RELOC_TYPE. */
1601
1602static void
1603append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
17a2f251 1604 bfd_reloc_code_real_type *reloc_type)
252b5132
RH
1605{
1606 register unsigned long prev_pinfo, pinfo;
1607 char *f;
f6688943 1608 fixS *fixp[3];
252b5132 1609 int nops = 0;
895921c9
MR
1610 relax_stateT prev_insn_frag_type = 0;
1611 bfd_boolean relaxed_branch = FALSE;
f5040a92 1612 bfd_boolean force_new_frag = FALSE;
252b5132
RH
1613
1614 /* Mark instruction labels in mips16 mode. */
f9419b05 1615 mips16_mark_labels ();
252b5132
RH
1616
1617 prev_pinfo = prev_insn.insn_mo->pinfo;
1618 pinfo = ip->insn_mo->pinfo;
1619
4d7206a2
RS
1620 if (mips_relax.sequence != 2
1621 && (!mips_opts.noreorder || prev_nop_frag != NULL))
252b5132
RH
1622 {
1623 int prev_prev_nop;
1624
1625 /* If the previous insn required any delay slots, see if we need
1626 to insert a NOP or two. There are eight kinds of possible
1627 hazards, of which an instruction can have at most one type.
1628 (1) a load from memory delay
1629 (2) a load from a coprocessor delay
1630 (3) an unconditional branch delay
1631 (4) a conditional branch delay
1632 (5) a move to coprocessor register delay
1633 (6) a load coprocessor register from memory delay
1634 (7) a coprocessor condition code delay
1635 (8) a HI/LO special register delay
1636
1637 There are a lot of optimizations we could do that we don't.
1638 In particular, we do not, in general, reorder instructions.
1639 If you use gcc with optimization, it will reorder
1640 instructions and generally do much more optimization then we
1641 do here; repeating all that work in the assembler would only
1642 benefit hand written assembly code, and does not seem worth
1643 it. */
1644
1645 /* This is how a NOP is emitted. */
1646#define emit_nop() \
1647 (mips_opts.mips16 \
1648 ? md_number_to_chars (frag_more (2), 0x6500, 2) \
1649 : md_number_to_chars (frag_more (4), 0, 4))
1650
1651 /* The previous insn might require a delay slot, depending upon
1652 the contents of the current insn. */
1653 if (! mips_opts.mips16
81912461
ILT
1654 && (((prev_pinfo & INSN_LOAD_MEMORY_DELAY)
1655 && ! gpr_interlocks)
1656 || ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1657 && ! cop_interlocks)))
252b5132
RH
1658 {
1659 /* A load from a coprocessor or from memory. All load
1660 delays delay the use of general register rt for one
81912461 1661 instruction. */
beae10d5 1662 /* Itbl support may require additional care here. */
252b5132
RH
1663 know (prev_pinfo & INSN_WRITE_GPR_T);
1664 if (mips_optimize == 0
1665 || insn_uses_reg (ip,
1666 ((prev_insn.insn_opcode >> OP_SH_RT)
1667 & OP_MASK_RT),
1668 MIPS_GR_REG))
1669 ++nops;
1670 }
1671 else if (! mips_opts.mips16
252b5132 1672 && (((prev_pinfo & INSN_COPROC_MOVE_DELAY)
beae10d5 1673 && ! cop_interlocks)
81912461
ILT
1674 || ((prev_pinfo & INSN_COPROC_MEMORY_DELAY)
1675 && ! cop_mem_interlocks)))
252b5132
RH
1676 {
1677 /* A generic coprocessor delay. The previous instruction
1678 modified a coprocessor general or control register. If
1679 it modified a control register, we need to avoid any
1680 coprocessor instruction (this is probably not always
1681 required, but it sometimes is). If it modified a general
1682 register, we avoid using that register.
1683
252b5132
RH
1684 This case is not handled very well. There is no special
1685 knowledge of CP0 handling, and the coprocessors other
1686 than the floating point unit are not distinguished at
1687 all. */
1688 /* Itbl support may require additional care here. FIXME!
bdaaa2e1 1689 Need to modify this to include knowledge about
252b5132
RH
1690 user specified delays! */
1691 if (prev_pinfo & INSN_WRITE_FPR_T)
1692 {
1693 if (mips_optimize == 0
1694 || insn_uses_reg (ip,
1695 ((prev_insn.insn_opcode >> OP_SH_FT)
1696 & OP_MASK_FT),
1697 MIPS_FP_REG))
1698 ++nops;
1699 }
1700 else if (prev_pinfo & INSN_WRITE_FPR_S)
1701 {
1702 if (mips_optimize == 0
1703 || insn_uses_reg (ip,
1704 ((prev_insn.insn_opcode >> OP_SH_FS)
1705 & OP_MASK_FS),
1706 MIPS_FP_REG))
1707 ++nops;
1708 }
1709 else
1710 {
1711 /* We don't know exactly what the previous instruction
1712 does. If the current instruction uses a coprocessor
1713 register, we must insert a NOP. If previous
1714 instruction may set the condition codes, and the
1715 current instruction uses them, we must insert two
1716 NOPS. */
bdaaa2e1 1717 /* Itbl support may require additional care here. */
252b5132
RH
1718 if (mips_optimize == 0
1719 || ((prev_pinfo & INSN_WRITE_COND_CODE)
1720 && (pinfo & INSN_READ_COND_CODE)))
1721 nops += 2;
1722 else if (pinfo & INSN_COP)
1723 ++nops;
1724 }
1725 }
1726 else if (! mips_opts.mips16
252b5132
RH
1727 && (prev_pinfo & INSN_WRITE_COND_CODE)
1728 && ! cop_interlocks)
1729 {
1730 /* The previous instruction sets the coprocessor condition
1731 codes, but does not require a general coprocessor delay
1732 (this means it is a floating point comparison
1733 instruction). If this instruction uses the condition
1734 codes, we need to insert a single NOP. */
beae10d5 1735 /* Itbl support may require additional care here. */
252b5132
RH
1736 if (mips_optimize == 0
1737 || (pinfo & INSN_READ_COND_CODE))
1738 ++nops;
1739 }
6b76fefe
CM
1740
1741 /* If we're fixing up mfhi/mflo for the r7000 and the
1742 previous insn was an mfhi/mflo and the current insn
1743 reads the register that the mfhi/mflo wrote to, then
1744 insert two nops. */
1745
1746 else if (mips_7000_hilo_fix
1747 && MF_HILO_INSN (prev_pinfo)
1748 && insn_uses_reg (ip, ((prev_insn.insn_opcode >> OP_SH_RD)
beae10d5
KH
1749 & OP_MASK_RD),
1750 MIPS_GR_REG))
6b76fefe
CM
1751 {
1752 nops += 2;
1753 }
1754
1755 /* If we're fixing up mfhi/mflo for the r7000 and the
1756 2nd previous insn was an mfhi/mflo and the current insn
1757 reads the register that the mfhi/mflo wrote to, then
1758 insert one nop. */
1759
1760 else if (mips_7000_hilo_fix
1761 && MF_HILO_INSN (prev_prev_insn.insn_opcode)
1762 && insn_uses_reg (ip, ((prev_prev_insn.insn_opcode >> OP_SH_RD)
1763 & OP_MASK_RD),
1764 MIPS_GR_REG))
bdaaa2e1 1765
6b76fefe 1766 {
f9419b05 1767 ++nops;
6b76fefe 1768 }
bdaaa2e1 1769
252b5132
RH
1770 else if (prev_pinfo & INSN_READ_LO)
1771 {
1772 /* The previous instruction reads the LO register; if the
1773 current instruction writes to the LO register, we must
bdaaa2e1 1774 insert two NOPS. Some newer processors have interlocks.
67c1ffbe
KH
1775 Also the tx39's multiply instructions can be executed
1776 immediately after a read from HI/LO (without the delay),
bdaaa2e1
KH
1777 though the tx39's divide insns still do require the
1778 delay. */
252b5132 1779 if (! (hilo_interlocks
048cdf86 1780 || (mips_opts.arch == CPU_R3900 && (pinfo & INSN_MULT)))
252b5132
RH
1781 && (mips_optimize == 0
1782 || (pinfo & INSN_WRITE_LO)))
1783 nops += 2;
1784 /* Most mips16 branch insns don't have a delay slot.
1785 If a read from LO is immediately followed by a branch
1786 to a write to LO we have a read followed by a write
1787 less than 2 insns away. We assume the target of
1788 a branch might be a write to LO, and insert a nop
bdaaa2e1 1789 between a read and an immediately following branch. */
252b5132
RH
1790 else if (mips_opts.mips16
1791 && (mips_optimize == 0
1792 || (pinfo & MIPS16_INSN_BRANCH)))
f9419b05 1793 ++nops;
252b5132
RH
1794 }
1795 else if (prev_insn.insn_mo->pinfo & INSN_READ_HI)
1796 {
1797 /* The previous instruction reads the HI register; if the
1798 current instruction writes to the HI register, we must
1799 insert a NOP. Some newer processors have interlocks.
bdaaa2e1 1800 Also the note tx39's multiply above. */
252b5132 1801 if (! (hilo_interlocks
048cdf86 1802 || (mips_opts.arch == CPU_R3900 && (pinfo & INSN_MULT)))
252b5132
RH
1803 && (mips_optimize == 0
1804 || (pinfo & INSN_WRITE_HI)))
1805 nops += 2;
1806 /* Most mips16 branch insns don't have a delay slot.
1807 If a read from HI is immediately followed by a branch
1808 to a write to HI we have a read followed by a write
1809 less than 2 insns away. We assume the target of
1810 a branch might be a write to HI, and insert a nop
bdaaa2e1 1811 between a read and an immediately following branch. */
252b5132
RH
1812 else if (mips_opts.mips16
1813 && (mips_optimize == 0
1814 || (pinfo & MIPS16_INSN_BRANCH)))
f9419b05 1815 ++nops;
252b5132
RH
1816 }
1817
1818 /* If the previous instruction was in a noreorder section, then
1819 we don't want to insert the nop after all. */
bdaaa2e1 1820 /* Itbl support may require additional care here. */
252b5132
RH
1821 if (prev_insn_unreordered)
1822 nops = 0;
1823
1824 /* There are two cases which require two intervening
1825 instructions: 1) setting the condition codes using a move to
1826 coprocessor instruction which requires a general coprocessor
1827 delay and then reading the condition codes 2) reading the HI
1828 or LO register and then writing to it (except on processors
1829 which have interlocks). If we are not already emitting a NOP
1830 instruction, we must check for these cases compared to the
1831 instruction previous to the previous instruction. */
1832 if ((! mips_opts.mips16
252b5132
RH
1833 && (prev_prev_insn.insn_mo->pinfo & INSN_COPROC_MOVE_DELAY)
1834 && (prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
1835 && (pinfo & INSN_READ_COND_CODE)
1836 && ! cop_interlocks)
1837 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_LO)
1838 && (pinfo & INSN_WRITE_LO)
1839 && ! (hilo_interlocks
048cdf86 1840 || (mips_opts.arch == CPU_R3900 && (pinfo & INSN_MULT))))
252b5132
RH
1841 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
1842 && (pinfo & INSN_WRITE_HI)
1843 && ! (hilo_interlocks
048cdf86 1844 || (mips_opts.arch == CPU_R3900 && (pinfo & INSN_MULT)))))
252b5132
RH
1845 prev_prev_nop = 1;
1846 else
1847 prev_prev_nop = 0;
1848
1849 if (prev_prev_insn_unreordered)
1850 prev_prev_nop = 0;
1851
1852 if (prev_prev_nop && nops == 0)
1853 ++nops;
1854
d766e8ec 1855 if (mips_fix_vr4120 && prev_insn.insn_mo->name)
60b63b72
RS
1856 {
1857 /* We're out of bits in pinfo, so we must resort to string
1858 ops here. Shortcuts are selected based on opcodes being
d766e8ec 1859 limited to the VR4120 instruction set. */
60b63b72
RS
1860 int min_nops = 0;
1861 const char *pn = prev_insn.insn_mo->name;
1862 const char *tn = ip->insn_mo->name;
1863 if (strncmp(pn, "macc", 4) == 0
1864 || strncmp(pn, "dmacc", 5) == 0)
1865 {
1866 /* Errata 21 - [D]DIV[U] after [D]MACC */
1867 if (strstr (tn, "div"))
1868 {
1869 min_nops = 1;
1870 }
1871
1872 /* Errata 23 - Continuous DMULT[U]/DMACC instructions */
1873 if (pn[0] == 'd' /* dmacc */
1874 && (strncmp(tn, "dmult", 5) == 0
1875 || strncmp(tn, "dmacc", 5) == 0))
1876 {
1877 min_nops = 1;
1878 }
1879
1880 /* Errata 24 - MT{LO,HI} after [D]MACC */
1881 if (strcmp (tn, "mtlo") == 0
1882 || strcmp (tn, "mthi") == 0)
1883 {
1884 min_nops = 1;
1885 }
1886
1887 }
1888 else if (strncmp(pn, "dmult", 5) == 0
1889 && (strncmp(tn, "dmult", 5) == 0
1890 || strncmp(tn, "dmacc", 5) == 0))
1891 {
1892 /* Here is the rest of errata 23. */
1893 min_nops = 1;
1894 }
1895 if (nops < min_nops)
1896 nops = min_nops;
1897 }
1898
252b5132
RH
1899 /* If we are being given a nop instruction, don't bother with
1900 one of the nops we would otherwise output. This will only
1901 happen when a nop instruction is used with mips_optimize set
1902 to 0. */
1903 if (nops > 0
1904 && ! mips_opts.noreorder
156c2f8b 1905 && ip->insn_opcode == (unsigned) (mips_opts.mips16 ? 0x6500 : 0))
252b5132
RH
1906 --nops;
1907
1908 /* Now emit the right number of NOP instructions. */
1909 if (nops > 0 && ! mips_opts.noreorder)
1910 {
1911 fragS *old_frag;
1912 unsigned long old_frag_offset;
1913 int i;
1914 struct insn_label_list *l;
1915
1916 old_frag = frag_now;
1917 old_frag_offset = frag_now_fix ();
1918
1919 for (i = 0; i < nops; i++)
1920 emit_nop ();
1921
1922 if (listing)
1923 {
1924 listing_prev_line ();
1925 /* We may be at the start of a variant frag. In case we
1926 are, make sure there is enough space for the frag
1927 after the frags created by listing_prev_line. The
1928 argument to frag_grow here must be at least as large
1929 as the argument to all other calls to frag_grow in
1930 this file. We don't have to worry about being in the
1931 middle of a variant frag, because the variants insert
1932 all needed nop instructions themselves. */
1933 frag_grow (40);
1934 }
1935
1936 for (l = insn_labels; l != NULL; l = l->next)
1937 {
98aa84af
AM
1938 valueT val;
1939
252b5132 1940 assert (S_GET_SEGMENT (l->label) == now_seg);
49309057 1941 symbol_set_frag (l->label, frag_now);
98aa84af 1942 val = (valueT) frag_now_fix ();
252b5132
RH
1943 /* mips16 text labels are stored as odd. */
1944 if (mips_opts.mips16)
f9419b05 1945 ++val;
98aa84af 1946 S_SET_VALUE (l->label, val);
252b5132
RH
1947 }
1948
1949#ifndef NO_ECOFF_DEBUGGING
1950 if (ECOFF_DEBUGGING)
1951 ecoff_fix_loc (old_frag, old_frag_offset);
1952#endif
1953 }
1954 else if (prev_nop_frag != NULL)
1955 {
1956 /* We have a frag holding nops we may be able to remove. If
1957 we don't need any nops, we can decrease the size of
1958 prev_nop_frag by the size of one instruction. If we do
bdaaa2e1 1959 need some nops, we count them in prev_nops_required. */
252b5132
RH
1960 if (prev_nop_frag_since == 0)
1961 {
1962 if (nops == 0)
1963 {
1964 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1965 --prev_nop_frag_holds;
1966 }
1967 else
1968 prev_nop_frag_required += nops;
1969 }
1970 else
1971 {
1972 if (prev_prev_nop == 0)
1973 {
1974 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1975 --prev_nop_frag_holds;
1976 }
1977 else
1978 ++prev_nop_frag_required;
1979 }
1980
1981 if (prev_nop_frag_holds <= prev_nop_frag_required)
1982 prev_nop_frag = NULL;
1983
1984 ++prev_nop_frag_since;
1985
1986 /* Sanity check: by the time we reach the second instruction
1987 after prev_nop_frag, we should have used up all the nops
1988 one way or another. */
1989 assert (prev_nop_frag_since <= 1 || prev_nop_frag == NULL);
1990 }
1991 }
1992
895921c9
MR
1993 /* Record the frag type before frag_var. */
1994 if (prev_insn_frag)
1995 prev_insn_frag_type = prev_insn_frag->fr_type;
1996
4d7206a2 1997 if (address_expr
0b25d3e6 1998 && *reloc_type == BFD_RELOC_16_PCREL_S2
4a6a3df4
AO
1999 && (pinfo & INSN_UNCOND_BRANCH_DELAY || pinfo & INSN_COND_BRANCH_DELAY
2000 || pinfo & INSN_COND_BRANCH_LIKELY)
2001 && mips_relax_branch
2002 /* Don't try branch relaxation within .set nomacro, or within
2003 .set noat if we use $at for PIC computations. If it turns
2004 out that the branch was out-of-range, we'll get an error. */
2005 && !mips_opts.warn_about_macros
2006 && !(mips_opts.noat && mips_pic != NO_PIC)
2007 && !mips_opts.mips16)
2008 {
895921c9 2009 relaxed_branch = TRUE;
4a6a3df4
AO
2010 f = frag_var (rs_machine_dependent,
2011 relaxed_branch_length
2012 (NULL, NULL,
2013 (pinfo & INSN_UNCOND_BRANCH_DELAY) ? -1
2014 : (pinfo & INSN_COND_BRANCH_LIKELY) ? 1 : 0), 4,
2015 RELAX_BRANCH_ENCODE
af6ae2ad 2016 (pinfo & INSN_UNCOND_BRANCH_DELAY,
4a6a3df4
AO
2017 pinfo & INSN_COND_BRANCH_LIKELY,
2018 pinfo & INSN_WRITE_GPR_31,
2019 0),
2020 address_expr->X_add_symbol,
2021 address_expr->X_add_number,
2022 0);
2023 *reloc_type = BFD_RELOC_UNUSED;
2024 }
2025 else if (*reloc_type > BFD_RELOC_UNUSED)
252b5132
RH
2026 {
2027 /* We need to set up a variant frag. */
2028 assert (mips_opts.mips16 && address_expr != NULL);
2029 f = frag_var (rs_machine_dependent, 4, 0,
f6688943 2030 RELAX_MIPS16_ENCODE (*reloc_type - BFD_RELOC_UNUSED,
252b5132
RH
2031 mips16_small, mips16_ext,
2032 (prev_pinfo
2033 & INSN_UNCOND_BRANCH_DELAY),
f6688943 2034 (*prev_insn_reloc_type
252b5132 2035 == BFD_RELOC_MIPS16_JMP)),
c4e7957c 2036 make_expr_symbol (address_expr), 0, NULL);
252b5132 2037 }
252b5132
RH
2038 else if (mips_opts.mips16
2039 && ! ip->use_extend
f6688943 2040 && *reloc_type != BFD_RELOC_MIPS16_JMP)
252b5132
RH
2041 {
2042 /* Make sure there is enough room to swap this instruction with
2043 a following jump instruction. */
2044 frag_grow (6);
2045 f = frag_more (2);
2046 }
2047 else
2048 {
2049 if (mips_opts.mips16
2050 && mips_opts.noreorder
2051 && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
2052 as_warn (_("extended instruction in delay slot"));
2053
4d7206a2
RS
2054 if (mips_relax.sequence)
2055 {
2056 /* If we've reached the end of this frag, turn it into a variant
2057 frag and record the information for the instructions we've
2058 written so far. */
2059 if (frag_room () < 4)
2060 relax_close_frag ();
2061 mips_relax.sizes[mips_relax.sequence - 1] += 4;
2062 }
2063
584892a6
RS
2064 if (mips_relax.sequence != 2)
2065 mips_macro_warning.sizes[0] += 4;
2066 if (mips_relax.sequence != 1)
2067 mips_macro_warning.sizes[1] += 4;
2068
252b5132
RH
2069 f = frag_more (4);
2070 }
2071
f6688943
TS
2072 fixp[0] = fixp[1] = fixp[2] = NULL;
2073 if (address_expr != NULL && *reloc_type < BFD_RELOC_UNUSED)
252b5132
RH
2074 {
2075 if (address_expr->X_op == O_constant)
2076 {
4db1a35d 2077 valueT tmp;
f6688943
TS
2078
2079 switch (*reloc_type)
252b5132
RH
2080 {
2081 case BFD_RELOC_32:
2082 ip->insn_opcode |= address_expr->X_add_number;
2083 break;
2084
f6688943 2085 case BFD_RELOC_MIPS_HIGHEST:
6344efa4
AM
2086 tmp = (address_expr->X_add_number
2087 + ((valueT) 0x8000 << 32) + 0x80008000) >> 16;
f6688943
TS
2088 tmp >>= 16;
2089 ip->insn_opcode |= (tmp >> 16) & 0xffff;
2090 break;
2091
2092 case BFD_RELOC_MIPS_HIGHER:
2093 tmp = (address_expr->X_add_number + 0x80008000) >> 16;
2094 ip->insn_opcode |= (tmp >> 16) & 0xffff;
2095 break;
2096
2097 case BFD_RELOC_HI16_S:
2098 ip->insn_opcode |= ((address_expr->X_add_number + 0x8000)
2099 >> 16) & 0xffff;
2100 break;
2101
2102 case BFD_RELOC_HI16:
2103 ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
2104 break;
2105
252b5132 2106 case BFD_RELOC_LO16:
ed6fb7bd 2107 case BFD_RELOC_MIPS_GOT_DISP:
252b5132
RH
2108 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2109 break;
2110
2111 case BFD_RELOC_MIPS_JMP:
2112 if ((address_expr->X_add_number & 3) != 0)
2113 as_bad (_("jump to misaligned address (0x%lx)"),
2114 (unsigned long) address_expr->X_add_number);
f3c0ec86 2115 if (address_expr->X_add_number & ~0xfffffff)
7496292d
TS
2116 as_bad (_("jump address range overflow (0x%lx)"),
2117 (unsigned long) address_expr->X_add_number);
252b5132
RH
2118 ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
2119 break;
2120
2121 case BFD_RELOC_MIPS16_JMP:
2122 if ((address_expr->X_add_number & 3) != 0)
2123 as_bad (_("jump to misaligned address (0x%lx)"),
2124 (unsigned long) address_expr->X_add_number);
f3c0ec86 2125 if (address_expr->X_add_number & ~0xfffffff)
7496292d
TS
2126 as_bad (_("jump address range overflow (0x%lx)"),
2127 (unsigned long) address_expr->X_add_number);
252b5132
RH
2128 ip->insn_opcode |=
2129 (((address_expr->X_add_number & 0x7c0000) << 3)
2130 | ((address_expr->X_add_number & 0xf800000) >> 7)
2131 | ((address_expr->X_add_number & 0x3fffc) >> 2));
2132 break;
2133
252b5132
RH
2134 case BFD_RELOC_16_PCREL_S2:
2135 goto need_reloc;
2136
2137 default:
2138 internalError ();
2139 }
2140 }
2141 else
252b5132 2142 need_reloc:
4d7206a2
RS
2143 {
2144 reloc_howto_type *howto;
2145 int i;
34ce925e 2146
4d7206a2
RS
2147 /* In a compound relocation, it is the final (outermost)
2148 operator that determines the relocated field. */
2149 for (i = 1; i < 3; i++)
2150 if (reloc_type[i] == BFD_RELOC_UNUSED)
2151 break;
34ce925e 2152
4d7206a2
RS
2153 howto = bfd_reloc_type_lookup (stdoutput, reloc_type[i - 1]);
2154 fixp[0] = fix_new_exp (frag_now, f - frag_now->fr_literal,
2155 bfd_get_reloc_size(howto),
2156 address_expr,
2157 reloc_type[0] == BFD_RELOC_16_PCREL_S2,
2158 reloc_type[0]);
2159
2160 /* These relocations can have an addend that won't fit in
2161 4 octets for 64bit assembly. */
2162 if (HAVE_64BIT_GPRS
2163 && ! howto->partial_inplace
2164 && (reloc_type[0] == BFD_RELOC_16
2165 || reloc_type[0] == BFD_RELOC_32
2166 || reloc_type[0] == BFD_RELOC_MIPS_JMP
2167 || reloc_type[0] == BFD_RELOC_HI16_S
2168 || reloc_type[0] == BFD_RELOC_LO16
2169 || reloc_type[0] == BFD_RELOC_GPREL16
2170 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
2171 || reloc_type[0] == BFD_RELOC_GPREL32
2172 || reloc_type[0] == BFD_RELOC_64
2173 || reloc_type[0] == BFD_RELOC_CTOR
2174 || reloc_type[0] == BFD_RELOC_MIPS_SUB
2175 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
2176 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
2177 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
2178 || reloc_type[0] == BFD_RELOC_MIPS_REL16
2179 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT))
2180 fixp[0]->fx_no_overflow = 1;
2181
2182 if (mips_relax.sequence)
2183 {
2184 if (mips_relax.first_fixup == 0)
2185 mips_relax.first_fixup = fixp[0];
2186 }
2187 else if (reloc_needs_lo_p (*reloc_type))
2188 {
2189 struct mips_hi_fixup *hi_fixup;
252b5132 2190
4d7206a2
RS
2191 /* Reuse the last entry if it already has a matching %lo. */
2192 hi_fixup = mips_hi_fixup_list;
2193 if (hi_fixup == 0
2194 || !fixup_has_matching_lo_p (hi_fixup->fixp))
2195 {
2196 hi_fixup = ((struct mips_hi_fixup *)
2197 xmalloc (sizeof (struct mips_hi_fixup)));
2198 hi_fixup->next = mips_hi_fixup_list;
2199 mips_hi_fixup_list = hi_fixup;
252b5132 2200 }
4d7206a2
RS
2201 hi_fixup->fixp = fixp[0];
2202 hi_fixup->seg = now_seg;
2203 }
f6688943 2204
4d7206a2
RS
2205 /* Add fixups for the second and third relocations, if given.
2206 Note that the ABI allows the second relocation to be
2207 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the
2208 moment we only use RSS_UNDEF, but we could add support
2209 for the others if it ever becomes necessary. */
2210 for (i = 1; i < 3; i++)
2211 if (reloc_type[i] != BFD_RELOC_UNUSED)
2212 {
2213 address_expr->X_op = O_absent;
2214 address_expr->X_add_symbol = 0;
2215 address_expr->X_add_number = 0;
34ce925e 2216
4d7206a2
RS
2217 fixp[i] = fix_new_exp (frag_now, fixp[0]->fx_where,
2218 fixp[0]->fx_size, address_expr,
2219 FALSE, reloc_type[i]);
2220 }
252b5132
RH
2221 }
2222 }
2223
2224 if (! mips_opts.mips16)
c5dd6aab
DJ
2225 {
2226 md_number_to_chars (f, ip->insn_opcode, 4);
2227#ifdef OBJ_ELF
2228 dwarf2_emit_insn (4);
2229#endif
2230 }
f6688943 2231 else if (*reloc_type == BFD_RELOC_MIPS16_JMP)
252b5132
RH
2232 {
2233 md_number_to_chars (f, ip->insn_opcode >> 16, 2);
2234 md_number_to_chars (f + 2, ip->insn_opcode & 0xffff, 2);
c5dd6aab
DJ
2235#ifdef OBJ_ELF
2236 dwarf2_emit_insn (4);
2237#endif
252b5132
RH
2238 }
2239 else
2240 {
2241 if (ip->use_extend)
2242 {
2243 md_number_to_chars (f, 0xf000 | ip->extend, 2);
2244 f += 2;
2245 }
2246 md_number_to_chars (f, ip->insn_opcode, 2);
c5dd6aab
DJ
2247#ifdef OBJ_ELF
2248 dwarf2_emit_insn (ip->use_extend ? 4 : 2);
2249#endif
252b5132
RH
2250 }
2251
2252 /* Update the register mask information. */
2253 if (! mips_opts.mips16)
2254 {
2255 if (pinfo & INSN_WRITE_GPR_D)
2256 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
2257 if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2258 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
2259 if (pinfo & INSN_READ_GPR_S)
2260 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
2261 if (pinfo & INSN_WRITE_GPR_31)
f9419b05 2262 mips_gprmask |= 1 << RA;
252b5132
RH
2263 if (pinfo & INSN_WRITE_FPR_D)
2264 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
2265 if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2266 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
2267 if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2268 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
2269 if ((pinfo & INSN_READ_FPR_R) != 0)
2270 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
2271 if (pinfo & INSN_COP)
2272 {
bdaaa2e1
KH
2273 /* We don't keep enough information to sort these cases out.
2274 The itbl support does keep this information however, although
2275 we currently don't support itbl fprmats as part of the cop
2276 instruction. May want to add this support in the future. */
252b5132
RH
2277 }
2278 /* Never set the bit for $0, which is always zero. */
beae10d5 2279 mips_gprmask &= ~1 << 0;
252b5132
RH
2280 }
2281 else
2282 {
2283 if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2284 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RX)
2285 & MIPS16OP_MASK_RX);
2286 if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2287 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RY)
2288 & MIPS16OP_MASK_RY);
2289 if (pinfo & MIPS16_INSN_WRITE_Z)
2290 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RZ)
2291 & MIPS16OP_MASK_RZ);
2292 if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2293 mips_gprmask |= 1 << TREG;
2294 if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2295 mips_gprmask |= 1 << SP;
2296 if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2297 mips_gprmask |= 1 << RA;
2298 if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2299 mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2300 if (pinfo & MIPS16_INSN_READ_Z)
2301 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
2302 & MIPS16OP_MASK_MOVE32Z);
2303 if (pinfo & MIPS16_INSN_READ_GPR_X)
2304 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
2305 & MIPS16OP_MASK_REGR32);
2306 }
2307
4d7206a2 2308 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
252b5132
RH
2309 {
2310 /* Filling the branch delay slot is more complex. We try to
2311 switch the branch with the previous instruction, which we can
2312 do if the previous instruction does not set up a condition
2313 that the branch tests and if the branch is not itself the
2314 target of any branch. */
2315 if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2316 || (pinfo & INSN_COND_BRANCH_DELAY))
2317 {
2318 if (mips_optimize < 2
2319 /* If we have seen .set volatile or .set nomove, don't
2320 optimize. */
2321 || mips_opts.nomove != 0
2322 /* If we had to emit any NOP instructions, then we
2323 already know we can not swap. */
2324 || nops != 0
2325 /* If we don't even know the previous insn, we can not
bdaaa2e1 2326 swap. */
252b5132
RH
2327 || ! prev_insn_valid
2328 /* If the previous insn is already in a branch delay
2329 slot, then we can not swap. */
2330 || prev_insn_is_delay_slot
2331 /* If the previous previous insn was in a .set
2332 noreorder, we can't swap. Actually, the MIPS
2333 assembler will swap in this situation. However, gcc
2334 configured -with-gnu-as will generate code like
2335 .set noreorder
2336 lw $4,XXX
2337 .set reorder
2338 INSN
2339 bne $4,$0,foo
2340 in which we can not swap the bne and INSN. If gcc is
2341 not configured -with-gnu-as, it does not output the
2342 .set pseudo-ops. We don't have to check
2343 prev_insn_unreordered, because prev_insn_valid will
2344 be 0 in that case. We don't want to use
2345 prev_prev_insn_valid, because we do want to be able
2346 to swap at the start of a function. */
2347 || prev_prev_insn_unreordered
2348 /* If the branch is itself the target of a branch, we
2349 can not swap. We cheat on this; all we check for is
2350 whether there is a label on this instruction. If
2351 there are any branches to anything other than a
2352 label, users must use .set noreorder. */
2353 || insn_labels != NULL
895921c9
MR
2354 /* If the previous instruction is in a variant frag
2355 other than this branch's one, we cannot do the swap.
2356 This does not apply to the mips16, which uses variant
2357 frags for different purposes. */
252b5132 2358 || (! mips_opts.mips16
895921c9 2359 && prev_insn_frag_type == rs_machine_dependent)
252b5132
RH
2360 /* If the branch reads the condition codes, we don't
2361 even try to swap, because in the sequence
2362 ctc1 $X,$31
2363 INSN
2364 INSN
2365 bc1t LABEL
2366 we can not swap, and I don't feel like handling that
2367 case. */
2368 || (! mips_opts.mips16
81912461
ILT
2369 && (pinfo & INSN_READ_COND_CODE)
2370 && ! cop_interlocks)
252b5132 2371 /* We can not swap with an instruction that requires a
67c1ffbe 2372 delay slot, because the target of the branch might
252b5132
RH
2373 interfere with that instruction. */
2374 || (! mips_opts.mips16
252b5132 2375 && (prev_pinfo
bdaaa2e1 2376 /* Itbl support may require additional care here. */
252b5132
RH
2377 & (INSN_LOAD_COPROC_DELAY
2378 | INSN_COPROC_MOVE_DELAY
81912461
ILT
2379 | INSN_WRITE_COND_CODE))
2380 && ! cop_interlocks)
252b5132 2381 || (! (hilo_interlocks
048cdf86 2382 || (mips_opts.arch == CPU_R3900 && (pinfo & INSN_MULT)))
252b5132
RH
2383 && (prev_pinfo
2384 & (INSN_READ_LO
2385 | INSN_READ_HI)))
2386 || (! mips_opts.mips16
81912461
ILT
2387 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY)
2388 && ! gpr_interlocks)
252b5132 2389 || (! mips_opts.mips16
bdaaa2e1 2390 /* Itbl support may require additional care here. */
81912461
ILT
2391 && (prev_pinfo & INSN_COPROC_MEMORY_DELAY)
2392 && ! cop_mem_interlocks)
252b5132
RH
2393 /* We can not swap with a branch instruction. */
2394 || (prev_pinfo
2395 & (INSN_UNCOND_BRANCH_DELAY
2396 | INSN_COND_BRANCH_DELAY
2397 | INSN_COND_BRANCH_LIKELY))
2398 /* We do not swap with a trap instruction, since it
2399 complicates trap handlers to have the trap
2400 instruction be in a delay slot. */
2401 || (prev_pinfo & INSN_TRAP)
2402 /* If the branch reads a register that the previous
2403 instruction sets, we can not swap. */
2404 || (! mips_opts.mips16
2405 && (prev_pinfo & INSN_WRITE_GPR_T)
2406 && insn_uses_reg (ip,
2407 ((prev_insn.insn_opcode >> OP_SH_RT)
2408 & OP_MASK_RT),
2409 MIPS_GR_REG))
2410 || (! mips_opts.mips16
2411 && (prev_pinfo & INSN_WRITE_GPR_D)
2412 && insn_uses_reg (ip,
2413 ((prev_insn.insn_opcode >> OP_SH_RD)
2414 & OP_MASK_RD),
2415 MIPS_GR_REG))
2416 || (mips_opts.mips16
2417 && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2418 && insn_uses_reg (ip,
2419 ((prev_insn.insn_opcode
2420 >> MIPS16OP_SH_RX)
2421 & MIPS16OP_MASK_RX),
2422 MIPS16_REG))
2423 || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2424 && insn_uses_reg (ip,
2425 ((prev_insn.insn_opcode
2426 >> MIPS16OP_SH_RY)
2427 & MIPS16OP_MASK_RY),
2428 MIPS16_REG))
2429 || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2430 && insn_uses_reg (ip,
2431 ((prev_insn.insn_opcode
2432 >> MIPS16OP_SH_RZ)
2433 & MIPS16OP_MASK_RZ),
2434 MIPS16_REG))
2435 || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2436 && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2437 || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2438 && insn_uses_reg (ip, RA, MIPS_GR_REG))
2439 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2440 && insn_uses_reg (ip,
2441 MIPS16OP_EXTRACT_REG32R (prev_insn.
2442 insn_opcode),
2443 MIPS_GR_REG))))
2444 /* If the branch writes a register that the previous
2445 instruction sets, we can not swap (we know that
2446 branches write only to RD or to $31). */
2447 || (! mips_opts.mips16
2448 && (prev_pinfo & INSN_WRITE_GPR_T)
2449 && (((pinfo & INSN_WRITE_GPR_D)
2450 && (((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT)
2451 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2452 || ((pinfo & INSN_WRITE_GPR_31)
2453 && (((prev_insn.insn_opcode >> OP_SH_RT)
2454 & OP_MASK_RT)
f9419b05 2455 == RA))))
252b5132
RH
2456 || (! mips_opts.mips16
2457 && (prev_pinfo & INSN_WRITE_GPR_D)
2458 && (((pinfo & INSN_WRITE_GPR_D)
2459 && (((prev_insn.insn_opcode >> OP_SH_RD) & OP_MASK_RD)
2460 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2461 || ((pinfo & INSN_WRITE_GPR_31)
2462 && (((prev_insn.insn_opcode >> OP_SH_RD)
2463 & OP_MASK_RD)
f9419b05 2464 == RA))))
252b5132
RH
2465 || (mips_opts.mips16
2466 && (pinfo & MIPS16_INSN_WRITE_31)
2467 && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2468 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2469 && (MIPS16OP_EXTRACT_REG32R (prev_insn.insn_opcode)
2470 == RA))))
2471 /* If the branch writes a register that the previous
2472 instruction reads, we can not swap (we know that
2473 branches only write to RD or to $31). */
2474 || (! mips_opts.mips16
2475 && (pinfo & INSN_WRITE_GPR_D)
2476 && insn_uses_reg (&prev_insn,
2477 ((ip->insn_opcode >> OP_SH_RD)
2478 & OP_MASK_RD),
2479 MIPS_GR_REG))
2480 || (! mips_opts.mips16
2481 && (pinfo & INSN_WRITE_GPR_31)
f9419b05 2482 && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
252b5132
RH
2483 || (mips_opts.mips16
2484 && (pinfo & MIPS16_INSN_WRITE_31)
2485 && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2486 /* If we are generating embedded PIC code, the branch
2487 might be expanded into a sequence which uses $at, so
2488 we can't swap with an instruction which reads it. */
2489 || (mips_pic == EMBEDDED_PIC
2490 && insn_uses_reg (&prev_insn, AT, MIPS_GR_REG))
2491 /* If the previous previous instruction has a load
2492 delay, and sets a register that the branch reads, we
2493 can not swap. */
2494 || (! mips_opts.mips16
bdaaa2e1 2495 /* Itbl support may require additional care here. */
81912461
ILT
2496 && (((prev_prev_insn.insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
2497 && ! cop_interlocks)
2498 || ((prev_prev_insn.insn_mo->pinfo
2499 & INSN_LOAD_MEMORY_DELAY)
2500 && ! gpr_interlocks))
252b5132
RH
2501 && insn_uses_reg (ip,
2502 ((prev_prev_insn.insn_opcode >> OP_SH_RT)
2503 & OP_MASK_RT),
2504 MIPS_GR_REG))
2505 /* If one instruction sets a condition code and the
2506 other one uses a condition code, we can not swap. */
2507 || ((pinfo & INSN_READ_COND_CODE)
2508 && (prev_pinfo & INSN_WRITE_COND_CODE))
2509 || ((pinfo & INSN_WRITE_COND_CODE)
2510 && (prev_pinfo & INSN_READ_COND_CODE))
2511 /* If the previous instruction uses the PC, we can not
2512 swap. */
2513 || (mips_opts.mips16
2514 && (prev_pinfo & MIPS16_INSN_READ_PC))
2515 /* If the previous instruction was extended, we can not
2516 swap. */
2517 || (mips_opts.mips16 && prev_insn_extended)
2518 /* If the previous instruction had a fixup in mips16
2519 mode, we can not swap. This normally means that the
2520 previous instruction was a 4 byte branch anyhow. */
f6688943 2521 || (mips_opts.mips16 && prev_insn_fixp[0])
bdaaa2e1
KH
2522 /* If the previous instruction is a sync, sync.l, or
2523 sync.p, we can not swap. */
f173e82e 2524 || (prev_pinfo & INSN_SYNC))
252b5132
RH
2525 {
2526 /* We could do even better for unconditional branches to
2527 portions of this object file; we could pick up the
2528 instruction at the destination, put it in the delay
2529 slot, and bump the destination address. */
2530 emit_nop ();
2531 /* Update the previous insn information. */
2532 prev_prev_insn = *ip;
2533 prev_insn.insn_mo = &dummy_opcode;
2534 }
2535 else
2536 {
2537 /* It looks like we can actually do the swap. */
2538 if (! mips_opts.mips16)
2539 {
2540 char *prev_f;
2541 char temp[4];
2542
2543 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
895921c9
MR
2544 if (!relaxed_branch)
2545 {
2546 /* If this is not a relaxed branch, then just
2547 swap the instructions. */
2548 memcpy (temp, prev_f, 4);
2549 memcpy (prev_f, f, 4);
2550 memcpy (f, temp, 4);
2551 }
2552 else
2553 {
2554 /* If this is a relaxed branch, then we move the
2555 instruction to be placed in the delay slot to
2556 the current frag, shrinking the fixed part of
2557 the originating frag. If the branch occupies
2558 the tail of the latter, we move it backwards,
2559 into the space freed by the moved instruction. */
2560 f = frag_more (4);
2561 memcpy (f, prev_f, 4);
2562 prev_insn_frag->fr_fix -= 4;
2563 if (prev_insn_frag->fr_type == rs_machine_dependent)
2564 memmove (prev_f, prev_f + 4, prev_insn_frag->fr_var);
2565 }
2566
f6688943
TS
2567 if (prev_insn_fixp[0])
2568 {
2569 prev_insn_fixp[0]->fx_frag = frag_now;
2570 prev_insn_fixp[0]->fx_where = f - frag_now->fr_literal;
2571 }
2572 if (prev_insn_fixp[1])
2573 {
2574 prev_insn_fixp[1]->fx_frag = frag_now;
2575 prev_insn_fixp[1]->fx_where = f - frag_now->fr_literal;
2576 }
2577 if (prev_insn_fixp[2])
252b5132 2578 {
f6688943
TS
2579 prev_insn_fixp[2]->fx_frag = frag_now;
2580 prev_insn_fixp[2]->fx_where = f - frag_now->fr_literal;
252b5132 2581 }
f5040a92
AO
2582 if (prev_insn_fixp[0] && HAVE_NEWABI
2583 && prev_insn_frag != frag_now
2584 && (prev_insn_fixp[0]->fx_r_type
2585 == BFD_RELOC_MIPS_GOT_DISP
2586 || (prev_insn_fixp[0]->fx_r_type
2587 == BFD_RELOC_MIPS_CALL16)))
2588 {
2589 /* To avoid confusion in tc_gen_reloc, we must
2590 ensure that this does not become a variant
2591 frag. */
2592 force_new_frag = TRUE;
2593 }
895921c9
MR
2594
2595 if (!relaxed_branch)
f6688943 2596 {
895921c9
MR
2597 if (fixp[0])
2598 {
2599 fixp[0]->fx_frag = prev_insn_frag;
2600 fixp[0]->fx_where = prev_insn_where;
2601 }
2602 if (fixp[1])
2603 {
2604 fixp[1]->fx_frag = prev_insn_frag;
2605 fixp[1]->fx_where = prev_insn_where;
2606 }
2607 if (fixp[2])
2608 {
2609 fixp[2]->fx_frag = prev_insn_frag;
2610 fixp[2]->fx_where = prev_insn_where;
2611 }
f6688943 2612 }
895921c9 2613 else if (prev_insn_frag->fr_type == rs_machine_dependent)
f6688943 2614 {
895921c9
MR
2615 if (fixp[0])
2616 fixp[0]->fx_where -= 4;
2617 if (fixp[1])
2618 fixp[1]->fx_where -= 4;
2619 if (fixp[2])
2620 fixp[2]->fx_where -= 4;
252b5132
RH
2621 }
2622 }
2623 else
2624 {
2625 char *prev_f;
2626 char temp[2];
2627
f6688943
TS
2628 assert (prev_insn_fixp[0] == NULL);
2629 assert (prev_insn_fixp[1] == NULL);
2630 assert (prev_insn_fixp[2] == NULL);
252b5132
RH
2631 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2632 memcpy (temp, prev_f, 2);
2633 memcpy (prev_f, f, 2);
f6688943 2634 if (*reloc_type != BFD_RELOC_MIPS16_JMP)
252b5132 2635 {
f6688943 2636 assert (*reloc_type == BFD_RELOC_UNUSED);
252b5132
RH
2637 memcpy (f, temp, 2);
2638 }
2639 else
2640 {
2641 memcpy (f, f + 2, 2);
2642 memcpy (f + 2, temp, 2);
2643 }
f6688943
TS
2644 if (fixp[0])
2645 {
2646 fixp[0]->fx_frag = prev_insn_frag;
2647 fixp[0]->fx_where = prev_insn_where;
2648 }
2649 if (fixp[1])
2650 {
2651 fixp[1]->fx_frag = prev_insn_frag;
2652 fixp[1]->fx_where = prev_insn_where;
2653 }
2654 if (fixp[2])
252b5132 2655 {
f6688943
TS
2656 fixp[2]->fx_frag = prev_insn_frag;
2657 fixp[2]->fx_where = prev_insn_where;
252b5132
RH
2658 }
2659 }
2660
2661 /* Update the previous insn information; leave prev_insn
2662 unchanged. */
2663 prev_prev_insn = *ip;
2664 }
2665 prev_insn_is_delay_slot = 1;
2666
2667 /* If that was an unconditional branch, forget the previous
2668 insn information. */
2669 if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2670 {
2671 prev_prev_insn.insn_mo = &dummy_opcode;
2672 prev_insn.insn_mo = &dummy_opcode;
2673 }
2674
f6688943
TS
2675 prev_insn_fixp[0] = NULL;
2676 prev_insn_fixp[1] = NULL;
2677 prev_insn_fixp[2] = NULL;
2678 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2679 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2680 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
252b5132
RH
2681 prev_insn_extended = 0;
2682 }
2683 else if (pinfo & INSN_COND_BRANCH_LIKELY)
2684 {
2685 /* We don't yet optimize a branch likely. What we should do
2686 is look at the target, copy the instruction found there
2687 into the delay slot, and increment the branch to jump to
2688 the next instruction. */
2689 emit_nop ();
2690 /* Update the previous insn information. */
2691 prev_prev_insn = *ip;
2692 prev_insn.insn_mo = &dummy_opcode;
f6688943
TS
2693 prev_insn_fixp[0] = NULL;
2694 prev_insn_fixp[1] = NULL;
2695 prev_insn_fixp[2] = NULL;
2696 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2697 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2698 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
252b5132
RH
2699 prev_insn_extended = 0;
2700 }
2701 else
2702 {
2703 /* Update the previous insn information. */
2704 if (nops > 0)
2705 prev_prev_insn.insn_mo = &dummy_opcode;
2706 else
2707 prev_prev_insn = prev_insn;
2708 prev_insn = *ip;
2709
2710 /* Any time we see a branch, we always fill the delay slot
2711 immediately; since this insn is not a branch, we know it
2712 is not in a delay slot. */
2713 prev_insn_is_delay_slot = 0;
2714
f6688943
TS
2715 prev_insn_fixp[0] = fixp[0];
2716 prev_insn_fixp[1] = fixp[1];
2717 prev_insn_fixp[2] = fixp[2];
2718 prev_insn_reloc_type[0] = reloc_type[0];
2719 prev_insn_reloc_type[1] = reloc_type[1];
2720 prev_insn_reloc_type[2] = reloc_type[2];
252b5132
RH
2721 if (mips_opts.mips16)
2722 prev_insn_extended = (ip->use_extend
f6688943 2723 || *reloc_type > BFD_RELOC_UNUSED);
252b5132
RH
2724 }
2725
2726 prev_prev_insn_unreordered = prev_insn_unreordered;
2727 prev_insn_unreordered = 0;
2728 prev_insn_frag = frag_now;
2729 prev_insn_where = f - frag_now->fr_literal;
2730 prev_insn_valid = 1;
2731 }
4d7206a2 2732 else if (mips_relax.sequence != 2)
252b5132
RH
2733 {
2734 /* We need to record a bit of information even when we are not
2735 reordering, in order to determine the base address for mips16
2736 PC relative relocs. */
2737 prev_prev_insn = prev_insn;
2738 prev_insn = *ip;
f6688943
TS
2739 prev_insn_reloc_type[0] = reloc_type[0];
2740 prev_insn_reloc_type[1] = reloc_type[1];
2741 prev_insn_reloc_type[2] = reloc_type[2];
252b5132
RH
2742 prev_prev_insn_unreordered = prev_insn_unreordered;
2743 prev_insn_unreordered = 1;
2744 }
2745
2746 /* We just output an insn, so the next one doesn't have a label. */
2747 mips_clear_insn_labels ();
252b5132
RH
2748}
2749
2750/* This function forgets that there was any previous instruction or
2751 label. If PRESERVE is non-zero, it remembers enough information to
bdaaa2e1 2752 know whether nops are needed before a noreorder section. */
252b5132
RH
2753
2754static void
17a2f251 2755mips_no_prev_insn (int preserve)
252b5132
RH
2756{
2757 if (! preserve)
2758 {
2759 prev_insn.insn_mo = &dummy_opcode;
2760 prev_prev_insn.insn_mo = &dummy_opcode;
2761 prev_nop_frag = NULL;
2762 prev_nop_frag_holds = 0;
2763 prev_nop_frag_required = 0;
2764 prev_nop_frag_since = 0;
2765 }
2766 prev_insn_valid = 0;
2767 prev_insn_is_delay_slot = 0;
2768 prev_insn_unreordered = 0;
2769 prev_insn_extended = 0;
f6688943
TS
2770 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2771 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2772 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
252b5132
RH
2773 prev_prev_insn_unreordered = 0;
2774 mips_clear_insn_labels ();
2775}
2776
2777/* This function must be called whenever we turn on noreorder or emit
2778 something other than instructions. It inserts any NOPS which might
2779 be needed by the previous instruction, and clears the information
2780 kept for the previous instructions. The INSNS parameter is true if
bdaaa2e1 2781 instructions are to follow. */
252b5132
RH
2782
2783static void
17a2f251 2784mips_emit_delays (bfd_boolean insns)
252b5132
RH
2785{
2786 if (! mips_opts.noreorder)
2787 {
2788 int nops;
2789
2790 nops = 0;
2791 if ((! mips_opts.mips16
81912461
ILT
2792 && ((prev_insn.insn_mo->pinfo
2793 & (INSN_LOAD_COPROC_DELAY
2794 | INSN_COPROC_MOVE_DELAY
2795 | INSN_WRITE_COND_CODE))
2796 && ! cop_interlocks))
252b5132
RH
2797 || (! hilo_interlocks
2798 && (prev_insn.insn_mo->pinfo
2799 & (INSN_READ_LO
2800 | INSN_READ_HI)))
2801 || (! mips_opts.mips16
81912461
ILT
2802 && (prev_insn.insn_mo->pinfo & INSN_LOAD_MEMORY_DELAY)
2803 && ! gpr_interlocks)
252b5132 2804 || (! mips_opts.mips16
81912461
ILT
2805 && (prev_insn.insn_mo->pinfo & INSN_COPROC_MEMORY_DELAY)
2806 && ! cop_mem_interlocks))
252b5132 2807 {
beae10d5 2808 /* Itbl support may require additional care here. */
252b5132
RH
2809 ++nops;
2810 if ((! mips_opts.mips16
81912461
ILT
2811 && ((prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
2812 && ! cop_interlocks))
252b5132
RH
2813 || (! hilo_interlocks
2814 && ((prev_insn.insn_mo->pinfo & INSN_READ_HI)
2815 || (prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2816 ++nops;
2817
2818 if (prev_insn_unreordered)
2819 nops = 0;
2820 }
2821 else if ((! mips_opts.mips16
81912461
ILT
2822 && ((prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
2823 && ! cop_interlocks))
252b5132
RH
2824 || (! hilo_interlocks
2825 && ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
2826 || (prev_prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2827 {
beae10d5 2828 /* Itbl support may require additional care here. */
252b5132
RH
2829 if (! prev_prev_insn_unreordered)
2830 ++nops;
2831 }
2832
d766e8ec 2833 if (mips_fix_vr4120 && prev_insn.insn_mo->name)
60b63b72
RS
2834 {
2835 int min_nops = 0;
2836 const char *pn = prev_insn.insn_mo->name;
2837 if (strncmp(pn, "macc", 4) == 0
2838 || strncmp(pn, "dmacc", 5) == 0
2839 || strncmp(pn, "dmult", 5) == 0)
2840 {
2841 min_nops = 1;
2842 }
2843 if (nops < min_nops)
2844 nops = min_nops;
2845 }
2846
252b5132
RH
2847 if (nops > 0)
2848 {
2849 struct insn_label_list *l;
2850
2851 if (insns)
2852 {
2853 /* Record the frag which holds the nop instructions, so
2854 that we can remove them if we don't need them. */
2855 frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2856 prev_nop_frag = frag_now;
2857 prev_nop_frag_holds = nops;
2858 prev_nop_frag_required = 0;
2859 prev_nop_frag_since = 0;
2860 }
2861
2862 for (; nops > 0; --nops)
2863 emit_nop ();
2864
2865 if (insns)
2866 {
2867 /* Move on to a new frag, so that it is safe to simply
bdaaa2e1 2868 decrease the size of prev_nop_frag. */
252b5132
RH
2869 frag_wane (frag_now);
2870 frag_new (0);
2871 }
2872
2873 for (l = insn_labels; l != NULL; l = l->next)
2874 {
98aa84af
AM
2875 valueT val;
2876
252b5132 2877 assert (S_GET_SEGMENT (l->label) == now_seg);
49309057 2878 symbol_set_frag (l->label, frag_now);
98aa84af 2879 val = (valueT) frag_now_fix ();
252b5132
RH
2880 /* mips16 text labels are stored as odd. */
2881 if (mips_opts.mips16)
f9419b05 2882 ++val;
98aa84af 2883 S_SET_VALUE (l->label, val);
252b5132
RH
2884 }
2885 }
2886 }
2887
2888 /* Mark instruction labels in mips16 mode. */
f9419b05 2889 if (insns)
252b5132
RH
2890 mips16_mark_labels ();
2891
2892 mips_no_prev_insn (insns);
2893}
2894
584892a6
RS
2895/* Set up global variables for the start of a new macro. */
2896
2897static void
2898macro_start (void)
2899{
2900 memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
2901 mips_macro_warning.delay_slot_p = (mips_opts.noreorder
2902 && (prev_insn.insn_mo->pinfo
2903 & (INSN_UNCOND_BRANCH_DELAY
2904 | INSN_COND_BRANCH_DELAY
2905 | INSN_COND_BRANCH_LIKELY)) != 0);
2906}
2907
2908/* Given that a macro is longer than 4 bytes, return the appropriate warning
2909 for it. Return null if no warning is needed. SUBTYPE is a bitmask of
2910 RELAX_DELAY_SLOT and RELAX_NOMACRO. */
2911
2912static const char *
2913macro_warning (relax_substateT subtype)
2914{
2915 if (subtype & RELAX_DELAY_SLOT)
2916 return _("Macro instruction expanded into multiple instructions"
2917 " in a branch delay slot");
2918 else if (subtype & RELAX_NOMACRO)
2919 return _("Macro instruction expanded into multiple instructions");
2920 else
2921 return 0;
2922}
2923
2924/* Finish up a macro. Emit warnings as appropriate. */
2925
2926static void
2927macro_end (void)
2928{
2929 if (mips_macro_warning.sizes[0] > 4 || mips_macro_warning.sizes[1] > 4)
2930 {
2931 relax_substateT subtype;
2932
2933 /* Set up the relaxation warning flags. */
2934 subtype = 0;
2935 if (mips_macro_warning.sizes[1] > mips_macro_warning.sizes[0])
2936 subtype |= RELAX_SECOND_LONGER;
2937 if (mips_opts.warn_about_macros)
2938 subtype |= RELAX_NOMACRO;
2939 if (mips_macro_warning.delay_slot_p)
2940 subtype |= RELAX_DELAY_SLOT;
2941
2942 if (mips_macro_warning.sizes[0] > 4 && mips_macro_warning.sizes[1] > 4)
2943 {
2944 /* Either the macro has a single implementation or both
2945 implementations are longer than 4 bytes. Emit the
2946 warning now. */
2947 const char *msg = macro_warning (subtype);
2948 if (msg != 0)
2949 as_warn (msg);
2950 }
2951 else
2952 {
2953 /* One implementation might need a warning but the other
2954 definitely doesn't. */
2955 mips_macro_warning.first_frag->fr_subtype |= subtype;
2956 }
2957 }
2958}
2959
252b5132
RH
2960/* Build an instruction created by a macro expansion. This is passed
2961 a pointer to the count of instructions created so far, an
2962 expression, the name of the instruction to build, an operand format
2963 string, and corresponding arguments. */
2964
252b5132 2965static void
67c0d1eb 2966macro_build (expressionS *ep, const char *name, const char *fmt, ...)
252b5132
RH
2967{
2968 struct mips_cl_insn insn;
f6688943 2969 bfd_reloc_code_real_type r[3];
252b5132 2970 va_list args;
252b5132 2971
252b5132 2972 va_start (args, fmt);
252b5132 2973
252b5132
RH
2974 if (mips_opts.mips16)
2975 {
67c0d1eb 2976 mips16_macro_build (ep, name, fmt, args);
252b5132
RH
2977 va_end (args);
2978 return;
2979 }
2980
f6688943
TS
2981 r[0] = BFD_RELOC_UNUSED;
2982 r[1] = BFD_RELOC_UNUSED;
2983 r[2] = BFD_RELOC_UNUSED;
252b5132
RH
2984 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
2985 assert (insn.insn_mo);
2986 assert (strcmp (name, insn.insn_mo->name) == 0);
2987
2988 /* Search until we get a match for NAME. */
2989 while (1)
2990 {
b34976b6 2991 /* It is assumed here that macros will never generate
deec1734 2992 MDMX or MIPS-3D instructions. */
252b5132
RH
2993 if (strcmp (fmt, insn.insn_mo->args) == 0
2994 && insn.insn_mo->pinfo != INSN_MACRO
aec421e0
TS
2995 && OPCODE_IS_MEMBER (insn.insn_mo,
2996 (mips_opts.isa
3396de36 2997 | (file_ase_mips16 ? INSN_MIPS16 : 0)),
fef14a42
TS
2998 mips_opts.arch)
2999 && (mips_opts.arch != CPU_R4650 || (insn.insn_mo->pinfo & FP_D) == 0))
252b5132
RH
3000 break;
3001
3002 ++insn.insn_mo;
3003 assert (insn.insn_mo->name);
3004 assert (strcmp (name, insn.insn_mo->name) == 0);
3005 }
3006
3007 insn.insn_opcode = insn.insn_mo->match;
3008 for (;;)
3009 {
3010 switch (*fmt++)
3011 {
3012 case '\0':
3013 break;
3014
3015 case ',':
3016 case '(':
3017 case ')':
3018 continue;
3019
5f74bc13
CD
3020 case '+':
3021 switch (*fmt++)
3022 {
3023 case 'A':
3024 case 'E':
3025 insn.insn_opcode |= (va_arg (args, int)
3026 & OP_MASK_SHAMT) << OP_SH_SHAMT;
3027 continue;
3028
3029 case 'B':
3030 case 'F':
3031 /* Note that in the macro case, these arguments are already
3032 in MSB form. (When handling the instruction in the
3033 non-macro case, these arguments are sizes from which
3034 MSB values must be calculated.) */
3035 insn.insn_opcode |= (va_arg (args, int)
3036 & OP_MASK_INSMSB) << OP_SH_INSMSB;
3037 continue;
3038
3039 case 'C':
3040 case 'G':
3041 case 'H':
3042 /* Note that in the macro case, these arguments are already
3043 in MSBD form. (When handling the instruction in the
3044 non-macro case, these arguments are sizes from which
3045 MSBD values must be calculated.) */
3046 insn.insn_opcode |= (va_arg (args, int)
3047 & OP_MASK_EXTMSBD) << OP_SH_EXTMSBD;
3048 continue;
3049
3050 default:
3051 internalError ();
3052 }
3053 continue;
3054
252b5132
RH
3055 case 't':
3056 case 'w':
3057 case 'E':
38487616 3058 insn.insn_opcode |= va_arg (args, int) << OP_SH_RT;
252b5132
RH
3059 continue;
3060
3061 case 'c':
38487616
TS
3062 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE;
3063 continue;
3064
252b5132
RH
3065 case 'T':
3066 case 'W':
38487616 3067 insn.insn_opcode |= va_arg (args, int) << OP_SH_FT;
252b5132
RH
3068 continue;
3069
3070 case 'd':
3071 case 'G':
af7ee8bf 3072 case 'K':
38487616 3073 insn.insn_opcode |= va_arg (args, int) << OP_SH_RD;
252b5132
RH
3074 continue;
3075
4372b673
NC
3076 case 'U':
3077 {
3078 int tmp = va_arg (args, int);
3079
38487616
TS
3080 insn.insn_opcode |= tmp << OP_SH_RT;
3081 insn.insn_opcode |= tmp << OP_SH_RD;
beae10d5 3082 continue;
4372b673
NC
3083 }
3084
252b5132
RH
3085 case 'V':
3086 case 'S':
38487616 3087 insn.insn_opcode |= va_arg (args, int) << OP_SH_FS;
252b5132
RH
3088 continue;
3089
3090 case 'z':
3091 continue;
3092
3093 case '<':
38487616 3094 insn.insn_opcode |= va_arg (args, int) << OP_SH_SHAMT;
252b5132
RH
3095 continue;
3096
3097 case 'D':
38487616 3098 insn.insn_opcode |= va_arg (args, int) << OP_SH_FD;
252b5132
RH
3099 continue;
3100
3101 case 'B':
38487616 3102 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE20;
252b5132
RH
3103 continue;
3104
4372b673 3105 case 'J':
38487616 3106 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE19;
4372b673
NC
3107 continue;
3108
252b5132 3109 case 'q':
38487616 3110 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE2;
252b5132
RH
3111 continue;
3112
3113 case 'b':
3114 case 's':
3115 case 'r':
3116 case 'v':
38487616 3117 insn.insn_opcode |= va_arg (args, int) << OP_SH_RS;
252b5132
RH
3118 continue;
3119
3120 case 'i':
3121 case 'j':
3122 case 'o':
f6688943 3123 *r = (bfd_reloc_code_real_type) va_arg (args, int);
cdf6fd85 3124 assert (*r == BFD_RELOC_GPREL16
f6688943
TS
3125 || *r == BFD_RELOC_MIPS_LITERAL
3126 || *r == BFD_RELOC_MIPS_HIGHER
3127 || *r == BFD_RELOC_HI16_S
3128 || *r == BFD_RELOC_LO16
3129 || *r == BFD_RELOC_MIPS_GOT16
3130 || *r == BFD_RELOC_MIPS_CALL16
438c16b8
TS
3131 || *r == BFD_RELOC_MIPS_GOT_DISP
3132 || *r == BFD_RELOC_MIPS_GOT_PAGE
3133 || *r == BFD_RELOC_MIPS_GOT_OFST
f6688943
TS
3134 || *r == BFD_RELOC_MIPS_GOT_LO16
3135 || *r == BFD_RELOC_MIPS_CALL_LO16
252b5132 3136 || (ep->X_op == O_subtract
f6688943 3137 && *r == BFD_RELOC_PCREL_LO16));
252b5132
RH
3138 continue;
3139
3140 case 'u':
f6688943 3141 *r = (bfd_reloc_code_real_type) va_arg (args, int);
252b5132
RH
3142 assert (ep != NULL
3143 && (ep->X_op == O_constant
3144 || (ep->X_op == O_symbol
f6688943
TS
3145 && (*r == BFD_RELOC_MIPS_HIGHEST
3146 || *r == BFD_RELOC_HI16_S
3147 || *r == BFD_RELOC_HI16
3148 || *r == BFD_RELOC_GPREL16
3149 || *r == BFD_RELOC_MIPS_GOT_HI16
3150 || *r == BFD_RELOC_MIPS_CALL_HI16))
252b5132 3151 || (ep->X_op == O_subtract
f6688943 3152 && *r == BFD_RELOC_PCREL_HI16_S)));
252b5132
RH
3153 continue;
3154
3155 case 'p':
3156 assert (ep != NULL);
3157 /*
3158 * This allows macro() to pass an immediate expression for
3159 * creating short branches without creating a symbol.
0b25d3e6
AO
3160 * Note that the expression still might come from the assembly
3161 * input, in which case the value is not checked for range nor
3162 * is a relocation entry generated (yuck).
252b5132
RH
3163 */
3164 if (ep->X_op == O_constant)
3165 {
3166 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
3167 ep = NULL;
3168 }
3169 else
0b25d3e6 3170 *r = BFD_RELOC_16_PCREL_S2;
252b5132
RH
3171 continue;
3172
3173 case 'a':
3174 assert (ep != NULL);
f6688943 3175 *r = BFD_RELOC_MIPS_JMP;
252b5132
RH
3176 continue;
3177
3178 case 'C':
3179 insn.insn_opcode |= va_arg (args, unsigned long);
3180 continue;
3181
3182 default:
3183 internalError ();
3184 }
3185 break;
3186 }
3187 va_end (args);
f6688943 3188 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 3189
4d7206a2 3190 append_insn (&insn, ep, r);
252b5132
RH
3191}
3192
3193static void
67c0d1eb 3194mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
17a2f251 3195 va_list args)
252b5132
RH
3196{
3197 struct mips_cl_insn insn;
f6688943
TS
3198 bfd_reloc_code_real_type r[3]
3199 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 3200
252b5132
RH
3201 insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
3202 assert (insn.insn_mo);
3203 assert (strcmp (name, insn.insn_mo->name) == 0);
3204
3205 while (strcmp (fmt, insn.insn_mo->args) != 0
3206 || insn.insn_mo->pinfo == INSN_MACRO)
3207 {
3208 ++insn.insn_mo;
3209 assert (insn.insn_mo->name);
3210 assert (strcmp (name, insn.insn_mo->name) == 0);
3211 }
3212
3213 insn.insn_opcode = insn.insn_mo->match;
b34976b6 3214 insn.use_extend = FALSE;
252b5132
RH
3215
3216 for (;;)
3217 {
3218 int c;
3219
3220 c = *fmt++;
3221 switch (c)
3222 {
3223 case '\0':
3224 break;
3225
3226 case ',':
3227 case '(':
3228 case ')':
3229 continue;
3230
3231 case 'y':
3232 case 'w':
3233 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
3234 continue;
3235
3236 case 'x':
3237 case 'v':
3238 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
3239 continue;
3240
3241 case 'z':
3242 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
3243 continue;
3244
3245 case 'Z':
3246 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
3247 continue;
3248
3249 case '0':
3250 case 'S':
3251 case 'P':
3252 case 'R':
3253 continue;
3254
3255 case 'X':
3256 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
3257 continue;
3258
3259 case 'Y':
3260 {
3261 int regno;
3262
3263 regno = va_arg (args, int);
3264 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
3265 insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
3266 }
3267 continue;
3268
3269 case '<':
3270 case '>':
3271 case '4':
3272 case '5':
3273 case 'H':
3274 case 'W':
3275 case 'D':
3276 case 'j':
3277 case '8':
3278 case 'V':
3279 case 'C':
3280 case 'U':
3281 case 'k':
3282 case 'K':
3283 case 'p':
3284 case 'q':
3285 {
3286 assert (ep != NULL);
3287
3288 if (ep->X_op != O_constant)
874e8986 3289 *r = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
3290 else
3291 {
b34976b6
AM
3292 mips16_immed (NULL, 0, c, ep->X_add_number, FALSE, FALSE,
3293 FALSE, &insn.insn_opcode, &insn.use_extend,
c4e7957c 3294 &insn.extend);
252b5132 3295 ep = NULL;
f6688943 3296 *r = BFD_RELOC_UNUSED;
252b5132
RH
3297 }
3298 }
3299 continue;
3300
3301 case '6':
3302 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
3303 continue;
3304 }
3305
3306 break;
3307 }
3308
f6688943 3309 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 3310
4d7206a2 3311 append_insn (&insn, ep, r);
252b5132
RH
3312}
3313
438c16b8
TS
3314/*
3315 * Generate a "jalr" instruction with a relocation hint to the called
3316 * function. This occurs in NewABI PIC code.
3317 */
3318static void
67c0d1eb 3319macro_build_jalr (expressionS *ep)
438c16b8 3320{
685736be 3321 char *f = NULL;
b34976b6 3322
438c16b8 3323 if (HAVE_NEWABI)
f21f8242 3324 {
cc3d92a5 3325 frag_grow (8);
f21f8242
AO
3326 f = frag_more (0);
3327 }
67c0d1eb 3328 macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
438c16b8 3329 if (HAVE_NEWABI)
f21f8242 3330 fix_new_exp (frag_now, f - frag_now->fr_literal,
a105a300 3331 4, ep, FALSE, BFD_RELOC_MIPS_JALR);
438c16b8
TS
3332}
3333
252b5132
RH
3334/*
3335 * Generate a "lui" instruction.
3336 */
3337static void
67c0d1eb 3338macro_build_lui (expressionS *ep, int regnum)
252b5132
RH
3339{
3340 expressionS high_expr;
3341 struct mips_cl_insn insn;
f6688943
TS
3342 bfd_reloc_code_real_type r[3]
3343 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
5a38dc70
AM
3344 const char *name = "lui";
3345 const char *fmt = "t,u";
252b5132
RH
3346
3347 assert (! mips_opts.mips16);
3348
4d7206a2 3349 high_expr = *ep;
252b5132
RH
3350
3351 if (high_expr.X_op == O_constant)
3352 {
3353 /* we can compute the instruction now without a relocation entry */
e7d556df
TS
3354 high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3355 >> 16) & 0xffff;
f6688943 3356 *r = BFD_RELOC_UNUSED;
252b5132 3357 }
78e1bb40 3358 else
252b5132
RH
3359 {
3360 assert (ep->X_op == O_symbol);
3361 /* _gp_disp is a special case, used from s_cpload. */
3362 assert (mips_pic == NO_PIC
78e1bb40
AO
3363 || (! HAVE_NEWABI
3364 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0));
f6688943 3365 *r = BFD_RELOC_HI16_S;
252b5132
RH
3366 }
3367
252b5132
RH
3368 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3369 assert (insn.insn_mo);
3370 assert (strcmp (name, insn.insn_mo->name) == 0);
3371 assert (strcmp (fmt, insn.insn_mo->args) == 0);
3372
3373 insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
f6688943 3374 if (*r == BFD_RELOC_UNUSED)
252b5132
RH
3375 {
3376 insn.insn_opcode |= high_expr.X_add_number;
4d7206a2 3377 append_insn (&insn, NULL, r);
252b5132
RH
3378 }
3379 else
4d7206a2 3380 append_insn (&insn, &high_expr, r);
252b5132
RH
3381}
3382
885add95
CD
3383/* Generate a sequence of instructions to do a load or store from a constant
3384 offset off of a base register (breg) into/from a target register (treg),
3385 using AT if necessary. */
3386static void
67c0d1eb
RS
3387macro_build_ldst_constoffset (expressionS *ep, const char *op,
3388 int treg, int breg, int dbl)
885add95
CD
3389{
3390 assert (ep->X_op == O_constant);
3391
256ab948 3392 /* Sign-extending 32-bit constants makes their handling easier. */
d17e7bce
TS
3393 if (! dbl && ! ((ep->X_add_number & ~((bfd_vma) 0x7fffffff))
3394 == ~((bfd_vma) 0x7fffffff)))
ae826530 3395 {
d17e7bce 3396 if (ep->X_add_number & ~((bfd_vma) 0xffffffff))
1b8e29e5 3397 as_bad (_("constant too large"));
ae826530 3398
1b8e29e5
TS
3399 ep->X_add_number = (((ep->X_add_number & 0xffffffff) ^ 0x80000000)
3400 - 0x80000000);
ae826530 3401 }
256ab948 3402
67c1ffbe 3403 /* Right now, this routine can only handle signed 32-bit constants. */
ecd13cd3 3404 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
885add95
CD
3405 as_warn (_("operand overflow"));
3406
3407 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
3408 {
3409 /* Signed 16-bit offset will fit in the op. Easy! */
67c0d1eb 3410 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
885add95
CD
3411 }
3412 else
3413 {
3414 /* 32-bit offset, need multiple instructions and AT, like:
3415 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
3416 addu $tempreg,$tempreg,$breg
3417 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
3418 to handle the complete offset. */
67c0d1eb
RS
3419 macro_build_lui (ep, AT);
3420 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
3421 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
885add95
CD
3422
3423 if (mips_opts.noat)
3424 as_warn (_("Macro used $at after \".set noat\""));
3425 }
3426}
3427
252b5132
RH
3428/* set_at()
3429 * Generates code to set the $at register to true (one)
3430 * if reg is less than the immediate expression.
3431 */
3432static void
67c0d1eb 3433set_at (int reg, int unsignedp)
252b5132
RH
3434{
3435 if (imm_expr.X_op == O_constant
3436 && imm_expr.X_add_number >= -0x8000
3437 && imm_expr.X_add_number < 0x8000)
67c0d1eb
RS
3438 macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
3439 AT, reg, BFD_RELOC_LO16);
252b5132
RH
3440 else
3441 {
67c0d1eb
RS
3442 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
3443 macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
252b5132
RH
3444 }
3445}
3446
13757d0c
TS
3447static void
3448normalize_constant_expr (expressionS *ex)
3449{
3450 if (ex->X_op == O_constant && HAVE_32BIT_GPRS)
3451 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
3452 - 0x80000000);
3453}
3454
252b5132
RH
3455/* Warn if an expression is not a constant. */
3456
3457static void
17a2f251 3458check_absolute_expr (struct mips_cl_insn *ip, expressionS *ex)
252b5132
RH
3459{
3460 if (ex->X_op == O_big)
3461 as_bad (_("unsupported large constant"));
3462 else if (ex->X_op != O_constant)
3463 as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
13757d0c
TS
3464
3465 normalize_constant_expr (ex);
252b5132
RH
3466}
3467
3468/* Count the leading zeroes by performing a binary chop. This is a
3469 bulky bit of source, but performance is a LOT better for the
3470 majority of values than a simple loop to count the bits:
3471 for (lcnt = 0; (lcnt < 32); lcnt++)
3472 if ((v) & (1 << (31 - lcnt)))
3473 break;
3474 However it is not code size friendly, and the gain will drop a bit
3475 on certain cached systems.
3476*/
3477#define COUNT_TOP_ZEROES(v) \
3478 (((v) & ~0xffff) == 0 \
3479 ? ((v) & ~0xff) == 0 \
3480 ? ((v) & ~0xf) == 0 \
3481 ? ((v) & ~0x3) == 0 \
3482 ? ((v) & ~0x1) == 0 \
3483 ? !(v) \
3484 ? 32 \
3485 : 31 \
3486 : 30 \
3487 : ((v) & ~0x7) == 0 \
3488 ? 29 \
3489 : 28 \
3490 : ((v) & ~0x3f) == 0 \
3491 ? ((v) & ~0x1f) == 0 \
3492 ? 27 \
3493 : 26 \
3494 : ((v) & ~0x7f) == 0 \
3495 ? 25 \
3496 : 24 \
3497 : ((v) & ~0xfff) == 0 \
3498 ? ((v) & ~0x3ff) == 0 \
3499 ? ((v) & ~0x1ff) == 0 \
3500 ? 23 \
3501 : 22 \
3502 : ((v) & ~0x7ff) == 0 \
3503 ? 21 \
3504 : 20 \
3505 : ((v) & ~0x3fff) == 0 \
3506 ? ((v) & ~0x1fff) == 0 \
3507 ? 19 \
3508 : 18 \
3509 : ((v) & ~0x7fff) == 0 \
3510 ? 17 \
3511 : 16 \
3512 : ((v) & ~0xffffff) == 0 \
3513 ? ((v) & ~0xfffff) == 0 \
3514 ? ((v) & ~0x3ffff) == 0 \
3515 ? ((v) & ~0x1ffff) == 0 \
3516 ? 15 \
3517 : 14 \
3518 : ((v) & ~0x7ffff) == 0 \
3519 ? 13 \
3520 : 12 \
3521 : ((v) & ~0x3fffff) == 0 \
3522 ? ((v) & ~0x1fffff) == 0 \
3523 ? 11 \
3524 : 10 \
3525 : ((v) & ~0x7fffff) == 0 \
3526 ? 9 \
3527 : 8 \
3528 : ((v) & ~0xfffffff) == 0 \
3529 ? ((v) & ~0x3ffffff) == 0 \
3530 ? ((v) & ~0x1ffffff) == 0 \
3531 ? 7 \
3532 : 6 \
3533 : ((v) & ~0x7ffffff) == 0 \
3534 ? 5 \
3535 : 4 \
3536 : ((v) & ~0x3fffffff) == 0 \
3537 ? ((v) & ~0x1fffffff) == 0 \
3538 ? 3 \
3539 : 2 \
3540 : ((v) & ~0x7fffffff) == 0 \
3541 ? 1 \
3542 : 0)
3543
3544/* load_register()
67c1ffbe 3545 * This routine generates the least number of instructions necessary to load
252b5132
RH
3546 * an absolute expression value into a register.
3547 */
3548static void
67c0d1eb 3549load_register (int reg, expressionS *ep, int dbl)
252b5132
RH
3550{
3551 int freg;
3552 expressionS hi32, lo32;
3553
3554 if (ep->X_op != O_big)
3555 {
3556 assert (ep->X_op == O_constant);
256ab948
TS
3557
3558 /* Sign-extending 32-bit constants makes their handling easier. */
d17e7bce
TS
3559 if (! dbl && ! ((ep->X_add_number & ~((bfd_vma) 0x7fffffff))
3560 == ~((bfd_vma) 0x7fffffff)))
ae826530 3561 {
d17e7bce 3562 if (ep->X_add_number & ~((bfd_vma) 0xffffffff))
1b8e29e5 3563 as_bad (_("constant too large"));
ae826530 3564
1b8e29e5
TS
3565 ep->X_add_number = (((ep->X_add_number & 0xffffffff) ^ 0x80000000)
3566 - 0x80000000);
ae826530 3567 }
256ab948
TS
3568
3569 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
252b5132
RH
3570 {
3571 /* We can handle 16 bit signed values with an addiu to
3572 $zero. No need to ever use daddiu here, since $zero and
3573 the result are always correct in 32 bit mode. */
67c0d1eb 3574 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
3575 return;
3576 }
3577 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3578 {
3579 /* We can handle 16 bit unsigned values with an ori to
3580 $zero. */
67c0d1eb 3581 macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
252b5132
RH
3582 return;
3583 }
256ab948 3584 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
252b5132
RH
3585 {
3586 /* 32 bit values require an lui. */
67c0d1eb 3587 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_HI16);
252b5132 3588 if ((ep->X_add_number & 0xffff) != 0)
67c0d1eb 3589 macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
252b5132
RH
3590 return;
3591 }
3592 }
3593
3594 /* The value is larger than 32 bits. */
3595
ca4e0257 3596 if (HAVE_32BIT_GPRS)
252b5132 3597 {
956cd1d6
TS
3598 as_bad (_("Number (0x%lx) larger than 32 bits"),
3599 (unsigned long) ep->X_add_number);
67c0d1eb 3600 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
3601 return;
3602 }
3603
3604 if (ep->X_op != O_big)
3605 {
3606 hi32 = *ep;
3607 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3608 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3609 hi32.X_add_number &= 0xffffffff;
3610 lo32 = *ep;
3611 lo32.X_add_number &= 0xffffffff;
3612 }
3613 else
3614 {
3615 assert (ep->X_add_number > 2);
3616 if (ep->X_add_number == 3)
3617 generic_bignum[3] = 0;
3618 else if (ep->X_add_number > 4)
3619 as_bad (_("Number larger than 64 bits"));
3620 lo32.X_op = O_constant;
3621 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3622 hi32.X_op = O_constant;
3623 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3624 }
3625
3626 if (hi32.X_add_number == 0)
3627 freg = 0;
3628 else
3629 {
3630 int shift, bit;
3631 unsigned long hi, lo;
3632
956cd1d6 3633 if (hi32.X_add_number == (offsetT) 0xffffffff)
beae10d5
KH
3634 {
3635 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3636 {
67c0d1eb 3637 macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
3638 return;
3639 }
3640 if (lo32.X_add_number & 0x80000000)
3641 {
67c0d1eb 3642 macro_build (&lo32, "lui", "t,u", reg, BFD_RELOC_HI16);
252b5132 3643 if (lo32.X_add_number & 0xffff)
67c0d1eb 3644 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
beae10d5
KH
3645 return;
3646 }
3647 }
252b5132
RH
3648
3649 /* Check for 16bit shifted constant. We know that hi32 is
3650 non-zero, so start the mask on the first bit of the hi32
3651 value. */
3652 shift = 17;
3653 do
beae10d5
KH
3654 {
3655 unsigned long himask, lomask;
3656
3657 if (shift < 32)
3658 {
3659 himask = 0xffff >> (32 - shift);
3660 lomask = (0xffff << shift) & 0xffffffff;
3661 }
3662 else
3663 {
3664 himask = 0xffff << (shift - 32);
3665 lomask = 0;
3666 }
3667 if ((hi32.X_add_number & ~(offsetT) himask) == 0
3668 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3669 {
3670 expressionS tmp;
3671
3672 tmp.X_op = O_constant;
3673 if (shift < 32)
3674 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3675 | (lo32.X_add_number >> shift));
3676 else
3677 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
67c0d1eb
RS
3678 macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
3679 macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", "d,w,<",
3680 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
3681 return;
3682 }
f9419b05 3683 ++shift;
beae10d5
KH
3684 }
3685 while (shift <= (64 - 16));
252b5132
RH
3686
3687 /* Find the bit number of the lowest one bit, and store the
3688 shifted value in hi/lo. */
3689 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3690 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3691 if (lo != 0)
3692 {
3693 bit = 0;
3694 while ((lo & 1) == 0)
3695 {
3696 lo >>= 1;
3697 ++bit;
3698 }
3699 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3700 hi >>= bit;
3701 }
3702 else
3703 {
3704 bit = 32;
3705 while ((hi & 1) == 0)
3706 {
3707 hi >>= 1;
3708 ++bit;
3709 }
3710 lo = hi;
3711 hi = 0;
3712 }
3713
3714 /* Optimize if the shifted value is a (power of 2) - 1. */
3715 if ((hi == 0 && ((lo + 1) & lo) == 0)
3716 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
beae10d5
KH
3717 {
3718 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
252b5132 3719 if (shift != 0)
beae10d5 3720 {
252b5132
RH
3721 expressionS tmp;
3722
3723 /* This instruction will set the register to be all
3724 ones. */
beae10d5
KH
3725 tmp.X_op = O_constant;
3726 tmp.X_add_number = (offsetT) -1;
67c0d1eb 3727 macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
3728 if (bit != 0)
3729 {
3730 bit += shift;
67c0d1eb
RS
3731 macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", "d,w,<",
3732 reg, reg, (bit >= 32) ? bit - 32 : bit);
beae10d5 3733 }
67c0d1eb
RS
3734 macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", "d,w,<",
3735 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
3736 return;
3737 }
3738 }
252b5132
RH
3739
3740 /* Sign extend hi32 before calling load_register, because we can
3741 generally get better code when we load a sign extended value. */
3742 if ((hi32.X_add_number & 0x80000000) != 0)
beae10d5 3743 hi32.X_add_number |= ~(offsetT) 0xffffffff;
67c0d1eb 3744 load_register (reg, &hi32, 0);
252b5132
RH
3745 freg = reg;
3746 }
3747 if ((lo32.X_add_number & 0xffff0000) == 0)
3748 {
3749 if (freg != 0)
3750 {
67c0d1eb 3751 macro_build (NULL, "dsll32", "d,w,<", reg, freg, 0);
252b5132
RH
3752 freg = reg;
3753 }
3754 }
3755 else
3756 {
3757 expressionS mid16;
3758
956cd1d6 3759 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
beae10d5 3760 {
67c0d1eb
RS
3761 macro_build (&lo32, "lui", "t,u", reg, BFD_RELOC_HI16);
3762 macro_build (NULL, "dsrl32", "d,w,<", reg, reg, 0);
beae10d5
KH
3763 return;
3764 }
252b5132
RH
3765
3766 if (freg != 0)
3767 {
67c0d1eb 3768 macro_build (NULL, "dsll", "d,w,<", reg, freg, 16);
252b5132
RH
3769 freg = reg;
3770 }
3771 mid16 = lo32;
3772 mid16.X_add_number >>= 16;
67c0d1eb
RS
3773 macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
3774 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
252b5132
RH
3775 freg = reg;
3776 }
3777 if ((lo32.X_add_number & 0xffff) != 0)
67c0d1eb 3778 macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
252b5132
RH
3779}
3780
269137b2
TS
3781static inline void
3782load_delay_nop (void)
3783{
3784 if (!gpr_interlocks)
3785 macro_build (NULL, "nop", "");
3786}
3787
252b5132
RH
3788/* Load an address into a register. */
3789
3790static void
67c0d1eb 3791load_address (int reg, expressionS *ep, int *used_at)
252b5132 3792{
252b5132
RH
3793 if (ep->X_op != O_constant
3794 && ep->X_op != O_symbol)
3795 {
3796 as_bad (_("expression too complex"));
3797 ep->X_op = O_constant;
3798 }
3799
3800 if (ep->X_op == O_constant)
3801 {
67c0d1eb 3802 load_register (reg, ep, HAVE_64BIT_ADDRESSES);
252b5132
RH
3803 return;
3804 }
3805
3806 if (mips_pic == NO_PIC)
3807 {
3808 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 3809 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
3810 Otherwise we want
3811 lui $reg,<sym> (BFD_RELOC_HI16_S)
3812 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
d6bc6245 3813 If we have an addend, we always use the latter form.
76b3015f 3814
d6bc6245
TS
3815 With 64bit address space and a usable $at we want
3816 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3817 lui $at,<sym> (BFD_RELOC_HI16_S)
3818 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3819 daddiu $at,<sym> (BFD_RELOC_LO16)
3820 dsll32 $reg,0
3a482fd5 3821 daddu $reg,$reg,$at
76b3015f 3822
c03099e6 3823 If $at is already in use, we use a path which is suboptimal
d6bc6245
TS
3824 on superscalar processors.
3825 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3826 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3827 dsll $reg,16
3828 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
3829 dsll $reg,16
3830 daddiu $reg,<sym> (BFD_RELOC_LO16)
3831 */
c9914766 3832 if (HAVE_64BIT_ADDRESSES)
d6bc6245 3833 {
e864ceca
RS
3834 /* ??? We don't provide a GP-relative alternative for these macros.
3835 It used not to be possible with the original relaxation code,
3836 but it could be done now. */
d6bc6245 3837
460597ba 3838 if (*used_at == 0 && ! mips_opts.noat)
d6bc6245 3839 {
67c0d1eb
RS
3840 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_HIGHEST);
3841 macro_build (ep, "lui", "t,u", AT, BFD_RELOC_HI16_S);
3842 macro_build (ep, "daddiu", "t,r,j", reg, reg,
3843 BFD_RELOC_MIPS_HIGHER);
3844 macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
3845 macro_build (NULL, "dsll32", "d,w,<", reg, reg, 0);
3846 macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
d6bc6245
TS
3847 *used_at = 1;
3848 }
3849 else
3850 {
67c0d1eb
RS
3851 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_HIGHEST);
3852 macro_build (ep, "daddiu", "t,r,j", reg, reg,
3853 BFD_RELOC_MIPS_HIGHER);
3854 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
3855 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
3856 macro_build (NULL, "dsll", "d,w,<", reg, reg, 16);
3857 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
d6bc6245
TS
3858 }
3859 }
252b5132
RH
3860 else
3861 {
d6bc6245
TS
3862 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3863 && ! nopic_need_relax (ep->X_add_symbol, 1))
3864 {
4d7206a2 3865 relax_start (ep->X_add_symbol);
67c0d1eb 3866 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
17a2f251 3867 mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 3868 relax_switch ();
d6bc6245 3869 }
67c0d1eb
RS
3870 macro_build_lui (ep, reg);
3871 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
3872 reg, reg, BFD_RELOC_LO16);
4d7206a2
RS
3873 if (mips_relax.sequence)
3874 relax_end ();
d6bc6245 3875 }
252b5132
RH
3876 }
3877 else if (mips_pic == SVR4_PIC && ! mips_big_got)
3878 {
3879 expressionS ex;
3880
3881 /* If this is a reference to an external symbol, we want
3882 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3883 Otherwise we want
3884 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3885 nop
3886 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
f5040a92
AO
3887 If there is a constant, it must be added in after.
3888
ed6fb7bd 3889 If we have NewABI, we want
f5040a92
AO
3890 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
3891 unless we're referencing a global symbol with a non-zero
3892 offset, in which case cst must be added separately. */
ed6fb7bd
SC
3893 if (HAVE_NEWABI)
3894 {
f5040a92
AO
3895 if (ep->X_add_number)
3896 {
4d7206a2 3897 ex.X_add_number = ep->X_add_number;
f5040a92 3898 ep->X_add_number = 0;
4d7206a2 3899 relax_start (ep->X_add_symbol);
67c0d1eb
RS
3900 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3901 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
3902 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3903 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3904 ex.X_op = O_constant;
67c0d1eb 3905 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 3906 reg, reg, BFD_RELOC_LO16);
f5040a92 3907 ep->X_add_number = ex.X_add_number;
4d7206a2 3908 relax_switch ();
f5040a92 3909 }
67c0d1eb 3910 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 3911 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2
RS
3912 if (mips_relax.sequence)
3913 relax_end ();
ed6fb7bd
SC
3914 }
3915 else
3916 {
f5040a92
AO
3917 ex.X_add_number = ep->X_add_number;
3918 ep->X_add_number = 0;
67c0d1eb
RS
3919 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
3920 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 3921 load_delay_nop ();
4d7206a2
RS
3922 relax_start (ep->X_add_symbol);
3923 relax_switch ();
67c0d1eb 3924 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 3925 BFD_RELOC_LO16);
4d7206a2 3926 relax_end ();
ed6fb7bd 3927
f5040a92
AO
3928 if (ex.X_add_number != 0)
3929 {
3930 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3931 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3932 ex.X_op = O_constant;
67c0d1eb 3933 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 3934 reg, reg, BFD_RELOC_LO16);
f5040a92 3935 }
252b5132
RH
3936 }
3937 }
3938 else if (mips_pic == SVR4_PIC)
3939 {
3940 expressionS ex;
252b5132
RH
3941
3942 /* This is the large GOT case. If this is a reference to an
3943 external symbol, we want
3944 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
3945 addu $reg,$reg,$gp
3946 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
f5040a92
AO
3947
3948 Otherwise, for a reference to a local symbol in old ABI, we want
252b5132
RH
3949 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3950 nop
3951 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
684022ea 3952 If there is a constant, it must be added in after.
f5040a92
AO
3953
3954 In the NewABI, for local symbols, with or without offsets, we want:
438c16b8
TS
3955 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
3956 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
f5040a92 3957 */
438c16b8
TS
3958 if (HAVE_NEWABI)
3959 {
4d7206a2 3960 ex.X_add_number = ep->X_add_number;
f5040a92 3961 ep->X_add_number = 0;
4d7206a2 3962 relax_start (ep->X_add_symbol);
67c0d1eb
RS
3963 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_GOT_HI16);
3964 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
3965 reg, reg, mips_gp_register);
3966 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
3967 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
f5040a92
AO
3968 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3969 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3970 else if (ex.X_add_number)
3971 {
3972 ex.X_op = O_constant;
67c0d1eb
RS
3973 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
3974 BFD_RELOC_LO16);
f5040a92
AO
3975 }
3976
3977 ep->X_add_number = ex.X_add_number;
4d7206a2 3978 relax_switch ();
67c0d1eb 3979 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 3980 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
67c0d1eb
RS
3981 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
3982 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 3983 relax_end ();
438c16b8 3984 }
252b5132 3985 else
438c16b8 3986 {
f5040a92
AO
3987 ex.X_add_number = ep->X_add_number;
3988 ep->X_add_number = 0;
4d7206a2 3989 relax_start (ep->X_add_symbol);
67c0d1eb
RS
3990 macro_build (ep, "lui", "t,u", reg, BFD_RELOC_MIPS_GOT_HI16);
3991 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
3992 reg, reg, mips_gp_register);
3993 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
3994 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
4d7206a2
RS
3995 relax_switch ();
3996 if (reg_needs_delay (mips_gp_register))
438c16b8
TS
3997 {
3998 /* We need a nop before loading from $gp. This special
3999 check is required because the lui which starts the main
4000 instruction stream does not refer to $gp, and so will not
4001 insert the nop which may be required. */
67c0d1eb 4002 macro_build (NULL, "nop", "");
438c16b8 4003 }
67c0d1eb 4004 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 4005 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 4006 load_delay_nop ();
67c0d1eb 4007 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 4008 BFD_RELOC_LO16);
4d7206a2 4009 relax_end ();
438c16b8 4010
f5040a92
AO
4011 if (ex.X_add_number != 0)
4012 {
4013 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4014 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4015 ex.X_op = O_constant;
67c0d1eb
RS
4016 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
4017 BFD_RELOC_LO16);
f5040a92 4018 }
252b5132
RH
4019 }
4020 }
4021 else if (mips_pic == EMBEDDED_PIC)
4022 {
4023 /* We always do
cdf6fd85 4024 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
c9914766 4025 */
67c0d1eb
RS
4026 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
4027 reg, mips_gp_register, BFD_RELOC_GPREL16);
252b5132
RH
4028 }
4029 else
4030 abort ();
4031}
4032
ea1fb5dc
RS
4033/* Move the contents of register SOURCE into register DEST. */
4034
4035static void
67c0d1eb 4036move_register (int dest, int source)
ea1fb5dc 4037{
67c0d1eb
RS
4038 macro_build (NULL, HAVE_32BIT_GPRS ? "addu" : "daddu", "d,v,t",
4039 dest, source, 0);
ea1fb5dc
RS
4040}
4041
4d7206a2 4042/* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
f6a22291
MR
4043 LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
4044 The two alternatives are:
4d7206a2
RS
4045
4046 Global symbol Local sybmol
4047 ------------- ------------
4048 lw DEST,%got(SYMBOL) lw DEST,%got(SYMBOL + OFFSET)
4049 ... ...
4050 addiu DEST,DEST,OFFSET addiu DEST,DEST,%lo(SYMBOL + OFFSET)
4051
4052 load_got_offset emits the first instruction and add_got_offset
f6a22291
MR
4053 emits the second for a 16-bit offset or add_got_offset_hilo emits
4054 a sequence to add a 32-bit offset using a scratch register. */
4d7206a2
RS
4055
4056static void
67c0d1eb 4057load_got_offset (int dest, expressionS *local)
4d7206a2
RS
4058{
4059 expressionS global;
4060
4061 global = *local;
4062 global.X_add_number = 0;
4063
4064 relax_start (local->X_add_symbol);
67c0d1eb
RS
4065 macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
4066 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2 4067 relax_switch ();
67c0d1eb
RS
4068 macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
4069 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2
RS
4070 relax_end ();
4071}
4072
4073static void
67c0d1eb 4074add_got_offset (int dest, expressionS *local)
4d7206a2
RS
4075{
4076 expressionS global;
4077
4078 global.X_op = O_constant;
4079 global.X_op_symbol = NULL;
4080 global.X_add_symbol = NULL;
4081 global.X_add_number = local->X_add_number;
4082
4083 relax_start (local->X_add_symbol);
67c0d1eb 4084 macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
4d7206a2
RS
4085 dest, dest, BFD_RELOC_LO16);
4086 relax_switch ();
67c0d1eb 4087 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
4d7206a2
RS
4088 relax_end ();
4089}
4090
f6a22291
MR
4091static void
4092add_got_offset_hilo (int dest, expressionS *local, int tmp)
4093{
4094 expressionS global;
4095 int hold_mips_optimize;
4096
4097 global.X_op = O_constant;
4098 global.X_op_symbol = NULL;
4099 global.X_add_symbol = NULL;
4100 global.X_add_number = local->X_add_number;
4101
4102 relax_start (local->X_add_symbol);
4103 load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
4104 relax_switch ();
4105 /* Set mips_optimize around the lui instruction to avoid
4106 inserting an unnecessary nop after the lw. */
4107 hold_mips_optimize = mips_optimize;
4108 mips_optimize = 2;
4109 macro_build_lui (&global, tmp);
4110 mips_optimize = hold_mips_optimize;
4111 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
4112 relax_end ();
4113
4114 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
4115}
4116
252b5132
RH
4117/*
4118 * Build macros
4119 * This routine implements the seemingly endless macro or synthesized
4120 * instructions and addressing modes in the mips assembly language. Many
4121 * of these macros are simple and are similar to each other. These could
67c1ffbe 4122 * probably be handled by some kind of table or grammar approach instead of
252b5132
RH
4123 * this verbose method. Others are not simple macros but are more like
4124 * optimizing code generation.
4125 * One interesting optimization is when several store macros appear
67c1ffbe 4126 * consecutively that would load AT with the upper half of the same address.
252b5132
RH
4127 * The ensuing load upper instructions are ommited. This implies some kind
4128 * of global optimization. We currently only optimize within a single macro.
4129 * For many of the load and store macros if the address is specified as a
4130 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
4131 * first load register 'at' with zero and use it as the base register. The
4132 * mips assembler simply uses register $zero. Just one tiny optimization
4133 * we're missing.
4134 */
4135static void
17a2f251 4136macro (struct mips_cl_insn *ip)
252b5132
RH
4137{
4138 register int treg, sreg, dreg, breg;
4139 int tempreg;
4140 int mask;
43841e91 4141 int used_at = 0;
252b5132
RH
4142 expressionS expr1;
4143 const char *s;
4144 const char *s2;
4145 const char *fmt;
4146 int likely = 0;
4147 int dbl = 0;
4148 int coproc = 0;
4149 int lr = 0;
4150 int imm = 0;
1abe91b1 4151 int call = 0;
252b5132 4152 int off;
67c0d1eb 4153 offsetT maxnum;
252b5132 4154 bfd_reloc_code_real_type r;
252b5132
RH
4155 int hold_mips_optimize;
4156
4157 assert (! mips_opts.mips16);
4158
4159 treg = (ip->insn_opcode >> 16) & 0x1f;
4160 dreg = (ip->insn_opcode >> 11) & 0x1f;
4161 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
4162 mask = ip->insn_mo->mask;
4163
4164 expr1.X_op = O_constant;
4165 expr1.X_op_symbol = NULL;
4166 expr1.X_add_symbol = NULL;
4167 expr1.X_add_number = 1;
4168
4169 switch (mask)
4170 {
4171 case M_DABS:
4172 dbl = 1;
4173 case M_ABS:
4174 /* bgez $a0,.+12
4175 move v0,$a0
4176 sub v0,$zero,$a0
4177 */
4178
b34976b6 4179 mips_emit_delays (TRUE);
252b5132
RH
4180 ++mips_opts.noreorder;
4181 mips_any_noreorder = 1;
4182
4183 expr1.X_add_number = 8;
67c0d1eb 4184 macro_build (&expr1, "bgez", "s,p", sreg);
252b5132 4185 if (dreg == sreg)
67c0d1eb 4186 macro_build (NULL, "nop", "", 0);
252b5132 4187 else
67c0d1eb
RS
4188 move_register (dreg, sreg);
4189 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
252b5132
RH
4190
4191 --mips_opts.noreorder;
4192 return;
4193
4194 case M_ADD_I:
4195 s = "addi";
4196 s2 = "add";
4197 goto do_addi;
4198 case M_ADDU_I:
4199 s = "addiu";
4200 s2 = "addu";
4201 goto do_addi;
4202 case M_DADD_I:
4203 dbl = 1;
4204 s = "daddi";
4205 s2 = "dadd";
4206 goto do_addi;
4207 case M_DADDU_I:
4208 dbl = 1;
4209 s = "daddiu";
4210 s2 = "daddu";
4211 do_addi:
4212 if (imm_expr.X_op == O_constant
4213 && imm_expr.X_add_number >= -0x8000
4214 && imm_expr.X_add_number < 0x8000)
4215 {
67c0d1eb 4216 macro_build (&imm_expr, s, "t,r,j", treg, sreg, BFD_RELOC_LO16);
252b5132
RH
4217 return;
4218 }
67c0d1eb
RS
4219 load_register (AT, &imm_expr, dbl);
4220 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
252b5132
RH
4221 break;
4222
4223 case M_AND_I:
4224 s = "andi";
4225 s2 = "and";
4226 goto do_bit;
4227 case M_OR_I:
4228 s = "ori";
4229 s2 = "or";
4230 goto do_bit;
4231 case M_NOR_I:
4232 s = "";
4233 s2 = "nor";
4234 goto do_bit;
4235 case M_XOR_I:
4236 s = "xori";
4237 s2 = "xor";
4238 do_bit:
4239 if (imm_expr.X_op == O_constant
4240 && imm_expr.X_add_number >= 0
4241 && imm_expr.X_add_number < 0x10000)
4242 {
4243 if (mask != M_NOR_I)
67c0d1eb 4244 macro_build (&imm_expr, s, "t,r,i", treg, sreg, BFD_RELOC_LO16);
252b5132
RH
4245 else
4246 {
67c0d1eb
RS
4247 macro_build (&imm_expr, "ori", "t,r,i",
4248 treg, sreg, BFD_RELOC_LO16);
4249 macro_build (NULL, "nor", "d,v,t", treg, treg, 0);
252b5132
RH
4250 }
4251 return;
4252 }
4253
67c0d1eb
RS
4254 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
4255 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
252b5132
RH
4256 break;
4257
4258 case M_BEQ_I:
4259 s = "beq";
4260 goto beq_i;
4261 case M_BEQL_I:
4262 s = "beql";
4263 likely = 1;
4264 goto beq_i;
4265 case M_BNE_I:
4266 s = "bne";
4267 goto beq_i;
4268 case M_BNEL_I:
4269 s = "bnel";
4270 likely = 1;
4271 beq_i:
4272 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4273 {
67c0d1eb 4274 macro_build (&offset_expr, s, "s,t,p", sreg, 0);
252b5132
RH
4275 return;
4276 }
67c0d1eb
RS
4277 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
4278 macro_build (&offset_expr, s, "s,t,p", sreg, AT);
252b5132
RH
4279 break;
4280
4281 case M_BGEL:
4282 likely = 1;
4283 case M_BGE:
4284 if (treg == 0)
4285 {
67c0d1eb 4286 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", sreg);
252b5132
RH
4287 return;
4288 }
4289 if (sreg == 0)
4290 {
67c0d1eb 4291 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", treg);
252b5132
RH
4292 return;
4293 }
67c0d1eb
RS
4294 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
4295 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4296 break;
4297
4298 case M_BGTL_I:
4299 likely = 1;
4300 case M_BGT_I:
4301 /* check for > max integer */
4302 maxnum = 0x7fffffff;
ca4e0257 4303 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
4304 {
4305 maxnum <<= 16;
4306 maxnum |= 0xffff;
4307 maxnum <<= 16;
4308 maxnum |= 0xffff;
4309 }
4310 if (imm_expr.X_op == O_constant
4311 && imm_expr.X_add_number >= maxnum
ca4e0257 4312 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
4313 {
4314 do_false:
4315 /* result is always false */
4316 if (! likely)
67c0d1eb 4317 macro_build (NULL, "nop", "", 0);
252b5132 4318 else
67c0d1eb 4319 macro_build (&offset_expr, "bnel", "s,t,p", 0, 0);
252b5132
RH
4320 return;
4321 }
4322 if (imm_expr.X_op != O_constant)
4323 as_bad (_("Unsupported large constant"));
f9419b05 4324 ++imm_expr.X_add_number;
252b5132
RH
4325 /* FALLTHROUGH */
4326 case M_BGE_I:
4327 case M_BGEL_I:
4328 if (mask == M_BGEL_I)
4329 likely = 1;
4330 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4331 {
67c0d1eb 4332 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", sreg);
252b5132
RH
4333 return;
4334 }
4335 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4336 {
67c0d1eb 4337 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", sreg);
252b5132
RH
4338 return;
4339 }
4340 maxnum = 0x7fffffff;
ca4e0257 4341 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
4342 {
4343 maxnum <<= 16;
4344 maxnum |= 0xffff;
4345 maxnum <<= 16;
4346 maxnum |= 0xffff;
4347 }
4348 maxnum = - maxnum - 1;
4349 if (imm_expr.X_op == O_constant
4350 && imm_expr.X_add_number <= maxnum
ca4e0257 4351 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
4352 {
4353 do_true:
4354 /* result is always true */
4355 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
67c0d1eb 4356 macro_build (&offset_expr, "b", "p");
252b5132
RH
4357 return;
4358 }
67c0d1eb
RS
4359 set_at (sreg, 0);
4360 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4361 break;
4362
4363 case M_BGEUL:
4364 likely = 1;
4365 case M_BGEU:
4366 if (treg == 0)
4367 goto do_true;
4368 if (sreg == 0)
4369 {
67c0d1eb 4370 macro_build (&offset_expr, likely ? "beql" : "beq",
17a2f251 4371 "s,t,p", 0, treg);
252b5132
RH
4372 return;
4373 }
67c0d1eb
RS
4374 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
4375 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4376 break;
4377
4378 case M_BGTUL_I:
4379 likely = 1;
4380 case M_BGTU_I:
4381 if (sreg == 0
ca4e0257 4382 || (HAVE_32BIT_GPRS
252b5132 4383 && imm_expr.X_op == O_constant
956cd1d6 4384 && imm_expr.X_add_number == (offsetT) 0xffffffff))
252b5132
RH
4385 goto do_false;
4386 if (imm_expr.X_op != O_constant)
4387 as_bad (_("Unsupported large constant"));
f9419b05 4388 ++imm_expr.X_add_number;
252b5132
RH
4389 /* FALLTHROUGH */
4390 case M_BGEU_I:
4391 case M_BGEUL_I:
4392 if (mask == M_BGEUL_I)
4393 likely = 1;
4394 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4395 goto do_true;
4396 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4397 {
67c0d1eb 4398 macro_build (&offset_expr, likely ? "bnel" : "bne",
17a2f251 4399 "s,t,p", sreg, 0);
252b5132
RH
4400 return;
4401 }
67c0d1eb
RS
4402 set_at (sreg, 1);
4403 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4404 break;
4405
4406 case M_BGTL:
4407 likely = 1;
4408 case M_BGT:
4409 if (treg == 0)
4410 {
67c0d1eb 4411 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", sreg);
252b5132
RH
4412 return;
4413 }
4414 if (sreg == 0)
4415 {
67c0d1eb 4416 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", treg);
252b5132
RH
4417 return;
4418 }
67c0d1eb
RS
4419 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
4420 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4421 break;
4422
4423 case M_BGTUL:
4424 likely = 1;
4425 case M_BGTU:
4426 if (treg == 0)
4427 {
67c0d1eb 4428 macro_build (&offset_expr, likely ? "bnel" : "bne",
17a2f251 4429 "s,t,p", sreg, 0);
252b5132
RH
4430 return;
4431 }
4432 if (sreg == 0)
4433 goto do_false;
67c0d1eb
RS
4434 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
4435 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4436 break;
4437
4438 case M_BLEL:
4439 likely = 1;
4440 case M_BLE:
4441 if (treg == 0)
4442 {
67c0d1eb 4443 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", sreg);
252b5132
RH
4444 return;
4445 }
4446 if (sreg == 0)
4447 {
67c0d1eb 4448 macro_build (&offset_expr, likely ? "bgezl" : "bgez", "s,p", treg);
252b5132
RH
4449 return;
4450 }
67c0d1eb
RS
4451 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
4452 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4453 break;
4454
4455 case M_BLEL_I:
4456 likely = 1;
4457 case M_BLE_I:
4458 maxnum = 0x7fffffff;
ca4e0257 4459 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
4460 {
4461 maxnum <<= 16;
4462 maxnum |= 0xffff;
4463 maxnum <<= 16;
4464 maxnum |= 0xffff;
4465 }
4466 if (imm_expr.X_op == O_constant
4467 && imm_expr.X_add_number >= maxnum
ca4e0257 4468 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
4469 goto do_true;
4470 if (imm_expr.X_op != O_constant)
4471 as_bad (_("Unsupported large constant"));
f9419b05 4472 ++imm_expr.X_add_number;
252b5132
RH
4473 /* FALLTHROUGH */
4474 case M_BLT_I:
4475 case M_BLTL_I:
4476 if (mask == M_BLTL_I)
4477 likely = 1;
4478 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4479 {
67c0d1eb 4480 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", sreg);
252b5132
RH
4481 return;
4482 }
4483 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4484 {
67c0d1eb 4485 macro_build (&offset_expr, likely ? "blezl" : "blez", "s,p", sreg);
252b5132
RH
4486 return;
4487 }
67c0d1eb
RS
4488 set_at (sreg, 0);
4489 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4490 break;
4491
4492 case M_BLEUL:
4493 likely = 1;
4494 case M_BLEU:
4495 if (treg == 0)
4496 {
67c0d1eb 4497 macro_build (&offset_expr, likely ? "beql" : "beq",
17a2f251 4498 "s,t,p", sreg, 0);
252b5132
RH
4499 return;
4500 }
4501 if (sreg == 0)
4502 goto do_true;
67c0d1eb
RS
4503 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
4504 macro_build (&offset_expr, likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4505 break;
4506
4507 case M_BLEUL_I:
4508 likely = 1;
4509 case M_BLEU_I:
4510 if (sreg == 0
ca4e0257 4511 || (HAVE_32BIT_GPRS
252b5132 4512 && imm_expr.X_op == O_constant
956cd1d6 4513 && imm_expr.X_add_number == (offsetT) 0xffffffff))
252b5132
RH
4514 goto do_true;
4515 if (imm_expr.X_op != O_constant)
4516 as_bad (_("Unsupported large constant"));
f9419b05 4517 ++imm_expr.X_add_number;
252b5132
RH
4518 /* FALLTHROUGH */
4519 case M_BLTU_I:
4520 case M_BLTUL_I:
4521 if (mask == M_BLTUL_I)
4522 likely = 1;
4523 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4524 goto do_false;
4525 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4526 {
67c0d1eb 4527 macro_build (&offset_expr, likely ? "beql" : "beq",
252b5132
RH
4528 "s,t,p", sreg, 0);
4529 return;
4530 }
67c0d1eb
RS
4531 set_at (sreg, 1);
4532 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4533 break;
4534
4535 case M_BLTL:
4536 likely = 1;
4537 case M_BLT:
4538 if (treg == 0)
4539 {
67c0d1eb 4540 macro_build (&offset_expr, likely ? "bltzl" : "bltz", "s,p", sreg);
252b5132
RH
4541 return;
4542 }
4543 if (sreg == 0)
4544 {
67c0d1eb 4545 macro_build (&offset_expr, likely ? "bgtzl" : "bgtz", "s,p", treg);
252b5132
RH
4546 return;
4547 }
67c0d1eb
RS
4548 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
4549 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4550 break;
4551
4552 case M_BLTUL:
4553 likely = 1;
4554 case M_BLTU:
4555 if (treg == 0)
4556 goto do_false;
4557 if (sreg == 0)
4558 {
67c0d1eb 4559 macro_build (&offset_expr, likely ? "bnel" : "bne",
17a2f251 4560 "s,t,p", 0, treg);
252b5132
RH
4561 return;
4562 }
67c0d1eb
RS
4563 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
4564 macro_build (&offset_expr, likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4565 break;
4566
5f74bc13
CD
4567 case M_DEXT:
4568 {
4569 unsigned long pos;
4570 unsigned long size;
4571
4572 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
4573 {
4574 as_bad (_("Unsupported large constant"));
4575 pos = size = 1;
4576 }
4577 else
4578 {
4579 pos = (unsigned long) imm_expr.X_add_number;
4580 size = (unsigned long) imm2_expr.X_add_number;
4581 }
4582
4583 if (pos > 63)
4584 {
4585 as_bad (_("Improper position (%lu)"), pos);
4586 pos = 1;
4587 }
4588 if (size == 0 || size > 64
4589 || (pos + size - 1) > 63)
4590 {
4591 as_bad (_("Improper extract size (%lu, position %lu)"),
4592 size, pos);
4593 size = 1;
4594 }
4595
4596 if (size <= 32 && pos < 32)
4597 {
4598 s = "dext";
4599 fmt = "t,r,+A,+C";
4600 }
4601 else if (size <= 32)
4602 {
4603 s = "dextu";
4604 fmt = "t,r,+E,+H";
4605 }
4606 else
4607 {
4608 s = "dextm";
4609 fmt = "t,r,+A,+G";
4610 }
67c0d1eb 4611 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, pos, size - 1);
5f74bc13
CD
4612 }
4613 return;
4614
4615 case M_DINS:
4616 {
4617 unsigned long pos;
4618 unsigned long size;
4619
4620 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
4621 {
4622 as_bad (_("Unsupported large constant"));
4623 pos = size = 1;
4624 }
4625 else
4626 {
4627 pos = (unsigned long) imm_expr.X_add_number;
4628 size = (unsigned long) imm2_expr.X_add_number;
4629 }
4630
4631 if (pos > 63)
4632 {
4633 as_bad (_("Improper position (%lu)"), pos);
4634 pos = 1;
4635 }
4636 if (size == 0 || size > 64
4637 || (pos + size - 1) > 63)
4638 {
4639 as_bad (_("Improper insert size (%lu, position %lu)"),
4640 size, pos);
4641 size = 1;
4642 }
4643
4644 if (pos < 32 && (pos + size - 1) < 32)
4645 {
4646 s = "dins";
4647 fmt = "t,r,+A,+B";
4648 }
4649 else if (pos >= 32)
4650 {
4651 s = "dinsu";
4652 fmt = "t,r,+E,+F";
4653 }
4654 else
4655 {
4656 s = "dinsm";
4657 fmt = "t,r,+A,+F";
4658 }
67c0d1eb
RS
4659 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, pos,
4660 pos + size - 1);
5f74bc13
CD
4661 }
4662 return;
4663
252b5132
RH
4664 case M_DDIV_3:
4665 dbl = 1;
4666 case M_DIV_3:
4667 s = "mflo";
4668 goto do_div3;
4669 case M_DREM_3:
4670 dbl = 1;
4671 case M_REM_3:
4672 s = "mfhi";
4673 do_div3:
4674 if (treg == 0)
4675 {
4676 as_warn (_("Divide by zero."));
4677 if (mips_trap)
67c0d1eb 4678 macro_build (NULL, "teq", "s,t,q", 0, 0, 7);
252b5132 4679 else
67c0d1eb 4680 macro_build (NULL, "break", "c", 7);
252b5132
RH
4681 return;
4682 }
4683
b34976b6 4684 mips_emit_delays (TRUE);
252b5132
RH
4685 ++mips_opts.noreorder;
4686 mips_any_noreorder = 1;
4687 if (mips_trap)
4688 {
67c0d1eb
RS
4689 macro_build (NULL, "teq", "s,t,q", treg, 0, 7);
4690 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
252b5132
RH
4691 }
4692 else
4693 {
4694 expr1.X_add_number = 8;
67c0d1eb
RS
4695 macro_build (&expr1, "bne", "s,t,p", treg, 0);
4696 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
4697 macro_build (NULL, "break", "c", 7);
252b5132
RH
4698 }
4699 expr1.X_add_number = -1;
f6a22291 4700 load_register (AT, &expr1, dbl);
252b5132 4701 expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
67c0d1eb 4702 macro_build (&expr1, "bne", "s,t,p", treg, AT);
252b5132
RH
4703 if (dbl)
4704 {
4705 expr1.X_add_number = 1;
f6a22291 4706 load_register (AT, &expr1, dbl);
67c0d1eb 4707 macro_build (NULL, "dsll32", "d,w,<", AT, AT, 31);
252b5132
RH
4708 }
4709 else
4710 {
4711 expr1.X_add_number = 0x80000000;
67c0d1eb 4712 macro_build (&expr1, "lui", "t,u", AT, BFD_RELOC_HI16);
252b5132
RH
4713 }
4714 if (mips_trap)
4715 {
67c0d1eb 4716 macro_build (NULL, "teq", "s,t,q", sreg, AT, 6);
252b5132
RH
4717 /* We want to close the noreorder block as soon as possible, so
4718 that later insns are available for delay slot filling. */
4719 --mips_opts.noreorder;
4720 }
4721 else
4722 {
4723 expr1.X_add_number = 8;
67c0d1eb
RS
4724 macro_build (&expr1, "bne", "s,t,p", sreg, AT);
4725 macro_build (NULL, "nop", "", 0);
252b5132
RH
4726
4727 /* We want to close the noreorder block as soon as possible, so
4728 that later insns are available for delay slot filling. */
4729 --mips_opts.noreorder;
4730
67c0d1eb 4731 macro_build (NULL, "break", "c", 6);
252b5132 4732 }
67c0d1eb 4733 macro_build (NULL, s, "d", dreg);
252b5132
RH
4734 break;
4735
4736 case M_DIV_3I:
4737 s = "div";
4738 s2 = "mflo";
4739 goto do_divi;
4740 case M_DIVU_3I:
4741 s = "divu";
4742 s2 = "mflo";
4743 goto do_divi;
4744 case M_REM_3I:
4745 s = "div";
4746 s2 = "mfhi";
4747 goto do_divi;
4748 case M_REMU_3I:
4749 s = "divu";
4750 s2 = "mfhi";
4751 goto do_divi;
4752 case M_DDIV_3I:
4753 dbl = 1;
4754 s = "ddiv";
4755 s2 = "mflo";
4756 goto do_divi;
4757 case M_DDIVU_3I:
4758 dbl = 1;
4759 s = "ddivu";
4760 s2 = "mflo";
4761 goto do_divi;
4762 case M_DREM_3I:
4763 dbl = 1;
4764 s = "ddiv";
4765 s2 = "mfhi";
4766 goto do_divi;
4767 case M_DREMU_3I:
4768 dbl = 1;
4769 s = "ddivu";
4770 s2 = "mfhi";
4771 do_divi:
4772 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4773 {
4774 as_warn (_("Divide by zero."));
4775 if (mips_trap)
67c0d1eb 4776 macro_build (NULL, "teq", "s,t,q", 0, 0, 7);
252b5132 4777 else
67c0d1eb 4778 macro_build (NULL, "break", "c", 7);
252b5132
RH
4779 return;
4780 }
4781 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4782 {
4783 if (strcmp (s2, "mflo") == 0)
67c0d1eb 4784 move_register (dreg, sreg);
252b5132 4785 else
67c0d1eb 4786 move_register (dreg, 0);
252b5132
RH
4787 return;
4788 }
4789 if (imm_expr.X_op == O_constant
4790 && imm_expr.X_add_number == -1
4791 && s[strlen (s) - 1] != 'u')
4792 {
4793 if (strcmp (s2, "mflo") == 0)
4794 {
67c0d1eb 4795 macro_build (NULL, dbl ? "dneg" : "neg", "d,w", dreg, sreg);
252b5132
RH
4796 }
4797 else
67c0d1eb 4798 move_register (dreg, 0);
252b5132
RH
4799 return;
4800 }
4801
67c0d1eb
RS
4802 load_register (AT, &imm_expr, dbl);
4803 macro_build (NULL, s, "z,s,t", sreg, AT);
4804 macro_build (NULL, s2, "d", dreg);
252b5132
RH
4805 break;
4806
4807 case M_DIVU_3:
4808 s = "divu";
4809 s2 = "mflo";
4810 goto do_divu3;
4811 case M_REMU_3:
4812 s = "divu";
4813 s2 = "mfhi";
4814 goto do_divu3;
4815 case M_DDIVU_3:
4816 s = "ddivu";
4817 s2 = "mflo";
4818 goto do_divu3;
4819 case M_DREMU_3:
4820 s = "ddivu";
4821 s2 = "mfhi";
4822 do_divu3:
b34976b6 4823 mips_emit_delays (TRUE);
252b5132
RH
4824 ++mips_opts.noreorder;
4825 mips_any_noreorder = 1;
4826 if (mips_trap)
4827 {
67c0d1eb
RS
4828 macro_build (NULL, "teq", "s,t,q", treg, 0, 7);
4829 macro_build (NULL, s, "z,s,t", sreg, treg);
252b5132
RH
4830 /* We want to close the noreorder block as soon as possible, so
4831 that later insns are available for delay slot filling. */
4832 --mips_opts.noreorder;
4833 }
4834 else
4835 {
4836 expr1.X_add_number = 8;
67c0d1eb
RS
4837 macro_build (&expr1, "bne", "s,t,p", treg, 0);
4838 macro_build (NULL, s, "z,s,t", sreg, treg);
252b5132
RH
4839
4840 /* We want to close the noreorder block as soon as possible, so
4841 that later insns are available for delay slot filling. */
4842 --mips_opts.noreorder;
67c0d1eb 4843 macro_build (NULL, "break", "c", 7);
252b5132 4844 }
67c0d1eb 4845 macro_build (NULL, s2, "d", dreg);
252b5132
RH
4846 return;
4847
1abe91b1
MR
4848 case M_DLCA_AB:
4849 dbl = 1;
4850 case M_LCA_AB:
4851 call = 1;
4852 goto do_la;
252b5132
RH
4853 case M_DLA_AB:
4854 dbl = 1;
4855 case M_LA_AB:
1abe91b1 4856 do_la:
252b5132
RH
4857 /* Load the address of a symbol into a register. If breg is not
4858 zero, we then add a base register to it. */
4859
3bec30a8
TS
4860 if (dbl && HAVE_32BIT_GPRS)
4861 as_warn (_("dla used to load 32-bit register"));
4862
c90bbe5b 4863 if (! dbl && HAVE_64BIT_OBJECTS)
3bec30a8
TS
4864 as_warn (_("la used to load 64-bit address"));
4865
0c11417f
MR
4866 if (offset_expr.X_op == O_constant
4867 && offset_expr.X_add_number >= -0x8000
4868 && offset_expr.X_add_number < 0x8000)
4869 {
67c0d1eb 4870 macro_build (&offset_expr,
b7c7d6c1 4871 (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
17a2f251 4872 "t,r,j", treg, sreg, BFD_RELOC_LO16);
0c11417f
MR
4873 return;
4874 }
4875
afdbd6d0
CD
4876 if (treg == breg)
4877 {
4878 tempreg = AT;
4879 used_at = 1;
4880 }
4881 else
4882 {
4883 tempreg = treg;
4884 used_at = 0;
4885 }
4886
252b5132
RH
4887 /* When generating embedded PIC code, we permit expressions of
4888 the form
afdbd6d0
CD
4889 la $treg,foo-bar
4890 la $treg,foo-bar($breg)
bb2d6cd7 4891 where bar is an address in the current section. These are used
252b5132
RH
4892 when getting the addresses of functions. We don't permit
4893 X_add_number to be non-zero, because if the symbol is
4894 external the relaxing code needs to know that any addend is
4895 purely the offset to X_op_symbol. */
4896 if (mips_pic == EMBEDDED_PIC
4897 && offset_expr.X_op == O_subtract
49309057 4898 && (symbol_constant_p (offset_expr.X_op_symbol)
bb2d6cd7 4899 ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
49309057
ILT
4900 : (symbol_equated_p (offset_expr.X_op_symbol)
4901 && (S_GET_SEGMENT
4902 (symbol_get_value_expression (offset_expr.X_op_symbol)
4903 ->X_add_symbol)
bb2d6cd7 4904 == now_seg)))
bb2d6cd7
GK
4905 && (offset_expr.X_add_number == 0
4906 || OUTPUT_FLAVOR == bfd_target_elf_flavour))
252b5132 4907 {
afdbd6d0
CD
4908 if (breg == 0)
4909 {
4910 tempreg = treg;
4911 used_at = 0;
67c0d1eb
RS
4912 macro_build (&offset_expr, "lui", "t,u",
4913 tempreg, BFD_RELOC_PCREL_HI16_S);
afdbd6d0
CD
4914 }
4915 else
4916 {
67c0d1eb
RS
4917 macro_build (&offset_expr, "lui", "t,u",
4918 tempreg, BFD_RELOC_PCREL_HI16_S);
4919 macro_build (NULL,
4d34fb5f 4920 (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu",
afdbd6d0
CD
4921 "d,v,t", tempreg, tempreg, breg);
4922 }
67c0d1eb 4923 macro_build (&offset_expr,
4d34fb5f 4924 (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
17a2f251 4925 "t,r,j", treg, tempreg, BFD_RELOC_PCREL_LO16);
afdbd6d0
CD
4926 if (! used_at)
4927 return;
4928 break;
252b5132
RH
4929 }
4930
4931 if (offset_expr.X_op != O_symbol
4932 && offset_expr.X_op != O_constant)
4933 {
4934 as_bad (_("expression too complex"));
4935 offset_expr.X_op = O_constant;
4936 }
4937
252b5132 4938 if (offset_expr.X_op == O_constant)
67c0d1eb 4939 load_register (tempreg, &offset_expr,
4d34fb5f
TS
4940 ((mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
4941 ? (dbl || HAVE_64BIT_ADDRESSES)
4942 : HAVE_64BIT_ADDRESSES));
252b5132
RH
4943 else if (mips_pic == NO_PIC)
4944 {
d6bc6245 4945 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 4946 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
4947 Otherwise we want
4948 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
4949 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4950 If we have a constant, we need two instructions anyhow,
d6bc6245 4951 so we may as well always use the latter form.
76b3015f 4952
d6bc6245
TS
4953 With 64bit address space and a usable $at we want
4954 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4955 lui $at,<sym> (BFD_RELOC_HI16_S)
4956 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4957 daddiu $at,<sym> (BFD_RELOC_LO16)
4958 dsll32 $tempreg,0
3a482fd5 4959 daddu $tempreg,$tempreg,$at
76b3015f 4960
c03099e6 4961 If $at is already in use, we use a path which is suboptimal
d6bc6245
TS
4962 on superscalar processors.
4963 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4964 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4965 dsll $tempreg,16
4966 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
4967 dsll $tempreg,16
4968 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
4969 */
d6bc6245 4970 if (HAVE_64BIT_ADDRESSES)
252b5132 4971 {
e864ceca
RS
4972 /* ??? We don't provide a GP-relative alternative for
4973 these macros. It used not to be possible with the
4974 original relaxation code, but it could be done now. */
d6bc6245 4975
460597ba 4976 if (used_at == 0 && ! mips_opts.noat)
98d3f06f 4977 {
67c0d1eb 4978 macro_build (&offset_expr, "lui", "t,u",
17a2f251 4979 tempreg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb 4980 macro_build (&offset_expr, "lui", "t,u",
17a2f251 4981 AT, BFD_RELOC_HI16_S);
67c0d1eb 4982 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 4983 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
67c0d1eb 4984 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 4985 AT, AT, BFD_RELOC_LO16);
67c0d1eb
RS
4986 macro_build (NULL, "dsll32", "d,w,<", tempreg, tempreg, 0);
4987 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
98d3f06f
KH
4988 used_at = 1;
4989 }
4990 else
4991 {
67c0d1eb 4992 macro_build (&offset_expr, "lui", "t,u",
17a2f251 4993 tempreg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb 4994 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 4995 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
67c0d1eb
RS
4996 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
4997 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 4998 tempreg, tempreg, BFD_RELOC_HI16_S);
67c0d1eb
RS
4999 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
5000 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 5001 tempreg, tempreg, BFD_RELOC_LO16);
98d3f06f
KH
5002 }
5003 }
5004 else
5005 {
5006 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
5007 && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
5008 {
4d7206a2 5009 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
5010 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5011 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 5012 relax_switch ();
98d3f06f 5013 }
67c0d1eb
RS
5014 macro_build_lui (&offset_expr, tempreg);
5015 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5016 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2
RS
5017 if (mips_relax.sequence)
5018 relax_end ();
98d3f06f 5019 }
252b5132 5020 }
f5040a92 5021 else if (mips_pic == SVR4_PIC && ! mips_big_got && ! HAVE_NEWABI)
252b5132 5022 {
9117d219
NC
5023 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5024
252b5132
RH
5025 /* If this is a reference to an external symbol, and there
5026 is no constant, we want
5027 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
1abe91b1 5028 or for lca or if tempreg is PIC_CALL_REG
9117d219 5029 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
252b5132
RH
5030 For a local symbol, we want
5031 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5032 nop
5033 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5034
5035 If we have a small constant, and this is a reference to
5036 an external symbol, we want
5037 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5038 nop
5039 addiu $tempreg,$tempreg,<constant>
5040 For a local symbol, we want the same instruction
5041 sequence, but we output a BFD_RELOC_LO16 reloc on the
5042 addiu instruction.
5043
5044 If we have a large constant, and this is a reference to
5045 an external symbol, we want
5046 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5047 lui $at,<hiconstant>
5048 addiu $at,$at,<loconstant>
5049 addu $tempreg,$tempreg,$at
5050 For a local symbol, we want the same instruction
5051 sequence, but we output a BFD_RELOC_LO16 reloc on the
ed6fb7bd 5052 addiu instruction.
ed6fb7bd
SC
5053 */
5054
4d7206a2 5055 if (offset_expr.X_add_number == 0)
252b5132 5056 {
4d7206a2
RS
5057 if (breg == 0 && (call || tempreg == PIC_CALL_REG))
5058 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
5059
5060 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
5061 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5062 lw_reloc_type, mips_gp_register);
4d7206a2 5063 if (breg != 0)
252b5132
RH
5064 {
5065 /* We're going to put in an addu instruction using
5066 tempreg, so we may as well insert the nop right
5067 now. */
269137b2 5068 load_delay_nop ();
252b5132 5069 }
4d7206a2 5070 relax_switch ();
67c0d1eb
RS
5071 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5072 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 5073 load_delay_nop ();
67c0d1eb
RS
5074 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5075 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2 5076 relax_end ();
252b5132
RH
5077 /* FIXME: If breg == 0, and the next instruction uses
5078 $tempreg, then if this variant case is used an extra
5079 nop will be generated. */
5080 }
4d7206a2
RS
5081 else if (offset_expr.X_add_number >= -0x8000
5082 && offset_expr.X_add_number < 0x8000)
252b5132 5083 {
67c0d1eb 5084 load_got_offset (tempreg, &offset_expr);
269137b2 5085 load_delay_nop ();
67c0d1eb 5086 add_got_offset (tempreg, &offset_expr);
252b5132
RH
5087 }
5088 else
5089 {
4d7206a2
RS
5090 expr1.X_add_number = offset_expr.X_add_number;
5091 offset_expr.X_add_number =
5092 ((offset_expr.X_add_number + 0x8000) & 0xffff) - 0x8000;
67c0d1eb 5093 load_got_offset (tempreg, &offset_expr);
f6a22291 5094 offset_expr.X_add_number = expr1.X_add_number;
252b5132
RH
5095 /* If we are going to add in a base register, and the
5096 target register and the base register are the same,
5097 then we are using AT as a temporary register. Since
5098 we want to load the constant into AT, we add our
5099 current AT (from the global offset table) and the
5100 register into the register now, and pretend we were
5101 not using a base register. */
67c0d1eb 5102 if (breg == treg)
252b5132 5103 {
269137b2 5104 load_delay_nop ();
67c0d1eb 5105 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5106 treg, AT, breg);
252b5132
RH
5107 breg = 0;
5108 tempreg = treg;
252b5132 5109 }
f6a22291 5110 add_got_offset_hilo (tempreg, &offset_expr, AT);
252b5132
RH
5111 used_at = 1;
5112 }
5113 }
f5040a92
AO
5114 else if (mips_pic == SVR4_PIC && ! mips_big_got && HAVE_NEWABI)
5115 {
67c0d1eb 5116 int add_breg_early = 0;
f5040a92
AO
5117
5118 /* If this is a reference to an external, and there is no
5119 constant, or local symbol (*), with or without a
5120 constant, we want
5121 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
1abe91b1 5122 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
5123 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5124
5125 If we have a small constant, and this is a reference to
5126 an external symbol, we want
5127 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5128 addiu $tempreg,$tempreg,<constant>
5129
5130 If we have a large constant, and this is a reference to
5131 an external symbol, we want
5132 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5133 lui $at,<hiconstant>
5134 addiu $at,$at,<loconstant>
5135 addu $tempreg,$tempreg,$at
5136
5137 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
5138 local symbols, even though it introduces an additional
5139 instruction. */
5140
f5040a92
AO
5141 if (offset_expr.X_add_number)
5142 {
4d7206a2 5143 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
5144 offset_expr.X_add_number = 0;
5145
4d7206a2 5146 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
5147 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5148 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
5149
5150 if (expr1.X_add_number >= -0x8000
5151 && expr1.X_add_number < 0x8000)
5152 {
67c0d1eb
RS
5153 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
5154 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 5155 }
ecd13cd3 5156 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92
AO
5157 {
5158 int dreg;
5159
5160 /* If we are going to add in a base register, and the
5161 target register and the base register are the same,
5162 then we are using AT as a temporary register. Since
5163 we want to load the constant into AT, we add our
5164 current AT (from the global offset table) and the
5165 register into the register now, and pretend we were
5166 not using a base register. */
5167 if (breg != treg)
5168 dreg = tempreg;
5169 else
5170 {
5171 assert (tempreg == AT);
67c0d1eb
RS
5172 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
5173 treg, AT, breg);
f5040a92 5174 dreg = treg;
67c0d1eb 5175 add_breg_early = 1;
f5040a92
AO
5176 }
5177
f6a22291 5178 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 5179 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5180 dreg, dreg, AT);
f5040a92 5181
f5040a92
AO
5182 used_at = 1;
5183 }
5184 else
5185 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5186
4d7206a2 5187 relax_switch ();
f5040a92
AO
5188 offset_expr.X_add_number = expr1.X_add_number;
5189
67c0d1eb
RS
5190 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5191 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5192 if (add_breg_early)
f5040a92 5193 {
67c0d1eb 5194 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
f899b4b8 5195 treg, tempreg, breg);
f5040a92
AO
5196 breg = 0;
5197 tempreg = treg;
5198 }
4d7206a2 5199 relax_end ();
f5040a92 5200 }
4d7206a2 5201 else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
f5040a92 5202 {
4d7206a2 5203 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
5204 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5205 BFD_RELOC_MIPS_CALL16, mips_gp_register);
4d7206a2 5206 relax_switch ();
67c0d1eb
RS
5207 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5208 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2 5209 relax_end ();
f5040a92 5210 }
4d7206a2 5211 else
f5040a92 5212 {
67c0d1eb
RS
5213 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5214 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
5215 }
5216 }
5217 else if (mips_pic == SVR4_PIC && ! HAVE_NEWABI)
252b5132 5218 {
67c0d1eb 5219 int gpdelay;
9117d219
NC
5220 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5221 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
ed6fb7bd 5222 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
252b5132
RH
5223
5224 /* This is the large GOT case. If this is a reference to an
5225 external symbol, and there is no constant, we want
5226 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5227 addu $tempreg,$tempreg,$gp
5228 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 5229 or for lca or if tempreg is PIC_CALL_REG
9117d219
NC
5230 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5231 addu $tempreg,$tempreg,$gp
5232 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
252b5132
RH
5233 For a local symbol, we want
5234 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5235 nop
5236 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5237
5238 If we have a small constant, and this is a reference to
5239 an external symbol, we want
5240 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5241 addu $tempreg,$tempreg,$gp
5242 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5243 nop
5244 addiu $tempreg,$tempreg,<constant>
5245 For a local symbol, we want
5246 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5247 nop
5248 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
5249
5250 If we have a large constant, and this is a reference to
5251 an external symbol, we want
5252 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5253 addu $tempreg,$tempreg,$gp
5254 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5255 lui $at,<hiconstant>
5256 addiu $at,$at,<loconstant>
5257 addu $tempreg,$tempreg,$at
5258 For a local symbol, we want
5259 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5260 lui $at,<hiconstant>
5261 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
5262 addu $tempreg,$tempreg,$at
f5040a92 5263 */
438c16b8 5264
252b5132
RH
5265 expr1.X_add_number = offset_expr.X_add_number;
5266 offset_expr.X_add_number = 0;
4d7206a2 5267 relax_start (offset_expr.X_add_symbol);
67c0d1eb 5268 gpdelay = reg_needs_delay (mips_gp_register);
1abe91b1
MR
5269 if (expr1.X_add_number == 0 && breg == 0
5270 && (call || tempreg == PIC_CALL_REG))
9117d219
NC
5271 {
5272 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5273 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5274 }
67c0d1eb
RS
5275 macro_build (&offset_expr, "lui", "t,u", tempreg, lui_reloc_type);
5276 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5277 tempreg, tempreg, mips_gp_register);
67c0d1eb 5278 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 5279 tempreg, lw_reloc_type, tempreg);
252b5132
RH
5280 if (expr1.X_add_number == 0)
5281 {
67c0d1eb 5282 if (breg != 0)
252b5132
RH
5283 {
5284 /* We're going to put in an addu instruction using
5285 tempreg, so we may as well insert the nop right
5286 now. */
269137b2 5287 load_delay_nop ();
252b5132 5288 }
252b5132
RH
5289 }
5290 else if (expr1.X_add_number >= -0x8000
5291 && expr1.X_add_number < 0x8000)
5292 {
269137b2 5293 load_delay_nop ();
67c0d1eb 5294 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 5295 tempreg, tempreg, BFD_RELOC_LO16);
252b5132
RH
5296 }
5297 else
5298 {
67c0d1eb 5299 int dreg;
252b5132
RH
5300
5301 /* If we are going to add in a base register, and the
5302 target register and the base register are the same,
5303 then we are using AT as a temporary register. Since
5304 we want to load the constant into AT, we add our
5305 current AT (from the global offset table) and the
5306 register into the register now, and pretend we were
5307 not using a base register. */
5308 if (breg != treg)
67c0d1eb 5309 dreg = tempreg;
252b5132
RH
5310 else
5311 {
5312 assert (tempreg == AT);
269137b2 5313 load_delay_nop ();
67c0d1eb 5314 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5315 treg, AT, breg);
252b5132 5316 dreg = treg;
252b5132
RH
5317 }
5318
f6a22291 5319 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 5320 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
252b5132 5321
252b5132
RH
5322 used_at = 1;
5323 }
4d7206a2
RS
5324 offset_expr.X_add_number =
5325 ((expr1.X_add_number + 0x8000) & 0xffff) - 0x8000;
5326 relax_switch ();
252b5132 5327
67c0d1eb 5328 if (gpdelay)
252b5132
RH
5329 {
5330 /* This is needed because this instruction uses $gp, but
f5040a92 5331 the first instruction on the main stream does not. */
67c0d1eb 5332 macro_build (NULL, "nop", "");
252b5132 5333 }
ed6fb7bd 5334
67c0d1eb
RS
5335 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5336 local_reloc_type, mips_gp_register);
f5040a92 5337 if (expr1.X_add_number >= -0x8000
252b5132
RH
5338 && expr1.X_add_number < 0x8000)
5339 {
269137b2 5340 load_delay_nop ();
67c0d1eb
RS
5341 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5342 tempreg, tempreg, BFD_RELOC_LO16);
252b5132 5343 /* FIXME: If add_number is 0, and there was no base
f5040a92
AO
5344 register, the external symbol case ended with a load,
5345 so if the symbol turns out to not be external, and
5346 the next instruction uses tempreg, an unnecessary nop
5347 will be inserted. */
252b5132
RH
5348 }
5349 else
5350 {
5351 if (breg == treg)
5352 {
5353 /* We must add in the base register now, as in the
f5040a92 5354 external symbol case. */
252b5132 5355 assert (tempreg == AT);
269137b2 5356 load_delay_nop ();
67c0d1eb 5357 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5358 treg, AT, breg);
252b5132
RH
5359 tempreg = treg;
5360 /* We set breg to 0 because we have arranged to add
f5040a92 5361 it in in both cases. */
252b5132
RH
5362 breg = 0;
5363 }
5364
67c0d1eb
RS
5365 macro_build_lui (&expr1, AT);
5366 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 5367 AT, AT, BFD_RELOC_LO16);
67c0d1eb 5368 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5369 tempreg, tempreg, AT);
252b5132 5370 }
4d7206a2 5371 relax_end ();
252b5132 5372 }
f5040a92
AO
5373 else if (mips_pic == SVR4_PIC && HAVE_NEWABI)
5374 {
f5040a92
AO
5375 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5376 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
67c0d1eb 5377 int add_breg_early = 0;
f5040a92
AO
5378
5379 /* This is the large GOT case. If this is a reference to an
5380 external symbol, and there is no constant, we want
5381 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5382 add $tempreg,$tempreg,$gp
5383 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 5384 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
5385 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5386 add $tempreg,$tempreg,$gp
5387 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5388
5389 If we have a small constant, and this is a reference to
5390 an external symbol, we want
5391 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5392 add $tempreg,$tempreg,$gp
5393 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5394 addi $tempreg,$tempreg,<constant>
5395
5396 If we have a large constant, and this is a reference to
5397 an external symbol, we want
5398 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5399 addu $tempreg,$tempreg,$gp
5400 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5401 lui $at,<hiconstant>
5402 addi $at,$at,<loconstant>
5403 add $tempreg,$tempreg,$at
5404
5405 If we have NewABI, and we know it's a local symbol, we want
5406 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
5407 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
5408 otherwise we have to resort to GOT_HI16/GOT_LO16. */
5409
4d7206a2 5410 relax_start (offset_expr.X_add_symbol);
f5040a92 5411
4d7206a2 5412 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
5413 offset_expr.X_add_number = 0;
5414
1abe91b1
MR
5415 if (expr1.X_add_number == 0 && breg == 0
5416 && (call || tempreg == PIC_CALL_REG))
f5040a92
AO
5417 {
5418 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5419 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5420 }
67c0d1eb
RS
5421 macro_build (&offset_expr, "lui", "t,u", tempreg, lui_reloc_type);
5422 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5423 tempreg, tempreg, mips_gp_register);
67c0d1eb
RS
5424 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5425 tempreg, lw_reloc_type, tempreg);
f5040a92
AO
5426
5427 if (expr1.X_add_number == 0)
4d7206a2 5428 ;
f5040a92
AO
5429 else if (expr1.X_add_number >= -0x8000
5430 && expr1.X_add_number < 0x8000)
5431 {
67c0d1eb 5432 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 5433 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 5434 }
ecd13cd3 5435 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92
AO
5436 {
5437 int dreg;
5438
5439 /* If we are going to add in a base register, and the
5440 target register and the base register are the same,
5441 then we are using AT as a temporary register. Since
5442 we want to load the constant into AT, we add our
5443 current AT (from the global offset table) and the
5444 register into the register now, and pretend we were
5445 not using a base register. */
5446 if (breg != treg)
5447 dreg = tempreg;
5448 else
5449 {
5450 assert (tempreg == AT);
67c0d1eb 5451 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5452 treg, AT, breg);
f5040a92 5453 dreg = treg;
67c0d1eb 5454 add_breg_early = 1;
f5040a92
AO
5455 }
5456
f6a22291 5457 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 5458 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
f5040a92 5459
f5040a92
AO
5460 used_at = 1;
5461 }
5462 else
5463 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5464
4d7206a2 5465 relax_switch ();
f5040a92 5466 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
5467 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
5468 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
5469 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
5470 tempreg, BFD_RELOC_MIPS_GOT_OFST);
5471 if (add_breg_early)
f5040a92 5472 {
67c0d1eb 5473 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 5474 treg, tempreg, breg);
f5040a92
AO
5475 breg = 0;
5476 tempreg = treg;
5477 }
4d7206a2 5478 relax_end ();
f5040a92 5479 }
252b5132
RH
5480 else if (mips_pic == EMBEDDED_PIC)
5481 {
5482 /* We use
cdf6fd85 5483 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132 5484 */
67c0d1eb
RS
5485 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
5486 mips_gp_register, BFD_RELOC_GPREL16);
252b5132
RH
5487 }
5488 else
5489 abort ();
5490
5491 if (breg != 0)
4d34fb5f
TS
5492 {
5493 char *s;
5494
5495 if (mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
b7c7d6c1 5496 s = (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu";
4d34fb5f 5497 else
f899b4b8 5498 s = ADDRESS_ADD_INSN;
4d34fb5f 5499
67c0d1eb 5500 macro_build (NULL, s, "d,v,t", treg, tempreg, breg);
4d34fb5f 5501 }
252b5132
RH
5502
5503 if (! used_at)
5504 return;
5505
5506 break;
5507
5508 case M_J_A:
5509 /* The j instruction may not be used in PIC code, since it
5510 requires an absolute address. We convert it to a b
5511 instruction. */
5512 if (mips_pic == NO_PIC)
67c0d1eb 5513 macro_build (&offset_expr, "j", "a");
252b5132 5514 else
67c0d1eb 5515 macro_build (&offset_expr, "b", "p");
252b5132
RH
5516 return;
5517
5518 /* The jal instructions must be handled as macros because when
5519 generating PIC code they expand to multi-instruction
5520 sequences. Normally they are simple instructions. */
5521 case M_JAL_1:
5522 dreg = RA;
5523 /* Fall through. */
5524 case M_JAL_2:
5525 if (mips_pic == NO_PIC
5526 || mips_pic == EMBEDDED_PIC)
67c0d1eb 5527 macro_build (NULL, "jalr", "d,s", dreg, sreg);
252b5132
RH
5528 else if (mips_pic == SVR4_PIC)
5529 {
5530 if (sreg != PIC_CALL_REG)
5531 as_warn (_("MIPS PIC call to register other than $25"));
bdaaa2e1 5532
67c0d1eb 5533 macro_build (NULL, "jalr", "d,s", dreg, sreg);
6478892d 5534 if (! HAVE_NEWABI)
252b5132 5535 {
6478892d
TS
5536 if (mips_cprestore_offset < 0)
5537 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5538 else
5539 {
7a621144
DJ
5540 if (! mips_frame_reg_valid)
5541 {
5542 as_warn (_("No .frame pseudo-op used in PIC code"));
5543 /* Quiet this warning. */
5544 mips_frame_reg_valid = 1;
5545 }
5546 if (! mips_cprestore_valid)
5547 {
5548 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5549 /* Quiet this warning. */
5550 mips_cprestore_valid = 1;
5551 }
6478892d 5552 expr1.X_add_number = mips_cprestore_offset;
67c0d1eb 5553 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 5554 mips_gp_register,
256ab948
TS
5555 mips_frame_reg,
5556 HAVE_64BIT_ADDRESSES);
6478892d 5557 }
252b5132
RH
5558 }
5559 }
5560 else
5561 abort ();
5562
5563 return;
5564
5565 case M_JAL_A:
5566 if (mips_pic == NO_PIC)
67c0d1eb 5567 macro_build (&offset_expr, "jal", "a");
252b5132
RH
5568 else if (mips_pic == SVR4_PIC)
5569 {
5570 /* If this is a reference to an external symbol, and we are
5571 using a small GOT, we want
5572 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5573 nop
f9419b05 5574 jalr $ra,$25
252b5132
RH
5575 nop
5576 lw $gp,cprestore($sp)
5577 The cprestore value is set using the .cprestore
5578 pseudo-op. If we are using a big GOT, we want
5579 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5580 addu $25,$25,$gp
5581 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
5582 nop
f9419b05 5583 jalr $ra,$25
252b5132
RH
5584 nop
5585 lw $gp,cprestore($sp)
5586 If the symbol is not external, we want
5587 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5588 nop
5589 addiu $25,$25,<sym> (BFD_RELOC_LO16)
f9419b05 5590 jalr $ra,$25
252b5132 5591 nop
438c16b8 5592 lw $gp,cprestore($sp)
f5040a92
AO
5593
5594 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
5595 sequences above, minus nops, unless the symbol is local,
5596 which enables us to use GOT_PAGE/GOT_OFST (big got) or
5597 GOT_DISP. */
438c16b8 5598 if (HAVE_NEWABI)
252b5132 5599 {
f5040a92
AO
5600 if (! mips_big_got)
5601 {
4d7206a2 5602 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
5603 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5604 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
f5040a92 5605 mips_gp_register);
4d7206a2 5606 relax_switch ();
67c0d1eb
RS
5607 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5608 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
4d7206a2
RS
5609 mips_gp_register);
5610 relax_end ();
f5040a92
AO
5611 }
5612 else
5613 {
4d7206a2 5614 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
5615 macro_build (&offset_expr, "lui", "t,u", PIC_CALL_REG,
5616 BFD_RELOC_MIPS_CALL_HI16);
5617 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
5618 PIC_CALL_REG, mips_gp_register);
5619 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5620 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
5621 PIC_CALL_REG);
4d7206a2 5622 relax_switch ();
67c0d1eb
RS
5623 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5624 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
5625 mips_gp_register);
5626 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5627 PIC_CALL_REG, PIC_CALL_REG,
17a2f251 5628 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 5629 relax_end ();
f5040a92 5630 }
684022ea 5631
67c0d1eb 5632 macro_build_jalr (&offset_expr);
252b5132
RH
5633 }
5634 else
5635 {
4d7206a2 5636 relax_start (offset_expr.X_add_symbol);
438c16b8
TS
5637 if (! mips_big_got)
5638 {
67c0d1eb
RS
5639 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5640 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
17a2f251 5641 mips_gp_register);
269137b2 5642 load_delay_nop ();
4d7206a2 5643 relax_switch ();
438c16b8 5644 }
252b5132 5645 else
252b5132 5646 {
67c0d1eb
RS
5647 int gpdelay;
5648
5649 gpdelay = reg_needs_delay (mips_gp_register);
5650 macro_build (&offset_expr, "lui", "t,u", PIC_CALL_REG,
5651 BFD_RELOC_MIPS_CALL_HI16);
5652 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
5653 PIC_CALL_REG, mips_gp_register);
5654 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5655 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
5656 PIC_CALL_REG);
269137b2 5657 load_delay_nop ();
4d7206a2 5658 relax_switch ();
67c0d1eb
RS
5659 if (gpdelay)
5660 macro_build (NULL, "nop", "");
252b5132 5661 }
67c0d1eb
RS
5662 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
5663 PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
4d7206a2 5664 mips_gp_register);
269137b2 5665 load_delay_nop ();
67c0d1eb
RS
5666 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
5667 PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
4d7206a2 5668 relax_end ();
67c0d1eb 5669 macro_build_jalr (&offset_expr);
438c16b8 5670
6478892d
TS
5671 if (mips_cprestore_offset < 0)
5672 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5673 else
5674 {
7a621144
DJ
5675 if (! mips_frame_reg_valid)
5676 {
5677 as_warn (_("No .frame pseudo-op used in PIC code"));
5678 /* Quiet this warning. */
5679 mips_frame_reg_valid = 1;
5680 }
5681 if (! mips_cprestore_valid)
5682 {
5683 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5684 /* Quiet this warning. */
5685 mips_cprestore_valid = 1;
5686 }
6478892d 5687 if (mips_opts.noreorder)
67c0d1eb 5688 macro_build (NULL, "nop", "");
6478892d 5689 expr1.X_add_number = mips_cprestore_offset;
67c0d1eb 5690 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 5691 mips_gp_register,
256ab948
TS
5692 mips_frame_reg,
5693 HAVE_64BIT_ADDRESSES);
6478892d 5694 }
252b5132
RH
5695 }
5696 }
5697 else if (mips_pic == EMBEDDED_PIC)
5698 {
67c0d1eb 5699 macro_build (&offset_expr, "bal", "p");
252b5132
RH
5700 /* The linker may expand the call to a longer sequence which
5701 uses $at, so we must break rather than return. */
5702 break;
5703 }
5704 else
5705 abort ();
5706
5707 return;
5708
5709 case M_LB_AB:
5710 s = "lb";
5711 goto ld;
5712 case M_LBU_AB:
5713 s = "lbu";
5714 goto ld;
5715 case M_LH_AB:
5716 s = "lh";
5717 goto ld;
5718 case M_LHU_AB:
5719 s = "lhu";
5720 goto ld;
5721 case M_LW_AB:
5722 s = "lw";
5723 goto ld;
5724 case M_LWC0_AB:
5725 s = "lwc0";
bdaaa2e1 5726 /* Itbl support may require additional care here. */
252b5132
RH
5727 coproc = 1;
5728 goto ld;
5729 case M_LWC1_AB:
5730 s = "lwc1";
bdaaa2e1 5731 /* Itbl support may require additional care here. */
252b5132
RH
5732 coproc = 1;
5733 goto ld;
5734 case M_LWC2_AB:
5735 s = "lwc2";
bdaaa2e1 5736 /* Itbl support may require additional care here. */
252b5132
RH
5737 coproc = 1;
5738 goto ld;
5739 case M_LWC3_AB:
5740 s = "lwc3";
bdaaa2e1 5741 /* Itbl support may require additional care here. */
252b5132
RH
5742 coproc = 1;
5743 goto ld;
5744 case M_LWL_AB:
5745 s = "lwl";
5746 lr = 1;
5747 goto ld;
5748 case M_LWR_AB:
5749 s = "lwr";
5750 lr = 1;
5751 goto ld;
5752 case M_LDC1_AB:
fef14a42 5753 if (mips_opts.arch == CPU_R4650)
252b5132
RH
5754 {
5755 as_bad (_("opcode not supported on this processor"));
5756 return;
5757 }
5758 s = "ldc1";
bdaaa2e1 5759 /* Itbl support may require additional care here. */
252b5132
RH
5760 coproc = 1;
5761 goto ld;
5762 case M_LDC2_AB:
5763 s = "ldc2";
bdaaa2e1 5764 /* Itbl support may require additional care here. */
252b5132
RH
5765 coproc = 1;
5766 goto ld;
5767 case M_LDC3_AB:
5768 s = "ldc3";
bdaaa2e1 5769 /* Itbl support may require additional care here. */
252b5132
RH
5770 coproc = 1;
5771 goto ld;
5772 case M_LDL_AB:
5773 s = "ldl";
5774 lr = 1;
5775 goto ld;
5776 case M_LDR_AB:
5777 s = "ldr";
5778 lr = 1;
5779 goto ld;
5780 case M_LL_AB:
5781 s = "ll";
5782 goto ld;
5783 case M_LLD_AB:
5784 s = "lld";
5785 goto ld;
5786 case M_LWU_AB:
5787 s = "lwu";
5788 ld:
5789 if (breg == treg || coproc || lr)
5790 {
5791 tempreg = AT;
5792 used_at = 1;
5793 }
5794 else
5795 {
5796 tempreg = treg;
5797 used_at = 0;
5798 }
5799 goto ld_st;
5800 case M_SB_AB:
5801 s = "sb";
5802 goto st;
5803 case M_SH_AB:
5804 s = "sh";
5805 goto st;
5806 case M_SW_AB:
5807 s = "sw";
5808 goto st;
5809 case M_SWC0_AB:
5810 s = "swc0";
bdaaa2e1 5811 /* Itbl support may require additional care here. */
252b5132
RH
5812 coproc = 1;
5813 goto st;
5814 case M_SWC1_AB:
5815 s = "swc1";
bdaaa2e1 5816 /* Itbl support may require additional care here. */
252b5132
RH
5817 coproc = 1;
5818 goto st;
5819 case M_SWC2_AB:
5820 s = "swc2";
bdaaa2e1 5821 /* Itbl support may require additional care here. */
252b5132
RH
5822 coproc = 1;
5823 goto st;
5824 case M_SWC3_AB:
5825 s = "swc3";
bdaaa2e1 5826 /* Itbl support may require additional care here. */
252b5132
RH
5827 coproc = 1;
5828 goto st;
5829 case M_SWL_AB:
5830 s = "swl";
5831 goto st;
5832 case M_SWR_AB:
5833 s = "swr";
5834 goto st;
5835 case M_SC_AB:
5836 s = "sc";
5837 goto st;
5838 case M_SCD_AB:
5839 s = "scd";
5840 goto st;
5841 case M_SDC1_AB:
fef14a42 5842 if (mips_opts.arch == CPU_R4650)
252b5132
RH
5843 {
5844 as_bad (_("opcode not supported on this processor"));
5845 return;
5846 }
5847 s = "sdc1";
5848 coproc = 1;
bdaaa2e1 5849 /* Itbl support may require additional care here. */
252b5132
RH
5850 goto st;
5851 case M_SDC2_AB:
5852 s = "sdc2";
bdaaa2e1 5853 /* Itbl support may require additional care here. */
252b5132
RH
5854 coproc = 1;
5855 goto st;
5856 case M_SDC3_AB:
5857 s = "sdc3";
bdaaa2e1 5858 /* Itbl support may require additional care here. */
252b5132
RH
5859 coproc = 1;
5860 goto st;
5861 case M_SDL_AB:
5862 s = "sdl";
5863 goto st;
5864 case M_SDR_AB:
5865 s = "sdr";
5866 st:
5867 tempreg = AT;
5868 used_at = 1;
5869 ld_st:
bdaaa2e1 5870 /* Itbl support may require additional care here. */
252b5132
RH
5871 if (mask == M_LWC1_AB
5872 || mask == M_SWC1_AB
5873 || mask == M_LDC1_AB
5874 || mask == M_SDC1_AB
5875 || mask == M_L_DAB
5876 || mask == M_S_DAB)
5877 fmt = "T,o(b)";
5878 else if (coproc)
5879 fmt = "E,o(b)";
5880 else
5881 fmt = "t,o(b)";
5882
256ab948
TS
5883 /* Sign-extending 32-bit constants makes their handling easier.
5884 The HAVE_64BIT_GPRS... part is due to the linux kernel hack
5885 described below. */
5886 if ((! HAVE_64BIT_ADDRESSES
5887 && (! HAVE_64BIT_GPRS && offset_expr.X_op == O_constant))
d17e7bce
TS
5888 && (offset_expr.X_op == O_constant)
5889 && ! ((offset_expr.X_add_number & ~((bfd_vma) 0x7fffffff))
5890 == ~((bfd_vma) 0x7fffffff)))
ae826530 5891 {
d17e7bce 5892 if (offset_expr.X_add_number & ~((bfd_vma) 0xffffffff))
1b8e29e5 5893 as_bad (_("constant too large"));
ae826530 5894
1b8e29e5
TS
5895 offset_expr.X_add_number = (((offset_expr.X_add_number & 0xffffffff)
5896 ^ 0x80000000) - 0x80000000);
ae826530 5897 }
256ab948 5898
afdbd6d0
CD
5899 /* For embedded PIC, we allow loads where the offset is calculated
5900 by subtracting a symbol in the current segment from an unknown
5901 symbol, relative to a base register, e.g.:
5902 <op> $treg, <sym>-<localsym>($breg)
5903 This is used by the compiler for switch statements. */
76b3015f 5904 if (mips_pic == EMBEDDED_PIC
afdbd6d0
CD
5905 && offset_expr.X_op == O_subtract
5906 && (symbol_constant_p (offset_expr.X_op_symbol)
5907 ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
5908 : (symbol_equated_p (offset_expr.X_op_symbol)
5909 && (S_GET_SEGMENT
5910 (symbol_get_value_expression (offset_expr.X_op_symbol)
5911 ->X_add_symbol)
5912 == now_seg)))
5913 && breg != 0
5914 && (offset_expr.X_add_number == 0
5915 || OUTPUT_FLAVOR == bfd_target_elf_flavour))
5916 {
5917 /* For this case, we output the instructions:
5918 lui $tempreg,<sym> (BFD_RELOC_PCREL_HI16_S)
5919 addiu $tempreg,$tempreg,$breg
5920 <op> $treg,<sym>($tempreg) (BFD_RELOC_PCREL_LO16)
5921 If the relocation would fit entirely in 16 bits, it would be
5922 nice to emit:
5923 <op> $treg,<sym>($breg) (BFD_RELOC_PCREL_LO16)
5924 instead, but that seems quite difficult. */
67c0d1eb 5925 macro_build (&offset_expr, "lui", "t,u", tempreg,
17a2f251 5926 BFD_RELOC_PCREL_HI16_S);
67c0d1eb
RS
5927 macro_build (NULL,
5928 ((bfd_arch_bits_per_address (stdoutput) == 32
5929 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
5930 ? "addu" : "daddu"),
5931 "d,v,t", tempreg, tempreg, breg);
5932 macro_build (&offset_expr, s, fmt, treg,
5933 BFD_RELOC_PCREL_LO16, tempreg);
afdbd6d0
CD
5934 if (! used_at)
5935 return;
5936 break;
5937 }
5938
252b5132
RH
5939 if (offset_expr.X_op != O_constant
5940 && offset_expr.X_op != O_symbol)
5941 {
5942 as_bad (_("expression too complex"));
5943 offset_expr.X_op = O_constant;
5944 }
5945
5946 /* A constant expression in PIC code can be handled just as it
5947 is in non PIC code. */
5948 if (mips_pic == NO_PIC
5949 || offset_expr.X_op == O_constant)
5950 {
5951 /* If this is a reference to a GP relative symbol, and there
5952 is no base register, we want
cdf6fd85 5953 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
5954 Otherwise, if there is no base register, we want
5955 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5956 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5957 If we have a constant, we need two instructions anyhow,
5958 so we always use the latter form.
5959
5960 If we have a base register, and this is a reference to a
5961 GP relative symbol, we want
5962 addu $tempreg,$breg,$gp
cdf6fd85 5963 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
5964 Otherwise we want
5965 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5966 addu $tempreg,$tempreg,$breg
5967 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245 5968 With a constant we always use the latter case.
76b3015f 5969
d6bc6245
TS
5970 With 64bit address space and no base register and $at usable,
5971 we want
5972 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5973 lui $at,<sym> (BFD_RELOC_HI16_S)
5974 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5975 dsll32 $tempreg,0
5976 daddu $tempreg,$at
5977 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5978 If we have a base register, we want
5979 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5980 lui $at,<sym> (BFD_RELOC_HI16_S)
5981 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5982 daddu $at,$breg
5983 dsll32 $tempreg,0
5984 daddu $tempreg,$at
5985 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5986
5987 Without $at we can't generate the optimal path for superscalar
5988 processors here since this would require two temporary registers.
5989 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5990 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5991 dsll $tempreg,16
5992 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5993 dsll $tempreg,16
5994 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5995 If we have a base register, we want
5996 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5997 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5998 dsll $tempreg,16
5999 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
6000 dsll $tempreg,16
6001 daddu $tempreg,$tempreg,$breg
6002 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6373ee54
CD
6003
6004 If we have 64-bit addresses, as an optimization, for
6005 addresses which are 32-bit constants (e.g. kseg0/kseg1
6006 addresses) we fall back to the 32-bit address generation
78d32a17
MR
6007 mechanism since it is more efficient. Note that due to
6008 the signed offset used by memory operations, the 32-bit
6009 range is shifted down by 32768 here. This code should
6373ee54
CD
6010 probably attempt to generate 64-bit constants more
6011 efficiently in general.
9214dd3b
AO
6012
6013 As an extension for architectures with 64-bit registers,
6014 we don't truncate 64-bit addresses given as literal
6015 constants down to 32 bits, to support existing practice
6016 in the mips64 Linux (the kernel), that compiles source
6017 files with -mabi=64, assembling them as o32 or n32 (with
6018 -Wa,-32 or -Wa,-n32). This is not beautiful, but since
6019 the whole kernel is loaded into a memory region that is
67c1ffbe 6020 addressable with sign-extended 32-bit addresses, it is
9214dd3b
AO
6021 wasteful to compute the upper 32 bits of every
6022 non-literal address, that takes more space and time.
6023 Some day this should probably be implemented as an
6024 assembler option, such that the kernel doesn't have to
6025 use such ugly hacks, even though it will still have to
6026 end up converting the binary to ELF32 for a number of
6027 platforms whose boot loaders don't support ELF64
6028 binaries. */
256ab948
TS
6029 if ((HAVE_64BIT_ADDRESSES
6030 && ! (offset_expr.X_op == O_constant
6031 && IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000)))
6032 || (HAVE_64BIT_GPRS
6033 && offset_expr.X_op == O_constant
6034 && ! IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000)))
d6bc6245 6035 {
e864ceca
RS
6036 /* ??? We don't provide a GP-relative alternative for
6037 these macros. It used not to be possible with the
6038 original relaxation code, but it could be done now. */
d6bc6245 6039
460597ba 6040 if (used_at == 0 && ! mips_opts.noat)
d6bc6245 6041 {
67c0d1eb
RS
6042 macro_build (&offset_expr, "lui", "t,u", tempreg,
6043 BFD_RELOC_MIPS_HIGHEST);
6044 macro_build (&offset_expr, "lui", "t,u", AT,
6045 BFD_RELOC_HI16_S);
6046 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
6047 tempreg, BFD_RELOC_MIPS_HIGHER);
d6bc6245 6048 if (breg != 0)
67c0d1eb
RS
6049 macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
6050 macro_build (NULL, "dsll32", "d,w,<", tempreg, tempreg, 0);
6051 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
6052 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16,
6053 tempreg);
d6bc6245
TS
6054 used_at = 1;
6055 }
6056 else
6057 {
67c0d1eb
RS
6058 macro_build (&offset_expr, "lui", "t,u", tempreg,
6059 BFD_RELOC_MIPS_HIGHEST);
6060 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
6061 tempreg, BFD_RELOC_MIPS_HIGHER);
6062 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
6063 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
6064 tempreg, BFD_RELOC_HI16_S);
6065 macro_build (NULL, "dsll", "d,w,<", tempreg, tempreg, 16);
d6bc6245 6066 if (breg != 0)
67c0d1eb 6067 macro_build (NULL, "daddu", "d,v,t",
17a2f251 6068 tempreg, tempreg, breg);
67c0d1eb 6069 macro_build (&offset_expr, s, fmt, treg,
17a2f251 6070 BFD_RELOC_LO16, tempreg);
d6bc6245
TS
6071 }
6072
6073 return;
6074 }
256ab948
TS
6075
6076 if (offset_expr.X_op == O_constant
ecd13cd3 6077 && ! IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000))
5a7a0b7b 6078 as_bad (_("load/store address overflow (max 32 bits)"));
76b3015f 6079
252b5132
RH
6080 if (breg == 0)
6081 {
67c0d1eb
RS
6082 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6083 && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 6084 {
4d7206a2 6085 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
6086 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_GPREL16,
6087 mips_gp_register);
4d7206a2 6088 relax_switch ();
252b5132
RH
6089 used_at = 0;
6090 }
67c0d1eb
RS
6091 macro_build_lui (&offset_expr, tempreg);
6092 macro_build (&offset_expr, s, fmt, treg,
17a2f251 6093 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
6094 if (mips_relax.sequence)
6095 relax_end ();
252b5132
RH
6096 }
6097 else
6098 {
67c0d1eb
RS
6099 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6100 && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 6101 {
4d7206a2 6102 relax_start (offset_expr.X_add_symbol);
67c0d1eb 6103 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6104 tempreg, breg, mips_gp_register);
67c0d1eb 6105 macro_build (&offset_expr, s, fmt, treg,
17a2f251 6106 BFD_RELOC_GPREL16, tempreg);
4d7206a2 6107 relax_switch ();
252b5132 6108 }
67c0d1eb
RS
6109 macro_build_lui (&offset_expr, tempreg);
6110 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6111 tempreg, tempreg, breg);
67c0d1eb 6112 macro_build (&offset_expr, s, fmt, treg,
17a2f251 6113 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
6114 if (mips_relax.sequence)
6115 relax_end ();
252b5132
RH
6116 }
6117 }
6118 else if (mips_pic == SVR4_PIC && ! mips_big_got)
6119 {
ed6fb7bd 6120 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
f9419b05 6121
252b5132
RH
6122 /* If this is a reference to an external symbol, we want
6123 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6124 nop
6125 <op> $treg,0($tempreg)
6126 Otherwise we want
6127 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6128 nop
6129 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6130 <op> $treg,0($tempreg)
f5040a92
AO
6131
6132 For NewABI, we want
6133 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6134 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
6135
252b5132
RH
6136 If there is a base register, we add it to $tempreg before
6137 the <op>. If there is a constant, we stick it in the
6138 <op> instruction. We don't handle constants larger than
6139 16 bits, because we have no way to load the upper 16 bits
6140 (actually, we could handle them for the subset of cases
6141 in which we are not using $at). */
6142 assert (offset_expr.X_op == O_symbol);
f5040a92
AO
6143 if (HAVE_NEWABI)
6144 {
67c0d1eb
RS
6145 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6146 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 6147 if (breg != 0)
67c0d1eb 6148 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6149 tempreg, tempreg, breg);
67c0d1eb 6150 macro_build (&offset_expr, s, fmt, treg,
17a2f251 6151 BFD_RELOC_MIPS_GOT_OFST, tempreg);
f5040a92
AO
6152
6153 if (! used_at)
6154 return;
6155
6156 break;
6157 }
252b5132
RH
6158 expr1.X_add_number = offset_expr.X_add_number;
6159 offset_expr.X_add_number = 0;
6160 if (expr1.X_add_number < -0x8000
6161 || expr1.X_add_number >= 0x8000)
6162 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb
RS
6163 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6164 lw_reloc_type, mips_gp_register);
269137b2 6165 load_delay_nop ();
4d7206a2
RS
6166 relax_start (offset_expr.X_add_symbol);
6167 relax_switch ();
67c0d1eb
RS
6168 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
6169 tempreg, BFD_RELOC_LO16);
4d7206a2 6170 relax_end ();
252b5132 6171 if (breg != 0)
67c0d1eb 6172 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6173 tempreg, tempreg, breg);
67c0d1eb 6174 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
252b5132 6175 }
f5040a92 6176 else if (mips_pic == SVR4_PIC && ! HAVE_NEWABI)
252b5132 6177 {
67c0d1eb 6178 int gpdelay;
252b5132
RH
6179
6180 /* If this is a reference to an external symbol, we want
6181 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6182 addu $tempreg,$tempreg,$gp
6183 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6184 <op> $treg,0($tempreg)
6185 Otherwise we want
6186 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6187 nop
6188 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6189 <op> $treg,0($tempreg)
6190 If there is a base register, we add it to $tempreg before
6191 the <op>. If there is a constant, we stick it in the
6192 <op> instruction. We don't handle constants larger than
6193 16 bits, because we have no way to load the upper 16 bits
6194 (actually, we could handle them for the subset of cases
f5040a92 6195 in which we are not using $at). */
252b5132
RH
6196 assert (offset_expr.X_op == O_symbol);
6197 expr1.X_add_number = offset_expr.X_add_number;
6198 offset_expr.X_add_number = 0;
6199 if (expr1.X_add_number < -0x8000
6200 || expr1.X_add_number >= 0x8000)
6201 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 6202 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 6203 relax_start (offset_expr.X_add_symbol);
67c0d1eb 6204 macro_build (&offset_expr, "lui", "t,u", tempreg,
17a2f251 6205 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
6206 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
6207 mips_gp_register);
6208 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6209 BFD_RELOC_MIPS_GOT_LO16, tempreg);
4d7206a2 6210 relax_switch ();
67c0d1eb
RS
6211 if (gpdelay)
6212 macro_build (NULL, "nop", "");
6213 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6214 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 6215 load_delay_nop ();
67c0d1eb
RS
6216 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
6217 tempreg, BFD_RELOC_LO16);
4d7206a2
RS
6218 relax_end ();
6219
252b5132 6220 if (breg != 0)
67c0d1eb 6221 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6222 tempreg, tempreg, breg);
67c0d1eb 6223 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
252b5132 6224 }
f5040a92
AO
6225 else if (mips_pic == SVR4_PIC && HAVE_NEWABI)
6226 {
f5040a92
AO
6227 /* If this is a reference to an external symbol, we want
6228 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6229 add $tempreg,$tempreg,$gp
6230 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6231 <op> $treg,<ofst>($tempreg)
6232 Otherwise, for local symbols, we want:
6233 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6234 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
6235 assert (offset_expr.X_op == O_symbol);
4d7206a2 6236 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
6237 offset_expr.X_add_number = 0;
6238 if (expr1.X_add_number < -0x8000
6239 || expr1.X_add_number >= 0x8000)
6240 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4d7206a2 6241 relax_start (offset_expr.X_add_symbol);
67c0d1eb 6242 macro_build (&offset_expr, "lui", "t,u", tempreg,
17a2f251 6243 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
6244 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
6245 mips_gp_register);
6246 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6247 BFD_RELOC_MIPS_GOT_LO16, tempreg);
f5040a92 6248 if (breg != 0)
67c0d1eb 6249 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6250 tempreg, tempreg, breg);
67c0d1eb 6251 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
684022ea 6252
4d7206a2 6253 relax_switch ();
f5040a92 6254 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
6255 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
6256 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 6257 if (breg != 0)
67c0d1eb 6258 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6259 tempreg, tempreg, breg);
67c0d1eb 6260 macro_build (&offset_expr, s, fmt, treg,
17a2f251 6261 BFD_RELOC_MIPS_GOT_OFST, tempreg);
4d7206a2 6262 relax_end ();
f5040a92 6263 }
252b5132
RH
6264 else if (mips_pic == EMBEDDED_PIC)
6265 {
6266 /* If there is no base register, we want
cdf6fd85 6267 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
6268 If there is a base register, we want
6269 addu $tempreg,$breg,$gp
cdf6fd85 6270 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
6271 */
6272 assert (offset_expr.X_op == O_symbol);
6273 if (breg == 0)
6274 {
67c0d1eb
RS
6275 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_GPREL16,
6276 mips_gp_register);
252b5132
RH
6277 used_at = 0;
6278 }
6279 else
6280 {
67c0d1eb 6281 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6282 tempreg, breg, mips_gp_register);
67c0d1eb 6283 macro_build (&offset_expr, s, fmt, treg,
17a2f251 6284 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6285 }
6286 }
6287 else
6288 abort ();
6289
6290 if (! used_at)
6291 return;
6292
6293 break;
6294
6295 case M_LI:
6296 case M_LI_S:
67c0d1eb 6297 load_register (treg, &imm_expr, 0);
252b5132
RH
6298 return;
6299
6300 case M_DLI:
67c0d1eb 6301 load_register (treg, &imm_expr, 1);
252b5132
RH
6302 return;
6303
6304 case M_LI_SS:
6305 if (imm_expr.X_op == O_constant)
6306 {
67c0d1eb
RS
6307 load_register (AT, &imm_expr, 0);
6308 macro_build (NULL, "mtc1", "t,G", AT, treg);
252b5132
RH
6309 break;
6310 }
6311 else
6312 {
6313 assert (offset_expr.X_op == O_symbol
6314 && strcmp (segment_name (S_GET_SEGMENT
6315 (offset_expr.X_add_symbol)),
6316 ".lit4") == 0
6317 && offset_expr.X_add_number == 0);
67c0d1eb 6318 macro_build (&offset_expr, "lwc1", "T,o(b)", treg,
17a2f251 6319 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
252b5132
RH
6320 return;
6321 }
6322
6323 case M_LI_D:
ca4e0257
RS
6324 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
6325 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
6326 order 32 bits of the value and the low order 32 bits are either
6327 zero or in OFFSET_EXPR. */
252b5132
RH
6328 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6329 {
ca4e0257 6330 if (HAVE_64BIT_GPRS)
67c0d1eb 6331 load_register (treg, &imm_expr, 1);
252b5132
RH
6332 else
6333 {
6334 int hreg, lreg;
6335
6336 if (target_big_endian)
6337 {
6338 hreg = treg;
6339 lreg = treg + 1;
6340 }
6341 else
6342 {
6343 hreg = treg + 1;
6344 lreg = treg;
6345 }
6346
6347 if (hreg <= 31)
67c0d1eb 6348 load_register (hreg, &imm_expr, 0);
252b5132
RH
6349 if (lreg <= 31)
6350 {
6351 if (offset_expr.X_op == O_absent)
67c0d1eb 6352 move_register (lreg, 0);
252b5132
RH
6353 else
6354 {
6355 assert (offset_expr.X_op == O_constant);
67c0d1eb 6356 load_register (lreg, &offset_expr, 0);
252b5132
RH
6357 }
6358 }
6359 }
6360 return;
6361 }
6362
6363 /* We know that sym is in the .rdata section. First we get the
6364 upper 16 bits of the address. */
6365 if (mips_pic == NO_PIC)
6366 {
67c0d1eb 6367 macro_build_lui (&offset_expr, AT);
252b5132
RH
6368 }
6369 else if (mips_pic == SVR4_PIC)
6370 {
67c0d1eb
RS
6371 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6372 BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132
RH
6373 }
6374 else if (mips_pic == EMBEDDED_PIC)
6375 {
6376 /* For embedded PIC we pick up the entire address off $gp in
6377 a single instruction. */
67c0d1eb
RS
6378 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", AT,
6379 mips_gp_register, BFD_RELOC_GPREL16);
252b5132
RH
6380 offset_expr.X_op = O_constant;
6381 offset_expr.X_add_number = 0;
6382 }
6383 else
6384 abort ();
bdaaa2e1 6385
252b5132 6386 /* Now we load the register(s). */
ca4e0257 6387 if (HAVE_64BIT_GPRS)
67c0d1eb 6388 macro_build (&offset_expr, "ld", "t,o(b)", treg, BFD_RELOC_LO16, AT);
252b5132
RH
6389 else
6390 {
67c0d1eb 6391 macro_build (&offset_expr, "lw", "t,o(b)", treg, BFD_RELOC_LO16, AT);
f9419b05 6392 if (treg != RA)
252b5132
RH
6393 {
6394 /* FIXME: How in the world do we deal with the possible
6395 overflow here? */
6396 offset_expr.X_add_number += 4;
67c0d1eb 6397 macro_build (&offset_expr, "lw", "t,o(b)",
17a2f251 6398 treg + 1, BFD_RELOC_LO16, AT);
252b5132
RH
6399 }
6400 }
252b5132
RH
6401 break;
6402
6403 case M_LI_DD:
ca4e0257
RS
6404 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
6405 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
6406 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
6407 the value and the low order 32 bits are either zero or in
6408 OFFSET_EXPR. */
252b5132
RH
6409 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6410 {
67c0d1eb 6411 load_register (AT, &imm_expr, HAVE_64BIT_FPRS);
ca4e0257
RS
6412 if (HAVE_64BIT_FPRS)
6413 {
6414 assert (HAVE_64BIT_GPRS);
67c0d1eb 6415 macro_build (NULL, "dmtc1", "t,S", AT, treg);
ca4e0257 6416 }
252b5132
RH
6417 else
6418 {
67c0d1eb 6419 macro_build (NULL, "mtc1", "t,G", AT, treg + 1);
252b5132 6420 if (offset_expr.X_op == O_absent)
67c0d1eb 6421 macro_build (NULL, "mtc1", "t,G", 0, treg);
252b5132
RH
6422 else
6423 {
6424 assert (offset_expr.X_op == O_constant);
67c0d1eb
RS
6425 load_register (AT, &offset_expr, 0);
6426 macro_build (NULL, "mtc1", "t,G", AT, treg);
252b5132
RH
6427 }
6428 }
6429 break;
6430 }
6431
6432 assert (offset_expr.X_op == O_symbol
6433 && offset_expr.X_add_number == 0);
6434 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
6435 if (strcmp (s, ".lit8") == 0)
6436 {
e7af610e 6437 if (mips_opts.isa != ISA_MIPS1)
252b5132 6438 {
67c0d1eb 6439 macro_build (&offset_expr, "ldc1", "T,o(b)", treg,
17a2f251 6440 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
252b5132
RH
6441 return;
6442 }
c9914766 6443 breg = mips_gp_register;
252b5132
RH
6444 r = BFD_RELOC_MIPS_LITERAL;
6445 goto dob;
6446 }
6447 else
6448 {
6449 assert (strcmp (s, RDATA_SECTION_NAME) == 0);
6450 if (mips_pic == SVR4_PIC)
67c0d1eb
RS
6451 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6452 BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132
RH
6453 else
6454 {
6455 /* FIXME: This won't work for a 64 bit address. */
67c0d1eb 6456 macro_build_lui (&offset_expr, AT);
252b5132 6457 }
bdaaa2e1 6458
e7af610e 6459 if (mips_opts.isa != ISA_MIPS1)
252b5132 6460 {
67c0d1eb
RS
6461 macro_build (&offset_expr, "ldc1", "T,o(b)",
6462 treg, BFD_RELOC_LO16, AT);
252b5132
RH
6463 break;
6464 }
6465 breg = AT;
6466 r = BFD_RELOC_LO16;
6467 goto dob;
6468 }
6469
6470 case M_L_DOB:
fef14a42 6471 if (mips_opts.arch == CPU_R4650)
252b5132
RH
6472 {
6473 as_bad (_("opcode not supported on this processor"));
6474 return;
6475 }
6476 /* Even on a big endian machine $fn comes before $fn+1. We have
6477 to adjust when loading from memory. */
6478 r = BFD_RELOC_LO16;
6479 dob:
e7af610e 6480 assert (mips_opts.isa == ISA_MIPS1);
67c0d1eb 6481 macro_build (&offset_expr, "lwc1", "T,o(b)",
17a2f251 6482 target_big_endian ? treg + 1 : treg, r, breg);
252b5132
RH
6483 /* FIXME: A possible overflow which I don't know how to deal
6484 with. */
6485 offset_expr.X_add_number += 4;
67c0d1eb 6486 macro_build (&offset_expr, "lwc1", "T,o(b)",
17a2f251 6487 target_big_endian ? treg : treg + 1, r, breg);
252b5132 6488
252b5132
RH
6489 if (breg != AT)
6490 return;
6491 break;
6492
6493 case M_L_DAB:
6494 /*
6495 * The MIPS assembler seems to check for X_add_number not
6496 * being double aligned and generating:
6497 * lui at,%hi(foo+1)
6498 * addu at,at,v1
6499 * addiu at,at,%lo(foo+1)
6500 * lwc1 f2,0(at)
6501 * lwc1 f3,4(at)
6502 * But, the resulting address is the same after relocation so why
6503 * generate the extra instruction?
6504 */
fef14a42 6505 if (mips_opts.arch == CPU_R4650)
252b5132
RH
6506 {
6507 as_bad (_("opcode not supported on this processor"));
6508 return;
6509 }
bdaaa2e1 6510 /* Itbl support may require additional care here. */
252b5132 6511 coproc = 1;
e7af610e 6512 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
6513 {
6514 s = "ldc1";
6515 goto ld;
6516 }
6517
6518 s = "lwc1";
6519 fmt = "T,o(b)";
6520 goto ldd_std;
6521
6522 case M_S_DAB:
fef14a42 6523 if (mips_opts.arch == CPU_R4650)
252b5132
RH
6524 {
6525 as_bad (_("opcode not supported on this processor"));
6526 return;
6527 }
6528
e7af610e 6529 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
6530 {
6531 s = "sdc1";
6532 goto st;
6533 }
6534
6535 s = "swc1";
6536 fmt = "T,o(b)";
bdaaa2e1 6537 /* Itbl support may require additional care here. */
252b5132
RH
6538 coproc = 1;
6539 goto ldd_std;
6540
6541 case M_LD_AB:
ca4e0257 6542 if (HAVE_64BIT_GPRS)
252b5132
RH
6543 {
6544 s = "ld";
6545 goto ld;
6546 }
6547
6548 s = "lw";
6549 fmt = "t,o(b)";
6550 goto ldd_std;
6551
6552 case M_SD_AB:
ca4e0257 6553 if (HAVE_64BIT_GPRS)
252b5132
RH
6554 {
6555 s = "sd";
6556 goto st;
6557 }
6558
6559 s = "sw";
6560 fmt = "t,o(b)";
6561
6562 ldd_std:
afdbd6d0
CD
6563 /* We do _not_ bother to allow embedded PIC (symbol-local_symbol)
6564 loads for the case of doing a pair of loads to simulate an 'ld'.
6565 This is not currently done by the compiler, and assembly coders
6566 writing embedded-pic code can cope. */
6567
252b5132
RH
6568 if (offset_expr.X_op != O_symbol
6569 && offset_expr.X_op != O_constant)
6570 {
6571 as_bad (_("expression too complex"));
6572 offset_expr.X_op = O_constant;
6573 }
6574
6575 /* Even on a big endian machine $fn comes before $fn+1. We have
6576 to adjust when loading from memory. We set coproc if we must
6577 load $fn+1 first. */
bdaaa2e1 6578 /* Itbl support may require additional care here. */
252b5132
RH
6579 if (! target_big_endian)
6580 coproc = 0;
6581
6582 if (mips_pic == NO_PIC
6583 || offset_expr.X_op == O_constant)
6584 {
6585 /* If this is a reference to a GP relative symbol, we want
cdf6fd85
TS
6586 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6587 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
6588 If we have a base register, we use this
6589 addu $at,$breg,$gp
cdf6fd85
TS
6590 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6591 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
6592 If this is not a GP relative symbol, we want
6593 lui $at,<sym> (BFD_RELOC_HI16_S)
6594 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6595 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6596 If there is a base register, we add it to $at after the
6597 lui instruction. If there is a constant, we always use
6598 the last case. */
e7d556df 6599 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
252b5132 6600 || nopic_need_relax (offset_expr.X_add_symbol, 1))
67c0d1eb 6601 used_at = 1;
252b5132
RH
6602 else
6603 {
4d7206a2 6604 relax_start (offset_expr.X_add_symbol);
252b5132
RH
6605 if (breg == 0)
6606 {
c9914766 6607 tempreg = mips_gp_register;
252b5132
RH
6608 used_at = 0;
6609 }
6610 else
6611 {
67c0d1eb 6612 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6613 AT, breg, mips_gp_register);
252b5132 6614 tempreg = AT;
252b5132
RH
6615 used_at = 1;
6616 }
6617
beae10d5 6618 /* Itbl support may require additional care here. */
67c0d1eb 6619 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
17a2f251 6620 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6621 offset_expr.X_add_number += 4;
6622
6623 /* Set mips_optimize to 2 to avoid inserting an
6624 undesired nop. */
6625 hold_mips_optimize = mips_optimize;
6626 mips_optimize = 2;
beae10d5 6627 /* Itbl support may require additional care here. */
67c0d1eb 6628 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
17a2f251 6629 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6630 mips_optimize = hold_mips_optimize;
6631
4d7206a2 6632 relax_switch ();
252b5132
RH
6633
6634 /* We just generated two relocs. When tc_gen_reloc
6635 handles this case, it will skip the first reloc and
6636 handle the second. The second reloc already has an
6637 extra addend of 4, which we added above. We must
6638 subtract it out, and then subtract another 4 to make
6639 the first reloc come out right. The second reloc
6640 will come out right because we are going to add 4 to
6641 offset_expr when we build its instruction below.
6642
6643 If we have a symbol, then we don't want to include
6644 the offset, because it will wind up being included
6645 when we generate the reloc. */
6646
6647 if (offset_expr.X_op == O_constant)
6648 offset_expr.X_add_number -= 8;
6649 else
6650 {
6651 offset_expr.X_add_number = -4;
6652 offset_expr.X_op = O_constant;
6653 }
6654 }
67c0d1eb 6655 macro_build_lui (&offset_expr, AT);
252b5132 6656 if (breg != 0)
67c0d1eb 6657 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 6658 /* Itbl support may require additional care here. */
67c0d1eb 6659 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
17a2f251 6660 BFD_RELOC_LO16, AT);
252b5132
RH
6661 /* FIXME: How do we handle overflow here? */
6662 offset_expr.X_add_number += 4;
beae10d5 6663 /* Itbl support may require additional care here. */
67c0d1eb 6664 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
17a2f251 6665 BFD_RELOC_LO16, AT);
4d7206a2
RS
6666 if (mips_relax.sequence)
6667 relax_end ();
bdaaa2e1 6668 }
252b5132
RH
6669 else if (mips_pic == SVR4_PIC && ! mips_big_got)
6670 {
252b5132
RH
6671 /* If this is a reference to an external symbol, we want
6672 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6673 nop
6674 <op> $treg,0($at)
6675 <op> $treg+1,4($at)
6676 Otherwise we want
6677 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6678 nop
6679 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6680 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6681 If there is a base register we add it to $at before the
6682 lwc1 instructions. If there is a constant we include it
6683 in the lwc1 instructions. */
6684 used_at = 1;
6685 expr1.X_add_number = offset_expr.X_add_number;
252b5132
RH
6686 if (expr1.X_add_number < -0x8000
6687 || expr1.X_add_number >= 0x8000 - 4)
6688 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 6689 load_got_offset (AT, &offset_expr);
269137b2 6690 load_delay_nop ();
252b5132 6691 if (breg != 0)
67c0d1eb 6692 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
252b5132
RH
6693
6694 /* Set mips_optimize to 2 to avoid inserting an undesired
6695 nop. */
6696 hold_mips_optimize = mips_optimize;
6697 mips_optimize = 2;
4d7206a2 6698
beae10d5 6699 /* Itbl support may require additional care here. */
4d7206a2 6700 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
6701 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
6702 BFD_RELOC_LO16, AT);
4d7206a2 6703 expr1.X_add_number += 4;
67c0d1eb
RS
6704 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
6705 BFD_RELOC_LO16, AT);
4d7206a2 6706 relax_switch ();
67c0d1eb
RS
6707 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6708 BFD_RELOC_LO16, AT);
4d7206a2 6709 offset_expr.X_add_number += 4;
67c0d1eb
RS
6710 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6711 BFD_RELOC_LO16, AT);
4d7206a2 6712 relax_end ();
252b5132 6713
4d7206a2 6714 mips_optimize = hold_mips_optimize;
252b5132
RH
6715 }
6716 else if (mips_pic == SVR4_PIC)
6717 {
67c0d1eb 6718 int gpdelay;
252b5132
RH
6719
6720 /* If this is a reference to an external symbol, we want
6721 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6722 addu $at,$at,$gp
6723 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
6724 nop
6725 <op> $treg,0($at)
6726 <op> $treg+1,4($at)
6727 Otherwise we want
6728 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6729 nop
6730 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6731 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6732 If there is a base register we add it to $at before the
6733 lwc1 instructions. If there is a constant we include it
6734 in the lwc1 instructions. */
6735 used_at = 1;
6736 expr1.X_add_number = offset_expr.X_add_number;
6737 offset_expr.X_add_number = 0;
6738 if (expr1.X_add_number < -0x8000
6739 || expr1.X_add_number >= 0x8000 - 4)
6740 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 6741 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 6742 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
6743 macro_build (&offset_expr, "lui", "t,u",
6744 AT, BFD_RELOC_MIPS_GOT_HI16);
6745 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6746 AT, AT, mips_gp_register);
67c0d1eb 6747 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 6748 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
269137b2 6749 load_delay_nop ();
252b5132 6750 if (breg != 0)
67c0d1eb 6751 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 6752 /* Itbl support may require additional care here. */
67c0d1eb 6753 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
17a2f251 6754 BFD_RELOC_LO16, AT);
252b5132
RH
6755 expr1.X_add_number += 4;
6756
6757 /* Set mips_optimize to 2 to avoid inserting an undesired
6758 nop. */
6759 hold_mips_optimize = mips_optimize;
6760 mips_optimize = 2;
beae10d5 6761 /* Itbl support may require additional care here. */
67c0d1eb 6762 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
17a2f251 6763 BFD_RELOC_LO16, AT);
252b5132
RH
6764 mips_optimize = hold_mips_optimize;
6765 expr1.X_add_number -= 4;
6766
4d7206a2
RS
6767 relax_switch ();
6768 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
6769 if (gpdelay)
6770 macro_build (NULL, "nop", "");
6771 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
6772 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 6773 load_delay_nop ();
252b5132 6774 if (breg != 0)
67c0d1eb 6775 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 6776 /* Itbl support may require additional care here. */
67c0d1eb
RS
6777 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
6778 BFD_RELOC_LO16, AT);
4d7206a2 6779 offset_expr.X_add_number += 4;
252b5132
RH
6780
6781 /* Set mips_optimize to 2 to avoid inserting an undesired
6782 nop. */
6783 hold_mips_optimize = mips_optimize;
6784 mips_optimize = 2;
beae10d5 6785 /* Itbl support may require additional care here. */
67c0d1eb
RS
6786 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
6787 BFD_RELOC_LO16, AT);
252b5132 6788 mips_optimize = hold_mips_optimize;
4d7206a2 6789 relax_end ();
252b5132
RH
6790 }
6791 else if (mips_pic == EMBEDDED_PIC)
6792 {
6793 /* If there is no base register, we use
cdf6fd85
TS
6794 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6795 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
6796 If we have a base register, we use
6797 addu $at,$breg,$gp
cdf6fd85
TS
6798 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6799 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
6800 */
6801 if (breg == 0)
6802 {
c9914766 6803 tempreg = mips_gp_register;
252b5132
RH
6804 used_at = 0;
6805 }
6806 else
6807 {
67c0d1eb 6808 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 6809 AT, breg, mips_gp_register);
252b5132
RH
6810 tempreg = AT;
6811 used_at = 1;
6812 }
6813
beae10d5 6814 /* Itbl support may require additional care here. */
67c0d1eb 6815 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
17a2f251 6816 BFD_RELOC_GPREL16, tempreg);
252b5132 6817 offset_expr.X_add_number += 4;
beae10d5 6818 /* Itbl support may require additional care here. */
67c0d1eb 6819 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
17a2f251 6820 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6821 }
6822 else
6823 abort ();
6824
6825 if (! used_at)
6826 return;
6827
6828 break;
6829
6830 case M_LD_OB:
6831 s = "lw";
6832 goto sd_ob;
6833 case M_SD_OB:
6834 s = "sw";
6835 sd_ob:
ca4e0257 6836 assert (HAVE_32BIT_ADDRESSES);
67c0d1eb 6837 macro_build (&offset_expr, s, "t,o(b)", treg, BFD_RELOC_LO16, breg);
252b5132 6838 offset_expr.X_add_number += 4;
67c0d1eb 6839 macro_build (&offset_expr, s, "t,o(b)", treg + 1, BFD_RELOC_LO16, breg);
252b5132
RH
6840 return;
6841
6842 /* New code added to support COPZ instructions.
6843 This code builds table entries out of the macros in mip_opcodes.
6844 R4000 uses interlocks to handle coproc delays.
6845 Other chips (like the R3000) require nops to be inserted for delays.
6846
f72c8c98 6847 FIXME: Currently, we require that the user handle delays.
252b5132
RH
6848 In order to fill delay slots for non-interlocked chips,
6849 we must have a way to specify delays based on the coprocessor.
6850 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
6851 What are the side-effects of the cop instruction?
6852 What cache support might we have and what are its effects?
6853 Both coprocessor & memory require delays. how long???
bdaaa2e1 6854 What registers are read/set/modified?
252b5132
RH
6855
6856 If an itbl is provided to interpret cop instructions,
bdaaa2e1 6857 this knowledge can be encoded in the itbl spec. */
252b5132
RH
6858
6859 case M_COP0:
6860 s = "c0";
6861 goto copz;
6862 case M_COP1:
6863 s = "c1";
6864 goto copz;
6865 case M_COP2:
6866 s = "c2";
6867 goto copz;
6868 case M_COP3:
6869 s = "c3";
6870 copz:
6871 /* For now we just do C (same as Cz). The parameter will be
6872 stored in insn_opcode by mips_ip. */
67c0d1eb 6873 macro_build (NULL, s, "C", ip->insn_opcode);
252b5132
RH
6874 return;
6875
ea1fb5dc 6876 case M_MOVE:
67c0d1eb 6877 move_register (dreg, sreg);
ea1fb5dc
RS
6878 return;
6879
252b5132
RH
6880#ifdef LOSING_COMPILER
6881 default:
6882 /* Try and see if this is a new itbl instruction.
6883 This code builds table entries out of the macros in mip_opcodes.
6884 FIXME: For now we just assemble the expression and pass it's
6885 value along as a 32-bit immediate.
bdaaa2e1 6886 We may want to have the assembler assemble this value,
252b5132
RH
6887 so that we gain the assembler's knowledge of delay slots,
6888 symbols, etc.
6889 Would it be more efficient to use mask (id) here? */
bdaaa2e1 6890 if (itbl_have_entries
252b5132 6891 && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
beae10d5 6892 {
252b5132
RH
6893 s = ip->insn_mo->name;
6894 s2 = "cop3";
6895 coproc = ITBL_DECODE_PNUM (immed_expr);;
67c0d1eb 6896 macro_build (&immed_expr, s, "C");
252b5132 6897 return;
beae10d5 6898 }
252b5132
RH
6899 macro2 (ip);
6900 return;
6901 }
6902 if (mips_opts.noat)
6903 as_warn (_("Macro used $at after \".set noat\""));
6904}
bdaaa2e1 6905
252b5132 6906static void
17a2f251 6907macro2 (struct mips_cl_insn *ip)
252b5132
RH
6908{
6909 register int treg, sreg, dreg, breg;
6910 int tempreg;
6911 int mask;
252b5132
RH
6912 int used_at;
6913 expressionS expr1;
6914 const char *s;
6915 const char *s2;
6916 const char *fmt;
6917 int likely = 0;
6918 int dbl = 0;
6919 int coproc = 0;
6920 int lr = 0;
6921 int imm = 0;
6922 int off;
6923 offsetT maxnum;
6924 bfd_reloc_code_real_type r;
bdaaa2e1 6925
252b5132
RH
6926 treg = (ip->insn_opcode >> 16) & 0x1f;
6927 dreg = (ip->insn_opcode >> 11) & 0x1f;
6928 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
6929 mask = ip->insn_mo->mask;
bdaaa2e1 6930
252b5132
RH
6931 expr1.X_op = O_constant;
6932 expr1.X_op_symbol = NULL;
6933 expr1.X_add_symbol = NULL;
6934 expr1.X_add_number = 1;
bdaaa2e1 6935
252b5132
RH
6936 switch (mask)
6937 {
6938#endif /* LOSING_COMPILER */
6939
6940 case M_DMUL:
6941 dbl = 1;
6942 case M_MUL:
67c0d1eb
RS
6943 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", sreg, treg);
6944 macro_build (NULL, "mflo", "d", dreg);
252b5132
RH
6945 return;
6946
6947 case M_DMUL_I:
6948 dbl = 1;
6949 case M_MUL_I:
6950 /* The MIPS assembler some times generates shifts and adds. I'm
6951 not trying to be that fancy. GCC should do this for us
6952 anyway. */
67c0d1eb
RS
6953 load_register (AT, &imm_expr, dbl);
6954 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, AT);
6955 macro_build (NULL, "mflo", "d", dreg);
252b5132
RH
6956 break;
6957
6958 case M_DMULO_I:
6959 dbl = 1;
6960 case M_MULO_I:
6961 imm = 1;
6962 goto do_mulo;
6963
6964 case M_DMULO:
6965 dbl = 1;
6966 case M_MULO:
6967 do_mulo:
b34976b6 6968 mips_emit_delays (TRUE);
252b5132
RH
6969 ++mips_opts.noreorder;
6970 mips_any_noreorder = 1;
6971 if (imm)
67c0d1eb
RS
6972 load_register (AT, &imm_expr, dbl);
6973 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
6974 macro_build (NULL, "mflo", "d", dreg);
6975 macro_build (NULL, dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, RA);
6976 macro_build (NULL, "mfhi", "d", AT);
252b5132 6977 if (mips_trap)
67c0d1eb 6978 macro_build (NULL, "tne", "s,t,q", dreg, AT, 6);
252b5132
RH
6979 else
6980 {
6981 expr1.X_add_number = 8;
67c0d1eb
RS
6982 macro_build (&expr1, "beq", "s,t,p", dreg, AT);
6983 macro_build (NULL, "nop", "", 0);
6984 macro_build (NULL, "break", "c", 6);
252b5132
RH
6985 }
6986 --mips_opts.noreorder;
67c0d1eb 6987 macro_build (NULL, "mflo", "d", dreg);
252b5132
RH
6988 break;
6989
6990 case M_DMULOU_I:
6991 dbl = 1;
6992 case M_MULOU_I:
6993 imm = 1;
6994 goto do_mulou;
6995
6996 case M_DMULOU:
6997 dbl = 1;
6998 case M_MULOU:
6999 do_mulou:
b34976b6 7000 mips_emit_delays (TRUE);
252b5132
RH
7001 ++mips_opts.noreorder;
7002 mips_any_noreorder = 1;
7003 if (imm)
67c0d1eb
RS
7004 load_register (AT, &imm_expr, dbl);
7005 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
17a2f251 7006 sreg, imm ? AT : treg);
67c0d1eb
RS
7007 macro_build (NULL, "mfhi", "d", AT);
7008 macro_build (NULL, "mflo", "d", dreg);
252b5132 7009 if (mips_trap)
67c0d1eb 7010 macro_build (NULL, "tne", "s,t,q", AT, 0, 6);
252b5132
RH
7011 else
7012 {
7013 expr1.X_add_number = 8;
67c0d1eb
RS
7014 macro_build (&expr1, "beq", "s,t,p", AT, 0);
7015 macro_build (NULL, "nop", "", 0);
7016 macro_build (NULL, "break", "c", 6);
252b5132
RH
7017 }
7018 --mips_opts.noreorder;
7019 break;
7020
771c7ce4 7021 case M_DROL:
fef14a42 7022 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097
CD
7023 {
7024 if (dreg == sreg)
7025 {
7026 tempreg = AT;
7027 used_at = 1;
7028 }
7029 else
7030 {
7031 tempreg = dreg;
7032 used_at = 0;
7033 }
67c0d1eb
RS
7034 macro_build (NULL, "dnegu", "d,w", tempreg, treg);
7035 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, tempreg);
82dd0097
CD
7036 if (used_at)
7037 break;
7038 return;
7039 }
67c0d1eb
RS
7040 macro_build (NULL, "dsubu", "d,v,t", AT, 0, treg);
7041 macro_build (NULL, "dsrlv", "d,t,s", AT, sreg, AT);
7042 macro_build (NULL, "dsllv", "d,t,s", dreg, sreg, treg);
7043 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
7044 break;
7045
252b5132 7046 case M_ROL:
fef14a42 7047 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097
CD
7048 {
7049 if (dreg == sreg)
7050 {
7051 tempreg = AT;
7052 used_at = 1;
7053 }
7054 else
7055 {
7056 tempreg = dreg;
7057 used_at = 0;
7058 }
67c0d1eb
RS
7059 macro_build (NULL, "negu", "d,w", tempreg, treg);
7060 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, tempreg);
82dd0097
CD
7061 if (used_at)
7062 break;
7063 return;
7064 }
67c0d1eb
RS
7065 macro_build (NULL, "subu", "d,v,t", AT, 0, treg);
7066 macro_build (NULL, "srlv", "d,t,s", AT, sreg, AT);
7067 macro_build (NULL, "sllv", "d,t,s", dreg, sreg, treg);
7068 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
7069 break;
7070
771c7ce4
TS
7071 case M_DROL_I:
7072 {
7073 unsigned int rot;
82dd0097 7074 char *l, *r;
771c7ce4
TS
7075
7076 if (imm_expr.X_op != O_constant)
82dd0097 7077 as_bad (_("Improper rotate count"));
771c7ce4 7078 rot = imm_expr.X_add_number & 0x3f;
fef14a42 7079 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
60b63b72
RS
7080 {
7081 rot = (64 - rot) & 0x3f;
7082 if (rot >= 32)
67c0d1eb 7083 macro_build (NULL, "dror32", "d,w,<", dreg, sreg, rot - 32);
60b63b72 7084 else
67c0d1eb 7085 macro_build (NULL, "dror", "d,w,<", dreg, sreg, rot);
82dd0097 7086 return;
60b63b72 7087 }
483fc7cd 7088 if (rot == 0)
483fc7cd 7089 {
67c0d1eb 7090 macro_build (NULL, "dsrl", "d,w,<", dreg, sreg, 0);
82dd0097 7091 return;
483fc7cd 7092 }
82dd0097
CD
7093 l = (rot < 0x20) ? "dsll" : "dsll32";
7094 r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
7095 rot &= 0x1f;
67c0d1eb
RS
7096 macro_build (NULL, l, "d,w,<", AT, sreg, rot);
7097 macro_build (NULL, r, "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7098 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
7099 }
7100 break;
7101
252b5132 7102 case M_ROL_I:
771c7ce4
TS
7103 {
7104 unsigned int rot;
7105
7106 if (imm_expr.X_op != O_constant)
82dd0097 7107 as_bad (_("Improper rotate count"));
771c7ce4 7108 rot = imm_expr.X_add_number & 0x1f;
fef14a42 7109 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
60b63b72 7110 {
67c0d1eb 7111 macro_build (NULL, "ror", "d,w,<", dreg, sreg, (32 - rot) & 0x1f);
82dd0097 7112 return;
60b63b72 7113 }
483fc7cd 7114 if (rot == 0)
483fc7cd 7115 {
67c0d1eb 7116 macro_build (NULL, "srl", "d,w,<", dreg, sreg, 0);
82dd0097 7117 return;
483fc7cd 7118 }
67c0d1eb
RS
7119 macro_build (NULL, "sll", "d,w,<", AT, sreg, rot);
7120 macro_build (NULL, "srl", "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7121 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
7122 }
7123 break;
7124
7125 case M_DROR:
fef14a42 7126 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097 7127 {
67c0d1eb 7128 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, treg);
82dd0097
CD
7129 return;
7130 }
67c0d1eb
RS
7131 macro_build (NULL, "dsubu", "d,v,t", AT, 0, treg);
7132 macro_build (NULL, "dsllv", "d,t,s", AT, sreg, AT);
7133 macro_build (NULL, "dsrlv", "d,t,s", dreg, sreg, treg);
7134 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
7135 break;
7136
7137 case M_ROR:
fef14a42 7138 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 7139 {
67c0d1eb 7140 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, treg);
82dd0097
CD
7141 return;
7142 }
67c0d1eb
RS
7143 macro_build (NULL, "subu", "d,v,t", AT, 0, treg);
7144 macro_build (NULL, "sllv", "d,t,s", AT, sreg, AT);
7145 macro_build (NULL, "srlv", "d,t,s", dreg, sreg, treg);
7146 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
7147 break;
7148
771c7ce4
TS
7149 case M_DROR_I:
7150 {
7151 unsigned int rot;
82dd0097 7152 char *l, *r;
771c7ce4
TS
7153
7154 if (imm_expr.X_op != O_constant)
82dd0097 7155 as_bad (_("Improper rotate count"));
771c7ce4 7156 rot = imm_expr.X_add_number & 0x3f;
fef14a42 7157 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097
CD
7158 {
7159 if (rot >= 32)
67c0d1eb 7160 macro_build (NULL, "dror32", "d,w,<", dreg, sreg, rot - 32);
82dd0097 7161 else
67c0d1eb 7162 macro_build (NULL, "dror", "d,w,<", dreg, sreg, rot);
82dd0097
CD
7163 return;
7164 }
483fc7cd 7165 if (rot == 0)
483fc7cd 7166 {
67c0d1eb 7167 macro_build (NULL, "dsrl", "d,w,<", dreg, sreg, 0);
82dd0097 7168 return;
483fc7cd 7169 }
82dd0097
CD
7170 r = (rot < 0x20) ? "dsrl" : "dsrl32";
7171 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
7172 rot &= 0x1f;
67c0d1eb
RS
7173 macro_build (NULL, r, "d,w,<", AT, sreg, rot);
7174 macro_build (NULL, l, "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7175 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
7176 }
7177 break;
7178
252b5132 7179 case M_ROR_I:
771c7ce4
TS
7180 {
7181 unsigned int rot;
7182
7183 if (imm_expr.X_op != O_constant)
82dd0097 7184 as_bad (_("Improper rotate count"));
771c7ce4 7185 rot = imm_expr.X_add_number & 0x1f;
fef14a42 7186 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 7187 {
67c0d1eb 7188 macro_build (NULL, "ror", "d,w,<", dreg, sreg, rot);
82dd0097
CD
7189 return;
7190 }
483fc7cd 7191 if (rot == 0)
483fc7cd 7192 {
67c0d1eb 7193 macro_build (NULL, "srl", "d,w,<", dreg, sreg, 0);
82dd0097 7194 return;
483fc7cd 7195 }
67c0d1eb
RS
7196 macro_build (NULL, "srl", "d,w,<", AT, sreg, rot);
7197 macro_build (NULL, "sll", "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7198 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4 7199 }
252b5132
RH
7200 break;
7201
7202 case M_S_DOB:
fef14a42 7203 if (mips_opts.arch == CPU_R4650)
252b5132
RH
7204 {
7205 as_bad (_("opcode not supported on this processor"));
7206 return;
7207 }
e7af610e 7208 assert (mips_opts.isa == ISA_MIPS1);
252b5132
RH
7209 /* Even on a big endian machine $fn comes before $fn+1. We have
7210 to adjust when storing to memory. */
67c0d1eb
RS
7211 macro_build (&offset_expr, "swc1", "T,o(b)",
7212 target_big_endian ? treg + 1 : treg, BFD_RELOC_LO16, breg);
252b5132 7213 offset_expr.X_add_number += 4;
67c0d1eb
RS
7214 macro_build (&offset_expr, "swc1", "T,o(b)",
7215 target_big_endian ? treg : treg + 1, BFD_RELOC_LO16, breg);
252b5132
RH
7216 return;
7217
7218 case M_SEQ:
7219 if (sreg == 0)
67c0d1eb 7220 macro_build (&expr1, "sltiu", "t,r,j", dreg, treg, BFD_RELOC_LO16);
252b5132 7221 else if (treg == 0)
67c0d1eb 7222 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7223 else
7224 {
67c0d1eb
RS
7225 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
7226 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
252b5132
RH
7227 }
7228 return;
7229
7230 case M_SEQ_I:
7231 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7232 {
67c0d1eb 7233 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7234 return;
7235 }
7236 if (sreg == 0)
7237 {
7238 as_warn (_("Instruction %s: result is always false"),
7239 ip->insn_mo->name);
67c0d1eb 7240 move_register (dreg, 0);
252b5132
RH
7241 return;
7242 }
7243 if (imm_expr.X_op == O_constant
7244 && imm_expr.X_add_number >= 0
7245 && imm_expr.X_add_number < 0x10000)
7246 {
67c0d1eb 7247 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7248 used_at = 0;
7249 }
7250 else if (imm_expr.X_op == O_constant
7251 && imm_expr.X_add_number > -0x8000
7252 && imm_expr.X_add_number < 0)
7253 {
7254 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 7255 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
17a2f251 7256 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7257 used_at = 0;
7258 }
7259 else
7260 {
67c0d1eb
RS
7261 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7262 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
252b5132
RH
7263 used_at = 1;
7264 }
67c0d1eb 7265 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
252b5132
RH
7266 if (used_at)
7267 break;
7268 return;
7269
7270 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
7271 s = "slt";
7272 goto sge;
7273 case M_SGEU:
7274 s = "sltu";
7275 sge:
67c0d1eb
RS
7276 macro_build (NULL, s, "d,v,t", dreg, sreg, treg);
7277 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
252b5132
RH
7278 return;
7279
7280 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
7281 case M_SGEU_I:
7282 if (imm_expr.X_op == O_constant
7283 && imm_expr.X_add_number >= -0x8000
7284 && imm_expr.X_add_number < 0x8000)
7285 {
67c0d1eb
RS
7286 macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
7287 dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7288 used_at = 0;
7289 }
7290 else
7291 {
67c0d1eb
RS
7292 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7293 macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
7294 dreg, sreg, AT);
252b5132
RH
7295 used_at = 1;
7296 }
67c0d1eb 7297 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
252b5132
RH
7298 if (used_at)
7299 break;
7300 return;
7301
7302 case M_SGT: /* sreg > treg <==> treg < sreg */
7303 s = "slt";
7304 goto sgt;
7305 case M_SGTU:
7306 s = "sltu";
7307 sgt:
67c0d1eb 7308 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
252b5132
RH
7309 return;
7310
7311 case M_SGT_I: /* sreg > I <==> I < sreg */
7312 s = "slt";
7313 goto sgti;
7314 case M_SGTU_I:
7315 s = "sltu";
7316 sgti:
67c0d1eb
RS
7317 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7318 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
252b5132
RH
7319 break;
7320
2396cfb9 7321 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
252b5132
RH
7322 s = "slt";
7323 goto sle;
7324 case M_SLEU:
7325 s = "sltu";
7326 sle:
67c0d1eb
RS
7327 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
7328 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
252b5132
RH
7329 return;
7330
2396cfb9 7331 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
252b5132
RH
7332 s = "slt";
7333 goto slei;
7334 case M_SLEU_I:
7335 s = "sltu";
7336 slei:
67c0d1eb
RS
7337 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7338 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
7339 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
252b5132
RH
7340 break;
7341
7342 case M_SLT_I:
7343 if (imm_expr.X_op == O_constant
7344 && imm_expr.X_add_number >= -0x8000
7345 && imm_expr.X_add_number < 0x8000)
7346 {
67c0d1eb 7347 macro_build (&imm_expr, "slti", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7348 return;
7349 }
67c0d1eb
RS
7350 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7351 macro_build (NULL, "slt", "d,v,t", dreg, sreg, AT);
252b5132
RH
7352 break;
7353
7354 case M_SLTU_I:
7355 if (imm_expr.X_op == O_constant
7356 && imm_expr.X_add_number >= -0x8000
7357 && imm_expr.X_add_number < 0x8000)
7358 {
67c0d1eb 7359 macro_build (&imm_expr, "sltiu", "t,r,j", dreg, sreg,
17a2f251 7360 BFD_RELOC_LO16);
252b5132
RH
7361 return;
7362 }
67c0d1eb
RS
7363 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7364 macro_build (NULL, "sltu", "d,v,t", dreg, sreg, AT);
252b5132
RH
7365 break;
7366
7367 case M_SNE:
7368 if (sreg == 0)
67c0d1eb 7369 macro_build (NULL, "sltu", "d,v,t", dreg, 0, treg);
252b5132 7370 else if (treg == 0)
67c0d1eb 7371 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
252b5132
RH
7372 else
7373 {
67c0d1eb
RS
7374 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
7375 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
252b5132
RH
7376 }
7377 return;
7378
7379 case M_SNE_I:
7380 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7381 {
67c0d1eb 7382 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
252b5132
RH
7383 return;
7384 }
7385 if (sreg == 0)
7386 {
7387 as_warn (_("Instruction %s: result is always true"),
7388 ip->insn_mo->name);
67c0d1eb
RS
7389 macro_build (&expr1, HAVE_32BIT_GPRS ? "addiu" : "daddiu", "t,r,j",
7390 dreg, 0, BFD_RELOC_LO16);
252b5132
RH
7391 return;
7392 }
7393 if (imm_expr.X_op == O_constant
7394 && imm_expr.X_add_number >= 0
7395 && imm_expr.X_add_number < 0x10000)
7396 {
67c0d1eb 7397 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7398 used_at = 0;
7399 }
7400 else if (imm_expr.X_op == O_constant
7401 && imm_expr.X_add_number > -0x8000
7402 && imm_expr.X_add_number < 0)
7403 {
7404 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 7405 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
17a2f251 7406 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7407 used_at = 0;
7408 }
7409 else
7410 {
67c0d1eb
RS
7411 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7412 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
252b5132
RH
7413 used_at = 1;
7414 }
67c0d1eb 7415 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
252b5132
RH
7416 if (used_at)
7417 break;
7418 return;
7419
7420 case M_DSUB_I:
7421 dbl = 1;
7422 case M_SUB_I:
7423 if (imm_expr.X_op == O_constant
7424 && imm_expr.X_add_number > -0x8000
7425 && imm_expr.X_add_number <= 0x8000)
7426 {
7427 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb
RS
7428 macro_build (&imm_expr, dbl ? "daddi" : "addi", "t,r,j",
7429 dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7430 return;
7431 }
67c0d1eb
RS
7432 load_register (AT, &imm_expr, dbl);
7433 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
252b5132
RH
7434 break;
7435
7436 case M_DSUBU_I:
7437 dbl = 1;
7438 case M_SUBU_I:
7439 if (imm_expr.X_op == O_constant
7440 && imm_expr.X_add_number > -0x8000
7441 && imm_expr.X_add_number <= 0x8000)
7442 {
7443 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb
RS
7444 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "t,r,j",
7445 dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
7446 return;
7447 }
67c0d1eb
RS
7448 load_register (AT, &imm_expr, dbl);
7449 macro_build (NULL, dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
252b5132
RH
7450 break;
7451
7452 case M_TEQ_I:
7453 s = "teq";
7454 goto trap;
7455 case M_TGE_I:
7456 s = "tge";
7457 goto trap;
7458 case M_TGEU_I:
7459 s = "tgeu";
7460 goto trap;
7461 case M_TLT_I:
7462 s = "tlt";
7463 goto trap;
7464 case M_TLTU_I:
7465 s = "tltu";
7466 goto trap;
7467 case M_TNE_I:
7468 s = "tne";
7469 trap:
67c0d1eb
RS
7470 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
7471 macro_build (NULL, s, "s,t", sreg, AT);
252b5132
RH
7472 break;
7473
252b5132 7474 case M_TRUNCWS:
43841e91 7475 case M_TRUNCWD:
e7af610e 7476 assert (mips_opts.isa == ISA_MIPS1);
252b5132
RH
7477 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
7478 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
7479
7480 /*
7481 * Is the double cfc1 instruction a bug in the mips assembler;
7482 * or is there a reason for it?
7483 */
b34976b6 7484 mips_emit_delays (TRUE);
252b5132
RH
7485 ++mips_opts.noreorder;
7486 mips_any_noreorder = 1;
67c0d1eb
RS
7487 macro_build (NULL, "cfc1", "t,G", treg, RA);
7488 macro_build (NULL, "cfc1", "t,G", treg, RA);
7489 macro_build (NULL, "nop", "");
252b5132 7490 expr1.X_add_number = 3;
67c0d1eb 7491 macro_build (&expr1, "ori", "t,r,i", AT, treg, BFD_RELOC_LO16);
252b5132 7492 expr1.X_add_number = 2;
67c0d1eb
RS
7493 macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
7494 macro_build (NULL, "ctc1", "t,G", AT, RA);
7495 macro_build (NULL, "nop", "");
7496 macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
7497 dreg, sreg);
7498 macro_build (NULL, "ctc1", "t,G", treg, RA);
7499 macro_build (NULL, "nop", "");
252b5132
RH
7500 --mips_opts.noreorder;
7501 break;
7502
7503 case M_ULH:
7504 s = "lb";
7505 goto ulh;
7506 case M_ULHU:
7507 s = "lbu";
7508 ulh:
7509 if (offset_expr.X_add_number >= 0x7fff)
7510 as_bad (_("operand overflow"));
252b5132 7511 if (! target_big_endian)
f9419b05 7512 ++offset_expr.X_add_number;
67c0d1eb 7513 macro_build (&offset_expr, s, "t,o(b)", AT, BFD_RELOC_LO16, breg);
252b5132 7514 if (! target_big_endian)
f9419b05 7515 --offset_expr.X_add_number;
252b5132 7516 else
f9419b05 7517 ++offset_expr.X_add_number;
67c0d1eb
RS
7518 macro_build (&offset_expr, "lbu", "t,o(b)", treg, BFD_RELOC_LO16, breg);
7519 macro_build (NULL, "sll", "d,w,<", AT, AT, 8);
7520 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
252b5132
RH
7521 break;
7522
7523 case M_ULD:
7524 s = "ldl";
7525 s2 = "ldr";
7526 off = 7;
7527 goto ulw;
7528 case M_ULW:
7529 s = "lwl";
7530 s2 = "lwr";
7531 off = 3;
7532 ulw:
7533 if (offset_expr.X_add_number >= 0x8000 - off)
7534 as_bad (_("operand overflow"));
af22f5b2
CD
7535 if (treg != breg)
7536 tempreg = treg;
7537 else
7538 tempreg = AT;
252b5132
RH
7539 if (! target_big_endian)
7540 offset_expr.X_add_number += off;
67c0d1eb 7541 macro_build (&offset_expr, s, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
252b5132
RH
7542 if (! target_big_endian)
7543 offset_expr.X_add_number -= off;
7544 else
7545 offset_expr.X_add_number += off;
67c0d1eb 7546 macro_build (&offset_expr, s2, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
af22f5b2
CD
7547
7548 /* If necessary, move the result in tempreg the final destination. */
7549 if (treg == tempreg)
7550 return;
7551 /* Protect second load's delay slot. */
017315e4 7552 load_delay_nop ();
67c0d1eb 7553 move_register (treg, tempreg);
af22f5b2 7554 break;
252b5132
RH
7555
7556 case M_ULD_A:
7557 s = "ldl";
7558 s2 = "ldr";
7559 off = 7;
7560 goto ulwa;
7561 case M_ULW_A:
7562 s = "lwl";
7563 s2 = "lwr";
7564 off = 3;
7565 ulwa:
d6bc6245 7566 used_at = 1;
67c0d1eb 7567 load_address (AT, &offset_expr, &used_at);
252b5132 7568 if (breg != 0)
67c0d1eb 7569 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
252b5132
RH
7570 if (! target_big_endian)
7571 expr1.X_add_number = off;
7572 else
7573 expr1.X_add_number = 0;
67c0d1eb 7574 macro_build (&expr1, s, "t,o(b)", treg, BFD_RELOC_LO16, AT);
252b5132
RH
7575 if (! target_big_endian)
7576 expr1.X_add_number = 0;
7577 else
7578 expr1.X_add_number = off;
67c0d1eb 7579 macro_build (&expr1, s2, "t,o(b)", treg, BFD_RELOC_LO16, AT);
252b5132
RH
7580 break;
7581
7582 case M_ULH_A:
7583 case M_ULHU_A:
d6bc6245 7584 used_at = 1;
67c0d1eb 7585 load_address (AT, &offset_expr, &used_at);
252b5132 7586 if (breg != 0)
67c0d1eb 7587 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
252b5132
RH
7588 if (target_big_endian)
7589 expr1.X_add_number = 0;
67c0d1eb 7590 macro_build (&expr1, mask == M_ULH_A ? "lb" : "lbu", "t,o(b)",
17a2f251 7591 treg, BFD_RELOC_LO16, AT);
252b5132
RH
7592 if (target_big_endian)
7593 expr1.X_add_number = 1;
7594 else
7595 expr1.X_add_number = 0;
67c0d1eb
RS
7596 macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
7597 macro_build (NULL, "sll", "d,w,<", treg, treg, 8);
7598 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
252b5132
RH
7599 break;
7600
7601 case M_USH:
7602 if (offset_expr.X_add_number >= 0x7fff)
7603 as_bad (_("operand overflow"));
7604 if (target_big_endian)
f9419b05 7605 ++offset_expr.X_add_number;
67c0d1eb
RS
7606 macro_build (&offset_expr, "sb", "t,o(b)", treg, BFD_RELOC_LO16, breg);
7607 macro_build (NULL, "srl", "d,w,<", AT, treg, 8);
252b5132 7608 if (target_big_endian)
f9419b05 7609 --offset_expr.X_add_number;
252b5132 7610 else
f9419b05 7611 ++offset_expr.X_add_number;
67c0d1eb 7612 macro_build (&offset_expr, "sb", "t,o(b)", AT, BFD_RELOC_LO16, breg);
252b5132
RH
7613 break;
7614
7615 case M_USD:
7616 s = "sdl";
7617 s2 = "sdr";
7618 off = 7;
7619 goto usw;
7620 case M_USW:
7621 s = "swl";
7622 s2 = "swr";
7623 off = 3;
7624 usw:
7625 if (offset_expr.X_add_number >= 0x8000 - off)
7626 as_bad (_("operand overflow"));
7627 if (! target_big_endian)
7628 offset_expr.X_add_number += off;
67c0d1eb 7629 macro_build (&offset_expr, s, "t,o(b)", treg, BFD_RELOC_LO16, breg);
252b5132
RH
7630 if (! target_big_endian)
7631 offset_expr.X_add_number -= off;
7632 else
7633 offset_expr.X_add_number += off;
67c0d1eb 7634 macro_build (&offset_expr, s2, "t,o(b)", treg, BFD_RELOC_LO16, breg);
252b5132
RH
7635 return;
7636
7637 case M_USD_A:
7638 s = "sdl";
7639 s2 = "sdr";
7640 off = 7;
7641 goto uswa;
7642 case M_USW_A:
7643 s = "swl";
7644 s2 = "swr";
7645 off = 3;
7646 uswa:
d6bc6245 7647 used_at = 1;
67c0d1eb 7648 load_address (AT, &offset_expr, &used_at);
252b5132 7649 if (breg != 0)
67c0d1eb 7650 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
252b5132
RH
7651 if (! target_big_endian)
7652 expr1.X_add_number = off;
7653 else
7654 expr1.X_add_number = 0;
67c0d1eb 7655 macro_build (&expr1, s, "t,o(b)", treg, BFD_RELOC_LO16, AT);
252b5132
RH
7656 if (! target_big_endian)
7657 expr1.X_add_number = 0;
7658 else
7659 expr1.X_add_number = off;
67c0d1eb 7660 macro_build (&expr1, s2, "t,o(b)", treg, BFD_RELOC_LO16, AT);
252b5132
RH
7661 break;
7662
7663 case M_USH_A:
d6bc6245 7664 used_at = 1;
67c0d1eb 7665 load_address (AT, &offset_expr, &used_at);
252b5132 7666 if (breg != 0)
67c0d1eb 7667 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
252b5132
RH
7668 if (! target_big_endian)
7669 expr1.X_add_number = 0;
67c0d1eb
RS
7670 macro_build (&expr1, "sb", "t,o(b)", treg, BFD_RELOC_LO16, AT);
7671 macro_build (NULL, "srl", "d,w,<", treg, treg, 8);
252b5132
RH
7672 if (! target_big_endian)
7673 expr1.X_add_number = 1;
7674 else
7675 expr1.X_add_number = 0;
67c0d1eb 7676 macro_build (&expr1, "sb", "t,o(b)", treg, BFD_RELOC_LO16, AT);
252b5132
RH
7677 if (! target_big_endian)
7678 expr1.X_add_number = 0;
7679 else
7680 expr1.X_add_number = 1;
67c0d1eb
RS
7681 macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
7682 macro_build (NULL, "sll", "d,w,<", treg, treg, 8);
7683 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
252b5132
RH
7684 break;
7685
7686 default:
7687 /* FIXME: Check if this is one of the itbl macros, since they
bdaaa2e1 7688 are added dynamically. */
252b5132
RH
7689 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7690 break;
7691 }
7692 if (mips_opts.noat)
7693 as_warn (_("Macro used $at after \".set noat\""));
7694}
7695
7696/* Implement macros in mips16 mode. */
7697
7698static void
17a2f251 7699mips16_macro (struct mips_cl_insn *ip)
252b5132
RH
7700{
7701 int mask;
7702 int xreg, yreg, zreg, tmp;
252b5132
RH
7703 expressionS expr1;
7704 int dbl;
7705 const char *s, *s2, *s3;
7706
7707 mask = ip->insn_mo->mask;
7708
7709 xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
7710 yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
7711 zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
7712
252b5132
RH
7713 expr1.X_op = O_constant;
7714 expr1.X_op_symbol = NULL;
7715 expr1.X_add_symbol = NULL;
7716 expr1.X_add_number = 1;
7717
7718 dbl = 0;
7719
7720 switch (mask)
7721 {
7722 default:
7723 internalError ();
7724
7725 case M_DDIV_3:
7726 dbl = 1;
7727 case M_DIV_3:
7728 s = "mflo";
7729 goto do_div3;
7730 case M_DREM_3:
7731 dbl = 1;
7732 case M_REM_3:
7733 s = "mfhi";
7734 do_div3:
b34976b6 7735 mips_emit_delays (TRUE);
252b5132
RH
7736 ++mips_opts.noreorder;
7737 mips_any_noreorder = 1;
67c0d1eb 7738 macro_build (NULL, dbl ? "ddiv" : "div", "0,x,y", xreg, yreg);
252b5132 7739 expr1.X_add_number = 2;
67c0d1eb
RS
7740 macro_build (&expr1, "bnez", "x,p", yreg);
7741 macro_build (NULL, "break", "6", 7);
bdaaa2e1 7742
252b5132
RH
7743 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7744 since that causes an overflow. We should do that as well,
7745 but I don't see how to do the comparisons without a temporary
7746 register. */
7747 --mips_opts.noreorder;
67c0d1eb 7748 macro_build (NULL, s, "x", zreg);
252b5132
RH
7749 break;
7750
7751 case M_DIVU_3:
7752 s = "divu";
7753 s2 = "mflo";
7754 goto do_divu3;
7755 case M_REMU_3:
7756 s = "divu";
7757 s2 = "mfhi";
7758 goto do_divu3;
7759 case M_DDIVU_3:
7760 s = "ddivu";
7761 s2 = "mflo";
7762 goto do_divu3;
7763 case M_DREMU_3:
7764 s = "ddivu";
7765 s2 = "mfhi";
7766 do_divu3:
b34976b6 7767 mips_emit_delays (TRUE);
252b5132
RH
7768 ++mips_opts.noreorder;
7769 mips_any_noreorder = 1;
67c0d1eb 7770 macro_build (NULL, s, "0,x,y", xreg, yreg);
252b5132 7771 expr1.X_add_number = 2;
67c0d1eb
RS
7772 macro_build (&expr1, "bnez", "x,p", yreg);
7773 macro_build (NULL, "break", "6", 7);
252b5132 7774 --mips_opts.noreorder;
67c0d1eb 7775 macro_build (NULL, s2, "x", zreg);
252b5132
RH
7776 break;
7777
7778 case M_DMUL:
7779 dbl = 1;
7780 case M_MUL:
67c0d1eb
RS
7781 macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
7782 macro_build (NULL, "mflo", "x", zreg);
252b5132
RH
7783 return;
7784
7785 case M_DSUBU_I:
7786 dbl = 1;
7787 goto do_subu;
7788 case M_SUBU_I:
7789 do_subu:
7790 if (imm_expr.X_op != O_constant)
7791 as_bad (_("Unsupported large constant"));
7792 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 7793 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
252b5132
RH
7794 break;
7795
7796 case M_SUBU_I_2:
7797 if (imm_expr.X_op != O_constant)
7798 as_bad (_("Unsupported large constant"));
7799 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 7800 macro_build (&imm_expr, "addiu", "x,k", xreg);
252b5132
RH
7801 break;
7802
7803 case M_DSUBU_I_2:
7804 if (imm_expr.X_op != O_constant)
7805 as_bad (_("Unsupported large constant"));
7806 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 7807 macro_build (&imm_expr, "daddiu", "y,j", yreg);
252b5132
RH
7808 break;
7809
7810 case M_BEQ:
7811 s = "cmp";
7812 s2 = "bteqz";
7813 goto do_branch;
7814 case M_BNE:
7815 s = "cmp";
7816 s2 = "btnez";
7817 goto do_branch;
7818 case M_BLT:
7819 s = "slt";
7820 s2 = "btnez";
7821 goto do_branch;
7822 case M_BLTU:
7823 s = "sltu";
7824 s2 = "btnez";
7825 goto do_branch;
7826 case M_BLE:
7827 s = "slt";
7828 s2 = "bteqz";
7829 goto do_reverse_branch;
7830 case M_BLEU:
7831 s = "sltu";
7832 s2 = "bteqz";
7833 goto do_reverse_branch;
7834 case M_BGE:
7835 s = "slt";
7836 s2 = "bteqz";
7837 goto do_branch;
7838 case M_BGEU:
7839 s = "sltu";
7840 s2 = "bteqz";
7841 goto do_branch;
7842 case M_BGT:
7843 s = "slt";
7844 s2 = "btnez";
7845 goto do_reverse_branch;
7846 case M_BGTU:
7847 s = "sltu";
7848 s2 = "btnez";
7849
7850 do_reverse_branch:
7851 tmp = xreg;
7852 xreg = yreg;
7853 yreg = tmp;
7854
7855 do_branch:
67c0d1eb
RS
7856 macro_build (NULL, s, "x,y", xreg, yreg);
7857 macro_build (&offset_expr, s2, "p");
252b5132
RH
7858 break;
7859
7860 case M_BEQ_I:
7861 s = "cmpi";
7862 s2 = "bteqz";
7863 s3 = "x,U";
7864 goto do_branch_i;
7865 case M_BNE_I:
7866 s = "cmpi";
7867 s2 = "btnez";
7868 s3 = "x,U";
7869 goto do_branch_i;
7870 case M_BLT_I:
7871 s = "slti";
7872 s2 = "btnez";
7873 s3 = "x,8";
7874 goto do_branch_i;
7875 case M_BLTU_I:
7876 s = "sltiu";
7877 s2 = "btnez";
7878 s3 = "x,8";
7879 goto do_branch_i;
7880 case M_BLE_I:
7881 s = "slti";
7882 s2 = "btnez";
7883 s3 = "x,8";
7884 goto do_addone_branch_i;
7885 case M_BLEU_I:
7886 s = "sltiu";
7887 s2 = "btnez";
7888 s3 = "x,8";
7889 goto do_addone_branch_i;
7890 case M_BGE_I:
7891 s = "slti";
7892 s2 = "bteqz";
7893 s3 = "x,8";
7894 goto do_branch_i;
7895 case M_BGEU_I:
7896 s = "sltiu";
7897 s2 = "bteqz";
7898 s3 = "x,8";
7899 goto do_branch_i;
7900 case M_BGT_I:
7901 s = "slti";
7902 s2 = "bteqz";
7903 s3 = "x,8";
7904 goto do_addone_branch_i;
7905 case M_BGTU_I:
7906 s = "sltiu";
7907 s2 = "bteqz";
7908 s3 = "x,8";
7909
7910 do_addone_branch_i:
7911 if (imm_expr.X_op != O_constant)
7912 as_bad (_("Unsupported large constant"));
7913 ++imm_expr.X_add_number;
7914
7915 do_branch_i:
67c0d1eb
RS
7916 macro_build (&imm_expr, s, s3, xreg);
7917 macro_build (&offset_expr, s2, "p");
252b5132
RH
7918 break;
7919
7920 case M_ABS:
7921 expr1.X_add_number = 0;
67c0d1eb 7922 macro_build (&expr1, "slti", "x,8", yreg);
252b5132 7923 if (xreg != yreg)
67c0d1eb 7924 move_register (xreg, yreg);
252b5132 7925 expr1.X_add_number = 2;
67c0d1eb
RS
7926 macro_build (&expr1, "bteqz", "p");
7927 macro_build (NULL, "neg", "x,w", xreg, xreg);
252b5132
RH
7928 }
7929}
7930
7931/* For consistency checking, verify that all bits are specified either
7932 by the match/mask part of the instruction definition, or by the
7933 operand list. */
7934static int
17a2f251 7935validate_mips_insn (const struct mips_opcode *opc)
252b5132
RH
7936{
7937 const char *p = opc->args;
7938 char c;
7939 unsigned long used_bits = opc->mask;
7940
7941 if ((used_bits & opc->match) != opc->match)
7942 {
7943 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
7944 opc->name, opc->args);
7945 return 0;
7946 }
7947#define USE_BITS(mask,shift) (used_bits |= ((mask) << (shift)))
7948 while (*p)
7949 switch (c = *p++)
7950 {
7951 case ',': break;
7952 case '(': break;
7953 case ')': break;
af7ee8bf
CD
7954 case '+':
7955 switch (c = *p++)
7956 {
7957 case 'A': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7958 case 'B': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
7959 case 'C': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
bbcc0807
CD
7960 case 'D': USE_BITS (OP_MASK_RD, OP_SH_RD);
7961 USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
5f74bc13
CD
7962 case 'E': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7963 case 'F': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
7964 case 'G': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
7965 case 'H': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
7966 case 'I': break;
af7ee8bf
CD
7967 default:
7968 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
7969 c, opc->name, opc->args);
7970 return 0;
7971 }
7972 break;
252b5132
RH
7973 case '<': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7974 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7975 case 'A': break;
4372b673 7976 case 'B': USE_BITS (OP_MASK_CODE20, OP_SH_CODE20); break;
252b5132
RH
7977 case 'C': USE_BITS (OP_MASK_COPZ, OP_SH_COPZ); break;
7978 case 'D': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
7979 case 'E': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7980 case 'F': break;
7981 case 'G': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
156c2f8b 7982 case 'H': USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
252b5132 7983 case 'I': break;
e972090a 7984 case 'J': USE_BITS (OP_MASK_CODE19, OP_SH_CODE19); break;
af7ee8bf 7985 case 'K': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
252b5132
RH
7986 case 'L': break;
7987 case 'M': USE_BITS (OP_MASK_CCC, OP_SH_CCC); break;
7988 case 'N': USE_BITS (OP_MASK_BCC, OP_SH_BCC); break;
deec1734
CD
7989 case 'O': USE_BITS (OP_MASK_ALN, OP_SH_ALN); break;
7990 case 'Q': USE_BITS (OP_MASK_VSEL, OP_SH_VSEL);
7991 USE_BITS (OP_MASK_FT, OP_SH_FT); break;
252b5132
RH
7992 case 'R': USE_BITS (OP_MASK_FR, OP_SH_FR); break;
7993 case 'S': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7994 case 'T': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7995 case 'V': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7996 case 'W': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
deec1734
CD
7997 case 'X': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
7998 case 'Y': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7999 case 'Z': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
252b5132
RH
8000 case 'a': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break;
8001 case 'b': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8002 case 'c': USE_BITS (OP_MASK_CODE, OP_SH_CODE); break;
8003 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
8004 case 'f': break;
8005 case 'h': USE_BITS (OP_MASK_PREFX, OP_SH_PREFX); break;
8006 case 'i': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
8007 case 'j': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
8008 case 'k': USE_BITS (OP_MASK_CACHE, OP_SH_CACHE); break;
8009 case 'l': break;
8010 case 'o': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
8011 case 'p': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
8012 case 'q': USE_BITS (OP_MASK_CODE2, OP_SH_CODE2); break;
8013 case 'r': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8014 case 's': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8015 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8016 case 'u': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
8017 case 'v': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8018 case 'w': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8019 case 'x': break;
8020 case 'z': break;
8021 case 'P': USE_BITS (OP_MASK_PERFREG, OP_SH_PERFREG); break;
4372b673
NC
8022 case 'U': USE_BITS (OP_MASK_RD, OP_SH_RD);
8023 USE_BITS (OP_MASK_RT, OP_SH_RT); break;
60b63b72
RS
8024 case 'e': USE_BITS (OP_MASK_VECBYTE, OP_SH_VECBYTE); break;
8025 case '%': USE_BITS (OP_MASK_VECALIGN, OP_SH_VECALIGN); break;
8026 case '[': break;
8027 case ']': break;
252b5132
RH
8028 default:
8029 as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
8030 c, opc->name, opc->args);
8031 return 0;
8032 }
8033#undef USE_BITS
8034 if (used_bits != 0xffffffff)
8035 {
8036 as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
8037 ~used_bits & 0xffffffff, opc->name, opc->args);
8038 return 0;
8039 }
8040 return 1;
8041}
8042
8043/* This routine assembles an instruction into its binary format. As a
8044 side effect, it sets one of the global variables imm_reloc or
8045 offset_reloc to the type of relocation to do if one of the operands
8046 is an address expression. */
8047
8048static void
17a2f251 8049mips_ip (char *str, struct mips_cl_insn *ip)
252b5132
RH
8050{
8051 char *s;
8052 const char *args;
43841e91 8053 char c = 0;
252b5132
RH
8054 struct mips_opcode *insn;
8055 char *argsStart;
8056 unsigned int regno;
8057 unsigned int lastregno = 0;
af7ee8bf 8058 unsigned int lastpos = 0;
071742cf 8059 unsigned int limlo, limhi;
252b5132
RH
8060 char *s_reset;
8061 char save_c = 0;
252b5132
RH
8062
8063 insn_error = NULL;
8064
8065 /* If the instruction contains a '.', we first try to match an instruction
8066 including the '.'. Then we try again without the '.'. */
8067 insn = NULL;
3882b010 8068 for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
252b5132
RH
8069 continue;
8070
8071 /* If we stopped on whitespace, then replace the whitespace with null for
8072 the call to hash_find. Save the character we replaced just in case we
8073 have to re-parse the instruction. */
3882b010 8074 if (ISSPACE (*s))
252b5132
RH
8075 {
8076 save_c = *s;
8077 *s++ = '\0';
8078 }
bdaaa2e1 8079
252b5132
RH
8080 insn = (struct mips_opcode *) hash_find (op_hash, str);
8081
8082 /* If we didn't find the instruction in the opcode table, try again, but
8083 this time with just the instruction up to, but not including the
8084 first '.'. */
8085 if (insn == NULL)
8086 {
bdaaa2e1 8087 /* Restore the character we overwrite above (if any). */
252b5132
RH
8088 if (save_c)
8089 *(--s) = save_c;
8090
8091 /* Scan up to the first '.' or whitespace. */
3882b010
L
8092 for (s = str;
8093 *s != '\0' && *s != '.' && !ISSPACE (*s);
8094 ++s)
252b5132
RH
8095 continue;
8096
8097 /* If we did not find a '.', then we can quit now. */
8098 if (*s != '.')
8099 {
8100 insn_error = "unrecognized opcode";
8101 return;
8102 }
8103
8104 /* Lookup the instruction in the hash table. */
8105 *s++ = '\0';
8106 if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
8107 {
8108 insn_error = "unrecognized opcode";
8109 return;
8110 }
252b5132
RH
8111 }
8112
8113 argsStart = s;
8114 for (;;)
8115 {
b34976b6 8116 bfd_boolean ok;
252b5132
RH
8117
8118 assert (strcmp (insn->name, str) == 0);
8119
1f25f5d3
CD
8120 if (OPCODE_IS_MEMBER (insn,
8121 (mips_opts.isa
3396de36 8122 | (file_ase_mips16 ? INSN_MIPS16 : 0)
deec1734 8123 | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
98d3f06f 8124 | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
fef14a42 8125 mips_opts.arch))
b34976b6 8126 ok = TRUE;
bdaaa2e1 8127 else
b34976b6 8128 ok = FALSE;
bdaaa2e1 8129
252b5132
RH
8130 if (insn->pinfo != INSN_MACRO)
8131 {
fef14a42 8132 if (mips_opts.arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
b34976b6 8133 ok = FALSE;
252b5132
RH
8134 }
8135
8136 if (! ok)
8137 {
8138 if (insn + 1 < &mips_opcodes[NUMOPCODES]
8139 && strcmp (insn->name, insn[1].name) == 0)
8140 {
8141 ++insn;
8142 continue;
8143 }
252b5132 8144 else
beae10d5 8145 {
268f6bed
L
8146 if (!insn_error)
8147 {
8148 static char buf[100];
fef14a42
TS
8149 sprintf (buf,
8150 _("opcode not supported on this processor: %s (%s)"),
8151 mips_cpu_info_from_arch (mips_opts.arch)->name,
8152 mips_cpu_info_from_isa (mips_opts.isa)->name);
268f6bed
L
8153 insn_error = buf;
8154 }
8155 if (save_c)
8156 *(--s) = save_c;
2bd7f1f3 8157 return;
252b5132 8158 }
252b5132
RH
8159 }
8160
8161 ip->insn_mo = insn;
8162 ip->insn_opcode = insn->match;
268f6bed 8163 insn_error = NULL;
252b5132
RH
8164 for (args = insn->args;; ++args)
8165 {
deec1734
CD
8166 int is_mdmx;
8167
ad8d3bb3 8168 s += strspn (s, " \t");
deec1734 8169 is_mdmx = 0;
252b5132
RH
8170 switch (*args)
8171 {
8172 case '\0': /* end of args */
8173 if (*s == '\0')
8174 return;
8175 break;
8176
8177 case ',':
8178 if (*s++ == *args)
8179 continue;
8180 s--;
8181 switch (*++args)
8182 {
8183 case 'r':
8184 case 'v':
38487616 8185 ip->insn_opcode |= lastregno << OP_SH_RS;
252b5132
RH
8186 continue;
8187
8188 case 'w':
38487616
TS
8189 ip->insn_opcode |= lastregno << OP_SH_RT;
8190 continue;
8191
252b5132 8192 case 'W':
38487616 8193 ip->insn_opcode |= lastregno << OP_SH_FT;
252b5132
RH
8194 continue;
8195
8196 case 'V':
38487616 8197 ip->insn_opcode |= lastregno << OP_SH_FS;
252b5132
RH
8198 continue;
8199 }
8200 break;
8201
8202 case '(':
8203 /* Handle optional base register.
8204 Either the base register is omitted or
bdaaa2e1 8205 we must have a left paren. */
252b5132
RH
8206 /* This is dependent on the next operand specifier
8207 is a base register specification. */
8208 assert (args[1] == 'b' || args[1] == '5'
8209 || args[1] == '-' || args[1] == '4');
8210 if (*s == '\0')
8211 return;
8212
8213 case ')': /* these must match exactly */
60b63b72
RS
8214 case '[':
8215 case ']':
252b5132
RH
8216 if (*s++ == *args)
8217 continue;
8218 break;
8219
af7ee8bf
CD
8220 case '+': /* Opcode extension character. */
8221 switch (*++args)
8222 {
071742cf
CD
8223 case 'A': /* ins/ext position, becomes LSB. */
8224 limlo = 0;
8225 limhi = 31;
5f74bc13
CD
8226 goto do_lsb;
8227 case 'E':
8228 limlo = 32;
8229 limhi = 63;
8230 goto do_lsb;
8231do_lsb:
071742cf
CD
8232 my_getExpression (&imm_expr, s);
8233 check_absolute_expr (ip, &imm_expr);
8234 if ((unsigned long) imm_expr.X_add_number < limlo
8235 || (unsigned long) imm_expr.X_add_number > limhi)
8236 {
8237 as_bad (_("Improper position (%lu)"),
8238 (unsigned long) imm_expr.X_add_number);
8239 imm_expr.X_add_number = limlo;
8240 }
8241 lastpos = imm_expr.X_add_number;
8242 ip->insn_opcode |= (imm_expr.X_add_number
8243 & OP_MASK_SHAMT) << OP_SH_SHAMT;
8244 imm_expr.X_op = O_absent;
8245 s = expr_end;
8246 continue;
8247
8248 case 'B': /* ins size, becomes MSB. */
8249 limlo = 1;
8250 limhi = 32;
5f74bc13
CD
8251 goto do_msb;
8252 case 'F':
8253 limlo = 33;
8254 limhi = 64;
8255 goto do_msb;
8256do_msb:
071742cf
CD
8257 my_getExpression (&imm_expr, s);
8258 check_absolute_expr (ip, &imm_expr);
8259 /* Check for negative input so that small negative numbers
8260 will not succeed incorrectly. The checks against
8261 (pos+size) transitively check "size" itself,
8262 assuming that "pos" is reasonable. */
8263 if ((long) imm_expr.X_add_number < 0
8264 || ((unsigned long) imm_expr.X_add_number
8265 + lastpos) < limlo
8266 || ((unsigned long) imm_expr.X_add_number
8267 + lastpos) > limhi)
8268 {
8269 as_bad (_("Improper insert size (%lu, position %lu)"),
8270 (unsigned long) imm_expr.X_add_number,
8271 (unsigned long) lastpos);
8272 imm_expr.X_add_number = limlo - lastpos;
8273 }
8274 ip->insn_opcode |= ((lastpos + imm_expr.X_add_number - 1)
8275 & OP_MASK_INSMSB) << OP_SH_INSMSB;
8276 imm_expr.X_op = O_absent;
8277 s = expr_end;
8278 continue;
8279
8280 case 'C': /* ext size, becomes MSBD. */
8281 limlo = 1;
8282 limhi = 32;
5f74bc13
CD
8283 goto do_msbd;
8284 case 'G':
8285 limlo = 33;
8286 limhi = 64;
8287 goto do_msbd;
8288 case 'H':
8289 limlo = 33;
8290 limhi = 64;
8291 goto do_msbd;
8292do_msbd:
071742cf
CD
8293 my_getExpression (&imm_expr, s);
8294 check_absolute_expr (ip, &imm_expr);
8295 /* Check for negative input so that small negative numbers
8296 will not succeed incorrectly. The checks against
8297 (pos+size) transitively check "size" itself,
8298 assuming that "pos" is reasonable. */
8299 if ((long) imm_expr.X_add_number < 0
8300 || ((unsigned long) imm_expr.X_add_number
8301 + lastpos) < limlo
8302 || ((unsigned long) imm_expr.X_add_number
8303 + lastpos) > limhi)
8304 {
8305 as_bad (_("Improper extract size (%lu, position %lu)"),
8306 (unsigned long) imm_expr.X_add_number,
8307 (unsigned long) lastpos);
8308 imm_expr.X_add_number = limlo - lastpos;
8309 }
8310 ip->insn_opcode |= ((imm_expr.X_add_number - 1)
8311 & OP_MASK_EXTMSBD) << OP_SH_EXTMSBD;
8312 imm_expr.X_op = O_absent;
8313 s = expr_end;
8314 continue;
af7ee8bf 8315
bbcc0807
CD
8316 case 'D':
8317 /* +D is for disassembly only; never match. */
8318 break;
8319
5f74bc13
CD
8320 case 'I':
8321 /* "+I" is like "I", except that imm2_expr is used. */
8322 my_getExpression (&imm2_expr, s);
8323 if (imm2_expr.X_op != O_big
8324 && imm2_expr.X_op != O_constant)
8325 insn_error = _("absolute expression required");
13757d0c 8326 normalize_constant_expr (&imm2_expr);
5f74bc13
CD
8327 s = expr_end;
8328 continue;
8329
af7ee8bf
CD
8330 default:
8331 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8332 *args, insn->name, insn->args);
8333 /* Further processing is fruitless. */
8334 return;
8335 }
8336 break;
8337
252b5132
RH
8338 case '<': /* must be at least one digit */
8339 /*
8340 * According to the manual, if the shift amount is greater
b6ff326e
KH
8341 * than 31 or less than 0, then the shift amount should be
8342 * mod 32. In reality the mips assembler issues an error.
252b5132
RH
8343 * We issue a warning and mask out all but the low 5 bits.
8344 */
8345 my_getExpression (&imm_expr, s);
8346 check_absolute_expr (ip, &imm_expr);
8347 if ((unsigned long) imm_expr.X_add_number > 31)
8348 {
793b27f4
TS
8349 as_warn (_("Improper shift amount (%lu)"),
8350 (unsigned long) imm_expr.X_add_number);
38487616 8351 imm_expr.X_add_number &= OP_MASK_SHAMT;
252b5132 8352 }
38487616 8353 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SHAMT;
252b5132
RH
8354 imm_expr.X_op = O_absent;
8355 s = expr_end;
8356 continue;
8357
8358 case '>': /* shift amount minus 32 */
8359 my_getExpression (&imm_expr, s);
8360 check_absolute_expr (ip, &imm_expr);
8361 if ((unsigned long) imm_expr.X_add_number < 32
8362 || (unsigned long) imm_expr.X_add_number > 63)
8363 break;
38487616 8364 ip->insn_opcode |= (imm_expr.X_add_number - 32) << OP_SH_SHAMT;
252b5132
RH
8365 imm_expr.X_op = O_absent;
8366 s = expr_end;
8367 continue;
8368
252b5132
RH
8369 case 'k': /* cache code */
8370 case 'h': /* prefx code */
8371 my_getExpression (&imm_expr, s);
8372 check_absolute_expr (ip, &imm_expr);
8373 if ((unsigned long) imm_expr.X_add_number > 31)
8374 {
8375 as_warn (_("Invalid value for `%s' (%lu)"),
8376 ip->insn_mo->name,
8377 (unsigned long) imm_expr.X_add_number);
8378 imm_expr.X_add_number &= 0x1f;
8379 }
8380 if (*args == 'k')
8381 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
8382 else
8383 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
8384 imm_expr.X_op = O_absent;
8385 s = expr_end;
8386 continue;
8387
8388 case 'c': /* break code */
8389 my_getExpression (&imm_expr, s);
8390 check_absolute_expr (ip, &imm_expr);
793b27f4 8391 if ((unsigned long) imm_expr.X_add_number > 1023)
252b5132 8392 {
793b27f4
TS
8393 as_warn (_("Illegal break code (%lu)"),
8394 (unsigned long) imm_expr.X_add_number);
38487616 8395 imm_expr.X_add_number &= OP_MASK_CODE;
252b5132 8396 }
38487616 8397 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE;
252b5132
RH
8398 imm_expr.X_op = O_absent;
8399 s = expr_end;
8400 continue;
8401
8402 case 'q': /* lower break code */
8403 my_getExpression (&imm_expr, s);
8404 check_absolute_expr (ip, &imm_expr);
793b27f4 8405 if ((unsigned long) imm_expr.X_add_number > 1023)
252b5132 8406 {
793b27f4
TS
8407 as_warn (_("Illegal lower break code (%lu)"),
8408 (unsigned long) imm_expr.X_add_number);
38487616 8409 imm_expr.X_add_number &= OP_MASK_CODE2;
252b5132 8410 }
38487616 8411 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE2;
252b5132
RH
8412 imm_expr.X_op = O_absent;
8413 s = expr_end;
8414 continue;
8415
4372b673 8416 case 'B': /* 20-bit syscall/break code. */
156c2f8b 8417 my_getExpression (&imm_expr, s);
156c2f8b 8418 check_absolute_expr (ip, &imm_expr);
793b27f4
TS
8419 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
8420 as_warn (_("Illegal 20-bit code (%lu)"),
8421 (unsigned long) imm_expr.X_add_number);
38487616 8422 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE20;
252b5132
RH
8423 imm_expr.X_op = O_absent;
8424 s = expr_end;
8425 continue;
8426
98d3f06f 8427 case 'C': /* Coprocessor code */
beae10d5 8428 my_getExpression (&imm_expr, s);
252b5132 8429 check_absolute_expr (ip, &imm_expr);
98d3f06f 8430 if ((unsigned long) imm_expr.X_add_number >= (1 << 25))
252b5132 8431 {
793b27f4
TS
8432 as_warn (_("Coproccesor code > 25 bits (%lu)"),
8433 (unsigned long) imm_expr.X_add_number);
98d3f06f 8434 imm_expr.X_add_number &= ((1 << 25) - 1);
252b5132 8435 }
beae10d5
KH
8436 ip->insn_opcode |= imm_expr.X_add_number;
8437 imm_expr.X_op = O_absent;
8438 s = expr_end;
8439 continue;
252b5132 8440
4372b673
NC
8441 case 'J': /* 19-bit wait code. */
8442 my_getExpression (&imm_expr, s);
8443 check_absolute_expr (ip, &imm_expr);
793b27f4
TS
8444 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
8445 as_warn (_("Illegal 19-bit code (%lu)"),
8446 (unsigned long) imm_expr.X_add_number);
38487616 8447 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE19;
4372b673
NC
8448 imm_expr.X_op = O_absent;
8449 s = expr_end;
8450 continue;
8451
252b5132 8452 case 'P': /* Performance register */
beae10d5 8453 my_getExpression (&imm_expr, s);
252b5132 8454 check_absolute_expr (ip, &imm_expr);
beae10d5 8455 if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
252b5132 8456 {
793b27f4
TS
8457 as_warn (_("Invalid performance register (%lu)"),
8458 (unsigned long) imm_expr.X_add_number);
38487616 8459 imm_expr.X_add_number &= OP_MASK_PERFREG;
252b5132 8460 }
38487616 8461 ip->insn_opcode |= (imm_expr.X_add_number << OP_SH_PERFREG);
beae10d5
KH
8462 imm_expr.X_op = O_absent;
8463 s = expr_end;
8464 continue;
252b5132
RH
8465
8466 case 'b': /* base register */
8467 case 'd': /* destination register */
8468 case 's': /* source register */
8469 case 't': /* target register */
8470 case 'r': /* both target and source */
8471 case 'v': /* both dest and source */
8472 case 'w': /* both dest and target */
8473 case 'E': /* coprocessor target register */
8474 case 'G': /* coprocessor destination register */
af7ee8bf 8475 case 'K': /* 'rdhwr' destination register */
252b5132
RH
8476 case 'x': /* ignore register name */
8477 case 'z': /* must be zero register */
4372b673 8478 case 'U': /* destination register (clo/clz). */
252b5132
RH
8479 s_reset = s;
8480 if (s[0] == '$')
8481 {
8482
3882b010 8483 if (ISDIGIT (s[1]))
252b5132
RH
8484 {
8485 ++s;
8486 regno = 0;
8487 do
8488 {
8489 regno *= 10;
8490 regno += *s - '0';
8491 ++s;
8492 }
3882b010 8493 while (ISDIGIT (*s));
252b5132
RH
8494 if (regno > 31)
8495 as_bad (_("Invalid register number (%d)"), regno);
8496 }
af7ee8bf 8497 else if (*args == 'E' || *args == 'G' || *args == 'K')
252b5132
RH
8498 goto notreg;
8499 else
8500 {
76db943d
TS
8501 if (s[1] == 'r' && s[2] == 'a')
8502 {
8503 s += 3;
8504 regno = RA;
8505 }
8506 else if (s[1] == 'f' && s[2] == 'p')
252b5132
RH
8507 {
8508 s += 3;
8509 regno = FP;
8510 }
8511 else if (s[1] == 's' && s[2] == 'p')
8512 {
8513 s += 3;
8514 regno = SP;
8515 }
8516 else if (s[1] == 'g' && s[2] == 'p')
8517 {
8518 s += 3;
8519 regno = GP;
8520 }
8521 else if (s[1] == 'a' && s[2] == 't')
8522 {
8523 s += 3;
8524 regno = AT;
8525 }
8526 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8527 {
8528 s += 4;
8529 regno = KT0;
8530 }
8531 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8532 {
8533 s += 4;
8534 regno = KT1;
8535 }
85b51719
TS
8536 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
8537 {
8538 s += 5;
8539 regno = ZERO;
8540 }
252b5132
RH
8541 else if (itbl_have_entries)
8542 {
8543 char *p, *n;
d7ba4a77 8544 unsigned long r;
252b5132 8545
d7ba4a77 8546 p = s + 1; /* advance past '$' */
252b5132
RH
8547 n = itbl_get_field (&p); /* n is name */
8548
d7ba4a77
ILT
8549 /* See if this is a register defined in an
8550 itbl entry. */
8551 if (itbl_get_reg_val (n, &r))
252b5132
RH
8552 {
8553 /* Get_field advances to the start of
8554 the next field, so we need to back
d7ba4a77 8555 rack to the end of the last field. */
bdaaa2e1 8556 if (p)
252b5132 8557 s = p - 1;
bdaaa2e1 8558 else
d7ba4a77 8559 s = strchr (s, '\0');
252b5132
RH
8560 regno = r;
8561 }
8562 else
8563 goto notreg;
beae10d5 8564 }
252b5132
RH
8565 else
8566 goto notreg;
8567 }
8568 if (regno == AT
8569 && ! mips_opts.noat
8570 && *args != 'E'
af7ee8bf
CD
8571 && *args != 'G'
8572 && *args != 'K')
252b5132
RH
8573 as_warn (_("Used $at without \".set noat\""));
8574 c = *args;
8575 if (*s == ' ')
f9419b05 8576 ++s;
252b5132
RH
8577 if (args[1] != *s)
8578 {
8579 if (c == 'r' || c == 'v' || c == 'w')
8580 {
8581 regno = lastregno;
8582 s = s_reset;
f9419b05 8583 ++args;
252b5132
RH
8584 }
8585 }
8586 /* 'z' only matches $0. */
8587 if (c == 'z' && regno != 0)
8588 break;
8589
bdaaa2e1
KH
8590 /* Now that we have assembled one operand, we use the args string
8591 * to figure out where it goes in the instruction. */
252b5132
RH
8592 switch (c)
8593 {
8594 case 'r':
8595 case 's':
8596 case 'v':
8597 case 'b':
38487616 8598 ip->insn_opcode |= regno << OP_SH_RS;
252b5132
RH
8599 break;
8600 case 'd':
8601 case 'G':
af7ee8bf 8602 case 'K':
38487616 8603 ip->insn_opcode |= regno << OP_SH_RD;
252b5132 8604 break;
4372b673 8605 case 'U':
38487616
TS
8606 ip->insn_opcode |= regno << OP_SH_RD;
8607 ip->insn_opcode |= regno << OP_SH_RT;
4372b673 8608 break;
252b5132
RH
8609 case 'w':
8610 case 't':
8611 case 'E':
38487616 8612 ip->insn_opcode |= regno << OP_SH_RT;
252b5132
RH
8613 break;
8614 case 'x':
8615 /* This case exists because on the r3000 trunc
8616 expands into a macro which requires a gp
8617 register. On the r6000 or r4000 it is
8618 assembled into a single instruction which
8619 ignores the register. Thus the insn version
8620 is MIPS_ISA2 and uses 'x', and the macro
8621 version is MIPS_ISA1 and uses 't'. */
8622 break;
8623 case 'z':
8624 /* This case is for the div instruction, which
8625 acts differently if the destination argument
8626 is $0. This only matches $0, and is checked
8627 outside the switch. */
8628 break;
8629 case 'D':
8630 /* Itbl operand; not yet implemented. FIXME ?? */
8631 break;
8632 /* What about all other operands like 'i', which
8633 can be specified in the opcode table? */
8634 }
8635 lastregno = regno;
8636 continue;
8637 }
8638 notreg:
8639 switch (*args++)
8640 {
8641 case 'r':
8642 case 'v':
38487616 8643 ip->insn_opcode |= lastregno << OP_SH_RS;
252b5132
RH
8644 continue;
8645 case 'w':
38487616 8646 ip->insn_opcode |= lastregno << OP_SH_RT;
252b5132
RH
8647 continue;
8648 }
8649 break;
8650
deec1734
CD
8651 case 'O': /* MDMX alignment immediate constant. */
8652 my_getExpression (&imm_expr, s);
8653 check_absolute_expr (ip, &imm_expr);
8654 if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
8655 {
8656 as_warn ("Improper align amount (%ld), using low bits",
8657 (long) imm_expr.X_add_number);
8658 imm_expr.X_add_number &= OP_MASK_ALN;
8659 }
8660 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_ALN;
8661 imm_expr.X_op = O_absent;
8662 s = expr_end;
8663 continue;
8664
8665 case 'Q': /* MDMX vector, element sel, or const. */
8666 if (s[0] != '$')
8667 {
8668 /* MDMX Immediate. */
8669 my_getExpression (&imm_expr, s);
8670 check_absolute_expr (ip, &imm_expr);
8671 if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
8672 {
8673 as_warn (_("Invalid MDMX Immediate (%ld)"),
8674 (long) imm_expr.X_add_number);
8675 imm_expr.X_add_number &= OP_MASK_FT;
8676 }
8677 imm_expr.X_add_number &= OP_MASK_FT;
8678 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8679 ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
8680 else
8681 ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
8682 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_FT;
8683 imm_expr.X_op = O_absent;
8684 s = expr_end;
8685 continue;
8686 }
8687 /* Not MDMX Immediate. Fall through. */
8688 case 'X': /* MDMX destination register. */
8689 case 'Y': /* MDMX source register. */
8690 case 'Z': /* MDMX target register. */
8691 is_mdmx = 1;
252b5132
RH
8692 case 'D': /* floating point destination register */
8693 case 'S': /* floating point source register */
8694 case 'T': /* floating point target register */
8695 case 'R': /* floating point source register */
8696 case 'V':
8697 case 'W':
8698 s_reset = s;
deec1734
CD
8699 /* Accept $fN for FP and MDMX register numbers, and in
8700 addition accept $vN for MDMX register numbers. */
8701 if ((s[0] == '$' && s[1] == 'f' && ISDIGIT (s[2]))
8702 || (is_mdmx != 0 && s[0] == '$' && s[1] == 'v'
8703 && ISDIGIT (s[2])))
252b5132
RH
8704 {
8705 s += 2;
8706 regno = 0;
8707 do
8708 {
8709 regno *= 10;
8710 regno += *s - '0';
8711 ++s;
8712 }
3882b010 8713 while (ISDIGIT (*s));
252b5132
RH
8714
8715 if (regno > 31)
8716 as_bad (_("Invalid float register number (%d)"), regno);
8717
8718 if ((regno & 1) != 0
ca4e0257 8719 && HAVE_32BIT_FPRS
252b5132
RH
8720 && ! (strcmp (str, "mtc1") == 0
8721 || strcmp (str, "mfc1") == 0
8722 || strcmp (str, "lwc1") == 0
8723 || strcmp (str, "swc1") == 0
8724 || strcmp (str, "l.s") == 0
8725 || strcmp (str, "s.s") == 0))
8726 as_warn (_("Float register should be even, was %d"),
8727 regno);
8728
8729 c = *args;
8730 if (*s == ' ')
f9419b05 8731 ++s;
252b5132
RH
8732 if (args[1] != *s)
8733 {
8734 if (c == 'V' || c == 'W')
8735 {
8736 regno = lastregno;
8737 s = s_reset;
f9419b05 8738 ++args;
252b5132
RH
8739 }
8740 }
8741 switch (c)
8742 {
8743 case 'D':
deec1734 8744 case 'X':
38487616 8745 ip->insn_opcode |= regno << OP_SH_FD;
252b5132
RH
8746 break;
8747 case 'V':
8748 case 'S':
deec1734 8749 case 'Y':
38487616 8750 ip->insn_opcode |= regno << OP_SH_FS;
252b5132 8751 break;
deec1734
CD
8752 case 'Q':
8753 /* This is like 'Z', but also needs to fix the MDMX
8754 vector/scalar select bits. Note that the
8755 scalar immediate case is handled above. */
8756 if (*s == '[')
8757 {
8758 int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
8759 int max_el = (is_qh ? 3 : 7);
8760 s++;
8761 my_getExpression(&imm_expr, s);
8762 check_absolute_expr (ip, &imm_expr);
8763 s = expr_end;
8764 if (imm_expr.X_add_number > max_el)
8765 as_bad(_("Bad element selector %ld"),
8766 (long) imm_expr.X_add_number);
8767 imm_expr.X_add_number &= max_el;
8768 ip->insn_opcode |= (imm_expr.X_add_number
8769 << (OP_SH_VSEL +
8770 (is_qh ? 2 : 1)));
8771 if (*s != ']')
8772 as_warn(_("Expecting ']' found '%s'"), s);
8773 else
8774 s++;
8775 }
8776 else
8777 {
8778 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8779 ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
8780 << OP_SH_VSEL);
8781 else
8782 ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
8783 OP_SH_VSEL);
8784 }
8785 /* Fall through */
252b5132
RH
8786 case 'W':
8787 case 'T':
deec1734 8788 case 'Z':
38487616 8789 ip->insn_opcode |= regno << OP_SH_FT;
252b5132
RH
8790 break;
8791 case 'R':
38487616 8792 ip->insn_opcode |= regno << OP_SH_FR;
252b5132
RH
8793 break;
8794 }
8795 lastregno = regno;
8796 continue;
8797 }
8798
252b5132
RH
8799 switch (*args++)
8800 {
8801 case 'V':
38487616 8802 ip->insn_opcode |= lastregno << OP_SH_FS;
252b5132
RH
8803 continue;
8804 case 'W':
38487616 8805 ip->insn_opcode |= lastregno << OP_SH_FT;
252b5132
RH
8806 continue;
8807 }
8808 break;
8809
8810 case 'I':
8811 my_getExpression (&imm_expr, s);
8812 if (imm_expr.X_op != O_big
8813 && imm_expr.X_op != O_constant)
8814 insn_error = _("absolute expression required");
13757d0c 8815 normalize_constant_expr (&imm_expr);
252b5132
RH
8816 s = expr_end;
8817 continue;
8818
8819 case 'A':
8820 my_getExpression (&offset_expr, s);
f6688943 8821 *imm_reloc = BFD_RELOC_32;
252b5132
RH
8822 s = expr_end;
8823 continue;
8824
8825 case 'F':
8826 case 'L':
8827 case 'f':
8828 case 'l':
8829 {
8830 int f64;
ca4e0257 8831 int using_gprs;
252b5132
RH
8832 char *save_in;
8833 char *err;
8834 unsigned char temp[8];
8835 int len;
8836 unsigned int length;
8837 segT seg;
8838 subsegT subseg;
8839 char *p;
8840
8841 /* These only appear as the last operand in an
8842 instruction, and every instruction that accepts
8843 them in any variant accepts them in all variants.
8844 This means we don't have to worry about backing out
8845 any changes if the instruction does not match.
8846
8847 The difference between them is the size of the
8848 floating point constant and where it goes. For 'F'
8849 and 'L' the constant is 64 bits; for 'f' and 'l' it
8850 is 32 bits. Where the constant is placed is based
8851 on how the MIPS assembler does things:
8852 F -- .rdata
8853 L -- .lit8
8854 f -- immediate value
8855 l -- .lit4
8856
8857 The .lit4 and .lit8 sections are only used if
8858 permitted by the -G argument.
8859
8860 When generating embedded PIC code, we use the
8861 .lit8 section but not the .lit4 section (we can do
8862 .lit4 inline easily; we need to put .lit8
8863 somewhere in the data segment, and using .lit8
8864 permits the linker to eventually combine identical
ca4e0257
RS
8865 .lit8 entries).
8866
8867 The code below needs to know whether the target register
8868 is 32 or 64 bits wide. It relies on the fact 'f' and
8869 'F' are used with GPR-based instructions and 'l' and
8870 'L' are used with FPR-based instructions. */
252b5132
RH
8871
8872 f64 = *args == 'F' || *args == 'L';
ca4e0257 8873 using_gprs = *args == 'F' || *args == 'f';
252b5132
RH
8874
8875 save_in = input_line_pointer;
8876 input_line_pointer = s;
8877 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
8878 length = len;
8879 s = input_line_pointer;
8880 input_line_pointer = save_in;
8881 if (err != NULL && *err != '\0')
8882 {
8883 as_bad (_("Bad floating point constant: %s"), err);
8884 memset (temp, '\0', sizeof temp);
8885 length = f64 ? 8 : 4;
8886 }
8887
156c2f8b 8888 assert (length == (unsigned) (f64 ? 8 : 4));
252b5132
RH
8889
8890 if (*args == 'f'
8891 || (*args == 'l'
4d0d148d 8892 && (mips_pic == EMBEDDED_PIC
252b5132
RH
8893 || g_switch_value < 4
8894 || (temp[0] == 0 && temp[1] == 0)
8895 || (temp[2] == 0 && temp[3] == 0))))
8896 {
8897 imm_expr.X_op = O_constant;
8898 if (! target_big_endian)
8899 imm_expr.X_add_number = bfd_getl32 (temp);
8900 else
8901 imm_expr.X_add_number = bfd_getb32 (temp);
8902 }
8903 else if (length > 4
119d663a 8904 && ! mips_disable_float_construction
ca4e0257
RS
8905 /* Constants can only be constructed in GPRs and
8906 copied to FPRs if the GPRs are at least as wide
8907 as the FPRs. Force the constant into memory if
8908 we are using 64-bit FPRs but the GPRs are only
8909 32 bits wide. */
8910 && (using_gprs
8911 || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
252b5132
RH
8912 && ((temp[0] == 0 && temp[1] == 0)
8913 || (temp[2] == 0 && temp[3] == 0))
8914 && ((temp[4] == 0 && temp[5] == 0)
8915 || (temp[6] == 0 && temp[7] == 0)))
8916 {
ca4e0257
RS
8917 /* The value is simple enough to load with a couple of
8918 instructions. If using 32-bit registers, set
8919 imm_expr to the high order 32 bits and offset_expr to
8920 the low order 32 bits. Otherwise, set imm_expr to
8921 the entire 64 bit constant. */
8922 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
252b5132
RH
8923 {
8924 imm_expr.X_op = O_constant;
8925 offset_expr.X_op = O_constant;
8926 if (! target_big_endian)
8927 {
8928 imm_expr.X_add_number = bfd_getl32 (temp + 4);
8929 offset_expr.X_add_number = bfd_getl32 (temp);
8930 }
8931 else
8932 {
8933 imm_expr.X_add_number = bfd_getb32 (temp);
8934 offset_expr.X_add_number = bfd_getb32 (temp + 4);
8935 }
8936 if (offset_expr.X_add_number == 0)
8937 offset_expr.X_op = O_absent;
8938 }
8939 else if (sizeof (imm_expr.X_add_number) > 4)
8940 {
8941 imm_expr.X_op = O_constant;
8942 if (! target_big_endian)
8943 imm_expr.X_add_number = bfd_getl64 (temp);
8944 else
8945 imm_expr.X_add_number = bfd_getb64 (temp);
8946 }
8947 else
8948 {
8949 imm_expr.X_op = O_big;
8950 imm_expr.X_add_number = 4;
8951 if (! target_big_endian)
8952 {
8953 generic_bignum[0] = bfd_getl16 (temp);
8954 generic_bignum[1] = bfd_getl16 (temp + 2);
8955 generic_bignum[2] = bfd_getl16 (temp + 4);
8956 generic_bignum[3] = bfd_getl16 (temp + 6);
8957 }
8958 else
8959 {
8960 generic_bignum[0] = bfd_getb16 (temp + 6);
8961 generic_bignum[1] = bfd_getb16 (temp + 4);
8962 generic_bignum[2] = bfd_getb16 (temp + 2);
8963 generic_bignum[3] = bfd_getb16 (temp);
8964 }
8965 }
8966 }
8967 else
8968 {
8969 const char *newname;
8970 segT new_seg;
8971
8972 /* Switch to the right section. */
8973 seg = now_seg;
8974 subseg = now_subseg;
8975 switch (*args)
8976 {
8977 default: /* unused default case avoids warnings. */
8978 case 'L':
8979 newname = RDATA_SECTION_NAME;
4d0d148d 8980 if ((g_switch_value >= 8)
bb2d6cd7 8981 || mips_pic == EMBEDDED_PIC)
252b5132
RH
8982 newname = ".lit8";
8983 break;
8984 case 'F':
bb2d6cd7
GK
8985 if (mips_pic == EMBEDDED_PIC)
8986 newname = ".lit8";
8987 else
8988 newname = RDATA_SECTION_NAME;
252b5132
RH
8989 break;
8990 case 'l':
4d0d148d 8991 assert (g_switch_value >= 4);
252b5132
RH
8992 newname = ".lit4";
8993 break;
8994 }
8995 new_seg = subseg_new (newname, (subsegT) 0);
8996 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
8997 bfd_set_section_flags (stdoutput, new_seg,
8998 (SEC_ALLOC
8999 | SEC_LOAD
9000 | SEC_READONLY
9001 | SEC_DATA));
9002 frag_align (*args == 'l' ? 2 : 3, 0, 0);
9003 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
9004 && strcmp (TARGET_OS, "elf") != 0)
9005 record_alignment (new_seg, 4);
9006 else
9007 record_alignment (new_seg, *args == 'l' ? 2 : 3);
9008 if (seg == now_seg)
9009 as_bad (_("Can't use floating point insn in this section"));
9010
9011 /* Set the argument to the current address in the
9012 section. */
9013 offset_expr.X_op = O_symbol;
9014 offset_expr.X_add_symbol =
9015 symbol_new ("L0\001", now_seg,
9016 (valueT) frag_now_fix (), frag_now);
9017 offset_expr.X_add_number = 0;
9018
9019 /* Put the floating point number into the section. */
9020 p = frag_more ((int) length);
9021 memcpy (p, temp, length);
9022
9023 /* Switch back to the original section. */
9024 subseg_set (seg, subseg);
9025 }
9026 }
9027 continue;
9028
9029 case 'i': /* 16 bit unsigned immediate */
9030 case 'j': /* 16 bit signed immediate */
f6688943 9031 *imm_reloc = BFD_RELOC_LO16;
5e0116d5 9032 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0)
252b5132
RH
9033 {
9034 int more;
5e0116d5
RS
9035 offsetT minval, maxval;
9036
9037 more = (insn + 1 < &mips_opcodes[NUMOPCODES]
9038 && strcmp (insn->name, insn[1].name) == 0);
9039
9040 /* If the expression was written as an unsigned number,
9041 only treat it as signed if there are no more
9042 alternatives. */
9043 if (more
9044 && *args == 'j'
9045 && sizeof (imm_expr.X_add_number) <= 4
9046 && imm_expr.X_op == O_constant
9047 && imm_expr.X_add_number < 0
9048 && imm_expr.X_unsigned
9049 && HAVE_64BIT_GPRS)
9050 break;
9051
9052 /* For compatibility with older assemblers, we accept
9053 0x8000-0xffff as signed 16-bit numbers when only
9054 signed numbers are allowed. */
9055 if (*args == 'i')
9056 minval = 0, maxval = 0xffff;
9057 else if (more)
9058 minval = -0x8000, maxval = 0x7fff;
252b5132 9059 else
5e0116d5
RS
9060 minval = -0x8000, maxval = 0xffff;
9061
9062 if (imm_expr.X_op != O_constant
9063 || imm_expr.X_add_number < minval
9064 || imm_expr.X_add_number > maxval)
252b5132
RH
9065 {
9066 if (more)
9067 break;
2ae7e77b
AH
9068 if (imm_expr.X_op == O_constant
9069 || imm_expr.X_op == O_big)
5e0116d5 9070 as_bad (_("expression out of range"));
252b5132
RH
9071 }
9072 }
9073 s = expr_end;
9074 continue;
9075
9076 case 'o': /* 16 bit offset */
5e0116d5
RS
9077 /* Check whether there is only a single bracketed expression
9078 left. If so, it must be the base register and the
9079 constant must be zero. */
9080 if (*s == '(' && strchr (s + 1, '(') == 0)
9081 {
9082 offset_expr.X_op = O_constant;
9083 offset_expr.X_add_number = 0;
9084 continue;
9085 }
252b5132
RH
9086
9087 /* If this value won't fit into a 16 bit offset, then go
9088 find a macro that will generate the 32 bit offset
afdbd6d0 9089 code pattern. */
5e0116d5 9090 if (my_getSmallExpression (&offset_expr, offset_reloc, s) == 0
252b5132
RH
9091 && (offset_expr.X_op != O_constant
9092 || offset_expr.X_add_number >= 0x8000
afdbd6d0 9093 || offset_expr.X_add_number < -0x8000))
252b5132
RH
9094 break;
9095
252b5132
RH
9096 s = expr_end;
9097 continue;
9098
9099 case 'p': /* pc relative offset */
0b25d3e6 9100 *offset_reloc = BFD_RELOC_16_PCREL_S2;
252b5132
RH
9101 my_getExpression (&offset_expr, s);
9102 s = expr_end;
9103 continue;
9104
9105 case 'u': /* upper 16 bits */
5e0116d5
RS
9106 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0
9107 && imm_expr.X_op == O_constant
9108 && (imm_expr.X_add_number < 0
9109 || imm_expr.X_add_number >= 0x10000))
252b5132
RH
9110 as_bad (_("lui expression not in range 0..65535"));
9111 s = expr_end;
9112 continue;
9113
9114 case 'a': /* 26 bit address */
9115 my_getExpression (&offset_expr, s);
9116 s = expr_end;
f6688943 9117 *offset_reloc = BFD_RELOC_MIPS_JMP;
252b5132
RH
9118 continue;
9119
9120 case 'N': /* 3 bit branch condition code */
9121 case 'M': /* 3 bit compare condition code */
9122 if (strncmp (s, "$fcc", 4) != 0)
9123 break;
9124 s += 4;
9125 regno = 0;
9126 do
9127 {
9128 regno *= 10;
9129 regno += *s - '0';
9130 ++s;
9131 }
3882b010 9132 while (ISDIGIT (*s));
252b5132 9133 if (regno > 7)
30c378fd
CD
9134 as_bad (_("Invalid condition code register $fcc%d"), regno);
9135 if ((strcmp(str + strlen(str) - 3, ".ps") == 0
9136 || strcmp(str + strlen(str) - 5, "any2f") == 0
9137 || strcmp(str + strlen(str) - 5, "any2t") == 0)
9138 && (regno & 1) != 0)
9139 as_warn(_("Condition code register should be even for %s, was %d"),
9140 str, regno);
9141 if ((strcmp(str + strlen(str) - 5, "any4f") == 0
9142 || strcmp(str + strlen(str) - 5, "any4t") == 0)
9143 && (regno & 3) != 0)
9144 as_warn(_("Condition code register should be 0 or 4 for %s, was %d"),
9145 str, regno);
252b5132
RH
9146 if (*args == 'N')
9147 ip->insn_opcode |= regno << OP_SH_BCC;
9148 else
9149 ip->insn_opcode |= regno << OP_SH_CCC;
beae10d5 9150 continue;
252b5132 9151
156c2f8b
NC
9152 case 'H':
9153 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
9154 s += 2;
3882b010 9155 if (ISDIGIT (*s))
156c2f8b
NC
9156 {
9157 c = 0;
9158 do
9159 {
9160 c *= 10;
9161 c += *s - '0';
9162 ++s;
9163 }
3882b010 9164 while (ISDIGIT (*s));
156c2f8b
NC
9165 }
9166 else
9167 c = 8; /* Invalid sel value. */
9168
9169 if (c > 7)
9170 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
9171 ip->insn_opcode |= c;
9172 continue;
9173
60b63b72
RS
9174 case 'e':
9175 /* Must be at least one digit. */
9176 my_getExpression (&imm_expr, s);
9177 check_absolute_expr (ip, &imm_expr);
9178
9179 if ((unsigned long) imm_expr.X_add_number
9180 > (unsigned long) OP_MASK_VECBYTE)
9181 {
9182 as_bad (_("bad byte vector index (%ld)"),
9183 (long) imm_expr.X_add_number);
9184 imm_expr.X_add_number = 0;
9185 }
9186
9187 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECBYTE;
9188 imm_expr.X_op = O_absent;
9189 s = expr_end;
9190 continue;
9191
9192 case '%':
9193 my_getExpression (&imm_expr, s);
9194 check_absolute_expr (ip, &imm_expr);
9195
9196 if ((unsigned long) imm_expr.X_add_number
9197 > (unsigned long) OP_MASK_VECALIGN)
9198 {
9199 as_bad (_("bad byte vector index (%ld)"),
9200 (long) imm_expr.X_add_number);
9201 imm_expr.X_add_number = 0;
9202 }
9203
9204 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECALIGN;
9205 imm_expr.X_op = O_absent;
9206 s = expr_end;
9207 continue;
9208
252b5132
RH
9209 default:
9210 as_bad (_("bad char = '%c'\n"), *args);
9211 internalError ();
9212 }
9213 break;
9214 }
9215 /* Args don't match. */
9216 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
9217 !strcmp (insn->name, insn[1].name))
9218 {
9219 ++insn;
9220 s = argsStart;
268f6bed 9221 insn_error = _("illegal operands");
252b5132
RH
9222 continue;
9223 }
268f6bed
L
9224 if (save_c)
9225 *(--s) = save_c;
252b5132
RH
9226 insn_error = _("illegal operands");
9227 return;
9228 }
9229}
9230
9231/* This routine assembles an instruction into its binary format when
9232 assembling for the mips16. As a side effect, it sets one of the
9233 global variables imm_reloc or offset_reloc to the type of
9234 relocation to do if one of the operands is an address expression.
9235 It also sets mips16_small and mips16_ext if the user explicitly
9236 requested a small or extended instruction. */
9237
9238static void
17a2f251 9239mips16_ip (char *str, struct mips_cl_insn *ip)
252b5132
RH
9240{
9241 char *s;
9242 const char *args;
9243 struct mips_opcode *insn;
9244 char *argsstart;
9245 unsigned int regno;
9246 unsigned int lastregno = 0;
9247 char *s_reset;
9248
9249 insn_error = NULL;
9250
b34976b6
AM
9251 mips16_small = FALSE;
9252 mips16_ext = FALSE;
252b5132 9253
3882b010 9254 for (s = str; ISLOWER (*s); ++s)
252b5132
RH
9255 ;
9256 switch (*s)
9257 {
9258 case '\0':
9259 break;
9260
9261 case ' ':
9262 *s++ = '\0';
9263 break;
9264
9265 case '.':
9266 if (s[1] == 't' && s[2] == ' ')
9267 {
9268 *s = '\0';
b34976b6 9269 mips16_small = TRUE;
252b5132
RH
9270 s += 3;
9271 break;
9272 }
9273 else if (s[1] == 'e' && s[2] == ' ')
9274 {
9275 *s = '\0';
b34976b6 9276 mips16_ext = TRUE;
252b5132
RH
9277 s += 3;
9278 break;
9279 }
9280 /* Fall through. */
9281 default:
9282 insn_error = _("unknown opcode");
9283 return;
9284 }
9285
9286 if (mips_opts.noautoextend && ! mips16_ext)
b34976b6 9287 mips16_small = TRUE;
252b5132
RH
9288
9289 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
9290 {
9291 insn_error = _("unrecognized opcode");
9292 return;
9293 }
9294
9295 argsstart = s;
9296 for (;;)
9297 {
9298 assert (strcmp (insn->name, str) == 0);
9299
9300 ip->insn_mo = insn;
9301 ip->insn_opcode = insn->match;
b34976b6 9302 ip->use_extend = FALSE;
252b5132 9303 imm_expr.X_op = O_absent;
f6688943
TS
9304 imm_reloc[0] = BFD_RELOC_UNUSED;
9305 imm_reloc[1] = BFD_RELOC_UNUSED;
9306 imm_reloc[2] = BFD_RELOC_UNUSED;
5f74bc13 9307 imm2_expr.X_op = O_absent;
252b5132 9308 offset_expr.X_op = O_absent;
f6688943
TS
9309 offset_reloc[0] = BFD_RELOC_UNUSED;
9310 offset_reloc[1] = BFD_RELOC_UNUSED;
9311 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
9312 for (args = insn->args; 1; ++args)
9313 {
9314 int c;
9315
9316 if (*s == ' ')
9317 ++s;
9318
9319 /* In this switch statement we call break if we did not find
9320 a match, continue if we did find a match, or return if we
9321 are done. */
9322
9323 c = *args;
9324 switch (c)
9325 {
9326 case '\0':
9327 if (*s == '\0')
9328 {
9329 /* Stuff the immediate value in now, if we can. */
9330 if (imm_expr.X_op == O_constant
f6688943 9331 && *imm_reloc > BFD_RELOC_UNUSED
252b5132
RH
9332 && insn->pinfo != INSN_MACRO)
9333 {
c4e7957c 9334 mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
b34976b6 9335 imm_expr.X_add_number, TRUE, mips16_small,
252b5132
RH
9336 mips16_ext, &ip->insn_opcode,
9337 &ip->use_extend, &ip->extend);
9338 imm_expr.X_op = O_absent;
f6688943 9339 *imm_reloc = BFD_RELOC_UNUSED;
252b5132
RH
9340 }
9341
9342 return;
9343 }
9344 break;
9345
9346 case ',':
9347 if (*s++ == c)
9348 continue;
9349 s--;
9350 switch (*++args)
9351 {
9352 case 'v':
9353 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9354 continue;
9355 case 'w':
9356 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9357 continue;
9358 }
9359 break;
9360
9361 case '(':
9362 case ')':
9363 if (*s++ == c)
9364 continue;
9365 break;
9366
9367 case 'v':
9368 case 'w':
9369 if (s[0] != '$')
9370 {
9371 if (c == 'v')
9372 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9373 else
9374 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9375 ++args;
9376 continue;
9377 }
9378 /* Fall through. */
9379 case 'x':
9380 case 'y':
9381 case 'z':
9382 case 'Z':
9383 case '0':
9384 case 'S':
9385 case 'R':
9386 case 'X':
9387 case 'Y':
9388 if (s[0] != '$')
9389 break;
9390 s_reset = s;
3882b010 9391 if (ISDIGIT (s[1]))
252b5132
RH
9392 {
9393 ++s;
9394 regno = 0;
9395 do
9396 {
9397 regno *= 10;
9398 regno += *s - '0';
9399 ++s;
9400 }
3882b010 9401 while (ISDIGIT (*s));
252b5132
RH
9402 if (regno > 31)
9403 {
9404 as_bad (_("invalid register number (%d)"), regno);
9405 regno = 2;
9406 }
9407 }
9408 else
9409 {
76db943d
TS
9410 if (s[1] == 'r' && s[2] == 'a')
9411 {
9412 s += 3;
9413 regno = RA;
9414 }
9415 else if (s[1] == 'f' && s[2] == 'p')
252b5132
RH
9416 {
9417 s += 3;
9418 regno = FP;
9419 }
9420 else if (s[1] == 's' && s[2] == 'p')
9421 {
9422 s += 3;
9423 regno = SP;
9424 }
9425 else if (s[1] == 'g' && s[2] == 'p')
9426 {
9427 s += 3;
9428 regno = GP;
9429 }
9430 else if (s[1] == 'a' && s[2] == 't')
9431 {
9432 s += 3;
9433 regno = AT;
9434 }
9435 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
9436 {
9437 s += 4;
9438 regno = KT0;
9439 }
9440 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
9441 {
9442 s += 4;
9443 regno = KT1;
9444 }
85b51719
TS
9445 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
9446 {
9447 s += 5;
9448 regno = ZERO;
9449 }
252b5132
RH
9450 else
9451 break;
9452 }
9453
9454 if (*s == ' ')
9455 ++s;
9456 if (args[1] != *s)
9457 {
9458 if (c == 'v' || c == 'w')
9459 {
9460 regno = mips16_to_32_reg_map[lastregno];
9461 s = s_reset;
f9419b05 9462 ++args;
252b5132
RH
9463 }
9464 }
9465
9466 switch (c)
9467 {
9468 case 'x':
9469 case 'y':
9470 case 'z':
9471 case 'v':
9472 case 'w':
9473 case 'Z':
9474 regno = mips32_to_16_reg_map[regno];
9475 break;
9476
9477 case '0':
9478 if (regno != 0)
9479 regno = ILLEGAL_REG;
9480 break;
9481
9482 case 'S':
9483 if (regno != SP)
9484 regno = ILLEGAL_REG;
9485 break;
9486
9487 case 'R':
9488 if (regno != RA)
9489 regno = ILLEGAL_REG;
9490 break;
9491
9492 case 'X':
9493 case 'Y':
9494 if (regno == AT && ! mips_opts.noat)
9495 as_warn (_("used $at without \".set noat\""));
9496 break;
9497
9498 default:
9499 internalError ();
9500 }
9501
9502 if (regno == ILLEGAL_REG)
9503 break;
9504
9505 switch (c)
9506 {
9507 case 'x':
9508 case 'v':
9509 ip->insn_opcode |= regno << MIPS16OP_SH_RX;
9510 break;
9511 case 'y':
9512 case 'w':
9513 ip->insn_opcode |= regno << MIPS16OP_SH_RY;
9514 break;
9515 case 'z':
9516 ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
9517 break;
9518 case 'Z':
9519 ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
9520 case '0':
9521 case 'S':
9522 case 'R':
9523 break;
9524 case 'X':
9525 ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
9526 break;
9527 case 'Y':
9528 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
9529 ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
9530 break;
9531 default:
9532 internalError ();
9533 }
9534
9535 lastregno = regno;
9536 continue;
9537
9538 case 'P':
9539 if (strncmp (s, "$pc", 3) == 0)
9540 {
9541 s += 3;
9542 continue;
9543 }
9544 break;
9545
9546 case '<':
9547 case '>':
9548 case '[':
9549 case ']':
9550 case '4':
9551 case '5':
9552 case 'H':
9553 case 'W':
9554 case 'D':
9555 case 'j':
9556 case '8':
9557 case 'V':
9558 case 'C':
9559 case 'U':
9560 case 'k':
9561 case 'K':
9562 if (s[0] == '%'
9563 && strncmp (s + 1, "gprel(", sizeof "gprel(" - 1) == 0)
9564 {
9565 /* This is %gprel(SYMBOL). We need to read SYMBOL,
9566 and generate the appropriate reloc. If the text
9567 inside %gprel is not a symbol name with an
9568 optional offset, then we generate a normal reloc
9569 and will probably fail later. */
9570 my_getExpression (&imm_expr, s + sizeof "%gprel" - 1);
9571 if (imm_expr.X_op == O_symbol)
9572 {
b34976b6 9573 mips16_ext = TRUE;
f6688943 9574 *imm_reloc = BFD_RELOC_MIPS16_GPREL;
252b5132 9575 s = expr_end;
b34976b6 9576 ip->use_extend = TRUE;
252b5132
RH
9577 ip->extend = 0;
9578 continue;
9579 }
9580 }
9581 else
9582 {
9583 /* Just pick up a normal expression. */
9584 my_getExpression (&imm_expr, s);
9585 }
9586
9587 if (imm_expr.X_op == O_register)
9588 {
9589 /* What we thought was an expression turned out to
9590 be a register. */
9591
9592 if (s[0] == '(' && args[1] == '(')
9593 {
9594 /* It looks like the expression was omitted
9595 before a register indirection, which means
9596 that the expression is implicitly zero. We
9597 still set up imm_expr, so that we handle
9598 explicit extensions correctly. */
9599 imm_expr.X_op = O_constant;
9600 imm_expr.X_add_number = 0;
f6688943 9601 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
9602 continue;
9603 }
9604
9605 break;
9606 }
9607
9608 /* We need to relax this instruction. */
f6688943 9609 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
9610 s = expr_end;
9611 continue;
9612
9613 case 'p':
9614 case 'q':
9615 case 'A':
9616 case 'B':
9617 case 'E':
9618 /* We use offset_reloc rather than imm_reloc for the PC
9619 relative operands. This lets macros with both
9620 immediate and address operands work correctly. */
9621 my_getExpression (&offset_expr, s);
9622
9623 if (offset_expr.X_op == O_register)
9624 break;
9625
9626 /* We need to relax this instruction. */
f6688943 9627 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
9628 s = expr_end;
9629 continue;
9630
9631 case '6': /* break code */
9632 my_getExpression (&imm_expr, s);
9633 check_absolute_expr (ip, &imm_expr);
9634 if ((unsigned long) imm_expr.X_add_number > 63)
9635 {
9636 as_warn (_("Invalid value for `%s' (%lu)"),
9637 ip->insn_mo->name,
9638 (unsigned long) imm_expr.X_add_number);
9639 imm_expr.X_add_number &= 0x3f;
9640 }
9641 ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
9642 imm_expr.X_op = O_absent;
9643 s = expr_end;
9644 continue;
9645
9646 case 'a': /* 26 bit address */
9647 my_getExpression (&offset_expr, s);
9648 s = expr_end;
f6688943 9649 *offset_reloc = BFD_RELOC_MIPS16_JMP;
252b5132
RH
9650 ip->insn_opcode <<= 16;
9651 continue;
9652
9653 case 'l': /* register list for entry macro */
9654 case 'L': /* register list for exit macro */
9655 {
9656 int mask;
9657
9658 if (c == 'l')
9659 mask = 0;
9660 else
9661 mask = 7 << 3;
9662 while (*s != '\0')
9663 {
9664 int freg, reg1, reg2;
9665
9666 while (*s == ' ' || *s == ',')
9667 ++s;
9668 if (*s != '$')
9669 {
9670 as_bad (_("can't parse register list"));
9671 break;
9672 }
9673 ++s;
9674 if (*s != 'f')
9675 freg = 0;
9676 else
9677 {
9678 freg = 1;
9679 ++s;
9680 }
9681 reg1 = 0;
3882b010 9682 while (ISDIGIT (*s))
252b5132
RH
9683 {
9684 reg1 *= 10;
9685 reg1 += *s - '0';
9686 ++s;
9687 }
9688 if (*s == ' ')
9689 ++s;
9690 if (*s != '-')
9691 reg2 = reg1;
9692 else
9693 {
9694 ++s;
9695 if (*s != '$')
9696 break;
9697 ++s;
9698 if (freg)
9699 {
9700 if (*s == 'f')
9701 ++s;
9702 else
9703 {
9704 as_bad (_("invalid register list"));
9705 break;
9706 }
9707 }
9708 reg2 = 0;
3882b010 9709 while (ISDIGIT (*s))
252b5132
RH
9710 {
9711 reg2 *= 10;
9712 reg2 += *s - '0';
9713 ++s;
9714 }
9715 }
9716 if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
9717 {
9718 mask &= ~ (7 << 3);
9719 mask |= 5 << 3;
9720 }
9721 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
9722 {
9723 mask &= ~ (7 << 3);
9724 mask |= 6 << 3;
9725 }
9726 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
9727 mask |= (reg2 - 3) << 3;
9728 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
9729 mask |= (reg2 - 15) << 1;
f9419b05 9730 else if (reg1 == RA && reg2 == RA)
252b5132
RH
9731 mask |= 1;
9732 else
9733 {
9734 as_bad (_("invalid register list"));
9735 break;
9736 }
9737 }
9738 /* The mask is filled in in the opcode table for the
9739 benefit of the disassembler. We remove it before
9740 applying the actual mask. */
9741 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
9742 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
9743 }
9744 continue;
9745
9746 case 'e': /* extend code */
9747 my_getExpression (&imm_expr, s);
9748 check_absolute_expr (ip, &imm_expr);
9749 if ((unsigned long) imm_expr.X_add_number > 0x7ff)
9750 {
9751 as_warn (_("Invalid value for `%s' (%lu)"),
9752 ip->insn_mo->name,
9753 (unsigned long) imm_expr.X_add_number);
9754 imm_expr.X_add_number &= 0x7ff;
9755 }
9756 ip->insn_opcode |= imm_expr.X_add_number;
9757 imm_expr.X_op = O_absent;
9758 s = expr_end;
9759 continue;
9760
9761 default:
9762 internalError ();
9763 }
9764 break;
9765 }
9766
9767 /* Args don't match. */
9768 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
9769 strcmp (insn->name, insn[1].name) == 0)
9770 {
9771 ++insn;
9772 s = argsstart;
9773 continue;
9774 }
9775
9776 insn_error = _("illegal operands");
9777
9778 return;
9779 }
9780}
9781
9782/* This structure holds information we know about a mips16 immediate
9783 argument type. */
9784
e972090a
NC
9785struct mips16_immed_operand
9786{
252b5132
RH
9787 /* The type code used in the argument string in the opcode table. */
9788 int type;
9789 /* The number of bits in the short form of the opcode. */
9790 int nbits;
9791 /* The number of bits in the extended form of the opcode. */
9792 int extbits;
9793 /* The amount by which the short form is shifted when it is used;
9794 for example, the sw instruction has a shift count of 2. */
9795 int shift;
9796 /* The amount by which the short form is shifted when it is stored
9797 into the instruction code. */
9798 int op_shift;
9799 /* Non-zero if the short form is unsigned. */
9800 int unsp;
9801 /* Non-zero if the extended form is unsigned. */
9802 int extu;
9803 /* Non-zero if the value is PC relative. */
9804 int pcrel;
9805};
9806
9807/* The mips16 immediate operand types. */
9808
9809static const struct mips16_immed_operand mips16_immed_operands[] =
9810{
9811 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9812 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9813 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9814 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9815 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
9816 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
9817 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
9818 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
9819 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
9820 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
9821 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
9822 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
9823 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
9824 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
9825 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
9826 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
9827 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9828 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9829 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
9830 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
9831 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
9832};
9833
9834#define MIPS16_NUM_IMMED \
9835 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
9836
9837/* Handle a mips16 instruction with an immediate value. This or's the
9838 small immediate value into *INSN. It sets *USE_EXTEND to indicate
9839 whether an extended value is needed; if one is needed, it sets
9840 *EXTEND to the value. The argument type is TYPE. The value is VAL.
9841 If SMALL is true, an unextended opcode was explicitly requested.
9842 If EXT is true, an extended opcode was explicitly requested. If
9843 WARN is true, warn if EXT does not match reality. */
9844
9845static void
17a2f251
TS
9846mips16_immed (char *file, unsigned int line, int type, offsetT val,
9847 bfd_boolean warn, bfd_boolean small, bfd_boolean ext,
9848 unsigned long *insn, bfd_boolean *use_extend,
9849 unsigned short *extend)
252b5132
RH
9850{
9851 register const struct mips16_immed_operand *op;
9852 int mintiny, maxtiny;
b34976b6 9853 bfd_boolean needext;
252b5132
RH
9854
9855 op = mips16_immed_operands;
9856 while (op->type != type)
9857 {
9858 ++op;
9859 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
9860 }
9861
9862 if (op->unsp)
9863 {
9864 if (type == '<' || type == '>' || type == '[' || type == ']')
9865 {
9866 mintiny = 1;
9867 maxtiny = 1 << op->nbits;
9868 }
9869 else
9870 {
9871 mintiny = 0;
9872 maxtiny = (1 << op->nbits) - 1;
9873 }
9874 }
9875 else
9876 {
9877 mintiny = - (1 << (op->nbits - 1));
9878 maxtiny = (1 << (op->nbits - 1)) - 1;
9879 }
9880
9881 /* Branch offsets have an implicit 0 in the lowest bit. */
9882 if (type == 'p' || type == 'q')
9883 val /= 2;
9884
9885 if ((val & ((1 << op->shift) - 1)) != 0
9886 || val < (mintiny << op->shift)
9887 || val > (maxtiny << op->shift))
b34976b6 9888 needext = TRUE;
252b5132 9889 else
b34976b6 9890 needext = FALSE;
252b5132
RH
9891
9892 if (warn && ext && ! needext)
beae10d5
KH
9893 as_warn_where (file, line,
9894 _("extended operand requested but not required"));
252b5132
RH
9895 if (small && needext)
9896 as_bad_where (file, line, _("invalid unextended operand value"));
9897
9898 if (small || (! ext && ! needext))
9899 {
9900 int insnval;
9901
b34976b6 9902 *use_extend = FALSE;
252b5132
RH
9903 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
9904 insnval <<= op->op_shift;
9905 *insn |= insnval;
9906 }
9907 else
9908 {
9909 long minext, maxext;
9910 int extval;
9911
9912 if (op->extu)
9913 {
9914 minext = 0;
9915 maxext = (1 << op->extbits) - 1;
9916 }
9917 else
9918 {
9919 minext = - (1 << (op->extbits - 1));
9920 maxext = (1 << (op->extbits - 1)) - 1;
9921 }
9922 if (val < minext || val > maxext)
9923 as_bad_where (file, line,
9924 _("operand value out of range for instruction"));
9925
b34976b6 9926 *use_extend = TRUE;
252b5132
RH
9927 if (op->extbits == 16)
9928 {
9929 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
9930 val &= 0x1f;
9931 }
9932 else if (op->extbits == 15)
9933 {
9934 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
9935 val &= 0xf;
9936 }
9937 else
9938 {
9939 extval = ((val & 0x1f) << 6) | (val & 0x20);
9940 val = 0;
9941 }
9942
9943 *extend = (unsigned short) extval;
9944 *insn |= val;
9945 }
9946}
9947\f
5e0116d5 9948static const struct percent_op_match
ad8d3bb3 9949{
5e0116d5
RS
9950 const char *str;
9951 bfd_reloc_code_real_type reloc;
ad8d3bb3
TS
9952} percent_op[] =
9953{
5e0116d5 9954 {"%lo", BFD_RELOC_LO16},
ad8d3bb3 9955#ifdef OBJ_ELF
5e0116d5
RS
9956 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
9957 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
9958 {"%call16", BFD_RELOC_MIPS_CALL16},
9959 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
9960 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
9961 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
9962 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
9963 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
9964 {"%got", BFD_RELOC_MIPS_GOT16},
9965 {"%gp_rel", BFD_RELOC_GPREL16},
9966 {"%half", BFD_RELOC_16},
9967 {"%highest", BFD_RELOC_MIPS_HIGHEST},
9968 {"%higher", BFD_RELOC_MIPS_HIGHER},
9969 {"%neg", BFD_RELOC_MIPS_SUB},
ad8d3bb3 9970#endif
5e0116d5 9971 {"%hi", BFD_RELOC_HI16_S}
ad8d3bb3
TS
9972};
9973
252b5132 9974
5e0116d5
RS
9975/* Return true if *STR points to a relocation operator. When returning true,
9976 move *STR over the operator and store its relocation code in *RELOC.
9977 Leave both *STR and *RELOC alone when returning false. */
9978
9979static bfd_boolean
17a2f251 9980parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
252b5132 9981{
5e0116d5 9982 size_t i;
76b3015f 9983
5e0116d5
RS
9984 for (i = 0; i < ARRAY_SIZE (percent_op); i++)
9985 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
394f9b3a 9986 {
5e0116d5
RS
9987 *str += strlen (percent_op[i].str);
9988 *reloc = percent_op[i].reloc;
394f9b3a 9989
5e0116d5
RS
9990 /* Check whether the output BFD supports this relocation.
9991 If not, issue an error and fall back on something safe. */
9992 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
394f9b3a 9993 {
5e0116d5
RS
9994 as_bad ("relocation %s isn't supported by the current ABI",
9995 percent_op[i].str);
9996 *reloc = BFD_RELOC_LO16;
394f9b3a 9997 }
5e0116d5 9998 return TRUE;
394f9b3a 9999 }
5e0116d5 10000 return FALSE;
394f9b3a 10001}
ad8d3bb3 10002
ad8d3bb3 10003
5e0116d5
RS
10004/* Parse string STR as a 16-bit relocatable operand. Store the
10005 expression in *EP and the relocations in the array starting
10006 at RELOC. Return the number of relocation operators used.
ad8d3bb3 10007
5e0116d5
RS
10008 On exit, EXPR_END points to the first character after the expression.
10009 If no relocation operators are used, RELOC[0] is set to BFD_RELOC_LO16. */
ad8d3bb3 10010
5e0116d5 10011static size_t
17a2f251
TS
10012my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
10013 char *str)
ad8d3bb3 10014{
5e0116d5
RS
10015 bfd_reloc_code_real_type reversed_reloc[3];
10016 size_t reloc_index, i;
09b8f35a
RS
10017 int crux_depth, str_depth;
10018 char *crux;
5e0116d5
RS
10019
10020 /* Search for the start of the main expression, recoding relocations
09b8f35a
RS
10021 in REVERSED_RELOC. End the loop with CRUX pointing to the start
10022 of the main expression and with CRUX_DEPTH containing the number
10023 of open brackets at that point. */
10024 reloc_index = -1;
10025 str_depth = 0;
10026 do
fb1b3232 10027 {
09b8f35a
RS
10028 reloc_index++;
10029 crux = str;
10030 crux_depth = str_depth;
10031
10032 /* Skip over whitespace and brackets, keeping count of the number
10033 of brackets. */
10034 while (*str == ' ' || *str == '\t' || *str == '(')
10035 if (*str++ == '(')
10036 str_depth++;
5e0116d5 10037 }
09b8f35a
RS
10038 while (*str == '%'
10039 && reloc_index < (HAVE_NEWABI ? 3 : 1)
10040 && parse_relocation (&str, &reversed_reloc[reloc_index]));
ad8d3bb3 10041
09b8f35a 10042 my_getExpression (ep, crux);
5e0116d5 10043 str = expr_end;
394f9b3a 10044
5e0116d5 10045 /* Match every open bracket. */
09b8f35a 10046 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
5e0116d5 10047 if (*str++ == ')')
09b8f35a 10048 crux_depth--;
394f9b3a 10049
09b8f35a 10050 if (crux_depth > 0)
5e0116d5 10051 as_bad ("unclosed '('");
394f9b3a 10052
5e0116d5 10053 expr_end = str;
252b5132 10054
64bdfcaf
RS
10055 if (reloc_index == 0)
10056 reloc[0] = BFD_RELOC_LO16;
10057 else
10058 {
10059 prev_reloc_op_frag = frag_now;
10060 for (i = 0; i < reloc_index; i++)
10061 reloc[i] = reversed_reloc[reloc_index - 1 - i];
10062 }
fb1b3232 10063
5e0116d5 10064 return reloc_index;
252b5132
RH
10065}
10066
10067static void
17a2f251 10068my_getExpression (expressionS *ep, char *str)
252b5132
RH
10069{
10070 char *save_in;
98aa84af 10071 valueT val;
252b5132
RH
10072
10073 save_in = input_line_pointer;
10074 input_line_pointer = str;
10075 expression (ep);
10076 expr_end = input_line_pointer;
10077 input_line_pointer = save_in;
10078
10079 /* If we are in mips16 mode, and this is an expression based on `.',
10080 then we bump the value of the symbol by 1 since that is how other
10081 text symbols are handled. We don't bother to handle complex
10082 expressions, just `.' plus or minus a constant. */
10083 if (mips_opts.mips16
10084 && ep->X_op == O_symbol
10085 && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
10086 && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
49309057
ILT
10087 && symbol_get_frag (ep->X_add_symbol) == frag_now
10088 && symbol_constant_p (ep->X_add_symbol)
98aa84af
AM
10089 && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
10090 S_SET_VALUE (ep->X_add_symbol, val + 1);
252b5132
RH
10091}
10092
10093/* Turn a string in input_line_pointer into a floating point constant
bc0d738a
NC
10094 of type TYPE, and store the appropriate bytes in *LITP. The number
10095 of LITTLENUMS emitted is stored in *SIZEP. An error message is
252b5132
RH
10096 returned, or NULL on OK. */
10097
10098char *
17a2f251 10099md_atof (int type, char *litP, int *sizeP)
252b5132
RH
10100{
10101 int prec;
10102 LITTLENUM_TYPE words[4];
10103 char *t;
10104 int i;
10105
10106 switch (type)
10107 {
10108 case 'f':
10109 prec = 2;
10110 break;
10111
10112 case 'd':
10113 prec = 4;
10114 break;
10115
10116 default:
10117 *sizeP = 0;
10118 return _("bad call to md_atof");
10119 }
10120
10121 t = atof_ieee (input_line_pointer, type, words);
10122 if (t)
10123 input_line_pointer = t;
10124
10125 *sizeP = prec * 2;
10126
10127 if (! target_big_endian)
10128 {
10129 for (i = prec - 1; i >= 0; i--)
10130 {
17a2f251 10131 md_number_to_chars (litP, words[i], 2);
252b5132
RH
10132 litP += 2;
10133 }
10134 }
10135 else
10136 {
10137 for (i = 0; i < prec; i++)
10138 {
17a2f251 10139 md_number_to_chars (litP, words[i], 2);
252b5132
RH
10140 litP += 2;
10141 }
10142 }
bdaaa2e1 10143
252b5132
RH
10144 return NULL;
10145}
10146
10147void
17a2f251 10148md_number_to_chars (char *buf, valueT val, int n)
252b5132
RH
10149{
10150 if (target_big_endian)
10151 number_to_chars_bigendian (buf, val, n);
10152 else
10153 number_to_chars_littleendian (buf, val, n);
10154}
10155\f
ae948b86 10156#ifdef OBJ_ELF
e013f690
TS
10157static int support_64bit_objects(void)
10158{
10159 const char **list, **l;
aa3d8fdf 10160 int yes;
e013f690
TS
10161
10162 list = bfd_target_list ();
10163 for (l = list; *l != NULL; l++)
10164#ifdef TE_TMIPS
10165 /* This is traditional mips */
10166 if (strcmp (*l, "elf64-tradbigmips") == 0
10167 || strcmp (*l, "elf64-tradlittlemips") == 0)
10168#else
10169 if (strcmp (*l, "elf64-bigmips") == 0
10170 || strcmp (*l, "elf64-littlemips") == 0)
10171#endif
10172 break;
aa3d8fdf 10173 yes = (*l != NULL);
e013f690 10174 free (list);
aa3d8fdf 10175 return yes;
e013f690 10176}
ae948b86 10177#endif /* OBJ_ELF */
e013f690 10178
78849248 10179const char *md_shortopts = "O::g::G:";
252b5132 10180
e972090a
NC
10181struct option md_longopts[] =
10182{
f9b4148d
CD
10183 /* Options which specify architecture. */
10184#define OPTION_ARCH_BASE (OPTION_MD_BASE)
10185#define OPTION_MARCH (OPTION_ARCH_BASE + 0)
10186 {"march", required_argument, NULL, OPTION_MARCH},
10187#define OPTION_MTUNE (OPTION_ARCH_BASE + 1)
10188 {"mtune", required_argument, NULL, OPTION_MTUNE},
10189#define OPTION_MIPS1 (OPTION_ARCH_BASE + 2)
252b5132
RH
10190 {"mips0", no_argument, NULL, OPTION_MIPS1},
10191 {"mips1", no_argument, NULL, OPTION_MIPS1},
f9b4148d 10192#define OPTION_MIPS2 (OPTION_ARCH_BASE + 3)
252b5132 10193 {"mips2", no_argument, NULL, OPTION_MIPS2},
f9b4148d 10194#define OPTION_MIPS3 (OPTION_ARCH_BASE + 4)
252b5132 10195 {"mips3", no_argument, NULL, OPTION_MIPS3},
f9b4148d 10196#define OPTION_MIPS4 (OPTION_ARCH_BASE + 5)
252b5132 10197 {"mips4", no_argument, NULL, OPTION_MIPS4},
f9b4148d 10198#define OPTION_MIPS5 (OPTION_ARCH_BASE + 6)
ae948b86 10199 {"mips5", no_argument, NULL, OPTION_MIPS5},
f9b4148d 10200#define OPTION_MIPS32 (OPTION_ARCH_BASE + 7)
ae948b86 10201 {"mips32", no_argument, NULL, OPTION_MIPS32},
f9b4148d 10202#define OPTION_MIPS64 (OPTION_ARCH_BASE + 8)
ae948b86 10203 {"mips64", no_argument, NULL, OPTION_MIPS64},
f9b4148d
CD
10204#define OPTION_MIPS32R2 (OPTION_ARCH_BASE + 9)
10205 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
5f74bc13
CD
10206#define OPTION_MIPS64R2 (OPTION_ARCH_BASE + 10)
10207 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
f9b4148d
CD
10208
10209 /* Options which specify Application Specific Extensions (ASEs). */
5f74bc13 10210#define OPTION_ASE_BASE (OPTION_ARCH_BASE + 11)
f9b4148d
CD
10211#define OPTION_MIPS16 (OPTION_ASE_BASE + 0)
10212 {"mips16", no_argument, NULL, OPTION_MIPS16},
10213#define OPTION_NO_MIPS16 (OPTION_ASE_BASE + 1)
10214 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
10215#define OPTION_MIPS3D (OPTION_ASE_BASE + 2)
10216 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
10217#define OPTION_NO_MIPS3D (OPTION_ASE_BASE + 3)
10218 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
10219#define OPTION_MDMX (OPTION_ASE_BASE + 4)
10220 {"mdmx", no_argument, NULL, OPTION_MDMX},
10221#define OPTION_NO_MDMX (OPTION_ASE_BASE + 5)
10222 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
10223
10224 /* Old-style architecture options. Don't add more of these. */
10225#define OPTION_COMPAT_ARCH_BASE (OPTION_ASE_BASE + 6)
10226#define OPTION_M4650 (OPTION_COMPAT_ARCH_BASE + 0)
10227 {"m4650", no_argument, NULL, OPTION_M4650},
10228#define OPTION_NO_M4650 (OPTION_COMPAT_ARCH_BASE + 1)
10229 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
10230#define OPTION_M4010 (OPTION_COMPAT_ARCH_BASE + 2)
10231 {"m4010", no_argument, NULL, OPTION_M4010},
10232#define OPTION_NO_M4010 (OPTION_COMPAT_ARCH_BASE + 3)
10233 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
10234#define OPTION_M4100 (OPTION_COMPAT_ARCH_BASE + 4)
10235 {"m4100", no_argument, NULL, OPTION_M4100},
10236#define OPTION_NO_M4100 (OPTION_COMPAT_ARCH_BASE + 5)
10237 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
10238#define OPTION_M3900 (OPTION_COMPAT_ARCH_BASE + 6)
10239 {"m3900", no_argument, NULL, OPTION_M3900},
10240#define OPTION_NO_M3900 (OPTION_COMPAT_ARCH_BASE + 7)
10241 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
10242
10243 /* Options which enable bug fixes. */
10244#define OPTION_FIX_BASE (OPTION_COMPAT_ARCH_BASE + 8)
10245#define OPTION_M7000_HILO_FIX (OPTION_FIX_BASE + 0)
10246 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
10247#define OPTION_MNO_7000_HILO_FIX (OPTION_FIX_BASE + 1)
10248 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10249 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
d766e8ec
RS
10250#define OPTION_FIX_VR4120 (OPTION_FIX_BASE + 2)
10251#define OPTION_NO_FIX_VR4120 (OPTION_FIX_BASE + 3)
10252 {"mfix-vr4120", no_argument, NULL, OPTION_FIX_VR4120},
10253 {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
f9b4148d
CD
10254
10255 /* Miscellaneous options. */
10256#define OPTION_MISC_BASE (OPTION_FIX_BASE + 4)
1ffcab4b 10257#define OPTION_TRAP (OPTION_MISC_BASE + 0)
252b5132
RH
10258 {"trap", no_argument, NULL, OPTION_TRAP},
10259 {"no-break", no_argument, NULL, OPTION_TRAP},
1ffcab4b 10260#define OPTION_BREAK (OPTION_MISC_BASE + 1)
252b5132
RH
10261 {"break", no_argument, NULL, OPTION_BREAK},
10262 {"no-trap", no_argument, NULL, OPTION_BREAK},
1ffcab4b 10263#define OPTION_EB (OPTION_MISC_BASE + 2)
252b5132 10264 {"EB", no_argument, NULL, OPTION_EB},
1ffcab4b 10265#define OPTION_EL (OPTION_MISC_BASE + 3)
252b5132 10266 {"EL", no_argument, NULL, OPTION_EL},
1ffcab4b 10267#define OPTION_FP32 (OPTION_MISC_BASE + 4)
ae948b86 10268 {"mfp32", no_argument, NULL, OPTION_FP32},
1ffcab4b 10269#define OPTION_GP32 (OPTION_MISC_BASE + 5)
c97ef257 10270 {"mgp32", no_argument, NULL, OPTION_GP32},
1ffcab4b 10271#define OPTION_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 6)
119d663a 10272 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
1ffcab4b 10273#define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MISC_BASE + 7)
119d663a 10274 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
1ffcab4b 10275#define OPTION_FP64 (OPTION_MISC_BASE + 8)
316f5878 10276 {"mfp64", no_argument, NULL, OPTION_FP64},
1ffcab4b 10277#define OPTION_GP64 (OPTION_MISC_BASE + 9)
ae948b86 10278 {"mgp64", no_argument, NULL, OPTION_GP64},
1ffcab4b
CD
10279#define OPTION_RELAX_BRANCH (OPTION_MISC_BASE + 10)
10280#define OPTION_NO_RELAX_BRANCH (OPTION_MISC_BASE + 11)
4a6a3df4
AO
10281 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
10282 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
f9b4148d
CD
10283
10284 /* ELF-specific options. */
156c2f8b 10285#ifdef OBJ_ELF
1ffcab4b 10286#define OPTION_ELF_BASE (OPTION_MISC_BASE + 12)
156c2f8b 10287#define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
156c2f8b
NC
10288 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
10289 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
ae948b86 10290#define OPTION_NON_SHARED (OPTION_ELF_BASE + 1)
156c2f8b 10291 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
ae948b86 10292#define OPTION_XGOT (OPTION_ELF_BASE + 2)
156c2f8b 10293 {"xgot", no_argument, NULL, OPTION_XGOT},
ae948b86
TS
10294#define OPTION_MABI (OPTION_ELF_BASE + 3)
10295 {"mabi", required_argument, NULL, OPTION_MABI},
10296#define OPTION_32 (OPTION_ELF_BASE + 4)
156c2f8b 10297 {"32", no_argument, NULL, OPTION_32},
ae948b86 10298#define OPTION_N32 (OPTION_ELF_BASE + 5)
e013f690 10299 {"n32", no_argument, NULL, OPTION_N32},
ae948b86 10300#define OPTION_64 (OPTION_ELF_BASE + 6)
156c2f8b 10301 {"64", no_argument, NULL, OPTION_64},
ecb4347a
DJ
10302#define OPTION_MDEBUG (OPTION_ELF_BASE + 7)
10303 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
10304#define OPTION_NO_MDEBUG (OPTION_ELF_BASE + 8)
10305 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
dcd410fe
RO
10306#define OPTION_PDR (OPTION_ELF_BASE + 9)
10307 {"mpdr", no_argument, NULL, OPTION_PDR},
10308#define OPTION_NO_PDR (OPTION_ELF_BASE + 10)
10309 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
ae948b86 10310#endif /* OBJ_ELF */
f9b4148d 10311
252b5132
RH
10312 {NULL, no_argument, NULL, 0}
10313};
156c2f8b 10314size_t md_longopts_size = sizeof (md_longopts);
252b5132 10315
316f5878
RS
10316/* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
10317 NEW_VALUE. Warn if another value was already specified. Note:
10318 we have to defer parsing the -march and -mtune arguments in order
10319 to handle 'from-abi' correctly, since the ABI might be specified
10320 in a later argument. */
10321
10322static void
17a2f251 10323mips_set_option_string (const char **string_ptr, const char *new_value)
316f5878
RS
10324{
10325 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
10326 as_warn (_("A different %s was already specified, is now %s"),
10327 string_ptr == &mips_arch_string ? "-march" : "-mtune",
10328 new_value);
10329
10330 *string_ptr = new_value;
10331}
10332
252b5132 10333int
17a2f251 10334md_parse_option (int c, char *arg)
252b5132
RH
10335{
10336 switch (c)
10337 {
119d663a
NC
10338 case OPTION_CONSTRUCT_FLOATS:
10339 mips_disable_float_construction = 0;
10340 break;
bdaaa2e1 10341
119d663a
NC
10342 case OPTION_NO_CONSTRUCT_FLOATS:
10343 mips_disable_float_construction = 1;
10344 break;
bdaaa2e1 10345
252b5132
RH
10346 case OPTION_TRAP:
10347 mips_trap = 1;
10348 break;
10349
10350 case OPTION_BREAK:
10351 mips_trap = 0;
10352 break;
10353
10354 case OPTION_EB:
10355 target_big_endian = 1;
10356 break;
10357
10358 case OPTION_EL:
10359 target_big_endian = 0;
10360 break;
10361
10362 case 'O':
10363 if (arg && arg[1] == '0')
10364 mips_optimize = 1;
10365 else
10366 mips_optimize = 2;
10367 break;
10368
10369 case 'g':
10370 if (arg == NULL)
10371 mips_debug = 2;
10372 else
10373 mips_debug = atoi (arg);
10374 /* When the MIPS assembler sees -g or -g2, it does not do
10375 optimizations which limit full symbolic debugging. We take
10376 that to be equivalent to -O0. */
10377 if (mips_debug == 2)
10378 mips_optimize = 1;
10379 break;
10380
10381 case OPTION_MIPS1:
316f5878 10382 file_mips_isa = ISA_MIPS1;
252b5132
RH
10383 break;
10384
10385 case OPTION_MIPS2:
316f5878 10386 file_mips_isa = ISA_MIPS2;
252b5132
RH
10387 break;
10388
10389 case OPTION_MIPS3:
316f5878 10390 file_mips_isa = ISA_MIPS3;
252b5132
RH
10391 break;
10392
10393 case OPTION_MIPS4:
316f5878 10394 file_mips_isa = ISA_MIPS4;
e7af610e
NC
10395 break;
10396
84ea6cf2 10397 case OPTION_MIPS5:
316f5878 10398 file_mips_isa = ISA_MIPS5;
84ea6cf2
NC
10399 break;
10400
e7af610e 10401 case OPTION_MIPS32:
316f5878 10402 file_mips_isa = ISA_MIPS32;
252b5132
RH
10403 break;
10404
af7ee8bf
CD
10405 case OPTION_MIPS32R2:
10406 file_mips_isa = ISA_MIPS32R2;
10407 break;
10408
5f74bc13
CD
10409 case OPTION_MIPS64R2:
10410 file_mips_isa = ISA_MIPS64R2;
10411 break;
10412
84ea6cf2 10413 case OPTION_MIPS64:
316f5878 10414 file_mips_isa = ISA_MIPS64;
84ea6cf2
NC
10415 break;
10416
ec68c924 10417 case OPTION_MTUNE:
316f5878
RS
10418 mips_set_option_string (&mips_tune_string, arg);
10419 break;
ec68c924 10420
316f5878
RS
10421 case OPTION_MARCH:
10422 mips_set_option_string (&mips_arch_string, arg);
252b5132
RH
10423 break;
10424
10425 case OPTION_M4650:
316f5878
RS
10426 mips_set_option_string (&mips_arch_string, "4650");
10427 mips_set_option_string (&mips_tune_string, "4650");
252b5132
RH
10428 break;
10429
10430 case OPTION_NO_M4650:
10431 break;
10432
10433 case OPTION_M4010:
316f5878
RS
10434 mips_set_option_string (&mips_arch_string, "4010");
10435 mips_set_option_string (&mips_tune_string, "4010");
252b5132
RH
10436 break;
10437
10438 case OPTION_NO_M4010:
10439 break;
10440
10441 case OPTION_M4100:
316f5878
RS
10442 mips_set_option_string (&mips_arch_string, "4100");
10443 mips_set_option_string (&mips_tune_string, "4100");
252b5132
RH
10444 break;
10445
10446 case OPTION_NO_M4100:
10447 break;
10448
252b5132 10449 case OPTION_M3900:
316f5878
RS
10450 mips_set_option_string (&mips_arch_string, "3900");
10451 mips_set_option_string (&mips_tune_string, "3900");
252b5132 10452 break;
bdaaa2e1 10453
252b5132
RH
10454 case OPTION_NO_M3900:
10455 break;
10456
deec1734
CD
10457 case OPTION_MDMX:
10458 mips_opts.ase_mdmx = 1;
10459 break;
10460
10461 case OPTION_NO_MDMX:
10462 mips_opts.ase_mdmx = 0;
10463 break;
10464
252b5132
RH
10465 case OPTION_MIPS16:
10466 mips_opts.mips16 = 1;
b34976b6 10467 mips_no_prev_insn (FALSE);
252b5132
RH
10468 break;
10469
10470 case OPTION_NO_MIPS16:
10471 mips_opts.mips16 = 0;
b34976b6 10472 mips_no_prev_insn (FALSE);
252b5132
RH
10473 break;
10474
1f25f5d3
CD
10475 case OPTION_MIPS3D:
10476 mips_opts.ase_mips3d = 1;
10477 break;
10478
10479 case OPTION_NO_MIPS3D:
10480 mips_opts.ase_mips3d = 0;
10481 break;
10482
d766e8ec
RS
10483 case OPTION_FIX_VR4120:
10484 mips_fix_vr4120 = 1;
60b63b72
RS
10485 break;
10486
d766e8ec
RS
10487 case OPTION_NO_FIX_VR4120:
10488 mips_fix_vr4120 = 0;
60b63b72
RS
10489 break;
10490
4a6a3df4
AO
10491 case OPTION_RELAX_BRANCH:
10492 mips_relax_branch = 1;
10493 break;
10494
10495 case OPTION_NO_RELAX_BRANCH:
10496 mips_relax_branch = 0;
10497 break;
10498
0f074f60 10499#ifdef OBJ_ELF
252b5132
RH
10500 /* When generating ELF code, we permit -KPIC and -call_shared to
10501 select SVR4_PIC, and -non_shared to select no PIC. This is
10502 intended to be compatible with Irix 5. */
10503 case OPTION_CALL_SHARED:
10504 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10505 {
10506 as_bad (_("-call_shared is supported only for ELF format"));
10507 return 0;
10508 }
10509 mips_pic = SVR4_PIC;
143d77c5 10510 mips_abicalls = TRUE;
252b5132
RH
10511 if (g_switch_seen && g_switch_value != 0)
10512 {
10513 as_bad (_("-G may not be used with SVR4 PIC code"));
10514 return 0;
10515 }
10516 g_switch_value = 0;
10517 break;
10518
10519 case OPTION_NON_SHARED:
10520 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10521 {
10522 as_bad (_("-non_shared is supported only for ELF format"));
10523 return 0;
10524 }
10525 mips_pic = NO_PIC;
143d77c5 10526 mips_abicalls = FALSE;
252b5132
RH
10527 break;
10528
10529 /* The -xgot option tells the assembler to use 32 offsets when
10530 accessing the got in SVR4_PIC mode. It is for Irix
10531 compatibility. */
10532 case OPTION_XGOT:
10533 mips_big_got = 1;
10534 break;
0f074f60 10535#endif /* OBJ_ELF */
252b5132
RH
10536
10537 case 'G':
4d0d148d 10538 if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
252b5132
RH
10539 {
10540 as_bad (_("-G may not be used with SVR4 or embedded PIC code"));
10541 return 0;
10542 }
10543 else
10544 g_switch_value = atoi (arg);
10545 g_switch_seen = 1;
10546 break;
10547
0f074f60 10548#ifdef OBJ_ELF
34ba82a8
TS
10549 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
10550 and -mabi=64. */
252b5132 10551 case OPTION_32:
34ba82a8
TS
10552 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10553 {
10554 as_bad (_("-32 is supported for ELF format only"));
10555 return 0;
10556 }
316f5878 10557 mips_abi = O32_ABI;
252b5132
RH
10558 break;
10559
e013f690 10560 case OPTION_N32:
34ba82a8
TS
10561 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10562 {
10563 as_bad (_("-n32 is supported for ELF format only"));
10564 return 0;
10565 }
316f5878 10566 mips_abi = N32_ABI;
e013f690 10567 break;
252b5132 10568
e013f690 10569 case OPTION_64:
34ba82a8
TS
10570 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10571 {
10572 as_bad (_("-64 is supported for ELF format only"));
10573 return 0;
10574 }
316f5878 10575 mips_abi = N64_ABI;
e013f690
TS
10576 if (! support_64bit_objects())
10577 as_fatal (_("No compiled in support for 64 bit object file format"));
252b5132 10578 break;
ae948b86 10579#endif /* OBJ_ELF */
252b5132 10580
c97ef257 10581 case OPTION_GP32:
a325df1d 10582 file_mips_gp32 = 1;
c97ef257
AH
10583 break;
10584
10585 case OPTION_GP64:
a325df1d 10586 file_mips_gp32 = 0;
c97ef257 10587 break;
252b5132 10588
ca4e0257 10589 case OPTION_FP32:
a325df1d 10590 file_mips_fp32 = 1;
316f5878
RS
10591 break;
10592
10593 case OPTION_FP64:
10594 file_mips_fp32 = 0;
ca4e0257
RS
10595 break;
10596
ae948b86 10597#ifdef OBJ_ELF
252b5132 10598 case OPTION_MABI:
34ba82a8
TS
10599 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10600 {
10601 as_bad (_("-mabi is supported for ELF format only"));
10602 return 0;
10603 }
e013f690 10604 if (strcmp (arg, "32") == 0)
316f5878 10605 mips_abi = O32_ABI;
e013f690 10606 else if (strcmp (arg, "o64") == 0)
316f5878 10607 mips_abi = O64_ABI;
e013f690 10608 else if (strcmp (arg, "n32") == 0)
316f5878 10609 mips_abi = N32_ABI;
e013f690
TS
10610 else if (strcmp (arg, "64") == 0)
10611 {
316f5878 10612 mips_abi = N64_ABI;
e013f690
TS
10613 if (! support_64bit_objects())
10614 as_fatal (_("No compiled in support for 64 bit object file "
10615 "format"));
10616 }
10617 else if (strcmp (arg, "eabi") == 0)
316f5878 10618 mips_abi = EABI_ABI;
e013f690 10619 else
da0e507f
TS
10620 {
10621 as_fatal (_("invalid abi -mabi=%s"), arg);
10622 return 0;
10623 }
252b5132 10624 break;
e013f690 10625#endif /* OBJ_ELF */
252b5132 10626
6b76fefe 10627 case OPTION_M7000_HILO_FIX:
b34976b6 10628 mips_7000_hilo_fix = TRUE;
6b76fefe
CM
10629 break;
10630
9ee72ff1 10631 case OPTION_MNO_7000_HILO_FIX:
b34976b6 10632 mips_7000_hilo_fix = FALSE;
6b76fefe
CM
10633 break;
10634
ecb4347a
DJ
10635#ifdef OBJ_ELF
10636 case OPTION_MDEBUG:
b34976b6 10637 mips_flag_mdebug = TRUE;
ecb4347a
DJ
10638 break;
10639
10640 case OPTION_NO_MDEBUG:
b34976b6 10641 mips_flag_mdebug = FALSE;
ecb4347a 10642 break;
dcd410fe
RO
10643
10644 case OPTION_PDR:
10645 mips_flag_pdr = TRUE;
10646 break;
10647
10648 case OPTION_NO_PDR:
10649 mips_flag_pdr = FALSE;
10650 break;
ecb4347a
DJ
10651#endif /* OBJ_ELF */
10652
252b5132
RH
10653 default:
10654 return 0;
10655 }
10656
10657 return 1;
10658}
316f5878
RS
10659\f
10660/* Set up globals to generate code for the ISA or processor
10661 described by INFO. */
252b5132 10662
252b5132 10663static void
17a2f251 10664mips_set_architecture (const struct mips_cpu_info *info)
252b5132 10665{
316f5878 10666 if (info != 0)
252b5132 10667 {
fef14a42
TS
10668 file_mips_arch = info->cpu;
10669 mips_opts.arch = info->cpu;
316f5878 10670 mips_opts.isa = info->isa;
252b5132 10671 }
252b5132
RH
10672}
10673
252b5132 10674
316f5878 10675/* Likewise for tuning. */
252b5132 10676
316f5878 10677static void
17a2f251 10678mips_set_tune (const struct mips_cpu_info *info)
316f5878
RS
10679{
10680 if (info != 0)
fef14a42 10681 mips_tune = info->cpu;
316f5878 10682}
80cc45a5 10683
34ba82a8 10684
252b5132 10685void
17a2f251 10686mips_after_parse_args (void)
e9670677 10687{
fef14a42
TS
10688 const struct mips_cpu_info *arch_info = 0;
10689 const struct mips_cpu_info *tune_info = 0;
10690
e9670677
MR
10691 /* GP relative stuff not working for PE */
10692 if (strncmp (TARGET_OS, "pe", 2) == 0
10693 && g_switch_value != 0)
10694 {
10695 if (g_switch_seen)
10696 as_bad (_("-G not supported in this configuration."));
10697 g_switch_value = 0;
10698 }
10699
cac012d6
AO
10700 if (mips_abi == NO_ABI)
10701 mips_abi = MIPS_DEFAULT_ABI;
10702
22923709
RS
10703 /* The following code determines the architecture and register size.
10704 Similar code was added to GCC 3.3 (see override_options() in
10705 config/mips/mips.c). The GAS and GCC code should be kept in sync
10706 as much as possible. */
e9670677 10707
316f5878 10708 if (mips_arch_string != 0)
fef14a42 10709 arch_info = mips_parse_cpu ("-march", mips_arch_string);
e9670677 10710
316f5878 10711 if (file_mips_isa != ISA_UNKNOWN)
e9670677 10712 {
316f5878 10713 /* Handle -mipsN. At this point, file_mips_isa contains the
fef14a42 10714 ISA level specified by -mipsN, while arch_info->isa contains
316f5878 10715 the -march selection (if any). */
fef14a42 10716 if (arch_info != 0)
e9670677 10717 {
316f5878
RS
10718 /* -march takes precedence over -mipsN, since it is more descriptive.
10719 There's no harm in specifying both as long as the ISA levels
10720 are the same. */
fef14a42 10721 if (file_mips_isa != arch_info->isa)
316f5878
RS
10722 as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
10723 mips_cpu_info_from_isa (file_mips_isa)->name,
fef14a42 10724 mips_cpu_info_from_isa (arch_info->isa)->name);
e9670677 10725 }
316f5878 10726 else
fef14a42 10727 arch_info = mips_cpu_info_from_isa (file_mips_isa);
e9670677
MR
10728 }
10729
fef14a42
TS
10730 if (arch_info == 0)
10731 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
e9670677 10732
fef14a42 10733 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
316f5878 10734 as_bad ("-march=%s is not compatible with the selected ABI",
fef14a42
TS
10735 arch_info->name);
10736
10737 mips_set_architecture (arch_info);
10738
10739 /* Optimize for file_mips_arch, unless -mtune selects a different processor. */
10740 if (mips_tune_string != 0)
10741 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
e9670677 10742
fef14a42
TS
10743 if (tune_info == 0)
10744 mips_set_tune (arch_info);
10745 else
10746 mips_set_tune (tune_info);
e9670677 10747
316f5878 10748 if (file_mips_gp32 >= 0)
e9670677 10749 {
316f5878
RS
10750 /* The user specified the size of the integer registers. Make sure
10751 it agrees with the ABI and ISA. */
10752 if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
10753 as_bad (_("-mgp64 used with a 32-bit processor"));
10754 else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
10755 as_bad (_("-mgp32 used with a 64-bit ABI"));
10756 else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
10757 as_bad (_("-mgp64 used with a 32-bit ABI"));
e9670677
MR
10758 }
10759 else
10760 {
316f5878
RS
10761 /* Infer the integer register size from the ABI and processor.
10762 Restrict ourselves to 32-bit registers if that's all the
10763 processor has, or if the ABI cannot handle 64-bit registers. */
10764 file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
10765 || !ISA_HAS_64BIT_REGS (mips_opts.isa));
e9670677
MR
10766 }
10767
316f5878
RS
10768 /* ??? GAS treats single-float processors as though they had 64-bit
10769 float registers (although it complains when double-precision
10770 instructions are used). As things stand, saying they have 32-bit
10771 registers would lead to spurious "register must be even" messages.
10772 So here we assume float registers are always the same size as
10773 integer ones, unless the user says otherwise. */
10774 if (file_mips_fp32 < 0)
10775 file_mips_fp32 = file_mips_gp32;
e9670677 10776
316f5878 10777 /* End of GCC-shared inference code. */
e9670677 10778
17a2f251
TS
10779 /* This flag is set when we have a 64-bit capable CPU but use only
10780 32-bit wide registers. Note that EABI does not use it. */
10781 if (ISA_HAS_64BIT_REGS (mips_opts.isa)
10782 && ((mips_abi == NO_ABI && file_mips_gp32 == 1)
10783 || mips_abi == O32_ABI))
316f5878 10784 mips_32bitmode = 1;
e9670677
MR
10785
10786 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
10787 as_bad (_("trap exception not supported at ISA 1"));
10788
e9670677
MR
10789 /* If the selected architecture includes support for ASEs, enable
10790 generation of code for them. */
a4672219 10791 if (mips_opts.mips16 == -1)
fef14a42 10792 mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_arch)) ? 1 : 0;
ffdefa66 10793 if (mips_opts.ase_mips3d == -1)
fef14a42 10794 mips_opts.ase_mips3d = (CPU_HAS_MIPS3D (file_mips_arch)) ? 1 : 0;
ffdefa66 10795 if (mips_opts.ase_mdmx == -1)
fef14a42 10796 mips_opts.ase_mdmx = (CPU_HAS_MDMX (file_mips_arch)) ? 1 : 0;
e9670677 10797
e9670677 10798 file_mips_isa = mips_opts.isa;
a4672219 10799 file_ase_mips16 = mips_opts.mips16;
e9670677
MR
10800 file_ase_mips3d = mips_opts.ase_mips3d;
10801 file_ase_mdmx = mips_opts.ase_mdmx;
10802 mips_opts.gp32 = file_mips_gp32;
10803 mips_opts.fp32 = file_mips_fp32;
10804
ecb4347a
DJ
10805 if (mips_flag_mdebug < 0)
10806 {
10807#ifdef OBJ_MAYBE_ECOFF
10808 if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
10809 mips_flag_mdebug = 1;
10810 else
10811#endif /* OBJ_MAYBE_ECOFF */
10812 mips_flag_mdebug = 0;
10813 }
e9670677
MR
10814}
10815\f
10816void
17a2f251 10817mips_init_after_args (void)
252b5132
RH
10818{
10819 /* initialize opcodes */
10820 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
beae10d5 10821 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
252b5132
RH
10822}
10823
10824long
17a2f251 10825md_pcrel_from (fixS *fixP)
252b5132 10826{
a7ebbfdf
TS
10827 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
10828 switch (fixP->fx_r_type)
10829 {
10830 case BFD_RELOC_16_PCREL_S2:
10831 case BFD_RELOC_MIPS_JMP:
10832 /* Return the address of the delay slot. */
10833 return addr + 4;
10834 default:
10835 return addr;
10836 }
252b5132
RH
10837}
10838
252b5132
RH
10839/* This is called before the symbol table is processed. In order to
10840 work with gcc when using mips-tfile, we must keep all local labels.
10841 However, in other cases, we want to discard them. If we were
10842 called with -g, but we didn't see any debugging information, it may
10843 mean that gcc is smuggling debugging information through to
10844 mips-tfile, in which case we must generate all local labels. */
10845
10846void
17a2f251 10847mips_frob_file_before_adjust (void)
252b5132
RH
10848{
10849#ifndef NO_ECOFF_DEBUGGING
10850 if (ECOFF_DEBUGGING
10851 && mips_debug != 0
10852 && ! ecoff_debugging_seen)
10853 flag_keep_locals = 1;
10854#endif
10855}
10856
10857/* Sort any unmatched HI16_S relocs so that they immediately precede
94f592af 10858 the corresponding LO reloc. This is called before md_apply_fix3 and
252b5132
RH
10859 tc_gen_reloc. Unmatched HI16_S relocs can only be generated by
10860 explicit use of the %hi modifier. */
10861
10862void
17a2f251 10863mips_frob_file (void)
252b5132
RH
10864{
10865 struct mips_hi_fixup *l;
10866
10867 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
10868 {
10869 segment_info_type *seginfo;
10870 int pass;
10871
5919d012 10872 assert (reloc_needs_lo_p (l->fixp->fx_r_type));
252b5132 10873
5919d012
RS
10874 /* If a GOT16 relocation turns out to be against a global symbol,
10875 there isn't supposed to be a matching LO. */
10876 if (l->fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
10877 && !pic_need_relax (l->fixp->fx_addsy, l->seg))
10878 continue;
10879
10880 /* Check quickly whether the next fixup happens to be a matching %lo. */
10881 if (fixup_has_matching_lo_p (l->fixp))
252b5132
RH
10882 continue;
10883
10884 /* Look through the fixups for this segment for a matching %lo.
10885 When we find one, move the %hi just in front of it. We do
10886 this in two passes. In the first pass, we try to find a
10887 unique %lo. In the second pass, we permit multiple %hi
10888 relocs for a single %lo (this is a GNU extension). */
10889 seginfo = seg_info (l->seg);
10890 for (pass = 0; pass < 2; pass++)
10891 {
10892 fixS *f, *prev;
10893
10894 prev = NULL;
10895 for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
10896 {
10897 /* Check whether this is a %lo fixup which matches l->fixp. */
10898 if (f->fx_r_type == BFD_RELOC_LO16
10899 && f->fx_addsy == l->fixp->fx_addsy
10900 && f->fx_offset == l->fixp->fx_offset
10901 && (pass == 1
10902 || prev == NULL
5919d012
RS
10903 || !reloc_needs_lo_p (prev->fx_r_type)
10904 || !fixup_has_matching_lo_p (prev)))
252b5132
RH
10905 {
10906 fixS **pf;
10907
10908 /* Move l->fixp before f. */
10909 for (pf = &seginfo->fix_root;
10910 *pf != l->fixp;
10911 pf = &(*pf)->fx_next)
10912 assert (*pf != NULL);
10913
10914 *pf = l->fixp->fx_next;
10915
10916 l->fixp->fx_next = f;
10917 if (prev == NULL)
10918 seginfo->fix_root = l->fixp;
10919 else
10920 prev->fx_next = l->fixp;
10921
10922 break;
10923 }
10924
10925 prev = f;
10926 }
10927
10928 if (f != NULL)
10929 break;
10930
10931#if 0 /* GCC code motion plus incomplete dead code elimination
10932 can leave a %hi without a %lo. */
10933 if (pass == 1)
10934 as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
10935 _("Unmatched %%hi reloc"));
10936#endif
10937 }
10938 }
10939}
10940
10941/* When generating embedded PIC code we need to use a special
10942 relocation to represent the difference of two symbols in the .text
10943 section (switch tables use a difference of this sort). See
10944 include/coff/mips.h for details. This macro checks whether this
10945 fixup requires the special reloc. */
10946#define SWITCH_TABLE(fixp) \
10947 ((fixp)->fx_r_type == BFD_RELOC_32 \
bb2d6cd7 10948 && OUTPUT_FLAVOR != bfd_target_elf_flavour \
252b5132
RH
10949 && (fixp)->fx_addsy != NULL \
10950 && (fixp)->fx_subsy != NULL \
10951 && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
10952 && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
10953
10954/* When generating embedded PIC code we must keep all PC relative
10955 relocations, in case the linker has to relax a call. We also need
f6688943
TS
10956 to keep relocations for switch table entries.
10957
10958 We may have combined relocations without symbols in the N32/N64 ABI.
10959 We have to prevent gas from dropping them. */
252b5132 10960
252b5132 10961int
17a2f251 10962mips_force_relocation (fixS *fixp)
252b5132 10963{
ae6063d4 10964 if (generic_force_reloc (fixp))
252b5132
RH
10965 return 1;
10966
f6688943
TS
10967 if (HAVE_NEWABI
10968 && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
10969 && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
10970 || fixp->fx_r_type == BFD_RELOC_HI16_S
10971 || fixp->fx_r_type == BFD_RELOC_LO16))
10972 return 1;
10973
252b5132
RH
10974 return (mips_pic == EMBEDDED_PIC
10975 && (fixp->fx_pcrel
10976 || SWITCH_TABLE (fixp)
10977 || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
10978 || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
10979}
10980
45f8dfe8
AO
10981/* This hook is called before a fix is simplified. We don't really
10982 decide whether to skip a fix here. Rather, we turn global symbols
10983 used as branch targets into local symbols, such that they undergo
10984 simplification. We can only do this if the symbol is defined and
10985 it is in the same section as the branch. If this doesn't hold, we
10986 emit a better error message than just saying the relocation is not
10987 valid for the selected object format.
10988
10989 FIXP is the fix-up we're going to try to simplify, SEG is the
10990 segment in which the fix up occurs. The return value should be
10991 non-zero to indicate the fix-up is valid for further
10992 simplifications. */
10993
10994int
17a2f251 10995mips_validate_fix (struct fix *fixP, asection *seg)
45f8dfe8
AO
10996{
10997 /* There's a lot of discussion on whether it should be possible to
10998 use R_MIPS_PC16 to represent branch relocations. The outcome
10999 seems to be that it can, but gas/bfd are very broken in creating
11000 RELA relocations for this, so for now we only accept branches to
11001 symbols in the same section. Anything else is of dubious value,
11002 since there's no guarantee that at link time the symbol would be
11003 in range. Even for branches to local symbols this is arguably
11004 wrong, since it we assume the symbol is not going to be
11005 overridden, which should be possible per ELF library semantics,
11006 but then, there isn't a dynamic relocation that could be used to
11007 this effect, and the target would likely be out of range as well.
11008
11009 Unfortunately, it seems that there is too much code out there
11010 that relies on branches to symbols that are global to be resolved
11011 as if they were local, like the IRIX tools do, so we do it as
11012 well, but with a warning so that people are reminded to fix their
11013 code. If we ever get back to using R_MIPS_PC16 for branch
11014 targets, this entire block should go away (and probably the
11015 whole function). */
11016
11017 if (fixP->fx_r_type == BFD_RELOC_16_PCREL_S2
11018 && (((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
11019 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
11020 && mips_pic != EMBEDDED_PIC)
11021 || bfd_reloc_type_lookup (stdoutput, BFD_RELOC_16_PCREL_S2) == NULL)
11022 && fixP->fx_addsy)
11023 {
11024 if (! S_IS_DEFINED (fixP->fx_addsy))
11025 {
11026 as_bad_where (fixP->fx_file, fixP->fx_line,
11027 _("Cannot branch to undefined symbol."));
11028 /* Avoid any further errors about this fixup. */
11029 fixP->fx_done = 1;
11030 }
11031 else if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
11032 {
11033 as_bad_where (fixP->fx_file, fixP->fx_line,
11034 _("Cannot branch to symbol in another section."));
11035 fixP->fx_done = 1;
11036 }
11037 else if (S_IS_EXTERNAL (fixP->fx_addsy))
11038 {
11039 symbolS *sym = fixP->fx_addsy;
11040
115695a8
CD
11041 if (mips_pic == SVR4_PIC)
11042 as_warn_where (fixP->fx_file, fixP->fx_line,
11043 _("Pretending global symbol used as branch target is local."));
45f8dfe8
AO
11044
11045 fixP->fx_addsy = symbol_create (S_GET_NAME (sym),
11046 S_GET_SEGMENT (sym),
11047 S_GET_VALUE (sym),
11048 symbol_get_frag (sym));
11049 copy_symbol_attributes (fixP->fx_addsy, sym);
11050 S_CLEAR_EXTERNAL (fixP->fx_addsy);
11051 assert (symbol_resolved_p (sym));
11052 symbol_mark_resolved (fixP->fx_addsy);
11053 }
11054 }
11055
11056 return 1;
11057}
11058
252b5132
RH
11059/* Apply a fixup to the object file. */
11060
94f592af 11061void
17a2f251 11062md_apply_fix3 (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 11063{
874e8986 11064 bfd_byte *buf;
98aa84af 11065 long insn;
ed6fb7bd 11066 static int previous_fx_r_type = 0;
a7ebbfdf 11067 reloc_howto_type *howto;
252b5132 11068
a7ebbfdf
TS
11069 /* We ignore generic BFD relocations we don't know about. */
11070 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
11071 if (! howto)
11072 return;
65551fa4 11073
252b5132
RH
11074 assert (fixP->fx_size == 4
11075 || fixP->fx_r_type == BFD_RELOC_16
11076 || fixP->fx_r_type == BFD_RELOC_64
f6688943
TS
11077 || fixP->fx_r_type == BFD_RELOC_CTOR
11078 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
252b5132 11079 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
a7ebbfdf 11080 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY);
252b5132 11081
a7ebbfdf 11082 buf = (bfd_byte *) (fixP->fx_frag->fr_literal + fixP->fx_where);
252b5132 11083
ed6fb7bd
SC
11084 /* We are not done if this is a composite relocation to set up gp. */
11085 if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel
11086 && !(fixP->fx_r_type == BFD_RELOC_MIPS_SUB
10181a0d
AO
11087 || (fixP->fx_r_type == BFD_RELOC_64
11088 && (previous_fx_r_type == BFD_RELOC_GPREL32
11089 || previous_fx_r_type == BFD_RELOC_GPREL16))
ed6fb7bd
SC
11090 || (previous_fx_r_type == BFD_RELOC_MIPS_SUB
11091 && (fixP->fx_r_type == BFD_RELOC_HI16_S
11092 || fixP->fx_r_type == BFD_RELOC_LO16))))
252b5132 11093 fixP->fx_done = 1;
ed6fb7bd 11094 previous_fx_r_type = fixP->fx_r_type;
252b5132
RH
11095
11096 switch (fixP->fx_r_type)
11097 {
11098 case BFD_RELOC_MIPS_JMP:
e369bcce
TS
11099 case BFD_RELOC_MIPS_SHIFT5:
11100 case BFD_RELOC_MIPS_SHIFT6:
11101 case BFD_RELOC_MIPS_GOT_DISP:
11102 case BFD_RELOC_MIPS_GOT_PAGE:
11103 case BFD_RELOC_MIPS_GOT_OFST:
11104 case BFD_RELOC_MIPS_SUB:
11105 case BFD_RELOC_MIPS_INSERT_A:
11106 case BFD_RELOC_MIPS_INSERT_B:
11107 case BFD_RELOC_MIPS_DELETE:
11108 case BFD_RELOC_MIPS_HIGHEST:
11109 case BFD_RELOC_MIPS_HIGHER:
11110 case BFD_RELOC_MIPS_SCN_DISP:
11111 case BFD_RELOC_MIPS_REL16:
11112 case BFD_RELOC_MIPS_RELGOT:
11113 case BFD_RELOC_MIPS_JALR:
252b5132
RH
11114 case BFD_RELOC_HI16:
11115 case BFD_RELOC_HI16_S:
cdf6fd85 11116 case BFD_RELOC_GPREL16:
252b5132
RH
11117 case BFD_RELOC_MIPS_LITERAL:
11118 case BFD_RELOC_MIPS_CALL16:
11119 case BFD_RELOC_MIPS_GOT16:
cdf6fd85 11120 case BFD_RELOC_GPREL32:
252b5132
RH
11121 case BFD_RELOC_MIPS_GOT_HI16:
11122 case BFD_RELOC_MIPS_GOT_LO16:
11123 case BFD_RELOC_MIPS_CALL_HI16:
11124 case BFD_RELOC_MIPS_CALL_LO16:
11125 case BFD_RELOC_MIPS16_GPREL:
11126 if (fixP->fx_pcrel)
11127 as_bad_where (fixP->fx_file, fixP->fx_line,
11128 _("Invalid PC relative reloc"));
11129 /* Nothing needed to do. The value comes from the reloc entry */
11130 break;
11131
11132 case BFD_RELOC_MIPS16_JMP:
11133 /* We currently always generate a reloc against a symbol, which
11134 means that we don't want an addend even if the symbol is
11135 defined. */
a7ebbfdf 11136 *valP = 0;
252b5132
RH
11137 break;
11138
11139 case BFD_RELOC_PCREL_HI16_S:
11140 /* The addend for this is tricky if it is internal, so we just
11141 do everything here rather than in bfd_install_relocation. */
a7ebbfdf 11142 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && !fixP->fx_done)
bb2d6cd7
GK
11143 break;
11144 if (fixP->fx_addsy
11145 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
252b5132
RH
11146 {
11147 /* For an external symbol adjust by the address to make it
11148 pcrel_offset. We use the address of the RELLO reloc
11149 which follows this one. */
a7ebbfdf 11150 *valP += (fixP->fx_next->fx_frag->fr_address
252b5132
RH
11151 + fixP->fx_next->fx_where);
11152 }
a7ebbfdf 11153 *valP = ((*valP + 0x8000) >> 16) & 0xffff;
252b5132
RH
11154 if (target_big_endian)
11155 buf += 2;
17a2f251 11156 md_number_to_chars (buf, *valP, 2);
252b5132
RH
11157 break;
11158
11159 case BFD_RELOC_PCREL_LO16:
11160 /* The addend for this is tricky if it is internal, so we just
11161 do everything here rather than in bfd_install_relocation. */
a7ebbfdf 11162 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && !fixP->fx_done)
bb2d6cd7
GK
11163 break;
11164 if (fixP->fx_addsy
11165 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
a7ebbfdf 11166 *valP += fixP->fx_frag->fr_address + fixP->fx_where;
252b5132
RH
11167 if (target_big_endian)
11168 buf += 2;
17a2f251 11169 md_number_to_chars (buf, *valP, 2);
252b5132
RH
11170 break;
11171
11172 case BFD_RELOC_64:
11173 /* This is handled like BFD_RELOC_32, but we output a sign
11174 extended value if we are only 32 bits. */
11175 if (fixP->fx_done
11176 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11177 {
11178 if (8 <= sizeof (valueT))
a7ebbfdf 11179 md_number_to_chars (buf, *valP, 8);
252b5132
RH
11180 else
11181 {
a7ebbfdf 11182 valueT hiv;
252b5132 11183
a7ebbfdf 11184 if ((*valP & 0x80000000) != 0)
252b5132
RH
11185 hiv = 0xffffffff;
11186 else
11187 hiv = 0;
a7ebbfdf
TS
11188 md_number_to_chars ((char *)(buf + target_big_endian ? 4 : 0),
11189 *valP, 4);
11190 md_number_to_chars ((char *)(buf + target_big_endian ? 0 : 4),
11191 hiv, 4);
252b5132
RH
11192 }
11193 }
11194 break;
11195
056350c6 11196 case BFD_RELOC_RVA:
252b5132
RH
11197 case BFD_RELOC_32:
11198 /* If we are deleting this reloc entry, we must fill in the
11199 value now. This can happen if we have a .word which is not
11200 resolved when it appears but is later defined. We also need
11201 to fill in the value if this is an embedded PIC switch table
11202 entry. */
11203 if (fixP->fx_done
11204 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
a7ebbfdf 11205 md_number_to_chars (buf, *valP, 4);
252b5132
RH
11206 break;
11207
11208 case BFD_RELOC_16:
11209 /* If we are deleting this reloc entry, we must fill in the
11210 value now. */
11211 assert (fixP->fx_size == 2);
11212 if (fixP->fx_done)
a7ebbfdf 11213 md_number_to_chars (buf, *valP, 2);
252b5132
RH
11214 break;
11215
11216 case BFD_RELOC_LO16:
11217 /* When handling an embedded PIC switch statement, we can wind
11218 up deleting a LO16 reloc. See the 'o' case in mips_ip. */
11219 if (fixP->fx_done)
11220 {
a7ebbfdf 11221 if (*valP + 0x8000 > 0xffff)
252b5132
RH
11222 as_bad_where (fixP->fx_file, fixP->fx_line,
11223 _("relocation overflow"));
252b5132
RH
11224 if (target_big_endian)
11225 buf += 2;
17a2f251 11226 md_number_to_chars (buf, *valP, 2);
252b5132
RH
11227 }
11228 break;
11229
11230 case BFD_RELOC_16_PCREL_S2:
a7ebbfdf 11231 if ((*valP & 0x3) != 0)
cb56d3d3 11232 as_bad_where (fixP->fx_file, fixP->fx_line,
a7ebbfdf 11233 _("Branch to odd address (%lx)"), (long) *valP);
cb56d3d3 11234
252b5132
RH
11235 /*
11236 * We need to save the bits in the instruction since fixup_segment()
11237 * might be deleting the relocation entry (i.e., a branch within
11238 * the current segment).
11239 */
a7ebbfdf 11240 if (! fixP->fx_done)
bb2d6cd7 11241 break;
252b5132
RH
11242
11243 /* update old instruction data */
252b5132
RH
11244 if (target_big_endian)
11245 insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
11246 else
11247 insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
11248
a7ebbfdf
TS
11249 if (*valP + 0x20000 <= 0x3ffff)
11250 {
11251 insn |= (*valP >> 2) & 0xffff;
17a2f251 11252 md_number_to_chars (buf, insn, 4);
a7ebbfdf
TS
11253 }
11254 else if (mips_pic == NO_PIC
11255 && fixP->fx_done
11256 && fixP->fx_frag->fr_address >= text_section->vma
11257 && (fixP->fx_frag->fr_address
11258 < text_section->vma + text_section->_raw_size)
11259 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
11260 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
11261 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
252b5132
RH
11262 {
11263 /* The branch offset is too large. If this is an
11264 unconditional branch, and we are not generating PIC code,
11265 we can convert it to an absolute jump instruction. */
a7ebbfdf
TS
11266 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
11267 insn = 0x0c000000; /* jal */
252b5132 11268 else
a7ebbfdf
TS
11269 insn = 0x08000000; /* j */
11270 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
11271 fixP->fx_done = 0;
11272 fixP->fx_addsy = section_symbol (text_section);
11273 *valP += md_pcrel_from (fixP);
17a2f251 11274 md_number_to_chars (buf, insn, 4);
a7ebbfdf
TS
11275 }
11276 else
11277 {
11278 /* If we got here, we have branch-relaxation disabled,
11279 and there's nothing we can do to fix this instruction
11280 without turning it into a longer sequence. */
11281 as_bad_where (fixP->fx_file, fixP->fx_line,
11282 _("Branch out of range"));
252b5132 11283 }
252b5132
RH
11284 break;
11285
11286 case BFD_RELOC_VTABLE_INHERIT:
11287 fixP->fx_done = 0;
11288 if (fixP->fx_addsy
11289 && !S_IS_DEFINED (fixP->fx_addsy)
11290 && !S_IS_WEAK (fixP->fx_addsy))
11291 S_SET_WEAK (fixP->fx_addsy);
11292 break;
11293
11294 case BFD_RELOC_VTABLE_ENTRY:
11295 fixP->fx_done = 0;
11296 break;
11297
11298 default:
11299 internalError ();
11300 }
a7ebbfdf
TS
11301
11302 /* Remember value for tc_gen_reloc. */
11303 fixP->fx_addnumber = *valP;
252b5132
RH
11304}
11305
11306#if 0
11307void
17a2f251 11308printInsn (unsigned long oc)
252b5132
RH
11309{
11310 const struct mips_opcode *p;
11311 int treg, sreg, dreg, shamt;
11312 short imm;
11313 const char *args;
11314 int i;
11315
11316 for (i = 0; i < NUMOPCODES; ++i)
11317 {
11318 p = &mips_opcodes[i];
11319 if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
11320 {
11321 printf ("%08lx %s\t", oc, p->name);
11322 treg = (oc >> 16) & 0x1f;
11323 sreg = (oc >> 21) & 0x1f;
11324 dreg = (oc >> 11) & 0x1f;
11325 shamt = (oc >> 6) & 0x1f;
11326 imm = oc;
11327 for (args = p->args;; ++args)
11328 {
11329 switch (*args)
11330 {
11331 case '\0':
11332 printf ("\n");
11333 break;
11334
11335 case ',':
11336 case '(':
11337 case ')':
11338 printf ("%c", *args);
11339 continue;
11340
11341 case 'r':
11342 assert (treg == sreg);
11343 printf ("$%d,$%d", treg, sreg);
11344 continue;
11345
11346 case 'd':
11347 case 'G':
11348 printf ("$%d", dreg);
11349 continue;
11350
11351 case 't':
11352 case 'E':
11353 printf ("$%d", treg);
11354 continue;
11355
11356 case 'k':
11357 printf ("0x%x", treg);
11358 continue;
11359
11360 case 'b':
11361 case 's':
11362 printf ("$%d", sreg);
11363 continue;
11364
11365 case 'a':
11366 printf ("0x%08lx", oc & 0x1ffffff);
11367 continue;
11368
11369 case 'i':
11370 case 'j':
11371 case 'o':
11372 case 'u':
11373 printf ("%d", imm);
11374 continue;
11375
11376 case '<':
11377 case '>':
11378 printf ("$%d", shamt);
11379 continue;
11380
11381 default:
11382 internalError ();
11383 }
11384 break;
11385 }
11386 return;
11387 }
11388 }
11389 printf (_("%08lx UNDEFINED\n"), oc);
11390}
11391#endif
11392
11393static symbolS *
17a2f251 11394get_symbol (void)
252b5132
RH
11395{
11396 int c;
11397 char *name;
11398 symbolS *p;
11399
11400 name = input_line_pointer;
11401 c = get_symbol_end ();
11402 p = (symbolS *) symbol_find_or_make (name);
11403 *input_line_pointer = c;
11404 return p;
11405}
11406
11407/* Align the current frag to a given power of two. The MIPS assembler
11408 also automatically adjusts any preceding label. */
11409
11410static void
17a2f251 11411mips_align (int to, int fill, symbolS *label)
252b5132 11412{
b34976b6 11413 mips_emit_delays (FALSE);
252b5132
RH
11414 frag_align (to, fill, 0);
11415 record_alignment (now_seg, to);
11416 if (label != NULL)
11417 {
11418 assert (S_GET_SEGMENT (label) == now_seg);
49309057 11419 symbol_set_frag (label, frag_now);
252b5132
RH
11420 S_SET_VALUE (label, (valueT) frag_now_fix ());
11421 }
11422}
11423
11424/* Align to a given power of two. .align 0 turns off the automatic
11425 alignment used by the data creating pseudo-ops. */
11426
11427static void
17a2f251 11428s_align (int x ATTRIBUTE_UNUSED)
252b5132
RH
11429{
11430 register int temp;
11431 register long temp_fill;
11432 long max_alignment = 15;
11433
11434 /*
11435
67c1ffbe 11436 o Note that the assembler pulls down any immediately preceding label
252b5132
RH
11437 to the aligned address.
11438 o It's not documented but auto alignment is reinstated by
11439 a .align pseudo instruction.
11440 o Note also that after auto alignment is turned off the mips assembler
11441 issues an error on attempt to assemble an improperly aligned data item.
11442 We don't.
11443
11444 */
11445
11446 temp = get_absolute_expression ();
11447 if (temp > max_alignment)
11448 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
11449 else if (temp < 0)
11450 {
11451 as_warn (_("Alignment negative: 0 assumed."));
11452 temp = 0;
11453 }
11454 if (*input_line_pointer == ',')
11455 {
f9419b05 11456 ++input_line_pointer;
252b5132
RH
11457 temp_fill = get_absolute_expression ();
11458 }
11459 else
11460 temp_fill = 0;
11461 if (temp)
11462 {
11463 auto_align = 1;
11464 mips_align (temp, (int) temp_fill,
11465 insn_labels != NULL ? insn_labels->label : NULL);
11466 }
11467 else
11468 {
11469 auto_align = 0;
11470 }
11471
11472 demand_empty_rest_of_line ();
11473}
11474
11475void
17a2f251 11476mips_flush_pending_output (void)
252b5132 11477{
b34976b6 11478 mips_emit_delays (FALSE);
252b5132
RH
11479 mips_clear_insn_labels ();
11480}
11481
11482static void
17a2f251 11483s_change_sec (int sec)
252b5132
RH
11484{
11485 segT seg;
11486
11487 /* When generating embedded PIC code, we only use the .text, .lit8,
11488 .sdata and .sbss sections. We change the .data and .rdata
11489 pseudo-ops to use .sdata. */
11490 if (mips_pic == EMBEDDED_PIC
11491 && (sec == 'd' || sec == 'r'))
11492 sec = 's';
11493
11494#ifdef OBJ_ELF
11495 /* The ELF backend needs to know that we are changing sections, so
11496 that .previous works correctly. We could do something like check
b6ff326e 11497 for an obj_section_change_hook macro, but that might be confusing
252b5132
RH
11498 as it would not be appropriate to use it in the section changing
11499 functions in read.c, since obj-elf.c intercepts those. FIXME:
11500 This should be cleaner, somehow. */
11501 obj_elf_section_change_hook ();
11502#endif
11503
b34976b6 11504 mips_emit_delays (FALSE);
252b5132
RH
11505 switch (sec)
11506 {
11507 case 't':
11508 s_text (0);
11509 break;
11510 case 'd':
11511 s_data (0);
11512 break;
11513 case 'b':
11514 subseg_set (bss_section, (subsegT) get_absolute_expression ());
11515 demand_empty_rest_of_line ();
11516 break;
11517
11518 case 'r':
4d0d148d
TS
11519 seg = subseg_new (RDATA_SECTION_NAME,
11520 (subsegT) get_absolute_expression ());
11521 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
252b5132 11522 {
4d0d148d
TS
11523 bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
11524 | SEC_READONLY | SEC_RELOC
11525 | SEC_DATA));
11526 if (strcmp (TARGET_OS, "elf") != 0)
11527 record_alignment (seg, 4);
252b5132 11528 }
4d0d148d 11529 demand_empty_rest_of_line ();
252b5132
RH
11530 break;
11531
11532 case 's':
4d0d148d
TS
11533 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
11534 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
252b5132 11535 {
4d0d148d
TS
11536 bfd_set_section_flags (stdoutput, seg,
11537 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
11538 if (strcmp (TARGET_OS, "elf") != 0)
11539 record_alignment (seg, 4);
252b5132 11540 }
4d0d148d
TS
11541 demand_empty_rest_of_line ();
11542 break;
252b5132
RH
11543 }
11544
11545 auto_align = 1;
11546}
b34976b6 11547
cca86cc8 11548void
17a2f251 11549s_change_section (int ignore ATTRIBUTE_UNUSED)
cca86cc8 11550{
7ed4a06a 11551#ifdef OBJ_ELF
cca86cc8
SC
11552 char *section_name;
11553 char c;
684022ea 11554 char next_c = 0;
cca86cc8
SC
11555 int section_type;
11556 int section_flag;
11557 int section_entry_size;
11558 int section_alignment;
b34976b6 11559
7ed4a06a
TS
11560 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11561 return;
11562
cca86cc8
SC
11563 section_name = input_line_pointer;
11564 c = get_symbol_end ();
a816d1ed
AO
11565 if (c)
11566 next_c = *(input_line_pointer + 1);
cca86cc8 11567
4cf0dd0d
TS
11568 /* Do we have .section Name<,"flags">? */
11569 if (c != ',' || (c == ',' && next_c == '"'))
cca86cc8 11570 {
4cf0dd0d
TS
11571 /* just after name is now '\0'. */
11572 *input_line_pointer = c;
cca86cc8
SC
11573 input_line_pointer = section_name;
11574 obj_elf_section (ignore);
11575 return;
11576 }
11577 input_line_pointer++;
11578
11579 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
11580 if (c == ',')
11581 section_type = get_absolute_expression ();
11582 else
11583 section_type = 0;
11584 if (*input_line_pointer++ == ',')
11585 section_flag = get_absolute_expression ();
11586 else
11587 section_flag = 0;
11588 if (*input_line_pointer++ == ',')
11589 section_entry_size = get_absolute_expression ();
11590 else
11591 section_entry_size = 0;
11592 if (*input_line_pointer++ == ',')
11593 section_alignment = get_absolute_expression ();
11594 else
11595 section_alignment = 0;
11596
a816d1ed
AO
11597 section_name = xstrdup (section_name);
11598
8ab8a5c8
RS
11599 /* When using the generic form of .section (as implemented by obj-elf.c),
11600 there's no way to set the section type to SHT_MIPS_DWARF. Users have
11601 traditionally had to fall back on the more common @progbits instead.
11602
11603 There's nothing really harmful in this, since bfd will correct
11604 SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file. But it
11605 means that, for backwards compatibiltiy, the special_section entries
11606 for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
11607
11608 Even so, we shouldn't force users of the MIPS .section syntax to
11609 incorrectly label the sections as SHT_PROGBITS. The best compromise
11610 seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
11611 generic type-checking code. */
11612 if (section_type == SHT_MIPS_DWARF)
11613 section_type = SHT_PROGBITS;
11614
cca86cc8
SC
11615 obj_elf_change_section (section_name, section_type, section_flag,
11616 section_entry_size, 0, 0, 0);
a816d1ed
AO
11617
11618 if (now_seg->name != section_name)
11619 free (section_name);
7ed4a06a 11620#endif /* OBJ_ELF */
cca86cc8 11621}
252b5132
RH
11622
11623void
17a2f251 11624mips_enable_auto_align (void)
252b5132
RH
11625{
11626 auto_align = 1;
11627}
11628
11629static void
17a2f251 11630s_cons (int log_size)
252b5132
RH
11631{
11632 symbolS *label;
11633
11634 label = insn_labels != NULL ? insn_labels->label : NULL;
b34976b6 11635 mips_emit_delays (FALSE);
252b5132
RH
11636 if (log_size > 0 && auto_align)
11637 mips_align (log_size, 0, label);
11638 mips_clear_insn_labels ();
11639 cons (1 << log_size);
11640}
11641
11642static void
17a2f251 11643s_float_cons (int type)
252b5132
RH
11644{
11645 symbolS *label;
11646
11647 label = insn_labels != NULL ? insn_labels->label : NULL;
11648
b34976b6 11649 mips_emit_delays (FALSE);
252b5132
RH
11650
11651 if (auto_align)
49309057
ILT
11652 {
11653 if (type == 'd')
11654 mips_align (3, 0, label);
11655 else
11656 mips_align (2, 0, label);
11657 }
252b5132
RH
11658
11659 mips_clear_insn_labels ();
11660
11661 float_cons (type);
11662}
11663
11664/* Handle .globl. We need to override it because on Irix 5 you are
11665 permitted to say
11666 .globl foo .text
11667 where foo is an undefined symbol, to mean that foo should be
11668 considered to be the address of a function. */
11669
11670static void
17a2f251 11671s_mips_globl (int x ATTRIBUTE_UNUSED)
252b5132
RH
11672{
11673 char *name;
11674 int c;
11675 symbolS *symbolP;
11676 flagword flag;
11677
11678 name = input_line_pointer;
11679 c = get_symbol_end ();
11680 symbolP = symbol_find_or_make (name);
11681 *input_line_pointer = c;
11682 SKIP_WHITESPACE ();
11683
11684 /* On Irix 5, every global symbol that is not explicitly labelled as
11685 being a function is apparently labelled as being an object. */
11686 flag = BSF_OBJECT;
11687
11688 if (! is_end_of_line[(unsigned char) *input_line_pointer])
11689 {
11690 char *secname;
11691 asection *sec;
11692
11693 secname = input_line_pointer;
11694 c = get_symbol_end ();
11695 sec = bfd_get_section_by_name (stdoutput, secname);
11696 if (sec == NULL)
11697 as_bad (_("%s: no such section"), secname);
11698 *input_line_pointer = c;
11699
11700 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
11701 flag = BSF_FUNCTION;
11702 }
11703
49309057 11704 symbol_get_bfdsym (symbolP)->flags |= flag;
252b5132
RH
11705
11706 S_SET_EXTERNAL (symbolP);
11707 demand_empty_rest_of_line ();
11708}
11709
11710static void
17a2f251 11711s_option (int x ATTRIBUTE_UNUSED)
252b5132
RH
11712{
11713 char *opt;
11714 char c;
11715
11716 opt = input_line_pointer;
11717 c = get_symbol_end ();
11718
11719 if (*opt == 'O')
11720 {
11721 /* FIXME: What does this mean? */
11722 }
11723 else if (strncmp (opt, "pic", 3) == 0)
11724 {
11725 int i;
11726
11727 i = atoi (opt + 3);
11728 if (i == 0)
11729 mips_pic = NO_PIC;
11730 else if (i == 2)
143d77c5 11731 {
252b5132 11732 mips_pic = SVR4_PIC;
143d77c5
EC
11733 mips_abicalls = TRUE;
11734 }
252b5132
RH
11735 else
11736 as_bad (_(".option pic%d not supported"), i);
11737
4d0d148d 11738 if (mips_pic == SVR4_PIC)
252b5132
RH
11739 {
11740 if (g_switch_seen && g_switch_value != 0)
11741 as_warn (_("-G may not be used with SVR4 PIC code"));
11742 g_switch_value = 0;
11743 bfd_set_gp_size (stdoutput, 0);
11744 }
11745 }
11746 else
11747 as_warn (_("Unrecognized option \"%s\""), opt);
11748
11749 *input_line_pointer = c;
11750 demand_empty_rest_of_line ();
11751}
11752
11753/* This structure is used to hold a stack of .set values. */
11754
e972090a
NC
11755struct mips_option_stack
11756{
252b5132
RH
11757 struct mips_option_stack *next;
11758 struct mips_set_options options;
11759};
11760
11761static struct mips_option_stack *mips_opts_stack;
11762
11763/* Handle the .set pseudo-op. */
11764
11765static void
17a2f251 11766s_mipsset (int x ATTRIBUTE_UNUSED)
252b5132
RH
11767{
11768 char *name = input_line_pointer, ch;
11769
11770 while (!is_end_of_line[(unsigned char) *input_line_pointer])
f9419b05 11771 ++input_line_pointer;
252b5132
RH
11772 ch = *input_line_pointer;
11773 *input_line_pointer = '\0';
11774
11775 if (strcmp (name, "reorder") == 0)
11776 {
11777 if (mips_opts.noreorder && prev_nop_frag != NULL)
11778 {
11779 /* If we still have pending nops, we can discard them. The
11780 usual nop handling will insert any that are still
bdaaa2e1 11781 needed. */
252b5132
RH
11782 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11783 * (mips_opts.mips16 ? 2 : 4));
11784 prev_nop_frag = NULL;
11785 }
11786 mips_opts.noreorder = 0;
11787 }
11788 else if (strcmp (name, "noreorder") == 0)
11789 {
b34976b6 11790 mips_emit_delays (TRUE);
252b5132
RH
11791 mips_opts.noreorder = 1;
11792 mips_any_noreorder = 1;
11793 }
11794 else if (strcmp (name, "at") == 0)
11795 {
11796 mips_opts.noat = 0;
11797 }
11798 else if (strcmp (name, "noat") == 0)
11799 {
11800 mips_opts.noat = 1;
11801 }
11802 else if (strcmp (name, "macro") == 0)
11803 {
11804 mips_opts.warn_about_macros = 0;
11805 }
11806 else if (strcmp (name, "nomacro") == 0)
11807 {
11808 if (mips_opts.noreorder == 0)
11809 as_bad (_("`noreorder' must be set before `nomacro'"));
11810 mips_opts.warn_about_macros = 1;
11811 }
11812 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
11813 {
11814 mips_opts.nomove = 0;
11815 }
11816 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
11817 {
11818 mips_opts.nomove = 1;
11819 }
11820 else if (strcmp (name, "bopt") == 0)
11821 {
11822 mips_opts.nobopt = 0;
11823 }
11824 else if (strcmp (name, "nobopt") == 0)
11825 {
11826 mips_opts.nobopt = 1;
11827 }
11828 else if (strcmp (name, "mips16") == 0
11829 || strcmp (name, "MIPS-16") == 0)
11830 mips_opts.mips16 = 1;
11831 else if (strcmp (name, "nomips16") == 0
11832 || strcmp (name, "noMIPS-16") == 0)
11833 mips_opts.mips16 = 0;
1f25f5d3
CD
11834 else if (strcmp (name, "mips3d") == 0)
11835 mips_opts.ase_mips3d = 1;
11836 else if (strcmp (name, "nomips3d") == 0)
11837 mips_opts.ase_mips3d = 0;
a4672219
TS
11838 else if (strcmp (name, "mdmx") == 0)
11839 mips_opts.ase_mdmx = 1;
11840 else if (strcmp (name, "nomdmx") == 0)
11841 mips_opts.ase_mdmx = 0;
1a2c1fad 11842 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
252b5132 11843 {
af7ee8bf 11844 int reset = 0;
252b5132 11845
1a2c1fad
CD
11846 /* Permit the user to change the ISA and architecture on the fly.
11847 Needless to say, misuse can cause serious problems. */
81a21e38 11848 if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
af7ee8bf
CD
11849 {
11850 reset = 1;
11851 mips_opts.isa = file_mips_isa;
1a2c1fad 11852 mips_opts.arch = file_mips_arch;
1a2c1fad
CD
11853 }
11854 else if (strncmp (name, "arch=", 5) == 0)
11855 {
11856 const struct mips_cpu_info *p;
11857
11858 p = mips_parse_cpu("internal use", name + 5);
11859 if (!p)
11860 as_bad (_("unknown architecture %s"), name + 5);
11861 else
11862 {
11863 mips_opts.arch = p->cpu;
11864 mips_opts.isa = p->isa;
11865 }
11866 }
81a21e38
TS
11867 else if (strncmp (name, "mips", 4) == 0)
11868 {
11869 const struct mips_cpu_info *p;
11870
11871 p = mips_parse_cpu("internal use", name);
11872 if (!p)
11873 as_bad (_("unknown ISA level %s"), name + 4);
11874 else
11875 {
11876 mips_opts.arch = p->cpu;
11877 mips_opts.isa = p->isa;
11878 }
11879 }
af7ee8bf 11880 else
81a21e38 11881 as_bad (_("unknown ISA or architecture %s"), name);
af7ee8bf
CD
11882
11883 switch (mips_opts.isa)
98d3f06f
KH
11884 {
11885 case 0:
98d3f06f 11886 break;
af7ee8bf
CD
11887 case ISA_MIPS1:
11888 case ISA_MIPS2:
11889 case ISA_MIPS32:
11890 case ISA_MIPS32R2:
98d3f06f
KH
11891 mips_opts.gp32 = 1;
11892 mips_opts.fp32 = 1;
11893 break;
af7ee8bf
CD
11894 case ISA_MIPS3:
11895 case ISA_MIPS4:
11896 case ISA_MIPS5:
11897 case ISA_MIPS64:
5f74bc13 11898 case ISA_MIPS64R2:
98d3f06f
KH
11899 mips_opts.gp32 = 0;
11900 mips_opts.fp32 = 0;
11901 break;
11902 default:
11903 as_bad (_("unknown ISA level %s"), name + 4);
11904 break;
11905 }
af7ee8bf 11906 if (reset)
98d3f06f 11907 {
af7ee8bf
CD
11908 mips_opts.gp32 = file_mips_gp32;
11909 mips_opts.fp32 = file_mips_fp32;
98d3f06f 11910 }
252b5132
RH
11911 }
11912 else if (strcmp (name, "autoextend") == 0)
11913 mips_opts.noautoextend = 0;
11914 else if (strcmp (name, "noautoextend") == 0)
11915 mips_opts.noautoextend = 1;
11916 else if (strcmp (name, "push") == 0)
11917 {
11918 struct mips_option_stack *s;
11919
11920 s = (struct mips_option_stack *) xmalloc (sizeof *s);
11921 s->next = mips_opts_stack;
11922 s->options = mips_opts;
11923 mips_opts_stack = s;
11924 }
11925 else if (strcmp (name, "pop") == 0)
11926 {
11927 struct mips_option_stack *s;
11928
11929 s = mips_opts_stack;
11930 if (s == NULL)
11931 as_bad (_(".set pop with no .set push"));
11932 else
11933 {
11934 /* If we're changing the reorder mode we need to handle
11935 delay slots correctly. */
11936 if (s->options.noreorder && ! mips_opts.noreorder)
b34976b6 11937 mips_emit_delays (TRUE);
252b5132
RH
11938 else if (! s->options.noreorder && mips_opts.noreorder)
11939 {
11940 if (prev_nop_frag != NULL)
11941 {
11942 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11943 * (mips_opts.mips16 ? 2 : 4));
11944 prev_nop_frag = NULL;
11945 }
11946 }
11947
11948 mips_opts = s->options;
11949 mips_opts_stack = s->next;
11950 free (s);
11951 }
11952 }
11953 else
11954 {
11955 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
11956 }
11957 *input_line_pointer = ch;
11958 demand_empty_rest_of_line ();
11959}
11960
11961/* Handle the .abicalls pseudo-op. I believe this is equivalent to
11962 .option pic2. It means to generate SVR4 PIC calls. */
11963
11964static void
17a2f251 11965s_abicalls (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
11966{
11967 mips_pic = SVR4_PIC;
143d77c5 11968 mips_abicalls = TRUE;
4d0d148d
TS
11969
11970 if (g_switch_seen && g_switch_value != 0)
11971 as_warn (_("-G may not be used with SVR4 PIC code"));
11972 g_switch_value = 0;
11973
252b5132
RH
11974 bfd_set_gp_size (stdoutput, 0);
11975 demand_empty_rest_of_line ();
11976}
11977
11978/* Handle the .cpload pseudo-op. This is used when generating SVR4
11979 PIC code. It sets the $gp register for the function based on the
11980 function address, which is in the register named in the argument.
11981 This uses a relocation against _gp_disp, which is handled specially
11982 by the linker. The result is:
11983 lui $gp,%hi(_gp_disp)
11984 addiu $gp,$gp,%lo(_gp_disp)
11985 addu $gp,$gp,.cpload argument
11986 The .cpload argument is normally $25 == $t9. */
11987
11988static void
17a2f251 11989s_cpload (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
11990{
11991 expressionS ex;
252b5132 11992
6478892d
TS
11993 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
11994 .cpload is ignored. */
11995 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
11996 {
11997 s_ignore (0);
11998 return;
11999 }
12000
d3ecfc59 12001 /* .cpload should be in a .set noreorder section. */
252b5132
RH
12002 if (mips_opts.noreorder == 0)
12003 as_warn (_(".cpload not in noreorder section"));
12004
12005 ex.X_op = O_symbol;
12006 ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
12007 ex.X_op_symbol = NULL;
12008 ex.X_add_number = 0;
12009
12010 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
49309057 12011 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
252b5132 12012
584892a6 12013 macro_start ();
67c0d1eb
RS
12014 macro_build_lui (&ex, mips_gp_register);
12015 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
17a2f251 12016 mips_gp_register, BFD_RELOC_LO16);
67c0d1eb 12017 macro_build (NULL, "addu", "d,v,t", mips_gp_register,
17a2f251 12018 mips_gp_register, tc_get_register (0));
584892a6 12019 macro_end ();
252b5132
RH
12020
12021 demand_empty_rest_of_line ();
12022}
12023
6478892d
TS
12024/* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
12025 .cpsetup $reg1, offset|$reg2, label
12026
12027 If offset is given, this results in:
12028 sd $gp, offset($sp)
956cd1d6 12029 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
12030 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
12031 daddu $gp, $gp, $reg1
6478892d
TS
12032
12033 If $reg2 is given, this results in:
12034 daddu $reg2, $gp, $0
956cd1d6 12035 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
12036 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
12037 daddu $gp, $gp, $reg1
12038 $reg1 is normally $25 == $t9. */
6478892d 12039static void
17a2f251 12040s_cpsetup (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
12041{
12042 expressionS ex_off;
12043 expressionS ex_sym;
12044 int reg1;
f21f8242 12045 char *f;
6478892d 12046
8586fc66 12047 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
6478892d
TS
12048 We also need NewABI support. */
12049 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12050 {
12051 s_ignore (0);
12052 return;
12053 }
12054
12055 reg1 = tc_get_register (0);
12056 SKIP_WHITESPACE ();
12057 if (*input_line_pointer != ',')
12058 {
12059 as_bad (_("missing argument separator ',' for .cpsetup"));
12060 return;
12061 }
12062 else
80245285 12063 ++input_line_pointer;
6478892d
TS
12064 SKIP_WHITESPACE ();
12065 if (*input_line_pointer == '$')
80245285
TS
12066 {
12067 mips_cpreturn_register = tc_get_register (0);
12068 mips_cpreturn_offset = -1;
12069 }
6478892d 12070 else
80245285
TS
12071 {
12072 mips_cpreturn_offset = get_absolute_expression ();
12073 mips_cpreturn_register = -1;
12074 }
6478892d
TS
12075 SKIP_WHITESPACE ();
12076 if (*input_line_pointer != ',')
12077 {
12078 as_bad (_("missing argument separator ',' for .cpsetup"));
12079 return;
12080 }
12081 else
f9419b05 12082 ++input_line_pointer;
6478892d 12083 SKIP_WHITESPACE ();
f21f8242 12084 expression (&ex_sym);
6478892d 12085
584892a6 12086 macro_start ();
6478892d
TS
12087 if (mips_cpreturn_register == -1)
12088 {
12089 ex_off.X_op = O_constant;
12090 ex_off.X_add_symbol = NULL;
12091 ex_off.X_op_symbol = NULL;
12092 ex_off.X_add_number = mips_cpreturn_offset;
12093
67c0d1eb 12094 macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
17a2f251 12095 BFD_RELOC_LO16, SP);
6478892d
TS
12096 }
12097 else
67c0d1eb 12098 macro_build (NULL, "daddu", "d,v,t", mips_cpreturn_register,
17a2f251 12099 mips_gp_register, 0);
6478892d 12100
f21f8242
AO
12101 /* Ensure there's room for the next two instructions, so that `f'
12102 doesn't end up with an address in the wrong frag. */
12103 frag_grow (8);
12104 f = frag_more (0);
67c0d1eb 12105 macro_build (&ex_sym, "lui", "t,u", mips_gp_register, BFD_RELOC_GPREL16);
f21f8242 12106 fix_new (frag_now, f - frag_now->fr_literal,
a105a300 12107 8, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
f21f8242 12108 fix_new (frag_now, f - frag_now->fr_literal,
a105a300 12109 4, NULL, 0, 0, BFD_RELOC_HI16_S);
f21f8242
AO
12110
12111 f = frag_more (0);
67c0d1eb 12112 macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
17a2f251 12113 mips_gp_register, BFD_RELOC_GPREL16);
f21f8242 12114 fix_new (frag_now, f - frag_now->fr_literal,
a105a300 12115 8, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
f21f8242 12116 fix_new (frag_now, f - frag_now->fr_literal,
a105a300 12117 4, NULL, 0, 0, BFD_RELOC_LO16);
f21f8242 12118
67c0d1eb 12119 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
17a2f251 12120 mips_gp_register, reg1);
584892a6 12121 macro_end ();
6478892d
TS
12122
12123 demand_empty_rest_of_line ();
12124}
12125
12126static void
17a2f251 12127s_cplocal (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
12128{
12129 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
12130 .cplocal is ignored. */
12131 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12132 {
12133 s_ignore (0);
12134 return;
12135 }
12136
12137 mips_gp_register = tc_get_register (0);
85b51719 12138 demand_empty_rest_of_line ();
6478892d
TS
12139}
12140
252b5132
RH
12141/* Handle the .cprestore pseudo-op. This stores $gp into a given
12142 offset from $sp. The offset is remembered, and after making a PIC
12143 call $gp is restored from that location. */
12144
12145static void
17a2f251 12146s_cprestore (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
12147{
12148 expressionS ex;
252b5132 12149
6478892d 12150 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
c9914766 12151 .cprestore is ignored. */
6478892d 12152 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
12153 {
12154 s_ignore (0);
12155 return;
12156 }
12157
12158 mips_cprestore_offset = get_absolute_expression ();
7a621144 12159 mips_cprestore_valid = 1;
252b5132
RH
12160
12161 ex.X_op = O_constant;
12162 ex.X_add_symbol = NULL;
12163 ex.X_op_symbol = NULL;
12164 ex.X_add_number = mips_cprestore_offset;
12165
584892a6 12166 macro_start ();
67c0d1eb
RS
12167 macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
12168 SP, HAVE_64BIT_ADDRESSES);
584892a6 12169 macro_end ();
252b5132
RH
12170
12171 demand_empty_rest_of_line ();
12172}
12173
6478892d 12174/* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
67c1ffbe 12175 was given in the preceding .cpsetup, it results in:
6478892d 12176 ld $gp, offset($sp)
76b3015f 12177
6478892d 12178 If a register $reg2 was given there, it results in:
609f23f4 12179 daddu $gp, $reg2, $0
6478892d
TS
12180 */
12181static void
17a2f251 12182s_cpreturn (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
12183{
12184 expressionS ex;
6478892d
TS
12185
12186 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
12187 We also need NewABI support. */
12188 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12189 {
12190 s_ignore (0);
12191 return;
12192 }
12193
584892a6 12194 macro_start ();
6478892d
TS
12195 if (mips_cpreturn_register == -1)
12196 {
12197 ex.X_op = O_constant;
12198 ex.X_add_symbol = NULL;
12199 ex.X_op_symbol = NULL;
12200 ex.X_add_number = mips_cpreturn_offset;
12201
67c0d1eb 12202 macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
6478892d
TS
12203 }
12204 else
67c0d1eb 12205 macro_build (NULL, "daddu", "d,v,t", mips_gp_register,
17a2f251 12206 mips_cpreturn_register, 0);
584892a6 12207 macro_end ();
6478892d
TS
12208
12209 demand_empty_rest_of_line ();
12210}
12211
12212/* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
12213 code. It sets the offset to use in gp_rel relocations. */
12214
12215static void
17a2f251 12216s_gpvalue (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
12217{
12218 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
12219 We also need NewABI support. */
12220 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12221 {
12222 s_ignore (0);
12223 return;
12224 }
12225
def2e0dd 12226 mips_gprel_offset = get_absolute_expression ();
6478892d
TS
12227
12228 demand_empty_rest_of_line ();
12229}
12230
252b5132
RH
12231/* Handle the .gpword pseudo-op. This is used when generating PIC
12232 code. It generates a 32 bit GP relative reloc. */
12233
12234static void
17a2f251 12235s_gpword (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
12236{
12237 symbolS *label;
12238 expressionS ex;
12239 char *p;
12240
12241 /* When not generating PIC code, this is treated as .word. */
12242 if (mips_pic != SVR4_PIC)
12243 {
12244 s_cons (2);
12245 return;
12246 }
12247
12248 label = insn_labels != NULL ? insn_labels->label : NULL;
b34976b6 12249 mips_emit_delays (TRUE);
252b5132
RH
12250 if (auto_align)
12251 mips_align (2, 0, label);
12252 mips_clear_insn_labels ();
12253
12254 expression (&ex);
12255
12256 if (ex.X_op != O_symbol || ex.X_add_number != 0)
12257 {
12258 as_bad (_("Unsupported use of .gpword"));
12259 ignore_rest_of_line ();
12260 }
12261
12262 p = frag_more (4);
17a2f251 12263 md_number_to_chars (p, 0, 4);
b34976b6 12264 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
cdf6fd85 12265 BFD_RELOC_GPREL32);
252b5132
RH
12266
12267 demand_empty_rest_of_line ();
12268}
12269
10181a0d 12270static void
17a2f251 12271s_gpdword (int ignore ATTRIBUTE_UNUSED)
10181a0d
AO
12272{
12273 symbolS *label;
12274 expressionS ex;
12275 char *p;
12276
12277 /* When not generating PIC code, this is treated as .dword. */
12278 if (mips_pic != SVR4_PIC)
12279 {
12280 s_cons (3);
12281 return;
12282 }
12283
12284 label = insn_labels != NULL ? insn_labels->label : NULL;
b34976b6 12285 mips_emit_delays (TRUE);
10181a0d
AO
12286 if (auto_align)
12287 mips_align (3, 0, label);
12288 mips_clear_insn_labels ();
12289
12290 expression (&ex);
12291
12292 if (ex.X_op != O_symbol || ex.X_add_number != 0)
12293 {
12294 as_bad (_("Unsupported use of .gpdword"));
12295 ignore_rest_of_line ();
12296 }
12297
12298 p = frag_more (8);
17a2f251 12299 md_number_to_chars (p, 0, 8);
a105a300 12300 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
10181a0d
AO
12301 BFD_RELOC_GPREL32);
12302
12303 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
12304 ex.X_op = O_absent;
12305 ex.X_add_symbol = 0;
12306 ex.X_add_number = 0;
b34976b6 12307 fix_new_exp (frag_now, p - frag_now->fr_literal, 8, &ex, FALSE,
10181a0d
AO
12308 BFD_RELOC_64);
12309
12310 demand_empty_rest_of_line ();
12311}
12312
252b5132
RH
12313/* Handle the .cpadd pseudo-op. This is used when dealing with switch
12314 tables in SVR4 PIC code. */
12315
12316static void
17a2f251 12317s_cpadd (int ignore ATTRIBUTE_UNUSED)
252b5132 12318{
252b5132
RH
12319 int reg;
12320
10181a0d
AO
12321 /* This is ignored when not generating SVR4 PIC code. */
12322 if (mips_pic != SVR4_PIC)
252b5132
RH
12323 {
12324 s_ignore (0);
12325 return;
12326 }
12327
12328 /* Add $gp to the register named as an argument. */
584892a6 12329 macro_start ();
252b5132 12330 reg = tc_get_register (0);
67c0d1eb 12331 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
584892a6 12332 macro_end ();
252b5132 12333
bdaaa2e1 12334 demand_empty_rest_of_line ();
252b5132
RH
12335}
12336
12337/* Handle the .insn pseudo-op. This marks instruction labels in
12338 mips16 mode. This permits the linker to handle them specially,
12339 such as generating jalx instructions when needed. We also make
12340 them odd for the duration of the assembly, in order to generate the
12341 right sort of code. We will make them even in the adjust_symtab
12342 routine, while leaving them marked. This is convenient for the
12343 debugger and the disassembler. The linker knows to make them odd
12344 again. */
12345
12346static void
17a2f251 12347s_insn (int ignore ATTRIBUTE_UNUSED)
252b5132 12348{
f9419b05 12349 mips16_mark_labels ();
252b5132
RH
12350
12351 demand_empty_rest_of_line ();
12352}
12353
12354/* Handle a .stabn directive. We need these in order to mark a label
12355 as being a mips16 text label correctly. Sometimes the compiler
12356 will emit a label, followed by a .stabn, and then switch sections.
12357 If the label and .stabn are in mips16 mode, then the label is
12358 really a mips16 text label. */
12359
12360static void
17a2f251 12361s_mips_stab (int type)
252b5132 12362{
f9419b05 12363 if (type == 'n')
252b5132
RH
12364 mips16_mark_labels ();
12365
12366 s_stab (type);
12367}
12368
12369/* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
12370 */
12371
12372static void
17a2f251 12373s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
12374{
12375 char *name;
12376 int c;
12377 symbolS *symbolP;
12378 expressionS exp;
12379
12380 name = input_line_pointer;
12381 c = get_symbol_end ();
12382 symbolP = symbol_find_or_make (name);
12383 S_SET_WEAK (symbolP);
12384 *input_line_pointer = c;
12385
12386 SKIP_WHITESPACE ();
12387
12388 if (! is_end_of_line[(unsigned char) *input_line_pointer])
12389 {
12390 if (S_IS_DEFINED (symbolP))
12391 {
956cd1d6 12392 as_bad ("ignoring attempt to redefine symbol %s",
252b5132
RH
12393 S_GET_NAME (symbolP));
12394 ignore_rest_of_line ();
12395 return;
12396 }
bdaaa2e1 12397
252b5132
RH
12398 if (*input_line_pointer == ',')
12399 {
12400 ++input_line_pointer;
12401 SKIP_WHITESPACE ();
12402 }
bdaaa2e1 12403
252b5132
RH
12404 expression (&exp);
12405 if (exp.X_op != O_symbol)
12406 {
12407 as_bad ("bad .weakext directive");
98d3f06f 12408 ignore_rest_of_line ();
252b5132
RH
12409 return;
12410 }
49309057 12411 symbol_set_value_expression (symbolP, &exp);
252b5132
RH
12412 }
12413
12414 demand_empty_rest_of_line ();
12415}
12416
12417/* Parse a register string into a number. Called from the ECOFF code
12418 to parse .frame. The argument is non-zero if this is the frame
12419 register, so that we can record it in mips_frame_reg. */
12420
12421int
17a2f251 12422tc_get_register (int frame)
252b5132
RH
12423{
12424 int reg;
12425
12426 SKIP_WHITESPACE ();
12427 if (*input_line_pointer++ != '$')
12428 {
12429 as_warn (_("expected `$'"));
85b51719 12430 reg = ZERO;
252b5132 12431 }
3882b010 12432 else if (ISDIGIT (*input_line_pointer))
252b5132
RH
12433 {
12434 reg = get_absolute_expression ();
12435 if (reg < 0 || reg >= 32)
12436 {
12437 as_warn (_("Bad register number"));
85b51719 12438 reg = ZERO;
252b5132
RH
12439 }
12440 }
12441 else
12442 {
76db943d 12443 if (strncmp (input_line_pointer, "ra", 2) == 0)
85b51719
TS
12444 {
12445 reg = RA;
12446 input_line_pointer += 2;
12447 }
76db943d 12448 else if (strncmp (input_line_pointer, "fp", 2) == 0)
85b51719
TS
12449 {
12450 reg = FP;
12451 input_line_pointer += 2;
12452 }
252b5132 12453 else if (strncmp (input_line_pointer, "sp", 2) == 0)
85b51719
TS
12454 {
12455 reg = SP;
12456 input_line_pointer += 2;
12457 }
252b5132 12458 else if (strncmp (input_line_pointer, "gp", 2) == 0)
85b51719
TS
12459 {
12460 reg = GP;
12461 input_line_pointer += 2;
12462 }
252b5132 12463 else if (strncmp (input_line_pointer, "at", 2) == 0)
85b51719
TS
12464 {
12465 reg = AT;
12466 input_line_pointer += 2;
12467 }
12468 else if (strncmp (input_line_pointer, "kt0", 3) == 0)
12469 {
12470 reg = KT0;
12471 input_line_pointer += 3;
12472 }
12473 else if (strncmp (input_line_pointer, "kt1", 3) == 0)
12474 {
12475 reg = KT1;
12476 input_line_pointer += 3;
12477 }
12478 else if (strncmp (input_line_pointer, "zero", 4) == 0)
12479 {
12480 reg = ZERO;
12481 input_line_pointer += 4;
12482 }
252b5132
RH
12483 else
12484 {
12485 as_warn (_("Unrecognized register name"));
85b51719
TS
12486 reg = ZERO;
12487 while (ISALNUM(*input_line_pointer))
12488 input_line_pointer++;
252b5132 12489 }
252b5132
RH
12490 }
12491 if (frame)
7a621144
DJ
12492 {
12493 mips_frame_reg = reg != 0 ? reg : SP;
12494 mips_frame_reg_valid = 1;
12495 mips_cprestore_valid = 0;
12496 }
252b5132
RH
12497 return reg;
12498}
12499
12500valueT
17a2f251 12501md_section_align (asection *seg, valueT addr)
252b5132
RH
12502{
12503 int align = bfd_get_section_alignment (stdoutput, seg);
12504
12505#ifdef OBJ_ELF
12506 /* We don't need to align ELF sections to the full alignment.
12507 However, Irix 5 may prefer that we align them at least to a 16
12508 byte boundary. We don't bother to align the sections if we are
12509 targeted for an embedded system. */
12510 if (strcmp (TARGET_OS, "elf") == 0)
12511 return addr;
12512 if (align > 4)
12513 align = 4;
12514#endif
12515
12516 return ((addr + (1 << align) - 1) & (-1 << align));
12517}
12518
12519/* Utility routine, called from above as well. If called while the
12520 input file is still being read, it's only an approximation. (For
12521 example, a symbol may later become defined which appeared to be
12522 undefined earlier.) */
12523
12524static int
17a2f251 12525nopic_need_relax (symbolS *sym, int before_relaxing)
252b5132
RH
12526{
12527 if (sym == 0)
12528 return 0;
12529
4d0d148d 12530 if (g_switch_value > 0)
252b5132
RH
12531 {
12532 const char *symname;
12533 int change;
12534
c9914766 12535 /* Find out whether this symbol can be referenced off the $gp
252b5132
RH
12536 register. It can be if it is smaller than the -G size or if
12537 it is in the .sdata or .sbss section. Certain symbols can
c9914766 12538 not be referenced off the $gp, although it appears as though
252b5132
RH
12539 they can. */
12540 symname = S_GET_NAME (sym);
12541 if (symname != (const char *) NULL
12542 && (strcmp (symname, "eprol") == 0
12543 || strcmp (symname, "etext") == 0
12544 || strcmp (symname, "_gp") == 0
12545 || strcmp (symname, "edata") == 0
12546 || strcmp (symname, "_fbss") == 0
12547 || strcmp (symname, "_fdata") == 0
12548 || strcmp (symname, "_ftext") == 0
12549 || strcmp (symname, "end") == 0
12550 || strcmp (symname, "_gp_disp") == 0))
12551 change = 1;
12552 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
12553 && (0
12554#ifndef NO_ECOFF_DEBUGGING
49309057
ILT
12555 || (symbol_get_obj (sym)->ecoff_extern_size != 0
12556 && (symbol_get_obj (sym)->ecoff_extern_size
12557 <= g_switch_value))
252b5132
RH
12558#endif
12559 /* We must defer this decision until after the whole
12560 file has been read, since there might be a .extern
12561 after the first use of this symbol. */
12562 || (before_relaxing
12563#ifndef NO_ECOFF_DEBUGGING
49309057 12564 && symbol_get_obj (sym)->ecoff_extern_size == 0
252b5132
RH
12565#endif
12566 && S_GET_VALUE (sym) == 0)
12567 || (S_GET_VALUE (sym) != 0
12568 && S_GET_VALUE (sym) <= g_switch_value)))
12569 change = 0;
12570 else
12571 {
12572 const char *segname;
12573
12574 segname = segment_name (S_GET_SEGMENT (sym));
12575 assert (strcmp (segname, ".lit8") != 0
12576 && strcmp (segname, ".lit4") != 0);
12577 change = (strcmp (segname, ".sdata") != 0
fba2b7f9
GK
12578 && strcmp (segname, ".sbss") != 0
12579 && strncmp (segname, ".sdata.", 7) != 0
12580 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
252b5132
RH
12581 }
12582 return change;
12583 }
12584 else
c9914766 12585 /* We are not optimizing for the $gp register. */
252b5132
RH
12586 return 1;
12587}
12588
5919d012
RS
12589
12590/* Return true if the given symbol should be considered local for SVR4 PIC. */
12591
12592static bfd_boolean
17a2f251 12593pic_need_relax (symbolS *sym, asection *segtype)
5919d012
RS
12594{
12595 asection *symsec;
12596 bfd_boolean linkonce;
12597
12598 /* Handle the case of a symbol equated to another symbol. */
12599 while (symbol_equated_reloc_p (sym))
12600 {
12601 symbolS *n;
12602
12603 /* It's possible to get a loop here in a badly written
12604 program. */
12605 n = symbol_get_value_expression (sym)->X_add_symbol;
12606 if (n == sym)
12607 break;
12608 sym = n;
12609 }
12610
12611 symsec = S_GET_SEGMENT (sym);
12612
12613 /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
12614 linkonce = FALSE;
12615 if (symsec != segtype && ! S_IS_LOCAL (sym))
12616 {
12617 if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
12618 != 0)
12619 linkonce = TRUE;
12620
12621 /* The GNU toolchain uses an extension for ELF: a section
12622 beginning with the magic string .gnu.linkonce is a linkonce
12623 section. */
12624 if (strncmp (segment_name (symsec), ".gnu.linkonce",
12625 sizeof ".gnu.linkonce" - 1) == 0)
12626 linkonce = TRUE;
12627 }
12628
12629 /* This must duplicate the test in adjust_reloc_syms. */
12630 return (symsec != &bfd_und_section
12631 && symsec != &bfd_abs_section
12632 && ! bfd_is_com_section (symsec)
12633 && !linkonce
12634#ifdef OBJ_ELF
12635 /* A global or weak symbol is treated as external. */
12636 && (OUTPUT_FLAVOR != bfd_target_elf_flavour
12637 || (! S_IS_WEAK (sym)
12638 && (! S_IS_EXTERNAL (sym)
12639 || mips_pic == EMBEDDED_PIC)))
12640#endif
12641 );
12642}
12643
12644
252b5132
RH
12645/* Given a mips16 variant frag FRAGP, return non-zero if it needs an
12646 extended opcode. SEC is the section the frag is in. */
12647
12648static int
17a2f251 12649mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
252b5132
RH
12650{
12651 int type;
12652 register const struct mips16_immed_operand *op;
12653 offsetT val;
12654 int mintiny, maxtiny;
12655 segT symsec;
98aa84af 12656 fragS *sym_frag;
252b5132
RH
12657
12658 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
12659 return 0;
12660 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
12661 return 1;
12662
12663 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12664 op = mips16_immed_operands;
12665 while (op->type != type)
12666 {
12667 ++op;
12668 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
12669 }
12670
12671 if (op->unsp)
12672 {
12673 if (type == '<' || type == '>' || type == '[' || type == ']')
12674 {
12675 mintiny = 1;
12676 maxtiny = 1 << op->nbits;
12677 }
12678 else
12679 {
12680 mintiny = 0;
12681 maxtiny = (1 << op->nbits) - 1;
12682 }
12683 }
12684 else
12685 {
12686 mintiny = - (1 << (op->nbits - 1));
12687 maxtiny = (1 << (op->nbits - 1)) - 1;
12688 }
12689
98aa84af 12690 sym_frag = symbol_get_frag (fragp->fr_symbol);
ac62c346 12691 val = S_GET_VALUE (fragp->fr_symbol);
98aa84af 12692 symsec = S_GET_SEGMENT (fragp->fr_symbol);
252b5132
RH
12693
12694 if (op->pcrel)
12695 {
12696 addressT addr;
12697
12698 /* We won't have the section when we are called from
12699 mips_relax_frag. However, we will always have been called
12700 from md_estimate_size_before_relax first. If this is a
12701 branch to a different section, we mark it as such. If SEC is
12702 NULL, and the frag is not marked, then it must be a branch to
12703 the same section. */
12704 if (sec == NULL)
12705 {
12706 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
12707 return 1;
12708 }
12709 else
12710 {
98aa84af 12711 /* Must have been called from md_estimate_size_before_relax. */
252b5132
RH
12712 if (symsec != sec)
12713 {
12714 fragp->fr_subtype =
12715 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12716
12717 /* FIXME: We should support this, and let the linker
12718 catch branches and loads that are out of range. */
12719 as_bad_where (fragp->fr_file, fragp->fr_line,
12720 _("unsupported PC relative reference to different section"));
12721
12722 return 1;
12723 }
98aa84af
AM
12724 if (fragp != sym_frag && sym_frag->fr_address == 0)
12725 /* Assume non-extended on the first relaxation pass.
12726 The address we have calculated will be bogus if this is
12727 a forward branch to another frag, as the forward frag
12728 will have fr_address == 0. */
12729 return 0;
252b5132
RH
12730 }
12731
12732 /* In this case, we know for sure that the symbol fragment is in
98aa84af
AM
12733 the same section. If the relax_marker of the symbol fragment
12734 differs from the relax_marker of this fragment, we have not
12735 yet adjusted the symbol fragment fr_address. We want to add
252b5132
RH
12736 in STRETCH in order to get a better estimate of the address.
12737 This particularly matters because of the shift bits. */
12738 if (stretch != 0
98aa84af 12739 && sym_frag->relax_marker != fragp->relax_marker)
252b5132
RH
12740 {
12741 fragS *f;
12742
12743 /* Adjust stretch for any alignment frag. Note that if have
12744 been expanding the earlier code, the symbol may be
12745 defined in what appears to be an earlier frag. FIXME:
12746 This doesn't handle the fr_subtype field, which specifies
12747 a maximum number of bytes to skip when doing an
12748 alignment. */
98aa84af 12749 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
252b5132
RH
12750 {
12751 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
12752 {
12753 if (stretch < 0)
12754 stretch = - ((- stretch)
12755 & ~ ((1 << (int) f->fr_offset) - 1));
12756 else
12757 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
12758 if (stretch == 0)
12759 break;
12760 }
12761 }
12762 if (f != NULL)
12763 val += stretch;
12764 }
12765
12766 addr = fragp->fr_address + fragp->fr_fix;
12767
12768 /* The base address rules are complicated. The base address of
12769 a branch is the following instruction. The base address of a
12770 PC relative load or add is the instruction itself, but if it
12771 is in a delay slot (in which case it can not be extended) use
12772 the address of the instruction whose delay slot it is in. */
12773 if (type == 'p' || type == 'q')
12774 {
12775 addr += 2;
12776
12777 /* If we are currently assuming that this frag should be
12778 extended, then, the current address is two bytes
bdaaa2e1 12779 higher. */
252b5132
RH
12780 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12781 addr += 2;
12782
12783 /* Ignore the low bit in the target, since it will be set
12784 for a text label. */
12785 if ((val & 1) != 0)
12786 --val;
12787 }
12788 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12789 addr -= 4;
12790 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12791 addr -= 2;
12792
12793 val -= addr & ~ ((1 << op->shift) - 1);
12794
12795 /* Branch offsets have an implicit 0 in the lowest bit. */
12796 if (type == 'p' || type == 'q')
12797 val /= 2;
12798
12799 /* If any of the shifted bits are set, we must use an extended
12800 opcode. If the address depends on the size of this
12801 instruction, this can lead to a loop, so we arrange to always
12802 use an extended opcode. We only check this when we are in
12803 the main relaxation loop, when SEC is NULL. */
12804 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
12805 {
12806 fragp->fr_subtype =
12807 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12808 return 1;
12809 }
12810
12811 /* If we are about to mark a frag as extended because the value
12812 is precisely maxtiny + 1, then there is a chance of an
12813 infinite loop as in the following code:
12814 la $4,foo
12815 .skip 1020
12816 .align 2
12817 foo:
12818 In this case when the la is extended, foo is 0x3fc bytes
12819 away, so the la can be shrunk, but then foo is 0x400 away, so
12820 the la must be extended. To avoid this loop, we mark the
12821 frag as extended if it was small, and is about to become
12822 extended with a value of maxtiny + 1. */
12823 if (val == ((maxtiny + 1) << op->shift)
12824 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
12825 && sec == NULL)
12826 {
12827 fragp->fr_subtype =
12828 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12829 return 1;
12830 }
12831 }
12832 else if (symsec != absolute_section && sec != NULL)
12833 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
12834
12835 if ((val & ((1 << op->shift) - 1)) != 0
12836 || val < (mintiny << op->shift)
12837 || val > (maxtiny << op->shift))
12838 return 1;
12839 else
12840 return 0;
12841}
12842
4a6a3df4
AO
12843/* Compute the length of a branch sequence, and adjust the
12844 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
12845 worst-case length is computed, with UPDATE being used to indicate
12846 whether an unconditional (-1), branch-likely (+1) or regular (0)
12847 branch is to be computed. */
12848static int
17a2f251 12849relaxed_branch_length (fragS *fragp, asection *sec, int update)
4a6a3df4 12850{
b34976b6 12851 bfd_boolean toofar;
4a6a3df4
AO
12852 int length;
12853
12854 if (fragp
12855 && S_IS_DEFINED (fragp->fr_symbol)
12856 && sec == S_GET_SEGMENT (fragp->fr_symbol))
12857 {
12858 addressT addr;
12859 offsetT val;
12860
12861 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
12862
12863 addr = fragp->fr_address + fragp->fr_fix + 4;
12864
12865 val -= addr;
12866
12867 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
12868 }
12869 else if (fragp)
12870 /* If the symbol is not defined or it's in a different segment,
12871 assume the user knows what's going on and emit a short
12872 branch. */
b34976b6 12873 toofar = FALSE;
4a6a3df4 12874 else
b34976b6 12875 toofar = TRUE;
4a6a3df4
AO
12876
12877 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
12878 fragp->fr_subtype
af6ae2ad 12879 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_UNCOND (fragp->fr_subtype),
4a6a3df4
AO
12880 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
12881 RELAX_BRANCH_LINK (fragp->fr_subtype),
12882 toofar);
12883
12884 length = 4;
12885 if (toofar)
12886 {
12887 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
12888 length += 8;
12889
12890 if (mips_pic != NO_PIC)
12891 {
12892 /* Additional space for PIC loading of target address. */
12893 length += 8;
12894 if (mips_opts.isa == ISA_MIPS1)
12895 /* Additional space for $at-stabilizing nop. */
12896 length += 4;
12897 }
12898
12899 /* If branch is conditional. */
12900 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
12901 length += 8;
12902 }
b34976b6 12903
4a6a3df4
AO
12904 return length;
12905}
12906
252b5132
RH
12907/* Estimate the size of a frag before relaxing. Unless this is the
12908 mips16, we are not really relaxing here, and the final size is
12909 encoded in the subtype information. For the mips16, we have to
12910 decide whether we are using an extended opcode or not. */
12911
252b5132 12912int
17a2f251 12913md_estimate_size_before_relax (fragS *fragp, asection *segtype)
252b5132 12914{
5919d012 12915 int change;
252b5132 12916
4a6a3df4
AO
12917 if (RELAX_BRANCH_P (fragp->fr_subtype))
12918 {
12919
b34976b6
AM
12920 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
12921
4a6a3df4
AO
12922 return fragp->fr_var;
12923 }
12924
252b5132 12925 if (RELAX_MIPS16_P (fragp->fr_subtype))
177b4a6a
AO
12926 /* We don't want to modify the EXTENDED bit here; it might get us
12927 into infinite loops. We change it only in mips_relax_frag(). */
12928 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
252b5132
RH
12929
12930 if (mips_pic == NO_PIC)
5919d012 12931 change = nopic_need_relax (fragp->fr_symbol, 0);
252b5132 12932 else if (mips_pic == SVR4_PIC)
5919d012 12933 change = pic_need_relax (fragp->fr_symbol, segtype);
252b5132
RH
12934 else
12935 abort ();
12936
12937 if (change)
12938 {
4d7206a2 12939 fragp->fr_subtype |= RELAX_USE_SECOND;
4d7206a2 12940 return -RELAX_FIRST (fragp->fr_subtype);
252b5132 12941 }
4d7206a2
RS
12942 else
12943 return -RELAX_SECOND (fragp->fr_subtype);
252b5132
RH
12944}
12945
12946/* This is called to see whether a reloc against a defined symbol
12947 should be converted into a reloc against a section. Don't adjust
12948 MIPS16 jump relocations, so we don't have to worry about the format
12949 of the offset in the .o file. Don't adjust relocations against
12950 mips16 symbols, so that the linker can find them if it needs to set
12951 up a stub. */
12952
12953int
17a2f251 12954mips_fix_adjustable (fixS *fixp)
252b5132
RH
12955{
12956 if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
12957 return 0;
a161fe53 12958
252b5132
RH
12959 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
12960 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12961 return 0;
a161fe53 12962
252b5132
RH
12963 if (fixp->fx_addsy == NULL)
12964 return 1;
a161fe53 12965
252b5132
RH
12966#ifdef OBJ_ELF
12967 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
12968 && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
12969 && fixp->fx_subsy == NULL)
12970 return 0;
12971#endif
a161fe53 12972
252b5132
RH
12973 return 1;
12974}
12975
12976/* Translate internal representation of relocation info to BFD target
12977 format. */
12978
12979arelent **
17a2f251 12980tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
12981{
12982 static arelent *retval[4];
12983 arelent *reloc;
12984 bfd_reloc_code_real_type code;
12985
4b0cff4e
TS
12986 memset (retval, 0, sizeof(retval));
12987 reloc = retval[0] = (arelent *) xcalloc (1, sizeof (arelent));
49309057
ILT
12988 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
12989 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
12990 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
12991
12992 if (mips_pic == EMBEDDED_PIC
12993 && SWITCH_TABLE (fixp))
12994 {
12995 /* For a switch table entry we use a special reloc. The addend
12996 is actually the difference between the reloc address and the
12997 subtrahend. */
12998 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
12999 if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
13000 as_fatal (_("Double check fx_r_type in tc-mips.c:tc_gen_reloc"));
13001 fixp->fx_r_type = BFD_RELOC_GPREL32;
13002 }
30ac9238 13003 else if (fixp->fx_pcrel)
252b5132 13004 {
30ac9238
RS
13005 bfd_vma pcrel_address;
13006
13007 /* Set PCREL_ADDRESS to this relocation's "PC". The PC for high
13008 high-part relocs is the address of the low-part reloc. */
13009 if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
13010 {
13011 assert (fixp->fx_next != NULL
13012 && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
13013 pcrel_address = (fixp->fx_next->fx_where
13014 + fixp->fx_next->fx_frag->fr_address);
13015 }
252b5132 13016 else
30ac9238
RS
13017 pcrel_address = reloc->address;
13018
13019 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
13020 {
13021 /* At this point, fx_addnumber is "symbol offset - pcrel_address".
13022 Relocations want only the symbol offset. */
13023 reloc->addend = fixp->fx_addnumber + pcrel_address;
13024 }
13025 else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16
13026 || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
4514d474 13027 {
30ac9238 13028 /* We use a special addend for an internal RELLO or RELHI reloc. */
4514d474 13029 if (symbol_section_p (fixp->fx_addsy))
30ac9238 13030 reloc->addend = pcrel_address - S_GET_VALUE (fixp->fx_subsy);
4514d474 13031 else
30ac9238 13032 reloc->addend = fixp->fx_addnumber + pcrel_address;
4514d474 13033 }
252b5132 13034 else
4514d474 13035 {
4d0d148d
TS
13036 /* A gruesome hack which is a result of the gruesome gas reloc
13037 handling. */
13038 reloc->addend = pcrel_address;
4514d474 13039 }
252b5132 13040 }
252b5132 13041 else
30ac9238 13042 reloc->addend = fixp->fx_addnumber;
252b5132 13043
438c16b8
TS
13044 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
13045 entry to be used in the relocation's section offset. */
13046 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
252b5132
RH
13047 {
13048 reloc->address = reloc->addend;
13049 reloc->addend = 0;
13050 }
13051
13052 /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
13053 fixup_segment converted a non-PC relative reloc into a PC
13054 relative reloc. In such a case, we need to convert the reloc
13055 code. */
13056 code = fixp->fx_r_type;
13057 if (fixp->fx_pcrel)
13058 {
13059 switch (code)
13060 {
13061 case BFD_RELOC_8:
13062 code = BFD_RELOC_8_PCREL;
13063 break;
13064 case BFD_RELOC_16:
13065 code = BFD_RELOC_16_PCREL;
13066 break;
13067 case BFD_RELOC_32:
13068 code = BFD_RELOC_32_PCREL;
13069 break;
13070 case BFD_RELOC_64:
13071 code = BFD_RELOC_64_PCREL;
13072 break;
13073 case BFD_RELOC_8_PCREL:
13074 case BFD_RELOC_16_PCREL:
13075 case BFD_RELOC_32_PCREL:
13076 case BFD_RELOC_64_PCREL:
13077 case BFD_RELOC_16_PCREL_S2:
13078 case BFD_RELOC_PCREL_HI16_S:
13079 case BFD_RELOC_PCREL_LO16:
13080 break;
13081 default:
13082 as_bad_where (fixp->fx_file, fixp->fx_line,
13083 _("Cannot make %s relocation PC relative"),
13084 bfd_get_reloc_code_name (code));
13085 }
13086 }
13087
0b25d3e6
AO
13088 /* To support a PC relative reloc when generating embedded PIC code
13089 for ECOFF, we use a Cygnus extension. We check for that here to
13090 make sure that we don't let such a reloc escape normally. */
13091 if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
13092 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
13093 && code == BFD_RELOC_16_PCREL_S2
13094 && mips_pic != EMBEDDED_PIC)
13095 reloc->howto = NULL;
13096 else
13097 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
13098
252b5132
RH
13099 if (reloc->howto == NULL)
13100 {
13101 as_bad_where (fixp->fx_file, fixp->fx_line,
13102 _("Can not represent %s relocation in this object file format"),
13103 bfd_get_reloc_code_name (code));
13104 retval[0] = NULL;
13105 }
13106
13107 return retval;
13108}
13109
13110/* Relax a machine dependent frag. This returns the amount by which
13111 the current size of the frag should change. */
13112
13113int
17a2f251 13114mips_relax_frag (asection *sec, fragS *fragp, long stretch)
252b5132 13115{
4a6a3df4
AO
13116 if (RELAX_BRANCH_P (fragp->fr_subtype))
13117 {
13118 offsetT old_var = fragp->fr_var;
b34976b6
AM
13119
13120 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
4a6a3df4
AO
13121
13122 return fragp->fr_var - old_var;
13123 }
13124
252b5132
RH
13125 if (! RELAX_MIPS16_P (fragp->fr_subtype))
13126 return 0;
13127
c4e7957c 13128 if (mips16_extended_frag (fragp, NULL, stretch))
252b5132
RH
13129 {
13130 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13131 return 0;
13132 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
13133 return 2;
13134 }
13135 else
13136 {
13137 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13138 return 0;
13139 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
13140 return -2;
13141 }
13142
13143 return 0;
13144}
13145
13146/* Convert a machine dependent frag. */
13147
13148void
17a2f251 13149md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
252b5132 13150{
4a6a3df4
AO
13151 if (RELAX_BRANCH_P (fragp->fr_subtype))
13152 {
13153 bfd_byte *buf;
13154 unsigned long insn;
13155 expressionS exp;
13156 fixS *fixp;
b34976b6 13157
4a6a3df4
AO
13158 buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
13159
13160 if (target_big_endian)
13161 insn = bfd_getb32 (buf);
13162 else
13163 insn = bfd_getl32 (buf);
b34976b6 13164
4a6a3df4
AO
13165 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13166 {
13167 /* We generate a fixup instead of applying it right now
13168 because, if there are linker relaxations, we're going to
13169 need the relocations. */
13170 exp.X_op = O_symbol;
13171 exp.X_add_symbol = fragp->fr_symbol;
13172 exp.X_add_number = fragp->fr_offset;
13173
13174 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
0b25d3e6
AO
13175 4, &exp, 1,
13176 BFD_RELOC_16_PCREL_S2);
4a6a3df4
AO
13177 fixp->fx_file = fragp->fr_file;
13178 fixp->fx_line = fragp->fr_line;
b34976b6 13179
17a2f251 13180 md_number_to_chars (buf, insn, 4);
4a6a3df4
AO
13181 buf += 4;
13182 }
13183 else
13184 {
13185 int i;
13186
13187 as_warn_where (fragp->fr_file, fragp->fr_line,
13188 _("relaxed out-of-range branch into a jump"));
13189
13190 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
13191 goto uncond;
13192
13193 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13194 {
13195 /* Reverse the branch. */
13196 switch ((insn >> 28) & 0xf)
13197 {
13198 case 4:
13199 /* bc[0-3][tf]l? and bc1any[24][ft] instructions can
13200 have the condition reversed by tweaking a single
13201 bit, and their opcodes all have 0x4???????. */
13202 assert ((insn & 0xf1000000) == 0x41000000);
13203 insn ^= 0x00010000;
13204 break;
13205
13206 case 0:
13207 /* bltz 0x04000000 bgez 0x04010000
13208 bltzal 0x04100000 bgezal 0x04110000 */
13209 assert ((insn & 0xfc0e0000) == 0x04000000);
13210 insn ^= 0x00010000;
13211 break;
b34976b6 13212
4a6a3df4
AO
13213 case 1:
13214 /* beq 0x10000000 bne 0x14000000
13215 blez 0x18000000 bgtz 0x1c000000 */
13216 insn ^= 0x04000000;
13217 break;
13218
13219 default:
13220 abort ();
13221 }
13222 }
13223
13224 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13225 {
13226 /* Clear the and-link bit. */
13227 assert ((insn & 0xfc1c0000) == 0x04100000);
13228
13229 /* bltzal 0x04100000 bgezal 0x04110000
13230 bltzall 0x04120000 bgezall 0x04130000 */
13231 insn &= ~0x00100000;
13232 }
13233
13234 /* Branch over the branch (if the branch was likely) or the
13235 full jump (not likely case). Compute the offset from the
13236 current instruction to branch to. */
13237 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13238 i = 16;
13239 else
13240 {
13241 /* How many bytes in instructions we've already emitted? */
13242 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13243 /* How many bytes in instructions from here to the end? */
13244 i = fragp->fr_var - i;
13245 }
13246 /* Convert to instruction count. */
13247 i >>= 2;
13248 /* Branch counts from the next instruction. */
b34976b6 13249 i--;
4a6a3df4
AO
13250 insn |= i;
13251 /* Branch over the jump. */
17a2f251 13252 md_number_to_chars (buf, insn, 4);
4a6a3df4
AO
13253 buf += 4;
13254
13255 /* Nop */
17a2f251 13256 md_number_to_chars (buf, 0, 4);
4a6a3df4
AO
13257 buf += 4;
13258
13259 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13260 {
13261 /* beql $0, $0, 2f */
13262 insn = 0x50000000;
13263 /* Compute the PC offset from the current instruction to
13264 the end of the variable frag. */
13265 /* How many bytes in instructions we've already emitted? */
13266 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13267 /* How many bytes in instructions from here to the end? */
13268 i = fragp->fr_var - i;
13269 /* Convert to instruction count. */
13270 i >>= 2;
13271 /* Don't decrement i, because we want to branch over the
13272 delay slot. */
13273
13274 insn |= i;
17a2f251 13275 md_number_to_chars (buf, insn, 4);
4a6a3df4
AO
13276 buf += 4;
13277
17a2f251 13278 md_number_to_chars (buf, 0, 4);
4a6a3df4
AO
13279 buf += 4;
13280 }
13281
13282 uncond:
13283 if (mips_pic == NO_PIC)
13284 {
13285 /* j or jal. */
13286 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
13287 ? 0x0c000000 : 0x08000000);
13288 exp.X_op = O_symbol;
13289 exp.X_add_symbol = fragp->fr_symbol;
13290 exp.X_add_number = fragp->fr_offset;
13291
13292 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13293 4, &exp, 0, BFD_RELOC_MIPS_JMP);
13294 fixp->fx_file = fragp->fr_file;
13295 fixp->fx_line = fragp->fr_line;
13296
17a2f251 13297 md_number_to_chars (buf, insn, 4);
4a6a3df4
AO
13298 buf += 4;
13299 }
13300 else
13301 {
13302 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
13303 insn = HAVE_64BIT_ADDRESSES ? 0xdf810000 : 0x8f810000;
13304 exp.X_op = O_symbol;
13305 exp.X_add_symbol = fragp->fr_symbol;
13306 exp.X_add_number = fragp->fr_offset;
13307
13308 if (fragp->fr_offset)
13309 {
13310 exp.X_add_symbol = make_expr_symbol (&exp);
13311 exp.X_add_number = 0;
13312 }
13313
13314 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13315 4, &exp, 0, BFD_RELOC_MIPS_GOT16);
13316 fixp->fx_file = fragp->fr_file;
13317 fixp->fx_line = fragp->fr_line;
13318
17a2f251 13319 md_number_to_chars (buf, insn, 4);
4a6a3df4 13320 buf += 4;
b34976b6 13321
4a6a3df4
AO
13322 if (mips_opts.isa == ISA_MIPS1)
13323 {
13324 /* nop */
17a2f251 13325 md_number_to_chars (buf, 0, 4);
4a6a3df4
AO
13326 buf += 4;
13327 }
13328
13329 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
13330 insn = HAVE_64BIT_ADDRESSES ? 0x64210000 : 0x24210000;
13331
13332 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13333 4, &exp, 0, BFD_RELOC_LO16);
13334 fixp->fx_file = fragp->fr_file;
13335 fixp->fx_line = fragp->fr_line;
b34976b6 13336
17a2f251 13337 md_number_to_chars (buf, insn, 4);
4a6a3df4
AO
13338 buf += 4;
13339
13340 /* j(al)r $at. */
13341 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13342 insn = 0x0020f809;
13343 else
13344 insn = 0x00200008;
13345
17a2f251 13346 md_number_to_chars (buf, insn, 4);
4a6a3df4
AO
13347 buf += 4;
13348 }
13349 }
13350
13351 assert (buf == (bfd_byte *)fragp->fr_literal
13352 + fragp->fr_fix + fragp->fr_var);
13353
13354 fragp->fr_fix += fragp->fr_var;
13355
13356 return;
13357 }
13358
252b5132
RH
13359 if (RELAX_MIPS16_P (fragp->fr_subtype))
13360 {
13361 int type;
13362 register const struct mips16_immed_operand *op;
b34976b6 13363 bfd_boolean small, ext;
252b5132
RH
13364 offsetT val;
13365 bfd_byte *buf;
13366 unsigned long insn;
b34976b6 13367 bfd_boolean use_extend;
252b5132
RH
13368 unsigned short extend;
13369
13370 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
13371 op = mips16_immed_operands;
13372 while (op->type != type)
13373 ++op;
13374
13375 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13376 {
b34976b6
AM
13377 small = FALSE;
13378 ext = TRUE;
252b5132
RH
13379 }
13380 else
13381 {
b34976b6
AM
13382 small = TRUE;
13383 ext = FALSE;
252b5132
RH
13384 }
13385
6386f3a7 13386 resolve_symbol_value (fragp->fr_symbol);
252b5132
RH
13387 val = S_GET_VALUE (fragp->fr_symbol);
13388 if (op->pcrel)
13389 {
13390 addressT addr;
13391
13392 addr = fragp->fr_address + fragp->fr_fix;
13393
13394 /* The rules for the base address of a PC relative reloc are
13395 complicated; see mips16_extended_frag. */
13396 if (type == 'p' || type == 'q')
13397 {
13398 addr += 2;
13399 if (ext)
13400 addr += 2;
13401 /* Ignore the low bit in the target, since it will be
13402 set for a text label. */
13403 if ((val & 1) != 0)
13404 --val;
13405 }
13406 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13407 addr -= 4;
13408 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13409 addr -= 2;
13410
13411 addr &= ~ (addressT) ((1 << op->shift) - 1);
13412 val -= addr;
13413
13414 /* Make sure the section winds up with the alignment we have
13415 assumed. */
13416 if (op->shift > 0)
13417 record_alignment (asec, op->shift);
13418 }
13419
13420 if (ext
13421 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
13422 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
13423 as_warn_where (fragp->fr_file, fragp->fr_line,
13424 _("extended instruction in delay slot"));
13425
13426 buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
13427
13428 if (target_big_endian)
13429 insn = bfd_getb16 (buf);
13430 else
13431 insn = bfd_getl16 (buf);
13432
13433 mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
13434 RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
13435 small, ext, &insn, &use_extend, &extend);
13436
13437 if (use_extend)
13438 {
17a2f251 13439 md_number_to_chars (buf, 0xf000 | extend, 2);
252b5132
RH
13440 fragp->fr_fix += 2;
13441 buf += 2;
13442 }
13443
17a2f251 13444 md_number_to_chars (buf, insn, 2);
252b5132
RH
13445 fragp->fr_fix += 2;
13446 buf += 2;
13447 }
13448 else
13449 {
4d7206a2
RS
13450 int first, second;
13451 fixS *fixp;
252b5132 13452
4d7206a2
RS
13453 first = RELAX_FIRST (fragp->fr_subtype);
13454 second = RELAX_SECOND (fragp->fr_subtype);
13455 fixp = (fixS *) fragp->fr_opcode;
252b5132 13456
584892a6
RS
13457 /* Possibly emit a warning if we've chosen the longer option. */
13458 if (((fragp->fr_subtype & RELAX_USE_SECOND) != 0)
13459 == ((fragp->fr_subtype & RELAX_SECOND_LONGER) != 0))
13460 {
13461 const char *msg = macro_warning (fragp->fr_subtype);
13462 if (msg != 0)
13463 as_warn_where (fragp->fr_file, fragp->fr_line, msg);
13464 }
13465
4d7206a2
RS
13466 /* Go through all the fixups for the first sequence. Disable them
13467 (by marking them as done) if we're going to use the second
13468 sequence instead. */
13469 while (fixp
13470 && fixp->fx_frag == fragp
13471 && fixp->fx_where < fragp->fr_fix - second)
13472 {
13473 if (fragp->fr_subtype & RELAX_USE_SECOND)
13474 fixp->fx_done = 1;
13475 fixp = fixp->fx_next;
13476 }
252b5132 13477
4d7206a2
RS
13478 /* Go through the fixups for the second sequence. Disable them if
13479 we're going to use the first sequence, otherwise adjust their
13480 addresses to account for the relaxation. */
13481 while (fixp && fixp->fx_frag == fragp)
13482 {
13483 if (fragp->fr_subtype & RELAX_USE_SECOND)
13484 fixp->fx_where -= first;
13485 else
13486 fixp->fx_done = 1;
13487 fixp = fixp->fx_next;
13488 }
13489
13490 /* Now modify the frag contents. */
13491 if (fragp->fr_subtype & RELAX_USE_SECOND)
13492 {
13493 char *start;
13494
13495 start = fragp->fr_literal + fragp->fr_fix - first - second;
13496 memmove (start, start + first, second);
13497 fragp->fr_fix -= first;
13498 }
13499 else
13500 fragp->fr_fix -= second;
252b5132
RH
13501 }
13502}
13503
13504#ifdef OBJ_ELF
13505
13506/* This function is called after the relocs have been generated.
13507 We've been storing mips16 text labels as odd. Here we convert them
13508 back to even for the convenience of the debugger. */
13509
13510void
17a2f251 13511mips_frob_file_after_relocs (void)
252b5132
RH
13512{
13513 asymbol **syms;
13514 unsigned int count, i;
13515
13516 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
13517 return;
13518
13519 syms = bfd_get_outsymbols (stdoutput);
13520 count = bfd_get_symcount (stdoutput);
13521 for (i = 0; i < count; i++, syms++)
13522 {
13523 if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
13524 && ((*syms)->value & 1) != 0)
13525 {
13526 (*syms)->value &= ~1;
13527 /* If the symbol has an odd size, it was probably computed
13528 incorrectly, so adjust that as well. */
13529 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
13530 ++elf_symbol (*syms)->internal_elf_sym.st_size;
13531 }
13532 }
13533}
13534
13535#endif
13536
13537/* This function is called whenever a label is defined. It is used
13538 when handling branch delays; if a branch has a label, we assume we
13539 can not move it. */
13540
13541void
17a2f251 13542mips_define_label (symbolS *sym)
252b5132
RH
13543{
13544 struct insn_label_list *l;
13545
13546 if (free_insn_labels == NULL)
13547 l = (struct insn_label_list *) xmalloc (sizeof *l);
13548 else
13549 {
13550 l = free_insn_labels;
13551 free_insn_labels = l->next;
13552 }
13553
13554 l->label = sym;
13555 l->next = insn_labels;
13556 insn_labels = l;
13557}
13558\f
13559#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13560
13561/* Some special processing for a MIPS ELF file. */
13562
13563void
17a2f251 13564mips_elf_final_processing (void)
252b5132
RH
13565{
13566 /* Write out the register information. */
316f5878 13567 if (mips_abi != N64_ABI)
252b5132
RH
13568 {
13569 Elf32_RegInfo s;
13570
13571 s.ri_gprmask = mips_gprmask;
13572 s.ri_cprmask[0] = mips_cprmask[0];
13573 s.ri_cprmask[1] = mips_cprmask[1];
13574 s.ri_cprmask[2] = mips_cprmask[2];
13575 s.ri_cprmask[3] = mips_cprmask[3];
13576 /* The gp_value field is set by the MIPS ELF backend. */
13577
13578 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
13579 ((Elf32_External_RegInfo *)
13580 mips_regmask_frag));
13581 }
13582 else
13583 {
13584 Elf64_Internal_RegInfo s;
13585
13586 s.ri_gprmask = mips_gprmask;
13587 s.ri_pad = 0;
13588 s.ri_cprmask[0] = mips_cprmask[0];
13589 s.ri_cprmask[1] = mips_cprmask[1];
13590 s.ri_cprmask[2] = mips_cprmask[2];
13591 s.ri_cprmask[3] = mips_cprmask[3];
13592 /* The gp_value field is set by the MIPS ELF backend. */
13593
13594 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
13595 ((Elf64_External_RegInfo *)
13596 mips_regmask_frag));
13597 }
13598
13599 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
13600 sort of BFD interface for this. */
13601 if (mips_any_noreorder)
13602 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
13603 if (mips_pic != NO_PIC)
143d77c5 13604 {
252b5132 13605 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
143d77c5
EC
13606 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
13607 }
13608 if (mips_abicalls)
13609 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
252b5132 13610
98d3f06f 13611 /* Set MIPS ELF flags for ASEs. */
a4672219
TS
13612 if (file_ase_mips16)
13613 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
1f25f5d3
CD
13614#if 0 /* XXX FIXME */
13615 if (file_ase_mips3d)
13616 elf_elfheader (stdoutput)->e_flags |= ???;
13617#endif
deec1734
CD
13618 if (file_ase_mdmx)
13619 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
1f25f5d3 13620
bdaaa2e1 13621 /* Set the MIPS ELF ABI flags. */
316f5878 13622 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
252b5132 13623 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
316f5878 13624 else if (mips_abi == O64_ABI)
252b5132 13625 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
316f5878 13626 else if (mips_abi == EABI_ABI)
252b5132 13627 {
316f5878 13628 if (!file_mips_gp32)
252b5132
RH
13629 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
13630 else
13631 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
13632 }
316f5878 13633 else if (mips_abi == N32_ABI)
be00bddd
TS
13634 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
13635
c9914766 13636 /* Nothing to do for N64_ABI. */
252b5132
RH
13637
13638 if (mips_32bitmode)
13639 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
13640}
13641
13642#endif /* OBJ_ELF || OBJ_MAYBE_ELF */
13643\f
beae10d5
KH
13644typedef struct proc {
13645 symbolS *isym;
13646 unsigned long reg_mask;
13647 unsigned long reg_offset;
13648 unsigned long fpreg_mask;
13649 unsigned long fpreg_offset;
13650 unsigned long frame_offset;
13651 unsigned long frame_reg;
13652 unsigned long pc_reg;
13653} procS;
252b5132
RH
13654
13655static procS cur_proc;
13656static procS *cur_proc_ptr;
13657static int numprocs;
13658
0a9ef439 13659/* Fill in an rs_align_code fragment. */
a19d8eb0 13660
0a9ef439 13661void
17a2f251 13662mips_handle_align (fragS *fragp)
a19d8eb0 13663{
0a9ef439
RH
13664 if (fragp->fr_type != rs_align_code)
13665 return;
13666
13667 if (mips_opts.mips16)
a19d8eb0
CP
13668 {
13669 static const unsigned char be_nop[] = { 0x65, 0x00 };
13670 static const unsigned char le_nop[] = { 0x00, 0x65 };
13671
0a9ef439
RH
13672 int bytes;
13673 char *p;
a19d8eb0 13674
0a9ef439
RH
13675 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
13676 p = fragp->fr_literal + fragp->fr_fix;
13677
13678 if (bytes & 1)
13679 {
13680 *p++ = 0;
f9419b05 13681 fragp->fr_fix++;
0a9ef439
RH
13682 }
13683
13684 memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
13685 fragp->fr_var = 2;
a19d8eb0
CP
13686 }
13687
0a9ef439 13688 /* For mips32, a nop is a zero, which we trivially get by doing nothing. */
a19d8eb0
CP
13689}
13690
252b5132 13691static void
17a2f251 13692md_obj_begin (void)
252b5132
RH
13693{
13694}
13695
13696static void
17a2f251 13697md_obj_end (void)
252b5132
RH
13698{
13699 /* check for premature end, nesting errors, etc */
13700 if (cur_proc_ptr)
9a41af64 13701 as_warn (_("missing .end at end of assembly"));
252b5132
RH
13702}
13703
13704static long
17a2f251 13705get_number (void)
252b5132
RH
13706{
13707 int negative = 0;
13708 long val = 0;
13709
13710 if (*input_line_pointer == '-')
13711 {
13712 ++input_line_pointer;
13713 negative = 1;
13714 }
3882b010 13715 if (!ISDIGIT (*input_line_pointer))
956cd1d6 13716 as_bad (_("expected simple number"));
252b5132
RH
13717 if (input_line_pointer[0] == '0')
13718 {
13719 if (input_line_pointer[1] == 'x')
13720 {
13721 input_line_pointer += 2;
3882b010 13722 while (ISXDIGIT (*input_line_pointer))
252b5132
RH
13723 {
13724 val <<= 4;
13725 val |= hex_value (*input_line_pointer++);
13726 }
13727 return negative ? -val : val;
13728 }
13729 else
13730 {
13731 ++input_line_pointer;
3882b010 13732 while (ISDIGIT (*input_line_pointer))
252b5132
RH
13733 {
13734 val <<= 3;
13735 val |= *input_line_pointer++ - '0';
13736 }
13737 return negative ? -val : val;
13738 }
13739 }
3882b010 13740 if (!ISDIGIT (*input_line_pointer))
252b5132
RH
13741 {
13742 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
13743 *input_line_pointer, *input_line_pointer);
956cd1d6 13744 as_warn (_("invalid number"));
252b5132
RH
13745 return -1;
13746 }
3882b010 13747 while (ISDIGIT (*input_line_pointer))
252b5132
RH
13748 {
13749 val *= 10;
13750 val += *input_line_pointer++ - '0';
13751 }
13752 return negative ? -val : val;
13753}
13754
13755/* The .file directive; just like the usual .file directive, but there
c5dd6aab
DJ
13756 is an initial number which is the ECOFF file index. In the non-ECOFF
13757 case .file implies DWARF-2. */
13758
13759static void
17a2f251 13760s_mips_file (int x ATTRIBUTE_UNUSED)
c5dd6aab 13761{
ecb4347a
DJ
13762 static int first_file_directive = 0;
13763
c5dd6aab
DJ
13764 if (ECOFF_DEBUGGING)
13765 {
13766 get_number ();
13767 s_app_file (0);
13768 }
13769 else
ecb4347a
DJ
13770 {
13771 char *filename;
13772
13773 filename = dwarf2_directive_file (0);
13774
13775 /* Versions of GCC up to 3.1 start files with a ".file"
13776 directive even for stabs output. Make sure that this
13777 ".file" is handled. Note that you need a version of GCC
13778 after 3.1 in order to support DWARF-2 on MIPS. */
13779 if (filename != NULL && ! first_file_directive)
13780 {
13781 (void) new_logical_line (filename, -1);
13782 s_app_file_string (filename);
13783 }
13784 first_file_directive = 1;
13785 }
c5dd6aab
DJ
13786}
13787
13788/* The .loc directive, implying DWARF-2. */
252b5132
RH
13789
13790static void
17a2f251 13791s_mips_loc (int x ATTRIBUTE_UNUSED)
252b5132 13792{
c5dd6aab
DJ
13793 if (!ECOFF_DEBUGGING)
13794 dwarf2_directive_loc (0);
252b5132
RH
13795}
13796
252b5132
RH
13797/* The .end directive. */
13798
13799static void
17a2f251 13800s_mips_end (int x ATTRIBUTE_UNUSED)
252b5132
RH
13801{
13802 symbolS *p;
252b5132 13803
7a621144
DJ
13804 /* Following functions need their own .frame and .cprestore directives. */
13805 mips_frame_reg_valid = 0;
13806 mips_cprestore_valid = 0;
13807
252b5132
RH
13808 if (!is_end_of_line[(unsigned char) *input_line_pointer])
13809 {
13810 p = get_symbol ();
13811 demand_empty_rest_of_line ();
13812 }
13813 else
13814 p = NULL;
13815
14949570 13816 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
13817 as_warn (_(".end not in text section"));
13818
13819 if (!cur_proc_ptr)
13820 {
13821 as_warn (_(".end directive without a preceding .ent directive."));
13822 demand_empty_rest_of_line ();
13823 return;
13824 }
13825
13826 if (p != NULL)
13827 {
13828 assert (S_GET_NAME (p));
13829 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->isym)))
13830 as_warn (_(".end symbol does not match .ent symbol."));
ecb4347a
DJ
13831
13832 if (debug_type == DEBUG_STABS)
13833 stabs_generate_asm_endfunc (S_GET_NAME (p),
13834 S_GET_NAME (p));
252b5132
RH
13835 }
13836 else
13837 as_warn (_(".end directive missing or unknown symbol"));
13838
ecb4347a
DJ
13839#ifdef OBJ_ELF
13840 /* Generate a .pdr section. */
dcd410fe
RO
13841 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING
13842 && mips_flag_pdr)
ecb4347a
DJ
13843 {
13844 segT saved_seg = now_seg;
13845 subsegT saved_subseg = now_subseg;
13846 valueT dot;
13847 expressionS exp;
13848 char *fragp;
252b5132 13849
ecb4347a 13850 dot = frag_now_fix ();
252b5132
RH
13851
13852#ifdef md_flush_pending_output
ecb4347a 13853 md_flush_pending_output ();
252b5132
RH
13854#endif
13855
ecb4347a
DJ
13856 assert (pdr_seg);
13857 subseg_set (pdr_seg, 0);
252b5132 13858
ecb4347a
DJ
13859 /* Write the symbol. */
13860 exp.X_op = O_symbol;
13861 exp.X_add_symbol = p;
13862 exp.X_add_number = 0;
13863 emit_expr (&exp, 4);
252b5132 13864
ecb4347a 13865 fragp = frag_more (7 * 4);
252b5132 13866
17a2f251
TS
13867 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
13868 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
13869 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
13870 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
13871 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
13872 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
13873 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
252b5132 13874
ecb4347a
DJ
13875 subseg_set (saved_seg, saved_subseg);
13876 }
13877#endif /* OBJ_ELF */
252b5132
RH
13878
13879 cur_proc_ptr = NULL;
13880}
13881
13882/* The .aent and .ent directives. */
13883
13884static void
17a2f251 13885s_mips_ent (int aent)
252b5132 13886{
252b5132 13887 symbolS *symbolP;
252b5132
RH
13888
13889 symbolP = get_symbol ();
13890 if (*input_line_pointer == ',')
f9419b05 13891 ++input_line_pointer;
252b5132 13892 SKIP_WHITESPACE ();
3882b010 13893 if (ISDIGIT (*input_line_pointer)
d9a62219 13894 || *input_line_pointer == '-')
874e8986 13895 get_number ();
252b5132 13896
14949570 13897 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
13898 as_warn (_(".ent or .aent not in text section."));
13899
13900 if (!aent && cur_proc_ptr)
9a41af64 13901 as_warn (_("missing .end"));
252b5132
RH
13902
13903 if (!aent)
13904 {
7a621144
DJ
13905 /* This function needs its own .frame and .cprestore directives. */
13906 mips_frame_reg_valid = 0;
13907 mips_cprestore_valid = 0;
13908
252b5132
RH
13909 cur_proc_ptr = &cur_proc;
13910 memset (cur_proc_ptr, '\0', sizeof (procS));
13911
13912 cur_proc_ptr->isym = symbolP;
13913
49309057 13914 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
252b5132 13915
f9419b05 13916 ++numprocs;
ecb4347a
DJ
13917
13918 if (debug_type == DEBUG_STABS)
13919 stabs_generate_asm_func (S_GET_NAME (symbolP),
13920 S_GET_NAME (symbolP));
252b5132
RH
13921 }
13922
13923 demand_empty_rest_of_line ();
13924}
13925
13926/* The .frame directive. If the mdebug section is present (IRIX 5 native)
bdaaa2e1 13927 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
252b5132 13928 s_mips_frame is used so that we can set the PDR information correctly.
bdaaa2e1 13929 We can't use the ecoff routines because they make reference to the ecoff
252b5132
RH
13930 symbol table (in the mdebug section). */
13931
13932static void
17a2f251 13933s_mips_frame (int ignore ATTRIBUTE_UNUSED)
252b5132 13934{
ecb4347a
DJ
13935#ifdef OBJ_ELF
13936 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
13937 {
13938 long val;
252b5132 13939
ecb4347a
DJ
13940 if (cur_proc_ptr == (procS *) NULL)
13941 {
13942 as_warn (_(".frame outside of .ent"));
13943 demand_empty_rest_of_line ();
13944 return;
13945 }
252b5132 13946
ecb4347a
DJ
13947 cur_proc_ptr->frame_reg = tc_get_register (1);
13948
13949 SKIP_WHITESPACE ();
13950 if (*input_line_pointer++ != ','
13951 || get_absolute_expression_and_terminator (&val) != ',')
13952 {
13953 as_warn (_("Bad .frame directive"));
13954 --input_line_pointer;
13955 demand_empty_rest_of_line ();
13956 return;
13957 }
252b5132 13958
ecb4347a
DJ
13959 cur_proc_ptr->frame_offset = val;
13960 cur_proc_ptr->pc_reg = tc_get_register (0);
252b5132 13961
252b5132 13962 demand_empty_rest_of_line ();
252b5132 13963 }
ecb4347a
DJ
13964 else
13965#endif /* OBJ_ELF */
13966 s_ignore (ignore);
252b5132
RH
13967}
13968
bdaaa2e1
KH
13969/* The .fmask and .mask directives. If the mdebug section is present
13970 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
252b5132 13971 embedded targets, s_mips_mask is used so that we can set the PDR
bdaaa2e1 13972 information correctly. We can't use the ecoff routines because they
252b5132
RH
13973 make reference to the ecoff symbol table (in the mdebug section). */
13974
13975static void
17a2f251 13976s_mips_mask (int reg_type)
252b5132 13977{
ecb4347a
DJ
13978#ifdef OBJ_ELF
13979 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
252b5132 13980 {
ecb4347a 13981 long mask, off;
252b5132 13982
ecb4347a
DJ
13983 if (cur_proc_ptr == (procS *) NULL)
13984 {
13985 as_warn (_(".mask/.fmask outside of .ent"));
13986 demand_empty_rest_of_line ();
13987 return;
13988 }
252b5132 13989
ecb4347a
DJ
13990 if (get_absolute_expression_and_terminator (&mask) != ',')
13991 {
13992 as_warn (_("Bad .mask/.fmask directive"));
13993 --input_line_pointer;
13994 demand_empty_rest_of_line ();
13995 return;
13996 }
252b5132 13997
ecb4347a
DJ
13998 off = get_absolute_expression ();
13999
14000 if (reg_type == 'F')
14001 {
14002 cur_proc_ptr->fpreg_mask = mask;
14003 cur_proc_ptr->fpreg_offset = off;
14004 }
14005 else
14006 {
14007 cur_proc_ptr->reg_mask = mask;
14008 cur_proc_ptr->reg_offset = off;
14009 }
14010
14011 demand_empty_rest_of_line ();
252b5132
RH
14012 }
14013 else
ecb4347a
DJ
14014#endif /* OBJ_ELF */
14015 s_ignore (reg_type);
252b5132
RH
14016}
14017
14018/* The .loc directive. */
14019
14020#if 0
14021static void
17a2f251 14022s_loc (int x)
252b5132
RH
14023{
14024 symbolS *symbolP;
14025 int lineno;
14026 int addroff;
14027
14028 assert (now_seg == text_section);
14029
14030 lineno = get_number ();
14031 addroff = frag_now_fix ();
14032
14033 symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
14034 S_SET_TYPE (symbolP, N_SLINE);
14035 S_SET_OTHER (symbolP, 0);
14036 S_SET_DESC (symbolP, lineno);
14037 symbolP->sy_segment = now_seg;
14038}
14039#endif
e7af610e 14040
316f5878
RS
14041/* A table describing all the processors gas knows about. Names are
14042 matched in the order listed.
e7af610e 14043
316f5878
RS
14044 To ease comparison, please keep this table in the same order as
14045 gcc's mips_cpu_info_table[]. */
e972090a
NC
14046static const struct mips_cpu_info mips_cpu_info_table[] =
14047{
316f5878
RS
14048 /* Entries for generic ISAs */
14049 { "mips1", 1, ISA_MIPS1, CPU_R3000 },
14050 { "mips2", 1, ISA_MIPS2, CPU_R6000 },
14051 { "mips3", 1, ISA_MIPS3, CPU_R4000 },
14052 { "mips4", 1, ISA_MIPS4, CPU_R8000 },
14053 { "mips5", 1, ISA_MIPS5, CPU_MIPS5 },
14054 { "mips32", 1, ISA_MIPS32, CPU_MIPS32 },
af7ee8bf 14055 { "mips32r2", 1, ISA_MIPS32R2, CPU_MIPS32R2 },
316f5878 14056 { "mips64", 1, ISA_MIPS64, CPU_MIPS64 },
5f74bc13 14057 { "mips64r2", 1, ISA_MIPS64R2, CPU_MIPS64R2 },
316f5878
RS
14058
14059 /* MIPS I */
14060 { "r3000", 0, ISA_MIPS1, CPU_R3000 },
14061 { "r2000", 0, ISA_MIPS1, CPU_R3000 },
14062 { "r3900", 0, ISA_MIPS1, CPU_R3900 },
14063
14064 /* MIPS II */
14065 { "r6000", 0, ISA_MIPS2, CPU_R6000 },
14066
14067 /* MIPS III */
14068 { "r4000", 0, ISA_MIPS3, CPU_R4000 },
14069 { "r4010", 0, ISA_MIPS2, CPU_R4010 },
14070 { "vr4100", 0, ISA_MIPS3, CPU_VR4100 },
14071 { "vr4111", 0, ISA_MIPS3, CPU_R4111 },
60b63b72
RS
14072 { "vr4120", 0, ISA_MIPS3, CPU_VR4120 },
14073 { "vr4130", 0, ISA_MIPS3, CPU_VR4120 },
14074 { "vr4181", 0, ISA_MIPS3, CPU_R4111 },
316f5878
RS
14075 { "vr4300", 0, ISA_MIPS3, CPU_R4300 },
14076 { "r4400", 0, ISA_MIPS3, CPU_R4400 },
14077 { "r4600", 0, ISA_MIPS3, CPU_R4600 },
14078 { "orion", 0, ISA_MIPS3, CPU_R4600 },
14079 { "r4650", 0, ISA_MIPS3, CPU_R4650 },
14080
14081 /* MIPS IV */
14082 { "r8000", 0, ISA_MIPS4, CPU_R8000 },
14083 { "r10000", 0, ISA_MIPS4, CPU_R10000 },
14084 { "r12000", 0, ISA_MIPS4, CPU_R12000 },
14085 { "vr5000", 0, ISA_MIPS4, CPU_R5000 },
60b63b72
RS
14086 { "vr5400", 0, ISA_MIPS4, CPU_VR5400 },
14087 { "vr5500", 0, ISA_MIPS4, CPU_VR5500 },
316f5878
RS
14088 { "rm5200", 0, ISA_MIPS4, CPU_R5000 },
14089 { "rm5230", 0, ISA_MIPS4, CPU_R5000 },
14090 { "rm5231", 0, ISA_MIPS4, CPU_R5000 },
14091 { "rm5261", 0, ISA_MIPS4, CPU_R5000 },
14092 { "rm5721", 0, ISA_MIPS4, CPU_R5000 },
5a7ea749
RS
14093 { "rm7000", 0, ISA_MIPS4, CPU_RM7000 },
14094 { "rm9000", 0, ISA_MIPS4, CPU_RM7000 },
316f5878
RS
14095
14096 /* MIPS 32 */
fef14a42 14097 { "4kc", 0, ISA_MIPS32, CPU_MIPS32 },
316f5878
RS
14098 { "4km", 0, ISA_MIPS32, CPU_MIPS32 },
14099 { "4kp", 0, ISA_MIPS32, CPU_MIPS32 },
e7af610e 14100
316f5878
RS
14101 /* MIPS 64 */
14102 { "5kc", 0, ISA_MIPS64, CPU_MIPS64 },
14103 { "20kc", 0, ISA_MIPS64, CPU_MIPS64 },
e7af610e 14104
c7a23324 14105 /* Broadcom SB-1 CPU core */
316f5878 14106 { "sb1", 0, ISA_MIPS64, CPU_SB1 },
e7af610e 14107
316f5878
RS
14108 /* End marker */
14109 { NULL, 0, 0, 0 }
14110};
e7af610e 14111
84ea6cf2 14112
316f5878
RS
14113/* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
14114 with a final "000" replaced by "k". Ignore case.
e7af610e 14115
316f5878 14116 Note: this function is shared between GCC and GAS. */
c6c98b38 14117
b34976b6 14118static bfd_boolean
17a2f251 14119mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
14120{
14121 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
14122 given++, canonical++;
14123
14124 return ((*given == 0 && *canonical == 0)
14125 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
14126}
14127
14128
14129/* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
14130 CPU name. We've traditionally allowed a lot of variation here.
14131
14132 Note: this function is shared between GCC and GAS. */
14133
b34976b6 14134static bfd_boolean
17a2f251 14135mips_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
14136{
14137 /* First see if the name matches exactly, or with a final "000"
14138 turned into "k". */
14139 if (mips_strict_matching_cpu_name_p (canonical, given))
b34976b6 14140 return TRUE;
316f5878
RS
14141
14142 /* If not, try comparing based on numerical designation alone.
14143 See if GIVEN is an unadorned number, or 'r' followed by a number. */
14144 if (TOLOWER (*given) == 'r')
14145 given++;
14146 if (!ISDIGIT (*given))
b34976b6 14147 return FALSE;
316f5878
RS
14148
14149 /* Skip over some well-known prefixes in the canonical name,
14150 hoping to find a number there too. */
14151 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
14152 canonical += 2;
14153 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
14154 canonical += 2;
14155 else if (TOLOWER (canonical[0]) == 'r')
14156 canonical += 1;
14157
14158 return mips_strict_matching_cpu_name_p (canonical, given);
14159}
14160
14161
14162/* Parse an option that takes the name of a processor as its argument.
14163 OPTION is the name of the option and CPU_STRING is the argument.
14164 Return the corresponding processor enumeration if the CPU_STRING is
14165 recognized, otherwise report an error and return null.
14166
14167 A similar function exists in GCC. */
e7af610e
NC
14168
14169static const struct mips_cpu_info *
17a2f251 14170mips_parse_cpu (const char *option, const char *cpu_string)
e7af610e 14171{
316f5878 14172 const struct mips_cpu_info *p;
e7af610e 14173
316f5878
RS
14174 /* 'from-abi' selects the most compatible architecture for the given
14175 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
14176 EABIs, we have to decide whether we're using the 32-bit or 64-bit
14177 version. Look first at the -mgp options, if given, otherwise base
14178 the choice on MIPS_DEFAULT_64BIT.
e7af610e 14179
316f5878
RS
14180 Treat NO_ABI like the EABIs. One reason to do this is that the
14181 plain 'mips' and 'mips64' configs have 'from-abi' as their default
14182 architecture. This code picks MIPS I for 'mips' and MIPS III for
14183 'mips64', just as we did in the days before 'from-abi'. */
14184 if (strcasecmp (cpu_string, "from-abi") == 0)
14185 {
14186 if (ABI_NEEDS_32BIT_REGS (mips_abi))
14187 return mips_cpu_info_from_isa (ISA_MIPS1);
14188
14189 if (ABI_NEEDS_64BIT_REGS (mips_abi))
14190 return mips_cpu_info_from_isa (ISA_MIPS3);
14191
14192 if (file_mips_gp32 >= 0)
14193 return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
14194
14195 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
14196 ? ISA_MIPS3
14197 : ISA_MIPS1);
14198 }
14199
14200 /* 'default' has traditionally been a no-op. Probably not very useful. */
14201 if (strcasecmp (cpu_string, "default") == 0)
14202 return 0;
14203
14204 for (p = mips_cpu_info_table; p->name != 0; p++)
14205 if (mips_matching_cpu_name_p (p->name, cpu_string))
14206 return p;
14207
14208 as_bad ("Bad value (%s) for %s", cpu_string, option);
14209 return 0;
e7af610e
NC
14210}
14211
316f5878
RS
14212/* Return the canonical processor information for ISA (a member of the
14213 ISA_MIPS* enumeration). */
14214
e7af610e 14215static const struct mips_cpu_info *
17a2f251 14216mips_cpu_info_from_isa (int isa)
e7af610e
NC
14217{
14218 int i;
14219
14220 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14221 if (mips_cpu_info_table[i].is_isa
316f5878 14222 && isa == mips_cpu_info_table[i].isa)
e7af610e
NC
14223 return (&mips_cpu_info_table[i]);
14224
e972090a 14225 return NULL;
e7af610e 14226}
fef14a42
TS
14227
14228static const struct mips_cpu_info *
17a2f251 14229mips_cpu_info_from_arch (int arch)
fef14a42
TS
14230{
14231 int i;
14232
14233 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14234 if (arch == mips_cpu_info_table[i].cpu)
14235 return (&mips_cpu_info_table[i]);
14236
14237 return NULL;
14238}
316f5878
RS
14239\f
14240static void
17a2f251 14241show (FILE *stream, const char *string, int *col_p, int *first_p)
316f5878
RS
14242{
14243 if (*first_p)
14244 {
14245 fprintf (stream, "%24s", "");
14246 *col_p = 24;
14247 }
14248 else
14249 {
14250 fprintf (stream, ", ");
14251 *col_p += 2;
14252 }
e7af610e 14253
316f5878
RS
14254 if (*col_p + strlen (string) > 72)
14255 {
14256 fprintf (stream, "\n%24s", "");
14257 *col_p = 24;
14258 }
14259
14260 fprintf (stream, "%s", string);
14261 *col_p += strlen (string);
14262
14263 *first_p = 0;
14264}
14265
14266void
17a2f251 14267md_show_usage (FILE *stream)
e7af610e 14268{
316f5878
RS
14269 int column, first;
14270 size_t i;
14271
14272 fprintf (stream, _("\
14273MIPS options:\n\
316f5878
RS
14274-EB generate big endian output\n\
14275-EL generate little endian output\n\
14276-g, -g2 do not remove unneeded NOPs or swap branches\n\
14277-G NUM allow referencing objects up to NUM bytes\n\
14278 implicitly with the gp register [default 8]\n"));
14279 fprintf (stream, _("\
14280-mips1 generate MIPS ISA I instructions\n\
14281-mips2 generate MIPS ISA II instructions\n\
14282-mips3 generate MIPS ISA III instructions\n\
14283-mips4 generate MIPS ISA IV instructions\n\
14284-mips5 generate MIPS ISA V instructions\n\
14285-mips32 generate MIPS32 ISA instructions\n\
af7ee8bf 14286-mips32r2 generate MIPS32 release 2 ISA instructions\n\
316f5878 14287-mips64 generate MIPS64 ISA instructions\n\
5f74bc13 14288-mips64r2 generate MIPS64 release 2 ISA instructions\n\
316f5878
RS
14289-march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
14290
14291 first = 1;
e7af610e
NC
14292
14293 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
316f5878
RS
14294 show (stream, mips_cpu_info_table[i].name, &column, &first);
14295 show (stream, "from-abi", &column, &first);
14296 fputc ('\n', stream);
e7af610e 14297
316f5878
RS
14298 fprintf (stream, _("\
14299-mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
14300-no-mCPU don't generate code specific to CPU.\n\
14301 For -mCPU and -no-mCPU, CPU must be one of:\n"));
14302
14303 first = 1;
14304
14305 show (stream, "3900", &column, &first);
14306 show (stream, "4010", &column, &first);
14307 show (stream, "4100", &column, &first);
14308 show (stream, "4650", &column, &first);
14309 fputc ('\n', stream);
14310
14311 fprintf (stream, _("\
14312-mips16 generate mips16 instructions\n\
14313-no-mips16 do not generate mips16 instructions\n"));
14314 fprintf (stream, _("\
d766e8ec 14315-mfix-vr4120 work around certain VR4120 errata\n\
316f5878
RS
14316-mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
14317-mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
14318-O0 remove unneeded NOPs, do not swap branches\n\
14319-O remove unneeded NOPs and swap branches\n\
316f5878
RS
14320--[no-]construct-floats [dis]allow floating point values to be constructed\n\
14321--trap, --no-break trap exception on div by 0 and mult overflow\n\
14322--break, --no-trap break exception on div by 0 and mult overflow\n"));
14323#ifdef OBJ_ELF
14324 fprintf (stream, _("\
14325-KPIC, -call_shared generate SVR4 position independent code\n\
14326-non_shared do not generate position independent code\n\
14327-xgot assume a 32 bit GOT\n\
dcd410fe 14328-mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
316f5878
RS
14329-mabi=ABI create ABI conformant object file for:\n"));
14330
14331 first = 1;
14332
14333 show (stream, "32", &column, &first);
14334 show (stream, "o64", &column, &first);
14335 show (stream, "n32", &column, &first);
14336 show (stream, "64", &column, &first);
14337 show (stream, "eabi", &column, &first);
14338
14339 fputc ('\n', stream);
14340
14341 fprintf (stream, _("\
14342-32 create o32 ABI object file (default)\n\
14343-n32 create n32 ABI object file\n\
14344-64 create 64 ABI object file\n"));
14345#endif
e7af610e 14346}
14e777e0
KB
14347
14348enum dwarf2_format
17a2f251 14349mips_dwarf2_format (void)
14e777e0
KB
14350{
14351 if (mips_abi == N64_ABI)
1de5b6a1
AO
14352 {
14353#ifdef TE_IRIX
14354 return dwarf2_format_64bit_irix;
14355#else
14356 return dwarf2_format_64bit;
14357#endif
14358 }
14e777e0
KB
14359 else
14360 return dwarf2_format_32bit;
14361}
73369e65
EC
14362
14363int
14364mips_dwarf2_addr_size (void)
14365{
14366 if (mips_abi == N64_ABI)
14367 return 8;
73369e65
EC
14368 else
14369 return 4;
14370}
This page took 1.952208 seconds and 4 git commands to generate.