* elf32-hppa.c (group_sections): Don't share a stub section if
[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
252b5132
RH
134/* This is the set of options which may be modified by the .set
135 pseudo-op. We use a struct so that .set push and .set pop are more
136 reliable. */
137
e972090a
NC
138struct mips_set_options
139{
252b5132
RH
140 /* MIPS ISA (Instruction Set Architecture) level. This is set to -1
141 if it has not been initialized. Changed by `.set mipsN', and the
142 -mipsN command line option, and the default CPU. */
143 int isa;
1f25f5d3
CD
144 /* Enabled Application Specific Extensions (ASEs). These are set to -1
145 if they have not been initialized. Changed by `.set <asename>', by
146 command line options, and based on the default architecture. */
147 int ase_mips3d;
deec1734 148 int ase_mdmx;
252b5132
RH
149 /* Whether we are assembling for the mips16 processor. 0 if we are
150 not, 1 if we are, and -1 if the value has not been initialized.
151 Changed by `.set mips16' and `.set nomips16', and the -mips16 and
152 -nomips16 command line options, and the default CPU. */
153 int mips16;
154 /* Non-zero if we should not reorder instructions. Changed by `.set
155 reorder' and `.set noreorder'. */
156 int noreorder;
157 /* Non-zero if we should not permit the $at ($1) register to be used
158 in instructions. Changed by `.set at' and `.set noat'. */
159 int noat;
160 /* Non-zero if we should warn when a macro instruction expands into
161 more than one machine instruction. Changed by `.set nomacro' and
162 `.set macro'. */
163 int warn_about_macros;
164 /* Non-zero if we should not move instructions. Changed by `.set
165 move', `.set volatile', `.set nomove', and `.set novolatile'. */
166 int nomove;
167 /* Non-zero if we should not optimize branches by moving the target
168 of the branch into the delay slot. Actually, we don't perform
169 this optimization anyhow. Changed by `.set bopt' and `.set
170 nobopt'. */
171 int nobopt;
172 /* Non-zero if we should not autoextend mips16 instructions.
173 Changed by `.set autoextend' and `.set noautoextend'. */
174 int noautoextend;
a325df1d
TS
175 /* Restrict general purpose registers and floating point registers
176 to 32 bit. This is initially determined when -mgp32 or -mfp32
177 is passed but can changed if the assembler code uses .set mipsN. */
178 int gp32;
179 int fp32;
252b5132
RH
180};
181
a325df1d 182/* True if -mgp32 was passed. */
a8e8e863 183static int file_mips_gp32 = -1;
a325df1d
TS
184
185/* True if -mfp32 was passed. */
a8e8e863 186static int file_mips_fp32 = -1;
a325df1d 187
252b5132 188/* This is the struct we use to hold the current set of options. Note
a4672219 189 that we must set the isa field to ISA_UNKNOWN and the ASE fields to
e7af610e 190 -1 to indicate that they have not been initialized. */
252b5132 191
e972090a
NC
192static struct mips_set_options mips_opts =
193{
316f5878 194 ISA_UNKNOWN, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0
e7af610e 195};
252b5132
RH
196
197/* These variables are filled in with the masks of registers used.
198 The object format code reads them and puts them in the appropriate
199 place. */
200unsigned long mips_gprmask;
201unsigned long mips_cprmask[4];
202
203/* MIPS ISA we are using for this output file. */
e7af610e 204static int file_mips_isa = ISA_UNKNOWN;
252b5132 205
a4672219
TS
206/* True if -mips16 was passed or implied by arguments passed on the
207 command line (e.g., by -march). */
208static int file_ase_mips16;
209
1f25f5d3
CD
210/* True if -mips3d was passed or implied by arguments passed on the
211 command line (e.g., by -march). */
212static int file_ase_mips3d;
213
deec1734
CD
214/* True if -mdmx was passed or implied by arguments passed on the
215 command line (e.g., by -march). */
216static int file_ase_mdmx;
217
ec68c924
EC
218/* The argument of the -march= flag. The architecture we are assembling. */
219static int mips_arch = CPU_UNKNOWN;
316f5878
RS
220static const char *mips_arch_string;
221static const struct mips_cpu_info *mips_arch_info;
ec68c924
EC
222
223/* The argument of the -mtune= flag. The architecture for which we
224 are optimizing. */
225static int mips_tune = CPU_UNKNOWN;
316f5878
RS
226static const char *mips_tune_string;
227static const struct mips_cpu_info *mips_tune_info;
ec68c924 228
316f5878 229/* True when generating 32-bit code for a 64-bit processor. */
252b5132
RH
230static int mips_32bitmode = 0;
231
9ce8a5dd
GRK
232/* Some ISA's have delay slots for instructions which read or write
233 from a coprocessor (eg. mips1-mips3); some don't (eg mips4).
bdaaa2e1 234 Return true if instructions marked INSN_LOAD_COPROC_DELAY,
9ce8a5dd
GRK
235 INSN_COPROC_MOVE_DELAY, or INSN_WRITE_COND_CODE actually have a
236 delay slot in this ISA. The uses of this macro assume that any
237 ISA that has delay slots for one of these, has them for all. They
238 also assume that ISAs which don't have delays for these insns, don't
bdaaa2e1 239 have delays for the INSN_LOAD_MEMORY_DELAY instructions either. */
9ce8a5dd 240#define ISA_HAS_COPROC_DELAYS(ISA) ( \
e7af610e
NC
241 (ISA) == ISA_MIPS1 \
242 || (ISA) == ISA_MIPS2 \
243 || (ISA) == ISA_MIPS3 \
9ce8a5dd
GRK
244 )
245
316f5878
RS
246/* True if the given ABI requires 32-bit registers. */
247#define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
248
249/* Likewise 64-bit registers. */
250#define ABI_NEEDS_64BIT_REGS(ABI) \
251 ((ABI) == N32_ABI \
252 || (ABI) == N64_ABI \
253 || (ABI) == O64_ABI)
254
bdaaa2e1 255/* Return true if ISA supports 64 bit gp register instructions. */
9ce8a5dd 256#define ISA_HAS_64BIT_REGS(ISA) ( \
e7af610e
NC
257 (ISA) == ISA_MIPS3 \
258 || (ISA) == ISA_MIPS4 \
84ea6cf2 259 || (ISA) == ISA_MIPS5 \
d1cf510e 260 || (ISA) == ISA_MIPS64 \
9ce8a5dd
GRK
261 )
262
af7ee8bf
CD
263/* Return true if ISA supports 64-bit right rotate (dror et al.)
264 instructions. */
265#define ISA_HAS_DROR(ISA) ( \
266 0 \
267 )
268
269/* Return true if ISA supports 32-bit right rotate (ror et al.)
270 instructions. */
271#define ISA_HAS_ROR(ISA) ( \
272 (ISA) == ISA_MIPS32R2 \
273 )
274
e013f690 275#define HAVE_32BIT_GPRS \
316f5878 276 (mips_opts.gp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
ca4e0257 277
e013f690 278#define HAVE_32BIT_FPRS \
316f5878 279 (mips_opts.fp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
ca4e0257
RS
280
281#define HAVE_64BIT_GPRS (! HAVE_32BIT_GPRS)
282#define HAVE_64BIT_FPRS (! HAVE_32BIT_FPRS)
283
316f5878 284#define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
e013f690 285
316f5878 286#define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
e013f690
TS
287
288/* We can only have 64bit addresses if the object file format
289 supports it. */
afdbd6d0
CD
290#define HAVE_32BIT_ADDRESSES \
291 (HAVE_32BIT_GPRS \
292 || ((bfd_arch_bits_per_address (stdoutput) == 32 \
293 || ! HAVE_64BIT_OBJECTS) \
294 && mips_pic != EMBEDDED_PIC))
e013f690
TS
295
296#define HAVE_64BIT_ADDRESSES (! HAVE_32BIT_ADDRESSES)
ca4e0257 297
a4672219 298/* Return true if the given CPU supports the MIPS16 ASE. */
3396de36
TS
299#define CPU_HAS_MIPS16(cpu) \
300 (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0 \
301 || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
a4672219 302
1f25f5d3
CD
303/* Return true if the given CPU supports the MIPS3D ASE. */
304#define CPU_HAS_MIPS3D(cpu) ((cpu) == CPU_SB1 \
305 )
306
deec1734 307/* Return true if the given CPU supports the MDMX ASE. */
b34976b6 308#define CPU_HAS_MDMX(cpu) (FALSE \
deec1734
CD
309 )
310
60b63b72
RS
311/* True if CPU has a dror instruction. */
312#define CPU_HAS_DROR(CPU) ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
313
314/* True if CPU has a ror instruction. */
315#define CPU_HAS_ROR(CPU) CPU_HAS_DROR (CPU)
316
bdaaa2e1 317/* Whether the processor uses hardware interlocks to protect
252b5132 318 reads from the HI and LO registers, and thus does not
ec68c924 319 require nops to be inserted. */
252b5132 320
ec68c924 321#define hilo_interlocks (mips_arch == CPU_R4010 \
60b63b72 322 || mips_arch == CPU_VR5500 \
0a758a12 323 || mips_arch == CPU_SB1 \
252b5132
RH
324 )
325
326/* Whether the processor uses hardware interlocks to protect reads
327 from the GPRs, and thus does not require nops to be inserted. */
328#define gpr_interlocks \
e7af610e 329 (mips_opts.isa != ISA_MIPS1 \
60b63b72
RS
330 || mips_arch == CPU_VR5400 \
331 || mips_arch == CPU_VR5500 \
ec68c924 332 || mips_arch == CPU_R3900)
252b5132
RH
333
334/* As with other "interlocks" this is used by hardware that has FP
335 (co-processor) interlocks. */
bdaaa2e1 336/* Itbl support may require additional care here. */
ec68c924 337#define cop_interlocks (mips_arch == CPU_R4300 \
60b63b72
RS
338 || mips_arch == CPU_VR5400 \
339 || mips_arch == CPU_VR5500 \
0a758a12 340 || mips_arch == CPU_SB1 \
252b5132
RH
341 )
342
6b76fefe
CM
343/* Is this a mfhi or mflo instruction? */
344#define MF_HILO_INSN(PINFO) \
345 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
346
252b5132
RH
347/* MIPS PIC level. */
348
a161fe53 349enum mips_pic_level mips_pic;
252b5132 350
39c0a331
L
351/* Warn about all NOPS that the assembler generates. */
352static int warn_nops = 0;
353
c9914766 354/* 1 if we should generate 32 bit offsets from the $gp register in
252b5132 355 SVR4_PIC mode. Currently has no meaning in other modes. */
c9914766 356static int mips_big_got = 0;
252b5132
RH
357
358/* 1 if trap instructions should used for overflow rather than break
359 instructions. */
c9914766 360static int mips_trap = 0;
252b5132 361
119d663a 362/* 1 if double width floating point constants should not be constructed
b6ff326e 363 by assembling two single width halves into two single width floating
119d663a
NC
364 point registers which just happen to alias the double width destination
365 register. On some architectures this aliasing can be disabled by a bit
d547a75e 366 in the status register, and the setting of this bit cannot be determined
119d663a
NC
367 automatically at assemble time. */
368static int mips_disable_float_construction;
369
252b5132
RH
370/* Non-zero if any .set noreorder directives were used. */
371
372static int mips_any_noreorder;
373
6b76fefe
CM
374/* Non-zero if nops should be inserted when the register referenced in
375 an mfhi/mflo instruction is read in the next two instructions. */
376static int mips_7000_hilo_fix;
377
252b5132 378/* The size of the small data section. */
156c2f8b 379static unsigned int g_switch_value = 8;
252b5132
RH
380/* Whether the -G option was used. */
381static int g_switch_seen = 0;
382
383#define N_RMASK 0xc4
384#define N_VFP 0xd4
385
386/* If we can determine in advance that GP optimization won't be
387 possible, we can skip the relaxation stuff that tries to produce
388 GP-relative references. This makes delay slot optimization work
389 better.
390
391 This function can only provide a guess, but it seems to work for
fba2b7f9
GK
392 gcc output. It needs to guess right for gcc, otherwise gcc
393 will put what it thinks is a GP-relative instruction in a branch
394 delay slot.
252b5132
RH
395
396 I don't know if a fix is needed for the SVR4_PIC mode. I've only
397 fixed it for the non-PIC mode. KR 95/04/07 */
398static int nopic_need_relax PARAMS ((symbolS *, int));
399
400/* handle of the OPCODE hash table */
401static struct hash_control *op_hash = NULL;
402
403/* The opcode hash table we use for the mips16. */
404static struct hash_control *mips16_op_hash = NULL;
405
406/* This array holds the chars that always start a comment. If the
407 pre-processor is disabled, these aren't very useful */
408const char comment_chars[] = "#";
409
410/* This array holds the chars that only start a comment at the beginning of
411 a line. If the line seems to have the form '# 123 filename'
412 .line and .file directives will appear in the pre-processed output */
413/* Note that input_file.c hand checks for '#' at the beginning of the
414 first line of the input file. This is because the compiler outputs
bdaaa2e1 415 #NO_APP at the beginning of its output. */
252b5132
RH
416/* Also note that C style comments are always supported. */
417const char line_comment_chars[] = "#";
418
bdaaa2e1 419/* This array holds machine specific line separator characters. */
63a0b638 420const char line_separator_chars[] = ";";
252b5132
RH
421
422/* Chars that can be used to separate mant from exp in floating point nums */
423const char EXP_CHARS[] = "eE";
424
425/* Chars that mean this number is a floating point constant */
426/* As in 0f12.456 */
427/* or 0d1.2345e12 */
428const char FLT_CHARS[] = "rRsSfFdDxXpP";
429
430/* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
431 changed in read.c . Ideally it shouldn't have to know about it at all,
432 but nothing is ideal around here.
433 */
434
435static char *insn_error;
436
437static int auto_align = 1;
438
439/* When outputting SVR4 PIC code, the assembler needs to know the
440 offset in the stack frame from which to restore the $gp register.
441 This is set by the .cprestore pseudo-op, and saved in this
442 variable. */
443static offsetT mips_cprestore_offset = -1;
444
6478892d
TS
445/* Similiar for NewABI PIC code, where $gp is callee-saved. NewABI has some
446 more optimizations, it can use a register value instead of a memory-saved
956cd1d6 447 offset and even an other register than $gp as global pointer. */
6478892d
TS
448static offsetT mips_cpreturn_offset = -1;
449static int mips_cpreturn_register = -1;
450static int mips_gp_register = GP;
def2e0dd 451static int mips_gprel_offset = 0;
6478892d 452
7a621144
DJ
453/* Whether mips_cprestore_offset has been set in the current function
454 (or whether it has already been warned about, if not). */
455static int mips_cprestore_valid = 0;
456
252b5132
RH
457/* This is the register which holds the stack frame, as set by the
458 .frame pseudo-op. This is needed to implement .cprestore. */
459static int mips_frame_reg = SP;
460
7a621144
DJ
461/* Whether mips_frame_reg has been set in the current function
462 (or whether it has already been warned about, if not). */
463static int mips_frame_reg_valid = 0;
464
252b5132
RH
465/* To output NOP instructions correctly, we need to keep information
466 about the previous two instructions. */
467
468/* Whether we are optimizing. The default value of 2 means to remove
469 unneeded NOPs and swap branch instructions when possible. A value
470 of 1 means to not swap branches. A value of 0 means to always
471 insert NOPs. */
472static int mips_optimize = 2;
473
474/* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
475 equivalent to seeing no -g option at all. */
476static int mips_debug = 0;
477
478/* The previous instruction. */
479static struct mips_cl_insn prev_insn;
480
481/* The instruction before prev_insn. */
482static struct mips_cl_insn prev_prev_insn;
483
484/* If we don't want information for prev_insn or prev_prev_insn, we
485 point the insn_mo field at this dummy integer. */
43841e91 486static const struct mips_opcode dummy_opcode = { NULL, NULL, 0, 0, 0, 0 };
252b5132
RH
487
488/* Non-zero if prev_insn is valid. */
489static int prev_insn_valid;
490
491/* The frag for the previous instruction. */
492static struct frag *prev_insn_frag;
493
494/* The offset into prev_insn_frag for the previous instruction. */
495static long prev_insn_where;
496
497/* The reloc type for the previous instruction, if any. */
f6688943 498static bfd_reloc_code_real_type prev_insn_reloc_type[3];
252b5132
RH
499
500/* The reloc for the previous instruction, if any. */
f6688943 501static fixS *prev_insn_fixp[3];
252b5132
RH
502
503/* Non-zero if the previous instruction was in a delay slot. */
504static int prev_insn_is_delay_slot;
505
506/* Non-zero if the previous instruction was in a .set noreorder. */
507static int prev_insn_unreordered;
508
509/* Non-zero if the previous instruction uses an extend opcode (if
510 mips16). */
511static int prev_insn_extended;
512
513/* Non-zero if the previous previous instruction was in a .set
514 noreorder. */
515static int prev_prev_insn_unreordered;
516
517/* If this is set, it points to a frag holding nop instructions which
518 were inserted before the start of a noreorder section. If those
519 nops turn out to be unnecessary, the size of the frag can be
520 decreased. */
521static fragS *prev_nop_frag;
522
523/* The number of nop instructions we created in prev_nop_frag. */
524static int prev_nop_frag_holds;
525
526/* The number of nop instructions that we know we need in
bdaaa2e1 527 prev_nop_frag. */
252b5132
RH
528static int prev_nop_frag_required;
529
530/* The number of instructions we've seen since prev_nop_frag. */
531static int prev_nop_frag_since;
532
533/* For ECOFF and ELF, relocations against symbols are done in two
534 parts, with a HI relocation and a LO relocation. Each relocation
535 has only 16 bits of space to store an addend. This means that in
536 order for the linker to handle carries correctly, it must be able
537 to locate both the HI and the LO relocation. This means that the
538 relocations must appear in order in the relocation table.
539
540 In order to implement this, we keep track of each unmatched HI
541 relocation. We then sort them so that they immediately precede the
bdaaa2e1 542 corresponding LO relocation. */
252b5132 543
e972090a
NC
544struct mips_hi_fixup
545{
252b5132
RH
546 /* Next HI fixup. */
547 struct mips_hi_fixup *next;
548 /* This fixup. */
549 fixS *fixp;
550 /* The section this fixup is in. */
551 segT seg;
552};
553
554/* The list of unmatched HI relocs. */
555
556static struct mips_hi_fixup *mips_hi_fixup_list;
557
558/* Map normal MIPS register numbers to mips16 register numbers. */
559
560#define X ILLEGAL_REG
e972090a
NC
561static const int mips32_to_16_reg_map[] =
562{
252b5132
RH
563 X, X, 2, 3, 4, 5, 6, 7,
564 X, X, X, X, X, X, X, X,
565 0, 1, X, X, X, X, X, X,
566 X, X, X, X, X, X, X, X
567};
568#undef X
569
570/* Map mips16 register numbers to normal MIPS register numbers. */
571
e972090a
NC
572static const unsigned int mips16_to_32_reg_map[] =
573{
252b5132
RH
574 16, 17, 2, 3, 4, 5, 6, 7
575};
60b63b72
RS
576
577static int mips_fix_4122_bugs;
4a6a3df4
AO
578
579/* We don't relax branches by default, since this causes us to expand
580 `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
581 fail to compute the offset before expanding the macro to the most
582 efficient expansion. */
583
584static int mips_relax_branch;
252b5132
RH
585\f
586/* Since the MIPS does not have multiple forms of PC relative
587 instructions, we do not have to do relaxing as is done on other
588 platforms. However, we do have to handle GP relative addressing
589 correctly, which turns out to be a similar problem.
590
591 Every macro that refers to a symbol can occur in (at least) two
592 forms, one with GP relative addressing and one without. For
593 example, loading a global variable into a register generally uses
594 a macro instruction like this:
595 lw $4,i
596 If i can be addressed off the GP register (this is true if it is in
597 the .sbss or .sdata section, or if it is known to be smaller than
598 the -G argument) this will generate the following instruction:
599 lw $4,i($gp)
600 This instruction will use a GPREL reloc. If i can not be addressed
601 off the GP register, the following instruction sequence will be used:
602 lui $at,i
603 lw $4,i($at)
604 In this case the first instruction will have a HI16 reloc, and the
605 second reloc will have a LO16 reloc. Both relocs will be against
606 the symbol i.
607
608 The issue here is that we may not know whether i is GP addressable
609 until after we see the instruction that uses it. Therefore, we
610 want to be able to choose the final instruction sequence only at
611 the end of the assembly. This is similar to the way other
612 platforms choose the size of a PC relative instruction only at the
613 end of assembly.
614
615 When generating position independent code we do not use GP
616 addressing in quite the same way, but the issue still arises as
617 external symbols and local symbols must be handled differently.
618
619 We handle these issues by actually generating both possible
620 instruction sequences. The longer one is put in a frag_var with
621 type rs_machine_dependent. We encode what to do with the frag in
622 the subtype field. We encode (1) the number of existing bytes to
623 replace, (2) the number of new bytes to use, (3) the offset from
624 the start of the existing bytes to the first reloc we must generate
625 (that is, the offset is applied from the start of the existing
626 bytes after they are replaced by the new bytes, if any), (4) the
627 offset from the start of the existing bytes to the second reloc,
628 (5) whether a third reloc is needed (the third reloc is always four
629 bytes after the second reloc), and (6) whether to warn if this
630 variant is used (this is sometimes needed if .set nomacro or .set
631 noat is in effect). All these numbers are reasonably small.
632
633 Generating two instruction sequences must be handled carefully to
634 ensure that delay slots are handled correctly. Fortunately, there
635 are a limited number of cases. When the second instruction
636 sequence is generated, append_insn is directed to maintain the
637 existing delay slot information, so it continues to apply to any
638 code after the second instruction sequence. This means that the
639 second instruction sequence must not impose any requirements not
640 required by the first instruction sequence.
641
642 These variant frags are then handled in functions called by the
643 machine independent code. md_estimate_size_before_relax returns
644 the final size of the frag. md_convert_frag sets up the final form
645 of the frag. tc_gen_reloc adjust the first reloc and adds a second
646 one if needed. */
647#define RELAX_ENCODE(old, new, reloc1, reloc2, reloc3, warn) \
648 ((relax_substateT) \
649 (((old) << 23) \
650 | ((new) << 16) \
651 | (((reloc1) + 64) << 9) \
652 | (((reloc2) + 64) << 2) \
653 | ((reloc3) ? (1 << 1) : 0) \
654 | ((warn) ? 1 : 0)))
655#define RELAX_OLD(i) (((i) >> 23) & 0x7f)
656#define RELAX_NEW(i) (((i) >> 16) & 0x7f)
9a41af64
TS
657#define RELAX_RELOC1(i) ((valueT) (((i) >> 9) & 0x7f) - 64)
658#define RELAX_RELOC2(i) ((valueT) (((i) >> 2) & 0x7f) - 64)
252b5132
RH
659#define RELAX_RELOC3(i) (((i) >> 1) & 1)
660#define RELAX_WARN(i) ((i) & 1)
661
4a6a3df4
AO
662/* Branch without likely bit. If label is out of range, we turn:
663
664 beq reg1, reg2, label
665 delay slot
666
667 into
668
669 bne reg1, reg2, 0f
670 nop
671 j label
672 0: delay slot
673
674 with the following opcode replacements:
675
676 beq <-> bne
677 blez <-> bgtz
678 bltz <-> bgez
679 bc1f <-> bc1t
680
681 bltzal <-> bgezal (with jal label instead of j label)
682
683 Even though keeping the delay slot instruction in the delay slot of
684 the branch would be more efficient, it would be very tricky to do
685 correctly, because we'd have to introduce a variable frag *after*
686 the delay slot instruction, and expand that instead. Let's do it
687 the easy way for now, even if the branch-not-taken case now costs
688 one additional instruction. Out-of-range branches are not supposed
689 to be common, anyway.
690
691 Branch likely. If label is out of range, we turn:
692
693 beql reg1, reg2, label
694 delay slot (annulled if branch not taken)
695
696 into
697
698 beql reg1, reg2, 1f
699 nop
700 beql $0, $0, 2f
701 nop
702 1: j[al] label
703 delay slot (executed only if branch taken)
704 2:
705
706 It would be possible to generate a shorter sequence by losing the
707 likely bit, generating something like:
b34976b6 708
4a6a3df4
AO
709 bne reg1, reg2, 0f
710 nop
711 j[al] label
712 delay slot (executed only if branch taken)
713 0:
714
715 beql -> bne
716 bnel -> beq
717 blezl -> bgtz
718 bgtzl -> blez
719 bltzl -> bgez
720 bgezl -> bltz
721 bc1fl -> bc1t
722 bc1tl -> bc1f
723
724 bltzall -> bgezal (with jal label instead of j label)
725 bgezall -> bltzal (ditto)
726
727
728 but it's not clear that it would actually improve performance. */
af6ae2ad 729#define RELAX_BRANCH_ENCODE(uncond, likely, link, toofar) \
4a6a3df4
AO
730 ((relax_substateT) \
731 (0xc0000000 \
732 | ((toofar) ? 1 : 0) \
733 | ((link) ? 2 : 0) \
734 | ((likely) ? 4 : 0) \
af6ae2ad 735 | ((uncond) ? 8 : 0)))
4a6a3df4 736#define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
4a6a3df4
AO
737#define RELAX_BRANCH_UNCOND(i) (((i) & 8) != 0)
738#define RELAX_BRANCH_LIKELY(i) (((i) & 4) != 0)
739#define RELAX_BRANCH_LINK(i) (((i) & 2) != 0)
ae6063d4 740#define RELAX_BRANCH_TOOFAR(i) (((i) & 1) != 0)
4a6a3df4 741
252b5132
RH
742/* For mips16 code, we use an entirely different form of relaxation.
743 mips16 supports two versions of most instructions which take
744 immediate values: a small one which takes some small value, and a
745 larger one which takes a 16 bit value. Since branches also follow
746 this pattern, relaxing these values is required.
747
748 We can assemble both mips16 and normal MIPS code in a single
749 object. Therefore, we need to support this type of relaxation at
750 the same time that we support the relaxation described above. We
751 use the high bit of the subtype field to distinguish these cases.
752
753 The information we store for this type of relaxation is the
754 argument code found in the opcode file for this relocation, whether
755 the user explicitly requested a small or extended form, and whether
756 the relocation is in a jump or jal delay slot. That tells us the
757 size of the value, and how it should be stored. We also store
758 whether the fragment is considered to be extended or not. We also
759 store whether this is known to be a branch to a different section,
760 whether we have tried to relax this frag yet, and whether we have
761 ever extended a PC relative fragment because of a shift count. */
762#define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
763 (0x80000000 \
764 | ((type) & 0xff) \
765 | ((small) ? 0x100 : 0) \
766 | ((ext) ? 0x200 : 0) \
767 | ((dslot) ? 0x400 : 0) \
768 | ((jal_dslot) ? 0x800 : 0))
4a6a3df4 769#define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
252b5132
RH
770#define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
771#define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
772#define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
773#define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
774#define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
775#define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
776#define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
777#define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
778#define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
779#define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
780#define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
885add95
CD
781
782/* Is the given value a sign-extended 32-bit value? */
783#define IS_SEXT_32BIT_NUM(x) \
784 (((x) &~ (offsetT) 0x7fffffff) == 0 \
785 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
786
787/* Is the given value a sign-extended 16-bit value? */
788#define IS_SEXT_16BIT_NUM(x) \
789 (((x) &~ (offsetT) 0x7fff) == 0 \
790 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
791
252b5132
RH
792\f
793/* Prototypes for static functions. */
794
795#ifdef __STDC__
796#define internalError() \
797 as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
798#else
799#define internalError() as_fatal (_("MIPS internal Error"));
800#endif
801
802enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
803
b34976b6
AM
804static int insn_uses_reg
805 PARAMS ((struct mips_cl_insn *ip, unsigned int reg,
806 enum mips_regclass class));
807static int reg_needs_delay
808 PARAMS ((unsigned int));
809static void mips16_mark_labels
810 PARAMS ((void));
811static void append_insn
812 PARAMS ((char *place, struct mips_cl_insn * ip, expressionS * p,
5e0116d5 813 bfd_reloc_code_real_type *r));
b34976b6
AM
814static void mips_no_prev_insn
815 PARAMS ((int));
816static void mips_emit_delays
817 PARAMS ((bfd_boolean));
252b5132 818#ifdef USE_STDARG
b34976b6
AM
819static void macro_build
820 PARAMS ((char *place, int *counter, expressionS * ep, const char *name,
821 const char *fmt, ...));
252b5132
RH
822#else
823static void macro_build ();
824#endif
b34976b6
AM
825static void mips16_macro_build
826 PARAMS ((char *, int *, expressionS *, const char *, const char *, va_list));
827static void macro_build_jalr
828 PARAMS ((int, expressionS *));
829static void macro_build_lui
830 PARAMS ((char *place, int *counter, expressionS * ep, int regnum));
831static void macro_build_ldst_constoffset
832 PARAMS ((char *place, int *counter, expressionS * ep, const char *op,
833 int valreg, int breg));
834static void set_at
835 PARAMS ((int *counter, int reg, int unsignedp));
836static void check_absolute_expr
837 PARAMS ((struct mips_cl_insn * ip, expressionS *));
838static void load_register
839 PARAMS ((int *, int, expressionS *, int));
840static void load_address
841 PARAMS ((int *, int, expressionS *, int *));
842static void move_register
843 PARAMS ((int *, int, int));
844static void macro
845 PARAMS ((struct mips_cl_insn * ip));
846static void mips16_macro
847 PARAMS ((struct mips_cl_insn * ip));
252b5132 848#ifdef LOSING_COMPILER
b34976b6
AM
849static void macro2
850 PARAMS ((struct mips_cl_insn * ip));
252b5132 851#endif
b34976b6
AM
852static void mips_ip
853 PARAMS ((char *str, struct mips_cl_insn * ip));
854static void mips16_ip
855 PARAMS ((char *str, struct mips_cl_insn * ip));
856static void mips16_immed
857 PARAMS ((char *, unsigned int, int, offsetT, bfd_boolean, bfd_boolean,
858 bfd_boolean, unsigned long *, bfd_boolean *, unsigned short *));
5e0116d5
RS
859static bfd_boolean parse_relocation
860 PARAMS ((char **, bfd_reloc_code_real_type *));
861static size_t my_getSmallExpression
862 PARAMS ((expressionS *, bfd_reloc_code_real_type *, char *));
b34976b6
AM
863static void my_getExpression
864 PARAMS ((expressionS *, char *));
ae948b86 865#ifdef OBJ_ELF
b34976b6
AM
866static int support_64bit_objects
867 PARAMS((void));
ae948b86 868#endif
b34976b6
AM
869static void mips_set_option_string
870 PARAMS ((const char **, const char *));
871static symbolS *get_symbol
872 PARAMS ((void));
873static void mips_align
874 PARAMS ((int to, int fill, symbolS *label));
875static void s_align
876 PARAMS ((int));
877static void s_change_sec
878 PARAMS ((int));
879static void s_change_section
880 PARAMS ((int));
881static void s_cons
882 PARAMS ((int));
883static void s_float_cons
884 PARAMS ((int));
885static void s_mips_globl
886 PARAMS ((int));
887static void s_option
888 PARAMS ((int));
889static void s_mipsset
890 PARAMS ((int));
891static void s_abicalls
892 PARAMS ((int));
893static void s_cpload
894 PARAMS ((int));
895static void s_cpsetup
896 PARAMS ((int));
897static void s_cplocal
898 PARAMS ((int));
899static void s_cprestore
900 PARAMS ((int));
901static void s_cpreturn
902 PARAMS ((int));
903static void s_gpvalue
904 PARAMS ((int));
905static void s_gpword
906 PARAMS ((int));
907static void s_gpdword
908 PARAMS ((int));
909static void s_cpadd
910 PARAMS ((int));
911static void s_insn
912 PARAMS ((int));
913static void md_obj_begin
914 PARAMS ((void));
915static void md_obj_end
916 PARAMS ((void));
917static long get_number
918 PARAMS ((void));
919static void s_mips_ent
920 PARAMS ((int));
921static void s_mips_end
922 PARAMS ((int));
923static void s_mips_frame
924 PARAMS ((int));
925static void s_mips_mask
926 PARAMS ((int));
927static void s_mips_stab
928 PARAMS ((int));
929static void s_mips_weakext
930 PARAMS ((int));
931static void s_mips_file
932 PARAMS ((int));
933static void s_mips_loc
934 PARAMS ((int));
935static int mips16_extended_frag
936 PARAMS ((fragS *, asection *, long));
4a6a3df4 937static int relaxed_branch_length (fragS *, asection *, int);
b34976b6
AM
938static int validate_mips_insn
939 PARAMS ((const struct mips_opcode *));
940static void show
941 PARAMS ((FILE *, const char *, int *, int *));
add55e1f 942#ifdef OBJ_ELF
b34976b6
AM
943static int mips_need_elf_addend_fixup
944 PARAMS ((fixS *));
add55e1f 945#endif
e7af610e
NC
946
947/* Table and functions used to map between CPU/ISA names, and
948 ISA levels, and CPU numbers. */
949
e972090a
NC
950struct mips_cpu_info
951{
e7af610e
NC
952 const char *name; /* CPU or ISA name. */
953 int is_isa; /* Is this an ISA? (If 0, a CPU.) */
954 int isa; /* ISA level. */
955 int cpu; /* CPU number (default CPU if ISA). */
956};
957
b34976b6
AM
958static void mips_set_architecture
959 PARAMS ((const struct mips_cpu_info *));
960static void mips_set_tune
961 PARAMS ((const struct mips_cpu_info *));
962static bfd_boolean mips_strict_matching_cpu_name_p
963 PARAMS ((const char *, const char *));
964static bfd_boolean mips_matching_cpu_name_p
965 PARAMS ((const char *, const char *));
966static const struct mips_cpu_info *mips_parse_cpu
967 PARAMS ((const char *, const char *));
968static const struct mips_cpu_info *mips_cpu_info_from_isa
969 PARAMS ((int));
252b5132
RH
970\f
971/* Pseudo-op table.
972
973 The following pseudo-ops from the Kane and Heinrich MIPS book
974 should be defined here, but are currently unsupported: .alias,
975 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
976
977 The following pseudo-ops from the Kane and Heinrich MIPS book are
978 specific to the type of debugging information being generated, and
979 should be defined by the object format: .aent, .begin, .bend,
980 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
981 .vreg.
982
983 The following pseudo-ops from the Kane and Heinrich MIPS book are
984 not MIPS CPU specific, but are also not specific to the object file
985 format. This file is probably the best place to define them, but
986 they are not currently supported: .asm0, .endr, .lab, .repeat,
987 .struct. */
988
e972090a
NC
989static const pseudo_typeS mips_pseudo_table[] =
990{
beae10d5 991 /* MIPS specific pseudo-ops. */
252b5132
RH
992 {"option", s_option, 0},
993 {"set", s_mipsset, 0},
994 {"rdata", s_change_sec, 'r'},
995 {"sdata", s_change_sec, 's'},
996 {"livereg", s_ignore, 0},
997 {"abicalls", s_abicalls, 0},
998 {"cpload", s_cpload, 0},
6478892d
TS
999 {"cpsetup", s_cpsetup, 0},
1000 {"cplocal", s_cplocal, 0},
252b5132 1001 {"cprestore", s_cprestore, 0},
6478892d
TS
1002 {"cpreturn", s_cpreturn, 0},
1003 {"gpvalue", s_gpvalue, 0},
252b5132 1004 {"gpword", s_gpword, 0},
10181a0d 1005 {"gpdword", s_gpdword, 0},
252b5132
RH
1006 {"cpadd", s_cpadd, 0},
1007 {"insn", s_insn, 0},
1008
beae10d5 1009 /* Relatively generic pseudo-ops that happen to be used on MIPS
252b5132
RH
1010 chips. */
1011 {"asciiz", stringer, 1},
1012 {"bss", s_change_sec, 'b'},
1013 {"err", s_err, 0},
1014 {"half", s_cons, 1},
1015 {"dword", s_cons, 3},
1016 {"weakext", s_mips_weakext, 0},
1017
beae10d5 1018 /* These pseudo-ops are defined in read.c, but must be overridden
252b5132
RH
1019 here for one reason or another. */
1020 {"align", s_align, 0},
1021 {"byte", s_cons, 0},
1022 {"data", s_change_sec, 'd'},
1023 {"double", s_float_cons, 'd'},
1024 {"float", s_float_cons, 'f'},
1025 {"globl", s_mips_globl, 0},
1026 {"global", s_mips_globl, 0},
1027 {"hword", s_cons, 1},
1028 {"int", s_cons, 2},
1029 {"long", s_cons, 2},
1030 {"octa", s_cons, 4},
1031 {"quad", s_cons, 3},
cca86cc8 1032 {"section", s_change_section, 0},
252b5132
RH
1033 {"short", s_cons, 1},
1034 {"single", s_float_cons, 'f'},
1035 {"stabn", s_mips_stab, 'n'},
1036 {"text", s_change_sec, 't'},
1037 {"word", s_cons, 2},
add56521 1038
add56521 1039 { "extern", ecoff_directive_extern, 0},
add56521 1040
43841e91 1041 { NULL, NULL, 0 },
252b5132
RH
1042};
1043
e972090a
NC
1044static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1045{
beae10d5
KH
1046 /* These pseudo-ops should be defined by the object file format.
1047 However, a.out doesn't support them, so we have versions here. */
252b5132
RH
1048 {"aent", s_mips_ent, 1},
1049 {"bgnb", s_ignore, 0},
1050 {"end", s_mips_end, 0},
1051 {"endb", s_ignore, 0},
1052 {"ent", s_mips_ent, 0},
c5dd6aab 1053 {"file", s_mips_file, 0},
252b5132
RH
1054 {"fmask", s_mips_mask, 'F'},
1055 {"frame", s_mips_frame, 0},
c5dd6aab 1056 {"loc", s_mips_loc, 0},
252b5132
RH
1057 {"mask", s_mips_mask, 'R'},
1058 {"verstamp", s_ignore, 0},
43841e91 1059 { NULL, NULL, 0 },
252b5132
RH
1060};
1061
1062extern void pop_insert PARAMS ((const pseudo_typeS *));
1063
1064void
1065mips_pop_insert ()
1066{
1067 pop_insert (mips_pseudo_table);
1068 if (! ECOFF_DEBUGGING)
1069 pop_insert (mips_nonecoff_pseudo_table);
1070}
1071\f
1072/* Symbols labelling the current insn. */
1073
e972090a
NC
1074struct insn_label_list
1075{
252b5132
RH
1076 struct insn_label_list *next;
1077 symbolS *label;
1078};
1079
1080static struct insn_label_list *insn_labels;
1081static struct insn_label_list *free_insn_labels;
1082
1083static void mips_clear_insn_labels PARAMS ((void));
1084
1085static inline void
1086mips_clear_insn_labels ()
1087{
1088 register struct insn_label_list **pl;
1089
1090 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1091 ;
1092 *pl = insn_labels;
1093 insn_labels = NULL;
1094}
1095\f
1096static char *expr_end;
1097
1098/* Expressions which appear in instructions. These are set by
1099 mips_ip. */
1100
1101static expressionS imm_expr;
1102static expressionS offset_expr;
1103
1104/* Relocs associated with imm_expr and offset_expr. */
1105
f6688943
TS
1106static bfd_reloc_code_real_type imm_reloc[3]
1107 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1108static bfd_reloc_code_real_type offset_reloc[3]
1109 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 1110
252b5132
RH
1111/* These are set by mips16_ip if an explicit extension is used. */
1112
b34976b6 1113static bfd_boolean mips16_small, mips16_ext;
252b5132 1114
7ed4a06a 1115#ifdef OBJ_ELF
ecb4347a
DJ
1116/* The pdr segment for per procedure frame/regmask info. Not used for
1117 ECOFF debugging. */
252b5132
RH
1118
1119static segT pdr_seg;
7ed4a06a 1120#endif
252b5132 1121
e013f690
TS
1122/* The default target format to use. */
1123
1124const char *
1125mips_target_format ()
1126{
1127 switch (OUTPUT_FLAVOR)
1128 {
1129 case bfd_target_aout_flavour:
1130 return target_big_endian ? "a.out-mips-big" : "a.out-mips-little";
1131 case bfd_target_ecoff_flavour:
1132 return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
1133 case bfd_target_coff_flavour:
1134 return "pe-mips";
1135 case bfd_target_elf_flavour:
1136#ifdef TE_TMIPS
cfe86eaa 1137 /* This is traditional mips. */
e013f690 1138 return (target_big_endian
cfe86eaa
TS
1139 ? (HAVE_64BIT_OBJECTS
1140 ? "elf64-tradbigmips"
1141 : (HAVE_NEWABI
1142 ? "elf32-ntradbigmips" : "elf32-tradbigmips"))
1143 : (HAVE_64BIT_OBJECTS
1144 ? "elf64-tradlittlemips"
1145 : (HAVE_NEWABI
1146 ? "elf32-ntradlittlemips" : "elf32-tradlittlemips")));
e013f690
TS
1147#else
1148 return (target_big_endian
cfe86eaa
TS
1149 ? (HAVE_64BIT_OBJECTS
1150 ? "elf64-bigmips"
1151 : (HAVE_NEWABI
1152 ? "elf32-nbigmips" : "elf32-bigmips"))
1153 : (HAVE_64BIT_OBJECTS
1154 ? "elf64-littlemips"
1155 : (HAVE_NEWABI
1156 ? "elf32-nlittlemips" : "elf32-littlemips")));
e013f690
TS
1157#endif
1158 default:
1159 abort ();
1160 return NULL;
1161 }
1162}
1163
156c2f8b
NC
1164/* This function is called once, at assembler startup time. It should
1165 set up all the tables, etc. that the MD part of the assembler will need. */
1166
252b5132
RH
1167void
1168md_begin ()
1169{
252b5132 1170 register const char *retval = NULL;
156c2f8b 1171 int i = 0;
252b5132 1172 int broken = 0;
1f25f5d3 1173
ec68c924 1174 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, mips_arch))
252b5132
RH
1175 as_warn (_("Could not set architecture and machine"));
1176
252b5132
RH
1177 op_hash = hash_new ();
1178
1179 for (i = 0; i < NUMOPCODES;)
1180 {
1181 const char *name = mips_opcodes[i].name;
1182
1183 retval = hash_insert (op_hash, name, (PTR) &mips_opcodes[i]);
1184 if (retval != NULL)
1185 {
1186 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1187 mips_opcodes[i].name, retval);
1188 /* Probably a memory allocation problem? Give up now. */
1189 as_fatal (_("Broken assembler. No assembly attempted."));
1190 }
1191 do
1192 {
1193 if (mips_opcodes[i].pinfo != INSN_MACRO)
1194 {
1195 if (!validate_mips_insn (&mips_opcodes[i]))
1196 broken = 1;
1197 }
1198 ++i;
1199 }
1200 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1201 }
1202
1203 mips16_op_hash = hash_new ();
1204
1205 i = 0;
1206 while (i < bfd_mips16_num_opcodes)
1207 {
1208 const char *name = mips16_opcodes[i].name;
1209
1210 retval = hash_insert (mips16_op_hash, name, (PTR) &mips16_opcodes[i]);
1211 if (retval != NULL)
1212 as_fatal (_("internal: can't hash `%s': %s"),
1213 mips16_opcodes[i].name, retval);
1214 do
1215 {
1216 if (mips16_opcodes[i].pinfo != INSN_MACRO
1217 && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1218 != mips16_opcodes[i].match))
1219 {
1220 fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1221 mips16_opcodes[i].name, mips16_opcodes[i].args);
1222 broken = 1;
1223 }
1224 ++i;
1225 }
1226 while (i < bfd_mips16_num_opcodes
1227 && strcmp (mips16_opcodes[i].name, name) == 0);
1228 }
1229
1230 if (broken)
1231 as_fatal (_("Broken assembler. No assembly attempted."));
1232
1233 /* We add all the general register names to the symbol table. This
1234 helps us detect invalid uses of them. */
1235 for (i = 0; i < 32; i++)
1236 {
1237 char buf[5];
1238
1239 sprintf (buf, "$%d", i);
1240 symbol_table_insert (symbol_new (buf, reg_section, i,
1241 &zero_address_frag));
1242 }
76db943d
TS
1243 symbol_table_insert (symbol_new ("$ra", reg_section, RA,
1244 &zero_address_frag));
252b5132
RH
1245 symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1246 &zero_address_frag));
1247 symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1248 &zero_address_frag));
1249 symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1250 &zero_address_frag));
1251 symbol_table_insert (symbol_new ("$at", reg_section, AT,
1252 &zero_address_frag));
1253 symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1254 &zero_address_frag));
1255 symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1256 &zero_address_frag));
85b51719
TS
1257 symbol_table_insert (symbol_new ("$zero", reg_section, ZERO,
1258 &zero_address_frag));
252b5132
RH
1259 symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1260 &zero_address_frag));
1261
6047c971
AO
1262 /* If we don't add these register names to the symbol table, they
1263 may end up being added as regular symbols by operand(), and then
1264 make it to the object file as undefined in case they're not
1265 regarded as local symbols. They're local in o32, since `$' is a
1266 local symbol prefix, but not in n32 or n64. */
1267 for (i = 0; i < 8; i++)
1268 {
1269 char buf[6];
1270
1271 sprintf (buf, "$fcc%i", i);
1272 symbol_table_insert (symbol_new (buf, reg_section, -1,
1273 &zero_address_frag));
1274 }
1275
b34976b6 1276 mips_no_prev_insn (FALSE);
252b5132
RH
1277
1278 mips_gprmask = 0;
1279 mips_cprmask[0] = 0;
1280 mips_cprmask[1] = 0;
1281 mips_cprmask[2] = 0;
1282 mips_cprmask[3] = 0;
1283
1284 /* set the default alignment for the text section (2**2) */
1285 record_alignment (text_section, 2);
1286
1287 if (USE_GLOBAL_POINTER_OPT)
1288 bfd_set_gp_size (stdoutput, g_switch_value);
1289
1290 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1291 {
1292 /* On a native system, sections must be aligned to 16 byte
1293 boundaries. When configured for an embedded ELF target, we
1294 don't bother. */
1295 if (strcmp (TARGET_OS, "elf") != 0)
1296 {
1297 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1298 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1299 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1300 }
1301
1302 /* Create a .reginfo section for register masks and a .mdebug
1303 section for debugging information. */
1304 {
1305 segT seg;
1306 subsegT subseg;
1307 flagword flags;
1308 segT sec;
1309
1310 seg = now_seg;
1311 subseg = now_subseg;
1312
1313 /* The ABI says this section should be loaded so that the
1314 running program can access it. However, we don't load it
1315 if we are configured for an embedded target */
1316 flags = SEC_READONLY | SEC_DATA;
1317 if (strcmp (TARGET_OS, "elf") != 0)
1318 flags |= SEC_ALLOC | SEC_LOAD;
1319
316f5878 1320 if (mips_abi != N64_ABI)
252b5132
RH
1321 {
1322 sec = subseg_new (".reginfo", (subsegT) 0);
1323
195325d2
TS
1324 bfd_set_section_flags (stdoutput, sec, flags);
1325 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
bdaaa2e1 1326
252b5132
RH
1327#ifdef OBJ_ELF
1328 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1329#endif
1330 }
1331 else
1332 {
1333 /* The 64-bit ABI uses a .MIPS.options section rather than
1334 .reginfo section. */
1335 sec = subseg_new (".MIPS.options", (subsegT) 0);
195325d2
TS
1336 bfd_set_section_flags (stdoutput, sec, flags);
1337 bfd_set_section_alignment (stdoutput, sec, 3);
252b5132
RH
1338
1339#ifdef OBJ_ELF
1340 /* Set up the option header. */
1341 {
1342 Elf_Internal_Options opthdr;
1343 char *f;
1344
1345 opthdr.kind = ODK_REGINFO;
1346 opthdr.size = (sizeof (Elf_External_Options)
1347 + sizeof (Elf64_External_RegInfo));
1348 opthdr.section = 0;
1349 opthdr.info = 0;
1350 f = frag_more (sizeof (Elf_External_Options));
1351 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1352 (Elf_External_Options *) f);
1353
1354 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1355 }
1356#endif
1357 }
1358
1359 if (ECOFF_DEBUGGING)
1360 {
1361 sec = subseg_new (".mdebug", (subsegT) 0);
1362 (void) bfd_set_section_flags (stdoutput, sec,
1363 SEC_HAS_CONTENTS | SEC_READONLY);
1364 (void) bfd_set_section_alignment (stdoutput, sec, 2);
1365 }
ecb4347a
DJ
1366#ifdef OBJ_ELF
1367 else if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1368 {
1369 pdr_seg = subseg_new (".pdr", (subsegT) 0);
1370 (void) bfd_set_section_flags (stdoutput, pdr_seg,
1371 SEC_READONLY | SEC_RELOC
1372 | SEC_DEBUGGING);
1373 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1374 }
252b5132
RH
1375#endif
1376
1377 subseg_set (seg, subseg);
1378 }
1379 }
1380
1381 if (! ECOFF_DEBUGGING)
1382 md_obj_begin ();
1383}
1384
1385void
1386md_mips_end ()
1387{
1388 if (! ECOFF_DEBUGGING)
1389 md_obj_end ();
1390}
1391
1392void
1393md_assemble (str)
1394 char *str;
1395{
1396 struct mips_cl_insn insn;
f6688943
TS
1397 bfd_reloc_code_real_type unused_reloc[3]
1398 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132
RH
1399
1400 imm_expr.X_op = O_absent;
252b5132 1401 offset_expr.X_op = O_absent;
f6688943
TS
1402 imm_reloc[0] = BFD_RELOC_UNUSED;
1403 imm_reloc[1] = BFD_RELOC_UNUSED;
1404 imm_reloc[2] = BFD_RELOC_UNUSED;
1405 offset_reloc[0] = BFD_RELOC_UNUSED;
1406 offset_reloc[1] = BFD_RELOC_UNUSED;
1407 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
1408
1409 if (mips_opts.mips16)
1410 mips16_ip (str, &insn);
1411 else
1412 {
1413 mips_ip (str, &insn);
beae10d5
KH
1414 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1415 str, insn.insn_opcode));
252b5132
RH
1416 }
1417
1418 if (insn_error)
1419 {
1420 as_bad ("%s `%s'", insn_error, str);
1421 return;
1422 }
1423
1424 if (insn.insn_mo->pinfo == INSN_MACRO)
1425 {
1426 if (mips_opts.mips16)
1427 mips16_macro (&insn);
1428 else
1429 macro (&insn);
1430 }
1431 else
1432 {
1433 if (imm_expr.X_op != O_absent)
5e0116d5 1434 append_insn (NULL, &insn, &imm_expr, imm_reloc);
252b5132 1435 else if (offset_expr.X_op != O_absent)
5e0116d5 1436 append_insn (NULL, &insn, &offset_expr, offset_reloc);
252b5132 1437 else
5e0116d5 1438 append_insn (NULL, &insn, NULL, unused_reloc);
252b5132
RH
1439 }
1440}
1441
1442/* See whether instruction IP reads register REG. CLASS is the type
1443 of register. */
1444
1445static int
1446insn_uses_reg (ip, reg, class)
1447 struct mips_cl_insn *ip;
1448 unsigned int reg;
1449 enum mips_regclass class;
1450{
1451 if (class == MIPS16_REG)
1452 {
1453 assert (mips_opts.mips16);
1454 reg = mips16_to_32_reg_map[reg];
1455 class = MIPS_GR_REG;
1456 }
1457
85b51719
TS
1458 /* Don't report on general register ZERO, since it never changes. */
1459 if (class == MIPS_GR_REG && reg == ZERO)
252b5132
RH
1460 return 0;
1461
1462 if (class == MIPS_FP_REG)
1463 {
1464 assert (! mips_opts.mips16);
1465 /* If we are called with either $f0 or $f1, we must check $f0.
1466 This is not optimal, because it will introduce an unnecessary
1467 NOP between "lwc1 $f0" and "swc1 $f1". To fix this we would
1468 need to distinguish reading both $f0 and $f1 or just one of
1469 them. Note that we don't have to check the other way,
1470 because there is no instruction that sets both $f0 and $f1
1471 and requires a delay. */
1472 if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1473 && ((((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS) &~(unsigned)1)
1474 == (reg &~ (unsigned) 1)))
1475 return 1;
1476 if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1477 && ((((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT) &~(unsigned)1)
1478 == (reg &~ (unsigned) 1)))
1479 return 1;
1480 }
1481 else if (! mips_opts.mips16)
1482 {
1483 if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1484 && ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == reg)
1485 return 1;
1486 if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1487 && ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT) == reg)
1488 return 1;
1489 }
1490 else
1491 {
1492 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1493 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RX)
1494 & MIPS16OP_MASK_RX)]
1495 == reg))
1496 return 1;
1497 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1498 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RY)
1499 & MIPS16OP_MASK_RY)]
1500 == reg))
1501 return 1;
1502 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1503 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
1504 & MIPS16OP_MASK_MOVE32Z)]
1505 == reg))
1506 return 1;
1507 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1508 return 1;
1509 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1510 return 1;
1511 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1512 return 1;
1513 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1514 && ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
1515 & MIPS16OP_MASK_REGR32) == reg)
1516 return 1;
1517 }
1518
1519 return 0;
1520}
1521
1522/* This function returns true if modifying a register requires a
1523 delay. */
1524
1525static int
1526reg_needs_delay (reg)
156c2f8b 1527 unsigned int reg;
252b5132
RH
1528{
1529 unsigned long prev_pinfo;
1530
1531 prev_pinfo = prev_insn.insn_mo->pinfo;
1532 if (! mips_opts.noreorder
9ce8a5dd 1533 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
1534 && ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1535 || (! gpr_interlocks
1536 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1537 {
1538 /* A load from a coprocessor or from memory. All load
1539 delays delay the use of general register rt for one
1540 instruction on the r3000. The r6000 and r4000 use
1541 interlocks. */
bdaaa2e1 1542 /* Itbl support may require additional care here. */
252b5132
RH
1543 know (prev_pinfo & INSN_WRITE_GPR_T);
1544 if (reg == ((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT))
1545 return 1;
1546 }
1547
1548 return 0;
1549}
1550
1551/* Mark instruction labels in mips16 mode. This permits the linker to
1552 handle them specially, such as generating jalx instructions when
1553 needed. We also make them odd for the duration of the assembly, in
1554 order to generate the right sort of code. We will make them even
1555 in the adjust_symtab routine, while leaving them marked. This is
1556 convenient for the debugger and the disassembler. The linker knows
1557 to make them odd again. */
1558
1559static void
1560mips16_mark_labels ()
1561{
1562 if (mips_opts.mips16)
1563 {
1564 struct insn_label_list *l;
98aa84af 1565 valueT val;
252b5132
RH
1566
1567 for (l = insn_labels; l != NULL; l = l->next)
1568 {
1569#ifdef OBJ_ELF
1570 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1571 S_SET_OTHER (l->label, STO_MIPS16);
1572#endif
98aa84af
AM
1573 val = S_GET_VALUE (l->label);
1574 if ((val & 1) == 0)
1575 S_SET_VALUE (l->label, val + 1);
252b5132
RH
1576 }
1577 }
1578}
1579
1580/* Output an instruction. PLACE is where to put the instruction; if
1581 it is NULL, this uses frag_more to get room. IP is the instruction
1582 information. ADDRESS_EXPR is an operand of the instruction to be
1583 used with RELOC_TYPE. */
1584
1585static void
5e0116d5 1586append_insn (place, ip, address_expr, reloc_type)
252b5132
RH
1587 char *place;
1588 struct mips_cl_insn *ip;
1589 expressionS *address_expr;
f6688943 1590 bfd_reloc_code_real_type *reloc_type;
252b5132
RH
1591{
1592 register unsigned long prev_pinfo, pinfo;
1593 char *f;
f6688943 1594 fixS *fixp[3];
252b5132 1595 int nops = 0;
5e0116d5 1596 bfd_boolean unmatched_reloc_p;
252b5132
RH
1597
1598 /* Mark instruction labels in mips16 mode. */
f9419b05 1599 mips16_mark_labels ();
252b5132
RH
1600
1601 prev_pinfo = prev_insn.insn_mo->pinfo;
1602 pinfo = ip->insn_mo->pinfo;
5e0116d5 1603 unmatched_reloc_p = FALSE;
252b5132
RH
1604
1605 if (place == NULL && (! mips_opts.noreorder || prev_nop_frag != NULL))
1606 {
1607 int prev_prev_nop;
1608
1609 /* If the previous insn required any delay slots, see if we need
1610 to insert a NOP or two. There are eight kinds of possible
1611 hazards, of which an instruction can have at most one type.
1612 (1) a load from memory delay
1613 (2) a load from a coprocessor delay
1614 (3) an unconditional branch delay
1615 (4) a conditional branch delay
1616 (5) a move to coprocessor register delay
1617 (6) a load coprocessor register from memory delay
1618 (7) a coprocessor condition code delay
1619 (8) a HI/LO special register delay
1620
1621 There are a lot of optimizations we could do that we don't.
1622 In particular, we do not, in general, reorder instructions.
1623 If you use gcc with optimization, it will reorder
1624 instructions and generally do much more optimization then we
1625 do here; repeating all that work in the assembler would only
1626 benefit hand written assembly code, and does not seem worth
1627 it. */
1628
1629 /* This is how a NOP is emitted. */
1630#define emit_nop() \
1631 (mips_opts.mips16 \
1632 ? md_number_to_chars (frag_more (2), 0x6500, 2) \
1633 : md_number_to_chars (frag_more (4), 0, 4))
1634
1635 /* The previous insn might require a delay slot, depending upon
1636 the contents of the current insn. */
1637 if (! mips_opts.mips16
9ce8a5dd 1638 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
1639 && (((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1640 && ! cop_interlocks)
1641 || (! gpr_interlocks
1642 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1643 {
1644 /* A load from a coprocessor or from memory. All load
1645 delays delay the use of general register rt for one
1646 instruction on the r3000. The r6000 and r4000 use
1647 interlocks. */
beae10d5 1648 /* Itbl support may require additional care here. */
252b5132
RH
1649 know (prev_pinfo & INSN_WRITE_GPR_T);
1650 if (mips_optimize == 0
1651 || insn_uses_reg (ip,
1652 ((prev_insn.insn_opcode >> OP_SH_RT)
1653 & OP_MASK_RT),
1654 MIPS_GR_REG))
1655 ++nops;
1656 }
1657 else if (! mips_opts.mips16
9ce8a5dd 1658 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132 1659 && (((prev_pinfo & INSN_COPROC_MOVE_DELAY)
beae10d5 1660 && ! cop_interlocks)
e7af610e 1661 || (mips_opts.isa == ISA_MIPS1
252b5132
RH
1662 && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))))
1663 {
1664 /* A generic coprocessor delay. The previous instruction
1665 modified a coprocessor general or control register. If
1666 it modified a control register, we need to avoid any
1667 coprocessor instruction (this is probably not always
1668 required, but it sometimes is). If it modified a general
1669 register, we avoid using that register.
1670
1671 On the r6000 and r4000 loading a coprocessor register
1672 from memory is interlocked, and does not require a delay.
1673
1674 This case is not handled very well. There is no special
1675 knowledge of CP0 handling, and the coprocessors other
1676 than the floating point unit are not distinguished at
1677 all. */
1678 /* Itbl support may require additional care here. FIXME!
bdaaa2e1 1679 Need to modify this to include knowledge about
252b5132
RH
1680 user specified delays! */
1681 if (prev_pinfo & INSN_WRITE_FPR_T)
1682 {
1683 if (mips_optimize == 0
1684 || insn_uses_reg (ip,
1685 ((prev_insn.insn_opcode >> OP_SH_FT)
1686 & OP_MASK_FT),
1687 MIPS_FP_REG))
1688 ++nops;
1689 }
1690 else if (prev_pinfo & INSN_WRITE_FPR_S)
1691 {
1692 if (mips_optimize == 0
1693 || insn_uses_reg (ip,
1694 ((prev_insn.insn_opcode >> OP_SH_FS)
1695 & OP_MASK_FS),
1696 MIPS_FP_REG))
1697 ++nops;
1698 }
1699 else
1700 {
1701 /* We don't know exactly what the previous instruction
1702 does. If the current instruction uses a coprocessor
1703 register, we must insert a NOP. If previous
1704 instruction may set the condition codes, and the
1705 current instruction uses them, we must insert two
1706 NOPS. */
bdaaa2e1 1707 /* Itbl support may require additional care here. */
252b5132
RH
1708 if (mips_optimize == 0
1709 || ((prev_pinfo & INSN_WRITE_COND_CODE)
1710 && (pinfo & INSN_READ_COND_CODE)))
1711 nops += 2;
1712 else if (pinfo & INSN_COP)
1713 ++nops;
1714 }
1715 }
1716 else if (! mips_opts.mips16
9ce8a5dd 1717 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
1718 && (prev_pinfo & INSN_WRITE_COND_CODE)
1719 && ! cop_interlocks)
1720 {
1721 /* The previous instruction sets the coprocessor condition
1722 codes, but does not require a general coprocessor delay
1723 (this means it is a floating point comparison
1724 instruction). If this instruction uses the condition
1725 codes, we need to insert a single NOP. */
beae10d5 1726 /* Itbl support may require additional care here. */
252b5132
RH
1727 if (mips_optimize == 0
1728 || (pinfo & INSN_READ_COND_CODE))
1729 ++nops;
1730 }
6b76fefe
CM
1731
1732 /* If we're fixing up mfhi/mflo for the r7000 and the
1733 previous insn was an mfhi/mflo and the current insn
1734 reads the register that the mfhi/mflo wrote to, then
1735 insert two nops. */
1736
1737 else if (mips_7000_hilo_fix
1738 && MF_HILO_INSN (prev_pinfo)
1739 && insn_uses_reg (ip, ((prev_insn.insn_opcode >> OP_SH_RD)
beae10d5
KH
1740 & OP_MASK_RD),
1741 MIPS_GR_REG))
6b76fefe
CM
1742 {
1743 nops += 2;
1744 }
1745
1746 /* If we're fixing up mfhi/mflo for the r7000 and the
1747 2nd previous insn was an mfhi/mflo and the current insn
1748 reads the register that the mfhi/mflo wrote to, then
1749 insert one nop. */
1750
1751 else if (mips_7000_hilo_fix
1752 && MF_HILO_INSN (prev_prev_insn.insn_opcode)
1753 && insn_uses_reg (ip, ((prev_prev_insn.insn_opcode >> OP_SH_RD)
1754 & OP_MASK_RD),
1755 MIPS_GR_REG))
bdaaa2e1 1756
6b76fefe 1757 {
f9419b05 1758 ++nops;
6b76fefe 1759 }
bdaaa2e1 1760
252b5132
RH
1761 else if (prev_pinfo & INSN_READ_LO)
1762 {
1763 /* The previous instruction reads the LO register; if the
1764 current instruction writes to the LO register, we must
bdaaa2e1
KH
1765 insert two NOPS. Some newer processors have interlocks.
1766 Also the tx39's multiply instructions can be exectuted
252b5132 1767 immediatly after a read from HI/LO (without the delay),
bdaaa2e1
KH
1768 though the tx39's divide insns still do require the
1769 delay. */
252b5132 1770 if (! (hilo_interlocks
ec68c924 1771 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
252b5132
RH
1772 && (mips_optimize == 0
1773 || (pinfo & INSN_WRITE_LO)))
1774 nops += 2;
1775 /* Most mips16 branch insns don't have a delay slot.
1776 If a read from LO is immediately followed by a branch
1777 to a write to LO we have a read followed by a write
1778 less than 2 insns away. We assume the target of
1779 a branch might be a write to LO, and insert a nop
bdaaa2e1 1780 between a read and an immediately following branch. */
252b5132
RH
1781 else if (mips_opts.mips16
1782 && (mips_optimize == 0
1783 || (pinfo & MIPS16_INSN_BRANCH)))
f9419b05 1784 ++nops;
252b5132
RH
1785 }
1786 else if (prev_insn.insn_mo->pinfo & INSN_READ_HI)
1787 {
1788 /* The previous instruction reads the HI register; if the
1789 current instruction writes to the HI register, we must
1790 insert a NOP. Some newer processors have interlocks.
bdaaa2e1 1791 Also the note tx39's multiply above. */
252b5132 1792 if (! (hilo_interlocks
ec68c924 1793 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
252b5132
RH
1794 && (mips_optimize == 0
1795 || (pinfo & INSN_WRITE_HI)))
1796 nops += 2;
1797 /* Most mips16 branch insns don't have a delay slot.
1798 If a read from HI is immediately followed by a branch
1799 to a write to HI we have a read followed by a write
1800 less than 2 insns away. We assume the target of
1801 a branch might be a write to HI, and insert a nop
bdaaa2e1 1802 between a read and an immediately following branch. */
252b5132
RH
1803 else if (mips_opts.mips16
1804 && (mips_optimize == 0
1805 || (pinfo & MIPS16_INSN_BRANCH)))
f9419b05 1806 ++nops;
252b5132
RH
1807 }
1808
1809 /* If the previous instruction was in a noreorder section, then
1810 we don't want to insert the nop after all. */
bdaaa2e1 1811 /* Itbl support may require additional care here. */
252b5132
RH
1812 if (prev_insn_unreordered)
1813 nops = 0;
1814
1815 /* There are two cases which require two intervening
1816 instructions: 1) setting the condition codes using a move to
1817 coprocessor instruction which requires a general coprocessor
1818 delay and then reading the condition codes 2) reading the HI
1819 or LO register and then writing to it (except on processors
1820 which have interlocks). If we are not already emitting a NOP
1821 instruction, we must check for these cases compared to the
1822 instruction previous to the previous instruction. */
1823 if ((! mips_opts.mips16
9ce8a5dd 1824 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
1825 && (prev_prev_insn.insn_mo->pinfo & INSN_COPROC_MOVE_DELAY)
1826 && (prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
1827 && (pinfo & INSN_READ_COND_CODE)
1828 && ! cop_interlocks)
1829 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_LO)
1830 && (pinfo & INSN_WRITE_LO)
1831 && ! (hilo_interlocks
ec68c924 1832 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT))))
252b5132
RH
1833 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
1834 && (pinfo & INSN_WRITE_HI)
1835 && ! (hilo_interlocks
ec68c924 1836 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))))
252b5132
RH
1837 prev_prev_nop = 1;
1838 else
1839 prev_prev_nop = 0;
1840
1841 if (prev_prev_insn_unreordered)
1842 prev_prev_nop = 0;
1843
1844 if (prev_prev_nop && nops == 0)
1845 ++nops;
1846
60b63b72
RS
1847 if (mips_fix_4122_bugs && prev_insn.insn_mo->name)
1848 {
1849 /* We're out of bits in pinfo, so we must resort to string
1850 ops here. Shortcuts are selected based on opcodes being
1851 limited to the VR4122 instruction set. */
1852 int min_nops = 0;
1853 const char *pn = prev_insn.insn_mo->name;
1854 const char *tn = ip->insn_mo->name;
1855 if (strncmp(pn, "macc", 4) == 0
1856 || strncmp(pn, "dmacc", 5) == 0)
1857 {
1858 /* Errata 21 - [D]DIV[U] after [D]MACC */
1859 if (strstr (tn, "div"))
1860 {
1861 min_nops = 1;
1862 }
1863
1864 /* Errata 23 - Continuous DMULT[U]/DMACC instructions */
1865 if (pn[0] == 'd' /* dmacc */
1866 && (strncmp(tn, "dmult", 5) == 0
1867 || strncmp(tn, "dmacc", 5) == 0))
1868 {
1869 min_nops = 1;
1870 }
1871
1872 /* Errata 24 - MT{LO,HI} after [D]MACC */
1873 if (strcmp (tn, "mtlo") == 0
1874 || strcmp (tn, "mthi") == 0)
1875 {
1876 min_nops = 1;
1877 }
1878
1879 }
1880 else if (strncmp(pn, "dmult", 5) == 0
1881 && (strncmp(tn, "dmult", 5) == 0
1882 || strncmp(tn, "dmacc", 5) == 0))
1883 {
1884 /* Here is the rest of errata 23. */
1885 min_nops = 1;
1886 }
1887 if (nops < min_nops)
1888 nops = min_nops;
1889 }
1890
252b5132
RH
1891 /* If we are being given a nop instruction, don't bother with
1892 one of the nops we would otherwise output. This will only
1893 happen when a nop instruction is used with mips_optimize set
1894 to 0. */
1895 if (nops > 0
1896 && ! mips_opts.noreorder
156c2f8b 1897 && ip->insn_opcode == (unsigned) (mips_opts.mips16 ? 0x6500 : 0))
252b5132
RH
1898 --nops;
1899
1900 /* Now emit the right number of NOP instructions. */
1901 if (nops > 0 && ! mips_opts.noreorder)
1902 {
1903 fragS *old_frag;
1904 unsigned long old_frag_offset;
1905 int i;
1906 struct insn_label_list *l;
1907
1908 old_frag = frag_now;
1909 old_frag_offset = frag_now_fix ();
1910
1911 for (i = 0; i < nops; i++)
1912 emit_nop ();
1913
1914 if (listing)
1915 {
1916 listing_prev_line ();
1917 /* We may be at the start of a variant frag. In case we
1918 are, make sure there is enough space for the frag
1919 after the frags created by listing_prev_line. The
1920 argument to frag_grow here must be at least as large
1921 as the argument to all other calls to frag_grow in
1922 this file. We don't have to worry about being in the
1923 middle of a variant frag, because the variants insert
1924 all needed nop instructions themselves. */
1925 frag_grow (40);
1926 }
1927
1928 for (l = insn_labels; l != NULL; l = l->next)
1929 {
98aa84af
AM
1930 valueT val;
1931
252b5132 1932 assert (S_GET_SEGMENT (l->label) == now_seg);
49309057 1933 symbol_set_frag (l->label, frag_now);
98aa84af 1934 val = (valueT) frag_now_fix ();
252b5132
RH
1935 /* mips16 text labels are stored as odd. */
1936 if (mips_opts.mips16)
f9419b05 1937 ++val;
98aa84af 1938 S_SET_VALUE (l->label, val);
252b5132
RH
1939 }
1940
1941#ifndef NO_ECOFF_DEBUGGING
1942 if (ECOFF_DEBUGGING)
1943 ecoff_fix_loc (old_frag, old_frag_offset);
1944#endif
1945 }
1946 else if (prev_nop_frag != NULL)
1947 {
1948 /* We have a frag holding nops we may be able to remove. If
1949 we don't need any nops, we can decrease the size of
1950 prev_nop_frag by the size of one instruction. If we do
bdaaa2e1 1951 need some nops, we count them in prev_nops_required. */
252b5132
RH
1952 if (prev_nop_frag_since == 0)
1953 {
1954 if (nops == 0)
1955 {
1956 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1957 --prev_nop_frag_holds;
1958 }
1959 else
1960 prev_nop_frag_required += nops;
1961 }
1962 else
1963 {
1964 if (prev_prev_nop == 0)
1965 {
1966 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1967 --prev_nop_frag_holds;
1968 }
1969 else
1970 ++prev_nop_frag_required;
1971 }
1972
1973 if (prev_nop_frag_holds <= prev_nop_frag_required)
1974 prev_nop_frag = NULL;
1975
1976 ++prev_nop_frag_since;
1977
1978 /* Sanity check: by the time we reach the second instruction
1979 after prev_nop_frag, we should have used up all the nops
1980 one way or another. */
1981 assert (prev_nop_frag_since <= 1 || prev_nop_frag == NULL);
1982 }
1983 }
1984
4a6a3df4
AO
1985 if (place == NULL
1986 && address_expr
af6ae2ad 1987 && *reloc_type == BFD_RELOC_16_PCREL_S2
4a6a3df4
AO
1988 && (pinfo & INSN_UNCOND_BRANCH_DELAY || pinfo & INSN_COND_BRANCH_DELAY
1989 || pinfo & INSN_COND_BRANCH_LIKELY)
1990 && mips_relax_branch
1991 /* Don't try branch relaxation within .set nomacro, or within
1992 .set noat if we use $at for PIC computations. If it turns
1993 out that the branch was out-of-range, we'll get an error. */
1994 && !mips_opts.warn_about_macros
1995 && !(mips_opts.noat && mips_pic != NO_PIC)
1996 && !mips_opts.mips16)
1997 {
1998 f = frag_var (rs_machine_dependent,
1999 relaxed_branch_length
2000 (NULL, NULL,
2001 (pinfo & INSN_UNCOND_BRANCH_DELAY) ? -1
2002 : (pinfo & INSN_COND_BRANCH_LIKELY) ? 1 : 0), 4,
2003 RELAX_BRANCH_ENCODE
af6ae2ad 2004 (pinfo & INSN_UNCOND_BRANCH_DELAY,
4a6a3df4
AO
2005 pinfo & INSN_COND_BRANCH_LIKELY,
2006 pinfo & INSN_WRITE_GPR_31,
2007 0),
2008 address_expr->X_add_symbol,
2009 address_expr->X_add_number,
2010 0);
2011 *reloc_type = BFD_RELOC_UNUSED;
2012 }
2013 else if (*reloc_type > BFD_RELOC_UNUSED)
252b5132
RH
2014 {
2015 /* We need to set up a variant frag. */
2016 assert (mips_opts.mips16 && address_expr != NULL);
2017 f = frag_var (rs_machine_dependent, 4, 0,
f6688943 2018 RELAX_MIPS16_ENCODE (*reloc_type - BFD_RELOC_UNUSED,
252b5132
RH
2019 mips16_small, mips16_ext,
2020 (prev_pinfo
2021 & INSN_UNCOND_BRANCH_DELAY),
f6688943 2022 (*prev_insn_reloc_type
252b5132 2023 == BFD_RELOC_MIPS16_JMP)),
c4e7957c 2024 make_expr_symbol (address_expr), 0, NULL);
252b5132
RH
2025 }
2026 else if (place != NULL)
2027 f = place;
2028 else if (mips_opts.mips16
2029 && ! ip->use_extend
f6688943 2030 && *reloc_type != BFD_RELOC_MIPS16_JMP)
252b5132
RH
2031 {
2032 /* Make sure there is enough room to swap this instruction with
2033 a following jump instruction. */
2034 frag_grow (6);
2035 f = frag_more (2);
2036 }
2037 else
2038 {
2039 if (mips_opts.mips16
2040 && mips_opts.noreorder
2041 && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
2042 as_warn (_("extended instruction in delay slot"));
2043
2044 f = frag_more (4);
2045 }
2046
f6688943
TS
2047 fixp[0] = fixp[1] = fixp[2] = NULL;
2048 if (address_expr != NULL && *reloc_type < BFD_RELOC_UNUSED)
252b5132
RH
2049 {
2050 if (address_expr->X_op == O_constant)
2051 {
4db1a35d 2052 valueT tmp;
f6688943
TS
2053
2054 switch (*reloc_type)
252b5132
RH
2055 {
2056 case BFD_RELOC_32:
2057 ip->insn_opcode |= address_expr->X_add_number;
2058 break;
2059
f6688943
TS
2060 case BFD_RELOC_MIPS_HIGHEST:
2061 tmp = (address_expr->X_add_number + 0x800080008000) >> 16;
2062 tmp >>= 16;
2063 ip->insn_opcode |= (tmp >> 16) & 0xffff;
2064 break;
2065
2066 case BFD_RELOC_MIPS_HIGHER:
2067 tmp = (address_expr->X_add_number + 0x80008000) >> 16;
2068 ip->insn_opcode |= (tmp >> 16) & 0xffff;
2069 break;
2070
2071 case BFD_RELOC_HI16_S:
2072 ip->insn_opcode |= ((address_expr->X_add_number + 0x8000)
2073 >> 16) & 0xffff;
2074 break;
2075
2076 case BFD_RELOC_HI16:
2077 ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
2078 break;
2079
252b5132 2080 case BFD_RELOC_LO16:
ed6fb7bd 2081 case BFD_RELOC_MIPS_GOT_DISP:
252b5132
RH
2082 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2083 break;
2084
2085 case BFD_RELOC_MIPS_JMP:
2086 if ((address_expr->X_add_number & 3) != 0)
2087 as_bad (_("jump to misaligned address (0x%lx)"),
2088 (unsigned long) address_expr->X_add_number);
f3c0ec86 2089 if (address_expr->X_add_number & ~0xfffffff)
7496292d
TS
2090 as_bad (_("jump address range overflow (0x%lx)"),
2091 (unsigned long) address_expr->X_add_number);
252b5132
RH
2092 ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
2093 break;
2094
2095 case BFD_RELOC_MIPS16_JMP:
2096 if ((address_expr->X_add_number & 3) != 0)
2097 as_bad (_("jump to misaligned address (0x%lx)"),
2098 (unsigned long) address_expr->X_add_number);
f3c0ec86 2099 if (address_expr->X_add_number & ~0xfffffff)
7496292d
TS
2100 as_bad (_("jump address range overflow (0x%lx)"),
2101 (unsigned long) address_expr->X_add_number);
252b5132
RH
2102 ip->insn_opcode |=
2103 (((address_expr->X_add_number & 0x7c0000) << 3)
2104 | ((address_expr->X_add_number & 0xf800000) >> 7)
2105 | ((address_expr->X_add_number & 0x3fffc) >> 2));
2106 break;
2107
252b5132
RH
2108 case BFD_RELOC_16_PCREL_S2:
2109 goto need_reloc;
2110
2111 default:
2112 internalError ();
2113 }
2114 }
2115 else
2116 {
2117 need_reloc:
f6688943 2118 /* Don't generate a reloc if we are writing into a variant frag. */
252b5132
RH
2119 if (place == NULL)
2120 {
f6688943
TS
2121 fixp[0] = fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
2122 address_expr,
af6ae2ad 2123 *reloc_type == BFD_RELOC_16_PCREL_S2,
f6688943
TS
2124 reloc_type[0]);
2125
b6ff326e 2126 /* These relocations can have an addend that won't fit in
f6688943
TS
2127 4 octets for 64bit assembly. */
2128 if (HAVE_64BIT_GPRS &&
2129 (*reloc_type == BFD_RELOC_16
98d3f06f
KH
2130 || *reloc_type == BFD_RELOC_32
2131 || *reloc_type == BFD_RELOC_MIPS_JMP
2132 || *reloc_type == BFD_RELOC_HI16_S
2133 || *reloc_type == BFD_RELOC_LO16
2134 || *reloc_type == BFD_RELOC_GPREL16
2135 || *reloc_type == BFD_RELOC_MIPS_LITERAL
2136 || *reloc_type == BFD_RELOC_GPREL32
2137 || *reloc_type == BFD_RELOC_64
2138 || *reloc_type == BFD_RELOC_CTOR
2139 || *reloc_type == BFD_RELOC_MIPS_SUB
2140 || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2141 || *reloc_type == BFD_RELOC_MIPS_HIGHER
2142 || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2143 || *reloc_type == BFD_RELOC_MIPS_REL16
2144 || *reloc_type == BFD_RELOC_MIPS_RELGOT))
f6688943
TS
2145 fixp[0]->fx_no_overflow = 1;
2146
5e0116d5 2147 if (reloc_type[0] == BFD_RELOC_HI16_S)
252b5132
RH
2148 {
2149 struct mips_hi_fixup *hi_fixup;
2150
252b5132
RH
2151 hi_fixup = ((struct mips_hi_fixup *)
2152 xmalloc (sizeof (struct mips_hi_fixup)));
f6688943 2153 hi_fixup->fixp = fixp[0];
252b5132
RH
2154 hi_fixup->seg = now_seg;
2155 hi_fixup->next = mips_hi_fixup_list;
2156 mips_hi_fixup_list = hi_fixup;
5e0116d5 2157 unmatched_reloc_p = TRUE;
252b5132 2158 }
f6688943
TS
2159
2160 if (reloc_type[1] != BFD_RELOC_UNUSED)
2161 {
2162 /* FIXME: This symbol can be one of
2163 RSS_UNDEF, RSS_GP, RSS_GP0, RSS_LOC. */
2164 address_expr->X_op = O_absent;
2165 address_expr->X_add_symbol = 0;
2166 address_expr->X_add_number = 0;
2167
2168 fixp[1] = fix_new_exp (frag_now, f - frag_now->fr_literal,
b34976b6 2169 4, address_expr, FALSE,
f6688943
TS
2170 reloc_type[1]);
2171
b6ff326e 2172 /* These relocations can have an addend that won't fit in
f6688943
TS
2173 4 octets for 64bit assembly. */
2174 if (HAVE_64BIT_GPRS &&
2175 (*reloc_type == BFD_RELOC_16
2176 || *reloc_type == BFD_RELOC_32
2177 || *reloc_type == BFD_RELOC_MIPS_JMP
2178 || *reloc_type == BFD_RELOC_HI16_S
2179 || *reloc_type == BFD_RELOC_LO16
2180 || *reloc_type == BFD_RELOC_GPREL16
2181 || *reloc_type == BFD_RELOC_MIPS_LITERAL
2182 || *reloc_type == BFD_RELOC_GPREL32
2183 || *reloc_type == BFD_RELOC_64
2184 || *reloc_type == BFD_RELOC_CTOR
2185 || *reloc_type == BFD_RELOC_MIPS_SUB
2186 || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2187 || *reloc_type == BFD_RELOC_MIPS_HIGHER
2188 || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2189 || *reloc_type == BFD_RELOC_MIPS_REL16
2190 || *reloc_type == BFD_RELOC_MIPS_RELGOT))
98d3f06f 2191 fixp[1]->fx_no_overflow = 1;
f6688943
TS
2192
2193 if (reloc_type[2] != BFD_RELOC_UNUSED)
2194 {
2195 address_expr->X_op = O_absent;
2196 address_expr->X_add_symbol = 0;
2197 address_expr->X_add_number = 0;
2198
2199 fixp[2] = fix_new_exp (frag_now,
2200 f - frag_now->fr_literal, 4,
b34976b6 2201 address_expr, FALSE,
f6688943
TS
2202 reloc_type[2]);
2203
b6ff326e 2204 /* These relocations can have an addend that won't fit in
f6688943
TS
2205 4 octets for 64bit assembly. */
2206 if (HAVE_64BIT_GPRS &&
2207 (*reloc_type == BFD_RELOC_16
2208 || *reloc_type == BFD_RELOC_32
2209 || *reloc_type == BFD_RELOC_MIPS_JMP
2210 || *reloc_type == BFD_RELOC_HI16_S
2211 || *reloc_type == BFD_RELOC_LO16
2212 || *reloc_type == BFD_RELOC_GPREL16
2213 || *reloc_type == BFD_RELOC_MIPS_LITERAL
2214 || *reloc_type == BFD_RELOC_GPREL32
2215 || *reloc_type == BFD_RELOC_64
2216 || *reloc_type == BFD_RELOC_CTOR
2217 || *reloc_type == BFD_RELOC_MIPS_SUB
2218 || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2219 || *reloc_type == BFD_RELOC_MIPS_HIGHER
2220 || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2221 || *reloc_type == BFD_RELOC_MIPS_REL16
2222 || *reloc_type == BFD_RELOC_MIPS_RELGOT))
98d3f06f 2223 fixp[2]->fx_no_overflow = 1;
f6688943
TS
2224 }
2225 }
252b5132
RH
2226 }
2227 }
2228 }
2229
2230 if (! mips_opts.mips16)
c5dd6aab
DJ
2231 {
2232 md_number_to_chars (f, ip->insn_opcode, 4);
2233#ifdef OBJ_ELF
2234 dwarf2_emit_insn (4);
2235#endif
2236 }
f6688943 2237 else if (*reloc_type == BFD_RELOC_MIPS16_JMP)
252b5132
RH
2238 {
2239 md_number_to_chars (f, ip->insn_opcode >> 16, 2);
2240 md_number_to_chars (f + 2, ip->insn_opcode & 0xffff, 2);
c5dd6aab
DJ
2241#ifdef OBJ_ELF
2242 dwarf2_emit_insn (4);
2243#endif
252b5132
RH
2244 }
2245 else
2246 {
2247 if (ip->use_extend)
2248 {
2249 md_number_to_chars (f, 0xf000 | ip->extend, 2);
2250 f += 2;
2251 }
2252 md_number_to_chars (f, ip->insn_opcode, 2);
c5dd6aab
DJ
2253#ifdef OBJ_ELF
2254 dwarf2_emit_insn (ip->use_extend ? 4 : 2);
2255#endif
252b5132
RH
2256 }
2257
2258 /* Update the register mask information. */
2259 if (! mips_opts.mips16)
2260 {
2261 if (pinfo & INSN_WRITE_GPR_D)
2262 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
2263 if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2264 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
2265 if (pinfo & INSN_READ_GPR_S)
2266 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
2267 if (pinfo & INSN_WRITE_GPR_31)
f9419b05 2268 mips_gprmask |= 1 << RA;
252b5132
RH
2269 if (pinfo & INSN_WRITE_FPR_D)
2270 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
2271 if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2272 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
2273 if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2274 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
2275 if ((pinfo & INSN_READ_FPR_R) != 0)
2276 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
2277 if (pinfo & INSN_COP)
2278 {
bdaaa2e1
KH
2279 /* We don't keep enough information to sort these cases out.
2280 The itbl support does keep this information however, although
2281 we currently don't support itbl fprmats as part of the cop
2282 instruction. May want to add this support in the future. */
252b5132
RH
2283 }
2284 /* Never set the bit for $0, which is always zero. */
beae10d5 2285 mips_gprmask &= ~1 << 0;
252b5132
RH
2286 }
2287 else
2288 {
2289 if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2290 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RX)
2291 & MIPS16OP_MASK_RX);
2292 if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2293 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RY)
2294 & MIPS16OP_MASK_RY);
2295 if (pinfo & MIPS16_INSN_WRITE_Z)
2296 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RZ)
2297 & MIPS16OP_MASK_RZ);
2298 if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2299 mips_gprmask |= 1 << TREG;
2300 if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2301 mips_gprmask |= 1 << SP;
2302 if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2303 mips_gprmask |= 1 << RA;
2304 if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2305 mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2306 if (pinfo & MIPS16_INSN_READ_Z)
2307 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
2308 & MIPS16OP_MASK_MOVE32Z);
2309 if (pinfo & MIPS16_INSN_READ_GPR_X)
2310 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
2311 & MIPS16OP_MASK_REGR32);
2312 }
2313
2314 if (place == NULL && ! mips_opts.noreorder)
2315 {
2316 /* Filling the branch delay slot is more complex. We try to
2317 switch the branch with the previous instruction, which we can
2318 do if the previous instruction does not set up a condition
2319 that the branch tests and if the branch is not itself the
2320 target of any branch. */
2321 if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2322 || (pinfo & INSN_COND_BRANCH_DELAY))
2323 {
2324 if (mips_optimize < 2
2325 /* If we have seen .set volatile or .set nomove, don't
2326 optimize. */
2327 || mips_opts.nomove != 0
2328 /* If we had to emit any NOP instructions, then we
2329 already know we can not swap. */
2330 || nops != 0
2331 /* If we don't even know the previous insn, we can not
bdaaa2e1 2332 swap. */
252b5132
RH
2333 || ! prev_insn_valid
2334 /* If the previous insn is already in a branch delay
2335 slot, then we can not swap. */
2336 || prev_insn_is_delay_slot
2337 /* If the previous previous insn was in a .set
2338 noreorder, we can't swap. Actually, the MIPS
2339 assembler will swap in this situation. However, gcc
2340 configured -with-gnu-as will generate code like
2341 .set noreorder
2342 lw $4,XXX
2343 .set reorder
2344 INSN
2345 bne $4,$0,foo
2346 in which we can not swap the bne and INSN. If gcc is
2347 not configured -with-gnu-as, it does not output the
2348 .set pseudo-ops. We don't have to check
2349 prev_insn_unreordered, because prev_insn_valid will
2350 be 0 in that case. We don't want to use
2351 prev_prev_insn_valid, because we do want to be able
2352 to swap at the start of a function. */
2353 || prev_prev_insn_unreordered
2354 /* If the branch is itself the target of a branch, we
2355 can not swap. We cheat on this; all we check for is
2356 whether there is a label on this instruction. If
2357 there are any branches to anything other than a
2358 label, users must use .set noreorder. */
2359 || insn_labels != NULL
2360 /* If the previous instruction is in a variant frag, we
2361 can not do the swap. This does not apply to the
2362 mips16, which uses variant frags for different
2363 purposes. */
2364 || (! mips_opts.mips16
2365 && prev_insn_frag->fr_type == rs_machine_dependent)
2366 /* If the branch reads the condition codes, we don't
2367 even try to swap, because in the sequence
2368 ctc1 $X,$31
2369 INSN
2370 INSN
2371 bc1t LABEL
2372 we can not swap, and I don't feel like handling that
2373 case. */
2374 || (! mips_opts.mips16
9ce8a5dd 2375 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
2376 && (pinfo & INSN_READ_COND_CODE))
2377 /* We can not swap with an instruction that requires a
2378 delay slot, becase the target of the branch might
2379 interfere with that instruction. */
2380 || (! mips_opts.mips16
9ce8a5dd 2381 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132 2382 && (prev_pinfo
bdaaa2e1 2383 /* Itbl support may require additional care here. */
252b5132
RH
2384 & (INSN_LOAD_COPROC_DELAY
2385 | INSN_COPROC_MOVE_DELAY
2386 | INSN_WRITE_COND_CODE)))
2387 || (! (hilo_interlocks
ec68c924 2388 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
252b5132
RH
2389 && (prev_pinfo
2390 & (INSN_READ_LO
2391 | INSN_READ_HI)))
2392 || (! mips_opts.mips16
2393 && ! gpr_interlocks
2394 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))
2395 || (! mips_opts.mips16
e7af610e 2396 && mips_opts.isa == ISA_MIPS1
bdaaa2e1 2397 /* Itbl support may require additional care here. */
252b5132
RH
2398 && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))
2399 /* We can not swap with a branch instruction. */
2400 || (prev_pinfo
2401 & (INSN_UNCOND_BRANCH_DELAY
2402 | INSN_COND_BRANCH_DELAY
2403 | INSN_COND_BRANCH_LIKELY))
2404 /* We do not swap with a trap instruction, since it
2405 complicates trap handlers to have the trap
2406 instruction be in a delay slot. */
2407 || (prev_pinfo & INSN_TRAP)
2408 /* If the branch reads a register that the previous
2409 instruction sets, we can not swap. */
2410 || (! mips_opts.mips16
2411 && (prev_pinfo & INSN_WRITE_GPR_T)
2412 && insn_uses_reg (ip,
2413 ((prev_insn.insn_opcode >> OP_SH_RT)
2414 & OP_MASK_RT),
2415 MIPS_GR_REG))
2416 || (! mips_opts.mips16
2417 && (prev_pinfo & INSN_WRITE_GPR_D)
2418 && insn_uses_reg (ip,
2419 ((prev_insn.insn_opcode >> OP_SH_RD)
2420 & OP_MASK_RD),
2421 MIPS_GR_REG))
2422 || (mips_opts.mips16
2423 && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2424 && insn_uses_reg (ip,
2425 ((prev_insn.insn_opcode
2426 >> MIPS16OP_SH_RX)
2427 & MIPS16OP_MASK_RX),
2428 MIPS16_REG))
2429 || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2430 && insn_uses_reg (ip,
2431 ((prev_insn.insn_opcode
2432 >> MIPS16OP_SH_RY)
2433 & MIPS16OP_MASK_RY),
2434 MIPS16_REG))
2435 || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2436 && insn_uses_reg (ip,
2437 ((prev_insn.insn_opcode
2438 >> MIPS16OP_SH_RZ)
2439 & MIPS16OP_MASK_RZ),
2440 MIPS16_REG))
2441 || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2442 && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2443 || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2444 && insn_uses_reg (ip, RA, MIPS_GR_REG))
2445 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2446 && insn_uses_reg (ip,
2447 MIPS16OP_EXTRACT_REG32R (prev_insn.
2448 insn_opcode),
2449 MIPS_GR_REG))))
2450 /* If the branch writes a register that the previous
2451 instruction sets, we can not swap (we know that
2452 branches write only to RD or to $31). */
2453 || (! mips_opts.mips16
2454 && (prev_pinfo & INSN_WRITE_GPR_T)
2455 && (((pinfo & INSN_WRITE_GPR_D)
2456 && (((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT)
2457 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2458 || ((pinfo & INSN_WRITE_GPR_31)
2459 && (((prev_insn.insn_opcode >> OP_SH_RT)
2460 & OP_MASK_RT)
f9419b05 2461 == RA))))
252b5132
RH
2462 || (! mips_opts.mips16
2463 && (prev_pinfo & INSN_WRITE_GPR_D)
2464 && (((pinfo & INSN_WRITE_GPR_D)
2465 && (((prev_insn.insn_opcode >> OP_SH_RD) & OP_MASK_RD)
2466 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2467 || ((pinfo & INSN_WRITE_GPR_31)
2468 && (((prev_insn.insn_opcode >> OP_SH_RD)
2469 & OP_MASK_RD)
f9419b05 2470 == RA))))
252b5132
RH
2471 || (mips_opts.mips16
2472 && (pinfo & MIPS16_INSN_WRITE_31)
2473 && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2474 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2475 && (MIPS16OP_EXTRACT_REG32R (prev_insn.insn_opcode)
2476 == RA))))
2477 /* If the branch writes a register that the previous
2478 instruction reads, we can not swap (we know that
2479 branches only write to RD or to $31). */
2480 || (! mips_opts.mips16
2481 && (pinfo & INSN_WRITE_GPR_D)
2482 && insn_uses_reg (&prev_insn,
2483 ((ip->insn_opcode >> OP_SH_RD)
2484 & OP_MASK_RD),
2485 MIPS_GR_REG))
2486 || (! mips_opts.mips16
2487 && (pinfo & INSN_WRITE_GPR_31)
f9419b05 2488 && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
252b5132
RH
2489 || (mips_opts.mips16
2490 && (pinfo & MIPS16_INSN_WRITE_31)
2491 && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2492 /* If we are generating embedded PIC code, the branch
2493 might be expanded into a sequence which uses $at, so
2494 we can't swap with an instruction which reads it. */
2495 || (mips_pic == EMBEDDED_PIC
2496 && insn_uses_reg (&prev_insn, AT, MIPS_GR_REG))
2497 /* If the previous previous instruction has a load
2498 delay, and sets a register that the branch reads, we
2499 can not swap. */
2500 || (! mips_opts.mips16
9ce8a5dd 2501 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
bdaaa2e1 2502 /* Itbl support may require additional care here. */
252b5132
RH
2503 && ((prev_prev_insn.insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
2504 || (! gpr_interlocks
2505 && (prev_prev_insn.insn_mo->pinfo
2506 & INSN_LOAD_MEMORY_DELAY)))
2507 && insn_uses_reg (ip,
2508 ((prev_prev_insn.insn_opcode >> OP_SH_RT)
2509 & OP_MASK_RT),
2510 MIPS_GR_REG))
2511 /* If one instruction sets a condition code and the
2512 other one uses a condition code, we can not swap. */
2513 || ((pinfo & INSN_READ_COND_CODE)
2514 && (prev_pinfo & INSN_WRITE_COND_CODE))
2515 || ((pinfo & INSN_WRITE_COND_CODE)
2516 && (prev_pinfo & INSN_READ_COND_CODE))
2517 /* If the previous instruction uses the PC, we can not
2518 swap. */
2519 || (mips_opts.mips16
2520 && (prev_pinfo & MIPS16_INSN_READ_PC))
2521 /* If the previous instruction was extended, we can not
2522 swap. */
2523 || (mips_opts.mips16 && prev_insn_extended)
2524 /* If the previous instruction had a fixup in mips16
2525 mode, we can not swap. This normally means that the
2526 previous instruction was a 4 byte branch anyhow. */
f6688943 2527 || (mips_opts.mips16 && prev_insn_fixp[0])
bdaaa2e1
KH
2528 /* If the previous instruction is a sync, sync.l, or
2529 sync.p, we can not swap. */
f173e82e 2530 || (prev_pinfo & INSN_SYNC))
252b5132
RH
2531 {
2532 /* We could do even better for unconditional branches to
2533 portions of this object file; we could pick up the
2534 instruction at the destination, put it in the delay
2535 slot, and bump the destination address. */
2536 emit_nop ();
2537 /* Update the previous insn information. */
2538 prev_prev_insn = *ip;
2539 prev_insn.insn_mo = &dummy_opcode;
2540 }
2541 else
2542 {
2543 /* It looks like we can actually do the swap. */
2544 if (! mips_opts.mips16)
2545 {
2546 char *prev_f;
2547 char temp[4];
2548
2549 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2550 memcpy (temp, prev_f, 4);
2551 memcpy (prev_f, f, 4);
2552 memcpy (f, temp, 4);
f6688943
TS
2553 if (prev_insn_fixp[0])
2554 {
2555 prev_insn_fixp[0]->fx_frag = frag_now;
2556 prev_insn_fixp[0]->fx_where = f - frag_now->fr_literal;
2557 }
2558 if (prev_insn_fixp[1])
2559 {
2560 prev_insn_fixp[1]->fx_frag = frag_now;
2561 prev_insn_fixp[1]->fx_where = f - frag_now->fr_literal;
2562 }
2563 if (prev_insn_fixp[2])
252b5132 2564 {
f6688943
TS
2565 prev_insn_fixp[2]->fx_frag = frag_now;
2566 prev_insn_fixp[2]->fx_where = f - frag_now->fr_literal;
252b5132 2567 }
f6688943 2568 if (fixp[0])
252b5132 2569 {
f6688943
TS
2570 fixp[0]->fx_frag = prev_insn_frag;
2571 fixp[0]->fx_where = prev_insn_where;
2572 }
2573 if (fixp[1])
2574 {
2575 fixp[1]->fx_frag = prev_insn_frag;
2576 fixp[1]->fx_where = prev_insn_where;
2577 }
2578 if (fixp[2])
2579 {
2580 fixp[2]->fx_frag = prev_insn_frag;
2581 fixp[2]->fx_where = prev_insn_where;
252b5132
RH
2582 }
2583 }
2584 else
2585 {
2586 char *prev_f;
2587 char temp[2];
2588
f6688943
TS
2589 assert (prev_insn_fixp[0] == NULL);
2590 assert (prev_insn_fixp[1] == NULL);
2591 assert (prev_insn_fixp[2] == NULL);
252b5132
RH
2592 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2593 memcpy (temp, prev_f, 2);
2594 memcpy (prev_f, f, 2);
f6688943 2595 if (*reloc_type != BFD_RELOC_MIPS16_JMP)
252b5132 2596 {
f6688943 2597 assert (*reloc_type == BFD_RELOC_UNUSED);
252b5132
RH
2598 memcpy (f, temp, 2);
2599 }
2600 else
2601 {
2602 memcpy (f, f + 2, 2);
2603 memcpy (f + 2, temp, 2);
2604 }
f6688943
TS
2605 if (fixp[0])
2606 {
2607 fixp[0]->fx_frag = prev_insn_frag;
2608 fixp[0]->fx_where = prev_insn_where;
2609 }
2610 if (fixp[1])
2611 {
2612 fixp[1]->fx_frag = prev_insn_frag;
2613 fixp[1]->fx_where = prev_insn_where;
2614 }
2615 if (fixp[2])
252b5132 2616 {
f6688943
TS
2617 fixp[2]->fx_frag = prev_insn_frag;
2618 fixp[2]->fx_where = prev_insn_where;
252b5132
RH
2619 }
2620 }
2621
2622 /* Update the previous insn information; leave prev_insn
2623 unchanged. */
2624 prev_prev_insn = *ip;
2625 }
2626 prev_insn_is_delay_slot = 1;
2627
2628 /* If that was an unconditional branch, forget the previous
2629 insn information. */
2630 if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2631 {
2632 prev_prev_insn.insn_mo = &dummy_opcode;
2633 prev_insn.insn_mo = &dummy_opcode;
2634 }
2635
f6688943
TS
2636 prev_insn_fixp[0] = NULL;
2637 prev_insn_fixp[1] = NULL;
2638 prev_insn_fixp[2] = NULL;
2639 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2640 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2641 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
252b5132
RH
2642 prev_insn_extended = 0;
2643 }
2644 else if (pinfo & INSN_COND_BRANCH_LIKELY)
2645 {
2646 /* We don't yet optimize a branch likely. What we should do
2647 is look at the target, copy the instruction found there
2648 into the delay slot, and increment the branch to jump to
2649 the next instruction. */
2650 emit_nop ();
2651 /* Update the previous insn information. */
2652 prev_prev_insn = *ip;
2653 prev_insn.insn_mo = &dummy_opcode;
f6688943
TS
2654 prev_insn_fixp[0] = NULL;
2655 prev_insn_fixp[1] = NULL;
2656 prev_insn_fixp[2] = NULL;
2657 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2658 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2659 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
252b5132
RH
2660 prev_insn_extended = 0;
2661 }
2662 else
2663 {
2664 /* Update the previous insn information. */
2665 if (nops > 0)
2666 prev_prev_insn.insn_mo = &dummy_opcode;
2667 else
2668 prev_prev_insn = prev_insn;
2669 prev_insn = *ip;
2670
2671 /* Any time we see a branch, we always fill the delay slot
2672 immediately; since this insn is not a branch, we know it
2673 is not in a delay slot. */
2674 prev_insn_is_delay_slot = 0;
2675
f6688943
TS
2676 prev_insn_fixp[0] = fixp[0];
2677 prev_insn_fixp[1] = fixp[1];
2678 prev_insn_fixp[2] = fixp[2];
2679 prev_insn_reloc_type[0] = reloc_type[0];
2680 prev_insn_reloc_type[1] = reloc_type[1];
2681 prev_insn_reloc_type[2] = reloc_type[2];
252b5132
RH
2682 if (mips_opts.mips16)
2683 prev_insn_extended = (ip->use_extend
f6688943 2684 || *reloc_type > BFD_RELOC_UNUSED);
252b5132
RH
2685 }
2686
2687 prev_prev_insn_unreordered = prev_insn_unreordered;
2688 prev_insn_unreordered = 0;
2689 prev_insn_frag = frag_now;
2690 prev_insn_where = f - frag_now->fr_literal;
2691 prev_insn_valid = 1;
2692 }
2693 else if (place == NULL)
2694 {
2695 /* We need to record a bit of information even when we are not
2696 reordering, in order to determine the base address for mips16
2697 PC relative relocs. */
2698 prev_prev_insn = prev_insn;
2699 prev_insn = *ip;
f6688943
TS
2700 prev_insn_reloc_type[0] = reloc_type[0];
2701 prev_insn_reloc_type[1] = reloc_type[1];
2702 prev_insn_reloc_type[2] = reloc_type[2];
252b5132
RH
2703 prev_prev_insn_unreordered = prev_insn_unreordered;
2704 prev_insn_unreordered = 1;
2705 }
2706
2707 /* We just output an insn, so the next one doesn't have a label. */
2708 mips_clear_insn_labels ();
2709
2710 /* We must ensure that a fixup associated with an unmatched %hi
2711 reloc does not become a variant frag. Otherwise, the
2712 rearrangement of %hi relocs in frob_file may confuse
2713 tc_gen_reloc. */
5e0116d5 2714 if (unmatched_reloc_p)
252b5132
RH
2715 {
2716 frag_wane (frag_now);
2717 frag_new (0);
2718 }
2719}
2720
2721/* This function forgets that there was any previous instruction or
2722 label. If PRESERVE is non-zero, it remembers enough information to
bdaaa2e1 2723 know whether nops are needed before a noreorder section. */
252b5132
RH
2724
2725static void
2726mips_no_prev_insn (preserve)
2727 int preserve;
2728{
2729 if (! preserve)
2730 {
2731 prev_insn.insn_mo = &dummy_opcode;
2732 prev_prev_insn.insn_mo = &dummy_opcode;
2733 prev_nop_frag = NULL;
2734 prev_nop_frag_holds = 0;
2735 prev_nop_frag_required = 0;
2736 prev_nop_frag_since = 0;
2737 }
2738 prev_insn_valid = 0;
2739 prev_insn_is_delay_slot = 0;
2740 prev_insn_unreordered = 0;
2741 prev_insn_extended = 0;
f6688943
TS
2742 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2743 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2744 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
252b5132
RH
2745 prev_prev_insn_unreordered = 0;
2746 mips_clear_insn_labels ();
2747}
2748
2749/* This function must be called whenever we turn on noreorder or emit
2750 something other than instructions. It inserts any NOPS which might
2751 be needed by the previous instruction, and clears the information
2752 kept for the previous instructions. The INSNS parameter is true if
bdaaa2e1 2753 instructions are to follow. */
252b5132
RH
2754
2755static void
2756mips_emit_delays (insns)
b34976b6 2757 bfd_boolean insns;
252b5132
RH
2758{
2759 if (! mips_opts.noreorder)
2760 {
2761 int nops;
2762
2763 nops = 0;
2764 if ((! mips_opts.mips16
9ce8a5dd 2765 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
2766 && (! cop_interlocks
2767 && (prev_insn.insn_mo->pinfo
2768 & (INSN_LOAD_COPROC_DELAY
2769 | INSN_COPROC_MOVE_DELAY
2770 | INSN_WRITE_COND_CODE))))
2771 || (! hilo_interlocks
2772 && (prev_insn.insn_mo->pinfo
2773 & (INSN_READ_LO
2774 | INSN_READ_HI)))
2775 || (! mips_opts.mips16
2776 && ! gpr_interlocks
bdaaa2e1 2777 && (prev_insn.insn_mo->pinfo
252b5132
RH
2778 & INSN_LOAD_MEMORY_DELAY))
2779 || (! mips_opts.mips16
e7af610e 2780 && mips_opts.isa == ISA_MIPS1
252b5132
RH
2781 && (prev_insn.insn_mo->pinfo
2782 & INSN_COPROC_MEMORY_DELAY)))
2783 {
beae10d5 2784 /* Itbl support may require additional care here. */
252b5132
RH
2785 ++nops;
2786 if ((! mips_opts.mips16
9ce8a5dd 2787 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
2788 && (! cop_interlocks
2789 && prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2790 || (! hilo_interlocks
2791 && ((prev_insn.insn_mo->pinfo & INSN_READ_HI)
2792 || (prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2793 ++nops;
2794
2795 if (prev_insn_unreordered)
2796 nops = 0;
2797 }
2798 else if ((! mips_opts.mips16
9ce8a5dd 2799 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
2800 && (! cop_interlocks
2801 && prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2802 || (! hilo_interlocks
2803 && ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
2804 || (prev_prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2805 {
beae10d5 2806 /* Itbl support may require additional care here. */
252b5132
RH
2807 if (! prev_prev_insn_unreordered)
2808 ++nops;
2809 }
2810
60b63b72
RS
2811 if (mips_fix_4122_bugs && prev_insn.insn_mo->name)
2812 {
2813 int min_nops = 0;
2814 const char *pn = prev_insn.insn_mo->name;
2815 if (strncmp(pn, "macc", 4) == 0
2816 || strncmp(pn, "dmacc", 5) == 0
2817 || strncmp(pn, "dmult", 5) == 0)
2818 {
2819 min_nops = 1;
2820 }
2821 if (nops < min_nops)
2822 nops = min_nops;
2823 }
2824
252b5132
RH
2825 if (nops > 0)
2826 {
2827 struct insn_label_list *l;
2828
2829 if (insns)
2830 {
2831 /* Record the frag which holds the nop instructions, so
2832 that we can remove them if we don't need them. */
2833 frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2834 prev_nop_frag = frag_now;
2835 prev_nop_frag_holds = nops;
2836 prev_nop_frag_required = 0;
2837 prev_nop_frag_since = 0;
2838 }
2839
2840 for (; nops > 0; --nops)
2841 emit_nop ();
2842
2843 if (insns)
2844 {
2845 /* Move on to a new frag, so that it is safe to simply
bdaaa2e1 2846 decrease the size of prev_nop_frag. */
252b5132
RH
2847 frag_wane (frag_now);
2848 frag_new (0);
2849 }
2850
2851 for (l = insn_labels; l != NULL; l = l->next)
2852 {
98aa84af
AM
2853 valueT val;
2854
252b5132 2855 assert (S_GET_SEGMENT (l->label) == now_seg);
49309057 2856 symbol_set_frag (l->label, frag_now);
98aa84af 2857 val = (valueT) frag_now_fix ();
252b5132
RH
2858 /* mips16 text labels are stored as odd. */
2859 if (mips_opts.mips16)
f9419b05 2860 ++val;
98aa84af 2861 S_SET_VALUE (l->label, val);
252b5132
RH
2862 }
2863 }
2864 }
2865
2866 /* Mark instruction labels in mips16 mode. */
f9419b05 2867 if (insns)
252b5132
RH
2868 mips16_mark_labels ();
2869
2870 mips_no_prev_insn (insns);
2871}
2872
2873/* Build an instruction created by a macro expansion. This is passed
2874 a pointer to the count of instructions created so far, an
2875 expression, the name of the instruction to build, an operand format
2876 string, and corresponding arguments. */
2877
2878#ifdef USE_STDARG
2879static void
2880macro_build (char *place,
2881 int *counter,
2882 expressionS * ep,
2883 const char *name,
2884 const char *fmt,
2885 ...)
2886#else
2887static void
2888macro_build (place, counter, ep, name, fmt, va_alist)
2889 char *place;
2890 int *counter;
2891 expressionS *ep;
2892 const char *name;
2893 const char *fmt;
2894 va_dcl
2895#endif
2896{
2897 struct mips_cl_insn insn;
f6688943 2898 bfd_reloc_code_real_type r[3];
252b5132 2899 va_list args;
252b5132
RH
2900
2901#ifdef USE_STDARG
2902 va_start (args, fmt);
2903#else
2904 va_start (args);
2905#endif
2906
2907 /*
2908 * If the macro is about to expand into a second instruction,
2909 * print a warning if needed. We need to pass ip as a parameter
2910 * to generate a better warning message here...
2911 */
2912 if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
2913 as_warn (_("Macro instruction expanded into multiple instructions"));
2914
80cc45a5
EC
2915 /*
2916 * If the macro is about to expand into a second instruction,
2917 * and it is in a delay slot, print a warning.
2918 */
2919 if (place == NULL
2920 && *counter == 1
2921 && mips_opts.noreorder
2922 && (prev_prev_insn.insn_mo->pinfo
2923 & (INSN_UNCOND_BRANCH_DELAY | INSN_COND_BRANCH_DELAY
2b2e39bf 2924 | INSN_COND_BRANCH_LIKELY)) != 0)
80cc45a5
EC
2925 as_warn (_("Macro instruction expanded into multiple instructions in a branch delay slot"));
2926
252b5132 2927 if (place == NULL)
f9419b05 2928 ++*counter; /* bump instruction counter */
252b5132
RH
2929
2930 if (mips_opts.mips16)
2931 {
2932 mips16_macro_build (place, counter, ep, name, fmt, args);
2933 va_end (args);
2934 return;
2935 }
2936
f6688943
TS
2937 r[0] = BFD_RELOC_UNUSED;
2938 r[1] = BFD_RELOC_UNUSED;
2939 r[2] = BFD_RELOC_UNUSED;
252b5132
RH
2940 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
2941 assert (insn.insn_mo);
2942 assert (strcmp (name, insn.insn_mo->name) == 0);
2943
2944 /* Search until we get a match for NAME. */
2945 while (1)
2946 {
b34976b6 2947 /* It is assumed here that macros will never generate
deec1734 2948 MDMX or MIPS-3D instructions. */
252b5132
RH
2949 if (strcmp (fmt, insn.insn_mo->args) == 0
2950 && insn.insn_mo->pinfo != INSN_MACRO
aec421e0
TS
2951 && OPCODE_IS_MEMBER (insn.insn_mo,
2952 (mips_opts.isa
3396de36 2953 | (file_ase_mips16 ? INSN_MIPS16 : 0)),
aec421e0 2954 mips_arch)
ec68c924 2955 && (mips_arch != CPU_R4650 || (insn.insn_mo->pinfo & FP_D) == 0))
252b5132
RH
2956 break;
2957
2958 ++insn.insn_mo;
2959 assert (insn.insn_mo->name);
2960 assert (strcmp (name, insn.insn_mo->name) == 0);
2961 }
2962
2963 insn.insn_opcode = insn.insn_mo->match;
2964 for (;;)
2965 {
2966 switch (*fmt++)
2967 {
2968 case '\0':
2969 break;
2970
2971 case ',':
2972 case '(':
2973 case ')':
2974 continue;
2975
2976 case 't':
2977 case 'w':
2978 case 'E':
38487616 2979 insn.insn_opcode |= va_arg (args, int) << OP_SH_RT;
252b5132
RH
2980 continue;
2981
2982 case 'c':
38487616
TS
2983 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE;
2984 continue;
2985
252b5132
RH
2986 case 'T':
2987 case 'W':
38487616 2988 insn.insn_opcode |= va_arg (args, int) << OP_SH_FT;
252b5132
RH
2989 continue;
2990
2991 case 'd':
2992 case 'G':
af7ee8bf 2993 case 'K':
38487616 2994 insn.insn_opcode |= va_arg (args, int) << OP_SH_RD;
252b5132
RH
2995 continue;
2996
4372b673
NC
2997 case 'U':
2998 {
2999 int tmp = va_arg (args, int);
3000
38487616
TS
3001 insn.insn_opcode |= tmp << OP_SH_RT;
3002 insn.insn_opcode |= tmp << OP_SH_RD;
beae10d5 3003 continue;
4372b673
NC
3004 }
3005
252b5132
RH
3006 case 'V':
3007 case 'S':
38487616 3008 insn.insn_opcode |= va_arg (args, int) << OP_SH_FS;
252b5132
RH
3009 continue;
3010
3011 case 'z':
3012 continue;
3013
3014 case '<':
38487616 3015 insn.insn_opcode |= va_arg (args, int) << OP_SH_SHAMT;
252b5132
RH
3016 continue;
3017
3018 case 'D':
38487616 3019 insn.insn_opcode |= va_arg (args, int) << OP_SH_FD;
252b5132
RH
3020 continue;
3021
3022 case 'B':
38487616 3023 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE20;
252b5132
RH
3024 continue;
3025
4372b673 3026 case 'J':
38487616 3027 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE19;
4372b673
NC
3028 continue;
3029
252b5132 3030 case 'q':
38487616 3031 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE2;
252b5132
RH
3032 continue;
3033
3034 case 'b':
3035 case 's':
3036 case 'r':
3037 case 'v':
38487616 3038 insn.insn_opcode |= va_arg (args, int) << OP_SH_RS;
252b5132
RH
3039 continue;
3040
3041 case 'i':
3042 case 'j':
3043 case 'o':
f6688943 3044 *r = (bfd_reloc_code_real_type) va_arg (args, int);
cdf6fd85 3045 assert (*r == BFD_RELOC_GPREL16
f6688943
TS
3046 || *r == BFD_RELOC_MIPS_LITERAL
3047 || *r == BFD_RELOC_MIPS_HIGHER
3048 || *r == BFD_RELOC_HI16_S
3049 || *r == BFD_RELOC_LO16
3050 || *r == BFD_RELOC_MIPS_GOT16
3051 || *r == BFD_RELOC_MIPS_CALL16
438c16b8
TS
3052 || *r == BFD_RELOC_MIPS_GOT_DISP
3053 || *r == BFD_RELOC_MIPS_GOT_PAGE
3054 || *r == BFD_RELOC_MIPS_GOT_OFST
f6688943
TS
3055 || *r == BFD_RELOC_MIPS_GOT_LO16
3056 || *r == BFD_RELOC_MIPS_CALL_LO16
252b5132 3057 || (ep->X_op == O_subtract
f6688943 3058 && *r == BFD_RELOC_PCREL_LO16));
252b5132
RH
3059 continue;
3060
3061 case 'u':
f6688943 3062 *r = (bfd_reloc_code_real_type) va_arg (args, int);
252b5132
RH
3063 assert (ep != NULL
3064 && (ep->X_op == O_constant
3065 || (ep->X_op == O_symbol
f6688943
TS
3066 && (*r == BFD_RELOC_MIPS_HIGHEST
3067 || *r == BFD_RELOC_HI16_S
3068 || *r == BFD_RELOC_HI16
3069 || *r == BFD_RELOC_GPREL16
3070 || *r == BFD_RELOC_MIPS_GOT_HI16
3071 || *r == BFD_RELOC_MIPS_CALL_HI16))
252b5132 3072 || (ep->X_op == O_subtract
f6688943 3073 && *r == BFD_RELOC_PCREL_HI16_S)));
252b5132
RH
3074 continue;
3075
3076 case 'p':
3077 assert (ep != NULL);
3078 /*
3079 * This allows macro() to pass an immediate expression for
3080 * creating short branches without creating a symbol.
3081 * Note that the expression still might come from the assembly
3082 * input, in which case the value is not checked for range nor
3083 * is a relocation entry generated (yuck).
3084 */
3085 if (ep->X_op == O_constant)
3086 {
3087 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
3088 ep = NULL;
3089 }
3090 else
af6ae2ad 3091 *r = BFD_RELOC_16_PCREL_S2;
252b5132
RH
3092 continue;
3093
3094 case 'a':
3095 assert (ep != NULL);
f6688943 3096 *r = BFD_RELOC_MIPS_JMP;
252b5132
RH
3097 continue;
3098
3099 case 'C':
3100 insn.insn_opcode |= va_arg (args, unsigned long);
3101 continue;
3102
3103 default:
3104 internalError ();
3105 }
3106 break;
3107 }
3108 va_end (args);
f6688943 3109 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 3110
5e0116d5 3111 append_insn (place, &insn, ep, r);
252b5132
RH
3112}
3113
3114static void
3115mips16_macro_build (place, counter, ep, name, fmt, args)
3116 char *place;
43841e91 3117 int *counter ATTRIBUTE_UNUSED;
252b5132
RH
3118 expressionS *ep;
3119 const char *name;
3120 const char *fmt;
3121 va_list args;
3122{
3123 struct mips_cl_insn insn;
f6688943
TS
3124 bfd_reloc_code_real_type r[3]
3125 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 3126
252b5132
RH
3127 insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
3128 assert (insn.insn_mo);
3129 assert (strcmp (name, insn.insn_mo->name) == 0);
3130
3131 while (strcmp (fmt, insn.insn_mo->args) != 0
3132 || insn.insn_mo->pinfo == INSN_MACRO)
3133 {
3134 ++insn.insn_mo;
3135 assert (insn.insn_mo->name);
3136 assert (strcmp (name, insn.insn_mo->name) == 0);
3137 }
3138
3139 insn.insn_opcode = insn.insn_mo->match;
b34976b6 3140 insn.use_extend = FALSE;
252b5132
RH
3141
3142 for (;;)
3143 {
3144 int c;
3145
3146 c = *fmt++;
3147 switch (c)
3148 {
3149 case '\0':
3150 break;
3151
3152 case ',':
3153 case '(':
3154 case ')':
3155 continue;
3156
3157 case 'y':
3158 case 'w':
3159 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
3160 continue;
3161
3162 case 'x':
3163 case 'v':
3164 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
3165 continue;
3166
3167 case 'z':
3168 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
3169 continue;
3170
3171 case 'Z':
3172 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
3173 continue;
3174
3175 case '0':
3176 case 'S':
3177 case 'P':
3178 case 'R':
3179 continue;
3180
3181 case 'X':
3182 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
3183 continue;
3184
3185 case 'Y':
3186 {
3187 int regno;
3188
3189 regno = va_arg (args, int);
3190 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
3191 insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
3192 }
3193 continue;
3194
3195 case '<':
3196 case '>':
3197 case '4':
3198 case '5':
3199 case 'H':
3200 case 'W':
3201 case 'D':
3202 case 'j':
3203 case '8':
3204 case 'V':
3205 case 'C':
3206 case 'U':
3207 case 'k':
3208 case 'K':
3209 case 'p':
3210 case 'q':
3211 {
3212 assert (ep != NULL);
3213
3214 if (ep->X_op != O_constant)
874e8986 3215 *r = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
3216 else
3217 {
b34976b6
AM
3218 mips16_immed (NULL, 0, c, ep->X_add_number, FALSE, FALSE,
3219 FALSE, &insn.insn_opcode, &insn.use_extend,
c4e7957c 3220 &insn.extend);
252b5132 3221 ep = NULL;
f6688943 3222 *r = BFD_RELOC_UNUSED;
252b5132
RH
3223 }
3224 }
3225 continue;
3226
3227 case '6':
3228 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
3229 continue;
3230 }
3231
3232 break;
3233 }
3234
f6688943 3235 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 3236
5e0116d5 3237 append_insn (place, &insn, ep, r);
252b5132
RH
3238}
3239
438c16b8
TS
3240/*
3241 * Generate a "jalr" instruction with a relocation hint to the called
3242 * function. This occurs in NewABI PIC code.
3243 */
3244static void
3245macro_build_jalr (icnt, ep)
3246 int icnt;
3247 expressionS *ep;
3248{
f21f8242 3249 char *f;
b34976b6 3250
438c16b8 3251 if (HAVE_NEWABI)
f21f8242
AO
3252 {
3253 frag_grow (4);
3254 f = frag_more (0);
3255 }
438c16b8
TS
3256 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr", "d,s",
3257 RA, PIC_CALL_REG);
3258 if (HAVE_NEWABI)
f21f8242 3259 fix_new_exp (frag_now, f - frag_now->fr_literal,
b34976b6 3260 0, ep, FALSE, BFD_RELOC_MIPS_JALR);
438c16b8
TS
3261}
3262
252b5132
RH
3263/*
3264 * Generate a "lui" instruction.
3265 */
3266static void
3267macro_build_lui (place, counter, ep, regnum)
3268 char *place;
3269 int *counter;
3270 expressionS *ep;
3271 int regnum;
3272{
3273 expressionS high_expr;
3274 struct mips_cl_insn insn;
f6688943
TS
3275 bfd_reloc_code_real_type r[3]
3276 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
5a38dc70
AM
3277 const char *name = "lui";
3278 const char *fmt = "t,u";
252b5132
RH
3279
3280 assert (! mips_opts.mips16);
3281
3282 if (place == NULL)
3283 high_expr = *ep;
3284 else
3285 {
3286 high_expr.X_op = O_constant;
3287 high_expr.X_add_number = ep->X_add_number;
3288 }
3289
3290 if (high_expr.X_op == O_constant)
3291 {
3292 /* we can compute the instruction now without a relocation entry */
e7d556df
TS
3293 high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3294 >> 16) & 0xffff;
f6688943 3295 *r = BFD_RELOC_UNUSED;
252b5132 3296 }
78e1bb40 3297 else
252b5132
RH
3298 {
3299 assert (ep->X_op == O_symbol);
3300 /* _gp_disp is a special case, used from s_cpload. */
3301 assert (mips_pic == NO_PIC
78e1bb40
AO
3302 || (! HAVE_NEWABI
3303 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0));
f6688943 3304 *r = BFD_RELOC_HI16_S;
252b5132
RH
3305 }
3306
3307 /*
3308 * If the macro is about to expand into a second instruction,
3309 * print a warning if needed. We need to pass ip as a parameter
3310 * to generate a better warning message here...
3311 */
3312 if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
3313 as_warn (_("Macro instruction expanded into multiple instructions"));
3314
3315 if (place == NULL)
f9419b05 3316 ++*counter; /* bump instruction counter */
252b5132
RH
3317
3318 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3319 assert (insn.insn_mo);
3320 assert (strcmp (name, insn.insn_mo->name) == 0);
3321 assert (strcmp (fmt, insn.insn_mo->args) == 0);
3322
3323 insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
f6688943 3324 if (*r == BFD_RELOC_UNUSED)
252b5132
RH
3325 {
3326 insn.insn_opcode |= high_expr.X_add_number;
5e0116d5 3327 append_insn (place, &insn, NULL, r);
252b5132
RH
3328 }
3329 else
5e0116d5 3330 append_insn (place, &insn, &high_expr, r);
252b5132
RH
3331}
3332
885add95
CD
3333/* Generate a sequence of instructions to do a load or store from a constant
3334 offset off of a base register (breg) into/from a target register (treg),
3335 using AT if necessary. */
3336static void
3337macro_build_ldst_constoffset (place, counter, ep, op, treg, breg)
3338 char *place;
3339 int *counter;
3340 expressionS *ep;
3341 const char *op;
3342 int treg, breg;
3343{
3344 assert (ep->X_op == O_constant);
3345
3346 /* Right now, this routine can only handle signed 32-bit contants. */
3347 if (! IS_SEXT_32BIT_NUM(ep->X_add_number))
3348 as_warn (_("operand overflow"));
3349
3350 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
3351 {
3352 /* Signed 16-bit offset will fit in the op. Easy! */
3353 macro_build (place, counter, ep, op, "t,o(b)", treg,
3354 (int) BFD_RELOC_LO16, breg);
3355 }
3356 else
3357 {
3358 /* 32-bit offset, need multiple instructions and AT, like:
3359 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
3360 addu $tempreg,$tempreg,$breg
3361 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
3362 to handle the complete offset. */
3363 macro_build_lui (place, counter, ep, AT);
3364 if (place != NULL)
3365 place += 4;
3366 macro_build (place, counter, (expressionS *) NULL,
3367 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
3368 "d,v,t", AT, AT, breg);
3369 if (place != NULL)
3370 place += 4;
3371 macro_build (place, counter, ep, op, "t,o(b)", treg,
3372 (int) BFD_RELOC_LO16, AT);
3373
3374 if (mips_opts.noat)
3375 as_warn (_("Macro used $at after \".set noat\""));
3376 }
3377}
3378
252b5132
RH
3379/* set_at()
3380 * Generates code to set the $at register to true (one)
3381 * if reg is less than the immediate expression.
3382 */
3383static void
3384set_at (counter, reg, unsignedp)
3385 int *counter;
3386 int reg;
3387 int unsignedp;
3388{
3389 if (imm_expr.X_op == O_constant
3390 && imm_expr.X_add_number >= -0x8000
3391 && imm_expr.X_add_number < 0x8000)
3392 macro_build ((char *) NULL, counter, &imm_expr,
3393 unsignedp ? "sltiu" : "slti",
3394 "t,r,j", AT, reg, (int) BFD_RELOC_LO16);
3395 else
3396 {
4d34fb5f 3397 load_register (counter, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9 3398 macro_build ((char *) NULL, counter, (expressionS *) NULL,
252b5132
RH
3399 unsignedp ? "sltu" : "slt",
3400 "d,v,t", AT, reg, AT);
3401 }
3402}
3403
3404/* Warn if an expression is not a constant. */
3405
3406static void
3407check_absolute_expr (ip, ex)
3408 struct mips_cl_insn *ip;
3409 expressionS *ex;
3410{
3411 if (ex->X_op == O_big)
3412 as_bad (_("unsupported large constant"));
3413 else if (ex->X_op != O_constant)
3414 as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
3415}
3416
3417/* Count the leading zeroes by performing a binary chop. This is a
3418 bulky bit of source, but performance is a LOT better for the
3419 majority of values than a simple loop to count the bits:
3420 for (lcnt = 0; (lcnt < 32); lcnt++)
3421 if ((v) & (1 << (31 - lcnt)))
3422 break;
3423 However it is not code size friendly, and the gain will drop a bit
3424 on certain cached systems.
3425*/
3426#define COUNT_TOP_ZEROES(v) \
3427 (((v) & ~0xffff) == 0 \
3428 ? ((v) & ~0xff) == 0 \
3429 ? ((v) & ~0xf) == 0 \
3430 ? ((v) & ~0x3) == 0 \
3431 ? ((v) & ~0x1) == 0 \
3432 ? !(v) \
3433 ? 32 \
3434 : 31 \
3435 : 30 \
3436 : ((v) & ~0x7) == 0 \
3437 ? 29 \
3438 : 28 \
3439 : ((v) & ~0x3f) == 0 \
3440 ? ((v) & ~0x1f) == 0 \
3441 ? 27 \
3442 : 26 \
3443 : ((v) & ~0x7f) == 0 \
3444 ? 25 \
3445 : 24 \
3446 : ((v) & ~0xfff) == 0 \
3447 ? ((v) & ~0x3ff) == 0 \
3448 ? ((v) & ~0x1ff) == 0 \
3449 ? 23 \
3450 : 22 \
3451 : ((v) & ~0x7ff) == 0 \
3452 ? 21 \
3453 : 20 \
3454 : ((v) & ~0x3fff) == 0 \
3455 ? ((v) & ~0x1fff) == 0 \
3456 ? 19 \
3457 : 18 \
3458 : ((v) & ~0x7fff) == 0 \
3459 ? 17 \
3460 : 16 \
3461 : ((v) & ~0xffffff) == 0 \
3462 ? ((v) & ~0xfffff) == 0 \
3463 ? ((v) & ~0x3ffff) == 0 \
3464 ? ((v) & ~0x1ffff) == 0 \
3465 ? 15 \
3466 : 14 \
3467 : ((v) & ~0x7ffff) == 0 \
3468 ? 13 \
3469 : 12 \
3470 : ((v) & ~0x3fffff) == 0 \
3471 ? ((v) & ~0x1fffff) == 0 \
3472 ? 11 \
3473 : 10 \
3474 : ((v) & ~0x7fffff) == 0 \
3475 ? 9 \
3476 : 8 \
3477 : ((v) & ~0xfffffff) == 0 \
3478 ? ((v) & ~0x3ffffff) == 0 \
3479 ? ((v) & ~0x1ffffff) == 0 \
3480 ? 7 \
3481 : 6 \
3482 : ((v) & ~0x7ffffff) == 0 \
3483 ? 5 \
3484 : 4 \
3485 : ((v) & ~0x3fffffff) == 0 \
3486 ? ((v) & ~0x1fffffff) == 0 \
3487 ? 3 \
3488 : 2 \
3489 : ((v) & ~0x7fffffff) == 0 \
3490 ? 1 \
3491 : 0)
3492
3493/* load_register()
3494 * This routine generates the least number of instructions neccessary to load
3495 * an absolute expression value into a register.
3496 */
3497static void
3498load_register (counter, reg, ep, dbl)
3499 int *counter;
3500 int reg;
3501 expressionS *ep;
3502 int dbl;
3503{
3504 int freg;
3505 expressionS hi32, lo32;
3506
3507 if (ep->X_op != O_big)
3508 {
3509 assert (ep->X_op == O_constant);
3510 if (ep->X_add_number < 0x8000
3511 && (ep->X_add_number >= 0
3512 || (ep->X_add_number >= -0x8000
3513 && (! dbl
3514 || ! ep->X_unsigned
3515 || sizeof (ep->X_add_number) > 4))))
3516 {
3517 /* We can handle 16 bit signed values with an addiu to
3518 $zero. No need to ever use daddiu here, since $zero and
3519 the result are always correct in 32 bit mode. */
3520 macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3521 (int) BFD_RELOC_LO16);
3522 return;
3523 }
3524 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3525 {
3526 /* We can handle 16 bit unsigned values with an ori to
3527 $zero. */
3528 macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, 0,
3529 (int) BFD_RELOC_LO16);
3530 return;
3531 }
6373ee54 3532 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)
252b5132
RH
3533 && (! dbl
3534 || ! ep->X_unsigned
3535 || sizeof (ep->X_add_number) > 4
3536 || (ep->X_add_number & 0x80000000) == 0))
ca4e0257 3537 || ((HAVE_32BIT_GPRS || ! dbl)
252b5132 3538 && (ep->X_add_number &~ (offsetT) 0xffffffff) == 0)
ca4e0257 3539 || (HAVE_32BIT_GPRS
252b5132
RH
3540 && ! dbl
3541 && ((ep->X_add_number &~ (offsetT) 0xffffffff)
3542 == ~ (offsetT) 0xffffffff)))
3543 {
3544 /* 32 bit values require an lui. */
3545 macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3546 (int) BFD_RELOC_HI16);
3547 if ((ep->X_add_number & 0xffff) != 0)
3548 macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, reg,
3549 (int) BFD_RELOC_LO16);
3550 return;
3551 }
3552 }
3553
3554 /* The value is larger than 32 bits. */
3555
ca4e0257 3556 if (HAVE_32BIT_GPRS)
252b5132 3557 {
956cd1d6
TS
3558 as_bad (_("Number (0x%lx) larger than 32 bits"),
3559 (unsigned long) ep->X_add_number);
252b5132
RH
3560 macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3561 (int) BFD_RELOC_LO16);
3562 return;
3563 }
3564
3565 if (ep->X_op != O_big)
3566 {
3567 hi32 = *ep;
3568 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3569 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3570 hi32.X_add_number &= 0xffffffff;
3571 lo32 = *ep;
3572 lo32.X_add_number &= 0xffffffff;
3573 }
3574 else
3575 {
3576 assert (ep->X_add_number > 2);
3577 if (ep->X_add_number == 3)
3578 generic_bignum[3] = 0;
3579 else if (ep->X_add_number > 4)
3580 as_bad (_("Number larger than 64 bits"));
3581 lo32.X_op = O_constant;
3582 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3583 hi32.X_op = O_constant;
3584 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3585 }
3586
3587 if (hi32.X_add_number == 0)
3588 freg = 0;
3589 else
3590 {
3591 int shift, bit;
3592 unsigned long hi, lo;
3593
956cd1d6 3594 if (hi32.X_add_number == (offsetT) 0xffffffff)
beae10d5
KH
3595 {
3596 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3597 {
3598 macro_build ((char *) NULL, counter, &lo32, "addiu", "t,r,j",
252b5132 3599 reg, 0, (int) BFD_RELOC_LO16);
beae10d5
KH
3600 return;
3601 }
3602 if (lo32.X_add_number & 0x80000000)
3603 {
3604 macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3605 (int) BFD_RELOC_HI16);
252b5132
RH
3606 if (lo32.X_add_number & 0xffff)
3607 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i",
3608 reg, reg, (int) BFD_RELOC_LO16);
beae10d5
KH
3609 return;
3610 }
3611 }
252b5132
RH
3612
3613 /* Check for 16bit shifted constant. We know that hi32 is
3614 non-zero, so start the mask on the first bit of the hi32
3615 value. */
3616 shift = 17;
3617 do
beae10d5
KH
3618 {
3619 unsigned long himask, lomask;
3620
3621 if (shift < 32)
3622 {
3623 himask = 0xffff >> (32 - shift);
3624 lomask = (0xffff << shift) & 0xffffffff;
3625 }
3626 else
3627 {
3628 himask = 0xffff << (shift - 32);
3629 lomask = 0;
3630 }
3631 if ((hi32.X_add_number & ~(offsetT) himask) == 0
3632 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3633 {
3634 expressionS tmp;
3635
3636 tmp.X_op = O_constant;
3637 if (shift < 32)
3638 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3639 | (lo32.X_add_number >> shift));
3640 else
3641 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
3642 macro_build ((char *) NULL, counter, &tmp,
3643 "ori", "t,r,i", reg, 0,
3644 (int) BFD_RELOC_LO16);
2396cfb9 3645 macro_build ((char *) NULL, counter, (expressionS *) NULL,
beae10d5
KH
3646 (shift >= 32) ? "dsll32" : "dsll",
3647 "d,w,<", reg, reg,
3648 (shift >= 32) ? shift - 32 : shift);
3649 return;
3650 }
f9419b05 3651 ++shift;
beae10d5
KH
3652 }
3653 while (shift <= (64 - 16));
252b5132
RH
3654
3655 /* Find the bit number of the lowest one bit, and store the
3656 shifted value in hi/lo. */
3657 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3658 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3659 if (lo != 0)
3660 {
3661 bit = 0;
3662 while ((lo & 1) == 0)
3663 {
3664 lo >>= 1;
3665 ++bit;
3666 }
3667 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3668 hi >>= bit;
3669 }
3670 else
3671 {
3672 bit = 32;
3673 while ((hi & 1) == 0)
3674 {
3675 hi >>= 1;
3676 ++bit;
3677 }
3678 lo = hi;
3679 hi = 0;
3680 }
3681
3682 /* Optimize if the shifted value is a (power of 2) - 1. */
3683 if ((hi == 0 && ((lo + 1) & lo) == 0)
3684 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
beae10d5
KH
3685 {
3686 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
252b5132 3687 if (shift != 0)
beae10d5 3688 {
252b5132
RH
3689 expressionS tmp;
3690
3691 /* This instruction will set the register to be all
3692 ones. */
beae10d5
KH
3693 tmp.X_op = O_constant;
3694 tmp.X_add_number = (offsetT) -1;
3695 macro_build ((char *) NULL, counter, &tmp, "addiu", "t,r,j",
252b5132 3696 reg, 0, (int) BFD_RELOC_LO16);
beae10d5
KH
3697 if (bit != 0)
3698 {
3699 bit += shift;
2396cfb9 3700 macro_build ((char *) NULL, counter, (expressionS *) NULL,
beae10d5
KH
3701 (bit >= 32) ? "dsll32" : "dsll",
3702 "d,w,<", reg, reg,
3703 (bit >= 32) ? bit - 32 : bit);
3704 }
2396cfb9 3705 macro_build ((char *) NULL, counter, (expressionS *) NULL,
252b5132 3706 (shift >= 32) ? "dsrl32" : "dsrl",
beae10d5 3707 "d,w,<", reg, reg,
252b5132 3708 (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
3709 return;
3710 }
3711 }
252b5132
RH
3712
3713 /* Sign extend hi32 before calling load_register, because we can
3714 generally get better code when we load a sign extended value. */
3715 if ((hi32.X_add_number & 0x80000000) != 0)
beae10d5 3716 hi32.X_add_number |= ~(offsetT) 0xffffffff;
252b5132
RH
3717 load_register (counter, reg, &hi32, 0);
3718 freg = reg;
3719 }
3720 if ((lo32.X_add_number & 0xffff0000) == 0)
3721 {
3722 if (freg != 0)
3723 {
2396cfb9
TS
3724 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3725 "dsll32", "d,w,<", reg, freg, 0);
252b5132
RH
3726 freg = reg;
3727 }
3728 }
3729 else
3730 {
3731 expressionS mid16;
3732
956cd1d6 3733 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
beae10d5 3734 {
252b5132
RH
3735 macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3736 (int) BFD_RELOC_HI16);
956cd1d6
TS
3737 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3738 "dsrl32", "d,w,<", reg, reg, 0);
beae10d5
KH
3739 return;
3740 }
252b5132
RH
3741
3742 if (freg != 0)
3743 {
956cd1d6
TS
3744 macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3745 "d,w,<", reg, freg, 16);
252b5132
RH
3746 freg = reg;
3747 }
3748 mid16 = lo32;
3749 mid16.X_add_number >>= 16;
3750 macro_build ((char *) NULL, counter, &mid16, "ori", "t,r,i", reg,
3751 freg, (int) BFD_RELOC_LO16);
956cd1d6
TS
3752 macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3753 "d,w,<", reg, reg, 16);
252b5132
RH
3754 freg = reg;
3755 }
3756 if ((lo32.X_add_number & 0xffff) != 0)
3757 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i", reg, freg,
3758 (int) BFD_RELOC_LO16);
3759}
3760
3761/* Load an address into a register. */
3762
3763static void
c9914766 3764load_address (counter, reg, ep, used_at)
252b5132
RH
3765 int *counter;
3766 int reg;
3767 expressionS *ep;
d6bc6245 3768 int *used_at;
252b5132 3769{
f9419b05 3770 char *p = NULL;
252b5132
RH
3771
3772 if (ep->X_op != O_constant
3773 && ep->X_op != O_symbol)
3774 {
3775 as_bad (_("expression too complex"));
3776 ep->X_op = O_constant;
3777 }
3778
3779 if (ep->X_op == O_constant)
3780 {
c9914766 3781 load_register (counter, reg, ep, HAVE_64BIT_ADDRESSES);
252b5132
RH
3782 return;
3783 }
3784
3785 if (mips_pic == NO_PIC)
3786 {
3787 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 3788 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
3789 Otherwise we want
3790 lui $reg,<sym> (BFD_RELOC_HI16_S)
3791 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
d6bc6245 3792 If we have an addend, we always use the latter form.
76b3015f 3793
d6bc6245
TS
3794 With 64bit address space and a usable $at we want
3795 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3796 lui $at,<sym> (BFD_RELOC_HI16_S)
3797 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3798 daddiu $at,<sym> (BFD_RELOC_LO16)
3799 dsll32 $reg,0
3a482fd5 3800 daddu $reg,$reg,$at
76b3015f 3801
c03099e6 3802 If $at is already in use, we use a path which is suboptimal
d6bc6245
TS
3803 on superscalar processors.
3804 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3805 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3806 dsll $reg,16
3807 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
3808 dsll $reg,16
3809 daddiu $reg,<sym> (BFD_RELOC_LO16)
3810 */
c9914766 3811 if (HAVE_64BIT_ADDRESSES)
d6bc6245 3812 {
d6bc6245
TS
3813 /* We don't do GP optimization for now because RELAX_ENCODE can't
3814 hold the data for such large chunks. */
3815
460597ba 3816 if (*used_at == 0 && ! mips_opts.noat)
d6bc6245
TS
3817 {
3818 macro_build (p, counter, ep, "lui", "t,u",
3819 reg, (int) BFD_RELOC_MIPS_HIGHEST);
3820 macro_build (p, counter, ep, "lui", "t,u",
3821 AT, (int) BFD_RELOC_HI16_S);
3822 macro_build (p, counter, ep, "daddiu", "t,r,j",
3823 reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3824 macro_build (p, counter, ep, "daddiu", "t,r,j",
3825 AT, AT, (int) BFD_RELOC_LO16);
2396cfb9
TS
3826 macro_build (p, counter, (expressionS *) NULL, "dsll32",
3827 "d,w,<", reg, reg, 0);
3a482fd5 3828 macro_build (p, counter, (expressionS *) NULL, "daddu",
2396cfb9 3829 "d,v,t", reg, reg, AT);
d6bc6245
TS
3830 *used_at = 1;
3831 }
3832 else
3833 {
3834 macro_build (p, counter, ep, "lui", "t,u",
3835 reg, (int) BFD_RELOC_MIPS_HIGHEST);
3836 macro_build (p, counter, ep, "daddiu", "t,r,j",
3837 reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
2396cfb9
TS
3838 macro_build (p, counter, (expressionS *) NULL, "dsll",
3839 "d,w,<", reg, reg, 16);
d6bc6245
TS
3840 macro_build (p, counter, ep, "daddiu", "t,r,j",
3841 reg, reg, (int) BFD_RELOC_HI16_S);
2396cfb9
TS
3842 macro_build (p, counter, (expressionS *) NULL, "dsll",
3843 "d,w,<", reg, reg, 16);
d6bc6245
TS
3844 macro_build (p, counter, ep, "daddiu", "t,r,j",
3845 reg, reg, (int) BFD_RELOC_LO16);
3846 }
3847 }
252b5132
RH
3848 else
3849 {
d6bc6245
TS
3850 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3851 && ! nopic_need_relax (ep->X_add_symbol, 1))
3852 {
3853 frag_grow (20);
3854 macro_build ((char *) NULL, counter, ep,
c9914766
TS
3855 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
3856 reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
d6bc6245
TS
3857 p = frag_var (rs_machine_dependent, 8, 0,
3858 RELAX_ENCODE (4, 8, 0, 4, 0,
3859 mips_opts.warn_about_macros),
956cd1d6 3860 ep->X_add_symbol, 0, NULL);
d6bc6245
TS
3861 }
3862 macro_build_lui (p, counter, ep, reg);
3863 if (p != NULL)
3864 p += 4;
c9914766
TS
3865 macro_build (p, counter, ep,
3866 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
d6bc6245
TS
3867 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3868 }
252b5132
RH
3869 }
3870 else if (mips_pic == SVR4_PIC && ! mips_big_got)
3871 {
3872 expressionS ex;
3873
3874 /* If this is a reference to an external symbol, we want
3875 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3876 Otherwise we want
3877 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3878 nop
3879 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
ed6fb7bd
SC
3880 If we have NewABI, we want
3881 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
252b5132
RH
3882 If there is a constant, it must be added in after. */
3883 ex.X_add_number = ep->X_add_number;
3884 ep->X_add_number = 0;
3885 frag_grow (20);
ed6fb7bd
SC
3886 if (HAVE_NEWABI)
3887 {
3888 macro_build ((char *) NULL, counter, ep,
3889 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3890 (int) BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
3891 }
3892 else
3893 {
3894 macro_build ((char *) NULL, counter, ep,
3895 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
3896 reg, (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
3897 macro_build ((char *) NULL, counter, (expressionS *) NULL, "nop", "");
3898 p = frag_var (rs_machine_dependent, 4, 0,
3899 RELAX_ENCODE (0, 4, -8, 0, 0, mips_opts.warn_about_macros),
3900 ep->X_add_symbol, (offsetT) 0, (char *) NULL);
3901 macro_build (p, counter, ep,
3902 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3903 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3904 }
3905
252b5132
RH
3906 if (ex.X_add_number != 0)
3907 {
3908 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3909 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3910 ex.X_op = O_constant;
3911 macro_build ((char *) NULL, counter, &ex,
ca4e0257 3912 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
3913 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3914 }
3915 }
3916 else if (mips_pic == SVR4_PIC)
3917 {
3918 expressionS ex;
3919 int off;
3920
3921 /* This is the large GOT case. If this is a reference to an
3922 external symbol, we want
3923 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
3924 addu $reg,$reg,$gp
3925 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
3926 Otherwise, for a reference to a local symbol, we want
3927 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3928 nop
3929 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
438c16b8
TS
3930 If we have NewABI, we want
3931 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
3932 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
252b5132
RH
3933 If there is a constant, it must be added in after. */
3934 ex.X_add_number = ep->X_add_number;
3935 ep->X_add_number = 0;
438c16b8
TS
3936 if (HAVE_NEWABI)
3937 {
3938 macro_build ((char *) NULL, counter, ep,
3939 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3940 (int) BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
3941 macro_build (p, counter, ep,
3942 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
3943 reg, reg, (int) BFD_RELOC_MIPS_GOT_OFST);
3944 }
252b5132 3945 else
438c16b8
TS
3946 {
3947 if (reg_needs_delay (mips_gp_register))
3948 off = 4;
3949 else
3950 off = 0;
3951 frag_grow (32);
3952 macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3953 (int) BFD_RELOC_MIPS_GOT_HI16);
3954 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3955 HAVE_32BIT_ADDRESSES ? "addu" : "daddu", "d,v,t", reg,
3956 reg, mips_gp_register);
3957 macro_build ((char *) NULL, counter, ep,
3958 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
3959 "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT_LO16, reg);
3960 p = frag_var (rs_machine_dependent, 12 + off, 0,
3961 RELAX_ENCODE (12, 12 + off, off, 8 + off, 0,
3962 mips_opts.warn_about_macros),
3963 ep->X_add_symbol, 0, NULL);
3964 if (off > 0)
3965 {
3966 /* We need a nop before loading from $gp. This special
3967 check is required because the lui which starts the main
3968 instruction stream does not refer to $gp, and so will not
3969 insert the nop which may be required. */
3970 macro_build (p, counter, (expressionS *) NULL, "nop", "");
3971 p += 4;
3972 }
3973 macro_build (p, counter, ep,
3974 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3975 (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
3976 p += 4;
252b5132
RH
3977 macro_build (p, counter, (expressionS *) NULL, "nop", "");
3978 p += 4;
438c16b8
TS
3979 macro_build (p, counter, ep,
3980 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3981 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
252b5132 3982 }
438c16b8 3983
252b5132
RH
3984 if (ex.X_add_number != 0)
3985 {
3986 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3987 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3988 ex.X_op = O_constant;
f7ea7ef2
TS
3989 macro_build ((char *) NULL, counter, &ex,
3990 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
3991 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3992 }
3993 }
3994 else if (mips_pic == EMBEDDED_PIC)
3995 {
3996 /* We always do
cdf6fd85 3997 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
c9914766
TS
3998 */
3999 macro_build ((char *) NULL, counter, ep,
4000 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
4001 "t,r,j", reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
252b5132
RH
4002 }
4003 else
4004 abort ();
4005}
4006
ea1fb5dc
RS
4007/* Move the contents of register SOURCE into register DEST. */
4008
4009static void
4010move_register (counter, dest, source)
4011 int *counter;
4012 int dest;
4013 int source;
4014{
4015 macro_build ((char *) NULL, counter, (expressionS *) NULL,
4016 HAVE_32BIT_GPRS ? "addu" : "daddu",
4017 "d,v,t", dest, source, 0);
4018}
4019
252b5132
RH
4020/*
4021 * Build macros
4022 * This routine implements the seemingly endless macro or synthesized
4023 * instructions and addressing modes in the mips assembly language. Many
4024 * of these macros are simple and are similar to each other. These could
4025 * probably be handled by some kind of table or grammer aproach instead of
4026 * this verbose method. Others are not simple macros but are more like
4027 * optimizing code generation.
4028 * One interesting optimization is when several store macros appear
4029 * consecutivly that would load AT with the upper half of the same address.
4030 * The ensuing load upper instructions are ommited. This implies some kind
4031 * of global optimization. We currently only optimize within a single macro.
4032 * For many of the load and store macros if the address is specified as a
4033 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
4034 * first load register 'at' with zero and use it as the base register. The
4035 * mips assembler simply uses register $zero. Just one tiny optimization
4036 * we're missing.
4037 */
4038static void
4039macro (ip)
4040 struct mips_cl_insn *ip;
4041{
4042 register int treg, sreg, dreg, breg;
4043 int tempreg;
4044 int mask;
4045 int icnt = 0;
43841e91 4046 int used_at = 0;
252b5132
RH
4047 expressionS expr1;
4048 const char *s;
4049 const char *s2;
4050 const char *fmt;
4051 int likely = 0;
4052 int dbl = 0;
4053 int coproc = 0;
4054 int lr = 0;
4055 int imm = 0;
4056 offsetT maxnum;
4057 int off;
4058 bfd_reloc_code_real_type r;
252b5132
RH
4059 int hold_mips_optimize;
4060
4061 assert (! mips_opts.mips16);
4062
4063 treg = (ip->insn_opcode >> 16) & 0x1f;
4064 dreg = (ip->insn_opcode >> 11) & 0x1f;
4065 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
4066 mask = ip->insn_mo->mask;
4067
4068 expr1.X_op = O_constant;
4069 expr1.X_op_symbol = NULL;
4070 expr1.X_add_symbol = NULL;
4071 expr1.X_add_number = 1;
4072
4073 switch (mask)
4074 {
4075 case M_DABS:
4076 dbl = 1;
4077 case M_ABS:
4078 /* bgez $a0,.+12
4079 move v0,$a0
4080 sub v0,$zero,$a0
4081 */
4082
b34976b6 4083 mips_emit_delays (TRUE);
252b5132
RH
4084 ++mips_opts.noreorder;
4085 mips_any_noreorder = 1;
4086
4087 expr1.X_add_number = 8;
4088 macro_build ((char *) NULL, &icnt, &expr1, "bgez", "s,p", sreg);
4089 if (dreg == sreg)
2396cfb9
TS
4090 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
4091 0);
252b5132 4092 else
ea1fb5dc 4093 move_register (&icnt, dreg, sreg);
2396cfb9 4094 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 4095 dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
252b5132
RH
4096
4097 --mips_opts.noreorder;
4098 return;
4099
4100 case M_ADD_I:
4101 s = "addi";
4102 s2 = "add";
4103 goto do_addi;
4104 case M_ADDU_I:
4105 s = "addiu";
4106 s2 = "addu";
4107 goto do_addi;
4108 case M_DADD_I:
4109 dbl = 1;
4110 s = "daddi";
4111 s2 = "dadd";
4112 goto do_addi;
4113 case M_DADDU_I:
4114 dbl = 1;
4115 s = "daddiu";
4116 s2 = "daddu";
4117 do_addi:
4118 if (imm_expr.X_op == O_constant
4119 && imm_expr.X_add_number >= -0x8000
4120 && imm_expr.X_add_number < 0x8000)
4121 {
4122 macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,j", treg, sreg,
4123 (int) BFD_RELOC_LO16);
4124 return;
4125 }
4126 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9
TS
4127 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
4128 treg, sreg, AT);
252b5132
RH
4129 break;
4130
4131 case M_AND_I:
4132 s = "andi";
4133 s2 = "and";
4134 goto do_bit;
4135 case M_OR_I:
4136 s = "ori";
4137 s2 = "or";
4138 goto do_bit;
4139 case M_NOR_I:
4140 s = "";
4141 s2 = "nor";
4142 goto do_bit;
4143 case M_XOR_I:
4144 s = "xori";
4145 s2 = "xor";
4146 do_bit:
4147 if (imm_expr.X_op == O_constant
4148 && imm_expr.X_add_number >= 0
4149 && imm_expr.X_add_number < 0x10000)
4150 {
4151 if (mask != M_NOR_I)
4152 macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,i", treg,
4153 sreg, (int) BFD_RELOC_LO16);
4154 else
4155 {
4156 macro_build ((char *) NULL, &icnt, &imm_expr, "ori", "t,r,i",
4157 treg, sreg, (int) BFD_RELOC_LO16);
2396cfb9
TS
4158 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nor",
4159 "d,v,t", treg, treg, 0);
252b5132
RH
4160 }
4161 return;
4162 }
4163
d6bc6245 4164 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
4165 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
4166 treg, sreg, AT);
252b5132
RH
4167 break;
4168
4169 case M_BEQ_I:
4170 s = "beq";
4171 goto beq_i;
4172 case M_BEQL_I:
4173 s = "beql";
4174 likely = 1;
4175 goto beq_i;
4176 case M_BNE_I:
4177 s = "bne";
4178 goto beq_i;
4179 case M_BNEL_I:
4180 s = "bnel";
4181 likely = 1;
4182 beq_i:
4183 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4184 {
4185 macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg,
4186 0);
4187 return;
4188 }
4d34fb5f 4189 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
252b5132
RH
4190 macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg, AT);
4191 break;
4192
4193 case M_BGEL:
4194 likely = 1;
4195 case M_BGE:
4196 if (treg == 0)
4197 {
4198 macro_build ((char *) NULL, &icnt, &offset_expr,
2396cfb9 4199 likely ? "bgezl" : "bgez", "s,p", sreg);
252b5132
RH
4200 return;
4201 }
4202 if (sreg == 0)
4203 {
4204 macro_build ((char *) NULL, &icnt, &offset_expr,
2396cfb9 4205 likely ? "blezl" : "blez", "s,p", treg);
252b5132
RH
4206 return;
4207 }
2396cfb9
TS
4208 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4209 AT, sreg, treg);
252b5132 4210 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4211 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4212 break;
4213
4214 case M_BGTL_I:
4215 likely = 1;
4216 case M_BGT_I:
4217 /* check for > max integer */
4218 maxnum = 0x7fffffff;
ca4e0257 4219 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
4220 {
4221 maxnum <<= 16;
4222 maxnum |= 0xffff;
4223 maxnum <<= 16;
4224 maxnum |= 0xffff;
4225 }
4226 if (imm_expr.X_op == O_constant
4227 && imm_expr.X_add_number >= maxnum
ca4e0257 4228 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
4229 {
4230 do_false:
4231 /* result is always false */
4232 if (! likely)
4233 {
39c0a331
L
4234 if (warn_nops)
4235 as_warn (_("Branch %s is always false (nop)"),
4236 ip->insn_mo->name);
2396cfb9
TS
4237 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop",
4238 "", 0);
252b5132
RH
4239 }
4240 else
4241 {
39c0a331
L
4242 if (warn_nops)
4243 as_warn (_("Branch likely %s is always false"),
4244 ip->insn_mo->name);
252b5132
RH
4245 macro_build ((char *) NULL, &icnt, &offset_expr, "bnel",
4246 "s,t,p", 0, 0);
4247 }
4248 return;
4249 }
4250 if (imm_expr.X_op != O_constant)
4251 as_bad (_("Unsupported large constant"));
f9419b05 4252 ++imm_expr.X_add_number;
252b5132
RH
4253 /* FALLTHROUGH */
4254 case M_BGE_I:
4255 case M_BGEL_I:
4256 if (mask == M_BGEL_I)
4257 likely = 1;
4258 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4259 {
4260 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4261 likely ? "bgezl" : "bgez", "s,p", sreg);
252b5132
RH
4262 return;
4263 }
4264 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4265 {
4266 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4267 likely ? "bgtzl" : "bgtz", "s,p", sreg);
252b5132
RH
4268 return;
4269 }
4270 maxnum = 0x7fffffff;
ca4e0257 4271 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
4272 {
4273 maxnum <<= 16;
4274 maxnum |= 0xffff;
4275 maxnum <<= 16;
4276 maxnum |= 0xffff;
4277 }
4278 maxnum = - maxnum - 1;
4279 if (imm_expr.X_op == O_constant
4280 && imm_expr.X_add_number <= maxnum
ca4e0257 4281 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
4282 {
4283 do_true:
4284 /* result is always true */
4285 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
4286 macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
4287 return;
4288 }
4289 set_at (&icnt, sreg, 0);
4290 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4291 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4292 break;
4293
4294 case M_BGEUL:
4295 likely = 1;
4296 case M_BGEU:
4297 if (treg == 0)
4298 goto do_true;
4299 if (sreg == 0)
4300 {
4301 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4302 likely ? "beql" : "beq", "s,t,p", 0, treg);
252b5132
RH
4303 return;
4304 }
2396cfb9
TS
4305 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4306 "d,v,t", AT, sreg, treg);
252b5132 4307 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4308 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4309 break;
4310
4311 case M_BGTUL_I:
4312 likely = 1;
4313 case M_BGTU_I:
4314 if (sreg == 0
ca4e0257 4315 || (HAVE_32BIT_GPRS
252b5132 4316 && imm_expr.X_op == O_constant
956cd1d6 4317 && imm_expr.X_add_number == (offsetT) 0xffffffff))
252b5132
RH
4318 goto do_false;
4319 if (imm_expr.X_op != O_constant)
4320 as_bad (_("Unsupported large constant"));
f9419b05 4321 ++imm_expr.X_add_number;
252b5132
RH
4322 /* FALLTHROUGH */
4323 case M_BGEU_I:
4324 case M_BGEUL_I:
4325 if (mask == M_BGEUL_I)
4326 likely = 1;
4327 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4328 goto do_true;
4329 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4330 {
4331 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4332 likely ? "bnel" : "bne", "s,t,p", sreg, 0);
252b5132
RH
4333 return;
4334 }
4335 set_at (&icnt, sreg, 1);
4336 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4337 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4338 break;
4339
4340 case M_BGTL:
4341 likely = 1;
4342 case M_BGT:
4343 if (treg == 0)
4344 {
4345 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4346 likely ? "bgtzl" : "bgtz", "s,p", sreg);
252b5132
RH
4347 return;
4348 }
4349 if (sreg == 0)
4350 {
4351 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4352 likely ? "bltzl" : "bltz", "s,p", treg);
252b5132
RH
4353 return;
4354 }
2396cfb9
TS
4355 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4356 AT, treg, sreg);
252b5132 4357 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4358 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4359 break;
4360
4361 case M_BGTUL:
4362 likely = 1;
4363 case M_BGTU:
4364 if (treg == 0)
4365 {
4366 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4367 likely ? "bnel" : "bne", "s,t,p", sreg, 0);
252b5132
RH
4368 return;
4369 }
4370 if (sreg == 0)
4371 goto do_false;
2396cfb9
TS
4372 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4373 "d,v,t", AT, treg, sreg);
252b5132 4374 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4375 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4376 break;
4377
4378 case M_BLEL:
4379 likely = 1;
4380 case M_BLE:
4381 if (treg == 0)
4382 {
4383 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4384 likely ? "blezl" : "blez", "s,p", sreg);
252b5132
RH
4385 return;
4386 }
4387 if (sreg == 0)
4388 {
4389 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4390 likely ? "bgezl" : "bgez", "s,p", treg);
252b5132
RH
4391 return;
4392 }
2396cfb9
TS
4393 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4394 AT, treg, sreg);
252b5132 4395 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4396 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4397 break;
4398
4399 case M_BLEL_I:
4400 likely = 1;
4401 case M_BLE_I:
4402 maxnum = 0x7fffffff;
ca4e0257 4403 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
4404 {
4405 maxnum <<= 16;
4406 maxnum |= 0xffff;
4407 maxnum <<= 16;
4408 maxnum |= 0xffff;
4409 }
4410 if (imm_expr.X_op == O_constant
4411 && imm_expr.X_add_number >= maxnum
ca4e0257 4412 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
4413 goto do_true;
4414 if (imm_expr.X_op != O_constant)
4415 as_bad (_("Unsupported large constant"));
f9419b05 4416 ++imm_expr.X_add_number;
252b5132
RH
4417 /* FALLTHROUGH */
4418 case M_BLT_I:
4419 case M_BLTL_I:
4420 if (mask == M_BLTL_I)
4421 likely = 1;
4422 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4423 {
4424 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4425 likely ? "bltzl" : "bltz", "s,p", sreg);
252b5132
RH
4426 return;
4427 }
4428 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4429 {
4430 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4431 likely ? "blezl" : "blez", "s,p", sreg);
252b5132
RH
4432 return;
4433 }
4434 set_at (&icnt, sreg, 0);
4435 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4436 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4437 break;
4438
4439 case M_BLEUL:
4440 likely = 1;
4441 case M_BLEU:
4442 if (treg == 0)
4443 {
4444 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4445 likely ? "beql" : "beq", "s,t,p", sreg, 0);
252b5132
RH
4446 return;
4447 }
4448 if (sreg == 0)
4449 goto do_true;
2396cfb9
TS
4450 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4451 "d,v,t", AT, treg, sreg);
252b5132 4452 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4453 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4454 break;
4455
4456 case M_BLEUL_I:
4457 likely = 1;
4458 case M_BLEU_I:
4459 if (sreg == 0
ca4e0257 4460 || (HAVE_32BIT_GPRS
252b5132 4461 && imm_expr.X_op == O_constant
956cd1d6 4462 && imm_expr.X_add_number == (offsetT) 0xffffffff))
252b5132
RH
4463 goto do_true;
4464 if (imm_expr.X_op != O_constant)
4465 as_bad (_("Unsupported large constant"));
f9419b05 4466 ++imm_expr.X_add_number;
252b5132
RH
4467 /* FALLTHROUGH */
4468 case M_BLTU_I:
4469 case M_BLTUL_I:
4470 if (mask == M_BLTUL_I)
4471 likely = 1;
4472 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4473 goto do_false;
4474 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4475 {
4476 macro_build ((char *) NULL, &icnt, &offset_expr,
4477 likely ? "beql" : "beq",
4478 "s,t,p", sreg, 0);
4479 return;
4480 }
4481 set_at (&icnt, sreg, 1);
4482 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4483 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4484 break;
4485
4486 case M_BLTL:
4487 likely = 1;
4488 case M_BLT:
4489 if (treg == 0)
4490 {
4491 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4492 likely ? "bltzl" : "bltz", "s,p", sreg);
252b5132
RH
4493 return;
4494 }
4495 if (sreg == 0)
4496 {
4497 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4498 likely ? "bgtzl" : "bgtz", "s,p", treg);
252b5132
RH
4499 return;
4500 }
2396cfb9
TS
4501 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4502 AT, sreg, treg);
252b5132 4503 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4504 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4505 break;
4506
4507 case M_BLTUL:
4508 likely = 1;
4509 case M_BLTU:
4510 if (treg == 0)
4511 goto do_false;
4512 if (sreg == 0)
4513 {
4514 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4515 likely ? "bnel" : "bne", "s,t,p", 0, treg);
252b5132
RH
4516 return;
4517 }
2396cfb9
TS
4518 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4519 "d,v,t", AT, sreg,
252b5132
RH
4520 treg);
4521 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4522 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4523 break;
4524
4525 case M_DDIV_3:
4526 dbl = 1;
4527 case M_DIV_3:
4528 s = "mflo";
4529 goto do_div3;
4530 case M_DREM_3:
4531 dbl = 1;
4532 case M_REM_3:
4533 s = "mfhi";
4534 do_div3:
4535 if (treg == 0)
4536 {
4537 as_warn (_("Divide by zero."));
4538 if (mips_trap)
2396cfb9 4539 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
9bd7d936 4540 "s,t,q", 0, 0, 7);
252b5132 4541 else
2396cfb9
TS
4542 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4543 "c", 7);
252b5132
RH
4544 return;
4545 }
4546
b34976b6 4547 mips_emit_delays (TRUE);
252b5132
RH
4548 ++mips_opts.noreorder;
4549 mips_any_noreorder = 1;
4550 if (mips_trap)
4551 {
2396cfb9 4552 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
9bd7d936 4553 "s,t,q", treg, 0, 7);
2396cfb9 4554 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 4555 dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
252b5132
RH
4556 }
4557 else
4558 {
4559 expr1.X_add_number = 8;
4560 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
2396cfb9 4561 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 4562 dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
2396cfb9
TS
4563 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4564 "c", 7);
252b5132
RH
4565 }
4566 expr1.X_add_number = -1;
4567 macro_build ((char *) NULL, &icnt, &expr1,
4568 dbl ? "daddiu" : "addiu",
4569 "t,r,j", AT, 0, (int) BFD_RELOC_LO16);
4570 expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
4571 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, AT);
4572 if (dbl)
4573 {
4574 expr1.X_add_number = 1;
4575 macro_build ((char *) NULL, &icnt, &expr1, "daddiu", "t,r,j", AT, 0,
4576 (int) BFD_RELOC_LO16);
2396cfb9
TS
4577 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsll32",
4578 "d,w,<", AT, AT, 31);
252b5132
RH
4579 }
4580 else
4581 {
4582 expr1.X_add_number = 0x80000000;
4583 macro_build ((char *) NULL, &icnt, &expr1, "lui", "t,u", AT,
4584 (int) BFD_RELOC_HI16);
4585 }
4586 if (mips_trap)
4587 {
2396cfb9 4588 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
9bd7d936 4589 "s,t,q", sreg, AT, 6);
252b5132
RH
4590 /* We want to close the noreorder block as soon as possible, so
4591 that later insns are available for delay slot filling. */
4592 --mips_opts.noreorder;
4593 }
4594 else
4595 {
4596 expr1.X_add_number = 8;
4597 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", sreg, AT);
2396cfb9
TS
4598 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
4599 0);
252b5132
RH
4600
4601 /* We want to close the noreorder block as soon as possible, so
4602 that later insns are available for delay slot filling. */
4603 --mips_opts.noreorder;
4604
2396cfb9
TS
4605 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4606 "c", 6);
252b5132 4607 }
2396cfb9 4608 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d", dreg);
252b5132
RH
4609 break;
4610
4611 case M_DIV_3I:
4612 s = "div";
4613 s2 = "mflo";
4614 goto do_divi;
4615 case M_DIVU_3I:
4616 s = "divu";
4617 s2 = "mflo";
4618 goto do_divi;
4619 case M_REM_3I:
4620 s = "div";
4621 s2 = "mfhi";
4622 goto do_divi;
4623 case M_REMU_3I:
4624 s = "divu";
4625 s2 = "mfhi";
4626 goto do_divi;
4627 case M_DDIV_3I:
4628 dbl = 1;
4629 s = "ddiv";
4630 s2 = "mflo";
4631 goto do_divi;
4632 case M_DDIVU_3I:
4633 dbl = 1;
4634 s = "ddivu";
4635 s2 = "mflo";
4636 goto do_divi;
4637 case M_DREM_3I:
4638 dbl = 1;
4639 s = "ddiv";
4640 s2 = "mfhi";
4641 goto do_divi;
4642 case M_DREMU_3I:
4643 dbl = 1;
4644 s = "ddivu";
4645 s2 = "mfhi";
4646 do_divi:
4647 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4648 {
4649 as_warn (_("Divide by zero."));
4650 if (mips_trap)
2396cfb9 4651 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
9bd7d936 4652 "s,t,q", 0, 0, 7);
252b5132 4653 else
2396cfb9
TS
4654 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4655 "c", 7);
252b5132
RH
4656 return;
4657 }
4658 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4659 {
4660 if (strcmp (s2, "mflo") == 0)
ea1fb5dc 4661 move_register (&icnt, dreg, sreg);
252b5132 4662 else
ea1fb5dc 4663 move_register (&icnt, dreg, 0);
252b5132
RH
4664 return;
4665 }
4666 if (imm_expr.X_op == O_constant
4667 && imm_expr.X_add_number == -1
4668 && s[strlen (s) - 1] != 'u')
4669 {
4670 if (strcmp (s2, "mflo") == 0)
4671 {
2396cfb9
TS
4672 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4673 dbl ? "dneg" : "neg", "d,w", dreg, sreg);
252b5132
RH
4674 }
4675 else
ea1fb5dc 4676 move_register (&icnt, dreg, 0);
252b5132
RH
4677 return;
4678 }
4679
4680 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9
TS
4681 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4682 sreg, AT);
4683 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
252b5132
RH
4684 break;
4685
4686 case M_DIVU_3:
4687 s = "divu";
4688 s2 = "mflo";
4689 goto do_divu3;
4690 case M_REMU_3:
4691 s = "divu";
4692 s2 = "mfhi";
4693 goto do_divu3;
4694 case M_DDIVU_3:
4695 s = "ddivu";
4696 s2 = "mflo";
4697 goto do_divu3;
4698 case M_DREMU_3:
4699 s = "ddivu";
4700 s2 = "mfhi";
4701 do_divu3:
b34976b6 4702 mips_emit_delays (TRUE);
252b5132
RH
4703 ++mips_opts.noreorder;
4704 mips_any_noreorder = 1;
4705 if (mips_trap)
4706 {
2396cfb9 4707 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
9bd7d936 4708 "s,t,q", treg, 0, 7);
2396cfb9
TS
4709 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4710 sreg, treg);
252b5132
RH
4711 /* We want to close the noreorder block as soon as possible, so
4712 that later insns are available for delay slot filling. */
4713 --mips_opts.noreorder;
4714 }
4715 else
4716 {
4717 expr1.X_add_number = 8;
4718 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
2396cfb9
TS
4719 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4720 sreg, treg);
252b5132
RH
4721
4722 /* We want to close the noreorder block as soon as possible, so
4723 that later insns are available for delay slot filling. */
4724 --mips_opts.noreorder;
2396cfb9
TS
4725 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4726 "c", 7);
252b5132 4727 }
2396cfb9 4728 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
252b5132
RH
4729 return;
4730
4731 case M_DLA_AB:
4732 dbl = 1;
4733 case M_LA_AB:
4734 /* Load the address of a symbol into a register. If breg is not
4735 zero, we then add a base register to it. */
4736
3bec30a8
TS
4737 if (dbl && HAVE_32BIT_GPRS)
4738 as_warn (_("dla used to load 32-bit register"));
4739
c90bbe5b 4740 if (! dbl && HAVE_64BIT_OBJECTS)
3bec30a8
TS
4741 as_warn (_("la used to load 64-bit address"));
4742
0c11417f
MR
4743 if (offset_expr.X_op == O_constant
4744 && offset_expr.X_add_number >= -0x8000
4745 && offset_expr.X_add_number < 0x8000)
4746 {
4747 macro_build ((char *) NULL, &icnt, &offset_expr,
4748 (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
4749 "t,r,j", treg, sreg, (int) BFD_RELOC_LO16);
4750 return;
4751 }
4752
afdbd6d0
CD
4753 if (treg == breg)
4754 {
4755 tempreg = AT;
4756 used_at = 1;
4757 }
4758 else
4759 {
4760 tempreg = treg;
4761 used_at = 0;
4762 }
4763
252b5132
RH
4764 /* When generating embedded PIC code, we permit expressions of
4765 the form
afdbd6d0
CD
4766 la $treg,foo-bar
4767 la $treg,foo-bar($breg)
bb2d6cd7 4768 where bar is an address in the current section. These are used
252b5132
RH
4769 when getting the addresses of functions. We don't permit
4770 X_add_number to be non-zero, because if the symbol is
4771 external the relaxing code needs to know that any addend is
4772 purely the offset to X_op_symbol. */
4773 if (mips_pic == EMBEDDED_PIC
4774 && offset_expr.X_op == O_subtract
49309057 4775 && (symbol_constant_p (offset_expr.X_op_symbol)
bb2d6cd7 4776 ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
49309057
ILT
4777 : (symbol_equated_p (offset_expr.X_op_symbol)
4778 && (S_GET_SEGMENT
4779 (symbol_get_value_expression (offset_expr.X_op_symbol)
4780 ->X_add_symbol)
bb2d6cd7 4781 == now_seg)))
bb2d6cd7
GK
4782 && (offset_expr.X_add_number == 0
4783 || OUTPUT_FLAVOR == bfd_target_elf_flavour))
252b5132 4784 {
afdbd6d0
CD
4785 if (breg == 0)
4786 {
4787 tempreg = treg;
4788 used_at = 0;
4789 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4790 tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4791 }
4792 else
4793 {
4794 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4795 tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4796 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4d34fb5f 4797 (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu",
afdbd6d0
CD
4798 "d,v,t", tempreg, tempreg, breg);
4799 }
252b5132 4800 macro_build ((char *) NULL, &icnt, &offset_expr,
4d34fb5f 4801 (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
afdbd6d0
CD
4802 "t,r,j", treg, tempreg, (int) BFD_RELOC_PCREL_LO16);
4803 if (! used_at)
4804 return;
4805 break;
252b5132
RH
4806 }
4807
4808 if (offset_expr.X_op != O_symbol
4809 && offset_expr.X_op != O_constant)
4810 {
4811 as_bad (_("expression too complex"));
4812 offset_expr.X_op = O_constant;
4813 }
4814
252b5132 4815 if (offset_expr.X_op == O_constant)
4d34fb5f
TS
4816 load_register (&icnt, tempreg, &offset_expr,
4817 ((mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
4818 ? (dbl || HAVE_64BIT_ADDRESSES)
4819 : HAVE_64BIT_ADDRESSES));
252b5132
RH
4820 else if (mips_pic == NO_PIC)
4821 {
d6bc6245 4822 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 4823 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
4824 Otherwise we want
4825 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
4826 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4827 If we have a constant, we need two instructions anyhow,
d6bc6245 4828 so we may as well always use the latter form.
76b3015f 4829
d6bc6245
TS
4830 With 64bit address space and a usable $at we want
4831 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4832 lui $at,<sym> (BFD_RELOC_HI16_S)
4833 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4834 daddiu $at,<sym> (BFD_RELOC_LO16)
4835 dsll32 $tempreg,0
3a482fd5 4836 daddu $tempreg,$tempreg,$at
76b3015f 4837
c03099e6 4838 If $at is already in use, we use a path which is suboptimal
d6bc6245
TS
4839 on superscalar processors.
4840 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4841 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4842 dsll $tempreg,16
4843 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
4844 dsll $tempreg,16
4845 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
4846 */
f9419b05 4847 char *p = NULL;
d6bc6245 4848 if (HAVE_64BIT_ADDRESSES)
252b5132 4849 {
d6bc6245
TS
4850 /* We don't do GP optimization for now because RELAX_ENCODE can't
4851 hold the data for such large chunks. */
4852
460597ba 4853 if (used_at == 0 && ! mips_opts.noat)
98d3f06f
KH
4854 {
4855 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4856 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4857 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4858 AT, (int) BFD_RELOC_HI16_S);
4859 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4860 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4861 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4862 AT, AT, (int) BFD_RELOC_LO16);
4863 macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
4864 "d,w,<", tempreg, tempreg, 0);
3a482fd5
MR
4865 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
4866 "d,v,t", tempreg, tempreg, AT);
98d3f06f
KH
4867 used_at = 1;
4868 }
4869 else
4870 {
4871 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4872 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4873 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4874 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4875 macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4876 tempreg, tempreg, 16);
4877 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4878 tempreg, tempreg, (int) BFD_RELOC_HI16_S);
4879 macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4880 tempreg, tempreg, 16);
4881 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4882 tempreg, tempreg, (int) BFD_RELOC_LO16);
4883 }
4884 }
4885 else
4886 {
4887 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4888 && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
4889 {
4890 frag_grow (20);
4891 macro_build ((char *) NULL, &icnt, &offset_expr, "addiu",
c9914766
TS
4892 "t,r,j", tempreg, mips_gp_register,
4893 (int) BFD_RELOC_GPREL16);
98d3f06f
KH
4894 p = frag_var (rs_machine_dependent, 8, 0,
4895 RELAX_ENCODE (4, 8, 0, 4, 0,
4896 mips_opts.warn_about_macros),
4897 offset_expr.X_add_symbol, 0, NULL);
4898 }
4899 macro_build_lui (p, &icnt, &offset_expr, tempreg);
4900 if (p != NULL)
4901 p += 4;
4902 macro_build (p, &icnt, &offset_expr, "addiu",
4903 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4904 }
252b5132
RH
4905 }
4906 else if (mips_pic == SVR4_PIC && ! mips_big_got)
4907 {
9117d219
NC
4908 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
4909
252b5132
RH
4910 /* If this is a reference to an external symbol, and there
4911 is no constant, we want
4912 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9117d219
NC
4913 or if tempreg is PIC_CALL_REG
4914 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
252b5132
RH
4915 For a local symbol, we want
4916 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4917 nop
4918 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4919
4920 If we have a small constant, and this is a reference to
4921 an external symbol, we want
4922 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4923 nop
4924 addiu $tempreg,$tempreg,<constant>
4925 For a local symbol, we want the same instruction
4926 sequence, but we output a BFD_RELOC_LO16 reloc on the
4927 addiu instruction.
4928
4929 If we have a large constant, and this is a reference to
4930 an external symbol, we want
4931 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4932 lui $at,<hiconstant>
4933 addiu $at,$at,<loconstant>
4934 addu $tempreg,$tempreg,$at
4935 For a local symbol, we want the same instruction
4936 sequence, but we output a BFD_RELOC_LO16 reloc on the
ed6fb7bd
SC
4937 addiu instruction.
4938
4939 For NewABI, we want for local or external data addresses
4940 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
4941 For a local function symbol, we want
4942 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
4943 nop
4944 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
4945 */
4946
252b5132
RH
4947 expr1.X_add_number = offset_expr.X_add_number;
4948 offset_expr.X_add_number = 0;
4949 frag_grow (32);
9117d219
NC
4950 if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4951 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
ed6fb7bd
SC
4952 else if (HAVE_NEWABI)
4953 lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_DISP;
4d34fb5f
TS
4954 macro_build ((char *) NULL, &icnt, &offset_expr,
4955 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
c9914766 4956 "t,o(b)", tempreg, lw_reloc_type, mips_gp_register);
252b5132
RH
4957 if (expr1.X_add_number == 0)
4958 {
4959 int off;
f9419b05 4960 char *p;
252b5132
RH
4961
4962 if (breg == 0)
4963 off = 0;
4964 else
4965 {
4966 /* We're going to put in an addu instruction using
4967 tempreg, so we may as well insert the nop right
4968 now. */
4969 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4970 "nop", "");
4971 off = 4;
4972 }
4973 p = frag_var (rs_machine_dependent, 8 - off, 0,
4974 RELAX_ENCODE (0, 8 - off, -4 - off, 4 - off, 0,
4975 (breg == 0
4976 ? mips_opts.warn_about_macros
4977 : 0)),
c4e7957c 4978 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
4979 if (breg == 0)
4980 {
4981 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4982 p += 4;
4983 }
4984 macro_build (p, &icnt, &expr1,
ca4e0257 4985 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
4986 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4987 /* FIXME: If breg == 0, and the next instruction uses
4988 $tempreg, then if this variant case is used an extra
4989 nop will be generated. */
4990 }
4991 else if (expr1.X_add_number >= -0x8000
4992 && expr1.X_add_number < 0x8000)
4993 {
4994 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4995 "nop", "");
4996 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 4997 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132 4998 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
c4e7957c
TS
4999 frag_var (rs_machine_dependent, 0, 0,
5000 RELAX_ENCODE (0, 0, -12, -4, 0, 0),
5001 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5002 }
5003 else
5004 {
5005 int off1;
5006
5007 /* If we are going to add in a base register, and the
5008 target register and the base register are the same,
5009 then we are using AT as a temporary register. Since
5010 we want to load the constant into AT, we add our
5011 current AT (from the global offset table) and the
5012 register into the register now, and pretend we were
5013 not using a base register. */
5014 if (breg != treg)
5015 off1 = 0;
5016 else
5017 {
5018 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5019 "nop", "");
5020 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5021 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5022 "d,v,t", treg, AT, breg);
5023 breg = 0;
5024 tempreg = treg;
5025 off1 = -8;
5026 }
5027
5028 /* Set mips_optimize around the lui instruction to avoid
5029 inserting an unnecessary nop after the lw. */
5030 hold_mips_optimize = mips_optimize;
5031 mips_optimize = 2;
c4e7957c 5032 macro_build_lui (NULL, &icnt, &expr1, AT);
252b5132
RH
5033 mips_optimize = hold_mips_optimize;
5034
5035 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 5036 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
5037 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5038 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5039 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132 5040 "d,v,t", tempreg, tempreg, AT);
c4e7957c
TS
5041 frag_var (rs_machine_dependent, 0, 0,
5042 RELAX_ENCODE (0, 0, -16 + off1, -8, 0, 0),
5043 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5044 used_at = 1;
5045 }
5046 }
5047 else if (mips_pic == SVR4_PIC)
5048 {
5049 int gpdel;
f9419b05 5050 char *p;
9117d219
NC
5051 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
5052 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
ed6fb7bd 5053 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
252b5132
RH
5054
5055 /* This is the large GOT case. If this is a reference to an
5056 external symbol, and there is no constant, we want
5057 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5058 addu $tempreg,$tempreg,$gp
5059 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
9117d219
NC
5060 or if tempreg is PIC_CALL_REG
5061 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5062 addu $tempreg,$tempreg,$gp
5063 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
252b5132
RH
5064 For a local symbol, we want
5065 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5066 nop
5067 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5068
5069 If we have a small constant, and this is a reference to
5070 an external symbol, we want
5071 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5072 addu $tempreg,$tempreg,$gp
5073 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5074 nop
5075 addiu $tempreg,$tempreg,<constant>
5076 For a local symbol, we want
5077 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5078 nop
5079 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
5080
5081 If we have a large constant, and this is a reference to
5082 an external symbol, we want
5083 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5084 addu $tempreg,$tempreg,$gp
5085 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5086 lui $at,<hiconstant>
5087 addiu $at,$at,<loconstant>
5088 addu $tempreg,$tempreg,$at
5089 For a local symbol, we want
5090 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5091 lui $at,<hiconstant>
5092 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
5093 addu $tempreg,$tempreg,$at
438c16b8 5094
ed6fb7bd
SC
5095 For NewABI, we want for local data addresses
5096 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
438c16b8 5097 */
438c16b8 5098
252b5132
RH
5099 expr1.X_add_number = offset_expr.X_add_number;
5100 offset_expr.X_add_number = 0;
5101 frag_grow (52);
f7ea7ef2 5102 if (reg_needs_delay (mips_gp_register))
252b5132
RH
5103 gpdel = 4;
5104 else
5105 gpdel = 0;
9117d219
NC
5106 if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
5107 {
5108 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5109 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5110 }
252b5132 5111 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
9117d219 5112 tempreg, lui_reloc_type);
252b5132 5113 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5114 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 5115 "d,v,t", tempreg, tempreg, mips_gp_register);
252b5132 5116 macro_build ((char *) NULL, &icnt, &offset_expr,
4d34fb5f 5117 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
9117d219 5118 "t,o(b)", tempreg, lw_reloc_type, tempreg);
252b5132
RH
5119 if (expr1.X_add_number == 0)
5120 {
5121 int off;
5122
5123 if (breg == 0)
5124 off = 0;
5125 else
5126 {
5127 /* We're going to put in an addu instruction using
5128 tempreg, so we may as well insert the nop right
5129 now. */
5130 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5131 "nop", "");
5132 off = 4;
5133 }
5134
5135 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5136 RELAX_ENCODE (12 + off, 12 + gpdel, gpdel,
5137 8 + gpdel, 0,
5138 (breg == 0
5139 ? mips_opts.warn_about_macros
5140 : 0)),
c4e7957c 5141 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5142 }
5143 else if (expr1.X_add_number >= -0x8000
5144 && expr1.X_add_number < 0x8000)
5145 {
5146 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5147 "nop", "");
5148 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 5149 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
5150 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5151
5152 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5153 RELAX_ENCODE (20, 12 + gpdel, gpdel, 8 + gpdel, 0,
5154 (breg == 0
5155 ? mips_opts.warn_about_macros
5156 : 0)),
c4e7957c 5157 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5158 }
5159 else
5160 {
5161 int adj, dreg;
5162
5163 /* If we are going to add in a base register, and the
5164 target register and the base register are the same,
5165 then we are using AT as a temporary register. Since
5166 we want to load the constant into AT, we add our
5167 current AT (from the global offset table) and the
5168 register into the register now, and pretend we were
5169 not using a base register. */
5170 if (breg != treg)
5171 {
5172 adj = 0;
5173 dreg = tempreg;
5174 }
5175 else
5176 {
5177 assert (tempreg == AT);
5178 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5179 "nop", "");
5180 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5181 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5182 "d,v,t", treg, AT, breg);
5183 dreg = treg;
5184 adj = 8;
5185 }
5186
5187 /* Set mips_optimize around the lui instruction to avoid
5188 inserting an unnecessary nop after the lw. */
5189 hold_mips_optimize = mips_optimize;
5190 mips_optimize = 2;
c4e7957c 5191 macro_build_lui (NULL, &icnt, &expr1, AT);
252b5132
RH
5192 mips_optimize = hold_mips_optimize;
5193
5194 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 5195 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
5196 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5197 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5198 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5199 "d,v,t", dreg, dreg, AT);
5200
5201 p = frag_var (rs_machine_dependent, 16 + gpdel + adj, 0,
5202 RELAX_ENCODE (24 + adj, 16 + gpdel + adj, gpdel,
5203 8 + gpdel, 0,
5204 (breg == 0
5205 ? mips_opts.warn_about_macros
5206 : 0)),
c4e7957c 5207 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5208
5209 used_at = 1;
5210 }
5211
5212 if (gpdel > 0)
5213 {
5214 /* This is needed because this instruction uses $gp, but
5215 the first instruction on the main stream does not. */
5216 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5217 p += 4;
5218 }
ed6fb7bd
SC
5219
5220 if (HAVE_NEWABI)
5221 local_reloc_type = (int) BFD_RELOC_MIPS_GOT_DISP;
252b5132 5222 macro_build (p, &icnt, &offset_expr,
4d34fb5f 5223 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
ed6fb7bd
SC
5224 "t,o(b)", tempreg,
5225 local_reloc_type,
c9914766 5226 mips_gp_register);
252b5132 5227 p += 4;
ed6fb7bd
SC
5228 if (expr1.X_add_number == 0 && HAVE_NEWABI)
5229 {
5230 /* BFD_RELOC_MIPS_GOT_DISP is sufficient for newabi */
5231 }
5232 else
5233 if (expr1.X_add_number >= -0x8000
252b5132
RH
5234 && expr1.X_add_number < 0x8000)
5235 {
5236 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5237 p += 4;
5238 macro_build (p, &icnt, &expr1,
ca4e0257 5239 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
5240 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5241 /* FIXME: If add_number is 0, and there was no base
5242 register, the external symbol case ended with a load,
5243 so if the symbol turns out to not be external, and
5244 the next instruction uses tempreg, an unnecessary nop
5245 will be inserted. */
5246 }
5247 else
5248 {
5249 if (breg == treg)
5250 {
5251 /* We must add in the base register now, as in the
5252 external symbol case. */
5253 assert (tempreg == AT);
5254 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5255 p += 4;
5256 macro_build (p, &icnt, (expressionS *) NULL,
ca4e0257 5257 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5258 "d,v,t", treg, AT, breg);
5259 p += 4;
5260 tempreg = treg;
5261 /* We set breg to 0 because we have arranged to add
5262 it in in both cases. */
5263 breg = 0;
5264 }
5265
5266 macro_build_lui (p, &icnt, &expr1, AT);
5267 p += 4;
5268 macro_build (p, &icnt, &expr1,
ca4e0257 5269 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
5270 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5271 p += 4;
5272 macro_build (p, &icnt, (expressionS *) NULL,
ca4e0257 5273 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5274 "d,v,t", tempreg, tempreg, AT);
5275 p += 4;
5276 }
5277 }
5278 else if (mips_pic == EMBEDDED_PIC)
5279 {
5280 /* We use
cdf6fd85 5281 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
5282 */
5283 macro_build ((char *) NULL, &icnt, &offset_expr,
c9914766
TS
5284 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
5285 tempreg, mips_gp_register, (int) BFD_RELOC_GPREL16);
252b5132
RH
5286 }
5287 else
5288 abort ();
5289
5290 if (breg != 0)
4d34fb5f
TS
5291 {
5292 char *s;
5293
5294 if (mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
5295 s = (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu";
5296 else
5297 s = HAVE_64BIT_ADDRESSES ? "daddu" : "addu";
5298
5299 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s,
5300 "d,v,t", treg, tempreg, breg);
5301 }
252b5132
RH
5302
5303 if (! used_at)
5304 return;
5305
5306 break;
5307
5308 case M_J_A:
5309 /* The j instruction may not be used in PIC code, since it
5310 requires an absolute address. We convert it to a b
5311 instruction. */
5312 if (mips_pic == NO_PIC)
5313 macro_build ((char *) NULL, &icnt, &offset_expr, "j", "a");
5314 else
5315 macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
5316 return;
5317
5318 /* The jal instructions must be handled as macros because when
5319 generating PIC code they expand to multi-instruction
5320 sequences. Normally they are simple instructions. */
5321 case M_JAL_1:
5322 dreg = RA;
5323 /* Fall through. */
5324 case M_JAL_2:
5325 if (mips_pic == NO_PIC
5326 || mips_pic == EMBEDDED_PIC)
5327 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5328 "d,s", dreg, sreg);
5329 else if (mips_pic == SVR4_PIC)
5330 {
5331 if (sreg != PIC_CALL_REG)
5332 as_warn (_("MIPS PIC call to register other than $25"));
bdaaa2e1 5333
252b5132
RH
5334 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5335 "d,s", dreg, sreg);
6478892d 5336 if (! HAVE_NEWABI)
252b5132 5337 {
6478892d
TS
5338 if (mips_cprestore_offset < 0)
5339 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5340 else
5341 {
7a621144
DJ
5342 if (! mips_frame_reg_valid)
5343 {
5344 as_warn (_("No .frame pseudo-op used in PIC code"));
5345 /* Quiet this warning. */
5346 mips_frame_reg_valid = 1;
5347 }
5348 if (! mips_cprestore_valid)
5349 {
5350 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5351 /* Quiet this warning. */
5352 mips_cprestore_valid = 1;
5353 }
6478892d 5354 expr1.X_add_number = mips_cprestore_offset;
885add95
CD
5355 macro_build_ldst_constoffset ((char *) NULL, &icnt, &expr1,
5356 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5357 mips_gp_register, mips_frame_reg);
6478892d 5358 }
252b5132
RH
5359 }
5360 }
5361 else
5362 abort ();
5363
5364 return;
5365
5366 case M_JAL_A:
5367 if (mips_pic == NO_PIC)
5368 macro_build ((char *) NULL, &icnt, &offset_expr, "jal", "a");
5369 else if (mips_pic == SVR4_PIC)
5370 {
f9419b05
TS
5371 char *p;
5372
252b5132
RH
5373 /* If this is a reference to an external symbol, and we are
5374 using a small GOT, we want
5375 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5376 nop
f9419b05 5377 jalr $ra,$25
252b5132
RH
5378 nop
5379 lw $gp,cprestore($sp)
5380 The cprestore value is set using the .cprestore
5381 pseudo-op. If we are using a big GOT, we want
5382 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5383 addu $25,$25,$gp
5384 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
5385 nop
f9419b05 5386 jalr $ra,$25
252b5132
RH
5387 nop
5388 lw $gp,cprestore($sp)
5389 If the symbol is not external, we want
5390 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5391 nop
5392 addiu $25,$25,<sym> (BFD_RELOC_LO16)
f9419b05 5393 jalr $ra,$25
252b5132 5394 nop
438c16b8
TS
5395 lw $gp,cprestore($sp)
5396 For NewABI, we want
5397 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5398 jalr $ra,$25 (BFD_RELOC_MIPS_JALR)
5399 */
5400 if (HAVE_NEWABI)
252b5132
RH
5401 {
5402 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 5403 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132 5404 "t,o(b)", PIC_CALL_REG,
438c16b8
TS
5405 (int) BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5406 macro_build_jalr (icnt, &offset_expr);
252b5132
RH
5407 }
5408 else
5409 {
438c16b8
TS
5410 frag_grow (40);
5411 if (! mips_big_got)
5412 {
5413 macro_build ((char *) NULL, &icnt, &offset_expr,
5414 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5415 "t,o(b)", PIC_CALL_REG,
5416 (int) BFD_RELOC_MIPS_CALL16, mips_gp_register);
5417 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5418 "nop", "");
5419 p = frag_var (rs_machine_dependent, 4, 0,
5420 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5421 offset_expr.X_add_symbol, 0, NULL);
5422 }
252b5132 5423 else
252b5132 5424 {
438c16b8
TS
5425 int gpdel;
5426
5427 if (reg_needs_delay (mips_gp_register))
5428 gpdel = 4;
5429 else
5430 gpdel = 0;
5431 macro_build ((char *) NULL, &icnt, &offset_expr, "lui",
5432 "t,u", PIC_CALL_REG,
5433 (int) BFD_RELOC_MIPS_CALL_HI16);
5434 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5435 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5436 "d,v,t", PIC_CALL_REG, PIC_CALL_REG,
5437 mips_gp_register);
5438 macro_build ((char *) NULL, &icnt, &offset_expr,
5439 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5440 "t,o(b)", PIC_CALL_REG,
5441 (int) BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
5442 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5443 "nop", "");
5444 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5445 RELAX_ENCODE (16, 12 + gpdel, gpdel,
5446 8 + gpdel, 0, 0),
5447 offset_expr.X_add_symbol, 0, NULL);
5448 if (gpdel > 0)
5449 {
5450 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5451 p += 4;
5452 }
5453 macro_build (p, &icnt, &offset_expr,
5454 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5455 "t,o(b)", PIC_CALL_REG,
5456 (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
5457 p += 4;
252b5132
RH
5458 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5459 p += 4;
5460 }
5461 macro_build (p, &icnt, &offset_expr,
438c16b8
TS
5462 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5463 "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
5464 (int) BFD_RELOC_LO16);
5465 macro_build_jalr (icnt, &offset_expr);
5466
6478892d
TS
5467 if (mips_cprestore_offset < 0)
5468 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5469 else
5470 {
7a621144
DJ
5471 if (! mips_frame_reg_valid)
5472 {
5473 as_warn (_("No .frame pseudo-op used in PIC code"));
5474 /* Quiet this warning. */
5475 mips_frame_reg_valid = 1;
5476 }
5477 if (! mips_cprestore_valid)
5478 {
5479 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5480 /* Quiet this warning. */
5481 mips_cprestore_valid = 1;
5482 }
6478892d
TS
5483 if (mips_opts.noreorder)
5484 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
98d3f06f 5485 "nop", "");
6478892d 5486 expr1.X_add_number = mips_cprestore_offset;
885add95
CD
5487 macro_build_ldst_constoffset ((char *) NULL, &icnt, &expr1,
5488 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5489 mips_gp_register, mips_frame_reg);
6478892d 5490 }
252b5132
RH
5491 }
5492 }
5493 else if (mips_pic == EMBEDDED_PIC)
5494 {
5495 macro_build ((char *) NULL, &icnt, &offset_expr, "bal", "p");
5496 /* The linker may expand the call to a longer sequence which
5497 uses $at, so we must break rather than return. */
5498 break;
5499 }
5500 else
5501 abort ();
5502
5503 return;
5504
5505 case M_LB_AB:
5506 s = "lb";
5507 goto ld;
5508 case M_LBU_AB:
5509 s = "lbu";
5510 goto ld;
5511 case M_LH_AB:
5512 s = "lh";
5513 goto ld;
5514 case M_LHU_AB:
5515 s = "lhu";
5516 goto ld;
5517 case M_LW_AB:
5518 s = "lw";
5519 goto ld;
5520 case M_LWC0_AB:
5521 s = "lwc0";
bdaaa2e1 5522 /* Itbl support may require additional care here. */
252b5132
RH
5523 coproc = 1;
5524 goto ld;
5525 case M_LWC1_AB:
5526 s = "lwc1";
bdaaa2e1 5527 /* Itbl support may require additional care here. */
252b5132
RH
5528 coproc = 1;
5529 goto ld;
5530 case M_LWC2_AB:
5531 s = "lwc2";
bdaaa2e1 5532 /* Itbl support may require additional care here. */
252b5132
RH
5533 coproc = 1;
5534 goto ld;
5535 case M_LWC3_AB:
5536 s = "lwc3";
bdaaa2e1 5537 /* Itbl support may require additional care here. */
252b5132
RH
5538 coproc = 1;
5539 goto ld;
5540 case M_LWL_AB:
5541 s = "lwl";
5542 lr = 1;
5543 goto ld;
5544 case M_LWR_AB:
5545 s = "lwr";
5546 lr = 1;
5547 goto ld;
5548 case M_LDC1_AB:
ec68c924 5549 if (mips_arch == CPU_R4650)
252b5132
RH
5550 {
5551 as_bad (_("opcode not supported on this processor"));
5552 return;
5553 }
5554 s = "ldc1";
bdaaa2e1 5555 /* Itbl support may require additional care here. */
252b5132
RH
5556 coproc = 1;
5557 goto ld;
5558 case M_LDC2_AB:
5559 s = "ldc2";
bdaaa2e1 5560 /* Itbl support may require additional care here. */
252b5132
RH
5561 coproc = 1;
5562 goto ld;
5563 case M_LDC3_AB:
5564 s = "ldc3";
bdaaa2e1 5565 /* Itbl support may require additional care here. */
252b5132
RH
5566 coproc = 1;
5567 goto ld;
5568 case M_LDL_AB:
5569 s = "ldl";
5570 lr = 1;
5571 goto ld;
5572 case M_LDR_AB:
5573 s = "ldr";
5574 lr = 1;
5575 goto ld;
5576 case M_LL_AB:
5577 s = "ll";
5578 goto ld;
5579 case M_LLD_AB:
5580 s = "lld";
5581 goto ld;
5582 case M_LWU_AB:
5583 s = "lwu";
5584 ld:
5585 if (breg == treg || coproc || lr)
5586 {
5587 tempreg = AT;
5588 used_at = 1;
5589 }
5590 else
5591 {
5592 tempreg = treg;
5593 used_at = 0;
5594 }
5595 goto ld_st;
5596 case M_SB_AB:
5597 s = "sb";
5598 goto st;
5599 case M_SH_AB:
5600 s = "sh";
5601 goto st;
5602 case M_SW_AB:
5603 s = "sw";
5604 goto st;
5605 case M_SWC0_AB:
5606 s = "swc0";
bdaaa2e1 5607 /* Itbl support may require additional care here. */
252b5132
RH
5608 coproc = 1;
5609 goto st;
5610 case M_SWC1_AB:
5611 s = "swc1";
bdaaa2e1 5612 /* Itbl support may require additional care here. */
252b5132
RH
5613 coproc = 1;
5614 goto st;
5615 case M_SWC2_AB:
5616 s = "swc2";
bdaaa2e1 5617 /* Itbl support may require additional care here. */
252b5132
RH
5618 coproc = 1;
5619 goto st;
5620 case M_SWC3_AB:
5621 s = "swc3";
bdaaa2e1 5622 /* Itbl support may require additional care here. */
252b5132
RH
5623 coproc = 1;
5624 goto st;
5625 case M_SWL_AB:
5626 s = "swl";
5627 goto st;
5628 case M_SWR_AB:
5629 s = "swr";
5630 goto st;
5631 case M_SC_AB:
5632 s = "sc";
5633 goto st;
5634 case M_SCD_AB:
5635 s = "scd";
5636 goto st;
5637 case M_SDC1_AB:
ec68c924 5638 if (mips_arch == CPU_R4650)
252b5132
RH
5639 {
5640 as_bad (_("opcode not supported on this processor"));
5641 return;
5642 }
5643 s = "sdc1";
5644 coproc = 1;
bdaaa2e1 5645 /* Itbl support may require additional care here. */
252b5132
RH
5646 goto st;
5647 case M_SDC2_AB:
5648 s = "sdc2";
bdaaa2e1 5649 /* Itbl support may require additional care here. */
252b5132
RH
5650 coproc = 1;
5651 goto st;
5652 case M_SDC3_AB:
5653 s = "sdc3";
bdaaa2e1 5654 /* Itbl support may require additional care here. */
252b5132
RH
5655 coproc = 1;
5656 goto st;
5657 case M_SDL_AB:
5658 s = "sdl";
5659 goto st;
5660 case M_SDR_AB:
5661 s = "sdr";
5662 st:
5663 tempreg = AT;
5664 used_at = 1;
5665 ld_st:
bdaaa2e1 5666 /* Itbl support may require additional care here. */
252b5132
RH
5667 if (mask == M_LWC1_AB
5668 || mask == M_SWC1_AB
5669 || mask == M_LDC1_AB
5670 || mask == M_SDC1_AB
5671 || mask == M_L_DAB
5672 || mask == M_S_DAB)
5673 fmt = "T,o(b)";
5674 else if (coproc)
5675 fmt = "E,o(b)";
5676 else
5677 fmt = "t,o(b)";
5678
afdbd6d0
CD
5679 /* For embedded PIC, we allow loads where the offset is calculated
5680 by subtracting a symbol in the current segment from an unknown
5681 symbol, relative to a base register, e.g.:
5682 <op> $treg, <sym>-<localsym>($breg)
5683 This is used by the compiler for switch statements. */
76b3015f 5684 if (mips_pic == EMBEDDED_PIC
afdbd6d0
CD
5685 && offset_expr.X_op == O_subtract
5686 && (symbol_constant_p (offset_expr.X_op_symbol)
5687 ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
5688 : (symbol_equated_p (offset_expr.X_op_symbol)
5689 && (S_GET_SEGMENT
5690 (symbol_get_value_expression (offset_expr.X_op_symbol)
5691 ->X_add_symbol)
5692 == now_seg)))
5693 && breg != 0
5694 && (offset_expr.X_add_number == 0
5695 || OUTPUT_FLAVOR == bfd_target_elf_flavour))
5696 {
5697 /* For this case, we output the instructions:
5698 lui $tempreg,<sym> (BFD_RELOC_PCREL_HI16_S)
5699 addiu $tempreg,$tempreg,$breg
5700 <op> $treg,<sym>($tempreg) (BFD_RELOC_PCREL_LO16)
5701 If the relocation would fit entirely in 16 bits, it would be
5702 nice to emit:
5703 <op> $treg,<sym>($breg) (BFD_RELOC_PCREL_LO16)
5704 instead, but that seems quite difficult. */
5705 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5706 tempreg, (int) BFD_RELOC_PCREL_HI16_S);
5707 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5708 ((bfd_arch_bits_per_address (stdoutput) == 32
5709 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
5710 ? "addu" : "daddu"),
5711 "d,v,t", tempreg, tempreg, breg);
5712 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt, treg,
5713 (int) BFD_RELOC_PCREL_LO16, tempreg);
5714 if (! used_at)
5715 return;
5716 break;
5717 }
5718
252b5132
RH
5719 if (offset_expr.X_op != O_constant
5720 && offset_expr.X_op != O_symbol)
5721 {
5722 as_bad (_("expression too complex"));
5723 offset_expr.X_op = O_constant;
5724 }
5725
5726 /* A constant expression in PIC code can be handled just as it
5727 is in non PIC code. */
5728 if (mips_pic == NO_PIC
5729 || offset_expr.X_op == O_constant)
5730 {
f9419b05
TS
5731 char *p;
5732
252b5132
RH
5733 /* If this is a reference to a GP relative symbol, and there
5734 is no base register, we want
cdf6fd85 5735 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
5736 Otherwise, if there is no base register, we want
5737 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5738 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5739 If we have a constant, we need two instructions anyhow,
5740 so we always use the latter form.
5741
5742 If we have a base register, and this is a reference to a
5743 GP relative symbol, we want
5744 addu $tempreg,$breg,$gp
cdf6fd85 5745 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
5746 Otherwise we want
5747 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5748 addu $tempreg,$tempreg,$breg
5749 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245 5750 With a constant we always use the latter case.
76b3015f 5751
d6bc6245
TS
5752 With 64bit address space and no base register and $at usable,
5753 we want
5754 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5755 lui $at,<sym> (BFD_RELOC_HI16_S)
5756 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5757 dsll32 $tempreg,0
5758 daddu $tempreg,$at
5759 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5760 If we have a base register, we want
5761 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5762 lui $at,<sym> (BFD_RELOC_HI16_S)
5763 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5764 daddu $at,$breg
5765 dsll32 $tempreg,0
5766 daddu $tempreg,$at
5767 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5768
5769 Without $at we can't generate the optimal path for superscalar
5770 processors here since this would require two temporary registers.
5771 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5772 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5773 dsll $tempreg,16
5774 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5775 dsll $tempreg,16
5776 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5777 If we have a base register, we want
5778 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5779 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5780 dsll $tempreg,16
5781 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5782 dsll $tempreg,16
5783 daddu $tempreg,$tempreg,$breg
5784 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6373ee54
CD
5785
5786 If we have 64-bit addresses, as an optimization, for
5787 addresses which are 32-bit constants (e.g. kseg0/kseg1
5788 addresses) we fall back to the 32-bit address generation
78d32a17
MR
5789 mechanism since it is more efficient. Note that due to
5790 the signed offset used by memory operations, the 32-bit
5791 range is shifted down by 32768 here. This code should
6373ee54
CD
5792 probably attempt to generate 64-bit constants more
5793 efficiently in general.
d6bc6245 5794 */
6373ee54
CD
5795 if (HAVE_64BIT_ADDRESSES
5796 && !(offset_expr.X_op == O_constant
78d32a17 5797 && IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000)))
d6bc6245
TS
5798 {
5799 p = NULL;
5800
5801 /* We don't do GP optimization for now because RELAX_ENCODE can't
5802 hold the data for such large chunks. */
5803
460597ba 5804 if (used_at == 0 && ! mips_opts.noat)
d6bc6245
TS
5805 {
5806 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5807 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5808 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5809 AT, (int) BFD_RELOC_HI16_S);
5810 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5811 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5812 if (breg != 0)
2396cfb9
TS
5813 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5814 "d,v,t", AT, AT, breg);
5815 macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
5816 "d,w,<", tempreg, tempreg, 0);
5817 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5818 "d,v,t", tempreg, tempreg, AT);
d6bc6245
TS
5819 macro_build (p, &icnt, &offset_expr, s,
5820 fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5821 used_at = 1;
5822 }
5823 else
5824 {
5825 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5826 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5827 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5828 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
2396cfb9
TS
5829 macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5830 "d,w,<", tempreg, tempreg, 16);
d6bc6245
TS
5831 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5832 tempreg, tempreg, (int) BFD_RELOC_HI16_S);
2396cfb9
TS
5833 macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5834 "d,w,<", tempreg, tempreg, 16);
d6bc6245 5835 if (breg != 0)
2396cfb9
TS
5836 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5837 "d,v,t", tempreg, tempreg, breg);
d6bc6245
TS
5838 macro_build (p, &icnt, &offset_expr, s,
5839 fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5840 }
5841
5842 return;
5843 }
76b3015f 5844
252b5132
RH
5845 if (breg == 0)
5846 {
e7d556df 5847 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
252b5132
RH
5848 || nopic_need_relax (offset_expr.X_add_symbol, 1))
5849 p = NULL;
5850 else
5851 {
5852 frag_grow (20);
5853 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
c9914766
TS
5854 treg, (int) BFD_RELOC_GPREL16,
5855 mips_gp_register);
252b5132
RH
5856 p = frag_var (rs_machine_dependent, 8, 0,
5857 RELAX_ENCODE (4, 8, 0, 4, 0,
5858 (mips_opts.warn_about_macros
5859 || (used_at
5860 && mips_opts.noat))),
956cd1d6 5861 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5862 used_at = 0;
5863 }
5864 macro_build_lui (p, &icnt, &offset_expr, tempreg);
5865 if (p != NULL)
5866 p += 4;
5867 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5868 (int) BFD_RELOC_LO16, tempreg);
5869 }
5870 else
5871 {
e7d556df 5872 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
252b5132
RH
5873 || nopic_need_relax (offset_expr.X_add_symbol, 1))
5874 p = NULL;
5875 else
5876 {
5877 frag_grow (28);
5878 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5879 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 5880 "d,v,t", tempreg, breg, mips_gp_register);
252b5132 5881 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
cdf6fd85 5882 treg, (int) BFD_RELOC_GPREL16, tempreg);
252b5132
RH
5883 p = frag_var (rs_machine_dependent, 12, 0,
5884 RELAX_ENCODE (8, 12, 0, 8, 0, 0),
956cd1d6 5885 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5886 }
5887 macro_build_lui (p, &icnt, &offset_expr, tempreg);
5888 if (p != NULL)
5889 p += 4;
5890 macro_build (p, &icnt, (expressionS *) NULL,
ca4e0257 5891 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5892 "d,v,t", tempreg, tempreg, breg);
5893 if (p != NULL)
5894 p += 4;
5895 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5896 (int) BFD_RELOC_LO16, tempreg);
5897 }
5898 }
5899 else if (mips_pic == SVR4_PIC && ! mips_big_got)
5900 {
f9419b05 5901 char *p;
ed6fb7bd 5902 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
f9419b05 5903
252b5132
RH
5904 /* If this is a reference to an external symbol, we want
5905 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5906 nop
5907 <op> $treg,0($tempreg)
5908 Otherwise we want
5909 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5910 nop
5911 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5912 <op> $treg,0($tempreg)
ed6fb7bd
SC
5913 If we have NewABI, we want
5914 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
252b5132
RH
5915 If there is a base register, we add it to $tempreg before
5916 the <op>. If there is a constant, we stick it in the
5917 <op> instruction. We don't handle constants larger than
5918 16 bits, because we have no way to load the upper 16 bits
5919 (actually, we could handle them for the subset of cases
5920 in which we are not using $at). */
5921 assert (offset_expr.X_op == O_symbol);
5922 expr1.X_add_number = offset_expr.X_add_number;
5923 offset_expr.X_add_number = 0;
ed6fb7bd
SC
5924 if (HAVE_NEWABI)
5925 lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_DISP;
252b5132
RH
5926 if (expr1.X_add_number < -0x8000
5927 || expr1.X_add_number >= 0x8000)
5928 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5929 frag_grow (20);
5930 macro_build ((char *) NULL, &icnt, &offset_expr,
c9914766 5931 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", tempreg,
ed6fb7bd 5932 (int) lw_reloc_type, mips_gp_register);
252b5132 5933 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
bdaaa2e1 5934 p = frag_var (rs_machine_dependent, 4, 0,
252b5132 5935 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
c4e7957c 5936 offset_expr.X_add_symbol, 0, NULL);
252b5132 5937 macro_build (p, &icnt, &offset_expr,
ca4e0257 5938 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
5939 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5940 if (breg != 0)
5941 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5942 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5943 "d,v,t", tempreg, tempreg, breg);
5944 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5945 (int) BFD_RELOC_LO16, tempreg);
5946 }
5947 else if (mips_pic == SVR4_PIC)
5948 {
5949 int gpdel;
f9419b05 5950 char *p;
252b5132
RH
5951
5952 /* If this is a reference to an external symbol, we want
5953 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5954 addu $tempreg,$tempreg,$gp
5955 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5956 <op> $treg,0($tempreg)
5957 Otherwise we want
5958 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5959 nop
5960 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5961 <op> $treg,0($tempreg)
5962 If there is a base register, we add it to $tempreg before
5963 the <op>. If there is a constant, we stick it in the
5964 <op> instruction. We don't handle constants larger than
5965 16 bits, because we have no way to load the upper 16 bits
5966 (actually, we could handle them for the subset of cases
438c16b8
TS
5967 in which we are not using $at).
5968
5969 For NewABI, we want
5970 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
5971 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
5972 <op> $treg,0($tempreg)
5973 */
252b5132
RH
5974 assert (offset_expr.X_op == O_symbol);
5975 expr1.X_add_number = offset_expr.X_add_number;
5976 offset_expr.X_add_number = 0;
5977 if (expr1.X_add_number < -0x8000
5978 || expr1.X_add_number >= 0x8000)
5979 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
438c16b8
TS
5980 if (HAVE_NEWABI)
5981 {
5982 macro_build ((char *) NULL, &icnt, &offset_expr,
5983 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5984 "t,o(b)", tempreg, BFD_RELOC_MIPS_GOT_PAGE,
5985 mips_gp_register);
5986 macro_build ((char *) NULL, &icnt, &offset_expr,
5987 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5988 "t,r,j", tempreg, tempreg,
5989 BFD_RELOC_MIPS_GOT_OFST);
5990 if (breg != 0)
5991 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5992 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5993 "d,v,t", tempreg, tempreg, breg);
5994 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5995 (int) BFD_RELOC_LO16, tempreg);
5996
5997 if (! used_at)
5998 return;
5999
6000 break;
6001 }
f7ea7ef2 6002 if (reg_needs_delay (mips_gp_register))
252b5132
RH
6003 gpdel = 4;
6004 else
6005 gpdel = 0;
6006 frag_grow (36);
6007 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
6008 tempreg, (int) BFD_RELOC_MIPS_GOT_HI16);
6009 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6010 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 6011 "d,v,t", tempreg, tempreg, mips_gp_register);
252b5132 6012 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 6013 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
6014 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_LO16,
6015 tempreg);
6016 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
6017 RELAX_ENCODE (12, 12 + gpdel, gpdel, 8 + gpdel, 0, 0),
c4e7957c 6018 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
6019 if (gpdel > 0)
6020 {
6021 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6022 p += 4;
6023 }
6024 macro_build (p, &icnt, &offset_expr,
ca4e0257 6025 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
c9914766
TS
6026 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16,
6027 mips_gp_register);
252b5132
RH
6028 p += 4;
6029 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6030 p += 4;
6031 macro_build (p, &icnt, &offset_expr,
ca4e0257 6032 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
6033 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
6034 if (breg != 0)
6035 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6036 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
6037 "d,v,t", tempreg, tempreg, breg);
6038 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
6039 (int) BFD_RELOC_LO16, tempreg);
6040 }
6041 else if (mips_pic == EMBEDDED_PIC)
6042 {
6043 /* If there is no base register, we want
cdf6fd85 6044 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
6045 If there is a base register, we want
6046 addu $tempreg,$breg,$gp
cdf6fd85 6047 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
6048 */
6049 assert (offset_expr.X_op == O_symbol);
6050 if (breg == 0)
6051 {
6052 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
c9914766 6053 treg, (int) BFD_RELOC_GPREL16, mips_gp_register);
252b5132
RH
6054 used_at = 0;
6055 }
6056 else
6057 {
6058 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6059 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 6060 "d,v,t", tempreg, breg, mips_gp_register);
252b5132 6061 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
cdf6fd85 6062 treg, (int) BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6063 }
6064 }
6065 else
6066 abort ();
6067
6068 if (! used_at)
6069 return;
6070
6071 break;
6072
6073 case M_LI:
6074 case M_LI_S:
6075 load_register (&icnt, treg, &imm_expr, 0);
6076 return;
6077
6078 case M_DLI:
6079 load_register (&icnt, treg, &imm_expr, 1);
6080 return;
6081
6082 case M_LI_SS:
6083 if (imm_expr.X_op == O_constant)
6084 {
6085 load_register (&icnt, AT, &imm_expr, 0);
6086 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6087 "mtc1", "t,G", AT, treg);
6088 break;
6089 }
6090 else
6091 {
6092 assert (offset_expr.X_op == O_symbol
6093 && strcmp (segment_name (S_GET_SEGMENT
6094 (offset_expr.X_add_symbol)),
6095 ".lit4") == 0
6096 && offset_expr.X_add_number == 0);
6097 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
c9914766 6098 treg, (int) BFD_RELOC_MIPS_LITERAL, mips_gp_register);
252b5132
RH
6099 return;
6100 }
6101
6102 case M_LI_D:
ca4e0257
RS
6103 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
6104 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
6105 order 32 bits of the value and the low order 32 bits are either
6106 zero or in OFFSET_EXPR. */
252b5132
RH
6107 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6108 {
ca4e0257 6109 if (HAVE_64BIT_GPRS)
252b5132
RH
6110 load_register (&icnt, treg, &imm_expr, 1);
6111 else
6112 {
6113 int hreg, lreg;
6114
6115 if (target_big_endian)
6116 {
6117 hreg = treg;
6118 lreg = treg + 1;
6119 }
6120 else
6121 {
6122 hreg = treg + 1;
6123 lreg = treg;
6124 }
6125
6126 if (hreg <= 31)
6127 load_register (&icnt, hreg, &imm_expr, 0);
6128 if (lreg <= 31)
6129 {
6130 if (offset_expr.X_op == O_absent)
ea1fb5dc 6131 move_register (&icnt, lreg, 0);
252b5132
RH
6132 else
6133 {
6134 assert (offset_expr.X_op == O_constant);
6135 load_register (&icnt, lreg, &offset_expr, 0);
6136 }
6137 }
6138 }
6139 return;
6140 }
6141
6142 /* We know that sym is in the .rdata section. First we get the
6143 upper 16 bits of the address. */
6144 if (mips_pic == NO_PIC)
6145 {
956cd1d6 6146 macro_build_lui (NULL, &icnt, &offset_expr, AT);
252b5132
RH
6147 }
6148 else if (mips_pic == SVR4_PIC)
6149 {
6150 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 6151 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
c9914766
TS
6152 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6153 mips_gp_register);
252b5132
RH
6154 }
6155 else if (mips_pic == EMBEDDED_PIC)
6156 {
6157 /* For embedded PIC we pick up the entire address off $gp in
6158 a single instruction. */
6159 macro_build ((char *) NULL, &icnt, &offset_expr,
c9914766
TS
6160 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j", AT,
6161 mips_gp_register, (int) BFD_RELOC_GPREL16);
252b5132
RH
6162 offset_expr.X_op = O_constant;
6163 offset_expr.X_add_number = 0;
6164 }
6165 else
6166 abort ();
bdaaa2e1 6167
252b5132 6168 /* Now we load the register(s). */
ca4e0257 6169 if (HAVE_64BIT_GPRS)
252b5132
RH
6170 macro_build ((char *) NULL, &icnt, &offset_expr, "ld", "t,o(b)",
6171 treg, (int) BFD_RELOC_LO16, AT);
6172 else
6173 {
6174 macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
6175 treg, (int) BFD_RELOC_LO16, AT);
f9419b05 6176 if (treg != RA)
252b5132
RH
6177 {
6178 /* FIXME: How in the world do we deal with the possible
6179 overflow here? */
6180 offset_expr.X_add_number += 4;
6181 macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
6182 treg + 1, (int) BFD_RELOC_LO16, AT);
6183 }
6184 }
6185
6186 /* To avoid confusion in tc_gen_reloc, we must ensure that this
6187 does not become a variant frag. */
6188 frag_wane (frag_now);
6189 frag_new (0);
6190
6191 break;
6192
6193 case M_LI_DD:
ca4e0257
RS
6194 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
6195 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
6196 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
6197 the value and the low order 32 bits are either zero or in
6198 OFFSET_EXPR. */
252b5132
RH
6199 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6200 {
ca4e0257
RS
6201 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_FPRS);
6202 if (HAVE_64BIT_FPRS)
6203 {
6204 assert (HAVE_64BIT_GPRS);
6205 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6206 "dmtc1", "t,S", AT, treg);
6207 }
252b5132
RH
6208 else
6209 {
6210 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6211 "mtc1", "t,G", AT, treg + 1);
6212 if (offset_expr.X_op == O_absent)
6213 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6214 "mtc1", "t,G", 0, treg);
6215 else
6216 {
6217 assert (offset_expr.X_op == O_constant);
6218 load_register (&icnt, AT, &offset_expr, 0);
6219 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6220 "mtc1", "t,G", AT, treg);
6221 }
6222 }
6223 break;
6224 }
6225
6226 assert (offset_expr.X_op == O_symbol
6227 && offset_expr.X_add_number == 0);
6228 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
6229 if (strcmp (s, ".lit8") == 0)
6230 {
e7af610e 6231 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
6232 {
6233 macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
c9914766
TS
6234 "T,o(b)", treg, (int) BFD_RELOC_MIPS_LITERAL,
6235 mips_gp_register);
252b5132
RH
6236 return;
6237 }
c9914766 6238 breg = mips_gp_register;
252b5132
RH
6239 r = BFD_RELOC_MIPS_LITERAL;
6240 goto dob;
6241 }
6242 else
6243 {
6244 assert (strcmp (s, RDATA_SECTION_NAME) == 0);
6245 if (mips_pic == SVR4_PIC)
6246 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 6247 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
c9914766
TS
6248 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6249 mips_gp_register);
252b5132
RH
6250 else
6251 {
6252 /* FIXME: This won't work for a 64 bit address. */
956cd1d6 6253 macro_build_lui (NULL, &icnt, &offset_expr, AT);
252b5132 6254 }
bdaaa2e1 6255
e7af610e 6256 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
6257 {
6258 macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
6259 "T,o(b)", treg, (int) BFD_RELOC_LO16, AT);
6260
6261 /* To avoid confusion in tc_gen_reloc, we must ensure
6262 that this does not become a variant frag. */
6263 frag_wane (frag_now);
6264 frag_new (0);
6265
6266 break;
6267 }
6268 breg = AT;
6269 r = BFD_RELOC_LO16;
6270 goto dob;
6271 }
6272
6273 case M_L_DOB:
ec68c924 6274 if (mips_arch == CPU_R4650)
252b5132
RH
6275 {
6276 as_bad (_("opcode not supported on this processor"));
6277 return;
6278 }
6279 /* Even on a big endian machine $fn comes before $fn+1. We have
6280 to adjust when loading from memory. */
6281 r = BFD_RELOC_LO16;
6282 dob:
e7af610e 6283 assert (mips_opts.isa == ISA_MIPS1);
252b5132
RH
6284 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6285 target_big_endian ? treg + 1 : treg,
6286 (int) r, breg);
6287 /* FIXME: A possible overflow which I don't know how to deal
6288 with. */
6289 offset_expr.X_add_number += 4;
6290 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6291 target_big_endian ? treg : treg + 1,
6292 (int) r, breg);
6293
6294 /* To avoid confusion in tc_gen_reloc, we must ensure that this
6295 does not become a variant frag. */
6296 frag_wane (frag_now);
6297 frag_new (0);
6298
6299 if (breg != AT)
6300 return;
6301 break;
6302
6303 case M_L_DAB:
6304 /*
6305 * The MIPS assembler seems to check for X_add_number not
6306 * being double aligned and generating:
6307 * lui at,%hi(foo+1)
6308 * addu at,at,v1
6309 * addiu at,at,%lo(foo+1)
6310 * lwc1 f2,0(at)
6311 * lwc1 f3,4(at)
6312 * But, the resulting address is the same after relocation so why
6313 * generate the extra instruction?
6314 */
ec68c924 6315 if (mips_arch == CPU_R4650)
252b5132
RH
6316 {
6317 as_bad (_("opcode not supported on this processor"));
6318 return;
6319 }
bdaaa2e1 6320 /* Itbl support may require additional care here. */
252b5132 6321 coproc = 1;
e7af610e 6322 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
6323 {
6324 s = "ldc1";
6325 goto ld;
6326 }
6327
6328 s = "lwc1";
6329 fmt = "T,o(b)";
6330 goto ldd_std;
6331
6332 case M_S_DAB:
ec68c924 6333 if (mips_arch == CPU_R4650)
252b5132
RH
6334 {
6335 as_bad (_("opcode not supported on this processor"));
6336 return;
6337 }
6338
e7af610e 6339 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
6340 {
6341 s = "sdc1";
6342 goto st;
6343 }
6344
6345 s = "swc1";
6346 fmt = "T,o(b)";
bdaaa2e1 6347 /* Itbl support may require additional care here. */
252b5132
RH
6348 coproc = 1;
6349 goto ldd_std;
6350
6351 case M_LD_AB:
ca4e0257 6352 if (HAVE_64BIT_GPRS)
252b5132
RH
6353 {
6354 s = "ld";
6355 goto ld;
6356 }
6357
6358 s = "lw";
6359 fmt = "t,o(b)";
6360 goto ldd_std;
6361
6362 case M_SD_AB:
ca4e0257 6363 if (HAVE_64BIT_GPRS)
252b5132
RH
6364 {
6365 s = "sd";
6366 goto st;
6367 }
6368
6369 s = "sw";
6370 fmt = "t,o(b)";
6371
6372 ldd_std:
afdbd6d0
CD
6373 /* We do _not_ bother to allow embedded PIC (symbol-local_symbol)
6374 loads for the case of doing a pair of loads to simulate an 'ld'.
6375 This is not currently done by the compiler, and assembly coders
6376 writing embedded-pic code can cope. */
6377
252b5132
RH
6378 if (offset_expr.X_op != O_symbol
6379 && offset_expr.X_op != O_constant)
6380 {
6381 as_bad (_("expression too complex"));
6382 offset_expr.X_op = O_constant;
6383 }
6384
6385 /* Even on a big endian machine $fn comes before $fn+1. We have
6386 to adjust when loading from memory. We set coproc if we must
6387 load $fn+1 first. */
bdaaa2e1 6388 /* Itbl support may require additional care here. */
252b5132
RH
6389 if (! target_big_endian)
6390 coproc = 0;
6391
6392 if (mips_pic == NO_PIC
6393 || offset_expr.X_op == O_constant)
6394 {
f9419b05
TS
6395 char *p;
6396
252b5132 6397 /* If this is a reference to a GP relative symbol, we want
cdf6fd85
TS
6398 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6399 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
6400 If we have a base register, we use this
6401 addu $at,$breg,$gp
cdf6fd85
TS
6402 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6403 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
6404 If this is not a GP relative symbol, we want
6405 lui $at,<sym> (BFD_RELOC_HI16_S)
6406 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6407 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6408 If there is a base register, we add it to $at after the
6409 lui instruction. If there is a constant, we always use
6410 the last case. */
e7d556df 6411 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
252b5132
RH
6412 || nopic_need_relax (offset_expr.X_add_symbol, 1))
6413 {
6414 p = NULL;
6415 used_at = 1;
6416 }
6417 else
6418 {
6419 int off;
6420
6421 if (breg == 0)
6422 {
6423 frag_grow (28);
c9914766 6424 tempreg = mips_gp_register;
252b5132
RH
6425 off = 0;
6426 used_at = 0;
6427 }
6428 else
6429 {
6430 frag_grow (36);
6431 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6432 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 6433 "d,v,t", AT, breg, mips_gp_register);
252b5132
RH
6434 tempreg = AT;
6435 off = 4;
6436 used_at = 1;
6437 }
6438
beae10d5 6439 /* Itbl support may require additional care here. */
252b5132
RH
6440 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6441 coproc ? treg + 1 : treg,
cdf6fd85 6442 (int) BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6443 offset_expr.X_add_number += 4;
6444
6445 /* Set mips_optimize to 2 to avoid inserting an
6446 undesired nop. */
6447 hold_mips_optimize = mips_optimize;
6448 mips_optimize = 2;
beae10d5 6449 /* Itbl support may require additional care here. */
252b5132
RH
6450 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6451 coproc ? treg : treg + 1,
cdf6fd85 6452 (int) BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6453 mips_optimize = hold_mips_optimize;
6454
6455 p = frag_var (rs_machine_dependent, 12 + off, 0,
6456 RELAX_ENCODE (8 + off, 12 + off, 0, 4 + off, 1,
6457 used_at && mips_opts.noat),
956cd1d6 6458 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
6459
6460 /* We just generated two relocs. When tc_gen_reloc
6461 handles this case, it will skip the first reloc and
6462 handle the second. The second reloc already has an
6463 extra addend of 4, which we added above. We must
6464 subtract it out, and then subtract another 4 to make
6465 the first reloc come out right. The second reloc
6466 will come out right because we are going to add 4 to
6467 offset_expr when we build its instruction below.
6468
6469 If we have a symbol, then we don't want to include
6470 the offset, because it will wind up being included
6471 when we generate the reloc. */
6472
6473 if (offset_expr.X_op == O_constant)
6474 offset_expr.X_add_number -= 8;
6475 else
6476 {
6477 offset_expr.X_add_number = -4;
6478 offset_expr.X_op = O_constant;
6479 }
6480 }
6481 macro_build_lui (p, &icnt, &offset_expr, AT);
6482 if (p != NULL)
6483 p += 4;
6484 if (breg != 0)
6485 {
6486 macro_build (p, &icnt, (expressionS *) NULL,
ca4e0257 6487 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
6488 "d,v,t", AT, breg, AT);
6489 if (p != NULL)
6490 p += 4;
6491 }
beae10d5 6492 /* Itbl support may require additional care here. */
252b5132
RH
6493 macro_build (p, &icnt, &offset_expr, s, fmt,
6494 coproc ? treg + 1 : treg,
6495 (int) BFD_RELOC_LO16, AT);
6496 if (p != NULL)
6497 p += 4;
6498 /* FIXME: How do we handle overflow here? */
6499 offset_expr.X_add_number += 4;
beae10d5 6500 /* Itbl support may require additional care here. */
252b5132
RH
6501 macro_build (p, &icnt, &offset_expr, s, fmt,
6502 coproc ? treg : treg + 1,
6503 (int) BFD_RELOC_LO16, AT);
bdaaa2e1 6504 }
252b5132
RH
6505 else if (mips_pic == SVR4_PIC && ! mips_big_got)
6506 {
6507 int off;
6508
6509 /* If this is a reference to an external symbol, we want
6510 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6511 nop
6512 <op> $treg,0($at)
6513 <op> $treg+1,4($at)
6514 Otherwise we want
6515 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6516 nop
6517 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6518 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6519 If there is a base register we add it to $at before the
6520 lwc1 instructions. If there is a constant we include it
6521 in the lwc1 instructions. */
6522 used_at = 1;
6523 expr1.X_add_number = offset_expr.X_add_number;
6524 offset_expr.X_add_number = 0;
6525 if (expr1.X_add_number < -0x8000
6526 || expr1.X_add_number >= 0x8000 - 4)
6527 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6528 if (breg == 0)
6529 off = 0;
6530 else
6531 off = 4;
6532 frag_grow (24 + off);
6533 macro_build ((char *) NULL, &icnt, &offset_expr,
c9914766
TS
6534 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", AT,
6535 (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132
RH
6536 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6537 if (breg != 0)
6538 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6539 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132 6540 "d,v,t", AT, breg, AT);
beae10d5 6541 /* Itbl support may require additional care here. */
252b5132
RH
6542 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6543 coproc ? treg + 1 : treg,
6544 (int) BFD_RELOC_LO16, AT);
6545 expr1.X_add_number += 4;
6546
6547 /* Set mips_optimize to 2 to avoid inserting an undesired
6548 nop. */
6549 hold_mips_optimize = mips_optimize;
6550 mips_optimize = 2;
beae10d5 6551 /* Itbl support may require additional care here. */
252b5132
RH
6552 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6553 coproc ? treg : treg + 1,
6554 (int) BFD_RELOC_LO16, AT);
6555 mips_optimize = hold_mips_optimize;
6556
6557 (void) frag_var (rs_machine_dependent, 0, 0,
6558 RELAX_ENCODE (0, 0, -16 - off, -8, 1, 0),
c4e7957c 6559 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
6560 }
6561 else if (mips_pic == SVR4_PIC)
6562 {
6563 int gpdel, off;
f9419b05 6564 char *p;
252b5132
RH
6565
6566 /* If this is a reference to an external symbol, we want
6567 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6568 addu $at,$at,$gp
6569 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
6570 nop
6571 <op> $treg,0($at)
6572 <op> $treg+1,4($at)
6573 Otherwise we want
6574 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6575 nop
6576 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6577 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6578 If there is a base register we add it to $at before the
6579 lwc1 instructions. If there is a constant we include it
6580 in the lwc1 instructions. */
6581 used_at = 1;
6582 expr1.X_add_number = offset_expr.X_add_number;
6583 offset_expr.X_add_number = 0;
6584 if (expr1.X_add_number < -0x8000
6585 || expr1.X_add_number >= 0x8000 - 4)
6586 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
c9914766 6587 if (reg_needs_delay (mips_gp_register))
252b5132
RH
6588 gpdel = 4;
6589 else
6590 gpdel = 0;
6591 if (breg == 0)
6592 off = 0;
6593 else
6594 off = 4;
6595 frag_grow (56);
6596 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
6597 AT, (int) BFD_RELOC_MIPS_GOT_HI16);
6598 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6599 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 6600 "d,v,t", AT, AT, mips_gp_register);
252b5132 6601 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 6602 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
6603 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT_LO16, AT);
6604 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6605 if (breg != 0)
6606 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6607 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132 6608 "d,v,t", AT, breg, AT);
beae10d5 6609 /* Itbl support may require additional care here. */
252b5132
RH
6610 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6611 coproc ? treg + 1 : treg,
6612 (int) BFD_RELOC_LO16, AT);
6613 expr1.X_add_number += 4;
6614
6615 /* Set mips_optimize to 2 to avoid inserting an undesired
6616 nop. */
6617 hold_mips_optimize = mips_optimize;
6618 mips_optimize = 2;
beae10d5 6619 /* Itbl support may require additional care here. */
252b5132
RH
6620 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6621 coproc ? treg : treg + 1,
6622 (int) BFD_RELOC_LO16, AT);
6623 mips_optimize = hold_mips_optimize;
6624 expr1.X_add_number -= 4;
6625
6626 p = frag_var (rs_machine_dependent, 16 + gpdel + off, 0,
6627 RELAX_ENCODE (24 + off, 16 + gpdel + off, gpdel,
6628 8 + gpdel + off, 1, 0),
c4e7957c 6629 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
6630 if (gpdel > 0)
6631 {
6632 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6633 p += 4;
6634 }
6635 macro_build (p, &icnt, &offset_expr,
ca4e0257 6636 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
c9914766
TS
6637 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6638 mips_gp_register);
252b5132
RH
6639 p += 4;
6640 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6641 p += 4;
6642 if (breg != 0)
6643 {
6644 macro_build (p, &icnt, (expressionS *) NULL,
ca4e0257 6645 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
6646 "d,v,t", AT, breg, AT);
6647 p += 4;
6648 }
beae10d5 6649 /* Itbl support may require additional care here. */
252b5132
RH
6650 macro_build (p, &icnt, &expr1, s, fmt,
6651 coproc ? treg + 1 : treg,
6652 (int) BFD_RELOC_LO16, AT);
6653 p += 4;
6654 expr1.X_add_number += 4;
6655
6656 /* Set mips_optimize to 2 to avoid inserting an undesired
6657 nop. */
6658 hold_mips_optimize = mips_optimize;
6659 mips_optimize = 2;
beae10d5 6660 /* Itbl support may require additional care here. */
252b5132
RH
6661 macro_build (p, &icnt, &expr1, s, fmt,
6662 coproc ? treg : treg + 1,
6663 (int) BFD_RELOC_LO16, AT);
6664 mips_optimize = hold_mips_optimize;
6665 }
6666 else if (mips_pic == EMBEDDED_PIC)
6667 {
6668 /* If there is no base register, we use
cdf6fd85
TS
6669 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6670 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
6671 If we have a base register, we use
6672 addu $at,$breg,$gp
cdf6fd85
TS
6673 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6674 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
6675 */
6676 if (breg == 0)
6677 {
c9914766 6678 tempreg = mips_gp_register;
252b5132
RH
6679 used_at = 0;
6680 }
6681 else
6682 {
6683 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6684 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 6685 "d,v,t", AT, breg, mips_gp_register);
252b5132
RH
6686 tempreg = AT;
6687 used_at = 1;
6688 }
6689
beae10d5 6690 /* Itbl support may require additional care here. */
252b5132
RH
6691 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6692 coproc ? treg + 1 : treg,
cdf6fd85 6693 (int) BFD_RELOC_GPREL16, tempreg);
252b5132 6694 offset_expr.X_add_number += 4;
beae10d5 6695 /* Itbl support may require additional care here. */
252b5132
RH
6696 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6697 coproc ? treg : treg + 1,
cdf6fd85 6698 (int) BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6699 }
6700 else
6701 abort ();
6702
6703 if (! used_at)
6704 return;
6705
6706 break;
6707
6708 case M_LD_OB:
6709 s = "lw";
6710 goto sd_ob;
6711 case M_SD_OB:
6712 s = "sw";
6713 sd_ob:
ca4e0257 6714 assert (HAVE_32BIT_ADDRESSES);
252b5132
RH
6715 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
6716 (int) BFD_RELOC_LO16, breg);
6717 offset_expr.X_add_number += 4;
6718 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg + 1,
6719 (int) BFD_RELOC_LO16, breg);
6720 return;
6721
6722 /* New code added to support COPZ instructions.
6723 This code builds table entries out of the macros in mip_opcodes.
6724 R4000 uses interlocks to handle coproc delays.
6725 Other chips (like the R3000) require nops to be inserted for delays.
6726
f72c8c98 6727 FIXME: Currently, we require that the user handle delays.
252b5132
RH
6728 In order to fill delay slots for non-interlocked chips,
6729 we must have a way to specify delays based on the coprocessor.
6730 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
6731 What are the side-effects of the cop instruction?
6732 What cache support might we have and what are its effects?
6733 Both coprocessor & memory require delays. how long???
bdaaa2e1 6734 What registers are read/set/modified?
252b5132
RH
6735
6736 If an itbl is provided to interpret cop instructions,
bdaaa2e1 6737 this knowledge can be encoded in the itbl spec. */
252b5132
RH
6738
6739 case M_COP0:
6740 s = "c0";
6741 goto copz;
6742 case M_COP1:
6743 s = "c1";
6744 goto copz;
6745 case M_COP2:
6746 s = "c2";
6747 goto copz;
6748 case M_COP3:
6749 s = "c3";
6750 copz:
6751 /* For now we just do C (same as Cz). The parameter will be
6752 stored in insn_opcode by mips_ip. */
6753 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "C",
6754 ip->insn_opcode);
6755 return;
6756
ea1fb5dc
RS
6757 case M_MOVE:
6758 move_register (&icnt, dreg, sreg);
6759 return;
6760
252b5132
RH
6761#ifdef LOSING_COMPILER
6762 default:
6763 /* Try and see if this is a new itbl instruction.
6764 This code builds table entries out of the macros in mip_opcodes.
6765 FIXME: For now we just assemble the expression and pass it's
6766 value along as a 32-bit immediate.
bdaaa2e1 6767 We may want to have the assembler assemble this value,
252b5132
RH
6768 so that we gain the assembler's knowledge of delay slots,
6769 symbols, etc.
6770 Would it be more efficient to use mask (id) here? */
bdaaa2e1 6771 if (itbl_have_entries
252b5132 6772 && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
beae10d5 6773 {
252b5132
RH
6774 s = ip->insn_mo->name;
6775 s2 = "cop3";
6776 coproc = ITBL_DECODE_PNUM (immed_expr);;
6777 macro_build ((char *) NULL, &icnt, &immed_expr, s, "C");
6778 return;
beae10d5 6779 }
252b5132
RH
6780 macro2 (ip);
6781 return;
6782 }
6783 if (mips_opts.noat)
6784 as_warn (_("Macro used $at after \".set noat\""));
6785}
bdaaa2e1 6786
252b5132
RH
6787static void
6788macro2 (ip)
6789 struct mips_cl_insn *ip;
6790{
6791 register int treg, sreg, dreg, breg;
6792 int tempreg;
6793 int mask;
6794 int icnt = 0;
6795 int used_at;
6796 expressionS expr1;
6797 const char *s;
6798 const char *s2;
6799 const char *fmt;
6800 int likely = 0;
6801 int dbl = 0;
6802 int coproc = 0;
6803 int lr = 0;
6804 int imm = 0;
6805 int off;
6806 offsetT maxnum;
6807 bfd_reloc_code_real_type r;
6808 char *p;
bdaaa2e1 6809
252b5132
RH
6810 treg = (ip->insn_opcode >> 16) & 0x1f;
6811 dreg = (ip->insn_opcode >> 11) & 0x1f;
6812 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
6813 mask = ip->insn_mo->mask;
bdaaa2e1 6814
252b5132
RH
6815 expr1.X_op = O_constant;
6816 expr1.X_op_symbol = NULL;
6817 expr1.X_add_symbol = NULL;
6818 expr1.X_add_number = 1;
bdaaa2e1 6819
252b5132
RH
6820 switch (mask)
6821 {
6822#endif /* LOSING_COMPILER */
6823
6824 case M_DMUL:
6825 dbl = 1;
6826 case M_MUL:
2396cfb9
TS
6827 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6828 dbl ? "dmultu" : "multu", "s,t", sreg, treg);
6829 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6830 dreg);
252b5132
RH
6831 return;
6832
6833 case M_DMUL_I:
6834 dbl = 1;
6835 case M_MUL_I:
6836 /* The MIPS assembler some times generates shifts and adds. I'm
6837 not trying to be that fancy. GCC should do this for us
6838 anyway. */
6839 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9 6840 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 6841 dbl ? "dmult" : "mult", "s,t", sreg, AT);
2396cfb9
TS
6842 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6843 dreg);
252b5132
RH
6844 break;
6845
6846 case M_DMULO_I:
6847 dbl = 1;
6848 case M_MULO_I:
6849 imm = 1;
6850 goto do_mulo;
6851
6852 case M_DMULO:
6853 dbl = 1;
6854 case M_MULO:
6855 do_mulo:
b34976b6 6856 mips_emit_delays (TRUE);
252b5132
RH
6857 ++mips_opts.noreorder;
6858 mips_any_noreorder = 1;
6859 if (imm)
6860 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9 6861 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 6862 dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
2396cfb9
TS
6863 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6864 dreg);
6865 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
f9419b05 6866 dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, RA);
2396cfb9
TS
6867 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6868 AT);
252b5132 6869 if (mips_trap)
9bd7d936
MR
6870 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne",
6871 "s,t,q", dreg, AT, 6);
252b5132
RH
6872 else
6873 {
6874 expr1.X_add_number = 8;
2396cfb9
TS
6875 macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", dreg,
6876 AT);
6877 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6878 0);
6879 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6880 "c", 6);
252b5132
RH
6881 }
6882 --mips_opts.noreorder;
2396cfb9 6883 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d", dreg);
252b5132
RH
6884 break;
6885
6886 case M_DMULOU_I:
6887 dbl = 1;
6888 case M_MULOU_I:
6889 imm = 1;
6890 goto do_mulou;
6891
6892 case M_DMULOU:
6893 dbl = 1;
6894 case M_MULOU:
6895 do_mulou:
b34976b6 6896 mips_emit_delays (TRUE);
252b5132
RH
6897 ++mips_opts.noreorder;
6898 mips_any_noreorder = 1;
6899 if (imm)
6900 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9 6901 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
252b5132
RH
6902 dbl ? "dmultu" : "multu",
6903 "s,t", sreg, imm ? AT : treg);
2396cfb9
TS
6904 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6905 AT);
6906 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6907 dreg);
252b5132 6908 if (mips_trap)
9bd7d936
MR
6909 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne",
6910 "s,t,q", AT, 0, 6);
252b5132
RH
6911 else
6912 {
6913 expr1.X_add_number = 8;
6914 macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", AT, 0);
2396cfb9
TS
6915 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6916 0);
6917 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6918 "c", 6);
252b5132
RH
6919 }
6920 --mips_opts.noreorder;
6921 break;
6922
771c7ce4 6923 case M_DROL:
af7ee8bf 6924 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_arch))
82dd0097
CD
6925 {
6926 if (dreg == sreg)
6927 {
6928 tempreg = AT;
6929 used_at = 1;
6930 }
6931 else
6932 {
6933 tempreg = dreg;
6934 used_at = 0;
6935 }
6936 macro_build ((char *) NULL, &icnt, NULL, "dnegu",
6937 "d,w", tempreg, treg);
6938 macro_build ((char *) NULL, &icnt, NULL, "drorv",
6939 "d,t,s", dreg, sreg, tempreg);
6940 if (used_at)
6941 break;
6942 return;
6943 }
771c7ce4
TS
6944 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
6945 "d,v,t", AT, 0, treg);
6946 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
6947 "d,t,s", AT, sreg, AT);
6948 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
6949 "d,t,s", dreg, sreg, treg);
6950 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6951 "d,v,t", dreg, dreg, AT);
6952 break;
6953
252b5132 6954 case M_ROL:
af7ee8bf 6955 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_arch))
82dd0097
CD
6956 {
6957 if (dreg == sreg)
6958 {
6959 tempreg = AT;
6960 used_at = 1;
6961 }
6962 else
6963 {
6964 tempreg = dreg;
6965 used_at = 0;
6966 }
6967 macro_build ((char *) NULL, &icnt, NULL, "negu",
6968 "d,w", tempreg, treg);
6969 macro_build ((char *) NULL, &icnt, NULL, "rorv",
6970 "d,t,s", dreg, sreg, tempreg);
6971 if (used_at)
6972 break;
6973 return;
6974 }
2396cfb9
TS
6975 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
6976 "d,v,t", AT, 0, treg);
6977 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
6978 "d,t,s", AT, sreg, AT);
6979 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
6980 "d,t,s", dreg, sreg, treg);
6981 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6982 "d,v,t", dreg, dreg, AT);
252b5132
RH
6983 break;
6984
771c7ce4
TS
6985 case M_DROL_I:
6986 {
6987 unsigned int rot;
82dd0097 6988 char *l, *r;
771c7ce4
TS
6989
6990 if (imm_expr.X_op != O_constant)
82dd0097 6991 as_bad (_("Improper rotate count"));
771c7ce4 6992 rot = imm_expr.X_add_number & 0x3f;
af7ee8bf 6993 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_arch))
60b63b72
RS
6994 {
6995 rot = (64 - rot) & 0x3f;
6996 if (rot >= 32)
6997 macro_build ((char *) NULL, &icnt, NULL, "dror32",
6998 "d,w,<", dreg, sreg, rot - 32);
6999 else
7000 macro_build ((char *) NULL, &icnt, NULL, "dror",
7001 "d,w,<", dreg, sreg, rot);
82dd0097 7002 return;
60b63b72 7003 }
483fc7cd 7004 if (rot == 0)
483fc7cd 7005 {
82dd0097
CD
7006 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrl",
7007 "d,w,<", dreg, sreg, 0);
7008 return;
483fc7cd 7009 }
82dd0097
CD
7010 l = (rot < 0x20) ? "dsll" : "dsll32";
7011 r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
7012 rot &= 0x1f;
7013 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
7014 "d,w,<", AT, sreg, rot);
7015 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
7016 "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7017 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7018 "d,v,t", dreg, dreg, AT);
771c7ce4
TS
7019 }
7020 break;
7021
252b5132 7022 case M_ROL_I:
771c7ce4
TS
7023 {
7024 unsigned int rot;
7025
7026 if (imm_expr.X_op != O_constant)
82dd0097 7027 as_bad (_("Improper rotate count"));
771c7ce4 7028 rot = imm_expr.X_add_number & 0x1f;
af7ee8bf 7029 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_arch))
60b63b72
RS
7030 {
7031 macro_build ((char *) NULL, &icnt, NULL, "ror",
7032 "d,w,<", dreg, sreg, (32 - rot) & 0x1f);
82dd0097 7033 return;
60b63b72 7034 }
483fc7cd 7035 if (rot == 0)
483fc7cd 7036 {
483fc7cd 7037 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
82dd0097
CD
7038 "d,w,<", dreg, sreg, 0);
7039 return;
483fc7cd 7040 }
82dd0097
CD
7041 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
7042 "d,w,<", AT, sreg, rot);
7043 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
7044 "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7045 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7046 "d,v,t", dreg, dreg, AT);
771c7ce4
TS
7047 }
7048 break;
7049
7050 case M_DROR:
af7ee8bf 7051 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_arch))
82dd0097
CD
7052 {
7053 macro_build ((char *) NULL, &icnt, NULL, "drorv",
7054 "d,t,s", dreg, sreg, treg);
7055 return;
7056 }
771c7ce4
TS
7057 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
7058 "d,v,t", AT, 0, treg);
7059 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
7060 "d,t,s", AT, sreg, AT);
7061 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
7062 "d,t,s", dreg, sreg, treg);
7063 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7064 "d,v,t", dreg, dreg, AT);
252b5132
RH
7065 break;
7066
7067 case M_ROR:
af7ee8bf 7068 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_arch))
82dd0097
CD
7069 {
7070 macro_build ((char *) NULL, &icnt, NULL, "rorv",
7071 "d,t,s", dreg, sreg, treg);
7072 return;
7073 }
2396cfb9
TS
7074 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
7075 "d,v,t", AT, 0, treg);
7076 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
7077 "d,t,s", AT, sreg, AT);
7078 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
7079 "d,t,s", dreg, sreg, treg);
7080 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7081 "d,v,t", dreg, dreg, AT);
252b5132
RH
7082 break;
7083
771c7ce4
TS
7084 case M_DROR_I:
7085 {
7086 unsigned int rot;
82dd0097 7087 char *l, *r;
771c7ce4
TS
7088
7089 if (imm_expr.X_op != O_constant)
82dd0097 7090 as_bad (_("Improper rotate count"));
771c7ce4 7091 rot = imm_expr.X_add_number & 0x3f;
af7ee8bf 7092 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_arch))
82dd0097
CD
7093 {
7094 if (rot >= 32)
7095 macro_build ((char *) NULL, &icnt, NULL, "dror32",
7096 "d,w,<", dreg, sreg, rot - 32);
7097 else
7098 macro_build ((char *) NULL, &icnt, NULL, "dror",
7099 "d,w,<", dreg, sreg, rot);
7100 return;
7101 }
483fc7cd 7102 if (rot == 0)
483fc7cd 7103 {
82dd0097
CD
7104 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrl",
7105 "d,w,<", dreg, sreg, 0);
7106 return;
483fc7cd 7107 }
82dd0097
CD
7108 r = (rot < 0x20) ? "dsrl" : "dsrl32";
7109 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
7110 rot &= 0x1f;
7111 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
7112 "d,w,<", AT, sreg, rot);
7113 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
7114 "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7115 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7116 "d,v,t", dreg, dreg, AT);
771c7ce4
TS
7117 }
7118 break;
7119
252b5132 7120 case M_ROR_I:
771c7ce4
TS
7121 {
7122 unsigned int rot;
7123
7124 if (imm_expr.X_op != O_constant)
82dd0097 7125 as_bad (_("Improper rotate count"));
771c7ce4 7126 rot = imm_expr.X_add_number & 0x1f;
af7ee8bf 7127 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_arch))
82dd0097
CD
7128 {
7129 macro_build ((char *) NULL, &icnt, NULL, "ror",
7130 "d,w,<", dreg, sreg, rot);
7131 return;
7132 }
483fc7cd 7133 if (rot == 0)
483fc7cd
RS
7134 {
7135 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
82dd0097
CD
7136 "d,w,<", dreg, sreg, 0);
7137 return;
483fc7cd 7138 }
82dd0097
CD
7139 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
7140 "d,w,<", AT, sreg, rot);
7141 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
7142 "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
7143 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
7144 "d,v,t", dreg, dreg, AT);
771c7ce4 7145 }
252b5132
RH
7146 break;
7147
7148 case M_S_DOB:
ec68c924 7149 if (mips_arch == CPU_R4650)
252b5132
RH
7150 {
7151 as_bad (_("opcode not supported on this processor"));
7152 return;
7153 }
e7af610e 7154 assert (mips_opts.isa == ISA_MIPS1);
252b5132
RH
7155 /* Even on a big endian machine $fn comes before $fn+1. We have
7156 to adjust when storing to memory. */
7157 macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
7158 target_big_endian ? treg + 1 : treg,
7159 (int) BFD_RELOC_LO16, breg);
7160 offset_expr.X_add_number += 4;
7161 macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
7162 target_big_endian ? treg : treg + 1,
7163 (int) BFD_RELOC_LO16, breg);
7164 return;
7165
7166 case M_SEQ:
7167 if (sreg == 0)
7168 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7169 treg, (int) BFD_RELOC_LO16);
7170 else if (treg == 0)
7171 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7172 sreg, (int) BFD_RELOC_LO16);
7173 else
7174 {
2396cfb9
TS
7175 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7176 "d,v,t", dreg, sreg, treg);
252b5132
RH
7177 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7178 dreg, (int) BFD_RELOC_LO16);
7179 }
7180 return;
7181
7182 case M_SEQ_I:
7183 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7184 {
7185 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
7186 sreg, (int) BFD_RELOC_LO16);
7187 return;
7188 }
7189 if (sreg == 0)
7190 {
7191 as_warn (_("Instruction %s: result is always false"),
7192 ip->insn_mo->name);
ea1fb5dc 7193 move_register (&icnt, dreg, 0);
252b5132
RH
7194 return;
7195 }
7196 if (imm_expr.X_op == O_constant
7197 && imm_expr.X_add_number >= 0
7198 && imm_expr.X_add_number < 0x10000)
7199 {
7200 macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg,
7201 sreg, (int) BFD_RELOC_LO16);
7202 used_at = 0;
7203 }
7204 else if (imm_expr.X_op == O_constant
7205 && imm_expr.X_add_number > -0x8000
7206 && imm_expr.X_add_number < 0)
7207 {
7208 imm_expr.X_add_number = -imm_expr.X_add_number;
7209 macro_build ((char *) NULL, &icnt, &imm_expr,
ca4e0257 7210 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
252b5132
RH
7211 "t,r,j", dreg, sreg,
7212 (int) BFD_RELOC_LO16);
7213 used_at = 0;
7214 }
7215 else
7216 {
4d34fb5f 7217 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
7218 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7219 "d,v,t", dreg, sreg, AT);
252b5132
RH
7220 used_at = 1;
7221 }
7222 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
7223 (int) BFD_RELOC_LO16);
7224 if (used_at)
7225 break;
7226 return;
7227
7228 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
7229 s = "slt";
7230 goto sge;
7231 case M_SGEU:
7232 s = "sltu";
7233 sge:
2396cfb9
TS
7234 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7235 dreg, sreg, treg);
252b5132
RH
7236 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7237 (int) BFD_RELOC_LO16);
7238 return;
7239
7240 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
7241 case M_SGEU_I:
7242 if (imm_expr.X_op == O_constant
7243 && imm_expr.X_add_number >= -0x8000
7244 && imm_expr.X_add_number < 0x8000)
7245 {
7246 macro_build ((char *) NULL, &icnt, &imm_expr,
7247 mask == M_SGE_I ? "slti" : "sltiu",
7248 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7249 used_at = 0;
7250 }
7251 else
7252 {
4d34fb5f 7253 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
7254 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7255 mask == M_SGE_I ? "slt" : "sltu", "d,v,t", dreg, sreg,
7256 AT);
252b5132
RH
7257 used_at = 1;
7258 }
7259 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7260 (int) BFD_RELOC_LO16);
7261 if (used_at)
7262 break;
7263 return;
7264
7265 case M_SGT: /* sreg > treg <==> treg < sreg */
7266 s = "slt";
7267 goto sgt;
7268 case M_SGTU:
7269 s = "sltu";
7270 sgt:
2396cfb9
TS
7271 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7272 dreg, treg, sreg);
252b5132
RH
7273 return;
7274
7275 case M_SGT_I: /* sreg > I <==> I < sreg */
7276 s = "slt";
7277 goto sgti;
7278 case M_SGTU_I:
7279 s = "sltu";
7280 sgti:
4d34fb5f 7281 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
7282 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7283 dreg, AT, sreg);
252b5132
RH
7284 break;
7285
2396cfb9 7286 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
252b5132
RH
7287 s = "slt";
7288 goto sle;
7289 case M_SLEU:
7290 s = "sltu";
7291 sle:
2396cfb9
TS
7292 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7293 dreg, treg, sreg);
252b5132
RH
7294 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7295 (int) BFD_RELOC_LO16);
7296 return;
7297
2396cfb9 7298 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
252b5132
RH
7299 s = "slt";
7300 goto slei;
7301 case M_SLEU_I:
7302 s = "sltu";
7303 slei:
4d34fb5f 7304 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
7305 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7306 dreg, AT, sreg);
252b5132
RH
7307 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7308 (int) BFD_RELOC_LO16);
7309 break;
7310
7311 case M_SLT_I:
7312 if (imm_expr.X_op == O_constant
7313 && imm_expr.X_add_number >= -0x8000
7314 && imm_expr.X_add_number < 0x8000)
7315 {
7316 macro_build ((char *) NULL, &icnt, &imm_expr, "slti", "t,r,j",
7317 dreg, sreg, (int) BFD_RELOC_LO16);
7318 return;
7319 }
4d34fb5f 7320 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
7321 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
7322 dreg, sreg, AT);
252b5132
RH
7323 break;
7324
7325 case M_SLTU_I:
7326 if (imm_expr.X_op == O_constant
7327 && imm_expr.X_add_number >= -0x8000
7328 && imm_expr.X_add_number < 0x8000)
7329 {
7330 macro_build ((char *) NULL, &icnt, &imm_expr, "sltiu", "t,r,j",
7331 dreg, sreg, (int) BFD_RELOC_LO16);
7332 return;
7333 }
4d34fb5f 7334 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
7335 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7336 "d,v,t", dreg, sreg, AT);
252b5132
RH
7337 break;
7338
7339 case M_SNE:
7340 if (sreg == 0)
2396cfb9
TS
7341 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7342 "d,v,t", dreg, 0, treg);
252b5132 7343 else if (treg == 0)
2396cfb9
TS
7344 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7345 "d,v,t", dreg, 0, sreg);
252b5132
RH
7346 else
7347 {
2396cfb9
TS
7348 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7349 "d,v,t", dreg, sreg, treg);
7350 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7351 "d,v,t", dreg, 0, dreg);
252b5132
RH
7352 }
7353 return;
7354
7355 case M_SNE_I:
7356 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7357 {
2396cfb9
TS
7358 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7359 "d,v,t", dreg, 0, sreg);
252b5132
RH
7360 return;
7361 }
7362 if (sreg == 0)
7363 {
7364 as_warn (_("Instruction %s: result is always true"),
7365 ip->insn_mo->name);
7366 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 7367 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
252b5132
RH
7368 "t,r,j", dreg, 0, (int) BFD_RELOC_LO16);
7369 return;
7370 }
7371 if (imm_expr.X_op == O_constant
7372 && imm_expr.X_add_number >= 0
7373 && imm_expr.X_add_number < 0x10000)
7374 {
7375 macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i",
7376 dreg, sreg, (int) BFD_RELOC_LO16);
7377 used_at = 0;
7378 }
7379 else if (imm_expr.X_op == O_constant
7380 && imm_expr.X_add_number > -0x8000
7381 && imm_expr.X_add_number < 0)
7382 {
7383 imm_expr.X_add_number = -imm_expr.X_add_number;
7384 macro_build ((char *) NULL, &icnt, &imm_expr,
ca4e0257 7385 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
252b5132
RH
7386 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7387 used_at = 0;
7388 }
7389 else
7390 {
4d34fb5f 7391 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
7392 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7393 "d,v,t", dreg, sreg, AT);
252b5132
RH
7394 used_at = 1;
7395 }
2396cfb9
TS
7396 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7397 "d,v,t", dreg, 0, dreg);
252b5132
RH
7398 if (used_at)
7399 break;
7400 return;
7401
7402 case M_DSUB_I:
7403 dbl = 1;
7404 case M_SUB_I:
7405 if (imm_expr.X_op == O_constant
7406 && imm_expr.X_add_number > -0x8000
7407 && imm_expr.X_add_number <= 0x8000)
7408 {
7409 imm_expr.X_add_number = -imm_expr.X_add_number;
7410 macro_build ((char *) NULL, &icnt, &imm_expr,
7411 dbl ? "daddi" : "addi",
7412 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7413 return;
7414 }
7415 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9 7416 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 7417 dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
252b5132
RH
7418 break;
7419
7420 case M_DSUBU_I:
7421 dbl = 1;
7422 case M_SUBU_I:
7423 if (imm_expr.X_op == O_constant
7424 && imm_expr.X_add_number > -0x8000
7425 && imm_expr.X_add_number <= 0x8000)
7426 {
7427 imm_expr.X_add_number = -imm_expr.X_add_number;
7428 macro_build ((char *) NULL, &icnt, &imm_expr,
7429 dbl ? "daddiu" : "addiu",
7430 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7431 return;
7432 }
7433 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9 7434 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 7435 dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
252b5132
RH
7436 break;
7437
7438 case M_TEQ_I:
7439 s = "teq";
7440 goto trap;
7441 case M_TGE_I:
7442 s = "tge";
7443 goto trap;
7444 case M_TGEU_I:
7445 s = "tgeu";
7446 goto trap;
7447 case M_TLT_I:
7448 s = "tlt";
7449 goto trap;
7450 case M_TLTU_I:
7451 s = "tltu";
7452 goto trap;
7453 case M_TNE_I:
7454 s = "tne";
7455 trap:
4d34fb5f 7456 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
7457 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "s,t", sreg,
7458 AT);
252b5132
RH
7459 break;
7460
252b5132 7461 case M_TRUNCWS:
43841e91 7462 case M_TRUNCWD:
e7af610e 7463 assert (mips_opts.isa == ISA_MIPS1);
252b5132
RH
7464 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
7465 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
7466
7467 /*
7468 * Is the double cfc1 instruction a bug in the mips assembler;
7469 * or is there a reason for it?
7470 */
b34976b6 7471 mips_emit_delays (TRUE);
252b5132
RH
7472 ++mips_opts.noreorder;
7473 mips_any_noreorder = 1;
2396cfb9 7474 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
f9419b05 7475 treg, RA);
2396cfb9 7476 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
f9419b05 7477 treg, RA);
2396cfb9 7478 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
252b5132
RH
7479 expr1.X_add_number = 3;
7480 macro_build ((char *) NULL, &icnt, &expr1, "ori", "t,r,i", AT, treg,
7481 (int) BFD_RELOC_LO16);
7482 expr1.X_add_number = 2;
7483 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", AT, AT,
7484 (int) BFD_RELOC_LO16);
2396cfb9 7485 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
f9419b05 7486 AT, RA);
2396cfb9
TS
7487 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7488 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
252b5132 7489 mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S", dreg, sreg);
2396cfb9 7490 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
f9419b05 7491 treg, RA);
2396cfb9 7492 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
252b5132
RH
7493 --mips_opts.noreorder;
7494 break;
7495
7496 case M_ULH:
7497 s = "lb";
7498 goto ulh;
7499 case M_ULHU:
7500 s = "lbu";
7501 ulh:
7502 if (offset_expr.X_add_number >= 0x7fff)
7503 as_bad (_("operand overflow"));
7504 /* avoid load delay */
7505 if (! target_big_endian)
f9419b05 7506 ++offset_expr.X_add_number;
252b5132
RH
7507 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7508 (int) BFD_RELOC_LO16, breg);
7509 if (! target_big_endian)
f9419b05 7510 --offset_expr.X_add_number;
252b5132 7511 else
f9419b05 7512 ++offset_expr.X_add_number;
252b5132
RH
7513 macro_build ((char *) NULL, &icnt, &offset_expr, "lbu", "t,o(b)", AT,
7514 (int) BFD_RELOC_LO16, breg);
2396cfb9
TS
7515 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7516 treg, treg, 8);
7517 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7518 treg, treg, AT);
252b5132
RH
7519 break;
7520
7521 case M_ULD:
7522 s = "ldl";
7523 s2 = "ldr";
7524 off = 7;
7525 goto ulw;
7526 case M_ULW:
7527 s = "lwl";
7528 s2 = "lwr";
7529 off = 3;
7530 ulw:
7531 if (offset_expr.X_add_number >= 0x8000 - off)
7532 as_bad (_("operand overflow"));
7533 if (! target_big_endian)
7534 offset_expr.X_add_number += off;
7535 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7536 (int) BFD_RELOC_LO16, breg);
7537 if (! target_big_endian)
7538 offset_expr.X_add_number -= off;
7539 else
7540 offset_expr.X_add_number += off;
7541 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7542 (int) BFD_RELOC_LO16, breg);
7543 return;
7544
7545 case M_ULD_A:
7546 s = "ldl";
7547 s2 = "ldr";
7548 off = 7;
7549 goto ulwa;
7550 case M_ULW_A:
7551 s = "lwl";
7552 s2 = "lwr";
7553 off = 3;
7554 ulwa:
d6bc6245 7555 used_at = 1;
c9914766 7556 load_address (&icnt, AT, &offset_expr, &used_at);
252b5132
RH
7557 if (breg != 0)
7558 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 7559 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
7560 "d,v,t", AT, AT, breg);
7561 if (! target_big_endian)
7562 expr1.X_add_number = off;
7563 else
7564 expr1.X_add_number = 0;
7565 macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7566 (int) BFD_RELOC_LO16, AT);
7567 if (! target_big_endian)
7568 expr1.X_add_number = 0;
7569 else
7570 expr1.X_add_number = off;
7571 macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7572 (int) BFD_RELOC_LO16, AT);
7573 break;
7574
7575 case M_ULH_A:
7576 case M_ULHU_A:
d6bc6245 7577 used_at = 1;
c9914766 7578 load_address (&icnt, AT, &offset_expr, &used_at);
252b5132
RH
7579 if (breg != 0)
7580 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 7581 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
7582 "d,v,t", AT, AT, breg);
7583 if (target_big_endian)
7584 expr1.X_add_number = 0;
7585 macro_build ((char *) NULL, &icnt, &expr1,
7586 mask == M_ULH_A ? "lb" : "lbu", "t,o(b)", treg,
7587 (int) BFD_RELOC_LO16, AT);
7588 if (target_big_endian)
7589 expr1.X_add_number = 1;
7590 else
7591 expr1.X_add_number = 0;
7592 macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7593 (int) BFD_RELOC_LO16, AT);
2396cfb9
TS
7594 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7595 treg, treg, 8);
7596 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7597 treg, treg, AT);
252b5132
RH
7598 break;
7599
7600 case M_USH:
7601 if (offset_expr.X_add_number >= 0x7fff)
7602 as_bad (_("operand overflow"));
7603 if (target_big_endian)
f9419b05 7604 ++offset_expr.X_add_number;
252b5132
RH
7605 macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", treg,
7606 (int) BFD_RELOC_LO16, breg);
2396cfb9
TS
7607 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7608 AT, treg, 8);
252b5132 7609 if (target_big_endian)
f9419b05 7610 --offset_expr.X_add_number;
252b5132 7611 else
f9419b05 7612 ++offset_expr.X_add_number;
252b5132
RH
7613 macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", AT,
7614 (int) BFD_RELOC_LO16, breg);
7615 break;
7616
7617 case M_USD:
7618 s = "sdl";
7619 s2 = "sdr";
7620 off = 7;
7621 goto usw;
7622 case M_USW:
7623 s = "swl";
7624 s2 = "swr";
7625 off = 3;
7626 usw:
7627 if (offset_expr.X_add_number >= 0x8000 - off)
7628 as_bad (_("operand overflow"));
7629 if (! target_big_endian)
7630 offset_expr.X_add_number += off;
7631 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7632 (int) BFD_RELOC_LO16, breg);
7633 if (! target_big_endian)
7634 offset_expr.X_add_number -= off;
7635 else
7636 offset_expr.X_add_number += off;
7637 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7638 (int) BFD_RELOC_LO16, breg);
7639 return;
7640
7641 case M_USD_A:
7642 s = "sdl";
7643 s2 = "sdr";
7644 off = 7;
7645 goto uswa;
7646 case M_USW_A:
7647 s = "swl";
7648 s2 = "swr";
7649 off = 3;
7650 uswa:
d6bc6245 7651 used_at = 1;
c9914766 7652 load_address (&icnt, AT, &offset_expr, &used_at);
252b5132
RH
7653 if (breg != 0)
7654 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 7655 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
7656 "d,v,t", AT, AT, breg);
7657 if (! target_big_endian)
7658 expr1.X_add_number = off;
7659 else
7660 expr1.X_add_number = 0;
7661 macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7662 (int) BFD_RELOC_LO16, AT);
7663 if (! target_big_endian)
7664 expr1.X_add_number = 0;
7665 else
7666 expr1.X_add_number = off;
7667 macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7668 (int) BFD_RELOC_LO16, AT);
7669 break;
7670
7671 case M_USH_A:
d6bc6245 7672 used_at = 1;
c9914766 7673 load_address (&icnt, AT, &offset_expr, &used_at);
252b5132
RH
7674 if (breg != 0)
7675 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 7676 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
7677 "d,v,t", AT, AT, breg);
7678 if (! target_big_endian)
7679 expr1.X_add_number = 0;
7680 macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7681 (int) BFD_RELOC_LO16, AT);
2396cfb9
TS
7682 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7683 treg, treg, 8);
252b5132
RH
7684 if (! target_big_endian)
7685 expr1.X_add_number = 1;
7686 else
7687 expr1.X_add_number = 0;
7688 macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7689 (int) BFD_RELOC_LO16, AT);
7690 if (! target_big_endian)
7691 expr1.X_add_number = 0;
7692 else
7693 expr1.X_add_number = 1;
7694 macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7695 (int) BFD_RELOC_LO16, AT);
2396cfb9
TS
7696 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7697 treg, treg, 8);
7698 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7699 treg, treg, AT);
252b5132
RH
7700 break;
7701
7702 default:
7703 /* FIXME: Check if this is one of the itbl macros, since they
bdaaa2e1 7704 are added dynamically. */
252b5132
RH
7705 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7706 break;
7707 }
7708 if (mips_opts.noat)
7709 as_warn (_("Macro used $at after \".set noat\""));
7710}
7711
7712/* Implement macros in mips16 mode. */
7713
7714static void
7715mips16_macro (ip)
7716 struct mips_cl_insn *ip;
7717{
7718 int mask;
7719 int xreg, yreg, zreg, tmp;
7720 int icnt;
7721 expressionS expr1;
7722 int dbl;
7723 const char *s, *s2, *s3;
7724
7725 mask = ip->insn_mo->mask;
7726
7727 xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
7728 yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
7729 zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
7730
7731 icnt = 0;
7732
7733 expr1.X_op = O_constant;
7734 expr1.X_op_symbol = NULL;
7735 expr1.X_add_symbol = NULL;
7736 expr1.X_add_number = 1;
7737
7738 dbl = 0;
7739
7740 switch (mask)
7741 {
7742 default:
7743 internalError ();
7744
7745 case M_DDIV_3:
7746 dbl = 1;
7747 case M_DIV_3:
7748 s = "mflo";
7749 goto do_div3;
7750 case M_DREM_3:
7751 dbl = 1;
7752 case M_REM_3:
7753 s = "mfhi";
7754 do_div3:
b34976b6 7755 mips_emit_delays (TRUE);
252b5132
RH
7756 ++mips_opts.noreorder;
7757 mips_any_noreorder = 1;
2396cfb9 7758 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
252b5132
RH
7759 dbl ? "ddiv" : "div",
7760 "0,x,y", xreg, yreg);
7761 expr1.X_add_number = 2;
7762 macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
2396cfb9
TS
7763 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break", "6",
7764 7);
bdaaa2e1 7765
252b5132
RH
7766 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7767 since that causes an overflow. We should do that as well,
7768 but I don't see how to do the comparisons without a temporary
7769 register. */
7770 --mips_opts.noreorder;
2396cfb9 7771 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x", zreg);
252b5132
RH
7772 break;
7773
7774 case M_DIVU_3:
7775 s = "divu";
7776 s2 = "mflo";
7777 goto do_divu3;
7778 case M_REMU_3:
7779 s = "divu";
7780 s2 = "mfhi";
7781 goto do_divu3;
7782 case M_DDIVU_3:
7783 s = "ddivu";
7784 s2 = "mflo";
7785 goto do_divu3;
7786 case M_DREMU_3:
7787 s = "ddivu";
7788 s2 = "mfhi";
7789 do_divu3:
b34976b6 7790 mips_emit_delays (TRUE);
252b5132
RH
7791 ++mips_opts.noreorder;
7792 mips_any_noreorder = 1;
2396cfb9
TS
7793 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "0,x,y",
7794 xreg, yreg);
252b5132
RH
7795 expr1.X_add_number = 2;
7796 macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
98d3f06f
KH
7797 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
7798 "6", 7);
252b5132 7799 --mips_opts.noreorder;
2396cfb9 7800 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "x", zreg);
252b5132
RH
7801 break;
7802
7803 case M_DMUL:
7804 dbl = 1;
7805 case M_MUL:
2396cfb9 7806 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 7807 dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
2396cfb9
TS
7808 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "x",
7809 zreg);
252b5132
RH
7810 return;
7811
7812 case M_DSUBU_I:
7813 dbl = 1;
7814 goto do_subu;
7815 case M_SUBU_I:
7816 do_subu:
7817 if (imm_expr.X_op != O_constant)
7818 as_bad (_("Unsupported large constant"));
7819 imm_expr.X_add_number = -imm_expr.X_add_number;
7820 macro_build ((char *) NULL, &icnt, &imm_expr,
9a41af64 7821 dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
252b5132
RH
7822 break;
7823
7824 case M_SUBU_I_2:
7825 if (imm_expr.X_op != O_constant)
7826 as_bad (_("Unsupported large constant"));
7827 imm_expr.X_add_number = -imm_expr.X_add_number;
7828 macro_build ((char *) NULL, &icnt, &imm_expr, "addiu",
7829 "x,k", xreg);
7830 break;
7831
7832 case M_DSUBU_I_2:
7833 if (imm_expr.X_op != O_constant)
7834 as_bad (_("Unsupported large constant"));
7835 imm_expr.X_add_number = -imm_expr.X_add_number;
7836 macro_build ((char *) NULL, &icnt, &imm_expr, "daddiu",
7837 "y,j", yreg);
7838 break;
7839
7840 case M_BEQ:
7841 s = "cmp";
7842 s2 = "bteqz";
7843 goto do_branch;
7844 case M_BNE:
7845 s = "cmp";
7846 s2 = "btnez";
7847 goto do_branch;
7848 case M_BLT:
7849 s = "slt";
7850 s2 = "btnez";
7851 goto do_branch;
7852 case M_BLTU:
7853 s = "sltu";
7854 s2 = "btnez";
7855 goto do_branch;
7856 case M_BLE:
7857 s = "slt";
7858 s2 = "bteqz";
7859 goto do_reverse_branch;
7860 case M_BLEU:
7861 s = "sltu";
7862 s2 = "bteqz";
7863 goto do_reverse_branch;
7864 case M_BGE:
7865 s = "slt";
7866 s2 = "bteqz";
7867 goto do_branch;
7868 case M_BGEU:
7869 s = "sltu";
7870 s2 = "bteqz";
7871 goto do_branch;
7872 case M_BGT:
7873 s = "slt";
7874 s2 = "btnez";
7875 goto do_reverse_branch;
7876 case M_BGTU:
7877 s = "sltu";
7878 s2 = "btnez";
7879
7880 do_reverse_branch:
7881 tmp = xreg;
7882 xreg = yreg;
7883 yreg = tmp;
7884
7885 do_branch:
7886 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x,y",
7887 xreg, yreg);
7888 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7889 break;
7890
7891 case M_BEQ_I:
7892 s = "cmpi";
7893 s2 = "bteqz";
7894 s3 = "x,U";
7895 goto do_branch_i;
7896 case M_BNE_I:
7897 s = "cmpi";
7898 s2 = "btnez";
7899 s3 = "x,U";
7900 goto do_branch_i;
7901 case M_BLT_I:
7902 s = "slti";
7903 s2 = "btnez";
7904 s3 = "x,8";
7905 goto do_branch_i;
7906 case M_BLTU_I:
7907 s = "sltiu";
7908 s2 = "btnez";
7909 s3 = "x,8";
7910 goto do_branch_i;
7911 case M_BLE_I:
7912 s = "slti";
7913 s2 = "btnez";
7914 s3 = "x,8";
7915 goto do_addone_branch_i;
7916 case M_BLEU_I:
7917 s = "sltiu";
7918 s2 = "btnez";
7919 s3 = "x,8";
7920 goto do_addone_branch_i;
7921 case M_BGE_I:
7922 s = "slti";
7923 s2 = "bteqz";
7924 s3 = "x,8";
7925 goto do_branch_i;
7926 case M_BGEU_I:
7927 s = "sltiu";
7928 s2 = "bteqz";
7929 s3 = "x,8";
7930 goto do_branch_i;
7931 case M_BGT_I:
7932 s = "slti";
7933 s2 = "bteqz";
7934 s3 = "x,8";
7935 goto do_addone_branch_i;
7936 case M_BGTU_I:
7937 s = "sltiu";
7938 s2 = "bteqz";
7939 s3 = "x,8";
7940
7941 do_addone_branch_i:
7942 if (imm_expr.X_op != O_constant)
7943 as_bad (_("Unsupported large constant"));
7944 ++imm_expr.X_add_number;
7945
7946 do_branch_i:
7947 macro_build ((char *) NULL, &icnt, &imm_expr, s, s3, xreg);
7948 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7949 break;
7950
7951 case M_ABS:
7952 expr1.X_add_number = 0;
98d3f06f 7953 macro_build ((char *) NULL, &icnt, &expr1, "slti", "x,8", yreg);
252b5132 7954 if (xreg != yreg)
ea1fb5dc 7955 move_register (&icnt, xreg, yreg);
252b5132
RH
7956 expr1.X_add_number = 2;
7957 macro_build ((char *) NULL, &icnt, &expr1, "bteqz", "p");
7958 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7959 "neg", "x,w", xreg, xreg);
7960 }
7961}
7962
7963/* For consistency checking, verify that all bits are specified either
7964 by the match/mask part of the instruction definition, or by the
7965 operand list. */
7966static int
7967validate_mips_insn (opc)
7968 const struct mips_opcode *opc;
7969{
7970 const char *p = opc->args;
7971 char c;
7972 unsigned long used_bits = opc->mask;
7973
7974 if ((used_bits & opc->match) != opc->match)
7975 {
7976 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
7977 opc->name, opc->args);
7978 return 0;
7979 }
7980#define USE_BITS(mask,shift) (used_bits |= ((mask) << (shift)))
7981 while (*p)
7982 switch (c = *p++)
7983 {
7984 case ',': break;
7985 case '(': break;
7986 case ')': break;
af7ee8bf
CD
7987 case '+':
7988 switch (c = *p++)
7989 {
7990 case 'A': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7991 case 'B': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
7992 case 'C': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
bbcc0807
CD
7993 case 'D': USE_BITS (OP_MASK_RD, OP_SH_RD);
7994 USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
af7ee8bf
CD
7995 default:
7996 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
7997 c, opc->name, opc->args);
7998 return 0;
7999 }
8000 break;
252b5132
RH
8001 case '<': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
8002 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
8003 case 'A': break;
4372b673 8004 case 'B': USE_BITS (OP_MASK_CODE20, OP_SH_CODE20); break;
252b5132
RH
8005 case 'C': USE_BITS (OP_MASK_COPZ, OP_SH_COPZ); break;
8006 case 'D': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
8007 case 'E': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8008 case 'F': break;
8009 case 'G': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
156c2f8b 8010 case 'H': USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
252b5132 8011 case 'I': break;
e972090a 8012 case 'J': USE_BITS (OP_MASK_CODE19, OP_SH_CODE19); break;
af7ee8bf 8013 case 'K': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
252b5132
RH
8014 case 'L': break;
8015 case 'M': USE_BITS (OP_MASK_CCC, OP_SH_CCC); break;
8016 case 'N': USE_BITS (OP_MASK_BCC, OP_SH_BCC); break;
deec1734
CD
8017 case 'O': USE_BITS (OP_MASK_ALN, OP_SH_ALN); break;
8018 case 'Q': USE_BITS (OP_MASK_VSEL, OP_SH_VSEL);
8019 USE_BITS (OP_MASK_FT, OP_SH_FT); break;
252b5132
RH
8020 case 'R': USE_BITS (OP_MASK_FR, OP_SH_FR); break;
8021 case 'S': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
8022 case 'T': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
8023 case 'V': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
8024 case 'W': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
deec1734
CD
8025 case 'X': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
8026 case 'Y': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
8027 case 'Z': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
252b5132
RH
8028 case 'a': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break;
8029 case 'b': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8030 case 'c': USE_BITS (OP_MASK_CODE, OP_SH_CODE); break;
8031 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
8032 case 'f': break;
8033 case 'h': USE_BITS (OP_MASK_PREFX, OP_SH_PREFX); break;
8034 case 'i': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
8035 case 'j': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
8036 case 'k': USE_BITS (OP_MASK_CACHE, OP_SH_CACHE); break;
8037 case 'l': break;
8038 case 'o': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
8039 case 'p': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
8040 case 'q': USE_BITS (OP_MASK_CODE2, OP_SH_CODE2); break;
8041 case 'r': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8042 case 's': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8043 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8044 case 'u': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
8045 case 'v': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
8046 case 'w': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
8047 case 'x': break;
8048 case 'z': break;
8049 case 'P': USE_BITS (OP_MASK_PERFREG, OP_SH_PERFREG); break;
4372b673
NC
8050 case 'U': USE_BITS (OP_MASK_RD, OP_SH_RD);
8051 USE_BITS (OP_MASK_RT, OP_SH_RT); break;
60b63b72
RS
8052 case 'e': USE_BITS (OP_MASK_VECBYTE, OP_SH_VECBYTE); break;
8053 case '%': USE_BITS (OP_MASK_VECALIGN, OP_SH_VECALIGN); break;
8054 case '[': break;
8055 case ']': break;
252b5132
RH
8056 default:
8057 as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
8058 c, opc->name, opc->args);
8059 return 0;
8060 }
8061#undef USE_BITS
8062 if (used_bits != 0xffffffff)
8063 {
8064 as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
8065 ~used_bits & 0xffffffff, opc->name, opc->args);
8066 return 0;
8067 }
8068 return 1;
8069}
8070
8071/* This routine assembles an instruction into its binary format. As a
8072 side effect, it sets one of the global variables imm_reloc or
8073 offset_reloc to the type of relocation to do if one of the operands
8074 is an address expression. */
8075
8076static void
8077mips_ip (str, ip)
8078 char *str;
8079 struct mips_cl_insn *ip;
8080{
8081 char *s;
8082 const char *args;
43841e91 8083 char c = 0;
252b5132
RH
8084 struct mips_opcode *insn;
8085 char *argsStart;
8086 unsigned int regno;
8087 unsigned int lastregno = 0;
af7ee8bf 8088 unsigned int lastpos = 0;
071742cf 8089 unsigned int limlo, limhi;
252b5132
RH
8090 char *s_reset;
8091 char save_c = 0;
252b5132
RH
8092
8093 insn_error = NULL;
8094
8095 /* If the instruction contains a '.', we first try to match an instruction
8096 including the '.'. Then we try again without the '.'. */
8097 insn = NULL;
3882b010 8098 for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
252b5132
RH
8099 continue;
8100
8101 /* If we stopped on whitespace, then replace the whitespace with null for
8102 the call to hash_find. Save the character we replaced just in case we
8103 have to re-parse the instruction. */
3882b010 8104 if (ISSPACE (*s))
252b5132
RH
8105 {
8106 save_c = *s;
8107 *s++ = '\0';
8108 }
bdaaa2e1 8109
252b5132
RH
8110 insn = (struct mips_opcode *) hash_find (op_hash, str);
8111
8112 /* If we didn't find the instruction in the opcode table, try again, but
8113 this time with just the instruction up to, but not including the
8114 first '.'. */
8115 if (insn == NULL)
8116 {
bdaaa2e1 8117 /* Restore the character we overwrite above (if any). */
252b5132
RH
8118 if (save_c)
8119 *(--s) = save_c;
8120
8121 /* Scan up to the first '.' or whitespace. */
3882b010
L
8122 for (s = str;
8123 *s != '\0' && *s != '.' && !ISSPACE (*s);
8124 ++s)
252b5132
RH
8125 continue;
8126
8127 /* If we did not find a '.', then we can quit now. */
8128 if (*s != '.')
8129 {
8130 insn_error = "unrecognized opcode";
8131 return;
8132 }
8133
8134 /* Lookup the instruction in the hash table. */
8135 *s++ = '\0';
8136 if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
8137 {
8138 insn_error = "unrecognized opcode";
8139 return;
8140 }
252b5132
RH
8141 }
8142
8143 argsStart = s;
8144 for (;;)
8145 {
b34976b6 8146 bfd_boolean ok;
252b5132
RH
8147
8148 assert (strcmp (insn->name, str) == 0);
8149
1f25f5d3
CD
8150 if (OPCODE_IS_MEMBER (insn,
8151 (mips_opts.isa
3396de36 8152 | (file_ase_mips16 ? INSN_MIPS16 : 0)
deec1734 8153 | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
98d3f06f 8154 | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
1f25f5d3 8155 mips_arch))
b34976b6 8156 ok = TRUE;
bdaaa2e1 8157 else
b34976b6 8158 ok = FALSE;
bdaaa2e1 8159
252b5132
RH
8160 if (insn->pinfo != INSN_MACRO)
8161 {
ec68c924 8162 if (mips_arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
b34976b6 8163 ok = FALSE;
252b5132
RH
8164 }
8165
8166 if (! ok)
8167 {
8168 if (insn + 1 < &mips_opcodes[NUMOPCODES]
8169 && strcmp (insn->name, insn[1].name) == 0)
8170 {
8171 ++insn;
8172 continue;
8173 }
252b5132 8174 else
beae10d5 8175 {
268f6bed
L
8176 if (!insn_error)
8177 {
8178 static char buf[100];
316f5878
RS
8179 if (mips_arch_info->is_isa)
8180 sprintf (buf,
8181 _("opcode not supported at this ISA level (%s)"),
8182 mips_cpu_info_from_isa (mips_opts.isa)->name);
8183 else
8184 sprintf (buf,
8185 _("opcode not supported on this processor: %s (%s)"),
8186 mips_arch_info->name,
8187 mips_cpu_info_from_isa (mips_opts.isa)->name);
268f6bed
L
8188 insn_error = buf;
8189 }
8190 if (save_c)
8191 *(--s) = save_c;
2bd7f1f3 8192 return;
252b5132 8193 }
252b5132
RH
8194 }
8195
8196 ip->insn_mo = insn;
8197 ip->insn_opcode = insn->match;
268f6bed 8198 insn_error = NULL;
252b5132
RH
8199 for (args = insn->args;; ++args)
8200 {
deec1734
CD
8201 int is_mdmx;
8202
ad8d3bb3 8203 s += strspn (s, " \t");
deec1734 8204 is_mdmx = 0;
252b5132
RH
8205 switch (*args)
8206 {
8207 case '\0': /* end of args */
8208 if (*s == '\0')
8209 return;
8210 break;
8211
8212 case ',':
8213 if (*s++ == *args)
8214 continue;
8215 s--;
8216 switch (*++args)
8217 {
8218 case 'r':
8219 case 'v':
38487616 8220 ip->insn_opcode |= lastregno << OP_SH_RS;
252b5132
RH
8221 continue;
8222
8223 case 'w':
38487616
TS
8224 ip->insn_opcode |= lastregno << OP_SH_RT;
8225 continue;
8226
252b5132 8227 case 'W':
38487616 8228 ip->insn_opcode |= lastregno << OP_SH_FT;
252b5132
RH
8229 continue;
8230
8231 case 'V':
38487616 8232 ip->insn_opcode |= lastregno << OP_SH_FS;
252b5132
RH
8233 continue;
8234 }
8235 break;
8236
8237 case '(':
8238 /* Handle optional base register.
8239 Either the base register is omitted or
bdaaa2e1 8240 we must have a left paren. */
252b5132
RH
8241 /* This is dependent on the next operand specifier
8242 is a base register specification. */
8243 assert (args[1] == 'b' || args[1] == '5'
8244 || args[1] == '-' || args[1] == '4');
8245 if (*s == '\0')
8246 return;
8247
8248 case ')': /* these must match exactly */
60b63b72
RS
8249 case '[':
8250 case ']':
252b5132
RH
8251 if (*s++ == *args)
8252 continue;
8253 break;
8254
af7ee8bf
CD
8255 case '+': /* Opcode extension character. */
8256 switch (*++args)
8257 {
071742cf
CD
8258 case 'A': /* ins/ext position, becomes LSB. */
8259 limlo = 0;
8260 limhi = 31;
8261 my_getExpression (&imm_expr, s);
8262 check_absolute_expr (ip, &imm_expr);
8263 if ((unsigned long) imm_expr.X_add_number < limlo
8264 || (unsigned long) imm_expr.X_add_number > limhi)
8265 {
8266 as_bad (_("Improper position (%lu)"),
8267 (unsigned long) imm_expr.X_add_number);
8268 imm_expr.X_add_number = limlo;
8269 }
8270 lastpos = imm_expr.X_add_number;
8271 ip->insn_opcode |= (imm_expr.X_add_number
8272 & OP_MASK_SHAMT) << OP_SH_SHAMT;
8273 imm_expr.X_op = O_absent;
8274 s = expr_end;
8275 continue;
8276
8277 case 'B': /* ins size, becomes MSB. */
8278 limlo = 1;
8279 limhi = 32;
8280 my_getExpression (&imm_expr, s);
8281 check_absolute_expr (ip, &imm_expr);
8282 /* Check for negative input so that small negative numbers
8283 will not succeed incorrectly. The checks against
8284 (pos+size) transitively check "size" itself,
8285 assuming that "pos" is reasonable. */
8286 if ((long) imm_expr.X_add_number < 0
8287 || ((unsigned long) imm_expr.X_add_number
8288 + lastpos) < limlo
8289 || ((unsigned long) imm_expr.X_add_number
8290 + lastpos) > limhi)
8291 {
8292 as_bad (_("Improper insert size (%lu, position %lu)"),
8293 (unsigned long) imm_expr.X_add_number,
8294 (unsigned long) lastpos);
8295 imm_expr.X_add_number = limlo - lastpos;
8296 }
8297 ip->insn_opcode |= ((lastpos + imm_expr.X_add_number - 1)
8298 & OP_MASK_INSMSB) << OP_SH_INSMSB;
8299 imm_expr.X_op = O_absent;
8300 s = expr_end;
8301 continue;
8302
8303 case 'C': /* ext size, becomes MSBD. */
8304 limlo = 1;
8305 limhi = 32;
8306 my_getExpression (&imm_expr, s);
8307 check_absolute_expr (ip, &imm_expr);
8308 /* Check for negative input so that small negative numbers
8309 will not succeed incorrectly. The checks against
8310 (pos+size) transitively check "size" itself,
8311 assuming that "pos" is reasonable. */
8312 if ((long) imm_expr.X_add_number < 0
8313 || ((unsigned long) imm_expr.X_add_number
8314 + lastpos) < limlo
8315 || ((unsigned long) imm_expr.X_add_number
8316 + lastpos) > limhi)
8317 {
8318 as_bad (_("Improper extract size (%lu, position %lu)"),
8319 (unsigned long) imm_expr.X_add_number,
8320 (unsigned long) lastpos);
8321 imm_expr.X_add_number = limlo - lastpos;
8322 }
8323 ip->insn_opcode |= ((imm_expr.X_add_number - 1)
8324 & OP_MASK_EXTMSBD) << OP_SH_EXTMSBD;
8325 imm_expr.X_op = O_absent;
8326 s = expr_end;
8327 continue;
af7ee8bf 8328
bbcc0807
CD
8329 case 'D':
8330 /* +D is for disassembly only; never match. */
8331 break;
8332
af7ee8bf
CD
8333 default:
8334 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
8335 *args, insn->name, insn->args);
8336 /* Further processing is fruitless. */
8337 return;
8338 }
8339 break;
8340
252b5132
RH
8341 case '<': /* must be at least one digit */
8342 /*
8343 * According to the manual, if the shift amount is greater
b6ff326e
KH
8344 * than 31 or less than 0, then the shift amount should be
8345 * mod 32. In reality the mips assembler issues an error.
252b5132
RH
8346 * We issue a warning and mask out all but the low 5 bits.
8347 */
8348 my_getExpression (&imm_expr, s);
8349 check_absolute_expr (ip, &imm_expr);
8350 if ((unsigned long) imm_expr.X_add_number > 31)
8351 {
793b27f4
TS
8352 as_warn (_("Improper shift amount (%lu)"),
8353 (unsigned long) imm_expr.X_add_number);
38487616 8354 imm_expr.X_add_number &= OP_MASK_SHAMT;
252b5132 8355 }
38487616 8356 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SHAMT;
252b5132
RH
8357 imm_expr.X_op = O_absent;
8358 s = expr_end;
8359 continue;
8360
8361 case '>': /* shift amount minus 32 */
8362 my_getExpression (&imm_expr, s);
8363 check_absolute_expr (ip, &imm_expr);
8364 if ((unsigned long) imm_expr.X_add_number < 32
8365 || (unsigned long) imm_expr.X_add_number > 63)
8366 break;
38487616 8367 ip->insn_opcode |= (imm_expr.X_add_number - 32) << OP_SH_SHAMT;
252b5132
RH
8368 imm_expr.X_op = O_absent;
8369 s = expr_end;
8370 continue;
8371
252b5132
RH
8372 case 'k': /* cache code */
8373 case 'h': /* prefx code */
8374 my_getExpression (&imm_expr, s);
8375 check_absolute_expr (ip, &imm_expr);
8376 if ((unsigned long) imm_expr.X_add_number > 31)
8377 {
8378 as_warn (_("Invalid value for `%s' (%lu)"),
8379 ip->insn_mo->name,
8380 (unsigned long) imm_expr.X_add_number);
8381 imm_expr.X_add_number &= 0x1f;
8382 }
8383 if (*args == 'k')
8384 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
8385 else
8386 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
8387 imm_expr.X_op = O_absent;
8388 s = expr_end;
8389 continue;
8390
8391 case 'c': /* break code */
8392 my_getExpression (&imm_expr, s);
8393 check_absolute_expr (ip, &imm_expr);
793b27f4 8394 if ((unsigned long) imm_expr.X_add_number > 1023)
252b5132 8395 {
793b27f4
TS
8396 as_warn (_("Illegal break code (%lu)"),
8397 (unsigned long) imm_expr.X_add_number);
38487616 8398 imm_expr.X_add_number &= OP_MASK_CODE;
252b5132 8399 }
38487616 8400 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE;
252b5132
RH
8401 imm_expr.X_op = O_absent;
8402 s = expr_end;
8403 continue;
8404
8405 case 'q': /* lower break code */
8406 my_getExpression (&imm_expr, s);
8407 check_absolute_expr (ip, &imm_expr);
793b27f4 8408 if ((unsigned long) imm_expr.X_add_number > 1023)
252b5132 8409 {
793b27f4
TS
8410 as_warn (_("Illegal lower break code (%lu)"),
8411 (unsigned long) imm_expr.X_add_number);
38487616 8412 imm_expr.X_add_number &= OP_MASK_CODE2;
252b5132 8413 }
38487616 8414 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE2;
252b5132
RH
8415 imm_expr.X_op = O_absent;
8416 s = expr_end;
8417 continue;
8418
4372b673 8419 case 'B': /* 20-bit syscall/break code. */
156c2f8b 8420 my_getExpression (&imm_expr, s);
156c2f8b 8421 check_absolute_expr (ip, &imm_expr);
793b27f4
TS
8422 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
8423 as_warn (_("Illegal 20-bit code (%lu)"),
8424 (unsigned long) imm_expr.X_add_number);
38487616 8425 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE20;
252b5132
RH
8426 imm_expr.X_op = O_absent;
8427 s = expr_end;
8428 continue;
8429
98d3f06f 8430 case 'C': /* Coprocessor code */
beae10d5 8431 my_getExpression (&imm_expr, s);
252b5132 8432 check_absolute_expr (ip, &imm_expr);
98d3f06f 8433 if ((unsigned long) imm_expr.X_add_number >= (1 << 25))
252b5132 8434 {
793b27f4
TS
8435 as_warn (_("Coproccesor code > 25 bits (%lu)"),
8436 (unsigned long) imm_expr.X_add_number);
98d3f06f 8437 imm_expr.X_add_number &= ((1 << 25) - 1);
252b5132 8438 }
beae10d5
KH
8439 ip->insn_opcode |= imm_expr.X_add_number;
8440 imm_expr.X_op = O_absent;
8441 s = expr_end;
8442 continue;
252b5132 8443
4372b673
NC
8444 case 'J': /* 19-bit wait code. */
8445 my_getExpression (&imm_expr, s);
8446 check_absolute_expr (ip, &imm_expr);
793b27f4
TS
8447 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
8448 as_warn (_("Illegal 19-bit code (%lu)"),
8449 (unsigned long) imm_expr.X_add_number);
38487616 8450 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE19;
4372b673
NC
8451 imm_expr.X_op = O_absent;
8452 s = expr_end;
8453 continue;
8454
252b5132 8455 case 'P': /* Performance register */
beae10d5 8456 my_getExpression (&imm_expr, s);
252b5132 8457 check_absolute_expr (ip, &imm_expr);
beae10d5 8458 if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
252b5132 8459 {
793b27f4
TS
8460 as_warn (_("Invalid performance register (%lu)"),
8461 (unsigned long) imm_expr.X_add_number);
38487616 8462 imm_expr.X_add_number &= OP_MASK_PERFREG;
252b5132 8463 }
38487616 8464 ip->insn_opcode |= (imm_expr.X_add_number << OP_SH_PERFREG);
beae10d5
KH
8465 imm_expr.X_op = O_absent;
8466 s = expr_end;
8467 continue;
252b5132
RH
8468
8469 case 'b': /* base register */
8470 case 'd': /* destination register */
8471 case 's': /* source register */
8472 case 't': /* target register */
8473 case 'r': /* both target and source */
8474 case 'v': /* both dest and source */
8475 case 'w': /* both dest and target */
8476 case 'E': /* coprocessor target register */
8477 case 'G': /* coprocessor destination register */
af7ee8bf 8478 case 'K': /* 'rdhwr' destination register */
252b5132
RH
8479 case 'x': /* ignore register name */
8480 case 'z': /* must be zero register */
4372b673 8481 case 'U': /* destination register (clo/clz). */
252b5132
RH
8482 s_reset = s;
8483 if (s[0] == '$')
8484 {
8485
3882b010 8486 if (ISDIGIT (s[1]))
252b5132
RH
8487 {
8488 ++s;
8489 regno = 0;
8490 do
8491 {
8492 regno *= 10;
8493 regno += *s - '0';
8494 ++s;
8495 }
3882b010 8496 while (ISDIGIT (*s));
252b5132
RH
8497 if (regno > 31)
8498 as_bad (_("Invalid register number (%d)"), regno);
8499 }
af7ee8bf 8500 else if (*args == 'E' || *args == 'G' || *args == 'K')
252b5132
RH
8501 goto notreg;
8502 else
8503 {
76db943d
TS
8504 if (s[1] == 'r' && s[2] == 'a')
8505 {
8506 s += 3;
8507 regno = RA;
8508 }
8509 else if (s[1] == 'f' && s[2] == 'p')
252b5132
RH
8510 {
8511 s += 3;
8512 regno = FP;
8513 }
8514 else if (s[1] == 's' && s[2] == 'p')
8515 {
8516 s += 3;
8517 regno = SP;
8518 }
8519 else if (s[1] == 'g' && s[2] == 'p')
8520 {
8521 s += 3;
8522 regno = GP;
8523 }
8524 else if (s[1] == 'a' && s[2] == 't')
8525 {
8526 s += 3;
8527 regno = AT;
8528 }
8529 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8530 {
8531 s += 4;
8532 regno = KT0;
8533 }
8534 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8535 {
8536 s += 4;
8537 regno = KT1;
8538 }
85b51719
TS
8539 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
8540 {
8541 s += 5;
8542 regno = ZERO;
8543 }
252b5132
RH
8544 else if (itbl_have_entries)
8545 {
8546 char *p, *n;
d7ba4a77 8547 unsigned long r;
252b5132 8548
d7ba4a77 8549 p = s + 1; /* advance past '$' */
252b5132
RH
8550 n = itbl_get_field (&p); /* n is name */
8551
d7ba4a77
ILT
8552 /* See if this is a register defined in an
8553 itbl entry. */
8554 if (itbl_get_reg_val (n, &r))
252b5132
RH
8555 {
8556 /* Get_field advances to the start of
8557 the next field, so we need to back
d7ba4a77 8558 rack to the end of the last field. */
bdaaa2e1 8559 if (p)
252b5132 8560 s = p - 1;
bdaaa2e1 8561 else
d7ba4a77 8562 s = strchr (s, '\0');
252b5132
RH
8563 regno = r;
8564 }
8565 else
8566 goto notreg;
beae10d5 8567 }
252b5132
RH
8568 else
8569 goto notreg;
8570 }
8571 if (regno == AT
8572 && ! mips_opts.noat
8573 && *args != 'E'
af7ee8bf
CD
8574 && *args != 'G'
8575 && *args != 'K')
252b5132
RH
8576 as_warn (_("Used $at without \".set noat\""));
8577 c = *args;
8578 if (*s == ' ')
f9419b05 8579 ++s;
252b5132
RH
8580 if (args[1] != *s)
8581 {
8582 if (c == 'r' || c == 'v' || c == 'w')
8583 {
8584 regno = lastregno;
8585 s = s_reset;
f9419b05 8586 ++args;
252b5132
RH
8587 }
8588 }
8589 /* 'z' only matches $0. */
8590 if (c == 'z' && regno != 0)
8591 break;
8592
bdaaa2e1
KH
8593 /* Now that we have assembled one operand, we use the args string
8594 * to figure out where it goes in the instruction. */
252b5132
RH
8595 switch (c)
8596 {
8597 case 'r':
8598 case 's':
8599 case 'v':
8600 case 'b':
38487616 8601 ip->insn_opcode |= regno << OP_SH_RS;
252b5132
RH
8602 break;
8603 case 'd':
8604 case 'G':
af7ee8bf 8605 case 'K':
38487616 8606 ip->insn_opcode |= regno << OP_SH_RD;
252b5132 8607 break;
4372b673 8608 case 'U':
38487616
TS
8609 ip->insn_opcode |= regno << OP_SH_RD;
8610 ip->insn_opcode |= regno << OP_SH_RT;
4372b673 8611 break;
252b5132
RH
8612 case 'w':
8613 case 't':
8614 case 'E':
38487616 8615 ip->insn_opcode |= regno << OP_SH_RT;
252b5132
RH
8616 break;
8617 case 'x':
8618 /* This case exists because on the r3000 trunc
8619 expands into a macro which requires a gp
8620 register. On the r6000 or r4000 it is
8621 assembled into a single instruction which
8622 ignores the register. Thus the insn version
8623 is MIPS_ISA2 and uses 'x', and the macro
8624 version is MIPS_ISA1 and uses 't'. */
8625 break;
8626 case 'z':
8627 /* This case is for the div instruction, which
8628 acts differently if the destination argument
8629 is $0. This only matches $0, and is checked
8630 outside the switch. */
8631 break;
8632 case 'D':
8633 /* Itbl operand; not yet implemented. FIXME ?? */
8634 break;
8635 /* What about all other operands like 'i', which
8636 can be specified in the opcode table? */
8637 }
8638 lastregno = regno;
8639 continue;
8640 }
8641 notreg:
8642 switch (*args++)
8643 {
8644 case 'r':
8645 case 'v':
38487616 8646 ip->insn_opcode |= lastregno << OP_SH_RS;
252b5132
RH
8647 continue;
8648 case 'w':
38487616 8649 ip->insn_opcode |= lastregno << OP_SH_RT;
252b5132
RH
8650 continue;
8651 }
8652 break;
8653
deec1734
CD
8654 case 'O': /* MDMX alignment immediate constant. */
8655 my_getExpression (&imm_expr, s);
8656 check_absolute_expr (ip, &imm_expr);
8657 if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
8658 {
8659 as_warn ("Improper align amount (%ld), using low bits",
8660 (long) imm_expr.X_add_number);
8661 imm_expr.X_add_number &= OP_MASK_ALN;
8662 }
8663 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_ALN;
8664 imm_expr.X_op = O_absent;
8665 s = expr_end;
8666 continue;
8667
8668 case 'Q': /* MDMX vector, element sel, or const. */
8669 if (s[0] != '$')
8670 {
8671 /* MDMX Immediate. */
8672 my_getExpression (&imm_expr, s);
8673 check_absolute_expr (ip, &imm_expr);
8674 if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
8675 {
8676 as_warn (_("Invalid MDMX Immediate (%ld)"),
8677 (long) imm_expr.X_add_number);
8678 imm_expr.X_add_number &= OP_MASK_FT;
8679 }
8680 imm_expr.X_add_number &= OP_MASK_FT;
8681 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8682 ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
8683 else
8684 ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
8685 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_FT;
8686 imm_expr.X_op = O_absent;
8687 s = expr_end;
8688 continue;
8689 }
8690 /* Not MDMX Immediate. Fall through. */
8691 case 'X': /* MDMX destination register. */
8692 case 'Y': /* MDMX source register. */
8693 case 'Z': /* MDMX target register. */
8694 is_mdmx = 1;
252b5132
RH
8695 case 'D': /* floating point destination register */
8696 case 'S': /* floating point source register */
8697 case 'T': /* floating point target register */
8698 case 'R': /* floating point source register */
8699 case 'V':
8700 case 'W':
8701 s_reset = s;
deec1734
CD
8702 /* Accept $fN for FP and MDMX register numbers, and in
8703 addition accept $vN for MDMX register numbers. */
8704 if ((s[0] == '$' && s[1] == 'f' && ISDIGIT (s[2]))
8705 || (is_mdmx != 0 && s[0] == '$' && s[1] == 'v'
8706 && ISDIGIT (s[2])))
252b5132
RH
8707 {
8708 s += 2;
8709 regno = 0;
8710 do
8711 {
8712 regno *= 10;
8713 regno += *s - '0';
8714 ++s;
8715 }
3882b010 8716 while (ISDIGIT (*s));
252b5132
RH
8717
8718 if (regno > 31)
8719 as_bad (_("Invalid float register number (%d)"), regno);
8720
8721 if ((regno & 1) != 0
ca4e0257 8722 && HAVE_32BIT_FPRS
252b5132
RH
8723 && ! (strcmp (str, "mtc1") == 0
8724 || strcmp (str, "mfc1") == 0
8725 || strcmp (str, "lwc1") == 0
8726 || strcmp (str, "swc1") == 0
8727 || strcmp (str, "l.s") == 0
8728 || strcmp (str, "s.s") == 0))
8729 as_warn (_("Float register should be even, was %d"),
8730 regno);
8731
8732 c = *args;
8733 if (*s == ' ')
f9419b05 8734 ++s;
252b5132
RH
8735 if (args[1] != *s)
8736 {
8737 if (c == 'V' || c == 'W')
8738 {
8739 regno = lastregno;
8740 s = s_reset;
f9419b05 8741 ++args;
252b5132
RH
8742 }
8743 }
8744 switch (c)
8745 {
8746 case 'D':
deec1734 8747 case 'X':
38487616 8748 ip->insn_opcode |= regno << OP_SH_FD;
252b5132
RH
8749 break;
8750 case 'V':
8751 case 'S':
deec1734 8752 case 'Y':
38487616 8753 ip->insn_opcode |= regno << OP_SH_FS;
252b5132 8754 break;
deec1734
CD
8755 case 'Q':
8756 /* This is like 'Z', but also needs to fix the MDMX
8757 vector/scalar select bits. Note that the
8758 scalar immediate case is handled above. */
8759 if (*s == '[')
8760 {
8761 int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
8762 int max_el = (is_qh ? 3 : 7);
8763 s++;
8764 my_getExpression(&imm_expr, s);
8765 check_absolute_expr (ip, &imm_expr);
8766 s = expr_end;
8767 if (imm_expr.X_add_number > max_el)
8768 as_bad(_("Bad element selector %ld"),
8769 (long) imm_expr.X_add_number);
8770 imm_expr.X_add_number &= max_el;
8771 ip->insn_opcode |= (imm_expr.X_add_number
8772 << (OP_SH_VSEL +
8773 (is_qh ? 2 : 1)));
8774 if (*s != ']')
8775 as_warn(_("Expecting ']' found '%s'"), s);
8776 else
8777 s++;
8778 }
8779 else
8780 {
8781 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8782 ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
8783 << OP_SH_VSEL);
8784 else
8785 ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
8786 OP_SH_VSEL);
8787 }
8788 /* Fall through */
252b5132
RH
8789 case 'W':
8790 case 'T':
deec1734 8791 case 'Z':
38487616 8792 ip->insn_opcode |= regno << OP_SH_FT;
252b5132
RH
8793 break;
8794 case 'R':
38487616 8795 ip->insn_opcode |= regno << OP_SH_FR;
252b5132
RH
8796 break;
8797 }
8798 lastregno = regno;
8799 continue;
8800 }
8801
252b5132
RH
8802 switch (*args++)
8803 {
8804 case 'V':
38487616 8805 ip->insn_opcode |= lastregno << OP_SH_FS;
252b5132
RH
8806 continue;
8807 case 'W':
38487616 8808 ip->insn_opcode |= lastregno << OP_SH_FT;
252b5132
RH
8809 continue;
8810 }
8811 break;
8812
8813 case 'I':
8814 my_getExpression (&imm_expr, s);
8815 if (imm_expr.X_op != O_big
8816 && imm_expr.X_op != O_constant)
8817 insn_error = _("absolute expression required");
8818 s = expr_end;
8819 continue;
8820
8821 case 'A':
8822 my_getExpression (&offset_expr, s);
f6688943 8823 *imm_reloc = BFD_RELOC_32;
252b5132
RH
8824 s = expr_end;
8825 continue;
8826
8827 case 'F':
8828 case 'L':
8829 case 'f':
8830 case 'l':
8831 {
8832 int f64;
ca4e0257 8833 int using_gprs;
252b5132
RH
8834 char *save_in;
8835 char *err;
8836 unsigned char temp[8];
8837 int len;
8838 unsigned int length;
8839 segT seg;
8840 subsegT subseg;
8841 char *p;
8842
8843 /* These only appear as the last operand in an
8844 instruction, and every instruction that accepts
8845 them in any variant accepts them in all variants.
8846 This means we don't have to worry about backing out
8847 any changes if the instruction does not match.
8848
8849 The difference between them is the size of the
8850 floating point constant and where it goes. For 'F'
8851 and 'L' the constant is 64 bits; for 'f' and 'l' it
8852 is 32 bits. Where the constant is placed is based
8853 on how the MIPS assembler does things:
8854 F -- .rdata
8855 L -- .lit8
8856 f -- immediate value
8857 l -- .lit4
8858
8859 The .lit4 and .lit8 sections are only used if
8860 permitted by the -G argument.
8861
8862 When generating embedded PIC code, we use the
8863 .lit8 section but not the .lit4 section (we can do
8864 .lit4 inline easily; we need to put .lit8
8865 somewhere in the data segment, and using .lit8
8866 permits the linker to eventually combine identical
ca4e0257
RS
8867 .lit8 entries).
8868
8869 The code below needs to know whether the target register
8870 is 32 or 64 bits wide. It relies on the fact 'f' and
8871 'F' are used with GPR-based instructions and 'l' and
8872 'L' are used with FPR-based instructions. */
252b5132
RH
8873
8874 f64 = *args == 'F' || *args == 'L';
ca4e0257 8875 using_gprs = *args == 'F' || *args == 'f';
252b5132
RH
8876
8877 save_in = input_line_pointer;
8878 input_line_pointer = s;
8879 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
8880 length = len;
8881 s = input_line_pointer;
8882 input_line_pointer = save_in;
8883 if (err != NULL && *err != '\0')
8884 {
8885 as_bad (_("Bad floating point constant: %s"), err);
8886 memset (temp, '\0', sizeof temp);
8887 length = f64 ? 8 : 4;
8888 }
8889
156c2f8b 8890 assert (length == (unsigned) (f64 ? 8 : 4));
252b5132
RH
8891
8892 if (*args == 'f'
8893 || (*args == 'l'
8894 && (! USE_GLOBAL_POINTER_OPT
8895 || mips_pic == EMBEDDED_PIC
8896 || g_switch_value < 4
8897 || (temp[0] == 0 && temp[1] == 0)
8898 || (temp[2] == 0 && temp[3] == 0))))
8899 {
8900 imm_expr.X_op = O_constant;
8901 if (! target_big_endian)
8902 imm_expr.X_add_number = bfd_getl32 (temp);
8903 else
8904 imm_expr.X_add_number = bfd_getb32 (temp);
8905 }
8906 else if (length > 4
119d663a 8907 && ! mips_disable_float_construction
ca4e0257
RS
8908 /* Constants can only be constructed in GPRs and
8909 copied to FPRs if the GPRs are at least as wide
8910 as the FPRs. Force the constant into memory if
8911 we are using 64-bit FPRs but the GPRs are only
8912 32 bits wide. */
8913 && (using_gprs
8914 || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
252b5132
RH
8915 && ((temp[0] == 0 && temp[1] == 0)
8916 || (temp[2] == 0 && temp[3] == 0))
8917 && ((temp[4] == 0 && temp[5] == 0)
8918 || (temp[6] == 0 && temp[7] == 0)))
8919 {
ca4e0257
RS
8920 /* The value is simple enough to load with a couple of
8921 instructions. If using 32-bit registers, set
8922 imm_expr to the high order 32 bits and offset_expr to
8923 the low order 32 bits. Otherwise, set imm_expr to
8924 the entire 64 bit constant. */
8925 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
252b5132
RH
8926 {
8927 imm_expr.X_op = O_constant;
8928 offset_expr.X_op = O_constant;
8929 if (! target_big_endian)
8930 {
8931 imm_expr.X_add_number = bfd_getl32 (temp + 4);
8932 offset_expr.X_add_number = bfd_getl32 (temp);
8933 }
8934 else
8935 {
8936 imm_expr.X_add_number = bfd_getb32 (temp);
8937 offset_expr.X_add_number = bfd_getb32 (temp + 4);
8938 }
8939 if (offset_expr.X_add_number == 0)
8940 offset_expr.X_op = O_absent;
8941 }
8942 else if (sizeof (imm_expr.X_add_number) > 4)
8943 {
8944 imm_expr.X_op = O_constant;
8945 if (! target_big_endian)
8946 imm_expr.X_add_number = bfd_getl64 (temp);
8947 else
8948 imm_expr.X_add_number = bfd_getb64 (temp);
8949 }
8950 else
8951 {
8952 imm_expr.X_op = O_big;
8953 imm_expr.X_add_number = 4;
8954 if (! target_big_endian)
8955 {
8956 generic_bignum[0] = bfd_getl16 (temp);
8957 generic_bignum[1] = bfd_getl16 (temp + 2);
8958 generic_bignum[2] = bfd_getl16 (temp + 4);
8959 generic_bignum[3] = bfd_getl16 (temp + 6);
8960 }
8961 else
8962 {
8963 generic_bignum[0] = bfd_getb16 (temp + 6);
8964 generic_bignum[1] = bfd_getb16 (temp + 4);
8965 generic_bignum[2] = bfd_getb16 (temp + 2);
8966 generic_bignum[3] = bfd_getb16 (temp);
8967 }
8968 }
8969 }
8970 else
8971 {
8972 const char *newname;
8973 segT new_seg;
8974
8975 /* Switch to the right section. */
8976 seg = now_seg;
8977 subseg = now_subseg;
8978 switch (*args)
8979 {
8980 default: /* unused default case avoids warnings. */
8981 case 'L':
8982 newname = RDATA_SECTION_NAME;
bb2d6cd7
GK
8983 if ((USE_GLOBAL_POINTER_OPT && g_switch_value >= 8)
8984 || mips_pic == EMBEDDED_PIC)
252b5132
RH
8985 newname = ".lit8";
8986 break;
8987 case 'F':
bb2d6cd7
GK
8988 if (mips_pic == EMBEDDED_PIC)
8989 newname = ".lit8";
8990 else
8991 newname = RDATA_SECTION_NAME;
252b5132
RH
8992 break;
8993 case 'l':
8994 assert (!USE_GLOBAL_POINTER_OPT
8995 || g_switch_value >= 4);
8996 newname = ".lit4";
8997 break;
8998 }
8999 new_seg = subseg_new (newname, (subsegT) 0);
9000 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
9001 bfd_set_section_flags (stdoutput, new_seg,
9002 (SEC_ALLOC
9003 | SEC_LOAD
9004 | SEC_READONLY
9005 | SEC_DATA));
9006 frag_align (*args == 'l' ? 2 : 3, 0, 0);
9007 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
9008 && strcmp (TARGET_OS, "elf") != 0)
9009 record_alignment (new_seg, 4);
9010 else
9011 record_alignment (new_seg, *args == 'l' ? 2 : 3);
9012 if (seg == now_seg)
9013 as_bad (_("Can't use floating point insn in this section"));
9014
9015 /* Set the argument to the current address in the
9016 section. */
9017 offset_expr.X_op = O_symbol;
9018 offset_expr.X_add_symbol =
9019 symbol_new ("L0\001", now_seg,
9020 (valueT) frag_now_fix (), frag_now);
9021 offset_expr.X_add_number = 0;
9022
9023 /* Put the floating point number into the section. */
9024 p = frag_more ((int) length);
9025 memcpy (p, temp, length);
9026
9027 /* Switch back to the original section. */
9028 subseg_set (seg, subseg);
9029 }
9030 }
9031 continue;
9032
9033 case 'i': /* 16 bit unsigned immediate */
9034 case 'j': /* 16 bit signed immediate */
f6688943 9035 *imm_reloc = BFD_RELOC_LO16;
5e0116d5 9036 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0)
252b5132
RH
9037 {
9038 int more;
5e0116d5
RS
9039 offsetT minval, maxval;
9040
9041 more = (insn + 1 < &mips_opcodes[NUMOPCODES]
9042 && strcmp (insn->name, insn[1].name) == 0);
9043
9044 /* If the expression was written as an unsigned number,
9045 only treat it as signed if there are no more
9046 alternatives. */
9047 if (more
9048 && *args == 'j'
9049 && sizeof (imm_expr.X_add_number) <= 4
9050 && imm_expr.X_op == O_constant
9051 && imm_expr.X_add_number < 0
9052 && imm_expr.X_unsigned
9053 && HAVE_64BIT_GPRS)
9054 break;
9055
9056 /* For compatibility with older assemblers, we accept
9057 0x8000-0xffff as signed 16-bit numbers when only
9058 signed numbers are allowed. */
9059 if (*args == 'i')
9060 minval = 0, maxval = 0xffff;
9061 else if (more)
9062 minval = -0x8000, maxval = 0x7fff;
252b5132 9063 else
5e0116d5
RS
9064 minval = -0x8000, maxval = 0xffff;
9065
9066 if (imm_expr.X_op != O_constant
9067 || imm_expr.X_add_number < minval
9068 || imm_expr.X_add_number > maxval)
252b5132
RH
9069 {
9070 if (more)
9071 break;
2ae7e77b
AH
9072 if (imm_expr.X_op == O_constant
9073 || imm_expr.X_op == O_big)
5e0116d5 9074 as_bad (_("expression out of range"));
252b5132
RH
9075 }
9076 }
9077 s = expr_end;
9078 continue;
9079
9080 case 'o': /* 16 bit offset */
5e0116d5
RS
9081 /* Check whether there is only a single bracketed expression
9082 left. If so, it must be the base register and the
9083 constant must be zero. */
9084 if (*s == '(' && strchr (s + 1, '(') == 0)
9085 {
9086 offset_expr.X_op = O_constant;
9087 offset_expr.X_add_number = 0;
9088 continue;
9089 }
252b5132
RH
9090
9091 /* If this value won't fit into a 16 bit offset, then go
9092 find a macro that will generate the 32 bit offset
afdbd6d0 9093 code pattern. */
5e0116d5 9094 if (my_getSmallExpression (&offset_expr, offset_reloc, s) == 0
252b5132
RH
9095 && (offset_expr.X_op != O_constant
9096 || offset_expr.X_add_number >= 0x8000
afdbd6d0 9097 || offset_expr.X_add_number < -0x8000))
252b5132
RH
9098 break;
9099
252b5132
RH
9100 s = expr_end;
9101 continue;
9102
9103 case 'p': /* pc relative offset */
af6ae2ad 9104 *offset_reloc = BFD_RELOC_16_PCREL_S2;
252b5132
RH
9105 my_getExpression (&offset_expr, s);
9106 s = expr_end;
9107 continue;
9108
9109 case 'u': /* upper 16 bits */
5e0116d5
RS
9110 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0
9111 && imm_expr.X_op == O_constant
9112 && (imm_expr.X_add_number < 0
9113 || imm_expr.X_add_number >= 0x10000))
252b5132
RH
9114 as_bad (_("lui expression not in range 0..65535"));
9115 s = expr_end;
9116 continue;
9117
9118 case 'a': /* 26 bit address */
9119 my_getExpression (&offset_expr, s);
9120 s = expr_end;
f6688943 9121 *offset_reloc = BFD_RELOC_MIPS_JMP;
252b5132
RH
9122 continue;
9123
9124 case 'N': /* 3 bit branch condition code */
9125 case 'M': /* 3 bit compare condition code */
9126 if (strncmp (s, "$fcc", 4) != 0)
9127 break;
9128 s += 4;
9129 regno = 0;
9130 do
9131 {
9132 regno *= 10;
9133 regno += *s - '0';
9134 ++s;
9135 }
3882b010 9136 while (ISDIGIT (*s));
252b5132
RH
9137 if (regno > 7)
9138 as_bad (_("invalid condition code register $fcc%d"), regno);
9139 if (*args == 'N')
9140 ip->insn_opcode |= regno << OP_SH_BCC;
9141 else
9142 ip->insn_opcode |= regno << OP_SH_CCC;
beae10d5 9143 continue;
252b5132 9144
156c2f8b
NC
9145 case 'H':
9146 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
9147 s += 2;
3882b010 9148 if (ISDIGIT (*s))
156c2f8b
NC
9149 {
9150 c = 0;
9151 do
9152 {
9153 c *= 10;
9154 c += *s - '0';
9155 ++s;
9156 }
3882b010 9157 while (ISDIGIT (*s));
156c2f8b
NC
9158 }
9159 else
9160 c = 8; /* Invalid sel value. */
9161
9162 if (c > 7)
9163 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
9164 ip->insn_opcode |= c;
9165 continue;
9166
60b63b72
RS
9167 case 'e':
9168 /* Must be at least one digit. */
9169 my_getExpression (&imm_expr, s);
9170 check_absolute_expr (ip, &imm_expr);
9171
9172 if ((unsigned long) imm_expr.X_add_number
9173 > (unsigned long) OP_MASK_VECBYTE)
9174 {
9175 as_bad (_("bad byte vector index (%ld)"),
9176 (long) imm_expr.X_add_number);
9177 imm_expr.X_add_number = 0;
9178 }
9179
9180 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECBYTE;
9181 imm_expr.X_op = O_absent;
9182 s = expr_end;
9183 continue;
9184
9185 case '%':
9186 my_getExpression (&imm_expr, s);
9187 check_absolute_expr (ip, &imm_expr);
9188
9189 if ((unsigned long) imm_expr.X_add_number
9190 > (unsigned long) OP_MASK_VECALIGN)
9191 {
9192 as_bad (_("bad byte vector index (%ld)"),
9193 (long) imm_expr.X_add_number);
9194 imm_expr.X_add_number = 0;
9195 }
9196
9197 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_VECALIGN;
9198 imm_expr.X_op = O_absent;
9199 s = expr_end;
9200 continue;
9201
252b5132
RH
9202 default:
9203 as_bad (_("bad char = '%c'\n"), *args);
9204 internalError ();
9205 }
9206 break;
9207 }
9208 /* Args don't match. */
9209 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
9210 !strcmp (insn->name, insn[1].name))
9211 {
9212 ++insn;
9213 s = argsStart;
268f6bed 9214 insn_error = _("illegal operands");
252b5132
RH
9215 continue;
9216 }
268f6bed
L
9217 if (save_c)
9218 *(--s) = save_c;
252b5132
RH
9219 insn_error = _("illegal operands");
9220 return;
9221 }
9222}
9223
9224/* This routine assembles an instruction into its binary format when
9225 assembling for the mips16. As a side effect, it sets one of the
9226 global variables imm_reloc or offset_reloc to the type of
9227 relocation to do if one of the operands is an address expression.
9228 It also sets mips16_small and mips16_ext if the user explicitly
9229 requested a small or extended instruction. */
9230
9231static void
9232mips16_ip (str, ip)
9233 char *str;
9234 struct mips_cl_insn *ip;
9235{
9236 char *s;
9237 const char *args;
9238 struct mips_opcode *insn;
9239 char *argsstart;
9240 unsigned int regno;
9241 unsigned int lastregno = 0;
9242 char *s_reset;
9243
9244 insn_error = NULL;
9245
b34976b6
AM
9246 mips16_small = FALSE;
9247 mips16_ext = FALSE;
252b5132 9248
3882b010 9249 for (s = str; ISLOWER (*s); ++s)
252b5132
RH
9250 ;
9251 switch (*s)
9252 {
9253 case '\0':
9254 break;
9255
9256 case ' ':
9257 *s++ = '\0';
9258 break;
9259
9260 case '.':
9261 if (s[1] == 't' && s[2] == ' ')
9262 {
9263 *s = '\0';
b34976b6 9264 mips16_small = TRUE;
252b5132
RH
9265 s += 3;
9266 break;
9267 }
9268 else if (s[1] == 'e' && s[2] == ' ')
9269 {
9270 *s = '\0';
b34976b6 9271 mips16_ext = TRUE;
252b5132
RH
9272 s += 3;
9273 break;
9274 }
9275 /* Fall through. */
9276 default:
9277 insn_error = _("unknown opcode");
9278 return;
9279 }
9280
9281 if (mips_opts.noautoextend && ! mips16_ext)
b34976b6 9282 mips16_small = TRUE;
252b5132
RH
9283
9284 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
9285 {
9286 insn_error = _("unrecognized opcode");
9287 return;
9288 }
9289
9290 argsstart = s;
9291 for (;;)
9292 {
9293 assert (strcmp (insn->name, str) == 0);
9294
9295 ip->insn_mo = insn;
9296 ip->insn_opcode = insn->match;
b34976b6 9297 ip->use_extend = FALSE;
252b5132 9298 imm_expr.X_op = O_absent;
f6688943
TS
9299 imm_reloc[0] = BFD_RELOC_UNUSED;
9300 imm_reloc[1] = BFD_RELOC_UNUSED;
9301 imm_reloc[2] = BFD_RELOC_UNUSED;
252b5132 9302 offset_expr.X_op = O_absent;
f6688943
TS
9303 offset_reloc[0] = BFD_RELOC_UNUSED;
9304 offset_reloc[1] = BFD_RELOC_UNUSED;
9305 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
9306 for (args = insn->args; 1; ++args)
9307 {
9308 int c;
9309
9310 if (*s == ' ')
9311 ++s;
9312
9313 /* In this switch statement we call break if we did not find
9314 a match, continue if we did find a match, or return if we
9315 are done. */
9316
9317 c = *args;
9318 switch (c)
9319 {
9320 case '\0':
9321 if (*s == '\0')
9322 {
9323 /* Stuff the immediate value in now, if we can. */
9324 if (imm_expr.X_op == O_constant
f6688943 9325 && *imm_reloc > BFD_RELOC_UNUSED
252b5132
RH
9326 && insn->pinfo != INSN_MACRO)
9327 {
c4e7957c 9328 mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
b34976b6 9329 imm_expr.X_add_number, TRUE, mips16_small,
252b5132
RH
9330 mips16_ext, &ip->insn_opcode,
9331 &ip->use_extend, &ip->extend);
9332 imm_expr.X_op = O_absent;
f6688943 9333 *imm_reloc = BFD_RELOC_UNUSED;
252b5132
RH
9334 }
9335
9336 return;
9337 }
9338 break;
9339
9340 case ',':
9341 if (*s++ == c)
9342 continue;
9343 s--;
9344 switch (*++args)
9345 {
9346 case 'v':
9347 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9348 continue;
9349 case 'w':
9350 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9351 continue;
9352 }
9353 break;
9354
9355 case '(':
9356 case ')':
9357 if (*s++ == c)
9358 continue;
9359 break;
9360
9361 case 'v':
9362 case 'w':
9363 if (s[0] != '$')
9364 {
9365 if (c == 'v')
9366 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9367 else
9368 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9369 ++args;
9370 continue;
9371 }
9372 /* Fall through. */
9373 case 'x':
9374 case 'y':
9375 case 'z':
9376 case 'Z':
9377 case '0':
9378 case 'S':
9379 case 'R':
9380 case 'X':
9381 case 'Y':
9382 if (s[0] != '$')
9383 break;
9384 s_reset = s;
3882b010 9385 if (ISDIGIT (s[1]))
252b5132
RH
9386 {
9387 ++s;
9388 regno = 0;
9389 do
9390 {
9391 regno *= 10;
9392 regno += *s - '0';
9393 ++s;
9394 }
3882b010 9395 while (ISDIGIT (*s));
252b5132
RH
9396 if (regno > 31)
9397 {
9398 as_bad (_("invalid register number (%d)"), regno);
9399 regno = 2;
9400 }
9401 }
9402 else
9403 {
76db943d
TS
9404 if (s[1] == 'r' && s[2] == 'a')
9405 {
9406 s += 3;
9407 regno = RA;
9408 }
9409 else if (s[1] == 'f' && s[2] == 'p')
252b5132
RH
9410 {
9411 s += 3;
9412 regno = FP;
9413 }
9414 else if (s[1] == 's' && s[2] == 'p')
9415 {
9416 s += 3;
9417 regno = SP;
9418 }
9419 else if (s[1] == 'g' && s[2] == 'p')
9420 {
9421 s += 3;
9422 regno = GP;
9423 }
9424 else if (s[1] == 'a' && s[2] == 't')
9425 {
9426 s += 3;
9427 regno = AT;
9428 }
9429 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
9430 {
9431 s += 4;
9432 regno = KT0;
9433 }
9434 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
9435 {
9436 s += 4;
9437 regno = KT1;
9438 }
85b51719
TS
9439 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
9440 {
9441 s += 5;
9442 regno = ZERO;
9443 }
252b5132
RH
9444 else
9445 break;
9446 }
9447
9448 if (*s == ' ')
9449 ++s;
9450 if (args[1] != *s)
9451 {
9452 if (c == 'v' || c == 'w')
9453 {
9454 regno = mips16_to_32_reg_map[lastregno];
9455 s = s_reset;
f9419b05 9456 ++args;
252b5132
RH
9457 }
9458 }
9459
9460 switch (c)
9461 {
9462 case 'x':
9463 case 'y':
9464 case 'z':
9465 case 'v':
9466 case 'w':
9467 case 'Z':
9468 regno = mips32_to_16_reg_map[regno];
9469 break;
9470
9471 case '0':
9472 if (regno != 0)
9473 regno = ILLEGAL_REG;
9474 break;
9475
9476 case 'S':
9477 if (regno != SP)
9478 regno = ILLEGAL_REG;
9479 break;
9480
9481 case 'R':
9482 if (regno != RA)
9483 regno = ILLEGAL_REG;
9484 break;
9485
9486 case 'X':
9487 case 'Y':
9488 if (regno == AT && ! mips_opts.noat)
9489 as_warn (_("used $at without \".set noat\""));
9490 break;
9491
9492 default:
9493 internalError ();
9494 }
9495
9496 if (regno == ILLEGAL_REG)
9497 break;
9498
9499 switch (c)
9500 {
9501 case 'x':
9502 case 'v':
9503 ip->insn_opcode |= regno << MIPS16OP_SH_RX;
9504 break;
9505 case 'y':
9506 case 'w':
9507 ip->insn_opcode |= regno << MIPS16OP_SH_RY;
9508 break;
9509 case 'z':
9510 ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
9511 break;
9512 case 'Z':
9513 ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
9514 case '0':
9515 case 'S':
9516 case 'R':
9517 break;
9518 case 'X':
9519 ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
9520 break;
9521 case 'Y':
9522 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
9523 ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
9524 break;
9525 default:
9526 internalError ();
9527 }
9528
9529 lastregno = regno;
9530 continue;
9531
9532 case 'P':
9533 if (strncmp (s, "$pc", 3) == 0)
9534 {
9535 s += 3;
9536 continue;
9537 }
9538 break;
9539
9540 case '<':
9541 case '>':
9542 case '[':
9543 case ']':
9544 case '4':
9545 case '5':
9546 case 'H':
9547 case 'W':
9548 case 'D':
9549 case 'j':
9550 case '8':
9551 case 'V':
9552 case 'C':
9553 case 'U':
9554 case 'k':
9555 case 'K':
9556 if (s[0] == '%'
9557 && strncmp (s + 1, "gprel(", sizeof "gprel(" - 1) == 0)
9558 {
9559 /* This is %gprel(SYMBOL). We need to read SYMBOL,
9560 and generate the appropriate reloc. If the text
9561 inside %gprel is not a symbol name with an
9562 optional offset, then we generate a normal reloc
9563 and will probably fail later. */
9564 my_getExpression (&imm_expr, s + sizeof "%gprel" - 1);
9565 if (imm_expr.X_op == O_symbol)
9566 {
b34976b6 9567 mips16_ext = TRUE;
f6688943 9568 *imm_reloc = BFD_RELOC_MIPS16_GPREL;
252b5132 9569 s = expr_end;
b34976b6 9570 ip->use_extend = TRUE;
252b5132
RH
9571 ip->extend = 0;
9572 continue;
9573 }
9574 }
9575 else
9576 {
9577 /* Just pick up a normal expression. */
9578 my_getExpression (&imm_expr, s);
9579 }
9580
9581 if (imm_expr.X_op == O_register)
9582 {
9583 /* What we thought was an expression turned out to
9584 be a register. */
9585
9586 if (s[0] == '(' && args[1] == '(')
9587 {
9588 /* It looks like the expression was omitted
9589 before a register indirection, which means
9590 that the expression is implicitly zero. We
9591 still set up imm_expr, so that we handle
9592 explicit extensions correctly. */
9593 imm_expr.X_op = O_constant;
9594 imm_expr.X_add_number = 0;
f6688943 9595 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
9596 continue;
9597 }
9598
9599 break;
9600 }
9601
9602 /* We need to relax this instruction. */
f6688943 9603 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
9604 s = expr_end;
9605 continue;
9606
9607 case 'p':
9608 case 'q':
9609 case 'A':
9610 case 'B':
9611 case 'E':
9612 /* We use offset_reloc rather than imm_reloc for the PC
9613 relative operands. This lets macros with both
9614 immediate and address operands work correctly. */
9615 my_getExpression (&offset_expr, s);
9616
9617 if (offset_expr.X_op == O_register)
9618 break;
9619
9620 /* We need to relax this instruction. */
f6688943 9621 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
9622 s = expr_end;
9623 continue;
9624
9625 case '6': /* break code */
9626 my_getExpression (&imm_expr, s);
9627 check_absolute_expr (ip, &imm_expr);
9628 if ((unsigned long) imm_expr.X_add_number > 63)
9629 {
9630 as_warn (_("Invalid value for `%s' (%lu)"),
9631 ip->insn_mo->name,
9632 (unsigned long) imm_expr.X_add_number);
9633 imm_expr.X_add_number &= 0x3f;
9634 }
9635 ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
9636 imm_expr.X_op = O_absent;
9637 s = expr_end;
9638 continue;
9639
9640 case 'a': /* 26 bit address */
9641 my_getExpression (&offset_expr, s);
9642 s = expr_end;
f6688943 9643 *offset_reloc = BFD_RELOC_MIPS16_JMP;
252b5132
RH
9644 ip->insn_opcode <<= 16;
9645 continue;
9646
9647 case 'l': /* register list for entry macro */
9648 case 'L': /* register list for exit macro */
9649 {
9650 int mask;
9651
9652 if (c == 'l')
9653 mask = 0;
9654 else
9655 mask = 7 << 3;
9656 while (*s != '\0')
9657 {
9658 int freg, reg1, reg2;
9659
9660 while (*s == ' ' || *s == ',')
9661 ++s;
9662 if (*s != '$')
9663 {
9664 as_bad (_("can't parse register list"));
9665 break;
9666 }
9667 ++s;
9668 if (*s != 'f')
9669 freg = 0;
9670 else
9671 {
9672 freg = 1;
9673 ++s;
9674 }
9675 reg1 = 0;
3882b010 9676 while (ISDIGIT (*s))
252b5132
RH
9677 {
9678 reg1 *= 10;
9679 reg1 += *s - '0';
9680 ++s;
9681 }
9682 if (*s == ' ')
9683 ++s;
9684 if (*s != '-')
9685 reg2 = reg1;
9686 else
9687 {
9688 ++s;
9689 if (*s != '$')
9690 break;
9691 ++s;
9692 if (freg)
9693 {
9694 if (*s == 'f')
9695 ++s;
9696 else
9697 {
9698 as_bad (_("invalid register list"));
9699 break;
9700 }
9701 }
9702 reg2 = 0;
3882b010 9703 while (ISDIGIT (*s))
252b5132
RH
9704 {
9705 reg2 *= 10;
9706 reg2 += *s - '0';
9707 ++s;
9708 }
9709 }
9710 if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
9711 {
9712 mask &= ~ (7 << 3);
9713 mask |= 5 << 3;
9714 }
9715 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
9716 {
9717 mask &= ~ (7 << 3);
9718 mask |= 6 << 3;
9719 }
9720 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
9721 mask |= (reg2 - 3) << 3;
9722 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
9723 mask |= (reg2 - 15) << 1;
f9419b05 9724 else if (reg1 == RA && reg2 == RA)
252b5132
RH
9725 mask |= 1;
9726 else
9727 {
9728 as_bad (_("invalid register list"));
9729 break;
9730 }
9731 }
9732 /* The mask is filled in in the opcode table for the
9733 benefit of the disassembler. We remove it before
9734 applying the actual mask. */
9735 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
9736 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
9737 }
9738 continue;
9739
9740 case 'e': /* extend code */
9741 my_getExpression (&imm_expr, s);
9742 check_absolute_expr (ip, &imm_expr);
9743 if ((unsigned long) imm_expr.X_add_number > 0x7ff)
9744 {
9745 as_warn (_("Invalid value for `%s' (%lu)"),
9746 ip->insn_mo->name,
9747 (unsigned long) imm_expr.X_add_number);
9748 imm_expr.X_add_number &= 0x7ff;
9749 }
9750 ip->insn_opcode |= imm_expr.X_add_number;
9751 imm_expr.X_op = O_absent;
9752 s = expr_end;
9753 continue;
9754
9755 default:
9756 internalError ();
9757 }
9758 break;
9759 }
9760
9761 /* Args don't match. */
9762 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
9763 strcmp (insn->name, insn[1].name) == 0)
9764 {
9765 ++insn;
9766 s = argsstart;
9767 continue;
9768 }
9769
9770 insn_error = _("illegal operands");
9771
9772 return;
9773 }
9774}
9775
9776/* This structure holds information we know about a mips16 immediate
9777 argument type. */
9778
e972090a
NC
9779struct mips16_immed_operand
9780{
252b5132
RH
9781 /* The type code used in the argument string in the opcode table. */
9782 int type;
9783 /* The number of bits in the short form of the opcode. */
9784 int nbits;
9785 /* The number of bits in the extended form of the opcode. */
9786 int extbits;
9787 /* The amount by which the short form is shifted when it is used;
9788 for example, the sw instruction has a shift count of 2. */
9789 int shift;
9790 /* The amount by which the short form is shifted when it is stored
9791 into the instruction code. */
9792 int op_shift;
9793 /* Non-zero if the short form is unsigned. */
9794 int unsp;
9795 /* Non-zero if the extended form is unsigned. */
9796 int extu;
9797 /* Non-zero if the value is PC relative. */
9798 int pcrel;
9799};
9800
9801/* The mips16 immediate operand types. */
9802
9803static const struct mips16_immed_operand mips16_immed_operands[] =
9804{
9805 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9806 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9807 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9808 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9809 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
9810 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
9811 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
9812 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
9813 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
9814 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
9815 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
9816 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
9817 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
9818 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
9819 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
9820 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
9821 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9822 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9823 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
9824 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
9825 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
9826};
9827
9828#define MIPS16_NUM_IMMED \
9829 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
9830
9831/* Handle a mips16 instruction with an immediate value. This or's the
9832 small immediate value into *INSN. It sets *USE_EXTEND to indicate
9833 whether an extended value is needed; if one is needed, it sets
9834 *EXTEND to the value. The argument type is TYPE. The value is VAL.
9835 If SMALL is true, an unextended opcode was explicitly requested.
9836 If EXT is true, an extended opcode was explicitly requested. If
9837 WARN is true, warn if EXT does not match reality. */
9838
9839static void
9840mips16_immed (file, line, type, val, warn, small, ext, insn, use_extend,
9841 extend)
9842 char *file;
9843 unsigned int line;
9844 int type;
9845 offsetT val;
b34976b6
AM
9846 bfd_boolean warn;
9847 bfd_boolean small;
9848 bfd_boolean ext;
252b5132 9849 unsigned long *insn;
b34976b6 9850 bfd_boolean *use_extend;
252b5132
RH
9851 unsigned short *extend;
9852{
9853 register const struct mips16_immed_operand *op;
9854 int mintiny, maxtiny;
b34976b6 9855 bfd_boolean needext;
252b5132
RH
9856
9857 op = mips16_immed_operands;
9858 while (op->type != type)
9859 {
9860 ++op;
9861 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
9862 }
9863
9864 if (op->unsp)
9865 {
9866 if (type == '<' || type == '>' || type == '[' || type == ']')
9867 {
9868 mintiny = 1;
9869 maxtiny = 1 << op->nbits;
9870 }
9871 else
9872 {
9873 mintiny = 0;
9874 maxtiny = (1 << op->nbits) - 1;
9875 }
9876 }
9877 else
9878 {
9879 mintiny = - (1 << (op->nbits - 1));
9880 maxtiny = (1 << (op->nbits - 1)) - 1;
9881 }
9882
9883 /* Branch offsets have an implicit 0 in the lowest bit. */
9884 if (type == 'p' || type == 'q')
9885 val /= 2;
9886
9887 if ((val & ((1 << op->shift) - 1)) != 0
9888 || val < (mintiny << op->shift)
9889 || val > (maxtiny << op->shift))
b34976b6 9890 needext = TRUE;
252b5132 9891 else
b34976b6 9892 needext = FALSE;
252b5132
RH
9893
9894 if (warn && ext && ! needext)
beae10d5
KH
9895 as_warn_where (file, line,
9896 _("extended operand requested but not required"));
252b5132
RH
9897 if (small && needext)
9898 as_bad_where (file, line, _("invalid unextended operand value"));
9899
9900 if (small || (! ext && ! needext))
9901 {
9902 int insnval;
9903
b34976b6 9904 *use_extend = FALSE;
252b5132
RH
9905 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
9906 insnval <<= op->op_shift;
9907 *insn |= insnval;
9908 }
9909 else
9910 {
9911 long minext, maxext;
9912 int extval;
9913
9914 if (op->extu)
9915 {
9916 minext = 0;
9917 maxext = (1 << op->extbits) - 1;
9918 }
9919 else
9920 {
9921 minext = - (1 << (op->extbits - 1));
9922 maxext = (1 << (op->extbits - 1)) - 1;
9923 }
9924 if (val < minext || val > maxext)
9925 as_bad_where (file, line,
9926 _("operand value out of range for instruction"));
9927
b34976b6 9928 *use_extend = TRUE;
252b5132
RH
9929 if (op->extbits == 16)
9930 {
9931 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
9932 val &= 0x1f;
9933 }
9934 else if (op->extbits == 15)
9935 {
9936 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
9937 val &= 0xf;
9938 }
9939 else
9940 {
9941 extval = ((val & 0x1f) << 6) | (val & 0x20);
9942 val = 0;
9943 }
9944
9945 *extend = (unsigned short) extval;
9946 *insn |= val;
9947 }
9948}
9949\f
5e0116d5 9950static const struct percent_op_match
ad8d3bb3 9951{
5e0116d5
RS
9952 const char *str;
9953 bfd_reloc_code_real_type reloc;
ad8d3bb3
TS
9954} percent_op[] =
9955{
5e0116d5 9956 {"%lo", BFD_RELOC_LO16},
ad8d3bb3 9957#ifdef OBJ_ELF
5e0116d5
RS
9958 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
9959 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
9960 {"%call16", BFD_RELOC_MIPS_CALL16},
9961 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
9962 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
9963 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
9964 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
9965 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
9966 {"%got", BFD_RELOC_MIPS_GOT16},
9967 {"%gp_rel", BFD_RELOC_GPREL16},
9968 {"%half", BFD_RELOC_16},
9969 {"%highest", BFD_RELOC_MIPS_HIGHEST},
9970 {"%higher", BFD_RELOC_MIPS_HIGHER},
9971 {"%neg", BFD_RELOC_MIPS_SUB},
ad8d3bb3 9972#endif
5e0116d5 9973 {"%hi", BFD_RELOC_HI16_S}
ad8d3bb3
TS
9974};
9975
252b5132 9976
5e0116d5
RS
9977/* Return true if *STR points to a relocation operator. When returning true,
9978 move *STR over the operator and store its relocation code in *RELOC.
9979 Leave both *STR and *RELOC alone when returning false. */
9980
9981static bfd_boolean
9982parse_relocation (str, reloc)
ad8d3bb3 9983 char **str;
5e0116d5 9984 bfd_reloc_code_real_type *reloc;
252b5132 9985{
5e0116d5 9986 size_t i;
76b3015f 9987
5e0116d5
RS
9988 for (i = 0; i < ARRAY_SIZE (percent_op); i++)
9989 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
394f9b3a 9990 {
5e0116d5
RS
9991 *str += strlen (percent_op[i].str);
9992 *reloc = percent_op[i].reloc;
394f9b3a 9993
5e0116d5
RS
9994 /* Check whether the output BFD supports this relocation.
9995 If not, issue an error and fall back on something safe. */
9996 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
394f9b3a 9997 {
5e0116d5
RS
9998 as_bad ("relocation %s isn't supported by the current ABI",
9999 percent_op[i].str);
10000 *reloc = BFD_RELOC_LO16;
394f9b3a 10001 }
5e0116d5 10002 return TRUE;
394f9b3a 10003 }
5e0116d5 10004 return FALSE;
394f9b3a 10005}
ad8d3bb3 10006
ad8d3bb3 10007
5e0116d5
RS
10008/* Parse string STR as a 16-bit relocatable operand. Store the
10009 expression in *EP and the relocations in the array starting
10010 at RELOC. Return the number of relocation operators used.
ad8d3bb3 10011
5e0116d5
RS
10012 On exit, EXPR_END points to the first character after the expression.
10013 If no relocation operators are used, RELOC[0] is set to BFD_RELOC_LO16. */
ad8d3bb3 10014
5e0116d5
RS
10015static size_t
10016my_getSmallExpression (ep, reloc, str)
ad8d3bb3 10017 expressionS *ep;
5e0116d5 10018 bfd_reloc_code_real_type *reloc;
ad8d3bb3
TS
10019 char *str;
10020{
5e0116d5
RS
10021 bfd_reloc_code_real_type reversed_reloc[3];
10022 size_t reloc_index, i;
09b8f35a
RS
10023 int crux_depth, str_depth;
10024 char *crux;
5e0116d5
RS
10025
10026 /* Search for the start of the main expression, recoding relocations
09b8f35a
RS
10027 in REVERSED_RELOC. End the loop with CRUX pointing to the start
10028 of the main expression and with CRUX_DEPTH containing the number
10029 of open brackets at that point. */
10030 reloc_index = -1;
10031 str_depth = 0;
10032 do
fb1b3232 10033 {
09b8f35a
RS
10034 reloc_index++;
10035 crux = str;
10036 crux_depth = str_depth;
10037
10038 /* Skip over whitespace and brackets, keeping count of the number
10039 of brackets. */
10040 while (*str == ' ' || *str == '\t' || *str == '(')
10041 if (*str++ == '(')
10042 str_depth++;
5e0116d5 10043 }
09b8f35a
RS
10044 while (*str == '%'
10045 && reloc_index < (HAVE_NEWABI ? 3 : 1)
10046 && parse_relocation (&str, &reversed_reloc[reloc_index]));
ad8d3bb3 10047
09b8f35a 10048 my_getExpression (ep, crux);
5e0116d5 10049 str = expr_end;
394f9b3a 10050
5e0116d5 10051 /* Match every open bracket. */
09b8f35a 10052 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
5e0116d5 10053 if (*str++ == ')')
09b8f35a 10054 crux_depth--;
394f9b3a 10055
09b8f35a 10056 if (crux_depth > 0)
5e0116d5 10057 as_bad ("unclosed '('");
394f9b3a 10058
5e0116d5 10059 expr_end = str;
252b5132 10060
5e0116d5
RS
10061 reloc[0] = BFD_RELOC_LO16;
10062 for (i = 0; i < reloc_index; i++)
10063 reloc[i] = reversed_reloc[reloc_index - 1 - i];
fb1b3232 10064
5e0116d5 10065 return reloc_index;
252b5132
RH
10066}
10067
10068static void
10069my_getExpression (ep, str)
10070 expressionS *ep;
10071 char *str;
10072{
10073 char *save_in;
98aa84af 10074 valueT val;
252b5132
RH
10075
10076 save_in = input_line_pointer;
10077 input_line_pointer = str;
10078 expression (ep);
10079 expr_end = input_line_pointer;
10080 input_line_pointer = save_in;
10081
10082 /* If we are in mips16 mode, and this is an expression based on `.',
10083 then we bump the value of the symbol by 1 since that is how other
10084 text symbols are handled. We don't bother to handle complex
10085 expressions, just `.' plus or minus a constant. */
10086 if (mips_opts.mips16
10087 && ep->X_op == O_symbol
10088 && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
10089 && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
49309057
ILT
10090 && symbol_get_frag (ep->X_add_symbol) == frag_now
10091 && symbol_constant_p (ep->X_add_symbol)
98aa84af
AM
10092 && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
10093 S_SET_VALUE (ep->X_add_symbol, val + 1);
252b5132
RH
10094}
10095
10096/* Turn a string in input_line_pointer into a floating point constant
bc0d738a
NC
10097 of type TYPE, and store the appropriate bytes in *LITP. The number
10098 of LITTLENUMS emitted is stored in *SIZEP. An error message is
252b5132
RH
10099 returned, or NULL on OK. */
10100
10101char *
10102md_atof (type, litP, sizeP)
10103 int type;
10104 char *litP;
10105 int *sizeP;
10106{
10107 int prec;
10108 LITTLENUM_TYPE words[4];
10109 char *t;
10110 int i;
10111
10112 switch (type)
10113 {
10114 case 'f':
10115 prec = 2;
10116 break;
10117
10118 case 'd':
10119 prec = 4;
10120 break;
10121
10122 default:
10123 *sizeP = 0;
10124 return _("bad call to md_atof");
10125 }
10126
10127 t = atof_ieee (input_line_pointer, type, words);
10128 if (t)
10129 input_line_pointer = t;
10130
10131 *sizeP = prec * 2;
10132
10133 if (! target_big_endian)
10134 {
10135 for (i = prec - 1; i >= 0; i--)
10136 {
10137 md_number_to_chars (litP, (valueT) words[i], 2);
10138 litP += 2;
10139 }
10140 }
10141 else
10142 {
10143 for (i = 0; i < prec; i++)
10144 {
10145 md_number_to_chars (litP, (valueT) words[i], 2);
10146 litP += 2;
10147 }
10148 }
bdaaa2e1 10149
252b5132
RH
10150 return NULL;
10151}
10152
10153void
10154md_number_to_chars (buf, val, n)
10155 char *buf;
10156 valueT val;
10157 int n;
10158{
10159 if (target_big_endian)
10160 number_to_chars_bigendian (buf, val, n);
10161 else
10162 number_to_chars_littleendian (buf, val, n);
10163}
10164\f
ae948b86 10165#ifdef OBJ_ELF
e013f690
TS
10166static int support_64bit_objects(void)
10167{
10168 const char **list, **l;
aa3d8fdf 10169 int yes;
e013f690
TS
10170
10171 list = bfd_target_list ();
10172 for (l = list; *l != NULL; l++)
10173#ifdef TE_TMIPS
10174 /* This is traditional mips */
10175 if (strcmp (*l, "elf64-tradbigmips") == 0
10176 || strcmp (*l, "elf64-tradlittlemips") == 0)
10177#else
10178 if (strcmp (*l, "elf64-bigmips") == 0
10179 || strcmp (*l, "elf64-littlemips") == 0)
10180#endif
10181 break;
aa3d8fdf 10182 yes = (*l != NULL);
e013f690 10183 free (list);
aa3d8fdf 10184 return yes;
e013f690 10185}
ae948b86 10186#endif /* OBJ_ELF */
e013f690 10187
5a38dc70 10188const char *md_shortopts = "nO::g::G:";
252b5132 10189
e972090a
NC
10190struct option md_longopts[] =
10191{
252b5132
RH
10192#define OPTION_MIPS1 (OPTION_MD_BASE + 1)
10193 {"mips0", no_argument, NULL, OPTION_MIPS1},
10194 {"mips1", no_argument, NULL, OPTION_MIPS1},
10195#define OPTION_MIPS2 (OPTION_MD_BASE + 2)
10196 {"mips2", no_argument, NULL, OPTION_MIPS2},
10197#define OPTION_MIPS3 (OPTION_MD_BASE + 3)
10198 {"mips3", no_argument, NULL, OPTION_MIPS3},
10199#define OPTION_MIPS4 (OPTION_MD_BASE + 4)
10200 {"mips4", no_argument, NULL, OPTION_MIPS4},
ae948b86
TS
10201#define OPTION_MIPS5 (OPTION_MD_BASE + 5)
10202 {"mips5", no_argument, NULL, OPTION_MIPS5},
10203#define OPTION_MIPS32 (OPTION_MD_BASE + 6)
10204 {"mips32", no_argument, NULL, OPTION_MIPS32},
10205#define OPTION_MIPS64 (OPTION_MD_BASE + 7)
10206 {"mips64", no_argument, NULL, OPTION_MIPS64},
10207#define OPTION_MEMBEDDED_PIC (OPTION_MD_BASE + 8)
252b5132 10208 {"membedded-pic", no_argument, NULL, OPTION_MEMBEDDED_PIC},
ae948b86 10209#define OPTION_TRAP (OPTION_MD_BASE + 9)
252b5132
RH
10210 {"trap", no_argument, NULL, OPTION_TRAP},
10211 {"no-break", no_argument, NULL, OPTION_TRAP},
ae948b86 10212#define OPTION_BREAK (OPTION_MD_BASE + 10)
252b5132
RH
10213 {"break", no_argument, NULL, OPTION_BREAK},
10214 {"no-trap", no_argument, NULL, OPTION_BREAK},
ae948b86 10215#define OPTION_EB (OPTION_MD_BASE + 11)
252b5132 10216 {"EB", no_argument, NULL, OPTION_EB},
ae948b86 10217#define OPTION_EL (OPTION_MD_BASE + 12)
252b5132 10218 {"EL", no_argument, NULL, OPTION_EL},
ae948b86 10219#define OPTION_MIPS16 (OPTION_MD_BASE + 13)
252b5132 10220 {"mips16", no_argument, NULL, OPTION_MIPS16},
ae948b86 10221#define OPTION_NO_MIPS16 (OPTION_MD_BASE + 14)
252b5132 10222 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
ae948b86 10223#define OPTION_M7000_HILO_FIX (OPTION_MD_BASE + 15)
6b76fefe 10224 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
9ee72ff1
TS
10225#define OPTION_MNO_7000_HILO_FIX (OPTION_MD_BASE + 16)
10226 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10227 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
ae948b86
TS
10228#define OPTION_FP32 (OPTION_MD_BASE + 17)
10229 {"mfp32", no_argument, NULL, OPTION_FP32},
10230#define OPTION_GP32 (OPTION_MD_BASE + 18)
c97ef257 10231 {"mgp32", no_argument, NULL, OPTION_GP32},
ae948b86 10232#define OPTION_CONSTRUCT_FLOATS (OPTION_MD_BASE + 19)
119d663a 10233 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
ae948b86 10234#define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MD_BASE + 20)
119d663a 10235 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
ae948b86 10236#define OPTION_MARCH (OPTION_MD_BASE + 21)
ec68c924 10237 {"march", required_argument, NULL, OPTION_MARCH},
ae948b86 10238#define OPTION_MTUNE (OPTION_MD_BASE + 22)
ec68c924 10239 {"mtune", required_argument, NULL, OPTION_MTUNE},
316f5878
RS
10240#define OPTION_FP64 (OPTION_MD_BASE + 23)
10241 {"mfp64", no_argument, NULL, OPTION_FP64},
ae948b86
TS
10242#define OPTION_M4650 (OPTION_MD_BASE + 24)
10243 {"m4650", no_argument, NULL, OPTION_M4650},
10244#define OPTION_NO_M4650 (OPTION_MD_BASE + 25)
10245 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
10246#define OPTION_M4010 (OPTION_MD_BASE + 26)
10247 {"m4010", no_argument, NULL, OPTION_M4010},
10248#define OPTION_NO_M4010 (OPTION_MD_BASE + 27)
10249 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
10250#define OPTION_M4100 (OPTION_MD_BASE + 28)
10251 {"m4100", no_argument, NULL, OPTION_M4100},
10252#define OPTION_NO_M4100 (OPTION_MD_BASE + 29)
10253 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
10254#define OPTION_M3900 (OPTION_MD_BASE + 30)
10255 {"m3900", no_argument, NULL, OPTION_M3900},
10256#define OPTION_NO_M3900 (OPTION_MD_BASE + 31)
10257 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
10258#define OPTION_GP64 (OPTION_MD_BASE + 32)
10259 {"mgp64", no_argument, NULL, OPTION_GP64},
1f25f5d3
CD
10260#define OPTION_MIPS3D (OPTION_MD_BASE + 33)
10261 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
10262#define OPTION_NO_MIPS3D (OPTION_MD_BASE + 34)
10263 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
deec1734
CD
10264#define OPTION_MDMX (OPTION_MD_BASE + 35)
10265 {"mdmx", no_argument, NULL, OPTION_MDMX},
10266#define OPTION_NO_MDMX (OPTION_MD_BASE + 36)
10267 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
60b63b72
RS
10268#define OPTION_FIX_VR4122 (OPTION_MD_BASE + 37)
10269#define OPTION_NO_FIX_VR4122 (OPTION_MD_BASE + 38)
10270 {"mfix-vr4122-bugs", no_argument, NULL, OPTION_FIX_VR4122},
10271 {"no-mfix-vr4122-bugs", no_argument, NULL, OPTION_NO_FIX_VR4122},
4a6a3df4
AO
10272#define OPTION_RELAX_BRANCH (OPTION_MD_BASE + 39)
10273#define OPTION_NO_RELAX_BRANCH (OPTION_MD_BASE + 40)
10274 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
10275 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
af7ee8bf
CD
10276#define OPTION_MIPS32R2 (OPTION_MD_BASE + 41)
10277 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
156c2f8b 10278#ifdef OBJ_ELF
af7ee8bf 10279#define OPTION_ELF_BASE (OPTION_MD_BASE + 42)
156c2f8b 10280#define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
156c2f8b
NC
10281 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
10282 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
ae948b86 10283#define OPTION_NON_SHARED (OPTION_ELF_BASE + 1)
156c2f8b 10284 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
ae948b86 10285#define OPTION_XGOT (OPTION_ELF_BASE + 2)
156c2f8b 10286 {"xgot", no_argument, NULL, OPTION_XGOT},
ae948b86
TS
10287#define OPTION_MABI (OPTION_ELF_BASE + 3)
10288 {"mabi", required_argument, NULL, OPTION_MABI},
10289#define OPTION_32 (OPTION_ELF_BASE + 4)
156c2f8b 10290 {"32", no_argument, NULL, OPTION_32},
ae948b86 10291#define OPTION_N32 (OPTION_ELF_BASE + 5)
e013f690 10292 {"n32", no_argument, NULL, OPTION_N32},
ae948b86 10293#define OPTION_64 (OPTION_ELF_BASE + 6)
156c2f8b 10294 {"64", no_argument, NULL, OPTION_64},
ecb4347a
DJ
10295#define OPTION_MDEBUG (OPTION_ELF_BASE + 7)
10296 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
10297#define OPTION_NO_MDEBUG (OPTION_ELF_BASE + 8)
10298 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
ae948b86 10299#endif /* OBJ_ELF */
252b5132
RH
10300 {NULL, no_argument, NULL, 0}
10301};
156c2f8b 10302size_t md_longopts_size = sizeof (md_longopts);
252b5132 10303
316f5878
RS
10304/* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
10305 NEW_VALUE. Warn if another value was already specified. Note:
10306 we have to defer parsing the -march and -mtune arguments in order
10307 to handle 'from-abi' correctly, since the ABI might be specified
10308 in a later argument. */
10309
10310static void
10311mips_set_option_string (string_ptr, new_value)
10312 const char **string_ptr, *new_value;
10313{
10314 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
10315 as_warn (_("A different %s was already specified, is now %s"),
10316 string_ptr == &mips_arch_string ? "-march" : "-mtune",
10317 new_value);
10318
10319 *string_ptr = new_value;
10320}
10321
252b5132
RH
10322int
10323md_parse_option (c, arg)
10324 int c;
10325 char *arg;
10326{
10327 switch (c)
10328 {
119d663a
NC
10329 case OPTION_CONSTRUCT_FLOATS:
10330 mips_disable_float_construction = 0;
10331 break;
bdaaa2e1 10332
119d663a
NC
10333 case OPTION_NO_CONSTRUCT_FLOATS:
10334 mips_disable_float_construction = 1;
10335 break;
bdaaa2e1 10336
252b5132
RH
10337 case OPTION_TRAP:
10338 mips_trap = 1;
10339 break;
10340
10341 case OPTION_BREAK:
10342 mips_trap = 0;
10343 break;
10344
10345 case OPTION_EB:
10346 target_big_endian = 1;
10347 break;
10348
10349 case OPTION_EL:
10350 target_big_endian = 0;
10351 break;
10352
39c0a331
L
10353 case 'n':
10354 warn_nops = 1;
10355 break;
10356
252b5132
RH
10357 case 'O':
10358 if (arg && arg[1] == '0')
10359 mips_optimize = 1;
10360 else
10361 mips_optimize = 2;
10362 break;
10363
10364 case 'g':
10365 if (arg == NULL)
10366 mips_debug = 2;
10367 else
10368 mips_debug = atoi (arg);
10369 /* When the MIPS assembler sees -g or -g2, it does not do
10370 optimizations which limit full symbolic debugging. We take
10371 that to be equivalent to -O0. */
10372 if (mips_debug == 2)
10373 mips_optimize = 1;
10374 break;
10375
10376 case OPTION_MIPS1:
316f5878 10377 file_mips_isa = ISA_MIPS1;
252b5132
RH
10378 break;
10379
10380 case OPTION_MIPS2:
316f5878 10381 file_mips_isa = ISA_MIPS2;
252b5132
RH
10382 break;
10383
10384 case OPTION_MIPS3:
316f5878 10385 file_mips_isa = ISA_MIPS3;
252b5132
RH
10386 break;
10387
10388 case OPTION_MIPS4:
316f5878 10389 file_mips_isa = ISA_MIPS4;
e7af610e
NC
10390 break;
10391
84ea6cf2 10392 case OPTION_MIPS5:
316f5878 10393 file_mips_isa = ISA_MIPS5;
84ea6cf2
NC
10394 break;
10395
e7af610e 10396 case OPTION_MIPS32:
316f5878 10397 file_mips_isa = ISA_MIPS32;
252b5132
RH
10398 break;
10399
af7ee8bf
CD
10400 case OPTION_MIPS32R2:
10401 file_mips_isa = ISA_MIPS32R2;
10402 break;
10403
84ea6cf2 10404 case OPTION_MIPS64:
316f5878 10405 file_mips_isa = ISA_MIPS64;
84ea6cf2
NC
10406 break;
10407
ec68c924 10408 case OPTION_MTUNE:
316f5878
RS
10409 mips_set_option_string (&mips_tune_string, arg);
10410 break;
ec68c924 10411
316f5878
RS
10412 case OPTION_MARCH:
10413 mips_set_option_string (&mips_arch_string, arg);
252b5132
RH
10414 break;
10415
10416 case OPTION_M4650:
316f5878
RS
10417 mips_set_option_string (&mips_arch_string, "4650");
10418 mips_set_option_string (&mips_tune_string, "4650");
252b5132
RH
10419 break;
10420
10421 case OPTION_NO_M4650:
10422 break;
10423
10424 case OPTION_M4010:
316f5878
RS
10425 mips_set_option_string (&mips_arch_string, "4010");
10426 mips_set_option_string (&mips_tune_string, "4010");
252b5132
RH
10427 break;
10428
10429 case OPTION_NO_M4010:
10430 break;
10431
10432 case OPTION_M4100:
316f5878
RS
10433 mips_set_option_string (&mips_arch_string, "4100");
10434 mips_set_option_string (&mips_tune_string, "4100");
252b5132
RH
10435 break;
10436
10437 case OPTION_NO_M4100:
10438 break;
10439
252b5132 10440 case OPTION_M3900:
316f5878
RS
10441 mips_set_option_string (&mips_arch_string, "3900");
10442 mips_set_option_string (&mips_tune_string, "3900");
252b5132 10443 break;
bdaaa2e1 10444
252b5132
RH
10445 case OPTION_NO_M3900:
10446 break;
10447
deec1734
CD
10448 case OPTION_MDMX:
10449 mips_opts.ase_mdmx = 1;
10450 break;
10451
10452 case OPTION_NO_MDMX:
10453 mips_opts.ase_mdmx = 0;
10454 break;
10455
252b5132
RH
10456 case OPTION_MIPS16:
10457 mips_opts.mips16 = 1;
b34976b6 10458 mips_no_prev_insn (FALSE);
252b5132
RH
10459 break;
10460
10461 case OPTION_NO_MIPS16:
10462 mips_opts.mips16 = 0;
b34976b6 10463 mips_no_prev_insn (FALSE);
252b5132
RH
10464 break;
10465
1f25f5d3
CD
10466 case OPTION_MIPS3D:
10467 mips_opts.ase_mips3d = 1;
10468 break;
10469
10470 case OPTION_NO_MIPS3D:
10471 mips_opts.ase_mips3d = 0;
10472 break;
10473
252b5132
RH
10474 case OPTION_MEMBEDDED_PIC:
10475 mips_pic = EMBEDDED_PIC;
10476 if (USE_GLOBAL_POINTER_OPT && g_switch_seen)
10477 {
10478 as_bad (_("-G may not be used with embedded PIC code"));
10479 return 0;
10480 }
10481 g_switch_value = 0x7fffffff;
10482 break;
10483
60b63b72
RS
10484 case OPTION_FIX_VR4122:
10485 mips_fix_4122_bugs = 1;
10486 break;
10487
10488 case OPTION_NO_FIX_VR4122:
10489 mips_fix_4122_bugs = 0;
10490 break;
10491
4a6a3df4
AO
10492 case OPTION_RELAX_BRANCH:
10493 mips_relax_branch = 1;
10494 break;
10495
10496 case OPTION_NO_RELAX_BRANCH:
10497 mips_relax_branch = 0;
10498 break;
10499
0f074f60 10500#ifdef OBJ_ELF
252b5132
RH
10501 /* When generating ELF code, we permit -KPIC and -call_shared to
10502 select SVR4_PIC, and -non_shared to select no PIC. This is
10503 intended to be compatible with Irix 5. */
10504 case OPTION_CALL_SHARED:
10505 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10506 {
10507 as_bad (_("-call_shared is supported only for ELF format"));
10508 return 0;
10509 }
10510 mips_pic = SVR4_PIC;
10511 if (g_switch_seen && g_switch_value != 0)
10512 {
10513 as_bad (_("-G may not be used with SVR4 PIC code"));
10514 return 0;
10515 }
10516 g_switch_value = 0;
10517 break;
10518
10519 case OPTION_NON_SHARED:
10520 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10521 {
10522 as_bad (_("-non_shared is supported only for ELF format"));
10523 return 0;
10524 }
10525 mips_pic = NO_PIC;
10526 break;
10527
10528 /* The -xgot option tells the assembler to use 32 offsets when
10529 accessing the got in SVR4_PIC mode. It is for Irix
10530 compatibility. */
10531 case OPTION_XGOT:
10532 mips_big_got = 1;
10533 break;
0f074f60 10534#endif /* OBJ_ELF */
252b5132
RH
10535
10536 case 'G':
10537 if (! USE_GLOBAL_POINTER_OPT)
10538 {
10539 as_bad (_("-G is not supported for this configuration"));
10540 return 0;
10541 }
10542 else if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
10543 {
10544 as_bad (_("-G may not be used with SVR4 or embedded PIC code"));
10545 return 0;
10546 }
10547 else
10548 g_switch_value = atoi (arg);
10549 g_switch_seen = 1;
10550 break;
10551
0f074f60 10552#ifdef OBJ_ELF
34ba82a8
TS
10553 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
10554 and -mabi=64. */
252b5132 10555 case OPTION_32:
34ba82a8
TS
10556 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10557 {
10558 as_bad (_("-32 is supported for ELF format only"));
10559 return 0;
10560 }
316f5878 10561 mips_abi = O32_ABI;
252b5132
RH
10562 break;
10563
e013f690 10564 case OPTION_N32:
34ba82a8
TS
10565 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10566 {
10567 as_bad (_("-n32 is supported for ELF format only"));
10568 return 0;
10569 }
316f5878 10570 mips_abi = N32_ABI;
e013f690 10571 break;
252b5132 10572
e013f690 10573 case OPTION_64:
34ba82a8
TS
10574 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10575 {
10576 as_bad (_("-64 is supported for ELF format only"));
10577 return 0;
10578 }
316f5878 10579 mips_abi = N64_ABI;
e013f690
TS
10580 if (! support_64bit_objects())
10581 as_fatal (_("No compiled in support for 64 bit object file format"));
252b5132 10582 break;
ae948b86 10583#endif /* OBJ_ELF */
252b5132 10584
c97ef257 10585 case OPTION_GP32:
a325df1d 10586 file_mips_gp32 = 1;
c97ef257
AH
10587 break;
10588
10589 case OPTION_GP64:
a325df1d 10590 file_mips_gp32 = 0;
c97ef257 10591 break;
252b5132 10592
ca4e0257 10593 case OPTION_FP32:
a325df1d 10594 file_mips_fp32 = 1;
316f5878
RS
10595 break;
10596
10597 case OPTION_FP64:
10598 file_mips_fp32 = 0;
ca4e0257
RS
10599 break;
10600
ae948b86 10601#ifdef OBJ_ELF
252b5132 10602 case OPTION_MABI:
34ba82a8
TS
10603 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10604 {
10605 as_bad (_("-mabi is supported for ELF format only"));
10606 return 0;
10607 }
e013f690 10608 if (strcmp (arg, "32") == 0)
316f5878 10609 mips_abi = O32_ABI;
e013f690 10610 else if (strcmp (arg, "o64") == 0)
316f5878 10611 mips_abi = O64_ABI;
e013f690 10612 else if (strcmp (arg, "n32") == 0)
316f5878 10613 mips_abi = N32_ABI;
e013f690
TS
10614 else if (strcmp (arg, "64") == 0)
10615 {
316f5878 10616 mips_abi = N64_ABI;
e013f690
TS
10617 if (! support_64bit_objects())
10618 as_fatal (_("No compiled in support for 64 bit object file "
10619 "format"));
10620 }
10621 else if (strcmp (arg, "eabi") == 0)
316f5878 10622 mips_abi = EABI_ABI;
e013f690 10623 else
da0e507f
TS
10624 {
10625 as_fatal (_("invalid abi -mabi=%s"), arg);
10626 return 0;
10627 }
252b5132 10628 break;
e013f690 10629#endif /* OBJ_ELF */
252b5132 10630
6b76fefe 10631 case OPTION_M7000_HILO_FIX:
b34976b6 10632 mips_7000_hilo_fix = TRUE;
6b76fefe
CM
10633 break;
10634
9ee72ff1 10635 case OPTION_MNO_7000_HILO_FIX:
b34976b6 10636 mips_7000_hilo_fix = FALSE;
6b76fefe
CM
10637 break;
10638
ecb4347a
DJ
10639#ifdef OBJ_ELF
10640 case OPTION_MDEBUG:
b34976b6 10641 mips_flag_mdebug = TRUE;
ecb4347a
DJ
10642 break;
10643
10644 case OPTION_NO_MDEBUG:
b34976b6 10645 mips_flag_mdebug = FALSE;
ecb4347a
DJ
10646 break;
10647#endif /* OBJ_ELF */
10648
252b5132
RH
10649 default:
10650 return 0;
10651 }
10652
10653 return 1;
10654}
316f5878
RS
10655\f
10656/* Set up globals to generate code for the ISA or processor
10657 described by INFO. */
252b5132 10658
252b5132 10659static void
316f5878
RS
10660mips_set_architecture (info)
10661 const struct mips_cpu_info *info;
252b5132 10662{
316f5878 10663 if (info != 0)
252b5132 10664 {
316f5878
RS
10665 mips_arch_info = info;
10666 mips_arch = info->cpu;
10667 mips_opts.isa = info->isa;
252b5132 10668 }
252b5132
RH
10669}
10670
252b5132 10671
316f5878 10672/* Likewise for tuning. */
252b5132 10673
316f5878
RS
10674static void
10675mips_set_tune (info)
10676 const struct mips_cpu_info *info;
10677{
10678 if (info != 0)
10679 {
10680 mips_tune_info = info;
10681 mips_tune = info->cpu;
10682 }
10683}
80cc45a5 10684
34ba82a8 10685
252b5132 10686void
e9670677
MR
10687mips_after_parse_args ()
10688{
e9670677
MR
10689 /* GP relative stuff not working for PE */
10690 if (strncmp (TARGET_OS, "pe", 2) == 0
10691 && g_switch_value != 0)
10692 {
10693 if (g_switch_seen)
10694 as_bad (_("-G not supported in this configuration."));
10695 g_switch_value = 0;
10696 }
10697
22923709
RS
10698 /* The following code determines the architecture and register size.
10699 Similar code was added to GCC 3.3 (see override_options() in
10700 config/mips/mips.c). The GAS and GCC code should be kept in sync
10701 as much as possible. */
e9670677 10702
316f5878
RS
10703 if (mips_arch_string != 0)
10704 mips_set_architecture (mips_parse_cpu ("-march", mips_arch_string));
e9670677 10705
316f5878
RS
10706 if (mips_tune_string != 0)
10707 mips_set_tune (mips_parse_cpu ("-mtune", mips_tune_string));
e9670677 10708
316f5878 10709 if (file_mips_isa != ISA_UNKNOWN)
e9670677 10710 {
316f5878
RS
10711 /* Handle -mipsN. At this point, file_mips_isa contains the
10712 ISA level specified by -mipsN, while mips_opts.isa contains
10713 the -march selection (if any). */
10714 if (mips_arch_info != 0)
e9670677 10715 {
316f5878
RS
10716 /* -march takes precedence over -mipsN, since it is more descriptive.
10717 There's no harm in specifying both as long as the ISA levels
10718 are the same. */
10719 if (file_mips_isa != mips_opts.isa)
10720 as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
10721 mips_cpu_info_from_isa (file_mips_isa)->name,
10722 mips_cpu_info_from_isa (mips_opts.isa)->name);
e9670677 10723 }
316f5878
RS
10724 else
10725 mips_set_architecture (mips_cpu_info_from_isa (file_mips_isa));
e9670677
MR
10726 }
10727
316f5878
RS
10728 if (mips_arch_info == 0)
10729 mips_set_architecture (mips_parse_cpu ("default CPU",
10730 MIPS_CPU_STRING_DEFAULT));
e9670677 10731
316f5878
RS
10732 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (mips_opts.isa))
10733 as_bad ("-march=%s is not compatible with the selected ABI",
10734 mips_arch_info->name);
e9670677 10735
316f5878
RS
10736 /* Optimize for mips_arch, unless -mtune selects a different processor. */
10737 if (mips_tune_info == 0)
10738 mips_set_tune (mips_arch_info);
e9670677 10739
316f5878 10740 if (file_mips_gp32 >= 0)
e9670677 10741 {
316f5878
RS
10742 /* The user specified the size of the integer registers. Make sure
10743 it agrees with the ABI and ISA. */
10744 if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
10745 as_bad (_("-mgp64 used with a 32-bit processor"));
10746 else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
10747 as_bad (_("-mgp32 used with a 64-bit ABI"));
10748 else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
10749 as_bad (_("-mgp64 used with a 32-bit ABI"));
e9670677
MR
10750 }
10751 else
10752 {
316f5878
RS
10753 /* Infer the integer register size from the ABI and processor.
10754 Restrict ourselves to 32-bit registers if that's all the
10755 processor has, or if the ABI cannot handle 64-bit registers. */
10756 file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
10757 || !ISA_HAS_64BIT_REGS (mips_opts.isa));
e9670677
MR
10758 }
10759
316f5878
RS
10760 /* ??? GAS treats single-float processors as though they had 64-bit
10761 float registers (although it complains when double-precision
10762 instructions are used). As things stand, saying they have 32-bit
10763 registers would lead to spurious "register must be even" messages.
10764 So here we assume float registers are always the same size as
10765 integer ones, unless the user says otherwise. */
10766 if (file_mips_fp32 < 0)
10767 file_mips_fp32 = file_mips_gp32;
e9670677 10768
316f5878 10769 /* End of GCC-shared inference code. */
e9670677 10770
316f5878
RS
10771 /* ??? When do we want this flag to be set? Who uses it? */
10772 if (file_mips_gp32 == 1
10773 && mips_abi == NO_ABI
10774 && ISA_HAS_64BIT_REGS (mips_opts.isa))
10775 mips_32bitmode = 1;
e9670677
MR
10776
10777 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
10778 as_bad (_("trap exception not supported at ISA 1"));
10779
e9670677
MR
10780 /* If the selected architecture includes support for ASEs, enable
10781 generation of code for them. */
a4672219
TS
10782 if (mips_opts.mips16 == -1)
10783 mips_opts.mips16 = (CPU_HAS_MIPS16 (mips_arch)) ? 1 : 0;
ffdefa66 10784 if (mips_opts.ase_mips3d == -1)
a4672219 10785 mips_opts.ase_mips3d = (CPU_HAS_MIPS3D (mips_arch)) ? 1 : 0;
ffdefa66 10786 if (mips_opts.ase_mdmx == -1)
a4672219 10787 mips_opts.ase_mdmx = (CPU_HAS_MDMX (mips_arch)) ? 1 : 0;
e9670677 10788
e9670677 10789 file_mips_isa = mips_opts.isa;
a4672219 10790 file_ase_mips16 = mips_opts.mips16;
e9670677
MR
10791 file_ase_mips3d = mips_opts.ase_mips3d;
10792 file_ase_mdmx = mips_opts.ase_mdmx;
10793 mips_opts.gp32 = file_mips_gp32;
10794 mips_opts.fp32 = file_mips_fp32;
10795
ecb4347a
DJ
10796 if (mips_flag_mdebug < 0)
10797 {
10798#ifdef OBJ_MAYBE_ECOFF
10799 if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
10800 mips_flag_mdebug = 1;
10801 else
10802#endif /* OBJ_MAYBE_ECOFF */
10803 mips_flag_mdebug = 0;
10804 }
e9670677
MR
10805}
10806\f
10807void
252b5132
RH
10808mips_init_after_args ()
10809{
10810 /* initialize opcodes */
10811 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
beae10d5 10812 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
252b5132
RH
10813}
10814
10815long
10816md_pcrel_from (fixP)
10817 fixS *fixP;
10818{
10819 if (OUTPUT_FLAVOR != bfd_target_aout_flavour
10820 && fixP->fx_addsy != (symbolS *) NULL
10821 && ! S_IS_DEFINED (fixP->fx_addsy))
af6ae2ad 10822 return 4;
252b5132 10823
c9914766 10824 /* Return the address of the delay slot. */
252b5132
RH
10825 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
10826}
10827
252b5132
RH
10828/* This is called before the symbol table is processed. In order to
10829 work with gcc when using mips-tfile, we must keep all local labels.
10830 However, in other cases, we want to discard them. If we were
10831 called with -g, but we didn't see any debugging information, it may
10832 mean that gcc is smuggling debugging information through to
10833 mips-tfile, in which case we must generate all local labels. */
10834
10835void
10836mips_frob_file_before_adjust ()
10837{
10838#ifndef NO_ECOFF_DEBUGGING
10839 if (ECOFF_DEBUGGING
10840 && mips_debug != 0
10841 && ! ecoff_debugging_seen)
10842 flag_keep_locals = 1;
10843#endif
10844}
10845
10846/* Sort any unmatched HI16_S relocs so that they immediately precede
94f592af 10847 the corresponding LO reloc. This is called before md_apply_fix3 and
252b5132
RH
10848 tc_gen_reloc. Unmatched HI16_S relocs can only be generated by
10849 explicit use of the %hi modifier. */
10850
10851void
10852mips_frob_file ()
10853{
10854 struct mips_hi_fixup *l;
10855
10856 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
10857 {
10858 segment_info_type *seginfo;
10859 int pass;
10860
10861 assert (l->fixp->fx_r_type == BFD_RELOC_HI16_S);
10862
10863 /* Check quickly whether the next fixup happens to be a matching
10864 %lo. */
10865 if (l->fixp->fx_next != NULL
10866 && l->fixp->fx_next->fx_r_type == BFD_RELOC_LO16
10867 && l->fixp->fx_addsy == l->fixp->fx_next->fx_addsy
10868 && l->fixp->fx_offset == l->fixp->fx_next->fx_offset)
10869 continue;
10870
10871 /* Look through the fixups for this segment for a matching %lo.
10872 When we find one, move the %hi just in front of it. We do
10873 this in two passes. In the first pass, we try to find a
10874 unique %lo. In the second pass, we permit multiple %hi
10875 relocs for a single %lo (this is a GNU extension). */
10876 seginfo = seg_info (l->seg);
10877 for (pass = 0; pass < 2; pass++)
10878 {
10879 fixS *f, *prev;
10880
10881 prev = NULL;
10882 for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
10883 {
10884 /* Check whether this is a %lo fixup which matches l->fixp. */
10885 if (f->fx_r_type == BFD_RELOC_LO16
10886 && f->fx_addsy == l->fixp->fx_addsy
10887 && f->fx_offset == l->fixp->fx_offset
10888 && (pass == 1
10889 || prev == NULL
10890 || prev->fx_r_type != BFD_RELOC_HI16_S
10891 || prev->fx_addsy != f->fx_addsy
10892 || prev->fx_offset != f->fx_offset))
10893 {
10894 fixS **pf;
10895
10896 /* Move l->fixp before f. */
10897 for (pf = &seginfo->fix_root;
10898 *pf != l->fixp;
10899 pf = &(*pf)->fx_next)
10900 assert (*pf != NULL);
10901
10902 *pf = l->fixp->fx_next;
10903
10904 l->fixp->fx_next = f;
10905 if (prev == NULL)
10906 seginfo->fix_root = l->fixp;
10907 else
10908 prev->fx_next = l->fixp;
10909
10910 break;
10911 }
10912
10913 prev = f;
10914 }
10915
10916 if (f != NULL)
10917 break;
10918
10919#if 0 /* GCC code motion plus incomplete dead code elimination
10920 can leave a %hi without a %lo. */
10921 if (pass == 1)
10922 as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
10923 _("Unmatched %%hi reloc"));
10924#endif
10925 }
10926 }
10927}
10928
10929/* When generating embedded PIC code we need to use a special
10930 relocation to represent the difference of two symbols in the .text
10931 section (switch tables use a difference of this sort). See
10932 include/coff/mips.h for details. This macro checks whether this
10933 fixup requires the special reloc. */
10934#define SWITCH_TABLE(fixp) \
10935 ((fixp)->fx_r_type == BFD_RELOC_32 \
bb2d6cd7 10936 && OUTPUT_FLAVOR != bfd_target_elf_flavour \
252b5132
RH
10937 && (fixp)->fx_addsy != NULL \
10938 && (fixp)->fx_subsy != NULL \
10939 && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
10940 && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
10941
10942/* When generating embedded PIC code we must keep all PC relative
10943 relocations, in case the linker has to relax a call. We also need
f6688943
TS
10944 to keep relocations for switch table entries.
10945
10946 We may have combined relocations without symbols in the N32/N64 ABI.
10947 We have to prevent gas from dropping them. */
252b5132 10948
252b5132
RH
10949int
10950mips_force_relocation (fixp)
10951 fixS *fixp;
10952{
ae6063d4 10953 if (generic_force_reloc (fixp))
252b5132
RH
10954 return 1;
10955
f6688943
TS
10956 if (HAVE_NEWABI
10957 && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
10958 && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
10959 || fixp->fx_r_type == BFD_RELOC_HI16_S
10960 || fixp->fx_r_type == BFD_RELOC_LO16))
10961 return 1;
10962
252b5132
RH
10963 return (mips_pic == EMBEDDED_PIC
10964 && (fixp->fx_pcrel
10965 || SWITCH_TABLE (fixp)
10966 || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
10967 || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
10968}
10969
add55e1f
RS
10970#ifdef OBJ_ELF
10971static int
10972mips_need_elf_addend_fixup (fixP)
10973 fixS *fixP;
10974{
2d2bf3e0
CD
10975 if (S_GET_OTHER (fixP->fx_addsy) == STO_MIPS16)
10976 return 1;
b25a253c
CD
10977 if (mips_pic == EMBEDDED_PIC
10978 && S_IS_WEAK (fixP->fx_addsy))
10979 return 1;
10980 if (mips_pic != EMBEDDED_PIC
10981 && (S_IS_WEAK (fixP->fx_addsy)
bad9ca53 10982 || S_IS_EXTERNAL (fixP->fx_addsy))
2d2bf3e0
CD
10983 && !S_IS_COMMON (fixP->fx_addsy))
10984 return 1;
10985 if (symbol_used_in_reloc_p (fixP->fx_addsy)
10986 && (((bfd_get_section_flags (stdoutput,
10987 S_GET_SEGMENT (fixP->fx_addsy))
631cb423 10988 & (SEC_LINK_ONCE | SEC_MERGE)) != 0)
2d2bf3e0
CD
10989 || !strncmp (segment_name (S_GET_SEGMENT (fixP->fx_addsy)),
10990 ".gnu.linkonce",
10991 sizeof (".gnu.linkonce") - 1)))
10992 return 1;
10993 return 0;
add55e1f
RS
10994}
10995#endif
10996
252b5132
RH
10997/* Apply a fixup to the object file. */
10998
94f592af
NC
10999void
11000md_apply_fix3 (fixP, valP, seg)
252b5132 11001 fixS *fixP;
98d3f06f 11002 valueT *valP;
94f592af 11003 segT seg ATTRIBUTE_UNUSED;
252b5132 11004{
874e8986 11005 bfd_byte *buf;
98aa84af
AM
11006 long insn;
11007 valueT value;
ed6fb7bd 11008 static int previous_fx_r_type = 0;
252b5132 11009
65551fa4
CD
11010 /* FIXME: Maybe just return for all reloc types not listed below?
11011 Eric Christopher says: "This is stupid, please rewrite md_apply_fix3. */
11012 if (fixP->fx_r_type == BFD_RELOC_8)
11013 return;
11014
252b5132
RH
11015 assert (fixP->fx_size == 4
11016 || fixP->fx_r_type == BFD_RELOC_16
f6688943
TS
11017 || fixP->fx_r_type == BFD_RELOC_32
11018 || fixP->fx_r_type == BFD_RELOC_MIPS_JMP
11019 || fixP->fx_r_type == BFD_RELOC_HI16_S
11020 || fixP->fx_r_type == BFD_RELOC_LO16
11021 || fixP->fx_r_type == BFD_RELOC_GPREL16
76b3015f 11022 || fixP->fx_r_type == BFD_RELOC_MIPS_LITERAL
f6688943 11023 || fixP->fx_r_type == BFD_RELOC_GPREL32
252b5132 11024 || fixP->fx_r_type == BFD_RELOC_64
f6688943
TS
11025 || fixP->fx_r_type == BFD_RELOC_CTOR
11026 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
11027 || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHEST
11028 || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHER
11029 || fixP->fx_r_type == BFD_RELOC_MIPS_SCN_DISP
11030 || fixP->fx_r_type == BFD_RELOC_MIPS_REL16
11031 || fixP->fx_r_type == BFD_RELOC_MIPS_RELGOT
252b5132 11032 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
438c16b8
TS
11033 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
11034 || fixP->fx_r_type == BFD_RELOC_MIPS_JALR);
252b5132 11035
98d3f06f 11036 value = *valP;
252b5132
RH
11037
11038 /* If we aren't adjusting this fixup to be against the section
11039 symbol, we need to adjust the value. */
11040#ifdef OBJ_ELF
11041 if (fixP->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour)
bb2d6cd7 11042 {
add55e1f 11043 if (mips_need_elf_addend_fixup (fixP))
98aa84af 11044 {
d6e9d61a 11045 reloc_howto_type *howto;
98aa84af 11046 valueT symval = S_GET_VALUE (fixP->fx_addsy);
94f592af 11047
98aa84af 11048 value -= symval;
d6e9d61a
MR
11049
11050 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
af6ae2ad 11051 if (value != 0 && howto && howto->partial_inplace
7a49a8c2 11052 && (! fixP->fx_pcrel || howto->pcrel_offset))
98aa84af
AM
11053 {
11054 /* In this case, the bfd_install_relocation routine will
11055 incorrectly add the symbol value back in. We just want
7a49a8c2 11056 the addend to appear in the object file.
b34976b6 11057
7a49a8c2
L
11058 howto->pcrel_offset is added for R_MIPS_PC16, which is
11059 generated for code like
b34976b6 11060
7a49a8c2
L
11061 globl g1 .text
11062 .text
11063 .space 20
11064 g1:
11065 x:
11066 bal g1
11067 */
98aa84af 11068 value -= symval;
7461da6e
RS
11069
11070 /* Make sure the addend is still non-zero. If it became zero
11071 after the last operation, set it to a spurious value and
11072 subtract the same value from the object file's contents. */
11073 if (value == 0)
11074 {
11075 value = 8;
11076
11077 /* The in-place addends for LO16 relocations are signed;
11078 leave the matching HI16 in-place addends as zero. */
11079 if (fixP->fx_r_type != BFD_RELOC_HI16_S)
11080 {
7461da6e
RS
11081 bfd_vma contents, mask, field;
11082
7461da6e
RS
11083 contents = bfd_get_bits (fixP->fx_frag->fr_literal
11084 + fixP->fx_where,
11085 fixP->fx_size * 8,
11086 target_big_endian);
11087
11088 /* MASK has bits set where the relocation should go.
11089 FIELD is -value, shifted into the appropriate place
11090 for this relocation. */
11091 mask = 1 << (howto->bitsize - 1);
11092 mask = (((mask - 1) << 1) | 1) << howto->bitpos;
11093 field = (-value >> howto->rightshift) << howto->bitpos;
11094
11095 bfd_put_bits ((field & mask) | (contents & ~mask),
11096 fixP->fx_frag->fr_literal + fixP->fx_where,
11097 fixP->fx_size * 8,
11098 target_big_endian);
11099 }
11100 }
98aa84af
AM
11101 }
11102 }
252b5132 11103
bb2d6cd7
GK
11104 /* This code was generated using trial and error and so is
11105 fragile and not trustworthy. If you change it, you should
11106 rerun the elf-rel, elf-rel2, and empic testcases and ensure
11107 they still pass. */
11108 if (fixP->fx_pcrel || fixP->fx_subsy != NULL)
11109 {
11110 value += fixP->fx_frag->fr_address + fixP->fx_where;
11111
11112 /* BFD's REL handling, for MIPS, is _very_ weird.
11113 This gives the right results, but it can't possibly
11114 be the way things are supposed to work. */
af6ae2ad 11115 if (fixP->fx_r_type != BFD_RELOC_16_PCREL_S2
bb2d6cd7
GK
11116 || S_GET_SEGMENT (fixP->fx_addsy) != undefined_section)
11117 value += fixP->fx_frag->fr_address + fixP->fx_where;
11118 }
11119 }
11120#endif
252b5132 11121
94f592af 11122 fixP->fx_addnumber = value; /* Remember value for tc_gen_reloc. */
252b5132 11123
ed6fb7bd
SC
11124 /* We are not done if this is a composite relocation to set up gp. */
11125 if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel
11126 && !(fixP->fx_r_type == BFD_RELOC_MIPS_SUB
10181a0d
AO
11127 || (fixP->fx_r_type == BFD_RELOC_64
11128 && (previous_fx_r_type == BFD_RELOC_GPREL32
11129 || previous_fx_r_type == BFD_RELOC_GPREL16))
ed6fb7bd
SC
11130 || (previous_fx_r_type == BFD_RELOC_MIPS_SUB
11131 && (fixP->fx_r_type == BFD_RELOC_HI16_S
11132 || fixP->fx_r_type == BFD_RELOC_LO16))))
252b5132 11133 fixP->fx_done = 1;
ed6fb7bd 11134 previous_fx_r_type = fixP->fx_r_type;
252b5132
RH
11135
11136 switch (fixP->fx_r_type)
11137 {
11138 case BFD_RELOC_MIPS_JMP:
e369bcce
TS
11139 case BFD_RELOC_MIPS_SHIFT5:
11140 case BFD_RELOC_MIPS_SHIFT6:
11141 case BFD_RELOC_MIPS_GOT_DISP:
11142 case BFD_RELOC_MIPS_GOT_PAGE:
11143 case BFD_RELOC_MIPS_GOT_OFST:
11144 case BFD_RELOC_MIPS_SUB:
11145 case BFD_RELOC_MIPS_INSERT_A:
11146 case BFD_RELOC_MIPS_INSERT_B:
11147 case BFD_RELOC_MIPS_DELETE:
11148 case BFD_RELOC_MIPS_HIGHEST:
11149 case BFD_RELOC_MIPS_HIGHER:
11150 case BFD_RELOC_MIPS_SCN_DISP:
11151 case BFD_RELOC_MIPS_REL16:
11152 case BFD_RELOC_MIPS_RELGOT:
11153 case BFD_RELOC_MIPS_JALR:
252b5132
RH
11154 case BFD_RELOC_HI16:
11155 case BFD_RELOC_HI16_S:
cdf6fd85 11156 case BFD_RELOC_GPREL16:
252b5132
RH
11157 case BFD_RELOC_MIPS_LITERAL:
11158 case BFD_RELOC_MIPS_CALL16:
11159 case BFD_RELOC_MIPS_GOT16:
cdf6fd85 11160 case BFD_RELOC_GPREL32:
252b5132
RH
11161 case BFD_RELOC_MIPS_GOT_HI16:
11162 case BFD_RELOC_MIPS_GOT_LO16:
11163 case BFD_RELOC_MIPS_CALL_HI16:
11164 case BFD_RELOC_MIPS_CALL_LO16:
11165 case BFD_RELOC_MIPS16_GPREL:
11166 if (fixP->fx_pcrel)
11167 as_bad_where (fixP->fx_file, fixP->fx_line,
11168 _("Invalid PC relative reloc"));
11169 /* Nothing needed to do. The value comes from the reloc entry */
11170 break;
11171
11172 case BFD_RELOC_MIPS16_JMP:
11173 /* We currently always generate a reloc against a symbol, which
11174 means that we don't want an addend even if the symbol is
11175 defined. */
11176 fixP->fx_addnumber = 0;
11177 break;
11178
11179 case BFD_RELOC_PCREL_HI16_S:
11180 /* The addend for this is tricky if it is internal, so we just
11181 do everything here rather than in bfd_install_relocation. */
bdaaa2e1 11182 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
bb2d6cd7
GK
11183 && !fixP->fx_done
11184 && value != 0)
11185 break;
11186 if (fixP->fx_addsy
11187 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
252b5132
RH
11188 {
11189 /* For an external symbol adjust by the address to make it
11190 pcrel_offset. We use the address of the RELLO reloc
11191 which follows this one. */
11192 value += (fixP->fx_next->fx_frag->fr_address
11193 + fixP->fx_next->fx_where);
11194 }
e7d556df 11195 value = ((value + 0x8000) >> 16) & 0xffff;
874e8986 11196 buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
252b5132
RH
11197 if (target_big_endian)
11198 buf += 2;
874e8986 11199 md_number_to_chars ((char *) buf, value, 2);
252b5132
RH
11200 break;
11201
11202 case BFD_RELOC_PCREL_LO16:
11203 /* The addend for this is tricky if it is internal, so we just
11204 do everything here rather than in bfd_install_relocation. */
bdaaa2e1 11205 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
bb2d6cd7
GK
11206 && !fixP->fx_done
11207 && value != 0)
11208 break;
11209 if (fixP->fx_addsy
11210 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
252b5132 11211 value += fixP->fx_frag->fr_address + fixP->fx_where;
874e8986 11212 buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
252b5132
RH
11213 if (target_big_endian)
11214 buf += 2;
874e8986 11215 md_number_to_chars ((char *) buf, value, 2);
252b5132
RH
11216 break;
11217
11218 case BFD_RELOC_64:
11219 /* This is handled like BFD_RELOC_32, but we output a sign
11220 extended value if we are only 32 bits. */
11221 if (fixP->fx_done
11222 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11223 {
11224 if (8 <= sizeof (valueT))
11225 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11226 value, 8);
11227 else
11228 {
11229 long w1, w2;
11230 long hiv;
11231
11232 w1 = w2 = fixP->fx_where;
11233 if (target_big_endian)
11234 w1 += 4;
11235 else
11236 w2 += 4;
11237 md_number_to_chars (fixP->fx_frag->fr_literal + w1, value, 4);
11238 if ((value & 0x80000000) != 0)
11239 hiv = 0xffffffff;
11240 else
11241 hiv = 0;
11242 md_number_to_chars (fixP->fx_frag->fr_literal + w2, hiv, 4);
11243 }
11244 }
11245 break;
11246
056350c6 11247 case BFD_RELOC_RVA:
252b5132
RH
11248 case BFD_RELOC_32:
11249 /* If we are deleting this reloc entry, we must fill in the
11250 value now. This can happen if we have a .word which is not
11251 resolved when it appears but is later defined. We also need
11252 to fill in the value if this is an embedded PIC switch table
11253 entry. */
11254 if (fixP->fx_done
11255 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11256 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11257 value, 4);
11258 break;
11259
11260 case BFD_RELOC_16:
11261 /* If we are deleting this reloc entry, we must fill in the
11262 value now. */
11263 assert (fixP->fx_size == 2);
11264 if (fixP->fx_done)
11265 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11266 value, 2);
11267 break;
11268
11269 case BFD_RELOC_LO16:
11270 /* When handling an embedded PIC switch statement, we can wind
11271 up deleting a LO16 reloc. See the 'o' case in mips_ip. */
11272 if (fixP->fx_done)
11273 {
98aa84af 11274 if (value + 0x8000 > 0xffff)
252b5132
RH
11275 as_bad_where (fixP->fx_file, fixP->fx_line,
11276 _("relocation overflow"));
874e8986 11277 buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
252b5132
RH
11278 if (target_big_endian)
11279 buf += 2;
874e8986 11280 md_number_to_chars ((char *) buf, value, 2);
252b5132
RH
11281 }
11282 break;
11283
11284 case BFD_RELOC_16_PCREL_S2:
cb56d3d3
TS
11285 if ((value & 0x3) != 0)
11286 as_bad_where (fixP->fx_file, fixP->fx_line,
11287 _("Branch to odd address (%lx)"), (long) value);
11288
252b5132
RH
11289 /*
11290 * We need to save the bits in the instruction since fixup_segment()
11291 * might be deleting the relocation entry (i.e., a branch within
11292 * the current segment).
11293 */
af6ae2ad 11294 if (!fixP->fx_done && (value != 0 || HAVE_NEWABI))
bb2d6cd7
GK
11295 break;
11296 /* If 'value' is zero, the remaining reloc code won't actually
11297 do the store, so it must be done here. This is probably
11298 a bug somewhere. */
b25a253c
CD
11299 if (!fixP->fx_done
11300 && (fixP->fx_r_type != BFD_RELOC_16_PCREL_S2
11301 || fixP->fx_addsy == NULL /* ??? */
11302 || ! S_IS_DEFINED (fixP->fx_addsy)))
bb2d6cd7 11303 value -= fixP->fx_frag->fr_address + fixP->fx_where;
bdaaa2e1 11304
98aa84af 11305 value = (offsetT) value >> 2;
252b5132
RH
11306
11307 /* update old instruction data */
874e8986 11308 buf = (bfd_byte *) (fixP->fx_where + fixP->fx_frag->fr_literal);
252b5132
RH
11309 if (target_big_endian)
11310 insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
11311 else
11312 insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
11313
98aa84af 11314 if (value + 0x8000 <= 0xffff)
252b5132
RH
11315 insn |= value & 0xffff;
11316 else
11317 {
11318 /* The branch offset is too large. If this is an
11319 unconditional branch, and we are not generating PIC code,
11320 we can convert it to an absolute jump instruction. */
11321 if (mips_pic == NO_PIC
11322 && fixP->fx_done
11323 && fixP->fx_frag->fr_address >= text_section->vma
11324 && (fixP->fx_frag->fr_address
11325 < text_section->vma + text_section->_raw_size)
11326 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
11327 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
11328 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
11329 {
11330 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
11331 insn = 0x0c000000; /* jal */
11332 else
11333 insn = 0x08000000; /* j */
11334 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
11335 fixP->fx_done = 0;
11336 fixP->fx_addsy = section_symbol (text_section);
11337 fixP->fx_addnumber = (value << 2) + md_pcrel_from (fixP);
11338 }
11339 else
11340 {
4a6a3df4
AO
11341 /* If we got here, we have branch-relaxation disabled,
11342 and there's nothing we can do to fix this instruction
11343 without turning it into a longer sequence. */
252b5132
RH
11344 as_bad_where (fixP->fx_file, fixP->fx_line,
11345 _("Branch out of range"));
11346 }
11347 }
11348
11349 md_number_to_chars ((char *) buf, (valueT) insn, 4);
11350 break;
11351
11352 case BFD_RELOC_VTABLE_INHERIT:
11353 fixP->fx_done = 0;
11354 if (fixP->fx_addsy
11355 && !S_IS_DEFINED (fixP->fx_addsy)
11356 && !S_IS_WEAK (fixP->fx_addsy))
11357 S_SET_WEAK (fixP->fx_addsy);
11358 break;
11359
11360 case BFD_RELOC_VTABLE_ENTRY:
11361 fixP->fx_done = 0;
11362 break;
11363
11364 default:
11365 internalError ();
11366 }
252b5132
RH
11367}
11368
11369#if 0
11370void
11371printInsn (oc)
11372 unsigned long oc;
11373{
11374 const struct mips_opcode *p;
11375 int treg, sreg, dreg, shamt;
11376 short imm;
11377 const char *args;
11378 int i;
11379
11380 for (i = 0; i < NUMOPCODES; ++i)
11381 {
11382 p = &mips_opcodes[i];
11383 if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
11384 {
11385 printf ("%08lx %s\t", oc, p->name);
11386 treg = (oc >> 16) & 0x1f;
11387 sreg = (oc >> 21) & 0x1f;
11388 dreg = (oc >> 11) & 0x1f;
11389 shamt = (oc >> 6) & 0x1f;
11390 imm = oc;
11391 for (args = p->args;; ++args)
11392 {
11393 switch (*args)
11394 {
11395 case '\0':
11396 printf ("\n");
11397 break;
11398
11399 case ',':
11400 case '(':
11401 case ')':
11402 printf ("%c", *args);
11403 continue;
11404
11405 case 'r':
11406 assert (treg == sreg);
11407 printf ("$%d,$%d", treg, sreg);
11408 continue;
11409
11410 case 'd':
11411 case 'G':
11412 printf ("$%d", dreg);
11413 continue;
11414
11415 case 't':
11416 case 'E':
11417 printf ("$%d", treg);
11418 continue;
11419
11420 case 'k':
11421 printf ("0x%x", treg);
11422 continue;
11423
11424 case 'b':
11425 case 's':
11426 printf ("$%d", sreg);
11427 continue;
11428
11429 case 'a':
11430 printf ("0x%08lx", oc & 0x1ffffff);
11431 continue;
11432
11433 case 'i':
11434 case 'j':
11435 case 'o':
11436 case 'u':
11437 printf ("%d", imm);
11438 continue;
11439
11440 case '<':
11441 case '>':
11442 printf ("$%d", shamt);
11443 continue;
11444
11445 default:
11446 internalError ();
11447 }
11448 break;
11449 }
11450 return;
11451 }
11452 }
11453 printf (_("%08lx UNDEFINED\n"), oc);
11454}
11455#endif
11456
11457static symbolS *
11458get_symbol ()
11459{
11460 int c;
11461 char *name;
11462 symbolS *p;
11463
11464 name = input_line_pointer;
11465 c = get_symbol_end ();
11466 p = (symbolS *) symbol_find_or_make (name);
11467 *input_line_pointer = c;
11468 return p;
11469}
11470
11471/* Align the current frag to a given power of two. The MIPS assembler
11472 also automatically adjusts any preceding label. */
11473
11474static void
11475mips_align (to, fill, label)
11476 int to;
11477 int fill;
11478 symbolS *label;
11479{
b34976b6 11480 mips_emit_delays (FALSE);
252b5132
RH
11481 frag_align (to, fill, 0);
11482 record_alignment (now_seg, to);
11483 if (label != NULL)
11484 {
11485 assert (S_GET_SEGMENT (label) == now_seg);
49309057 11486 symbol_set_frag (label, frag_now);
252b5132
RH
11487 S_SET_VALUE (label, (valueT) frag_now_fix ());
11488 }
11489}
11490
11491/* Align to a given power of two. .align 0 turns off the automatic
11492 alignment used by the data creating pseudo-ops. */
11493
11494static void
11495s_align (x)
43841e91 11496 int x ATTRIBUTE_UNUSED;
252b5132
RH
11497{
11498 register int temp;
11499 register long temp_fill;
11500 long max_alignment = 15;
11501
11502 /*
11503
11504 o Note that the assembler pulls down any immediately preceeding label
11505 to the aligned address.
11506 o It's not documented but auto alignment is reinstated by
11507 a .align pseudo instruction.
11508 o Note also that after auto alignment is turned off the mips assembler
11509 issues an error on attempt to assemble an improperly aligned data item.
11510 We don't.
11511
11512 */
11513
11514 temp = get_absolute_expression ();
11515 if (temp > max_alignment)
11516 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
11517 else if (temp < 0)
11518 {
11519 as_warn (_("Alignment negative: 0 assumed."));
11520 temp = 0;
11521 }
11522 if (*input_line_pointer == ',')
11523 {
f9419b05 11524 ++input_line_pointer;
252b5132
RH
11525 temp_fill = get_absolute_expression ();
11526 }
11527 else
11528 temp_fill = 0;
11529 if (temp)
11530 {
11531 auto_align = 1;
11532 mips_align (temp, (int) temp_fill,
11533 insn_labels != NULL ? insn_labels->label : NULL);
11534 }
11535 else
11536 {
11537 auto_align = 0;
11538 }
11539
11540 demand_empty_rest_of_line ();
11541}
11542
11543void
11544mips_flush_pending_output ()
11545{
b34976b6 11546 mips_emit_delays (FALSE);
252b5132
RH
11547 mips_clear_insn_labels ();
11548}
11549
11550static void
11551s_change_sec (sec)
11552 int sec;
11553{
11554 segT seg;
11555
11556 /* When generating embedded PIC code, we only use the .text, .lit8,
11557 .sdata and .sbss sections. We change the .data and .rdata
11558 pseudo-ops to use .sdata. */
11559 if (mips_pic == EMBEDDED_PIC
11560 && (sec == 'd' || sec == 'r'))
11561 sec = 's';
11562
11563#ifdef OBJ_ELF
11564 /* The ELF backend needs to know that we are changing sections, so
11565 that .previous works correctly. We could do something like check
b6ff326e 11566 for an obj_section_change_hook macro, but that might be confusing
252b5132
RH
11567 as it would not be appropriate to use it in the section changing
11568 functions in read.c, since obj-elf.c intercepts those. FIXME:
11569 This should be cleaner, somehow. */
11570 obj_elf_section_change_hook ();
11571#endif
11572
b34976b6 11573 mips_emit_delays (FALSE);
252b5132
RH
11574 switch (sec)
11575 {
11576 case 't':
11577 s_text (0);
11578 break;
11579 case 'd':
11580 s_data (0);
11581 break;
11582 case 'b':
11583 subseg_set (bss_section, (subsegT) get_absolute_expression ());
11584 demand_empty_rest_of_line ();
11585 break;
11586
11587 case 'r':
11588 if (USE_GLOBAL_POINTER_OPT)
11589 {
11590 seg = subseg_new (RDATA_SECTION_NAME,
11591 (subsegT) get_absolute_expression ());
11592 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11593 {
11594 bfd_set_section_flags (stdoutput, seg,
11595 (SEC_ALLOC
11596 | SEC_LOAD
11597 | SEC_READONLY
11598 | SEC_RELOC
11599 | SEC_DATA));
11600 if (strcmp (TARGET_OS, "elf") != 0)
e799a695 11601 record_alignment (seg, 4);
252b5132
RH
11602 }
11603 demand_empty_rest_of_line ();
11604 }
11605 else
11606 {
11607 as_bad (_("No read only data section in this object file format"));
11608 demand_empty_rest_of_line ();
11609 return;
11610 }
11611 break;
11612
11613 case 's':
11614 if (USE_GLOBAL_POINTER_OPT)
11615 {
11616 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
11617 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11618 {
11619 bfd_set_section_flags (stdoutput, seg,
11620 SEC_ALLOC | SEC_LOAD | SEC_RELOC
11621 | SEC_DATA);
11622 if (strcmp (TARGET_OS, "elf") != 0)
e799a695 11623 record_alignment (seg, 4);
252b5132
RH
11624 }
11625 demand_empty_rest_of_line ();
11626 break;
11627 }
11628 else
11629 {
11630 as_bad (_("Global pointers not supported; recompile -G 0"));
11631 demand_empty_rest_of_line ();
11632 return;
11633 }
11634 }
11635
11636 auto_align = 1;
11637}
b34976b6 11638
cca86cc8
SC
11639void
11640s_change_section (ignore)
11641 int ignore ATTRIBUTE_UNUSED;
11642{
7ed4a06a 11643#ifdef OBJ_ELF
cca86cc8
SC
11644 char *section_name;
11645 char c;
4cf0dd0d 11646 char next_c;
cca86cc8
SC
11647 int section_type;
11648 int section_flag;
11649 int section_entry_size;
11650 int section_alignment;
b34976b6 11651
7ed4a06a
TS
11652 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11653 return;
11654
cca86cc8
SC
11655 section_name = input_line_pointer;
11656 c = get_symbol_end ();
a816d1ed
AO
11657 if (c)
11658 next_c = *(input_line_pointer + 1);
cca86cc8 11659
4cf0dd0d
TS
11660 /* Do we have .section Name<,"flags">? */
11661 if (c != ',' || (c == ',' && next_c == '"'))
cca86cc8 11662 {
4cf0dd0d
TS
11663 /* just after name is now '\0'. */
11664 *input_line_pointer = c;
cca86cc8
SC
11665 input_line_pointer = section_name;
11666 obj_elf_section (ignore);
11667 return;
11668 }
11669 input_line_pointer++;
11670
11671 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
11672 if (c == ',')
11673 section_type = get_absolute_expression ();
11674 else
11675 section_type = 0;
11676 if (*input_line_pointer++ == ',')
11677 section_flag = get_absolute_expression ();
11678 else
11679 section_flag = 0;
11680 if (*input_line_pointer++ == ',')
11681 section_entry_size = get_absolute_expression ();
11682 else
11683 section_entry_size = 0;
11684 if (*input_line_pointer++ == ',')
11685 section_alignment = get_absolute_expression ();
11686 else
11687 section_alignment = 0;
11688
a816d1ed
AO
11689 section_name = xstrdup (section_name);
11690
cca86cc8
SC
11691 obj_elf_change_section (section_name, section_type, section_flag,
11692 section_entry_size, 0, 0, 0);
a816d1ed
AO
11693
11694 if (now_seg->name != section_name)
11695 free (section_name);
7ed4a06a 11696#endif /* OBJ_ELF */
cca86cc8 11697}
252b5132
RH
11698
11699void
11700mips_enable_auto_align ()
11701{
11702 auto_align = 1;
11703}
11704
11705static void
11706s_cons (log_size)
11707 int log_size;
11708{
11709 symbolS *label;
11710
11711 label = insn_labels != NULL ? insn_labels->label : NULL;
b34976b6 11712 mips_emit_delays (FALSE);
252b5132
RH
11713 if (log_size > 0 && auto_align)
11714 mips_align (log_size, 0, label);
11715 mips_clear_insn_labels ();
11716 cons (1 << log_size);
11717}
11718
11719static void
11720s_float_cons (type)
11721 int type;
11722{
11723 symbolS *label;
11724
11725 label = insn_labels != NULL ? insn_labels->label : NULL;
11726
b34976b6 11727 mips_emit_delays (FALSE);
252b5132
RH
11728
11729 if (auto_align)
49309057
ILT
11730 {
11731 if (type == 'd')
11732 mips_align (3, 0, label);
11733 else
11734 mips_align (2, 0, label);
11735 }
252b5132
RH
11736
11737 mips_clear_insn_labels ();
11738
11739 float_cons (type);
11740}
11741
11742/* Handle .globl. We need to override it because on Irix 5 you are
11743 permitted to say
11744 .globl foo .text
11745 where foo is an undefined symbol, to mean that foo should be
11746 considered to be the address of a function. */
11747
11748static void
11749s_mips_globl (x)
43841e91 11750 int x ATTRIBUTE_UNUSED;
252b5132
RH
11751{
11752 char *name;
11753 int c;
11754 symbolS *symbolP;
11755 flagword flag;
11756
11757 name = input_line_pointer;
11758 c = get_symbol_end ();
11759 symbolP = symbol_find_or_make (name);
11760 *input_line_pointer = c;
11761 SKIP_WHITESPACE ();
11762
11763 /* On Irix 5, every global symbol that is not explicitly labelled as
11764 being a function is apparently labelled as being an object. */
11765 flag = BSF_OBJECT;
11766
11767 if (! is_end_of_line[(unsigned char) *input_line_pointer])
11768 {
11769 char *secname;
11770 asection *sec;
11771
11772 secname = input_line_pointer;
11773 c = get_symbol_end ();
11774 sec = bfd_get_section_by_name (stdoutput, secname);
11775 if (sec == NULL)
11776 as_bad (_("%s: no such section"), secname);
11777 *input_line_pointer = c;
11778
11779 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
11780 flag = BSF_FUNCTION;
11781 }
11782
49309057 11783 symbol_get_bfdsym (symbolP)->flags |= flag;
252b5132
RH
11784
11785 S_SET_EXTERNAL (symbolP);
11786 demand_empty_rest_of_line ();
11787}
11788
11789static void
11790s_option (x)
43841e91 11791 int x ATTRIBUTE_UNUSED;
252b5132
RH
11792{
11793 char *opt;
11794 char c;
11795
11796 opt = input_line_pointer;
11797 c = get_symbol_end ();
11798
11799 if (*opt == 'O')
11800 {
11801 /* FIXME: What does this mean? */
11802 }
11803 else if (strncmp (opt, "pic", 3) == 0)
11804 {
11805 int i;
11806
11807 i = atoi (opt + 3);
11808 if (i == 0)
11809 mips_pic = NO_PIC;
11810 else if (i == 2)
11811 mips_pic = SVR4_PIC;
11812 else
11813 as_bad (_(".option pic%d not supported"), i);
11814
11815 if (USE_GLOBAL_POINTER_OPT && mips_pic == SVR4_PIC)
11816 {
11817 if (g_switch_seen && g_switch_value != 0)
11818 as_warn (_("-G may not be used with SVR4 PIC code"));
11819 g_switch_value = 0;
11820 bfd_set_gp_size (stdoutput, 0);
11821 }
11822 }
11823 else
11824 as_warn (_("Unrecognized option \"%s\""), opt);
11825
11826 *input_line_pointer = c;
11827 demand_empty_rest_of_line ();
11828}
11829
11830/* This structure is used to hold a stack of .set values. */
11831
e972090a
NC
11832struct mips_option_stack
11833{
252b5132
RH
11834 struct mips_option_stack *next;
11835 struct mips_set_options options;
11836};
11837
11838static struct mips_option_stack *mips_opts_stack;
11839
11840/* Handle the .set pseudo-op. */
11841
11842static void
11843s_mipsset (x)
43841e91 11844 int x ATTRIBUTE_UNUSED;
252b5132
RH
11845{
11846 char *name = input_line_pointer, ch;
11847
11848 while (!is_end_of_line[(unsigned char) *input_line_pointer])
f9419b05 11849 ++input_line_pointer;
252b5132
RH
11850 ch = *input_line_pointer;
11851 *input_line_pointer = '\0';
11852
11853 if (strcmp (name, "reorder") == 0)
11854 {
11855 if (mips_opts.noreorder && prev_nop_frag != NULL)
11856 {
11857 /* If we still have pending nops, we can discard them. The
11858 usual nop handling will insert any that are still
bdaaa2e1 11859 needed. */
252b5132
RH
11860 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11861 * (mips_opts.mips16 ? 2 : 4));
11862 prev_nop_frag = NULL;
11863 }
11864 mips_opts.noreorder = 0;
11865 }
11866 else if (strcmp (name, "noreorder") == 0)
11867 {
b34976b6 11868 mips_emit_delays (TRUE);
252b5132
RH
11869 mips_opts.noreorder = 1;
11870 mips_any_noreorder = 1;
11871 }
11872 else if (strcmp (name, "at") == 0)
11873 {
11874 mips_opts.noat = 0;
11875 }
11876 else if (strcmp (name, "noat") == 0)
11877 {
11878 mips_opts.noat = 1;
11879 }
11880 else if (strcmp (name, "macro") == 0)
11881 {
11882 mips_opts.warn_about_macros = 0;
11883 }
11884 else if (strcmp (name, "nomacro") == 0)
11885 {
11886 if (mips_opts.noreorder == 0)
11887 as_bad (_("`noreorder' must be set before `nomacro'"));
11888 mips_opts.warn_about_macros = 1;
11889 }
11890 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
11891 {
11892 mips_opts.nomove = 0;
11893 }
11894 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
11895 {
11896 mips_opts.nomove = 1;
11897 }
11898 else if (strcmp (name, "bopt") == 0)
11899 {
11900 mips_opts.nobopt = 0;
11901 }
11902 else if (strcmp (name, "nobopt") == 0)
11903 {
11904 mips_opts.nobopt = 1;
11905 }
11906 else if (strcmp (name, "mips16") == 0
11907 || strcmp (name, "MIPS-16") == 0)
11908 mips_opts.mips16 = 1;
11909 else if (strcmp (name, "nomips16") == 0
11910 || strcmp (name, "noMIPS-16") == 0)
11911 mips_opts.mips16 = 0;
1f25f5d3
CD
11912 else if (strcmp (name, "mips3d") == 0)
11913 mips_opts.ase_mips3d = 1;
11914 else if (strcmp (name, "nomips3d") == 0)
11915 mips_opts.ase_mips3d = 0;
a4672219
TS
11916 else if (strcmp (name, "mdmx") == 0)
11917 mips_opts.ase_mdmx = 1;
11918 else if (strcmp (name, "nomdmx") == 0)
11919 mips_opts.ase_mdmx = 0;
252b5132
RH
11920 else if (strncmp (name, "mips", 4) == 0)
11921 {
af7ee8bf 11922 int reset = 0;
252b5132
RH
11923
11924 /* Permit the user to change the ISA on the fly. Needless to
11925 say, misuse can cause serious problems. */
af7ee8bf
CD
11926 if (strcmp (name, "mips0") == 0)
11927 {
11928 reset = 1;
11929 mips_opts.isa = file_mips_isa;
11930 }
11931 else if (strcmp (name, "mips1") == 0)
11932 mips_opts.isa = ISA_MIPS1;
11933 else if (strcmp (name, "mips2") == 0)
11934 mips_opts.isa = ISA_MIPS2;
11935 else if (strcmp (name, "mips3") == 0)
11936 mips_opts.isa = ISA_MIPS3;
11937 else if (strcmp (name, "mips4") == 0)
11938 mips_opts.isa = ISA_MIPS4;
11939 else if (strcmp (name, "mips5") == 0)
11940 mips_opts.isa = ISA_MIPS5;
11941 else if (strcmp (name, "mips32") == 0)
11942 mips_opts.isa = ISA_MIPS32;
11943 else if (strcmp (name, "mips32r2") == 0)
11944 mips_opts.isa = ISA_MIPS32R2;
11945 else if (strcmp (name, "mips64") == 0)
11946 mips_opts.isa = ISA_MIPS64;
11947 else
11948 as_bad (_("unknown ISA level %s"), name + 4);
11949
11950 switch (mips_opts.isa)
98d3f06f
KH
11951 {
11952 case 0:
98d3f06f 11953 break;
af7ee8bf
CD
11954 case ISA_MIPS1:
11955 case ISA_MIPS2:
11956 case ISA_MIPS32:
11957 case ISA_MIPS32R2:
98d3f06f
KH
11958 mips_opts.gp32 = 1;
11959 mips_opts.fp32 = 1;
11960 break;
af7ee8bf
CD
11961 case ISA_MIPS3:
11962 case ISA_MIPS4:
11963 case ISA_MIPS5:
11964 case ISA_MIPS64:
98d3f06f
KH
11965 mips_opts.gp32 = 0;
11966 mips_opts.fp32 = 0;
11967 break;
11968 default:
11969 as_bad (_("unknown ISA level %s"), name + 4);
11970 break;
11971 }
af7ee8bf 11972 if (reset)
98d3f06f 11973 {
af7ee8bf
CD
11974 mips_opts.gp32 = file_mips_gp32;
11975 mips_opts.fp32 = file_mips_fp32;
98d3f06f 11976 }
252b5132
RH
11977 }
11978 else if (strcmp (name, "autoextend") == 0)
11979 mips_opts.noautoextend = 0;
11980 else if (strcmp (name, "noautoextend") == 0)
11981 mips_opts.noautoextend = 1;
11982 else if (strcmp (name, "push") == 0)
11983 {
11984 struct mips_option_stack *s;
11985
11986 s = (struct mips_option_stack *) xmalloc (sizeof *s);
11987 s->next = mips_opts_stack;
11988 s->options = mips_opts;
11989 mips_opts_stack = s;
11990 }
11991 else if (strcmp (name, "pop") == 0)
11992 {
11993 struct mips_option_stack *s;
11994
11995 s = mips_opts_stack;
11996 if (s == NULL)
11997 as_bad (_(".set pop with no .set push"));
11998 else
11999 {
12000 /* If we're changing the reorder mode we need to handle
12001 delay slots correctly. */
12002 if (s->options.noreorder && ! mips_opts.noreorder)
b34976b6 12003 mips_emit_delays (TRUE);
252b5132
RH
12004 else if (! s->options.noreorder && mips_opts.noreorder)
12005 {
12006 if (prev_nop_frag != NULL)
12007 {
12008 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
12009 * (mips_opts.mips16 ? 2 : 4));
12010 prev_nop_frag = NULL;
12011 }
12012 }
12013
12014 mips_opts = s->options;
12015 mips_opts_stack = s->next;
12016 free (s);
12017 }
12018 }
12019 else
12020 {
12021 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
12022 }
12023 *input_line_pointer = ch;
12024 demand_empty_rest_of_line ();
12025}
12026
12027/* Handle the .abicalls pseudo-op. I believe this is equivalent to
12028 .option pic2. It means to generate SVR4 PIC calls. */
12029
12030static void
12031s_abicalls (ignore)
43841e91 12032 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
12033{
12034 mips_pic = SVR4_PIC;
12035 if (USE_GLOBAL_POINTER_OPT)
12036 {
12037 if (g_switch_seen && g_switch_value != 0)
12038 as_warn (_("-G may not be used with SVR4 PIC code"));
12039 g_switch_value = 0;
12040 }
12041 bfd_set_gp_size (stdoutput, 0);
12042 demand_empty_rest_of_line ();
12043}
12044
12045/* Handle the .cpload pseudo-op. This is used when generating SVR4
12046 PIC code. It sets the $gp register for the function based on the
12047 function address, which is in the register named in the argument.
12048 This uses a relocation against _gp_disp, which is handled specially
12049 by the linker. The result is:
12050 lui $gp,%hi(_gp_disp)
12051 addiu $gp,$gp,%lo(_gp_disp)
12052 addu $gp,$gp,.cpload argument
12053 The .cpload argument is normally $25 == $t9. */
12054
12055static void
12056s_cpload (ignore)
43841e91 12057 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
12058{
12059 expressionS ex;
12060 int icnt = 0;
12061
6478892d
TS
12062 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
12063 .cpload is ignored. */
12064 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
12065 {
12066 s_ignore (0);
12067 return;
12068 }
12069
d3ecfc59 12070 /* .cpload should be in a .set noreorder section. */
252b5132
RH
12071 if (mips_opts.noreorder == 0)
12072 as_warn (_(".cpload not in noreorder section"));
12073
12074 ex.X_op = O_symbol;
12075 ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
12076 ex.X_op_symbol = NULL;
12077 ex.X_add_number = 0;
12078
12079 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
49309057 12080 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
252b5132 12081
c9914766
TS
12082 macro_build_lui (NULL, &icnt, &ex, mips_gp_register);
12083 macro_build ((char *) NULL, &icnt, &ex, "addiu", "t,r,j",
12084 mips_gp_register, mips_gp_register, (int) BFD_RELOC_LO16);
252b5132
RH
12085
12086 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "addu", "d,v,t",
c9914766 12087 mips_gp_register, mips_gp_register, tc_get_register (0));
252b5132
RH
12088
12089 demand_empty_rest_of_line ();
12090}
12091
6478892d
TS
12092/* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
12093 .cpsetup $reg1, offset|$reg2, label
12094
12095 If offset is given, this results in:
12096 sd $gp, offset($sp)
956cd1d6 12097 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
12098 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
12099 daddu $gp, $gp, $reg1
6478892d
TS
12100
12101 If $reg2 is given, this results in:
12102 daddu $reg2, $gp, $0
956cd1d6 12103 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
12104 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
12105 daddu $gp, $gp, $reg1
12106 $reg1 is normally $25 == $t9. */
6478892d
TS
12107static void
12108s_cpsetup (ignore)
12109 int ignore ATTRIBUTE_UNUSED;
12110{
12111 expressionS ex_off;
12112 expressionS ex_sym;
12113 int reg1;
12114 int icnt = 0;
f21f8242 12115 char *f;
6478892d 12116
8586fc66 12117 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
6478892d
TS
12118 We also need NewABI support. */
12119 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12120 {
12121 s_ignore (0);
12122 return;
12123 }
12124
12125 reg1 = tc_get_register (0);
12126 SKIP_WHITESPACE ();
12127 if (*input_line_pointer != ',')
12128 {
12129 as_bad (_("missing argument separator ',' for .cpsetup"));
12130 return;
12131 }
12132 else
80245285 12133 ++input_line_pointer;
6478892d
TS
12134 SKIP_WHITESPACE ();
12135 if (*input_line_pointer == '$')
80245285
TS
12136 {
12137 mips_cpreturn_register = tc_get_register (0);
12138 mips_cpreturn_offset = -1;
12139 }
6478892d 12140 else
80245285
TS
12141 {
12142 mips_cpreturn_offset = get_absolute_expression ();
12143 mips_cpreturn_register = -1;
12144 }
6478892d
TS
12145 SKIP_WHITESPACE ();
12146 if (*input_line_pointer != ',')
12147 {
12148 as_bad (_("missing argument separator ',' for .cpsetup"));
12149 return;
12150 }
12151 else
f9419b05 12152 ++input_line_pointer;
6478892d 12153 SKIP_WHITESPACE ();
f21f8242 12154 expression (&ex_sym);
6478892d
TS
12155
12156 if (mips_cpreturn_register == -1)
12157 {
12158 ex_off.X_op = O_constant;
12159 ex_off.X_add_symbol = NULL;
12160 ex_off.X_op_symbol = NULL;
12161 ex_off.X_add_number = mips_cpreturn_offset;
12162
12163 macro_build ((char *) NULL, &icnt, &ex_off, "sd", "t,o(b)",
12164 mips_gp_register, (int) BFD_RELOC_LO16, SP);
12165 }
12166 else
12167 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
12168 "d,v,t", mips_cpreturn_register, mips_gp_register, 0);
12169
f21f8242
AO
12170 /* Ensure there's room for the next two instructions, so that `f'
12171 doesn't end up with an address in the wrong frag. */
12172 frag_grow (8);
12173 f = frag_more (0);
6478892d
TS
12174 macro_build ((char *) NULL, &icnt, &ex_sym, "lui", "t,u", mips_gp_register,
12175 (int) BFD_RELOC_GPREL16);
f21f8242
AO
12176 fix_new (frag_now, f - frag_now->fr_literal,
12177 0, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
12178 fix_new (frag_now, f - frag_now->fr_literal,
12179 0, NULL, 0, 0, BFD_RELOC_HI16_S);
12180
12181 f = frag_more (0);
6478892d
TS
12182 macro_build ((char *) NULL, &icnt, &ex_sym, "addiu", "t,r,j",
12183 mips_gp_register, mips_gp_register, (int) BFD_RELOC_GPREL16);
f21f8242
AO
12184 fix_new (frag_now, f - frag_now->fr_literal,
12185 0, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
12186 fix_new (frag_now, f - frag_now->fr_literal,
12187 0, NULL, 0, 0, BFD_RELOC_LO16);
12188
8586fc66
TS
12189 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
12190 HAVE_64BIT_ADDRESSES ? "daddu" : "addu", "d,v,t",
12191 mips_gp_register, mips_gp_register, reg1);
6478892d
TS
12192
12193 demand_empty_rest_of_line ();
12194}
12195
12196static void
12197s_cplocal (ignore)
12198 int ignore ATTRIBUTE_UNUSED;
12199{
12200 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
12201 .cplocal is ignored. */
12202 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12203 {
12204 s_ignore (0);
12205 return;
12206 }
12207
12208 mips_gp_register = tc_get_register (0);
85b51719 12209 demand_empty_rest_of_line ();
6478892d
TS
12210}
12211
252b5132
RH
12212/* Handle the .cprestore pseudo-op. This stores $gp into a given
12213 offset from $sp. The offset is remembered, and after making a PIC
12214 call $gp is restored from that location. */
12215
12216static void
12217s_cprestore (ignore)
43841e91 12218 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
12219{
12220 expressionS ex;
12221 int icnt = 0;
12222
6478892d 12223 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
c9914766 12224 .cprestore is ignored. */
6478892d 12225 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
12226 {
12227 s_ignore (0);
12228 return;
12229 }
12230
12231 mips_cprestore_offset = get_absolute_expression ();
7a621144 12232 mips_cprestore_valid = 1;
252b5132
RH
12233
12234 ex.X_op = O_constant;
12235 ex.X_add_symbol = NULL;
12236 ex.X_op_symbol = NULL;
12237 ex.X_add_number = mips_cprestore_offset;
12238
885add95
CD
12239 macro_build_ldst_constoffset ((char *) NULL, &icnt, &ex,
12240 HAVE_32BIT_ADDRESSES ? "sw" : "sd",
12241 mips_gp_register, SP);
252b5132
RH
12242
12243 demand_empty_rest_of_line ();
12244}
12245
6478892d
TS
12246/* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
12247 was given in the preceeding .gpsetup, it results in:
12248 ld $gp, offset($sp)
76b3015f 12249
6478892d
TS
12250 If a register $reg2 was given there, it results in:
12251 daddiu $gp, $gp, $reg2
12252 */
12253static void
12254s_cpreturn (ignore)
12255 int ignore ATTRIBUTE_UNUSED;
12256{
12257 expressionS ex;
12258 int icnt = 0;
12259
12260 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
12261 We also need NewABI support. */
12262 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12263 {
12264 s_ignore (0);
12265 return;
12266 }
12267
12268 if (mips_cpreturn_register == -1)
12269 {
12270 ex.X_op = O_constant;
12271 ex.X_add_symbol = NULL;
12272 ex.X_op_symbol = NULL;
12273 ex.X_add_number = mips_cpreturn_offset;
12274
12275 macro_build ((char *) NULL, &icnt, &ex, "ld", "t,o(b)",
12276 mips_gp_register, (int) BFD_RELOC_LO16, SP);
12277 }
12278 else
12279 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
12280 "d,v,t", mips_gp_register, mips_cpreturn_register, 0);
12281
12282 demand_empty_rest_of_line ();
12283}
12284
12285/* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
12286 code. It sets the offset to use in gp_rel relocations. */
12287
12288static void
12289s_gpvalue (ignore)
12290 int ignore ATTRIBUTE_UNUSED;
12291{
12292 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
12293 We also need NewABI support. */
12294 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12295 {
12296 s_ignore (0);
12297 return;
12298 }
12299
def2e0dd 12300 mips_gprel_offset = get_absolute_expression ();
6478892d
TS
12301
12302 demand_empty_rest_of_line ();
12303}
12304
252b5132
RH
12305/* Handle the .gpword pseudo-op. This is used when generating PIC
12306 code. It generates a 32 bit GP relative reloc. */
12307
12308static void
12309s_gpword (ignore)
43841e91 12310 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
12311{
12312 symbolS *label;
12313 expressionS ex;
12314 char *p;
12315
12316 /* When not generating PIC code, this is treated as .word. */
12317 if (mips_pic != SVR4_PIC)
12318 {
12319 s_cons (2);
12320 return;
12321 }
12322
12323 label = insn_labels != NULL ? insn_labels->label : NULL;
b34976b6 12324 mips_emit_delays (TRUE);
252b5132
RH
12325 if (auto_align)
12326 mips_align (2, 0, label);
12327 mips_clear_insn_labels ();
12328
12329 expression (&ex);
12330
12331 if (ex.X_op != O_symbol || ex.X_add_number != 0)
12332 {
12333 as_bad (_("Unsupported use of .gpword"));
12334 ignore_rest_of_line ();
12335 }
12336
12337 p = frag_more (4);
12338 md_number_to_chars (p, (valueT) 0, 4);
b34976b6 12339 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
cdf6fd85 12340 BFD_RELOC_GPREL32);
252b5132
RH
12341
12342 demand_empty_rest_of_line ();
12343}
12344
10181a0d
AO
12345static void
12346s_gpdword (ignore)
12347 int ignore ATTRIBUTE_UNUSED;
12348{
12349 symbolS *label;
12350 expressionS ex;
12351 char *p;
12352
12353 /* When not generating PIC code, this is treated as .dword. */
12354 if (mips_pic != SVR4_PIC)
12355 {
12356 s_cons (3);
12357 return;
12358 }
12359
12360 label = insn_labels != NULL ? insn_labels->label : NULL;
b34976b6 12361 mips_emit_delays (TRUE);
10181a0d
AO
12362 if (auto_align)
12363 mips_align (3, 0, label);
12364 mips_clear_insn_labels ();
12365
12366 expression (&ex);
12367
12368 if (ex.X_op != O_symbol || ex.X_add_number != 0)
12369 {
12370 as_bad (_("Unsupported use of .gpdword"));
12371 ignore_rest_of_line ();
12372 }
12373
12374 p = frag_more (8);
12375 md_number_to_chars (p, (valueT) 0, 8);
b34976b6 12376 fix_new_exp (frag_now, p - frag_now->fr_literal, 8, &ex, FALSE,
10181a0d
AO
12377 BFD_RELOC_GPREL32);
12378
12379 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
12380 ex.X_op = O_absent;
12381 ex.X_add_symbol = 0;
12382 ex.X_add_number = 0;
b34976b6 12383 fix_new_exp (frag_now, p - frag_now->fr_literal, 8, &ex, FALSE,
10181a0d
AO
12384 BFD_RELOC_64);
12385
12386 demand_empty_rest_of_line ();
12387}
12388
252b5132
RH
12389/* Handle the .cpadd pseudo-op. This is used when dealing with switch
12390 tables in SVR4 PIC code. */
12391
12392static void
12393s_cpadd (ignore)
43841e91 12394 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
12395{
12396 int icnt = 0;
12397 int reg;
12398
10181a0d
AO
12399 /* This is ignored when not generating SVR4 PIC code. */
12400 if (mips_pic != SVR4_PIC)
252b5132
RH
12401 {
12402 s_ignore (0);
12403 return;
12404 }
12405
12406 /* Add $gp to the register named as an argument. */
12407 reg = tc_get_register (0);
12408 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 12409 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 12410 "d,v,t", reg, reg, mips_gp_register);
252b5132 12411
bdaaa2e1 12412 demand_empty_rest_of_line ();
252b5132
RH
12413}
12414
12415/* Handle the .insn pseudo-op. This marks instruction labels in
12416 mips16 mode. This permits the linker to handle them specially,
12417 such as generating jalx instructions when needed. We also make
12418 them odd for the duration of the assembly, in order to generate the
12419 right sort of code. We will make them even in the adjust_symtab
12420 routine, while leaving them marked. This is convenient for the
12421 debugger and the disassembler. The linker knows to make them odd
12422 again. */
12423
12424static void
12425s_insn (ignore)
43841e91 12426 int ignore ATTRIBUTE_UNUSED;
252b5132 12427{
f9419b05 12428 mips16_mark_labels ();
252b5132
RH
12429
12430 demand_empty_rest_of_line ();
12431}
12432
12433/* Handle a .stabn directive. We need these in order to mark a label
12434 as being a mips16 text label correctly. Sometimes the compiler
12435 will emit a label, followed by a .stabn, and then switch sections.
12436 If the label and .stabn are in mips16 mode, then the label is
12437 really a mips16 text label. */
12438
12439static void
12440s_mips_stab (type)
12441 int type;
12442{
f9419b05 12443 if (type == 'n')
252b5132
RH
12444 mips16_mark_labels ();
12445
12446 s_stab (type);
12447}
12448
12449/* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
12450 */
12451
12452static void
12453s_mips_weakext (ignore)
43841e91 12454 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
12455{
12456 char *name;
12457 int c;
12458 symbolS *symbolP;
12459 expressionS exp;
12460
12461 name = input_line_pointer;
12462 c = get_symbol_end ();
12463 symbolP = symbol_find_or_make (name);
12464 S_SET_WEAK (symbolP);
12465 *input_line_pointer = c;
12466
12467 SKIP_WHITESPACE ();
12468
12469 if (! is_end_of_line[(unsigned char) *input_line_pointer])
12470 {
12471 if (S_IS_DEFINED (symbolP))
12472 {
956cd1d6 12473 as_bad ("ignoring attempt to redefine symbol %s",
252b5132
RH
12474 S_GET_NAME (symbolP));
12475 ignore_rest_of_line ();
12476 return;
12477 }
bdaaa2e1 12478
252b5132
RH
12479 if (*input_line_pointer == ',')
12480 {
12481 ++input_line_pointer;
12482 SKIP_WHITESPACE ();
12483 }
bdaaa2e1 12484
252b5132
RH
12485 expression (&exp);
12486 if (exp.X_op != O_symbol)
12487 {
12488 as_bad ("bad .weakext directive");
98d3f06f 12489 ignore_rest_of_line ();
252b5132
RH
12490 return;
12491 }
49309057 12492 symbol_set_value_expression (symbolP, &exp);
252b5132
RH
12493 }
12494
12495 demand_empty_rest_of_line ();
12496}
12497
12498/* Parse a register string into a number. Called from the ECOFF code
12499 to parse .frame. The argument is non-zero if this is the frame
12500 register, so that we can record it in mips_frame_reg. */
12501
12502int
12503tc_get_register (frame)
12504 int frame;
12505{
12506 int reg;
12507
12508 SKIP_WHITESPACE ();
12509 if (*input_line_pointer++ != '$')
12510 {
12511 as_warn (_("expected `$'"));
85b51719 12512 reg = ZERO;
252b5132 12513 }
3882b010 12514 else if (ISDIGIT (*input_line_pointer))
252b5132
RH
12515 {
12516 reg = get_absolute_expression ();
12517 if (reg < 0 || reg >= 32)
12518 {
12519 as_warn (_("Bad register number"));
85b51719 12520 reg = ZERO;
252b5132
RH
12521 }
12522 }
12523 else
12524 {
76db943d 12525 if (strncmp (input_line_pointer, "ra", 2) == 0)
85b51719
TS
12526 {
12527 reg = RA;
12528 input_line_pointer += 2;
12529 }
76db943d 12530 else if (strncmp (input_line_pointer, "fp", 2) == 0)
85b51719
TS
12531 {
12532 reg = FP;
12533 input_line_pointer += 2;
12534 }
252b5132 12535 else if (strncmp (input_line_pointer, "sp", 2) == 0)
85b51719
TS
12536 {
12537 reg = SP;
12538 input_line_pointer += 2;
12539 }
252b5132 12540 else if (strncmp (input_line_pointer, "gp", 2) == 0)
85b51719
TS
12541 {
12542 reg = GP;
12543 input_line_pointer += 2;
12544 }
252b5132 12545 else if (strncmp (input_line_pointer, "at", 2) == 0)
85b51719
TS
12546 {
12547 reg = AT;
12548 input_line_pointer += 2;
12549 }
12550 else if (strncmp (input_line_pointer, "kt0", 3) == 0)
12551 {
12552 reg = KT0;
12553 input_line_pointer += 3;
12554 }
12555 else if (strncmp (input_line_pointer, "kt1", 3) == 0)
12556 {
12557 reg = KT1;
12558 input_line_pointer += 3;
12559 }
12560 else if (strncmp (input_line_pointer, "zero", 4) == 0)
12561 {
12562 reg = ZERO;
12563 input_line_pointer += 4;
12564 }
252b5132
RH
12565 else
12566 {
12567 as_warn (_("Unrecognized register name"));
85b51719
TS
12568 reg = ZERO;
12569 while (ISALNUM(*input_line_pointer))
12570 input_line_pointer++;
252b5132 12571 }
252b5132
RH
12572 }
12573 if (frame)
7a621144
DJ
12574 {
12575 mips_frame_reg = reg != 0 ? reg : SP;
12576 mips_frame_reg_valid = 1;
12577 mips_cprestore_valid = 0;
12578 }
252b5132
RH
12579 return reg;
12580}
12581
12582valueT
12583md_section_align (seg, addr)
12584 asection *seg;
12585 valueT addr;
12586{
12587 int align = bfd_get_section_alignment (stdoutput, seg);
12588
12589#ifdef OBJ_ELF
12590 /* We don't need to align ELF sections to the full alignment.
12591 However, Irix 5 may prefer that we align them at least to a 16
12592 byte boundary. We don't bother to align the sections if we are
12593 targeted for an embedded system. */
12594 if (strcmp (TARGET_OS, "elf") == 0)
12595 return addr;
12596 if (align > 4)
12597 align = 4;
12598#endif
12599
12600 return ((addr + (1 << align) - 1) & (-1 << align));
12601}
12602
12603/* Utility routine, called from above as well. If called while the
12604 input file is still being read, it's only an approximation. (For
12605 example, a symbol may later become defined which appeared to be
12606 undefined earlier.) */
12607
12608static int
12609nopic_need_relax (sym, before_relaxing)
12610 symbolS *sym;
12611 int before_relaxing;
12612{
12613 if (sym == 0)
12614 return 0;
12615
6478892d 12616 if (USE_GLOBAL_POINTER_OPT && g_switch_value > 0)
252b5132
RH
12617 {
12618 const char *symname;
12619 int change;
12620
c9914766 12621 /* Find out whether this symbol can be referenced off the $gp
252b5132
RH
12622 register. It can be if it is smaller than the -G size or if
12623 it is in the .sdata or .sbss section. Certain symbols can
c9914766 12624 not be referenced off the $gp, although it appears as though
252b5132
RH
12625 they can. */
12626 symname = S_GET_NAME (sym);
12627 if (symname != (const char *) NULL
12628 && (strcmp (symname, "eprol") == 0
12629 || strcmp (symname, "etext") == 0
12630 || strcmp (symname, "_gp") == 0
12631 || strcmp (symname, "edata") == 0
12632 || strcmp (symname, "_fbss") == 0
12633 || strcmp (symname, "_fdata") == 0
12634 || strcmp (symname, "_ftext") == 0
12635 || strcmp (symname, "end") == 0
12636 || strcmp (symname, "_gp_disp") == 0))
12637 change = 1;
12638 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
12639 && (0
12640#ifndef NO_ECOFF_DEBUGGING
49309057
ILT
12641 || (symbol_get_obj (sym)->ecoff_extern_size != 0
12642 && (symbol_get_obj (sym)->ecoff_extern_size
12643 <= g_switch_value))
252b5132
RH
12644#endif
12645 /* We must defer this decision until after the whole
12646 file has been read, since there might be a .extern
12647 after the first use of this symbol. */
12648 || (before_relaxing
12649#ifndef NO_ECOFF_DEBUGGING
49309057 12650 && symbol_get_obj (sym)->ecoff_extern_size == 0
252b5132
RH
12651#endif
12652 && S_GET_VALUE (sym) == 0)
12653 || (S_GET_VALUE (sym) != 0
12654 && S_GET_VALUE (sym) <= g_switch_value)))
12655 change = 0;
12656 else
12657 {
12658 const char *segname;
12659
12660 segname = segment_name (S_GET_SEGMENT (sym));
12661 assert (strcmp (segname, ".lit8") != 0
12662 && strcmp (segname, ".lit4") != 0);
12663 change = (strcmp (segname, ".sdata") != 0
fba2b7f9
GK
12664 && strcmp (segname, ".sbss") != 0
12665 && strncmp (segname, ".sdata.", 7) != 0
12666 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
252b5132
RH
12667 }
12668 return change;
12669 }
12670 else
c9914766 12671 /* We are not optimizing for the $gp register. */
252b5132
RH
12672 return 1;
12673}
12674
12675/* Given a mips16 variant frag FRAGP, return non-zero if it needs an
12676 extended opcode. SEC is the section the frag is in. */
12677
12678static int
12679mips16_extended_frag (fragp, sec, stretch)
12680 fragS *fragp;
12681 asection *sec;
12682 long stretch;
12683{
12684 int type;
12685 register const struct mips16_immed_operand *op;
12686 offsetT val;
12687 int mintiny, maxtiny;
12688 segT symsec;
98aa84af 12689 fragS *sym_frag;
252b5132
RH
12690
12691 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
12692 return 0;
12693 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
12694 return 1;
12695
12696 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12697 op = mips16_immed_operands;
12698 while (op->type != type)
12699 {
12700 ++op;
12701 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
12702 }
12703
12704 if (op->unsp)
12705 {
12706 if (type == '<' || type == '>' || type == '[' || type == ']')
12707 {
12708 mintiny = 1;
12709 maxtiny = 1 << op->nbits;
12710 }
12711 else
12712 {
12713 mintiny = 0;
12714 maxtiny = (1 << op->nbits) - 1;
12715 }
12716 }
12717 else
12718 {
12719 mintiny = - (1 << (op->nbits - 1));
12720 maxtiny = (1 << (op->nbits - 1)) - 1;
12721 }
12722
98aa84af 12723 sym_frag = symbol_get_frag (fragp->fr_symbol);
ac62c346 12724 val = S_GET_VALUE (fragp->fr_symbol);
98aa84af 12725 symsec = S_GET_SEGMENT (fragp->fr_symbol);
252b5132
RH
12726
12727 if (op->pcrel)
12728 {
12729 addressT addr;
12730
12731 /* We won't have the section when we are called from
12732 mips_relax_frag. However, we will always have been called
12733 from md_estimate_size_before_relax first. If this is a
12734 branch to a different section, we mark it as such. If SEC is
12735 NULL, and the frag is not marked, then it must be a branch to
12736 the same section. */
12737 if (sec == NULL)
12738 {
12739 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
12740 return 1;
12741 }
12742 else
12743 {
98aa84af 12744 /* Must have been called from md_estimate_size_before_relax. */
252b5132
RH
12745 if (symsec != sec)
12746 {
12747 fragp->fr_subtype =
12748 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12749
12750 /* FIXME: We should support this, and let the linker
12751 catch branches and loads that are out of range. */
12752 as_bad_where (fragp->fr_file, fragp->fr_line,
12753 _("unsupported PC relative reference to different section"));
12754
12755 return 1;
12756 }
98aa84af
AM
12757 if (fragp != sym_frag && sym_frag->fr_address == 0)
12758 /* Assume non-extended on the first relaxation pass.
12759 The address we have calculated will be bogus if this is
12760 a forward branch to another frag, as the forward frag
12761 will have fr_address == 0. */
12762 return 0;
252b5132
RH
12763 }
12764
12765 /* In this case, we know for sure that the symbol fragment is in
98aa84af
AM
12766 the same section. If the relax_marker of the symbol fragment
12767 differs from the relax_marker of this fragment, we have not
12768 yet adjusted the symbol fragment fr_address. We want to add
252b5132
RH
12769 in STRETCH in order to get a better estimate of the address.
12770 This particularly matters because of the shift bits. */
12771 if (stretch != 0
98aa84af 12772 && sym_frag->relax_marker != fragp->relax_marker)
252b5132
RH
12773 {
12774 fragS *f;
12775
12776 /* Adjust stretch for any alignment frag. Note that if have
12777 been expanding the earlier code, the symbol may be
12778 defined in what appears to be an earlier frag. FIXME:
12779 This doesn't handle the fr_subtype field, which specifies
12780 a maximum number of bytes to skip when doing an
12781 alignment. */
98aa84af 12782 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
252b5132
RH
12783 {
12784 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
12785 {
12786 if (stretch < 0)
12787 stretch = - ((- stretch)
12788 & ~ ((1 << (int) f->fr_offset) - 1));
12789 else
12790 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
12791 if (stretch == 0)
12792 break;
12793 }
12794 }
12795 if (f != NULL)
12796 val += stretch;
12797 }
12798
12799 addr = fragp->fr_address + fragp->fr_fix;
12800
12801 /* The base address rules are complicated. The base address of
12802 a branch is the following instruction. The base address of a
12803 PC relative load or add is the instruction itself, but if it
12804 is in a delay slot (in which case it can not be extended) use
12805 the address of the instruction whose delay slot it is in. */
12806 if (type == 'p' || type == 'q')
12807 {
12808 addr += 2;
12809
12810 /* If we are currently assuming that this frag should be
12811 extended, then, the current address is two bytes
bdaaa2e1 12812 higher. */
252b5132
RH
12813 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12814 addr += 2;
12815
12816 /* Ignore the low bit in the target, since it will be set
12817 for a text label. */
12818 if ((val & 1) != 0)
12819 --val;
12820 }
12821 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12822 addr -= 4;
12823 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12824 addr -= 2;
12825
12826 val -= addr & ~ ((1 << op->shift) - 1);
12827
12828 /* Branch offsets have an implicit 0 in the lowest bit. */
12829 if (type == 'p' || type == 'q')
12830 val /= 2;
12831
12832 /* If any of the shifted bits are set, we must use an extended
12833 opcode. If the address depends on the size of this
12834 instruction, this can lead to a loop, so we arrange to always
12835 use an extended opcode. We only check this when we are in
12836 the main relaxation loop, when SEC is NULL. */
12837 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
12838 {
12839 fragp->fr_subtype =
12840 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12841 return 1;
12842 }
12843
12844 /* If we are about to mark a frag as extended because the value
12845 is precisely maxtiny + 1, then there is a chance of an
12846 infinite loop as in the following code:
12847 la $4,foo
12848 .skip 1020
12849 .align 2
12850 foo:
12851 In this case when the la is extended, foo is 0x3fc bytes
12852 away, so the la can be shrunk, but then foo is 0x400 away, so
12853 the la must be extended. To avoid this loop, we mark the
12854 frag as extended if it was small, and is about to become
12855 extended with a value of maxtiny + 1. */
12856 if (val == ((maxtiny + 1) << op->shift)
12857 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
12858 && sec == NULL)
12859 {
12860 fragp->fr_subtype =
12861 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12862 return 1;
12863 }
12864 }
12865 else if (symsec != absolute_section && sec != NULL)
12866 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
12867
12868 if ((val & ((1 << op->shift) - 1)) != 0
12869 || val < (mintiny << op->shift)
12870 || val > (maxtiny << op->shift))
12871 return 1;
12872 else
12873 return 0;
12874}
12875
4a6a3df4
AO
12876/* Compute the length of a branch sequence, and adjust the
12877 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
12878 worst-case length is computed, with UPDATE being used to indicate
12879 whether an unconditional (-1), branch-likely (+1) or regular (0)
12880 branch is to be computed. */
12881static int
12882relaxed_branch_length (fragp, sec, update)
12883 fragS *fragp;
12884 asection *sec;
12885 int update;
12886{
b34976b6 12887 bfd_boolean toofar;
4a6a3df4
AO
12888 int length;
12889
12890 if (fragp
12891 && S_IS_DEFINED (fragp->fr_symbol)
12892 && sec == S_GET_SEGMENT (fragp->fr_symbol))
12893 {
12894 addressT addr;
12895 offsetT val;
12896
12897 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
12898
12899 addr = fragp->fr_address + fragp->fr_fix + 4;
12900
12901 val -= addr;
12902
12903 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
12904 }
12905 else if (fragp)
12906 /* If the symbol is not defined or it's in a different segment,
12907 assume the user knows what's going on and emit a short
12908 branch. */
b34976b6 12909 toofar = FALSE;
4a6a3df4 12910 else
b34976b6 12911 toofar = TRUE;
4a6a3df4
AO
12912
12913 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
12914 fragp->fr_subtype
af6ae2ad 12915 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_UNCOND (fragp->fr_subtype),
4a6a3df4
AO
12916 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
12917 RELAX_BRANCH_LINK (fragp->fr_subtype),
12918 toofar);
12919
12920 length = 4;
12921 if (toofar)
12922 {
12923 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
12924 length += 8;
12925
12926 if (mips_pic != NO_PIC)
12927 {
12928 /* Additional space for PIC loading of target address. */
12929 length += 8;
12930 if (mips_opts.isa == ISA_MIPS1)
12931 /* Additional space for $at-stabilizing nop. */
12932 length += 4;
12933 }
12934
12935 /* If branch is conditional. */
12936 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
12937 length += 8;
12938 }
b34976b6 12939
4a6a3df4
AO
12940 return length;
12941}
12942
252b5132
RH
12943/* Estimate the size of a frag before relaxing. Unless this is the
12944 mips16, we are not really relaxing here, and the final size is
12945 encoded in the subtype information. For the mips16, we have to
12946 decide whether we are using an extended opcode or not. */
12947
252b5132
RH
12948int
12949md_estimate_size_before_relax (fragp, segtype)
12950 fragS *fragp;
12951 asection *segtype;
12952{
43841e91 12953 int change = 0;
b34976b6 12954 bfd_boolean linkonce = FALSE;
252b5132 12955
4a6a3df4
AO
12956 if (RELAX_BRANCH_P (fragp->fr_subtype))
12957 {
12958
b34976b6
AM
12959 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
12960
4a6a3df4
AO
12961 return fragp->fr_var;
12962 }
12963
252b5132 12964 if (RELAX_MIPS16_P (fragp->fr_subtype))
177b4a6a
AO
12965 /* We don't want to modify the EXTENDED bit here; it might get us
12966 into infinite loops. We change it only in mips_relax_frag(). */
12967 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
252b5132
RH
12968
12969 if (mips_pic == NO_PIC)
12970 {
12971 change = nopic_need_relax (fragp->fr_symbol, 0);
12972 }
12973 else if (mips_pic == SVR4_PIC)
12974 {
12975 symbolS *sym;
12976 asection *symsec;
12977
12978 sym = fragp->fr_symbol;
12979
12980 /* Handle the case of a symbol equated to another symbol. */
e0890092 12981 while (symbol_equated_reloc_p (sym))
252b5132
RH
12982 {
12983 symbolS *n;
12984
12985 /* It's possible to get a loop here in a badly written
12986 program. */
49309057 12987 n = symbol_get_value_expression (sym)->X_add_symbol;
252b5132
RH
12988 if (n == sym)
12989 break;
12990 sym = n;
12991 }
12992
12993 symsec = S_GET_SEGMENT (sym);
12994
8614eeee
UC
12995 /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
12996 if (symsec != segtype && ! S_IS_LOCAL (sym))
beae10d5
KH
12997 {
12998 if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
12999 != 0)
b34976b6 13000 linkonce = TRUE;
beae10d5
KH
13001
13002 /* The GNU toolchain uses an extension for ELF: a section
13003 beginning with the magic string .gnu.linkonce is a linkonce
13004 section. */
13005 if (strncmp (segment_name (symsec), ".gnu.linkonce",
13006 sizeof ".gnu.linkonce" - 1) == 0)
b34976b6 13007 linkonce = TRUE;
beae10d5 13008 }
8614eeee 13009
252b5132
RH
13010 /* This must duplicate the test in adjust_reloc_syms. */
13011 change = (symsec != &bfd_und_section
13012 && symsec != &bfd_abs_section
426b0403 13013 && ! bfd_is_com_section (symsec)
8614eeee 13014 && !linkonce
426b0403 13015#ifdef OBJ_ELF
ea4ff978 13016 /* A global or weak symbol is treated as external. */
9151e8bf 13017 && (OUTPUT_FLAVOR != bfd_target_elf_flavour
b25a253c 13018 || (! S_IS_WEAK (sym)
bad9ca53
TS
13019 && (! S_IS_EXTERNAL (sym)
13020 || mips_pic == EMBEDDED_PIC)))
426b0403
AM
13021#endif
13022 );
252b5132
RH
13023 }
13024 else
13025 abort ();
13026
13027 if (change)
13028 {
13029 /* Record the offset to the first reloc in the fr_opcode field.
13030 This lets md_convert_frag and tc_gen_reloc know that the code
13031 must be expanded. */
13032 fragp->fr_opcode = (fragp->fr_literal
13033 + fragp->fr_fix
13034 - RELAX_OLD (fragp->fr_subtype)
13035 + RELAX_RELOC1 (fragp->fr_subtype));
13036 /* FIXME: This really needs as_warn_where. */
13037 if (RELAX_WARN (fragp->fr_subtype))
9a41af64
TS
13038 as_warn (_("AT used after \".set noat\" or macro used after "
13039 "\".set nomacro\""));
13040
13041 return RELAX_NEW (fragp->fr_subtype) - RELAX_OLD (fragp->fr_subtype);
252b5132
RH
13042 }
13043
9a41af64 13044 return 0;
252b5132
RH
13045}
13046
13047/* This is called to see whether a reloc against a defined symbol
13048 should be converted into a reloc against a section. Don't adjust
13049 MIPS16 jump relocations, so we don't have to worry about the format
13050 of the offset in the .o file. Don't adjust relocations against
13051 mips16 symbols, so that the linker can find them if it needs to set
13052 up a stub. */
13053
13054int
13055mips_fix_adjustable (fixp)
13056 fixS *fixp;
13057{
13058 if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
13059 return 0;
a161fe53 13060
252b5132
RH
13061 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
13062 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
13063 return 0;
a161fe53 13064
252b5132
RH
13065 if (fixp->fx_addsy == NULL)
13066 return 1;
a161fe53 13067
252b5132
RH
13068#ifdef OBJ_ELF
13069 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
13070 && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
13071 && fixp->fx_subsy == NULL)
13072 return 0;
13073#endif
a161fe53 13074
252b5132
RH
13075 return 1;
13076}
13077
13078/* Translate internal representation of relocation info to BFD target
13079 format. */
13080
13081arelent **
13082tc_gen_reloc (section, fixp)
43841e91 13083 asection *section ATTRIBUTE_UNUSED;
252b5132
RH
13084 fixS *fixp;
13085{
13086 static arelent *retval[4];
13087 arelent *reloc;
13088 bfd_reloc_code_real_type code;
13089
13090 reloc = retval[0] = (arelent *) xmalloc (sizeof (arelent));
13091 retval[1] = NULL;
13092
49309057
ILT
13093 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13094 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
13095 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
13096
13097 if (mips_pic == EMBEDDED_PIC
13098 && SWITCH_TABLE (fixp))
13099 {
13100 /* For a switch table entry we use a special reloc. The addend
13101 is actually the difference between the reloc address and the
13102 subtrahend. */
13103 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
13104 if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
13105 as_fatal (_("Double check fx_r_type in tc-mips.c:tc_gen_reloc"));
13106 fixp->fx_r_type = BFD_RELOC_GPREL32;
13107 }
13108 else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16)
13109 {
4514d474
CD
13110 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
13111 reloc->addend = fixp->fx_addnumber;
252b5132 13112 else
4514d474
CD
13113 {
13114 /* We use a special addend for an internal RELLO reloc. */
13115 if (symbol_section_p (fixp->fx_addsy))
13116 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
13117 else
13118 reloc->addend = fixp->fx_addnumber + reloc->address;
13119 }
252b5132
RH
13120 }
13121 else if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
13122 {
13123 assert (fixp->fx_next != NULL
13124 && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
4514d474
CD
13125
13126 /* The reloc is relative to the RELLO; adjust the addend
252b5132 13127 accordingly. */
4514d474
CD
13128 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
13129 reloc->addend = fixp->fx_next->fx_addnumber;
252b5132 13130 else
4514d474
CD
13131 {
13132 /* We use a special addend for an internal RELHI reloc. */
13133 if (symbol_section_p (fixp->fx_addsy))
13134 reloc->addend = (fixp->fx_next->fx_frag->fr_address
13135 + fixp->fx_next->fx_where
13136 - S_GET_VALUE (fixp->fx_subsy));
13137 else
13138 reloc->addend = (fixp->fx_addnumber
13139 + fixp->fx_next->fx_frag->fr_address
13140 + fixp->fx_next->fx_where);
13141 }
252b5132 13142 }
4514d474
CD
13143 else if (fixp->fx_pcrel == 0 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
13144 reloc->addend = fixp->fx_addnumber;
252b5132
RH
13145 else
13146 {
13147 if (OUTPUT_FLAVOR != bfd_target_aout_flavour)
13148 /* A gruesome hack which is a result of the gruesome gas reloc
13149 handling. */
13150 reloc->addend = reloc->address;
13151 else
13152 reloc->addend = -reloc->address;
13153 }
13154
13155 /* If this is a variant frag, we may need to adjust the existing
13156 reloc and generate a new one. */
13157 if (fixp->fx_frag->fr_opcode != NULL
ed6fb7bd
SC
13158 && ((fixp->fx_r_type == BFD_RELOC_GPREL16
13159 && ! HAVE_NEWABI)
252b5132
RH
13160 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
13161 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL16
13162 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
13163 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16
13164 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
6478892d 13165 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_LO16)
ed6fb7bd 13166 )
252b5132
RH
13167 {
13168 arelent *reloc2;
13169
13170 assert (! RELAX_MIPS16_P (fixp->fx_frag->fr_subtype));
13171
13172 /* If this is not the last reloc in this frag, then we have two
13173 GPREL relocs, or a GOT_HI16/GOT_LO16 pair, or a
13174 CALL_HI16/CALL_LO16, both of which are being replaced. Let
13175 the second one handle all of them. */
13176 if (fixp->fx_next != NULL
13177 && fixp->fx_frag == fixp->fx_next->fx_frag)
13178 {
cdf6fd85
TS
13179 assert ((fixp->fx_r_type == BFD_RELOC_GPREL16
13180 && fixp->fx_next->fx_r_type == BFD_RELOC_GPREL16)
252b5132
RH
13181 || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
13182 && (fixp->fx_next->fx_r_type
13183 == BFD_RELOC_MIPS_GOT_LO16))
13184 || (fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
13185 && (fixp->fx_next->fx_r_type
13186 == BFD_RELOC_MIPS_CALL_LO16)));
13187 retval[0] = NULL;
13188 return retval;
13189 }
13190
13191 fixp->fx_where = fixp->fx_frag->fr_opcode - fixp->fx_frag->fr_literal;
13192 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
13193 reloc2 = retval[1] = (arelent *) xmalloc (sizeof (arelent));
13194 retval[2] = NULL;
49309057
ILT
13195 reloc2->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
13196 *reloc2->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
13197 reloc2->address = (reloc->address
13198 + (RELAX_RELOC2 (fixp->fx_frag->fr_subtype)
13199 - RELAX_RELOC1 (fixp->fx_frag->fr_subtype)));
13200 reloc2->addend = fixp->fx_addnumber;
13201 reloc2->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO16);
13202 assert (reloc2->howto != NULL);
13203
13204 if (RELAX_RELOC3 (fixp->fx_frag->fr_subtype))
13205 {
13206 arelent *reloc3;
13207
13208 reloc3 = retval[2] = (arelent *) xmalloc (sizeof (arelent));
13209 retval[3] = NULL;
13210 *reloc3 = *reloc2;
13211 reloc3->address += 4;
13212 }
13213
13214 if (mips_pic == NO_PIC)
13215 {
cdf6fd85 13216 assert (fixp->fx_r_type == BFD_RELOC_GPREL16);
252b5132
RH
13217 fixp->fx_r_type = BFD_RELOC_HI16_S;
13218 }
13219 else if (mips_pic == SVR4_PIC)
13220 {
13221 switch (fixp->fx_r_type)
13222 {
13223 default:
13224 abort ();
13225 case BFD_RELOC_MIPS_GOT16:
13226 break;
252b5132
RH
13227 case BFD_RELOC_MIPS_GOT_LO16:
13228 case BFD_RELOC_MIPS_CALL_LO16:
13229 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
13230 break;
ed6fb7bd
SC
13231 case BFD_RELOC_MIPS_CALL16:
13232 if (HAVE_NEWABI)
13233 {
13234 /* BFD_RELOC_MIPS_GOT16;*/
13235 fixp->fx_r_type = BFD_RELOC_MIPS_GOT_PAGE;
13236 reloc2->howto = bfd_reloc_type_lookup
13237 (stdoutput, BFD_RELOC_MIPS_GOT_OFST);
13238 }
13239 else
13240 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
13241 break;
252b5132
RH
13242 }
13243 }
13244 else
13245 abort ();
ed6fb7bd
SC
13246
13247 /* newabi uses R_MIPS_GOT_DISP for local symbols */
6b70243f 13248 if (HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16)
ed6fb7bd
SC
13249 {
13250 fixp->fx_r_type = BFD_RELOC_MIPS_GOT_DISP;
13251 retval[1] = NULL;
13252 }
252b5132
RH
13253 }
13254
438c16b8
TS
13255 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
13256 entry to be used in the relocation's section offset. */
13257 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
252b5132
RH
13258 {
13259 reloc->address = reloc->addend;
13260 reloc->addend = 0;
13261 }
13262
13263 /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
13264 fixup_segment converted a non-PC relative reloc into a PC
13265 relative reloc. In such a case, we need to convert the reloc
13266 code. */
13267 code = fixp->fx_r_type;
13268 if (fixp->fx_pcrel)
13269 {
13270 switch (code)
13271 {
13272 case BFD_RELOC_8:
13273 code = BFD_RELOC_8_PCREL;
13274 break;
13275 case BFD_RELOC_16:
13276 code = BFD_RELOC_16_PCREL;
13277 break;
13278 case BFD_RELOC_32:
13279 code = BFD_RELOC_32_PCREL;
13280 break;
13281 case BFD_RELOC_64:
13282 code = BFD_RELOC_64_PCREL;
13283 break;
13284 case BFD_RELOC_8_PCREL:
13285 case BFD_RELOC_16_PCREL:
13286 case BFD_RELOC_32_PCREL:
13287 case BFD_RELOC_64_PCREL:
13288 case BFD_RELOC_16_PCREL_S2:
13289 case BFD_RELOC_PCREL_HI16_S:
13290 case BFD_RELOC_PCREL_LO16:
13291 break;
13292 default:
13293 as_bad_where (fixp->fx_file, fixp->fx_line,
13294 _("Cannot make %s relocation PC relative"),
13295 bfd_get_reloc_code_name (code));
13296 }
13297 }
13298
add55e1f
RS
13299#ifdef OBJ_ELF
13300 /* md_apply_fix3 has a double-subtraction hack to get
13301 bfd_install_relocation to behave nicely. GPREL relocations are
13302 handled correctly without this hack, so undo it here. We can't
13303 stop md_apply_fix3 from subtracting twice in the first place since
13304 the fake addend is required for variant frags above. */
13305 if (fixp->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour
98605598 13306 && (code == BFD_RELOC_GPREL16 || code == BFD_RELOC_MIPS16_GPREL)
add55e1f
RS
13307 && reloc->addend != 0
13308 && mips_need_elf_addend_fixup (fixp))
13309 reloc->addend += S_GET_VALUE (fixp->fx_addsy);
13310#endif
13311
252b5132
RH
13312 /* To support a PC relative reloc when generating embedded PIC code
13313 for ECOFF, we use a Cygnus extension. We check for that here to
13314 make sure that we don't let such a reloc escape normally. */
bb2d6cd7
GK
13315 if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
13316 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
252b5132
RH
13317 && code == BFD_RELOC_16_PCREL_S2
13318 && mips_pic != EMBEDDED_PIC)
13319 reloc->howto = NULL;
13320 else
13321 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
13322
13323 if (reloc->howto == NULL)
13324 {
13325 as_bad_where (fixp->fx_file, fixp->fx_line,
13326 _("Can not represent %s relocation in this object file format"),
13327 bfd_get_reloc_code_name (code));
13328 retval[0] = NULL;
13329 }
13330
13331 return retval;
13332}
13333
13334/* Relax a machine dependent frag. This returns the amount by which
13335 the current size of the frag should change. */
13336
13337int
4a6a3df4
AO
13338mips_relax_frag (sec, fragp, stretch)
13339 asection *sec;
252b5132
RH
13340 fragS *fragp;
13341 long stretch;
13342{
4a6a3df4
AO
13343 if (RELAX_BRANCH_P (fragp->fr_subtype))
13344 {
13345 offsetT old_var = fragp->fr_var;
b34976b6
AM
13346
13347 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
4a6a3df4
AO
13348
13349 return fragp->fr_var - old_var;
13350 }
13351
252b5132
RH
13352 if (! RELAX_MIPS16_P (fragp->fr_subtype))
13353 return 0;
13354
c4e7957c 13355 if (mips16_extended_frag (fragp, NULL, stretch))
252b5132
RH
13356 {
13357 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13358 return 0;
13359 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
13360 return 2;
13361 }
13362 else
13363 {
13364 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13365 return 0;
13366 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
13367 return -2;
13368 }
13369
13370 return 0;
13371}
13372
13373/* Convert a machine dependent frag. */
13374
13375void
13376md_convert_frag (abfd, asec, fragp)
43841e91 13377 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
13378 segT asec;
13379 fragS *fragp;
13380{
13381 int old, new;
13382 char *fixptr;
13383
4a6a3df4
AO
13384 if (RELAX_BRANCH_P (fragp->fr_subtype))
13385 {
13386 bfd_byte *buf;
13387 unsigned long insn;
13388 expressionS exp;
13389 fixS *fixp;
b34976b6 13390
4a6a3df4
AO
13391 buf = (bfd_byte *)fragp->fr_literal + fragp->fr_fix;
13392
13393 if (target_big_endian)
13394 insn = bfd_getb32 (buf);
13395 else
13396 insn = bfd_getl32 (buf);
b34976b6 13397
4a6a3df4
AO
13398 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
13399 {
13400 /* We generate a fixup instead of applying it right now
13401 because, if there are linker relaxations, we're going to
13402 need the relocations. */
13403 exp.X_op = O_symbol;
13404 exp.X_add_symbol = fragp->fr_symbol;
13405 exp.X_add_number = fragp->fr_offset;
13406
13407 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13408 4, &exp, 1,
af6ae2ad 13409 BFD_RELOC_16_PCREL_S2);
4a6a3df4
AO
13410 fixp->fx_file = fragp->fr_file;
13411 fixp->fx_line = fragp->fr_line;
b34976b6 13412
4a6a3df4
AO
13413 md_number_to_chars ((char *)buf, insn, 4);
13414 buf += 4;
13415 }
13416 else
13417 {
13418 int i;
13419
13420 as_warn_where (fragp->fr_file, fragp->fr_line,
13421 _("relaxed out-of-range branch into a jump"));
13422
13423 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
13424 goto uncond;
13425
13426 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13427 {
13428 /* Reverse the branch. */
13429 switch ((insn >> 28) & 0xf)
13430 {
13431 case 4:
13432 /* bc[0-3][tf]l? and bc1any[24][ft] instructions can
13433 have the condition reversed by tweaking a single
13434 bit, and their opcodes all have 0x4???????. */
13435 assert ((insn & 0xf1000000) == 0x41000000);
13436 insn ^= 0x00010000;
13437 break;
13438
13439 case 0:
13440 /* bltz 0x04000000 bgez 0x04010000
13441 bltzal 0x04100000 bgezal 0x04110000 */
13442 assert ((insn & 0xfc0e0000) == 0x04000000);
13443 insn ^= 0x00010000;
13444 break;
b34976b6 13445
4a6a3df4
AO
13446 case 1:
13447 /* beq 0x10000000 bne 0x14000000
13448 blez 0x18000000 bgtz 0x1c000000 */
13449 insn ^= 0x04000000;
13450 break;
13451
13452 default:
13453 abort ();
13454 }
13455 }
13456
13457 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13458 {
13459 /* Clear the and-link bit. */
13460 assert ((insn & 0xfc1c0000) == 0x04100000);
13461
13462 /* bltzal 0x04100000 bgezal 0x04110000
13463 bltzall 0x04120000 bgezall 0x04130000 */
13464 insn &= ~0x00100000;
13465 }
13466
13467 /* Branch over the branch (if the branch was likely) or the
13468 full jump (not likely case). Compute the offset from the
13469 current instruction to branch to. */
13470 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13471 i = 16;
13472 else
13473 {
13474 /* How many bytes in instructions we've already emitted? */
13475 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13476 /* How many bytes in instructions from here to the end? */
13477 i = fragp->fr_var - i;
13478 }
13479 /* Convert to instruction count. */
13480 i >>= 2;
13481 /* Branch counts from the next instruction. */
b34976b6 13482 i--;
4a6a3df4
AO
13483 insn |= i;
13484 /* Branch over the jump. */
13485 md_number_to_chars ((char *)buf, insn, 4);
13486 buf += 4;
13487
13488 /* Nop */
13489 md_number_to_chars ((char*)buf, 0, 4);
13490 buf += 4;
13491
13492 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
13493 {
13494 /* beql $0, $0, 2f */
13495 insn = 0x50000000;
13496 /* Compute the PC offset from the current instruction to
13497 the end of the variable frag. */
13498 /* How many bytes in instructions we've already emitted? */
13499 i = buf - (bfd_byte *)fragp->fr_literal - fragp->fr_fix;
13500 /* How many bytes in instructions from here to the end? */
13501 i = fragp->fr_var - i;
13502 /* Convert to instruction count. */
13503 i >>= 2;
13504 /* Don't decrement i, because we want to branch over the
13505 delay slot. */
13506
13507 insn |= i;
13508 md_number_to_chars ((char *)buf, insn, 4);
13509 buf += 4;
13510
13511 md_number_to_chars ((char *)buf, 0, 4);
13512 buf += 4;
13513 }
13514
13515 uncond:
13516 if (mips_pic == NO_PIC)
13517 {
13518 /* j or jal. */
13519 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
13520 ? 0x0c000000 : 0x08000000);
13521 exp.X_op = O_symbol;
13522 exp.X_add_symbol = fragp->fr_symbol;
13523 exp.X_add_number = fragp->fr_offset;
13524
13525 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13526 4, &exp, 0, BFD_RELOC_MIPS_JMP);
13527 fixp->fx_file = fragp->fr_file;
13528 fixp->fx_line = fragp->fr_line;
13529
13530 md_number_to_chars ((char*)buf, insn, 4);
13531 buf += 4;
13532 }
13533 else
13534 {
13535 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
13536 insn = HAVE_64BIT_ADDRESSES ? 0xdf810000 : 0x8f810000;
13537 exp.X_op = O_symbol;
13538 exp.X_add_symbol = fragp->fr_symbol;
13539 exp.X_add_number = fragp->fr_offset;
13540
13541 if (fragp->fr_offset)
13542 {
13543 exp.X_add_symbol = make_expr_symbol (&exp);
13544 exp.X_add_number = 0;
13545 }
13546
13547 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13548 4, &exp, 0, BFD_RELOC_MIPS_GOT16);
13549 fixp->fx_file = fragp->fr_file;
13550 fixp->fx_line = fragp->fr_line;
13551
13552 md_number_to_chars ((char*)buf, insn, 4);
13553 buf += 4;
b34976b6 13554
4a6a3df4
AO
13555 if (mips_opts.isa == ISA_MIPS1)
13556 {
13557 /* nop */
13558 md_number_to_chars ((char*)buf, 0, 4);
13559 buf += 4;
13560 }
13561
13562 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
13563 insn = HAVE_64BIT_ADDRESSES ? 0x64210000 : 0x24210000;
13564
13565 fixp = fix_new_exp (fragp, buf - (bfd_byte *)fragp->fr_literal,
13566 4, &exp, 0, BFD_RELOC_LO16);
13567 fixp->fx_file = fragp->fr_file;
13568 fixp->fx_line = fragp->fr_line;
b34976b6 13569
4a6a3df4
AO
13570 md_number_to_chars ((char*)buf, insn, 4);
13571 buf += 4;
13572
13573 /* j(al)r $at. */
13574 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
13575 insn = 0x0020f809;
13576 else
13577 insn = 0x00200008;
13578
13579 md_number_to_chars ((char*)buf, insn, 4);
13580 buf += 4;
13581 }
13582 }
13583
13584 assert (buf == (bfd_byte *)fragp->fr_literal
13585 + fragp->fr_fix + fragp->fr_var);
13586
13587 fragp->fr_fix += fragp->fr_var;
13588
13589 return;
13590 }
13591
252b5132
RH
13592 if (RELAX_MIPS16_P (fragp->fr_subtype))
13593 {
13594 int type;
13595 register const struct mips16_immed_operand *op;
b34976b6 13596 bfd_boolean small, ext;
252b5132
RH
13597 offsetT val;
13598 bfd_byte *buf;
13599 unsigned long insn;
b34976b6 13600 bfd_boolean use_extend;
252b5132
RH
13601 unsigned short extend;
13602
13603 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
13604 op = mips16_immed_operands;
13605 while (op->type != type)
13606 ++op;
13607
13608 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
13609 {
b34976b6
AM
13610 small = FALSE;
13611 ext = TRUE;
252b5132
RH
13612 }
13613 else
13614 {
b34976b6
AM
13615 small = TRUE;
13616 ext = FALSE;
252b5132
RH
13617 }
13618
6386f3a7 13619 resolve_symbol_value (fragp->fr_symbol);
252b5132
RH
13620 val = S_GET_VALUE (fragp->fr_symbol);
13621 if (op->pcrel)
13622 {
13623 addressT addr;
13624
13625 addr = fragp->fr_address + fragp->fr_fix;
13626
13627 /* The rules for the base address of a PC relative reloc are
13628 complicated; see mips16_extended_frag. */
13629 if (type == 'p' || type == 'q')
13630 {
13631 addr += 2;
13632 if (ext)
13633 addr += 2;
13634 /* Ignore the low bit in the target, since it will be
13635 set for a text label. */
13636 if ((val & 1) != 0)
13637 --val;
13638 }
13639 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13640 addr -= 4;
13641 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13642 addr -= 2;
13643
13644 addr &= ~ (addressT) ((1 << op->shift) - 1);
13645 val -= addr;
13646
13647 /* Make sure the section winds up with the alignment we have
13648 assumed. */
13649 if (op->shift > 0)
13650 record_alignment (asec, op->shift);
13651 }
13652
13653 if (ext
13654 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
13655 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
13656 as_warn_where (fragp->fr_file, fragp->fr_line,
13657 _("extended instruction in delay slot"));
13658
13659 buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
13660
13661 if (target_big_endian)
13662 insn = bfd_getb16 (buf);
13663 else
13664 insn = bfd_getl16 (buf);
13665
13666 mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
13667 RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
13668 small, ext, &insn, &use_extend, &extend);
13669
13670 if (use_extend)
13671 {
874e8986 13672 md_number_to_chars ((char *) buf, 0xf000 | extend, 2);
252b5132
RH
13673 fragp->fr_fix += 2;
13674 buf += 2;
13675 }
13676
874e8986 13677 md_number_to_chars ((char *) buf, insn, 2);
252b5132
RH
13678 fragp->fr_fix += 2;
13679 buf += 2;
13680 }
13681 else
13682 {
13683 if (fragp->fr_opcode == NULL)
13684 return;
13685
13686 old = RELAX_OLD (fragp->fr_subtype);
13687 new = RELAX_NEW (fragp->fr_subtype);
13688 fixptr = fragp->fr_literal + fragp->fr_fix;
13689
13690 if (new > 0)
13691 memcpy (fixptr - old, fixptr, new);
13692
13693 fragp->fr_fix += new - old;
13694 }
13695}
13696
13697#ifdef OBJ_ELF
13698
13699/* This function is called after the relocs have been generated.
13700 We've been storing mips16 text labels as odd. Here we convert them
13701 back to even for the convenience of the debugger. */
13702
13703void
13704mips_frob_file_after_relocs ()
13705{
13706 asymbol **syms;
13707 unsigned int count, i;
13708
13709 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
13710 return;
13711
13712 syms = bfd_get_outsymbols (stdoutput);
13713 count = bfd_get_symcount (stdoutput);
13714 for (i = 0; i < count; i++, syms++)
13715 {
13716 if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
13717 && ((*syms)->value & 1) != 0)
13718 {
13719 (*syms)->value &= ~1;
13720 /* If the symbol has an odd size, it was probably computed
13721 incorrectly, so adjust that as well. */
13722 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
13723 ++elf_symbol (*syms)->internal_elf_sym.st_size;
13724 }
13725 }
13726}
13727
13728#endif
13729
13730/* This function is called whenever a label is defined. It is used
13731 when handling branch delays; if a branch has a label, we assume we
13732 can not move it. */
13733
13734void
13735mips_define_label (sym)
13736 symbolS *sym;
13737{
13738 struct insn_label_list *l;
13739
13740 if (free_insn_labels == NULL)
13741 l = (struct insn_label_list *) xmalloc (sizeof *l);
13742 else
13743 {
13744 l = free_insn_labels;
13745 free_insn_labels = l->next;
13746 }
13747
13748 l->label = sym;
13749 l->next = insn_labels;
13750 insn_labels = l;
13751}
13752\f
13753#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13754
13755/* Some special processing for a MIPS ELF file. */
13756
13757void
13758mips_elf_final_processing ()
13759{
13760 /* Write out the register information. */
316f5878 13761 if (mips_abi != N64_ABI)
252b5132
RH
13762 {
13763 Elf32_RegInfo s;
13764
13765 s.ri_gprmask = mips_gprmask;
13766 s.ri_cprmask[0] = mips_cprmask[0];
13767 s.ri_cprmask[1] = mips_cprmask[1];
13768 s.ri_cprmask[2] = mips_cprmask[2];
13769 s.ri_cprmask[3] = mips_cprmask[3];
13770 /* The gp_value field is set by the MIPS ELF backend. */
13771
13772 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
13773 ((Elf32_External_RegInfo *)
13774 mips_regmask_frag));
13775 }
13776 else
13777 {
13778 Elf64_Internal_RegInfo s;
13779
13780 s.ri_gprmask = mips_gprmask;
13781 s.ri_pad = 0;
13782 s.ri_cprmask[0] = mips_cprmask[0];
13783 s.ri_cprmask[1] = mips_cprmask[1];
13784 s.ri_cprmask[2] = mips_cprmask[2];
13785 s.ri_cprmask[3] = mips_cprmask[3];
13786 /* The gp_value field is set by the MIPS ELF backend. */
13787
13788 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
13789 ((Elf64_External_RegInfo *)
13790 mips_regmask_frag));
13791 }
13792
13793 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
13794 sort of BFD interface for this. */
13795 if (mips_any_noreorder)
13796 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
13797 if (mips_pic != NO_PIC)
13798 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
13799
98d3f06f 13800 /* Set MIPS ELF flags for ASEs. */
a4672219
TS
13801 if (file_ase_mips16)
13802 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
1f25f5d3
CD
13803#if 0 /* XXX FIXME */
13804 if (file_ase_mips3d)
13805 elf_elfheader (stdoutput)->e_flags |= ???;
13806#endif
deec1734
CD
13807 if (file_ase_mdmx)
13808 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
1f25f5d3 13809
bdaaa2e1 13810 /* Set the MIPS ELF ABI flags. */
316f5878 13811 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
252b5132 13812 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
316f5878 13813 else if (mips_abi == O64_ABI)
252b5132 13814 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
316f5878 13815 else if (mips_abi == EABI_ABI)
252b5132 13816 {
316f5878 13817 if (!file_mips_gp32)
252b5132
RH
13818 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
13819 else
13820 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
13821 }
316f5878 13822 else if (mips_abi == N32_ABI)
be00bddd
TS
13823 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
13824
c9914766 13825 /* Nothing to do for N64_ABI. */
252b5132
RH
13826
13827 if (mips_32bitmode)
13828 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
13829}
13830
13831#endif /* OBJ_ELF || OBJ_MAYBE_ELF */
13832\f
beae10d5
KH
13833typedef struct proc {
13834 symbolS *isym;
13835 unsigned long reg_mask;
13836 unsigned long reg_offset;
13837 unsigned long fpreg_mask;
13838 unsigned long fpreg_offset;
13839 unsigned long frame_offset;
13840 unsigned long frame_reg;
13841 unsigned long pc_reg;
13842} procS;
252b5132
RH
13843
13844static procS cur_proc;
13845static procS *cur_proc_ptr;
13846static int numprocs;
13847
0a9ef439 13848/* Fill in an rs_align_code fragment. */
a19d8eb0 13849
0a9ef439
RH
13850void
13851mips_handle_align (fragp)
13852 fragS *fragp;
a19d8eb0 13853{
0a9ef439
RH
13854 if (fragp->fr_type != rs_align_code)
13855 return;
13856
13857 if (mips_opts.mips16)
a19d8eb0
CP
13858 {
13859 static const unsigned char be_nop[] = { 0x65, 0x00 };
13860 static const unsigned char le_nop[] = { 0x00, 0x65 };
13861
0a9ef439
RH
13862 int bytes;
13863 char *p;
a19d8eb0 13864
0a9ef439
RH
13865 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
13866 p = fragp->fr_literal + fragp->fr_fix;
13867
13868 if (bytes & 1)
13869 {
13870 *p++ = 0;
f9419b05 13871 fragp->fr_fix++;
0a9ef439
RH
13872 }
13873
13874 memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
13875 fragp->fr_var = 2;
a19d8eb0
CP
13876 }
13877
0a9ef439 13878 /* For mips32, a nop is a zero, which we trivially get by doing nothing. */
a19d8eb0
CP
13879}
13880
252b5132
RH
13881static void
13882md_obj_begin ()
13883{
13884}
13885
13886static void
13887md_obj_end ()
13888{
13889 /* check for premature end, nesting errors, etc */
13890 if (cur_proc_ptr)
9a41af64 13891 as_warn (_("missing .end at end of assembly"));
252b5132
RH
13892}
13893
13894static long
13895get_number ()
13896{
13897 int negative = 0;
13898 long val = 0;
13899
13900 if (*input_line_pointer == '-')
13901 {
13902 ++input_line_pointer;
13903 negative = 1;
13904 }
3882b010 13905 if (!ISDIGIT (*input_line_pointer))
956cd1d6 13906 as_bad (_("expected simple number"));
252b5132
RH
13907 if (input_line_pointer[0] == '0')
13908 {
13909 if (input_line_pointer[1] == 'x')
13910 {
13911 input_line_pointer += 2;
3882b010 13912 while (ISXDIGIT (*input_line_pointer))
252b5132
RH
13913 {
13914 val <<= 4;
13915 val |= hex_value (*input_line_pointer++);
13916 }
13917 return negative ? -val : val;
13918 }
13919 else
13920 {
13921 ++input_line_pointer;
3882b010 13922 while (ISDIGIT (*input_line_pointer))
252b5132
RH
13923 {
13924 val <<= 3;
13925 val |= *input_line_pointer++ - '0';
13926 }
13927 return negative ? -val : val;
13928 }
13929 }
3882b010 13930 if (!ISDIGIT (*input_line_pointer))
252b5132
RH
13931 {
13932 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
13933 *input_line_pointer, *input_line_pointer);
956cd1d6 13934 as_warn (_("invalid number"));
252b5132
RH
13935 return -1;
13936 }
3882b010 13937 while (ISDIGIT (*input_line_pointer))
252b5132
RH
13938 {
13939 val *= 10;
13940 val += *input_line_pointer++ - '0';
13941 }
13942 return negative ? -val : val;
13943}
13944
13945/* The .file directive; just like the usual .file directive, but there
c5dd6aab
DJ
13946 is an initial number which is the ECOFF file index. In the non-ECOFF
13947 case .file implies DWARF-2. */
13948
13949static void
13950s_mips_file (x)
13951 int x ATTRIBUTE_UNUSED;
13952{
ecb4347a
DJ
13953 static int first_file_directive = 0;
13954
c5dd6aab
DJ
13955 if (ECOFF_DEBUGGING)
13956 {
13957 get_number ();
13958 s_app_file (0);
13959 }
13960 else
ecb4347a
DJ
13961 {
13962 char *filename;
13963
13964 filename = dwarf2_directive_file (0);
13965
13966 /* Versions of GCC up to 3.1 start files with a ".file"
13967 directive even for stabs output. Make sure that this
13968 ".file" is handled. Note that you need a version of GCC
13969 after 3.1 in order to support DWARF-2 on MIPS. */
13970 if (filename != NULL && ! first_file_directive)
13971 {
13972 (void) new_logical_line (filename, -1);
13973 s_app_file_string (filename);
13974 }
13975 first_file_directive = 1;
13976 }
c5dd6aab
DJ
13977}
13978
13979/* The .loc directive, implying DWARF-2. */
252b5132
RH
13980
13981static void
c5dd6aab 13982s_mips_loc (x)
43841e91 13983 int x ATTRIBUTE_UNUSED;
252b5132 13984{
c5dd6aab
DJ
13985 if (!ECOFF_DEBUGGING)
13986 dwarf2_directive_loc (0);
252b5132
RH
13987}
13988
252b5132
RH
13989/* The .end directive. */
13990
13991static void
13992s_mips_end (x)
43841e91 13993 int x ATTRIBUTE_UNUSED;
252b5132
RH
13994{
13995 symbolS *p;
13996 int maybe_text;
13997
7a621144
DJ
13998 /* Following functions need their own .frame and .cprestore directives. */
13999 mips_frame_reg_valid = 0;
14000 mips_cprestore_valid = 0;
14001
252b5132
RH
14002 if (!is_end_of_line[(unsigned char) *input_line_pointer])
14003 {
14004 p = get_symbol ();
14005 demand_empty_rest_of_line ();
14006 }
14007 else
14008 p = NULL;
14009
14010#ifdef BFD_ASSEMBLER
14011 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
14012 maybe_text = 1;
14013 else
14014 maybe_text = 0;
14015#else
14016 if (now_seg != data_section && now_seg != bss_section)
14017 maybe_text = 1;
14018 else
14019 maybe_text = 0;
14020#endif
14021
14022 if (!maybe_text)
14023 as_warn (_(".end not in text section"));
14024
14025 if (!cur_proc_ptr)
14026 {
14027 as_warn (_(".end directive without a preceding .ent directive."));
14028 demand_empty_rest_of_line ();
14029 return;
14030 }
14031
14032 if (p != NULL)
14033 {
14034 assert (S_GET_NAME (p));
14035 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->isym)))
14036 as_warn (_(".end symbol does not match .ent symbol."));
ecb4347a
DJ
14037
14038 if (debug_type == DEBUG_STABS)
14039 stabs_generate_asm_endfunc (S_GET_NAME (p),
14040 S_GET_NAME (p));
252b5132
RH
14041 }
14042 else
14043 as_warn (_(".end directive missing or unknown symbol"));
14044
ecb4347a
DJ
14045#ifdef OBJ_ELF
14046 /* Generate a .pdr section. */
14047 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14048 {
14049 segT saved_seg = now_seg;
14050 subsegT saved_subseg = now_subseg;
14051 valueT dot;
14052 expressionS exp;
14053 char *fragp;
252b5132 14054
ecb4347a 14055 dot = frag_now_fix ();
252b5132
RH
14056
14057#ifdef md_flush_pending_output
ecb4347a 14058 md_flush_pending_output ();
252b5132
RH
14059#endif
14060
ecb4347a
DJ
14061 assert (pdr_seg);
14062 subseg_set (pdr_seg, 0);
252b5132 14063
ecb4347a
DJ
14064 /* Write the symbol. */
14065 exp.X_op = O_symbol;
14066 exp.X_add_symbol = p;
14067 exp.X_add_number = 0;
14068 emit_expr (&exp, 4);
252b5132 14069
ecb4347a 14070 fragp = frag_more (7 * 4);
252b5132 14071
ecb4347a
DJ
14072 md_number_to_chars (fragp, (valueT) cur_proc_ptr->reg_mask, 4);
14073 md_number_to_chars (fragp + 4, (valueT) cur_proc_ptr->reg_offset, 4);
14074 md_number_to_chars (fragp + 8, (valueT) cur_proc_ptr->fpreg_mask, 4);
14075 md_number_to_chars (fragp + 12, (valueT) cur_proc_ptr->fpreg_offset, 4);
14076 md_number_to_chars (fragp + 16, (valueT) cur_proc_ptr->frame_offset, 4);
14077 md_number_to_chars (fragp + 20, (valueT) cur_proc_ptr->frame_reg, 4);
14078 md_number_to_chars (fragp + 24, (valueT) cur_proc_ptr->pc_reg, 4);
252b5132 14079
ecb4347a
DJ
14080 subseg_set (saved_seg, saved_subseg);
14081 }
14082#endif /* OBJ_ELF */
252b5132
RH
14083
14084 cur_proc_ptr = NULL;
14085}
14086
14087/* The .aent and .ent directives. */
14088
14089static void
14090s_mips_ent (aent)
14091 int aent;
14092{
252b5132
RH
14093 symbolS *symbolP;
14094 int maybe_text;
14095
14096 symbolP = get_symbol ();
14097 if (*input_line_pointer == ',')
f9419b05 14098 ++input_line_pointer;
252b5132 14099 SKIP_WHITESPACE ();
3882b010 14100 if (ISDIGIT (*input_line_pointer)
d9a62219 14101 || *input_line_pointer == '-')
874e8986 14102 get_number ();
252b5132
RH
14103
14104#ifdef BFD_ASSEMBLER
14105 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
14106 maybe_text = 1;
14107 else
14108 maybe_text = 0;
14109#else
14110 if (now_seg != data_section && now_seg != bss_section)
14111 maybe_text = 1;
14112 else
14113 maybe_text = 0;
14114#endif
14115
14116 if (!maybe_text)
14117 as_warn (_(".ent or .aent not in text section."));
14118
14119 if (!aent && cur_proc_ptr)
9a41af64 14120 as_warn (_("missing .end"));
252b5132
RH
14121
14122 if (!aent)
14123 {
7a621144
DJ
14124 /* This function needs its own .frame and .cprestore directives. */
14125 mips_frame_reg_valid = 0;
14126 mips_cprestore_valid = 0;
14127
252b5132
RH
14128 cur_proc_ptr = &cur_proc;
14129 memset (cur_proc_ptr, '\0', sizeof (procS));
14130
14131 cur_proc_ptr->isym = symbolP;
14132
49309057 14133 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
252b5132 14134
f9419b05 14135 ++numprocs;
ecb4347a
DJ
14136
14137 if (debug_type == DEBUG_STABS)
14138 stabs_generate_asm_func (S_GET_NAME (symbolP),
14139 S_GET_NAME (symbolP));
252b5132
RH
14140 }
14141
14142 demand_empty_rest_of_line ();
14143}
14144
14145/* The .frame directive. If the mdebug section is present (IRIX 5 native)
bdaaa2e1 14146 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
252b5132 14147 s_mips_frame is used so that we can set the PDR information correctly.
bdaaa2e1 14148 We can't use the ecoff routines because they make reference to the ecoff
252b5132
RH
14149 symbol table (in the mdebug section). */
14150
14151static void
14152s_mips_frame (ignore)
2b3c5a5d 14153 int ignore ATTRIBUTE_UNUSED;
252b5132 14154{
ecb4347a
DJ
14155#ifdef OBJ_ELF
14156 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
14157 {
14158 long val;
252b5132 14159
ecb4347a
DJ
14160 if (cur_proc_ptr == (procS *) NULL)
14161 {
14162 as_warn (_(".frame outside of .ent"));
14163 demand_empty_rest_of_line ();
14164 return;
14165 }
252b5132 14166
ecb4347a
DJ
14167 cur_proc_ptr->frame_reg = tc_get_register (1);
14168
14169 SKIP_WHITESPACE ();
14170 if (*input_line_pointer++ != ','
14171 || get_absolute_expression_and_terminator (&val) != ',')
14172 {
14173 as_warn (_("Bad .frame directive"));
14174 --input_line_pointer;
14175 demand_empty_rest_of_line ();
14176 return;
14177 }
252b5132 14178
ecb4347a
DJ
14179 cur_proc_ptr->frame_offset = val;
14180 cur_proc_ptr->pc_reg = tc_get_register (0);
252b5132 14181
252b5132 14182 demand_empty_rest_of_line ();
252b5132 14183 }
ecb4347a
DJ
14184 else
14185#endif /* OBJ_ELF */
14186 s_ignore (ignore);
252b5132
RH
14187}
14188
bdaaa2e1
KH
14189/* The .fmask and .mask directives. If the mdebug section is present
14190 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
252b5132 14191 embedded targets, s_mips_mask is used so that we can set the PDR
bdaaa2e1 14192 information correctly. We can't use the ecoff routines because they
252b5132
RH
14193 make reference to the ecoff symbol table (in the mdebug section). */
14194
14195static void
14196s_mips_mask (reg_type)
14197 char reg_type;
14198{
ecb4347a
DJ
14199#ifdef OBJ_ELF
14200 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
252b5132 14201 {
ecb4347a 14202 long mask, off;
252b5132 14203
ecb4347a
DJ
14204 if (cur_proc_ptr == (procS *) NULL)
14205 {
14206 as_warn (_(".mask/.fmask outside of .ent"));
14207 demand_empty_rest_of_line ();
14208 return;
14209 }
252b5132 14210
ecb4347a
DJ
14211 if (get_absolute_expression_and_terminator (&mask) != ',')
14212 {
14213 as_warn (_("Bad .mask/.fmask directive"));
14214 --input_line_pointer;
14215 demand_empty_rest_of_line ();
14216 return;
14217 }
252b5132 14218
ecb4347a
DJ
14219 off = get_absolute_expression ();
14220
14221 if (reg_type == 'F')
14222 {
14223 cur_proc_ptr->fpreg_mask = mask;
14224 cur_proc_ptr->fpreg_offset = off;
14225 }
14226 else
14227 {
14228 cur_proc_ptr->reg_mask = mask;
14229 cur_proc_ptr->reg_offset = off;
14230 }
14231
14232 demand_empty_rest_of_line ();
252b5132
RH
14233 }
14234 else
ecb4347a
DJ
14235#endif /* OBJ_ELF */
14236 s_ignore (reg_type);
252b5132
RH
14237}
14238
14239/* The .loc directive. */
14240
14241#if 0
14242static void
14243s_loc (x)
14244 int x;
14245{
14246 symbolS *symbolP;
14247 int lineno;
14248 int addroff;
14249
14250 assert (now_seg == text_section);
14251
14252 lineno = get_number ();
14253 addroff = frag_now_fix ();
14254
14255 symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
14256 S_SET_TYPE (symbolP, N_SLINE);
14257 S_SET_OTHER (symbolP, 0);
14258 S_SET_DESC (symbolP, lineno);
14259 symbolP->sy_segment = now_seg;
14260}
14261#endif
e7af610e 14262
316f5878
RS
14263/* A table describing all the processors gas knows about. Names are
14264 matched in the order listed.
e7af610e 14265
316f5878
RS
14266 To ease comparison, please keep this table in the same order as
14267 gcc's mips_cpu_info_table[]. */
e972090a
NC
14268static const struct mips_cpu_info mips_cpu_info_table[] =
14269{
316f5878
RS
14270 /* Entries for generic ISAs */
14271 { "mips1", 1, ISA_MIPS1, CPU_R3000 },
14272 { "mips2", 1, ISA_MIPS2, CPU_R6000 },
14273 { "mips3", 1, ISA_MIPS3, CPU_R4000 },
14274 { "mips4", 1, ISA_MIPS4, CPU_R8000 },
14275 { "mips5", 1, ISA_MIPS5, CPU_MIPS5 },
14276 { "mips32", 1, ISA_MIPS32, CPU_MIPS32 },
af7ee8bf 14277 { "mips32r2", 1, ISA_MIPS32R2, CPU_MIPS32R2 },
316f5878
RS
14278 { "mips64", 1, ISA_MIPS64, CPU_MIPS64 },
14279
14280 /* MIPS I */
14281 { "r3000", 0, ISA_MIPS1, CPU_R3000 },
14282 { "r2000", 0, ISA_MIPS1, CPU_R3000 },
14283 { "r3900", 0, ISA_MIPS1, CPU_R3900 },
14284
14285 /* MIPS II */
14286 { "r6000", 0, ISA_MIPS2, CPU_R6000 },
14287
14288 /* MIPS III */
14289 { "r4000", 0, ISA_MIPS3, CPU_R4000 },
14290 { "r4010", 0, ISA_MIPS2, CPU_R4010 },
14291 { "vr4100", 0, ISA_MIPS3, CPU_VR4100 },
14292 { "vr4111", 0, ISA_MIPS3, CPU_R4111 },
60b63b72
RS
14293 { "vr4120", 0, ISA_MIPS3, CPU_VR4120 },
14294 { "vr4130", 0, ISA_MIPS3, CPU_VR4120 },
14295 { "vr4181", 0, ISA_MIPS3, CPU_R4111 },
316f5878
RS
14296 { "vr4300", 0, ISA_MIPS3, CPU_R4300 },
14297 { "r4400", 0, ISA_MIPS3, CPU_R4400 },
14298 { "r4600", 0, ISA_MIPS3, CPU_R4600 },
14299 { "orion", 0, ISA_MIPS3, CPU_R4600 },
14300 { "r4650", 0, ISA_MIPS3, CPU_R4650 },
14301
14302 /* MIPS IV */
14303 { "r8000", 0, ISA_MIPS4, CPU_R8000 },
14304 { "r10000", 0, ISA_MIPS4, CPU_R10000 },
14305 { "r12000", 0, ISA_MIPS4, CPU_R12000 },
14306 { "vr5000", 0, ISA_MIPS4, CPU_R5000 },
60b63b72
RS
14307 { "vr5400", 0, ISA_MIPS4, CPU_VR5400 },
14308 { "vr5500", 0, ISA_MIPS4, CPU_VR5500 },
316f5878
RS
14309 { "rm5200", 0, ISA_MIPS4, CPU_R5000 },
14310 { "rm5230", 0, ISA_MIPS4, CPU_R5000 },
14311 { "rm5231", 0, ISA_MIPS4, CPU_R5000 },
14312 { "rm5261", 0, ISA_MIPS4, CPU_R5000 },
14313 { "rm5721", 0, ISA_MIPS4, CPU_R5000 },
14314 { "r7000", 0, ISA_MIPS4, CPU_R5000 },
14315
14316 /* MIPS 32 */
14317 { "4kc", 0, ISA_MIPS32, CPU_MIPS32, },
14318 { "4km", 0, ISA_MIPS32, CPU_MIPS32 },
14319 { "4kp", 0, ISA_MIPS32, CPU_MIPS32 },
e7af610e 14320
316f5878
RS
14321 /* MIPS 64 */
14322 { "5kc", 0, ISA_MIPS64, CPU_MIPS64 },
14323 { "20kc", 0, ISA_MIPS64, CPU_MIPS64 },
e7af610e 14324
c7a23324 14325 /* Broadcom SB-1 CPU core */
316f5878 14326 { "sb1", 0, ISA_MIPS64, CPU_SB1 },
e7af610e 14327
316f5878
RS
14328 /* End marker */
14329 { NULL, 0, 0, 0 }
14330};
e7af610e 14331
84ea6cf2 14332
316f5878
RS
14333/* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
14334 with a final "000" replaced by "k". Ignore case.
e7af610e 14335
316f5878 14336 Note: this function is shared between GCC and GAS. */
c6c98b38 14337
b34976b6 14338static bfd_boolean
316f5878
RS
14339mips_strict_matching_cpu_name_p (canonical, given)
14340 const char *canonical, *given;
14341{
14342 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
14343 given++, canonical++;
14344
14345 return ((*given == 0 && *canonical == 0)
14346 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
14347}
14348
14349
14350/* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
14351 CPU name. We've traditionally allowed a lot of variation here.
14352
14353 Note: this function is shared between GCC and GAS. */
14354
b34976b6 14355static bfd_boolean
316f5878
RS
14356mips_matching_cpu_name_p (canonical, given)
14357 const char *canonical, *given;
14358{
14359 /* First see if the name matches exactly, or with a final "000"
14360 turned into "k". */
14361 if (mips_strict_matching_cpu_name_p (canonical, given))
b34976b6 14362 return TRUE;
316f5878
RS
14363
14364 /* If not, try comparing based on numerical designation alone.
14365 See if GIVEN is an unadorned number, or 'r' followed by a number. */
14366 if (TOLOWER (*given) == 'r')
14367 given++;
14368 if (!ISDIGIT (*given))
b34976b6 14369 return FALSE;
316f5878
RS
14370
14371 /* Skip over some well-known prefixes in the canonical name,
14372 hoping to find a number there too. */
14373 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
14374 canonical += 2;
14375 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
14376 canonical += 2;
14377 else if (TOLOWER (canonical[0]) == 'r')
14378 canonical += 1;
14379
14380 return mips_strict_matching_cpu_name_p (canonical, given);
14381}
14382
14383
14384/* Parse an option that takes the name of a processor as its argument.
14385 OPTION is the name of the option and CPU_STRING is the argument.
14386 Return the corresponding processor enumeration if the CPU_STRING is
14387 recognized, otherwise report an error and return null.
14388
14389 A similar function exists in GCC. */
e7af610e
NC
14390
14391static const struct mips_cpu_info *
316f5878
RS
14392mips_parse_cpu (option, cpu_string)
14393 const char *option, *cpu_string;
e7af610e 14394{
316f5878 14395 const struct mips_cpu_info *p;
e7af610e 14396
316f5878
RS
14397 /* 'from-abi' selects the most compatible architecture for the given
14398 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
14399 EABIs, we have to decide whether we're using the 32-bit or 64-bit
14400 version. Look first at the -mgp options, if given, otherwise base
14401 the choice on MIPS_DEFAULT_64BIT.
e7af610e 14402
316f5878
RS
14403 Treat NO_ABI like the EABIs. One reason to do this is that the
14404 plain 'mips' and 'mips64' configs have 'from-abi' as their default
14405 architecture. This code picks MIPS I for 'mips' and MIPS III for
14406 'mips64', just as we did in the days before 'from-abi'. */
14407 if (strcasecmp (cpu_string, "from-abi") == 0)
14408 {
14409 if (ABI_NEEDS_32BIT_REGS (mips_abi))
14410 return mips_cpu_info_from_isa (ISA_MIPS1);
14411
14412 if (ABI_NEEDS_64BIT_REGS (mips_abi))
14413 return mips_cpu_info_from_isa (ISA_MIPS3);
14414
14415 if (file_mips_gp32 >= 0)
14416 return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
14417
14418 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
14419 ? ISA_MIPS3
14420 : ISA_MIPS1);
14421 }
14422
14423 /* 'default' has traditionally been a no-op. Probably not very useful. */
14424 if (strcasecmp (cpu_string, "default") == 0)
14425 return 0;
14426
14427 for (p = mips_cpu_info_table; p->name != 0; p++)
14428 if (mips_matching_cpu_name_p (p->name, cpu_string))
14429 return p;
14430
14431 as_bad ("Bad value (%s) for %s", cpu_string, option);
14432 return 0;
e7af610e
NC
14433}
14434
316f5878
RS
14435/* Return the canonical processor information for ISA (a member of the
14436 ISA_MIPS* enumeration). */
14437
e7af610e
NC
14438static const struct mips_cpu_info *
14439mips_cpu_info_from_isa (isa)
14440 int isa;
14441{
14442 int i;
14443
14444 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
14445 if (mips_cpu_info_table[i].is_isa
316f5878 14446 && isa == mips_cpu_info_table[i].isa)
e7af610e
NC
14447 return (&mips_cpu_info_table[i]);
14448
e972090a 14449 return NULL;
e7af610e 14450}
316f5878
RS
14451\f
14452static void
14453show (stream, string, col_p, first_p)
14454 FILE *stream;
14455 const char *string;
14456 int *col_p;
14457 int *first_p;
14458{
14459 if (*first_p)
14460 {
14461 fprintf (stream, "%24s", "");
14462 *col_p = 24;
14463 }
14464 else
14465 {
14466 fprintf (stream, ", ");
14467 *col_p += 2;
14468 }
e7af610e 14469
316f5878
RS
14470 if (*col_p + strlen (string) > 72)
14471 {
14472 fprintf (stream, "\n%24s", "");
14473 *col_p = 24;
14474 }
14475
14476 fprintf (stream, "%s", string);
14477 *col_p += strlen (string);
14478
14479 *first_p = 0;
14480}
14481
14482void
14483md_show_usage (stream)
14484 FILE *stream;
e7af610e 14485{
316f5878
RS
14486 int column, first;
14487 size_t i;
14488
14489 fprintf (stream, _("\
14490MIPS options:\n\
14491-membedded-pic generate embedded position independent code\n\
14492-EB generate big endian output\n\
14493-EL generate little endian output\n\
14494-g, -g2 do not remove unneeded NOPs or swap branches\n\
14495-G NUM allow referencing objects up to NUM bytes\n\
14496 implicitly with the gp register [default 8]\n"));
14497 fprintf (stream, _("\
14498-mips1 generate MIPS ISA I instructions\n\
14499-mips2 generate MIPS ISA II instructions\n\
14500-mips3 generate MIPS ISA III instructions\n\
14501-mips4 generate MIPS ISA IV instructions\n\
14502-mips5 generate MIPS ISA V instructions\n\
14503-mips32 generate MIPS32 ISA instructions\n\
af7ee8bf 14504-mips32r2 generate MIPS32 release 2 ISA instructions\n\
316f5878
RS
14505-mips64 generate MIPS64 ISA instructions\n\
14506-march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
14507
14508 first = 1;
e7af610e
NC
14509
14510 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
316f5878
RS
14511 show (stream, mips_cpu_info_table[i].name, &column, &first);
14512 show (stream, "from-abi", &column, &first);
14513 fputc ('\n', stream);
e7af610e 14514
316f5878
RS
14515 fprintf (stream, _("\
14516-mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
14517-no-mCPU don't generate code specific to CPU.\n\
14518 For -mCPU and -no-mCPU, CPU must be one of:\n"));
14519
14520 first = 1;
14521
14522 show (stream, "3900", &column, &first);
14523 show (stream, "4010", &column, &first);
14524 show (stream, "4100", &column, &first);
14525 show (stream, "4650", &column, &first);
14526 fputc ('\n', stream);
14527
14528 fprintf (stream, _("\
14529-mips16 generate mips16 instructions\n\
14530-no-mips16 do not generate mips16 instructions\n"));
14531 fprintf (stream, _("\
14532-mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
14533-mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
14534-O0 remove unneeded NOPs, do not swap branches\n\
14535-O remove unneeded NOPs and swap branches\n\
14536-n warn about NOPs generated from macros\n\
14537--[no-]construct-floats [dis]allow floating point values to be constructed\n\
14538--trap, --no-break trap exception on div by 0 and mult overflow\n\
14539--break, --no-trap break exception on div by 0 and mult overflow\n"));
14540#ifdef OBJ_ELF
14541 fprintf (stream, _("\
14542-KPIC, -call_shared generate SVR4 position independent code\n\
14543-non_shared do not generate position independent code\n\
14544-xgot assume a 32 bit GOT\n\
14545-mabi=ABI create ABI conformant object file for:\n"));
14546
14547 first = 1;
14548
14549 show (stream, "32", &column, &first);
14550 show (stream, "o64", &column, &first);
14551 show (stream, "n32", &column, &first);
14552 show (stream, "64", &column, &first);
14553 show (stream, "eabi", &column, &first);
14554
14555 fputc ('\n', stream);
14556
14557 fprintf (stream, _("\
14558-32 create o32 ABI object file (default)\n\
14559-n32 create n32 ABI object file\n\
14560-64 create 64 ABI object file\n"));
14561#endif
e7af610e 14562}
14e777e0
KB
14563
14564enum dwarf2_format
14565mips_dwarf2_format ()
14566{
14567 if (mips_abi == N64_ABI)
1de5b6a1
AO
14568 {
14569#ifdef TE_IRIX
14570 return dwarf2_format_64bit_irix;
14571#else
14572 return dwarf2_format_64bit;
14573#endif
14574 }
14e777e0
KB
14575 else
14576 return dwarf2_format_32bit;
14577}
This page took 1.126661 seconds and 4 git commands to generate.