* emulparams/elf64btsmip.sh: n64 replaces .reginfo with .MIPS.options.
[deliverable/binutils-gdb.git] / gas / config / tc-mips.c
CommitLineData
252b5132 1/* tc-mips.c -- assemble code for a MIPS chip.
f7e42eb4 2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
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;
252b5132
RH
144 /* Whether we are assembling for the mips16 processor. 0 if we are
145 not, 1 if we are, and -1 if the value has not been initialized.
146 Changed by `.set mips16' and `.set nomips16', and the -mips16 and
147 -nomips16 command line options, and the default CPU. */
148 int mips16;
149 /* Non-zero if we should not reorder instructions. Changed by `.set
150 reorder' and `.set noreorder'. */
151 int noreorder;
152 /* Non-zero if we should not permit the $at ($1) register to be used
153 in instructions. Changed by `.set at' and `.set noat'. */
154 int noat;
155 /* Non-zero if we should warn when a macro instruction expands into
156 more than one machine instruction. Changed by `.set nomacro' and
157 `.set macro'. */
158 int warn_about_macros;
159 /* Non-zero if we should not move instructions. Changed by `.set
160 move', `.set volatile', `.set nomove', and `.set novolatile'. */
161 int nomove;
162 /* Non-zero if we should not optimize branches by moving the target
163 of the branch into the delay slot. Actually, we don't perform
164 this optimization anyhow. Changed by `.set bopt' and `.set
165 nobopt'. */
166 int nobopt;
167 /* Non-zero if we should not autoextend mips16 instructions.
168 Changed by `.set autoextend' and `.set noautoextend'. */
169 int noautoextend;
a325df1d
TS
170 /* Restrict general purpose registers and floating point registers
171 to 32 bit. This is initially determined when -mgp32 or -mfp32
172 is passed but can changed if the assembler code uses .set mipsN. */
173 int gp32;
174 int fp32;
175 /* The ABI currently in use. This is changed by .set mipsN to loosen
176 restrictions and doesn't affect the whole file. */
177 enum mips_abi_level abi;
252b5132
RH
178};
179
a325df1d 180/* True if -mgp32 was passed. */
a8e8e863 181static int file_mips_gp32 = -1;
a325df1d
TS
182
183/* True if -mfp32 was passed. */
a8e8e863 184static int file_mips_fp32 = -1;
a325df1d 185
252b5132 186/* This is the struct we use to hold the current set of options. Note
e7af610e
NC
187 that we must set the isa field to ISA_UNKNOWN and the mips16 field to
188 -1 to indicate that they have not been initialized. */
252b5132 189
e972090a
NC
190static struct mips_set_options mips_opts =
191{
1f25f5d3 192 ISA_UNKNOWN, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, NO_ABI
e7af610e 193};
252b5132
RH
194
195/* These variables are filled in with the masks of registers used.
196 The object format code reads them and puts them in the appropriate
197 place. */
198unsigned long mips_gprmask;
199unsigned long mips_cprmask[4];
200
201/* MIPS ISA we are using for this output file. */
e7af610e 202static int file_mips_isa = ISA_UNKNOWN;
252b5132 203
1f25f5d3
CD
204/* True if -mips3d was passed or implied by arguments passed on the
205 command line (e.g., by -march). */
206static int file_ase_mips3d;
207
ec68c924 208/* The argument of the -mcpu= flag. Historical for code generation. */
e7af610e 209static int mips_cpu = CPU_UNKNOWN;
252b5132 210
ec68c924
EC
211/* The argument of the -march= flag. The architecture we are assembling. */
212static int mips_arch = CPU_UNKNOWN;
213
214/* The argument of the -mtune= flag. The architecture for which we
215 are optimizing. */
216static int mips_tune = CPU_UNKNOWN;
217
2f4dcb11 218/* Whether we should mark the file EABI64 or EABI32. */
252b5132
RH
219static int mips_eabi64 = 0;
220
221/* If they asked for mips1 or mips2 and a cpu that is
bdaaa2e1 222 mips3 or greater, then mark the object file 32BITMODE. */
252b5132
RH
223static int mips_32bitmode = 0;
224
9ce8a5dd
GRK
225/* Some ISA's have delay slots for instructions which read or write
226 from a coprocessor (eg. mips1-mips3); some don't (eg mips4).
bdaaa2e1 227 Return true if instructions marked INSN_LOAD_COPROC_DELAY,
9ce8a5dd
GRK
228 INSN_COPROC_MOVE_DELAY, or INSN_WRITE_COND_CODE actually have a
229 delay slot in this ISA. The uses of this macro assume that any
230 ISA that has delay slots for one of these, has them for all. They
231 also assume that ISAs which don't have delays for these insns, don't
bdaaa2e1 232 have delays for the INSN_LOAD_MEMORY_DELAY instructions either. */
9ce8a5dd 233#define ISA_HAS_COPROC_DELAYS(ISA) ( \
e7af610e
NC
234 (ISA) == ISA_MIPS1 \
235 || (ISA) == ISA_MIPS2 \
236 || (ISA) == ISA_MIPS3 \
9ce8a5dd
GRK
237 )
238
bdaaa2e1 239/* Return true if ISA supports 64 bit gp register instructions. */
9ce8a5dd 240#define ISA_HAS_64BIT_REGS(ISA) ( \
e7af610e
NC
241 (ISA) == ISA_MIPS3 \
242 || (ISA) == ISA_MIPS4 \
84ea6cf2 243 || (ISA) == ISA_MIPS5 \
d1cf510e 244 || (ISA) == ISA_MIPS64 \
9ce8a5dd
GRK
245 )
246
e013f690 247#define HAVE_32BIT_GPRS \
a325df1d
TS
248 (mips_opts.gp32 \
249 || mips_opts.abi == O32_ABI \
e013f690 250 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
ca4e0257 251
e013f690 252#define HAVE_32BIT_FPRS \
a325df1d
TS
253 (mips_opts.fp32 \
254 || mips_opts.abi == O32_ABI \
e013f690 255 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
ca4e0257
RS
256
257#define HAVE_64BIT_GPRS (! HAVE_32BIT_GPRS)
258#define HAVE_64BIT_FPRS (! HAVE_32BIT_FPRS)
259
a325df1d 260#define HAVE_NEWABI (mips_opts.abi == N32_ABI || mips_opts.abi == N64_ABI)
e013f690 261
a325df1d 262#define HAVE_64BIT_OBJECTS (mips_opts.abi == N64_ABI)
e013f690
TS
263
264/* We can only have 64bit addresses if the object file format
265 supports it. */
afdbd6d0
CD
266#define HAVE_32BIT_ADDRESSES \
267 (HAVE_32BIT_GPRS \
268 || ((bfd_arch_bits_per_address (stdoutput) == 32 \
269 || ! HAVE_64BIT_OBJECTS) \
270 && mips_pic != EMBEDDED_PIC))
e013f690
TS
271
272#define HAVE_64BIT_ADDRESSES (! HAVE_32BIT_ADDRESSES)
ca4e0257 273
1f25f5d3
CD
274/* Return true if the given CPU supports the MIPS3D ASE. */
275#define CPU_HAS_MIPS3D(cpu) ((cpu) == CPU_SB1 \
276 )
277
bdaaa2e1 278/* Whether the processor uses hardware interlocks to protect
252b5132 279 reads from the HI and LO registers, and thus does not
ec68c924 280 require nops to be inserted. */
252b5132 281
ec68c924 282#define hilo_interlocks (mips_arch == CPU_R4010 \
0a758a12 283 || mips_arch == CPU_SB1 \
252b5132
RH
284 )
285
286/* Whether the processor uses hardware interlocks to protect reads
287 from the GPRs, and thus does not require nops to be inserted. */
288#define gpr_interlocks \
e7af610e 289 (mips_opts.isa != ISA_MIPS1 \
ec68c924 290 || mips_arch == CPU_R3900)
252b5132
RH
291
292/* As with other "interlocks" this is used by hardware that has FP
293 (co-processor) interlocks. */
bdaaa2e1 294/* Itbl support may require additional care here. */
ec68c924 295#define cop_interlocks (mips_arch == CPU_R4300 \
0a758a12 296 || mips_arch == CPU_SB1 \
252b5132
RH
297 )
298
6b76fefe
CM
299/* Is this a mfhi or mflo instruction? */
300#define MF_HILO_INSN(PINFO) \
301 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
302
252b5132
RH
303/* MIPS PIC level. */
304
e972090a
NC
305enum mips_pic_level
306{
252b5132
RH
307 /* Do not generate PIC code. */
308 NO_PIC,
309
310 /* Generate PIC code as in Irix 4. This is not implemented, and I'm
311 not sure what it is supposed to do. */
312 IRIX4_PIC,
313
314 /* Generate PIC code as in the SVR4 MIPS ABI. */
315 SVR4_PIC,
316
317 /* Generate PIC code without using a global offset table: the data
318 segment has a maximum size of 64K, all data references are off
319 the $gp register, and all text references are PC relative. This
320 is used on some embedded systems. */
321 EMBEDDED_PIC
322};
323
324static enum mips_pic_level mips_pic;
325
39c0a331
L
326/* Warn about all NOPS that the assembler generates. */
327static int warn_nops = 0;
328
252b5132
RH
329/* 1 if we should generate 32 bit offsets from the GP register in
330 SVR4_PIC mode. Currently has no meaning in other modes. */
331static int mips_big_got;
332
333/* 1 if trap instructions should used for overflow rather than break
334 instructions. */
335static int mips_trap;
336
119d663a 337/* 1 if double width floating point constants should not be constructed
b6ff326e 338 by assembling two single width halves into two single width floating
119d663a
NC
339 point registers which just happen to alias the double width destination
340 register. On some architectures this aliasing can be disabled by a bit
d547a75e 341 in the status register, and the setting of this bit cannot be determined
119d663a
NC
342 automatically at assemble time. */
343static int mips_disable_float_construction;
344
252b5132
RH
345/* Non-zero if any .set noreorder directives were used. */
346
347static int mips_any_noreorder;
348
6b76fefe
CM
349/* Non-zero if nops should be inserted when the register referenced in
350 an mfhi/mflo instruction is read in the next two instructions. */
351static int mips_7000_hilo_fix;
352
252b5132 353/* The size of the small data section. */
156c2f8b 354static unsigned int g_switch_value = 8;
252b5132
RH
355/* Whether the -G option was used. */
356static int g_switch_seen = 0;
357
358#define N_RMASK 0xc4
359#define N_VFP 0xd4
360
361/* If we can determine in advance that GP optimization won't be
362 possible, we can skip the relaxation stuff that tries to produce
363 GP-relative references. This makes delay slot optimization work
364 better.
365
366 This function can only provide a guess, but it seems to work for
fba2b7f9
GK
367 gcc output. It needs to guess right for gcc, otherwise gcc
368 will put what it thinks is a GP-relative instruction in a branch
369 delay slot.
252b5132
RH
370
371 I don't know if a fix is needed for the SVR4_PIC mode. I've only
372 fixed it for the non-PIC mode. KR 95/04/07 */
373static int nopic_need_relax PARAMS ((symbolS *, int));
374
375/* handle of the OPCODE hash table */
376static struct hash_control *op_hash = NULL;
377
378/* The opcode hash table we use for the mips16. */
379static struct hash_control *mips16_op_hash = NULL;
380
381/* This array holds the chars that always start a comment. If the
382 pre-processor is disabled, these aren't very useful */
383const char comment_chars[] = "#";
384
385/* This array holds the chars that only start a comment at the beginning of
386 a line. If the line seems to have the form '# 123 filename'
387 .line and .file directives will appear in the pre-processed output */
388/* Note that input_file.c hand checks for '#' at the beginning of the
389 first line of the input file. This is because the compiler outputs
bdaaa2e1 390 #NO_APP at the beginning of its output. */
252b5132
RH
391/* Also note that C style comments are always supported. */
392const char line_comment_chars[] = "#";
393
bdaaa2e1 394/* This array holds machine specific line separator characters. */
63a0b638 395const char line_separator_chars[] = ";";
252b5132
RH
396
397/* Chars that can be used to separate mant from exp in floating point nums */
398const char EXP_CHARS[] = "eE";
399
400/* Chars that mean this number is a floating point constant */
401/* As in 0f12.456 */
402/* or 0d1.2345e12 */
403const char FLT_CHARS[] = "rRsSfFdDxXpP";
404
405/* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
406 changed in read.c . Ideally it shouldn't have to know about it at all,
407 but nothing is ideal around here.
408 */
409
410static char *insn_error;
411
412static int auto_align = 1;
413
414/* When outputting SVR4 PIC code, the assembler needs to know the
415 offset in the stack frame from which to restore the $gp register.
416 This is set by the .cprestore pseudo-op, and saved in this
417 variable. */
418static offsetT mips_cprestore_offset = -1;
419
6478892d
TS
420/* Similiar for NewABI PIC code, where $gp is callee-saved. NewABI has some
421 more optimizations, it can use a register value instead of a memory-saved
956cd1d6 422 offset and even an other register than $gp as global pointer. */
6478892d
TS
423static offsetT mips_cpreturn_offset = -1;
424static int mips_cpreturn_register = -1;
425static int mips_gp_register = GP;
426
7a621144
DJ
427/* Whether mips_cprestore_offset has been set in the current function
428 (or whether it has already been warned about, if not). */
429static int mips_cprestore_valid = 0;
430
252b5132
RH
431/* This is the register which holds the stack frame, as set by the
432 .frame pseudo-op. This is needed to implement .cprestore. */
433static int mips_frame_reg = SP;
434
7a621144
DJ
435/* Whether mips_frame_reg has been set in the current function
436 (or whether it has already been warned about, if not). */
437static int mips_frame_reg_valid = 0;
438
252b5132
RH
439/* To output NOP instructions correctly, we need to keep information
440 about the previous two instructions. */
441
442/* Whether we are optimizing. The default value of 2 means to remove
443 unneeded NOPs and swap branch instructions when possible. A value
444 of 1 means to not swap branches. A value of 0 means to always
445 insert NOPs. */
446static int mips_optimize = 2;
447
448/* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
449 equivalent to seeing no -g option at all. */
450static int mips_debug = 0;
451
452/* The previous instruction. */
453static struct mips_cl_insn prev_insn;
454
455/* The instruction before prev_insn. */
456static struct mips_cl_insn prev_prev_insn;
457
458/* If we don't want information for prev_insn or prev_prev_insn, we
459 point the insn_mo field at this dummy integer. */
43841e91 460static const struct mips_opcode dummy_opcode = { NULL, NULL, 0, 0, 0, 0 };
252b5132
RH
461
462/* Non-zero if prev_insn is valid. */
463static int prev_insn_valid;
464
465/* The frag for the previous instruction. */
466static struct frag *prev_insn_frag;
467
468/* The offset into prev_insn_frag for the previous instruction. */
469static long prev_insn_where;
470
471/* The reloc type for the previous instruction, if any. */
f6688943 472static bfd_reloc_code_real_type prev_insn_reloc_type[3];
252b5132
RH
473
474/* The reloc for the previous instruction, if any. */
f6688943 475static fixS *prev_insn_fixp[3];
252b5132
RH
476
477/* Non-zero if the previous instruction was in a delay slot. */
478static int prev_insn_is_delay_slot;
479
480/* Non-zero if the previous instruction was in a .set noreorder. */
481static int prev_insn_unreordered;
482
483/* Non-zero if the previous instruction uses an extend opcode (if
484 mips16). */
485static int prev_insn_extended;
486
487/* Non-zero if the previous previous instruction was in a .set
488 noreorder. */
489static int prev_prev_insn_unreordered;
490
491/* If this is set, it points to a frag holding nop instructions which
492 were inserted before the start of a noreorder section. If those
493 nops turn out to be unnecessary, the size of the frag can be
494 decreased. */
495static fragS *prev_nop_frag;
496
497/* The number of nop instructions we created in prev_nop_frag. */
498static int prev_nop_frag_holds;
499
500/* The number of nop instructions that we know we need in
bdaaa2e1 501 prev_nop_frag. */
252b5132
RH
502static int prev_nop_frag_required;
503
504/* The number of instructions we've seen since prev_nop_frag. */
505static int prev_nop_frag_since;
506
507/* For ECOFF and ELF, relocations against symbols are done in two
508 parts, with a HI relocation and a LO relocation. Each relocation
509 has only 16 bits of space to store an addend. This means that in
510 order for the linker to handle carries correctly, it must be able
511 to locate both the HI and the LO relocation. This means that the
512 relocations must appear in order in the relocation table.
513
514 In order to implement this, we keep track of each unmatched HI
515 relocation. We then sort them so that they immediately precede the
bdaaa2e1 516 corresponding LO relocation. */
252b5132 517
e972090a
NC
518struct mips_hi_fixup
519{
252b5132
RH
520 /* Next HI fixup. */
521 struct mips_hi_fixup *next;
522 /* This fixup. */
523 fixS *fixp;
524 /* The section this fixup is in. */
525 segT seg;
526};
527
528/* The list of unmatched HI relocs. */
529
530static struct mips_hi_fixup *mips_hi_fixup_list;
531
532/* Map normal MIPS register numbers to mips16 register numbers. */
533
534#define X ILLEGAL_REG
e972090a
NC
535static const int mips32_to_16_reg_map[] =
536{
252b5132
RH
537 X, X, 2, 3, 4, 5, 6, 7,
538 X, X, X, X, X, X, X, X,
539 0, 1, X, X, X, X, X, X,
540 X, X, X, X, X, X, X, X
541};
542#undef X
543
544/* Map mips16 register numbers to normal MIPS register numbers. */
545
e972090a
NC
546static const unsigned int mips16_to_32_reg_map[] =
547{
252b5132
RH
548 16, 17, 2, 3, 4, 5, 6, 7
549};
550\f
551/* Since the MIPS does not have multiple forms of PC relative
552 instructions, we do not have to do relaxing as is done on other
553 platforms. However, we do have to handle GP relative addressing
554 correctly, which turns out to be a similar problem.
555
556 Every macro that refers to a symbol can occur in (at least) two
557 forms, one with GP relative addressing and one without. For
558 example, loading a global variable into a register generally uses
559 a macro instruction like this:
560 lw $4,i
561 If i can be addressed off the GP register (this is true if it is in
562 the .sbss or .sdata section, or if it is known to be smaller than
563 the -G argument) this will generate the following instruction:
564 lw $4,i($gp)
565 This instruction will use a GPREL reloc. If i can not be addressed
566 off the GP register, the following instruction sequence will be used:
567 lui $at,i
568 lw $4,i($at)
569 In this case the first instruction will have a HI16 reloc, and the
570 second reloc will have a LO16 reloc. Both relocs will be against
571 the symbol i.
572
573 The issue here is that we may not know whether i is GP addressable
574 until after we see the instruction that uses it. Therefore, we
575 want to be able to choose the final instruction sequence only at
576 the end of the assembly. This is similar to the way other
577 platforms choose the size of a PC relative instruction only at the
578 end of assembly.
579
580 When generating position independent code we do not use GP
581 addressing in quite the same way, but the issue still arises as
582 external symbols and local symbols must be handled differently.
583
584 We handle these issues by actually generating both possible
585 instruction sequences. The longer one is put in a frag_var with
586 type rs_machine_dependent. We encode what to do with the frag in
587 the subtype field. We encode (1) the number of existing bytes to
588 replace, (2) the number of new bytes to use, (3) the offset from
589 the start of the existing bytes to the first reloc we must generate
590 (that is, the offset is applied from the start of the existing
591 bytes after they are replaced by the new bytes, if any), (4) the
592 offset from the start of the existing bytes to the second reloc,
593 (5) whether a third reloc is needed (the third reloc is always four
594 bytes after the second reloc), and (6) whether to warn if this
595 variant is used (this is sometimes needed if .set nomacro or .set
596 noat is in effect). All these numbers are reasonably small.
597
598 Generating two instruction sequences must be handled carefully to
599 ensure that delay slots are handled correctly. Fortunately, there
600 are a limited number of cases. When the second instruction
601 sequence is generated, append_insn is directed to maintain the
602 existing delay slot information, so it continues to apply to any
603 code after the second instruction sequence. This means that the
604 second instruction sequence must not impose any requirements not
605 required by the first instruction sequence.
606
607 These variant frags are then handled in functions called by the
608 machine independent code. md_estimate_size_before_relax returns
609 the final size of the frag. md_convert_frag sets up the final form
610 of the frag. tc_gen_reloc adjust the first reloc and adds a second
611 one if needed. */
612#define RELAX_ENCODE(old, new, reloc1, reloc2, reloc3, warn) \
613 ((relax_substateT) \
614 (((old) << 23) \
615 | ((new) << 16) \
616 | (((reloc1) + 64) << 9) \
617 | (((reloc2) + 64) << 2) \
618 | ((reloc3) ? (1 << 1) : 0) \
619 | ((warn) ? 1 : 0)))
620#define RELAX_OLD(i) (((i) >> 23) & 0x7f)
621#define RELAX_NEW(i) (((i) >> 16) & 0x7f)
9a41af64
TS
622#define RELAX_RELOC1(i) ((valueT) (((i) >> 9) & 0x7f) - 64)
623#define RELAX_RELOC2(i) ((valueT) (((i) >> 2) & 0x7f) - 64)
252b5132
RH
624#define RELAX_RELOC3(i) (((i) >> 1) & 1)
625#define RELAX_WARN(i) ((i) & 1)
626
627/* For mips16 code, we use an entirely different form of relaxation.
628 mips16 supports two versions of most instructions which take
629 immediate values: a small one which takes some small value, and a
630 larger one which takes a 16 bit value. Since branches also follow
631 this pattern, relaxing these values is required.
632
633 We can assemble both mips16 and normal MIPS code in a single
634 object. Therefore, we need to support this type of relaxation at
635 the same time that we support the relaxation described above. We
636 use the high bit of the subtype field to distinguish these cases.
637
638 The information we store for this type of relaxation is the
639 argument code found in the opcode file for this relocation, whether
640 the user explicitly requested a small or extended form, and whether
641 the relocation is in a jump or jal delay slot. That tells us the
642 size of the value, and how it should be stored. We also store
643 whether the fragment is considered to be extended or not. We also
644 store whether this is known to be a branch to a different section,
645 whether we have tried to relax this frag yet, and whether we have
646 ever extended a PC relative fragment because of a shift count. */
647#define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
648 (0x80000000 \
649 | ((type) & 0xff) \
650 | ((small) ? 0x100 : 0) \
651 | ((ext) ? 0x200 : 0) \
652 | ((dslot) ? 0x400 : 0) \
653 | ((jal_dslot) ? 0x800 : 0))
654#define RELAX_MIPS16_P(i) (((i) & 0x80000000) != 0)
655#define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
656#define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
657#define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
658#define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
659#define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
660#define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
661#define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
662#define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
663#define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
664#define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
665#define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
666\f
667/* Prototypes for static functions. */
668
669#ifdef __STDC__
670#define internalError() \
671 as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
672#else
673#define internalError() as_fatal (_("MIPS internal Error"));
674#endif
675
676enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
677
678static int insn_uses_reg PARAMS ((struct mips_cl_insn *ip,
679 unsigned int reg, enum mips_regclass class));
156c2f8b 680static int reg_needs_delay PARAMS ((unsigned int));
252b5132
RH
681static void mips16_mark_labels PARAMS ((void));
682static void append_insn PARAMS ((char *place,
683 struct mips_cl_insn * ip,
684 expressionS * p,
f6688943 685 bfd_reloc_code_real_type *r,
252b5132
RH
686 boolean));
687static void mips_no_prev_insn PARAMS ((int));
688static void mips_emit_delays PARAMS ((boolean));
689#ifdef USE_STDARG
690static void macro_build PARAMS ((char *place, int *counter, expressionS * ep,
691 const char *name, const char *fmt,
692 ...));
693#else
694static void macro_build ();
695#endif
696static void mips16_macro_build PARAMS ((char *, int *, expressionS *,
697 const char *, const char *,
698 va_list));
699static void macro_build_lui PARAMS ((char *place, int *counter,
700 expressionS * ep, int regnum));
701static void set_at PARAMS ((int *counter, int reg, int unsignedp));
702static void check_absolute_expr PARAMS ((struct mips_cl_insn * ip,
703 expressionS *));
704static void load_register PARAMS ((int *, int, expressionS *, int));
d6bc6245 705static void load_address PARAMS ((int *, int, expressionS *, int, int *));
ea1fb5dc 706static void move_register PARAMS ((int *, int, int));
252b5132
RH
707static void macro PARAMS ((struct mips_cl_insn * ip));
708static void mips16_macro PARAMS ((struct mips_cl_insn * ip));
709#ifdef LOSING_COMPILER
710static void macro2 PARAMS ((struct mips_cl_insn * ip));
711#endif
712static void mips_ip PARAMS ((char *str, struct mips_cl_insn * ip));
713static void mips16_ip PARAMS ((char *str, struct mips_cl_insn * ip));
714static void mips16_immed PARAMS ((char *, unsigned int, int, offsetT, boolean,
715 boolean, boolean, unsigned long *,
716 boolean *, unsigned short *));
394f9b3a 717static int my_getPercentOp PARAMS ((char **, unsigned int *, int *));
ad8d3bb3
TS
718static int my_getSmallParser PARAMS ((char **, unsigned int *, int *));
719static int my_getSmallExpression PARAMS ((expressionS *, char *));
720static void my_getExpression PARAMS ((expressionS *, char *));
ae948b86 721#ifdef OBJ_ELF
e013f690 722static int support_64bit_objects PARAMS((void));
ae948b86 723#endif
252b5132
RH
724static symbolS *get_symbol PARAMS ((void));
725static void mips_align PARAMS ((int to, int fill, symbolS *label));
726static void s_align PARAMS ((int));
727static void s_change_sec PARAMS ((int));
728static void s_cons PARAMS ((int));
729static void s_float_cons PARAMS ((int));
730static void s_mips_globl PARAMS ((int));
731static void s_option PARAMS ((int));
732static void s_mipsset PARAMS ((int));
733static void s_abicalls PARAMS ((int));
734static void s_cpload PARAMS ((int));
6478892d
TS
735static void s_cpsetup PARAMS ((int));
736static void s_cplocal PARAMS ((int));
252b5132 737static void s_cprestore PARAMS ((int));
6478892d
TS
738static void s_cpreturn PARAMS ((int));
739static void s_gpvalue PARAMS ((int));
252b5132
RH
740static void s_gpword PARAMS ((int));
741static void s_cpadd PARAMS ((int));
742static void s_insn PARAMS ((int));
743static void md_obj_begin PARAMS ((void));
744static void md_obj_end PARAMS ((void));
745static long get_number PARAMS ((void));
746static void s_mips_ent PARAMS ((int));
747static void s_mips_end PARAMS ((int));
748static void s_mips_frame PARAMS ((int));
749static void s_mips_mask PARAMS ((int));
750static void s_mips_stab PARAMS ((int));
751static void s_mips_weakext PARAMS ((int));
752static void s_file PARAMS ((int));
753static int mips16_extended_frag PARAMS ((fragS *, asection *, long));
e7af610e
NC
754static const char *mips_isa_to_str PARAMS ((int));
755static const char *mips_cpu_to_str PARAMS ((int));
252b5132 756static int validate_mips_insn PARAMS ((const struct mips_opcode *));
e87a0284 757static void show PARAMS ((FILE *, char *, int *, int *));
add55e1f
RS
758#ifdef OBJ_ELF
759static int mips_need_elf_addend_fixup PARAMS ((fixS *));
760#endif
e7af610e 761
ad8d3bb3 762/* Return values of my_getSmallExpression(). */
fb1b3232 763
ad8d3bb3 764enum small_ex_type
fb1b3232
TS
765{
766 S_EX_NONE = 0,
ad8d3bb3
TS
767 S_EX_REGISTER,
768
769 /* Direct relocation creation by %percent_op(). */
770 S_EX_HALF,
fb1b3232 771 S_EX_HI,
ad8d3bb3
TS
772 S_EX_LO,
773 S_EX_GP_REL,
774 S_EX_GOT,
775 S_EX_CALL16,
776 S_EX_GOT_DISP,
777 S_EX_GOT_PAGE,
778 S_EX_GOT_OFST,
779 S_EX_GOT_HI,
780 S_EX_GOT_LO,
781 S_EX_NEG,
fb1b3232
TS
782 S_EX_HIGHER,
783 S_EX_HIGHEST,
ad8d3bb3
TS
784 S_EX_CALL_HI,
785 S_EX_CALL_LO
fb1b3232
TS
786};
787
e7af610e
NC
788/* Table and functions used to map between CPU/ISA names, and
789 ISA levels, and CPU numbers. */
790
e972090a
NC
791struct mips_cpu_info
792{
e7af610e
NC
793 const char *name; /* CPU or ISA name. */
794 int is_isa; /* Is this an ISA? (If 0, a CPU.) */
795 int isa; /* ISA level. */
796 int cpu; /* CPU number (default CPU if ISA). */
797};
798
799static const struct mips_cpu_info *mips_cpu_info_from_name PARAMS ((const char *));
800static const struct mips_cpu_info *mips_cpu_info_from_isa PARAMS ((int));
801static const struct mips_cpu_info *mips_cpu_info_from_cpu PARAMS ((int));
252b5132
RH
802\f
803/* Pseudo-op table.
804
805 The following pseudo-ops from the Kane and Heinrich MIPS book
806 should be defined here, but are currently unsupported: .alias,
807 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
808
809 The following pseudo-ops from the Kane and Heinrich MIPS book are
810 specific to the type of debugging information being generated, and
811 should be defined by the object format: .aent, .begin, .bend,
812 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
813 .vreg.
814
815 The following pseudo-ops from the Kane and Heinrich MIPS book are
816 not MIPS CPU specific, but are also not specific to the object file
817 format. This file is probably the best place to define them, but
818 they are not currently supported: .asm0, .endr, .lab, .repeat,
819 .struct. */
820
e972090a
NC
821static const pseudo_typeS mips_pseudo_table[] =
822{
beae10d5 823 /* MIPS specific pseudo-ops. */
252b5132
RH
824 {"option", s_option, 0},
825 {"set", s_mipsset, 0},
826 {"rdata", s_change_sec, 'r'},
827 {"sdata", s_change_sec, 's'},
828 {"livereg", s_ignore, 0},
829 {"abicalls", s_abicalls, 0},
830 {"cpload", s_cpload, 0},
6478892d
TS
831 {"cpsetup", s_cpsetup, 0},
832 {"cplocal", s_cplocal, 0},
252b5132 833 {"cprestore", s_cprestore, 0},
6478892d
TS
834 {"cpreturn", s_cpreturn, 0},
835 {"gpvalue", s_gpvalue, 0},
252b5132
RH
836 {"gpword", s_gpword, 0},
837 {"cpadd", s_cpadd, 0},
838 {"insn", s_insn, 0},
839
beae10d5 840 /* Relatively generic pseudo-ops that happen to be used on MIPS
252b5132
RH
841 chips. */
842 {"asciiz", stringer, 1},
843 {"bss", s_change_sec, 'b'},
844 {"err", s_err, 0},
845 {"half", s_cons, 1},
846 {"dword", s_cons, 3},
847 {"weakext", s_mips_weakext, 0},
848
beae10d5 849 /* These pseudo-ops are defined in read.c, but must be overridden
252b5132
RH
850 here for one reason or another. */
851 {"align", s_align, 0},
852 {"byte", s_cons, 0},
853 {"data", s_change_sec, 'd'},
854 {"double", s_float_cons, 'd'},
855 {"float", s_float_cons, 'f'},
856 {"globl", s_mips_globl, 0},
857 {"global", s_mips_globl, 0},
858 {"hword", s_cons, 1},
859 {"int", s_cons, 2},
860 {"long", s_cons, 2},
861 {"octa", s_cons, 4},
862 {"quad", s_cons, 3},
863 {"short", s_cons, 1},
864 {"single", s_float_cons, 'f'},
865 {"stabn", s_mips_stab, 'n'},
866 {"text", s_change_sec, 't'},
867 {"word", s_cons, 2},
add56521
L
868
869#ifdef MIPS_STABS_ELF
870 { "extern", ecoff_directive_extern, 0},
871#endif
872
43841e91 873 { NULL, NULL, 0 },
252b5132
RH
874};
875
e972090a
NC
876static const pseudo_typeS mips_nonecoff_pseudo_table[] =
877{
beae10d5
KH
878 /* These pseudo-ops should be defined by the object file format.
879 However, a.out doesn't support them, so we have versions here. */
252b5132
RH
880 {"aent", s_mips_ent, 1},
881 {"bgnb", s_ignore, 0},
882 {"end", s_mips_end, 0},
883 {"endb", s_ignore, 0},
884 {"ent", s_mips_ent, 0},
885 {"file", s_file, 0},
886 {"fmask", s_mips_mask, 'F'},
887 {"frame", s_mips_frame, 0},
888 {"loc", s_ignore, 0},
889 {"mask", s_mips_mask, 'R'},
890 {"verstamp", s_ignore, 0},
43841e91 891 { NULL, NULL, 0 },
252b5132
RH
892};
893
894extern void pop_insert PARAMS ((const pseudo_typeS *));
895
896void
897mips_pop_insert ()
898{
899 pop_insert (mips_pseudo_table);
900 if (! ECOFF_DEBUGGING)
901 pop_insert (mips_nonecoff_pseudo_table);
902}
903\f
904/* Symbols labelling the current insn. */
905
e972090a
NC
906struct insn_label_list
907{
252b5132
RH
908 struct insn_label_list *next;
909 symbolS *label;
910};
911
912static struct insn_label_list *insn_labels;
913static struct insn_label_list *free_insn_labels;
914
915static void mips_clear_insn_labels PARAMS ((void));
916
917static inline void
918mips_clear_insn_labels ()
919{
920 register struct insn_label_list **pl;
921
922 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
923 ;
924 *pl = insn_labels;
925 insn_labels = NULL;
926}
927\f
928static char *expr_end;
929
930/* Expressions which appear in instructions. These are set by
931 mips_ip. */
932
933static expressionS imm_expr;
934static expressionS offset_expr;
935
936/* Relocs associated with imm_expr and offset_expr. */
937
f6688943
TS
938static bfd_reloc_code_real_type imm_reloc[3]
939 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
940static bfd_reloc_code_real_type offset_reloc[3]
941 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132
RH
942
943/* This is set by mips_ip if imm_reloc is an unmatched HI16_S reloc. */
944
945static boolean imm_unmatched_hi;
946
947/* These are set by mips16_ip if an explicit extension is used. */
948
949static boolean mips16_small, mips16_ext;
950
951#ifdef MIPS_STABS_ELF
952/* The pdr segment for per procedure frame/regmask info */
953
954static segT pdr_seg;
955#endif
956
e7af610e
NC
957static const char *
958mips_isa_to_str (isa)
959 int isa;
960{
961 const struct mips_cpu_info *ci;
962 static char s[20];
963
964 ci = mips_cpu_info_from_isa (isa);
965 if (ci != NULL)
966 return (ci->name);
967
968 sprintf (s, "ISA#%d", isa);
969 return s;
970}
971
972static const char *
156c2f8b
NC
973mips_cpu_to_str (cpu)
974 int cpu;
975{
e7af610e 976 const struct mips_cpu_info *ci;
156c2f8b 977 static char s[16];
e7af610e
NC
978
979 ci = mips_cpu_info_from_cpu (cpu);
980 if (ci != NULL)
981 return (ci->name);
982
983 sprintf (s, "CPU#%d", cpu);
984 return s;
156c2f8b
NC
985}
986
e013f690
TS
987/* The default target format to use. */
988
989const char *
990mips_target_format ()
991{
992 switch (OUTPUT_FLAVOR)
993 {
994 case bfd_target_aout_flavour:
995 return target_big_endian ? "a.out-mips-big" : "a.out-mips-little";
996 case bfd_target_ecoff_flavour:
997 return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
998 case bfd_target_coff_flavour:
999 return "pe-mips";
1000 case bfd_target_elf_flavour:
1001#ifdef TE_TMIPS
1002 /* This is traditional mips */
1003 return (target_big_endian
1004 ? (HAVE_64BIT_OBJECTS ? "elf64-tradbigmips"
1005 : "elf32-tradbigmips")
1006 : (HAVE_64BIT_OBJECTS ? "elf64-tradlittlemips"
1007 : "elf32-tradlittlemips"));
1008#else
1009 return (target_big_endian
1010 ? (HAVE_64BIT_OBJECTS ? "elf64-bigmips" : "elf32-bigmips")
1011 : (HAVE_64BIT_OBJECTS ? "elf64-littlemips"
1012 : "elf32-littlemips"));
1013#endif
1014 default:
1015 abort ();
1016 return NULL;
1017 }
1018}
1019
156c2f8b
NC
1020/* This function is called once, at assembler startup time. It should
1021 set up all the tables, etc. that the MD part of the assembler will need. */
1022
252b5132
RH
1023void
1024md_begin ()
1025{
252b5132 1026 register const char *retval = NULL;
156c2f8b 1027 int i = 0;
252b5132
RH
1028 const char *cpu;
1029 char *a = NULL;
1030 int broken = 0;
1031 int mips_isa_from_cpu;
e7af610e
NC
1032 int target_cpu_had_mips16 = 0;
1033 const struct mips_cpu_info *ci;
252b5132 1034
056350c6
NC
1035 /* GP relative stuff not working for PE */
1036 if (strncmp (TARGET_OS, "pe", 2) == 0
1037 && g_switch_value != 0)
1038 {
1039 if (g_switch_seen)
1040 as_bad (_("-G not supported in this configuration."));
1041 g_switch_value = 0;
1042 }
1043
252b5132
RH
1044 cpu = TARGET_CPU;
1045 if (strcmp (cpu + (sizeof TARGET_CPU) - 3, "el") == 0)
1046 {
1047 a = xmalloc (sizeof TARGET_CPU);
1048 strcpy (a, TARGET_CPU);
1049 a[(sizeof TARGET_CPU) - 3] = '\0';
1050 cpu = a;
1051 }
1052
e7af610e 1053 if (strncmp (cpu, "mips16", sizeof "mips16" - 1) == 0)
252b5132 1054 {
e7af610e
NC
1055 target_cpu_had_mips16 = 1;
1056 cpu += sizeof "mips16" - 1;
252b5132
RH
1057 }
1058
e7af610e
NC
1059 if (mips_opts.mips16 < 0)
1060 mips_opts.mips16 = target_cpu_had_mips16;
252b5132 1061
6dce9e24
TS
1062 /* Backward compatibility for historic -mcpu= option. Check for
1063 incompatible options, warn if -mcpu is used. */
1064 if (mips_cpu != CPU_UNKNOWN
1065 && mips_arch != CPU_UNKNOWN
1066 && mips_cpu != mips_arch)
1067 {
1068 as_fatal (_("The -mcpu option can't be used together with -march. "
1069 "Use -mtune instead of -mcpu."));
1070 }
1071
1072 if (mips_cpu != CPU_UNKNOWN
1073 && mips_tune != CPU_UNKNOWN
1074 && mips_cpu != mips_tune)
1075 {
1076 as_fatal (_("The -mcpu option can't be used together with -mtune. "
1077 "Use -march instead of -mcpu."));
1078 }
1079
a8e8e863
DJ
1080#if 1
1081 /* For backward compatibility, let -mipsN set various defaults. */
1082 /* This code should go away, to be replaced with something rather more
1083 draconian. Until GCC 3.1 has been released for some reasonable
1084 amount of time, however, we need to support this. */
1085 if (mips_opts.isa != ISA_UNKNOWN)
1086 {
1087 /* Translate -mipsN to the appropriate settings of file_mips_gp32
1088 and file_mips_fp32. Tag binaries as using the mipsN ISA. */
1089 if (file_mips_gp32 < 0)
1090 {
1091 if (ISA_HAS_64BIT_REGS (mips_opts.isa))
1092 file_mips_gp32 = 0;
1093 else
1094 file_mips_gp32 = 1;
1095 }
1096 if (file_mips_fp32 < 0)
1097 {
1098 if (ISA_HAS_64BIT_REGS (mips_opts.isa))
1099 file_mips_fp32 = 0;
1100 else
1101 file_mips_fp32 = 1;
1102 }
1103
1104 ci = mips_cpu_info_from_isa (mips_opts.isa);
1105 assert (ci != NULL);
1106 /* -mipsN has higher priority than -mcpu but lower than -march. */
1107 if (mips_arch == CPU_UNKNOWN)
1108 mips_arch = ci->cpu;
1109
1110 /* Default mips_abi. */
1111 if (mips_opts.abi == NO_ABI)
1112 {
1113 if (mips_opts.isa == ISA_MIPS1 || mips_opts.isa == ISA_MIPS2)
1114 mips_opts.abi = O32_ABI;
1115 else if (mips_opts.isa == ISA_MIPS3 || mips_opts.isa == ISA_MIPS4)
1116 mips_opts.abi = O64_ABI;
1117 }
1118 }
1119
6dce9e24
TS
1120 if (mips_arch == CPU_UNKNOWN && mips_cpu != CPU_UNKNOWN)
1121 {
1122 ci = mips_cpu_info_from_cpu (mips_cpu);
1123 assert (ci != NULL);
1124 mips_arch = ci->cpu;
1125 as_warn (_("The -mcpu option is deprecated. Please use -march and "
1126 "-mtune instead."));
1127 }
1128
a8e8e863
DJ
1129 /* Set tune from -mcpu, not from -mipsN. */
1130 if (mips_tune == CPU_UNKNOWN && mips_cpu != CPU_UNKNOWN)
1131 {
1132 ci = mips_cpu_info_from_cpu (mips_cpu);
1133 assert (ci != NULL);
1134 mips_tune = ci->cpu;
1135 }
1136
ec68c924 1137 /* At this point, mips_arch will either be CPU_UNKNOWN if no ARCH was
e7af610e
NC
1138 specified on the command line, or some other value if one was.
1139 Similarly, mips_opts.isa will be ISA_UNKNOWN if not specified on
1140 the command line, or will be set otherwise if one was. */
a8e8e863
DJ
1141
1142 if (mips_arch != CPU_UNKNOWN && mips_opts.isa != ISA_UNKNOWN)
1143 /* Handled above. */;
1144#else
1145 if (mips_arch == CPU_UNKNOWN && mips_cpu != CPU_UNKNOWN)
1146 {
1147 ci = mips_cpu_info_from_cpu (mips_cpu);
1148 assert (ci != NULL);
1149 mips_arch = ci->cpu;
1150 as_warn (_("The -mcpu option is deprecated. Please use -march and "
1151 "-mtune instead."));
1152 }
1153
1154 /* At this point, mips_arch will either be CPU_UNKNOWN if no ARCH was
1155 specified on the command line, or some other value if one was.
1156 Similarly, mips_opts.isa will be ISA_UNKNOWN if not specified on
1157 the command line, or will be set otherwise if one was. */
1158
ec68c924 1159 if (mips_arch != CPU_UNKNOWN && mips_opts.isa != ISA_UNKNOWN)
252b5132 1160 {
1ff54a84
TS
1161 /* We have to check if the isa is the default isa of arch. Otherwise
1162 we'll get invalid object file headers. */
1163 ci = mips_cpu_info_from_cpu (mips_arch);
1164 assert (ci != NULL);
1165 if (mips_opts.isa != ci->isa)
1166 {
1167 /* This really should be an error instead of a warning, but old
1168 compilers only have -mcpu which sets both arch and tune. For
1169 now, we discard arch and preserve tune. */
1170 as_warn (_("The -march option is incompatible to -mipsN and "
1171 "therefore ignored."));
1172 if (mips_tune == CPU_UNKNOWN)
1173 mips_tune = mips_arch;
1174 ci = mips_cpu_info_from_isa (mips_opts.isa);
1175 assert (ci != NULL);
1176 mips_arch = ci->cpu;
1177 }
252b5132 1178 }
a8e8e863 1179#endif
ec68c924 1180 else if (mips_arch != CPU_UNKNOWN && mips_opts.isa == ISA_UNKNOWN)
252b5132 1181 {
ec68c924
EC
1182 /* We have ARCH, we need ISA. */
1183 ci = mips_cpu_info_from_cpu (mips_arch);
e7af610e
NC
1184 assert (ci != NULL);
1185 mips_opts.isa = ci->isa;
1186 }
ec68c924 1187 else if (mips_arch == CPU_UNKNOWN && mips_opts.isa != ISA_UNKNOWN)
e7af610e 1188 {
ec68c924 1189 /* We have ISA, we need default ARCH. */
e7af610e
NC
1190 ci = mips_cpu_info_from_isa (mips_opts.isa);
1191 assert (ci != NULL);
ec68c924
EC
1192 mips_arch = ci->cpu;
1193 }
e7af610e
NC
1194 else
1195 {
ec68c924 1196 /* We need to set both ISA and ARCH from target cpu. */
e7af610e
NC
1197 ci = mips_cpu_info_from_name (cpu);
1198 if (ci == NULL)
beae10d5 1199 ci = mips_cpu_info_from_cpu (CPU_R3000);
e7af610e
NC
1200 assert (ci != NULL);
1201 mips_opts.isa = ci->isa;
ec68c924 1202 mips_arch = ci->cpu;
252b5132
RH
1203 }
1204
ec68c924
EC
1205 if (mips_tune == CPU_UNKNOWN)
1206 mips_tune = mips_arch;
1207
1208 ci = mips_cpu_info_from_cpu (mips_arch);
e7af610e
NC
1209 assert (ci != NULL);
1210 mips_isa_from_cpu = ci->isa;
1211
252b5132 1212 /* End of TARGET_CPU processing, get rid of malloced memory
bdaaa2e1 1213 if necessary. */
252b5132
RH
1214 cpu = NULL;
1215 if (a != NULL)
1216 {
156c2f8b
NC
1217 free (a);
1218 a = NULL;
252b5132
RH
1219 }
1220
e7af610e 1221 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
252b5132
RH
1222 as_bad (_("trap exception not supported at ISA 1"));
1223
1224 /* Set the EABI kind based on the ISA before the user gets
1225 to change the ISA with directives. This isn't really
bdaaa2e1 1226 the best, but then neither is basing the abi on the isa. */
9ce8a5dd 1227 if (ISA_HAS_64BIT_REGS (mips_opts.isa)
a325df1d 1228 && mips_opts.abi == EABI_ABI)
252b5132
RH
1229 mips_eabi64 = 1;
1230
e7af610e
NC
1231 /* If they asked for mips1 or mips2 and a cpu that is
1232 mips3 or greater, then mark the object file 32BITMODE. */
1233 if (mips_isa_from_cpu != ISA_UNKNOWN
1234 && ! ISA_HAS_64BIT_REGS (mips_opts.isa)
1235 && ISA_HAS_64BIT_REGS (mips_isa_from_cpu))
1236 mips_32bitmode = 1;
252b5132 1237
1f25f5d3
CD
1238 /* If the selected architecture includes support for ASEs, enable
1239 generation of code for them. */
1240 if (mips_opts.ase_mips3d == -1 && CPU_HAS_MIPS3D (mips_arch))
1241 mips_opts.ase_mips3d = 1;
1242
ec68c924 1243 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, mips_arch))
252b5132
RH
1244 as_warn (_("Could not set architecture and machine"));
1245
a8e8e863
DJ
1246 if (file_mips_gp32 < 0)
1247 file_mips_gp32 = 0;
1248 if (file_mips_fp32 < 0)
1249 file_mips_fp32 = 0;
1250
252b5132 1251 file_mips_isa = mips_opts.isa;
a325df1d 1252 file_mips_abi = mips_opts.abi;
1f25f5d3 1253 file_ase_mips3d = mips_opts.ase_mips3d;
a325df1d
TS
1254 mips_opts.gp32 = file_mips_gp32;
1255 mips_opts.fp32 = file_mips_fp32;
252b5132
RH
1256
1257 op_hash = hash_new ();
1258
1259 for (i = 0; i < NUMOPCODES;)
1260 {
1261 const char *name = mips_opcodes[i].name;
1262
1263 retval = hash_insert (op_hash, name, (PTR) &mips_opcodes[i]);
1264 if (retval != NULL)
1265 {
1266 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1267 mips_opcodes[i].name, retval);
1268 /* Probably a memory allocation problem? Give up now. */
1269 as_fatal (_("Broken assembler. No assembly attempted."));
1270 }
1271 do
1272 {
1273 if (mips_opcodes[i].pinfo != INSN_MACRO)
1274 {
1275 if (!validate_mips_insn (&mips_opcodes[i]))
1276 broken = 1;
1277 }
1278 ++i;
1279 }
1280 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1281 }
1282
1283 mips16_op_hash = hash_new ();
1284
1285 i = 0;
1286 while (i < bfd_mips16_num_opcodes)
1287 {
1288 const char *name = mips16_opcodes[i].name;
1289
1290 retval = hash_insert (mips16_op_hash, name, (PTR) &mips16_opcodes[i]);
1291 if (retval != NULL)
1292 as_fatal (_("internal: can't hash `%s': %s"),
1293 mips16_opcodes[i].name, retval);
1294 do
1295 {
1296 if (mips16_opcodes[i].pinfo != INSN_MACRO
1297 && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1298 != mips16_opcodes[i].match))
1299 {
1300 fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1301 mips16_opcodes[i].name, mips16_opcodes[i].args);
1302 broken = 1;
1303 }
1304 ++i;
1305 }
1306 while (i < bfd_mips16_num_opcodes
1307 && strcmp (mips16_opcodes[i].name, name) == 0);
1308 }
1309
1310 if (broken)
1311 as_fatal (_("Broken assembler. No assembly attempted."));
1312
1313 /* We add all the general register names to the symbol table. This
1314 helps us detect invalid uses of them. */
1315 for (i = 0; i < 32; i++)
1316 {
1317 char buf[5];
1318
1319 sprintf (buf, "$%d", i);
1320 symbol_table_insert (symbol_new (buf, reg_section, i,
1321 &zero_address_frag));
1322 }
1323 symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1324 &zero_address_frag));
1325 symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1326 &zero_address_frag));
1327 symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1328 &zero_address_frag));
1329 symbol_table_insert (symbol_new ("$at", reg_section, AT,
1330 &zero_address_frag));
1331 symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1332 &zero_address_frag));
1333 symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1334 &zero_address_frag));
1335 symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1336 &zero_address_frag));
1337
1338 mips_no_prev_insn (false);
1339
1340 mips_gprmask = 0;
1341 mips_cprmask[0] = 0;
1342 mips_cprmask[1] = 0;
1343 mips_cprmask[2] = 0;
1344 mips_cprmask[3] = 0;
1345
1346 /* set the default alignment for the text section (2**2) */
1347 record_alignment (text_section, 2);
1348
1349 if (USE_GLOBAL_POINTER_OPT)
1350 bfd_set_gp_size (stdoutput, g_switch_value);
1351
1352 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1353 {
1354 /* On a native system, sections must be aligned to 16 byte
1355 boundaries. When configured for an embedded ELF target, we
1356 don't bother. */
1357 if (strcmp (TARGET_OS, "elf") != 0)
1358 {
1359 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1360 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1361 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1362 }
1363
1364 /* Create a .reginfo section for register masks and a .mdebug
1365 section for debugging information. */
1366 {
1367 segT seg;
1368 subsegT subseg;
1369 flagword flags;
1370 segT sec;
1371
1372 seg = now_seg;
1373 subseg = now_subseg;
1374
1375 /* The ABI says this section should be loaded so that the
1376 running program can access it. However, we don't load it
1377 if we are configured for an embedded target */
1378 flags = SEC_READONLY | SEC_DATA;
1379 if (strcmp (TARGET_OS, "elf") != 0)
1380 flags |= SEC_ALLOC | SEC_LOAD;
1381
e013f690 1382 if (! HAVE_NEWABI)
252b5132
RH
1383 {
1384 sec = subseg_new (".reginfo", (subsegT) 0);
1385
252b5132
RH
1386 (void) bfd_set_section_flags (stdoutput, sec, flags);
1387 (void) bfd_set_section_alignment (stdoutput, sec, 2);
bdaaa2e1 1388
252b5132
RH
1389#ifdef OBJ_ELF
1390 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1391#endif
1392 }
1393 else
1394 {
1395 /* The 64-bit ABI uses a .MIPS.options section rather than
1396 .reginfo section. */
1397 sec = subseg_new (".MIPS.options", (subsegT) 0);
1398 (void) bfd_set_section_flags (stdoutput, sec, flags);
1399 (void) bfd_set_section_alignment (stdoutput, sec, 3);
1400
1401#ifdef OBJ_ELF
1402 /* Set up the option header. */
1403 {
1404 Elf_Internal_Options opthdr;
1405 char *f;
1406
1407 opthdr.kind = ODK_REGINFO;
1408 opthdr.size = (sizeof (Elf_External_Options)
1409 + sizeof (Elf64_External_RegInfo));
1410 opthdr.section = 0;
1411 opthdr.info = 0;
1412 f = frag_more (sizeof (Elf_External_Options));
1413 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1414 (Elf_External_Options *) f);
1415
1416 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1417 }
1418#endif
1419 }
1420
1421 if (ECOFF_DEBUGGING)
1422 {
1423 sec = subseg_new (".mdebug", (subsegT) 0);
1424 (void) bfd_set_section_flags (stdoutput, sec,
1425 SEC_HAS_CONTENTS | SEC_READONLY);
1426 (void) bfd_set_section_alignment (stdoutput, sec, 2);
1427 }
1428
1429#ifdef MIPS_STABS_ELF
1430 pdr_seg = subseg_new (".pdr", (subsegT) 0);
1431 (void) bfd_set_section_flags (stdoutput, pdr_seg,
1432 SEC_READONLY | SEC_RELOC | SEC_DEBUGGING);
1433 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1434#endif
1435
1436 subseg_set (seg, subseg);
1437 }
1438 }
1439
1440 if (! ECOFF_DEBUGGING)
1441 md_obj_begin ();
1442}
1443
1444void
1445md_mips_end ()
1446{
1447 if (! ECOFF_DEBUGGING)
1448 md_obj_end ();
1449}
1450
1451void
1452md_assemble (str)
1453 char *str;
1454{
1455 struct mips_cl_insn insn;
f6688943
TS
1456 bfd_reloc_code_real_type unused_reloc[3]
1457 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132
RH
1458
1459 imm_expr.X_op = O_absent;
252b5132
RH
1460 imm_unmatched_hi = false;
1461 offset_expr.X_op = O_absent;
f6688943
TS
1462 imm_reloc[0] = BFD_RELOC_UNUSED;
1463 imm_reloc[1] = BFD_RELOC_UNUSED;
1464 imm_reloc[2] = BFD_RELOC_UNUSED;
1465 offset_reloc[0] = BFD_RELOC_UNUSED;
1466 offset_reloc[1] = BFD_RELOC_UNUSED;
1467 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
1468
1469 if (mips_opts.mips16)
1470 mips16_ip (str, &insn);
1471 else
1472 {
1473 mips_ip (str, &insn);
beae10d5
KH
1474 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1475 str, insn.insn_opcode));
252b5132
RH
1476 }
1477
1478 if (insn_error)
1479 {
1480 as_bad ("%s `%s'", insn_error, str);
1481 return;
1482 }
1483
1484 if (insn.insn_mo->pinfo == INSN_MACRO)
1485 {
1486 if (mips_opts.mips16)
1487 mips16_macro (&insn);
1488 else
1489 macro (&insn);
1490 }
1491 else
1492 {
1493 if (imm_expr.X_op != O_absent)
c4e7957c 1494 append_insn (NULL, &insn, &imm_expr, imm_reloc, imm_unmatched_hi);
252b5132 1495 else if (offset_expr.X_op != O_absent)
c4e7957c 1496 append_insn (NULL, &insn, &offset_expr, offset_reloc, false);
252b5132 1497 else
c4e7957c 1498 append_insn (NULL, &insn, NULL, unused_reloc, false);
252b5132
RH
1499 }
1500}
1501
1502/* See whether instruction IP reads register REG. CLASS is the type
1503 of register. */
1504
1505static int
1506insn_uses_reg (ip, reg, class)
1507 struct mips_cl_insn *ip;
1508 unsigned int reg;
1509 enum mips_regclass class;
1510{
1511 if (class == MIPS16_REG)
1512 {
1513 assert (mips_opts.mips16);
1514 reg = mips16_to_32_reg_map[reg];
1515 class = MIPS_GR_REG;
1516 }
1517
1518 /* Don't report on general register 0, since it never changes. */
1519 if (class == MIPS_GR_REG && reg == 0)
1520 return 0;
1521
1522 if (class == MIPS_FP_REG)
1523 {
1524 assert (! mips_opts.mips16);
1525 /* If we are called with either $f0 or $f1, we must check $f0.
1526 This is not optimal, because it will introduce an unnecessary
1527 NOP between "lwc1 $f0" and "swc1 $f1". To fix this we would
1528 need to distinguish reading both $f0 and $f1 or just one of
1529 them. Note that we don't have to check the other way,
1530 because there is no instruction that sets both $f0 and $f1
1531 and requires a delay. */
1532 if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1533 && ((((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS) &~(unsigned)1)
1534 == (reg &~ (unsigned) 1)))
1535 return 1;
1536 if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1537 && ((((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT) &~(unsigned)1)
1538 == (reg &~ (unsigned) 1)))
1539 return 1;
1540 }
1541 else if (! mips_opts.mips16)
1542 {
1543 if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1544 && ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == reg)
1545 return 1;
1546 if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1547 && ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT) == reg)
1548 return 1;
1549 }
1550 else
1551 {
1552 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1553 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RX)
1554 & MIPS16OP_MASK_RX)]
1555 == reg))
1556 return 1;
1557 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1558 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RY)
1559 & MIPS16OP_MASK_RY)]
1560 == reg))
1561 return 1;
1562 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1563 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
1564 & MIPS16OP_MASK_MOVE32Z)]
1565 == reg))
1566 return 1;
1567 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1568 return 1;
1569 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1570 return 1;
1571 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1572 return 1;
1573 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1574 && ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
1575 & MIPS16OP_MASK_REGR32) == reg)
1576 return 1;
1577 }
1578
1579 return 0;
1580}
1581
1582/* This function returns true if modifying a register requires a
1583 delay. */
1584
1585static int
1586reg_needs_delay (reg)
156c2f8b 1587 unsigned int reg;
252b5132
RH
1588{
1589 unsigned long prev_pinfo;
1590
1591 prev_pinfo = prev_insn.insn_mo->pinfo;
1592 if (! mips_opts.noreorder
9ce8a5dd 1593 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
1594 && ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1595 || (! gpr_interlocks
1596 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1597 {
1598 /* A load from a coprocessor or from memory. All load
1599 delays delay the use of general register rt for one
1600 instruction on the r3000. The r6000 and r4000 use
1601 interlocks. */
bdaaa2e1 1602 /* Itbl support may require additional care here. */
252b5132
RH
1603 know (prev_pinfo & INSN_WRITE_GPR_T);
1604 if (reg == ((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT))
1605 return 1;
1606 }
1607
1608 return 0;
1609}
1610
1611/* Mark instruction labels in mips16 mode. This permits the linker to
1612 handle them specially, such as generating jalx instructions when
1613 needed. We also make them odd for the duration of the assembly, in
1614 order to generate the right sort of code. We will make them even
1615 in the adjust_symtab routine, while leaving them marked. This is
1616 convenient for the debugger and the disassembler. The linker knows
1617 to make them odd again. */
1618
1619static void
1620mips16_mark_labels ()
1621{
1622 if (mips_opts.mips16)
1623 {
1624 struct insn_label_list *l;
98aa84af 1625 valueT val;
252b5132
RH
1626
1627 for (l = insn_labels; l != NULL; l = l->next)
1628 {
1629#ifdef OBJ_ELF
1630 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1631 S_SET_OTHER (l->label, STO_MIPS16);
1632#endif
98aa84af
AM
1633 val = S_GET_VALUE (l->label);
1634 if ((val & 1) == 0)
1635 S_SET_VALUE (l->label, val + 1);
252b5132
RH
1636 }
1637 }
1638}
1639
1640/* Output an instruction. PLACE is where to put the instruction; if
1641 it is NULL, this uses frag_more to get room. IP is the instruction
1642 information. ADDRESS_EXPR is an operand of the instruction to be
1643 used with RELOC_TYPE. */
1644
1645static void
1646append_insn (place, ip, address_expr, reloc_type, unmatched_hi)
1647 char *place;
1648 struct mips_cl_insn *ip;
1649 expressionS *address_expr;
f6688943 1650 bfd_reloc_code_real_type *reloc_type;
252b5132
RH
1651 boolean unmatched_hi;
1652{
1653 register unsigned long prev_pinfo, pinfo;
1654 char *f;
f6688943 1655 fixS *fixp[3];
252b5132
RH
1656 int nops = 0;
1657
1658 /* Mark instruction labels in mips16 mode. */
1659 if (mips_opts.mips16)
1660 mips16_mark_labels ();
1661
1662 prev_pinfo = prev_insn.insn_mo->pinfo;
1663 pinfo = ip->insn_mo->pinfo;
1664
1665 if (place == NULL && (! mips_opts.noreorder || prev_nop_frag != NULL))
1666 {
1667 int prev_prev_nop;
1668
1669 /* If the previous insn required any delay slots, see if we need
1670 to insert a NOP or two. There are eight kinds of possible
1671 hazards, of which an instruction can have at most one type.
1672 (1) a load from memory delay
1673 (2) a load from a coprocessor delay
1674 (3) an unconditional branch delay
1675 (4) a conditional branch delay
1676 (5) a move to coprocessor register delay
1677 (6) a load coprocessor register from memory delay
1678 (7) a coprocessor condition code delay
1679 (8) a HI/LO special register delay
1680
1681 There are a lot of optimizations we could do that we don't.
1682 In particular, we do not, in general, reorder instructions.
1683 If you use gcc with optimization, it will reorder
1684 instructions and generally do much more optimization then we
1685 do here; repeating all that work in the assembler would only
1686 benefit hand written assembly code, and does not seem worth
1687 it. */
1688
1689 /* This is how a NOP is emitted. */
1690#define emit_nop() \
1691 (mips_opts.mips16 \
1692 ? md_number_to_chars (frag_more (2), 0x6500, 2) \
1693 : md_number_to_chars (frag_more (4), 0, 4))
1694
1695 /* The previous insn might require a delay slot, depending upon
1696 the contents of the current insn. */
1697 if (! mips_opts.mips16
9ce8a5dd 1698 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
1699 && (((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1700 && ! cop_interlocks)
1701 || (! gpr_interlocks
1702 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1703 {
1704 /* A load from a coprocessor or from memory. All load
1705 delays delay the use of general register rt for one
1706 instruction on the r3000. The r6000 and r4000 use
1707 interlocks. */
beae10d5 1708 /* Itbl support may require additional care here. */
252b5132
RH
1709 know (prev_pinfo & INSN_WRITE_GPR_T);
1710 if (mips_optimize == 0
1711 || insn_uses_reg (ip,
1712 ((prev_insn.insn_opcode >> OP_SH_RT)
1713 & OP_MASK_RT),
1714 MIPS_GR_REG))
1715 ++nops;
1716 }
1717 else if (! mips_opts.mips16
9ce8a5dd 1718 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132 1719 && (((prev_pinfo & INSN_COPROC_MOVE_DELAY)
beae10d5 1720 && ! cop_interlocks)
e7af610e 1721 || (mips_opts.isa == ISA_MIPS1
252b5132
RH
1722 && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))))
1723 {
1724 /* A generic coprocessor delay. The previous instruction
1725 modified a coprocessor general or control register. If
1726 it modified a control register, we need to avoid any
1727 coprocessor instruction (this is probably not always
1728 required, but it sometimes is). If it modified a general
1729 register, we avoid using that register.
1730
1731 On the r6000 and r4000 loading a coprocessor register
1732 from memory is interlocked, and does not require a delay.
1733
1734 This case is not handled very well. There is no special
1735 knowledge of CP0 handling, and the coprocessors other
1736 than the floating point unit are not distinguished at
1737 all. */
1738 /* Itbl support may require additional care here. FIXME!
bdaaa2e1 1739 Need to modify this to include knowledge about
252b5132
RH
1740 user specified delays! */
1741 if (prev_pinfo & INSN_WRITE_FPR_T)
1742 {
1743 if (mips_optimize == 0
1744 || insn_uses_reg (ip,
1745 ((prev_insn.insn_opcode >> OP_SH_FT)
1746 & OP_MASK_FT),
1747 MIPS_FP_REG))
1748 ++nops;
1749 }
1750 else if (prev_pinfo & INSN_WRITE_FPR_S)
1751 {
1752 if (mips_optimize == 0
1753 || insn_uses_reg (ip,
1754 ((prev_insn.insn_opcode >> OP_SH_FS)
1755 & OP_MASK_FS),
1756 MIPS_FP_REG))
1757 ++nops;
1758 }
1759 else
1760 {
1761 /* We don't know exactly what the previous instruction
1762 does. If the current instruction uses a coprocessor
1763 register, we must insert a NOP. If previous
1764 instruction may set the condition codes, and the
1765 current instruction uses them, we must insert two
1766 NOPS. */
bdaaa2e1 1767 /* Itbl support may require additional care here. */
252b5132
RH
1768 if (mips_optimize == 0
1769 || ((prev_pinfo & INSN_WRITE_COND_CODE)
1770 && (pinfo & INSN_READ_COND_CODE)))
1771 nops += 2;
1772 else if (pinfo & INSN_COP)
1773 ++nops;
1774 }
1775 }
1776 else if (! mips_opts.mips16
9ce8a5dd 1777 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
1778 && (prev_pinfo & INSN_WRITE_COND_CODE)
1779 && ! cop_interlocks)
1780 {
1781 /* The previous instruction sets the coprocessor condition
1782 codes, but does not require a general coprocessor delay
1783 (this means it is a floating point comparison
1784 instruction). If this instruction uses the condition
1785 codes, we need to insert a single NOP. */
beae10d5 1786 /* Itbl support may require additional care here. */
252b5132
RH
1787 if (mips_optimize == 0
1788 || (pinfo & INSN_READ_COND_CODE))
1789 ++nops;
1790 }
6b76fefe
CM
1791
1792 /* If we're fixing up mfhi/mflo for the r7000 and the
1793 previous insn was an mfhi/mflo and the current insn
1794 reads the register that the mfhi/mflo wrote to, then
1795 insert two nops. */
1796
1797 else if (mips_7000_hilo_fix
1798 && MF_HILO_INSN (prev_pinfo)
1799 && insn_uses_reg (ip, ((prev_insn.insn_opcode >> OP_SH_RD)
beae10d5
KH
1800 & OP_MASK_RD),
1801 MIPS_GR_REG))
6b76fefe
CM
1802 {
1803 nops += 2;
1804 }
1805
1806 /* If we're fixing up mfhi/mflo for the r7000 and the
1807 2nd previous insn was an mfhi/mflo and the current insn
1808 reads the register that the mfhi/mflo wrote to, then
1809 insert one nop. */
1810
1811 else if (mips_7000_hilo_fix
1812 && MF_HILO_INSN (prev_prev_insn.insn_opcode)
1813 && insn_uses_reg (ip, ((prev_prev_insn.insn_opcode >> OP_SH_RD)
1814 & OP_MASK_RD),
1815 MIPS_GR_REG))
bdaaa2e1 1816
6b76fefe
CM
1817 {
1818 nops += 1;
1819 }
bdaaa2e1 1820
252b5132
RH
1821 else if (prev_pinfo & INSN_READ_LO)
1822 {
1823 /* The previous instruction reads the LO register; if the
1824 current instruction writes to the LO register, we must
bdaaa2e1
KH
1825 insert two NOPS. Some newer processors have interlocks.
1826 Also the tx39's multiply instructions can be exectuted
252b5132 1827 immediatly after a read from HI/LO (without the delay),
bdaaa2e1
KH
1828 though the tx39's divide insns still do require the
1829 delay. */
252b5132 1830 if (! (hilo_interlocks
ec68c924 1831 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
252b5132
RH
1832 && (mips_optimize == 0
1833 || (pinfo & INSN_WRITE_LO)))
1834 nops += 2;
1835 /* Most mips16 branch insns don't have a delay slot.
1836 If a read from LO is immediately followed by a branch
1837 to a write to LO we have a read followed by a write
1838 less than 2 insns away. We assume the target of
1839 a branch might be a write to LO, and insert a nop
bdaaa2e1 1840 between a read and an immediately following branch. */
252b5132
RH
1841 else if (mips_opts.mips16
1842 && (mips_optimize == 0
1843 || (pinfo & MIPS16_INSN_BRANCH)))
1844 nops += 1;
1845 }
1846 else if (prev_insn.insn_mo->pinfo & INSN_READ_HI)
1847 {
1848 /* The previous instruction reads the HI register; if the
1849 current instruction writes to the HI register, we must
1850 insert a NOP. Some newer processors have interlocks.
bdaaa2e1 1851 Also the note tx39's multiply above. */
252b5132 1852 if (! (hilo_interlocks
ec68c924 1853 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
252b5132
RH
1854 && (mips_optimize == 0
1855 || (pinfo & INSN_WRITE_HI)))
1856 nops += 2;
1857 /* Most mips16 branch insns don't have a delay slot.
1858 If a read from HI is immediately followed by a branch
1859 to a write to HI we have a read followed by a write
1860 less than 2 insns away. We assume the target of
1861 a branch might be a write to HI, and insert a nop
bdaaa2e1 1862 between a read and an immediately following branch. */
252b5132
RH
1863 else if (mips_opts.mips16
1864 && (mips_optimize == 0
1865 || (pinfo & MIPS16_INSN_BRANCH)))
1866 nops += 1;
1867 }
1868
1869 /* If the previous instruction was in a noreorder section, then
1870 we don't want to insert the nop after all. */
bdaaa2e1 1871 /* Itbl support may require additional care here. */
252b5132
RH
1872 if (prev_insn_unreordered)
1873 nops = 0;
1874
1875 /* There are two cases which require two intervening
1876 instructions: 1) setting the condition codes using a move to
1877 coprocessor instruction which requires a general coprocessor
1878 delay and then reading the condition codes 2) reading the HI
1879 or LO register and then writing to it (except on processors
1880 which have interlocks). If we are not already emitting a NOP
1881 instruction, we must check for these cases compared to the
1882 instruction previous to the previous instruction. */
1883 if ((! mips_opts.mips16
9ce8a5dd 1884 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
1885 && (prev_prev_insn.insn_mo->pinfo & INSN_COPROC_MOVE_DELAY)
1886 && (prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
1887 && (pinfo & INSN_READ_COND_CODE)
1888 && ! cop_interlocks)
1889 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_LO)
1890 && (pinfo & INSN_WRITE_LO)
1891 && ! (hilo_interlocks
ec68c924 1892 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT))))
252b5132
RH
1893 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
1894 && (pinfo & INSN_WRITE_HI)
1895 && ! (hilo_interlocks
ec68c924 1896 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))))
252b5132
RH
1897 prev_prev_nop = 1;
1898 else
1899 prev_prev_nop = 0;
1900
1901 if (prev_prev_insn_unreordered)
1902 prev_prev_nop = 0;
1903
1904 if (prev_prev_nop && nops == 0)
1905 ++nops;
1906
1907 /* If we are being given a nop instruction, don't bother with
1908 one of the nops we would otherwise output. This will only
1909 happen when a nop instruction is used with mips_optimize set
1910 to 0. */
1911 if (nops > 0
1912 && ! mips_opts.noreorder
156c2f8b 1913 && ip->insn_opcode == (unsigned) (mips_opts.mips16 ? 0x6500 : 0))
252b5132
RH
1914 --nops;
1915
1916 /* Now emit the right number of NOP instructions. */
1917 if (nops > 0 && ! mips_opts.noreorder)
1918 {
1919 fragS *old_frag;
1920 unsigned long old_frag_offset;
1921 int i;
1922 struct insn_label_list *l;
1923
1924 old_frag = frag_now;
1925 old_frag_offset = frag_now_fix ();
1926
1927 for (i = 0; i < nops; i++)
1928 emit_nop ();
1929
1930 if (listing)
1931 {
1932 listing_prev_line ();
1933 /* We may be at the start of a variant frag. In case we
1934 are, make sure there is enough space for the frag
1935 after the frags created by listing_prev_line. The
1936 argument to frag_grow here must be at least as large
1937 as the argument to all other calls to frag_grow in
1938 this file. We don't have to worry about being in the
1939 middle of a variant frag, because the variants insert
1940 all needed nop instructions themselves. */
1941 frag_grow (40);
1942 }
1943
1944 for (l = insn_labels; l != NULL; l = l->next)
1945 {
98aa84af
AM
1946 valueT val;
1947
252b5132 1948 assert (S_GET_SEGMENT (l->label) == now_seg);
49309057 1949 symbol_set_frag (l->label, frag_now);
98aa84af 1950 val = (valueT) frag_now_fix ();
252b5132
RH
1951 /* mips16 text labels are stored as odd. */
1952 if (mips_opts.mips16)
98aa84af
AM
1953 val += 1;
1954 S_SET_VALUE (l->label, val);
252b5132
RH
1955 }
1956
1957#ifndef NO_ECOFF_DEBUGGING
1958 if (ECOFF_DEBUGGING)
1959 ecoff_fix_loc (old_frag, old_frag_offset);
1960#endif
1961 }
1962 else if (prev_nop_frag != NULL)
1963 {
1964 /* We have a frag holding nops we may be able to remove. If
1965 we don't need any nops, we can decrease the size of
1966 prev_nop_frag by the size of one instruction. If we do
bdaaa2e1 1967 need some nops, we count them in prev_nops_required. */
252b5132
RH
1968 if (prev_nop_frag_since == 0)
1969 {
1970 if (nops == 0)
1971 {
1972 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1973 --prev_nop_frag_holds;
1974 }
1975 else
1976 prev_nop_frag_required += nops;
1977 }
1978 else
1979 {
1980 if (prev_prev_nop == 0)
1981 {
1982 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1983 --prev_nop_frag_holds;
1984 }
1985 else
1986 ++prev_nop_frag_required;
1987 }
1988
1989 if (prev_nop_frag_holds <= prev_nop_frag_required)
1990 prev_nop_frag = NULL;
1991
1992 ++prev_nop_frag_since;
1993
1994 /* Sanity check: by the time we reach the second instruction
1995 after prev_nop_frag, we should have used up all the nops
1996 one way or another. */
1997 assert (prev_nop_frag_since <= 1 || prev_nop_frag == NULL);
1998 }
1999 }
2000
f6688943 2001 if (*reloc_type > BFD_RELOC_UNUSED)
252b5132
RH
2002 {
2003 /* We need to set up a variant frag. */
2004 assert (mips_opts.mips16 && address_expr != NULL);
2005 f = frag_var (rs_machine_dependent, 4, 0,
f6688943 2006 RELAX_MIPS16_ENCODE (*reloc_type - BFD_RELOC_UNUSED,
252b5132
RH
2007 mips16_small, mips16_ext,
2008 (prev_pinfo
2009 & INSN_UNCOND_BRANCH_DELAY),
f6688943 2010 (*prev_insn_reloc_type
252b5132 2011 == BFD_RELOC_MIPS16_JMP)),
c4e7957c 2012 make_expr_symbol (address_expr), 0, NULL);
252b5132
RH
2013 }
2014 else if (place != NULL)
2015 f = place;
2016 else if (mips_opts.mips16
2017 && ! ip->use_extend
f6688943 2018 && *reloc_type != BFD_RELOC_MIPS16_JMP)
252b5132
RH
2019 {
2020 /* Make sure there is enough room to swap this instruction with
2021 a following jump instruction. */
2022 frag_grow (6);
2023 f = frag_more (2);
2024 }
2025 else
2026 {
2027 if (mips_opts.mips16
2028 && mips_opts.noreorder
2029 && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
2030 as_warn (_("extended instruction in delay slot"));
2031
2032 f = frag_more (4);
2033 }
2034
f6688943
TS
2035 fixp[0] = fixp[1] = fixp[2] = NULL;
2036 if (address_expr != NULL && *reloc_type < BFD_RELOC_UNUSED)
252b5132
RH
2037 {
2038 if (address_expr->X_op == O_constant)
2039 {
f6688943
TS
2040 unsigned long tmp;
2041
2042 switch (*reloc_type)
252b5132
RH
2043 {
2044 case BFD_RELOC_32:
2045 ip->insn_opcode |= address_expr->X_add_number;
2046 break;
2047
f6688943
TS
2048 case BFD_RELOC_MIPS_HIGHEST:
2049 tmp = (address_expr->X_add_number + 0x800080008000) >> 16;
2050 tmp >>= 16;
2051 ip->insn_opcode |= (tmp >> 16) & 0xffff;
2052 break;
2053
2054 case BFD_RELOC_MIPS_HIGHER:
2055 tmp = (address_expr->X_add_number + 0x80008000) >> 16;
2056 ip->insn_opcode |= (tmp >> 16) & 0xffff;
2057 break;
2058
2059 case BFD_RELOC_HI16_S:
2060 ip->insn_opcode |= ((address_expr->X_add_number + 0x8000)
2061 >> 16) & 0xffff;
2062 break;
2063
2064 case BFD_RELOC_HI16:
2065 ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
2066 break;
2067
252b5132
RH
2068 case BFD_RELOC_LO16:
2069 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
2070 break;
2071
2072 case BFD_RELOC_MIPS_JMP:
2073 if ((address_expr->X_add_number & 3) != 0)
2074 as_bad (_("jump to misaligned address (0x%lx)"),
2075 (unsigned long) address_expr->X_add_number);
7496292d
TS
2076 if (address_expr->X_add_number & ~0xfffffff
2077 || address_expr->X_add_number > 0x7fffffc)
2078 as_bad (_("jump address range overflow (0x%lx)"),
2079 (unsigned long) address_expr->X_add_number);
252b5132
RH
2080 ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
2081 break;
2082
2083 case BFD_RELOC_MIPS16_JMP:
2084 if ((address_expr->X_add_number & 3) != 0)
2085 as_bad (_("jump to misaligned address (0x%lx)"),
2086 (unsigned long) address_expr->X_add_number);
7496292d
TS
2087 if (address_expr->X_add_number & ~0xfffffff
2088 || address_expr->X_add_number > 0x7fffffc)
2089 as_bad (_("jump address range overflow (0x%lx)"),
2090 (unsigned long) address_expr->X_add_number);
252b5132
RH
2091 ip->insn_opcode |=
2092 (((address_expr->X_add_number & 0x7c0000) << 3)
2093 | ((address_expr->X_add_number & 0xf800000) >> 7)
2094 | ((address_expr->X_add_number & 0x3fffc) >> 2));
2095 break;
2096
cb56d3d3 2097 case BFD_RELOC_16_PCREL:
233b8738 2098 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
cb56d3d3
TS
2099 break;
2100
252b5132
RH
2101 case BFD_RELOC_16_PCREL_S2:
2102 goto need_reloc;
2103
2104 default:
2105 internalError ();
2106 }
2107 }
2108 else
2109 {
2110 need_reloc:
f6688943 2111 /* Don't generate a reloc if we are writing into a variant frag. */
252b5132
RH
2112 if (place == NULL)
2113 {
f6688943
TS
2114 fixp[0] = fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
2115 address_expr,
2116 (*reloc_type == BFD_RELOC_16_PCREL
2117 || *reloc_type == BFD_RELOC_16_PCREL_S2),
2118 reloc_type[0]);
2119
b6ff326e 2120 /* These relocations can have an addend that won't fit in
f6688943
TS
2121 4 octets for 64bit assembly. */
2122 if (HAVE_64BIT_GPRS &&
2123 (*reloc_type == BFD_RELOC_16
2124 || *reloc_type == BFD_RELOC_32
2125 || *reloc_type == BFD_RELOC_MIPS_JMP
2126 || *reloc_type == BFD_RELOC_HI16_S
2127 || *reloc_type == BFD_RELOC_LO16
2128 || *reloc_type == BFD_RELOC_GPREL16
2129 || *reloc_type == BFD_RELOC_MIPS_LITERAL
2130 || *reloc_type == BFD_RELOC_GPREL32
2131 || *reloc_type == BFD_RELOC_64
2132 || *reloc_type == BFD_RELOC_CTOR
2133 || *reloc_type == BFD_RELOC_MIPS_SUB
2134 || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2135 || *reloc_type == BFD_RELOC_MIPS_HIGHER
2136 || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2137 || *reloc_type == BFD_RELOC_MIPS_REL16
2138 || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2139 fixp[0]->fx_no_overflow = 1;
2140
252b5132
RH
2141 if (unmatched_hi)
2142 {
2143 struct mips_hi_fixup *hi_fixup;
2144
f6688943 2145 assert (*reloc_type == BFD_RELOC_HI16_S);
252b5132
RH
2146 hi_fixup = ((struct mips_hi_fixup *)
2147 xmalloc (sizeof (struct mips_hi_fixup)));
f6688943 2148 hi_fixup->fixp = fixp[0];
252b5132
RH
2149 hi_fixup->seg = now_seg;
2150 hi_fixup->next = mips_hi_fixup_list;
2151 mips_hi_fixup_list = hi_fixup;
2152 }
f6688943
TS
2153
2154 if (reloc_type[1] != BFD_RELOC_UNUSED)
2155 {
2156 /* FIXME: This symbol can be one of
2157 RSS_UNDEF, RSS_GP, RSS_GP0, RSS_LOC. */
2158 address_expr->X_op = O_absent;
2159 address_expr->X_add_symbol = 0;
2160 address_expr->X_add_number = 0;
2161
2162 fixp[1] = fix_new_exp (frag_now, f - frag_now->fr_literal,
2163 4, address_expr, false,
2164 reloc_type[1]);
2165
b6ff326e 2166 /* These relocations can have an addend that won't fit in
f6688943
TS
2167 4 octets for 64bit assembly. */
2168 if (HAVE_64BIT_GPRS &&
2169 (*reloc_type == BFD_RELOC_16
2170 || *reloc_type == BFD_RELOC_32
2171 || *reloc_type == BFD_RELOC_MIPS_JMP
2172 || *reloc_type == BFD_RELOC_HI16_S
2173 || *reloc_type == BFD_RELOC_LO16
2174 || *reloc_type == BFD_RELOC_GPREL16
2175 || *reloc_type == BFD_RELOC_MIPS_LITERAL
2176 || *reloc_type == BFD_RELOC_GPREL32
2177 || *reloc_type == BFD_RELOC_64
2178 || *reloc_type == BFD_RELOC_CTOR
2179 || *reloc_type == BFD_RELOC_MIPS_SUB
2180 || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2181 || *reloc_type == BFD_RELOC_MIPS_HIGHER
2182 || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2183 || *reloc_type == BFD_RELOC_MIPS_REL16
2184 || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2185 fixp[1]->fx_no_overflow = 1;
2186
2187 if (reloc_type[2] != BFD_RELOC_UNUSED)
2188 {
2189 address_expr->X_op = O_absent;
2190 address_expr->X_add_symbol = 0;
2191 address_expr->X_add_number = 0;
2192
2193 fixp[2] = fix_new_exp (frag_now,
2194 f - frag_now->fr_literal, 4,
2195 address_expr, false,
2196 reloc_type[2]);
2197
b6ff326e 2198 /* These relocations can have an addend that won't fit in
f6688943
TS
2199 4 octets for 64bit assembly. */
2200 if (HAVE_64BIT_GPRS &&
2201 (*reloc_type == BFD_RELOC_16
2202 || *reloc_type == BFD_RELOC_32
2203 || *reloc_type == BFD_RELOC_MIPS_JMP
2204 || *reloc_type == BFD_RELOC_HI16_S
2205 || *reloc_type == BFD_RELOC_LO16
2206 || *reloc_type == BFD_RELOC_GPREL16
2207 || *reloc_type == BFD_RELOC_MIPS_LITERAL
2208 || *reloc_type == BFD_RELOC_GPREL32
2209 || *reloc_type == BFD_RELOC_64
2210 || *reloc_type == BFD_RELOC_CTOR
2211 || *reloc_type == BFD_RELOC_MIPS_SUB
2212 || *reloc_type == BFD_RELOC_MIPS_HIGHEST
2213 || *reloc_type == BFD_RELOC_MIPS_HIGHER
2214 || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
2215 || *reloc_type == BFD_RELOC_MIPS_REL16
2216 || *reloc_type == BFD_RELOC_MIPS_RELGOT))
2217 fixp[2]->fx_no_overflow = 1;
2218 }
2219 }
252b5132
RH
2220 }
2221 }
2222 }
2223
2224 if (! mips_opts.mips16)
2225 md_number_to_chars (f, ip->insn_opcode, 4);
f6688943 2226 else if (*reloc_type == BFD_RELOC_MIPS16_JMP)
252b5132
RH
2227 {
2228 md_number_to_chars (f, ip->insn_opcode >> 16, 2);
2229 md_number_to_chars (f + 2, ip->insn_opcode & 0xffff, 2);
2230 }
2231 else
2232 {
2233 if (ip->use_extend)
2234 {
2235 md_number_to_chars (f, 0xf000 | ip->extend, 2);
2236 f += 2;
2237 }
2238 md_number_to_chars (f, ip->insn_opcode, 2);
2239 }
2240
2241 /* Update the register mask information. */
2242 if (! mips_opts.mips16)
2243 {
2244 if (pinfo & INSN_WRITE_GPR_D)
2245 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
2246 if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2247 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
2248 if (pinfo & INSN_READ_GPR_S)
2249 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
2250 if (pinfo & INSN_WRITE_GPR_31)
2251 mips_gprmask |= 1 << 31;
2252 if (pinfo & INSN_WRITE_FPR_D)
2253 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
2254 if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2255 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
2256 if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2257 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
2258 if ((pinfo & INSN_READ_FPR_R) != 0)
2259 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
2260 if (pinfo & INSN_COP)
2261 {
bdaaa2e1
KH
2262 /* We don't keep enough information to sort these cases out.
2263 The itbl support does keep this information however, although
2264 we currently don't support itbl fprmats as part of the cop
2265 instruction. May want to add this support in the future. */
252b5132
RH
2266 }
2267 /* Never set the bit for $0, which is always zero. */
beae10d5 2268 mips_gprmask &= ~1 << 0;
252b5132
RH
2269 }
2270 else
2271 {
2272 if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2273 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RX)
2274 & MIPS16OP_MASK_RX);
2275 if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2276 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RY)
2277 & MIPS16OP_MASK_RY);
2278 if (pinfo & MIPS16_INSN_WRITE_Z)
2279 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RZ)
2280 & MIPS16OP_MASK_RZ);
2281 if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2282 mips_gprmask |= 1 << TREG;
2283 if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2284 mips_gprmask |= 1 << SP;
2285 if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2286 mips_gprmask |= 1 << RA;
2287 if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2288 mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2289 if (pinfo & MIPS16_INSN_READ_Z)
2290 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
2291 & MIPS16OP_MASK_MOVE32Z);
2292 if (pinfo & MIPS16_INSN_READ_GPR_X)
2293 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
2294 & MIPS16OP_MASK_REGR32);
2295 }
2296
2297 if (place == NULL && ! mips_opts.noreorder)
2298 {
2299 /* Filling the branch delay slot is more complex. We try to
2300 switch the branch with the previous instruction, which we can
2301 do if the previous instruction does not set up a condition
2302 that the branch tests and if the branch is not itself the
2303 target of any branch. */
2304 if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2305 || (pinfo & INSN_COND_BRANCH_DELAY))
2306 {
2307 if (mips_optimize < 2
2308 /* If we have seen .set volatile or .set nomove, don't
2309 optimize. */
2310 || mips_opts.nomove != 0
2311 /* If we had to emit any NOP instructions, then we
2312 already know we can not swap. */
2313 || nops != 0
2314 /* If we don't even know the previous insn, we can not
bdaaa2e1 2315 swap. */
252b5132
RH
2316 || ! prev_insn_valid
2317 /* If the previous insn is already in a branch delay
2318 slot, then we can not swap. */
2319 || prev_insn_is_delay_slot
2320 /* If the previous previous insn was in a .set
2321 noreorder, we can't swap. Actually, the MIPS
2322 assembler will swap in this situation. However, gcc
2323 configured -with-gnu-as will generate code like
2324 .set noreorder
2325 lw $4,XXX
2326 .set reorder
2327 INSN
2328 bne $4,$0,foo
2329 in which we can not swap the bne and INSN. If gcc is
2330 not configured -with-gnu-as, it does not output the
2331 .set pseudo-ops. We don't have to check
2332 prev_insn_unreordered, because prev_insn_valid will
2333 be 0 in that case. We don't want to use
2334 prev_prev_insn_valid, because we do want to be able
2335 to swap at the start of a function. */
2336 || prev_prev_insn_unreordered
2337 /* If the branch is itself the target of a branch, we
2338 can not swap. We cheat on this; all we check for is
2339 whether there is a label on this instruction. If
2340 there are any branches to anything other than a
2341 label, users must use .set noreorder. */
2342 || insn_labels != NULL
2343 /* If the previous instruction is in a variant frag, we
2344 can not do the swap. This does not apply to the
2345 mips16, which uses variant frags for different
2346 purposes. */
2347 || (! mips_opts.mips16
2348 && prev_insn_frag->fr_type == rs_machine_dependent)
2349 /* If the branch reads the condition codes, we don't
2350 even try to swap, because in the sequence
2351 ctc1 $X,$31
2352 INSN
2353 INSN
2354 bc1t LABEL
2355 we can not swap, and I don't feel like handling that
2356 case. */
2357 || (! mips_opts.mips16
9ce8a5dd 2358 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
2359 && (pinfo & INSN_READ_COND_CODE))
2360 /* We can not swap with an instruction that requires a
2361 delay slot, becase the target of the branch might
2362 interfere with that instruction. */
2363 || (! mips_opts.mips16
9ce8a5dd 2364 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132 2365 && (prev_pinfo
bdaaa2e1 2366 /* Itbl support may require additional care here. */
252b5132
RH
2367 & (INSN_LOAD_COPROC_DELAY
2368 | INSN_COPROC_MOVE_DELAY
2369 | INSN_WRITE_COND_CODE)))
2370 || (! (hilo_interlocks
ec68c924 2371 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
252b5132
RH
2372 && (prev_pinfo
2373 & (INSN_READ_LO
2374 | INSN_READ_HI)))
2375 || (! mips_opts.mips16
2376 && ! gpr_interlocks
2377 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))
2378 || (! mips_opts.mips16
e7af610e 2379 && mips_opts.isa == ISA_MIPS1
bdaaa2e1 2380 /* Itbl support may require additional care here. */
252b5132
RH
2381 && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))
2382 /* We can not swap with a branch instruction. */
2383 || (prev_pinfo
2384 & (INSN_UNCOND_BRANCH_DELAY
2385 | INSN_COND_BRANCH_DELAY
2386 | INSN_COND_BRANCH_LIKELY))
2387 /* We do not swap with a trap instruction, since it
2388 complicates trap handlers to have the trap
2389 instruction be in a delay slot. */
2390 || (prev_pinfo & INSN_TRAP)
2391 /* If the branch reads a register that the previous
2392 instruction sets, we can not swap. */
2393 || (! mips_opts.mips16
2394 && (prev_pinfo & INSN_WRITE_GPR_T)
2395 && insn_uses_reg (ip,
2396 ((prev_insn.insn_opcode >> OP_SH_RT)
2397 & OP_MASK_RT),
2398 MIPS_GR_REG))
2399 || (! mips_opts.mips16
2400 && (prev_pinfo & INSN_WRITE_GPR_D)
2401 && insn_uses_reg (ip,
2402 ((prev_insn.insn_opcode >> OP_SH_RD)
2403 & OP_MASK_RD),
2404 MIPS_GR_REG))
2405 || (mips_opts.mips16
2406 && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2407 && insn_uses_reg (ip,
2408 ((prev_insn.insn_opcode
2409 >> MIPS16OP_SH_RX)
2410 & MIPS16OP_MASK_RX),
2411 MIPS16_REG))
2412 || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2413 && insn_uses_reg (ip,
2414 ((prev_insn.insn_opcode
2415 >> MIPS16OP_SH_RY)
2416 & MIPS16OP_MASK_RY),
2417 MIPS16_REG))
2418 || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2419 && insn_uses_reg (ip,
2420 ((prev_insn.insn_opcode
2421 >> MIPS16OP_SH_RZ)
2422 & MIPS16OP_MASK_RZ),
2423 MIPS16_REG))
2424 || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2425 && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2426 || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2427 && insn_uses_reg (ip, RA, MIPS_GR_REG))
2428 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2429 && insn_uses_reg (ip,
2430 MIPS16OP_EXTRACT_REG32R (prev_insn.
2431 insn_opcode),
2432 MIPS_GR_REG))))
2433 /* If the branch writes a register that the previous
2434 instruction sets, we can not swap (we know that
2435 branches write only to RD or to $31). */
2436 || (! mips_opts.mips16
2437 && (prev_pinfo & INSN_WRITE_GPR_T)
2438 && (((pinfo & INSN_WRITE_GPR_D)
2439 && (((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT)
2440 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2441 || ((pinfo & INSN_WRITE_GPR_31)
2442 && (((prev_insn.insn_opcode >> OP_SH_RT)
2443 & OP_MASK_RT)
2444 == 31))))
2445 || (! mips_opts.mips16
2446 && (prev_pinfo & INSN_WRITE_GPR_D)
2447 && (((pinfo & INSN_WRITE_GPR_D)
2448 && (((prev_insn.insn_opcode >> OP_SH_RD) & OP_MASK_RD)
2449 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2450 || ((pinfo & INSN_WRITE_GPR_31)
2451 && (((prev_insn.insn_opcode >> OP_SH_RD)
2452 & OP_MASK_RD)
2453 == 31))))
2454 || (mips_opts.mips16
2455 && (pinfo & MIPS16_INSN_WRITE_31)
2456 && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2457 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2458 && (MIPS16OP_EXTRACT_REG32R (prev_insn.insn_opcode)
2459 == RA))))
2460 /* If the branch writes a register that the previous
2461 instruction reads, we can not swap (we know that
2462 branches only write to RD or to $31). */
2463 || (! mips_opts.mips16
2464 && (pinfo & INSN_WRITE_GPR_D)
2465 && insn_uses_reg (&prev_insn,
2466 ((ip->insn_opcode >> OP_SH_RD)
2467 & OP_MASK_RD),
2468 MIPS_GR_REG))
2469 || (! mips_opts.mips16
2470 && (pinfo & INSN_WRITE_GPR_31)
2471 && insn_uses_reg (&prev_insn, 31, MIPS_GR_REG))
2472 || (mips_opts.mips16
2473 && (pinfo & MIPS16_INSN_WRITE_31)
2474 && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2475 /* If we are generating embedded PIC code, the branch
2476 might be expanded into a sequence which uses $at, so
2477 we can't swap with an instruction which reads it. */
2478 || (mips_pic == EMBEDDED_PIC
2479 && insn_uses_reg (&prev_insn, AT, MIPS_GR_REG))
2480 /* If the previous previous instruction has a load
2481 delay, and sets a register that the branch reads, we
2482 can not swap. */
2483 || (! mips_opts.mips16
9ce8a5dd 2484 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
bdaaa2e1 2485 /* Itbl support may require additional care here. */
252b5132
RH
2486 && ((prev_prev_insn.insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
2487 || (! gpr_interlocks
2488 && (prev_prev_insn.insn_mo->pinfo
2489 & INSN_LOAD_MEMORY_DELAY)))
2490 && insn_uses_reg (ip,
2491 ((prev_prev_insn.insn_opcode >> OP_SH_RT)
2492 & OP_MASK_RT),
2493 MIPS_GR_REG))
2494 /* If one instruction sets a condition code and the
2495 other one uses a condition code, we can not swap. */
2496 || ((pinfo & INSN_READ_COND_CODE)
2497 && (prev_pinfo & INSN_WRITE_COND_CODE))
2498 || ((pinfo & INSN_WRITE_COND_CODE)
2499 && (prev_pinfo & INSN_READ_COND_CODE))
2500 /* If the previous instruction uses the PC, we can not
2501 swap. */
2502 || (mips_opts.mips16
2503 && (prev_pinfo & MIPS16_INSN_READ_PC))
2504 /* If the previous instruction was extended, we can not
2505 swap. */
2506 || (mips_opts.mips16 && prev_insn_extended)
2507 /* If the previous instruction had a fixup in mips16
2508 mode, we can not swap. This normally means that the
2509 previous instruction was a 4 byte branch anyhow. */
f6688943 2510 || (mips_opts.mips16 && prev_insn_fixp[0])
bdaaa2e1
KH
2511 /* If the previous instruction is a sync, sync.l, or
2512 sync.p, we can not swap. */
f173e82e 2513 || (prev_pinfo & INSN_SYNC))
252b5132
RH
2514 {
2515 /* We could do even better for unconditional branches to
2516 portions of this object file; we could pick up the
2517 instruction at the destination, put it in the delay
2518 slot, and bump the destination address. */
2519 emit_nop ();
2520 /* Update the previous insn information. */
2521 prev_prev_insn = *ip;
2522 prev_insn.insn_mo = &dummy_opcode;
2523 }
2524 else
2525 {
2526 /* It looks like we can actually do the swap. */
2527 if (! mips_opts.mips16)
2528 {
2529 char *prev_f;
2530 char temp[4];
2531
2532 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2533 memcpy (temp, prev_f, 4);
2534 memcpy (prev_f, f, 4);
2535 memcpy (f, temp, 4);
f6688943
TS
2536 if (prev_insn_fixp[0])
2537 {
2538 prev_insn_fixp[0]->fx_frag = frag_now;
2539 prev_insn_fixp[0]->fx_where = f - frag_now->fr_literal;
2540 }
2541 if (prev_insn_fixp[1])
2542 {
2543 prev_insn_fixp[1]->fx_frag = frag_now;
2544 prev_insn_fixp[1]->fx_where = f - frag_now->fr_literal;
2545 }
2546 if (prev_insn_fixp[2])
252b5132 2547 {
f6688943
TS
2548 prev_insn_fixp[2]->fx_frag = frag_now;
2549 prev_insn_fixp[2]->fx_where = f - frag_now->fr_literal;
252b5132 2550 }
f6688943 2551 if (fixp[0])
252b5132 2552 {
f6688943
TS
2553 fixp[0]->fx_frag = prev_insn_frag;
2554 fixp[0]->fx_where = prev_insn_where;
2555 }
2556 if (fixp[1])
2557 {
2558 fixp[1]->fx_frag = prev_insn_frag;
2559 fixp[1]->fx_where = prev_insn_where;
2560 }
2561 if (fixp[2])
2562 {
2563 fixp[2]->fx_frag = prev_insn_frag;
2564 fixp[2]->fx_where = prev_insn_where;
252b5132
RH
2565 }
2566 }
2567 else
2568 {
2569 char *prev_f;
2570 char temp[2];
2571
f6688943
TS
2572 assert (prev_insn_fixp[0] == NULL);
2573 assert (prev_insn_fixp[1] == NULL);
2574 assert (prev_insn_fixp[2] == NULL);
252b5132
RH
2575 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2576 memcpy (temp, prev_f, 2);
2577 memcpy (prev_f, f, 2);
f6688943 2578 if (*reloc_type != BFD_RELOC_MIPS16_JMP)
252b5132 2579 {
f6688943 2580 assert (*reloc_type == BFD_RELOC_UNUSED);
252b5132
RH
2581 memcpy (f, temp, 2);
2582 }
2583 else
2584 {
2585 memcpy (f, f + 2, 2);
2586 memcpy (f + 2, temp, 2);
2587 }
f6688943
TS
2588 if (fixp[0])
2589 {
2590 fixp[0]->fx_frag = prev_insn_frag;
2591 fixp[0]->fx_where = prev_insn_where;
2592 }
2593 if (fixp[1])
2594 {
2595 fixp[1]->fx_frag = prev_insn_frag;
2596 fixp[1]->fx_where = prev_insn_where;
2597 }
2598 if (fixp[2])
252b5132 2599 {
f6688943
TS
2600 fixp[2]->fx_frag = prev_insn_frag;
2601 fixp[2]->fx_where = prev_insn_where;
252b5132
RH
2602 }
2603 }
2604
2605 /* Update the previous insn information; leave prev_insn
2606 unchanged. */
2607 prev_prev_insn = *ip;
2608 }
2609 prev_insn_is_delay_slot = 1;
2610
2611 /* If that was an unconditional branch, forget the previous
2612 insn information. */
2613 if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2614 {
2615 prev_prev_insn.insn_mo = &dummy_opcode;
2616 prev_insn.insn_mo = &dummy_opcode;
2617 }
2618
f6688943
TS
2619 prev_insn_fixp[0] = NULL;
2620 prev_insn_fixp[1] = NULL;
2621 prev_insn_fixp[2] = NULL;
2622 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2623 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2624 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
252b5132
RH
2625 prev_insn_extended = 0;
2626 }
2627 else if (pinfo & INSN_COND_BRANCH_LIKELY)
2628 {
2629 /* We don't yet optimize a branch likely. What we should do
2630 is look at the target, copy the instruction found there
2631 into the delay slot, and increment the branch to jump to
2632 the next instruction. */
2633 emit_nop ();
2634 /* Update the previous insn information. */
2635 prev_prev_insn = *ip;
2636 prev_insn.insn_mo = &dummy_opcode;
f6688943
TS
2637 prev_insn_fixp[0] = NULL;
2638 prev_insn_fixp[1] = NULL;
2639 prev_insn_fixp[2] = NULL;
2640 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2641 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2642 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
252b5132
RH
2643 prev_insn_extended = 0;
2644 }
2645 else
2646 {
2647 /* Update the previous insn information. */
2648 if (nops > 0)
2649 prev_prev_insn.insn_mo = &dummy_opcode;
2650 else
2651 prev_prev_insn = prev_insn;
2652 prev_insn = *ip;
2653
2654 /* Any time we see a branch, we always fill the delay slot
2655 immediately; since this insn is not a branch, we know it
2656 is not in a delay slot. */
2657 prev_insn_is_delay_slot = 0;
2658
f6688943
TS
2659 prev_insn_fixp[0] = fixp[0];
2660 prev_insn_fixp[1] = fixp[1];
2661 prev_insn_fixp[2] = fixp[2];
2662 prev_insn_reloc_type[0] = reloc_type[0];
2663 prev_insn_reloc_type[1] = reloc_type[1];
2664 prev_insn_reloc_type[2] = reloc_type[2];
252b5132
RH
2665 if (mips_opts.mips16)
2666 prev_insn_extended = (ip->use_extend
f6688943 2667 || *reloc_type > BFD_RELOC_UNUSED);
252b5132
RH
2668 }
2669
2670 prev_prev_insn_unreordered = prev_insn_unreordered;
2671 prev_insn_unreordered = 0;
2672 prev_insn_frag = frag_now;
2673 prev_insn_where = f - frag_now->fr_literal;
2674 prev_insn_valid = 1;
2675 }
2676 else if (place == NULL)
2677 {
2678 /* We need to record a bit of information even when we are not
2679 reordering, in order to determine the base address for mips16
2680 PC relative relocs. */
2681 prev_prev_insn = prev_insn;
2682 prev_insn = *ip;
f6688943
TS
2683 prev_insn_reloc_type[0] = reloc_type[0];
2684 prev_insn_reloc_type[1] = reloc_type[1];
2685 prev_insn_reloc_type[2] = reloc_type[2];
252b5132
RH
2686 prev_prev_insn_unreordered = prev_insn_unreordered;
2687 prev_insn_unreordered = 1;
2688 }
2689
2690 /* We just output an insn, so the next one doesn't have a label. */
2691 mips_clear_insn_labels ();
2692
2693 /* We must ensure that a fixup associated with an unmatched %hi
2694 reloc does not become a variant frag. Otherwise, the
2695 rearrangement of %hi relocs in frob_file may confuse
2696 tc_gen_reloc. */
2697 if (unmatched_hi)
2698 {
2699 frag_wane (frag_now);
2700 frag_new (0);
2701 }
2702}
2703
2704/* This function forgets that there was any previous instruction or
2705 label. If PRESERVE is non-zero, it remembers enough information to
bdaaa2e1 2706 know whether nops are needed before a noreorder section. */
252b5132
RH
2707
2708static void
2709mips_no_prev_insn (preserve)
2710 int preserve;
2711{
2712 if (! preserve)
2713 {
2714 prev_insn.insn_mo = &dummy_opcode;
2715 prev_prev_insn.insn_mo = &dummy_opcode;
2716 prev_nop_frag = NULL;
2717 prev_nop_frag_holds = 0;
2718 prev_nop_frag_required = 0;
2719 prev_nop_frag_since = 0;
2720 }
2721 prev_insn_valid = 0;
2722 prev_insn_is_delay_slot = 0;
2723 prev_insn_unreordered = 0;
2724 prev_insn_extended = 0;
f6688943
TS
2725 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2726 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2727 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
252b5132
RH
2728 prev_prev_insn_unreordered = 0;
2729 mips_clear_insn_labels ();
2730}
2731
2732/* This function must be called whenever we turn on noreorder or emit
2733 something other than instructions. It inserts any NOPS which might
2734 be needed by the previous instruction, and clears the information
2735 kept for the previous instructions. The INSNS parameter is true if
bdaaa2e1 2736 instructions are to follow. */
252b5132
RH
2737
2738static void
2739mips_emit_delays (insns)
2740 boolean insns;
2741{
2742 if (! mips_opts.noreorder)
2743 {
2744 int nops;
2745
2746 nops = 0;
2747 if ((! mips_opts.mips16
9ce8a5dd 2748 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
2749 && (! cop_interlocks
2750 && (prev_insn.insn_mo->pinfo
2751 & (INSN_LOAD_COPROC_DELAY
2752 | INSN_COPROC_MOVE_DELAY
2753 | INSN_WRITE_COND_CODE))))
2754 || (! hilo_interlocks
2755 && (prev_insn.insn_mo->pinfo
2756 & (INSN_READ_LO
2757 | INSN_READ_HI)))
2758 || (! mips_opts.mips16
2759 && ! gpr_interlocks
bdaaa2e1 2760 && (prev_insn.insn_mo->pinfo
252b5132
RH
2761 & INSN_LOAD_MEMORY_DELAY))
2762 || (! mips_opts.mips16
e7af610e 2763 && mips_opts.isa == ISA_MIPS1
252b5132
RH
2764 && (prev_insn.insn_mo->pinfo
2765 & INSN_COPROC_MEMORY_DELAY)))
2766 {
beae10d5 2767 /* Itbl support may require additional care here. */
252b5132
RH
2768 ++nops;
2769 if ((! mips_opts.mips16
9ce8a5dd 2770 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
2771 && (! cop_interlocks
2772 && prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2773 || (! hilo_interlocks
2774 && ((prev_insn.insn_mo->pinfo & INSN_READ_HI)
2775 || (prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2776 ++nops;
2777
2778 if (prev_insn_unreordered)
2779 nops = 0;
2780 }
2781 else if ((! mips_opts.mips16
9ce8a5dd 2782 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
2783 && (! cop_interlocks
2784 && prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2785 || (! hilo_interlocks
2786 && ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
2787 || (prev_prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2788 {
beae10d5 2789 /* Itbl support may require additional care here. */
252b5132
RH
2790 if (! prev_prev_insn_unreordered)
2791 ++nops;
2792 }
2793
2794 if (nops > 0)
2795 {
2796 struct insn_label_list *l;
2797
2798 if (insns)
2799 {
2800 /* Record the frag which holds the nop instructions, so
2801 that we can remove them if we don't need them. */
2802 frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2803 prev_nop_frag = frag_now;
2804 prev_nop_frag_holds = nops;
2805 prev_nop_frag_required = 0;
2806 prev_nop_frag_since = 0;
2807 }
2808
2809 for (; nops > 0; --nops)
2810 emit_nop ();
2811
2812 if (insns)
2813 {
2814 /* Move on to a new frag, so that it is safe to simply
bdaaa2e1 2815 decrease the size of prev_nop_frag. */
252b5132
RH
2816 frag_wane (frag_now);
2817 frag_new (0);
2818 }
2819
2820 for (l = insn_labels; l != NULL; l = l->next)
2821 {
98aa84af
AM
2822 valueT val;
2823
252b5132 2824 assert (S_GET_SEGMENT (l->label) == now_seg);
49309057 2825 symbol_set_frag (l->label, frag_now);
98aa84af 2826 val = (valueT) frag_now_fix ();
252b5132
RH
2827 /* mips16 text labels are stored as odd. */
2828 if (mips_opts.mips16)
98aa84af
AM
2829 val += 1;
2830 S_SET_VALUE (l->label, val);
252b5132
RH
2831 }
2832 }
2833 }
2834
2835 /* Mark instruction labels in mips16 mode. */
2836 if (mips_opts.mips16 && insns)
2837 mips16_mark_labels ();
2838
2839 mips_no_prev_insn (insns);
2840}
2841
2842/* Build an instruction created by a macro expansion. This is passed
2843 a pointer to the count of instructions created so far, an
2844 expression, the name of the instruction to build, an operand format
2845 string, and corresponding arguments. */
2846
2847#ifdef USE_STDARG
2848static void
2849macro_build (char *place,
2850 int *counter,
2851 expressionS * ep,
2852 const char *name,
2853 const char *fmt,
2854 ...)
2855#else
2856static void
2857macro_build (place, counter, ep, name, fmt, va_alist)
2858 char *place;
2859 int *counter;
2860 expressionS *ep;
2861 const char *name;
2862 const char *fmt;
2863 va_dcl
2864#endif
2865{
2866 struct mips_cl_insn insn;
f6688943 2867 bfd_reloc_code_real_type r[3];
252b5132 2868 va_list args;
252b5132
RH
2869
2870#ifdef USE_STDARG
2871 va_start (args, fmt);
2872#else
2873 va_start (args);
2874#endif
2875
2876 /*
2877 * If the macro is about to expand into a second instruction,
2878 * print a warning if needed. We need to pass ip as a parameter
2879 * to generate a better warning message here...
2880 */
2881 if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
2882 as_warn (_("Macro instruction expanded into multiple instructions"));
2883
2884 if (place == NULL)
2885 *counter += 1; /* bump instruction counter */
2886
2887 if (mips_opts.mips16)
2888 {
2889 mips16_macro_build (place, counter, ep, name, fmt, args);
2890 va_end (args);
2891 return;
2892 }
2893
f6688943
TS
2894 r[0] = BFD_RELOC_UNUSED;
2895 r[1] = BFD_RELOC_UNUSED;
2896 r[2] = BFD_RELOC_UNUSED;
252b5132
RH
2897 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
2898 assert (insn.insn_mo);
2899 assert (strcmp (name, insn.insn_mo->name) == 0);
2900
2901 /* Search until we get a match for NAME. */
2902 while (1)
2903 {
252b5132
RH
2904 if (strcmp (fmt, insn.insn_mo->args) == 0
2905 && insn.insn_mo->pinfo != INSN_MACRO
1f25f5d3
CD
2906 && OPCODE_IS_MEMBER (insn.insn_mo,
2907 (mips_opts.isa
2908 | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
2909 mips_arch)
ec68c924 2910 && (mips_arch != CPU_R4650 || (insn.insn_mo->pinfo & FP_D) == 0))
252b5132
RH
2911 break;
2912
2913 ++insn.insn_mo;
2914 assert (insn.insn_mo->name);
2915 assert (strcmp (name, insn.insn_mo->name) == 0);
2916 }
2917
2918 insn.insn_opcode = insn.insn_mo->match;
2919 for (;;)
2920 {
2921 switch (*fmt++)
2922 {
2923 case '\0':
2924 break;
2925
2926 case ',':
2927 case '(':
2928 case ')':
2929 continue;
2930
2931 case 't':
2932 case 'w':
2933 case 'E':
38487616 2934 insn.insn_opcode |= va_arg (args, int) << OP_SH_RT;
252b5132
RH
2935 continue;
2936
2937 case 'c':
38487616
TS
2938 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE;
2939 continue;
2940
252b5132
RH
2941 case 'T':
2942 case 'W':
38487616 2943 insn.insn_opcode |= va_arg (args, int) << OP_SH_FT;
252b5132
RH
2944 continue;
2945
2946 case 'd':
2947 case 'G':
38487616 2948 insn.insn_opcode |= va_arg (args, int) << OP_SH_RD;
252b5132
RH
2949 continue;
2950
4372b673
NC
2951 case 'U':
2952 {
2953 int tmp = va_arg (args, int);
2954
38487616
TS
2955 insn.insn_opcode |= tmp << OP_SH_RT;
2956 insn.insn_opcode |= tmp << OP_SH_RD;
beae10d5 2957 continue;
4372b673
NC
2958 }
2959
252b5132
RH
2960 case 'V':
2961 case 'S':
38487616 2962 insn.insn_opcode |= va_arg (args, int) << OP_SH_FS;
252b5132
RH
2963 continue;
2964
2965 case 'z':
2966 continue;
2967
2968 case '<':
38487616 2969 insn.insn_opcode |= va_arg (args, int) << OP_SH_SHAMT;
252b5132
RH
2970 continue;
2971
2972 case 'D':
38487616 2973 insn.insn_opcode |= va_arg (args, int) << OP_SH_FD;
252b5132
RH
2974 continue;
2975
2976 case 'B':
38487616 2977 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE20;
252b5132
RH
2978 continue;
2979
4372b673 2980 case 'J':
38487616 2981 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE19;
4372b673
NC
2982 continue;
2983
252b5132 2984 case 'q':
38487616 2985 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE2;
252b5132
RH
2986 continue;
2987
2988 case 'b':
2989 case 's':
2990 case 'r':
2991 case 'v':
38487616 2992 insn.insn_opcode |= va_arg (args, int) << OP_SH_RS;
252b5132
RH
2993 continue;
2994
2995 case 'i':
2996 case 'j':
2997 case 'o':
f6688943 2998 *r = (bfd_reloc_code_real_type) va_arg (args, int);
cdf6fd85 2999 assert (*r == BFD_RELOC_GPREL16
f6688943
TS
3000 || *r == BFD_RELOC_MIPS_LITERAL
3001 || *r == BFD_RELOC_MIPS_HIGHER
3002 || *r == BFD_RELOC_HI16_S
3003 || *r == BFD_RELOC_LO16
3004 || *r == BFD_RELOC_MIPS_GOT16
3005 || *r == BFD_RELOC_MIPS_CALL16
3006 || *r == BFD_RELOC_MIPS_GOT_LO16
3007 || *r == BFD_RELOC_MIPS_CALL_LO16
252b5132 3008 || (ep->X_op == O_subtract
f6688943 3009 && *r == BFD_RELOC_PCREL_LO16));
252b5132
RH
3010 continue;
3011
3012 case 'u':
f6688943 3013 *r = (bfd_reloc_code_real_type) va_arg (args, int);
252b5132
RH
3014 assert (ep != NULL
3015 && (ep->X_op == O_constant
3016 || (ep->X_op == O_symbol
f6688943
TS
3017 && (*r == BFD_RELOC_MIPS_HIGHEST
3018 || *r == BFD_RELOC_HI16_S
3019 || *r == BFD_RELOC_HI16
3020 || *r == BFD_RELOC_GPREL16
3021 || *r == BFD_RELOC_MIPS_GOT_HI16
3022 || *r == BFD_RELOC_MIPS_CALL_HI16))
252b5132 3023 || (ep->X_op == O_subtract
f6688943 3024 && *r == BFD_RELOC_PCREL_HI16_S)));
252b5132
RH
3025 continue;
3026
3027 case 'p':
3028 assert (ep != NULL);
3029 /*
3030 * This allows macro() to pass an immediate expression for
3031 * creating short branches without creating a symbol.
3032 * Note that the expression still might come from the assembly
3033 * input, in which case the value is not checked for range nor
3034 * is a relocation entry generated (yuck).
3035 */
3036 if (ep->X_op == O_constant)
3037 {
3038 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
3039 ep = NULL;
3040 }
3041 else
cb56d3d3 3042 if (mips_pic == EMBEDDED_PIC)
f6688943 3043 *r = BFD_RELOC_16_PCREL_S2;
cb56d3d3 3044 else
f6688943 3045 *r = BFD_RELOC_16_PCREL;
252b5132
RH
3046 continue;
3047
3048 case 'a':
3049 assert (ep != NULL);
f6688943 3050 *r = BFD_RELOC_MIPS_JMP;
252b5132
RH
3051 continue;
3052
3053 case 'C':
3054 insn.insn_opcode |= va_arg (args, unsigned long);
3055 continue;
3056
3057 default:
3058 internalError ();
3059 }
3060 break;
3061 }
3062 va_end (args);
f6688943 3063 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132
RH
3064
3065 append_insn (place, &insn, ep, r, false);
3066}
3067
3068static void
3069mips16_macro_build (place, counter, ep, name, fmt, args)
3070 char *place;
43841e91 3071 int *counter ATTRIBUTE_UNUSED;
252b5132
RH
3072 expressionS *ep;
3073 const char *name;
3074 const char *fmt;
3075 va_list args;
3076{
3077 struct mips_cl_insn insn;
f6688943
TS
3078 bfd_reloc_code_real_type r[3]
3079 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 3080
252b5132
RH
3081 insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
3082 assert (insn.insn_mo);
3083 assert (strcmp (name, insn.insn_mo->name) == 0);
3084
3085 while (strcmp (fmt, insn.insn_mo->args) != 0
3086 || insn.insn_mo->pinfo == INSN_MACRO)
3087 {
3088 ++insn.insn_mo;
3089 assert (insn.insn_mo->name);
3090 assert (strcmp (name, insn.insn_mo->name) == 0);
3091 }
3092
3093 insn.insn_opcode = insn.insn_mo->match;
3094 insn.use_extend = false;
3095
3096 for (;;)
3097 {
3098 int c;
3099
3100 c = *fmt++;
3101 switch (c)
3102 {
3103 case '\0':
3104 break;
3105
3106 case ',':
3107 case '(':
3108 case ')':
3109 continue;
3110
3111 case 'y':
3112 case 'w':
3113 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
3114 continue;
3115
3116 case 'x':
3117 case 'v':
3118 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
3119 continue;
3120
3121 case 'z':
3122 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
3123 continue;
3124
3125 case 'Z':
3126 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
3127 continue;
3128
3129 case '0':
3130 case 'S':
3131 case 'P':
3132 case 'R':
3133 continue;
3134
3135 case 'X':
3136 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
3137 continue;
3138
3139 case 'Y':
3140 {
3141 int regno;
3142
3143 regno = va_arg (args, int);
3144 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
3145 insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
3146 }
3147 continue;
3148
3149 case '<':
3150 case '>':
3151 case '4':
3152 case '5':
3153 case 'H':
3154 case 'W':
3155 case 'D':
3156 case 'j':
3157 case '8':
3158 case 'V':
3159 case 'C':
3160 case 'U':
3161 case 'k':
3162 case 'K':
3163 case 'p':
3164 case 'q':
3165 {
3166 assert (ep != NULL);
3167
3168 if (ep->X_op != O_constant)
f6688943 3169 *r = BFD_RELOC_UNUSED + c;
252b5132
RH
3170 else
3171 {
c4e7957c
TS
3172 mips16_immed (NULL, 0, c, ep->X_add_number, false, false,
3173 false, &insn.insn_opcode, &insn.use_extend,
3174 &insn.extend);
252b5132 3175 ep = NULL;
f6688943 3176 *r = BFD_RELOC_UNUSED;
252b5132
RH
3177 }
3178 }
3179 continue;
3180
3181 case '6':
3182 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
3183 continue;
3184 }
3185
3186 break;
3187 }
3188
f6688943 3189 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132
RH
3190
3191 append_insn (place, &insn, ep, r, false);
3192}
3193
3194/*
3195 * Generate a "lui" instruction.
3196 */
3197static void
3198macro_build_lui (place, counter, ep, regnum)
3199 char *place;
3200 int *counter;
3201 expressionS *ep;
3202 int regnum;
3203{
3204 expressionS high_expr;
3205 struct mips_cl_insn insn;
f6688943
TS
3206 bfd_reloc_code_real_type r[3]
3207 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132
RH
3208 CONST char *name = "lui";
3209 CONST char *fmt = "t,u";
3210
3211 assert (! mips_opts.mips16);
3212
3213 if (place == NULL)
3214 high_expr = *ep;
3215 else
3216 {
3217 high_expr.X_op = O_constant;
3218 high_expr.X_add_number = ep->X_add_number;
3219 }
3220
3221 if (high_expr.X_op == O_constant)
3222 {
3223 /* we can compute the instruction now without a relocation entry */
e7d556df
TS
3224 high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3225 >> 16) & 0xffff;
f6688943 3226 *r = BFD_RELOC_UNUSED;
252b5132 3227 }
f6688943 3228 else if (! HAVE_NEWABI)
252b5132
RH
3229 {
3230 assert (ep->X_op == O_symbol);
3231 /* _gp_disp is a special case, used from s_cpload. */
3232 assert (mips_pic == NO_PIC
3233 || strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0);
f6688943 3234 *r = BFD_RELOC_HI16_S;
252b5132
RH
3235 }
3236
3237 /*
3238 * If the macro is about to expand into a second instruction,
3239 * print a warning if needed. We need to pass ip as a parameter
3240 * to generate a better warning message here...
3241 */
3242 if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
3243 as_warn (_("Macro instruction expanded into multiple instructions"));
3244
3245 if (place == NULL)
3246 *counter += 1; /* bump instruction counter */
3247
3248 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3249 assert (insn.insn_mo);
3250 assert (strcmp (name, insn.insn_mo->name) == 0);
3251 assert (strcmp (fmt, insn.insn_mo->args) == 0);
3252
3253 insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
f6688943 3254 if (*r == BFD_RELOC_UNUSED)
252b5132
RH
3255 {
3256 insn.insn_opcode |= high_expr.X_add_number;
3257 append_insn (place, &insn, NULL, r, false);
3258 }
3259 else
3260 append_insn (place, &insn, &high_expr, r, false);
3261}
3262
3263/* set_at()
3264 * Generates code to set the $at register to true (one)
3265 * if reg is less than the immediate expression.
3266 */
3267static void
3268set_at (counter, reg, unsignedp)
3269 int *counter;
3270 int reg;
3271 int unsignedp;
3272{
3273 if (imm_expr.X_op == O_constant
3274 && imm_expr.X_add_number >= -0x8000
3275 && imm_expr.X_add_number < 0x8000)
3276 macro_build ((char *) NULL, counter, &imm_expr,
3277 unsignedp ? "sltiu" : "slti",
3278 "t,r,j", AT, reg, (int) BFD_RELOC_LO16);
3279 else
3280 {
4d34fb5f 3281 load_register (counter, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9 3282 macro_build ((char *) NULL, counter, (expressionS *) NULL,
252b5132
RH
3283 unsignedp ? "sltu" : "slt",
3284 "d,v,t", AT, reg, AT);
3285 }
3286}
3287
3288/* Warn if an expression is not a constant. */
3289
3290static void
3291check_absolute_expr (ip, ex)
3292 struct mips_cl_insn *ip;
3293 expressionS *ex;
3294{
3295 if (ex->X_op == O_big)
3296 as_bad (_("unsupported large constant"));
3297 else if (ex->X_op != O_constant)
3298 as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
3299}
3300
3301/* Count the leading zeroes by performing a binary chop. This is a
3302 bulky bit of source, but performance is a LOT better for the
3303 majority of values than a simple loop to count the bits:
3304 for (lcnt = 0; (lcnt < 32); lcnt++)
3305 if ((v) & (1 << (31 - lcnt)))
3306 break;
3307 However it is not code size friendly, and the gain will drop a bit
3308 on certain cached systems.
3309*/
3310#define COUNT_TOP_ZEROES(v) \
3311 (((v) & ~0xffff) == 0 \
3312 ? ((v) & ~0xff) == 0 \
3313 ? ((v) & ~0xf) == 0 \
3314 ? ((v) & ~0x3) == 0 \
3315 ? ((v) & ~0x1) == 0 \
3316 ? !(v) \
3317 ? 32 \
3318 : 31 \
3319 : 30 \
3320 : ((v) & ~0x7) == 0 \
3321 ? 29 \
3322 : 28 \
3323 : ((v) & ~0x3f) == 0 \
3324 ? ((v) & ~0x1f) == 0 \
3325 ? 27 \
3326 : 26 \
3327 : ((v) & ~0x7f) == 0 \
3328 ? 25 \
3329 : 24 \
3330 : ((v) & ~0xfff) == 0 \
3331 ? ((v) & ~0x3ff) == 0 \
3332 ? ((v) & ~0x1ff) == 0 \
3333 ? 23 \
3334 : 22 \
3335 : ((v) & ~0x7ff) == 0 \
3336 ? 21 \
3337 : 20 \
3338 : ((v) & ~0x3fff) == 0 \
3339 ? ((v) & ~0x1fff) == 0 \
3340 ? 19 \
3341 : 18 \
3342 : ((v) & ~0x7fff) == 0 \
3343 ? 17 \
3344 : 16 \
3345 : ((v) & ~0xffffff) == 0 \
3346 ? ((v) & ~0xfffff) == 0 \
3347 ? ((v) & ~0x3ffff) == 0 \
3348 ? ((v) & ~0x1ffff) == 0 \
3349 ? 15 \
3350 : 14 \
3351 : ((v) & ~0x7ffff) == 0 \
3352 ? 13 \
3353 : 12 \
3354 : ((v) & ~0x3fffff) == 0 \
3355 ? ((v) & ~0x1fffff) == 0 \
3356 ? 11 \
3357 : 10 \
3358 : ((v) & ~0x7fffff) == 0 \
3359 ? 9 \
3360 : 8 \
3361 : ((v) & ~0xfffffff) == 0 \
3362 ? ((v) & ~0x3ffffff) == 0 \
3363 ? ((v) & ~0x1ffffff) == 0 \
3364 ? 7 \
3365 : 6 \
3366 : ((v) & ~0x7ffffff) == 0 \
3367 ? 5 \
3368 : 4 \
3369 : ((v) & ~0x3fffffff) == 0 \
3370 ? ((v) & ~0x1fffffff) == 0 \
3371 ? 3 \
3372 : 2 \
3373 : ((v) & ~0x7fffffff) == 0 \
3374 ? 1 \
3375 : 0)
3376
6373ee54
CD
3377/* Is the given value a sign-extended 32-bit value? */
3378#define IS_SEXT_32BIT_NUM(x) \
3379 (((x) &~ (offsetT) 0x7fffffff) == 0 \
3380 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
3381
252b5132
RH
3382/* load_register()
3383 * This routine generates the least number of instructions neccessary to load
3384 * an absolute expression value into a register.
3385 */
3386static void
3387load_register (counter, reg, ep, dbl)
3388 int *counter;
3389 int reg;
3390 expressionS *ep;
3391 int dbl;
3392{
3393 int freg;
3394 expressionS hi32, lo32;
3395
3396 if (ep->X_op != O_big)
3397 {
3398 assert (ep->X_op == O_constant);
3399 if (ep->X_add_number < 0x8000
3400 && (ep->X_add_number >= 0
3401 || (ep->X_add_number >= -0x8000
3402 && (! dbl
3403 || ! ep->X_unsigned
3404 || sizeof (ep->X_add_number) > 4))))
3405 {
3406 /* We can handle 16 bit signed values with an addiu to
3407 $zero. No need to ever use daddiu here, since $zero and
3408 the result are always correct in 32 bit mode. */
3409 macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3410 (int) BFD_RELOC_LO16);
3411 return;
3412 }
3413 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3414 {
3415 /* We can handle 16 bit unsigned values with an ori to
3416 $zero. */
3417 macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, 0,
3418 (int) BFD_RELOC_LO16);
3419 return;
3420 }
6373ee54 3421 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)
252b5132
RH
3422 && (! dbl
3423 || ! ep->X_unsigned
3424 || sizeof (ep->X_add_number) > 4
3425 || (ep->X_add_number & 0x80000000) == 0))
ca4e0257 3426 || ((HAVE_32BIT_GPRS || ! dbl)
252b5132 3427 && (ep->X_add_number &~ (offsetT) 0xffffffff) == 0)
ca4e0257 3428 || (HAVE_32BIT_GPRS
252b5132
RH
3429 && ! dbl
3430 && ((ep->X_add_number &~ (offsetT) 0xffffffff)
3431 == ~ (offsetT) 0xffffffff)))
3432 {
3433 /* 32 bit values require an lui. */
3434 macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3435 (int) BFD_RELOC_HI16);
3436 if ((ep->X_add_number & 0xffff) != 0)
3437 macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, reg,
3438 (int) BFD_RELOC_LO16);
3439 return;
3440 }
3441 }
3442
3443 /* The value is larger than 32 bits. */
3444
ca4e0257 3445 if (HAVE_32BIT_GPRS)
252b5132 3446 {
956cd1d6
TS
3447 as_bad (_("Number (0x%lx) larger than 32 bits"),
3448 (unsigned long) ep->X_add_number);
252b5132
RH
3449 macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3450 (int) BFD_RELOC_LO16);
3451 return;
3452 }
3453
3454 if (ep->X_op != O_big)
3455 {
3456 hi32 = *ep;
3457 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3458 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3459 hi32.X_add_number &= 0xffffffff;
3460 lo32 = *ep;
3461 lo32.X_add_number &= 0xffffffff;
3462 }
3463 else
3464 {
3465 assert (ep->X_add_number > 2);
3466 if (ep->X_add_number == 3)
3467 generic_bignum[3] = 0;
3468 else if (ep->X_add_number > 4)
3469 as_bad (_("Number larger than 64 bits"));
3470 lo32.X_op = O_constant;
3471 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3472 hi32.X_op = O_constant;
3473 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3474 }
3475
3476 if (hi32.X_add_number == 0)
3477 freg = 0;
3478 else
3479 {
3480 int shift, bit;
3481 unsigned long hi, lo;
3482
956cd1d6 3483 if (hi32.X_add_number == (offsetT) 0xffffffff)
beae10d5
KH
3484 {
3485 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3486 {
3487 macro_build ((char *) NULL, counter, &lo32, "addiu", "t,r,j",
252b5132 3488 reg, 0, (int) BFD_RELOC_LO16);
beae10d5
KH
3489 return;
3490 }
3491 if (lo32.X_add_number & 0x80000000)
3492 {
3493 macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3494 (int) BFD_RELOC_HI16);
252b5132
RH
3495 if (lo32.X_add_number & 0xffff)
3496 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i",
3497 reg, reg, (int) BFD_RELOC_LO16);
beae10d5
KH
3498 return;
3499 }
3500 }
252b5132
RH
3501
3502 /* Check for 16bit shifted constant. We know that hi32 is
3503 non-zero, so start the mask on the first bit of the hi32
3504 value. */
3505 shift = 17;
3506 do
beae10d5
KH
3507 {
3508 unsigned long himask, lomask;
3509
3510 if (shift < 32)
3511 {
3512 himask = 0xffff >> (32 - shift);
3513 lomask = (0xffff << shift) & 0xffffffff;
3514 }
3515 else
3516 {
3517 himask = 0xffff << (shift - 32);
3518 lomask = 0;
3519 }
3520 if ((hi32.X_add_number & ~(offsetT) himask) == 0
3521 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3522 {
3523 expressionS tmp;
3524
3525 tmp.X_op = O_constant;
3526 if (shift < 32)
3527 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3528 | (lo32.X_add_number >> shift));
3529 else
3530 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
3531 macro_build ((char *) NULL, counter, &tmp,
3532 "ori", "t,r,i", reg, 0,
3533 (int) BFD_RELOC_LO16);
2396cfb9 3534 macro_build ((char *) NULL, counter, (expressionS *) NULL,
beae10d5
KH
3535 (shift >= 32) ? "dsll32" : "dsll",
3536 "d,w,<", reg, reg,
3537 (shift >= 32) ? shift - 32 : shift);
3538 return;
3539 }
3540 shift++;
3541 }
3542 while (shift <= (64 - 16));
252b5132
RH
3543
3544 /* Find the bit number of the lowest one bit, and store the
3545 shifted value in hi/lo. */
3546 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3547 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3548 if (lo != 0)
3549 {
3550 bit = 0;
3551 while ((lo & 1) == 0)
3552 {
3553 lo >>= 1;
3554 ++bit;
3555 }
3556 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3557 hi >>= bit;
3558 }
3559 else
3560 {
3561 bit = 32;
3562 while ((hi & 1) == 0)
3563 {
3564 hi >>= 1;
3565 ++bit;
3566 }
3567 lo = hi;
3568 hi = 0;
3569 }
3570
3571 /* Optimize if the shifted value is a (power of 2) - 1. */
3572 if ((hi == 0 && ((lo + 1) & lo) == 0)
3573 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
beae10d5
KH
3574 {
3575 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
252b5132 3576 if (shift != 0)
beae10d5 3577 {
252b5132
RH
3578 expressionS tmp;
3579
3580 /* This instruction will set the register to be all
3581 ones. */
beae10d5
KH
3582 tmp.X_op = O_constant;
3583 tmp.X_add_number = (offsetT) -1;
3584 macro_build ((char *) NULL, counter, &tmp, "addiu", "t,r,j",
252b5132 3585 reg, 0, (int) BFD_RELOC_LO16);
beae10d5
KH
3586 if (bit != 0)
3587 {
3588 bit += shift;
2396cfb9 3589 macro_build ((char *) NULL, counter, (expressionS *) NULL,
beae10d5
KH
3590 (bit >= 32) ? "dsll32" : "dsll",
3591 "d,w,<", reg, reg,
3592 (bit >= 32) ? bit - 32 : bit);
3593 }
2396cfb9 3594 macro_build ((char *) NULL, counter, (expressionS *) NULL,
252b5132 3595 (shift >= 32) ? "dsrl32" : "dsrl",
beae10d5 3596 "d,w,<", reg, reg,
252b5132 3597 (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
3598 return;
3599 }
3600 }
252b5132
RH
3601
3602 /* Sign extend hi32 before calling load_register, because we can
3603 generally get better code when we load a sign extended value. */
3604 if ((hi32.X_add_number & 0x80000000) != 0)
beae10d5 3605 hi32.X_add_number |= ~(offsetT) 0xffffffff;
252b5132
RH
3606 load_register (counter, reg, &hi32, 0);
3607 freg = reg;
3608 }
3609 if ((lo32.X_add_number & 0xffff0000) == 0)
3610 {
3611 if (freg != 0)
3612 {
2396cfb9
TS
3613 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3614 "dsll32", "d,w,<", reg, freg, 0);
252b5132
RH
3615 freg = reg;
3616 }
3617 }
3618 else
3619 {
3620 expressionS mid16;
3621
956cd1d6 3622 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
beae10d5 3623 {
252b5132
RH
3624 macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3625 (int) BFD_RELOC_HI16);
956cd1d6
TS
3626 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3627 "dsrl32", "d,w,<", reg, reg, 0);
beae10d5
KH
3628 return;
3629 }
252b5132
RH
3630
3631 if (freg != 0)
3632 {
956cd1d6
TS
3633 macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3634 "d,w,<", reg, freg, 16);
252b5132
RH
3635 freg = reg;
3636 }
3637 mid16 = lo32;
3638 mid16.X_add_number >>= 16;
3639 macro_build ((char *) NULL, counter, &mid16, "ori", "t,r,i", reg,
3640 freg, (int) BFD_RELOC_LO16);
956cd1d6
TS
3641 macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3642 "d,w,<", reg, reg, 16);
252b5132
RH
3643 freg = reg;
3644 }
3645 if ((lo32.X_add_number & 0xffff) != 0)
3646 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i", reg, freg,
3647 (int) BFD_RELOC_LO16);
3648}
3649
3650/* Load an address into a register. */
3651
3652static void
d6bc6245 3653load_address (counter, reg, ep, dbl, used_at)
252b5132
RH
3654 int *counter;
3655 int reg;
3656 expressionS *ep;
d6bc6245
TS
3657 int dbl;
3658 int *used_at;
252b5132
RH
3659{
3660 char *p;
3661
3662 if (ep->X_op != O_constant
3663 && ep->X_op != O_symbol)
3664 {
3665 as_bad (_("expression too complex"));
3666 ep->X_op = O_constant;
3667 }
3668
3669 if (ep->X_op == O_constant)
3670 {
d6bc6245 3671 load_register (counter, reg, ep, dbl);
252b5132
RH
3672 return;
3673 }
3674
3675 if (mips_pic == NO_PIC)
3676 {
3677 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 3678 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
3679 Otherwise we want
3680 lui $reg,<sym> (BFD_RELOC_HI16_S)
3681 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
d6bc6245 3682 If we have an addend, we always use the latter form.
76b3015f 3683
d6bc6245
TS
3684 With 64bit address space and a usable $at we want
3685 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3686 lui $at,<sym> (BFD_RELOC_HI16_S)
3687 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3688 daddiu $at,<sym> (BFD_RELOC_LO16)
3689 dsll32 $reg,0
3690 dadd $reg,$reg,$at
76b3015f 3691
d6bc6245
TS
3692 If $at is already in use, we use an path which is suboptimal
3693 on superscalar processors.
3694 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3695 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3696 dsll $reg,16
3697 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
3698 dsll $reg,16
3699 daddiu $reg,<sym> (BFD_RELOC_LO16)
3700 */
956cd1d6 3701 if (dbl)
d6bc6245
TS
3702 {
3703 p = NULL;
3704
3705 /* We don't do GP optimization for now because RELAX_ENCODE can't
3706 hold the data for such large chunks. */
3707
3708 if (*used_at == 0)
3709 {
3710 macro_build (p, counter, ep, "lui", "t,u",
3711 reg, (int) BFD_RELOC_MIPS_HIGHEST);
3712 macro_build (p, counter, ep, "lui", "t,u",
3713 AT, (int) BFD_RELOC_HI16_S);
3714 macro_build (p, counter, ep, "daddiu", "t,r,j",
3715 reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3716 macro_build (p, counter, ep, "daddiu", "t,r,j",
3717 AT, AT, (int) BFD_RELOC_LO16);
2396cfb9
TS
3718 macro_build (p, counter, (expressionS *) NULL, "dsll32",
3719 "d,w,<", reg, reg, 0);
3720 macro_build (p, counter, (expressionS *) NULL, "dadd",
3721 "d,v,t", reg, reg, AT);
d6bc6245
TS
3722 *used_at = 1;
3723 }
3724 else
3725 {
3726 macro_build (p, counter, ep, "lui", "t,u",
3727 reg, (int) BFD_RELOC_MIPS_HIGHEST);
3728 macro_build (p, counter, ep, "daddiu", "t,r,j",
3729 reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
2396cfb9
TS
3730 macro_build (p, counter, (expressionS *) NULL, "dsll",
3731 "d,w,<", reg, reg, 16);
d6bc6245
TS
3732 macro_build (p, counter, ep, "daddiu", "t,r,j",
3733 reg, reg, (int) BFD_RELOC_HI16_S);
2396cfb9
TS
3734 macro_build (p, counter, (expressionS *) NULL, "dsll",
3735 "d,w,<", reg, reg, 16);
d6bc6245
TS
3736 macro_build (p, counter, ep, "daddiu", "t,r,j",
3737 reg, reg, (int) BFD_RELOC_LO16);
3738 }
3739 }
252b5132
RH
3740 else
3741 {
d6bc6245
TS
3742 p = NULL;
3743 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3744 && ! nopic_need_relax (ep->X_add_symbol, 1))
3745 {
3746 frag_grow (20);
3747 macro_build ((char *) NULL, counter, ep,
cdf6fd85
TS
3748 dbl ? "daddiu" : "addiu", "t,r,j", reg, GP,
3749 (int) BFD_RELOC_GPREL16);
d6bc6245
TS
3750 p = frag_var (rs_machine_dependent, 8, 0,
3751 RELAX_ENCODE (4, 8, 0, 4, 0,
3752 mips_opts.warn_about_macros),
956cd1d6 3753 ep->X_add_symbol, 0, NULL);
d6bc6245
TS
3754 }
3755 macro_build_lui (p, counter, ep, reg);
3756 if (p != NULL)
3757 p += 4;
956cd1d6 3758 macro_build (p, counter, ep, dbl ? "daddiu" : "addiu",
d6bc6245
TS
3759 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3760 }
252b5132
RH
3761 }
3762 else if (mips_pic == SVR4_PIC && ! mips_big_got)
3763 {
3764 expressionS ex;
3765
3766 /* If this is a reference to an external symbol, we want
3767 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3768 Otherwise we want
3769 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3770 nop
3771 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3772 If there is a constant, it must be added in after. */
3773 ex.X_add_number = ep->X_add_number;
3774 ep->X_add_number = 0;
3775 frag_grow (20);
3776 macro_build ((char *) NULL, counter, ep,
ca4e0257 3777 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
3778 "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT16, GP);
3779 macro_build ((char *) NULL, counter, (expressionS *) NULL, "nop", "");
3780 p = frag_var (rs_machine_dependent, 4, 0,
3781 RELAX_ENCODE (0, 4, -8, 0, 0, mips_opts.warn_about_macros),
3782 ep->X_add_symbol, (offsetT) 0, (char *) NULL);
3783 macro_build (p, counter, ep,
ca4e0257 3784 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
3785 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3786 if (ex.X_add_number != 0)
3787 {
3788 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3789 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3790 ex.X_op = O_constant;
3791 macro_build ((char *) NULL, counter, &ex,
ca4e0257 3792 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
3793 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3794 }
3795 }
3796 else if (mips_pic == SVR4_PIC)
3797 {
3798 expressionS ex;
3799 int off;
3800
3801 /* This is the large GOT case. If this is a reference to an
3802 external symbol, we want
3803 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
3804 addu $reg,$reg,$gp
3805 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
3806 Otherwise, for a reference to a local symbol, we want
3807 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3808 nop
3809 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3810 If there is a constant, it must be added in after. */
3811 ex.X_add_number = ep->X_add_number;
3812 ep->X_add_number = 0;
3813 if (reg_needs_delay (GP))
3814 off = 4;
3815 else
3816 off = 0;
3817 frag_grow (32);
3818 macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3819 (int) BFD_RELOC_MIPS_GOT_HI16);
3820 macro_build ((char *) NULL, counter, (expressionS *) NULL,
956cd1d6
TS
3821 dbl ? "daddu" : "addu", "d,v,t", reg, reg, GP);
3822 macro_build ((char *) NULL, counter, ep, dbl ? "ld" : "lw",
252b5132
RH
3823 "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT_LO16, reg);
3824 p = frag_var (rs_machine_dependent, 12 + off, 0,
3825 RELAX_ENCODE (12, 12 + off, off, 8 + off, 0,
3826 mips_opts.warn_about_macros),
956cd1d6 3827 ep->X_add_symbol, 0, NULL);
252b5132
RH
3828 if (off > 0)
3829 {
3830 /* We need a nop before loading from $gp. This special
3831 check is required because the lui which starts the main
3832 instruction stream does not refer to $gp, and so will not
3833 insert the nop which may be required. */
3834 macro_build (p, counter, (expressionS *) NULL, "nop", "");
3835 p += 4;
3836 }
956cd1d6 3837 macro_build (p, counter, ep, dbl ? "ld" : "lw",
252b5132
RH
3838 "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT16, GP);
3839 p += 4;
3840 macro_build (p, counter, (expressionS *) NULL, "nop", "");
3841 p += 4;
956cd1d6 3842 macro_build (p, counter, ep, dbl ? "daddiu" : "addiu",
252b5132
RH
3843 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3844 if (ex.X_add_number != 0)
3845 {
3846 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3847 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3848 ex.X_op = O_constant;
956cd1d6 3849 macro_build ((char *) NULL, counter, &ex, dbl ? "daddiu" : "addiu",
252b5132
RH
3850 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3851 }
3852 }
3853 else if (mips_pic == EMBEDDED_PIC)
3854 {
3855 /* We always do
cdf6fd85 3856 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132 3857 */
cdf6fd85
TS
3858 macro_build ((char *) NULL, counter, ep, dbl ? "daddiu" : "addiu",
3859 "t,r,j", reg, GP, (int) BFD_RELOC_GPREL16);
252b5132
RH
3860 }
3861 else
3862 abort ();
3863}
3864
ea1fb5dc
RS
3865/* Move the contents of register SOURCE into register DEST. */
3866
3867static void
3868move_register (counter, dest, source)
3869 int *counter;
3870 int dest;
3871 int source;
3872{
3873 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3874 HAVE_32BIT_GPRS ? "addu" : "daddu",
3875 "d,v,t", dest, source, 0);
3876}
3877
252b5132
RH
3878/*
3879 * Build macros
3880 * This routine implements the seemingly endless macro or synthesized
3881 * instructions and addressing modes in the mips assembly language. Many
3882 * of these macros are simple and are similar to each other. These could
3883 * probably be handled by some kind of table or grammer aproach instead of
3884 * this verbose method. Others are not simple macros but are more like
3885 * optimizing code generation.
3886 * One interesting optimization is when several store macros appear
3887 * consecutivly that would load AT with the upper half of the same address.
3888 * The ensuing load upper instructions are ommited. This implies some kind
3889 * of global optimization. We currently only optimize within a single macro.
3890 * For many of the load and store macros if the address is specified as a
3891 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
3892 * first load register 'at' with zero and use it as the base register. The
3893 * mips assembler simply uses register $zero. Just one tiny optimization
3894 * we're missing.
3895 */
3896static void
3897macro (ip)
3898 struct mips_cl_insn *ip;
3899{
3900 register int treg, sreg, dreg, breg;
3901 int tempreg;
3902 int mask;
3903 int icnt = 0;
43841e91 3904 int used_at = 0;
252b5132
RH
3905 expressionS expr1;
3906 const char *s;
3907 const char *s2;
3908 const char *fmt;
3909 int likely = 0;
3910 int dbl = 0;
3911 int coproc = 0;
3912 int lr = 0;
3913 int imm = 0;
3914 offsetT maxnum;
3915 int off;
3916 bfd_reloc_code_real_type r;
3917 char *p;
3918 int hold_mips_optimize;
3919
3920 assert (! mips_opts.mips16);
3921
3922 treg = (ip->insn_opcode >> 16) & 0x1f;
3923 dreg = (ip->insn_opcode >> 11) & 0x1f;
3924 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
3925 mask = ip->insn_mo->mask;
3926
3927 expr1.X_op = O_constant;
3928 expr1.X_op_symbol = NULL;
3929 expr1.X_add_symbol = NULL;
3930 expr1.X_add_number = 1;
3931
3932 switch (mask)
3933 {
3934 case M_DABS:
3935 dbl = 1;
3936 case M_ABS:
3937 /* bgez $a0,.+12
3938 move v0,$a0
3939 sub v0,$zero,$a0
3940 */
3941
3942 mips_emit_delays (true);
3943 ++mips_opts.noreorder;
3944 mips_any_noreorder = 1;
3945
3946 expr1.X_add_number = 8;
3947 macro_build ((char *) NULL, &icnt, &expr1, "bgez", "s,p", sreg);
3948 if (dreg == sreg)
2396cfb9
TS
3949 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
3950 0);
252b5132 3951 else
ea1fb5dc 3952 move_register (&icnt, dreg, sreg);
2396cfb9 3953 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 3954 dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
252b5132
RH
3955
3956 --mips_opts.noreorder;
3957 return;
3958
3959 case M_ADD_I:
3960 s = "addi";
3961 s2 = "add";
3962 goto do_addi;
3963 case M_ADDU_I:
3964 s = "addiu";
3965 s2 = "addu";
3966 goto do_addi;
3967 case M_DADD_I:
3968 dbl = 1;
3969 s = "daddi";
3970 s2 = "dadd";
3971 goto do_addi;
3972 case M_DADDU_I:
3973 dbl = 1;
3974 s = "daddiu";
3975 s2 = "daddu";
3976 do_addi:
3977 if (imm_expr.X_op == O_constant
3978 && imm_expr.X_add_number >= -0x8000
3979 && imm_expr.X_add_number < 0x8000)
3980 {
3981 macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,j", treg, sreg,
3982 (int) BFD_RELOC_LO16);
3983 return;
3984 }
3985 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9
TS
3986 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
3987 treg, sreg, AT);
252b5132
RH
3988 break;
3989
3990 case M_AND_I:
3991 s = "andi";
3992 s2 = "and";
3993 goto do_bit;
3994 case M_OR_I:
3995 s = "ori";
3996 s2 = "or";
3997 goto do_bit;
3998 case M_NOR_I:
3999 s = "";
4000 s2 = "nor";
4001 goto do_bit;
4002 case M_XOR_I:
4003 s = "xori";
4004 s2 = "xor";
4005 do_bit:
4006 if (imm_expr.X_op == O_constant
4007 && imm_expr.X_add_number >= 0
4008 && imm_expr.X_add_number < 0x10000)
4009 {
4010 if (mask != M_NOR_I)
4011 macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,i", treg,
4012 sreg, (int) BFD_RELOC_LO16);
4013 else
4014 {
4015 macro_build ((char *) NULL, &icnt, &imm_expr, "ori", "t,r,i",
4016 treg, sreg, (int) BFD_RELOC_LO16);
2396cfb9
TS
4017 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nor",
4018 "d,v,t", treg, treg, 0);
252b5132
RH
4019 }
4020 return;
4021 }
4022
d6bc6245 4023 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
4024 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
4025 treg, sreg, AT);
252b5132
RH
4026 break;
4027
4028 case M_BEQ_I:
4029 s = "beq";
4030 goto beq_i;
4031 case M_BEQL_I:
4032 s = "beql";
4033 likely = 1;
4034 goto beq_i;
4035 case M_BNE_I:
4036 s = "bne";
4037 goto beq_i;
4038 case M_BNEL_I:
4039 s = "bnel";
4040 likely = 1;
4041 beq_i:
4042 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4043 {
4044 macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg,
4045 0);
4046 return;
4047 }
4d34fb5f 4048 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
252b5132
RH
4049 macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg, AT);
4050 break;
4051
4052 case M_BGEL:
4053 likely = 1;
4054 case M_BGE:
4055 if (treg == 0)
4056 {
4057 macro_build ((char *) NULL, &icnt, &offset_expr,
2396cfb9 4058 likely ? "bgezl" : "bgez", "s,p", sreg);
252b5132
RH
4059 return;
4060 }
4061 if (sreg == 0)
4062 {
4063 macro_build ((char *) NULL, &icnt, &offset_expr,
2396cfb9 4064 likely ? "blezl" : "blez", "s,p", treg);
252b5132
RH
4065 return;
4066 }
2396cfb9
TS
4067 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4068 AT, sreg, treg);
252b5132 4069 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4070 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4071 break;
4072
4073 case M_BGTL_I:
4074 likely = 1;
4075 case M_BGT_I:
4076 /* check for > max integer */
4077 maxnum = 0x7fffffff;
ca4e0257 4078 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
4079 {
4080 maxnum <<= 16;
4081 maxnum |= 0xffff;
4082 maxnum <<= 16;
4083 maxnum |= 0xffff;
4084 }
4085 if (imm_expr.X_op == O_constant
4086 && imm_expr.X_add_number >= maxnum
ca4e0257 4087 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
4088 {
4089 do_false:
4090 /* result is always false */
4091 if (! likely)
4092 {
39c0a331
L
4093 if (warn_nops)
4094 as_warn (_("Branch %s is always false (nop)"),
4095 ip->insn_mo->name);
2396cfb9
TS
4096 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop",
4097 "", 0);
252b5132
RH
4098 }
4099 else
4100 {
39c0a331
L
4101 if (warn_nops)
4102 as_warn (_("Branch likely %s is always false"),
4103 ip->insn_mo->name);
252b5132
RH
4104 macro_build ((char *) NULL, &icnt, &offset_expr, "bnel",
4105 "s,t,p", 0, 0);
4106 }
4107 return;
4108 }
4109 if (imm_expr.X_op != O_constant)
4110 as_bad (_("Unsupported large constant"));
4111 imm_expr.X_add_number++;
4112 /* FALLTHROUGH */
4113 case M_BGE_I:
4114 case M_BGEL_I:
4115 if (mask == M_BGEL_I)
4116 likely = 1;
4117 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4118 {
4119 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4120 likely ? "bgezl" : "bgez", "s,p", sreg);
252b5132
RH
4121 return;
4122 }
4123 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4124 {
4125 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4126 likely ? "bgtzl" : "bgtz", "s,p", sreg);
252b5132
RH
4127 return;
4128 }
4129 maxnum = 0x7fffffff;
ca4e0257 4130 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
4131 {
4132 maxnum <<= 16;
4133 maxnum |= 0xffff;
4134 maxnum <<= 16;
4135 maxnum |= 0xffff;
4136 }
4137 maxnum = - maxnum - 1;
4138 if (imm_expr.X_op == O_constant
4139 && imm_expr.X_add_number <= maxnum
ca4e0257 4140 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
4141 {
4142 do_true:
4143 /* result is always true */
4144 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
4145 macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
4146 return;
4147 }
4148 set_at (&icnt, sreg, 0);
4149 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4150 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4151 break;
4152
4153 case M_BGEUL:
4154 likely = 1;
4155 case M_BGEU:
4156 if (treg == 0)
4157 goto do_true;
4158 if (sreg == 0)
4159 {
4160 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4161 likely ? "beql" : "beq", "s,t,p", 0, treg);
252b5132
RH
4162 return;
4163 }
2396cfb9
TS
4164 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4165 "d,v,t", AT, sreg, treg);
252b5132 4166 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4167 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4168 break;
4169
4170 case M_BGTUL_I:
4171 likely = 1;
4172 case M_BGTU_I:
4173 if (sreg == 0
ca4e0257 4174 || (HAVE_32BIT_GPRS
252b5132 4175 && imm_expr.X_op == O_constant
956cd1d6 4176 && imm_expr.X_add_number == (offsetT) 0xffffffff))
252b5132
RH
4177 goto do_false;
4178 if (imm_expr.X_op != O_constant)
4179 as_bad (_("Unsupported large constant"));
4180 imm_expr.X_add_number++;
4181 /* FALLTHROUGH */
4182 case M_BGEU_I:
4183 case M_BGEUL_I:
4184 if (mask == M_BGEUL_I)
4185 likely = 1;
4186 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4187 goto do_true;
4188 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4189 {
4190 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4191 likely ? "bnel" : "bne", "s,t,p", sreg, 0);
252b5132
RH
4192 return;
4193 }
4194 set_at (&icnt, sreg, 1);
4195 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4196 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4197 break;
4198
4199 case M_BGTL:
4200 likely = 1;
4201 case M_BGT:
4202 if (treg == 0)
4203 {
4204 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4205 likely ? "bgtzl" : "bgtz", "s,p", sreg);
252b5132
RH
4206 return;
4207 }
4208 if (sreg == 0)
4209 {
4210 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4211 likely ? "bltzl" : "bltz", "s,p", treg);
252b5132
RH
4212 return;
4213 }
2396cfb9
TS
4214 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4215 AT, treg, sreg);
252b5132 4216 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4217 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4218 break;
4219
4220 case M_BGTUL:
4221 likely = 1;
4222 case M_BGTU:
4223 if (treg == 0)
4224 {
4225 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4226 likely ? "bnel" : "bne", "s,t,p", sreg, 0);
252b5132
RH
4227 return;
4228 }
4229 if (sreg == 0)
4230 goto do_false;
2396cfb9
TS
4231 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4232 "d,v,t", AT, treg, sreg);
252b5132 4233 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4234 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4235 break;
4236
4237 case M_BLEL:
4238 likely = 1;
4239 case M_BLE:
4240 if (treg == 0)
4241 {
4242 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4243 likely ? "blezl" : "blez", "s,p", sreg);
252b5132
RH
4244 return;
4245 }
4246 if (sreg == 0)
4247 {
4248 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4249 likely ? "bgezl" : "bgez", "s,p", treg);
252b5132
RH
4250 return;
4251 }
2396cfb9
TS
4252 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4253 AT, treg, sreg);
252b5132 4254 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4255 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4256 break;
4257
4258 case M_BLEL_I:
4259 likely = 1;
4260 case M_BLE_I:
4261 maxnum = 0x7fffffff;
ca4e0257 4262 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
4263 {
4264 maxnum <<= 16;
4265 maxnum |= 0xffff;
4266 maxnum <<= 16;
4267 maxnum |= 0xffff;
4268 }
4269 if (imm_expr.X_op == O_constant
4270 && imm_expr.X_add_number >= maxnum
ca4e0257 4271 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
4272 goto do_true;
4273 if (imm_expr.X_op != O_constant)
4274 as_bad (_("Unsupported large constant"));
4275 imm_expr.X_add_number++;
4276 /* FALLTHROUGH */
4277 case M_BLT_I:
4278 case M_BLTL_I:
4279 if (mask == M_BLTL_I)
4280 likely = 1;
4281 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4282 {
4283 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4284 likely ? "bltzl" : "bltz", "s,p", sreg);
252b5132
RH
4285 return;
4286 }
4287 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4288 {
4289 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4290 likely ? "blezl" : "blez", "s,p", sreg);
252b5132
RH
4291 return;
4292 }
4293 set_at (&icnt, sreg, 0);
4294 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4295 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4296 break;
4297
4298 case M_BLEUL:
4299 likely = 1;
4300 case M_BLEU:
4301 if (treg == 0)
4302 {
4303 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4304 likely ? "beql" : "beq", "s,t,p", sreg, 0);
252b5132
RH
4305 return;
4306 }
4307 if (sreg == 0)
4308 goto do_true;
2396cfb9
TS
4309 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4310 "d,v,t", AT, treg, sreg);
252b5132 4311 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4312 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4313 break;
4314
4315 case M_BLEUL_I:
4316 likely = 1;
4317 case M_BLEU_I:
4318 if (sreg == 0
ca4e0257 4319 || (HAVE_32BIT_GPRS
252b5132 4320 && imm_expr.X_op == O_constant
956cd1d6 4321 && imm_expr.X_add_number == (offsetT) 0xffffffff))
252b5132
RH
4322 goto do_true;
4323 if (imm_expr.X_op != O_constant)
4324 as_bad (_("Unsupported large constant"));
4325 imm_expr.X_add_number++;
4326 /* FALLTHROUGH */
4327 case M_BLTU_I:
4328 case M_BLTUL_I:
4329 if (mask == M_BLTUL_I)
4330 likely = 1;
4331 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4332 goto do_false;
4333 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4334 {
4335 macro_build ((char *) NULL, &icnt, &offset_expr,
4336 likely ? "beql" : "beq",
4337 "s,t,p", sreg, 0);
4338 return;
4339 }
4340 set_at (&icnt, sreg, 1);
4341 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4342 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4343 break;
4344
4345 case M_BLTL:
4346 likely = 1;
4347 case M_BLT:
4348 if (treg == 0)
4349 {
4350 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4351 likely ? "bltzl" : "bltz", "s,p", sreg);
252b5132
RH
4352 return;
4353 }
4354 if (sreg == 0)
4355 {
4356 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4357 likely ? "bgtzl" : "bgtz", "s,p", treg);
252b5132
RH
4358 return;
4359 }
2396cfb9
TS
4360 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4361 AT, sreg, treg);
252b5132 4362 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4363 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4364 break;
4365
4366 case M_BLTUL:
4367 likely = 1;
4368 case M_BLTU:
4369 if (treg == 0)
4370 goto do_false;
4371 if (sreg == 0)
4372 {
4373 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4374 likely ? "bnel" : "bne", "s,t,p", 0, treg);
252b5132
RH
4375 return;
4376 }
2396cfb9
TS
4377 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4378 "d,v,t", AT, sreg,
252b5132
RH
4379 treg);
4380 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4381 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4382 break;
4383
4384 case M_DDIV_3:
4385 dbl = 1;
4386 case M_DIV_3:
4387 s = "mflo";
4388 goto do_div3;
4389 case M_DREM_3:
4390 dbl = 1;
4391 case M_REM_3:
4392 s = "mfhi";
4393 do_div3:
4394 if (treg == 0)
4395 {
4396 as_warn (_("Divide by zero."));
4397 if (mips_trap)
2396cfb9
TS
4398 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4399 "s,t", 0, 0);
252b5132 4400 else
2396cfb9
TS
4401 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4402 "c", 7);
252b5132
RH
4403 return;
4404 }
4405
4406 mips_emit_delays (true);
4407 ++mips_opts.noreorder;
4408 mips_any_noreorder = 1;
4409 if (mips_trap)
4410 {
2396cfb9
TS
4411 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4412 "s,t", treg, 0);
4413 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 4414 dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
252b5132
RH
4415 }
4416 else
4417 {
4418 expr1.X_add_number = 8;
4419 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
2396cfb9 4420 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 4421 dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
2396cfb9
TS
4422 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4423 "c", 7);
252b5132
RH
4424 }
4425 expr1.X_add_number = -1;
4426 macro_build ((char *) NULL, &icnt, &expr1,
4427 dbl ? "daddiu" : "addiu",
4428 "t,r,j", AT, 0, (int) BFD_RELOC_LO16);
4429 expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
4430 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, AT);
4431 if (dbl)
4432 {
4433 expr1.X_add_number = 1;
4434 macro_build ((char *) NULL, &icnt, &expr1, "daddiu", "t,r,j", AT, 0,
4435 (int) BFD_RELOC_LO16);
2396cfb9
TS
4436 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsll32",
4437 "d,w,<", AT, AT, 31);
252b5132
RH
4438 }
4439 else
4440 {
4441 expr1.X_add_number = 0x80000000;
4442 macro_build ((char *) NULL, &icnt, &expr1, "lui", "t,u", AT,
4443 (int) BFD_RELOC_HI16);
4444 }
4445 if (mips_trap)
4446 {
2396cfb9
TS
4447 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4448 "s,t", sreg, AT);
252b5132
RH
4449 /* We want to close the noreorder block as soon as possible, so
4450 that later insns are available for delay slot filling. */
4451 --mips_opts.noreorder;
4452 }
4453 else
4454 {
4455 expr1.X_add_number = 8;
4456 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", sreg, AT);
2396cfb9
TS
4457 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
4458 0);
252b5132
RH
4459
4460 /* We want to close the noreorder block as soon as possible, so
4461 that later insns are available for delay slot filling. */
4462 --mips_opts.noreorder;
4463
2396cfb9
TS
4464 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4465 "c", 6);
252b5132 4466 }
2396cfb9 4467 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d", dreg);
252b5132
RH
4468 break;
4469
4470 case M_DIV_3I:
4471 s = "div";
4472 s2 = "mflo";
4473 goto do_divi;
4474 case M_DIVU_3I:
4475 s = "divu";
4476 s2 = "mflo";
4477 goto do_divi;
4478 case M_REM_3I:
4479 s = "div";
4480 s2 = "mfhi";
4481 goto do_divi;
4482 case M_REMU_3I:
4483 s = "divu";
4484 s2 = "mfhi";
4485 goto do_divi;
4486 case M_DDIV_3I:
4487 dbl = 1;
4488 s = "ddiv";
4489 s2 = "mflo";
4490 goto do_divi;
4491 case M_DDIVU_3I:
4492 dbl = 1;
4493 s = "ddivu";
4494 s2 = "mflo";
4495 goto do_divi;
4496 case M_DREM_3I:
4497 dbl = 1;
4498 s = "ddiv";
4499 s2 = "mfhi";
4500 goto do_divi;
4501 case M_DREMU_3I:
4502 dbl = 1;
4503 s = "ddivu";
4504 s2 = "mfhi";
4505 do_divi:
4506 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4507 {
4508 as_warn (_("Divide by zero."));
4509 if (mips_trap)
2396cfb9
TS
4510 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4511 "s,t", 0, 0);
252b5132 4512 else
2396cfb9
TS
4513 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4514 "c", 7);
252b5132
RH
4515 return;
4516 }
4517 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4518 {
4519 if (strcmp (s2, "mflo") == 0)
ea1fb5dc 4520 move_register (&icnt, dreg, sreg);
252b5132 4521 else
ea1fb5dc 4522 move_register (&icnt, dreg, 0);
252b5132
RH
4523 return;
4524 }
4525 if (imm_expr.X_op == O_constant
4526 && imm_expr.X_add_number == -1
4527 && s[strlen (s) - 1] != 'u')
4528 {
4529 if (strcmp (s2, "mflo") == 0)
4530 {
2396cfb9
TS
4531 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4532 dbl ? "dneg" : "neg", "d,w", dreg, sreg);
252b5132
RH
4533 }
4534 else
ea1fb5dc 4535 move_register (&icnt, dreg, 0);
252b5132
RH
4536 return;
4537 }
4538
4539 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9
TS
4540 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4541 sreg, AT);
4542 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
252b5132
RH
4543 break;
4544
4545 case M_DIVU_3:
4546 s = "divu";
4547 s2 = "mflo";
4548 goto do_divu3;
4549 case M_REMU_3:
4550 s = "divu";
4551 s2 = "mfhi";
4552 goto do_divu3;
4553 case M_DDIVU_3:
4554 s = "ddivu";
4555 s2 = "mflo";
4556 goto do_divu3;
4557 case M_DREMU_3:
4558 s = "ddivu";
4559 s2 = "mfhi";
4560 do_divu3:
4561 mips_emit_delays (true);
4562 ++mips_opts.noreorder;
4563 mips_any_noreorder = 1;
4564 if (mips_trap)
4565 {
2396cfb9
TS
4566 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4567 "s,t", treg, 0);
4568 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4569 sreg, treg);
252b5132
RH
4570 /* We want to close the noreorder block as soon as possible, so
4571 that later insns are available for delay slot filling. */
4572 --mips_opts.noreorder;
4573 }
4574 else
4575 {
4576 expr1.X_add_number = 8;
4577 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
2396cfb9
TS
4578 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4579 sreg, treg);
252b5132
RH
4580
4581 /* We want to close the noreorder block as soon as possible, so
4582 that later insns are available for delay slot filling. */
4583 --mips_opts.noreorder;
2396cfb9
TS
4584 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4585 "c", 7);
252b5132 4586 }
2396cfb9 4587 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
252b5132
RH
4588 return;
4589
4590 case M_DLA_AB:
4591 dbl = 1;
4592 case M_LA_AB:
4593 /* Load the address of a symbol into a register. If breg is not
4594 zero, we then add a base register to it. */
4595
afdbd6d0
CD
4596 if (treg == breg)
4597 {
4598 tempreg = AT;
4599 used_at = 1;
4600 }
4601 else
4602 {
4603 tempreg = treg;
4604 used_at = 0;
4605 }
4606
252b5132
RH
4607 /* When generating embedded PIC code, we permit expressions of
4608 the form
afdbd6d0
CD
4609 la $treg,foo-bar
4610 la $treg,foo-bar($breg)
bb2d6cd7 4611 where bar is an address in the current section. These are used
252b5132
RH
4612 when getting the addresses of functions. We don't permit
4613 X_add_number to be non-zero, because if the symbol is
4614 external the relaxing code needs to know that any addend is
4615 purely the offset to X_op_symbol. */
4616 if (mips_pic == EMBEDDED_PIC
4617 && offset_expr.X_op == O_subtract
49309057 4618 && (symbol_constant_p (offset_expr.X_op_symbol)
bb2d6cd7 4619 ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
49309057
ILT
4620 : (symbol_equated_p (offset_expr.X_op_symbol)
4621 && (S_GET_SEGMENT
4622 (symbol_get_value_expression (offset_expr.X_op_symbol)
4623 ->X_add_symbol)
bb2d6cd7 4624 == now_seg)))
bb2d6cd7
GK
4625 && (offset_expr.X_add_number == 0
4626 || OUTPUT_FLAVOR == bfd_target_elf_flavour))
252b5132 4627 {
afdbd6d0
CD
4628 if (breg == 0)
4629 {
4630 tempreg = treg;
4631 used_at = 0;
4632 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4633 tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4634 }
4635 else
4636 {
4637 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4638 tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4639 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4d34fb5f 4640 (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu",
afdbd6d0
CD
4641 "d,v,t", tempreg, tempreg, breg);
4642 }
252b5132 4643 macro_build ((char *) NULL, &icnt, &offset_expr,
4d34fb5f 4644 (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
afdbd6d0
CD
4645 "t,r,j", treg, tempreg, (int) BFD_RELOC_PCREL_LO16);
4646 if (! used_at)
4647 return;
4648 break;
252b5132
RH
4649 }
4650
4651 if (offset_expr.X_op != O_symbol
4652 && offset_expr.X_op != O_constant)
4653 {
4654 as_bad (_("expression too complex"));
4655 offset_expr.X_op = O_constant;
4656 }
4657
252b5132 4658 if (offset_expr.X_op == O_constant)
4d34fb5f
TS
4659 load_register (&icnt, tempreg, &offset_expr,
4660 ((mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
4661 ? (dbl || HAVE_64BIT_ADDRESSES)
4662 : HAVE_64BIT_ADDRESSES));
252b5132
RH
4663 else if (mips_pic == NO_PIC)
4664 {
d6bc6245 4665 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 4666 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
4667 Otherwise we want
4668 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
4669 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4670 If we have a constant, we need two instructions anyhow,
d6bc6245 4671 so we may as well always use the latter form.
76b3015f 4672
d6bc6245
TS
4673 With 64bit address space and a usable $at we want
4674 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4675 lui $at,<sym> (BFD_RELOC_HI16_S)
4676 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4677 daddiu $at,<sym> (BFD_RELOC_LO16)
4678 dsll32 $tempreg,0
4679 dadd $tempreg,$tempreg,$at
76b3015f 4680
d6bc6245
TS
4681 If $at is already in use, we use an path which is suboptimal
4682 on superscalar processors.
4683 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4684 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4685 dsll $tempreg,16
4686 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
4687 dsll $tempreg,16
4688 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
4689 */
4690 p = NULL;
4691 if (HAVE_64BIT_ADDRESSES)
252b5132 4692 {
d6bc6245
TS
4693 /* We don't do GP optimization for now because RELAX_ENCODE can't
4694 hold the data for such large chunks. */
4695
4696 if (used_at == 0)
4697 {
4698 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4699 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4700 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4701 AT, (int) BFD_RELOC_HI16_S);
4702 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4703 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4704 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4705 AT, AT, (int) BFD_RELOC_LO16);
2396cfb9
TS
4706 macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
4707 "d,w,<", tempreg, tempreg, 0);
4708 macro_build (p, &icnt, (expressionS *) NULL, "dadd", "d,v,t",
d6bc6245
TS
4709 tempreg, tempreg, AT);
4710 used_at = 1;
4711 }
4712 else
4713 {
4714 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4715 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4716 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4717 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
2396cfb9 4718 macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
d6bc6245
TS
4719 tempreg, tempreg, 16);
4720 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4721 tempreg, tempreg, (int) BFD_RELOC_HI16_S);
2396cfb9 4722 macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
d6bc6245
TS
4723 tempreg, tempreg, 16);
4724 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4725 tempreg, tempreg, (int) BFD_RELOC_LO16);
4726 }
4727 }
4728 else
4729 {
4730 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4731 && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
4732 {
4733 frag_grow (20);
4d34fb5f 4734 macro_build ((char *) NULL, &icnt, &offset_expr, "addiu",
cdf6fd85 4735 "t,r,j", tempreg, GP, (int) BFD_RELOC_GPREL16);
d6bc6245
TS
4736 p = frag_var (rs_machine_dependent, 8, 0,
4737 RELAX_ENCODE (4, 8, 0, 4, 0,
4738 mips_opts.warn_about_macros),
956cd1d6 4739 offset_expr.X_add_symbol, 0, NULL);
d6bc6245
TS
4740 }
4741 macro_build_lui (p, &icnt, &offset_expr, tempreg);
4742 if (p != NULL)
4743 p += 4;
4d34fb5f 4744 macro_build (p, &icnt, &offset_expr, "addiu",
d6bc6245
TS
4745 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4746 }
252b5132
RH
4747 }
4748 else if (mips_pic == SVR4_PIC && ! mips_big_got)
4749 {
9117d219
NC
4750 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
4751
252b5132
RH
4752 /* If this is a reference to an external symbol, and there
4753 is no constant, we want
4754 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9117d219
NC
4755 or if tempreg is PIC_CALL_REG
4756 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
252b5132
RH
4757 For a local symbol, we want
4758 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4759 nop
4760 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4761
4762 If we have a small constant, and this is a reference to
4763 an external symbol, we want
4764 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4765 nop
4766 addiu $tempreg,$tempreg,<constant>
4767 For a local symbol, we want the same instruction
4768 sequence, but we output a BFD_RELOC_LO16 reloc on the
4769 addiu instruction.
4770
4771 If we have a large constant, and this is a reference to
4772 an external symbol, we want
4773 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4774 lui $at,<hiconstant>
4775 addiu $at,$at,<loconstant>
4776 addu $tempreg,$tempreg,$at
4777 For a local symbol, we want the same instruction
4778 sequence, but we output a BFD_RELOC_LO16 reloc on the
4779 addiu instruction. */
4780 expr1.X_add_number = offset_expr.X_add_number;
4781 offset_expr.X_add_number = 0;
4782 frag_grow (32);
9117d219
NC
4783 if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4784 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
4d34fb5f
TS
4785 macro_build ((char *) NULL, &icnt, &offset_expr,
4786 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
9117d219 4787 "t,o(b)", tempreg, lw_reloc_type, GP);
252b5132
RH
4788 if (expr1.X_add_number == 0)
4789 {
4790 int off;
4791
4792 if (breg == 0)
4793 off = 0;
4794 else
4795 {
4796 /* We're going to put in an addu instruction using
4797 tempreg, so we may as well insert the nop right
4798 now. */
4799 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4800 "nop", "");
4801 off = 4;
4802 }
4803 p = frag_var (rs_machine_dependent, 8 - off, 0,
4804 RELAX_ENCODE (0, 8 - off, -4 - off, 4 - off, 0,
4805 (breg == 0
4806 ? mips_opts.warn_about_macros
4807 : 0)),
c4e7957c 4808 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
4809 if (breg == 0)
4810 {
4811 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4812 p += 4;
4813 }
4814 macro_build (p, &icnt, &expr1,
ca4e0257 4815 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
4816 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4817 /* FIXME: If breg == 0, and the next instruction uses
4818 $tempreg, then if this variant case is used an extra
4819 nop will be generated. */
4820 }
4821 else if (expr1.X_add_number >= -0x8000
4822 && expr1.X_add_number < 0x8000)
4823 {
4824 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4825 "nop", "");
4826 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 4827 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132 4828 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
c4e7957c
TS
4829 frag_var (rs_machine_dependent, 0, 0,
4830 RELAX_ENCODE (0, 0, -12, -4, 0, 0),
4831 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
4832 }
4833 else
4834 {
4835 int off1;
4836
4837 /* If we are going to add in a base register, and the
4838 target register and the base register are the same,
4839 then we are using AT as a temporary register. Since
4840 we want to load the constant into AT, we add our
4841 current AT (from the global offset table) and the
4842 register into the register now, and pretend we were
4843 not using a base register. */
4844 if (breg != treg)
4845 off1 = 0;
4846 else
4847 {
4848 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4849 "nop", "");
4850 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 4851 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
4852 "d,v,t", treg, AT, breg);
4853 breg = 0;
4854 tempreg = treg;
4855 off1 = -8;
4856 }
4857
4858 /* Set mips_optimize around the lui instruction to avoid
4859 inserting an unnecessary nop after the lw. */
4860 hold_mips_optimize = mips_optimize;
4861 mips_optimize = 2;
c4e7957c 4862 macro_build_lui (NULL, &icnt, &expr1, AT);
252b5132
RH
4863 mips_optimize = hold_mips_optimize;
4864
4865 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 4866 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
4867 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4868 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 4869 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132 4870 "d,v,t", tempreg, tempreg, AT);
c4e7957c
TS
4871 frag_var (rs_machine_dependent, 0, 0,
4872 RELAX_ENCODE (0, 0, -16 + off1, -8, 0, 0),
4873 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
4874 used_at = 1;
4875 }
4876 }
4877 else if (mips_pic == SVR4_PIC)
4878 {
4879 int gpdel;
9117d219
NC
4880 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
4881 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
252b5132
RH
4882
4883 /* This is the large GOT case. If this is a reference to an
4884 external symbol, and there is no constant, we want
4885 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
4886 addu $tempreg,$tempreg,$gp
4887 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
9117d219
NC
4888 or if tempreg is PIC_CALL_REG
4889 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
4890 addu $tempreg,$tempreg,$gp
4891 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
252b5132
RH
4892 For a local symbol, we want
4893 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4894 nop
4895 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4896
4897 If we have a small constant, and this is a reference to
4898 an external symbol, we want
4899 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
4900 addu $tempreg,$tempreg,$gp
4901 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4902 nop
4903 addiu $tempreg,$tempreg,<constant>
4904 For a local symbol, we want
4905 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4906 nop
4907 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
4908
4909 If we have a large constant, and this is a reference to
4910 an external symbol, we want
4911 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
4912 addu $tempreg,$tempreg,$gp
4913 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4914 lui $at,<hiconstant>
4915 addiu $at,$at,<loconstant>
4916 addu $tempreg,$tempreg,$at
4917 For a local symbol, we want
4918 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4919 lui $at,<hiconstant>
4920 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
4921 addu $tempreg,$tempreg,$at
4922 */
4923 expr1.X_add_number = offset_expr.X_add_number;
4924 offset_expr.X_add_number = 0;
4925 frag_grow (52);
4926 if (reg_needs_delay (GP))
4927 gpdel = 4;
4928 else
4929 gpdel = 0;
9117d219
NC
4930 if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4931 {
4932 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
4933 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
4934 }
252b5132 4935 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
9117d219 4936 tempreg, lui_reloc_type);
252b5132 4937 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 4938 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
4939 "d,v,t", tempreg, tempreg, GP);
4940 macro_build ((char *) NULL, &icnt, &offset_expr,
4d34fb5f 4941 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
9117d219 4942 "t,o(b)", tempreg, lw_reloc_type, tempreg);
252b5132
RH
4943 if (expr1.X_add_number == 0)
4944 {
4945 int off;
4946
4947 if (breg == 0)
4948 off = 0;
4949 else
4950 {
4951 /* We're going to put in an addu instruction using
4952 tempreg, so we may as well insert the nop right
4953 now. */
4954 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4955 "nop", "");
4956 off = 4;
4957 }
4958
4959 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4960 RELAX_ENCODE (12 + off, 12 + gpdel, gpdel,
4961 8 + gpdel, 0,
4962 (breg == 0
4963 ? mips_opts.warn_about_macros
4964 : 0)),
c4e7957c 4965 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
4966 }
4967 else if (expr1.X_add_number >= -0x8000
4968 && expr1.X_add_number < 0x8000)
4969 {
4970 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4971 "nop", "");
4972 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 4973 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
4974 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4975
4976 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4977 RELAX_ENCODE (20, 12 + gpdel, gpdel, 8 + gpdel, 0,
4978 (breg == 0
4979 ? mips_opts.warn_about_macros
4980 : 0)),
c4e7957c 4981 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
4982 }
4983 else
4984 {
4985 int adj, dreg;
4986
4987 /* If we are going to add in a base register, and the
4988 target register and the base register are the same,
4989 then we are using AT as a temporary register. Since
4990 we want to load the constant into AT, we add our
4991 current AT (from the global offset table) and the
4992 register into the register now, and pretend we were
4993 not using a base register. */
4994 if (breg != treg)
4995 {
4996 adj = 0;
4997 dreg = tempreg;
4998 }
4999 else
5000 {
5001 assert (tempreg == AT);
5002 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5003 "nop", "");
5004 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5005 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5006 "d,v,t", treg, AT, breg);
5007 dreg = treg;
5008 adj = 8;
5009 }
5010
5011 /* Set mips_optimize around the lui instruction to avoid
5012 inserting an unnecessary nop after the lw. */
5013 hold_mips_optimize = mips_optimize;
5014 mips_optimize = 2;
c4e7957c 5015 macro_build_lui (NULL, &icnt, &expr1, AT);
252b5132
RH
5016 mips_optimize = hold_mips_optimize;
5017
5018 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 5019 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
5020 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5021 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5022 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5023 "d,v,t", dreg, dreg, AT);
5024
5025 p = frag_var (rs_machine_dependent, 16 + gpdel + adj, 0,
5026 RELAX_ENCODE (24 + adj, 16 + gpdel + adj, gpdel,
5027 8 + gpdel, 0,
5028 (breg == 0
5029 ? mips_opts.warn_about_macros
5030 : 0)),
c4e7957c 5031 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5032
5033 used_at = 1;
5034 }
5035
5036 if (gpdel > 0)
5037 {
5038 /* This is needed because this instruction uses $gp, but
5039 the first instruction on the main stream does not. */
5040 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5041 p += 4;
5042 }
5043 macro_build (p, &icnt, &offset_expr,
4d34fb5f 5044 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
5045 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16, GP);
5046 p += 4;
5047 if (expr1.X_add_number >= -0x8000
5048 && expr1.X_add_number < 0x8000)
5049 {
5050 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5051 p += 4;
5052 macro_build (p, &icnt, &expr1,
ca4e0257 5053 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
5054 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5055 /* FIXME: If add_number is 0, and there was no base
5056 register, the external symbol case ended with a load,
5057 so if the symbol turns out to not be external, and
5058 the next instruction uses tempreg, an unnecessary nop
5059 will be inserted. */
5060 }
5061 else
5062 {
5063 if (breg == treg)
5064 {
5065 /* We must add in the base register now, as in the
5066 external symbol case. */
5067 assert (tempreg == AT);
5068 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5069 p += 4;
5070 macro_build (p, &icnt, (expressionS *) NULL,
ca4e0257 5071 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5072 "d,v,t", treg, AT, breg);
5073 p += 4;
5074 tempreg = treg;
5075 /* We set breg to 0 because we have arranged to add
5076 it in in both cases. */
5077 breg = 0;
5078 }
5079
5080 macro_build_lui (p, &icnt, &expr1, AT);
5081 p += 4;
5082 macro_build (p, &icnt, &expr1,
ca4e0257 5083 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
5084 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5085 p += 4;
5086 macro_build (p, &icnt, (expressionS *) NULL,
ca4e0257 5087 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5088 "d,v,t", tempreg, tempreg, AT);
5089 p += 4;
5090 }
5091 }
5092 else if (mips_pic == EMBEDDED_PIC)
5093 {
5094 /* We use
cdf6fd85 5095 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
5096 */
5097 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 5098 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
cdf6fd85 5099 "t,r,j", tempreg, GP, (int) BFD_RELOC_GPREL16);
252b5132
RH
5100 }
5101 else
5102 abort ();
5103
5104 if (breg != 0)
4d34fb5f
TS
5105 {
5106 char *s;
5107
5108 if (mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
5109 s = (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu";
5110 else
5111 s = HAVE_64BIT_ADDRESSES ? "daddu" : "addu";
5112
5113 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s,
5114 "d,v,t", treg, tempreg, breg);
5115 }
252b5132
RH
5116
5117 if (! used_at)
5118 return;
5119
5120 break;
5121
5122 case M_J_A:
5123 /* The j instruction may not be used in PIC code, since it
5124 requires an absolute address. We convert it to a b
5125 instruction. */
5126 if (mips_pic == NO_PIC)
5127 macro_build ((char *) NULL, &icnt, &offset_expr, "j", "a");
5128 else
5129 macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
5130 return;
5131
5132 /* The jal instructions must be handled as macros because when
5133 generating PIC code they expand to multi-instruction
5134 sequences. Normally they are simple instructions. */
5135 case M_JAL_1:
5136 dreg = RA;
5137 /* Fall through. */
5138 case M_JAL_2:
5139 if (mips_pic == NO_PIC
5140 || mips_pic == EMBEDDED_PIC)
5141 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5142 "d,s", dreg, sreg);
5143 else if (mips_pic == SVR4_PIC)
5144 {
5145 if (sreg != PIC_CALL_REG)
5146 as_warn (_("MIPS PIC call to register other than $25"));
bdaaa2e1 5147
252b5132
RH
5148 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5149 "d,s", dreg, sreg);
6478892d 5150 if (! HAVE_NEWABI)
252b5132 5151 {
6478892d
TS
5152 if (mips_cprestore_offset < 0)
5153 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5154 else
5155 {
7a621144
DJ
5156 if (! mips_frame_reg_valid)
5157 {
5158 as_warn (_("No .frame pseudo-op used in PIC code"));
5159 /* Quiet this warning. */
5160 mips_frame_reg_valid = 1;
5161 }
5162 if (! mips_cprestore_valid)
5163 {
5164 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5165 /* Quiet this warning. */
5166 mips_cprestore_valid = 1;
5167 }
6478892d
TS
5168 expr1.X_add_number = mips_cprestore_offset;
5169 macro_build ((char *) NULL, &icnt, &expr1,
5170 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
5171 GP, (int) BFD_RELOC_LO16, mips_frame_reg);
5172 }
252b5132
RH
5173 }
5174 }
5175 else
5176 abort ();
5177
5178 return;
5179
5180 case M_JAL_A:
5181 if (mips_pic == NO_PIC)
5182 macro_build ((char *) NULL, &icnt, &offset_expr, "jal", "a");
5183 else if (mips_pic == SVR4_PIC)
5184 {
5185 /* If this is a reference to an external symbol, and we are
5186 using a small GOT, we want
5187 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5188 nop
5189 jalr $25
5190 nop
5191 lw $gp,cprestore($sp)
5192 The cprestore value is set using the .cprestore
5193 pseudo-op. If we are using a big GOT, we want
5194 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5195 addu $25,$25,$gp
5196 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
5197 nop
5198 jalr $25
5199 nop
5200 lw $gp,cprestore($sp)
5201 If the symbol is not external, we want
5202 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5203 nop
5204 addiu $25,$25,<sym> (BFD_RELOC_LO16)
5205 jalr $25
5206 nop
5207 lw $gp,cprestore($sp) */
5208 frag_grow (40);
5209 if (! mips_big_got)
5210 {
5211 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 5212 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
5213 "t,o(b)", PIC_CALL_REG,
5214 (int) BFD_RELOC_MIPS_CALL16, GP);
5215 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5216 "nop", "");
5217 p = frag_var (rs_machine_dependent, 4, 0,
5218 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
c4e7957c 5219 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5220 }
5221 else
5222 {
5223 int gpdel;
5224
5225 if (reg_needs_delay (GP))
5226 gpdel = 4;
5227 else
5228 gpdel = 0;
5229 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5230 PIC_CALL_REG, (int) BFD_RELOC_MIPS_CALL_HI16);
5231 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5232 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5233 "d,v,t", PIC_CALL_REG, PIC_CALL_REG, GP);
5234 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 5235 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
5236 "t,o(b)", PIC_CALL_REG,
5237 (int) BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
5238 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5239 "nop", "");
5240 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5241 RELAX_ENCODE (16, 12 + gpdel, gpdel, 8 + gpdel,
5242 0, 0),
c4e7957c 5243 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5244 if (gpdel > 0)
5245 {
5246 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5247 p += 4;
5248 }
5249 macro_build (p, &icnt, &offset_expr,
ca4e0257 5250 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
5251 "t,o(b)", PIC_CALL_REG,
5252 (int) BFD_RELOC_MIPS_GOT16, GP);
5253 p += 4;
5254 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5255 p += 4;
bdaaa2e1 5256 }
252b5132 5257 macro_build (p, &icnt, &offset_expr,
ca4e0257 5258 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
5259 "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
5260 (int) BFD_RELOC_LO16);
5261 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5262 "jalr", "s", PIC_CALL_REG);
6478892d 5263 if (! HAVE_NEWABI)
252b5132 5264 {
6478892d
TS
5265 if (mips_cprestore_offset < 0)
5266 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5267 else
5268 {
7a621144
DJ
5269 if (! mips_frame_reg_valid)
5270 {
5271 as_warn (_("No .frame pseudo-op used in PIC code"));
5272 /* Quiet this warning. */
5273 mips_frame_reg_valid = 1;
5274 }
5275 if (! mips_cprestore_valid)
5276 {
5277 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5278 /* Quiet this warning. */
5279 mips_cprestore_valid = 1;
5280 }
6478892d
TS
5281 if (mips_opts.noreorder)
5282 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
252b5132 5283 "nop", "");
6478892d
TS
5284 expr1.X_add_number = mips_cprestore_offset;
5285 macro_build ((char *) NULL, &icnt, &expr1,
5286 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
5287 GP, (int) BFD_RELOC_LO16, mips_frame_reg);
5288 }
252b5132
RH
5289 }
5290 }
5291 else if (mips_pic == EMBEDDED_PIC)
5292 {
5293 macro_build ((char *) NULL, &icnt, &offset_expr, "bal", "p");
5294 /* The linker may expand the call to a longer sequence which
5295 uses $at, so we must break rather than return. */
5296 break;
5297 }
5298 else
5299 abort ();
5300
5301 return;
5302
5303 case M_LB_AB:
5304 s = "lb";
5305 goto ld;
5306 case M_LBU_AB:
5307 s = "lbu";
5308 goto ld;
5309 case M_LH_AB:
5310 s = "lh";
5311 goto ld;
5312 case M_LHU_AB:
5313 s = "lhu";
5314 goto ld;
5315 case M_LW_AB:
5316 s = "lw";
5317 goto ld;
5318 case M_LWC0_AB:
5319 s = "lwc0";
bdaaa2e1 5320 /* Itbl support may require additional care here. */
252b5132
RH
5321 coproc = 1;
5322 goto ld;
5323 case M_LWC1_AB:
5324 s = "lwc1";
bdaaa2e1 5325 /* Itbl support may require additional care here. */
252b5132
RH
5326 coproc = 1;
5327 goto ld;
5328 case M_LWC2_AB:
5329 s = "lwc2";
bdaaa2e1 5330 /* Itbl support may require additional care here. */
252b5132
RH
5331 coproc = 1;
5332 goto ld;
5333 case M_LWC3_AB:
5334 s = "lwc3";
bdaaa2e1 5335 /* Itbl support may require additional care here. */
252b5132
RH
5336 coproc = 1;
5337 goto ld;
5338 case M_LWL_AB:
5339 s = "lwl";
5340 lr = 1;
5341 goto ld;
5342 case M_LWR_AB:
5343 s = "lwr";
5344 lr = 1;
5345 goto ld;
5346 case M_LDC1_AB:
ec68c924 5347 if (mips_arch == CPU_R4650)
252b5132
RH
5348 {
5349 as_bad (_("opcode not supported on this processor"));
5350 return;
5351 }
5352 s = "ldc1";
bdaaa2e1 5353 /* Itbl support may require additional care here. */
252b5132
RH
5354 coproc = 1;
5355 goto ld;
5356 case M_LDC2_AB:
5357 s = "ldc2";
bdaaa2e1 5358 /* Itbl support may require additional care here. */
252b5132
RH
5359 coproc = 1;
5360 goto ld;
5361 case M_LDC3_AB:
5362 s = "ldc3";
bdaaa2e1 5363 /* Itbl support may require additional care here. */
252b5132
RH
5364 coproc = 1;
5365 goto ld;
5366 case M_LDL_AB:
5367 s = "ldl";
5368 lr = 1;
5369 goto ld;
5370 case M_LDR_AB:
5371 s = "ldr";
5372 lr = 1;
5373 goto ld;
5374 case M_LL_AB:
5375 s = "ll";
5376 goto ld;
5377 case M_LLD_AB:
5378 s = "lld";
5379 goto ld;
5380 case M_LWU_AB:
5381 s = "lwu";
5382 ld:
5383 if (breg == treg || coproc || lr)
5384 {
5385 tempreg = AT;
5386 used_at = 1;
5387 }
5388 else
5389 {
5390 tempreg = treg;
5391 used_at = 0;
5392 }
5393 goto ld_st;
5394 case M_SB_AB:
5395 s = "sb";
5396 goto st;
5397 case M_SH_AB:
5398 s = "sh";
5399 goto st;
5400 case M_SW_AB:
5401 s = "sw";
5402 goto st;
5403 case M_SWC0_AB:
5404 s = "swc0";
bdaaa2e1 5405 /* Itbl support may require additional care here. */
252b5132
RH
5406 coproc = 1;
5407 goto st;
5408 case M_SWC1_AB:
5409 s = "swc1";
bdaaa2e1 5410 /* Itbl support may require additional care here. */
252b5132
RH
5411 coproc = 1;
5412 goto st;
5413 case M_SWC2_AB:
5414 s = "swc2";
bdaaa2e1 5415 /* Itbl support may require additional care here. */
252b5132
RH
5416 coproc = 1;
5417 goto st;
5418 case M_SWC3_AB:
5419 s = "swc3";
bdaaa2e1 5420 /* Itbl support may require additional care here. */
252b5132
RH
5421 coproc = 1;
5422 goto st;
5423 case M_SWL_AB:
5424 s = "swl";
5425 goto st;
5426 case M_SWR_AB:
5427 s = "swr";
5428 goto st;
5429 case M_SC_AB:
5430 s = "sc";
5431 goto st;
5432 case M_SCD_AB:
5433 s = "scd";
5434 goto st;
5435 case M_SDC1_AB:
ec68c924 5436 if (mips_arch == CPU_R4650)
252b5132
RH
5437 {
5438 as_bad (_("opcode not supported on this processor"));
5439 return;
5440 }
5441 s = "sdc1";
5442 coproc = 1;
bdaaa2e1 5443 /* Itbl support may require additional care here. */
252b5132
RH
5444 goto st;
5445 case M_SDC2_AB:
5446 s = "sdc2";
bdaaa2e1 5447 /* Itbl support may require additional care here. */
252b5132
RH
5448 coproc = 1;
5449 goto st;
5450 case M_SDC3_AB:
5451 s = "sdc3";
bdaaa2e1 5452 /* Itbl support may require additional care here. */
252b5132
RH
5453 coproc = 1;
5454 goto st;
5455 case M_SDL_AB:
5456 s = "sdl";
5457 goto st;
5458 case M_SDR_AB:
5459 s = "sdr";
5460 st:
5461 tempreg = AT;
5462 used_at = 1;
5463 ld_st:
bdaaa2e1 5464 /* Itbl support may require additional care here. */
252b5132
RH
5465 if (mask == M_LWC1_AB
5466 || mask == M_SWC1_AB
5467 || mask == M_LDC1_AB
5468 || mask == M_SDC1_AB
5469 || mask == M_L_DAB
5470 || mask == M_S_DAB)
5471 fmt = "T,o(b)";
5472 else if (coproc)
5473 fmt = "E,o(b)";
5474 else
5475 fmt = "t,o(b)";
5476
afdbd6d0
CD
5477 /* For embedded PIC, we allow loads where the offset is calculated
5478 by subtracting a symbol in the current segment from an unknown
5479 symbol, relative to a base register, e.g.:
5480 <op> $treg, <sym>-<localsym>($breg)
5481 This is used by the compiler for switch statements. */
76b3015f 5482 if (mips_pic == EMBEDDED_PIC
afdbd6d0
CD
5483 && offset_expr.X_op == O_subtract
5484 && (symbol_constant_p (offset_expr.X_op_symbol)
5485 ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
5486 : (symbol_equated_p (offset_expr.X_op_symbol)
5487 && (S_GET_SEGMENT
5488 (symbol_get_value_expression (offset_expr.X_op_symbol)
5489 ->X_add_symbol)
5490 == now_seg)))
5491 && breg != 0
5492 && (offset_expr.X_add_number == 0
5493 || OUTPUT_FLAVOR == bfd_target_elf_flavour))
5494 {
5495 /* For this case, we output the instructions:
5496 lui $tempreg,<sym> (BFD_RELOC_PCREL_HI16_S)
5497 addiu $tempreg,$tempreg,$breg
5498 <op> $treg,<sym>($tempreg) (BFD_RELOC_PCREL_LO16)
5499 If the relocation would fit entirely in 16 bits, it would be
5500 nice to emit:
5501 <op> $treg,<sym>($breg) (BFD_RELOC_PCREL_LO16)
5502 instead, but that seems quite difficult. */
5503 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5504 tempreg, (int) BFD_RELOC_PCREL_HI16_S);
5505 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5506 ((bfd_arch_bits_per_address (stdoutput) == 32
5507 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
5508 ? "addu" : "daddu"),
5509 "d,v,t", tempreg, tempreg, breg);
5510 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt, treg,
5511 (int) BFD_RELOC_PCREL_LO16, tempreg);
5512 if (! used_at)
5513 return;
5514 break;
5515 }
5516
252b5132
RH
5517 if (offset_expr.X_op != O_constant
5518 && offset_expr.X_op != O_symbol)
5519 {
5520 as_bad (_("expression too complex"));
5521 offset_expr.X_op = O_constant;
5522 }
5523
5524 /* A constant expression in PIC code can be handled just as it
5525 is in non PIC code. */
5526 if (mips_pic == NO_PIC
5527 || offset_expr.X_op == O_constant)
5528 {
5529 /* If this is a reference to a GP relative symbol, and there
5530 is no base register, we want
cdf6fd85 5531 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
5532 Otherwise, if there is no base register, we want
5533 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5534 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5535 If we have a constant, we need two instructions anyhow,
5536 so we always use the latter form.
5537
5538 If we have a base register, and this is a reference to a
5539 GP relative symbol, we want
5540 addu $tempreg,$breg,$gp
cdf6fd85 5541 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
5542 Otherwise we want
5543 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5544 addu $tempreg,$tempreg,$breg
5545 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245 5546 With a constant we always use the latter case.
76b3015f 5547
d6bc6245
TS
5548 With 64bit address space and no base register and $at usable,
5549 we want
5550 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5551 lui $at,<sym> (BFD_RELOC_HI16_S)
5552 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5553 dsll32 $tempreg,0
5554 daddu $tempreg,$at
5555 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5556 If we have a base register, we want
5557 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5558 lui $at,<sym> (BFD_RELOC_HI16_S)
5559 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5560 daddu $at,$breg
5561 dsll32 $tempreg,0
5562 daddu $tempreg,$at
5563 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5564
5565 Without $at we can't generate the optimal path for superscalar
5566 processors here since this would require two temporary registers.
5567 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5568 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5569 dsll $tempreg,16
5570 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5571 dsll $tempreg,16
5572 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5573 If we have a base register, we want
5574 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5575 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5576 dsll $tempreg,16
5577 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5578 dsll $tempreg,16
5579 daddu $tempreg,$tempreg,$breg
5580 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6373ee54
CD
5581
5582 If we have 64-bit addresses, as an optimization, for
5583 addresses which are 32-bit constants (e.g. kseg0/kseg1
5584 addresses) we fall back to the 32-bit address generation
5585 mechanism since it is more efficient. This code should
5586 probably attempt to generate 64-bit constants more
5587 efficiently in general.
d6bc6245 5588 */
6373ee54
CD
5589 if (HAVE_64BIT_ADDRESSES
5590 && !(offset_expr.X_op == O_constant
5591 && IS_SEXT_32BIT_NUM (offset_expr.X_add_number)))
d6bc6245
TS
5592 {
5593 p = NULL;
5594
5595 /* We don't do GP optimization for now because RELAX_ENCODE can't
5596 hold the data for such large chunks. */
5597
5598 if (used_at == 0)
5599 {
5600 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5601 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5602 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5603 AT, (int) BFD_RELOC_HI16_S);
5604 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5605 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5606 if (breg != 0)
2396cfb9
TS
5607 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5608 "d,v,t", AT, AT, breg);
5609 macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
5610 "d,w,<", tempreg, tempreg, 0);
5611 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5612 "d,v,t", tempreg, tempreg, AT);
d6bc6245
TS
5613 macro_build (p, &icnt, &offset_expr, s,
5614 fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5615 used_at = 1;
5616 }
5617 else
5618 {
5619 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5620 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5621 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5622 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
2396cfb9
TS
5623 macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5624 "d,w,<", tempreg, tempreg, 16);
d6bc6245
TS
5625 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5626 tempreg, tempreg, (int) BFD_RELOC_HI16_S);
2396cfb9
TS
5627 macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5628 "d,w,<", tempreg, tempreg, 16);
d6bc6245 5629 if (breg != 0)
2396cfb9
TS
5630 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5631 "d,v,t", tempreg, tempreg, breg);
d6bc6245
TS
5632 macro_build (p, &icnt, &offset_expr, s,
5633 fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5634 }
5635
5636 return;
5637 }
76b3015f 5638
252b5132
RH
5639 if (breg == 0)
5640 {
e7d556df 5641 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
252b5132
RH
5642 || nopic_need_relax (offset_expr.X_add_symbol, 1))
5643 p = NULL;
5644 else
5645 {
5646 frag_grow (20);
5647 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
cdf6fd85 5648 treg, (int) BFD_RELOC_GPREL16, GP);
252b5132
RH
5649 p = frag_var (rs_machine_dependent, 8, 0,
5650 RELAX_ENCODE (4, 8, 0, 4, 0,
5651 (mips_opts.warn_about_macros
5652 || (used_at
5653 && mips_opts.noat))),
956cd1d6 5654 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5655 used_at = 0;
5656 }
5657 macro_build_lui (p, &icnt, &offset_expr, tempreg);
5658 if (p != NULL)
5659 p += 4;
5660 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5661 (int) BFD_RELOC_LO16, tempreg);
5662 }
5663 else
5664 {
e7d556df 5665 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
252b5132
RH
5666 || nopic_need_relax (offset_expr.X_add_symbol, 1))
5667 p = NULL;
5668 else
5669 {
5670 frag_grow (28);
5671 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5672 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5673 "d,v,t", tempreg, breg, GP);
5674 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
cdf6fd85 5675 treg, (int) BFD_RELOC_GPREL16, tempreg);
252b5132
RH
5676 p = frag_var (rs_machine_dependent, 12, 0,
5677 RELAX_ENCODE (8, 12, 0, 8, 0, 0),
956cd1d6 5678 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5679 }
5680 macro_build_lui (p, &icnt, &offset_expr, tempreg);
5681 if (p != NULL)
5682 p += 4;
5683 macro_build (p, &icnt, (expressionS *) NULL,
ca4e0257 5684 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5685 "d,v,t", tempreg, tempreg, breg);
5686 if (p != NULL)
5687 p += 4;
5688 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5689 (int) BFD_RELOC_LO16, tempreg);
5690 }
5691 }
5692 else if (mips_pic == SVR4_PIC && ! mips_big_got)
5693 {
5694 /* If this is a reference to an external symbol, we want
5695 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5696 nop
5697 <op> $treg,0($tempreg)
5698 Otherwise we want
5699 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5700 nop
5701 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5702 <op> $treg,0($tempreg)
5703 If there is a base register, we add it to $tempreg before
5704 the <op>. If there is a constant, we stick it in the
5705 <op> instruction. We don't handle constants larger than
5706 16 bits, because we have no way to load the upper 16 bits
5707 (actually, we could handle them for the subset of cases
5708 in which we are not using $at). */
5709 assert (offset_expr.X_op == O_symbol);
5710 expr1.X_add_number = offset_expr.X_add_number;
5711 offset_expr.X_add_number = 0;
5712 if (expr1.X_add_number < -0x8000
5713 || expr1.X_add_number >= 0x8000)
5714 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5715 frag_grow (20);
5716 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 5717 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
5718 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16, GP);
5719 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
bdaaa2e1 5720 p = frag_var (rs_machine_dependent, 4, 0,
252b5132 5721 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
c4e7957c 5722 offset_expr.X_add_symbol, 0, NULL);
252b5132 5723 macro_build (p, &icnt, &offset_expr,
ca4e0257 5724 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
5725 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5726 if (breg != 0)
5727 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5728 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5729 "d,v,t", tempreg, tempreg, breg);
5730 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5731 (int) BFD_RELOC_LO16, tempreg);
5732 }
5733 else if (mips_pic == SVR4_PIC)
5734 {
5735 int gpdel;
5736
5737 /* If this is a reference to an external symbol, we want
5738 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5739 addu $tempreg,$tempreg,$gp
5740 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5741 <op> $treg,0($tempreg)
5742 Otherwise we want
5743 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5744 nop
5745 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5746 <op> $treg,0($tempreg)
5747 If there is a base register, we add it to $tempreg before
5748 the <op>. If there is a constant, we stick it in the
5749 <op> instruction. We don't handle constants larger than
5750 16 bits, because we have no way to load the upper 16 bits
5751 (actually, we could handle them for the subset of cases
5752 in which we are not using $at). */
5753 assert (offset_expr.X_op == O_symbol);
5754 expr1.X_add_number = offset_expr.X_add_number;
5755 offset_expr.X_add_number = 0;
5756 if (expr1.X_add_number < -0x8000
5757 || expr1.X_add_number >= 0x8000)
5758 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5759 if (reg_needs_delay (GP))
5760 gpdel = 4;
5761 else
5762 gpdel = 0;
5763 frag_grow (36);
5764 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5765 tempreg, (int) BFD_RELOC_MIPS_GOT_HI16);
5766 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5767 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5768 "d,v,t", tempreg, tempreg, GP);
5769 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 5770 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
5771 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_LO16,
5772 tempreg);
5773 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5774 RELAX_ENCODE (12, 12 + gpdel, gpdel, 8 + gpdel, 0, 0),
c4e7957c 5775 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5776 if (gpdel > 0)
5777 {
5778 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5779 p += 4;
5780 }
5781 macro_build (p, &icnt, &offset_expr,
ca4e0257 5782 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
5783 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16, GP);
5784 p += 4;
5785 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5786 p += 4;
5787 macro_build (p, &icnt, &offset_expr,
ca4e0257 5788 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
5789 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5790 if (breg != 0)
5791 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5792 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5793 "d,v,t", tempreg, tempreg, breg);
5794 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5795 (int) BFD_RELOC_LO16, tempreg);
5796 }
5797 else if (mips_pic == EMBEDDED_PIC)
5798 {
5799 /* If there is no base register, we want
cdf6fd85 5800 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
5801 If there is a base register, we want
5802 addu $tempreg,$breg,$gp
cdf6fd85 5803 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
5804 */
5805 assert (offset_expr.X_op == O_symbol);
5806 if (breg == 0)
5807 {
5808 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
cdf6fd85 5809 treg, (int) BFD_RELOC_GPREL16, GP);
252b5132
RH
5810 used_at = 0;
5811 }
5812 else
5813 {
5814 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5815 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5816 "d,v,t", tempreg, breg, GP);
5817 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
cdf6fd85 5818 treg, (int) BFD_RELOC_GPREL16, tempreg);
252b5132
RH
5819 }
5820 }
5821 else
5822 abort ();
5823
5824 if (! used_at)
5825 return;
5826
5827 break;
5828
5829 case M_LI:
5830 case M_LI_S:
5831 load_register (&icnt, treg, &imm_expr, 0);
5832 return;
5833
5834 case M_DLI:
5835 load_register (&icnt, treg, &imm_expr, 1);
5836 return;
5837
5838 case M_LI_SS:
5839 if (imm_expr.X_op == O_constant)
5840 {
5841 load_register (&icnt, AT, &imm_expr, 0);
5842 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5843 "mtc1", "t,G", AT, treg);
5844 break;
5845 }
5846 else
5847 {
5848 assert (offset_expr.X_op == O_symbol
5849 && strcmp (segment_name (S_GET_SEGMENT
5850 (offset_expr.X_add_symbol)),
5851 ".lit4") == 0
5852 && offset_expr.X_add_number == 0);
5853 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
5854 treg, (int) BFD_RELOC_MIPS_LITERAL, GP);
5855 return;
5856 }
5857
5858 case M_LI_D:
ca4e0257
RS
5859 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
5860 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
5861 order 32 bits of the value and the low order 32 bits are either
5862 zero or in OFFSET_EXPR. */
252b5132
RH
5863 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
5864 {
ca4e0257 5865 if (HAVE_64BIT_GPRS)
252b5132
RH
5866 load_register (&icnt, treg, &imm_expr, 1);
5867 else
5868 {
5869 int hreg, lreg;
5870
5871 if (target_big_endian)
5872 {
5873 hreg = treg;
5874 lreg = treg + 1;
5875 }
5876 else
5877 {
5878 hreg = treg + 1;
5879 lreg = treg;
5880 }
5881
5882 if (hreg <= 31)
5883 load_register (&icnt, hreg, &imm_expr, 0);
5884 if (lreg <= 31)
5885 {
5886 if (offset_expr.X_op == O_absent)
ea1fb5dc 5887 move_register (&icnt, lreg, 0);
252b5132
RH
5888 else
5889 {
5890 assert (offset_expr.X_op == O_constant);
5891 load_register (&icnt, lreg, &offset_expr, 0);
5892 }
5893 }
5894 }
5895 return;
5896 }
5897
5898 /* We know that sym is in the .rdata section. First we get the
5899 upper 16 bits of the address. */
5900 if (mips_pic == NO_PIC)
5901 {
956cd1d6 5902 macro_build_lui (NULL, &icnt, &offset_expr, AT);
252b5132
RH
5903 }
5904 else if (mips_pic == SVR4_PIC)
5905 {
5906 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 5907 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
5908 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
5909 }
5910 else if (mips_pic == EMBEDDED_PIC)
5911 {
5912 /* For embedded PIC we pick up the entire address off $gp in
5913 a single instruction. */
5914 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 5915 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
cdf6fd85 5916 "t,r,j", AT, GP, (int) BFD_RELOC_GPREL16);
252b5132
RH
5917 offset_expr.X_op = O_constant;
5918 offset_expr.X_add_number = 0;
5919 }
5920 else
5921 abort ();
bdaaa2e1 5922
252b5132 5923 /* Now we load the register(s). */
ca4e0257 5924 if (HAVE_64BIT_GPRS)
252b5132
RH
5925 macro_build ((char *) NULL, &icnt, &offset_expr, "ld", "t,o(b)",
5926 treg, (int) BFD_RELOC_LO16, AT);
5927 else
5928 {
5929 macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
5930 treg, (int) BFD_RELOC_LO16, AT);
5931 if (treg != 31)
5932 {
5933 /* FIXME: How in the world do we deal with the possible
5934 overflow here? */
5935 offset_expr.X_add_number += 4;
5936 macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
5937 treg + 1, (int) BFD_RELOC_LO16, AT);
5938 }
5939 }
5940
5941 /* To avoid confusion in tc_gen_reloc, we must ensure that this
5942 does not become a variant frag. */
5943 frag_wane (frag_now);
5944 frag_new (0);
5945
5946 break;
5947
5948 case M_LI_DD:
ca4e0257
RS
5949 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
5950 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
5951 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
5952 the value and the low order 32 bits are either zero or in
5953 OFFSET_EXPR. */
252b5132
RH
5954 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
5955 {
ca4e0257
RS
5956 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_FPRS);
5957 if (HAVE_64BIT_FPRS)
5958 {
5959 assert (HAVE_64BIT_GPRS);
5960 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5961 "dmtc1", "t,S", AT, treg);
5962 }
252b5132
RH
5963 else
5964 {
5965 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5966 "mtc1", "t,G", AT, treg + 1);
5967 if (offset_expr.X_op == O_absent)
5968 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5969 "mtc1", "t,G", 0, treg);
5970 else
5971 {
5972 assert (offset_expr.X_op == O_constant);
5973 load_register (&icnt, AT, &offset_expr, 0);
5974 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5975 "mtc1", "t,G", AT, treg);
5976 }
5977 }
5978 break;
5979 }
5980
5981 assert (offset_expr.X_op == O_symbol
5982 && offset_expr.X_add_number == 0);
5983 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
5984 if (strcmp (s, ".lit8") == 0)
5985 {
e7af610e 5986 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
5987 {
5988 macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
5989 "T,o(b)", treg, (int) BFD_RELOC_MIPS_LITERAL, GP);
5990 return;
5991 }
5992 breg = GP;
5993 r = BFD_RELOC_MIPS_LITERAL;
5994 goto dob;
5995 }
5996 else
5997 {
5998 assert (strcmp (s, RDATA_SECTION_NAME) == 0);
5999 if (mips_pic == SVR4_PIC)
6000 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 6001 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
6002 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
6003 else
6004 {
6005 /* FIXME: This won't work for a 64 bit address. */
956cd1d6 6006 macro_build_lui (NULL, &icnt, &offset_expr, AT);
252b5132 6007 }
bdaaa2e1 6008
e7af610e 6009 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
6010 {
6011 macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
6012 "T,o(b)", treg, (int) BFD_RELOC_LO16, AT);
6013
6014 /* To avoid confusion in tc_gen_reloc, we must ensure
6015 that this does not become a variant frag. */
6016 frag_wane (frag_now);
6017 frag_new (0);
6018
6019 break;
6020 }
6021 breg = AT;
6022 r = BFD_RELOC_LO16;
6023 goto dob;
6024 }
6025
6026 case M_L_DOB:
ec68c924 6027 if (mips_arch == CPU_R4650)
252b5132
RH
6028 {
6029 as_bad (_("opcode not supported on this processor"));
6030 return;
6031 }
6032 /* Even on a big endian machine $fn comes before $fn+1. We have
6033 to adjust when loading from memory. */
6034 r = BFD_RELOC_LO16;
6035 dob:
e7af610e 6036 assert (mips_opts.isa == ISA_MIPS1);
252b5132
RH
6037 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6038 target_big_endian ? treg + 1 : treg,
6039 (int) r, breg);
6040 /* FIXME: A possible overflow which I don't know how to deal
6041 with. */
6042 offset_expr.X_add_number += 4;
6043 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6044 target_big_endian ? treg : treg + 1,
6045 (int) r, breg);
6046
6047 /* To avoid confusion in tc_gen_reloc, we must ensure that this
6048 does not become a variant frag. */
6049 frag_wane (frag_now);
6050 frag_new (0);
6051
6052 if (breg != AT)
6053 return;
6054 break;
6055
6056 case M_L_DAB:
6057 /*
6058 * The MIPS assembler seems to check for X_add_number not
6059 * being double aligned and generating:
6060 * lui at,%hi(foo+1)
6061 * addu at,at,v1
6062 * addiu at,at,%lo(foo+1)
6063 * lwc1 f2,0(at)
6064 * lwc1 f3,4(at)
6065 * But, the resulting address is the same after relocation so why
6066 * generate the extra instruction?
6067 */
ec68c924 6068 if (mips_arch == CPU_R4650)
252b5132
RH
6069 {
6070 as_bad (_("opcode not supported on this processor"));
6071 return;
6072 }
bdaaa2e1 6073 /* Itbl support may require additional care here. */
252b5132 6074 coproc = 1;
e7af610e 6075 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
6076 {
6077 s = "ldc1";
6078 goto ld;
6079 }
6080
6081 s = "lwc1";
6082 fmt = "T,o(b)";
6083 goto ldd_std;
6084
6085 case M_S_DAB:
ec68c924 6086 if (mips_arch == CPU_R4650)
252b5132
RH
6087 {
6088 as_bad (_("opcode not supported on this processor"));
6089 return;
6090 }
6091
e7af610e 6092 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
6093 {
6094 s = "sdc1";
6095 goto st;
6096 }
6097
6098 s = "swc1";
6099 fmt = "T,o(b)";
bdaaa2e1 6100 /* Itbl support may require additional care here. */
252b5132
RH
6101 coproc = 1;
6102 goto ldd_std;
6103
6104 case M_LD_AB:
ca4e0257 6105 if (HAVE_64BIT_GPRS)
252b5132
RH
6106 {
6107 s = "ld";
6108 goto ld;
6109 }
6110
6111 s = "lw";
6112 fmt = "t,o(b)";
6113 goto ldd_std;
6114
6115 case M_SD_AB:
ca4e0257 6116 if (HAVE_64BIT_GPRS)
252b5132
RH
6117 {
6118 s = "sd";
6119 goto st;
6120 }
6121
6122 s = "sw";
6123 fmt = "t,o(b)";
6124
6125 ldd_std:
afdbd6d0
CD
6126 /* We do _not_ bother to allow embedded PIC (symbol-local_symbol)
6127 loads for the case of doing a pair of loads to simulate an 'ld'.
6128 This is not currently done by the compiler, and assembly coders
6129 writing embedded-pic code can cope. */
6130
252b5132
RH
6131 if (offset_expr.X_op != O_symbol
6132 && offset_expr.X_op != O_constant)
6133 {
6134 as_bad (_("expression too complex"));
6135 offset_expr.X_op = O_constant;
6136 }
6137
6138 /* Even on a big endian machine $fn comes before $fn+1. We have
6139 to adjust when loading from memory. We set coproc if we must
6140 load $fn+1 first. */
bdaaa2e1 6141 /* Itbl support may require additional care here. */
252b5132
RH
6142 if (! target_big_endian)
6143 coproc = 0;
6144
6145 if (mips_pic == NO_PIC
6146 || offset_expr.X_op == O_constant)
6147 {
6148 /* If this is a reference to a GP relative symbol, we want
cdf6fd85
TS
6149 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6150 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
6151 If we have a base register, we use this
6152 addu $at,$breg,$gp
cdf6fd85
TS
6153 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6154 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
6155 If this is not a GP relative symbol, we want
6156 lui $at,<sym> (BFD_RELOC_HI16_S)
6157 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6158 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6159 If there is a base register, we add it to $at after the
6160 lui instruction. If there is a constant, we always use
6161 the last case. */
e7d556df 6162 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
252b5132
RH
6163 || nopic_need_relax (offset_expr.X_add_symbol, 1))
6164 {
6165 p = NULL;
6166 used_at = 1;
6167 }
6168 else
6169 {
6170 int off;
6171
6172 if (breg == 0)
6173 {
6174 frag_grow (28);
6175 tempreg = GP;
6176 off = 0;
6177 used_at = 0;
6178 }
6179 else
6180 {
6181 frag_grow (36);
6182 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6183 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
6184 "d,v,t", AT, breg, GP);
6185 tempreg = AT;
6186 off = 4;
6187 used_at = 1;
6188 }
6189
beae10d5 6190 /* Itbl support may require additional care here. */
252b5132
RH
6191 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6192 coproc ? treg + 1 : treg,
cdf6fd85 6193 (int) BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6194 offset_expr.X_add_number += 4;
6195
6196 /* Set mips_optimize to 2 to avoid inserting an
6197 undesired nop. */
6198 hold_mips_optimize = mips_optimize;
6199 mips_optimize = 2;
beae10d5 6200 /* Itbl support may require additional care here. */
252b5132
RH
6201 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6202 coproc ? treg : treg + 1,
cdf6fd85 6203 (int) BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6204 mips_optimize = hold_mips_optimize;
6205
6206 p = frag_var (rs_machine_dependent, 12 + off, 0,
6207 RELAX_ENCODE (8 + off, 12 + off, 0, 4 + off, 1,
6208 used_at && mips_opts.noat),
956cd1d6 6209 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
6210
6211 /* We just generated two relocs. When tc_gen_reloc
6212 handles this case, it will skip the first reloc and
6213 handle the second. The second reloc already has an
6214 extra addend of 4, which we added above. We must
6215 subtract it out, and then subtract another 4 to make
6216 the first reloc come out right. The second reloc
6217 will come out right because we are going to add 4 to
6218 offset_expr when we build its instruction below.
6219
6220 If we have a symbol, then we don't want to include
6221 the offset, because it will wind up being included
6222 when we generate the reloc. */
6223
6224 if (offset_expr.X_op == O_constant)
6225 offset_expr.X_add_number -= 8;
6226 else
6227 {
6228 offset_expr.X_add_number = -4;
6229 offset_expr.X_op = O_constant;
6230 }
6231 }
6232 macro_build_lui (p, &icnt, &offset_expr, AT);
6233 if (p != NULL)
6234 p += 4;
6235 if (breg != 0)
6236 {
6237 macro_build (p, &icnt, (expressionS *) NULL,
ca4e0257 6238 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
6239 "d,v,t", AT, breg, AT);
6240 if (p != NULL)
6241 p += 4;
6242 }
beae10d5 6243 /* Itbl support may require additional care here. */
252b5132
RH
6244 macro_build (p, &icnt, &offset_expr, s, fmt,
6245 coproc ? treg + 1 : treg,
6246 (int) BFD_RELOC_LO16, AT);
6247 if (p != NULL)
6248 p += 4;
6249 /* FIXME: How do we handle overflow here? */
6250 offset_expr.X_add_number += 4;
beae10d5 6251 /* Itbl support may require additional care here. */
252b5132
RH
6252 macro_build (p, &icnt, &offset_expr, s, fmt,
6253 coproc ? treg : treg + 1,
6254 (int) BFD_RELOC_LO16, AT);
bdaaa2e1 6255 }
252b5132
RH
6256 else if (mips_pic == SVR4_PIC && ! mips_big_got)
6257 {
6258 int off;
6259
6260 /* If this is a reference to an external symbol, we want
6261 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6262 nop
6263 <op> $treg,0($at)
6264 <op> $treg+1,4($at)
6265 Otherwise we want
6266 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6267 nop
6268 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6269 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6270 If there is a base register we add it to $at before the
6271 lwc1 instructions. If there is a constant we include it
6272 in the lwc1 instructions. */
6273 used_at = 1;
6274 expr1.X_add_number = offset_expr.X_add_number;
6275 offset_expr.X_add_number = 0;
6276 if (expr1.X_add_number < -0x8000
6277 || expr1.X_add_number >= 0x8000 - 4)
6278 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6279 if (breg == 0)
6280 off = 0;
6281 else
6282 off = 4;
6283 frag_grow (24 + off);
6284 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 6285 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
6286 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
6287 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6288 if (breg != 0)
6289 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6290 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132 6291 "d,v,t", AT, breg, AT);
beae10d5 6292 /* Itbl support may require additional care here. */
252b5132
RH
6293 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6294 coproc ? treg + 1 : treg,
6295 (int) BFD_RELOC_LO16, AT);
6296 expr1.X_add_number += 4;
6297
6298 /* Set mips_optimize to 2 to avoid inserting an undesired
6299 nop. */
6300 hold_mips_optimize = mips_optimize;
6301 mips_optimize = 2;
beae10d5 6302 /* Itbl support may require additional care here. */
252b5132
RH
6303 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6304 coproc ? treg : treg + 1,
6305 (int) BFD_RELOC_LO16, AT);
6306 mips_optimize = hold_mips_optimize;
6307
6308 (void) frag_var (rs_machine_dependent, 0, 0,
6309 RELAX_ENCODE (0, 0, -16 - off, -8, 1, 0),
c4e7957c 6310 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
6311 }
6312 else if (mips_pic == SVR4_PIC)
6313 {
6314 int gpdel, off;
6315
6316 /* If this is a reference to an external symbol, we want
6317 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6318 addu $at,$at,$gp
6319 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
6320 nop
6321 <op> $treg,0($at)
6322 <op> $treg+1,4($at)
6323 Otherwise we want
6324 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6325 nop
6326 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6327 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6328 If there is a base register we add it to $at before the
6329 lwc1 instructions. If there is a constant we include it
6330 in the lwc1 instructions. */
6331 used_at = 1;
6332 expr1.X_add_number = offset_expr.X_add_number;
6333 offset_expr.X_add_number = 0;
6334 if (expr1.X_add_number < -0x8000
6335 || expr1.X_add_number >= 0x8000 - 4)
6336 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6337 if (reg_needs_delay (GP))
6338 gpdel = 4;
6339 else
6340 gpdel = 0;
6341 if (breg == 0)
6342 off = 0;
6343 else
6344 off = 4;
6345 frag_grow (56);
6346 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
6347 AT, (int) BFD_RELOC_MIPS_GOT_HI16);
6348 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6349 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
6350 "d,v,t", AT, AT, GP);
6351 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 6352 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
6353 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT_LO16, AT);
6354 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6355 if (breg != 0)
6356 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6357 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132 6358 "d,v,t", AT, breg, AT);
beae10d5 6359 /* Itbl support may require additional care here. */
252b5132
RH
6360 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6361 coproc ? treg + 1 : treg,
6362 (int) BFD_RELOC_LO16, AT);
6363 expr1.X_add_number += 4;
6364
6365 /* Set mips_optimize to 2 to avoid inserting an undesired
6366 nop. */
6367 hold_mips_optimize = mips_optimize;
6368 mips_optimize = 2;
beae10d5 6369 /* Itbl support may require additional care here. */
252b5132
RH
6370 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6371 coproc ? treg : treg + 1,
6372 (int) BFD_RELOC_LO16, AT);
6373 mips_optimize = hold_mips_optimize;
6374 expr1.X_add_number -= 4;
6375
6376 p = frag_var (rs_machine_dependent, 16 + gpdel + off, 0,
6377 RELAX_ENCODE (24 + off, 16 + gpdel + off, gpdel,
6378 8 + gpdel + off, 1, 0),
c4e7957c 6379 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
6380 if (gpdel > 0)
6381 {
6382 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6383 p += 4;
6384 }
6385 macro_build (p, &icnt, &offset_expr,
ca4e0257 6386 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
6387 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16, GP);
6388 p += 4;
6389 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6390 p += 4;
6391 if (breg != 0)
6392 {
6393 macro_build (p, &icnt, (expressionS *) NULL,
ca4e0257 6394 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
6395 "d,v,t", AT, breg, AT);
6396 p += 4;
6397 }
beae10d5 6398 /* Itbl support may require additional care here. */
252b5132
RH
6399 macro_build (p, &icnt, &expr1, s, fmt,
6400 coproc ? treg + 1 : treg,
6401 (int) BFD_RELOC_LO16, AT);
6402 p += 4;
6403 expr1.X_add_number += 4;
6404
6405 /* Set mips_optimize to 2 to avoid inserting an undesired
6406 nop. */
6407 hold_mips_optimize = mips_optimize;
6408 mips_optimize = 2;
beae10d5 6409 /* Itbl support may require additional care here. */
252b5132
RH
6410 macro_build (p, &icnt, &expr1, s, fmt,
6411 coproc ? treg : treg + 1,
6412 (int) BFD_RELOC_LO16, AT);
6413 mips_optimize = hold_mips_optimize;
6414 }
6415 else if (mips_pic == EMBEDDED_PIC)
6416 {
6417 /* If there is no base register, we use
cdf6fd85
TS
6418 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6419 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
6420 If we have a base register, we use
6421 addu $at,$breg,$gp
cdf6fd85
TS
6422 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6423 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
6424 */
6425 if (breg == 0)
6426 {
6427 tempreg = GP;
6428 used_at = 0;
6429 }
6430 else
6431 {
6432 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6433 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
6434 "d,v,t", AT, breg, GP);
6435 tempreg = AT;
6436 used_at = 1;
6437 }
6438
beae10d5 6439 /* Itbl support may require additional care here. */
252b5132
RH
6440 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6441 coproc ? treg + 1 : treg,
cdf6fd85 6442 (int) BFD_RELOC_GPREL16, tempreg);
252b5132 6443 offset_expr.X_add_number += 4;
beae10d5 6444 /* Itbl support may require additional care here. */
252b5132
RH
6445 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6446 coproc ? treg : treg + 1,
cdf6fd85 6447 (int) BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6448 }
6449 else
6450 abort ();
6451
6452 if (! used_at)
6453 return;
6454
6455 break;
6456
6457 case M_LD_OB:
6458 s = "lw";
6459 goto sd_ob;
6460 case M_SD_OB:
6461 s = "sw";
6462 sd_ob:
ca4e0257 6463 assert (HAVE_32BIT_ADDRESSES);
252b5132
RH
6464 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
6465 (int) BFD_RELOC_LO16, breg);
6466 offset_expr.X_add_number += 4;
6467 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg + 1,
6468 (int) BFD_RELOC_LO16, breg);
6469 return;
6470
6471 /* New code added to support COPZ instructions.
6472 This code builds table entries out of the macros in mip_opcodes.
6473 R4000 uses interlocks to handle coproc delays.
6474 Other chips (like the R3000) require nops to be inserted for delays.
6475
f72c8c98 6476 FIXME: Currently, we require that the user handle delays.
252b5132
RH
6477 In order to fill delay slots for non-interlocked chips,
6478 we must have a way to specify delays based on the coprocessor.
6479 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
6480 What are the side-effects of the cop instruction?
6481 What cache support might we have and what are its effects?
6482 Both coprocessor & memory require delays. how long???
bdaaa2e1 6483 What registers are read/set/modified?
252b5132
RH
6484
6485 If an itbl is provided to interpret cop instructions,
bdaaa2e1 6486 this knowledge can be encoded in the itbl spec. */
252b5132
RH
6487
6488 case M_COP0:
6489 s = "c0";
6490 goto copz;
6491 case M_COP1:
6492 s = "c1";
6493 goto copz;
6494 case M_COP2:
6495 s = "c2";
6496 goto copz;
6497 case M_COP3:
6498 s = "c3";
6499 copz:
6500 /* For now we just do C (same as Cz). The parameter will be
6501 stored in insn_opcode by mips_ip. */
6502 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "C",
6503 ip->insn_opcode);
6504 return;
6505
ea1fb5dc
RS
6506 case M_MOVE:
6507 move_register (&icnt, dreg, sreg);
6508 return;
6509
252b5132
RH
6510#ifdef LOSING_COMPILER
6511 default:
6512 /* Try and see if this is a new itbl instruction.
6513 This code builds table entries out of the macros in mip_opcodes.
6514 FIXME: For now we just assemble the expression and pass it's
6515 value along as a 32-bit immediate.
bdaaa2e1 6516 We may want to have the assembler assemble this value,
252b5132
RH
6517 so that we gain the assembler's knowledge of delay slots,
6518 symbols, etc.
6519 Would it be more efficient to use mask (id) here? */
bdaaa2e1 6520 if (itbl_have_entries
252b5132 6521 && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
beae10d5 6522 {
252b5132
RH
6523 s = ip->insn_mo->name;
6524 s2 = "cop3";
6525 coproc = ITBL_DECODE_PNUM (immed_expr);;
6526 macro_build ((char *) NULL, &icnt, &immed_expr, s, "C");
6527 return;
beae10d5 6528 }
252b5132
RH
6529 macro2 (ip);
6530 return;
6531 }
6532 if (mips_opts.noat)
6533 as_warn (_("Macro used $at after \".set noat\""));
6534}
bdaaa2e1 6535
252b5132
RH
6536static void
6537macro2 (ip)
6538 struct mips_cl_insn *ip;
6539{
6540 register int treg, sreg, dreg, breg;
6541 int tempreg;
6542 int mask;
6543 int icnt = 0;
6544 int used_at;
6545 expressionS expr1;
6546 const char *s;
6547 const char *s2;
6548 const char *fmt;
6549 int likely = 0;
6550 int dbl = 0;
6551 int coproc = 0;
6552 int lr = 0;
6553 int imm = 0;
6554 int off;
6555 offsetT maxnum;
6556 bfd_reloc_code_real_type r;
6557 char *p;
bdaaa2e1 6558
252b5132
RH
6559 treg = (ip->insn_opcode >> 16) & 0x1f;
6560 dreg = (ip->insn_opcode >> 11) & 0x1f;
6561 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
6562 mask = ip->insn_mo->mask;
bdaaa2e1 6563
252b5132
RH
6564 expr1.X_op = O_constant;
6565 expr1.X_op_symbol = NULL;
6566 expr1.X_add_symbol = NULL;
6567 expr1.X_add_number = 1;
bdaaa2e1 6568
252b5132
RH
6569 switch (mask)
6570 {
6571#endif /* LOSING_COMPILER */
6572
6573 case M_DMUL:
6574 dbl = 1;
6575 case M_MUL:
2396cfb9
TS
6576 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6577 dbl ? "dmultu" : "multu", "s,t", sreg, treg);
6578 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6579 dreg);
252b5132
RH
6580 return;
6581
6582 case M_DMUL_I:
6583 dbl = 1;
6584 case M_MUL_I:
6585 /* The MIPS assembler some times generates shifts and adds. I'm
6586 not trying to be that fancy. GCC should do this for us
6587 anyway. */
6588 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9 6589 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 6590 dbl ? "dmult" : "mult", "s,t", sreg, AT);
2396cfb9
TS
6591 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6592 dreg);
252b5132
RH
6593 break;
6594
6595 case M_DMULO_I:
6596 dbl = 1;
6597 case M_MULO_I:
6598 imm = 1;
6599 goto do_mulo;
6600
6601 case M_DMULO:
6602 dbl = 1;
6603 case M_MULO:
6604 do_mulo:
6605 mips_emit_delays (true);
6606 ++mips_opts.noreorder;
6607 mips_any_noreorder = 1;
6608 if (imm)
6609 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9 6610 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 6611 dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
2396cfb9
TS
6612 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6613 dreg);
6614 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 6615 dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, 31);
2396cfb9
TS
6616 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6617 AT);
252b5132 6618 if (mips_trap)
2396cfb9
TS
6619 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne", "s,t",
6620 dreg, AT);
252b5132
RH
6621 else
6622 {
6623 expr1.X_add_number = 8;
2396cfb9
TS
6624 macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", dreg,
6625 AT);
6626 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6627 0);
6628 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6629 "c", 6);
252b5132
RH
6630 }
6631 --mips_opts.noreorder;
2396cfb9 6632 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d", dreg);
252b5132
RH
6633 break;
6634
6635 case M_DMULOU_I:
6636 dbl = 1;
6637 case M_MULOU_I:
6638 imm = 1;
6639 goto do_mulou;
6640
6641 case M_DMULOU:
6642 dbl = 1;
6643 case M_MULOU:
6644 do_mulou:
6645 mips_emit_delays (true);
6646 ++mips_opts.noreorder;
6647 mips_any_noreorder = 1;
6648 if (imm)
6649 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9 6650 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
252b5132
RH
6651 dbl ? "dmultu" : "multu",
6652 "s,t", sreg, imm ? AT : treg);
2396cfb9
TS
6653 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6654 AT);
6655 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6656 dreg);
252b5132 6657 if (mips_trap)
2396cfb9
TS
6658 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne", "s,t",
6659 AT, 0);
252b5132
RH
6660 else
6661 {
6662 expr1.X_add_number = 8;
6663 macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", AT, 0);
2396cfb9
TS
6664 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6665 0);
6666 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6667 "c", 6);
252b5132
RH
6668 }
6669 --mips_opts.noreorder;
6670 break;
6671
6672 case M_ROL:
2396cfb9
TS
6673 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
6674 "d,v,t", AT, 0, treg);
6675 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
6676 "d,t,s", AT, sreg, AT);
6677 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
6678 "d,t,s", dreg, sreg, treg);
6679 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6680 "d,v,t", dreg, dreg, AT);
252b5132
RH
6681 break;
6682
6683 case M_ROL_I:
6684 if (imm_expr.X_op != O_constant)
6685 as_bad (_("rotate count too large"));
2396cfb9
TS
6686 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
6687 AT, sreg, (int) (imm_expr.X_add_number & 0x1f));
6688 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
6689 dreg, sreg, (int) ((0 - imm_expr.X_add_number) & 0x1f));
6690 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
6691 dreg, dreg, AT);
252b5132
RH
6692 break;
6693
6694 case M_ROR:
2396cfb9
TS
6695 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
6696 "d,v,t", AT, 0, treg);
6697 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
6698 "d,t,s", AT, sreg, AT);
6699 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
6700 "d,t,s", dreg, sreg, treg);
6701 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6702 "d,v,t", dreg, dreg, AT);
252b5132
RH
6703 break;
6704
6705 case M_ROR_I:
6706 if (imm_expr.X_op != O_constant)
6707 as_bad (_("rotate count too large"));
2396cfb9
TS
6708 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
6709 AT, sreg, (int) (imm_expr.X_add_number & 0x1f));
6710 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
6711 dreg, sreg, (int) ((0 - imm_expr.X_add_number) & 0x1f));
6712 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
6713 dreg, dreg, AT);
252b5132
RH
6714 break;
6715
6716 case M_S_DOB:
ec68c924 6717 if (mips_arch == CPU_R4650)
252b5132
RH
6718 {
6719 as_bad (_("opcode not supported on this processor"));
6720 return;
6721 }
e7af610e 6722 assert (mips_opts.isa == ISA_MIPS1);
252b5132
RH
6723 /* Even on a big endian machine $fn comes before $fn+1. We have
6724 to adjust when storing to memory. */
6725 macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
6726 target_big_endian ? treg + 1 : treg,
6727 (int) BFD_RELOC_LO16, breg);
6728 offset_expr.X_add_number += 4;
6729 macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
6730 target_big_endian ? treg : treg + 1,
6731 (int) BFD_RELOC_LO16, breg);
6732 return;
6733
6734 case M_SEQ:
6735 if (sreg == 0)
6736 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6737 treg, (int) BFD_RELOC_LO16);
6738 else if (treg == 0)
6739 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6740 sreg, (int) BFD_RELOC_LO16);
6741 else
6742 {
2396cfb9
TS
6743 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6744 "d,v,t", dreg, sreg, treg);
252b5132
RH
6745 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6746 dreg, (int) BFD_RELOC_LO16);
6747 }
6748 return;
6749
6750 case M_SEQ_I:
6751 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6752 {
6753 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6754 sreg, (int) BFD_RELOC_LO16);
6755 return;
6756 }
6757 if (sreg == 0)
6758 {
6759 as_warn (_("Instruction %s: result is always false"),
6760 ip->insn_mo->name);
ea1fb5dc 6761 move_register (&icnt, dreg, 0);
252b5132
RH
6762 return;
6763 }
6764 if (imm_expr.X_op == O_constant
6765 && imm_expr.X_add_number >= 0
6766 && imm_expr.X_add_number < 0x10000)
6767 {
6768 macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg,
6769 sreg, (int) BFD_RELOC_LO16);
6770 used_at = 0;
6771 }
6772 else if (imm_expr.X_op == O_constant
6773 && imm_expr.X_add_number > -0x8000
6774 && imm_expr.X_add_number < 0)
6775 {
6776 imm_expr.X_add_number = -imm_expr.X_add_number;
6777 macro_build ((char *) NULL, &icnt, &imm_expr,
ca4e0257 6778 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
252b5132
RH
6779 "t,r,j", dreg, sreg,
6780 (int) BFD_RELOC_LO16);
6781 used_at = 0;
6782 }
6783 else
6784 {
4d34fb5f 6785 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
6786 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6787 "d,v,t", dreg, sreg, AT);
252b5132
RH
6788 used_at = 1;
6789 }
6790 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
6791 (int) BFD_RELOC_LO16);
6792 if (used_at)
6793 break;
6794 return;
6795
6796 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
6797 s = "slt";
6798 goto sge;
6799 case M_SGEU:
6800 s = "sltu";
6801 sge:
2396cfb9
TS
6802 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6803 dreg, sreg, treg);
252b5132
RH
6804 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6805 (int) BFD_RELOC_LO16);
6806 return;
6807
6808 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
6809 case M_SGEU_I:
6810 if (imm_expr.X_op == O_constant
6811 && imm_expr.X_add_number >= -0x8000
6812 && imm_expr.X_add_number < 0x8000)
6813 {
6814 macro_build ((char *) NULL, &icnt, &imm_expr,
6815 mask == M_SGE_I ? "slti" : "sltiu",
6816 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6817 used_at = 0;
6818 }
6819 else
6820 {
4d34fb5f 6821 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
6822 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6823 mask == M_SGE_I ? "slt" : "sltu", "d,v,t", dreg, sreg,
6824 AT);
252b5132
RH
6825 used_at = 1;
6826 }
6827 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6828 (int) BFD_RELOC_LO16);
6829 if (used_at)
6830 break;
6831 return;
6832
6833 case M_SGT: /* sreg > treg <==> treg < sreg */
6834 s = "slt";
6835 goto sgt;
6836 case M_SGTU:
6837 s = "sltu";
6838 sgt:
2396cfb9
TS
6839 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6840 dreg, treg, sreg);
252b5132
RH
6841 return;
6842
6843 case M_SGT_I: /* sreg > I <==> I < sreg */
6844 s = "slt";
6845 goto sgti;
6846 case M_SGTU_I:
6847 s = "sltu";
6848 sgti:
4d34fb5f 6849 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
6850 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6851 dreg, AT, sreg);
252b5132
RH
6852 break;
6853
2396cfb9 6854 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
252b5132
RH
6855 s = "slt";
6856 goto sle;
6857 case M_SLEU:
6858 s = "sltu";
6859 sle:
2396cfb9
TS
6860 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6861 dreg, treg, sreg);
252b5132
RH
6862 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6863 (int) BFD_RELOC_LO16);
6864 return;
6865
2396cfb9 6866 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
252b5132
RH
6867 s = "slt";
6868 goto slei;
6869 case M_SLEU_I:
6870 s = "sltu";
6871 slei:
4d34fb5f 6872 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
6873 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6874 dreg, AT, sreg);
252b5132
RH
6875 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6876 (int) BFD_RELOC_LO16);
6877 break;
6878
6879 case M_SLT_I:
6880 if (imm_expr.X_op == O_constant
6881 && imm_expr.X_add_number >= -0x8000
6882 && imm_expr.X_add_number < 0x8000)
6883 {
6884 macro_build ((char *) NULL, &icnt, &imm_expr, "slti", "t,r,j",
6885 dreg, sreg, (int) BFD_RELOC_LO16);
6886 return;
6887 }
4d34fb5f 6888 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
6889 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
6890 dreg, sreg, AT);
252b5132
RH
6891 break;
6892
6893 case M_SLTU_I:
6894 if (imm_expr.X_op == O_constant
6895 && imm_expr.X_add_number >= -0x8000
6896 && imm_expr.X_add_number < 0x8000)
6897 {
6898 macro_build ((char *) NULL, &icnt, &imm_expr, "sltiu", "t,r,j",
6899 dreg, sreg, (int) BFD_RELOC_LO16);
6900 return;
6901 }
4d34fb5f 6902 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
6903 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6904 "d,v,t", dreg, sreg, AT);
252b5132
RH
6905 break;
6906
6907 case M_SNE:
6908 if (sreg == 0)
2396cfb9
TS
6909 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6910 "d,v,t", dreg, 0, treg);
252b5132 6911 else if (treg == 0)
2396cfb9
TS
6912 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6913 "d,v,t", dreg, 0, sreg);
252b5132
RH
6914 else
6915 {
2396cfb9
TS
6916 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6917 "d,v,t", dreg, sreg, treg);
6918 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6919 "d,v,t", dreg, 0, dreg);
252b5132
RH
6920 }
6921 return;
6922
6923 case M_SNE_I:
6924 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6925 {
2396cfb9
TS
6926 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6927 "d,v,t", dreg, 0, sreg);
252b5132
RH
6928 return;
6929 }
6930 if (sreg == 0)
6931 {
6932 as_warn (_("Instruction %s: result is always true"),
6933 ip->insn_mo->name);
6934 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 6935 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
252b5132
RH
6936 "t,r,j", dreg, 0, (int) BFD_RELOC_LO16);
6937 return;
6938 }
6939 if (imm_expr.X_op == O_constant
6940 && imm_expr.X_add_number >= 0
6941 && imm_expr.X_add_number < 0x10000)
6942 {
6943 macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i",
6944 dreg, sreg, (int) BFD_RELOC_LO16);
6945 used_at = 0;
6946 }
6947 else if (imm_expr.X_op == O_constant
6948 && imm_expr.X_add_number > -0x8000
6949 && imm_expr.X_add_number < 0)
6950 {
6951 imm_expr.X_add_number = -imm_expr.X_add_number;
6952 macro_build ((char *) NULL, &icnt, &imm_expr,
ca4e0257 6953 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
252b5132
RH
6954 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6955 used_at = 0;
6956 }
6957 else
6958 {
4d34fb5f 6959 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
6960 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6961 "d,v,t", dreg, sreg, AT);
252b5132
RH
6962 used_at = 1;
6963 }
2396cfb9
TS
6964 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6965 "d,v,t", dreg, 0, dreg);
252b5132
RH
6966 if (used_at)
6967 break;
6968 return;
6969
6970 case M_DSUB_I:
6971 dbl = 1;
6972 case M_SUB_I:
6973 if (imm_expr.X_op == O_constant
6974 && imm_expr.X_add_number > -0x8000
6975 && imm_expr.X_add_number <= 0x8000)
6976 {
6977 imm_expr.X_add_number = -imm_expr.X_add_number;
6978 macro_build ((char *) NULL, &icnt, &imm_expr,
6979 dbl ? "daddi" : "addi",
6980 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6981 return;
6982 }
6983 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9 6984 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 6985 dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
252b5132
RH
6986 break;
6987
6988 case M_DSUBU_I:
6989 dbl = 1;
6990 case M_SUBU_I:
6991 if (imm_expr.X_op == O_constant
6992 && imm_expr.X_add_number > -0x8000
6993 && imm_expr.X_add_number <= 0x8000)
6994 {
6995 imm_expr.X_add_number = -imm_expr.X_add_number;
6996 macro_build ((char *) NULL, &icnt, &imm_expr,
6997 dbl ? "daddiu" : "addiu",
6998 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6999 return;
7000 }
7001 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9 7002 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 7003 dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
252b5132
RH
7004 break;
7005
7006 case M_TEQ_I:
7007 s = "teq";
7008 goto trap;
7009 case M_TGE_I:
7010 s = "tge";
7011 goto trap;
7012 case M_TGEU_I:
7013 s = "tgeu";
7014 goto trap;
7015 case M_TLT_I:
7016 s = "tlt";
7017 goto trap;
7018 case M_TLTU_I:
7019 s = "tltu";
7020 goto trap;
7021 case M_TNE_I:
7022 s = "tne";
7023 trap:
4d34fb5f 7024 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
7025 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "s,t", sreg,
7026 AT);
252b5132
RH
7027 break;
7028
252b5132 7029 case M_TRUNCWS:
43841e91 7030 case M_TRUNCWD:
e7af610e 7031 assert (mips_opts.isa == ISA_MIPS1);
252b5132
RH
7032 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
7033 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
7034
7035 /*
7036 * Is the double cfc1 instruction a bug in the mips assembler;
7037 * or is there a reason for it?
7038 */
7039 mips_emit_delays (true);
7040 ++mips_opts.noreorder;
7041 mips_any_noreorder = 1;
2396cfb9
TS
7042 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
7043 treg, 31);
7044 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
7045 treg, 31);
7046 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
252b5132
RH
7047 expr1.X_add_number = 3;
7048 macro_build ((char *) NULL, &icnt, &expr1, "ori", "t,r,i", AT, treg,
7049 (int) BFD_RELOC_LO16);
7050 expr1.X_add_number = 2;
7051 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", AT, AT,
7052 (int) BFD_RELOC_LO16);
2396cfb9
TS
7053 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
7054 AT, 31);
7055 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7056 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
252b5132 7057 mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S", dreg, sreg);
2396cfb9
TS
7058 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
7059 treg, 31);
7060 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
252b5132
RH
7061 --mips_opts.noreorder;
7062 break;
7063
7064 case M_ULH:
7065 s = "lb";
7066 goto ulh;
7067 case M_ULHU:
7068 s = "lbu";
7069 ulh:
7070 if (offset_expr.X_add_number >= 0x7fff)
7071 as_bad (_("operand overflow"));
7072 /* avoid load delay */
7073 if (! target_big_endian)
7074 offset_expr.X_add_number += 1;
7075 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7076 (int) BFD_RELOC_LO16, breg);
7077 if (! target_big_endian)
7078 offset_expr.X_add_number -= 1;
7079 else
7080 offset_expr.X_add_number += 1;
7081 macro_build ((char *) NULL, &icnt, &offset_expr, "lbu", "t,o(b)", AT,
7082 (int) BFD_RELOC_LO16, breg);
2396cfb9
TS
7083 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7084 treg, treg, 8);
7085 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7086 treg, treg, AT);
252b5132
RH
7087 break;
7088
7089 case M_ULD:
7090 s = "ldl";
7091 s2 = "ldr";
7092 off = 7;
7093 goto ulw;
7094 case M_ULW:
7095 s = "lwl";
7096 s2 = "lwr";
7097 off = 3;
7098 ulw:
7099 if (offset_expr.X_add_number >= 0x8000 - off)
7100 as_bad (_("operand overflow"));
7101 if (! target_big_endian)
7102 offset_expr.X_add_number += off;
7103 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7104 (int) BFD_RELOC_LO16, breg);
7105 if (! target_big_endian)
7106 offset_expr.X_add_number -= off;
7107 else
7108 offset_expr.X_add_number += off;
7109 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7110 (int) BFD_RELOC_LO16, breg);
7111 return;
7112
7113 case M_ULD_A:
7114 s = "ldl";
7115 s2 = "ldr";
7116 off = 7;
7117 goto ulwa;
7118 case M_ULW_A:
7119 s = "lwl";
7120 s2 = "lwr";
7121 off = 3;
7122 ulwa:
d6bc6245
TS
7123 used_at = 1;
7124 load_address (&icnt, AT, &offset_expr, HAVE_64BIT_ADDRESSES, &used_at);
252b5132
RH
7125 if (breg != 0)
7126 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 7127 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
7128 "d,v,t", AT, AT, breg);
7129 if (! target_big_endian)
7130 expr1.X_add_number = off;
7131 else
7132 expr1.X_add_number = 0;
7133 macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7134 (int) BFD_RELOC_LO16, AT);
7135 if (! target_big_endian)
7136 expr1.X_add_number = 0;
7137 else
7138 expr1.X_add_number = off;
7139 macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7140 (int) BFD_RELOC_LO16, AT);
7141 break;
7142
7143 case M_ULH_A:
7144 case M_ULHU_A:
d6bc6245
TS
7145 used_at = 1;
7146 load_address (&icnt, AT, &offset_expr, HAVE_64BIT_ADDRESSES, &used_at);
252b5132
RH
7147 if (breg != 0)
7148 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 7149 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
7150 "d,v,t", AT, AT, breg);
7151 if (target_big_endian)
7152 expr1.X_add_number = 0;
7153 macro_build ((char *) NULL, &icnt, &expr1,
7154 mask == M_ULH_A ? "lb" : "lbu", "t,o(b)", treg,
7155 (int) BFD_RELOC_LO16, AT);
7156 if (target_big_endian)
7157 expr1.X_add_number = 1;
7158 else
7159 expr1.X_add_number = 0;
7160 macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7161 (int) BFD_RELOC_LO16, AT);
2396cfb9
TS
7162 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7163 treg, treg, 8);
7164 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7165 treg, treg, AT);
252b5132
RH
7166 break;
7167
7168 case M_USH:
7169 if (offset_expr.X_add_number >= 0x7fff)
7170 as_bad (_("operand overflow"));
7171 if (target_big_endian)
7172 offset_expr.X_add_number += 1;
7173 macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", treg,
7174 (int) BFD_RELOC_LO16, breg);
2396cfb9
TS
7175 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7176 AT, treg, 8);
252b5132
RH
7177 if (target_big_endian)
7178 offset_expr.X_add_number -= 1;
7179 else
7180 offset_expr.X_add_number += 1;
7181 macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", AT,
7182 (int) BFD_RELOC_LO16, breg);
7183 break;
7184
7185 case M_USD:
7186 s = "sdl";
7187 s2 = "sdr";
7188 off = 7;
7189 goto usw;
7190 case M_USW:
7191 s = "swl";
7192 s2 = "swr";
7193 off = 3;
7194 usw:
7195 if (offset_expr.X_add_number >= 0x8000 - off)
7196 as_bad (_("operand overflow"));
7197 if (! target_big_endian)
7198 offset_expr.X_add_number += off;
7199 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7200 (int) BFD_RELOC_LO16, breg);
7201 if (! target_big_endian)
7202 offset_expr.X_add_number -= off;
7203 else
7204 offset_expr.X_add_number += off;
7205 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7206 (int) BFD_RELOC_LO16, breg);
7207 return;
7208
7209 case M_USD_A:
7210 s = "sdl";
7211 s2 = "sdr";
7212 off = 7;
7213 goto uswa;
7214 case M_USW_A:
7215 s = "swl";
7216 s2 = "swr";
7217 off = 3;
7218 uswa:
d6bc6245
TS
7219 used_at = 1;
7220 load_address (&icnt, AT, &offset_expr, HAVE_64BIT_ADDRESSES, &used_at);
252b5132
RH
7221 if (breg != 0)
7222 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 7223 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
7224 "d,v,t", AT, AT, breg);
7225 if (! target_big_endian)
7226 expr1.X_add_number = off;
7227 else
7228 expr1.X_add_number = 0;
7229 macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7230 (int) BFD_RELOC_LO16, AT);
7231 if (! target_big_endian)
7232 expr1.X_add_number = 0;
7233 else
7234 expr1.X_add_number = off;
7235 macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7236 (int) BFD_RELOC_LO16, AT);
7237 break;
7238
7239 case M_USH_A:
d6bc6245
TS
7240 used_at = 1;
7241 load_address (&icnt, AT, &offset_expr, HAVE_64BIT_ADDRESSES, &used_at);
252b5132
RH
7242 if (breg != 0)
7243 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 7244 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
7245 "d,v,t", AT, AT, breg);
7246 if (! target_big_endian)
7247 expr1.X_add_number = 0;
7248 macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7249 (int) BFD_RELOC_LO16, AT);
2396cfb9
TS
7250 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7251 treg, treg, 8);
252b5132
RH
7252 if (! target_big_endian)
7253 expr1.X_add_number = 1;
7254 else
7255 expr1.X_add_number = 0;
7256 macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7257 (int) BFD_RELOC_LO16, AT);
7258 if (! target_big_endian)
7259 expr1.X_add_number = 0;
7260 else
7261 expr1.X_add_number = 1;
7262 macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7263 (int) BFD_RELOC_LO16, AT);
2396cfb9
TS
7264 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7265 treg, treg, 8);
7266 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7267 treg, treg, AT);
252b5132
RH
7268 break;
7269
7270 default:
7271 /* FIXME: Check if this is one of the itbl macros, since they
bdaaa2e1 7272 are added dynamically. */
252b5132
RH
7273 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7274 break;
7275 }
7276 if (mips_opts.noat)
7277 as_warn (_("Macro used $at after \".set noat\""));
7278}
7279
7280/* Implement macros in mips16 mode. */
7281
7282static void
7283mips16_macro (ip)
7284 struct mips_cl_insn *ip;
7285{
7286 int mask;
7287 int xreg, yreg, zreg, tmp;
7288 int icnt;
7289 expressionS expr1;
7290 int dbl;
7291 const char *s, *s2, *s3;
7292
7293 mask = ip->insn_mo->mask;
7294
7295 xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
7296 yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
7297 zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
7298
7299 icnt = 0;
7300
7301 expr1.X_op = O_constant;
7302 expr1.X_op_symbol = NULL;
7303 expr1.X_add_symbol = NULL;
7304 expr1.X_add_number = 1;
7305
7306 dbl = 0;
7307
7308 switch (mask)
7309 {
7310 default:
7311 internalError ();
7312
7313 case M_DDIV_3:
7314 dbl = 1;
7315 case M_DIV_3:
7316 s = "mflo";
7317 goto do_div3;
7318 case M_DREM_3:
7319 dbl = 1;
7320 case M_REM_3:
7321 s = "mfhi";
7322 do_div3:
7323 mips_emit_delays (true);
7324 ++mips_opts.noreorder;
7325 mips_any_noreorder = 1;
2396cfb9 7326 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
252b5132
RH
7327 dbl ? "ddiv" : "div",
7328 "0,x,y", xreg, yreg);
7329 expr1.X_add_number = 2;
7330 macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
2396cfb9
TS
7331 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break", "6",
7332 7);
bdaaa2e1 7333
252b5132
RH
7334 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7335 since that causes an overflow. We should do that as well,
7336 but I don't see how to do the comparisons without a temporary
7337 register. */
7338 --mips_opts.noreorder;
2396cfb9 7339 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x", zreg);
252b5132
RH
7340 break;
7341
7342 case M_DIVU_3:
7343 s = "divu";
7344 s2 = "mflo";
7345 goto do_divu3;
7346 case M_REMU_3:
7347 s = "divu";
7348 s2 = "mfhi";
7349 goto do_divu3;
7350 case M_DDIVU_3:
7351 s = "ddivu";
7352 s2 = "mflo";
7353 goto do_divu3;
7354 case M_DREMU_3:
7355 s = "ddivu";
7356 s2 = "mfhi";
7357 do_divu3:
7358 mips_emit_delays (true);
7359 ++mips_opts.noreorder;
7360 mips_any_noreorder = 1;
2396cfb9
TS
7361 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "0,x,y",
7362 xreg, yreg);
252b5132
RH
7363 expr1.X_add_number = 2;
7364 macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
2396cfb9
TS
7365 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
7366 "6", 7);
252b5132 7367 --mips_opts.noreorder;
2396cfb9 7368 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "x", zreg);
252b5132
RH
7369 break;
7370
7371 case M_DMUL:
7372 dbl = 1;
7373 case M_MUL:
2396cfb9 7374 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 7375 dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
2396cfb9
TS
7376 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "x",
7377 zreg);
252b5132
RH
7378 return;
7379
7380 case M_DSUBU_I:
7381 dbl = 1;
7382 goto do_subu;
7383 case M_SUBU_I:
7384 do_subu:
7385 if (imm_expr.X_op != O_constant)
7386 as_bad (_("Unsupported large constant"));
7387 imm_expr.X_add_number = -imm_expr.X_add_number;
7388 macro_build ((char *) NULL, &icnt, &imm_expr,
9a41af64 7389 dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
252b5132
RH
7390 break;
7391
7392 case M_SUBU_I_2:
7393 if (imm_expr.X_op != O_constant)
7394 as_bad (_("Unsupported large constant"));
7395 imm_expr.X_add_number = -imm_expr.X_add_number;
7396 macro_build ((char *) NULL, &icnt, &imm_expr, "addiu",
7397 "x,k", xreg);
7398 break;
7399
7400 case M_DSUBU_I_2:
7401 if (imm_expr.X_op != O_constant)
7402 as_bad (_("Unsupported large constant"));
7403 imm_expr.X_add_number = -imm_expr.X_add_number;
7404 macro_build ((char *) NULL, &icnt, &imm_expr, "daddiu",
7405 "y,j", yreg);
7406 break;
7407
7408 case M_BEQ:
7409 s = "cmp";
7410 s2 = "bteqz";
7411 goto do_branch;
7412 case M_BNE:
7413 s = "cmp";
7414 s2 = "btnez";
7415 goto do_branch;
7416 case M_BLT:
7417 s = "slt";
7418 s2 = "btnez";
7419 goto do_branch;
7420 case M_BLTU:
7421 s = "sltu";
7422 s2 = "btnez";
7423 goto do_branch;
7424 case M_BLE:
7425 s = "slt";
7426 s2 = "bteqz";
7427 goto do_reverse_branch;
7428 case M_BLEU:
7429 s = "sltu";
7430 s2 = "bteqz";
7431 goto do_reverse_branch;
7432 case M_BGE:
7433 s = "slt";
7434 s2 = "bteqz";
7435 goto do_branch;
7436 case M_BGEU:
7437 s = "sltu";
7438 s2 = "bteqz";
7439 goto do_branch;
7440 case M_BGT:
7441 s = "slt";
7442 s2 = "btnez";
7443 goto do_reverse_branch;
7444 case M_BGTU:
7445 s = "sltu";
7446 s2 = "btnez";
7447
7448 do_reverse_branch:
7449 tmp = xreg;
7450 xreg = yreg;
7451 yreg = tmp;
7452
7453 do_branch:
7454 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x,y",
7455 xreg, yreg);
7456 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7457 break;
7458
7459 case M_BEQ_I:
7460 s = "cmpi";
7461 s2 = "bteqz";
7462 s3 = "x,U";
7463 goto do_branch_i;
7464 case M_BNE_I:
7465 s = "cmpi";
7466 s2 = "btnez";
7467 s3 = "x,U";
7468 goto do_branch_i;
7469 case M_BLT_I:
7470 s = "slti";
7471 s2 = "btnez";
7472 s3 = "x,8";
7473 goto do_branch_i;
7474 case M_BLTU_I:
7475 s = "sltiu";
7476 s2 = "btnez";
7477 s3 = "x,8";
7478 goto do_branch_i;
7479 case M_BLE_I:
7480 s = "slti";
7481 s2 = "btnez";
7482 s3 = "x,8";
7483 goto do_addone_branch_i;
7484 case M_BLEU_I:
7485 s = "sltiu";
7486 s2 = "btnez";
7487 s3 = "x,8";
7488 goto do_addone_branch_i;
7489 case M_BGE_I:
7490 s = "slti";
7491 s2 = "bteqz";
7492 s3 = "x,8";
7493 goto do_branch_i;
7494 case M_BGEU_I:
7495 s = "sltiu";
7496 s2 = "bteqz";
7497 s3 = "x,8";
7498 goto do_branch_i;
7499 case M_BGT_I:
7500 s = "slti";
7501 s2 = "bteqz";
7502 s3 = "x,8";
7503 goto do_addone_branch_i;
7504 case M_BGTU_I:
7505 s = "sltiu";
7506 s2 = "bteqz";
7507 s3 = "x,8";
7508
7509 do_addone_branch_i:
7510 if (imm_expr.X_op != O_constant)
7511 as_bad (_("Unsupported large constant"));
7512 ++imm_expr.X_add_number;
7513
7514 do_branch_i:
7515 macro_build ((char *) NULL, &icnt, &imm_expr, s, s3, xreg);
7516 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7517 break;
7518
7519 case M_ABS:
7520 expr1.X_add_number = 0;
7521 macro_build ((char *) NULL, &icnt, &expr1, "slti", "x,8", yreg);
7522 if (xreg != yreg)
ea1fb5dc 7523 move_register (&icnt, xreg, yreg);
252b5132
RH
7524 expr1.X_add_number = 2;
7525 macro_build ((char *) NULL, &icnt, &expr1, "bteqz", "p");
7526 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7527 "neg", "x,w", xreg, xreg);
7528 }
7529}
7530
7531/* For consistency checking, verify that all bits are specified either
7532 by the match/mask part of the instruction definition, or by the
7533 operand list. */
7534static int
7535validate_mips_insn (opc)
7536 const struct mips_opcode *opc;
7537{
7538 const char *p = opc->args;
7539 char c;
7540 unsigned long used_bits = opc->mask;
7541
7542 if ((used_bits & opc->match) != opc->match)
7543 {
7544 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
7545 opc->name, opc->args);
7546 return 0;
7547 }
7548#define USE_BITS(mask,shift) (used_bits |= ((mask) << (shift)))
7549 while (*p)
7550 switch (c = *p++)
7551 {
7552 case ',': break;
7553 case '(': break;
7554 case ')': break;
7555 case '<': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7556 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7557 case 'A': break;
4372b673 7558 case 'B': USE_BITS (OP_MASK_CODE20, OP_SH_CODE20); break;
252b5132
RH
7559 case 'C': USE_BITS (OP_MASK_COPZ, OP_SH_COPZ); break;
7560 case 'D': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
7561 case 'E': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7562 case 'F': break;
7563 case 'G': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
156c2f8b 7564 case 'H': USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
252b5132 7565 case 'I': break;
e972090a 7566 case 'J': USE_BITS (OP_MASK_CODE19, OP_SH_CODE19); break;
252b5132
RH
7567 case 'L': break;
7568 case 'M': USE_BITS (OP_MASK_CCC, OP_SH_CCC); break;
7569 case 'N': USE_BITS (OP_MASK_BCC, OP_SH_BCC); break;
7570 case 'R': USE_BITS (OP_MASK_FR, OP_SH_FR); break;
7571 case 'S': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7572 case 'T': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7573 case 'V': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7574 case 'W': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7575 case 'a': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break;
7576 case 'b': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7577 case 'c': USE_BITS (OP_MASK_CODE, OP_SH_CODE); break;
7578 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
7579 case 'f': break;
7580 case 'h': USE_BITS (OP_MASK_PREFX, OP_SH_PREFX); break;
7581 case 'i': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
7582 case 'j': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7583 case 'k': USE_BITS (OP_MASK_CACHE, OP_SH_CACHE); break;
7584 case 'l': break;
7585 case 'o': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7586 case 'p': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7587 case 'q': USE_BITS (OP_MASK_CODE2, OP_SH_CODE2); break;
7588 case 'r': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7589 case 's': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7590 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7591 case 'u': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
7592 case 'v': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7593 case 'w': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7594 case 'x': break;
7595 case 'z': break;
7596 case 'P': USE_BITS (OP_MASK_PERFREG, OP_SH_PERFREG); break;
4372b673
NC
7597 case 'U': USE_BITS (OP_MASK_RD, OP_SH_RD);
7598 USE_BITS (OP_MASK_RT, OP_SH_RT); break;
252b5132
RH
7599 default:
7600 as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
7601 c, opc->name, opc->args);
7602 return 0;
7603 }
7604#undef USE_BITS
7605 if (used_bits != 0xffffffff)
7606 {
7607 as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
7608 ~used_bits & 0xffffffff, opc->name, opc->args);
7609 return 0;
7610 }
7611 return 1;
7612}
7613
7614/* This routine assembles an instruction into its binary format. As a
7615 side effect, it sets one of the global variables imm_reloc or
7616 offset_reloc to the type of relocation to do if one of the operands
7617 is an address expression. */
7618
7619static void
7620mips_ip (str, ip)
7621 char *str;
7622 struct mips_cl_insn *ip;
7623{
7624 char *s;
7625 const char *args;
43841e91 7626 char c = 0;
252b5132
RH
7627 struct mips_opcode *insn;
7628 char *argsStart;
7629 unsigned int regno;
7630 unsigned int lastregno = 0;
7631 char *s_reset;
7632 char save_c = 0;
7633 int full_opcode_match = 1;
7634
7635 insn_error = NULL;
7636
7637 /* If the instruction contains a '.', we first try to match an instruction
7638 including the '.'. Then we try again without the '.'. */
7639 insn = NULL;
3882b010 7640 for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
252b5132
RH
7641 continue;
7642
7643 /* If we stopped on whitespace, then replace the whitespace with null for
7644 the call to hash_find. Save the character we replaced just in case we
7645 have to re-parse the instruction. */
3882b010 7646 if (ISSPACE (*s))
252b5132
RH
7647 {
7648 save_c = *s;
7649 *s++ = '\0';
7650 }
bdaaa2e1 7651
252b5132
RH
7652 insn = (struct mips_opcode *) hash_find (op_hash, str);
7653
7654 /* If we didn't find the instruction in the opcode table, try again, but
7655 this time with just the instruction up to, but not including the
7656 first '.'. */
7657 if (insn == NULL)
7658 {
bdaaa2e1 7659 /* Restore the character we overwrite above (if any). */
252b5132
RH
7660 if (save_c)
7661 *(--s) = save_c;
7662
7663 /* Scan up to the first '.' or whitespace. */
3882b010
L
7664 for (s = str;
7665 *s != '\0' && *s != '.' && !ISSPACE (*s);
7666 ++s)
252b5132
RH
7667 continue;
7668
7669 /* If we did not find a '.', then we can quit now. */
7670 if (*s != '.')
7671 {
7672 insn_error = "unrecognized opcode";
7673 return;
7674 }
7675
7676 /* Lookup the instruction in the hash table. */
7677 *s++ = '\0';
7678 if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
7679 {
7680 insn_error = "unrecognized opcode";
7681 return;
7682 }
7683
7684 full_opcode_match = 0;
7685 }
7686
7687 argsStart = s;
7688 for (;;)
7689 {
252b5132
RH
7690 boolean ok;
7691
7692 assert (strcmp (insn->name, str) == 0);
7693
1f25f5d3
CD
7694 if (OPCODE_IS_MEMBER (insn,
7695 (mips_opts.isa
7696 | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
7697 mips_arch))
252b5132 7698 ok = true;
bdaaa2e1 7699 else
252b5132 7700 ok = false;
bdaaa2e1 7701
252b5132
RH
7702 if (insn->pinfo != INSN_MACRO)
7703 {
ec68c924 7704 if (mips_arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
252b5132
RH
7705 ok = false;
7706 }
7707
7708 if (! ok)
7709 {
7710 if (insn + 1 < &mips_opcodes[NUMOPCODES]
7711 && strcmp (insn->name, insn[1].name) == 0)
7712 {
7713 ++insn;
7714 continue;
7715 }
252b5132 7716 else
beae10d5 7717 {
268f6bed
L
7718 if (!insn_error)
7719 {
7720 static char buf[100];
7721 sprintf (buf,
7722 _("opcode not supported on this processor: %s (%s)"),
ec68c924 7723 mips_cpu_to_str (mips_arch),
268f6bed 7724 mips_isa_to_str (mips_opts.isa));
bdaaa2e1 7725
268f6bed
L
7726 insn_error = buf;
7727 }
7728 if (save_c)
7729 *(--s) = save_c;
2bd7f1f3 7730 return;
252b5132 7731 }
252b5132
RH
7732 }
7733
7734 ip->insn_mo = insn;
7735 ip->insn_opcode = insn->match;
268f6bed 7736 insn_error = NULL;
252b5132
RH
7737 for (args = insn->args;; ++args)
7738 {
ad8d3bb3 7739 s += strspn (s, " \t");
252b5132
RH
7740 switch (*args)
7741 {
7742 case '\0': /* end of args */
7743 if (*s == '\0')
7744 return;
7745 break;
7746
7747 case ',':
7748 if (*s++ == *args)
7749 continue;
7750 s--;
7751 switch (*++args)
7752 {
7753 case 'r':
7754 case 'v':
38487616 7755 ip->insn_opcode |= lastregno << OP_SH_RS;
252b5132
RH
7756 continue;
7757
7758 case 'w':
38487616
TS
7759 ip->insn_opcode |= lastregno << OP_SH_RT;
7760 continue;
7761
252b5132 7762 case 'W':
38487616 7763 ip->insn_opcode |= lastregno << OP_SH_FT;
252b5132
RH
7764 continue;
7765
7766 case 'V':
38487616 7767 ip->insn_opcode |= lastregno << OP_SH_FS;
252b5132
RH
7768 continue;
7769 }
7770 break;
7771
7772 case '(':
7773 /* Handle optional base register.
7774 Either the base register is omitted or
bdaaa2e1 7775 we must have a left paren. */
252b5132
RH
7776 /* This is dependent on the next operand specifier
7777 is a base register specification. */
7778 assert (args[1] == 'b' || args[1] == '5'
7779 || args[1] == '-' || args[1] == '4');
7780 if (*s == '\0')
7781 return;
7782
7783 case ')': /* these must match exactly */
7784 if (*s++ == *args)
7785 continue;
7786 break;
7787
7788 case '<': /* must be at least one digit */
7789 /*
7790 * According to the manual, if the shift amount is greater
b6ff326e
KH
7791 * than 31 or less than 0, then the shift amount should be
7792 * mod 32. In reality the mips assembler issues an error.
252b5132
RH
7793 * We issue a warning and mask out all but the low 5 bits.
7794 */
7795 my_getExpression (&imm_expr, s);
7796 check_absolute_expr (ip, &imm_expr);
7797 if ((unsigned long) imm_expr.X_add_number > 31)
7798 {
7799 as_warn (_("Improper shift amount (%ld)"),
7800 (long) imm_expr.X_add_number);
38487616 7801 imm_expr.X_add_number &= OP_MASK_SHAMT;
252b5132 7802 }
38487616 7803 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SHAMT;
252b5132
RH
7804 imm_expr.X_op = O_absent;
7805 s = expr_end;
7806 continue;
7807
7808 case '>': /* shift amount minus 32 */
7809 my_getExpression (&imm_expr, s);
7810 check_absolute_expr (ip, &imm_expr);
7811 if ((unsigned long) imm_expr.X_add_number < 32
7812 || (unsigned long) imm_expr.X_add_number > 63)
7813 break;
38487616 7814 ip->insn_opcode |= (imm_expr.X_add_number - 32) << OP_SH_SHAMT;
252b5132
RH
7815 imm_expr.X_op = O_absent;
7816 s = expr_end;
7817 continue;
7818
252b5132
RH
7819 case 'k': /* cache code */
7820 case 'h': /* prefx code */
7821 my_getExpression (&imm_expr, s);
7822 check_absolute_expr (ip, &imm_expr);
7823 if ((unsigned long) imm_expr.X_add_number > 31)
7824 {
7825 as_warn (_("Invalid value for `%s' (%lu)"),
7826 ip->insn_mo->name,
7827 (unsigned long) imm_expr.X_add_number);
7828 imm_expr.X_add_number &= 0x1f;
7829 }
7830 if (*args == 'k')
7831 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
7832 else
7833 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
7834 imm_expr.X_op = O_absent;
7835 s = expr_end;
7836 continue;
7837
7838 case 'c': /* break code */
7839 my_getExpression (&imm_expr, s);
7840 check_absolute_expr (ip, &imm_expr);
7841 if ((unsigned) imm_expr.X_add_number > 1023)
7842 {
7843 as_warn (_("Illegal break code (%ld)"),
7844 (long) imm_expr.X_add_number);
38487616 7845 imm_expr.X_add_number &= OP_MASK_CODE;
252b5132 7846 }
38487616 7847 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE;
252b5132
RH
7848 imm_expr.X_op = O_absent;
7849 s = expr_end;
7850 continue;
7851
7852 case 'q': /* lower break code */
7853 my_getExpression (&imm_expr, s);
7854 check_absolute_expr (ip, &imm_expr);
7855 if ((unsigned) imm_expr.X_add_number > 1023)
7856 {
7857 as_warn (_("Illegal lower break code (%ld)"),
7858 (long) imm_expr.X_add_number);
38487616 7859 imm_expr.X_add_number &= OP_MASK_CODE2;
252b5132 7860 }
38487616 7861 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE2;
252b5132
RH
7862 imm_expr.X_op = O_absent;
7863 s = expr_end;
7864 continue;
7865
4372b673 7866 case 'B': /* 20-bit syscall/break code. */
156c2f8b 7867 my_getExpression (&imm_expr, s);
156c2f8b 7868 check_absolute_expr (ip, &imm_expr);
38487616 7869 if ((unsigned) imm_expr.X_add_number > OP_MASK_CODE20)
4372b673 7870 as_warn (_("Illegal 20-bit code (%ld)"),
252b5132 7871 (long) imm_expr.X_add_number);
38487616 7872 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE20;
252b5132
RH
7873 imm_expr.X_op = O_absent;
7874 s = expr_end;
7875 continue;
7876
7877 case 'C': /* Coprocessor code */
beae10d5 7878 my_getExpression (&imm_expr, s);
252b5132 7879 check_absolute_expr (ip, &imm_expr);
beae10d5 7880 if ((unsigned long) imm_expr.X_add_number >= (1<<25))
252b5132 7881 {
beae10d5 7882 as_warn (_("Coproccesor code > 25 bits (%ld)"),
252b5132 7883 (long) imm_expr.X_add_number);
beae10d5 7884 imm_expr.X_add_number &= ((1<<25) - 1);
252b5132 7885 }
beae10d5
KH
7886 ip->insn_opcode |= imm_expr.X_add_number;
7887 imm_expr.X_op = O_absent;
7888 s = expr_end;
7889 continue;
252b5132 7890
4372b673
NC
7891 case 'J': /* 19-bit wait code. */
7892 my_getExpression (&imm_expr, s);
7893 check_absolute_expr (ip, &imm_expr);
38487616 7894 if ((unsigned) imm_expr.X_add_number > OP_MASK_CODE19)
4372b673
NC
7895 as_warn (_("Illegal 19-bit code (%ld)"),
7896 (long) imm_expr.X_add_number);
38487616 7897 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE19;
4372b673
NC
7898 imm_expr.X_op = O_absent;
7899 s = expr_end;
7900 continue;
7901
252b5132 7902 case 'P': /* Performance register */
beae10d5 7903 my_getExpression (&imm_expr, s);
252b5132 7904 check_absolute_expr (ip, &imm_expr);
beae10d5 7905 if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
252b5132 7906 {
38487616 7907 as_warn (_("Invalid performance register (%ld)"),
252b5132 7908 (long) imm_expr.X_add_number);
38487616 7909 imm_expr.X_add_number &= OP_MASK_PERFREG;
252b5132 7910 }
38487616 7911 ip->insn_opcode |= (imm_expr.X_add_number << OP_SH_PERFREG);
beae10d5
KH
7912 imm_expr.X_op = O_absent;
7913 s = expr_end;
7914 continue;
252b5132
RH
7915
7916 case 'b': /* base register */
7917 case 'd': /* destination register */
7918 case 's': /* source register */
7919 case 't': /* target register */
7920 case 'r': /* both target and source */
7921 case 'v': /* both dest and source */
7922 case 'w': /* both dest and target */
7923 case 'E': /* coprocessor target register */
7924 case 'G': /* coprocessor destination register */
7925 case 'x': /* ignore register name */
7926 case 'z': /* must be zero register */
4372b673 7927 case 'U': /* destination register (clo/clz). */
252b5132
RH
7928 s_reset = s;
7929 if (s[0] == '$')
7930 {
7931
3882b010 7932 if (ISDIGIT (s[1]))
252b5132
RH
7933 {
7934 ++s;
7935 regno = 0;
7936 do
7937 {
7938 regno *= 10;
7939 regno += *s - '0';
7940 ++s;
7941 }
3882b010 7942 while (ISDIGIT (*s));
252b5132
RH
7943 if (regno > 31)
7944 as_bad (_("Invalid register number (%d)"), regno);
7945 }
7946 else if (*args == 'E' || *args == 'G')
7947 goto notreg;
7948 else
7949 {
7950 if (s[1] == 'f' && s[2] == 'p')
7951 {
7952 s += 3;
7953 regno = FP;
7954 }
7955 else if (s[1] == 's' && s[2] == 'p')
7956 {
7957 s += 3;
7958 regno = SP;
7959 }
7960 else if (s[1] == 'g' && s[2] == 'p')
7961 {
7962 s += 3;
7963 regno = GP;
7964 }
7965 else if (s[1] == 'a' && s[2] == 't')
7966 {
7967 s += 3;
7968 regno = AT;
7969 }
7970 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
7971 {
7972 s += 4;
7973 regno = KT0;
7974 }
7975 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
7976 {
7977 s += 4;
7978 regno = KT1;
7979 }
7980 else if (itbl_have_entries)
7981 {
7982 char *p, *n;
d7ba4a77 7983 unsigned long r;
252b5132 7984
d7ba4a77 7985 p = s + 1; /* advance past '$' */
252b5132
RH
7986 n = itbl_get_field (&p); /* n is name */
7987
d7ba4a77
ILT
7988 /* See if this is a register defined in an
7989 itbl entry. */
7990 if (itbl_get_reg_val (n, &r))
252b5132
RH
7991 {
7992 /* Get_field advances to the start of
7993 the next field, so we need to back
d7ba4a77 7994 rack to the end of the last field. */
bdaaa2e1 7995 if (p)
252b5132 7996 s = p - 1;
bdaaa2e1 7997 else
d7ba4a77 7998 s = strchr (s, '\0');
252b5132
RH
7999 regno = r;
8000 }
8001 else
8002 goto notreg;
beae10d5 8003 }
252b5132
RH
8004 else
8005 goto notreg;
8006 }
8007 if (regno == AT
8008 && ! mips_opts.noat
8009 && *args != 'E'
8010 && *args != 'G')
8011 as_warn (_("Used $at without \".set noat\""));
8012 c = *args;
8013 if (*s == ' ')
8014 s++;
8015 if (args[1] != *s)
8016 {
8017 if (c == 'r' || c == 'v' || c == 'w')
8018 {
8019 regno = lastregno;
8020 s = s_reset;
8021 args++;
8022 }
8023 }
8024 /* 'z' only matches $0. */
8025 if (c == 'z' && regno != 0)
8026 break;
8027
bdaaa2e1
KH
8028 /* Now that we have assembled one operand, we use the args string
8029 * to figure out where it goes in the instruction. */
252b5132
RH
8030 switch (c)
8031 {
8032 case 'r':
8033 case 's':
8034 case 'v':
8035 case 'b':
38487616 8036 ip->insn_opcode |= regno << OP_SH_RS;
252b5132
RH
8037 break;
8038 case 'd':
8039 case 'G':
38487616 8040 ip->insn_opcode |= regno << OP_SH_RD;
252b5132 8041 break;
4372b673 8042 case 'U':
38487616
TS
8043 ip->insn_opcode |= regno << OP_SH_RD;
8044 ip->insn_opcode |= regno << OP_SH_RT;
4372b673 8045 break;
252b5132
RH
8046 case 'w':
8047 case 't':
8048 case 'E':
38487616 8049 ip->insn_opcode |= regno << OP_SH_RT;
252b5132
RH
8050 break;
8051 case 'x':
8052 /* This case exists because on the r3000 trunc
8053 expands into a macro which requires a gp
8054 register. On the r6000 or r4000 it is
8055 assembled into a single instruction which
8056 ignores the register. Thus the insn version
8057 is MIPS_ISA2 and uses 'x', and the macro
8058 version is MIPS_ISA1 and uses 't'. */
8059 break;
8060 case 'z':
8061 /* This case is for the div instruction, which
8062 acts differently if the destination argument
8063 is $0. This only matches $0, and is checked
8064 outside the switch. */
8065 break;
8066 case 'D':
8067 /* Itbl operand; not yet implemented. FIXME ?? */
8068 break;
8069 /* What about all other operands like 'i', which
8070 can be specified in the opcode table? */
8071 }
8072 lastregno = regno;
8073 continue;
8074 }
8075 notreg:
8076 switch (*args++)
8077 {
8078 case 'r':
8079 case 'v':
38487616 8080 ip->insn_opcode |= lastregno << OP_SH_RS;
252b5132
RH
8081 continue;
8082 case 'w':
38487616 8083 ip->insn_opcode |= lastregno << OP_SH_RT;
252b5132
RH
8084 continue;
8085 }
8086 break;
8087
8088 case 'D': /* floating point destination register */
8089 case 'S': /* floating point source register */
8090 case 'T': /* floating point target register */
8091 case 'R': /* floating point source register */
8092 case 'V':
8093 case 'W':
8094 s_reset = s;
3882b010
L
8095 if (s[0] == '$' && s[1] == 'f'
8096 && ISDIGIT (s[2]))
252b5132
RH
8097 {
8098 s += 2;
8099 regno = 0;
8100 do
8101 {
8102 regno *= 10;
8103 regno += *s - '0';
8104 ++s;
8105 }
3882b010 8106 while (ISDIGIT (*s));
252b5132
RH
8107
8108 if (regno > 31)
8109 as_bad (_("Invalid float register number (%d)"), regno);
8110
8111 if ((regno & 1) != 0
ca4e0257 8112 && HAVE_32BIT_FPRS
252b5132
RH
8113 && ! (strcmp (str, "mtc1") == 0
8114 || strcmp (str, "mfc1") == 0
8115 || strcmp (str, "lwc1") == 0
8116 || strcmp (str, "swc1") == 0
8117 || strcmp (str, "l.s") == 0
8118 || strcmp (str, "s.s") == 0))
8119 as_warn (_("Float register should be even, was %d"),
8120 regno);
8121
8122 c = *args;
8123 if (*s == ' ')
8124 s++;
8125 if (args[1] != *s)
8126 {
8127 if (c == 'V' || c == 'W')
8128 {
8129 regno = lastregno;
8130 s = s_reset;
8131 args++;
8132 }
8133 }
8134 switch (c)
8135 {
8136 case 'D':
38487616 8137 ip->insn_opcode |= regno << OP_SH_FD;
252b5132
RH
8138 break;
8139 case 'V':
8140 case 'S':
38487616 8141 ip->insn_opcode |= regno << OP_SH_FS;
252b5132
RH
8142 break;
8143 case 'W':
8144 case 'T':
38487616 8145 ip->insn_opcode |= regno << OP_SH_FT;
252b5132
RH
8146 break;
8147 case 'R':
38487616 8148 ip->insn_opcode |= regno << OP_SH_FR;
252b5132
RH
8149 break;
8150 }
8151 lastregno = regno;
8152 continue;
8153 }
8154
252b5132
RH
8155 switch (*args++)
8156 {
8157 case 'V':
38487616 8158 ip->insn_opcode |= lastregno << OP_SH_FS;
252b5132
RH
8159 continue;
8160 case 'W':
38487616 8161 ip->insn_opcode |= lastregno << OP_SH_FT;
252b5132
RH
8162 continue;
8163 }
8164 break;
8165
8166 case 'I':
8167 my_getExpression (&imm_expr, s);
8168 if (imm_expr.X_op != O_big
8169 && imm_expr.X_op != O_constant)
8170 insn_error = _("absolute expression required");
8171 s = expr_end;
8172 continue;
8173
8174 case 'A':
8175 my_getExpression (&offset_expr, s);
f6688943 8176 *imm_reloc = BFD_RELOC_32;
252b5132
RH
8177 s = expr_end;
8178 continue;
8179
8180 case 'F':
8181 case 'L':
8182 case 'f':
8183 case 'l':
8184 {
8185 int f64;
ca4e0257 8186 int using_gprs;
252b5132
RH
8187 char *save_in;
8188 char *err;
8189 unsigned char temp[8];
8190 int len;
8191 unsigned int length;
8192 segT seg;
8193 subsegT subseg;
8194 char *p;
8195
8196 /* These only appear as the last operand in an
8197 instruction, and every instruction that accepts
8198 them in any variant accepts them in all variants.
8199 This means we don't have to worry about backing out
8200 any changes if the instruction does not match.
8201
8202 The difference between them is the size of the
8203 floating point constant and where it goes. For 'F'
8204 and 'L' the constant is 64 bits; for 'f' and 'l' it
8205 is 32 bits. Where the constant is placed is based
8206 on how the MIPS assembler does things:
8207 F -- .rdata
8208 L -- .lit8
8209 f -- immediate value
8210 l -- .lit4
8211
8212 The .lit4 and .lit8 sections are only used if
8213 permitted by the -G argument.
8214
8215 When generating embedded PIC code, we use the
8216 .lit8 section but not the .lit4 section (we can do
8217 .lit4 inline easily; we need to put .lit8
8218 somewhere in the data segment, and using .lit8
8219 permits the linker to eventually combine identical
ca4e0257
RS
8220 .lit8 entries).
8221
8222 The code below needs to know whether the target register
8223 is 32 or 64 bits wide. It relies on the fact 'f' and
8224 'F' are used with GPR-based instructions and 'l' and
8225 'L' are used with FPR-based instructions. */
252b5132
RH
8226
8227 f64 = *args == 'F' || *args == 'L';
ca4e0257 8228 using_gprs = *args == 'F' || *args == 'f';
252b5132
RH
8229
8230 save_in = input_line_pointer;
8231 input_line_pointer = s;
8232 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
8233 length = len;
8234 s = input_line_pointer;
8235 input_line_pointer = save_in;
8236 if (err != NULL && *err != '\0')
8237 {
8238 as_bad (_("Bad floating point constant: %s"), err);
8239 memset (temp, '\0', sizeof temp);
8240 length = f64 ? 8 : 4;
8241 }
8242
156c2f8b 8243 assert (length == (unsigned) (f64 ? 8 : 4));
252b5132
RH
8244
8245 if (*args == 'f'
8246 || (*args == 'l'
8247 && (! USE_GLOBAL_POINTER_OPT
8248 || mips_pic == EMBEDDED_PIC
8249 || g_switch_value < 4
8250 || (temp[0] == 0 && temp[1] == 0)
8251 || (temp[2] == 0 && temp[3] == 0))))
8252 {
8253 imm_expr.X_op = O_constant;
8254 if (! target_big_endian)
8255 imm_expr.X_add_number = bfd_getl32 (temp);
8256 else
8257 imm_expr.X_add_number = bfd_getb32 (temp);
8258 }
8259 else if (length > 4
119d663a 8260 && ! mips_disable_float_construction
ca4e0257
RS
8261 /* Constants can only be constructed in GPRs and
8262 copied to FPRs if the GPRs are at least as wide
8263 as the FPRs. Force the constant into memory if
8264 we are using 64-bit FPRs but the GPRs are only
8265 32 bits wide. */
8266 && (using_gprs
8267 || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
252b5132
RH
8268 && ((temp[0] == 0 && temp[1] == 0)
8269 || (temp[2] == 0 && temp[3] == 0))
8270 && ((temp[4] == 0 && temp[5] == 0)
8271 || (temp[6] == 0 && temp[7] == 0)))
8272 {
ca4e0257
RS
8273 /* The value is simple enough to load with a couple of
8274 instructions. If using 32-bit registers, set
8275 imm_expr to the high order 32 bits and offset_expr to
8276 the low order 32 bits. Otherwise, set imm_expr to
8277 the entire 64 bit constant. */
8278 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
252b5132
RH
8279 {
8280 imm_expr.X_op = O_constant;
8281 offset_expr.X_op = O_constant;
8282 if (! target_big_endian)
8283 {
8284 imm_expr.X_add_number = bfd_getl32 (temp + 4);
8285 offset_expr.X_add_number = bfd_getl32 (temp);
8286 }
8287 else
8288 {
8289 imm_expr.X_add_number = bfd_getb32 (temp);
8290 offset_expr.X_add_number = bfd_getb32 (temp + 4);
8291 }
8292 if (offset_expr.X_add_number == 0)
8293 offset_expr.X_op = O_absent;
8294 }
8295 else if (sizeof (imm_expr.X_add_number) > 4)
8296 {
8297 imm_expr.X_op = O_constant;
8298 if (! target_big_endian)
8299 imm_expr.X_add_number = bfd_getl64 (temp);
8300 else
8301 imm_expr.X_add_number = bfd_getb64 (temp);
8302 }
8303 else
8304 {
8305 imm_expr.X_op = O_big;
8306 imm_expr.X_add_number = 4;
8307 if (! target_big_endian)
8308 {
8309 generic_bignum[0] = bfd_getl16 (temp);
8310 generic_bignum[1] = bfd_getl16 (temp + 2);
8311 generic_bignum[2] = bfd_getl16 (temp + 4);
8312 generic_bignum[3] = bfd_getl16 (temp + 6);
8313 }
8314 else
8315 {
8316 generic_bignum[0] = bfd_getb16 (temp + 6);
8317 generic_bignum[1] = bfd_getb16 (temp + 4);
8318 generic_bignum[2] = bfd_getb16 (temp + 2);
8319 generic_bignum[3] = bfd_getb16 (temp);
8320 }
8321 }
8322 }
8323 else
8324 {
8325 const char *newname;
8326 segT new_seg;
8327
8328 /* Switch to the right section. */
8329 seg = now_seg;
8330 subseg = now_subseg;
8331 switch (*args)
8332 {
8333 default: /* unused default case avoids warnings. */
8334 case 'L':
8335 newname = RDATA_SECTION_NAME;
bb2d6cd7
GK
8336 if ((USE_GLOBAL_POINTER_OPT && g_switch_value >= 8)
8337 || mips_pic == EMBEDDED_PIC)
252b5132
RH
8338 newname = ".lit8";
8339 break;
8340 case 'F':
bb2d6cd7
GK
8341 if (mips_pic == EMBEDDED_PIC)
8342 newname = ".lit8";
8343 else
8344 newname = RDATA_SECTION_NAME;
252b5132
RH
8345 break;
8346 case 'l':
8347 assert (!USE_GLOBAL_POINTER_OPT
8348 || g_switch_value >= 4);
8349 newname = ".lit4";
8350 break;
8351 }
8352 new_seg = subseg_new (newname, (subsegT) 0);
8353 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
8354 bfd_set_section_flags (stdoutput, new_seg,
8355 (SEC_ALLOC
8356 | SEC_LOAD
8357 | SEC_READONLY
8358 | SEC_DATA));
8359 frag_align (*args == 'l' ? 2 : 3, 0, 0);
8360 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
8361 && strcmp (TARGET_OS, "elf") != 0)
8362 record_alignment (new_seg, 4);
8363 else
8364 record_alignment (new_seg, *args == 'l' ? 2 : 3);
8365 if (seg == now_seg)
8366 as_bad (_("Can't use floating point insn in this section"));
8367
8368 /* Set the argument to the current address in the
8369 section. */
8370 offset_expr.X_op = O_symbol;
8371 offset_expr.X_add_symbol =
8372 symbol_new ("L0\001", now_seg,
8373 (valueT) frag_now_fix (), frag_now);
8374 offset_expr.X_add_number = 0;
8375
8376 /* Put the floating point number into the section. */
8377 p = frag_more ((int) length);
8378 memcpy (p, temp, length);
8379
8380 /* Switch back to the original section. */
8381 subseg_set (seg, subseg);
8382 }
8383 }
8384 continue;
8385
8386 case 'i': /* 16 bit unsigned immediate */
8387 case 'j': /* 16 bit signed immediate */
f6688943 8388 *imm_reloc = BFD_RELOC_LO16;
252b5132 8389 c = my_getSmallExpression (&imm_expr, s);
fb1b3232 8390 if (c != S_EX_NONE)
252b5132 8391 {
fb1b3232 8392 if (c != S_EX_LO)
252b5132
RH
8393 {
8394 if (imm_expr.X_op == O_constant)
8395 imm_expr.X_add_number =
8396 (imm_expr.X_add_number >> 16) & 0xffff;
ad8d3bb3 8397#ifdef OBJ_ELF
fb1b3232 8398 else if (c == S_EX_HIGHEST)
f6688943 8399 *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
fb1b3232 8400 else if (c == S_EX_HIGHER)
f6688943 8401 *imm_reloc = BFD_RELOC_MIPS_HIGHER;
ad8d3bb3
TS
8402 else if (c == S_EX_GP_REL)
8403 {
8404 /* This occurs in NewABI only. */
8405 c = my_getSmallExpression (&imm_expr, s);
8406 if (c != S_EX_NEG)
8407 as_bad (_("bad composition of relocations"));
8408 else
8409 {
8410 c = my_getSmallExpression (&imm_expr, s);
8411 if (c != S_EX_LO)
8412 as_bad (_("bad composition of relocations"));
8413 else
8414 {
8415 imm_reloc[0] = BFD_RELOC_GPREL16;
8416 imm_reloc[1] = BFD_RELOC_MIPS_SUB;
8417 imm_reloc[2] = BFD_RELOC_LO16;
8418 }
8419 }
8420 }
8421#endif
fb1b3232 8422 else if (c == S_EX_HI)
252b5132 8423 {
f6688943 8424 *imm_reloc = BFD_RELOC_HI16_S;
252b5132
RH
8425 imm_unmatched_hi = true;
8426 }
8427 else
f6688943 8428 *imm_reloc = BFD_RELOC_HI16;
252b5132
RH
8429 }
8430 else if (imm_expr.X_op == O_constant)
8431 imm_expr.X_add_number &= 0xffff;
8432 }
8433 if (*args == 'i')
8434 {
fb1b3232 8435 if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
252b5132 8436 || ((imm_expr.X_add_number < 0
beae10d5
KH
8437 || imm_expr.X_add_number >= 0x10000)
8438 && imm_expr.X_op == O_constant))
252b5132
RH
8439 {
8440 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8441 !strcmp (insn->name, insn[1].name))
8442 break;
2ae7e77b
AH
8443 if (imm_expr.X_op == O_constant
8444 || imm_expr.X_op == O_big)
252b5132
RH
8445 as_bad (_("16 bit expression not in range 0..65535"));
8446 }
8447 }
8448 else
8449 {
8450 int more;
8451 offsetT max;
8452
8453 /* The upper bound should be 0x8000, but
8454 unfortunately the MIPS assembler accepts numbers
8455 from 0x8000 to 0xffff and sign extends them, and
8456 we want to be compatible. We only permit this
8457 extended range for an instruction which does not
8458 provide any further alternates, since those
8459 alternates may handle other cases. People should
8460 use the numbers they mean, rather than relying on
8461 a mysterious sign extension. */
8462 more = (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8463 strcmp (insn->name, insn[1].name) == 0);
8464 if (more)
8465 max = 0x8000;
8466 else
8467 max = 0x10000;
fb1b3232 8468 if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
252b5132 8469 || ((imm_expr.X_add_number < -0x8000
beae10d5
KH
8470 || imm_expr.X_add_number >= max)
8471 && imm_expr.X_op == O_constant)
252b5132
RH
8472 || (more
8473 && imm_expr.X_add_number < 0
ca4e0257 8474 && HAVE_64BIT_GPRS
252b5132
RH
8475 && imm_expr.X_unsigned
8476 && sizeof (imm_expr.X_add_number) <= 4))
8477 {
8478 if (more)
8479 break;
2ae7e77b
AH
8480 if (imm_expr.X_op == O_constant
8481 || imm_expr.X_op == O_big)
252b5132
RH
8482 as_bad (_("16 bit expression not in range -32768..32767"));
8483 }
8484 }
8485 s = expr_end;
8486 continue;
8487
8488 case 'o': /* 16 bit offset */
8489 c = my_getSmallExpression (&offset_expr, s);
8490
8491 /* If this value won't fit into a 16 bit offset, then go
8492 find a macro that will generate the 32 bit offset
afdbd6d0 8493 code pattern. */
fb1b3232 8494 if (c == S_EX_NONE
252b5132
RH
8495 && (offset_expr.X_op != O_constant
8496 || offset_expr.X_add_number >= 0x8000
afdbd6d0 8497 || offset_expr.X_add_number < -0x8000))
252b5132
RH
8498 break;
8499
fb1b3232 8500 if (c == S_EX_HI)
252b5132
RH
8501 {
8502 if (offset_expr.X_op != O_constant)
8503 break;
8504 offset_expr.X_add_number =
8505 (offset_expr.X_add_number >> 16) & 0xffff;
8506 }
f6688943 8507 *offset_reloc = BFD_RELOC_LO16;
252b5132
RH
8508 s = expr_end;
8509 continue;
8510
8511 case 'p': /* pc relative offset */
cb56d3d3 8512 if (mips_pic == EMBEDDED_PIC)
f6688943 8513 *offset_reloc = BFD_RELOC_16_PCREL_S2;
cb56d3d3 8514 else
f6688943 8515 *offset_reloc = BFD_RELOC_16_PCREL;
252b5132
RH
8516 my_getExpression (&offset_expr, s);
8517 s = expr_end;
8518 continue;
8519
8520 case 'u': /* upper 16 bits */
8521 c = my_getSmallExpression (&imm_expr, s);
f6688943 8522 *imm_reloc = BFD_RELOC_LO16;
e7d556df 8523 if (c != S_EX_NONE)
252b5132 8524 {
fb1b3232 8525 if (c != S_EX_LO)
252b5132
RH
8526 {
8527 if (imm_expr.X_op == O_constant)
8528 imm_expr.X_add_number =
8529 (imm_expr.X_add_number >> 16) & 0xffff;
fb1b3232 8530 else if (c == S_EX_HI)
252b5132 8531 {
f6688943 8532 *imm_reloc = BFD_RELOC_HI16_S;
252b5132
RH
8533 imm_unmatched_hi = true;
8534 }
645dc66c
TS
8535#ifdef OBJ_ELF
8536 else if (c == S_EX_HIGHEST)
8537 *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
ad8d3bb3
TS
8538 else if (c == S_EX_GP_REL)
8539 {
8540 /* This occurs in NewABI only. */
8541 c = my_getSmallExpression (&imm_expr, s);
8542 if (c != S_EX_NEG)
8543 as_bad (_("bad composition of relocations"));
8544 else
8545 {
8546 c = my_getSmallExpression (&imm_expr, s);
8547 if (c != S_EX_HI)
8548 as_bad (_("bad composition of relocations"));
8549 else
8550 {
8551 imm_reloc[0] = BFD_RELOC_GPREL16;
8552 imm_reloc[1] = BFD_RELOC_MIPS_SUB;
8553 imm_reloc[2] = BFD_RELOC_HI16_S;
8554 }
8555 }
8556 }
8557#endif
252b5132 8558 else
f6688943 8559 *imm_reloc = BFD_RELOC_HI16;
252b5132
RH
8560 }
8561 else if (imm_expr.X_op == O_constant)
8562 imm_expr.X_add_number &= 0xffff;
8563 }
8564 if (imm_expr.X_op == O_constant
8565 && (imm_expr.X_add_number < 0
8566 || imm_expr.X_add_number >= 0x10000))
8567 as_bad (_("lui expression not in range 0..65535"));
8568 s = expr_end;
8569 continue;
8570
8571 case 'a': /* 26 bit address */
8572 my_getExpression (&offset_expr, s);
8573 s = expr_end;
f6688943 8574 *offset_reloc = BFD_RELOC_MIPS_JMP;
252b5132
RH
8575 continue;
8576
8577 case 'N': /* 3 bit branch condition code */
8578 case 'M': /* 3 bit compare condition code */
8579 if (strncmp (s, "$fcc", 4) != 0)
8580 break;
8581 s += 4;
8582 regno = 0;
8583 do
8584 {
8585 regno *= 10;
8586 regno += *s - '0';
8587 ++s;
8588 }
3882b010 8589 while (ISDIGIT (*s));
252b5132
RH
8590 if (regno > 7)
8591 as_bad (_("invalid condition code register $fcc%d"), regno);
8592 if (*args == 'N')
8593 ip->insn_opcode |= regno << OP_SH_BCC;
8594 else
8595 ip->insn_opcode |= regno << OP_SH_CCC;
beae10d5 8596 continue;
252b5132 8597
156c2f8b
NC
8598 case 'H':
8599 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
8600 s += 2;
3882b010 8601 if (ISDIGIT (*s))
156c2f8b
NC
8602 {
8603 c = 0;
8604 do
8605 {
8606 c *= 10;
8607 c += *s - '0';
8608 ++s;
8609 }
3882b010 8610 while (ISDIGIT (*s));
156c2f8b
NC
8611 }
8612 else
8613 c = 8; /* Invalid sel value. */
8614
8615 if (c > 7)
8616 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
8617 ip->insn_opcode |= c;
8618 continue;
8619
252b5132
RH
8620 default:
8621 as_bad (_("bad char = '%c'\n"), *args);
8622 internalError ();
8623 }
8624 break;
8625 }
8626 /* Args don't match. */
8627 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8628 !strcmp (insn->name, insn[1].name))
8629 {
8630 ++insn;
8631 s = argsStart;
268f6bed 8632 insn_error = _("illegal operands");
252b5132
RH
8633 continue;
8634 }
268f6bed
L
8635 if (save_c)
8636 *(--s) = save_c;
252b5132
RH
8637 insn_error = _("illegal operands");
8638 return;
8639 }
8640}
8641
8642/* This routine assembles an instruction into its binary format when
8643 assembling for the mips16. As a side effect, it sets one of the
8644 global variables imm_reloc or offset_reloc to the type of
8645 relocation to do if one of the operands is an address expression.
8646 It also sets mips16_small and mips16_ext if the user explicitly
8647 requested a small or extended instruction. */
8648
8649static void
8650mips16_ip (str, ip)
8651 char *str;
8652 struct mips_cl_insn *ip;
8653{
8654 char *s;
8655 const char *args;
8656 struct mips_opcode *insn;
8657 char *argsstart;
8658 unsigned int regno;
8659 unsigned int lastregno = 0;
8660 char *s_reset;
8661
8662 insn_error = NULL;
8663
8664 mips16_small = false;
8665 mips16_ext = false;
8666
3882b010 8667 for (s = str; ISLOWER (*s); ++s)
252b5132
RH
8668 ;
8669 switch (*s)
8670 {
8671 case '\0':
8672 break;
8673
8674 case ' ':
8675 *s++ = '\0';
8676 break;
8677
8678 case '.':
8679 if (s[1] == 't' && s[2] == ' ')
8680 {
8681 *s = '\0';
8682 mips16_small = true;
8683 s += 3;
8684 break;
8685 }
8686 else if (s[1] == 'e' && s[2] == ' ')
8687 {
8688 *s = '\0';
8689 mips16_ext = true;
8690 s += 3;
8691 break;
8692 }
8693 /* Fall through. */
8694 default:
8695 insn_error = _("unknown opcode");
8696 return;
8697 }
8698
8699 if (mips_opts.noautoextend && ! mips16_ext)
8700 mips16_small = true;
8701
8702 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
8703 {
8704 insn_error = _("unrecognized opcode");
8705 return;
8706 }
8707
8708 argsstart = s;
8709 for (;;)
8710 {
8711 assert (strcmp (insn->name, str) == 0);
8712
8713 ip->insn_mo = insn;
8714 ip->insn_opcode = insn->match;
8715 ip->use_extend = false;
8716 imm_expr.X_op = O_absent;
f6688943
TS
8717 imm_reloc[0] = BFD_RELOC_UNUSED;
8718 imm_reloc[1] = BFD_RELOC_UNUSED;
8719 imm_reloc[2] = BFD_RELOC_UNUSED;
252b5132 8720 offset_expr.X_op = O_absent;
f6688943
TS
8721 offset_reloc[0] = BFD_RELOC_UNUSED;
8722 offset_reloc[1] = BFD_RELOC_UNUSED;
8723 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
8724 for (args = insn->args; 1; ++args)
8725 {
8726 int c;
8727
8728 if (*s == ' ')
8729 ++s;
8730
8731 /* In this switch statement we call break if we did not find
8732 a match, continue if we did find a match, or return if we
8733 are done. */
8734
8735 c = *args;
8736 switch (c)
8737 {
8738 case '\0':
8739 if (*s == '\0')
8740 {
8741 /* Stuff the immediate value in now, if we can. */
8742 if (imm_expr.X_op == O_constant
f6688943 8743 && *imm_reloc > BFD_RELOC_UNUSED
252b5132
RH
8744 && insn->pinfo != INSN_MACRO)
8745 {
c4e7957c 8746 mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
252b5132
RH
8747 imm_expr.X_add_number, true, mips16_small,
8748 mips16_ext, &ip->insn_opcode,
8749 &ip->use_extend, &ip->extend);
8750 imm_expr.X_op = O_absent;
f6688943 8751 *imm_reloc = BFD_RELOC_UNUSED;
252b5132
RH
8752 }
8753
8754 return;
8755 }
8756 break;
8757
8758 case ',':
8759 if (*s++ == c)
8760 continue;
8761 s--;
8762 switch (*++args)
8763 {
8764 case 'v':
8765 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
8766 continue;
8767 case 'w':
8768 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
8769 continue;
8770 }
8771 break;
8772
8773 case '(':
8774 case ')':
8775 if (*s++ == c)
8776 continue;
8777 break;
8778
8779 case 'v':
8780 case 'w':
8781 if (s[0] != '$')
8782 {
8783 if (c == 'v')
8784 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
8785 else
8786 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
8787 ++args;
8788 continue;
8789 }
8790 /* Fall through. */
8791 case 'x':
8792 case 'y':
8793 case 'z':
8794 case 'Z':
8795 case '0':
8796 case 'S':
8797 case 'R':
8798 case 'X':
8799 case 'Y':
8800 if (s[0] != '$')
8801 break;
8802 s_reset = s;
3882b010 8803 if (ISDIGIT (s[1]))
252b5132
RH
8804 {
8805 ++s;
8806 regno = 0;
8807 do
8808 {
8809 regno *= 10;
8810 regno += *s - '0';
8811 ++s;
8812 }
3882b010 8813 while (ISDIGIT (*s));
252b5132
RH
8814 if (regno > 31)
8815 {
8816 as_bad (_("invalid register number (%d)"), regno);
8817 regno = 2;
8818 }
8819 }
8820 else
8821 {
8822 if (s[1] == 'f' && s[2] == 'p')
8823 {
8824 s += 3;
8825 regno = FP;
8826 }
8827 else if (s[1] == 's' && s[2] == 'p')
8828 {
8829 s += 3;
8830 regno = SP;
8831 }
8832 else if (s[1] == 'g' && s[2] == 'p')
8833 {
8834 s += 3;
8835 regno = GP;
8836 }
8837 else if (s[1] == 'a' && s[2] == 't')
8838 {
8839 s += 3;
8840 regno = AT;
8841 }
8842 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8843 {
8844 s += 4;
8845 regno = KT0;
8846 }
8847 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8848 {
8849 s += 4;
8850 regno = KT1;
8851 }
8852 else
8853 break;
8854 }
8855
8856 if (*s == ' ')
8857 ++s;
8858 if (args[1] != *s)
8859 {
8860 if (c == 'v' || c == 'w')
8861 {
8862 regno = mips16_to_32_reg_map[lastregno];
8863 s = s_reset;
8864 args++;
8865 }
8866 }
8867
8868 switch (c)
8869 {
8870 case 'x':
8871 case 'y':
8872 case 'z':
8873 case 'v':
8874 case 'w':
8875 case 'Z':
8876 regno = mips32_to_16_reg_map[regno];
8877 break;
8878
8879 case '0':
8880 if (regno != 0)
8881 regno = ILLEGAL_REG;
8882 break;
8883
8884 case 'S':
8885 if (regno != SP)
8886 regno = ILLEGAL_REG;
8887 break;
8888
8889 case 'R':
8890 if (regno != RA)
8891 regno = ILLEGAL_REG;
8892 break;
8893
8894 case 'X':
8895 case 'Y':
8896 if (regno == AT && ! mips_opts.noat)
8897 as_warn (_("used $at without \".set noat\""));
8898 break;
8899
8900 default:
8901 internalError ();
8902 }
8903
8904 if (regno == ILLEGAL_REG)
8905 break;
8906
8907 switch (c)
8908 {
8909 case 'x':
8910 case 'v':
8911 ip->insn_opcode |= regno << MIPS16OP_SH_RX;
8912 break;
8913 case 'y':
8914 case 'w':
8915 ip->insn_opcode |= regno << MIPS16OP_SH_RY;
8916 break;
8917 case 'z':
8918 ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
8919 break;
8920 case 'Z':
8921 ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
8922 case '0':
8923 case 'S':
8924 case 'R':
8925 break;
8926 case 'X':
8927 ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
8928 break;
8929 case 'Y':
8930 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
8931 ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
8932 break;
8933 default:
8934 internalError ();
8935 }
8936
8937 lastregno = regno;
8938 continue;
8939
8940 case 'P':
8941 if (strncmp (s, "$pc", 3) == 0)
8942 {
8943 s += 3;
8944 continue;
8945 }
8946 break;
8947
8948 case '<':
8949 case '>':
8950 case '[':
8951 case ']':
8952 case '4':
8953 case '5':
8954 case 'H':
8955 case 'W':
8956 case 'D':
8957 case 'j':
8958 case '8':
8959 case 'V':
8960 case 'C':
8961 case 'U':
8962 case 'k':
8963 case 'K':
8964 if (s[0] == '%'
8965 && strncmp (s + 1, "gprel(", sizeof "gprel(" - 1) == 0)
8966 {
8967 /* This is %gprel(SYMBOL). We need to read SYMBOL,
8968 and generate the appropriate reloc. If the text
8969 inside %gprel is not a symbol name with an
8970 optional offset, then we generate a normal reloc
8971 and will probably fail later. */
8972 my_getExpression (&imm_expr, s + sizeof "%gprel" - 1);
8973 if (imm_expr.X_op == O_symbol)
8974 {
8975 mips16_ext = true;
f6688943 8976 *imm_reloc = BFD_RELOC_MIPS16_GPREL;
252b5132
RH
8977 s = expr_end;
8978 ip->use_extend = true;
8979 ip->extend = 0;
8980 continue;
8981 }
8982 }
8983 else
8984 {
8985 /* Just pick up a normal expression. */
8986 my_getExpression (&imm_expr, s);
8987 }
8988
8989 if (imm_expr.X_op == O_register)
8990 {
8991 /* What we thought was an expression turned out to
8992 be a register. */
8993
8994 if (s[0] == '(' && args[1] == '(')
8995 {
8996 /* It looks like the expression was omitted
8997 before a register indirection, which means
8998 that the expression is implicitly zero. We
8999 still set up imm_expr, so that we handle
9000 explicit extensions correctly. */
9001 imm_expr.X_op = O_constant;
9002 imm_expr.X_add_number = 0;
f6688943 9003 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
9004 continue;
9005 }
9006
9007 break;
9008 }
9009
9010 /* We need to relax this instruction. */
f6688943 9011 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
9012 s = expr_end;
9013 continue;
9014
9015 case 'p':
9016 case 'q':
9017 case 'A':
9018 case 'B':
9019 case 'E':
9020 /* We use offset_reloc rather than imm_reloc for the PC
9021 relative operands. This lets macros with both
9022 immediate and address operands work correctly. */
9023 my_getExpression (&offset_expr, s);
9024
9025 if (offset_expr.X_op == O_register)
9026 break;
9027
9028 /* We need to relax this instruction. */
f6688943 9029 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
9030 s = expr_end;
9031 continue;
9032
9033 case '6': /* break code */
9034 my_getExpression (&imm_expr, s);
9035 check_absolute_expr (ip, &imm_expr);
9036 if ((unsigned long) imm_expr.X_add_number > 63)
9037 {
9038 as_warn (_("Invalid value for `%s' (%lu)"),
9039 ip->insn_mo->name,
9040 (unsigned long) imm_expr.X_add_number);
9041 imm_expr.X_add_number &= 0x3f;
9042 }
9043 ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
9044 imm_expr.X_op = O_absent;
9045 s = expr_end;
9046 continue;
9047
9048 case 'a': /* 26 bit address */
9049 my_getExpression (&offset_expr, s);
9050 s = expr_end;
f6688943 9051 *offset_reloc = BFD_RELOC_MIPS16_JMP;
252b5132
RH
9052 ip->insn_opcode <<= 16;
9053 continue;
9054
9055 case 'l': /* register list for entry macro */
9056 case 'L': /* register list for exit macro */
9057 {
9058 int mask;
9059
9060 if (c == 'l')
9061 mask = 0;
9062 else
9063 mask = 7 << 3;
9064 while (*s != '\0')
9065 {
9066 int freg, reg1, reg2;
9067
9068 while (*s == ' ' || *s == ',')
9069 ++s;
9070 if (*s != '$')
9071 {
9072 as_bad (_("can't parse register list"));
9073 break;
9074 }
9075 ++s;
9076 if (*s != 'f')
9077 freg = 0;
9078 else
9079 {
9080 freg = 1;
9081 ++s;
9082 }
9083 reg1 = 0;
3882b010 9084 while (ISDIGIT (*s))
252b5132
RH
9085 {
9086 reg1 *= 10;
9087 reg1 += *s - '0';
9088 ++s;
9089 }
9090 if (*s == ' ')
9091 ++s;
9092 if (*s != '-')
9093 reg2 = reg1;
9094 else
9095 {
9096 ++s;
9097 if (*s != '$')
9098 break;
9099 ++s;
9100 if (freg)
9101 {
9102 if (*s == 'f')
9103 ++s;
9104 else
9105 {
9106 as_bad (_("invalid register list"));
9107 break;
9108 }
9109 }
9110 reg2 = 0;
3882b010 9111 while (ISDIGIT (*s))
252b5132
RH
9112 {
9113 reg2 *= 10;
9114 reg2 += *s - '0';
9115 ++s;
9116 }
9117 }
9118 if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
9119 {
9120 mask &= ~ (7 << 3);
9121 mask |= 5 << 3;
9122 }
9123 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
9124 {
9125 mask &= ~ (7 << 3);
9126 mask |= 6 << 3;
9127 }
9128 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
9129 mask |= (reg2 - 3) << 3;
9130 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
9131 mask |= (reg2 - 15) << 1;
9132 else if (reg1 == 31 && reg2 == 31)
9133 mask |= 1;
9134 else
9135 {
9136 as_bad (_("invalid register list"));
9137 break;
9138 }
9139 }
9140 /* The mask is filled in in the opcode table for the
9141 benefit of the disassembler. We remove it before
9142 applying the actual mask. */
9143 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
9144 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
9145 }
9146 continue;
9147
9148 case 'e': /* extend code */
9149 my_getExpression (&imm_expr, s);
9150 check_absolute_expr (ip, &imm_expr);
9151 if ((unsigned long) imm_expr.X_add_number > 0x7ff)
9152 {
9153 as_warn (_("Invalid value for `%s' (%lu)"),
9154 ip->insn_mo->name,
9155 (unsigned long) imm_expr.X_add_number);
9156 imm_expr.X_add_number &= 0x7ff;
9157 }
9158 ip->insn_opcode |= imm_expr.X_add_number;
9159 imm_expr.X_op = O_absent;
9160 s = expr_end;
9161 continue;
9162
9163 default:
9164 internalError ();
9165 }
9166 break;
9167 }
9168
9169 /* Args don't match. */
9170 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
9171 strcmp (insn->name, insn[1].name) == 0)
9172 {
9173 ++insn;
9174 s = argsstart;
9175 continue;
9176 }
9177
9178 insn_error = _("illegal operands");
9179
9180 return;
9181 }
9182}
9183
9184/* This structure holds information we know about a mips16 immediate
9185 argument type. */
9186
e972090a
NC
9187struct mips16_immed_operand
9188{
252b5132
RH
9189 /* The type code used in the argument string in the opcode table. */
9190 int type;
9191 /* The number of bits in the short form of the opcode. */
9192 int nbits;
9193 /* The number of bits in the extended form of the opcode. */
9194 int extbits;
9195 /* The amount by which the short form is shifted when it is used;
9196 for example, the sw instruction has a shift count of 2. */
9197 int shift;
9198 /* The amount by which the short form is shifted when it is stored
9199 into the instruction code. */
9200 int op_shift;
9201 /* Non-zero if the short form is unsigned. */
9202 int unsp;
9203 /* Non-zero if the extended form is unsigned. */
9204 int extu;
9205 /* Non-zero if the value is PC relative. */
9206 int pcrel;
9207};
9208
9209/* The mips16 immediate operand types. */
9210
9211static const struct mips16_immed_operand mips16_immed_operands[] =
9212{
9213 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9214 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9215 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9216 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9217 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
9218 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
9219 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
9220 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
9221 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
9222 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
9223 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
9224 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
9225 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
9226 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
9227 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
9228 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
9229 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9230 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9231 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
9232 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
9233 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
9234};
9235
9236#define MIPS16_NUM_IMMED \
9237 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
9238
9239/* Handle a mips16 instruction with an immediate value. This or's the
9240 small immediate value into *INSN. It sets *USE_EXTEND to indicate
9241 whether an extended value is needed; if one is needed, it sets
9242 *EXTEND to the value. The argument type is TYPE. The value is VAL.
9243 If SMALL is true, an unextended opcode was explicitly requested.
9244 If EXT is true, an extended opcode was explicitly requested. If
9245 WARN is true, warn if EXT does not match reality. */
9246
9247static void
9248mips16_immed (file, line, type, val, warn, small, ext, insn, use_extend,
9249 extend)
9250 char *file;
9251 unsigned int line;
9252 int type;
9253 offsetT val;
9254 boolean warn;
9255 boolean small;
9256 boolean ext;
9257 unsigned long *insn;
9258 boolean *use_extend;
9259 unsigned short *extend;
9260{
9261 register const struct mips16_immed_operand *op;
9262 int mintiny, maxtiny;
9263 boolean needext;
9264
9265 op = mips16_immed_operands;
9266 while (op->type != type)
9267 {
9268 ++op;
9269 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
9270 }
9271
9272 if (op->unsp)
9273 {
9274 if (type == '<' || type == '>' || type == '[' || type == ']')
9275 {
9276 mintiny = 1;
9277 maxtiny = 1 << op->nbits;
9278 }
9279 else
9280 {
9281 mintiny = 0;
9282 maxtiny = (1 << op->nbits) - 1;
9283 }
9284 }
9285 else
9286 {
9287 mintiny = - (1 << (op->nbits - 1));
9288 maxtiny = (1 << (op->nbits - 1)) - 1;
9289 }
9290
9291 /* Branch offsets have an implicit 0 in the lowest bit. */
9292 if (type == 'p' || type == 'q')
9293 val /= 2;
9294
9295 if ((val & ((1 << op->shift) - 1)) != 0
9296 || val < (mintiny << op->shift)
9297 || val > (maxtiny << op->shift))
9298 needext = true;
9299 else
9300 needext = false;
9301
9302 if (warn && ext && ! needext)
beae10d5
KH
9303 as_warn_where (file, line,
9304 _("extended operand requested but not required"));
252b5132
RH
9305 if (small && needext)
9306 as_bad_where (file, line, _("invalid unextended operand value"));
9307
9308 if (small || (! ext && ! needext))
9309 {
9310 int insnval;
9311
9312 *use_extend = false;
9313 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
9314 insnval <<= op->op_shift;
9315 *insn |= insnval;
9316 }
9317 else
9318 {
9319 long minext, maxext;
9320 int extval;
9321
9322 if (op->extu)
9323 {
9324 minext = 0;
9325 maxext = (1 << op->extbits) - 1;
9326 }
9327 else
9328 {
9329 minext = - (1 << (op->extbits - 1));
9330 maxext = (1 << (op->extbits - 1)) - 1;
9331 }
9332 if (val < minext || val > maxext)
9333 as_bad_where (file, line,
9334 _("operand value out of range for instruction"));
9335
9336 *use_extend = true;
9337 if (op->extbits == 16)
9338 {
9339 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
9340 val &= 0x1f;
9341 }
9342 else if (op->extbits == 15)
9343 {
9344 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
9345 val &= 0xf;
9346 }
9347 else
9348 {
9349 extval = ((val & 0x1f) << 6) | (val & 0x20);
9350 val = 0;
9351 }
9352
9353 *extend = (unsigned short) extval;
9354 *insn |= val;
9355 }
9356}
9357\f
ad8d3bb3
TS
9358static struct percent_op_match
9359{
9360 const char *str;
9361 const enum small_ex_type type;
9362} percent_op[] =
9363{
ad8d3bb3
TS
9364 {"%lo", S_EX_LO},
9365#ifdef OBJ_ELF
394f9b3a
TS
9366 {"%call_hi", S_EX_CALL_HI},
9367 {"%call_lo", S_EX_CALL_LO},
ad8d3bb3
TS
9368 {"%call16", S_EX_CALL16},
9369 {"%got_disp", S_EX_GOT_DISP},
9370 {"%got_page", S_EX_GOT_PAGE},
9371 {"%got_ofst", S_EX_GOT_OFST},
9372 {"%got_hi", S_EX_GOT_HI},
9373 {"%got_lo", S_EX_GOT_LO},
394f9b3a
TS
9374 {"%got", S_EX_GOT},
9375 {"%gp_rel", S_EX_GP_REL},
9376 {"%half", S_EX_HALF},
ad8d3bb3 9377 {"%highest", S_EX_HIGHEST},
394f9b3a
TS
9378 {"%higher", S_EX_HIGHER},
9379 {"%neg", S_EX_NEG},
ad8d3bb3 9380#endif
394f9b3a 9381 {"%hi", S_EX_HI}
ad8d3bb3
TS
9382};
9383
9384/* Parse small expression input. STR gets adjusted to eat up whitespace.
9385 It detects valid "%percent_op(...)" and "($reg)" strings. Percent_op's
9386 can be nested, this is handled by blanking the innermost, parsing the
9387 rest by subsequent calls. */
252b5132
RH
9388
9389static int
ad8d3bb3
TS
9390my_getSmallParser (str, len, nestlevel)
9391 char **str;
9392 unsigned int *len;
9393 int *nestlevel;
252b5132 9394{
ad8d3bb3
TS
9395 *len = 0;
9396 *str += strspn (*str, " \t");
394f9b3a 9397 /* Check for expression in parentheses. */
ad8d3bb3 9398 if (**str == '(')
252b5132 9399 {
ad8d3bb3
TS
9400 char *b = *str + 1 + strspn (*str + 1, " \t");
9401 char *e;
9402
9403 /* Check for base register. */
9404 if (b[0] == '$')
9405 {
9406 if (strchr (b, ')')
9407 && (e = b + strcspn (b, ") \t"))
9408 && e - b > 1 && e - b < 4)
9409 {
9410 if ((e - b == 3
9411 && ((b[1] == 'f' && b[2] == 'p')
9412 || (b[1] == 's' && b[2] == 'p')
9413 || (b[1] == 'g' && b[2] == 'p')
9414 || (b[1] == 'a' && b[2] == 't')
9415 || (ISDIGIT (b[1])
9416 && ISDIGIT (b[2]))))
9417 || (ISDIGIT (b[1])))
9418 {
9419 *len = strcspn (*str, ")") + 1;
9420 return S_EX_REGISTER;
9421 }
9422 }
9423 }
394f9b3a 9424 /* Check for percent_op (in parentheses). */
ad8d3bb3
TS
9425 else if (b[0] == '%')
9426 {
9427 *str = b;
394f9b3a 9428 return my_getPercentOp (str, len, nestlevel);
ad8d3bb3 9429 }
76b3015f 9430
394f9b3a
TS
9431 /* Some other expression in the parentheses, which can contain
9432 parentheses itself. Attempt to find the matching one. */
9433 {
9434 int pcnt = 1;
9435 char *s;
9436
9437 *len = 1;
9438 for (s = *str + 1; *s && pcnt; s++, (*len)++)
9439 {
9440 if (*s == '(')
9441 pcnt++;
9442 else if (*s == ')')
9443 pcnt--;
9444 }
9445 }
fb1b3232 9446 }
394f9b3a 9447 /* Check for percent_op (outside of parentheses). */
ad8d3bb3 9448 else if (*str[0] == '%')
394f9b3a
TS
9449 return my_getPercentOp (str, len, nestlevel);
9450
9451 /* Any other expression. */
9452 return S_EX_NONE;
9453}
ad8d3bb3 9454
394f9b3a
TS
9455static int
9456my_getPercentOp (str, len, nestlevel)
9457 char **str;
9458 unsigned int *len;
9459 int *nestlevel;
9460{
9461 char *tmp = *str + 1;
9462 unsigned int i = 0;
ad8d3bb3 9463
394f9b3a
TS
9464 while (ISALPHA (*tmp) || *tmp == '_')
9465 {
9466 *tmp = TOLOWER (*tmp);
9467 tmp++;
9468 }
9469 while (i < (sizeof (percent_op) / sizeof (struct percent_op_match)))
9470 {
9471 if (strncmp (*str, percent_op[i].str, strlen (percent_op[i].str)))
9472 i++;
9473 else
ad8d3bb3 9474 {
394f9b3a 9475 int type = percent_op[i].type;
ad8d3bb3 9476
394f9b3a
TS
9477 /* Only %hi and %lo are allowed for OldABI. */
9478 if (! HAVE_NEWABI && type != S_EX_HI && type != S_EX_LO)
9479 return S_EX_NONE;
ad8d3bb3 9480
394f9b3a
TS
9481 *len = strlen (percent_op[i].str);
9482 (*nestlevel)++;
9483 return type;
ad8d3bb3 9484 }
fb1b3232 9485 }
ad8d3bb3
TS
9486 return S_EX_NONE;
9487}
9488
9489static int
9490my_getSmallExpression (ep, str)
9491 expressionS *ep;
9492 char *str;
9493{
9494 static char *oldstr = NULL;
9495 int c = S_EX_NONE;
9496 int oldc;
394f9b3a 9497 int nestlevel = -1;
ad8d3bb3
TS
9498 unsigned int len;
9499
394f9b3a
TS
9500 /* Don't update oldstr if the last call had nested percent_op's. We need
9501 it to parse the outer ones later. */
ad8d3bb3
TS
9502 if (! oldstr)
9503 oldstr = str;
76b3015f 9504
ad8d3bb3 9505 do
fb1b3232 9506 {
ad8d3bb3 9507 oldc = c;
394f9b3a 9508 c = my_getSmallParser (&str, &len, &nestlevel);
ad8d3bb3
TS
9509 if (c != S_EX_NONE && c != S_EX_REGISTER)
9510 str += len;
fb1b3232 9511 }
ad8d3bb3
TS
9512 while (c != S_EX_NONE && c != S_EX_REGISTER);
9513
394f9b3a 9514 if (nestlevel >= 0)
fb1b3232 9515 {
394f9b3a
TS
9516 /* A percent_op was encountered. Don't try to get an expression if
9517 it is already blanked out. */
ad8d3bb3
TS
9518 if (*(str + strspn (str + 1, " )")) != ')')
9519 {
9520 char save;
9521
394f9b3a 9522 /* Let my_getExpression() stop at the closing parenthesis. */
ad8d3bb3
TS
9523 save = *(str + len);
9524 *(str + len) = '\0';
9525 my_getExpression (ep, str);
9526 *(str + len) = save;
9527 }
394f9b3a 9528 if (nestlevel > 0)
ad8d3bb3 9529 {
394f9b3a
TS
9530 /* Blank out including the % sign and the proper matching
9531 parenthesis. */
9532 int pcnt = 1;
9533 char *s = strrchr (oldstr, '%');
9534 char *end;
9535
9536 for (end = strchr (s, '(') + 1; *end && pcnt; end++)
9537 {
9538 if (*end == '(')
9539 pcnt++;
9540 else if (*end == ')')
9541 pcnt--;
9542 }
9543
9544 memset (s, ' ', end - s);
ad8d3bb3
TS
9545 str = oldstr;
9546 }
9547 else
394f9b3a
TS
9548 expr_end = str + len;
9549
ad8d3bb3 9550 c = oldc;
fb1b3232 9551 }
ad8d3bb3 9552 else if (c == S_EX_NONE)
fb1b3232 9553 {
ad8d3bb3 9554 my_getExpression (ep, str);
fb1b3232 9555 }
ad8d3bb3 9556 else if (c == S_EX_REGISTER)
fb1b3232 9557 {
ad8d3bb3
TS
9558 ep->X_op = O_constant;
9559 expr_end = str;
9560 ep->X_add_symbol = NULL;
9561 ep->X_op_symbol = NULL;
9562 ep->X_add_number = 0;
fb1b3232 9563 }
fb1b3232
TS
9564 else
9565 {
ad8d3bb3 9566 as_fatal(_("internal error"));
fb1b3232 9567 }
252b5132 9568
394f9b3a
TS
9569 if (nestlevel <= 0)
9570 /* All percent_op's have been handled. */
ad8d3bb3 9571 oldstr = NULL;
fb1b3232 9572
fb1b3232 9573 return c;
252b5132
RH
9574}
9575
9576static void
9577my_getExpression (ep, str)
9578 expressionS *ep;
9579 char *str;
9580{
9581 char *save_in;
98aa84af 9582 valueT val;
252b5132
RH
9583
9584 save_in = input_line_pointer;
9585 input_line_pointer = str;
9586 expression (ep);
9587 expr_end = input_line_pointer;
9588 input_line_pointer = save_in;
9589
9590 /* If we are in mips16 mode, and this is an expression based on `.',
9591 then we bump the value of the symbol by 1 since that is how other
9592 text symbols are handled. We don't bother to handle complex
9593 expressions, just `.' plus or minus a constant. */
9594 if (mips_opts.mips16
9595 && ep->X_op == O_symbol
9596 && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
9597 && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
49309057
ILT
9598 && symbol_get_frag (ep->X_add_symbol) == frag_now
9599 && symbol_constant_p (ep->X_add_symbol)
98aa84af
AM
9600 && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
9601 S_SET_VALUE (ep->X_add_symbol, val + 1);
252b5132
RH
9602}
9603
9604/* Turn a string in input_line_pointer into a floating point constant
bc0d738a
NC
9605 of type TYPE, and store the appropriate bytes in *LITP. The number
9606 of LITTLENUMS emitted is stored in *SIZEP. An error message is
252b5132
RH
9607 returned, or NULL on OK. */
9608
9609char *
9610md_atof (type, litP, sizeP)
9611 int type;
9612 char *litP;
9613 int *sizeP;
9614{
9615 int prec;
9616 LITTLENUM_TYPE words[4];
9617 char *t;
9618 int i;
9619
9620 switch (type)
9621 {
9622 case 'f':
9623 prec = 2;
9624 break;
9625
9626 case 'd':
9627 prec = 4;
9628 break;
9629
9630 default:
9631 *sizeP = 0;
9632 return _("bad call to md_atof");
9633 }
9634
9635 t = atof_ieee (input_line_pointer, type, words);
9636 if (t)
9637 input_line_pointer = t;
9638
9639 *sizeP = prec * 2;
9640
9641 if (! target_big_endian)
9642 {
9643 for (i = prec - 1; i >= 0; i--)
9644 {
9645 md_number_to_chars (litP, (valueT) words[i], 2);
9646 litP += 2;
9647 }
9648 }
9649 else
9650 {
9651 for (i = 0; i < prec; i++)
9652 {
9653 md_number_to_chars (litP, (valueT) words[i], 2);
9654 litP += 2;
9655 }
9656 }
bdaaa2e1 9657
252b5132
RH
9658 return NULL;
9659}
9660
9661void
9662md_number_to_chars (buf, val, n)
9663 char *buf;
9664 valueT val;
9665 int n;
9666{
9667 if (target_big_endian)
9668 number_to_chars_bigendian (buf, val, n);
9669 else
9670 number_to_chars_littleendian (buf, val, n);
9671}
9672\f
ae948b86 9673#ifdef OBJ_ELF
e013f690
TS
9674static int support_64bit_objects(void)
9675{
9676 const char **list, **l;
9677
9678 list = bfd_target_list ();
9679 for (l = list; *l != NULL; l++)
9680#ifdef TE_TMIPS
9681 /* This is traditional mips */
9682 if (strcmp (*l, "elf64-tradbigmips") == 0
9683 || strcmp (*l, "elf64-tradlittlemips") == 0)
9684#else
9685 if (strcmp (*l, "elf64-bigmips") == 0
9686 || strcmp (*l, "elf64-littlemips") == 0)
9687#endif
9688 break;
9689 free (list);
9690 return (*l != NULL);
9691}
ae948b86 9692#endif /* OBJ_ELF */
e013f690 9693
39c0a331 9694CONST char *md_shortopts = "nO::g::G:";
252b5132 9695
e972090a
NC
9696struct option md_longopts[] =
9697{
252b5132
RH
9698#define OPTION_MIPS1 (OPTION_MD_BASE + 1)
9699 {"mips0", no_argument, NULL, OPTION_MIPS1},
9700 {"mips1", no_argument, NULL, OPTION_MIPS1},
9701#define OPTION_MIPS2 (OPTION_MD_BASE + 2)
9702 {"mips2", no_argument, NULL, OPTION_MIPS2},
9703#define OPTION_MIPS3 (OPTION_MD_BASE + 3)
9704 {"mips3", no_argument, NULL, OPTION_MIPS3},
9705#define OPTION_MIPS4 (OPTION_MD_BASE + 4)
9706 {"mips4", no_argument, NULL, OPTION_MIPS4},
ae948b86
TS
9707#define OPTION_MIPS5 (OPTION_MD_BASE + 5)
9708 {"mips5", no_argument, NULL, OPTION_MIPS5},
9709#define OPTION_MIPS32 (OPTION_MD_BASE + 6)
9710 {"mips32", no_argument, NULL, OPTION_MIPS32},
9711#define OPTION_MIPS64 (OPTION_MD_BASE + 7)
9712 {"mips64", no_argument, NULL, OPTION_MIPS64},
9713#define OPTION_MEMBEDDED_PIC (OPTION_MD_BASE + 8)
252b5132 9714 {"membedded-pic", no_argument, NULL, OPTION_MEMBEDDED_PIC},
ae948b86 9715#define OPTION_TRAP (OPTION_MD_BASE + 9)
252b5132
RH
9716 {"trap", no_argument, NULL, OPTION_TRAP},
9717 {"no-break", no_argument, NULL, OPTION_TRAP},
ae948b86 9718#define OPTION_BREAK (OPTION_MD_BASE + 10)
252b5132
RH
9719 {"break", no_argument, NULL, OPTION_BREAK},
9720 {"no-trap", no_argument, NULL, OPTION_BREAK},
ae948b86 9721#define OPTION_EB (OPTION_MD_BASE + 11)
252b5132 9722 {"EB", no_argument, NULL, OPTION_EB},
ae948b86 9723#define OPTION_EL (OPTION_MD_BASE + 12)
252b5132 9724 {"EL", no_argument, NULL, OPTION_EL},
ae948b86 9725#define OPTION_MIPS16 (OPTION_MD_BASE + 13)
252b5132 9726 {"mips16", no_argument, NULL, OPTION_MIPS16},
ae948b86 9727#define OPTION_NO_MIPS16 (OPTION_MD_BASE + 14)
252b5132 9728 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
ae948b86 9729#define OPTION_M7000_HILO_FIX (OPTION_MD_BASE + 15)
6b76fefe 9730 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
ae948b86 9731#define OPTION_NO_M7000_HILO_FIX (OPTION_MD_BASE + 16)
6b76fefe 9732 {"no-fix-7000", no_argument, NULL, OPTION_NO_M7000_HILO_FIX},
ae948b86
TS
9733#define OPTION_FP32 (OPTION_MD_BASE + 17)
9734 {"mfp32", no_argument, NULL, OPTION_FP32},
9735#define OPTION_GP32 (OPTION_MD_BASE + 18)
c97ef257 9736 {"mgp32", no_argument, NULL, OPTION_GP32},
ae948b86 9737#define OPTION_CONSTRUCT_FLOATS (OPTION_MD_BASE + 19)
119d663a 9738 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
ae948b86 9739#define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MD_BASE + 20)
119d663a 9740 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
ae948b86 9741#define OPTION_MARCH (OPTION_MD_BASE + 21)
ec68c924 9742 {"march", required_argument, NULL, OPTION_MARCH},
ae948b86 9743#define OPTION_MTUNE (OPTION_MD_BASE + 22)
ec68c924 9744 {"mtune", required_argument, NULL, OPTION_MTUNE},
ae948b86
TS
9745#define OPTION_MCPU (OPTION_MD_BASE + 23)
9746 {"mcpu", required_argument, NULL, OPTION_MCPU},
9747#define OPTION_M4650 (OPTION_MD_BASE + 24)
9748 {"m4650", no_argument, NULL, OPTION_M4650},
9749#define OPTION_NO_M4650 (OPTION_MD_BASE + 25)
9750 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
9751#define OPTION_M4010 (OPTION_MD_BASE + 26)
9752 {"m4010", no_argument, NULL, OPTION_M4010},
9753#define OPTION_NO_M4010 (OPTION_MD_BASE + 27)
9754 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
9755#define OPTION_M4100 (OPTION_MD_BASE + 28)
9756 {"m4100", no_argument, NULL, OPTION_M4100},
9757#define OPTION_NO_M4100 (OPTION_MD_BASE + 29)
9758 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
9759#define OPTION_M3900 (OPTION_MD_BASE + 30)
9760 {"m3900", no_argument, NULL, OPTION_M3900},
9761#define OPTION_NO_M3900 (OPTION_MD_BASE + 31)
9762 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
9763#define OPTION_GP64 (OPTION_MD_BASE + 32)
9764 {"mgp64", no_argument, NULL, OPTION_GP64},
1f25f5d3
CD
9765#define OPTION_MIPS3D (OPTION_MD_BASE + 33)
9766 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
9767#define OPTION_NO_MIPS3D (OPTION_MD_BASE + 34)
9768 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
156c2f8b 9769#ifdef OBJ_ELF
1f25f5d3 9770#define OPTION_ELF_BASE (OPTION_MD_BASE + 35)
156c2f8b 9771#define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
156c2f8b
NC
9772 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
9773 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
ae948b86 9774#define OPTION_NON_SHARED (OPTION_ELF_BASE + 1)
156c2f8b 9775 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
ae948b86 9776#define OPTION_XGOT (OPTION_ELF_BASE + 2)
156c2f8b 9777 {"xgot", no_argument, NULL, OPTION_XGOT},
ae948b86
TS
9778#define OPTION_MABI (OPTION_ELF_BASE + 3)
9779 {"mabi", required_argument, NULL, OPTION_MABI},
9780#define OPTION_32 (OPTION_ELF_BASE + 4)
156c2f8b 9781 {"32", no_argument, NULL, OPTION_32},
ae948b86 9782#define OPTION_N32 (OPTION_ELF_BASE + 5)
e013f690 9783 {"n32", no_argument, NULL, OPTION_N32},
ae948b86 9784#define OPTION_64 (OPTION_ELF_BASE + 6)
156c2f8b 9785 {"64", no_argument, NULL, OPTION_64},
ae948b86 9786#endif /* OBJ_ELF */
252b5132
RH
9787 {NULL, no_argument, NULL, 0}
9788};
156c2f8b 9789size_t md_longopts_size = sizeof (md_longopts);
252b5132
RH
9790
9791int
9792md_parse_option (c, arg)
9793 int c;
9794 char *arg;
9795{
9796 switch (c)
9797 {
119d663a
NC
9798 case OPTION_CONSTRUCT_FLOATS:
9799 mips_disable_float_construction = 0;
9800 break;
bdaaa2e1 9801
119d663a
NC
9802 case OPTION_NO_CONSTRUCT_FLOATS:
9803 mips_disable_float_construction = 1;
9804 break;
bdaaa2e1 9805
252b5132
RH
9806 case OPTION_TRAP:
9807 mips_trap = 1;
9808 break;
9809
9810 case OPTION_BREAK:
9811 mips_trap = 0;
9812 break;
9813
9814 case OPTION_EB:
9815 target_big_endian = 1;
9816 break;
9817
9818 case OPTION_EL:
9819 target_big_endian = 0;
9820 break;
9821
39c0a331
L
9822 case 'n':
9823 warn_nops = 1;
9824 break;
9825
252b5132
RH
9826 case 'O':
9827 if (arg && arg[1] == '0')
9828 mips_optimize = 1;
9829 else
9830 mips_optimize = 2;
9831 break;
9832
9833 case 'g':
9834 if (arg == NULL)
9835 mips_debug = 2;
9836 else
9837 mips_debug = atoi (arg);
9838 /* When the MIPS assembler sees -g or -g2, it does not do
9839 optimizations which limit full symbolic debugging. We take
9840 that to be equivalent to -O0. */
9841 if (mips_debug == 2)
9842 mips_optimize = 1;
9843 break;
9844
9845 case OPTION_MIPS1:
e7af610e 9846 mips_opts.isa = ISA_MIPS1;
252b5132
RH
9847 break;
9848
9849 case OPTION_MIPS2:
e7af610e 9850 mips_opts.isa = ISA_MIPS2;
252b5132
RH
9851 break;
9852
9853 case OPTION_MIPS3:
e7af610e 9854 mips_opts.isa = ISA_MIPS3;
252b5132
RH
9855 break;
9856
9857 case OPTION_MIPS4:
e7af610e
NC
9858 mips_opts.isa = ISA_MIPS4;
9859 break;
9860
84ea6cf2
NC
9861 case OPTION_MIPS5:
9862 mips_opts.isa = ISA_MIPS5;
9863 break;
9864
e7af610e
NC
9865 case OPTION_MIPS32:
9866 mips_opts.isa = ISA_MIPS32;
252b5132
RH
9867 break;
9868
84ea6cf2
NC
9869 case OPTION_MIPS64:
9870 mips_opts.isa = ISA_MIPS64;
9871 break;
9872
ec68c924
EC
9873 case OPTION_MTUNE:
9874 case OPTION_MARCH:
252b5132
RH
9875 case OPTION_MCPU:
9876 {
ec68c924
EC
9877 int cpu = CPU_UNKNOWN;
9878
e7af610e 9879 /* Identify the processor type. */
ec68c924 9880 if (strcasecmp (arg, "default") != 0)
252b5132 9881 {
e7af610e 9882 const struct mips_cpu_info *ci;
252b5132 9883
e7af610e
NC
9884 ci = mips_cpu_info_from_name (arg);
9885 if (ci == NULL || ci->is_isa)
ec68c924
EC
9886 {
9887 switch (c)
9888 {
9889 case OPTION_MTUNE:
9890 as_fatal (_("invalid architecture -mtune=%s"), arg);
9891 break;
9892 case OPTION_MARCH:
9893 as_fatal (_("invalid architecture -march=%s"), arg);
9894 break;
9895 case OPTION_MCPU:
9896 as_fatal (_("invalid architecture -mcpu=%s"), arg);
9897 break;
9898 }
9899 }
e7af610e 9900 else
ec68c924
EC
9901 cpu = ci->cpu;
9902 }
9903
9904 switch (c)
9905 {
9906 case OPTION_MTUNE:
6dce9e24
TS
9907 if (mips_tune != CPU_UNKNOWN && mips_tune != cpu)
9908 as_warn(_("A different -mtune= was already specified, is now "
9909 "-mtune=%s"), arg);
ec68c924
EC
9910 mips_tune = cpu;
9911 break;
9912 case OPTION_MARCH:
6dce9e24
TS
9913 if (mips_arch != CPU_UNKNOWN && mips_arch != cpu)
9914 as_warn(_("A different -march= was already specified, is now "
9915 "-march=%s"), arg);
ec68c924
EC
9916 mips_arch = cpu;
9917 break;
9918 case OPTION_MCPU:
6dce9e24
TS
9919 if (mips_cpu != CPU_UNKNOWN && mips_cpu != cpu)
9920 as_warn(_("A different -mcpu= was already specified, is now "
9921 "-mcpu=%s"), arg);
ec68c924 9922 mips_cpu = cpu;
252b5132
RH
9923 }
9924 }
9925 break;
9926
9927 case OPTION_M4650:
6dce9e24
TS
9928 if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_R4650)
9929 || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_R4650))
9930 as_warn(_("A different -march= or -mtune= was already specified, "
9931 "is now -m4650"));
ec68c924
EC
9932 mips_arch = CPU_R4650;
9933 mips_tune = CPU_R4650;
252b5132
RH
9934 break;
9935
9936 case OPTION_NO_M4650:
9937 break;
9938
9939 case OPTION_M4010:
6dce9e24
TS
9940 if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_R4010)
9941 || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_R4010))
9942 as_warn(_("A different -march= or -mtune= was already specified, "
9943 "is now -m4010"));
ec68c924
EC
9944 mips_arch = CPU_R4010;
9945 mips_tune = CPU_R4010;
252b5132
RH
9946 break;
9947
9948 case OPTION_NO_M4010:
9949 break;
9950
9951 case OPTION_M4100:
6dce9e24
TS
9952 if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_VR4100)
9953 || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_VR4100))
9954 as_warn(_("A different -march= or -mtune= was already specified, "
9955 "is now -m4100"));
ec68c924
EC
9956 mips_arch = CPU_VR4100;
9957 mips_tune = CPU_VR4100;
252b5132
RH
9958 break;
9959
9960 case OPTION_NO_M4100:
9961 break;
9962
252b5132 9963 case OPTION_M3900:
6dce9e24
TS
9964 if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_R3900)
9965 || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_R3900))
9966 as_warn(_("A different -march= or -mtune= was already specified, "
9967 "is now -m3900"));
ec68c924
EC
9968 mips_arch = CPU_R3900;
9969 mips_tune = CPU_R3900;
252b5132 9970 break;
bdaaa2e1 9971
252b5132
RH
9972 case OPTION_NO_M3900:
9973 break;
9974
9975 case OPTION_MIPS16:
9976 mips_opts.mips16 = 1;
9977 mips_no_prev_insn (false);
9978 break;
9979
9980 case OPTION_NO_MIPS16:
9981 mips_opts.mips16 = 0;
9982 mips_no_prev_insn (false);
9983 break;
9984
1f25f5d3
CD
9985 case OPTION_MIPS3D:
9986 mips_opts.ase_mips3d = 1;
9987 break;
9988
9989 case OPTION_NO_MIPS3D:
9990 mips_opts.ase_mips3d = 0;
9991 break;
9992
252b5132
RH
9993 case OPTION_MEMBEDDED_PIC:
9994 mips_pic = EMBEDDED_PIC;
9995 if (USE_GLOBAL_POINTER_OPT && g_switch_seen)
9996 {
9997 as_bad (_("-G may not be used with embedded PIC code"));
9998 return 0;
9999 }
10000 g_switch_value = 0x7fffffff;
10001 break;
10002
0f074f60 10003#ifdef OBJ_ELF
252b5132
RH
10004 /* When generating ELF code, we permit -KPIC and -call_shared to
10005 select SVR4_PIC, and -non_shared to select no PIC. This is
10006 intended to be compatible with Irix 5. */
10007 case OPTION_CALL_SHARED:
10008 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10009 {
10010 as_bad (_("-call_shared is supported only for ELF format"));
10011 return 0;
10012 }
10013 mips_pic = SVR4_PIC;
10014 if (g_switch_seen && g_switch_value != 0)
10015 {
10016 as_bad (_("-G may not be used with SVR4 PIC code"));
10017 return 0;
10018 }
10019 g_switch_value = 0;
10020 break;
10021
10022 case OPTION_NON_SHARED:
10023 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10024 {
10025 as_bad (_("-non_shared is supported only for ELF format"));
10026 return 0;
10027 }
10028 mips_pic = NO_PIC;
10029 break;
10030
10031 /* The -xgot option tells the assembler to use 32 offsets when
10032 accessing the got in SVR4_PIC mode. It is for Irix
10033 compatibility. */
10034 case OPTION_XGOT:
10035 mips_big_got = 1;
10036 break;
0f074f60 10037#endif /* OBJ_ELF */
252b5132
RH
10038
10039 case 'G':
10040 if (! USE_GLOBAL_POINTER_OPT)
10041 {
10042 as_bad (_("-G is not supported for this configuration"));
10043 return 0;
10044 }
10045 else if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
10046 {
10047 as_bad (_("-G may not be used with SVR4 or embedded PIC code"));
10048 return 0;
10049 }
10050 else
10051 g_switch_value = atoi (arg);
10052 g_switch_seen = 1;
10053 break;
10054
0f074f60 10055#ifdef OBJ_ELF
34ba82a8
TS
10056 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
10057 and -mabi=64. */
252b5132 10058 case OPTION_32:
34ba82a8
TS
10059 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10060 {
10061 as_bad (_("-32 is supported for ELF format only"));
10062 return 0;
10063 }
a325df1d 10064 mips_opts.abi = O32_ABI;
252b5132
RH
10065 break;
10066
e013f690 10067 case OPTION_N32:
34ba82a8
TS
10068 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10069 {
10070 as_bad (_("-n32 is supported for ELF format only"));
10071 return 0;
10072 }
a325df1d 10073 mips_opts.abi = N32_ABI;
e013f690 10074 break;
252b5132 10075
e013f690 10076 case OPTION_64:
34ba82a8
TS
10077 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10078 {
10079 as_bad (_("-64 is supported for ELF format only"));
10080 return 0;
10081 }
a325df1d 10082 mips_opts.abi = N64_ABI;
e013f690
TS
10083 if (! support_64bit_objects())
10084 as_fatal (_("No compiled in support for 64 bit object file format"));
252b5132 10085 break;
ae948b86 10086#endif /* OBJ_ELF */
252b5132 10087
c97ef257 10088 case OPTION_GP32:
a325df1d
TS
10089 file_mips_gp32 = 1;
10090 if (mips_opts.abi != O32_ABI)
10091 mips_opts.abi = NO_ABI;
c97ef257
AH
10092 break;
10093
10094 case OPTION_GP64:
a325df1d
TS
10095 file_mips_gp32 = 0;
10096 if (mips_opts.abi == O32_ABI)
10097 mips_opts.abi = NO_ABI;
c97ef257 10098 break;
252b5132 10099
ca4e0257 10100 case OPTION_FP32:
a325df1d
TS
10101 file_mips_fp32 = 1;
10102 if (mips_opts.abi != O32_ABI)
10103 mips_opts.abi = NO_ABI;
ca4e0257
RS
10104 break;
10105
ae948b86 10106#ifdef OBJ_ELF
252b5132 10107 case OPTION_MABI:
34ba82a8
TS
10108 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10109 {
10110 as_bad (_("-mabi is supported for ELF format only"));
10111 return 0;
10112 }
e013f690 10113 if (strcmp (arg, "32") == 0)
a325df1d 10114 mips_opts.abi = O32_ABI;
e013f690 10115 else if (strcmp (arg, "o64") == 0)
a325df1d 10116 mips_opts.abi = O64_ABI;
e013f690 10117 else if (strcmp (arg, "n32") == 0)
a325df1d 10118 mips_opts.abi = N32_ABI;
e013f690
TS
10119 else if (strcmp (arg, "64") == 0)
10120 {
a325df1d 10121 mips_opts.abi = N64_ABI;
e013f690
TS
10122 if (! support_64bit_objects())
10123 as_fatal (_("No compiled in support for 64 bit object file "
10124 "format"));
10125 }
10126 else if (strcmp (arg, "eabi") == 0)
a325df1d 10127 mips_opts.abi = EABI_ABI;
e013f690 10128 else
da0e507f
TS
10129 {
10130 as_fatal (_("invalid abi -mabi=%s"), arg);
10131 return 0;
10132 }
252b5132 10133 break;
e013f690 10134#endif /* OBJ_ELF */
252b5132 10135
6b76fefe
CM
10136 case OPTION_M7000_HILO_FIX:
10137 mips_7000_hilo_fix = true;
10138 break;
10139
10140 case OPTION_NO_M7000_HILO_FIX:
10141 mips_7000_hilo_fix = false;
10142 break;
10143
252b5132
RH
10144 default:
10145 return 0;
10146 }
10147
10148 return 1;
10149}
10150
252b5132
RH
10151static void
10152show (stream, string, col_p, first_p)
10153 FILE *stream;
10154 char *string;
10155 int *col_p;
10156 int *first_p;
10157{
10158 if (*first_p)
10159 {
10160 fprintf (stream, "%24s", "");
10161 *col_p = 24;
10162 }
10163 else
10164 {
10165 fprintf (stream, ", ");
10166 *col_p += 2;
10167 }
10168
10169 if (*col_p + strlen (string) > 72)
10170 {
10171 fprintf (stream, "\n%24s", "");
10172 *col_p = 24;
10173 }
10174
10175 fprintf (stream, "%s", string);
10176 *col_p += strlen (string);
10177
10178 *first_p = 0;
10179}
10180
252b5132
RH
10181void
10182md_show_usage (stream)
10183 FILE *stream;
10184{
10185 int column, first;
10186
beae10d5 10187 fprintf (stream, _("\
252b5132
RH
10188MIPS options:\n\
10189-membedded-pic generate embedded position independent code\n\
10190-EB generate big endian output\n\
10191-EL generate little endian output\n\
9a41af64 10192-g, -g2 do not remove unneeded NOPs or swap branches\n\
252b5132
RH
10193-G NUM allow referencing objects up to NUM bytes\n\
10194 implicitly with the gp register [default 8]\n"));
beae10d5 10195 fprintf (stream, _("\
252b5132
RH
10196-mips1 generate MIPS ISA I instructions\n\
10197-mips2 generate MIPS ISA II instructions\n\
10198-mips3 generate MIPS ISA III instructions\n\
10199-mips4 generate MIPS ISA IV instructions\n\
84ea6cf2 10200-mips5 generate MIPS ISA V instructions\n\
e7af610e 10201-mips32 generate MIPS32 ISA instructions\n\
84ea6cf2 10202-mips64 generate MIPS64 ISA instructions\n\
ec68c924 10203-march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
252b5132
RH
10204
10205 first = 1;
10206
10207 show (stream, "2000", &column, &first);
10208 show (stream, "3000", &column, &first);
10209 show (stream, "3900", &column, &first);
10210 show (stream, "4000", &column, &first);
10211 show (stream, "4010", &column, &first);
10212 show (stream, "4100", &column, &first);
10213 show (stream, "4111", &column, &first);
10214 show (stream, "4300", &column, &first);
10215 show (stream, "4400", &column, &first);
10216 show (stream, "4600", &column, &first);
10217 show (stream, "4650", &column, &first);
10218 show (stream, "5000", &column, &first);
18ae5d72
EC
10219 show (stream, "5200", &column, &first);
10220 show (stream, "5230", &column, &first);
10221 show (stream, "5231", &column, &first);
10222 show (stream, "5261", &column, &first);
10223 show (stream, "5721", &column, &first);
252b5132
RH
10224 show (stream, "6000", &column, &first);
10225 show (stream, "8000", &column, &first);
10226 show (stream, "10000", &column, &first);
d1cf510e 10227 show (stream, "12000", &column, &first);
2e4acd24 10228 show (stream, "sb1", &column, &first);
252b5132
RH
10229 fputc ('\n', stream);
10230
10231 fprintf (stream, _("\
ec68c924 10232-mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
252b5132
RH
10233-no-mCPU don't generate code specific to CPU.\n\
10234 For -mCPU and -no-mCPU, CPU must be one of:\n"));
10235
10236 first = 1;
10237
10238 show (stream, "3900", &column, &first);
10239 show (stream, "4010", &column, &first);
10240 show (stream, "4100", &column, &first);
10241 show (stream, "4650", &column, &first);
10242 fputc ('\n', stream);
10243
beae10d5 10244 fprintf (stream, _("\
252b5132
RH
10245-mips16 generate mips16 instructions\n\
10246-no-mips16 do not generate mips16 instructions\n"));
beae10d5 10247 fprintf (stream, _("\
ca4e0257
RS
10248-mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
10249-mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
252b5132
RH
10250-O0 remove unneeded NOPs, do not swap branches\n\
10251-O remove unneeded NOPs and swap branches\n\
63486801 10252-n warn about NOPs generated from macros\n\
119d663a 10253--[no-]construct-floats [dis]allow floating point values to be constructed\n\
252b5132
RH
10254--trap, --no-break trap exception on div by 0 and mult overflow\n\
10255--break, --no-trap break exception on div by 0 and mult overflow\n"));
10256#ifdef OBJ_ELF
beae10d5 10257 fprintf (stream, _("\
252b5132
RH
10258-KPIC, -call_shared generate SVR4 position independent code\n\
10259-non_shared do not generate position independent code\n\
10260-xgot assume a 32 bit GOT\n\
34ba82a8
TS
10261-mabi=ABI create ABI conformant object file for:\n"));
10262
10263 first = 1;
10264
10265 show (stream, "32", &column, &first);
10266 show (stream, "o64", &column, &first);
10267 show (stream, "n32", &column, &first);
10268 show (stream, "64", &column, &first);
10269 show (stream, "eabi", &column, &first);
10270
10271 fputc ('\n', stream);
10272
10273 fprintf (stream, _("\
e013f690
TS
10274-32 create o32 ABI object file (default)\n\
10275-n32 create n32 ABI object file\n\
10276-64 create 64 ABI object file\n"));
252b5132
RH
10277#endif
10278}
10279\f
10280void
10281mips_init_after_args ()
10282{
10283 /* initialize opcodes */
10284 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
beae10d5 10285 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
252b5132
RH
10286}
10287
10288long
10289md_pcrel_from (fixP)
10290 fixS *fixP;
10291{
10292 if (OUTPUT_FLAVOR != bfd_target_aout_flavour
10293 && fixP->fx_addsy != (symbolS *) NULL
10294 && ! S_IS_DEFINED (fixP->fx_addsy))
10295 {
6478892d
TS
10296 /* This makes a branch to an undefined symbol be a branch to the
10297 current location. */
cb56d3d3 10298 if (mips_pic == EMBEDDED_PIC)
6478892d 10299 return 4;
cb56d3d3 10300 else
6478892d 10301 return 1;
252b5132
RH
10302 }
10303
10304 /* return the address of the delay slot */
10305 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
10306}
10307
252b5132
RH
10308/* This is called before the symbol table is processed. In order to
10309 work with gcc when using mips-tfile, we must keep all local labels.
10310 However, in other cases, we want to discard them. If we were
10311 called with -g, but we didn't see any debugging information, it may
10312 mean that gcc is smuggling debugging information through to
10313 mips-tfile, in which case we must generate all local labels. */
10314
10315void
10316mips_frob_file_before_adjust ()
10317{
10318#ifndef NO_ECOFF_DEBUGGING
10319 if (ECOFF_DEBUGGING
10320 && mips_debug != 0
10321 && ! ecoff_debugging_seen)
10322 flag_keep_locals = 1;
10323#endif
10324}
10325
10326/* Sort any unmatched HI16_S relocs so that they immediately precede
94f592af 10327 the corresponding LO reloc. This is called before md_apply_fix3 and
252b5132
RH
10328 tc_gen_reloc. Unmatched HI16_S relocs can only be generated by
10329 explicit use of the %hi modifier. */
10330
10331void
10332mips_frob_file ()
10333{
10334 struct mips_hi_fixup *l;
10335
10336 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
10337 {
10338 segment_info_type *seginfo;
10339 int pass;
10340
10341 assert (l->fixp->fx_r_type == BFD_RELOC_HI16_S);
10342
10343 /* Check quickly whether the next fixup happens to be a matching
10344 %lo. */
10345 if (l->fixp->fx_next != NULL
10346 && l->fixp->fx_next->fx_r_type == BFD_RELOC_LO16
10347 && l->fixp->fx_addsy == l->fixp->fx_next->fx_addsy
10348 && l->fixp->fx_offset == l->fixp->fx_next->fx_offset)
10349 continue;
10350
10351 /* Look through the fixups for this segment for a matching %lo.
10352 When we find one, move the %hi just in front of it. We do
10353 this in two passes. In the first pass, we try to find a
10354 unique %lo. In the second pass, we permit multiple %hi
10355 relocs for a single %lo (this is a GNU extension). */
10356 seginfo = seg_info (l->seg);
10357 for (pass = 0; pass < 2; pass++)
10358 {
10359 fixS *f, *prev;
10360
10361 prev = NULL;
10362 for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
10363 {
10364 /* Check whether this is a %lo fixup which matches l->fixp. */
10365 if (f->fx_r_type == BFD_RELOC_LO16
10366 && f->fx_addsy == l->fixp->fx_addsy
10367 && f->fx_offset == l->fixp->fx_offset
10368 && (pass == 1
10369 || prev == NULL
10370 || prev->fx_r_type != BFD_RELOC_HI16_S
10371 || prev->fx_addsy != f->fx_addsy
10372 || prev->fx_offset != f->fx_offset))
10373 {
10374 fixS **pf;
10375
10376 /* Move l->fixp before f. */
10377 for (pf = &seginfo->fix_root;
10378 *pf != l->fixp;
10379 pf = &(*pf)->fx_next)
10380 assert (*pf != NULL);
10381
10382 *pf = l->fixp->fx_next;
10383
10384 l->fixp->fx_next = f;
10385 if (prev == NULL)
10386 seginfo->fix_root = l->fixp;
10387 else
10388 prev->fx_next = l->fixp;
10389
10390 break;
10391 }
10392
10393 prev = f;
10394 }
10395
10396 if (f != NULL)
10397 break;
10398
10399#if 0 /* GCC code motion plus incomplete dead code elimination
10400 can leave a %hi without a %lo. */
10401 if (pass == 1)
10402 as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
10403 _("Unmatched %%hi reloc"));
10404#endif
10405 }
10406 }
10407}
10408
10409/* When generating embedded PIC code we need to use a special
10410 relocation to represent the difference of two symbols in the .text
10411 section (switch tables use a difference of this sort). See
10412 include/coff/mips.h for details. This macro checks whether this
10413 fixup requires the special reloc. */
10414#define SWITCH_TABLE(fixp) \
10415 ((fixp)->fx_r_type == BFD_RELOC_32 \
bb2d6cd7 10416 && OUTPUT_FLAVOR != bfd_target_elf_flavour \
252b5132
RH
10417 && (fixp)->fx_addsy != NULL \
10418 && (fixp)->fx_subsy != NULL \
10419 && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
10420 && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
10421
10422/* When generating embedded PIC code we must keep all PC relative
10423 relocations, in case the linker has to relax a call. We also need
f6688943
TS
10424 to keep relocations for switch table entries.
10425
10426 We may have combined relocations without symbols in the N32/N64 ABI.
10427 We have to prevent gas from dropping them. */
252b5132 10428
252b5132
RH
10429int
10430mips_force_relocation (fixp)
10431 fixS *fixp;
10432{
10433 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
10434 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
10435 return 1;
10436
f6688943
TS
10437 if (HAVE_NEWABI
10438 && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
10439 && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
10440 || fixp->fx_r_type == BFD_RELOC_HI16_S
10441 || fixp->fx_r_type == BFD_RELOC_LO16))
10442 return 1;
10443
252b5132
RH
10444 return (mips_pic == EMBEDDED_PIC
10445 && (fixp->fx_pcrel
10446 || SWITCH_TABLE (fixp)
10447 || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
10448 || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
10449}
10450
add55e1f
RS
10451#ifdef OBJ_ELF
10452static int
10453mips_need_elf_addend_fixup (fixP)
10454 fixS *fixP;
10455{
2d2bf3e0
CD
10456 if (S_GET_OTHER (fixP->fx_addsy) == STO_MIPS16)
10457 return 1;
b25a253c
CD
10458 if (mips_pic == EMBEDDED_PIC
10459 && S_IS_WEAK (fixP->fx_addsy))
10460 return 1;
10461 if (mips_pic != EMBEDDED_PIC
10462 && (S_IS_WEAK (fixP->fx_addsy)
10463 || S_IS_EXTERN (fixP->fx_addsy))
2d2bf3e0
CD
10464 && !S_IS_COMMON (fixP->fx_addsy))
10465 return 1;
10466 if (symbol_used_in_reloc_p (fixP->fx_addsy)
10467 && (((bfd_get_section_flags (stdoutput,
10468 S_GET_SEGMENT (fixP->fx_addsy))
10469 & SEC_LINK_ONCE) != 0)
10470 || !strncmp (segment_name (S_GET_SEGMENT (fixP->fx_addsy)),
10471 ".gnu.linkonce",
10472 sizeof (".gnu.linkonce") - 1)))
10473 return 1;
10474 return 0;
add55e1f
RS
10475}
10476#endif
10477
252b5132
RH
10478/* Apply a fixup to the object file. */
10479
94f592af
NC
10480void
10481md_apply_fix3 (fixP, valP, seg)
252b5132 10482 fixS *fixP;
94f592af
NC
10483 valueT * valP;
10484 segT seg ATTRIBUTE_UNUSED;
252b5132
RH
10485{
10486 unsigned char *buf;
98aa84af
AM
10487 long insn;
10488 valueT value;
252b5132
RH
10489
10490 assert (fixP->fx_size == 4
10491 || fixP->fx_r_type == BFD_RELOC_16
f6688943
TS
10492 || fixP->fx_r_type == BFD_RELOC_32
10493 || fixP->fx_r_type == BFD_RELOC_MIPS_JMP
10494 || fixP->fx_r_type == BFD_RELOC_HI16_S
10495 || fixP->fx_r_type == BFD_RELOC_LO16
10496 || fixP->fx_r_type == BFD_RELOC_GPREL16
76b3015f 10497 || fixP->fx_r_type == BFD_RELOC_MIPS_LITERAL
f6688943 10498 || fixP->fx_r_type == BFD_RELOC_GPREL32
252b5132 10499 || fixP->fx_r_type == BFD_RELOC_64
f6688943
TS
10500 || fixP->fx_r_type == BFD_RELOC_CTOR
10501 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
10502 || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHEST
10503 || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHER
10504 || fixP->fx_r_type == BFD_RELOC_MIPS_SCN_DISP
10505 || fixP->fx_r_type == BFD_RELOC_MIPS_REL16
10506 || fixP->fx_r_type == BFD_RELOC_MIPS_RELGOT
252b5132
RH
10507 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
10508 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY);
10509
94f592af 10510 value = * valP;
252b5132
RH
10511
10512 /* If we aren't adjusting this fixup to be against the section
10513 symbol, we need to adjust the value. */
10514#ifdef OBJ_ELF
10515 if (fixP->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour)
bb2d6cd7 10516 {
add55e1f 10517 if (mips_need_elf_addend_fixup (fixP))
98aa84af
AM
10518 {
10519 valueT symval = S_GET_VALUE (fixP->fx_addsy);
94f592af 10520
98aa84af 10521 value -= symval;
add55e1f 10522 if (value != 0 && ! fixP->fx_pcrel)
98aa84af
AM
10523 {
10524 /* In this case, the bfd_install_relocation routine will
10525 incorrectly add the symbol value back in. We just want
7461da6e 10526 the addend to appear in the object file. */
98aa84af 10527 value -= symval;
7461da6e
RS
10528
10529 /* Make sure the addend is still non-zero. If it became zero
10530 after the last operation, set it to a spurious value and
10531 subtract the same value from the object file's contents. */
10532 if (value == 0)
10533 {
10534 value = 8;
10535
10536 /* The in-place addends for LO16 relocations are signed;
10537 leave the matching HI16 in-place addends as zero. */
10538 if (fixP->fx_r_type != BFD_RELOC_HI16_S)
10539 {
10540 reloc_howto_type *howto;
10541 bfd_vma contents, mask, field;
10542
10543 howto = bfd_reloc_type_lookup (stdoutput,
10544 fixP->fx_r_type);
10545
10546 contents = bfd_get_bits (fixP->fx_frag->fr_literal
10547 + fixP->fx_where,
10548 fixP->fx_size * 8,
10549 target_big_endian);
10550
10551 /* MASK has bits set where the relocation should go.
10552 FIELD is -value, shifted into the appropriate place
10553 for this relocation. */
10554 mask = 1 << (howto->bitsize - 1);
10555 mask = (((mask - 1) << 1) | 1) << howto->bitpos;
10556 field = (-value >> howto->rightshift) << howto->bitpos;
10557
10558 bfd_put_bits ((field & mask) | (contents & ~mask),
10559 fixP->fx_frag->fr_literal + fixP->fx_where,
10560 fixP->fx_size * 8,
10561 target_big_endian);
10562 }
10563 }
98aa84af
AM
10564 }
10565 }
252b5132 10566
bb2d6cd7
GK
10567 /* This code was generated using trial and error and so is
10568 fragile and not trustworthy. If you change it, you should
10569 rerun the elf-rel, elf-rel2, and empic testcases and ensure
10570 they still pass. */
10571 if (fixP->fx_pcrel || fixP->fx_subsy != NULL)
10572 {
10573 value += fixP->fx_frag->fr_address + fixP->fx_where;
10574
10575 /* BFD's REL handling, for MIPS, is _very_ weird.
10576 This gives the right results, but it can't possibly
10577 be the way things are supposed to work. */
cb56d3d3
TS
10578 if ((fixP->fx_r_type != BFD_RELOC_16_PCREL
10579 && fixP->fx_r_type != BFD_RELOC_16_PCREL_S2)
bb2d6cd7
GK
10580 || S_GET_SEGMENT (fixP->fx_addsy) != undefined_section)
10581 value += fixP->fx_frag->fr_address + fixP->fx_where;
10582 }
10583 }
10584#endif
252b5132 10585
94f592af 10586 fixP->fx_addnumber = value; /* Remember value for tc_gen_reloc. */
252b5132
RH
10587
10588 if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel)
10589 fixP->fx_done = 1;
10590
10591 switch (fixP->fx_r_type)
10592 {
10593 case BFD_RELOC_MIPS_JMP:
e369bcce
TS
10594 case BFD_RELOC_MIPS_SHIFT5:
10595 case BFD_RELOC_MIPS_SHIFT6:
10596 case BFD_RELOC_MIPS_GOT_DISP:
10597 case BFD_RELOC_MIPS_GOT_PAGE:
10598 case BFD_RELOC_MIPS_GOT_OFST:
10599 case BFD_RELOC_MIPS_SUB:
10600 case BFD_RELOC_MIPS_INSERT_A:
10601 case BFD_RELOC_MIPS_INSERT_B:
10602 case BFD_RELOC_MIPS_DELETE:
10603 case BFD_RELOC_MIPS_HIGHEST:
10604 case BFD_RELOC_MIPS_HIGHER:
10605 case BFD_RELOC_MIPS_SCN_DISP:
10606 case BFD_RELOC_MIPS_REL16:
10607 case BFD_RELOC_MIPS_RELGOT:
10608 case BFD_RELOC_MIPS_JALR:
252b5132
RH
10609 case BFD_RELOC_HI16:
10610 case BFD_RELOC_HI16_S:
cdf6fd85 10611 case BFD_RELOC_GPREL16:
252b5132
RH
10612 case BFD_RELOC_MIPS_LITERAL:
10613 case BFD_RELOC_MIPS_CALL16:
10614 case BFD_RELOC_MIPS_GOT16:
cdf6fd85 10615 case BFD_RELOC_GPREL32:
252b5132
RH
10616 case BFD_RELOC_MIPS_GOT_HI16:
10617 case BFD_RELOC_MIPS_GOT_LO16:
10618 case BFD_RELOC_MIPS_CALL_HI16:
10619 case BFD_RELOC_MIPS_CALL_LO16:
10620 case BFD_RELOC_MIPS16_GPREL:
10621 if (fixP->fx_pcrel)
10622 as_bad_where (fixP->fx_file, fixP->fx_line,
10623 _("Invalid PC relative reloc"));
10624 /* Nothing needed to do. The value comes from the reloc entry */
10625 break;
10626
10627 case BFD_RELOC_MIPS16_JMP:
10628 /* We currently always generate a reloc against a symbol, which
10629 means that we don't want an addend even if the symbol is
10630 defined. */
10631 fixP->fx_addnumber = 0;
10632 break;
10633
10634 case BFD_RELOC_PCREL_HI16_S:
10635 /* The addend for this is tricky if it is internal, so we just
10636 do everything here rather than in bfd_install_relocation. */
bdaaa2e1 10637 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
bb2d6cd7
GK
10638 && !fixP->fx_done
10639 && value != 0)
10640 break;
10641 if (fixP->fx_addsy
10642 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
252b5132
RH
10643 {
10644 /* For an external symbol adjust by the address to make it
10645 pcrel_offset. We use the address of the RELLO reloc
10646 which follows this one. */
10647 value += (fixP->fx_next->fx_frag->fr_address
10648 + fixP->fx_next->fx_where);
10649 }
e7d556df 10650 value = ((value + 0x8000) >> 16) & 0xffff;
252b5132
RH
10651 buf = (unsigned char *) fixP->fx_frag->fr_literal + fixP->fx_where;
10652 if (target_big_endian)
10653 buf += 2;
10654 md_number_to_chars (buf, value, 2);
10655 break;
10656
10657 case BFD_RELOC_PCREL_LO16:
10658 /* The addend for this is tricky if it is internal, so we just
10659 do everything here rather than in bfd_install_relocation. */
bdaaa2e1 10660 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
bb2d6cd7
GK
10661 && !fixP->fx_done
10662 && value != 0)
10663 break;
10664 if (fixP->fx_addsy
10665 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
252b5132
RH
10666 value += fixP->fx_frag->fr_address + fixP->fx_where;
10667 buf = (unsigned char *) fixP->fx_frag->fr_literal + fixP->fx_where;
10668 if (target_big_endian)
10669 buf += 2;
10670 md_number_to_chars (buf, value, 2);
10671 break;
10672
10673 case BFD_RELOC_64:
10674 /* This is handled like BFD_RELOC_32, but we output a sign
10675 extended value if we are only 32 bits. */
10676 if (fixP->fx_done
10677 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
10678 {
10679 if (8 <= sizeof (valueT))
10680 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
10681 value, 8);
10682 else
10683 {
10684 long w1, w2;
10685 long hiv;
10686
10687 w1 = w2 = fixP->fx_where;
10688 if (target_big_endian)
10689 w1 += 4;
10690 else
10691 w2 += 4;
10692 md_number_to_chars (fixP->fx_frag->fr_literal + w1, value, 4);
10693 if ((value & 0x80000000) != 0)
10694 hiv = 0xffffffff;
10695 else
10696 hiv = 0;
10697 md_number_to_chars (fixP->fx_frag->fr_literal + w2, hiv, 4);
10698 }
10699 }
10700 break;
10701
056350c6 10702 case BFD_RELOC_RVA:
252b5132
RH
10703 case BFD_RELOC_32:
10704 /* If we are deleting this reloc entry, we must fill in the
10705 value now. This can happen if we have a .word which is not
10706 resolved when it appears but is later defined. We also need
10707 to fill in the value if this is an embedded PIC switch table
10708 entry. */
10709 if (fixP->fx_done
10710 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
10711 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
10712 value, 4);
10713 break;
10714
10715 case BFD_RELOC_16:
10716 /* If we are deleting this reloc entry, we must fill in the
10717 value now. */
10718 assert (fixP->fx_size == 2);
10719 if (fixP->fx_done)
10720 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
10721 value, 2);
10722 break;
10723
10724 case BFD_RELOC_LO16:
10725 /* When handling an embedded PIC switch statement, we can wind
10726 up deleting a LO16 reloc. See the 'o' case in mips_ip. */
10727 if (fixP->fx_done)
10728 {
98aa84af 10729 if (value + 0x8000 > 0xffff)
252b5132
RH
10730 as_bad_where (fixP->fx_file, fixP->fx_line,
10731 _("relocation overflow"));
10732 buf = (unsigned char *) fixP->fx_frag->fr_literal + fixP->fx_where;
10733 if (target_big_endian)
10734 buf += 2;
10735 md_number_to_chars (buf, value, 2);
10736 }
10737 break;
10738
10739 case BFD_RELOC_16_PCREL_S2:
cb56d3d3
TS
10740 if ((value & 0x3) != 0)
10741 as_bad_where (fixP->fx_file, fixP->fx_line,
10742 _("Branch to odd address (%lx)"), (long) value);
10743
10744 /* Fall through. */
10745
10746 case BFD_RELOC_16_PCREL:
252b5132
RH
10747 /*
10748 * We need to save the bits in the instruction since fixup_segment()
10749 * might be deleting the relocation entry (i.e., a branch within
10750 * the current segment).
10751 */
bb2d6cd7
GK
10752 if (!fixP->fx_done && value != 0)
10753 break;
10754 /* If 'value' is zero, the remaining reloc code won't actually
10755 do the store, so it must be done here. This is probably
10756 a bug somewhere. */
b25a253c
CD
10757 if (!fixP->fx_done
10758 && (fixP->fx_r_type != BFD_RELOC_16_PCREL_S2
10759 || fixP->fx_addsy == NULL /* ??? */
10760 || ! S_IS_DEFINED (fixP->fx_addsy)))
bb2d6cd7 10761 value -= fixP->fx_frag->fr_address + fixP->fx_where;
bdaaa2e1 10762
98aa84af 10763 value = (offsetT) value >> 2;
252b5132
RH
10764
10765 /* update old instruction data */
10766 buf = (unsigned char *) (fixP->fx_where + fixP->fx_frag->fr_literal);
10767 if (target_big_endian)
10768 insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
10769 else
10770 insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
10771
98aa84af 10772 if (value + 0x8000 <= 0xffff)
252b5132
RH
10773 insn |= value & 0xffff;
10774 else
10775 {
10776 /* The branch offset is too large. If this is an
10777 unconditional branch, and we are not generating PIC code,
10778 we can convert it to an absolute jump instruction. */
10779 if (mips_pic == NO_PIC
10780 && fixP->fx_done
10781 && fixP->fx_frag->fr_address >= text_section->vma
10782 && (fixP->fx_frag->fr_address
10783 < text_section->vma + text_section->_raw_size)
10784 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
10785 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
10786 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
10787 {
10788 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
10789 insn = 0x0c000000; /* jal */
10790 else
10791 insn = 0x08000000; /* j */
10792 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
10793 fixP->fx_done = 0;
10794 fixP->fx_addsy = section_symbol (text_section);
10795 fixP->fx_addnumber = (value << 2) + md_pcrel_from (fixP);
10796 }
10797 else
10798 {
10799 /* FIXME. It would be possible in principle to handle
10800 conditional branches which overflow. They could be
10801 transformed into a branch around a jump. This would
10802 require setting up variant frags for each different
10803 branch type. The native MIPS assembler attempts to
10804 handle these cases, but it appears to do it
10805 incorrectly. */
10806 as_bad_where (fixP->fx_file, fixP->fx_line,
10807 _("Branch out of range"));
10808 }
10809 }
10810
10811 md_number_to_chars ((char *) buf, (valueT) insn, 4);
10812 break;
10813
10814 case BFD_RELOC_VTABLE_INHERIT:
10815 fixP->fx_done = 0;
10816 if (fixP->fx_addsy
10817 && !S_IS_DEFINED (fixP->fx_addsy)
10818 && !S_IS_WEAK (fixP->fx_addsy))
10819 S_SET_WEAK (fixP->fx_addsy);
10820 break;
10821
10822 case BFD_RELOC_VTABLE_ENTRY:
10823 fixP->fx_done = 0;
10824 break;
10825
10826 default:
10827 internalError ();
10828 }
252b5132
RH
10829}
10830
10831#if 0
10832void
10833printInsn (oc)
10834 unsigned long oc;
10835{
10836 const struct mips_opcode *p;
10837 int treg, sreg, dreg, shamt;
10838 short imm;
10839 const char *args;
10840 int i;
10841
10842 for (i = 0; i < NUMOPCODES; ++i)
10843 {
10844 p = &mips_opcodes[i];
10845 if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
10846 {
10847 printf ("%08lx %s\t", oc, p->name);
10848 treg = (oc >> 16) & 0x1f;
10849 sreg = (oc >> 21) & 0x1f;
10850 dreg = (oc >> 11) & 0x1f;
10851 shamt = (oc >> 6) & 0x1f;
10852 imm = oc;
10853 for (args = p->args;; ++args)
10854 {
10855 switch (*args)
10856 {
10857 case '\0':
10858 printf ("\n");
10859 break;
10860
10861 case ',':
10862 case '(':
10863 case ')':
10864 printf ("%c", *args);
10865 continue;
10866
10867 case 'r':
10868 assert (treg == sreg);
10869 printf ("$%d,$%d", treg, sreg);
10870 continue;
10871
10872 case 'd':
10873 case 'G':
10874 printf ("$%d", dreg);
10875 continue;
10876
10877 case 't':
10878 case 'E':
10879 printf ("$%d", treg);
10880 continue;
10881
10882 case 'k':
10883 printf ("0x%x", treg);
10884 continue;
10885
10886 case 'b':
10887 case 's':
10888 printf ("$%d", sreg);
10889 continue;
10890
10891 case 'a':
10892 printf ("0x%08lx", oc & 0x1ffffff);
10893 continue;
10894
10895 case 'i':
10896 case 'j':
10897 case 'o':
10898 case 'u':
10899 printf ("%d", imm);
10900 continue;
10901
10902 case '<':
10903 case '>':
10904 printf ("$%d", shamt);
10905 continue;
10906
10907 default:
10908 internalError ();
10909 }
10910 break;
10911 }
10912 return;
10913 }
10914 }
10915 printf (_("%08lx UNDEFINED\n"), oc);
10916}
10917#endif
10918
10919static symbolS *
10920get_symbol ()
10921{
10922 int c;
10923 char *name;
10924 symbolS *p;
10925
10926 name = input_line_pointer;
10927 c = get_symbol_end ();
10928 p = (symbolS *) symbol_find_or_make (name);
10929 *input_line_pointer = c;
10930 return p;
10931}
10932
10933/* Align the current frag to a given power of two. The MIPS assembler
10934 also automatically adjusts any preceding label. */
10935
10936static void
10937mips_align (to, fill, label)
10938 int to;
10939 int fill;
10940 symbolS *label;
10941{
10942 mips_emit_delays (false);
10943 frag_align (to, fill, 0);
10944 record_alignment (now_seg, to);
10945 if (label != NULL)
10946 {
10947 assert (S_GET_SEGMENT (label) == now_seg);
49309057 10948 symbol_set_frag (label, frag_now);
252b5132
RH
10949 S_SET_VALUE (label, (valueT) frag_now_fix ());
10950 }
10951}
10952
10953/* Align to a given power of two. .align 0 turns off the automatic
10954 alignment used by the data creating pseudo-ops. */
10955
10956static void
10957s_align (x)
43841e91 10958 int x ATTRIBUTE_UNUSED;
252b5132
RH
10959{
10960 register int temp;
10961 register long temp_fill;
10962 long max_alignment = 15;
10963
10964 /*
10965
10966 o Note that the assembler pulls down any immediately preceeding label
10967 to the aligned address.
10968 o It's not documented but auto alignment is reinstated by
10969 a .align pseudo instruction.
10970 o Note also that after auto alignment is turned off the mips assembler
10971 issues an error on attempt to assemble an improperly aligned data item.
10972 We don't.
10973
10974 */
10975
10976 temp = get_absolute_expression ();
10977 if (temp > max_alignment)
10978 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
10979 else if (temp < 0)
10980 {
10981 as_warn (_("Alignment negative: 0 assumed."));
10982 temp = 0;
10983 }
10984 if (*input_line_pointer == ',')
10985 {
10986 input_line_pointer++;
10987 temp_fill = get_absolute_expression ();
10988 }
10989 else
10990 temp_fill = 0;
10991 if (temp)
10992 {
10993 auto_align = 1;
10994 mips_align (temp, (int) temp_fill,
10995 insn_labels != NULL ? insn_labels->label : NULL);
10996 }
10997 else
10998 {
10999 auto_align = 0;
11000 }
11001
11002 demand_empty_rest_of_line ();
11003}
11004
11005void
11006mips_flush_pending_output ()
11007{
11008 mips_emit_delays (false);
11009 mips_clear_insn_labels ();
11010}
11011
11012static void
11013s_change_sec (sec)
11014 int sec;
11015{
11016 segT seg;
11017
11018 /* When generating embedded PIC code, we only use the .text, .lit8,
11019 .sdata and .sbss sections. We change the .data and .rdata
11020 pseudo-ops to use .sdata. */
11021 if (mips_pic == EMBEDDED_PIC
11022 && (sec == 'd' || sec == 'r'))
11023 sec = 's';
11024
11025#ifdef OBJ_ELF
11026 /* The ELF backend needs to know that we are changing sections, so
11027 that .previous works correctly. We could do something like check
b6ff326e 11028 for an obj_section_change_hook macro, but that might be confusing
252b5132
RH
11029 as it would not be appropriate to use it in the section changing
11030 functions in read.c, since obj-elf.c intercepts those. FIXME:
11031 This should be cleaner, somehow. */
11032 obj_elf_section_change_hook ();
11033#endif
11034
11035 mips_emit_delays (false);
11036 switch (sec)
11037 {
11038 case 't':
11039 s_text (0);
11040 break;
11041 case 'd':
11042 s_data (0);
11043 break;
11044 case 'b':
11045 subseg_set (bss_section, (subsegT) get_absolute_expression ());
11046 demand_empty_rest_of_line ();
11047 break;
11048
11049 case 'r':
11050 if (USE_GLOBAL_POINTER_OPT)
11051 {
11052 seg = subseg_new (RDATA_SECTION_NAME,
11053 (subsegT) get_absolute_expression ());
11054 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11055 {
11056 bfd_set_section_flags (stdoutput, seg,
11057 (SEC_ALLOC
11058 | SEC_LOAD
11059 | SEC_READONLY
11060 | SEC_RELOC
11061 | SEC_DATA));
11062 if (strcmp (TARGET_OS, "elf") != 0)
e799a695 11063 record_alignment (seg, 4);
252b5132
RH
11064 }
11065 demand_empty_rest_of_line ();
11066 }
11067 else
11068 {
11069 as_bad (_("No read only data section in this object file format"));
11070 demand_empty_rest_of_line ();
11071 return;
11072 }
11073 break;
11074
11075 case 's':
11076 if (USE_GLOBAL_POINTER_OPT)
11077 {
11078 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
11079 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11080 {
11081 bfd_set_section_flags (stdoutput, seg,
11082 SEC_ALLOC | SEC_LOAD | SEC_RELOC
11083 | SEC_DATA);
11084 if (strcmp (TARGET_OS, "elf") != 0)
e799a695 11085 record_alignment (seg, 4);
252b5132
RH
11086 }
11087 demand_empty_rest_of_line ();
11088 break;
11089 }
11090 else
11091 {
11092 as_bad (_("Global pointers not supported; recompile -G 0"));
11093 demand_empty_rest_of_line ();
11094 return;
11095 }
11096 }
11097
11098 auto_align = 1;
11099}
11100
11101void
11102mips_enable_auto_align ()
11103{
11104 auto_align = 1;
11105}
11106
11107static void
11108s_cons (log_size)
11109 int log_size;
11110{
11111 symbolS *label;
11112
11113 label = insn_labels != NULL ? insn_labels->label : NULL;
11114 mips_emit_delays (false);
11115 if (log_size > 0 && auto_align)
11116 mips_align (log_size, 0, label);
11117 mips_clear_insn_labels ();
11118 cons (1 << log_size);
11119}
11120
11121static void
11122s_float_cons (type)
11123 int type;
11124{
11125 symbolS *label;
11126
11127 label = insn_labels != NULL ? insn_labels->label : NULL;
11128
11129 mips_emit_delays (false);
11130
11131 if (auto_align)
49309057
ILT
11132 {
11133 if (type == 'd')
11134 mips_align (3, 0, label);
11135 else
11136 mips_align (2, 0, label);
11137 }
252b5132
RH
11138
11139 mips_clear_insn_labels ();
11140
11141 float_cons (type);
11142}
11143
11144/* Handle .globl. We need to override it because on Irix 5 you are
11145 permitted to say
11146 .globl foo .text
11147 where foo is an undefined symbol, to mean that foo should be
11148 considered to be the address of a function. */
11149
11150static void
11151s_mips_globl (x)
43841e91 11152 int x ATTRIBUTE_UNUSED;
252b5132
RH
11153{
11154 char *name;
11155 int c;
11156 symbolS *symbolP;
11157 flagword flag;
11158
11159 name = input_line_pointer;
11160 c = get_symbol_end ();
11161 symbolP = symbol_find_or_make (name);
11162 *input_line_pointer = c;
11163 SKIP_WHITESPACE ();
11164
11165 /* On Irix 5, every global symbol that is not explicitly labelled as
11166 being a function is apparently labelled as being an object. */
11167 flag = BSF_OBJECT;
11168
11169 if (! is_end_of_line[(unsigned char) *input_line_pointer])
11170 {
11171 char *secname;
11172 asection *sec;
11173
11174 secname = input_line_pointer;
11175 c = get_symbol_end ();
11176 sec = bfd_get_section_by_name (stdoutput, secname);
11177 if (sec == NULL)
11178 as_bad (_("%s: no such section"), secname);
11179 *input_line_pointer = c;
11180
11181 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
11182 flag = BSF_FUNCTION;
11183 }
11184
49309057 11185 symbol_get_bfdsym (symbolP)->flags |= flag;
252b5132
RH
11186
11187 S_SET_EXTERNAL (symbolP);
11188 demand_empty_rest_of_line ();
11189}
11190
11191static void
11192s_option (x)
43841e91 11193 int x ATTRIBUTE_UNUSED;
252b5132
RH
11194{
11195 char *opt;
11196 char c;
11197
11198 opt = input_line_pointer;
11199 c = get_symbol_end ();
11200
11201 if (*opt == 'O')
11202 {
11203 /* FIXME: What does this mean? */
11204 }
11205 else if (strncmp (opt, "pic", 3) == 0)
11206 {
11207 int i;
11208
11209 i = atoi (opt + 3);
11210 if (i == 0)
11211 mips_pic = NO_PIC;
11212 else if (i == 2)
11213 mips_pic = SVR4_PIC;
11214 else
11215 as_bad (_(".option pic%d not supported"), i);
11216
11217 if (USE_GLOBAL_POINTER_OPT && mips_pic == SVR4_PIC)
11218 {
11219 if (g_switch_seen && g_switch_value != 0)
11220 as_warn (_("-G may not be used with SVR4 PIC code"));
11221 g_switch_value = 0;
11222 bfd_set_gp_size (stdoutput, 0);
11223 }
11224 }
11225 else
11226 as_warn (_("Unrecognized option \"%s\""), opt);
11227
11228 *input_line_pointer = c;
11229 demand_empty_rest_of_line ();
11230}
11231
11232/* This structure is used to hold a stack of .set values. */
11233
e972090a
NC
11234struct mips_option_stack
11235{
252b5132
RH
11236 struct mips_option_stack *next;
11237 struct mips_set_options options;
11238};
11239
11240static struct mips_option_stack *mips_opts_stack;
11241
11242/* Handle the .set pseudo-op. */
11243
11244static void
11245s_mipsset (x)
43841e91 11246 int x ATTRIBUTE_UNUSED;
252b5132
RH
11247{
11248 char *name = input_line_pointer, ch;
11249
11250 while (!is_end_of_line[(unsigned char) *input_line_pointer])
11251 input_line_pointer++;
11252 ch = *input_line_pointer;
11253 *input_line_pointer = '\0';
11254
11255 if (strcmp (name, "reorder") == 0)
11256 {
11257 if (mips_opts.noreorder && prev_nop_frag != NULL)
11258 {
11259 /* If we still have pending nops, we can discard them. The
11260 usual nop handling will insert any that are still
bdaaa2e1 11261 needed. */
252b5132
RH
11262 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11263 * (mips_opts.mips16 ? 2 : 4));
11264 prev_nop_frag = NULL;
11265 }
11266 mips_opts.noreorder = 0;
11267 }
11268 else if (strcmp (name, "noreorder") == 0)
11269 {
11270 mips_emit_delays (true);
11271 mips_opts.noreorder = 1;
11272 mips_any_noreorder = 1;
11273 }
11274 else if (strcmp (name, "at") == 0)
11275 {
11276 mips_opts.noat = 0;
11277 }
11278 else if (strcmp (name, "noat") == 0)
11279 {
11280 mips_opts.noat = 1;
11281 }
11282 else if (strcmp (name, "macro") == 0)
11283 {
11284 mips_opts.warn_about_macros = 0;
11285 }
11286 else if (strcmp (name, "nomacro") == 0)
11287 {
11288 if (mips_opts.noreorder == 0)
11289 as_bad (_("`noreorder' must be set before `nomacro'"));
11290 mips_opts.warn_about_macros = 1;
11291 }
11292 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
11293 {
11294 mips_opts.nomove = 0;
11295 }
11296 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
11297 {
11298 mips_opts.nomove = 1;
11299 }
11300 else if (strcmp (name, "bopt") == 0)
11301 {
11302 mips_opts.nobopt = 0;
11303 }
11304 else if (strcmp (name, "nobopt") == 0)
11305 {
11306 mips_opts.nobopt = 1;
11307 }
11308 else if (strcmp (name, "mips16") == 0
11309 || strcmp (name, "MIPS-16") == 0)
11310 mips_opts.mips16 = 1;
11311 else if (strcmp (name, "nomips16") == 0
11312 || strcmp (name, "noMIPS-16") == 0)
11313 mips_opts.mips16 = 0;
1f25f5d3
CD
11314 else if (strcmp (name, "mips3d") == 0)
11315 mips_opts.ase_mips3d = 1;
11316 else if (strcmp (name, "nomips3d") == 0)
11317 mips_opts.ase_mips3d = 0;
252b5132
RH
11318 else if (strncmp (name, "mips", 4) == 0)
11319 {
11320 int isa;
11321
11322 /* Permit the user to change the ISA on the fly. Needless to
11323 say, misuse can cause serious problems. */
11324 isa = atoi (name + 4);
553178e4
TS
11325 switch (isa)
11326 {
11327 case 0:
a325df1d
TS
11328 mips_opts.gp32 = file_mips_gp32;
11329 mips_opts.fp32 = file_mips_fp32;
11330 mips_opts.abi = file_mips_abi;
553178e4
TS
11331 break;
11332 case 1:
11333 case 2:
11334 case 32:
a325df1d
TS
11335 mips_opts.gp32 = 1;
11336 mips_opts.fp32 = 1;
553178e4
TS
11337 break;
11338 case 3:
11339 case 4:
11340 case 5:
11341 case 64:
a325df1d
TS
11342 /* Loosen ABI register width restriction. */
11343 if (mips_opts.abi == O32_ABI)
11344 mips_opts.abi = NO_ABI;
11345 mips_opts.gp32 = 0;
11346 mips_opts.fp32 = 0;
553178e4
TS
11347 break;
11348 default:
a325df1d 11349 as_bad (_("unknown ISA level %s"), name + 4);
553178e4
TS
11350 break;
11351 }
11352
e7af610e
NC
11353 switch (isa)
11354 {
84ea6cf2
NC
11355 case 0: mips_opts.isa = file_mips_isa; break;
11356 case 1: mips_opts.isa = ISA_MIPS1; break;
11357 case 2: mips_opts.isa = ISA_MIPS2; break;
11358 case 3: mips_opts.isa = ISA_MIPS3; break;
84ea6cf2 11359 case 4: mips_opts.isa = ISA_MIPS4; break;
9a41af64 11360 case 5: mips_opts.isa = ISA_MIPS5; break;
84ea6cf2
NC
11361 case 32: mips_opts.isa = ISA_MIPS32; break;
11362 case 64: mips_opts.isa = ISA_MIPS64; break;
a325df1d 11363 default: as_bad (_("unknown ISA level %s"), name + 4); break;
e7af610e 11364 }
252b5132
RH
11365 }
11366 else if (strcmp (name, "autoextend") == 0)
11367 mips_opts.noautoextend = 0;
11368 else if (strcmp (name, "noautoextend") == 0)
11369 mips_opts.noautoextend = 1;
11370 else if (strcmp (name, "push") == 0)
11371 {
11372 struct mips_option_stack *s;
11373
11374 s = (struct mips_option_stack *) xmalloc (sizeof *s);
11375 s->next = mips_opts_stack;
11376 s->options = mips_opts;
11377 mips_opts_stack = s;
11378 }
11379 else if (strcmp (name, "pop") == 0)
11380 {
11381 struct mips_option_stack *s;
11382
11383 s = mips_opts_stack;
11384 if (s == NULL)
11385 as_bad (_(".set pop with no .set push"));
11386 else
11387 {
11388 /* If we're changing the reorder mode we need to handle
11389 delay slots correctly. */
11390 if (s->options.noreorder && ! mips_opts.noreorder)
11391 mips_emit_delays (true);
11392 else if (! s->options.noreorder && mips_opts.noreorder)
11393 {
11394 if (prev_nop_frag != NULL)
11395 {
11396 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11397 * (mips_opts.mips16 ? 2 : 4));
11398 prev_nop_frag = NULL;
11399 }
11400 }
11401
11402 mips_opts = s->options;
11403 mips_opts_stack = s->next;
11404 free (s);
11405 }
11406 }
11407 else
11408 {
11409 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
11410 }
11411 *input_line_pointer = ch;
11412 demand_empty_rest_of_line ();
11413}
11414
11415/* Handle the .abicalls pseudo-op. I believe this is equivalent to
11416 .option pic2. It means to generate SVR4 PIC calls. */
11417
11418static void
11419s_abicalls (ignore)
43841e91 11420 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
11421{
11422 mips_pic = SVR4_PIC;
11423 if (USE_GLOBAL_POINTER_OPT)
11424 {
11425 if (g_switch_seen && g_switch_value != 0)
11426 as_warn (_("-G may not be used with SVR4 PIC code"));
11427 g_switch_value = 0;
11428 }
11429 bfd_set_gp_size (stdoutput, 0);
11430 demand_empty_rest_of_line ();
11431}
11432
11433/* Handle the .cpload pseudo-op. This is used when generating SVR4
11434 PIC code. It sets the $gp register for the function based on the
11435 function address, which is in the register named in the argument.
11436 This uses a relocation against _gp_disp, which is handled specially
11437 by the linker. The result is:
11438 lui $gp,%hi(_gp_disp)
11439 addiu $gp,$gp,%lo(_gp_disp)
11440 addu $gp,$gp,.cpload argument
11441 The .cpload argument is normally $25 == $t9. */
11442
11443static void
11444s_cpload (ignore)
43841e91 11445 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
11446{
11447 expressionS ex;
11448 int icnt = 0;
11449
6478892d
TS
11450 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
11451 .cpload is ignored. */
11452 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
11453 {
11454 s_ignore (0);
11455 return;
11456 }
11457
d3ecfc59 11458 /* .cpload should be in a .set noreorder section. */
252b5132
RH
11459 if (mips_opts.noreorder == 0)
11460 as_warn (_(".cpload not in noreorder section"));
11461
11462 ex.X_op = O_symbol;
11463 ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
11464 ex.X_op_symbol = NULL;
11465 ex.X_add_number = 0;
11466
11467 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
49309057 11468 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
252b5132 11469
c4e7957c 11470 macro_build_lui (NULL, &icnt, &ex, GP);
252b5132
RH
11471 macro_build ((char *) NULL, &icnt, &ex, "addiu", "t,r,j", GP, GP,
11472 (int) BFD_RELOC_LO16);
11473
11474 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "addu", "d,v,t",
11475 GP, GP, tc_get_register (0));
11476
11477 demand_empty_rest_of_line ();
11478}
11479
6478892d
TS
11480/* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
11481 .cpsetup $reg1, offset|$reg2, label
11482
11483 If offset is given, this results in:
11484 sd $gp, offset($sp)
956cd1d6
TS
11485 lui $gp, %hi(%neg(%gp_rel(label)))
11486 daddiu $gp, $gp, %lo(%neg(%gp_rel(label)))
6478892d
TS
11487 addu $gp, $gp, $reg1
11488
11489 If $reg2 is given, this results in:
11490 daddu $reg2, $gp, $0
956cd1d6
TS
11491 lui $gp, %hi(%neg(%gp_rel(label)))
11492 daddiu $gp, $gp, %lo(%neg(%gp_rel(label)))
6478892d
TS
11493 addu $gp, $gp, $reg1
11494 */
11495static void
11496s_cpsetup (ignore)
11497 int ignore ATTRIBUTE_UNUSED;
11498{
11499 expressionS ex_off;
11500 expressionS ex_sym;
11501 int reg1;
11502 int icnt = 0;
11503 char *sym;
11504
11505 /* If we are not generating SVR4 PIC code, .cpload is ignored.
11506 We also need NewABI support. */
11507 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11508 {
11509 s_ignore (0);
11510 return;
11511 }
11512
11513 reg1 = tc_get_register (0);
11514 SKIP_WHITESPACE ();
11515 if (*input_line_pointer != ',')
11516 {
11517 as_bad (_("missing argument separator ',' for .cpsetup"));
11518 return;
11519 }
11520 else
11521 input_line_pointer++;
11522 SKIP_WHITESPACE ();
11523 if (*input_line_pointer == '$')
11524 mips_cpreturn_register = tc_get_register (0);
11525 else
11526 mips_cpreturn_offset = get_absolute_expression ();
11527 SKIP_WHITESPACE ();
11528 if (*input_line_pointer != ',')
11529 {
11530 as_bad (_("missing argument separator ',' for .cpsetup"));
11531 return;
11532 }
11533 else
11534 input_line_pointer++;
11535 SKIP_WHITESPACE ();
11536 sym = input_line_pointer;
11537 while (ISALNUM (*input_line_pointer))
11538 input_line_pointer++;
11539 *input_line_pointer = 0;
11540
11541 ex_sym.X_op = O_symbol;
11542 ex_sym.X_add_symbol = symbol_find_or_make (sym);
11543 ex_sym.X_op_symbol = NULL;
11544 ex_sym.X_add_number = 0;
11545
11546 if (mips_cpreturn_register == -1)
11547 {
11548 ex_off.X_op = O_constant;
11549 ex_off.X_add_symbol = NULL;
11550 ex_off.X_op_symbol = NULL;
11551 ex_off.X_add_number = mips_cpreturn_offset;
11552
11553 macro_build ((char *) NULL, &icnt, &ex_off, "sd", "t,o(b)",
11554 mips_gp_register, (int) BFD_RELOC_LO16, SP);
11555 }
11556 else
11557 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
11558 "d,v,t", mips_cpreturn_register, mips_gp_register, 0);
11559
11560 macro_build ((char *) NULL, &icnt, &ex_sym, "lui", "t,u", mips_gp_register,
11561 (int) BFD_RELOC_GPREL16);
11562 fix_new (frag_now, (char *) prev_insn_fixp - 4 - frag_now->fr_literal, 0,
11563 NULL, 0, 0, BFD_RELOC_MIPS_SUB);
11564 fix_new (frag_now, (char *) prev_insn_fixp - 4 - frag_now->fr_literal, 0,
11565 NULL, 0, 0, BFD_RELOC_HI16_S);
11566 macro_build ((char *) NULL, &icnt, &ex_sym, "addiu", "t,r,j",
11567 mips_gp_register, mips_gp_register, (int) BFD_RELOC_GPREL16);
11568 fix_new (frag_now, (char *) prev_insn_fixp - 4 - frag_now->fr_literal, 0,
11569 NULL, 0, 0, BFD_RELOC_MIPS_SUB);
11570 fix_new (frag_now, (char *) prev_insn_fixp - 4 - frag_now->fr_literal, 0,
11571 NULL, 0, 0, BFD_RELOC_LO16);
11572 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
11573 "d,v,t", mips_gp_register, mips_gp_register, reg1);
11574
11575 demand_empty_rest_of_line ();
11576}
11577
11578static void
11579s_cplocal (ignore)
11580 int ignore ATTRIBUTE_UNUSED;
11581{
11582 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
11583 .cplocal is ignored. */
11584 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11585 {
11586 s_ignore (0);
11587 return;
11588 }
11589
11590 mips_gp_register = tc_get_register (0);
11591}
11592
252b5132
RH
11593/* Handle the .cprestore pseudo-op. This stores $gp into a given
11594 offset from $sp. The offset is remembered, and after making a PIC
11595 call $gp is restored from that location. */
11596
11597static void
11598s_cprestore (ignore)
43841e91 11599 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
11600{
11601 expressionS ex;
11602 int icnt = 0;
11603
6478892d
TS
11604 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
11605 .cprestore is ignored. */
11606 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
11607 {
11608 s_ignore (0);
11609 return;
11610 }
11611
11612 mips_cprestore_offset = get_absolute_expression ();
7a621144 11613 mips_cprestore_valid = 1;
252b5132
RH
11614
11615 ex.X_op = O_constant;
11616 ex.X_add_symbol = NULL;
11617 ex.X_op_symbol = NULL;
11618 ex.X_add_number = mips_cprestore_offset;
11619
11620 macro_build ((char *) NULL, &icnt, &ex,
ca4e0257 11621 HAVE_32BIT_ADDRESSES ? "sw" : "sd",
252b5132
RH
11622 "t,o(b)", GP, (int) BFD_RELOC_LO16, SP);
11623
11624 demand_empty_rest_of_line ();
11625}
11626
6478892d
TS
11627/* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
11628 was given in the preceeding .gpsetup, it results in:
11629 ld $gp, offset($sp)
76b3015f 11630
6478892d
TS
11631 If a register $reg2 was given there, it results in:
11632 daddiu $gp, $gp, $reg2
11633 */
11634static void
11635s_cpreturn (ignore)
11636 int ignore ATTRIBUTE_UNUSED;
11637{
11638 expressionS ex;
11639 int icnt = 0;
11640
11641 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
11642 We also need NewABI support. */
11643 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11644 {
11645 s_ignore (0);
11646 return;
11647 }
11648
11649 if (mips_cpreturn_register == -1)
11650 {
11651 ex.X_op = O_constant;
11652 ex.X_add_symbol = NULL;
11653 ex.X_op_symbol = NULL;
11654 ex.X_add_number = mips_cpreturn_offset;
11655
11656 macro_build ((char *) NULL, &icnt, &ex, "ld", "t,o(b)",
11657 mips_gp_register, (int) BFD_RELOC_LO16, SP);
11658 }
11659 else
11660 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
11661 "d,v,t", mips_gp_register, mips_cpreturn_register, 0);
11662
11663 demand_empty_rest_of_line ();
11664}
11665
11666/* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
11667 code. It sets the offset to use in gp_rel relocations. */
11668
11669static void
11670s_gpvalue (ignore)
11671 int ignore ATTRIBUTE_UNUSED;
11672{
11673 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
11674 We also need NewABI support. */
11675 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11676 {
11677 s_ignore (0);
11678 return;
11679 }
11680
11681 mips_cpreturn_offset = get_absolute_expression ();
11682
11683 demand_empty_rest_of_line ();
11684}
11685
252b5132
RH
11686/* Handle the .gpword pseudo-op. This is used when generating PIC
11687 code. It generates a 32 bit GP relative reloc. */
11688
11689static void
11690s_gpword (ignore)
43841e91 11691 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
11692{
11693 symbolS *label;
11694 expressionS ex;
11695 char *p;
11696
11697 /* When not generating PIC code, this is treated as .word. */
11698 if (mips_pic != SVR4_PIC)
11699 {
11700 s_cons (2);
11701 return;
11702 }
11703
11704 label = insn_labels != NULL ? insn_labels->label : NULL;
11705 mips_emit_delays (true);
11706 if (auto_align)
11707 mips_align (2, 0, label);
11708 mips_clear_insn_labels ();
11709
11710 expression (&ex);
11711
11712 if (ex.X_op != O_symbol || ex.X_add_number != 0)
11713 {
11714 as_bad (_("Unsupported use of .gpword"));
11715 ignore_rest_of_line ();
11716 }
11717
11718 p = frag_more (4);
11719 md_number_to_chars (p, (valueT) 0, 4);
11720 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, 0,
cdf6fd85 11721 BFD_RELOC_GPREL32);
252b5132
RH
11722
11723 demand_empty_rest_of_line ();
11724}
11725
11726/* Handle the .cpadd pseudo-op. This is used when dealing with switch
11727 tables in SVR4 PIC code. */
11728
11729static void
11730s_cpadd (ignore)
43841e91 11731 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
11732{
11733 int icnt = 0;
11734 int reg;
11735
6478892d
TS
11736 /* This is ignored when not generating SVR4 PIC code or if this is NewABI
11737 code. */
11738 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
11739 {
11740 s_ignore (0);
11741 return;
11742 }
11743
11744 /* Add $gp to the register named as an argument. */
11745 reg = tc_get_register (0);
11746 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 11747 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
11748 "d,v,t", reg, reg, GP);
11749
bdaaa2e1 11750 demand_empty_rest_of_line ();
252b5132
RH
11751}
11752
11753/* Handle the .insn pseudo-op. This marks instruction labels in
11754 mips16 mode. This permits the linker to handle them specially,
11755 such as generating jalx instructions when needed. We also make
11756 them odd for the duration of the assembly, in order to generate the
11757 right sort of code. We will make them even in the adjust_symtab
11758 routine, while leaving them marked. This is convenient for the
11759 debugger and the disassembler. The linker knows to make them odd
11760 again. */
11761
11762static void
11763s_insn (ignore)
43841e91 11764 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
11765{
11766 if (mips_opts.mips16)
11767 mips16_mark_labels ();
11768
11769 demand_empty_rest_of_line ();
11770}
11771
11772/* Handle a .stabn directive. We need these in order to mark a label
11773 as being a mips16 text label correctly. Sometimes the compiler
11774 will emit a label, followed by a .stabn, and then switch sections.
11775 If the label and .stabn are in mips16 mode, then the label is
11776 really a mips16 text label. */
11777
11778static void
11779s_mips_stab (type)
11780 int type;
11781{
11782 if (type == 'n' && mips_opts.mips16)
11783 mips16_mark_labels ();
11784
11785 s_stab (type);
11786}
11787
11788/* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
11789 */
11790
11791static void
11792s_mips_weakext (ignore)
43841e91 11793 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
11794{
11795 char *name;
11796 int c;
11797 symbolS *symbolP;
11798 expressionS exp;
11799
11800 name = input_line_pointer;
11801 c = get_symbol_end ();
11802 symbolP = symbol_find_or_make (name);
11803 S_SET_WEAK (symbolP);
11804 *input_line_pointer = c;
11805
11806 SKIP_WHITESPACE ();
11807
11808 if (! is_end_of_line[(unsigned char) *input_line_pointer])
11809 {
11810 if (S_IS_DEFINED (symbolP))
11811 {
956cd1d6 11812 as_bad ("ignoring attempt to redefine symbol %s",
252b5132
RH
11813 S_GET_NAME (symbolP));
11814 ignore_rest_of_line ();
11815 return;
11816 }
bdaaa2e1 11817
252b5132
RH
11818 if (*input_line_pointer == ',')
11819 {
11820 ++input_line_pointer;
11821 SKIP_WHITESPACE ();
11822 }
bdaaa2e1 11823
252b5132
RH
11824 expression (&exp);
11825 if (exp.X_op != O_symbol)
11826 {
11827 as_bad ("bad .weakext directive");
11828 ignore_rest_of_line();
11829 return;
11830 }
49309057 11831 symbol_set_value_expression (symbolP, &exp);
252b5132
RH
11832 }
11833
11834 demand_empty_rest_of_line ();
11835}
11836
11837/* Parse a register string into a number. Called from the ECOFF code
11838 to parse .frame. The argument is non-zero if this is the frame
11839 register, so that we can record it in mips_frame_reg. */
11840
11841int
11842tc_get_register (frame)
11843 int frame;
11844{
11845 int reg;
11846
11847 SKIP_WHITESPACE ();
11848 if (*input_line_pointer++ != '$')
11849 {
11850 as_warn (_("expected `$'"));
11851 reg = 0;
11852 }
3882b010 11853 else if (ISDIGIT (*input_line_pointer))
252b5132
RH
11854 {
11855 reg = get_absolute_expression ();
11856 if (reg < 0 || reg >= 32)
11857 {
11858 as_warn (_("Bad register number"));
11859 reg = 0;
11860 }
11861 }
11862 else
11863 {
11864 if (strncmp (input_line_pointer, "fp", 2) == 0)
11865 reg = FP;
11866 else if (strncmp (input_line_pointer, "sp", 2) == 0)
11867 reg = SP;
11868 else if (strncmp (input_line_pointer, "gp", 2) == 0)
11869 reg = GP;
11870 else if (strncmp (input_line_pointer, "at", 2) == 0)
11871 reg = AT;
11872 else
11873 {
11874 as_warn (_("Unrecognized register name"));
11875 reg = 0;
11876 }
11877 input_line_pointer += 2;
11878 }
11879 if (frame)
7a621144
DJ
11880 {
11881 mips_frame_reg = reg != 0 ? reg : SP;
11882 mips_frame_reg_valid = 1;
11883 mips_cprestore_valid = 0;
11884 }
252b5132
RH
11885 return reg;
11886}
11887
11888valueT
11889md_section_align (seg, addr)
11890 asection *seg;
11891 valueT addr;
11892{
11893 int align = bfd_get_section_alignment (stdoutput, seg);
11894
11895#ifdef OBJ_ELF
11896 /* We don't need to align ELF sections to the full alignment.
11897 However, Irix 5 may prefer that we align them at least to a 16
11898 byte boundary. We don't bother to align the sections if we are
11899 targeted for an embedded system. */
11900 if (strcmp (TARGET_OS, "elf") == 0)
11901 return addr;
11902 if (align > 4)
11903 align = 4;
11904#endif
11905
11906 return ((addr + (1 << align) - 1) & (-1 << align));
11907}
11908
11909/* Utility routine, called from above as well. If called while the
11910 input file is still being read, it's only an approximation. (For
11911 example, a symbol may later become defined which appeared to be
11912 undefined earlier.) */
11913
11914static int
11915nopic_need_relax (sym, before_relaxing)
11916 symbolS *sym;
11917 int before_relaxing;
11918{
11919 if (sym == 0)
11920 return 0;
11921
6478892d 11922 if (USE_GLOBAL_POINTER_OPT && g_switch_value > 0)
252b5132
RH
11923 {
11924 const char *symname;
11925 int change;
11926
11927 /* Find out whether this symbol can be referenced off the GP
11928 register. It can be if it is smaller than the -G size or if
11929 it is in the .sdata or .sbss section. Certain symbols can
11930 not be referenced off the GP, although it appears as though
11931 they can. */
11932 symname = S_GET_NAME (sym);
11933 if (symname != (const char *) NULL
11934 && (strcmp (symname, "eprol") == 0
11935 || strcmp (symname, "etext") == 0
11936 || strcmp (symname, "_gp") == 0
11937 || strcmp (symname, "edata") == 0
11938 || strcmp (symname, "_fbss") == 0
11939 || strcmp (symname, "_fdata") == 0
11940 || strcmp (symname, "_ftext") == 0
11941 || strcmp (symname, "end") == 0
11942 || strcmp (symname, "_gp_disp") == 0))
11943 change = 1;
11944 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
11945 && (0
11946#ifndef NO_ECOFF_DEBUGGING
49309057
ILT
11947 || (symbol_get_obj (sym)->ecoff_extern_size != 0
11948 && (symbol_get_obj (sym)->ecoff_extern_size
11949 <= g_switch_value))
252b5132
RH
11950#endif
11951 /* We must defer this decision until after the whole
11952 file has been read, since there might be a .extern
11953 after the first use of this symbol. */
11954 || (before_relaxing
11955#ifndef NO_ECOFF_DEBUGGING
49309057 11956 && symbol_get_obj (sym)->ecoff_extern_size == 0
252b5132
RH
11957#endif
11958 && S_GET_VALUE (sym) == 0)
11959 || (S_GET_VALUE (sym) != 0
11960 && S_GET_VALUE (sym) <= g_switch_value)))
11961 change = 0;
11962 else
11963 {
11964 const char *segname;
11965
11966 segname = segment_name (S_GET_SEGMENT (sym));
11967 assert (strcmp (segname, ".lit8") != 0
11968 && strcmp (segname, ".lit4") != 0);
11969 change = (strcmp (segname, ".sdata") != 0
fba2b7f9
GK
11970 && strcmp (segname, ".sbss") != 0
11971 && strncmp (segname, ".sdata.", 7) != 0
11972 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
252b5132
RH
11973 }
11974 return change;
11975 }
11976 else
11977 /* We are not optimizing for the GP register. */
11978 return 1;
11979}
11980
11981/* Given a mips16 variant frag FRAGP, return non-zero if it needs an
11982 extended opcode. SEC is the section the frag is in. */
11983
11984static int
11985mips16_extended_frag (fragp, sec, stretch)
11986 fragS *fragp;
11987 asection *sec;
11988 long stretch;
11989{
11990 int type;
11991 register const struct mips16_immed_operand *op;
11992 offsetT val;
11993 int mintiny, maxtiny;
11994 segT symsec;
98aa84af 11995 fragS *sym_frag;
252b5132
RH
11996
11997 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
11998 return 0;
11999 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
12000 return 1;
12001
12002 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12003 op = mips16_immed_operands;
12004 while (op->type != type)
12005 {
12006 ++op;
12007 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
12008 }
12009
12010 if (op->unsp)
12011 {
12012 if (type == '<' || type == '>' || type == '[' || type == ']')
12013 {
12014 mintiny = 1;
12015 maxtiny = 1 << op->nbits;
12016 }
12017 else
12018 {
12019 mintiny = 0;
12020 maxtiny = (1 << op->nbits) - 1;
12021 }
12022 }
12023 else
12024 {
12025 mintiny = - (1 << (op->nbits - 1));
12026 maxtiny = (1 << (op->nbits - 1)) - 1;
12027 }
12028
98aa84af 12029 sym_frag = symbol_get_frag (fragp->fr_symbol);
ac62c346 12030 val = S_GET_VALUE (fragp->fr_symbol);
98aa84af 12031 symsec = S_GET_SEGMENT (fragp->fr_symbol);
252b5132
RH
12032
12033 if (op->pcrel)
12034 {
12035 addressT addr;
12036
12037 /* We won't have the section when we are called from
12038 mips_relax_frag. However, we will always have been called
12039 from md_estimate_size_before_relax first. If this is a
12040 branch to a different section, we mark it as such. If SEC is
12041 NULL, and the frag is not marked, then it must be a branch to
12042 the same section. */
12043 if (sec == NULL)
12044 {
12045 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
12046 return 1;
12047 }
12048 else
12049 {
98aa84af 12050 /* Must have been called from md_estimate_size_before_relax. */
252b5132
RH
12051 if (symsec != sec)
12052 {
12053 fragp->fr_subtype =
12054 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12055
12056 /* FIXME: We should support this, and let the linker
12057 catch branches and loads that are out of range. */
12058 as_bad_where (fragp->fr_file, fragp->fr_line,
12059 _("unsupported PC relative reference to different section"));
12060
12061 return 1;
12062 }
98aa84af
AM
12063 if (fragp != sym_frag && sym_frag->fr_address == 0)
12064 /* Assume non-extended on the first relaxation pass.
12065 The address we have calculated will be bogus if this is
12066 a forward branch to another frag, as the forward frag
12067 will have fr_address == 0. */
12068 return 0;
252b5132
RH
12069 }
12070
12071 /* In this case, we know for sure that the symbol fragment is in
98aa84af
AM
12072 the same section. If the relax_marker of the symbol fragment
12073 differs from the relax_marker of this fragment, we have not
12074 yet adjusted the symbol fragment fr_address. We want to add
252b5132
RH
12075 in STRETCH in order to get a better estimate of the address.
12076 This particularly matters because of the shift bits. */
12077 if (stretch != 0
98aa84af 12078 && sym_frag->relax_marker != fragp->relax_marker)
252b5132
RH
12079 {
12080 fragS *f;
12081
12082 /* Adjust stretch for any alignment frag. Note that if have
12083 been expanding the earlier code, the symbol may be
12084 defined in what appears to be an earlier frag. FIXME:
12085 This doesn't handle the fr_subtype field, which specifies
12086 a maximum number of bytes to skip when doing an
12087 alignment. */
98aa84af 12088 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
252b5132
RH
12089 {
12090 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
12091 {
12092 if (stretch < 0)
12093 stretch = - ((- stretch)
12094 & ~ ((1 << (int) f->fr_offset) - 1));
12095 else
12096 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
12097 if (stretch == 0)
12098 break;
12099 }
12100 }
12101 if (f != NULL)
12102 val += stretch;
12103 }
12104
12105 addr = fragp->fr_address + fragp->fr_fix;
12106
12107 /* The base address rules are complicated. The base address of
12108 a branch is the following instruction. The base address of a
12109 PC relative load or add is the instruction itself, but if it
12110 is in a delay slot (in which case it can not be extended) use
12111 the address of the instruction whose delay slot it is in. */
12112 if (type == 'p' || type == 'q')
12113 {
12114 addr += 2;
12115
12116 /* If we are currently assuming that this frag should be
12117 extended, then, the current address is two bytes
bdaaa2e1 12118 higher. */
252b5132
RH
12119 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12120 addr += 2;
12121
12122 /* Ignore the low bit in the target, since it will be set
12123 for a text label. */
12124 if ((val & 1) != 0)
12125 --val;
12126 }
12127 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12128 addr -= 4;
12129 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12130 addr -= 2;
12131
12132 val -= addr & ~ ((1 << op->shift) - 1);
12133
12134 /* Branch offsets have an implicit 0 in the lowest bit. */
12135 if (type == 'p' || type == 'q')
12136 val /= 2;
12137
12138 /* If any of the shifted bits are set, we must use an extended
12139 opcode. If the address depends on the size of this
12140 instruction, this can lead to a loop, so we arrange to always
12141 use an extended opcode. We only check this when we are in
12142 the main relaxation loop, when SEC is NULL. */
12143 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
12144 {
12145 fragp->fr_subtype =
12146 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12147 return 1;
12148 }
12149
12150 /* If we are about to mark a frag as extended because the value
12151 is precisely maxtiny + 1, then there is a chance of an
12152 infinite loop as in the following code:
12153 la $4,foo
12154 .skip 1020
12155 .align 2
12156 foo:
12157 In this case when the la is extended, foo is 0x3fc bytes
12158 away, so the la can be shrunk, but then foo is 0x400 away, so
12159 the la must be extended. To avoid this loop, we mark the
12160 frag as extended if it was small, and is about to become
12161 extended with a value of maxtiny + 1. */
12162 if (val == ((maxtiny + 1) << op->shift)
12163 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
12164 && sec == NULL)
12165 {
12166 fragp->fr_subtype =
12167 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12168 return 1;
12169 }
12170 }
12171 else if (symsec != absolute_section && sec != NULL)
12172 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
12173
12174 if ((val & ((1 << op->shift) - 1)) != 0
12175 || val < (mintiny << op->shift)
12176 || val > (maxtiny << op->shift))
12177 return 1;
12178 else
12179 return 0;
12180}
12181
12182/* Estimate the size of a frag before relaxing. Unless this is the
12183 mips16, we are not really relaxing here, and the final size is
12184 encoded in the subtype information. For the mips16, we have to
12185 decide whether we are using an extended opcode or not. */
12186
252b5132
RH
12187int
12188md_estimate_size_before_relax (fragp, segtype)
12189 fragS *fragp;
12190 asection *segtype;
12191{
43841e91 12192 int change = 0;
8614eeee 12193 boolean linkonce = false;
252b5132
RH
12194
12195 if (RELAX_MIPS16_P (fragp->fr_subtype))
177b4a6a
AO
12196 /* We don't want to modify the EXTENDED bit here; it might get us
12197 into infinite loops. We change it only in mips_relax_frag(). */
12198 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
252b5132
RH
12199
12200 if (mips_pic == NO_PIC)
12201 {
12202 change = nopic_need_relax (fragp->fr_symbol, 0);
12203 }
12204 else if (mips_pic == SVR4_PIC)
12205 {
12206 symbolS *sym;
12207 asection *symsec;
12208
12209 sym = fragp->fr_symbol;
12210
12211 /* Handle the case of a symbol equated to another symbol. */
e0890092 12212 while (symbol_equated_reloc_p (sym))
252b5132
RH
12213 {
12214 symbolS *n;
12215
12216 /* It's possible to get a loop here in a badly written
12217 program. */
49309057 12218 n = symbol_get_value_expression (sym)->X_add_symbol;
252b5132
RH
12219 if (n == sym)
12220 break;
12221 sym = n;
12222 }
12223
12224 symsec = S_GET_SEGMENT (sym);
12225
8614eeee
UC
12226 /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
12227 if (symsec != segtype && ! S_IS_LOCAL (sym))
beae10d5
KH
12228 {
12229 if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
12230 != 0)
12231 linkonce = true;
12232
12233 /* The GNU toolchain uses an extension for ELF: a section
12234 beginning with the magic string .gnu.linkonce is a linkonce
12235 section. */
12236 if (strncmp (segment_name (symsec), ".gnu.linkonce",
12237 sizeof ".gnu.linkonce" - 1) == 0)
12238 linkonce = true;
12239 }
8614eeee 12240
252b5132
RH
12241 /* This must duplicate the test in adjust_reloc_syms. */
12242 change = (symsec != &bfd_und_section
12243 && symsec != &bfd_abs_section
426b0403 12244 && ! bfd_is_com_section (symsec)
8614eeee 12245 && !linkonce
426b0403 12246#ifdef OBJ_ELF
ea4ff978 12247 /* A global or weak symbol is treated as external. */
9151e8bf 12248 && (OUTPUT_FLAVOR != bfd_target_elf_flavour
b25a253c
CD
12249 || (! S_IS_WEAK (sym)
12250 && (! S_IS_EXTERN (sym) || mips_pic == EMBEDDED_PIC)))
426b0403
AM
12251#endif
12252 );
252b5132
RH
12253 }
12254 else
12255 abort ();
12256
12257 if (change)
12258 {
12259 /* Record the offset to the first reloc in the fr_opcode field.
12260 This lets md_convert_frag and tc_gen_reloc know that the code
12261 must be expanded. */
12262 fragp->fr_opcode = (fragp->fr_literal
12263 + fragp->fr_fix
12264 - RELAX_OLD (fragp->fr_subtype)
12265 + RELAX_RELOC1 (fragp->fr_subtype));
12266 /* FIXME: This really needs as_warn_where. */
12267 if (RELAX_WARN (fragp->fr_subtype))
9a41af64
TS
12268 as_warn (_("AT used after \".set noat\" or macro used after "
12269 "\".set nomacro\""));
12270
12271 return RELAX_NEW (fragp->fr_subtype) - RELAX_OLD (fragp->fr_subtype);
252b5132
RH
12272 }
12273
9a41af64 12274 return 0;
252b5132
RH
12275}
12276
12277/* This is called to see whether a reloc against a defined symbol
12278 should be converted into a reloc against a section. Don't adjust
12279 MIPS16 jump relocations, so we don't have to worry about the format
12280 of the offset in the .o file. Don't adjust relocations against
12281 mips16 symbols, so that the linker can find them if it needs to set
12282 up a stub. */
12283
12284int
12285mips_fix_adjustable (fixp)
12286 fixS *fixp;
12287{
ea4ff978
L
12288#ifdef OBJ_ELF
12289 /* Prevent all adjustments to global symbols. */
46bac6de 12290 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
b25a253c 12291 && mips_pic != EMBEDDED_PIC
46bac6de 12292 && (S_IS_EXTERN (fixp->fx_addsy) || S_IS_WEAK (fixp->fx_addsy)))
ea4ff978
L
12293 return 0;
12294#endif
252b5132
RH
12295 if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
12296 return 0;
12297 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
12298 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12299 return 0;
12300 if (fixp->fx_addsy == NULL)
12301 return 1;
12302#ifdef OBJ_ELF
12303 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
12304 && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
12305 && fixp->fx_subsy == NULL)
12306 return 0;
12307#endif
12308 return 1;
12309}
12310
12311/* Translate internal representation of relocation info to BFD target
12312 format. */
12313
12314arelent **
12315tc_gen_reloc (section, fixp)
43841e91 12316 asection *section ATTRIBUTE_UNUSED;
252b5132
RH
12317 fixS *fixp;
12318{
12319 static arelent *retval[4];
12320 arelent *reloc;
12321 bfd_reloc_code_real_type code;
12322
12323 reloc = retval[0] = (arelent *) xmalloc (sizeof (arelent));
12324 retval[1] = NULL;
12325
49309057
ILT
12326 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
12327 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
12328 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
12329
12330 if (mips_pic == EMBEDDED_PIC
12331 && SWITCH_TABLE (fixp))
12332 {
12333 /* For a switch table entry we use a special reloc. The addend
12334 is actually the difference between the reloc address and the
12335 subtrahend. */
12336 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
12337 if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
12338 as_fatal (_("Double check fx_r_type in tc-mips.c:tc_gen_reloc"));
12339 fixp->fx_r_type = BFD_RELOC_GPREL32;
12340 }
12341 else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16)
12342 {
4514d474
CD
12343 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
12344 reloc->addend = fixp->fx_addnumber;
252b5132 12345 else
4514d474
CD
12346 {
12347 /* We use a special addend for an internal RELLO reloc. */
12348 if (symbol_section_p (fixp->fx_addsy))
12349 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
12350 else
12351 reloc->addend = fixp->fx_addnumber + reloc->address;
12352 }
252b5132
RH
12353 }
12354 else if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
12355 {
12356 assert (fixp->fx_next != NULL
12357 && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
4514d474
CD
12358
12359 /* The reloc is relative to the RELLO; adjust the addend
252b5132 12360 accordingly. */
4514d474
CD
12361 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
12362 reloc->addend = fixp->fx_next->fx_addnumber;
252b5132 12363 else
4514d474
CD
12364 {
12365 /* We use a special addend for an internal RELHI reloc. */
12366 if (symbol_section_p (fixp->fx_addsy))
12367 reloc->addend = (fixp->fx_next->fx_frag->fr_address
12368 + fixp->fx_next->fx_where
12369 - S_GET_VALUE (fixp->fx_subsy));
12370 else
12371 reloc->addend = (fixp->fx_addnumber
12372 + fixp->fx_next->fx_frag->fr_address
12373 + fixp->fx_next->fx_where);
12374 }
252b5132 12375 }
4514d474
CD
12376 else if (fixp->fx_pcrel == 0 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
12377 reloc->addend = fixp->fx_addnumber;
252b5132
RH
12378 else
12379 {
12380 if (OUTPUT_FLAVOR != bfd_target_aout_flavour)
12381 /* A gruesome hack which is a result of the gruesome gas reloc
12382 handling. */
12383 reloc->addend = reloc->address;
12384 else
12385 reloc->addend = -reloc->address;
12386 }
12387
12388 /* If this is a variant frag, we may need to adjust the existing
12389 reloc and generate a new one. */
12390 if (fixp->fx_frag->fr_opcode != NULL
cdf6fd85 12391 && (fixp->fx_r_type == BFD_RELOC_GPREL16
252b5132
RH
12392 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
12393 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL16
12394 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
12395 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16
12396 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
6478892d
TS
12397 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_LO16)
12398 && ! HAVE_NEWABI)
252b5132
RH
12399 {
12400 arelent *reloc2;
12401
12402 assert (! RELAX_MIPS16_P (fixp->fx_frag->fr_subtype));
12403
12404 /* If this is not the last reloc in this frag, then we have two
12405 GPREL relocs, or a GOT_HI16/GOT_LO16 pair, or a
12406 CALL_HI16/CALL_LO16, both of which are being replaced. Let
12407 the second one handle all of them. */
12408 if (fixp->fx_next != NULL
12409 && fixp->fx_frag == fixp->fx_next->fx_frag)
12410 {
cdf6fd85
TS
12411 assert ((fixp->fx_r_type == BFD_RELOC_GPREL16
12412 && fixp->fx_next->fx_r_type == BFD_RELOC_GPREL16)
252b5132
RH
12413 || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
12414 && (fixp->fx_next->fx_r_type
12415 == BFD_RELOC_MIPS_GOT_LO16))
12416 || (fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
12417 && (fixp->fx_next->fx_r_type
12418 == BFD_RELOC_MIPS_CALL_LO16)));
12419 retval[0] = NULL;
12420 return retval;
12421 }
12422
12423 fixp->fx_where = fixp->fx_frag->fr_opcode - fixp->fx_frag->fr_literal;
12424 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
12425 reloc2 = retval[1] = (arelent *) xmalloc (sizeof (arelent));
12426 retval[2] = NULL;
49309057
ILT
12427 reloc2->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
12428 *reloc2->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
12429 reloc2->address = (reloc->address
12430 + (RELAX_RELOC2 (fixp->fx_frag->fr_subtype)
12431 - RELAX_RELOC1 (fixp->fx_frag->fr_subtype)));
12432 reloc2->addend = fixp->fx_addnumber;
12433 reloc2->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO16);
12434 assert (reloc2->howto != NULL);
12435
12436 if (RELAX_RELOC3 (fixp->fx_frag->fr_subtype))
12437 {
12438 arelent *reloc3;
12439
12440 reloc3 = retval[2] = (arelent *) xmalloc (sizeof (arelent));
12441 retval[3] = NULL;
12442 *reloc3 = *reloc2;
12443 reloc3->address += 4;
12444 }
12445
12446 if (mips_pic == NO_PIC)
12447 {
cdf6fd85 12448 assert (fixp->fx_r_type == BFD_RELOC_GPREL16);
252b5132
RH
12449 fixp->fx_r_type = BFD_RELOC_HI16_S;
12450 }
12451 else if (mips_pic == SVR4_PIC)
12452 {
12453 switch (fixp->fx_r_type)
12454 {
12455 default:
12456 abort ();
12457 case BFD_RELOC_MIPS_GOT16:
12458 break;
12459 case BFD_RELOC_MIPS_CALL16:
12460 case BFD_RELOC_MIPS_GOT_LO16:
12461 case BFD_RELOC_MIPS_CALL_LO16:
12462 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
12463 break;
12464 }
12465 }
12466 else
12467 abort ();
12468 }
12469
12470 /* Since MIPS ELF uses Rel instead of Rela, encode the vtable entry
12471 to be used in the relocation's section offset. */
12472 if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12473 {
12474 reloc->address = reloc->addend;
12475 reloc->addend = 0;
12476 }
12477
12478 /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
12479 fixup_segment converted a non-PC relative reloc into a PC
12480 relative reloc. In such a case, we need to convert the reloc
12481 code. */
12482 code = fixp->fx_r_type;
12483 if (fixp->fx_pcrel)
12484 {
12485 switch (code)
12486 {
12487 case BFD_RELOC_8:
12488 code = BFD_RELOC_8_PCREL;
12489 break;
12490 case BFD_RELOC_16:
12491 code = BFD_RELOC_16_PCREL;
12492 break;
12493 case BFD_RELOC_32:
12494 code = BFD_RELOC_32_PCREL;
12495 break;
12496 case BFD_RELOC_64:
12497 code = BFD_RELOC_64_PCREL;
12498 break;
12499 case BFD_RELOC_8_PCREL:
12500 case BFD_RELOC_16_PCREL:
12501 case BFD_RELOC_32_PCREL:
12502 case BFD_RELOC_64_PCREL:
12503 case BFD_RELOC_16_PCREL_S2:
12504 case BFD_RELOC_PCREL_HI16_S:
12505 case BFD_RELOC_PCREL_LO16:
12506 break;
12507 default:
12508 as_bad_where (fixp->fx_file, fixp->fx_line,
12509 _("Cannot make %s relocation PC relative"),
12510 bfd_get_reloc_code_name (code));
12511 }
12512 }
12513
add55e1f
RS
12514#ifdef OBJ_ELF
12515 /* md_apply_fix3 has a double-subtraction hack to get
12516 bfd_install_relocation to behave nicely. GPREL relocations are
12517 handled correctly without this hack, so undo it here. We can't
12518 stop md_apply_fix3 from subtracting twice in the first place since
12519 the fake addend is required for variant frags above. */
12520 if (fixp->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour
d11008e1 12521 && code == BFD_RELOC_GPREL16
add55e1f
RS
12522 && reloc->addend != 0
12523 && mips_need_elf_addend_fixup (fixp))
12524 reloc->addend += S_GET_VALUE (fixp->fx_addsy);
12525#endif
12526
252b5132
RH
12527 /* To support a PC relative reloc when generating embedded PIC code
12528 for ECOFF, we use a Cygnus extension. We check for that here to
12529 make sure that we don't let such a reloc escape normally. */
bb2d6cd7
GK
12530 if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
12531 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
252b5132
RH
12532 && code == BFD_RELOC_16_PCREL_S2
12533 && mips_pic != EMBEDDED_PIC)
12534 reloc->howto = NULL;
12535 else
12536 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
12537
12538 if (reloc->howto == NULL)
12539 {
12540 as_bad_where (fixp->fx_file, fixp->fx_line,
12541 _("Can not represent %s relocation in this object file format"),
12542 bfd_get_reloc_code_name (code));
12543 retval[0] = NULL;
12544 }
12545
12546 return retval;
12547}
12548
12549/* Relax a machine dependent frag. This returns the amount by which
12550 the current size of the frag should change. */
12551
12552int
12553mips_relax_frag (fragp, stretch)
12554 fragS *fragp;
12555 long stretch;
12556{
12557 if (! RELAX_MIPS16_P (fragp->fr_subtype))
12558 return 0;
12559
c4e7957c 12560 if (mips16_extended_frag (fragp, NULL, stretch))
252b5132
RH
12561 {
12562 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12563 return 0;
12564 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
12565 return 2;
12566 }
12567 else
12568 {
12569 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12570 return 0;
12571 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
12572 return -2;
12573 }
12574
12575 return 0;
12576}
12577
12578/* Convert a machine dependent frag. */
12579
12580void
12581md_convert_frag (abfd, asec, fragp)
43841e91 12582 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
12583 segT asec;
12584 fragS *fragp;
12585{
12586 int old, new;
12587 char *fixptr;
12588
12589 if (RELAX_MIPS16_P (fragp->fr_subtype))
12590 {
12591 int type;
12592 register const struct mips16_immed_operand *op;
12593 boolean small, ext;
12594 offsetT val;
12595 bfd_byte *buf;
12596 unsigned long insn;
12597 boolean use_extend;
12598 unsigned short extend;
12599
12600 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12601 op = mips16_immed_operands;
12602 while (op->type != type)
12603 ++op;
12604
12605 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12606 {
12607 small = false;
12608 ext = true;
12609 }
12610 else
12611 {
12612 small = true;
12613 ext = false;
12614 }
12615
6386f3a7 12616 resolve_symbol_value (fragp->fr_symbol);
252b5132
RH
12617 val = S_GET_VALUE (fragp->fr_symbol);
12618 if (op->pcrel)
12619 {
12620 addressT addr;
12621
12622 addr = fragp->fr_address + fragp->fr_fix;
12623
12624 /* The rules for the base address of a PC relative reloc are
12625 complicated; see mips16_extended_frag. */
12626 if (type == 'p' || type == 'q')
12627 {
12628 addr += 2;
12629 if (ext)
12630 addr += 2;
12631 /* Ignore the low bit in the target, since it will be
12632 set for a text label. */
12633 if ((val & 1) != 0)
12634 --val;
12635 }
12636 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12637 addr -= 4;
12638 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12639 addr -= 2;
12640
12641 addr &= ~ (addressT) ((1 << op->shift) - 1);
12642 val -= addr;
12643
12644 /* Make sure the section winds up with the alignment we have
12645 assumed. */
12646 if (op->shift > 0)
12647 record_alignment (asec, op->shift);
12648 }
12649
12650 if (ext
12651 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
12652 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
12653 as_warn_where (fragp->fr_file, fragp->fr_line,
12654 _("extended instruction in delay slot"));
12655
12656 buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
12657
12658 if (target_big_endian)
12659 insn = bfd_getb16 (buf);
12660 else
12661 insn = bfd_getl16 (buf);
12662
12663 mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
12664 RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
12665 small, ext, &insn, &use_extend, &extend);
12666
12667 if (use_extend)
12668 {
12669 md_number_to_chars (buf, 0xf000 | extend, 2);
12670 fragp->fr_fix += 2;
12671 buf += 2;
12672 }
12673
12674 md_number_to_chars (buf, insn, 2);
12675 fragp->fr_fix += 2;
12676 buf += 2;
12677 }
12678 else
12679 {
12680 if (fragp->fr_opcode == NULL)
12681 return;
12682
12683 old = RELAX_OLD (fragp->fr_subtype);
12684 new = RELAX_NEW (fragp->fr_subtype);
12685 fixptr = fragp->fr_literal + fragp->fr_fix;
12686
12687 if (new > 0)
12688 memcpy (fixptr - old, fixptr, new);
12689
12690 fragp->fr_fix += new - old;
12691 }
12692}
12693
12694#ifdef OBJ_ELF
12695
12696/* This function is called after the relocs have been generated.
12697 We've been storing mips16 text labels as odd. Here we convert them
12698 back to even for the convenience of the debugger. */
12699
12700void
12701mips_frob_file_after_relocs ()
12702{
12703 asymbol **syms;
12704 unsigned int count, i;
12705
12706 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
12707 return;
12708
12709 syms = bfd_get_outsymbols (stdoutput);
12710 count = bfd_get_symcount (stdoutput);
12711 for (i = 0; i < count; i++, syms++)
12712 {
12713 if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
12714 && ((*syms)->value & 1) != 0)
12715 {
12716 (*syms)->value &= ~1;
12717 /* If the symbol has an odd size, it was probably computed
12718 incorrectly, so adjust that as well. */
12719 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
12720 ++elf_symbol (*syms)->internal_elf_sym.st_size;
12721 }
12722 }
12723}
12724
12725#endif
12726
12727/* This function is called whenever a label is defined. It is used
12728 when handling branch delays; if a branch has a label, we assume we
12729 can not move it. */
12730
12731void
12732mips_define_label (sym)
12733 symbolS *sym;
12734{
12735 struct insn_label_list *l;
12736
12737 if (free_insn_labels == NULL)
12738 l = (struct insn_label_list *) xmalloc (sizeof *l);
12739 else
12740 {
12741 l = free_insn_labels;
12742 free_insn_labels = l->next;
12743 }
12744
12745 l->label = sym;
12746 l->next = insn_labels;
12747 insn_labels = l;
12748}
12749\f
12750#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
12751
12752/* Some special processing for a MIPS ELF file. */
12753
12754void
12755mips_elf_final_processing ()
12756{
12757 /* Write out the register information. */
e013f690 12758 if (! HAVE_NEWABI)
252b5132
RH
12759 {
12760 Elf32_RegInfo s;
12761
12762 s.ri_gprmask = mips_gprmask;
12763 s.ri_cprmask[0] = mips_cprmask[0];
12764 s.ri_cprmask[1] = mips_cprmask[1];
12765 s.ri_cprmask[2] = mips_cprmask[2];
12766 s.ri_cprmask[3] = mips_cprmask[3];
12767 /* The gp_value field is set by the MIPS ELF backend. */
12768
12769 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
12770 ((Elf32_External_RegInfo *)
12771 mips_regmask_frag));
12772 }
12773 else
12774 {
12775 Elf64_Internal_RegInfo s;
12776
12777 s.ri_gprmask = mips_gprmask;
12778 s.ri_pad = 0;
12779 s.ri_cprmask[0] = mips_cprmask[0];
12780 s.ri_cprmask[1] = mips_cprmask[1];
12781 s.ri_cprmask[2] = mips_cprmask[2];
12782 s.ri_cprmask[3] = mips_cprmask[3];
12783 /* The gp_value field is set by the MIPS ELF backend. */
12784
12785 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
12786 ((Elf64_External_RegInfo *)
12787 mips_regmask_frag));
12788 }
12789
12790 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
12791 sort of BFD interface for this. */
12792 if (mips_any_noreorder)
12793 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
12794 if (mips_pic != NO_PIC)
12795 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
12796
1f25f5d3
CD
12797 /* Set MIPS ELF flags for ASEs. */
12798#if 0 /* XXX FIXME */
12799 if (file_ase_mips3d)
12800 elf_elfheader (stdoutput)->e_flags |= ???;
12801#endif
12802
bdaaa2e1 12803 /* Set the MIPS ELF ABI flags. */
a325df1d 12804 if (file_mips_abi == NO_ABI)
252b5132 12805 ;
a325df1d 12806 else if (file_mips_abi == O32_ABI)
252b5132 12807 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
a325df1d 12808 else if (file_mips_abi == O64_ABI)
252b5132 12809 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
a325df1d 12810 else if (file_mips_abi == EABI_ABI)
252b5132
RH
12811 {
12812 if (mips_eabi64)
12813 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
12814 else
12815 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
12816 }
a325df1d 12817 else if (file_mips_abi == N32_ABI)
be00bddd
TS
12818 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
12819
12820 /* Nothing to do for "64". */
252b5132
RH
12821
12822 if (mips_32bitmode)
12823 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
12824}
12825
12826#endif /* OBJ_ELF || OBJ_MAYBE_ELF */
12827\f
beae10d5
KH
12828typedef struct proc {
12829 symbolS *isym;
12830 unsigned long reg_mask;
12831 unsigned long reg_offset;
12832 unsigned long fpreg_mask;
12833 unsigned long fpreg_offset;
12834 unsigned long frame_offset;
12835 unsigned long frame_reg;
12836 unsigned long pc_reg;
12837} procS;
252b5132
RH
12838
12839static procS cur_proc;
12840static procS *cur_proc_ptr;
12841static int numprocs;
12842
0a9ef439 12843/* Fill in an rs_align_code fragment. */
a19d8eb0 12844
0a9ef439
RH
12845void
12846mips_handle_align (fragp)
12847 fragS *fragp;
a19d8eb0 12848{
0a9ef439
RH
12849 if (fragp->fr_type != rs_align_code)
12850 return;
12851
12852 if (mips_opts.mips16)
a19d8eb0
CP
12853 {
12854 static const unsigned char be_nop[] = { 0x65, 0x00 };
12855 static const unsigned char le_nop[] = { 0x00, 0x65 };
12856
0a9ef439
RH
12857 int bytes;
12858 char *p;
a19d8eb0 12859
0a9ef439
RH
12860 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
12861 p = fragp->fr_literal + fragp->fr_fix;
12862
12863 if (bytes & 1)
12864 {
12865 *p++ = 0;
12866 fragp->fr_fix += 1;
12867 }
12868
12869 memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
12870 fragp->fr_var = 2;
a19d8eb0
CP
12871 }
12872
0a9ef439 12873 /* For mips32, a nop is a zero, which we trivially get by doing nothing. */
a19d8eb0
CP
12874}
12875
252b5132
RH
12876static void
12877md_obj_begin ()
12878{
12879}
12880
12881static void
12882md_obj_end ()
12883{
12884 /* check for premature end, nesting errors, etc */
12885 if (cur_proc_ptr)
9a41af64 12886 as_warn (_("missing .end at end of assembly"));
252b5132
RH
12887}
12888
12889static long
12890get_number ()
12891{
12892 int negative = 0;
12893 long val = 0;
12894
12895 if (*input_line_pointer == '-')
12896 {
12897 ++input_line_pointer;
12898 negative = 1;
12899 }
3882b010 12900 if (!ISDIGIT (*input_line_pointer))
956cd1d6 12901 as_bad (_("expected simple number"));
252b5132
RH
12902 if (input_line_pointer[0] == '0')
12903 {
12904 if (input_line_pointer[1] == 'x')
12905 {
12906 input_line_pointer += 2;
3882b010 12907 while (ISXDIGIT (*input_line_pointer))
252b5132
RH
12908 {
12909 val <<= 4;
12910 val |= hex_value (*input_line_pointer++);
12911 }
12912 return negative ? -val : val;
12913 }
12914 else
12915 {
12916 ++input_line_pointer;
3882b010 12917 while (ISDIGIT (*input_line_pointer))
252b5132
RH
12918 {
12919 val <<= 3;
12920 val |= *input_line_pointer++ - '0';
12921 }
12922 return negative ? -val : val;
12923 }
12924 }
3882b010 12925 if (!ISDIGIT (*input_line_pointer))
252b5132
RH
12926 {
12927 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
12928 *input_line_pointer, *input_line_pointer);
956cd1d6 12929 as_warn (_("invalid number"));
252b5132
RH
12930 return -1;
12931 }
3882b010 12932 while (ISDIGIT (*input_line_pointer))
252b5132
RH
12933 {
12934 val *= 10;
12935 val += *input_line_pointer++ - '0';
12936 }
12937 return negative ? -val : val;
12938}
12939
12940/* The .file directive; just like the usual .file directive, but there
12941 is an initial number which is the ECOFF file index. */
12942
12943static void
12944s_file (x)
43841e91 12945 int x ATTRIBUTE_UNUSED;
252b5132
RH
12946{
12947 int line;
12948
12949 line = get_number ();
12950 s_app_file (0);
12951}
12952
252b5132
RH
12953/* The .end directive. */
12954
12955static void
12956s_mips_end (x)
43841e91 12957 int x ATTRIBUTE_UNUSED;
252b5132
RH
12958{
12959 symbolS *p;
12960 int maybe_text;
12961
7a621144
DJ
12962 /* Following functions need their own .frame and .cprestore directives. */
12963 mips_frame_reg_valid = 0;
12964 mips_cprestore_valid = 0;
12965
252b5132
RH
12966 if (!is_end_of_line[(unsigned char) *input_line_pointer])
12967 {
12968 p = get_symbol ();
12969 demand_empty_rest_of_line ();
12970 }
12971 else
12972 p = NULL;
12973
12974#ifdef BFD_ASSEMBLER
12975 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
12976 maybe_text = 1;
12977 else
12978 maybe_text = 0;
12979#else
12980 if (now_seg != data_section && now_seg != bss_section)
12981 maybe_text = 1;
12982 else
12983 maybe_text = 0;
12984#endif
12985
12986 if (!maybe_text)
12987 as_warn (_(".end not in text section"));
12988
12989 if (!cur_proc_ptr)
12990 {
12991 as_warn (_(".end directive without a preceding .ent directive."));
12992 demand_empty_rest_of_line ();
12993 return;
12994 }
12995
12996 if (p != NULL)
12997 {
12998 assert (S_GET_NAME (p));
12999 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->isym)))
13000 as_warn (_(".end symbol does not match .ent symbol."));
13001 }
13002 else
13003 as_warn (_(".end directive missing or unknown symbol"));
13004
13005#ifdef MIPS_STABS_ELF
13006 {
13007 segT saved_seg = now_seg;
13008 subsegT saved_subseg = now_subseg;
252b5132 13009 valueT dot;
252b5132
RH
13010 expressionS exp;
13011 char *fragp;
13012
13013 dot = frag_now_fix ();
13014
13015#ifdef md_flush_pending_output
13016 md_flush_pending_output ();
13017#endif
13018
13019 assert (pdr_seg);
13020 subseg_set (pdr_seg, 0);
13021
beae10d5 13022 /* Write the symbol. */
252b5132
RH
13023 exp.X_op = O_symbol;
13024 exp.X_add_symbol = p;
13025 exp.X_add_number = 0;
13026 emit_expr (&exp, 4);
13027
beae10d5 13028 fragp = frag_more (7 * 4);
252b5132 13029
beae10d5
KH
13030 md_number_to_chars (fragp, (valueT) cur_proc_ptr->reg_mask, 4);
13031 md_number_to_chars (fragp + 4, (valueT) cur_proc_ptr->reg_offset, 4);
13032 md_number_to_chars (fragp + 8, (valueT) cur_proc_ptr->fpreg_mask, 4);
13033 md_number_to_chars (fragp + 12, (valueT) cur_proc_ptr->fpreg_offset, 4);
13034 md_number_to_chars (fragp + 16, (valueT) cur_proc_ptr->frame_offset, 4);
13035 md_number_to_chars (fragp + 20, (valueT) cur_proc_ptr->frame_reg, 4);
13036 md_number_to_chars (fragp + 24, (valueT) cur_proc_ptr->pc_reg, 4);
252b5132
RH
13037
13038 subseg_set (saved_seg, saved_subseg);
13039 }
13040#endif
13041
13042 cur_proc_ptr = NULL;
13043}
13044
13045/* The .aent and .ent directives. */
13046
13047static void
13048s_mips_ent (aent)
13049 int aent;
13050{
13051 int number = 0;
13052 symbolS *symbolP;
13053 int maybe_text;
13054
13055 symbolP = get_symbol ();
13056 if (*input_line_pointer == ',')
13057 input_line_pointer++;
13058 SKIP_WHITESPACE ();
3882b010 13059 if (ISDIGIT (*input_line_pointer)
d9a62219 13060 || *input_line_pointer == '-')
252b5132
RH
13061 number = get_number ();
13062
13063#ifdef BFD_ASSEMBLER
13064 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
13065 maybe_text = 1;
13066 else
13067 maybe_text = 0;
13068#else
13069 if (now_seg != data_section && now_seg != bss_section)
13070 maybe_text = 1;
13071 else
13072 maybe_text = 0;
13073#endif
13074
13075 if (!maybe_text)
13076 as_warn (_(".ent or .aent not in text section."));
13077
13078 if (!aent && cur_proc_ptr)
9a41af64 13079 as_warn (_("missing .end"));
252b5132
RH
13080
13081 if (!aent)
13082 {
7a621144
DJ
13083 /* This function needs its own .frame and .cprestore directives. */
13084 mips_frame_reg_valid = 0;
13085 mips_cprestore_valid = 0;
13086
252b5132
RH
13087 cur_proc_ptr = &cur_proc;
13088 memset (cur_proc_ptr, '\0', sizeof (procS));
13089
13090 cur_proc_ptr->isym = symbolP;
13091
49309057 13092 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
252b5132
RH
13093
13094 numprocs++;
13095 }
13096
13097 demand_empty_rest_of_line ();
13098}
13099
13100/* The .frame directive. If the mdebug section is present (IRIX 5 native)
bdaaa2e1 13101 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
252b5132 13102 s_mips_frame is used so that we can set the PDR information correctly.
bdaaa2e1 13103 We can't use the ecoff routines because they make reference to the ecoff
252b5132
RH
13104 symbol table (in the mdebug section). */
13105
13106static void
13107s_mips_frame (ignore)
2b3c5a5d 13108 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
13109{
13110#ifdef MIPS_STABS_ELF
13111
13112 long val;
13113
beae10d5 13114 if (cur_proc_ptr == (procS *) NULL)
252b5132
RH
13115 {
13116 as_warn (_(".frame outside of .ent"));
13117 demand_empty_rest_of_line ();
13118 return;
13119 }
13120
13121 cur_proc_ptr->frame_reg = tc_get_register (1);
13122
13123 SKIP_WHITESPACE ();
13124 if (*input_line_pointer++ != ','
13125 || get_absolute_expression_and_terminator (&val) != ',')
13126 {
13127 as_warn (_("Bad .frame directive"));
13128 --input_line_pointer;
13129 demand_empty_rest_of_line ();
13130 return;
13131 }
13132
13133 cur_proc_ptr->frame_offset = val;
13134 cur_proc_ptr->pc_reg = tc_get_register (0);
13135
13136 demand_empty_rest_of_line ();
13137#else
13138 s_ignore (ignore);
13139#endif /* MIPS_STABS_ELF */
13140}
13141
bdaaa2e1
KH
13142/* The .fmask and .mask directives. If the mdebug section is present
13143 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
252b5132 13144 embedded targets, s_mips_mask is used so that we can set the PDR
bdaaa2e1 13145 information correctly. We can't use the ecoff routines because they
252b5132
RH
13146 make reference to the ecoff symbol table (in the mdebug section). */
13147
13148static void
13149s_mips_mask (reg_type)
13150 char reg_type;
13151{
13152#ifdef MIPS_STABS_ELF
13153 long mask, off;
bdaaa2e1 13154
252b5132
RH
13155 if (cur_proc_ptr == (procS *) NULL)
13156 {
13157 as_warn (_(".mask/.fmask outside of .ent"));
13158 demand_empty_rest_of_line ();
13159 return;
13160 }
13161
13162 if (get_absolute_expression_and_terminator (&mask) != ',')
13163 {
13164 as_warn (_("Bad .mask/.fmask directive"));
13165 --input_line_pointer;
13166 demand_empty_rest_of_line ();
13167 return;
13168 }
13169
13170 off = get_absolute_expression ();
13171
13172 if (reg_type == 'F')
13173 {
13174 cur_proc_ptr->fpreg_mask = mask;
13175 cur_proc_ptr->fpreg_offset = off;
13176 }
13177 else
13178 {
13179 cur_proc_ptr->reg_mask = mask;
13180 cur_proc_ptr->reg_offset = off;
13181 }
13182
13183 demand_empty_rest_of_line ();
13184#else
13185 s_ignore (reg_type);
13186#endif /* MIPS_STABS_ELF */
13187}
13188
13189/* The .loc directive. */
13190
13191#if 0
13192static void
13193s_loc (x)
13194 int x;
13195{
13196 symbolS *symbolP;
13197 int lineno;
13198 int addroff;
13199
13200 assert (now_seg == text_section);
13201
13202 lineno = get_number ();
13203 addroff = frag_now_fix ();
13204
13205 symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
13206 S_SET_TYPE (symbolP, N_SLINE);
13207 S_SET_OTHER (symbolP, 0);
13208 S_SET_DESC (symbolP, lineno);
13209 symbolP->sy_segment = now_seg;
13210}
13211#endif
e7af610e
NC
13212
13213/* CPU name/ISA/number mapping table.
13214
13215 Entries are grouped by type. The first matching CPU or ISA entry
13216 gets chosen by CPU or ISA, so it should be the 'canonical' name
13217 for that type. Entries after that within the type are sorted
13218 alphabetically.
13219
13220 Case is ignored in comparison, so put the canonical entry in the
13221 appropriate case but everything else in lower case to ease eye pain. */
e972090a
NC
13222static const struct mips_cpu_info mips_cpu_info_table[] =
13223{
e7af610e
NC
13224 /* MIPS1 ISA */
13225 { "MIPS1", 1, ISA_MIPS1, CPU_R3000, },
13226 { "mips", 1, ISA_MIPS1, CPU_R3000, },
13227
13228 /* MIPS2 ISA */
13229 { "MIPS2", 1, ISA_MIPS2, CPU_R6000, },
13230
13231 /* MIPS3 ISA */
13232 { "MIPS3", 1, ISA_MIPS3, CPU_R4000, },
13233
13234 /* MIPS4 ISA */
13235 { "MIPS4", 1, ISA_MIPS4, CPU_R8000, },
13236
84ea6cf2
NC
13237 /* MIPS5 ISA */
13238 { "MIPS5", 1, ISA_MIPS5, CPU_MIPS5, },
13239 { "Generic-MIPS5", 0, ISA_MIPS5, CPU_MIPS5, },
13240
e7af610e
NC
13241 /* MIPS32 ISA */
13242 { "MIPS32", 1, ISA_MIPS32, CPU_MIPS32, },
3c02b2ab 13243 { "mipsisa32", 0, ISA_MIPS32, CPU_MIPS32, },
e7af610e 13244 { "Generic-MIPS32", 0, ISA_MIPS32, CPU_MIPS32, },
3c02b2ab
EC
13245 { "4kc", 0, ISA_MIPS32, CPU_MIPS32, },
13246 { "4km", 0, ISA_MIPS32, CPU_MIPS32, },
13247 { "4kp", 0, ISA_MIPS32, CPU_MIPS32, },
13248
13249 /* For historical reasons. */
13250 { "MIPS64", 1, ISA_MIPS3, CPU_R4000, },
e7af610e 13251
84ea6cf2 13252 /* MIPS64 ISA */
3c02b2ab 13253 { "mipsisa64", 1, ISA_MIPS64, CPU_MIPS64, },
84ea6cf2 13254 { "Generic-MIPS64", 0, ISA_MIPS64, CPU_MIPS64, },
3c02b2ab
EC
13255 { "5kc", 0, ISA_MIPS64, CPU_MIPS64, },
13256 { "20kc", 0, ISA_MIPS64, CPU_MIPS64, },
e7af610e
NC
13257
13258 /* R2000 CPU */
13259 { "R2000", 0, ISA_MIPS1, CPU_R2000, },
13260 { "2000", 0, ISA_MIPS1, CPU_R2000, },
13261 { "2k", 0, ISA_MIPS1, CPU_R2000, },
13262 { "r2k", 0, ISA_MIPS1, CPU_R2000, },
13263
13264 /* R3000 CPU */
13265 { "R3000", 0, ISA_MIPS1, CPU_R3000, },
13266 { "3000", 0, ISA_MIPS1, CPU_R3000, },
13267 { "3k", 0, ISA_MIPS1, CPU_R3000, },
13268 { "r3k", 0, ISA_MIPS1, CPU_R3000, },
13269
13270 /* TX3900 CPU */
13271 { "R3900", 0, ISA_MIPS1, CPU_R3900, },
13272 { "3900", 0, ISA_MIPS1, CPU_R3900, },
e972090a 13273 { "mipstx39", 0, ISA_MIPS1, CPU_R3900, },
e7af610e
NC
13274
13275 /* R4000 CPU */
13276 { "R4000", 0, ISA_MIPS3, CPU_R4000, },
13277 { "4000", 0, ISA_MIPS3, CPU_R4000, },
13278 { "4k", 0, ISA_MIPS3, CPU_R4000, }, /* beware */
13279 { "r4k", 0, ISA_MIPS3, CPU_R4000, },
13280
13281 /* R4010 CPU */
13282 { "R4010", 0, ISA_MIPS2, CPU_R4010, },
13283 { "4010", 0, ISA_MIPS2, CPU_R4010, },
13284
13285 /* R4400 CPU */
13286 { "R4400", 0, ISA_MIPS3, CPU_R4400, },
13287 { "4400", 0, ISA_MIPS3, CPU_R4400, },
13288
13289 /* R4600 CPU */
13290 { "R4600", 0, ISA_MIPS3, CPU_R4600, },
13291 { "4600", 0, ISA_MIPS3, CPU_R4600, },
13292 { "mips64orion", 0, ISA_MIPS3, CPU_R4600, },
13293 { "orion", 0, ISA_MIPS3, CPU_R4600, },
13294
13295 /* R4650 CPU */
13296 { "R4650", 0, ISA_MIPS3, CPU_R4650, },
13297 { "4650", 0, ISA_MIPS3, CPU_R4650, },
13298
13299 /* R6000 CPU */
13300 { "R6000", 0, ISA_MIPS2, CPU_R6000, },
13301 { "6000", 0, ISA_MIPS2, CPU_R6000, },
13302 { "6k", 0, ISA_MIPS2, CPU_R6000, },
13303 { "r6k", 0, ISA_MIPS2, CPU_R6000, },
13304
13305 /* R8000 CPU */
13306 { "R8000", 0, ISA_MIPS4, CPU_R8000, },
13307 { "8000", 0, ISA_MIPS4, CPU_R8000, },
13308 { "8k", 0, ISA_MIPS4, CPU_R8000, },
13309 { "r8k", 0, ISA_MIPS4, CPU_R8000, },
13310
13311 /* R10000 CPU */
13312 { "R10000", 0, ISA_MIPS4, CPU_R10000, },
13313 { "10000", 0, ISA_MIPS4, CPU_R10000, },
13314 { "10k", 0, ISA_MIPS4, CPU_R10000, },
13315 { "r10k", 0, ISA_MIPS4, CPU_R10000, },
13316
d1cf510e
NC
13317 /* R12000 CPU */
13318 { "R12000", 0, ISA_MIPS4, CPU_R12000, },
13319 { "12000", 0, ISA_MIPS4, CPU_R12000, },
13320 { "12k", 0, ISA_MIPS4, CPU_R12000, },
13321 { "r12k", 0, ISA_MIPS4, CPU_R12000, },
13322
e7af610e
NC
13323 /* VR4100 CPU */
13324 { "VR4100", 0, ISA_MIPS3, CPU_VR4100, },
13325 { "4100", 0, ISA_MIPS3, CPU_VR4100, },
13326 { "mips64vr4100", 0, ISA_MIPS3, CPU_VR4100, },
13327 { "r4100", 0, ISA_MIPS3, CPU_VR4100, },
13328
13329 /* VR4111 CPU */
13330 { "VR4111", 0, ISA_MIPS3, CPU_R4111, },
13331 { "4111", 0, ISA_MIPS3, CPU_R4111, },
13332 { "mips64vr4111", 0, ISA_MIPS3, CPU_R4111, },
13333 { "r4111", 0, ISA_MIPS3, CPU_R4111, },
13334
13335 /* VR4300 CPU */
13336 { "VR4300", 0, ISA_MIPS3, CPU_R4300, },
13337 { "4300", 0, ISA_MIPS3, CPU_R4300, },
13338 { "mips64vr4300", 0, ISA_MIPS3, CPU_R4300, },
13339 { "r4300", 0, ISA_MIPS3, CPU_R4300, },
13340
13341 /* VR5000 CPU */
13342 { "VR5000", 0, ISA_MIPS4, CPU_R5000, },
13343 { "5000", 0, ISA_MIPS4, CPU_R5000, },
13344 { "5k", 0, ISA_MIPS4, CPU_R5000, },
13345 { "mips64vr5000", 0, ISA_MIPS4, CPU_R5000, },
13346 { "r5000", 0, ISA_MIPS4, CPU_R5000, },
13347 { "r5200", 0, ISA_MIPS4, CPU_R5000, },
18ae5d72 13348 { "rm5200", 0, ISA_MIPS4, CPU_R5000, },
e7af610e 13349 { "r5230", 0, ISA_MIPS4, CPU_R5000, },
18ae5d72 13350 { "rm5230", 0, ISA_MIPS4, CPU_R5000, },
e7af610e 13351 { "r5231", 0, ISA_MIPS4, CPU_R5000, },
18ae5d72 13352 { "rm5231", 0, ISA_MIPS4, CPU_R5000, },
e7af610e 13353 { "r5261", 0, ISA_MIPS4, CPU_R5000, },
18ae5d72 13354 { "rm5261", 0, ISA_MIPS4, CPU_R5000, },
e7af610e 13355 { "r5721", 0, ISA_MIPS4, CPU_R5000, },
18ae5d72 13356 { "rm5721", 0, ISA_MIPS4, CPU_R5000, },
e7af610e
NC
13357 { "r5k", 0, ISA_MIPS4, CPU_R5000, },
13358 { "r7000", 0, ISA_MIPS4, CPU_R5000, },
13359
2e4acd24 13360 /* Broadcom SB-1 CPU */
c6c98b38 13361 { "SB-1", 0, ISA_MIPS64, CPU_SB1, },
e972090a 13362 { "sb-1250", 0, ISA_MIPS64, CPU_SB1, },
c6c98b38
NC
13363 { "sb1", 0, ISA_MIPS64, CPU_SB1, },
13364 { "sb1250", 0, ISA_MIPS64, CPU_SB1, },
13365
beae10d5 13366 /* End marker. */
e7af610e
NC
13367 { NULL, 0, 0, 0, },
13368};
13369
13370static const struct mips_cpu_info *
13371mips_cpu_info_from_name (name)
13372 const char *name;
13373{
13374 int i;
13375
13376 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
beae10d5 13377 if (strcasecmp (name, mips_cpu_info_table[i].name) == 0)
e7af610e
NC
13378 return (&mips_cpu_info_table[i]);
13379
e972090a 13380 return NULL;
e7af610e
NC
13381}
13382
13383static const struct mips_cpu_info *
13384mips_cpu_info_from_isa (isa)
13385 int isa;
13386{
13387 int i;
13388
13389 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13390 if (mips_cpu_info_table[i].is_isa
13391 && isa == mips_cpu_info_table[i].isa)
13392 return (&mips_cpu_info_table[i]);
13393
e972090a 13394 return NULL;
e7af610e
NC
13395}
13396
13397static const struct mips_cpu_info *
13398mips_cpu_info_from_cpu (cpu)
13399 int cpu;
13400{
13401 int i;
13402
13403 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13404 if (!mips_cpu_info_table[i].is_isa
13405 && cpu == mips_cpu_info_table[i].cpu)
13406 return (&mips_cpu_info_table[i]);
13407
e972090a 13408 return NULL;
e7af610e 13409}
This page took 0.854152 seconds and 4 git commands to generate.