2002-06-06 Elena Zannoni <ezannoni@redhat.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 1039 int broken = 0;
1f25f5d3 1040
ec68c924 1041 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, mips_arch))
252b5132
RH
1042 as_warn (_("Could not set architecture and machine"));
1043
252b5132
RH
1044 op_hash = hash_new ();
1045
1046 for (i = 0; i < NUMOPCODES;)
1047 {
1048 const char *name = mips_opcodes[i].name;
1049
1050 retval = hash_insert (op_hash, name, (PTR) &mips_opcodes[i]);
1051 if (retval != NULL)
1052 {
1053 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1054 mips_opcodes[i].name, retval);
1055 /* Probably a memory allocation problem? Give up now. */
1056 as_fatal (_("Broken assembler. No assembly attempted."));
1057 }
1058 do
1059 {
1060 if (mips_opcodes[i].pinfo != INSN_MACRO)
1061 {
1062 if (!validate_mips_insn (&mips_opcodes[i]))
1063 broken = 1;
1064 }
1065 ++i;
1066 }
1067 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1068 }
1069
1070 mips16_op_hash = hash_new ();
1071
1072 i = 0;
1073 while (i < bfd_mips16_num_opcodes)
1074 {
1075 const char *name = mips16_opcodes[i].name;
1076
1077 retval = hash_insert (mips16_op_hash, name, (PTR) &mips16_opcodes[i]);
1078 if (retval != NULL)
1079 as_fatal (_("internal: can't hash `%s': %s"),
1080 mips16_opcodes[i].name, retval);
1081 do
1082 {
1083 if (mips16_opcodes[i].pinfo != INSN_MACRO
1084 && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1085 != mips16_opcodes[i].match))
1086 {
1087 fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1088 mips16_opcodes[i].name, mips16_opcodes[i].args);
1089 broken = 1;
1090 }
1091 ++i;
1092 }
1093 while (i < bfd_mips16_num_opcodes
1094 && strcmp (mips16_opcodes[i].name, name) == 0);
1095 }
1096
1097 if (broken)
1098 as_fatal (_("Broken assembler. No assembly attempted."));
1099
1100 /* We add all the general register names to the symbol table. This
1101 helps us detect invalid uses of them. */
1102 for (i = 0; i < 32; i++)
1103 {
1104 char buf[5];
1105
1106 sprintf (buf, "$%d", i);
1107 symbol_table_insert (symbol_new (buf, reg_section, i,
1108 &zero_address_frag));
1109 }
76db943d
TS
1110 symbol_table_insert (symbol_new ("$ra", reg_section, RA,
1111 &zero_address_frag));
252b5132
RH
1112 symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1113 &zero_address_frag));
1114 symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1115 &zero_address_frag));
1116 symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1117 &zero_address_frag));
1118 symbol_table_insert (symbol_new ("$at", reg_section, AT,
1119 &zero_address_frag));
1120 symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1121 &zero_address_frag));
1122 symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1123 &zero_address_frag));
1124 symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1125 &zero_address_frag));
1126
1127 mips_no_prev_insn (false);
1128
1129 mips_gprmask = 0;
1130 mips_cprmask[0] = 0;
1131 mips_cprmask[1] = 0;
1132 mips_cprmask[2] = 0;
1133 mips_cprmask[3] = 0;
1134
1135 /* set the default alignment for the text section (2**2) */
1136 record_alignment (text_section, 2);
1137
1138 if (USE_GLOBAL_POINTER_OPT)
1139 bfd_set_gp_size (stdoutput, g_switch_value);
1140
1141 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1142 {
1143 /* On a native system, sections must be aligned to 16 byte
1144 boundaries. When configured for an embedded ELF target, we
1145 don't bother. */
1146 if (strcmp (TARGET_OS, "elf") != 0)
1147 {
1148 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1149 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1150 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1151 }
1152
1153 /* Create a .reginfo section for register masks and a .mdebug
1154 section for debugging information. */
1155 {
1156 segT seg;
1157 subsegT subseg;
1158 flagword flags;
1159 segT sec;
1160
1161 seg = now_seg;
1162 subseg = now_subseg;
1163
1164 /* The ABI says this section should be loaded so that the
1165 running program can access it. However, we don't load it
1166 if we are configured for an embedded target */
1167 flags = SEC_READONLY | SEC_DATA;
1168 if (strcmp (TARGET_OS, "elf") != 0)
1169 flags |= SEC_ALLOC | SEC_LOAD;
1170
195325d2 1171 if (file_mips_abi != N64_ABI)
252b5132
RH
1172 {
1173 sec = subseg_new (".reginfo", (subsegT) 0);
1174
195325d2
TS
1175 bfd_set_section_flags (stdoutput, sec, flags);
1176 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
bdaaa2e1 1177
252b5132
RH
1178#ifdef OBJ_ELF
1179 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1180#endif
1181 }
1182 else
1183 {
1184 /* The 64-bit ABI uses a .MIPS.options section rather than
1185 .reginfo section. */
1186 sec = subseg_new (".MIPS.options", (subsegT) 0);
195325d2
TS
1187 bfd_set_section_flags (stdoutput, sec, flags);
1188 bfd_set_section_alignment (stdoutput, sec, 3);
252b5132
RH
1189
1190#ifdef OBJ_ELF
1191 /* Set up the option header. */
1192 {
1193 Elf_Internal_Options opthdr;
1194 char *f;
1195
1196 opthdr.kind = ODK_REGINFO;
1197 opthdr.size = (sizeof (Elf_External_Options)
1198 + sizeof (Elf64_External_RegInfo));
1199 opthdr.section = 0;
1200 opthdr.info = 0;
1201 f = frag_more (sizeof (Elf_External_Options));
1202 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1203 (Elf_External_Options *) f);
1204
1205 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1206 }
1207#endif
1208 }
1209
1210 if (ECOFF_DEBUGGING)
1211 {
1212 sec = subseg_new (".mdebug", (subsegT) 0);
1213 (void) bfd_set_section_flags (stdoutput, sec,
1214 SEC_HAS_CONTENTS | SEC_READONLY);
1215 (void) bfd_set_section_alignment (stdoutput, sec, 2);
1216 }
1217
1218#ifdef MIPS_STABS_ELF
1219 pdr_seg = subseg_new (".pdr", (subsegT) 0);
1220 (void) bfd_set_section_flags (stdoutput, pdr_seg,
1221 SEC_READONLY | SEC_RELOC | SEC_DEBUGGING);
1222 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1223#endif
1224
1225 subseg_set (seg, subseg);
1226 }
1227 }
1228
1229 if (! ECOFF_DEBUGGING)
1230 md_obj_begin ();
1231}
1232
1233void
1234md_mips_end ()
1235{
1236 if (! ECOFF_DEBUGGING)
1237 md_obj_end ();
1238}
1239
1240void
1241md_assemble (str)
1242 char *str;
1243{
1244 struct mips_cl_insn insn;
f6688943
TS
1245 bfd_reloc_code_real_type unused_reloc[3]
1246 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132
RH
1247
1248 imm_expr.X_op = O_absent;
252b5132
RH
1249 imm_unmatched_hi = false;
1250 offset_expr.X_op = O_absent;
f6688943
TS
1251 imm_reloc[0] = BFD_RELOC_UNUSED;
1252 imm_reloc[1] = BFD_RELOC_UNUSED;
1253 imm_reloc[2] = BFD_RELOC_UNUSED;
1254 offset_reloc[0] = BFD_RELOC_UNUSED;
1255 offset_reloc[1] = BFD_RELOC_UNUSED;
1256 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
1257
1258 if (mips_opts.mips16)
1259 mips16_ip (str, &insn);
1260 else
1261 {
1262 mips_ip (str, &insn);
beae10d5
KH
1263 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1264 str, insn.insn_opcode));
252b5132
RH
1265 }
1266
1267 if (insn_error)
1268 {
1269 as_bad ("%s `%s'", insn_error, str);
1270 return;
1271 }
1272
1273 if (insn.insn_mo->pinfo == INSN_MACRO)
1274 {
1275 if (mips_opts.mips16)
1276 mips16_macro (&insn);
1277 else
1278 macro (&insn);
1279 }
1280 else
1281 {
1282 if (imm_expr.X_op != O_absent)
c4e7957c 1283 append_insn (NULL, &insn, &imm_expr, imm_reloc, imm_unmatched_hi);
252b5132 1284 else if (offset_expr.X_op != O_absent)
c4e7957c 1285 append_insn (NULL, &insn, &offset_expr, offset_reloc, false);
252b5132 1286 else
c4e7957c 1287 append_insn (NULL, &insn, NULL, unused_reloc, false);
252b5132
RH
1288 }
1289}
1290
1291/* See whether instruction IP reads register REG. CLASS is the type
1292 of register. */
1293
1294static int
1295insn_uses_reg (ip, reg, class)
1296 struct mips_cl_insn *ip;
1297 unsigned int reg;
1298 enum mips_regclass class;
1299{
1300 if (class == MIPS16_REG)
1301 {
1302 assert (mips_opts.mips16);
1303 reg = mips16_to_32_reg_map[reg];
1304 class = MIPS_GR_REG;
1305 }
1306
1307 /* Don't report on general register 0, since it never changes. */
1308 if (class == MIPS_GR_REG && reg == 0)
1309 return 0;
1310
1311 if (class == MIPS_FP_REG)
1312 {
1313 assert (! mips_opts.mips16);
1314 /* If we are called with either $f0 or $f1, we must check $f0.
1315 This is not optimal, because it will introduce an unnecessary
1316 NOP between "lwc1 $f0" and "swc1 $f1". To fix this we would
1317 need to distinguish reading both $f0 and $f1 or just one of
1318 them. Note that we don't have to check the other way,
1319 because there is no instruction that sets both $f0 and $f1
1320 and requires a delay. */
1321 if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1322 && ((((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS) &~(unsigned)1)
1323 == (reg &~ (unsigned) 1)))
1324 return 1;
1325 if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1326 && ((((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT) &~(unsigned)1)
1327 == (reg &~ (unsigned) 1)))
1328 return 1;
1329 }
1330 else if (! mips_opts.mips16)
1331 {
1332 if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1333 && ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == reg)
1334 return 1;
1335 if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1336 && ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT) == reg)
1337 return 1;
1338 }
1339 else
1340 {
1341 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1342 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RX)
1343 & MIPS16OP_MASK_RX)]
1344 == reg))
1345 return 1;
1346 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1347 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RY)
1348 & MIPS16OP_MASK_RY)]
1349 == reg))
1350 return 1;
1351 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1352 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
1353 & MIPS16OP_MASK_MOVE32Z)]
1354 == reg))
1355 return 1;
1356 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1357 return 1;
1358 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1359 return 1;
1360 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1361 return 1;
1362 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1363 && ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
1364 & MIPS16OP_MASK_REGR32) == reg)
1365 return 1;
1366 }
1367
1368 return 0;
1369}
1370
1371/* This function returns true if modifying a register requires a
1372 delay. */
1373
1374static int
1375reg_needs_delay (reg)
156c2f8b 1376 unsigned int reg;
252b5132
RH
1377{
1378 unsigned long prev_pinfo;
1379
1380 prev_pinfo = prev_insn.insn_mo->pinfo;
1381 if (! mips_opts.noreorder
9ce8a5dd 1382 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
1383 && ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1384 || (! gpr_interlocks
1385 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1386 {
1387 /* A load from a coprocessor or from memory. All load
1388 delays delay the use of general register rt for one
1389 instruction on the r3000. The r6000 and r4000 use
1390 interlocks. */
bdaaa2e1 1391 /* Itbl support may require additional care here. */
252b5132
RH
1392 know (prev_pinfo & INSN_WRITE_GPR_T);
1393 if (reg == ((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT))
1394 return 1;
1395 }
1396
1397 return 0;
1398}
1399
1400/* Mark instruction labels in mips16 mode. This permits the linker to
1401 handle them specially, such as generating jalx instructions when
1402 needed. We also make them odd for the duration of the assembly, in
1403 order to generate the right sort of code. We will make them even
1404 in the adjust_symtab routine, while leaving them marked. This is
1405 convenient for the debugger and the disassembler. The linker knows
1406 to make them odd again. */
1407
1408static void
1409mips16_mark_labels ()
1410{
1411 if (mips_opts.mips16)
1412 {
1413 struct insn_label_list *l;
98aa84af 1414 valueT val;
252b5132
RH
1415
1416 for (l = insn_labels; l != NULL; l = l->next)
1417 {
1418#ifdef OBJ_ELF
1419 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1420 S_SET_OTHER (l->label, STO_MIPS16);
1421#endif
98aa84af
AM
1422 val = S_GET_VALUE (l->label);
1423 if ((val & 1) == 0)
1424 S_SET_VALUE (l->label, val + 1);
252b5132
RH
1425 }
1426 }
1427}
1428
1429/* Output an instruction. PLACE is where to put the instruction; if
1430 it is NULL, this uses frag_more to get room. IP is the instruction
1431 information. ADDRESS_EXPR is an operand of the instruction to be
1432 used with RELOC_TYPE. */
1433
1434static void
1435append_insn (place, ip, address_expr, reloc_type, unmatched_hi)
1436 char *place;
1437 struct mips_cl_insn *ip;
1438 expressionS *address_expr;
f6688943 1439 bfd_reloc_code_real_type *reloc_type;
252b5132
RH
1440 boolean unmatched_hi;
1441{
1442 register unsigned long prev_pinfo, pinfo;
1443 char *f;
f6688943 1444 fixS *fixp[3];
252b5132
RH
1445 int nops = 0;
1446
1447 /* Mark instruction labels in mips16 mode. */
f9419b05 1448 mips16_mark_labels ();
252b5132
RH
1449
1450 prev_pinfo = prev_insn.insn_mo->pinfo;
1451 pinfo = ip->insn_mo->pinfo;
1452
1453 if (place == NULL && (! mips_opts.noreorder || prev_nop_frag != NULL))
1454 {
1455 int prev_prev_nop;
1456
1457 /* If the previous insn required any delay slots, see if we need
1458 to insert a NOP or two. There are eight kinds of possible
1459 hazards, of which an instruction can have at most one type.
1460 (1) a load from memory delay
1461 (2) a load from a coprocessor delay
1462 (3) an unconditional branch delay
1463 (4) a conditional branch delay
1464 (5) a move to coprocessor register delay
1465 (6) a load coprocessor register from memory delay
1466 (7) a coprocessor condition code delay
1467 (8) a HI/LO special register delay
1468
1469 There are a lot of optimizations we could do that we don't.
1470 In particular, we do not, in general, reorder instructions.
1471 If you use gcc with optimization, it will reorder
1472 instructions and generally do much more optimization then we
1473 do here; repeating all that work in the assembler would only
1474 benefit hand written assembly code, and does not seem worth
1475 it. */
1476
1477 /* This is how a NOP is emitted. */
1478#define emit_nop() \
1479 (mips_opts.mips16 \
1480 ? md_number_to_chars (frag_more (2), 0x6500, 2) \
1481 : md_number_to_chars (frag_more (4), 0, 4))
1482
1483 /* The previous insn might require a delay slot, depending upon
1484 the contents of the current insn. */
1485 if (! mips_opts.mips16
9ce8a5dd 1486 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
1487 && (((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1488 && ! cop_interlocks)
1489 || (! gpr_interlocks
1490 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1491 {
1492 /* A load from a coprocessor or from memory. All load
1493 delays delay the use of general register rt for one
1494 instruction on the r3000. The r6000 and r4000 use
1495 interlocks. */
beae10d5 1496 /* Itbl support may require additional care here. */
252b5132
RH
1497 know (prev_pinfo & INSN_WRITE_GPR_T);
1498 if (mips_optimize == 0
1499 || insn_uses_reg (ip,
1500 ((prev_insn.insn_opcode >> OP_SH_RT)
1501 & OP_MASK_RT),
1502 MIPS_GR_REG))
1503 ++nops;
1504 }
1505 else if (! mips_opts.mips16
9ce8a5dd 1506 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132 1507 && (((prev_pinfo & INSN_COPROC_MOVE_DELAY)
beae10d5 1508 && ! cop_interlocks)
e7af610e 1509 || (mips_opts.isa == ISA_MIPS1
252b5132
RH
1510 && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))))
1511 {
1512 /* A generic coprocessor delay. The previous instruction
1513 modified a coprocessor general or control register. If
1514 it modified a control register, we need to avoid any
1515 coprocessor instruction (this is probably not always
1516 required, but it sometimes is). If it modified a general
1517 register, we avoid using that register.
1518
1519 On the r6000 and r4000 loading a coprocessor register
1520 from memory is interlocked, and does not require a delay.
1521
1522 This case is not handled very well. There is no special
1523 knowledge of CP0 handling, and the coprocessors other
1524 than the floating point unit are not distinguished at
1525 all. */
1526 /* Itbl support may require additional care here. FIXME!
bdaaa2e1 1527 Need to modify this to include knowledge about
252b5132
RH
1528 user specified delays! */
1529 if (prev_pinfo & INSN_WRITE_FPR_T)
1530 {
1531 if (mips_optimize == 0
1532 || insn_uses_reg (ip,
1533 ((prev_insn.insn_opcode >> OP_SH_FT)
1534 & OP_MASK_FT),
1535 MIPS_FP_REG))
1536 ++nops;
1537 }
1538 else if (prev_pinfo & INSN_WRITE_FPR_S)
1539 {
1540 if (mips_optimize == 0
1541 || insn_uses_reg (ip,
1542 ((prev_insn.insn_opcode >> OP_SH_FS)
1543 & OP_MASK_FS),
1544 MIPS_FP_REG))
1545 ++nops;
1546 }
1547 else
1548 {
1549 /* We don't know exactly what the previous instruction
1550 does. If the current instruction uses a coprocessor
1551 register, we must insert a NOP. If previous
1552 instruction may set the condition codes, and the
1553 current instruction uses them, we must insert two
1554 NOPS. */
bdaaa2e1 1555 /* Itbl support may require additional care here. */
252b5132
RH
1556 if (mips_optimize == 0
1557 || ((prev_pinfo & INSN_WRITE_COND_CODE)
1558 && (pinfo & INSN_READ_COND_CODE)))
1559 nops += 2;
1560 else if (pinfo & INSN_COP)
1561 ++nops;
1562 }
1563 }
1564 else if (! mips_opts.mips16
9ce8a5dd 1565 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
1566 && (prev_pinfo & INSN_WRITE_COND_CODE)
1567 && ! cop_interlocks)
1568 {
1569 /* The previous instruction sets the coprocessor condition
1570 codes, but does not require a general coprocessor delay
1571 (this means it is a floating point comparison
1572 instruction). If this instruction uses the condition
1573 codes, we need to insert a single NOP. */
beae10d5 1574 /* Itbl support may require additional care here. */
252b5132
RH
1575 if (mips_optimize == 0
1576 || (pinfo & INSN_READ_COND_CODE))
1577 ++nops;
1578 }
6b76fefe
CM
1579
1580 /* If we're fixing up mfhi/mflo for the r7000 and the
1581 previous insn was an mfhi/mflo and the current insn
1582 reads the register that the mfhi/mflo wrote to, then
1583 insert two nops. */
1584
1585 else if (mips_7000_hilo_fix
1586 && MF_HILO_INSN (prev_pinfo)
1587 && insn_uses_reg (ip, ((prev_insn.insn_opcode >> OP_SH_RD)
beae10d5
KH
1588 & OP_MASK_RD),
1589 MIPS_GR_REG))
6b76fefe
CM
1590 {
1591 nops += 2;
1592 }
1593
1594 /* If we're fixing up mfhi/mflo for the r7000 and the
1595 2nd previous insn was an mfhi/mflo and the current insn
1596 reads the register that the mfhi/mflo wrote to, then
1597 insert one nop. */
1598
1599 else if (mips_7000_hilo_fix
1600 && MF_HILO_INSN (prev_prev_insn.insn_opcode)
1601 && insn_uses_reg (ip, ((prev_prev_insn.insn_opcode >> OP_SH_RD)
1602 & OP_MASK_RD),
1603 MIPS_GR_REG))
bdaaa2e1 1604
6b76fefe 1605 {
f9419b05 1606 ++nops;
6b76fefe 1607 }
bdaaa2e1 1608
252b5132
RH
1609 else if (prev_pinfo & INSN_READ_LO)
1610 {
1611 /* The previous instruction reads the LO register; if the
1612 current instruction writes to the LO register, we must
bdaaa2e1
KH
1613 insert two NOPS. Some newer processors have interlocks.
1614 Also the tx39's multiply instructions can be exectuted
252b5132 1615 immediatly after a read from HI/LO (without the delay),
bdaaa2e1
KH
1616 though the tx39's divide insns still do require the
1617 delay. */
252b5132 1618 if (! (hilo_interlocks
ec68c924 1619 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
252b5132
RH
1620 && (mips_optimize == 0
1621 || (pinfo & INSN_WRITE_LO)))
1622 nops += 2;
1623 /* Most mips16 branch insns don't have a delay slot.
1624 If a read from LO is immediately followed by a branch
1625 to a write to LO we have a read followed by a write
1626 less than 2 insns away. We assume the target of
1627 a branch might be a write to LO, and insert a nop
bdaaa2e1 1628 between a read and an immediately following branch. */
252b5132
RH
1629 else if (mips_opts.mips16
1630 && (mips_optimize == 0
1631 || (pinfo & MIPS16_INSN_BRANCH)))
f9419b05 1632 ++nops;
252b5132
RH
1633 }
1634 else if (prev_insn.insn_mo->pinfo & INSN_READ_HI)
1635 {
1636 /* The previous instruction reads the HI register; if the
1637 current instruction writes to the HI register, we must
1638 insert a NOP. Some newer processors have interlocks.
bdaaa2e1 1639 Also the note tx39's multiply above. */
252b5132 1640 if (! (hilo_interlocks
ec68c924 1641 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
252b5132
RH
1642 && (mips_optimize == 0
1643 || (pinfo & INSN_WRITE_HI)))
1644 nops += 2;
1645 /* Most mips16 branch insns don't have a delay slot.
1646 If a read from HI is immediately followed by a branch
1647 to a write to HI we have a read followed by a write
1648 less than 2 insns away. We assume the target of
1649 a branch might be a write to HI, and insert a nop
bdaaa2e1 1650 between a read and an immediately following branch. */
252b5132
RH
1651 else if (mips_opts.mips16
1652 && (mips_optimize == 0
1653 || (pinfo & MIPS16_INSN_BRANCH)))
f9419b05 1654 ++nops;
252b5132
RH
1655 }
1656
1657 /* If the previous instruction was in a noreorder section, then
1658 we don't want to insert the nop after all. */
bdaaa2e1 1659 /* Itbl support may require additional care here. */
252b5132
RH
1660 if (prev_insn_unreordered)
1661 nops = 0;
1662
1663 /* There are two cases which require two intervening
1664 instructions: 1) setting the condition codes using a move to
1665 coprocessor instruction which requires a general coprocessor
1666 delay and then reading the condition codes 2) reading the HI
1667 or LO register and then writing to it (except on processors
1668 which have interlocks). If we are not already emitting a NOP
1669 instruction, we must check for these cases compared to the
1670 instruction previous to the previous instruction. */
1671 if ((! mips_opts.mips16
9ce8a5dd 1672 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
1673 && (prev_prev_insn.insn_mo->pinfo & INSN_COPROC_MOVE_DELAY)
1674 && (prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
1675 && (pinfo & INSN_READ_COND_CODE)
1676 && ! cop_interlocks)
1677 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_LO)
1678 && (pinfo & INSN_WRITE_LO)
1679 && ! (hilo_interlocks
ec68c924 1680 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT))))
252b5132
RH
1681 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
1682 && (pinfo & INSN_WRITE_HI)
1683 && ! (hilo_interlocks
ec68c924 1684 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))))
252b5132
RH
1685 prev_prev_nop = 1;
1686 else
1687 prev_prev_nop = 0;
1688
1689 if (prev_prev_insn_unreordered)
1690 prev_prev_nop = 0;
1691
1692 if (prev_prev_nop && nops == 0)
1693 ++nops;
1694
1695 /* If we are being given a nop instruction, don't bother with
1696 one of the nops we would otherwise output. This will only
1697 happen when a nop instruction is used with mips_optimize set
1698 to 0. */
1699 if (nops > 0
1700 && ! mips_opts.noreorder
156c2f8b 1701 && ip->insn_opcode == (unsigned) (mips_opts.mips16 ? 0x6500 : 0))
252b5132
RH
1702 --nops;
1703
1704 /* Now emit the right number of NOP instructions. */
1705 if (nops > 0 && ! mips_opts.noreorder)
1706 {
1707 fragS *old_frag;
1708 unsigned long old_frag_offset;
1709 int i;
1710 struct insn_label_list *l;
1711
1712 old_frag = frag_now;
1713 old_frag_offset = frag_now_fix ();
1714
1715 for (i = 0; i < nops; i++)
1716 emit_nop ();
1717
1718 if (listing)
1719 {
1720 listing_prev_line ();
1721 /* We may be at the start of a variant frag. In case we
1722 are, make sure there is enough space for the frag
1723 after the frags created by listing_prev_line. The
1724 argument to frag_grow here must be at least as large
1725 as the argument to all other calls to frag_grow in
1726 this file. We don't have to worry about being in the
1727 middle of a variant frag, because the variants insert
1728 all needed nop instructions themselves. */
1729 frag_grow (40);
1730 }
1731
1732 for (l = insn_labels; l != NULL; l = l->next)
1733 {
98aa84af
AM
1734 valueT val;
1735
252b5132 1736 assert (S_GET_SEGMENT (l->label) == now_seg);
49309057 1737 symbol_set_frag (l->label, frag_now);
98aa84af 1738 val = (valueT) frag_now_fix ();
252b5132
RH
1739 /* mips16 text labels are stored as odd. */
1740 if (mips_opts.mips16)
f9419b05 1741 ++val;
98aa84af 1742 S_SET_VALUE (l->label, val);
252b5132
RH
1743 }
1744
1745#ifndef NO_ECOFF_DEBUGGING
1746 if (ECOFF_DEBUGGING)
1747 ecoff_fix_loc (old_frag, old_frag_offset);
1748#endif
1749 }
1750 else if (prev_nop_frag != NULL)
1751 {
1752 /* We have a frag holding nops we may be able to remove. If
1753 we don't need any nops, we can decrease the size of
1754 prev_nop_frag by the size of one instruction. If we do
bdaaa2e1 1755 need some nops, we count them in prev_nops_required. */
252b5132
RH
1756 if (prev_nop_frag_since == 0)
1757 {
1758 if (nops == 0)
1759 {
1760 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1761 --prev_nop_frag_holds;
1762 }
1763 else
1764 prev_nop_frag_required += nops;
1765 }
1766 else
1767 {
1768 if (prev_prev_nop == 0)
1769 {
1770 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1771 --prev_nop_frag_holds;
1772 }
1773 else
1774 ++prev_nop_frag_required;
1775 }
1776
1777 if (prev_nop_frag_holds <= prev_nop_frag_required)
1778 prev_nop_frag = NULL;
1779
1780 ++prev_nop_frag_since;
1781
1782 /* Sanity check: by the time we reach the second instruction
1783 after prev_nop_frag, we should have used up all the nops
1784 one way or another. */
1785 assert (prev_nop_frag_since <= 1 || prev_nop_frag == NULL);
1786 }
1787 }
1788
f6688943 1789 if (*reloc_type > BFD_RELOC_UNUSED)
252b5132
RH
1790 {
1791 /* We need to set up a variant frag. */
1792 assert (mips_opts.mips16 && address_expr != NULL);
1793 f = frag_var (rs_machine_dependent, 4, 0,
f6688943 1794 RELAX_MIPS16_ENCODE (*reloc_type - BFD_RELOC_UNUSED,
252b5132
RH
1795 mips16_small, mips16_ext,
1796 (prev_pinfo
1797 & INSN_UNCOND_BRANCH_DELAY),
f6688943 1798 (*prev_insn_reloc_type
252b5132 1799 == BFD_RELOC_MIPS16_JMP)),
c4e7957c 1800 make_expr_symbol (address_expr), 0, NULL);
252b5132
RH
1801 }
1802 else if (place != NULL)
1803 f = place;
1804 else if (mips_opts.mips16
1805 && ! ip->use_extend
f6688943 1806 && *reloc_type != BFD_RELOC_MIPS16_JMP)
252b5132
RH
1807 {
1808 /* Make sure there is enough room to swap this instruction with
1809 a following jump instruction. */
1810 frag_grow (6);
1811 f = frag_more (2);
1812 }
1813 else
1814 {
1815 if (mips_opts.mips16
1816 && mips_opts.noreorder
1817 && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
1818 as_warn (_("extended instruction in delay slot"));
1819
1820 f = frag_more (4);
1821 }
1822
f6688943
TS
1823 fixp[0] = fixp[1] = fixp[2] = NULL;
1824 if (address_expr != NULL && *reloc_type < BFD_RELOC_UNUSED)
252b5132
RH
1825 {
1826 if (address_expr->X_op == O_constant)
1827 {
4db1a35d 1828 valueT tmp;
f6688943
TS
1829
1830 switch (*reloc_type)
252b5132
RH
1831 {
1832 case BFD_RELOC_32:
1833 ip->insn_opcode |= address_expr->X_add_number;
1834 break;
1835
f6688943
TS
1836 case BFD_RELOC_MIPS_HIGHEST:
1837 tmp = (address_expr->X_add_number + 0x800080008000) >> 16;
1838 tmp >>= 16;
1839 ip->insn_opcode |= (tmp >> 16) & 0xffff;
1840 break;
1841
1842 case BFD_RELOC_MIPS_HIGHER:
1843 tmp = (address_expr->X_add_number + 0x80008000) >> 16;
1844 ip->insn_opcode |= (tmp >> 16) & 0xffff;
1845 break;
1846
1847 case BFD_RELOC_HI16_S:
1848 ip->insn_opcode |= ((address_expr->X_add_number + 0x8000)
1849 >> 16) & 0xffff;
1850 break;
1851
1852 case BFD_RELOC_HI16:
1853 ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
1854 break;
1855
252b5132
RH
1856 case BFD_RELOC_LO16:
1857 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
1858 break;
1859
1860 case BFD_RELOC_MIPS_JMP:
1861 if ((address_expr->X_add_number & 3) != 0)
1862 as_bad (_("jump to misaligned address (0x%lx)"),
1863 (unsigned long) address_expr->X_add_number);
7496292d
TS
1864 if (address_expr->X_add_number & ~0xfffffff
1865 || address_expr->X_add_number > 0x7fffffc)
1866 as_bad (_("jump address range overflow (0x%lx)"),
1867 (unsigned long) address_expr->X_add_number);
252b5132
RH
1868 ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
1869 break;
1870
1871 case BFD_RELOC_MIPS16_JMP:
1872 if ((address_expr->X_add_number & 3) != 0)
1873 as_bad (_("jump to misaligned address (0x%lx)"),
1874 (unsigned long) address_expr->X_add_number);
7496292d
TS
1875 if (address_expr->X_add_number & ~0xfffffff
1876 || address_expr->X_add_number > 0x7fffffc)
1877 as_bad (_("jump address range overflow (0x%lx)"),
1878 (unsigned long) address_expr->X_add_number);
252b5132
RH
1879 ip->insn_opcode |=
1880 (((address_expr->X_add_number & 0x7c0000) << 3)
1881 | ((address_expr->X_add_number & 0xf800000) >> 7)
1882 | ((address_expr->X_add_number & 0x3fffc) >> 2));
1883 break;
1884
cb56d3d3 1885 case BFD_RELOC_16_PCREL:
233b8738 1886 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
cb56d3d3
TS
1887 break;
1888
252b5132
RH
1889 case BFD_RELOC_16_PCREL_S2:
1890 goto need_reloc;
1891
1892 default:
1893 internalError ();
1894 }
1895 }
1896 else
1897 {
1898 need_reloc:
f6688943 1899 /* Don't generate a reloc if we are writing into a variant frag. */
252b5132
RH
1900 if (place == NULL)
1901 {
f6688943
TS
1902 fixp[0] = fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
1903 address_expr,
1904 (*reloc_type == BFD_RELOC_16_PCREL
1905 || *reloc_type == BFD_RELOC_16_PCREL_S2),
1906 reloc_type[0]);
1907
b6ff326e 1908 /* These relocations can have an addend that won't fit in
f6688943
TS
1909 4 octets for 64bit assembly. */
1910 if (HAVE_64BIT_GPRS &&
1911 (*reloc_type == BFD_RELOC_16
98d3f06f
KH
1912 || *reloc_type == BFD_RELOC_32
1913 || *reloc_type == BFD_RELOC_MIPS_JMP
1914 || *reloc_type == BFD_RELOC_HI16_S
1915 || *reloc_type == BFD_RELOC_LO16
1916 || *reloc_type == BFD_RELOC_GPREL16
1917 || *reloc_type == BFD_RELOC_MIPS_LITERAL
1918 || *reloc_type == BFD_RELOC_GPREL32
1919 || *reloc_type == BFD_RELOC_64
1920 || *reloc_type == BFD_RELOC_CTOR
1921 || *reloc_type == BFD_RELOC_MIPS_SUB
1922 || *reloc_type == BFD_RELOC_MIPS_HIGHEST
1923 || *reloc_type == BFD_RELOC_MIPS_HIGHER
1924 || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
1925 || *reloc_type == BFD_RELOC_MIPS_REL16
1926 || *reloc_type == BFD_RELOC_MIPS_RELGOT))
f6688943
TS
1927 fixp[0]->fx_no_overflow = 1;
1928
252b5132
RH
1929 if (unmatched_hi)
1930 {
1931 struct mips_hi_fixup *hi_fixup;
1932
f6688943 1933 assert (*reloc_type == BFD_RELOC_HI16_S);
252b5132
RH
1934 hi_fixup = ((struct mips_hi_fixup *)
1935 xmalloc (sizeof (struct mips_hi_fixup)));
f6688943 1936 hi_fixup->fixp = fixp[0];
252b5132
RH
1937 hi_fixup->seg = now_seg;
1938 hi_fixup->next = mips_hi_fixup_list;
1939 mips_hi_fixup_list = hi_fixup;
1940 }
f6688943
TS
1941
1942 if (reloc_type[1] != BFD_RELOC_UNUSED)
1943 {
1944 /* FIXME: This symbol can be one of
1945 RSS_UNDEF, RSS_GP, RSS_GP0, RSS_LOC. */
1946 address_expr->X_op = O_absent;
1947 address_expr->X_add_symbol = 0;
1948 address_expr->X_add_number = 0;
1949
1950 fixp[1] = fix_new_exp (frag_now, f - frag_now->fr_literal,
1951 4, address_expr, false,
1952 reloc_type[1]);
1953
b6ff326e 1954 /* These relocations can have an addend that won't fit in
f6688943
TS
1955 4 octets for 64bit assembly. */
1956 if (HAVE_64BIT_GPRS &&
1957 (*reloc_type == BFD_RELOC_16
1958 || *reloc_type == BFD_RELOC_32
1959 || *reloc_type == BFD_RELOC_MIPS_JMP
1960 || *reloc_type == BFD_RELOC_HI16_S
1961 || *reloc_type == BFD_RELOC_LO16
1962 || *reloc_type == BFD_RELOC_GPREL16
1963 || *reloc_type == BFD_RELOC_MIPS_LITERAL
1964 || *reloc_type == BFD_RELOC_GPREL32
1965 || *reloc_type == BFD_RELOC_64
1966 || *reloc_type == BFD_RELOC_CTOR
1967 || *reloc_type == BFD_RELOC_MIPS_SUB
1968 || *reloc_type == BFD_RELOC_MIPS_HIGHEST
1969 || *reloc_type == BFD_RELOC_MIPS_HIGHER
1970 || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
1971 || *reloc_type == BFD_RELOC_MIPS_REL16
1972 || *reloc_type == BFD_RELOC_MIPS_RELGOT))
98d3f06f 1973 fixp[1]->fx_no_overflow = 1;
f6688943
TS
1974
1975 if (reloc_type[2] != BFD_RELOC_UNUSED)
1976 {
1977 address_expr->X_op = O_absent;
1978 address_expr->X_add_symbol = 0;
1979 address_expr->X_add_number = 0;
1980
1981 fixp[2] = fix_new_exp (frag_now,
1982 f - frag_now->fr_literal, 4,
1983 address_expr, false,
1984 reloc_type[2]);
1985
b6ff326e 1986 /* These relocations can have an addend that won't fit in
f6688943
TS
1987 4 octets for 64bit assembly. */
1988 if (HAVE_64BIT_GPRS &&
1989 (*reloc_type == BFD_RELOC_16
1990 || *reloc_type == BFD_RELOC_32
1991 || *reloc_type == BFD_RELOC_MIPS_JMP
1992 || *reloc_type == BFD_RELOC_HI16_S
1993 || *reloc_type == BFD_RELOC_LO16
1994 || *reloc_type == BFD_RELOC_GPREL16
1995 || *reloc_type == BFD_RELOC_MIPS_LITERAL
1996 || *reloc_type == BFD_RELOC_GPREL32
1997 || *reloc_type == BFD_RELOC_64
1998 || *reloc_type == BFD_RELOC_CTOR
1999 || *reloc_type == BFD_RELOC_MIPS_SUB
2000 || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2001 || *reloc_type == BFD_RELOC_MIPS_HIGHER
2002 || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2003 || *reloc_type == BFD_RELOC_MIPS_REL16
2004 || *reloc_type == BFD_RELOC_MIPS_RELGOT))
98d3f06f 2005 fixp[2]->fx_no_overflow = 1;
f6688943
TS
2006 }
2007 }
252b5132
RH
2008 }
2009 }
2010 }
2011
2012 if (! mips_opts.mips16)
2013 md_number_to_chars (f, ip->insn_opcode, 4);
f6688943 2014 else if (*reloc_type == BFD_RELOC_MIPS16_JMP)
252b5132
RH
2015 {
2016 md_number_to_chars (f, ip->insn_opcode >> 16, 2);
2017 md_number_to_chars (f + 2, ip->insn_opcode & 0xffff, 2);
2018 }
2019 else
2020 {
2021 if (ip->use_extend)
2022 {
2023 md_number_to_chars (f, 0xf000 | ip->extend, 2);
2024 f += 2;
2025 }
2026 md_number_to_chars (f, ip->insn_opcode, 2);
2027 }
2028
2029 /* Update the register mask information. */
2030 if (! mips_opts.mips16)
2031 {
2032 if (pinfo & INSN_WRITE_GPR_D)
2033 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
2034 if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2035 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
2036 if (pinfo & INSN_READ_GPR_S)
2037 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
2038 if (pinfo & INSN_WRITE_GPR_31)
f9419b05 2039 mips_gprmask |= 1 << RA;
252b5132
RH
2040 if (pinfo & INSN_WRITE_FPR_D)
2041 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
2042 if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2043 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
2044 if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2045 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
2046 if ((pinfo & INSN_READ_FPR_R) != 0)
2047 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
2048 if (pinfo & INSN_COP)
2049 {
bdaaa2e1
KH
2050 /* We don't keep enough information to sort these cases out.
2051 The itbl support does keep this information however, although
2052 we currently don't support itbl fprmats as part of the cop
2053 instruction. May want to add this support in the future. */
252b5132
RH
2054 }
2055 /* Never set the bit for $0, which is always zero. */
beae10d5 2056 mips_gprmask &= ~1 << 0;
252b5132
RH
2057 }
2058 else
2059 {
2060 if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2061 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RX)
2062 & MIPS16OP_MASK_RX);
2063 if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2064 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RY)
2065 & MIPS16OP_MASK_RY);
2066 if (pinfo & MIPS16_INSN_WRITE_Z)
2067 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RZ)
2068 & MIPS16OP_MASK_RZ);
2069 if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2070 mips_gprmask |= 1 << TREG;
2071 if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2072 mips_gprmask |= 1 << SP;
2073 if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2074 mips_gprmask |= 1 << RA;
2075 if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2076 mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2077 if (pinfo & MIPS16_INSN_READ_Z)
2078 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
2079 & MIPS16OP_MASK_MOVE32Z);
2080 if (pinfo & MIPS16_INSN_READ_GPR_X)
2081 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
2082 & MIPS16OP_MASK_REGR32);
2083 }
2084
2085 if (place == NULL && ! mips_opts.noreorder)
2086 {
2087 /* Filling the branch delay slot is more complex. We try to
2088 switch the branch with the previous instruction, which we can
2089 do if the previous instruction does not set up a condition
2090 that the branch tests and if the branch is not itself the
2091 target of any branch. */
2092 if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2093 || (pinfo & INSN_COND_BRANCH_DELAY))
2094 {
2095 if (mips_optimize < 2
2096 /* If we have seen .set volatile or .set nomove, don't
2097 optimize. */
2098 || mips_opts.nomove != 0
2099 /* If we had to emit any NOP instructions, then we
2100 already know we can not swap. */
2101 || nops != 0
2102 /* If we don't even know the previous insn, we can not
bdaaa2e1 2103 swap. */
252b5132
RH
2104 || ! prev_insn_valid
2105 /* If the previous insn is already in a branch delay
2106 slot, then we can not swap. */
2107 || prev_insn_is_delay_slot
2108 /* If the previous previous insn was in a .set
2109 noreorder, we can't swap. Actually, the MIPS
2110 assembler will swap in this situation. However, gcc
2111 configured -with-gnu-as will generate code like
2112 .set noreorder
2113 lw $4,XXX
2114 .set reorder
2115 INSN
2116 bne $4,$0,foo
2117 in which we can not swap the bne and INSN. If gcc is
2118 not configured -with-gnu-as, it does not output the
2119 .set pseudo-ops. We don't have to check
2120 prev_insn_unreordered, because prev_insn_valid will
2121 be 0 in that case. We don't want to use
2122 prev_prev_insn_valid, because we do want to be able
2123 to swap at the start of a function. */
2124 || prev_prev_insn_unreordered
2125 /* If the branch is itself the target of a branch, we
2126 can not swap. We cheat on this; all we check for is
2127 whether there is a label on this instruction. If
2128 there are any branches to anything other than a
2129 label, users must use .set noreorder. */
2130 || insn_labels != NULL
2131 /* If the previous instruction is in a variant frag, we
2132 can not do the swap. This does not apply to the
2133 mips16, which uses variant frags for different
2134 purposes. */
2135 || (! mips_opts.mips16
2136 && prev_insn_frag->fr_type == rs_machine_dependent)
2137 /* If the branch reads the condition codes, we don't
2138 even try to swap, because in the sequence
2139 ctc1 $X,$31
2140 INSN
2141 INSN
2142 bc1t LABEL
2143 we can not swap, and I don't feel like handling that
2144 case. */
2145 || (! mips_opts.mips16
9ce8a5dd 2146 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
2147 && (pinfo & INSN_READ_COND_CODE))
2148 /* We can not swap with an instruction that requires a
2149 delay slot, becase the target of the branch might
2150 interfere with that instruction. */
2151 || (! mips_opts.mips16
9ce8a5dd 2152 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132 2153 && (prev_pinfo
bdaaa2e1 2154 /* Itbl support may require additional care here. */
252b5132
RH
2155 & (INSN_LOAD_COPROC_DELAY
2156 | INSN_COPROC_MOVE_DELAY
2157 | INSN_WRITE_COND_CODE)))
2158 || (! (hilo_interlocks
ec68c924 2159 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
252b5132
RH
2160 && (prev_pinfo
2161 & (INSN_READ_LO
2162 | INSN_READ_HI)))
2163 || (! mips_opts.mips16
2164 && ! gpr_interlocks
2165 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))
2166 || (! mips_opts.mips16
e7af610e 2167 && mips_opts.isa == ISA_MIPS1
bdaaa2e1 2168 /* Itbl support may require additional care here. */
252b5132
RH
2169 && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))
2170 /* We can not swap with a branch instruction. */
2171 || (prev_pinfo
2172 & (INSN_UNCOND_BRANCH_DELAY
2173 | INSN_COND_BRANCH_DELAY
2174 | INSN_COND_BRANCH_LIKELY))
2175 /* We do not swap with a trap instruction, since it
2176 complicates trap handlers to have the trap
2177 instruction be in a delay slot. */
2178 || (prev_pinfo & INSN_TRAP)
2179 /* If the branch reads a register that the previous
2180 instruction sets, we can not swap. */
2181 || (! mips_opts.mips16
2182 && (prev_pinfo & INSN_WRITE_GPR_T)
2183 && insn_uses_reg (ip,
2184 ((prev_insn.insn_opcode >> OP_SH_RT)
2185 & OP_MASK_RT),
2186 MIPS_GR_REG))
2187 || (! mips_opts.mips16
2188 && (prev_pinfo & INSN_WRITE_GPR_D)
2189 && insn_uses_reg (ip,
2190 ((prev_insn.insn_opcode >> OP_SH_RD)
2191 & OP_MASK_RD),
2192 MIPS_GR_REG))
2193 || (mips_opts.mips16
2194 && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2195 && insn_uses_reg (ip,
2196 ((prev_insn.insn_opcode
2197 >> MIPS16OP_SH_RX)
2198 & MIPS16OP_MASK_RX),
2199 MIPS16_REG))
2200 || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2201 && insn_uses_reg (ip,
2202 ((prev_insn.insn_opcode
2203 >> MIPS16OP_SH_RY)
2204 & MIPS16OP_MASK_RY),
2205 MIPS16_REG))
2206 || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2207 && insn_uses_reg (ip,
2208 ((prev_insn.insn_opcode
2209 >> MIPS16OP_SH_RZ)
2210 & MIPS16OP_MASK_RZ),
2211 MIPS16_REG))
2212 || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2213 && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2214 || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2215 && insn_uses_reg (ip, RA, MIPS_GR_REG))
2216 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2217 && insn_uses_reg (ip,
2218 MIPS16OP_EXTRACT_REG32R (prev_insn.
2219 insn_opcode),
2220 MIPS_GR_REG))))
2221 /* If the branch writes a register that the previous
2222 instruction sets, we can not swap (we know that
2223 branches write only to RD or to $31). */
2224 || (! mips_opts.mips16
2225 && (prev_pinfo & INSN_WRITE_GPR_T)
2226 && (((pinfo & INSN_WRITE_GPR_D)
2227 && (((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT)
2228 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2229 || ((pinfo & INSN_WRITE_GPR_31)
2230 && (((prev_insn.insn_opcode >> OP_SH_RT)
2231 & OP_MASK_RT)
f9419b05 2232 == RA))))
252b5132
RH
2233 || (! mips_opts.mips16
2234 && (prev_pinfo & INSN_WRITE_GPR_D)
2235 && (((pinfo & INSN_WRITE_GPR_D)
2236 && (((prev_insn.insn_opcode >> OP_SH_RD) & OP_MASK_RD)
2237 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2238 || ((pinfo & INSN_WRITE_GPR_31)
2239 && (((prev_insn.insn_opcode >> OP_SH_RD)
2240 & OP_MASK_RD)
f9419b05 2241 == RA))))
252b5132
RH
2242 || (mips_opts.mips16
2243 && (pinfo & MIPS16_INSN_WRITE_31)
2244 && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2245 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2246 && (MIPS16OP_EXTRACT_REG32R (prev_insn.insn_opcode)
2247 == RA))))
2248 /* If the branch writes a register that the previous
2249 instruction reads, we can not swap (we know that
2250 branches only write to RD or to $31). */
2251 || (! mips_opts.mips16
2252 && (pinfo & INSN_WRITE_GPR_D)
2253 && insn_uses_reg (&prev_insn,
2254 ((ip->insn_opcode >> OP_SH_RD)
2255 & OP_MASK_RD),
2256 MIPS_GR_REG))
2257 || (! mips_opts.mips16
2258 && (pinfo & INSN_WRITE_GPR_31)
f9419b05 2259 && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
252b5132
RH
2260 || (mips_opts.mips16
2261 && (pinfo & MIPS16_INSN_WRITE_31)
2262 && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2263 /* If we are generating embedded PIC code, the branch
2264 might be expanded into a sequence which uses $at, so
2265 we can't swap with an instruction which reads it. */
2266 || (mips_pic == EMBEDDED_PIC
2267 && insn_uses_reg (&prev_insn, AT, MIPS_GR_REG))
2268 /* If the previous previous instruction has a load
2269 delay, and sets a register that the branch reads, we
2270 can not swap. */
2271 || (! mips_opts.mips16
9ce8a5dd 2272 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
bdaaa2e1 2273 /* Itbl support may require additional care here. */
252b5132
RH
2274 && ((prev_prev_insn.insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
2275 || (! gpr_interlocks
2276 && (prev_prev_insn.insn_mo->pinfo
2277 & INSN_LOAD_MEMORY_DELAY)))
2278 && insn_uses_reg (ip,
2279 ((prev_prev_insn.insn_opcode >> OP_SH_RT)
2280 & OP_MASK_RT),
2281 MIPS_GR_REG))
2282 /* If one instruction sets a condition code and the
2283 other one uses a condition code, we can not swap. */
2284 || ((pinfo & INSN_READ_COND_CODE)
2285 && (prev_pinfo & INSN_WRITE_COND_CODE))
2286 || ((pinfo & INSN_WRITE_COND_CODE)
2287 && (prev_pinfo & INSN_READ_COND_CODE))
2288 /* If the previous instruction uses the PC, we can not
2289 swap. */
2290 || (mips_opts.mips16
2291 && (prev_pinfo & MIPS16_INSN_READ_PC))
2292 /* If the previous instruction was extended, we can not
2293 swap. */
2294 || (mips_opts.mips16 && prev_insn_extended)
2295 /* If the previous instruction had a fixup in mips16
2296 mode, we can not swap. This normally means that the
2297 previous instruction was a 4 byte branch anyhow. */
f6688943 2298 || (mips_opts.mips16 && prev_insn_fixp[0])
bdaaa2e1
KH
2299 /* If the previous instruction is a sync, sync.l, or
2300 sync.p, we can not swap. */
f173e82e 2301 || (prev_pinfo & INSN_SYNC))
252b5132
RH
2302 {
2303 /* We could do even better for unconditional branches to
2304 portions of this object file; we could pick up the
2305 instruction at the destination, put it in the delay
2306 slot, and bump the destination address. */
2307 emit_nop ();
2308 /* Update the previous insn information. */
2309 prev_prev_insn = *ip;
2310 prev_insn.insn_mo = &dummy_opcode;
2311 }
2312 else
2313 {
2314 /* It looks like we can actually do the swap. */
2315 if (! mips_opts.mips16)
2316 {
2317 char *prev_f;
2318 char temp[4];
2319
2320 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2321 memcpy (temp, prev_f, 4);
2322 memcpy (prev_f, f, 4);
2323 memcpy (f, temp, 4);
f6688943
TS
2324 if (prev_insn_fixp[0])
2325 {
2326 prev_insn_fixp[0]->fx_frag = frag_now;
2327 prev_insn_fixp[0]->fx_where = f - frag_now->fr_literal;
2328 }
2329 if (prev_insn_fixp[1])
2330 {
2331 prev_insn_fixp[1]->fx_frag = frag_now;
2332 prev_insn_fixp[1]->fx_where = f - frag_now->fr_literal;
2333 }
2334 if (prev_insn_fixp[2])
252b5132 2335 {
f6688943
TS
2336 prev_insn_fixp[2]->fx_frag = frag_now;
2337 prev_insn_fixp[2]->fx_where = f - frag_now->fr_literal;
252b5132 2338 }
f6688943 2339 if (fixp[0])
252b5132 2340 {
f6688943
TS
2341 fixp[0]->fx_frag = prev_insn_frag;
2342 fixp[0]->fx_where = prev_insn_where;
2343 }
2344 if (fixp[1])
2345 {
2346 fixp[1]->fx_frag = prev_insn_frag;
2347 fixp[1]->fx_where = prev_insn_where;
2348 }
2349 if (fixp[2])
2350 {
2351 fixp[2]->fx_frag = prev_insn_frag;
2352 fixp[2]->fx_where = prev_insn_where;
252b5132
RH
2353 }
2354 }
2355 else
2356 {
2357 char *prev_f;
2358 char temp[2];
2359
f6688943
TS
2360 assert (prev_insn_fixp[0] == NULL);
2361 assert (prev_insn_fixp[1] == NULL);
2362 assert (prev_insn_fixp[2] == NULL);
252b5132
RH
2363 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2364 memcpy (temp, prev_f, 2);
2365 memcpy (prev_f, f, 2);
f6688943 2366 if (*reloc_type != BFD_RELOC_MIPS16_JMP)
252b5132 2367 {
f6688943 2368 assert (*reloc_type == BFD_RELOC_UNUSED);
252b5132
RH
2369 memcpy (f, temp, 2);
2370 }
2371 else
2372 {
2373 memcpy (f, f + 2, 2);
2374 memcpy (f + 2, temp, 2);
2375 }
f6688943
TS
2376 if (fixp[0])
2377 {
2378 fixp[0]->fx_frag = prev_insn_frag;
2379 fixp[0]->fx_where = prev_insn_where;
2380 }
2381 if (fixp[1])
2382 {
2383 fixp[1]->fx_frag = prev_insn_frag;
2384 fixp[1]->fx_where = prev_insn_where;
2385 }
2386 if (fixp[2])
252b5132 2387 {
f6688943
TS
2388 fixp[2]->fx_frag = prev_insn_frag;
2389 fixp[2]->fx_where = prev_insn_where;
252b5132
RH
2390 }
2391 }
2392
2393 /* Update the previous insn information; leave prev_insn
2394 unchanged. */
2395 prev_prev_insn = *ip;
2396 }
2397 prev_insn_is_delay_slot = 1;
2398
2399 /* If that was an unconditional branch, forget the previous
2400 insn information. */
2401 if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2402 {
2403 prev_prev_insn.insn_mo = &dummy_opcode;
2404 prev_insn.insn_mo = &dummy_opcode;
2405 }
2406
f6688943
TS
2407 prev_insn_fixp[0] = NULL;
2408 prev_insn_fixp[1] = NULL;
2409 prev_insn_fixp[2] = NULL;
2410 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2411 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2412 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
252b5132
RH
2413 prev_insn_extended = 0;
2414 }
2415 else if (pinfo & INSN_COND_BRANCH_LIKELY)
2416 {
2417 /* We don't yet optimize a branch likely. What we should do
2418 is look at the target, copy the instruction found there
2419 into the delay slot, and increment the branch to jump to
2420 the next instruction. */
2421 emit_nop ();
2422 /* Update the previous insn information. */
2423 prev_prev_insn = *ip;
2424 prev_insn.insn_mo = &dummy_opcode;
f6688943
TS
2425 prev_insn_fixp[0] = NULL;
2426 prev_insn_fixp[1] = NULL;
2427 prev_insn_fixp[2] = NULL;
2428 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2429 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2430 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
252b5132
RH
2431 prev_insn_extended = 0;
2432 }
2433 else
2434 {
2435 /* Update the previous insn information. */
2436 if (nops > 0)
2437 prev_prev_insn.insn_mo = &dummy_opcode;
2438 else
2439 prev_prev_insn = prev_insn;
2440 prev_insn = *ip;
2441
2442 /* Any time we see a branch, we always fill the delay slot
2443 immediately; since this insn is not a branch, we know it
2444 is not in a delay slot. */
2445 prev_insn_is_delay_slot = 0;
2446
f6688943
TS
2447 prev_insn_fixp[0] = fixp[0];
2448 prev_insn_fixp[1] = fixp[1];
2449 prev_insn_fixp[2] = fixp[2];
2450 prev_insn_reloc_type[0] = reloc_type[0];
2451 prev_insn_reloc_type[1] = reloc_type[1];
2452 prev_insn_reloc_type[2] = reloc_type[2];
252b5132
RH
2453 if (mips_opts.mips16)
2454 prev_insn_extended = (ip->use_extend
f6688943 2455 || *reloc_type > BFD_RELOC_UNUSED);
252b5132
RH
2456 }
2457
2458 prev_prev_insn_unreordered = prev_insn_unreordered;
2459 prev_insn_unreordered = 0;
2460 prev_insn_frag = frag_now;
2461 prev_insn_where = f - frag_now->fr_literal;
2462 prev_insn_valid = 1;
2463 }
2464 else if (place == NULL)
2465 {
2466 /* We need to record a bit of information even when we are not
2467 reordering, in order to determine the base address for mips16
2468 PC relative relocs. */
2469 prev_prev_insn = prev_insn;
2470 prev_insn = *ip;
f6688943
TS
2471 prev_insn_reloc_type[0] = reloc_type[0];
2472 prev_insn_reloc_type[1] = reloc_type[1];
2473 prev_insn_reloc_type[2] = reloc_type[2];
252b5132
RH
2474 prev_prev_insn_unreordered = prev_insn_unreordered;
2475 prev_insn_unreordered = 1;
2476 }
2477
2478 /* We just output an insn, so the next one doesn't have a label. */
2479 mips_clear_insn_labels ();
2480
2481 /* We must ensure that a fixup associated with an unmatched %hi
2482 reloc does not become a variant frag. Otherwise, the
2483 rearrangement of %hi relocs in frob_file may confuse
2484 tc_gen_reloc. */
2485 if (unmatched_hi)
2486 {
2487 frag_wane (frag_now);
2488 frag_new (0);
2489 }
2490}
2491
2492/* This function forgets that there was any previous instruction or
2493 label. If PRESERVE is non-zero, it remembers enough information to
bdaaa2e1 2494 know whether nops are needed before a noreorder section. */
252b5132
RH
2495
2496static void
2497mips_no_prev_insn (preserve)
2498 int preserve;
2499{
2500 if (! preserve)
2501 {
2502 prev_insn.insn_mo = &dummy_opcode;
2503 prev_prev_insn.insn_mo = &dummy_opcode;
2504 prev_nop_frag = NULL;
2505 prev_nop_frag_holds = 0;
2506 prev_nop_frag_required = 0;
2507 prev_nop_frag_since = 0;
2508 }
2509 prev_insn_valid = 0;
2510 prev_insn_is_delay_slot = 0;
2511 prev_insn_unreordered = 0;
2512 prev_insn_extended = 0;
f6688943
TS
2513 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2514 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2515 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
252b5132
RH
2516 prev_prev_insn_unreordered = 0;
2517 mips_clear_insn_labels ();
2518}
2519
2520/* This function must be called whenever we turn on noreorder or emit
2521 something other than instructions. It inserts any NOPS which might
2522 be needed by the previous instruction, and clears the information
2523 kept for the previous instructions. The INSNS parameter is true if
bdaaa2e1 2524 instructions are to follow. */
252b5132
RH
2525
2526static void
2527mips_emit_delays (insns)
2528 boolean insns;
2529{
2530 if (! mips_opts.noreorder)
2531 {
2532 int nops;
2533
2534 nops = 0;
2535 if ((! mips_opts.mips16
9ce8a5dd 2536 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
2537 && (! cop_interlocks
2538 && (prev_insn.insn_mo->pinfo
2539 & (INSN_LOAD_COPROC_DELAY
2540 | INSN_COPROC_MOVE_DELAY
2541 | INSN_WRITE_COND_CODE))))
2542 || (! hilo_interlocks
2543 && (prev_insn.insn_mo->pinfo
2544 & (INSN_READ_LO
2545 | INSN_READ_HI)))
2546 || (! mips_opts.mips16
2547 && ! gpr_interlocks
bdaaa2e1 2548 && (prev_insn.insn_mo->pinfo
252b5132
RH
2549 & INSN_LOAD_MEMORY_DELAY))
2550 || (! mips_opts.mips16
e7af610e 2551 && mips_opts.isa == ISA_MIPS1
252b5132
RH
2552 && (prev_insn.insn_mo->pinfo
2553 & INSN_COPROC_MEMORY_DELAY)))
2554 {
beae10d5 2555 /* Itbl support may require additional care here. */
252b5132
RH
2556 ++nops;
2557 if ((! mips_opts.mips16
9ce8a5dd 2558 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
2559 && (! cop_interlocks
2560 && prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2561 || (! hilo_interlocks
2562 && ((prev_insn.insn_mo->pinfo & INSN_READ_HI)
2563 || (prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2564 ++nops;
2565
2566 if (prev_insn_unreordered)
2567 nops = 0;
2568 }
2569 else if ((! mips_opts.mips16
9ce8a5dd 2570 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
2571 && (! cop_interlocks
2572 && prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2573 || (! hilo_interlocks
2574 && ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
2575 || (prev_prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2576 {
beae10d5 2577 /* Itbl support may require additional care here. */
252b5132
RH
2578 if (! prev_prev_insn_unreordered)
2579 ++nops;
2580 }
2581
2582 if (nops > 0)
2583 {
2584 struct insn_label_list *l;
2585
2586 if (insns)
2587 {
2588 /* Record the frag which holds the nop instructions, so
2589 that we can remove them if we don't need them. */
2590 frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2591 prev_nop_frag = frag_now;
2592 prev_nop_frag_holds = nops;
2593 prev_nop_frag_required = 0;
2594 prev_nop_frag_since = 0;
2595 }
2596
2597 for (; nops > 0; --nops)
2598 emit_nop ();
2599
2600 if (insns)
2601 {
2602 /* Move on to a new frag, so that it is safe to simply
bdaaa2e1 2603 decrease the size of prev_nop_frag. */
252b5132
RH
2604 frag_wane (frag_now);
2605 frag_new (0);
2606 }
2607
2608 for (l = insn_labels; l != NULL; l = l->next)
2609 {
98aa84af
AM
2610 valueT val;
2611
252b5132 2612 assert (S_GET_SEGMENT (l->label) == now_seg);
49309057 2613 symbol_set_frag (l->label, frag_now);
98aa84af 2614 val = (valueT) frag_now_fix ();
252b5132
RH
2615 /* mips16 text labels are stored as odd. */
2616 if (mips_opts.mips16)
f9419b05 2617 ++val;
98aa84af 2618 S_SET_VALUE (l->label, val);
252b5132
RH
2619 }
2620 }
2621 }
2622
2623 /* Mark instruction labels in mips16 mode. */
f9419b05 2624 if (insns)
252b5132
RH
2625 mips16_mark_labels ();
2626
2627 mips_no_prev_insn (insns);
2628}
2629
2630/* Build an instruction created by a macro expansion. This is passed
2631 a pointer to the count of instructions created so far, an
2632 expression, the name of the instruction to build, an operand format
2633 string, and corresponding arguments. */
2634
2635#ifdef USE_STDARG
2636static void
2637macro_build (char *place,
2638 int *counter,
2639 expressionS * ep,
2640 const char *name,
2641 const char *fmt,
2642 ...)
2643#else
2644static void
2645macro_build (place, counter, ep, name, fmt, va_alist)
2646 char *place;
2647 int *counter;
2648 expressionS *ep;
2649 const char *name;
2650 const char *fmt;
2651 va_dcl
2652#endif
2653{
2654 struct mips_cl_insn insn;
f6688943 2655 bfd_reloc_code_real_type r[3];
252b5132 2656 va_list args;
252b5132
RH
2657
2658#ifdef USE_STDARG
2659 va_start (args, fmt);
2660#else
2661 va_start (args);
2662#endif
2663
2664 /*
2665 * If the macro is about to expand into a second instruction,
2666 * print a warning if needed. We need to pass ip as a parameter
2667 * to generate a better warning message here...
2668 */
2669 if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
2670 as_warn (_("Macro instruction expanded into multiple instructions"));
2671
80cc45a5
EC
2672 /*
2673 * If the macro is about to expand into a second instruction,
2674 * and it is in a delay slot, print a warning.
2675 */
2676 if (place == NULL
2677 && *counter == 1
2678 && mips_opts.noreorder
2679 && (prev_prev_insn.insn_mo->pinfo
2680 & (INSN_UNCOND_BRANCH_DELAY | INSN_COND_BRANCH_DELAY
2b2e39bf 2681 | INSN_COND_BRANCH_LIKELY)) != 0)
80cc45a5
EC
2682 as_warn (_("Macro instruction expanded into multiple instructions in a branch delay slot"));
2683
252b5132 2684 if (place == NULL)
f9419b05 2685 ++*counter; /* bump instruction counter */
252b5132
RH
2686
2687 if (mips_opts.mips16)
2688 {
2689 mips16_macro_build (place, counter, ep, name, fmt, args);
2690 va_end (args);
2691 return;
2692 }
2693
f6688943
TS
2694 r[0] = BFD_RELOC_UNUSED;
2695 r[1] = BFD_RELOC_UNUSED;
2696 r[2] = BFD_RELOC_UNUSED;
252b5132
RH
2697 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
2698 assert (insn.insn_mo);
2699 assert (strcmp (name, insn.insn_mo->name) == 0);
2700
2701 /* Search until we get a match for NAME. */
2702 while (1)
2703 {
deec1734
CD
2704 /* It is assumed here that macros will never generate
2705 MDMX or MIPS-3D instructions. */
252b5132
RH
2706 if (strcmp (fmt, insn.insn_mo->args) == 0
2707 && insn.insn_mo->pinfo != INSN_MACRO
af55c2e6 2708 && OPCODE_IS_MEMBER (insn.insn_mo, mips_opts.isa, mips_arch)
ec68c924 2709 && (mips_arch != CPU_R4650 || (insn.insn_mo->pinfo & FP_D) == 0))
252b5132
RH
2710 break;
2711
2712 ++insn.insn_mo;
2713 assert (insn.insn_mo->name);
2714 assert (strcmp (name, insn.insn_mo->name) == 0);
2715 }
2716
2717 insn.insn_opcode = insn.insn_mo->match;
2718 for (;;)
2719 {
2720 switch (*fmt++)
2721 {
2722 case '\0':
2723 break;
2724
2725 case ',':
2726 case '(':
2727 case ')':
2728 continue;
2729
2730 case 't':
2731 case 'w':
2732 case 'E':
38487616 2733 insn.insn_opcode |= va_arg (args, int) << OP_SH_RT;
252b5132
RH
2734 continue;
2735
2736 case 'c':
38487616
TS
2737 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE;
2738 continue;
2739
252b5132
RH
2740 case 'T':
2741 case 'W':
38487616 2742 insn.insn_opcode |= va_arg (args, int) << OP_SH_FT;
252b5132
RH
2743 continue;
2744
2745 case 'd':
2746 case 'G':
38487616 2747 insn.insn_opcode |= va_arg (args, int) << OP_SH_RD;
252b5132
RH
2748 continue;
2749
4372b673
NC
2750 case 'U':
2751 {
2752 int tmp = va_arg (args, int);
2753
38487616
TS
2754 insn.insn_opcode |= tmp << OP_SH_RT;
2755 insn.insn_opcode |= tmp << OP_SH_RD;
beae10d5 2756 continue;
4372b673
NC
2757 }
2758
252b5132
RH
2759 case 'V':
2760 case 'S':
38487616 2761 insn.insn_opcode |= va_arg (args, int) << OP_SH_FS;
252b5132
RH
2762 continue;
2763
2764 case 'z':
2765 continue;
2766
2767 case '<':
38487616 2768 insn.insn_opcode |= va_arg (args, int) << OP_SH_SHAMT;
252b5132
RH
2769 continue;
2770
2771 case 'D':
38487616 2772 insn.insn_opcode |= va_arg (args, int) << OP_SH_FD;
252b5132
RH
2773 continue;
2774
2775 case 'B':
38487616 2776 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE20;
252b5132
RH
2777 continue;
2778
4372b673 2779 case 'J':
38487616 2780 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE19;
4372b673
NC
2781 continue;
2782
252b5132 2783 case 'q':
38487616 2784 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE2;
252b5132
RH
2785 continue;
2786
2787 case 'b':
2788 case 's':
2789 case 'r':
2790 case 'v':
38487616 2791 insn.insn_opcode |= va_arg (args, int) << OP_SH_RS;
252b5132
RH
2792 continue;
2793
2794 case 'i':
2795 case 'j':
2796 case 'o':
f6688943 2797 *r = (bfd_reloc_code_real_type) va_arg (args, int);
cdf6fd85 2798 assert (*r == BFD_RELOC_GPREL16
f6688943
TS
2799 || *r == BFD_RELOC_MIPS_LITERAL
2800 || *r == BFD_RELOC_MIPS_HIGHER
2801 || *r == BFD_RELOC_HI16_S
2802 || *r == BFD_RELOC_LO16
2803 || *r == BFD_RELOC_MIPS_GOT16
2804 || *r == BFD_RELOC_MIPS_CALL16
438c16b8
TS
2805 || *r == BFD_RELOC_MIPS_GOT_DISP
2806 || *r == BFD_RELOC_MIPS_GOT_PAGE
2807 || *r == BFD_RELOC_MIPS_GOT_OFST
f6688943
TS
2808 || *r == BFD_RELOC_MIPS_GOT_LO16
2809 || *r == BFD_RELOC_MIPS_CALL_LO16
252b5132 2810 || (ep->X_op == O_subtract
f6688943 2811 && *r == BFD_RELOC_PCREL_LO16));
252b5132
RH
2812 continue;
2813
2814 case 'u':
f6688943 2815 *r = (bfd_reloc_code_real_type) va_arg (args, int);
252b5132
RH
2816 assert (ep != NULL
2817 && (ep->X_op == O_constant
2818 || (ep->X_op == O_symbol
f6688943
TS
2819 && (*r == BFD_RELOC_MIPS_HIGHEST
2820 || *r == BFD_RELOC_HI16_S
2821 || *r == BFD_RELOC_HI16
2822 || *r == BFD_RELOC_GPREL16
2823 || *r == BFD_RELOC_MIPS_GOT_HI16
2824 || *r == BFD_RELOC_MIPS_CALL_HI16))
252b5132 2825 || (ep->X_op == O_subtract
f6688943 2826 && *r == BFD_RELOC_PCREL_HI16_S)));
252b5132
RH
2827 continue;
2828
2829 case 'p':
2830 assert (ep != NULL);
2831 /*
2832 * This allows macro() to pass an immediate expression for
2833 * creating short branches without creating a symbol.
2834 * Note that the expression still might come from the assembly
2835 * input, in which case the value is not checked for range nor
2836 * is a relocation entry generated (yuck).
2837 */
2838 if (ep->X_op == O_constant)
2839 {
2840 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
2841 ep = NULL;
2842 }
2843 else
cb56d3d3 2844 if (mips_pic == EMBEDDED_PIC)
f6688943 2845 *r = BFD_RELOC_16_PCREL_S2;
cb56d3d3 2846 else
f6688943 2847 *r = BFD_RELOC_16_PCREL;
252b5132
RH
2848 continue;
2849
2850 case 'a':
2851 assert (ep != NULL);
f6688943 2852 *r = BFD_RELOC_MIPS_JMP;
252b5132
RH
2853 continue;
2854
2855 case 'C':
2856 insn.insn_opcode |= va_arg (args, unsigned long);
2857 continue;
2858
2859 default:
2860 internalError ();
2861 }
2862 break;
2863 }
2864 va_end (args);
f6688943 2865 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132
RH
2866
2867 append_insn (place, &insn, ep, r, false);
2868}
2869
2870static void
2871mips16_macro_build (place, counter, ep, name, fmt, args)
2872 char *place;
43841e91 2873 int *counter ATTRIBUTE_UNUSED;
252b5132
RH
2874 expressionS *ep;
2875 const char *name;
2876 const char *fmt;
2877 va_list args;
2878{
2879 struct mips_cl_insn insn;
f6688943
TS
2880 bfd_reloc_code_real_type r[3]
2881 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 2882
252b5132
RH
2883 insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
2884 assert (insn.insn_mo);
2885 assert (strcmp (name, insn.insn_mo->name) == 0);
2886
2887 while (strcmp (fmt, insn.insn_mo->args) != 0
2888 || insn.insn_mo->pinfo == INSN_MACRO)
2889 {
2890 ++insn.insn_mo;
2891 assert (insn.insn_mo->name);
2892 assert (strcmp (name, insn.insn_mo->name) == 0);
2893 }
2894
2895 insn.insn_opcode = insn.insn_mo->match;
2896 insn.use_extend = false;
2897
2898 for (;;)
2899 {
2900 int c;
2901
2902 c = *fmt++;
2903 switch (c)
2904 {
2905 case '\0':
2906 break;
2907
2908 case ',':
2909 case '(':
2910 case ')':
2911 continue;
2912
2913 case 'y':
2914 case 'w':
2915 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
2916 continue;
2917
2918 case 'x':
2919 case 'v':
2920 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
2921 continue;
2922
2923 case 'z':
2924 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
2925 continue;
2926
2927 case 'Z':
2928 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
2929 continue;
2930
2931 case '0':
2932 case 'S':
2933 case 'P':
2934 case 'R':
2935 continue;
2936
2937 case 'X':
2938 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
2939 continue;
2940
2941 case 'Y':
2942 {
2943 int regno;
2944
2945 regno = va_arg (args, int);
2946 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
2947 insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
2948 }
2949 continue;
2950
2951 case '<':
2952 case '>':
2953 case '4':
2954 case '5':
2955 case 'H':
2956 case 'W':
2957 case 'D':
2958 case 'j':
2959 case '8':
2960 case 'V':
2961 case 'C':
2962 case 'U':
2963 case 'k':
2964 case 'K':
2965 case 'p':
2966 case 'q':
2967 {
2968 assert (ep != NULL);
2969
2970 if (ep->X_op != O_constant)
874e8986 2971 *r = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
2972 else
2973 {
c4e7957c
TS
2974 mips16_immed (NULL, 0, c, ep->X_add_number, false, false,
2975 false, &insn.insn_opcode, &insn.use_extend,
2976 &insn.extend);
252b5132 2977 ep = NULL;
f6688943 2978 *r = BFD_RELOC_UNUSED;
252b5132
RH
2979 }
2980 }
2981 continue;
2982
2983 case '6':
2984 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
2985 continue;
2986 }
2987
2988 break;
2989 }
2990
f6688943 2991 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132
RH
2992
2993 append_insn (place, &insn, ep, r, false);
2994}
2995
438c16b8
TS
2996/*
2997 * Generate a "jalr" instruction with a relocation hint to the called
2998 * function. This occurs in NewABI PIC code.
2999 */
3000static void
3001macro_build_jalr (icnt, ep)
3002 int icnt;
3003 expressionS *ep;
3004{
3005 if (HAVE_NEWABI)
3006 frag_more (0);
3007 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr", "d,s",
3008 RA, PIC_CALL_REG);
3009 if (HAVE_NEWABI)
3010 fix_new_exp (frag_now, 0, 0, ep, false, BFD_RELOC_MIPS_JALR);
3011}
3012
252b5132
RH
3013/*
3014 * Generate a "lui" instruction.
3015 */
3016static void
3017macro_build_lui (place, counter, ep, regnum)
3018 char *place;
3019 int *counter;
3020 expressionS *ep;
3021 int regnum;
3022{
3023 expressionS high_expr;
3024 struct mips_cl_insn insn;
f6688943
TS
3025 bfd_reloc_code_real_type r[3]
3026 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132
RH
3027 CONST char *name = "lui";
3028 CONST char *fmt = "t,u";
3029
3030 assert (! mips_opts.mips16);
3031
3032 if (place == NULL)
3033 high_expr = *ep;
3034 else
3035 {
3036 high_expr.X_op = O_constant;
3037 high_expr.X_add_number = ep->X_add_number;
3038 }
3039
3040 if (high_expr.X_op == O_constant)
3041 {
3042 /* we can compute the instruction now without a relocation entry */
e7d556df
TS
3043 high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3044 >> 16) & 0xffff;
f6688943 3045 *r = BFD_RELOC_UNUSED;
252b5132 3046 }
f6688943 3047 else if (! HAVE_NEWABI)
252b5132
RH
3048 {
3049 assert (ep->X_op == O_symbol);
3050 /* _gp_disp is a special case, used from s_cpload. */
3051 assert (mips_pic == NO_PIC
3052 || strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0);
f6688943 3053 *r = BFD_RELOC_HI16_S;
252b5132
RH
3054 }
3055
3056 /*
3057 * If the macro is about to expand into a second instruction,
3058 * print a warning if needed. We need to pass ip as a parameter
3059 * to generate a better warning message here...
3060 */
3061 if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
3062 as_warn (_("Macro instruction expanded into multiple instructions"));
3063
3064 if (place == NULL)
f9419b05 3065 ++*counter; /* bump instruction counter */
252b5132
RH
3066
3067 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3068 assert (insn.insn_mo);
3069 assert (strcmp (name, insn.insn_mo->name) == 0);
3070 assert (strcmp (fmt, insn.insn_mo->args) == 0);
3071
3072 insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
f6688943 3073 if (*r == BFD_RELOC_UNUSED)
252b5132
RH
3074 {
3075 insn.insn_opcode |= high_expr.X_add_number;
3076 append_insn (place, &insn, NULL, r, false);
3077 }
3078 else
3079 append_insn (place, &insn, &high_expr, r, false);
3080}
3081
3082/* set_at()
3083 * Generates code to set the $at register to true (one)
3084 * if reg is less than the immediate expression.
3085 */
3086static void
3087set_at (counter, reg, unsignedp)
3088 int *counter;
3089 int reg;
3090 int unsignedp;
3091{
3092 if (imm_expr.X_op == O_constant
3093 && imm_expr.X_add_number >= -0x8000
3094 && imm_expr.X_add_number < 0x8000)
3095 macro_build ((char *) NULL, counter, &imm_expr,
3096 unsignedp ? "sltiu" : "slti",
3097 "t,r,j", AT, reg, (int) BFD_RELOC_LO16);
3098 else
3099 {
4d34fb5f 3100 load_register (counter, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9 3101 macro_build ((char *) NULL, counter, (expressionS *) NULL,
252b5132
RH
3102 unsignedp ? "sltu" : "slt",
3103 "d,v,t", AT, reg, AT);
3104 }
3105}
3106
3107/* Warn if an expression is not a constant. */
3108
3109static void
3110check_absolute_expr (ip, ex)
3111 struct mips_cl_insn *ip;
3112 expressionS *ex;
3113{
3114 if (ex->X_op == O_big)
3115 as_bad (_("unsupported large constant"));
3116 else if (ex->X_op != O_constant)
3117 as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
3118}
3119
3120/* Count the leading zeroes by performing a binary chop. This is a
3121 bulky bit of source, but performance is a LOT better for the
3122 majority of values than a simple loop to count the bits:
3123 for (lcnt = 0; (lcnt < 32); lcnt++)
3124 if ((v) & (1 << (31 - lcnt)))
3125 break;
3126 However it is not code size friendly, and the gain will drop a bit
3127 on certain cached systems.
3128*/
3129#define COUNT_TOP_ZEROES(v) \
3130 (((v) & ~0xffff) == 0 \
3131 ? ((v) & ~0xff) == 0 \
3132 ? ((v) & ~0xf) == 0 \
3133 ? ((v) & ~0x3) == 0 \
3134 ? ((v) & ~0x1) == 0 \
3135 ? !(v) \
3136 ? 32 \
3137 : 31 \
3138 : 30 \
3139 : ((v) & ~0x7) == 0 \
3140 ? 29 \
3141 : 28 \
3142 : ((v) & ~0x3f) == 0 \
3143 ? ((v) & ~0x1f) == 0 \
3144 ? 27 \
3145 : 26 \
3146 : ((v) & ~0x7f) == 0 \
3147 ? 25 \
3148 : 24 \
3149 : ((v) & ~0xfff) == 0 \
3150 ? ((v) & ~0x3ff) == 0 \
3151 ? ((v) & ~0x1ff) == 0 \
3152 ? 23 \
3153 : 22 \
3154 : ((v) & ~0x7ff) == 0 \
3155 ? 21 \
3156 : 20 \
3157 : ((v) & ~0x3fff) == 0 \
3158 ? ((v) & ~0x1fff) == 0 \
3159 ? 19 \
3160 : 18 \
3161 : ((v) & ~0x7fff) == 0 \
3162 ? 17 \
3163 : 16 \
3164 : ((v) & ~0xffffff) == 0 \
3165 ? ((v) & ~0xfffff) == 0 \
3166 ? ((v) & ~0x3ffff) == 0 \
3167 ? ((v) & ~0x1ffff) == 0 \
3168 ? 15 \
3169 : 14 \
3170 : ((v) & ~0x7ffff) == 0 \
3171 ? 13 \
3172 : 12 \
3173 : ((v) & ~0x3fffff) == 0 \
3174 ? ((v) & ~0x1fffff) == 0 \
3175 ? 11 \
3176 : 10 \
3177 : ((v) & ~0x7fffff) == 0 \
3178 ? 9 \
3179 : 8 \
3180 : ((v) & ~0xfffffff) == 0 \
3181 ? ((v) & ~0x3ffffff) == 0 \
3182 ? ((v) & ~0x1ffffff) == 0 \
3183 ? 7 \
3184 : 6 \
3185 : ((v) & ~0x7ffffff) == 0 \
3186 ? 5 \
3187 : 4 \
3188 : ((v) & ~0x3fffffff) == 0 \
3189 ? ((v) & ~0x1fffffff) == 0 \
3190 ? 3 \
3191 : 2 \
3192 : ((v) & ~0x7fffffff) == 0 \
3193 ? 1 \
3194 : 0)
3195
6373ee54
CD
3196/* Is the given value a sign-extended 32-bit value? */
3197#define IS_SEXT_32BIT_NUM(x) \
3198 (((x) &~ (offsetT) 0x7fffffff) == 0 \
3199 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
3200
252b5132
RH
3201/* load_register()
3202 * This routine generates the least number of instructions neccessary to load
3203 * an absolute expression value into a register.
3204 */
3205static void
3206load_register (counter, reg, ep, dbl)
3207 int *counter;
3208 int reg;
3209 expressionS *ep;
3210 int dbl;
3211{
3212 int freg;
3213 expressionS hi32, lo32;
3214
3215 if (ep->X_op != O_big)
3216 {
3217 assert (ep->X_op == O_constant);
3218 if (ep->X_add_number < 0x8000
3219 && (ep->X_add_number >= 0
3220 || (ep->X_add_number >= -0x8000
3221 && (! dbl
3222 || ! ep->X_unsigned
3223 || sizeof (ep->X_add_number) > 4))))
3224 {
3225 /* We can handle 16 bit signed values with an addiu to
3226 $zero. No need to ever use daddiu here, since $zero and
3227 the result are always correct in 32 bit mode. */
3228 macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3229 (int) BFD_RELOC_LO16);
3230 return;
3231 }
3232 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3233 {
3234 /* We can handle 16 bit unsigned values with an ori to
3235 $zero. */
3236 macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, 0,
3237 (int) BFD_RELOC_LO16);
3238 return;
3239 }
6373ee54 3240 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)
252b5132
RH
3241 && (! dbl
3242 || ! ep->X_unsigned
3243 || sizeof (ep->X_add_number) > 4
3244 || (ep->X_add_number & 0x80000000) == 0))
ca4e0257 3245 || ((HAVE_32BIT_GPRS || ! dbl)
252b5132 3246 && (ep->X_add_number &~ (offsetT) 0xffffffff) == 0)
ca4e0257 3247 || (HAVE_32BIT_GPRS
252b5132
RH
3248 && ! dbl
3249 && ((ep->X_add_number &~ (offsetT) 0xffffffff)
3250 == ~ (offsetT) 0xffffffff)))
3251 {
3252 /* 32 bit values require an lui. */
3253 macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3254 (int) BFD_RELOC_HI16);
3255 if ((ep->X_add_number & 0xffff) != 0)
3256 macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, reg,
3257 (int) BFD_RELOC_LO16);
3258 return;
3259 }
3260 }
3261
3262 /* The value is larger than 32 bits. */
3263
ca4e0257 3264 if (HAVE_32BIT_GPRS)
252b5132 3265 {
956cd1d6
TS
3266 as_bad (_("Number (0x%lx) larger than 32 bits"),
3267 (unsigned long) ep->X_add_number);
252b5132
RH
3268 macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3269 (int) BFD_RELOC_LO16);
3270 return;
3271 }
3272
3273 if (ep->X_op != O_big)
3274 {
3275 hi32 = *ep;
3276 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3277 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3278 hi32.X_add_number &= 0xffffffff;
3279 lo32 = *ep;
3280 lo32.X_add_number &= 0xffffffff;
3281 }
3282 else
3283 {
3284 assert (ep->X_add_number > 2);
3285 if (ep->X_add_number == 3)
3286 generic_bignum[3] = 0;
3287 else if (ep->X_add_number > 4)
3288 as_bad (_("Number larger than 64 bits"));
3289 lo32.X_op = O_constant;
3290 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3291 hi32.X_op = O_constant;
3292 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3293 }
3294
3295 if (hi32.X_add_number == 0)
3296 freg = 0;
3297 else
3298 {
3299 int shift, bit;
3300 unsigned long hi, lo;
3301
956cd1d6 3302 if (hi32.X_add_number == (offsetT) 0xffffffff)
beae10d5
KH
3303 {
3304 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3305 {
3306 macro_build ((char *) NULL, counter, &lo32, "addiu", "t,r,j",
252b5132 3307 reg, 0, (int) BFD_RELOC_LO16);
beae10d5
KH
3308 return;
3309 }
3310 if (lo32.X_add_number & 0x80000000)
3311 {
3312 macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3313 (int) BFD_RELOC_HI16);
252b5132
RH
3314 if (lo32.X_add_number & 0xffff)
3315 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i",
3316 reg, reg, (int) BFD_RELOC_LO16);
beae10d5
KH
3317 return;
3318 }
3319 }
252b5132
RH
3320
3321 /* Check for 16bit shifted constant. We know that hi32 is
3322 non-zero, so start the mask on the first bit of the hi32
3323 value. */
3324 shift = 17;
3325 do
beae10d5
KH
3326 {
3327 unsigned long himask, lomask;
3328
3329 if (shift < 32)
3330 {
3331 himask = 0xffff >> (32 - shift);
3332 lomask = (0xffff << shift) & 0xffffffff;
3333 }
3334 else
3335 {
3336 himask = 0xffff << (shift - 32);
3337 lomask = 0;
3338 }
3339 if ((hi32.X_add_number & ~(offsetT) himask) == 0
3340 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3341 {
3342 expressionS tmp;
3343
3344 tmp.X_op = O_constant;
3345 if (shift < 32)
3346 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3347 | (lo32.X_add_number >> shift));
3348 else
3349 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
3350 macro_build ((char *) NULL, counter, &tmp,
3351 "ori", "t,r,i", reg, 0,
3352 (int) BFD_RELOC_LO16);
2396cfb9 3353 macro_build ((char *) NULL, counter, (expressionS *) NULL,
beae10d5
KH
3354 (shift >= 32) ? "dsll32" : "dsll",
3355 "d,w,<", reg, reg,
3356 (shift >= 32) ? shift - 32 : shift);
3357 return;
3358 }
f9419b05 3359 ++shift;
beae10d5
KH
3360 }
3361 while (shift <= (64 - 16));
252b5132
RH
3362
3363 /* Find the bit number of the lowest one bit, and store the
3364 shifted value in hi/lo. */
3365 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3366 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3367 if (lo != 0)
3368 {
3369 bit = 0;
3370 while ((lo & 1) == 0)
3371 {
3372 lo >>= 1;
3373 ++bit;
3374 }
3375 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3376 hi >>= bit;
3377 }
3378 else
3379 {
3380 bit = 32;
3381 while ((hi & 1) == 0)
3382 {
3383 hi >>= 1;
3384 ++bit;
3385 }
3386 lo = hi;
3387 hi = 0;
3388 }
3389
3390 /* Optimize if the shifted value is a (power of 2) - 1. */
3391 if ((hi == 0 && ((lo + 1) & lo) == 0)
3392 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
beae10d5
KH
3393 {
3394 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
252b5132 3395 if (shift != 0)
beae10d5 3396 {
252b5132
RH
3397 expressionS tmp;
3398
3399 /* This instruction will set the register to be all
3400 ones. */
beae10d5
KH
3401 tmp.X_op = O_constant;
3402 tmp.X_add_number = (offsetT) -1;
3403 macro_build ((char *) NULL, counter, &tmp, "addiu", "t,r,j",
252b5132 3404 reg, 0, (int) BFD_RELOC_LO16);
beae10d5
KH
3405 if (bit != 0)
3406 {
3407 bit += shift;
2396cfb9 3408 macro_build ((char *) NULL, counter, (expressionS *) NULL,
beae10d5
KH
3409 (bit >= 32) ? "dsll32" : "dsll",
3410 "d,w,<", reg, reg,
3411 (bit >= 32) ? bit - 32 : bit);
3412 }
2396cfb9 3413 macro_build ((char *) NULL, counter, (expressionS *) NULL,
252b5132 3414 (shift >= 32) ? "dsrl32" : "dsrl",
beae10d5 3415 "d,w,<", reg, reg,
252b5132 3416 (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
3417 return;
3418 }
3419 }
252b5132
RH
3420
3421 /* Sign extend hi32 before calling load_register, because we can
3422 generally get better code when we load a sign extended value. */
3423 if ((hi32.X_add_number & 0x80000000) != 0)
beae10d5 3424 hi32.X_add_number |= ~(offsetT) 0xffffffff;
252b5132
RH
3425 load_register (counter, reg, &hi32, 0);
3426 freg = reg;
3427 }
3428 if ((lo32.X_add_number & 0xffff0000) == 0)
3429 {
3430 if (freg != 0)
3431 {
2396cfb9
TS
3432 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3433 "dsll32", "d,w,<", reg, freg, 0);
252b5132
RH
3434 freg = reg;
3435 }
3436 }
3437 else
3438 {
3439 expressionS mid16;
3440
956cd1d6 3441 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
beae10d5 3442 {
252b5132
RH
3443 macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3444 (int) BFD_RELOC_HI16);
956cd1d6
TS
3445 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3446 "dsrl32", "d,w,<", reg, reg, 0);
beae10d5
KH
3447 return;
3448 }
252b5132
RH
3449
3450 if (freg != 0)
3451 {
956cd1d6
TS
3452 macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3453 "d,w,<", reg, freg, 16);
252b5132
RH
3454 freg = reg;
3455 }
3456 mid16 = lo32;
3457 mid16.X_add_number >>= 16;
3458 macro_build ((char *) NULL, counter, &mid16, "ori", "t,r,i", reg,
3459 freg, (int) BFD_RELOC_LO16);
956cd1d6
TS
3460 macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3461 "d,w,<", reg, reg, 16);
252b5132
RH
3462 freg = reg;
3463 }
3464 if ((lo32.X_add_number & 0xffff) != 0)
3465 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i", reg, freg,
3466 (int) BFD_RELOC_LO16);
3467}
3468
3469/* Load an address into a register. */
3470
3471static void
c9914766 3472load_address (counter, reg, ep, used_at)
252b5132
RH
3473 int *counter;
3474 int reg;
3475 expressionS *ep;
d6bc6245 3476 int *used_at;
252b5132 3477{
f9419b05 3478 char *p = NULL;
252b5132
RH
3479
3480 if (ep->X_op != O_constant
3481 && ep->X_op != O_symbol)
3482 {
3483 as_bad (_("expression too complex"));
3484 ep->X_op = O_constant;
3485 }
3486
3487 if (ep->X_op == O_constant)
3488 {
c9914766 3489 load_register (counter, reg, ep, HAVE_64BIT_ADDRESSES);
252b5132
RH
3490 return;
3491 }
3492
3493 if (mips_pic == NO_PIC)
3494 {
3495 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 3496 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
3497 Otherwise we want
3498 lui $reg,<sym> (BFD_RELOC_HI16_S)
3499 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
d6bc6245 3500 If we have an addend, we always use the latter form.
76b3015f 3501
d6bc6245
TS
3502 With 64bit address space and a usable $at we want
3503 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3504 lui $at,<sym> (BFD_RELOC_HI16_S)
3505 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3506 daddiu $at,<sym> (BFD_RELOC_LO16)
3507 dsll32 $reg,0
3508 dadd $reg,$reg,$at
76b3015f 3509
d6bc6245
TS
3510 If $at is already in use, we use an path which is suboptimal
3511 on superscalar processors.
3512 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3513 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3514 dsll $reg,16
3515 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
3516 dsll $reg,16
3517 daddiu $reg,<sym> (BFD_RELOC_LO16)
3518 */
c9914766 3519 if (HAVE_64BIT_ADDRESSES)
d6bc6245 3520 {
d6bc6245
TS
3521 /* We don't do GP optimization for now because RELAX_ENCODE can't
3522 hold the data for such large chunks. */
3523
3524 if (*used_at == 0)
3525 {
3526 macro_build (p, counter, ep, "lui", "t,u",
3527 reg, (int) BFD_RELOC_MIPS_HIGHEST);
3528 macro_build (p, counter, ep, "lui", "t,u",
3529 AT, (int) BFD_RELOC_HI16_S);
3530 macro_build (p, counter, ep, "daddiu", "t,r,j",
3531 reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3532 macro_build (p, counter, ep, "daddiu", "t,r,j",
3533 AT, AT, (int) BFD_RELOC_LO16);
2396cfb9
TS
3534 macro_build (p, counter, (expressionS *) NULL, "dsll32",
3535 "d,w,<", reg, reg, 0);
3536 macro_build (p, counter, (expressionS *) NULL, "dadd",
3537 "d,v,t", reg, reg, AT);
d6bc6245
TS
3538 *used_at = 1;
3539 }
3540 else
3541 {
3542 macro_build (p, counter, ep, "lui", "t,u",
3543 reg, (int) BFD_RELOC_MIPS_HIGHEST);
3544 macro_build (p, counter, ep, "daddiu", "t,r,j",
3545 reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
2396cfb9
TS
3546 macro_build (p, counter, (expressionS *) NULL, "dsll",
3547 "d,w,<", reg, reg, 16);
d6bc6245
TS
3548 macro_build (p, counter, ep, "daddiu", "t,r,j",
3549 reg, reg, (int) BFD_RELOC_HI16_S);
2396cfb9
TS
3550 macro_build (p, counter, (expressionS *) NULL, "dsll",
3551 "d,w,<", reg, reg, 16);
d6bc6245
TS
3552 macro_build (p, counter, ep, "daddiu", "t,r,j",
3553 reg, reg, (int) BFD_RELOC_LO16);
3554 }
3555 }
252b5132
RH
3556 else
3557 {
d6bc6245
TS
3558 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3559 && ! nopic_need_relax (ep->X_add_symbol, 1))
3560 {
3561 frag_grow (20);
3562 macro_build ((char *) NULL, counter, ep,
c9914766
TS
3563 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
3564 reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
d6bc6245
TS
3565 p = frag_var (rs_machine_dependent, 8, 0,
3566 RELAX_ENCODE (4, 8, 0, 4, 0,
3567 mips_opts.warn_about_macros),
956cd1d6 3568 ep->X_add_symbol, 0, NULL);
d6bc6245
TS
3569 }
3570 macro_build_lui (p, counter, ep, reg);
3571 if (p != NULL)
3572 p += 4;
c9914766
TS
3573 macro_build (p, counter, ep,
3574 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
d6bc6245
TS
3575 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3576 }
252b5132
RH
3577 }
3578 else if (mips_pic == SVR4_PIC && ! mips_big_got)
3579 {
3580 expressionS ex;
3581
3582 /* If this is a reference to an external symbol, we want
3583 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3584 Otherwise we want
3585 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3586 nop
3587 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3588 If there is a constant, it must be added in after. */
3589 ex.X_add_number = ep->X_add_number;
3590 ep->X_add_number = 0;
3591 frag_grow (20);
3592 macro_build ((char *) NULL, counter, ep,
c9914766
TS
3593 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
3594 reg, (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132
RH
3595 macro_build ((char *) NULL, counter, (expressionS *) NULL, "nop", "");
3596 p = frag_var (rs_machine_dependent, 4, 0,
3597 RELAX_ENCODE (0, 4, -8, 0, 0, mips_opts.warn_about_macros),
3598 ep->X_add_symbol, (offsetT) 0, (char *) NULL);
3599 macro_build (p, counter, ep,
ca4e0257 3600 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
3601 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3602 if (ex.X_add_number != 0)
3603 {
3604 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3605 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3606 ex.X_op = O_constant;
3607 macro_build ((char *) NULL, counter, &ex,
ca4e0257 3608 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
3609 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3610 }
3611 }
3612 else if (mips_pic == SVR4_PIC)
3613 {
3614 expressionS ex;
3615 int off;
3616
3617 /* This is the large GOT case. If this is a reference to an
3618 external symbol, we want
3619 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
3620 addu $reg,$reg,$gp
3621 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
3622 Otherwise, for a reference to a local symbol, we want
3623 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3624 nop
3625 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
438c16b8
TS
3626 If we have NewABI, we want
3627 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
3628 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
252b5132
RH
3629 If there is a constant, it must be added in after. */
3630 ex.X_add_number = ep->X_add_number;
3631 ep->X_add_number = 0;
438c16b8
TS
3632 if (HAVE_NEWABI)
3633 {
3634 macro_build ((char *) NULL, counter, ep,
3635 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3636 (int) BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
3637 macro_build (p, counter, ep,
3638 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
3639 reg, reg, (int) BFD_RELOC_MIPS_GOT_OFST);
3640 }
252b5132 3641 else
438c16b8
TS
3642 {
3643 if (reg_needs_delay (mips_gp_register))
3644 off = 4;
3645 else
3646 off = 0;
3647 frag_grow (32);
3648 macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3649 (int) BFD_RELOC_MIPS_GOT_HI16);
3650 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3651 HAVE_32BIT_ADDRESSES ? "addu" : "daddu", "d,v,t", reg,
3652 reg, mips_gp_register);
3653 macro_build ((char *) NULL, counter, ep,
3654 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
3655 "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT_LO16, reg);
3656 p = frag_var (rs_machine_dependent, 12 + off, 0,
3657 RELAX_ENCODE (12, 12 + off, off, 8 + off, 0,
3658 mips_opts.warn_about_macros),
3659 ep->X_add_symbol, 0, NULL);
3660 if (off > 0)
3661 {
3662 /* We need a nop before loading from $gp. This special
3663 check is required because the lui which starts the main
3664 instruction stream does not refer to $gp, and so will not
3665 insert the nop which may be required. */
3666 macro_build (p, counter, (expressionS *) NULL, "nop", "");
3667 p += 4;
3668 }
3669 macro_build (p, counter, ep,
3670 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3671 (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
3672 p += 4;
252b5132
RH
3673 macro_build (p, counter, (expressionS *) NULL, "nop", "");
3674 p += 4;
438c16b8
TS
3675 macro_build (p, counter, ep,
3676 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3677 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
252b5132 3678 }
438c16b8 3679
252b5132
RH
3680 if (ex.X_add_number != 0)
3681 {
3682 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3683 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3684 ex.X_op = O_constant;
f7ea7ef2
TS
3685 macro_build ((char *) NULL, counter, &ex,
3686 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
3687 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3688 }
3689 }
3690 else if (mips_pic == EMBEDDED_PIC)
3691 {
3692 /* We always do
cdf6fd85 3693 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
c9914766
TS
3694 */
3695 macro_build ((char *) NULL, counter, ep,
3696 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3697 "t,r,j", reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
252b5132
RH
3698 }
3699 else
3700 abort ();
3701}
3702
ea1fb5dc
RS
3703/* Move the contents of register SOURCE into register DEST. */
3704
3705static void
3706move_register (counter, dest, source)
3707 int *counter;
3708 int dest;
3709 int source;
3710{
3711 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3712 HAVE_32BIT_GPRS ? "addu" : "daddu",
3713 "d,v,t", dest, source, 0);
3714}
3715
252b5132
RH
3716/*
3717 * Build macros
3718 * This routine implements the seemingly endless macro or synthesized
3719 * instructions and addressing modes in the mips assembly language. Many
3720 * of these macros are simple and are similar to each other. These could
3721 * probably be handled by some kind of table or grammer aproach instead of
3722 * this verbose method. Others are not simple macros but are more like
3723 * optimizing code generation.
3724 * One interesting optimization is when several store macros appear
3725 * consecutivly that would load AT with the upper half of the same address.
3726 * The ensuing load upper instructions are ommited. This implies some kind
3727 * of global optimization. We currently only optimize within a single macro.
3728 * For many of the load and store macros if the address is specified as a
3729 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
3730 * first load register 'at' with zero and use it as the base register. The
3731 * mips assembler simply uses register $zero. Just one tiny optimization
3732 * we're missing.
3733 */
3734static void
3735macro (ip)
3736 struct mips_cl_insn *ip;
3737{
3738 register int treg, sreg, dreg, breg;
3739 int tempreg;
3740 int mask;
3741 int icnt = 0;
43841e91 3742 int used_at = 0;
252b5132
RH
3743 expressionS expr1;
3744 const char *s;
3745 const char *s2;
3746 const char *fmt;
3747 int likely = 0;
3748 int dbl = 0;
3749 int coproc = 0;
3750 int lr = 0;
3751 int imm = 0;
3752 offsetT maxnum;
3753 int off;
3754 bfd_reloc_code_real_type r;
252b5132
RH
3755 int hold_mips_optimize;
3756
3757 assert (! mips_opts.mips16);
3758
3759 treg = (ip->insn_opcode >> 16) & 0x1f;
3760 dreg = (ip->insn_opcode >> 11) & 0x1f;
3761 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
3762 mask = ip->insn_mo->mask;
3763
3764 expr1.X_op = O_constant;
3765 expr1.X_op_symbol = NULL;
3766 expr1.X_add_symbol = NULL;
3767 expr1.X_add_number = 1;
3768
3769 switch (mask)
3770 {
3771 case M_DABS:
3772 dbl = 1;
3773 case M_ABS:
3774 /* bgez $a0,.+12
3775 move v0,$a0
3776 sub v0,$zero,$a0
3777 */
3778
3779 mips_emit_delays (true);
3780 ++mips_opts.noreorder;
3781 mips_any_noreorder = 1;
3782
3783 expr1.X_add_number = 8;
3784 macro_build ((char *) NULL, &icnt, &expr1, "bgez", "s,p", sreg);
3785 if (dreg == sreg)
2396cfb9
TS
3786 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
3787 0);
252b5132 3788 else
ea1fb5dc 3789 move_register (&icnt, dreg, sreg);
2396cfb9 3790 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 3791 dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
252b5132
RH
3792
3793 --mips_opts.noreorder;
3794 return;
3795
3796 case M_ADD_I:
3797 s = "addi";
3798 s2 = "add";
3799 goto do_addi;
3800 case M_ADDU_I:
3801 s = "addiu";
3802 s2 = "addu";
3803 goto do_addi;
3804 case M_DADD_I:
3805 dbl = 1;
3806 s = "daddi";
3807 s2 = "dadd";
3808 goto do_addi;
3809 case M_DADDU_I:
3810 dbl = 1;
3811 s = "daddiu";
3812 s2 = "daddu";
3813 do_addi:
3814 if (imm_expr.X_op == O_constant
3815 && imm_expr.X_add_number >= -0x8000
3816 && imm_expr.X_add_number < 0x8000)
3817 {
3818 macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,j", treg, sreg,
3819 (int) BFD_RELOC_LO16);
3820 return;
3821 }
3822 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9
TS
3823 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
3824 treg, sreg, AT);
252b5132
RH
3825 break;
3826
3827 case M_AND_I:
3828 s = "andi";
3829 s2 = "and";
3830 goto do_bit;
3831 case M_OR_I:
3832 s = "ori";
3833 s2 = "or";
3834 goto do_bit;
3835 case M_NOR_I:
3836 s = "";
3837 s2 = "nor";
3838 goto do_bit;
3839 case M_XOR_I:
3840 s = "xori";
3841 s2 = "xor";
3842 do_bit:
3843 if (imm_expr.X_op == O_constant
3844 && imm_expr.X_add_number >= 0
3845 && imm_expr.X_add_number < 0x10000)
3846 {
3847 if (mask != M_NOR_I)
3848 macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,i", treg,
3849 sreg, (int) BFD_RELOC_LO16);
3850 else
3851 {
3852 macro_build ((char *) NULL, &icnt, &imm_expr, "ori", "t,r,i",
3853 treg, sreg, (int) BFD_RELOC_LO16);
2396cfb9
TS
3854 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nor",
3855 "d,v,t", treg, treg, 0);
252b5132
RH
3856 }
3857 return;
3858 }
3859
d6bc6245 3860 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
3861 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
3862 treg, sreg, AT);
252b5132
RH
3863 break;
3864
3865 case M_BEQ_I:
3866 s = "beq";
3867 goto beq_i;
3868 case M_BEQL_I:
3869 s = "beql";
3870 likely = 1;
3871 goto beq_i;
3872 case M_BNE_I:
3873 s = "bne";
3874 goto beq_i;
3875 case M_BNEL_I:
3876 s = "bnel";
3877 likely = 1;
3878 beq_i:
3879 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
3880 {
3881 macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg,
3882 0);
3883 return;
3884 }
4d34fb5f 3885 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
252b5132
RH
3886 macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg, AT);
3887 break;
3888
3889 case M_BGEL:
3890 likely = 1;
3891 case M_BGE:
3892 if (treg == 0)
3893 {
3894 macro_build ((char *) NULL, &icnt, &offset_expr,
2396cfb9 3895 likely ? "bgezl" : "bgez", "s,p", sreg);
252b5132
RH
3896 return;
3897 }
3898 if (sreg == 0)
3899 {
3900 macro_build ((char *) NULL, &icnt, &offset_expr,
2396cfb9 3901 likely ? "blezl" : "blez", "s,p", treg);
252b5132
RH
3902 return;
3903 }
2396cfb9
TS
3904 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
3905 AT, sreg, treg);
252b5132 3906 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 3907 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
3908 break;
3909
3910 case M_BGTL_I:
3911 likely = 1;
3912 case M_BGT_I:
3913 /* check for > max integer */
3914 maxnum = 0x7fffffff;
ca4e0257 3915 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
3916 {
3917 maxnum <<= 16;
3918 maxnum |= 0xffff;
3919 maxnum <<= 16;
3920 maxnum |= 0xffff;
3921 }
3922 if (imm_expr.X_op == O_constant
3923 && imm_expr.X_add_number >= maxnum
ca4e0257 3924 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
3925 {
3926 do_false:
3927 /* result is always false */
3928 if (! likely)
3929 {
39c0a331
L
3930 if (warn_nops)
3931 as_warn (_("Branch %s is always false (nop)"),
3932 ip->insn_mo->name);
2396cfb9
TS
3933 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop",
3934 "", 0);
252b5132
RH
3935 }
3936 else
3937 {
39c0a331
L
3938 if (warn_nops)
3939 as_warn (_("Branch likely %s is always false"),
3940 ip->insn_mo->name);
252b5132
RH
3941 macro_build ((char *) NULL, &icnt, &offset_expr, "bnel",
3942 "s,t,p", 0, 0);
3943 }
3944 return;
3945 }
3946 if (imm_expr.X_op != O_constant)
3947 as_bad (_("Unsupported large constant"));
f9419b05 3948 ++imm_expr.X_add_number;
252b5132
RH
3949 /* FALLTHROUGH */
3950 case M_BGE_I:
3951 case M_BGEL_I:
3952 if (mask == M_BGEL_I)
3953 likely = 1;
3954 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
3955 {
3956 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 3957 likely ? "bgezl" : "bgez", "s,p", sreg);
252b5132
RH
3958 return;
3959 }
3960 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
3961 {
3962 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 3963 likely ? "bgtzl" : "bgtz", "s,p", sreg);
252b5132
RH
3964 return;
3965 }
3966 maxnum = 0x7fffffff;
ca4e0257 3967 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
3968 {
3969 maxnum <<= 16;
3970 maxnum |= 0xffff;
3971 maxnum <<= 16;
3972 maxnum |= 0xffff;
3973 }
3974 maxnum = - maxnum - 1;
3975 if (imm_expr.X_op == O_constant
3976 && imm_expr.X_add_number <= maxnum
ca4e0257 3977 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
3978 {
3979 do_true:
3980 /* result is always true */
3981 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
3982 macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
3983 return;
3984 }
3985 set_at (&icnt, sreg, 0);
3986 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 3987 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
3988 break;
3989
3990 case M_BGEUL:
3991 likely = 1;
3992 case M_BGEU:
3993 if (treg == 0)
3994 goto do_true;
3995 if (sreg == 0)
3996 {
3997 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 3998 likely ? "beql" : "beq", "s,t,p", 0, treg);
252b5132
RH
3999 return;
4000 }
2396cfb9
TS
4001 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4002 "d,v,t", AT, sreg, treg);
252b5132 4003 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4004 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4005 break;
4006
4007 case M_BGTUL_I:
4008 likely = 1;
4009 case M_BGTU_I:
4010 if (sreg == 0
ca4e0257 4011 || (HAVE_32BIT_GPRS
252b5132 4012 && imm_expr.X_op == O_constant
956cd1d6 4013 && imm_expr.X_add_number == (offsetT) 0xffffffff))
252b5132
RH
4014 goto do_false;
4015 if (imm_expr.X_op != O_constant)
4016 as_bad (_("Unsupported large constant"));
f9419b05 4017 ++imm_expr.X_add_number;
252b5132
RH
4018 /* FALLTHROUGH */
4019 case M_BGEU_I:
4020 case M_BGEUL_I:
4021 if (mask == M_BGEUL_I)
4022 likely = 1;
4023 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4024 goto do_true;
4025 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4026 {
4027 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4028 likely ? "bnel" : "bne", "s,t,p", sreg, 0);
252b5132
RH
4029 return;
4030 }
4031 set_at (&icnt, sreg, 1);
4032 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4033 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4034 break;
4035
4036 case M_BGTL:
4037 likely = 1;
4038 case M_BGT:
4039 if (treg == 0)
4040 {
4041 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4042 likely ? "bgtzl" : "bgtz", "s,p", sreg);
252b5132
RH
4043 return;
4044 }
4045 if (sreg == 0)
4046 {
4047 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4048 likely ? "bltzl" : "bltz", "s,p", treg);
252b5132
RH
4049 return;
4050 }
2396cfb9
TS
4051 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4052 AT, treg, sreg);
252b5132 4053 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4054 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4055 break;
4056
4057 case M_BGTUL:
4058 likely = 1;
4059 case M_BGTU:
4060 if (treg == 0)
4061 {
4062 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4063 likely ? "bnel" : "bne", "s,t,p", sreg, 0);
252b5132
RH
4064 return;
4065 }
4066 if (sreg == 0)
4067 goto do_false;
2396cfb9
TS
4068 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4069 "d,v,t", AT, treg, sreg);
252b5132 4070 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4071 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4072 break;
4073
4074 case M_BLEL:
4075 likely = 1;
4076 case M_BLE:
4077 if (treg == 0)
4078 {
4079 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4080 likely ? "blezl" : "blez", "s,p", sreg);
252b5132
RH
4081 return;
4082 }
4083 if (sreg == 0)
4084 {
4085 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4086 likely ? "bgezl" : "bgez", "s,p", treg);
252b5132
RH
4087 return;
4088 }
2396cfb9
TS
4089 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4090 AT, treg, sreg);
252b5132 4091 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4092 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4093 break;
4094
4095 case M_BLEL_I:
4096 likely = 1;
4097 case M_BLE_I:
4098 maxnum = 0x7fffffff;
ca4e0257 4099 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
4100 {
4101 maxnum <<= 16;
4102 maxnum |= 0xffff;
4103 maxnum <<= 16;
4104 maxnum |= 0xffff;
4105 }
4106 if (imm_expr.X_op == O_constant
4107 && imm_expr.X_add_number >= maxnum
ca4e0257 4108 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
4109 goto do_true;
4110 if (imm_expr.X_op != O_constant)
4111 as_bad (_("Unsupported large constant"));
f9419b05 4112 ++imm_expr.X_add_number;
252b5132
RH
4113 /* FALLTHROUGH */
4114 case M_BLT_I:
4115 case M_BLTL_I:
4116 if (mask == M_BLTL_I)
4117 likely = 1;
4118 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4119 {
4120 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4121 likely ? "bltzl" : "bltz", "s,p", sreg);
252b5132
RH
4122 return;
4123 }
4124 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4125 {
4126 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4127 likely ? "blezl" : "blez", "s,p", sreg);
252b5132
RH
4128 return;
4129 }
4130 set_at (&icnt, sreg, 0);
4131 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4132 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4133 break;
4134
4135 case M_BLEUL:
4136 likely = 1;
4137 case M_BLEU:
4138 if (treg == 0)
4139 {
4140 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4141 likely ? "beql" : "beq", "s,t,p", sreg, 0);
252b5132
RH
4142 return;
4143 }
4144 if (sreg == 0)
4145 goto do_true;
2396cfb9
TS
4146 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4147 "d,v,t", AT, treg, sreg);
252b5132 4148 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4149 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4150 break;
4151
4152 case M_BLEUL_I:
4153 likely = 1;
4154 case M_BLEU_I:
4155 if (sreg == 0
ca4e0257 4156 || (HAVE_32BIT_GPRS
252b5132 4157 && imm_expr.X_op == O_constant
956cd1d6 4158 && imm_expr.X_add_number == (offsetT) 0xffffffff))
252b5132
RH
4159 goto do_true;
4160 if (imm_expr.X_op != O_constant)
4161 as_bad (_("Unsupported large constant"));
f9419b05 4162 ++imm_expr.X_add_number;
252b5132
RH
4163 /* FALLTHROUGH */
4164 case M_BLTU_I:
4165 case M_BLTUL_I:
4166 if (mask == M_BLTUL_I)
4167 likely = 1;
4168 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4169 goto do_false;
4170 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4171 {
4172 macro_build ((char *) NULL, &icnt, &offset_expr,
4173 likely ? "beql" : "beq",
4174 "s,t,p", sreg, 0);
4175 return;
4176 }
4177 set_at (&icnt, sreg, 1);
4178 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4179 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4180 break;
4181
4182 case M_BLTL:
4183 likely = 1;
4184 case M_BLT:
4185 if (treg == 0)
4186 {
4187 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4188 likely ? "bltzl" : "bltz", "s,p", sreg);
252b5132
RH
4189 return;
4190 }
4191 if (sreg == 0)
4192 {
4193 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4194 likely ? "bgtzl" : "bgtz", "s,p", treg);
252b5132
RH
4195 return;
4196 }
2396cfb9
TS
4197 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4198 AT, sreg, treg);
252b5132 4199 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4200 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4201 break;
4202
4203 case M_BLTUL:
4204 likely = 1;
4205 case M_BLTU:
4206 if (treg == 0)
4207 goto do_false;
4208 if (sreg == 0)
4209 {
4210 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4211 likely ? "bnel" : "bne", "s,t,p", 0, treg);
252b5132
RH
4212 return;
4213 }
2396cfb9
TS
4214 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4215 "d,v,t", AT, sreg,
252b5132
RH
4216 treg);
4217 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4218 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4219 break;
4220
4221 case M_DDIV_3:
4222 dbl = 1;
4223 case M_DIV_3:
4224 s = "mflo";
4225 goto do_div3;
4226 case M_DREM_3:
4227 dbl = 1;
4228 case M_REM_3:
4229 s = "mfhi";
4230 do_div3:
4231 if (treg == 0)
4232 {
4233 as_warn (_("Divide by zero."));
4234 if (mips_trap)
2396cfb9
TS
4235 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4236 "s,t", 0, 0);
252b5132 4237 else
2396cfb9
TS
4238 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4239 "c", 7);
252b5132
RH
4240 return;
4241 }
4242
4243 mips_emit_delays (true);
4244 ++mips_opts.noreorder;
4245 mips_any_noreorder = 1;
4246 if (mips_trap)
4247 {
2396cfb9
TS
4248 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4249 "s,t", treg, 0);
4250 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 4251 dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
252b5132
RH
4252 }
4253 else
4254 {
4255 expr1.X_add_number = 8;
4256 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
2396cfb9 4257 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 4258 dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
2396cfb9
TS
4259 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4260 "c", 7);
252b5132
RH
4261 }
4262 expr1.X_add_number = -1;
4263 macro_build ((char *) NULL, &icnt, &expr1,
4264 dbl ? "daddiu" : "addiu",
4265 "t,r,j", AT, 0, (int) BFD_RELOC_LO16);
4266 expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
4267 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, AT);
4268 if (dbl)
4269 {
4270 expr1.X_add_number = 1;
4271 macro_build ((char *) NULL, &icnt, &expr1, "daddiu", "t,r,j", AT, 0,
4272 (int) BFD_RELOC_LO16);
2396cfb9
TS
4273 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsll32",
4274 "d,w,<", AT, AT, 31);
252b5132
RH
4275 }
4276 else
4277 {
4278 expr1.X_add_number = 0x80000000;
4279 macro_build ((char *) NULL, &icnt, &expr1, "lui", "t,u", AT,
4280 (int) BFD_RELOC_HI16);
4281 }
4282 if (mips_trap)
4283 {
2396cfb9
TS
4284 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4285 "s,t", sreg, AT);
252b5132
RH
4286 /* We want to close the noreorder block as soon as possible, so
4287 that later insns are available for delay slot filling. */
4288 --mips_opts.noreorder;
4289 }
4290 else
4291 {
4292 expr1.X_add_number = 8;
4293 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", sreg, AT);
2396cfb9
TS
4294 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
4295 0);
252b5132
RH
4296
4297 /* We want to close the noreorder block as soon as possible, so
4298 that later insns are available for delay slot filling. */
4299 --mips_opts.noreorder;
4300
2396cfb9
TS
4301 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4302 "c", 6);
252b5132 4303 }
2396cfb9 4304 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d", dreg);
252b5132
RH
4305 break;
4306
4307 case M_DIV_3I:
4308 s = "div";
4309 s2 = "mflo";
4310 goto do_divi;
4311 case M_DIVU_3I:
4312 s = "divu";
4313 s2 = "mflo";
4314 goto do_divi;
4315 case M_REM_3I:
4316 s = "div";
4317 s2 = "mfhi";
4318 goto do_divi;
4319 case M_REMU_3I:
4320 s = "divu";
4321 s2 = "mfhi";
4322 goto do_divi;
4323 case M_DDIV_3I:
4324 dbl = 1;
4325 s = "ddiv";
4326 s2 = "mflo";
4327 goto do_divi;
4328 case M_DDIVU_3I:
4329 dbl = 1;
4330 s = "ddivu";
4331 s2 = "mflo";
4332 goto do_divi;
4333 case M_DREM_3I:
4334 dbl = 1;
4335 s = "ddiv";
4336 s2 = "mfhi";
4337 goto do_divi;
4338 case M_DREMU_3I:
4339 dbl = 1;
4340 s = "ddivu";
4341 s2 = "mfhi";
4342 do_divi:
4343 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4344 {
4345 as_warn (_("Divide by zero."));
4346 if (mips_trap)
2396cfb9
TS
4347 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4348 "s,t", 0, 0);
252b5132 4349 else
2396cfb9
TS
4350 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4351 "c", 7);
252b5132
RH
4352 return;
4353 }
4354 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4355 {
4356 if (strcmp (s2, "mflo") == 0)
ea1fb5dc 4357 move_register (&icnt, dreg, sreg);
252b5132 4358 else
ea1fb5dc 4359 move_register (&icnt, dreg, 0);
252b5132
RH
4360 return;
4361 }
4362 if (imm_expr.X_op == O_constant
4363 && imm_expr.X_add_number == -1
4364 && s[strlen (s) - 1] != 'u')
4365 {
4366 if (strcmp (s2, "mflo") == 0)
4367 {
2396cfb9
TS
4368 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4369 dbl ? "dneg" : "neg", "d,w", dreg, sreg);
252b5132
RH
4370 }
4371 else
ea1fb5dc 4372 move_register (&icnt, dreg, 0);
252b5132
RH
4373 return;
4374 }
4375
4376 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9
TS
4377 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4378 sreg, AT);
4379 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
252b5132
RH
4380 break;
4381
4382 case M_DIVU_3:
4383 s = "divu";
4384 s2 = "mflo";
4385 goto do_divu3;
4386 case M_REMU_3:
4387 s = "divu";
4388 s2 = "mfhi";
4389 goto do_divu3;
4390 case M_DDIVU_3:
4391 s = "ddivu";
4392 s2 = "mflo";
4393 goto do_divu3;
4394 case M_DREMU_3:
4395 s = "ddivu";
4396 s2 = "mfhi";
4397 do_divu3:
4398 mips_emit_delays (true);
4399 ++mips_opts.noreorder;
4400 mips_any_noreorder = 1;
4401 if (mips_trap)
4402 {
2396cfb9
TS
4403 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4404 "s,t", treg, 0);
4405 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4406 sreg, treg);
252b5132
RH
4407 /* We want to close the noreorder block as soon as possible, so
4408 that later insns are available for delay slot filling. */
4409 --mips_opts.noreorder;
4410 }
4411 else
4412 {
4413 expr1.X_add_number = 8;
4414 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
2396cfb9
TS
4415 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4416 sreg, treg);
252b5132
RH
4417
4418 /* We want to close the noreorder block as soon as possible, so
4419 that later insns are available for delay slot filling. */
4420 --mips_opts.noreorder;
2396cfb9
TS
4421 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4422 "c", 7);
252b5132 4423 }
2396cfb9 4424 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
252b5132
RH
4425 return;
4426
4427 case M_DLA_AB:
4428 dbl = 1;
4429 case M_LA_AB:
4430 /* Load the address of a symbol into a register. If breg is not
4431 zero, we then add a base register to it. */
4432
3bec30a8
TS
4433 if (dbl && HAVE_32BIT_GPRS)
4434 as_warn (_("dla used to load 32-bit register"));
4435
c90bbe5b 4436 if (! dbl && HAVE_64BIT_OBJECTS)
3bec30a8
TS
4437 as_warn (_("la used to load 64-bit address"));
4438
afdbd6d0
CD
4439 if (treg == breg)
4440 {
4441 tempreg = AT;
4442 used_at = 1;
4443 }
4444 else
4445 {
4446 tempreg = treg;
4447 used_at = 0;
4448 }
4449
252b5132
RH
4450 /* When generating embedded PIC code, we permit expressions of
4451 the form
afdbd6d0
CD
4452 la $treg,foo-bar
4453 la $treg,foo-bar($breg)
bb2d6cd7 4454 where bar is an address in the current section. These are used
252b5132
RH
4455 when getting the addresses of functions. We don't permit
4456 X_add_number to be non-zero, because if the symbol is
4457 external the relaxing code needs to know that any addend is
4458 purely the offset to X_op_symbol. */
4459 if (mips_pic == EMBEDDED_PIC
4460 && offset_expr.X_op == O_subtract
49309057 4461 && (symbol_constant_p (offset_expr.X_op_symbol)
bb2d6cd7 4462 ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
49309057
ILT
4463 : (symbol_equated_p (offset_expr.X_op_symbol)
4464 && (S_GET_SEGMENT
4465 (symbol_get_value_expression (offset_expr.X_op_symbol)
4466 ->X_add_symbol)
bb2d6cd7 4467 == now_seg)))
bb2d6cd7
GK
4468 && (offset_expr.X_add_number == 0
4469 || OUTPUT_FLAVOR == bfd_target_elf_flavour))
252b5132 4470 {
afdbd6d0
CD
4471 if (breg == 0)
4472 {
4473 tempreg = treg;
4474 used_at = 0;
4475 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4476 tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4477 }
4478 else
4479 {
4480 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4481 tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4482 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4d34fb5f 4483 (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu",
afdbd6d0
CD
4484 "d,v,t", tempreg, tempreg, breg);
4485 }
252b5132 4486 macro_build ((char *) NULL, &icnt, &offset_expr,
4d34fb5f 4487 (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
afdbd6d0
CD
4488 "t,r,j", treg, tempreg, (int) BFD_RELOC_PCREL_LO16);
4489 if (! used_at)
4490 return;
4491 break;
252b5132
RH
4492 }
4493
4494 if (offset_expr.X_op != O_symbol
4495 && offset_expr.X_op != O_constant)
4496 {
4497 as_bad (_("expression too complex"));
4498 offset_expr.X_op = O_constant;
4499 }
4500
252b5132 4501 if (offset_expr.X_op == O_constant)
4d34fb5f
TS
4502 load_register (&icnt, tempreg, &offset_expr,
4503 ((mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
4504 ? (dbl || HAVE_64BIT_ADDRESSES)
4505 : HAVE_64BIT_ADDRESSES));
252b5132
RH
4506 else if (mips_pic == NO_PIC)
4507 {
d6bc6245 4508 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 4509 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
4510 Otherwise we want
4511 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
4512 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4513 If we have a constant, we need two instructions anyhow,
d6bc6245 4514 so we may as well always use the latter form.
76b3015f 4515
d6bc6245
TS
4516 With 64bit address space and a usable $at we want
4517 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4518 lui $at,<sym> (BFD_RELOC_HI16_S)
4519 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4520 daddiu $at,<sym> (BFD_RELOC_LO16)
4521 dsll32 $tempreg,0
4522 dadd $tempreg,$tempreg,$at
76b3015f 4523
d6bc6245
TS
4524 If $at is already in use, we use an path which is suboptimal
4525 on superscalar processors.
4526 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4527 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4528 dsll $tempreg,16
4529 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
4530 dsll $tempreg,16
4531 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
4532 */
f9419b05 4533 char *p = NULL;
d6bc6245 4534 if (HAVE_64BIT_ADDRESSES)
252b5132 4535 {
d6bc6245
TS
4536 /* We don't do GP optimization for now because RELAX_ENCODE can't
4537 hold the data for such large chunks. */
4538
98d3f06f
KH
4539 if (used_at == 0)
4540 {
4541 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4542 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4543 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4544 AT, (int) BFD_RELOC_HI16_S);
4545 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4546 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4547 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4548 AT, AT, (int) BFD_RELOC_LO16);
4549 macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
4550 "d,w,<", tempreg, tempreg, 0);
4551 macro_build (p, &icnt, (expressionS *) NULL, "dadd", "d,v,t",
4552 tempreg, tempreg, AT);
4553 used_at = 1;
4554 }
4555 else
4556 {
4557 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4558 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4559 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4560 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4561 macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4562 tempreg, tempreg, 16);
4563 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4564 tempreg, tempreg, (int) BFD_RELOC_HI16_S);
4565 macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4566 tempreg, tempreg, 16);
4567 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4568 tempreg, tempreg, (int) BFD_RELOC_LO16);
4569 }
4570 }
4571 else
4572 {
4573 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4574 && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
4575 {
4576 frag_grow (20);
4577 macro_build ((char *) NULL, &icnt, &offset_expr, "addiu",
c9914766
TS
4578 "t,r,j", tempreg, mips_gp_register,
4579 (int) BFD_RELOC_GPREL16);
98d3f06f
KH
4580 p = frag_var (rs_machine_dependent, 8, 0,
4581 RELAX_ENCODE (4, 8, 0, 4, 0,
4582 mips_opts.warn_about_macros),
4583 offset_expr.X_add_symbol, 0, NULL);
4584 }
4585 macro_build_lui (p, &icnt, &offset_expr, tempreg);
4586 if (p != NULL)
4587 p += 4;
4588 macro_build (p, &icnt, &offset_expr, "addiu",
4589 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4590 }
252b5132
RH
4591 }
4592 else if (mips_pic == SVR4_PIC && ! mips_big_got)
4593 {
9117d219
NC
4594 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
4595
252b5132
RH
4596 /* If this is a reference to an external symbol, and there
4597 is no constant, we want
4598 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9117d219
NC
4599 or if tempreg is PIC_CALL_REG
4600 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
252b5132
RH
4601 For a local symbol, we want
4602 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4603 nop
4604 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4605
4606 If we have a small constant, and this is a reference to
4607 an external symbol, we want
4608 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4609 nop
4610 addiu $tempreg,$tempreg,<constant>
4611 For a local symbol, we want the same instruction
4612 sequence, but we output a BFD_RELOC_LO16 reloc on the
4613 addiu instruction.
4614
4615 If we have a large constant, and this is a reference to
4616 an external symbol, we want
4617 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4618 lui $at,<hiconstant>
4619 addiu $at,$at,<loconstant>
4620 addu $tempreg,$tempreg,$at
4621 For a local symbol, we want the same instruction
4622 sequence, but we output a BFD_RELOC_LO16 reloc on the
4623 addiu instruction. */
4624 expr1.X_add_number = offset_expr.X_add_number;
4625 offset_expr.X_add_number = 0;
4626 frag_grow (32);
9117d219
NC
4627 if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4628 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
4d34fb5f
TS
4629 macro_build ((char *) NULL, &icnt, &offset_expr,
4630 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
c9914766 4631 "t,o(b)", tempreg, lw_reloc_type, mips_gp_register);
252b5132
RH
4632 if (expr1.X_add_number == 0)
4633 {
4634 int off;
f9419b05 4635 char *p;
252b5132
RH
4636
4637 if (breg == 0)
4638 off = 0;
4639 else
4640 {
4641 /* We're going to put in an addu instruction using
4642 tempreg, so we may as well insert the nop right
4643 now. */
4644 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4645 "nop", "");
4646 off = 4;
4647 }
4648 p = frag_var (rs_machine_dependent, 8 - off, 0,
4649 RELAX_ENCODE (0, 8 - off, -4 - off, 4 - off, 0,
4650 (breg == 0
4651 ? mips_opts.warn_about_macros
4652 : 0)),
c4e7957c 4653 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
4654 if (breg == 0)
4655 {
4656 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4657 p += 4;
4658 }
4659 macro_build (p, &icnt, &expr1,
ca4e0257 4660 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
4661 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4662 /* FIXME: If breg == 0, and the next instruction uses
4663 $tempreg, then if this variant case is used an extra
4664 nop will be generated. */
4665 }
4666 else if (expr1.X_add_number >= -0x8000
4667 && expr1.X_add_number < 0x8000)
4668 {
4669 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4670 "nop", "");
4671 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 4672 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132 4673 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
c4e7957c
TS
4674 frag_var (rs_machine_dependent, 0, 0,
4675 RELAX_ENCODE (0, 0, -12, -4, 0, 0),
4676 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
4677 }
4678 else
4679 {
4680 int off1;
4681
4682 /* If we are going to add in a base register, and the
4683 target register and the base register are the same,
4684 then we are using AT as a temporary register. Since
4685 we want to load the constant into AT, we add our
4686 current AT (from the global offset table) and the
4687 register into the register now, and pretend we were
4688 not using a base register. */
4689 if (breg != treg)
4690 off1 = 0;
4691 else
4692 {
4693 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4694 "nop", "");
4695 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 4696 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
4697 "d,v,t", treg, AT, breg);
4698 breg = 0;
4699 tempreg = treg;
4700 off1 = -8;
4701 }
4702
4703 /* Set mips_optimize around the lui instruction to avoid
4704 inserting an unnecessary nop after the lw. */
4705 hold_mips_optimize = mips_optimize;
4706 mips_optimize = 2;
c4e7957c 4707 macro_build_lui (NULL, &icnt, &expr1, AT);
252b5132
RH
4708 mips_optimize = hold_mips_optimize;
4709
4710 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 4711 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
4712 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4713 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 4714 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132 4715 "d,v,t", tempreg, tempreg, AT);
c4e7957c
TS
4716 frag_var (rs_machine_dependent, 0, 0,
4717 RELAX_ENCODE (0, 0, -16 + off1, -8, 0, 0),
4718 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
4719 used_at = 1;
4720 }
4721 }
4722 else if (mips_pic == SVR4_PIC)
4723 {
4724 int gpdel;
f9419b05 4725 char *p;
9117d219
NC
4726 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
4727 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
252b5132
RH
4728
4729 /* This is the large GOT case. If this is a reference to an
4730 external symbol, and there is no constant, we want
4731 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
4732 addu $tempreg,$tempreg,$gp
4733 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
9117d219
NC
4734 or if tempreg is PIC_CALL_REG
4735 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
4736 addu $tempreg,$tempreg,$gp
4737 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
252b5132
RH
4738 For a local symbol, we want
4739 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4740 nop
4741 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4742
4743 If we have a small constant, and this is a reference to
4744 an external symbol, we want
4745 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
4746 addu $tempreg,$tempreg,$gp
4747 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4748 nop
4749 addiu $tempreg,$tempreg,<constant>
4750 For a local symbol, we want
4751 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4752 nop
4753 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
4754
4755 If we have a large constant, and this is a reference to
4756 an external symbol, we want
4757 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
4758 addu $tempreg,$tempreg,$gp
4759 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4760 lui $at,<hiconstant>
4761 addiu $at,$at,<loconstant>
4762 addu $tempreg,$tempreg,$at
4763 For a local symbol, we want
4764 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4765 lui $at,<hiconstant>
4766 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
4767 addu $tempreg,$tempreg,$at
438c16b8
TS
4768
4769 For NewABI, we want for data addresses
4770 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
4771 If tempreg is PIC_CALL_REG pointing to a external symbol, we want
4772 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
4773 */
4774 if (HAVE_NEWABI)
4775 {
4776 int reloc_type = (tempreg == PIC_CALL_REG
4777 ? BFD_RELOC_MIPS_CALL16
4778 : BFD_RELOC_MIPS_GOT_DISP);
4779
4780 macro_build ((char *) NULL, &icnt, &offset_expr,
4781 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4782 "t,o(b)", tempreg, reloc_type, mips_gp_register);
4783
4784 if (breg != 0)
4785 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4786 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4787 "d,v,t", treg, tempreg, breg);
4788
4789 if (! used_at)
4790 return;
4791
4792 break;
4793 }
252b5132
RH
4794 expr1.X_add_number = offset_expr.X_add_number;
4795 offset_expr.X_add_number = 0;
4796 frag_grow (52);
f7ea7ef2 4797 if (reg_needs_delay (mips_gp_register))
252b5132
RH
4798 gpdel = 4;
4799 else
4800 gpdel = 0;
9117d219
NC
4801 if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4802 {
4803 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
4804 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
4805 }
252b5132 4806 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
9117d219 4807 tempreg, lui_reloc_type);
252b5132 4808 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 4809 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 4810 "d,v,t", tempreg, tempreg, mips_gp_register);
252b5132 4811 macro_build ((char *) NULL, &icnt, &offset_expr,
4d34fb5f 4812 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
9117d219 4813 "t,o(b)", tempreg, lw_reloc_type, tempreg);
252b5132
RH
4814 if (expr1.X_add_number == 0)
4815 {
4816 int off;
4817
4818 if (breg == 0)
4819 off = 0;
4820 else
4821 {
4822 /* We're going to put in an addu instruction using
4823 tempreg, so we may as well insert the nop right
4824 now. */
4825 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4826 "nop", "");
4827 off = 4;
4828 }
4829
4830 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4831 RELAX_ENCODE (12 + off, 12 + gpdel, gpdel,
4832 8 + gpdel, 0,
4833 (breg == 0
4834 ? mips_opts.warn_about_macros
4835 : 0)),
c4e7957c 4836 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
4837 }
4838 else if (expr1.X_add_number >= -0x8000
4839 && expr1.X_add_number < 0x8000)
4840 {
4841 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4842 "nop", "");
4843 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 4844 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
4845 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4846
4847 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4848 RELAX_ENCODE (20, 12 + gpdel, gpdel, 8 + gpdel, 0,
4849 (breg == 0
4850 ? mips_opts.warn_about_macros
4851 : 0)),
c4e7957c 4852 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
4853 }
4854 else
4855 {
4856 int adj, dreg;
4857
4858 /* If we are going to add in a base register, and the
4859 target register and the base register are the same,
4860 then we are using AT as a temporary register. Since
4861 we want to load the constant into AT, we add our
4862 current AT (from the global offset table) and the
4863 register into the register now, and pretend we were
4864 not using a base register. */
4865 if (breg != treg)
4866 {
4867 adj = 0;
4868 dreg = tempreg;
4869 }
4870 else
4871 {
4872 assert (tempreg == AT);
4873 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4874 "nop", "");
4875 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 4876 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
4877 "d,v,t", treg, AT, breg);
4878 dreg = treg;
4879 adj = 8;
4880 }
4881
4882 /* Set mips_optimize around the lui instruction to avoid
4883 inserting an unnecessary nop after the lw. */
4884 hold_mips_optimize = mips_optimize;
4885 mips_optimize = 2;
c4e7957c 4886 macro_build_lui (NULL, &icnt, &expr1, AT);
252b5132
RH
4887 mips_optimize = hold_mips_optimize;
4888
4889 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 4890 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
4891 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4892 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 4893 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
4894 "d,v,t", dreg, dreg, AT);
4895
4896 p = frag_var (rs_machine_dependent, 16 + gpdel + adj, 0,
4897 RELAX_ENCODE (24 + adj, 16 + gpdel + adj, gpdel,
4898 8 + gpdel, 0,
4899 (breg == 0
4900 ? mips_opts.warn_about_macros
4901 : 0)),
c4e7957c 4902 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
4903
4904 used_at = 1;
4905 }
4906
4907 if (gpdel > 0)
4908 {
4909 /* This is needed because this instruction uses $gp, but
4910 the first instruction on the main stream does not. */
4911 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4912 p += 4;
4913 }
4914 macro_build (p, &icnt, &offset_expr,
4d34fb5f 4915 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
c9914766
TS
4916 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16,
4917 mips_gp_register);
252b5132
RH
4918 p += 4;
4919 if (expr1.X_add_number >= -0x8000
4920 && expr1.X_add_number < 0x8000)
4921 {
4922 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4923 p += 4;
4924 macro_build (p, &icnt, &expr1,
ca4e0257 4925 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
4926 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4927 /* FIXME: If add_number is 0, and there was no base
4928 register, the external symbol case ended with a load,
4929 so if the symbol turns out to not be external, and
4930 the next instruction uses tempreg, an unnecessary nop
4931 will be inserted. */
4932 }
4933 else
4934 {
4935 if (breg == treg)
4936 {
4937 /* We must add in the base register now, as in the
4938 external symbol case. */
4939 assert (tempreg == AT);
4940 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4941 p += 4;
4942 macro_build (p, &icnt, (expressionS *) NULL,
ca4e0257 4943 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
4944 "d,v,t", treg, AT, breg);
4945 p += 4;
4946 tempreg = treg;
4947 /* We set breg to 0 because we have arranged to add
4948 it in in both cases. */
4949 breg = 0;
4950 }
4951
4952 macro_build_lui (p, &icnt, &expr1, AT);
4953 p += 4;
4954 macro_build (p, &icnt, &expr1,
ca4e0257 4955 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
4956 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4957 p += 4;
4958 macro_build (p, &icnt, (expressionS *) NULL,
ca4e0257 4959 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
4960 "d,v,t", tempreg, tempreg, AT);
4961 p += 4;
4962 }
4963 }
4964 else if (mips_pic == EMBEDDED_PIC)
4965 {
4966 /* We use
cdf6fd85 4967 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
4968 */
4969 macro_build ((char *) NULL, &icnt, &offset_expr,
c9914766
TS
4970 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
4971 tempreg, mips_gp_register, (int) BFD_RELOC_GPREL16);
252b5132
RH
4972 }
4973 else
4974 abort ();
4975
4976 if (breg != 0)
4d34fb5f
TS
4977 {
4978 char *s;
4979
4980 if (mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
4981 s = (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu";
4982 else
4983 s = HAVE_64BIT_ADDRESSES ? "daddu" : "addu";
4984
4985 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s,
4986 "d,v,t", treg, tempreg, breg);
4987 }
252b5132
RH
4988
4989 if (! used_at)
4990 return;
4991
4992 break;
4993
4994 case M_J_A:
4995 /* The j instruction may not be used in PIC code, since it
4996 requires an absolute address. We convert it to a b
4997 instruction. */
4998 if (mips_pic == NO_PIC)
4999 macro_build ((char *) NULL, &icnt, &offset_expr, "j", "a");
5000 else
5001 macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
5002 return;
5003
5004 /* The jal instructions must be handled as macros because when
5005 generating PIC code they expand to multi-instruction
5006 sequences. Normally they are simple instructions. */
5007 case M_JAL_1:
5008 dreg = RA;
5009 /* Fall through. */
5010 case M_JAL_2:
5011 if (mips_pic == NO_PIC
5012 || mips_pic == EMBEDDED_PIC)
5013 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5014 "d,s", dreg, sreg);
5015 else if (mips_pic == SVR4_PIC)
5016 {
5017 if (sreg != PIC_CALL_REG)
5018 as_warn (_("MIPS PIC call to register other than $25"));
bdaaa2e1 5019
252b5132
RH
5020 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5021 "d,s", dreg, sreg);
6478892d 5022 if (! HAVE_NEWABI)
252b5132 5023 {
6478892d
TS
5024 if (mips_cprestore_offset < 0)
5025 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5026 else
5027 {
7a621144
DJ
5028 if (! mips_frame_reg_valid)
5029 {
5030 as_warn (_("No .frame pseudo-op used in PIC code"));
5031 /* Quiet this warning. */
5032 mips_frame_reg_valid = 1;
5033 }
5034 if (! mips_cprestore_valid)
5035 {
5036 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5037 /* Quiet this warning. */
5038 mips_cprestore_valid = 1;
5039 }
6478892d
TS
5040 expr1.X_add_number = mips_cprestore_offset;
5041 macro_build ((char *) NULL, &icnt, &expr1,
5042 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
c9914766
TS
5043 mips_gp_register, (int) BFD_RELOC_LO16,
5044 mips_frame_reg);
6478892d 5045 }
252b5132
RH
5046 }
5047 }
5048 else
5049 abort ();
5050
5051 return;
5052
5053 case M_JAL_A:
5054 if (mips_pic == NO_PIC)
5055 macro_build ((char *) NULL, &icnt, &offset_expr, "jal", "a");
5056 else if (mips_pic == SVR4_PIC)
5057 {
f9419b05
TS
5058 char *p;
5059
252b5132
RH
5060 /* If this is a reference to an external symbol, and we are
5061 using a small GOT, we want
5062 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5063 nop
f9419b05 5064 jalr $ra,$25
252b5132
RH
5065 nop
5066 lw $gp,cprestore($sp)
5067 The cprestore value is set using the .cprestore
5068 pseudo-op. If we are using a big GOT, we want
5069 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5070 addu $25,$25,$gp
5071 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
5072 nop
f9419b05 5073 jalr $ra,$25
252b5132
RH
5074 nop
5075 lw $gp,cprestore($sp)
5076 If the symbol is not external, we want
5077 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5078 nop
5079 addiu $25,$25,<sym> (BFD_RELOC_LO16)
f9419b05 5080 jalr $ra,$25
252b5132 5081 nop
438c16b8
TS
5082 lw $gp,cprestore($sp)
5083 For NewABI, we want
5084 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5085 jalr $ra,$25 (BFD_RELOC_MIPS_JALR)
5086 */
5087 if (HAVE_NEWABI)
252b5132
RH
5088 {
5089 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 5090 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132 5091 "t,o(b)", PIC_CALL_REG,
438c16b8
TS
5092 (int) BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5093 macro_build_jalr (icnt, &offset_expr);
252b5132
RH
5094 }
5095 else
5096 {
438c16b8
TS
5097 frag_grow (40);
5098 if (! mips_big_got)
5099 {
5100 macro_build ((char *) NULL, &icnt, &offset_expr,
5101 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5102 "t,o(b)", PIC_CALL_REG,
5103 (int) BFD_RELOC_MIPS_CALL16, mips_gp_register);
5104 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5105 "nop", "");
5106 p = frag_var (rs_machine_dependent, 4, 0,
5107 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5108 offset_expr.X_add_symbol, 0, NULL);
5109 }
252b5132 5110 else
252b5132 5111 {
438c16b8
TS
5112 int gpdel;
5113
5114 if (reg_needs_delay (mips_gp_register))
5115 gpdel = 4;
5116 else
5117 gpdel = 0;
5118 macro_build ((char *) NULL, &icnt, &offset_expr, "lui",
5119 "t,u", PIC_CALL_REG,
5120 (int) BFD_RELOC_MIPS_CALL_HI16);
5121 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5122 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5123 "d,v,t", PIC_CALL_REG, PIC_CALL_REG,
5124 mips_gp_register);
5125 macro_build ((char *) NULL, &icnt, &offset_expr,
5126 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5127 "t,o(b)", PIC_CALL_REG,
5128 (int) BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
5129 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5130 "nop", "");
5131 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5132 RELAX_ENCODE (16, 12 + gpdel, gpdel,
5133 8 + gpdel, 0, 0),
5134 offset_expr.X_add_symbol, 0, NULL);
5135 if (gpdel > 0)
5136 {
5137 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5138 p += 4;
5139 }
5140 macro_build (p, &icnt, &offset_expr,
5141 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5142 "t,o(b)", PIC_CALL_REG,
5143 (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
5144 p += 4;
252b5132
RH
5145 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5146 p += 4;
5147 }
5148 macro_build (p, &icnt, &offset_expr,
438c16b8
TS
5149 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5150 "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
5151 (int) BFD_RELOC_LO16);
5152 macro_build_jalr (icnt, &offset_expr);
5153
6478892d
TS
5154 if (mips_cprestore_offset < 0)
5155 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5156 else
5157 {
7a621144
DJ
5158 if (! mips_frame_reg_valid)
5159 {
5160 as_warn (_("No .frame pseudo-op used in PIC code"));
5161 /* Quiet this warning. */
5162 mips_frame_reg_valid = 1;
5163 }
5164 if (! mips_cprestore_valid)
5165 {
5166 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5167 /* Quiet this warning. */
5168 mips_cprestore_valid = 1;
5169 }
6478892d
TS
5170 if (mips_opts.noreorder)
5171 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
98d3f06f 5172 "nop", "");
6478892d
TS
5173 expr1.X_add_number = mips_cprestore_offset;
5174 macro_build ((char *) NULL, &icnt, &expr1,
5175 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
c9914766
TS
5176 mips_gp_register, (int) BFD_RELOC_LO16,
5177 mips_frame_reg);
6478892d 5178 }
252b5132
RH
5179 }
5180 }
5181 else if (mips_pic == EMBEDDED_PIC)
5182 {
5183 macro_build ((char *) NULL, &icnt, &offset_expr, "bal", "p");
5184 /* The linker may expand the call to a longer sequence which
5185 uses $at, so we must break rather than return. */
5186 break;
5187 }
5188 else
5189 abort ();
5190
5191 return;
5192
5193 case M_LB_AB:
5194 s = "lb";
5195 goto ld;
5196 case M_LBU_AB:
5197 s = "lbu";
5198 goto ld;
5199 case M_LH_AB:
5200 s = "lh";
5201 goto ld;
5202 case M_LHU_AB:
5203 s = "lhu";
5204 goto ld;
5205 case M_LW_AB:
5206 s = "lw";
5207 goto ld;
5208 case M_LWC0_AB:
5209 s = "lwc0";
bdaaa2e1 5210 /* Itbl support may require additional care here. */
252b5132
RH
5211 coproc = 1;
5212 goto ld;
5213 case M_LWC1_AB:
5214 s = "lwc1";
bdaaa2e1 5215 /* Itbl support may require additional care here. */
252b5132
RH
5216 coproc = 1;
5217 goto ld;
5218 case M_LWC2_AB:
5219 s = "lwc2";
bdaaa2e1 5220 /* Itbl support may require additional care here. */
252b5132
RH
5221 coproc = 1;
5222 goto ld;
5223 case M_LWC3_AB:
5224 s = "lwc3";
bdaaa2e1 5225 /* Itbl support may require additional care here. */
252b5132
RH
5226 coproc = 1;
5227 goto ld;
5228 case M_LWL_AB:
5229 s = "lwl";
5230 lr = 1;
5231 goto ld;
5232 case M_LWR_AB:
5233 s = "lwr";
5234 lr = 1;
5235 goto ld;
5236 case M_LDC1_AB:
ec68c924 5237 if (mips_arch == CPU_R4650)
252b5132
RH
5238 {
5239 as_bad (_("opcode not supported on this processor"));
5240 return;
5241 }
5242 s = "ldc1";
bdaaa2e1 5243 /* Itbl support may require additional care here. */
252b5132
RH
5244 coproc = 1;
5245 goto ld;
5246 case M_LDC2_AB:
5247 s = "ldc2";
bdaaa2e1 5248 /* Itbl support may require additional care here. */
252b5132
RH
5249 coproc = 1;
5250 goto ld;
5251 case M_LDC3_AB:
5252 s = "ldc3";
bdaaa2e1 5253 /* Itbl support may require additional care here. */
252b5132
RH
5254 coproc = 1;
5255 goto ld;
5256 case M_LDL_AB:
5257 s = "ldl";
5258 lr = 1;
5259 goto ld;
5260 case M_LDR_AB:
5261 s = "ldr";
5262 lr = 1;
5263 goto ld;
5264 case M_LL_AB:
5265 s = "ll";
5266 goto ld;
5267 case M_LLD_AB:
5268 s = "lld";
5269 goto ld;
5270 case M_LWU_AB:
5271 s = "lwu";
5272 ld:
5273 if (breg == treg || coproc || lr)
5274 {
5275 tempreg = AT;
5276 used_at = 1;
5277 }
5278 else
5279 {
5280 tempreg = treg;
5281 used_at = 0;
5282 }
5283 goto ld_st;
5284 case M_SB_AB:
5285 s = "sb";
5286 goto st;
5287 case M_SH_AB:
5288 s = "sh";
5289 goto st;
5290 case M_SW_AB:
5291 s = "sw";
5292 goto st;
5293 case M_SWC0_AB:
5294 s = "swc0";
bdaaa2e1 5295 /* Itbl support may require additional care here. */
252b5132
RH
5296 coproc = 1;
5297 goto st;
5298 case M_SWC1_AB:
5299 s = "swc1";
bdaaa2e1 5300 /* Itbl support may require additional care here. */
252b5132
RH
5301 coproc = 1;
5302 goto st;
5303 case M_SWC2_AB:
5304 s = "swc2";
bdaaa2e1 5305 /* Itbl support may require additional care here. */
252b5132
RH
5306 coproc = 1;
5307 goto st;
5308 case M_SWC3_AB:
5309 s = "swc3";
bdaaa2e1 5310 /* Itbl support may require additional care here. */
252b5132
RH
5311 coproc = 1;
5312 goto st;
5313 case M_SWL_AB:
5314 s = "swl";
5315 goto st;
5316 case M_SWR_AB:
5317 s = "swr";
5318 goto st;
5319 case M_SC_AB:
5320 s = "sc";
5321 goto st;
5322 case M_SCD_AB:
5323 s = "scd";
5324 goto st;
5325 case M_SDC1_AB:
ec68c924 5326 if (mips_arch == CPU_R4650)
252b5132
RH
5327 {
5328 as_bad (_("opcode not supported on this processor"));
5329 return;
5330 }
5331 s = "sdc1";
5332 coproc = 1;
bdaaa2e1 5333 /* Itbl support may require additional care here. */
252b5132
RH
5334 goto st;
5335 case M_SDC2_AB:
5336 s = "sdc2";
bdaaa2e1 5337 /* Itbl support may require additional care here. */
252b5132
RH
5338 coproc = 1;
5339 goto st;
5340 case M_SDC3_AB:
5341 s = "sdc3";
bdaaa2e1 5342 /* Itbl support may require additional care here. */
252b5132
RH
5343 coproc = 1;
5344 goto st;
5345 case M_SDL_AB:
5346 s = "sdl";
5347 goto st;
5348 case M_SDR_AB:
5349 s = "sdr";
5350 st:
5351 tempreg = AT;
5352 used_at = 1;
5353 ld_st:
bdaaa2e1 5354 /* Itbl support may require additional care here. */
252b5132
RH
5355 if (mask == M_LWC1_AB
5356 || mask == M_SWC1_AB
5357 || mask == M_LDC1_AB
5358 || mask == M_SDC1_AB
5359 || mask == M_L_DAB
5360 || mask == M_S_DAB)
5361 fmt = "T,o(b)";
5362 else if (coproc)
5363 fmt = "E,o(b)";
5364 else
5365 fmt = "t,o(b)";
5366
afdbd6d0
CD
5367 /* For embedded PIC, we allow loads where the offset is calculated
5368 by subtracting a symbol in the current segment from an unknown
5369 symbol, relative to a base register, e.g.:
5370 <op> $treg, <sym>-<localsym>($breg)
5371 This is used by the compiler for switch statements. */
76b3015f 5372 if (mips_pic == EMBEDDED_PIC
afdbd6d0
CD
5373 && offset_expr.X_op == O_subtract
5374 && (symbol_constant_p (offset_expr.X_op_symbol)
5375 ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
5376 : (symbol_equated_p (offset_expr.X_op_symbol)
5377 && (S_GET_SEGMENT
5378 (symbol_get_value_expression (offset_expr.X_op_symbol)
5379 ->X_add_symbol)
5380 == now_seg)))
5381 && breg != 0
5382 && (offset_expr.X_add_number == 0
5383 || OUTPUT_FLAVOR == bfd_target_elf_flavour))
5384 {
5385 /* For this case, we output the instructions:
5386 lui $tempreg,<sym> (BFD_RELOC_PCREL_HI16_S)
5387 addiu $tempreg,$tempreg,$breg
5388 <op> $treg,<sym>($tempreg) (BFD_RELOC_PCREL_LO16)
5389 If the relocation would fit entirely in 16 bits, it would be
5390 nice to emit:
5391 <op> $treg,<sym>($breg) (BFD_RELOC_PCREL_LO16)
5392 instead, but that seems quite difficult. */
5393 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5394 tempreg, (int) BFD_RELOC_PCREL_HI16_S);
5395 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5396 ((bfd_arch_bits_per_address (stdoutput) == 32
5397 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
5398 ? "addu" : "daddu"),
5399 "d,v,t", tempreg, tempreg, breg);
5400 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt, treg,
5401 (int) BFD_RELOC_PCREL_LO16, tempreg);
5402 if (! used_at)
5403 return;
5404 break;
5405 }
5406
252b5132
RH
5407 if (offset_expr.X_op != O_constant
5408 && offset_expr.X_op != O_symbol)
5409 {
5410 as_bad (_("expression too complex"));
5411 offset_expr.X_op = O_constant;
5412 }
5413
5414 /* A constant expression in PIC code can be handled just as it
5415 is in non PIC code. */
5416 if (mips_pic == NO_PIC
5417 || offset_expr.X_op == O_constant)
5418 {
f9419b05
TS
5419 char *p;
5420
252b5132
RH
5421 /* If this is a reference to a GP relative symbol, and there
5422 is no base register, we want
cdf6fd85 5423 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
5424 Otherwise, if there is no base register, we want
5425 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5426 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5427 If we have a constant, we need two instructions anyhow,
5428 so we always use the latter form.
5429
5430 If we have a base register, and this is a reference to a
5431 GP relative symbol, we want
5432 addu $tempreg,$breg,$gp
cdf6fd85 5433 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
5434 Otherwise we want
5435 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5436 addu $tempreg,$tempreg,$breg
5437 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245 5438 With a constant we always use the latter case.
76b3015f 5439
d6bc6245
TS
5440 With 64bit address space and no base register and $at usable,
5441 we want
5442 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5443 lui $at,<sym> (BFD_RELOC_HI16_S)
5444 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5445 dsll32 $tempreg,0
5446 daddu $tempreg,$at
5447 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5448 If we have a base register, we want
5449 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5450 lui $at,<sym> (BFD_RELOC_HI16_S)
5451 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5452 daddu $at,$breg
5453 dsll32 $tempreg,0
5454 daddu $tempreg,$at
5455 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5456
5457 Without $at we can't generate the optimal path for superscalar
5458 processors here since this would require two temporary registers.
5459 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5460 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5461 dsll $tempreg,16
5462 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5463 dsll $tempreg,16
5464 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5465 If we have a base register, we want
5466 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5467 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5468 dsll $tempreg,16
5469 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5470 dsll $tempreg,16
5471 daddu $tempreg,$tempreg,$breg
5472 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6373ee54
CD
5473
5474 If we have 64-bit addresses, as an optimization, for
5475 addresses which are 32-bit constants (e.g. kseg0/kseg1
5476 addresses) we fall back to the 32-bit address generation
5477 mechanism since it is more efficient. This code should
5478 probably attempt to generate 64-bit constants more
5479 efficiently in general.
d6bc6245 5480 */
6373ee54
CD
5481 if (HAVE_64BIT_ADDRESSES
5482 && !(offset_expr.X_op == O_constant
5483 && IS_SEXT_32BIT_NUM (offset_expr.X_add_number)))
d6bc6245
TS
5484 {
5485 p = NULL;
5486
5487 /* We don't do GP optimization for now because RELAX_ENCODE can't
5488 hold the data for such large chunks. */
5489
5490 if (used_at == 0)
5491 {
5492 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5493 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5494 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5495 AT, (int) BFD_RELOC_HI16_S);
5496 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5497 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5498 if (breg != 0)
2396cfb9
TS
5499 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5500 "d,v,t", AT, AT, breg);
5501 macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
5502 "d,w,<", tempreg, tempreg, 0);
5503 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5504 "d,v,t", tempreg, tempreg, AT);
d6bc6245
TS
5505 macro_build (p, &icnt, &offset_expr, s,
5506 fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5507 used_at = 1;
5508 }
5509 else
5510 {
5511 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5512 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5513 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5514 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
2396cfb9
TS
5515 macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5516 "d,w,<", tempreg, tempreg, 16);
d6bc6245
TS
5517 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5518 tempreg, tempreg, (int) BFD_RELOC_HI16_S);
2396cfb9
TS
5519 macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5520 "d,w,<", tempreg, tempreg, 16);
d6bc6245 5521 if (breg != 0)
2396cfb9
TS
5522 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5523 "d,v,t", tempreg, tempreg, breg);
d6bc6245
TS
5524 macro_build (p, &icnt, &offset_expr, s,
5525 fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5526 }
5527
5528 return;
5529 }
76b3015f 5530
252b5132
RH
5531 if (breg == 0)
5532 {
e7d556df 5533 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
252b5132
RH
5534 || nopic_need_relax (offset_expr.X_add_symbol, 1))
5535 p = NULL;
5536 else
5537 {
5538 frag_grow (20);
5539 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
c9914766
TS
5540 treg, (int) BFD_RELOC_GPREL16,
5541 mips_gp_register);
252b5132
RH
5542 p = frag_var (rs_machine_dependent, 8, 0,
5543 RELAX_ENCODE (4, 8, 0, 4, 0,
5544 (mips_opts.warn_about_macros
5545 || (used_at
5546 && mips_opts.noat))),
956cd1d6 5547 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5548 used_at = 0;
5549 }
5550 macro_build_lui (p, &icnt, &offset_expr, tempreg);
5551 if (p != NULL)
5552 p += 4;
5553 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5554 (int) BFD_RELOC_LO16, tempreg);
5555 }
5556 else
5557 {
e7d556df 5558 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
252b5132
RH
5559 || nopic_need_relax (offset_expr.X_add_symbol, 1))
5560 p = NULL;
5561 else
5562 {
5563 frag_grow (28);
5564 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5565 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 5566 "d,v,t", tempreg, breg, mips_gp_register);
252b5132 5567 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
cdf6fd85 5568 treg, (int) BFD_RELOC_GPREL16, tempreg);
252b5132
RH
5569 p = frag_var (rs_machine_dependent, 12, 0,
5570 RELAX_ENCODE (8, 12, 0, 8, 0, 0),
956cd1d6 5571 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5572 }
5573 macro_build_lui (p, &icnt, &offset_expr, tempreg);
5574 if (p != NULL)
5575 p += 4;
5576 macro_build (p, &icnt, (expressionS *) NULL,
ca4e0257 5577 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5578 "d,v,t", tempreg, tempreg, breg);
5579 if (p != NULL)
5580 p += 4;
5581 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5582 (int) BFD_RELOC_LO16, tempreg);
5583 }
5584 }
5585 else if (mips_pic == SVR4_PIC && ! mips_big_got)
5586 {
f9419b05
TS
5587 char *p;
5588
252b5132
RH
5589 /* If this is a reference to an external symbol, we want
5590 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5591 nop
5592 <op> $treg,0($tempreg)
5593 Otherwise we want
5594 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5595 nop
5596 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5597 <op> $treg,0($tempreg)
5598 If there is a base register, we add it to $tempreg before
5599 the <op>. If there is a constant, we stick it in the
5600 <op> instruction. We don't handle constants larger than
5601 16 bits, because we have no way to load the upper 16 bits
5602 (actually, we could handle them for the subset of cases
5603 in which we are not using $at). */
5604 assert (offset_expr.X_op == O_symbol);
5605 expr1.X_add_number = offset_expr.X_add_number;
5606 offset_expr.X_add_number = 0;
5607 if (expr1.X_add_number < -0x8000
5608 || expr1.X_add_number >= 0x8000)
5609 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5610 frag_grow (20);
5611 macro_build ((char *) NULL, &icnt, &offset_expr,
c9914766
TS
5612 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", tempreg,
5613 (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132 5614 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
bdaaa2e1 5615 p = frag_var (rs_machine_dependent, 4, 0,
252b5132 5616 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
c4e7957c 5617 offset_expr.X_add_symbol, 0, NULL);
252b5132 5618 macro_build (p, &icnt, &offset_expr,
ca4e0257 5619 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
5620 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5621 if (breg != 0)
5622 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5623 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5624 "d,v,t", tempreg, tempreg, breg);
5625 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5626 (int) BFD_RELOC_LO16, tempreg);
5627 }
5628 else if (mips_pic == SVR4_PIC)
5629 {
5630 int gpdel;
f9419b05 5631 char *p;
252b5132
RH
5632
5633 /* If this is a reference to an external symbol, we want
5634 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5635 addu $tempreg,$tempreg,$gp
5636 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5637 <op> $treg,0($tempreg)
5638 Otherwise we want
5639 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5640 nop
5641 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5642 <op> $treg,0($tempreg)
5643 If there is a base register, we add it to $tempreg before
5644 the <op>. If there is a constant, we stick it in the
5645 <op> instruction. We don't handle constants larger than
5646 16 bits, because we have no way to load the upper 16 bits
5647 (actually, we could handle them for the subset of cases
438c16b8
TS
5648 in which we are not using $at).
5649
5650 For NewABI, we want
5651 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
5652 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
5653 <op> $treg,0($tempreg)
5654 */
252b5132
RH
5655 assert (offset_expr.X_op == O_symbol);
5656 expr1.X_add_number = offset_expr.X_add_number;
5657 offset_expr.X_add_number = 0;
5658 if (expr1.X_add_number < -0x8000
5659 || expr1.X_add_number >= 0x8000)
5660 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
438c16b8
TS
5661 if (HAVE_NEWABI)
5662 {
5663 macro_build ((char *) NULL, &icnt, &offset_expr,
5664 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5665 "t,o(b)", tempreg, BFD_RELOC_MIPS_GOT_PAGE,
5666 mips_gp_register);
5667 macro_build ((char *) NULL, &icnt, &offset_expr,
5668 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5669 "t,r,j", tempreg, tempreg,
5670 BFD_RELOC_MIPS_GOT_OFST);
5671 if (breg != 0)
5672 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5673 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5674 "d,v,t", tempreg, tempreg, breg);
5675 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5676 (int) BFD_RELOC_LO16, tempreg);
5677
5678 if (! used_at)
5679 return;
5680
5681 break;
5682 }
f7ea7ef2 5683 if (reg_needs_delay (mips_gp_register))
252b5132
RH
5684 gpdel = 4;
5685 else
5686 gpdel = 0;
5687 frag_grow (36);
5688 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5689 tempreg, (int) BFD_RELOC_MIPS_GOT_HI16);
5690 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5691 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 5692 "d,v,t", tempreg, tempreg, mips_gp_register);
252b5132 5693 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 5694 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
5695 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_LO16,
5696 tempreg);
5697 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5698 RELAX_ENCODE (12, 12 + gpdel, gpdel, 8 + gpdel, 0, 0),
c4e7957c 5699 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5700 if (gpdel > 0)
5701 {
5702 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5703 p += 4;
5704 }
5705 macro_build (p, &icnt, &offset_expr,
ca4e0257 5706 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
c9914766
TS
5707 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16,
5708 mips_gp_register);
252b5132
RH
5709 p += 4;
5710 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5711 p += 4;
5712 macro_build (p, &icnt, &offset_expr,
ca4e0257 5713 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
5714 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5715 if (breg != 0)
5716 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5717 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5718 "d,v,t", tempreg, tempreg, breg);
5719 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5720 (int) BFD_RELOC_LO16, tempreg);
5721 }
5722 else if (mips_pic == EMBEDDED_PIC)
5723 {
5724 /* If there is no base register, we want
cdf6fd85 5725 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
5726 If there is a base register, we want
5727 addu $tempreg,$breg,$gp
cdf6fd85 5728 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
5729 */
5730 assert (offset_expr.X_op == O_symbol);
5731 if (breg == 0)
5732 {
5733 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
c9914766 5734 treg, (int) BFD_RELOC_GPREL16, mips_gp_register);
252b5132
RH
5735 used_at = 0;
5736 }
5737 else
5738 {
5739 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5740 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 5741 "d,v,t", tempreg, breg, mips_gp_register);
252b5132 5742 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
cdf6fd85 5743 treg, (int) BFD_RELOC_GPREL16, tempreg);
252b5132
RH
5744 }
5745 }
5746 else
5747 abort ();
5748
5749 if (! used_at)
5750 return;
5751
5752 break;
5753
5754 case M_LI:
5755 case M_LI_S:
5756 load_register (&icnt, treg, &imm_expr, 0);
5757 return;
5758
5759 case M_DLI:
5760 load_register (&icnt, treg, &imm_expr, 1);
5761 return;
5762
5763 case M_LI_SS:
5764 if (imm_expr.X_op == O_constant)
5765 {
5766 load_register (&icnt, AT, &imm_expr, 0);
5767 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5768 "mtc1", "t,G", AT, treg);
5769 break;
5770 }
5771 else
5772 {
5773 assert (offset_expr.X_op == O_symbol
5774 && strcmp (segment_name (S_GET_SEGMENT
5775 (offset_expr.X_add_symbol)),
5776 ".lit4") == 0
5777 && offset_expr.X_add_number == 0);
5778 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
c9914766 5779 treg, (int) BFD_RELOC_MIPS_LITERAL, mips_gp_register);
252b5132
RH
5780 return;
5781 }
5782
5783 case M_LI_D:
ca4e0257
RS
5784 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
5785 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
5786 order 32 bits of the value and the low order 32 bits are either
5787 zero or in OFFSET_EXPR. */
252b5132
RH
5788 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
5789 {
ca4e0257 5790 if (HAVE_64BIT_GPRS)
252b5132
RH
5791 load_register (&icnt, treg, &imm_expr, 1);
5792 else
5793 {
5794 int hreg, lreg;
5795
5796 if (target_big_endian)
5797 {
5798 hreg = treg;
5799 lreg = treg + 1;
5800 }
5801 else
5802 {
5803 hreg = treg + 1;
5804 lreg = treg;
5805 }
5806
5807 if (hreg <= 31)
5808 load_register (&icnt, hreg, &imm_expr, 0);
5809 if (lreg <= 31)
5810 {
5811 if (offset_expr.X_op == O_absent)
ea1fb5dc 5812 move_register (&icnt, lreg, 0);
252b5132
RH
5813 else
5814 {
5815 assert (offset_expr.X_op == O_constant);
5816 load_register (&icnt, lreg, &offset_expr, 0);
5817 }
5818 }
5819 }
5820 return;
5821 }
5822
5823 /* We know that sym is in the .rdata section. First we get the
5824 upper 16 bits of the address. */
5825 if (mips_pic == NO_PIC)
5826 {
956cd1d6 5827 macro_build_lui (NULL, &icnt, &offset_expr, AT);
252b5132
RH
5828 }
5829 else if (mips_pic == SVR4_PIC)
5830 {
5831 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 5832 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
c9914766
TS
5833 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
5834 mips_gp_register);
252b5132
RH
5835 }
5836 else if (mips_pic == EMBEDDED_PIC)
5837 {
5838 /* For embedded PIC we pick up the entire address off $gp in
5839 a single instruction. */
5840 macro_build ((char *) NULL, &icnt, &offset_expr,
c9914766
TS
5841 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j", AT,
5842 mips_gp_register, (int) BFD_RELOC_GPREL16);
252b5132
RH
5843 offset_expr.X_op = O_constant;
5844 offset_expr.X_add_number = 0;
5845 }
5846 else
5847 abort ();
bdaaa2e1 5848
252b5132 5849 /* Now we load the register(s). */
ca4e0257 5850 if (HAVE_64BIT_GPRS)
252b5132
RH
5851 macro_build ((char *) NULL, &icnt, &offset_expr, "ld", "t,o(b)",
5852 treg, (int) BFD_RELOC_LO16, AT);
5853 else
5854 {
5855 macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
5856 treg, (int) BFD_RELOC_LO16, AT);
f9419b05 5857 if (treg != RA)
252b5132
RH
5858 {
5859 /* FIXME: How in the world do we deal with the possible
5860 overflow here? */
5861 offset_expr.X_add_number += 4;
5862 macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
5863 treg + 1, (int) BFD_RELOC_LO16, AT);
5864 }
5865 }
5866
5867 /* To avoid confusion in tc_gen_reloc, we must ensure that this
5868 does not become a variant frag. */
5869 frag_wane (frag_now);
5870 frag_new (0);
5871
5872 break;
5873
5874 case M_LI_DD:
ca4e0257
RS
5875 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
5876 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
5877 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
5878 the value and the low order 32 bits are either zero or in
5879 OFFSET_EXPR. */
252b5132
RH
5880 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
5881 {
ca4e0257
RS
5882 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_FPRS);
5883 if (HAVE_64BIT_FPRS)
5884 {
5885 assert (HAVE_64BIT_GPRS);
5886 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5887 "dmtc1", "t,S", AT, treg);
5888 }
252b5132
RH
5889 else
5890 {
5891 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5892 "mtc1", "t,G", AT, treg + 1);
5893 if (offset_expr.X_op == O_absent)
5894 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5895 "mtc1", "t,G", 0, treg);
5896 else
5897 {
5898 assert (offset_expr.X_op == O_constant);
5899 load_register (&icnt, AT, &offset_expr, 0);
5900 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5901 "mtc1", "t,G", AT, treg);
5902 }
5903 }
5904 break;
5905 }
5906
5907 assert (offset_expr.X_op == O_symbol
5908 && offset_expr.X_add_number == 0);
5909 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
5910 if (strcmp (s, ".lit8") == 0)
5911 {
e7af610e 5912 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
5913 {
5914 macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
c9914766
TS
5915 "T,o(b)", treg, (int) BFD_RELOC_MIPS_LITERAL,
5916 mips_gp_register);
252b5132
RH
5917 return;
5918 }
c9914766 5919 breg = mips_gp_register;
252b5132
RH
5920 r = BFD_RELOC_MIPS_LITERAL;
5921 goto dob;
5922 }
5923 else
5924 {
5925 assert (strcmp (s, RDATA_SECTION_NAME) == 0);
5926 if (mips_pic == SVR4_PIC)
5927 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 5928 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
c9914766
TS
5929 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
5930 mips_gp_register);
252b5132
RH
5931 else
5932 {
5933 /* FIXME: This won't work for a 64 bit address. */
956cd1d6 5934 macro_build_lui (NULL, &icnt, &offset_expr, AT);
252b5132 5935 }
bdaaa2e1 5936
e7af610e 5937 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
5938 {
5939 macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
5940 "T,o(b)", treg, (int) BFD_RELOC_LO16, AT);
5941
5942 /* To avoid confusion in tc_gen_reloc, we must ensure
5943 that this does not become a variant frag. */
5944 frag_wane (frag_now);
5945 frag_new (0);
5946
5947 break;
5948 }
5949 breg = AT;
5950 r = BFD_RELOC_LO16;
5951 goto dob;
5952 }
5953
5954 case M_L_DOB:
ec68c924 5955 if (mips_arch == CPU_R4650)
252b5132
RH
5956 {
5957 as_bad (_("opcode not supported on this processor"));
5958 return;
5959 }
5960 /* Even on a big endian machine $fn comes before $fn+1. We have
5961 to adjust when loading from memory. */
5962 r = BFD_RELOC_LO16;
5963 dob:
e7af610e 5964 assert (mips_opts.isa == ISA_MIPS1);
252b5132
RH
5965 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
5966 target_big_endian ? treg + 1 : treg,
5967 (int) r, breg);
5968 /* FIXME: A possible overflow which I don't know how to deal
5969 with. */
5970 offset_expr.X_add_number += 4;
5971 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
5972 target_big_endian ? treg : treg + 1,
5973 (int) r, breg);
5974
5975 /* To avoid confusion in tc_gen_reloc, we must ensure that this
5976 does not become a variant frag. */
5977 frag_wane (frag_now);
5978 frag_new (0);
5979
5980 if (breg != AT)
5981 return;
5982 break;
5983
5984 case M_L_DAB:
5985 /*
5986 * The MIPS assembler seems to check for X_add_number not
5987 * being double aligned and generating:
5988 * lui at,%hi(foo+1)
5989 * addu at,at,v1
5990 * addiu at,at,%lo(foo+1)
5991 * lwc1 f2,0(at)
5992 * lwc1 f3,4(at)
5993 * But, the resulting address is the same after relocation so why
5994 * generate the extra instruction?
5995 */
ec68c924 5996 if (mips_arch == CPU_R4650)
252b5132
RH
5997 {
5998 as_bad (_("opcode not supported on this processor"));
5999 return;
6000 }
bdaaa2e1 6001 /* Itbl support may require additional care here. */
252b5132 6002 coproc = 1;
e7af610e 6003 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
6004 {
6005 s = "ldc1";
6006 goto ld;
6007 }
6008
6009 s = "lwc1";
6010 fmt = "T,o(b)";
6011 goto ldd_std;
6012
6013 case M_S_DAB:
ec68c924 6014 if (mips_arch == CPU_R4650)
252b5132
RH
6015 {
6016 as_bad (_("opcode not supported on this processor"));
6017 return;
6018 }
6019
e7af610e 6020 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
6021 {
6022 s = "sdc1";
6023 goto st;
6024 }
6025
6026 s = "swc1";
6027 fmt = "T,o(b)";
bdaaa2e1 6028 /* Itbl support may require additional care here. */
252b5132
RH
6029 coproc = 1;
6030 goto ldd_std;
6031
6032 case M_LD_AB:
ca4e0257 6033 if (HAVE_64BIT_GPRS)
252b5132
RH
6034 {
6035 s = "ld";
6036 goto ld;
6037 }
6038
6039 s = "lw";
6040 fmt = "t,o(b)";
6041 goto ldd_std;
6042
6043 case M_SD_AB:
ca4e0257 6044 if (HAVE_64BIT_GPRS)
252b5132
RH
6045 {
6046 s = "sd";
6047 goto st;
6048 }
6049
6050 s = "sw";
6051 fmt = "t,o(b)";
6052
6053 ldd_std:
afdbd6d0
CD
6054 /* We do _not_ bother to allow embedded PIC (symbol-local_symbol)
6055 loads for the case of doing a pair of loads to simulate an 'ld'.
6056 This is not currently done by the compiler, and assembly coders
6057 writing embedded-pic code can cope. */
6058
252b5132
RH
6059 if (offset_expr.X_op != O_symbol
6060 && offset_expr.X_op != O_constant)
6061 {
6062 as_bad (_("expression too complex"));
6063 offset_expr.X_op = O_constant;
6064 }
6065
6066 /* Even on a big endian machine $fn comes before $fn+1. We have
6067 to adjust when loading from memory. We set coproc if we must
6068 load $fn+1 first. */
bdaaa2e1 6069 /* Itbl support may require additional care here. */
252b5132
RH
6070 if (! target_big_endian)
6071 coproc = 0;
6072
6073 if (mips_pic == NO_PIC
6074 || offset_expr.X_op == O_constant)
6075 {
f9419b05
TS
6076 char *p;
6077
252b5132 6078 /* If this is a reference to a GP relative symbol, we want
cdf6fd85
TS
6079 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6080 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
6081 If we have a base register, we use this
6082 addu $at,$breg,$gp
cdf6fd85
TS
6083 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6084 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
6085 If this is not a GP relative symbol, we want
6086 lui $at,<sym> (BFD_RELOC_HI16_S)
6087 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6088 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6089 If there is a base register, we add it to $at after the
6090 lui instruction. If there is a constant, we always use
6091 the last case. */
e7d556df 6092 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
252b5132
RH
6093 || nopic_need_relax (offset_expr.X_add_symbol, 1))
6094 {
6095 p = NULL;
6096 used_at = 1;
6097 }
6098 else
6099 {
6100 int off;
6101
6102 if (breg == 0)
6103 {
6104 frag_grow (28);
c9914766 6105 tempreg = mips_gp_register;
252b5132
RH
6106 off = 0;
6107 used_at = 0;
6108 }
6109 else
6110 {
6111 frag_grow (36);
6112 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6113 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 6114 "d,v,t", AT, breg, mips_gp_register);
252b5132
RH
6115 tempreg = AT;
6116 off = 4;
6117 used_at = 1;
6118 }
6119
beae10d5 6120 /* Itbl support may require additional care here. */
252b5132
RH
6121 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6122 coproc ? treg + 1 : treg,
cdf6fd85 6123 (int) BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6124 offset_expr.X_add_number += 4;
6125
6126 /* Set mips_optimize to 2 to avoid inserting an
6127 undesired nop. */
6128 hold_mips_optimize = mips_optimize;
6129 mips_optimize = 2;
beae10d5 6130 /* Itbl support may require additional care here. */
252b5132
RH
6131 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6132 coproc ? treg : treg + 1,
cdf6fd85 6133 (int) BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6134 mips_optimize = hold_mips_optimize;
6135
6136 p = frag_var (rs_machine_dependent, 12 + off, 0,
6137 RELAX_ENCODE (8 + off, 12 + off, 0, 4 + off, 1,
6138 used_at && mips_opts.noat),
956cd1d6 6139 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
6140
6141 /* We just generated two relocs. When tc_gen_reloc
6142 handles this case, it will skip the first reloc and
6143 handle the second. The second reloc already has an
6144 extra addend of 4, which we added above. We must
6145 subtract it out, and then subtract another 4 to make
6146 the first reloc come out right. The second reloc
6147 will come out right because we are going to add 4 to
6148 offset_expr when we build its instruction below.
6149
6150 If we have a symbol, then we don't want to include
6151 the offset, because it will wind up being included
6152 when we generate the reloc. */
6153
6154 if (offset_expr.X_op == O_constant)
6155 offset_expr.X_add_number -= 8;
6156 else
6157 {
6158 offset_expr.X_add_number = -4;
6159 offset_expr.X_op = O_constant;
6160 }
6161 }
6162 macro_build_lui (p, &icnt, &offset_expr, AT);
6163 if (p != NULL)
6164 p += 4;
6165 if (breg != 0)
6166 {
6167 macro_build (p, &icnt, (expressionS *) NULL,
ca4e0257 6168 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
6169 "d,v,t", AT, breg, AT);
6170 if (p != NULL)
6171 p += 4;
6172 }
beae10d5 6173 /* Itbl support may require additional care here. */
252b5132
RH
6174 macro_build (p, &icnt, &offset_expr, s, fmt,
6175 coproc ? treg + 1 : treg,
6176 (int) BFD_RELOC_LO16, AT);
6177 if (p != NULL)
6178 p += 4;
6179 /* FIXME: How do we handle overflow here? */
6180 offset_expr.X_add_number += 4;
beae10d5 6181 /* Itbl support may require additional care here. */
252b5132
RH
6182 macro_build (p, &icnt, &offset_expr, s, fmt,
6183 coproc ? treg : treg + 1,
6184 (int) BFD_RELOC_LO16, AT);
bdaaa2e1 6185 }
252b5132
RH
6186 else if (mips_pic == SVR4_PIC && ! mips_big_got)
6187 {
6188 int off;
6189
6190 /* If this is a reference to an external symbol, we want
6191 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6192 nop
6193 <op> $treg,0($at)
6194 <op> $treg+1,4($at)
6195 Otherwise we want
6196 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6197 nop
6198 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6199 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6200 If there is a base register we add it to $at before the
6201 lwc1 instructions. If there is a constant we include it
6202 in the lwc1 instructions. */
6203 used_at = 1;
6204 expr1.X_add_number = offset_expr.X_add_number;
6205 offset_expr.X_add_number = 0;
6206 if (expr1.X_add_number < -0x8000
6207 || expr1.X_add_number >= 0x8000 - 4)
6208 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6209 if (breg == 0)
6210 off = 0;
6211 else
6212 off = 4;
6213 frag_grow (24 + off);
6214 macro_build ((char *) NULL, &icnt, &offset_expr,
c9914766
TS
6215 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", AT,
6216 (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132
RH
6217 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6218 if (breg != 0)
6219 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6220 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132 6221 "d,v,t", AT, breg, AT);
beae10d5 6222 /* Itbl support may require additional care here. */
252b5132
RH
6223 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6224 coproc ? treg + 1 : treg,
6225 (int) BFD_RELOC_LO16, AT);
6226 expr1.X_add_number += 4;
6227
6228 /* Set mips_optimize to 2 to avoid inserting an undesired
6229 nop. */
6230 hold_mips_optimize = mips_optimize;
6231 mips_optimize = 2;
beae10d5 6232 /* Itbl support may require additional care here. */
252b5132
RH
6233 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6234 coproc ? treg : treg + 1,
6235 (int) BFD_RELOC_LO16, AT);
6236 mips_optimize = hold_mips_optimize;
6237
6238 (void) frag_var (rs_machine_dependent, 0, 0,
6239 RELAX_ENCODE (0, 0, -16 - off, -8, 1, 0),
c4e7957c 6240 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
6241 }
6242 else if (mips_pic == SVR4_PIC)
6243 {
6244 int gpdel, off;
f9419b05 6245 char *p;
252b5132
RH
6246
6247 /* If this is a reference to an external symbol, we want
6248 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6249 addu $at,$at,$gp
6250 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
6251 nop
6252 <op> $treg,0($at)
6253 <op> $treg+1,4($at)
6254 Otherwise we want
6255 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6256 nop
6257 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6258 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6259 If there is a base register we add it to $at before the
6260 lwc1 instructions. If there is a constant we include it
6261 in the lwc1 instructions. */
6262 used_at = 1;
6263 expr1.X_add_number = offset_expr.X_add_number;
6264 offset_expr.X_add_number = 0;
6265 if (expr1.X_add_number < -0x8000
6266 || expr1.X_add_number >= 0x8000 - 4)
6267 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
c9914766 6268 if (reg_needs_delay (mips_gp_register))
252b5132
RH
6269 gpdel = 4;
6270 else
6271 gpdel = 0;
6272 if (breg == 0)
6273 off = 0;
6274 else
6275 off = 4;
6276 frag_grow (56);
6277 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
6278 AT, (int) BFD_RELOC_MIPS_GOT_HI16);
6279 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6280 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 6281 "d,v,t", AT, AT, mips_gp_register);
252b5132 6282 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 6283 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
6284 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT_LO16, AT);
6285 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6286 if (breg != 0)
6287 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6288 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132 6289 "d,v,t", AT, breg, AT);
beae10d5 6290 /* Itbl support may require additional care here. */
252b5132
RH
6291 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6292 coproc ? treg + 1 : treg,
6293 (int) BFD_RELOC_LO16, AT);
6294 expr1.X_add_number += 4;
6295
6296 /* Set mips_optimize to 2 to avoid inserting an undesired
6297 nop. */
6298 hold_mips_optimize = mips_optimize;
6299 mips_optimize = 2;
beae10d5 6300 /* Itbl support may require additional care here. */
252b5132
RH
6301 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6302 coproc ? treg : treg + 1,
6303 (int) BFD_RELOC_LO16, AT);
6304 mips_optimize = hold_mips_optimize;
6305 expr1.X_add_number -= 4;
6306
6307 p = frag_var (rs_machine_dependent, 16 + gpdel + off, 0,
6308 RELAX_ENCODE (24 + off, 16 + gpdel + off, gpdel,
6309 8 + gpdel + off, 1, 0),
c4e7957c 6310 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
6311 if (gpdel > 0)
6312 {
6313 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6314 p += 4;
6315 }
6316 macro_build (p, &icnt, &offset_expr,
ca4e0257 6317 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
c9914766
TS
6318 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6319 mips_gp_register);
252b5132
RH
6320 p += 4;
6321 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6322 p += 4;
6323 if (breg != 0)
6324 {
6325 macro_build (p, &icnt, (expressionS *) NULL,
ca4e0257 6326 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
6327 "d,v,t", AT, breg, AT);
6328 p += 4;
6329 }
beae10d5 6330 /* Itbl support may require additional care here. */
252b5132
RH
6331 macro_build (p, &icnt, &expr1, s, fmt,
6332 coproc ? treg + 1 : treg,
6333 (int) BFD_RELOC_LO16, AT);
6334 p += 4;
6335 expr1.X_add_number += 4;
6336
6337 /* Set mips_optimize to 2 to avoid inserting an undesired
6338 nop. */
6339 hold_mips_optimize = mips_optimize;
6340 mips_optimize = 2;
beae10d5 6341 /* Itbl support may require additional care here. */
252b5132
RH
6342 macro_build (p, &icnt, &expr1, s, fmt,
6343 coproc ? treg : treg + 1,
6344 (int) BFD_RELOC_LO16, AT);
6345 mips_optimize = hold_mips_optimize;
6346 }
6347 else if (mips_pic == EMBEDDED_PIC)
6348 {
6349 /* If there is no base register, we use
cdf6fd85
TS
6350 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6351 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
6352 If we have a base register, we use
6353 addu $at,$breg,$gp
cdf6fd85
TS
6354 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6355 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
6356 */
6357 if (breg == 0)
6358 {
c9914766 6359 tempreg = mips_gp_register;
252b5132
RH
6360 used_at = 0;
6361 }
6362 else
6363 {
6364 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6365 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 6366 "d,v,t", AT, breg, mips_gp_register);
252b5132
RH
6367 tempreg = AT;
6368 used_at = 1;
6369 }
6370
beae10d5 6371 /* Itbl support may require additional care here. */
252b5132
RH
6372 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6373 coproc ? treg + 1 : treg,
cdf6fd85 6374 (int) BFD_RELOC_GPREL16, tempreg);
252b5132 6375 offset_expr.X_add_number += 4;
beae10d5 6376 /* Itbl support may require additional care here. */
252b5132
RH
6377 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6378 coproc ? treg : treg + 1,
cdf6fd85 6379 (int) BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6380 }
6381 else
6382 abort ();
6383
6384 if (! used_at)
6385 return;
6386
6387 break;
6388
6389 case M_LD_OB:
6390 s = "lw";
6391 goto sd_ob;
6392 case M_SD_OB:
6393 s = "sw";
6394 sd_ob:
ca4e0257 6395 assert (HAVE_32BIT_ADDRESSES);
252b5132
RH
6396 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
6397 (int) BFD_RELOC_LO16, breg);
6398 offset_expr.X_add_number += 4;
6399 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg + 1,
6400 (int) BFD_RELOC_LO16, breg);
6401 return;
6402
6403 /* New code added to support COPZ instructions.
6404 This code builds table entries out of the macros in mip_opcodes.
6405 R4000 uses interlocks to handle coproc delays.
6406 Other chips (like the R3000) require nops to be inserted for delays.
6407
f72c8c98 6408 FIXME: Currently, we require that the user handle delays.
252b5132
RH
6409 In order to fill delay slots for non-interlocked chips,
6410 we must have a way to specify delays based on the coprocessor.
6411 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
6412 What are the side-effects of the cop instruction?
6413 What cache support might we have and what are its effects?
6414 Both coprocessor & memory require delays. how long???
bdaaa2e1 6415 What registers are read/set/modified?
252b5132
RH
6416
6417 If an itbl is provided to interpret cop instructions,
bdaaa2e1 6418 this knowledge can be encoded in the itbl spec. */
252b5132
RH
6419
6420 case M_COP0:
6421 s = "c0";
6422 goto copz;
6423 case M_COP1:
6424 s = "c1";
6425 goto copz;
6426 case M_COP2:
6427 s = "c2";
6428 goto copz;
6429 case M_COP3:
6430 s = "c3";
6431 copz:
6432 /* For now we just do C (same as Cz). The parameter will be
6433 stored in insn_opcode by mips_ip. */
6434 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "C",
6435 ip->insn_opcode);
6436 return;
6437
ea1fb5dc
RS
6438 case M_MOVE:
6439 move_register (&icnt, dreg, sreg);
6440 return;
6441
252b5132
RH
6442#ifdef LOSING_COMPILER
6443 default:
6444 /* Try and see if this is a new itbl instruction.
6445 This code builds table entries out of the macros in mip_opcodes.
6446 FIXME: For now we just assemble the expression and pass it's
6447 value along as a 32-bit immediate.
bdaaa2e1 6448 We may want to have the assembler assemble this value,
252b5132
RH
6449 so that we gain the assembler's knowledge of delay slots,
6450 symbols, etc.
6451 Would it be more efficient to use mask (id) here? */
bdaaa2e1 6452 if (itbl_have_entries
252b5132 6453 && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
beae10d5 6454 {
252b5132
RH
6455 s = ip->insn_mo->name;
6456 s2 = "cop3";
6457 coproc = ITBL_DECODE_PNUM (immed_expr);;
6458 macro_build ((char *) NULL, &icnt, &immed_expr, s, "C");
6459 return;
beae10d5 6460 }
252b5132
RH
6461 macro2 (ip);
6462 return;
6463 }
6464 if (mips_opts.noat)
6465 as_warn (_("Macro used $at after \".set noat\""));
6466}
bdaaa2e1 6467
252b5132
RH
6468static void
6469macro2 (ip)
6470 struct mips_cl_insn *ip;
6471{
6472 register int treg, sreg, dreg, breg;
6473 int tempreg;
6474 int mask;
6475 int icnt = 0;
6476 int used_at;
6477 expressionS expr1;
6478 const char *s;
6479 const char *s2;
6480 const char *fmt;
6481 int likely = 0;
6482 int dbl = 0;
6483 int coproc = 0;
6484 int lr = 0;
6485 int imm = 0;
6486 int off;
6487 offsetT maxnum;
6488 bfd_reloc_code_real_type r;
6489 char *p;
bdaaa2e1 6490
252b5132
RH
6491 treg = (ip->insn_opcode >> 16) & 0x1f;
6492 dreg = (ip->insn_opcode >> 11) & 0x1f;
6493 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
6494 mask = ip->insn_mo->mask;
bdaaa2e1 6495
252b5132
RH
6496 expr1.X_op = O_constant;
6497 expr1.X_op_symbol = NULL;
6498 expr1.X_add_symbol = NULL;
6499 expr1.X_add_number = 1;
bdaaa2e1 6500
252b5132
RH
6501 switch (mask)
6502 {
6503#endif /* LOSING_COMPILER */
6504
6505 case M_DMUL:
6506 dbl = 1;
6507 case M_MUL:
2396cfb9
TS
6508 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6509 dbl ? "dmultu" : "multu", "s,t", sreg, treg);
6510 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6511 dreg);
252b5132
RH
6512 return;
6513
6514 case M_DMUL_I:
6515 dbl = 1;
6516 case M_MUL_I:
6517 /* The MIPS assembler some times generates shifts and adds. I'm
6518 not trying to be that fancy. GCC should do this for us
6519 anyway. */
6520 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9 6521 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 6522 dbl ? "dmult" : "mult", "s,t", sreg, AT);
2396cfb9
TS
6523 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6524 dreg);
252b5132
RH
6525 break;
6526
6527 case M_DMULO_I:
6528 dbl = 1;
6529 case M_MULO_I:
6530 imm = 1;
6531 goto do_mulo;
6532
6533 case M_DMULO:
6534 dbl = 1;
6535 case M_MULO:
6536 do_mulo:
6537 mips_emit_delays (true);
6538 ++mips_opts.noreorder;
6539 mips_any_noreorder = 1;
6540 if (imm)
6541 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9 6542 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 6543 dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
2396cfb9
TS
6544 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6545 dreg);
6546 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
f9419b05 6547 dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, RA);
2396cfb9
TS
6548 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6549 AT);
252b5132 6550 if (mips_trap)
2396cfb9
TS
6551 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne", "s,t",
6552 dreg, AT);
252b5132
RH
6553 else
6554 {
6555 expr1.X_add_number = 8;
2396cfb9
TS
6556 macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", dreg,
6557 AT);
6558 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6559 0);
6560 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6561 "c", 6);
252b5132
RH
6562 }
6563 --mips_opts.noreorder;
2396cfb9 6564 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d", dreg);
252b5132
RH
6565 break;
6566
6567 case M_DMULOU_I:
6568 dbl = 1;
6569 case M_MULOU_I:
6570 imm = 1;
6571 goto do_mulou;
6572
6573 case M_DMULOU:
6574 dbl = 1;
6575 case M_MULOU:
6576 do_mulou:
6577 mips_emit_delays (true);
6578 ++mips_opts.noreorder;
6579 mips_any_noreorder = 1;
6580 if (imm)
6581 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9 6582 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
252b5132
RH
6583 dbl ? "dmultu" : "multu",
6584 "s,t", sreg, imm ? AT : treg);
2396cfb9
TS
6585 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6586 AT);
6587 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6588 dreg);
252b5132 6589 if (mips_trap)
2396cfb9
TS
6590 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne", "s,t",
6591 AT, 0);
252b5132
RH
6592 else
6593 {
6594 expr1.X_add_number = 8;
6595 macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", AT, 0);
2396cfb9
TS
6596 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6597 0);
6598 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6599 "c", 6);
252b5132
RH
6600 }
6601 --mips_opts.noreorder;
6602 break;
6603
771c7ce4
TS
6604 case M_DROL:
6605 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
6606 "d,v,t", AT, 0, treg);
6607 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
6608 "d,t,s", AT, sreg, AT);
6609 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
6610 "d,t,s", dreg, sreg, treg);
6611 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6612 "d,v,t", dreg, dreg, AT);
6613 break;
6614
252b5132 6615 case M_ROL:
2396cfb9
TS
6616 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
6617 "d,v,t", AT, 0, treg);
6618 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
6619 "d,t,s", AT, sreg, AT);
6620 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
6621 "d,t,s", dreg, sreg, treg);
6622 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6623 "d,v,t", dreg, dreg, AT);
252b5132
RH
6624 break;
6625
771c7ce4
TS
6626 case M_DROL_I:
6627 {
6628 unsigned int rot;
6629 char *l, *r;
6630
6631 if (imm_expr.X_op != O_constant)
6632 as_bad (_("rotate count too large"));
6633 rot = imm_expr.X_add_number & 0x3f;
6634 if (! rot)
6635 break;
6636 l = (rot < 0x20) ? "dsll" : "dsll32";
6637 r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
6638 rot &= 0x1f;
6639 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
6640 "d,w,<", AT, sreg, rot);
6641 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
6642 "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6643 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6644 "d,v,t", dreg, dreg, AT);
6645 }
6646 break;
6647
252b5132 6648 case M_ROL_I:
771c7ce4
TS
6649 {
6650 unsigned int rot;
6651
6652 if (imm_expr.X_op != O_constant)
6653 as_bad (_("rotate count too large"));
6654 rot = imm_expr.X_add_number & 0x1f;
6655 if (! rot)
6656 break;
6657 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
6658 "d,w,<", AT, sreg, rot);
6659 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
6660 "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6661 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6662 "d,v,t", dreg, dreg, AT);
6663 }
6664 break;
6665
6666 case M_DROR:
6667 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
6668 "d,v,t", AT, 0, treg);
6669 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
6670 "d,t,s", AT, sreg, AT);
6671 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
6672 "d,t,s", dreg, sreg, treg);
6673 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6674 "d,v,t", dreg, dreg, AT);
252b5132
RH
6675 break;
6676
6677 case M_ROR:
2396cfb9
TS
6678 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
6679 "d,v,t", AT, 0, treg);
6680 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
6681 "d,t,s", AT, sreg, AT);
6682 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
6683 "d,t,s", dreg, sreg, treg);
6684 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6685 "d,v,t", dreg, dreg, AT);
252b5132
RH
6686 break;
6687
771c7ce4
TS
6688 case M_DROR_I:
6689 {
6690 unsigned int rot;
6691 char *l, *r;
6692
6693 if (imm_expr.X_op != O_constant)
6694 as_bad (_("rotate count too large"));
6695 rot = imm_expr.X_add_number & 0x3f;
6696 if (! rot)
6697 break;
6698 r = (rot < 0x20) ? "dsrl" : "dsrl32";
6699 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
6700 rot &= 0x1f;
6701 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
6702 "d,w,<", AT, sreg, rot);
6703 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
6704 "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6705 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6706 "d,v,t", dreg, dreg, AT);
6707 }
6708 break;
6709
252b5132 6710 case M_ROR_I:
771c7ce4
TS
6711 {
6712 unsigned int rot;
6713
6714 if (imm_expr.X_op != O_constant)
6715 as_bad (_("rotate count too large"));
6716 rot = imm_expr.X_add_number & 0x1f;
6717 if (! rot)
6718 break;
6719 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
6720 "d,w,<", AT, sreg, rot);
6721 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
6722 "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6723 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6724 "d,v,t", dreg, dreg, AT);
6725 }
252b5132
RH
6726 break;
6727
6728 case M_S_DOB:
ec68c924 6729 if (mips_arch == CPU_R4650)
252b5132
RH
6730 {
6731 as_bad (_("opcode not supported on this processor"));
6732 return;
6733 }
e7af610e 6734 assert (mips_opts.isa == ISA_MIPS1);
252b5132
RH
6735 /* Even on a big endian machine $fn comes before $fn+1. We have
6736 to adjust when storing to memory. */
6737 macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
6738 target_big_endian ? treg + 1 : treg,
6739 (int) BFD_RELOC_LO16, breg);
6740 offset_expr.X_add_number += 4;
6741 macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
6742 target_big_endian ? treg : treg + 1,
6743 (int) BFD_RELOC_LO16, breg);
6744 return;
6745
6746 case M_SEQ:
6747 if (sreg == 0)
6748 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6749 treg, (int) BFD_RELOC_LO16);
6750 else if (treg == 0)
6751 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6752 sreg, (int) BFD_RELOC_LO16);
6753 else
6754 {
2396cfb9
TS
6755 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6756 "d,v,t", dreg, sreg, treg);
252b5132
RH
6757 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6758 dreg, (int) BFD_RELOC_LO16);
6759 }
6760 return;
6761
6762 case M_SEQ_I:
6763 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6764 {
6765 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6766 sreg, (int) BFD_RELOC_LO16);
6767 return;
6768 }
6769 if (sreg == 0)
6770 {
6771 as_warn (_("Instruction %s: result is always false"),
6772 ip->insn_mo->name);
ea1fb5dc 6773 move_register (&icnt, dreg, 0);
252b5132
RH
6774 return;
6775 }
6776 if (imm_expr.X_op == O_constant
6777 && imm_expr.X_add_number >= 0
6778 && imm_expr.X_add_number < 0x10000)
6779 {
6780 macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg,
6781 sreg, (int) BFD_RELOC_LO16);
6782 used_at = 0;
6783 }
6784 else if (imm_expr.X_op == O_constant
6785 && imm_expr.X_add_number > -0x8000
6786 && imm_expr.X_add_number < 0)
6787 {
6788 imm_expr.X_add_number = -imm_expr.X_add_number;
6789 macro_build ((char *) NULL, &icnt, &imm_expr,
ca4e0257 6790 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
252b5132
RH
6791 "t,r,j", dreg, sreg,
6792 (int) BFD_RELOC_LO16);
6793 used_at = 0;
6794 }
6795 else
6796 {
4d34fb5f 6797 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
6798 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6799 "d,v,t", dreg, sreg, AT);
252b5132
RH
6800 used_at = 1;
6801 }
6802 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
6803 (int) BFD_RELOC_LO16);
6804 if (used_at)
6805 break;
6806 return;
6807
6808 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
6809 s = "slt";
6810 goto sge;
6811 case M_SGEU:
6812 s = "sltu";
6813 sge:
2396cfb9
TS
6814 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6815 dreg, sreg, treg);
252b5132
RH
6816 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6817 (int) BFD_RELOC_LO16);
6818 return;
6819
6820 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
6821 case M_SGEU_I:
6822 if (imm_expr.X_op == O_constant
6823 && imm_expr.X_add_number >= -0x8000
6824 && imm_expr.X_add_number < 0x8000)
6825 {
6826 macro_build ((char *) NULL, &icnt, &imm_expr,
6827 mask == M_SGE_I ? "slti" : "sltiu",
6828 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6829 used_at = 0;
6830 }
6831 else
6832 {
4d34fb5f 6833 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
6834 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6835 mask == M_SGE_I ? "slt" : "sltu", "d,v,t", dreg, sreg,
6836 AT);
252b5132
RH
6837 used_at = 1;
6838 }
6839 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6840 (int) BFD_RELOC_LO16);
6841 if (used_at)
6842 break;
6843 return;
6844
6845 case M_SGT: /* sreg > treg <==> treg < sreg */
6846 s = "slt";
6847 goto sgt;
6848 case M_SGTU:
6849 s = "sltu";
6850 sgt:
2396cfb9
TS
6851 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6852 dreg, treg, sreg);
252b5132
RH
6853 return;
6854
6855 case M_SGT_I: /* sreg > I <==> I < sreg */
6856 s = "slt";
6857 goto sgti;
6858 case M_SGTU_I:
6859 s = "sltu";
6860 sgti:
4d34fb5f 6861 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
6862 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6863 dreg, AT, sreg);
252b5132
RH
6864 break;
6865
2396cfb9 6866 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
252b5132
RH
6867 s = "slt";
6868 goto sle;
6869 case M_SLEU:
6870 s = "sltu";
6871 sle:
2396cfb9
TS
6872 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6873 dreg, treg, sreg);
252b5132
RH
6874 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6875 (int) BFD_RELOC_LO16);
6876 return;
6877
2396cfb9 6878 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
252b5132
RH
6879 s = "slt";
6880 goto slei;
6881 case M_SLEU_I:
6882 s = "sltu";
6883 slei:
4d34fb5f 6884 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
6885 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6886 dreg, AT, sreg);
252b5132
RH
6887 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6888 (int) BFD_RELOC_LO16);
6889 break;
6890
6891 case M_SLT_I:
6892 if (imm_expr.X_op == O_constant
6893 && imm_expr.X_add_number >= -0x8000
6894 && imm_expr.X_add_number < 0x8000)
6895 {
6896 macro_build ((char *) NULL, &icnt, &imm_expr, "slti", "t,r,j",
6897 dreg, sreg, (int) BFD_RELOC_LO16);
6898 return;
6899 }
4d34fb5f 6900 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
6901 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
6902 dreg, sreg, AT);
252b5132
RH
6903 break;
6904
6905 case M_SLTU_I:
6906 if (imm_expr.X_op == O_constant
6907 && imm_expr.X_add_number >= -0x8000
6908 && imm_expr.X_add_number < 0x8000)
6909 {
6910 macro_build ((char *) NULL, &icnt, &imm_expr, "sltiu", "t,r,j",
6911 dreg, sreg, (int) BFD_RELOC_LO16);
6912 return;
6913 }
4d34fb5f 6914 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
6915 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6916 "d,v,t", dreg, sreg, AT);
252b5132
RH
6917 break;
6918
6919 case M_SNE:
6920 if (sreg == 0)
2396cfb9
TS
6921 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6922 "d,v,t", dreg, 0, treg);
252b5132 6923 else if (treg == 0)
2396cfb9
TS
6924 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6925 "d,v,t", dreg, 0, sreg);
252b5132
RH
6926 else
6927 {
2396cfb9
TS
6928 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6929 "d,v,t", dreg, sreg, treg);
6930 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6931 "d,v,t", dreg, 0, dreg);
252b5132
RH
6932 }
6933 return;
6934
6935 case M_SNE_I:
6936 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6937 {
2396cfb9
TS
6938 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6939 "d,v,t", dreg, 0, sreg);
252b5132
RH
6940 return;
6941 }
6942 if (sreg == 0)
6943 {
6944 as_warn (_("Instruction %s: result is always true"),
6945 ip->insn_mo->name);
6946 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 6947 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
252b5132
RH
6948 "t,r,j", dreg, 0, (int) BFD_RELOC_LO16);
6949 return;
6950 }
6951 if (imm_expr.X_op == O_constant
6952 && imm_expr.X_add_number >= 0
6953 && imm_expr.X_add_number < 0x10000)
6954 {
6955 macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i",
6956 dreg, sreg, (int) BFD_RELOC_LO16);
6957 used_at = 0;
6958 }
6959 else if (imm_expr.X_op == O_constant
6960 && imm_expr.X_add_number > -0x8000
6961 && imm_expr.X_add_number < 0)
6962 {
6963 imm_expr.X_add_number = -imm_expr.X_add_number;
6964 macro_build ((char *) NULL, &icnt, &imm_expr,
ca4e0257 6965 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
252b5132
RH
6966 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6967 used_at = 0;
6968 }
6969 else
6970 {
4d34fb5f 6971 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
6972 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6973 "d,v,t", dreg, sreg, AT);
252b5132
RH
6974 used_at = 1;
6975 }
2396cfb9
TS
6976 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6977 "d,v,t", dreg, 0, dreg);
252b5132
RH
6978 if (used_at)
6979 break;
6980 return;
6981
6982 case M_DSUB_I:
6983 dbl = 1;
6984 case M_SUB_I:
6985 if (imm_expr.X_op == O_constant
6986 && imm_expr.X_add_number > -0x8000
6987 && imm_expr.X_add_number <= 0x8000)
6988 {
6989 imm_expr.X_add_number = -imm_expr.X_add_number;
6990 macro_build ((char *) NULL, &icnt, &imm_expr,
6991 dbl ? "daddi" : "addi",
6992 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6993 return;
6994 }
6995 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9 6996 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 6997 dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
252b5132
RH
6998 break;
6999
7000 case M_DSUBU_I:
7001 dbl = 1;
7002 case M_SUBU_I:
7003 if (imm_expr.X_op == O_constant
7004 && imm_expr.X_add_number > -0x8000
7005 && imm_expr.X_add_number <= 0x8000)
7006 {
7007 imm_expr.X_add_number = -imm_expr.X_add_number;
7008 macro_build ((char *) NULL, &icnt, &imm_expr,
7009 dbl ? "daddiu" : "addiu",
7010 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7011 return;
7012 }
7013 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9 7014 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 7015 dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
252b5132
RH
7016 break;
7017
7018 case M_TEQ_I:
7019 s = "teq";
7020 goto trap;
7021 case M_TGE_I:
7022 s = "tge";
7023 goto trap;
7024 case M_TGEU_I:
7025 s = "tgeu";
7026 goto trap;
7027 case M_TLT_I:
7028 s = "tlt";
7029 goto trap;
7030 case M_TLTU_I:
7031 s = "tltu";
7032 goto trap;
7033 case M_TNE_I:
7034 s = "tne";
7035 trap:
4d34fb5f 7036 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
7037 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "s,t", sreg,
7038 AT);
252b5132
RH
7039 break;
7040
252b5132 7041 case M_TRUNCWS:
43841e91 7042 case M_TRUNCWD:
e7af610e 7043 assert (mips_opts.isa == ISA_MIPS1);
252b5132
RH
7044 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
7045 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
7046
7047 /*
7048 * Is the double cfc1 instruction a bug in the mips assembler;
7049 * or is there a reason for it?
7050 */
7051 mips_emit_delays (true);
7052 ++mips_opts.noreorder;
7053 mips_any_noreorder = 1;
2396cfb9 7054 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
f9419b05 7055 treg, RA);
2396cfb9 7056 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
f9419b05 7057 treg, RA);
2396cfb9 7058 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
252b5132
RH
7059 expr1.X_add_number = 3;
7060 macro_build ((char *) NULL, &icnt, &expr1, "ori", "t,r,i", AT, treg,
7061 (int) BFD_RELOC_LO16);
7062 expr1.X_add_number = 2;
7063 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", AT, AT,
7064 (int) BFD_RELOC_LO16);
2396cfb9 7065 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
f9419b05 7066 AT, RA);
2396cfb9
TS
7067 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7068 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
252b5132 7069 mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S", dreg, sreg);
2396cfb9 7070 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
f9419b05 7071 treg, RA);
2396cfb9 7072 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
252b5132
RH
7073 --mips_opts.noreorder;
7074 break;
7075
7076 case M_ULH:
7077 s = "lb";
7078 goto ulh;
7079 case M_ULHU:
7080 s = "lbu";
7081 ulh:
7082 if (offset_expr.X_add_number >= 0x7fff)
7083 as_bad (_("operand overflow"));
7084 /* avoid load delay */
7085 if (! target_big_endian)
f9419b05 7086 ++offset_expr.X_add_number;
252b5132
RH
7087 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7088 (int) BFD_RELOC_LO16, breg);
7089 if (! target_big_endian)
f9419b05 7090 --offset_expr.X_add_number;
252b5132 7091 else
f9419b05 7092 ++offset_expr.X_add_number;
252b5132
RH
7093 macro_build ((char *) NULL, &icnt, &offset_expr, "lbu", "t,o(b)", AT,
7094 (int) BFD_RELOC_LO16, breg);
2396cfb9
TS
7095 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7096 treg, treg, 8);
7097 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7098 treg, treg, AT);
252b5132
RH
7099 break;
7100
7101 case M_ULD:
7102 s = "ldl";
7103 s2 = "ldr";
7104 off = 7;
7105 goto ulw;
7106 case M_ULW:
7107 s = "lwl";
7108 s2 = "lwr";
7109 off = 3;
7110 ulw:
7111 if (offset_expr.X_add_number >= 0x8000 - off)
7112 as_bad (_("operand overflow"));
7113 if (! target_big_endian)
7114 offset_expr.X_add_number += off;
7115 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7116 (int) BFD_RELOC_LO16, breg);
7117 if (! target_big_endian)
7118 offset_expr.X_add_number -= off;
7119 else
7120 offset_expr.X_add_number += off;
7121 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7122 (int) BFD_RELOC_LO16, breg);
7123 return;
7124
7125 case M_ULD_A:
7126 s = "ldl";
7127 s2 = "ldr";
7128 off = 7;
7129 goto ulwa;
7130 case M_ULW_A:
7131 s = "lwl";
7132 s2 = "lwr";
7133 off = 3;
7134 ulwa:
d6bc6245 7135 used_at = 1;
c9914766 7136 load_address (&icnt, AT, &offset_expr, &used_at);
252b5132
RH
7137 if (breg != 0)
7138 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 7139 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
7140 "d,v,t", AT, AT, breg);
7141 if (! target_big_endian)
7142 expr1.X_add_number = off;
7143 else
7144 expr1.X_add_number = 0;
7145 macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7146 (int) BFD_RELOC_LO16, AT);
7147 if (! target_big_endian)
7148 expr1.X_add_number = 0;
7149 else
7150 expr1.X_add_number = off;
7151 macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7152 (int) BFD_RELOC_LO16, AT);
7153 break;
7154
7155 case M_ULH_A:
7156 case M_ULHU_A:
d6bc6245 7157 used_at = 1;
c9914766 7158 load_address (&icnt, AT, &offset_expr, &used_at);
252b5132
RH
7159 if (breg != 0)
7160 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 7161 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
7162 "d,v,t", AT, AT, breg);
7163 if (target_big_endian)
7164 expr1.X_add_number = 0;
7165 macro_build ((char *) NULL, &icnt, &expr1,
7166 mask == M_ULH_A ? "lb" : "lbu", "t,o(b)", treg,
7167 (int) BFD_RELOC_LO16, AT);
7168 if (target_big_endian)
7169 expr1.X_add_number = 1;
7170 else
7171 expr1.X_add_number = 0;
7172 macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7173 (int) BFD_RELOC_LO16, AT);
2396cfb9
TS
7174 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7175 treg, treg, 8);
7176 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7177 treg, treg, AT);
252b5132
RH
7178 break;
7179
7180 case M_USH:
7181 if (offset_expr.X_add_number >= 0x7fff)
7182 as_bad (_("operand overflow"));
7183 if (target_big_endian)
f9419b05 7184 ++offset_expr.X_add_number;
252b5132
RH
7185 macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", treg,
7186 (int) BFD_RELOC_LO16, breg);
2396cfb9
TS
7187 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7188 AT, treg, 8);
252b5132 7189 if (target_big_endian)
f9419b05 7190 --offset_expr.X_add_number;
252b5132 7191 else
f9419b05 7192 ++offset_expr.X_add_number;
252b5132
RH
7193 macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", AT,
7194 (int) BFD_RELOC_LO16, breg);
7195 break;
7196
7197 case M_USD:
7198 s = "sdl";
7199 s2 = "sdr";
7200 off = 7;
7201 goto usw;
7202 case M_USW:
7203 s = "swl";
7204 s2 = "swr";
7205 off = 3;
7206 usw:
7207 if (offset_expr.X_add_number >= 0x8000 - off)
7208 as_bad (_("operand overflow"));
7209 if (! target_big_endian)
7210 offset_expr.X_add_number += off;
7211 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7212 (int) BFD_RELOC_LO16, breg);
7213 if (! target_big_endian)
7214 offset_expr.X_add_number -= off;
7215 else
7216 offset_expr.X_add_number += off;
7217 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7218 (int) BFD_RELOC_LO16, breg);
7219 return;
7220
7221 case M_USD_A:
7222 s = "sdl";
7223 s2 = "sdr";
7224 off = 7;
7225 goto uswa;
7226 case M_USW_A:
7227 s = "swl";
7228 s2 = "swr";
7229 off = 3;
7230 uswa:
d6bc6245 7231 used_at = 1;
c9914766 7232 load_address (&icnt, AT, &offset_expr, &used_at);
252b5132
RH
7233 if (breg != 0)
7234 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 7235 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
7236 "d,v,t", AT, AT, breg);
7237 if (! target_big_endian)
7238 expr1.X_add_number = off;
7239 else
7240 expr1.X_add_number = 0;
7241 macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7242 (int) BFD_RELOC_LO16, AT);
7243 if (! target_big_endian)
7244 expr1.X_add_number = 0;
7245 else
7246 expr1.X_add_number = off;
7247 macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7248 (int) BFD_RELOC_LO16, AT);
7249 break;
7250
7251 case M_USH_A:
d6bc6245 7252 used_at = 1;
c9914766 7253 load_address (&icnt, AT, &offset_expr, &used_at);
252b5132
RH
7254 if (breg != 0)
7255 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 7256 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
7257 "d,v,t", AT, AT, breg);
7258 if (! target_big_endian)
7259 expr1.X_add_number = 0;
7260 macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7261 (int) BFD_RELOC_LO16, AT);
2396cfb9
TS
7262 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7263 treg, treg, 8);
252b5132
RH
7264 if (! target_big_endian)
7265 expr1.X_add_number = 1;
7266 else
7267 expr1.X_add_number = 0;
7268 macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7269 (int) BFD_RELOC_LO16, AT);
7270 if (! target_big_endian)
7271 expr1.X_add_number = 0;
7272 else
7273 expr1.X_add_number = 1;
7274 macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7275 (int) BFD_RELOC_LO16, AT);
2396cfb9
TS
7276 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7277 treg, treg, 8);
7278 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7279 treg, treg, AT);
252b5132
RH
7280 break;
7281
7282 default:
7283 /* FIXME: Check if this is one of the itbl macros, since they
bdaaa2e1 7284 are added dynamically. */
252b5132
RH
7285 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7286 break;
7287 }
7288 if (mips_opts.noat)
7289 as_warn (_("Macro used $at after \".set noat\""));
7290}
7291
7292/* Implement macros in mips16 mode. */
7293
7294static void
7295mips16_macro (ip)
7296 struct mips_cl_insn *ip;
7297{
7298 int mask;
7299 int xreg, yreg, zreg, tmp;
7300 int icnt;
7301 expressionS expr1;
7302 int dbl;
7303 const char *s, *s2, *s3;
7304
7305 mask = ip->insn_mo->mask;
7306
7307 xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
7308 yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
7309 zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
7310
7311 icnt = 0;
7312
7313 expr1.X_op = O_constant;
7314 expr1.X_op_symbol = NULL;
7315 expr1.X_add_symbol = NULL;
7316 expr1.X_add_number = 1;
7317
7318 dbl = 0;
7319
7320 switch (mask)
7321 {
7322 default:
7323 internalError ();
7324
7325 case M_DDIV_3:
7326 dbl = 1;
7327 case M_DIV_3:
7328 s = "mflo";
7329 goto do_div3;
7330 case M_DREM_3:
7331 dbl = 1;
7332 case M_REM_3:
7333 s = "mfhi";
7334 do_div3:
7335 mips_emit_delays (true);
7336 ++mips_opts.noreorder;
7337 mips_any_noreorder = 1;
2396cfb9 7338 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
252b5132
RH
7339 dbl ? "ddiv" : "div",
7340 "0,x,y", xreg, yreg);
7341 expr1.X_add_number = 2;
7342 macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
2396cfb9
TS
7343 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break", "6",
7344 7);
bdaaa2e1 7345
252b5132
RH
7346 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7347 since that causes an overflow. We should do that as well,
7348 but I don't see how to do the comparisons without a temporary
7349 register. */
7350 --mips_opts.noreorder;
2396cfb9 7351 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x", zreg);
252b5132
RH
7352 break;
7353
7354 case M_DIVU_3:
7355 s = "divu";
7356 s2 = "mflo";
7357 goto do_divu3;
7358 case M_REMU_3:
7359 s = "divu";
7360 s2 = "mfhi";
7361 goto do_divu3;
7362 case M_DDIVU_3:
7363 s = "ddivu";
7364 s2 = "mflo";
7365 goto do_divu3;
7366 case M_DREMU_3:
7367 s = "ddivu";
7368 s2 = "mfhi";
7369 do_divu3:
7370 mips_emit_delays (true);
7371 ++mips_opts.noreorder;
7372 mips_any_noreorder = 1;
2396cfb9
TS
7373 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "0,x,y",
7374 xreg, yreg);
252b5132
RH
7375 expr1.X_add_number = 2;
7376 macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
98d3f06f
KH
7377 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
7378 "6", 7);
252b5132 7379 --mips_opts.noreorder;
2396cfb9 7380 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "x", zreg);
252b5132
RH
7381 break;
7382
7383 case M_DMUL:
7384 dbl = 1;
7385 case M_MUL:
2396cfb9 7386 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 7387 dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
2396cfb9
TS
7388 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "x",
7389 zreg);
252b5132
RH
7390 return;
7391
7392 case M_DSUBU_I:
7393 dbl = 1;
7394 goto do_subu;
7395 case M_SUBU_I:
7396 do_subu:
7397 if (imm_expr.X_op != O_constant)
7398 as_bad (_("Unsupported large constant"));
7399 imm_expr.X_add_number = -imm_expr.X_add_number;
7400 macro_build ((char *) NULL, &icnt, &imm_expr,
9a41af64 7401 dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
252b5132
RH
7402 break;
7403
7404 case M_SUBU_I_2:
7405 if (imm_expr.X_op != O_constant)
7406 as_bad (_("Unsupported large constant"));
7407 imm_expr.X_add_number = -imm_expr.X_add_number;
7408 macro_build ((char *) NULL, &icnt, &imm_expr, "addiu",
7409 "x,k", xreg);
7410 break;
7411
7412 case M_DSUBU_I_2:
7413 if (imm_expr.X_op != O_constant)
7414 as_bad (_("Unsupported large constant"));
7415 imm_expr.X_add_number = -imm_expr.X_add_number;
7416 macro_build ((char *) NULL, &icnt, &imm_expr, "daddiu",
7417 "y,j", yreg);
7418 break;
7419
7420 case M_BEQ:
7421 s = "cmp";
7422 s2 = "bteqz";
7423 goto do_branch;
7424 case M_BNE:
7425 s = "cmp";
7426 s2 = "btnez";
7427 goto do_branch;
7428 case M_BLT:
7429 s = "slt";
7430 s2 = "btnez";
7431 goto do_branch;
7432 case M_BLTU:
7433 s = "sltu";
7434 s2 = "btnez";
7435 goto do_branch;
7436 case M_BLE:
7437 s = "slt";
7438 s2 = "bteqz";
7439 goto do_reverse_branch;
7440 case M_BLEU:
7441 s = "sltu";
7442 s2 = "bteqz";
7443 goto do_reverse_branch;
7444 case M_BGE:
7445 s = "slt";
7446 s2 = "bteqz";
7447 goto do_branch;
7448 case M_BGEU:
7449 s = "sltu";
7450 s2 = "bteqz";
7451 goto do_branch;
7452 case M_BGT:
7453 s = "slt";
7454 s2 = "btnez";
7455 goto do_reverse_branch;
7456 case M_BGTU:
7457 s = "sltu";
7458 s2 = "btnez";
7459
7460 do_reverse_branch:
7461 tmp = xreg;
7462 xreg = yreg;
7463 yreg = tmp;
7464
7465 do_branch:
7466 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x,y",
7467 xreg, yreg);
7468 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7469 break;
7470
7471 case M_BEQ_I:
7472 s = "cmpi";
7473 s2 = "bteqz";
7474 s3 = "x,U";
7475 goto do_branch_i;
7476 case M_BNE_I:
7477 s = "cmpi";
7478 s2 = "btnez";
7479 s3 = "x,U";
7480 goto do_branch_i;
7481 case M_BLT_I:
7482 s = "slti";
7483 s2 = "btnez";
7484 s3 = "x,8";
7485 goto do_branch_i;
7486 case M_BLTU_I:
7487 s = "sltiu";
7488 s2 = "btnez";
7489 s3 = "x,8";
7490 goto do_branch_i;
7491 case M_BLE_I:
7492 s = "slti";
7493 s2 = "btnez";
7494 s3 = "x,8";
7495 goto do_addone_branch_i;
7496 case M_BLEU_I:
7497 s = "sltiu";
7498 s2 = "btnez";
7499 s3 = "x,8";
7500 goto do_addone_branch_i;
7501 case M_BGE_I:
7502 s = "slti";
7503 s2 = "bteqz";
7504 s3 = "x,8";
7505 goto do_branch_i;
7506 case M_BGEU_I:
7507 s = "sltiu";
7508 s2 = "bteqz";
7509 s3 = "x,8";
7510 goto do_branch_i;
7511 case M_BGT_I:
7512 s = "slti";
7513 s2 = "bteqz";
7514 s3 = "x,8";
7515 goto do_addone_branch_i;
7516 case M_BGTU_I:
7517 s = "sltiu";
7518 s2 = "bteqz";
7519 s3 = "x,8";
7520
7521 do_addone_branch_i:
7522 if (imm_expr.X_op != O_constant)
7523 as_bad (_("Unsupported large constant"));
7524 ++imm_expr.X_add_number;
7525
7526 do_branch_i:
7527 macro_build ((char *) NULL, &icnt, &imm_expr, s, s3, xreg);
7528 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7529 break;
7530
7531 case M_ABS:
7532 expr1.X_add_number = 0;
98d3f06f 7533 macro_build ((char *) NULL, &icnt, &expr1, "slti", "x,8", yreg);
252b5132 7534 if (xreg != yreg)
ea1fb5dc 7535 move_register (&icnt, xreg, yreg);
252b5132
RH
7536 expr1.X_add_number = 2;
7537 macro_build ((char *) NULL, &icnt, &expr1, "bteqz", "p");
7538 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7539 "neg", "x,w", xreg, xreg);
7540 }
7541}
7542
7543/* For consistency checking, verify that all bits are specified either
7544 by the match/mask part of the instruction definition, or by the
7545 operand list. */
7546static int
7547validate_mips_insn (opc)
7548 const struct mips_opcode *opc;
7549{
7550 const char *p = opc->args;
7551 char c;
7552 unsigned long used_bits = opc->mask;
7553
7554 if ((used_bits & opc->match) != opc->match)
7555 {
7556 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
7557 opc->name, opc->args);
7558 return 0;
7559 }
7560#define USE_BITS(mask,shift) (used_bits |= ((mask) << (shift)))
7561 while (*p)
7562 switch (c = *p++)
7563 {
7564 case ',': break;
7565 case '(': break;
7566 case ')': break;
7567 case '<': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7568 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7569 case 'A': break;
4372b673 7570 case 'B': USE_BITS (OP_MASK_CODE20, OP_SH_CODE20); break;
252b5132
RH
7571 case 'C': USE_BITS (OP_MASK_COPZ, OP_SH_COPZ); break;
7572 case 'D': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
7573 case 'E': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7574 case 'F': break;
7575 case 'G': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
156c2f8b 7576 case 'H': USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
252b5132 7577 case 'I': break;
e972090a 7578 case 'J': USE_BITS (OP_MASK_CODE19, OP_SH_CODE19); break;
252b5132
RH
7579 case 'L': break;
7580 case 'M': USE_BITS (OP_MASK_CCC, OP_SH_CCC); break;
7581 case 'N': USE_BITS (OP_MASK_BCC, OP_SH_BCC); break;
deec1734
CD
7582 case 'O': USE_BITS (OP_MASK_ALN, OP_SH_ALN); break;
7583 case 'Q': USE_BITS (OP_MASK_VSEL, OP_SH_VSEL);
7584 USE_BITS (OP_MASK_FT, OP_SH_FT); break;
252b5132
RH
7585 case 'R': USE_BITS (OP_MASK_FR, OP_SH_FR); break;
7586 case 'S': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7587 case 'T': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7588 case 'V': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7589 case 'W': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
deec1734
CD
7590 case 'X': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
7591 case 'Y': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7592 case 'Z': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
252b5132
RH
7593 case 'a': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break;
7594 case 'b': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7595 case 'c': USE_BITS (OP_MASK_CODE, OP_SH_CODE); break;
7596 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
7597 case 'f': break;
7598 case 'h': USE_BITS (OP_MASK_PREFX, OP_SH_PREFX); break;
7599 case 'i': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
7600 case 'j': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7601 case 'k': USE_BITS (OP_MASK_CACHE, OP_SH_CACHE); break;
7602 case 'l': break;
7603 case 'o': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7604 case 'p': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7605 case 'q': USE_BITS (OP_MASK_CODE2, OP_SH_CODE2); break;
7606 case 'r': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7607 case 's': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7608 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7609 case 'u': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
7610 case 'v': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7611 case 'w': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7612 case 'x': break;
7613 case 'z': break;
7614 case 'P': USE_BITS (OP_MASK_PERFREG, OP_SH_PERFREG); break;
4372b673
NC
7615 case 'U': USE_BITS (OP_MASK_RD, OP_SH_RD);
7616 USE_BITS (OP_MASK_RT, OP_SH_RT); break;
252b5132
RH
7617 default:
7618 as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
7619 c, opc->name, opc->args);
7620 return 0;
7621 }
7622#undef USE_BITS
7623 if (used_bits != 0xffffffff)
7624 {
7625 as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
7626 ~used_bits & 0xffffffff, opc->name, opc->args);
7627 return 0;
7628 }
7629 return 1;
7630}
7631
7632/* This routine assembles an instruction into its binary format. As a
7633 side effect, it sets one of the global variables imm_reloc or
7634 offset_reloc to the type of relocation to do if one of the operands
7635 is an address expression. */
7636
7637static void
7638mips_ip (str, ip)
7639 char *str;
7640 struct mips_cl_insn *ip;
7641{
7642 char *s;
7643 const char *args;
43841e91 7644 char c = 0;
252b5132
RH
7645 struct mips_opcode *insn;
7646 char *argsStart;
7647 unsigned int regno;
7648 unsigned int lastregno = 0;
7649 char *s_reset;
7650 char save_c = 0;
252b5132
RH
7651
7652 insn_error = NULL;
7653
7654 /* If the instruction contains a '.', we first try to match an instruction
7655 including the '.'. Then we try again without the '.'. */
7656 insn = NULL;
3882b010 7657 for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
252b5132
RH
7658 continue;
7659
7660 /* If we stopped on whitespace, then replace the whitespace with null for
7661 the call to hash_find. Save the character we replaced just in case we
7662 have to re-parse the instruction. */
3882b010 7663 if (ISSPACE (*s))
252b5132
RH
7664 {
7665 save_c = *s;
7666 *s++ = '\0';
7667 }
bdaaa2e1 7668
252b5132
RH
7669 insn = (struct mips_opcode *) hash_find (op_hash, str);
7670
7671 /* If we didn't find the instruction in the opcode table, try again, but
7672 this time with just the instruction up to, but not including the
7673 first '.'. */
7674 if (insn == NULL)
7675 {
bdaaa2e1 7676 /* Restore the character we overwrite above (if any). */
252b5132
RH
7677 if (save_c)
7678 *(--s) = save_c;
7679
7680 /* Scan up to the first '.' or whitespace. */
3882b010
L
7681 for (s = str;
7682 *s != '\0' && *s != '.' && !ISSPACE (*s);
7683 ++s)
252b5132
RH
7684 continue;
7685
7686 /* If we did not find a '.', then we can quit now. */
7687 if (*s != '.')
7688 {
7689 insn_error = "unrecognized opcode";
7690 return;
7691 }
7692
7693 /* Lookup the instruction in the hash table. */
7694 *s++ = '\0';
7695 if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
7696 {
7697 insn_error = "unrecognized opcode";
7698 return;
7699 }
252b5132
RH
7700 }
7701
7702 argsStart = s;
7703 for (;;)
7704 {
252b5132
RH
7705 boolean ok;
7706
7707 assert (strcmp (insn->name, str) == 0);
7708
1f25f5d3
CD
7709 if (OPCODE_IS_MEMBER (insn,
7710 (mips_opts.isa
deec1734 7711 | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
98d3f06f 7712 | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
1f25f5d3 7713 mips_arch))
252b5132 7714 ok = true;
bdaaa2e1 7715 else
252b5132 7716 ok = false;
bdaaa2e1 7717
252b5132
RH
7718 if (insn->pinfo != INSN_MACRO)
7719 {
ec68c924 7720 if (mips_arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
252b5132
RH
7721 ok = false;
7722 }
7723
7724 if (! ok)
7725 {
7726 if (insn + 1 < &mips_opcodes[NUMOPCODES]
7727 && strcmp (insn->name, insn[1].name) == 0)
7728 {
7729 ++insn;
7730 continue;
7731 }
252b5132 7732 else
beae10d5 7733 {
268f6bed
L
7734 if (!insn_error)
7735 {
7736 static char buf[100];
7737 sprintf (buf,
7738 _("opcode not supported on this processor: %s (%s)"),
ec68c924 7739 mips_cpu_to_str (mips_arch),
268f6bed 7740 mips_isa_to_str (mips_opts.isa));
bdaaa2e1 7741
268f6bed
L
7742 insn_error = buf;
7743 }
7744 if (save_c)
7745 *(--s) = save_c;
2bd7f1f3 7746 return;
252b5132 7747 }
252b5132
RH
7748 }
7749
7750 ip->insn_mo = insn;
7751 ip->insn_opcode = insn->match;
268f6bed 7752 insn_error = NULL;
252b5132
RH
7753 for (args = insn->args;; ++args)
7754 {
deec1734
CD
7755 int is_mdmx;
7756
ad8d3bb3 7757 s += strspn (s, " \t");
deec1734 7758 is_mdmx = 0;
252b5132
RH
7759 switch (*args)
7760 {
7761 case '\0': /* end of args */
7762 if (*s == '\0')
7763 return;
7764 break;
7765
7766 case ',':
7767 if (*s++ == *args)
7768 continue;
7769 s--;
7770 switch (*++args)
7771 {
7772 case 'r':
7773 case 'v':
38487616 7774 ip->insn_opcode |= lastregno << OP_SH_RS;
252b5132
RH
7775 continue;
7776
7777 case 'w':
38487616
TS
7778 ip->insn_opcode |= lastregno << OP_SH_RT;
7779 continue;
7780
252b5132 7781 case 'W':
38487616 7782 ip->insn_opcode |= lastregno << OP_SH_FT;
252b5132
RH
7783 continue;
7784
7785 case 'V':
38487616 7786 ip->insn_opcode |= lastregno << OP_SH_FS;
252b5132
RH
7787 continue;
7788 }
7789 break;
7790
7791 case '(':
7792 /* Handle optional base register.
7793 Either the base register is omitted or
bdaaa2e1 7794 we must have a left paren. */
252b5132
RH
7795 /* This is dependent on the next operand specifier
7796 is a base register specification. */
7797 assert (args[1] == 'b' || args[1] == '5'
7798 || args[1] == '-' || args[1] == '4');
7799 if (*s == '\0')
7800 return;
7801
7802 case ')': /* these must match exactly */
7803 if (*s++ == *args)
7804 continue;
7805 break;
7806
7807 case '<': /* must be at least one digit */
7808 /*
7809 * According to the manual, if the shift amount is greater
b6ff326e
KH
7810 * than 31 or less than 0, then the shift amount should be
7811 * mod 32. In reality the mips assembler issues an error.
252b5132
RH
7812 * We issue a warning and mask out all but the low 5 bits.
7813 */
7814 my_getExpression (&imm_expr, s);
7815 check_absolute_expr (ip, &imm_expr);
7816 if ((unsigned long) imm_expr.X_add_number > 31)
7817 {
793b27f4
TS
7818 as_warn (_("Improper shift amount (%lu)"),
7819 (unsigned long) imm_expr.X_add_number);
38487616 7820 imm_expr.X_add_number &= OP_MASK_SHAMT;
252b5132 7821 }
38487616 7822 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SHAMT;
252b5132
RH
7823 imm_expr.X_op = O_absent;
7824 s = expr_end;
7825 continue;
7826
7827 case '>': /* shift amount minus 32 */
7828 my_getExpression (&imm_expr, s);
7829 check_absolute_expr (ip, &imm_expr);
7830 if ((unsigned long) imm_expr.X_add_number < 32
7831 || (unsigned long) imm_expr.X_add_number > 63)
7832 break;
38487616 7833 ip->insn_opcode |= (imm_expr.X_add_number - 32) << OP_SH_SHAMT;
252b5132
RH
7834 imm_expr.X_op = O_absent;
7835 s = expr_end;
7836 continue;
7837
252b5132
RH
7838 case 'k': /* cache code */
7839 case 'h': /* prefx code */
7840 my_getExpression (&imm_expr, s);
7841 check_absolute_expr (ip, &imm_expr);
7842 if ((unsigned long) imm_expr.X_add_number > 31)
7843 {
7844 as_warn (_("Invalid value for `%s' (%lu)"),
7845 ip->insn_mo->name,
7846 (unsigned long) imm_expr.X_add_number);
7847 imm_expr.X_add_number &= 0x1f;
7848 }
7849 if (*args == 'k')
7850 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
7851 else
7852 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
7853 imm_expr.X_op = O_absent;
7854 s = expr_end;
7855 continue;
7856
7857 case 'c': /* break code */
7858 my_getExpression (&imm_expr, s);
7859 check_absolute_expr (ip, &imm_expr);
793b27f4 7860 if ((unsigned long) imm_expr.X_add_number > 1023)
252b5132 7861 {
793b27f4
TS
7862 as_warn (_("Illegal break code (%lu)"),
7863 (unsigned long) imm_expr.X_add_number);
38487616 7864 imm_expr.X_add_number &= OP_MASK_CODE;
252b5132 7865 }
38487616 7866 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE;
252b5132
RH
7867 imm_expr.X_op = O_absent;
7868 s = expr_end;
7869 continue;
7870
7871 case 'q': /* lower break code */
7872 my_getExpression (&imm_expr, s);
7873 check_absolute_expr (ip, &imm_expr);
793b27f4 7874 if ((unsigned long) imm_expr.X_add_number > 1023)
252b5132 7875 {
793b27f4
TS
7876 as_warn (_("Illegal lower break code (%lu)"),
7877 (unsigned long) imm_expr.X_add_number);
38487616 7878 imm_expr.X_add_number &= OP_MASK_CODE2;
252b5132 7879 }
38487616 7880 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE2;
252b5132
RH
7881 imm_expr.X_op = O_absent;
7882 s = expr_end;
7883 continue;
7884
4372b673 7885 case 'B': /* 20-bit syscall/break code. */
156c2f8b 7886 my_getExpression (&imm_expr, s);
156c2f8b 7887 check_absolute_expr (ip, &imm_expr);
793b27f4
TS
7888 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
7889 as_warn (_("Illegal 20-bit code (%lu)"),
7890 (unsigned long) imm_expr.X_add_number);
38487616 7891 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE20;
252b5132
RH
7892 imm_expr.X_op = O_absent;
7893 s = expr_end;
7894 continue;
7895
98d3f06f 7896 case 'C': /* Coprocessor code */
beae10d5 7897 my_getExpression (&imm_expr, s);
252b5132 7898 check_absolute_expr (ip, &imm_expr);
98d3f06f 7899 if ((unsigned long) imm_expr.X_add_number >= (1 << 25))
252b5132 7900 {
793b27f4
TS
7901 as_warn (_("Coproccesor code > 25 bits (%lu)"),
7902 (unsigned long) imm_expr.X_add_number);
98d3f06f 7903 imm_expr.X_add_number &= ((1 << 25) - 1);
252b5132 7904 }
beae10d5
KH
7905 ip->insn_opcode |= imm_expr.X_add_number;
7906 imm_expr.X_op = O_absent;
7907 s = expr_end;
7908 continue;
252b5132 7909
4372b673
NC
7910 case 'J': /* 19-bit wait code. */
7911 my_getExpression (&imm_expr, s);
7912 check_absolute_expr (ip, &imm_expr);
793b27f4
TS
7913 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
7914 as_warn (_("Illegal 19-bit code (%lu)"),
7915 (unsigned long) imm_expr.X_add_number);
38487616 7916 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE19;
4372b673
NC
7917 imm_expr.X_op = O_absent;
7918 s = expr_end;
7919 continue;
7920
252b5132 7921 case 'P': /* Performance register */
beae10d5 7922 my_getExpression (&imm_expr, s);
252b5132 7923 check_absolute_expr (ip, &imm_expr);
beae10d5 7924 if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
252b5132 7925 {
793b27f4
TS
7926 as_warn (_("Invalid performance register (%lu)"),
7927 (unsigned long) imm_expr.X_add_number);
38487616 7928 imm_expr.X_add_number &= OP_MASK_PERFREG;
252b5132 7929 }
38487616 7930 ip->insn_opcode |= (imm_expr.X_add_number << OP_SH_PERFREG);
beae10d5
KH
7931 imm_expr.X_op = O_absent;
7932 s = expr_end;
7933 continue;
252b5132
RH
7934
7935 case 'b': /* base register */
7936 case 'd': /* destination register */
7937 case 's': /* source register */
7938 case 't': /* target register */
7939 case 'r': /* both target and source */
7940 case 'v': /* both dest and source */
7941 case 'w': /* both dest and target */
7942 case 'E': /* coprocessor target register */
7943 case 'G': /* coprocessor destination register */
7944 case 'x': /* ignore register name */
7945 case 'z': /* must be zero register */
4372b673 7946 case 'U': /* destination register (clo/clz). */
252b5132
RH
7947 s_reset = s;
7948 if (s[0] == '$')
7949 {
7950
3882b010 7951 if (ISDIGIT (s[1]))
252b5132
RH
7952 {
7953 ++s;
7954 regno = 0;
7955 do
7956 {
7957 regno *= 10;
7958 regno += *s - '0';
7959 ++s;
7960 }
3882b010 7961 while (ISDIGIT (*s));
252b5132
RH
7962 if (regno > 31)
7963 as_bad (_("Invalid register number (%d)"), regno);
7964 }
7965 else if (*args == 'E' || *args == 'G')
7966 goto notreg;
7967 else
7968 {
76db943d
TS
7969 if (s[1] == 'r' && s[2] == 'a')
7970 {
7971 s += 3;
7972 regno = RA;
7973 }
7974 else if (s[1] == 'f' && s[2] == 'p')
252b5132
RH
7975 {
7976 s += 3;
7977 regno = FP;
7978 }
7979 else if (s[1] == 's' && s[2] == 'p')
7980 {
7981 s += 3;
7982 regno = SP;
7983 }
7984 else if (s[1] == 'g' && s[2] == 'p')
7985 {
7986 s += 3;
7987 regno = GP;
7988 }
7989 else if (s[1] == 'a' && s[2] == 't')
7990 {
7991 s += 3;
7992 regno = AT;
7993 }
7994 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
7995 {
7996 s += 4;
7997 regno = KT0;
7998 }
7999 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8000 {
8001 s += 4;
8002 regno = KT1;
8003 }
8004 else if (itbl_have_entries)
8005 {
8006 char *p, *n;
d7ba4a77 8007 unsigned long r;
252b5132 8008
d7ba4a77 8009 p = s + 1; /* advance past '$' */
252b5132
RH
8010 n = itbl_get_field (&p); /* n is name */
8011
d7ba4a77
ILT
8012 /* See if this is a register defined in an
8013 itbl entry. */
8014 if (itbl_get_reg_val (n, &r))
252b5132
RH
8015 {
8016 /* Get_field advances to the start of
8017 the next field, so we need to back
d7ba4a77 8018 rack to the end of the last field. */
bdaaa2e1 8019 if (p)
252b5132 8020 s = p - 1;
bdaaa2e1 8021 else
d7ba4a77 8022 s = strchr (s, '\0');
252b5132
RH
8023 regno = r;
8024 }
8025 else
8026 goto notreg;
beae10d5 8027 }
252b5132
RH
8028 else
8029 goto notreg;
8030 }
8031 if (regno == AT
8032 && ! mips_opts.noat
8033 && *args != 'E'
8034 && *args != 'G')
8035 as_warn (_("Used $at without \".set noat\""));
8036 c = *args;
8037 if (*s == ' ')
f9419b05 8038 ++s;
252b5132
RH
8039 if (args[1] != *s)
8040 {
8041 if (c == 'r' || c == 'v' || c == 'w')
8042 {
8043 regno = lastregno;
8044 s = s_reset;
f9419b05 8045 ++args;
252b5132
RH
8046 }
8047 }
8048 /* 'z' only matches $0. */
8049 if (c == 'z' && regno != 0)
8050 break;
8051
bdaaa2e1
KH
8052 /* Now that we have assembled one operand, we use the args string
8053 * to figure out where it goes in the instruction. */
252b5132
RH
8054 switch (c)
8055 {
8056 case 'r':
8057 case 's':
8058 case 'v':
8059 case 'b':
38487616 8060 ip->insn_opcode |= regno << OP_SH_RS;
252b5132
RH
8061 break;
8062 case 'd':
8063 case 'G':
38487616 8064 ip->insn_opcode |= regno << OP_SH_RD;
252b5132 8065 break;
4372b673 8066 case 'U':
38487616
TS
8067 ip->insn_opcode |= regno << OP_SH_RD;
8068 ip->insn_opcode |= regno << OP_SH_RT;
4372b673 8069 break;
252b5132
RH
8070 case 'w':
8071 case 't':
8072 case 'E':
38487616 8073 ip->insn_opcode |= regno << OP_SH_RT;
252b5132
RH
8074 break;
8075 case 'x':
8076 /* This case exists because on the r3000 trunc
8077 expands into a macro which requires a gp
8078 register. On the r6000 or r4000 it is
8079 assembled into a single instruction which
8080 ignores the register. Thus the insn version
8081 is MIPS_ISA2 and uses 'x', and the macro
8082 version is MIPS_ISA1 and uses 't'. */
8083 break;
8084 case 'z':
8085 /* This case is for the div instruction, which
8086 acts differently if the destination argument
8087 is $0. This only matches $0, and is checked
8088 outside the switch. */
8089 break;
8090 case 'D':
8091 /* Itbl operand; not yet implemented. FIXME ?? */
8092 break;
8093 /* What about all other operands like 'i', which
8094 can be specified in the opcode table? */
8095 }
8096 lastregno = regno;
8097 continue;
8098 }
8099 notreg:
8100 switch (*args++)
8101 {
8102 case 'r':
8103 case 'v':
38487616 8104 ip->insn_opcode |= lastregno << OP_SH_RS;
252b5132
RH
8105 continue;
8106 case 'w':
38487616 8107 ip->insn_opcode |= lastregno << OP_SH_RT;
252b5132
RH
8108 continue;
8109 }
8110 break;
8111
deec1734
CD
8112 case 'O': /* MDMX alignment immediate constant. */
8113 my_getExpression (&imm_expr, s);
8114 check_absolute_expr (ip, &imm_expr);
8115 if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
8116 {
8117 as_warn ("Improper align amount (%ld), using low bits",
8118 (long) imm_expr.X_add_number);
8119 imm_expr.X_add_number &= OP_MASK_ALN;
8120 }
8121 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_ALN;
8122 imm_expr.X_op = O_absent;
8123 s = expr_end;
8124 continue;
8125
8126 case 'Q': /* MDMX vector, element sel, or const. */
8127 if (s[0] != '$')
8128 {
8129 /* MDMX Immediate. */
8130 my_getExpression (&imm_expr, s);
8131 check_absolute_expr (ip, &imm_expr);
8132 if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
8133 {
8134 as_warn (_("Invalid MDMX Immediate (%ld)"),
8135 (long) imm_expr.X_add_number);
8136 imm_expr.X_add_number &= OP_MASK_FT;
8137 }
8138 imm_expr.X_add_number &= OP_MASK_FT;
8139 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8140 ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
8141 else
8142 ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
8143 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_FT;
8144 imm_expr.X_op = O_absent;
8145 s = expr_end;
8146 continue;
8147 }
8148 /* Not MDMX Immediate. Fall through. */
8149 case 'X': /* MDMX destination register. */
8150 case 'Y': /* MDMX source register. */
8151 case 'Z': /* MDMX target register. */
8152 is_mdmx = 1;
252b5132
RH
8153 case 'D': /* floating point destination register */
8154 case 'S': /* floating point source register */
8155 case 'T': /* floating point target register */
8156 case 'R': /* floating point source register */
8157 case 'V':
8158 case 'W':
8159 s_reset = s;
deec1734
CD
8160 /* Accept $fN for FP and MDMX register numbers, and in
8161 addition accept $vN for MDMX register numbers. */
8162 if ((s[0] == '$' && s[1] == 'f' && ISDIGIT (s[2]))
8163 || (is_mdmx != 0 && s[0] == '$' && s[1] == 'v'
8164 && ISDIGIT (s[2])))
252b5132
RH
8165 {
8166 s += 2;
8167 regno = 0;
8168 do
8169 {
8170 regno *= 10;
8171 regno += *s - '0';
8172 ++s;
8173 }
3882b010 8174 while (ISDIGIT (*s));
252b5132
RH
8175
8176 if (regno > 31)
8177 as_bad (_("Invalid float register number (%d)"), regno);
8178
8179 if ((regno & 1) != 0
ca4e0257 8180 && HAVE_32BIT_FPRS
252b5132
RH
8181 && ! (strcmp (str, "mtc1") == 0
8182 || strcmp (str, "mfc1") == 0
8183 || strcmp (str, "lwc1") == 0
8184 || strcmp (str, "swc1") == 0
8185 || strcmp (str, "l.s") == 0
8186 || strcmp (str, "s.s") == 0))
8187 as_warn (_("Float register should be even, was %d"),
8188 regno);
8189
8190 c = *args;
8191 if (*s == ' ')
f9419b05 8192 ++s;
252b5132
RH
8193 if (args[1] != *s)
8194 {
8195 if (c == 'V' || c == 'W')
8196 {
8197 regno = lastregno;
8198 s = s_reset;
f9419b05 8199 ++args;
252b5132
RH
8200 }
8201 }
8202 switch (c)
8203 {
8204 case 'D':
deec1734 8205 case 'X':
38487616 8206 ip->insn_opcode |= regno << OP_SH_FD;
252b5132
RH
8207 break;
8208 case 'V':
8209 case 'S':
deec1734 8210 case 'Y':
38487616 8211 ip->insn_opcode |= regno << OP_SH_FS;
252b5132 8212 break;
deec1734
CD
8213 case 'Q':
8214 /* This is like 'Z', but also needs to fix the MDMX
8215 vector/scalar select bits. Note that the
8216 scalar immediate case is handled above. */
8217 if (*s == '[')
8218 {
8219 int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
8220 int max_el = (is_qh ? 3 : 7);
8221 s++;
8222 my_getExpression(&imm_expr, s);
8223 check_absolute_expr (ip, &imm_expr);
8224 s = expr_end;
8225 if (imm_expr.X_add_number > max_el)
8226 as_bad(_("Bad element selector %ld"),
8227 (long) imm_expr.X_add_number);
8228 imm_expr.X_add_number &= max_el;
8229 ip->insn_opcode |= (imm_expr.X_add_number
8230 << (OP_SH_VSEL +
8231 (is_qh ? 2 : 1)));
8232 if (*s != ']')
8233 as_warn(_("Expecting ']' found '%s'"), s);
8234 else
8235 s++;
8236 }
8237 else
8238 {
8239 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8240 ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
8241 << OP_SH_VSEL);
8242 else
8243 ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
8244 OP_SH_VSEL);
8245 }
8246 /* Fall through */
252b5132
RH
8247 case 'W':
8248 case 'T':
deec1734 8249 case 'Z':
38487616 8250 ip->insn_opcode |= regno << OP_SH_FT;
252b5132
RH
8251 break;
8252 case 'R':
38487616 8253 ip->insn_opcode |= regno << OP_SH_FR;
252b5132
RH
8254 break;
8255 }
8256 lastregno = regno;
8257 continue;
8258 }
8259
252b5132
RH
8260 switch (*args++)
8261 {
8262 case 'V':
38487616 8263 ip->insn_opcode |= lastregno << OP_SH_FS;
252b5132
RH
8264 continue;
8265 case 'W':
38487616 8266 ip->insn_opcode |= lastregno << OP_SH_FT;
252b5132
RH
8267 continue;
8268 }
8269 break;
8270
8271 case 'I':
8272 my_getExpression (&imm_expr, s);
8273 if (imm_expr.X_op != O_big
8274 && imm_expr.X_op != O_constant)
8275 insn_error = _("absolute expression required");
8276 s = expr_end;
8277 continue;
8278
8279 case 'A':
8280 my_getExpression (&offset_expr, s);
f6688943 8281 *imm_reloc = BFD_RELOC_32;
252b5132
RH
8282 s = expr_end;
8283 continue;
8284
8285 case 'F':
8286 case 'L':
8287 case 'f':
8288 case 'l':
8289 {
8290 int f64;
ca4e0257 8291 int using_gprs;
252b5132
RH
8292 char *save_in;
8293 char *err;
8294 unsigned char temp[8];
8295 int len;
8296 unsigned int length;
8297 segT seg;
8298 subsegT subseg;
8299 char *p;
8300
8301 /* These only appear as the last operand in an
8302 instruction, and every instruction that accepts
8303 them in any variant accepts them in all variants.
8304 This means we don't have to worry about backing out
8305 any changes if the instruction does not match.
8306
8307 The difference between them is the size of the
8308 floating point constant and where it goes. For 'F'
8309 and 'L' the constant is 64 bits; for 'f' and 'l' it
8310 is 32 bits. Where the constant is placed is based
8311 on how the MIPS assembler does things:
8312 F -- .rdata
8313 L -- .lit8
8314 f -- immediate value
8315 l -- .lit4
8316
8317 The .lit4 and .lit8 sections are only used if
8318 permitted by the -G argument.
8319
8320 When generating embedded PIC code, we use the
8321 .lit8 section but not the .lit4 section (we can do
8322 .lit4 inline easily; we need to put .lit8
8323 somewhere in the data segment, and using .lit8
8324 permits the linker to eventually combine identical
ca4e0257
RS
8325 .lit8 entries).
8326
8327 The code below needs to know whether the target register
8328 is 32 or 64 bits wide. It relies on the fact 'f' and
8329 'F' are used with GPR-based instructions and 'l' and
8330 'L' are used with FPR-based instructions. */
252b5132
RH
8331
8332 f64 = *args == 'F' || *args == 'L';
ca4e0257 8333 using_gprs = *args == 'F' || *args == 'f';
252b5132
RH
8334
8335 save_in = input_line_pointer;
8336 input_line_pointer = s;
8337 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
8338 length = len;
8339 s = input_line_pointer;
8340 input_line_pointer = save_in;
8341 if (err != NULL && *err != '\0')
8342 {
8343 as_bad (_("Bad floating point constant: %s"), err);
8344 memset (temp, '\0', sizeof temp);
8345 length = f64 ? 8 : 4;
8346 }
8347
156c2f8b 8348 assert (length == (unsigned) (f64 ? 8 : 4));
252b5132
RH
8349
8350 if (*args == 'f'
8351 || (*args == 'l'
8352 && (! USE_GLOBAL_POINTER_OPT
8353 || mips_pic == EMBEDDED_PIC
8354 || g_switch_value < 4
8355 || (temp[0] == 0 && temp[1] == 0)
8356 || (temp[2] == 0 && temp[3] == 0))))
8357 {
8358 imm_expr.X_op = O_constant;
8359 if (! target_big_endian)
8360 imm_expr.X_add_number = bfd_getl32 (temp);
8361 else
8362 imm_expr.X_add_number = bfd_getb32 (temp);
8363 }
8364 else if (length > 4
119d663a 8365 && ! mips_disable_float_construction
ca4e0257
RS
8366 /* Constants can only be constructed in GPRs and
8367 copied to FPRs if the GPRs are at least as wide
8368 as the FPRs. Force the constant into memory if
8369 we are using 64-bit FPRs but the GPRs are only
8370 32 bits wide. */
8371 && (using_gprs
8372 || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
252b5132
RH
8373 && ((temp[0] == 0 && temp[1] == 0)
8374 || (temp[2] == 0 && temp[3] == 0))
8375 && ((temp[4] == 0 && temp[5] == 0)
8376 || (temp[6] == 0 && temp[7] == 0)))
8377 {
ca4e0257
RS
8378 /* The value is simple enough to load with a couple of
8379 instructions. If using 32-bit registers, set
8380 imm_expr to the high order 32 bits and offset_expr to
8381 the low order 32 bits. Otherwise, set imm_expr to
8382 the entire 64 bit constant. */
8383 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
252b5132
RH
8384 {
8385 imm_expr.X_op = O_constant;
8386 offset_expr.X_op = O_constant;
8387 if (! target_big_endian)
8388 {
8389 imm_expr.X_add_number = bfd_getl32 (temp + 4);
8390 offset_expr.X_add_number = bfd_getl32 (temp);
8391 }
8392 else
8393 {
8394 imm_expr.X_add_number = bfd_getb32 (temp);
8395 offset_expr.X_add_number = bfd_getb32 (temp + 4);
8396 }
8397 if (offset_expr.X_add_number == 0)
8398 offset_expr.X_op = O_absent;
8399 }
8400 else if (sizeof (imm_expr.X_add_number) > 4)
8401 {
8402 imm_expr.X_op = O_constant;
8403 if (! target_big_endian)
8404 imm_expr.X_add_number = bfd_getl64 (temp);
8405 else
8406 imm_expr.X_add_number = bfd_getb64 (temp);
8407 }
8408 else
8409 {
8410 imm_expr.X_op = O_big;
8411 imm_expr.X_add_number = 4;
8412 if (! target_big_endian)
8413 {
8414 generic_bignum[0] = bfd_getl16 (temp);
8415 generic_bignum[1] = bfd_getl16 (temp + 2);
8416 generic_bignum[2] = bfd_getl16 (temp + 4);
8417 generic_bignum[3] = bfd_getl16 (temp + 6);
8418 }
8419 else
8420 {
8421 generic_bignum[0] = bfd_getb16 (temp + 6);
8422 generic_bignum[1] = bfd_getb16 (temp + 4);
8423 generic_bignum[2] = bfd_getb16 (temp + 2);
8424 generic_bignum[3] = bfd_getb16 (temp);
8425 }
8426 }
8427 }
8428 else
8429 {
8430 const char *newname;
8431 segT new_seg;
8432
8433 /* Switch to the right section. */
8434 seg = now_seg;
8435 subseg = now_subseg;
8436 switch (*args)
8437 {
8438 default: /* unused default case avoids warnings. */
8439 case 'L':
8440 newname = RDATA_SECTION_NAME;
bb2d6cd7
GK
8441 if ((USE_GLOBAL_POINTER_OPT && g_switch_value >= 8)
8442 || mips_pic == EMBEDDED_PIC)
252b5132
RH
8443 newname = ".lit8";
8444 break;
8445 case 'F':
bb2d6cd7
GK
8446 if (mips_pic == EMBEDDED_PIC)
8447 newname = ".lit8";
8448 else
8449 newname = RDATA_SECTION_NAME;
252b5132
RH
8450 break;
8451 case 'l':
8452 assert (!USE_GLOBAL_POINTER_OPT
8453 || g_switch_value >= 4);
8454 newname = ".lit4";
8455 break;
8456 }
8457 new_seg = subseg_new (newname, (subsegT) 0);
8458 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
8459 bfd_set_section_flags (stdoutput, new_seg,
8460 (SEC_ALLOC
8461 | SEC_LOAD
8462 | SEC_READONLY
8463 | SEC_DATA));
8464 frag_align (*args == 'l' ? 2 : 3, 0, 0);
8465 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
8466 && strcmp (TARGET_OS, "elf") != 0)
8467 record_alignment (new_seg, 4);
8468 else
8469 record_alignment (new_seg, *args == 'l' ? 2 : 3);
8470 if (seg == now_seg)
8471 as_bad (_("Can't use floating point insn in this section"));
8472
8473 /* Set the argument to the current address in the
8474 section. */
8475 offset_expr.X_op = O_symbol;
8476 offset_expr.X_add_symbol =
8477 symbol_new ("L0\001", now_seg,
8478 (valueT) frag_now_fix (), frag_now);
8479 offset_expr.X_add_number = 0;
8480
8481 /* Put the floating point number into the section. */
8482 p = frag_more ((int) length);
8483 memcpy (p, temp, length);
8484
8485 /* Switch back to the original section. */
8486 subseg_set (seg, subseg);
8487 }
8488 }
8489 continue;
8490
8491 case 'i': /* 16 bit unsigned immediate */
8492 case 'j': /* 16 bit signed immediate */
f6688943 8493 *imm_reloc = BFD_RELOC_LO16;
252b5132 8494 c = my_getSmallExpression (&imm_expr, s);
fb1b3232 8495 if (c != S_EX_NONE)
252b5132 8496 {
fb1b3232 8497 if (c != S_EX_LO)
252b5132
RH
8498 {
8499 if (imm_expr.X_op == O_constant)
8500 imm_expr.X_add_number =
8501 (imm_expr.X_add_number >> 16) & 0xffff;
ad8d3bb3 8502#ifdef OBJ_ELF
fb1b3232 8503 else if (c == S_EX_HIGHEST)
98d3f06f 8504 *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
fb1b3232 8505 else if (c == S_EX_HIGHER)
98d3f06f 8506 *imm_reloc = BFD_RELOC_MIPS_HIGHER;
ad8d3bb3
TS
8507 else if (c == S_EX_GP_REL)
8508 {
8509 /* This occurs in NewABI only. */
8510 c = my_getSmallExpression (&imm_expr, s);
8511 if (c != S_EX_NEG)
8512 as_bad (_("bad composition of relocations"));
8513 else
8514 {
8515 c = my_getSmallExpression (&imm_expr, s);
8516 if (c != S_EX_LO)
8517 as_bad (_("bad composition of relocations"));
8518 else
8519 {
8520 imm_reloc[0] = BFD_RELOC_GPREL16;
8521 imm_reloc[1] = BFD_RELOC_MIPS_SUB;
8522 imm_reloc[2] = BFD_RELOC_LO16;
8523 }
8524 }
8525 }
8526#endif
fb1b3232 8527 else if (c == S_EX_HI)
252b5132 8528 {
f6688943 8529 *imm_reloc = BFD_RELOC_HI16_S;
252b5132
RH
8530 imm_unmatched_hi = true;
8531 }
8532 else
f6688943 8533 *imm_reloc = BFD_RELOC_HI16;
252b5132
RH
8534 }
8535 else if (imm_expr.X_op == O_constant)
8536 imm_expr.X_add_number &= 0xffff;
8537 }
8538 if (*args == 'i')
8539 {
fb1b3232 8540 if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
252b5132 8541 || ((imm_expr.X_add_number < 0
beae10d5
KH
8542 || imm_expr.X_add_number >= 0x10000)
8543 && imm_expr.X_op == O_constant))
252b5132
RH
8544 {
8545 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8546 !strcmp (insn->name, insn[1].name))
8547 break;
2ae7e77b
AH
8548 if (imm_expr.X_op == O_constant
8549 || imm_expr.X_op == O_big)
252b5132
RH
8550 as_bad (_("16 bit expression not in range 0..65535"));
8551 }
8552 }
8553 else
8554 {
8555 int more;
8556 offsetT max;
8557
8558 /* The upper bound should be 0x8000, but
8559 unfortunately the MIPS assembler accepts numbers
8560 from 0x8000 to 0xffff and sign extends them, and
8561 we want to be compatible. We only permit this
8562 extended range for an instruction which does not
8563 provide any further alternates, since those
8564 alternates may handle other cases. People should
8565 use the numbers they mean, rather than relying on
8566 a mysterious sign extension. */
8567 more = (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8568 strcmp (insn->name, insn[1].name) == 0);
8569 if (more)
8570 max = 0x8000;
8571 else
8572 max = 0x10000;
fb1b3232 8573 if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
252b5132 8574 || ((imm_expr.X_add_number < -0x8000
beae10d5
KH
8575 || imm_expr.X_add_number >= max)
8576 && imm_expr.X_op == O_constant)
252b5132
RH
8577 || (more
8578 && imm_expr.X_add_number < 0
ca4e0257 8579 && HAVE_64BIT_GPRS
252b5132
RH
8580 && imm_expr.X_unsigned
8581 && sizeof (imm_expr.X_add_number) <= 4))
8582 {
8583 if (more)
8584 break;
2ae7e77b
AH
8585 if (imm_expr.X_op == O_constant
8586 || imm_expr.X_op == O_big)
252b5132
RH
8587 as_bad (_("16 bit expression not in range -32768..32767"));
8588 }
8589 }
8590 s = expr_end;
8591 continue;
8592
8593 case 'o': /* 16 bit offset */
8594 c = my_getSmallExpression (&offset_expr, s);
8595
8596 /* If this value won't fit into a 16 bit offset, then go
8597 find a macro that will generate the 32 bit offset
afdbd6d0 8598 code pattern. */
fb1b3232 8599 if (c == S_EX_NONE
252b5132
RH
8600 && (offset_expr.X_op != O_constant
8601 || offset_expr.X_add_number >= 0x8000
afdbd6d0 8602 || offset_expr.X_add_number < -0x8000))
252b5132
RH
8603 break;
8604
fb1b3232 8605 if (c == S_EX_HI)
252b5132
RH
8606 {
8607 if (offset_expr.X_op != O_constant)
8608 break;
8609 offset_expr.X_add_number =
8610 (offset_expr.X_add_number >> 16) & 0xffff;
8611 }
f6688943 8612 *offset_reloc = BFD_RELOC_LO16;
252b5132
RH
8613 s = expr_end;
8614 continue;
8615
8616 case 'p': /* pc relative offset */
cb56d3d3 8617 if (mips_pic == EMBEDDED_PIC)
f6688943 8618 *offset_reloc = BFD_RELOC_16_PCREL_S2;
cb56d3d3 8619 else
f6688943 8620 *offset_reloc = BFD_RELOC_16_PCREL;
252b5132
RH
8621 my_getExpression (&offset_expr, s);
8622 s = expr_end;
8623 continue;
8624
8625 case 'u': /* upper 16 bits */
8626 c = my_getSmallExpression (&imm_expr, s);
f6688943 8627 *imm_reloc = BFD_RELOC_LO16;
e7d556df 8628 if (c != S_EX_NONE)
252b5132 8629 {
fb1b3232 8630 if (c != S_EX_LO)
252b5132
RH
8631 {
8632 if (imm_expr.X_op == O_constant)
8633 imm_expr.X_add_number =
8634 (imm_expr.X_add_number >> 16) & 0xffff;
fb1b3232 8635 else if (c == S_EX_HI)
252b5132 8636 {
f6688943 8637 *imm_reloc = BFD_RELOC_HI16_S;
252b5132
RH
8638 imm_unmatched_hi = true;
8639 }
645dc66c
TS
8640#ifdef OBJ_ELF
8641 else if (c == S_EX_HIGHEST)
98d3f06f 8642 *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
ad8d3bb3
TS
8643 else if (c == S_EX_GP_REL)
8644 {
8645 /* This occurs in NewABI only. */
8646 c = my_getSmallExpression (&imm_expr, s);
8647 if (c != S_EX_NEG)
8648 as_bad (_("bad composition of relocations"));
8649 else
8650 {
8651 c = my_getSmallExpression (&imm_expr, s);
8652 if (c != S_EX_HI)
8653 as_bad (_("bad composition of relocations"));
8654 else
8655 {
8656 imm_reloc[0] = BFD_RELOC_GPREL16;
8657 imm_reloc[1] = BFD_RELOC_MIPS_SUB;
8658 imm_reloc[2] = BFD_RELOC_HI16_S;
8659 }
8660 }
8661 }
8662#endif
252b5132 8663 else
f6688943 8664 *imm_reloc = BFD_RELOC_HI16;
252b5132
RH
8665 }
8666 else if (imm_expr.X_op == O_constant)
8667 imm_expr.X_add_number &= 0xffff;
8668 }
8669 if (imm_expr.X_op == O_constant
8670 && (imm_expr.X_add_number < 0
8671 || imm_expr.X_add_number >= 0x10000))
8672 as_bad (_("lui expression not in range 0..65535"));
8673 s = expr_end;
8674 continue;
8675
8676 case 'a': /* 26 bit address */
8677 my_getExpression (&offset_expr, s);
8678 s = expr_end;
f6688943 8679 *offset_reloc = BFD_RELOC_MIPS_JMP;
252b5132
RH
8680 continue;
8681
8682 case 'N': /* 3 bit branch condition code */
8683 case 'M': /* 3 bit compare condition code */
8684 if (strncmp (s, "$fcc", 4) != 0)
8685 break;
8686 s += 4;
8687 regno = 0;
8688 do
8689 {
8690 regno *= 10;
8691 regno += *s - '0';
8692 ++s;
8693 }
3882b010 8694 while (ISDIGIT (*s));
252b5132
RH
8695 if (regno > 7)
8696 as_bad (_("invalid condition code register $fcc%d"), regno);
8697 if (*args == 'N')
8698 ip->insn_opcode |= regno << OP_SH_BCC;
8699 else
8700 ip->insn_opcode |= regno << OP_SH_CCC;
beae10d5 8701 continue;
252b5132 8702
156c2f8b
NC
8703 case 'H':
8704 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
8705 s += 2;
3882b010 8706 if (ISDIGIT (*s))
156c2f8b
NC
8707 {
8708 c = 0;
8709 do
8710 {
8711 c *= 10;
8712 c += *s - '0';
8713 ++s;
8714 }
3882b010 8715 while (ISDIGIT (*s));
156c2f8b
NC
8716 }
8717 else
8718 c = 8; /* Invalid sel value. */
8719
8720 if (c > 7)
8721 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
8722 ip->insn_opcode |= c;
8723 continue;
8724
252b5132
RH
8725 default:
8726 as_bad (_("bad char = '%c'\n"), *args);
8727 internalError ();
8728 }
8729 break;
8730 }
8731 /* Args don't match. */
8732 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8733 !strcmp (insn->name, insn[1].name))
8734 {
8735 ++insn;
8736 s = argsStart;
268f6bed 8737 insn_error = _("illegal operands");
252b5132
RH
8738 continue;
8739 }
268f6bed
L
8740 if (save_c)
8741 *(--s) = save_c;
252b5132
RH
8742 insn_error = _("illegal operands");
8743 return;
8744 }
8745}
8746
8747/* This routine assembles an instruction into its binary format when
8748 assembling for the mips16. As a side effect, it sets one of the
8749 global variables imm_reloc or offset_reloc to the type of
8750 relocation to do if one of the operands is an address expression.
8751 It also sets mips16_small and mips16_ext if the user explicitly
8752 requested a small or extended instruction. */
8753
8754static void
8755mips16_ip (str, ip)
8756 char *str;
8757 struct mips_cl_insn *ip;
8758{
8759 char *s;
8760 const char *args;
8761 struct mips_opcode *insn;
8762 char *argsstart;
8763 unsigned int regno;
8764 unsigned int lastregno = 0;
8765 char *s_reset;
8766
8767 insn_error = NULL;
8768
8769 mips16_small = false;
8770 mips16_ext = false;
8771
3882b010 8772 for (s = str; ISLOWER (*s); ++s)
252b5132
RH
8773 ;
8774 switch (*s)
8775 {
8776 case '\0':
8777 break;
8778
8779 case ' ':
8780 *s++ = '\0';
8781 break;
8782
8783 case '.':
8784 if (s[1] == 't' && s[2] == ' ')
8785 {
8786 *s = '\0';
8787 mips16_small = true;
8788 s += 3;
8789 break;
8790 }
8791 else if (s[1] == 'e' && s[2] == ' ')
8792 {
8793 *s = '\0';
8794 mips16_ext = true;
8795 s += 3;
8796 break;
8797 }
8798 /* Fall through. */
8799 default:
8800 insn_error = _("unknown opcode");
8801 return;
8802 }
8803
8804 if (mips_opts.noautoextend && ! mips16_ext)
8805 mips16_small = true;
8806
8807 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
8808 {
8809 insn_error = _("unrecognized opcode");
8810 return;
8811 }
8812
8813 argsstart = s;
8814 for (;;)
8815 {
8816 assert (strcmp (insn->name, str) == 0);
8817
8818 ip->insn_mo = insn;
8819 ip->insn_opcode = insn->match;
8820 ip->use_extend = false;
8821 imm_expr.X_op = O_absent;
f6688943
TS
8822 imm_reloc[0] = BFD_RELOC_UNUSED;
8823 imm_reloc[1] = BFD_RELOC_UNUSED;
8824 imm_reloc[2] = BFD_RELOC_UNUSED;
252b5132 8825 offset_expr.X_op = O_absent;
f6688943
TS
8826 offset_reloc[0] = BFD_RELOC_UNUSED;
8827 offset_reloc[1] = BFD_RELOC_UNUSED;
8828 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
8829 for (args = insn->args; 1; ++args)
8830 {
8831 int c;
8832
8833 if (*s == ' ')
8834 ++s;
8835
8836 /* In this switch statement we call break if we did not find
8837 a match, continue if we did find a match, or return if we
8838 are done. */
8839
8840 c = *args;
8841 switch (c)
8842 {
8843 case '\0':
8844 if (*s == '\0')
8845 {
8846 /* Stuff the immediate value in now, if we can. */
8847 if (imm_expr.X_op == O_constant
f6688943 8848 && *imm_reloc > BFD_RELOC_UNUSED
252b5132
RH
8849 && insn->pinfo != INSN_MACRO)
8850 {
c4e7957c 8851 mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
252b5132
RH
8852 imm_expr.X_add_number, true, mips16_small,
8853 mips16_ext, &ip->insn_opcode,
8854 &ip->use_extend, &ip->extend);
8855 imm_expr.X_op = O_absent;
f6688943 8856 *imm_reloc = BFD_RELOC_UNUSED;
252b5132
RH
8857 }
8858
8859 return;
8860 }
8861 break;
8862
8863 case ',':
8864 if (*s++ == c)
8865 continue;
8866 s--;
8867 switch (*++args)
8868 {
8869 case 'v':
8870 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
8871 continue;
8872 case 'w':
8873 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
8874 continue;
8875 }
8876 break;
8877
8878 case '(':
8879 case ')':
8880 if (*s++ == c)
8881 continue;
8882 break;
8883
8884 case 'v':
8885 case 'w':
8886 if (s[0] != '$')
8887 {
8888 if (c == 'v')
8889 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
8890 else
8891 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
8892 ++args;
8893 continue;
8894 }
8895 /* Fall through. */
8896 case 'x':
8897 case 'y':
8898 case 'z':
8899 case 'Z':
8900 case '0':
8901 case 'S':
8902 case 'R':
8903 case 'X':
8904 case 'Y':
8905 if (s[0] != '$')
8906 break;
8907 s_reset = s;
3882b010 8908 if (ISDIGIT (s[1]))
252b5132
RH
8909 {
8910 ++s;
8911 regno = 0;
8912 do
8913 {
8914 regno *= 10;
8915 regno += *s - '0';
8916 ++s;
8917 }
3882b010 8918 while (ISDIGIT (*s));
252b5132
RH
8919 if (regno > 31)
8920 {
8921 as_bad (_("invalid register number (%d)"), regno);
8922 regno = 2;
8923 }
8924 }
8925 else
8926 {
76db943d
TS
8927 if (s[1] == 'r' && s[2] == 'a')
8928 {
8929 s += 3;
8930 regno = RA;
8931 }
8932 else if (s[1] == 'f' && s[2] == 'p')
252b5132
RH
8933 {
8934 s += 3;
8935 regno = FP;
8936 }
8937 else if (s[1] == 's' && s[2] == 'p')
8938 {
8939 s += 3;
8940 regno = SP;
8941 }
8942 else if (s[1] == 'g' && s[2] == 'p')
8943 {
8944 s += 3;
8945 regno = GP;
8946 }
8947 else if (s[1] == 'a' && s[2] == 't')
8948 {
8949 s += 3;
8950 regno = AT;
8951 }
8952 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8953 {
8954 s += 4;
8955 regno = KT0;
8956 }
8957 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8958 {
8959 s += 4;
8960 regno = KT1;
8961 }
8962 else
8963 break;
8964 }
8965
8966 if (*s == ' ')
8967 ++s;
8968 if (args[1] != *s)
8969 {
8970 if (c == 'v' || c == 'w')
8971 {
8972 regno = mips16_to_32_reg_map[lastregno];
8973 s = s_reset;
f9419b05 8974 ++args;
252b5132
RH
8975 }
8976 }
8977
8978 switch (c)
8979 {
8980 case 'x':
8981 case 'y':
8982 case 'z':
8983 case 'v':
8984 case 'w':
8985 case 'Z':
8986 regno = mips32_to_16_reg_map[regno];
8987 break;
8988
8989 case '0':
8990 if (regno != 0)
8991 regno = ILLEGAL_REG;
8992 break;
8993
8994 case 'S':
8995 if (regno != SP)
8996 regno = ILLEGAL_REG;
8997 break;
8998
8999 case 'R':
9000 if (regno != RA)
9001 regno = ILLEGAL_REG;
9002 break;
9003
9004 case 'X':
9005 case 'Y':
9006 if (regno == AT && ! mips_opts.noat)
9007 as_warn (_("used $at without \".set noat\""));
9008 break;
9009
9010 default:
9011 internalError ();
9012 }
9013
9014 if (regno == ILLEGAL_REG)
9015 break;
9016
9017 switch (c)
9018 {
9019 case 'x':
9020 case 'v':
9021 ip->insn_opcode |= regno << MIPS16OP_SH_RX;
9022 break;
9023 case 'y':
9024 case 'w':
9025 ip->insn_opcode |= regno << MIPS16OP_SH_RY;
9026 break;
9027 case 'z':
9028 ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
9029 break;
9030 case 'Z':
9031 ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
9032 case '0':
9033 case 'S':
9034 case 'R':
9035 break;
9036 case 'X':
9037 ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
9038 break;
9039 case 'Y':
9040 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
9041 ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
9042 break;
9043 default:
9044 internalError ();
9045 }
9046
9047 lastregno = regno;
9048 continue;
9049
9050 case 'P':
9051 if (strncmp (s, "$pc", 3) == 0)
9052 {
9053 s += 3;
9054 continue;
9055 }
9056 break;
9057
9058 case '<':
9059 case '>':
9060 case '[':
9061 case ']':
9062 case '4':
9063 case '5':
9064 case 'H':
9065 case 'W':
9066 case 'D':
9067 case 'j':
9068 case '8':
9069 case 'V':
9070 case 'C':
9071 case 'U':
9072 case 'k':
9073 case 'K':
9074 if (s[0] == '%'
9075 && strncmp (s + 1, "gprel(", sizeof "gprel(" - 1) == 0)
9076 {
9077 /* This is %gprel(SYMBOL). We need to read SYMBOL,
9078 and generate the appropriate reloc. If the text
9079 inside %gprel is not a symbol name with an
9080 optional offset, then we generate a normal reloc
9081 and will probably fail later. */
9082 my_getExpression (&imm_expr, s + sizeof "%gprel" - 1);
9083 if (imm_expr.X_op == O_symbol)
9084 {
9085 mips16_ext = true;
f6688943 9086 *imm_reloc = BFD_RELOC_MIPS16_GPREL;
252b5132
RH
9087 s = expr_end;
9088 ip->use_extend = true;
9089 ip->extend = 0;
9090 continue;
9091 }
9092 }
9093 else
9094 {
9095 /* Just pick up a normal expression. */
9096 my_getExpression (&imm_expr, s);
9097 }
9098
9099 if (imm_expr.X_op == O_register)
9100 {
9101 /* What we thought was an expression turned out to
9102 be a register. */
9103
9104 if (s[0] == '(' && args[1] == '(')
9105 {
9106 /* It looks like the expression was omitted
9107 before a register indirection, which means
9108 that the expression is implicitly zero. We
9109 still set up imm_expr, so that we handle
9110 explicit extensions correctly. */
9111 imm_expr.X_op = O_constant;
9112 imm_expr.X_add_number = 0;
f6688943 9113 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
9114 continue;
9115 }
9116
9117 break;
9118 }
9119
9120 /* We need to relax this instruction. */
f6688943 9121 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
9122 s = expr_end;
9123 continue;
9124
9125 case 'p':
9126 case 'q':
9127 case 'A':
9128 case 'B':
9129 case 'E':
9130 /* We use offset_reloc rather than imm_reloc for the PC
9131 relative operands. This lets macros with both
9132 immediate and address operands work correctly. */
9133 my_getExpression (&offset_expr, s);
9134
9135 if (offset_expr.X_op == O_register)
9136 break;
9137
9138 /* We need to relax this instruction. */
f6688943 9139 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
9140 s = expr_end;
9141 continue;
9142
9143 case '6': /* break code */
9144 my_getExpression (&imm_expr, s);
9145 check_absolute_expr (ip, &imm_expr);
9146 if ((unsigned long) imm_expr.X_add_number > 63)
9147 {
9148 as_warn (_("Invalid value for `%s' (%lu)"),
9149 ip->insn_mo->name,
9150 (unsigned long) imm_expr.X_add_number);
9151 imm_expr.X_add_number &= 0x3f;
9152 }
9153 ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
9154 imm_expr.X_op = O_absent;
9155 s = expr_end;
9156 continue;
9157
9158 case 'a': /* 26 bit address */
9159 my_getExpression (&offset_expr, s);
9160 s = expr_end;
f6688943 9161 *offset_reloc = BFD_RELOC_MIPS16_JMP;
252b5132
RH
9162 ip->insn_opcode <<= 16;
9163 continue;
9164
9165 case 'l': /* register list for entry macro */
9166 case 'L': /* register list for exit macro */
9167 {
9168 int mask;
9169
9170 if (c == 'l')
9171 mask = 0;
9172 else
9173 mask = 7 << 3;
9174 while (*s != '\0')
9175 {
9176 int freg, reg1, reg2;
9177
9178 while (*s == ' ' || *s == ',')
9179 ++s;
9180 if (*s != '$')
9181 {
9182 as_bad (_("can't parse register list"));
9183 break;
9184 }
9185 ++s;
9186 if (*s != 'f')
9187 freg = 0;
9188 else
9189 {
9190 freg = 1;
9191 ++s;
9192 }
9193 reg1 = 0;
3882b010 9194 while (ISDIGIT (*s))
252b5132
RH
9195 {
9196 reg1 *= 10;
9197 reg1 += *s - '0';
9198 ++s;
9199 }
9200 if (*s == ' ')
9201 ++s;
9202 if (*s != '-')
9203 reg2 = reg1;
9204 else
9205 {
9206 ++s;
9207 if (*s != '$')
9208 break;
9209 ++s;
9210 if (freg)
9211 {
9212 if (*s == 'f')
9213 ++s;
9214 else
9215 {
9216 as_bad (_("invalid register list"));
9217 break;
9218 }
9219 }
9220 reg2 = 0;
3882b010 9221 while (ISDIGIT (*s))
252b5132
RH
9222 {
9223 reg2 *= 10;
9224 reg2 += *s - '0';
9225 ++s;
9226 }
9227 }
9228 if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
9229 {
9230 mask &= ~ (7 << 3);
9231 mask |= 5 << 3;
9232 }
9233 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
9234 {
9235 mask &= ~ (7 << 3);
9236 mask |= 6 << 3;
9237 }
9238 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
9239 mask |= (reg2 - 3) << 3;
9240 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
9241 mask |= (reg2 - 15) << 1;
f9419b05 9242 else if (reg1 == RA && reg2 == RA)
252b5132
RH
9243 mask |= 1;
9244 else
9245 {
9246 as_bad (_("invalid register list"));
9247 break;
9248 }
9249 }
9250 /* The mask is filled in in the opcode table for the
9251 benefit of the disassembler. We remove it before
9252 applying the actual mask. */
9253 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
9254 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
9255 }
9256 continue;
9257
9258 case 'e': /* extend code */
9259 my_getExpression (&imm_expr, s);
9260 check_absolute_expr (ip, &imm_expr);
9261 if ((unsigned long) imm_expr.X_add_number > 0x7ff)
9262 {
9263 as_warn (_("Invalid value for `%s' (%lu)"),
9264 ip->insn_mo->name,
9265 (unsigned long) imm_expr.X_add_number);
9266 imm_expr.X_add_number &= 0x7ff;
9267 }
9268 ip->insn_opcode |= imm_expr.X_add_number;
9269 imm_expr.X_op = O_absent;
9270 s = expr_end;
9271 continue;
9272
9273 default:
9274 internalError ();
9275 }
9276 break;
9277 }
9278
9279 /* Args don't match. */
9280 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
9281 strcmp (insn->name, insn[1].name) == 0)
9282 {
9283 ++insn;
9284 s = argsstart;
9285 continue;
9286 }
9287
9288 insn_error = _("illegal operands");
9289
9290 return;
9291 }
9292}
9293
9294/* This structure holds information we know about a mips16 immediate
9295 argument type. */
9296
e972090a
NC
9297struct mips16_immed_operand
9298{
252b5132
RH
9299 /* The type code used in the argument string in the opcode table. */
9300 int type;
9301 /* The number of bits in the short form of the opcode. */
9302 int nbits;
9303 /* The number of bits in the extended form of the opcode. */
9304 int extbits;
9305 /* The amount by which the short form is shifted when it is used;
9306 for example, the sw instruction has a shift count of 2. */
9307 int shift;
9308 /* The amount by which the short form is shifted when it is stored
9309 into the instruction code. */
9310 int op_shift;
9311 /* Non-zero if the short form is unsigned. */
9312 int unsp;
9313 /* Non-zero if the extended form is unsigned. */
9314 int extu;
9315 /* Non-zero if the value is PC relative. */
9316 int pcrel;
9317};
9318
9319/* The mips16 immediate operand types. */
9320
9321static const struct mips16_immed_operand mips16_immed_operands[] =
9322{
9323 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9324 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9325 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9326 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9327 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
9328 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
9329 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
9330 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
9331 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
9332 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
9333 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
9334 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
9335 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
9336 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
9337 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
9338 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
9339 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9340 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9341 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
9342 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
9343 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
9344};
9345
9346#define MIPS16_NUM_IMMED \
9347 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
9348
9349/* Handle a mips16 instruction with an immediate value. This or's the
9350 small immediate value into *INSN. It sets *USE_EXTEND to indicate
9351 whether an extended value is needed; if one is needed, it sets
9352 *EXTEND to the value. The argument type is TYPE. The value is VAL.
9353 If SMALL is true, an unextended opcode was explicitly requested.
9354 If EXT is true, an extended opcode was explicitly requested. If
9355 WARN is true, warn if EXT does not match reality. */
9356
9357static void
9358mips16_immed (file, line, type, val, warn, small, ext, insn, use_extend,
9359 extend)
9360 char *file;
9361 unsigned int line;
9362 int type;
9363 offsetT val;
9364 boolean warn;
9365 boolean small;
9366 boolean ext;
9367 unsigned long *insn;
9368 boolean *use_extend;
9369 unsigned short *extend;
9370{
9371 register const struct mips16_immed_operand *op;
9372 int mintiny, maxtiny;
9373 boolean needext;
9374
9375 op = mips16_immed_operands;
9376 while (op->type != type)
9377 {
9378 ++op;
9379 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
9380 }
9381
9382 if (op->unsp)
9383 {
9384 if (type == '<' || type == '>' || type == '[' || type == ']')
9385 {
9386 mintiny = 1;
9387 maxtiny = 1 << op->nbits;
9388 }
9389 else
9390 {
9391 mintiny = 0;
9392 maxtiny = (1 << op->nbits) - 1;
9393 }
9394 }
9395 else
9396 {
9397 mintiny = - (1 << (op->nbits - 1));
9398 maxtiny = (1 << (op->nbits - 1)) - 1;
9399 }
9400
9401 /* Branch offsets have an implicit 0 in the lowest bit. */
9402 if (type == 'p' || type == 'q')
9403 val /= 2;
9404
9405 if ((val & ((1 << op->shift) - 1)) != 0
9406 || val < (mintiny << op->shift)
9407 || val > (maxtiny << op->shift))
9408 needext = true;
9409 else
9410 needext = false;
9411
9412 if (warn && ext && ! needext)
beae10d5
KH
9413 as_warn_where (file, line,
9414 _("extended operand requested but not required"));
252b5132
RH
9415 if (small && needext)
9416 as_bad_where (file, line, _("invalid unextended operand value"));
9417
9418 if (small || (! ext && ! needext))
9419 {
9420 int insnval;
9421
9422 *use_extend = false;
9423 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
9424 insnval <<= op->op_shift;
9425 *insn |= insnval;
9426 }
9427 else
9428 {
9429 long minext, maxext;
9430 int extval;
9431
9432 if (op->extu)
9433 {
9434 minext = 0;
9435 maxext = (1 << op->extbits) - 1;
9436 }
9437 else
9438 {
9439 minext = - (1 << (op->extbits - 1));
9440 maxext = (1 << (op->extbits - 1)) - 1;
9441 }
9442 if (val < minext || val > maxext)
9443 as_bad_where (file, line,
9444 _("operand value out of range for instruction"));
9445
9446 *use_extend = true;
9447 if (op->extbits == 16)
9448 {
9449 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
9450 val &= 0x1f;
9451 }
9452 else if (op->extbits == 15)
9453 {
9454 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
9455 val &= 0xf;
9456 }
9457 else
9458 {
9459 extval = ((val & 0x1f) << 6) | (val & 0x20);
9460 val = 0;
9461 }
9462
9463 *extend = (unsigned short) extval;
9464 *insn |= val;
9465 }
9466}
9467\f
ad8d3bb3
TS
9468static struct percent_op_match
9469{
9470 const char *str;
9471 const enum small_ex_type type;
9472} percent_op[] =
9473{
ad8d3bb3
TS
9474 {"%lo", S_EX_LO},
9475#ifdef OBJ_ELF
394f9b3a
TS
9476 {"%call_hi", S_EX_CALL_HI},
9477 {"%call_lo", S_EX_CALL_LO},
ad8d3bb3
TS
9478 {"%call16", S_EX_CALL16},
9479 {"%got_disp", S_EX_GOT_DISP},
9480 {"%got_page", S_EX_GOT_PAGE},
9481 {"%got_ofst", S_EX_GOT_OFST},
9482 {"%got_hi", S_EX_GOT_HI},
9483 {"%got_lo", S_EX_GOT_LO},
394f9b3a
TS
9484 {"%got", S_EX_GOT},
9485 {"%gp_rel", S_EX_GP_REL},
9486 {"%half", S_EX_HALF},
ad8d3bb3 9487 {"%highest", S_EX_HIGHEST},
394f9b3a
TS
9488 {"%higher", S_EX_HIGHER},
9489 {"%neg", S_EX_NEG},
ad8d3bb3 9490#endif
394f9b3a 9491 {"%hi", S_EX_HI}
ad8d3bb3
TS
9492};
9493
9494/* Parse small expression input. STR gets adjusted to eat up whitespace.
9495 It detects valid "%percent_op(...)" and "($reg)" strings. Percent_op's
9496 can be nested, this is handled by blanking the innermost, parsing the
9497 rest by subsequent calls. */
252b5132
RH
9498
9499static int
ad8d3bb3
TS
9500my_getSmallParser (str, len, nestlevel)
9501 char **str;
9502 unsigned int *len;
9503 int *nestlevel;
252b5132 9504{
ad8d3bb3
TS
9505 *len = 0;
9506 *str += strspn (*str, " \t");
394f9b3a 9507 /* Check for expression in parentheses. */
ad8d3bb3 9508 if (**str == '(')
252b5132 9509 {
ad8d3bb3
TS
9510 char *b = *str + 1 + strspn (*str + 1, " \t");
9511 char *e;
9512
9513 /* Check for base register. */
9514 if (b[0] == '$')
9515 {
9516 if (strchr (b, ')')
9517 && (e = b + strcspn (b, ") \t"))
9518 && e - b > 1 && e - b < 4)
9519 {
98d3f06f
KH
9520 if ((e - b == 3
9521 && ((b[1] == 'f' && b[2] == 'p')
9522 || (b[1] == 's' && b[2] == 'p')
9523 || (b[1] == 'g' && b[2] == 'p')
9524 || (b[1] == 'a' && b[2] == 't')
9525 || (ISDIGIT (b[1])
9526 && ISDIGIT (b[2]))))
9527 || (ISDIGIT (b[1])))
9528 {
9529 *len = strcspn (*str, ")") + 1;
9530 return S_EX_REGISTER;
9531 }
ad8d3bb3
TS
9532 }
9533 }
394f9b3a 9534 /* Check for percent_op (in parentheses). */
ad8d3bb3
TS
9535 else if (b[0] == '%')
9536 {
9537 *str = b;
394f9b3a 9538 return my_getPercentOp (str, len, nestlevel);
ad8d3bb3 9539 }
76b3015f 9540
394f9b3a
TS
9541 /* Some other expression in the parentheses, which can contain
9542 parentheses itself. Attempt to find the matching one. */
9543 {
9544 int pcnt = 1;
9545 char *s;
9546
9547 *len = 1;
9548 for (s = *str + 1; *s && pcnt; s++, (*len)++)
9549 {
9550 if (*s == '(')
f9419b05 9551 ++pcnt;
394f9b3a 9552 else if (*s == ')')
f9419b05 9553 --pcnt;
394f9b3a
TS
9554 }
9555 }
fb1b3232 9556 }
394f9b3a 9557 /* Check for percent_op (outside of parentheses). */
ad8d3bb3 9558 else if (*str[0] == '%')
394f9b3a
TS
9559 return my_getPercentOp (str, len, nestlevel);
9560
9561 /* Any other expression. */
9562 return S_EX_NONE;
9563}
ad8d3bb3 9564
394f9b3a
TS
9565static int
9566my_getPercentOp (str, len, nestlevel)
9567 char **str;
9568 unsigned int *len;
9569 int *nestlevel;
9570{
9571 char *tmp = *str + 1;
9572 unsigned int i = 0;
ad8d3bb3 9573
394f9b3a
TS
9574 while (ISALPHA (*tmp) || *tmp == '_')
9575 {
9576 *tmp = TOLOWER (*tmp);
9577 tmp++;
9578 }
9579 while (i < (sizeof (percent_op) / sizeof (struct percent_op_match)))
9580 {
9581 if (strncmp (*str, percent_op[i].str, strlen (percent_op[i].str)))
98d3f06f 9582 i++;
394f9b3a 9583 else
ad8d3bb3 9584 {
394f9b3a 9585 int type = percent_op[i].type;
ad8d3bb3 9586
394f9b3a
TS
9587 /* Only %hi and %lo are allowed for OldABI. */
9588 if (! HAVE_NEWABI && type != S_EX_HI && type != S_EX_LO)
9589 return S_EX_NONE;
ad8d3bb3 9590
394f9b3a 9591 *len = strlen (percent_op[i].str);
f9419b05 9592 ++(*nestlevel);
394f9b3a 9593 return type;
ad8d3bb3 9594 }
fb1b3232 9595 }
ad8d3bb3
TS
9596 return S_EX_NONE;
9597}
9598
9599static int
9600my_getSmallExpression (ep, str)
9601 expressionS *ep;
9602 char *str;
9603{
9604 static char *oldstr = NULL;
9605 int c = S_EX_NONE;
9606 int oldc;
394f9b3a 9607 int nestlevel = -1;
ad8d3bb3
TS
9608 unsigned int len;
9609
394f9b3a
TS
9610 /* Don't update oldstr if the last call had nested percent_op's. We need
9611 it to parse the outer ones later. */
ad8d3bb3
TS
9612 if (! oldstr)
9613 oldstr = str;
76b3015f 9614
ad8d3bb3 9615 do
fb1b3232 9616 {
ad8d3bb3 9617 oldc = c;
394f9b3a 9618 c = my_getSmallParser (&str, &len, &nestlevel);
ad8d3bb3
TS
9619 if (c != S_EX_NONE && c != S_EX_REGISTER)
9620 str += len;
fb1b3232 9621 }
ad8d3bb3
TS
9622 while (c != S_EX_NONE && c != S_EX_REGISTER);
9623
394f9b3a 9624 if (nestlevel >= 0)
fb1b3232 9625 {
394f9b3a
TS
9626 /* A percent_op was encountered. Don't try to get an expression if
9627 it is already blanked out. */
ad8d3bb3
TS
9628 if (*(str + strspn (str + 1, " )")) != ')')
9629 {
9630 char save;
9631
394f9b3a 9632 /* Let my_getExpression() stop at the closing parenthesis. */
ad8d3bb3
TS
9633 save = *(str + len);
9634 *(str + len) = '\0';
9635 my_getExpression (ep, str);
9636 *(str + len) = save;
9637 }
394f9b3a 9638 if (nestlevel > 0)
ad8d3bb3 9639 {
394f9b3a
TS
9640 /* Blank out including the % sign and the proper matching
9641 parenthesis. */
9642 int pcnt = 1;
9643 char *s = strrchr (oldstr, '%');
9644 char *end;
9645
9646 for (end = strchr (s, '(') + 1; *end && pcnt; end++)
9647 {
9648 if (*end == '(')
f9419b05 9649 ++pcnt;
394f9b3a 9650 else if (*end == ')')
f9419b05 9651 --pcnt;
394f9b3a
TS
9652 }
9653
9654 memset (s, ' ', end - s);
ad8d3bb3
TS
9655 str = oldstr;
9656 }
9657 else
394f9b3a
TS
9658 expr_end = str + len;
9659
ad8d3bb3 9660 c = oldc;
fb1b3232 9661 }
ad8d3bb3 9662 else if (c == S_EX_NONE)
fb1b3232 9663 {
ad8d3bb3 9664 my_getExpression (ep, str);
fb1b3232 9665 }
ad8d3bb3 9666 else if (c == S_EX_REGISTER)
fb1b3232 9667 {
ad8d3bb3
TS
9668 ep->X_op = O_constant;
9669 expr_end = str;
9670 ep->X_add_symbol = NULL;
9671 ep->X_op_symbol = NULL;
9672 ep->X_add_number = 0;
fb1b3232 9673 }
fb1b3232
TS
9674 else
9675 {
98d3f06f 9676 as_fatal (_("internal error"));
fb1b3232 9677 }
252b5132 9678
394f9b3a
TS
9679 if (nestlevel <= 0)
9680 /* All percent_op's have been handled. */
ad8d3bb3 9681 oldstr = NULL;
fb1b3232 9682
fb1b3232 9683 return c;
252b5132
RH
9684}
9685
9686static void
9687my_getExpression (ep, str)
9688 expressionS *ep;
9689 char *str;
9690{
9691 char *save_in;
98aa84af 9692 valueT val;
252b5132
RH
9693
9694 save_in = input_line_pointer;
9695 input_line_pointer = str;
9696 expression (ep);
9697 expr_end = input_line_pointer;
9698 input_line_pointer = save_in;
9699
9700 /* If we are in mips16 mode, and this is an expression based on `.',
9701 then we bump the value of the symbol by 1 since that is how other
9702 text symbols are handled. We don't bother to handle complex
9703 expressions, just `.' plus or minus a constant. */
9704 if (mips_opts.mips16
9705 && ep->X_op == O_symbol
9706 && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
9707 && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
49309057
ILT
9708 && symbol_get_frag (ep->X_add_symbol) == frag_now
9709 && symbol_constant_p (ep->X_add_symbol)
98aa84af
AM
9710 && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
9711 S_SET_VALUE (ep->X_add_symbol, val + 1);
252b5132
RH
9712}
9713
9714/* Turn a string in input_line_pointer into a floating point constant
bc0d738a
NC
9715 of type TYPE, and store the appropriate bytes in *LITP. The number
9716 of LITTLENUMS emitted is stored in *SIZEP. An error message is
252b5132
RH
9717 returned, or NULL on OK. */
9718
9719char *
9720md_atof (type, litP, sizeP)
9721 int type;
9722 char *litP;
9723 int *sizeP;
9724{
9725 int prec;
9726 LITTLENUM_TYPE words[4];
9727 char *t;
9728 int i;
9729
9730 switch (type)
9731 {
9732 case 'f':
9733 prec = 2;
9734 break;
9735
9736 case 'd':
9737 prec = 4;
9738 break;
9739
9740 default:
9741 *sizeP = 0;
9742 return _("bad call to md_atof");
9743 }
9744
9745 t = atof_ieee (input_line_pointer, type, words);
9746 if (t)
9747 input_line_pointer = t;
9748
9749 *sizeP = prec * 2;
9750
9751 if (! target_big_endian)
9752 {
9753 for (i = prec - 1; i >= 0; i--)
9754 {
9755 md_number_to_chars (litP, (valueT) words[i], 2);
9756 litP += 2;
9757 }
9758 }
9759 else
9760 {
9761 for (i = 0; i < prec; i++)
9762 {
9763 md_number_to_chars (litP, (valueT) words[i], 2);
9764 litP += 2;
9765 }
9766 }
bdaaa2e1 9767
252b5132
RH
9768 return NULL;
9769}
9770
9771void
9772md_number_to_chars (buf, val, n)
9773 char *buf;
9774 valueT val;
9775 int n;
9776{
9777 if (target_big_endian)
9778 number_to_chars_bigendian (buf, val, n);
9779 else
9780 number_to_chars_littleendian (buf, val, n);
9781}
9782\f
ae948b86 9783#ifdef OBJ_ELF
e013f690
TS
9784static int support_64bit_objects(void)
9785{
9786 const char **list, **l;
9787
9788 list = bfd_target_list ();
9789 for (l = list; *l != NULL; l++)
9790#ifdef TE_TMIPS
9791 /* This is traditional mips */
9792 if (strcmp (*l, "elf64-tradbigmips") == 0
9793 || strcmp (*l, "elf64-tradlittlemips") == 0)
9794#else
9795 if (strcmp (*l, "elf64-bigmips") == 0
9796 || strcmp (*l, "elf64-littlemips") == 0)
9797#endif
9798 break;
9799 free (list);
9800 return (*l != NULL);
9801}
ae948b86 9802#endif /* OBJ_ELF */
e013f690 9803
39c0a331 9804CONST char *md_shortopts = "nO::g::G:";
252b5132 9805
e972090a
NC
9806struct option md_longopts[] =
9807{
252b5132
RH
9808#define OPTION_MIPS1 (OPTION_MD_BASE + 1)
9809 {"mips0", no_argument, NULL, OPTION_MIPS1},
9810 {"mips1", no_argument, NULL, OPTION_MIPS1},
9811#define OPTION_MIPS2 (OPTION_MD_BASE + 2)
9812 {"mips2", no_argument, NULL, OPTION_MIPS2},
9813#define OPTION_MIPS3 (OPTION_MD_BASE + 3)
9814 {"mips3", no_argument, NULL, OPTION_MIPS3},
9815#define OPTION_MIPS4 (OPTION_MD_BASE + 4)
9816 {"mips4", no_argument, NULL, OPTION_MIPS4},
ae948b86
TS
9817#define OPTION_MIPS5 (OPTION_MD_BASE + 5)
9818 {"mips5", no_argument, NULL, OPTION_MIPS5},
9819#define OPTION_MIPS32 (OPTION_MD_BASE + 6)
9820 {"mips32", no_argument, NULL, OPTION_MIPS32},
9821#define OPTION_MIPS64 (OPTION_MD_BASE + 7)
9822 {"mips64", no_argument, NULL, OPTION_MIPS64},
9823#define OPTION_MEMBEDDED_PIC (OPTION_MD_BASE + 8)
252b5132 9824 {"membedded-pic", no_argument, NULL, OPTION_MEMBEDDED_PIC},
ae948b86 9825#define OPTION_TRAP (OPTION_MD_BASE + 9)
252b5132
RH
9826 {"trap", no_argument, NULL, OPTION_TRAP},
9827 {"no-break", no_argument, NULL, OPTION_TRAP},
ae948b86 9828#define OPTION_BREAK (OPTION_MD_BASE + 10)
252b5132
RH
9829 {"break", no_argument, NULL, OPTION_BREAK},
9830 {"no-trap", no_argument, NULL, OPTION_BREAK},
ae948b86 9831#define OPTION_EB (OPTION_MD_BASE + 11)
252b5132 9832 {"EB", no_argument, NULL, OPTION_EB},
ae948b86 9833#define OPTION_EL (OPTION_MD_BASE + 12)
252b5132 9834 {"EL", no_argument, NULL, OPTION_EL},
ae948b86 9835#define OPTION_MIPS16 (OPTION_MD_BASE + 13)
252b5132 9836 {"mips16", no_argument, NULL, OPTION_MIPS16},
ae948b86 9837#define OPTION_NO_MIPS16 (OPTION_MD_BASE + 14)
252b5132 9838 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
ae948b86 9839#define OPTION_M7000_HILO_FIX (OPTION_MD_BASE + 15)
6b76fefe 9840 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
9ee72ff1
TS
9841#define OPTION_MNO_7000_HILO_FIX (OPTION_MD_BASE + 16)
9842 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
9843 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
ae948b86
TS
9844#define OPTION_FP32 (OPTION_MD_BASE + 17)
9845 {"mfp32", no_argument, NULL, OPTION_FP32},
9846#define OPTION_GP32 (OPTION_MD_BASE + 18)
c97ef257 9847 {"mgp32", no_argument, NULL, OPTION_GP32},
ae948b86 9848#define OPTION_CONSTRUCT_FLOATS (OPTION_MD_BASE + 19)
119d663a 9849 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
ae948b86 9850#define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MD_BASE + 20)
119d663a 9851 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
ae948b86 9852#define OPTION_MARCH (OPTION_MD_BASE + 21)
ec68c924 9853 {"march", required_argument, NULL, OPTION_MARCH},
ae948b86 9854#define OPTION_MTUNE (OPTION_MD_BASE + 22)
ec68c924 9855 {"mtune", required_argument, NULL, OPTION_MTUNE},
ae948b86
TS
9856#define OPTION_MCPU (OPTION_MD_BASE + 23)
9857 {"mcpu", required_argument, NULL, OPTION_MCPU},
9858#define OPTION_M4650 (OPTION_MD_BASE + 24)
9859 {"m4650", no_argument, NULL, OPTION_M4650},
9860#define OPTION_NO_M4650 (OPTION_MD_BASE + 25)
9861 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
9862#define OPTION_M4010 (OPTION_MD_BASE + 26)
9863 {"m4010", no_argument, NULL, OPTION_M4010},
9864#define OPTION_NO_M4010 (OPTION_MD_BASE + 27)
9865 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
9866#define OPTION_M4100 (OPTION_MD_BASE + 28)
9867 {"m4100", no_argument, NULL, OPTION_M4100},
9868#define OPTION_NO_M4100 (OPTION_MD_BASE + 29)
9869 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
9870#define OPTION_M3900 (OPTION_MD_BASE + 30)
9871 {"m3900", no_argument, NULL, OPTION_M3900},
9872#define OPTION_NO_M3900 (OPTION_MD_BASE + 31)
9873 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
9874#define OPTION_GP64 (OPTION_MD_BASE + 32)
9875 {"mgp64", no_argument, NULL, OPTION_GP64},
1f25f5d3
CD
9876#define OPTION_MIPS3D (OPTION_MD_BASE + 33)
9877 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
9878#define OPTION_NO_MIPS3D (OPTION_MD_BASE + 34)
9879 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
deec1734
CD
9880#define OPTION_MDMX (OPTION_MD_BASE + 35)
9881 {"mdmx", no_argument, NULL, OPTION_MDMX},
9882#define OPTION_NO_MDMX (OPTION_MD_BASE + 36)
9883 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
156c2f8b 9884#ifdef OBJ_ELF
deec1734 9885#define OPTION_ELF_BASE (OPTION_MD_BASE + 37)
156c2f8b 9886#define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
156c2f8b
NC
9887 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
9888 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
ae948b86 9889#define OPTION_NON_SHARED (OPTION_ELF_BASE + 1)
156c2f8b 9890 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
ae948b86 9891#define OPTION_XGOT (OPTION_ELF_BASE + 2)
156c2f8b 9892 {"xgot", no_argument, NULL, OPTION_XGOT},
ae948b86
TS
9893#define OPTION_MABI (OPTION_ELF_BASE + 3)
9894 {"mabi", required_argument, NULL, OPTION_MABI},
9895#define OPTION_32 (OPTION_ELF_BASE + 4)
156c2f8b 9896 {"32", no_argument, NULL, OPTION_32},
ae948b86 9897#define OPTION_N32 (OPTION_ELF_BASE + 5)
e013f690 9898 {"n32", no_argument, NULL, OPTION_N32},
ae948b86 9899#define OPTION_64 (OPTION_ELF_BASE + 6)
156c2f8b 9900 {"64", no_argument, NULL, OPTION_64},
ae948b86 9901#endif /* OBJ_ELF */
252b5132
RH
9902 {NULL, no_argument, NULL, 0}
9903};
156c2f8b 9904size_t md_longopts_size = sizeof (md_longopts);
252b5132
RH
9905
9906int
9907md_parse_option (c, arg)
9908 int c;
9909 char *arg;
9910{
9911 switch (c)
9912 {
119d663a
NC
9913 case OPTION_CONSTRUCT_FLOATS:
9914 mips_disable_float_construction = 0;
9915 break;
bdaaa2e1 9916
119d663a
NC
9917 case OPTION_NO_CONSTRUCT_FLOATS:
9918 mips_disable_float_construction = 1;
9919 break;
bdaaa2e1 9920
252b5132
RH
9921 case OPTION_TRAP:
9922 mips_trap = 1;
9923 break;
9924
9925 case OPTION_BREAK:
9926 mips_trap = 0;
9927 break;
9928
9929 case OPTION_EB:
9930 target_big_endian = 1;
9931 break;
9932
9933 case OPTION_EL:
9934 target_big_endian = 0;
9935 break;
9936
39c0a331
L
9937 case 'n':
9938 warn_nops = 1;
9939 break;
9940
252b5132
RH
9941 case 'O':
9942 if (arg && arg[1] == '0')
9943 mips_optimize = 1;
9944 else
9945 mips_optimize = 2;
9946 break;
9947
9948 case 'g':
9949 if (arg == NULL)
9950 mips_debug = 2;
9951 else
9952 mips_debug = atoi (arg);
9953 /* When the MIPS assembler sees -g or -g2, it does not do
9954 optimizations which limit full symbolic debugging. We take
9955 that to be equivalent to -O0. */
9956 if (mips_debug == 2)
9957 mips_optimize = 1;
9958 break;
9959
9960 case OPTION_MIPS1:
e7af610e 9961 mips_opts.isa = ISA_MIPS1;
252b5132
RH
9962 break;
9963
9964 case OPTION_MIPS2:
e7af610e 9965 mips_opts.isa = ISA_MIPS2;
252b5132
RH
9966 break;
9967
9968 case OPTION_MIPS3:
e7af610e 9969 mips_opts.isa = ISA_MIPS3;
252b5132
RH
9970 break;
9971
9972 case OPTION_MIPS4:
e7af610e
NC
9973 mips_opts.isa = ISA_MIPS4;
9974 break;
9975
84ea6cf2
NC
9976 case OPTION_MIPS5:
9977 mips_opts.isa = ISA_MIPS5;
9978 break;
9979
e7af610e
NC
9980 case OPTION_MIPS32:
9981 mips_opts.isa = ISA_MIPS32;
252b5132
RH
9982 break;
9983
84ea6cf2
NC
9984 case OPTION_MIPS64:
9985 mips_opts.isa = ISA_MIPS64;
9986 break;
9987
ec68c924
EC
9988 case OPTION_MTUNE:
9989 case OPTION_MARCH:
252b5132
RH
9990 case OPTION_MCPU:
9991 {
ec68c924
EC
9992 int cpu = CPU_UNKNOWN;
9993
e7af610e 9994 /* Identify the processor type. */
ec68c924 9995 if (strcasecmp (arg, "default") != 0)
252b5132 9996 {
e7af610e 9997 const struct mips_cpu_info *ci;
252b5132 9998
e7af610e
NC
9999 ci = mips_cpu_info_from_name (arg);
10000 if (ci == NULL || ci->is_isa)
ec68c924
EC
10001 {
10002 switch (c)
10003 {
10004 case OPTION_MTUNE:
10005 as_fatal (_("invalid architecture -mtune=%s"), arg);
10006 break;
10007 case OPTION_MARCH:
10008 as_fatal (_("invalid architecture -march=%s"), arg);
10009 break;
10010 case OPTION_MCPU:
10011 as_fatal (_("invalid architecture -mcpu=%s"), arg);
10012 break;
10013 }
10014 }
e7af610e 10015 else
98d3f06f 10016 cpu = ci->cpu;
ec68c924
EC
10017 }
10018
10019 switch (c)
10020 {
10021 case OPTION_MTUNE:
6dce9e24 10022 if (mips_tune != CPU_UNKNOWN && mips_tune != cpu)
98d3f06f
KH
10023 as_warn (_("A different -mtune= was already specified, is now "
10024 "-mtune=%s"), arg);
ec68c924
EC
10025 mips_tune = cpu;
10026 break;
10027 case OPTION_MARCH:
6dce9e24 10028 if (mips_arch != CPU_UNKNOWN && mips_arch != cpu)
98d3f06f
KH
10029 as_warn (_("A different -march= was already specified, is now "
10030 "-march=%s"), arg);
ec68c924
EC
10031 mips_arch = cpu;
10032 break;
10033 case OPTION_MCPU:
6dce9e24 10034 if (mips_cpu != CPU_UNKNOWN && mips_cpu != cpu)
98d3f06f
KH
10035 as_warn (_("A different -mcpu= was already specified, is now "
10036 "-mcpu=%s"), arg);
ec68c924 10037 mips_cpu = cpu;
252b5132
RH
10038 }
10039 }
10040 break;
10041
10042 case OPTION_M4650:
6dce9e24
TS
10043 if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_R4650)
10044 || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_R4650))
98d3f06f
KH
10045 as_warn (_("A different -march= or -mtune= was already specified, "
10046 "is now -m4650"));
ec68c924
EC
10047 mips_arch = CPU_R4650;
10048 mips_tune = CPU_R4650;
252b5132
RH
10049 break;
10050
10051 case OPTION_NO_M4650:
10052 break;
10053
10054 case OPTION_M4010:
6dce9e24
TS
10055 if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_R4010)
10056 || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_R4010))
98d3f06f
KH
10057 as_warn (_("A different -march= or -mtune= was already specified, "
10058 "is now -m4010"));
ec68c924
EC
10059 mips_arch = CPU_R4010;
10060 mips_tune = CPU_R4010;
252b5132
RH
10061 break;
10062
10063 case OPTION_NO_M4010:
10064 break;
10065
10066 case OPTION_M4100:
6dce9e24
TS
10067 if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_VR4100)
10068 || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_VR4100))
98d3f06f
KH
10069 as_warn (_("A different -march= or -mtune= was already specified, "
10070 "is now -m4100"));
ec68c924
EC
10071 mips_arch = CPU_VR4100;
10072 mips_tune = CPU_VR4100;
252b5132
RH
10073 break;
10074
10075 case OPTION_NO_M4100:
10076 break;
10077
252b5132 10078 case OPTION_M3900:
6dce9e24
TS
10079 if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_R3900)
10080 || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_R3900))
98d3f06f
KH
10081 as_warn (_("A different -march= or -mtune= was already specified, "
10082 "is now -m3900"));
ec68c924
EC
10083 mips_arch = CPU_R3900;
10084 mips_tune = CPU_R3900;
252b5132 10085 break;
bdaaa2e1 10086
252b5132
RH
10087 case OPTION_NO_M3900:
10088 break;
10089
deec1734
CD
10090 case OPTION_MDMX:
10091 mips_opts.ase_mdmx = 1;
10092 break;
10093
10094 case OPTION_NO_MDMX:
10095 mips_opts.ase_mdmx = 0;
10096 break;
10097
252b5132
RH
10098 case OPTION_MIPS16:
10099 mips_opts.mips16 = 1;
10100 mips_no_prev_insn (false);
10101 break;
10102
10103 case OPTION_NO_MIPS16:
10104 mips_opts.mips16 = 0;
10105 mips_no_prev_insn (false);
10106 break;
10107
1f25f5d3
CD
10108 case OPTION_MIPS3D:
10109 mips_opts.ase_mips3d = 1;
10110 break;
10111
10112 case OPTION_NO_MIPS3D:
10113 mips_opts.ase_mips3d = 0;
10114 break;
10115
252b5132
RH
10116 case OPTION_MEMBEDDED_PIC:
10117 mips_pic = EMBEDDED_PIC;
10118 if (USE_GLOBAL_POINTER_OPT && g_switch_seen)
10119 {
10120 as_bad (_("-G may not be used with embedded PIC code"));
10121 return 0;
10122 }
10123 g_switch_value = 0x7fffffff;
10124 break;
10125
0f074f60 10126#ifdef OBJ_ELF
252b5132
RH
10127 /* When generating ELF code, we permit -KPIC and -call_shared to
10128 select SVR4_PIC, and -non_shared to select no PIC. This is
10129 intended to be compatible with Irix 5. */
10130 case OPTION_CALL_SHARED:
10131 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10132 {
10133 as_bad (_("-call_shared is supported only for ELF format"));
10134 return 0;
10135 }
10136 mips_pic = SVR4_PIC;
10137 if (g_switch_seen && g_switch_value != 0)
10138 {
10139 as_bad (_("-G may not be used with SVR4 PIC code"));
10140 return 0;
10141 }
10142 g_switch_value = 0;
10143 break;
10144
10145 case OPTION_NON_SHARED:
10146 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10147 {
10148 as_bad (_("-non_shared is supported only for ELF format"));
10149 return 0;
10150 }
10151 mips_pic = NO_PIC;
10152 break;
10153
10154 /* The -xgot option tells the assembler to use 32 offsets when
10155 accessing the got in SVR4_PIC mode. It is for Irix
10156 compatibility. */
10157 case OPTION_XGOT:
10158 mips_big_got = 1;
10159 break;
0f074f60 10160#endif /* OBJ_ELF */
252b5132
RH
10161
10162 case 'G':
10163 if (! USE_GLOBAL_POINTER_OPT)
10164 {
10165 as_bad (_("-G is not supported for this configuration"));
10166 return 0;
10167 }
10168 else if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
10169 {
10170 as_bad (_("-G may not be used with SVR4 or embedded PIC code"));
10171 return 0;
10172 }
10173 else
10174 g_switch_value = atoi (arg);
10175 g_switch_seen = 1;
10176 break;
10177
0f074f60 10178#ifdef OBJ_ELF
34ba82a8
TS
10179 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
10180 and -mabi=64. */
252b5132 10181 case OPTION_32:
34ba82a8
TS
10182 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10183 {
10184 as_bad (_("-32 is supported for ELF format only"));
10185 return 0;
10186 }
a325df1d 10187 mips_opts.abi = O32_ABI;
252b5132
RH
10188 break;
10189
e013f690 10190 case OPTION_N32:
34ba82a8
TS
10191 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10192 {
10193 as_bad (_("-n32 is supported for ELF format only"));
10194 return 0;
10195 }
a325df1d 10196 mips_opts.abi = N32_ABI;
e013f690 10197 break;
252b5132 10198
e013f690 10199 case OPTION_64:
34ba82a8
TS
10200 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10201 {
10202 as_bad (_("-64 is supported for ELF format only"));
10203 return 0;
10204 }
a325df1d 10205 mips_opts.abi = N64_ABI;
e013f690
TS
10206 if (! support_64bit_objects())
10207 as_fatal (_("No compiled in support for 64 bit object file format"));
252b5132 10208 break;
ae948b86 10209#endif /* OBJ_ELF */
252b5132 10210
c97ef257 10211 case OPTION_GP32:
a325df1d
TS
10212 file_mips_gp32 = 1;
10213 if (mips_opts.abi != O32_ABI)
10214 mips_opts.abi = NO_ABI;
c97ef257
AH
10215 break;
10216
10217 case OPTION_GP64:
a325df1d
TS
10218 file_mips_gp32 = 0;
10219 if (mips_opts.abi == O32_ABI)
10220 mips_opts.abi = NO_ABI;
c97ef257 10221 break;
252b5132 10222
ca4e0257 10223 case OPTION_FP32:
a325df1d
TS
10224 file_mips_fp32 = 1;
10225 if (mips_opts.abi != O32_ABI)
10226 mips_opts.abi = NO_ABI;
ca4e0257
RS
10227 break;
10228
ae948b86 10229#ifdef OBJ_ELF
252b5132 10230 case OPTION_MABI:
34ba82a8
TS
10231 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10232 {
10233 as_bad (_("-mabi is supported for ELF format only"));
10234 return 0;
10235 }
e013f690 10236 if (strcmp (arg, "32") == 0)
a325df1d 10237 mips_opts.abi = O32_ABI;
e013f690 10238 else if (strcmp (arg, "o64") == 0)
a325df1d 10239 mips_opts.abi = O64_ABI;
e013f690 10240 else if (strcmp (arg, "n32") == 0)
a325df1d 10241 mips_opts.abi = N32_ABI;
e013f690
TS
10242 else if (strcmp (arg, "64") == 0)
10243 {
a325df1d 10244 mips_opts.abi = N64_ABI;
e013f690
TS
10245 if (! support_64bit_objects())
10246 as_fatal (_("No compiled in support for 64 bit object file "
10247 "format"));
10248 }
10249 else if (strcmp (arg, "eabi") == 0)
a325df1d 10250 mips_opts.abi = EABI_ABI;
e013f690 10251 else
da0e507f
TS
10252 {
10253 as_fatal (_("invalid abi -mabi=%s"), arg);
10254 return 0;
10255 }
252b5132 10256 break;
e013f690 10257#endif /* OBJ_ELF */
252b5132 10258
6b76fefe
CM
10259 case OPTION_M7000_HILO_FIX:
10260 mips_7000_hilo_fix = true;
10261 break;
10262
9ee72ff1 10263 case OPTION_MNO_7000_HILO_FIX:
6b76fefe
CM
10264 mips_7000_hilo_fix = false;
10265 break;
10266
252b5132
RH
10267 default:
10268 return 0;
10269 }
10270
10271 return 1;
10272}
10273
252b5132
RH
10274static void
10275show (stream, string, col_p, first_p)
10276 FILE *stream;
10277 char *string;
10278 int *col_p;
10279 int *first_p;
10280{
10281 if (*first_p)
10282 {
10283 fprintf (stream, "%24s", "");
10284 *col_p = 24;
10285 }
10286 else
10287 {
10288 fprintf (stream, ", ");
10289 *col_p += 2;
10290 }
10291
10292 if (*col_p + strlen (string) > 72)
10293 {
10294 fprintf (stream, "\n%24s", "");
10295 *col_p = 24;
10296 }
10297
10298 fprintf (stream, "%s", string);
10299 *col_p += strlen (string);
10300
10301 *first_p = 0;
10302}
10303
252b5132
RH
10304void
10305md_show_usage (stream)
10306 FILE *stream;
10307{
10308 int column, first;
10309
beae10d5 10310 fprintf (stream, _("\
252b5132
RH
10311MIPS options:\n\
10312-membedded-pic generate embedded position independent code\n\
10313-EB generate big endian output\n\
10314-EL generate little endian output\n\
9a41af64 10315-g, -g2 do not remove unneeded NOPs or swap branches\n\
252b5132
RH
10316-G NUM allow referencing objects up to NUM bytes\n\
10317 implicitly with the gp register [default 8]\n"));
beae10d5 10318 fprintf (stream, _("\
252b5132
RH
10319-mips1 generate MIPS ISA I instructions\n\
10320-mips2 generate MIPS ISA II instructions\n\
10321-mips3 generate MIPS ISA III instructions\n\
10322-mips4 generate MIPS ISA IV instructions\n\
84ea6cf2 10323-mips5 generate MIPS ISA V instructions\n\
e7af610e 10324-mips32 generate MIPS32 ISA instructions\n\
84ea6cf2 10325-mips64 generate MIPS64 ISA instructions\n\
ec68c924 10326-march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
252b5132
RH
10327
10328 first = 1;
10329
10330 show (stream, "2000", &column, &first);
10331 show (stream, "3000", &column, &first);
10332 show (stream, "3900", &column, &first);
10333 show (stream, "4000", &column, &first);
10334 show (stream, "4010", &column, &first);
10335 show (stream, "4100", &column, &first);
10336 show (stream, "4111", &column, &first);
10337 show (stream, "4300", &column, &first);
10338 show (stream, "4400", &column, &first);
10339 show (stream, "4600", &column, &first);
10340 show (stream, "4650", &column, &first);
10341 show (stream, "5000", &column, &first);
18ae5d72
EC
10342 show (stream, "5200", &column, &first);
10343 show (stream, "5230", &column, &first);
10344 show (stream, "5231", &column, &first);
10345 show (stream, "5261", &column, &first);
10346 show (stream, "5721", &column, &first);
252b5132
RH
10347 show (stream, "6000", &column, &first);
10348 show (stream, "8000", &column, &first);
10349 show (stream, "10000", &column, &first);
d1cf510e 10350 show (stream, "12000", &column, &first);
2e4acd24 10351 show (stream, "sb1", &column, &first);
252b5132
RH
10352 fputc ('\n', stream);
10353
10354 fprintf (stream, _("\
ec68c924 10355-mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
252b5132
RH
10356-no-mCPU don't generate code specific to CPU.\n\
10357 For -mCPU and -no-mCPU, CPU must be one of:\n"));
10358
10359 first = 1;
10360
10361 show (stream, "3900", &column, &first);
10362 show (stream, "4010", &column, &first);
10363 show (stream, "4100", &column, &first);
10364 show (stream, "4650", &column, &first);
10365 fputc ('\n', stream);
10366
beae10d5 10367 fprintf (stream, _("\
252b5132
RH
10368-mips16 generate mips16 instructions\n\
10369-no-mips16 do not generate mips16 instructions\n"));
beae10d5 10370 fprintf (stream, _("\
ca4e0257
RS
10371-mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
10372-mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
252b5132
RH
10373-O0 remove unneeded NOPs, do not swap branches\n\
10374-O remove unneeded NOPs and swap branches\n\
63486801 10375-n warn about NOPs generated from macros\n\
119d663a 10376--[no-]construct-floats [dis]allow floating point values to be constructed\n\
252b5132
RH
10377--trap, --no-break trap exception on div by 0 and mult overflow\n\
10378--break, --no-trap break exception on div by 0 and mult overflow\n"));
10379#ifdef OBJ_ELF
beae10d5 10380 fprintf (stream, _("\
252b5132
RH
10381-KPIC, -call_shared generate SVR4 position independent code\n\
10382-non_shared do not generate position independent code\n\
10383-xgot assume a 32 bit GOT\n\
34ba82a8
TS
10384-mabi=ABI create ABI conformant object file for:\n"));
10385
10386 first = 1;
10387
10388 show (stream, "32", &column, &first);
10389 show (stream, "o64", &column, &first);
10390 show (stream, "n32", &column, &first);
10391 show (stream, "64", &column, &first);
10392 show (stream, "eabi", &column, &first);
80cc45a5 10393
34ba82a8
TS
10394 fputc ('\n', stream);
10395
10396 fprintf (stream, _("\
e013f690
TS
10397-32 create o32 ABI object file (default)\n\
10398-n32 create n32 ABI object file\n\
10399-64 create 64 ABI object file\n"));
252b5132
RH
10400#endif
10401}
10402\f
10403void
e9670677
MR
10404mips_after_parse_args ()
10405{
10406 const char *cpu;
10407 char *a = NULL;
10408 int mips_isa_from_cpu;
10409 int target_cpu_had_mips16 = 0;
10410 const struct mips_cpu_info *ci;
10411
10412 /* GP relative stuff not working for PE */
10413 if (strncmp (TARGET_OS, "pe", 2) == 0
10414 && g_switch_value != 0)
10415 {
10416 if (g_switch_seen)
10417 as_bad (_("-G not supported in this configuration."));
10418 g_switch_value = 0;
10419 }
10420
10421 cpu = TARGET_CPU;
10422 if (strcmp (cpu + (sizeof TARGET_CPU) - 3, "el") == 0)
10423 {
10424 a = xmalloc (sizeof TARGET_CPU);
10425 strcpy (a, TARGET_CPU);
10426 a[(sizeof TARGET_CPU) - 3] = '\0';
10427 cpu = a;
10428 }
10429
10430 if (strncmp (cpu, "mips16", sizeof "mips16" - 1) == 0)
10431 {
10432 target_cpu_had_mips16 = 1;
10433 cpu += sizeof "mips16" - 1;
10434 }
10435
10436 if (mips_opts.mips16 < 0)
10437 mips_opts.mips16 = target_cpu_had_mips16;
10438
10439 /* Backward compatibility for historic -mcpu= option. Check for
10440 incompatible options, warn if -mcpu is used. */
10441 if (mips_cpu != CPU_UNKNOWN
10442 && mips_arch != CPU_UNKNOWN
10443 && mips_cpu != mips_arch)
10444 {
10445 as_fatal (_("The -mcpu option can't be used together with -march. "
10446 "Use -mtune instead of -mcpu."));
10447 }
10448
10449 if (mips_cpu != CPU_UNKNOWN
10450 && mips_tune != CPU_UNKNOWN
10451 && mips_cpu != mips_tune)
10452 {
10453 as_fatal (_("The -mcpu option can't be used together with -mtune. "
10454 "Use -march instead of -mcpu."));
10455 }
10456
10457#if 1
10458 /* For backward compatibility, let -mipsN set various defaults. */
10459 /* This code should go away, to be replaced with something rather more
10460 draconian. Until GCC 3.1 has been released for some reasonable
10461 amount of time, however, we need to support this. */
10462 if (mips_opts.isa != ISA_UNKNOWN)
10463 {
10464 /* Translate -mipsN to the appropriate settings of file_mips_gp32
10465 and file_mips_fp32. Tag binaries as using the mipsN ISA. */
10466 if (file_mips_gp32 < 0)
10467 {
10468 if (ISA_HAS_64BIT_REGS (mips_opts.isa))
10469 file_mips_gp32 = 0;
10470 else
10471 file_mips_gp32 = 1;
10472 }
10473 if (file_mips_fp32 < 0)
10474 {
10475 if (ISA_HAS_64BIT_REGS (mips_opts.isa))
10476 file_mips_fp32 = 0;
10477 else
10478 file_mips_fp32 = 1;
10479 }
10480
10481 ci = mips_cpu_info_from_isa (mips_opts.isa);
10482 assert (ci != NULL);
10483 /* -mipsN has higher priority than -mcpu but lower than -march. */
10484 if (mips_arch == CPU_UNKNOWN)
10485 mips_arch = ci->cpu;
10486
10487 /* Default mips_abi. */
10488 if (mips_opts.abi == NO_ABI)
10489 {
10490 if (mips_opts.isa == ISA_MIPS1 || mips_opts.isa == ISA_MIPS2)
10491 mips_opts.abi = O32_ABI;
10492 else if (mips_opts.isa == ISA_MIPS3 || mips_opts.isa == ISA_MIPS4)
10493 mips_opts.abi = O64_ABI;
10494 }
10495 }
10496
10497 if (mips_arch == CPU_UNKNOWN && mips_cpu != CPU_UNKNOWN)
10498 {
10499 ci = mips_cpu_info_from_cpu (mips_cpu);
10500 assert (ci != NULL);
10501 mips_arch = ci->cpu;
10502 as_warn (_("The -mcpu option is deprecated. Please use -march and "
10503 "-mtune instead."));
10504 }
10505
10506 /* Set tune from -mcpu, not from -mipsN. */
10507 if (mips_tune == CPU_UNKNOWN && mips_cpu != CPU_UNKNOWN)
10508 {
10509 ci = mips_cpu_info_from_cpu (mips_cpu);
10510 assert (ci != NULL);
10511 mips_tune = ci->cpu;
10512 }
10513
10514 /* At this point, mips_arch will either be CPU_UNKNOWN if no ARCH was
10515 specified on the command line, or some other value if one was.
10516 Similarly, mips_opts.isa will be ISA_UNKNOWN if not specified on
10517 the command line, or will be set otherwise if one was. */
10518
10519 if (mips_arch != CPU_UNKNOWN && mips_opts.isa != ISA_UNKNOWN)
10520 /* Handled above. */;
10521#else
10522 if (mips_arch == CPU_UNKNOWN && mips_cpu != CPU_UNKNOWN)
10523 {
10524 ci = mips_cpu_info_from_cpu (mips_cpu);
10525 assert (ci != NULL);
10526 mips_arch = ci->cpu;
10527 as_warn (_("The -mcpu option is deprecated. Please use -march and "
10528 "-mtune instead."));
10529 }
10530
10531 /* At this point, mips_arch will either be CPU_UNKNOWN if no ARCH was
10532 specified on the command line, or some other value if one was.
10533 Similarly, mips_opts.isa will be ISA_UNKNOWN if not specified on
10534 the command line, or will be set otherwise if one was. */
10535
10536 if (mips_arch != CPU_UNKNOWN && mips_opts.isa != ISA_UNKNOWN)
10537 {
10538 /* We have to check if the isa is the default isa of arch. Otherwise
10539 we'll get invalid object file headers. */
10540 ci = mips_cpu_info_from_cpu (mips_arch);
10541 assert (ci != NULL);
10542 if (mips_opts.isa != ci->isa)
10543 {
10544 /* This really should be an error instead of a warning, but old
10545 compilers only have -mcpu which sets both arch and tune. For
10546 now, we discard arch and preserve tune. */
10547 as_warn (_("The -march option is incompatible to -mipsN and "
10548 "therefore ignored."));
10549 if (mips_tune == CPU_UNKNOWN)
10550 mips_tune = mips_arch;
10551 ci = mips_cpu_info_from_isa (mips_opts.isa);
10552 assert (ci != NULL);
10553 mips_arch = ci->cpu;
10554 }
10555 }
10556#endif
10557 else if (mips_arch != CPU_UNKNOWN && mips_opts.isa == ISA_UNKNOWN)
10558 {
10559 /* We have ARCH, we need ISA. */
10560 ci = mips_cpu_info_from_cpu (mips_arch);
10561 assert (ci != NULL);
10562 mips_opts.isa = ci->isa;
10563 }
10564 else if (mips_arch == CPU_UNKNOWN && mips_opts.isa != ISA_UNKNOWN)
10565 {
10566 /* We have ISA, we need default ARCH. */
10567 ci = mips_cpu_info_from_isa (mips_opts.isa);
10568 assert (ci != NULL);
10569 mips_arch = ci->cpu;
10570 }
10571 else
10572 {
10573 /* We need to set both ISA and ARCH from target cpu. */
10574 ci = mips_cpu_info_from_name (cpu);
10575 if (ci == NULL)
10576 ci = mips_cpu_info_from_cpu (CPU_R3000);
10577 assert (ci != NULL);
10578 mips_opts.isa = ci->isa;
10579 mips_arch = ci->cpu;
10580 }
10581
10582 if (mips_tune == CPU_UNKNOWN)
10583 mips_tune = mips_arch;
10584
10585 ci = mips_cpu_info_from_cpu (mips_arch);
10586 assert (ci != NULL);
10587 mips_isa_from_cpu = ci->isa;
10588
10589 /* End of TARGET_CPU processing, get rid of malloced memory
10590 if necessary. */
10591 cpu = NULL;
10592 if (a != NULL)
10593 {
10594 free (a);
10595 a = NULL;
10596 }
10597
10598 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
10599 as_bad (_("trap exception not supported at ISA 1"));
10600
10601 /* Set the EABI kind based on the ISA before the user gets
10602 to change the ISA with directives. This isn't really
10603 the best, but then neither is basing the abi on the isa. */
10604 if (ISA_HAS_64BIT_REGS (mips_opts.isa)
10605 && mips_opts.abi == EABI_ABI)
10606 mips_eabi64 = 1;
10607
10608 /* If they asked for mips1 or mips2 and a cpu that is
10609 mips3 or greater, then mark the object file 32BITMODE. */
10610 if (mips_isa_from_cpu != ISA_UNKNOWN
10611 && ! ISA_HAS_64BIT_REGS (mips_opts.isa)
10612 && ISA_HAS_64BIT_REGS (mips_isa_from_cpu))
10613 mips_32bitmode = 1;
10614
10615 /* If the selected architecture includes support for ASEs, enable
10616 generation of code for them. */
10617 if (mips_opts.ase_mips3d == -1 && CPU_HAS_MIPS3D (mips_arch))
10618 mips_opts.ase_mips3d = 1;
10619 if (mips_opts.ase_mdmx == -1 && CPU_HAS_MDMX (mips_arch))
10620 mips_opts.ase_mdmx = 1;
10621
10622 if (file_mips_gp32 < 0)
10623 file_mips_gp32 = 0;
10624 if (file_mips_fp32 < 0)
10625 file_mips_fp32 = 0;
10626
10627 file_mips_isa = mips_opts.isa;
10628 file_mips_abi = mips_opts.abi;
10629 file_ase_mips3d = mips_opts.ase_mips3d;
10630 file_ase_mdmx = mips_opts.ase_mdmx;
10631 mips_opts.gp32 = file_mips_gp32;
10632 mips_opts.fp32 = file_mips_fp32;
10633
10634 if (HAVE_NEWABI)
10635 mips_big_got = 1;
10636}
10637\f
10638void
252b5132
RH
10639mips_init_after_args ()
10640{
10641 /* initialize opcodes */
10642 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
beae10d5 10643 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
252b5132
RH
10644}
10645
10646long
10647md_pcrel_from (fixP)
10648 fixS *fixP;
10649{
10650 if (OUTPUT_FLAVOR != bfd_target_aout_flavour
10651 && fixP->fx_addsy != (symbolS *) NULL
10652 && ! S_IS_DEFINED (fixP->fx_addsy))
10653 {
6478892d
TS
10654 /* This makes a branch to an undefined symbol be a branch to the
10655 current location. */
cb56d3d3 10656 if (mips_pic == EMBEDDED_PIC)
6478892d 10657 return 4;
cb56d3d3 10658 else
6478892d 10659 return 1;
252b5132
RH
10660 }
10661
c9914766 10662 /* Return the address of the delay slot. */
252b5132
RH
10663 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
10664}
10665
252b5132
RH
10666/* This is called before the symbol table is processed. In order to
10667 work with gcc when using mips-tfile, we must keep all local labels.
10668 However, in other cases, we want to discard them. If we were
10669 called with -g, but we didn't see any debugging information, it may
10670 mean that gcc is smuggling debugging information through to
10671 mips-tfile, in which case we must generate all local labels. */
10672
10673void
10674mips_frob_file_before_adjust ()
10675{
10676#ifndef NO_ECOFF_DEBUGGING
10677 if (ECOFF_DEBUGGING
10678 && mips_debug != 0
10679 && ! ecoff_debugging_seen)
10680 flag_keep_locals = 1;
10681#endif
10682}
10683
10684/* Sort any unmatched HI16_S relocs so that they immediately precede
94f592af 10685 the corresponding LO reloc. This is called before md_apply_fix3 and
252b5132
RH
10686 tc_gen_reloc. Unmatched HI16_S relocs can only be generated by
10687 explicit use of the %hi modifier. */
10688
10689void
10690mips_frob_file ()
10691{
10692 struct mips_hi_fixup *l;
10693
10694 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
10695 {
10696 segment_info_type *seginfo;
10697 int pass;
10698
10699 assert (l->fixp->fx_r_type == BFD_RELOC_HI16_S);
10700
10701 /* Check quickly whether the next fixup happens to be a matching
10702 %lo. */
10703 if (l->fixp->fx_next != NULL
10704 && l->fixp->fx_next->fx_r_type == BFD_RELOC_LO16
10705 && l->fixp->fx_addsy == l->fixp->fx_next->fx_addsy
10706 && l->fixp->fx_offset == l->fixp->fx_next->fx_offset)
10707 continue;
10708
10709 /* Look through the fixups for this segment for a matching %lo.
10710 When we find one, move the %hi just in front of it. We do
10711 this in two passes. In the first pass, we try to find a
10712 unique %lo. In the second pass, we permit multiple %hi
10713 relocs for a single %lo (this is a GNU extension). */
10714 seginfo = seg_info (l->seg);
10715 for (pass = 0; pass < 2; pass++)
10716 {
10717 fixS *f, *prev;
10718
10719 prev = NULL;
10720 for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
10721 {
10722 /* Check whether this is a %lo fixup which matches l->fixp. */
10723 if (f->fx_r_type == BFD_RELOC_LO16
10724 && f->fx_addsy == l->fixp->fx_addsy
10725 && f->fx_offset == l->fixp->fx_offset
10726 && (pass == 1
10727 || prev == NULL
10728 || prev->fx_r_type != BFD_RELOC_HI16_S
10729 || prev->fx_addsy != f->fx_addsy
10730 || prev->fx_offset != f->fx_offset))
10731 {
10732 fixS **pf;
10733
10734 /* Move l->fixp before f. */
10735 for (pf = &seginfo->fix_root;
10736 *pf != l->fixp;
10737 pf = &(*pf)->fx_next)
10738 assert (*pf != NULL);
10739
10740 *pf = l->fixp->fx_next;
10741
10742 l->fixp->fx_next = f;
10743 if (prev == NULL)
10744 seginfo->fix_root = l->fixp;
10745 else
10746 prev->fx_next = l->fixp;
10747
10748 break;
10749 }
10750
10751 prev = f;
10752 }
10753
10754 if (f != NULL)
10755 break;
10756
10757#if 0 /* GCC code motion plus incomplete dead code elimination
10758 can leave a %hi without a %lo. */
10759 if (pass == 1)
10760 as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
10761 _("Unmatched %%hi reloc"));
10762#endif
10763 }
10764 }
10765}
10766
10767/* When generating embedded PIC code we need to use a special
10768 relocation to represent the difference of two symbols in the .text
10769 section (switch tables use a difference of this sort). See
10770 include/coff/mips.h for details. This macro checks whether this
10771 fixup requires the special reloc. */
10772#define SWITCH_TABLE(fixp) \
10773 ((fixp)->fx_r_type == BFD_RELOC_32 \
bb2d6cd7 10774 && OUTPUT_FLAVOR != bfd_target_elf_flavour \
252b5132
RH
10775 && (fixp)->fx_addsy != NULL \
10776 && (fixp)->fx_subsy != NULL \
10777 && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
10778 && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
10779
10780/* When generating embedded PIC code we must keep all PC relative
10781 relocations, in case the linker has to relax a call. We also need
f6688943
TS
10782 to keep relocations for switch table entries.
10783
10784 We may have combined relocations without symbols in the N32/N64 ABI.
10785 We have to prevent gas from dropping them. */
252b5132 10786
252b5132
RH
10787int
10788mips_force_relocation (fixp)
10789 fixS *fixp;
10790{
10791 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
10792 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
10793 return 1;
10794
f6688943
TS
10795 if (HAVE_NEWABI
10796 && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
10797 && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
10798 || fixp->fx_r_type == BFD_RELOC_HI16_S
10799 || fixp->fx_r_type == BFD_RELOC_LO16))
10800 return 1;
10801
252b5132
RH
10802 return (mips_pic == EMBEDDED_PIC
10803 && (fixp->fx_pcrel
10804 || SWITCH_TABLE (fixp)
10805 || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
10806 || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
10807}
10808
add55e1f
RS
10809#ifdef OBJ_ELF
10810static int
10811mips_need_elf_addend_fixup (fixP)
10812 fixS *fixP;
10813{
2d2bf3e0
CD
10814 if (S_GET_OTHER (fixP->fx_addsy) == STO_MIPS16)
10815 return 1;
b25a253c
CD
10816 if (mips_pic == EMBEDDED_PIC
10817 && S_IS_WEAK (fixP->fx_addsy))
10818 return 1;
10819 if (mips_pic != EMBEDDED_PIC
10820 && (S_IS_WEAK (fixP->fx_addsy)
10821 || S_IS_EXTERN (fixP->fx_addsy))
2d2bf3e0
CD
10822 && !S_IS_COMMON (fixP->fx_addsy))
10823 return 1;
10824 if (symbol_used_in_reloc_p (fixP->fx_addsy)
10825 && (((bfd_get_section_flags (stdoutput,
10826 S_GET_SEGMENT (fixP->fx_addsy))
10827 & SEC_LINK_ONCE) != 0)
10828 || !strncmp (segment_name (S_GET_SEGMENT (fixP->fx_addsy)),
10829 ".gnu.linkonce",
10830 sizeof (".gnu.linkonce") - 1)))
10831 return 1;
10832 return 0;
add55e1f
RS
10833}
10834#endif
10835
252b5132
RH
10836/* Apply a fixup to the object file. */
10837
94f592af
NC
10838void
10839md_apply_fix3 (fixP, valP, seg)
252b5132 10840 fixS *fixP;
98d3f06f 10841 valueT *valP;
94f592af 10842 segT seg ATTRIBUTE_UNUSED;
252b5132 10843{
874e8986 10844 bfd_byte *buf;
98aa84af
AM
10845 long insn;
10846 valueT value;
252b5132
RH
10847
10848 assert (fixP->fx_size == 4
10849 || fixP->fx_r_type == BFD_RELOC_16
f6688943
TS
10850 || fixP->fx_r_type == BFD_RELOC_32
10851 || fixP->fx_r_type == BFD_RELOC_MIPS_JMP
10852 || fixP->fx_r_type == BFD_RELOC_HI16_S
10853 || fixP->fx_r_type == BFD_RELOC_LO16
10854 || fixP->fx_r_type == BFD_RELOC_GPREL16
76b3015f 10855 || fixP->fx_r_type == BFD_RELOC_MIPS_LITERAL
f6688943 10856 || fixP->fx_r_type == BFD_RELOC_GPREL32
252b5132 10857 || fixP->fx_r_type == BFD_RELOC_64
f6688943
TS
10858 || fixP->fx_r_type == BFD_RELOC_CTOR
10859 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
10860 || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHEST
10861 || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHER
10862 || fixP->fx_r_type == BFD_RELOC_MIPS_SCN_DISP
10863 || fixP->fx_r_type == BFD_RELOC_MIPS_REL16
10864 || fixP->fx_r_type == BFD_RELOC_MIPS_RELGOT
252b5132 10865 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
438c16b8
TS
10866 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
10867 || fixP->fx_r_type == BFD_RELOC_MIPS_JALR);
252b5132 10868
98d3f06f 10869 value = *valP;
252b5132
RH
10870
10871 /* If we aren't adjusting this fixup to be against the section
10872 symbol, we need to adjust the value. */
10873#ifdef OBJ_ELF
10874 if (fixP->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour)
bb2d6cd7 10875 {
add55e1f 10876 if (mips_need_elf_addend_fixup (fixP))
98aa84af
AM
10877 {
10878 valueT symval = S_GET_VALUE (fixP->fx_addsy);
94f592af 10879
98aa84af 10880 value -= symval;
add55e1f 10881 if (value != 0 && ! fixP->fx_pcrel)
98aa84af
AM
10882 {
10883 /* In this case, the bfd_install_relocation routine will
10884 incorrectly add the symbol value back in. We just want
7461da6e 10885 the addend to appear in the object file. */
98aa84af 10886 value -= symval;
7461da6e
RS
10887
10888 /* Make sure the addend is still non-zero. If it became zero
10889 after the last operation, set it to a spurious value and
10890 subtract the same value from the object file's contents. */
10891 if (value == 0)
10892 {
10893 value = 8;
10894
10895 /* The in-place addends for LO16 relocations are signed;
10896 leave the matching HI16 in-place addends as zero. */
10897 if (fixP->fx_r_type != BFD_RELOC_HI16_S)
10898 {
10899 reloc_howto_type *howto;
10900 bfd_vma contents, mask, field;
10901
10902 howto = bfd_reloc_type_lookup (stdoutput,
10903 fixP->fx_r_type);
10904
10905 contents = bfd_get_bits (fixP->fx_frag->fr_literal
10906 + fixP->fx_where,
10907 fixP->fx_size * 8,
10908 target_big_endian);
10909
10910 /* MASK has bits set where the relocation should go.
10911 FIELD is -value, shifted into the appropriate place
10912 for this relocation. */
10913 mask = 1 << (howto->bitsize - 1);
10914 mask = (((mask - 1) << 1) | 1) << howto->bitpos;
10915 field = (-value >> howto->rightshift) << howto->bitpos;
10916
10917 bfd_put_bits ((field & mask) | (contents & ~mask),
10918 fixP->fx_frag->fr_literal + fixP->fx_where,
10919 fixP->fx_size * 8,
10920 target_big_endian);
10921 }
10922 }
98aa84af
AM
10923 }
10924 }
252b5132 10925
bb2d6cd7
GK
10926 /* This code was generated using trial and error and so is
10927 fragile and not trustworthy. If you change it, you should
10928 rerun the elf-rel, elf-rel2, and empic testcases and ensure
10929 they still pass. */
10930 if (fixP->fx_pcrel || fixP->fx_subsy != NULL)
10931 {
10932 value += fixP->fx_frag->fr_address + fixP->fx_where;
10933
10934 /* BFD's REL handling, for MIPS, is _very_ weird.
10935 This gives the right results, but it can't possibly
10936 be the way things are supposed to work. */
cb56d3d3
TS
10937 if ((fixP->fx_r_type != BFD_RELOC_16_PCREL
10938 && fixP->fx_r_type != BFD_RELOC_16_PCREL_S2)
bb2d6cd7
GK
10939 || S_GET_SEGMENT (fixP->fx_addsy) != undefined_section)
10940 value += fixP->fx_frag->fr_address + fixP->fx_where;
10941 }
10942 }
10943#endif
252b5132 10944
94f592af 10945 fixP->fx_addnumber = value; /* Remember value for tc_gen_reloc. */
252b5132
RH
10946
10947 if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel)
10948 fixP->fx_done = 1;
10949
10950 switch (fixP->fx_r_type)
10951 {
10952 case BFD_RELOC_MIPS_JMP:
e369bcce
TS
10953 case BFD_RELOC_MIPS_SHIFT5:
10954 case BFD_RELOC_MIPS_SHIFT6:
10955 case BFD_RELOC_MIPS_GOT_DISP:
10956 case BFD_RELOC_MIPS_GOT_PAGE:
10957 case BFD_RELOC_MIPS_GOT_OFST:
10958 case BFD_RELOC_MIPS_SUB:
10959 case BFD_RELOC_MIPS_INSERT_A:
10960 case BFD_RELOC_MIPS_INSERT_B:
10961 case BFD_RELOC_MIPS_DELETE:
10962 case BFD_RELOC_MIPS_HIGHEST:
10963 case BFD_RELOC_MIPS_HIGHER:
10964 case BFD_RELOC_MIPS_SCN_DISP:
10965 case BFD_RELOC_MIPS_REL16:
10966 case BFD_RELOC_MIPS_RELGOT:
10967 case BFD_RELOC_MIPS_JALR:
252b5132
RH
10968 case BFD_RELOC_HI16:
10969 case BFD_RELOC_HI16_S:
cdf6fd85 10970 case BFD_RELOC_GPREL16:
252b5132
RH
10971 case BFD_RELOC_MIPS_LITERAL:
10972 case BFD_RELOC_MIPS_CALL16:
10973 case BFD_RELOC_MIPS_GOT16:
cdf6fd85 10974 case BFD_RELOC_GPREL32:
252b5132
RH
10975 case BFD_RELOC_MIPS_GOT_HI16:
10976 case BFD_RELOC_MIPS_GOT_LO16:
10977 case BFD_RELOC_MIPS_CALL_HI16:
10978 case BFD_RELOC_MIPS_CALL_LO16:
10979 case BFD_RELOC_MIPS16_GPREL:
10980 if (fixP->fx_pcrel)
10981 as_bad_where (fixP->fx_file, fixP->fx_line,
10982 _("Invalid PC relative reloc"));
10983 /* Nothing needed to do. The value comes from the reloc entry */
10984 break;
10985
10986 case BFD_RELOC_MIPS16_JMP:
10987 /* We currently always generate a reloc against a symbol, which
10988 means that we don't want an addend even if the symbol is
10989 defined. */
10990 fixP->fx_addnumber = 0;
10991 break;
10992
10993 case BFD_RELOC_PCREL_HI16_S:
10994 /* The addend for this is tricky if it is internal, so we just
10995 do everything here rather than in bfd_install_relocation. */
bdaaa2e1 10996 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
bb2d6cd7
GK
10997 && !fixP->fx_done
10998 && value != 0)
10999 break;
11000 if (fixP->fx_addsy
11001 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
252b5132
RH
11002 {
11003 /* For an external symbol adjust by the address to make it
11004 pcrel_offset. We use the address of the RELLO reloc
11005 which follows this one. */
11006 value += (fixP->fx_next->fx_frag->fr_address
11007 + fixP->fx_next->fx_where);
11008 }
e7d556df 11009 value = ((value + 0x8000) >> 16) & 0xffff;
874e8986 11010 buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
252b5132
RH
11011 if (target_big_endian)
11012 buf += 2;
874e8986 11013 md_number_to_chars ((char *) buf, value, 2);
252b5132
RH
11014 break;
11015
11016 case BFD_RELOC_PCREL_LO16:
11017 /* The addend for this is tricky if it is internal, so we just
11018 do everything here rather than in bfd_install_relocation. */
bdaaa2e1 11019 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
bb2d6cd7
GK
11020 && !fixP->fx_done
11021 && value != 0)
11022 break;
11023 if (fixP->fx_addsy
11024 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
252b5132 11025 value += fixP->fx_frag->fr_address + fixP->fx_where;
874e8986 11026 buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
252b5132
RH
11027 if (target_big_endian)
11028 buf += 2;
874e8986 11029 md_number_to_chars ((char *) buf, value, 2);
252b5132
RH
11030 break;
11031
11032 case BFD_RELOC_64:
11033 /* This is handled like BFD_RELOC_32, but we output a sign
11034 extended value if we are only 32 bits. */
11035 if (fixP->fx_done
11036 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11037 {
11038 if (8 <= sizeof (valueT))
11039 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11040 value, 8);
11041 else
11042 {
11043 long w1, w2;
11044 long hiv;
11045
11046 w1 = w2 = fixP->fx_where;
11047 if (target_big_endian)
11048 w1 += 4;
11049 else
11050 w2 += 4;
11051 md_number_to_chars (fixP->fx_frag->fr_literal + w1, value, 4);
11052 if ((value & 0x80000000) != 0)
11053 hiv = 0xffffffff;
11054 else
11055 hiv = 0;
11056 md_number_to_chars (fixP->fx_frag->fr_literal + w2, hiv, 4);
11057 }
11058 }
11059 break;
11060
056350c6 11061 case BFD_RELOC_RVA:
252b5132
RH
11062 case BFD_RELOC_32:
11063 /* If we are deleting this reloc entry, we must fill in the
11064 value now. This can happen if we have a .word which is not
11065 resolved when it appears but is later defined. We also need
11066 to fill in the value if this is an embedded PIC switch table
11067 entry. */
11068 if (fixP->fx_done
11069 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
11070 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11071 value, 4);
11072 break;
11073
11074 case BFD_RELOC_16:
11075 /* If we are deleting this reloc entry, we must fill in the
11076 value now. */
11077 assert (fixP->fx_size == 2);
11078 if (fixP->fx_done)
11079 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
11080 value, 2);
11081 break;
11082
11083 case BFD_RELOC_LO16:
11084 /* When handling an embedded PIC switch statement, we can wind
11085 up deleting a LO16 reloc. See the 'o' case in mips_ip. */
11086 if (fixP->fx_done)
11087 {
98aa84af 11088 if (value + 0x8000 > 0xffff)
252b5132
RH
11089 as_bad_where (fixP->fx_file, fixP->fx_line,
11090 _("relocation overflow"));
874e8986 11091 buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
252b5132
RH
11092 if (target_big_endian)
11093 buf += 2;
874e8986 11094 md_number_to_chars ((char *) buf, value, 2);
252b5132
RH
11095 }
11096 break;
11097
11098 case BFD_RELOC_16_PCREL_S2:
cb56d3d3
TS
11099 if ((value & 0x3) != 0)
11100 as_bad_where (fixP->fx_file, fixP->fx_line,
11101 _("Branch to odd address (%lx)"), (long) value);
11102
11103 /* Fall through. */
11104
11105 case BFD_RELOC_16_PCREL:
252b5132
RH
11106 /*
11107 * We need to save the bits in the instruction since fixup_segment()
11108 * might be deleting the relocation entry (i.e., a branch within
11109 * the current segment).
11110 */
bb2d6cd7
GK
11111 if (!fixP->fx_done && value != 0)
11112 break;
11113 /* If 'value' is zero, the remaining reloc code won't actually
11114 do the store, so it must be done here. This is probably
11115 a bug somewhere. */
b25a253c
CD
11116 if (!fixP->fx_done
11117 && (fixP->fx_r_type != BFD_RELOC_16_PCREL_S2
11118 || fixP->fx_addsy == NULL /* ??? */
11119 || ! S_IS_DEFINED (fixP->fx_addsy)))
bb2d6cd7 11120 value -= fixP->fx_frag->fr_address + fixP->fx_where;
bdaaa2e1 11121
98aa84af 11122 value = (offsetT) value >> 2;
252b5132
RH
11123
11124 /* update old instruction data */
874e8986 11125 buf = (bfd_byte *) (fixP->fx_where + fixP->fx_frag->fr_literal);
252b5132
RH
11126 if (target_big_endian)
11127 insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
11128 else
11129 insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
11130
98aa84af 11131 if (value + 0x8000 <= 0xffff)
252b5132
RH
11132 insn |= value & 0xffff;
11133 else
11134 {
11135 /* The branch offset is too large. If this is an
11136 unconditional branch, and we are not generating PIC code,
11137 we can convert it to an absolute jump instruction. */
11138 if (mips_pic == NO_PIC
11139 && fixP->fx_done
11140 && fixP->fx_frag->fr_address >= text_section->vma
11141 && (fixP->fx_frag->fr_address
11142 < text_section->vma + text_section->_raw_size)
11143 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
11144 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
11145 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
11146 {
11147 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
11148 insn = 0x0c000000; /* jal */
11149 else
11150 insn = 0x08000000; /* j */
11151 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
11152 fixP->fx_done = 0;
11153 fixP->fx_addsy = section_symbol (text_section);
11154 fixP->fx_addnumber = (value << 2) + md_pcrel_from (fixP);
11155 }
11156 else
11157 {
11158 /* FIXME. It would be possible in principle to handle
11159 conditional branches which overflow. They could be
11160 transformed into a branch around a jump. This would
11161 require setting up variant frags for each different
11162 branch type. The native MIPS assembler attempts to
11163 handle these cases, but it appears to do it
11164 incorrectly. */
11165 as_bad_where (fixP->fx_file, fixP->fx_line,
11166 _("Branch out of range"));
11167 }
11168 }
11169
11170 md_number_to_chars ((char *) buf, (valueT) insn, 4);
11171 break;
11172
11173 case BFD_RELOC_VTABLE_INHERIT:
11174 fixP->fx_done = 0;
11175 if (fixP->fx_addsy
11176 && !S_IS_DEFINED (fixP->fx_addsy)
11177 && !S_IS_WEAK (fixP->fx_addsy))
11178 S_SET_WEAK (fixP->fx_addsy);
11179 break;
11180
11181 case BFD_RELOC_VTABLE_ENTRY:
11182 fixP->fx_done = 0;
11183 break;
11184
11185 default:
11186 internalError ();
11187 }
252b5132
RH
11188}
11189
11190#if 0
11191void
11192printInsn (oc)
11193 unsigned long oc;
11194{
11195 const struct mips_opcode *p;
11196 int treg, sreg, dreg, shamt;
11197 short imm;
11198 const char *args;
11199 int i;
11200
11201 for (i = 0; i < NUMOPCODES; ++i)
11202 {
11203 p = &mips_opcodes[i];
11204 if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
11205 {
11206 printf ("%08lx %s\t", oc, p->name);
11207 treg = (oc >> 16) & 0x1f;
11208 sreg = (oc >> 21) & 0x1f;
11209 dreg = (oc >> 11) & 0x1f;
11210 shamt = (oc >> 6) & 0x1f;
11211 imm = oc;
11212 for (args = p->args;; ++args)
11213 {
11214 switch (*args)
11215 {
11216 case '\0':
11217 printf ("\n");
11218 break;
11219
11220 case ',':
11221 case '(':
11222 case ')':
11223 printf ("%c", *args);
11224 continue;
11225
11226 case 'r':
11227 assert (treg == sreg);
11228 printf ("$%d,$%d", treg, sreg);
11229 continue;
11230
11231 case 'd':
11232 case 'G':
11233 printf ("$%d", dreg);
11234 continue;
11235
11236 case 't':
11237 case 'E':
11238 printf ("$%d", treg);
11239 continue;
11240
11241 case 'k':
11242 printf ("0x%x", treg);
11243 continue;
11244
11245 case 'b':
11246 case 's':
11247 printf ("$%d", sreg);
11248 continue;
11249
11250 case 'a':
11251 printf ("0x%08lx", oc & 0x1ffffff);
11252 continue;
11253
11254 case 'i':
11255 case 'j':
11256 case 'o':
11257 case 'u':
11258 printf ("%d", imm);
11259 continue;
11260
11261 case '<':
11262 case '>':
11263 printf ("$%d", shamt);
11264 continue;
11265
11266 default:
11267 internalError ();
11268 }
11269 break;
11270 }
11271 return;
11272 }
11273 }
11274 printf (_("%08lx UNDEFINED\n"), oc);
11275}
11276#endif
11277
11278static symbolS *
11279get_symbol ()
11280{
11281 int c;
11282 char *name;
11283 symbolS *p;
11284
11285 name = input_line_pointer;
11286 c = get_symbol_end ();
11287 p = (symbolS *) symbol_find_or_make (name);
11288 *input_line_pointer = c;
11289 return p;
11290}
11291
11292/* Align the current frag to a given power of two. The MIPS assembler
11293 also automatically adjusts any preceding label. */
11294
11295static void
11296mips_align (to, fill, label)
11297 int to;
11298 int fill;
11299 symbolS *label;
11300{
11301 mips_emit_delays (false);
11302 frag_align (to, fill, 0);
11303 record_alignment (now_seg, to);
11304 if (label != NULL)
11305 {
11306 assert (S_GET_SEGMENT (label) == now_seg);
49309057 11307 symbol_set_frag (label, frag_now);
252b5132
RH
11308 S_SET_VALUE (label, (valueT) frag_now_fix ());
11309 }
11310}
11311
11312/* Align to a given power of two. .align 0 turns off the automatic
11313 alignment used by the data creating pseudo-ops. */
11314
11315static void
11316s_align (x)
43841e91 11317 int x ATTRIBUTE_UNUSED;
252b5132
RH
11318{
11319 register int temp;
11320 register long temp_fill;
11321 long max_alignment = 15;
11322
11323 /*
11324
11325 o Note that the assembler pulls down any immediately preceeding label
11326 to the aligned address.
11327 o It's not documented but auto alignment is reinstated by
11328 a .align pseudo instruction.
11329 o Note also that after auto alignment is turned off the mips assembler
11330 issues an error on attempt to assemble an improperly aligned data item.
11331 We don't.
11332
11333 */
11334
11335 temp = get_absolute_expression ();
11336 if (temp > max_alignment)
11337 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
11338 else if (temp < 0)
11339 {
11340 as_warn (_("Alignment negative: 0 assumed."));
11341 temp = 0;
11342 }
11343 if (*input_line_pointer == ',')
11344 {
f9419b05 11345 ++input_line_pointer;
252b5132
RH
11346 temp_fill = get_absolute_expression ();
11347 }
11348 else
11349 temp_fill = 0;
11350 if (temp)
11351 {
11352 auto_align = 1;
11353 mips_align (temp, (int) temp_fill,
11354 insn_labels != NULL ? insn_labels->label : NULL);
11355 }
11356 else
11357 {
11358 auto_align = 0;
11359 }
11360
11361 demand_empty_rest_of_line ();
11362}
11363
11364void
11365mips_flush_pending_output ()
11366{
11367 mips_emit_delays (false);
11368 mips_clear_insn_labels ();
11369}
11370
11371static void
11372s_change_sec (sec)
11373 int sec;
11374{
11375 segT seg;
11376
11377 /* When generating embedded PIC code, we only use the .text, .lit8,
11378 .sdata and .sbss sections. We change the .data and .rdata
11379 pseudo-ops to use .sdata. */
11380 if (mips_pic == EMBEDDED_PIC
11381 && (sec == 'd' || sec == 'r'))
11382 sec = 's';
11383
11384#ifdef OBJ_ELF
11385 /* The ELF backend needs to know that we are changing sections, so
11386 that .previous works correctly. We could do something like check
b6ff326e 11387 for an obj_section_change_hook macro, but that might be confusing
252b5132
RH
11388 as it would not be appropriate to use it in the section changing
11389 functions in read.c, since obj-elf.c intercepts those. FIXME:
11390 This should be cleaner, somehow. */
11391 obj_elf_section_change_hook ();
11392#endif
11393
11394 mips_emit_delays (false);
11395 switch (sec)
11396 {
11397 case 't':
11398 s_text (0);
11399 break;
11400 case 'd':
11401 s_data (0);
11402 break;
11403 case 'b':
11404 subseg_set (bss_section, (subsegT) get_absolute_expression ());
11405 demand_empty_rest_of_line ();
11406 break;
11407
11408 case 'r':
11409 if (USE_GLOBAL_POINTER_OPT)
11410 {
11411 seg = subseg_new (RDATA_SECTION_NAME,
11412 (subsegT) get_absolute_expression ());
11413 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11414 {
11415 bfd_set_section_flags (stdoutput, seg,
11416 (SEC_ALLOC
11417 | SEC_LOAD
11418 | SEC_READONLY
11419 | SEC_RELOC
11420 | SEC_DATA));
11421 if (strcmp (TARGET_OS, "elf") != 0)
e799a695 11422 record_alignment (seg, 4);
252b5132
RH
11423 }
11424 demand_empty_rest_of_line ();
11425 }
11426 else
11427 {
11428 as_bad (_("No read only data section in this object file format"));
11429 demand_empty_rest_of_line ();
11430 return;
11431 }
11432 break;
11433
11434 case 's':
11435 if (USE_GLOBAL_POINTER_OPT)
11436 {
11437 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
11438 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11439 {
11440 bfd_set_section_flags (stdoutput, seg,
11441 SEC_ALLOC | SEC_LOAD | SEC_RELOC
11442 | SEC_DATA);
11443 if (strcmp (TARGET_OS, "elf") != 0)
e799a695 11444 record_alignment (seg, 4);
252b5132
RH
11445 }
11446 demand_empty_rest_of_line ();
11447 break;
11448 }
11449 else
11450 {
11451 as_bad (_("Global pointers not supported; recompile -G 0"));
11452 demand_empty_rest_of_line ();
11453 return;
11454 }
11455 }
11456
11457 auto_align = 1;
11458}
11459
11460void
11461mips_enable_auto_align ()
11462{
11463 auto_align = 1;
11464}
11465
11466static void
11467s_cons (log_size)
11468 int log_size;
11469{
11470 symbolS *label;
11471
11472 label = insn_labels != NULL ? insn_labels->label : NULL;
11473 mips_emit_delays (false);
11474 if (log_size > 0 && auto_align)
11475 mips_align (log_size, 0, label);
11476 mips_clear_insn_labels ();
11477 cons (1 << log_size);
11478}
11479
11480static void
11481s_float_cons (type)
11482 int type;
11483{
11484 symbolS *label;
11485
11486 label = insn_labels != NULL ? insn_labels->label : NULL;
11487
11488 mips_emit_delays (false);
11489
11490 if (auto_align)
49309057
ILT
11491 {
11492 if (type == 'd')
11493 mips_align (3, 0, label);
11494 else
11495 mips_align (2, 0, label);
11496 }
252b5132
RH
11497
11498 mips_clear_insn_labels ();
11499
11500 float_cons (type);
11501}
11502
11503/* Handle .globl. We need to override it because on Irix 5 you are
11504 permitted to say
11505 .globl foo .text
11506 where foo is an undefined symbol, to mean that foo should be
11507 considered to be the address of a function. */
11508
11509static void
11510s_mips_globl (x)
43841e91 11511 int x ATTRIBUTE_UNUSED;
252b5132
RH
11512{
11513 char *name;
11514 int c;
11515 symbolS *symbolP;
11516 flagword flag;
11517
11518 name = input_line_pointer;
11519 c = get_symbol_end ();
11520 symbolP = symbol_find_or_make (name);
11521 *input_line_pointer = c;
11522 SKIP_WHITESPACE ();
11523
11524 /* On Irix 5, every global symbol that is not explicitly labelled as
11525 being a function is apparently labelled as being an object. */
11526 flag = BSF_OBJECT;
11527
11528 if (! is_end_of_line[(unsigned char) *input_line_pointer])
11529 {
11530 char *secname;
11531 asection *sec;
11532
11533 secname = input_line_pointer;
11534 c = get_symbol_end ();
11535 sec = bfd_get_section_by_name (stdoutput, secname);
11536 if (sec == NULL)
11537 as_bad (_("%s: no such section"), secname);
11538 *input_line_pointer = c;
11539
11540 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
11541 flag = BSF_FUNCTION;
11542 }
11543
49309057 11544 symbol_get_bfdsym (symbolP)->flags |= flag;
252b5132
RH
11545
11546 S_SET_EXTERNAL (symbolP);
11547 demand_empty_rest_of_line ();
11548}
11549
11550static void
11551s_option (x)
43841e91 11552 int x ATTRIBUTE_UNUSED;
252b5132
RH
11553{
11554 char *opt;
11555 char c;
11556
11557 opt = input_line_pointer;
11558 c = get_symbol_end ();
11559
11560 if (*opt == 'O')
11561 {
11562 /* FIXME: What does this mean? */
11563 }
11564 else if (strncmp (opt, "pic", 3) == 0)
11565 {
11566 int i;
11567
11568 i = atoi (opt + 3);
11569 if (i == 0)
11570 mips_pic = NO_PIC;
11571 else if (i == 2)
11572 mips_pic = SVR4_PIC;
11573 else
11574 as_bad (_(".option pic%d not supported"), i);
11575
11576 if (USE_GLOBAL_POINTER_OPT && mips_pic == SVR4_PIC)
11577 {
11578 if (g_switch_seen && g_switch_value != 0)
11579 as_warn (_("-G may not be used with SVR4 PIC code"));
11580 g_switch_value = 0;
11581 bfd_set_gp_size (stdoutput, 0);
11582 }
11583 }
11584 else
11585 as_warn (_("Unrecognized option \"%s\""), opt);
11586
11587 *input_line_pointer = c;
11588 demand_empty_rest_of_line ();
11589}
11590
11591/* This structure is used to hold a stack of .set values. */
11592
e972090a
NC
11593struct mips_option_stack
11594{
252b5132
RH
11595 struct mips_option_stack *next;
11596 struct mips_set_options options;
11597};
11598
11599static struct mips_option_stack *mips_opts_stack;
11600
11601/* Handle the .set pseudo-op. */
11602
11603static void
11604s_mipsset (x)
43841e91 11605 int x ATTRIBUTE_UNUSED;
252b5132
RH
11606{
11607 char *name = input_line_pointer, ch;
11608
11609 while (!is_end_of_line[(unsigned char) *input_line_pointer])
f9419b05 11610 ++input_line_pointer;
252b5132
RH
11611 ch = *input_line_pointer;
11612 *input_line_pointer = '\0';
11613
11614 if (strcmp (name, "reorder") == 0)
11615 {
11616 if (mips_opts.noreorder && prev_nop_frag != NULL)
11617 {
11618 /* If we still have pending nops, we can discard them. The
11619 usual nop handling will insert any that are still
bdaaa2e1 11620 needed. */
252b5132
RH
11621 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11622 * (mips_opts.mips16 ? 2 : 4));
11623 prev_nop_frag = NULL;
11624 }
11625 mips_opts.noreorder = 0;
11626 }
11627 else if (strcmp (name, "noreorder") == 0)
11628 {
11629 mips_emit_delays (true);
11630 mips_opts.noreorder = 1;
11631 mips_any_noreorder = 1;
11632 }
11633 else if (strcmp (name, "at") == 0)
11634 {
11635 mips_opts.noat = 0;
11636 }
11637 else if (strcmp (name, "noat") == 0)
11638 {
11639 mips_opts.noat = 1;
11640 }
11641 else if (strcmp (name, "macro") == 0)
11642 {
11643 mips_opts.warn_about_macros = 0;
11644 }
11645 else if (strcmp (name, "nomacro") == 0)
11646 {
11647 if (mips_opts.noreorder == 0)
11648 as_bad (_("`noreorder' must be set before `nomacro'"));
11649 mips_opts.warn_about_macros = 1;
11650 }
11651 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
11652 {
11653 mips_opts.nomove = 0;
11654 }
11655 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
11656 {
11657 mips_opts.nomove = 1;
11658 }
11659 else if (strcmp (name, "bopt") == 0)
11660 {
11661 mips_opts.nobopt = 0;
11662 }
11663 else if (strcmp (name, "nobopt") == 0)
11664 {
11665 mips_opts.nobopt = 1;
11666 }
deec1734
CD
11667 else if (strcmp (name, "mdmx") == 0)
11668 mips_opts.ase_mdmx = 1;
11669 else if (strcmp (name, "nomdmx") == 0)
11670 mips_opts.ase_mdmx = 0;
252b5132
RH
11671 else if (strcmp (name, "mips16") == 0
11672 || strcmp (name, "MIPS-16") == 0)
11673 mips_opts.mips16 = 1;
11674 else if (strcmp (name, "nomips16") == 0
11675 || strcmp (name, "noMIPS-16") == 0)
11676 mips_opts.mips16 = 0;
1f25f5d3
CD
11677 else if (strcmp (name, "mips3d") == 0)
11678 mips_opts.ase_mips3d = 1;
11679 else if (strcmp (name, "nomips3d") == 0)
11680 mips_opts.ase_mips3d = 0;
252b5132
RH
11681 else if (strncmp (name, "mips", 4) == 0)
11682 {
11683 int isa;
11684
11685 /* Permit the user to change the ISA on the fly. Needless to
11686 say, misuse can cause serious problems. */
11687 isa = atoi (name + 4);
553178e4 11688 switch (isa)
98d3f06f
KH
11689 {
11690 case 0:
11691 mips_opts.gp32 = file_mips_gp32;
11692 mips_opts.fp32 = file_mips_fp32;
11693 mips_opts.abi = file_mips_abi;
11694 break;
11695 case 1:
11696 case 2:
11697 case 32:
11698 mips_opts.gp32 = 1;
11699 mips_opts.fp32 = 1;
11700 break;
11701 case 3:
11702 case 4:
11703 case 5:
11704 case 64:
11705 /* Loosen ABI register width restriction. */
11706 if (mips_opts.abi == O32_ABI)
11707 mips_opts.abi = NO_ABI;
11708 mips_opts.gp32 = 0;
11709 mips_opts.fp32 = 0;
11710 break;
11711 default:
11712 as_bad (_("unknown ISA level %s"), name + 4);
11713 break;
11714 }
553178e4 11715
e7af610e 11716 switch (isa)
98d3f06f
KH
11717 {
11718 case 0: mips_opts.isa = file_mips_isa; break;
11719 case 1: mips_opts.isa = ISA_MIPS1; break;
11720 case 2: mips_opts.isa = ISA_MIPS2; break;
11721 case 3: mips_opts.isa = ISA_MIPS3; break;
11722 case 4: mips_opts.isa = ISA_MIPS4; break;
11723 case 5: mips_opts.isa = ISA_MIPS5; break;
11724 case 32: mips_opts.isa = ISA_MIPS32; break;
11725 case 64: mips_opts.isa = ISA_MIPS64; break;
11726 default: as_bad (_("unknown ISA level %s"), name + 4); break;
11727 }
252b5132
RH
11728 }
11729 else if (strcmp (name, "autoextend") == 0)
11730 mips_opts.noautoextend = 0;
11731 else if (strcmp (name, "noautoextend") == 0)
11732 mips_opts.noautoextend = 1;
11733 else if (strcmp (name, "push") == 0)
11734 {
11735 struct mips_option_stack *s;
11736
11737 s = (struct mips_option_stack *) xmalloc (sizeof *s);
11738 s->next = mips_opts_stack;
11739 s->options = mips_opts;
11740 mips_opts_stack = s;
11741 }
11742 else if (strcmp (name, "pop") == 0)
11743 {
11744 struct mips_option_stack *s;
11745
11746 s = mips_opts_stack;
11747 if (s == NULL)
11748 as_bad (_(".set pop with no .set push"));
11749 else
11750 {
11751 /* If we're changing the reorder mode we need to handle
11752 delay slots correctly. */
11753 if (s->options.noreorder && ! mips_opts.noreorder)
11754 mips_emit_delays (true);
11755 else if (! s->options.noreorder && mips_opts.noreorder)
11756 {
11757 if (prev_nop_frag != NULL)
11758 {
11759 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11760 * (mips_opts.mips16 ? 2 : 4));
11761 prev_nop_frag = NULL;
11762 }
11763 }
11764
11765 mips_opts = s->options;
11766 mips_opts_stack = s->next;
11767 free (s);
11768 }
11769 }
11770 else
11771 {
11772 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
11773 }
11774 *input_line_pointer = ch;
11775 demand_empty_rest_of_line ();
11776}
11777
11778/* Handle the .abicalls pseudo-op. I believe this is equivalent to
11779 .option pic2. It means to generate SVR4 PIC calls. */
11780
11781static void
11782s_abicalls (ignore)
43841e91 11783 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
11784{
11785 mips_pic = SVR4_PIC;
11786 if (USE_GLOBAL_POINTER_OPT)
11787 {
11788 if (g_switch_seen && g_switch_value != 0)
11789 as_warn (_("-G may not be used with SVR4 PIC code"));
11790 g_switch_value = 0;
11791 }
11792 bfd_set_gp_size (stdoutput, 0);
11793 demand_empty_rest_of_line ();
11794}
11795
11796/* Handle the .cpload pseudo-op. This is used when generating SVR4
11797 PIC code. It sets the $gp register for the function based on the
11798 function address, which is in the register named in the argument.
11799 This uses a relocation against _gp_disp, which is handled specially
11800 by the linker. The result is:
11801 lui $gp,%hi(_gp_disp)
11802 addiu $gp,$gp,%lo(_gp_disp)
11803 addu $gp,$gp,.cpload argument
11804 The .cpload argument is normally $25 == $t9. */
11805
11806static void
11807s_cpload (ignore)
43841e91 11808 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
11809{
11810 expressionS ex;
11811 int icnt = 0;
11812
6478892d
TS
11813 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
11814 .cpload is ignored. */
11815 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
11816 {
11817 s_ignore (0);
11818 return;
11819 }
11820
d3ecfc59 11821 /* .cpload should be in a .set noreorder section. */
252b5132
RH
11822 if (mips_opts.noreorder == 0)
11823 as_warn (_(".cpload not in noreorder section"));
11824
11825 ex.X_op = O_symbol;
11826 ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
11827 ex.X_op_symbol = NULL;
11828 ex.X_add_number = 0;
11829
11830 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
49309057 11831 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
252b5132 11832
c9914766
TS
11833 macro_build_lui (NULL, &icnt, &ex, mips_gp_register);
11834 macro_build ((char *) NULL, &icnt, &ex, "addiu", "t,r,j",
11835 mips_gp_register, mips_gp_register, (int) BFD_RELOC_LO16);
252b5132
RH
11836
11837 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "addu", "d,v,t",
c9914766 11838 mips_gp_register, mips_gp_register, tc_get_register (0));
252b5132
RH
11839
11840 demand_empty_rest_of_line ();
11841}
11842
6478892d
TS
11843/* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
11844 .cpsetup $reg1, offset|$reg2, label
11845
11846 If offset is given, this results in:
11847 sd $gp, offset($sp)
956cd1d6 11848 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
11849 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
11850 daddu $gp, $gp, $reg1
6478892d
TS
11851
11852 If $reg2 is given, this results in:
11853 daddu $reg2, $gp, $0
956cd1d6 11854 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
11855 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
11856 daddu $gp, $gp, $reg1
11857 $reg1 is normally $25 == $t9. */
6478892d
TS
11858static void
11859s_cpsetup (ignore)
11860 int ignore ATTRIBUTE_UNUSED;
11861{
11862 expressionS ex_off;
11863 expressionS ex_sym;
11864 int reg1;
11865 int icnt = 0;
11866 char *sym;
11867
8586fc66 11868 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
6478892d
TS
11869 We also need NewABI support. */
11870 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11871 {
11872 s_ignore (0);
11873 return;
11874 }
11875
11876 reg1 = tc_get_register (0);
11877 SKIP_WHITESPACE ();
11878 if (*input_line_pointer != ',')
11879 {
11880 as_bad (_("missing argument separator ',' for .cpsetup"));
11881 return;
11882 }
11883 else
80245285 11884 ++input_line_pointer;
6478892d
TS
11885 SKIP_WHITESPACE ();
11886 if (*input_line_pointer == '$')
80245285
TS
11887 {
11888 mips_cpreturn_register = tc_get_register (0);
11889 mips_cpreturn_offset = -1;
11890 }
6478892d 11891 else
80245285
TS
11892 {
11893 mips_cpreturn_offset = get_absolute_expression ();
11894 mips_cpreturn_register = -1;
11895 }
6478892d
TS
11896 SKIP_WHITESPACE ();
11897 if (*input_line_pointer != ',')
11898 {
11899 as_bad (_("missing argument separator ',' for .cpsetup"));
11900 return;
11901 }
11902 else
f9419b05 11903 ++input_line_pointer;
6478892d
TS
11904 SKIP_WHITESPACE ();
11905 sym = input_line_pointer;
11906 while (ISALNUM (*input_line_pointer))
f9419b05 11907 ++input_line_pointer;
6478892d
TS
11908 *input_line_pointer = 0;
11909
11910 ex_sym.X_op = O_symbol;
11911 ex_sym.X_add_symbol = symbol_find_or_make (sym);
11912 ex_sym.X_op_symbol = NULL;
11913 ex_sym.X_add_number = 0;
11914
11915 if (mips_cpreturn_register == -1)
11916 {
11917 ex_off.X_op = O_constant;
11918 ex_off.X_add_symbol = NULL;
11919 ex_off.X_op_symbol = NULL;
11920 ex_off.X_add_number = mips_cpreturn_offset;
11921
11922 macro_build ((char *) NULL, &icnt, &ex_off, "sd", "t,o(b)",
11923 mips_gp_register, (int) BFD_RELOC_LO16, SP);
11924 }
11925 else
11926 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
11927 "d,v,t", mips_cpreturn_register, mips_gp_register, 0);
11928
11929 macro_build ((char *) NULL, &icnt, &ex_sym, "lui", "t,u", mips_gp_register,
11930 (int) BFD_RELOC_GPREL16);
8586fc66
TS
11931 fix_new (frag_now, prev_insn_where, 0, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
11932 fix_new (frag_now, prev_insn_where, 0, NULL, 0, 0, BFD_RELOC_HI16_S);
6478892d
TS
11933 macro_build ((char *) NULL, &icnt, &ex_sym, "addiu", "t,r,j",
11934 mips_gp_register, mips_gp_register, (int) BFD_RELOC_GPREL16);
8586fc66
TS
11935 fix_new (frag_now, prev_insn_where, 0, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
11936 fix_new (frag_now, prev_insn_where, 0, NULL, 0, 0, BFD_RELOC_LO16);
11937 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
11938 HAVE_64BIT_ADDRESSES ? "daddu" : "addu", "d,v,t",
11939 mips_gp_register, mips_gp_register, reg1);
6478892d
TS
11940
11941 demand_empty_rest_of_line ();
11942}
11943
11944static void
11945s_cplocal (ignore)
11946 int ignore ATTRIBUTE_UNUSED;
11947{
11948 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
11949 .cplocal is ignored. */
11950 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11951 {
11952 s_ignore (0);
11953 return;
11954 }
11955
11956 mips_gp_register = tc_get_register (0);
11957}
11958
252b5132
RH
11959/* Handle the .cprestore pseudo-op. This stores $gp into a given
11960 offset from $sp. The offset is remembered, and after making a PIC
11961 call $gp is restored from that location. */
11962
11963static void
11964s_cprestore (ignore)
43841e91 11965 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
11966{
11967 expressionS ex;
11968 int icnt = 0;
11969
6478892d 11970 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
c9914766 11971 .cprestore is ignored. */
6478892d 11972 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
11973 {
11974 s_ignore (0);
11975 return;
11976 }
11977
11978 mips_cprestore_offset = get_absolute_expression ();
7a621144 11979 mips_cprestore_valid = 1;
252b5132
RH
11980
11981 ex.X_op = O_constant;
11982 ex.X_add_symbol = NULL;
11983 ex.X_op_symbol = NULL;
11984 ex.X_add_number = mips_cprestore_offset;
11985
c9914766
TS
11986 macro_build ((char *) NULL, &icnt, &ex, HAVE_32BIT_ADDRESSES ? "sw" : "sd",
11987 "t,o(b)", mips_gp_register, (int) BFD_RELOC_LO16, SP);
252b5132
RH
11988
11989 demand_empty_rest_of_line ();
11990}
11991
6478892d
TS
11992/* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
11993 was given in the preceeding .gpsetup, it results in:
11994 ld $gp, offset($sp)
76b3015f 11995
6478892d
TS
11996 If a register $reg2 was given there, it results in:
11997 daddiu $gp, $gp, $reg2
11998 */
11999static void
12000s_cpreturn (ignore)
12001 int ignore ATTRIBUTE_UNUSED;
12002{
12003 expressionS ex;
12004 int icnt = 0;
12005
12006 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
12007 We also need NewABI support. */
12008 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12009 {
12010 s_ignore (0);
12011 return;
12012 }
12013
12014 if (mips_cpreturn_register == -1)
12015 {
12016 ex.X_op = O_constant;
12017 ex.X_add_symbol = NULL;
12018 ex.X_op_symbol = NULL;
12019 ex.X_add_number = mips_cpreturn_offset;
12020
12021 macro_build ((char *) NULL, &icnt, &ex, "ld", "t,o(b)",
12022 mips_gp_register, (int) BFD_RELOC_LO16, SP);
12023 }
12024 else
12025 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
12026 "d,v,t", mips_gp_register, mips_cpreturn_register, 0);
12027
12028 demand_empty_rest_of_line ();
12029}
12030
12031/* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
12032 code. It sets the offset to use in gp_rel relocations. */
12033
12034static void
12035s_gpvalue (ignore)
12036 int ignore ATTRIBUTE_UNUSED;
12037{
12038 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
12039 We also need NewABI support. */
12040 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
12041 {
12042 s_ignore (0);
12043 return;
12044 }
12045
def2e0dd 12046 mips_gprel_offset = get_absolute_expression ();
6478892d
TS
12047
12048 demand_empty_rest_of_line ();
12049}
12050
252b5132
RH
12051/* Handle the .gpword pseudo-op. This is used when generating PIC
12052 code. It generates a 32 bit GP relative reloc. */
12053
12054static void
12055s_gpword (ignore)
43841e91 12056 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
12057{
12058 symbolS *label;
12059 expressionS ex;
12060 char *p;
12061
12062 /* When not generating PIC code, this is treated as .word. */
12063 if (mips_pic != SVR4_PIC)
12064 {
12065 s_cons (2);
12066 return;
12067 }
12068
12069 label = insn_labels != NULL ? insn_labels->label : NULL;
12070 mips_emit_delays (true);
12071 if (auto_align)
12072 mips_align (2, 0, label);
12073 mips_clear_insn_labels ();
12074
12075 expression (&ex);
12076
12077 if (ex.X_op != O_symbol || ex.X_add_number != 0)
12078 {
12079 as_bad (_("Unsupported use of .gpword"));
12080 ignore_rest_of_line ();
12081 }
12082
12083 p = frag_more (4);
12084 md_number_to_chars (p, (valueT) 0, 4);
c9914766 12085 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, false,
cdf6fd85 12086 BFD_RELOC_GPREL32);
252b5132
RH
12087
12088 demand_empty_rest_of_line ();
12089}
12090
12091/* Handle the .cpadd pseudo-op. This is used when dealing with switch
12092 tables in SVR4 PIC code. */
12093
12094static void
12095s_cpadd (ignore)
43841e91 12096 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
12097{
12098 int icnt = 0;
12099 int reg;
12100
6478892d
TS
12101 /* This is ignored when not generating SVR4 PIC code or if this is NewABI
12102 code. */
12103 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
12104 {
12105 s_ignore (0);
12106 return;
12107 }
12108
12109 /* Add $gp to the register named as an argument. */
12110 reg = tc_get_register (0);
12111 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 12112 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 12113 "d,v,t", reg, reg, mips_gp_register);
252b5132 12114
bdaaa2e1 12115 demand_empty_rest_of_line ();
252b5132
RH
12116}
12117
12118/* Handle the .insn pseudo-op. This marks instruction labels in
12119 mips16 mode. This permits the linker to handle them specially,
12120 such as generating jalx instructions when needed. We also make
12121 them odd for the duration of the assembly, in order to generate the
12122 right sort of code. We will make them even in the adjust_symtab
12123 routine, while leaving them marked. This is convenient for the
12124 debugger and the disassembler. The linker knows to make them odd
12125 again. */
12126
12127static void
12128s_insn (ignore)
43841e91 12129 int ignore ATTRIBUTE_UNUSED;
252b5132 12130{
f9419b05 12131 mips16_mark_labels ();
252b5132
RH
12132
12133 demand_empty_rest_of_line ();
12134}
12135
12136/* Handle a .stabn directive. We need these in order to mark a label
12137 as being a mips16 text label correctly. Sometimes the compiler
12138 will emit a label, followed by a .stabn, and then switch sections.
12139 If the label and .stabn are in mips16 mode, then the label is
12140 really a mips16 text label. */
12141
12142static void
12143s_mips_stab (type)
12144 int type;
12145{
f9419b05 12146 if (type == 'n')
252b5132
RH
12147 mips16_mark_labels ();
12148
12149 s_stab (type);
12150}
12151
12152/* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
12153 */
12154
12155static void
12156s_mips_weakext (ignore)
43841e91 12157 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
12158{
12159 char *name;
12160 int c;
12161 symbolS *symbolP;
12162 expressionS exp;
12163
12164 name = input_line_pointer;
12165 c = get_symbol_end ();
12166 symbolP = symbol_find_or_make (name);
12167 S_SET_WEAK (symbolP);
12168 *input_line_pointer = c;
12169
12170 SKIP_WHITESPACE ();
12171
12172 if (! is_end_of_line[(unsigned char) *input_line_pointer])
12173 {
12174 if (S_IS_DEFINED (symbolP))
12175 {
956cd1d6 12176 as_bad ("ignoring attempt to redefine symbol %s",
252b5132
RH
12177 S_GET_NAME (symbolP));
12178 ignore_rest_of_line ();
12179 return;
12180 }
bdaaa2e1 12181
252b5132
RH
12182 if (*input_line_pointer == ',')
12183 {
12184 ++input_line_pointer;
12185 SKIP_WHITESPACE ();
12186 }
bdaaa2e1 12187
252b5132
RH
12188 expression (&exp);
12189 if (exp.X_op != O_symbol)
12190 {
12191 as_bad ("bad .weakext directive");
98d3f06f 12192 ignore_rest_of_line ();
252b5132
RH
12193 return;
12194 }
49309057 12195 symbol_set_value_expression (symbolP, &exp);
252b5132
RH
12196 }
12197
12198 demand_empty_rest_of_line ();
12199}
12200
12201/* Parse a register string into a number. Called from the ECOFF code
12202 to parse .frame. The argument is non-zero if this is the frame
12203 register, so that we can record it in mips_frame_reg. */
12204
12205int
12206tc_get_register (frame)
12207 int frame;
12208{
12209 int reg;
12210
12211 SKIP_WHITESPACE ();
12212 if (*input_line_pointer++ != '$')
12213 {
12214 as_warn (_("expected `$'"));
12215 reg = 0;
12216 }
3882b010 12217 else if (ISDIGIT (*input_line_pointer))
252b5132
RH
12218 {
12219 reg = get_absolute_expression ();
12220 if (reg < 0 || reg >= 32)
12221 {
12222 as_warn (_("Bad register number"));
12223 reg = 0;
12224 }
12225 }
12226 else
12227 {
76db943d
TS
12228 if (strncmp (input_line_pointer, "ra", 2) == 0)
12229 reg = RA;
12230 else if (strncmp (input_line_pointer, "fp", 2) == 0)
252b5132
RH
12231 reg = FP;
12232 else if (strncmp (input_line_pointer, "sp", 2) == 0)
12233 reg = SP;
12234 else if (strncmp (input_line_pointer, "gp", 2) == 0)
12235 reg = GP;
12236 else if (strncmp (input_line_pointer, "at", 2) == 0)
12237 reg = AT;
12238 else
12239 {
12240 as_warn (_("Unrecognized register name"));
12241 reg = 0;
12242 }
12243 input_line_pointer += 2;
12244 }
12245 if (frame)
7a621144
DJ
12246 {
12247 mips_frame_reg = reg != 0 ? reg : SP;
12248 mips_frame_reg_valid = 1;
12249 mips_cprestore_valid = 0;
12250 }
252b5132
RH
12251 return reg;
12252}
12253
12254valueT
12255md_section_align (seg, addr)
12256 asection *seg;
12257 valueT addr;
12258{
12259 int align = bfd_get_section_alignment (stdoutput, seg);
12260
12261#ifdef OBJ_ELF
12262 /* We don't need to align ELF sections to the full alignment.
12263 However, Irix 5 may prefer that we align them at least to a 16
12264 byte boundary. We don't bother to align the sections if we are
12265 targeted for an embedded system. */
12266 if (strcmp (TARGET_OS, "elf") == 0)
12267 return addr;
12268 if (align > 4)
12269 align = 4;
12270#endif
12271
12272 return ((addr + (1 << align) - 1) & (-1 << align));
12273}
12274
12275/* Utility routine, called from above as well. If called while the
12276 input file is still being read, it's only an approximation. (For
12277 example, a symbol may later become defined which appeared to be
12278 undefined earlier.) */
12279
12280static int
12281nopic_need_relax (sym, before_relaxing)
12282 symbolS *sym;
12283 int before_relaxing;
12284{
12285 if (sym == 0)
12286 return 0;
12287
6478892d 12288 if (USE_GLOBAL_POINTER_OPT && g_switch_value > 0)
252b5132
RH
12289 {
12290 const char *symname;
12291 int change;
12292
c9914766 12293 /* Find out whether this symbol can be referenced off the $gp
252b5132
RH
12294 register. It can be if it is smaller than the -G size or if
12295 it is in the .sdata or .sbss section. Certain symbols can
c9914766 12296 not be referenced off the $gp, although it appears as though
252b5132
RH
12297 they can. */
12298 symname = S_GET_NAME (sym);
12299 if (symname != (const char *) NULL
12300 && (strcmp (symname, "eprol") == 0
12301 || strcmp (symname, "etext") == 0
12302 || strcmp (symname, "_gp") == 0
12303 || strcmp (symname, "edata") == 0
12304 || strcmp (symname, "_fbss") == 0
12305 || strcmp (symname, "_fdata") == 0
12306 || strcmp (symname, "_ftext") == 0
12307 || strcmp (symname, "end") == 0
12308 || strcmp (symname, "_gp_disp") == 0))
12309 change = 1;
12310 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
12311 && (0
12312#ifndef NO_ECOFF_DEBUGGING
49309057
ILT
12313 || (symbol_get_obj (sym)->ecoff_extern_size != 0
12314 && (symbol_get_obj (sym)->ecoff_extern_size
12315 <= g_switch_value))
252b5132
RH
12316#endif
12317 /* We must defer this decision until after the whole
12318 file has been read, since there might be a .extern
12319 after the first use of this symbol. */
12320 || (before_relaxing
12321#ifndef NO_ECOFF_DEBUGGING
49309057 12322 && symbol_get_obj (sym)->ecoff_extern_size == 0
252b5132
RH
12323#endif
12324 && S_GET_VALUE (sym) == 0)
12325 || (S_GET_VALUE (sym) != 0
12326 && S_GET_VALUE (sym) <= g_switch_value)))
12327 change = 0;
12328 else
12329 {
12330 const char *segname;
12331
12332 segname = segment_name (S_GET_SEGMENT (sym));
12333 assert (strcmp (segname, ".lit8") != 0
12334 && strcmp (segname, ".lit4") != 0);
12335 change = (strcmp (segname, ".sdata") != 0
fba2b7f9
GK
12336 && strcmp (segname, ".sbss") != 0
12337 && strncmp (segname, ".sdata.", 7) != 0
12338 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
252b5132
RH
12339 }
12340 return change;
12341 }
12342 else
c9914766 12343 /* We are not optimizing for the $gp register. */
252b5132
RH
12344 return 1;
12345}
12346
12347/* Given a mips16 variant frag FRAGP, return non-zero if it needs an
12348 extended opcode. SEC is the section the frag is in. */
12349
12350static int
12351mips16_extended_frag (fragp, sec, stretch)
12352 fragS *fragp;
12353 asection *sec;
12354 long stretch;
12355{
12356 int type;
12357 register const struct mips16_immed_operand *op;
12358 offsetT val;
12359 int mintiny, maxtiny;
12360 segT symsec;
98aa84af 12361 fragS *sym_frag;
252b5132
RH
12362
12363 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
12364 return 0;
12365 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
12366 return 1;
12367
12368 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12369 op = mips16_immed_operands;
12370 while (op->type != type)
12371 {
12372 ++op;
12373 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
12374 }
12375
12376 if (op->unsp)
12377 {
12378 if (type == '<' || type == '>' || type == '[' || type == ']')
12379 {
12380 mintiny = 1;
12381 maxtiny = 1 << op->nbits;
12382 }
12383 else
12384 {
12385 mintiny = 0;
12386 maxtiny = (1 << op->nbits) - 1;
12387 }
12388 }
12389 else
12390 {
12391 mintiny = - (1 << (op->nbits - 1));
12392 maxtiny = (1 << (op->nbits - 1)) - 1;
12393 }
12394
98aa84af 12395 sym_frag = symbol_get_frag (fragp->fr_symbol);
ac62c346 12396 val = S_GET_VALUE (fragp->fr_symbol);
98aa84af 12397 symsec = S_GET_SEGMENT (fragp->fr_symbol);
252b5132
RH
12398
12399 if (op->pcrel)
12400 {
12401 addressT addr;
12402
12403 /* We won't have the section when we are called from
12404 mips_relax_frag. However, we will always have been called
12405 from md_estimate_size_before_relax first. If this is a
12406 branch to a different section, we mark it as such. If SEC is
12407 NULL, and the frag is not marked, then it must be a branch to
12408 the same section. */
12409 if (sec == NULL)
12410 {
12411 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
12412 return 1;
12413 }
12414 else
12415 {
98aa84af 12416 /* Must have been called from md_estimate_size_before_relax. */
252b5132
RH
12417 if (symsec != sec)
12418 {
12419 fragp->fr_subtype =
12420 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12421
12422 /* FIXME: We should support this, and let the linker
12423 catch branches and loads that are out of range. */
12424 as_bad_where (fragp->fr_file, fragp->fr_line,
12425 _("unsupported PC relative reference to different section"));
12426
12427 return 1;
12428 }
98aa84af
AM
12429 if (fragp != sym_frag && sym_frag->fr_address == 0)
12430 /* Assume non-extended on the first relaxation pass.
12431 The address we have calculated will be bogus if this is
12432 a forward branch to another frag, as the forward frag
12433 will have fr_address == 0. */
12434 return 0;
252b5132
RH
12435 }
12436
12437 /* In this case, we know for sure that the symbol fragment is in
98aa84af
AM
12438 the same section. If the relax_marker of the symbol fragment
12439 differs from the relax_marker of this fragment, we have not
12440 yet adjusted the symbol fragment fr_address. We want to add
252b5132
RH
12441 in STRETCH in order to get a better estimate of the address.
12442 This particularly matters because of the shift bits. */
12443 if (stretch != 0
98aa84af 12444 && sym_frag->relax_marker != fragp->relax_marker)
252b5132
RH
12445 {
12446 fragS *f;
12447
12448 /* Adjust stretch for any alignment frag. Note that if have
12449 been expanding the earlier code, the symbol may be
12450 defined in what appears to be an earlier frag. FIXME:
12451 This doesn't handle the fr_subtype field, which specifies
12452 a maximum number of bytes to skip when doing an
12453 alignment. */
98aa84af 12454 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
252b5132
RH
12455 {
12456 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
12457 {
12458 if (stretch < 0)
12459 stretch = - ((- stretch)
12460 & ~ ((1 << (int) f->fr_offset) - 1));
12461 else
12462 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
12463 if (stretch == 0)
12464 break;
12465 }
12466 }
12467 if (f != NULL)
12468 val += stretch;
12469 }
12470
12471 addr = fragp->fr_address + fragp->fr_fix;
12472
12473 /* The base address rules are complicated. The base address of
12474 a branch is the following instruction. The base address of a
12475 PC relative load or add is the instruction itself, but if it
12476 is in a delay slot (in which case it can not be extended) use
12477 the address of the instruction whose delay slot it is in. */
12478 if (type == 'p' || type == 'q')
12479 {
12480 addr += 2;
12481
12482 /* If we are currently assuming that this frag should be
12483 extended, then, the current address is two bytes
bdaaa2e1 12484 higher. */
252b5132
RH
12485 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12486 addr += 2;
12487
12488 /* Ignore the low bit in the target, since it will be set
12489 for a text label. */
12490 if ((val & 1) != 0)
12491 --val;
12492 }
12493 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12494 addr -= 4;
12495 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12496 addr -= 2;
12497
12498 val -= addr & ~ ((1 << op->shift) - 1);
12499
12500 /* Branch offsets have an implicit 0 in the lowest bit. */
12501 if (type == 'p' || type == 'q')
12502 val /= 2;
12503
12504 /* If any of the shifted bits are set, we must use an extended
12505 opcode. If the address depends on the size of this
12506 instruction, this can lead to a loop, so we arrange to always
12507 use an extended opcode. We only check this when we are in
12508 the main relaxation loop, when SEC is NULL. */
12509 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
12510 {
12511 fragp->fr_subtype =
12512 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12513 return 1;
12514 }
12515
12516 /* If we are about to mark a frag as extended because the value
12517 is precisely maxtiny + 1, then there is a chance of an
12518 infinite loop as in the following code:
12519 la $4,foo
12520 .skip 1020
12521 .align 2
12522 foo:
12523 In this case when the la is extended, foo is 0x3fc bytes
12524 away, so the la can be shrunk, but then foo is 0x400 away, so
12525 the la must be extended. To avoid this loop, we mark the
12526 frag as extended if it was small, and is about to become
12527 extended with a value of maxtiny + 1. */
12528 if (val == ((maxtiny + 1) << op->shift)
12529 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
12530 && sec == NULL)
12531 {
12532 fragp->fr_subtype =
12533 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12534 return 1;
12535 }
12536 }
12537 else if (symsec != absolute_section && sec != NULL)
12538 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
12539
12540 if ((val & ((1 << op->shift) - 1)) != 0
12541 || val < (mintiny << op->shift)
12542 || val > (maxtiny << op->shift))
12543 return 1;
12544 else
12545 return 0;
12546}
12547
12548/* Estimate the size of a frag before relaxing. Unless this is the
12549 mips16, we are not really relaxing here, and the final size is
12550 encoded in the subtype information. For the mips16, we have to
12551 decide whether we are using an extended opcode or not. */
12552
252b5132
RH
12553int
12554md_estimate_size_before_relax (fragp, segtype)
12555 fragS *fragp;
12556 asection *segtype;
12557{
43841e91 12558 int change = 0;
8614eeee 12559 boolean linkonce = false;
252b5132
RH
12560
12561 if (RELAX_MIPS16_P (fragp->fr_subtype))
177b4a6a
AO
12562 /* We don't want to modify the EXTENDED bit here; it might get us
12563 into infinite loops. We change it only in mips_relax_frag(). */
12564 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
252b5132
RH
12565
12566 if (mips_pic == NO_PIC)
12567 {
12568 change = nopic_need_relax (fragp->fr_symbol, 0);
12569 }
12570 else if (mips_pic == SVR4_PIC)
12571 {
12572 symbolS *sym;
12573 asection *symsec;
12574
12575 sym = fragp->fr_symbol;
12576
12577 /* Handle the case of a symbol equated to another symbol. */
e0890092 12578 while (symbol_equated_reloc_p (sym))
252b5132
RH
12579 {
12580 symbolS *n;
12581
12582 /* It's possible to get a loop here in a badly written
12583 program. */
49309057 12584 n = symbol_get_value_expression (sym)->X_add_symbol;
252b5132
RH
12585 if (n == sym)
12586 break;
12587 sym = n;
12588 }
12589
12590 symsec = S_GET_SEGMENT (sym);
12591
8614eeee
UC
12592 /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
12593 if (symsec != segtype && ! S_IS_LOCAL (sym))
beae10d5
KH
12594 {
12595 if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
12596 != 0)
12597 linkonce = true;
12598
12599 /* The GNU toolchain uses an extension for ELF: a section
12600 beginning with the magic string .gnu.linkonce is a linkonce
12601 section. */
12602 if (strncmp (segment_name (symsec), ".gnu.linkonce",
12603 sizeof ".gnu.linkonce" - 1) == 0)
12604 linkonce = true;
12605 }
8614eeee 12606
252b5132
RH
12607 /* This must duplicate the test in adjust_reloc_syms. */
12608 change = (symsec != &bfd_und_section
12609 && symsec != &bfd_abs_section
426b0403 12610 && ! bfd_is_com_section (symsec)
8614eeee 12611 && !linkonce
426b0403 12612#ifdef OBJ_ELF
ea4ff978 12613 /* A global or weak symbol is treated as external. */
9151e8bf 12614 && (OUTPUT_FLAVOR != bfd_target_elf_flavour
b25a253c
CD
12615 || (! S_IS_WEAK (sym)
12616 && (! S_IS_EXTERN (sym) || mips_pic == EMBEDDED_PIC)))
426b0403
AM
12617#endif
12618 );
252b5132
RH
12619 }
12620 else
12621 abort ();
12622
12623 if (change)
12624 {
12625 /* Record the offset to the first reloc in the fr_opcode field.
12626 This lets md_convert_frag and tc_gen_reloc know that the code
12627 must be expanded. */
12628 fragp->fr_opcode = (fragp->fr_literal
12629 + fragp->fr_fix
12630 - RELAX_OLD (fragp->fr_subtype)
12631 + RELAX_RELOC1 (fragp->fr_subtype));
12632 /* FIXME: This really needs as_warn_where. */
12633 if (RELAX_WARN (fragp->fr_subtype))
9a41af64
TS
12634 as_warn (_("AT used after \".set noat\" or macro used after "
12635 "\".set nomacro\""));
12636
12637 return RELAX_NEW (fragp->fr_subtype) - RELAX_OLD (fragp->fr_subtype);
252b5132
RH
12638 }
12639
9a41af64 12640 return 0;
252b5132
RH
12641}
12642
12643/* This is called to see whether a reloc against a defined symbol
12644 should be converted into a reloc against a section. Don't adjust
12645 MIPS16 jump relocations, so we don't have to worry about the format
12646 of the offset in the .o file. Don't adjust relocations against
12647 mips16 symbols, so that the linker can find them if it needs to set
12648 up a stub. */
12649
12650int
12651mips_fix_adjustable (fixp)
12652 fixS *fixp;
12653{
ea4ff978
L
12654#ifdef OBJ_ELF
12655 /* Prevent all adjustments to global symbols. */
46bac6de 12656 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
b25a253c 12657 && mips_pic != EMBEDDED_PIC
46bac6de 12658 && (S_IS_EXTERN (fixp->fx_addsy) || S_IS_WEAK (fixp->fx_addsy)))
ea4ff978
L
12659 return 0;
12660#endif
252b5132
RH
12661 if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
12662 return 0;
12663 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
12664 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12665 return 0;
12666 if (fixp->fx_addsy == NULL)
12667 return 1;
12668#ifdef OBJ_ELF
12669 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
12670 && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
12671 && fixp->fx_subsy == NULL)
12672 return 0;
12673#endif
12674 return 1;
12675}
12676
12677/* Translate internal representation of relocation info to BFD target
12678 format. */
12679
12680arelent **
12681tc_gen_reloc (section, fixp)
43841e91 12682 asection *section ATTRIBUTE_UNUSED;
252b5132
RH
12683 fixS *fixp;
12684{
12685 static arelent *retval[4];
12686 arelent *reloc;
12687 bfd_reloc_code_real_type code;
12688
12689 reloc = retval[0] = (arelent *) xmalloc (sizeof (arelent));
12690 retval[1] = NULL;
12691
49309057
ILT
12692 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
12693 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
12694 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
12695
12696 if (mips_pic == EMBEDDED_PIC
12697 && SWITCH_TABLE (fixp))
12698 {
12699 /* For a switch table entry we use a special reloc. The addend
12700 is actually the difference between the reloc address and the
12701 subtrahend. */
12702 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
12703 if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
12704 as_fatal (_("Double check fx_r_type in tc-mips.c:tc_gen_reloc"));
12705 fixp->fx_r_type = BFD_RELOC_GPREL32;
12706 }
12707 else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16)
12708 {
4514d474
CD
12709 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
12710 reloc->addend = fixp->fx_addnumber;
252b5132 12711 else
4514d474
CD
12712 {
12713 /* We use a special addend for an internal RELLO reloc. */
12714 if (symbol_section_p (fixp->fx_addsy))
12715 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
12716 else
12717 reloc->addend = fixp->fx_addnumber + reloc->address;
12718 }
252b5132
RH
12719 }
12720 else if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
12721 {
12722 assert (fixp->fx_next != NULL
12723 && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
4514d474
CD
12724
12725 /* The reloc is relative to the RELLO; adjust the addend
252b5132 12726 accordingly. */
4514d474
CD
12727 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
12728 reloc->addend = fixp->fx_next->fx_addnumber;
252b5132 12729 else
4514d474
CD
12730 {
12731 /* We use a special addend for an internal RELHI reloc. */
12732 if (symbol_section_p (fixp->fx_addsy))
12733 reloc->addend = (fixp->fx_next->fx_frag->fr_address
12734 + fixp->fx_next->fx_where
12735 - S_GET_VALUE (fixp->fx_subsy));
12736 else
12737 reloc->addend = (fixp->fx_addnumber
12738 + fixp->fx_next->fx_frag->fr_address
12739 + fixp->fx_next->fx_where);
12740 }
252b5132 12741 }
4514d474
CD
12742 else if (fixp->fx_pcrel == 0 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
12743 reloc->addend = fixp->fx_addnumber;
252b5132
RH
12744 else
12745 {
12746 if (OUTPUT_FLAVOR != bfd_target_aout_flavour)
12747 /* A gruesome hack which is a result of the gruesome gas reloc
12748 handling. */
12749 reloc->addend = reloc->address;
12750 else
12751 reloc->addend = -reloc->address;
12752 }
12753
12754 /* If this is a variant frag, we may need to adjust the existing
12755 reloc and generate a new one. */
12756 if (fixp->fx_frag->fr_opcode != NULL
cdf6fd85 12757 && (fixp->fx_r_type == BFD_RELOC_GPREL16
252b5132
RH
12758 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
12759 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL16
12760 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
12761 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16
12762 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
6478892d
TS
12763 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_LO16)
12764 && ! HAVE_NEWABI)
252b5132
RH
12765 {
12766 arelent *reloc2;
12767
12768 assert (! RELAX_MIPS16_P (fixp->fx_frag->fr_subtype));
12769
12770 /* If this is not the last reloc in this frag, then we have two
12771 GPREL relocs, or a GOT_HI16/GOT_LO16 pair, or a
12772 CALL_HI16/CALL_LO16, both of which are being replaced. Let
12773 the second one handle all of them. */
12774 if (fixp->fx_next != NULL
12775 && fixp->fx_frag == fixp->fx_next->fx_frag)
12776 {
cdf6fd85
TS
12777 assert ((fixp->fx_r_type == BFD_RELOC_GPREL16
12778 && fixp->fx_next->fx_r_type == BFD_RELOC_GPREL16)
252b5132
RH
12779 || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
12780 && (fixp->fx_next->fx_r_type
12781 == BFD_RELOC_MIPS_GOT_LO16))
12782 || (fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
12783 && (fixp->fx_next->fx_r_type
12784 == BFD_RELOC_MIPS_CALL_LO16)));
12785 retval[0] = NULL;
12786 return retval;
12787 }
12788
12789 fixp->fx_where = fixp->fx_frag->fr_opcode - fixp->fx_frag->fr_literal;
12790 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
12791 reloc2 = retval[1] = (arelent *) xmalloc (sizeof (arelent));
12792 retval[2] = NULL;
49309057
ILT
12793 reloc2->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
12794 *reloc2->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
12795 reloc2->address = (reloc->address
12796 + (RELAX_RELOC2 (fixp->fx_frag->fr_subtype)
12797 - RELAX_RELOC1 (fixp->fx_frag->fr_subtype)));
12798 reloc2->addend = fixp->fx_addnumber;
12799 reloc2->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO16);
12800 assert (reloc2->howto != NULL);
12801
12802 if (RELAX_RELOC3 (fixp->fx_frag->fr_subtype))
12803 {
12804 arelent *reloc3;
12805
12806 reloc3 = retval[2] = (arelent *) xmalloc (sizeof (arelent));
12807 retval[3] = NULL;
12808 *reloc3 = *reloc2;
12809 reloc3->address += 4;
12810 }
12811
12812 if (mips_pic == NO_PIC)
12813 {
cdf6fd85 12814 assert (fixp->fx_r_type == BFD_RELOC_GPREL16);
252b5132
RH
12815 fixp->fx_r_type = BFD_RELOC_HI16_S;
12816 }
12817 else if (mips_pic == SVR4_PIC)
12818 {
12819 switch (fixp->fx_r_type)
12820 {
12821 default:
12822 abort ();
12823 case BFD_RELOC_MIPS_GOT16:
12824 break;
12825 case BFD_RELOC_MIPS_CALL16:
12826 case BFD_RELOC_MIPS_GOT_LO16:
12827 case BFD_RELOC_MIPS_CALL_LO16:
12828 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
12829 break;
12830 }
12831 }
12832 else
12833 abort ();
12834 }
12835
438c16b8
TS
12836 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
12837 entry to be used in the relocation's section offset. */
12838 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
252b5132
RH
12839 {
12840 reloc->address = reloc->addend;
12841 reloc->addend = 0;
12842 }
12843
12844 /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
12845 fixup_segment converted a non-PC relative reloc into a PC
12846 relative reloc. In such a case, we need to convert the reloc
12847 code. */
12848 code = fixp->fx_r_type;
12849 if (fixp->fx_pcrel)
12850 {
12851 switch (code)
12852 {
12853 case BFD_RELOC_8:
12854 code = BFD_RELOC_8_PCREL;
12855 break;
12856 case BFD_RELOC_16:
12857 code = BFD_RELOC_16_PCREL;
12858 break;
12859 case BFD_RELOC_32:
12860 code = BFD_RELOC_32_PCREL;
12861 break;
12862 case BFD_RELOC_64:
12863 code = BFD_RELOC_64_PCREL;
12864 break;
12865 case BFD_RELOC_8_PCREL:
12866 case BFD_RELOC_16_PCREL:
12867 case BFD_RELOC_32_PCREL:
12868 case BFD_RELOC_64_PCREL:
12869 case BFD_RELOC_16_PCREL_S2:
12870 case BFD_RELOC_PCREL_HI16_S:
12871 case BFD_RELOC_PCREL_LO16:
12872 break;
12873 default:
12874 as_bad_where (fixp->fx_file, fixp->fx_line,
12875 _("Cannot make %s relocation PC relative"),
12876 bfd_get_reloc_code_name (code));
12877 }
12878 }
12879
add55e1f
RS
12880#ifdef OBJ_ELF
12881 /* md_apply_fix3 has a double-subtraction hack to get
12882 bfd_install_relocation to behave nicely. GPREL relocations are
12883 handled correctly without this hack, so undo it here. We can't
12884 stop md_apply_fix3 from subtracting twice in the first place since
12885 the fake addend is required for variant frags above. */
12886 if (fixp->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour
d11008e1 12887 && code == BFD_RELOC_GPREL16
add55e1f
RS
12888 && reloc->addend != 0
12889 && mips_need_elf_addend_fixup (fixp))
12890 reloc->addend += S_GET_VALUE (fixp->fx_addsy);
12891#endif
12892
252b5132
RH
12893 /* To support a PC relative reloc when generating embedded PIC code
12894 for ECOFF, we use a Cygnus extension. We check for that here to
12895 make sure that we don't let such a reloc escape normally. */
bb2d6cd7
GK
12896 if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
12897 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
252b5132
RH
12898 && code == BFD_RELOC_16_PCREL_S2
12899 && mips_pic != EMBEDDED_PIC)
12900 reloc->howto = NULL;
12901 else
12902 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
12903
12904 if (reloc->howto == NULL)
12905 {
12906 as_bad_where (fixp->fx_file, fixp->fx_line,
12907 _("Can not represent %s relocation in this object file format"),
12908 bfd_get_reloc_code_name (code));
12909 retval[0] = NULL;
12910 }
12911
12912 return retval;
12913}
12914
12915/* Relax a machine dependent frag. This returns the amount by which
12916 the current size of the frag should change. */
12917
12918int
12919mips_relax_frag (fragp, stretch)
12920 fragS *fragp;
12921 long stretch;
12922{
12923 if (! RELAX_MIPS16_P (fragp->fr_subtype))
12924 return 0;
12925
c4e7957c 12926 if (mips16_extended_frag (fragp, NULL, stretch))
252b5132
RH
12927 {
12928 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12929 return 0;
12930 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
12931 return 2;
12932 }
12933 else
12934 {
12935 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12936 return 0;
12937 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
12938 return -2;
12939 }
12940
12941 return 0;
12942}
12943
12944/* Convert a machine dependent frag. */
12945
12946void
12947md_convert_frag (abfd, asec, fragp)
43841e91 12948 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
12949 segT asec;
12950 fragS *fragp;
12951{
12952 int old, new;
12953 char *fixptr;
12954
12955 if (RELAX_MIPS16_P (fragp->fr_subtype))
12956 {
12957 int type;
12958 register const struct mips16_immed_operand *op;
12959 boolean small, ext;
12960 offsetT val;
12961 bfd_byte *buf;
12962 unsigned long insn;
12963 boolean use_extend;
12964 unsigned short extend;
12965
12966 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12967 op = mips16_immed_operands;
12968 while (op->type != type)
12969 ++op;
12970
12971 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12972 {
12973 small = false;
12974 ext = true;
12975 }
12976 else
12977 {
12978 small = true;
12979 ext = false;
12980 }
12981
6386f3a7 12982 resolve_symbol_value (fragp->fr_symbol);
252b5132
RH
12983 val = S_GET_VALUE (fragp->fr_symbol);
12984 if (op->pcrel)
12985 {
12986 addressT addr;
12987
12988 addr = fragp->fr_address + fragp->fr_fix;
12989
12990 /* The rules for the base address of a PC relative reloc are
12991 complicated; see mips16_extended_frag. */
12992 if (type == 'p' || type == 'q')
12993 {
12994 addr += 2;
12995 if (ext)
12996 addr += 2;
12997 /* Ignore the low bit in the target, since it will be
12998 set for a text label. */
12999 if ((val & 1) != 0)
13000 --val;
13001 }
13002 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
13003 addr -= 4;
13004 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
13005 addr -= 2;
13006
13007 addr &= ~ (addressT) ((1 << op->shift) - 1);
13008 val -= addr;
13009
13010 /* Make sure the section winds up with the alignment we have
13011 assumed. */
13012 if (op->shift > 0)
13013 record_alignment (asec, op->shift);
13014 }
13015
13016 if (ext
13017 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
13018 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
13019 as_warn_where (fragp->fr_file, fragp->fr_line,
13020 _("extended instruction in delay slot"));
13021
13022 buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
13023
13024 if (target_big_endian)
13025 insn = bfd_getb16 (buf);
13026 else
13027 insn = bfd_getl16 (buf);
13028
13029 mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
13030 RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
13031 small, ext, &insn, &use_extend, &extend);
13032
13033 if (use_extend)
13034 {
874e8986 13035 md_number_to_chars ((char *) buf, 0xf000 | extend, 2);
252b5132
RH
13036 fragp->fr_fix += 2;
13037 buf += 2;
13038 }
13039
874e8986 13040 md_number_to_chars ((char *) buf, insn, 2);
252b5132
RH
13041 fragp->fr_fix += 2;
13042 buf += 2;
13043 }
13044 else
13045 {
13046 if (fragp->fr_opcode == NULL)
13047 return;
13048
13049 old = RELAX_OLD (fragp->fr_subtype);
13050 new = RELAX_NEW (fragp->fr_subtype);
13051 fixptr = fragp->fr_literal + fragp->fr_fix;
13052
13053 if (new > 0)
13054 memcpy (fixptr - old, fixptr, new);
13055
13056 fragp->fr_fix += new - old;
13057 }
13058}
13059
13060#ifdef OBJ_ELF
13061
13062/* This function is called after the relocs have been generated.
13063 We've been storing mips16 text labels as odd. Here we convert them
13064 back to even for the convenience of the debugger. */
13065
13066void
13067mips_frob_file_after_relocs ()
13068{
13069 asymbol **syms;
13070 unsigned int count, i;
13071
13072 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
13073 return;
13074
13075 syms = bfd_get_outsymbols (stdoutput);
13076 count = bfd_get_symcount (stdoutput);
13077 for (i = 0; i < count; i++, syms++)
13078 {
13079 if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
13080 && ((*syms)->value & 1) != 0)
13081 {
13082 (*syms)->value &= ~1;
13083 /* If the symbol has an odd size, it was probably computed
13084 incorrectly, so adjust that as well. */
13085 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
13086 ++elf_symbol (*syms)->internal_elf_sym.st_size;
13087 }
13088 }
13089}
13090
13091#endif
13092
13093/* This function is called whenever a label is defined. It is used
13094 when handling branch delays; if a branch has a label, we assume we
13095 can not move it. */
13096
13097void
13098mips_define_label (sym)
13099 symbolS *sym;
13100{
13101 struct insn_label_list *l;
13102
13103 if (free_insn_labels == NULL)
13104 l = (struct insn_label_list *) xmalloc (sizeof *l);
13105 else
13106 {
13107 l = free_insn_labels;
13108 free_insn_labels = l->next;
13109 }
13110
13111 l->label = sym;
13112 l->next = insn_labels;
13113 insn_labels = l;
13114}
13115\f
13116#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13117
13118/* Some special processing for a MIPS ELF file. */
13119
13120void
13121mips_elf_final_processing ()
13122{
13123 /* Write out the register information. */
195325d2 13124 if (file_mips_abi != N64_ABI)
252b5132
RH
13125 {
13126 Elf32_RegInfo s;
13127
13128 s.ri_gprmask = mips_gprmask;
13129 s.ri_cprmask[0] = mips_cprmask[0];
13130 s.ri_cprmask[1] = mips_cprmask[1];
13131 s.ri_cprmask[2] = mips_cprmask[2];
13132 s.ri_cprmask[3] = mips_cprmask[3];
13133 /* The gp_value field is set by the MIPS ELF backend. */
13134
13135 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
13136 ((Elf32_External_RegInfo *)
13137 mips_regmask_frag));
13138 }
13139 else
13140 {
13141 Elf64_Internal_RegInfo s;
13142
13143 s.ri_gprmask = mips_gprmask;
13144 s.ri_pad = 0;
13145 s.ri_cprmask[0] = mips_cprmask[0];
13146 s.ri_cprmask[1] = mips_cprmask[1];
13147 s.ri_cprmask[2] = mips_cprmask[2];
13148 s.ri_cprmask[3] = mips_cprmask[3];
13149 /* The gp_value field is set by the MIPS ELF backend. */
13150
13151 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
13152 ((Elf64_External_RegInfo *)
13153 mips_regmask_frag));
13154 }
13155
13156 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
13157 sort of BFD interface for this. */
13158 if (mips_any_noreorder)
13159 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
13160 if (mips_pic != NO_PIC)
13161 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
13162
98d3f06f 13163 /* Set MIPS ELF flags for ASEs. */
1f25f5d3
CD
13164#if 0 /* XXX FIXME */
13165 if (file_ase_mips3d)
13166 elf_elfheader (stdoutput)->e_flags |= ???;
13167#endif
deec1734
CD
13168 if (file_ase_mdmx)
13169 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
1f25f5d3 13170
bdaaa2e1 13171 /* Set the MIPS ELF ABI flags. */
a325df1d 13172 if (file_mips_abi == NO_ABI)
252b5132 13173 ;
a325df1d 13174 else if (file_mips_abi == O32_ABI)
252b5132 13175 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
a325df1d 13176 else if (file_mips_abi == O64_ABI)
252b5132 13177 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
a325df1d 13178 else if (file_mips_abi == EABI_ABI)
252b5132
RH
13179 {
13180 if (mips_eabi64)
13181 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
13182 else
13183 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
13184 }
a325df1d 13185 else if (file_mips_abi == N32_ABI)
be00bddd
TS
13186 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
13187
c9914766 13188 /* Nothing to do for N64_ABI. */
252b5132
RH
13189
13190 if (mips_32bitmode)
13191 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
13192}
13193
13194#endif /* OBJ_ELF || OBJ_MAYBE_ELF */
13195\f
beae10d5
KH
13196typedef struct proc {
13197 symbolS *isym;
13198 unsigned long reg_mask;
13199 unsigned long reg_offset;
13200 unsigned long fpreg_mask;
13201 unsigned long fpreg_offset;
13202 unsigned long frame_offset;
13203 unsigned long frame_reg;
13204 unsigned long pc_reg;
13205} procS;
252b5132
RH
13206
13207static procS cur_proc;
13208static procS *cur_proc_ptr;
13209static int numprocs;
13210
0a9ef439 13211/* Fill in an rs_align_code fragment. */
a19d8eb0 13212
0a9ef439
RH
13213void
13214mips_handle_align (fragp)
13215 fragS *fragp;
a19d8eb0 13216{
0a9ef439
RH
13217 if (fragp->fr_type != rs_align_code)
13218 return;
13219
13220 if (mips_opts.mips16)
a19d8eb0
CP
13221 {
13222 static const unsigned char be_nop[] = { 0x65, 0x00 };
13223 static const unsigned char le_nop[] = { 0x00, 0x65 };
13224
0a9ef439
RH
13225 int bytes;
13226 char *p;
a19d8eb0 13227
0a9ef439
RH
13228 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
13229 p = fragp->fr_literal + fragp->fr_fix;
13230
13231 if (bytes & 1)
13232 {
13233 *p++ = 0;
f9419b05 13234 fragp->fr_fix++;
0a9ef439
RH
13235 }
13236
13237 memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
13238 fragp->fr_var = 2;
a19d8eb0
CP
13239 }
13240
0a9ef439 13241 /* For mips32, a nop is a zero, which we trivially get by doing nothing. */
a19d8eb0
CP
13242}
13243
252b5132
RH
13244static void
13245md_obj_begin ()
13246{
13247}
13248
13249static void
13250md_obj_end ()
13251{
13252 /* check for premature end, nesting errors, etc */
13253 if (cur_proc_ptr)
9a41af64 13254 as_warn (_("missing .end at end of assembly"));
252b5132
RH
13255}
13256
13257static long
13258get_number ()
13259{
13260 int negative = 0;
13261 long val = 0;
13262
13263 if (*input_line_pointer == '-')
13264 {
13265 ++input_line_pointer;
13266 negative = 1;
13267 }
3882b010 13268 if (!ISDIGIT (*input_line_pointer))
956cd1d6 13269 as_bad (_("expected simple number"));
252b5132
RH
13270 if (input_line_pointer[0] == '0')
13271 {
13272 if (input_line_pointer[1] == 'x')
13273 {
13274 input_line_pointer += 2;
3882b010 13275 while (ISXDIGIT (*input_line_pointer))
252b5132
RH
13276 {
13277 val <<= 4;
13278 val |= hex_value (*input_line_pointer++);
13279 }
13280 return negative ? -val : val;
13281 }
13282 else
13283 {
13284 ++input_line_pointer;
3882b010 13285 while (ISDIGIT (*input_line_pointer))
252b5132
RH
13286 {
13287 val <<= 3;
13288 val |= *input_line_pointer++ - '0';
13289 }
13290 return negative ? -val : val;
13291 }
13292 }
3882b010 13293 if (!ISDIGIT (*input_line_pointer))
252b5132
RH
13294 {
13295 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
13296 *input_line_pointer, *input_line_pointer);
956cd1d6 13297 as_warn (_("invalid number"));
252b5132
RH
13298 return -1;
13299 }
3882b010 13300 while (ISDIGIT (*input_line_pointer))
252b5132
RH
13301 {
13302 val *= 10;
13303 val += *input_line_pointer++ - '0';
13304 }
13305 return negative ? -val : val;
13306}
13307
13308/* The .file directive; just like the usual .file directive, but there
13309 is an initial number which is the ECOFF file index. */
13310
13311static void
13312s_file (x)
43841e91 13313 int x ATTRIBUTE_UNUSED;
252b5132 13314{
874e8986 13315 get_number ();
252b5132
RH
13316 s_app_file (0);
13317}
13318
252b5132
RH
13319/* The .end directive. */
13320
13321static void
13322s_mips_end (x)
43841e91 13323 int x ATTRIBUTE_UNUSED;
252b5132
RH
13324{
13325 symbolS *p;
13326 int maybe_text;
13327
7a621144
DJ
13328 /* Following functions need their own .frame and .cprestore directives. */
13329 mips_frame_reg_valid = 0;
13330 mips_cprestore_valid = 0;
13331
252b5132
RH
13332 if (!is_end_of_line[(unsigned char) *input_line_pointer])
13333 {
13334 p = get_symbol ();
13335 demand_empty_rest_of_line ();
13336 }
13337 else
13338 p = NULL;
13339
13340#ifdef BFD_ASSEMBLER
13341 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
13342 maybe_text = 1;
13343 else
13344 maybe_text = 0;
13345#else
13346 if (now_seg != data_section && now_seg != bss_section)
13347 maybe_text = 1;
13348 else
13349 maybe_text = 0;
13350#endif
13351
13352 if (!maybe_text)
13353 as_warn (_(".end not in text section"));
13354
13355 if (!cur_proc_ptr)
13356 {
13357 as_warn (_(".end directive without a preceding .ent directive."));
13358 demand_empty_rest_of_line ();
13359 return;
13360 }
13361
13362 if (p != NULL)
13363 {
13364 assert (S_GET_NAME (p));
13365 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->isym)))
13366 as_warn (_(".end symbol does not match .ent symbol."));
13367 }
13368 else
13369 as_warn (_(".end directive missing or unknown symbol"));
13370
13371#ifdef MIPS_STABS_ELF
13372 {
13373 segT saved_seg = now_seg;
13374 subsegT saved_subseg = now_subseg;
252b5132 13375 valueT dot;
252b5132
RH
13376 expressionS exp;
13377 char *fragp;
13378
13379 dot = frag_now_fix ();
13380
13381#ifdef md_flush_pending_output
13382 md_flush_pending_output ();
13383#endif
13384
13385 assert (pdr_seg);
13386 subseg_set (pdr_seg, 0);
13387
beae10d5 13388 /* Write the symbol. */
252b5132
RH
13389 exp.X_op = O_symbol;
13390 exp.X_add_symbol = p;
13391 exp.X_add_number = 0;
13392 emit_expr (&exp, 4);
13393
beae10d5 13394 fragp = frag_more (7 * 4);
252b5132 13395
beae10d5
KH
13396 md_number_to_chars (fragp, (valueT) cur_proc_ptr->reg_mask, 4);
13397 md_number_to_chars (fragp + 4, (valueT) cur_proc_ptr->reg_offset, 4);
13398 md_number_to_chars (fragp + 8, (valueT) cur_proc_ptr->fpreg_mask, 4);
13399 md_number_to_chars (fragp + 12, (valueT) cur_proc_ptr->fpreg_offset, 4);
13400 md_number_to_chars (fragp + 16, (valueT) cur_proc_ptr->frame_offset, 4);
13401 md_number_to_chars (fragp + 20, (valueT) cur_proc_ptr->frame_reg, 4);
13402 md_number_to_chars (fragp + 24, (valueT) cur_proc_ptr->pc_reg, 4);
252b5132
RH
13403
13404 subseg_set (saved_seg, saved_subseg);
13405 }
f9419b05 13406#endif /* MIPS_STABS_ELF */
252b5132
RH
13407
13408 cur_proc_ptr = NULL;
13409}
13410
13411/* The .aent and .ent directives. */
13412
13413static void
13414s_mips_ent (aent)
13415 int aent;
13416{
252b5132
RH
13417 symbolS *symbolP;
13418 int maybe_text;
13419
13420 symbolP = get_symbol ();
13421 if (*input_line_pointer == ',')
f9419b05 13422 ++input_line_pointer;
252b5132 13423 SKIP_WHITESPACE ();
3882b010 13424 if (ISDIGIT (*input_line_pointer)
d9a62219 13425 || *input_line_pointer == '-')
874e8986 13426 get_number ();
252b5132
RH
13427
13428#ifdef BFD_ASSEMBLER
13429 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
13430 maybe_text = 1;
13431 else
13432 maybe_text = 0;
13433#else
13434 if (now_seg != data_section && now_seg != bss_section)
13435 maybe_text = 1;
13436 else
13437 maybe_text = 0;
13438#endif
13439
13440 if (!maybe_text)
13441 as_warn (_(".ent or .aent not in text section."));
13442
13443 if (!aent && cur_proc_ptr)
9a41af64 13444 as_warn (_("missing .end"));
252b5132
RH
13445
13446 if (!aent)
13447 {
7a621144
DJ
13448 /* This function needs its own .frame and .cprestore directives. */
13449 mips_frame_reg_valid = 0;
13450 mips_cprestore_valid = 0;
13451
252b5132
RH
13452 cur_proc_ptr = &cur_proc;
13453 memset (cur_proc_ptr, '\0', sizeof (procS));
13454
13455 cur_proc_ptr->isym = symbolP;
13456
49309057 13457 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
252b5132 13458
f9419b05 13459 ++numprocs;
252b5132
RH
13460 }
13461
13462 demand_empty_rest_of_line ();
13463}
13464
13465/* The .frame directive. If the mdebug section is present (IRIX 5 native)
bdaaa2e1 13466 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
252b5132 13467 s_mips_frame is used so that we can set the PDR information correctly.
bdaaa2e1 13468 We can't use the ecoff routines because they make reference to the ecoff
252b5132
RH
13469 symbol table (in the mdebug section). */
13470
13471static void
13472s_mips_frame (ignore)
2b3c5a5d 13473 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
13474{
13475#ifdef MIPS_STABS_ELF
13476
13477 long val;
13478
beae10d5 13479 if (cur_proc_ptr == (procS *) NULL)
252b5132
RH
13480 {
13481 as_warn (_(".frame outside of .ent"));
13482 demand_empty_rest_of_line ();
13483 return;
13484 }
13485
13486 cur_proc_ptr->frame_reg = tc_get_register (1);
13487
13488 SKIP_WHITESPACE ();
13489 if (*input_line_pointer++ != ','
13490 || get_absolute_expression_and_terminator (&val) != ',')
13491 {
13492 as_warn (_("Bad .frame directive"));
13493 --input_line_pointer;
13494 demand_empty_rest_of_line ();
13495 return;
13496 }
13497
13498 cur_proc_ptr->frame_offset = val;
13499 cur_proc_ptr->pc_reg = tc_get_register (0);
13500
13501 demand_empty_rest_of_line ();
13502#else
13503 s_ignore (ignore);
13504#endif /* MIPS_STABS_ELF */
13505}
13506
bdaaa2e1
KH
13507/* The .fmask and .mask directives. If the mdebug section is present
13508 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
252b5132 13509 embedded targets, s_mips_mask is used so that we can set the PDR
bdaaa2e1 13510 information correctly. We can't use the ecoff routines because they
252b5132
RH
13511 make reference to the ecoff symbol table (in the mdebug section). */
13512
13513static void
13514s_mips_mask (reg_type)
13515 char reg_type;
13516{
13517#ifdef MIPS_STABS_ELF
13518 long mask, off;
bdaaa2e1 13519
252b5132
RH
13520 if (cur_proc_ptr == (procS *) NULL)
13521 {
13522 as_warn (_(".mask/.fmask outside of .ent"));
13523 demand_empty_rest_of_line ();
13524 return;
13525 }
13526
13527 if (get_absolute_expression_and_terminator (&mask) != ',')
13528 {
13529 as_warn (_("Bad .mask/.fmask directive"));
13530 --input_line_pointer;
13531 demand_empty_rest_of_line ();
13532 return;
13533 }
13534
13535 off = get_absolute_expression ();
13536
13537 if (reg_type == 'F')
13538 {
13539 cur_proc_ptr->fpreg_mask = mask;
13540 cur_proc_ptr->fpreg_offset = off;
13541 }
13542 else
13543 {
13544 cur_proc_ptr->reg_mask = mask;
13545 cur_proc_ptr->reg_offset = off;
13546 }
13547
13548 demand_empty_rest_of_line ();
13549#else
13550 s_ignore (reg_type);
13551#endif /* MIPS_STABS_ELF */
13552}
13553
13554/* The .loc directive. */
13555
13556#if 0
13557static void
13558s_loc (x)
13559 int x;
13560{
13561 symbolS *symbolP;
13562 int lineno;
13563 int addroff;
13564
13565 assert (now_seg == text_section);
13566
13567 lineno = get_number ();
13568 addroff = frag_now_fix ();
13569
13570 symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
13571 S_SET_TYPE (symbolP, N_SLINE);
13572 S_SET_OTHER (symbolP, 0);
13573 S_SET_DESC (symbolP, lineno);
13574 symbolP->sy_segment = now_seg;
13575}
13576#endif
e7af610e
NC
13577
13578/* CPU name/ISA/number mapping table.
13579
13580 Entries are grouped by type. The first matching CPU or ISA entry
13581 gets chosen by CPU or ISA, so it should be the 'canonical' name
13582 for that type. Entries after that within the type are sorted
13583 alphabetically.
13584
13585 Case is ignored in comparison, so put the canonical entry in the
13586 appropriate case but everything else in lower case to ease eye pain. */
e972090a
NC
13587static const struct mips_cpu_info mips_cpu_info_table[] =
13588{
e7af610e
NC
13589 /* MIPS1 ISA */
13590 { "MIPS1", 1, ISA_MIPS1, CPU_R3000, },
13591 { "mips", 1, ISA_MIPS1, CPU_R3000, },
13592
13593 /* MIPS2 ISA */
13594 { "MIPS2", 1, ISA_MIPS2, CPU_R6000, },
13595
13596 /* MIPS3 ISA */
13597 { "MIPS3", 1, ISA_MIPS3, CPU_R4000, },
13598
13599 /* MIPS4 ISA */
13600 { "MIPS4", 1, ISA_MIPS4, CPU_R8000, },
13601
84ea6cf2
NC
13602 /* MIPS5 ISA */
13603 { "MIPS5", 1, ISA_MIPS5, CPU_MIPS5, },
13604 { "Generic-MIPS5", 0, ISA_MIPS5, CPU_MIPS5, },
13605
e7af610e
NC
13606 /* MIPS32 ISA */
13607 { "MIPS32", 1, ISA_MIPS32, CPU_MIPS32, },
3c02b2ab 13608 { "mipsisa32", 0, ISA_MIPS32, CPU_MIPS32, },
e7af610e 13609 { "Generic-MIPS32", 0, ISA_MIPS32, CPU_MIPS32, },
3c02b2ab
EC
13610 { "4kc", 0, ISA_MIPS32, CPU_MIPS32, },
13611 { "4km", 0, ISA_MIPS32, CPU_MIPS32, },
13612 { "4kp", 0, ISA_MIPS32, CPU_MIPS32, },
13613
13614 /* For historical reasons. */
13615 { "MIPS64", 1, ISA_MIPS3, CPU_R4000, },
e7af610e 13616
84ea6cf2 13617 /* MIPS64 ISA */
3c02b2ab 13618 { "mipsisa64", 1, ISA_MIPS64, CPU_MIPS64, },
84ea6cf2 13619 { "Generic-MIPS64", 0, ISA_MIPS64, CPU_MIPS64, },
3c02b2ab
EC
13620 { "5kc", 0, ISA_MIPS64, CPU_MIPS64, },
13621 { "20kc", 0, ISA_MIPS64, CPU_MIPS64, },
e7af610e
NC
13622
13623 /* R2000 CPU */
13624 { "R2000", 0, ISA_MIPS1, CPU_R2000, },
13625 { "2000", 0, ISA_MIPS1, CPU_R2000, },
13626 { "2k", 0, ISA_MIPS1, CPU_R2000, },
13627 { "r2k", 0, ISA_MIPS1, CPU_R2000, },
13628
13629 /* R3000 CPU */
13630 { "R3000", 0, ISA_MIPS1, CPU_R3000, },
13631 { "3000", 0, ISA_MIPS1, CPU_R3000, },
13632 { "3k", 0, ISA_MIPS1, CPU_R3000, },
13633 { "r3k", 0, ISA_MIPS1, CPU_R3000, },
13634
13635 /* TX3900 CPU */
13636 { "R3900", 0, ISA_MIPS1, CPU_R3900, },
13637 { "3900", 0, ISA_MIPS1, CPU_R3900, },
e972090a 13638 { "mipstx39", 0, ISA_MIPS1, CPU_R3900, },
e7af610e
NC
13639
13640 /* R4000 CPU */
13641 { "R4000", 0, ISA_MIPS3, CPU_R4000, },
13642 { "4000", 0, ISA_MIPS3, CPU_R4000, },
13643 { "4k", 0, ISA_MIPS3, CPU_R4000, }, /* beware */
13644 { "r4k", 0, ISA_MIPS3, CPU_R4000, },
13645
13646 /* R4010 CPU */
13647 { "R4010", 0, ISA_MIPS2, CPU_R4010, },
13648 { "4010", 0, ISA_MIPS2, CPU_R4010, },
13649
13650 /* R4400 CPU */
13651 { "R4400", 0, ISA_MIPS3, CPU_R4400, },
13652 { "4400", 0, ISA_MIPS3, CPU_R4400, },
13653
13654 /* R4600 CPU */
13655 { "R4600", 0, ISA_MIPS3, CPU_R4600, },
13656 { "4600", 0, ISA_MIPS3, CPU_R4600, },
13657 { "mips64orion", 0, ISA_MIPS3, CPU_R4600, },
13658 { "orion", 0, ISA_MIPS3, CPU_R4600, },
13659
13660 /* R4650 CPU */
13661 { "R4650", 0, ISA_MIPS3, CPU_R4650, },
13662 { "4650", 0, ISA_MIPS3, CPU_R4650, },
13663
13664 /* R6000 CPU */
13665 { "R6000", 0, ISA_MIPS2, CPU_R6000, },
13666 { "6000", 0, ISA_MIPS2, CPU_R6000, },
13667 { "6k", 0, ISA_MIPS2, CPU_R6000, },
13668 { "r6k", 0, ISA_MIPS2, CPU_R6000, },
13669
13670 /* R8000 CPU */
13671 { "R8000", 0, ISA_MIPS4, CPU_R8000, },
13672 { "8000", 0, ISA_MIPS4, CPU_R8000, },
13673 { "8k", 0, ISA_MIPS4, CPU_R8000, },
13674 { "r8k", 0, ISA_MIPS4, CPU_R8000, },
13675
13676 /* R10000 CPU */
13677 { "R10000", 0, ISA_MIPS4, CPU_R10000, },
13678 { "10000", 0, ISA_MIPS4, CPU_R10000, },
13679 { "10k", 0, ISA_MIPS4, CPU_R10000, },
13680 { "r10k", 0, ISA_MIPS4, CPU_R10000, },
13681
d1cf510e
NC
13682 /* R12000 CPU */
13683 { "R12000", 0, ISA_MIPS4, CPU_R12000, },
13684 { "12000", 0, ISA_MIPS4, CPU_R12000, },
13685 { "12k", 0, ISA_MIPS4, CPU_R12000, },
13686 { "r12k", 0, ISA_MIPS4, CPU_R12000, },
13687
e7af610e
NC
13688 /* VR4100 CPU */
13689 { "VR4100", 0, ISA_MIPS3, CPU_VR4100, },
13690 { "4100", 0, ISA_MIPS3, CPU_VR4100, },
13691 { "mips64vr4100", 0, ISA_MIPS3, CPU_VR4100, },
13692 { "r4100", 0, ISA_MIPS3, CPU_VR4100, },
13693
13694 /* VR4111 CPU */
13695 { "VR4111", 0, ISA_MIPS3, CPU_R4111, },
13696 { "4111", 0, ISA_MIPS3, CPU_R4111, },
13697 { "mips64vr4111", 0, ISA_MIPS3, CPU_R4111, },
13698 { "r4111", 0, ISA_MIPS3, CPU_R4111, },
13699
13700 /* VR4300 CPU */
13701 { "VR4300", 0, ISA_MIPS3, CPU_R4300, },
13702 { "4300", 0, ISA_MIPS3, CPU_R4300, },
13703 { "mips64vr4300", 0, ISA_MIPS3, CPU_R4300, },
13704 { "r4300", 0, ISA_MIPS3, CPU_R4300, },
13705
13706 /* VR5000 CPU */
13707 { "VR5000", 0, ISA_MIPS4, CPU_R5000, },
13708 { "5000", 0, ISA_MIPS4, CPU_R5000, },
13709 { "5k", 0, ISA_MIPS4, CPU_R5000, },
13710 { "mips64vr5000", 0, ISA_MIPS4, CPU_R5000, },
13711 { "r5000", 0, ISA_MIPS4, CPU_R5000, },
13712 { "r5200", 0, ISA_MIPS4, CPU_R5000, },
18ae5d72 13713 { "rm5200", 0, ISA_MIPS4, CPU_R5000, },
e7af610e 13714 { "r5230", 0, ISA_MIPS4, CPU_R5000, },
18ae5d72 13715 { "rm5230", 0, ISA_MIPS4, CPU_R5000, },
e7af610e 13716 { "r5231", 0, ISA_MIPS4, CPU_R5000, },
18ae5d72 13717 { "rm5231", 0, ISA_MIPS4, CPU_R5000, },
e7af610e 13718 { "r5261", 0, ISA_MIPS4, CPU_R5000, },
18ae5d72 13719 { "rm5261", 0, ISA_MIPS4, CPU_R5000, },
e7af610e 13720 { "r5721", 0, ISA_MIPS4, CPU_R5000, },
18ae5d72 13721 { "rm5721", 0, ISA_MIPS4, CPU_R5000, },
e7af610e
NC
13722 { "r5k", 0, ISA_MIPS4, CPU_R5000, },
13723 { "r7000", 0, ISA_MIPS4, CPU_R5000, },
13724
2e4acd24 13725 /* Broadcom SB-1 CPU */
c6c98b38 13726 { "SB-1", 0, ISA_MIPS64, CPU_SB1, },
e972090a 13727 { "sb-1250", 0, ISA_MIPS64, CPU_SB1, },
c6c98b38
NC
13728 { "sb1", 0, ISA_MIPS64, CPU_SB1, },
13729 { "sb1250", 0, ISA_MIPS64, CPU_SB1, },
13730
beae10d5 13731 /* End marker. */
e7af610e
NC
13732 { NULL, 0, 0, 0, },
13733};
13734
13735static const struct mips_cpu_info *
13736mips_cpu_info_from_name (name)
13737 const char *name;
13738{
13739 int i;
13740
13741 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
beae10d5 13742 if (strcasecmp (name, mips_cpu_info_table[i].name) == 0)
e7af610e
NC
13743 return (&mips_cpu_info_table[i]);
13744
e972090a 13745 return NULL;
e7af610e
NC
13746}
13747
13748static const struct mips_cpu_info *
13749mips_cpu_info_from_isa (isa)
13750 int isa;
13751{
13752 int i;
13753
13754 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13755 if (mips_cpu_info_table[i].is_isa
13756 && isa == mips_cpu_info_table[i].isa)
13757 return (&mips_cpu_info_table[i]);
13758
e972090a 13759 return NULL;
e7af610e
NC
13760}
13761
13762static const struct mips_cpu_info *
13763mips_cpu_info_from_cpu (cpu)
13764 int cpu;
13765{
13766 int i;
13767
13768 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13769 if (!mips_cpu_info_table[i].is_isa
13770 && cpu == mips_cpu_info_table[i].cpu)
13771 return (&mips_cpu_info_table[i]);
13772
e972090a 13773 return NULL;
e7af610e 13774}
This page took 0.934522 seconds and 4 git commands to generate.