2003-05-07 Eric Christopher <echristo@redhat.com>
[deliverable/binutils-gdb.git] / gas / config / tc-mips.c
CommitLineData
252b5132 1/* tc-mips.c -- assemble code for a MIPS chip.
071742cf 2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
82efde3a 3 Free Software Foundation, Inc.
252b5132
RH
4 Contributed by the OSF and Ralph Campbell.
5 Written by Keith Knowles and Ralph Campbell, working independently.
6 Modified for ECOFF and R4000 support by Ian Lance Taylor of Cygnus
7 Support.
8
9 This file is part of GAS.
10
11 GAS is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2, or (at your option)
14 any later version.
15
16 GAS is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with GAS; see the file COPYING. If not, write to the Free
23 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 02111-1307, USA. */
25
26#include "as.h"
27#include "config.h"
28#include "subsegs.h"
3882b010 29#include "safe-ctype.h"
252b5132
RH
30
31#ifdef USE_STDARG
32#include <stdarg.h>
33#endif
34#ifdef USE_VARARGS
35#include <varargs.h>
36#endif
37
38#include "opcode/mips.h"
39#include "itbl-ops.h"
c5dd6aab 40#include "dwarf2dbg.h"
252b5132
RH
41
42#ifdef DEBUG
43#define DBG(x) printf x
44#else
45#define DBG(x)
46#endif
47
48#ifdef OBJ_MAYBE_ELF
49/* Clean up namespace so we can include obj-elf.h too. */
50static int mips_output_flavor PARAMS ((void));
51static int mips_output_flavor () { return OUTPUT_FLAVOR; }
52#undef OBJ_PROCESS_STAB
53#undef OUTPUT_FLAVOR
54#undef S_GET_ALIGN
55#undef S_GET_SIZE
56#undef S_SET_ALIGN
57#undef S_SET_SIZE
252b5132
RH
58#undef obj_frob_file
59#undef obj_frob_file_after_relocs
60#undef obj_frob_symbol
61#undef obj_pop_insert
62#undef obj_sec_sym_ok_for_reloc
63#undef OBJ_COPY_SYMBOL_ATTRIBUTES
64
65#include "obj-elf.h"
66/* Fix any of them that we actually care about. */
67#undef OUTPUT_FLAVOR
68#define OUTPUT_FLAVOR mips_output_flavor()
69#endif
70
71#if defined (OBJ_ELF)
72#include "elf/mips.h"
73#endif
74
75#ifndef ECOFF_DEBUGGING
76#define NO_ECOFF_DEBUGGING
77#define ECOFF_DEBUGGING 0
78#endif
79
ecb4347a
DJ
80int mips_flag_mdebug = -1;
81
252b5132
RH
82#include "ecoff.h"
83
84#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
85static char *mips_regmask_frag;
86#endif
87
85b51719 88#define ZERO 0
252b5132
RH
89#define AT 1
90#define TREG 24
91#define PIC_CALL_REG 25
92#define KT0 26
93#define KT1 27
94#define GP 28
95#define SP 29
96#define FP 30
97#define RA 31
98
99#define ILLEGAL_REG (32)
100
101/* Allow override of standard little-endian ECOFF format. */
102
103#ifndef ECOFF_LITTLE_FORMAT
104#define ECOFF_LITTLE_FORMAT "ecoff-littlemips"
105#endif
106
107extern int target_big_endian;
108
252b5132
RH
109/* The name of the readonly data section. */
110#define RDATA_SECTION_NAME (OUTPUT_FLAVOR == bfd_target_aout_flavour \
111 ? ".data" \
112 : OUTPUT_FLAVOR == bfd_target_ecoff_flavour \
113 ? ".rdata" \
056350c6
NC
114 : OUTPUT_FLAVOR == bfd_target_coff_flavour \
115 ? ".rdata" \
252b5132
RH
116 : OUTPUT_FLAVOR == bfd_target_elf_flavour \
117 ? ".rodata" \
118 : (abort (), ""))
119
a325df1d
TS
120/* The ABI to use. */
121enum mips_abi_level
122{
123 NO_ABI = 0,
124 O32_ABI,
125 O64_ABI,
126 N32_ABI,
127 N64_ABI,
128 EABI_ABI
129};
130
131/* MIPS ABI we are using for this output file. */
316f5878 132static enum mips_abi_level mips_abi = NO_ABI;
a325df1d 133
143d77c5
EC
134/* Whether or not we have code that can call pic code. */
135int mips_abicalls = FALSE;
136
252b5132
RH
137/* This is the set of options which may be modified by the .set
138 pseudo-op. We use a struct so that .set push and .set pop are more
139 reliable. */
140
e972090a
NC
141struct mips_set_options
142{
252b5132
RH
143 /* MIPS ISA (Instruction Set Architecture) level. This is set to -1
144 if it has not been initialized. Changed by `.set mipsN', and the
145 -mipsN command line option, and the default CPU. */
146 int isa;
1f25f5d3
CD
147 /* Enabled Application Specific Extensions (ASEs). These are set to -1
148 if they have not been initialized. Changed by `.set <asename>', by
149 command line options, and based on the default architecture. */
150 int ase_mips3d;
deec1734 151 int ase_mdmx;
252b5132
RH
152 /* Whether we are assembling for the mips16 processor. 0 if we are
153 not, 1 if we are, and -1 if the value has not been initialized.
154 Changed by `.set mips16' and `.set nomips16', and the -mips16 and
155 -nomips16 command line options, and the default CPU. */
156 int mips16;
157 /* Non-zero if we should not reorder instructions. Changed by `.set
158 reorder' and `.set noreorder'. */
159 int noreorder;
160 /* Non-zero if we should not permit the $at ($1) register to be used
161 in instructions. Changed by `.set at' and `.set noat'. */
162 int noat;
163 /* Non-zero if we should warn when a macro instruction expands into
164 more than one machine instruction. Changed by `.set nomacro' and
165 `.set macro'. */
166 int warn_about_macros;
167 /* Non-zero if we should not move instructions. Changed by `.set
168 move', `.set volatile', `.set nomove', and `.set novolatile'. */
169 int nomove;
170 /* Non-zero if we should not optimize branches by moving the target
171 of the branch into the delay slot. Actually, we don't perform
172 this optimization anyhow. Changed by `.set bopt' and `.set
173 nobopt'. */
174 int nobopt;
175 /* Non-zero if we should not autoextend mips16 instructions.
176 Changed by `.set autoextend' and `.set noautoextend'. */
177 int noautoextend;
a325df1d
TS
178 /* Restrict general purpose registers and floating point registers
179 to 32 bit. This is initially determined when -mgp32 or -mfp32
180 is passed but can changed if the assembler code uses .set mipsN. */
181 int gp32;
182 int fp32;
252b5132
RH
183};
184
a325df1d 185/* True if -mgp32 was passed. */
a8e8e863 186static int file_mips_gp32 = -1;
a325df1d
TS
187
188/* True if -mfp32 was passed. */
a8e8e863 189static int file_mips_fp32 = -1;
a325df1d 190
252b5132 191/* This is the struct we use to hold the current set of options. Note
a4672219 192 that we must set the isa field to ISA_UNKNOWN and the ASE fields to
e7af610e 193 -1 to indicate that they have not been initialized. */
252b5132 194
e972090a
NC
195static struct mips_set_options mips_opts =
196{
316f5878 197 ISA_UNKNOWN, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0
e7af610e 198};
252b5132
RH
199
200/* These variables are filled in with the masks of registers used.
201 The object format code reads them and puts them in the appropriate
202 place. */
203unsigned long mips_gprmask;
204unsigned long mips_cprmask[4];
205
206/* MIPS ISA we are using for this output file. */
e7af610e 207static int file_mips_isa = ISA_UNKNOWN;
252b5132 208
a4672219
TS
209/* True if -mips16 was passed or implied by arguments passed on the
210 command line (e.g., by -march). */
211static int file_ase_mips16;
212
1f25f5d3
CD
213/* True if -mips3d was passed or implied by arguments passed on the
214 command line (e.g., by -march). */
215static int file_ase_mips3d;
216
deec1734
CD
217/* True if -mdmx was passed or implied by arguments passed on the
218 command line (e.g., by -march). */
219static int file_ase_mdmx;
220
ec68c924
EC
221/* The argument of the -march= flag. The architecture we are assembling. */
222static int mips_arch = CPU_UNKNOWN;
316f5878
RS
223static const char *mips_arch_string;
224static const struct mips_cpu_info *mips_arch_info;
ec68c924
EC
225
226/* The argument of the -mtune= flag. The architecture for which we
227 are optimizing. */
228static int mips_tune = CPU_UNKNOWN;
316f5878
RS
229static const char *mips_tune_string;
230static const struct mips_cpu_info *mips_tune_info;
ec68c924 231
316f5878 232/* True when generating 32-bit code for a 64-bit processor. */
252b5132
RH
233static int mips_32bitmode = 0;
234
9ce8a5dd
GRK
235/* Some ISA's have delay slots for instructions which read or write
236 from a coprocessor (eg. mips1-mips3); some don't (eg mips4).
bdaaa2e1 237 Return true if instructions marked INSN_LOAD_COPROC_DELAY,
9ce8a5dd
GRK
238 INSN_COPROC_MOVE_DELAY, or INSN_WRITE_COND_CODE actually have a
239 delay slot in this ISA. The uses of this macro assume that any
240 ISA that has delay slots for one of these, has them for all. They
241 also assume that ISAs which don't have delays for these insns, don't
bdaaa2e1 242 have delays for the INSN_LOAD_MEMORY_DELAY instructions either. */
9ce8a5dd 243#define ISA_HAS_COPROC_DELAYS(ISA) ( \
e7af610e
NC
244 (ISA) == ISA_MIPS1 \
245 || (ISA) == ISA_MIPS2 \
246 || (ISA) == ISA_MIPS3 \
9ce8a5dd
GRK
247 )
248
316f5878
RS
249/* True if the given ABI requires 32-bit registers. */
250#define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
251
252/* Likewise 64-bit registers. */
253#define ABI_NEEDS_64BIT_REGS(ABI) \
254 ((ABI) == N32_ABI \
255 || (ABI) == N64_ABI \
256 || (ABI) == O64_ABI)
257
bdaaa2e1 258/* Return true if ISA supports 64 bit gp register instructions. */
9ce8a5dd 259#define ISA_HAS_64BIT_REGS(ISA) ( \
e7af610e
NC
260 (ISA) == ISA_MIPS3 \
261 || (ISA) == ISA_MIPS4 \
84ea6cf2 262 || (ISA) == ISA_MIPS5 \
d1cf510e 263 || (ISA) == ISA_MIPS64 \
9ce8a5dd
GRK
264 )
265
af7ee8bf
CD
266/* Return true if ISA supports 64-bit right rotate (dror et al.)
267 instructions. */
268#define ISA_HAS_DROR(ISA) ( \
269 0 \
270 )
271
272/* Return true if ISA supports 32-bit right rotate (ror et al.)
273 instructions. */
274#define ISA_HAS_ROR(ISA) ( \
275 (ISA) == ISA_MIPS32R2 \
276 )
277
e013f690 278#define HAVE_32BIT_GPRS \
316f5878 279 (mips_opts.gp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
ca4e0257 280
e013f690 281#define HAVE_32BIT_FPRS \
316f5878 282 (mips_opts.fp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
ca4e0257
RS
283
284#define HAVE_64BIT_GPRS (! HAVE_32BIT_GPRS)
285#define HAVE_64BIT_FPRS (! HAVE_32BIT_FPRS)
286
316f5878 287#define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
e013f690 288
316f5878 289#define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
e013f690
TS
290
291/* We can only have 64bit addresses if the object file format
292 supports it. */
afdbd6d0
CD
293#define HAVE_32BIT_ADDRESSES \
294 (HAVE_32BIT_GPRS \
295 || ((bfd_arch_bits_per_address (stdoutput) == 32 \
296 || ! HAVE_64BIT_OBJECTS) \
297 && mips_pic != EMBEDDED_PIC))
e013f690
TS
298
299#define HAVE_64BIT_ADDRESSES (! HAVE_32BIT_ADDRESSES)
5a7a0b7b
AO
300#define HAVE_64BIT_ADDRESS_CONSTANTS (HAVE_64BIT_ADDRESSES \
301 || HAVE_64BIT_GPRS)
ca4e0257 302
a4672219 303/* Return true if the given CPU supports the MIPS16 ASE. */
3396de36
TS
304#define CPU_HAS_MIPS16(cpu) \
305 (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0 \
306 || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
a4672219 307
1f25f5d3
CD
308/* Return true if the given CPU supports the MIPS3D ASE. */
309#define CPU_HAS_MIPS3D(cpu) ((cpu) == CPU_SB1 \
310 )
311
deec1734 312/* Return true if the given CPU supports the MDMX ASE. */
b34976b6 313#define CPU_HAS_MDMX(cpu) (FALSE \
deec1734
CD
314 )
315
60b63b72
RS
316/* True if CPU has a dror instruction. */
317#define CPU_HAS_DROR(CPU) ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
318
319/* True if CPU has a ror instruction. */
320#define CPU_HAS_ROR(CPU) CPU_HAS_DROR (CPU)
321
bdaaa2e1 322/* Whether the processor uses hardware interlocks to protect
252b5132 323 reads from the HI and LO registers, and thus does not
ec68c924 324 require nops to be inserted. */
252b5132 325
ec68c924 326#define hilo_interlocks (mips_arch == CPU_R4010 \
60b63b72 327 || mips_arch == CPU_VR5500 \
0a758a12 328 || mips_arch == CPU_SB1 \
252b5132
RH
329 )
330
331/* Whether the processor uses hardware interlocks to protect reads
332 from the GPRs, and thus does not require nops to be inserted. */
333#define gpr_interlocks \
e7af610e 334 (mips_opts.isa != ISA_MIPS1 \
60b63b72
RS
335 || mips_arch == CPU_VR5400 \
336 || mips_arch == CPU_VR5500 \
ec68c924 337 || mips_arch == CPU_R3900)
252b5132
RH
338
339/* As with other "interlocks" this is used by hardware that has FP
340 (co-processor) interlocks. */
bdaaa2e1 341/* Itbl support may require additional care here. */
ec68c924 342#define cop_interlocks (mips_arch == CPU_R4300 \
60b63b72
RS
343 || mips_arch == CPU_VR5400 \
344 || mips_arch == CPU_VR5500 \
0a758a12 345 || mips_arch == CPU_SB1 \
252b5132
RH
346 )
347
6b76fefe
CM
348/* Is this a mfhi or mflo instruction? */
349#define MF_HILO_INSN(PINFO) \
350 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
351
252b5132
RH
352/* MIPS PIC level. */
353
a161fe53 354enum mips_pic_level mips_pic;
252b5132 355
39c0a331
L
356/* Warn about all NOPS that the assembler generates. */
357static int warn_nops = 0;
358
c9914766 359/* 1 if we should generate 32 bit offsets from the $gp register in
252b5132 360 SVR4_PIC mode. Currently has no meaning in other modes. */
c9914766 361static int mips_big_got = 0;
252b5132
RH
362
363/* 1 if trap instructions should used for overflow rather than break
364 instructions. */
c9914766 365static int mips_trap = 0;
252b5132 366
119d663a 367/* 1 if double width floating point constants should not be constructed
b6ff326e 368 by assembling two single width halves into two single width floating
119d663a
NC
369 point registers which just happen to alias the double width destination
370 register. On some architectures this aliasing can be disabled by a bit
d547a75e 371 in the status register, and the setting of this bit cannot be determined
119d663a
NC
372 automatically at assemble time. */
373static int mips_disable_float_construction;
374
252b5132
RH
375/* Non-zero if any .set noreorder directives were used. */
376
377static int mips_any_noreorder;
378
6b76fefe
CM
379/* Non-zero if nops should be inserted when the register referenced in
380 an mfhi/mflo instruction is read in the next two instructions. */
381static int mips_7000_hilo_fix;
382
252b5132 383/* The size of the small data section. */
156c2f8b 384static unsigned int g_switch_value = 8;
252b5132
RH
385/* Whether the -G option was used. */
386static int g_switch_seen = 0;
387
388#define N_RMASK 0xc4
389#define N_VFP 0xd4
390
391/* If we can determine in advance that GP optimization won't be
392 possible, we can skip the relaxation stuff that tries to produce
393 GP-relative references. This makes delay slot optimization work
394 better.
395
396 This function can only provide a guess, but it seems to work for
fba2b7f9
GK
397 gcc output. It needs to guess right for gcc, otherwise gcc
398 will put what it thinks is a GP-relative instruction in a branch
399 delay slot.
252b5132
RH
400
401 I don't know if a fix is needed for the SVR4_PIC mode. I've only
402 fixed it for the non-PIC mode. KR 95/04/07 */
403static int nopic_need_relax PARAMS ((symbolS *, int));
404
405/* handle of the OPCODE hash table */
406static struct hash_control *op_hash = NULL;
407
408/* The opcode hash table we use for the mips16. */
409static struct hash_control *mips16_op_hash = NULL;
410
411/* This array holds the chars that always start a comment. If the
412 pre-processor is disabled, these aren't very useful */
413const char comment_chars[] = "#";
414
415/* This array holds the chars that only start a comment at the beginning of
416 a line. If the line seems to have the form '# 123 filename'
417 .line and .file directives will appear in the pre-processed output */
418/* Note that input_file.c hand checks for '#' at the beginning of the
419 first line of the input file. This is because the compiler outputs
bdaaa2e1 420 #NO_APP at the beginning of its output. */
252b5132
RH
421/* Also note that C style comments are always supported. */
422const char line_comment_chars[] = "#";
423
bdaaa2e1 424/* This array holds machine specific line separator characters. */
63a0b638 425const char line_separator_chars[] = ";";
252b5132
RH
426
427/* Chars that can be used to separate mant from exp in floating point nums */
428const char EXP_CHARS[] = "eE";
429
430/* Chars that mean this number is a floating point constant */
431/* As in 0f12.456 */
432/* or 0d1.2345e12 */
433const char FLT_CHARS[] = "rRsSfFdDxXpP";
434
435/* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
436 changed in read.c . Ideally it shouldn't have to know about it at all,
437 but nothing is ideal around here.
438 */
439
440static char *insn_error;
441
442static int auto_align = 1;
443
444/* When outputting SVR4 PIC code, the assembler needs to know the
445 offset in the stack frame from which to restore the $gp register.
446 This is set by the .cprestore pseudo-op, and saved in this
447 variable. */
448static offsetT mips_cprestore_offset = -1;
449
6478892d
TS
450/* Similiar for NewABI PIC code, where $gp is callee-saved. NewABI has some
451 more optimizations, it can use a register value instead of a memory-saved
956cd1d6 452 offset and even an other register than $gp as global pointer. */
6478892d
TS
453static offsetT mips_cpreturn_offset = -1;
454static int mips_cpreturn_register = -1;
455static int mips_gp_register = GP;
def2e0dd 456static int mips_gprel_offset = 0;
6478892d 457
7a621144
DJ
458/* Whether mips_cprestore_offset has been set in the current function
459 (or whether it has already been warned about, if not). */
460static int mips_cprestore_valid = 0;
461
252b5132
RH
462/* This is the register which holds the stack frame, as set by the
463 .frame pseudo-op. This is needed to implement .cprestore. */
464static int mips_frame_reg = SP;
465
7a621144
DJ
466/* Whether mips_frame_reg has been set in the current function
467 (or whether it has already been warned about, if not). */
468static int mips_frame_reg_valid = 0;
469
252b5132
RH
470/* To output NOP instructions correctly, we need to keep information
471 about the previous two instructions. */
472
473/* Whether we are optimizing. The default value of 2 means to remove
474 unneeded NOPs and swap branch instructions when possible. A value
475 of 1 means to not swap branches. A value of 0 means to always
476 insert NOPs. */
477static int mips_optimize = 2;
478
479/* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
480 equivalent to seeing no -g option at all. */
481static int mips_debug = 0;
482
483/* The previous instruction. */
484static struct mips_cl_insn prev_insn;
485
486/* The instruction before prev_insn. */
487static struct mips_cl_insn prev_prev_insn;
488
489/* If we don't want information for prev_insn or prev_prev_insn, we
490 point the insn_mo field at this dummy integer. */
43841e91 491static const struct mips_opcode dummy_opcode = { NULL, NULL, 0, 0, 0, 0 };
252b5132
RH
492
493/* Non-zero if prev_insn is valid. */
494static int prev_insn_valid;
495
496/* The frag for the previous instruction. */
497static struct frag *prev_insn_frag;
498
499/* The offset into prev_insn_frag for the previous instruction. */
500static long prev_insn_where;
501
502/* The reloc type for the previous instruction, if any. */
f6688943 503static bfd_reloc_code_real_type prev_insn_reloc_type[3];
252b5132
RH
504
505/* The reloc for the previous instruction, if any. */
f6688943 506static fixS *prev_insn_fixp[3];
252b5132
RH
507
508/* Non-zero if the previous instruction was in a delay slot. */
509static int prev_insn_is_delay_slot;
510
511/* Non-zero if the previous instruction was in a .set noreorder. */
512static int prev_insn_unreordered;
513
514/* Non-zero if the previous instruction uses an extend opcode (if
515 mips16). */
516static int prev_insn_extended;
517
518/* Non-zero if the previous previous instruction was in a .set
519 noreorder. */
520static int prev_prev_insn_unreordered;
521
522/* If this is set, it points to a frag holding nop instructions which
523 were inserted before the start of a noreorder section. If those
524 nops turn out to be unnecessary, the size of the frag can be
525 decreased. */
526static fragS *prev_nop_frag;
527
528/* The number of nop instructions we created in prev_nop_frag. */
529static int prev_nop_frag_holds;
530
531/* The number of nop instructions that we know we need in
bdaaa2e1 532 prev_nop_frag. */
252b5132
RH
533static int prev_nop_frag_required;
534
535/* The number of instructions we've seen since prev_nop_frag. */
536static int prev_nop_frag_since;
537
538/* For ECOFF and ELF, relocations against symbols are done in two
539 parts, with a HI relocation and a LO relocation. Each relocation
540 has only 16 bits of space to store an addend. This means that in
541 order for the linker to handle carries correctly, it must be able
542 to locate both the HI and the LO relocation. This means that the
543 relocations must appear in order in the relocation table.
544
545 In order to implement this, we keep track of each unmatched HI
546 relocation. We then sort them so that they immediately precede the
bdaaa2e1 547 corresponding LO relocation. */
252b5132 548
e972090a
NC
549struct mips_hi_fixup
550{
252b5132
RH
551 /* Next HI fixup. */
552 struct mips_hi_fixup *next;
553 /* This fixup. */
554 fixS *fixp;
555 /* The section this fixup is in. */
556 segT seg;
557};
558
559/* The list of unmatched HI relocs. */
560
561static struct mips_hi_fixup *mips_hi_fixup_list;
562
64bdfcaf
RS
563/* The frag containing the last explicit relocation operator.
564 Null if explicit relocations have not been used. */
565
566static fragS *prev_reloc_op_frag;
567
252b5132
RH
568/* Map normal MIPS register numbers to mips16 register numbers. */
569
570#define X ILLEGAL_REG
e972090a
NC
571static const int mips32_to_16_reg_map[] =
572{
252b5132
RH
573 X, X, 2, 3, 4, 5, 6, 7,
574 X, X, X, X, X, X, X, X,
575 0, 1, X, X, X, X, X, X,
576 X, X, X, X, X, X, X, X
577};
578#undef X
579
580/* Map mips16 register numbers to normal MIPS register numbers. */
581
e972090a
NC
582static const unsigned int mips16_to_32_reg_map[] =
583{
252b5132
RH
584 16, 17, 2, 3, 4, 5, 6, 7
585};
60b63b72
RS
586
587static int mips_fix_4122_bugs;
4a6a3df4
AO
588
589/* We don't relax branches by default, since this causes us to expand
590 `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
591 fail to compute the offset before expanding the macro to the most
592 efficient expansion. */
593
594static int mips_relax_branch;
252b5132
RH
595\f
596/* Since the MIPS does not have multiple forms of PC relative
597 instructions, we do not have to do relaxing as is done on other
598 platforms. However, we do have to handle GP relative addressing
599 correctly, which turns out to be a similar problem.
600
601 Every macro that refers to a symbol can occur in (at least) two
602 forms, one with GP relative addressing and one without. For
603 example, loading a global variable into a register generally uses
604 a macro instruction like this:
605 lw $4,i
606 If i can be addressed off the GP register (this is true if it is in
607 the .sbss or .sdata section, or if it is known to be smaller than
608 the -G argument) this will generate the following instruction:
609 lw $4,i($gp)
610 This instruction will use a GPREL reloc. If i can not be addressed
611 off the GP register, the following instruction sequence will be used:
612 lui $at,i
613 lw $4,i($at)
614 In this case the first instruction will have a HI16 reloc, and the
615 second reloc will have a LO16 reloc. Both relocs will be against
616 the symbol i.
617
618 The issue here is that we may not know whether i is GP addressable
619 until after we see the instruction that uses it. Therefore, we
620 want to be able to choose the final instruction sequence only at
621 the end of the assembly. This is similar to the way other
622 platforms choose the size of a PC relative instruction only at the
623 end of assembly.
624
625 When generating position independent code we do not use GP
626 addressing in quite the same way, but the issue still arises as
627 external symbols and local symbols must be handled differently.
628
629 We handle these issues by actually generating both possible
630 instruction sequences. The longer one is put in a frag_var with
631 type rs_machine_dependent. We encode what to do with the frag in
632 the subtype field. We encode (1) the number of existing bytes to
633 replace, (2) the number of new bytes to use, (3) the offset from
634 the start of the existing bytes to the first reloc we must generate
635 (that is, the offset is applied from the start of the existing
636 bytes after they are replaced by the new bytes, if any), (4) the
637 offset from the start of the existing bytes to the second reloc,
638 (5) whether a third reloc is needed (the third reloc is always four
639 bytes after the second reloc), and (6) whether to warn if this
640 variant is used (this is sometimes needed if .set nomacro or .set
641 noat is in effect). All these numbers are reasonably small.
642
643 Generating two instruction sequences must be handled carefully to
644 ensure that delay slots are handled correctly. Fortunately, there
645 are a limited number of cases. When the second instruction
646 sequence is generated, append_insn is directed to maintain the
647 existing delay slot information, so it continues to apply to any
648 code after the second instruction sequence. This means that the
649 second instruction sequence must not impose any requirements not
650 required by the first instruction sequence.
651
652 These variant frags are then handled in functions called by the
653 machine independent code. md_estimate_size_before_relax returns
654 the final size of the frag. md_convert_frag sets up the final form
655 of the frag. tc_gen_reloc adjust the first reloc and adds a second
656 one if needed. */
657#define RELAX_ENCODE(old, new, reloc1, reloc2, reloc3, warn) \
658 ((relax_substateT) \
659 (((old) << 23) \
660 | ((new) << 16) \
661 | (((reloc1) + 64) << 9) \
662 | (((reloc2) + 64) << 2) \
663 | ((reloc3) ? (1 << 1) : 0) \
664 | ((warn) ? 1 : 0)))
665#define RELAX_OLD(i) (((i) >> 23) & 0x7f)
666#define RELAX_NEW(i) (((i) >> 16) & 0x7f)
9a41af64
TS
667#define RELAX_RELOC1(i) ((valueT) (((i) >> 9) & 0x7f) - 64)
668#define RELAX_RELOC2(i) ((valueT) (((i) >> 2) & 0x7f) - 64)
252b5132
RH
669#define RELAX_RELOC3(i) (((i) >> 1) & 1)
670#define RELAX_WARN(i) ((i) & 1)
671
4a6a3df4
AO
672/* Branch without likely bit. If label is out of range, we turn:
673
674 beq reg1, reg2, label
675 delay slot
676
677 into
678
679 bne reg1, reg2, 0f
680 nop
681 j label
682 0: delay slot
683
684 with the following opcode replacements:
685
686 beq <-> bne
687 blez <-> bgtz
688 bltz <-> bgez
689 bc1f <-> bc1t
690
691 bltzal <-> bgezal (with jal label instead of j label)
692
693 Even though keeping the delay slot instruction in the delay slot of
694 the branch would be more efficient, it would be very tricky to do
695 correctly, because we'd have to introduce a variable frag *after*
696 the delay slot instruction, and expand that instead. Let's do it
697 the easy way for now, even if the branch-not-taken case now costs
698 one additional instruction. Out-of-range branches are not supposed
699 to be common, anyway.
700
701 Branch likely. If label is out of range, we turn:
702
703 beql reg1, reg2, label
704 delay slot (annulled if branch not taken)
705
706 into
707
708 beql reg1, reg2, 1f
709 nop
710 beql $0, $0, 2f
711 nop
712 1: j[al] label
713 delay slot (executed only if branch taken)
714 2:
715
716 It would be possible to generate a shorter sequence by losing the
717 likely bit, generating something like:
b34976b6 718
4a6a3df4
AO
719 bne reg1, reg2, 0f
720 nop
721 j[al] label
722 delay slot (executed only if branch taken)
723 0:
724
725 beql -> bne
726 bnel -> beq
727 blezl -> bgtz
728 bgtzl -> blez
729 bltzl -> bgez
730 bgezl -> bltz
731 bc1fl -> bc1t
732 bc1tl -> bc1f
733
734 bltzall -> bgezal (with jal label instead of j label)
735 bgezall -> bltzal (ditto)
736
737
738 but it's not clear that it would actually improve performance. */
af6ae2ad 739#define RELAX_BRANCH_ENCODE(uncond, likely, link, toofar) \
4a6a3df4
AO
740 ((relax_substateT) \
741 (0xc0000000 \
742 | ((toofar) ? 1 : 0) \
743 | ((link) ? 2 : 0) \
744 | ((likely) ? 4 : 0) \
af6ae2ad 745 | ((uncond) ? 8 : 0)))
4a6a3df4 746#define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
4a6a3df4
AO
747#define RELAX_BRANCH_UNCOND(i) (((i) & 8) != 0)
748#define RELAX_BRANCH_LIKELY(i) (((i) & 4) != 0)
749#define RELAX_BRANCH_LINK(i) (((i) & 2) != 0)
ae6063d4 750#define RELAX_BRANCH_TOOFAR(i) (((i) & 1) != 0)
4a6a3df4 751
252b5132
RH
752/* For mips16 code, we use an entirely different form of relaxation.
753 mips16 supports two versions of most instructions which take
754 immediate values: a small one which takes some small value, and a
755 larger one which takes a 16 bit value. Since branches also follow
756 this pattern, relaxing these values is required.
757
758 We can assemble both mips16 and normal MIPS code in a single
759 object. Therefore, we need to support this type of relaxation at
760 the same time that we support the relaxation described above. We
761 use the high bit of the subtype field to distinguish these cases.
762
763 The information we store for this type of relaxation is the
764 argument code found in the opcode file for this relocation, whether
765 the user explicitly requested a small or extended form, and whether
766 the relocation is in a jump or jal delay slot. That tells us the
767 size of the value, and how it should be stored. We also store
768 whether the fragment is considered to be extended or not. We also
769 store whether this is known to be a branch to a different section,
770 whether we have tried to relax this frag yet, and whether we have
771 ever extended a PC relative fragment because of a shift count. */
772#define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
773 (0x80000000 \
774 | ((type) & 0xff) \
775 | ((small) ? 0x100 : 0) \
776 | ((ext) ? 0x200 : 0) \
777 | ((dslot) ? 0x400 : 0) \
778 | ((jal_dslot) ? 0x800 : 0))
4a6a3df4 779#define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
252b5132
RH
780#define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
781#define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
782#define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
783#define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
784#define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
785#define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
786#define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
787#define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
788#define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
789#define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
790#define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
885add95
CD
791
792/* Is the given value a sign-extended 32-bit value? */
793#define IS_SEXT_32BIT_NUM(x) \
794 (((x) &~ (offsetT) 0x7fffffff) == 0 \
795 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
796
797/* Is the given value a sign-extended 16-bit value? */
798#define IS_SEXT_16BIT_NUM(x) \
799 (((x) &~ (offsetT) 0x7fff) == 0 \
800 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
801
252b5132
RH
802\f
803/* Prototypes for static functions. */
804
805#ifdef __STDC__
806#define internalError() \
807 as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
808#else
809#define internalError() as_fatal (_("MIPS internal Error"));
810#endif
811
812enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
813
5919d012
RS
814static inline bfd_boolean reloc_needs_lo_p
815 PARAMS ((bfd_reloc_code_real_type));
816static inline bfd_boolean fixup_has_matching_lo_p
817 PARAMS ((fixS *));
b34976b6
AM
818static int insn_uses_reg
819 PARAMS ((struct mips_cl_insn *ip, unsigned int reg,
820 enum mips_regclass class));
821static int reg_needs_delay
822 PARAMS ((unsigned int));
823static void mips16_mark_labels
824 PARAMS ((void));
825static void append_insn
826 PARAMS ((char *place, struct mips_cl_insn * ip, expressionS * p,
5e0116d5 827 bfd_reloc_code_real_type *r));
b34976b6
AM
828static void mips_no_prev_insn
829 PARAMS ((int));
830static void mips_emit_delays
831 PARAMS ((bfd_boolean));
252b5132 832#ifdef USE_STDARG
b34976b6
AM
833static void macro_build
834 PARAMS ((char *place, int *counter, expressionS * ep, const char *name,
835 const char *fmt, ...));
252b5132
RH
836#else
837static void macro_build ();
838#endif
b34976b6
AM
839static void mips16_macro_build
840 PARAMS ((char *, int *, expressionS *, const char *, const char *, va_list));
841static void macro_build_jalr
842 PARAMS ((int, expressionS *));
843static void macro_build_lui
844 PARAMS ((char *place, int *counter, expressionS * ep, int regnum));
845static void macro_build_ldst_constoffset
846 PARAMS ((char *place, int *counter, expressionS * ep, const char *op,
847 int valreg, int breg));
848static void set_at
849 PARAMS ((int *counter, int reg, int unsignedp));
850static void check_absolute_expr
851 PARAMS ((struct mips_cl_insn * ip, expressionS *));
852static void load_register
853 PARAMS ((int *, int, expressionS *, int));
854static void load_address
855 PARAMS ((int *, int, expressionS *, int *));
856static void move_register
857 PARAMS ((int *, int, int));
858static void macro
859 PARAMS ((struct mips_cl_insn * ip));
860static void mips16_macro
861 PARAMS ((struct mips_cl_insn * ip));
252b5132 862#ifdef LOSING_COMPILER
b34976b6
AM
863static void macro2
864 PARAMS ((struct mips_cl_insn * ip));
252b5132 865#endif
b34976b6
AM
866static void mips_ip
867 PARAMS ((char *str, struct mips_cl_insn * ip));
868static void mips16_ip
869 PARAMS ((char *str, struct mips_cl_insn * ip));
870static void mips16_immed
871 PARAMS ((char *, unsigned int, int, offsetT, bfd_boolean, bfd_boolean,
872 bfd_boolean, unsigned long *, bfd_boolean *, unsigned short *));
5e0116d5
RS
873static bfd_boolean parse_relocation
874 PARAMS ((char **, bfd_reloc_code_real_type *));
875static size_t my_getSmallExpression
876 PARAMS ((expressionS *, bfd_reloc_code_real_type *, char *));
b34976b6
AM
877static void my_getExpression
878 PARAMS ((expressionS *, char *));
ae948b86 879#ifdef OBJ_ELF
b34976b6
AM
880static int support_64bit_objects
881 PARAMS((void));
ae948b86 882#endif
b34976b6
AM
883static void mips_set_option_string
884 PARAMS ((const char **, const char *));
885static symbolS *get_symbol
886 PARAMS ((void));
887static void mips_align
888 PARAMS ((int to, int fill, symbolS *label));
889static void s_align
890 PARAMS ((int));
891static void s_change_sec
892 PARAMS ((int));
893static void s_change_section
894 PARAMS ((int));
895static void s_cons
896 PARAMS ((int));
897static void s_float_cons
898 PARAMS ((int));
899static void s_mips_globl
900 PARAMS ((int));
901static void s_option
902 PARAMS ((int));
903static void s_mipsset
904 PARAMS ((int));
905static void s_abicalls
906 PARAMS ((int));
907static void s_cpload
908 PARAMS ((int));
909static void s_cpsetup
910 PARAMS ((int));
911static void s_cplocal
912 PARAMS ((int));
913static void s_cprestore
914 PARAMS ((int));
915static void s_cpreturn
916 PARAMS ((int));
917static void s_gpvalue
918 PARAMS ((int));
919static void s_gpword
920 PARAMS ((int));
921static void s_gpdword
922 PARAMS ((int));
923static void s_cpadd
924 PARAMS ((int));
925static void s_insn
926 PARAMS ((int));
927static void md_obj_begin
928 PARAMS ((void));
929static void md_obj_end
930 PARAMS ((void));
931static long get_number
932 PARAMS ((void));
933static void s_mips_ent
934 PARAMS ((int));
935static void s_mips_end
936 PARAMS ((int));
937static void s_mips_frame
938 PARAMS ((int));
939static void s_mips_mask
940 PARAMS ((int));
941static void s_mips_stab
942 PARAMS ((int));
943static void s_mips_weakext
944 PARAMS ((int));
945static void s_mips_file
946 PARAMS ((int));
947static void s_mips_loc
948 PARAMS ((int));
5919d012
RS
949static bfd_boolean pic_need_relax
950 PARAMS ((symbolS *, asection *));
b34976b6
AM
951static int mips16_extended_frag
952 PARAMS ((fragS *, asection *, long));
4a6a3df4 953static int relaxed_branch_length (fragS *, asection *, int);
b34976b6
AM
954static int validate_mips_insn
955 PARAMS ((const struct mips_opcode *));
956static void show
957 PARAMS ((FILE *, const char *, int *, int *));
add55e1f 958#ifdef OBJ_ELF
b34976b6
AM
959static int mips_need_elf_addend_fixup
960 PARAMS ((fixS *));
add55e1f 961#endif
e7af610e
NC
962
963/* Table and functions used to map between CPU/ISA names, and
964 ISA levels, and CPU numbers. */
965
e972090a
NC
966struct mips_cpu_info
967{
e7af610e
NC
968 const char *name; /* CPU or ISA name. */
969 int is_isa; /* Is this an ISA? (If 0, a CPU.) */
970 int isa; /* ISA level. */
971 int cpu; /* CPU number (default CPU if ISA). */
972};
973
b34976b6
AM
974static void mips_set_architecture
975 PARAMS ((const struct mips_cpu_info *));
976static void mips_set_tune
977 PARAMS ((const struct mips_cpu_info *));
978static bfd_boolean mips_strict_matching_cpu_name_p
979 PARAMS ((const char *, const char *));
980static bfd_boolean mips_matching_cpu_name_p
981 PARAMS ((const char *, const char *));
982static const struct mips_cpu_info *mips_parse_cpu
983 PARAMS ((const char *, const char *));
984static const struct mips_cpu_info *mips_cpu_info_from_isa
985 PARAMS ((int));
252b5132
RH
986\f
987/* Pseudo-op table.
988
989 The following pseudo-ops from the Kane and Heinrich MIPS book
990 should be defined here, but are currently unsupported: .alias,
991 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
992
993 The following pseudo-ops from the Kane and Heinrich MIPS book are
994 specific to the type of debugging information being generated, and
995 should be defined by the object format: .aent, .begin, .bend,
996 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
997 .vreg.
998
999 The following pseudo-ops from the Kane and Heinrich MIPS book are
1000 not MIPS CPU specific, but are also not specific to the object file
1001 format. This file is probably the best place to define them, but
1002 they are not currently supported: .asm0, .endr, .lab, .repeat,
1003 .struct. */
1004
e972090a
NC
1005static const pseudo_typeS mips_pseudo_table[] =
1006{
beae10d5 1007 /* MIPS specific pseudo-ops. */
252b5132
RH
1008 {"option", s_option, 0},
1009 {"set", s_mipsset, 0},
1010 {"rdata", s_change_sec, 'r'},
1011 {"sdata", s_change_sec, 's'},
1012 {"livereg", s_ignore, 0},
1013 {"abicalls", s_abicalls, 0},
1014 {"cpload", s_cpload, 0},
6478892d
TS
1015 {"cpsetup", s_cpsetup, 0},
1016 {"cplocal", s_cplocal, 0},
252b5132 1017 {"cprestore", s_cprestore, 0},
6478892d
TS
1018 {"cpreturn", s_cpreturn, 0},
1019 {"gpvalue", s_gpvalue, 0},
252b5132 1020 {"gpword", s_gpword, 0},
10181a0d 1021 {"gpdword", s_gpdword, 0},
252b5132
RH
1022 {"cpadd", s_cpadd, 0},
1023 {"insn", s_insn, 0},
1024
beae10d5 1025 /* Relatively generic pseudo-ops that happen to be used on MIPS
252b5132
RH
1026 chips. */
1027 {"asciiz", stringer, 1},
1028 {"bss", s_change_sec, 'b'},
1029 {"err", s_err, 0},
1030 {"half", s_cons, 1},
1031 {"dword", s_cons, 3},
1032 {"weakext", s_mips_weakext, 0},
1033
beae10d5 1034 /* These pseudo-ops are defined in read.c, but must be overridden
252b5132
RH
1035 here for one reason or another. */
1036 {"align", s_align, 0},
1037 {"byte", s_cons, 0},
1038 {"data", s_change_sec, 'd'},
1039 {"double", s_float_cons, 'd'},
1040 {"float", s_float_cons, 'f'},
1041 {"globl", s_mips_globl, 0},
1042 {"global", s_mips_globl, 0},
1043 {"hword", s_cons, 1},
1044 {"int", s_cons, 2},
1045 {"long", s_cons, 2},
1046 {"octa", s_cons, 4},
1047 {"quad", s_cons, 3},
cca86cc8 1048 {"section", s_change_section, 0},
252b5132
RH
1049 {"short", s_cons, 1},
1050 {"single", s_float_cons, 'f'},
1051 {"stabn", s_mips_stab, 'n'},
1052 {"text", s_change_sec, 't'},
1053 {"word", s_cons, 2},
add56521 1054
add56521 1055 { "extern", ecoff_directive_extern, 0},
add56521 1056
43841e91 1057 { NULL, NULL, 0 },
252b5132
RH
1058};
1059
e972090a
NC
1060static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1061{
beae10d5
KH
1062 /* These pseudo-ops should be defined by the object file format.
1063 However, a.out doesn't support them, so we have versions here. */
252b5132
RH
1064 {"aent", s_mips_ent, 1},
1065 {"bgnb", s_ignore, 0},
1066 {"end", s_mips_end, 0},
1067 {"endb", s_ignore, 0},
1068 {"ent", s_mips_ent, 0},
c5dd6aab 1069 {"file", s_mips_file, 0},
252b5132
RH
1070 {"fmask", s_mips_mask, 'F'},
1071 {"frame", s_mips_frame, 0},
c5dd6aab 1072 {"loc", s_mips_loc, 0},
252b5132
RH
1073 {"mask", s_mips_mask, 'R'},
1074 {"verstamp", s_ignore, 0},
43841e91 1075 { NULL, NULL, 0 },
252b5132
RH
1076};
1077
1078extern void pop_insert PARAMS ((const pseudo_typeS *));
1079
1080void
1081mips_pop_insert ()
1082{
1083 pop_insert (mips_pseudo_table);
1084 if (! ECOFF_DEBUGGING)
1085 pop_insert (mips_nonecoff_pseudo_table);
1086}
1087\f
1088/* Symbols labelling the current insn. */
1089
e972090a
NC
1090struct insn_label_list
1091{
252b5132
RH
1092 struct insn_label_list *next;
1093 symbolS *label;
1094};
1095
1096static struct insn_label_list *insn_labels;
1097static struct insn_label_list *free_insn_labels;
1098
1099static void mips_clear_insn_labels PARAMS ((void));
1100
1101static inline void
1102mips_clear_insn_labels ()
1103{
1104 register struct insn_label_list **pl;
1105
1106 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1107 ;
1108 *pl = insn_labels;
1109 insn_labels = NULL;
1110}
1111\f
1112static char *expr_end;
1113
1114/* Expressions which appear in instructions. These are set by
1115 mips_ip. */
1116
1117static expressionS imm_expr;
1118static expressionS offset_expr;
1119
1120/* Relocs associated with imm_expr and offset_expr. */
1121
f6688943
TS
1122static bfd_reloc_code_real_type imm_reloc[3]
1123 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1124static bfd_reloc_code_real_type offset_reloc[3]
1125 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 1126
252b5132
RH
1127/* These are set by mips16_ip if an explicit extension is used. */
1128
b34976b6 1129static bfd_boolean mips16_small, mips16_ext;
252b5132 1130
7ed4a06a 1131#ifdef OBJ_ELF
ecb4347a
DJ
1132/* The pdr segment for per procedure frame/regmask info. Not used for
1133 ECOFF debugging. */
252b5132
RH
1134
1135static segT pdr_seg;
7ed4a06a 1136#endif
252b5132 1137
e013f690
TS
1138/* The default target format to use. */
1139
1140const char *
1141mips_target_format ()
1142{
1143 switch (OUTPUT_FLAVOR)
1144 {
1145 case bfd_target_aout_flavour:
1146 return target_big_endian ? "a.out-mips-big" : "a.out-mips-little";
1147 case bfd_target_ecoff_flavour:
1148 return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
1149 case bfd_target_coff_flavour:
1150 return "pe-mips";
1151 case bfd_target_elf_flavour:
1152#ifdef TE_TMIPS
cfe86eaa 1153 /* This is traditional mips. */
e013f690 1154 return (target_big_endian
cfe86eaa
TS
1155 ? (HAVE_64BIT_OBJECTS
1156 ? "elf64-tradbigmips"
1157 : (HAVE_NEWABI
1158 ? "elf32-ntradbigmips" : "elf32-tradbigmips"))
1159 : (HAVE_64BIT_OBJECTS
1160 ? "elf64-tradlittlemips"
1161 : (HAVE_NEWABI
1162 ? "elf32-ntradlittlemips" : "elf32-tradlittlemips")));
e013f690
TS
1163#else
1164 return (target_big_endian
cfe86eaa
TS
1165 ? (HAVE_64BIT_OBJECTS
1166 ? "elf64-bigmips"
1167 : (HAVE_NEWABI
1168 ? "elf32-nbigmips" : "elf32-bigmips"))
1169 : (HAVE_64BIT_OBJECTS
1170 ? "elf64-littlemips"
1171 : (HAVE_NEWABI
1172 ? "elf32-nlittlemips" : "elf32-littlemips")));
e013f690
TS
1173#endif
1174 default:
1175 abort ();
1176 return NULL;
1177 }
1178}
1179
156c2f8b
NC
1180/* This function is called once, at assembler startup time. It should
1181 set up all the tables, etc. that the MD part of the assembler will need. */
1182
252b5132
RH
1183void
1184md_begin ()
1185{
252b5132 1186 register const char *retval = NULL;
156c2f8b 1187 int i = 0;
252b5132 1188 int broken = 0;
1f25f5d3 1189
ec68c924 1190 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, mips_arch))
252b5132
RH
1191 as_warn (_("Could not set architecture and machine"));
1192
252b5132
RH
1193 op_hash = hash_new ();
1194
1195 for (i = 0; i < NUMOPCODES;)
1196 {
1197 const char *name = mips_opcodes[i].name;
1198
1199 retval = hash_insert (op_hash, name, (PTR) &mips_opcodes[i]);
1200 if (retval != NULL)
1201 {
1202 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1203 mips_opcodes[i].name, retval);
1204 /* Probably a memory allocation problem? Give up now. */
1205 as_fatal (_("Broken assembler. No assembly attempted."));
1206 }
1207 do
1208 {
1209 if (mips_opcodes[i].pinfo != INSN_MACRO)
1210 {
1211 if (!validate_mips_insn (&mips_opcodes[i]))
1212 broken = 1;
1213 }
1214 ++i;
1215 }
1216 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1217 }
1218
1219 mips16_op_hash = hash_new ();
1220
1221 i = 0;
1222 while (i < bfd_mips16_num_opcodes)
1223 {
1224 const char *name = mips16_opcodes[i].name;
1225
1226 retval = hash_insert (mips16_op_hash, name, (PTR) &mips16_opcodes[i]);
1227 if (retval != NULL)
1228 as_fatal (_("internal: can't hash `%s': %s"),
1229 mips16_opcodes[i].name, retval);
1230 do
1231 {
1232 if (mips16_opcodes[i].pinfo != INSN_MACRO
1233 && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1234 != mips16_opcodes[i].match))
1235 {
1236 fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1237 mips16_opcodes[i].name, mips16_opcodes[i].args);
1238 broken = 1;
1239 }
1240 ++i;
1241 }
1242 while (i < bfd_mips16_num_opcodes
1243 && strcmp (mips16_opcodes[i].name, name) == 0);
1244 }
1245
1246 if (broken)
1247 as_fatal (_("Broken assembler. No assembly attempted."));
1248
1249 /* We add all the general register names to the symbol table. This
1250 helps us detect invalid uses of them. */
1251 for (i = 0; i < 32; i++)
1252 {
1253 char buf[5];
1254
1255 sprintf (buf, "$%d", i);
1256 symbol_table_insert (symbol_new (buf, reg_section, i,
1257 &zero_address_frag));
1258 }
76db943d
TS
1259 symbol_table_insert (symbol_new ("$ra", reg_section, RA,
1260 &zero_address_frag));
252b5132
RH
1261 symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1262 &zero_address_frag));
1263 symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1264 &zero_address_frag));
1265 symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1266 &zero_address_frag));
1267 symbol_table_insert (symbol_new ("$at", reg_section, AT,
1268 &zero_address_frag));
1269 symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1270 &zero_address_frag));
1271 symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1272 &zero_address_frag));
85b51719
TS
1273 symbol_table_insert (symbol_new ("$zero", reg_section, ZERO,
1274 &zero_address_frag));
252b5132
RH
1275 symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1276 &zero_address_frag));
1277
6047c971
AO
1278 /* If we don't add these register names to the symbol table, they
1279 may end up being added as regular symbols by operand(), and then
1280 make it to the object file as undefined in case they're not
1281 regarded as local symbols. They're local in o32, since `$' is a
1282 local symbol prefix, but not in n32 or n64. */
1283 for (i = 0; i < 8; i++)
1284 {
1285 char buf[6];
1286
1287 sprintf (buf, "$fcc%i", i);
1288 symbol_table_insert (symbol_new (buf, reg_section, -1,
1289 &zero_address_frag));
1290 }
1291
b34976b6 1292 mips_no_prev_insn (FALSE);
252b5132
RH
1293
1294 mips_gprmask = 0;
1295 mips_cprmask[0] = 0;
1296 mips_cprmask[1] = 0;
1297 mips_cprmask[2] = 0;
1298 mips_cprmask[3] = 0;
1299
1300 /* set the default alignment for the text section (2**2) */
1301 record_alignment (text_section, 2);
1302
1303 if (USE_GLOBAL_POINTER_OPT)
1304 bfd_set_gp_size (stdoutput, g_switch_value);
1305
1306 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1307 {
1308 /* On a native system, sections must be aligned to 16 byte
1309 boundaries. When configured for an embedded ELF target, we
1310 don't bother. */
1311 if (strcmp (TARGET_OS, "elf") != 0)
1312 {
1313 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1314 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1315 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1316 }
1317
1318 /* Create a .reginfo section for register masks and a .mdebug
1319 section for debugging information. */
1320 {
1321 segT seg;
1322 subsegT subseg;
1323 flagword flags;
1324 segT sec;
1325
1326 seg = now_seg;
1327 subseg = now_subseg;
1328
1329 /* The ABI says this section should be loaded so that the
1330 running program can access it. However, we don't load it
1331 if we are configured for an embedded target */
1332 flags = SEC_READONLY | SEC_DATA;
1333 if (strcmp (TARGET_OS, "elf") != 0)
1334 flags |= SEC_ALLOC | SEC_LOAD;
1335
316f5878 1336 if (mips_abi != N64_ABI)
252b5132
RH
1337 {
1338 sec = subseg_new (".reginfo", (subsegT) 0);
1339
195325d2
TS
1340 bfd_set_section_flags (stdoutput, sec, flags);
1341 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
bdaaa2e1 1342
252b5132
RH
1343#ifdef OBJ_ELF
1344 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1345#endif
1346 }
1347 else
1348 {
1349 /* The 64-bit ABI uses a .MIPS.options section rather than
1350 .reginfo section. */
1351 sec = subseg_new (".MIPS.options", (subsegT) 0);
195325d2
TS
1352 bfd_set_section_flags (stdoutput, sec, flags);
1353 bfd_set_section_alignment (stdoutput, sec, 3);
252b5132
RH
1354
1355#ifdef OBJ_ELF
1356 /* Set up the option header. */
1357 {
1358 Elf_Internal_Options opthdr;
1359 char *f;
1360
1361 opthdr.kind = ODK_REGINFO;
1362 opthdr.size = (sizeof (Elf_External_Options)
1363 + sizeof (Elf64_External_RegInfo));
1364 opthdr.section = 0;
1365 opthdr.info = 0;
1366 f = frag_more (sizeof (Elf_External_Options));
1367 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1368 (Elf_External_Options *) f);
1369
1370 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1371 }
1372#endif
1373 }
1374
1375 if (ECOFF_DEBUGGING)
1376 {
1377 sec = subseg_new (".mdebug", (subsegT) 0);
1378 (void) bfd_set_section_flags (stdoutput, sec,
1379 SEC_HAS_CONTENTS | SEC_READONLY);
1380 (void) bfd_set_section_alignment (stdoutput, sec, 2);
1381 }
ecb4347a
DJ
1382#ifdef OBJ_ELF
1383 else if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1384 {
1385 pdr_seg = subseg_new (".pdr", (subsegT) 0);
1386 (void) bfd_set_section_flags (stdoutput, pdr_seg,
1387 SEC_READONLY | SEC_RELOC
1388 | SEC_DEBUGGING);
1389 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1390 }
252b5132
RH
1391#endif
1392
1393 subseg_set (seg, subseg);
1394 }
1395 }
1396
1397 if (! ECOFF_DEBUGGING)
1398 md_obj_begin ();
1399}
1400
1401void
1402md_mips_end ()
1403{
1404 if (! ECOFF_DEBUGGING)
1405 md_obj_end ();
1406}
1407
1408void
1409md_assemble (str)
1410 char *str;
1411{
1412 struct mips_cl_insn insn;
f6688943
TS
1413 bfd_reloc_code_real_type unused_reloc[3]
1414 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132
RH
1415
1416 imm_expr.X_op = O_absent;
252b5132 1417 offset_expr.X_op = O_absent;
f6688943
TS
1418 imm_reloc[0] = BFD_RELOC_UNUSED;
1419 imm_reloc[1] = BFD_RELOC_UNUSED;
1420 imm_reloc[2] = BFD_RELOC_UNUSED;
1421 offset_reloc[0] = BFD_RELOC_UNUSED;
1422 offset_reloc[1] = BFD_RELOC_UNUSED;
1423 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
1424
1425 if (mips_opts.mips16)
1426 mips16_ip (str, &insn);
1427 else
1428 {
1429 mips_ip (str, &insn);
beae10d5
KH
1430 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1431 str, insn.insn_opcode));
252b5132
RH
1432 }
1433
1434 if (insn_error)
1435 {
1436 as_bad ("%s `%s'", insn_error, str);
1437 return;
1438 }
1439
1440 if (insn.insn_mo->pinfo == INSN_MACRO)
1441 {
1442 if (mips_opts.mips16)
1443 mips16_macro (&insn);
1444 else
1445 macro (&insn);
1446 }
1447 else
1448 {
1449 if (imm_expr.X_op != O_absent)
5e0116d5 1450 append_insn (NULL, &insn, &imm_expr, imm_reloc);
252b5132 1451 else if (offset_expr.X_op != O_absent)
5e0116d5 1452 append_insn (NULL, &insn, &offset_expr, offset_reloc);
252b5132 1453 else
5e0116d5 1454 append_insn (NULL, &insn, NULL, unused_reloc);
252b5132
RH
1455 }
1456}
1457
5919d012
RS
1458/* Return true if the given relocation might need a matching %lo().
1459 Note that R_MIPS_GOT16 relocations only need a matching %lo() when
1460 applied to local symbols. */
1461
1462static inline bfd_boolean
1463reloc_needs_lo_p (reloc)
1464 bfd_reloc_code_real_type reloc;
1465{
1466 return (reloc == BFD_RELOC_HI16_S
1467 || reloc == BFD_RELOC_MIPS_GOT16);
1468}
1469
1470/* Return true if the given fixup is followed by a matching R_MIPS_LO16
1471 relocation. */
1472
1473static inline bfd_boolean
1474fixup_has_matching_lo_p (fixp)
1475 fixS *fixp;
1476{
1477 return (fixp->fx_next != NULL
1478 && fixp->fx_next->fx_r_type == BFD_RELOC_LO16
1479 && fixp->fx_addsy == fixp->fx_next->fx_addsy
1480 && fixp->fx_offset == fixp->fx_next->fx_offset);
1481}
1482
252b5132
RH
1483/* See whether instruction IP reads register REG. CLASS is the type
1484 of register. */
1485
1486static int
1487insn_uses_reg (ip, reg, class)
1488 struct mips_cl_insn *ip;
1489 unsigned int reg;
1490 enum mips_regclass class;
1491{
1492 if (class == MIPS16_REG)
1493 {
1494 assert (mips_opts.mips16);
1495 reg = mips16_to_32_reg_map[reg];
1496 class = MIPS_GR_REG;
1497 }
1498
85b51719
TS
1499 /* Don't report on general register ZERO, since it never changes. */
1500 if (class == MIPS_GR_REG && reg == ZERO)
252b5132
RH
1501 return 0;
1502
1503 if (class == MIPS_FP_REG)
1504 {
1505 assert (! mips_opts.mips16);
1506 /* If we are called with either $f0 or $f1, we must check $f0.
1507 This is not optimal, because it will introduce an unnecessary
1508 NOP between "lwc1 $f0" and "swc1 $f1". To fix this we would
1509 need to distinguish reading both $f0 and $f1 or just one of
1510 them. Note that we don't have to check the other way,
1511 because there is no instruction that sets both $f0 and $f1
1512 and requires a delay. */
1513 if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1514 && ((((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS) &~(unsigned)1)
1515 == (reg &~ (unsigned) 1)))
1516 return 1;
1517 if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1518 && ((((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT) &~(unsigned)1)
1519 == (reg &~ (unsigned) 1)))
1520 return 1;
1521 }
1522 else if (! mips_opts.mips16)
1523 {
1524 if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1525 && ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == reg)
1526 return 1;
1527 if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1528 && ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT) == reg)
1529 return 1;
1530 }
1531 else
1532 {
1533 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1534 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RX)
1535 & MIPS16OP_MASK_RX)]
1536 == reg))
1537 return 1;
1538 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1539 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RY)
1540 & MIPS16OP_MASK_RY)]
1541 == reg))
1542 return 1;
1543 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1544 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
1545 & MIPS16OP_MASK_MOVE32Z)]
1546 == reg))
1547 return 1;
1548 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1549 return 1;
1550 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1551 return 1;
1552 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1553 return 1;
1554 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1555 && ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
1556 & MIPS16OP_MASK_REGR32) == reg)
1557 return 1;
1558 }
1559
1560 return 0;
1561}
1562
1563/* This function returns true if modifying a register requires a
1564 delay. */
1565
1566static int
1567reg_needs_delay (reg)
156c2f8b 1568 unsigned int reg;
252b5132
RH
1569{
1570 unsigned long prev_pinfo;
1571
1572 prev_pinfo = prev_insn.insn_mo->pinfo;
1573 if (! mips_opts.noreorder
9ce8a5dd 1574 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
1575 && ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1576 || (! gpr_interlocks
1577 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1578 {
1579 /* A load from a coprocessor or from memory. All load
1580 delays delay the use of general register rt for one
1581 instruction on the r3000. The r6000 and r4000 use
1582 interlocks. */
bdaaa2e1 1583 /* Itbl support may require additional care here. */
252b5132
RH
1584 know (prev_pinfo & INSN_WRITE_GPR_T);
1585 if (reg == ((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT))
1586 return 1;
1587 }
1588
1589 return 0;
1590}
1591
1592/* Mark instruction labels in mips16 mode. This permits the linker to
1593 handle them specially, such as generating jalx instructions when
1594 needed. We also make them odd for the duration of the assembly, in
1595 order to generate the right sort of code. We will make them even
1596 in the adjust_symtab routine, while leaving them marked. This is
1597 convenient for the debugger and the disassembler. The linker knows
1598 to make them odd again. */
1599
1600static void
1601mips16_mark_labels ()
1602{
1603 if (mips_opts.mips16)
1604 {
1605 struct insn_label_list *l;
98aa84af 1606 valueT val;
252b5132
RH
1607
1608 for (l = insn_labels; l != NULL; l = l->next)
1609 {
1610#ifdef OBJ_ELF
1611 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1612 S_SET_OTHER (l->label, STO_MIPS16);
1613#endif
98aa84af
AM
1614 val = S_GET_VALUE (l->label);
1615 if ((val & 1) == 0)
1616 S_SET_VALUE (l->label, val + 1);
252b5132
RH
1617 }
1618 }
1619}
1620
1621/* Output an instruction. PLACE is where to put the instruction; if
1622 it is NULL, this uses frag_more to get room. IP is the instruction
1623 information. ADDRESS_EXPR is an operand of the instruction to be
1624 used with RELOC_TYPE. */
1625
1626static void
5e0116d5 1627append_insn (place, ip, address_expr, reloc_type)
252b5132
RH
1628 char *place;
1629 struct mips_cl_insn *ip;
1630 expressionS *address_expr;
f6688943 1631 bfd_reloc_code_real_type *reloc_type;
252b5132
RH
1632{
1633 register unsigned long prev_pinfo, pinfo;
1634 char *f;
f6688943 1635 fixS *fixp[3];
252b5132 1636 int nops = 0;
f5040a92 1637 bfd_boolean force_new_frag = FALSE;
252b5132
RH
1638
1639 /* Mark instruction labels in mips16 mode. */
f9419b05 1640 mips16_mark_labels ();
252b5132
RH
1641
1642 prev_pinfo = prev_insn.insn_mo->pinfo;
1643 pinfo = ip->insn_mo->pinfo;
1644
1645 if (place == NULL && (! mips_opts.noreorder || prev_nop_frag != NULL))
1646 {
1647 int prev_prev_nop;
1648
1649 /* If the previous insn required any delay slots, see if we need
1650 to insert a NOP or two. There are eight kinds of possible
1651 hazards, of which an instruction can have at most one type.
1652 (1) a load from memory delay
1653 (2) a load from a coprocessor delay
1654 (3) an unconditional branch delay
1655 (4) a conditional branch delay
1656 (5) a move to coprocessor register delay
1657 (6) a load coprocessor register from memory delay
1658 (7) a coprocessor condition code delay
1659 (8) a HI/LO special register delay
1660
1661 There are a lot of optimizations we could do that we don't.
1662 In particular, we do not, in general, reorder instructions.
1663 If you use gcc with optimization, it will reorder
1664 instructions and generally do much more optimization then we
1665 do here; repeating all that work in the assembler would only
1666 benefit hand written assembly code, and does not seem worth
1667 it. */
1668
1669 /* This is how a NOP is emitted. */
1670#define emit_nop() \
1671 (mips_opts.mips16 \
1672 ? md_number_to_chars (frag_more (2), 0x6500, 2) \
1673 : md_number_to_chars (frag_more (4), 0, 4))
1674
1675 /* The previous insn might require a delay slot, depending upon
1676 the contents of the current insn. */
1677 if (! mips_opts.mips16
9ce8a5dd 1678 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
1679 && (((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1680 && ! cop_interlocks)
1681 || (! gpr_interlocks
1682 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1683 {
1684 /* A load from a coprocessor or from memory. All load
1685 delays delay the use of general register rt for one
1686 instruction on the r3000. The r6000 and r4000 use
1687 interlocks. */
beae10d5 1688 /* Itbl support may require additional care here. */
252b5132
RH
1689 know (prev_pinfo & INSN_WRITE_GPR_T);
1690 if (mips_optimize == 0
1691 || insn_uses_reg (ip,
1692 ((prev_insn.insn_opcode >> OP_SH_RT)
1693 & OP_MASK_RT),
1694 MIPS_GR_REG))
1695 ++nops;
1696 }
1697 else if (! mips_opts.mips16
9ce8a5dd 1698 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132 1699 && (((prev_pinfo & INSN_COPROC_MOVE_DELAY)
beae10d5 1700 && ! cop_interlocks)
e7af610e 1701 || (mips_opts.isa == ISA_MIPS1
252b5132
RH
1702 && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))))
1703 {
1704 /* A generic coprocessor delay. The previous instruction
1705 modified a coprocessor general or control register. If
1706 it modified a control register, we need to avoid any
1707 coprocessor instruction (this is probably not always
1708 required, but it sometimes is). If it modified a general
1709 register, we avoid using that register.
1710
1711 On the r6000 and r4000 loading a coprocessor register
1712 from memory is interlocked, and does not require a delay.
1713
1714 This case is not handled very well. There is no special
1715 knowledge of CP0 handling, and the coprocessors other
1716 than the floating point unit are not distinguished at
1717 all. */
1718 /* Itbl support may require additional care here. FIXME!
bdaaa2e1 1719 Need to modify this to include knowledge about
252b5132
RH
1720 user specified delays! */
1721 if (prev_pinfo & INSN_WRITE_FPR_T)
1722 {
1723 if (mips_optimize == 0
1724 || insn_uses_reg (ip,
1725 ((prev_insn.insn_opcode >> OP_SH_FT)
1726 & OP_MASK_FT),
1727 MIPS_FP_REG))
1728 ++nops;
1729 }
1730 else if (prev_pinfo & INSN_WRITE_FPR_S)
1731 {
1732 if (mips_optimize == 0
1733 || insn_uses_reg (ip,
1734 ((prev_insn.insn_opcode >> OP_SH_FS)
1735 & OP_MASK_FS),
1736 MIPS_FP_REG))
1737 ++nops;
1738 }
1739 else
1740 {
1741 /* We don't know exactly what the previous instruction
1742 does. If the current instruction uses a coprocessor
1743 register, we must insert a NOP. If previous
1744 instruction may set the condition codes, and the
1745 current instruction uses them, we must insert two
1746 NOPS. */
bdaaa2e1 1747 /* Itbl support may require additional care here. */
252b5132
RH
1748 if (mips_optimize == 0
1749 || ((prev_pinfo & INSN_WRITE_COND_CODE)
1750 && (pinfo & INSN_READ_COND_CODE)))
1751 nops += 2;
1752 else if (pinfo & INSN_COP)
1753 ++nops;
1754 }
1755 }
1756 else if (! mips_opts.mips16
9ce8a5dd 1757 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
1758 && (prev_pinfo & INSN_WRITE_COND_CODE)
1759 && ! cop_interlocks)
1760 {
1761 /* The previous instruction sets the coprocessor condition
1762 codes, but does not require a general coprocessor delay
1763 (this means it is a floating point comparison
1764 instruction). If this instruction uses the condition
1765 codes, we need to insert a single NOP. */
beae10d5 1766 /* Itbl support may require additional care here. */
252b5132
RH
1767 if (mips_optimize == 0
1768 || (pinfo & INSN_READ_COND_CODE))
1769 ++nops;
1770 }
6b76fefe
CM
1771
1772 /* If we're fixing up mfhi/mflo for the r7000 and the
1773 previous insn was an mfhi/mflo and the current insn
1774 reads the register that the mfhi/mflo wrote to, then
1775 insert two nops. */
1776
1777 else if (mips_7000_hilo_fix
1778 && MF_HILO_INSN (prev_pinfo)
1779 && insn_uses_reg (ip, ((prev_insn.insn_opcode >> OP_SH_RD)
beae10d5
KH
1780 & OP_MASK_RD),
1781 MIPS_GR_REG))
6b76fefe
CM
1782 {
1783 nops += 2;
1784 }
1785
1786 /* If we're fixing up mfhi/mflo for the r7000 and the
1787 2nd previous insn was an mfhi/mflo and the current insn
1788 reads the register that the mfhi/mflo wrote to, then
1789 insert one nop. */
1790
1791 else if (mips_7000_hilo_fix
1792 && MF_HILO_INSN (prev_prev_insn.insn_opcode)
1793 && insn_uses_reg (ip, ((prev_prev_insn.insn_opcode >> OP_SH_RD)
1794 & OP_MASK_RD),
1795 MIPS_GR_REG))
bdaaa2e1 1796
6b76fefe 1797 {
f9419b05 1798 ++nops;
6b76fefe 1799 }
bdaaa2e1 1800
252b5132
RH
1801 else if (prev_pinfo & INSN_READ_LO)
1802 {
1803 /* The previous instruction reads the LO register; if the
1804 current instruction writes to the LO register, we must
bdaaa2e1
KH
1805 insert two NOPS. Some newer processors have interlocks.
1806 Also the tx39's multiply instructions can be exectuted
252b5132 1807 immediatly after a read from HI/LO (without the delay),
bdaaa2e1
KH
1808 though the tx39's divide insns still do require the
1809 delay. */
252b5132 1810 if (! (hilo_interlocks
ec68c924 1811 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
252b5132
RH
1812 && (mips_optimize == 0
1813 || (pinfo & INSN_WRITE_LO)))
1814 nops += 2;
1815 /* Most mips16 branch insns don't have a delay slot.
1816 If a read from LO is immediately followed by a branch
1817 to a write to LO we have a read followed by a write
1818 less than 2 insns away. We assume the target of
1819 a branch might be a write to LO, and insert a nop
bdaaa2e1 1820 between a read and an immediately following branch. */
252b5132
RH
1821 else if (mips_opts.mips16
1822 && (mips_optimize == 0
1823 || (pinfo & MIPS16_INSN_BRANCH)))
f9419b05 1824 ++nops;
252b5132
RH
1825 }
1826 else if (prev_insn.insn_mo->pinfo & INSN_READ_HI)
1827 {
1828 /* The previous instruction reads the HI register; if the
1829 current instruction writes to the HI register, we must
1830 insert a NOP. Some newer processors have interlocks.
bdaaa2e1 1831 Also the note tx39's multiply above. */
252b5132 1832 if (! (hilo_interlocks
ec68c924 1833 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
252b5132
RH
1834 && (mips_optimize == 0
1835 || (pinfo & INSN_WRITE_HI)))
1836 nops += 2;
1837 /* Most mips16 branch insns don't have a delay slot.
1838 If a read from HI is immediately followed by a branch
1839 to a write to HI we have a read followed by a write
1840 less than 2 insns away. We assume the target of
1841 a branch might be a write to HI, and insert a nop
bdaaa2e1 1842 between a read and an immediately following branch. */
252b5132
RH
1843 else if (mips_opts.mips16
1844 && (mips_optimize == 0
1845 || (pinfo & MIPS16_INSN_BRANCH)))
f9419b05 1846 ++nops;
252b5132
RH
1847 }
1848
1849 /* If the previous instruction was in a noreorder section, then
1850 we don't want to insert the nop after all. */
bdaaa2e1 1851 /* Itbl support may require additional care here. */
252b5132
RH
1852 if (prev_insn_unreordered)
1853 nops = 0;
1854
1855 /* There are two cases which require two intervening
1856 instructions: 1) setting the condition codes using a move to
1857 coprocessor instruction which requires a general coprocessor
1858 delay and then reading the condition codes 2) reading the HI
1859 or LO register and then writing to it (except on processors
1860 which have interlocks). If we are not already emitting a NOP
1861 instruction, we must check for these cases compared to the
1862 instruction previous to the previous instruction. */
1863 if ((! mips_opts.mips16
9ce8a5dd 1864 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
1865 && (prev_prev_insn.insn_mo->pinfo & INSN_COPROC_MOVE_DELAY)
1866 && (prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
1867 && (pinfo & INSN_READ_COND_CODE)
1868 && ! cop_interlocks)
1869 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_LO)
1870 && (pinfo & INSN_WRITE_LO)
1871 && ! (hilo_interlocks
ec68c924 1872 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT))))
252b5132
RH
1873 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
1874 && (pinfo & INSN_WRITE_HI)
1875 && ! (hilo_interlocks
ec68c924 1876 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))))
252b5132
RH
1877 prev_prev_nop = 1;
1878 else
1879 prev_prev_nop = 0;
1880
1881 if (prev_prev_insn_unreordered)
1882 prev_prev_nop = 0;
1883
1884 if (prev_prev_nop && nops == 0)
1885 ++nops;
1886
60b63b72
RS
1887 if (mips_fix_4122_bugs && prev_insn.insn_mo->name)
1888 {
1889 /* We're out of bits in pinfo, so we must resort to string
1890 ops here. Shortcuts are selected based on opcodes being
1891 limited to the VR4122 instruction set. */
1892 int min_nops = 0;
1893 const char *pn = prev_insn.insn_mo->name;
1894 const char *tn = ip->insn_mo->name;
1895 if (strncmp(pn, "macc", 4) == 0
1896 || strncmp(pn, "dmacc", 5) == 0)
1897 {
1898 /* Errata 21 - [D]DIV[U] after [D]MACC */
1899 if (strstr (tn, "div"))
1900 {
1901 min_nops = 1;
1902 }
1903
1904 /* Errata 23 - Continuous DMULT[U]/DMACC instructions */
1905 if (pn[0] == 'd' /* dmacc */
1906 && (strncmp(tn, "dmult", 5) == 0
1907 || strncmp(tn, "dmacc", 5) == 0))
1908 {
1909 min_nops = 1;
1910 }
1911
1912 /* Errata 24 - MT{LO,HI} after [D]MACC */
1913 if (strcmp (tn, "mtlo") == 0
1914 || strcmp (tn, "mthi") == 0)
1915 {
1916 min_nops = 1;
1917 }
1918
1919 }
1920 else if (strncmp(pn, "dmult", 5) == 0
1921 && (strncmp(tn, "dmult", 5) == 0
1922 || strncmp(tn, "dmacc", 5) == 0))
1923 {
1924 /* Here is the rest of errata 23. */
1925 min_nops = 1;
1926 }
1927 if (nops < min_nops)
1928 nops = min_nops;
1929 }
1930
252b5132
RH
1931 /* If we are being given a nop instruction, don't bother with
1932 one of the nops we would otherwise output. This will only
1933 happen when a nop instruction is used with mips_optimize set
1934 to 0. */
1935 if (nops > 0
1936 && ! mips_opts.noreorder
156c2f8b 1937 && ip->insn_opcode == (unsigned) (mips_opts.mips16 ? 0x6500 : 0))
252b5132
RH
1938 --nops;
1939
1940 /* Now emit the right number of NOP instructions. */
1941 if (nops > 0 && ! mips_opts.noreorder)
1942 {
1943 fragS *old_frag;
1944 unsigned long old_frag_offset;
1945 int i;
1946 struct insn_label_list *l;
1947
1948 old_frag = frag_now;
1949 old_frag_offset = frag_now_fix ();
1950
1951 for (i = 0; i < nops; i++)
1952 emit_nop ();
1953
1954 if (listing)
1955 {
1956 listing_prev_line ();
1957 /* We may be at the start of a variant frag. In case we
1958 are, make sure there is enough space for the frag
1959 after the frags created by listing_prev_line. The
1960 argument to frag_grow here must be at least as large
1961 as the argument to all other calls to frag_grow in
1962 this file. We don't have to worry about being in the
1963 middle of a variant frag, because the variants insert
1964 all needed nop instructions themselves. */
1965 frag_grow (40);
1966 }
1967
1968 for (l = insn_labels; l != NULL; l = l->next)
1969 {
98aa84af
AM
1970 valueT val;
1971
252b5132 1972 assert (S_GET_SEGMENT (l->label) == now_seg);
49309057 1973 symbol_set_frag (l->label, frag_now);
98aa84af 1974 val = (valueT) frag_now_fix ();
252b5132
RH
1975 /* mips16 text labels are stored as odd. */
1976 if (mips_opts.mips16)
f9419b05 1977 ++val;
98aa84af 1978 S_SET_VALUE (l->label, val);
252b5132
RH
1979 }
1980
1981#ifndef NO_ECOFF_DEBUGGING
1982 if (ECOFF_DEBUGGING)
1983 ecoff_fix_loc (old_frag, old_frag_offset);
1984#endif
1985 }
1986 else if (prev_nop_frag != NULL)
1987 {
1988 /* We have a frag holding nops we may be able to remove. If
1989 we don't need any nops, we can decrease the size of
1990 prev_nop_frag by the size of one instruction. If we do
bdaaa2e1 1991 need some nops, we count them in prev_nops_required. */
252b5132
RH
1992 if (prev_nop_frag_since == 0)
1993 {
1994 if (nops == 0)
1995 {
1996 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1997 --prev_nop_frag_holds;
1998 }
1999 else
2000 prev_nop_frag_required += nops;
2001 }
2002 else
2003 {
2004 if (prev_prev_nop == 0)
2005 {
2006 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
2007 --prev_nop_frag_holds;
2008 }
2009 else
2010 ++prev_nop_frag_required;
2011 }
2012
2013 if (prev_nop_frag_holds <= prev_nop_frag_required)
2014 prev_nop_frag = NULL;
2015
2016 ++prev_nop_frag_since;
2017
2018 /* Sanity check: by the time we reach the second instruction
2019 after prev_nop_frag, we should have used up all the nops
2020 one way or another. */
2021 assert (prev_nop_frag_since <= 1 || prev_nop_frag == NULL);
2022 }
2023 }
2024
4a6a3df4
AO
2025 if (place == NULL
2026 && address_expr
0b25d3e6 2027 && *reloc_type == BFD_RELOC_16_PCREL_S2
4a6a3df4
AO
2028 && (pinfo & INSN_UNCOND_BRANCH_DELAY || pinfo & INSN_COND_BRANCH_DELAY
2029 || pinfo & INSN_COND_BRANCH_LIKELY)
2030 && mips_relax_branch
2031 /* Don't try branch relaxation within .set nomacro, or within
2032 .set noat if we use $at for PIC computations. If it turns
2033 out that the branch was out-of-range, we'll get an error. */
2034 && !mips_opts.warn_about_macros
2035 && !(mips_opts.noat && mips_pic != NO_PIC)
2036 && !mips_opts.mips16)
2037 {
2038 f = frag_var (rs_machine_dependent,
2039 relaxed_branch_length
2040 (NULL, NULL,
2041 (pinfo & INSN_UNCOND_BRANCH_DELAY) ? -1
2042 : (pinfo & INSN_COND_BRANCH_LIKELY) ? 1 : 0), 4,
2043 RELAX_BRANCH_ENCODE
af6ae2ad 2044 (pinfo & INSN_UNCOND_BRANCH_DELAY,
4a6a3df4
AO
2045 pinfo & INSN_COND_BRANCH_LIKELY,
2046 pinfo & INSN_WRITE_GPR_31,
2047 0),
2048 address_expr->X_add_symbol,
2049 address_expr->X_add_number,
2050 0);
2051 *reloc_type = BFD_RELOC_UNUSED;
2052 }
2053 else if (*reloc_type > BFD_RELOC_UNUSED)
252b5132
RH
2054 {
2055 /* We need to set up a variant frag. */
2056 assert (mips_opts.mips16 && address_expr != NULL);
2057 f = frag_var (rs_machine_dependent, 4, 0,
f6688943 2058 RELAX_MIPS16_ENCODE (*reloc_type - BFD_RELOC_UNUSED,
252b5132
RH
2059 mips16_small, mips16_ext,
2060 (prev_pinfo
2061 & INSN_UNCOND_BRANCH_DELAY),
f6688943 2062 (*prev_insn_reloc_type
252b5132 2063 == BFD_RELOC_MIPS16_JMP)),
c4e7957c 2064 make_expr_symbol (address_expr), 0, NULL);
252b5132
RH
2065 }
2066 else if (place != NULL)
2067 f = place;
2068 else if (mips_opts.mips16
2069 && ! ip->use_extend
f6688943 2070 && *reloc_type != BFD_RELOC_MIPS16_JMP)
252b5132
RH
2071 {
2072 /* Make sure there is enough room to swap this instruction with
2073 a following jump instruction. */
2074 frag_grow (6);
2075 f = frag_more (2);
2076 }
2077 else
2078 {
2079 if (mips_opts.mips16
2080 && mips_opts.noreorder
2081 && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
2082 as_warn (_("extended instruction in delay slot"));
2083
2084 f = frag_more (4);
2085 }
2086
f6688943
TS
2087 fixp[0] = fixp[1] = fixp[2] = NULL;
2088 if (address_expr != NULL && *reloc_type < BFD_RELOC_UNUSED)
252b5132
RH
2089 {
2090 if (address_expr->X_op == O_constant)
2091 {
4db1a35d 2092 valueT tmp;
f6688943
TS
2093
2094 switch (*reloc_type)
252b5132
RH
2095 {
2096 case BFD_RELOC_32:
2097 ip->insn_opcode |= address_expr->X_add_number;
2098 break;
2099
f6688943
TS
2100 case BFD_RELOC_MIPS_HIGHEST:
2101 tmp = (address_expr->X_add_number + 0x800080008000) >> 16;
2102 tmp >>= 16;
2103 ip->insn_opcode |= (tmp >> 16) & 0xffff;
2104 break;
2105
2106 case BFD_RELOC_MIPS_HIGHER:
2107 tmp = (address_expr->X_add_number + 0x80008000) >> 16;
2108 ip->insn_opcode |= (tmp >> 16) & 0xffff;
2109 break;
2110
2111 case BFD_RELOC_HI16_S:
2112 ip->insn_opcode |= ((address_expr->X_add_number + 0x8000)
2113 >> 16) & 0xffff;
2114 break;
2115
2116 case BFD_RELOC_HI16:
2117 ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
2118 break;
2119
252b5132 2120 case BFD_RELOC_LO16:
ed6fb7bd 2121 case BFD_RELOC_MIPS_GOT_DISP:
252b5132
RH
2122 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2123 break;
2124
2125 case BFD_RELOC_MIPS_JMP:
2126 if ((address_expr->X_add_number & 3) != 0)
2127 as_bad (_("jump to misaligned address (0x%lx)"),
2128 (unsigned long) address_expr->X_add_number);
f3c0ec86 2129 if (address_expr->X_add_number & ~0xfffffff)
7496292d
TS
2130 as_bad (_("jump address range overflow (0x%lx)"),
2131 (unsigned long) address_expr->X_add_number);
252b5132
RH
2132 ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
2133 break;
2134
2135 case BFD_RELOC_MIPS16_JMP:
2136 if ((address_expr->X_add_number & 3) != 0)
2137 as_bad (_("jump to misaligned address (0x%lx)"),
2138 (unsigned long) address_expr->X_add_number);
f3c0ec86 2139 if (address_expr->X_add_number & ~0xfffffff)
7496292d
TS
2140 as_bad (_("jump address range overflow (0x%lx)"),
2141 (unsigned long) address_expr->X_add_number);
252b5132
RH
2142 ip->insn_opcode |=
2143 (((address_expr->X_add_number & 0x7c0000) << 3)
2144 | ((address_expr->X_add_number & 0xf800000) >> 7)
2145 | ((address_expr->X_add_number & 0x3fffc) >> 2));
2146 break;
2147
252b5132
RH
2148 case BFD_RELOC_16_PCREL_S2:
2149 goto need_reloc;
2150
2151 default:
2152 internalError ();
2153 }
2154 }
2155 else
2156 {
2157 need_reloc:
f6688943 2158 /* Don't generate a reloc if we are writing into a variant frag. */
252b5132
RH
2159 if (place == NULL)
2160 {
f6688943
TS
2161 fixp[0] = fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
2162 address_expr,
0b25d3e6 2163 *reloc_type == BFD_RELOC_16_PCREL_S2,
f6688943
TS
2164 reloc_type[0]);
2165
b6ff326e 2166 /* These relocations can have an addend that won't fit in
f6688943
TS
2167 4 octets for 64bit assembly. */
2168 if (HAVE_64BIT_GPRS &&
2169 (*reloc_type == BFD_RELOC_16
98d3f06f
KH
2170 || *reloc_type == BFD_RELOC_32
2171 || *reloc_type == BFD_RELOC_MIPS_JMP
2172 || *reloc_type == BFD_RELOC_HI16_S
2173 || *reloc_type == BFD_RELOC_LO16
2174 || *reloc_type == BFD_RELOC_GPREL16
2175 || *reloc_type == BFD_RELOC_MIPS_LITERAL
2176 || *reloc_type == BFD_RELOC_GPREL32
2177 || *reloc_type == BFD_RELOC_64
2178 || *reloc_type == BFD_RELOC_CTOR
2179 || *reloc_type == BFD_RELOC_MIPS_SUB
2180 || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2181 || *reloc_type == BFD_RELOC_MIPS_HIGHER
2182 || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2183 || *reloc_type == BFD_RELOC_MIPS_REL16
2184 || *reloc_type == BFD_RELOC_MIPS_RELGOT))
f6688943
TS
2185 fixp[0]->fx_no_overflow = 1;
2186
5919d012 2187 if (reloc_needs_lo_p (*reloc_type))
252b5132
RH
2188 {
2189 struct mips_hi_fixup *hi_fixup;
2190
5919d012
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;
2200 }
f6688943 2201 hi_fixup->fixp = fixp[0];
252b5132 2202 hi_fixup->seg = now_seg;
252b5132 2203 }
f6688943
TS
2204
2205 if (reloc_type[1] != BFD_RELOC_UNUSED)
2206 {
2207 /* FIXME: This symbol can be one of
2208 RSS_UNDEF, RSS_GP, RSS_GP0, RSS_LOC. */
2209 address_expr->X_op = O_absent;
2210 address_expr->X_add_symbol = 0;
2211 address_expr->X_add_number = 0;
2212
2213 fixp[1] = fix_new_exp (frag_now, f - frag_now->fr_literal,
b34976b6 2214 4, address_expr, FALSE,
f6688943
TS
2215 reloc_type[1]);
2216
b6ff326e 2217 /* These relocations can have an addend that won't fit in
f6688943
TS
2218 4 octets for 64bit assembly. */
2219 if (HAVE_64BIT_GPRS &&
2220 (*reloc_type == BFD_RELOC_16
2221 || *reloc_type == BFD_RELOC_32
2222 || *reloc_type == BFD_RELOC_MIPS_JMP
2223 || *reloc_type == BFD_RELOC_HI16_S
2224 || *reloc_type == BFD_RELOC_LO16
2225 || *reloc_type == BFD_RELOC_GPREL16
2226 || *reloc_type == BFD_RELOC_MIPS_LITERAL
2227 || *reloc_type == BFD_RELOC_GPREL32
2228 || *reloc_type == BFD_RELOC_64
2229 || *reloc_type == BFD_RELOC_CTOR
2230 || *reloc_type == BFD_RELOC_MIPS_SUB
2231 || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2232 || *reloc_type == BFD_RELOC_MIPS_HIGHER
2233 || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2234 || *reloc_type == BFD_RELOC_MIPS_REL16
2235 || *reloc_type == BFD_RELOC_MIPS_RELGOT))
98d3f06f 2236 fixp[1]->fx_no_overflow = 1;
f6688943
TS
2237
2238 if (reloc_type[2] != BFD_RELOC_UNUSED)
2239 {
2240 address_expr->X_op = O_absent;
2241 address_expr->X_add_symbol = 0;
2242 address_expr->X_add_number = 0;
2243
2244 fixp[2] = fix_new_exp (frag_now,
2245 f - frag_now->fr_literal, 4,
b34976b6 2246 address_expr, FALSE,
f6688943
TS
2247 reloc_type[2]);
2248
b6ff326e 2249 /* These relocations can have an addend that won't fit in
f6688943
TS
2250 4 octets for 64bit assembly. */
2251 if (HAVE_64BIT_GPRS &&
2252 (*reloc_type == BFD_RELOC_16
2253 || *reloc_type == BFD_RELOC_32
2254 || *reloc_type == BFD_RELOC_MIPS_JMP
2255 || *reloc_type == BFD_RELOC_HI16_S
2256 || *reloc_type == BFD_RELOC_LO16
2257 || *reloc_type == BFD_RELOC_GPREL16
2258 || *reloc_type == BFD_RELOC_MIPS_LITERAL
2259 || *reloc_type == BFD_RELOC_GPREL32
2260 || *reloc_type == BFD_RELOC_64
2261 || *reloc_type == BFD_RELOC_CTOR
2262 || *reloc_type == BFD_RELOC_MIPS_SUB
2263 || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2264 || *reloc_type == BFD_RELOC_MIPS_HIGHER
2265 || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2266 || *reloc_type == BFD_RELOC_MIPS_REL16
2267 || *reloc_type == BFD_RELOC_MIPS_RELGOT))
98d3f06f 2268 fixp[2]->fx_no_overflow = 1;
f6688943
TS
2269 }
2270 }
252b5132
RH
2271 }
2272 }
2273 }
2274
2275 if (! mips_opts.mips16)
c5dd6aab
DJ
2276 {
2277 md_number_to_chars (f, ip->insn_opcode, 4);
2278#ifdef OBJ_ELF
2279 dwarf2_emit_insn (4);
2280#endif
2281 }
f6688943 2282 else if (*reloc_type == BFD_RELOC_MIPS16_JMP)
252b5132
RH
2283 {
2284 md_number_to_chars (f, ip->insn_opcode >> 16, 2);
2285 md_number_to_chars (f + 2, ip->insn_opcode & 0xffff, 2);
c5dd6aab
DJ
2286#ifdef OBJ_ELF
2287 dwarf2_emit_insn (4);
2288#endif
252b5132
RH
2289 }
2290 else
2291 {
2292 if (ip->use_extend)
2293 {
2294 md_number_to_chars (f, 0xf000 | ip->extend, 2);
2295 f += 2;
2296 }
2297 md_number_to_chars (f, ip->insn_opcode, 2);
c5dd6aab
DJ
2298#ifdef OBJ_ELF
2299 dwarf2_emit_insn (ip->use_extend ? 4 : 2);
2300#endif
252b5132
RH
2301 }
2302
2303 /* Update the register mask information. */
2304 if (! mips_opts.mips16)
2305 {
2306 if (pinfo & INSN_WRITE_GPR_D)
2307 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
2308 if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2309 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
2310 if (pinfo & INSN_READ_GPR_S)
2311 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
2312 if (pinfo & INSN_WRITE_GPR_31)
f9419b05 2313 mips_gprmask |= 1 << RA;
252b5132
RH
2314 if (pinfo & INSN_WRITE_FPR_D)
2315 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
2316 if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2317 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
2318 if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2319 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
2320 if ((pinfo & INSN_READ_FPR_R) != 0)
2321 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
2322 if (pinfo & INSN_COP)
2323 {
bdaaa2e1
KH
2324 /* We don't keep enough information to sort these cases out.
2325 The itbl support does keep this information however, although
2326 we currently don't support itbl fprmats as part of the cop
2327 instruction. May want to add this support in the future. */
252b5132
RH
2328 }
2329 /* Never set the bit for $0, which is always zero. */
beae10d5 2330 mips_gprmask &= ~1 << 0;
252b5132
RH
2331 }
2332 else
2333 {
2334 if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2335 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RX)
2336 & MIPS16OP_MASK_RX);
2337 if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2338 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RY)
2339 & MIPS16OP_MASK_RY);
2340 if (pinfo & MIPS16_INSN_WRITE_Z)
2341 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RZ)
2342 & MIPS16OP_MASK_RZ);
2343 if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2344 mips_gprmask |= 1 << TREG;
2345 if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2346 mips_gprmask |= 1 << SP;
2347 if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2348 mips_gprmask |= 1 << RA;
2349 if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2350 mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2351 if (pinfo & MIPS16_INSN_READ_Z)
2352 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
2353 & MIPS16OP_MASK_MOVE32Z);
2354 if (pinfo & MIPS16_INSN_READ_GPR_X)
2355 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
2356 & MIPS16OP_MASK_REGR32);
2357 }
2358
2359 if (place == NULL && ! mips_opts.noreorder)
2360 {
2361 /* Filling the branch delay slot is more complex. We try to
2362 switch the branch with the previous instruction, which we can
2363 do if the previous instruction does not set up a condition
2364 that the branch tests and if the branch is not itself the
2365 target of any branch. */
2366 if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2367 || (pinfo & INSN_COND_BRANCH_DELAY))
2368 {
2369 if (mips_optimize < 2
2370 /* If we have seen .set volatile or .set nomove, don't
2371 optimize. */
2372 || mips_opts.nomove != 0
2373 /* If we had to emit any NOP instructions, then we
2374 already know we can not swap. */
2375 || nops != 0
2376 /* If we don't even know the previous insn, we can not
bdaaa2e1 2377 swap. */
252b5132
RH
2378 || ! prev_insn_valid
2379 /* If the previous insn is already in a branch delay
2380 slot, then we can not swap. */
2381 || prev_insn_is_delay_slot
2382 /* If the previous previous insn was in a .set
2383 noreorder, we can't swap. Actually, the MIPS
2384 assembler will swap in this situation. However, gcc
2385 configured -with-gnu-as will generate code like
2386 .set noreorder
2387 lw $4,XXX
2388 .set reorder
2389 INSN
2390 bne $4,$0,foo
2391 in which we can not swap the bne and INSN. If gcc is
2392 not configured -with-gnu-as, it does not output the
2393 .set pseudo-ops. We don't have to check
2394 prev_insn_unreordered, because prev_insn_valid will
2395 be 0 in that case. We don't want to use
2396 prev_prev_insn_valid, because we do want to be able
2397 to swap at the start of a function. */
2398 || prev_prev_insn_unreordered
2399 /* If the branch is itself the target of a branch, we
2400 can not swap. We cheat on this; all we check for is
2401 whether there is a label on this instruction. If
2402 there are any branches to anything other than a
2403 label, users must use .set noreorder. */
2404 || insn_labels != NULL
2405 /* If the previous instruction is in a variant frag, we
2406 can not do the swap. This does not apply to the
2407 mips16, which uses variant frags for different
2408 purposes. */
2409 || (! mips_opts.mips16
2410 && prev_insn_frag->fr_type == rs_machine_dependent)
2411 /* If the branch reads the condition codes, we don't
2412 even try to swap, because in the sequence
2413 ctc1 $X,$31
2414 INSN
2415 INSN
2416 bc1t LABEL
2417 we can not swap, and I don't feel like handling that
2418 case. */
2419 || (! mips_opts.mips16
9ce8a5dd 2420 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
2421 && (pinfo & INSN_READ_COND_CODE))
2422 /* We can not swap with an instruction that requires a
2423 delay slot, becase the target of the branch might
2424 interfere with that instruction. */
2425 || (! mips_opts.mips16
9ce8a5dd 2426 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132 2427 && (prev_pinfo
bdaaa2e1 2428 /* Itbl support may require additional care here. */
252b5132
RH
2429 & (INSN_LOAD_COPROC_DELAY
2430 | INSN_COPROC_MOVE_DELAY
2431 | INSN_WRITE_COND_CODE)))
2432 || (! (hilo_interlocks
ec68c924 2433 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
252b5132
RH
2434 && (prev_pinfo
2435 & (INSN_READ_LO
2436 | INSN_READ_HI)))
2437 || (! mips_opts.mips16
2438 && ! gpr_interlocks
2439 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))
2440 || (! mips_opts.mips16
e7af610e 2441 && mips_opts.isa == ISA_MIPS1
bdaaa2e1 2442 /* Itbl support may require additional care here. */
252b5132
RH
2443 && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))
2444 /* We can not swap with a branch instruction. */
2445 || (prev_pinfo
2446 & (INSN_UNCOND_BRANCH_DELAY
2447 | INSN_COND_BRANCH_DELAY
2448 | INSN_COND_BRANCH_LIKELY))
2449 /* We do not swap with a trap instruction, since it
2450 complicates trap handlers to have the trap
2451 instruction be in a delay slot. */
2452 || (prev_pinfo & INSN_TRAP)
2453 /* If the branch reads a register that the previous
2454 instruction sets, we can not swap. */
2455 || (! mips_opts.mips16
2456 && (prev_pinfo & INSN_WRITE_GPR_T)
2457 && insn_uses_reg (ip,
2458 ((prev_insn.insn_opcode >> OP_SH_RT)
2459 & OP_MASK_RT),
2460 MIPS_GR_REG))
2461 || (! mips_opts.mips16
2462 && (prev_pinfo & INSN_WRITE_GPR_D)
2463 && insn_uses_reg (ip,
2464 ((prev_insn.insn_opcode >> OP_SH_RD)
2465 & OP_MASK_RD),
2466 MIPS_GR_REG))
2467 || (mips_opts.mips16
2468 && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2469 && insn_uses_reg (ip,
2470 ((prev_insn.insn_opcode
2471 >> MIPS16OP_SH_RX)
2472 & MIPS16OP_MASK_RX),
2473 MIPS16_REG))
2474 || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2475 && insn_uses_reg (ip,
2476 ((prev_insn.insn_opcode
2477 >> MIPS16OP_SH_RY)
2478 & MIPS16OP_MASK_RY),
2479 MIPS16_REG))
2480 || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2481 && insn_uses_reg (ip,
2482 ((prev_insn.insn_opcode
2483 >> MIPS16OP_SH_RZ)
2484 & MIPS16OP_MASK_RZ),
2485 MIPS16_REG))
2486 || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2487 && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2488 || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2489 && insn_uses_reg (ip, RA, MIPS_GR_REG))
2490 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2491 && insn_uses_reg (ip,
2492 MIPS16OP_EXTRACT_REG32R (prev_insn.
2493 insn_opcode),
2494 MIPS_GR_REG))))
2495 /* If the branch writes a register that the previous
2496 instruction sets, we can not swap (we know that
2497 branches write only to RD or to $31). */
2498 || (! mips_opts.mips16
2499 && (prev_pinfo & INSN_WRITE_GPR_T)
2500 && (((pinfo & INSN_WRITE_GPR_D)
2501 && (((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT)
2502 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2503 || ((pinfo & INSN_WRITE_GPR_31)
2504 && (((prev_insn.insn_opcode >> OP_SH_RT)
2505 & OP_MASK_RT)
f9419b05 2506 == RA))))
252b5132
RH
2507 || (! mips_opts.mips16
2508 && (prev_pinfo & INSN_WRITE_GPR_D)
2509 && (((pinfo & INSN_WRITE_GPR_D)
2510 && (((prev_insn.insn_opcode >> OP_SH_RD) & OP_MASK_RD)
2511 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2512 || ((pinfo & INSN_WRITE_GPR_31)
2513 && (((prev_insn.insn_opcode >> OP_SH_RD)
2514 & OP_MASK_RD)
f9419b05 2515 == RA))))
252b5132
RH
2516 || (mips_opts.mips16
2517 && (pinfo & MIPS16_INSN_WRITE_31)
2518 && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2519 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2520 && (MIPS16OP_EXTRACT_REG32R (prev_insn.insn_opcode)
2521 == RA))))
2522 /* If the branch writes a register that the previous
2523 instruction reads, we can not swap (we know that
2524 branches only write to RD or to $31). */
2525 || (! mips_opts.mips16
2526 && (pinfo & INSN_WRITE_GPR_D)
2527 && insn_uses_reg (&prev_insn,
2528 ((ip->insn_opcode >> OP_SH_RD)
2529 & OP_MASK_RD),
2530 MIPS_GR_REG))
2531 || (! mips_opts.mips16
2532 && (pinfo & INSN_WRITE_GPR_31)
f9419b05 2533 && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
252b5132
RH
2534 || (mips_opts.mips16
2535 && (pinfo & MIPS16_INSN_WRITE_31)
2536 && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2537 /* If we are generating embedded PIC code, the branch
2538 might be expanded into a sequence which uses $at, so
2539 we can't swap with an instruction which reads it. */
2540 || (mips_pic == EMBEDDED_PIC
2541 && insn_uses_reg (&prev_insn, AT, MIPS_GR_REG))
2542 /* If the previous previous instruction has a load
2543 delay, and sets a register that the branch reads, we
2544 can not swap. */
2545 || (! mips_opts.mips16
9ce8a5dd 2546 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
bdaaa2e1 2547 /* Itbl support may require additional care here. */
252b5132
RH
2548 && ((prev_prev_insn.insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
2549 || (! gpr_interlocks
2550 && (prev_prev_insn.insn_mo->pinfo
2551 & INSN_LOAD_MEMORY_DELAY)))
2552 && insn_uses_reg (ip,
2553 ((prev_prev_insn.insn_opcode >> OP_SH_RT)
2554 & OP_MASK_RT),
2555 MIPS_GR_REG))
2556 /* If one instruction sets a condition code and the
2557 other one uses a condition code, we can not swap. */
2558 || ((pinfo & INSN_READ_COND_CODE)
2559 && (prev_pinfo & INSN_WRITE_COND_CODE))
2560 || ((pinfo & INSN_WRITE_COND_CODE)
2561 && (prev_pinfo & INSN_READ_COND_CODE))
2562 /* If the previous instruction uses the PC, we can not
2563 swap. */
2564 || (mips_opts.mips16
2565 && (prev_pinfo & MIPS16_INSN_READ_PC))
2566 /* If the previous instruction was extended, we can not
2567 swap. */
2568 || (mips_opts.mips16 && prev_insn_extended)
2569 /* If the previous instruction had a fixup in mips16
2570 mode, we can not swap. This normally means that the
2571 previous instruction was a 4 byte branch anyhow. */
f6688943 2572 || (mips_opts.mips16 && prev_insn_fixp[0])
bdaaa2e1
KH
2573 /* If the previous instruction is a sync, sync.l, or
2574 sync.p, we can not swap. */
f173e82e 2575 || (prev_pinfo & INSN_SYNC))
252b5132
RH
2576 {
2577 /* We could do even better for unconditional branches to
2578 portions of this object file; we could pick up the
2579 instruction at the destination, put it in the delay
2580 slot, and bump the destination address. */
2581 emit_nop ();
2582 /* Update the previous insn information. */
2583 prev_prev_insn = *ip;
2584 prev_insn.insn_mo = &dummy_opcode;
2585 }
2586 else
2587 {
2588 /* It looks like we can actually do the swap. */
2589 if (! mips_opts.mips16)
2590 {
2591 char *prev_f;
2592 char temp[4];
2593
2594 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2595 memcpy (temp, prev_f, 4);
2596 memcpy (prev_f, f, 4);
2597 memcpy (f, temp, 4);
f6688943
TS
2598 if (prev_insn_fixp[0])
2599 {
2600 prev_insn_fixp[0]->fx_frag = frag_now;
2601 prev_insn_fixp[0]->fx_where = f - frag_now->fr_literal;
2602 }
2603 if (prev_insn_fixp[1])
2604 {
2605 prev_insn_fixp[1]->fx_frag = frag_now;
2606 prev_insn_fixp[1]->fx_where = f - frag_now->fr_literal;
2607 }
2608 if (prev_insn_fixp[2])
252b5132 2609 {
f6688943
TS
2610 prev_insn_fixp[2]->fx_frag = frag_now;
2611 prev_insn_fixp[2]->fx_where = f - frag_now->fr_literal;
252b5132 2612 }
f5040a92
AO
2613 if (prev_insn_fixp[0] && HAVE_NEWABI
2614 && prev_insn_frag != frag_now
2615 && (prev_insn_fixp[0]->fx_r_type
2616 == BFD_RELOC_MIPS_GOT_DISP
2617 || (prev_insn_fixp[0]->fx_r_type
2618 == BFD_RELOC_MIPS_CALL16)))
2619 {
2620 /* To avoid confusion in tc_gen_reloc, we must
2621 ensure that this does not become a variant
2622 frag. */
2623 force_new_frag = TRUE;
2624 }
f6688943 2625 if (fixp[0])
252b5132 2626 {
f6688943
TS
2627 fixp[0]->fx_frag = prev_insn_frag;
2628 fixp[0]->fx_where = prev_insn_where;
2629 }
2630 if (fixp[1])
2631 {
2632 fixp[1]->fx_frag = prev_insn_frag;
2633 fixp[1]->fx_where = prev_insn_where;
2634 }
2635 if (fixp[2])
2636 {
2637 fixp[2]->fx_frag = prev_insn_frag;
2638 fixp[2]->fx_where = prev_insn_where;
252b5132
RH
2639 }
2640 }
2641 else
2642 {
2643 char *prev_f;
2644 char temp[2];
2645
f6688943
TS
2646 assert (prev_insn_fixp[0] == NULL);
2647 assert (prev_insn_fixp[1] == NULL);
2648 assert (prev_insn_fixp[2] == NULL);
252b5132
RH
2649 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2650 memcpy (temp, prev_f, 2);
2651 memcpy (prev_f, f, 2);
f6688943 2652 if (*reloc_type != BFD_RELOC_MIPS16_JMP)
252b5132 2653 {
f6688943 2654 assert (*reloc_type == BFD_RELOC_UNUSED);
252b5132
RH
2655 memcpy (f, temp, 2);
2656 }
2657 else
2658 {
2659 memcpy (f, f + 2, 2);
2660 memcpy (f + 2, temp, 2);
2661 }
f6688943
TS
2662 if (fixp[0])
2663 {
2664 fixp[0]->fx_frag = prev_insn_frag;
2665 fixp[0]->fx_where = prev_insn_where;
2666 }
2667 if (fixp[1])
2668 {
2669 fixp[1]->fx_frag = prev_insn_frag;
2670 fixp[1]->fx_where = prev_insn_where;
2671 }
2672 if (fixp[2])
252b5132 2673 {
f6688943
TS
2674 fixp[2]->fx_frag = prev_insn_frag;
2675 fixp[2]->fx_where = prev_insn_where;
252b5132
RH
2676 }
2677 }
2678
2679 /* Update the previous insn information; leave prev_insn
2680 unchanged. */
2681 prev_prev_insn = *ip;
2682 }
2683 prev_insn_is_delay_slot = 1;
2684
2685 /* If that was an unconditional branch, forget the previous
2686 insn information. */
2687 if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2688 {
2689 prev_prev_insn.insn_mo = &dummy_opcode;
2690 prev_insn.insn_mo = &dummy_opcode;
2691 }
2692
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 if (pinfo & INSN_COND_BRANCH_LIKELY)
2702 {
2703 /* We don't yet optimize a branch likely. What we should do
2704 is look at the target, copy the instruction found there
2705 into the delay slot, and increment the branch to jump to
2706 the next instruction. */
2707 emit_nop ();
2708 /* Update the previous insn information. */
2709 prev_prev_insn = *ip;
2710 prev_insn.insn_mo = &dummy_opcode;
f6688943
TS
2711 prev_insn_fixp[0] = NULL;
2712 prev_insn_fixp[1] = NULL;
2713 prev_insn_fixp[2] = NULL;
2714 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2715 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2716 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
252b5132
RH
2717 prev_insn_extended = 0;
2718 }
2719 else
2720 {
2721 /* Update the previous insn information. */
2722 if (nops > 0)
2723 prev_prev_insn.insn_mo = &dummy_opcode;
2724 else
2725 prev_prev_insn = prev_insn;
2726 prev_insn = *ip;
2727
2728 /* Any time we see a branch, we always fill the delay slot
2729 immediately; since this insn is not a branch, we know it
2730 is not in a delay slot. */
2731 prev_insn_is_delay_slot = 0;
2732
f6688943
TS
2733 prev_insn_fixp[0] = fixp[0];
2734 prev_insn_fixp[1] = fixp[1];
2735 prev_insn_fixp[2] = fixp[2];
2736 prev_insn_reloc_type[0] = reloc_type[0];
2737 prev_insn_reloc_type[1] = reloc_type[1];
2738 prev_insn_reloc_type[2] = reloc_type[2];
252b5132
RH
2739 if (mips_opts.mips16)
2740 prev_insn_extended = (ip->use_extend
f6688943 2741 || *reloc_type > BFD_RELOC_UNUSED);
252b5132
RH
2742 }
2743
2744 prev_prev_insn_unreordered = prev_insn_unreordered;
2745 prev_insn_unreordered = 0;
2746 prev_insn_frag = frag_now;
2747 prev_insn_where = f - frag_now->fr_literal;
2748 prev_insn_valid = 1;
2749 }
2750 else if (place == NULL)
2751 {
2752 /* We need to record a bit of information even when we are not
2753 reordering, in order to determine the base address for mips16
2754 PC relative relocs. */
2755 prev_prev_insn = prev_insn;
2756 prev_insn = *ip;
f6688943
TS
2757 prev_insn_reloc_type[0] = reloc_type[0];
2758 prev_insn_reloc_type[1] = reloc_type[1];
2759 prev_insn_reloc_type[2] = reloc_type[2];
252b5132
RH
2760 prev_prev_insn_unreordered = prev_insn_unreordered;
2761 prev_insn_unreordered = 1;
2762 }
2763
2764 /* We just output an insn, so the next one doesn't have a label. */
2765 mips_clear_insn_labels ();
f5040a92
AO
2766
2767 /* We must ensure that the frag to which an instruction that was
2768 moved from a non-variant frag doesn't become a variant frag,
2769 otherwise tc_gen_reloc may get confused. */
2770 if (force_new_frag)
2771 {
2772 frag_wane (frag_now);
2773 frag_new (0);
2774 }
252b5132
RH
2775}
2776
2777/* This function forgets that there was any previous instruction or
2778 label. If PRESERVE is non-zero, it remembers enough information to
bdaaa2e1 2779 know whether nops are needed before a noreorder section. */
252b5132
RH
2780
2781static void
2782mips_no_prev_insn (preserve)
2783 int preserve;
2784{
2785 if (! preserve)
2786 {
2787 prev_insn.insn_mo = &dummy_opcode;
2788 prev_prev_insn.insn_mo = &dummy_opcode;
2789 prev_nop_frag = NULL;
2790 prev_nop_frag_holds = 0;
2791 prev_nop_frag_required = 0;
2792 prev_nop_frag_since = 0;
2793 }
2794 prev_insn_valid = 0;
2795 prev_insn_is_delay_slot = 0;
2796 prev_insn_unreordered = 0;
2797 prev_insn_extended = 0;
f6688943
TS
2798 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2799 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2800 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
252b5132
RH
2801 prev_prev_insn_unreordered = 0;
2802 mips_clear_insn_labels ();
2803}
2804
2805/* This function must be called whenever we turn on noreorder or emit
2806 something other than instructions. It inserts any NOPS which might
2807 be needed by the previous instruction, and clears the information
2808 kept for the previous instructions. The INSNS parameter is true if
bdaaa2e1 2809 instructions are to follow. */
252b5132
RH
2810
2811static void
2812mips_emit_delays (insns)
b34976b6 2813 bfd_boolean insns;
252b5132
RH
2814{
2815 if (! mips_opts.noreorder)
2816 {
2817 int nops;
2818
2819 nops = 0;
2820 if ((! mips_opts.mips16
9ce8a5dd 2821 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
2822 && (! cop_interlocks
2823 && (prev_insn.insn_mo->pinfo
2824 & (INSN_LOAD_COPROC_DELAY
2825 | INSN_COPROC_MOVE_DELAY
2826 | INSN_WRITE_COND_CODE))))
2827 || (! hilo_interlocks
2828 && (prev_insn.insn_mo->pinfo
2829 & (INSN_READ_LO
2830 | INSN_READ_HI)))
2831 || (! mips_opts.mips16
2832 && ! gpr_interlocks
bdaaa2e1 2833 && (prev_insn.insn_mo->pinfo
252b5132
RH
2834 & INSN_LOAD_MEMORY_DELAY))
2835 || (! mips_opts.mips16
e7af610e 2836 && mips_opts.isa == ISA_MIPS1
252b5132
RH
2837 && (prev_insn.insn_mo->pinfo
2838 & INSN_COPROC_MEMORY_DELAY)))
2839 {
beae10d5 2840 /* Itbl support may require additional care here. */
252b5132
RH
2841 ++nops;
2842 if ((! mips_opts.mips16
9ce8a5dd 2843 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
2844 && (! cop_interlocks
2845 && prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2846 || (! hilo_interlocks
2847 && ((prev_insn.insn_mo->pinfo & INSN_READ_HI)
2848 || (prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2849 ++nops;
2850
2851 if (prev_insn_unreordered)
2852 nops = 0;
2853 }
2854 else if ((! mips_opts.mips16
9ce8a5dd 2855 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
2856 && (! cop_interlocks
2857 && prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2858 || (! hilo_interlocks
2859 && ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
2860 || (prev_prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2861 {
beae10d5 2862 /* Itbl support may require additional care here. */
252b5132
RH
2863 if (! prev_prev_insn_unreordered)
2864 ++nops;
2865 }
2866
60b63b72
RS
2867 if (mips_fix_4122_bugs && prev_insn.insn_mo->name)
2868 {
2869 int min_nops = 0;
2870 const char *pn = prev_insn.insn_mo->name;
2871 if (strncmp(pn, "macc", 4) == 0
2872 || strncmp(pn, "dmacc", 5) == 0
2873 || strncmp(pn, "dmult", 5) == 0)
2874 {
2875 min_nops = 1;
2876 }
2877 if (nops < min_nops)
2878 nops = min_nops;
2879 }
2880
252b5132
RH
2881 if (nops > 0)
2882 {
2883 struct insn_label_list *l;
2884
2885 if (insns)
2886 {
2887 /* Record the frag which holds the nop instructions, so
2888 that we can remove them if we don't need them. */
2889 frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2890 prev_nop_frag = frag_now;
2891 prev_nop_frag_holds = nops;
2892 prev_nop_frag_required = 0;
2893 prev_nop_frag_since = 0;
2894 }
2895
2896 for (; nops > 0; --nops)
2897 emit_nop ();
2898
2899 if (insns)
2900 {
2901 /* Move on to a new frag, so that it is safe to simply
bdaaa2e1 2902 decrease the size of prev_nop_frag. */
252b5132
RH
2903 frag_wane (frag_now);
2904 frag_new (0);
2905 }
2906
2907 for (l = insn_labels; l != NULL; l = l->next)
2908 {
98aa84af
AM
2909 valueT val;
2910
252b5132 2911 assert (S_GET_SEGMENT (l->label) == now_seg);
49309057 2912 symbol_set_frag (l->label, frag_now);
98aa84af 2913 val = (valueT) frag_now_fix ();
252b5132
RH
2914 /* mips16 text labels are stored as odd. */
2915 if (mips_opts.mips16)
f9419b05 2916 ++val;
98aa84af 2917 S_SET_VALUE (l->label, val);
252b5132
RH
2918 }
2919 }
2920 }
2921
2922 /* Mark instruction labels in mips16 mode. */
f9419b05 2923 if (insns)
252b5132
RH
2924 mips16_mark_labels ();
2925
2926 mips_no_prev_insn (insns);
2927}
2928
2929/* Build an instruction created by a macro expansion. This is passed
2930 a pointer to the count of instructions created so far, an
2931 expression, the name of the instruction to build, an operand format
2932 string, and corresponding arguments. */
2933
2934#ifdef USE_STDARG
2935static void
2936macro_build (char *place,
2937 int *counter,
2938 expressionS * ep,
2939 const char *name,
2940 const char *fmt,
2941 ...)
2942#else
2943static void
2944macro_build (place, counter, ep, name, fmt, va_alist)
2945 char *place;
2946 int *counter;
2947 expressionS *ep;
2948 const char *name;
2949 const char *fmt;
2950 va_dcl
2951#endif
2952{
2953 struct mips_cl_insn insn;
f6688943 2954 bfd_reloc_code_real_type r[3];
252b5132 2955 va_list args;
252b5132
RH
2956
2957#ifdef USE_STDARG
2958 va_start (args, fmt);
2959#else
2960 va_start (args);
2961#endif
2962
2963 /*
2964 * If the macro is about to expand into a second instruction,
2965 * print a warning if needed. We need to pass ip as a parameter
2966 * to generate a better warning message here...
2967 */
2968 if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
2969 as_warn (_("Macro instruction expanded into multiple instructions"));
2970
80cc45a5
EC
2971 /*
2972 * If the macro is about to expand into a second instruction,
2973 * and it is in a delay slot, print a warning.
2974 */
2975 if (place == NULL
2976 && *counter == 1
2977 && mips_opts.noreorder
2978 && (prev_prev_insn.insn_mo->pinfo
2979 & (INSN_UNCOND_BRANCH_DELAY | INSN_COND_BRANCH_DELAY
2b2e39bf 2980 | INSN_COND_BRANCH_LIKELY)) != 0)
80cc45a5
EC
2981 as_warn (_("Macro instruction expanded into multiple instructions in a branch delay slot"));
2982
252b5132 2983 if (place == NULL)
f9419b05 2984 ++*counter; /* bump instruction counter */
252b5132
RH
2985
2986 if (mips_opts.mips16)
2987 {
2988 mips16_macro_build (place, counter, ep, name, fmt, args);
2989 va_end (args);
2990 return;
2991 }
2992
f6688943
TS
2993 r[0] = BFD_RELOC_UNUSED;
2994 r[1] = BFD_RELOC_UNUSED;
2995 r[2] = BFD_RELOC_UNUSED;
252b5132
RH
2996 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
2997 assert (insn.insn_mo);
2998 assert (strcmp (name, insn.insn_mo->name) == 0);
2999
3000 /* Search until we get a match for NAME. */
3001 while (1)
3002 {
b34976b6 3003 /* It is assumed here that macros will never generate
deec1734 3004 MDMX or MIPS-3D instructions. */
252b5132
RH
3005 if (strcmp (fmt, insn.insn_mo->args) == 0
3006 && insn.insn_mo->pinfo != INSN_MACRO
aec421e0
TS
3007 && OPCODE_IS_MEMBER (insn.insn_mo,
3008 (mips_opts.isa
3396de36 3009 | (file_ase_mips16 ? INSN_MIPS16 : 0)),
aec421e0 3010 mips_arch)
ec68c924 3011 && (mips_arch != CPU_R4650 || (insn.insn_mo->pinfo & FP_D) == 0))
252b5132
RH
3012 break;
3013
3014 ++insn.insn_mo;
3015 assert (insn.insn_mo->name);
3016 assert (strcmp (name, insn.insn_mo->name) == 0);
3017 }
3018
3019 insn.insn_opcode = insn.insn_mo->match;
3020 for (;;)
3021 {
3022 switch (*fmt++)
3023 {
3024 case '\0':
3025 break;
3026
3027 case ',':
3028 case '(':
3029 case ')':
3030 continue;
3031
3032 case 't':
3033 case 'w':
3034 case 'E':
38487616 3035 insn.insn_opcode |= va_arg (args, int) << OP_SH_RT;
252b5132
RH
3036 continue;
3037
3038 case 'c':
38487616
TS
3039 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE;
3040 continue;
3041
252b5132
RH
3042 case 'T':
3043 case 'W':
38487616 3044 insn.insn_opcode |= va_arg (args, int) << OP_SH_FT;
252b5132
RH
3045 continue;
3046
3047 case 'd':
3048 case 'G':
af7ee8bf 3049 case 'K':
38487616 3050 insn.insn_opcode |= va_arg (args, int) << OP_SH_RD;
252b5132
RH
3051 continue;
3052
4372b673
NC
3053 case 'U':
3054 {
3055 int tmp = va_arg (args, int);
3056
38487616
TS
3057 insn.insn_opcode |= tmp << OP_SH_RT;
3058 insn.insn_opcode |= tmp << OP_SH_RD;
beae10d5 3059 continue;
4372b673
NC
3060 }
3061
252b5132
RH
3062 case 'V':
3063 case 'S':
38487616 3064 insn.insn_opcode |= va_arg (args, int) << OP_SH_FS;
252b5132
RH
3065 continue;
3066
3067 case 'z':
3068 continue;
3069
3070 case '<':
38487616 3071 insn.insn_opcode |= va_arg (args, int) << OP_SH_SHAMT;
252b5132
RH
3072 continue;
3073
3074 case 'D':
38487616 3075 insn.insn_opcode |= va_arg (args, int) << OP_SH_FD;
252b5132
RH
3076 continue;
3077
3078 case 'B':
38487616 3079 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE20;
252b5132
RH
3080 continue;
3081
4372b673 3082 case 'J':
38487616 3083 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE19;
4372b673
NC
3084 continue;
3085
252b5132 3086 case 'q':
38487616 3087 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE2;
252b5132
RH
3088 continue;
3089
3090 case 'b':
3091 case 's':
3092 case 'r':
3093 case 'v':
38487616 3094 insn.insn_opcode |= va_arg (args, int) << OP_SH_RS;
252b5132
RH
3095 continue;
3096
3097 case 'i':
3098 case 'j':
3099 case 'o':
f6688943 3100 *r = (bfd_reloc_code_real_type) va_arg (args, int);
cdf6fd85 3101 assert (*r == BFD_RELOC_GPREL16
f6688943
TS
3102 || *r == BFD_RELOC_MIPS_LITERAL
3103 || *r == BFD_RELOC_MIPS_HIGHER
3104 || *r == BFD_RELOC_HI16_S
3105 || *r == BFD_RELOC_LO16
3106 || *r == BFD_RELOC_MIPS_GOT16
3107 || *r == BFD_RELOC_MIPS_CALL16
438c16b8
TS
3108 || *r == BFD_RELOC_MIPS_GOT_DISP
3109 || *r == BFD_RELOC_MIPS_GOT_PAGE
3110 || *r == BFD_RELOC_MIPS_GOT_OFST
f6688943
TS
3111 || *r == BFD_RELOC_MIPS_GOT_LO16
3112 || *r == BFD_RELOC_MIPS_CALL_LO16
252b5132 3113 || (ep->X_op == O_subtract
f6688943 3114 && *r == BFD_RELOC_PCREL_LO16));
252b5132
RH
3115 continue;
3116
3117 case 'u':
f6688943 3118 *r = (bfd_reloc_code_real_type) va_arg (args, int);
252b5132
RH
3119 assert (ep != NULL
3120 && (ep->X_op == O_constant
3121 || (ep->X_op == O_symbol
f6688943
TS
3122 && (*r == BFD_RELOC_MIPS_HIGHEST
3123 || *r == BFD_RELOC_HI16_S
3124 || *r == BFD_RELOC_HI16
3125 || *r == BFD_RELOC_GPREL16
3126 || *r == BFD_RELOC_MIPS_GOT_HI16
3127 || *r == BFD_RELOC_MIPS_CALL_HI16))
252b5132 3128 || (ep->X_op == O_subtract
f6688943 3129 && *r == BFD_RELOC_PCREL_HI16_S)));
252b5132
RH
3130 continue;
3131
3132 case 'p':
3133 assert (ep != NULL);
3134 /*
3135 * This allows macro() to pass an immediate expression for
3136 * creating short branches without creating a symbol.
0b25d3e6
AO
3137 * Note that the expression still might come from the assembly
3138 * input, in which case the value is not checked for range nor
3139 * is a relocation entry generated (yuck).
252b5132
RH
3140 */
3141 if (ep->X_op == O_constant)
3142 {
3143 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
3144 ep = NULL;
3145 }
3146 else
0b25d3e6 3147 *r = BFD_RELOC_16_PCREL_S2;
252b5132
RH
3148 continue;
3149
3150 case 'a':
3151 assert (ep != NULL);
f6688943 3152 *r = BFD_RELOC_MIPS_JMP;
252b5132
RH
3153 continue;
3154
3155 case 'C':
3156 insn.insn_opcode |= va_arg (args, unsigned long);
3157 continue;
3158
3159 default:
3160 internalError ();
3161 }
3162 break;
3163 }
3164 va_end (args);
f6688943 3165 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 3166
5e0116d5 3167 append_insn (place, &insn, ep, r);
252b5132
RH
3168}
3169
3170static void
3171mips16_macro_build (place, counter, ep, name, fmt, args)
3172 char *place;
43841e91 3173 int *counter ATTRIBUTE_UNUSED;
252b5132
RH
3174 expressionS *ep;
3175 const char *name;
3176 const char *fmt;
3177 va_list args;
3178{
3179 struct mips_cl_insn insn;
f6688943
TS
3180 bfd_reloc_code_real_type r[3]
3181 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 3182
252b5132
RH
3183 insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
3184 assert (insn.insn_mo);
3185 assert (strcmp (name, insn.insn_mo->name) == 0);
3186
3187 while (strcmp (fmt, insn.insn_mo->args) != 0
3188 || insn.insn_mo->pinfo == INSN_MACRO)
3189 {
3190 ++insn.insn_mo;
3191 assert (insn.insn_mo->name);
3192 assert (strcmp (name, insn.insn_mo->name) == 0);
3193 }
3194
3195 insn.insn_opcode = insn.insn_mo->match;
b34976b6 3196 insn.use_extend = FALSE;
252b5132
RH
3197
3198 for (;;)
3199 {
3200 int c;
3201
3202 c = *fmt++;
3203 switch (c)
3204 {
3205 case '\0':
3206 break;
3207
3208 case ',':
3209 case '(':
3210 case ')':
3211 continue;
3212
3213 case 'y':
3214 case 'w':
3215 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
3216 continue;
3217
3218 case 'x':
3219 case 'v':
3220 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
3221 continue;
3222
3223 case 'z':
3224 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
3225 continue;
3226
3227 case 'Z':
3228 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
3229 continue;
3230
3231 case '0':
3232 case 'S':
3233 case 'P':
3234 case 'R':
3235 continue;
3236
3237 case 'X':
3238 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
3239 continue;
3240
3241 case 'Y':
3242 {
3243 int regno;
3244
3245 regno = va_arg (args, int);
3246 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
3247 insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
3248 }
3249 continue;
3250
3251 case '<':
3252 case '>':
3253 case '4':
3254 case '5':
3255 case 'H':
3256 case 'W':
3257 case 'D':
3258 case 'j':
3259 case '8':
3260 case 'V':
3261 case 'C':
3262 case 'U':
3263 case 'k':
3264 case 'K':
3265 case 'p':
3266 case 'q':
3267 {
3268 assert (ep != NULL);
3269
3270 if (ep->X_op != O_constant)
874e8986 3271 *r = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
3272 else
3273 {
b34976b6
AM
3274 mips16_immed (NULL, 0, c, ep->X_add_number, FALSE, FALSE,
3275 FALSE, &insn.insn_opcode, &insn.use_extend,
c4e7957c 3276 &insn.extend);
252b5132 3277 ep = NULL;
f6688943 3278 *r = BFD_RELOC_UNUSED;
252b5132
RH
3279 }
3280 }
3281 continue;
3282
3283 case '6':
3284 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
3285 continue;
3286 }
3287
3288 break;
3289 }
3290
f6688943 3291 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 3292
5e0116d5 3293 append_insn (place, &insn, ep, r);
252b5132
RH
3294}
3295
438c16b8
TS
3296/*
3297 * Generate a "jalr" instruction with a relocation hint to the called
3298 * function. This occurs in NewABI PIC code.
3299 */
3300static void
3301macro_build_jalr (icnt, ep)
3302 int icnt;
3303 expressionS *ep;
3304{
f21f8242 3305 char *f;
b34976b6 3306
438c16b8 3307 if (HAVE_NEWABI)
f21f8242
AO
3308 {
3309 frag_grow (4);
3310 f = frag_more (0);
3311 }
438c16b8
TS
3312 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr", "d,s",
3313 RA, PIC_CALL_REG);
3314 if (HAVE_NEWABI)
f21f8242 3315 fix_new_exp (frag_now, f - frag_now->fr_literal,
b34976b6 3316 0, ep, FALSE, BFD_RELOC_MIPS_JALR);
438c16b8
TS
3317}
3318
252b5132
RH
3319/*
3320 * Generate a "lui" instruction.
3321 */
3322static void
3323macro_build_lui (place, counter, ep, regnum)
3324 char *place;
3325 int *counter;
3326 expressionS *ep;
3327 int regnum;
3328{
3329 expressionS high_expr;
3330 struct mips_cl_insn insn;
f6688943
TS
3331 bfd_reloc_code_real_type r[3]
3332 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
5a38dc70
AM
3333 const char *name = "lui";
3334 const char *fmt = "t,u";
252b5132
RH
3335
3336 assert (! mips_opts.mips16);
3337
3338 if (place == NULL)
3339 high_expr = *ep;
3340 else
3341 {
3342 high_expr.X_op = O_constant;
3343 high_expr.X_add_number = ep->X_add_number;
3344 }
3345
3346 if (high_expr.X_op == O_constant)
3347 {
3348 /* we can compute the instruction now without a relocation entry */
e7d556df
TS
3349 high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3350 >> 16) & 0xffff;
f6688943 3351 *r = BFD_RELOC_UNUSED;
252b5132 3352 }
78e1bb40 3353 else
252b5132
RH
3354 {
3355 assert (ep->X_op == O_symbol);
3356 /* _gp_disp is a special case, used from s_cpload. */
3357 assert (mips_pic == NO_PIC
78e1bb40
AO
3358 || (! HAVE_NEWABI
3359 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0));
f6688943 3360 *r = BFD_RELOC_HI16_S;
252b5132
RH
3361 }
3362
3363 /*
3364 * If the macro is about to expand into a second instruction,
3365 * print a warning if needed. We need to pass ip as a parameter
3366 * to generate a better warning message here...
3367 */
3368 if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
3369 as_warn (_("Macro instruction expanded into multiple instructions"));
3370
3371 if (place == NULL)
f9419b05 3372 ++*counter; /* bump instruction counter */
252b5132
RH
3373
3374 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3375 assert (insn.insn_mo);
3376 assert (strcmp (name, insn.insn_mo->name) == 0);
3377 assert (strcmp (fmt, insn.insn_mo->args) == 0);
3378
3379 insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
f6688943 3380 if (*r == BFD_RELOC_UNUSED)
252b5132
RH
3381 {
3382 insn.insn_opcode |= high_expr.X_add_number;
5e0116d5 3383 append_insn (place, &insn, NULL, r);
252b5132
RH
3384 }
3385 else
5e0116d5 3386 append_insn (place, &insn, &high_expr, r);
252b5132
RH
3387}
3388
885add95
CD
3389/* Generate a sequence of instructions to do a load or store from a constant
3390 offset off of a base register (breg) into/from a target register (treg),
3391 using AT if necessary. */
3392static void
3393macro_build_ldst_constoffset (place, counter, ep, op, treg, breg)
3394 char *place;
3395 int *counter;
3396 expressionS *ep;
3397 const char *op;
3398 int treg, breg;
3399{
3400 assert (ep->X_op == O_constant);
3401
3402 /* Right now, this routine can only handle signed 32-bit contants. */
3403 if (! IS_SEXT_32BIT_NUM(ep->X_add_number))
3404 as_warn (_("operand overflow"));
3405
3406 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
3407 {
3408 /* Signed 16-bit offset will fit in the op. Easy! */
3409 macro_build (place, counter, ep, op, "t,o(b)", treg,
3410 (int) BFD_RELOC_LO16, breg);
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. */
3419 macro_build_lui (place, counter, ep, AT);
3420 if (place != NULL)
3421 place += 4;
3422 macro_build (place, counter, (expressionS *) NULL,
f5040a92
AO
3423 HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
3424 ? "add" : "addu" : "daddu",
885add95
CD
3425 "d,v,t", AT, AT, breg);
3426 if (place != NULL)
3427 place += 4;
3428 macro_build (place, counter, ep, op, "t,o(b)", treg,
3429 (int) BFD_RELOC_LO16, AT);
3430
3431 if (mips_opts.noat)
3432 as_warn (_("Macro used $at after \".set noat\""));
3433 }
3434}
3435
252b5132
RH
3436/* set_at()
3437 * Generates code to set the $at register to true (one)
3438 * if reg is less than the immediate expression.
3439 */
3440static void
3441set_at (counter, reg, unsignedp)
3442 int *counter;
3443 int reg;
3444 int unsignedp;
3445{
3446 if (imm_expr.X_op == O_constant
3447 && imm_expr.X_add_number >= -0x8000
3448 && imm_expr.X_add_number < 0x8000)
3449 macro_build ((char *) NULL, counter, &imm_expr,
3450 unsignedp ? "sltiu" : "slti",
3451 "t,r,j", AT, reg, (int) BFD_RELOC_LO16);
3452 else
3453 {
4d34fb5f 3454 load_register (counter, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9 3455 macro_build ((char *) NULL, counter, (expressionS *) NULL,
252b5132
RH
3456 unsignedp ? "sltu" : "slt",
3457 "d,v,t", AT, reg, AT);
3458 }
3459}
3460
3461/* Warn if an expression is not a constant. */
3462
3463static void
3464check_absolute_expr (ip, ex)
3465 struct mips_cl_insn *ip;
3466 expressionS *ex;
3467{
3468 if (ex->X_op == O_big)
3469 as_bad (_("unsupported large constant"));
3470 else if (ex->X_op != O_constant)
3471 as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
3472}
3473
3474/* Count the leading zeroes by performing a binary chop. This is a
3475 bulky bit of source, but performance is a LOT better for the
3476 majority of values than a simple loop to count the bits:
3477 for (lcnt = 0; (lcnt < 32); lcnt++)
3478 if ((v) & (1 << (31 - lcnt)))
3479 break;
3480 However it is not code size friendly, and the gain will drop a bit
3481 on certain cached systems.
3482*/
3483#define COUNT_TOP_ZEROES(v) \
3484 (((v) & ~0xffff) == 0 \
3485 ? ((v) & ~0xff) == 0 \
3486 ? ((v) & ~0xf) == 0 \
3487 ? ((v) & ~0x3) == 0 \
3488 ? ((v) & ~0x1) == 0 \
3489 ? !(v) \
3490 ? 32 \
3491 : 31 \
3492 : 30 \
3493 : ((v) & ~0x7) == 0 \
3494 ? 29 \
3495 : 28 \
3496 : ((v) & ~0x3f) == 0 \
3497 ? ((v) & ~0x1f) == 0 \
3498 ? 27 \
3499 : 26 \
3500 : ((v) & ~0x7f) == 0 \
3501 ? 25 \
3502 : 24 \
3503 : ((v) & ~0xfff) == 0 \
3504 ? ((v) & ~0x3ff) == 0 \
3505 ? ((v) & ~0x1ff) == 0 \
3506 ? 23 \
3507 : 22 \
3508 : ((v) & ~0x7ff) == 0 \
3509 ? 21 \
3510 : 20 \
3511 : ((v) & ~0x3fff) == 0 \
3512 ? ((v) & ~0x1fff) == 0 \
3513 ? 19 \
3514 : 18 \
3515 : ((v) & ~0x7fff) == 0 \
3516 ? 17 \
3517 : 16 \
3518 : ((v) & ~0xffffff) == 0 \
3519 ? ((v) & ~0xfffff) == 0 \
3520 ? ((v) & ~0x3ffff) == 0 \
3521 ? ((v) & ~0x1ffff) == 0 \
3522 ? 15 \
3523 : 14 \
3524 : ((v) & ~0x7ffff) == 0 \
3525 ? 13 \
3526 : 12 \
3527 : ((v) & ~0x3fffff) == 0 \
3528 ? ((v) & ~0x1fffff) == 0 \
3529 ? 11 \
3530 : 10 \
3531 : ((v) & ~0x7fffff) == 0 \
3532 ? 9 \
3533 : 8 \
3534 : ((v) & ~0xfffffff) == 0 \
3535 ? ((v) & ~0x3ffffff) == 0 \
3536 ? ((v) & ~0x1ffffff) == 0 \
3537 ? 7 \
3538 : 6 \
3539 : ((v) & ~0x7ffffff) == 0 \
3540 ? 5 \
3541 : 4 \
3542 : ((v) & ~0x3fffffff) == 0 \
3543 ? ((v) & ~0x1fffffff) == 0 \
3544 ? 3 \
3545 : 2 \
3546 : ((v) & ~0x7fffffff) == 0 \
3547 ? 1 \
3548 : 0)
3549
3550/* load_register()
3551 * This routine generates the least number of instructions neccessary to load
3552 * an absolute expression value into a register.
3553 */
3554static void
3555load_register (counter, reg, ep, dbl)
3556 int *counter;
3557 int reg;
3558 expressionS *ep;
3559 int dbl;
3560{
3561 int freg;
3562 expressionS hi32, lo32;
3563
3564 if (ep->X_op != O_big)
3565 {
3566 assert (ep->X_op == O_constant);
3567 if (ep->X_add_number < 0x8000
3568 && (ep->X_add_number >= 0
3569 || (ep->X_add_number >= -0x8000
3570 && (! dbl
3571 || ! ep->X_unsigned
3572 || sizeof (ep->X_add_number) > 4))))
3573 {
3574 /* We can handle 16 bit signed values with an addiu to
3575 $zero. No need to ever use daddiu here, since $zero and
3576 the result are always correct in 32 bit mode. */
3577 macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3578 (int) BFD_RELOC_LO16);
3579 return;
3580 }
3581 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3582 {
3583 /* We can handle 16 bit unsigned values with an ori to
3584 $zero. */
3585 macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, 0,
3586 (int) BFD_RELOC_LO16);
3587 return;
3588 }
6373ee54 3589 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)
252b5132
RH
3590 && (! dbl
3591 || ! ep->X_unsigned
3592 || sizeof (ep->X_add_number) > 4
3593 || (ep->X_add_number & 0x80000000) == 0))
ca4e0257 3594 || ((HAVE_32BIT_GPRS || ! dbl)
252b5132 3595 && (ep->X_add_number &~ (offsetT) 0xffffffff) == 0)
ca4e0257 3596 || (HAVE_32BIT_GPRS
252b5132
RH
3597 && ! dbl
3598 && ((ep->X_add_number &~ (offsetT) 0xffffffff)
3599 == ~ (offsetT) 0xffffffff)))
3600 {
3601 /* 32 bit values require an lui. */
3602 macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3603 (int) BFD_RELOC_HI16);
3604 if ((ep->X_add_number & 0xffff) != 0)
3605 macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, reg,
3606 (int) BFD_RELOC_LO16);
3607 return;
3608 }
3609 }
3610
3611 /* The value is larger than 32 bits. */
3612
ca4e0257 3613 if (HAVE_32BIT_GPRS)
252b5132 3614 {
956cd1d6
TS
3615 as_bad (_("Number (0x%lx) larger than 32 bits"),
3616 (unsigned long) ep->X_add_number);
252b5132
RH
3617 macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3618 (int) BFD_RELOC_LO16);
3619 return;
3620 }
3621
3622 if (ep->X_op != O_big)
3623 {
3624 hi32 = *ep;
3625 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3626 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3627 hi32.X_add_number &= 0xffffffff;
3628 lo32 = *ep;
3629 lo32.X_add_number &= 0xffffffff;
3630 }
3631 else
3632 {
3633 assert (ep->X_add_number > 2);
3634 if (ep->X_add_number == 3)
3635 generic_bignum[3] = 0;
3636 else if (ep->X_add_number > 4)
3637 as_bad (_("Number larger than 64 bits"));
3638 lo32.X_op = O_constant;
3639 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3640 hi32.X_op = O_constant;
3641 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3642 }
3643
3644 if (hi32.X_add_number == 0)
3645 freg = 0;
3646 else
3647 {
3648 int shift, bit;
3649 unsigned long hi, lo;
3650
956cd1d6 3651 if (hi32.X_add_number == (offsetT) 0xffffffff)
beae10d5
KH
3652 {
3653 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3654 {
3655 macro_build ((char *) NULL, counter, &lo32, "addiu", "t,r,j",
252b5132 3656 reg, 0, (int) BFD_RELOC_LO16);
beae10d5
KH
3657 return;
3658 }
3659 if (lo32.X_add_number & 0x80000000)
3660 {
3661 macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3662 (int) BFD_RELOC_HI16);
252b5132
RH
3663 if (lo32.X_add_number & 0xffff)
3664 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i",
3665 reg, reg, (int) BFD_RELOC_LO16);
beae10d5
KH
3666 return;
3667 }
3668 }
252b5132
RH
3669
3670 /* Check for 16bit shifted constant. We know that hi32 is
3671 non-zero, so start the mask on the first bit of the hi32
3672 value. */
3673 shift = 17;
3674 do
beae10d5
KH
3675 {
3676 unsigned long himask, lomask;
3677
3678 if (shift < 32)
3679 {
3680 himask = 0xffff >> (32 - shift);
3681 lomask = (0xffff << shift) & 0xffffffff;
3682 }
3683 else
3684 {
3685 himask = 0xffff << (shift - 32);
3686 lomask = 0;
3687 }
3688 if ((hi32.X_add_number & ~(offsetT) himask) == 0
3689 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3690 {
3691 expressionS tmp;
3692
3693 tmp.X_op = O_constant;
3694 if (shift < 32)
3695 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3696 | (lo32.X_add_number >> shift));
3697 else
3698 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
3699 macro_build ((char *) NULL, counter, &tmp,
3700 "ori", "t,r,i", reg, 0,
3701 (int) BFD_RELOC_LO16);
2396cfb9 3702 macro_build ((char *) NULL, counter, (expressionS *) NULL,
beae10d5
KH
3703 (shift >= 32) ? "dsll32" : "dsll",
3704 "d,w,<", reg, reg,
3705 (shift >= 32) ? shift - 32 : shift);
3706 return;
3707 }
f9419b05 3708 ++shift;
beae10d5
KH
3709 }
3710 while (shift <= (64 - 16));
252b5132
RH
3711
3712 /* Find the bit number of the lowest one bit, and store the
3713 shifted value in hi/lo. */
3714 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3715 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3716 if (lo != 0)
3717 {
3718 bit = 0;
3719 while ((lo & 1) == 0)
3720 {
3721 lo >>= 1;
3722 ++bit;
3723 }
3724 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3725 hi >>= bit;
3726 }
3727 else
3728 {
3729 bit = 32;
3730 while ((hi & 1) == 0)
3731 {
3732 hi >>= 1;
3733 ++bit;
3734 }
3735 lo = hi;
3736 hi = 0;
3737 }
3738
3739 /* Optimize if the shifted value is a (power of 2) - 1. */
3740 if ((hi == 0 && ((lo + 1) & lo) == 0)
3741 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
beae10d5
KH
3742 {
3743 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
252b5132 3744 if (shift != 0)
beae10d5 3745 {
252b5132
RH
3746 expressionS tmp;
3747
3748 /* This instruction will set the register to be all
3749 ones. */
beae10d5
KH
3750 tmp.X_op = O_constant;
3751 tmp.X_add_number = (offsetT) -1;
3752 macro_build ((char *) NULL, counter, &tmp, "addiu", "t,r,j",
252b5132 3753 reg, 0, (int) BFD_RELOC_LO16);
beae10d5
KH
3754 if (bit != 0)
3755 {
3756 bit += shift;
2396cfb9 3757 macro_build ((char *) NULL, counter, (expressionS *) NULL,
beae10d5
KH
3758 (bit >= 32) ? "dsll32" : "dsll",
3759 "d,w,<", reg, reg,
3760 (bit >= 32) ? bit - 32 : bit);
3761 }
2396cfb9 3762 macro_build ((char *) NULL, counter, (expressionS *) NULL,
252b5132 3763 (shift >= 32) ? "dsrl32" : "dsrl",
beae10d5 3764 "d,w,<", reg, reg,
252b5132 3765 (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
3766 return;
3767 }
3768 }
252b5132
RH
3769
3770 /* Sign extend hi32 before calling load_register, because we can
3771 generally get better code when we load a sign extended value. */
3772 if ((hi32.X_add_number & 0x80000000) != 0)
beae10d5 3773 hi32.X_add_number |= ~(offsetT) 0xffffffff;
252b5132
RH
3774 load_register (counter, reg, &hi32, 0);
3775 freg = reg;
3776 }
3777 if ((lo32.X_add_number & 0xffff0000) == 0)
3778 {
3779 if (freg != 0)
3780 {
2396cfb9
TS
3781 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3782 "dsll32", "d,w,<", reg, freg, 0);
252b5132
RH
3783 freg = reg;
3784 }
3785 }
3786 else
3787 {
3788 expressionS mid16;
3789
956cd1d6 3790 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
beae10d5 3791 {
252b5132
RH
3792 macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3793 (int) BFD_RELOC_HI16);
956cd1d6
TS
3794 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3795 "dsrl32", "d,w,<", reg, reg, 0);
beae10d5
KH
3796 return;
3797 }
252b5132
RH
3798
3799 if (freg != 0)
3800 {
956cd1d6
TS
3801 macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3802 "d,w,<", reg, freg, 16);
252b5132
RH
3803 freg = reg;
3804 }
3805 mid16 = lo32;
3806 mid16.X_add_number >>= 16;
3807 macro_build ((char *) NULL, counter, &mid16, "ori", "t,r,i", reg,
3808 freg, (int) BFD_RELOC_LO16);
956cd1d6
TS
3809 macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3810 "d,w,<", reg, reg, 16);
252b5132
RH
3811 freg = reg;
3812 }
3813 if ((lo32.X_add_number & 0xffff) != 0)
3814 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i", reg, freg,
3815 (int) BFD_RELOC_LO16);
3816}
3817
3818/* Load an address into a register. */
3819
3820static void
c9914766 3821load_address (counter, reg, ep, used_at)
252b5132
RH
3822 int *counter;
3823 int reg;
3824 expressionS *ep;
d6bc6245 3825 int *used_at;
252b5132 3826{
f9419b05 3827 char *p = NULL;
252b5132
RH
3828
3829 if (ep->X_op != O_constant
3830 && ep->X_op != O_symbol)
3831 {
3832 as_bad (_("expression too complex"));
3833 ep->X_op = O_constant;
3834 }
3835
3836 if (ep->X_op == O_constant)
3837 {
c9914766 3838 load_register (counter, reg, ep, HAVE_64BIT_ADDRESSES);
252b5132
RH
3839 return;
3840 }
3841
3842 if (mips_pic == NO_PIC)
3843 {
3844 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 3845 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
3846 Otherwise we want
3847 lui $reg,<sym> (BFD_RELOC_HI16_S)
3848 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
d6bc6245 3849 If we have an addend, we always use the latter form.
76b3015f 3850
d6bc6245
TS
3851 With 64bit address space and a usable $at we want
3852 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3853 lui $at,<sym> (BFD_RELOC_HI16_S)
3854 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3855 daddiu $at,<sym> (BFD_RELOC_LO16)
3856 dsll32 $reg,0
3a482fd5 3857 daddu $reg,$reg,$at
76b3015f 3858
c03099e6 3859 If $at is already in use, we use a path which is suboptimal
d6bc6245
TS
3860 on superscalar processors.
3861 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3862 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3863 dsll $reg,16
3864 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
3865 dsll $reg,16
3866 daddiu $reg,<sym> (BFD_RELOC_LO16)
3867 */
c9914766 3868 if (HAVE_64BIT_ADDRESSES)
d6bc6245 3869 {
d6bc6245
TS
3870 /* We don't do GP optimization for now because RELAX_ENCODE can't
3871 hold the data for such large chunks. */
3872
460597ba 3873 if (*used_at == 0 && ! mips_opts.noat)
d6bc6245
TS
3874 {
3875 macro_build (p, counter, ep, "lui", "t,u",
3876 reg, (int) BFD_RELOC_MIPS_HIGHEST);
3877 macro_build (p, counter, ep, "lui", "t,u",
3878 AT, (int) BFD_RELOC_HI16_S);
3879 macro_build (p, counter, ep, "daddiu", "t,r,j",
3880 reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3881 macro_build (p, counter, ep, "daddiu", "t,r,j",
3882 AT, AT, (int) BFD_RELOC_LO16);
2396cfb9
TS
3883 macro_build (p, counter, (expressionS *) NULL, "dsll32",
3884 "d,w,<", reg, reg, 0);
3a482fd5 3885 macro_build (p, counter, (expressionS *) NULL, "daddu",
2396cfb9 3886 "d,v,t", reg, reg, AT);
d6bc6245
TS
3887 *used_at = 1;
3888 }
3889 else
3890 {
3891 macro_build (p, counter, ep, "lui", "t,u",
3892 reg, (int) BFD_RELOC_MIPS_HIGHEST);
3893 macro_build (p, counter, ep, "daddiu", "t,r,j",
3894 reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
2396cfb9
TS
3895 macro_build (p, counter, (expressionS *) NULL, "dsll",
3896 "d,w,<", reg, reg, 16);
d6bc6245
TS
3897 macro_build (p, counter, ep, "daddiu", "t,r,j",
3898 reg, reg, (int) BFD_RELOC_HI16_S);
2396cfb9
TS
3899 macro_build (p, counter, (expressionS *) NULL, "dsll",
3900 "d,w,<", reg, reg, 16);
d6bc6245
TS
3901 macro_build (p, counter, ep, "daddiu", "t,r,j",
3902 reg, reg, (int) BFD_RELOC_LO16);
3903 }
3904 }
252b5132
RH
3905 else
3906 {
d6bc6245
TS
3907 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3908 && ! nopic_need_relax (ep->X_add_symbol, 1))
3909 {
3910 frag_grow (20);
3911 macro_build ((char *) NULL, counter, ep,
f5040a92
AO
3912 HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
3913 ? "addi" : "addiu" : "daddiu", "t,r,j",
c9914766 3914 reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
d6bc6245
TS
3915 p = frag_var (rs_machine_dependent, 8, 0,
3916 RELAX_ENCODE (4, 8, 0, 4, 0,
3917 mips_opts.warn_about_macros),
956cd1d6 3918 ep->X_add_symbol, 0, NULL);
d6bc6245
TS
3919 }
3920 macro_build_lui (p, counter, ep, reg);
3921 if (p != NULL)
3922 p += 4;
c9914766 3923 macro_build (p, counter, ep,
f5040a92
AO
3924 HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
3925 ? "addi" : "addiu" : "daddiu",
d6bc6245
TS
3926 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3927 }
252b5132
RH
3928 }
3929 else if (mips_pic == SVR4_PIC && ! mips_big_got)
3930 {
3931 expressionS ex;
3932
3933 /* If this is a reference to an external symbol, we want
3934 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3935 Otherwise we want
3936 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3937 nop
3938 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
f5040a92
AO
3939 If there is a constant, it must be added in after.
3940
ed6fb7bd 3941 If we have NewABI, we want
f5040a92
AO
3942 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
3943 unless we're referencing a global symbol with a non-zero
3944 offset, in which case cst must be added separately. */
ed6fb7bd
SC
3945 if (HAVE_NEWABI)
3946 {
f5040a92 3947 frag_grow (12);
684022ea 3948
f5040a92
AO
3949 if (ep->X_add_number)
3950 {
3951 frag_now->tc_frag_data.tc_fr_offset =
3952 ex.X_add_number = ep->X_add_number;
3953 ep->X_add_number = 0;
3954 macro_build ((char *) NULL, counter, ep,
3955 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3956 (int) BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
3957 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3958 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3959 ex.X_op = O_constant;
3960 macro_build ((char *) NULL, counter, &ex,
3961 HAVE_32BIT_ADDRESSES ? "addi" : "daddiu",
3962 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3963 p = frag_var (rs_machine_dependent, 8, 0,
3964 RELAX_ENCODE (8, 4, 0, 0, 0,
3965 mips_opts.warn_about_macros),
3966 ep->X_add_symbol, 0, (char *) NULL);
3967 ep->X_add_number = ex.X_add_number;
3968 }
3969
3970 macro_build (p, counter, ep,
ed6fb7bd
SC
3971 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3972 (int) BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
3973
3974 if (! p)
3975 {
3976 /* To avoid confusion in tc_gen_reloc, we must ensure
3977 that this does not become a variant frag. */
3978 frag_wane (frag_now);
3979 frag_new (0);
3980 }
ed6fb7bd
SC
3981 }
3982 else
3983 {
f5040a92
AO
3984 ex.X_add_number = ep->X_add_number;
3985 ep->X_add_number = 0;
3986 frag_grow (20);
ed6fb7bd
SC
3987 macro_build ((char *) NULL, counter, ep,
3988 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
3989 reg, (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
3990 macro_build ((char *) NULL, counter, (expressionS *) NULL, "nop", "");
3991 p = frag_var (rs_machine_dependent, 4, 0,
3992 RELAX_ENCODE (0, 4, -8, 0, 0, mips_opts.warn_about_macros),
3993 ep->X_add_symbol, (offsetT) 0, (char *) NULL);
3994 macro_build (p, counter, ep,
3995 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3996 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
ed6fb7bd 3997
f5040a92
AO
3998 if (ex.X_add_number != 0)
3999 {
4000 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4001 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4002 ex.X_op = O_constant;
4003 macro_build ((char *) NULL, counter, &ex,
4004 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4005 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
4006 }
252b5132
RH
4007 }
4008 }
4009 else if (mips_pic == SVR4_PIC)
4010 {
4011 expressionS ex;
4012 int off;
4013
4014 /* This is the large GOT case. If this is a reference to an
4015 external symbol, we want
4016 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
4017 addu $reg,$reg,$gp
4018 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
f5040a92
AO
4019
4020 Otherwise, for a reference to a local symbol in old ABI, we want
252b5132
RH
4021 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4022 nop
4023 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
684022ea 4024 If there is a constant, it must be added in after.
f5040a92
AO
4025
4026 In the NewABI, for local symbols, with or without offsets, we want:
438c16b8
TS
4027 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
4028 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
f5040a92 4029 */
438c16b8
TS
4030 if (HAVE_NEWABI)
4031 {
f5040a92
AO
4032 frag_grow (24);
4033
4034 frag_now->tc_frag_data.tc_fr_offset =
4035 ex.X_add_number = ep->X_add_number;
4036 ep->X_add_number = 0;
4037 macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
4038 (int) BFD_RELOC_MIPS_GOT_HI16);
4039 macro_build ((char *) NULL, counter, (expressionS *) NULL,
4040 HAVE_32BIT_ADDRESSES ? "add" : "daddu", "d,v,t", reg,
4041 reg, mips_gp_register);
438c16b8 4042 macro_build ((char *) NULL, counter, ep,
f5040a92
AO
4043 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4044 "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT_LO16, reg);
4045 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4046 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4047 else if (ex.X_add_number)
4048 {
4049 ex.X_op = O_constant;
4050 macro_build ((char *) NULL, counter, &ex,
4051 HAVE_32BIT_ADDRESSES ? "addi" : "daddiu",
4052 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
4053 }
4054
4055 ep->X_add_number = ex.X_add_number;
4056 p = frag_var (rs_machine_dependent, 8, 0,
4057 RELAX_ENCODE (ex.X_add_number ? 16 : 12, 8, 0, 4, 0,
4058 mips_opts.warn_about_macros),
4059 ep->X_add_symbol, 0, (char *) NULL);
4060 macro_build (p, counter, ep,
438c16b8
TS
4061 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
4062 (int) BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92
AO
4063 macro_build (p + 4, counter, ep,
4064 HAVE_32BIT_ADDRESSES ? "addi" : "daddiu", "t,r,j",
438c16b8
TS
4065 reg, reg, (int) BFD_RELOC_MIPS_GOT_OFST);
4066 }
252b5132 4067 else
438c16b8 4068 {
f5040a92
AO
4069 ex.X_add_number = ep->X_add_number;
4070 ep->X_add_number = 0;
438c16b8
TS
4071 if (reg_needs_delay (mips_gp_register))
4072 off = 4;
4073 else
4074 off = 0;
4075 frag_grow (32);
4076 macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
4077 (int) BFD_RELOC_MIPS_GOT_HI16);
4078 macro_build ((char *) NULL, counter, (expressionS *) NULL,
4079 HAVE_32BIT_ADDRESSES ? "addu" : "daddu", "d,v,t", reg,
4080 reg, mips_gp_register);
4081 macro_build ((char *) NULL, counter, ep,
4082 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4083 "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT_LO16, reg);
4084 p = frag_var (rs_machine_dependent, 12 + off, 0,
4085 RELAX_ENCODE (12, 12 + off, off, 8 + off, 0,
4086 mips_opts.warn_about_macros),
4087 ep->X_add_symbol, 0, NULL);
4088 if (off > 0)
4089 {
4090 /* We need a nop before loading from $gp. This special
4091 check is required because the lui which starts the main
4092 instruction stream does not refer to $gp, and so will not
4093 insert the nop which may be required. */
4094 macro_build (p, counter, (expressionS *) NULL, "nop", "");
4095 p += 4;
4096 }
4097 macro_build (p, counter, ep,
4098 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
4099 (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
4100 p += 4;
252b5132
RH
4101 macro_build (p, counter, (expressionS *) NULL, "nop", "");
4102 p += 4;
438c16b8
TS
4103 macro_build (p, counter, ep,
4104 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4105 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
438c16b8 4106
f5040a92
AO
4107 if (ex.X_add_number != 0)
4108 {
4109 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
4110 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4111 ex.X_op = O_constant;
4112 macro_build ((char *) NULL, counter, &ex,
4113 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4114 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
4115 }
252b5132
RH
4116 }
4117 }
4118 else if (mips_pic == EMBEDDED_PIC)
4119 {
4120 /* We always do
cdf6fd85 4121 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
c9914766
TS
4122 */
4123 macro_build ((char *) NULL, counter, ep,
4124 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4125 "t,r,j", reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
252b5132
RH
4126 }
4127 else
4128 abort ();
4129}
4130
ea1fb5dc
RS
4131/* Move the contents of register SOURCE into register DEST. */
4132
4133static void
4134move_register (counter, dest, source)
4135 int *counter;
4136 int dest;
4137 int source;
4138{
4139 macro_build ((char *) NULL, counter, (expressionS *) NULL,
4140 HAVE_32BIT_GPRS ? "addu" : "daddu",
4141 "d,v,t", dest, source, 0);
4142}
4143
252b5132
RH
4144/*
4145 * Build macros
4146 * This routine implements the seemingly endless macro or synthesized
4147 * instructions and addressing modes in the mips assembly language. Many
4148 * of these macros are simple and are similar to each other. These could
4149 * probably be handled by some kind of table or grammer aproach instead of
4150 * this verbose method. Others are not simple macros but are more like
4151 * optimizing code generation.
4152 * One interesting optimization is when several store macros appear
4153 * consecutivly that would load AT with the upper half of the same address.
4154 * The ensuing load upper instructions are ommited. This implies some kind
4155 * of global optimization. We currently only optimize within a single macro.
4156 * For many of the load and store macros if the address is specified as a
4157 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
4158 * first load register 'at' with zero and use it as the base register. The
4159 * mips assembler simply uses register $zero. Just one tiny optimization
4160 * we're missing.
4161 */
4162static void
4163macro (ip)
4164 struct mips_cl_insn *ip;
4165{
4166 register int treg, sreg, dreg, breg;
4167 int tempreg;
4168 int mask;
4169 int icnt = 0;
43841e91 4170 int used_at = 0;
252b5132
RH
4171 expressionS expr1;
4172 const char *s;
4173 const char *s2;
4174 const char *fmt;
4175 int likely = 0;
4176 int dbl = 0;
4177 int coproc = 0;
4178 int lr = 0;
4179 int imm = 0;
4180 offsetT maxnum;
4181 int off;
4182 bfd_reloc_code_real_type r;
252b5132
RH
4183 int hold_mips_optimize;
4184
4185 assert (! mips_opts.mips16);
4186
4187 treg = (ip->insn_opcode >> 16) & 0x1f;
4188 dreg = (ip->insn_opcode >> 11) & 0x1f;
4189 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
4190 mask = ip->insn_mo->mask;
4191
4192 expr1.X_op = O_constant;
4193 expr1.X_op_symbol = NULL;
4194 expr1.X_add_symbol = NULL;
4195 expr1.X_add_number = 1;
4196
5919d012
RS
4197 /* Umatched fixups should not be put in the same frag as a relaxable
4198 macro. For example, suppose we have:
4199
4200 lui $4,%hi(l1) # 1
4201 la $5,l2 # 2
4202 addiu $4,$4,%lo(l1) # 3
4203
4204 If instructions 1 and 2 were put in the same frag, md_frob_file would
4205 move the fixup for #1 after the fixups for the "unrelaxed" version of
4206 #2. This would confuse tc_gen_reloc, which expects the relocations
4207 for #2 to be the last for that frag.
4208
64bdfcaf
RS
4209 Also, if tc_gen_reloc sees certain relocations in a variant frag,
4210 it assumes that they belong to a relaxable macro. We mustn't put
4211 other uses of such relocations into a variant frag.
4212
4213 To avoid both problems, finish the current frag it contains a
4214 %reloc() operator. The macro then goes into a new frag. */
4215 if (prev_reloc_op_frag == frag_now)
5919d012
RS
4216 {
4217 frag_wane (frag_now);
4218 frag_new (0);
4219 }
4220
252b5132
RH
4221 switch (mask)
4222 {
4223 case M_DABS:
4224 dbl = 1;
4225 case M_ABS:
4226 /* bgez $a0,.+12
4227 move v0,$a0
4228 sub v0,$zero,$a0
4229 */
4230
b34976b6 4231 mips_emit_delays (TRUE);
252b5132
RH
4232 ++mips_opts.noreorder;
4233 mips_any_noreorder = 1;
4234
4235 expr1.X_add_number = 8;
4236 macro_build ((char *) NULL, &icnt, &expr1, "bgez", "s,p", sreg);
4237 if (dreg == sreg)
2396cfb9
TS
4238 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
4239 0);
252b5132 4240 else
ea1fb5dc 4241 move_register (&icnt, dreg, sreg);
2396cfb9 4242 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 4243 dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
252b5132
RH
4244
4245 --mips_opts.noreorder;
4246 return;
4247
4248 case M_ADD_I:
4249 s = "addi";
4250 s2 = "add";
4251 goto do_addi;
4252 case M_ADDU_I:
4253 s = "addiu";
4254 s2 = "addu";
4255 goto do_addi;
4256 case M_DADD_I:
4257 dbl = 1;
4258 s = "daddi";
4259 s2 = "dadd";
4260 goto do_addi;
4261 case M_DADDU_I:
4262 dbl = 1;
4263 s = "daddiu";
4264 s2 = "daddu";
4265 do_addi:
4266 if (imm_expr.X_op == O_constant
4267 && imm_expr.X_add_number >= -0x8000
4268 && imm_expr.X_add_number < 0x8000)
4269 {
4270 macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,j", treg, sreg,
4271 (int) BFD_RELOC_LO16);
4272 return;
4273 }
4274 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9
TS
4275 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
4276 treg, sreg, AT);
252b5132
RH
4277 break;
4278
4279 case M_AND_I:
4280 s = "andi";
4281 s2 = "and";
4282 goto do_bit;
4283 case M_OR_I:
4284 s = "ori";
4285 s2 = "or";
4286 goto do_bit;
4287 case M_NOR_I:
4288 s = "";
4289 s2 = "nor";
4290 goto do_bit;
4291 case M_XOR_I:
4292 s = "xori";
4293 s2 = "xor";
4294 do_bit:
4295 if (imm_expr.X_op == O_constant
4296 && imm_expr.X_add_number >= 0
4297 && imm_expr.X_add_number < 0x10000)
4298 {
4299 if (mask != M_NOR_I)
4300 macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,i", treg,
4301 sreg, (int) BFD_RELOC_LO16);
4302 else
4303 {
4304 macro_build ((char *) NULL, &icnt, &imm_expr, "ori", "t,r,i",
4305 treg, sreg, (int) BFD_RELOC_LO16);
2396cfb9
TS
4306 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nor",
4307 "d,v,t", treg, treg, 0);
252b5132
RH
4308 }
4309 return;
4310 }
4311
d6bc6245 4312 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
4313 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
4314 treg, sreg, AT);
252b5132
RH
4315 break;
4316
4317 case M_BEQ_I:
4318 s = "beq";
4319 goto beq_i;
4320 case M_BEQL_I:
4321 s = "beql";
4322 likely = 1;
4323 goto beq_i;
4324 case M_BNE_I:
4325 s = "bne";
4326 goto beq_i;
4327 case M_BNEL_I:
4328 s = "bnel";
4329 likely = 1;
4330 beq_i:
4331 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4332 {
4333 macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg,
4334 0);
4335 return;
4336 }
4d34fb5f 4337 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
252b5132
RH
4338 macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg, AT);
4339 break;
4340
4341 case M_BGEL:
4342 likely = 1;
4343 case M_BGE:
4344 if (treg == 0)
4345 {
4346 macro_build ((char *) NULL, &icnt, &offset_expr,
2396cfb9 4347 likely ? "bgezl" : "bgez", "s,p", sreg);
252b5132
RH
4348 return;
4349 }
4350 if (sreg == 0)
4351 {
4352 macro_build ((char *) NULL, &icnt, &offset_expr,
2396cfb9 4353 likely ? "blezl" : "blez", "s,p", treg);
252b5132
RH
4354 return;
4355 }
2396cfb9
TS
4356 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4357 AT, sreg, treg);
252b5132 4358 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4359 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4360 break;
4361
4362 case M_BGTL_I:
4363 likely = 1;
4364 case M_BGT_I:
4365 /* check for > max integer */
4366 maxnum = 0x7fffffff;
ca4e0257 4367 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
4368 {
4369 maxnum <<= 16;
4370 maxnum |= 0xffff;
4371 maxnum <<= 16;
4372 maxnum |= 0xffff;
4373 }
4374 if (imm_expr.X_op == O_constant
4375 && imm_expr.X_add_number >= maxnum
ca4e0257 4376 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
4377 {
4378 do_false:
4379 /* result is always false */
4380 if (! likely)
4381 {
39c0a331
L
4382 if (warn_nops)
4383 as_warn (_("Branch %s is always false (nop)"),
4384 ip->insn_mo->name);
2396cfb9
TS
4385 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop",
4386 "", 0);
252b5132
RH
4387 }
4388 else
4389 {
39c0a331
L
4390 if (warn_nops)
4391 as_warn (_("Branch likely %s is always false"),
4392 ip->insn_mo->name);
252b5132
RH
4393 macro_build ((char *) NULL, &icnt, &offset_expr, "bnel",
4394 "s,t,p", 0, 0);
4395 }
4396 return;
4397 }
4398 if (imm_expr.X_op != O_constant)
4399 as_bad (_("Unsupported large constant"));
f9419b05 4400 ++imm_expr.X_add_number;
252b5132
RH
4401 /* FALLTHROUGH */
4402 case M_BGE_I:
4403 case M_BGEL_I:
4404 if (mask == M_BGEL_I)
4405 likely = 1;
4406 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4407 {
4408 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4409 likely ? "bgezl" : "bgez", "s,p", sreg);
252b5132
RH
4410 return;
4411 }
4412 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4413 {
4414 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4415 likely ? "bgtzl" : "bgtz", "s,p", sreg);
252b5132
RH
4416 return;
4417 }
4418 maxnum = 0x7fffffff;
ca4e0257 4419 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
4420 {
4421 maxnum <<= 16;
4422 maxnum |= 0xffff;
4423 maxnum <<= 16;
4424 maxnum |= 0xffff;
4425 }
4426 maxnum = - maxnum - 1;
4427 if (imm_expr.X_op == O_constant
4428 && imm_expr.X_add_number <= maxnum
ca4e0257 4429 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
4430 {
4431 do_true:
4432 /* result is always true */
4433 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
4434 macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
4435 return;
4436 }
4437 set_at (&icnt, sreg, 0);
4438 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4439 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4440 break;
4441
4442 case M_BGEUL:
4443 likely = 1;
4444 case M_BGEU:
4445 if (treg == 0)
4446 goto do_true;
4447 if (sreg == 0)
4448 {
4449 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4450 likely ? "beql" : "beq", "s,t,p", 0, treg);
252b5132
RH
4451 return;
4452 }
2396cfb9
TS
4453 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4454 "d,v,t", AT, sreg, treg);
252b5132 4455 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4456 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4457 break;
4458
4459 case M_BGTUL_I:
4460 likely = 1;
4461 case M_BGTU_I:
4462 if (sreg == 0
ca4e0257 4463 || (HAVE_32BIT_GPRS
252b5132 4464 && imm_expr.X_op == O_constant
956cd1d6 4465 && imm_expr.X_add_number == (offsetT) 0xffffffff))
252b5132
RH
4466 goto do_false;
4467 if (imm_expr.X_op != O_constant)
4468 as_bad (_("Unsupported large constant"));
f9419b05 4469 ++imm_expr.X_add_number;
252b5132
RH
4470 /* FALLTHROUGH */
4471 case M_BGEU_I:
4472 case M_BGEUL_I:
4473 if (mask == M_BGEUL_I)
4474 likely = 1;
4475 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4476 goto do_true;
4477 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4478 {
4479 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4480 likely ? "bnel" : "bne", "s,t,p", sreg, 0);
252b5132
RH
4481 return;
4482 }
4483 set_at (&icnt, sreg, 1);
4484 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4485 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4486 break;
4487
4488 case M_BGTL:
4489 likely = 1;
4490 case M_BGT:
4491 if (treg == 0)
4492 {
4493 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4494 likely ? "bgtzl" : "bgtz", "s,p", sreg);
252b5132
RH
4495 return;
4496 }
4497 if (sreg == 0)
4498 {
4499 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4500 likely ? "bltzl" : "bltz", "s,p", treg);
252b5132
RH
4501 return;
4502 }
2396cfb9
TS
4503 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4504 AT, treg, sreg);
252b5132 4505 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4506 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4507 break;
4508
4509 case M_BGTUL:
4510 likely = 1;
4511 case M_BGTU:
4512 if (treg == 0)
4513 {
4514 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4515 likely ? "bnel" : "bne", "s,t,p", sreg, 0);
252b5132
RH
4516 return;
4517 }
4518 if (sreg == 0)
4519 goto do_false;
2396cfb9
TS
4520 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4521 "d,v,t", AT, treg, sreg);
252b5132 4522 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4523 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4524 break;
4525
4526 case M_BLEL:
4527 likely = 1;
4528 case M_BLE:
4529 if (treg == 0)
4530 {
4531 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4532 likely ? "blezl" : "blez", "s,p", sreg);
252b5132
RH
4533 return;
4534 }
4535 if (sreg == 0)
4536 {
4537 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4538 likely ? "bgezl" : "bgez", "s,p", treg);
252b5132
RH
4539 return;
4540 }
2396cfb9
TS
4541 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4542 AT, treg, sreg);
252b5132 4543 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4544 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4545 break;
4546
4547 case M_BLEL_I:
4548 likely = 1;
4549 case M_BLE_I:
4550 maxnum = 0x7fffffff;
ca4e0257 4551 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
4552 {
4553 maxnum <<= 16;
4554 maxnum |= 0xffff;
4555 maxnum <<= 16;
4556 maxnum |= 0xffff;
4557 }
4558 if (imm_expr.X_op == O_constant
4559 && imm_expr.X_add_number >= maxnum
ca4e0257 4560 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
4561 goto do_true;
4562 if (imm_expr.X_op != O_constant)
4563 as_bad (_("Unsupported large constant"));
f9419b05 4564 ++imm_expr.X_add_number;
252b5132
RH
4565 /* FALLTHROUGH */
4566 case M_BLT_I:
4567 case M_BLTL_I:
4568 if (mask == M_BLTL_I)
4569 likely = 1;
4570 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4571 {
4572 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4573 likely ? "bltzl" : "bltz", "s,p", sreg);
252b5132
RH
4574 return;
4575 }
4576 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4577 {
4578 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4579 likely ? "blezl" : "blez", "s,p", sreg);
252b5132
RH
4580 return;
4581 }
4582 set_at (&icnt, sreg, 0);
4583 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4584 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4585 break;
4586
4587 case M_BLEUL:
4588 likely = 1;
4589 case M_BLEU:
4590 if (treg == 0)
4591 {
4592 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4593 likely ? "beql" : "beq", "s,t,p", sreg, 0);
252b5132
RH
4594 return;
4595 }
4596 if (sreg == 0)
4597 goto do_true;
2396cfb9
TS
4598 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4599 "d,v,t", AT, treg, sreg);
252b5132 4600 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4601 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4602 break;
4603
4604 case M_BLEUL_I:
4605 likely = 1;
4606 case M_BLEU_I:
4607 if (sreg == 0
ca4e0257 4608 || (HAVE_32BIT_GPRS
252b5132 4609 && imm_expr.X_op == O_constant
956cd1d6 4610 && imm_expr.X_add_number == (offsetT) 0xffffffff))
252b5132
RH
4611 goto do_true;
4612 if (imm_expr.X_op != O_constant)
4613 as_bad (_("Unsupported large constant"));
f9419b05 4614 ++imm_expr.X_add_number;
252b5132
RH
4615 /* FALLTHROUGH */
4616 case M_BLTU_I:
4617 case M_BLTUL_I:
4618 if (mask == M_BLTUL_I)
4619 likely = 1;
4620 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4621 goto do_false;
4622 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4623 {
4624 macro_build ((char *) NULL, &icnt, &offset_expr,
4625 likely ? "beql" : "beq",
4626 "s,t,p", sreg, 0);
4627 return;
4628 }
4629 set_at (&icnt, sreg, 1);
4630 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4631 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4632 break;
4633
4634 case M_BLTL:
4635 likely = 1;
4636 case M_BLT:
4637 if (treg == 0)
4638 {
4639 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4640 likely ? "bltzl" : "bltz", "s,p", sreg);
252b5132
RH
4641 return;
4642 }
4643 if (sreg == 0)
4644 {
4645 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4646 likely ? "bgtzl" : "bgtz", "s,p", treg);
252b5132
RH
4647 return;
4648 }
2396cfb9
TS
4649 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4650 AT, sreg, treg);
252b5132 4651 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4652 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4653 break;
4654
4655 case M_BLTUL:
4656 likely = 1;
4657 case M_BLTU:
4658 if (treg == 0)
4659 goto do_false;
4660 if (sreg == 0)
4661 {
4662 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4663 likely ? "bnel" : "bne", "s,t,p", 0, treg);
252b5132
RH
4664 return;
4665 }
2396cfb9
TS
4666 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4667 "d,v,t", AT, sreg,
252b5132
RH
4668 treg);
4669 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4670 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4671 break;
4672
4673 case M_DDIV_3:
4674 dbl = 1;
4675 case M_DIV_3:
4676 s = "mflo";
4677 goto do_div3;
4678 case M_DREM_3:
4679 dbl = 1;
4680 case M_REM_3:
4681 s = "mfhi";
4682 do_div3:
4683 if (treg == 0)
4684 {
4685 as_warn (_("Divide by zero."));
4686 if (mips_trap)
2396cfb9 4687 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
9bd7d936 4688 "s,t,q", 0, 0, 7);
252b5132 4689 else
2396cfb9
TS
4690 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4691 "c", 7);
252b5132
RH
4692 return;
4693 }
4694
b34976b6 4695 mips_emit_delays (TRUE);
252b5132
RH
4696 ++mips_opts.noreorder;
4697 mips_any_noreorder = 1;
4698 if (mips_trap)
4699 {
2396cfb9 4700 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
9bd7d936 4701 "s,t,q", treg, 0, 7);
2396cfb9 4702 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 4703 dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
252b5132
RH
4704 }
4705 else
4706 {
4707 expr1.X_add_number = 8;
4708 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
2396cfb9 4709 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 4710 dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
2396cfb9
TS
4711 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4712 "c", 7);
252b5132
RH
4713 }
4714 expr1.X_add_number = -1;
4715 macro_build ((char *) NULL, &icnt, &expr1,
4716 dbl ? "daddiu" : "addiu",
4717 "t,r,j", AT, 0, (int) BFD_RELOC_LO16);
4718 expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
4719 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, AT);
4720 if (dbl)
4721 {
4722 expr1.X_add_number = 1;
4723 macro_build ((char *) NULL, &icnt, &expr1, "daddiu", "t,r,j", AT, 0,
4724 (int) BFD_RELOC_LO16);
2396cfb9
TS
4725 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsll32",
4726 "d,w,<", AT, AT, 31);
252b5132
RH
4727 }
4728 else
4729 {
4730 expr1.X_add_number = 0x80000000;
4731 macro_build ((char *) NULL, &icnt, &expr1, "lui", "t,u", AT,
4732 (int) BFD_RELOC_HI16);
4733 }
4734 if (mips_trap)
4735 {
2396cfb9 4736 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
9bd7d936 4737 "s,t,q", sreg, AT, 6);
252b5132
RH
4738 /* We want to close the noreorder block as soon as possible, so
4739 that later insns are available for delay slot filling. */
4740 --mips_opts.noreorder;
4741 }
4742 else
4743 {
4744 expr1.X_add_number = 8;
4745 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", sreg, AT);
2396cfb9
TS
4746 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
4747 0);
252b5132
RH
4748
4749 /* We want to close the noreorder block as soon as possible, so
4750 that later insns are available for delay slot filling. */
4751 --mips_opts.noreorder;
4752
2396cfb9
TS
4753 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4754 "c", 6);
252b5132 4755 }
2396cfb9 4756 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d", dreg);
252b5132
RH
4757 break;
4758
4759 case M_DIV_3I:
4760 s = "div";
4761 s2 = "mflo";
4762 goto do_divi;
4763 case M_DIVU_3I:
4764 s = "divu";
4765 s2 = "mflo";
4766 goto do_divi;
4767 case M_REM_3I:
4768 s = "div";
4769 s2 = "mfhi";
4770 goto do_divi;
4771 case M_REMU_3I:
4772 s = "divu";
4773 s2 = "mfhi";
4774 goto do_divi;
4775 case M_DDIV_3I:
4776 dbl = 1;
4777 s = "ddiv";
4778 s2 = "mflo";
4779 goto do_divi;
4780 case M_DDIVU_3I:
4781 dbl = 1;
4782 s = "ddivu";
4783 s2 = "mflo";
4784 goto do_divi;
4785 case M_DREM_3I:
4786 dbl = 1;
4787 s = "ddiv";
4788 s2 = "mfhi";
4789 goto do_divi;
4790 case M_DREMU_3I:
4791 dbl = 1;
4792 s = "ddivu";
4793 s2 = "mfhi";
4794 do_divi:
4795 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4796 {
4797 as_warn (_("Divide by zero."));
4798 if (mips_trap)
2396cfb9 4799 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
9bd7d936 4800 "s,t,q", 0, 0, 7);
252b5132 4801 else
2396cfb9
TS
4802 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4803 "c", 7);
252b5132
RH
4804 return;
4805 }
4806 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4807 {
4808 if (strcmp (s2, "mflo") == 0)
ea1fb5dc 4809 move_register (&icnt, dreg, sreg);
252b5132 4810 else
ea1fb5dc 4811 move_register (&icnt, dreg, 0);
252b5132
RH
4812 return;
4813 }
4814 if (imm_expr.X_op == O_constant
4815 && imm_expr.X_add_number == -1
4816 && s[strlen (s) - 1] != 'u')
4817 {
4818 if (strcmp (s2, "mflo") == 0)
4819 {
2396cfb9
TS
4820 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4821 dbl ? "dneg" : "neg", "d,w", dreg, sreg);
252b5132
RH
4822 }
4823 else
ea1fb5dc 4824 move_register (&icnt, dreg, 0);
252b5132
RH
4825 return;
4826 }
4827
4828 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9
TS
4829 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4830 sreg, AT);
4831 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
252b5132
RH
4832 break;
4833
4834 case M_DIVU_3:
4835 s = "divu";
4836 s2 = "mflo";
4837 goto do_divu3;
4838 case M_REMU_3:
4839 s = "divu";
4840 s2 = "mfhi";
4841 goto do_divu3;
4842 case M_DDIVU_3:
4843 s = "ddivu";
4844 s2 = "mflo";
4845 goto do_divu3;
4846 case M_DREMU_3:
4847 s = "ddivu";
4848 s2 = "mfhi";
4849 do_divu3:
b34976b6 4850 mips_emit_delays (TRUE);
252b5132
RH
4851 ++mips_opts.noreorder;
4852 mips_any_noreorder = 1;
4853 if (mips_trap)
4854 {
2396cfb9 4855 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
9bd7d936 4856 "s,t,q", treg, 0, 7);
2396cfb9
TS
4857 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4858 sreg, treg);
252b5132
RH
4859 /* We want to close the noreorder block as soon as possible, so
4860 that later insns are available for delay slot filling. */
4861 --mips_opts.noreorder;
4862 }
4863 else
4864 {
4865 expr1.X_add_number = 8;
4866 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
2396cfb9
TS
4867 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4868 sreg, treg);
252b5132
RH
4869
4870 /* We want to close the noreorder block as soon as possible, so
4871 that later insns are available for delay slot filling. */
4872 --mips_opts.noreorder;
2396cfb9
TS
4873 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4874 "c", 7);
252b5132 4875 }
2396cfb9 4876 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
252b5132
RH
4877 return;
4878
4879 case M_DLA_AB:
4880 dbl = 1;
4881 case M_LA_AB:
4882 /* Load the address of a symbol into a register. If breg is not
4883 zero, we then add a base register to it. */
4884
3bec30a8
TS
4885 if (dbl && HAVE_32BIT_GPRS)
4886 as_warn (_("dla used to load 32-bit register"));
4887
c90bbe5b 4888 if (! dbl && HAVE_64BIT_OBJECTS)
3bec30a8
TS
4889 as_warn (_("la used to load 64-bit address"));
4890
0c11417f
MR
4891 if (offset_expr.X_op == O_constant
4892 && offset_expr.X_add_number >= -0x8000
4893 && offset_expr.X_add_number < 0x8000)
4894 {
4895 macro_build ((char *) NULL, &icnt, &offset_expr,
f5040a92
AO
4896 (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" :
4897 HAVE_NEWABI ? "addi" : "addiu",
0c11417f
MR
4898 "t,r,j", treg, sreg, (int) BFD_RELOC_LO16);
4899 return;
4900 }
4901
afdbd6d0
CD
4902 if (treg == breg)
4903 {
4904 tempreg = AT;
4905 used_at = 1;
4906 }
4907 else
4908 {
4909 tempreg = treg;
4910 used_at = 0;
4911 }
4912
252b5132
RH
4913 /* When generating embedded PIC code, we permit expressions of
4914 the form
afdbd6d0
CD
4915 la $treg,foo-bar
4916 la $treg,foo-bar($breg)
bb2d6cd7 4917 where bar is an address in the current section. These are used
252b5132
RH
4918 when getting the addresses of functions. We don't permit
4919 X_add_number to be non-zero, because if the symbol is
4920 external the relaxing code needs to know that any addend is
4921 purely the offset to X_op_symbol. */
4922 if (mips_pic == EMBEDDED_PIC
4923 && offset_expr.X_op == O_subtract
49309057 4924 && (symbol_constant_p (offset_expr.X_op_symbol)
bb2d6cd7 4925 ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
49309057
ILT
4926 : (symbol_equated_p (offset_expr.X_op_symbol)
4927 && (S_GET_SEGMENT
4928 (symbol_get_value_expression (offset_expr.X_op_symbol)
4929 ->X_add_symbol)
bb2d6cd7 4930 == now_seg)))
bb2d6cd7
GK
4931 && (offset_expr.X_add_number == 0
4932 || OUTPUT_FLAVOR == bfd_target_elf_flavour))
252b5132 4933 {
afdbd6d0
CD
4934 if (breg == 0)
4935 {
4936 tempreg = treg;
4937 used_at = 0;
4938 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4939 tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4940 }
4941 else
4942 {
4943 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4944 tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4945 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4d34fb5f 4946 (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu",
afdbd6d0
CD
4947 "d,v,t", tempreg, tempreg, breg);
4948 }
252b5132 4949 macro_build ((char *) NULL, &icnt, &offset_expr,
4d34fb5f 4950 (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
afdbd6d0
CD
4951 "t,r,j", treg, tempreg, (int) BFD_RELOC_PCREL_LO16);
4952 if (! used_at)
4953 return;
4954 break;
252b5132
RH
4955 }
4956
4957 if (offset_expr.X_op != O_symbol
4958 && offset_expr.X_op != O_constant)
4959 {
4960 as_bad (_("expression too complex"));
4961 offset_expr.X_op = O_constant;
4962 }
4963
252b5132 4964 if (offset_expr.X_op == O_constant)
4d34fb5f
TS
4965 load_register (&icnt, tempreg, &offset_expr,
4966 ((mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
4967 ? (dbl || HAVE_64BIT_ADDRESSES)
4968 : HAVE_64BIT_ADDRESSES));
252b5132
RH
4969 else if (mips_pic == NO_PIC)
4970 {
d6bc6245 4971 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 4972 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
4973 Otherwise we want
4974 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
4975 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4976 If we have a constant, we need two instructions anyhow,
d6bc6245 4977 so we may as well always use the latter form.
76b3015f 4978
d6bc6245
TS
4979 With 64bit address space and a usable $at we want
4980 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4981 lui $at,<sym> (BFD_RELOC_HI16_S)
4982 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4983 daddiu $at,<sym> (BFD_RELOC_LO16)
4984 dsll32 $tempreg,0
3a482fd5 4985 daddu $tempreg,$tempreg,$at
76b3015f 4986
c03099e6 4987 If $at is already in use, we use a path which is suboptimal
d6bc6245
TS
4988 on superscalar processors.
4989 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4990 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4991 dsll $tempreg,16
4992 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
4993 dsll $tempreg,16
4994 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
4995 */
f9419b05 4996 char *p = NULL;
d6bc6245 4997 if (HAVE_64BIT_ADDRESSES)
252b5132 4998 {
d6bc6245
TS
4999 /* We don't do GP optimization for now because RELAX_ENCODE can't
5000 hold the data for such large chunks. */
5001
460597ba 5002 if (used_at == 0 && ! mips_opts.noat)
98d3f06f
KH
5003 {
5004 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5005 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5006 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5007 AT, (int) BFD_RELOC_HI16_S);
5008 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5009 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5010 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5011 AT, AT, (int) BFD_RELOC_LO16);
5012 macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
5013 "d,w,<", tempreg, tempreg, 0);
3a482fd5
MR
5014 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5015 "d,v,t", tempreg, tempreg, AT);
98d3f06f
KH
5016 used_at = 1;
5017 }
5018 else
5019 {
5020 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5021 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5022 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5023 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5024 macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
5025 tempreg, tempreg, 16);
5026 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5027 tempreg, tempreg, (int) BFD_RELOC_HI16_S);
5028 macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
5029 tempreg, tempreg, 16);
5030 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5031 tempreg, tempreg, (int) BFD_RELOC_LO16);
5032 }
5033 }
5034 else
5035 {
5036 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
5037 && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
5038 {
5039 frag_grow (20);
f5040a92
AO
5040 macro_build ((char *) NULL, &icnt, &offset_expr,
5041 HAVE_NEWABI ? "addi" : "addiu",
c9914766
TS
5042 "t,r,j", tempreg, mips_gp_register,
5043 (int) BFD_RELOC_GPREL16);
98d3f06f
KH
5044 p = frag_var (rs_machine_dependent, 8, 0,
5045 RELAX_ENCODE (4, 8, 0, 4, 0,
5046 mips_opts.warn_about_macros),
5047 offset_expr.X_add_symbol, 0, NULL);
5048 }
5049 macro_build_lui (p, &icnt, &offset_expr, tempreg);
5050 if (p != NULL)
5051 p += 4;
f5040a92
AO
5052 macro_build (p, &icnt, &offset_expr,
5053 HAVE_NEWABI ? "addi" : "addiu",
98d3f06f
KH
5054 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5055 }
252b5132 5056 }
f5040a92 5057 else if (mips_pic == SVR4_PIC && ! mips_big_got && ! HAVE_NEWABI)
252b5132 5058 {
9117d219
NC
5059 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
5060
252b5132
RH
5061 /* If this is a reference to an external symbol, and there
5062 is no constant, we want
5063 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9117d219
NC
5064 or if tempreg is PIC_CALL_REG
5065 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
252b5132
RH
5066 For a local symbol, we want
5067 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5068 nop
5069 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5070
5071 If we have a small constant, and this is a reference to
5072 an external symbol, we want
5073 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5074 nop
5075 addiu $tempreg,$tempreg,<constant>
5076 For a local symbol, we want the same instruction
5077 sequence, but we output a BFD_RELOC_LO16 reloc on the
5078 addiu instruction.
5079
5080 If we have a large constant, and this is a reference to
5081 an external symbol, we want
5082 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5083 lui $at,<hiconstant>
5084 addiu $at,$at,<loconstant>
5085 addu $tempreg,$tempreg,$at
5086 For a local symbol, we want the same instruction
5087 sequence, but we output a BFD_RELOC_LO16 reloc on the
ed6fb7bd 5088 addiu instruction.
ed6fb7bd
SC
5089 */
5090
252b5132
RH
5091 expr1.X_add_number = offset_expr.X_add_number;
5092 offset_expr.X_add_number = 0;
5093 frag_grow (32);
9117d219
NC
5094 if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
5095 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
4d34fb5f
TS
5096 macro_build ((char *) NULL, &icnt, &offset_expr,
5097 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
c9914766 5098 "t,o(b)", tempreg, lw_reloc_type, mips_gp_register);
252b5132
RH
5099 if (expr1.X_add_number == 0)
5100 {
5101 int off;
f9419b05 5102 char *p;
252b5132
RH
5103
5104 if (breg == 0)
5105 off = 0;
5106 else
5107 {
5108 /* We're going to put in an addu instruction using
5109 tempreg, so we may as well insert the nop right
5110 now. */
5111 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5112 "nop", "");
5113 off = 4;
5114 }
5115 p = frag_var (rs_machine_dependent, 8 - off, 0,
5116 RELAX_ENCODE (0, 8 - off, -4 - off, 4 - off, 0,
5117 (breg == 0
5118 ? mips_opts.warn_about_macros
5119 : 0)),
c4e7957c 5120 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5121 if (breg == 0)
5122 {
5123 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5124 p += 4;
5125 }
5126 macro_build (p, &icnt, &expr1,
ca4e0257 5127 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
5128 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5129 /* FIXME: If breg == 0, and the next instruction uses
5130 $tempreg, then if this variant case is used an extra
5131 nop will be generated. */
5132 }
5133 else if (expr1.X_add_number >= -0x8000
5134 && expr1.X_add_number < 0x8000)
5135 {
5136 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5137 "nop", "");
5138 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 5139 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132 5140 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
c4e7957c
TS
5141 frag_var (rs_machine_dependent, 0, 0,
5142 RELAX_ENCODE (0, 0, -12, -4, 0, 0),
5143 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5144 }
5145 else
5146 {
5147 int off1;
5148
5149 /* If we are going to add in a base register, and the
5150 target register and the base register are the same,
5151 then we are using AT as a temporary register. Since
5152 we want to load the constant into AT, we add our
5153 current AT (from the global offset table) and the
5154 register into the register now, and pretend we were
5155 not using a base register. */
5156 if (breg != treg)
5157 off1 = 0;
5158 else
5159 {
5160 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5161 "nop", "");
5162 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5163 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5164 "d,v,t", treg, AT, breg);
5165 breg = 0;
5166 tempreg = treg;
5167 off1 = -8;
5168 }
5169
5170 /* Set mips_optimize around the lui instruction to avoid
5171 inserting an unnecessary nop after the lw. */
5172 hold_mips_optimize = mips_optimize;
5173 mips_optimize = 2;
c4e7957c 5174 macro_build_lui (NULL, &icnt, &expr1, AT);
252b5132
RH
5175 mips_optimize = hold_mips_optimize;
5176
5177 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 5178 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
5179 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5180 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5181 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132 5182 "d,v,t", tempreg, tempreg, AT);
c4e7957c
TS
5183 frag_var (rs_machine_dependent, 0, 0,
5184 RELAX_ENCODE (0, 0, -16 + off1, -8, 0, 0),
5185 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5186 used_at = 1;
5187 }
5188 }
f5040a92
AO
5189 else if (mips_pic == SVR4_PIC && ! mips_big_got && HAVE_NEWABI)
5190 {
5191 char *p = NULL;
5192 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_DISP;
5193 int adj = 0;
5194
5195 /* If this is a reference to an external, and there is no
5196 constant, or local symbol (*), with or without a
5197 constant, we want
5198 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5199 or if tempreg is PIC_CALL_REG
5200 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5201
5202 If we have a small constant, and this is a reference to
5203 an external symbol, we want
5204 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5205 addiu $tempreg,$tempreg,<constant>
5206
5207 If we have a large constant, and this is a reference to
5208 an external symbol, we want
5209 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5210 lui $at,<hiconstant>
5211 addiu $at,$at,<loconstant>
5212 addu $tempreg,$tempreg,$at
5213
5214 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
5215 local symbols, even though it introduces an additional
5216 instruction. */
5217
5218 frag_grow (28);
5219 if (offset_expr.X_add_number == 0 && tempreg == PIC_CALL_REG)
5220 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
5221 if (offset_expr.X_add_number)
5222 {
5223 frag_now->tc_frag_data.tc_fr_offset =
5224 expr1.X_add_number = offset_expr.X_add_number;
5225 offset_expr.X_add_number = 0;
5226
5227 macro_build ((char *) NULL, &icnt, &offset_expr,
5228 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5229 "t,o(b)", tempreg, lw_reloc_type,
5230 mips_gp_register);
5231
5232 if (expr1.X_add_number >= -0x8000
5233 && expr1.X_add_number < 0x8000)
5234 {
5235 macro_build ((char *) NULL, &icnt, &expr1,
5236 HAVE_32BIT_ADDRESSES ? "addi" : "daddiu",
5237 "t,r,j", tempreg, tempreg,
5238 (int) BFD_RELOC_LO16);
5239 p = frag_var (rs_machine_dependent, 4, 0,
5240 RELAX_ENCODE (8, 4, 0, 0, 0, 0),
5241 offset_expr.X_add_symbol, 0, NULL);
5242 }
5243 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number))
5244 {
5245 int dreg;
5246
5247 /* If we are going to add in a base register, and the
5248 target register and the base register are the same,
5249 then we are using AT as a temporary register. Since
5250 we want to load the constant into AT, we add our
5251 current AT (from the global offset table) and the
5252 register into the register now, and pretend we were
5253 not using a base register. */
5254 if (breg != treg)
5255 dreg = tempreg;
5256 else
5257 {
5258 assert (tempreg == AT);
5259 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5260 HAVE_32BIT_ADDRESSES ? "add" : "daddu",
5261 "d,v,t", treg, AT, breg);
5262 dreg = treg;
5263 adj = 4;
5264 }
5265
5266 macro_build_lui ((char *) NULL, &icnt, &expr1, AT);
5267 macro_build ((char *) NULL, &icnt, &expr1,
5268 HAVE_32BIT_ADDRESSES ? "addi" : "daddiu",
5269 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5270 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5271 HAVE_32BIT_ADDRESSES ? "add" : "daddu",
5272 "d,v,t", dreg, dreg, AT);
5273
5274 p = frag_var (rs_machine_dependent, 4 + adj, 0,
5275 RELAX_ENCODE (16 + adj, 4 + adj,
5276 0, 0, 0, 0),
5277 offset_expr.X_add_symbol, 0, NULL);
5278
5279 used_at = 1;
5280 }
5281 else
5282 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5283
5284 offset_expr.X_add_number = expr1.X_add_number;
5285
5286 macro_build (p, &icnt, &offset_expr,
5287 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5288 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_DISP,
5289 mips_gp_register);
5290 if (adj)
5291 {
5292 macro_build (p + 4, &icnt, (expressionS *) NULL,
5293 HAVE_32BIT_ADDRESSES ? "add" : "daddu",
5294 "d,v,t", treg, tempreg, breg);
5295 breg = 0;
5296 tempreg = treg;
5297 }
5298 }
5299 else
5300 {
5301 macro_build ((char *) NULL, &icnt, &offset_expr,
5302 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5303 "t,o(b)", tempreg, lw_reloc_type,
5304 mips_gp_register);
5305 if (lw_reloc_type != BFD_RELOC_MIPS_GOT_DISP)
5306 p = frag_var (rs_machine_dependent, 0, 0,
5307 RELAX_ENCODE (0, 0, -4, 0, 0, 0),
5308 offset_expr.X_add_symbol, 0, NULL);
5309 }
5310
5311 if (! p)
5312 {
5313 /* To avoid confusion in tc_gen_reloc, we must ensure
5314 that this does not become a variant frag. */
5315 frag_wane (frag_now);
5316 frag_new (0);
5317 }
5318 }
5319 else if (mips_pic == SVR4_PIC && ! HAVE_NEWABI)
252b5132
RH
5320 {
5321 int gpdel;
f9419b05 5322 char *p;
9117d219
NC
5323 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5324 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
ed6fb7bd 5325 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
252b5132
RH
5326
5327 /* This is the large GOT case. If this is a reference to an
5328 external symbol, and there is no constant, we want
5329 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5330 addu $tempreg,$tempreg,$gp
5331 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
9117d219
NC
5332 or if tempreg is PIC_CALL_REG
5333 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5334 addu $tempreg,$tempreg,$gp
5335 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
252b5132
RH
5336 For a local symbol, we want
5337 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5338 nop
5339 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5340
5341 If we have a small constant, and this is a reference to
5342 an external symbol, we want
5343 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5344 addu $tempreg,$tempreg,$gp
5345 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5346 nop
5347 addiu $tempreg,$tempreg,<constant>
5348 For a local symbol, we want
5349 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5350 nop
5351 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
5352
5353 If we have a large constant, and this is a reference to
5354 an external symbol, we want
5355 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5356 addu $tempreg,$tempreg,$gp
5357 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5358 lui $at,<hiconstant>
5359 addiu $at,$at,<loconstant>
5360 addu $tempreg,$tempreg,$at
5361 For a local symbol, we want
5362 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5363 lui $at,<hiconstant>
5364 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
5365 addu $tempreg,$tempreg,$at
f5040a92 5366 */
438c16b8 5367
252b5132
RH
5368 expr1.X_add_number = offset_expr.X_add_number;
5369 offset_expr.X_add_number = 0;
5370 frag_grow (52);
f7ea7ef2 5371 if (reg_needs_delay (mips_gp_register))
252b5132
RH
5372 gpdel = 4;
5373 else
5374 gpdel = 0;
9117d219
NC
5375 if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
5376 {
5377 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5378 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5379 }
252b5132 5380 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
9117d219 5381 tempreg, lui_reloc_type);
252b5132 5382 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5383 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 5384 "d,v,t", tempreg, tempreg, mips_gp_register);
252b5132 5385 macro_build ((char *) NULL, &icnt, &offset_expr,
4d34fb5f 5386 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
9117d219 5387 "t,o(b)", tempreg, lw_reloc_type, tempreg);
252b5132
RH
5388 if (expr1.X_add_number == 0)
5389 {
5390 int off;
5391
5392 if (breg == 0)
5393 off = 0;
5394 else
5395 {
5396 /* We're going to put in an addu instruction using
5397 tempreg, so we may as well insert the nop right
5398 now. */
5399 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5400 "nop", "");
5401 off = 4;
5402 }
5403
5404 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5405 RELAX_ENCODE (12 + off, 12 + gpdel, gpdel,
5406 8 + gpdel, 0,
5407 (breg == 0
5408 ? mips_opts.warn_about_macros
5409 : 0)),
c4e7957c 5410 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5411 }
5412 else if (expr1.X_add_number >= -0x8000
5413 && expr1.X_add_number < 0x8000)
5414 {
5415 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5416 "nop", "");
5417 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 5418 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
5419 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5420
5421 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5422 RELAX_ENCODE (20, 12 + gpdel, gpdel, 8 + gpdel, 0,
5423 (breg == 0
5424 ? mips_opts.warn_about_macros
5425 : 0)),
c4e7957c 5426 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5427 }
5428 else
5429 {
5430 int adj, dreg;
5431
5432 /* If we are going to add in a base register, and the
5433 target register and the base register are the same,
5434 then we are using AT as a temporary register. Since
5435 we want to load the constant into AT, we add our
5436 current AT (from the global offset table) and the
5437 register into the register now, and pretend we were
5438 not using a base register. */
5439 if (breg != treg)
5440 {
5441 adj = 0;
5442 dreg = tempreg;
5443 }
5444 else
5445 {
5446 assert (tempreg == AT);
5447 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5448 "nop", "");
5449 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5450 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5451 "d,v,t", treg, AT, breg);
5452 dreg = treg;
5453 adj = 8;
5454 }
5455
5456 /* Set mips_optimize around the lui instruction to avoid
5457 inserting an unnecessary nop after the lw. */
5458 hold_mips_optimize = mips_optimize;
5459 mips_optimize = 2;
c4e7957c 5460 macro_build_lui (NULL, &icnt, &expr1, AT);
252b5132
RH
5461 mips_optimize = hold_mips_optimize;
5462
5463 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 5464 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
5465 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5466 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5467 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5468 "d,v,t", dreg, dreg, AT);
5469
5470 p = frag_var (rs_machine_dependent, 16 + gpdel + adj, 0,
5471 RELAX_ENCODE (24 + adj, 16 + gpdel + adj, gpdel,
5472 8 + gpdel, 0,
5473 (breg == 0
5474 ? mips_opts.warn_about_macros
5475 : 0)),
c4e7957c 5476 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5477
5478 used_at = 1;
5479 }
5480
5481 if (gpdel > 0)
5482 {
5483 /* This is needed because this instruction uses $gp, but
f5040a92 5484 the first instruction on the main stream does not. */
252b5132
RH
5485 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5486 p += 4;
5487 }
ed6fb7bd 5488
252b5132 5489 macro_build (p, &icnt, &offset_expr,
4d34fb5f 5490 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
ed6fb7bd
SC
5491 "t,o(b)", tempreg,
5492 local_reloc_type,
c9914766 5493 mips_gp_register);
252b5132 5494 p += 4;
f5040a92 5495 if (expr1.X_add_number >= -0x8000
252b5132
RH
5496 && expr1.X_add_number < 0x8000)
5497 {
5498 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5499 p += 4;
5500 macro_build (p, &icnt, &expr1,
ca4e0257 5501 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
5502 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5503 /* FIXME: If add_number is 0, and there was no base
f5040a92
AO
5504 register, the external symbol case ended with a load,
5505 so if the symbol turns out to not be external, and
5506 the next instruction uses tempreg, an unnecessary nop
5507 will be inserted. */
252b5132
RH
5508 }
5509 else
5510 {
5511 if (breg == treg)
5512 {
5513 /* We must add in the base register now, as in the
f5040a92 5514 external symbol case. */
252b5132
RH
5515 assert (tempreg == AT);
5516 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5517 p += 4;
5518 macro_build (p, &icnt, (expressionS *) NULL,
ca4e0257 5519 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5520 "d,v,t", treg, AT, breg);
5521 p += 4;
5522 tempreg = treg;
5523 /* We set breg to 0 because we have arranged to add
f5040a92 5524 it in in both cases. */
252b5132
RH
5525 breg = 0;
5526 }
5527
5528 macro_build_lui (p, &icnt, &expr1, AT);
5529 p += 4;
5530 macro_build (p, &icnt, &expr1,
ca4e0257 5531 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
5532 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5533 p += 4;
5534 macro_build (p, &icnt, (expressionS *) NULL,
ca4e0257 5535 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5536 "d,v,t", tempreg, tempreg, AT);
5537 p += 4;
5538 }
5539 }
f5040a92
AO
5540 else if (mips_pic == SVR4_PIC && HAVE_NEWABI)
5541 {
5542 char *p = NULL;
5543 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5544 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
5545 int adj = 0;
5546
5547 /* This is the large GOT case. If this is a reference to an
5548 external symbol, and there is no constant, we want
5549 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5550 add $tempreg,$tempreg,$gp
5551 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5552 or if tempreg is PIC_CALL_REG
5553 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5554 add $tempreg,$tempreg,$gp
5555 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
5556
5557 If we have a small constant, and this is a reference to
5558 an external symbol, we want
5559 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5560 add $tempreg,$tempreg,$gp
5561 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5562 addi $tempreg,$tempreg,<constant>
5563
5564 If we have a large constant, and this is a reference to
5565 an external symbol, we want
5566 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5567 addu $tempreg,$tempreg,$gp
5568 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5569 lui $at,<hiconstant>
5570 addi $at,$at,<loconstant>
5571 add $tempreg,$tempreg,$at
5572
5573 If we have NewABI, and we know it's a local symbol, we want
5574 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
5575 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
5576 otherwise we have to resort to GOT_HI16/GOT_LO16. */
5577
5578 frag_grow (40);
5579
5580 frag_now->tc_frag_data.tc_fr_offset =
5581 expr1.X_add_number = offset_expr.X_add_number;
5582 offset_expr.X_add_number = 0;
5583
5584 if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
5585 {
5586 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5587 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5588 }
5589 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5590 tempreg, lui_reloc_type);
5591 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5592 HAVE_32BIT_ADDRESSES ? "add" : "daddu",
5593 "d,v,t", tempreg, tempreg, mips_gp_register);
5594 macro_build ((char *) NULL, &icnt, &offset_expr,
5595 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5596 "t,o(b)", tempreg, lw_reloc_type, tempreg);
5597
5598 if (expr1.X_add_number == 0)
5599 {
5600 p = frag_var (rs_machine_dependent, 8, 0,
5601 RELAX_ENCODE (12, 8, 0, 4, 0,
5602 mips_opts.warn_about_macros),
5603 offset_expr.X_add_symbol, 0, NULL);
684022ea 5604 }
f5040a92
AO
5605 else if (expr1.X_add_number >= -0x8000
5606 && expr1.X_add_number < 0x8000)
5607 {
5608 macro_build ((char *) NULL, &icnt, &expr1,
5609 HAVE_32BIT_ADDRESSES ? "addi" : "daddiu",
5610 "t,r,j", tempreg, tempreg,
5611 (int) BFD_RELOC_LO16);
5612 p = frag_var (rs_machine_dependent, 8, 0,
5613 RELAX_ENCODE (16, 8, 0, 4, 0,
5614 mips_opts.warn_about_macros),
5615 offset_expr.X_add_symbol, 0, NULL);
5616 }
5617 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number))
5618 {
5619 int dreg;
5620
5621 /* If we are going to add in a base register, and the
5622 target register and the base register are the same,
5623 then we are using AT as a temporary register. Since
5624 we want to load the constant into AT, we add our
5625 current AT (from the global offset table) and the
5626 register into the register now, and pretend we were
5627 not using a base register. */
5628 if (breg != treg)
5629 dreg = tempreg;
5630 else
5631 {
5632 assert (tempreg == AT);
5633 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5634 HAVE_32BIT_ADDRESSES ? "add" : "daddu",
5635 "d,v,t", treg, AT, breg);
5636 dreg = treg;
5637 adj = 4;
5638 }
5639
5640 /* Set mips_optimize around the lui instruction to avoid
5641 inserting an unnecessary nop after the lw. */
5642 macro_build_lui ((char *) NULL, &icnt, &expr1, AT);
5643 macro_build ((char *) NULL, &icnt, &expr1,
5644 HAVE_32BIT_ADDRESSES ? "addi" : "daddiu",
5645 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5646 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5647 HAVE_32BIT_ADDRESSES ? "add" : "daddu",
5648 "d,v,t", dreg, dreg, AT);
5649
5650 p = frag_var (rs_machine_dependent, 8 + adj, 0,
5651 RELAX_ENCODE (24 + adj, 8 + adj,
5652 0, 4, 0,
5653 (breg == 0
5654 ? mips_opts.warn_about_macros
5655 : 0)),
5656 offset_expr.X_add_symbol, 0, NULL);
5657
5658 used_at = 1;
5659 }
5660 else
5661 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
5662
5663 offset_expr.X_add_number = expr1.X_add_number;
5664 macro_build (p, &icnt, &offset_expr,
5665 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
5666 tempreg,
5667 (int) BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
5668 macro_build (p + 4, &icnt, &offset_expr,
5669 HAVE_32BIT_ADDRESSES ? "addi" : "daddiu", "t,r,j",
5670 tempreg, tempreg, (int) BFD_RELOC_MIPS_GOT_OFST);
5671 if (adj)
5672 {
5673 macro_build (p + 8, &icnt, (expressionS *) NULL,
5674 HAVE_32BIT_ADDRESSES ? "add" : "daddu",
5675 "d,v,t", treg, tempreg, breg);
5676 breg = 0;
5677 tempreg = treg;
5678 }
5679 }
252b5132
RH
5680 else if (mips_pic == EMBEDDED_PIC)
5681 {
5682 /* We use
cdf6fd85 5683 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
5684 */
5685 macro_build ((char *) NULL, &icnt, &offset_expr,
c9914766
TS
5686 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
5687 tempreg, mips_gp_register, (int) BFD_RELOC_GPREL16);
252b5132
RH
5688 }
5689 else
5690 abort ();
5691
5692 if (breg != 0)
4d34fb5f
TS
5693 {
5694 char *s;
5695
5696 if (mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
f5040a92
AO
5697 s = (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" :
5698 HAVE_NEWABI ? "add" : "addu";
4d34fb5f 5699 else
f5040a92 5700 s = HAVE_64BIT_ADDRESSES ? "daddu" : HAVE_NEWABI ? "add" : "addu";
4d34fb5f
TS
5701
5702 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s,
5703 "d,v,t", treg, tempreg, breg);
5704 }
252b5132
RH
5705
5706 if (! used_at)
5707 return;
5708
5709 break;
5710
5711 case M_J_A:
5712 /* The j instruction may not be used in PIC code, since it
5713 requires an absolute address. We convert it to a b
5714 instruction. */
5715 if (mips_pic == NO_PIC)
5716 macro_build ((char *) NULL, &icnt, &offset_expr, "j", "a");
5717 else
5718 macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
5719 return;
5720
5721 /* The jal instructions must be handled as macros because when
5722 generating PIC code they expand to multi-instruction
5723 sequences. Normally they are simple instructions. */
5724 case M_JAL_1:
5725 dreg = RA;
5726 /* Fall through. */
5727 case M_JAL_2:
5728 if (mips_pic == NO_PIC
5729 || mips_pic == EMBEDDED_PIC)
5730 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5731 "d,s", dreg, sreg);
5732 else if (mips_pic == SVR4_PIC)
5733 {
5734 if (sreg != PIC_CALL_REG)
5735 as_warn (_("MIPS PIC call to register other than $25"));
bdaaa2e1 5736
252b5132
RH
5737 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5738 "d,s", dreg, sreg);
6478892d 5739 if (! HAVE_NEWABI)
252b5132 5740 {
6478892d
TS
5741 if (mips_cprestore_offset < 0)
5742 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5743 else
5744 {
7a621144
DJ
5745 if (! mips_frame_reg_valid)
5746 {
5747 as_warn (_("No .frame pseudo-op used in PIC code"));
5748 /* Quiet this warning. */
5749 mips_frame_reg_valid = 1;
5750 }
5751 if (! mips_cprestore_valid)
5752 {
5753 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5754 /* Quiet this warning. */
5755 mips_cprestore_valid = 1;
5756 }
6478892d 5757 expr1.X_add_number = mips_cprestore_offset;
885add95
CD
5758 macro_build_ldst_constoffset ((char *) NULL, &icnt, &expr1,
5759 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5760 mips_gp_register, mips_frame_reg);
6478892d 5761 }
252b5132
RH
5762 }
5763 }
5764 else
5765 abort ();
5766
5767 return;
5768
5769 case M_JAL_A:
5770 if (mips_pic == NO_PIC)
5771 macro_build ((char *) NULL, &icnt, &offset_expr, "jal", "a");
5772 else if (mips_pic == SVR4_PIC)
5773 {
f9419b05
TS
5774 char *p;
5775
252b5132
RH
5776 /* If this is a reference to an external symbol, and we are
5777 using a small GOT, we want
5778 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5779 nop
f9419b05 5780 jalr $ra,$25
252b5132
RH
5781 nop
5782 lw $gp,cprestore($sp)
5783 The cprestore value is set using the .cprestore
5784 pseudo-op. If we are using a big GOT, we want
5785 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5786 addu $25,$25,$gp
5787 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
5788 nop
f9419b05 5789 jalr $ra,$25
252b5132
RH
5790 nop
5791 lw $gp,cprestore($sp)
5792 If the symbol is not external, we want
5793 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5794 nop
5795 addiu $25,$25,<sym> (BFD_RELOC_LO16)
f9419b05 5796 jalr $ra,$25
252b5132 5797 nop
438c16b8 5798 lw $gp,cprestore($sp)
f5040a92
AO
5799
5800 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
5801 sequences above, minus nops, unless the symbol is local,
5802 which enables us to use GOT_PAGE/GOT_OFST (big got) or
5803 GOT_DISP. */
438c16b8 5804 if (HAVE_NEWABI)
252b5132 5805 {
f5040a92
AO
5806 if (! mips_big_got)
5807 {
5808 frag_grow (4);
5809 macro_build ((char *) NULL, &icnt, &offset_expr,
5810 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5811 "t,o(b)", PIC_CALL_REG,
5812 (int) BFD_RELOC_MIPS_CALL16,
5813 mips_gp_register);
5814 frag_var (rs_machine_dependent, 0, 0,
5815 RELAX_ENCODE (0, 0, -4, 0, 0, 0),
5816 offset_expr.X_add_symbol, 0, NULL);
5817 }
5818 else
5819 {
5820 frag_grow (20);
5821 macro_build ((char *) NULL, &icnt, &offset_expr, "lui",
5822 "t,u", PIC_CALL_REG,
5823 (int) BFD_RELOC_MIPS_CALL_HI16);
5824 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5825 HAVE_32BIT_ADDRESSES ? "add" : "daddu",
5826 "d,v,t", PIC_CALL_REG, PIC_CALL_REG,
5827 mips_gp_register);
5828 macro_build ((char *) NULL, &icnt, &offset_expr,
5829 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5830 "t,o(b)", PIC_CALL_REG,
5831 (int) BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
5832 p = frag_var (rs_machine_dependent, 8, 0,
5833 RELAX_ENCODE (12, 8, 0, 4, 0, 0),
5834 offset_expr.X_add_symbol, 0, NULL);
5835 macro_build (p, &icnt, &offset_expr,
5836 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
5837 tempreg, (int) BFD_RELOC_MIPS_GOT_PAGE,
5838 mips_gp_register);
5839 macro_build (p + 4, &icnt, &offset_expr,
5840 HAVE_32BIT_ADDRESSES ? "addi" : "daddiu",
5841 "t,r,j", tempreg, tempreg,
5842 (int) BFD_RELOC_MIPS_GOT_OFST);
5843 }
684022ea 5844
438c16b8 5845 macro_build_jalr (icnt, &offset_expr);
252b5132
RH
5846 }
5847 else
5848 {
438c16b8
TS
5849 frag_grow (40);
5850 if (! mips_big_got)
5851 {
5852 macro_build ((char *) NULL, &icnt, &offset_expr,
5853 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5854 "t,o(b)", PIC_CALL_REG,
5855 (int) BFD_RELOC_MIPS_CALL16, mips_gp_register);
5856 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5857 "nop", "");
5858 p = frag_var (rs_machine_dependent, 4, 0,
5859 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5860 offset_expr.X_add_symbol, 0, NULL);
5861 }
252b5132 5862 else
252b5132 5863 {
438c16b8
TS
5864 int gpdel;
5865
5866 if (reg_needs_delay (mips_gp_register))
5867 gpdel = 4;
5868 else
5869 gpdel = 0;
5870 macro_build ((char *) NULL, &icnt, &offset_expr, "lui",
5871 "t,u", PIC_CALL_REG,
5872 (int) BFD_RELOC_MIPS_CALL_HI16);
5873 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5874 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5875 "d,v,t", PIC_CALL_REG, PIC_CALL_REG,
5876 mips_gp_register);
5877 macro_build ((char *) NULL, &icnt, &offset_expr,
5878 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5879 "t,o(b)", PIC_CALL_REG,
5880 (int) BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
5881 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5882 "nop", "");
5883 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5884 RELAX_ENCODE (16, 12 + gpdel, gpdel,
5885 8 + gpdel, 0, 0),
5886 offset_expr.X_add_symbol, 0, NULL);
5887 if (gpdel > 0)
5888 {
5889 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5890 p += 4;
5891 }
5892 macro_build (p, &icnt, &offset_expr,
5893 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5894 "t,o(b)", PIC_CALL_REG,
5895 (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
5896 p += 4;
252b5132
RH
5897 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5898 p += 4;
5899 }
5900 macro_build (p, &icnt, &offset_expr,
438c16b8
TS
5901 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5902 "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
5903 (int) BFD_RELOC_LO16);
5904 macro_build_jalr (icnt, &offset_expr);
5905
6478892d
TS
5906 if (mips_cprestore_offset < 0)
5907 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5908 else
5909 {
7a621144
DJ
5910 if (! mips_frame_reg_valid)
5911 {
5912 as_warn (_("No .frame pseudo-op used in PIC code"));
5913 /* Quiet this warning. */
5914 mips_frame_reg_valid = 1;
5915 }
5916 if (! mips_cprestore_valid)
5917 {
5918 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5919 /* Quiet this warning. */
5920 mips_cprestore_valid = 1;
5921 }
6478892d
TS
5922 if (mips_opts.noreorder)
5923 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
98d3f06f 5924 "nop", "");
6478892d 5925 expr1.X_add_number = mips_cprestore_offset;
885add95
CD
5926 macro_build_ldst_constoffset ((char *) NULL, &icnt, &expr1,
5927 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5928 mips_gp_register, mips_frame_reg);
6478892d 5929 }
252b5132
RH
5930 }
5931 }
5932 else if (mips_pic == EMBEDDED_PIC)
5933 {
5934 macro_build ((char *) NULL, &icnt, &offset_expr, "bal", "p");
5935 /* The linker may expand the call to a longer sequence which
5936 uses $at, so we must break rather than return. */
5937 break;
5938 }
5939 else
5940 abort ();
5941
5942 return;
5943
5944 case M_LB_AB:
5945 s = "lb";
5946 goto ld;
5947 case M_LBU_AB:
5948 s = "lbu";
5949 goto ld;
5950 case M_LH_AB:
5951 s = "lh";
5952 goto ld;
5953 case M_LHU_AB:
5954 s = "lhu";
5955 goto ld;
5956 case M_LW_AB:
5957 s = "lw";
5958 goto ld;
5959 case M_LWC0_AB:
5960 s = "lwc0";
bdaaa2e1 5961 /* Itbl support may require additional care here. */
252b5132
RH
5962 coproc = 1;
5963 goto ld;
5964 case M_LWC1_AB:
5965 s = "lwc1";
bdaaa2e1 5966 /* Itbl support may require additional care here. */
252b5132
RH
5967 coproc = 1;
5968 goto ld;
5969 case M_LWC2_AB:
5970 s = "lwc2";
bdaaa2e1 5971 /* Itbl support may require additional care here. */
252b5132
RH
5972 coproc = 1;
5973 goto ld;
5974 case M_LWC3_AB:
5975 s = "lwc3";
bdaaa2e1 5976 /* Itbl support may require additional care here. */
252b5132
RH
5977 coproc = 1;
5978 goto ld;
5979 case M_LWL_AB:
5980 s = "lwl";
5981 lr = 1;
5982 goto ld;
5983 case M_LWR_AB:
5984 s = "lwr";
5985 lr = 1;
5986 goto ld;
5987 case M_LDC1_AB:
ec68c924 5988 if (mips_arch == CPU_R4650)
252b5132
RH
5989 {
5990 as_bad (_("opcode not supported on this processor"));
5991 return;
5992 }
5993 s = "ldc1";
bdaaa2e1 5994 /* Itbl support may require additional care here. */
252b5132
RH
5995 coproc = 1;
5996 goto ld;
5997 case M_LDC2_AB:
5998 s = "ldc2";
bdaaa2e1 5999 /* Itbl support may require additional care here. */
252b5132
RH
6000 coproc = 1;
6001 goto ld;
6002 case M_LDC3_AB:
6003 s = "ldc3";
bdaaa2e1 6004 /* Itbl support may require additional care here. */
252b5132
RH
6005 coproc = 1;
6006 goto ld;
6007 case M_LDL_AB:
6008 s = "ldl";
6009 lr = 1;
6010 goto ld;
6011 case M_LDR_AB:
6012 s = "ldr";
6013 lr = 1;
6014 goto ld;
6015 case M_LL_AB:
6016 s = "ll";
6017 goto ld;
6018 case M_LLD_AB:
6019 s = "lld";
6020 goto ld;
6021 case M_LWU_AB:
6022 s = "lwu";
6023 ld:
6024 if (breg == treg || coproc || lr)
6025 {
6026 tempreg = AT;
6027 used_at = 1;
6028 }
6029 else
6030 {
6031 tempreg = treg;
6032 used_at = 0;
6033 }
6034 goto ld_st;
6035 case M_SB_AB:
6036 s = "sb";
6037 goto st;
6038 case M_SH_AB:
6039 s = "sh";
6040 goto st;
6041 case M_SW_AB:
6042 s = "sw";
6043 goto st;
6044 case M_SWC0_AB:
6045 s = "swc0";
bdaaa2e1 6046 /* Itbl support may require additional care here. */
252b5132
RH
6047 coproc = 1;
6048 goto st;
6049 case M_SWC1_AB:
6050 s = "swc1";
bdaaa2e1 6051 /* Itbl support may require additional care here. */
252b5132
RH
6052 coproc = 1;
6053 goto st;
6054 case M_SWC2_AB:
6055 s = "swc2";
bdaaa2e1 6056 /* Itbl support may require additional care here. */
252b5132
RH
6057 coproc = 1;
6058 goto st;
6059 case M_SWC3_AB:
6060 s = "swc3";
bdaaa2e1 6061 /* Itbl support may require additional care here. */
252b5132
RH
6062 coproc = 1;
6063 goto st;
6064 case M_SWL_AB:
6065 s = "swl";
6066 goto st;
6067 case M_SWR_AB:
6068 s = "swr";
6069 goto st;
6070 case M_SC_AB:
6071 s = "sc";
6072 goto st;
6073 case M_SCD_AB:
6074 s = "scd";
6075 goto st;
6076 case M_SDC1_AB:
ec68c924 6077 if (mips_arch == CPU_R4650)
252b5132
RH
6078 {
6079 as_bad (_("opcode not supported on this processor"));
6080 return;
6081 }
6082 s = "sdc1";
6083 coproc = 1;
bdaaa2e1 6084 /* Itbl support may require additional care here. */
252b5132
RH
6085 goto st;
6086 case M_SDC2_AB:
6087 s = "sdc2";
bdaaa2e1 6088 /* Itbl support may require additional care here. */
252b5132
RH
6089 coproc = 1;
6090 goto st;
6091 case M_SDC3_AB:
6092 s = "sdc3";
bdaaa2e1 6093 /* Itbl support may require additional care here. */
252b5132
RH
6094 coproc = 1;
6095 goto st;
6096 case M_SDL_AB:
6097 s = "sdl";
6098 goto st;
6099 case M_SDR_AB:
6100 s = "sdr";
6101 st:
6102 tempreg = AT;
6103 used_at = 1;
6104 ld_st:
bdaaa2e1 6105 /* Itbl support may require additional care here. */
252b5132
RH
6106 if (mask == M_LWC1_AB
6107 || mask == M_SWC1_AB
6108 || mask == M_LDC1_AB
6109 || mask == M_SDC1_AB
6110 || mask == M_L_DAB
6111 || mask == M_S_DAB)
6112 fmt = "T,o(b)";
6113 else if (coproc)
6114 fmt = "E,o(b)";
6115 else
6116 fmt = "t,o(b)";
6117
afdbd6d0
CD
6118 /* For embedded PIC, we allow loads where the offset is calculated
6119 by subtracting a symbol in the current segment from an unknown
6120 symbol, relative to a base register, e.g.:
6121 <op> $treg, <sym>-<localsym>($breg)
6122 This is used by the compiler for switch statements. */
76b3015f 6123 if (mips_pic == EMBEDDED_PIC
afdbd6d0
CD
6124 && offset_expr.X_op == O_subtract
6125 && (symbol_constant_p (offset_expr.X_op_symbol)
6126 ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
6127 : (symbol_equated_p (offset_expr.X_op_symbol)
6128 && (S_GET_SEGMENT
6129 (symbol_get_value_expression (offset_expr.X_op_symbol)
6130 ->X_add_symbol)
6131 == now_seg)))
6132 && breg != 0
6133 && (offset_expr.X_add_number == 0
6134 || OUTPUT_FLAVOR == bfd_target_elf_flavour))
6135 {
6136 /* For this case, we output the instructions:
6137 lui $tempreg,<sym> (BFD_RELOC_PCREL_HI16_S)
6138 addiu $tempreg,$tempreg,$breg
6139 <op> $treg,<sym>($tempreg) (BFD_RELOC_PCREL_LO16)
6140 If the relocation would fit entirely in 16 bits, it would be
6141 nice to emit:
6142 <op> $treg,<sym>($breg) (BFD_RELOC_PCREL_LO16)
6143 instead, but that seems quite difficult. */
6144 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
6145 tempreg, (int) BFD_RELOC_PCREL_HI16_S);
6146 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6147 ((bfd_arch_bits_per_address (stdoutput) == 32
6148 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
f5040a92 6149 ? HAVE_NEWABI ? "add" : "addu" : "daddu"),
afdbd6d0
CD
6150 "d,v,t", tempreg, tempreg, breg);
6151 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt, treg,
6152 (int) BFD_RELOC_PCREL_LO16, tempreg);
6153 if (! used_at)
6154 return;
6155 break;
6156 }
6157
252b5132
RH
6158 if (offset_expr.X_op != O_constant
6159 && offset_expr.X_op != O_symbol)
6160 {
6161 as_bad (_("expression too complex"));
6162 offset_expr.X_op = O_constant;
6163 }
6164
6165 /* A constant expression in PIC code can be handled just as it
6166 is in non PIC code. */
6167 if (mips_pic == NO_PIC
6168 || offset_expr.X_op == O_constant)
6169 {
f9419b05
TS
6170 char *p;
6171
252b5132
RH
6172 /* If this is a reference to a GP relative symbol, and there
6173 is no base register, we want
cdf6fd85 6174 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
6175 Otherwise, if there is no base register, we want
6176 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
6177 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6178 If we have a constant, we need two instructions anyhow,
6179 so we always use the latter form.
6180
6181 If we have a base register, and this is a reference to a
6182 GP relative symbol, we want
6183 addu $tempreg,$breg,$gp
cdf6fd85 6184 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
6185 Otherwise we want
6186 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
6187 addu $tempreg,$tempreg,$breg
6188 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245 6189 With a constant we always use the latter case.
76b3015f 6190
d6bc6245
TS
6191 With 64bit address space and no base register and $at usable,
6192 we want
6193 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
6194 lui $at,<sym> (BFD_RELOC_HI16_S)
6195 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
6196 dsll32 $tempreg,0
6197 daddu $tempreg,$at
6198 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6199 If we have a base register, we want
6200 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
6201 lui $at,<sym> (BFD_RELOC_HI16_S)
6202 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
6203 daddu $at,$breg
6204 dsll32 $tempreg,0
6205 daddu $tempreg,$at
6206 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6207
6208 Without $at we can't generate the optimal path for superscalar
6209 processors here since this would require two temporary registers.
6210 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
6211 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
6212 dsll $tempreg,16
6213 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
6214 dsll $tempreg,16
6215 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6216 If we have a base register, we want
6217 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
6218 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
6219 dsll $tempreg,16
6220 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
6221 dsll $tempreg,16
6222 daddu $tempreg,$tempreg,$breg
6223 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6373ee54
CD
6224
6225 If we have 64-bit addresses, as an optimization, for
6226 addresses which are 32-bit constants (e.g. kseg0/kseg1
6227 addresses) we fall back to the 32-bit address generation
78d32a17
MR
6228 mechanism since it is more efficient. Note that due to
6229 the signed offset used by memory operations, the 32-bit
6230 range is shifted down by 32768 here. This code should
6373ee54
CD
6231 probably attempt to generate 64-bit constants more
6232 efficiently in general.
9214dd3b
AO
6233
6234 As an extension for architectures with 64-bit registers,
6235 we don't truncate 64-bit addresses given as literal
6236 constants down to 32 bits, to support existing practice
6237 in the mips64 Linux (the kernel), that compiles source
6238 files with -mabi=64, assembling them as o32 or n32 (with
6239 -Wa,-32 or -Wa,-n32). This is not beautiful, but since
6240 the whole kernel is loaded into a memory region that is
6241 addressible with sign-extended 32-bit addresses, it is
6242 wasteful to compute the upper 32 bits of every
6243 non-literal address, that takes more space and time.
6244 Some day this should probably be implemented as an
6245 assembler option, such that the kernel doesn't have to
6246 use such ugly hacks, even though it will still have to
6247 end up converting the binary to ELF32 for a number of
6248 platforms whose boot loaders don't support ELF64
6249 binaries. */
5a7a0b7b
AO
6250 if ((offset_expr.X_op != O_constant && HAVE_64BIT_ADDRESSES)
6251 || (offset_expr.X_op == O_constant
eb0a7d52 6252 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000)
5a7a0b7b 6253 && HAVE_64BIT_ADDRESS_CONSTANTS))
d6bc6245
TS
6254 {
6255 p = NULL;
6256
6257 /* We don't do GP optimization for now because RELAX_ENCODE can't
6258 hold the data for such large chunks. */
6259
460597ba 6260 if (used_at == 0 && ! mips_opts.noat)
d6bc6245
TS
6261 {
6262 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
6263 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
6264 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
6265 AT, (int) BFD_RELOC_HI16_S);
6266 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
6267 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
6268 if (breg != 0)
2396cfb9
TS
6269 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
6270 "d,v,t", AT, AT, breg);
6271 macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
6272 "d,w,<", tempreg, tempreg, 0);
6273 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
6274 "d,v,t", tempreg, tempreg, AT);
d6bc6245
TS
6275 macro_build (p, &icnt, &offset_expr, s,
6276 fmt, treg, (int) BFD_RELOC_LO16, tempreg);
6277 used_at = 1;
6278 }
6279 else
6280 {
6281 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
6282 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
6283 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
6284 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
2396cfb9
TS
6285 macro_build (p, &icnt, (expressionS *) NULL, "dsll",
6286 "d,w,<", tempreg, tempreg, 16);
d6bc6245
TS
6287 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
6288 tempreg, tempreg, (int) BFD_RELOC_HI16_S);
2396cfb9
TS
6289 macro_build (p, &icnt, (expressionS *) NULL, "dsll",
6290 "d,w,<", tempreg, tempreg, 16);
d6bc6245 6291 if (breg != 0)
2396cfb9
TS
6292 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
6293 "d,v,t", tempreg, tempreg, breg);
d6bc6245
TS
6294 macro_build (p, &icnt, &offset_expr, s,
6295 fmt, treg, (int) BFD_RELOC_LO16, tempreg);
6296 }
6297
6298 return;
6299 }
5a7a0b7b 6300 else if (offset_expr.X_op == O_constant
eb0a7d52 6301 && !HAVE_64BIT_ADDRESS_CONSTANTS
5a7a0b7b
AO
6302 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
6303 as_bad (_("load/store address overflow (max 32 bits)"));
76b3015f 6304
252b5132
RH
6305 if (breg == 0)
6306 {
e7d556df 6307 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
252b5132
RH
6308 || nopic_need_relax (offset_expr.X_add_symbol, 1))
6309 p = NULL;
6310 else
6311 {
6312 frag_grow (20);
6313 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
c9914766
TS
6314 treg, (int) BFD_RELOC_GPREL16,
6315 mips_gp_register);
252b5132
RH
6316 p = frag_var (rs_machine_dependent, 8, 0,
6317 RELAX_ENCODE (4, 8, 0, 4, 0,
6318 (mips_opts.warn_about_macros
6319 || (used_at
6320 && mips_opts.noat))),
956cd1d6 6321 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
6322 used_at = 0;
6323 }
6324 macro_build_lui (p, &icnt, &offset_expr, tempreg);
6325 if (p != NULL)
6326 p += 4;
6327 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
6328 (int) BFD_RELOC_LO16, tempreg);
6329 }
6330 else
6331 {
e7d556df 6332 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
252b5132
RH
6333 || nopic_need_relax (offset_expr.X_add_symbol, 1))
6334 p = NULL;
6335 else
6336 {
6337 frag_grow (28);
6338 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
f5040a92
AO
6339 HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
6340 ? "add" : "addu" : "daddu",
c9914766 6341 "d,v,t", tempreg, breg, mips_gp_register);
252b5132 6342 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
cdf6fd85 6343 treg, (int) BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6344 p = frag_var (rs_machine_dependent, 12, 0,
6345 RELAX_ENCODE (8, 12, 0, 8, 0, 0),
956cd1d6 6346 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
6347 }
6348 macro_build_lui (p, &icnt, &offset_expr, tempreg);
6349 if (p != NULL)
6350 p += 4;
6351 macro_build (p, &icnt, (expressionS *) NULL,
f5040a92
AO
6352 HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
6353 ? "add" : "addu" : "daddu",
252b5132
RH
6354 "d,v,t", tempreg, tempreg, breg);
6355 if (p != NULL)
6356 p += 4;
6357 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
6358 (int) BFD_RELOC_LO16, tempreg);
6359 }
6360 }
6361 else if (mips_pic == SVR4_PIC && ! mips_big_got)
6362 {
f9419b05 6363 char *p;
ed6fb7bd 6364 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
f9419b05 6365
252b5132
RH
6366 /* If this is a reference to an external symbol, we want
6367 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6368 nop
6369 <op> $treg,0($tempreg)
6370 Otherwise we want
6371 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6372 nop
6373 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6374 <op> $treg,0($tempreg)
f5040a92
AO
6375
6376 For NewABI, we want
6377 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6378 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
6379
252b5132
RH
6380 If there is a base register, we add it to $tempreg before
6381 the <op>. If there is a constant, we stick it in the
6382 <op> instruction. We don't handle constants larger than
6383 16 bits, because we have no way to load the upper 16 bits
6384 (actually, we could handle them for the subset of cases
6385 in which we are not using $at). */
6386 assert (offset_expr.X_op == O_symbol);
f5040a92
AO
6387 if (HAVE_NEWABI)
6388 {
6389 macro_build ((char *) NULL, &icnt, &offset_expr,
6390 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6391 "t,o(b)", tempreg, BFD_RELOC_MIPS_GOT_PAGE,
6392 mips_gp_register);
6393 if (breg != 0)
6394 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6395 HAVE_32BIT_ADDRESSES ? "add" : "daddu",
6396 "d,v,t", tempreg, tempreg, breg);
6397 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt, treg,
6398 (int) BFD_RELOC_MIPS_GOT_OFST, tempreg);
6399
6400 if (! used_at)
6401 return;
6402
6403 break;
6404 }
252b5132
RH
6405 expr1.X_add_number = offset_expr.X_add_number;
6406 offset_expr.X_add_number = 0;
6407 if (expr1.X_add_number < -0x8000
6408 || expr1.X_add_number >= 0x8000)
6409 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6410 frag_grow (20);
6411 macro_build ((char *) NULL, &icnt, &offset_expr,
c9914766 6412 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", tempreg,
ed6fb7bd 6413 (int) lw_reloc_type, mips_gp_register);
252b5132 6414 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
bdaaa2e1 6415 p = frag_var (rs_machine_dependent, 4, 0,
252b5132 6416 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
c4e7957c 6417 offset_expr.X_add_symbol, 0, NULL);
252b5132 6418 macro_build (p, &icnt, &offset_expr,
ca4e0257 6419 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
6420 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
6421 if (breg != 0)
6422 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6423 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
6424 "d,v,t", tempreg, tempreg, breg);
6425 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
6426 (int) BFD_RELOC_LO16, tempreg);
6427 }
f5040a92 6428 else if (mips_pic == SVR4_PIC && ! HAVE_NEWABI)
252b5132
RH
6429 {
6430 int gpdel;
f9419b05 6431 char *p;
252b5132
RH
6432
6433 /* If this is a reference to an external symbol, we want
6434 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6435 addu $tempreg,$tempreg,$gp
6436 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6437 <op> $treg,0($tempreg)
6438 Otherwise we want
6439 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6440 nop
6441 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
6442 <op> $treg,0($tempreg)
6443 If there is a base register, we add it to $tempreg before
6444 the <op>. If there is a constant, we stick it in the
6445 <op> instruction. We don't handle constants larger than
6446 16 bits, because we have no way to load the upper 16 bits
6447 (actually, we could handle them for the subset of cases
f5040a92 6448 in which we are not using $at). */
252b5132
RH
6449 assert (offset_expr.X_op == O_symbol);
6450 expr1.X_add_number = offset_expr.X_add_number;
6451 offset_expr.X_add_number = 0;
6452 if (expr1.X_add_number < -0x8000
6453 || expr1.X_add_number >= 0x8000)
6454 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
f7ea7ef2 6455 if (reg_needs_delay (mips_gp_register))
252b5132
RH
6456 gpdel = 4;
6457 else
6458 gpdel = 0;
6459 frag_grow (36);
6460 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
6461 tempreg, (int) BFD_RELOC_MIPS_GOT_HI16);
6462 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6463 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 6464 "d,v,t", tempreg, tempreg, mips_gp_register);
252b5132 6465 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 6466 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
6467 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_LO16,
6468 tempreg);
6469 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
6470 RELAX_ENCODE (12, 12 + gpdel, gpdel, 8 + gpdel, 0, 0),
c4e7957c 6471 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
6472 if (gpdel > 0)
6473 {
6474 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6475 p += 4;
6476 }
6477 macro_build (p, &icnt, &offset_expr,
ca4e0257 6478 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
c9914766
TS
6479 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16,
6480 mips_gp_register);
252b5132
RH
6481 p += 4;
6482 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6483 p += 4;
6484 macro_build (p, &icnt, &offset_expr,
ca4e0257 6485 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
6486 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
6487 if (breg != 0)
6488 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6489 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
6490 "d,v,t", tempreg, tempreg, breg);
6491 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
6492 (int) BFD_RELOC_LO16, tempreg);
6493 }
f5040a92
AO
6494 else if (mips_pic == SVR4_PIC && HAVE_NEWABI)
6495 {
6496 char *p;
6497 int bregsz = breg != 0 ? 4 : 0;
6498
6499 /* If this is a reference to an external symbol, we want
6500 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6501 add $tempreg,$tempreg,$gp
6502 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
6503 <op> $treg,<ofst>($tempreg)
6504 Otherwise, for local symbols, we want:
6505 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6506 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
6507 assert (offset_expr.X_op == O_symbol);
6508 frag_now->tc_frag_data.tc_fr_offset =
6509 expr1.X_add_number = offset_expr.X_add_number;
6510 offset_expr.X_add_number = 0;
6511 if (expr1.X_add_number < -0x8000
6512 || expr1.X_add_number >= 0x8000)
6513 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6514 frag_grow (36);
6515 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
6516 tempreg, (int) BFD_RELOC_MIPS_GOT_HI16);
6517 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6518 HAVE_32BIT_ADDRESSES ? "add" : "daddu",
6519 "d,v,t", tempreg, tempreg, mips_gp_register);
6520 macro_build ((char *) NULL, &icnt, &offset_expr,
6521 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6522 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_LO16,
6523 tempreg);
6524 if (breg != 0)
6525 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6526 HAVE_32BIT_ADDRESSES ? "add" : "daddu",
6527 "d,v,t", tempreg, tempreg, breg);
6528 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
6529 (int) BFD_RELOC_LO16, tempreg);
684022ea 6530
f5040a92
AO
6531 offset_expr.X_add_number = expr1.X_add_number;
6532 p = frag_var (rs_machine_dependent, 12 + bregsz, 0,
6533 RELAX_ENCODE (16 + bregsz, 8 + bregsz,
6534 0, 4 + bregsz, 0, 0),
6535 offset_expr.X_add_symbol, 0, NULL);
6536 macro_build (p, &icnt, &offset_expr,
6537 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
6538 "t,o(b)", tempreg,
6539 (int) BFD_RELOC_MIPS_GOT_PAGE,
6540 mips_gp_register);
6541 if (breg != 0)
6542 macro_build (p + 4, &icnt, (expressionS *) NULL,
6543 HAVE_32BIT_ADDRESSES ? "add" : "daddu",
6544 "d,v,t", tempreg, tempreg, breg);
6545 macro_build (p + 4 + bregsz, &icnt, &offset_expr, s, fmt, treg,
6546 (int) BFD_RELOC_MIPS_GOT_OFST, tempreg);
6547 }
252b5132
RH
6548 else if (mips_pic == EMBEDDED_PIC)
6549 {
6550 /* If there is no base register, we want
cdf6fd85 6551 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
6552 If there is a base register, we want
6553 addu $tempreg,$breg,$gp
cdf6fd85 6554 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
6555 */
6556 assert (offset_expr.X_op == O_symbol);
6557 if (breg == 0)
6558 {
6559 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
c9914766 6560 treg, (int) BFD_RELOC_GPREL16, mips_gp_register);
252b5132
RH
6561 used_at = 0;
6562 }
6563 else
6564 {
6565 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6566 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 6567 "d,v,t", tempreg, breg, mips_gp_register);
252b5132 6568 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
cdf6fd85 6569 treg, (int) BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6570 }
6571 }
6572 else
6573 abort ();
6574
6575 if (! used_at)
6576 return;
6577
6578 break;
6579
6580 case M_LI:
6581 case M_LI_S:
6582 load_register (&icnt, treg, &imm_expr, 0);
6583 return;
6584
6585 case M_DLI:
6586 load_register (&icnt, treg, &imm_expr, 1);
6587 return;
6588
6589 case M_LI_SS:
6590 if (imm_expr.X_op == O_constant)
6591 {
6592 load_register (&icnt, AT, &imm_expr, 0);
6593 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6594 "mtc1", "t,G", AT, treg);
6595 break;
6596 }
6597 else
6598 {
6599 assert (offset_expr.X_op == O_symbol
6600 && strcmp (segment_name (S_GET_SEGMENT
6601 (offset_expr.X_add_symbol)),
6602 ".lit4") == 0
6603 && offset_expr.X_add_number == 0);
6604 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
c9914766 6605 treg, (int) BFD_RELOC_MIPS_LITERAL, mips_gp_register);
252b5132
RH
6606 return;
6607 }
6608
6609 case M_LI_D:
ca4e0257
RS
6610 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
6611 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
6612 order 32 bits of the value and the low order 32 bits are either
6613 zero or in OFFSET_EXPR. */
252b5132
RH
6614 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6615 {
ca4e0257 6616 if (HAVE_64BIT_GPRS)
252b5132
RH
6617 load_register (&icnt, treg, &imm_expr, 1);
6618 else
6619 {
6620 int hreg, lreg;
6621
6622 if (target_big_endian)
6623 {
6624 hreg = treg;
6625 lreg = treg + 1;
6626 }
6627 else
6628 {
6629 hreg = treg + 1;
6630 lreg = treg;
6631 }
6632
6633 if (hreg <= 31)
6634 load_register (&icnt, hreg, &imm_expr, 0);
6635 if (lreg <= 31)
6636 {
6637 if (offset_expr.X_op == O_absent)
ea1fb5dc 6638 move_register (&icnt, lreg, 0);
252b5132
RH
6639 else
6640 {
6641 assert (offset_expr.X_op == O_constant);
6642 load_register (&icnt, lreg, &offset_expr, 0);
6643 }
6644 }
6645 }
6646 return;
6647 }
6648
6649 /* We know that sym is in the .rdata section. First we get the
6650 upper 16 bits of the address. */
6651 if (mips_pic == NO_PIC)
6652 {
956cd1d6 6653 macro_build_lui (NULL, &icnt, &offset_expr, AT);
252b5132
RH
6654 }
6655 else if (mips_pic == SVR4_PIC)
6656 {
6657 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 6658 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
c9914766
TS
6659 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6660 mips_gp_register);
252b5132
RH
6661 }
6662 else if (mips_pic == EMBEDDED_PIC)
6663 {
6664 /* For embedded PIC we pick up the entire address off $gp in
6665 a single instruction. */
6666 macro_build ((char *) NULL, &icnt, &offset_expr,
c9914766
TS
6667 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j", AT,
6668 mips_gp_register, (int) BFD_RELOC_GPREL16);
252b5132
RH
6669 offset_expr.X_op = O_constant;
6670 offset_expr.X_add_number = 0;
6671 }
6672 else
6673 abort ();
bdaaa2e1 6674
252b5132 6675 /* Now we load the register(s). */
ca4e0257 6676 if (HAVE_64BIT_GPRS)
252b5132
RH
6677 macro_build ((char *) NULL, &icnt, &offset_expr, "ld", "t,o(b)",
6678 treg, (int) BFD_RELOC_LO16, AT);
6679 else
6680 {
6681 macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
6682 treg, (int) BFD_RELOC_LO16, AT);
f9419b05 6683 if (treg != RA)
252b5132
RH
6684 {
6685 /* FIXME: How in the world do we deal with the possible
6686 overflow here? */
6687 offset_expr.X_add_number += 4;
6688 macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
6689 treg + 1, (int) BFD_RELOC_LO16, AT);
6690 }
6691 }
6692
6693 /* To avoid confusion in tc_gen_reloc, we must ensure that this
6694 does not become a variant frag. */
6695 frag_wane (frag_now);
6696 frag_new (0);
6697
6698 break;
6699
6700 case M_LI_DD:
ca4e0257
RS
6701 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
6702 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
6703 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
6704 the value and the low order 32 bits are either zero or in
6705 OFFSET_EXPR. */
252b5132
RH
6706 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6707 {
ca4e0257
RS
6708 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_FPRS);
6709 if (HAVE_64BIT_FPRS)
6710 {
6711 assert (HAVE_64BIT_GPRS);
6712 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6713 "dmtc1", "t,S", AT, treg);
6714 }
252b5132
RH
6715 else
6716 {
6717 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6718 "mtc1", "t,G", AT, treg + 1);
6719 if (offset_expr.X_op == O_absent)
6720 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6721 "mtc1", "t,G", 0, treg);
6722 else
6723 {
6724 assert (offset_expr.X_op == O_constant);
6725 load_register (&icnt, AT, &offset_expr, 0);
6726 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6727 "mtc1", "t,G", AT, treg);
6728 }
6729 }
6730 break;
6731 }
6732
6733 assert (offset_expr.X_op == O_symbol
6734 && offset_expr.X_add_number == 0);
6735 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
6736 if (strcmp (s, ".lit8") == 0)
6737 {
e7af610e 6738 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
6739 {
6740 macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
c9914766
TS
6741 "T,o(b)", treg, (int) BFD_RELOC_MIPS_LITERAL,
6742 mips_gp_register);
252b5132
RH
6743 return;
6744 }
c9914766 6745 breg = mips_gp_register;
252b5132
RH
6746 r = BFD_RELOC_MIPS_LITERAL;
6747 goto dob;
6748 }
6749 else
6750 {
6751 assert (strcmp (s, RDATA_SECTION_NAME) == 0);
6752 if (mips_pic == SVR4_PIC)
6753 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 6754 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
c9914766
TS
6755 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6756 mips_gp_register);
252b5132
RH
6757 else
6758 {
6759 /* FIXME: This won't work for a 64 bit address. */
956cd1d6 6760 macro_build_lui (NULL, &icnt, &offset_expr, AT);
252b5132 6761 }
bdaaa2e1 6762
e7af610e 6763 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
6764 {
6765 macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
6766 "T,o(b)", treg, (int) BFD_RELOC_LO16, AT);
6767
6768 /* To avoid confusion in tc_gen_reloc, we must ensure
6769 that this does not become a variant frag. */
6770 frag_wane (frag_now);
6771 frag_new (0);
6772
6773 break;
6774 }
6775 breg = AT;
6776 r = BFD_RELOC_LO16;
6777 goto dob;
6778 }
6779
6780 case M_L_DOB:
ec68c924 6781 if (mips_arch == CPU_R4650)
252b5132
RH
6782 {
6783 as_bad (_("opcode not supported on this processor"));
6784 return;
6785 }
6786 /* Even on a big endian machine $fn comes before $fn+1. We have
6787 to adjust when loading from memory. */
6788 r = BFD_RELOC_LO16;
6789 dob:
e7af610e 6790 assert (mips_opts.isa == ISA_MIPS1);
252b5132
RH
6791 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6792 target_big_endian ? treg + 1 : treg,
6793 (int) r, breg);
6794 /* FIXME: A possible overflow which I don't know how to deal
6795 with. */
6796 offset_expr.X_add_number += 4;
6797 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6798 target_big_endian ? treg : treg + 1,
6799 (int) r, breg);
6800
6801 /* To avoid confusion in tc_gen_reloc, we must ensure that this
6802 does not become a variant frag. */
6803 frag_wane (frag_now);
6804 frag_new (0);
6805
6806 if (breg != AT)
6807 return;
6808 break;
6809
6810 case M_L_DAB:
6811 /*
6812 * The MIPS assembler seems to check for X_add_number not
6813 * being double aligned and generating:
6814 * lui at,%hi(foo+1)
6815 * addu at,at,v1
6816 * addiu at,at,%lo(foo+1)
6817 * lwc1 f2,0(at)
6818 * lwc1 f3,4(at)
6819 * But, the resulting address is the same after relocation so why
6820 * generate the extra instruction?
6821 */
ec68c924 6822 if (mips_arch == CPU_R4650)
252b5132
RH
6823 {
6824 as_bad (_("opcode not supported on this processor"));
6825 return;
6826 }
bdaaa2e1 6827 /* Itbl support may require additional care here. */
252b5132 6828 coproc = 1;
e7af610e 6829 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
6830 {
6831 s = "ldc1";
6832 goto ld;
6833 }
6834
6835 s = "lwc1";
6836 fmt = "T,o(b)";
6837 goto ldd_std;
6838
6839 case M_S_DAB:
ec68c924 6840 if (mips_arch == CPU_R4650)
252b5132
RH
6841 {
6842 as_bad (_("opcode not supported on this processor"));
6843 return;
6844 }
6845
e7af610e 6846 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
6847 {
6848 s = "sdc1";
6849 goto st;
6850 }
6851
6852 s = "swc1";
6853 fmt = "T,o(b)";
bdaaa2e1 6854 /* Itbl support may require additional care here. */
252b5132
RH
6855 coproc = 1;
6856 goto ldd_std;
6857
6858 case M_LD_AB:
ca4e0257 6859 if (HAVE_64BIT_GPRS)
252b5132
RH
6860 {
6861 s = "ld";
6862 goto ld;
6863 }
6864
6865 s = "lw";
6866 fmt = "t,o(b)";
6867 goto ldd_std;
6868
6869 case M_SD_AB:
ca4e0257 6870 if (HAVE_64BIT_GPRS)
252b5132
RH
6871 {
6872 s = "sd";
6873 goto st;
6874 }
6875
6876 s = "sw";
6877 fmt = "t,o(b)";
6878
6879 ldd_std:
afdbd6d0
CD
6880 /* We do _not_ bother to allow embedded PIC (symbol-local_symbol)
6881 loads for the case of doing a pair of loads to simulate an 'ld'.
6882 This is not currently done by the compiler, and assembly coders
6883 writing embedded-pic code can cope. */
6884
252b5132
RH
6885 if (offset_expr.X_op != O_symbol
6886 && offset_expr.X_op != O_constant)
6887 {
6888 as_bad (_("expression too complex"));
6889 offset_expr.X_op = O_constant;
6890 }
6891
6892 /* Even on a big endian machine $fn comes before $fn+1. We have
6893 to adjust when loading from memory. We set coproc if we must
6894 load $fn+1 first. */
bdaaa2e1 6895 /* Itbl support may require additional care here. */
252b5132
RH
6896 if (! target_big_endian)
6897 coproc = 0;
6898
6899 if (mips_pic == NO_PIC
6900 || offset_expr.X_op == O_constant)
6901 {
f9419b05
TS
6902 char *p;
6903
252b5132 6904 /* If this is a reference to a GP relative symbol, we want
cdf6fd85
TS
6905 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6906 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
6907 If we have a base register, we use this
6908 addu $at,$breg,$gp
cdf6fd85
TS
6909 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6910 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
6911 If this is not a GP relative symbol, we want
6912 lui $at,<sym> (BFD_RELOC_HI16_S)
6913 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6914 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6915 If there is a base register, we add it to $at after the
6916 lui instruction. If there is a constant, we always use
6917 the last case. */
e7d556df 6918 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
252b5132
RH
6919 || nopic_need_relax (offset_expr.X_add_symbol, 1))
6920 {
6921 p = NULL;
6922 used_at = 1;
6923 }
6924 else
6925 {
6926 int off;
6927
6928 if (breg == 0)
6929 {
6930 frag_grow (28);
c9914766 6931 tempreg = mips_gp_register;
252b5132
RH
6932 off = 0;
6933 used_at = 0;
6934 }
6935 else
6936 {
6937 frag_grow (36);
6938 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
f5040a92
AO
6939 HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
6940 ? "add" : "addu" : "daddu",
c9914766 6941 "d,v,t", AT, breg, mips_gp_register);
252b5132
RH
6942 tempreg = AT;
6943 off = 4;
6944 used_at = 1;
6945 }
6946
beae10d5 6947 /* Itbl support may require additional care here. */
252b5132
RH
6948 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6949 coproc ? treg + 1 : treg,
cdf6fd85 6950 (int) BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6951 offset_expr.X_add_number += 4;
6952
6953 /* Set mips_optimize to 2 to avoid inserting an
6954 undesired nop. */
6955 hold_mips_optimize = mips_optimize;
6956 mips_optimize = 2;
beae10d5 6957 /* Itbl support may require additional care here. */
252b5132
RH
6958 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6959 coproc ? treg : treg + 1,
cdf6fd85 6960 (int) BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6961 mips_optimize = hold_mips_optimize;
6962
6963 p = frag_var (rs_machine_dependent, 12 + off, 0,
6964 RELAX_ENCODE (8 + off, 12 + off, 0, 4 + off, 1,
6965 used_at && mips_opts.noat),
956cd1d6 6966 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
6967
6968 /* We just generated two relocs. When tc_gen_reloc
6969 handles this case, it will skip the first reloc and
6970 handle the second. The second reloc already has an
6971 extra addend of 4, which we added above. We must
6972 subtract it out, and then subtract another 4 to make
6973 the first reloc come out right. The second reloc
6974 will come out right because we are going to add 4 to
6975 offset_expr when we build its instruction below.
6976
6977 If we have a symbol, then we don't want to include
6978 the offset, because it will wind up being included
6979 when we generate the reloc. */
6980
6981 if (offset_expr.X_op == O_constant)
6982 offset_expr.X_add_number -= 8;
6983 else
6984 {
6985 offset_expr.X_add_number = -4;
6986 offset_expr.X_op = O_constant;
6987 }
6988 }
6989 macro_build_lui (p, &icnt, &offset_expr, AT);
6990 if (p != NULL)
6991 p += 4;
6992 if (breg != 0)
6993 {
6994 macro_build (p, &icnt, (expressionS *) NULL,
f5040a92
AO
6995 HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
6996 ? "add" : "addu" : "daddu",
252b5132
RH
6997 "d,v,t", AT, breg, AT);
6998 if (p != NULL)
6999 p += 4;
7000 }
beae10d5 7001 /* Itbl support may require additional care here. */
252b5132
RH
7002 macro_build (p, &icnt, &offset_expr, s, fmt,
7003 coproc ? treg + 1 : treg,
7004 (int) BFD_RELOC_LO16, AT);
7005 if (p != NULL)
7006 p += 4;
7007 /* FIXME: How do we handle overflow here? */
7008 offset_expr.X_add_number += 4;
beae10d5 7009 /* Itbl support may require additional care here. */
252b5132
RH
7010 macro_build (p, &icnt, &offset_expr, s, fmt,
7011 coproc ? treg : treg + 1,
7012 (int) BFD_RELOC_LO16, AT);
bdaaa2e1 7013 }
252b5132
RH
7014 else if (mips_pic == SVR4_PIC && ! mips_big_got)
7015 {
7016 int off;
7017
7018 /* If this is a reference to an external symbol, we want
7019 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7020 nop
7021 <op> $treg,0($at)
7022 <op> $treg+1,4($at)
7023 Otherwise we want
7024 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7025 nop
7026 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
7027 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
7028 If there is a base register we add it to $at before the
7029 lwc1 instructions. If there is a constant we include it
7030 in the lwc1 instructions. */
7031 used_at = 1;
7032 expr1.X_add_number = offset_expr.X_add_number;
7033 offset_expr.X_add_number = 0;
7034 if (expr1.X_add_number < -0x8000
7035 || expr1.X_add_number >= 0x8000 - 4)
7036 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
7037 if (breg == 0)
7038 off = 0;
7039 else
7040 off = 4;
7041 frag_grow (24 + off);
7042 macro_build ((char *) NULL, &icnt, &offset_expr,
c9914766
TS
7043 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", AT,
7044 (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132
RH
7045 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7046 if (breg != 0)
7047 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
f5040a92
AO
7048 HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
7049 ? "add" : "addu" : "daddu",
252b5132 7050 "d,v,t", AT, breg, AT);
beae10d5 7051 /* Itbl support may require additional care here. */
252b5132
RH
7052 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
7053 coproc ? treg + 1 : treg,
7054 (int) BFD_RELOC_LO16, AT);
7055 expr1.X_add_number += 4;
7056
7057 /* Set mips_optimize to 2 to avoid inserting an undesired
7058 nop. */
7059 hold_mips_optimize = mips_optimize;
7060 mips_optimize = 2;
beae10d5 7061 /* Itbl support may require additional care here. */
252b5132
RH
7062 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
7063 coproc ? treg : treg + 1,
7064 (int) BFD_RELOC_LO16, AT);
7065 mips_optimize = hold_mips_optimize;
7066
7067 (void) frag_var (rs_machine_dependent, 0, 0,
7068 RELAX_ENCODE (0, 0, -16 - off, -8, 1, 0),
c4e7957c 7069 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
7070 }
7071 else if (mips_pic == SVR4_PIC)
7072 {
7073 int gpdel, off;
f9419b05 7074 char *p;
252b5132
RH
7075
7076 /* If this is a reference to an external symbol, we want
7077 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
7078 addu $at,$at,$gp
7079 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
7080 nop
7081 <op> $treg,0($at)
7082 <op> $treg+1,4($at)
7083 Otherwise we want
7084 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7085 nop
7086 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
7087 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
7088 If there is a base register we add it to $at before the
7089 lwc1 instructions. If there is a constant we include it
7090 in the lwc1 instructions. */
7091 used_at = 1;
7092 expr1.X_add_number = offset_expr.X_add_number;
7093 offset_expr.X_add_number = 0;
7094 if (expr1.X_add_number < -0x8000
7095 || expr1.X_add_number >= 0x8000 - 4)
7096 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
c9914766 7097 if (reg_needs_delay (mips_gp_register))
252b5132
RH
7098 gpdel = 4;
7099 else
7100 gpdel = 0;
7101 if (breg == 0)
7102 off = 0;
7103 else
7104 off = 4;
7105 frag_grow (56);
7106 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
7107 AT, (int) BFD_RELOC_MIPS_GOT_HI16);
7108 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
f5040a92
AO
7109 HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
7110 ? "add" : "addu" : "daddu",
c9914766 7111 "d,v,t", AT, AT, mips_gp_register);
252b5132 7112 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 7113 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
7114 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT_LO16, AT);
7115 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7116 if (breg != 0)
7117 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
f5040a92
AO
7118 HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
7119 ? "add" : "addu" : "daddu",
252b5132 7120 "d,v,t", AT, breg, AT);
beae10d5 7121 /* Itbl support may require additional care here. */
252b5132
RH
7122 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
7123 coproc ? treg + 1 : treg,
7124 (int) BFD_RELOC_LO16, AT);
7125 expr1.X_add_number += 4;
7126
7127 /* Set mips_optimize to 2 to avoid inserting an undesired
7128 nop. */
7129 hold_mips_optimize = mips_optimize;
7130 mips_optimize = 2;
beae10d5 7131 /* Itbl support may require additional care here. */
252b5132
RH
7132 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
7133 coproc ? treg : treg + 1,
7134 (int) BFD_RELOC_LO16, AT);
7135 mips_optimize = hold_mips_optimize;
7136 expr1.X_add_number -= 4;
7137
7138 p = frag_var (rs_machine_dependent, 16 + gpdel + off, 0,
7139 RELAX_ENCODE (24 + off, 16 + gpdel + off, gpdel,
7140 8 + gpdel + off, 1, 0),
c4e7957c 7141 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
7142 if (gpdel > 0)
7143 {
7144 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
7145 p += 4;
7146 }
7147 macro_build (p, &icnt, &offset_expr,
ca4e0257 7148 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
c9914766
TS
7149 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
7150 mips_gp_register);
252b5132
RH
7151 p += 4;
7152 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
7153 p += 4;
7154 if (breg != 0)
7155 {
7156 macro_build (p, &icnt, (expressionS *) NULL,
f5040a92
AO
7157 HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
7158 ? "add" : "addu" : "daddu",
252b5132
RH
7159 "d,v,t", AT, breg, AT);
7160 p += 4;
7161 }
beae10d5 7162 /* Itbl support may require additional care here. */
252b5132
RH
7163 macro_build (p, &icnt, &expr1, s, fmt,
7164 coproc ? treg + 1 : treg,
7165 (int) BFD_RELOC_LO16, AT);
7166 p += 4;
7167 expr1.X_add_number += 4;
7168
7169 /* Set mips_optimize to 2 to avoid inserting an undesired
7170 nop. */
7171 hold_mips_optimize = mips_optimize;
7172 mips_optimize = 2;
beae10d5 7173 /* Itbl support may require additional care here. */
252b5132
RH
7174 macro_build (p, &icnt, &expr1, s, fmt,
7175 coproc ? treg : treg + 1,
7176 (int) BFD_RELOC_LO16, AT);
7177 mips_optimize = hold_mips_optimize;
7178 }
7179 else if (mips_pic == EMBEDDED_PIC)
7180 {
7181 /* If there is no base register, we use
cdf6fd85
TS
7182 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
7183 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
7184 If we have a base register, we use
7185 addu $at,$breg,$gp
cdf6fd85
TS
7186 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
7187 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
7188 */
7189 if (breg == 0)
7190 {
c9914766 7191 tempreg = mips_gp_register;
252b5132
RH
7192 used_at = 0;
7193 }
7194 else
7195 {
7196 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 7197 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 7198 "d,v,t", AT, breg, mips_gp_register);
252b5132
RH
7199 tempreg = AT;
7200 used_at = 1;
7201 }
7202
beae10d5 7203 /* Itbl support may require additional care here. */
252b5132
RH
7204 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
7205 coproc ? treg + 1 : treg,
cdf6fd85 7206 (int) BFD_RELOC_GPREL16, tempreg);
252b5132 7207 offset_expr.X_add_number += 4;
beae10d5 7208 /* Itbl support may require additional care here. */
252b5132
RH
7209 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
7210 coproc ? treg : treg + 1,
cdf6fd85 7211 (int) BFD_RELOC_GPREL16, tempreg);
252b5132
RH
7212 }
7213 else
7214 abort ();
7215
7216 if (! used_at)
7217 return;
7218
7219 break;
7220
7221 case M_LD_OB:
7222 s = "lw";
7223 goto sd_ob;
7224 case M_SD_OB:
7225 s = "sw";
7226 sd_ob:
ca4e0257 7227 assert (HAVE_32BIT_ADDRESSES);
252b5132
RH
7228 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7229 (int) BFD_RELOC_LO16, breg);
7230 offset_expr.X_add_number += 4;
7231 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg + 1,
7232 (int) BFD_RELOC_LO16, breg);
7233 return;
7234
7235 /* New code added to support COPZ instructions.
7236 This code builds table entries out of the macros in mip_opcodes.
7237 R4000 uses interlocks to handle coproc delays.
7238 Other chips (like the R3000) require nops to be inserted for delays.
7239
f72c8c98 7240 FIXME: Currently, we require that the user handle delays.
252b5132
RH
7241 In order to fill delay slots for non-interlocked chips,
7242 we must have a way to specify delays based on the coprocessor.
7243 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
7244 What are the side-effects of the cop instruction?
7245 What cache support might we have and what are its effects?
7246 Both coprocessor & memory require delays. how long???
bdaaa2e1 7247 What registers are read/set/modified?
252b5132
RH
7248
7249 If an itbl is provided to interpret cop instructions,
bdaaa2e1 7250 this knowledge can be encoded in the itbl spec. */
252b5132
RH
7251
7252 case M_COP0:
7253 s = "c0";
7254 goto copz;
7255 case M_COP1:
7256 s = "c1";
7257 goto copz;
7258 case M_COP2:
7259 s = "c2";
7260 goto copz;
7261 case M_COP3:
7262 s = "c3";
7263 copz:
7264 /* For now we just do C (same as Cz). The parameter will be
7265 stored in insn_opcode by mips_ip. */
7266 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "C",
7267 ip->insn_opcode);
7268 return;
7269
ea1fb5dc
RS
7270 case M_MOVE:
7271 move_register (&icnt, dreg, sreg);
7272 return;
7273
252b5132
RH
7274#ifdef LOSING_COMPILER
7275 default:
7276 /* Try and see if this is a new itbl instruction.
7277 This code builds table entries out of the macros in mip_opcodes.
7278 FIXME: For now we just assemble the expression and pass it's
7279 value along as a 32-bit immediate.
bdaaa2e1 7280 We may want to have the assembler assemble this value,
252b5132
RH
7281 so that we gain the assembler's knowledge of delay slots,
7282 symbols, etc.
7283 Would it be more efficient to use mask (id) here? */
bdaaa2e1 7284 if (itbl_have_entries
252b5132 7285 && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
beae10d5 7286 {
252b5132
RH
7287 s = ip->insn_mo->name;
7288 s2 = "cop3";
7289 coproc = ITBL_DECODE_PNUM (immed_expr);;
7290 macro_build ((char *) NULL, &icnt, &immed_expr, s, "C");
7291 return;
beae10d5 7292 }
252b5132
RH
7293 macro2 (ip);
7294 return;
7295 }
7296 if (mips_opts.noat)
7297 as_warn (_("Macro used $at after \".set noat\""));
7298}
bdaaa2e1 7299
252b5132
RH
7300static void
7301macro2 (ip)
7302 struct mips_cl_insn *ip;
7303{
7304 register int treg, sreg, dreg, breg;
7305 int tempreg;
7306 int mask;
7307 int icnt = 0;
7308 int used_at;
7309 expressionS expr1;
7310 const char *s;
7311 const char *s2;
7312 const char *fmt;
7313 int likely = 0;
7314 int dbl = 0;
7315 int coproc = 0;
7316 int lr = 0;
7317 int imm = 0;
7318 int off;
7319 offsetT maxnum;
7320 bfd_reloc_code_real_type r;
7321 char *p;
bdaaa2e1 7322
252b5132
RH
7323 treg = (ip->insn_opcode >> 16) & 0x1f;
7324 dreg = (ip->insn_opcode >> 11) & 0x1f;
7325 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
7326 mask = ip->insn_mo->mask;
bdaaa2e1 7327
252b5132
RH
7328 expr1.X_op = O_constant;
7329 expr1.X_op_symbol = NULL;
7330 expr1.X_add_symbol = NULL;
7331 expr1.X_add_number = 1;
bdaaa2e1 7332
252b5132
RH
7333 switch (mask)
7334 {
7335#endif /* LOSING_COMPILER */
7336
7337 case M_DMUL:
7338 dbl = 1;
7339 case M_MUL:
2396cfb9
TS
7340 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7341 dbl ? "dmultu" : "multu", "s,t", sreg, treg);
7342 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
7343 dreg);
252b5132
RH
7344 return;
7345
7346 case M_DMUL_I:
7347 dbl = 1;
7348 case M_MUL_I:
7349 /* The MIPS assembler some times generates shifts and adds. I'm
7350 not trying to be that fancy. GCC should do this for us
7351 anyway. */
7352 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9 7353 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 7354 dbl ? "dmult" : "mult", "s,t", sreg, AT);
2396cfb9
TS
7355 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
7356 dreg);
252b5132
RH
7357 break;
7358
7359 case M_DMULO_I:
7360 dbl = 1;
7361 case M_MULO_I:
7362 imm = 1;
7363 goto do_mulo;
7364
7365 case M_DMULO:
7366 dbl = 1;
7367 case M_MULO:
7368 do_mulo:
b34976b6 7369 mips_emit_delays (TRUE);
252b5132
RH
7370 ++mips_opts.noreorder;
7371 mips_any_noreorder = 1;
7372 if (imm)
7373 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9 7374 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 7375 dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
2396cfb9
TS
7376 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
7377 dreg);
7378 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
f9419b05 7379 dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, RA);
2396cfb9
TS
7380 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
7381 AT);
252b5132 7382 if (mips_trap)
9bd7d936
MR
7383 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne",
7384 "s,t,q", dreg, AT, 6);
252b5132
RH
7385 else
7386 {
7387 expr1.X_add_number = 8;
2396cfb9
TS
7388 macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", dreg,
7389 AT);
7390 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
7391 0);
7392 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
7393 "c", 6);
252b5132
RH
7394 }
7395 --mips_opts.noreorder;
2396cfb9 7396 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d", dreg);
252b5132
RH
7397 break;
7398
7399 case M_DMULOU_I:
7400 dbl = 1;
7401 case M_MULOU_I:
7402 imm = 1;
7403 goto do_mulou;
7404
7405 case M_DMULOU:
7406 dbl = 1;
7407 case M_MULOU:
7408 do_mulou:
b34976b6 7409 mips_emit_delays (TRUE);
252b5132
RH
7410 ++mips_opts.noreorder;
7411 mips_any_noreorder = 1;
7412 if (imm)
7413 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9 7414 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
252b5132
RH
7415 dbl ? "dmultu" : "multu",
7416 "s,t", sreg, imm ? AT : treg);
2396cfb9
TS
7417 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
7418 AT);
7419 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
7420 dreg);
252b5132 7421 if (mips_trap)
9bd7d936
MR
7422 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne",
7423 "s,t,q", AT, 0, 6);
252b5132
RH
7424 else
7425 {
7426 expr1.X_add_number = 8;
7427 macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", AT, 0);
2396cfb9
TS
7428 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
7429 0);
7430 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
7431 "c", 6);
252b5132
RH
7432 }
7433 --mips_opts.noreorder;
7434 break;
7435
771c7ce4 7436 case M_DROL:
af7ee8bf 7437 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_arch))
82dd0097
CD
7438 {
7439 if (dreg == sreg)
7440 {
7441 tempreg = AT;
7442 used_at = 1;
7443 }
7444 else
7445 {
7446 tempreg = dreg;
7447 used_at = 0;
7448 }
7449 macro_build ((char *) NULL, &icnt, NULL, "dnegu",
7450 "d,w", tempreg, treg);
7451 macro_build ((char *) NULL, &icnt, NULL, "drorv",
7452 "d,t,s", dreg, sreg, tempreg);
7453 if (used_at)
7454 break;
7455 return;
7456 }
771c7ce4
TS
7457 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
7458 "d,v,t", AT, 0, treg);
7459 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
7460 "d,t,s", AT, sreg, AT);
7461 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
7462 "d,t,s", dreg, sreg, treg);
7463 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7464 "d,v,t", dreg, dreg, AT);
7465 break;
7466
252b5132 7467 case M_ROL:
af7ee8bf 7468 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_arch))
82dd0097
CD
7469 {
7470 if (dreg == sreg)
7471 {
7472 tempreg = AT;
7473 used_at = 1;
7474 }
7475 else
7476 {
7477 tempreg = dreg;
7478 used_at = 0;
7479 }
7480 macro_build ((char *) NULL, &icnt, NULL, "negu",
7481 "d,w", tempreg, treg);
7482 macro_build ((char *) NULL, &icnt, NULL, "rorv",
7483 "d,t,s", dreg, sreg, tempreg);
7484 if (used_at)
7485 break;
7486 return;
7487 }
2396cfb9
TS
7488 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
7489 "d,v,t", AT, 0, treg);
7490 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
7491 "d,t,s", AT, sreg, AT);
7492 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
7493 "d,t,s", dreg, sreg, treg);
7494 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7495 "d,v,t", dreg, dreg, AT);
252b5132
RH
7496 break;
7497
771c7ce4
TS
7498 case M_DROL_I:
7499 {
7500 unsigned int rot;
82dd0097 7501 char *l, *r;
771c7ce4
TS
7502
7503 if (imm_expr.X_op != O_constant)
82dd0097 7504 as_bad (_("Improper rotate count"));
771c7ce4 7505 rot = imm_expr.X_add_number & 0x3f;
af7ee8bf 7506 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_arch))
60b63b72
RS
7507 {
7508 rot = (64 - rot) & 0x3f;
7509 if (rot >= 32)
7510 macro_build ((char *) NULL, &icnt, NULL, "dror32",
7511 "d,w,<", dreg, sreg, rot - 32);
7512 else
7513 macro_build ((char *) NULL, &icnt, NULL, "dror",
7514 "d,w,<", dreg, sreg, rot);
82dd0097 7515 return;
60b63b72 7516 }
483fc7cd 7517 if (rot == 0)
483fc7cd 7518 {
82dd0097
CD
7519 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrl",
7520 "d,w,<", dreg, sreg, 0);
7521 return;
483fc7cd 7522 }
82dd0097
CD
7523 l = (rot < 0x20) ? "dsll" : "dsll32";
7524 r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
7525 rot &= 0x1f;
7526 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
7527 "d,w,<", AT, sreg, rot);
7528 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
7529 "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7530 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7531 "d,v,t", dreg, dreg, AT);
771c7ce4
TS
7532 }
7533 break;
7534
252b5132 7535 case M_ROL_I:
771c7ce4
TS
7536 {
7537 unsigned int rot;
7538
7539 if (imm_expr.X_op != O_constant)
82dd0097 7540 as_bad (_("Improper rotate count"));
771c7ce4 7541 rot = imm_expr.X_add_number & 0x1f;
af7ee8bf 7542 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_arch))
60b63b72
RS
7543 {
7544 macro_build ((char *) NULL, &icnt, NULL, "ror",
7545 "d,w,<", dreg, sreg, (32 - rot) & 0x1f);
82dd0097 7546 return;
60b63b72 7547 }
483fc7cd 7548 if (rot == 0)
483fc7cd 7549 {
483fc7cd 7550 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
82dd0097
CD
7551 "d,w,<", dreg, sreg, 0);
7552 return;
483fc7cd 7553 }
82dd0097
CD
7554 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
7555 "d,w,<", AT, sreg, rot);
7556 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
7557 "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7558 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7559 "d,v,t", dreg, dreg, AT);
771c7ce4
TS
7560 }
7561 break;
7562
7563 case M_DROR:
af7ee8bf 7564 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_arch))
82dd0097
CD
7565 {
7566 macro_build ((char *) NULL, &icnt, NULL, "drorv",
7567 "d,t,s", dreg, sreg, treg);
7568 return;
7569 }
771c7ce4
TS
7570 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
7571 "d,v,t", AT, 0, treg);
7572 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
7573 "d,t,s", AT, sreg, AT);
7574 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
7575 "d,t,s", dreg, sreg, treg);
7576 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7577 "d,v,t", dreg, dreg, AT);
252b5132
RH
7578 break;
7579
7580 case M_ROR:
af7ee8bf 7581 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_arch))
82dd0097
CD
7582 {
7583 macro_build ((char *) NULL, &icnt, NULL, "rorv",
7584 "d,t,s", dreg, sreg, treg);
7585 return;
7586 }
2396cfb9
TS
7587 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
7588 "d,v,t", AT, 0, treg);
7589 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
7590 "d,t,s", AT, sreg, AT);
7591 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
7592 "d,t,s", dreg, sreg, treg);
7593 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7594 "d,v,t", dreg, dreg, AT);
252b5132
RH
7595 break;
7596
771c7ce4
TS
7597 case M_DROR_I:
7598 {
7599 unsigned int rot;
82dd0097 7600 char *l, *r;
771c7ce4
TS
7601
7602 if (imm_expr.X_op != O_constant)
82dd0097 7603 as_bad (_("Improper rotate count"));
771c7ce4 7604 rot = imm_expr.X_add_number & 0x3f;
af7ee8bf 7605 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_arch))
82dd0097
CD
7606 {
7607 if (rot >= 32)
7608 macro_build ((char *) NULL, &icnt, NULL, "dror32",
7609 "d,w,<", dreg, sreg, rot - 32);
7610 else
7611 macro_build ((char *) NULL, &icnt, NULL, "dror",
7612 "d,w,<", dreg, sreg, rot);
7613 return;
7614 }
483fc7cd 7615 if (rot == 0)
483fc7cd 7616 {
82dd0097
CD
7617 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrl",
7618 "d,w,<", dreg, sreg, 0);
7619 return;
483fc7cd 7620 }
82dd0097
CD
7621 r = (rot < 0x20) ? "dsrl" : "dsrl32";
7622 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
7623 rot &= 0x1f;
7624 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
7625 "d,w,<", AT, sreg, rot);
7626 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
7627 "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7628 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7629 "d,v,t", dreg, dreg, AT);
771c7ce4
TS
7630 }
7631 break;
7632
252b5132 7633 case M_ROR_I:
771c7ce4
TS
7634 {
7635 unsigned int rot;
7636
7637 if (imm_expr.X_op != O_constant)
82dd0097 7638 as_bad (_("Improper rotate count"));
771c7ce4 7639 rot = imm_expr.X_add_number & 0x1f;
af7ee8bf 7640 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_arch))
82dd0097
CD
7641 {
7642 macro_build ((char *) NULL, &icnt, NULL, "ror",
7643 "d,w,<", dreg, sreg, rot);
7644 return;
7645 }
483fc7cd 7646 if (rot == 0)
483fc7cd
RS
7647 {
7648 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
82dd0097
CD
7649 "d,w,<", dreg, sreg, 0);
7650 return;
483fc7cd 7651 }
82dd0097
CD
7652 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
7653 "d,w,<", AT, sreg, rot);
7654 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
7655 "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7656 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7657 "d,v,t", dreg, dreg, AT);
771c7ce4 7658 }
252b5132
RH
7659 break;
7660
7661 case M_S_DOB:
ec68c924 7662 if (mips_arch == CPU_R4650)
252b5132
RH
7663 {
7664 as_bad (_("opcode not supported on this processor"));
7665 return;
7666 }
e7af610e 7667 assert (mips_opts.isa == ISA_MIPS1);
252b5132
RH
7668 /* Even on a big endian machine $fn comes before $fn+1. We have
7669 to adjust when storing to memory. */
7670 macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
7671 target_big_endian ? treg + 1 : treg,
7672 (int) BFD_RELOC_LO16, breg);
7673 offset_expr.X_add_number += 4;
7674 macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
7675 target_big_endian ? treg : treg + 1,
7676 (int) BFD_RELOC_LO16, breg);
7677 return;
7678
7679 case M_SEQ:
7680 if (sreg == 0)
7681 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7682 treg, (int) BFD_RELOC_LO16);
7683 else if (treg == 0)
7684 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7685 sreg, (int) BFD_RELOC_LO16);
7686 else
7687 {
2396cfb9
TS
7688 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7689 "d,v,t", dreg, sreg, treg);
252b5132
RH
7690 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7691 dreg, (int) BFD_RELOC_LO16);
7692 }
7693 return;
7694
7695 case M_SEQ_I:
7696 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7697 {
7698 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7699 sreg, (int) BFD_RELOC_LO16);
7700 return;
7701 }
7702 if (sreg == 0)
7703 {
7704 as_warn (_("Instruction %s: result is always false"),
7705 ip->insn_mo->name);
ea1fb5dc 7706 move_register (&icnt, dreg, 0);
252b5132
RH
7707 return;
7708 }
7709 if (imm_expr.X_op == O_constant
7710 && imm_expr.X_add_number >= 0
7711 && imm_expr.X_add_number < 0x10000)
7712 {
7713 macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg,
7714 sreg, (int) BFD_RELOC_LO16);
7715 used_at = 0;
7716 }
7717 else if (imm_expr.X_op == O_constant
7718 && imm_expr.X_add_number > -0x8000
7719 && imm_expr.X_add_number < 0)
7720 {
7721 imm_expr.X_add_number = -imm_expr.X_add_number;
7722 macro_build ((char *) NULL, &icnt, &imm_expr,
ca4e0257 7723 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
252b5132
RH
7724 "t,r,j", dreg, sreg,
7725 (int) BFD_RELOC_LO16);
7726 used_at = 0;
7727 }
7728 else
7729 {
4d34fb5f 7730 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
7731 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7732 "d,v,t", dreg, sreg, AT);
252b5132
RH
7733 used_at = 1;
7734 }
7735 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
7736 (int) BFD_RELOC_LO16);
7737 if (used_at)
7738 break;
7739 return;
7740
7741 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
7742 s = "slt";
7743 goto sge;
7744 case M_SGEU:
7745 s = "sltu";
7746 sge:
2396cfb9
TS
7747 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7748 dreg, sreg, treg);
252b5132
RH
7749 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7750 (int) BFD_RELOC_LO16);
7751 return;
7752
7753 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
7754 case M_SGEU_I:
7755 if (imm_expr.X_op == O_constant
7756 && imm_expr.X_add_number >= -0x8000
7757 && imm_expr.X_add_number < 0x8000)
7758 {
7759 macro_build ((char *) NULL, &icnt, &imm_expr,
7760 mask == M_SGE_I ? "slti" : "sltiu",
7761 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7762 used_at = 0;
7763 }
7764 else
7765 {
4d34fb5f 7766 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
7767 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7768 mask == M_SGE_I ? "slt" : "sltu", "d,v,t", dreg, sreg,
7769 AT);
252b5132
RH
7770 used_at = 1;
7771 }
7772 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7773 (int) BFD_RELOC_LO16);
7774 if (used_at)
7775 break;
7776 return;
7777
7778 case M_SGT: /* sreg > treg <==> treg < sreg */
7779 s = "slt";
7780 goto sgt;
7781 case M_SGTU:
7782 s = "sltu";
7783 sgt:
2396cfb9
TS
7784 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7785 dreg, treg, sreg);
252b5132
RH
7786 return;
7787
7788 case M_SGT_I: /* sreg > I <==> I < sreg */
7789 s = "slt";
7790 goto sgti;
7791 case M_SGTU_I:
7792 s = "sltu";
7793 sgti:
4d34fb5f 7794 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
7795 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7796 dreg, AT, sreg);
252b5132
RH
7797 break;
7798
2396cfb9 7799 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
252b5132
RH
7800 s = "slt";
7801 goto sle;
7802 case M_SLEU:
7803 s = "sltu";
7804 sle:
2396cfb9
TS
7805 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7806 dreg, treg, sreg);
252b5132
RH
7807 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7808 (int) BFD_RELOC_LO16);
7809 return;
7810
2396cfb9 7811 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
252b5132
RH
7812 s = "slt";
7813 goto slei;
7814 case M_SLEU_I:
7815 s = "sltu";
7816 slei:
4d34fb5f 7817 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
7818 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7819 dreg, AT, sreg);
252b5132
RH
7820 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7821 (int) BFD_RELOC_LO16);
7822 break;
7823
7824 case M_SLT_I:
7825 if (imm_expr.X_op == O_constant
7826 && imm_expr.X_add_number >= -0x8000
7827 && imm_expr.X_add_number < 0x8000)
7828 {
7829 macro_build ((char *) NULL, &icnt, &imm_expr, "slti", "t,r,j",
7830 dreg, sreg, (int) BFD_RELOC_LO16);
7831 return;
7832 }
4d34fb5f 7833 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
7834 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
7835 dreg, sreg, AT);
252b5132
RH
7836 break;
7837
7838 case M_SLTU_I:
7839 if (imm_expr.X_op == O_constant
7840 && imm_expr.X_add_number >= -0x8000
7841 && imm_expr.X_add_number < 0x8000)
7842 {
7843 macro_build ((char *) NULL, &icnt, &imm_expr, "sltiu", "t,r,j",
7844 dreg, sreg, (int) BFD_RELOC_LO16);
7845 return;
7846 }
4d34fb5f 7847 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
7848 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7849 "d,v,t", dreg, sreg, AT);
252b5132
RH
7850 break;
7851
7852 case M_SNE:
7853 if (sreg == 0)
2396cfb9
TS
7854 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7855 "d,v,t", dreg, 0, treg);
252b5132 7856 else if (treg == 0)
2396cfb9
TS
7857 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7858 "d,v,t", dreg, 0, sreg);
252b5132
RH
7859 else
7860 {
2396cfb9
TS
7861 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7862 "d,v,t", dreg, sreg, treg);
7863 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7864 "d,v,t", dreg, 0, dreg);
252b5132
RH
7865 }
7866 return;
7867
7868 case M_SNE_I:
7869 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7870 {
2396cfb9
TS
7871 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7872 "d,v,t", dreg, 0, sreg);
252b5132
RH
7873 return;
7874 }
7875 if (sreg == 0)
7876 {
7877 as_warn (_("Instruction %s: result is always true"),
7878 ip->insn_mo->name);
7879 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 7880 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
252b5132
RH
7881 "t,r,j", dreg, 0, (int) BFD_RELOC_LO16);
7882 return;
7883 }
7884 if (imm_expr.X_op == O_constant
7885 && imm_expr.X_add_number >= 0
7886 && imm_expr.X_add_number < 0x10000)
7887 {
7888 macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i",
7889 dreg, sreg, (int) BFD_RELOC_LO16);
7890 used_at = 0;
7891 }
7892 else if (imm_expr.X_op == O_constant
7893 && imm_expr.X_add_number > -0x8000
7894 && imm_expr.X_add_number < 0)
7895 {
7896 imm_expr.X_add_number = -imm_expr.X_add_number;
7897 macro_build ((char *) NULL, &icnt, &imm_expr,
ca4e0257 7898 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
252b5132
RH
7899 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7900 used_at = 0;
7901 }
7902 else
7903 {
4d34fb5f 7904 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
7905 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7906 "d,v,t", dreg, sreg, AT);
252b5132
RH
7907 used_at = 1;
7908 }
2396cfb9
TS
7909 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7910 "d,v,t", dreg, 0, dreg);
252b5132
RH
7911 if (used_at)
7912 break;
7913 return;
7914
7915 case M_DSUB_I:
7916 dbl = 1;
7917 case M_SUB_I:
7918 if (imm_expr.X_op == O_constant
7919 && imm_expr.X_add_number > -0x8000
7920 && imm_expr.X_add_number <= 0x8000)
7921 {
7922 imm_expr.X_add_number = -imm_expr.X_add_number;
7923 macro_build ((char *) NULL, &icnt, &imm_expr,
7924 dbl ? "daddi" : "addi",
7925 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7926 return;
7927 }
7928 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9 7929 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 7930 dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
252b5132
RH
7931 break;
7932
7933 case M_DSUBU_I:
7934 dbl = 1;
7935 case M_SUBU_I:
7936 if (imm_expr.X_op == O_constant
7937 && imm_expr.X_add_number > -0x8000
7938 && imm_expr.X_add_number <= 0x8000)
7939 {
7940 imm_expr.X_add_number = -imm_expr.X_add_number;
7941 macro_build ((char *) NULL, &icnt, &imm_expr,
7942 dbl ? "daddiu" : "addiu",
7943 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7944 return;
7945 }
7946 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9 7947 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 7948 dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
252b5132
RH
7949 break;
7950
7951 case M_TEQ_I:
7952 s = "teq";
7953 goto trap;
7954 case M_TGE_I:
7955 s = "tge";
7956 goto trap;
7957 case M_TGEU_I:
7958 s = "tgeu";
7959 goto trap;
7960 case M_TLT_I:
7961 s = "tlt";
7962 goto trap;
7963 case M_TLTU_I:
7964 s = "tltu";
7965 goto trap;
7966 case M_TNE_I:
7967 s = "tne";
7968 trap:
4d34fb5f 7969 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
7970 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "s,t", sreg,
7971 AT);
252b5132
RH
7972 break;
7973
252b5132 7974 case M_TRUNCWS:
43841e91 7975 case M_TRUNCWD:
e7af610e 7976 assert (mips_opts.isa == ISA_MIPS1);
252b5132
RH
7977 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
7978 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
7979
7980 /*
7981 * Is the double cfc1 instruction a bug in the mips assembler;
7982 * or is there a reason for it?
7983 */
b34976b6 7984 mips_emit_delays (TRUE);
252b5132
RH
7985 ++mips_opts.noreorder;
7986 mips_any_noreorder = 1;
2396cfb9 7987 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
f9419b05 7988 treg, RA);
2396cfb9 7989 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
f9419b05 7990 treg, RA);
2396cfb9 7991 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
252b5132
RH
7992 expr1.X_add_number = 3;
7993 macro_build ((char *) NULL, &icnt, &expr1, "ori", "t,r,i", AT, treg,
7994 (int) BFD_RELOC_LO16);
7995 expr1.X_add_number = 2;
7996 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", AT, AT,
7997 (int) BFD_RELOC_LO16);
2396cfb9 7998 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
f9419b05 7999 AT, RA);
2396cfb9
TS
8000 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
8001 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
252b5132 8002 mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S", dreg, sreg);
2396cfb9 8003 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
f9419b05 8004 treg, RA);
2396cfb9 8005 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
252b5132
RH
8006 --mips_opts.noreorder;
8007 break;
8008
8009 case M_ULH:
8010 s = "lb";
8011 goto ulh;
8012 case M_ULHU:
8013 s = "lbu";
8014 ulh:
8015 if (offset_expr.X_add_number >= 0x7fff)
8016 as_bad (_("operand overflow"));
252b5132 8017 if (! target_big_endian)
f9419b05 8018 ++offset_expr.X_add_number;
af22f5b2 8019 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", AT,
252b5132
RH
8020 (int) BFD_RELOC_LO16, breg);
8021 if (! target_big_endian)
f9419b05 8022 --offset_expr.X_add_number;
252b5132 8023 else
f9419b05 8024 ++offset_expr.X_add_number;
af22f5b2 8025 macro_build ((char *) NULL, &icnt, &offset_expr, "lbu", "t,o(b)", treg,
252b5132 8026 (int) BFD_RELOC_LO16, breg);
2396cfb9 8027 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
af22f5b2 8028 AT, AT, 8);
2396cfb9
TS
8029 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
8030 treg, treg, AT);
252b5132
RH
8031 break;
8032
8033 case M_ULD:
8034 s = "ldl";
8035 s2 = "ldr";
8036 off = 7;
8037 goto ulw;
8038 case M_ULW:
8039 s = "lwl";
8040 s2 = "lwr";
8041 off = 3;
8042 ulw:
8043 if (offset_expr.X_add_number >= 0x8000 - off)
8044 as_bad (_("operand overflow"));
af22f5b2
CD
8045 if (treg != breg)
8046 tempreg = treg;
8047 else
8048 tempreg = AT;
252b5132
RH
8049 if (! target_big_endian)
8050 offset_expr.X_add_number += off;
af22f5b2 8051 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", tempreg,
252b5132
RH
8052 (int) BFD_RELOC_LO16, breg);
8053 if (! target_big_endian)
8054 offset_expr.X_add_number -= off;
8055 else
8056 offset_expr.X_add_number += off;
af22f5b2 8057 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", tempreg,
252b5132 8058 (int) BFD_RELOC_LO16, breg);
af22f5b2
CD
8059
8060 /* If necessary, move the result in tempreg the final destination. */
8061 if (treg == tempreg)
8062 return;
8063 /* Protect second load's delay slot. */
8064 if (!gpr_interlocks)
8065 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
8066 move_register (&icnt, treg, tempreg);
8067 break;
252b5132
RH
8068
8069 case M_ULD_A:
8070 s = "ldl";
8071 s2 = "ldr";
8072 off = 7;
8073 goto ulwa;
8074 case M_ULW_A:
8075 s = "lwl";
8076 s2 = "lwr";
8077 off = 3;
8078 ulwa:
d6bc6245 8079 used_at = 1;
c9914766 8080 load_address (&icnt, AT, &offset_expr, &used_at);
252b5132
RH
8081 if (breg != 0)
8082 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
f5040a92
AO
8083 HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
8084 ? "add" : "addu" : "daddu",
252b5132
RH
8085 "d,v,t", AT, AT, breg);
8086 if (! target_big_endian)
8087 expr1.X_add_number = off;
8088 else
8089 expr1.X_add_number = 0;
8090 macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
8091 (int) BFD_RELOC_LO16, AT);
8092 if (! target_big_endian)
8093 expr1.X_add_number = 0;
8094 else
8095 expr1.X_add_number = off;
8096 macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
8097 (int) BFD_RELOC_LO16, AT);
8098 break;
8099
8100 case M_ULH_A:
8101 case M_ULHU_A:
d6bc6245 8102 used_at = 1;
c9914766 8103 load_address (&icnt, AT, &offset_expr, &used_at);
252b5132
RH
8104 if (breg != 0)
8105 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
f5040a92
AO
8106 HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
8107 ? "add" : "addu" : "daddu",
252b5132
RH
8108 "d,v,t", AT, AT, breg);
8109 if (target_big_endian)
8110 expr1.X_add_number = 0;
8111 macro_build ((char *) NULL, &icnt, &expr1,
8112 mask == M_ULH_A ? "lb" : "lbu", "t,o(b)", treg,
8113 (int) BFD_RELOC_LO16, AT);
8114 if (target_big_endian)
8115 expr1.X_add_number = 1;
8116 else
8117 expr1.X_add_number = 0;
8118 macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
8119 (int) BFD_RELOC_LO16, AT);
2396cfb9
TS
8120 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
8121 treg, treg, 8);
8122 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
8123 treg, treg, AT);
252b5132
RH
8124 break;
8125
8126 case M_USH:
8127 if (offset_expr.X_add_number >= 0x7fff)
8128 as_bad (_("operand overflow"));
8129 if (target_big_endian)
f9419b05 8130 ++offset_expr.X_add_number;
252b5132
RH
8131 macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", treg,
8132 (int) BFD_RELOC_LO16, breg);
2396cfb9
TS
8133 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
8134 AT, treg, 8);
252b5132 8135 if (target_big_endian)
f9419b05 8136 --offset_expr.X_add_number;
252b5132 8137 else
f9419b05 8138 ++offset_expr.X_add_number;
252b5132
RH
8139 macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", AT,
8140 (int) BFD_RELOC_LO16, breg);
8141 break;
8142
8143 case M_USD:
8144 s = "sdl";
8145 s2 = "sdr";
8146 off = 7;
8147 goto usw;
8148 case M_USW:
8149 s = "swl";
8150 s2 = "swr";
8151 off = 3;
8152 usw:
8153 if (offset_expr.X_add_number >= 0x8000 - off)
8154 as_bad (_("operand overflow"));
8155 if (! target_big_endian)
8156 offset_expr.X_add_number += off;
8157 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
8158 (int) BFD_RELOC_LO16, breg);
8159 if (! target_big_endian)
8160 offset_expr.X_add_number -= off;
8161 else
8162 offset_expr.X_add_number += off;
8163 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
8164 (int) BFD_RELOC_LO16, breg);
8165 return;
8166
8167 case M_USD_A:
8168 s = "sdl";
8169 s2 = "sdr";
8170 off = 7;
8171 goto uswa;
8172 case M_USW_A:
8173 s = "swl";
8174 s2 = "swr";
8175 off = 3;
8176 uswa:
d6bc6245 8177 used_at = 1;
c9914766 8178 load_address (&icnt, AT, &offset_expr, &used_at);
252b5132
RH
8179 if (breg != 0)
8180 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
f5040a92
AO
8181 HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
8182 ? "add" : "addu" : "daddu",
252b5132
RH
8183 "d,v,t", AT, AT, breg);
8184 if (! target_big_endian)
8185 expr1.X_add_number = off;
8186 else
8187 expr1.X_add_number = 0;
8188 macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
8189 (int) BFD_RELOC_LO16, AT);
8190 if (! target_big_endian)
8191 expr1.X_add_number = 0;
8192 else
8193 expr1.X_add_number = off;
8194 macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
8195 (int) BFD_RELOC_LO16, AT);
8196 break;
8197
8198 case M_USH_A:
d6bc6245 8199 used_at = 1;
c9914766 8200 load_address (&icnt, AT, &offset_expr, &used_at);
252b5132
RH
8201 if (breg != 0)
8202 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
f5040a92
AO
8203 HAVE_32BIT_ADDRESSES ? HAVE_NEWABI
8204 ? "add" : "addu" : "daddu",
252b5132
RH
8205 "d,v,t", AT, AT, breg);
8206 if (! target_big_endian)
8207 expr1.X_add_number = 0;
8208 macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
8209 (int) BFD_RELOC_LO16, AT);
2396cfb9
TS
8210 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
8211 treg, treg, 8);
252b5132
RH
8212 if (! target_big_endian)
8213 expr1.X_add_number = 1;
8214 else
8215 expr1.X_add_number = 0;
8216 macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
8217 (int) BFD_RELOC_LO16, AT);
8218 if (! target_big_endian)
8219 expr1.X_add_number = 0;
8220 else
8221 expr1.X_add_number = 1;
8222 macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
8223 (int) BFD_RELOC_LO16, AT);
2396cfb9
TS
8224 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
8225 treg, treg, 8);
8226 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
8227 treg, treg, AT);
252b5132
RH
8228 break;
8229
8230 default:
8231 /* FIXME: Check if this is one of the itbl macros, since they
bdaaa2e1 8232 are added dynamically. */
252b5132
RH
8233 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
8234 break;
8235 }
8236 if (mips_opts.noat)
8237 as_warn (_("Macro used $at after \".set noat\""));
8238}
8239
8240/* Implement macros in mips16 mode. */
8241
8242static void
8243mips16_macro (ip)
8244 struct mips_cl_insn *ip;
8245{
8246 int mask;
8247 int xreg, yreg, zreg, tmp;
8248 int icnt;
8249 expressionS expr1;
8250 int dbl;
8251 const char *s, *s2, *s3;
8252
8253 mask = ip->insn_mo->mask;
8254
8255 xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
8256 yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
8257 zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
8258
8259 icnt = 0;
8260
8261 expr1.X_op = O_constant;
8262 expr1.X_op_symbol = NULL;
8263 expr1.X_add_symbol = NULL;
8264 expr1.X_add_number = 1;
8265
8266 dbl = 0;
8267
8268 switch (mask)
8269 {
8270 default:
8271 internalError ();
8272
8273 case M_DDIV_3:
8274 dbl = 1;
8275 case M_DIV_3:
8276 s = "mflo";
8277 goto do_div3;
8278 case M_DREM_3:
8279 dbl = 1;
8280 case M_REM_3:
8281 s = "mfhi";
8282 do_div3:
b34976b6 8283 mips_emit_delays (TRUE);
252b5132
RH
8284 ++mips_opts.noreorder;
8285 mips_any_noreorder = 1;
2396cfb9 8286 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
252b5132
RH
8287 dbl ? "ddiv" : "div",
8288 "0,x,y", xreg, yreg);
8289 expr1.X_add_number = 2;
8290 macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
2396cfb9
TS
8291 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break", "6",
8292 7);
bdaaa2e1 8293
252b5132
RH
8294 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
8295 since that causes an overflow. We should do that as well,
8296 but I don't see how to do the comparisons without a temporary
8297 register. */
8298 --mips_opts.noreorder;
2396cfb9 8299 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x", zreg);
252b5132
RH
8300 break;
8301
8302 case M_DIVU_3:
8303 s = "divu";
8304 s2 = "mflo";
8305 goto do_divu3;
8306 case M_REMU_3:
8307 s = "divu";
8308 s2 = "mfhi";
8309 goto do_divu3;
8310 case M_DDIVU_3:
8311 s = "ddivu";
8312 s2 = "mflo";
8313 goto do_divu3;
8314 case M_DREMU_3:
8315 s = "ddivu";
8316 s2 = "mfhi";
8317 do_divu3:
b34976b6 8318 mips_emit_delays (TRUE);
252b5132
RH
8319 ++mips_opts.noreorder;
8320 mips_any_noreorder = 1;
2396cfb9
TS
8321 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "0,x,y",
8322 xreg, yreg);
252b5132
RH
8323 expr1.X_add_number = 2;
8324 macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
98d3f06f
KH
8325 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
8326 "6", 7);
252b5132 8327 --mips_opts.noreorder;
2396cfb9 8328 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "x", zreg);
252b5132
RH
8329 break;
8330
8331 case M_DMUL:
8332 dbl = 1;
8333 case M_MUL:
2396cfb9 8334 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 8335 dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
2396cfb9
TS
8336 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "x",
8337 zreg);
252b5132
RH
8338 return;
8339
8340 case M_DSUBU_I:
8341 dbl = 1;
8342 goto do_subu;
8343 case M_SUBU_I:
8344 do_subu:
8345 if (imm_expr.X_op != O_constant)
8346 as_bad (_("Unsupported large constant"));
8347 imm_expr.X_add_number = -imm_expr.X_add_number;
8348 macro_build ((char *) NULL, &icnt, &imm_expr,
9a41af64 8349 dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
252b5132
RH
8350 break;
8351
8352 case M_SUBU_I_2:
8353 if (imm_expr.X_op != O_constant)
8354 as_bad (_("Unsupported large constant"));
8355 imm_expr.X_add_number = -imm_expr.X_add_number;
8356 macro_build ((char *) NULL, &icnt, &imm_expr, "addiu",
8357 "x,k", xreg);
8358 break;
8359
8360 case M_DSUBU_I_2:
8361 if (imm_expr.X_op != O_constant)
8362 as_bad (_("Unsupported large constant"));
8363 imm_expr.X_add_number = -imm_expr.X_add_number;
8364 macro_build ((char *) NULL, &icnt, &imm_expr, "daddiu",
8365 "y,j", yreg);
8366 break;
8367
8368 case M_BEQ:
8369 s = "cmp";
8370 s2 = "bteqz";
8371 goto do_branch;
8372 case M_BNE:
8373 s = "cmp";
8374 s2 = "btnez";
8375 goto do_branch;
8376 case M_BLT:
8377 s = "slt";
8378 s2 = "btnez";
8379 goto do_branch;
8380 case M_BLTU:
8381 s = "sltu";
8382 s2 = "btnez";
8383 goto do_branch;
8384 case M_BLE:
8385 s = "slt";
8386 s2 = "bteqz";
8387 goto do_reverse_branch;
8388 case M_BLEU:
8389 s = "sltu";
8390 s2 = "bteqz";
8391 goto do_reverse_branch;
8392 case M_BGE:
8393 s = "slt";
8394 s2 = "bteqz";
8395 goto do_branch;
8396 case M_BGEU:
8397 s = "sltu";
8398 s2 = "bteqz";
8399 goto do_branch;
8400 case M_BGT:
8401 s = "slt";
8402 s2 = "btnez";
8403 goto do_reverse_branch;
8404 case M_BGTU:
8405 s = "sltu";
8406 s2 = "btnez";
8407
8408 do_reverse_branch:
8409 tmp = xreg;
8410 xreg = yreg;
8411 yreg = tmp;
8412
8413 do_branch:
8414 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x,y",
8415 xreg, yreg);
8416 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
8417 break;
8418
8419 case M_BEQ_I:
8420 s = "cmpi";
8421 s2 = "bteqz";
8422 s3 = "x,U";
8423 goto do_branch_i;
8424 case M_BNE_I:
8425 s = "cmpi";
8426 s2 = "btnez";
8427 s3 = "x,U";
8428 goto do_branch_i;
8429 case M_BLT_I:
8430 s = "slti";
8431 s2 = "btnez";
8432 s3 = "x,8";
8433 goto do_branch_i;
8434 case M_BLTU_I:
8435 s = "sltiu";
8436 s2 = "btnez";
8437 s3 = "x,8";
8438 goto do_branch_i;
8439 case M_BLE_I:
8440 s = "slti";
8441 s2 = "btnez";
8442 s3 = "x,8";
8443 goto do_addone_branch_i;
8444 case M_BLEU_I:
8445 s = "sltiu";
8446 s2 = "btnez";
8447 s3 = "x,8";
8448 goto do_addone_branch_i;
8449 case M_BGE_I:
8450 s = "slti";
8451 s2 = "bteqz";
8452 s3 = "x,8";
8453 goto do_branch_i;
8454 case M_BGEU_I:
8455 s = "sltiu";
8456 s2 = "bteqz";
8457 s3 = "x,8";
8458 goto do_branch_i;
8459 case M_BGT_I:
8460 s = "slti";
8461 s2 = "bteqz";
8462 s3 = "x,8";
8463 goto do_addone_branch_i;
8464 case M_BGTU_I:
8465 s = "sltiu";
8466 s2 = "bteqz";
8467 s3 = "x,8";
8468
8469 do_addone_branch_i:
8470 if (imm_expr.X_op != O_constant)
8471 as_bad (_("Unsupported large constant"));
8472 ++imm_expr.X_add_number;
8473
8474 do_branch_i:
8475 macro_build ((char *) NULL, &icnt, &imm_expr, s, s3, xreg);
8476 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
8477 break;
8478
8479 case M_ABS:
8480 expr1.X_add_number = 0;
98d3f06f 8481 macro_build ((char *) NULL, &icnt, &expr1, "slti", "x,8", yreg);
252b5132 8482 if (xreg != yreg)
ea1fb5dc 8483 move_register (&icnt, xreg, yreg);
252b5132
RH
8484 expr1.X_add_number = 2;
8485 macro_build ((char *) NULL, &icnt, &expr1, "bteqz", "p");
8486 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
8487 "neg", "x,w", xreg, xreg);
8488 }
8489}
8490
8491/* For consistency checking, verify that all bits are specified either
8492 by the match/mask part of the instruction definition, or by the
8493 operand list. */
8494static int
8495validate_mips_insn (opc)
8496 const struct mips_opcode *opc;
8497{
8498 const char *p = opc->args;
8499 char c;
8500 unsigned long used_bits = opc->mask;
8501
8502 if ((used_bits & opc->match) != opc->match)
8503 {
8504 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
8505 opc->name, opc->args);
8506 return 0;
8507 }
8508#define USE_BITS(mask,shift) (used_bits |= ((mask) << (shift)))
8509 while (*p)
8510 switch (c = *p++)
8511 {
8512 case ',': break;
8513 case '(': break;
8514 case ')': break;
af7ee8bf
CD
8515 case '+':
8516 switch (c = *p++)
8517 {
8518 case 'A': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
8519 case 'B': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
8520 case 'C': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
bbcc0807
CD
8521 case 'D': USE_BITS (OP_MASK_RD, OP_SH_RD);
8522 USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
af7ee8bf
CD
8523 default:
8524 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8525 c, opc->name, opc->args);
8526 return 0;
8527 }
8528 break;
252b5132
RH
8529 case '<': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
8530 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
8531 case 'A': break;
4372b673 8532 case 'B': USE_BITS (OP_MASK_CODE20, OP_SH_CODE20); break;
252b5132
RH
8533 case 'C': USE_BITS (OP_MASK_COPZ, OP_SH_COPZ); break;
8534 case 'D': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
8535 case 'E': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8536 case 'F': break;
8537 case 'G': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
156c2f8b 8538 case 'H': USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
252b5132 8539 case 'I': break;
e972090a 8540 case 'J': USE_BITS (OP_MASK_CODE19, OP_SH_CODE19); break;
af7ee8bf 8541 case 'K': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
252b5132
RH
8542 case 'L': break;
8543 case 'M': USE_BITS (OP_MASK_CCC, OP_SH_CCC); break;
8544 case 'N': USE_BITS (OP_MASK_BCC, OP_SH_BCC); break;
deec1734
CD
8545 case 'O': USE_BITS (OP_MASK_ALN, OP_SH_ALN); break;
8546 case 'Q': USE_BITS (OP_MASK_VSEL, OP_SH_VSEL);
8547 USE_BITS (OP_MASK_FT, OP_SH_FT); break;
252b5132
RH
8548 case 'R': USE_BITS (OP_MASK_FR, OP_SH_FR); break;
8549 case 'S': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
8550 case 'T': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
8551 case 'V': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
8552 case 'W': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
deec1734
CD
8553 case 'X': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
8554 case 'Y': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
8555 case 'Z': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
252b5132
RH
8556 case 'a': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break;
8557 case 'b': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8558 case 'c': USE_BITS (OP_MASK_CODE, OP_SH_CODE); break;
8559 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
8560 case 'f': break;
8561 case 'h': USE_BITS (OP_MASK_PREFX, OP_SH_PREFX); break;
8562 case 'i': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
8563 case 'j': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
8564 case 'k': USE_BITS (OP_MASK_CACHE, OP_SH_CACHE); break;
8565 case 'l': break;
8566 case 'o': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
8567 case 'p': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
8568 case 'q': USE_BITS (OP_MASK_CODE2, OP_SH_CODE2); break;
8569 case 'r': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8570 case 's': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8571 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8572 case 'u': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
8573 case 'v': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8574 case 'w': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8575 case 'x': break;
8576 case 'z': break;
8577 case 'P': USE_BITS (OP_MASK_PERFREG, OP_SH_PERFREG); break;
4372b673
NC
8578 case 'U': USE_BITS (OP_MASK_RD, OP_SH_RD);
8579 USE_BITS (OP_MASK_RT, OP_SH_RT); break;
60b63b72
RS
8580 case 'e': USE_BITS (OP_MASK_VECBYTE, OP_SH_VECBYTE); break;
8581 case '%': USE_BITS (OP_MASK_VECALIGN, OP_SH_VECALIGN); break;
8582 case '[': break;
8583 case ']': break;
252b5132
RH
8584 default:
8585 as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
8586 c, opc->name, opc->args);
8587 return 0;
8588 }
8589#undef USE_BITS
8590 if (used_bits != 0xffffffff)
8591 {
8592 as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
8593 ~used_bits & 0xffffffff, opc->name, opc->args);
8594 return 0;
8595 }
8596 return 1;
8597}
8598
8599/* This routine assembles an instruction into its binary format. As a
8600 side effect, it sets one of the global variables imm_reloc or
8601 offset_reloc to the type of relocation to do if one of the operands
8602 is an address expression. */
8603
8604static void
8605mips_ip (str, ip)
8606 char *str;
8607 struct mips_cl_insn *ip;
8608{
8609 char *s;
8610 const char *args;
43841e91 8611 char c = 0;
252b5132
RH
8612 struct mips_opcode *insn;
8613 char *argsStart;
8614 unsigned int regno;
8615 unsigned int lastregno = 0;
af7ee8bf 8616 unsigned int lastpos = 0;
071742cf 8617 unsigned int limlo, limhi;
252b5132
RH
8618 char *s_reset;
8619 char save_c = 0;
252b5132
RH
8620
8621 insn_error = NULL;
8622
8623 /* If the instruction contains a '.', we first try to match an instruction
8624 including the '.'. Then we try again without the '.'. */
8625 insn = NULL;
3882b010 8626 for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
252b5132
RH
8627 continue;
8628
8629 /* If we stopped on whitespace, then replace the whitespace with null for
8630 the call to hash_find. Save the character we replaced just in case we
8631 have to re-parse the instruction. */
3882b010 8632 if (ISSPACE (*s))
252b5132
RH
8633 {
8634 save_c = *s;
8635 *s++ = '\0';
8636 }
bdaaa2e1 8637
252b5132
RH
8638 insn = (struct mips_opcode *) hash_find (op_hash, str);
8639
8640 /* If we didn't find the instruction in the opcode table, try again, but
8641 this time with just the instruction up to, but not including the
8642 first '.'. */
8643 if (insn == NULL)
8644 {
bdaaa2e1 8645 /* Restore the character we overwrite above (if any). */
252b5132
RH
8646 if (save_c)
8647 *(--s) = save_c;
8648
8649 /* Scan up to the first '.' or whitespace. */
3882b010
L
8650 for (s = str;
8651 *s != '\0' && *s != '.' && !ISSPACE (*s);
8652 ++s)
252b5132
RH
8653 continue;
8654
8655 /* If we did not find a '.', then we can quit now. */
8656 if (*s != '.')
8657 {
8658 insn_error = "unrecognized opcode";
8659 return;
8660 }
8661
8662 /* Lookup the instruction in the hash table. */
8663 *s++ = '\0';
8664 if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
8665 {
8666 insn_error = "unrecognized opcode";
8667 return;
8668 }
252b5132
RH
8669 }
8670
8671 argsStart = s;
8672 for (;;)
8673 {
b34976b6 8674 bfd_boolean ok;
252b5132
RH
8675
8676 assert (strcmp (insn->name, str) == 0);
8677
1f25f5d3
CD
8678 if (OPCODE_IS_MEMBER (insn,
8679 (mips_opts.isa
3396de36 8680 | (file_ase_mips16 ? INSN_MIPS16 : 0)
deec1734 8681 | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
98d3f06f 8682 | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
1f25f5d3 8683 mips_arch))
b34976b6 8684 ok = TRUE;
bdaaa2e1 8685 else
b34976b6 8686 ok = FALSE;
bdaaa2e1 8687
252b5132
RH
8688 if (insn->pinfo != INSN_MACRO)
8689 {
ec68c924 8690 if (mips_arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
b34976b6 8691 ok = FALSE;
252b5132
RH
8692 }
8693
8694 if (! ok)
8695 {
8696 if (insn + 1 < &mips_opcodes[NUMOPCODES]
8697 && strcmp (insn->name, insn[1].name) == 0)
8698 {
8699 ++insn;
8700 continue;
8701 }
252b5132 8702 else
beae10d5 8703 {
268f6bed
L
8704 if (!insn_error)
8705 {
8706 static char buf[100];
316f5878
RS
8707 if (mips_arch_info->is_isa)
8708 sprintf (buf,
8709 _("opcode not supported at this ISA level (%s)"),
8710 mips_cpu_info_from_isa (mips_opts.isa)->name);
8711 else
8712 sprintf (buf,
8713 _("opcode not supported on this processor: %s (%s)"),
8714 mips_arch_info->name,
8715 mips_cpu_info_from_isa (mips_opts.isa)->name);
268f6bed
L
8716 insn_error = buf;
8717 }
8718 if (save_c)
8719 *(--s) = save_c;
2bd7f1f3 8720 return;
252b5132 8721 }
252b5132
RH
8722 }
8723
8724 ip->insn_mo = insn;
8725 ip->insn_opcode = insn->match;
268f6bed 8726 insn_error = NULL;
252b5132
RH
8727 for (args = insn->args;; ++args)
8728 {
deec1734
CD
8729 int is_mdmx;
8730
ad8d3bb3 8731 s += strspn (s, " \t");
deec1734 8732 is_mdmx = 0;
252b5132
RH
8733 switch (*args)
8734 {
8735 case '\0': /* end of args */
8736 if (*s == '\0')
8737 return;
8738 break;
8739
8740 case ',':
8741 if (*s++ == *args)
8742 continue;
8743 s--;
8744 switch (*++args)
8745 {
8746 case 'r':
8747 case 'v':
38487616 8748 ip->insn_opcode |= lastregno << OP_SH_RS;
252b5132
RH
8749 continue;
8750
8751 case 'w':
38487616
TS
8752 ip->insn_opcode |= lastregno << OP_SH_RT;
8753 continue;
8754
252b5132 8755 case 'W':
38487616 8756 ip->insn_opcode |= lastregno << OP_SH_FT;
252b5132
RH
8757 continue;
8758
8759 case 'V':
38487616 8760 ip->insn_opcode |= lastregno << OP_SH_FS;
252b5132
RH
8761 continue;
8762 }
8763 break;
8764
8765 case '(':
8766 /* Handle optional base register.
8767 Either the base register is omitted or
bdaaa2e1 8768 we must have a left paren. */
252b5132
RH
8769 /* This is dependent on the next operand specifier
8770 is a base register specification. */
8771 assert (args[1] == 'b' || args[1] == '5'
8772 || args[1] == '-' || args[1] == '4');
8773 if (*s == '\0')
8774 return;
8775
8776 case ')': /* these must match exactly */
60b63b72
RS
8777 case '[':
8778 case ']':
252b5132
RH
8779 if (*s++ == *args)
8780 continue;
8781 break;
8782
af7ee8bf
CD
8783 case '+': /* Opcode extension character. */
8784 switch (*++args)
8785 {
071742cf
CD
8786 case 'A': /* ins/ext position, becomes LSB. */
8787 limlo = 0;
8788 limhi = 31;
8789 my_getExpression (&imm_expr, s);
8790 check_absolute_expr (ip, &imm_expr);
8791 if ((unsigned long) imm_expr.X_add_number < limlo
8792 || (unsigned long) imm_expr.X_add_number > limhi)
8793 {
8794 as_bad (_("Improper position (%lu)"),
8795 (unsigned long) imm_expr.X_add_number);
8796 imm_expr.X_add_number = limlo;
8797 }
8798 lastpos = imm_expr.X_add_number;
8799 ip->insn_opcode |= (imm_expr.X_add_number
8800 & OP_MASK_SHAMT) << OP_SH_SHAMT;
8801 imm_expr.X_op = O_absent;
8802 s = expr_end;
8803 continue;
8804
8805 case 'B': /* ins size, becomes MSB. */
8806 limlo = 1;
8807 limhi = 32;
8808 my_getExpression (&imm_expr, s);
8809 check_absolute_expr (ip, &imm_expr);
8810 /* Check for negative input so that small negative numbers
8811 will not succeed incorrectly. The checks against
8812 (pos+size) transitively check "size" itself,
8813 assuming that "pos" is reasonable. */
8814 if ((long) imm_expr.X_add_number < 0
8815 || ((unsigned long) imm_expr.X_add_number
8816 + lastpos) < limlo
8817 || ((unsigned long) imm_expr.X_add_number
8818 + lastpos) > limhi)
8819 {
8820 as_bad (_("Improper insert size (%lu, position %lu)"),
8821 (unsigned long) imm_expr.X_add_number,
8822 (unsigned long) lastpos);
8823 imm_expr.X_add_number = limlo - lastpos;
8824 }
8825 ip->insn_opcode |= ((lastpos + imm_expr.X_add_number - 1)
8826 & OP_MASK_INSMSB) << OP_SH_INSMSB;
8827 imm_expr.X_op = O_absent;
8828 s = expr_end;
8829 continue;
8830
8831 case 'C': /* ext size, becomes MSBD. */
8832 limlo = 1;
8833 limhi = 32;
8834 my_getExpression (&imm_expr, s);
8835 check_absolute_expr (ip, &imm_expr);
8836 /* Check for negative input so that small negative numbers
8837 will not succeed incorrectly. The checks against
8838 (pos+size) transitively check "size" itself,
8839 assuming that "pos" is reasonable. */
8840 if ((long) imm_expr.X_add_number < 0
8841 || ((unsigned long) imm_expr.X_add_number
8842 + lastpos) < limlo
8843 || ((unsigned long) imm_expr.X_add_number
8844 + lastpos) > limhi)
8845 {
8846 as_bad (_("Improper extract size (%lu, position %lu)"),
8847 (unsigned long) imm_expr.X_add_number,
8848 (unsigned long) lastpos);
8849 imm_expr.X_add_number = limlo - lastpos;
8850 }
8851 ip->insn_opcode |= ((imm_expr.X_add_number - 1)
8852 & OP_MASK_EXTMSBD) << OP_SH_EXTMSBD;
8853 imm_expr.X_op = O_absent;
8854 s = expr_end;
8855 continue;
af7ee8bf 8856
bbcc0807
CD
8857 case 'D':
8858 /* +D is for disassembly only; never match. */
8859 break;
8860
af7ee8bf
CD
8861 default:
8862 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8863 *args, insn->name, insn->args);
8864 /* Further processing is fruitless. */
8865 return;
8866 }
8867 break;
8868
252b5132
RH
8869 case '<': /* must be at least one digit */
8870 /*
8871 * According to the manual, if the shift amount is greater
b6ff326e
KH
8872 * than 31 or less than 0, then the shift amount should be
8873 * mod 32. In reality the mips assembler issues an error.
252b5132
RH
8874 * We issue a warning and mask out all but the low 5 bits.
8875 */
8876 my_getExpression (&imm_expr, s);
8877 check_absolute_expr (ip, &imm_expr);
8878 if ((unsigned long) imm_expr.X_add_number > 31)
8879 {
793b27f4
TS
8880 as_warn (_("Improper shift amount (%lu)"),
8881 (unsigned long) imm_expr.X_add_number);
38487616 8882 imm_expr.X_add_number &= OP_MASK_SHAMT;
252b5132 8883 }
38487616 8884 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SHAMT;
252b5132
RH
8885 imm_expr.X_op = O_absent;
8886 s = expr_end;
8887 continue;
8888
8889 case '>': /* shift amount minus 32 */
8890 my_getExpression (&imm_expr, s);
8891 check_absolute_expr (ip, &imm_expr);
8892 if ((unsigned long) imm_expr.X_add_number < 32
8893 || (unsigned long) imm_expr.X_add_number > 63)
8894 break;
38487616 8895 ip->insn_opcode |= (imm_expr.X_add_number - 32) << OP_SH_SHAMT;
252b5132
RH
8896 imm_expr.X_op = O_absent;
8897 s = expr_end;
8898 continue;
8899
252b5132
RH
8900 case 'k': /* cache code */
8901 case 'h': /* prefx code */
8902 my_getExpression (&imm_expr, s);
8903 check_absolute_expr (ip, &imm_expr);
8904 if ((unsigned long) imm_expr.X_add_number > 31)
8905 {
8906 as_warn (_("Invalid value for `%s' (%lu)"),
8907 ip->insn_mo->name,
8908 (unsigned long) imm_expr.X_add_number);
8909 imm_expr.X_add_number &= 0x1f;
8910 }
8911 if (*args == 'k')
8912 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
8913 else
8914 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
8915 imm_expr.X_op = O_absent;
8916 s = expr_end;
8917 continue;
8918
8919 case 'c': /* break code */
8920 my_getExpression (&imm_expr, s);
8921 check_absolute_expr (ip, &imm_expr);
793b27f4 8922 if ((unsigned long) imm_expr.X_add_number > 1023)
252b5132 8923 {
793b27f4
TS
8924 as_warn (_("Illegal break code (%lu)"),
8925 (unsigned long) imm_expr.X_add_number);
38487616 8926 imm_expr.X_add_number &= OP_MASK_CODE;
252b5132 8927 }
38487616 8928 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE;
252b5132
RH
8929 imm_expr.X_op = O_absent;
8930 s = expr_end;
8931 continue;
8932
8933 case 'q': /* lower break code */
8934 my_getExpression (&imm_expr, s);
8935 check_absolute_expr (ip, &imm_expr);
793b27f4 8936 if ((unsigned long) imm_expr.X_add_number > 1023)
252b5132 8937 {
793b27f4
TS
8938 as_warn (_("Illegal lower break code (%lu)"),
8939 (unsigned long) imm_expr.X_add_number);
38487616 8940 imm_expr.X_add_number &= OP_MASK_CODE2;
252b5132 8941 }
38487616 8942 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE2;
252b5132
RH
8943 imm_expr.X_op = O_absent;
8944 s = expr_end;
8945 continue;
8946
4372b673 8947 case 'B': /* 20-bit syscall/break code. */
156c2f8b 8948 my_getExpression (&imm_expr, s);
156c2f8b 8949 check_absolute_expr (ip, &imm_expr);
793b27f4
TS
8950 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
8951 as_warn (_("Illegal 20-bit code (%lu)"),
8952 (unsigned long) imm_expr.X_add_number);
38487616 8953 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE20;
252b5132
RH
8954 imm_expr.X_op = O_absent;
8955 s = expr_end;
8956 continue;
8957
98d3f06f 8958 case 'C': /* Coprocessor code */
beae10d5 8959 my_getExpression (&imm_expr, s);
252b5132 8960 check_absolute_expr (ip, &imm_expr);
98d3f06f 8961 if ((unsigned long) imm_expr.X_add_number >= (1 << 25))
252b5132 8962 {
793b27f4
TS
8963 as_warn (_("Coproccesor code > 25 bits (%lu)"),
8964 (unsigned long) imm_expr.X_add_number);
98d3f06f 8965 imm_expr.X_add_number &= ((1 << 25) - 1);
252b5132 8966 }
beae10d5
KH
8967 ip->insn_opcode |= imm_expr.X_add_number;
8968 imm_expr.X_op = O_absent;
8969 s = expr_end;
8970 continue;
252b5132 8971
4372b673
NC
8972 case 'J': /* 19-bit wait code. */
8973 my_getExpression (&imm_expr, s);
8974 check_absolute_expr (ip, &imm_expr);
793b27f4
TS
8975 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
8976 as_warn (_("Illegal 19-bit code (%lu)"),
8977 (unsigned long) imm_expr.X_add_number);
38487616 8978 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE19;
4372b673
NC
8979 imm_expr.X_op = O_absent;
8980 s = expr_end;
8981 continue;
8982
252b5132 8983 case 'P': /* Performance register */
beae10d5 8984 my_getExpression (&imm_expr, s);
252b5132 8985 check_absolute_expr (ip, &imm_expr);
beae10d5 8986 if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
252b5132 8987 {
793b27f4
TS
8988 as_warn (_("Invalid performance register (%lu)"),
8989 (unsigned long) imm_expr.X_add_number);
38487616 8990 imm_expr.X_add_number &= OP_MASK_PERFREG;
252b5132 8991 }
38487616 8992 ip->insn_opcode |= (imm_expr.X_add_number << OP_SH_PERFREG);
beae10d5
KH
8993 imm_expr.X_op = O_absent;
8994 s = expr_end;
8995 continue;
252b5132
RH
8996
8997 case 'b': /* base register */
8998 case 'd': /* destination register */
8999 case 's': /* source register */
9000 case 't': /* target register */
9001 case 'r': /* both target and source */
9002 case 'v': /* both dest and source */
9003 case 'w': /* both dest and target */
9004 case 'E': /* coprocessor target register */
9005 case 'G': /* coprocessor destination register */
af7ee8bf 9006 case 'K': /* 'rdhwr' destination register */
252b5132
RH
9007 case 'x': /* ignore register name */
9008 case 'z': /* must be zero register */
4372b673 9009 case 'U': /* destination register (clo/clz). */
252b5132
RH
9010 s_reset = s;
9011 if (s[0] == '$')
9012 {
9013
3882b010 9014 if (ISDIGIT (s[1]))
252b5132
RH
9015 {
9016 ++s;
9017 regno = 0;
9018 do
9019 {
9020 regno *= 10;
9021 regno += *s - '0';
9022 ++s;
9023 }
3882b010 9024 while (ISDIGIT (*s));
252b5132
RH
9025 if (regno > 31)
9026 as_bad (_("Invalid register number (%d)"), regno);
9027 }
af7ee8bf 9028 else if (*args == 'E' || *args == 'G' || *args == 'K')
252b5132
RH
9029 goto notreg;
9030 else
9031 {
76db943d
TS
9032 if (s[1] == 'r' && s[2] == 'a')
9033 {
9034 s += 3;
9035 regno = RA;
9036 }
9037 else if (s[1] == 'f' && s[2] == 'p')
252b5132
RH
9038 {
9039 s += 3;
9040 regno = FP;
9041 }
9042 else if (s[1] == 's' && s[2] == 'p')
9043 {
9044 s += 3;
9045 regno = SP;
9046 }
9047 else if (s[1] == 'g' && s[2] == 'p')
9048 {
9049 s += 3;
9050 regno = GP;
9051 }
9052 else if (s[1] == 'a' && s[2] == 't')
9053 {
9054 s += 3;
9055 regno = AT;
9056 }
9057 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
9058 {
9059 s += 4;
9060 regno = KT0;
9061 }
9062 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
9063 {
9064 s += 4;
9065 regno = KT1;
9066 }
85b51719
TS
9067 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
9068 {
9069 s += 5;
9070 regno = ZERO;
9071 }
252b5132
RH
9072 else if (itbl_have_entries)
9073 {
9074 char *p, *n;
d7ba4a77 9075 unsigned long r;
252b5132 9076
d7ba4a77 9077 p = s + 1; /* advance past '$' */
252b5132
RH
9078 n = itbl_get_field (&p); /* n is name */
9079
d7ba4a77
ILT
9080 /* See if this is a register defined in an
9081 itbl entry. */
9082 if (itbl_get_reg_val (n, &r))
252b5132
RH
9083 {
9084 /* Get_field advances to the start of
9085 the next field, so we need to back
d7ba4a77 9086 rack to the end of the last field. */
bdaaa2e1 9087 if (p)
252b5132 9088 s = p - 1;
bdaaa2e1 9089 else
d7ba4a77 9090 s = strchr (s, '\0');
252b5132
RH
9091 regno = r;
9092 }
9093 else
9094 goto notreg;
beae10d5 9095 }
252b5132
RH
9096 else
9097 goto notreg;
9098 }
9099 if (regno == AT
9100 && ! mips_opts.noat
9101 && *args != 'E'
af7ee8bf
CD
9102 && *args != 'G'
9103 && *args != 'K')
252b5132
RH
9104 as_warn (_("Used $at without \".set noat\""));
9105 c = *args;
9106 if (*s == ' ')
f9419b05 9107 ++s;
252b5132
RH
9108 if (args[1] != *s)
9109 {
9110 if (c == 'r' || c == 'v' || c == 'w')
9111 {
9112 regno = lastregno;
9113 s = s_reset;
f9419b05 9114 ++args;
252b5132
RH
9115 }
9116 }
9117 /* 'z' only matches $0. */
9118 if (c == 'z' && regno != 0)
9119 break;
9120
bdaaa2e1
KH
9121 /* Now that we have assembled one operand, we use the args string
9122 * to figure out where it goes in the instruction. */
252b5132
RH
9123 switch (c)
9124 {
9125 case 'r':
9126 case 's':
9127 case 'v':
9128 case 'b':
38487616 9129 ip->insn_opcode |= regno << OP_SH_RS;
252b5132
RH
9130 break;
9131 case 'd':
9132 case 'G':
af7ee8bf 9133 case 'K':
38487616 9134 ip->insn_opcode |= regno << OP_SH_RD;
252b5132 9135 break;
4372b673 9136 case 'U':
38487616
TS
9137 ip->insn_opcode |= regno << OP_SH_RD;
9138 ip->insn_opcode |= regno << OP_SH_RT;
4372b673 9139 break;
252b5132
RH
9140 case 'w':
9141 case 't':
9142 case 'E':
38487616 9143 ip->insn_opcode |= regno << OP_SH_RT;
252b5132
RH
9144 break;
9145 case 'x':
9146 /* This case exists because on the r3000 trunc
9147 expands into a macro which requires a gp
9148 register. On the r6000 or r4000 it is
9149 assembled into a single instruction which
9150 ignores the register. Thus the insn version
9151 is MIPS_ISA2 and uses 'x', and the macro
9152 version is MIPS_ISA1 and uses 't'. */
9153 break;
9154 case 'z':
9155 /* This case is for the div instruction, which
9156 acts differently if the destination argument
9157 is $0. This only matches $0, and is checked
9158 outside the switch. */
9159 break;
9160 case 'D':
9161 /* Itbl operand; not yet implemented. FIXME ?? */
9162 break;
9163 /* What about all other operands like 'i', which
9164 can be specified in the opcode table? */
9165 }
9166 lastregno = regno;
9167 continue;
9168 }
9169 notreg:
9170 switch (*args++)
9171 {
9172 case 'r':
9173 case 'v':
38487616 9174 ip->insn_opcode |= lastregno << OP_SH_RS;
252b5132
RH
9175 continue;
9176 case 'w':
38487616 9177 ip->insn_opcode |= lastregno << OP_SH_RT;
252b5132
RH
9178 continue;
9179 }
9180 break;
9181
deec1734
CD
9182 case 'O': /* MDMX alignment immediate constant. */
9183 my_getExpression (&imm_expr, s);
9184 check_absolute_expr (ip, &imm_expr);
9185 if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
9186 {
9187 as_warn ("Improper align amount (%ld), using low bits",
9188 (long) imm_expr.X_add_number);
9189 imm_expr.X_add_number &= OP_MASK_ALN;
9190 }
9191 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_ALN;
9192 imm_expr.X_op = O_absent;
9193 s = expr_end;
9194 continue;
9195
9196 case 'Q': /* MDMX vector, element sel, or const. */
9197 if (s[0] != '$')
9198 {
9199 /* MDMX Immediate. */
9200 my_getExpression (&imm_expr, s);
9201 check_absolute_expr (ip, &imm_expr);
9202 if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
9203 {
9204 as_warn (_("Invalid MDMX Immediate (%ld)"),
9205 (long) imm_expr.X_add_number);
9206 imm_expr.X_add_number &= OP_MASK_FT;
9207 }
9208 imm_expr.X_add_number &= OP_MASK_FT;
9209 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
9210 ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
9211 else
9212 ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
9213 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_FT;
9214 imm_expr.X_op = O_absent;
9215 s = expr_end;
9216 continue;
9217 }
9218 /* Not MDMX Immediate. Fall through. */
9219 case 'X': /* MDMX destination register. */
9220 case 'Y': /* MDMX source register. */
9221 case 'Z': /* MDMX target register. */
9222 is_mdmx = 1;
252b5132
RH
9223 case 'D': /* floating point destination register */
9224 case 'S': /* floating point source register */
9225 case 'T': /* floating point target register */
9226 case 'R': /* floating point source register */
9227 case 'V':
9228 case 'W':
9229 s_reset = s;
deec1734
CD
9230 /* Accept $fN for FP and MDMX register numbers, and in
9231 addition accept $vN for MDMX register numbers. */
9232 if ((s[0] == '$' && s[1] == 'f' && ISDIGIT (s[2]))
9233 || (is_mdmx != 0 && s[0] == '$' && s[1] == 'v'
9234 && ISDIGIT (s[2])))
252b5132
RH
9235 {
9236 s += 2;
9237 regno = 0;
9238 do
9239 {
9240 regno *= 10;
9241 regno += *s - '0';
9242 ++s;
9243 }
3882b010 9244 while (ISDIGIT (*s));
252b5132
RH
9245
9246 if (regno > 31)
9247 as_bad (_("Invalid float register number (%d)"), regno);
9248
9249 if ((regno & 1) != 0
ca4e0257 9250 && HAVE_32BIT_FPRS
252b5132
RH
9251 && ! (strcmp (str, "mtc1") == 0
9252 || strcmp (str, "mfc1") == 0
9253 || strcmp (str, "lwc1") == 0
9254 || strcmp (str, "swc1") == 0
9255 || strcmp (str, "l.s") == 0
9256 || strcmp (str, "s.s") == 0))
9257 as_warn (_("Float register should be even, was %d"),
9258 regno);
9259
9260 c = *args;
9261 if (*s == ' ')
f9419b05 9262 ++s;
252b5132
RH
9263 if (args[1] != *s)
9264 {
9265 if (c == 'V' || c == 'W')
9266 {
9267 regno = lastregno;
9268 s = s_reset;
f9419b05 9269 ++args;
252b5132
RH
9270 }
9271 }
9272 switch (c)
9273 {
9274 case 'D':
deec1734 9275 case 'X':
38487616 9276 ip->insn_opcode |= regno << OP_SH_FD;
252b5132
RH
9277 break;
9278 case 'V':
9279 case 'S':
deec1734 9280 case 'Y':
38487616 9281 ip->insn_opcode |= regno << OP_SH_FS;
252b5132 9282 break;
deec1734
CD
9283 case 'Q':
9284 /* This is like 'Z', but also needs to fix the MDMX
9285 vector/scalar select bits. Note that the
9286 scalar immediate case is handled above. */
9287 if (*s == '[')
9288 {
9289 int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
9290 int max_el = (is_qh ? 3 : 7);
9291 s++;
9292 my_getExpression(&imm_expr, s);
9293 check_absolute_expr (ip, &imm_expr);
9294 s = expr_end;
9295 if (imm_expr.X_add_number > max_el)
9296 as_bad(_("Bad element selector %ld"),
9297 (long) imm_expr.X_add_number);
9298 imm_expr.X_add_number &= max_el;
9299 ip->insn_opcode |= (imm_expr.X_add_number
9300 << (OP_SH_VSEL +
9301 (is_qh ? 2 : 1)));
9302 if (*s != ']')
9303 as_warn(_("Expecting ']' found '%s'"), s);
9304 else
9305 s++;
9306 }
9307 else
9308 {
9309 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
9310 ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
9311 << OP_SH_VSEL);
9312 else
9313 ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
9314 OP_SH_VSEL);
9315 }
9316 /* Fall through */
252b5132
RH
9317 case 'W':
9318 case 'T':
deec1734 9319 case 'Z':
38487616 9320 ip->insn_opcode |= regno << OP_SH_FT;
252b5132
RH
9321 break;
9322 case 'R':
38487616 9323 ip->insn_opcode |= regno << OP_SH_FR;
252b5132
RH
9324 break;
9325 }
9326 lastregno = regno;
9327 continue;
9328 }
9329
252b5132
RH
9330 switch (*args++)
9331 {
9332 case 'V':
38487616 9333 ip->insn_opcode |= lastregno << OP_SH_FS;
252b5132
RH
9334 continue;
9335 case 'W':
38487616 9336 ip->insn_opcode |= lastregno << OP_SH_FT;
252b5132
RH
9337 continue;
9338 }
9339 break;
9340
9341 case 'I':
9342 my_getExpression (&imm_expr, s);
9343 if (imm_expr.X_op != O_big
9344 && imm_expr.X_op != O_constant)
9345 insn_error = _("absolute expression required");
9346 s = expr_end;
9347 continue;
9348
9349 case 'A':
9350 my_getExpression (&offset_expr, s);
f6688943 9351 *imm_reloc = BFD_RELOC_32;
252b5132
RH
9352 s = expr_end;
9353 continue;
9354
9355 case 'F':
9356 case 'L':
9357 case 'f':
9358 case 'l':
9359 {
9360 int f64;
ca4e0257 9361 int using_gprs;
252b5132
RH
9362 char *save_in;
9363 char *err;
9364 unsigned char temp[8];
9365 int len;
9366 unsigned int length;
9367 segT seg;
9368 subsegT subseg;
9369 char *p;
9370
9371 /* These only appear as the last operand in an
9372 instruction, and every instruction that accepts
9373 them in any variant accepts them in all variants.
9374 This means we don't have to worry about backing out
9375 any changes if the instruction does not match.
9376
9377 The difference between them is the size of the
9378 floating point constant and where it goes. For 'F'
9379 and 'L' the constant is 64 bits; for 'f' and 'l' it
9380 is 32 bits. Where the constant is placed is based
9381 on how the MIPS assembler does things:
9382 F -- .rdata
9383 L -- .lit8
9384 f -- immediate value
9385 l -- .lit4
9386
9387 The .lit4 and .lit8 sections are only used if
9388 permitted by the -G argument.
9389
9390 When generating embedded PIC code, we use the
9391 .lit8 section but not the .lit4 section (we can do
9392 .lit4 inline easily; we need to put .lit8
9393 somewhere in the data segment, and using .lit8
9394 permits the linker to eventually combine identical
ca4e0257
RS
9395 .lit8 entries).
9396
9397 The code below needs to know whether the target register
9398 is 32 or 64 bits wide. It relies on the fact 'f' and
9399 'F' are used with GPR-based instructions and 'l' and
9400 'L' are used with FPR-based instructions. */
252b5132
RH
9401
9402 f64 = *args == 'F' || *args == 'L';
ca4e0257 9403 using_gprs = *args == 'F' || *args == 'f';
252b5132
RH
9404
9405 save_in = input_line_pointer;
9406 input_line_pointer = s;
9407 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
9408 length = len;
9409 s = input_line_pointer;
9410 input_line_pointer = save_in;
9411 if (err != NULL && *err != '\0')
9412 {
9413 as_bad (_("Bad floating point constant: %s"), err);
9414 memset (temp, '\0', sizeof temp);
9415 length = f64 ? 8 : 4;
9416 }
9417
156c2f8b 9418 assert (length == (unsigned) (f64 ? 8 : 4));
252b5132
RH
9419
9420 if (*args == 'f'
9421 || (*args == 'l'
9422 && (! USE_GLOBAL_POINTER_OPT
9423 || mips_pic == EMBEDDED_PIC
9424 || g_switch_value < 4
9425 || (temp[0] == 0 && temp[1] == 0)
9426 || (temp[2] == 0 && temp[3] == 0))))
9427 {
9428 imm_expr.X_op = O_constant;
9429 if (! target_big_endian)
9430 imm_expr.X_add_number = bfd_getl32 (temp);
9431 else
9432 imm_expr.X_add_number = bfd_getb32 (temp);
9433 }
9434 else if (length > 4
119d663a 9435 && ! mips_disable_float_construction
ca4e0257
RS
9436 /* Constants can only be constructed in GPRs and
9437 copied to FPRs if the GPRs are at least as wide
9438 as the FPRs. Force the constant into memory if
9439 we are using 64-bit FPRs but the GPRs are only
9440 32 bits wide. */
9441 && (using_gprs
9442 || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
252b5132
RH
9443 && ((temp[0] == 0 && temp[1] == 0)
9444 || (temp[2] == 0 && temp[3] == 0))
9445 && ((temp[4] == 0 && temp[5] == 0)
9446 || (temp[6] == 0 && temp[7] == 0)))
9447 {
ca4e0257
RS
9448 /* The value is simple enough to load with a couple of
9449 instructions. If using 32-bit registers, set
9450 imm_expr to the high order 32 bits and offset_expr to
9451 the low order 32 bits. Otherwise, set imm_expr to
9452 the entire 64 bit constant. */
9453 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
252b5132
RH
9454 {
9455 imm_expr.X_op = O_constant;
9456 offset_expr.X_op = O_constant;
9457 if (! target_big_endian)
9458 {
9459 imm_expr.X_add_number = bfd_getl32 (temp + 4);
9460 offset_expr.X_add_number = bfd_getl32 (temp);
9461 }
9462 else
9463 {
9464 imm_expr.X_add_number = bfd_getb32 (temp);
9465 offset_expr.X_add_number = bfd_getb32 (temp + 4);
9466 }
9467 if (offset_expr.X_add_number == 0)
9468 offset_expr.X_op = O_absent;
9469 }
9470 else if (sizeof (imm_expr.X_add_number) > 4)
9471 {
9472 imm_expr.X_op = O_constant;
9473 if (! target_big_endian)
9474 imm_expr.X_add_number = bfd_getl64 (temp);
9475 else
9476 imm_expr.X_add_number = bfd_getb64 (temp);
9477 }
9478 else
9479 {
9480 imm_expr.X_op = O_big;
9481 imm_expr.X_add_number = 4;
9482 if (! target_big_endian)
9483 {
9484 generic_bignum[0] = bfd_getl16 (temp);
9485 generic_bignum[1] = bfd_getl16 (temp + 2);
9486 generic_bignum[2] = bfd_getl16 (temp + 4);
9487 generic_bignum[3] = bfd_getl16 (temp + 6);
9488 }
9489 else
9490 {
9491 generic_bignum[0] = bfd_getb16 (temp + 6);
9492 generic_bignum[1] = bfd_getb16 (temp + 4);
9493 generic_bignum[2] = bfd_getb16 (temp + 2);
9494 generic_bignum[3] = bfd_getb16 (temp);
9495 }
9496 }
9497 }
9498 else
9499 {
9500 const char *newname;
9501 segT new_seg;
9502
9503 /* Switch to the right section. */
9504 seg = now_seg;
9505 subseg = now_subseg;
9506 switch (*args)
9507 {
9508 default: /* unused default case avoids warnings. */
9509 case 'L':
9510 newname = RDATA_SECTION_NAME;
bb2d6cd7
GK
9511 if ((USE_GLOBAL_POINTER_OPT && g_switch_value >= 8)
9512 || mips_pic == EMBEDDED_PIC)
252b5132
RH
9513 newname = ".lit8";
9514 break;
9515 case 'F':
bb2d6cd7
GK
9516 if (mips_pic == EMBEDDED_PIC)
9517 newname = ".lit8";
9518 else
9519 newname = RDATA_SECTION_NAME;
252b5132
RH
9520 break;
9521 case 'l':
9522 assert (!USE_GLOBAL_POINTER_OPT
9523 || g_switch_value >= 4);
9524 newname = ".lit4";
9525 break;
9526 }
9527 new_seg = subseg_new (newname, (subsegT) 0);
9528 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
9529 bfd_set_section_flags (stdoutput, new_seg,
9530 (SEC_ALLOC
9531 | SEC_LOAD
9532 | SEC_READONLY
9533 | SEC_DATA));
9534 frag_align (*args == 'l' ? 2 : 3, 0, 0);
9535 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
9536 && strcmp (TARGET_OS, "elf") != 0)
9537 record_alignment (new_seg, 4);
9538 else
9539 record_alignment (new_seg, *args == 'l' ? 2 : 3);
9540 if (seg == now_seg)
9541 as_bad (_("Can't use floating point insn in this section"));
9542
9543 /* Set the argument to the current address in the
9544 section. */
9545 offset_expr.X_op = O_symbol;
9546 offset_expr.X_add_symbol =
9547 symbol_new ("L0\001", now_seg,
9548 (valueT) frag_now_fix (), frag_now);
9549 offset_expr.X_add_number = 0;
9550
9551 /* Put the floating point number into the section. */
9552 p = frag_more ((int) length);
9553 memcpy (p, temp, length);
9554
9555 /* Switch back to the original section. */
9556 subseg_set (seg, subseg);
9557 }
9558 }
9559 continue;
9560
9561 case 'i': /* 16 bit unsigned immediate */
9562 case 'j': /* 16 bit signed immediate */
f6688943 9563 *imm_reloc = BFD_RELOC_LO16;
5e0116d5 9564 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0)
252b5132
RH
9565 {
9566 int more;
5e0116d5
RS
9567 offsetT minval, maxval;
9568
9569 more = (insn + 1 < &mips_opcodes[NUMOPCODES]
9570 && strcmp (insn->name, insn[1].name) == 0);
9571
9572 /* If the expression was written as an unsigned number,
9573 only treat it as signed if there are no more
9574 alternatives. */
9575 if (more
9576 && *args == 'j'
9577 && sizeof (imm_expr.X_add_number) <= 4
9578 && imm_expr.X_op == O_constant
9579 && imm_expr.X_add_number < 0
9580 && imm_expr.X_unsigned
9581 && HAVE_64BIT_GPRS)
9582 break;
9583
9584 /* For compatibility with older assemblers, we accept
9585 0x8000-0xffff as signed 16-bit numbers when only
9586 signed numbers are allowed. */
9587 if (*args == 'i')
9588 minval = 0, maxval = 0xffff;
9589 else if (more)
9590 minval = -0x8000, maxval = 0x7fff;
252b5132 9591 else
5e0116d5
RS
9592 minval = -0x8000, maxval = 0xffff;
9593
9594 if (imm_expr.X_op != O_constant
9595 || imm_expr.X_add_number < minval
9596 || imm_expr.X_add_number > maxval)
252b5132
RH
9597 {
9598 if (more)
9599 break;
2ae7e77b
AH
9600 if (imm_expr.X_op == O_constant
9601 || imm_expr.X_op == O_big)
5e0116d5 9602 as_bad (_("expression out of range"));
252b5132
RH
9603 }
9604 }
9605 s = expr_end;
9606 continue;
9607
9608 case 'o': /* 16 bit offset */
5e0116d5
RS
9609 /* Check whether there is only a single bracketed expression
9610 left. If so, it must be the base register and the
9611 constant must be zero. */
9612 if (*s == '(' && strchr (s + 1, '(') == 0)
9613 {
9614 offset_expr.X_op = O_constant;
9615 offset_expr.X_add_number = 0;
9616 continue;
9617 }
252b5132
RH
9618
9619 /* If this value won't fit into a 16 bit offset, then go
9620 find a macro that will generate the 32 bit offset
afdbd6d0 9621 code pattern. */
5e0116d5 9622 if (my_getSmallExpression (&offset_expr, offset_reloc, s) == 0
252b5132
RH
9623 && (offset_expr.X_op != O_constant
9624 || offset_expr.X_add_number >= 0x8000
afdbd6d0 9625 || offset_expr.X_add_number < -0x8000))
252b5132
RH
9626 break;
9627
252b5132
RH
9628 s = expr_end;
9629 continue;
9630
9631 case 'p': /* pc relative offset */
0b25d3e6 9632 *offset_reloc = BFD_RELOC_16_PCREL_S2;
252b5132
RH
9633 my_getExpression (&offset_expr, s);
9634 s = expr_end;
9635 continue;
9636
9637 case 'u': /* upper 16 bits */
5e0116d5
RS
9638 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0
9639 && imm_expr.X_op == O_constant
9640 && (imm_expr.X_add_number < 0
9641 || imm_expr.X_add_number >= 0x10000))
252b5132
RH
9642 as_bad (_("lui expression not in range 0..65535"));
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_MIPS_JMP;
252b5132
RH
9650 continue;
9651
9652 case 'N': /* 3 bit branch condition code */
9653 case 'M': /* 3 bit compare condition code */
9654 if (strncmp (s, "$fcc", 4) != 0)
9655 break;
9656 s += 4;
9657 regno = 0;
9658 do
9659 {
9660 regno *= 10;
9661 regno += *s - '0';
9662 ++s;
9663 }
3882b010 9664 while (ISDIGIT (*s));
252b5132
RH
9665 if (regno > 7)
9666 as_bad (_("invalid condition code register $fcc%d"), regno);
9667 if (*args == 'N')
9668 ip->insn_opcode |= regno << OP_SH_BCC;
9669 else
9670 ip->insn_opcode |= regno << OP_SH_CCC;
beae10d5 9671 continue;
252b5132 9672
156c2f8b
NC
9673 case 'H':
9674 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
9675 s += 2;
3882b010 9676 if (ISDIGIT (*s))
156c2f8b
NC
9677 {
9678 c = 0;
9679 do
9680 {
9681 c *= 10;
9682 c += *s - '0';
9683 ++s;
9684 }
3882b010 9685 while (ISDIGIT (*s));
156c2f8b
NC
9686 }
9687 else
9688 c = 8; /* Invalid sel value. */
9689
9690 if (c > 7)
9691 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
9692 ip->insn_opcode |= c;
9693 continue;
9694
60b63b72
RS
9695 case 'e':
9696 /* Must be at least one digit. */
9697 my_getExpression (&imm_expr, s);
9698 check_absolute_expr (ip, &imm_expr);
9699
9700 if ((unsigned long) imm_expr.X_add_number
9701 > (unsigned long) OP_MASK_VECBYTE)
9702 {
9703 as_bad (_("bad byte vector index (%ld)"),
9704 (long) imm_expr.X_add_number);
9705 imm_expr.X_add_number = 0;
9706 }
9707
9708 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECBYTE;
9709 imm_expr.X_op = O_absent;
9710 s = expr_end;
9711 continue;
9712
9713 case '%':
9714 my_getExpression (&imm_expr, s);
9715 check_absolute_expr (ip, &imm_expr);
9716
9717 if ((unsigned long) imm_expr.X_add_number
9718 > (unsigned long) OP_MASK_VECALIGN)
9719 {
9720 as_bad (_("bad byte vector index (%ld)"),
9721 (long) imm_expr.X_add_number);
9722 imm_expr.X_add_number = 0;
9723 }
9724
9725 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECALIGN;
9726 imm_expr.X_op = O_absent;
9727 s = expr_end;
9728 continue;
9729
252b5132
RH
9730 default:
9731 as_bad (_("bad char = '%c'\n"), *args);
9732 internalError ();
9733 }
9734 break;
9735 }
9736 /* Args don't match. */
9737 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
9738 !strcmp (insn->name, insn[1].name))
9739 {
9740 ++insn;
9741 s = argsStart;
268f6bed 9742 insn_error = _("illegal operands");
252b5132
RH
9743 continue;
9744 }
268f6bed
L
9745 if (save_c)
9746 *(--s) = save_c;
252b5132
RH
9747 insn_error = _("illegal operands");
9748 return;
9749 }
9750}
9751
9752/* This routine assembles an instruction into its binary format when
9753 assembling for the mips16. As a side effect, it sets one of the
9754 global variables imm_reloc or offset_reloc to the type of
9755 relocation to do if one of the operands is an address expression.
9756 It also sets mips16_small and mips16_ext if the user explicitly
9757 requested a small or extended instruction. */
9758
9759static void
9760mips16_ip (str, ip)
9761 char *str;
9762 struct mips_cl_insn *ip;
9763{
9764 char *s;
9765 const char *args;
9766 struct mips_opcode *insn;
9767 char *argsstart;
9768 unsigned int regno;
9769 unsigned int lastregno = 0;
9770 char *s_reset;
9771
9772 insn_error = NULL;
9773
b34976b6
AM
9774 mips16_small = FALSE;
9775 mips16_ext = FALSE;
252b5132 9776
3882b010 9777 for (s = str; ISLOWER (*s); ++s)
252b5132
RH
9778 ;
9779 switch (*s)
9780 {
9781 case '\0':
9782 break;
9783
9784 case ' ':
9785 *s++ = '\0';
9786 break;
9787
9788 case '.':
9789 if (s[1] == 't' && s[2] == ' ')
9790 {
9791 *s = '\0';
b34976b6 9792 mips16_small = TRUE;
252b5132
RH
9793 s += 3;
9794 break;
9795 }
9796 else if (s[1] == 'e' && s[2] == ' ')
9797 {
9798 *s = '\0';
b34976b6 9799 mips16_ext = TRUE;
252b5132
RH
9800 s += 3;
9801 break;
9802 }
9803 /* Fall through. */
9804 default:
9805 insn_error = _("unknown opcode");
9806 return;
9807 }
9808
9809 if (mips_opts.noautoextend && ! mips16_ext)
b34976b6 9810 mips16_small = TRUE;
252b5132
RH
9811
9812 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
9813 {
9814 insn_error = _("unrecognized opcode");
9815 return;
9816 }
9817
9818 argsstart = s;
9819 for (;;)
9820 {
9821 assert (strcmp (insn->name, str) == 0);
9822
9823 ip->insn_mo = insn;
9824 ip->insn_opcode = insn->match;
b34976b6 9825 ip->use_extend = FALSE;
252b5132 9826 imm_expr.X_op = O_absent;
f6688943
TS
9827 imm_reloc[0] = BFD_RELOC_UNUSED;
9828 imm_reloc[1] = BFD_RELOC_UNUSED;
9829 imm_reloc[2] = BFD_RELOC_UNUSED;
252b5132 9830 offset_expr.X_op = O_absent;
f6688943
TS
9831 offset_reloc[0] = BFD_RELOC_UNUSED;
9832 offset_reloc[1] = BFD_RELOC_UNUSED;
9833 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
9834 for (args = insn->args; 1; ++args)
9835 {
9836 int c;
9837
9838 if (*s == ' ')
9839 ++s;
9840
9841 /* In this switch statement we call break if we did not find
9842 a match, continue if we did find a match, or return if we
9843 are done. */
9844
9845 c = *args;
9846 switch (c)
9847 {
9848 case '\0':
9849 if (*s == '\0')
9850 {
9851 /* Stuff the immediate value in now, if we can. */
9852 if (imm_expr.X_op == O_constant
f6688943 9853 && *imm_reloc > BFD_RELOC_UNUSED
252b5132
RH
9854 && insn->pinfo != INSN_MACRO)
9855 {
c4e7957c 9856 mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
b34976b6 9857 imm_expr.X_add_number, TRUE, mips16_small,
252b5132
RH
9858 mips16_ext, &ip->insn_opcode,
9859 &ip->use_extend, &ip->extend);
9860 imm_expr.X_op = O_absent;
f6688943 9861 *imm_reloc = BFD_RELOC_UNUSED;
252b5132
RH
9862 }
9863
9864 return;
9865 }
9866 break;
9867
9868 case ',':
9869 if (*s++ == c)
9870 continue;
9871 s--;
9872 switch (*++args)
9873 {
9874 case 'v':
9875 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9876 continue;
9877 case 'w':
9878 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9879 continue;
9880 }
9881 break;
9882
9883 case '(':
9884 case ')':
9885 if (*s++ == c)
9886 continue;
9887 break;
9888
9889 case 'v':
9890 case 'w':
9891 if (s[0] != '$')
9892 {
9893 if (c == 'v')
9894 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9895 else
9896 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9897 ++args;
9898 continue;
9899 }
9900 /* Fall through. */
9901 case 'x':
9902 case 'y':
9903 case 'z':
9904 case 'Z':
9905 case '0':
9906 case 'S':
9907 case 'R':
9908 case 'X':
9909 case 'Y':
9910 if (s[0] != '$')
9911 break;
9912 s_reset = s;
3882b010 9913 if (ISDIGIT (s[1]))
252b5132
RH
9914 {
9915 ++s;
9916 regno = 0;
9917 do
9918 {
9919 regno *= 10;
9920 regno += *s - '0';
9921 ++s;
9922 }
3882b010 9923 while (ISDIGIT (*s));
252b5132
RH
9924 if (regno > 31)
9925 {
9926 as_bad (_("invalid register number (%d)"), regno);
9927 regno = 2;
9928 }
9929 }
9930 else
9931 {
76db943d
TS
9932 if (s[1] == 'r' && s[2] == 'a')
9933 {
9934 s += 3;
9935 regno = RA;
9936 }
9937 else if (s[1] == 'f' && s[2] == 'p')
252b5132
RH
9938 {
9939 s += 3;
9940 regno = FP;
9941 }
9942 else if (s[1] == 's' && s[2] == 'p')
9943 {
9944 s += 3;
9945 regno = SP;
9946 }
9947 else if (s[1] == 'g' && s[2] == 'p')
9948 {
9949 s += 3;
9950 regno = GP;
9951 }
9952 else if (s[1] == 'a' && s[2] == 't')
9953 {
9954 s += 3;
9955 regno = AT;
9956 }
9957 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
9958 {
9959 s += 4;
9960 regno = KT0;
9961 }
9962 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
9963 {
9964 s += 4;
9965 regno = KT1;
9966 }
85b51719
TS
9967 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
9968 {
9969 s += 5;
9970 regno = ZERO;
9971 }
252b5132
RH
9972 else
9973 break;
9974 }
9975
9976 if (*s == ' ')
9977 ++s;
9978 if (args[1] != *s)
9979 {
9980 if (c == 'v' || c == 'w')
9981 {
9982 regno = mips16_to_32_reg_map[lastregno];
9983 s = s_reset;
f9419b05 9984 ++args;
252b5132
RH
9985 }
9986 }
9987
9988 switch (c)
9989 {
9990 case 'x':
9991 case 'y':
9992 case 'z':
9993 case 'v':
9994 case 'w':
9995 case 'Z':
9996 regno = mips32_to_16_reg_map[regno];
9997 break;
9998
9999 case '0':
10000 if (regno != 0)
10001 regno = ILLEGAL_REG;
10002 break;
10003
10004 case 'S':
10005 if (regno != SP)
10006 regno = ILLEGAL_REG;
10007 break;
10008
10009 case 'R':
10010 if (regno != RA)
10011 regno = ILLEGAL_REG;
10012 break;
10013
10014 case 'X':
10015 case 'Y':
10016 if (regno == AT && ! mips_opts.noat)
10017 as_warn (_("used $at without \".set noat\""));
10018 break;
10019
10020 default:
10021 internalError ();
10022 }
10023
10024 if (regno == ILLEGAL_REG)
10025 break;
10026
10027 switch (c)
10028 {
10029 case 'x':
10030 case 'v':
10031 ip->insn_opcode |= regno << MIPS16OP_SH_RX;
10032 break;
10033 case 'y':
10034 case 'w':
10035 ip->insn_opcode |= regno << MIPS16OP_SH_RY;
10036 break;
10037 case 'z':
10038 ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
10039 break;
10040 case 'Z':
10041 ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
10042 case '0':
10043 case 'S':
10044 case 'R':
10045 break;
10046 case 'X':
10047 ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
10048 break;
10049 case 'Y':
10050 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
10051 ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
10052 break;
10053 default:
10054 internalError ();
10055 }
10056
10057 lastregno = regno;
10058 continue;
10059
10060 case 'P':
10061 if (strncmp (s, "$pc", 3) == 0)
10062 {
10063 s += 3;
10064 continue;
10065 }
10066 break;
10067
10068 case '<':
10069 case '>':
10070 case '[':
10071 case ']':
10072 case '4':
10073 case '5':
10074 case 'H':
10075 case 'W':
10076 case 'D':
10077 case 'j':
10078 case '8':
10079 case 'V':
10080 case 'C':
10081 case 'U':
10082 case 'k':
10083 case 'K':
10084 if (s[0] == '%'
10085 && strncmp (s + 1, "gprel(", sizeof "gprel(" - 1) == 0)
10086 {
10087 /* This is %gprel(SYMBOL). We need to read SYMBOL,
10088 and generate the appropriate reloc. If the text
10089 inside %gprel is not a symbol name with an
10090 optional offset, then we generate a normal reloc
10091 and will probably fail later. */
10092 my_getExpression (&imm_expr, s + sizeof "%gprel" - 1);
10093 if (imm_expr.X_op == O_symbol)
10094 {
b34976b6 10095 mips16_ext = TRUE;
f6688943 10096 *imm_reloc = BFD_RELOC_MIPS16_GPREL;
252b5132 10097 s = expr_end;
b34976b6 10098 ip->use_extend = TRUE;
252b5132
RH
10099 ip->extend = 0;
10100 continue;
10101 }
10102 }
10103 else
10104 {
10105 /* Just pick up a normal expression. */
10106 my_getExpression (&imm_expr, s);
10107 }
10108
10109 if (imm_expr.X_op == O_register)
10110 {
10111 /* What we thought was an expression turned out to
10112 be a register. */
10113
10114 if (s[0] == '(' && args[1] == '(')
10115 {
10116 /* It looks like the expression was omitted
10117 before a register indirection, which means
10118 that the expression is implicitly zero. We
10119 still set up imm_expr, so that we handle
10120 explicit extensions correctly. */
10121 imm_expr.X_op = O_constant;
10122 imm_expr.X_add_number = 0;
f6688943 10123 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
10124 continue;
10125 }
10126
10127 break;
10128 }
10129
10130 /* We need to relax this instruction. */
f6688943 10131 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
10132 s = expr_end;
10133 continue;
10134
10135 case 'p':
10136 case 'q':
10137 case 'A':
10138 case 'B':
10139 case 'E':
10140 /* We use offset_reloc rather than imm_reloc for the PC
10141 relative operands. This lets macros with both
10142 immediate and address operands work correctly. */
10143 my_getExpression (&offset_expr, s);
10144
10145 if (offset_expr.X_op == O_register)
10146 break;
10147
10148 /* We need to relax this instruction. */
f6688943 10149 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
10150 s = expr_end;
10151 continue;
10152
10153 case '6': /* break code */
10154 my_getExpression (&imm_expr, s);
10155 check_absolute_expr (ip, &imm_expr);
10156 if ((unsigned long) imm_expr.X_add_number > 63)
10157 {
10158 as_warn (_("Invalid value for `%s' (%lu)"),
10159 ip->insn_mo->name,
10160 (unsigned long) imm_expr.X_add_number);
10161 imm_expr.X_add_number &= 0x3f;
10162 }
10163 ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
10164 imm_expr.X_op = O_absent;
10165 s = expr_end;
10166 continue;
10167
10168 case 'a': /* 26 bit address */
10169 my_getExpression (&offset_expr, s);
10170 s = expr_end;
f6688943 10171 *offset_reloc = BFD_RELOC_MIPS16_JMP;
252b5132
RH
10172 ip->insn_opcode <<= 16;
10173 continue;
10174
10175 case 'l': /* register list for entry macro */
10176 case 'L': /* register list for exit macro */
10177 {
10178 int mask;
10179
10180 if (c == 'l')
10181 mask = 0;
10182 else
10183 mask = 7 << 3;
10184 while (*s != '\0')
10185 {
10186 int freg, reg1, reg2;
10187
10188 while (*s == ' ' || *s == ',')
10189 ++s;
10190 if (*s != '$')
10191 {
10192 as_bad (_("can't parse register list"));
10193 break;
10194 }
10195 ++s;
10196 if (*s != 'f')
10197 freg = 0;
10198 else
10199 {
10200 freg = 1;
10201 ++s;
10202 }
10203 reg1 = 0;
3882b010 10204 while (ISDIGIT (*s))
252b5132
RH
10205 {
10206 reg1 *= 10;
10207 reg1 += *s - '0';
10208 ++s;
10209 }
10210 if (*s == ' ')
10211 ++s;
10212 if (*s != '-')
10213 reg2 = reg1;
10214 else
10215 {
10216 ++s;
10217 if (*s != '$')
10218 break;
10219 ++s;
10220 if (freg)
10221 {
10222 if (*s == 'f')
10223 ++s;
10224 else
10225 {
10226 as_bad (_("invalid register list"));
10227 break;
10228 }
10229 }
10230 reg2 = 0;
3882b010 10231 while (ISDIGIT (*s))
252b5132
RH
10232 {
10233 reg2 *= 10;
10234 reg2 += *s - '0';
10235 ++s;
10236 }
10237 }
10238 if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
10239 {
10240 mask &= ~ (7 << 3);
10241 mask |= 5 << 3;
10242 }
10243 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
10244 {
10245 mask &= ~ (7 << 3);
10246 mask |= 6 << 3;
10247 }
10248 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
10249 mask |= (reg2 - 3) << 3;
10250 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
10251 mask |= (reg2 - 15) << 1;
f9419b05 10252 else if (reg1 == RA && reg2 == RA)
252b5132
RH
10253 mask |= 1;
10254 else
10255 {
10256 as_bad (_("invalid register list"));
10257 break;
10258 }
10259 }
10260 /* The mask is filled in in the opcode table for the
10261 benefit of the disassembler. We remove it before
10262 applying the actual mask. */
10263 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
10264 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
10265 }
10266 continue;
10267
10268 case 'e': /* extend code */
10269 my_getExpression (&imm_expr, s);
10270 check_absolute_expr (ip, &imm_expr);
10271 if ((unsigned long) imm_expr.X_add_number > 0x7ff)
10272 {
10273 as_warn (_("Invalid value for `%s' (%lu)"),
10274 ip->insn_mo->name,
10275 (unsigned long) imm_expr.X_add_number);
10276 imm_expr.X_add_number &= 0x7ff;
10277 }
10278 ip->insn_opcode |= imm_expr.X_add_number;
10279 imm_expr.X_op = O_absent;
10280 s = expr_end;
10281 continue;
10282
10283 default:
10284 internalError ();
10285 }
10286 break;
10287 }
10288
10289 /* Args don't match. */
10290 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
10291 strcmp (insn->name, insn[1].name) == 0)
10292 {
10293 ++insn;
10294 s = argsstart;
10295 continue;
10296 }
10297
10298 insn_error = _("illegal operands");
10299
10300 return;
10301 }
10302}
10303
10304/* This structure holds information we know about a mips16 immediate
10305 argument type. */
10306
e972090a
NC
10307struct mips16_immed_operand
10308{
252b5132
RH
10309 /* The type code used in the argument string in the opcode table. */
10310 int type;
10311 /* The number of bits in the short form of the opcode. */
10312 int nbits;
10313 /* The number of bits in the extended form of the opcode. */
10314 int extbits;
10315 /* The amount by which the short form is shifted when it is used;
10316 for example, the sw instruction has a shift count of 2. */
10317 int shift;
10318 /* The amount by which the short form is shifted when it is stored
10319 into the instruction code. */
10320 int op_shift;
10321 /* Non-zero if the short form is unsigned. */
10322 int unsp;
10323 /* Non-zero if the extended form is unsigned. */
10324 int extu;
10325 /* Non-zero if the value is PC relative. */
10326 int pcrel;
10327};
10328
10329/* The mips16 immediate operand types. */
10330
10331static const struct mips16_immed_operand mips16_immed_operands[] =
10332{
10333 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
10334 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 },
10335 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
10336 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 },
10337 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
10338 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
10339 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
10340 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
10341 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
10342 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
10343 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
10344 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
10345 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
10346 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
10347 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
10348 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
10349 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
10350 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
10351 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
10352 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
10353 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
10354};
10355
10356#define MIPS16_NUM_IMMED \
10357 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
10358
10359/* Handle a mips16 instruction with an immediate value. This or's the
10360 small immediate value into *INSN. It sets *USE_EXTEND to indicate
10361 whether an extended value is needed; if one is needed, it sets
10362 *EXTEND to the value. The argument type is TYPE. The value is VAL.
10363 If SMALL is true, an unextended opcode was explicitly requested.
10364 If EXT is true, an extended opcode was explicitly requested. If
10365 WARN is true, warn if EXT does not match reality. */
10366
10367static void
10368mips16_immed (file, line, type, val, warn, small, ext, insn, use_extend,
10369 extend)
10370 char *file;
10371 unsigned int line;
10372 int type;
10373 offsetT val;
b34976b6
AM
10374 bfd_boolean warn;
10375 bfd_boolean small;
10376 bfd_boolean ext;
252b5132 10377 unsigned long *insn;
b34976b6 10378 bfd_boolean *use_extend;
252b5132
RH
10379 unsigned short *extend;
10380{
10381 register const struct mips16_immed_operand *op;
10382 int mintiny, maxtiny;
b34976b6 10383 bfd_boolean needext;
252b5132
RH
10384
10385 op = mips16_immed_operands;
10386 while (op->type != type)
10387 {
10388 ++op;
10389 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
10390 }
10391
10392 if (op->unsp)
10393 {
10394 if (type == '<' || type == '>' || type == '[' || type == ']')
10395 {
10396 mintiny = 1;
10397 maxtiny = 1 << op->nbits;
10398 }
10399 else
10400 {
10401 mintiny = 0;
10402 maxtiny = (1 << op->nbits) - 1;
10403 }
10404 }
10405 else
10406 {
10407 mintiny = - (1 << (op->nbits - 1));
10408 maxtiny = (1 << (op->nbits - 1)) - 1;
10409 }
10410
10411 /* Branch offsets have an implicit 0 in the lowest bit. */
10412 if (type == 'p' || type == 'q')
10413 val /= 2;
10414
10415 if ((val & ((1 << op->shift) - 1)) != 0
10416 || val < (mintiny << op->shift)
10417 || val > (maxtiny << op->shift))
b34976b6 10418 needext = TRUE;
252b5132 10419 else
b34976b6 10420 needext = FALSE;
252b5132
RH
10421
10422 if (warn && ext && ! needext)
beae10d5
KH
10423 as_warn_where (file, line,
10424 _("extended operand requested but not required"));
252b5132
RH
10425 if (small && needext)
10426 as_bad_where (file, line, _("invalid unextended operand value"));
10427
10428 if (small || (! ext && ! needext))
10429 {
10430 int insnval;
10431
b34976b6 10432 *use_extend = FALSE;
252b5132
RH
10433 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
10434 insnval <<= op->op_shift;
10435 *insn |= insnval;
10436 }
10437 else
10438 {
10439 long minext, maxext;
10440 int extval;
10441
10442 if (op->extu)
10443 {
10444 minext = 0;
10445 maxext = (1 << op->extbits) - 1;
10446 }
10447 else
10448 {
10449 minext = - (1 << (op->extbits - 1));
10450 maxext = (1 << (op->extbits - 1)) - 1;
10451 }
10452 if (val < minext || val > maxext)
10453 as_bad_where (file, line,
10454 _("operand value out of range for instruction"));
10455
b34976b6 10456 *use_extend = TRUE;
252b5132
RH
10457 if (op->extbits == 16)
10458 {
10459 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
10460 val &= 0x1f;
10461 }
10462 else if (op->extbits == 15)
10463 {
10464 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
10465 val &= 0xf;
10466 }
10467 else
10468 {
10469 extval = ((val & 0x1f) << 6) | (val & 0x20);
10470 val = 0;
10471 }
10472
10473 *extend = (unsigned short) extval;
10474 *insn |= val;
10475 }
10476}
10477\f
5e0116d5 10478static const struct percent_op_match
ad8d3bb3 10479{
5e0116d5
RS
10480 const char *str;
10481 bfd_reloc_code_real_type reloc;
ad8d3bb3
TS
10482} percent_op[] =
10483{
5e0116d5 10484 {"%lo", BFD_RELOC_LO16},
ad8d3bb3 10485#ifdef OBJ_ELF
5e0116d5
RS
10486 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
10487 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
10488 {"%call16", BFD_RELOC_MIPS_CALL16},
10489 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
10490 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
10491 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
10492 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
10493 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
10494 {"%got", BFD_RELOC_MIPS_GOT16},
10495 {"%gp_rel", BFD_RELOC_GPREL16},
10496 {"%half", BFD_RELOC_16},
10497 {"%highest", BFD_RELOC_MIPS_HIGHEST},
10498 {"%higher", BFD_RELOC_MIPS_HIGHER},
10499 {"%neg", BFD_RELOC_MIPS_SUB},
ad8d3bb3 10500#endif
5e0116d5 10501 {"%hi", BFD_RELOC_HI16_S}
ad8d3bb3
TS
10502};
10503
252b5132 10504
5e0116d5
RS
10505/* Return true if *STR points to a relocation operator. When returning true,
10506 move *STR over the operator and store its relocation code in *RELOC.
10507 Leave both *STR and *RELOC alone when returning false. */
10508
10509static bfd_boolean
10510parse_relocation (str, reloc)
ad8d3bb3 10511 char **str;
5e0116d5 10512 bfd_reloc_code_real_type *reloc;
252b5132 10513{
5e0116d5 10514 size_t i;
76b3015f 10515
5e0116d5
RS
10516 for (i = 0; i < ARRAY_SIZE (percent_op); i++)
10517 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
394f9b3a 10518 {
5e0116d5
RS
10519 *str += strlen (percent_op[i].str);
10520 *reloc = percent_op[i].reloc;
394f9b3a 10521
5e0116d5
RS
10522 /* Check whether the output BFD supports this relocation.
10523 If not, issue an error and fall back on something safe. */
10524 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
394f9b3a 10525 {
5e0116d5
RS
10526 as_bad ("relocation %s isn't supported by the current ABI",
10527 percent_op[i].str);
10528 *reloc = BFD_RELOC_LO16;
394f9b3a 10529 }
5e0116d5 10530 return TRUE;
394f9b3a 10531 }
5e0116d5 10532 return FALSE;
394f9b3a 10533}
ad8d3bb3 10534
ad8d3bb3 10535
5e0116d5
RS
10536/* Parse string STR as a 16-bit relocatable operand. Store the
10537 expression in *EP and the relocations in the array starting
10538 at RELOC. Return the number of relocation operators used.
ad8d3bb3 10539
5e0116d5
RS
10540 On exit, EXPR_END points to the first character after the expression.
10541 If no relocation operators are used, RELOC[0] is set to BFD_RELOC_LO16. */
ad8d3bb3 10542
5e0116d5
RS
10543static size_t
10544my_getSmallExpression (ep, reloc, str)
ad8d3bb3 10545 expressionS *ep;
5e0116d5 10546 bfd_reloc_code_real_type *reloc;
ad8d3bb3
TS
10547 char *str;
10548{
5e0116d5
RS
10549 bfd_reloc_code_real_type reversed_reloc[3];
10550 size_t reloc_index, i;
09b8f35a
RS
10551 int crux_depth, str_depth;
10552 char *crux;
5e0116d5
RS
10553
10554 /* Search for the start of the main expression, recoding relocations
09b8f35a
RS
10555 in REVERSED_RELOC. End the loop with CRUX pointing to the start
10556 of the main expression and with CRUX_DEPTH containing the number
10557 of open brackets at that point. */
10558 reloc_index = -1;
10559 str_depth = 0;
10560 do
fb1b3232 10561 {
09b8f35a
RS
10562 reloc_index++;
10563 crux = str;
10564 crux_depth = str_depth;
10565
10566 /* Skip over whitespace and brackets, keeping count of the number
10567 of brackets. */
10568 while (*str == ' ' || *str == '\t' || *str == '(')
10569 if (*str++ == '(')
10570 str_depth++;
5e0116d5 10571 }
09b8f35a
RS
10572 while (*str == '%'
10573 && reloc_index < (HAVE_NEWABI ? 3 : 1)
10574 && parse_relocation (&str, &reversed_reloc[reloc_index]));
ad8d3bb3 10575
09b8f35a 10576 my_getExpression (ep, crux);
5e0116d5 10577 str = expr_end;
394f9b3a 10578
5e0116d5 10579 /* Match every open bracket. */
09b8f35a 10580 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
5e0116d5 10581 if (*str++ == ')')
09b8f35a 10582 crux_depth--;
394f9b3a 10583
09b8f35a 10584 if (crux_depth > 0)
5e0116d5 10585 as_bad ("unclosed '('");
394f9b3a 10586
5e0116d5 10587 expr_end = str;
252b5132 10588
64bdfcaf
RS
10589 if (reloc_index == 0)
10590 reloc[0] = BFD_RELOC_LO16;
10591 else
10592 {
10593 prev_reloc_op_frag = frag_now;
10594 for (i = 0; i < reloc_index; i++)
10595 reloc[i] = reversed_reloc[reloc_index - 1 - i];
10596 }
fb1b3232 10597
5e0116d5 10598 return reloc_index;
252b5132
RH
10599}
10600
10601static void
10602my_getExpression (ep, str)
10603 expressionS *ep;
10604 char *str;
10605{
10606 char *save_in;
98aa84af 10607 valueT val;
252b5132
RH
10608
10609 save_in = input_line_pointer;
10610 input_line_pointer = str;
10611 expression (ep);
10612 expr_end = input_line_pointer;
10613 input_line_pointer = save_in;
10614
10615 /* If we are in mips16 mode, and this is an expression based on `.',
10616 then we bump the value of the symbol by 1 since that is how other
10617 text symbols are handled. We don't bother to handle complex
10618 expressions, just `.' plus or minus a constant. */
10619 if (mips_opts.mips16
10620 && ep->X_op == O_symbol
10621 && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
10622 && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
49309057
ILT
10623 && symbol_get_frag (ep->X_add_symbol) == frag_now
10624 && symbol_constant_p (ep->X_add_symbol)
98aa84af
AM
10625 && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
10626 S_SET_VALUE (ep->X_add_symbol, val + 1);
252b5132
RH
10627}
10628
10629/* Turn a string in input_line_pointer into a floating point constant
bc0d738a
NC
10630 of type TYPE, and store the appropriate bytes in *LITP. The number
10631 of LITTLENUMS emitted is stored in *SIZEP. An error message is
252b5132
RH
10632 returned, or NULL on OK. */
10633
10634char *
10635md_atof (type, litP, sizeP)
10636 int type;
10637 char *litP;
10638 int *sizeP;
10639{
10640 int prec;
10641 LITTLENUM_TYPE words[4];
10642 char *t;
10643 int i;
10644
10645 switch (type)
10646 {
10647 case 'f':
10648 prec = 2;
10649 break;
10650
10651 case 'd':
10652 prec = 4;
10653 break;
10654
10655 default:
10656 *sizeP = 0;
10657 return _("bad call to md_atof");
10658 }
10659
10660 t = atof_ieee (input_line_pointer, type, words);
10661 if (t)
10662 input_line_pointer = t;
10663
10664 *sizeP = prec * 2;
10665
10666 if (! target_big_endian)
10667 {
10668 for (i = prec - 1; i >= 0; i--)
10669 {
10670 md_number_to_chars (litP, (valueT) words[i], 2);
10671 litP += 2;
10672 }
10673 }
10674 else
10675 {
10676 for (i = 0; i < prec; i++)
10677 {
10678 md_number_to_chars (litP, (valueT) words[i], 2);
10679 litP += 2;
10680 }
10681 }
bdaaa2e1 10682
252b5132
RH
10683 return NULL;
10684}
10685
10686void
10687md_number_to_chars (buf, val, n)
10688 char *buf;
10689 valueT val;
10690 int n;
10691{
10692 if (target_big_endian)
10693 number_to_chars_bigendian (buf, val, n);
10694 else
10695 number_to_chars_littleendian (buf, val, n);
10696}
10697\f
ae948b86 10698#ifdef OBJ_ELF
e013f690
TS
10699static int support_64bit_objects(void)
10700{
10701 const char **list, **l;
aa3d8fdf 10702 int yes;
e013f690
TS
10703
10704 list = bfd_target_list ();
10705 for (l = list; *l != NULL; l++)
10706#ifdef TE_TMIPS
10707 /* This is traditional mips */
10708 if (strcmp (*l, "elf64-tradbigmips") == 0
10709 || strcmp (*l, "elf64-tradlittlemips") == 0)
10710#else
10711 if (strcmp (*l, "elf64-bigmips") == 0
10712 || strcmp (*l, "elf64-littlemips") == 0)
10713#endif
10714 break;
aa3d8fdf 10715 yes = (*l != NULL);
e013f690 10716 free (list);
aa3d8fdf 10717 return yes;
e013f690 10718}
ae948b86 10719#endif /* OBJ_ELF */
e013f690 10720
5a38dc70 10721const char *md_shortopts = "nO::g::G:";
252b5132 10722
e972090a
NC
10723struct option md_longopts[] =
10724{
252b5132
RH
10725#define OPTION_MIPS1 (OPTION_MD_BASE + 1)
10726 {"mips0", no_argument, NULL, OPTION_MIPS1},
10727 {"mips1", no_argument, NULL, OPTION_MIPS1},
10728#define OPTION_MIPS2 (OPTION_MD_BASE + 2)
10729 {"mips2", no_argument, NULL, OPTION_MIPS2},
10730#define OPTION_MIPS3 (OPTION_MD_BASE + 3)
10731 {"mips3", no_argument, NULL, OPTION_MIPS3},
10732#define OPTION_MIPS4 (OPTION_MD_BASE + 4)
10733 {"mips4", no_argument, NULL, OPTION_MIPS4},
ae948b86
TS
10734#define OPTION_MIPS5 (OPTION_MD_BASE + 5)
10735 {"mips5", no_argument, NULL, OPTION_MIPS5},
10736#define OPTION_MIPS32 (OPTION_MD_BASE + 6)
10737 {"mips32", no_argument, NULL, OPTION_MIPS32},
10738#define OPTION_MIPS64 (OPTION_MD_BASE + 7)
10739 {"mips64", no_argument, NULL, OPTION_MIPS64},
10740#define OPTION_MEMBEDDED_PIC (OPTION_MD_BASE + 8)
252b5132 10741 {"membedded-pic", no_argument, NULL, OPTION_MEMBEDDED_PIC},
ae948b86 10742#define OPTION_TRAP (OPTION_MD_BASE + 9)
252b5132
RH
10743 {"trap", no_argument, NULL, OPTION_TRAP},
10744 {"no-break", no_argument, NULL, OPTION_TRAP},
ae948b86 10745#define OPTION_BREAK (OPTION_MD_BASE + 10)
252b5132
RH
10746 {"break", no_argument, NULL, OPTION_BREAK},
10747 {"no-trap", no_argument, NULL, OPTION_BREAK},
ae948b86 10748#define OPTION_EB (OPTION_MD_BASE + 11)
252b5132 10749 {"EB", no_argument, NULL, OPTION_EB},
ae948b86 10750#define OPTION_EL (OPTION_MD_BASE + 12)
252b5132 10751 {"EL", no_argument, NULL, OPTION_EL},
ae948b86 10752#define OPTION_MIPS16 (OPTION_MD_BASE + 13)
252b5132 10753 {"mips16", no_argument, NULL, OPTION_MIPS16},
ae948b86 10754#define OPTION_NO_MIPS16 (OPTION_MD_BASE + 14)
252b5132 10755 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
ae948b86 10756#define OPTION_M7000_HILO_FIX (OPTION_MD_BASE + 15)
6b76fefe 10757 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
9ee72ff1
TS
10758#define OPTION_MNO_7000_HILO_FIX (OPTION_MD_BASE + 16)
10759 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10760 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
ae948b86
TS
10761#define OPTION_FP32 (OPTION_MD_BASE + 17)
10762 {"mfp32", no_argument, NULL, OPTION_FP32},
10763#define OPTION_GP32 (OPTION_MD_BASE + 18)
c97ef257 10764 {"mgp32", no_argument, NULL, OPTION_GP32},
ae948b86 10765#define OPTION_CONSTRUCT_FLOATS (OPTION_MD_BASE + 19)
119d663a 10766 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
ae948b86 10767#define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MD_BASE + 20)
119d663a 10768 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
ae948b86 10769#define OPTION_MARCH (OPTION_MD_BASE + 21)
ec68c924 10770 {"march", required_argument, NULL, OPTION_MARCH},
ae948b86 10771#define OPTION_MTUNE (OPTION_MD_BASE + 22)
ec68c924 10772 {"mtune", required_argument, NULL, OPTION_MTUNE},
316f5878
RS
10773#define OPTION_FP64 (OPTION_MD_BASE + 23)
10774 {"mfp64", no_argument, NULL, OPTION_FP64},
ae948b86
TS
10775#define OPTION_M4650 (OPTION_MD_BASE + 24)
10776 {"m4650", no_argument, NULL, OPTION_M4650},
10777#define OPTION_NO_M4650 (OPTION_MD_BASE + 25)
10778 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
10779#define OPTION_M4010 (OPTION_MD_BASE + 26)
10780 {"m4010", no_argument, NULL, OPTION_M4010},
10781#define OPTION_NO_M4010 (OPTION_MD_BASE + 27)
10782 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
10783#define OPTION_M4100 (OPTION_MD_BASE + 28)
10784 {"m4100", no_argument, NULL, OPTION_M4100},
10785#define OPTION_NO_M4100 (OPTION_MD_BASE + 29)
10786 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
10787#define OPTION_M3900 (OPTION_MD_BASE + 30)
10788 {"m3900", no_argument, NULL, OPTION_M3900},
10789#define OPTION_NO_M3900 (OPTION_MD_BASE + 31)
10790 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
10791#define OPTION_GP64 (OPTION_MD_BASE + 32)
10792 {"mgp64", no_argument, NULL, OPTION_GP64},
1f25f5d3
CD
10793#define OPTION_MIPS3D (OPTION_MD_BASE + 33)
10794 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
10795#define OPTION_NO_MIPS3D (OPTION_MD_BASE + 34)
10796 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
deec1734
CD
10797#define OPTION_MDMX (OPTION_MD_BASE + 35)
10798 {"mdmx", no_argument, NULL, OPTION_MDMX},
10799#define OPTION_NO_MDMX (OPTION_MD_BASE + 36)
10800 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
60b63b72
RS
10801#define OPTION_FIX_VR4122 (OPTION_MD_BASE + 37)
10802#define OPTION_NO_FIX_VR4122 (OPTION_MD_BASE + 38)
10803 {"mfix-vr4122-bugs", no_argument, NULL, OPTION_FIX_VR4122},
10804 {"no-mfix-vr4122-bugs", no_argument, NULL, OPTION_NO_FIX_VR4122},
4a6a3df4
AO
10805#define OPTION_RELAX_BRANCH (OPTION_MD_BASE + 39)
10806#define OPTION_NO_RELAX_BRANCH (OPTION_MD_BASE + 40)
10807 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
10808 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
af7ee8bf
CD
10809#define OPTION_MIPS32R2 (OPTION_MD_BASE + 41)
10810 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
156c2f8b 10811#ifdef OBJ_ELF
af7ee8bf 10812#define OPTION_ELF_BASE (OPTION_MD_BASE + 42)
156c2f8b 10813#define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
156c2f8b
NC
10814 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
10815 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
ae948b86 10816#define OPTION_NON_SHARED (OPTION_ELF_BASE + 1)
156c2f8b 10817 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
ae948b86 10818#define OPTION_XGOT (OPTION_ELF_BASE + 2)
156c2f8b 10819 {"xgot", no_argument, NULL, OPTION_XGOT},
ae948b86
TS
10820#define OPTION_MABI (OPTION_ELF_BASE + 3)
10821 {"mabi", required_argument, NULL, OPTION_MABI},
10822#define OPTION_32 (OPTION_ELF_BASE + 4)
156c2f8b 10823 {"32", no_argument, NULL, OPTION_32},
ae948b86 10824#define OPTION_N32 (OPTION_ELF_BASE + 5)
e013f690 10825 {"n32", no_argument, NULL, OPTION_N32},
ae948b86 10826#define OPTION_64 (OPTION_ELF_BASE + 6)
156c2f8b 10827 {"64", no_argument, NULL, OPTION_64},
ecb4347a
DJ
10828#define OPTION_MDEBUG (OPTION_ELF_BASE + 7)
10829 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
10830#define OPTION_NO_MDEBUG (OPTION_ELF_BASE + 8)
10831 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
ae948b86 10832#endif /* OBJ_ELF */
252b5132
RH
10833 {NULL, no_argument, NULL, 0}
10834};
156c2f8b 10835size_t md_longopts_size = sizeof (md_longopts);
252b5132 10836
316f5878
RS
10837/* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
10838 NEW_VALUE. Warn if another value was already specified. Note:
10839 we have to defer parsing the -march and -mtune arguments in order
10840 to handle 'from-abi' correctly, since the ABI might be specified
10841 in a later argument. */
10842
10843static void
10844mips_set_option_string (string_ptr, new_value)
10845 const char **string_ptr, *new_value;
10846{
10847 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
10848 as_warn (_("A different %s was already specified, is now %s"),
10849 string_ptr == &mips_arch_string ? "-march" : "-mtune",
10850 new_value);
10851
10852 *string_ptr = new_value;
10853}
10854
252b5132
RH
10855int
10856md_parse_option (c, arg)
10857 int c;
10858 char *arg;
10859{
10860 switch (c)
10861 {
119d663a
NC
10862 case OPTION_CONSTRUCT_FLOATS:
10863 mips_disable_float_construction = 0;
10864 break;
bdaaa2e1 10865
119d663a
NC
10866 case OPTION_NO_CONSTRUCT_FLOATS:
10867 mips_disable_float_construction = 1;
10868 break;
bdaaa2e1 10869
252b5132
RH
10870 case OPTION_TRAP:
10871 mips_trap = 1;
10872 break;
10873
10874 case OPTION_BREAK:
10875 mips_trap = 0;
10876 break;
10877
10878 case OPTION_EB:
10879 target_big_endian = 1;
10880 break;
10881
10882 case OPTION_EL:
10883 target_big_endian = 0;
10884 break;
10885
39c0a331
L
10886 case 'n':
10887 warn_nops = 1;
10888 break;
10889
252b5132
RH
10890 case 'O':
10891 if (arg && arg[1] == '0')
10892 mips_optimize = 1;
10893 else
10894 mips_optimize = 2;
10895 break;
10896
10897 case 'g':
10898 if (arg == NULL)
10899 mips_debug = 2;
10900 else
10901 mips_debug = atoi (arg);
10902 /* When the MIPS assembler sees -g or -g2, it does not do
10903 optimizations which limit full symbolic debugging. We take
10904 that to be equivalent to -O0. */
10905 if (mips_debug == 2)
10906 mips_optimize = 1;
10907 break;
10908
10909 case OPTION_MIPS1:
316f5878 10910 file_mips_isa = ISA_MIPS1;
252b5132
RH
10911 break;
10912
10913 case OPTION_MIPS2:
316f5878 10914 file_mips_isa = ISA_MIPS2;
252b5132
RH
10915 break;
10916
10917 case OPTION_MIPS3:
316f5878 10918 file_mips_isa = ISA_MIPS3;
252b5132
RH
10919 break;
10920
10921 case OPTION_MIPS4:
316f5878 10922 file_mips_isa = ISA_MIPS4;
e7af610e
NC
10923 break;
10924
84ea6cf2 10925 case OPTION_MIPS5:
316f5878 10926 file_mips_isa = ISA_MIPS5;
84ea6cf2
NC
10927 break;
10928
e7af610e 10929 case OPTION_MIPS32:
316f5878 10930 file_mips_isa = ISA_MIPS32;
252b5132
RH
10931 break;
10932
af7ee8bf
CD
10933 case OPTION_MIPS32R2:
10934 file_mips_isa = ISA_MIPS32R2;
10935 break;
10936
84ea6cf2 10937 case OPTION_MIPS64:
316f5878 10938 file_mips_isa = ISA_MIPS64;
84ea6cf2
NC
10939 break;
10940
ec68c924 10941 case OPTION_MTUNE:
316f5878
RS
10942 mips_set_option_string (&mips_tune_string, arg);
10943 break;
ec68c924 10944
316f5878
RS
10945 case OPTION_MARCH:
10946 mips_set_option_string (&mips_arch_string, arg);
252b5132
RH
10947 break;
10948
10949 case OPTION_M4650:
316f5878
RS
10950 mips_set_option_string (&mips_arch_string, "4650");
10951 mips_set_option_string (&mips_tune_string, "4650");
252b5132
RH
10952 break;
10953
10954 case OPTION_NO_M4650:
10955 break;
10956
10957 case OPTION_M4010:
316f5878
RS
10958 mips_set_option_string (&mips_arch_string, "4010");
10959 mips_set_option_string (&mips_tune_string, "4010");
252b5132
RH
10960 break;
10961
10962 case OPTION_NO_M4010:
10963 break;
10964
10965 case OPTION_M4100:
316f5878
RS
10966 mips_set_option_string (&mips_arch_string, "4100");
10967 mips_set_option_string (&mips_tune_string, "4100");
252b5132
RH
10968 break;
10969
10970 case OPTION_NO_M4100:
10971 break;
10972
252b5132 10973 case OPTION_M3900:
316f5878
RS
10974 mips_set_option_string (&mips_arch_string, "3900");
10975 mips_set_option_string (&mips_tune_string, "3900");
252b5132 10976 break;
bdaaa2e1 10977
252b5132
RH
10978 case OPTION_NO_M3900:
10979 break;
10980
deec1734
CD
10981 case OPTION_MDMX:
10982 mips_opts.ase_mdmx = 1;
10983 break;
10984
10985 case OPTION_NO_MDMX:
10986 mips_opts.ase_mdmx = 0;
10987 break;
10988
252b5132
RH
10989 case OPTION_MIPS16:
10990 mips_opts.mips16 = 1;
b34976b6 10991 mips_no_prev_insn (FALSE);
252b5132
RH
10992 break;
10993
10994 case OPTION_NO_MIPS16:
10995 mips_opts.mips16 = 0;
b34976b6 10996 mips_no_prev_insn (FALSE);
252b5132
RH
10997 break;
10998
1f25f5d3
CD
10999 case OPTION_MIPS3D:
11000 mips_opts.ase_mips3d = 1;
11001 break;
11002
11003 case OPTION_NO_MIPS3D:
11004 mips_opts.ase_mips3d = 0;
11005 break;
11006
252b5132
RH
11007 case OPTION_MEMBEDDED_PIC:
11008 mips_pic = EMBEDDED_PIC;
11009 if (USE_GLOBAL_POINTER_OPT && g_switch_seen)
11010 {
11011 as_bad (_("-G may not be used with embedded PIC code"));
11012 return 0;
11013 }
11014 g_switch_value = 0x7fffffff;
11015 break;
11016
60b63b72
RS
11017 case OPTION_FIX_VR4122:
11018 mips_fix_4122_bugs = 1;
11019 break;
11020
11021 case OPTION_NO_FIX_VR4122:
11022 mips_fix_4122_bugs = 0;
11023 break;
11024
4a6a3df4
AO
11025 case OPTION_RELAX_BRANCH:
11026 mips_relax_branch = 1;
11027 break;
11028
11029 case OPTION_NO_RELAX_BRANCH:
11030 mips_relax_branch = 0;
11031 break;
11032
0f074f60 11033#ifdef OBJ_ELF
252b5132
RH
11034 /* When generating ELF code, we permit -KPIC and -call_shared to
11035 select SVR4_PIC, and -non_shared to select no PIC. This is
11036 intended to be compatible with Irix 5. */
11037 case OPTION_CALL_SHARED:
11038 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11039 {
11040 as_bad (_("-call_shared is supported only for ELF format"));
11041 return 0;
11042 }
11043 mips_pic = SVR4_PIC;
143d77c5 11044 mips_abicalls = TRUE;
252b5132
RH
11045 if (g_switch_seen && g_switch_value != 0)
11046 {
11047 as_bad (_("-G may not be used with SVR4 PIC code"));
11048 return 0;
11049 }
11050 g_switch_value = 0;
11051 break;
11052
11053 case OPTION_NON_SHARED:
11054 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11055 {
11056 as_bad (_("-non_shared is supported only for ELF format"));
11057 return 0;
11058 }
11059 mips_pic = NO_PIC;
143d77c5 11060 mips_abicalls = FALSE;
252b5132
RH
11061 break;
11062
11063 /* The -xgot option tells the assembler to use 32 offsets when
11064 accessing the got in SVR4_PIC mode. It is for Irix
11065 compatibility. */
11066 case OPTION_XGOT:
11067 mips_big_got = 1;
11068 break;
0f074f60 11069#endif /* OBJ_ELF */
252b5132
RH
11070
11071 case 'G':
11072 if (! USE_GLOBAL_POINTER_OPT)
11073 {
11074 as_bad (_("-G is not supported for this configuration"));
11075 return 0;
11076 }
11077 else if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
11078 {
11079 as_bad (_("-G may not be used with SVR4 or embedded PIC code"));
11080 return 0;
11081 }
11082 else
11083 g_switch_value = atoi (arg);
11084 g_switch_seen = 1;
11085 break;
11086
0f074f60 11087#ifdef OBJ_ELF
34ba82a8
TS
11088 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
11089 and -mabi=64. */
252b5132 11090 case OPTION_32:
34ba82a8
TS
11091 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11092 {
11093 as_bad (_("-32 is supported for ELF format only"));
11094 return 0;
11095 }
316f5878 11096 mips_abi = O32_ABI;
252b5132
RH
11097 break;
11098
e013f690 11099 case OPTION_N32:
34ba82a8
TS
11100 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11101 {
11102 as_bad (_("-n32 is supported for ELF format only"));
11103 return 0;
11104 }
316f5878 11105 mips_abi = N32_ABI;
e013f690 11106 break;
252b5132 11107
e013f690 11108 case OPTION_64:
34ba82a8
TS
11109 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11110 {
11111 as_bad (_("-64 is supported for ELF format only"));
11112 return 0;
11113 }
316f5878 11114 mips_abi = N64_ABI;
e013f690
TS
11115 if (! support_64bit_objects())
11116 as_fatal (_("No compiled in support for 64 bit object file format"));
252b5132 11117 break;
ae948b86 11118#endif /* OBJ_ELF */
252b5132 11119
c97ef257 11120 case OPTION_GP32:
a325df1d 11121 file_mips_gp32 = 1;
c97ef257
AH
11122 break;
11123
11124 case OPTION_GP64:
a325df1d 11125 file_mips_gp32 = 0;
c97ef257 11126 break;
252b5132 11127
ca4e0257 11128 case OPTION_FP32:
a325df1d 11129 file_mips_fp32 = 1;
316f5878
RS
11130 break;
11131
11132 case OPTION_FP64:
11133 file_mips_fp32 = 0;
ca4e0257
RS
11134 break;
11135
ae948b86 11136#ifdef OBJ_ELF
252b5132 11137 case OPTION_MABI:
34ba82a8
TS
11138 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11139 {
11140 as_bad (_("-mabi is supported for ELF format only"));
11141 return 0;
11142 }
e013f690 11143 if (strcmp (arg, "32") == 0)
316f5878 11144 mips_abi = O32_ABI;
e013f690 11145 else if (strcmp (arg, "o64") == 0)
316f5878 11146 mips_abi = O64_ABI;
e013f690 11147 else if (strcmp (arg, "n32") == 0)
316f5878 11148 mips_abi = N32_ABI;
e013f690
TS
11149 else if (strcmp (arg, "64") == 0)
11150 {
316f5878 11151 mips_abi = N64_ABI;
e013f690
TS
11152 if (! support_64bit_objects())
11153 as_fatal (_("No compiled in support for 64 bit object file "
11154 "format"));
11155 }
11156 else if (strcmp (arg, "eabi") == 0)
316f5878 11157 mips_abi = EABI_ABI;
e013f690 11158 else
da0e507f
TS
11159 {
11160 as_fatal (_("invalid abi -mabi=%s"), arg);
11161 return 0;
11162 }
252b5132 11163 break;
e013f690 11164#endif /* OBJ_ELF */
252b5132 11165
6b76fefe 11166 case OPTION_M7000_HILO_FIX:
b34976b6 11167 mips_7000_hilo_fix = TRUE;
6b76fefe
CM
11168 break;
11169
9ee72ff1 11170 case OPTION_MNO_7000_HILO_FIX:
b34976b6 11171 mips_7000_hilo_fix = FALSE;
6b76fefe
CM
11172 break;
11173
ecb4347a
DJ
11174#ifdef OBJ_ELF
11175 case OPTION_MDEBUG:
b34976b6 11176 mips_flag_mdebug = TRUE;
ecb4347a
DJ
11177 break;
11178
11179 case OPTION_NO_MDEBUG:
b34976b6 11180 mips_flag_mdebug = FALSE;
ecb4347a
DJ
11181 break;
11182#endif /* OBJ_ELF */
11183
252b5132
RH
11184 default:
11185 return 0;
11186 }
11187
11188 return 1;
11189}
316f5878
RS
11190\f
11191/* Set up globals to generate code for the ISA or processor
11192 described by INFO. */
252b5132 11193
252b5132 11194static void
316f5878
RS
11195mips_set_architecture (info)
11196 const struct mips_cpu_info *info;
252b5132 11197{
316f5878 11198 if (info != 0)
252b5132 11199 {
316f5878
RS
11200 mips_arch_info = info;
11201 mips_arch = info->cpu;
11202 mips_opts.isa = info->isa;
252b5132 11203 }
252b5132
RH
11204}
11205
252b5132 11206
316f5878 11207/* Likewise for tuning. */
252b5132 11208
316f5878
RS
11209static void
11210mips_set_tune (info)
11211 const struct mips_cpu_info *info;
11212{
11213 if (info != 0)
11214 {
11215 mips_tune_info = info;
11216 mips_tune = info->cpu;
11217 }
11218}
80cc45a5 11219
34ba82a8 11220
252b5132 11221void
e9670677
MR
11222mips_after_parse_args ()
11223{
e9670677
MR
11224 /* GP relative stuff not working for PE */
11225 if (strncmp (TARGET_OS, "pe", 2) == 0
11226 && g_switch_value != 0)
11227 {
11228 if (g_switch_seen)
11229 as_bad (_("-G not supported in this configuration."));
11230 g_switch_value = 0;
11231 }
11232
cac012d6
AO
11233 if (mips_abi == NO_ABI)
11234 mips_abi = MIPS_DEFAULT_ABI;
11235
22923709
RS
11236 /* The following code determines the architecture and register size.
11237 Similar code was added to GCC 3.3 (see override_options() in
11238 config/mips/mips.c). The GAS and GCC code should be kept in sync
11239 as much as possible. */
e9670677 11240
316f5878
RS
11241 if (mips_arch_string != 0)
11242 mips_set_architecture (mips_parse_cpu ("-march", mips_arch_string));
e9670677 11243
316f5878
RS
11244 if (mips_tune_string != 0)
11245 mips_set_tune (mips_parse_cpu ("-mtune", mips_tune_string));
e9670677 11246
316f5878 11247 if (file_mips_isa != ISA_UNKNOWN)
e9670677 11248 {
316f5878
RS
11249 /* Handle -mipsN. At this point, file_mips_isa contains the
11250 ISA level specified by -mipsN, while mips_opts.isa contains
11251 the -march selection (if any). */
11252 if (mips_arch_info != 0)
e9670677 11253 {
316f5878
RS
11254 /* -march takes precedence over -mipsN, since it is more descriptive.
11255 There's no harm in specifying both as long as the ISA levels
11256 are the same. */
11257 if (file_mips_isa != mips_opts.isa)
11258 as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
11259 mips_cpu_info_from_isa (file_mips_isa)->name,
11260 mips_cpu_info_from_isa (mips_opts.isa)->name);
e9670677 11261 }
316f5878
RS
11262 else
11263 mips_set_architecture (mips_cpu_info_from_isa (file_mips_isa));
e9670677
MR
11264 }
11265
316f5878
RS
11266 if (mips_arch_info == 0)
11267 mips_set_architecture (mips_parse_cpu ("default CPU",
11268 MIPS_CPU_STRING_DEFAULT));
e9670677 11269
316f5878
RS
11270 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (mips_opts.isa))
11271 as_bad ("-march=%s is not compatible with the selected ABI",
11272 mips_arch_info->name);
e9670677 11273
316f5878
RS
11274 /* Optimize for mips_arch, unless -mtune selects a different processor. */
11275 if (mips_tune_info == 0)
11276 mips_set_tune (mips_arch_info);
e9670677 11277
316f5878 11278 if (file_mips_gp32 >= 0)
e9670677 11279 {
316f5878
RS
11280 /* The user specified the size of the integer registers. Make sure
11281 it agrees with the ABI and ISA. */
11282 if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
11283 as_bad (_("-mgp64 used with a 32-bit processor"));
11284 else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
11285 as_bad (_("-mgp32 used with a 64-bit ABI"));
11286 else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
11287 as_bad (_("-mgp64 used with a 32-bit ABI"));
e9670677
MR
11288 }
11289 else
11290 {
316f5878
RS
11291 /* Infer the integer register size from the ABI and processor.
11292 Restrict ourselves to 32-bit registers if that's all the
11293 processor has, or if the ABI cannot handle 64-bit registers. */
11294 file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
11295 || !ISA_HAS_64BIT_REGS (mips_opts.isa));
e9670677
MR
11296 }
11297
316f5878
RS
11298 /* ??? GAS treats single-float processors as though they had 64-bit
11299 float registers (although it complains when double-precision
11300 instructions are used). As things stand, saying they have 32-bit
11301 registers would lead to spurious "register must be even" messages.
11302 So here we assume float registers are always the same size as
11303 integer ones, unless the user says otherwise. */
11304 if (file_mips_fp32 < 0)
11305 file_mips_fp32 = file_mips_gp32;
e9670677 11306
316f5878 11307 /* End of GCC-shared inference code. */
e9670677 11308
316f5878
RS
11309 /* ??? When do we want this flag to be set? Who uses it? */
11310 if (file_mips_gp32 == 1
11311 && mips_abi == NO_ABI
11312 && ISA_HAS_64BIT_REGS (mips_opts.isa))
11313 mips_32bitmode = 1;
e9670677
MR
11314
11315 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
11316 as_bad (_("trap exception not supported at ISA 1"));
11317
e9670677
MR
11318 /* If the selected architecture includes support for ASEs, enable
11319 generation of code for them. */
a4672219
TS
11320 if (mips_opts.mips16 == -1)
11321 mips_opts.mips16 = (CPU_HAS_MIPS16 (mips_arch)) ? 1 : 0;
ffdefa66 11322 if (mips_opts.ase_mips3d == -1)
a4672219 11323 mips_opts.ase_mips3d = (CPU_HAS_MIPS3D (mips_arch)) ? 1 : 0;
ffdefa66 11324 if (mips_opts.ase_mdmx == -1)
a4672219 11325 mips_opts.ase_mdmx = (CPU_HAS_MDMX (mips_arch)) ? 1 : 0;
e9670677 11326
e9670677 11327 file_mips_isa = mips_opts.isa;
a4672219 11328 file_ase_mips16 = mips_opts.mips16;
e9670677
MR
11329 file_ase_mips3d = mips_opts.ase_mips3d;
11330 file_ase_mdmx = mips_opts.ase_mdmx;
11331 mips_opts.gp32 = file_mips_gp32;
11332 mips_opts.fp32 = file_mips_fp32;
11333
ecb4347a
DJ
11334 if (mips_flag_mdebug < 0)
11335 {
11336#ifdef OBJ_MAYBE_ECOFF
11337 if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
11338 mips_flag_mdebug = 1;
11339 else
11340#endif /* OBJ_MAYBE_ECOFF */
11341 mips_flag_mdebug = 0;
11342 }
e9670677
MR
11343}
11344\f
11345void
252b5132
RH
11346mips_init_after_args ()
11347{
11348 /* initialize opcodes */
11349 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
beae10d5 11350 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
252b5132
RH
11351}
11352
11353long
11354md_pcrel_from (fixP)
11355 fixS *fixP;
11356{
11357 if (OUTPUT_FLAVOR != bfd_target_aout_flavour
11358 && fixP->fx_addsy != (symbolS *) NULL
11359 && ! S_IS_DEFINED (fixP->fx_addsy))
af6ae2ad 11360 return 4;
252b5132 11361
c9914766 11362 /* Return the address of the delay slot. */
252b5132
RH
11363 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
11364}
11365
252b5132
RH
11366/* This is called before the symbol table is processed. In order to
11367 work with gcc when using mips-tfile, we must keep all local labels.
11368 However, in other cases, we want to discard them. If we were
11369 called with -g, but we didn't see any debugging information, it may
11370 mean that gcc is smuggling debugging information through to
11371 mips-tfile, in which case we must generate all local labels. */
11372
11373void
11374mips_frob_file_before_adjust ()
11375{
11376#ifndef NO_ECOFF_DEBUGGING
11377 if (ECOFF_DEBUGGING
11378 && mips_debug != 0
11379 && ! ecoff_debugging_seen)
11380 flag_keep_locals = 1;
11381#endif
11382}
11383
11384/* Sort any unmatched HI16_S relocs so that they immediately precede
94f592af 11385 the corresponding LO reloc. This is called before md_apply_fix3 and
252b5132
RH
11386 tc_gen_reloc. Unmatched HI16_S relocs can only be generated by
11387 explicit use of the %hi modifier. */
11388
11389void
11390mips_frob_file ()
11391{
11392 struct mips_hi_fixup *l;
11393
11394 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
11395 {
11396 segment_info_type *seginfo;
11397 int pass;
11398
5919d012 11399 assert (reloc_needs_lo_p (l->fixp->fx_r_type));
252b5132 11400
5919d012
RS
11401 /* If a GOT16 relocation turns out to be against a global symbol,
11402 there isn't supposed to be a matching LO. */
11403 if (l->fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
11404 && !pic_need_relax (l->fixp->fx_addsy, l->seg))
11405 continue;
11406
11407 /* Check quickly whether the next fixup happens to be a matching %lo. */
11408 if (fixup_has_matching_lo_p (l->fixp))
252b5132
RH
11409 continue;
11410
11411 /* Look through the fixups for this segment for a matching %lo.
11412 When we find one, move the %hi just in front of it. We do
11413 this in two passes. In the first pass, we try to find a
11414 unique %lo. In the second pass, we permit multiple %hi
11415 relocs for a single %lo (this is a GNU extension). */
11416 seginfo = seg_info (l->seg);
11417 for (pass = 0; pass < 2; pass++)
11418 {
11419 fixS *f, *prev;
11420
11421 prev = NULL;
11422 for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
11423 {
11424 /* Check whether this is a %lo fixup which matches l->fixp. */
11425 if (f->fx_r_type == BFD_RELOC_LO16
11426 && f->fx_addsy == l->fixp->fx_addsy
11427 && f->fx_offset == l->fixp->fx_offset
11428 && (pass == 1
11429 || prev == NULL
5919d012
RS
11430 || !reloc_needs_lo_p (prev->fx_r_type)
11431 || !fixup_has_matching_lo_p (prev)))
252b5132
RH
11432 {
11433 fixS **pf;
11434
11435 /* Move l->fixp before f. */
11436 for (pf = &seginfo->fix_root;
11437 *pf != l->fixp;
11438 pf = &(*pf)->fx_next)
11439 assert (*pf != NULL);
11440
11441 *pf = l->fixp->fx_next;
11442
11443 l->fixp->fx_next = f;
11444 if (prev == NULL)
11445 seginfo->fix_root = l->fixp;
11446 else
11447 prev->fx_next = l->fixp;
11448
11449 break;
11450 }
11451
11452 prev = f;
11453 }
11454
11455 if (f != NULL)
11456 break;
11457
11458#if 0 /* GCC code motion plus incomplete dead code elimination
11459 can leave a %hi without a %lo. */
11460 if (pass == 1)
11461 as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
11462 _("Unmatched %%hi reloc"));
11463#endif
11464 }
11465 }
11466}
11467
11468/* When generating embedded PIC code we need to use a special
11469 relocation to represent the difference of two symbols in the .text
11470 section (switch tables use a difference of this sort). See
11471 include/coff/mips.h for details. This macro checks whether this
11472 fixup requires the special reloc. */
11473#define SWITCH_TABLE(fixp) \
11474 ((fixp)->fx_r_type == BFD_RELOC_32 \
bb2d6cd7 11475 && OUTPUT_FLAVOR != bfd_target_elf_flavour \
252b5132
RH
11476 && (fixp)->fx_addsy != NULL \
11477 && (fixp)->fx_subsy != NULL \
11478 && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
11479 && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
11480
11481/* When generating embedded PIC code we must keep all PC relative
11482 relocations, in case the linker has to relax a call. We also need
f6688943
TS
11483 to keep relocations for switch table entries.
11484
11485 We may have combined relocations without symbols in the N32/N64 ABI.
11486 We have to prevent gas from dropping them. */
252b5132 11487
252b5132
RH
11488int
11489mips_force_relocation (fixp)
11490 fixS *fixp;
11491{
ae6063d4 11492 if (generic_force_reloc (fixp))
252b5132
RH
11493 return 1;
11494
f6688943
TS
11495 if (HAVE_NEWABI
11496 && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
11497 && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
11498 || fixp->fx_r_type == BFD_RELOC_HI16_S
11499 || fixp->fx_r_type == BFD_RELOC_LO16))
11500 return 1;
11501
252b5132
RH
11502 return (mips_pic == EMBEDDED_PIC
11503 && (fixp->fx_pcrel
11504 || SWITCH_TABLE (fixp)
11505 || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
11506 || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
11507}
11508
45f8dfe8
AO
11509/* This hook is called before a fix is simplified. We don't really
11510 decide whether to skip a fix here. Rather, we turn global symbols
11511 used as branch targets into local symbols, such that they undergo
11512 simplification. We can only do this if the symbol is defined and
11513 it is in the same section as the branch. If this doesn't hold, we
11514 emit a better error message than just saying the relocation is not
11515 valid for the selected object format.
11516
11517 FIXP is the fix-up we're going to try to simplify, SEG is the
11518 segment in which the fix up occurs. The return value should be
11519 non-zero to indicate the fix-up is valid for further
11520 simplifications. */
11521
11522int
11523mips_validate_fix (fixP, seg)
11524 struct fix *fixP;
11525 asection *seg;
11526{
11527 /* There's a lot of discussion on whether it should be possible to
11528 use R_MIPS_PC16 to represent branch relocations. The outcome
11529 seems to be that it can, but gas/bfd are very broken in creating
11530 RELA relocations for this, so for now we only accept branches to
11531 symbols in the same section. Anything else is of dubious value,
11532 since there's no guarantee that at link time the symbol would be
11533 in range. Even for branches to local symbols this is arguably
11534 wrong, since it we assume the symbol is not going to be
11535 overridden, which should be possible per ELF library semantics,
11536 but then, there isn't a dynamic relocation that could be used to
11537 this effect, and the target would likely be out of range as well.
11538
11539 Unfortunately, it seems that there is too much code out there
11540 that relies on branches to symbols that are global to be resolved
11541 as if they were local, like the IRIX tools do, so we do it as
11542 well, but with a warning so that people are reminded to fix their
11543 code. If we ever get back to using R_MIPS_PC16 for branch
11544 targets, this entire block should go away (and probably the
11545 whole function). */
11546
11547 if (fixP->fx_r_type == BFD_RELOC_16_PCREL_S2
11548 && (((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
11549 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
11550 && mips_pic != EMBEDDED_PIC)
11551 || bfd_reloc_type_lookup (stdoutput, BFD_RELOC_16_PCREL_S2) == NULL)
11552 && fixP->fx_addsy)
11553 {
11554 if (! S_IS_DEFINED (fixP->fx_addsy))
11555 {
11556 as_bad_where (fixP->fx_file, fixP->fx_line,
11557 _("Cannot branch to undefined symbol."));
11558 /* Avoid any further errors about this fixup. */
11559 fixP->fx_done = 1;
11560 }
11561 else if (S_GET_SEGMENT (fixP->fx_addsy) != seg)
11562 {
11563 as_bad_where (fixP->fx_file, fixP->fx_line,
11564 _("Cannot branch to symbol in another section."));
11565 fixP->fx_done = 1;
11566 }
11567 else if (S_IS_EXTERNAL (fixP->fx_addsy))
11568 {
11569 symbolS *sym = fixP->fx_addsy;
11570
11571 as_warn_where (fixP->fx_file, fixP->fx_line,
11572 _("Pretending global symbol used as branch target is local."));
11573
11574 fixP->fx_addsy = symbol_create (S_GET_NAME (sym),
11575 S_GET_SEGMENT (sym),
11576 S_GET_VALUE (sym),
11577 symbol_get_frag (sym));
11578 copy_symbol_attributes (fixP->fx_addsy, sym);
11579 S_CLEAR_EXTERNAL (fixP->fx_addsy);
11580 assert (symbol_resolved_p (sym));
11581 symbol_mark_resolved (fixP->fx_addsy);
11582 }
11583 }
11584
11585 return 1;
11586}
11587
add55e1f
RS
11588#ifdef OBJ_ELF
11589static int
11590mips_need_elf_addend_fixup (fixP)
11591 fixS *fixP;
11592{
2d2bf3e0
CD
11593 if (S_GET_OTHER (fixP->fx_addsy) == STO_MIPS16)
11594 return 1;
b25a253c
CD
11595 if (mips_pic == EMBEDDED_PIC
11596 && S_IS_WEAK (fixP->fx_addsy))
11597 return 1;
11598 if (mips_pic != EMBEDDED_PIC
11599 && (S_IS_WEAK (fixP->fx_addsy)
bad9ca53 11600 || S_IS_EXTERNAL (fixP->fx_addsy))
2d2bf3e0
CD
11601 && !S_IS_COMMON (fixP->fx_addsy))
11602 return 1;
9204e615
DJ
11603 if (((bfd_get_section_flags (stdoutput,
11604 S_GET_SEGMENT (fixP->fx_addsy))
11605 & (SEC_LINK_ONCE | SEC_MERGE)) != 0)
11606 || !strncmp (segment_name (S_GET_SEGMENT (fixP->fx_addsy)),
11607 ".gnu.linkonce",
11608 sizeof (".gnu.linkonce") - 1))
2d2bf3e0
CD
11609 return 1;
11610 return 0;
add55e1f
RS
11611}
11612#endif
11613
252b5132
RH
11614/* Apply a fixup to the object file. */
11615
94f592af
NC
11616void
11617md_apply_fix3 (fixP, valP, seg)
252b5132 11618 fixS *fixP;
98d3f06f 11619 valueT *valP;
94f592af 11620 segT seg ATTRIBUTE_UNUSED;
252b5132 11621{
874e8986 11622 bfd_byte *buf;
98aa84af
AM
11623 long insn;
11624 valueT value;
ed6fb7bd 11625 static int previous_fx_r_type = 0;
252b5132 11626
65551fa4
CD
11627 /* FIXME: Maybe just return for all reloc types not listed below?
11628 Eric Christopher says: "This is stupid, please rewrite md_apply_fix3. */
11629 if (fixP->fx_r_type == BFD_RELOC_8)
11630 return;
11631
252b5132
RH
11632 assert (fixP->fx_size == 4
11633 || fixP->fx_r_type == BFD_RELOC_16
f6688943
TS
11634 || fixP->fx_r_type == BFD_RELOC_32
11635 || fixP->fx_r_type == BFD_RELOC_MIPS_JMP
11636 || fixP->fx_r_type == BFD_RELOC_HI16_S
11637 || fixP->fx_r_type == BFD_RELOC_LO16
11638 || fixP->fx_r_type == BFD_RELOC_GPREL16
76b3015f 11639 || fixP->fx_r_type == BFD_RELOC_MIPS_LITERAL
f6688943 11640 || fixP->fx_r_type == BFD_RELOC_GPREL32
252b5132 11641 || fixP->fx_r_type == BFD_RELOC_64
f6688943
TS
11642 || fixP->fx_r_type == BFD_RELOC_CTOR
11643 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
11644 || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHEST
11645 || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHER
11646 || fixP->fx_r_type == BFD_RELOC_MIPS_SCN_DISP
11647 || fixP->fx_r_type == BFD_RELOC_MIPS_REL16
11648 || fixP->fx_r_type == BFD_RELOC_MIPS_RELGOT
252b5132 11649 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
438c16b8
TS
11650 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
11651 || fixP->fx_r_type == BFD_RELOC_MIPS_JALR);
252b5132 11652
98d3f06f 11653 value = *valP;
252b5132
RH
11654
11655 /* If we aren't adjusting this fixup to be against the section
11656 symbol, we need to adjust the value. */
11657#ifdef OBJ_ELF
11658 if (fixP->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour)
bb2d6cd7 11659 {
add55e1f 11660 if (mips_need_elf_addend_fixup (fixP))
98aa84af 11661 {
d6e9d61a 11662 reloc_howto_type *howto;
98aa84af 11663 valueT symval = S_GET_VALUE (fixP->fx_addsy);
94f592af 11664
98aa84af 11665 value -= symval;
d6e9d61a
MR
11666
11667 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
9204e615 11668 if (value != 0 && howto && howto->partial_inplace)
98aa84af
AM
11669 {
11670 /* In this case, the bfd_install_relocation routine will
11671 incorrectly add the symbol value back in. We just want
7a49a8c2 11672 the addend to appear in the object file.
b34976b6 11673
9204e615
DJ
11674 The condition above used to include
11675 "&& (! fixP->fx_pcrel || howto->pcrel_offset)".
11676
11677 However, howto can't be trusted here, because we
11678 might change the reloc type in tc_gen_reloc. We can
11679 check howto->partial_inplace because that conversion
11680 happens to preserve howto->partial_inplace; but it
11681 does not preserve howto->pcrel_offset. I've just
11682 eliminated the check, because all MIPS PC-relative
11683 relocations are marked howto->pcrel_offset.
11684
11685 howto->pcrel_offset was originally added for
11686 R_MIPS_PC16, which is generated for code like
b34976b6 11687
7a49a8c2
L
11688 globl g1 .text
11689 .text
11690 .space 20
11691 g1:
11692 x:
11693 bal g1
11694 */
98aa84af 11695 value -= symval;
7461da6e
RS
11696
11697 /* Make sure the addend is still non-zero. If it became zero
11698 after the last operation, set it to a spurious value and
11699 subtract the same value from the object file's contents. */
11700 if (value == 0)
11701 {
11702 value = 8;
11703
11704 /* The in-place addends for LO16 relocations are signed;
11705 leave the matching HI16 in-place addends as zero. */
11706 if (fixP->fx_r_type != BFD_RELOC_HI16_S)
11707 {
7461da6e
RS
11708 bfd_vma contents, mask, field;
11709
7461da6e
RS
11710 contents = bfd_get_bits (fixP->fx_frag->fr_literal
11711 + fixP->fx_where,
11712 fixP->fx_size * 8,
11713 target_big_endian);
11714
11715 /* MASK has bits set where the relocation should go.
11716 FIELD is -value, shifted into the appropriate place
11717 for this relocation. */
11718 mask = 1 << (howto->bitsize - 1);
11719 mask = (((mask - 1) << 1) | 1) << howto->bitpos;
11720 field = (-value >> howto->rightshift) << howto->bitpos;
11721
11722 bfd_put_bits ((field & mask) | (contents & ~mask),
11723 fixP->fx_frag->fr_literal + fixP->fx_where,
11724 fixP->fx_size * 8,
11725 target_big_endian);
11726 }
11727 }
98aa84af
AM
11728 }
11729 }
252b5132 11730
bb2d6cd7
GK
11731 /* This code was generated using trial and error and so is
11732 fragile and not trustworthy. If you change it, you should
11733 rerun the elf-rel, elf-rel2, and empic testcases and ensure
11734 they still pass. */
11735 if (fixP->fx_pcrel || fixP->fx_subsy != NULL)
11736 {
11737 value += fixP->fx_frag->fr_address + fixP->fx_where;
11738
11739 /* BFD's REL handling, for MIPS, is _very_ weird.
11740 This gives the right results, but it can't possibly
11741 be the way things are supposed to work. */
0b25d3e6 11742 if (fixP->fx_r_type != BFD_RELOC_16_PCREL_S2
bb2d6cd7
GK
11743 || S_GET_SEGMENT (fixP->fx_addsy) != undefined_section)
11744 value += fixP->fx_frag->fr_address + fixP->fx_where;
11745 }
11746 }
11747#endif
252b5132 11748
94f592af 11749 fixP->fx_addnumber = value; /* Remember value for tc_gen_reloc. */
252b5132 11750
ed6fb7bd
SC
11751 /* We are not done if this is a composite relocation to set up gp. */
11752 if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel
11753 && !(fixP->fx_r_type == BFD_RELOC_MIPS_SUB
10181a0d
AO
11754 || (fixP->fx_r_type == BFD_RELOC_64
11755 && (previous_fx_r_type == BFD_RELOC_GPREL32
11756 || previous_fx_r_type == BFD_RELOC_GPREL16))
ed6fb7bd
SC
11757 || (previous_fx_r_type == BFD_RELOC_MIPS_SUB
11758 && (fixP->fx_r_type == BFD_RELOC_HI16_S
11759 || fixP->fx_r_type == BFD_RELOC_LO16))))
252b5132 11760 fixP->fx_done = 1;
ed6fb7bd 11761 previous_fx_r_type = fixP->fx_r_type;
252b5132
RH
11762
11763 switch (fixP->fx_r_type)
11764 {
11765 case BFD_RELOC_MIPS_JMP:
e369bcce
TS
11766 case BFD_RELOC_MIPS_SHIFT5:
11767 case BFD_RELOC_MIPS_SHIFT6:
11768 case BFD_RELOC_MIPS_GOT_DISP:
11769 case BFD_RELOC_MIPS_GOT_PAGE:
11770 case BFD_RELOC_MIPS_GOT_OFST:
11771 case BFD_RELOC_MIPS_SUB:
11772 case BFD_RELOC_MIPS_INSERT_A:
11773 case BFD_RELOC_MIPS_INSERT_B:
11774 case BFD_RELOC_MIPS_DELETE:
11775 case BFD_RELOC_MIPS_HIGHEST:
11776 case BFD_RELOC_MIPS_HIGHER:
11777 case BFD_RELOC_MIPS_SCN_DISP:
11778 case BFD_RELOC_MIPS_REL16:
11779 case BFD_RELOC_MIPS_RELGOT:
11780 case BFD_RELOC_MIPS_JALR:
252b5132
RH
11781 case BFD_RELOC_HI16:
11782 case BFD_RELOC_HI16_S:
cdf6fd85 11783 case BFD_RELOC_GPREL16:
252b5132
RH
11784 case BFD_RELOC_MIPS_LITERAL:
11785 case BFD_RELOC_MIPS_CALL16:
11786 case BFD_RELOC_MIPS_GOT16:
cdf6fd85 11787 case BFD_RELOC_GPREL32:
252b5132
RH
11788 case BFD_RELOC_MIPS_GOT_HI16:
11789 case BFD_RELOC_MIPS_GOT_LO16:
11790 case BFD_RELOC_MIPS_CALL_HI16:
11791 case BFD_RELOC_MIPS_CALL_LO16:
11792 case BFD_RELOC_MIPS16_GPREL:
11793 if (fixP->fx_pcrel)
11794 as_bad_where (fixP->fx_file, fixP->fx_line,
11795 _("Invalid PC relative reloc"));
11796 /* Nothing needed to do. The value comes from the reloc entry */
11797 break;
11798
11799 case BFD_RELOC_MIPS16_JMP:
11800 /* We currently always generate a reloc against a symbol, which
11801 means that we don't want an addend even if the symbol is
11802 defined. */
11803 fixP->fx_addnumber = 0;
11804 break;
11805
11806 case BFD_RELOC_PCREL_HI16_S:
11807 /* The addend for this is tricky if it is internal, so we just
11808 do everything here rather than in bfd_install_relocation. */
bdaaa2e1 11809 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
bb2d6cd7
GK
11810 && !fixP->fx_done
11811 && value != 0)
11812 break;
11813 if (fixP->fx_addsy
11814 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
252b5132
RH
11815 {
11816 /* For an external symbol adjust by the address to make it
11817 pcrel_offset. We use the address of the RELLO reloc
11818 which follows this one. */
11819 value += (fixP->fx_next->fx_frag->fr_address
11820 + fixP->fx_next->fx_where);
11821 }
e7d556df 11822 value = ((value + 0x8000) >> 16) & 0xffff;
874e8986 11823 buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
252b5132
RH
11824 if (target_big_endian)
11825 buf += 2;
874e8986 11826 md_number_to_chars ((char *) buf, value, 2);
252b5132
RH
11827 break;
11828
11829 case BFD_RELOC_PCREL_LO16:
11830 /* The addend for this is tricky if it is internal, so we just
11831 do everything here rather than in bfd_install_relocation. */
bdaaa2e1 11832 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
bb2d6cd7
GK
11833 && !fixP->fx_done
11834 && value != 0)
11835 break;
11836 if (fixP->fx_addsy
11837 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
252b5132 11838 value += fixP->fx_frag->fr_address + fixP->fx_where;
874e8986 11839 buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
252b5132
RH
11840 if (target_big_endian)
11841 buf += 2;
874e8986 11842 md_number_to_chars ((char *) buf, value, 2);
252b5132
RH
11843 break;
11844
11845 case BFD_RELOC_64:
11846 /* This is handled like BFD_RELOC_32, but we output a sign
11847 extended value if we are only 32 bits. */
11848 if (fixP->fx_done
11849 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11850 {
11851 if (8 <= sizeof (valueT))
11852 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11853 value, 8);
11854 else
11855 {
11856 long w1, w2;
11857 long hiv;
11858
11859 w1 = w2 = fixP->fx_where;
11860 if (target_big_endian)
11861 w1 += 4;
11862 else
11863 w2 += 4;
11864 md_number_to_chars (fixP->fx_frag->fr_literal + w1, value, 4);
11865 if ((value & 0x80000000) != 0)
11866 hiv = 0xffffffff;
11867 else
11868 hiv = 0;
11869 md_number_to_chars (fixP->fx_frag->fr_literal + w2, hiv, 4);
11870 }
11871 }
11872 break;
11873
056350c6 11874 case BFD_RELOC_RVA:
252b5132
RH
11875 case BFD_RELOC_32:
11876 /* If we are deleting this reloc entry, we must fill in the
11877 value now. This can happen if we have a .word which is not
11878 resolved when it appears but is later defined. We also need
11879 to fill in the value if this is an embedded PIC switch table
11880 entry. */
11881 if (fixP->fx_done
11882 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11883 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11884 value, 4);
11885 break;
11886
11887 case BFD_RELOC_16:
11888 /* If we are deleting this reloc entry, we must fill in the
11889 value now. */
11890 assert (fixP->fx_size == 2);
11891 if (fixP->fx_done)
11892 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11893 value, 2);
11894 break;
11895
11896 case BFD_RELOC_LO16:
11897 /* When handling an embedded PIC switch statement, we can wind
11898 up deleting a LO16 reloc. See the 'o' case in mips_ip. */
11899 if (fixP->fx_done)
11900 {
98aa84af 11901 if (value + 0x8000 > 0xffff)
252b5132
RH
11902 as_bad_where (fixP->fx_file, fixP->fx_line,
11903 _("relocation overflow"));
874e8986 11904 buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
252b5132
RH
11905 if (target_big_endian)
11906 buf += 2;
874e8986 11907 md_number_to_chars ((char *) buf, value, 2);
252b5132
RH
11908 }
11909 break;
11910
11911 case BFD_RELOC_16_PCREL_S2:
cb56d3d3
TS
11912 if ((value & 0x3) != 0)
11913 as_bad_where (fixP->fx_file, fixP->fx_line,
0b25d3e6 11914 _("Branch to odd address (%lx)"), (long) value);
cb56d3d3 11915
252b5132
RH
11916 /*
11917 * We need to save the bits in the instruction since fixup_segment()
11918 * might be deleting the relocation entry (i.e., a branch within
11919 * the current segment).
11920 */
af6ae2ad 11921 if (!fixP->fx_done && (value != 0 || HAVE_NEWABI))
bb2d6cd7
GK
11922 break;
11923 /* If 'value' is zero, the remaining reloc code won't actually
11924 do the store, so it must be done here. This is probably
11925 a bug somewhere. */
b25a253c 11926 if (!fixP->fx_done
0b25d3e6
AO
11927 && (fixP->fx_r_type != BFD_RELOC_16_PCREL_S2
11928 || fixP->fx_addsy == NULL /* ??? */
b25a253c 11929 || ! S_IS_DEFINED (fixP->fx_addsy)))
bb2d6cd7 11930 value -= fixP->fx_frag->fr_address + fixP->fx_where;
bdaaa2e1 11931
98aa84af 11932 value = (offsetT) value >> 2;
252b5132
RH
11933
11934 /* update old instruction data */
874e8986 11935 buf = (bfd_byte *) (fixP->fx_where + fixP->fx_frag->fr_literal);
252b5132
RH
11936 if (target_big_endian)
11937 insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
11938 else
11939 insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
11940
98aa84af 11941 if (value + 0x8000 <= 0xffff)
252b5132
RH
11942 insn |= value & 0xffff;
11943 else
11944 {
11945 /* The branch offset is too large. If this is an
11946 unconditional branch, and we are not generating PIC code,
11947 we can convert it to an absolute jump instruction. */
11948 if (mips_pic == NO_PIC
11949 && fixP->fx_done
11950 && fixP->fx_frag->fr_address >= text_section->vma
11951 && (fixP->fx_frag->fr_address
11952 < text_section->vma + text_section->_raw_size)
11953 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
11954 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
11955 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
11956 {
11957 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
11958 insn = 0x0c000000; /* jal */
11959 else
11960 insn = 0x08000000; /* j */
11961 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
11962 fixP->fx_done = 0;
11963 fixP->fx_addsy = section_symbol (text_section);
11964 fixP->fx_addnumber = (value << 2) + md_pcrel_from (fixP);
11965 }
11966 else
11967 {
4a6a3df4
AO
11968 /* If we got here, we have branch-relaxation disabled,
11969 and there's nothing we can do to fix this instruction
11970 without turning it into a longer sequence. */
252b5132
RH
11971 as_bad_where (fixP->fx_file, fixP->fx_line,
11972 _("Branch out of range"));
11973 }
11974 }
11975
11976 md_number_to_chars ((char *) buf, (valueT) insn, 4);
11977 break;
11978
11979 case BFD_RELOC_VTABLE_INHERIT:
11980 fixP->fx_done = 0;
11981 if (fixP->fx_addsy
11982 && !S_IS_DEFINED (fixP->fx_addsy)
11983 && !S_IS_WEAK (fixP->fx_addsy))
11984 S_SET_WEAK (fixP->fx_addsy);
11985 break;
11986
11987 case BFD_RELOC_VTABLE_ENTRY:
11988 fixP->fx_done = 0;
11989 break;
11990
11991 default:
11992 internalError ();
11993 }
252b5132
RH
11994}
11995
11996#if 0
11997void
11998printInsn (oc)
11999 unsigned long oc;
12000{
12001 const struct mips_opcode *p;
12002 int treg, sreg, dreg, shamt;
12003 short imm;
12004 const char *args;
12005 int i;
12006
12007 for (i = 0; i < NUMOPCODES; ++i)
12008 {
12009 p = &mips_opcodes[i];
12010 if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
12011 {
12012 printf ("%08lx %s\t", oc, p->name);
12013 treg = (oc >> 16) & 0x1f;
12014 sreg = (oc >> 21) & 0x1f;
12015 dreg = (oc >> 11) & 0x1f;
12016 shamt = (oc >> 6) & 0x1f;
12017 imm = oc;
12018 for (args = p->args;; ++args)
12019 {
12020 switch (*args)
12021 {
12022 case '\0':
12023 printf ("\n");
12024 break;
12025
12026 case ',':
12027 case '(':
12028 case ')':
12029 printf ("%c", *args);
12030 continue;
12031
12032 case 'r':
12033 assert (treg == sreg);
12034 printf ("$%d,$%d", treg, sreg);
12035 continue;
12036
12037 case 'd':
12038 case 'G':
12039 printf ("$%d", dreg);
12040 continue;
12041
12042 case 't':
12043 case 'E':
12044 printf ("$%d", treg);
12045 continue;
12046
12047 case 'k':
12048 printf ("0x%x", treg);
12049 continue;
12050
12051 case 'b':
12052 case 's':
12053 printf ("$%d", sreg);
12054 continue;
12055
12056 case 'a':
12057 printf ("0x%08lx", oc & 0x1ffffff);
12058 continue;
12059
12060 case 'i':
12061 case 'j':
12062 case 'o':
12063 case 'u':
12064 printf ("%d", imm);
12065 continue;
12066
12067 case '<':
12068 case '>':
12069 printf ("$%d", shamt);
12070 continue;
12071
12072 default:
12073 internalError ();
12074 }
12075 break;
12076 }
12077 return;
12078 }
12079 }
12080 printf (_("%08lx UNDEFINED\n"), oc);
12081}
12082#endif
12083
12084static symbolS *
12085get_symbol ()
12086{
12087 int c;
12088 char *name;
12089 symbolS *p;
12090
12091 name = input_line_pointer;
12092 c = get_symbol_end ();
12093 p = (symbolS *) symbol_find_or_make (name);
12094 *input_line_pointer = c;
12095 return p;
12096}
12097
12098/* Align the current frag to a given power of two. The MIPS assembler
12099 also automatically adjusts any preceding label. */
12100
12101static void
12102mips_align (to, fill, label)
12103 int to;
12104 int fill;
12105 symbolS *label;
12106{
b34976b6 12107 mips_emit_delays (FALSE);
252b5132
RH
12108 frag_align (to, fill, 0);
12109 record_alignment (now_seg, to);
12110 if (label != NULL)
12111 {
12112 assert (S_GET_SEGMENT (label) == now_seg);
49309057 12113 symbol_set_frag (label, frag_now);
252b5132
RH
12114 S_SET_VALUE (label, (valueT) frag_now_fix ());
12115 }
12116}
12117
12118/* Align to a given power of two. .align 0 turns off the automatic
12119 alignment used by the data creating pseudo-ops. */
12120
12121static void
12122s_align (x)
43841e91 12123 int x ATTRIBUTE_UNUSED;
252b5132
RH
12124{
12125 register int temp;
12126 register long temp_fill;
12127 long max_alignment = 15;
12128
12129 /*
12130
12131 o Note that the assembler pulls down any immediately preceeding label
12132 to the aligned address.
12133 o It's not documented but auto alignment is reinstated by
12134 a .align pseudo instruction.
12135 o Note also that after auto alignment is turned off the mips assembler
12136 issues an error on attempt to assemble an improperly aligned data item.
12137 We don't.
12138
12139 */
12140
12141 temp = get_absolute_expression ();
12142 if (temp > max_alignment)
12143 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
12144 else if (temp < 0)
12145 {
12146 as_warn (_("Alignment negative: 0 assumed."));
12147 temp = 0;
12148 }
12149 if (*input_line_pointer == ',')
12150 {
f9419b05 12151 ++input_line_pointer;
252b5132
RH
12152 temp_fill = get_absolute_expression ();
12153 }
12154 else
12155 temp_fill = 0;
12156 if (temp)
12157 {
12158 auto_align = 1;
12159 mips_align (temp, (int) temp_fill,
12160 insn_labels != NULL ? insn_labels->label : NULL);
12161 }
12162 else
12163 {
12164 auto_align = 0;
12165 }
12166
12167 demand_empty_rest_of_line ();
12168}
12169
12170void
12171mips_flush_pending_output ()
12172{
b34976b6 12173 mips_emit_delays (FALSE);
252b5132
RH
12174 mips_clear_insn_labels ();
12175}
12176
12177static void
12178s_change_sec (sec)
12179 int sec;
12180{
12181 segT seg;
12182
12183 /* When generating embedded PIC code, we only use the .text, .lit8,
12184 .sdata and .sbss sections. We change the .data and .rdata
12185 pseudo-ops to use .sdata. */
12186 if (mips_pic == EMBEDDED_PIC
12187 && (sec == 'd' || sec == 'r'))
12188 sec = 's';
12189
12190#ifdef OBJ_ELF
12191 /* The ELF backend needs to know that we are changing sections, so
12192 that .previous works correctly. We could do something like check
b6ff326e 12193 for an obj_section_change_hook macro, but that might be confusing
252b5132
RH
12194 as it would not be appropriate to use it in the section changing
12195 functions in read.c, since obj-elf.c intercepts those. FIXME:
12196 This should be cleaner, somehow. */
12197 obj_elf_section_change_hook ();
12198#endif
12199
b34976b6 12200 mips_emit_delays (FALSE);
252b5132
RH
12201 switch (sec)
12202 {
12203 case 't':
12204 s_text (0);
12205 break;
12206 case 'd':
12207 s_data (0);
12208 break;
12209 case 'b':
12210 subseg_set (bss_section, (subsegT) get_absolute_expression ());
12211 demand_empty_rest_of_line ();
12212 break;
12213
12214 case 'r':
12215 if (USE_GLOBAL_POINTER_OPT)
12216 {
12217 seg = subseg_new (RDATA_SECTION_NAME,
12218 (subsegT) get_absolute_expression ());
12219 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
12220 {
12221 bfd_set_section_flags (stdoutput, seg,
12222 (SEC_ALLOC
12223 | SEC_LOAD
12224 | SEC_READONLY
12225 | SEC_RELOC
12226 | SEC_DATA));
12227 if (strcmp (TARGET_OS, "elf") != 0)
e799a695 12228 record_alignment (seg, 4);
252b5132
RH
12229 }
12230 demand_empty_rest_of_line ();
12231 }
12232 else
12233 {
12234 as_bad (_("No read only data section in this object file format"));
12235 demand_empty_rest_of_line ();
12236 return;
12237 }
12238 break;
12239
12240 case 's':
12241 if (USE_GLOBAL_POINTER_OPT)
12242 {
12243 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
12244 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
12245 {
12246 bfd_set_section_flags (stdoutput, seg,
12247 SEC_ALLOC | SEC_LOAD | SEC_RELOC
12248 | SEC_DATA);
12249 if (strcmp (TARGET_OS, "elf") != 0)
e799a695 12250 record_alignment (seg, 4);
252b5132
RH
12251 }
12252 demand_empty_rest_of_line ();
12253 break;
12254 }
12255 else
12256 {
12257 as_bad (_("Global pointers not supported; recompile -G 0"));
12258 demand_empty_rest_of_line ();
12259 return;
12260 }
12261 }
12262
12263 auto_align = 1;
12264}
b34976b6 12265
cca86cc8
SC
12266void
12267s_change_section (ignore)
12268 int ignore ATTRIBUTE_UNUSED;
12269{
7ed4a06a 12270#ifdef OBJ_ELF
cca86cc8
SC
12271 char *section_name;
12272 char c;
684022ea 12273 char next_c = 0;
cca86cc8
SC
12274 int section_type;
12275 int section_flag;
12276 int section_entry_size;
12277 int section_alignment;
b34976b6 12278
7ed4a06a
TS
12279 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
12280 return;
12281
cca86cc8
SC
12282 section_name = input_line_pointer;
12283 c = get_symbol_end ();
a816d1ed
AO
12284 if (c)
12285 next_c = *(input_line_pointer + 1);
cca86cc8 12286
4cf0dd0d
TS
12287 /* Do we have .section Name<,"flags">? */
12288 if (c != ',' || (c == ',' && next_c == '"'))
cca86cc8 12289 {
4cf0dd0d
TS
12290 /* just after name is now '\0'. */
12291 *input_line_pointer = c;
cca86cc8
SC
12292 input_line_pointer = section_name;
12293 obj_elf_section (ignore);
12294 return;
12295 }
12296 input_line_pointer++;
12297
12298 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
12299 if (c == ',')
12300 section_type = get_absolute_expression ();
12301 else
12302 section_type = 0;
12303 if (*input_line_pointer++ == ',')
12304 section_flag = get_absolute_expression ();
12305 else
12306 section_flag = 0;
12307 if (*input_line_pointer++ == ',')
12308 section_entry_size = get_absolute_expression ();
12309 else
12310 section_entry_size = 0;
12311 if (*input_line_pointer++ == ',')
12312 section_alignment = get_absolute_expression ();
12313 else
12314 section_alignment = 0;
12315
a816d1ed
AO
12316 section_name = xstrdup (section_name);
12317
cca86cc8
SC
12318 obj_elf_change_section (section_name, section_type, section_flag,
12319 section_entry_size, 0, 0, 0);
a816d1ed
AO
12320
12321 if (now_seg->name != section_name)
12322 free (section_name);
7ed4a06a 12323#endif /* OBJ_ELF */
cca86cc8 12324}
252b5132
RH
12325
12326void
12327mips_enable_auto_align ()
12328{
12329 auto_align = 1;
12330}
12331
12332static void
12333s_cons (log_size)
12334 int log_size;
12335{
12336 symbolS *label;
12337
12338 label = insn_labels != NULL ? insn_labels->label : NULL;
b34976b6 12339 mips_emit_delays (FALSE);
252b5132
RH
12340 if (log_size > 0 && auto_align)
12341 mips_align (log_size, 0, label);
12342 mips_clear_insn_labels ();
12343 cons (1 << log_size);
12344}
12345
12346static void
12347s_float_cons (type)
12348 int type;
12349{
12350 symbolS *label;
12351
12352 label = insn_labels != NULL ? insn_labels->label : NULL;
12353
b34976b6 12354 mips_emit_delays (FALSE);
252b5132
RH
12355
12356 if (auto_align)
49309057
ILT
12357 {
12358 if (type == 'd')
12359 mips_align (3, 0, label);
12360 else
12361 mips_align (2, 0, label);
12362 }
252b5132
RH
12363
12364 mips_clear_insn_labels ();
12365
12366 float_cons (type);
12367}
12368
12369/* Handle .globl. We need to override it because on Irix 5 you are
12370 permitted to say
12371 .globl foo .text
12372 where foo is an undefined symbol, to mean that foo should be
12373 considered to be the address of a function. */
12374
12375static void
12376s_mips_globl (x)
43841e91 12377 int x ATTRIBUTE_UNUSED;
252b5132
RH
12378{
12379 char *name;
12380 int c;
12381 symbolS *symbolP;
12382 flagword flag;
12383
12384 name = input_line_pointer;
12385 c = get_symbol_end ();
12386 symbolP = symbol_find_or_make (name);
12387 *input_line_pointer = c;
12388 SKIP_WHITESPACE ();
12389
12390 /* On Irix 5, every global symbol that is not explicitly labelled as
12391 being a function is apparently labelled as being an object. */
12392 flag = BSF_OBJECT;
12393
12394 if (! is_end_of_line[(unsigned char) *input_line_pointer])
12395 {
12396 char *secname;
12397 asection *sec;
12398
12399 secname = input_line_pointer;
12400 c = get_symbol_end ();
12401 sec = bfd_get_section_by_name (stdoutput, secname);
12402 if (sec == NULL)
12403 as_bad (_("%s: no such section"), secname);
12404 *input_line_pointer = c;
12405
12406 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
12407 flag = BSF_FUNCTION;
12408 }
12409
49309057 12410 symbol_get_bfdsym (symbolP)->flags |= flag;
252b5132
RH
12411
12412 S_SET_EXTERNAL (symbolP);
12413 demand_empty_rest_of_line ();
12414}
12415
12416static void
12417s_option (x)
43841e91 12418 int x ATTRIBUTE_UNUSED;
252b5132
RH
12419{
12420 char *opt;
12421 char c;
12422
12423 opt = input_line_pointer;
12424 c = get_symbol_end ();
12425
12426 if (*opt == 'O')
12427 {
12428 /* FIXME: What does this mean? */
12429 }
12430 else if (strncmp (opt, "pic", 3) == 0)
12431 {
12432 int i;
12433
12434 i = atoi (opt + 3);
12435 if (i == 0)
12436 mips_pic = NO_PIC;
12437 else if (i == 2)
143d77c5 12438 {
252b5132 12439 mips_pic = SVR4_PIC;
143d77c5
EC
12440 mips_abicalls = TRUE;
12441 }
252b5132
RH
12442 else
12443 as_bad (_(".option pic%d not supported"), i);
12444
12445 if (USE_GLOBAL_POINTER_OPT && mips_pic == SVR4_PIC)
12446 {
12447 if (g_switch_seen && g_switch_value != 0)
12448 as_warn (_("-G may not be used with SVR4 PIC code"));
12449 g_switch_value = 0;
12450 bfd_set_gp_size (stdoutput, 0);
12451 }
12452 }
12453 else
12454 as_warn (_("Unrecognized option \"%s\""), opt);
12455
12456 *input_line_pointer = c;
12457 demand_empty_rest_of_line ();
12458}
12459
12460/* This structure is used to hold a stack of .set values. */
12461
e972090a
NC
12462struct mips_option_stack
12463{
252b5132
RH
12464 struct mips_option_stack *next;
12465 struct mips_set_options options;
12466};
12467
12468static struct mips_option_stack *mips_opts_stack;
12469
12470/* Handle the .set pseudo-op. */
12471
12472static void
12473s_mipsset (x)
43841e91 12474 int x ATTRIBUTE_UNUSED;
252b5132
RH
12475{
12476 char *name = input_line_pointer, ch;
12477
12478 while (!is_end_of_line[(unsigned char) *input_line_pointer])
f9419b05 12479 ++input_line_pointer;
252b5132
RH
12480 ch = *input_line_pointer;
12481 *input_line_pointer = '\0';
12482
12483 if (strcmp (name, "reorder") == 0)
12484 {
12485 if (mips_opts.noreorder && prev_nop_frag != NULL)
12486 {
12487 /* If we still have pending nops, we can discard them. The
12488 usual nop handling will insert any that are still
bdaaa2e1 12489 needed. */
252b5132
RH
12490 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
12491 * (mips_opts.mips16 ? 2 : 4));
12492 prev_nop_frag = NULL;
12493 }
12494 mips_opts.noreorder = 0;
12495 }
12496 else if (strcmp (name, "noreorder") == 0)
12497 {
b34976b6 12498 mips_emit_delays (TRUE);
252b5132
RH
12499 mips_opts.noreorder = 1;
12500 mips_any_noreorder = 1;
12501 }
12502 else if (strcmp (name, "at") == 0)
12503 {
12504 mips_opts.noat = 0;
12505 }
12506 else if (strcmp (name, "noat") == 0)
12507 {
12508 mips_opts.noat = 1;
12509 }
12510 else if (strcmp (name, "macro") == 0)
12511 {
12512 mips_opts.warn_about_macros = 0;
12513 }
12514 else if (strcmp (name, "nomacro") == 0)
12515 {
12516 if (mips_opts.noreorder == 0)
12517 as_bad (_("`noreorder' must be set before `nomacro'"));
12518 mips_opts.warn_about_macros = 1;
12519 }
12520 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
12521 {
12522 mips_opts.nomove = 0;
12523 }
12524 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
12525 {
12526 mips_opts.nomove = 1;
12527 }
12528 else if (strcmp (name, "bopt") == 0)
12529 {
12530 mips_opts.nobopt = 0;
12531 }
12532 else if (strcmp (name, "nobopt") == 0)
12533 {
12534 mips_opts.nobopt = 1;
12535 }
12536 else if (strcmp (name, "mips16") == 0
12537 || strcmp (name, "MIPS-16") == 0)
12538 mips_opts.mips16 = 1;
12539 else if (strcmp (name, "nomips16") == 0
12540 || strcmp (name, "noMIPS-16") == 0)
12541 mips_opts.mips16 = 0;
1f25f5d3
CD
12542 else if (strcmp (name, "mips3d") == 0)
12543 mips_opts.ase_mips3d = 1;
12544 else if (strcmp (name, "nomips3d") == 0)
12545 mips_opts.ase_mips3d = 0;
a4672219
TS
12546 else if (strcmp (name, "mdmx") == 0)
12547 mips_opts.ase_mdmx = 1;
12548 else if (strcmp (name, "nomdmx") == 0)
12549 mips_opts.ase_mdmx = 0;
252b5132
RH
12550 else if (strncmp (name, "mips", 4) == 0)
12551 {
af7ee8bf 12552 int reset = 0;
252b5132
RH
12553
12554 /* Permit the user to change the ISA on the fly. Needless to
12555 say, misuse can cause serious problems. */
af7ee8bf
CD
12556 if (strcmp (name, "mips0") == 0)
12557 {
12558 reset = 1;
12559 mips_opts.isa = file_mips_isa;
12560 }
12561 else if (strcmp (name, "mips1") == 0)
12562 mips_opts.isa = ISA_MIPS1;
12563 else if (strcmp (name, "mips2") == 0)
12564 mips_opts.isa = ISA_MIPS2;
12565 else if (strcmp (name, "mips3") == 0)
12566 mips_opts.isa = ISA_MIPS3;
12567 else if (strcmp (name, "mips4") == 0)
12568 mips_opts.isa = ISA_MIPS4;
12569 else if (strcmp (name, "mips5") == 0)
12570 mips_opts.isa = ISA_MIPS5;
12571 else if (strcmp (name, "mips32") == 0)
12572 mips_opts.isa = ISA_MIPS32;
12573 else if (strcmp (name, "mips32r2") == 0)
12574 mips_opts.isa = ISA_MIPS32R2;
12575 else if (strcmp (name, "mips64") == 0)
12576 mips_opts.isa = ISA_MIPS64;
12577 else
12578 as_bad (_("unknown ISA level %s"), name + 4);
12579
12580 switch (mips_opts.isa)
98d3f06f
KH
12581 {
12582 case 0:
98d3f06f 12583 break;
af7ee8bf
CD
12584 case ISA_MIPS1:
12585 case ISA_MIPS2:
12586 case ISA_MIPS32:
12587 case ISA_MIPS32R2:
98d3f06f
KH
12588 mips_opts.gp32 = 1;
12589 mips_opts.fp32 = 1;
12590 break;
af7ee8bf
CD
12591 case ISA_MIPS3:
12592 case ISA_MIPS4:
12593 case ISA_MIPS5:
12594 case ISA_MIPS64:
98d3f06f
KH
12595 mips_opts.gp32 = 0;
12596 mips_opts.fp32 = 0;
12597 break;
12598 default:
12599 as_bad (_("unknown ISA level %s"), name + 4);
12600 break;
12601 }
af7ee8bf 12602 if (reset)
98d3f06f 12603 {
af7ee8bf
CD
12604 mips_opts.gp32 = file_mips_gp32;
12605 mips_opts.fp32 = file_mips_fp32;
98d3f06f 12606 }
252b5132
RH
12607 }
12608 else if (strcmp (name, "autoextend") == 0)
12609 mips_opts.noautoextend = 0;
12610 else if (strcmp (name, "noautoextend") == 0)
12611 mips_opts.noautoextend = 1;
12612 else if (strcmp (name, "push") == 0)
12613 {
12614 struct mips_option_stack *s;
12615
12616 s = (struct mips_option_stack *) xmalloc (sizeof *s);
12617 s->next = mips_opts_stack;
12618 s->options = mips_opts;
12619 mips_opts_stack = s;
12620 }
12621 else if (strcmp (name, "pop") == 0)
12622 {
12623 struct mips_option_stack *s;
12624
12625 s = mips_opts_stack;
12626 if (s == NULL)
12627 as_bad (_(".set pop with no .set push"));
12628 else
12629 {
12630 /* If we're changing the reorder mode we need to handle
12631 delay slots correctly. */
12632 if (s->options.noreorder && ! mips_opts.noreorder)
b34976b6 12633 mips_emit_delays (TRUE);
252b5132
RH
12634 else if (! s->options.noreorder && mips_opts.noreorder)
12635 {
12636 if (prev_nop_frag != NULL)
12637 {
12638 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
12639 * (mips_opts.mips16 ? 2 : 4));
12640 prev_nop_frag = NULL;
12641 }
12642 }
12643
12644 mips_opts = s->options;
12645 mips_opts_stack = s->next;
12646 free (s);
12647 }
12648 }
12649 else
12650 {
12651 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
12652 }
12653 *input_line_pointer = ch;
12654 demand_empty_rest_of_line ();
12655}
12656
12657/* Handle the .abicalls pseudo-op. I believe this is equivalent to
12658 .option pic2. It means to generate SVR4 PIC calls. */
12659
12660static void
12661s_abicalls (ignore)
43841e91 12662 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
12663{
12664 mips_pic = SVR4_PIC;
143d77c5 12665 mips_abicalls = TRUE;
252b5132
RH
12666 if (USE_GLOBAL_POINTER_OPT)
12667 {
12668 if (g_switch_seen && g_switch_value != 0)
12669 as_warn (_("-G may not be used with SVR4 PIC code"));
12670 g_switch_value = 0;
12671 }
12672 bfd_set_gp_size (stdoutput, 0);
12673 demand_empty_rest_of_line ();
12674}
12675
12676/* Handle the .cpload pseudo-op. This is used when generating SVR4
12677 PIC code. It sets the $gp register for the function based on the
12678 function address, which is in the register named in the argument.
12679 This uses a relocation against _gp_disp, which is handled specially
12680 by the linker. The result is:
12681 lui $gp,%hi(_gp_disp)
12682 addiu $gp,$gp,%lo(_gp_disp)
12683 addu $gp,$gp,.cpload argument
12684 The .cpload argument is normally $25 == $t9. */
12685
12686static void
12687s_cpload (ignore)
43841e91 12688 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
12689{
12690 expressionS ex;
12691 int icnt = 0;
12692
6478892d
TS
12693 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12694 .cpload is ignored. */
12695 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
12696 {
12697 s_ignore (0);
12698 return;
12699 }
12700
d3ecfc59 12701 /* .cpload should be in a .set noreorder section. */
252b5132
RH
12702 if (mips_opts.noreorder == 0)
12703 as_warn (_(".cpload not in noreorder section"));
12704
12705 ex.X_op = O_symbol;
12706 ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
12707 ex.X_op_symbol = NULL;
12708 ex.X_add_number = 0;
12709
12710 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
49309057 12711 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
252b5132 12712
c9914766
TS
12713 macro_build_lui (NULL, &icnt, &ex, mips_gp_register);
12714 macro_build ((char *) NULL, &icnt, &ex, "addiu", "t,r,j",
12715 mips_gp_register, mips_gp_register, (int) BFD_RELOC_LO16);
252b5132
RH
12716
12717 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "addu", "d,v,t",
c9914766 12718 mips_gp_register, mips_gp_register, tc_get_register (0));
252b5132
RH
12719
12720 demand_empty_rest_of_line ();
12721}
12722
6478892d
TS
12723/* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
12724 .cpsetup $reg1, offset|$reg2, label
12725
12726 If offset is given, this results in:
12727 sd $gp, offset($sp)
956cd1d6 12728 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
12729 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
12730 daddu $gp, $gp, $reg1
6478892d
TS
12731
12732 If $reg2 is given, this results in:
12733 daddu $reg2, $gp, $0
956cd1d6 12734 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
12735 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
12736 daddu $gp, $gp, $reg1
12737 $reg1 is normally $25 == $t9. */
6478892d
TS
12738static void
12739s_cpsetup (ignore)
12740 int ignore ATTRIBUTE_UNUSED;
12741{
12742 expressionS ex_off;
12743 expressionS ex_sym;
12744 int reg1;
12745 int icnt = 0;
f21f8242 12746 char *f;
6478892d 12747
8586fc66 12748 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
6478892d
TS
12749 We also need NewABI support. */
12750 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12751 {
12752 s_ignore (0);
12753 return;
12754 }
12755
12756 reg1 = tc_get_register (0);
12757 SKIP_WHITESPACE ();
12758 if (*input_line_pointer != ',')
12759 {
12760 as_bad (_("missing argument separator ',' for .cpsetup"));
12761 return;
12762 }
12763 else
80245285 12764 ++input_line_pointer;
6478892d
TS
12765 SKIP_WHITESPACE ();
12766 if (*input_line_pointer == '$')
80245285
TS
12767 {
12768 mips_cpreturn_register = tc_get_register (0);
12769 mips_cpreturn_offset = -1;
12770 }
6478892d 12771 else
80245285
TS
12772 {
12773 mips_cpreturn_offset = get_absolute_expression ();
12774 mips_cpreturn_register = -1;
12775 }
6478892d
TS
12776 SKIP_WHITESPACE ();
12777 if (*input_line_pointer != ',')
12778 {
12779 as_bad (_("missing argument separator ',' for .cpsetup"));
12780 return;
12781 }
12782 else
f9419b05 12783 ++input_line_pointer;
6478892d 12784 SKIP_WHITESPACE ();
f21f8242 12785 expression (&ex_sym);
6478892d
TS
12786
12787 if (mips_cpreturn_register == -1)
12788 {
12789 ex_off.X_op = O_constant;
12790 ex_off.X_add_symbol = NULL;
12791 ex_off.X_op_symbol = NULL;
12792 ex_off.X_add_number = mips_cpreturn_offset;
12793
12794 macro_build ((char *) NULL, &icnt, &ex_off, "sd", "t,o(b)",
12795 mips_gp_register, (int) BFD_RELOC_LO16, SP);
12796 }
12797 else
12798 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
12799 "d,v,t", mips_cpreturn_register, mips_gp_register, 0);
12800
f21f8242
AO
12801 /* Ensure there's room for the next two instructions, so that `f'
12802 doesn't end up with an address in the wrong frag. */
12803 frag_grow (8);
12804 f = frag_more (0);
6478892d
TS
12805 macro_build ((char *) NULL, &icnt, &ex_sym, "lui", "t,u", mips_gp_register,
12806 (int) BFD_RELOC_GPREL16);
f21f8242
AO
12807 fix_new (frag_now, f - frag_now->fr_literal,
12808 0, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
12809 fix_new (frag_now, f - frag_now->fr_literal,
12810 0, NULL, 0, 0, BFD_RELOC_HI16_S);
12811
12812 f = frag_more (0);
6478892d
TS
12813 macro_build ((char *) NULL, &icnt, &ex_sym, "addiu", "t,r,j",
12814 mips_gp_register, mips_gp_register, (int) BFD_RELOC_GPREL16);
f21f8242
AO
12815 fix_new (frag_now, f - frag_now->fr_literal,
12816 0, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
12817 fix_new (frag_now, f - frag_now->fr_literal,
12818 0, NULL, 0, 0, BFD_RELOC_LO16);
12819
8586fc66 12820 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
f5040a92 12821 HAVE_64BIT_ADDRESSES ? "daddu" : "add", "d,v,t",
8586fc66 12822 mips_gp_register, mips_gp_register, reg1);
6478892d
TS
12823
12824 demand_empty_rest_of_line ();
12825}
12826
12827static void
12828s_cplocal (ignore)
12829 int ignore ATTRIBUTE_UNUSED;
12830{
12831 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
12832 .cplocal is ignored. */
12833 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12834 {
12835 s_ignore (0);
12836 return;
12837 }
12838
12839 mips_gp_register = tc_get_register (0);
85b51719 12840 demand_empty_rest_of_line ();
6478892d
TS
12841}
12842
252b5132
RH
12843/* Handle the .cprestore pseudo-op. This stores $gp into a given
12844 offset from $sp. The offset is remembered, and after making a PIC
12845 call $gp is restored from that location. */
12846
12847static void
12848s_cprestore (ignore)
43841e91 12849 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
12850{
12851 expressionS ex;
12852 int icnt = 0;
12853
6478892d 12854 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
c9914766 12855 .cprestore is ignored. */
6478892d 12856 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
12857 {
12858 s_ignore (0);
12859 return;
12860 }
12861
12862 mips_cprestore_offset = get_absolute_expression ();
7a621144 12863 mips_cprestore_valid = 1;
252b5132
RH
12864
12865 ex.X_op = O_constant;
12866 ex.X_add_symbol = NULL;
12867 ex.X_op_symbol = NULL;
12868 ex.X_add_number = mips_cprestore_offset;
12869
885add95
CD
12870 macro_build_ldst_constoffset ((char *) NULL, &icnt, &ex,
12871 HAVE_32BIT_ADDRESSES ? "sw" : "sd",
12872 mips_gp_register, SP);
252b5132
RH
12873
12874 demand_empty_rest_of_line ();
12875}
12876
6478892d
TS
12877/* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
12878 was given in the preceeding .gpsetup, it results in:
12879 ld $gp, offset($sp)
76b3015f 12880
6478892d
TS
12881 If a register $reg2 was given there, it results in:
12882 daddiu $gp, $gp, $reg2
12883 */
12884static void
12885s_cpreturn (ignore)
12886 int ignore ATTRIBUTE_UNUSED;
12887{
12888 expressionS ex;
12889 int icnt = 0;
12890
12891 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
12892 We also need NewABI support. */
12893 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12894 {
12895 s_ignore (0);
12896 return;
12897 }
12898
12899 if (mips_cpreturn_register == -1)
12900 {
12901 ex.X_op = O_constant;
12902 ex.X_add_symbol = NULL;
12903 ex.X_op_symbol = NULL;
12904 ex.X_add_number = mips_cpreturn_offset;
12905
12906 macro_build ((char *) NULL, &icnt, &ex, "ld", "t,o(b)",
12907 mips_gp_register, (int) BFD_RELOC_LO16, SP);
12908 }
12909 else
12910 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
12911 "d,v,t", mips_gp_register, mips_cpreturn_register, 0);
12912
12913 demand_empty_rest_of_line ();
12914}
12915
12916/* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
12917 code. It sets the offset to use in gp_rel relocations. */
12918
12919static void
12920s_gpvalue (ignore)
12921 int ignore ATTRIBUTE_UNUSED;
12922{
12923 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
12924 We also need NewABI support. */
12925 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12926 {
12927 s_ignore (0);
12928 return;
12929 }
12930
def2e0dd 12931 mips_gprel_offset = get_absolute_expression ();
6478892d
TS
12932
12933 demand_empty_rest_of_line ();
12934}
12935
252b5132
RH
12936/* Handle the .gpword pseudo-op. This is used when generating PIC
12937 code. It generates a 32 bit GP relative reloc. */
12938
12939static void
12940s_gpword (ignore)
43841e91 12941 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
12942{
12943 symbolS *label;
12944 expressionS ex;
12945 char *p;
12946
12947 /* When not generating PIC code, this is treated as .word. */
12948 if (mips_pic != SVR4_PIC)
12949 {
12950 s_cons (2);
12951 return;
12952 }
12953
12954 label = insn_labels != NULL ? insn_labels->label : NULL;
b34976b6 12955 mips_emit_delays (TRUE);
252b5132
RH
12956 if (auto_align)
12957 mips_align (2, 0, label);
12958 mips_clear_insn_labels ();
12959
12960 expression (&ex);
12961
12962 if (ex.X_op != O_symbol || ex.X_add_number != 0)
12963 {
12964 as_bad (_("Unsupported use of .gpword"));
12965 ignore_rest_of_line ();
12966 }
12967
12968 p = frag_more (4);
12969 md_number_to_chars (p, (valueT) 0, 4);
b34976b6 12970 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
cdf6fd85 12971 BFD_RELOC_GPREL32);
252b5132
RH
12972
12973 demand_empty_rest_of_line ();
12974}
12975
10181a0d
AO
12976static void
12977s_gpdword (ignore)
12978 int ignore ATTRIBUTE_UNUSED;
12979{
12980 symbolS *label;
12981 expressionS ex;
12982 char *p;
12983
12984 /* When not generating PIC code, this is treated as .dword. */
12985 if (mips_pic != SVR4_PIC)
12986 {
12987 s_cons (3);
12988 return;
12989 }
12990
12991 label = insn_labels != NULL ? insn_labels->label : NULL;
b34976b6 12992 mips_emit_delays (TRUE);
10181a0d
AO
12993 if (auto_align)
12994 mips_align (3, 0, label);
12995 mips_clear_insn_labels ();
12996
12997 expression (&ex);
12998
12999 if (ex.X_op != O_symbol || ex.X_add_number != 0)
13000 {
13001 as_bad (_("Unsupported use of .gpdword"));
13002 ignore_rest_of_line ();
13003 }
13004
13005 p = frag_more (8);
13006 md_number_to_chars (p, (valueT) 0, 8);
b34976b6 13007 fix_new_exp (frag_now, p - frag_now->fr_literal, 8, &ex, FALSE,
10181a0d
AO
13008 BFD_RELOC_GPREL32);
13009
13010 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
13011 ex.X_op = O_absent;
13012 ex.X_add_symbol = 0;
13013 ex.X_add_number = 0;
b34976b6 13014 fix_new_exp (frag_now, p - frag_now->fr_literal, 8, &ex, FALSE,
10181a0d
AO
13015 BFD_RELOC_64);
13016
13017 demand_empty_rest_of_line ();
13018}
13019
252b5132
RH
13020/* Handle the .cpadd pseudo-op. This is used when dealing with switch
13021 tables in SVR4 PIC code. */
13022
13023static void
13024s_cpadd (ignore)
43841e91 13025 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
13026{
13027 int icnt = 0;
13028 int reg;
13029
10181a0d
AO
13030 /* This is ignored when not generating SVR4 PIC code. */
13031 if (mips_pic != SVR4_PIC)
252b5132
RH
13032 {
13033 s_ignore (0);
13034 return;
13035 }
13036
13037 /* Add $gp to the register named as an argument. */
13038 reg = tc_get_register (0);
13039 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
f5040a92 13040 HAVE_32BIT_ADDRESSES ? HAVE_NEWABI ? "add" : "addu" : "daddu",
c9914766 13041 "d,v,t", reg, reg, mips_gp_register);
252b5132 13042
bdaaa2e1 13043 demand_empty_rest_of_line ();
252b5132
RH
13044}
13045
13046/* Handle the .insn pseudo-op. This marks instruction labels in
13047 mips16 mode. This permits the linker to handle them specially,
13048 such as generating jalx instructions when needed. We also make
13049 them odd for the duration of the assembly, in order to generate the
13050 right sort of code. We will make them even in the adjust_symtab
13051 routine, while leaving them marked. This is convenient for the
13052 debugger and the disassembler. The linker knows to make them odd
13053 again. */
13054
13055static void
13056s_insn (ignore)
43841e91 13057 int ignore ATTRIBUTE_UNUSED;
252b5132 13058{
f9419b05 13059 mips16_mark_labels ();
252b5132
RH
13060
13061 demand_empty_rest_of_line ();
13062}
13063
13064/* Handle a .stabn directive. We need these in order to mark a label
13065 as being a mips16 text label correctly. Sometimes the compiler
13066 will emit a label, followed by a .stabn, and then switch sections.
13067 If the label and .stabn are in mips16 mode, then the label is
13068 really a mips16 text label. */
13069
13070static void
13071s_mips_stab (type)
13072 int type;
13073{
f9419b05 13074 if (type == 'n')
252b5132
RH
13075 mips16_mark_labels ();
13076
13077 s_stab (type);
13078}
13079
13080/* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
13081 */
13082
13083static void
13084s_mips_weakext (ignore)
43841e91 13085 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
13086{
13087 char *name;
13088 int c;
13089 symbolS *symbolP;
13090 expressionS exp;
13091
13092 name = input_line_pointer;
13093 c = get_symbol_end ();
13094 symbolP = symbol_find_or_make (name);
13095 S_SET_WEAK (symbolP);
13096 *input_line_pointer = c;
13097
13098 SKIP_WHITESPACE ();
13099
13100 if (! is_end_of_line[(unsigned char) *input_line_pointer])
13101 {
13102 if (S_IS_DEFINED (symbolP))
13103 {
956cd1d6 13104 as_bad ("ignoring attempt to redefine symbol %s",
252b5132
RH
13105 S_GET_NAME (symbolP));
13106 ignore_rest_of_line ();
13107 return;
13108 }
bdaaa2e1 13109
252b5132
RH
13110 if (*input_line_pointer == ',')
13111 {
13112 ++input_line_pointer;
13113 SKIP_WHITESPACE ();
13114 }
bdaaa2e1 13115
252b5132
RH
13116 expression (&exp);
13117 if (exp.X_op != O_symbol)
13118 {
13119 as_bad ("bad .weakext directive");
98d3f06f 13120 ignore_rest_of_line ();
252b5132
RH
13121 return;
13122 }
49309057 13123 symbol_set_value_expression (symbolP, &exp);
252b5132
RH
13124 }
13125
13126 demand_empty_rest_of_line ();
13127}
13128
13129/* Parse a register string into a number. Called from the ECOFF code
13130 to parse .frame. The argument is non-zero if this is the frame
13131 register, so that we can record it in mips_frame_reg. */
13132
13133int
13134tc_get_register (frame)
13135 int frame;
13136{
13137 int reg;
13138
13139 SKIP_WHITESPACE ();
13140 if (*input_line_pointer++ != '$')
13141 {
13142 as_warn (_("expected `$'"));
85b51719 13143 reg = ZERO;
252b5132 13144 }
3882b010 13145 else if (ISDIGIT (*input_line_pointer))
252b5132
RH
13146 {
13147 reg = get_absolute_expression ();
13148 if (reg < 0 || reg >= 32)
13149 {
13150 as_warn (_("Bad register number"));
85b51719 13151 reg = ZERO;
252b5132
RH
13152 }
13153 }
13154 else
13155 {
76db943d 13156 if (strncmp (input_line_pointer, "ra", 2) == 0)
85b51719
TS
13157 {
13158 reg = RA;
13159 input_line_pointer += 2;
13160 }
76db943d 13161 else if (strncmp (input_line_pointer, "fp", 2) == 0)
85b51719
TS
13162 {
13163 reg = FP;
13164 input_line_pointer += 2;
13165 }
252b5132 13166 else if (strncmp (input_line_pointer, "sp", 2) == 0)
85b51719
TS
13167 {
13168 reg = SP;
13169 input_line_pointer += 2;
13170 }
252b5132 13171 else if (strncmp (input_line_pointer, "gp", 2) == 0)
85b51719
TS
13172 {
13173 reg = GP;
13174 input_line_pointer += 2;
13175 }
252b5132 13176 else if (strncmp (input_line_pointer, "at", 2) == 0)
85b51719
TS
13177 {
13178 reg = AT;
13179 input_line_pointer += 2;
13180 }
13181 else if (strncmp (input_line_pointer, "kt0", 3) == 0)
13182 {
13183 reg = KT0;
13184 input_line_pointer += 3;
13185 }
13186 else if (strncmp (input_line_pointer, "kt1", 3) == 0)
13187 {
13188 reg = KT1;
13189 input_line_pointer += 3;
13190 }
13191 else if (strncmp (input_line_pointer, "zero", 4) == 0)
13192 {
13193 reg = ZERO;
13194 input_line_pointer += 4;
13195 }
252b5132
RH
13196 else
13197 {
13198 as_warn (_("Unrecognized register name"));
85b51719
TS
13199 reg = ZERO;
13200 while (ISALNUM(*input_line_pointer))
13201 input_line_pointer++;
252b5132 13202 }
252b5132
RH
13203 }
13204 if (frame)
7a621144
DJ
13205 {
13206 mips_frame_reg = reg != 0 ? reg : SP;
13207 mips_frame_reg_valid = 1;
13208 mips_cprestore_valid = 0;
13209 }
252b5132
RH
13210 return reg;
13211}
13212
13213valueT
13214md_section_align (seg, addr)
13215 asection *seg;
13216 valueT addr;
13217{
13218 int align = bfd_get_section_alignment (stdoutput, seg);
13219
13220#ifdef OBJ_ELF
13221 /* We don't need to align ELF sections to the full alignment.
13222 However, Irix 5 may prefer that we align them at least to a 16
13223 byte boundary. We don't bother to align the sections if we are
13224 targeted for an embedded system. */
13225 if (strcmp (TARGET_OS, "elf") == 0)
13226 return addr;
13227 if (align > 4)
13228 align = 4;
13229#endif
13230
13231 return ((addr + (1 << align) - 1) & (-1 << align));
13232}
13233
13234/* Utility routine, called from above as well. If called while the
13235 input file is still being read, it's only an approximation. (For
13236 example, a symbol may later become defined which appeared to be
13237 undefined earlier.) */
13238
13239static int
13240nopic_need_relax (sym, before_relaxing)
13241 symbolS *sym;
13242 int before_relaxing;
13243{
13244 if (sym == 0)
13245 return 0;
13246
6478892d 13247 if (USE_GLOBAL_POINTER_OPT && g_switch_value > 0)
252b5132
RH
13248 {
13249 const char *symname;
13250 int change;
13251
c9914766 13252 /* Find out whether this symbol can be referenced off the $gp
252b5132
RH
13253 register. It can be if it is smaller than the -G size or if
13254 it is in the .sdata or .sbss section. Certain symbols can
c9914766 13255 not be referenced off the $gp, although it appears as though
252b5132
RH
13256 they can. */
13257 symname = S_GET_NAME (sym);
13258 if (symname != (const char *) NULL
13259 && (strcmp (symname, "eprol") == 0
13260 || strcmp (symname, "etext") == 0
13261 || strcmp (symname, "_gp") == 0
13262 || strcmp (symname, "edata") == 0
13263 || strcmp (symname, "_fbss") == 0
13264 || strcmp (symname, "_fdata") == 0
13265 || strcmp (symname, "_ftext") == 0
13266 || strcmp (symname, "end") == 0
13267 || strcmp (symname, "_gp_disp") == 0))
13268 change = 1;
13269 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
13270 && (0
13271#ifndef NO_ECOFF_DEBUGGING
49309057
ILT
13272 || (symbol_get_obj (sym)->ecoff_extern_size != 0
13273 && (symbol_get_obj (sym)->ecoff_extern_size
13274 <= g_switch_value))
252b5132
RH
13275#endif
13276 /* We must defer this decision until after the whole
13277 file has been read, since there might be a .extern
13278 after the first use of this symbol. */
13279 || (before_relaxing
13280#ifndef NO_ECOFF_DEBUGGING
49309057 13281 && symbol_get_obj (sym)->ecoff_extern_size == 0
252b5132
RH
13282#endif
13283 && S_GET_VALUE (sym) == 0)
13284 || (S_GET_VALUE (sym) != 0
13285 && S_GET_VALUE (sym) <= g_switch_value)))
13286 change = 0;
13287 else
13288 {
13289 const char *segname;
13290
13291 segname = segment_name (S_GET_SEGMENT (sym));
13292 assert (strcmp (segname, ".lit8") != 0
13293 && strcmp (segname, ".lit4") != 0);
13294 change = (strcmp (segname, ".sdata") != 0
fba2b7f9
GK
13295 && strcmp (segname, ".sbss") != 0
13296 && strncmp (segname, ".sdata.", 7) != 0
13297 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
252b5132
RH
13298 }
13299 return change;
13300 }
13301 else
c9914766 13302 /* We are not optimizing for the $gp register. */
252b5132
RH
13303 return 1;
13304}
13305
5919d012
RS
13306
13307/* Return true if the given symbol should be considered local for SVR4 PIC. */
13308
13309static bfd_boolean
13310pic_need_relax (sym, segtype)
13311 symbolS *sym;
13312 asection *segtype;
13313{
13314 asection *symsec;
13315 bfd_boolean linkonce;
13316
13317 /* Handle the case of a symbol equated to another symbol. */
13318 while (symbol_equated_reloc_p (sym))
13319 {
13320 symbolS *n;
13321
13322 /* It's possible to get a loop here in a badly written
13323 program. */
13324 n = symbol_get_value_expression (sym)->X_add_symbol;
13325 if (n == sym)
13326 break;
13327 sym = n;
13328 }
13329
13330 symsec = S_GET_SEGMENT (sym);
13331
13332 /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
13333 linkonce = FALSE;
13334 if (symsec != segtype && ! S_IS_LOCAL (sym))
13335 {
13336 if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
13337 != 0)
13338 linkonce = TRUE;
13339
13340 /* The GNU toolchain uses an extension for ELF: a section
13341 beginning with the magic string .gnu.linkonce is a linkonce
13342 section. */
13343 if (strncmp (segment_name (symsec), ".gnu.linkonce",
13344 sizeof ".gnu.linkonce" - 1) == 0)
13345 linkonce = TRUE;
13346 }
13347
13348 /* This must duplicate the test in adjust_reloc_syms. */
13349 return (symsec != &bfd_und_section
13350 && symsec != &bfd_abs_section
13351 && ! bfd_is_com_section (symsec)
13352 && !linkonce
13353#ifdef OBJ_ELF
13354 /* A global or weak symbol is treated as external. */
13355 && (OUTPUT_FLAVOR != bfd_target_elf_flavour
13356 || (! S_IS_WEAK (sym)
13357 && (! S_IS_EXTERNAL (sym)
13358 || mips_pic == EMBEDDED_PIC)))
13359#endif
13360 );
13361}
13362
13363
252b5132
RH
13364/* Given a mips16 variant frag FRAGP, return non-zero if it needs an
13365 extended opcode. SEC is the section the frag is in. */
13366
13367static int
13368mips16_extended_frag (fragp, sec, stretch)
13369 fragS *fragp;
13370 asection *sec;
13371 long stretch;
13372{
13373 int type;
13374 register const struct mips16_immed_operand *op;
13375 offsetT val;
13376 int mintiny, maxtiny;
13377 segT symsec;
98aa84af 13378 fragS *sym_frag;
252b5132
RH
13379
13380 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
13381 return 0;
13382 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
13383 return 1;
13384
13385 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
13386 op = mips16_immed_operands;
13387 while (op->type != type)
13388 {
13389 ++op;
13390 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
13391 }
13392
13393 if (op->unsp)
13394 {
13395 if (type == '<' || type == '>' || type == '[' || type == ']')
13396 {
13397 mintiny = 1;
13398 maxtiny = 1 << op->nbits;
13399 }
13400 else
13401 {
13402 mintiny = 0;
13403 maxtiny = (1 << op->nbits) - 1;
13404 }
13405 }
13406 else
13407 {
13408 mintiny = - (1 << (op->nbits - 1));
13409 maxtiny = (1 << (op->nbits - 1)) - 1;
13410 }
13411
98aa84af 13412 sym_frag = symbol_get_frag (fragp->fr_symbol);
ac62c346 13413 val = S_GET_VALUE (fragp->fr_symbol);
98aa84af 13414 symsec = S_GET_SEGMENT (fragp->fr_symbol);
252b5132
RH
13415
13416 if (op->pcrel)
13417 {
13418 addressT addr;
13419
13420 /* We won't have the section when we are called from
13421 mips_relax_frag. However, we will always have been called
13422 from md_estimate_size_before_relax first. If this is a
13423 branch to a different section, we mark it as such. If SEC is
13424 NULL, and the frag is not marked, then it must be a branch to
13425 the same section. */
13426 if (sec == NULL)
13427 {
13428 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
13429 return 1;
13430 }
13431 else
13432 {
98aa84af 13433 /* Must have been called from md_estimate_size_before_relax. */
252b5132
RH
13434 if (symsec != sec)
13435 {
13436 fragp->fr_subtype =
13437 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13438
13439 /* FIXME: We should support this, and let the linker
13440 catch branches and loads that are out of range. */
13441 as_bad_where (fragp->fr_file, fragp->fr_line,
13442 _("unsupported PC relative reference to different section"));
13443
13444 return 1;
13445 }
98aa84af
AM
13446 if (fragp != sym_frag && sym_frag->fr_address == 0)
13447 /* Assume non-extended on the first relaxation pass.
13448 The address we have calculated will be bogus if this is
13449 a forward branch to another frag, as the forward frag
13450 will have fr_address == 0. */
13451 return 0;
252b5132
RH
13452 }
13453
13454 /* In this case, we know for sure that the symbol fragment is in
98aa84af
AM
13455 the same section. If the relax_marker of the symbol fragment
13456 differs from the relax_marker of this fragment, we have not
13457 yet adjusted the symbol fragment fr_address. We want to add
252b5132
RH
13458 in STRETCH in order to get a better estimate of the address.
13459 This particularly matters because of the shift bits. */
13460 if (stretch != 0
98aa84af 13461 && sym_frag->relax_marker != fragp->relax_marker)
252b5132
RH
13462 {
13463 fragS *f;
13464
13465 /* Adjust stretch for any alignment frag. Note that if have
13466 been expanding the earlier code, the symbol may be
13467 defined in what appears to be an earlier frag. FIXME:
13468 This doesn't handle the fr_subtype field, which specifies
13469 a maximum number of bytes to skip when doing an
13470 alignment. */
98aa84af 13471 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
252b5132
RH
13472 {
13473 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
13474 {
13475 if (stretch < 0)
13476 stretch = - ((- stretch)
13477 & ~ ((1 << (int) f->fr_offset) - 1));
13478 else
13479 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
13480 if (stretch == 0)
13481 break;
13482 }
13483 }
13484 if (f != NULL)
13485 val += stretch;
13486 }
13487
13488 addr = fragp->fr_address + fragp->fr_fix;
13489
13490 /* The base address rules are complicated. The base address of
13491 a branch is the following instruction. The base address of a
13492 PC relative load or add is the instruction itself, but if it
13493 is in a delay slot (in which case it can not be extended) use
13494 the address of the instruction whose delay slot it is in. */
13495 if (type == 'p' || type == 'q')
13496 {
13497 addr += 2;
13498
13499 /* If we are currently assuming that this frag should be
13500 extended, then, the current address is two bytes
bdaaa2e1 13501 higher. */
252b5132
RH
13502 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13503 addr += 2;
13504
13505 /* Ignore the low bit in the target, since it will be set
13506 for a text label. */
13507 if ((val & 1) != 0)
13508 --val;
13509 }
13510 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13511 addr -= 4;
13512 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13513 addr -= 2;
13514
13515 val -= addr & ~ ((1 << op->shift) - 1);
13516
13517 /* Branch offsets have an implicit 0 in the lowest bit. */
13518 if (type == 'p' || type == 'q')
13519 val /= 2;
13520
13521 /* If any of the shifted bits are set, we must use an extended
13522 opcode. If the address depends on the size of this
13523 instruction, this can lead to a loop, so we arrange to always
13524 use an extended opcode. We only check this when we are in
13525 the main relaxation loop, when SEC is NULL. */
13526 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
13527 {
13528 fragp->fr_subtype =
13529 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13530 return 1;
13531 }
13532
13533 /* If we are about to mark a frag as extended because the value
13534 is precisely maxtiny + 1, then there is a chance of an
13535 infinite loop as in the following code:
13536 la $4,foo
13537 .skip 1020
13538 .align 2
13539 foo:
13540 In this case when the la is extended, foo is 0x3fc bytes
13541 away, so the la can be shrunk, but then foo is 0x400 away, so
13542 the la must be extended. To avoid this loop, we mark the
13543 frag as extended if it was small, and is about to become
13544 extended with a value of maxtiny + 1. */
13545 if (val == ((maxtiny + 1) << op->shift)
13546 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
13547 && sec == NULL)
13548 {
13549 fragp->fr_subtype =
13550 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
13551 return 1;
13552 }
13553 }
13554 else if (symsec != absolute_section && sec != NULL)
13555 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
13556
13557 if ((val & ((1 << op->shift) - 1)) != 0
13558 || val < (mintiny << op->shift)
13559 || val > (maxtiny << op->shift))
13560 return 1;
13561 else
13562 return 0;
13563}
13564
4a6a3df4
AO
13565/* Compute the length of a branch sequence, and adjust the
13566 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
13567 worst-case length is computed, with UPDATE being used to indicate
13568 whether an unconditional (-1), branch-likely (+1) or regular (0)
13569 branch is to be computed. */
13570static int
13571relaxed_branch_length (fragp, sec, update)
13572 fragS *fragp;
13573 asection *sec;
13574 int update;
13575{
b34976b6 13576 bfd_boolean toofar;
4a6a3df4
AO
13577 int length;
13578
13579 if (fragp
13580 && S_IS_DEFINED (fragp->fr_symbol)
13581 && sec == S_GET_SEGMENT (fragp->fr_symbol))
13582 {
13583 addressT addr;
13584 offsetT val;
13585
13586 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
13587
13588 addr = fragp->fr_address + fragp->fr_fix + 4;
13589
13590 val -= addr;
13591
13592 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
13593 }
13594 else if (fragp)
13595 /* If the symbol is not defined or it's in a different segment,
13596 assume the user knows what's going on and emit a short
13597 branch. */
b34976b6 13598 toofar = FALSE;
4a6a3df4 13599 else
b34976b6 13600 toofar = TRUE;
4a6a3df4
AO
13601
13602 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13603 fragp->fr_subtype
af6ae2ad 13604 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_UNCOND (fragp->fr_subtype),
4a6a3df4
AO
13605 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
13606 RELAX_BRANCH_LINK (fragp->fr_subtype),
13607 toofar);
13608
13609 length = 4;
13610 if (toofar)
13611 {
13612 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
13613 length += 8;
13614
13615 if (mips_pic != NO_PIC)
13616 {
13617 /* Additional space for PIC loading of target address. */
13618 length += 8;
13619 if (mips_opts.isa == ISA_MIPS1)
13620 /* Additional space for $at-stabilizing nop. */
13621 length += 4;
13622 }
13623
13624 /* If branch is conditional. */
13625 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
13626 length += 8;
13627 }
b34976b6 13628
4a6a3df4
AO
13629 return length;
13630}
13631
252b5132
RH
13632/* Estimate the size of a frag before relaxing. Unless this is the
13633 mips16, we are not really relaxing here, and the final size is
13634 encoded in the subtype information. For the mips16, we have to
13635 decide whether we are using an extended opcode or not. */
13636
252b5132
RH
13637int
13638md_estimate_size_before_relax (fragp, segtype)
13639 fragS *fragp;
13640 asection *segtype;
13641{
5919d012 13642 int change;
252b5132 13643
4a6a3df4
AO
13644 if (RELAX_BRANCH_P (fragp->fr_subtype))
13645 {
13646
b34976b6
AM
13647 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
13648
4a6a3df4
AO
13649 return fragp->fr_var;
13650 }
13651
252b5132 13652 if (RELAX_MIPS16_P (fragp->fr_subtype))
177b4a6a
AO
13653 /* We don't want to modify the EXTENDED bit here; it might get us
13654 into infinite loops. We change it only in mips_relax_frag(). */
13655 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
252b5132
RH
13656
13657 if (mips_pic == NO_PIC)
5919d012 13658 change = nopic_need_relax (fragp->fr_symbol, 0);
252b5132 13659 else if (mips_pic == SVR4_PIC)
5919d012 13660 change = pic_need_relax (fragp->fr_symbol, segtype);
252b5132
RH
13661 else
13662 abort ();
13663
13664 if (change)
13665 {
13666 /* Record the offset to the first reloc in the fr_opcode field.
13667 This lets md_convert_frag and tc_gen_reloc know that the code
13668 must be expanded. */
13669 fragp->fr_opcode = (fragp->fr_literal
13670 + fragp->fr_fix
13671 - RELAX_OLD (fragp->fr_subtype)
13672 + RELAX_RELOC1 (fragp->fr_subtype));
13673 /* FIXME: This really needs as_warn_where. */
13674 if (RELAX_WARN (fragp->fr_subtype))
9a41af64
TS
13675 as_warn (_("AT used after \".set noat\" or macro used after "
13676 "\".set nomacro\""));
13677
13678 return RELAX_NEW (fragp->fr_subtype) - RELAX_OLD (fragp->fr_subtype);
252b5132
RH
13679 }
13680
9a41af64 13681 return 0;
252b5132
RH
13682}
13683
13684/* This is called to see whether a reloc against a defined symbol
13685 should be converted into a reloc against a section. Don't adjust
13686 MIPS16 jump relocations, so we don't have to worry about the format
13687 of the offset in the .o file. Don't adjust relocations against
13688 mips16 symbols, so that the linker can find them if it needs to set
13689 up a stub. */
13690
13691int
13692mips_fix_adjustable (fixp)
13693 fixS *fixp;
13694{
13695 if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
13696 return 0;
a161fe53 13697
252b5132
RH
13698 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
13699 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13700 return 0;
a161fe53 13701
252b5132
RH
13702 if (fixp->fx_addsy == NULL)
13703 return 1;
a161fe53 13704
252b5132
RH
13705#ifdef OBJ_ELF
13706 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
13707 && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
13708 && fixp->fx_subsy == NULL)
13709 return 0;
13710#endif
a161fe53 13711
252b5132
RH
13712 return 1;
13713}
13714
13715/* Translate internal representation of relocation info to BFD target
13716 format. */
13717
13718arelent **
13719tc_gen_reloc (section, fixp)
43841e91 13720 asection *section ATTRIBUTE_UNUSED;
252b5132
RH
13721 fixS *fixp;
13722{
13723 static arelent *retval[4];
13724 arelent *reloc;
13725 bfd_reloc_code_real_type code;
13726
13727 reloc = retval[0] = (arelent *) xmalloc (sizeof (arelent));
13728 retval[1] = NULL;
13729
49309057
ILT
13730 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13731 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
13732 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
13733
13734 if (mips_pic == EMBEDDED_PIC
13735 && SWITCH_TABLE (fixp))
13736 {
13737 /* For a switch table entry we use a special reloc. The addend
13738 is actually the difference between the reloc address and the
13739 subtrahend. */
13740 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
13741 if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
13742 as_fatal (_("Double check fx_r_type in tc-mips.c:tc_gen_reloc"));
13743 fixp->fx_r_type = BFD_RELOC_GPREL32;
13744 }
13745 else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16)
13746 {
4514d474
CD
13747 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
13748 reloc->addend = fixp->fx_addnumber;
252b5132 13749 else
4514d474
CD
13750 {
13751 /* We use a special addend for an internal RELLO reloc. */
13752 if (symbol_section_p (fixp->fx_addsy))
13753 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
13754 else
13755 reloc->addend = fixp->fx_addnumber + reloc->address;
13756 }
252b5132
RH
13757 }
13758 else if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
13759 {
13760 assert (fixp->fx_next != NULL
13761 && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
4514d474
CD
13762
13763 /* The reloc is relative to the RELLO; adjust the addend
252b5132 13764 accordingly. */
4514d474
CD
13765 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
13766 reloc->addend = fixp->fx_next->fx_addnumber;
252b5132 13767 else
4514d474
CD
13768 {
13769 /* We use a special addend for an internal RELHI reloc. */
13770 if (symbol_section_p (fixp->fx_addsy))
13771 reloc->addend = (fixp->fx_next->fx_frag->fr_address
13772 + fixp->fx_next->fx_where
13773 - S_GET_VALUE (fixp->fx_subsy));
13774 else
13775 reloc->addend = (fixp->fx_addnumber
13776 + fixp->fx_next->fx_frag->fr_address
13777 + fixp->fx_next->fx_where);
13778 }
252b5132 13779 }
4514d474
CD
13780 else if (fixp->fx_pcrel == 0 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
13781 reloc->addend = fixp->fx_addnumber;
252b5132
RH
13782 else
13783 {
13784 if (OUTPUT_FLAVOR != bfd_target_aout_flavour)
13785 /* A gruesome hack which is a result of the gruesome gas reloc
13786 handling. */
13787 reloc->addend = reloc->address;
13788 else
13789 reloc->addend = -reloc->address;
13790 }
13791
13792 /* If this is a variant frag, we may need to adjust the existing
13793 reloc and generate a new one. */
13794 if (fixp->fx_frag->fr_opcode != NULL
ed6fb7bd
SC
13795 && ((fixp->fx_r_type == BFD_RELOC_GPREL16
13796 && ! HAVE_NEWABI)
f5040a92
AO
13797 || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_DISP
13798 && HAVE_NEWABI)
252b5132
RH
13799 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
13800 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL16
13801 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
13802 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16
13803 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
6478892d 13804 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_LO16)
ed6fb7bd 13805 )
252b5132
RH
13806 {
13807 arelent *reloc2;
13808
13809 assert (! RELAX_MIPS16_P (fixp->fx_frag->fr_subtype));
13810
13811 /* If this is not the last reloc in this frag, then we have two
13812 GPREL relocs, or a GOT_HI16/GOT_LO16 pair, or a
13813 CALL_HI16/CALL_LO16, both of which are being replaced. Let
13814 the second one handle all of them. */
13815 if (fixp->fx_next != NULL
13816 && fixp->fx_frag == fixp->fx_next->fx_frag)
13817 {
cdf6fd85
TS
13818 assert ((fixp->fx_r_type == BFD_RELOC_GPREL16
13819 && fixp->fx_next->fx_r_type == BFD_RELOC_GPREL16)
252b5132
RH
13820 || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
13821 && (fixp->fx_next->fx_r_type
13822 == BFD_RELOC_MIPS_GOT_LO16))
13823 || (fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
13824 && (fixp->fx_next->fx_r_type
13825 == BFD_RELOC_MIPS_CALL_LO16)));
13826 retval[0] = NULL;
13827 return retval;
13828 }
13829
13830 fixp->fx_where = fixp->fx_frag->fr_opcode - fixp->fx_frag->fr_literal;
13831 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
f5040a92 13832 reloc->addend += fixp->fx_frag->tc_frag_data.tc_fr_offset;
252b5132
RH
13833 reloc2 = retval[1] = (arelent *) xmalloc (sizeof (arelent));
13834 retval[2] = NULL;
49309057
ILT
13835 reloc2->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13836 *reloc2->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
13837 reloc2->address = (reloc->address
13838 + (RELAX_RELOC2 (fixp->fx_frag->fr_subtype)
13839 - RELAX_RELOC1 (fixp->fx_frag->fr_subtype)));
f5040a92
AO
13840 reloc2->addend = fixp->fx_addnumber
13841 + fixp->fx_frag->tc_frag_data.tc_fr_offset;
252b5132
RH
13842 reloc2->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO16);
13843 assert (reloc2->howto != NULL);
13844
13845 if (RELAX_RELOC3 (fixp->fx_frag->fr_subtype))
13846 {
13847 arelent *reloc3;
13848
13849 reloc3 = retval[2] = (arelent *) xmalloc (sizeof (arelent));
13850 retval[3] = NULL;
13851 *reloc3 = *reloc2;
13852 reloc3->address += 4;
13853 }
13854
13855 if (mips_pic == NO_PIC)
13856 {
cdf6fd85 13857 assert (fixp->fx_r_type == BFD_RELOC_GPREL16);
252b5132
RH
13858 fixp->fx_r_type = BFD_RELOC_HI16_S;
13859 }
13860 else if (mips_pic == SVR4_PIC)
13861 {
13862 switch (fixp->fx_r_type)
13863 {
13864 default:
13865 abort ();
13866 case BFD_RELOC_MIPS_GOT16:
13867 break;
252b5132
RH
13868 case BFD_RELOC_MIPS_GOT_LO16:
13869 case BFD_RELOC_MIPS_CALL_LO16:
ed6fb7bd
SC
13870 if (HAVE_NEWABI)
13871 {
ed6fb7bd
SC
13872 fixp->fx_r_type = BFD_RELOC_MIPS_GOT_PAGE;
13873 reloc2->howto = bfd_reloc_type_lookup
13874 (stdoutput, BFD_RELOC_MIPS_GOT_OFST);
13875 }
13876 else
13877 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
13878 break;
f5040a92
AO
13879 case BFD_RELOC_MIPS_CALL16:
13880 case BFD_RELOC_MIPS_GOT_OFST:
13881 case BFD_RELOC_MIPS_GOT_DISP:
13882 if (HAVE_NEWABI)
13883 {
13884 /* It may seem nonsensical to relax GOT_DISP to
13885 GOT_DISP, but we're actually turning a GOT_DISP
13886 without offset into a GOT_DISP with an offset,
13887 getting rid of the separate addition, which we can
13888 do when the symbol is found to be local. */
13889 fixp->fx_r_type = BFD_RELOC_MIPS_GOT_DISP;
13890 retval[1] = NULL;
13891 }
13892 else
13893 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
13894 break;
252b5132
RH
13895 }
13896 }
13897 else
13898 abort ();
13899 }
13900
438c16b8
TS
13901 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
13902 entry to be used in the relocation's section offset. */
13903 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
252b5132
RH
13904 {
13905 reloc->address = reloc->addend;
13906 reloc->addend = 0;
13907 }
13908
13909 /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
13910 fixup_segment converted a non-PC relative reloc into a PC
13911 relative reloc. In such a case, we need to convert the reloc
13912 code. */
13913 code = fixp->fx_r_type;
13914 if (fixp->fx_pcrel)
13915 {
13916 switch (code)
13917 {
13918 case BFD_RELOC_8:
13919 code = BFD_RELOC_8_PCREL;
13920 break;
13921 case BFD_RELOC_16:
13922 code = BFD_RELOC_16_PCREL;
13923 break;
13924 case BFD_RELOC_32:
13925 code = BFD_RELOC_32_PCREL;
13926 break;
13927 case BFD_RELOC_64:
13928 code = BFD_RELOC_64_PCREL;
13929 break;
13930 case BFD_RELOC_8_PCREL:
13931 case BFD_RELOC_16_PCREL:
13932 case BFD_RELOC_32_PCREL:
13933 case BFD_RELOC_64_PCREL:
13934 case BFD_RELOC_16_PCREL_S2:
13935 case BFD_RELOC_PCREL_HI16_S:
13936 case BFD_RELOC_PCREL_LO16:
13937 break;
13938 default:
13939 as_bad_where (fixp->fx_file, fixp->fx_line,
13940 _("Cannot make %s relocation PC relative"),
13941 bfd_get_reloc_code_name (code));
13942 }
13943 }
13944
add55e1f
RS
13945#ifdef OBJ_ELF
13946 /* md_apply_fix3 has a double-subtraction hack to get
13947 bfd_install_relocation to behave nicely. GPREL relocations are
13948 handled correctly without this hack, so undo it here. We can't
13949 stop md_apply_fix3 from subtracting twice in the first place since
13950 the fake addend is required for variant frags above. */
13951 if (fixp->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour
98605598 13952 && (code == BFD_RELOC_GPREL16 || code == BFD_RELOC_MIPS16_GPREL)
add55e1f
RS
13953 && reloc->addend != 0
13954 && mips_need_elf_addend_fixup (fixp))
d8dbbec1
L
13955 {
13956 /* If howto->partial_inplace is false, md_apply_fix3 will only
13957 subtract it once. */
13958 reloc_howto_type *howto;
13959
13960 howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
13961 if (howto->partial_inplace)
13962 reloc->addend += S_GET_VALUE (fixp->fx_addsy);
13963 }
add55e1f
RS
13964#endif
13965
0b25d3e6
AO
13966 /* To support a PC relative reloc when generating embedded PIC code
13967 for ECOFF, we use a Cygnus extension. We check for that here to
13968 make sure that we don't let such a reloc escape normally. */
13969 if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
13970 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
13971 && code == BFD_RELOC_16_PCREL_S2
13972 && mips_pic != EMBEDDED_PIC)
13973 reloc->howto = NULL;
13974 else
13975 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
13976
252b5132
RH
13977 if (reloc->howto == NULL)
13978 {
13979 as_bad_where (fixp->fx_file, fixp->fx_line,
13980 _("Can not represent %s relocation in this object file format"),
13981 bfd_get_reloc_code_name (code));
13982 retval[0] = NULL;
13983 }
13984
13985 return retval;
13986}
13987
13988/* Relax a machine dependent frag. This returns the amount by which
13989 the current size of the frag should change. */
13990
13991int
4a6a3df4
AO
13992mips_relax_frag (sec, fragp, stretch)
13993 asection *sec;
252b5132
RH
13994 fragS *fragp;
13995 long stretch;
13996{
4a6a3df4
AO
13997 if (RELAX_BRANCH_P (fragp->fr_subtype))
13998 {
13999 offsetT old_var = fragp->fr_var;
b34976b6
AM
14000
14001 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
4a6a3df4
AO
14002
14003 return fragp->fr_var - old_var;
14004 }
14005
252b5132
RH
14006 if (! RELAX_MIPS16_P (fragp->fr_subtype))
14007 return 0;
14008
c4e7957c 14009 if (mips16_extended_frag (fragp, NULL, stretch))
252b5132
RH
14010 {
14011 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
14012 return 0;
14013 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
14014 return 2;
14015 }
14016 else
14017 {
14018 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
14019 return 0;
14020 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
14021 return -2;
14022 }
14023
14024 return 0;
14025}
14026
14027/* Convert a machine dependent frag. */
14028
14029void
14030md_convert_frag (abfd, asec, fragp)
43841e91 14031 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
14032 segT asec;
14033 fragS *fragp;
14034{
14035 int old, new;
14036 char *fixptr;
14037
4a6a3df4
AO
14038 if (RELAX_BRANCH_P (fragp->fr_subtype))
14039 {
14040 bfd_byte *buf;
14041 unsigned long insn;
14042 expressionS exp;
14043 fixS *fixp;
b34976b6 14044
4a6a3df4
AO
14045 buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
14046
14047 if (target_big_endian)
14048 insn = bfd_getb32 (buf);
14049 else
14050 insn = bfd_getl32 (buf);
b34976b6 14051
4a6a3df4
AO
14052 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
14053 {
14054 /* We generate a fixup instead of applying it right now
14055 because, if there are linker relaxations, we're going to
14056 need the relocations. */
14057 exp.X_op = O_symbol;
14058 exp.X_add_symbol = fragp->fr_symbol;
14059 exp.X_add_number = fragp->fr_offset;
14060
14061 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
0b25d3e6
AO
14062 4, &exp, 1,
14063 BFD_RELOC_16_PCREL_S2);
4a6a3df4
AO
14064 fixp->fx_file = fragp->fr_file;
14065 fixp->fx_line = fragp->fr_line;
b34976b6 14066
4a6a3df4
AO
14067 md_number_to_chars ((char *)buf, insn, 4);
14068 buf += 4;
14069 }
14070 else
14071 {
14072 int i;
14073
14074 as_warn_where (fragp->fr_file, fragp->fr_line,
14075 _("relaxed out-of-range branch into a jump"));
14076
14077 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
14078 goto uncond;
14079
14080 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
14081 {
14082 /* Reverse the branch. */
14083 switch ((insn >> 28) & 0xf)
14084 {
14085 case 4:
14086 /* bc[0-3][tf]l? and bc1any[24][ft] instructions can
14087 have the condition reversed by tweaking a single
14088 bit, and their opcodes all have 0x4???????. */
14089 assert ((insn & 0xf1000000) == 0x41000000);
14090 insn ^= 0x00010000;
14091 break;
14092
14093 case 0:
14094 /* bltz 0x04000000 bgez 0x04010000
14095 bltzal 0x04100000 bgezal 0x04110000 */
14096 assert ((insn & 0xfc0e0000) == 0x04000000);
14097 insn ^= 0x00010000;
14098 break;
b34976b6 14099
4a6a3df4
AO
14100 case 1:
14101 /* beq 0x10000000 bne 0x14000000
14102 blez 0x18000000 bgtz 0x1c000000 */
14103 insn ^= 0x04000000;
14104 break;
14105
14106 default:
14107 abort ();
14108 }
14109 }
14110
14111 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
14112 {
14113 /* Clear the and-link bit. */
14114 assert ((insn & 0xfc1c0000) == 0x04100000);
14115
14116 /* bltzal 0x04100000 bgezal 0x04110000
14117 bltzall 0x04120000 bgezall 0x04130000 */
14118 insn &= ~0x00100000;
14119 }
14120
14121 /* Branch over the branch (if the branch was likely) or the
14122 full jump (not likely case). Compute the offset from the
14123 current instruction to branch to. */
14124 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
14125 i = 16;
14126 else
14127 {
14128 /* How many bytes in instructions we've already emitted? */
14129 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
14130 /* How many bytes in instructions from here to the end? */
14131 i = fragp->fr_var - i;
14132 }
14133 /* Convert to instruction count. */
14134 i >>= 2;
14135 /* Branch counts from the next instruction. */
b34976b6 14136 i--;
4a6a3df4
AO
14137 insn |= i;
14138 /* Branch over the jump. */
14139 md_number_to_chars ((char *)buf, insn, 4);
14140 buf += 4;
14141
14142 /* Nop */
14143 md_number_to_chars ((char*)buf, 0, 4);
14144 buf += 4;
14145
14146 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
14147 {
14148 /* beql $0, $0, 2f */
14149 insn = 0x50000000;
14150 /* Compute the PC offset from the current instruction to
14151 the end of the variable frag. */
14152 /* How many bytes in instructions we've already emitted? */
14153 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
14154 /* How many bytes in instructions from here to the end? */
14155 i = fragp->fr_var - i;
14156 /* Convert to instruction count. */
14157 i >>= 2;
14158 /* Don't decrement i, because we want to branch over the
14159 delay slot. */
14160
14161 insn |= i;
14162 md_number_to_chars ((char *)buf, insn, 4);
14163 buf += 4;
14164
14165 md_number_to_chars ((char *)buf, 0, 4);
14166 buf += 4;
14167 }
14168
14169 uncond:
14170 if (mips_pic == NO_PIC)
14171 {
14172 /* j or jal. */
14173 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
14174 ? 0x0c000000 : 0x08000000);
14175 exp.X_op = O_symbol;
14176 exp.X_add_symbol = fragp->fr_symbol;
14177 exp.X_add_number = fragp->fr_offset;
14178
14179 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
14180 4, &exp, 0, BFD_RELOC_MIPS_JMP);
14181 fixp->fx_file = fragp->fr_file;
14182 fixp->fx_line = fragp->fr_line;
14183
14184 md_number_to_chars ((char*)buf, insn, 4);
14185 buf += 4;
14186 }
14187 else
14188 {
14189 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
14190 insn = HAVE_64BIT_ADDRESSES ? 0xdf810000 : 0x8f810000;
14191 exp.X_op = O_symbol;
14192 exp.X_add_symbol = fragp->fr_symbol;
14193 exp.X_add_number = fragp->fr_offset;
14194
14195 if (fragp->fr_offset)
14196 {
14197 exp.X_add_symbol = make_expr_symbol (&exp);
14198 exp.X_add_number = 0;
14199 }
14200
14201 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
14202 4, &exp, 0, BFD_RELOC_MIPS_GOT16);
14203 fixp->fx_file = fragp->fr_file;
14204 fixp->fx_line = fragp->fr_line;
14205
14206 md_number_to_chars ((char*)buf, insn, 4);
14207 buf += 4;
b34976b6 14208
4a6a3df4
AO
14209 if (mips_opts.isa == ISA_MIPS1)
14210 {
14211 /* nop */
14212 md_number_to_chars ((char*)buf, 0, 4);
14213 buf += 4;
14214 }
14215
14216 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
14217 insn = HAVE_64BIT_ADDRESSES ? 0x64210000 : 0x24210000;
14218
14219 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
14220 4, &exp, 0, BFD_RELOC_LO16);
14221 fixp->fx_file = fragp->fr_file;
14222 fixp->fx_line = fragp->fr_line;
b34976b6 14223
4a6a3df4
AO
14224 md_number_to_chars ((char*)buf, insn, 4);
14225 buf += 4;
14226
14227 /* j(al)r $at. */
14228 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
14229 insn = 0x0020f809;
14230 else
14231 insn = 0x00200008;
14232
14233 md_number_to_chars ((char*)buf, insn, 4);
14234 buf += 4;
14235 }
14236 }
14237
14238 assert (buf == (bfd_byte *)fragp->fr_literal
14239 + fragp->fr_fix + fragp->fr_var);
14240
14241 fragp->fr_fix += fragp->fr_var;
14242
14243 return;
14244 }
14245
252b5132
RH
14246 if (RELAX_MIPS16_P (fragp->fr_subtype))
14247 {
14248 int type;
14249 register const struct mips16_immed_operand *op;
b34976b6 14250 bfd_boolean small, ext;
252b5132
RH
14251 offsetT val;
14252 bfd_byte *buf;
14253 unsigned long insn;
b34976b6 14254 bfd_boolean use_extend;
252b5132
RH
14255 unsigned short extend;
14256
14257 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
14258 op = mips16_immed_operands;
14259 while (op->type != type)
14260 ++op;
14261
14262 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
14263 {
b34976b6
AM
14264 small = FALSE;
14265 ext = TRUE;
252b5132
RH
14266 }
14267 else
14268 {
b34976b6
AM
14269 small = TRUE;
14270 ext = FALSE;
252b5132
RH
14271 }
14272
6386f3a7 14273 resolve_symbol_value (fragp->fr_symbol);
252b5132
RH
14274 val = S_GET_VALUE (fragp->fr_symbol);
14275 if (op->pcrel)
14276 {
14277 addressT addr;
14278
14279 addr = fragp->fr_address + fragp->fr_fix;
14280
14281 /* The rules for the base address of a PC relative reloc are
14282 complicated; see mips16_extended_frag. */
14283 if (type == 'p' || type == 'q')
14284 {
14285 addr += 2;
14286 if (ext)
14287 addr += 2;
14288 /* Ignore the low bit in the target, since it will be
14289 set for a text label. */
14290 if ((val & 1) != 0)
14291 --val;
14292 }
14293 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
14294 addr -= 4;
14295 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
14296 addr -= 2;
14297
14298 addr &= ~ (addressT) ((1 << op->shift) - 1);
14299 val -= addr;
14300
14301 /* Make sure the section winds up with the alignment we have
14302 assumed. */
14303 if (op->shift > 0)
14304 record_alignment (asec, op->shift);
14305 }
14306
14307 if (ext
14308 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
14309 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
14310 as_warn_where (fragp->fr_file, fragp->fr_line,
14311 _("extended instruction in delay slot"));
14312
14313 buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
14314
14315 if (target_big_endian)
14316 insn = bfd_getb16 (buf);
14317 else
14318 insn = bfd_getl16 (buf);
14319
14320 mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
14321 RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
14322 small, ext, &insn, &use_extend, &extend);
14323
14324 if (use_extend)
14325 {
874e8986 14326 md_number_to_chars ((char *) buf, 0xf000 | extend, 2);
252b5132
RH
14327 fragp->fr_fix += 2;
14328 buf += 2;
14329 }
14330
874e8986 14331 md_number_to_chars ((char *) buf, insn, 2);
252b5132
RH
14332 fragp->fr_fix += 2;
14333 buf += 2;
14334 }
14335 else
14336 {
14337 if (fragp->fr_opcode == NULL)
14338 return;
14339
14340 old = RELAX_OLD (fragp->fr_subtype);
14341 new = RELAX_NEW (fragp->fr_subtype);
14342 fixptr = fragp->fr_literal + fragp->fr_fix;
14343
14344 if (new > 0)
f5040a92 14345 memmove (fixptr - old, fixptr, new);
252b5132
RH
14346
14347 fragp->fr_fix += new - old;
14348 }
14349}
14350
14351#ifdef OBJ_ELF
14352
14353/* This function is called after the relocs have been generated.
14354 We've been storing mips16 text labels as odd. Here we convert them
14355 back to even for the convenience of the debugger. */
14356
14357void
14358mips_frob_file_after_relocs ()
14359{
14360 asymbol **syms;
14361 unsigned int count, i;
14362
14363 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
14364 return;
14365
14366 syms = bfd_get_outsymbols (stdoutput);
14367 count = bfd_get_symcount (stdoutput);
14368 for (i = 0; i < count; i++, syms++)
14369 {
14370 if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
14371 && ((*syms)->value & 1) != 0)
14372 {
14373 (*syms)->value &= ~1;
14374 /* If the symbol has an odd size, it was probably computed
14375 incorrectly, so adjust that as well. */
14376 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
14377 ++elf_symbol (*syms)->internal_elf_sym.st_size;
14378 }
14379 }
14380}
14381
14382#endif
14383
14384/* This function is called whenever a label is defined. It is used
14385 when handling branch delays; if a branch has a label, we assume we
14386 can not move it. */
14387
14388void
14389mips_define_label (sym)
14390 symbolS *sym;
14391{
14392 struct insn_label_list *l;
14393
14394 if (free_insn_labels == NULL)
14395 l = (struct insn_label_list *) xmalloc (sizeof *l);
14396 else
14397 {
14398 l = free_insn_labels;
14399 free_insn_labels = l->next;
14400 }
14401
14402 l->label = sym;
14403 l->next = insn_labels;
14404 insn_labels = l;
14405}
14406\f
14407#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14408
14409/* Some special processing for a MIPS ELF file. */
14410
14411void
14412mips_elf_final_processing ()
14413{
14414 /* Write out the register information. */
316f5878 14415 if (mips_abi != N64_ABI)
252b5132
RH
14416 {
14417 Elf32_RegInfo s;
14418
14419 s.ri_gprmask = mips_gprmask;
14420 s.ri_cprmask[0] = mips_cprmask[0];
14421 s.ri_cprmask[1] = mips_cprmask[1];
14422 s.ri_cprmask[2] = mips_cprmask[2];
14423 s.ri_cprmask[3] = mips_cprmask[3];
14424 /* The gp_value field is set by the MIPS ELF backend. */
14425
14426 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
14427 ((Elf32_External_RegInfo *)
14428 mips_regmask_frag));
14429 }
14430 else
14431 {
14432 Elf64_Internal_RegInfo s;
14433
14434 s.ri_gprmask = mips_gprmask;
14435 s.ri_pad = 0;
14436 s.ri_cprmask[0] = mips_cprmask[0];
14437 s.ri_cprmask[1] = mips_cprmask[1];
14438 s.ri_cprmask[2] = mips_cprmask[2];
14439 s.ri_cprmask[3] = mips_cprmask[3];
14440 /* The gp_value field is set by the MIPS ELF backend. */
14441
14442 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
14443 ((Elf64_External_RegInfo *)
14444 mips_regmask_frag));
14445 }
14446
14447 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
14448 sort of BFD interface for this. */
14449 if (mips_any_noreorder)
14450 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
14451 if (mips_pic != NO_PIC)
143d77c5 14452 {
252b5132 14453 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
143d77c5
EC
14454 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
14455 }
14456 if (mips_abicalls)
14457 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
252b5132 14458
98d3f06f 14459 /* Set MIPS ELF flags for ASEs. */
a4672219
TS
14460 if (file_ase_mips16)
14461 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
1f25f5d3
CD
14462#if 0 /* XXX FIXME */
14463 if (file_ase_mips3d)
14464 elf_elfheader (stdoutput)->e_flags |= ???;
14465#endif
deec1734
CD
14466 if (file_ase_mdmx)
14467 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
1f25f5d3 14468
bdaaa2e1 14469 /* Set the MIPS ELF ABI flags. */
316f5878 14470 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
252b5132 14471 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
316f5878 14472 else if (mips_abi == O64_ABI)
252b5132 14473 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
316f5878 14474 else if (mips_abi == EABI_ABI)
252b5132 14475 {
316f5878 14476 if (!file_mips_gp32)
252b5132
RH
14477 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
14478 else
14479 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
14480 }
316f5878 14481 else if (mips_abi == N32_ABI)
be00bddd
TS
14482 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
14483
c9914766 14484 /* Nothing to do for N64_ABI. */
252b5132
RH
14485
14486 if (mips_32bitmode)
14487 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
14488}
14489
14490#endif /* OBJ_ELF || OBJ_MAYBE_ELF */
14491\f
beae10d5
KH
14492typedef struct proc {
14493 symbolS *isym;
14494 unsigned long reg_mask;
14495 unsigned long reg_offset;
14496 unsigned long fpreg_mask;
14497 unsigned long fpreg_offset;
14498 unsigned long frame_offset;
14499 unsigned long frame_reg;
14500 unsigned long pc_reg;
14501} procS;
252b5132
RH
14502
14503static procS cur_proc;
14504static procS *cur_proc_ptr;
14505static int numprocs;
14506
0a9ef439 14507/* Fill in an rs_align_code fragment. */
a19d8eb0 14508
0a9ef439
RH
14509void
14510mips_handle_align (fragp)
14511 fragS *fragp;
a19d8eb0 14512{
0a9ef439
RH
14513 if (fragp->fr_type != rs_align_code)
14514 return;
14515
14516 if (mips_opts.mips16)
a19d8eb0
CP
14517 {
14518 static const unsigned char be_nop[] = { 0x65, 0x00 };
14519 static const unsigned char le_nop[] = { 0x00, 0x65 };
14520
0a9ef439
RH
14521 int bytes;
14522 char *p;
a19d8eb0 14523
0a9ef439
RH
14524 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
14525 p = fragp->fr_literal + fragp->fr_fix;
14526
14527 if (bytes & 1)
14528 {
14529 *p++ = 0;
f9419b05 14530 fragp->fr_fix++;
0a9ef439
RH
14531 }
14532
14533 memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
14534 fragp->fr_var = 2;
a19d8eb0
CP
14535 }
14536
0a9ef439 14537 /* For mips32, a nop is a zero, which we trivially get by doing nothing. */
a19d8eb0
CP
14538}
14539
252b5132
RH
14540static void
14541md_obj_begin ()
14542{
14543}
14544
14545static void
14546md_obj_end ()
14547{
14548 /* check for premature end, nesting errors, etc */
14549 if (cur_proc_ptr)
9a41af64 14550 as_warn (_("missing .end at end of assembly"));
252b5132
RH
14551}
14552
14553static long
14554get_number ()
14555{
14556 int negative = 0;
14557 long val = 0;
14558
14559 if (*input_line_pointer == '-')
14560 {
14561 ++input_line_pointer;
14562 negative = 1;
14563 }
3882b010 14564 if (!ISDIGIT (*input_line_pointer))
956cd1d6 14565 as_bad (_("expected simple number"));
252b5132
RH
14566 if (input_line_pointer[0] == '0')
14567 {
14568 if (input_line_pointer[1] == 'x')
14569 {
14570 input_line_pointer += 2;
3882b010 14571 while (ISXDIGIT (*input_line_pointer))
252b5132
RH
14572 {
14573 val <<= 4;
14574 val |= hex_value (*input_line_pointer++);
14575 }
14576 return negative ? -val : val;
14577 }
14578 else
14579 {
14580 ++input_line_pointer;
3882b010 14581 while (ISDIGIT (*input_line_pointer))
252b5132
RH
14582 {
14583 val <<= 3;
14584 val |= *input_line_pointer++ - '0';
14585 }
14586 return negative ? -val : val;
14587 }
14588 }
3882b010 14589 if (!ISDIGIT (*input_line_pointer))
252b5132
RH
14590 {
14591 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
14592 *input_line_pointer, *input_line_pointer);
956cd1d6 14593 as_warn (_("invalid number"));
252b5132
RH
14594 return -1;
14595 }
3882b010 14596 while (ISDIGIT (*input_line_pointer))
252b5132
RH
14597 {
14598 val *= 10;
14599 val += *input_line_pointer++ - '0';
14600 }
14601 return negative ? -val : val;
14602}
14603
14604/* The .file directive; just like the usual .file directive, but there
c5dd6aab
DJ
14605 is an initial number which is the ECOFF file index. In the non-ECOFF
14606 case .file implies DWARF-2. */
14607
14608static void
14609s_mips_file (x)
14610 int x ATTRIBUTE_UNUSED;
14611{
ecb4347a
DJ
14612 static int first_file_directive = 0;
14613
c5dd6aab
DJ
14614 if (ECOFF_DEBUGGING)
14615 {
14616 get_number ();
14617 s_app_file (0);
14618 }
14619 else
ecb4347a
DJ
14620 {
14621 char *filename;
14622
14623 filename = dwarf2_directive_file (0);
14624
14625 /* Versions of GCC up to 3.1 start files with a ".file"
14626 directive even for stabs output. Make sure that this
14627 ".file" is handled. Note that you need a version of GCC
14628 after 3.1 in order to support DWARF-2 on MIPS. */
14629 if (filename != NULL && ! first_file_directive)
14630 {
14631 (void) new_logical_line (filename, -1);
14632 s_app_file_string (filename);
14633 }
14634 first_file_directive = 1;
14635 }
c5dd6aab
DJ
14636}
14637
14638/* The .loc directive, implying DWARF-2. */
252b5132
RH
14639
14640static void
c5dd6aab 14641s_mips_loc (x)
43841e91 14642 int x ATTRIBUTE_UNUSED;
252b5132 14643{
c5dd6aab
DJ
14644 if (!ECOFF_DEBUGGING)
14645 dwarf2_directive_loc (0);
252b5132
RH
14646}
14647
252b5132
RH
14648/* The .end directive. */
14649
14650static void
14651s_mips_end (x)
43841e91 14652 int x ATTRIBUTE_UNUSED;
252b5132
RH
14653{
14654 symbolS *p;
252b5132 14655
7a621144
DJ
14656 /* Following functions need their own .frame and .cprestore directives. */
14657 mips_frame_reg_valid = 0;
14658 mips_cprestore_valid = 0;
14659
252b5132
RH
14660 if (!is_end_of_line[(unsigned char) *input_line_pointer])
14661 {
14662 p = get_symbol ();
14663 demand_empty_rest_of_line ();
14664 }
14665 else
14666 p = NULL;
14667
14949570 14668 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
14669 as_warn (_(".end not in text section"));
14670
14671 if (!cur_proc_ptr)
14672 {
14673 as_warn (_(".end directive without a preceding .ent directive."));
14674 demand_empty_rest_of_line ();
14675 return;
14676 }
14677
14678 if (p != NULL)
14679 {
14680 assert (S_GET_NAME (p));
14681 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->isym)))
14682 as_warn (_(".end symbol does not match .ent symbol."));
ecb4347a
DJ
14683
14684 if (debug_type == DEBUG_STABS)
14685 stabs_generate_asm_endfunc (S_GET_NAME (p),
14686 S_GET_NAME (p));
252b5132
RH
14687 }
14688 else
14689 as_warn (_(".end directive missing or unknown symbol"));
14690
ecb4347a
DJ
14691#ifdef OBJ_ELF
14692 /* Generate a .pdr section. */
14693 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14694 {
14695 segT saved_seg = now_seg;
14696 subsegT saved_subseg = now_subseg;
14697 valueT dot;
14698 expressionS exp;
14699 char *fragp;
252b5132 14700
ecb4347a 14701 dot = frag_now_fix ();
252b5132
RH
14702
14703#ifdef md_flush_pending_output
ecb4347a 14704 md_flush_pending_output ();
252b5132
RH
14705#endif
14706
ecb4347a
DJ
14707 assert (pdr_seg);
14708 subseg_set (pdr_seg, 0);
252b5132 14709
ecb4347a
DJ
14710 /* Write the symbol. */
14711 exp.X_op = O_symbol;
14712 exp.X_add_symbol = p;
14713 exp.X_add_number = 0;
14714 emit_expr (&exp, 4);
252b5132 14715
ecb4347a 14716 fragp = frag_more (7 * 4);
252b5132 14717
ecb4347a
DJ
14718 md_number_to_chars (fragp, (valueT) cur_proc_ptr->reg_mask, 4);
14719 md_number_to_chars (fragp + 4, (valueT) cur_proc_ptr->reg_offset, 4);
14720 md_number_to_chars (fragp + 8, (valueT) cur_proc_ptr->fpreg_mask, 4);
14721 md_number_to_chars (fragp + 12, (valueT) cur_proc_ptr->fpreg_offset, 4);
14722 md_number_to_chars (fragp + 16, (valueT) cur_proc_ptr->frame_offset, 4);
14723 md_number_to_chars (fragp + 20, (valueT) cur_proc_ptr->frame_reg, 4);
14724 md_number_to_chars (fragp + 24, (valueT) cur_proc_ptr->pc_reg, 4);
252b5132 14725
ecb4347a
DJ
14726 subseg_set (saved_seg, saved_subseg);
14727 }
14728#endif /* OBJ_ELF */
252b5132
RH
14729
14730 cur_proc_ptr = NULL;
14731}
14732
14733/* The .aent and .ent directives. */
14734
14735static void
14736s_mips_ent (aent)
14737 int aent;
14738{
252b5132 14739 symbolS *symbolP;
252b5132
RH
14740
14741 symbolP = get_symbol ();
14742 if (*input_line_pointer == ',')
f9419b05 14743 ++input_line_pointer;
252b5132 14744 SKIP_WHITESPACE ();
3882b010 14745 if (ISDIGIT (*input_line_pointer)
d9a62219 14746 || *input_line_pointer == '-')
874e8986 14747 get_number ();
252b5132 14748
14949570 14749 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
14750 as_warn (_(".ent or .aent not in text section."));
14751
14752 if (!aent && cur_proc_ptr)
9a41af64 14753 as_warn (_("missing .end"));
252b5132
RH
14754
14755 if (!aent)
14756 {
7a621144
DJ
14757 /* This function needs its own .frame and .cprestore directives. */
14758 mips_frame_reg_valid = 0;
14759 mips_cprestore_valid = 0;
14760
252b5132
RH
14761 cur_proc_ptr = &cur_proc;
14762 memset (cur_proc_ptr, '\0', sizeof (procS));
14763
14764 cur_proc_ptr->isym = symbolP;
14765
49309057 14766 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
252b5132 14767
f9419b05 14768 ++numprocs;
ecb4347a
DJ
14769
14770 if (debug_type == DEBUG_STABS)
14771 stabs_generate_asm_func (S_GET_NAME (symbolP),
14772 S_GET_NAME (symbolP));
252b5132
RH
14773 }
14774
14775 demand_empty_rest_of_line ();
14776}
14777
14778/* The .frame directive. If the mdebug section is present (IRIX 5 native)
bdaaa2e1 14779 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
252b5132 14780 s_mips_frame is used so that we can set the PDR information correctly.
bdaaa2e1 14781 We can't use the ecoff routines because they make reference to the ecoff
252b5132
RH
14782 symbol table (in the mdebug section). */
14783
14784static void
14785s_mips_frame (ignore)
2b3c5a5d 14786 int ignore ATTRIBUTE_UNUSED;
252b5132 14787{
ecb4347a
DJ
14788#ifdef OBJ_ELF
14789 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14790 {
14791 long val;
252b5132 14792
ecb4347a
DJ
14793 if (cur_proc_ptr == (procS *) NULL)
14794 {
14795 as_warn (_(".frame outside of .ent"));
14796 demand_empty_rest_of_line ();
14797 return;
14798 }
252b5132 14799
ecb4347a
DJ
14800 cur_proc_ptr->frame_reg = tc_get_register (1);
14801
14802 SKIP_WHITESPACE ();
14803 if (*input_line_pointer++ != ','
14804 || get_absolute_expression_and_terminator (&val) != ',')
14805 {
14806 as_warn (_("Bad .frame directive"));
14807 --input_line_pointer;
14808 demand_empty_rest_of_line ();
14809 return;
14810 }
252b5132 14811
ecb4347a
DJ
14812 cur_proc_ptr->frame_offset = val;
14813 cur_proc_ptr->pc_reg = tc_get_register (0);
252b5132 14814
252b5132 14815 demand_empty_rest_of_line ();
252b5132 14816 }
ecb4347a
DJ
14817 else
14818#endif /* OBJ_ELF */
14819 s_ignore (ignore);
252b5132
RH
14820}
14821
bdaaa2e1
KH
14822/* The .fmask and .mask directives. If the mdebug section is present
14823 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
252b5132 14824 embedded targets, s_mips_mask is used so that we can set the PDR
bdaaa2e1 14825 information correctly. We can't use the ecoff routines because they
252b5132
RH
14826 make reference to the ecoff symbol table (in the mdebug section). */
14827
14828static void
14829s_mips_mask (reg_type)
14830 char reg_type;
14831{
ecb4347a
DJ
14832#ifdef OBJ_ELF
14833 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
252b5132 14834 {
ecb4347a 14835 long mask, off;
252b5132 14836
ecb4347a
DJ
14837 if (cur_proc_ptr == (procS *) NULL)
14838 {
14839 as_warn (_(".mask/.fmask outside of .ent"));
14840 demand_empty_rest_of_line ();
14841 return;
14842 }
252b5132 14843
ecb4347a
DJ
14844 if (get_absolute_expression_and_terminator (&mask) != ',')
14845 {
14846 as_warn (_("Bad .mask/.fmask directive"));
14847 --input_line_pointer;
14848 demand_empty_rest_of_line ();
14849 return;
14850 }
252b5132 14851
ecb4347a
DJ
14852 off = get_absolute_expression ();
14853
14854 if (reg_type == 'F')
14855 {
14856 cur_proc_ptr->fpreg_mask = mask;
14857 cur_proc_ptr->fpreg_offset = off;
14858 }
14859 else
14860 {
14861 cur_proc_ptr->reg_mask = mask;
14862 cur_proc_ptr->reg_offset = off;
14863 }
14864
14865 demand_empty_rest_of_line ();
252b5132
RH
14866 }
14867 else
ecb4347a
DJ
14868#endif /* OBJ_ELF */
14869 s_ignore (reg_type);
252b5132
RH
14870}
14871
14872/* The .loc directive. */
14873
14874#if 0
14875static void
14876s_loc (x)
14877 int x;
14878{
14879 symbolS *symbolP;
14880 int lineno;
14881 int addroff;
14882
14883 assert (now_seg == text_section);
14884
14885 lineno = get_number ();
14886 addroff = frag_now_fix ();
14887
14888 symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
14889 S_SET_TYPE (symbolP, N_SLINE);
14890 S_SET_OTHER (symbolP, 0);
14891 S_SET_DESC (symbolP, lineno);
14892 symbolP->sy_segment = now_seg;
14893}
14894#endif
e7af610e 14895
316f5878
RS
14896/* A table describing all the processors gas knows about. Names are
14897 matched in the order listed.
e7af610e 14898
316f5878
RS
14899 To ease comparison, please keep this table in the same order as
14900 gcc's mips_cpu_info_table[]. */
e972090a
NC
14901static const struct mips_cpu_info mips_cpu_info_table[] =
14902{
316f5878
RS
14903 /* Entries for generic ISAs */
14904 { "mips1", 1, ISA_MIPS1, CPU_R3000 },
14905 { "mips2", 1, ISA_MIPS2, CPU_R6000 },
14906 { "mips3", 1, ISA_MIPS3, CPU_R4000 },
14907 { "mips4", 1, ISA_MIPS4, CPU_R8000 },
14908 { "mips5", 1, ISA_MIPS5, CPU_MIPS5 },
14909 { "mips32", 1, ISA_MIPS32, CPU_MIPS32 },
af7ee8bf 14910 { "mips32r2", 1, ISA_MIPS32R2, CPU_MIPS32R2 },
316f5878
RS
14911 { "mips64", 1, ISA_MIPS64, CPU_MIPS64 },
14912
14913 /* MIPS I */
14914 { "r3000", 0, ISA_MIPS1, CPU_R3000 },
14915 { "r2000", 0, ISA_MIPS1, CPU_R3000 },
14916 { "r3900", 0, ISA_MIPS1, CPU_R3900 },
14917
14918 /* MIPS II */
14919 { "r6000", 0, ISA_MIPS2, CPU_R6000 },
14920
14921 /* MIPS III */
14922 { "r4000", 0, ISA_MIPS3, CPU_R4000 },
14923 { "r4010", 0, ISA_MIPS2, CPU_R4010 },
14924 { "vr4100", 0, ISA_MIPS3, CPU_VR4100 },
14925 { "vr4111", 0, ISA_MIPS3, CPU_R4111 },
60b63b72
RS
14926 { "vr4120", 0, ISA_MIPS3, CPU_VR4120 },
14927 { "vr4130", 0, ISA_MIPS3, CPU_VR4120 },
14928 { "vr4181", 0, ISA_MIPS3, CPU_R4111 },
316f5878
RS
14929 { "vr4300", 0, ISA_MIPS3, CPU_R4300 },
14930 { "r4400", 0, ISA_MIPS3, CPU_R4400 },
14931 { "r4600", 0, ISA_MIPS3, CPU_R4600 },
14932 { "orion", 0, ISA_MIPS3, CPU_R4600 },
14933 { "r4650", 0, ISA_MIPS3, CPU_R4650 },
14934
14935 /* MIPS IV */
14936 { "r8000", 0, ISA_MIPS4, CPU_R8000 },
14937 { "r10000", 0, ISA_MIPS4, CPU_R10000 },
14938 { "r12000", 0, ISA_MIPS4, CPU_R12000 },
14939 { "vr5000", 0, ISA_MIPS4, CPU_R5000 },
60b63b72
RS
14940 { "vr5400", 0, ISA_MIPS4, CPU_VR5400 },
14941 { "vr5500", 0, ISA_MIPS4, CPU_VR5500 },
316f5878
RS
14942 { "rm5200", 0, ISA_MIPS4, CPU_R5000 },
14943 { "rm5230", 0, ISA_MIPS4, CPU_R5000 },
14944 { "rm5231", 0, ISA_MIPS4, CPU_R5000 },
14945 { "rm5261", 0, ISA_MIPS4, CPU_R5000 },
14946 { "rm5721", 0, ISA_MIPS4, CPU_R5000 },
14947 { "r7000", 0, ISA_MIPS4, CPU_R5000 },
14948
14949 /* MIPS 32 */
14950 { "4kc", 0, ISA_MIPS32, CPU_MIPS32, },
14951 { "4km", 0, ISA_MIPS32, CPU_MIPS32 },
14952 { "4kp", 0, ISA_MIPS32, CPU_MIPS32 },
e7af610e 14953
316f5878
RS
14954 /* MIPS 64 */
14955 { "5kc", 0, ISA_MIPS64, CPU_MIPS64 },
14956 { "20kc", 0, ISA_MIPS64, CPU_MIPS64 },
e7af610e 14957
c7a23324 14958 /* Broadcom SB-1 CPU core */
316f5878 14959 { "sb1", 0, ISA_MIPS64, CPU_SB1 },
e7af610e 14960
316f5878
RS
14961 /* End marker */
14962 { NULL, 0, 0, 0 }
14963};
e7af610e 14964
84ea6cf2 14965
316f5878
RS
14966/* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
14967 with a final "000" replaced by "k". Ignore case.
e7af610e 14968
316f5878 14969 Note: this function is shared between GCC and GAS. */
c6c98b38 14970
b34976b6 14971static bfd_boolean
316f5878
RS
14972mips_strict_matching_cpu_name_p (canonical, given)
14973 const char *canonical, *given;
14974{
14975 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
14976 given++, canonical++;
14977
14978 return ((*given == 0 && *canonical == 0)
14979 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
14980}
14981
14982
14983/* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
14984 CPU name. We've traditionally allowed a lot of variation here.
14985
14986 Note: this function is shared between GCC and GAS. */
14987
b34976b6 14988static bfd_boolean
316f5878
RS
14989mips_matching_cpu_name_p (canonical, given)
14990 const char *canonical, *given;
14991{
14992 /* First see if the name matches exactly, or with a final "000"
14993 turned into "k". */
14994 if (mips_strict_matching_cpu_name_p (canonical, given))
b34976b6 14995 return TRUE;
316f5878
RS
14996
14997 /* If not, try comparing based on numerical designation alone.
14998 See if GIVEN is an unadorned number, or 'r' followed by a number. */
14999 if (TOLOWER (*given) == 'r')
15000 given++;
15001 if (!ISDIGIT (*given))
b34976b6 15002 return FALSE;
316f5878
RS
15003
15004 /* Skip over some well-known prefixes in the canonical name,
15005 hoping to find a number there too. */
15006 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
15007 canonical += 2;
15008 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
15009 canonical += 2;
15010 else if (TOLOWER (canonical[0]) == 'r')
15011 canonical += 1;
15012
15013 return mips_strict_matching_cpu_name_p (canonical, given);
15014}
15015
15016
15017/* Parse an option that takes the name of a processor as its argument.
15018 OPTION is the name of the option and CPU_STRING is the argument.
15019 Return the corresponding processor enumeration if the CPU_STRING is
15020 recognized, otherwise report an error and return null.
15021
15022 A similar function exists in GCC. */
e7af610e
NC
15023
15024static const struct mips_cpu_info *
316f5878
RS
15025mips_parse_cpu (option, cpu_string)
15026 const char *option, *cpu_string;
e7af610e 15027{
316f5878 15028 const struct mips_cpu_info *p;
e7af610e 15029
316f5878
RS
15030 /* 'from-abi' selects the most compatible architecture for the given
15031 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
15032 EABIs, we have to decide whether we're using the 32-bit or 64-bit
15033 version. Look first at the -mgp options, if given, otherwise base
15034 the choice on MIPS_DEFAULT_64BIT.
e7af610e 15035
316f5878
RS
15036 Treat NO_ABI like the EABIs. One reason to do this is that the
15037 plain 'mips' and 'mips64' configs have 'from-abi' as their default
15038 architecture. This code picks MIPS I for 'mips' and MIPS III for
15039 'mips64', just as we did in the days before 'from-abi'. */
15040 if (strcasecmp (cpu_string, "from-abi") == 0)
15041 {
15042 if (ABI_NEEDS_32BIT_REGS (mips_abi))
15043 return mips_cpu_info_from_isa (ISA_MIPS1);
15044
15045 if (ABI_NEEDS_64BIT_REGS (mips_abi))
15046 return mips_cpu_info_from_isa (ISA_MIPS3);
15047
15048 if (file_mips_gp32 >= 0)
15049 return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
15050
15051 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
15052 ? ISA_MIPS3
15053 : ISA_MIPS1);
15054 }
15055
15056 /* 'default' has traditionally been a no-op. Probably not very useful. */
15057 if (strcasecmp (cpu_string, "default") == 0)
15058 return 0;
15059
15060 for (p = mips_cpu_info_table; p->name != 0; p++)
15061 if (mips_matching_cpu_name_p (p->name, cpu_string))
15062 return p;
15063
15064 as_bad ("Bad value (%s) for %s", cpu_string, option);
15065 return 0;
e7af610e
NC
15066}
15067
316f5878
RS
15068/* Return the canonical processor information for ISA (a member of the
15069 ISA_MIPS* enumeration). */
15070
e7af610e
NC
15071static const struct mips_cpu_info *
15072mips_cpu_info_from_isa (isa)
15073 int isa;
15074{
15075 int i;
15076
15077 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
15078 if (mips_cpu_info_table[i].is_isa
316f5878 15079 && isa == mips_cpu_info_table[i].isa)
e7af610e
NC
15080 return (&mips_cpu_info_table[i]);
15081
e972090a 15082 return NULL;
e7af610e 15083}
316f5878
RS
15084\f
15085static void
15086show (stream, string, col_p, first_p)
15087 FILE *stream;
15088 const char *string;
15089 int *col_p;
15090 int *first_p;
15091{
15092 if (*first_p)
15093 {
15094 fprintf (stream, "%24s", "");
15095 *col_p = 24;
15096 }
15097 else
15098 {
15099 fprintf (stream, ", ");
15100 *col_p += 2;
15101 }
e7af610e 15102
316f5878
RS
15103 if (*col_p + strlen (string) > 72)
15104 {
15105 fprintf (stream, "\n%24s", "");
15106 *col_p = 24;
15107 }
15108
15109 fprintf (stream, "%s", string);
15110 *col_p += strlen (string);
15111
15112 *first_p = 0;
15113}
15114
15115void
15116md_show_usage (stream)
15117 FILE *stream;
e7af610e 15118{
316f5878
RS
15119 int column, first;
15120 size_t i;
15121
15122 fprintf (stream, _("\
15123MIPS options:\n\
15124-membedded-pic generate embedded position independent code\n\
15125-EB generate big endian output\n\
15126-EL generate little endian output\n\
15127-g, -g2 do not remove unneeded NOPs or swap branches\n\
15128-G NUM allow referencing objects up to NUM bytes\n\
15129 implicitly with the gp register [default 8]\n"));
15130 fprintf (stream, _("\
15131-mips1 generate MIPS ISA I instructions\n\
15132-mips2 generate MIPS ISA II instructions\n\
15133-mips3 generate MIPS ISA III instructions\n\
15134-mips4 generate MIPS ISA IV instructions\n\
15135-mips5 generate MIPS ISA V instructions\n\
15136-mips32 generate MIPS32 ISA instructions\n\
af7ee8bf 15137-mips32r2 generate MIPS32 release 2 ISA instructions\n\
316f5878
RS
15138-mips64 generate MIPS64 ISA instructions\n\
15139-march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
15140
15141 first = 1;
e7af610e
NC
15142
15143 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
316f5878
RS
15144 show (stream, mips_cpu_info_table[i].name, &column, &first);
15145 show (stream, "from-abi", &column, &first);
15146 fputc ('\n', stream);
e7af610e 15147
316f5878
RS
15148 fprintf (stream, _("\
15149-mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
15150-no-mCPU don't generate code specific to CPU.\n\
15151 For -mCPU and -no-mCPU, CPU must be one of:\n"));
15152
15153 first = 1;
15154
15155 show (stream, "3900", &column, &first);
15156 show (stream, "4010", &column, &first);
15157 show (stream, "4100", &column, &first);
15158 show (stream, "4650", &column, &first);
15159 fputc ('\n', stream);
15160
15161 fprintf (stream, _("\
15162-mips16 generate mips16 instructions\n\
15163-no-mips16 do not generate mips16 instructions\n"));
15164 fprintf (stream, _("\
15165-mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
15166-mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
15167-O0 remove unneeded NOPs, do not swap branches\n\
15168-O remove unneeded NOPs and swap branches\n\
15169-n warn about NOPs generated from macros\n\
15170--[no-]construct-floats [dis]allow floating point values to be constructed\n\
15171--trap, --no-break trap exception on div by 0 and mult overflow\n\
15172--break, --no-trap break exception on div by 0 and mult overflow\n"));
15173#ifdef OBJ_ELF
15174 fprintf (stream, _("\
15175-KPIC, -call_shared generate SVR4 position independent code\n\
15176-non_shared do not generate position independent code\n\
15177-xgot assume a 32 bit GOT\n\
15178-mabi=ABI create ABI conformant object file for:\n"));
15179
15180 first = 1;
15181
15182 show (stream, "32", &column, &first);
15183 show (stream, "o64", &column, &first);
15184 show (stream, "n32", &column, &first);
15185 show (stream, "64", &column, &first);
15186 show (stream, "eabi", &column, &first);
15187
15188 fputc ('\n', stream);
15189
15190 fprintf (stream, _("\
15191-32 create o32 ABI object file (default)\n\
15192-n32 create n32 ABI object file\n\
15193-64 create 64 ABI object file\n"));
15194#endif
e7af610e 15195}
14e777e0
KB
15196
15197enum dwarf2_format
15198mips_dwarf2_format ()
15199{
15200 if (mips_abi == N64_ABI)
1de5b6a1
AO
15201 {
15202#ifdef TE_IRIX
15203 return dwarf2_format_64bit_irix;
15204#else
15205 return dwarf2_format_64bit;
15206#endif
15207 }
14e777e0
KB
15208 else
15209 return dwarf2_format_32bit;
15210}
This page took 1.127405 seconds and 4 git commands to generate.