2002-06-04 Chris Demetriou <cgd@broadcom.com>
[deliverable/binutils-gdb.git] / gas / config / tc-mips.c
CommitLineData
252b5132 1/* tc-mips.c -- assemble code for a MIPS chip.
98d3f06f 2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
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"
40
41#ifdef DEBUG
42#define DBG(x) printf x
43#else
44#define DBG(x)
45#endif
46
47#ifdef OBJ_MAYBE_ELF
48/* Clean up namespace so we can include obj-elf.h too. */
49static int mips_output_flavor PARAMS ((void));
50static int mips_output_flavor () { return OUTPUT_FLAVOR; }
51#undef OBJ_PROCESS_STAB
52#undef OUTPUT_FLAVOR
53#undef S_GET_ALIGN
54#undef S_GET_SIZE
55#undef S_SET_ALIGN
56#undef S_SET_SIZE
252b5132
RH
57#undef obj_frob_file
58#undef obj_frob_file_after_relocs
59#undef obj_frob_symbol
60#undef obj_pop_insert
61#undef obj_sec_sym_ok_for_reloc
62#undef OBJ_COPY_SYMBOL_ATTRIBUTES
63
64#include "obj-elf.h"
65/* Fix any of them that we actually care about. */
66#undef OUTPUT_FLAVOR
67#define OUTPUT_FLAVOR mips_output_flavor()
68#endif
69
70#if defined (OBJ_ELF)
71#include "elf/mips.h"
72#endif
73
74#ifndef ECOFF_DEBUGGING
75#define NO_ECOFF_DEBUGGING
76#define ECOFF_DEBUGGING 0
77#endif
78
79#include "ecoff.h"
80
81#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
82static char *mips_regmask_frag;
83#endif
84
85#define AT 1
86#define TREG 24
87#define PIC_CALL_REG 25
88#define KT0 26
89#define KT1 27
90#define GP 28
91#define SP 29
92#define FP 30
93#define RA 31
94
95#define ILLEGAL_REG (32)
96
97/* Allow override of standard little-endian ECOFF format. */
98
99#ifndef ECOFF_LITTLE_FORMAT
100#define ECOFF_LITTLE_FORMAT "ecoff-littlemips"
101#endif
102
103extern int target_big_endian;
104
252b5132
RH
105/* The name of the readonly data section. */
106#define RDATA_SECTION_NAME (OUTPUT_FLAVOR == bfd_target_aout_flavour \
107 ? ".data" \
108 : OUTPUT_FLAVOR == bfd_target_ecoff_flavour \
109 ? ".rdata" \
056350c6
NC
110 : OUTPUT_FLAVOR == bfd_target_coff_flavour \
111 ? ".rdata" \
252b5132
RH
112 : OUTPUT_FLAVOR == bfd_target_elf_flavour \
113 ? ".rodata" \
114 : (abort (), ""))
115
a325df1d
TS
116/* The ABI to use. */
117enum mips_abi_level
118{
119 NO_ABI = 0,
120 O32_ABI,
121 O64_ABI,
122 N32_ABI,
123 N64_ABI,
124 EABI_ABI
125};
126
127/* MIPS ABI we are using for this output file. */
128static enum mips_abi_level file_mips_abi = NO_ABI;
129
252b5132
RH
130/* This is the set of options which may be modified by the .set
131 pseudo-op. We use a struct so that .set push and .set pop are more
132 reliable. */
133
e972090a
NC
134struct mips_set_options
135{
252b5132
RH
136 /* MIPS ISA (Instruction Set Architecture) level. This is set to -1
137 if it has not been initialized. Changed by `.set mipsN', and the
138 -mipsN command line option, and the default CPU. */
139 int isa;
1f25f5d3
CD
140 /* Enabled Application Specific Extensions (ASEs). These are set to -1
141 if they have not been initialized. Changed by `.set <asename>', by
142 command line options, and based on the default architecture. */
143 int ase_mips3d;
deec1734 144 int ase_mdmx;
252b5132
RH
145 /* Whether we are assembling for the mips16 processor. 0 if we are
146 not, 1 if we are, and -1 if the value has not been initialized.
147 Changed by `.set mips16' and `.set nomips16', and the -mips16 and
148 -nomips16 command line options, and the default CPU. */
149 int mips16;
150 /* Non-zero if we should not reorder instructions. Changed by `.set
151 reorder' and `.set noreorder'. */
152 int noreorder;
153 /* Non-zero if we should not permit the $at ($1) register to be used
154 in instructions. Changed by `.set at' and `.set noat'. */
155 int noat;
156 /* Non-zero if we should warn when a macro instruction expands into
157 more than one machine instruction. Changed by `.set nomacro' and
158 `.set macro'. */
159 int warn_about_macros;
160 /* Non-zero if we should not move instructions. Changed by `.set
161 move', `.set volatile', `.set nomove', and `.set novolatile'. */
162 int nomove;
163 /* Non-zero if we should not optimize branches by moving the target
164 of the branch into the delay slot. Actually, we don't perform
165 this optimization anyhow. Changed by `.set bopt' and `.set
166 nobopt'. */
167 int nobopt;
168 /* Non-zero if we should not autoextend mips16 instructions.
169 Changed by `.set autoextend' and `.set noautoextend'. */
170 int noautoextend;
a325df1d
TS
171 /* Restrict general purpose registers and floating point registers
172 to 32 bit. This is initially determined when -mgp32 or -mfp32
173 is passed but can changed if the assembler code uses .set mipsN. */
174 int gp32;
175 int fp32;
176 /* The ABI currently in use. This is changed by .set mipsN to loosen
177 restrictions and doesn't affect the whole file. */
178 enum mips_abi_level abi;
252b5132
RH
179};
180
a325df1d 181/* True if -mgp32 was passed. */
a8e8e863 182static int file_mips_gp32 = -1;
a325df1d
TS
183
184/* True if -mfp32 was passed. */
a8e8e863 185static int file_mips_fp32 = -1;
a325df1d 186
252b5132 187/* This is the struct we use to hold the current set of options. Note
e7af610e
NC
188 that we must set the isa field to ISA_UNKNOWN and the mips16 field to
189 -1 to indicate that they have not been initialized. */
252b5132 190
e972090a
NC
191static struct mips_set_options mips_opts =
192{
deec1734 193 ISA_UNKNOWN, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, NO_ABI
e7af610e 194};
252b5132
RH
195
196/* These variables are filled in with the masks of registers used.
197 The object format code reads them and puts them in the appropriate
198 place. */
199unsigned long mips_gprmask;
200unsigned long mips_cprmask[4];
201
202/* MIPS ISA we are using for this output file. */
e7af610e 203static int file_mips_isa = ISA_UNKNOWN;
252b5132 204
1f25f5d3
CD
205/* True if -mips3d was passed or implied by arguments passed on the
206 command line (e.g., by -march). */
207static int file_ase_mips3d;
208
deec1734
CD
209/* True if -mdmx was passed or implied by arguments passed on the
210 command line (e.g., by -march). */
211static int file_ase_mdmx;
212
ec68c924 213/* The argument of the -mcpu= flag. Historical for code generation. */
e7af610e 214static int mips_cpu = CPU_UNKNOWN;
252b5132 215
ec68c924
EC
216/* The argument of the -march= flag. The architecture we are assembling. */
217static int mips_arch = CPU_UNKNOWN;
218
219/* The argument of the -mtune= flag. The architecture for which we
220 are optimizing. */
221static int mips_tune = CPU_UNKNOWN;
222
2f4dcb11 223/* Whether we should mark the file EABI64 or EABI32. */
252b5132
RH
224static int mips_eabi64 = 0;
225
226/* If they asked for mips1 or mips2 and a cpu that is
bdaaa2e1 227 mips3 or greater, then mark the object file 32BITMODE. */
252b5132
RH
228static int mips_32bitmode = 0;
229
9ce8a5dd
GRK
230/* Some ISA's have delay slots for instructions which read or write
231 from a coprocessor (eg. mips1-mips3); some don't (eg mips4).
bdaaa2e1 232 Return true if instructions marked INSN_LOAD_COPROC_DELAY,
9ce8a5dd
GRK
233 INSN_COPROC_MOVE_DELAY, or INSN_WRITE_COND_CODE actually have a
234 delay slot in this ISA. The uses of this macro assume that any
235 ISA that has delay slots for one of these, has them for all. They
236 also assume that ISAs which don't have delays for these insns, don't
bdaaa2e1 237 have delays for the INSN_LOAD_MEMORY_DELAY instructions either. */
9ce8a5dd 238#define ISA_HAS_COPROC_DELAYS(ISA) ( \
e7af610e
NC
239 (ISA) == ISA_MIPS1 \
240 || (ISA) == ISA_MIPS2 \
241 || (ISA) == ISA_MIPS3 \
9ce8a5dd
GRK
242 )
243
bdaaa2e1 244/* Return true if ISA supports 64 bit gp register instructions. */
9ce8a5dd 245#define ISA_HAS_64BIT_REGS(ISA) ( \
e7af610e
NC
246 (ISA) == ISA_MIPS3 \
247 || (ISA) == ISA_MIPS4 \
84ea6cf2 248 || (ISA) == ISA_MIPS5 \
d1cf510e 249 || (ISA) == ISA_MIPS64 \
9ce8a5dd
GRK
250 )
251
e013f690 252#define HAVE_32BIT_GPRS \
a325df1d
TS
253 (mips_opts.gp32 \
254 || mips_opts.abi == O32_ABI \
e013f690 255 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
ca4e0257 256
e013f690 257#define HAVE_32BIT_FPRS \
a325df1d
TS
258 (mips_opts.fp32 \
259 || mips_opts.abi == O32_ABI \
e013f690 260 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
ca4e0257
RS
261
262#define HAVE_64BIT_GPRS (! HAVE_32BIT_GPRS)
263#define HAVE_64BIT_FPRS (! HAVE_32BIT_FPRS)
264
a325df1d 265#define HAVE_NEWABI (mips_opts.abi == N32_ABI || mips_opts.abi == N64_ABI)
e013f690 266
a325df1d 267#define HAVE_64BIT_OBJECTS (mips_opts.abi == N64_ABI)
e013f690
TS
268
269/* We can only have 64bit addresses if the object file format
270 supports it. */
afdbd6d0
CD
271#define HAVE_32BIT_ADDRESSES \
272 (HAVE_32BIT_GPRS \
273 || ((bfd_arch_bits_per_address (stdoutput) == 32 \
274 || ! HAVE_64BIT_OBJECTS) \
275 && mips_pic != EMBEDDED_PIC))
e013f690
TS
276
277#define HAVE_64BIT_ADDRESSES (! HAVE_32BIT_ADDRESSES)
ca4e0257 278
1f25f5d3
CD
279/* Return true if the given CPU supports the MIPS3D ASE. */
280#define CPU_HAS_MIPS3D(cpu) ((cpu) == CPU_SB1 \
281 )
282
deec1734
CD
283/* Return true if the given CPU supports the MDMX ASE. */
284#define CPU_HAS_MDMX(cpu) (0 \
285 )
286
bdaaa2e1 287/* Whether the processor uses hardware interlocks to protect
252b5132 288 reads from the HI and LO registers, and thus does not
ec68c924 289 require nops to be inserted. */
252b5132 290
ec68c924 291#define hilo_interlocks (mips_arch == CPU_R4010 \
0a758a12 292 || mips_arch == CPU_SB1 \
252b5132
RH
293 )
294
295/* Whether the processor uses hardware interlocks to protect reads
296 from the GPRs, and thus does not require nops to be inserted. */
297#define gpr_interlocks \
e7af610e 298 (mips_opts.isa != ISA_MIPS1 \
ec68c924 299 || mips_arch == CPU_R3900)
252b5132
RH
300
301/* As with other "interlocks" this is used by hardware that has FP
302 (co-processor) interlocks. */
bdaaa2e1 303/* Itbl support may require additional care here. */
ec68c924 304#define cop_interlocks (mips_arch == CPU_R4300 \
0a758a12 305 || mips_arch == CPU_SB1 \
252b5132
RH
306 )
307
6b76fefe
CM
308/* Is this a mfhi or mflo instruction? */
309#define MF_HILO_INSN(PINFO) \
310 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
311
252b5132
RH
312/* MIPS PIC level. */
313
e972090a
NC
314enum mips_pic_level
315{
252b5132
RH
316 /* Do not generate PIC code. */
317 NO_PIC,
318
319 /* Generate PIC code as in Irix 4. This is not implemented, and I'm
320 not sure what it is supposed to do. */
321 IRIX4_PIC,
322
323 /* Generate PIC code as in the SVR4 MIPS ABI. */
324 SVR4_PIC,
325
326 /* Generate PIC code without using a global offset table: the data
327 segment has a maximum size of 64K, all data references are off
328 the $gp register, and all text references are PC relative. This
329 is used on some embedded systems. */
330 EMBEDDED_PIC
331};
332
333static enum mips_pic_level mips_pic;
334
39c0a331
L
335/* Warn about all NOPS that the assembler generates. */
336static int warn_nops = 0;
337
c9914766 338/* 1 if we should generate 32 bit offsets from the $gp register in
252b5132 339 SVR4_PIC mode. Currently has no meaning in other modes. */
c9914766 340static int mips_big_got = 0;
252b5132
RH
341
342/* 1 if trap instructions should used for overflow rather than break
343 instructions. */
c9914766 344static int mips_trap = 0;
252b5132 345
119d663a 346/* 1 if double width floating point constants should not be constructed
b6ff326e 347 by assembling two single width halves into two single width floating
119d663a
NC
348 point registers which just happen to alias the double width destination
349 register. On some architectures this aliasing can be disabled by a bit
d547a75e 350 in the status register, and the setting of this bit cannot be determined
119d663a
NC
351 automatically at assemble time. */
352static int mips_disable_float_construction;
353
252b5132
RH
354/* Non-zero if any .set noreorder directives were used. */
355
356static int mips_any_noreorder;
357
6b76fefe
CM
358/* Non-zero if nops should be inserted when the register referenced in
359 an mfhi/mflo instruction is read in the next two instructions. */
360static int mips_7000_hilo_fix;
361
252b5132 362/* The size of the small data section. */
156c2f8b 363static unsigned int g_switch_value = 8;
252b5132
RH
364/* Whether the -G option was used. */
365static int g_switch_seen = 0;
366
367#define N_RMASK 0xc4
368#define N_VFP 0xd4
369
370/* If we can determine in advance that GP optimization won't be
371 possible, we can skip the relaxation stuff that tries to produce
372 GP-relative references. This makes delay slot optimization work
373 better.
374
375 This function can only provide a guess, but it seems to work for
fba2b7f9
GK
376 gcc output. It needs to guess right for gcc, otherwise gcc
377 will put what it thinks is a GP-relative instruction in a branch
378 delay slot.
252b5132
RH
379
380 I don't know if a fix is needed for the SVR4_PIC mode. I've only
381 fixed it for the non-PIC mode. KR 95/04/07 */
382static int nopic_need_relax PARAMS ((symbolS *, int));
383
384/* handle of the OPCODE hash table */
385static struct hash_control *op_hash = NULL;
386
387/* The opcode hash table we use for the mips16. */
388static struct hash_control *mips16_op_hash = NULL;
389
390/* This array holds the chars that always start a comment. If the
391 pre-processor is disabled, these aren't very useful */
392const char comment_chars[] = "#";
393
394/* This array holds the chars that only start a comment at the beginning of
395 a line. If the line seems to have the form '# 123 filename'
396 .line and .file directives will appear in the pre-processed output */
397/* Note that input_file.c hand checks for '#' at the beginning of the
398 first line of the input file. This is because the compiler outputs
bdaaa2e1 399 #NO_APP at the beginning of its output. */
252b5132
RH
400/* Also note that C style comments are always supported. */
401const char line_comment_chars[] = "#";
402
bdaaa2e1 403/* This array holds machine specific line separator characters. */
63a0b638 404const char line_separator_chars[] = ";";
252b5132
RH
405
406/* Chars that can be used to separate mant from exp in floating point nums */
407const char EXP_CHARS[] = "eE";
408
409/* Chars that mean this number is a floating point constant */
410/* As in 0f12.456 */
411/* or 0d1.2345e12 */
412const char FLT_CHARS[] = "rRsSfFdDxXpP";
413
414/* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
415 changed in read.c . Ideally it shouldn't have to know about it at all,
416 but nothing is ideal around here.
417 */
418
419static char *insn_error;
420
421static int auto_align = 1;
422
423/* When outputting SVR4 PIC code, the assembler needs to know the
424 offset in the stack frame from which to restore the $gp register.
425 This is set by the .cprestore pseudo-op, and saved in this
426 variable. */
427static offsetT mips_cprestore_offset = -1;
428
6478892d
TS
429/* Similiar for NewABI PIC code, where $gp is callee-saved. NewABI has some
430 more optimizations, it can use a register value instead of a memory-saved
956cd1d6 431 offset and even an other register than $gp as global pointer. */
6478892d
TS
432static offsetT mips_cpreturn_offset = -1;
433static int mips_cpreturn_register = -1;
434static int mips_gp_register = GP;
def2e0dd 435static int mips_gprel_offset = 0;
6478892d 436
7a621144
DJ
437/* Whether mips_cprestore_offset has been set in the current function
438 (or whether it has already been warned about, if not). */
439static int mips_cprestore_valid = 0;
440
252b5132
RH
441/* This is the register which holds the stack frame, as set by the
442 .frame pseudo-op. This is needed to implement .cprestore. */
443static int mips_frame_reg = SP;
444
7a621144
DJ
445/* Whether mips_frame_reg has been set in the current function
446 (or whether it has already been warned about, if not). */
447static int mips_frame_reg_valid = 0;
448
252b5132
RH
449/* To output NOP instructions correctly, we need to keep information
450 about the previous two instructions. */
451
452/* Whether we are optimizing. The default value of 2 means to remove
453 unneeded NOPs and swap branch instructions when possible. A value
454 of 1 means to not swap branches. A value of 0 means to always
455 insert NOPs. */
456static int mips_optimize = 2;
457
458/* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
459 equivalent to seeing no -g option at all. */
460static int mips_debug = 0;
461
462/* The previous instruction. */
463static struct mips_cl_insn prev_insn;
464
465/* The instruction before prev_insn. */
466static struct mips_cl_insn prev_prev_insn;
467
468/* If we don't want information for prev_insn or prev_prev_insn, we
469 point the insn_mo field at this dummy integer. */
43841e91 470static const struct mips_opcode dummy_opcode = { NULL, NULL, 0, 0, 0, 0 };
252b5132
RH
471
472/* Non-zero if prev_insn is valid. */
473static int prev_insn_valid;
474
475/* The frag for the previous instruction. */
476static struct frag *prev_insn_frag;
477
478/* The offset into prev_insn_frag for the previous instruction. */
479static long prev_insn_where;
480
481/* The reloc type for the previous instruction, if any. */
f6688943 482static bfd_reloc_code_real_type prev_insn_reloc_type[3];
252b5132
RH
483
484/* The reloc for the previous instruction, if any. */
f6688943 485static fixS *prev_insn_fixp[3];
252b5132
RH
486
487/* Non-zero if the previous instruction was in a delay slot. */
488static int prev_insn_is_delay_slot;
489
490/* Non-zero if the previous instruction was in a .set noreorder. */
491static int prev_insn_unreordered;
492
493/* Non-zero if the previous instruction uses an extend opcode (if
494 mips16). */
495static int prev_insn_extended;
496
497/* Non-zero if the previous previous instruction was in a .set
498 noreorder. */
499static int prev_prev_insn_unreordered;
500
501/* If this is set, it points to a frag holding nop instructions which
502 were inserted before the start of a noreorder section. If those
503 nops turn out to be unnecessary, the size of the frag can be
504 decreased. */
505static fragS *prev_nop_frag;
506
507/* The number of nop instructions we created in prev_nop_frag. */
508static int prev_nop_frag_holds;
509
510/* The number of nop instructions that we know we need in
bdaaa2e1 511 prev_nop_frag. */
252b5132
RH
512static int prev_nop_frag_required;
513
514/* The number of instructions we've seen since prev_nop_frag. */
515static int prev_nop_frag_since;
516
517/* For ECOFF and ELF, relocations against symbols are done in two
518 parts, with a HI relocation and a LO relocation. Each relocation
519 has only 16 bits of space to store an addend. This means that in
520 order for the linker to handle carries correctly, it must be able
521 to locate both the HI and the LO relocation. This means that the
522 relocations must appear in order in the relocation table.
523
524 In order to implement this, we keep track of each unmatched HI
525 relocation. We then sort them so that they immediately precede the
bdaaa2e1 526 corresponding LO relocation. */
252b5132 527
e972090a
NC
528struct mips_hi_fixup
529{
252b5132
RH
530 /* Next HI fixup. */
531 struct mips_hi_fixup *next;
532 /* This fixup. */
533 fixS *fixp;
534 /* The section this fixup is in. */
535 segT seg;
536};
537
538/* The list of unmatched HI relocs. */
539
540static struct mips_hi_fixup *mips_hi_fixup_list;
541
542/* Map normal MIPS register numbers to mips16 register numbers. */
543
544#define X ILLEGAL_REG
e972090a
NC
545static const int mips32_to_16_reg_map[] =
546{
252b5132
RH
547 X, X, 2, 3, 4, 5, 6, 7,
548 X, X, X, X, X, X, X, X,
549 0, 1, X, X, X, X, X, X,
550 X, X, X, X, X, X, X, X
551};
552#undef X
553
554/* Map mips16 register numbers to normal MIPS register numbers. */
555
e972090a
NC
556static const unsigned int mips16_to_32_reg_map[] =
557{
252b5132
RH
558 16, 17, 2, 3, 4, 5, 6, 7
559};
560\f
561/* Since the MIPS does not have multiple forms of PC relative
562 instructions, we do not have to do relaxing as is done on other
563 platforms. However, we do have to handle GP relative addressing
564 correctly, which turns out to be a similar problem.
565
566 Every macro that refers to a symbol can occur in (at least) two
567 forms, one with GP relative addressing and one without. For
568 example, loading a global variable into a register generally uses
569 a macro instruction like this:
570 lw $4,i
571 If i can be addressed off the GP register (this is true if it is in
572 the .sbss or .sdata section, or if it is known to be smaller than
573 the -G argument) this will generate the following instruction:
574 lw $4,i($gp)
575 This instruction will use a GPREL reloc. If i can not be addressed
576 off the GP register, the following instruction sequence will be used:
577 lui $at,i
578 lw $4,i($at)
579 In this case the first instruction will have a HI16 reloc, and the
580 second reloc will have a LO16 reloc. Both relocs will be against
581 the symbol i.
582
583 The issue here is that we may not know whether i is GP addressable
584 until after we see the instruction that uses it. Therefore, we
585 want to be able to choose the final instruction sequence only at
586 the end of the assembly. This is similar to the way other
587 platforms choose the size of a PC relative instruction only at the
588 end of assembly.
589
590 When generating position independent code we do not use GP
591 addressing in quite the same way, but the issue still arises as
592 external symbols and local symbols must be handled differently.
593
594 We handle these issues by actually generating both possible
595 instruction sequences. The longer one is put in a frag_var with
596 type rs_machine_dependent. We encode what to do with the frag in
597 the subtype field. We encode (1) the number of existing bytes to
598 replace, (2) the number of new bytes to use, (3) the offset from
599 the start of the existing bytes to the first reloc we must generate
600 (that is, the offset is applied from the start of the existing
601 bytes after they are replaced by the new bytes, if any), (4) the
602 offset from the start of the existing bytes to the second reloc,
603 (5) whether a third reloc is needed (the third reloc is always four
604 bytes after the second reloc), and (6) whether to warn if this
605 variant is used (this is sometimes needed if .set nomacro or .set
606 noat is in effect). All these numbers are reasonably small.
607
608 Generating two instruction sequences must be handled carefully to
609 ensure that delay slots are handled correctly. Fortunately, there
610 are a limited number of cases. When the second instruction
611 sequence is generated, append_insn is directed to maintain the
612 existing delay slot information, so it continues to apply to any
613 code after the second instruction sequence. This means that the
614 second instruction sequence must not impose any requirements not
615 required by the first instruction sequence.
616
617 These variant frags are then handled in functions called by the
618 machine independent code. md_estimate_size_before_relax returns
619 the final size of the frag. md_convert_frag sets up the final form
620 of the frag. tc_gen_reloc adjust the first reloc and adds a second
621 one if needed. */
622#define RELAX_ENCODE(old, new, reloc1, reloc2, reloc3, warn) \
623 ((relax_substateT) \
624 (((old) << 23) \
625 | ((new) << 16) \
626 | (((reloc1) + 64) << 9) \
627 | (((reloc2) + 64) << 2) \
628 | ((reloc3) ? (1 << 1) : 0) \
629 | ((warn) ? 1 : 0)))
630#define RELAX_OLD(i) (((i) >> 23) & 0x7f)
631#define RELAX_NEW(i) (((i) >> 16) & 0x7f)
9a41af64
TS
632#define RELAX_RELOC1(i) ((valueT) (((i) >> 9) & 0x7f) - 64)
633#define RELAX_RELOC2(i) ((valueT) (((i) >> 2) & 0x7f) - 64)
252b5132
RH
634#define RELAX_RELOC3(i) (((i) >> 1) & 1)
635#define RELAX_WARN(i) ((i) & 1)
636
637/* For mips16 code, we use an entirely different form of relaxation.
638 mips16 supports two versions of most instructions which take
639 immediate values: a small one which takes some small value, and a
640 larger one which takes a 16 bit value. Since branches also follow
641 this pattern, relaxing these values is required.
642
643 We can assemble both mips16 and normal MIPS code in a single
644 object. Therefore, we need to support this type of relaxation at
645 the same time that we support the relaxation described above. We
646 use the high bit of the subtype field to distinguish these cases.
647
648 The information we store for this type of relaxation is the
649 argument code found in the opcode file for this relocation, whether
650 the user explicitly requested a small or extended form, and whether
651 the relocation is in a jump or jal delay slot. That tells us the
652 size of the value, and how it should be stored. We also store
653 whether the fragment is considered to be extended or not. We also
654 store whether this is known to be a branch to a different section,
655 whether we have tried to relax this frag yet, and whether we have
656 ever extended a PC relative fragment because of a shift count. */
657#define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
658 (0x80000000 \
659 | ((type) & 0xff) \
660 | ((small) ? 0x100 : 0) \
661 | ((ext) ? 0x200 : 0) \
662 | ((dslot) ? 0x400 : 0) \
663 | ((jal_dslot) ? 0x800 : 0))
664#define RELAX_MIPS16_P(i) (((i) & 0x80000000) != 0)
665#define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
666#define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
667#define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
668#define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
669#define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
670#define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
671#define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
672#define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
673#define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
674#define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
675#define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
676\f
677/* Prototypes for static functions. */
678
679#ifdef __STDC__
680#define internalError() \
681 as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
682#else
683#define internalError() as_fatal (_("MIPS internal Error"));
684#endif
685
686enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
687
688static int insn_uses_reg PARAMS ((struct mips_cl_insn *ip,
689 unsigned int reg, enum mips_regclass class));
156c2f8b 690static int reg_needs_delay PARAMS ((unsigned int));
252b5132
RH
691static void mips16_mark_labels PARAMS ((void));
692static void append_insn PARAMS ((char *place,
693 struct mips_cl_insn * ip,
694 expressionS * p,
f6688943 695 bfd_reloc_code_real_type *r,
252b5132
RH
696 boolean));
697static void mips_no_prev_insn PARAMS ((int));
698static void mips_emit_delays PARAMS ((boolean));
699#ifdef USE_STDARG
700static void macro_build PARAMS ((char *place, int *counter, expressionS * ep,
701 const char *name, const char *fmt,
702 ...));
703#else
704static void macro_build ();
705#endif
706static void mips16_macro_build PARAMS ((char *, int *, expressionS *,
707 const char *, const char *,
708 va_list));
438c16b8 709static void macro_build_jalr PARAMS ((int, expressionS *));
252b5132
RH
710static void macro_build_lui PARAMS ((char *place, int *counter,
711 expressionS * ep, int regnum));
712static void set_at PARAMS ((int *counter, int reg, int unsignedp));
713static void check_absolute_expr PARAMS ((struct mips_cl_insn * ip,
714 expressionS *));
715static void load_register PARAMS ((int *, int, expressionS *, int));
c9914766 716static void load_address PARAMS ((int *, int, expressionS *, int *));
ea1fb5dc 717static void move_register PARAMS ((int *, int, int));
252b5132
RH
718static void macro PARAMS ((struct mips_cl_insn * ip));
719static void mips16_macro PARAMS ((struct mips_cl_insn * ip));
720#ifdef LOSING_COMPILER
721static void macro2 PARAMS ((struct mips_cl_insn * ip));
722#endif
723static void mips_ip PARAMS ((char *str, struct mips_cl_insn * ip));
724static void mips16_ip PARAMS ((char *str, struct mips_cl_insn * ip));
725static void mips16_immed PARAMS ((char *, unsigned int, int, offsetT, boolean,
726 boolean, boolean, unsigned long *,
727 boolean *, unsigned short *));
394f9b3a 728static int my_getPercentOp PARAMS ((char **, unsigned int *, int *));
ad8d3bb3
TS
729static int my_getSmallParser PARAMS ((char **, unsigned int *, int *));
730static int my_getSmallExpression PARAMS ((expressionS *, char *));
731static void my_getExpression PARAMS ((expressionS *, char *));
ae948b86 732#ifdef OBJ_ELF
e013f690 733static int support_64bit_objects PARAMS((void));
ae948b86 734#endif
252b5132
RH
735static symbolS *get_symbol PARAMS ((void));
736static void mips_align PARAMS ((int to, int fill, symbolS *label));
737static void s_align PARAMS ((int));
738static void s_change_sec PARAMS ((int));
739static void s_cons PARAMS ((int));
740static void s_float_cons PARAMS ((int));
741static void s_mips_globl PARAMS ((int));
742static void s_option PARAMS ((int));
743static void s_mipsset PARAMS ((int));
744static void s_abicalls PARAMS ((int));
745static void s_cpload PARAMS ((int));
6478892d
TS
746static void s_cpsetup PARAMS ((int));
747static void s_cplocal PARAMS ((int));
252b5132 748static void s_cprestore PARAMS ((int));
6478892d
TS
749static void s_cpreturn PARAMS ((int));
750static void s_gpvalue PARAMS ((int));
252b5132
RH
751static void s_gpword PARAMS ((int));
752static void s_cpadd PARAMS ((int));
753static void s_insn PARAMS ((int));
754static void md_obj_begin PARAMS ((void));
755static void md_obj_end PARAMS ((void));
756static long get_number PARAMS ((void));
757static void s_mips_ent PARAMS ((int));
758static void s_mips_end PARAMS ((int));
759static void s_mips_frame PARAMS ((int));
760static void s_mips_mask PARAMS ((int));
761static void s_mips_stab PARAMS ((int));
762static void s_mips_weakext PARAMS ((int));
763static void s_file PARAMS ((int));
764static int mips16_extended_frag PARAMS ((fragS *, asection *, long));
e7af610e
NC
765static const char *mips_isa_to_str PARAMS ((int));
766static const char *mips_cpu_to_str PARAMS ((int));
252b5132 767static int validate_mips_insn PARAMS ((const struct mips_opcode *));
e87a0284 768static void show PARAMS ((FILE *, char *, int *, int *));
add55e1f
RS
769#ifdef OBJ_ELF
770static int mips_need_elf_addend_fixup PARAMS ((fixS *));
771#endif
e7af610e 772
ad8d3bb3 773/* Return values of my_getSmallExpression(). */
fb1b3232 774
ad8d3bb3 775enum small_ex_type
fb1b3232
TS
776{
777 S_EX_NONE = 0,
ad8d3bb3
TS
778 S_EX_REGISTER,
779
780 /* Direct relocation creation by %percent_op(). */
781 S_EX_HALF,
fb1b3232 782 S_EX_HI,
ad8d3bb3
TS
783 S_EX_LO,
784 S_EX_GP_REL,
785 S_EX_GOT,
786 S_EX_CALL16,
787 S_EX_GOT_DISP,
788 S_EX_GOT_PAGE,
789 S_EX_GOT_OFST,
790 S_EX_GOT_HI,
791 S_EX_GOT_LO,
792 S_EX_NEG,
fb1b3232
TS
793 S_EX_HIGHER,
794 S_EX_HIGHEST,
ad8d3bb3
TS
795 S_EX_CALL_HI,
796 S_EX_CALL_LO
fb1b3232
TS
797};
798
e7af610e
NC
799/* Table and functions used to map between CPU/ISA names, and
800 ISA levels, and CPU numbers. */
801
e972090a
NC
802struct mips_cpu_info
803{
e7af610e
NC
804 const char *name; /* CPU or ISA name. */
805 int is_isa; /* Is this an ISA? (If 0, a CPU.) */
806 int isa; /* ISA level. */
807 int cpu; /* CPU number (default CPU if ISA). */
808};
809
810static const struct mips_cpu_info *mips_cpu_info_from_name PARAMS ((const char *));
811static const struct mips_cpu_info *mips_cpu_info_from_isa PARAMS ((int));
812static const struct mips_cpu_info *mips_cpu_info_from_cpu PARAMS ((int));
252b5132
RH
813\f
814/* Pseudo-op table.
815
816 The following pseudo-ops from the Kane and Heinrich MIPS book
817 should be defined here, but are currently unsupported: .alias,
818 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
819
820 The following pseudo-ops from the Kane and Heinrich MIPS book are
821 specific to the type of debugging information being generated, and
822 should be defined by the object format: .aent, .begin, .bend,
823 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
824 .vreg.
825
826 The following pseudo-ops from the Kane and Heinrich MIPS book are
827 not MIPS CPU specific, but are also not specific to the object file
828 format. This file is probably the best place to define them, but
829 they are not currently supported: .asm0, .endr, .lab, .repeat,
830 .struct. */
831
e972090a
NC
832static const pseudo_typeS mips_pseudo_table[] =
833{
beae10d5 834 /* MIPS specific pseudo-ops. */
252b5132
RH
835 {"option", s_option, 0},
836 {"set", s_mipsset, 0},
837 {"rdata", s_change_sec, 'r'},
838 {"sdata", s_change_sec, 's'},
839 {"livereg", s_ignore, 0},
840 {"abicalls", s_abicalls, 0},
841 {"cpload", s_cpload, 0},
6478892d
TS
842 {"cpsetup", s_cpsetup, 0},
843 {"cplocal", s_cplocal, 0},
252b5132 844 {"cprestore", s_cprestore, 0},
6478892d
TS
845 {"cpreturn", s_cpreturn, 0},
846 {"gpvalue", s_gpvalue, 0},
252b5132
RH
847 {"gpword", s_gpword, 0},
848 {"cpadd", s_cpadd, 0},
849 {"insn", s_insn, 0},
850
beae10d5 851 /* Relatively generic pseudo-ops that happen to be used on MIPS
252b5132
RH
852 chips. */
853 {"asciiz", stringer, 1},
854 {"bss", s_change_sec, 'b'},
855 {"err", s_err, 0},
856 {"half", s_cons, 1},
857 {"dword", s_cons, 3},
858 {"weakext", s_mips_weakext, 0},
859
beae10d5 860 /* These pseudo-ops are defined in read.c, but must be overridden
252b5132
RH
861 here for one reason or another. */
862 {"align", s_align, 0},
863 {"byte", s_cons, 0},
864 {"data", s_change_sec, 'd'},
865 {"double", s_float_cons, 'd'},
866 {"float", s_float_cons, 'f'},
867 {"globl", s_mips_globl, 0},
868 {"global", s_mips_globl, 0},
869 {"hword", s_cons, 1},
870 {"int", s_cons, 2},
871 {"long", s_cons, 2},
872 {"octa", s_cons, 4},
873 {"quad", s_cons, 3},
874 {"short", s_cons, 1},
875 {"single", s_float_cons, 'f'},
876 {"stabn", s_mips_stab, 'n'},
877 {"text", s_change_sec, 't'},
878 {"word", s_cons, 2},
add56521
L
879
880#ifdef MIPS_STABS_ELF
881 { "extern", ecoff_directive_extern, 0},
882#endif
883
43841e91 884 { NULL, NULL, 0 },
252b5132
RH
885};
886
e972090a
NC
887static const pseudo_typeS mips_nonecoff_pseudo_table[] =
888{
beae10d5
KH
889 /* These pseudo-ops should be defined by the object file format.
890 However, a.out doesn't support them, so we have versions here. */
252b5132
RH
891 {"aent", s_mips_ent, 1},
892 {"bgnb", s_ignore, 0},
893 {"end", s_mips_end, 0},
894 {"endb", s_ignore, 0},
895 {"ent", s_mips_ent, 0},
896 {"file", s_file, 0},
897 {"fmask", s_mips_mask, 'F'},
898 {"frame", s_mips_frame, 0},
899 {"loc", s_ignore, 0},
900 {"mask", s_mips_mask, 'R'},
901 {"verstamp", s_ignore, 0},
43841e91 902 { NULL, NULL, 0 },
252b5132
RH
903};
904
905extern void pop_insert PARAMS ((const pseudo_typeS *));
906
907void
908mips_pop_insert ()
909{
910 pop_insert (mips_pseudo_table);
911 if (! ECOFF_DEBUGGING)
912 pop_insert (mips_nonecoff_pseudo_table);
913}
914\f
915/* Symbols labelling the current insn. */
916
e972090a
NC
917struct insn_label_list
918{
252b5132
RH
919 struct insn_label_list *next;
920 symbolS *label;
921};
922
923static struct insn_label_list *insn_labels;
924static struct insn_label_list *free_insn_labels;
925
926static void mips_clear_insn_labels PARAMS ((void));
927
928static inline void
929mips_clear_insn_labels ()
930{
931 register struct insn_label_list **pl;
932
933 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
934 ;
935 *pl = insn_labels;
936 insn_labels = NULL;
937}
938\f
939static char *expr_end;
940
941/* Expressions which appear in instructions. These are set by
942 mips_ip. */
943
944static expressionS imm_expr;
945static expressionS offset_expr;
946
947/* Relocs associated with imm_expr and offset_expr. */
948
f6688943
TS
949static bfd_reloc_code_real_type imm_reloc[3]
950 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
951static bfd_reloc_code_real_type offset_reloc[3]
952 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132
RH
953
954/* This is set by mips_ip if imm_reloc is an unmatched HI16_S reloc. */
955
956static boolean imm_unmatched_hi;
957
958/* These are set by mips16_ip if an explicit extension is used. */
959
960static boolean mips16_small, mips16_ext;
961
962#ifdef MIPS_STABS_ELF
963/* The pdr segment for per procedure frame/regmask info */
964
965static segT pdr_seg;
966#endif
967
e7af610e
NC
968static const char *
969mips_isa_to_str (isa)
970 int isa;
971{
972 const struct mips_cpu_info *ci;
973 static char s[20];
974
975 ci = mips_cpu_info_from_isa (isa);
976 if (ci != NULL)
977 return (ci->name);
978
979 sprintf (s, "ISA#%d", isa);
980 return s;
981}
982
983static const char *
156c2f8b
NC
984mips_cpu_to_str (cpu)
985 int cpu;
986{
e7af610e 987 const struct mips_cpu_info *ci;
156c2f8b 988 static char s[16];
e7af610e
NC
989
990 ci = mips_cpu_info_from_cpu (cpu);
991 if (ci != NULL)
992 return (ci->name);
993
994 sprintf (s, "CPU#%d", cpu);
995 return s;
156c2f8b
NC
996}
997
e013f690
TS
998/* The default target format to use. */
999
1000const char *
1001mips_target_format ()
1002{
1003 switch (OUTPUT_FLAVOR)
1004 {
1005 case bfd_target_aout_flavour:
1006 return target_big_endian ? "a.out-mips-big" : "a.out-mips-little";
1007 case bfd_target_ecoff_flavour:
1008 return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
1009 case bfd_target_coff_flavour:
1010 return "pe-mips";
1011 case bfd_target_elf_flavour:
1012#ifdef TE_TMIPS
1013 /* This is traditional mips */
1014 return (target_big_endian
1015 ? (HAVE_64BIT_OBJECTS ? "elf64-tradbigmips"
1016 : "elf32-tradbigmips")
1017 : (HAVE_64BIT_OBJECTS ? "elf64-tradlittlemips"
1018 : "elf32-tradlittlemips"));
1019#else
1020 return (target_big_endian
1021 ? (HAVE_64BIT_OBJECTS ? "elf64-bigmips" : "elf32-bigmips")
1022 : (HAVE_64BIT_OBJECTS ? "elf64-littlemips"
1023 : "elf32-littlemips"));
1024#endif
1025 default:
1026 abort ();
1027 return NULL;
1028 }
1029}
1030
156c2f8b
NC
1031/* This function is called once, at assembler startup time. It should
1032 set up all the tables, etc. that the MD part of the assembler will need. */
1033
252b5132
RH
1034void
1035md_begin ()
1036{
252b5132 1037 register const char *retval = NULL;
156c2f8b 1038 int i = 0;
252b5132
RH
1039 const char *cpu;
1040 char *a = NULL;
1041 int broken = 0;
1042 int mips_isa_from_cpu;
e7af610e
NC
1043 int target_cpu_had_mips16 = 0;
1044 const struct mips_cpu_info *ci;
252b5132 1045
056350c6
NC
1046 /* GP relative stuff not working for PE */
1047 if (strncmp (TARGET_OS, "pe", 2) == 0
1048 && g_switch_value != 0)
1049 {
1050 if (g_switch_seen)
1051 as_bad (_("-G not supported in this configuration."));
1052 g_switch_value = 0;
1053 }
1054
252b5132
RH
1055 cpu = TARGET_CPU;
1056 if (strcmp (cpu + (sizeof TARGET_CPU) - 3, "el") == 0)
1057 {
1058 a = xmalloc (sizeof TARGET_CPU);
1059 strcpy (a, TARGET_CPU);
1060 a[(sizeof TARGET_CPU) - 3] = '\0';
1061 cpu = a;
1062 }
1063
e7af610e 1064 if (strncmp (cpu, "mips16", sizeof "mips16" - 1) == 0)
252b5132 1065 {
e7af610e
NC
1066 target_cpu_had_mips16 = 1;
1067 cpu += sizeof "mips16" - 1;
252b5132
RH
1068 }
1069
e7af610e
NC
1070 if (mips_opts.mips16 < 0)
1071 mips_opts.mips16 = target_cpu_had_mips16;
252b5132 1072
6dce9e24
TS
1073 /* Backward compatibility for historic -mcpu= option. Check for
1074 incompatible options, warn if -mcpu is used. */
1075 if (mips_cpu != CPU_UNKNOWN
1076 && mips_arch != CPU_UNKNOWN
1077 && mips_cpu != mips_arch)
1078 {
1079 as_fatal (_("The -mcpu option can't be used together with -march. "
1080 "Use -mtune instead of -mcpu."));
1081 }
1082
1083 if (mips_cpu != CPU_UNKNOWN
1084 && mips_tune != CPU_UNKNOWN
1085 && mips_cpu != mips_tune)
1086 {
1087 as_fatal (_("The -mcpu option can't be used together with -mtune. "
1088 "Use -march instead of -mcpu."));
1089 }
1090
a8e8e863
DJ
1091#if 1
1092 /* For backward compatibility, let -mipsN set various defaults. */
1093 /* This code should go away, to be replaced with something rather more
1094 draconian. Until GCC 3.1 has been released for some reasonable
1095 amount of time, however, we need to support this. */
1096 if (mips_opts.isa != ISA_UNKNOWN)
1097 {
1098 /* Translate -mipsN to the appropriate settings of file_mips_gp32
1099 and file_mips_fp32. Tag binaries as using the mipsN ISA. */
1100 if (file_mips_gp32 < 0)
1101 {
1102 if (ISA_HAS_64BIT_REGS (mips_opts.isa))
1103 file_mips_gp32 = 0;
1104 else
1105 file_mips_gp32 = 1;
1106 }
1107 if (file_mips_fp32 < 0)
1108 {
1109 if (ISA_HAS_64BIT_REGS (mips_opts.isa))
1110 file_mips_fp32 = 0;
1111 else
1112 file_mips_fp32 = 1;
1113 }
1114
1115 ci = mips_cpu_info_from_isa (mips_opts.isa);
1116 assert (ci != NULL);
1117 /* -mipsN has higher priority than -mcpu but lower than -march. */
1118 if (mips_arch == CPU_UNKNOWN)
1119 mips_arch = ci->cpu;
1120
1121 /* Default mips_abi. */
1122 if (mips_opts.abi == NO_ABI)
1123 {
1124 if (mips_opts.isa == ISA_MIPS1 || mips_opts.isa == ISA_MIPS2)
1125 mips_opts.abi = O32_ABI;
1126 else if (mips_opts.isa == ISA_MIPS3 || mips_opts.isa == ISA_MIPS4)
1127 mips_opts.abi = O64_ABI;
1128 }
1129 }
1130
6dce9e24
TS
1131 if (mips_arch == CPU_UNKNOWN && mips_cpu != CPU_UNKNOWN)
1132 {
1133 ci = mips_cpu_info_from_cpu (mips_cpu);
1134 assert (ci != NULL);
1135 mips_arch = ci->cpu;
1136 as_warn (_("The -mcpu option is deprecated. Please use -march and "
1137 "-mtune instead."));
1138 }
1139
a8e8e863
DJ
1140 /* Set tune from -mcpu, not from -mipsN. */
1141 if (mips_tune == CPU_UNKNOWN && mips_cpu != CPU_UNKNOWN)
1142 {
1143 ci = mips_cpu_info_from_cpu (mips_cpu);
1144 assert (ci != NULL);
1145 mips_tune = ci->cpu;
1146 }
1147
ec68c924 1148 /* At this point, mips_arch will either be CPU_UNKNOWN if no ARCH was
e7af610e
NC
1149 specified on the command line, or some other value if one was.
1150 Similarly, mips_opts.isa will be ISA_UNKNOWN if not specified on
1151 the command line, or will be set otherwise if one was. */
a8e8e863
DJ
1152
1153 if (mips_arch != CPU_UNKNOWN && mips_opts.isa != ISA_UNKNOWN)
1154 /* Handled above. */;
1155#else
1156 if (mips_arch == CPU_UNKNOWN && mips_cpu != CPU_UNKNOWN)
1157 {
1158 ci = mips_cpu_info_from_cpu (mips_cpu);
1159 assert (ci != NULL);
1160 mips_arch = ci->cpu;
1161 as_warn (_("The -mcpu option is deprecated. Please use -march and "
1162 "-mtune instead."));
1163 }
1164
1165 /* At this point, mips_arch will either be CPU_UNKNOWN if no ARCH was
1166 specified on the command line, or some other value if one was.
1167 Similarly, mips_opts.isa will be ISA_UNKNOWN if not specified on
1168 the command line, or will be set otherwise if one was. */
1169
ec68c924 1170 if (mips_arch != CPU_UNKNOWN && mips_opts.isa != ISA_UNKNOWN)
252b5132 1171 {
1ff54a84
TS
1172 /* We have to check if the isa is the default isa of arch. Otherwise
1173 we'll get invalid object file headers. */
1174 ci = mips_cpu_info_from_cpu (mips_arch);
1175 assert (ci != NULL);
1176 if (mips_opts.isa != ci->isa)
1177 {
1178 /* This really should be an error instead of a warning, but old
1179 compilers only have -mcpu which sets both arch and tune. For
1180 now, we discard arch and preserve tune. */
1181 as_warn (_("The -march option is incompatible to -mipsN and "
1182 "therefore ignored."));
1183 if (mips_tune == CPU_UNKNOWN)
1184 mips_tune = mips_arch;
1185 ci = mips_cpu_info_from_isa (mips_opts.isa);
1186 assert (ci != NULL);
1187 mips_arch = ci->cpu;
1188 }
252b5132 1189 }
a8e8e863 1190#endif
ec68c924 1191 else if (mips_arch != CPU_UNKNOWN && mips_opts.isa == ISA_UNKNOWN)
252b5132 1192 {
ec68c924
EC
1193 /* We have ARCH, we need ISA. */
1194 ci = mips_cpu_info_from_cpu (mips_arch);
e7af610e
NC
1195 assert (ci != NULL);
1196 mips_opts.isa = ci->isa;
1197 }
ec68c924 1198 else if (mips_arch == CPU_UNKNOWN && mips_opts.isa != ISA_UNKNOWN)
e7af610e 1199 {
ec68c924 1200 /* We have ISA, we need default ARCH. */
e7af610e
NC
1201 ci = mips_cpu_info_from_isa (mips_opts.isa);
1202 assert (ci != NULL);
ec68c924
EC
1203 mips_arch = ci->cpu;
1204 }
e7af610e
NC
1205 else
1206 {
ec68c924 1207 /* We need to set both ISA and ARCH from target cpu. */
e7af610e
NC
1208 ci = mips_cpu_info_from_name (cpu);
1209 if (ci == NULL)
beae10d5 1210 ci = mips_cpu_info_from_cpu (CPU_R3000);
e7af610e
NC
1211 assert (ci != NULL);
1212 mips_opts.isa = ci->isa;
ec68c924 1213 mips_arch = ci->cpu;
252b5132
RH
1214 }
1215
ec68c924
EC
1216 if (mips_tune == CPU_UNKNOWN)
1217 mips_tune = mips_arch;
1218
1219 ci = mips_cpu_info_from_cpu (mips_arch);
e7af610e
NC
1220 assert (ci != NULL);
1221 mips_isa_from_cpu = ci->isa;
1222
252b5132 1223 /* End of TARGET_CPU processing, get rid of malloced memory
bdaaa2e1 1224 if necessary. */
252b5132
RH
1225 cpu = NULL;
1226 if (a != NULL)
1227 {
156c2f8b
NC
1228 free (a);
1229 a = NULL;
252b5132
RH
1230 }
1231
e7af610e 1232 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
252b5132
RH
1233 as_bad (_("trap exception not supported at ISA 1"));
1234
1235 /* Set the EABI kind based on the ISA before the user gets
1236 to change the ISA with directives. This isn't really
bdaaa2e1 1237 the best, but then neither is basing the abi on the isa. */
9ce8a5dd 1238 if (ISA_HAS_64BIT_REGS (mips_opts.isa)
a325df1d 1239 && mips_opts.abi == EABI_ABI)
252b5132
RH
1240 mips_eabi64 = 1;
1241
e7af610e
NC
1242 /* If they asked for mips1 or mips2 and a cpu that is
1243 mips3 or greater, then mark the object file 32BITMODE. */
1244 if (mips_isa_from_cpu != ISA_UNKNOWN
1245 && ! ISA_HAS_64BIT_REGS (mips_opts.isa)
1246 && ISA_HAS_64BIT_REGS (mips_isa_from_cpu))
1247 mips_32bitmode = 1;
252b5132 1248
1f25f5d3
CD
1249 /* If the selected architecture includes support for ASEs, enable
1250 generation of code for them. */
1251 if (mips_opts.ase_mips3d == -1 && CPU_HAS_MIPS3D (mips_arch))
1252 mips_opts.ase_mips3d = 1;
deec1734
CD
1253 if (mips_opts.ase_mdmx == -1 && CPU_HAS_MDMX (mips_arch))
1254 mips_opts.ase_mdmx = 1;
1f25f5d3 1255
ec68c924 1256 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, mips_arch))
252b5132
RH
1257 as_warn (_("Could not set architecture and machine"));
1258
a8e8e863
DJ
1259 if (file_mips_gp32 < 0)
1260 file_mips_gp32 = 0;
1261 if (file_mips_fp32 < 0)
1262 file_mips_fp32 = 0;
1263
252b5132 1264 file_mips_isa = mips_opts.isa;
a325df1d 1265 file_mips_abi = mips_opts.abi;
1f25f5d3 1266 file_ase_mips3d = mips_opts.ase_mips3d;
deec1734 1267 file_ase_mdmx = mips_opts.ase_mdmx;
a325df1d
TS
1268 mips_opts.gp32 = file_mips_gp32;
1269 mips_opts.fp32 = file_mips_fp32;
252b5132 1270
438c16b8
TS
1271 if (HAVE_NEWABI)
1272 mips_big_got = 1;
1273
252b5132
RH
1274 op_hash = hash_new ();
1275
1276 for (i = 0; i < NUMOPCODES;)
1277 {
1278 const char *name = mips_opcodes[i].name;
1279
1280 retval = hash_insert (op_hash, name, (PTR) &mips_opcodes[i]);
1281 if (retval != NULL)
1282 {
1283 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1284 mips_opcodes[i].name, retval);
1285 /* Probably a memory allocation problem? Give up now. */
1286 as_fatal (_("Broken assembler. No assembly attempted."));
1287 }
1288 do
1289 {
1290 if (mips_opcodes[i].pinfo != INSN_MACRO)
1291 {
1292 if (!validate_mips_insn (&mips_opcodes[i]))
1293 broken = 1;
1294 }
1295 ++i;
1296 }
1297 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1298 }
1299
1300 mips16_op_hash = hash_new ();
1301
1302 i = 0;
1303 while (i < bfd_mips16_num_opcodes)
1304 {
1305 const char *name = mips16_opcodes[i].name;
1306
1307 retval = hash_insert (mips16_op_hash, name, (PTR) &mips16_opcodes[i]);
1308 if (retval != NULL)
1309 as_fatal (_("internal: can't hash `%s': %s"),
1310 mips16_opcodes[i].name, retval);
1311 do
1312 {
1313 if (mips16_opcodes[i].pinfo != INSN_MACRO
1314 && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1315 != mips16_opcodes[i].match))
1316 {
1317 fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1318 mips16_opcodes[i].name, mips16_opcodes[i].args);
1319 broken = 1;
1320 }
1321 ++i;
1322 }
1323 while (i < bfd_mips16_num_opcodes
1324 && strcmp (mips16_opcodes[i].name, name) == 0);
1325 }
1326
1327 if (broken)
1328 as_fatal (_("Broken assembler. No assembly attempted."));
1329
1330 /* We add all the general register names to the symbol table. This
1331 helps us detect invalid uses of them. */
1332 for (i = 0; i < 32; i++)
1333 {
1334 char buf[5];
1335
1336 sprintf (buf, "$%d", i);
1337 symbol_table_insert (symbol_new (buf, reg_section, i,
1338 &zero_address_frag));
1339 }
76db943d
TS
1340 symbol_table_insert (symbol_new ("$ra", reg_section, RA,
1341 &zero_address_frag));
252b5132
RH
1342 symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1343 &zero_address_frag));
1344 symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1345 &zero_address_frag));
1346 symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1347 &zero_address_frag));
1348 symbol_table_insert (symbol_new ("$at", reg_section, AT,
1349 &zero_address_frag));
1350 symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1351 &zero_address_frag));
1352 symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1353 &zero_address_frag));
1354 symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1355 &zero_address_frag));
1356
1357 mips_no_prev_insn (false);
1358
1359 mips_gprmask = 0;
1360 mips_cprmask[0] = 0;
1361 mips_cprmask[1] = 0;
1362 mips_cprmask[2] = 0;
1363 mips_cprmask[3] = 0;
1364
1365 /* set the default alignment for the text section (2**2) */
1366 record_alignment (text_section, 2);
1367
1368 if (USE_GLOBAL_POINTER_OPT)
1369 bfd_set_gp_size (stdoutput, g_switch_value);
1370
1371 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1372 {
1373 /* On a native system, sections must be aligned to 16 byte
1374 boundaries. When configured for an embedded ELF target, we
1375 don't bother. */
1376 if (strcmp (TARGET_OS, "elf") != 0)
1377 {
1378 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1379 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1380 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1381 }
1382
1383 /* Create a .reginfo section for register masks and a .mdebug
1384 section for debugging information. */
1385 {
1386 segT seg;
1387 subsegT subseg;
1388 flagword flags;
1389 segT sec;
1390
1391 seg = now_seg;
1392 subseg = now_subseg;
1393
1394 /* The ABI says this section should be loaded so that the
1395 running program can access it. However, we don't load it
1396 if we are configured for an embedded target */
1397 flags = SEC_READONLY | SEC_DATA;
1398 if (strcmp (TARGET_OS, "elf") != 0)
1399 flags |= SEC_ALLOC | SEC_LOAD;
1400
195325d2 1401 if (file_mips_abi != N64_ABI)
252b5132
RH
1402 {
1403 sec = subseg_new (".reginfo", (subsegT) 0);
1404
195325d2
TS
1405 bfd_set_section_flags (stdoutput, sec, flags);
1406 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
bdaaa2e1 1407
252b5132
RH
1408#ifdef OBJ_ELF
1409 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1410#endif
1411 }
1412 else
1413 {
1414 /* The 64-bit ABI uses a .MIPS.options section rather than
1415 .reginfo section. */
1416 sec = subseg_new (".MIPS.options", (subsegT) 0);
195325d2
TS
1417 bfd_set_section_flags (stdoutput, sec, flags);
1418 bfd_set_section_alignment (stdoutput, sec, 3);
252b5132
RH
1419
1420#ifdef OBJ_ELF
1421 /* Set up the option header. */
1422 {
1423 Elf_Internal_Options opthdr;
1424 char *f;
1425
1426 opthdr.kind = ODK_REGINFO;
1427 opthdr.size = (sizeof (Elf_External_Options)
1428 + sizeof (Elf64_External_RegInfo));
1429 opthdr.section = 0;
1430 opthdr.info = 0;
1431 f = frag_more (sizeof (Elf_External_Options));
1432 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1433 (Elf_External_Options *) f);
1434
1435 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1436 }
1437#endif
1438 }
1439
1440 if (ECOFF_DEBUGGING)
1441 {
1442 sec = subseg_new (".mdebug", (subsegT) 0);
1443 (void) bfd_set_section_flags (stdoutput, sec,
1444 SEC_HAS_CONTENTS | SEC_READONLY);
1445 (void) bfd_set_section_alignment (stdoutput, sec, 2);
1446 }
1447
1448#ifdef MIPS_STABS_ELF
1449 pdr_seg = subseg_new (".pdr", (subsegT) 0);
1450 (void) bfd_set_section_flags (stdoutput, pdr_seg,
1451 SEC_READONLY | SEC_RELOC | SEC_DEBUGGING);
1452 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1453#endif
1454
1455 subseg_set (seg, subseg);
1456 }
1457 }
1458
1459 if (! ECOFF_DEBUGGING)
1460 md_obj_begin ();
1461}
1462
1463void
1464md_mips_end ()
1465{
1466 if (! ECOFF_DEBUGGING)
1467 md_obj_end ();
1468}
1469
1470void
1471md_assemble (str)
1472 char *str;
1473{
1474 struct mips_cl_insn insn;
f6688943
TS
1475 bfd_reloc_code_real_type unused_reloc[3]
1476 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132
RH
1477
1478 imm_expr.X_op = O_absent;
252b5132
RH
1479 imm_unmatched_hi = false;
1480 offset_expr.X_op = O_absent;
f6688943
TS
1481 imm_reloc[0] = BFD_RELOC_UNUSED;
1482 imm_reloc[1] = BFD_RELOC_UNUSED;
1483 imm_reloc[2] = BFD_RELOC_UNUSED;
1484 offset_reloc[0] = BFD_RELOC_UNUSED;
1485 offset_reloc[1] = BFD_RELOC_UNUSED;
1486 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
1487
1488 if (mips_opts.mips16)
1489 mips16_ip (str, &insn);
1490 else
1491 {
1492 mips_ip (str, &insn);
beae10d5
KH
1493 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1494 str, insn.insn_opcode));
252b5132
RH
1495 }
1496
1497 if (insn_error)
1498 {
1499 as_bad ("%s `%s'", insn_error, str);
1500 return;
1501 }
1502
1503 if (insn.insn_mo->pinfo == INSN_MACRO)
1504 {
1505 if (mips_opts.mips16)
1506 mips16_macro (&insn);
1507 else
1508 macro (&insn);
1509 }
1510 else
1511 {
1512 if (imm_expr.X_op != O_absent)
c4e7957c 1513 append_insn (NULL, &insn, &imm_expr, imm_reloc, imm_unmatched_hi);
252b5132 1514 else if (offset_expr.X_op != O_absent)
c4e7957c 1515 append_insn (NULL, &insn, &offset_expr, offset_reloc, false);
252b5132 1516 else
c4e7957c 1517 append_insn (NULL, &insn, NULL, unused_reloc, false);
252b5132
RH
1518 }
1519}
1520
1521/* See whether instruction IP reads register REG. CLASS is the type
1522 of register. */
1523
1524static int
1525insn_uses_reg (ip, reg, class)
1526 struct mips_cl_insn *ip;
1527 unsigned int reg;
1528 enum mips_regclass class;
1529{
1530 if (class == MIPS16_REG)
1531 {
1532 assert (mips_opts.mips16);
1533 reg = mips16_to_32_reg_map[reg];
1534 class = MIPS_GR_REG;
1535 }
1536
1537 /* Don't report on general register 0, since it never changes. */
1538 if (class == MIPS_GR_REG && reg == 0)
1539 return 0;
1540
1541 if (class == MIPS_FP_REG)
1542 {
1543 assert (! mips_opts.mips16);
1544 /* If we are called with either $f0 or $f1, we must check $f0.
1545 This is not optimal, because it will introduce an unnecessary
1546 NOP between "lwc1 $f0" and "swc1 $f1". To fix this we would
1547 need to distinguish reading both $f0 and $f1 or just one of
1548 them. Note that we don't have to check the other way,
1549 because there is no instruction that sets both $f0 and $f1
1550 and requires a delay. */
1551 if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1552 && ((((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS) &~(unsigned)1)
1553 == (reg &~ (unsigned) 1)))
1554 return 1;
1555 if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1556 && ((((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT) &~(unsigned)1)
1557 == (reg &~ (unsigned) 1)))
1558 return 1;
1559 }
1560 else if (! mips_opts.mips16)
1561 {
1562 if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1563 && ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == reg)
1564 return 1;
1565 if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1566 && ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT) == reg)
1567 return 1;
1568 }
1569 else
1570 {
1571 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1572 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RX)
1573 & MIPS16OP_MASK_RX)]
1574 == reg))
1575 return 1;
1576 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1577 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RY)
1578 & MIPS16OP_MASK_RY)]
1579 == reg))
1580 return 1;
1581 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1582 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
1583 & MIPS16OP_MASK_MOVE32Z)]
1584 == reg))
1585 return 1;
1586 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1587 return 1;
1588 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1589 return 1;
1590 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1591 return 1;
1592 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1593 && ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
1594 & MIPS16OP_MASK_REGR32) == reg)
1595 return 1;
1596 }
1597
1598 return 0;
1599}
1600
1601/* This function returns true if modifying a register requires a
1602 delay. */
1603
1604static int
1605reg_needs_delay (reg)
156c2f8b 1606 unsigned int reg;
252b5132
RH
1607{
1608 unsigned long prev_pinfo;
1609
1610 prev_pinfo = prev_insn.insn_mo->pinfo;
1611 if (! mips_opts.noreorder
9ce8a5dd 1612 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
1613 && ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1614 || (! gpr_interlocks
1615 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1616 {
1617 /* A load from a coprocessor or from memory. All load
1618 delays delay the use of general register rt for one
1619 instruction on the r3000. The r6000 and r4000 use
1620 interlocks. */
bdaaa2e1 1621 /* Itbl support may require additional care here. */
252b5132
RH
1622 know (prev_pinfo & INSN_WRITE_GPR_T);
1623 if (reg == ((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT))
1624 return 1;
1625 }
1626
1627 return 0;
1628}
1629
1630/* Mark instruction labels in mips16 mode. This permits the linker to
1631 handle them specially, such as generating jalx instructions when
1632 needed. We also make them odd for the duration of the assembly, in
1633 order to generate the right sort of code. We will make them even
1634 in the adjust_symtab routine, while leaving them marked. This is
1635 convenient for the debugger and the disassembler. The linker knows
1636 to make them odd again. */
1637
1638static void
1639mips16_mark_labels ()
1640{
1641 if (mips_opts.mips16)
1642 {
1643 struct insn_label_list *l;
98aa84af 1644 valueT val;
252b5132
RH
1645
1646 for (l = insn_labels; l != NULL; l = l->next)
1647 {
1648#ifdef OBJ_ELF
1649 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1650 S_SET_OTHER (l->label, STO_MIPS16);
1651#endif
98aa84af
AM
1652 val = S_GET_VALUE (l->label);
1653 if ((val & 1) == 0)
1654 S_SET_VALUE (l->label, val + 1);
252b5132
RH
1655 }
1656 }
1657}
1658
1659/* Output an instruction. PLACE is where to put the instruction; if
1660 it is NULL, this uses frag_more to get room. IP is the instruction
1661 information. ADDRESS_EXPR is an operand of the instruction to be
1662 used with RELOC_TYPE. */
1663
1664static void
1665append_insn (place, ip, address_expr, reloc_type, unmatched_hi)
1666 char *place;
1667 struct mips_cl_insn *ip;
1668 expressionS *address_expr;
f6688943 1669 bfd_reloc_code_real_type *reloc_type;
252b5132
RH
1670 boolean unmatched_hi;
1671{
1672 register unsigned long prev_pinfo, pinfo;
1673 char *f;
f6688943 1674 fixS *fixp[3];
252b5132
RH
1675 int nops = 0;
1676
1677 /* Mark instruction labels in mips16 mode. */
f9419b05 1678 mips16_mark_labels ();
252b5132
RH
1679
1680 prev_pinfo = prev_insn.insn_mo->pinfo;
1681 pinfo = ip->insn_mo->pinfo;
1682
1683 if (place == NULL && (! mips_opts.noreorder || prev_nop_frag != NULL))
1684 {
1685 int prev_prev_nop;
1686
1687 /* If the previous insn required any delay slots, see if we need
1688 to insert a NOP or two. There are eight kinds of possible
1689 hazards, of which an instruction can have at most one type.
1690 (1) a load from memory delay
1691 (2) a load from a coprocessor delay
1692 (3) an unconditional branch delay
1693 (4) a conditional branch delay
1694 (5) a move to coprocessor register delay
1695 (6) a load coprocessor register from memory delay
1696 (7) a coprocessor condition code delay
1697 (8) a HI/LO special register delay
1698
1699 There are a lot of optimizations we could do that we don't.
1700 In particular, we do not, in general, reorder instructions.
1701 If you use gcc with optimization, it will reorder
1702 instructions and generally do much more optimization then we
1703 do here; repeating all that work in the assembler would only
1704 benefit hand written assembly code, and does not seem worth
1705 it. */
1706
1707 /* This is how a NOP is emitted. */
1708#define emit_nop() \
1709 (mips_opts.mips16 \
1710 ? md_number_to_chars (frag_more (2), 0x6500, 2) \
1711 : md_number_to_chars (frag_more (4), 0, 4))
1712
1713 /* The previous insn might require a delay slot, depending upon
1714 the contents of the current insn. */
1715 if (! mips_opts.mips16
9ce8a5dd 1716 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
1717 && (((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1718 && ! cop_interlocks)
1719 || (! gpr_interlocks
1720 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1721 {
1722 /* A load from a coprocessor or from memory. All load
1723 delays delay the use of general register rt for one
1724 instruction on the r3000. The r6000 and r4000 use
1725 interlocks. */
beae10d5 1726 /* Itbl support may require additional care here. */
252b5132
RH
1727 know (prev_pinfo & INSN_WRITE_GPR_T);
1728 if (mips_optimize == 0
1729 || insn_uses_reg (ip,
1730 ((prev_insn.insn_opcode >> OP_SH_RT)
1731 & OP_MASK_RT),
1732 MIPS_GR_REG))
1733 ++nops;
1734 }
1735 else if (! mips_opts.mips16
9ce8a5dd 1736 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132 1737 && (((prev_pinfo & INSN_COPROC_MOVE_DELAY)
beae10d5 1738 && ! cop_interlocks)
e7af610e 1739 || (mips_opts.isa == ISA_MIPS1
252b5132
RH
1740 && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))))
1741 {
1742 /* A generic coprocessor delay. The previous instruction
1743 modified a coprocessor general or control register. If
1744 it modified a control register, we need to avoid any
1745 coprocessor instruction (this is probably not always
1746 required, but it sometimes is). If it modified a general
1747 register, we avoid using that register.
1748
1749 On the r6000 and r4000 loading a coprocessor register
1750 from memory is interlocked, and does not require a delay.
1751
1752 This case is not handled very well. There is no special
1753 knowledge of CP0 handling, and the coprocessors other
1754 than the floating point unit are not distinguished at
1755 all. */
1756 /* Itbl support may require additional care here. FIXME!
bdaaa2e1 1757 Need to modify this to include knowledge about
252b5132
RH
1758 user specified delays! */
1759 if (prev_pinfo & INSN_WRITE_FPR_T)
1760 {
1761 if (mips_optimize == 0
1762 || insn_uses_reg (ip,
1763 ((prev_insn.insn_opcode >> OP_SH_FT)
1764 & OP_MASK_FT),
1765 MIPS_FP_REG))
1766 ++nops;
1767 }
1768 else if (prev_pinfo & INSN_WRITE_FPR_S)
1769 {
1770 if (mips_optimize == 0
1771 || insn_uses_reg (ip,
1772 ((prev_insn.insn_opcode >> OP_SH_FS)
1773 & OP_MASK_FS),
1774 MIPS_FP_REG))
1775 ++nops;
1776 }
1777 else
1778 {
1779 /* We don't know exactly what the previous instruction
1780 does. If the current instruction uses a coprocessor
1781 register, we must insert a NOP. If previous
1782 instruction may set the condition codes, and the
1783 current instruction uses them, we must insert two
1784 NOPS. */
bdaaa2e1 1785 /* Itbl support may require additional care here. */
252b5132
RH
1786 if (mips_optimize == 0
1787 || ((prev_pinfo & INSN_WRITE_COND_CODE)
1788 && (pinfo & INSN_READ_COND_CODE)))
1789 nops += 2;
1790 else if (pinfo & INSN_COP)
1791 ++nops;
1792 }
1793 }
1794 else if (! mips_opts.mips16
9ce8a5dd 1795 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
1796 && (prev_pinfo & INSN_WRITE_COND_CODE)
1797 && ! cop_interlocks)
1798 {
1799 /* The previous instruction sets the coprocessor condition
1800 codes, but does not require a general coprocessor delay
1801 (this means it is a floating point comparison
1802 instruction). If this instruction uses the condition
1803 codes, we need to insert a single NOP. */
beae10d5 1804 /* Itbl support may require additional care here. */
252b5132
RH
1805 if (mips_optimize == 0
1806 || (pinfo & INSN_READ_COND_CODE))
1807 ++nops;
1808 }
6b76fefe
CM
1809
1810 /* If we're fixing up mfhi/mflo for the r7000 and the
1811 previous insn was an mfhi/mflo and the current insn
1812 reads the register that the mfhi/mflo wrote to, then
1813 insert two nops. */
1814
1815 else if (mips_7000_hilo_fix
1816 && MF_HILO_INSN (prev_pinfo)
1817 && insn_uses_reg (ip, ((prev_insn.insn_opcode >> OP_SH_RD)
beae10d5
KH
1818 & OP_MASK_RD),
1819 MIPS_GR_REG))
6b76fefe
CM
1820 {
1821 nops += 2;
1822 }
1823
1824 /* If we're fixing up mfhi/mflo for the r7000 and the
1825 2nd previous insn was an mfhi/mflo and the current insn
1826 reads the register that the mfhi/mflo wrote to, then
1827 insert one nop. */
1828
1829 else if (mips_7000_hilo_fix
1830 && MF_HILO_INSN (prev_prev_insn.insn_opcode)
1831 && insn_uses_reg (ip, ((prev_prev_insn.insn_opcode >> OP_SH_RD)
1832 & OP_MASK_RD),
1833 MIPS_GR_REG))
bdaaa2e1 1834
6b76fefe 1835 {
f9419b05 1836 ++nops;
6b76fefe 1837 }
bdaaa2e1 1838
252b5132
RH
1839 else if (prev_pinfo & INSN_READ_LO)
1840 {
1841 /* The previous instruction reads the LO register; if the
1842 current instruction writes to the LO register, we must
bdaaa2e1
KH
1843 insert two NOPS. Some newer processors have interlocks.
1844 Also the tx39's multiply instructions can be exectuted
252b5132 1845 immediatly after a read from HI/LO (without the delay),
bdaaa2e1
KH
1846 though the tx39's divide insns still do require the
1847 delay. */
252b5132 1848 if (! (hilo_interlocks
ec68c924 1849 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
252b5132
RH
1850 && (mips_optimize == 0
1851 || (pinfo & INSN_WRITE_LO)))
1852 nops += 2;
1853 /* Most mips16 branch insns don't have a delay slot.
1854 If a read from LO is immediately followed by a branch
1855 to a write to LO we have a read followed by a write
1856 less than 2 insns away. We assume the target of
1857 a branch might be a write to LO, and insert a nop
bdaaa2e1 1858 between a read and an immediately following branch. */
252b5132
RH
1859 else if (mips_opts.mips16
1860 && (mips_optimize == 0
1861 || (pinfo & MIPS16_INSN_BRANCH)))
f9419b05 1862 ++nops;
252b5132
RH
1863 }
1864 else if (prev_insn.insn_mo->pinfo & INSN_READ_HI)
1865 {
1866 /* The previous instruction reads the HI register; if the
1867 current instruction writes to the HI register, we must
1868 insert a NOP. Some newer processors have interlocks.
bdaaa2e1 1869 Also the note tx39's multiply above. */
252b5132 1870 if (! (hilo_interlocks
ec68c924 1871 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
252b5132
RH
1872 && (mips_optimize == 0
1873 || (pinfo & INSN_WRITE_HI)))
1874 nops += 2;
1875 /* Most mips16 branch insns don't have a delay slot.
1876 If a read from HI is immediately followed by a branch
1877 to a write to HI we have a read followed by a write
1878 less than 2 insns away. We assume the target of
1879 a branch might be a write to HI, and insert a nop
bdaaa2e1 1880 between a read and an immediately following branch. */
252b5132
RH
1881 else if (mips_opts.mips16
1882 && (mips_optimize == 0
1883 || (pinfo & MIPS16_INSN_BRANCH)))
f9419b05 1884 ++nops;
252b5132
RH
1885 }
1886
1887 /* If the previous instruction was in a noreorder section, then
1888 we don't want to insert the nop after all. */
bdaaa2e1 1889 /* Itbl support may require additional care here. */
252b5132
RH
1890 if (prev_insn_unreordered)
1891 nops = 0;
1892
1893 /* There are two cases which require two intervening
1894 instructions: 1) setting the condition codes using a move to
1895 coprocessor instruction which requires a general coprocessor
1896 delay and then reading the condition codes 2) reading the HI
1897 or LO register and then writing to it (except on processors
1898 which have interlocks). If we are not already emitting a NOP
1899 instruction, we must check for these cases compared to the
1900 instruction previous to the previous instruction. */
1901 if ((! mips_opts.mips16
9ce8a5dd 1902 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
1903 && (prev_prev_insn.insn_mo->pinfo & INSN_COPROC_MOVE_DELAY)
1904 && (prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
1905 && (pinfo & INSN_READ_COND_CODE)
1906 && ! cop_interlocks)
1907 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_LO)
1908 && (pinfo & INSN_WRITE_LO)
1909 && ! (hilo_interlocks
ec68c924 1910 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT))))
252b5132
RH
1911 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
1912 && (pinfo & INSN_WRITE_HI)
1913 && ! (hilo_interlocks
ec68c924 1914 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))))
252b5132
RH
1915 prev_prev_nop = 1;
1916 else
1917 prev_prev_nop = 0;
1918
1919 if (prev_prev_insn_unreordered)
1920 prev_prev_nop = 0;
1921
1922 if (prev_prev_nop && nops == 0)
1923 ++nops;
1924
1925 /* If we are being given a nop instruction, don't bother with
1926 one of the nops we would otherwise output. This will only
1927 happen when a nop instruction is used with mips_optimize set
1928 to 0. */
1929 if (nops > 0
1930 && ! mips_opts.noreorder
156c2f8b 1931 && ip->insn_opcode == (unsigned) (mips_opts.mips16 ? 0x6500 : 0))
252b5132
RH
1932 --nops;
1933
1934 /* Now emit the right number of NOP instructions. */
1935 if (nops > 0 && ! mips_opts.noreorder)
1936 {
1937 fragS *old_frag;
1938 unsigned long old_frag_offset;
1939 int i;
1940 struct insn_label_list *l;
1941
1942 old_frag = frag_now;
1943 old_frag_offset = frag_now_fix ();
1944
1945 for (i = 0; i < nops; i++)
1946 emit_nop ();
1947
1948 if (listing)
1949 {
1950 listing_prev_line ();
1951 /* We may be at the start of a variant frag. In case we
1952 are, make sure there is enough space for the frag
1953 after the frags created by listing_prev_line. The
1954 argument to frag_grow here must be at least as large
1955 as the argument to all other calls to frag_grow in
1956 this file. We don't have to worry about being in the
1957 middle of a variant frag, because the variants insert
1958 all needed nop instructions themselves. */
1959 frag_grow (40);
1960 }
1961
1962 for (l = insn_labels; l != NULL; l = l->next)
1963 {
98aa84af
AM
1964 valueT val;
1965
252b5132 1966 assert (S_GET_SEGMENT (l->label) == now_seg);
49309057 1967 symbol_set_frag (l->label, frag_now);
98aa84af 1968 val = (valueT) frag_now_fix ();
252b5132
RH
1969 /* mips16 text labels are stored as odd. */
1970 if (mips_opts.mips16)
f9419b05 1971 ++val;
98aa84af 1972 S_SET_VALUE (l->label, val);
252b5132
RH
1973 }
1974
1975#ifndef NO_ECOFF_DEBUGGING
1976 if (ECOFF_DEBUGGING)
1977 ecoff_fix_loc (old_frag, old_frag_offset);
1978#endif
1979 }
1980 else if (prev_nop_frag != NULL)
1981 {
1982 /* We have a frag holding nops we may be able to remove. If
1983 we don't need any nops, we can decrease the size of
1984 prev_nop_frag by the size of one instruction. If we do
bdaaa2e1 1985 need some nops, we count them in prev_nops_required. */
252b5132
RH
1986 if (prev_nop_frag_since == 0)
1987 {
1988 if (nops == 0)
1989 {
1990 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1991 --prev_nop_frag_holds;
1992 }
1993 else
1994 prev_nop_frag_required += nops;
1995 }
1996 else
1997 {
1998 if (prev_prev_nop == 0)
1999 {
2000 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
2001 --prev_nop_frag_holds;
2002 }
2003 else
2004 ++prev_nop_frag_required;
2005 }
2006
2007 if (prev_nop_frag_holds <= prev_nop_frag_required)
2008 prev_nop_frag = NULL;
2009
2010 ++prev_nop_frag_since;
2011
2012 /* Sanity check: by the time we reach the second instruction
2013 after prev_nop_frag, we should have used up all the nops
2014 one way or another. */
2015 assert (prev_nop_frag_since <= 1 || prev_nop_frag == NULL);
2016 }
2017 }
2018
f6688943 2019 if (*reloc_type > BFD_RELOC_UNUSED)
252b5132
RH
2020 {
2021 /* We need to set up a variant frag. */
2022 assert (mips_opts.mips16 && address_expr != NULL);
2023 f = frag_var (rs_machine_dependent, 4, 0,
f6688943 2024 RELAX_MIPS16_ENCODE (*reloc_type - BFD_RELOC_UNUSED,
252b5132
RH
2025 mips16_small, mips16_ext,
2026 (prev_pinfo
2027 & INSN_UNCOND_BRANCH_DELAY),
f6688943 2028 (*prev_insn_reloc_type
252b5132 2029 == BFD_RELOC_MIPS16_JMP)),
c4e7957c 2030 make_expr_symbol (address_expr), 0, NULL);
252b5132
RH
2031 }
2032 else if (place != NULL)
2033 f = place;
2034 else if (mips_opts.mips16
2035 && ! ip->use_extend
f6688943 2036 && *reloc_type != BFD_RELOC_MIPS16_JMP)
252b5132
RH
2037 {
2038 /* Make sure there is enough room to swap this instruction with
2039 a following jump instruction. */
2040 frag_grow (6);
2041 f = frag_more (2);
2042 }
2043 else
2044 {
2045 if (mips_opts.mips16
2046 && mips_opts.noreorder
2047 && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
2048 as_warn (_("extended instruction in delay slot"));
2049
2050 f = frag_more (4);
2051 }
2052
f6688943
TS
2053 fixp[0] = fixp[1] = fixp[2] = NULL;
2054 if (address_expr != NULL && *reloc_type < BFD_RELOC_UNUSED)
252b5132
RH
2055 {
2056 if (address_expr->X_op == O_constant)
2057 {
4db1a35d 2058 valueT tmp;
f6688943
TS
2059
2060 switch (*reloc_type)
252b5132
RH
2061 {
2062 case BFD_RELOC_32:
2063 ip->insn_opcode |= address_expr->X_add_number;
2064 break;
2065
f6688943
TS
2066 case BFD_RELOC_MIPS_HIGHEST:
2067 tmp = (address_expr->X_add_number + 0x800080008000) >> 16;
2068 tmp >>= 16;
2069 ip->insn_opcode |= (tmp >> 16) & 0xffff;
2070 break;
2071
2072 case BFD_RELOC_MIPS_HIGHER:
2073 tmp = (address_expr->X_add_number + 0x80008000) >> 16;
2074 ip->insn_opcode |= (tmp >> 16) & 0xffff;
2075 break;
2076
2077 case BFD_RELOC_HI16_S:
2078 ip->insn_opcode |= ((address_expr->X_add_number + 0x8000)
2079 >> 16) & 0xffff;
2080 break;
2081
2082 case BFD_RELOC_HI16:
2083 ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
2084 break;
2085
252b5132
RH
2086 case BFD_RELOC_LO16:
2087 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2088 break;
2089
2090 case BFD_RELOC_MIPS_JMP:
2091 if ((address_expr->X_add_number & 3) != 0)
2092 as_bad (_("jump to misaligned address (0x%lx)"),
2093 (unsigned long) address_expr->X_add_number);
7496292d
TS
2094 if (address_expr->X_add_number & ~0xfffffff
2095 || address_expr->X_add_number > 0x7fffffc)
2096 as_bad (_("jump address range overflow (0x%lx)"),
2097 (unsigned long) address_expr->X_add_number);
252b5132
RH
2098 ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
2099 break;
2100
2101 case BFD_RELOC_MIPS16_JMP:
2102 if ((address_expr->X_add_number & 3) != 0)
2103 as_bad (_("jump to misaligned address (0x%lx)"),
2104 (unsigned long) address_expr->X_add_number);
7496292d
TS
2105 if (address_expr->X_add_number & ~0xfffffff
2106 || address_expr->X_add_number > 0x7fffffc)
2107 as_bad (_("jump address range overflow (0x%lx)"),
2108 (unsigned long) address_expr->X_add_number);
252b5132
RH
2109 ip->insn_opcode |=
2110 (((address_expr->X_add_number & 0x7c0000) << 3)
2111 | ((address_expr->X_add_number & 0xf800000) >> 7)
2112 | ((address_expr->X_add_number & 0x3fffc) >> 2));
2113 break;
2114
cb56d3d3 2115 case BFD_RELOC_16_PCREL:
233b8738 2116 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
cb56d3d3
TS
2117 break;
2118
252b5132
RH
2119 case BFD_RELOC_16_PCREL_S2:
2120 goto need_reloc;
2121
2122 default:
2123 internalError ();
2124 }
2125 }
2126 else
2127 {
2128 need_reloc:
f6688943 2129 /* Don't generate a reloc if we are writing into a variant frag. */
252b5132
RH
2130 if (place == NULL)
2131 {
f6688943
TS
2132 fixp[0] = fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
2133 address_expr,
2134 (*reloc_type == BFD_RELOC_16_PCREL
2135 || *reloc_type == BFD_RELOC_16_PCREL_S2),
2136 reloc_type[0]);
2137
b6ff326e 2138 /* These relocations can have an addend that won't fit in
f6688943
TS
2139 4 octets for 64bit assembly. */
2140 if (HAVE_64BIT_GPRS &&
2141 (*reloc_type == BFD_RELOC_16
98d3f06f
KH
2142 || *reloc_type == BFD_RELOC_32
2143 || *reloc_type == BFD_RELOC_MIPS_JMP
2144 || *reloc_type == BFD_RELOC_HI16_S
2145 || *reloc_type == BFD_RELOC_LO16
2146 || *reloc_type == BFD_RELOC_GPREL16
2147 || *reloc_type == BFD_RELOC_MIPS_LITERAL
2148 || *reloc_type == BFD_RELOC_GPREL32
2149 || *reloc_type == BFD_RELOC_64
2150 || *reloc_type == BFD_RELOC_CTOR
2151 || *reloc_type == BFD_RELOC_MIPS_SUB
2152 || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2153 || *reloc_type == BFD_RELOC_MIPS_HIGHER
2154 || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2155 || *reloc_type == BFD_RELOC_MIPS_REL16
2156 || *reloc_type == BFD_RELOC_MIPS_RELGOT))
f6688943
TS
2157 fixp[0]->fx_no_overflow = 1;
2158
252b5132
RH
2159 if (unmatched_hi)
2160 {
2161 struct mips_hi_fixup *hi_fixup;
2162
f6688943 2163 assert (*reloc_type == BFD_RELOC_HI16_S);
252b5132
RH
2164 hi_fixup = ((struct mips_hi_fixup *)
2165 xmalloc (sizeof (struct mips_hi_fixup)));
f6688943 2166 hi_fixup->fixp = fixp[0];
252b5132
RH
2167 hi_fixup->seg = now_seg;
2168 hi_fixup->next = mips_hi_fixup_list;
2169 mips_hi_fixup_list = hi_fixup;
2170 }
f6688943
TS
2171
2172 if (reloc_type[1] != BFD_RELOC_UNUSED)
2173 {
2174 /* FIXME: This symbol can be one of
2175 RSS_UNDEF, RSS_GP, RSS_GP0, RSS_LOC. */
2176 address_expr->X_op = O_absent;
2177 address_expr->X_add_symbol = 0;
2178 address_expr->X_add_number = 0;
2179
2180 fixp[1] = fix_new_exp (frag_now, f - frag_now->fr_literal,
2181 4, address_expr, false,
2182 reloc_type[1]);
2183
b6ff326e 2184 /* These relocations can have an addend that won't fit in
f6688943
TS
2185 4 octets for 64bit assembly. */
2186 if (HAVE_64BIT_GPRS &&
2187 (*reloc_type == BFD_RELOC_16
2188 || *reloc_type == BFD_RELOC_32
2189 || *reloc_type == BFD_RELOC_MIPS_JMP
2190 || *reloc_type == BFD_RELOC_HI16_S
2191 || *reloc_type == BFD_RELOC_LO16
2192 || *reloc_type == BFD_RELOC_GPREL16
2193 || *reloc_type == BFD_RELOC_MIPS_LITERAL
2194 || *reloc_type == BFD_RELOC_GPREL32
2195 || *reloc_type == BFD_RELOC_64
2196 || *reloc_type == BFD_RELOC_CTOR
2197 || *reloc_type == BFD_RELOC_MIPS_SUB
2198 || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2199 || *reloc_type == BFD_RELOC_MIPS_HIGHER
2200 || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2201 || *reloc_type == BFD_RELOC_MIPS_REL16
2202 || *reloc_type == BFD_RELOC_MIPS_RELGOT))
98d3f06f 2203 fixp[1]->fx_no_overflow = 1;
f6688943
TS
2204
2205 if (reloc_type[2] != BFD_RELOC_UNUSED)
2206 {
2207 address_expr->X_op = O_absent;
2208 address_expr->X_add_symbol = 0;
2209 address_expr->X_add_number = 0;
2210
2211 fixp[2] = fix_new_exp (frag_now,
2212 f - frag_now->fr_literal, 4,
2213 address_expr, false,
2214 reloc_type[2]);
2215
b6ff326e 2216 /* These relocations can have an addend that won't fit in
f6688943
TS
2217 4 octets for 64bit assembly. */
2218 if (HAVE_64BIT_GPRS &&
2219 (*reloc_type == BFD_RELOC_16
2220 || *reloc_type == BFD_RELOC_32
2221 || *reloc_type == BFD_RELOC_MIPS_JMP
2222 || *reloc_type == BFD_RELOC_HI16_S
2223 || *reloc_type == BFD_RELOC_LO16
2224 || *reloc_type == BFD_RELOC_GPREL16
2225 || *reloc_type == BFD_RELOC_MIPS_LITERAL
2226 || *reloc_type == BFD_RELOC_GPREL32
2227 || *reloc_type == BFD_RELOC_64
2228 || *reloc_type == BFD_RELOC_CTOR
2229 || *reloc_type == BFD_RELOC_MIPS_SUB
2230 || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2231 || *reloc_type == BFD_RELOC_MIPS_HIGHER
2232 || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2233 || *reloc_type == BFD_RELOC_MIPS_REL16
2234 || *reloc_type == BFD_RELOC_MIPS_RELGOT))
98d3f06f 2235 fixp[2]->fx_no_overflow = 1;
f6688943
TS
2236 }
2237 }
252b5132
RH
2238 }
2239 }
2240 }
2241
2242 if (! mips_opts.mips16)
2243 md_number_to_chars (f, ip->insn_opcode, 4);
f6688943 2244 else if (*reloc_type == BFD_RELOC_MIPS16_JMP)
252b5132
RH
2245 {
2246 md_number_to_chars (f, ip->insn_opcode >> 16, 2);
2247 md_number_to_chars (f + 2, ip->insn_opcode & 0xffff, 2);
2248 }
2249 else
2250 {
2251 if (ip->use_extend)
2252 {
2253 md_number_to_chars (f, 0xf000 | ip->extend, 2);
2254 f += 2;
2255 }
2256 md_number_to_chars (f, ip->insn_opcode, 2);
2257 }
2258
2259 /* Update the register mask information. */
2260 if (! mips_opts.mips16)
2261 {
2262 if (pinfo & INSN_WRITE_GPR_D)
2263 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
2264 if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2265 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
2266 if (pinfo & INSN_READ_GPR_S)
2267 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
2268 if (pinfo & INSN_WRITE_GPR_31)
f9419b05 2269 mips_gprmask |= 1 << RA;
252b5132
RH
2270 if (pinfo & INSN_WRITE_FPR_D)
2271 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
2272 if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2273 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
2274 if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2275 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
2276 if ((pinfo & INSN_READ_FPR_R) != 0)
2277 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
2278 if (pinfo & INSN_COP)
2279 {
bdaaa2e1
KH
2280 /* We don't keep enough information to sort these cases out.
2281 The itbl support does keep this information however, although
2282 we currently don't support itbl fprmats as part of the cop
2283 instruction. May want to add this support in the future. */
252b5132
RH
2284 }
2285 /* Never set the bit for $0, which is always zero. */
beae10d5 2286 mips_gprmask &= ~1 << 0;
252b5132
RH
2287 }
2288 else
2289 {
2290 if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2291 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RX)
2292 & MIPS16OP_MASK_RX);
2293 if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2294 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RY)
2295 & MIPS16OP_MASK_RY);
2296 if (pinfo & MIPS16_INSN_WRITE_Z)
2297 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RZ)
2298 & MIPS16OP_MASK_RZ);
2299 if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2300 mips_gprmask |= 1 << TREG;
2301 if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2302 mips_gprmask |= 1 << SP;
2303 if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2304 mips_gprmask |= 1 << RA;
2305 if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2306 mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2307 if (pinfo & MIPS16_INSN_READ_Z)
2308 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
2309 & MIPS16OP_MASK_MOVE32Z);
2310 if (pinfo & MIPS16_INSN_READ_GPR_X)
2311 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
2312 & MIPS16OP_MASK_REGR32);
2313 }
2314
2315 if (place == NULL && ! mips_opts.noreorder)
2316 {
2317 /* Filling the branch delay slot is more complex. We try to
2318 switch the branch with the previous instruction, which we can
2319 do if the previous instruction does not set up a condition
2320 that the branch tests and if the branch is not itself the
2321 target of any branch. */
2322 if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2323 || (pinfo & INSN_COND_BRANCH_DELAY))
2324 {
2325 if (mips_optimize < 2
2326 /* If we have seen .set volatile or .set nomove, don't
2327 optimize. */
2328 || mips_opts.nomove != 0
2329 /* If we had to emit any NOP instructions, then we
2330 already know we can not swap. */
2331 || nops != 0
2332 /* If we don't even know the previous insn, we can not
bdaaa2e1 2333 swap. */
252b5132
RH
2334 || ! prev_insn_valid
2335 /* If the previous insn is already in a branch delay
2336 slot, then we can not swap. */
2337 || prev_insn_is_delay_slot
2338 /* If the previous previous insn was in a .set
2339 noreorder, we can't swap. Actually, the MIPS
2340 assembler will swap in this situation. However, gcc
2341 configured -with-gnu-as will generate code like
2342 .set noreorder
2343 lw $4,XXX
2344 .set reorder
2345 INSN
2346 bne $4,$0,foo
2347 in which we can not swap the bne and INSN. If gcc is
2348 not configured -with-gnu-as, it does not output the
2349 .set pseudo-ops. We don't have to check
2350 prev_insn_unreordered, because prev_insn_valid will
2351 be 0 in that case. We don't want to use
2352 prev_prev_insn_valid, because we do want to be able
2353 to swap at the start of a function. */
2354 || prev_prev_insn_unreordered
2355 /* If the branch is itself the target of a branch, we
2356 can not swap. We cheat on this; all we check for is
2357 whether there is a label on this instruction. If
2358 there are any branches to anything other than a
2359 label, users must use .set noreorder. */
2360 || insn_labels != NULL
2361 /* If the previous instruction is in a variant frag, we
2362 can not do the swap. This does not apply to the
2363 mips16, which uses variant frags for different
2364 purposes. */
2365 || (! mips_opts.mips16
2366 && prev_insn_frag->fr_type == rs_machine_dependent)
2367 /* If the branch reads the condition codes, we don't
2368 even try to swap, because in the sequence
2369 ctc1 $X,$31
2370 INSN
2371 INSN
2372 bc1t LABEL
2373 we can not swap, and I don't feel like handling that
2374 case. */
2375 || (! mips_opts.mips16
9ce8a5dd 2376 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
2377 && (pinfo & INSN_READ_COND_CODE))
2378 /* We can not swap with an instruction that requires a
2379 delay slot, becase the target of the branch might
2380 interfere with that instruction. */
2381 || (! mips_opts.mips16
9ce8a5dd 2382 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132 2383 && (prev_pinfo
bdaaa2e1 2384 /* Itbl support may require additional care here. */
252b5132
RH
2385 & (INSN_LOAD_COPROC_DELAY
2386 | INSN_COPROC_MOVE_DELAY
2387 | INSN_WRITE_COND_CODE)))
2388 || (! (hilo_interlocks
ec68c924 2389 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
252b5132
RH
2390 && (prev_pinfo
2391 & (INSN_READ_LO
2392 | INSN_READ_HI)))
2393 || (! mips_opts.mips16
2394 && ! gpr_interlocks
2395 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))
2396 || (! mips_opts.mips16
e7af610e 2397 && mips_opts.isa == ISA_MIPS1
bdaaa2e1 2398 /* Itbl support may require additional care here. */
252b5132
RH
2399 && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))
2400 /* We can not swap with a branch instruction. */
2401 || (prev_pinfo
2402 & (INSN_UNCOND_BRANCH_DELAY
2403 | INSN_COND_BRANCH_DELAY
2404 | INSN_COND_BRANCH_LIKELY))
2405 /* We do not swap with a trap instruction, since it
2406 complicates trap handlers to have the trap
2407 instruction be in a delay slot. */
2408 || (prev_pinfo & INSN_TRAP)
2409 /* If the branch reads a register that the previous
2410 instruction sets, we can not swap. */
2411 || (! mips_opts.mips16
2412 && (prev_pinfo & INSN_WRITE_GPR_T)
2413 && insn_uses_reg (ip,
2414 ((prev_insn.insn_opcode >> OP_SH_RT)
2415 & OP_MASK_RT),
2416 MIPS_GR_REG))
2417 || (! mips_opts.mips16
2418 && (prev_pinfo & INSN_WRITE_GPR_D)
2419 && insn_uses_reg (ip,
2420 ((prev_insn.insn_opcode >> OP_SH_RD)
2421 & OP_MASK_RD),
2422 MIPS_GR_REG))
2423 || (mips_opts.mips16
2424 && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2425 && insn_uses_reg (ip,
2426 ((prev_insn.insn_opcode
2427 >> MIPS16OP_SH_RX)
2428 & MIPS16OP_MASK_RX),
2429 MIPS16_REG))
2430 || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2431 && insn_uses_reg (ip,
2432 ((prev_insn.insn_opcode
2433 >> MIPS16OP_SH_RY)
2434 & MIPS16OP_MASK_RY),
2435 MIPS16_REG))
2436 || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2437 && insn_uses_reg (ip,
2438 ((prev_insn.insn_opcode
2439 >> MIPS16OP_SH_RZ)
2440 & MIPS16OP_MASK_RZ),
2441 MIPS16_REG))
2442 || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2443 && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2444 || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2445 && insn_uses_reg (ip, RA, MIPS_GR_REG))
2446 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2447 && insn_uses_reg (ip,
2448 MIPS16OP_EXTRACT_REG32R (prev_insn.
2449 insn_opcode),
2450 MIPS_GR_REG))))
2451 /* If the branch writes a register that the previous
2452 instruction sets, we can not swap (we know that
2453 branches write only to RD or to $31). */
2454 || (! mips_opts.mips16
2455 && (prev_pinfo & INSN_WRITE_GPR_T)
2456 && (((pinfo & INSN_WRITE_GPR_D)
2457 && (((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT)
2458 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2459 || ((pinfo & INSN_WRITE_GPR_31)
2460 && (((prev_insn.insn_opcode >> OP_SH_RT)
2461 & OP_MASK_RT)
f9419b05 2462 == RA))))
252b5132
RH
2463 || (! mips_opts.mips16
2464 && (prev_pinfo & INSN_WRITE_GPR_D)
2465 && (((pinfo & INSN_WRITE_GPR_D)
2466 && (((prev_insn.insn_opcode >> OP_SH_RD) & OP_MASK_RD)
2467 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2468 || ((pinfo & INSN_WRITE_GPR_31)
2469 && (((prev_insn.insn_opcode >> OP_SH_RD)
2470 & OP_MASK_RD)
f9419b05 2471 == RA))))
252b5132
RH
2472 || (mips_opts.mips16
2473 && (pinfo & MIPS16_INSN_WRITE_31)
2474 && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2475 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2476 && (MIPS16OP_EXTRACT_REG32R (prev_insn.insn_opcode)
2477 == RA))))
2478 /* If the branch writes a register that the previous
2479 instruction reads, we can not swap (we know that
2480 branches only write to RD or to $31). */
2481 || (! mips_opts.mips16
2482 && (pinfo & INSN_WRITE_GPR_D)
2483 && insn_uses_reg (&prev_insn,
2484 ((ip->insn_opcode >> OP_SH_RD)
2485 & OP_MASK_RD),
2486 MIPS_GR_REG))
2487 || (! mips_opts.mips16
2488 && (pinfo & INSN_WRITE_GPR_31)
f9419b05 2489 && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
252b5132
RH
2490 || (mips_opts.mips16
2491 && (pinfo & MIPS16_INSN_WRITE_31)
2492 && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2493 /* If we are generating embedded PIC code, the branch
2494 might be expanded into a sequence which uses $at, so
2495 we can't swap with an instruction which reads it. */
2496 || (mips_pic == EMBEDDED_PIC
2497 && insn_uses_reg (&prev_insn, AT, MIPS_GR_REG))
2498 /* If the previous previous instruction has a load
2499 delay, and sets a register that the branch reads, we
2500 can not swap. */
2501 || (! mips_opts.mips16
9ce8a5dd 2502 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
bdaaa2e1 2503 /* Itbl support may require additional care here. */
252b5132
RH
2504 && ((prev_prev_insn.insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
2505 || (! gpr_interlocks
2506 && (prev_prev_insn.insn_mo->pinfo
2507 & INSN_LOAD_MEMORY_DELAY)))
2508 && insn_uses_reg (ip,
2509 ((prev_prev_insn.insn_opcode >> OP_SH_RT)
2510 & OP_MASK_RT),
2511 MIPS_GR_REG))
2512 /* If one instruction sets a condition code and the
2513 other one uses a condition code, we can not swap. */
2514 || ((pinfo & INSN_READ_COND_CODE)
2515 && (prev_pinfo & INSN_WRITE_COND_CODE))
2516 || ((pinfo & INSN_WRITE_COND_CODE)
2517 && (prev_pinfo & INSN_READ_COND_CODE))
2518 /* If the previous instruction uses the PC, we can not
2519 swap. */
2520 || (mips_opts.mips16
2521 && (prev_pinfo & MIPS16_INSN_READ_PC))
2522 /* If the previous instruction was extended, we can not
2523 swap. */
2524 || (mips_opts.mips16 && prev_insn_extended)
2525 /* If the previous instruction had a fixup in mips16
2526 mode, we can not swap. This normally means that the
2527 previous instruction was a 4 byte branch anyhow. */
f6688943 2528 || (mips_opts.mips16 && prev_insn_fixp[0])
bdaaa2e1
KH
2529 /* If the previous instruction is a sync, sync.l, or
2530 sync.p, we can not swap. */
f173e82e 2531 || (prev_pinfo & INSN_SYNC))
252b5132
RH
2532 {
2533 /* We could do even better for unconditional branches to
2534 portions of this object file; we could pick up the
2535 instruction at the destination, put it in the delay
2536 slot, and bump the destination address. */
2537 emit_nop ();
2538 /* Update the previous insn information. */
2539 prev_prev_insn = *ip;
2540 prev_insn.insn_mo = &dummy_opcode;
2541 }
2542 else
2543 {
2544 /* It looks like we can actually do the swap. */
2545 if (! mips_opts.mips16)
2546 {
2547 char *prev_f;
2548 char temp[4];
2549
2550 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2551 memcpy (temp, prev_f, 4);
2552 memcpy (prev_f, f, 4);
2553 memcpy (f, temp, 4);
f6688943
TS
2554 if (prev_insn_fixp[0])
2555 {
2556 prev_insn_fixp[0]->fx_frag = frag_now;
2557 prev_insn_fixp[0]->fx_where = f - frag_now->fr_literal;
2558 }
2559 if (prev_insn_fixp[1])
2560 {
2561 prev_insn_fixp[1]->fx_frag = frag_now;
2562 prev_insn_fixp[1]->fx_where = f - frag_now->fr_literal;
2563 }
2564 if (prev_insn_fixp[2])
252b5132 2565 {
f6688943
TS
2566 prev_insn_fixp[2]->fx_frag = frag_now;
2567 prev_insn_fixp[2]->fx_where = f - frag_now->fr_literal;
252b5132 2568 }
f6688943 2569 if (fixp[0])
252b5132 2570 {
f6688943
TS
2571 fixp[0]->fx_frag = prev_insn_frag;
2572 fixp[0]->fx_where = prev_insn_where;
2573 }
2574 if (fixp[1])
2575 {
2576 fixp[1]->fx_frag = prev_insn_frag;
2577 fixp[1]->fx_where = prev_insn_where;
2578 }
2579 if (fixp[2])
2580 {
2581 fixp[2]->fx_frag = prev_insn_frag;
2582 fixp[2]->fx_where = prev_insn_where;
252b5132
RH
2583 }
2584 }
2585 else
2586 {
2587 char *prev_f;
2588 char temp[2];
2589
f6688943
TS
2590 assert (prev_insn_fixp[0] == NULL);
2591 assert (prev_insn_fixp[1] == NULL);
2592 assert (prev_insn_fixp[2] == NULL);
252b5132
RH
2593 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2594 memcpy (temp, prev_f, 2);
2595 memcpy (prev_f, f, 2);
f6688943 2596 if (*reloc_type != BFD_RELOC_MIPS16_JMP)
252b5132 2597 {
f6688943 2598 assert (*reloc_type == BFD_RELOC_UNUSED);
252b5132
RH
2599 memcpy (f, temp, 2);
2600 }
2601 else
2602 {
2603 memcpy (f, f + 2, 2);
2604 memcpy (f + 2, temp, 2);
2605 }
f6688943
TS
2606 if (fixp[0])
2607 {
2608 fixp[0]->fx_frag = prev_insn_frag;
2609 fixp[0]->fx_where = prev_insn_where;
2610 }
2611 if (fixp[1])
2612 {
2613 fixp[1]->fx_frag = prev_insn_frag;
2614 fixp[1]->fx_where = prev_insn_where;
2615 }
2616 if (fixp[2])
252b5132 2617 {
f6688943
TS
2618 fixp[2]->fx_frag = prev_insn_frag;
2619 fixp[2]->fx_where = prev_insn_where;
252b5132
RH
2620 }
2621 }
2622
2623 /* Update the previous insn information; leave prev_insn
2624 unchanged. */
2625 prev_prev_insn = *ip;
2626 }
2627 prev_insn_is_delay_slot = 1;
2628
2629 /* If that was an unconditional branch, forget the previous
2630 insn information. */
2631 if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2632 {
2633 prev_prev_insn.insn_mo = &dummy_opcode;
2634 prev_insn.insn_mo = &dummy_opcode;
2635 }
2636
f6688943
TS
2637 prev_insn_fixp[0] = NULL;
2638 prev_insn_fixp[1] = NULL;
2639 prev_insn_fixp[2] = NULL;
2640 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2641 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2642 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
252b5132
RH
2643 prev_insn_extended = 0;
2644 }
2645 else if (pinfo & INSN_COND_BRANCH_LIKELY)
2646 {
2647 /* We don't yet optimize a branch likely. What we should do
2648 is look at the target, copy the instruction found there
2649 into the delay slot, and increment the branch to jump to
2650 the next instruction. */
2651 emit_nop ();
2652 /* Update the previous insn information. */
2653 prev_prev_insn = *ip;
2654 prev_insn.insn_mo = &dummy_opcode;
f6688943
TS
2655 prev_insn_fixp[0] = NULL;
2656 prev_insn_fixp[1] = NULL;
2657 prev_insn_fixp[2] = NULL;
2658 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2659 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2660 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
252b5132
RH
2661 prev_insn_extended = 0;
2662 }
2663 else
2664 {
2665 /* Update the previous insn information. */
2666 if (nops > 0)
2667 prev_prev_insn.insn_mo = &dummy_opcode;
2668 else
2669 prev_prev_insn = prev_insn;
2670 prev_insn = *ip;
2671
2672 /* Any time we see a branch, we always fill the delay slot
2673 immediately; since this insn is not a branch, we know it
2674 is not in a delay slot. */
2675 prev_insn_is_delay_slot = 0;
2676
f6688943
TS
2677 prev_insn_fixp[0] = fixp[0];
2678 prev_insn_fixp[1] = fixp[1];
2679 prev_insn_fixp[2] = fixp[2];
2680 prev_insn_reloc_type[0] = reloc_type[0];
2681 prev_insn_reloc_type[1] = reloc_type[1];
2682 prev_insn_reloc_type[2] = reloc_type[2];
252b5132
RH
2683 if (mips_opts.mips16)
2684 prev_insn_extended = (ip->use_extend
f6688943 2685 || *reloc_type > BFD_RELOC_UNUSED);
252b5132
RH
2686 }
2687
2688 prev_prev_insn_unreordered = prev_insn_unreordered;
2689 prev_insn_unreordered = 0;
2690 prev_insn_frag = frag_now;
2691 prev_insn_where = f - frag_now->fr_literal;
2692 prev_insn_valid = 1;
2693 }
2694 else if (place == NULL)
2695 {
2696 /* We need to record a bit of information even when we are not
2697 reordering, in order to determine the base address for mips16
2698 PC relative relocs. */
2699 prev_prev_insn = prev_insn;
2700 prev_insn = *ip;
f6688943
TS
2701 prev_insn_reloc_type[0] = reloc_type[0];
2702 prev_insn_reloc_type[1] = reloc_type[1];
2703 prev_insn_reloc_type[2] = reloc_type[2];
252b5132
RH
2704 prev_prev_insn_unreordered = prev_insn_unreordered;
2705 prev_insn_unreordered = 1;
2706 }
2707
2708 /* We just output an insn, so the next one doesn't have a label. */
2709 mips_clear_insn_labels ();
2710
2711 /* We must ensure that a fixup associated with an unmatched %hi
2712 reloc does not become a variant frag. Otherwise, the
2713 rearrangement of %hi relocs in frob_file may confuse
2714 tc_gen_reloc. */
2715 if (unmatched_hi)
2716 {
2717 frag_wane (frag_now);
2718 frag_new (0);
2719 }
2720}
2721
2722/* This function forgets that there was any previous instruction or
2723 label. If PRESERVE is non-zero, it remembers enough information to
bdaaa2e1 2724 know whether nops are needed before a noreorder section. */
252b5132
RH
2725
2726static void
2727mips_no_prev_insn (preserve)
2728 int preserve;
2729{
2730 if (! preserve)
2731 {
2732 prev_insn.insn_mo = &dummy_opcode;
2733 prev_prev_insn.insn_mo = &dummy_opcode;
2734 prev_nop_frag = NULL;
2735 prev_nop_frag_holds = 0;
2736 prev_nop_frag_required = 0;
2737 prev_nop_frag_since = 0;
2738 }
2739 prev_insn_valid = 0;
2740 prev_insn_is_delay_slot = 0;
2741 prev_insn_unreordered = 0;
2742 prev_insn_extended = 0;
f6688943
TS
2743 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2744 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2745 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
252b5132
RH
2746 prev_prev_insn_unreordered = 0;
2747 mips_clear_insn_labels ();
2748}
2749
2750/* This function must be called whenever we turn on noreorder or emit
2751 something other than instructions. It inserts any NOPS which might
2752 be needed by the previous instruction, and clears the information
2753 kept for the previous instructions. The INSNS parameter is true if
bdaaa2e1 2754 instructions are to follow. */
252b5132
RH
2755
2756static void
2757mips_emit_delays (insns)
2758 boolean insns;
2759{
2760 if (! mips_opts.noreorder)
2761 {
2762 int nops;
2763
2764 nops = 0;
2765 if ((! mips_opts.mips16
9ce8a5dd 2766 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
2767 && (! cop_interlocks
2768 && (prev_insn.insn_mo->pinfo
2769 & (INSN_LOAD_COPROC_DELAY
2770 | INSN_COPROC_MOVE_DELAY
2771 | INSN_WRITE_COND_CODE))))
2772 || (! hilo_interlocks
2773 && (prev_insn.insn_mo->pinfo
2774 & (INSN_READ_LO
2775 | INSN_READ_HI)))
2776 || (! mips_opts.mips16
2777 && ! gpr_interlocks
bdaaa2e1 2778 && (prev_insn.insn_mo->pinfo
252b5132
RH
2779 & INSN_LOAD_MEMORY_DELAY))
2780 || (! mips_opts.mips16
e7af610e 2781 && mips_opts.isa == ISA_MIPS1
252b5132
RH
2782 && (prev_insn.insn_mo->pinfo
2783 & INSN_COPROC_MEMORY_DELAY)))
2784 {
beae10d5 2785 /* Itbl support may require additional care here. */
252b5132
RH
2786 ++nops;
2787 if ((! mips_opts.mips16
9ce8a5dd 2788 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
2789 && (! cop_interlocks
2790 && prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2791 || (! hilo_interlocks
2792 && ((prev_insn.insn_mo->pinfo & INSN_READ_HI)
2793 || (prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2794 ++nops;
2795
2796 if (prev_insn_unreordered)
2797 nops = 0;
2798 }
2799 else if ((! mips_opts.mips16
9ce8a5dd 2800 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
2801 && (! cop_interlocks
2802 && prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2803 || (! hilo_interlocks
2804 && ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
2805 || (prev_prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2806 {
beae10d5 2807 /* Itbl support may require additional care here. */
252b5132
RH
2808 if (! prev_prev_insn_unreordered)
2809 ++nops;
2810 }
2811
2812 if (nops > 0)
2813 {
2814 struct insn_label_list *l;
2815
2816 if (insns)
2817 {
2818 /* Record the frag which holds the nop instructions, so
2819 that we can remove them if we don't need them. */
2820 frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2821 prev_nop_frag = frag_now;
2822 prev_nop_frag_holds = nops;
2823 prev_nop_frag_required = 0;
2824 prev_nop_frag_since = 0;
2825 }
2826
2827 for (; nops > 0; --nops)
2828 emit_nop ();
2829
2830 if (insns)
2831 {
2832 /* Move on to a new frag, so that it is safe to simply
bdaaa2e1 2833 decrease the size of prev_nop_frag. */
252b5132
RH
2834 frag_wane (frag_now);
2835 frag_new (0);
2836 }
2837
2838 for (l = insn_labels; l != NULL; l = l->next)
2839 {
98aa84af
AM
2840 valueT val;
2841
252b5132 2842 assert (S_GET_SEGMENT (l->label) == now_seg);
49309057 2843 symbol_set_frag (l->label, frag_now);
98aa84af 2844 val = (valueT) frag_now_fix ();
252b5132
RH
2845 /* mips16 text labels are stored as odd. */
2846 if (mips_opts.mips16)
f9419b05 2847 ++val;
98aa84af 2848 S_SET_VALUE (l->label, val);
252b5132
RH
2849 }
2850 }
2851 }
2852
2853 /* Mark instruction labels in mips16 mode. */
f9419b05 2854 if (insns)
252b5132
RH
2855 mips16_mark_labels ();
2856
2857 mips_no_prev_insn (insns);
2858}
2859
2860/* Build an instruction created by a macro expansion. This is passed
2861 a pointer to the count of instructions created so far, an
2862 expression, the name of the instruction to build, an operand format
2863 string, and corresponding arguments. */
2864
2865#ifdef USE_STDARG
2866static void
2867macro_build (char *place,
2868 int *counter,
2869 expressionS * ep,
2870 const char *name,
2871 const char *fmt,
2872 ...)
2873#else
2874static void
2875macro_build (place, counter, ep, name, fmt, va_alist)
2876 char *place;
2877 int *counter;
2878 expressionS *ep;
2879 const char *name;
2880 const char *fmt;
2881 va_dcl
2882#endif
2883{
2884 struct mips_cl_insn insn;
f6688943 2885 bfd_reloc_code_real_type r[3];
252b5132 2886 va_list args;
252b5132
RH
2887
2888#ifdef USE_STDARG
2889 va_start (args, fmt);
2890#else
2891 va_start (args);
2892#endif
2893
2894 /*
2895 * If the macro is about to expand into a second instruction,
2896 * print a warning if needed. We need to pass ip as a parameter
2897 * to generate a better warning message here...
2898 */
2899 if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
2900 as_warn (_("Macro instruction expanded into multiple instructions"));
2901
80cc45a5
EC
2902 /*
2903 * If the macro is about to expand into a second instruction,
2904 * and it is in a delay slot, print a warning.
2905 */
2906 if (place == NULL
2907 && *counter == 1
2908 && mips_opts.noreorder
2909 && (prev_prev_insn.insn_mo->pinfo
2910 & (INSN_UNCOND_BRANCH_DELAY | INSN_COND_BRANCH_DELAY
2b2e39bf 2911 | INSN_COND_BRANCH_LIKELY)) != 0)
80cc45a5
EC
2912 as_warn (_("Macro instruction expanded into multiple instructions in a branch delay slot"));
2913
252b5132 2914 if (place == NULL)
f9419b05 2915 ++*counter; /* bump instruction counter */
252b5132
RH
2916
2917 if (mips_opts.mips16)
2918 {
2919 mips16_macro_build (place, counter, ep, name, fmt, args);
2920 va_end (args);
2921 return;
2922 }
2923
f6688943
TS
2924 r[0] = BFD_RELOC_UNUSED;
2925 r[1] = BFD_RELOC_UNUSED;
2926 r[2] = BFD_RELOC_UNUSED;
252b5132
RH
2927 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
2928 assert (insn.insn_mo);
2929 assert (strcmp (name, insn.insn_mo->name) == 0);
2930
2931 /* Search until we get a match for NAME. */
2932 while (1)
2933 {
deec1734
CD
2934 /* It is assumed here that macros will never generate
2935 MDMX or MIPS-3D instructions. */
252b5132
RH
2936 if (strcmp (fmt, insn.insn_mo->args) == 0
2937 && insn.insn_mo->pinfo != INSN_MACRO
af55c2e6 2938 && OPCODE_IS_MEMBER (insn.insn_mo, mips_opts.isa, mips_arch)
ec68c924 2939 && (mips_arch != CPU_R4650 || (insn.insn_mo->pinfo & FP_D) == 0))
252b5132
RH
2940 break;
2941
2942 ++insn.insn_mo;
2943 assert (insn.insn_mo->name);
2944 assert (strcmp (name, insn.insn_mo->name) == 0);
2945 }
2946
2947 insn.insn_opcode = insn.insn_mo->match;
2948 for (;;)
2949 {
2950 switch (*fmt++)
2951 {
2952 case '\0':
2953 break;
2954
2955 case ',':
2956 case '(':
2957 case ')':
2958 continue;
2959
2960 case 't':
2961 case 'w':
2962 case 'E':
38487616 2963 insn.insn_opcode |= va_arg (args, int) << OP_SH_RT;
252b5132
RH
2964 continue;
2965
2966 case 'c':
38487616
TS
2967 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE;
2968 continue;
2969
252b5132
RH
2970 case 'T':
2971 case 'W':
38487616 2972 insn.insn_opcode |= va_arg (args, int) << OP_SH_FT;
252b5132
RH
2973 continue;
2974
2975 case 'd':
2976 case 'G':
38487616 2977 insn.insn_opcode |= va_arg (args, int) << OP_SH_RD;
252b5132
RH
2978 continue;
2979
4372b673
NC
2980 case 'U':
2981 {
2982 int tmp = va_arg (args, int);
2983
38487616
TS
2984 insn.insn_opcode |= tmp << OP_SH_RT;
2985 insn.insn_opcode |= tmp << OP_SH_RD;
beae10d5 2986 continue;
4372b673
NC
2987 }
2988
252b5132
RH
2989 case 'V':
2990 case 'S':
38487616 2991 insn.insn_opcode |= va_arg (args, int) << OP_SH_FS;
252b5132
RH
2992 continue;
2993
2994 case 'z':
2995 continue;
2996
2997 case '<':
38487616 2998 insn.insn_opcode |= va_arg (args, int) << OP_SH_SHAMT;
252b5132
RH
2999 continue;
3000
3001 case 'D':
38487616 3002 insn.insn_opcode |= va_arg (args, int) << OP_SH_FD;
252b5132
RH
3003 continue;
3004
3005 case 'B':
38487616 3006 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE20;
252b5132
RH
3007 continue;
3008
4372b673 3009 case 'J':
38487616 3010 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE19;
4372b673
NC
3011 continue;
3012
252b5132 3013 case 'q':
38487616 3014 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE2;
252b5132
RH
3015 continue;
3016
3017 case 'b':
3018 case 's':
3019 case 'r':
3020 case 'v':
38487616 3021 insn.insn_opcode |= va_arg (args, int) << OP_SH_RS;
252b5132
RH
3022 continue;
3023
3024 case 'i':
3025 case 'j':
3026 case 'o':
f6688943 3027 *r = (bfd_reloc_code_real_type) va_arg (args, int);
cdf6fd85 3028 assert (*r == BFD_RELOC_GPREL16
f6688943
TS
3029 || *r == BFD_RELOC_MIPS_LITERAL
3030 || *r == BFD_RELOC_MIPS_HIGHER
3031 || *r == BFD_RELOC_HI16_S
3032 || *r == BFD_RELOC_LO16
3033 || *r == BFD_RELOC_MIPS_GOT16
3034 || *r == BFD_RELOC_MIPS_CALL16
438c16b8
TS
3035 || *r == BFD_RELOC_MIPS_GOT_DISP
3036 || *r == BFD_RELOC_MIPS_GOT_PAGE
3037 || *r == BFD_RELOC_MIPS_GOT_OFST
f6688943
TS
3038 || *r == BFD_RELOC_MIPS_GOT_LO16
3039 || *r == BFD_RELOC_MIPS_CALL_LO16
252b5132 3040 || (ep->X_op == O_subtract
f6688943 3041 && *r == BFD_RELOC_PCREL_LO16));
252b5132
RH
3042 continue;
3043
3044 case 'u':
f6688943 3045 *r = (bfd_reloc_code_real_type) va_arg (args, int);
252b5132
RH
3046 assert (ep != NULL
3047 && (ep->X_op == O_constant
3048 || (ep->X_op == O_symbol
f6688943
TS
3049 && (*r == BFD_RELOC_MIPS_HIGHEST
3050 || *r == BFD_RELOC_HI16_S
3051 || *r == BFD_RELOC_HI16
3052 || *r == BFD_RELOC_GPREL16
3053 || *r == BFD_RELOC_MIPS_GOT_HI16
3054 || *r == BFD_RELOC_MIPS_CALL_HI16))
252b5132 3055 || (ep->X_op == O_subtract
f6688943 3056 && *r == BFD_RELOC_PCREL_HI16_S)));
252b5132
RH
3057 continue;
3058
3059 case 'p':
3060 assert (ep != NULL);
3061 /*
3062 * This allows macro() to pass an immediate expression for
3063 * creating short branches without creating a symbol.
3064 * Note that the expression still might come from the assembly
3065 * input, in which case the value is not checked for range nor
3066 * is a relocation entry generated (yuck).
3067 */
3068 if (ep->X_op == O_constant)
3069 {
3070 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
3071 ep = NULL;
3072 }
3073 else
cb56d3d3 3074 if (mips_pic == EMBEDDED_PIC)
f6688943 3075 *r = BFD_RELOC_16_PCREL_S2;
cb56d3d3 3076 else
f6688943 3077 *r = BFD_RELOC_16_PCREL;
252b5132
RH
3078 continue;
3079
3080 case 'a':
3081 assert (ep != NULL);
f6688943 3082 *r = BFD_RELOC_MIPS_JMP;
252b5132
RH
3083 continue;
3084
3085 case 'C':
3086 insn.insn_opcode |= va_arg (args, unsigned long);
3087 continue;
3088
3089 default:
3090 internalError ();
3091 }
3092 break;
3093 }
3094 va_end (args);
f6688943 3095 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132
RH
3096
3097 append_insn (place, &insn, ep, r, false);
3098}
3099
3100static void
3101mips16_macro_build (place, counter, ep, name, fmt, args)
3102 char *place;
43841e91 3103 int *counter ATTRIBUTE_UNUSED;
252b5132
RH
3104 expressionS *ep;
3105 const char *name;
3106 const char *fmt;
3107 va_list args;
3108{
3109 struct mips_cl_insn insn;
f6688943
TS
3110 bfd_reloc_code_real_type r[3]
3111 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 3112
252b5132
RH
3113 insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
3114 assert (insn.insn_mo);
3115 assert (strcmp (name, insn.insn_mo->name) == 0);
3116
3117 while (strcmp (fmt, insn.insn_mo->args) != 0
3118 || insn.insn_mo->pinfo == INSN_MACRO)
3119 {
3120 ++insn.insn_mo;
3121 assert (insn.insn_mo->name);
3122 assert (strcmp (name, insn.insn_mo->name) == 0);
3123 }
3124
3125 insn.insn_opcode = insn.insn_mo->match;
3126 insn.use_extend = false;
3127
3128 for (;;)
3129 {
3130 int c;
3131
3132 c = *fmt++;
3133 switch (c)
3134 {
3135 case '\0':
3136 break;
3137
3138 case ',':
3139 case '(':
3140 case ')':
3141 continue;
3142
3143 case 'y':
3144 case 'w':
3145 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
3146 continue;
3147
3148 case 'x':
3149 case 'v':
3150 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
3151 continue;
3152
3153 case 'z':
3154 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
3155 continue;
3156
3157 case 'Z':
3158 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
3159 continue;
3160
3161 case '0':
3162 case 'S':
3163 case 'P':
3164 case 'R':
3165 continue;
3166
3167 case 'X':
3168 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
3169 continue;
3170
3171 case 'Y':
3172 {
3173 int regno;
3174
3175 regno = va_arg (args, int);
3176 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
3177 insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
3178 }
3179 continue;
3180
3181 case '<':
3182 case '>':
3183 case '4':
3184 case '5':
3185 case 'H':
3186 case 'W':
3187 case 'D':
3188 case 'j':
3189 case '8':
3190 case 'V':
3191 case 'C':
3192 case 'U':
3193 case 'k':
3194 case 'K':
3195 case 'p':
3196 case 'q':
3197 {
3198 assert (ep != NULL);
3199
3200 if (ep->X_op != O_constant)
874e8986 3201 *r = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
3202 else
3203 {
c4e7957c
TS
3204 mips16_immed (NULL, 0, c, ep->X_add_number, false, false,
3205 false, &insn.insn_opcode, &insn.use_extend,
3206 &insn.extend);
252b5132 3207 ep = NULL;
f6688943 3208 *r = BFD_RELOC_UNUSED;
252b5132
RH
3209 }
3210 }
3211 continue;
3212
3213 case '6':
3214 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
3215 continue;
3216 }
3217
3218 break;
3219 }
3220
f6688943 3221 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132
RH
3222
3223 append_insn (place, &insn, ep, r, false);
3224}
3225
438c16b8
TS
3226/*
3227 * Generate a "jalr" instruction with a relocation hint to the called
3228 * function. This occurs in NewABI PIC code.
3229 */
3230static void
3231macro_build_jalr (icnt, ep)
3232 int icnt;
3233 expressionS *ep;
3234{
3235 if (HAVE_NEWABI)
3236 frag_more (0);
3237 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr", "d,s",
3238 RA, PIC_CALL_REG);
3239 if (HAVE_NEWABI)
3240 fix_new_exp (frag_now, 0, 0, ep, false, BFD_RELOC_MIPS_JALR);
3241}
3242
252b5132
RH
3243/*
3244 * Generate a "lui" instruction.
3245 */
3246static void
3247macro_build_lui (place, counter, ep, regnum)
3248 char *place;
3249 int *counter;
3250 expressionS *ep;
3251 int regnum;
3252{
3253 expressionS high_expr;
3254 struct mips_cl_insn insn;
f6688943
TS
3255 bfd_reloc_code_real_type r[3]
3256 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132
RH
3257 CONST char *name = "lui";
3258 CONST char *fmt = "t,u";
3259
3260 assert (! mips_opts.mips16);
3261
3262 if (place == NULL)
3263 high_expr = *ep;
3264 else
3265 {
3266 high_expr.X_op = O_constant;
3267 high_expr.X_add_number = ep->X_add_number;
3268 }
3269
3270 if (high_expr.X_op == O_constant)
3271 {
3272 /* we can compute the instruction now without a relocation entry */
e7d556df
TS
3273 high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3274 >> 16) & 0xffff;
f6688943 3275 *r = BFD_RELOC_UNUSED;
252b5132 3276 }
f6688943 3277 else if (! HAVE_NEWABI)
252b5132
RH
3278 {
3279 assert (ep->X_op == O_symbol);
3280 /* _gp_disp is a special case, used from s_cpload. */
3281 assert (mips_pic == NO_PIC
3282 || strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0);
f6688943 3283 *r = BFD_RELOC_HI16_S;
252b5132
RH
3284 }
3285
3286 /*
3287 * If the macro is about to expand into a second instruction,
3288 * print a warning if needed. We need to pass ip as a parameter
3289 * to generate a better warning message here...
3290 */
3291 if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
3292 as_warn (_("Macro instruction expanded into multiple instructions"));
3293
3294 if (place == NULL)
f9419b05 3295 ++*counter; /* bump instruction counter */
252b5132
RH
3296
3297 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3298 assert (insn.insn_mo);
3299 assert (strcmp (name, insn.insn_mo->name) == 0);
3300 assert (strcmp (fmt, insn.insn_mo->args) == 0);
3301
3302 insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
f6688943 3303 if (*r == BFD_RELOC_UNUSED)
252b5132
RH
3304 {
3305 insn.insn_opcode |= high_expr.X_add_number;
3306 append_insn (place, &insn, NULL, r, false);
3307 }
3308 else
3309 append_insn (place, &insn, &high_expr, r, false);
3310}
3311
3312/* set_at()
3313 * Generates code to set the $at register to true (one)
3314 * if reg is less than the immediate expression.
3315 */
3316static void
3317set_at (counter, reg, unsignedp)
3318 int *counter;
3319 int reg;
3320 int unsignedp;
3321{
3322 if (imm_expr.X_op == O_constant
3323 && imm_expr.X_add_number >= -0x8000
3324 && imm_expr.X_add_number < 0x8000)
3325 macro_build ((char *) NULL, counter, &imm_expr,
3326 unsignedp ? "sltiu" : "slti",
3327 "t,r,j", AT, reg, (int) BFD_RELOC_LO16);
3328 else
3329 {
4d34fb5f 3330 load_register (counter, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9 3331 macro_build ((char *) NULL, counter, (expressionS *) NULL,
252b5132
RH
3332 unsignedp ? "sltu" : "slt",
3333 "d,v,t", AT, reg, AT);
3334 }
3335}
3336
3337/* Warn if an expression is not a constant. */
3338
3339static void
3340check_absolute_expr (ip, ex)
3341 struct mips_cl_insn *ip;
3342 expressionS *ex;
3343{
3344 if (ex->X_op == O_big)
3345 as_bad (_("unsupported large constant"));
3346 else if (ex->X_op != O_constant)
3347 as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
3348}
3349
3350/* Count the leading zeroes by performing a binary chop. This is a
3351 bulky bit of source, but performance is a LOT better for the
3352 majority of values than a simple loop to count the bits:
3353 for (lcnt = 0; (lcnt < 32); lcnt++)
3354 if ((v) & (1 << (31 - lcnt)))
3355 break;
3356 However it is not code size friendly, and the gain will drop a bit
3357 on certain cached systems.
3358*/
3359#define COUNT_TOP_ZEROES(v) \
3360 (((v) & ~0xffff) == 0 \
3361 ? ((v) & ~0xff) == 0 \
3362 ? ((v) & ~0xf) == 0 \
3363 ? ((v) & ~0x3) == 0 \
3364 ? ((v) & ~0x1) == 0 \
3365 ? !(v) \
3366 ? 32 \
3367 : 31 \
3368 : 30 \
3369 : ((v) & ~0x7) == 0 \
3370 ? 29 \
3371 : 28 \
3372 : ((v) & ~0x3f) == 0 \
3373 ? ((v) & ~0x1f) == 0 \
3374 ? 27 \
3375 : 26 \
3376 : ((v) & ~0x7f) == 0 \
3377 ? 25 \
3378 : 24 \
3379 : ((v) & ~0xfff) == 0 \
3380 ? ((v) & ~0x3ff) == 0 \
3381 ? ((v) & ~0x1ff) == 0 \
3382 ? 23 \
3383 : 22 \
3384 : ((v) & ~0x7ff) == 0 \
3385 ? 21 \
3386 : 20 \
3387 : ((v) & ~0x3fff) == 0 \
3388 ? ((v) & ~0x1fff) == 0 \
3389 ? 19 \
3390 : 18 \
3391 : ((v) & ~0x7fff) == 0 \
3392 ? 17 \
3393 : 16 \
3394 : ((v) & ~0xffffff) == 0 \
3395 ? ((v) & ~0xfffff) == 0 \
3396 ? ((v) & ~0x3ffff) == 0 \
3397 ? ((v) & ~0x1ffff) == 0 \
3398 ? 15 \
3399 : 14 \
3400 : ((v) & ~0x7ffff) == 0 \
3401 ? 13 \
3402 : 12 \
3403 : ((v) & ~0x3fffff) == 0 \
3404 ? ((v) & ~0x1fffff) == 0 \
3405 ? 11 \
3406 : 10 \
3407 : ((v) & ~0x7fffff) == 0 \
3408 ? 9 \
3409 : 8 \
3410 : ((v) & ~0xfffffff) == 0 \
3411 ? ((v) & ~0x3ffffff) == 0 \
3412 ? ((v) & ~0x1ffffff) == 0 \
3413 ? 7 \
3414 : 6 \
3415 : ((v) & ~0x7ffffff) == 0 \
3416 ? 5 \
3417 : 4 \
3418 : ((v) & ~0x3fffffff) == 0 \
3419 ? ((v) & ~0x1fffffff) == 0 \
3420 ? 3 \
3421 : 2 \
3422 : ((v) & ~0x7fffffff) == 0 \
3423 ? 1 \
3424 : 0)
3425
6373ee54
CD
3426/* Is the given value a sign-extended 32-bit value? */
3427#define IS_SEXT_32BIT_NUM(x) \
3428 (((x) &~ (offsetT) 0x7fffffff) == 0 \
3429 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
3430
252b5132
RH
3431/* load_register()
3432 * This routine generates the least number of instructions neccessary to load
3433 * an absolute expression value into a register.
3434 */
3435static void
3436load_register (counter, reg, ep, dbl)
3437 int *counter;
3438 int reg;
3439 expressionS *ep;
3440 int dbl;
3441{
3442 int freg;
3443 expressionS hi32, lo32;
3444
3445 if (ep->X_op != O_big)
3446 {
3447 assert (ep->X_op == O_constant);
3448 if (ep->X_add_number < 0x8000
3449 && (ep->X_add_number >= 0
3450 || (ep->X_add_number >= -0x8000
3451 && (! dbl
3452 || ! ep->X_unsigned
3453 || sizeof (ep->X_add_number) > 4))))
3454 {
3455 /* We can handle 16 bit signed values with an addiu to
3456 $zero. No need to ever use daddiu here, since $zero and
3457 the result are always correct in 32 bit mode. */
3458 macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3459 (int) BFD_RELOC_LO16);
3460 return;
3461 }
3462 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3463 {
3464 /* We can handle 16 bit unsigned values with an ori to
3465 $zero. */
3466 macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, 0,
3467 (int) BFD_RELOC_LO16);
3468 return;
3469 }
6373ee54 3470 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)
252b5132
RH
3471 && (! dbl
3472 || ! ep->X_unsigned
3473 || sizeof (ep->X_add_number) > 4
3474 || (ep->X_add_number & 0x80000000) == 0))
ca4e0257 3475 || ((HAVE_32BIT_GPRS || ! dbl)
252b5132 3476 && (ep->X_add_number &~ (offsetT) 0xffffffff) == 0)
ca4e0257 3477 || (HAVE_32BIT_GPRS
252b5132
RH
3478 && ! dbl
3479 && ((ep->X_add_number &~ (offsetT) 0xffffffff)
3480 == ~ (offsetT) 0xffffffff)))
3481 {
3482 /* 32 bit values require an lui. */
3483 macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3484 (int) BFD_RELOC_HI16);
3485 if ((ep->X_add_number & 0xffff) != 0)
3486 macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, reg,
3487 (int) BFD_RELOC_LO16);
3488 return;
3489 }
3490 }
3491
3492 /* The value is larger than 32 bits. */
3493
ca4e0257 3494 if (HAVE_32BIT_GPRS)
252b5132 3495 {
956cd1d6
TS
3496 as_bad (_("Number (0x%lx) larger than 32 bits"),
3497 (unsigned long) ep->X_add_number);
252b5132
RH
3498 macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3499 (int) BFD_RELOC_LO16);
3500 return;
3501 }
3502
3503 if (ep->X_op != O_big)
3504 {
3505 hi32 = *ep;
3506 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3507 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3508 hi32.X_add_number &= 0xffffffff;
3509 lo32 = *ep;
3510 lo32.X_add_number &= 0xffffffff;
3511 }
3512 else
3513 {
3514 assert (ep->X_add_number > 2);
3515 if (ep->X_add_number == 3)
3516 generic_bignum[3] = 0;
3517 else if (ep->X_add_number > 4)
3518 as_bad (_("Number larger than 64 bits"));
3519 lo32.X_op = O_constant;
3520 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3521 hi32.X_op = O_constant;
3522 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3523 }
3524
3525 if (hi32.X_add_number == 0)
3526 freg = 0;
3527 else
3528 {
3529 int shift, bit;
3530 unsigned long hi, lo;
3531
956cd1d6 3532 if (hi32.X_add_number == (offsetT) 0xffffffff)
beae10d5
KH
3533 {
3534 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3535 {
3536 macro_build ((char *) NULL, counter, &lo32, "addiu", "t,r,j",
252b5132 3537 reg, 0, (int) BFD_RELOC_LO16);
beae10d5
KH
3538 return;
3539 }
3540 if (lo32.X_add_number & 0x80000000)
3541 {
3542 macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3543 (int) BFD_RELOC_HI16);
252b5132
RH
3544 if (lo32.X_add_number & 0xffff)
3545 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i",
3546 reg, reg, (int) BFD_RELOC_LO16);
beae10d5
KH
3547 return;
3548 }
3549 }
252b5132
RH
3550
3551 /* Check for 16bit shifted constant. We know that hi32 is
3552 non-zero, so start the mask on the first bit of the hi32
3553 value. */
3554 shift = 17;
3555 do
beae10d5
KH
3556 {
3557 unsigned long himask, lomask;
3558
3559 if (shift < 32)
3560 {
3561 himask = 0xffff >> (32 - shift);
3562 lomask = (0xffff << shift) & 0xffffffff;
3563 }
3564 else
3565 {
3566 himask = 0xffff << (shift - 32);
3567 lomask = 0;
3568 }
3569 if ((hi32.X_add_number & ~(offsetT) himask) == 0
3570 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3571 {
3572 expressionS tmp;
3573
3574 tmp.X_op = O_constant;
3575 if (shift < 32)
3576 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3577 | (lo32.X_add_number >> shift));
3578 else
3579 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
3580 macro_build ((char *) NULL, counter, &tmp,
3581 "ori", "t,r,i", reg, 0,
3582 (int) BFD_RELOC_LO16);
2396cfb9 3583 macro_build ((char *) NULL, counter, (expressionS *) NULL,
beae10d5
KH
3584 (shift >= 32) ? "dsll32" : "dsll",
3585 "d,w,<", reg, reg,
3586 (shift >= 32) ? shift - 32 : shift);
3587 return;
3588 }
f9419b05 3589 ++shift;
beae10d5
KH
3590 }
3591 while (shift <= (64 - 16));
252b5132
RH
3592
3593 /* Find the bit number of the lowest one bit, and store the
3594 shifted value in hi/lo. */
3595 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3596 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3597 if (lo != 0)
3598 {
3599 bit = 0;
3600 while ((lo & 1) == 0)
3601 {
3602 lo >>= 1;
3603 ++bit;
3604 }
3605 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3606 hi >>= bit;
3607 }
3608 else
3609 {
3610 bit = 32;
3611 while ((hi & 1) == 0)
3612 {
3613 hi >>= 1;
3614 ++bit;
3615 }
3616 lo = hi;
3617 hi = 0;
3618 }
3619
3620 /* Optimize if the shifted value is a (power of 2) - 1. */
3621 if ((hi == 0 && ((lo + 1) & lo) == 0)
3622 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
beae10d5
KH
3623 {
3624 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
252b5132 3625 if (shift != 0)
beae10d5 3626 {
252b5132
RH
3627 expressionS tmp;
3628
3629 /* This instruction will set the register to be all
3630 ones. */
beae10d5
KH
3631 tmp.X_op = O_constant;
3632 tmp.X_add_number = (offsetT) -1;
3633 macro_build ((char *) NULL, counter, &tmp, "addiu", "t,r,j",
252b5132 3634 reg, 0, (int) BFD_RELOC_LO16);
beae10d5
KH
3635 if (bit != 0)
3636 {
3637 bit += shift;
2396cfb9 3638 macro_build ((char *) NULL, counter, (expressionS *) NULL,
beae10d5
KH
3639 (bit >= 32) ? "dsll32" : "dsll",
3640 "d,w,<", reg, reg,
3641 (bit >= 32) ? bit - 32 : bit);
3642 }
2396cfb9 3643 macro_build ((char *) NULL, counter, (expressionS *) NULL,
252b5132 3644 (shift >= 32) ? "dsrl32" : "dsrl",
beae10d5 3645 "d,w,<", reg, reg,
252b5132 3646 (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
3647 return;
3648 }
3649 }
252b5132
RH
3650
3651 /* Sign extend hi32 before calling load_register, because we can
3652 generally get better code when we load a sign extended value. */
3653 if ((hi32.X_add_number & 0x80000000) != 0)
beae10d5 3654 hi32.X_add_number |= ~(offsetT) 0xffffffff;
252b5132
RH
3655 load_register (counter, reg, &hi32, 0);
3656 freg = reg;
3657 }
3658 if ((lo32.X_add_number & 0xffff0000) == 0)
3659 {
3660 if (freg != 0)
3661 {
2396cfb9
TS
3662 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3663 "dsll32", "d,w,<", reg, freg, 0);
252b5132
RH
3664 freg = reg;
3665 }
3666 }
3667 else
3668 {
3669 expressionS mid16;
3670
956cd1d6 3671 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
beae10d5 3672 {
252b5132
RH
3673 macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3674 (int) BFD_RELOC_HI16);
956cd1d6
TS
3675 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3676 "dsrl32", "d,w,<", reg, reg, 0);
beae10d5
KH
3677 return;
3678 }
252b5132
RH
3679
3680 if (freg != 0)
3681 {
956cd1d6
TS
3682 macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3683 "d,w,<", reg, freg, 16);
252b5132
RH
3684 freg = reg;
3685 }
3686 mid16 = lo32;
3687 mid16.X_add_number >>= 16;
3688 macro_build ((char *) NULL, counter, &mid16, "ori", "t,r,i", reg,
3689 freg, (int) BFD_RELOC_LO16);
956cd1d6
TS
3690 macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3691 "d,w,<", reg, reg, 16);
252b5132
RH
3692 freg = reg;
3693 }
3694 if ((lo32.X_add_number & 0xffff) != 0)
3695 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i", reg, freg,
3696 (int) BFD_RELOC_LO16);
3697}
3698
3699/* Load an address into a register. */
3700
3701static void
c9914766 3702load_address (counter, reg, ep, used_at)
252b5132
RH
3703 int *counter;
3704 int reg;
3705 expressionS *ep;
d6bc6245 3706 int *used_at;
252b5132 3707{
f9419b05 3708 char *p = NULL;
252b5132
RH
3709
3710 if (ep->X_op != O_constant
3711 && ep->X_op != O_symbol)
3712 {
3713 as_bad (_("expression too complex"));
3714 ep->X_op = O_constant;
3715 }
3716
3717 if (ep->X_op == O_constant)
3718 {
c9914766 3719 load_register (counter, reg, ep, HAVE_64BIT_ADDRESSES);
252b5132
RH
3720 return;
3721 }
3722
3723 if (mips_pic == NO_PIC)
3724 {
3725 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 3726 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
3727 Otherwise we want
3728 lui $reg,<sym> (BFD_RELOC_HI16_S)
3729 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
d6bc6245 3730 If we have an addend, we always use the latter form.
76b3015f 3731
d6bc6245
TS
3732 With 64bit address space and a usable $at we want
3733 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3734 lui $at,<sym> (BFD_RELOC_HI16_S)
3735 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3736 daddiu $at,<sym> (BFD_RELOC_LO16)
3737 dsll32 $reg,0
3738 dadd $reg,$reg,$at
76b3015f 3739
d6bc6245
TS
3740 If $at is already in use, we use an path which is suboptimal
3741 on superscalar processors.
3742 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3743 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3744 dsll $reg,16
3745 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
3746 dsll $reg,16
3747 daddiu $reg,<sym> (BFD_RELOC_LO16)
3748 */
c9914766 3749 if (HAVE_64BIT_ADDRESSES)
d6bc6245 3750 {
d6bc6245
TS
3751 /* We don't do GP optimization for now because RELAX_ENCODE can't
3752 hold the data for such large chunks. */
3753
3754 if (*used_at == 0)
3755 {
3756 macro_build (p, counter, ep, "lui", "t,u",
3757 reg, (int) BFD_RELOC_MIPS_HIGHEST);
3758 macro_build (p, counter, ep, "lui", "t,u",
3759 AT, (int) BFD_RELOC_HI16_S);
3760 macro_build (p, counter, ep, "daddiu", "t,r,j",
3761 reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3762 macro_build (p, counter, ep, "daddiu", "t,r,j",
3763 AT, AT, (int) BFD_RELOC_LO16);
2396cfb9
TS
3764 macro_build (p, counter, (expressionS *) NULL, "dsll32",
3765 "d,w,<", reg, reg, 0);
3766 macro_build (p, counter, (expressionS *) NULL, "dadd",
3767 "d,v,t", reg, reg, AT);
d6bc6245
TS
3768 *used_at = 1;
3769 }
3770 else
3771 {
3772 macro_build (p, counter, ep, "lui", "t,u",
3773 reg, (int) BFD_RELOC_MIPS_HIGHEST);
3774 macro_build (p, counter, ep, "daddiu", "t,r,j",
3775 reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
2396cfb9
TS
3776 macro_build (p, counter, (expressionS *) NULL, "dsll",
3777 "d,w,<", reg, reg, 16);
d6bc6245
TS
3778 macro_build (p, counter, ep, "daddiu", "t,r,j",
3779 reg, reg, (int) BFD_RELOC_HI16_S);
2396cfb9
TS
3780 macro_build (p, counter, (expressionS *) NULL, "dsll",
3781 "d,w,<", reg, reg, 16);
d6bc6245
TS
3782 macro_build (p, counter, ep, "daddiu", "t,r,j",
3783 reg, reg, (int) BFD_RELOC_LO16);
3784 }
3785 }
252b5132
RH
3786 else
3787 {
d6bc6245
TS
3788 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3789 && ! nopic_need_relax (ep->X_add_symbol, 1))
3790 {
3791 frag_grow (20);
3792 macro_build ((char *) NULL, counter, ep,
c9914766
TS
3793 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
3794 reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
d6bc6245
TS
3795 p = frag_var (rs_machine_dependent, 8, 0,
3796 RELAX_ENCODE (4, 8, 0, 4, 0,
3797 mips_opts.warn_about_macros),
956cd1d6 3798 ep->X_add_symbol, 0, NULL);
d6bc6245
TS
3799 }
3800 macro_build_lui (p, counter, ep, reg);
3801 if (p != NULL)
3802 p += 4;
c9914766
TS
3803 macro_build (p, counter, ep,
3804 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
d6bc6245
TS
3805 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3806 }
252b5132
RH
3807 }
3808 else if (mips_pic == SVR4_PIC && ! mips_big_got)
3809 {
3810 expressionS ex;
3811
3812 /* If this is a reference to an external symbol, we want
3813 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3814 Otherwise we want
3815 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3816 nop
3817 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3818 If there is a constant, it must be added in after. */
3819 ex.X_add_number = ep->X_add_number;
3820 ep->X_add_number = 0;
3821 frag_grow (20);
3822 macro_build ((char *) NULL, counter, ep,
c9914766
TS
3823 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
3824 reg, (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132
RH
3825 macro_build ((char *) NULL, counter, (expressionS *) NULL, "nop", "");
3826 p = frag_var (rs_machine_dependent, 4, 0,
3827 RELAX_ENCODE (0, 4, -8, 0, 0, mips_opts.warn_about_macros),
3828 ep->X_add_symbol, (offsetT) 0, (char *) NULL);
3829 macro_build (p, counter, ep,
ca4e0257 3830 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
3831 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3832 if (ex.X_add_number != 0)
3833 {
3834 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3835 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3836 ex.X_op = O_constant;
3837 macro_build ((char *) NULL, counter, &ex,
ca4e0257 3838 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
3839 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3840 }
3841 }
3842 else if (mips_pic == SVR4_PIC)
3843 {
3844 expressionS ex;
3845 int off;
3846
3847 /* This is the large GOT case. If this is a reference to an
3848 external symbol, we want
3849 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
3850 addu $reg,$reg,$gp
3851 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
3852 Otherwise, for a reference to a local symbol, we want
3853 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3854 nop
3855 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
438c16b8
TS
3856 If we have NewABI, we want
3857 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
3858 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
252b5132
RH
3859 If there is a constant, it must be added in after. */
3860 ex.X_add_number = ep->X_add_number;
3861 ep->X_add_number = 0;
438c16b8
TS
3862 if (HAVE_NEWABI)
3863 {
3864 macro_build ((char *) NULL, counter, ep,
3865 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3866 (int) BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
3867 macro_build (p, counter, ep,
3868 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
3869 reg, reg, (int) BFD_RELOC_MIPS_GOT_OFST);
3870 }
252b5132 3871 else
438c16b8
TS
3872 {
3873 if (reg_needs_delay (mips_gp_register))
3874 off = 4;
3875 else
3876 off = 0;
3877 frag_grow (32);
3878 macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3879 (int) BFD_RELOC_MIPS_GOT_HI16);
3880 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3881 HAVE_32BIT_ADDRESSES ? "addu" : "daddu", "d,v,t", reg,
3882 reg, mips_gp_register);
3883 macro_build ((char *) NULL, counter, ep,
3884 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
3885 "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT_LO16, reg);
3886 p = frag_var (rs_machine_dependent, 12 + off, 0,
3887 RELAX_ENCODE (12, 12 + off, off, 8 + off, 0,
3888 mips_opts.warn_about_macros),
3889 ep->X_add_symbol, 0, NULL);
3890 if (off > 0)
3891 {
3892 /* We need a nop before loading from $gp. This special
3893 check is required because the lui which starts the main
3894 instruction stream does not refer to $gp, and so will not
3895 insert the nop which may be required. */
3896 macro_build (p, counter, (expressionS *) NULL, "nop", "");
3897 p += 4;
3898 }
3899 macro_build (p, counter, ep,
3900 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3901 (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
3902 p += 4;
252b5132
RH
3903 macro_build (p, counter, (expressionS *) NULL, "nop", "");
3904 p += 4;
438c16b8
TS
3905 macro_build (p, counter, ep,
3906 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3907 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
252b5132 3908 }
438c16b8 3909
252b5132
RH
3910 if (ex.X_add_number != 0)
3911 {
3912 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3913 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3914 ex.X_op = O_constant;
f7ea7ef2
TS
3915 macro_build ((char *) NULL, counter, &ex,
3916 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
3917 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3918 }
3919 }
3920 else if (mips_pic == EMBEDDED_PIC)
3921 {
3922 /* We always do
cdf6fd85 3923 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
c9914766
TS
3924 */
3925 macro_build ((char *) NULL, counter, ep,
3926 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3927 "t,r,j", reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
252b5132
RH
3928 }
3929 else
3930 abort ();
3931}
3932
ea1fb5dc
RS
3933/* Move the contents of register SOURCE into register DEST. */
3934
3935static void
3936move_register (counter, dest, source)
3937 int *counter;
3938 int dest;
3939 int source;
3940{
3941 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3942 HAVE_32BIT_GPRS ? "addu" : "daddu",
3943 "d,v,t", dest, source, 0);
3944}
3945
252b5132
RH
3946/*
3947 * Build macros
3948 * This routine implements the seemingly endless macro or synthesized
3949 * instructions and addressing modes in the mips assembly language. Many
3950 * of these macros are simple and are similar to each other. These could
3951 * probably be handled by some kind of table or grammer aproach instead of
3952 * this verbose method. Others are not simple macros but are more like
3953 * optimizing code generation.
3954 * One interesting optimization is when several store macros appear
3955 * consecutivly that would load AT with the upper half of the same address.
3956 * The ensuing load upper instructions are ommited. This implies some kind
3957 * of global optimization. We currently only optimize within a single macro.
3958 * For many of the load and store macros if the address is specified as a
3959 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
3960 * first load register 'at' with zero and use it as the base register. The
3961 * mips assembler simply uses register $zero. Just one tiny optimization
3962 * we're missing.
3963 */
3964static void
3965macro (ip)
3966 struct mips_cl_insn *ip;
3967{
3968 register int treg, sreg, dreg, breg;
3969 int tempreg;
3970 int mask;
3971 int icnt = 0;
43841e91 3972 int used_at = 0;
252b5132
RH
3973 expressionS expr1;
3974 const char *s;
3975 const char *s2;
3976 const char *fmt;
3977 int likely = 0;
3978 int dbl = 0;
3979 int coproc = 0;
3980 int lr = 0;
3981 int imm = 0;
3982 offsetT maxnum;
3983 int off;
3984 bfd_reloc_code_real_type r;
252b5132
RH
3985 int hold_mips_optimize;
3986
3987 assert (! mips_opts.mips16);
3988
3989 treg = (ip->insn_opcode >> 16) & 0x1f;
3990 dreg = (ip->insn_opcode >> 11) & 0x1f;
3991 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
3992 mask = ip->insn_mo->mask;
3993
3994 expr1.X_op = O_constant;
3995 expr1.X_op_symbol = NULL;
3996 expr1.X_add_symbol = NULL;
3997 expr1.X_add_number = 1;
3998
3999 switch (mask)
4000 {
4001 case M_DABS:
4002 dbl = 1;
4003 case M_ABS:
4004 /* bgez $a0,.+12
4005 move v0,$a0
4006 sub v0,$zero,$a0
4007 */
4008
4009 mips_emit_delays (true);
4010 ++mips_opts.noreorder;
4011 mips_any_noreorder = 1;
4012
4013 expr1.X_add_number = 8;
4014 macro_build ((char *) NULL, &icnt, &expr1, "bgez", "s,p", sreg);
4015 if (dreg == sreg)
2396cfb9
TS
4016 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
4017 0);
252b5132 4018 else
ea1fb5dc 4019 move_register (&icnt, dreg, sreg);
2396cfb9 4020 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 4021 dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
252b5132
RH
4022
4023 --mips_opts.noreorder;
4024 return;
4025
4026 case M_ADD_I:
4027 s = "addi";
4028 s2 = "add";
4029 goto do_addi;
4030 case M_ADDU_I:
4031 s = "addiu";
4032 s2 = "addu";
4033 goto do_addi;
4034 case M_DADD_I:
4035 dbl = 1;
4036 s = "daddi";
4037 s2 = "dadd";
4038 goto do_addi;
4039 case M_DADDU_I:
4040 dbl = 1;
4041 s = "daddiu";
4042 s2 = "daddu";
4043 do_addi:
4044 if (imm_expr.X_op == O_constant
4045 && imm_expr.X_add_number >= -0x8000
4046 && imm_expr.X_add_number < 0x8000)
4047 {
4048 macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,j", treg, sreg,
4049 (int) BFD_RELOC_LO16);
4050 return;
4051 }
4052 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9
TS
4053 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
4054 treg, sreg, AT);
252b5132
RH
4055 break;
4056
4057 case M_AND_I:
4058 s = "andi";
4059 s2 = "and";
4060 goto do_bit;
4061 case M_OR_I:
4062 s = "ori";
4063 s2 = "or";
4064 goto do_bit;
4065 case M_NOR_I:
4066 s = "";
4067 s2 = "nor";
4068 goto do_bit;
4069 case M_XOR_I:
4070 s = "xori";
4071 s2 = "xor";
4072 do_bit:
4073 if (imm_expr.X_op == O_constant
4074 && imm_expr.X_add_number >= 0
4075 && imm_expr.X_add_number < 0x10000)
4076 {
4077 if (mask != M_NOR_I)
4078 macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,i", treg,
4079 sreg, (int) BFD_RELOC_LO16);
4080 else
4081 {
4082 macro_build ((char *) NULL, &icnt, &imm_expr, "ori", "t,r,i",
4083 treg, sreg, (int) BFD_RELOC_LO16);
2396cfb9
TS
4084 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nor",
4085 "d,v,t", treg, treg, 0);
252b5132
RH
4086 }
4087 return;
4088 }
4089
d6bc6245 4090 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
4091 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
4092 treg, sreg, AT);
252b5132
RH
4093 break;
4094
4095 case M_BEQ_I:
4096 s = "beq";
4097 goto beq_i;
4098 case M_BEQL_I:
4099 s = "beql";
4100 likely = 1;
4101 goto beq_i;
4102 case M_BNE_I:
4103 s = "bne";
4104 goto beq_i;
4105 case M_BNEL_I:
4106 s = "bnel";
4107 likely = 1;
4108 beq_i:
4109 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4110 {
4111 macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg,
4112 0);
4113 return;
4114 }
4d34fb5f 4115 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
252b5132
RH
4116 macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg, AT);
4117 break;
4118
4119 case M_BGEL:
4120 likely = 1;
4121 case M_BGE:
4122 if (treg == 0)
4123 {
4124 macro_build ((char *) NULL, &icnt, &offset_expr,
2396cfb9 4125 likely ? "bgezl" : "bgez", "s,p", sreg);
252b5132
RH
4126 return;
4127 }
4128 if (sreg == 0)
4129 {
4130 macro_build ((char *) NULL, &icnt, &offset_expr,
2396cfb9 4131 likely ? "blezl" : "blez", "s,p", treg);
252b5132
RH
4132 return;
4133 }
2396cfb9
TS
4134 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4135 AT, sreg, treg);
252b5132 4136 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4137 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4138 break;
4139
4140 case M_BGTL_I:
4141 likely = 1;
4142 case M_BGT_I:
4143 /* check for > max integer */
4144 maxnum = 0x7fffffff;
ca4e0257 4145 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
4146 {
4147 maxnum <<= 16;
4148 maxnum |= 0xffff;
4149 maxnum <<= 16;
4150 maxnum |= 0xffff;
4151 }
4152 if (imm_expr.X_op == O_constant
4153 && imm_expr.X_add_number >= maxnum
ca4e0257 4154 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
4155 {
4156 do_false:
4157 /* result is always false */
4158 if (! likely)
4159 {
39c0a331
L
4160 if (warn_nops)
4161 as_warn (_("Branch %s is always false (nop)"),
4162 ip->insn_mo->name);
2396cfb9
TS
4163 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop",
4164 "", 0);
252b5132
RH
4165 }
4166 else
4167 {
39c0a331
L
4168 if (warn_nops)
4169 as_warn (_("Branch likely %s is always false"),
4170 ip->insn_mo->name);
252b5132
RH
4171 macro_build ((char *) NULL, &icnt, &offset_expr, "bnel",
4172 "s,t,p", 0, 0);
4173 }
4174 return;
4175 }
4176 if (imm_expr.X_op != O_constant)
4177 as_bad (_("Unsupported large constant"));
f9419b05 4178 ++imm_expr.X_add_number;
252b5132
RH
4179 /* FALLTHROUGH */
4180 case M_BGE_I:
4181 case M_BGEL_I:
4182 if (mask == M_BGEL_I)
4183 likely = 1;
4184 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4185 {
4186 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4187 likely ? "bgezl" : "bgez", "s,p", sreg);
252b5132
RH
4188 return;
4189 }
4190 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4191 {
4192 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4193 likely ? "bgtzl" : "bgtz", "s,p", sreg);
252b5132
RH
4194 return;
4195 }
4196 maxnum = 0x7fffffff;
ca4e0257 4197 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
4198 {
4199 maxnum <<= 16;
4200 maxnum |= 0xffff;
4201 maxnum <<= 16;
4202 maxnum |= 0xffff;
4203 }
4204 maxnum = - maxnum - 1;
4205 if (imm_expr.X_op == O_constant
4206 && imm_expr.X_add_number <= maxnum
ca4e0257 4207 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
4208 {
4209 do_true:
4210 /* result is always true */
4211 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
4212 macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
4213 return;
4214 }
4215 set_at (&icnt, sreg, 0);
4216 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4217 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4218 break;
4219
4220 case M_BGEUL:
4221 likely = 1;
4222 case M_BGEU:
4223 if (treg == 0)
4224 goto do_true;
4225 if (sreg == 0)
4226 {
4227 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4228 likely ? "beql" : "beq", "s,t,p", 0, treg);
252b5132
RH
4229 return;
4230 }
2396cfb9
TS
4231 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4232 "d,v,t", AT, sreg, treg);
252b5132 4233 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4234 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4235 break;
4236
4237 case M_BGTUL_I:
4238 likely = 1;
4239 case M_BGTU_I:
4240 if (sreg == 0
ca4e0257 4241 || (HAVE_32BIT_GPRS
252b5132 4242 && imm_expr.X_op == O_constant
956cd1d6 4243 && imm_expr.X_add_number == (offsetT) 0xffffffff))
252b5132
RH
4244 goto do_false;
4245 if (imm_expr.X_op != O_constant)
4246 as_bad (_("Unsupported large constant"));
f9419b05 4247 ++imm_expr.X_add_number;
252b5132
RH
4248 /* FALLTHROUGH */
4249 case M_BGEU_I:
4250 case M_BGEUL_I:
4251 if (mask == M_BGEUL_I)
4252 likely = 1;
4253 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4254 goto do_true;
4255 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4256 {
4257 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4258 likely ? "bnel" : "bne", "s,t,p", sreg, 0);
252b5132
RH
4259 return;
4260 }
4261 set_at (&icnt, sreg, 1);
4262 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4263 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4264 break;
4265
4266 case M_BGTL:
4267 likely = 1;
4268 case M_BGT:
4269 if (treg == 0)
4270 {
4271 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4272 likely ? "bgtzl" : "bgtz", "s,p", sreg);
252b5132
RH
4273 return;
4274 }
4275 if (sreg == 0)
4276 {
4277 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4278 likely ? "bltzl" : "bltz", "s,p", treg);
252b5132
RH
4279 return;
4280 }
2396cfb9
TS
4281 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4282 AT, treg, sreg);
252b5132 4283 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4284 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4285 break;
4286
4287 case M_BGTUL:
4288 likely = 1;
4289 case M_BGTU:
4290 if (treg == 0)
4291 {
4292 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4293 likely ? "bnel" : "bne", "s,t,p", sreg, 0);
252b5132
RH
4294 return;
4295 }
4296 if (sreg == 0)
4297 goto do_false;
2396cfb9
TS
4298 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4299 "d,v,t", AT, treg, sreg);
252b5132 4300 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4301 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4302 break;
4303
4304 case M_BLEL:
4305 likely = 1;
4306 case M_BLE:
4307 if (treg == 0)
4308 {
4309 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4310 likely ? "blezl" : "blez", "s,p", sreg);
252b5132
RH
4311 return;
4312 }
4313 if (sreg == 0)
4314 {
4315 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4316 likely ? "bgezl" : "bgez", "s,p", treg);
252b5132
RH
4317 return;
4318 }
2396cfb9
TS
4319 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4320 AT, treg, sreg);
252b5132 4321 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4322 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4323 break;
4324
4325 case M_BLEL_I:
4326 likely = 1;
4327 case M_BLE_I:
4328 maxnum = 0x7fffffff;
ca4e0257 4329 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
4330 {
4331 maxnum <<= 16;
4332 maxnum |= 0xffff;
4333 maxnum <<= 16;
4334 maxnum |= 0xffff;
4335 }
4336 if (imm_expr.X_op == O_constant
4337 && imm_expr.X_add_number >= maxnum
ca4e0257 4338 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
4339 goto do_true;
4340 if (imm_expr.X_op != O_constant)
4341 as_bad (_("Unsupported large constant"));
f9419b05 4342 ++imm_expr.X_add_number;
252b5132
RH
4343 /* FALLTHROUGH */
4344 case M_BLT_I:
4345 case M_BLTL_I:
4346 if (mask == M_BLTL_I)
4347 likely = 1;
4348 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4349 {
4350 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4351 likely ? "bltzl" : "bltz", "s,p", sreg);
252b5132
RH
4352 return;
4353 }
4354 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4355 {
4356 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4357 likely ? "blezl" : "blez", "s,p", sreg);
252b5132
RH
4358 return;
4359 }
4360 set_at (&icnt, sreg, 0);
4361 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4362 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4363 break;
4364
4365 case M_BLEUL:
4366 likely = 1;
4367 case M_BLEU:
4368 if (treg == 0)
4369 {
4370 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4371 likely ? "beql" : "beq", "s,t,p", sreg, 0);
252b5132
RH
4372 return;
4373 }
4374 if (sreg == 0)
4375 goto do_true;
2396cfb9
TS
4376 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4377 "d,v,t", AT, treg, sreg);
252b5132 4378 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4379 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4380 break;
4381
4382 case M_BLEUL_I:
4383 likely = 1;
4384 case M_BLEU_I:
4385 if (sreg == 0
ca4e0257 4386 || (HAVE_32BIT_GPRS
252b5132 4387 && imm_expr.X_op == O_constant
956cd1d6 4388 && imm_expr.X_add_number == (offsetT) 0xffffffff))
252b5132
RH
4389 goto do_true;
4390 if (imm_expr.X_op != O_constant)
4391 as_bad (_("Unsupported large constant"));
f9419b05 4392 ++imm_expr.X_add_number;
252b5132
RH
4393 /* FALLTHROUGH */
4394 case M_BLTU_I:
4395 case M_BLTUL_I:
4396 if (mask == M_BLTUL_I)
4397 likely = 1;
4398 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4399 goto do_false;
4400 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4401 {
4402 macro_build ((char *) NULL, &icnt, &offset_expr,
4403 likely ? "beql" : "beq",
4404 "s,t,p", sreg, 0);
4405 return;
4406 }
4407 set_at (&icnt, sreg, 1);
4408 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4409 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4410 break;
4411
4412 case M_BLTL:
4413 likely = 1;
4414 case M_BLT:
4415 if (treg == 0)
4416 {
4417 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4418 likely ? "bltzl" : "bltz", "s,p", sreg);
252b5132
RH
4419 return;
4420 }
4421 if (sreg == 0)
4422 {
4423 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4424 likely ? "bgtzl" : "bgtz", "s,p", treg);
252b5132
RH
4425 return;
4426 }
2396cfb9
TS
4427 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4428 AT, sreg, treg);
252b5132 4429 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4430 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4431 break;
4432
4433 case M_BLTUL:
4434 likely = 1;
4435 case M_BLTU:
4436 if (treg == 0)
4437 goto do_false;
4438 if (sreg == 0)
4439 {
4440 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4441 likely ? "bnel" : "bne", "s,t,p", 0, treg);
252b5132
RH
4442 return;
4443 }
2396cfb9
TS
4444 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4445 "d,v,t", AT, sreg,
252b5132
RH
4446 treg);
4447 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4448 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4449 break;
4450
4451 case M_DDIV_3:
4452 dbl = 1;
4453 case M_DIV_3:
4454 s = "mflo";
4455 goto do_div3;
4456 case M_DREM_3:
4457 dbl = 1;
4458 case M_REM_3:
4459 s = "mfhi";
4460 do_div3:
4461 if (treg == 0)
4462 {
4463 as_warn (_("Divide by zero."));
4464 if (mips_trap)
2396cfb9
TS
4465 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4466 "s,t", 0, 0);
252b5132 4467 else
2396cfb9
TS
4468 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4469 "c", 7);
252b5132
RH
4470 return;
4471 }
4472
4473 mips_emit_delays (true);
4474 ++mips_opts.noreorder;
4475 mips_any_noreorder = 1;
4476 if (mips_trap)
4477 {
2396cfb9
TS
4478 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4479 "s,t", treg, 0);
4480 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 4481 dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
252b5132
RH
4482 }
4483 else
4484 {
4485 expr1.X_add_number = 8;
4486 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
2396cfb9 4487 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 4488 dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
2396cfb9
TS
4489 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4490 "c", 7);
252b5132
RH
4491 }
4492 expr1.X_add_number = -1;
4493 macro_build ((char *) NULL, &icnt, &expr1,
4494 dbl ? "daddiu" : "addiu",
4495 "t,r,j", AT, 0, (int) BFD_RELOC_LO16);
4496 expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
4497 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, AT);
4498 if (dbl)
4499 {
4500 expr1.X_add_number = 1;
4501 macro_build ((char *) NULL, &icnt, &expr1, "daddiu", "t,r,j", AT, 0,
4502 (int) BFD_RELOC_LO16);
2396cfb9
TS
4503 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsll32",
4504 "d,w,<", AT, AT, 31);
252b5132
RH
4505 }
4506 else
4507 {
4508 expr1.X_add_number = 0x80000000;
4509 macro_build ((char *) NULL, &icnt, &expr1, "lui", "t,u", AT,
4510 (int) BFD_RELOC_HI16);
4511 }
4512 if (mips_trap)
4513 {
2396cfb9
TS
4514 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4515 "s,t", sreg, AT);
252b5132
RH
4516 /* We want to close the noreorder block as soon as possible, so
4517 that later insns are available for delay slot filling. */
4518 --mips_opts.noreorder;
4519 }
4520 else
4521 {
4522 expr1.X_add_number = 8;
4523 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", sreg, AT);
2396cfb9
TS
4524 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
4525 0);
252b5132
RH
4526
4527 /* We want to close the noreorder block as soon as possible, so
4528 that later insns are available for delay slot filling. */
4529 --mips_opts.noreorder;
4530
2396cfb9
TS
4531 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4532 "c", 6);
252b5132 4533 }
2396cfb9 4534 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d", dreg);
252b5132
RH
4535 break;
4536
4537 case M_DIV_3I:
4538 s = "div";
4539 s2 = "mflo";
4540 goto do_divi;
4541 case M_DIVU_3I:
4542 s = "divu";
4543 s2 = "mflo";
4544 goto do_divi;
4545 case M_REM_3I:
4546 s = "div";
4547 s2 = "mfhi";
4548 goto do_divi;
4549 case M_REMU_3I:
4550 s = "divu";
4551 s2 = "mfhi";
4552 goto do_divi;
4553 case M_DDIV_3I:
4554 dbl = 1;
4555 s = "ddiv";
4556 s2 = "mflo";
4557 goto do_divi;
4558 case M_DDIVU_3I:
4559 dbl = 1;
4560 s = "ddivu";
4561 s2 = "mflo";
4562 goto do_divi;
4563 case M_DREM_3I:
4564 dbl = 1;
4565 s = "ddiv";
4566 s2 = "mfhi";
4567 goto do_divi;
4568 case M_DREMU_3I:
4569 dbl = 1;
4570 s = "ddivu";
4571 s2 = "mfhi";
4572 do_divi:
4573 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4574 {
4575 as_warn (_("Divide by zero."));
4576 if (mips_trap)
2396cfb9
TS
4577 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4578 "s,t", 0, 0);
252b5132 4579 else
2396cfb9
TS
4580 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4581 "c", 7);
252b5132
RH
4582 return;
4583 }
4584 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4585 {
4586 if (strcmp (s2, "mflo") == 0)
ea1fb5dc 4587 move_register (&icnt, dreg, sreg);
252b5132 4588 else
ea1fb5dc 4589 move_register (&icnt, dreg, 0);
252b5132
RH
4590 return;
4591 }
4592 if (imm_expr.X_op == O_constant
4593 && imm_expr.X_add_number == -1
4594 && s[strlen (s) - 1] != 'u')
4595 {
4596 if (strcmp (s2, "mflo") == 0)
4597 {
2396cfb9
TS
4598 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4599 dbl ? "dneg" : "neg", "d,w", dreg, sreg);
252b5132
RH
4600 }
4601 else
ea1fb5dc 4602 move_register (&icnt, dreg, 0);
252b5132
RH
4603 return;
4604 }
4605
4606 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9
TS
4607 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4608 sreg, AT);
4609 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
252b5132
RH
4610 break;
4611
4612 case M_DIVU_3:
4613 s = "divu";
4614 s2 = "mflo";
4615 goto do_divu3;
4616 case M_REMU_3:
4617 s = "divu";
4618 s2 = "mfhi";
4619 goto do_divu3;
4620 case M_DDIVU_3:
4621 s = "ddivu";
4622 s2 = "mflo";
4623 goto do_divu3;
4624 case M_DREMU_3:
4625 s = "ddivu";
4626 s2 = "mfhi";
4627 do_divu3:
4628 mips_emit_delays (true);
4629 ++mips_opts.noreorder;
4630 mips_any_noreorder = 1;
4631 if (mips_trap)
4632 {
2396cfb9
TS
4633 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4634 "s,t", treg, 0);
4635 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4636 sreg, treg);
252b5132
RH
4637 /* We want to close the noreorder block as soon as possible, so
4638 that later insns are available for delay slot filling. */
4639 --mips_opts.noreorder;
4640 }
4641 else
4642 {
4643 expr1.X_add_number = 8;
4644 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
2396cfb9
TS
4645 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4646 sreg, treg);
252b5132
RH
4647
4648 /* We want to close the noreorder block as soon as possible, so
4649 that later insns are available for delay slot filling. */
4650 --mips_opts.noreorder;
2396cfb9
TS
4651 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4652 "c", 7);
252b5132 4653 }
2396cfb9 4654 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
252b5132
RH
4655 return;
4656
4657 case M_DLA_AB:
4658 dbl = 1;
4659 case M_LA_AB:
4660 /* Load the address of a symbol into a register. If breg is not
4661 zero, we then add a base register to it. */
4662
3bec30a8
TS
4663 if (dbl && HAVE_32BIT_GPRS)
4664 as_warn (_("dla used to load 32-bit register"));
4665
c90bbe5b 4666 if (! dbl && HAVE_64BIT_OBJECTS)
3bec30a8
TS
4667 as_warn (_("la used to load 64-bit address"));
4668
afdbd6d0
CD
4669 if (treg == breg)
4670 {
4671 tempreg = AT;
4672 used_at = 1;
4673 }
4674 else
4675 {
4676 tempreg = treg;
4677 used_at = 0;
4678 }
4679
252b5132
RH
4680 /* When generating embedded PIC code, we permit expressions of
4681 the form
afdbd6d0
CD
4682 la $treg,foo-bar
4683 la $treg,foo-bar($breg)
bb2d6cd7 4684 where bar is an address in the current section. These are used
252b5132
RH
4685 when getting the addresses of functions. We don't permit
4686 X_add_number to be non-zero, because if the symbol is
4687 external the relaxing code needs to know that any addend is
4688 purely the offset to X_op_symbol. */
4689 if (mips_pic == EMBEDDED_PIC
4690 && offset_expr.X_op == O_subtract
49309057 4691 && (symbol_constant_p (offset_expr.X_op_symbol)
bb2d6cd7 4692 ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
49309057
ILT
4693 : (symbol_equated_p (offset_expr.X_op_symbol)
4694 && (S_GET_SEGMENT
4695 (symbol_get_value_expression (offset_expr.X_op_symbol)
4696 ->X_add_symbol)
bb2d6cd7 4697 == now_seg)))
bb2d6cd7
GK
4698 && (offset_expr.X_add_number == 0
4699 || OUTPUT_FLAVOR == bfd_target_elf_flavour))
252b5132 4700 {
afdbd6d0
CD
4701 if (breg == 0)
4702 {
4703 tempreg = treg;
4704 used_at = 0;
4705 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4706 tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4707 }
4708 else
4709 {
4710 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4711 tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4712 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4d34fb5f 4713 (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu",
afdbd6d0
CD
4714 "d,v,t", tempreg, tempreg, breg);
4715 }
252b5132 4716 macro_build ((char *) NULL, &icnt, &offset_expr,
4d34fb5f 4717 (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
afdbd6d0
CD
4718 "t,r,j", treg, tempreg, (int) BFD_RELOC_PCREL_LO16);
4719 if (! used_at)
4720 return;
4721 break;
252b5132
RH
4722 }
4723
4724 if (offset_expr.X_op != O_symbol
4725 && offset_expr.X_op != O_constant)
4726 {
4727 as_bad (_("expression too complex"));
4728 offset_expr.X_op = O_constant;
4729 }
4730
252b5132 4731 if (offset_expr.X_op == O_constant)
4d34fb5f
TS
4732 load_register (&icnt, tempreg, &offset_expr,
4733 ((mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
4734 ? (dbl || HAVE_64BIT_ADDRESSES)
4735 : HAVE_64BIT_ADDRESSES));
252b5132
RH
4736 else if (mips_pic == NO_PIC)
4737 {
d6bc6245 4738 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 4739 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
4740 Otherwise we want
4741 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
4742 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4743 If we have a constant, we need two instructions anyhow,
d6bc6245 4744 so we may as well always use the latter form.
76b3015f 4745
d6bc6245
TS
4746 With 64bit address space and a usable $at we want
4747 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4748 lui $at,<sym> (BFD_RELOC_HI16_S)
4749 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4750 daddiu $at,<sym> (BFD_RELOC_LO16)
4751 dsll32 $tempreg,0
4752 dadd $tempreg,$tempreg,$at
76b3015f 4753
d6bc6245
TS
4754 If $at is already in use, we use an path which is suboptimal
4755 on superscalar processors.
4756 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4757 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4758 dsll $tempreg,16
4759 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
4760 dsll $tempreg,16
4761 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
4762 */
f9419b05 4763 char *p = NULL;
d6bc6245 4764 if (HAVE_64BIT_ADDRESSES)
252b5132 4765 {
d6bc6245
TS
4766 /* We don't do GP optimization for now because RELAX_ENCODE can't
4767 hold the data for such large chunks. */
4768
98d3f06f
KH
4769 if (used_at == 0)
4770 {
4771 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4772 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4773 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4774 AT, (int) BFD_RELOC_HI16_S);
4775 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4776 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4777 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4778 AT, AT, (int) BFD_RELOC_LO16);
4779 macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
4780 "d,w,<", tempreg, tempreg, 0);
4781 macro_build (p, &icnt, (expressionS *) NULL, "dadd", "d,v,t",
4782 tempreg, tempreg, AT);
4783 used_at = 1;
4784 }
4785 else
4786 {
4787 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4788 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4789 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4790 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4791 macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4792 tempreg, tempreg, 16);
4793 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4794 tempreg, tempreg, (int) BFD_RELOC_HI16_S);
4795 macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4796 tempreg, tempreg, 16);
4797 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4798 tempreg, tempreg, (int) BFD_RELOC_LO16);
4799 }
4800 }
4801 else
4802 {
4803 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4804 && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
4805 {
4806 frag_grow (20);
4807 macro_build ((char *) NULL, &icnt, &offset_expr, "addiu",
c9914766
TS
4808 "t,r,j", tempreg, mips_gp_register,
4809 (int) BFD_RELOC_GPREL16);
98d3f06f
KH
4810 p = frag_var (rs_machine_dependent, 8, 0,
4811 RELAX_ENCODE (4, 8, 0, 4, 0,
4812 mips_opts.warn_about_macros),
4813 offset_expr.X_add_symbol, 0, NULL);
4814 }
4815 macro_build_lui (p, &icnt, &offset_expr, tempreg);
4816 if (p != NULL)
4817 p += 4;
4818 macro_build (p, &icnt, &offset_expr, "addiu",
4819 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4820 }
252b5132
RH
4821 }
4822 else if (mips_pic == SVR4_PIC && ! mips_big_got)
4823 {
9117d219
NC
4824 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
4825
252b5132
RH
4826 /* If this is a reference to an external symbol, and there
4827 is no constant, we want
4828 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9117d219
NC
4829 or if tempreg is PIC_CALL_REG
4830 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
252b5132
RH
4831 For a local symbol, we want
4832 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4833 nop
4834 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4835
4836 If we have a small constant, and this is a reference to
4837 an external symbol, we want
4838 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4839 nop
4840 addiu $tempreg,$tempreg,<constant>
4841 For a local symbol, we want the same instruction
4842 sequence, but we output a BFD_RELOC_LO16 reloc on the
4843 addiu instruction.
4844
4845 If we have a large constant, and this is a reference to
4846 an external symbol, we want
4847 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4848 lui $at,<hiconstant>
4849 addiu $at,$at,<loconstant>
4850 addu $tempreg,$tempreg,$at
4851 For a local symbol, we want the same instruction
4852 sequence, but we output a BFD_RELOC_LO16 reloc on the
4853 addiu instruction. */
4854 expr1.X_add_number = offset_expr.X_add_number;
4855 offset_expr.X_add_number = 0;
4856 frag_grow (32);
9117d219
NC
4857 if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4858 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
4d34fb5f
TS
4859 macro_build ((char *) NULL, &icnt, &offset_expr,
4860 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
c9914766 4861 "t,o(b)", tempreg, lw_reloc_type, mips_gp_register);
252b5132
RH
4862 if (expr1.X_add_number == 0)
4863 {
4864 int off;
f9419b05 4865 char *p;
252b5132
RH
4866
4867 if (breg == 0)
4868 off = 0;
4869 else
4870 {
4871 /* We're going to put in an addu instruction using
4872 tempreg, so we may as well insert the nop right
4873 now. */
4874 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4875 "nop", "");
4876 off = 4;
4877 }
4878 p = frag_var (rs_machine_dependent, 8 - off, 0,
4879 RELAX_ENCODE (0, 8 - off, -4 - off, 4 - off, 0,
4880 (breg == 0
4881 ? mips_opts.warn_about_macros
4882 : 0)),
c4e7957c 4883 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
4884 if (breg == 0)
4885 {
4886 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4887 p += 4;
4888 }
4889 macro_build (p, &icnt, &expr1,
ca4e0257 4890 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
4891 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4892 /* FIXME: If breg == 0, and the next instruction uses
4893 $tempreg, then if this variant case is used an extra
4894 nop will be generated. */
4895 }
4896 else if (expr1.X_add_number >= -0x8000
4897 && expr1.X_add_number < 0x8000)
4898 {
4899 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4900 "nop", "");
4901 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 4902 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132 4903 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
c4e7957c
TS
4904 frag_var (rs_machine_dependent, 0, 0,
4905 RELAX_ENCODE (0, 0, -12, -4, 0, 0),
4906 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
4907 }
4908 else
4909 {
4910 int off1;
4911
4912 /* If we are going to add in a base register, and the
4913 target register and the base register are the same,
4914 then we are using AT as a temporary register. Since
4915 we want to load the constant into AT, we add our
4916 current AT (from the global offset table) and the
4917 register into the register now, and pretend we were
4918 not using a base register. */
4919 if (breg != treg)
4920 off1 = 0;
4921 else
4922 {
4923 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4924 "nop", "");
4925 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 4926 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
4927 "d,v,t", treg, AT, breg);
4928 breg = 0;
4929 tempreg = treg;
4930 off1 = -8;
4931 }
4932
4933 /* Set mips_optimize around the lui instruction to avoid
4934 inserting an unnecessary nop after the lw. */
4935 hold_mips_optimize = mips_optimize;
4936 mips_optimize = 2;
c4e7957c 4937 macro_build_lui (NULL, &icnt, &expr1, AT);
252b5132
RH
4938 mips_optimize = hold_mips_optimize;
4939
4940 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 4941 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
4942 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4943 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 4944 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132 4945 "d,v,t", tempreg, tempreg, AT);
c4e7957c
TS
4946 frag_var (rs_machine_dependent, 0, 0,
4947 RELAX_ENCODE (0, 0, -16 + off1, -8, 0, 0),
4948 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
4949 used_at = 1;
4950 }
4951 }
4952 else if (mips_pic == SVR4_PIC)
4953 {
4954 int gpdel;
f9419b05 4955 char *p;
9117d219
NC
4956 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
4957 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
252b5132
RH
4958
4959 /* This is the large GOT case. If this is a reference to an
4960 external symbol, and there is no constant, we want
4961 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
4962 addu $tempreg,$tempreg,$gp
4963 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
9117d219
NC
4964 or if tempreg is PIC_CALL_REG
4965 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
4966 addu $tempreg,$tempreg,$gp
4967 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
252b5132
RH
4968 For a local symbol, we want
4969 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4970 nop
4971 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4972
4973 If we have a small constant, and this is a reference to
4974 an external symbol, we want
4975 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
4976 addu $tempreg,$tempreg,$gp
4977 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4978 nop
4979 addiu $tempreg,$tempreg,<constant>
4980 For a local symbol, we want
4981 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4982 nop
4983 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
4984
4985 If we have a large constant, and this is a reference to
4986 an external symbol, we want
4987 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
4988 addu $tempreg,$tempreg,$gp
4989 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4990 lui $at,<hiconstant>
4991 addiu $at,$at,<loconstant>
4992 addu $tempreg,$tempreg,$at
4993 For a local symbol, we want
4994 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4995 lui $at,<hiconstant>
4996 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
4997 addu $tempreg,$tempreg,$at
438c16b8
TS
4998
4999 For NewABI, we want for data addresses
5000 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5001 If tempreg is PIC_CALL_REG pointing to a external symbol, we want
5002 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5003 */
5004 if (HAVE_NEWABI)
5005 {
5006 int reloc_type = (tempreg == PIC_CALL_REG
5007 ? BFD_RELOC_MIPS_CALL16
5008 : BFD_RELOC_MIPS_GOT_DISP);
5009
5010 macro_build ((char *) NULL, &icnt, &offset_expr,
5011 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5012 "t,o(b)", tempreg, reloc_type, mips_gp_register);
5013
5014 if (breg != 0)
5015 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5016 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5017 "d,v,t", treg, tempreg, breg);
5018
5019 if (! used_at)
5020 return;
5021
5022 break;
5023 }
252b5132
RH
5024 expr1.X_add_number = offset_expr.X_add_number;
5025 offset_expr.X_add_number = 0;
5026 frag_grow (52);
f7ea7ef2 5027 if (reg_needs_delay (mips_gp_register))
252b5132
RH
5028 gpdel = 4;
5029 else
5030 gpdel = 0;
9117d219
NC
5031 if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
5032 {
5033 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
5034 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
5035 }
252b5132 5036 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
9117d219 5037 tempreg, lui_reloc_type);
252b5132 5038 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5039 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 5040 "d,v,t", tempreg, tempreg, mips_gp_register);
252b5132 5041 macro_build ((char *) NULL, &icnt, &offset_expr,
4d34fb5f 5042 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
9117d219 5043 "t,o(b)", tempreg, lw_reloc_type, tempreg);
252b5132
RH
5044 if (expr1.X_add_number == 0)
5045 {
5046 int off;
5047
5048 if (breg == 0)
5049 off = 0;
5050 else
5051 {
5052 /* We're going to put in an addu instruction using
5053 tempreg, so we may as well insert the nop right
5054 now. */
5055 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5056 "nop", "");
5057 off = 4;
5058 }
5059
5060 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5061 RELAX_ENCODE (12 + off, 12 + gpdel, gpdel,
5062 8 + gpdel, 0,
5063 (breg == 0
5064 ? mips_opts.warn_about_macros
5065 : 0)),
c4e7957c 5066 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5067 }
5068 else if (expr1.X_add_number >= -0x8000
5069 && expr1.X_add_number < 0x8000)
5070 {
5071 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5072 "nop", "");
5073 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 5074 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
5075 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5076
5077 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5078 RELAX_ENCODE (20, 12 + gpdel, gpdel, 8 + gpdel, 0,
5079 (breg == 0
5080 ? mips_opts.warn_about_macros
5081 : 0)),
c4e7957c 5082 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5083 }
5084 else
5085 {
5086 int adj, dreg;
5087
5088 /* If we are going to add in a base register, and the
5089 target register and the base register are the same,
5090 then we are using AT as a temporary register. Since
5091 we want to load the constant into AT, we add our
5092 current AT (from the global offset table) and the
5093 register into the register now, and pretend we were
5094 not using a base register. */
5095 if (breg != treg)
5096 {
5097 adj = 0;
5098 dreg = tempreg;
5099 }
5100 else
5101 {
5102 assert (tempreg == AT);
5103 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5104 "nop", "");
5105 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5106 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5107 "d,v,t", treg, AT, breg);
5108 dreg = treg;
5109 adj = 8;
5110 }
5111
5112 /* Set mips_optimize around the lui instruction to avoid
5113 inserting an unnecessary nop after the lw. */
5114 hold_mips_optimize = mips_optimize;
5115 mips_optimize = 2;
c4e7957c 5116 macro_build_lui (NULL, &icnt, &expr1, AT);
252b5132
RH
5117 mips_optimize = hold_mips_optimize;
5118
5119 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 5120 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
5121 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5122 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5123 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5124 "d,v,t", dreg, dreg, AT);
5125
5126 p = frag_var (rs_machine_dependent, 16 + gpdel + adj, 0,
5127 RELAX_ENCODE (24 + adj, 16 + gpdel + adj, gpdel,
5128 8 + gpdel, 0,
5129 (breg == 0
5130 ? mips_opts.warn_about_macros
5131 : 0)),
c4e7957c 5132 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5133
5134 used_at = 1;
5135 }
5136
5137 if (gpdel > 0)
5138 {
5139 /* This is needed because this instruction uses $gp, but
5140 the first instruction on the main stream does not. */
5141 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5142 p += 4;
5143 }
5144 macro_build (p, &icnt, &offset_expr,
4d34fb5f 5145 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
c9914766
TS
5146 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16,
5147 mips_gp_register);
252b5132
RH
5148 p += 4;
5149 if (expr1.X_add_number >= -0x8000
5150 && expr1.X_add_number < 0x8000)
5151 {
5152 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5153 p += 4;
5154 macro_build (p, &icnt, &expr1,
ca4e0257 5155 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
5156 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5157 /* FIXME: If add_number is 0, and there was no base
5158 register, the external symbol case ended with a load,
5159 so if the symbol turns out to not be external, and
5160 the next instruction uses tempreg, an unnecessary nop
5161 will be inserted. */
5162 }
5163 else
5164 {
5165 if (breg == treg)
5166 {
5167 /* We must add in the base register now, as in the
5168 external symbol case. */
5169 assert (tempreg == AT);
5170 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5171 p += 4;
5172 macro_build (p, &icnt, (expressionS *) NULL,
ca4e0257 5173 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5174 "d,v,t", treg, AT, breg);
5175 p += 4;
5176 tempreg = treg;
5177 /* We set breg to 0 because we have arranged to add
5178 it in in both cases. */
5179 breg = 0;
5180 }
5181
5182 macro_build_lui (p, &icnt, &expr1, AT);
5183 p += 4;
5184 macro_build (p, &icnt, &expr1,
ca4e0257 5185 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
5186 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5187 p += 4;
5188 macro_build (p, &icnt, (expressionS *) NULL,
ca4e0257 5189 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5190 "d,v,t", tempreg, tempreg, AT);
5191 p += 4;
5192 }
5193 }
5194 else if (mips_pic == EMBEDDED_PIC)
5195 {
5196 /* We use
cdf6fd85 5197 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
5198 */
5199 macro_build ((char *) NULL, &icnt, &offset_expr,
c9914766
TS
5200 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
5201 tempreg, mips_gp_register, (int) BFD_RELOC_GPREL16);
252b5132
RH
5202 }
5203 else
5204 abort ();
5205
5206 if (breg != 0)
4d34fb5f
TS
5207 {
5208 char *s;
5209
5210 if (mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
5211 s = (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu";
5212 else
5213 s = HAVE_64BIT_ADDRESSES ? "daddu" : "addu";
5214
5215 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s,
5216 "d,v,t", treg, tempreg, breg);
5217 }
252b5132
RH
5218
5219 if (! used_at)
5220 return;
5221
5222 break;
5223
5224 case M_J_A:
5225 /* The j instruction may not be used in PIC code, since it
5226 requires an absolute address. We convert it to a b
5227 instruction. */
5228 if (mips_pic == NO_PIC)
5229 macro_build ((char *) NULL, &icnt, &offset_expr, "j", "a");
5230 else
5231 macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
5232 return;
5233
5234 /* The jal instructions must be handled as macros because when
5235 generating PIC code they expand to multi-instruction
5236 sequences. Normally they are simple instructions. */
5237 case M_JAL_1:
5238 dreg = RA;
5239 /* Fall through. */
5240 case M_JAL_2:
5241 if (mips_pic == NO_PIC
5242 || mips_pic == EMBEDDED_PIC)
5243 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5244 "d,s", dreg, sreg);
5245 else if (mips_pic == SVR4_PIC)
5246 {
5247 if (sreg != PIC_CALL_REG)
5248 as_warn (_("MIPS PIC call to register other than $25"));
bdaaa2e1 5249
252b5132
RH
5250 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5251 "d,s", dreg, sreg);
6478892d 5252 if (! HAVE_NEWABI)
252b5132 5253 {
6478892d
TS
5254 if (mips_cprestore_offset < 0)
5255 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5256 else
5257 {
7a621144
DJ
5258 if (! mips_frame_reg_valid)
5259 {
5260 as_warn (_("No .frame pseudo-op used in PIC code"));
5261 /* Quiet this warning. */
5262 mips_frame_reg_valid = 1;
5263 }
5264 if (! mips_cprestore_valid)
5265 {
5266 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5267 /* Quiet this warning. */
5268 mips_cprestore_valid = 1;
5269 }
6478892d
TS
5270 expr1.X_add_number = mips_cprestore_offset;
5271 macro_build ((char *) NULL, &icnt, &expr1,
5272 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
c9914766
TS
5273 mips_gp_register, (int) BFD_RELOC_LO16,
5274 mips_frame_reg);
6478892d 5275 }
252b5132
RH
5276 }
5277 }
5278 else
5279 abort ();
5280
5281 return;
5282
5283 case M_JAL_A:
5284 if (mips_pic == NO_PIC)
5285 macro_build ((char *) NULL, &icnt, &offset_expr, "jal", "a");
5286 else if (mips_pic == SVR4_PIC)
5287 {
f9419b05
TS
5288 char *p;
5289
252b5132
RH
5290 /* If this is a reference to an external symbol, and we are
5291 using a small GOT, we want
5292 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5293 nop
f9419b05 5294 jalr $ra,$25
252b5132
RH
5295 nop
5296 lw $gp,cprestore($sp)
5297 The cprestore value is set using the .cprestore
5298 pseudo-op. If we are using a big GOT, we want
5299 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5300 addu $25,$25,$gp
5301 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
5302 nop
f9419b05 5303 jalr $ra,$25
252b5132
RH
5304 nop
5305 lw $gp,cprestore($sp)
5306 If the symbol is not external, we want
5307 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5308 nop
5309 addiu $25,$25,<sym> (BFD_RELOC_LO16)
f9419b05 5310 jalr $ra,$25
252b5132 5311 nop
438c16b8
TS
5312 lw $gp,cprestore($sp)
5313 For NewABI, we want
5314 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5315 jalr $ra,$25 (BFD_RELOC_MIPS_JALR)
5316 */
5317 if (HAVE_NEWABI)
252b5132
RH
5318 {
5319 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 5320 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132 5321 "t,o(b)", PIC_CALL_REG,
438c16b8
TS
5322 (int) BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5323 macro_build_jalr (icnt, &offset_expr);
252b5132
RH
5324 }
5325 else
5326 {
438c16b8
TS
5327 frag_grow (40);
5328 if (! mips_big_got)
5329 {
5330 macro_build ((char *) NULL, &icnt, &offset_expr,
5331 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5332 "t,o(b)", PIC_CALL_REG,
5333 (int) BFD_RELOC_MIPS_CALL16, mips_gp_register);
5334 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5335 "nop", "");
5336 p = frag_var (rs_machine_dependent, 4, 0,
5337 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5338 offset_expr.X_add_symbol, 0, NULL);
5339 }
252b5132 5340 else
252b5132 5341 {
438c16b8
TS
5342 int gpdel;
5343
5344 if (reg_needs_delay (mips_gp_register))
5345 gpdel = 4;
5346 else
5347 gpdel = 0;
5348 macro_build ((char *) NULL, &icnt, &offset_expr, "lui",
5349 "t,u", PIC_CALL_REG,
5350 (int) BFD_RELOC_MIPS_CALL_HI16);
5351 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5352 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5353 "d,v,t", PIC_CALL_REG, PIC_CALL_REG,
5354 mips_gp_register);
5355 macro_build ((char *) NULL, &icnt, &offset_expr,
5356 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5357 "t,o(b)", PIC_CALL_REG,
5358 (int) BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
5359 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5360 "nop", "");
5361 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5362 RELAX_ENCODE (16, 12 + gpdel, gpdel,
5363 8 + gpdel, 0, 0),
5364 offset_expr.X_add_symbol, 0, NULL);
5365 if (gpdel > 0)
5366 {
5367 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5368 p += 4;
5369 }
5370 macro_build (p, &icnt, &offset_expr,
5371 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5372 "t,o(b)", PIC_CALL_REG,
5373 (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
5374 p += 4;
252b5132
RH
5375 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5376 p += 4;
5377 }
5378 macro_build (p, &icnt, &offset_expr,
438c16b8
TS
5379 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5380 "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
5381 (int) BFD_RELOC_LO16);
5382 macro_build_jalr (icnt, &offset_expr);
5383
6478892d
TS
5384 if (mips_cprestore_offset < 0)
5385 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5386 else
5387 {
7a621144
DJ
5388 if (! mips_frame_reg_valid)
5389 {
5390 as_warn (_("No .frame pseudo-op used in PIC code"));
5391 /* Quiet this warning. */
5392 mips_frame_reg_valid = 1;
5393 }
5394 if (! mips_cprestore_valid)
5395 {
5396 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5397 /* Quiet this warning. */
5398 mips_cprestore_valid = 1;
5399 }
6478892d
TS
5400 if (mips_opts.noreorder)
5401 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
98d3f06f 5402 "nop", "");
6478892d
TS
5403 expr1.X_add_number = mips_cprestore_offset;
5404 macro_build ((char *) NULL, &icnt, &expr1,
5405 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
c9914766
TS
5406 mips_gp_register, (int) BFD_RELOC_LO16,
5407 mips_frame_reg);
6478892d 5408 }
252b5132
RH
5409 }
5410 }
5411 else if (mips_pic == EMBEDDED_PIC)
5412 {
5413 macro_build ((char *) NULL, &icnt, &offset_expr, "bal", "p");
5414 /* The linker may expand the call to a longer sequence which
5415 uses $at, so we must break rather than return. */
5416 break;
5417 }
5418 else
5419 abort ();
5420
5421 return;
5422
5423 case M_LB_AB:
5424 s = "lb";
5425 goto ld;
5426 case M_LBU_AB:
5427 s = "lbu";
5428 goto ld;
5429 case M_LH_AB:
5430 s = "lh";
5431 goto ld;
5432 case M_LHU_AB:
5433 s = "lhu";
5434 goto ld;
5435 case M_LW_AB:
5436 s = "lw";
5437 goto ld;
5438 case M_LWC0_AB:
5439 s = "lwc0";
bdaaa2e1 5440 /* Itbl support may require additional care here. */
252b5132
RH
5441 coproc = 1;
5442 goto ld;
5443 case M_LWC1_AB:
5444 s = "lwc1";
bdaaa2e1 5445 /* Itbl support may require additional care here. */
252b5132
RH
5446 coproc = 1;
5447 goto ld;
5448 case M_LWC2_AB:
5449 s = "lwc2";
bdaaa2e1 5450 /* Itbl support may require additional care here. */
252b5132
RH
5451 coproc = 1;
5452 goto ld;
5453 case M_LWC3_AB:
5454 s = "lwc3";
bdaaa2e1 5455 /* Itbl support may require additional care here. */
252b5132
RH
5456 coproc = 1;
5457 goto ld;
5458 case M_LWL_AB:
5459 s = "lwl";
5460 lr = 1;
5461 goto ld;
5462 case M_LWR_AB:
5463 s = "lwr";
5464 lr = 1;
5465 goto ld;
5466 case M_LDC1_AB:
ec68c924 5467 if (mips_arch == CPU_R4650)
252b5132
RH
5468 {
5469 as_bad (_("opcode not supported on this processor"));
5470 return;
5471 }
5472 s = "ldc1";
bdaaa2e1 5473 /* Itbl support may require additional care here. */
252b5132
RH
5474 coproc = 1;
5475 goto ld;
5476 case M_LDC2_AB:
5477 s = "ldc2";
bdaaa2e1 5478 /* Itbl support may require additional care here. */
252b5132
RH
5479 coproc = 1;
5480 goto ld;
5481 case M_LDC3_AB:
5482 s = "ldc3";
bdaaa2e1 5483 /* Itbl support may require additional care here. */
252b5132
RH
5484 coproc = 1;
5485 goto ld;
5486 case M_LDL_AB:
5487 s = "ldl";
5488 lr = 1;
5489 goto ld;
5490 case M_LDR_AB:
5491 s = "ldr";
5492 lr = 1;
5493 goto ld;
5494 case M_LL_AB:
5495 s = "ll";
5496 goto ld;
5497 case M_LLD_AB:
5498 s = "lld";
5499 goto ld;
5500 case M_LWU_AB:
5501 s = "lwu";
5502 ld:
5503 if (breg == treg || coproc || lr)
5504 {
5505 tempreg = AT;
5506 used_at = 1;
5507 }
5508 else
5509 {
5510 tempreg = treg;
5511 used_at = 0;
5512 }
5513 goto ld_st;
5514 case M_SB_AB:
5515 s = "sb";
5516 goto st;
5517 case M_SH_AB:
5518 s = "sh";
5519 goto st;
5520 case M_SW_AB:
5521 s = "sw";
5522 goto st;
5523 case M_SWC0_AB:
5524 s = "swc0";
bdaaa2e1 5525 /* Itbl support may require additional care here. */
252b5132
RH
5526 coproc = 1;
5527 goto st;
5528 case M_SWC1_AB:
5529 s = "swc1";
bdaaa2e1 5530 /* Itbl support may require additional care here. */
252b5132
RH
5531 coproc = 1;
5532 goto st;
5533 case M_SWC2_AB:
5534 s = "swc2";
bdaaa2e1 5535 /* Itbl support may require additional care here. */
252b5132
RH
5536 coproc = 1;
5537 goto st;
5538 case M_SWC3_AB:
5539 s = "swc3";
bdaaa2e1 5540 /* Itbl support may require additional care here. */
252b5132
RH
5541 coproc = 1;
5542 goto st;
5543 case M_SWL_AB:
5544 s = "swl";
5545 goto st;
5546 case M_SWR_AB:
5547 s = "swr";
5548 goto st;
5549 case M_SC_AB:
5550 s = "sc";
5551 goto st;
5552 case M_SCD_AB:
5553 s = "scd";
5554 goto st;
5555 case M_SDC1_AB:
ec68c924 5556 if (mips_arch == CPU_R4650)
252b5132
RH
5557 {
5558 as_bad (_("opcode not supported on this processor"));
5559 return;
5560 }
5561 s = "sdc1";
5562 coproc = 1;
bdaaa2e1 5563 /* Itbl support may require additional care here. */
252b5132
RH
5564 goto st;
5565 case M_SDC2_AB:
5566 s = "sdc2";
bdaaa2e1 5567 /* Itbl support may require additional care here. */
252b5132
RH
5568 coproc = 1;
5569 goto st;
5570 case M_SDC3_AB:
5571 s = "sdc3";
bdaaa2e1 5572 /* Itbl support may require additional care here. */
252b5132
RH
5573 coproc = 1;
5574 goto st;
5575 case M_SDL_AB:
5576 s = "sdl";
5577 goto st;
5578 case M_SDR_AB:
5579 s = "sdr";
5580 st:
5581 tempreg = AT;
5582 used_at = 1;
5583 ld_st:
bdaaa2e1 5584 /* Itbl support may require additional care here. */
252b5132
RH
5585 if (mask == M_LWC1_AB
5586 || mask == M_SWC1_AB
5587 || mask == M_LDC1_AB
5588 || mask == M_SDC1_AB
5589 || mask == M_L_DAB
5590 || mask == M_S_DAB)
5591 fmt = "T,o(b)";
5592 else if (coproc)
5593 fmt = "E,o(b)";
5594 else
5595 fmt = "t,o(b)";
5596
afdbd6d0
CD
5597 /* For embedded PIC, we allow loads where the offset is calculated
5598 by subtracting a symbol in the current segment from an unknown
5599 symbol, relative to a base register, e.g.:
5600 <op> $treg, <sym>-<localsym>($breg)
5601 This is used by the compiler for switch statements. */
76b3015f 5602 if (mips_pic == EMBEDDED_PIC
afdbd6d0
CD
5603 && offset_expr.X_op == O_subtract
5604 && (symbol_constant_p (offset_expr.X_op_symbol)
5605 ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
5606 : (symbol_equated_p (offset_expr.X_op_symbol)
5607 && (S_GET_SEGMENT
5608 (symbol_get_value_expression (offset_expr.X_op_symbol)
5609 ->X_add_symbol)
5610 == now_seg)))
5611 && breg != 0
5612 && (offset_expr.X_add_number == 0
5613 || OUTPUT_FLAVOR == bfd_target_elf_flavour))
5614 {
5615 /* For this case, we output the instructions:
5616 lui $tempreg,<sym> (BFD_RELOC_PCREL_HI16_S)
5617 addiu $tempreg,$tempreg,$breg
5618 <op> $treg,<sym>($tempreg) (BFD_RELOC_PCREL_LO16)
5619 If the relocation would fit entirely in 16 bits, it would be
5620 nice to emit:
5621 <op> $treg,<sym>($breg) (BFD_RELOC_PCREL_LO16)
5622 instead, but that seems quite difficult. */
5623 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5624 tempreg, (int) BFD_RELOC_PCREL_HI16_S);
5625 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5626 ((bfd_arch_bits_per_address (stdoutput) == 32
5627 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
5628 ? "addu" : "daddu"),
5629 "d,v,t", tempreg, tempreg, breg);
5630 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt, treg,
5631 (int) BFD_RELOC_PCREL_LO16, tempreg);
5632 if (! used_at)
5633 return;
5634 break;
5635 }
5636
252b5132
RH
5637 if (offset_expr.X_op != O_constant
5638 && offset_expr.X_op != O_symbol)
5639 {
5640 as_bad (_("expression too complex"));
5641 offset_expr.X_op = O_constant;
5642 }
5643
5644 /* A constant expression in PIC code can be handled just as it
5645 is in non PIC code. */
5646 if (mips_pic == NO_PIC
5647 || offset_expr.X_op == O_constant)
5648 {
f9419b05
TS
5649 char *p;
5650
252b5132
RH
5651 /* If this is a reference to a GP relative symbol, and there
5652 is no base register, we want
cdf6fd85 5653 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
5654 Otherwise, if there is no base register, we want
5655 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5656 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5657 If we have a constant, we need two instructions anyhow,
5658 so we always use the latter form.
5659
5660 If we have a base register, and this is a reference to a
5661 GP relative symbol, we want
5662 addu $tempreg,$breg,$gp
cdf6fd85 5663 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
5664 Otherwise we want
5665 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5666 addu $tempreg,$tempreg,$breg
5667 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245 5668 With a constant we always use the latter case.
76b3015f 5669
d6bc6245
TS
5670 With 64bit address space and no base register and $at usable,
5671 we want
5672 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5673 lui $at,<sym> (BFD_RELOC_HI16_S)
5674 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5675 dsll32 $tempreg,0
5676 daddu $tempreg,$at
5677 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5678 If we have a base register, we want
5679 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5680 lui $at,<sym> (BFD_RELOC_HI16_S)
5681 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5682 daddu $at,$breg
5683 dsll32 $tempreg,0
5684 daddu $tempreg,$at
5685 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5686
5687 Without $at we can't generate the optimal path for superscalar
5688 processors here since this would require two temporary registers.
5689 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5690 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5691 dsll $tempreg,16
5692 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5693 dsll $tempreg,16
5694 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5695 If we have a base register, we want
5696 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5697 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5698 dsll $tempreg,16
5699 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5700 dsll $tempreg,16
5701 daddu $tempreg,$tempreg,$breg
5702 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6373ee54
CD
5703
5704 If we have 64-bit addresses, as an optimization, for
5705 addresses which are 32-bit constants (e.g. kseg0/kseg1
5706 addresses) we fall back to the 32-bit address generation
5707 mechanism since it is more efficient. This code should
5708 probably attempt to generate 64-bit constants more
5709 efficiently in general.
d6bc6245 5710 */
6373ee54
CD
5711 if (HAVE_64BIT_ADDRESSES
5712 && !(offset_expr.X_op == O_constant
5713 && IS_SEXT_32BIT_NUM (offset_expr.X_add_number)))
d6bc6245
TS
5714 {
5715 p = NULL;
5716
5717 /* We don't do GP optimization for now because RELAX_ENCODE can't
5718 hold the data for such large chunks. */
5719
5720 if (used_at == 0)
5721 {
5722 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5723 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5724 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5725 AT, (int) BFD_RELOC_HI16_S);
5726 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5727 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5728 if (breg != 0)
2396cfb9
TS
5729 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5730 "d,v,t", AT, AT, breg);
5731 macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
5732 "d,w,<", tempreg, tempreg, 0);
5733 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5734 "d,v,t", tempreg, tempreg, AT);
d6bc6245
TS
5735 macro_build (p, &icnt, &offset_expr, s,
5736 fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5737 used_at = 1;
5738 }
5739 else
5740 {
5741 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5742 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5743 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5744 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
2396cfb9
TS
5745 macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5746 "d,w,<", tempreg, tempreg, 16);
d6bc6245
TS
5747 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5748 tempreg, tempreg, (int) BFD_RELOC_HI16_S);
2396cfb9
TS
5749 macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5750 "d,w,<", tempreg, tempreg, 16);
d6bc6245 5751 if (breg != 0)
2396cfb9
TS
5752 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5753 "d,v,t", tempreg, tempreg, breg);
d6bc6245
TS
5754 macro_build (p, &icnt, &offset_expr, s,
5755 fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5756 }
5757
5758 return;
5759 }
76b3015f 5760
252b5132
RH
5761 if (breg == 0)
5762 {
e7d556df 5763 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
252b5132
RH
5764 || nopic_need_relax (offset_expr.X_add_symbol, 1))
5765 p = NULL;
5766 else
5767 {
5768 frag_grow (20);
5769 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
c9914766
TS
5770 treg, (int) BFD_RELOC_GPREL16,
5771 mips_gp_register);
252b5132
RH
5772 p = frag_var (rs_machine_dependent, 8, 0,
5773 RELAX_ENCODE (4, 8, 0, 4, 0,
5774 (mips_opts.warn_about_macros
5775 || (used_at
5776 && mips_opts.noat))),
956cd1d6 5777 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5778 used_at = 0;
5779 }
5780 macro_build_lui (p, &icnt, &offset_expr, tempreg);
5781 if (p != NULL)
5782 p += 4;
5783 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5784 (int) BFD_RELOC_LO16, tempreg);
5785 }
5786 else
5787 {
e7d556df 5788 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
252b5132
RH
5789 || nopic_need_relax (offset_expr.X_add_symbol, 1))
5790 p = NULL;
5791 else
5792 {
5793 frag_grow (28);
5794 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5795 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 5796 "d,v,t", tempreg, breg, mips_gp_register);
252b5132 5797 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
cdf6fd85 5798 treg, (int) BFD_RELOC_GPREL16, tempreg);
252b5132
RH
5799 p = frag_var (rs_machine_dependent, 12, 0,
5800 RELAX_ENCODE (8, 12, 0, 8, 0, 0),
956cd1d6 5801 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5802 }
5803 macro_build_lui (p, &icnt, &offset_expr, tempreg);
5804 if (p != NULL)
5805 p += 4;
5806 macro_build (p, &icnt, (expressionS *) NULL,
ca4e0257 5807 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5808 "d,v,t", tempreg, tempreg, breg);
5809 if (p != NULL)
5810 p += 4;
5811 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5812 (int) BFD_RELOC_LO16, tempreg);
5813 }
5814 }
5815 else if (mips_pic == SVR4_PIC && ! mips_big_got)
5816 {
f9419b05
TS
5817 char *p;
5818
252b5132
RH
5819 /* If this is a reference to an external symbol, we want
5820 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5821 nop
5822 <op> $treg,0($tempreg)
5823 Otherwise we want
5824 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5825 nop
5826 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5827 <op> $treg,0($tempreg)
5828 If there is a base register, we add it to $tempreg before
5829 the <op>. If there is a constant, we stick it in the
5830 <op> instruction. We don't handle constants larger than
5831 16 bits, because we have no way to load the upper 16 bits
5832 (actually, we could handle them for the subset of cases
5833 in which we are not using $at). */
5834 assert (offset_expr.X_op == O_symbol);
5835 expr1.X_add_number = offset_expr.X_add_number;
5836 offset_expr.X_add_number = 0;
5837 if (expr1.X_add_number < -0x8000
5838 || expr1.X_add_number >= 0x8000)
5839 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5840 frag_grow (20);
5841 macro_build ((char *) NULL, &icnt, &offset_expr,
c9914766
TS
5842 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", tempreg,
5843 (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132 5844 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
bdaaa2e1 5845 p = frag_var (rs_machine_dependent, 4, 0,
252b5132 5846 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
c4e7957c 5847 offset_expr.X_add_symbol, 0, NULL);
252b5132 5848 macro_build (p, &icnt, &offset_expr,
ca4e0257 5849 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
5850 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5851 if (breg != 0)
5852 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5853 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5854 "d,v,t", tempreg, tempreg, breg);
5855 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5856 (int) BFD_RELOC_LO16, tempreg);
5857 }
5858 else if (mips_pic == SVR4_PIC)
5859 {
5860 int gpdel;
f9419b05 5861 char *p;
252b5132
RH
5862
5863 /* If this is a reference to an external symbol, we want
5864 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5865 addu $tempreg,$tempreg,$gp
5866 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5867 <op> $treg,0($tempreg)
5868 Otherwise we want
5869 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5870 nop
5871 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5872 <op> $treg,0($tempreg)
5873 If there is a base register, we add it to $tempreg before
5874 the <op>. If there is a constant, we stick it in the
5875 <op> instruction. We don't handle constants larger than
5876 16 bits, because we have no way to load the upper 16 bits
5877 (actually, we could handle them for the subset of cases
438c16b8
TS
5878 in which we are not using $at).
5879
5880 For NewABI, we want
5881 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
5882 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
5883 <op> $treg,0($tempreg)
5884 */
252b5132
RH
5885 assert (offset_expr.X_op == O_symbol);
5886 expr1.X_add_number = offset_expr.X_add_number;
5887 offset_expr.X_add_number = 0;
5888 if (expr1.X_add_number < -0x8000
5889 || expr1.X_add_number >= 0x8000)
5890 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
438c16b8
TS
5891 if (HAVE_NEWABI)
5892 {
5893 macro_build ((char *) NULL, &icnt, &offset_expr,
5894 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5895 "t,o(b)", tempreg, BFD_RELOC_MIPS_GOT_PAGE,
5896 mips_gp_register);
5897 macro_build ((char *) NULL, &icnt, &offset_expr,
5898 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5899 "t,r,j", tempreg, tempreg,
5900 BFD_RELOC_MIPS_GOT_OFST);
5901 if (breg != 0)
5902 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5903 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5904 "d,v,t", tempreg, tempreg, breg);
5905 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5906 (int) BFD_RELOC_LO16, tempreg);
5907
5908 if (! used_at)
5909 return;
5910
5911 break;
5912 }
f7ea7ef2 5913 if (reg_needs_delay (mips_gp_register))
252b5132
RH
5914 gpdel = 4;
5915 else
5916 gpdel = 0;
5917 frag_grow (36);
5918 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5919 tempreg, (int) BFD_RELOC_MIPS_GOT_HI16);
5920 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5921 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 5922 "d,v,t", tempreg, tempreg, mips_gp_register);
252b5132 5923 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 5924 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
5925 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_LO16,
5926 tempreg);
5927 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5928 RELAX_ENCODE (12, 12 + gpdel, gpdel, 8 + gpdel, 0, 0),
c4e7957c 5929 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5930 if (gpdel > 0)
5931 {
5932 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5933 p += 4;
5934 }
5935 macro_build (p, &icnt, &offset_expr,
ca4e0257 5936 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
c9914766
TS
5937 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16,
5938 mips_gp_register);
252b5132
RH
5939 p += 4;
5940 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5941 p += 4;
5942 macro_build (p, &icnt, &offset_expr,
ca4e0257 5943 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
5944 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5945 if (breg != 0)
5946 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5947 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5948 "d,v,t", tempreg, tempreg, breg);
5949 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5950 (int) BFD_RELOC_LO16, tempreg);
5951 }
5952 else if (mips_pic == EMBEDDED_PIC)
5953 {
5954 /* If there is no base register, we want
cdf6fd85 5955 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
5956 If there is a base register, we want
5957 addu $tempreg,$breg,$gp
cdf6fd85 5958 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
5959 */
5960 assert (offset_expr.X_op == O_symbol);
5961 if (breg == 0)
5962 {
5963 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
c9914766 5964 treg, (int) BFD_RELOC_GPREL16, mips_gp_register);
252b5132
RH
5965 used_at = 0;
5966 }
5967 else
5968 {
5969 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5970 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 5971 "d,v,t", tempreg, breg, mips_gp_register);
252b5132 5972 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
cdf6fd85 5973 treg, (int) BFD_RELOC_GPREL16, tempreg);
252b5132
RH
5974 }
5975 }
5976 else
5977 abort ();
5978
5979 if (! used_at)
5980 return;
5981
5982 break;
5983
5984 case M_LI:
5985 case M_LI_S:
5986 load_register (&icnt, treg, &imm_expr, 0);
5987 return;
5988
5989 case M_DLI:
5990 load_register (&icnt, treg, &imm_expr, 1);
5991 return;
5992
5993 case M_LI_SS:
5994 if (imm_expr.X_op == O_constant)
5995 {
5996 load_register (&icnt, AT, &imm_expr, 0);
5997 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5998 "mtc1", "t,G", AT, treg);
5999 break;
6000 }
6001 else
6002 {
6003 assert (offset_expr.X_op == O_symbol
6004 && strcmp (segment_name (S_GET_SEGMENT
6005 (offset_expr.X_add_symbol)),
6006 ".lit4") == 0
6007 && offset_expr.X_add_number == 0);
6008 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
c9914766 6009 treg, (int) BFD_RELOC_MIPS_LITERAL, mips_gp_register);
252b5132
RH
6010 return;
6011 }
6012
6013 case M_LI_D:
ca4e0257
RS
6014 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
6015 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
6016 order 32 bits of the value and the low order 32 bits are either
6017 zero or in OFFSET_EXPR. */
252b5132
RH
6018 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6019 {
ca4e0257 6020 if (HAVE_64BIT_GPRS)
252b5132
RH
6021 load_register (&icnt, treg, &imm_expr, 1);
6022 else
6023 {
6024 int hreg, lreg;
6025
6026 if (target_big_endian)
6027 {
6028 hreg = treg;
6029 lreg = treg + 1;
6030 }
6031 else
6032 {
6033 hreg = treg + 1;
6034 lreg = treg;
6035 }
6036
6037 if (hreg <= 31)
6038 load_register (&icnt, hreg, &imm_expr, 0);
6039 if (lreg <= 31)
6040 {
6041 if (offset_expr.X_op == O_absent)
ea1fb5dc 6042 move_register (&icnt, lreg, 0);
252b5132
RH
6043 else
6044 {
6045 assert (offset_expr.X_op == O_constant);
6046 load_register (&icnt, lreg, &offset_expr, 0);
6047 }
6048 }
6049 }
6050 return;
6051 }
6052
6053 /* We know that sym is in the .rdata section. First we get the
6054 upper 16 bits of the address. */
6055 if (mips_pic == NO_PIC)
6056 {
956cd1d6 6057 macro_build_lui (NULL, &icnt, &offset_expr, AT);
252b5132
RH
6058 }
6059 else if (mips_pic == SVR4_PIC)
6060 {
6061 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 6062 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
c9914766
TS
6063 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6064 mips_gp_register);
252b5132
RH
6065 }
6066 else if (mips_pic == EMBEDDED_PIC)
6067 {
6068 /* For embedded PIC we pick up the entire address off $gp in
6069 a single instruction. */
6070 macro_build ((char *) NULL, &icnt, &offset_expr,
c9914766
TS
6071 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j", AT,
6072 mips_gp_register, (int) BFD_RELOC_GPREL16);
252b5132
RH
6073 offset_expr.X_op = O_constant;
6074 offset_expr.X_add_number = 0;
6075 }
6076 else
6077 abort ();
bdaaa2e1 6078
252b5132 6079 /* Now we load the register(s). */
ca4e0257 6080 if (HAVE_64BIT_GPRS)
252b5132
RH
6081 macro_build ((char *) NULL, &icnt, &offset_expr, "ld", "t,o(b)",
6082 treg, (int) BFD_RELOC_LO16, AT);
6083 else
6084 {
6085 macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
6086 treg, (int) BFD_RELOC_LO16, AT);
f9419b05 6087 if (treg != RA)
252b5132
RH
6088 {
6089 /* FIXME: How in the world do we deal with the possible
6090 overflow here? */
6091 offset_expr.X_add_number += 4;
6092 macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
6093 treg + 1, (int) BFD_RELOC_LO16, AT);
6094 }
6095 }
6096
6097 /* To avoid confusion in tc_gen_reloc, we must ensure that this
6098 does not become a variant frag. */
6099 frag_wane (frag_now);
6100 frag_new (0);
6101
6102 break;
6103
6104 case M_LI_DD:
ca4e0257
RS
6105 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
6106 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
6107 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
6108 the value and the low order 32 bits are either zero or in
6109 OFFSET_EXPR. */
252b5132
RH
6110 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
6111 {
ca4e0257
RS
6112 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_FPRS);
6113 if (HAVE_64BIT_FPRS)
6114 {
6115 assert (HAVE_64BIT_GPRS);
6116 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6117 "dmtc1", "t,S", AT, treg);
6118 }
252b5132
RH
6119 else
6120 {
6121 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6122 "mtc1", "t,G", AT, treg + 1);
6123 if (offset_expr.X_op == O_absent)
6124 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6125 "mtc1", "t,G", 0, treg);
6126 else
6127 {
6128 assert (offset_expr.X_op == O_constant);
6129 load_register (&icnt, AT, &offset_expr, 0);
6130 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6131 "mtc1", "t,G", AT, treg);
6132 }
6133 }
6134 break;
6135 }
6136
6137 assert (offset_expr.X_op == O_symbol
6138 && offset_expr.X_add_number == 0);
6139 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
6140 if (strcmp (s, ".lit8") == 0)
6141 {
e7af610e 6142 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
6143 {
6144 macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
c9914766
TS
6145 "T,o(b)", treg, (int) BFD_RELOC_MIPS_LITERAL,
6146 mips_gp_register);
252b5132
RH
6147 return;
6148 }
c9914766 6149 breg = mips_gp_register;
252b5132
RH
6150 r = BFD_RELOC_MIPS_LITERAL;
6151 goto dob;
6152 }
6153 else
6154 {
6155 assert (strcmp (s, RDATA_SECTION_NAME) == 0);
6156 if (mips_pic == SVR4_PIC)
6157 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 6158 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
c9914766
TS
6159 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6160 mips_gp_register);
252b5132
RH
6161 else
6162 {
6163 /* FIXME: This won't work for a 64 bit address. */
956cd1d6 6164 macro_build_lui (NULL, &icnt, &offset_expr, AT);
252b5132 6165 }
bdaaa2e1 6166
e7af610e 6167 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
6168 {
6169 macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
6170 "T,o(b)", treg, (int) BFD_RELOC_LO16, AT);
6171
6172 /* To avoid confusion in tc_gen_reloc, we must ensure
6173 that this does not become a variant frag. */
6174 frag_wane (frag_now);
6175 frag_new (0);
6176
6177 break;
6178 }
6179 breg = AT;
6180 r = BFD_RELOC_LO16;
6181 goto dob;
6182 }
6183
6184 case M_L_DOB:
ec68c924 6185 if (mips_arch == CPU_R4650)
252b5132
RH
6186 {
6187 as_bad (_("opcode not supported on this processor"));
6188 return;
6189 }
6190 /* Even on a big endian machine $fn comes before $fn+1. We have
6191 to adjust when loading from memory. */
6192 r = BFD_RELOC_LO16;
6193 dob:
e7af610e 6194 assert (mips_opts.isa == ISA_MIPS1);
252b5132
RH
6195 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6196 target_big_endian ? treg + 1 : treg,
6197 (int) r, breg);
6198 /* FIXME: A possible overflow which I don't know how to deal
6199 with. */
6200 offset_expr.X_add_number += 4;
6201 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6202 target_big_endian ? treg : treg + 1,
6203 (int) r, breg);
6204
6205 /* To avoid confusion in tc_gen_reloc, we must ensure that this
6206 does not become a variant frag. */
6207 frag_wane (frag_now);
6208 frag_new (0);
6209
6210 if (breg != AT)
6211 return;
6212 break;
6213
6214 case M_L_DAB:
6215 /*
6216 * The MIPS assembler seems to check for X_add_number not
6217 * being double aligned and generating:
6218 * lui at,%hi(foo+1)
6219 * addu at,at,v1
6220 * addiu at,at,%lo(foo+1)
6221 * lwc1 f2,0(at)
6222 * lwc1 f3,4(at)
6223 * But, the resulting address is the same after relocation so why
6224 * generate the extra instruction?
6225 */
ec68c924 6226 if (mips_arch == CPU_R4650)
252b5132
RH
6227 {
6228 as_bad (_("opcode not supported on this processor"));
6229 return;
6230 }
bdaaa2e1 6231 /* Itbl support may require additional care here. */
252b5132 6232 coproc = 1;
e7af610e 6233 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
6234 {
6235 s = "ldc1";
6236 goto ld;
6237 }
6238
6239 s = "lwc1";
6240 fmt = "T,o(b)";
6241 goto ldd_std;
6242
6243 case M_S_DAB:
ec68c924 6244 if (mips_arch == CPU_R4650)
252b5132
RH
6245 {
6246 as_bad (_("opcode not supported on this processor"));
6247 return;
6248 }
6249
e7af610e 6250 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
6251 {
6252 s = "sdc1";
6253 goto st;
6254 }
6255
6256 s = "swc1";
6257 fmt = "T,o(b)";
bdaaa2e1 6258 /* Itbl support may require additional care here. */
252b5132
RH
6259 coproc = 1;
6260 goto ldd_std;
6261
6262 case M_LD_AB:
ca4e0257 6263 if (HAVE_64BIT_GPRS)
252b5132
RH
6264 {
6265 s = "ld";
6266 goto ld;
6267 }
6268
6269 s = "lw";
6270 fmt = "t,o(b)";
6271 goto ldd_std;
6272
6273 case M_SD_AB:
ca4e0257 6274 if (HAVE_64BIT_GPRS)
252b5132
RH
6275 {
6276 s = "sd";
6277 goto st;
6278 }
6279
6280 s = "sw";
6281 fmt = "t,o(b)";
6282
6283 ldd_std:
afdbd6d0
CD
6284 /* We do _not_ bother to allow embedded PIC (symbol-local_symbol)
6285 loads for the case of doing a pair of loads to simulate an 'ld'.
6286 This is not currently done by the compiler, and assembly coders
6287 writing embedded-pic code can cope. */
6288
252b5132
RH
6289 if (offset_expr.X_op != O_symbol
6290 && offset_expr.X_op != O_constant)
6291 {
6292 as_bad (_("expression too complex"));
6293 offset_expr.X_op = O_constant;
6294 }
6295
6296 /* Even on a big endian machine $fn comes before $fn+1. We have
6297 to adjust when loading from memory. We set coproc if we must
6298 load $fn+1 first. */
bdaaa2e1 6299 /* Itbl support may require additional care here. */
252b5132
RH
6300 if (! target_big_endian)
6301 coproc = 0;
6302
6303 if (mips_pic == NO_PIC
6304 || offset_expr.X_op == O_constant)
6305 {
f9419b05
TS
6306 char *p;
6307
252b5132 6308 /* If this is a reference to a GP relative symbol, we want
cdf6fd85
TS
6309 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6310 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
6311 If we have a base register, we use this
6312 addu $at,$breg,$gp
cdf6fd85
TS
6313 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6314 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
6315 If this is not a GP relative symbol, we want
6316 lui $at,<sym> (BFD_RELOC_HI16_S)
6317 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6318 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6319 If there is a base register, we add it to $at after the
6320 lui instruction. If there is a constant, we always use
6321 the last case. */
e7d556df 6322 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
252b5132
RH
6323 || nopic_need_relax (offset_expr.X_add_symbol, 1))
6324 {
6325 p = NULL;
6326 used_at = 1;
6327 }
6328 else
6329 {
6330 int off;
6331
6332 if (breg == 0)
6333 {
6334 frag_grow (28);
c9914766 6335 tempreg = mips_gp_register;
252b5132
RH
6336 off = 0;
6337 used_at = 0;
6338 }
6339 else
6340 {
6341 frag_grow (36);
6342 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6343 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 6344 "d,v,t", AT, breg, mips_gp_register);
252b5132
RH
6345 tempreg = AT;
6346 off = 4;
6347 used_at = 1;
6348 }
6349
beae10d5 6350 /* Itbl support may require additional care here. */
252b5132
RH
6351 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6352 coproc ? treg + 1 : treg,
cdf6fd85 6353 (int) BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6354 offset_expr.X_add_number += 4;
6355
6356 /* Set mips_optimize to 2 to avoid inserting an
6357 undesired nop. */
6358 hold_mips_optimize = mips_optimize;
6359 mips_optimize = 2;
beae10d5 6360 /* Itbl support may require additional care here. */
252b5132
RH
6361 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6362 coproc ? treg : treg + 1,
cdf6fd85 6363 (int) BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6364 mips_optimize = hold_mips_optimize;
6365
6366 p = frag_var (rs_machine_dependent, 12 + off, 0,
6367 RELAX_ENCODE (8 + off, 12 + off, 0, 4 + off, 1,
6368 used_at && mips_opts.noat),
956cd1d6 6369 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
6370
6371 /* We just generated two relocs. When tc_gen_reloc
6372 handles this case, it will skip the first reloc and
6373 handle the second. The second reloc already has an
6374 extra addend of 4, which we added above. We must
6375 subtract it out, and then subtract another 4 to make
6376 the first reloc come out right. The second reloc
6377 will come out right because we are going to add 4 to
6378 offset_expr when we build its instruction below.
6379
6380 If we have a symbol, then we don't want to include
6381 the offset, because it will wind up being included
6382 when we generate the reloc. */
6383
6384 if (offset_expr.X_op == O_constant)
6385 offset_expr.X_add_number -= 8;
6386 else
6387 {
6388 offset_expr.X_add_number = -4;
6389 offset_expr.X_op = O_constant;
6390 }
6391 }
6392 macro_build_lui (p, &icnt, &offset_expr, AT);
6393 if (p != NULL)
6394 p += 4;
6395 if (breg != 0)
6396 {
6397 macro_build (p, &icnt, (expressionS *) NULL,
ca4e0257 6398 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
6399 "d,v,t", AT, breg, AT);
6400 if (p != NULL)
6401 p += 4;
6402 }
beae10d5 6403 /* Itbl support may require additional care here. */
252b5132
RH
6404 macro_build (p, &icnt, &offset_expr, s, fmt,
6405 coproc ? treg + 1 : treg,
6406 (int) BFD_RELOC_LO16, AT);
6407 if (p != NULL)
6408 p += 4;
6409 /* FIXME: How do we handle overflow here? */
6410 offset_expr.X_add_number += 4;
beae10d5 6411 /* Itbl support may require additional care here. */
252b5132
RH
6412 macro_build (p, &icnt, &offset_expr, s, fmt,
6413 coproc ? treg : treg + 1,
6414 (int) BFD_RELOC_LO16, AT);
bdaaa2e1 6415 }
252b5132
RH
6416 else if (mips_pic == SVR4_PIC && ! mips_big_got)
6417 {
6418 int off;
6419
6420 /* If this is a reference to an external symbol, we want
6421 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6422 nop
6423 <op> $treg,0($at)
6424 <op> $treg+1,4($at)
6425 Otherwise we want
6426 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6427 nop
6428 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6429 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6430 If there is a base register we add it to $at before the
6431 lwc1 instructions. If there is a constant we include it
6432 in the lwc1 instructions. */
6433 used_at = 1;
6434 expr1.X_add_number = offset_expr.X_add_number;
6435 offset_expr.X_add_number = 0;
6436 if (expr1.X_add_number < -0x8000
6437 || expr1.X_add_number >= 0x8000 - 4)
6438 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6439 if (breg == 0)
6440 off = 0;
6441 else
6442 off = 4;
6443 frag_grow (24 + off);
6444 macro_build ((char *) NULL, &icnt, &offset_expr,
c9914766
TS
6445 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", AT,
6446 (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132
RH
6447 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6448 if (breg != 0)
6449 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6450 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132 6451 "d,v,t", AT, breg, AT);
beae10d5 6452 /* Itbl support may require additional care here. */
252b5132
RH
6453 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6454 coproc ? treg + 1 : treg,
6455 (int) BFD_RELOC_LO16, AT);
6456 expr1.X_add_number += 4;
6457
6458 /* Set mips_optimize to 2 to avoid inserting an undesired
6459 nop. */
6460 hold_mips_optimize = mips_optimize;
6461 mips_optimize = 2;
beae10d5 6462 /* Itbl support may require additional care here. */
252b5132
RH
6463 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6464 coproc ? treg : treg + 1,
6465 (int) BFD_RELOC_LO16, AT);
6466 mips_optimize = hold_mips_optimize;
6467
6468 (void) frag_var (rs_machine_dependent, 0, 0,
6469 RELAX_ENCODE (0, 0, -16 - off, -8, 1, 0),
c4e7957c 6470 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
6471 }
6472 else if (mips_pic == SVR4_PIC)
6473 {
6474 int gpdel, off;
f9419b05 6475 char *p;
252b5132
RH
6476
6477 /* If this is a reference to an external symbol, we want
6478 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6479 addu $at,$at,$gp
6480 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
6481 nop
6482 <op> $treg,0($at)
6483 <op> $treg+1,4($at)
6484 Otherwise we want
6485 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6486 nop
6487 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6488 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6489 If there is a base register we add it to $at before the
6490 lwc1 instructions. If there is a constant we include it
6491 in the lwc1 instructions. */
6492 used_at = 1;
6493 expr1.X_add_number = offset_expr.X_add_number;
6494 offset_expr.X_add_number = 0;
6495 if (expr1.X_add_number < -0x8000
6496 || expr1.X_add_number >= 0x8000 - 4)
6497 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
c9914766 6498 if (reg_needs_delay (mips_gp_register))
252b5132
RH
6499 gpdel = 4;
6500 else
6501 gpdel = 0;
6502 if (breg == 0)
6503 off = 0;
6504 else
6505 off = 4;
6506 frag_grow (56);
6507 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
6508 AT, (int) BFD_RELOC_MIPS_GOT_HI16);
6509 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6510 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 6511 "d,v,t", AT, AT, mips_gp_register);
252b5132 6512 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 6513 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
6514 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT_LO16, AT);
6515 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6516 if (breg != 0)
6517 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6518 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132 6519 "d,v,t", AT, breg, AT);
beae10d5 6520 /* Itbl support may require additional care here. */
252b5132
RH
6521 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6522 coproc ? treg + 1 : treg,
6523 (int) BFD_RELOC_LO16, AT);
6524 expr1.X_add_number += 4;
6525
6526 /* Set mips_optimize to 2 to avoid inserting an undesired
6527 nop. */
6528 hold_mips_optimize = mips_optimize;
6529 mips_optimize = 2;
beae10d5 6530 /* Itbl support may require additional care here. */
252b5132
RH
6531 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6532 coproc ? treg : treg + 1,
6533 (int) BFD_RELOC_LO16, AT);
6534 mips_optimize = hold_mips_optimize;
6535 expr1.X_add_number -= 4;
6536
6537 p = frag_var (rs_machine_dependent, 16 + gpdel + off, 0,
6538 RELAX_ENCODE (24 + off, 16 + gpdel + off, gpdel,
6539 8 + gpdel + off, 1, 0),
c4e7957c 6540 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
6541 if (gpdel > 0)
6542 {
6543 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6544 p += 4;
6545 }
6546 macro_build (p, &icnt, &offset_expr,
ca4e0257 6547 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
c9914766
TS
6548 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6549 mips_gp_register);
252b5132
RH
6550 p += 4;
6551 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6552 p += 4;
6553 if (breg != 0)
6554 {
6555 macro_build (p, &icnt, (expressionS *) NULL,
ca4e0257 6556 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
6557 "d,v,t", AT, breg, AT);
6558 p += 4;
6559 }
beae10d5 6560 /* Itbl support may require additional care here. */
252b5132
RH
6561 macro_build (p, &icnt, &expr1, s, fmt,
6562 coproc ? treg + 1 : treg,
6563 (int) BFD_RELOC_LO16, AT);
6564 p += 4;
6565 expr1.X_add_number += 4;
6566
6567 /* Set mips_optimize to 2 to avoid inserting an undesired
6568 nop. */
6569 hold_mips_optimize = mips_optimize;
6570 mips_optimize = 2;
beae10d5 6571 /* Itbl support may require additional care here. */
252b5132
RH
6572 macro_build (p, &icnt, &expr1, s, fmt,
6573 coproc ? treg : treg + 1,
6574 (int) BFD_RELOC_LO16, AT);
6575 mips_optimize = hold_mips_optimize;
6576 }
6577 else if (mips_pic == EMBEDDED_PIC)
6578 {
6579 /* If there is no base register, we use
cdf6fd85
TS
6580 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6581 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
6582 If we have a base register, we use
6583 addu $at,$breg,$gp
cdf6fd85
TS
6584 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6585 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
6586 */
6587 if (breg == 0)
6588 {
c9914766 6589 tempreg = mips_gp_register;
252b5132
RH
6590 used_at = 0;
6591 }
6592 else
6593 {
6594 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6595 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 6596 "d,v,t", AT, breg, mips_gp_register);
252b5132
RH
6597 tempreg = AT;
6598 used_at = 1;
6599 }
6600
beae10d5 6601 /* Itbl support may require additional care here. */
252b5132
RH
6602 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6603 coproc ? treg + 1 : treg,
cdf6fd85 6604 (int) BFD_RELOC_GPREL16, tempreg);
252b5132 6605 offset_expr.X_add_number += 4;
beae10d5 6606 /* Itbl support may require additional care here. */
252b5132
RH
6607 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6608 coproc ? treg : treg + 1,
cdf6fd85 6609 (int) BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6610 }
6611 else
6612 abort ();
6613
6614 if (! used_at)
6615 return;
6616
6617 break;
6618
6619 case M_LD_OB:
6620 s = "lw";
6621 goto sd_ob;
6622 case M_SD_OB:
6623 s = "sw";
6624 sd_ob:
ca4e0257 6625 assert (HAVE_32BIT_ADDRESSES);
252b5132
RH
6626 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
6627 (int) BFD_RELOC_LO16, breg);
6628 offset_expr.X_add_number += 4;
6629 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg + 1,
6630 (int) BFD_RELOC_LO16, breg);
6631 return;
6632
6633 /* New code added to support COPZ instructions.
6634 This code builds table entries out of the macros in mip_opcodes.
6635 R4000 uses interlocks to handle coproc delays.
6636 Other chips (like the R3000) require nops to be inserted for delays.
6637
f72c8c98 6638 FIXME: Currently, we require that the user handle delays.
252b5132
RH
6639 In order to fill delay slots for non-interlocked chips,
6640 we must have a way to specify delays based on the coprocessor.
6641 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
6642 What are the side-effects of the cop instruction?
6643 What cache support might we have and what are its effects?
6644 Both coprocessor & memory require delays. how long???
bdaaa2e1 6645 What registers are read/set/modified?
252b5132
RH
6646
6647 If an itbl is provided to interpret cop instructions,
bdaaa2e1 6648 this knowledge can be encoded in the itbl spec. */
252b5132
RH
6649
6650 case M_COP0:
6651 s = "c0";
6652 goto copz;
6653 case M_COP1:
6654 s = "c1";
6655 goto copz;
6656 case M_COP2:
6657 s = "c2";
6658 goto copz;
6659 case M_COP3:
6660 s = "c3";
6661 copz:
6662 /* For now we just do C (same as Cz). The parameter will be
6663 stored in insn_opcode by mips_ip. */
6664 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "C",
6665 ip->insn_opcode);
6666 return;
6667
ea1fb5dc
RS
6668 case M_MOVE:
6669 move_register (&icnt, dreg, sreg);
6670 return;
6671
252b5132
RH
6672#ifdef LOSING_COMPILER
6673 default:
6674 /* Try and see if this is a new itbl instruction.
6675 This code builds table entries out of the macros in mip_opcodes.
6676 FIXME: For now we just assemble the expression and pass it's
6677 value along as a 32-bit immediate.
bdaaa2e1 6678 We may want to have the assembler assemble this value,
252b5132
RH
6679 so that we gain the assembler's knowledge of delay slots,
6680 symbols, etc.
6681 Would it be more efficient to use mask (id) here? */
bdaaa2e1 6682 if (itbl_have_entries
252b5132 6683 && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
beae10d5 6684 {
252b5132
RH
6685 s = ip->insn_mo->name;
6686 s2 = "cop3";
6687 coproc = ITBL_DECODE_PNUM (immed_expr);;
6688 macro_build ((char *) NULL, &icnt, &immed_expr, s, "C");
6689 return;
beae10d5 6690 }
252b5132
RH
6691 macro2 (ip);
6692 return;
6693 }
6694 if (mips_opts.noat)
6695 as_warn (_("Macro used $at after \".set noat\""));
6696}
bdaaa2e1 6697
252b5132
RH
6698static void
6699macro2 (ip)
6700 struct mips_cl_insn *ip;
6701{
6702 register int treg, sreg, dreg, breg;
6703 int tempreg;
6704 int mask;
6705 int icnt = 0;
6706 int used_at;
6707 expressionS expr1;
6708 const char *s;
6709 const char *s2;
6710 const char *fmt;
6711 int likely = 0;
6712 int dbl = 0;
6713 int coproc = 0;
6714 int lr = 0;
6715 int imm = 0;
6716 int off;
6717 offsetT maxnum;
6718 bfd_reloc_code_real_type r;
6719 char *p;
bdaaa2e1 6720
252b5132
RH
6721 treg = (ip->insn_opcode >> 16) & 0x1f;
6722 dreg = (ip->insn_opcode >> 11) & 0x1f;
6723 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
6724 mask = ip->insn_mo->mask;
bdaaa2e1 6725
252b5132
RH
6726 expr1.X_op = O_constant;
6727 expr1.X_op_symbol = NULL;
6728 expr1.X_add_symbol = NULL;
6729 expr1.X_add_number = 1;
bdaaa2e1 6730
252b5132
RH
6731 switch (mask)
6732 {
6733#endif /* LOSING_COMPILER */
6734
6735 case M_DMUL:
6736 dbl = 1;
6737 case M_MUL:
2396cfb9
TS
6738 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6739 dbl ? "dmultu" : "multu", "s,t", sreg, treg);
6740 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6741 dreg);
252b5132
RH
6742 return;
6743
6744 case M_DMUL_I:
6745 dbl = 1;
6746 case M_MUL_I:
6747 /* The MIPS assembler some times generates shifts and adds. I'm
6748 not trying to be that fancy. GCC should do this for us
6749 anyway. */
6750 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9 6751 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 6752 dbl ? "dmult" : "mult", "s,t", sreg, AT);
2396cfb9
TS
6753 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6754 dreg);
252b5132
RH
6755 break;
6756
6757 case M_DMULO_I:
6758 dbl = 1;
6759 case M_MULO_I:
6760 imm = 1;
6761 goto do_mulo;
6762
6763 case M_DMULO:
6764 dbl = 1;
6765 case M_MULO:
6766 do_mulo:
6767 mips_emit_delays (true);
6768 ++mips_opts.noreorder;
6769 mips_any_noreorder = 1;
6770 if (imm)
6771 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9 6772 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 6773 dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
2396cfb9
TS
6774 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6775 dreg);
6776 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
f9419b05 6777 dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, RA);
2396cfb9
TS
6778 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6779 AT);
252b5132 6780 if (mips_trap)
2396cfb9
TS
6781 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne", "s,t",
6782 dreg, AT);
252b5132
RH
6783 else
6784 {
6785 expr1.X_add_number = 8;
2396cfb9
TS
6786 macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", dreg,
6787 AT);
6788 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6789 0);
6790 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6791 "c", 6);
252b5132
RH
6792 }
6793 --mips_opts.noreorder;
2396cfb9 6794 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d", dreg);
252b5132
RH
6795 break;
6796
6797 case M_DMULOU_I:
6798 dbl = 1;
6799 case M_MULOU_I:
6800 imm = 1;
6801 goto do_mulou;
6802
6803 case M_DMULOU:
6804 dbl = 1;
6805 case M_MULOU:
6806 do_mulou:
6807 mips_emit_delays (true);
6808 ++mips_opts.noreorder;
6809 mips_any_noreorder = 1;
6810 if (imm)
6811 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9 6812 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
252b5132
RH
6813 dbl ? "dmultu" : "multu",
6814 "s,t", sreg, imm ? AT : treg);
2396cfb9
TS
6815 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6816 AT);
6817 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6818 dreg);
252b5132 6819 if (mips_trap)
2396cfb9
TS
6820 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne", "s,t",
6821 AT, 0);
252b5132
RH
6822 else
6823 {
6824 expr1.X_add_number = 8;
6825 macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", AT, 0);
2396cfb9
TS
6826 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6827 0);
6828 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6829 "c", 6);
252b5132
RH
6830 }
6831 --mips_opts.noreorder;
6832 break;
6833
771c7ce4
TS
6834 case M_DROL:
6835 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
6836 "d,v,t", AT, 0, treg);
6837 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
6838 "d,t,s", AT, sreg, AT);
6839 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
6840 "d,t,s", dreg, sreg, treg);
6841 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6842 "d,v,t", dreg, dreg, AT);
6843 break;
6844
252b5132 6845 case M_ROL:
2396cfb9
TS
6846 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
6847 "d,v,t", AT, 0, treg);
6848 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
6849 "d,t,s", AT, sreg, AT);
6850 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
6851 "d,t,s", dreg, sreg, treg);
6852 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6853 "d,v,t", dreg, dreg, AT);
252b5132
RH
6854 break;
6855
771c7ce4
TS
6856 case M_DROL_I:
6857 {
6858 unsigned int rot;
6859 char *l, *r;
6860
6861 if (imm_expr.X_op != O_constant)
6862 as_bad (_("rotate count too large"));
6863 rot = imm_expr.X_add_number & 0x3f;
6864 if (! rot)
6865 break;
6866 l = (rot < 0x20) ? "dsll" : "dsll32";
6867 r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
6868 rot &= 0x1f;
6869 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
6870 "d,w,<", AT, sreg, rot);
6871 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
6872 "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6873 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6874 "d,v,t", dreg, dreg, AT);
6875 }
6876 break;
6877
252b5132 6878 case M_ROL_I:
771c7ce4
TS
6879 {
6880 unsigned int rot;
6881
6882 if (imm_expr.X_op != O_constant)
6883 as_bad (_("rotate count too large"));
6884 rot = imm_expr.X_add_number & 0x1f;
6885 if (! rot)
6886 break;
6887 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
6888 "d,w,<", AT, sreg, rot);
6889 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
6890 "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6891 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6892 "d,v,t", dreg, dreg, AT);
6893 }
6894 break;
6895
6896 case M_DROR:
6897 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
6898 "d,v,t", AT, 0, treg);
6899 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
6900 "d,t,s", AT, sreg, AT);
6901 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
6902 "d,t,s", dreg, sreg, treg);
6903 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6904 "d,v,t", dreg, dreg, AT);
252b5132
RH
6905 break;
6906
6907 case M_ROR:
2396cfb9
TS
6908 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
6909 "d,v,t", AT, 0, treg);
6910 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
6911 "d,t,s", AT, sreg, AT);
6912 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
6913 "d,t,s", dreg, sreg, treg);
6914 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6915 "d,v,t", dreg, dreg, AT);
252b5132
RH
6916 break;
6917
771c7ce4
TS
6918 case M_DROR_I:
6919 {
6920 unsigned int rot;
6921 char *l, *r;
6922
6923 if (imm_expr.X_op != O_constant)
6924 as_bad (_("rotate count too large"));
6925 rot = imm_expr.X_add_number & 0x3f;
6926 if (! rot)
6927 break;
6928 r = (rot < 0x20) ? "dsrl" : "dsrl32";
6929 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
6930 rot &= 0x1f;
6931 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
6932 "d,w,<", AT, sreg, rot);
6933 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
6934 "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6935 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6936 "d,v,t", dreg, dreg, AT);
6937 }
6938 break;
6939
252b5132 6940 case M_ROR_I:
771c7ce4
TS
6941 {
6942 unsigned int rot;
6943
6944 if (imm_expr.X_op != O_constant)
6945 as_bad (_("rotate count too large"));
6946 rot = imm_expr.X_add_number & 0x1f;
6947 if (! rot)
6948 break;
6949 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
6950 "d,w,<", AT, sreg, rot);
6951 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
6952 "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6953 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6954 "d,v,t", dreg, dreg, AT);
6955 }
252b5132
RH
6956 break;
6957
6958 case M_S_DOB:
ec68c924 6959 if (mips_arch == CPU_R4650)
252b5132
RH
6960 {
6961 as_bad (_("opcode not supported on this processor"));
6962 return;
6963 }
e7af610e 6964 assert (mips_opts.isa == ISA_MIPS1);
252b5132
RH
6965 /* Even on a big endian machine $fn comes before $fn+1. We have
6966 to adjust when storing to memory. */
6967 macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
6968 target_big_endian ? treg + 1 : treg,
6969 (int) BFD_RELOC_LO16, breg);
6970 offset_expr.X_add_number += 4;
6971 macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
6972 target_big_endian ? treg : treg + 1,
6973 (int) BFD_RELOC_LO16, breg);
6974 return;
6975
6976 case M_SEQ:
6977 if (sreg == 0)
6978 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6979 treg, (int) BFD_RELOC_LO16);
6980 else if (treg == 0)
6981 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6982 sreg, (int) BFD_RELOC_LO16);
6983 else
6984 {
2396cfb9
TS
6985 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6986 "d,v,t", dreg, sreg, treg);
252b5132
RH
6987 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6988 dreg, (int) BFD_RELOC_LO16);
6989 }
6990 return;
6991
6992 case M_SEQ_I:
6993 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6994 {
6995 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6996 sreg, (int) BFD_RELOC_LO16);
6997 return;
6998 }
6999 if (sreg == 0)
7000 {
7001 as_warn (_("Instruction %s: result is always false"),
7002 ip->insn_mo->name);
ea1fb5dc 7003 move_register (&icnt, dreg, 0);
252b5132
RH
7004 return;
7005 }
7006 if (imm_expr.X_op == O_constant
7007 && imm_expr.X_add_number >= 0
7008 && imm_expr.X_add_number < 0x10000)
7009 {
7010 macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg,
7011 sreg, (int) BFD_RELOC_LO16);
7012 used_at = 0;
7013 }
7014 else if (imm_expr.X_op == O_constant
7015 && imm_expr.X_add_number > -0x8000
7016 && imm_expr.X_add_number < 0)
7017 {
7018 imm_expr.X_add_number = -imm_expr.X_add_number;
7019 macro_build ((char *) NULL, &icnt, &imm_expr,
ca4e0257 7020 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
252b5132
RH
7021 "t,r,j", dreg, sreg,
7022 (int) BFD_RELOC_LO16);
7023 used_at = 0;
7024 }
7025 else
7026 {
4d34fb5f 7027 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
7028 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7029 "d,v,t", dreg, sreg, AT);
252b5132
RH
7030 used_at = 1;
7031 }
7032 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
7033 (int) BFD_RELOC_LO16);
7034 if (used_at)
7035 break;
7036 return;
7037
7038 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
7039 s = "slt";
7040 goto sge;
7041 case M_SGEU:
7042 s = "sltu";
7043 sge:
2396cfb9
TS
7044 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7045 dreg, sreg, treg);
252b5132
RH
7046 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7047 (int) BFD_RELOC_LO16);
7048 return;
7049
7050 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
7051 case M_SGEU_I:
7052 if (imm_expr.X_op == O_constant
7053 && imm_expr.X_add_number >= -0x8000
7054 && imm_expr.X_add_number < 0x8000)
7055 {
7056 macro_build ((char *) NULL, &icnt, &imm_expr,
7057 mask == M_SGE_I ? "slti" : "sltiu",
7058 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7059 used_at = 0;
7060 }
7061 else
7062 {
4d34fb5f 7063 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
7064 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7065 mask == M_SGE_I ? "slt" : "sltu", "d,v,t", dreg, sreg,
7066 AT);
252b5132
RH
7067 used_at = 1;
7068 }
7069 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7070 (int) BFD_RELOC_LO16);
7071 if (used_at)
7072 break;
7073 return;
7074
7075 case M_SGT: /* sreg > treg <==> treg < sreg */
7076 s = "slt";
7077 goto sgt;
7078 case M_SGTU:
7079 s = "sltu";
7080 sgt:
2396cfb9
TS
7081 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7082 dreg, treg, sreg);
252b5132
RH
7083 return;
7084
7085 case M_SGT_I: /* sreg > I <==> I < sreg */
7086 s = "slt";
7087 goto sgti;
7088 case M_SGTU_I:
7089 s = "sltu";
7090 sgti:
4d34fb5f 7091 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
7092 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7093 dreg, AT, sreg);
252b5132
RH
7094 break;
7095
2396cfb9 7096 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
252b5132
RH
7097 s = "slt";
7098 goto sle;
7099 case M_SLEU:
7100 s = "sltu";
7101 sle:
2396cfb9
TS
7102 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7103 dreg, treg, sreg);
252b5132
RH
7104 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7105 (int) BFD_RELOC_LO16);
7106 return;
7107
2396cfb9 7108 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
252b5132
RH
7109 s = "slt";
7110 goto slei;
7111 case M_SLEU_I:
7112 s = "sltu";
7113 slei:
4d34fb5f 7114 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
7115 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
7116 dreg, AT, sreg);
252b5132
RH
7117 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
7118 (int) BFD_RELOC_LO16);
7119 break;
7120
7121 case M_SLT_I:
7122 if (imm_expr.X_op == O_constant
7123 && imm_expr.X_add_number >= -0x8000
7124 && imm_expr.X_add_number < 0x8000)
7125 {
7126 macro_build ((char *) NULL, &icnt, &imm_expr, "slti", "t,r,j",
7127 dreg, sreg, (int) BFD_RELOC_LO16);
7128 return;
7129 }
4d34fb5f 7130 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
7131 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
7132 dreg, sreg, AT);
252b5132
RH
7133 break;
7134
7135 case M_SLTU_I:
7136 if (imm_expr.X_op == O_constant
7137 && imm_expr.X_add_number >= -0x8000
7138 && imm_expr.X_add_number < 0x8000)
7139 {
7140 macro_build ((char *) NULL, &icnt, &imm_expr, "sltiu", "t,r,j",
7141 dreg, sreg, (int) BFD_RELOC_LO16);
7142 return;
7143 }
4d34fb5f 7144 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
7145 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7146 "d,v,t", dreg, sreg, AT);
252b5132
RH
7147 break;
7148
7149 case M_SNE:
7150 if (sreg == 0)
2396cfb9
TS
7151 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7152 "d,v,t", dreg, 0, treg);
252b5132 7153 else if (treg == 0)
2396cfb9
TS
7154 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7155 "d,v,t", dreg, 0, sreg);
252b5132
RH
7156 else
7157 {
2396cfb9
TS
7158 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7159 "d,v,t", dreg, sreg, treg);
7160 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7161 "d,v,t", dreg, 0, dreg);
252b5132
RH
7162 }
7163 return;
7164
7165 case M_SNE_I:
7166 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7167 {
2396cfb9
TS
7168 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7169 "d,v,t", dreg, 0, sreg);
252b5132
RH
7170 return;
7171 }
7172 if (sreg == 0)
7173 {
7174 as_warn (_("Instruction %s: result is always true"),
7175 ip->insn_mo->name);
7176 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 7177 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
252b5132
RH
7178 "t,r,j", dreg, 0, (int) BFD_RELOC_LO16);
7179 return;
7180 }
7181 if (imm_expr.X_op == O_constant
7182 && imm_expr.X_add_number >= 0
7183 && imm_expr.X_add_number < 0x10000)
7184 {
7185 macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i",
7186 dreg, sreg, (int) BFD_RELOC_LO16);
7187 used_at = 0;
7188 }
7189 else if (imm_expr.X_op == O_constant
7190 && imm_expr.X_add_number > -0x8000
7191 && imm_expr.X_add_number < 0)
7192 {
7193 imm_expr.X_add_number = -imm_expr.X_add_number;
7194 macro_build ((char *) NULL, &icnt, &imm_expr,
ca4e0257 7195 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
252b5132
RH
7196 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7197 used_at = 0;
7198 }
7199 else
7200 {
4d34fb5f 7201 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
7202 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7203 "d,v,t", dreg, sreg, AT);
252b5132
RH
7204 used_at = 1;
7205 }
2396cfb9
TS
7206 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7207 "d,v,t", dreg, 0, dreg);
252b5132
RH
7208 if (used_at)
7209 break;
7210 return;
7211
7212 case M_DSUB_I:
7213 dbl = 1;
7214 case M_SUB_I:
7215 if (imm_expr.X_op == O_constant
7216 && imm_expr.X_add_number > -0x8000
7217 && imm_expr.X_add_number <= 0x8000)
7218 {
7219 imm_expr.X_add_number = -imm_expr.X_add_number;
7220 macro_build ((char *) NULL, &icnt, &imm_expr,
7221 dbl ? "daddi" : "addi",
7222 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7223 return;
7224 }
7225 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9 7226 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 7227 dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
252b5132
RH
7228 break;
7229
7230 case M_DSUBU_I:
7231 dbl = 1;
7232 case M_SUBU_I:
7233 if (imm_expr.X_op == O_constant
7234 && imm_expr.X_add_number > -0x8000
7235 && imm_expr.X_add_number <= 0x8000)
7236 {
7237 imm_expr.X_add_number = -imm_expr.X_add_number;
7238 macro_build ((char *) NULL, &icnt, &imm_expr,
7239 dbl ? "daddiu" : "addiu",
7240 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7241 return;
7242 }
7243 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9 7244 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 7245 dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
252b5132
RH
7246 break;
7247
7248 case M_TEQ_I:
7249 s = "teq";
7250 goto trap;
7251 case M_TGE_I:
7252 s = "tge";
7253 goto trap;
7254 case M_TGEU_I:
7255 s = "tgeu";
7256 goto trap;
7257 case M_TLT_I:
7258 s = "tlt";
7259 goto trap;
7260 case M_TLTU_I:
7261 s = "tltu";
7262 goto trap;
7263 case M_TNE_I:
7264 s = "tne";
7265 trap:
4d34fb5f 7266 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
7267 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "s,t", sreg,
7268 AT);
252b5132
RH
7269 break;
7270
252b5132 7271 case M_TRUNCWS:
43841e91 7272 case M_TRUNCWD:
e7af610e 7273 assert (mips_opts.isa == ISA_MIPS1);
252b5132
RH
7274 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
7275 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
7276
7277 /*
7278 * Is the double cfc1 instruction a bug in the mips assembler;
7279 * or is there a reason for it?
7280 */
7281 mips_emit_delays (true);
7282 ++mips_opts.noreorder;
7283 mips_any_noreorder = 1;
2396cfb9 7284 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
f9419b05 7285 treg, RA);
2396cfb9 7286 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
f9419b05 7287 treg, RA);
2396cfb9 7288 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
252b5132
RH
7289 expr1.X_add_number = 3;
7290 macro_build ((char *) NULL, &icnt, &expr1, "ori", "t,r,i", AT, treg,
7291 (int) BFD_RELOC_LO16);
7292 expr1.X_add_number = 2;
7293 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", AT, AT,
7294 (int) BFD_RELOC_LO16);
2396cfb9 7295 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
f9419b05 7296 AT, RA);
2396cfb9
TS
7297 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7298 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
252b5132 7299 mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S", dreg, sreg);
2396cfb9 7300 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
f9419b05 7301 treg, RA);
2396cfb9 7302 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
252b5132
RH
7303 --mips_opts.noreorder;
7304 break;
7305
7306 case M_ULH:
7307 s = "lb";
7308 goto ulh;
7309 case M_ULHU:
7310 s = "lbu";
7311 ulh:
7312 if (offset_expr.X_add_number >= 0x7fff)
7313 as_bad (_("operand overflow"));
7314 /* avoid load delay */
7315 if (! target_big_endian)
f9419b05 7316 ++offset_expr.X_add_number;
252b5132
RH
7317 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7318 (int) BFD_RELOC_LO16, breg);
7319 if (! target_big_endian)
f9419b05 7320 --offset_expr.X_add_number;
252b5132 7321 else
f9419b05 7322 ++offset_expr.X_add_number;
252b5132
RH
7323 macro_build ((char *) NULL, &icnt, &offset_expr, "lbu", "t,o(b)", AT,
7324 (int) BFD_RELOC_LO16, breg);
2396cfb9
TS
7325 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7326 treg, treg, 8);
7327 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7328 treg, treg, AT);
252b5132
RH
7329 break;
7330
7331 case M_ULD:
7332 s = "ldl";
7333 s2 = "ldr";
7334 off = 7;
7335 goto ulw;
7336 case M_ULW:
7337 s = "lwl";
7338 s2 = "lwr";
7339 off = 3;
7340 ulw:
7341 if (offset_expr.X_add_number >= 0x8000 - off)
7342 as_bad (_("operand overflow"));
7343 if (! target_big_endian)
7344 offset_expr.X_add_number += off;
7345 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7346 (int) BFD_RELOC_LO16, breg);
7347 if (! target_big_endian)
7348 offset_expr.X_add_number -= off;
7349 else
7350 offset_expr.X_add_number += off;
7351 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7352 (int) BFD_RELOC_LO16, breg);
7353 return;
7354
7355 case M_ULD_A:
7356 s = "ldl";
7357 s2 = "ldr";
7358 off = 7;
7359 goto ulwa;
7360 case M_ULW_A:
7361 s = "lwl";
7362 s2 = "lwr";
7363 off = 3;
7364 ulwa:
d6bc6245 7365 used_at = 1;
c9914766 7366 load_address (&icnt, AT, &offset_expr, &used_at);
252b5132
RH
7367 if (breg != 0)
7368 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 7369 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
7370 "d,v,t", AT, AT, breg);
7371 if (! target_big_endian)
7372 expr1.X_add_number = off;
7373 else
7374 expr1.X_add_number = 0;
7375 macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7376 (int) BFD_RELOC_LO16, AT);
7377 if (! target_big_endian)
7378 expr1.X_add_number = 0;
7379 else
7380 expr1.X_add_number = off;
7381 macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7382 (int) BFD_RELOC_LO16, AT);
7383 break;
7384
7385 case M_ULH_A:
7386 case M_ULHU_A:
d6bc6245 7387 used_at = 1;
c9914766 7388 load_address (&icnt, AT, &offset_expr, &used_at);
252b5132
RH
7389 if (breg != 0)
7390 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 7391 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
7392 "d,v,t", AT, AT, breg);
7393 if (target_big_endian)
7394 expr1.X_add_number = 0;
7395 macro_build ((char *) NULL, &icnt, &expr1,
7396 mask == M_ULH_A ? "lb" : "lbu", "t,o(b)", treg,
7397 (int) BFD_RELOC_LO16, AT);
7398 if (target_big_endian)
7399 expr1.X_add_number = 1;
7400 else
7401 expr1.X_add_number = 0;
7402 macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7403 (int) BFD_RELOC_LO16, AT);
2396cfb9
TS
7404 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7405 treg, treg, 8);
7406 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7407 treg, treg, AT);
252b5132
RH
7408 break;
7409
7410 case M_USH:
7411 if (offset_expr.X_add_number >= 0x7fff)
7412 as_bad (_("operand overflow"));
7413 if (target_big_endian)
f9419b05 7414 ++offset_expr.X_add_number;
252b5132
RH
7415 macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", treg,
7416 (int) BFD_RELOC_LO16, breg);
2396cfb9
TS
7417 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7418 AT, treg, 8);
252b5132 7419 if (target_big_endian)
f9419b05 7420 --offset_expr.X_add_number;
252b5132 7421 else
f9419b05 7422 ++offset_expr.X_add_number;
252b5132
RH
7423 macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", AT,
7424 (int) BFD_RELOC_LO16, breg);
7425 break;
7426
7427 case M_USD:
7428 s = "sdl";
7429 s2 = "sdr";
7430 off = 7;
7431 goto usw;
7432 case M_USW:
7433 s = "swl";
7434 s2 = "swr";
7435 off = 3;
7436 usw:
7437 if (offset_expr.X_add_number >= 0x8000 - off)
7438 as_bad (_("operand overflow"));
7439 if (! target_big_endian)
7440 offset_expr.X_add_number += off;
7441 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7442 (int) BFD_RELOC_LO16, breg);
7443 if (! target_big_endian)
7444 offset_expr.X_add_number -= off;
7445 else
7446 offset_expr.X_add_number += off;
7447 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7448 (int) BFD_RELOC_LO16, breg);
7449 return;
7450
7451 case M_USD_A:
7452 s = "sdl";
7453 s2 = "sdr";
7454 off = 7;
7455 goto uswa;
7456 case M_USW_A:
7457 s = "swl";
7458 s2 = "swr";
7459 off = 3;
7460 uswa:
d6bc6245 7461 used_at = 1;
c9914766 7462 load_address (&icnt, AT, &offset_expr, &used_at);
252b5132
RH
7463 if (breg != 0)
7464 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 7465 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
7466 "d,v,t", AT, AT, breg);
7467 if (! target_big_endian)
7468 expr1.X_add_number = off;
7469 else
7470 expr1.X_add_number = 0;
7471 macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7472 (int) BFD_RELOC_LO16, AT);
7473 if (! target_big_endian)
7474 expr1.X_add_number = 0;
7475 else
7476 expr1.X_add_number = off;
7477 macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7478 (int) BFD_RELOC_LO16, AT);
7479 break;
7480
7481 case M_USH_A:
d6bc6245 7482 used_at = 1;
c9914766 7483 load_address (&icnt, AT, &offset_expr, &used_at);
252b5132
RH
7484 if (breg != 0)
7485 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 7486 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
7487 "d,v,t", AT, AT, breg);
7488 if (! target_big_endian)
7489 expr1.X_add_number = 0;
7490 macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7491 (int) BFD_RELOC_LO16, AT);
2396cfb9
TS
7492 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7493 treg, treg, 8);
252b5132
RH
7494 if (! target_big_endian)
7495 expr1.X_add_number = 1;
7496 else
7497 expr1.X_add_number = 0;
7498 macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7499 (int) BFD_RELOC_LO16, AT);
7500 if (! target_big_endian)
7501 expr1.X_add_number = 0;
7502 else
7503 expr1.X_add_number = 1;
7504 macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7505 (int) BFD_RELOC_LO16, AT);
2396cfb9
TS
7506 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7507 treg, treg, 8);
7508 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7509 treg, treg, AT);
252b5132
RH
7510 break;
7511
7512 default:
7513 /* FIXME: Check if this is one of the itbl macros, since they
bdaaa2e1 7514 are added dynamically. */
252b5132
RH
7515 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7516 break;
7517 }
7518 if (mips_opts.noat)
7519 as_warn (_("Macro used $at after \".set noat\""));
7520}
7521
7522/* Implement macros in mips16 mode. */
7523
7524static void
7525mips16_macro (ip)
7526 struct mips_cl_insn *ip;
7527{
7528 int mask;
7529 int xreg, yreg, zreg, tmp;
7530 int icnt;
7531 expressionS expr1;
7532 int dbl;
7533 const char *s, *s2, *s3;
7534
7535 mask = ip->insn_mo->mask;
7536
7537 xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
7538 yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
7539 zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
7540
7541 icnt = 0;
7542
7543 expr1.X_op = O_constant;
7544 expr1.X_op_symbol = NULL;
7545 expr1.X_add_symbol = NULL;
7546 expr1.X_add_number = 1;
7547
7548 dbl = 0;
7549
7550 switch (mask)
7551 {
7552 default:
7553 internalError ();
7554
7555 case M_DDIV_3:
7556 dbl = 1;
7557 case M_DIV_3:
7558 s = "mflo";
7559 goto do_div3;
7560 case M_DREM_3:
7561 dbl = 1;
7562 case M_REM_3:
7563 s = "mfhi";
7564 do_div3:
7565 mips_emit_delays (true);
7566 ++mips_opts.noreorder;
7567 mips_any_noreorder = 1;
2396cfb9 7568 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
252b5132
RH
7569 dbl ? "ddiv" : "div",
7570 "0,x,y", xreg, yreg);
7571 expr1.X_add_number = 2;
7572 macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
2396cfb9
TS
7573 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break", "6",
7574 7);
bdaaa2e1 7575
252b5132
RH
7576 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7577 since that causes an overflow. We should do that as well,
7578 but I don't see how to do the comparisons without a temporary
7579 register. */
7580 --mips_opts.noreorder;
2396cfb9 7581 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x", zreg);
252b5132
RH
7582 break;
7583
7584 case M_DIVU_3:
7585 s = "divu";
7586 s2 = "mflo";
7587 goto do_divu3;
7588 case M_REMU_3:
7589 s = "divu";
7590 s2 = "mfhi";
7591 goto do_divu3;
7592 case M_DDIVU_3:
7593 s = "ddivu";
7594 s2 = "mflo";
7595 goto do_divu3;
7596 case M_DREMU_3:
7597 s = "ddivu";
7598 s2 = "mfhi";
7599 do_divu3:
7600 mips_emit_delays (true);
7601 ++mips_opts.noreorder;
7602 mips_any_noreorder = 1;
2396cfb9
TS
7603 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "0,x,y",
7604 xreg, yreg);
252b5132
RH
7605 expr1.X_add_number = 2;
7606 macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
98d3f06f
KH
7607 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
7608 "6", 7);
252b5132 7609 --mips_opts.noreorder;
2396cfb9 7610 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "x", zreg);
252b5132
RH
7611 break;
7612
7613 case M_DMUL:
7614 dbl = 1;
7615 case M_MUL:
2396cfb9 7616 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 7617 dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
2396cfb9
TS
7618 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "x",
7619 zreg);
252b5132
RH
7620 return;
7621
7622 case M_DSUBU_I:
7623 dbl = 1;
7624 goto do_subu;
7625 case M_SUBU_I:
7626 do_subu:
7627 if (imm_expr.X_op != O_constant)
7628 as_bad (_("Unsupported large constant"));
7629 imm_expr.X_add_number = -imm_expr.X_add_number;
7630 macro_build ((char *) NULL, &icnt, &imm_expr,
9a41af64 7631 dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
252b5132
RH
7632 break;
7633
7634 case M_SUBU_I_2:
7635 if (imm_expr.X_op != O_constant)
7636 as_bad (_("Unsupported large constant"));
7637 imm_expr.X_add_number = -imm_expr.X_add_number;
7638 macro_build ((char *) NULL, &icnt, &imm_expr, "addiu",
7639 "x,k", xreg);
7640 break;
7641
7642 case M_DSUBU_I_2:
7643 if (imm_expr.X_op != O_constant)
7644 as_bad (_("Unsupported large constant"));
7645 imm_expr.X_add_number = -imm_expr.X_add_number;
7646 macro_build ((char *) NULL, &icnt, &imm_expr, "daddiu",
7647 "y,j", yreg);
7648 break;
7649
7650 case M_BEQ:
7651 s = "cmp";
7652 s2 = "bteqz";
7653 goto do_branch;
7654 case M_BNE:
7655 s = "cmp";
7656 s2 = "btnez";
7657 goto do_branch;
7658 case M_BLT:
7659 s = "slt";
7660 s2 = "btnez";
7661 goto do_branch;
7662 case M_BLTU:
7663 s = "sltu";
7664 s2 = "btnez";
7665 goto do_branch;
7666 case M_BLE:
7667 s = "slt";
7668 s2 = "bteqz";
7669 goto do_reverse_branch;
7670 case M_BLEU:
7671 s = "sltu";
7672 s2 = "bteqz";
7673 goto do_reverse_branch;
7674 case M_BGE:
7675 s = "slt";
7676 s2 = "bteqz";
7677 goto do_branch;
7678 case M_BGEU:
7679 s = "sltu";
7680 s2 = "bteqz";
7681 goto do_branch;
7682 case M_BGT:
7683 s = "slt";
7684 s2 = "btnez";
7685 goto do_reverse_branch;
7686 case M_BGTU:
7687 s = "sltu";
7688 s2 = "btnez";
7689
7690 do_reverse_branch:
7691 tmp = xreg;
7692 xreg = yreg;
7693 yreg = tmp;
7694
7695 do_branch:
7696 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x,y",
7697 xreg, yreg);
7698 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7699 break;
7700
7701 case M_BEQ_I:
7702 s = "cmpi";
7703 s2 = "bteqz";
7704 s3 = "x,U";
7705 goto do_branch_i;
7706 case M_BNE_I:
7707 s = "cmpi";
7708 s2 = "btnez";
7709 s3 = "x,U";
7710 goto do_branch_i;
7711 case M_BLT_I:
7712 s = "slti";
7713 s2 = "btnez";
7714 s3 = "x,8";
7715 goto do_branch_i;
7716 case M_BLTU_I:
7717 s = "sltiu";
7718 s2 = "btnez";
7719 s3 = "x,8";
7720 goto do_branch_i;
7721 case M_BLE_I:
7722 s = "slti";
7723 s2 = "btnez";
7724 s3 = "x,8";
7725 goto do_addone_branch_i;
7726 case M_BLEU_I:
7727 s = "sltiu";
7728 s2 = "btnez";
7729 s3 = "x,8";
7730 goto do_addone_branch_i;
7731 case M_BGE_I:
7732 s = "slti";
7733 s2 = "bteqz";
7734 s3 = "x,8";
7735 goto do_branch_i;
7736 case M_BGEU_I:
7737 s = "sltiu";
7738 s2 = "bteqz";
7739 s3 = "x,8";
7740 goto do_branch_i;
7741 case M_BGT_I:
7742 s = "slti";
7743 s2 = "bteqz";
7744 s3 = "x,8";
7745 goto do_addone_branch_i;
7746 case M_BGTU_I:
7747 s = "sltiu";
7748 s2 = "bteqz";
7749 s3 = "x,8";
7750
7751 do_addone_branch_i:
7752 if (imm_expr.X_op != O_constant)
7753 as_bad (_("Unsupported large constant"));
7754 ++imm_expr.X_add_number;
7755
7756 do_branch_i:
7757 macro_build ((char *) NULL, &icnt, &imm_expr, s, s3, xreg);
7758 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7759 break;
7760
7761 case M_ABS:
7762 expr1.X_add_number = 0;
98d3f06f 7763 macro_build ((char *) NULL, &icnt, &expr1, "slti", "x,8", yreg);
252b5132 7764 if (xreg != yreg)
ea1fb5dc 7765 move_register (&icnt, xreg, yreg);
252b5132
RH
7766 expr1.X_add_number = 2;
7767 macro_build ((char *) NULL, &icnt, &expr1, "bteqz", "p");
7768 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7769 "neg", "x,w", xreg, xreg);
7770 }
7771}
7772
7773/* For consistency checking, verify that all bits are specified either
7774 by the match/mask part of the instruction definition, or by the
7775 operand list. */
7776static int
7777validate_mips_insn (opc)
7778 const struct mips_opcode *opc;
7779{
7780 const char *p = opc->args;
7781 char c;
7782 unsigned long used_bits = opc->mask;
7783
7784 if ((used_bits & opc->match) != opc->match)
7785 {
7786 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
7787 opc->name, opc->args);
7788 return 0;
7789 }
7790#define USE_BITS(mask,shift) (used_bits |= ((mask) << (shift)))
7791 while (*p)
7792 switch (c = *p++)
7793 {
7794 case ',': break;
7795 case '(': break;
7796 case ')': break;
7797 case '<': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7798 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7799 case 'A': break;
4372b673 7800 case 'B': USE_BITS (OP_MASK_CODE20, OP_SH_CODE20); break;
252b5132
RH
7801 case 'C': USE_BITS (OP_MASK_COPZ, OP_SH_COPZ); break;
7802 case 'D': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
7803 case 'E': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7804 case 'F': break;
7805 case 'G': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
156c2f8b 7806 case 'H': USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
252b5132 7807 case 'I': break;
e972090a 7808 case 'J': USE_BITS (OP_MASK_CODE19, OP_SH_CODE19); break;
252b5132
RH
7809 case 'L': break;
7810 case 'M': USE_BITS (OP_MASK_CCC, OP_SH_CCC); break;
7811 case 'N': USE_BITS (OP_MASK_BCC, OP_SH_BCC); break;
deec1734
CD
7812 case 'O': USE_BITS (OP_MASK_ALN, OP_SH_ALN); break;
7813 case 'Q': USE_BITS (OP_MASK_VSEL, OP_SH_VSEL);
7814 USE_BITS (OP_MASK_FT, OP_SH_FT); break;
252b5132
RH
7815 case 'R': USE_BITS (OP_MASK_FR, OP_SH_FR); break;
7816 case 'S': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7817 case 'T': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7818 case 'V': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7819 case 'W': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
deec1734
CD
7820 case 'X': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
7821 case 'Y': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7822 case 'Z': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
252b5132
RH
7823 case 'a': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break;
7824 case 'b': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7825 case 'c': USE_BITS (OP_MASK_CODE, OP_SH_CODE); break;
7826 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
7827 case 'f': break;
7828 case 'h': USE_BITS (OP_MASK_PREFX, OP_SH_PREFX); break;
7829 case 'i': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
7830 case 'j': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7831 case 'k': USE_BITS (OP_MASK_CACHE, OP_SH_CACHE); break;
7832 case 'l': break;
7833 case 'o': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7834 case 'p': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7835 case 'q': USE_BITS (OP_MASK_CODE2, OP_SH_CODE2); break;
7836 case 'r': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7837 case 's': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7838 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7839 case 'u': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
7840 case 'v': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7841 case 'w': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7842 case 'x': break;
7843 case 'z': break;
7844 case 'P': USE_BITS (OP_MASK_PERFREG, OP_SH_PERFREG); break;
4372b673
NC
7845 case 'U': USE_BITS (OP_MASK_RD, OP_SH_RD);
7846 USE_BITS (OP_MASK_RT, OP_SH_RT); break;
252b5132
RH
7847 default:
7848 as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
7849 c, opc->name, opc->args);
7850 return 0;
7851 }
7852#undef USE_BITS
7853 if (used_bits != 0xffffffff)
7854 {
7855 as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
7856 ~used_bits & 0xffffffff, opc->name, opc->args);
7857 return 0;
7858 }
7859 return 1;
7860}
7861
7862/* This routine assembles an instruction into its binary format. As a
7863 side effect, it sets one of the global variables imm_reloc or
7864 offset_reloc to the type of relocation to do if one of the operands
7865 is an address expression. */
7866
7867static void
7868mips_ip (str, ip)
7869 char *str;
7870 struct mips_cl_insn *ip;
7871{
7872 char *s;
7873 const char *args;
43841e91 7874 char c = 0;
252b5132
RH
7875 struct mips_opcode *insn;
7876 char *argsStart;
7877 unsigned int regno;
7878 unsigned int lastregno = 0;
7879 char *s_reset;
7880 char save_c = 0;
252b5132
RH
7881
7882 insn_error = NULL;
7883
7884 /* If the instruction contains a '.', we first try to match an instruction
7885 including the '.'. Then we try again without the '.'. */
7886 insn = NULL;
3882b010 7887 for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
252b5132
RH
7888 continue;
7889
7890 /* If we stopped on whitespace, then replace the whitespace with null for
7891 the call to hash_find. Save the character we replaced just in case we
7892 have to re-parse the instruction. */
3882b010 7893 if (ISSPACE (*s))
252b5132
RH
7894 {
7895 save_c = *s;
7896 *s++ = '\0';
7897 }
bdaaa2e1 7898
252b5132
RH
7899 insn = (struct mips_opcode *) hash_find (op_hash, str);
7900
7901 /* If we didn't find the instruction in the opcode table, try again, but
7902 this time with just the instruction up to, but not including the
7903 first '.'. */
7904 if (insn == NULL)
7905 {
bdaaa2e1 7906 /* Restore the character we overwrite above (if any). */
252b5132
RH
7907 if (save_c)
7908 *(--s) = save_c;
7909
7910 /* Scan up to the first '.' or whitespace. */
3882b010
L
7911 for (s = str;
7912 *s != '\0' && *s != '.' && !ISSPACE (*s);
7913 ++s)
252b5132
RH
7914 continue;
7915
7916 /* If we did not find a '.', then we can quit now. */
7917 if (*s != '.')
7918 {
7919 insn_error = "unrecognized opcode";
7920 return;
7921 }
7922
7923 /* Lookup the instruction in the hash table. */
7924 *s++ = '\0';
7925 if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
7926 {
7927 insn_error = "unrecognized opcode";
7928 return;
7929 }
252b5132
RH
7930 }
7931
7932 argsStart = s;
7933 for (;;)
7934 {
252b5132
RH
7935 boolean ok;
7936
7937 assert (strcmp (insn->name, str) == 0);
7938
1f25f5d3
CD
7939 if (OPCODE_IS_MEMBER (insn,
7940 (mips_opts.isa
deec1734 7941 | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
98d3f06f 7942 | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
1f25f5d3 7943 mips_arch))
252b5132 7944 ok = true;
bdaaa2e1 7945 else
252b5132 7946 ok = false;
bdaaa2e1 7947
252b5132
RH
7948 if (insn->pinfo != INSN_MACRO)
7949 {
ec68c924 7950 if (mips_arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
252b5132
RH
7951 ok = false;
7952 }
7953
7954 if (! ok)
7955 {
7956 if (insn + 1 < &mips_opcodes[NUMOPCODES]
7957 && strcmp (insn->name, insn[1].name) == 0)
7958 {
7959 ++insn;
7960 continue;
7961 }
252b5132 7962 else
beae10d5 7963 {
268f6bed
L
7964 if (!insn_error)
7965 {
7966 static char buf[100];
7967 sprintf (buf,
7968 _("opcode not supported on this processor: %s (%s)"),
ec68c924 7969 mips_cpu_to_str (mips_arch),
268f6bed 7970 mips_isa_to_str (mips_opts.isa));
bdaaa2e1 7971
268f6bed
L
7972 insn_error = buf;
7973 }
7974 if (save_c)
7975 *(--s) = save_c;
2bd7f1f3 7976 return;
252b5132 7977 }
252b5132
RH
7978 }
7979
7980 ip->insn_mo = insn;
7981 ip->insn_opcode = insn->match;
268f6bed 7982 insn_error = NULL;
252b5132
RH
7983 for (args = insn->args;; ++args)
7984 {
deec1734
CD
7985 int is_mdmx;
7986
ad8d3bb3 7987 s += strspn (s, " \t");
deec1734 7988 is_mdmx = 0;
252b5132
RH
7989 switch (*args)
7990 {
7991 case '\0': /* end of args */
7992 if (*s == '\0')
7993 return;
7994 break;
7995
7996 case ',':
7997 if (*s++ == *args)
7998 continue;
7999 s--;
8000 switch (*++args)
8001 {
8002 case 'r':
8003 case 'v':
38487616 8004 ip->insn_opcode |= lastregno << OP_SH_RS;
252b5132
RH
8005 continue;
8006
8007 case 'w':
38487616
TS
8008 ip->insn_opcode |= lastregno << OP_SH_RT;
8009 continue;
8010
252b5132 8011 case 'W':
38487616 8012 ip->insn_opcode |= lastregno << OP_SH_FT;
252b5132
RH
8013 continue;
8014
8015 case 'V':
38487616 8016 ip->insn_opcode |= lastregno << OP_SH_FS;
252b5132
RH
8017 continue;
8018 }
8019 break;
8020
8021 case '(':
8022 /* Handle optional base register.
8023 Either the base register is omitted or
bdaaa2e1 8024 we must have a left paren. */
252b5132
RH
8025 /* This is dependent on the next operand specifier
8026 is a base register specification. */
8027 assert (args[1] == 'b' || args[1] == '5'
8028 || args[1] == '-' || args[1] == '4');
8029 if (*s == '\0')
8030 return;
8031
8032 case ')': /* these must match exactly */
8033 if (*s++ == *args)
8034 continue;
8035 break;
8036
8037 case '<': /* must be at least one digit */
8038 /*
8039 * According to the manual, if the shift amount is greater
b6ff326e
KH
8040 * than 31 or less than 0, then the shift amount should be
8041 * mod 32. In reality the mips assembler issues an error.
252b5132
RH
8042 * We issue a warning and mask out all but the low 5 bits.
8043 */
8044 my_getExpression (&imm_expr, s);
8045 check_absolute_expr (ip, &imm_expr);
8046 if ((unsigned long) imm_expr.X_add_number > 31)
8047 {
793b27f4
TS
8048 as_warn (_("Improper shift amount (%lu)"),
8049 (unsigned long) imm_expr.X_add_number);
38487616 8050 imm_expr.X_add_number &= OP_MASK_SHAMT;
252b5132 8051 }
38487616 8052 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SHAMT;
252b5132
RH
8053 imm_expr.X_op = O_absent;
8054 s = expr_end;
8055 continue;
8056
8057 case '>': /* shift amount minus 32 */
8058 my_getExpression (&imm_expr, s);
8059 check_absolute_expr (ip, &imm_expr);
8060 if ((unsigned long) imm_expr.X_add_number < 32
8061 || (unsigned long) imm_expr.X_add_number > 63)
8062 break;
38487616 8063 ip->insn_opcode |= (imm_expr.X_add_number - 32) << OP_SH_SHAMT;
252b5132
RH
8064 imm_expr.X_op = O_absent;
8065 s = expr_end;
8066 continue;
8067
252b5132
RH
8068 case 'k': /* cache code */
8069 case 'h': /* prefx code */
8070 my_getExpression (&imm_expr, s);
8071 check_absolute_expr (ip, &imm_expr);
8072 if ((unsigned long) imm_expr.X_add_number > 31)
8073 {
8074 as_warn (_("Invalid value for `%s' (%lu)"),
8075 ip->insn_mo->name,
8076 (unsigned long) imm_expr.X_add_number);
8077 imm_expr.X_add_number &= 0x1f;
8078 }
8079 if (*args == 'k')
8080 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
8081 else
8082 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
8083 imm_expr.X_op = O_absent;
8084 s = expr_end;
8085 continue;
8086
8087 case 'c': /* break code */
8088 my_getExpression (&imm_expr, s);
8089 check_absolute_expr (ip, &imm_expr);
793b27f4 8090 if ((unsigned long) imm_expr.X_add_number > 1023)
252b5132 8091 {
793b27f4
TS
8092 as_warn (_("Illegal break code (%lu)"),
8093 (unsigned long) imm_expr.X_add_number);
38487616 8094 imm_expr.X_add_number &= OP_MASK_CODE;
252b5132 8095 }
38487616 8096 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE;
252b5132
RH
8097 imm_expr.X_op = O_absent;
8098 s = expr_end;
8099 continue;
8100
8101 case 'q': /* lower break code */
8102 my_getExpression (&imm_expr, s);
8103 check_absolute_expr (ip, &imm_expr);
793b27f4 8104 if ((unsigned long) imm_expr.X_add_number > 1023)
252b5132 8105 {
793b27f4
TS
8106 as_warn (_("Illegal lower break code (%lu)"),
8107 (unsigned long) imm_expr.X_add_number);
38487616 8108 imm_expr.X_add_number &= OP_MASK_CODE2;
252b5132 8109 }
38487616 8110 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE2;
252b5132
RH
8111 imm_expr.X_op = O_absent;
8112 s = expr_end;
8113 continue;
8114
4372b673 8115 case 'B': /* 20-bit syscall/break code. */
156c2f8b 8116 my_getExpression (&imm_expr, s);
156c2f8b 8117 check_absolute_expr (ip, &imm_expr);
793b27f4
TS
8118 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
8119 as_warn (_("Illegal 20-bit code (%lu)"),
8120 (unsigned long) imm_expr.X_add_number);
38487616 8121 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE20;
252b5132
RH
8122 imm_expr.X_op = O_absent;
8123 s = expr_end;
8124 continue;
8125
98d3f06f 8126 case 'C': /* Coprocessor code */
beae10d5 8127 my_getExpression (&imm_expr, s);
252b5132 8128 check_absolute_expr (ip, &imm_expr);
98d3f06f 8129 if ((unsigned long) imm_expr.X_add_number >= (1 << 25))
252b5132 8130 {
793b27f4
TS
8131 as_warn (_("Coproccesor code > 25 bits (%lu)"),
8132 (unsigned long) imm_expr.X_add_number);
98d3f06f 8133 imm_expr.X_add_number &= ((1 << 25) - 1);
252b5132 8134 }
beae10d5
KH
8135 ip->insn_opcode |= imm_expr.X_add_number;
8136 imm_expr.X_op = O_absent;
8137 s = expr_end;
8138 continue;
252b5132 8139
4372b673
NC
8140 case 'J': /* 19-bit wait code. */
8141 my_getExpression (&imm_expr, s);
8142 check_absolute_expr (ip, &imm_expr);
793b27f4
TS
8143 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
8144 as_warn (_("Illegal 19-bit code (%lu)"),
8145 (unsigned long) imm_expr.X_add_number);
38487616 8146 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE19;
4372b673
NC
8147 imm_expr.X_op = O_absent;
8148 s = expr_end;
8149 continue;
8150
252b5132 8151 case 'P': /* Performance register */
beae10d5 8152 my_getExpression (&imm_expr, s);
252b5132 8153 check_absolute_expr (ip, &imm_expr);
beae10d5 8154 if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
252b5132 8155 {
793b27f4
TS
8156 as_warn (_("Invalid performance register (%lu)"),
8157 (unsigned long) imm_expr.X_add_number);
38487616 8158 imm_expr.X_add_number &= OP_MASK_PERFREG;
252b5132 8159 }
38487616 8160 ip->insn_opcode |= (imm_expr.X_add_number << OP_SH_PERFREG);
beae10d5
KH
8161 imm_expr.X_op = O_absent;
8162 s = expr_end;
8163 continue;
252b5132
RH
8164
8165 case 'b': /* base register */
8166 case 'd': /* destination register */
8167 case 's': /* source register */
8168 case 't': /* target register */
8169 case 'r': /* both target and source */
8170 case 'v': /* both dest and source */
8171 case 'w': /* both dest and target */
8172 case 'E': /* coprocessor target register */
8173 case 'G': /* coprocessor destination register */
8174 case 'x': /* ignore register name */
8175 case 'z': /* must be zero register */
4372b673 8176 case 'U': /* destination register (clo/clz). */
252b5132
RH
8177 s_reset = s;
8178 if (s[0] == '$')
8179 {
8180
3882b010 8181 if (ISDIGIT (s[1]))
252b5132
RH
8182 {
8183 ++s;
8184 regno = 0;
8185 do
8186 {
8187 regno *= 10;
8188 regno += *s - '0';
8189 ++s;
8190 }
3882b010 8191 while (ISDIGIT (*s));
252b5132
RH
8192 if (regno > 31)
8193 as_bad (_("Invalid register number (%d)"), regno);
8194 }
8195 else if (*args == 'E' || *args == 'G')
8196 goto notreg;
8197 else
8198 {
76db943d
TS
8199 if (s[1] == 'r' && s[2] == 'a')
8200 {
8201 s += 3;
8202 regno = RA;
8203 }
8204 else if (s[1] == 'f' && s[2] == 'p')
252b5132
RH
8205 {
8206 s += 3;
8207 regno = FP;
8208 }
8209 else if (s[1] == 's' && s[2] == 'p')
8210 {
8211 s += 3;
8212 regno = SP;
8213 }
8214 else if (s[1] == 'g' && s[2] == 'p')
8215 {
8216 s += 3;
8217 regno = GP;
8218 }
8219 else if (s[1] == 'a' && s[2] == 't')
8220 {
8221 s += 3;
8222 regno = AT;
8223 }
8224 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8225 {
8226 s += 4;
8227 regno = KT0;
8228 }
8229 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8230 {
8231 s += 4;
8232 regno = KT1;
8233 }
8234 else if (itbl_have_entries)
8235 {
8236 char *p, *n;
d7ba4a77 8237 unsigned long r;
252b5132 8238
d7ba4a77 8239 p = s + 1; /* advance past '$' */
252b5132
RH
8240 n = itbl_get_field (&p); /* n is name */
8241
d7ba4a77
ILT
8242 /* See if this is a register defined in an
8243 itbl entry. */
8244 if (itbl_get_reg_val (n, &r))
252b5132
RH
8245 {
8246 /* Get_field advances to the start of
8247 the next field, so we need to back
d7ba4a77 8248 rack to the end of the last field. */
bdaaa2e1 8249 if (p)
252b5132 8250 s = p - 1;
bdaaa2e1 8251 else
d7ba4a77 8252 s = strchr (s, '\0');
252b5132
RH
8253 regno = r;
8254 }
8255 else
8256 goto notreg;
beae10d5 8257 }
252b5132
RH
8258 else
8259 goto notreg;
8260 }
8261 if (regno == AT
8262 && ! mips_opts.noat
8263 && *args != 'E'
8264 && *args != 'G')
8265 as_warn (_("Used $at without \".set noat\""));
8266 c = *args;
8267 if (*s == ' ')
f9419b05 8268 ++s;
252b5132
RH
8269 if (args[1] != *s)
8270 {
8271 if (c == 'r' || c == 'v' || c == 'w')
8272 {
8273 regno = lastregno;
8274 s = s_reset;
f9419b05 8275 ++args;
252b5132
RH
8276 }
8277 }
8278 /* 'z' only matches $0. */
8279 if (c == 'z' && regno != 0)
8280 break;
8281
bdaaa2e1
KH
8282 /* Now that we have assembled one operand, we use the args string
8283 * to figure out where it goes in the instruction. */
252b5132
RH
8284 switch (c)
8285 {
8286 case 'r':
8287 case 's':
8288 case 'v':
8289 case 'b':
38487616 8290 ip->insn_opcode |= regno << OP_SH_RS;
252b5132
RH
8291 break;
8292 case 'd':
8293 case 'G':
38487616 8294 ip->insn_opcode |= regno << OP_SH_RD;
252b5132 8295 break;
4372b673 8296 case 'U':
38487616
TS
8297 ip->insn_opcode |= regno << OP_SH_RD;
8298 ip->insn_opcode |= regno << OP_SH_RT;
4372b673 8299 break;
252b5132
RH
8300 case 'w':
8301 case 't':
8302 case 'E':
38487616 8303 ip->insn_opcode |= regno << OP_SH_RT;
252b5132
RH
8304 break;
8305 case 'x':
8306 /* This case exists because on the r3000 trunc
8307 expands into a macro which requires a gp
8308 register. On the r6000 or r4000 it is
8309 assembled into a single instruction which
8310 ignores the register. Thus the insn version
8311 is MIPS_ISA2 and uses 'x', and the macro
8312 version is MIPS_ISA1 and uses 't'. */
8313 break;
8314 case 'z':
8315 /* This case is for the div instruction, which
8316 acts differently if the destination argument
8317 is $0. This only matches $0, and is checked
8318 outside the switch. */
8319 break;
8320 case 'D':
8321 /* Itbl operand; not yet implemented. FIXME ?? */
8322 break;
8323 /* What about all other operands like 'i', which
8324 can be specified in the opcode table? */
8325 }
8326 lastregno = regno;
8327 continue;
8328 }
8329 notreg:
8330 switch (*args++)
8331 {
8332 case 'r':
8333 case 'v':
38487616 8334 ip->insn_opcode |= lastregno << OP_SH_RS;
252b5132
RH
8335 continue;
8336 case 'w':
38487616 8337 ip->insn_opcode |= lastregno << OP_SH_RT;
252b5132
RH
8338 continue;
8339 }
8340 break;
8341
deec1734
CD
8342 case 'O': /* MDMX alignment immediate constant. */
8343 my_getExpression (&imm_expr, s);
8344 check_absolute_expr (ip, &imm_expr);
8345 if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
8346 {
8347 as_warn ("Improper align amount (%ld), using low bits",
8348 (long) imm_expr.X_add_number);
8349 imm_expr.X_add_number &= OP_MASK_ALN;
8350 }
8351 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_ALN;
8352 imm_expr.X_op = O_absent;
8353 s = expr_end;
8354 continue;
8355
8356 case 'Q': /* MDMX vector, element sel, or const. */
8357 if (s[0] != '$')
8358 {
8359 /* MDMX Immediate. */
8360 my_getExpression (&imm_expr, s);
8361 check_absolute_expr (ip, &imm_expr);
8362 if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
8363 {
8364 as_warn (_("Invalid MDMX Immediate (%ld)"),
8365 (long) imm_expr.X_add_number);
8366 imm_expr.X_add_number &= OP_MASK_FT;
8367 }
8368 imm_expr.X_add_number &= OP_MASK_FT;
8369 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8370 ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
8371 else
8372 ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
8373 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_FT;
8374 imm_expr.X_op = O_absent;
8375 s = expr_end;
8376 continue;
8377 }
8378 /* Not MDMX Immediate. Fall through. */
8379 case 'X': /* MDMX destination register. */
8380 case 'Y': /* MDMX source register. */
8381 case 'Z': /* MDMX target register. */
8382 is_mdmx = 1;
252b5132
RH
8383 case 'D': /* floating point destination register */
8384 case 'S': /* floating point source register */
8385 case 'T': /* floating point target register */
8386 case 'R': /* floating point source register */
8387 case 'V':
8388 case 'W':
8389 s_reset = s;
deec1734
CD
8390 /* Accept $fN for FP and MDMX register numbers, and in
8391 addition accept $vN for MDMX register numbers. */
8392 if ((s[0] == '$' && s[1] == 'f' && ISDIGIT (s[2]))
8393 || (is_mdmx != 0 && s[0] == '$' && s[1] == 'v'
8394 && ISDIGIT (s[2])))
252b5132
RH
8395 {
8396 s += 2;
8397 regno = 0;
8398 do
8399 {
8400 regno *= 10;
8401 regno += *s - '0';
8402 ++s;
8403 }
3882b010 8404 while (ISDIGIT (*s));
252b5132
RH
8405
8406 if (regno > 31)
8407 as_bad (_("Invalid float register number (%d)"), regno);
8408
8409 if ((regno & 1) != 0
ca4e0257 8410 && HAVE_32BIT_FPRS
252b5132
RH
8411 && ! (strcmp (str, "mtc1") == 0
8412 || strcmp (str, "mfc1") == 0
8413 || strcmp (str, "lwc1") == 0
8414 || strcmp (str, "swc1") == 0
8415 || strcmp (str, "l.s") == 0
8416 || strcmp (str, "s.s") == 0))
8417 as_warn (_("Float register should be even, was %d"),
8418 regno);
8419
8420 c = *args;
8421 if (*s == ' ')
f9419b05 8422 ++s;
252b5132
RH
8423 if (args[1] != *s)
8424 {
8425 if (c == 'V' || c == 'W')
8426 {
8427 regno = lastregno;
8428 s = s_reset;
f9419b05 8429 ++args;
252b5132
RH
8430 }
8431 }
8432 switch (c)
8433 {
8434 case 'D':
deec1734 8435 case 'X':
38487616 8436 ip->insn_opcode |= regno << OP_SH_FD;
252b5132
RH
8437 break;
8438 case 'V':
8439 case 'S':
deec1734 8440 case 'Y':
38487616 8441 ip->insn_opcode |= regno << OP_SH_FS;
252b5132 8442 break;
deec1734
CD
8443 case 'Q':
8444 /* This is like 'Z', but also needs to fix the MDMX
8445 vector/scalar select bits. Note that the
8446 scalar immediate case is handled above. */
8447 if (*s == '[')
8448 {
8449 int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
8450 int max_el = (is_qh ? 3 : 7);
8451 s++;
8452 my_getExpression(&imm_expr, s);
8453 check_absolute_expr (ip, &imm_expr);
8454 s = expr_end;
8455 if (imm_expr.X_add_number > max_el)
8456 as_bad(_("Bad element selector %ld"),
8457 (long) imm_expr.X_add_number);
8458 imm_expr.X_add_number &= max_el;
8459 ip->insn_opcode |= (imm_expr.X_add_number
8460 << (OP_SH_VSEL +
8461 (is_qh ? 2 : 1)));
8462 if (*s != ']')
8463 as_warn(_("Expecting ']' found '%s'"), s);
8464 else
8465 s++;
8466 }
8467 else
8468 {
8469 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8470 ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
8471 << OP_SH_VSEL);
8472 else
8473 ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
8474 OP_SH_VSEL);
8475 }
8476 /* Fall through */
252b5132
RH
8477 case 'W':
8478 case 'T':
deec1734 8479 case 'Z':
38487616 8480 ip->insn_opcode |= regno << OP_SH_FT;
252b5132
RH
8481 break;
8482 case 'R':
38487616 8483 ip->insn_opcode |= regno << OP_SH_FR;
252b5132
RH
8484 break;
8485 }
8486 lastregno = regno;
8487 continue;
8488 }
8489
252b5132
RH
8490 switch (*args++)
8491 {
8492 case 'V':
38487616 8493 ip->insn_opcode |= lastregno << OP_SH_FS;
252b5132
RH
8494 continue;
8495 case 'W':
38487616 8496 ip->insn_opcode |= lastregno << OP_SH_FT;
252b5132
RH
8497 continue;
8498 }
8499 break;
8500
8501 case 'I':
8502 my_getExpression (&imm_expr, s);
8503 if (imm_expr.X_op != O_big
8504 && imm_expr.X_op != O_constant)
8505 insn_error = _("absolute expression required");
8506 s = expr_end;
8507 continue;
8508
8509 case 'A':
8510 my_getExpression (&offset_expr, s);
f6688943 8511 *imm_reloc = BFD_RELOC_32;
252b5132
RH
8512 s = expr_end;
8513 continue;
8514
8515 case 'F':
8516 case 'L':
8517 case 'f':
8518 case 'l':
8519 {
8520 int f64;
ca4e0257 8521 int using_gprs;
252b5132
RH
8522 char *save_in;
8523 char *err;
8524 unsigned char temp[8];
8525 int len;
8526 unsigned int length;
8527 segT seg;
8528 subsegT subseg;
8529 char *p;
8530
8531 /* These only appear as the last operand in an
8532 instruction, and every instruction that accepts
8533 them in any variant accepts them in all variants.
8534 This means we don't have to worry about backing out
8535 any changes if the instruction does not match.
8536
8537 The difference between them is the size of the
8538 floating point constant and where it goes. For 'F'
8539 and 'L' the constant is 64 bits; for 'f' and 'l' it
8540 is 32 bits. Where the constant is placed is based
8541 on how the MIPS assembler does things:
8542 F -- .rdata
8543 L -- .lit8
8544 f -- immediate value
8545 l -- .lit4
8546
8547 The .lit4 and .lit8 sections are only used if
8548 permitted by the -G argument.
8549
8550 When generating embedded PIC code, we use the
8551 .lit8 section but not the .lit4 section (we can do
8552 .lit4 inline easily; we need to put .lit8
8553 somewhere in the data segment, and using .lit8
8554 permits the linker to eventually combine identical
ca4e0257
RS
8555 .lit8 entries).
8556
8557 The code below needs to know whether the target register
8558 is 32 or 64 bits wide. It relies on the fact 'f' and
8559 'F' are used with GPR-based instructions and 'l' and
8560 'L' are used with FPR-based instructions. */
252b5132
RH
8561
8562 f64 = *args == 'F' || *args == 'L';
ca4e0257 8563 using_gprs = *args == 'F' || *args == 'f';
252b5132
RH
8564
8565 save_in = input_line_pointer;
8566 input_line_pointer = s;
8567 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
8568 length = len;
8569 s = input_line_pointer;
8570 input_line_pointer = save_in;
8571 if (err != NULL && *err != '\0')
8572 {
8573 as_bad (_("Bad floating point constant: %s"), err);
8574 memset (temp, '\0', sizeof temp);
8575 length = f64 ? 8 : 4;
8576 }
8577
156c2f8b 8578 assert (length == (unsigned) (f64 ? 8 : 4));
252b5132
RH
8579
8580 if (*args == 'f'
8581 || (*args == 'l'
8582 && (! USE_GLOBAL_POINTER_OPT
8583 || mips_pic == EMBEDDED_PIC
8584 || g_switch_value < 4
8585 || (temp[0] == 0 && temp[1] == 0)
8586 || (temp[2] == 0 && temp[3] == 0))))
8587 {
8588 imm_expr.X_op = O_constant;
8589 if (! target_big_endian)
8590 imm_expr.X_add_number = bfd_getl32 (temp);
8591 else
8592 imm_expr.X_add_number = bfd_getb32 (temp);
8593 }
8594 else if (length > 4
119d663a 8595 && ! mips_disable_float_construction
ca4e0257
RS
8596 /* Constants can only be constructed in GPRs and
8597 copied to FPRs if the GPRs are at least as wide
8598 as the FPRs. Force the constant into memory if
8599 we are using 64-bit FPRs but the GPRs are only
8600 32 bits wide. */
8601 && (using_gprs
8602 || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
252b5132
RH
8603 && ((temp[0] == 0 && temp[1] == 0)
8604 || (temp[2] == 0 && temp[3] == 0))
8605 && ((temp[4] == 0 && temp[5] == 0)
8606 || (temp[6] == 0 && temp[7] == 0)))
8607 {
ca4e0257
RS
8608 /* The value is simple enough to load with a couple of
8609 instructions. If using 32-bit registers, set
8610 imm_expr to the high order 32 bits and offset_expr to
8611 the low order 32 bits. Otherwise, set imm_expr to
8612 the entire 64 bit constant. */
8613 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
252b5132
RH
8614 {
8615 imm_expr.X_op = O_constant;
8616 offset_expr.X_op = O_constant;
8617 if (! target_big_endian)
8618 {
8619 imm_expr.X_add_number = bfd_getl32 (temp + 4);
8620 offset_expr.X_add_number = bfd_getl32 (temp);
8621 }
8622 else
8623 {
8624 imm_expr.X_add_number = bfd_getb32 (temp);
8625 offset_expr.X_add_number = bfd_getb32 (temp + 4);
8626 }
8627 if (offset_expr.X_add_number == 0)
8628 offset_expr.X_op = O_absent;
8629 }
8630 else if (sizeof (imm_expr.X_add_number) > 4)
8631 {
8632 imm_expr.X_op = O_constant;
8633 if (! target_big_endian)
8634 imm_expr.X_add_number = bfd_getl64 (temp);
8635 else
8636 imm_expr.X_add_number = bfd_getb64 (temp);
8637 }
8638 else
8639 {
8640 imm_expr.X_op = O_big;
8641 imm_expr.X_add_number = 4;
8642 if (! target_big_endian)
8643 {
8644 generic_bignum[0] = bfd_getl16 (temp);
8645 generic_bignum[1] = bfd_getl16 (temp + 2);
8646 generic_bignum[2] = bfd_getl16 (temp + 4);
8647 generic_bignum[3] = bfd_getl16 (temp + 6);
8648 }
8649 else
8650 {
8651 generic_bignum[0] = bfd_getb16 (temp + 6);
8652 generic_bignum[1] = bfd_getb16 (temp + 4);
8653 generic_bignum[2] = bfd_getb16 (temp + 2);
8654 generic_bignum[3] = bfd_getb16 (temp);
8655 }
8656 }
8657 }
8658 else
8659 {
8660 const char *newname;
8661 segT new_seg;
8662
8663 /* Switch to the right section. */
8664 seg = now_seg;
8665 subseg = now_subseg;
8666 switch (*args)
8667 {
8668 default: /* unused default case avoids warnings. */
8669 case 'L':
8670 newname = RDATA_SECTION_NAME;
bb2d6cd7
GK
8671 if ((USE_GLOBAL_POINTER_OPT && g_switch_value >= 8)
8672 || mips_pic == EMBEDDED_PIC)
252b5132
RH
8673 newname = ".lit8";
8674 break;
8675 case 'F':
bb2d6cd7
GK
8676 if (mips_pic == EMBEDDED_PIC)
8677 newname = ".lit8";
8678 else
8679 newname = RDATA_SECTION_NAME;
252b5132
RH
8680 break;
8681 case 'l':
8682 assert (!USE_GLOBAL_POINTER_OPT
8683 || g_switch_value >= 4);
8684 newname = ".lit4";
8685 break;
8686 }
8687 new_seg = subseg_new (newname, (subsegT) 0);
8688 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
8689 bfd_set_section_flags (stdoutput, new_seg,
8690 (SEC_ALLOC
8691 | SEC_LOAD
8692 | SEC_READONLY
8693 | SEC_DATA));
8694 frag_align (*args == 'l' ? 2 : 3, 0, 0);
8695 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
8696 && strcmp (TARGET_OS, "elf") != 0)
8697 record_alignment (new_seg, 4);
8698 else
8699 record_alignment (new_seg, *args == 'l' ? 2 : 3);
8700 if (seg == now_seg)
8701 as_bad (_("Can't use floating point insn in this section"));
8702
8703 /* Set the argument to the current address in the
8704 section. */
8705 offset_expr.X_op = O_symbol;
8706 offset_expr.X_add_symbol =
8707 symbol_new ("L0\001", now_seg,
8708 (valueT) frag_now_fix (), frag_now);
8709 offset_expr.X_add_number = 0;
8710
8711 /* Put the floating point number into the section. */
8712 p = frag_more ((int) length);
8713 memcpy (p, temp, length);
8714
8715 /* Switch back to the original section. */
8716 subseg_set (seg, subseg);
8717 }
8718 }
8719 continue;
8720
8721 case 'i': /* 16 bit unsigned immediate */
8722 case 'j': /* 16 bit signed immediate */
f6688943 8723 *imm_reloc = BFD_RELOC_LO16;
252b5132 8724 c = my_getSmallExpression (&imm_expr, s);
fb1b3232 8725 if (c != S_EX_NONE)
252b5132 8726 {
fb1b3232 8727 if (c != S_EX_LO)
252b5132
RH
8728 {
8729 if (imm_expr.X_op == O_constant)
8730 imm_expr.X_add_number =
8731 (imm_expr.X_add_number >> 16) & 0xffff;
ad8d3bb3 8732#ifdef OBJ_ELF
fb1b3232 8733 else if (c == S_EX_HIGHEST)
98d3f06f 8734 *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
fb1b3232 8735 else if (c == S_EX_HIGHER)
98d3f06f 8736 *imm_reloc = BFD_RELOC_MIPS_HIGHER;
ad8d3bb3
TS
8737 else if (c == S_EX_GP_REL)
8738 {
8739 /* This occurs in NewABI only. */
8740 c = my_getSmallExpression (&imm_expr, s);
8741 if (c != S_EX_NEG)
8742 as_bad (_("bad composition of relocations"));
8743 else
8744 {
8745 c = my_getSmallExpression (&imm_expr, s);
8746 if (c != S_EX_LO)
8747 as_bad (_("bad composition of relocations"));
8748 else
8749 {
8750 imm_reloc[0] = BFD_RELOC_GPREL16;
8751 imm_reloc[1] = BFD_RELOC_MIPS_SUB;
8752 imm_reloc[2] = BFD_RELOC_LO16;
8753 }
8754 }
8755 }
8756#endif
fb1b3232 8757 else if (c == S_EX_HI)
252b5132 8758 {
f6688943 8759 *imm_reloc = BFD_RELOC_HI16_S;
252b5132
RH
8760 imm_unmatched_hi = true;
8761 }
8762 else
f6688943 8763 *imm_reloc = BFD_RELOC_HI16;
252b5132
RH
8764 }
8765 else if (imm_expr.X_op == O_constant)
8766 imm_expr.X_add_number &= 0xffff;
8767 }
8768 if (*args == 'i')
8769 {
fb1b3232 8770 if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
252b5132 8771 || ((imm_expr.X_add_number < 0
beae10d5
KH
8772 || imm_expr.X_add_number >= 0x10000)
8773 && imm_expr.X_op == O_constant))
252b5132
RH
8774 {
8775 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8776 !strcmp (insn->name, insn[1].name))
8777 break;
2ae7e77b
AH
8778 if (imm_expr.X_op == O_constant
8779 || imm_expr.X_op == O_big)
252b5132
RH
8780 as_bad (_("16 bit expression not in range 0..65535"));
8781 }
8782 }
8783 else
8784 {
8785 int more;
8786 offsetT max;
8787
8788 /* The upper bound should be 0x8000, but
8789 unfortunately the MIPS assembler accepts numbers
8790 from 0x8000 to 0xffff and sign extends them, and
8791 we want to be compatible. We only permit this
8792 extended range for an instruction which does not
8793 provide any further alternates, since those
8794 alternates may handle other cases. People should
8795 use the numbers they mean, rather than relying on
8796 a mysterious sign extension. */
8797 more = (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8798 strcmp (insn->name, insn[1].name) == 0);
8799 if (more)
8800 max = 0x8000;
8801 else
8802 max = 0x10000;
fb1b3232 8803 if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
252b5132 8804 || ((imm_expr.X_add_number < -0x8000
beae10d5
KH
8805 || imm_expr.X_add_number >= max)
8806 && imm_expr.X_op == O_constant)
252b5132
RH
8807 || (more
8808 && imm_expr.X_add_number < 0
ca4e0257 8809 && HAVE_64BIT_GPRS
252b5132
RH
8810 && imm_expr.X_unsigned
8811 && sizeof (imm_expr.X_add_number) <= 4))
8812 {
8813 if (more)
8814 break;
2ae7e77b
AH
8815 if (imm_expr.X_op == O_constant
8816 || imm_expr.X_op == O_big)
252b5132
RH
8817 as_bad (_("16 bit expression not in range -32768..32767"));
8818 }
8819 }
8820 s = expr_end;
8821 continue;
8822
8823 case 'o': /* 16 bit offset */
8824 c = my_getSmallExpression (&offset_expr, s);
8825
8826 /* If this value won't fit into a 16 bit offset, then go
8827 find a macro that will generate the 32 bit offset
afdbd6d0 8828 code pattern. */
fb1b3232 8829 if (c == S_EX_NONE
252b5132
RH
8830 && (offset_expr.X_op != O_constant
8831 || offset_expr.X_add_number >= 0x8000
afdbd6d0 8832 || offset_expr.X_add_number < -0x8000))
252b5132
RH
8833 break;
8834
fb1b3232 8835 if (c == S_EX_HI)
252b5132
RH
8836 {
8837 if (offset_expr.X_op != O_constant)
8838 break;
8839 offset_expr.X_add_number =
8840 (offset_expr.X_add_number >> 16) & 0xffff;
8841 }
f6688943 8842 *offset_reloc = BFD_RELOC_LO16;
252b5132
RH
8843 s = expr_end;
8844 continue;
8845
8846 case 'p': /* pc relative offset */
cb56d3d3 8847 if (mips_pic == EMBEDDED_PIC)
f6688943 8848 *offset_reloc = BFD_RELOC_16_PCREL_S2;
cb56d3d3 8849 else
f6688943 8850 *offset_reloc = BFD_RELOC_16_PCREL;
252b5132
RH
8851 my_getExpression (&offset_expr, s);
8852 s = expr_end;
8853 continue;
8854
8855 case 'u': /* upper 16 bits */
8856 c = my_getSmallExpression (&imm_expr, s);
f6688943 8857 *imm_reloc = BFD_RELOC_LO16;
e7d556df 8858 if (c != S_EX_NONE)
252b5132 8859 {
fb1b3232 8860 if (c != S_EX_LO)
252b5132
RH
8861 {
8862 if (imm_expr.X_op == O_constant)
8863 imm_expr.X_add_number =
8864 (imm_expr.X_add_number >> 16) & 0xffff;
fb1b3232 8865 else if (c == S_EX_HI)
252b5132 8866 {
f6688943 8867 *imm_reloc = BFD_RELOC_HI16_S;
252b5132
RH
8868 imm_unmatched_hi = true;
8869 }
645dc66c
TS
8870#ifdef OBJ_ELF
8871 else if (c == S_EX_HIGHEST)
98d3f06f 8872 *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
ad8d3bb3
TS
8873 else if (c == S_EX_GP_REL)
8874 {
8875 /* This occurs in NewABI only. */
8876 c = my_getSmallExpression (&imm_expr, s);
8877 if (c != S_EX_NEG)
8878 as_bad (_("bad composition of relocations"));
8879 else
8880 {
8881 c = my_getSmallExpression (&imm_expr, s);
8882 if (c != S_EX_HI)
8883 as_bad (_("bad composition of relocations"));
8884 else
8885 {
8886 imm_reloc[0] = BFD_RELOC_GPREL16;
8887 imm_reloc[1] = BFD_RELOC_MIPS_SUB;
8888 imm_reloc[2] = BFD_RELOC_HI16_S;
8889 }
8890 }
8891 }
8892#endif
252b5132 8893 else
f6688943 8894 *imm_reloc = BFD_RELOC_HI16;
252b5132
RH
8895 }
8896 else if (imm_expr.X_op == O_constant)
8897 imm_expr.X_add_number &= 0xffff;
8898 }
8899 if (imm_expr.X_op == O_constant
8900 && (imm_expr.X_add_number < 0
8901 || imm_expr.X_add_number >= 0x10000))
8902 as_bad (_("lui expression not in range 0..65535"));
8903 s = expr_end;
8904 continue;
8905
8906 case 'a': /* 26 bit address */
8907 my_getExpression (&offset_expr, s);
8908 s = expr_end;
f6688943 8909 *offset_reloc = BFD_RELOC_MIPS_JMP;
252b5132
RH
8910 continue;
8911
8912 case 'N': /* 3 bit branch condition code */
8913 case 'M': /* 3 bit compare condition code */
8914 if (strncmp (s, "$fcc", 4) != 0)
8915 break;
8916 s += 4;
8917 regno = 0;
8918 do
8919 {
8920 regno *= 10;
8921 regno += *s - '0';
8922 ++s;
8923 }
3882b010 8924 while (ISDIGIT (*s));
252b5132
RH
8925 if (regno > 7)
8926 as_bad (_("invalid condition code register $fcc%d"), regno);
8927 if (*args == 'N')
8928 ip->insn_opcode |= regno << OP_SH_BCC;
8929 else
8930 ip->insn_opcode |= regno << OP_SH_CCC;
beae10d5 8931 continue;
252b5132 8932
156c2f8b
NC
8933 case 'H':
8934 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
8935 s += 2;
3882b010 8936 if (ISDIGIT (*s))
156c2f8b
NC
8937 {
8938 c = 0;
8939 do
8940 {
8941 c *= 10;
8942 c += *s - '0';
8943 ++s;
8944 }
3882b010 8945 while (ISDIGIT (*s));
156c2f8b
NC
8946 }
8947 else
8948 c = 8; /* Invalid sel value. */
8949
8950 if (c > 7)
8951 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
8952 ip->insn_opcode |= c;
8953 continue;
8954
252b5132
RH
8955 default:
8956 as_bad (_("bad char = '%c'\n"), *args);
8957 internalError ();
8958 }
8959 break;
8960 }
8961 /* Args don't match. */
8962 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8963 !strcmp (insn->name, insn[1].name))
8964 {
8965 ++insn;
8966 s = argsStart;
268f6bed 8967 insn_error = _("illegal operands");
252b5132
RH
8968 continue;
8969 }
268f6bed
L
8970 if (save_c)
8971 *(--s) = save_c;
252b5132
RH
8972 insn_error = _("illegal operands");
8973 return;
8974 }
8975}
8976
8977/* This routine assembles an instruction into its binary format when
8978 assembling for the mips16. As a side effect, it sets one of the
8979 global variables imm_reloc or offset_reloc to the type of
8980 relocation to do if one of the operands is an address expression.
8981 It also sets mips16_small and mips16_ext if the user explicitly
8982 requested a small or extended instruction. */
8983
8984static void
8985mips16_ip (str, ip)
8986 char *str;
8987 struct mips_cl_insn *ip;
8988{
8989 char *s;
8990 const char *args;
8991 struct mips_opcode *insn;
8992 char *argsstart;
8993 unsigned int regno;
8994 unsigned int lastregno = 0;
8995 char *s_reset;
8996
8997 insn_error = NULL;
8998
8999 mips16_small = false;
9000 mips16_ext = false;
9001
3882b010 9002 for (s = str; ISLOWER (*s); ++s)
252b5132
RH
9003 ;
9004 switch (*s)
9005 {
9006 case '\0':
9007 break;
9008
9009 case ' ':
9010 *s++ = '\0';
9011 break;
9012
9013 case '.':
9014 if (s[1] == 't' && s[2] == ' ')
9015 {
9016 *s = '\0';
9017 mips16_small = true;
9018 s += 3;
9019 break;
9020 }
9021 else if (s[1] == 'e' && s[2] == ' ')
9022 {
9023 *s = '\0';
9024 mips16_ext = true;
9025 s += 3;
9026 break;
9027 }
9028 /* Fall through. */
9029 default:
9030 insn_error = _("unknown opcode");
9031 return;
9032 }
9033
9034 if (mips_opts.noautoextend && ! mips16_ext)
9035 mips16_small = true;
9036
9037 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
9038 {
9039 insn_error = _("unrecognized opcode");
9040 return;
9041 }
9042
9043 argsstart = s;
9044 for (;;)
9045 {
9046 assert (strcmp (insn->name, str) == 0);
9047
9048 ip->insn_mo = insn;
9049 ip->insn_opcode = insn->match;
9050 ip->use_extend = false;
9051 imm_expr.X_op = O_absent;
f6688943
TS
9052 imm_reloc[0] = BFD_RELOC_UNUSED;
9053 imm_reloc[1] = BFD_RELOC_UNUSED;
9054 imm_reloc[2] = BFD_RELOC_UNUSED;
252b5132 9055 offset_expr.X_op = O_absent;
f6688943
TS
9056 offset_reloc[0] = BFD_RELOC_UNUSED;
9057 offset_reloc[1] = BFD_RELOC_UNUSED;
9058 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
9059 for (args = insn->args; 1; ++args)
9060 {
9061 int c;
9062
9063 if (*s == ' ')
9064 ++s;
9065
9066 /* In this switch statement we call break if we did not find
9067 a match, continue if we did find a match, or return if we
9068 are done. */
9069
9070 c = *args;
9071 switch (c)
9072 {
9073 case '\0':
9074 if (*s == '\0')
9075 {
9076 /* Stuff the immediate value in now, if we can. */
9077 if (imm_expr.X_op == O_constant
f6688943 9078 && *imm_reloc > BFD_RELOC_UNUSED
252b5132
RH
9079 && insn->pinfo != INSN_MACRO)
9080 {
c4e7957c 9081 mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
252b5132
RH
9082 imm_expr.X_add_number, true, mips16_small,
9083 mips16_ext, &ip->insn_opcode,
9084 &ip->use_extend, &ip->extend);
9085 imm_expr.X_op = O_absent;
f6688943 9086 *imm_reloc = BFD_RELOC_UNUSED;
252b5132
RH
9087 }
9088
9089 return;
9090 }
9091 break;
9092
9093 case ',':
9094 if (*s++ == c)
9095 continue;
9096 s--;
9097 switch (*++args)
9098 {
9099 case 'v':
9100 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9101 continue;
9102 case 'w':
9103 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9104 continue;
9105 }
9106 break;
9107
9108 case '(':
9109 case ')':
9110 if (*s++ == c)
9111 continue;
9112 break;
9113
9114 case 'v':
9115 case 'w':
9116 if (s[0] != '$')
9117 {
9118 if (c == 'v')
9119 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
9120 else
9121 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
9122 ++args;
9123 continue;
9124 }
9125 /* Fall through. */
9126 case 'x':
9127 case 'y':
9128 case 'z':
9129 case 'Z':
9130 case '0':
9131 case 'S':
9132 case 'R':
9133 case 'X':
9134 case 'Y':
9135 if (s[0] != '$')
9136 break;
9137 s_reset = s;
3882b010 9138 if (ISDIGIT (s[1]))
252b5132
RH
9139 {
9140 ++s;
9141 regno = 0;
9142 do
9143 {
9144 regno *= 10;
9145 regno += *s - '0';
9146 ++s;
9147 }
3882b010 9148 while (ISDIGIT (*s));
252b5132
RH
9149 if (regno > 31)
9150 {
9151 as_bad (_("invalid register number (%d)"), regno);
9152 regno = 2;
9153 }
9154 }
9155 else
9156 {
76db943d
TS
9157 if (s[1] == 'r' && s[2] == 'a')
9158 {
9159 s += 3;
9160 regno = RA;
9161 }
9162 else if (s[1] == 'f' && s[2] == 'p')
252b5132
RH
9163 {
9164 s += 3;
9165 regno = FP;
9166 }
9167 else if (s[1] == 's' && s[2] == 'p')
9168 {
9169 s += 3;
9170 regno = SP;
9171 }
9172 else if (s[1] == 'g' && s[2] == 'p')
9173 {
9174 s += 3;
9175 regno = GP;
9176 }
9177 else if (s[1] == 'a' && s[2] == 't')
9178 {
9179 s += 3;
9180 regno = AT;
9181 }
9182 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
9183 {
9184 s += 4;
9185 regno = KT0;
9186 }
9187 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
9188 {
9189 s += 4;
9190 regno = KT1;
9191 }
9192 else
9193 break;
9194 }
9195
9196 if (*s == ' ')
9197 ++s;
9198 if (args[1] != *s)
9199 {
9200 if (c == 'v' || c == 'w')
9201 {
9202 regno = mips16_to_32_reg_map[lastregno];
9203 s = s_reset;
f9419b05 9204 ++args;
252b5132
RH
9205 }
9206 }
9207
9208 switch (c)
9209 {
9210 case 'x':
9211 case 'y':
9212 case 'z':
9213 case 'v':
9214 case 'w':
9215 case 'Z':
9216 regno = mips32_to_16_reg_map[regno];
9217 break;
9218
9219 case '0':
9220 if (regno != 0)
9221 regno = ILLEGAL_REG;
9222 break;
9223
9224 case 'S':
9225 if (regno != SP)
9226 regno = ILLEGAL_REG;
9227 break;
9228
9229 case 'R':
9230 if (regno != RA)
9231 regno = ILLEGAL_REG;
9232 break;
9233
9234 case 'X':
9235 case 'Y':
9236 if (regno == AT && ! mips_opts.noat)
9237 as_warn (_("used $at without \".set noat\""));
9238 break;
9239
9240 default:
9241 internalError ();
9242 }
9243
9244 if (regno == ILLEGAL_REG)
9245 break;
9246
9247 switch (c)
9248 {
9249 case 'x':
9250 case 'v':
9251 ip->insn_opcode |= regno << MIPS16OP_SH_RX;
9252 break;
9253 case 'y':
9254 case 'w':
9255 ip->insn_opcode |= regno << MIPS16OP_SH_RY;
9256 break;
9257 case 'z':
9258 ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
9259 break;
9260 case 'Z':
9261 ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
9262 case '0':
9263 case 'S':
9264 case 'R':
9265 break;
9266 case 'X':
9267 ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
9268 break;
9269 case 'Y':
9270 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
9271 ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
9272 break;
9273 default:
9274 internalError ();
9275 }
9276
9277 lastregno = regno;
9278 continue;
9279
9280 case 'P':
9281 if (strncmp (s, "$pc", 3) == 0)
9282 {
9283 s += 3;
9284 continue;
9285 }
9286 break;
9287
9288 case '<':
9289 case '>':
9290 case '[':
9291 case ']':
9292 case '4':
9293 case '5':
9294 case 'H':
9295 case 'W':
9296 case 'D':
9297 case 'j':
9298 case '8':
9299 case 'V':
9300 case 'C':
9301 case 'U':
9302 case 'k':
9303 case 'K':
9304 if (s[0] == '%'
9305 && strncmp (s + 1, "gprel(", sizeof "gprel(" - 1) == 0)
9306 {
9307 /* This is %gprel(SYMBOL). We need to read SYMBOL,
9308 and generate the appropriate reloc. If the text
9309 inside %gprel is not a symbol name with an
9310 optional offset, then we generate a normal reloc
9311 and will probably fail later. */
9312 my_getExpression (&imm_expr, s + sizeof "%gprel" - 1);
9313 if (imm_expr.X_op == O_symbol)
9314 {
9315 mips16_ext = true;
f6688943 9316 *imm_reloc = BFD_RELOC_MIPS16_GPREL;
252b5132
RH
9317 s = expr_end;
9318 ip->use_extend = true;
9319 ip->extend = 0;
9320 continue;
9321 }
9322 }
9323 else
9324 {
9325 /* Just pick up a normal expression. */
9326 my_getExpression (&imm_expr, s);
9327 }
9328
9329 if (imm_expr.X_op == O_register)
9330 {
9331 /* What we thought was an expression turned out to
9332 be a register. */
9333
9334 if (s[0] == '(' && args[1] == '(')
9335 {
9336 /* It looks like the expression was omitted
9337 before a register indirection, which means
9338 that the expression is implicitly zero. We
9339 still set up imm_expr, so that we handle
9340 explicit extensions correctly. */
9341 imm_expr.X_op = O_constant;
9342 imm_expr.X_add_number = 0;
f6688943 9343 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
9344 continue;
9345 }
9346
9347 break;
9348 }
9349
9350 /* We need to relax this instruction. */
f6688943 9351 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
9352 s = expr_end;
9353 continue;
9354
9355 case 'p':
9356 case 'q':
9357 case 'A':
9358 case 'B':
9359 case 'E':
9360 /* We use offset_reloc rather than imm_reloc for the PC
9361 relative operands. This lets macros with both
9362 immediate and address operands work correctly. */
9363 my_getExpression (&offset_expr, s);
9364
9365 if (offset_expr.X_op == O_register)
9366 break;
9367
9368 /* We need to relax this instruction. */
f6688943 9369 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
9370 s = expr_end;
9371 continue;
9372
9373 case '6': /* break code */
9374 my_getExpression (&imm_expr, s);
9375 check_absolute_expr (ip, &imm_expr);
9376 if ((unsigned long) imm_expr.X_add_number > 63)
9377 {
9378 as_warn (_("Invalid value for `%s' (%lu)"),
9379 ip->insn_mo->name,
9380 (unsigned long) imm_expr.X_add_number);
9381 imm_expr.X_add_number &= 0x3f;
9382 }
9383 ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
9384 imm_expr.X_op = O_absent;
9385 s = expr_end;
9386 continue;
9387
9388 case 'a': /* 26 bit address */
9389 my_getExpression (&offset_expr, s);
9390 s = expr_end;
f6688943 9391 *offset_reloc = BFD_RELOC_MIPS16_JMP;
252b5132
RH
9392 ip->insn_opcode <<= 16;
9393 continue;
9394
9395 case 'l': /* register list for entry macro */
9396 case 'L': /* register list for exit macro */
9397 {
9398 int mask;
9399
9400 if (c == 'l')
9401 mask = 0;
9402 else
9403 mask = 7 << 3;
9404 while (*s != '\0')
9405 {
9406 int freg, reg1, reg2;
9407
9408 while (*s == ' ' || *s == ',')
9409 ++s;
9410 if (*s != '$')
9411 {
9412 as_bad (_("can't parse register list"));
9413 break;
9414 }
9415 ++s;
9416 if (*s != 'f')
9417 freg = 0;
9418 else
9419 {
9420 freg = 1;
9421 ++s;
9422 }
9423 reg1 = 0;
3882b010 9424 while (ISDIGIT (*s))
252b5132
RH
9425 {
9426 reg1 *= 10;
9427 reg1 += *s - '0';
9428 ++s;
9429 }
9430 if (*s == ' ')
9431 ++s;
9432 if (*s != '-')
9433 reg2 = reg1;
9434 else
9435 {
9436 ++s;
9437 if (*s != '$')
9438 break;
9439 ++s;
9440 if (freg)
9441 {
9442 if (*s == 'f')
9443 ++s;
9444 else
9445 {
9446 as_bad (_("invalid register list"));
9447 break;
9448 }
9449 }
9450 reg2 = 0;
3882b010 9451 while (ISDIGIT (*s))
252b5132
RH
9452 {
9453 reg2 *= 10;
9454 reg2 += *s - '0';
9455 ++s;
9456 }
9457 }
9458 if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
9459 {
9460 mask &= ~ (7 << 3);
9461 mask |= 5 << 3;
9462 }
9463 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
9464 {
9465 mask &= ~ (7 << 3);
9466 mask |= 6 << 3;
9467 }
9468 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
9469 mask |= (reg2 - 3) << 3;
9470 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
9471 mask |= (reg2 - 15) << 1;
f9419b05 9472 else if (reg1 == RA && reg2 == RA)
252b5132
RH
9473 mask |= 1;
9474 else
9475 {
9476 as_bad (_("invalid register list"));
9477 break;
9478 }
9479 }
9480 /* The mask is filled in in the opcode table for the
9481 benefit of the disassembler. We remove it before
9482 applying the actual mask. */
9483 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
9484 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
9485 }
9486 continue;
9487
9488 case 'e': /* extend code */
9489 my_getExpression (&imm_expr, s);
9490 check_absolute_expr (ip, &imm_expr);
9491 if ((unsigned long) imm_expr.X_add_number > 0x7ff)
9492 {
9493 as_warn (_("Invalid value for `%s' (%lu)"),
9494 ip->insn_mo->name,
9495 (unsigned long) imm_expr.X_add_number);
9496 imm_expr.X_add_number &= 0x7ff;
9497 }
9498 ip->insn_opcode |= imm_expr.X_add_number;
9499 imm_expr.X_op = O_absent;
9500 s = expr_end;
9501 continue;
9502
9503 default:
9504 internalError ();
9505 }
9506 break;
9507 }
9508
9509 /* Args don't match. */
9510 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
9511 strcmp (insn->name, insn[1].name) == 0)
9512 {
9513 ++insn;
9514 s = argsstart;
9515 continue;
9516 }
9517
9518 insn_error = _("illegal operands");
9519
9520 return;
9521 }
9522}
9523
9524/* This structure holds information we know about a mips16 immediate
9525 argument type. */
9526
e972090a
NC
9527struct mips16_immed_operand
9528{
252b5132
RH
9529 /* The type code used in the argument string in the opcode table. */
9530 int type;
9531 /* The number of bits in the short form of the opcode. */
9532 int nbits;
9533 /* The number of bits in the extended form of the opcode. */
9534 int extbits;
9535 /* The amount by which the short form is shifted when it is used;
9536 for example, the sw instruction has a shift count of 2. */
9537 int shift;
9538 /* The amount by which the short form is shifted when it is stored
9539 into the instruction code. */
9540 int op_shift;
9541 /* Non-zero if the short form is unsigned. */
9542 int unsp;
9543 /* Non-zero if the extended form is unsigned. */
9544 int extu;
9545 /* Non-zero if the value is PC relative. */
9546 int pcrel;
9547};
9548
9549/* The mips16 immediate operand types. */
9550
9551static const struct mips16_immed_operand mips16_immed_operands[] =
9552{
9553 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9554 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9555 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9556 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9557 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
9558 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
9559 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
9560 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
9561 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
9562 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
9563 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
9564 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
9565 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
9566 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
9567 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
9568 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
9569 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9570 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9571 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
9572 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
9573 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
9574};
9575
9576#define MIPS16_NUM_IMMED \
9577 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
9578
9579/* Handle a mips16 instruction with an immediate value. This or's the
9580 small immediate value into *INSN. It sets *USE_EXTEND to indicate
9581 whether an extended value is needed; if one is needed, it sets
9582 *EXTEND to the value. The argument type is TYPE. The value is VAL.
9583 If SMALL is true, an unextended opcode was explicitly requested.
9584 If EXT is true, an extended opcode was explicitly requested. If
9585 WARN is true, warn if EXT does not match reality. */
9586
9587static void
9588mips16_immed (file, line, type, val, warn, small, ext, insn, use_extend,
9589 extend)
9590 char *file;
9591 unsigned int line;
9592 int type;
9593 offsetT val;
9594 boolean warn;
9595 boolean small;
9596 boolean ext;
9597 unsigned long *insn;
9598 boolean *use_extend;
9599 unsigned short *extend;
9600{
9601 register const struct mips16_immed_operand *op;
9602 int mintiny, maxtiny;
9603 boolean needext;
9604
9605 op = mips16_immed_operands;
9606 while (op->type != type)
9607 {
9608 ++op;
9609 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
9610 }
9611
9612 if (op->unsp)
9613 {
9614 if (type == '<' || type == '>' || type == '[' || type == ']')
9615 {
9616 mintiny = 1;
9617 maxtiny = 1 << op->nbits;
9618 }
9619 else
9620 {
9621 mintiny = 0;
9622 maxtiny = (1 << op->nbits) - 1;
9623 }
9624 }
9625 else
9626 {
9627 mintiny = - (1 << (op->nbits - 1));
9628 maxtiny = (1 << (op->nbits - 1)) - 1;
9629 }
9630
9631 /* Branch offsets have an implicit 0 in the lowest bit. */
9632 if (type == 'p' || type == 'q')
9633 val /= 2;
9634
9635 if ((val & ((1 << op->shift) - 1)) != 0
9636 || val < (mintiny << op->shift)
9637 || val > (maxtiny << op->shift))
9638 needext = true;
9639 else
9640 needext = false;
9641
9642 if (warn && ext && ! needext)
beae10d5
KH
9643 as_warn_where (file, line,
9644 _("extended operand requested but not required"));
252b5132
RH
9645 if (small && needext)
9646 as_bad_where (file, line, _("invalid unextended operand value"));
9647
9648 if (small || (! ext && ! needext))
9649 {
9650 int insnval;
9651
9652 *use_extend = false;
9653 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
9654 insnval <<= op->op_shift;
9655 *insn |= insnval;
9656 }
9657 else
9658 {
9659 long minext, maxext;
9660 int extval;
9661
9662 if (op->extu)
9663 {
9664 minext = 0;
9665 maxext = (1 << op->extbits) - 1;
9666 }
9667 else
9668 {
9669 minext = - (1 << (op->extbits - 1));
9670 maxext = (1 << (op->extbits - 1)) - 1;
9671 }
9672 if (val < minext || val > maxext)
9673 as_bad_where (file, line,
9674 _("operand value out of range for instruction"));
9675
9676 *use_extend = true;
9677 if (op->extbits == 16)
9678 {
9679 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
9680 val &= 0x1f;
9681 }
9682 else if (op->extbits == 15)
9683 {
9684 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
9685 val &= 0xf;
9686 }
9687 else
9688 {
9689 extval = ((val & 0x1f) << 6) | (val & 0x20);
9690 val = 0;
9691 }
9692
9693 *extend = (unsigned short) extval;
9694 *insn |= val;
9695 }
9696}
9697\f
ad8d3bb3
TS
9698static struct percent_op_match
9699{
9700 const char *str;
9701 const enum small_ex_type type;
9702} percent_op[] =
9703{
ad8d3bb3
TS
9704 {"%lo", S_EX_LO},
9705#ifdef OBJ_ELF
394f9b3a
TS
9706 {"%call_hi", S_EX_CALL_HI},
9707 {"%call_lo", S_EX_CALL_LO},
ad8d3bb3
TS
9708 {"%call16", S_EX_CALL16},
9709 {"%got_disp", S_EX_GOT_DISP},
9710 {"%got_page", S_EX_GOT_PAGE},
9711 {"%got_ofst", S_EX_GOT_OFST},
9712 {"%got_hi", S_EX_GOT_HI},
9713 {"%got_lo", S_EX_GOT_LO},
394f9b3a
TS
9714 {"%got", S_EX_GOT},
9715 {"%gp_rel", S_EX_GP_REL},
9716 {"%half", S_EX_HALF},
ad8d3bb3 9717 {"%highest", S_EX_HIGHEST},
394f9b3a
TS
9718 {"%higher", S_EX_HIGHER},
9719 {"%neg", S_EX_NEG},
ad8d3bb3 9720#endif
394f9b3a 9721 {"%hi", S_EX_HI}
ad8d3bb3
TS
9722};
9723
9724/* Parse small expression input. STR gets adjusted to eat up whitespace.
9725 It detects valid "%percent_op(...)" and "($reg)" strings. Percent_op's
9726 can be nested, this is handled by blanking the innermost, parsing the
9727 rest by subsequent calls. */
252b5132
RH
9728
9729static int
ad8d3bb3
TS
9730my_getSmallParser (str, len, nestlevel)
9731 char **str;
9732 unsigned int *len;
9733 int *nestlevel;
252b5132 9734{
ad8d3bb3
TS
9735 *len = 0;
9736 *str += strspn (*str, " \t");
394f9b3a 9737 /* Check for expression in parentheses. */
ad8d3bb3 9738 if (**str == '(')
252b5132 9739 {
ad8d3bb3
TS
9740 char *b = *str + 1 + strspn (*str + 1, " \t");
9741 char *e;
9742
9743 /* Check for base register. */
9744 if (b[0] == '$')
9745 {
9746 if (strchr (b, ')')
9747 && (e = b + strcspn (b, ") \t"))
9748 && e - b > 1 && e - b < 4)
9749 {
98d3f06f
KH
9750 if ((e - b == 3
9751 && ((b[1] == 'f' && b[2] == 'p')
9752 || (b[1] == 's' && b[2] == 'p')
9753 || (b[1] == 'g' && b[2] == 'p')
9754 || (b[1] == 'a' && b[2] == 't')
9755 || (ISDIGIT (b[1])
9756 && ISDIGIT (b[2]))))
9757 || (ISDIGIT (b[1])))
9758 {
9759 *len = strcspn (*str, ")") + 1;
9760 return S_EX_REGISTER;
9761 }
ad8d3bb3
TS
9762 }
9763 }
394f9b3a 9764 /* Check for percent_op (in parentheses). */
ad8d3bb3
TS
9765 else if (b[0] == '%')
9766 {
9767 *str = b;
394f9b3a 9768 return my_getPercentOp (str, len, nestlevel);
ad8d3bb3 9769 }
76b3015f 9770
394f9b3a
TS
9771 /* Some other expression in the parentheses, which can contain
9772 parentheses itself. Attempt to find the matching one. */
9773 {
9774 int pcnt = 1;
9775 char *s;
9776
9777 *len = 1;
9778 for (s = *str + 1; *s && pcnt; s++, (*len)++)
9779 {
9780 if (*s == '(')
f9419b05 9781 ++pcnt;
394f9b3a 9782 else if (*s == ')')
f9419b05 9783 --pcnt;
394f9b3a
TS
9784 }
9785 }
fb1b3232 9786 }
394f9b3a 9787 /* Check for percent_op (outside of parentheses). */
ad8d3bb3 9788 else if (*str[0] == '%')
394f9b3a
TS
9789 return my_getPercentOp (str, len, nestlevel);
9790
9791 /* Any other expression. */
9792 return S_EX_NONE;
9793}
ad8d3bb3 9794
394f9b3a
TS
9795static int
9796my_getPercentOp (str, len, nestlevel)
9797 char **str;
9798 unsigned int *len;
9799 int *nestlevel;
9800{
9801 char *tmp = *str + 1;
9802 unsigned int i = 0;
ad8d3bb3 9803
394f9b3a
TS
9804 while (ISALPHA (*tmp) || *tmp == '_')
9805 {
9806 *tmp = TOLOWER (*tmp);
9807 tmp++;
9808 }
9809 while (i < (sizeof (percent_op) / sizeof (struct percent_op_match)))
9810 {
9811 if (strncmp (*str, percent_op[i].str, strlen (percent_op[i].str)))
98d3f06f 9812 i++;
394f9b3a 9813 else
ad8d3bb3 9814 {
394f9b3a 9815 int type = percent_op[i].type;
ad8d3bb3 9816
394f9b3a
TS
9817 /* Only %hi and %lo are allowed for OldABI. */
9818 if (! HAVE_NEWABI && type != S_EX_HI && type != S_EX_LO)
9819 return S_EX_NONE;
ad8d3bb3 9820
394f9b3a 9821 *len = strlen (percent_op[i].str);
f9419b05 9822 ++(*nestlevel);
394f9b3a 9823 return type;
ad8d3bb3 9824 }
fb1b3232 9825 }
ad8d3bb3
TS
9826 return S_EX_NONE;
9827}
9828
9829static int
9830my_getSmallExpression (ep, str)
9831 expressionS *ep;
9832 char *str;
9833{
9834 static char *oldstr = NULL;
9835 int c = S_EX_NONE;
9836 int oldc;
394f9b3a 9837 int nestlevel = -1;
ad8d3bb3
TS
9838 unsigned int len;
9839
394f9b3a
TS
9840 /* Don't update oldstr if the last call had nested percent_op's. We need
9841 it to parse the outer ones later. */
ad8d3bb3
TS
9842 if (! oldstr)
9843 oldstr = str;
76b3015f 9844
ad8d3bb3 9845 do
fb1b3232 9846 {
ad8d3bb3 9847 oldc = c;
394f9b3a 9848 c = my_getSmallParser (&str, &len, &nestlevel);
ad8d3bb3
TS
9849 if (c != S_EX_NONE && c != S_EX_REGISTER)
9850 str += len;
fb1b3232 9851 }
ad8d3bb3
TS
9852 while (c != S_EX_NONE && c != S_EX_REGISTER);
9853
394f9b3a 9854 if (nestlevel >= 0)
fb1b3232 9855 {
394f9b3a
TS
9856 /* A percent_op was encountered. Don't try to get an expression if
9857 it is already blanked out. */
ad8d3bb3
TS
9858 if (*(str + strspn (str + 1, " )")) != ')')
9859 {
9860 char save;
9861
394f9b3a 9862 /* Let my_getExpression() stop at the closing parenthesis. */
ad8d3bb3
TS
9863 save = *(str + len);
9864 *(str + len) = '\0';
9865 my_getExpression (ep, str);
9866 *(str + len) = save;
9867 }
394f9b3a 9868 if (nestlevel > 0)
ad8d3bb3 9869 {
394f9b3a
TS
9870 /* Blank out including the % sign and the proper matching
9871 parenthesis. */
9872 int pcnt = 1;
9873 char *s = strrchr (oldstr, '%');
9874 char *end;
9875
9876 for (end = strchr (s, '(') + 1; *end && pcnt; end++)
9877 {
9878 if (*end == '(')
f9419b05 9879 ++pcnt;
394f9b3a 9880 else if (*end == ')')
f9419b05 9881 --pcnt;
394f9b3a
TS
9882 }
9883
9884 memset (s, ' ', end - s);
ad8d3bb3
TS
9885 str = oldstr;
9886 }
9887 else
394f9b3a
TS
9888 expr_end = str + len;
9889
ad8d3bb3 9890 c = oldc;
fb1b3232 9891 }
ad8d3bb3 9892 else if (c == S_EX_NONE)
fb1b3232 9893 {
ad8d3bb3 9894 my_getExpression (ep, str);
fb1b3232 9895 }
ad8d3bb3 9896 else if (c == S_EX_REGISTER)
fb1b3232 9897 {
ad8d3bb3
TS
9898 ep->X_op = O_constant;
9899 expr_end = str;
9900 ep->X_add_symbol = NULL;
9901 ep->X_op_symbol = NULL;
9902 ep->X_add_number = 0;
fb1b3232 9903 }
fb1b3232
TS
9904 else
9905 {
98d3f06f 9906 as_fatal (_("internal error"));
fb1b3232 9907 }
252b5132 9908
394f9b3a
TS
9909 if (nestlevel <= 0)
9910 /* All percent_op's have been handled. */
ad8d3bb3 9911 oldstr = NULL;
fb1b3232 9912
fb1b3232 9913 return c;
252b5132
RH
9914}
9915
9916static void
9917my_getExpression (ep, str)
9918 expressionS *ep;
9919 char *str;
9920{
9921 char *save_in;
98aa84af 9922 valueT val;
252b5132
RH
9923
9924 save_in = input_line_pointer;
9925 input_line_pointer = str;
9926 expression (ep);
9927 expr_end = input_line_pointer;
9928 input_line_pointer = save_in;
9929
9930 /* If we are in mips16 mode, and this is an expression based on `.',
9931 then we bump the value of the symbol by 1 since that is how other
9932 text symbols are handled. We don't bother to handle complex
9933 expressions, just `.' plus or minus a constant. */
9934 if (mips_opts.mips16
9935 && ep->X_op == O_symbol
9936 && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
9937 && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
49309057
ILT
9938 && symbol_get_frag (ep->X_add_symbol) == frag_now
9939 && symbol_constant_p (ep->X_add_symbol)
98aa84af
AM
9940 && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
9941 S_SET_VALUE (ep->X_add_symbol, val + 1);
252b5132
RH
9942}
9943
9944/* Turn a string in input_line_pointer into a floating point constant
bc0d738a
NC
9945 of type TYPE, and store the appropriate bytes in *LITP. The number
9946 of LITTLENUMS emitted is stored in *SIZEP. An error message is
252b5132
RH
9947 returned, or NULL on OK. */
9948
9949char *
9950md_atof (type, litP, sizeP)
9951 int type;
9952 char *litP;
9953 int *sizeP;
9954{
9955 int prec;
9956 LITTLENUM_TYPE words[4];
9957 char *t;
9958 int i;
9959
9960 switch (type)
9961 {
9962 case 'f':
9963 prec = 2;
9964 break;
9965
9966 case 'd':
9967 prec = 4;
9968 break;
9969
9970 default:
9971 *sizeP = 0;
9972 return _("bad call to md_atof");
9973 }
9974
9975 t = atof_ieee (input_line_pointer, type, words);
9976 if (t)
9977 input_line_pointer = t;
9978
9979 *sizeP = prec * 2;
9980
9981 if (! target_big_endian)
9982 {
9983 for (i = prec - 1; i >= 0; i--)
9984 {
9985 md_number_to_chars (litP, (valueT) words[i], 2);
9986 litP += 2;
9987 }
9988 }
9989 else
9990 {
9991 for (i = 0; i < prec; i++)
9992 {
9993 md_number_to_chars (litP, (valueT) words[i], 2);
9994 litP += 2;
9995 }
9996 }
bdaaa2e1 9997
252b5132
RH
9998 return NULL;
9999}
10000
10001void
10002md_number_to_chars (buf, val, n)
10003 char *buf;
10004 valueT val;
10005 int n;
10006{
10007 if (target_big_endian)
10008 number_to_chars_bigendian (buf, val, n);
10009 else
10010 number_to_chars_littleendian (buf, val, n);
10011}
10012\f
ae948b86 10013#ifdef OBJ_ELF
e013f690
TS
10014static int support_64bit_objects(void)
10015{
10016 const char **list, **l;
10017
10018 list = bfd_target_list ();
10019 for (l = list; *l != NULL; l++)
10020#ifdef TE_TMIPS
10021 /* This is traditional mips */
10022 if (strcmp (*l, "elf64-tradbigmips") == 0
10023 || strcmp (*l, "elf64-tradlittlemips") == 0)
10024#else
10025 if (strcmp (*l, "elf64-bigmips") == 0
10026 || strcmp (*l, "elf64-littlemips") == 0)
10027#endif
10028 break;
10029 free (list);
10030 return (*l != NULL);
10031}
ae948b86 10032#endif /* OBJ_ELF */
e013f690 10033
39c0a331 10034CONST char *md_shortopts = "nO::g::G:";
252b5132 10035
e972090a
NC
10036struct option md_longopts[] =
10037{
252b5132
RH
10038#define OPTION_MIPS1 (OPTION_MD_BASE + 1)
10039 {"mips0", no_argument, NULL, OPTION_MIPS1},
10040 {"mips1", no_argument, NULL, OPTION_MIPS1},
10041#define OPTION_MIPS2 (OPTION_MD_BASE + 2)
10042 {"mips2", no_argument, NULL, OPTION_MIPS2},
10043#define OPTION_MIPS3 (OPTION_MD_BASE + 3)
10044 {"mips3", no_argument, NULL, OPTION_MIPS3},
10045#define OPTION_MIPS4 (OPTION_MD_BASE + 4)
10046 {"mips4", no_argument, NULL, OPTION_MIPS4},
ae948b86
TS
10047#define OPTION_MIPS5 (OPTION_MD_BASE + 5)
10048 {"mips5", no_argument, NULL, OPTION_MIPS5},
10049#define OPTION_MIPS32 (OPTION_MD_BASE + 6)
10050 {"mips32", no_argument, NULL, OPTION_MIPS32},
10051#define OPTION_MIPS64 (OPTION_MD_BASE + 7)
10052 {"mips64", no_argument, NULL, OPTION_MIPS64},
10053#define OPTION_MEMBEDDED_PIC (OPTION_MD_BASE + 8)
252b5132 10054 {"membedded-pic", no_argument, NULL, OPTION_MEMBEDDED_PIC},
ae948b86 10055#define OPTION_TRAP (OPTION_MD_BASE + 9)
252b5132
RH
10056 {"trap", no_argument, NULL, OPTION_TRAP},
10057 {"no-break", no_argument, NULL, OPTION_TRAP},
ae948b86 10058#define OPTION_BREAK (OPTION_MD_BASE + 10)
252b5132
RH
10059 {"break", no_argument, NULL, OPTION_BREAK},
10060 {"no-trap", no_argument, NULL, OPTION_BREAK},
ae948b86 10061#define OPTION_EB (OPTION_MD_BASE + 11)
252b5132 10062 {"EB", no_argument, NULL, OPTION_EB},
ae948b86 10063#define OPTION_EL (OPTION_MD_BASE + 12)
252b5132 10064 {"EL", no_argument, NULL, OPTION_EL},
ae948b86 10065#define OPTION_MIPS16 (OPTION_MD_BASE + 13)
252b5132 10066 {"mips16", no_argument, NULL, OPTION_MIPS16},
ae948b86 10067#define OPTION_NO_MIPS16 (OPTION_MD_BASE + 14)
252b5132 10068 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
ae948b86 10069#define OPTION_M7000_HILO_FIX (OPTION_MD_BASE + 15)
6b76fefe 10070 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
9ee72ff1
TS
10071#define OPTION_MNO_7000_HILO_FIX (OPTION_MD_BASE + 16)
10072 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
10073 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
ae948b86
TS
10074#define OPTION_FP32 (OPTION_MD_BASE + 17)
10075 {"mfp32", no_argument, NULL, OPTION_FP32},
10076#define OPTION_GP32 (OPTION_MD_BASE + 18)
c97ef257 10077 {"mgp32", no_argument, NULL, OPTION_GP32},
ae948b86 10078#define OPTION_CONSTRUCT_FLOATS (OPTION_MD_BASE + 19)
119d663a 10079 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
ae948b86 10080#define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MD_BASE + 20)
119d663a 10081 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
ae948b86 10082#define OPTION_MARCH (OPTION_MD_BASE + 21)
ec68c924 10083 {"march", required_argument, NULL, OPTION_MARCH},
ae948b86 10084#define OPTION_MTUNE (OPTION_MD_BASE + 22)
ec68c924 10085 {"mtune", required_argument, NULL, OPTION_MTUNE},
ae948b86
TS
10086#define OPTION_MCPU (OPTION_MD_BASE + 23)
10087 {"mcpu", required_argument, NULL, OPTION_MCPU},
10088#define OPTION_M4650 (OPTION_MD_BASE + 24)
10089 {"m4650", no_argument, NULL, OPTION_M4650},
10090#define OPTION_NO_M4650 (OPTION_MD_BASE + 25)
10091 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
10092#define OPTION_M4010 (OPTION_MD_BASE + 26)
10093 {"m4010", no_argument, NULL, OPTION_M4010},
10094#define OPTION_NO_M4010 (OPTION_MD_BASE + 27)
10095 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
10096#define OPTION_M4100 (OPTION_MD_BASE + 28)
10097 {"m4100", no_argument, NULL, OPTION_M4100},
10098#define OPTION_NO_M4100 (OPTION_MD_BASE + 29)
10099 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
10100#define OPTION_M3900 (OPTION_MD_BASE + 30)
10101 {"m3900", no_argument, NULL, OPTION_M3900},
10102#define OPTION_NO_M3900 (OPTION_MD_BASE + 31)
10103 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
10104#define OPTION_GP64 (OPTION_MD_BASE + 32)
10105 {"mgp64", no_argument, NULL, OPTION_GP64},
1f25f5d3
CD
10106#define OPTION_MIPS3D (OPTION_MD_BASE + 33)
10107 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
10108#define OPTION_NO_MIPS3D (OPTION_MD_BASE + 34)
10109 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
deec1734
CD
10110#define OPTION_MDMX (OPTION_MD_BASE + 35)
10111 {"mdmx", no_argument, NULL, OPTION_MDMX},
10112#define OPTION_NO_MDMX (OPTION_MD_BASE + 36)
10113 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
156c2f8b 10114#ifdef OBJ_ELF
deec1734 10115#define OPTION_ELF_BASE (OPTION_MD_BASE + 37)
156c2f8b 10116#define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
156c2f8b
NC
10117 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
10118 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
ae948b86 10119#define OPTION_NON_SHARED (OPTION_ELF_BASE + 1)
156c2f8b 10120 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
ae948b86 10121#define OPTION_XGOT (OPTION_ELF_BASE + 2)
156c2f8b 10122 {"xgot", no_argument, NULL, OPTION_XGOT},
ae948b86
TS
10123#define OPTION_MABI (OPTION_ELF_BASE + 3)
10124 {"mabi", required_argument, NULL, OPTION_MABI},
10125#define OPTION_32 (OPTION_ELF_BASE + 4)
156c2f8b 10126 {"32", no_argument, NULL, OPTION_32},
ae948b86 10127#define OPTION_N32 (OPTION_ELF_BASE + 5)
e013f690 10128 {"n32", no_argument, NULL, OPTION_N32},
ae948b86 10129#define OPTION_64 (OPTION_ELF_BASE + 6)
156c2f8b 10130 {"64", no_argument, NULL, OPTION_64},
ae948b86 10131#endif /* OBJ_ELF */
252b5132
RH
10132 {NULL, no_argument, NULL, 0}
10133};
156c2f8b 10134size_t md_longopts_size = sizeof (md_longopts);
252b5132
RH
10135
10136int
10137md_parse_option (c, arg)
10138 int c;
10139 char *arg;
10140{
10141 switch (c)
10142 {
119d663a
NC
10143 case OPTION_CONSTRUCT_FLOATS:
10144 mips_disable_float_construction = 0;
10145 break;
bdaaa2e1 10146
119d663a
NC
10147 case OPTION_NO_CONSTRUCT_FLOATS:
10148 mips_disable_float_construction = 1;
10149 break;
bdaaa2e1 10150
252b5132
RH
10151 case OPTION_TRAP:
10152 mips_trap = 1;
10153 break;
10154
10155 case OPTION_BREAK:
10156 mips_trap = 0;
10157 break;
10158
10159 case OPTION_EB:
10160 target_big_endian = 1;
10161 break;
10162
10163 case OPTION_EL:
10164 target_big_endian = 0;
10165 break;
10166
39c0a331
L
10167 case 'n':
10168 warn_nops = 1;
10169 break;
10170
252b5132
RH
10171 case 'O':
10172 if (arg && arg[1] == '0')
10173 mips_optimize = 1;
10174 else
10175 mips_optimize = 2;
10176 break;
10177
10178 case 'g':
10179 if (arg == NULL)
10180 mips_debug = 2;
10181 else
10182 mips_debug = atoi (arg);
10183 /* When the MIPS assembler sees -g or -g2, it does not do
10184 optimizations which limit full symbolic debugging. We take
10185 that to be equivalent to -O0. */
10186 if (mips_debug == 2)
10187 mips_optimize = 1;
10188 break;
10189
10190 case OPTION_MIPS1:
e7af610e 10191 mips_opts.isa = ISA_MIPS1;
252b5132
RH
10192 break;
10193
10194 case OPTION_MIPS2:
e7af610e 10195 mips_opts.isa = ISA_MIPS2;
252b5132
RH
10196 break;
10197
10198 case OPTION_MIPS3:
e7af610e 10199 mips_opts.isa = ISA_MIPS3;
252b5132
RH
10200 break;
10201
10202 case OPTION_MIPS4:
e7af610e
NC
10203 mips_opts.isa = ISA_MIPS4;
10204 break;
10205
84ea6cf2
NC
10206 case OPTION_MIPS5:
10207 mips_opts.isa = ISA_MIPS5;
10208 break;
10209
e7af610e
NC
10210 case OPTION_MIPS32:
10211 mips_opts.isa = ISA_MIPS32;
252b5132
RH
10212 break;
10213
84ea6cf2
NC
10214 case OPTION_MIPS64:
10215 mips_opts.isa = ISA_MIPS64;
10216 break;
10217
ec68c924
EC
10218 case OPTION_MTUNE:
10219 case OPTION_MARCH:
252b5132
RH
10220 case OPTION_MCPU:
10221 {
ec68c924
EC
10222 int cpu = CPU_UNKNOWN;
10223
e7af610e 10224 /* Identify the processor type. */
ec68c924 10225 if (strcasecmp (arg, "default") != 0)
252b5132 10226 {
e7af610e 10227 const struct mips_cpu_info *ci;
252b5132 10228
e7af610e
NC
10229 ci = mips_cpu_info_from_name (arg);
10230 if (ci == NULL || ci->is_isa)
ec68c924
EC
10231 {
10232 switch (c)
10233 {
10234 case OPTION_MTUNE:
10235 as_fatal (_("invalid architecture -mtune=%s"), arg);
10236 break;
10237 case OPTION_MARCH:
10238 as_fatal (_("invalid architecture -march=%s"), arg);
10239 break;
10240 case OPTION_MCPU:
10241 as_fatal (_("invalid architecture -mcpu=%s"), arg);
10242 break;
10243 }
10244 }
e7af610e 10245 else
98d3f06f 10246 cpu = ci->cpu;
ec68c924
EC
10247 }
10248
10249 switch (c)
10250 {
10251 case OPTION_MTUNE:
6dce9e24 10252 if (mips_tune != CPU_UNKNOWN && mips_tune != cpu)
98d3f06f
KH
10253 as_warn (_("A different -mtune= was already specified, is now "
10254 "-mtune=%s"), arg);
ec68c924
EC
10255 mips_tune = cpu;
10256 break;
10257 case OPTION_MARCH:
6dce9e24 10258 if (mips_arch != CPU_UNKNOWN && mips_arch != cpu)
98d3f06f
KH
10259 as_warn (_("A different -march= was already specified, is now "
10260 "-march=%s"), arg);
ec68c924
EC
10261 mips_arch = cpu;
10262 break;
10263 case OPTION_MCPU:
6dce9e24 10264 if (mips_cpu != CPU_UNKNOWN && mips_cpu != cpu)
98d3f06f
KH
10265 as_warn (_("A different -mcpu= was already specified, is now "
10266 "-mcpu=%s"), arg);
ec68c924 10267 mips_cpu = cpu;
252b5132
RH
10268 }
10269 }
10270 break;
10271
10272 case OPTION_M4650:
6dce9e24
TS
10273 if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_R4650)
10274 || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_R4650))
98d3f06f
KH
10275 as_warn (_("A different -march= or -mtune= was already specified, "
10276 "is now -m4650"));
ec68c924
EC
10277 mips_arch = CPU_R4650;
10278 mips_tune = CPU_R4650;
252b5132
RH
10279 break;
10280
10281 case OPTION_NO_M4650:
10282 break;
10283
10284 case OPTION_M4010:
6dce9e24
TS
10285 if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_R4010)
10286 || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_R4010))
98d3f06f
KH
10287 as_warn (_("A different -march= or -mtune= was already specified, "
10288 "is now -m4010"));
ec68c924
EC
10289 mips_arch = CPU_R4010;
10290 mips_tune = CPU_R4010;
252b5132
RH
10291 break;
10292
10293 case OPTION_NO_M4010:
10294 break;
10295
10296 case OPTION_M4100:
6dce9e24
TS
10297 if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_VR4100)
10298 || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_VR4100))
98d3f06f
KH
10299 as_warn (_("A different -march= or -mtune= was already specified, "
10300 "is now -m4100"));
ec68c924
EC
10301 mips_arch = CPU_VR4100;
10302 mips_tune = CPU_VR4100;
252b5132
RH
10303 break;
10304
10305 case OPTION_NO_M4100:
10306 break;
10307
252b5132 10308 case OPTION_M3900:
6dce9e24
TS
10309 if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_R3900)
10310 || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_R3900))
98d3f06f
KH
10311 as_warn (_("A different -march= or -mtune= was already specified, "
10312 "is now -m3900"));
ec68c924
EC
10313 mips_arch = CPU_R3900;
10314 mips_tune = CPU_R3900;
252b5132 10315 break;
bdaaa2e1 10316
252b5132
RH
10317 case OPTION_NO_M3900:
10318 break;
10319
deec1734
CD
10320 case OPTION_MDMX:
10321 mips_opts.ase_mdmx = 1;
10322 break;
10323
10324 case OPTION_NO_MDMX:
10325 mips_opts.ase_mdmx = 0;
10326 break;
10327
252b5132
RH
10328 case OPTION_MIPS16:
10329 mips_opts.mips16 = 1;
10330 mips_no_prev_insn (false);
10331 break;
10332
10333 case OPTION_NO_MIPS16:
10334 mips_opts.mips16 = 0;
10335 mips_no_prev_insn (false);
10336 break;
10337
1f25f5d3
CD
10338 case OPTION_MIPS3D:
10339 mips_opts.ase_mips3d = 1;
10340 break;
10341
10342 case OPTION_NO_MIPS3D:
10343 mips_opts.ase_mips3d = 0;
10344 break;
10345
252b5132
RH
10346 case OPTION_MEMBEDDED_PIC:
10347 mips_pic = EMBEDDED_PIC;
10348 if (USE_GLOBAL_POINTER_OPT && g_switch_seen)
10349 {
10350 as_bad (_("-G may not be used with embedded PIC code"));
10351 return 0;
10352 }
10353 g_switch_value = 0x7fffffff;
10354 break;
10355
0f074f60 10356#ifdef OBJ_ELF
252b5132
RH
10357 /* When generating ELF code, we permit -KPIC and -call_shared to
10358 select SVR4_PIC, and -non_shared to select no PIC. This is
10359 intended to be compatible with Irix 5. */
10360 case OPTION_CALL_SHARED:
10361 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10362 {
10363 as_bad (_("-call_shared is supported only for ELF format"));
10364 return 0;
10365 }
10366 mips_pic = SVR4_PIC;
10367 if (g_switch_seen && g_switch_value != 0)
10368 {
10369 as_bad (_("-G may not be used with SVR4 PIC code"));
10370 return 0;
10371 }
10372 g_switch_value = 0;
10373 break;
10374
10375 case OPTION_NON_SHARED:
10376 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10377 {
10378 as_bad (_("-non_shared is supported only for ELF format"));
10379 return 0;
10380 }
10381 mips_pic = NO_PIC;
10382 break;
10383
10384 /* The -xgot option tells the assembler to use 32 offsets when
10385 accessing the got in SVR4_PIC mode. It is for Irix
10386 compatibility. */
10387 case OPTION_XGOT:
10388 mips_big_got = 1;
10389 break;
0f074f60 10390#endif /* OBJ_ELF */
252b5132
RH
10391
10392 case 'G':
10393 if (! USE_GLOBAL_POINTER_OPT)
10394 {
10395 as_bad (_("-G is not supported for this configuration"));
10396 return 0;
10397 }
10398 else if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
10399 {
10400 as_bad (_("-G may not be used with SVR4 or embedded PIC code"));
10401 return 0;
10402 }
10403 else
10404 g_switch_value = atoi (arg);
10405 g_switch_seen = 1;
10406 break;
10407
0f074f60 10408#ifdef OBJ_ELF
34ba82a8
TS
10409 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
10410 and -mabi=64. */
252b5132 10411 case OPTION_32:
34ba82a8
TS
10412 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10413 {
10414 as_bad (_("-32 is supported for ELF format only"));
10415 return 0;
10416 }
a325df1d 10417 mips_opts.abi = O32_ABI;
252b5132
RH
10418 break;
10419
e013f690 10420 case OPTION_N32:
34ba82a8
TS
10421 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10422 {
10423 as_bad (_("-n32 is supported for ELF format only"));
10424 return 0;
10425 }
a325df1d 10426 mips_opts.abi = N32_ABI;
e013f690 10427 break;
252b5132 10428
e013f690 10429 case OPTION_64:
34ba82a8
TS
10430 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10431 {
10432 as_bad (_("-64 is supported for ELF format only"));
10433 return 0;
10434 }
a325df1d 10435 mips_opts.abi = N64_ABI;
e013f690
TS
10436 if (! support_64bit_objects())
10437 as_fatal (_("No compiled in support for 64 bit object file format"));
252b5132 10438 break;
ae948b86 10439#endif /* OBJ_ELF */
252b5132 10440
c97ef257 10441 case OPTION_GP32:
a325df1d
TS
10442 file_mips_gp32 = 1;
10443 if (mips_opts.abi != O32_ABI)
10444 mips_opts.abi = NO_ABI;
c97ef257
AH
10445 break;
10446
10447 case OPTION_GP64:
a325df1d
TS
10448 file_mips_gp32 = 0;
10449 if (mips_opts.abi == O32_ABI)
10450 mips_opts.abi = NO_ABI;
c97ef257 10451 break;
252b5132 10452
ca4e0257 10453 case OPTION_FP32:
a325df1d
TS
10454 file_mips_fp32 = 1;
10455 if (mips_opts.abi != O32_ABI)
10456 mips_opts.abi = NO_ABI;
ca4e0257
RS
10457 break;
10458
ae948b86 10459#ifdef OBJ_ELF
252b5132 10460 case OPTION_MABI:
34ba82a8
TS
10461 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10462 {
10463 as_bad (_("-mabi is supported for ELF format only"));
10464 return 0;
10465 }
e013f690 10466 if (strcmp (arg, "32") == 0)
a325df1d 10467 mips_opts.abi = O32_ABI;
e013f690 10468 else if (strcmp (arg, "o64") == 0)
a325df1d 10469 mips_opts.abi = O64_ABI;
e013f690 10470 else if (strcmp (arg, "n32") == 0)
a325df1d 10471 mips_opts.abi = N32_ABI;
e013f690
TS
10472 else if (strcmp (arg, "64") == 0)
10473 {
a325df1d 10474 mips_opts.abi = N64_ABI;
e013f690
TS
10475 if (! support_64bit_objects())
10476 as_fatal (_("No compiled in support for 64 bit object file "
10477 "format"));
10478 }
10479 else if (strcmp (arg, "eabi") == 0)
a325df1d 10480 mips_opts.abi = EABI_ABI;
e013f690 10481 else
da0e507f
TS
10482 {
10483 as_fatal (_("invalid abi -mabi=%s"), arg);
10484 return 0;
10485 }
252b5132 10486 break;
e013f690 10487#endif /* OBJ_ELF */
252b5132 10488
6b76fefe
CM
10489 case OPTION_M7000_HILO_FIX:
10490 mips_7000_hilo_fix = true;
10491 break;
10492
9ee72ff1 10493 case OPTION_MNO_7000_HILO_FIX:
6b76fefe
CM
10494 mips_7000_hilo_fix = false;
10495 break;
10496
252b5132
RH
10497 default:
10498 return 0;
10499 }
10500
10501 return 1;
10502}
10503
252b5132
RH
10504static void
10505show (stream, string, col_p, first_p)
10506 FILE *stream;
10507 char *string;
10508 int *col_p;
10509 int *first_p;
10510{
10511 if (*first_p)
10512 {
10513 fprintf (stream, "%24s", "");
10514 *col_p = 24;
10515 }
10516 else
10517 {
10518 fprintf (stream, ", ");
10519 *col_p += 2;
10520 }
10521
10522 if (*col_p + strlen (string) > 72)
10523 {
10524 fprintf (stream, "\n%24s", "");
10525 *col_p = 24;
10526 }
10527
10528 fprintf (stream, "%s", string);
10529 *col_p += strlen (string);
10530
10531 *first_p = 0;
10532}
10533
252b5132
RH
10534void
10535md_show_usage (stream)
10536 FILE *stream;
10537{
10538 int column, first;
10539
beae10d5 10540 fprintf (stream, _("\
252b5132
RH
10541MIPS options:\n\
10542-membedded-pic generate embedded position independent code\n\
10543-EB generate big endian output\n\
10544-EL generate little endian output\n\
9a41af64 10545-g, -g2 do not remove unneeded NOPs or swap branches\n\
252b5132
RH
10546-G NUM allow referencing objects up to NUM bytes\n\
10547 implicitly with the gp register [default 8]\n"));
beae10d5 10548 fprintf (stream, _("\
252b5132
RH
10549-mips1 generate MIPS ISA I instructions\n\
10550-mips2 generate MIPS ISA II instructions\n\
10551-mips3 generate MIPS ISA III instructions\n\
10552-mips4 generate MIPS ISA IV instructions\n\
84ea6cf2 10553-mips5 generate MIPS ISA V instructions\n\
e7af610e 10554-mips32 generate MIPS32 ISA instructions\n\
84ea6cf2 10555-mips64 generate MIPS64 ISA instructions\n\
ec68c924 10556-march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
252b5132
RH
10557
10558 first = 1;
10559
10560 show (stream, "2000", &column, &first);
10561 show (stream, "3000", &column, &first);
10562 show (stream, "3900", &column, &first);
10563 show (stream, "4000", &column, &first);
10564 show (stream, "4010", &column, &first);
10565 show (stream, "4100", &column, &first);
10566 show (stream, "4111", &column, &first);
10567 show (stream, "4300", &column, &first);
10568 show (stream, "4400", &column, &first);
10569 show (stream, "4600", &column, &first);
10570 show (stream, "4650", &column, &first);
10571 show (stream, "5000", &column, &first);
18ae5d72
EC
10572 show (stream, "5200", &column, &first);
10573 show (stream, "5230", &column, &first);
10574 show (stream, "5231", &column, &first);
10575 show (stream, "5261", &column, &first);
10576 show (stream, "5721", &column, &first);
252b5132
RH
10577 show (stream, "6000", &column, &first);
10578 show (stream, "8000", &column, &first);
10579 show (stream, "10000", &column, &first);
d1cf510e 10580 show (stream, "12000", &column, &first);
2e4acd24 10581 show (stream, "sb1", &column, &first);
252b5132
RH
10582 fputc ('\n', stream);
10583
10584 fprintf (stream, _("\
ec68c924 10585-mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
252b5132
RH
10586-no-mCPU don't generate code specific to CPU.\n\
10587 For -mCPU and -no-mCPU, CPU must be one of:\n"));
10588
10589 first = 1;
10590
10591 show (stream, "3900", &column, &first);
10592 show (stream, "4010", &column, &first);
10593 show (stream, "4100", &column, &first);
10594 show (stream, "4650", &column, &first);
10595 fputc ('\n', stream);
10596
beae10d5 10597 fprintf (stream, _("\
252b5132
RH
10598-mips16 generate mips16 instructions\n\
10599-no-mips16 do not generate mips16 instructions\n"));
beae10d5 10600 fprintf (stream, _("\
ca4e0257
RS
10601-mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
10602-mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
252b5132
RH
10603-O0 remove unneeded NOPs, do not swap branches\n\
10604-O remove unneeded NOPs and swap branches\n\
63486801 10605-n warn about NOPs generated from macros\n\
119d663a 10606--[no-]construct-floats [dis]allow floating point values to be constructed\n\
252b5132
RH
10607--trap, --no-break trap exception on div by 0 and mult overflow\n\
10608--break, --no-trap break exception on div by 0 and mult overflow\n"));
10609#ifdef OBJ_ELF
beae10d5 10610 fprintf (stream, _("\
252b5132
RH
10611-KPIC, -call_shared generate SVR4 position independent code\n\
10612-non_shared do not generate position independent code\n\
10613-xgot assume a 32 bit GOT\n\
34ba82a8
TS
10614-mabi=ABI create ABI conformant object file for:\n"));
10615
10616 first = 1;
10617
10618 show (stream, "32", &column, &first);
10619 show (stream, "o64", &column, &first);
10620 show (stream, "n32", &column, &first);
10621 show (stream, "64", &column, &first);
10622 show (stream, "eabi", &column, &first);
80cc45a5 10623
34ba82a8
TS
10624 fputc ('\n', stream);
10625
10626 fprintf (stream, _("\
e013f690
TS
10627-32 create o32 ABI object file (default)\n\
10628-n32 create n32 ABI object file\n\
10629-64 create 64 ABI object file\n"));
252b5132
RH
10630#endif
10631}
10632\f
10633void
10634mips_init_after_args ()
10635{
10636 /* initialize opcodes */
10637 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
beae10d5 10638 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
252b5132
RH
10639}
10640
10641long
10642md_pcrel_from (fixP)
10643 fixS *fixP;
10644{
10645 if (OUTPUT_FLAVOR != bfd_target_aout_flavour
10646 && fixP->fx_addsy != (symbolS *) NULL
10647 && ! S_IS_DEFINED (fixP->fx_addsy))
10648 {
6478892d
TS
10649 /* This makes a branch to an undefined symbol be a branch to the
10650 current location. */
cb56d3d3 10651 if (mips_pic == EMBEDDED_PIC)
6478892d 10652 return 4;
cb56d3d3 10653 else
6478892d 10654 return 1;
252b5132
RH
10655 }
10656
c9914766 10657 /* Return the address of the delay slot. */
252b5132
RH
10658 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
10659}
10660
252b5132
RH
10661/* This is called before the symbol table is processed. In order to
10662 work with gcc when using mips-tfile, we must keep all local labels.
10663 However, in other cases, we want to discard them. If we were
10664 called with -g, but we didn't see any debugging information, it may
10665 mean that gcc is smuggling debugging information through to
10666 mips-tfile, in which case we must generate all local labels. */
10667
10668void
10669mips_frob_file_before_adjust ()
10670{
10671#ifndef NO_ECOFF_DEBUGGING
10672 if (ECOFF_DEBUGGING
10673 && mips_debug != 0
10674 && ! ecoff_debugging_seen)
10675 flag_keep_locals = 1;
10676#endif
10677}
10678
10679/* Sort any unmatched HI16_S relocs so that they immediately precede
94f592af 10680 the corresponding LO reloc. This is called before md_apply_fix3 and
252b5132
RH
10681 tc_gen_reloc. Unmatched HI16_S relocs can only be generated by
10682 explicit use of the %hi modifier. */
10683
10684void
10685mips_frob_file ()
10686{
10687 struct mips_hi_fixup *l;
10688
10689 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
10690 {
10691 segment_info_type *seginfo;
10692 int pass;
10693
10694 assert (l->fixp->fx_r_type == BFD_RELOC_HI16_S);
10695
10696 /* Check quickly whether the next fixup happens to be a matching
10697 %lo. */
10698 if (l->fixp->fx_next != NULL
10699 && l->fixp->fx_next->fx_r_type == BFD_RELOC_LO16
10700 && l->fixp->fx_addsy == l->fixp->fx_next->fx_addsy
10701 && l->fixp->fx_offset == l->fixp->fx_next->fx_offset)
10702 continue;
10703
10704 /* Look through the fixups for this segment for a matching %lo.
10705 When we find one, move the %hi just in front of it. We do
10706 this in two passes. In the first pass, we try to find a
10707 unique %lo. In the second pass, we permit multiple %hi
10708 relocs for a single %lo (this is a GNU extension). */
10709 seginfo = seg_info (l->seg);
10710 for (pass = 0; pass < 2; pass++)
10711 {
10712 fixS *f, *prev;
10713
10714 prev = NULL;
10715 for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
10716 {
10717 /* Check whether this is a %lo fixup which matches l->fixp. */
10718 if (f->fx_r_type == BFD_RELOC_LO16
10719 && f->fx_addsy == l->fixp->fx_addsy
10720 && f->fx_offset == l->fixp->fx_offset
10721 && (pass == 1
10722 || prev == NULL
10723 || prev->fx_r_type != BFD_RELOC_HI16_S
10724 || prev->fx_addsy != f->fx_addsy
10725 || prev->fx_offset != f->fx_offset))
10726 {
10727 fixS **pf;
10728
10729 /* Move l->fixp before f. */
10730 for (pf = &seginfo->fix_root;
10731 *pf != l->fixp;
10732 pf = &(*pf)->fx_next)
10733 assert (*pf != NULL);
10734
10735 *pf = l->fixp->fx_next;
10736
10737 l->fixp->fx_next = f;
10738 if (prev == NULL)
10739 seginfo->fix_root = l->fixp;
10740 else
10741 prev->fx_next = l->fixp;
10742
10743 break;
10744 }
10745
10746 prev = f;
10747 }
10748
10749 if (f != NULL)
10750 break;
10751
10752#if 0 /* GCC code motion plus incomplete dead code elimination
10753 can leave a %hi without a %lo. */
10754 if (pass == 1)
10755 as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
10756 _("Unmatched %%hi reloc"));
10757#endif
10758 }
10759 }
10760}
10761
10762/* When generating embedded PIC code we need to use a special
10763 relocation to represent the difference of two symbols in the .text
10764 section (switch tables use a difference of this sort). See
10765 include/coff/mips.h for details. This macro checks whether this
10766 fixup requires the special reloc. */
10767#define SWITCH_TABLE(fixp) \
10768 ((fixp)->fx_r_type == BFD_RELOC_32 \
bb2d6cd7 10769 && OUTPUT_FLAVOR != bfd_target_elf_flavour \
252b5132
RH
10770 && (fixp)->fx_addsy != NULL \
10771 && (fixp)->fx_subsy != NULL \
10772 && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
10773 && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
10774
10775/* When generating embedded PIC code we must keep all PC relative
10776 relocations, in case the linker has to relax a call. We also need
f6688943
TS
10777 to keep relocations for switch table entries.
10778
10779 We may have combined relocations without symbols in the N32/N64 ABI.
10780 We have to prevent gas from dropping them. */
252b5132 10781
252b5132
RH
10782int
10783mips_force_relocation (fixp)
10784 fixS *fixp;
10785{
10786 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
10787 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
10788 return 1;
10789
f6688943
TS
10790 if (HAVE_NEWABI
10791 && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
10792 && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
10793 || fixp->fx_r_type == BFD_RELOC_HI16_S
10794 || fixp->fx_r_type == BFD_RELOC_LO16))
10795 return 1;
10796
252b5132
RH
10797 return (mips_pic == EMBEDDED_PIC
10798 && (fixp->fx_pcrel
10799 || SWITCH_TABLE (fixp)
10800 || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
10801 || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
10802}
10803
add55e1f
RS
10804#ifdef OBJ_ELF
10805static int
10806mips_need_elf_addend_fixup (fixP)
10807 fixS *fixP;
10808{
2d2bf3e0
CD
10809 if (S_GET_OTHER (fixP->fx_addsy) == STO_MIPS16)
10810 return 1;
b25a253c
CD
10811 if (mips_pic == EMBEDDED_PIC
10812 && S_IS_WEAK (fixP->fx_addsy))
10813 return 1;
10814 if (mips_pic != EMBEDDED_PIC
10815 && (S_IS_WEAK (fixP->fx_addsy)
10816 || S_IS_EXTERN (fixP->fx_addsy))
2d2bf3e0
CD
10817 && !S_IS_COMMON (fixP->fx_addsy))
10818 return 1;
10819 if (symbol_used_in_reloc_p (fixP->fx_addsy)
10820 && (((bfd_get_section_flags (stdoutput,
10821 S_GET_SEGMENT (fixP->fx_addsy))
10822 & SEC_LINK_ONCE) != 0)
10823 || !strncmp (segment_name (S_GET_SEGMENT (fixP->fx_addsy)),
10824 ".gnu.linkonce",
10825 sizeof (".gnu.linkonce") - 1)))
10826 return 1;
10827 return 0;
add55e1f
RS
10828}
10829#endif
10830
252b5132
RH
10831/* Apply a fixup to the object file. */
10832
94f592af
NC
10833void
10834md_apply_fix3 (fixP, valP, seg)
252b5132 10835 fixS *fixP;
98d3f06f 10836 valueT *valP;
94f592af 10837 segT seg ATTRIBUTE_UNUSED;
252b5132 10838{
874e8986 10839 bfd_byte *buf;
98aa84af
AM
10840 long insn;
10841 valueT value;
252b5132
RH
10842
10843 assert (fixP->fx_size == 4
10844 || fixP->fx_r_type == BFD_RELOC_16
f6688943
TS
10845 || fixP->fx_r_type == BFD_RELOC_32
10846 || fixP->fx_r_type == BFD_RELOC_MIPS_JMP
10847 || fixP->fx_r_type == BFD_RELOC_HI16_S
10848 || fixP->fx_r_type == BFD_RELOC_LO16
10849 || fixP->fx_r_type == BFD_RELOC_GPREL16
76b3015f 10850 || fixP->fx_r_type == BFD_RELOC_MIPS_LITERAL
f6688943 10851 || fixP->fx_r_type == BFD_RELOC_GPREL32
252b5132 10852 || fixP->fx_r_type == BFD_RELOC_64
f6688943
TS
10853 || fixP->fx_r_type == BFD_RELOC_CTOR
10854 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
10855 || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHEST
10856 || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHER
10857 || fixP->fx_r_type == BFD_RELOC_MIPS_SCN_DISP
10858 || fixP->fx_r_type == BFD_RELOC_MIPS_REL16
10859 || fixP->fx_r_type == BFD_RELOC_MIPS_RELGOT
252b5132 10860 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
438c16b8
TS
10861 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
10862 || fixP->fx_r_type == BFD_RELOC_MIPS_JALR);
252b5132 10863
98d3f06f 10864 value = *valP;
252b5132
RH
10865
10866 /* If we aren't adjusting this fixup to be against the section
10867 symbol, we need to adjust the value. */
10868#ifdef OBJ_ELF
10869 if (fixP->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour)
bb2d6cd7 10870 {
add55e1f 10871 if (mips_need_elf_addend_fixup (fixP))
98aa84af
AM
10872 {
10873 valueT symval = S_GET_VALUE (fixP->fx_addsy);
94f592af 10874
98aa84af 10875 value -= symval;
add55e1f 10876 if (value != 0 && ! fixP->fx_pcrel)
98aa84af
AM
10877 {
10878 /* In this case, the bfd_install_relocation routine will
10879 incorrectly add the symbol value back in. We just want
7461da6e 10880 the addend to appear in the object file. */
98aa84af 10881 value -= symval;
7461da6e
RS
10882
10883 /* Make sure the addend is still non-zero. If it became zero
10884 after the last operation, set it to a spurious value and
10885 subtract the same value from the object file's contents. */
10886 if (value == 0)
10887 {
10888 value = 8;
10889
10890 /* The in-place addends for LO16 relocations are signed;
10891 leave the matching HI16 in-place addends as zero. */
10892 if (fixP->fx_r_type != BFD_RELOC_HI16_S)
10893 {
10894 reloc_howto_type *howto;
10895 bfd_vma contents, mask, field;
10896
10897 howto = bfd_reloc_type_lookup (stdoutput,
10898 fixP->fx_r_type);
10899
10900 contents = bfd_get_bits (fixP->fx_frag->fr_literal
10901 + fixP->fx_where,
10902 fixP->fx_size * 8,
10903 target_big_endian);
10904
10905 /* MASK has bits set where the relocation should go.
10906 FIELD is -value, shifted into the appropriate place
10907 for this relocation. */
10908 mask = 1 << (howto->bitsize - 1);
10909 mask = (((mask - 1) << 1) | 1) << howto->bitpos;
10910 field = (-value >> howto->rightshift) << howto->bitpos;
10911
10912 bfd_put_bits ((field & mask) | (contents & ~mask),
10913 fixP->fx_frag->fr_literal + fixP->fx_where,
10914 fixP->fx_size * 8,
10915 target_big_endian);
10916 }
10917 }
98aa84af
AM
10918 }
10919 }
252b5132 10920
bb2d6cd7
GK
10921 /* This code was generated using trial and error and so is
10922 fragile and not trustworthy. If you change it, you should
10923 rerun the elf-rel, elf-rel2, and empic testcases and ensure
10924 they still pass. */
10925 if (fixP->fx_pcrel || fixP->fx_subsy != NULL)
10926 {
10927 value += fixP->fx_frag->fr_address + fixP->fx_where;
10928
10929 /* BFD's REL handling, for MIPS, is _very_ weird.
10930 This gives the right results, but it can't possibly
10931 be the way things are supposed to work. */
cb56d3d3
TS
10932 if ((fixP->fx_r_type != BFD_RELOC_16_PCREL
10933 && fixP->fx_r_type != BFD_RELOC_16_PCREL_S2)
bb2d6cd7
GK
10934 || S_GET_SEGMENT (fixP->fx_addsy) != undefined_section)
10935 value += fixP->fx_frag->fr_address + fixP->fx_where;
10936 }
10937 }
10938#endif
252b5132 10939
94f592af 10940 fixP->fx_addnumber = value; /* Remember value for tc_gen_reloc. */
252b5132
RH
10941
10942 if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel)
10943 fixP->fx_done = 1;
10944
10945 switch (fixP->fx_r_type)
10946 {
10947 case BFD_RELOC_MIPS_JMP:
e369bcce
TS
10948 case BFD_RELOC_MIPS_SHIFT5:
10949 case BFD_RELOC_MIPS_SHIFT6:
10950 case BFD_RELOC_MIPS_GOT_DISP:
10951 case BFD_RELOC_MIPS_GOT_PAGE:
10952 case BFD_RELOC_MIPS_GOT_OFST:
10953 case BFD_RELOC_MIPS_SUB:
10954 case BFD_RELOC_MIPS_INSERT_A:
10955 case BFD_RELOC_MIPS_INSERT_B:
10956 case BFD_RELOC_MIPS_DELETE:
10957 case BFD_RELOC_MIPS_HIGHEST:
10958 case BFD_RELOC_MIPS_HIGHER:
10959 case BFD_RELOC_MIPS_SCN_DISP:
10960 case BFD_RELOC_MIPS_REL16:
10961 case BFD_RELOC_MIPS_RELGOT:
10962 case BFD_RELOC_MIPS_JALR:
252b5132
RH
10963 case BFD_RELOC_HI16:
10964 case BFD_RELOC_HI16_S:
cdf6fd85 10965 case BFD_RELOC_GPREL16:
252b5132
RH
10966 case BFD_RELOC_MIPS_LITERAL:
10967 case BFD_RELOC_MIPS_CALL16:
10968 case BFD_RELOC_MIPS_GOT16:
cdf6fd85 10969 case BFD_RELOC_GPREL32:
252b5132
RH
10970 case BFD_RELOC_MIPS_GOT_HI16:
10971 case BFD_RELOC_MIPS_GOT_LO16:
10972 case BFD_RELOC_MIPS_CALL_HI16:
10973 case BFD_RELOC_MIPS_CALL_LO16:
10974 case BFD_RELOC_MIPS16_GPREL:
10975 if (fixP->fx_pcrel)
10976 as_bad_where (fixP->fx_file, fixP->fx_line,
10977 _("Invalid PC relative reloc"));
10978 /* Nothing needed to do. The value comes from the reloc entry */
10979 break;
10980
10981 case BFD_RELOC_MIPS16_JMP:
10982 /* We currently always generate a reloc against a symbol, which
10983 means that we don't want an addend even if the symbol is
10984 defined. */
10985 fixP->fx_addnumber = 0;
10986 break;
10987
10988 case BFD_RELOC_PCREL_HI16_S:
10989 /* The addend for this is tricky if it is internal, so we just
10990 do everything here rather than in bfd_install_relocation. */
bdaaa2e1 10991 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
bb2d6cd7
GK
10992 && !fixP->fx_done
10993 && value != 0)
10994 break;
10995 if (fixP->fx_addsy
10996 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
252b5132
RH
10997 {
10998 /* For an external symbol adjust by the address to make it
10999 pcrel_offset. We use the address of the RELLO reloc
11000 which follows this one. */
11001 value += (fixP->fx_next->fx_frag->fr_address
11002 + fixP->fx_next->fx_where);
11003 }
e7d556df 11004 value = ((value + 0x8000) >> 16) & 0xffff;
874e8986 11005 buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
252b5132
RH
11006 if (target_big_endian)
11007 buf += 2;
874e8986 11008 md_number_to_chars ((char *) buf, value, 2);
252b5132
RH
11009 break;
11010
11011 case BFD_RELOC_PCREL_LO16:
11012 /* The addend for this is tricky if it is internal, so we just
11013 do everything here rather than in bfd_install_relocation. */
bdaaa2e1 11014 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
bb2d6cd7
GK
11015 && !fixP->fx_done
11016 && value != 0)
11017 break;
11018 if (fixP->fx_addsy
11019 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
252b5132 11020 value += fixP->fx_frag->fr_address + fixP->fx_where;
874e8986 11021 buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
252b5132
RH
11022 if (target_big_endian)
11023 buf += 2;
874e8986 11024 md_number_to_chars ((char *) buf, value, 2);
252b5132
RH
11025 break;
11026
11027 case BFD_RELOC_64:
11028 /* This is handled like BFD_RELOC_32, but we output a sign
11029 extended value if we are only 32 bits. */
11030 if (fixP->fx_done
11031 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11032 {
11033 if (8 <= sizeof (valueT))
11034 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11035 value, 8);
11036 else
11037 {
11038 long w1, w2;
11039 long hiv;
11040
11041 w1 = w2 = fixP->fx_where;
11042 if (target_big_endian)
11043 w1 += 4;
11044 else
11045 w2 += 4;
11046 md_number_to_chars (fixP->fx_frag->fr_literal + w1, value, 4);
11047 if ((value & 0x80000000) != 0)
11048 hiv = 0xffffffff;
11049 else
11050 hiv = 0;
11051 md_number_to_chars (fixP->fx_frag->fr_literal + w2, hiv, 4);
11052 }
11053 }
11054 break;
11055
056350c6 11056 case BFD_RELOC_RVA:
252b5132
RH
11057 case BFD_RELOC_32:
11058 /* If we are deleting this reloc entry, we must fill in the
11059 value now. This can happen if we have a .word which is not
11060 resolved when it appears but is later defined. We also need
11061 to fill in the value if this is an embedded PIC switch table
11062 entry. */
11063 if (fixP->fx_done
11064 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11065 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11066 value, 4);
11067 break;
11068
11069 case BFD_RELOC_16:
11070 /* If we are deleting this reloc entry, we must fill in the
11071 value now. */
11072 assert (fixP->fx_size == 2);
11073 if (fixP->fx_done)
11074 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11075 value, 2);
11076 break;
11077
11078 case BFD_RELOC_LO16:
11079 /* When handling an embedded PIC switch statement, we can wind
11080 up deleting a LO16 reloc. See the 'o' case in mips_ip. */
11081 if (fixP->fx_done)
11082 {
98aa84af 11083 if (value + 0x8000 > 0xffff)
252b5132
RH
11084 as_bad_where (fixP->fx_file, fixP->fx_line,
11085 _("relocation overflow"));
874e8986 11086 buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
252b5132
RH
11087 if (target_big_endian)
11088 buf += 2;
874e8986 11089 md_number_to_chars ((char *) buf, value, 2);
252b5132
RH
11090 }
11091 break;
11092
11093 case BFD_RELOC_16_PCREL_S2:
cb56d3d3
TS
11094 if ((value & 0x3) != 0)
11095 as_bad_where (fixP->fx_file, fixP->fx_line,
11096 _("Branch to odd address (%lx)"), (long) value);
11097
11098 /* Fall through. */
11099
11100 case BFD_RELOC_16_PCREL:
252b5132
RH
11101 /*
11102 * We need to save the bits in the instruction since fixup_segment()
11103 * might be deleting the relocation entry (i.e., a branch within
11104 * the current segment).
11105 */
bb2d6cd7
GK
11106 if (!fixP->fx_done && value != 0)
11107 break;
11108 /* If 'value' is zero, the remaining reloc code won't actually
11109 do the store, so it must be done here. This is probably
11110 a bug somewhere. */
b25a253c
CD
11111 if (!fixP->fx_done
11112 && (fixP->fx_r_type != BFD_RELOC_16_PCREL_S2
11113 || fixP->fx_addsy == NULL /* ??? */
11114 || ! S_IS_DEFINED (fixP->fx_addsy)))
bb2d6cd7 11115 value -= fixP->fx_frag->fr_address + fixP->fx_where;
bdaaa2e1 11116
98aa84af 11117 value = (offsetT) value >> 2;
252b5132
RH
11118
11119 /* update old instruction data */
874e8986 11120 buf = (bfd_byte *) (fixP->fx_where + fixP->fx_frag->fr_literal);
252b5132
RH
11121 if (target_big_endian)
11122 insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
11123 else
11124 insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
11125
98aa84af 11126 if (value + 0x8000 <= 0xffff)
252b5132
RH
11127 insn |= value & 0xffff;
11128 else
11129 {
11130 /* The branch offset is too large. If this is an
11131 unconditional branch, and we are not generating PIC code,
11132 we can convert it to an absolute jump instruction. */
11133 if (mips_pic == NO_PIC
11134 && fixP->fx_done
11135 && fixP->fx_frag->fr_address >= text_section->vma
11136 && (fixP->fx_frag->fr_address
11137 < text_section->vma + text_section->_raw_size)
11138 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
11139 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
11140 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
11141 {
11142 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
11143 insn = 0x0c000000; /* jal */
11144 else
11145 insn = 0x08000000; /* j */
11146 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
11147 fixP->fx_done = 0;
11148 fixP->fx_addsy = section_symbol (text_section);
11149 fixP->fx_addnumber = (value << 2) + md_pcrel_from (fixP);
11150 }
11151 else
11152 {
11153 /* FIXME. It would be possible in principle to handle
11154 conditional branches which overflow. They could be
11155 transformed into a branch around a jump. This would
11156 require setting up variant frags for each different
11157 branch type. The native MIPS assembler attempts to
11158 handle these cases, but it appears to do it
11159 incorrectly. */
11160 as_bad_where (fixP->fx_file, fixP->fx_line,
11161 _("Branch out of range"));
11162 }
11163 }
11164
11165 md_number_to_chars ((char *) buf, (valueT) insn, 4);
11166 break;
11167
11168 case BFD_RELOC_VTABLE_INHERIT:
11169 fixP->fx_done = 0;
11170 if (fixP->fx_addsy
11171 && !S_IS_DEFINED (fixP->fx_addsy)
11172 && !S_IS_WEAK (fixP->fx_addsy))
11173 S_SET_WEAK (fixP->fx_addsy);
11174 break;
11175
11176 case BFD_RELOC_VTABLE_ENTRY:
11177 fixP->fx_done = 0;
11178 break;
11179
11180 default:
11181 internalError ();
11182 }
252b5132
RH
11183}
11184
11185#if 0
11186void
11187printInsn (oc)
11188 unsigned long oc;
11189{
11190 const struct mips_opcode *p;
11191 int treg, sreg, dreg, shamt;
11192 short imm;
11193 const char *args;
11194 int i;
11195
11196 for (i = 0; i < NUMOPCODES; ++i)
11197 {
11198 p = &mips_opcodes[i];
11199 if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
11200 {
11201 printf ("%08lx %s\t", oc, p->name);
11202 treg = (oc >> 16) & 0x1f;
11203 sreg = (oc >> 21) & 0x1f;
11204 dreg = (oc >> 11) & 0x1f;
11205 shamt = (oc >> 6) & 0x1f;
11206 imm = oc;
11207 for (args = p->args;; ++args)
11208 {
11209 switch (*args)
11210 {
11211 case '\0':
11212 printf ("\n");
11213 break;
11214
11215 case ',':
11216 case '(':
11217 case ')':
11218 printf ("%c", *args);
11219 continue;
11220
11221 case 'r':
11222 assert (treg == sreg);
11223 printf ("$%d,$%d", treg, sreg);
11224 continue;
11225
11226 case 'd':
11227 case 'G':
11228 printf ("$%d", dreg);
11229 continue;
11230
11231 case 't':
11232 case 'E':
11233 printf ("$%d", treg);
11234 continue;
11235
11236 case 'k':
11237 printf ("0x%x", treg);
11238 continue;
11239
11240 case 'b':
11241 case 's':
11242 printf ("$%d", sreg);
11243 continue;
11244
11245 case 'a':
11246 printf ("0x%08lx", oc & 0x1ffffff);
11247 continue;
11248
11249 case 'i':
11250 case 'j':
11251 case 'o':
11252 case 'u':
11253 printf ("%d", imm);
11254 continue;
11255
11256 case '<':
11257 case '>':
11258 printf ("$%d", shamt);
11259 continue;
11260
11261 default:
11262 internalError ();
11263 }
11264 break;
11265 }
11266 return;
11267 }
11268 }
11269 printf (_("%08lx UNDEFINED\n"), oc);
11270}
11271#endif
11272
11273static symbolS *
11274get_symbol ()
11275{
11276 int c;
11277 char *name;
11278 symbolS *p;
11279
11280 name = input_line_pointer;
11281 c = get_symbol_end ();
11282 p = (symbolS *) symbol_find_or_make (name);
11283 *input_line_pointer = c;
11284 return p;
11285}
11286
11287/* Align the current frag to a given power of two. The MIPS assembler
11288 also automatically adjusts any preceding label. */
11289
11290static void
11291mips_align (to, fill, label)
11292 int to;
11293 int fill;
11294 symbolS *label;
11295{
11296 mips_emit_delays (false);
11297 frag_align (to, fill, 0);
11298 record_alignment (now_seg, to);
11299 if (label != NULL)
11300 {
11301 assert (S_GET_SEGMENT (label) == now_seg);
49309057 11302 symbol_set_frag (label, frag_now);
252b5132
RH
11303 S_SET_VALUE (label, (valueT) frag_now_fix ());
11304 }
11305}
11306
11307/* Align to a given power of two. .align 0 turns off the automatic
11308 alignment used by the data creating pseudo-ops. */
11309
11310static void
11311s_align (x)
43841e91 11312 int x ATTRIBUTE_UNUSED;
252b5132
RH
11313{
11314 register int temp;
11315 register long temp_fill;
11316 long max_alignment = 15;
11317
11318 /*
11319
11320 o Note that the assembler pulls down any immediately preceeding label
11321 to the aligned address.
11322 o It's not documented but auto alignment is reinstated by
11323 a .align pseudo instruction.
11324 o Note also that after auto alignment is turned off the mips assembler
11325 issues an error on attempt to assemble an improperly aligned data item.
11326 We don't.
11327
11328 */
11329
11330 temp = get_absolute_expression ();
11331 if (temp > max_alignment)
11332 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
11333 else if (temp < 0)
11334 {
11335 as_warn (_("Alignment negative: 0 assumed."));
11336 temp = 0;
11337 }
11338 if (*input_line_pointer == ',')
11339 {
f9419b05 11340 ++input_line_pointer;
252b5132
RH
11341 temp_fill = get_absolute_expression ();
11342 }
11343 else
11344 temp_fill = 0;
11345 if (temp)
11346 {
11347 auto_align = 1;
11348 mips_align (temp, (int) temp_fill,
11349 insn_labels != NULL ? insn_labels->label : NULL);
11350 }
11351 else
11352 {
11353 auto_align = 0;
11354 }
11355
11356 demand_empty_rest_of_line ();
11357}
11358
11359void
11360mips_flush_pending_output ()
11361{
11362 mips_emit_delays (false);
11363 mips_clear_insn_labels ();
11364}
11365
11366static void
11367s_change_sec (sec)
11368 int sec;
11369{
11370 segT seg;
11371
11372 /* When generating embedded PIC code, we only use the .text, .lit8,
11373 .sdata and .sbss sections. We change the .data and .rdata
11374 pseudo-ops to use .sdata. */
11375 if (mips_pic == EMBEDDED_PIC
11376 && (sec == 'd' || sec == 'r'))
11377 sec = 's';
11378
11379#ifdef OBJ_ELF
11380 /* The ELF backend needs to know that we are changing sections, so
11381 that .previous works correctly. We could do something like check
b6ff326e 11382 for an obj_section_change_hook macro, but that might be confusing
252b5132
RH
11383 as it would not be appropriate to use it in the section changing
11384 functions in read.c, since obj-elf.c intercepts those. FIXME:
11385 This should be cleaner, somehow. */
11386 obj_elf_section_change_hook ();
11387#endif
11388
11389 mips_emit_delays (false);
11390 switch (sec)
11391 {
11392 case 't':
11393 s_text (0);
11394 break;
11395 case 'd':
11396 s_data (0);
11397 break;
11398 case 'b':
11399 subseg_set (bss_section, (subsegT) get_absolute_expression ());
11400 demand_empty_rest_of_line ();
11401 break;
11402
11403 case 'r':
11404 if (USE_GLOBAL_POINTER_OPT)
11405 {
11406 seg = subseg_new (RDATA_SECTION_NAME,
11407 (subsegT) get_absolute_expression ());
11408 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11409 {
11410 bfd_set_section_flags (stdoutput, seg,
11411 (SEC_ALLOC
11412 | SEC_LOAD
11413 | SEC_READONLY
11414 | SEC_RELOC
11415 | SEC_DATA));
11416 if (strcmp (TARGET_OS, "elf") != 0)
e799a695 11417 record_alignment (seg, 4);
252b5132
RH
11418 }
11419 demand_empty_rest_of_line ();
11420 }
11421 else
11422 {
11423 as_bad (_("No read only data section in this object file format"));
11424 demand_empty_rest_of_line ();
11425 return;
11426 }
11427 break;
11428
11429 case 's':
11430 if (USE_GLOBAL_POINTER_OPT)
11431 {
11432 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
11433 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11434 {
11435 bfd_set_section_flags (stdoutput, seg,
11436 SEC_ALLOC | SEC_LOAD | SEC_RELOC
11437 | SEC_DATA);
11438 if (strcmp (TARGET_OS, "elf") != 0)
e799a695 11439 record_alignment (seg, 4);
252b5132
RH
11440 }
11441 demand_empty_rest_of_line ();
11442 break;
11443 }
11444 else
11445 {
11446 as_bad (_("Global pointers not supported; recompile -G 0"));
11447 demand_empty_rest_of_line ();
11448 return;
11449 }
11450 }
11451
11452 auto_align = 1;
11453}
11454
11455void
11456mips_enable_auto_align ()
11457{
11458 auto_align = 1;
11459}
11460
11461static void
11462s_cons (log_size)
11463 int log_size;
11464{
11465 symbolS *label;
11466
11467 label = insn_labels != NULL ? insn_labels->label : NULL;
11468 mips_emit_delays (false);
11469 if (log_size > 0 && auto_align)
11470 mips_align (log_size, 0, label);
11471 mips_clear_insn_labels ();
11472 cons (1 << log_size);
11473}
11474
11475static void
11476s_float_cons (type)
11477 int type;
11478{
11479 symbolS *label;
11480
11481 label = insn_labels != NULL ? insn_labels->label : NULL;
11482
11483 mips_emit_delays (false);
11484
11485 if (auto_align)
49309057
ILT
11486 {
11487 if (type == 'd')
11488 mips_align (3, 0, label);
11489 else
11490 mips_align (2, 0, label);
11491 }
252b5132
RH
11492
11493 mips_clear_insn_labels ();
11494
11495 float_cons (type);
11496}
11497
11498/* Handle .globl. We need to override it because on Irix 5 you are
11499 permitted to say
11500 .globl foo .text
11501 where foo is an undefined symbol, to mean that foo should be
11502 considered to be the address of a function. */
11503
11504static void
11505s_mips_globl (x)
43841e91 11506 int x ATTRIBUTE_UNUSED;
252b5132
RH
11507{
11508 char *name;
11509 int c;
11510 symbolS *symbolP;
11511 flagword flag;
11512
11513 name = input_line_pointer;
11514 c = get_symbol_end ();
11515 symbolP = symbol_find_or_make (name);
11516 *input_line_pointer = c;
11517 SKIP_WHITESPACE ();
11518
11519 /* On Irix 5, every global symbol that is not explicitly labelled as
11520 being a function is apparently labelled as being an object. */
11521 flag = BSF_OBJECT;
11522
11523 if (! is_end_of_line[(unsigned char) *input_line_pointer])
11524 {
11525 char *secname;
11526 asection *sec;
11527
11528 secname = input_line_pointer;
11529 c = get_symbol_end ();
11530 sec = bfd_get_section_by_name (stdoutput, secname);
11531 if (sec == NULL)
11532 as_bad (_("%s: no such section"), secname);
11533 *input_line_pointer = c;
11534
11535 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
11536 flag = BSF_FUNCTION;
11537 }
11538
49309057 11539 symbol_get_bfdsym (symbolP)->flags |= flag;
252b5132
RH
11540
11541 S_SET_EXTERNAL (symbolP);
11542 demand_empty_rest_of_line ();
11543}
11544
11545static void
11546s_option (x)
43841e91 11547 int x ATTRIBUTE_UNUSED;
252b5132
RH
11548{
11549 char *opt;
11550 char c;
11551
11552 opt = input_line_pointer;
11553 c = get_symbol_end ();
11554
11555 if (*opt == 'O')
11556 {
11557 /* FIXME: What does this mean? */
11558 }
11559 else if (strncmp (opt, "pic", 3) == 0)
11560 {
11561 int i;
11562
11563 i = atoi (opt + 3);
11564 if (i == 0)
11565 mips_pic = NO_PIC;
11566 else if (i == 2)
11567 mips_pic = SVR4_PIC;
11568 else
11569 as_bad (_(".option pic%d not supported"), i);
11570
11571 if (USE_GLOBAL_POINTER_OPT && mips_pic == SVR4_PIC)
11572 {
11573 if (g_switch_seen && g_switch_value != 0)
11574 as_warn (_("-G may not be used with SVR4 PIC code"));
11575 g_switch_value = 0;
11576 bfd_set_gp_size (stdoutput, 0);
11577 }
11578 }
11579 else
11580 as_warn (_("Unrecognized option \"%s\""), opt);
11581
11582 *input_line_pointer = c;
11583 demand_empty_rest_of_line ();
11584}
11585
11586/* This structure is used to hold a stack of .set values. */
11587
e972090a
NC
11588struct mips_option_stack
11589{
252b5132
RH
11590 struct mips_option_stack *next;
11591 struct mips_set_options options;
11592};
11593
11594static struct mips_option_stack *mips_opts_stack;
11595
11596/* Handle the .set pseudo-op. */
11597
11598static void
11599s_mipsset (x)
43841e91 11600 int x ATTRIBUTE_UNUSED;
252b5132
RH
11601{
11602 char *name = input_line_pointer, ch;
11603
11604 while (!is_end_of_line[(unsigned char) *input_line_pointer])
f9419b05 11605 ++input_line_pointer;
252b5132
RH
11606 ch = *input_line_pointer;
11607 *input_line_pointer = '\0';
11608
11609 if (strcmp (name, "reorder") == 0)
11610 {
11611 if (mips_opts.noreorder && prev_nop_frag != NULL)
11612 {
11613 /* If we still have pending nops, we can discard them. The
11614 usual nop handling will insert any that are still
bdaaa2e1 11615 needed. */
252b5132
RH
11616 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11617 * (mips_opts.mips16 ? 2 : 4));
11618 prev_nop_frag = NULL;
11619 }
11620 mips_opts.noreorder = 0;
11621 }
11622 else if (strcmp (name, "noreorder") == 0)
11623 {
11624 mips_emit_delays (true);
11625 mips_opts.noreorder = 1;
11626 mips_any_noreorder = 1;
11627 }
11628 else if (strcmp (name, "at") == 0)
11629 {
11630 mips_opts.noat = 0;
11631 }
11632 else if (strcmp (name, "noat") == 0)
11633 {
11634 mips_opts.noat = 1;
11635 }
11636 else if (strcmp (name, "macro") == 0)
11637 {
11638 mips_opts.warn_about_macros = 0;
11639 }
11640 else if (strcmp (name, "nomacro") == 0)
11641 {
11642 if (mips_opts.noreorder == 0)
11643 as_bad (_("`noreorder' must be set before `nomacro'"));
11644 mips_opts.warn_about_macros = 1;
11645 }
11646 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
11647 {
11648 mips_opts.nomove = 0;
11649 }
11650 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
11651 {
11652 mips_opts.nomove = 1;
11653 }
11654 else if (strcmp (name, "bopt") == 0)
11655 {
11656 mips_opts.nobopt = 0;
11657 }
11658 else if (strcmp (name, "nobopt") == 0)
11659 {
11660 mips_opts.nobopt = 1;
11661 }
deec1734
CD
11662 else if (strcmp (name, "mdmx") == 0)
11663 mips_opts.ase_mdmx = 1;
11664 else if (strcmp (name, "nomdmx") == 0)
11665 mips_opts.ase_mdmx = 0;
252b5132
RH
11666 else if (strcmp (name, "mips16") == 0
11667 || strcmp (name, "MIPS-16") == 0)
11668 mips_opts.mips16 = 1;
11669 else if (strcmp (name, "nomips16") == 0
11670 || strcmp (name, "noMIPS-16") == 0)
11671 mips_opts.mips16 = 0;
1f25f5d3
CD
11672 else if (strcmp (name, "mips3d") == 0)
11673 mips_opts.ase_mips3d = 1;
11674 else if (strcmp (name, "nomips3d") == 0)
11675 mips_opts.ase_mips3d = 0;
252b5132
RH
11676 else if (strncmp (name, "mips", 4) == 0)
11677 {
11678 int isa;
11679
11680 /* Permit the user to change the ISA on the fly. Needless to
11681 say, misuse can cause serious problems. */
11682 isa = atoi (name + 4);
553178e4 11683 switch (isa)
98d3f06f
KH
11684 {
11685 case 0:
11686 mips_opts.gp32 = file_mips_gp32;
11687 mips_opts.fp32 = file_mips_fp32;
11688 mips_opts.abi = file_mips_abi;
11689 break;
11690 case 1:
11691 case 2:
11692 case 32:
11693 mips_opts.gp32 = 1;
11694 mips_opts.fp32 = 1;
11695 break;
11696 case 3:
11697 case 4:
11698 case 5:
11699 case 64:
11700 /* Loosen ABI register width restriction. */
11701 if (mips_opts.abi == O32_ABI)
11702 mips_opts.abi = NO_ABI;
11703 mips_opts.gp32 = 0;
11704 mips_opts.fp32 = 0;
11705 break;
11706 default:
11707 as_bad (_("unknown ISA level %s"), name + 4);
11708 break;
11709 }
553178e4 11710
e7af610e 11711 switch (isa)
98d3f06f
KH
11712 {
11713 case 0: mips_opts.isa = file_mips_isa; break;
11714 case 1: mips_opts.isa = ISA_MIPS1; break;
11715 case 2: mips_opts.isa = ISA_MIPS2; break;
11716 case 3: mips_opts.isa = ISA_MIPS3; break;
11717 case 4: mips_opts.isa = ISA_MIPS4; break;
11718 case 5: mips_opts.isa = ISA_MIPS5; break;
11719 case 32: mips_opts.isa = ISA_MIPS32; break;
11720 case 64: mips_opts.isa = ISA_MIPS64; break;
11721 default: as_bad (_("unknown ISA level %s"), name + 4); break;
11722 }
252b5132
RH
11723 }
11724 else if (strcmp (name, "autoextend") == 0)
11725 mips_opts.noautoextend = 0;
11726 else if (strcmp (name, "noautoextend") == 0)
11727 mips_opts.noautoextend = 1;
11728 else if (strcmp (name, "push") == 0)
11729 {
11730 struct mips_option_stack *s;
11731
11732 s = (struct mips_option_stack *) xmalloc (sizeof *s);
11733 s->next = mips_opts_stack;
11734 s->options = mips_opts;
11735 mips_opts_stack = s;
11736 }
11737 else if (strcmp (name, "pop") == 0)
11738 {
11739 struct mips_option_stack *s;
11740
11741 s = mips_opts_stack;
11742 if (s == NULL)
11743 as_bad (_(".set pop with no .set push"));
11744 else
11745 {
11746 /* If we're changing the reorder mode we need to handle
11747 delay slots correctly. */
11748 if (s->options.noreorder && ! mips_opts.noreorder)
11749 mips_emit_delays (true);
11750 else if (! s->options.noreorder && mips_opts.noreorder)
11751 {
11752 if (prev_nop_frag != NULL)
11753 {
11754 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11755 * (mips_opts.mips16 ? 2 : 4));
11756 prev_nop_frag = NULL;
11757 }
11758 }
11759
11760 mips_opts = s->options;
11761 mips_opts_stack = s->next;
11762 free (s);
11763 }
11764 }
11765 else
11766 {
11767 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
11768 }
11769 *input_line_pointer = ch;
11770 demand_empty_rest_of_line ();
11771}
11772
11773/* Handle the .abicalls pseudo-op. I believe this is equivalent to
11774 .option pic2. It means to generate SVR4 PIC calls. */
11775
11776static void
11777s_abicalls (ignore)
43841e91 11778 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
11779{
11780 mips_pic = SVR4_PIC;
11781 if (USE_GLOBAL_POINTER_OPT)
11782 {
11783 if (g_switch_seen && g_switch_value != 0)
11784 as_warn (_("-G may not be used with SVR4 PIC code"));
11785 g_switch_value = 0;
11786 }
11787 bfd_set_gp_size (stdoutput, 0);
11788 demand_empty_rest_of_line ();
11789}
11790
11791/* Handle the .cpload pseudo-op. This is used when generating SVR4
11792 PIC code. It sets the $gp register for the function based on the
11793 function address, which is in the register named in the argument.
11794 This uses a relocation against _gp_disp, which is handled specially
11795 by the linker. The result is:
11796 lui $gp,%hi(_gp_disp)
11797 addiu $gp,$gp,%lo(_gp_disp)
11798 addu $gp,$gp,.cpload argument
11799 The .cpload argument is normally $25 == $t9. */
11800
11801static void
11802s_cpload (ignore)
43841e91 11803 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
11804{
11805 expressionS ex;
11806 int icnt = 0;
11807
6478892d
TS
11808 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
11809 .cpload is ignored. */
11810 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
11811 {
11812 s_ignore (0);
11813 return;
11814 }
11815
d3ecfc59 11816 /* .cpload should be in a .set noreorder section. */
252b5132
RH
11817 if (mips_opts.noreorder == 0)
11818 as_warn (_(".cpload not in noreorder section"));
11819
11820 ex.X_op = O_symbol;
11821 ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
11822 ex.X_op_symbol = NULL;
11823 ex.X_add_number = 0;
11824
11825 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
49309057 11826 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
252b5132 11827
c9914766
TS
11828 macro_build_lui (NULL, &icnt, &ex, mips_gp_register);
11829 macro_build ((char *) NULL, &icnt, &ex, "addiu", "t,r,j",
11830 mips_gp_register, mips_gp_register, (int) BFD_RELOC_LO16);
252b5132
RH
11831
11832 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "addu", "d,v,t",
c9914766 11833 mips_gp_register, mips_gp_register, tc_get_register (0));
252b5132
RH
11834
11835 demand_empty_rest_of_line ();
11836}
11837
6478892d
TS
11838/* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
11839 .cpsetup $reg1, offset|$reg2, label
11840
11841 If offset is given, this results in:
11842 sd $gp, offset($sp)
956cd1d6 11843 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
11844 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
11845 daddu $gp, $gp, $reg1
6478892d
TS
11846
11847 If $reg2 is given, this results in:
11848 daddu $reg2, $gp, $0
956cd1d6 11849 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
11850 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
11851 daddu $gp, $gp, $reg1
11852 $reg1 is normally $25 == $t9. */
6478892d
TS
11853static void
11854s_cpsetup (ignore)
11855 int ignore ATTRIBUTE_UNUSED;
11856{
11857 expressionS ex_off;
11858 expressionS ex_sym;
11859 int reg1;
11860 int icnt = 0;
11861 char *sym;
11862
8586fc66 11863 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
6478892d
TS
11864 We also need NewABI support. */
11865 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11866 {
11867 s_ignore (0);
11868 return;
11869 }
11870
11871 reg1 = tc_get_register (0);
11872 SKIP_WHITESPACE ();
11873 if (*input_line_pointer != ',')
11874 {
11875 as_bad (_("missing argument separator ',' for .cpsetup"));
11876 return;
11877 }
11878 else
80245285 11879 ++input_line_pointer;
6478892d
TS
11880 SKIP_WHITESPACE ();
11881 if (*input_line_pointer == '$')
80245285
TS
11882 {
11883 mips_cpreturn_register = tc_get_register (0);
11884 mips_cpreturn_offset = -1;
11885 }
6478892d 11886 else
80245285
TS
11887 {
11888 mips_cpreturn_offset = get_absolute_expression ();
11889 mips_cpreturn_register = -1;
11890 }
6478892d
TS
11891 SKIP_WHITESPACE ();
11892 if (*input_line_pointer != ',')
11893 {
11894 as_bad (_("missing argument separator ',' for .cpsetup"));
11895 return;
11896 }
11897 else
f9419b05 11898 ++input_line_pointer;
6478892d
TS
11899 SKIP_WHITESPACE ();
11900 sym = input_line_pointer;
11901 while (ISALNUM (*input_line_pointer))
f9419b05 11902 ++input_line_pointer;
6478892d
TS
11903 *input_line_pointer = 0;
11904
11905 ex_sym.X_op = O_symbol;
11906 ex_sym.X_add_symbol = symbol_find_or_make (sym);
11907 ex_sym.X_op_symbol = NULL;
11908 ex_sym.X_add_number = 0;
11909
11910 if (mips_cpreturn_register == -1)
11911 {
11912 ex_off.X_op = O_constant;
11913 ex_off.X_add_symbol = NULL;
11914 ex_off.X_op_symbol = NULL;
11915 ex_off.X_add_number = mips_cpreturn_offset;
11916
11917 macro_build ((char *) NULL, &icnt, &ex_off, "sd", "t,o(b)",
11918 mips_gp_register, (int) BFD_RELOC_LO16, SP);
11919 }
11920 else
11921 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
11922 "d,v,t", mips_cpreturn_register, mips_gp_register, 0);
11923
11924 macro_build ((char *) NULL, &icnt, &ex_sym, "lui", "t,u", mips_gp_register,
11925 (int) BFD_RELOC_GPREL16);
8586fc66
TS
11926 fix_new (frag_now, prev_insn_where, 0, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
11927 fix_new (frag_now, prev_insn_where, 0, NULL, 0, 0, BFD_RELOC_HI16_S);
6478892d
TS
11928 macro_build ((char *) NULL, &icnt, &ex_sym, "addiu", "t,r,j",
11929 mips_gp_register, mips_gp_register, (int) BFD_RELOC_GPREL16);
8586fc66
TS
11930 fix_new (frag_now, prev_insn_where, 0, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
11931 fix_new (frag_now, prev_insn_where, 0, NULL, 0, 0, BFD_RELOC_LO16);
11932 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
11933 HAVE_64BIT_ADDRESSES ? "daddu" : "addu", "d,v,t",
11934 mips_gp_register, mips_gp_register, reg1);
6478892d
TS
11935
11936 demand_empty_rest_of_line ();
11937}
11938
11939static void
11940s_cplocal (ignore)
11941 int ignore ATTRIBUTE_UNUSED;
11942{
11943 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
11944 .cplocal is ignored. */
11945 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11946 {
11947 s_ignore (0);
11948 return;
11949 }
11950
11951 mips_gp_register = tc_get_register (0);
11952}
11953
252b5132
RH
11954/* Handle the .cprestore pseudo-op. This stores $gp into a given
11955 offset from $sp. The offset is remembered, and after making a PIC
11956 call $gp is restored from that location. */
11957
11958static void
11959s_cprestore (ignore)
43841e91 11960 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
11961{
11962 expressionS ex;
11963 int icnt = 0;
11964
6478892d 11965 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
c9914766 11966 .cprestore is ignored. */
6478892d 11967 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
11968 {
11969 s_ignore (0);
11970 return;
11971 }
11972
11973 mips_cprestore_offset = get_absolute_expression ();
7a621144 11974 mips_cprestore_valid = 1;
252b5132
RH
11975
11976 ex.X_op = O_constant;
11977 ex.X_add_symbol = NULL;
11978 ex.X_op_symbol = NULL;
11979 ex.X_add_number = mips_cprestore_offset;
11980
c9914766
TS
11981 macro_build ((char *) NULL, &icnt, &ex, HAVE_32BIT_ADDRESSES ? "sw" : "sd",
11982 "t,o(b)", mips_gp_register, (int) BFD_RELOC_LO16, SP);
252b5132
RH
11983
11984 demand_empty_rest_of_line ();
11985}
11986
6478892d
TS
11987/* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
11988 was given in the preceeding .gpsetup, it results in:
11989 ld $gp, offset($sp)
76b3015f 11990
6478892d
TS
11991 If a register $reg2 was given there, it results in:
11992 daddiu $gp, $gp, $reg2
11993 */
11994static void
11995s_cpreturn (ignore)
11996 int ignore ATTRIBUTE_UNUSED;
11997{
11998 expressionS ex;
11999 int icnt = 0;
12000
12001 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
12002 We also need NewABI support. */
12003 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12004 {
12005 s_ignore (0);
12006 return;
12007 }
12008
12009 if (mips_cpreturn_register == -1)
12010 {
12011 ex.X_op = O_constant;
12012 ex.X_add_symbol = NULL;
12013 ex.X_op_symbol = NULL;
12014 ex.X_add_number = mips_cpreturn_offset;
12015
12016 macro_build ((char *) NULL, &icnt, &ex, "ld", "t,o(b)",
12017 mips_gp_register, (int) BFD_RELOC_LO16, SP);
12018 }
12019 else
12020 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
12021 "d,v,t", mips_gp_register, mips_cpreturn_register, 0);
12022
12023 demand_empty_rest_of_line ();
12024}
12025
12026/* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
12027 code. It sets the offset to use in gp_rel relocations. */
12028
12029static void
12030s_gpvalue (ignore)
12031 int ignore ATTRIBUTE_UNUSED;
12032{
12033 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
12034 We also need NewABI support. */
12035 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12036 {
12037 s_ignore (0);
12038 return;
12039 }
12040
def2e0dd 12041 mips_gprel_offset = get_absolute_expression ();
6478892d
TS
12042
12043 demand_empty_rest_of_line ();
12044}
12045
252b5132
RH
12046/* Handle the .gpword pseudo-op. This is used when generating PIC
12047 code. It generates a 32 bit GP relative reloc. */
12048
12049static void
12050s_gpword (ignore)
43841e91 12051 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
12052{
12053 symbolS *label;
12054 expressionS ex;
12055 char *p;
12056
12057 /* When not generating PIC code, this is treated as .word. */
12058 if (mips_pic != SVR4_PIC)
12059 {
12060 s_cons (2);
12061 return;
12062 }
12063
12064 label = insn_labels != NULL ? insn_labels->label : NULL;
12065 mips_emit_delays (true);
12066 if (auto_align)
12067 mips_align (2, 0, label);
12068 mips_clear_insn_labels ();
12069
12070 expression (&ex);
12071
12072 if (ex.X_op != O_symbol || ex.X_add_number != 0)
12073 {
12074 as_bad (_("Unsupported use of .gpword"));
12075 ignore_rest_of_line ();
12076 }
12077
12078 p = frag_more (4);
12079 md_number_to_chars (p, (valueT) 0, 4);
c9914766 12080 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, false,
cdf6fd85 12081 BFD_RELOC_GPREL32);
252b5132
RH
12082
12083 demand_empty_rest_of_line ();
12084}
12085
12086/* Handle the .cpadd pseudo-op. This is used when dealing with switch
12087 tables in SVR4 PIC code. */
12088
12089static void
12090s_cpadd (ignore)
43841e91 12091 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
12092{
12093 int icnt = 0;
12094 int reg;
12095
6478892d
TS
12096 /* This is ignored when not generating SVR4 PIC code or if this is NewABI
12097 code. */
12098 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
12099 {
12100 s_ignore (0);
12101 return;
12102 }
12103
12104 /* Add $gp to the register named as an argument. */
12105 reg = tc_get_register (0);
12106 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 12107 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 12108 "d,v,t", reg, reg, mips_gp_register);
252b5132 12109
bdaaa2e1 12110 demand_empty_rest_of_line ();
252b5132
RH
12111}
12112
12113/* Handle the .insn pseudo-op. This marks instruction labels in
12114 mips16 mode. This permits the linker to handle them specially,
12115 such as generating jalx instructions when needed. We also make
12116 them odd for the duration of the assembly, in order to generate the
12117 right sort of code. We will make them even in the adjust_symtab
12118 routine, while leaving them marked. This is convenient for the
12119 debugger and the disassembler. The linker knows to make them odd
12120 again. */
12121
12122static void
12123s_insn (ignore)
43841e91 12124 int ignore ATTRIBUTE_UNUSED;
252b5132 12125{
f9419b05 12126 mips16_mark_labels ();
252b5132
RH
12127
12128 demand_empty_rest_of_line ();
12129}
12130
12131/* Handle a .stabn directive. We need these in order to mark a label
12132 as being a mips16 text label correctly. Sometimes the compiler
12133 will emit a label, followed by a .stabn, and then switch sections.
12134 If the label and .stabn are in mips16 mode, then the label is
12135 really a mips16 text label. */
12136
12137static void
12138s_mips_stab (type)
12139 int type;
12140{
f9419b05 12141 if (type == 'n')
252b5132
RH
12142 mips16_mark_labels ();
12143
12144 s_stab (type);
12145}
12146
12147/* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
12148 */
12149
12150static void
12151s_mips_weakext (ignore)
43841e91 12152 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
12153{
12154 char *name;
12155 int c;
12156 symbolS *symbolP;
12157 expressionS exp;
12158
12159 name = input_line_pointer;
12160 c = get_symbol_end ();
12161 symbolP = symbol_find_or_make (name);
12162 S_SET_WEAK (symbolP);
12163 *input_line_pointer = c;
12164
12165 SKIP_WHITESPACE ();
12166
12167 if (! is_end_of_line[(unsigned char) *input_line_pointer])
12168 {
12169 if (S_IS_DEFINED (symbolP))
12170 {
956cd1d6 12171 as_bad ("ignoring attempt to redefine symbol %s",
252b5132
RH
12172 S_GET_NAME (symbolP));
12173 ignore_rest_of_line ();
12174 return;
12175 }
bdaaa2e1 12176
252b5132
RH
12177 if (*input_line_pointer == ',')
12178 {
12179 ++input_line_pointer;
12180 SKIP_WHITESPACE ();
12181 }
bdaaa2e1 12182
252b5132
RH
12183 expression (&exp);
12184 if (exp.X_op != O_symbol)
12185 {
12186 as_bad ("bad .weakext directive");
98d3f06f 12187 ignore_rest_of_line ();
252b5132
RH
12188 return;
12189 }
49309057 12190 symbol_set_value_expression (symbolP, &exp);
252b5132
RH
12191 }
12192
12193 demand_empty_rest_of_line ();
12194}
12195
12196/* Parse a register string into a number. Called from the ECOFF code
12197 to parse .frame. The argument is non-zero if this is the frame
12198 register, so that we can record it in mips_frame_reg. */
12199
12200int
12201tc_get_register (frame)
12202 int frame;
12203{
12204 int reg;
12205
12206 SKIP_WHITESPACE ();
12207 if (*input_line_pointer++ != '$')
12208 {
12209 as_warn (_("expected `$'"));
12210 reg = 0;
12211 }
3882b010 12212 else if (ISDIGIT (*input_line_pointer))
252b5132
RH
12213 {
12214 reg = get_absolute_expression ();
12215 if (reg < 0 || reg >= 32)
12216 {
12217 as_warn (_("Bad register number"));
12218 reg = 0;
12219 }
12220 }
12221 else
12222 {
76db943d
TS
12223 if (strncmp (input_line_pointer, "ra", 2) == 0)
12224 reg = RA;
12225 else if (strncmp (input_line_pointer, "fp", 2) == 0)
252b5132
RH
12226 reg = FP;
12227 else if (strncmp (input_line_pointer, "sp", 2) == 0)
12228 reg = SP;
12229 else if (strncmp (input_line_pointer, "gp", 2) == 0)
12230 reg = GP;
12231 else if (strncmp (input_line_pointer, "at", 2) == 0)
12232 reg = AT;
12233 else
12234 {
12235 as_warn (_("Unrecognized register name"));
12236 reg = 0;
12237 }
12238 input_line_pointer += 2;
12239 }
12240 if (frame)
7a621144
DJ
12241 {
12242 mips_frame_reg = reg != 0 ? reg : SP;
12243 mips_frame_reg_valid = 1;
12244 mips_cprestore_valid = 0;
12245 }
252b5132
RH
12246 return reg;
12247}
12248
12249valueT
12250md_section_align (seg, addr)
12251 asection *seg;
12252 valueT addr;
12253{
12254 int align = bfd_get_section_alignment (stdoutput, seg);
12255
12256#ifdef OBJ_ELF
12257 /* We don't need to align ELF sections to the full alignment.
12258 However, Irix 5 may prefer that we align them at least to a 16
12259 byte boundary. We don't bother to align the sections if we are
12260 targeted for an embedded system. */
12261 if (strcmp (TARGET_OS, "elf") == 0)
12262 return addr;
12263 if (align > 4)
12264 align = 4;
12265#endif
12266
12267 return ((addr + (1 << align) - 1) & (-1 << align));
12268}
12269
12270/* Utility routine, called from above as well. If called while the
12271 input file is still being read, it's only an approximation. (For
12272 example, a symbol may later become defined which appeared to be
12273 undefined earlier.) */
12274
12275static int
12276nopic_need_relax (sym, before_relaxing)
12277 symbolS *sym;
12278 int before_relaxing;
12279{
12280 if (sym == 0)
12281 return 0;
12282
6478892d 12283 if (USE_GLOBAL_POINTER_OPT && g_switch_value > 0)
252b5132
RH
12284 {
12285 const char *symname;
12286 int change;
12287
c9914766 12288 /* Find out whether this symbol can be referenced off the $gp
252b5132
RH
12289 register. It can be if it is smaller than the -G size or if
12290 it is in the .sdata or .sbss section. Certain symbols can
c9914766 12291 not be referenced off the $gp, although it appears as though
252b5132
RH
12292 they can. */
12293 symname = S_GET_NAME (sym);
12294 if (symname != (const char *) NULL
12295 && (strcmp (symname, "eprol") == 0
12296 || strcmp (symname, "etext") == 0
12297 || strcmp (symname, "_gp") == 0
12298 || strcmp (symname, "edata") == 0
12299 || strcmp (symname, "_fbss") == 0
12300 || strcmp (symname, "_fdata") == 0
12301 || strcmp (symname, "_ftext") == 0
12302 || strcmp (symname, "end") == 0
12303 || strcmp (symname, "_gp_disp") == 0))
12304 change = 1;
12305 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
12306 && (0
12307#ifndef NO_ECOFF_DEBUGGING
49309057
ILT
12308 || (symbol_get_obj (sym)->ecoff_extern_size != 0
12309 && (symbol_get_obj (sym)->ecoff_extern_size
12310 <= g_switch_value))
252b5132
RH
12311#endif
12312 /* We must defer this decision until after the whole
12313 file has been read, since there might be a .extern
12314 after the first use of this symbol. */
12315 || (before_relaxing
12316#ifndef NO_ECOFF_DEBUGGING
49309057 12317 && symbol_get_obj (sym)->ecoff_extern_size == 0
252b5132
RH
12318#endif
12319 && S_GET_VALUE (sym) == 0)
12320 || (S_GET_VALUE (sym) != 0
12321 && S_GET_VALUE (sym) <= g_switch_value)))
12322 change = 0;
12323 else
12324 {
12325 const char *segname;
12326
12327 segname = segment_name (S_GET_SEGMENT (sym));
12328 assert (strcmp (segname, ".lit8") != 0
12329 && strcmp (segname, ".lit4") != 0);
12330 change = (strcmp (segname, ".sdata") != 0
fba2b7f9
GK
12331 && strcmp (segname, ".sbss") != 0
12332 && strncmp (segname, ".sdata.", 7) != 0
12333 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
252b5132
RH
12334 }
12335 return change;
12336 }
12337 else
c9914766 12338 /* We are not optimizing for the $gp register. */
252b5132
RH
12339 return 1;
12340}
12341
12342/* Given a mips16 variant frag FRAGP, return non-zero if it needs an
12343 extended opcode. SEC is the section the frag is in. */
12344
12345static int
12346mips16_extended_frag (fragp, sec, stretch)
12347 fragS *fragp;
12348 asection *sec;
12349 long stretch;
12350{
12351 int type;
12352 register const struct mips16_immed_operand *op;
12353 offsetT val;
12354 int mintiny, maxtiny;
12355 segT symsec;
98aa84af 12356 fragS *sym_frag;
252b5132
RH
12357
12358 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
12359 return 0;
12360 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
12361 return 1;
12362
12363 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12364 op = mips16_immed_operands;
12365 while (op->type != type)
12366 {
12367 ++op;
12368 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
12369 }
12370
12371 if (op->unsp)
12372 {
12373 if (type == '<' || type == '>' || type == '[' || type == ']')
12374 {
12375 mintiny = 1;
12376 maxtiny = 1 << op->nbits;
12377 }
12378 else
12379 {
12380 mintiny = 0;
12381 maxtiny = (1 << op->nbits) - 1;
12382 }
12383 }
12384 else
12385 {
12386 mintiny = - (1 << (op->nbits - 1));
12387 maxtiny = (1 << (op->nbits - 1)) - 1;
12388 }
12389
98aa84af 12390 sym_frag = symbol_get_frag (fragp->fr_symbol);
ac62c346 12391 val = S_GET_VALUE (fragp->fr_symbol);
98aa84af 12392 symsec = S_GET_SEGMENT (fragp->fr_symbol);
252b5132
RH
12393
12394 if (op->pcrel)
12395 {
12396 addressT addr;
12397
12398 /* We won't have the section when we are called from
12399 mips_relax_frag. However, we will always have been called
12400 from md_estimate_size_before_relax first. If this is a
12401 branch to a different section, we mark it as such. If SEC is
12402 NULL, and the frag is not marked, then it must be a branch to
12403 the same section. */
12404 if (sec == NULL)
12405 {
12406 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
12407 return 1;
12408 }
12409 else
12410 {
98aa84af 12411 /* Must have been called from md_estimate_size_before_relax. */
252b5132
RH
12412 if (symsec != sec)
12413 {
12414 fragp->fr_subtype =
12415 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12416
12417 /* FIXME: We should support this, and let the linker
12418 catch branches and loads that are out of range. */
12419 as_bad_where (fragp->fr_file, fragp->fr_line,
12420 _("unsupported PC relative reference to different section"));
12421
12422 return 1;
12423 }
98aa84af
AM
12424 if (fragp != sym_frag && sym_frag->fr_address == 0)
12425 /* Assume non-extended on the first relaxation pass.
12426 The address we have calculated will be bogus if this is
12427 a forward branch to another frag, as the forward frag
12428 will have fr_address == 0. */
12429 return 0;
252b5132
RH
12430 }
12431
12432 /* In this case, we know for sure that the symbol fragment is in
98aa84af
AM
12433 the same section. If the relax_marker of the symbol fragment
12434 differs from the relax_marker of this fragment, we have not
12435 yet adjusted the symbol fragment fr_address. We want to add
252b5132
RH
12436 in STRETCH in order to get a better estimate of the address.
12437 This particularly matters because of the shift bits. */
12438 if (stretch != 0
98aa84af 12439 && sym_frag->relax_marker != fragp->relax_marker)
252b5132
RH
12440 {
12441 fragS *f;
12442
12443 /* Adjust stretch for any alignment frag. Note that if have
12444 been expanding the earlier code, the symbol may be
12445 defined in what appears to be an earlier frag. FIXME:
12446 This doesn't handle the fr_subtype field, which specifies
12447 a maximum number of bytes to skip when doing an
12448 alignment. */
98aa84af 12449 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
252b5132
RH
12450 {
12451 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
12452 {
12453 if (stretch < 0)
12454 stretch = - ((- stretch)
12455 & ~ ((1 << (int) f->fr_offset) - 1));
12456 else
12457 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
12458 if (stretch == 0)
12459 break;
12460 }
12461 }
12462 if (f != NULL)
12463 val += stretch;
12464 }
12465
12466 addr = fragp->fr_address + fragp->fr_fix;
12467
12468 /* The base address rules are complicated. The base address of
12469 a branch is the following instruction. The base address of a
12470 PC relative load or add is the instruction itself, but if it
12471 is in a delay slot (in which case it can not be extended) use
12472 the address of the instruction whose delay slot it is in. */
12473 if (type == 'p' || type == 'q')
12474 {
12475 addr += 2;
12476
12477 /* If we are currently assuming that this frag should be
12478 extended, then, the current address is two bytes
bdaaa2e1 12479 higher. */
252b5132
RH
12480 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12481 addr += 2;
12482
12483 /* Ignore the low bit in the target, since it will be set
12484 for a text label. */
12485 if ((val & 1) != 0)
12486 --val;
12487 }
12488 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12489 addr -= 4;
12490 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12491 addr -= 2;
12492
12493 val -= addr & ~ ((1 << op->shift) - 1);
12494
12495 /* Branch offsets have an implicit 0 in the lowest bit. */
12496 if (type == 'p' || type == 'q')
12497 val /= 2;
12498
12499 /* If any of the shifted bits are set, we must use an extended
12500 opcode. If the address depends on the size of this
12501 instruction, this can lead to a loop, so we arrange to always
12502 use an extended opcode. We only check this when we are in
12503 the main relaxation loop, when SEC is NULL. */
12504 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
12505 {
12506 fragp->fr_subtype =
12507 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12508 return 1;
12509 }
12510
12511 /* If we are about to mark a frag as extended because the value
12512 is precisely maxtiny + 1, then there is a chance of an
12513 infinite loop as in the following code:
12514 la $4,foo
12515 .skip 1020
12516 .align 2
12517 foo:
12518 In this case when the la is extended, foo is 0x3fc bytes
12519 away, so the la can be shrunk, but then foo is 0x400 away, so
12520 the la must be extended. To avoid this loop, we mark the
12521 frag as extended if it was small, and is about to become
12522 extended with a value of maxtiny + 1. */
12523 if (val == ((maxtiny + 1) << op->shift)
12524 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
12525 && sec == NULL)
12526 {
12527 fragp->fr_subtype =
12528 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12529 return 1;
12530 }
12531 }
12532 else if (symsec != absolute_section && sec != NULL)
12533 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
12534
12535 if ((val & ((1 << op->shift) - 1)) != 0
12536 || val < (mintiny << op->shift)
12537 || val > (maxtiny << op->shift))
12538 return 1;
12539 else
12540 return 0;
12541}
12542
12543/* Estimate the size of a frag before relaxing. Unless this is the
12544 mips16, we are not really relaxing here, and the final size is
12545 encoded in the subtype information. For the mips16, we have to
12546 decide whether we are using an extended opcode or not. */
12547
252b5132
RH
12548int
12549md_estimate_size_before_relax (fragp, segtype)
12550 fragS *fragp;
12551 asection *segtype;
12552{
43841e91 12553 int change = 0;
8614eeee 12554 boolean linkonce = false;
252b5132
RH
12555
12556 if (RELAX_MIPS16_P (fragp->fr_subtype))
177b4a6a
AO
12557 /* We don't want to modify the EXTENDED bit here; it might get us
12558 into infinite loops. We change it only in mips_relax_frag(). */
12559 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
252b5132
RH
12560
12561 if (mips_pic == NO_PIC)
12562 {
12563 change = nopic_need_relax (fragp->fr_symbol, 0);
12564 }
12565 else if (mips_pic == SVR4_PIC)
12566 {
12567 symbolS *sym;
12568 asection *symsec;
12569
12570 sym = fragp->fr_symbol;
12571
12572 /* Handle the case of a symbol equated to another symbol. */
e0890092 12573 while (symbol_equated_reloc_p (sym))
252b5132
RH
12574 {
12575 symbolS *n;
12576
12577 /* It's possible to get a loop here in a badly written
12578 program. */
49309057 12579 n = symbol_get_value_expression (sym)->X_add_symbol;
252b5132
RH
12580 if (n == sym)
12581 break;
12582 sym = n;
12583 }
12584
12585 symsec = S_GET_SEGMENT (sym);
12586
8614eeee
UC
12587 /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
12588 if (symsec != segtype && ! S_IS_LOCAL (sym))
beae10d5
KH
12589 {
12590 if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
12591 != 0)
12592 linkonce = true;
12593
12594 /* The GNU toolchain uses an extension for ELF: a section
12595 beginning with the magic string .gnu.linkonce is a linkonce
12596 section. */
12597 if (strncmp (segment_name (symsec), ".gnu.linkonce",
12598 sizeof ".gnu.linkonce" - 1) == 0)
12599 linkonce = true;
12600 }
8614eeee 12601
252b5132
RH
12602 /* This must duplicate the test in adjust_reloc_syms. */
12603 change = (symsec != &bfd_und_section
12604 && symsec != &bfd_abs_section
426b0403 12605 && ! bfd_is_com_section (symsec)
8614eeee 12606 && !linkonce
426b0403 12607#ifdef OBJ_ELF
ea4ff978 12608 /* A global or weak symbol is treated as external. */
9151e8bf 12609 && (OUTPUT_FLAVOR != bfd_target_elf_flavour
b25a253c
CD
12610 || (! S_IS_WEAK (sym)
12611 && (! S_IS_EXTERN (sym) || mips_pic == EMBEDDED_PIC)))
426b0403
AM
12612#endif
12613 );
252b5132
RH
12614 }
12615 else
12616 abort ();
12617
12618 if (change)
12619 {
12620 /* Record the offset to the first reloc in the fr_opcode field.
12621 This lets md_convert_frag and tc_gen_reloc know that the code
12622 must be expanded. */
12623 fragp->fr_opcode = (fragp->fr_literal
12624 + fragp->fr_fix
12625 - RELAX_OLD (fragp->fr_subtype)
12626 + RELAX_RELOC1 (fragp->fr_subtype));
12627 /* FIXME: This really needs as_warn_where. */
12628 if (RELAX_WARN (fragp->fr_subtype))
9a41af64
TS
12629 as_warn (_("AT used after \".set noat\" or macro used after "
12630 "\".set nomacro\""));
12631
12632 return RELAX_NEW (fragp->fr_subtype) - RELAX_OLD (fragp->fr_subtype);
252b5132
RH
12633 }
12634
9a41af64 12635 return 0;
252b5132
RH
12636}
12637
12638/* This is called to see whether a reloc against a defined symbol
12639 should be converted into a reloc against a section. Don't adjust
12640 MIPS16 jump relocations, so we don't have to worry about the format
12641 of the offset in the .o file. Don't adjust relocations against
12642 mips16 symbols, so that the linker can find them if it needs to set
12643 up a stub. */
12644
12645int
12646mips_fix_adjustable (fixp)
12647 fixS *fixp;
12648{
ea4ff978
L
12649#ifdef OBJ_ELF
12650 /* Prevent all adjustments to global symbols. */
46bac6de 12651 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
b25a253c 12652 && mips_pic != EMBEDDED_PIC
46bac6de 12653 && (S_IS_EXTERN (fixp->fx_addsy) || S_IS_WEAK (fixp->fx_addsy)))
ea4ff978
L
12654 return 0;
12655#endif
252b5132
RH
12656 if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
12657 return 0;
12658 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
12659 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12660 return 0;
12661 if (fixp->fx_addsy == NULL)
12662 return 1;
12663#ifdef OBJ_ELF
12664 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
12665 && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
12666 && fixp->fx_subsy == NULL)
12667 return 0;
12668#endif
12669 return 1;
12670}
12671
12672/* Translate internal representation of relocation info to BFD target
12673 format. */
12674
12675arelent **
12676tc_gen_reloc (section, fixp)
43841e91 12677 asection *section ATTRIBUTE_UNUSED;
252b5132
RH
12678 fixS *fixp;
12679{
12680 static arelent *retval[4];
12681 arelent *reloc;
12682 bfd_reloc_code_real_type code;
12683
12684 reloc = retval[0] = (arelent *) xmalloc (sizeof (arelent));
12685 retval[1] = NULL;
12686
49309057
ILT
12687 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
12688 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
12689 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
12690
12691 if (mips_pic == EMBEDDED_PIC
12692 && SWITCH_TABLE (fixp))
12693 {
12694 /* For a switch table entry we use a special reloc. The addend
12695 is actually the difference between the reloc address and the
12696 subtrahend. */
12697 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
12698 if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
12699 as_fatal (_("Double check fx_r_type in tc-mips.c:tc_gen_reloc"));
12700 fixp->fx_r_type = BFD_RELOC_GPREL32;
12701 }
12702 else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16)
12703 {
4514d474
CD
12704 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
12705 reloc->addend = fixp->fx_addnumber;
252b5132 12706 else
4514d474
CD
12707 {
12708 /* We use a special addend for an internal RELLO reloc. */
12709 if (symbol_section_p (fixp->fx_addsy))
12710 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
12711 else
12712 reloc->addend = fixp->fx_addnumber + reloc->address;
12713 }
252b5132
RH
12714 }
12715 else if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
12716 {
12717 assert (fixp->fx_next != NULL
12718 && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
4514d474
CD
12719
12720 /* The reloc is relative to the RELLO; adjust the addend
252b5132 12721 accordingly. */
4514d474
CD
12722 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
12723 reloc->addend = fixp->fx_next->fx_addnumber;
252b5132 12724 else
4514d474
CD
12725 {
12726 /* We use a special addend for an internal RELHI reloc. */
12727 if (symbol_section_p (fixp->fx_addsy))
12728 reloc->addend = (fixp->fx_next->fx_frag->fr_address
12729 + fixp->fx_next->fx_where
12730 - S_GET_VALUE (fixp->fx_subsy));
12731 else
12732 reloc->addend = (fixp->fx_addnumber
12733 + fixp->fx_next->fx_frag->fr_address
12734 + fixp->fx_next->fx_where);
12735 }
252b5132 12736 }
4514d474
CD
12737 else if (fixp->fx_pcrel == 0 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
12738 reloc->addend = fixp->fx_addnumber;
252b5132
RH
12739 else
12740 {
12741 if (OUTPUT_FLAVOR != bfd_target_aout_flavour)
12742 /* A gruesome hack which is a result of the gruesome gas reloc
12743 handling. */
12744 reloc->addend = reloc->address;
12745 else
12746 reloc->addend = -reloc->address;
12747 }
12748
12749 /* If this is a variant frag, we may need to adjust the existing
12750 reloc and generate a new one. */
12751 if (fixp->fx_frag->fr_opcode != NULL
cdf6fd85 12752 && (fixp->fx_r_type == BFD_RELOC_GPREL16
252b5132
RH
12753 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
12754 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL16
12755 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
12756 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16
12757 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
6478892d
TS
12758 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_LO16)
12759 && ! HAVE_NEWABI)
252b5132
RH
12760 {
12761 arelent *reloc2;
12762
12763 assert (! RELAX_MIPS16_P (fixp->fx_frag->fr_subtype));
12764
12765 /* If this is not the last reloc in this frag, then we have two
12766 GPREL relocs, or a GOT_HI16/GOT_LO16 pair, or a
12767 CALL_HI16/CALL_LO16, both of which are being replaced. Let
12768 the second one handle all of them. */
12769 if (fixp->fx_next != NULL
12770 && fixp->fx_frag == fixp->fx_next->fx_frag)
12771 {
cdf6fd85
TS
12772 assert ((fixp->fx_r_type == BFD_RELOC_GPREL16
12773 && fixp->fx_next->fx_r_type == BFD_RELOC_GPREL16)
252b5132
RH
12774 || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
12775 && (fixp->fx_next->fx_r_type
12776 == BFD_RELOC_MIPS_GOT_LO16))
12777 || (fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
12778 && (fixp->fx_next->fx_r_type
12779 == BFD_RELOC_MIPS_CALL_LO16)));
12780 retval[0] = NULL;
12781 return retval;
12782 }
12783
12784 fixp->fx_where = fixp->fx_frag->fr_opcode - fixp->fx_frag->fr_literal;
12785 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
12786 reloc2 = retval[1] = (arelent *) xmalloc (sizeof (arelent));
12787 retval[2] = NULL;
49309057
ILT
12788 reloc2->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
12789 *reloc2->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
12790 reloc2->address = (reloc->address
12791 + (RELAX_RELOC2 (fixp->fx_frag->fr_subtype)
12792 - RELAX_RELOC1 (fixp->fx_frag->fr_subtype)));
12793 reloc2->addend = fixp->fx_addnumber;
12794 reloc2->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO16);
12795 assert (reloc2->howto != NULL);
12796
12797 if (RELAX_RELOC3 (fixp->fx_frag->fr_subtype))
12798 {
12799 arelent *reloc3;
12800
12801 reloc3 = retval[2] = (arelent *) xmalloc (sizeof (arelent));
12802 retval[3] = NULL;
12803 *reloc3 = *reloc2;
12804 reloc3->address += 4;
12805 }
12806
12807 if (mips_pic == NO_PIC)
12808 {
cdf6fd85 12809 assert (fixp->fx_r_type == BFD_RELOC_GPREL16);
252b5132
RH
12810 fixp->fx_r_type = BFD_RELOC_HI16_S;
12811 }
12812 else if (mips_pic == SVR4_PIC)
12813 {
12814 switch (fixp->fx_r_type)
12815 {
12816 default:
12817 abort ();
12818 case BFD_RELOC_MIPS_GOT16:
12819 break;
12820 case BFD_RELOC_MIPS_CALL16:
12821 case BFD_RELOC_MIPS_GOT_LO16:
12822 case BFD_RELOC_MIPS_CALL_LO16:
12823 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
12824 break;
12825 }
12826 }
12827 else
12828 abort ();
12829 }
12830
438c16b8
TS
12831 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
12832 entry to be used in the relocation's section offset. */
12833 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
252b5132
RH
12834 {
12835 reloc->address = reloc->addend;
12836 reloc->addend = 0;
12837 }
12838
12839 /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
12840 fixup_segment converted a non-PC relative reloc into a PC
12841 relative reloc. In such a case, we need to convert the reloc
12842 code. */
12843 code = fixp->fx_r_type;
12844 if (fixp->fx_pcrel)
12845 {
12846 switch (code)
12847 {
12848 case BFD_RELOC_8:
12849 code = BFD_RELOC_8_PCREL;
12850 break;
12851 case BFD_RELOC_16:
12852 code = BFD_RELOC_16_PCREL;
12853 break;
12854 case BFD_RELOC_32:
12855 code = BFD_RELOC_32_PCREL;
12856 break;
12857 case BFD_RELOC_64:
12858 code = BFD_RELOC_64_PCREL;
12859 break;
12860 case BFD_RELOC_8_PCREL:
12861 case BFD_RELOC_16_PCREL:
12862 case BFD_RELOC_32_PCREL:
12863 case BFD_RELOC_64_PCREL:
12864 case BFD_RELOC_16_PCREL_S2:
12865 case BFD_RELOC_PCREL_HI16_S:
12866 case BFD_RELOC_PCREL_LO16:
12867 break;
12868 default:
12869 as_bad_where (fixp->fx_file, fixp->fx_line,
12870 _("Cannot make %s relocation PC relative"),
12871 bfd_get_reloc_code_name (code));
12872 }
12873 }
12874
add55e1f
RS
12875#ifdef OBJ_ELF
12876 /* md_apply_fix3 has a double-subtraction hack to get
12877 bfd_install_relocation to behave nicely. GPREL relocations are
12878 handled correctly without this hack, so undo it here. We can't
12879 stop md_apply_fix3 from subtracting twice in the first place since
12880 the fake addend is required for variant frags above. */
12881 if (fixp->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour
d11008e1 12882 && code == BFD_RELOC_GPREL16
add55e1f
RS
12883 && reloc->addend != 0
12884 && mips_need_elf_addend_fixup (fixp))
12885 reloc->addend += S_GET_VALUE (fixp->fx_addsy);
12886#endif
12887
252b5132
RH
12888 /* To support a PC relative reloc when generating embedded PIC code
12889 for ECOFF, we use a Cygnus extension. We check for that here to
12890 make sure that we don't let such a reloc escape normally. */
bb2d6cd7
GK
12891 if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
12892 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
252b5132
RH
12893 && code == BFD_RELOC_16_PCREL_S2
12894 && mips_pic != EMBEDDED_PIC)
12895 reloc->howto = NULL;
12896 else
12897 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
12898
12899 if (reloc->howto == NULL)
12900 {
12901 as_bad_where (fixp->fx_file, fixp->fx_line,
12902 _("Can not represent %s relocation in this object file format"),
12903 bfd_get_reloc_code_name (code));
12904 retval[0] = NULL;
12905 }
12906
12907 return retval;
12908}
12909
12910/* Relax a machine dependent frag. This returns the amount by which
12911 the current size of the frag should change. */
12912
12913int
12914mips_relax_frag (fragp, stretch)
12915 fragS *fragp;
12916 long stretch;
12917{
12918 if (! RELAX_MIPS16_P (fragp->fr_subtype))
12919 return 0;
12920
c4e7957c 12921 if (mips16_extended_frag (fragp, NULL, stretch))
252b5132
RH
12922 {
12923 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12924 return 0;
12925 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
12926 return 2;
12927 }
12928 else
12929 {
12930 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12931 return 0;
12932 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
12933 return -2;
12934 }
12935
12936 return 0;
12937}
12938
12939/* Convert a machine dependent frag. */
12940
12941void
12942md_convert_frag (abfd, asec, fragp)
43841e91 12943 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
12944 segT asec;
12945 fragS *fragp;
12946{
12947 int old, new;
12948 char *fixptr;
12949
12950 if (RELAX_MIPS16_P (fragp->fr_subtype))
12951 {
12952 int type;
12953 register const struct mips16_immed_operand *op;
12954 boolean small, ext;
12955 offsetT val;
12956 bfd_byte *buf;
12957 unsigned long insn;
12958 boolean use_extend;
12959 unsigned short extend;
12960
12961 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12962 op = mips16_immed_operands;
12963 while (op->type != type)
12964 ++op;
12965
12966 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12967 {
12968 small = false;
12969 ext = true;
12970 }
12971 else
12972 {
12973 small = true;
12974 ext = false;
12975 }
12976
6386f3a7 12977 resolve_symbol_value (fragp->fr_symbol);
252b5132
RH
12978 val = S_GET_VALUE (fragp->fr_symbol);
12979 if (op->pcrel)
12980 {
12981 addressT addr;
12982
12983 addr = fragp->fr_address + fragp->fr_fix;
12984
12985 /* The rules for the base address of a PC relative reloc are
12986 complicated; see mips16_extended_frag. */
12987 if (type == 'p' || type == 'q')
12988 {
12989 addr += 2;
12990 if (ext)
12991 addr += 2;
12992 /* Ignore the low bit in the target, since it will be
12993 set for a text label. */
12994 if ((val & 1) != 0)
12995 --val;
12996 }
12997 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12998 addr -= 4;
12999 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13000 addr -= 2;
13001
13002 addr &= ~ (addressT) ((1 << op->shift) - 1);
13003 val -= addr;
13004
13005 /* Make sure the section winds up with the alignment we have
13006 assumed. */
13007 if (op->shift > 0)
13008 record_alignment (asec, op->shift);
13009 }
13010
13011 if (ext
13012 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
13013 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
13014 as_warn_where (fragp->fr_file, fragp->fr_line,
13015 _("extended instruction in delay slot"));
13016
13017 buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
13018
13019 if (target_big_endian)
13020 insn = bfd_getb16 (buf);
13021 else
13022 insn = bfd_getl16 (buf);
13023
13024 mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
13025 RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
13026 small, ext, &insn, &use_extend, &extend);
13027
13028 if (use_extend)
13029 {
874e8986 13030 md_number_to_chars ((char *) buf, 0xf000 | extend, 2);
252b5132
RH
13031 fragp->fr_fix += 2;
13032 buf += 2;
13033 }
13034
874e8986 13035 md_number_to_chars ((char *) buf, insn, 2);
252b5132
RH
13036 fragp->fr_fix += 2;
13037 buf += 2;
13038 }
13039 else
13040 {
13041 if (fragp->fr_opcode == NULL)
13042 return;
13043
13044 old = RELAX_OLD (fragp->fr_subtype);
13045 new = RELAX_NEW (fragp->fr_subtype);
13046 fixptr = fragp->fr_literal + fragp->fr_fix;
13047
13048 if (new > 0)
13049 memcpy (fixptr - old, fixptr, new);
13050
13051 fragp->fr_fix += new - old;
13052 }
13053}
13054
13055#ifdef OBJ_ELF
13056
13057/* This function is called after the relocs have been generated.
13058 We've been storing mips16 text labels as odd. Here we convert them
13059 back to even for the convenience of the debugger. */
13060
13061void
13062mips_frob_file_after_relocs ()
13063{
13064 asymbol **syms;
13065 unsigned int count, i;
13066
13067 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
13068 return;
13069
13070 syms = bfd_get_outsymbols (stdoutput);
13071 count = bfd_get_symcount (stdoutput);
13072 for (i = 0; i < count; i++, syms++)
13073 {
13074 if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
13075 && ((*syms)->value & 1) != 0)
13076 {
13077 (*syms)->value &= ~1;
13078 /* If the symbol has an odd size, it was probably computed
13079 incorrectly, so adjust that as well. */
13080 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
13081 ++elf_symbol (*syms)->internal_elf_sym.st_size;
13082 }
13083 }
13084}
13085
13086#endif
13087
13088/* This function is called whenever a label is defined. It is used
13089 when handling branch delays; if a branch has a label, we assume we
13090 can not move it. */
13091
13092void
13093mips_define_label (sym)
13094 symbolS *sym;
13095{
13096 struct insn_label_list *l;
13097
13098 if (free_insn_labels == NULL)
13099 l = (struct insn_label_list *) xmalloc (sizeof *l);
13100 else
13101 {
13102 l = free_insn_labels;
13103 free_insn_labels = l->next;
13104 }
13105
13106 l->label = sym;
13107 l->next = insn_labels;
13108 insn_labels = l;
13109}
13110\f
13111#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13112
13113/* Some special processing for a MIPS ELF file. */
13114
13115void
13116mips_elf_final_processing ()
13117{
13118 /* Write out the register information. */
195325d2 13119 if (file_mips_abi != N64_ABI)
252b5132
RH
13120 {
13121 Elf32_RegInfo s;
13122
13123 s.ri_gprmask = mips_gprmask;
13124 s.ri_cprmask[0] = mips_cprmask[0];
13125 s.ri_cprmask[1] = mips_cprmask[1];
13126 s.ri_cprmask[2] = mips_cprmask[2];
13127 s.ri_cprmask[3] = mips_cprmask[3];
13128 /* The gp_value field is set by the MIPS ELF backend. */
13129
13130 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
13131 ((Elf32_External_RegInfo *)
13132 mips_regmask_frag));
13133 }
13134 else
13135 {
13136 Elf64_Internal_RegInfo s;
13137
13138 s.ri_gprmask = mips_gprmask;
13139 s.ri_pad = 0;
13140 s.ri_cprmask[0] = mips_cprmask[0];
13141 s.ri_cprmask[1] = mips_cprmask[1];
13142 s.ri_cprmask[2] = mips_cprmask[2];
13143 s.ri_cprmask[3] = mips_cprmask[3];
13144 /* The gp_value field is set by the MIPS ELF backend. */
13145
13146 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
13147 ((Elf64_External_RegInfo *)
13148 mips_regmask_frag));
13149 }
13150
13151 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
13152 sort of BFD interface for this. */
13153 if (mips_any_noreorder)
13154 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
13155 if (mips_pic != NO_PIC)
13156 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
13157
98d3f06f 13158 /* Set MIPS ELF flags for ASEs. */
1f25f5d3
CD
13159#if 0 /* XXX FIXME */
13160 if (file_ase_mips3d)
13161 elf_elfheader (stdoutput)->e_flags |= ???;
13162#endif
deec1734
CD
13163 if (file_ase_mdmx)
13164 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
1f25f5d3 13165
bdaaa2e1 13166 /* Set the MIPS ELF ABI flags. */
a325df1d 13167 if (file_mips_abi == NO_ABI)
252b5132 13168 ;
a325df1d 13169 else if (file_mips_abi == O32_ABI)
252b5132 13170 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
a325df1d 13171 else if (file_mips_abi == O64_ABI)
252b5132 13172 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
a325df1d 13173 else if (file_mips_abi == EABI_ABI)
252b5132
RH
13174 {
13175 if (mips_eabi64)
13176 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
13177 else
13178 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
13179 }
a325df1d 13180 else if (file_mips_abi == N32_ABI)
be00bddd
TS
13181 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
13182
c9914766 13183 /* Nothing to do for N64_ABI. */
252b5132
RH
13184
13185 if (mips_32bitmode)
13186 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
13187}
13188
13189#endif /* OBJ_ELF || OBJ_MAYBE_ELF */
13190\f
beae10d5
KH
13191typedef struct proc {
13192 symbolS *isym;
13193 unsigned long reg_mask;
13194 unsigned long reg_offset;
13195 unsigned long fpreg_mask;
13196 unsigned long fpreg_offset;
13197 unsigned long frame_offset;
13198 unsigned long frame_reg;
13199 unsigned long pc_reg;
13200} procS;
252b5132
RH
13201
13202static procS cur_proc;
13203static procS *cur_proc_ptr;
13204static int numprocs;
13205
0a9ef439 13206/* Fill in an rs_align_code fragment. */
a19d8eb0 13207
0a9ef439
RH
13208void
13209mips_handle_align (fragp)
13210 fragS *fragp;
a19d8eb0 13211{
0a9ef439
RH
13212 if (fragp->fr_type != rs_align_code)
13213 return;
13214
13215 if (mips_opts.mips16)
a19d8eb0
CP
13216 {
13217 static const unsigned char be_nop[] = { 0x65, 0x00 };
13218 static const unsigned char le_nop[] = { 0x00, 0x65 };
13219
0a9ef439
RH
13220 int bytes;
13221 char *p;
a19d8eb0 13222
0a9ef439
RH
13223 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
13224 p = fragp->fr_literal + fragp->fr_fix;
13225
13226 if (bytes & 1)
13227 {
13228 *p++ = 0;
f9419b05 13229 fragp->fr_fix++;
0a9ef439
RH
13230 }
13231
13232 memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
13233 fragp->fr_var = 2;
a19d8eb0
CP
13234 }
13235
0a9ef439 13236 /* For mips32, a nop is a zero, which we trivially get by doing nothing. */
a19d8eb0
CP
13237}
13238
252b5132
RH
13239static void
13240md_obj_begin ()
13241{
13242}
13243
13244static void
13245md_obj_end ()
13246{
13247 /* check for premature end, nesting errors, etc */
13248 if (cur_proc_ptr)
9a41af64 13249 as_warn (_("missing .end at end of assembly"));
252b5132
RH
13250}
13251
13252static long
13253get_number ()
13254{
13255 int negative = 0;
13256 long val = 0;
13257
13258 if (*input_line_pointer == '-')
13259 {
13260 ++input_line_pointer;
13261 negative = 1;
13262 }
3882b010 13263 if (!ISDIGIT (*input_line_pointer))
956cd1d6 13264 as_bad (_("expected simple number"));
252b5132
RH
13265 if (input_line_pointer[0] == '0')
13266 {
13267 if (input_line_pointer[1] == 'x')
13268 {
13269 input_line_pointer += 2;
3882b010 13270 while (ISXDIGIT (*input_line_pointer))
252b5132
RH
13271 {
13272 val <<= 4;
13273 val |= hex_value (*input_line_pointer++);
13274 }
13275 return negative ? -val : val;
13276 }
13277 else
13278 {
13279 ++input_line_pointer;
3882b010 13280 while (ISDIGIT (*input_line_pointer))
252b5132
RH
13281 {
13282 val <<= 3;
13283 val |= *input_line_pointer++ - '0';
13284 }
13285 return negative ? -val : val;
13286 }
13287 }
3882b010 13288 if (!ISDIGIT (*input_line_pointer))
252b5132
RH
13289 {
13290 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
13291 *input_line_pointer, *input_line_pointer);
956cd1d6 13292 as_warn (_("invalid number"));
252b5132
RH
13293 return -1;
13294 }
3882b010 13295 while (ISDIGIT (*input_line_pointer))
252b5132
RH
13296 {
13297 val *= 10;
13298 val += *input_line_pointer++ - '0';
13299 }
13300 return negative ? -val : val;
13301}
13302
13303/* The .file directive; just like the usual .file directive, but there
13304 is an initial number which is the ECOFF file index. */
13305
13306static void
13307s_file (x)
43841e91 13308 int x ATTRIBUTE_UNUSED;
252b5132 13309{
874e8986 13310 get_number ();
252b5132
RH
13311 s_app_file (0);
13312}
13313
252b5132
RH
13314/* The .end directive. */
13315
13316static void
13317s_mips_end (x)
43841e91 13318 int x ATTRIBUTE_UNUSED;
252b5132
RH
13319{
13320 symbolS *p;
13321 int maybe_text;
13322
7a621144
DJ
13323 /* Following functions need their own .frame and .cprestore directives. */
13324 mips_frame_reg_valid = 0;
13325 mips_cprestore_valid = 0;
13326
252b5132
RH
13327 if (!is_end_of_line[(unsigned char) *input_line_pointer])
13328 {
13329 p = get_symbol ();
13330 demand_empty_rest_of_line ();
13331 }
13332 else
13333 p = NULL;
13334
13335#ifdef BFD_ASSEMBLER
13336 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
13337 maybe_text = 1;
13338 else
13339 maybe_text = 0;
13340#else
13341 if (now_seg != data_section && now_seg != bss_section)
13342 maybe_text = 1;
13343 else
13344 maybe_text = 0;
13345#endif
13346
13347 if (!maybe_text)
13348 as_warn (_(".end not in text section"));
13349
13350 if (!cur_proc_ptr)
13351 {
13352 as_warn (_(".end directive without a preceding .ent directive."));
13353 demand_empty_rest_of_line ();
13354 return;
13355 }
13356
13357 if (p != NULL)
13358 {
13359 assert (S_GET_NAME (p));
13360 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->isym)))
13361 as_warn (_(".end symbol does not match .ent symbol."));
13362 }
13363 else
13364 as_warn (_(".end directive missing or unknown symbol"));
13365
13366#ifdef MIPS_STABS_ELF
13367 {
13368 segT saved_seg = now_seg;
13369 subsegT saved_subseg = now_subseg;
252b5132 13370 valueT dot;
252b5132
RH
13371 expressionS exp;
13372 char *fragp;
13373
13374 dot = frag_now_fix ();
13375
13376#ifdef md_flush_pending_output
13377 md_flush_pending_output ();
13378#endif
13379
13380 assert (pdr_seg);
13381 subseg_set (pdr_seg, 0);
13382
beae10d5 13383 /* Write the symbol. */
252b5132
RH
13384 exp.X_op = O_symbol;
13385 exp.X_add_symbol = p;
13386 exp.X_add_number = 0;
13387 emit_expr (&exp, 4);
13388
beae10d5 13389 fragp = frag_more (7 * 4);
252b5132 13390
beae10d5
KH
13391 md_number_to_chars (fragp, (valueT) cur_proc_ptr->reg_mask, 4);
13392 md_number_to_chars (fragp + 4, (valueT) cur_proc_ptr->reg_offset, 4);
13393 md_number_to_chars (fragp + 8, (valueT) cur_proc_ptr->fpreg_mask, 4);
13394 md_number_to_chars (fragp + 12, (valueT) cur_proc_ptr->fpreg_offset, 4);
13395 md_number_to_chars (fragp + 16, (valueT) cur_proc_ptr->frame_offset, 4);
13396 md_number_to_chars (fragp + 20, (valueT) cur_proc_ptr->frame_reg, 4);
13397 md_number_to_chars (fragp + 24, (valueT) cur_proc_ptr->pc_reg, 4);
252b5132
RH
13398
13399 subseg_set (saved_seg, saved_subseg);
13400 }
f9419b05 13401#endif /* MIPS_STABS_ELF */
252b5132
RH
13402
13403 cur_proc_ptr = NULL;
13404}
13405
13406/* The .aent and .ent directives. */
13407
13408static void
13409s_mips_ent (aent)
13410 int aent;
13411{
252b5132
RH
13412 symbolS *symbolP;
13413 int maybe_text;
13414
13415 symbolP = get_symbol ();
13416 if (*input_line_pointer == ',')
f9419b05 13417 ++input_line_pointer;
252b5132 13418 SKIP_WHITESPACE ();
3882b010 13419 if (ISDIGIT (*input_line_pointer)
d9a62219 13420 || *input_line_pointer == '-')
874e8986 13421 get_number ();
252b5132
RH
13422
13423#ifdef BFD_ASSEMBLER
13424 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
13425 maybe_text = 1;
13426 else
13427 maybe_text = 0;
13428#else
13429 if (now_seg != data_section && now_seg != bss_section)
13430 maybe_text = 1;
13431 else
13432 maybe_text = 0;
13433#endif
13434
13435 if (!maybe_text)
13436 as_warn (_(".ent or .aent not in text section."));
13437
13438 if (!aent && cur_proc_ptr)
9a41af64 13439 as_warn (_("missing .end"));
252b5132
RH
13440
13441 if (!aent)
13442 {
7a621144
DJ
13443 /* This function needs its own .frame and .cprestore directives. */
13444 mips_frame_reg_valid = 0;
13445 mips_cprestore_valid = 0;
13446
252b5132
RH
13447 cur_proc_ptr = &cur_proc;
13448 memset (cur_proc_ptr, '\0', sizeof (procS));
13449
13450 cur_proc_ptr->isym = symbolP;
13451
49309057 13452 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
252b5132 13453
f9419b05 13454 ++numprocs;
252b5132
RH
13455 }
13456
13457 demand_empty_rest_of_line ();
13458}
13459
13460/* The .frame directive. If the mdebug section is present (IRIX 5 native)
bdaaa2e1 13461 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
252b5132 13462 s_mips_frame is used so that we can set the PDR information correctly.
bdaaa2e1 13463 We can't use the ecoff routines because they make reference to the ecoff
252b5132
RH
13464 symbol table (in the mdebug section). */
13465
13466static void
13467s_mips_frame (ignore)
2b3c5a5d 13468 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
13469{
13470#ifdef MIPS_STABS_ELF
13471
13472 long val;
13473
beae10d5 13474 if (cur_proc_ptr == (procS *) NULL)
252b5132
RH
13475 {
13476 as_warn (_(".frame outside of .ent"));
13477 demand_empty_rest_of_line ();
13478 return;
13479 }
13480
13481 cur_proc_ptr->frame_reg = tc_get_register (1);
13482
13483 SKIP_WHITESPACE ();
13484 if (*input_line_pointer++ != ','
13485 || get_absolute_expression_and_terminator (&val) != ',')
13486 {
13487 as_warn (_("Bad .frame directive"));
13488 --input_line_pointer;
13489 demand_empty_rest_of_line ();
13490 return;
13491 }
13492
13493 cur_proc_ptr->frame_offset = val;
13494 cur_proc_ptr->pc_reg = tc_get_register (0);
13495
13496 demand_empty_rest_of_line ();
13497#else
13498 s_ignore (ignore);
13499#endif /* MIPS_STABS_ELF */
13500}
13501
bdaaa2e1
KH
13502/* The .fmask and .mask directives. If the mdebug section is present
13503 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
252b5132 13504 embedded targets, s_mips_mask is used so that we can set the PDR
bdaaa2e1 13505 information correctly. We can't use the ecoff routines because they
252b5132
RH
13506 make reference to the ecoff symbol table (in the mdebug section). */
13507
13508static void
13509s_mips_mask (reg_type)
13510 char reg_type;
13511{
13512#ifdef MIPS_STABS_ELF
13513 long mask, off;
bdaaa2e1 13514
252b5132
RH
13515 if (cur_proc_ptr == (procS *) NULL)
13516 {
13517 as_warn (_(".mask/.fmask outside of .ent"));
13518 demand_empty_rest_of_line ();
13519 return;
13520 }
13521
13522 if (get_absolute_expression_and_terminator (&mask) != ',')
13523 {
13524 as_warn (_("Bad .mask/.fmask directive"));
13525 --input_line_pointer;
13526 demand_empty_rest_of_line ();
13527 return;
13528 }
13529
13530 off = get_absolute_expression ();
13531
13532 if (reg_type == 'F')
13533 {
13534 cur_proc_ptr->fpreg_mask = mask;
13535 cur_proc_ptr->fpreg_offset = off;
13536 }
13537 else
13538 {
13539 cur_proc_ptr->reg_mask = mask;
13540 cur_proc_ptr->reg_offset = off;
13541 }
13542
13543 demand_empty_rest_of_line ();
13544#else
13545 s_ignore (reg_type);
13546#endif /* MIPS_STABS_ELF */
13547}
13548
13549/* The .loc directive. */
13550
13551#if 0
13552static void
13553s_loc (x)
13554 int x;
13555{
13556 symbolS *symbolP;
13557 int lineno;
13558 int addroff;
13559
13560 assert (now_seg == text_section);
13561
13562 lineno = get_number ();
13563 addroff = frag_now_fix ();
13564
13565 symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
13566 S_SET_TYPE (symbolP, N_SLINE);
13567 S_SET_OTHER (symbolP, 0);
13568 S_SET_DESC (symbolP, lineno);
13569 symbolP->sy_segment = now_seg;
13570}
13571#endif
e7af610e
NC
13572
13573/* CPU name/ISA/number mapping table.
13574
13575 Entries are grouped by type. The first matching CPU or ISA entry
13576 gets chosen by CPU or ISA, so it should be the 'canonical' name
13577 for that type. Entries after that within the type are sorted
13578 alphabetically.
13579
13580 Case is ignored in comparison, so put the canonical entry in the
13581 appropriate case but everything else in lower case to ease eye pain. */
e972090a
NC
13582static const struct mips_cpu_info mips_cpu_info_table[] =
13583{
e7af610e
NC
13584 /* MIPS1 ISA */
13585 { "MIPS1", 1, ISA_MIPS1, CPU_R3000, },
13586 { "mips", 1, ISA_MIPS1, CPU_R3000, },
13587
13588 /* MIPS2 ISA */
13589 { "MIPS2", 1, ISA_MIPS2, CPU_R6000, },
13590
13591 /* MIPS3 ISA */
13592 { "MIPS3", 1, ISA_MIPS3, CPU_R4000, },
13593
13594 /* MIPS4 ISA */
13595 { "MIPS4", 1, ISA_MIPS4, CPU_R8000, },
13596
84ea6cf2
NC
13597 /* MIPS5 ISA */
13598 { "MIPS5", 1, ISA_MIPS5, CPU_MIPS5, },
13599 { "Generic-MIPS5", 0, ISA_MIPS5, CPU_MIPS5, },
13600
e7af610e
NC
13601 /* MIPS32 ISA */
13602 { "MIPS32", 1, ISA_MIPS32, CPU_MIPS32, },
3c02b2ab 13603 { "mipsisa32", 0, ISA_MIPS32, CPU_MIPS32, },
e7af610e 13604 { "Generic-MIPS32", 0, ISA_MIPS32, CPU_MIPS32, },
3c02b2ab
EC
13605 { "4kc", 0, ISA_MIPS32, CPU_MIPS32, },
13606 { "4km", 0, ISA_MIPS32, CPU_MIPS32, },
13607 { "4kp", 0, ISA_MIPS32, CPU_MIPS32, },
13608
13609 /* For historical reasons. */
13610 { "MIPS64", 1, ISA_MIPS3, CPU_R4000, },
e7af610e 13611
84ea6cf2 13612 /* MIPS64 ISA */
3c02b2ab 13613 { "mipsisa64", 1, ISA_MIPS64, CPU_MIPS64, },
84ea6cf2 13614 { "Generic-MIPS64", 0, ISA_MIPS64, CPU_MIPS64, },
3c02b2ab
EC
13615 { "5kc", 0, ISA_MIPS64, CPU_MIPS64, },
13616 { "20kc", 0, ISA_MIPS64, CPU_MIPS64, },
e7af610e
NC
13617
13618 /* R2000 CPU */
13619 { "R2000", 0, ISA_MIPS1, CPU_R2000, },
13620 { "2000", 0, ISA_MIPS1, CPU_R2000, },
13621 { "2k", 0, ISA_MIPS1, CPU_R2000, },
13622 { "r2k", 0, ISA_MIPS1, CPU_R2000, },
13623
13624 /* R3000 CPU */
13625 { "R3000", 0, ISA_MIPS1, CPU_R3000, },
13626 { "3000", 0, ISA_MIPS1, CPU_R3000, },
13627 { "3k", 0, ISA_MIPS1, CPU_R3000, },
13628 { "r3k", 0, ISA_MIPS1, CPU_R3000, },
13629
13630 /* TX3900 CPU */
13631 { "R3900", 0, ISA_MIPS1, CPU_R3900, },
13632 { "3900", 0, ISA_MIPS1, CPU_R3900, },
e972090a 13633 { "mipstx39", 0, ISA_MIPS1, CPU_R3900, },
e7af610e
NC
13634
13635 /* R4000 CPU */
13636 { "R4000", 0, ISA_MIPS3, CPU_R4000, },
13637 { "4000", 0, ISA_MIPS3, CPU_R4000, },
13638 { "4k", 0, ISA_MIPS3, CPU_R4000, }, /* beware */
13639 { "r4k", 0, ISA_MIPS3, CPU_R4000, },
13640
13641 /* R4010 CPU */
13642 { "R4010", 0, ISA_MIPS2, CPU_R4010, },
13643 { "4010", 0, ISA_MIPS2, CPU_R4010, },
13644
13645 /* R4400 CPU */
13646 { "R4400", 0, ISA_MIPS3, CPU_R4400, },
13647 { "4400", 0, ISA_MIPS3, CPU_R4400, },
13648
13649 /* R4600 CPU */
13650 { "R4600", 0, ISA_MIPS3, CPU_R4600, },
13651 { "4600", 0, ISA_MIPS3, CPU_R4600, },
13652 { "mips64orion", 0, ISA_MIPS3, CPU_R4600, },
13653 { "orion", 0, ISA_MIPS3, CPU_R4600, },
13654
13655 /* R4650 CPU */
13656 { "R4650", 0, ISA_MIPS3, CPU_R4650, },
13657 { "4650", 0, ISA_MIPS3, CPU_R4650, },
13658
13659 /* R6000 CPU */
13660 { "R6000", 0, ISA_MIPS2, CPU_R6000, },
13661 { "6000", 0, ISA_MIPS2, CPU_R6000, },
13662 { "6k", 0, ISA_MIPS2, CPU_R6000, },
13663 { "r6k", 0, ISA_MIPS2, CPU_R6000, },
13664
13665 /* R8000 CPU */
13666 { "R8000", 0, ISA_MIPS4, CPU_R8000, },
13667 { "8000", 0, ISA_MIPS4, CPU_R8000, },
13668 { "8k", 0, ISA_MIPS4, CPU_R8000, },
13669 { "r8k", 0, ISA_MIPS4, CPU_R8000, },
13670
13671 /* R10000 CPU */
13672 { "R10000", 0, ISA_MIPS4, CPU_R10000, },
13673 { "10000", 0, ISA_MIPS4, CPU_R10000, },
13674 { "10k", 0, ISA_MIPS4, CPU_R10000, },
13675 { "r10k", 0, ISA_MIPS4, CPU_R10000, },
13676
d1cf510e
NC
13677 /* R12000 CPU */
13678 { "R12000", 0, ISA_MIPS4, CPU_R12000, },
13679 { "12000", 0, ISA_MIPS4, CPU_R12000, },
13680 { "12k", 0, ISA_MIPS4, CPU_R12000, },
13681 { "r12k", 0, ISA_MIPS4, CPU_R12000, },
13682
e7af610e
NC
13683 /* VR4100 CPU */
13684 { "VR4100", 0, ISA_MIPS3, CPU_VR4100, },
13685 { "4100", 0, ISA_MIPS3, CPU_VR4100, },
13686 { "mips64vr4100", 0, ISA_MIPS3, CPU_VR4100, },
13687 { "r4100", 0, ISA_MIPS3, CPU_VR4100, },
13688
13689 /* VR4111 CPU */
13690 { "VR4111", 0, ISA_MIPS3, CPU_R4111, },
13691 { "4111", 0, ISA_MIPS3, CPU_R4111, },
13692 { "mips64vr4111", 0, ISA_MIPS3, CPU_R4111, },
13693 { "r4111", 0, ISA_MIPS3, CPU_R4111, },
13694
13695 /* VR4300 CPU */
13696 { "VR4300", 0, ISA_MIPS3, CPU_R4300, },
13697 { "4300", 0, ISA_MIPS3, CPU_R4300, },
13698 { "mips64vr4300", 0, ISA_MIPS3, CPU_R4300, },
13699 { "r4300", 0, ISA_MIPS3, CPU_R4300, },
13700
13701 /* VR5000 CPU */
13702 { "VR5000", 0, ISA_MIPS4, CPU_R5000, },
13703 { "5000", 0, ISA_MIPS4, CPU_R5000, },
13704 { "5k", 0, ISA_MIPS4, CPU_R5000, },
13705 { "mips64vr5000", 0, ISA_MIPS4, CPU_R5000, },
13706 { "r5000", 0, ISA_MIPS4, CPU_R5000, },
13707 { "r5200", 0, ISA_MIPS4, CPU_R5000, },
18ae5d72 13708 { "rm5200", 0, ISA_MIPS4, CPU_R5000, },
e7af610e 13709 { "r5230", 0, ISA_MIPS4, CPU_R5000, },
18ae5d72 13710 { "rm5230", 0, ISA_MIPS4, CPU_R5000, },
e7af610e 13711 { "r5231", 0, ISA_MIPS4, CPU_R5000, },
18ae5d72 13712 { "rm5231", 0, ISA_MIPS4, CPU_R5000, },
e7af610e 13713 { "r5261", 0, ISA_MIPS4, CPU_R5000, },
18ae5d72 13714 { "rm5261", 0, ISA_MIPS4, CPU_R5000, },
e7af610e 13715 { "r5721", 0, ISA_MIPS4, CPU_R5000, },
18ae5d72 13716 { "rm5721", 0, ISA_MIPS4, CPU_R5000, },
e7af610e
NC
13717 { "r5k", 0, ISA_MIPS4, CPU_R5000, },
13718 { "r7000", 0, ISA_MIPS4, CPU_R5000, },
13719
2e4acd24 13720 /* Broadcom SB-1 CPU */
c6c98b38 13721 { "SB-1", 0, ISA_MIPS64, CPU_SB1, },
e972090a 13722 { "sb-1250", 0, ISA_MIPS64, CPU_SB1, },
c6c98b38
NC
13723 { "sb1", 0, ISA_MIPS64, CPU_SB1, },
13724 { "sb1250", 0, ISA_MIPS64, CPU_SB1, },
13725
beae10d5 13726 /* End marker. */
e7af610e
NC
13727 { NULL, 0, 0, 0, },
13728};
13729
13730static const struct mips_cpu_info *
13731mips_cpu_info_from_name (name)
13732 const char *name;
13733{
13734 int i;
13735
13736 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
beae10d5 13737 if (strcasecmp (name, mips_cpu_info_table[i].name) == 0)
e7af610e
NC
13738 return (&mips_cpu_info_table[i]);
13739
e972090a 13740 return NULL;
e7af610e
NC
13741}
13742
13743static const struct mips_cpu_info *
13744mips_cpu_info_from_isa (isa)
13745 int isa;
13746{
13747 int i;
13748
13749 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13750 if (mips_cpu_info_table[i].is_isa
13751 && isa == mips_cpu_info_table[i].isa)
13752 return (&mips_cpu_info_table[i]);
13753
e972090a 13754 return NULL;
e7af610e
NC
13755}
13756
13757static const struct mips_cpu_info *
13758mips_cpu_info_from_cpu (cpu)
13759 int cpu;
13760{
13761 int i;
13762
13763 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13764 if (!mips_cpu_info_table[i].is_isa
13765 && cpu == mips_cpu_info_table[i].cpu)
13766 return (&mips_cpu_info_table[i]);
13767
e972090a 13768 return NULL;
e7af610e 13769}
This page took 0.936309 seconds and 4 git commands to generate.