Make safe_read_memory_integer() return non-zero result when successful.
[deliverable/binutils-gdb.git] / gas / config / tc-mips.c
CommitLineData
252b5132 1/* tc-mips.c -- assemble code for a MIPS chip.
98d3f06f 2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
82efde3a 3 Free Software Foundation, Inc.
252b5132
RH
4 Contributed by the OSF and Ralph Campbell.
5 Written by Keith Knowles and Ralph Campbell, working independently.
6 Modified for ECOFF and R4000 support by Ian Lance Taylor of Cygnus
7 Support.
8
9 This file is part of GAS.
10
11 GAS is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2, or (at your option)
14 any later version.
15
16 GAS is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with GAS; see the file COPYING. If not, write to the Free
23 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 02111-1307, USA. */
25
26#include "as.h"
27#include "config.h"
28#include "subsegs.h"
3882b010 29#include "safe-ctype.h"
252b5132
RH
30
31#ifdef USE_STDARG
32#include <stdarg.h>
33#endif
34#ifdef USE_VARARGS
35#include <varargs.h>
36#endif
37
38#include "opcode/mips.h"
39#include "itbl-ops.h"
40
41#ifdef DEBUG
42#define DBG(x) printf x
43#else
44#define DBG(x)
45#endif
46
47#ifdef OBJ_MAYBE_ELF
48/* Clean up namespace so we can include obj-elf.h too. */
49static int mips_output_flavor PARAMS ((void));
50static int mips_output_flavor () { return OUTPUT_FLAVOR; }
51#undef OBJ_PROCESS_STAB
52#undef OUTPUT_FLAVOR
53#undef S_GET_ALIGN
54#undef S_GET_SIZE
55#undef S_SET_ALIGN
56#undef S_SET_SIZE
252b5132
RH
57#undef obj_frob_file
58#undef obj_frob_file_after_relocs
59#undef obj_frob_symbol
60#undef obj_pop_insert
61#undef obj_sec_sym_ok_for_reloc
62#undef OBJ_COPY_SYMBOL_ATTRIBUTES
63
64#include "obj-elf.h"
65/* Fix any of them that we actually care about. */
66#undef OUTPUT_FLAVOR
67#define OUTPUT_FLAVOR mips_output_flavor()
68#endif
69
70#if defined (OBJ_ELF)
71#include "elf/mips.h"
72#endif
73
74#ifndef ECOFF_DEBUGGING
75#define NO_ECOFF_DEBUGGING
76#define ECOFF_DEBUGGING 0
77#endif
78
79#include "ecoff.h"
80
81#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
82static char *mips_regmask_frag;
83#endif
84
85#define AT 1
86#define TREG 24
87#define PIC_CALL_REG 25
88#define KT0 26
89#define KT1 27
90#define GP 28
91#define SP 29
92#define FP 30
93#define RA 31
94
95#define ILLEGAL_REG (32)
96
97/* Allow override of standard little-endian ECOFF format. */
98
99#ifndef ECOFF_LITTLE_FORMAT
100#define ECOFF_LITTLE_FORMAT "ecoff-littlemips"
101#endif
102
103extern int target_big_endian;
104
252b5132
RH
105/* The name of the readonly data section. */
106#define RDATA_SECTION_NAME (OUTPUT_FLAVOR == bfd_target_aout_flavour \
107 ? ".data" \
108 : OUTPUT_FLAVOR == bfd_target_ecoff_flavour \
109 ? ".rdata" \
056350c6
NC
110 : OUTPUT_FLAVOR == bfd_target_coff_flavour \
111 ? ".rdata" \
252b5132
RH
112 : OUTPUT_FLAVOR == bfd_target_elf_flavour \
113 ? ".rodata" \
114 : (abort (), ""))
115
a325df1d
TS
116/* The ABI to use. */
117enum mips_abi_level
118{
119 NO_ABI = 0,
120 O32_ABI,
121 O64_ABI,
122 N32_ABI,
123 N64_ABI,
124 EABI_ABI
125};
126
127/* MIPS ABI we are using for this output file. */
128static enum mips_abi_level file_mips_abi = NO_ABI;
129
252b5132
RH
130/* This is the set of options which may be modified by the .set
131 pseudo-op. We use a struct so that .set push and .set pop are more
132 reliable. */
133
e972090a
NC
134struct mips_set_options
135{
252b5132
RH
136 /* MIPS ISA (Instruction Set Architecture) level. This is set to -1
137 if it has not been initialized. Changed by `.set mipsN', and the
138 -mipsN command line option, and the default CPU. */
139 int isa;
1f25f5d3
CD
140 /* Enabled Application Specific Extensions (ASEs). These are set to -1
141 if they have not been initialized. Changed by `.set <asename>', by
142 command line options, and based on the default architecture. */
143 int ase_mips3d;
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
c9914766 329/* 1 if we should generate 32 bit offsets from the $gp register in
252b5132 330 SVR4_PIC mode. Currently has no meaning in other modes. */
c9914766 331static int mips_big_got = 0;
252b5132
RH
332
333/* 1 if trap instructions should used for overflow rather than break
334 instructions. */
c9914766 335static int mips_trap = 0;
252b5132 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;
def2e0dd 426static int mips_gprel_offset = 0;
6478892d 427
7a621144
DJ
428/* Whether mips_cprestore_offset has been set in the current function
429 (or whether it has already been warned about, if not). */
430static int mips_cprestore_valid = 0;
431
252b5132
RH
432/* This is the register which holds the stack frame, as set by the
433 .frame pseudo-op. This is needed to implement .cprestore. */
434static int mips_frame_reg = SP;
435
7a621144
DJ
436/* Whether mips_frame_reg has been set in the current function
437 (or whether it has already been warned about, if not). */
438static int mips_frame_reg_valid = 0;
439
252b5132
RH
440/* To output NOP instructions correctly, we need to keep information
441 about the previous two instructions. */
442
443/* Whether we are optimizing. The default value of 2 means to remove
444 unneeded NOPs and swap branch instructions when possible. A value
445 of 1 means to not swap branches. A value of 0 means to always
446 insert NOPs. */
447static int mips_optimize = 2;
448
449/* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
450 equivalent to seeing no -g option at all. */
451static int mips_debug = 0;
452
453/* The previous instruction. */
454static struct mips_cl_insn prev_insn;
455
456/* The instruction before prev_insn. */
457static struct mips_cl_insn prev_prev_insn;
458
459/* If we don't want information for prev_insn or prev_prev_insn, we
460 point the insn_mo field at this dummy integer. */
43841e91 461static const struct mips_opcode dummy_opcode = { NULL, NULL, 0, 0, 0, 0 };
252b5132
RH
462
463/* Non-zero if prev_insn is valid. */
464static int prev_insn_valid;
465
466/* The frag for the previous instruction. */
467static struct frag *prev_insn_frag;
468
469/* The offset into prev_insn_frag for the previous instruction. */
470static long prev_insn_where;
471
472/* The reloc type for the previous instruction, if any. */
f6688943 473static bfd_reloc_code_real_type prev_insn_reloc_type[3];
252b5132
RH
474
475/* The reloc for the previous instruction, if any. */
f6688943 476static fixS *prev_insn_fixp[3];
252b5132
RH
477
478/* Non-zero if the previous instruction was in a delay slot. */
479static int prev_insn_is_delay_slot;
480
481/* Non-zero if the previous instruction was in a .set noreorder. */
482static int prev_insn_unreordered;
483
484/* Non-zero if the previous instruction uses an extend opcode (if
485 mips16). */
486static int prev_insn_extended;
487
488/* Non-zero if the previous previous instruction was in a .set
489 noreorder. */
490static int prev_prev_insn_unreordered;
491
492/* If this is set, it points to a frag holding nop instructions which
493 were inserted before the start of a noreorder section. If those
494 nops turn out to be unnecessary, the size of the frag can be
495 decreased. */
496static fragS *prev_nop_frag;
497
498/* The number of nop instructions we created in prev_nop_frag. */
499static int prev_nop_frag_holds;
500
501/* The number of nop instructions that we know we need in
bdaaa2e1 502 prev_nop_frag. */
252b5132
RH
503static int prev_nop_frag_required;
504
505/* The number of instructions we've seen since prev_nop_frag. */
506static int prev_nop_frag_since;
507
508/* For ECOFF and ELF, relocations against symbols are done in two
509 parts, with a HI relocation and a LO relocation. Each relocation
510 has only 16 bits of space to store an addend. This means that in
511 order for the linker to handle carries correctly, it must be able
512 to locate both the HI and the LO relocation. This means that the
513 relocations must appear in order in the relocation table.
514
515 In order to implement this, we keep track of each unmatched HI
516 relocation. We then sort them so that they immediately precede the
bdaaa2e1 517 corresponding LO relocation. */
252b5132 518
e972090a
NC
519struct mips_hi_fixup
520{
252b5132
RH
521 /* Next HI fixup. */
522 struct mips_hi_fixup *next;
523 /* This fixup. */
524 fixS *fixp;
525 /* The section this fixup is in. */
526 segT seg;
527};
528
529/* The list of unmatched HI relocs. */
530
531static struct mips_hi_fixup *mips_hi_fixup_list;
532
533/* Map normal MIPS register numbers to mips16 register numbers. */
534
535#define X ILLEGAL_REG
e972090a
NC
536static const int mips32_to_16_reg_map[] =
537{
252b5132
RH
538 X, X, 2, 3, 4, 5, 6, 7,
539 X, X, X, X, X, X, X, X,
540 0, 1, X, X, X, X, X, X,
541 X, X, X, X, X, X, X, X
542};
543#undef X
544
545/* Map mips16 register numbers to normal MIPS register numbers. */
546
e972090a
NC
547static const unsigned int mips16_to_32_reg_map[] =
548{
252b5132
RH
549 16, 17, 2, 3, 4, 5, 6, 7
550};
551\f
552/* Since the MIPS does not have multiple forms of PC relative
553 instructions, we do not have to do relaxing as is done on other
554 platforms. However, we do have to handle GP relative addressing
555 correctly, which turns out to be a similar problem.
556
557 Every macro that refers to a symbol can occur in (at least) two
558 forms, one with GP relative addressing and one without. For
559 example, loading a global variable into a register generally uses
560 a macro instruction like this:
561 lw $4,i
562 If i can be addressed off the GP register (this is true if it is in
563 the .sbss or .sdata section, or if it is known to be smaller than
564 the -G argument) this will generate the following instruction:
565 lw $4,i($gp)
566 This instruction will use a GPREL reloc. If i can not be addressed
567 off the GP register, the following instruction sequence will be used:
568 lui $at,i
569 lw $4,i($at)
570 In this case the first instruction will have a HI16 reloc, and the
571 second reloc will have a LO16 reloc. Both relocs will be against
572 the symbol i.
573
574 The issue here is that we may not know whether i is GP addressable
575 until after we see the instruction that uses it. Therefore, we
576 want to be able to choose the final instruction sequence only at
577 the end of the assembly. This is similar to the way other
578 platforms choose the size of a PC relative instruction only at the
579 end of assembly.
580
581 When generating position independent code we do not use GP
582 addressing in quite the same way, but the issue still arises as
583 external symbols and local symbols must be handled differently.
584
585 We handle these issues by actually generating both possible
586 instruction sequences. The longer one is put in a frag_var with
587 type rs_machine_dependent. We encode what to do with the frag in
588 the subtype field. We encode (1) the number of existing bytes to
589 replace, (2) the number of new bytes to use, (3) the offset from
590 the start of the existing bytes to the first reloc we must generate
591 (that is, the offset is applied from the start of the existing
592 bytes after they are replaced by the new bytes, if any), (4) the
593 offset from the start of the existing bytes to the second reloc,
594 (5) whether a third reloc is needed (the third reloc is always four
595 bytes after the second reloc), and (6) whether to warn if this
596 variant is used (this is sometimes needed if .set nomacro or .set
597 noat is in effect). All these numbers are reasonably small.
598
599 Generating two instruction sequences must be handled carefully to
600 ensure that delay slots are handled correctly. Fortunately, there
601 are a limited number of cases. When the second instruction
602 sequence is generated, append_insn is directed to maintain the
603 existing delay slot information, so it continues to apply to any
604 code after the second instruction sequence. This means that the
605 second instruction sequence must not impose any requirements not
606 required by the first instruction sequence.
607
608 These variant frags are then handled in functions called by the
609 machine independent code. md_estimate_size_before_relax returns
610 the final size of the frag. md_convert_frag sets up the final form
611 of the frag. tc_gen_reloc adjust the first reloc and adds a second
612 one if needed. */
613#define RELAX_ENCODE(old, new, reloc1, reloc2, reloc3, warn) \
614 ((relax_substateT) \
615 (((old) << 23) \
616 | ((new) << 16) \
617 | (((reloc1) + 64) << 9) \
618 | (((reloc2) + 64) << 2) \
619 | ((reloc3) ? (1 << 1) : 0) \
620 | ((warn) ? 1 : 0)))
621#define RELAX_OLD(i) (((i) >> 23) & 0x7f)
622#define RELAX_NEW(i) (((i) >> 16) & 0x7f)
9a41af64
TS
623#define RELAX_RELOC1(i) ((valueT) (((i) >> 9) & 0x7f) - 64)
624#define RELAX_RELOC2(i) ((valueT) (((i) >> 2) & 0x7f) - 64)
252b5132
RH
625#define RELAX_RELOC3(i) (((i) >> 1) & 1)
626#define RELAX_WARN(i) ((i) & 1)
627
628/* For mips16 code, we use an entirely different form of relaxation.
629 mips16 supports two versions of most instructions which take
630 immediate values: a small one which takes some small value, and a
631 larger one which takes a 16 bit value. Since branches also follow
632 this pattern, relaxing these values is required.
633
634 We can assemble both mips16 and normal MIPS code in a single
635 object. Therefore, we need to support this type of relaxation at
636 the same time that we support the relaxation described above. We
637 use the high bit of the subtype field to distinguish these cases.
638
639 The information we store for this type of relaxation is the
640 argument code found in the opcode file for this relocation, whether
641 the user explicitly requested a small or extended form, and whether
642 the relocation is in a jump or jal delay slot. That tells us the
643 size of the value, and how it should be stored. We also store
644 whether the fragment is considered to be extended or not. We also
645 store whether this is known to be a branch to a different section,
646 whether we have tried to relax this frag yet, and whether we have
647 ever extended a PC relative fragment because of a shift count. */
648#define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
649 (0x80000000 \
650 | ((type) & 0xff) \
651 | ((small) ? 0x100 : 0) \
652 | ((ext) ? 0x200 : 0) \
653 | ((dslot) ? 0x400 : 0) \
654 | ((jal_dslot) ? 0x800 : 0))
655#define RELAX_MIPS16_P(i) (((i) & 0x80000000) != 0)
656#define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
657#define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
658#define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
659#define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
660#define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
661#define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
662#define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
663#define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
664#define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
665#define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
666#define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
667\f
668/* Prototypes for static functions. */
669
670#ifdef __STDC__
671#define internalError() \
672 as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
673#else
674#define internalError() as_fatal (_("MIPS internal Error"));
675#endif
676
677enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
678
679static int insn_uses_reg PARAMS ((struct mips_cl_insn *ip,
680 unsigned int reg, enum mips_regclass class));
156c2f8b 681static int reg_needs_delay PARAMS ((unsigned int));
252b5132
RH
682static void mips16_mark_labels PARAMS ((void));
683static void append_insn PARAMS ((char *place,
684 struct mips_cl_insn * ip,
685 expressionS * p,
f6688943 686 bfd_reloc_code_real_type *r,
252b5132
RH
687 boolean));
688static void mips_no_prev_insn PARAMS ((int));
689static void mips_emit_delays PARAMS ((boolean));
690#ifdef USE_STDARG
691static void macro_build PARAMS ((char *place, int *counter, expressionS * ep,
692 const char *name, const char *fmt,
693 ...));
694#else
695static void macro_build ();
696#endif
697static void mips16_macro_build PARAMS ((char *, int *, expressionS *,
698 const char *, const char *,
699 va_list));
700static void macro_build_lui PARAMS ((char *place, int *counter,
701 expressionS * ep, int regnum));
702static void set_at PARAMS ((int *counter, int reg, int unsignedp));
703static void check_absolute_expr PARAMS ((struct mips_cl_insn * ip,
704 expressionS *));
705static void load_register PARAMS ((int *, int, expressionS *, int));
c9914766 706static void load_address PARAMS ((int *, int, expressionS *, int *));
ea1fb5dc 707static void move_register PARAMS ((int *, int, int));
252b5132
RH
708static void macro PARAMS ((struct mips_cl_insn * ip));
709static void mips16_macro PARAMS ((struct mips_cl_insn * ip));
710#ifdef LOSING_COMPILER
711static void macro2 PARAMS ((struct mips_cl_insn * ip));
712#endif
713static void mips_ip PARAMS ((char *str, struct mips_cl_insn * ip));
714static void mips16_ip PARAMS ((char *str, struct mips_cl_insn * ip));
715static void mips16_immed PARAMS ((char *, unsigned int, int, offsetT, boolean,
716 boolean, boolean, unsigned long *,
717 boolean *, unsigned short *));
394f9b3a 718static int my_getPercentOp PARAMS ((char **, unsigned int *, int *));
ad8d3bb3
TS
719static int my_getSmallParser PARAMS ((char **, unsigned int *, int *));
720static int my_getSmallExpression PARAMS ((expressionS *, char *));
721static void my_getExpression PARAMS ((expressionS *, char *));
ae948b86 722#ifdef OBJ_ELF
e013f690 723static int support_64bit_objects PARAMS((void));
ae948b86 724#endif
252b5132
RH
725static symbolS *get_symbol PARAMS ((void));
726static void mips_align PARAMS ((int to, int fill, symbolS *label));
727static void s_align PARAMS ((int));
728static void s_change_sec PARAMS ((int));
729static void s_cons PARAMS ((int));
730static void s_float_cons PARAMS ((int));
731static void s_mips_globl PARAMS ((int));
732static void s_option PARAMS ((int));
733static void s_mipsset PARAMS ((int));
734static void s_abicalls PARAMS ((int));
735static void s_cpload PARAMS ((int));
6478892d
TS
736static void s_cpsetup PARAMS ((int));
737static void s_cplocal PARAMS ((int));
252b5132 738static void s_cprestore PARAMS ((int));
6478892d
TS
739static void s_cpreturn PARAMS ((int));
740static void s_gpvalue PARAMS ((int));
252b5132
RH
741static void s_gpword PARAMS ((int));
742static void s_cpadd PARAMS ((int));
743static void s_insn PARAMS ((int));
744static void md_obj_begin PARAMS ((void));
745static void md_obj_end PARAMS ((void));
746static long get_number PARAMS ((void));
747static void s_mips_ent PARAMS ((int));
748static void s_mips_end PARAMS ((int));
749static void s_mips_frame PARAMS ((int));
750static void s_mips_mask PARAMS ((int));
751static void s_mips_stab PARAMS ((int));
752static void s_mips_weakext PARAMS ((int));
753static void s_file PARAMS ((int));
754static int mips16_extended_frag PARAMS ((fragS *, asection *, long));
e7af610e
NC
755static const char *mips_isa_to_str PARAMS ((int));
756static const char *mips_cpu_to_str PARAMS ((int));
252b5132 757static int validate_mips_insn PARAMS ((const struct mips_opcode *));
e87a0284 758static void show PARAMS ((FILE *, char *, int *, int *));
add55e1f
RS
759#ifdef OBJ_ELF
760static int mips_need_elf_addend_fixup PARAMS ((fixS *));
761#endif
e7af610e 762
ad8d3bb3 763/* Return values of my_getSmallExpression(). */
fb1b3232 764
ad8d3bb3 765enum small_ex_type
fb1b3232
TS
766{
767 S_EX_NONE = 0,
ad8d3bb3
TS
768 S_EX_REGISTER,
769
770 /* Direct relocation creation by %percent_op(). */
771 S_EX_HALF,
fb1b3232 772 S_EX_HI,
ad8d3bb3
TS
773 S_EX_LO,
774 S_EX_GP_REL,
775 S_EX_GOT,
776 S_EX_CALL16,
777 S_EX_GOT_DISP,
778 S_EX_GOT_PAGE,
779 S_EX_GOT_OFST,
780 S_EX_GOT_HI,
781 S_EX_GOT_LO,
782 S_EX_NEG,
fb1b3232
TS
783 S_EX_HIGHER,
784 S_EX_HIGHEST,
ad8d3bb3
TS
785 S_EX_CALL_HI,
786 S_EX_CALL_LO
fb1b3232
TS
787};
788
e7af610e
NC
789/* Table and functions used to map between CPU/ISA names, and
790 ISA levels, and CPU numbers. */
791
e972090a
NC
792struct mips_cpu_info
793{
e7af610e
NC
794 const char *name; /* CPU or ISA name. */
795 int is_isa; /* Is this an ISA? (If 0, a CPU.) */
796 int isa; /* ISA level. */
797 int cpu; /* CPU number (default CPU if ISA). */
798};
799
800static const struct mips_cpu_info *mips_cpu_info_from_name PARAMS ((const char *));
801static const struct mips_cpu_info *mips_cpu_info_from_isa PARAMS ((int));
802static const struct mips_cpu_info *mips_cpu_info_from_cpu PARAMS ((int));
252b5132
RH
803\f
804/* Pseudo-op table.
805
806 The following pseudo-ops from the Kane and Heinrich MIPS book
807 should be defined here, but are currently unsupported: .alias,
808 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
809
810 The following pseudo-ops from the Kane and Heinrich MIPS book are
811 specific to the type of debugging information being generated, and
812 should be defined by the object format: .aent, .begin, .bend,
813 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
814 .vreg.
815
816 The following pseudo-ops from the Kane and Heinrich MIPS book are
817 not MIPS CPU specific, but are also not specific to the object file
818 format. This file is probably the best place to define them, but
819 they are not currently supported: .asm0, .endr, .lab, .repeat,
820 .struct. */
821
e972090a
NC
822static const pseudo_typeS mips_pseudo_table[] =
823{
beae10d5 824 /* MIPS specific pseudo-ops. */
252b5132
RH
825 {"option", s_option, 0},
826 {"set", s_mipsset, 0},
827 {"rdata", s_change_sec, 'r'},
828 {"sdata", s_change_sec, 's'},
829 {"livereg", s_ignore, 0},
830 {"abicalls", s_abicalls, 0},
831 {"cpload", s_cpload, 0},
6478892d
TS
832 {"cpsetup", s_cpsetup, 0},
833 {"cplocal", s_cplocal, 0},
252b5132 834 {"cprestore", s_cprestore, 0},
6478892d
TS
835 {"cpreturn", s_cpreturn, 0},
836 {"gpvalue", s_gpvalue, 0},
252b5132
RH
837 {"gpword", s_gpword, 0},
838 {"cpadd", s_cpadd, 0},
839 {"insn", s_insn, 0},
840
beae10d5 841 /* Relatively generic pseudo-ops that happen to be used on MIPS
252b5132
RH
842 chips. */
843 {"asciiz", stringer, 1},
844 {"bss", s_change_sec, 'b'},
845 {"err", s_err, 0},
846 {"half", s_cons, 1},
847 {"dword", s_cons, 3},
848 {"weakext", s_mips_weakext, 0},
849
beae10d5 850 /* These pseudo-ops are defined in read.c, but must be overridden
252b5132
RH
851 here for one reason or another. */
852 {"align", s_align, 0},
853 {"byte", s_cons, 0},
854 {"data", s_change_sec, 'd'},
855 {"double", s_float_cons, 'd'},
856 {"float", s_float_cons, 'f'},
857 {"globl", s_mips_globl, 0},
858 {"global", s_mips_globl, 0},
859 {"hword", s_cons, 1},
860 {"int", s_cons, 2},
861 {"long", s_cons, 2},
862 {"octa", s_cons, 4},
863 {"quad", s_cons, 3},
864 {"short", s_cons, 1},
865 {"single", s_float_cons, 'f'},
866 {"stabn", s_mips_stab, 'n'},
867 {"text", s_change_sec, 't'},
868 {"word", s_cons, 2},
add56521
L
869
870#ifdef MIPS_STABS_ELF
871 { "extern", ecoff_directive_extern, 0},
872#endif
873
43841e91 874 { NULL, NULL, 0 },
252b5132
RH
875};
876
e972090a
NC
877static const pseudo_typeS mips_nonecoff_pseudo_table[] =
878{
beae10d5
KH
879 /* These pseudo-ops should be defined by the object file format.
880 However, a.out doesn't support them, so we have versions here. */
252b5132
RH
881 {"aent", s_mips_ent, 1},
882 {"bgnb", s_ignore, 0},
883 {"end", s_mips_end, 0},
884 {"endb", s_ignore, 0},
885 {"ent", s_mips_ent, 0},
886 {"file", s_file, 0},
887 {"fmask", s_mips_mask, 'F'},
888 {"frame", s_mips_frame, 0},
889 {"loc", s_ignore, 0},
890 {"mask", s_mips_mask, 'R'},
891 {"verstamp", s_ignore, 0},
43841e91 892 { NULL, NULL, 0 },
252b5132
RH
893};
894
895extern void pop_insert PARAMS ((const pseudo_typeS *));
896
897void
898mips_pop_insert ()
899{
900 pop_insert (mips_pseudo_table);
901 if (! ECOFF_DEBUGGING)
902 pop_insert (mips_nonecoff_pseudo_table);
903}
904\f
905/* Symbols labelling the current insn. */
906
e972090a
NC
907struct insn_label_list
908{
252b5132
RH
909 struct insn_label_list *next;
910 symbolS *label;
911};
912
913static struct insn_label_list *insn_labels;
914static struct insn_label_list *free_insn_labels;
915
916static void mips_clear_insn_labels PARAMS ((void));
917
918static inline void
919mips_clear_insn_labels ()
920{
921 register struct insn_label_list **pl;
922
923 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
924 ;
925 *pl = insn_labels;
926 insn_labels = NULL;
927}
928\f
929static char *expr_end;
930
931/* Expressions which appear in instructions. These are set by
932 mips_ip. */
933
934static expressionS imm_expr;
935static expressionS offset_expr;
936
937/* Relocs associated with imm_expr and offset_expr. */
938
f6688943
TS
939static bfd_reloc_code_real_type imm_reloc[3]
940 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
941static bfd_reloc_code_real_type offset_reloc[3]
942 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132
RH
943
944/* This is set by mips_ip if imm_reloc is an unmatched HI16_S reloc. */
945
946static boolean imm_unmatched_hi;
947
948/* These are set by mips16_ip if an explicit extension is used. */
949
950static boolean mips16_small, mips16_ext;
951
952#ifdef MIPS_STABS_ELF
953/* The pdr segment for per procedure frame/regmask info */
954
955static segT pdr_seg;
956#endif
957
e7af610e
NC
958static const char *
959mips_isa_to_str (isa)
960 int isa;
961{
962 const struct mips_cpu_info *ci;
963 static char s[20];
964
965 ci = mips_cpu_info_from_isa (isa);
966 if (ci != NULL)
967 return (ci->name);
968
969 sprintf (s, "ISA#%d", isa);
970 return s;
971}
972
973static const char *
156c2f8b
NC
974mips_cpu_to_str (cpu)
975 int cpu;
976{
e7af610e 977 const struct mips_cpu_info *ci;
156c2f8b 978 static char s[16];
e7af610e
NC
979
980 ci = mips_cpu_info_from_cpu (cpu);
981 if (ci != NULL)
982 return (ci->name);
983
984 sprintf (s, "CPU#%d", cpu);
985 return s;
156c2f8b
NC
986}
987
e013f690
TS
988/* The default target format to use. */
989
990const char *
991mips_target_format ()
992{
993 switch (OUTPUT_FLAVOR)
994 {
995 case bfd_target_aout_flavour:
996 return target_big_endian ? "a.out-mips-big" : "a.out-mips-little";
997 case bfd_target_ecoff_flavour:
998 return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
999 case bfd_target_coff_flavour:
1000 return "pe-mips";
1001 case bfd_target_elf_flavour:
1002#ifdef TE_TMIPS
1003 /* This is traditional mips */
1004 return (target_big_endian
1005 ? (HAVE_64BIT_OBJECTS ? "elf64-tradbigmips"
1006 : "elf32-tradbigmips")
1007 : (HAVE_64BIT_OBJECTS ? "elf64-tradlittlemips"
1008 : "elf32-tradlittlemips"));
1009#else
1010 return (target_big_endian
1011 ? (HAVE_64BIT_OBJECTS ? "elf64-bigmips" : "elf32-bigmips")
1012 : (HAVE_64BIT_OBJECTS ? "elf64-littlemips"
1013 : "elf32-littlemips"));
1014#endif
1015 default:
1016 abort ();
1017 return NULL;
1018 }
1019}
1020
156c2f8b
NC
1021/* This function is called once, at assembler startup time. It should
1022 set up all the tables, etc. that the MD part of the assembler will need. */
1023
252b5132
RH
1024void
1025md_begin ()
1026{
252b5132 1027 register const char *retval = NULL;
156c2f8b 1028 int i = 0;
252b5132
RH
1029 const char *cpu;
1030 char *a = NULL;
1031 int broken = 0;
1032 int mips_isa_from_cpu;
e7af610e
NC
1033 int target_cpu_had_mips16 = 0;
1034 const struct mips_cpu_info *ci;
252b5132 1035
056350c6
NC
1036 /* GP relative stuff not working for PE */
1037 if (strncmp (TARGET_OS, "pe", 2) == 0
1038 && g_switch_value != 0)
1039 {
1040 if (g_switch_seen)
1041 as_bad (_("-G not supported in this configuration."));
1042 g_switch_value = 0;
1043 }
1044
252b5132
RH
1045 cpu = TARGET_CPU;
1046 if (strcmp (cpu + (sizeof TARGET_CPU) - 3, "el") == 0)
1047 {
1048 a = xmalloc (sizeof TARGET_CPU);
1049 strcpy (a, TARGET_CPU);
1050 a[(sizeof TARGET_CPU) - 3] = '\0';
1051 cpu = a;
1052 }
1053
e7af610e 1054 if (strncmp (cpu, "mips16", sizeof "mips16" - 1) == 0)
252b5132 1055 {
e7af610e
NC
1056 target_cpu_had_mips16 = 1;
1057 cpu += sizeof "mips16" - 1;
252b5132
RH
1058 }
1059
e7af610e
NC
1060 if (mips_opts.mips16 < 0)
1061 mips_opts.mips16 = target_cpu_had_mips16;
252b5132 1062
6dce9e24
TS
1063 /* Backward compatibility for historic -mcpu= option. Check for
1064 incompatible options, warn if -mcpu is used. */
1065 if (mips_cpu != CPU_UNKNOWN
1066 && mips_arch != CPU_UNKNOWN
1067 && mips_cpu != mips_arch)
1068 {
1069 as_fatal (_("The -mcpu option can't be used together with -march. "
1070 "Use -mtune instead of -mcpu."));
1071 }
1072
1073 if (mips_cpu != CPU_UNKNOWN
1074 && mips_tune != CPU_UNKNOWN
1075 && mips_cpu != mips_tune)
1076 {
1077 as_fatal (_("The -mcpu option can't be used together with -mtune. "
1078 "Use -march instead of -mcpu."));
1079 }
1080
a8e8e863
DJ
1081#if 1
1082 /* For backward compatibility, let -mipsN set various defaults. */
1083 /* This code should go away, to be replaced with something rather more
1084 draconian. Until GCC 3.1 has been released for some reasonable
1085 amount of time, however, we need to support this. */
1086 if (mips_opts.isa != ISA_UNKNOWN)
1087 {
1088 /* Translate -mipsN to the appropriate settings of file_mips_gp32
1089 and file_mips_fp32. Tag binaries as using the mipsN ISA. */
1090 if (file_mips_gp32 < 0)
1091 {
1092 if (ISA_HAS_64BIT_REGS (mips_opts.isa))
1093 file_mips_gp32 = 0;
1094 else
1095 file_mips_gp32 = 1;
1096 }
1097 if (file_mips_fp32 < 0)
1098 {
1099 if (ISA_HAS_64BIT_REGS (mips_opts.isa))
1100 file_mips_fp32 = 0;
1101 else
1102 file_mips_fp32 = 1;
1103 }
1104
1105 ci = mips_cpu_info_from_isa (mips_opts.isa);
1106 assert (ci != NULL);
1107 /* -mipsN has higher priority than -mcpu but lower than -march. */
1108 if (mips_arch == CPU_UNKNOWN)
1109 mips_arch = ci->cpu;
1110
1111 /* Default mips_abi. */
1112 if (mips_opts.abi == NO_ABI)
1113 {
1114 if (mips_opts.isa == ISA_MIPS1 || mips_opts.isa == ISA_MIPS2)
1115 mips_opts.abi = O32_ABI;
1116 else if (mips_opts.isa == ISA_MIPS3 || mips_opts.isa == ISA_MIPS4)
1117 mips_opts.abi = O64_ABI;
1118 }
1119 }
1120
6dce9e24
TS
1121 if (mips_arch == CPU_UNKNOWN && mips_cpu != CPU_UNKNOWN)
1122 {
1123 ci = mips_cpu_info_from_cpu (mips_cpu);
1124 assert (ci != NULL);
1125 mips_arch = ci->cpu;
1126 as_warn (_("The -mcpu option is deprecated. Please use -march and "
1127 "-mtune instead."));
1128 }
1129
a8e8e863
DJ
1130 /* Set tune from -mcpu, not from -mipsN. */
1131 if (mips_tune == CPU_UNKNOWN && mips_cpu != CPU_UNKNOWN)
1132 {
1133 ci = mips_cpu_info_from_cpu (mips_cpu);
1134 assert (ci != NULL);
1135 mips_tune = ci->cpu;
1136 }
1137
ec68c924 1138 /* At this point, mips_arch will either be CPU_UNKNOWN if no ARCH was
e7af610e
NC
1139 specified on the command line, or some other value if one was.
1140 Similarly, mips_opts.isa will be ISA_UNKNOWN if not specified on
1141 the command line, or will be set otherwise if one was. */
a8e8e863
DJ
1142
1143 if (mips_arch != CPU_UNKNOWN && mips_opts.isa != ISA_UNKNOWN)
1144 /* Handled above. */;
1145#else
1146 if (mips_arch == CPU_UNKNOWN && mips_cpu != CPU_UNKNOWN)
1147 {
1148 ci = mips_cpu_info_from_cpu (mips_cpu);
1149 assert (ci != NULL);
1150 mips_arch = ci->cpu;
1151 as_warn (_("The -mcpu option is deprecated. Please use -march and "
1152 "-mtune instead."));
1153 }
1154
1155 /* At this point, mips_arch will either be CPU_UNKNOWN if no ARCH was
1156 specified on the command line, or some other value if one was.
1157 Similarly, mips_opts.isa will be ISA_UNKNOWN if not specified on
1158 the command line, or will be set otherwise if one was. */
1159
ec68c924 1160 if (mips_arch != CPU_UNKNOWN && mips_opts.isa != ISA_UNKNOWN)
252b5132 1161 {
1ff54a84
TS
1162 /* We have to check if the isa is the default isa of arch. Otherwise
1163 we'll get invalid object file headers. */
1164 ci = mips_cpu_info_from_cpu (mips_arch);
1165 assert (ci != NULL);
1166 if (mips_opts.isa != ci->isa)
1167 {
1168 /* This really should be an error instead of a warning, but old
1169 compilers only have -mcpu which sets both arch and tune. For
1170 now, we discard arch and preserve tune. */
1171 as_warn (_("The -march option is incompatible to -mipsN and "
1172 "therefore ignored."));
1173 if (mips_tune == CPU_UNKNOWN)
1174 mips_tune = mips_arch;
1175 ci = mips_cpu_info_from_isa (mips_opts.isa);
1176 assert (ci != NULL);
1177 mips_arch = ci->cpu;
1178 }
252b5132 1179 }
a8e8e863 1180#endif
ec68c924 1181 else if (mips_arch != CPU_UNKNOWN && mips_opts.isa == ISA_UNKNOWN)
252b5132 1182 {
ec68c924
EC
1183 /* We have ARCH, we need ISA. */
1184 ci = mips_cpu_info_from_cpu (mips_arch);
e7af610e
NC
1185 assert (ci != NULL);
1186 mips_opts.isa = ci->isa;
1187 }
ec68c924 1188 else if (mips_arch == CPU_UNKNOWN && mips_opts.isa != ISA_UNKNOWN)
e7af610e 1189 {
ec68c924 1190 /* We have ISA, we need default ARCH. */
e7af610e
NC
1191 ci = mips_cpu_info_from_isa (mips_opts.isa);
1192 assert (ci != NULL);
ec68c924
EC
1193 mips_arch = ci->cpu;
1194 }
e7af610e
NC
1195 else
1196 {
ec68c924 1197 /* We need to set both ISA and ARCH from target cpu. */
e7af610e
NC
1198 ci = mips_cpu_info_from_name (cpu);
1199 if (ci == NULL)
beae10d5 1200 ci = mips_cpu_info_from_cpu (CPU_R3000);
e7af610e
NC
1201 assert (ci != NULL);
1202 mips_opts.isa = ci->isa;
ec68c924 1203 mips_arch = ci->cpu;
252b5132
RH
1204 }
1205
ec68c924
EC
1206 if (mips_tune == CPU_UNKNOWN)
1207 mips_tune = mips_arch;
1208
1209 ci = mips_cpu_info_from_cpu (mips_arch);
e7af610e
NC
1210 assert (ci != NULL);
1211 mips_isa_from_cpu = ci->isa;
1212
252b5132 1213 /* End of TARGET_CPU processing, get rid of malloced memory
bdaaa2e1 1214 if necessary. */
252b5132
RH
1215 cpu = NULL;
1216 if (a != NULL)
1217 {
156c2f8b
NC
1218 free (a);
1219 a = NULL;
252b5132
RH
1220 }
1221
e7af610e 1222 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
252b5132
RH
1223 as_bad (_("trap exception not supported at ISA 1"));
1224
1225 /* Set the EABI kind based on the ISA before the user gets
1226 to change the ISA with directives. This isn't really
bdaaa2e1 1227 the best, but then neither is basing the abi on the isa. */
9ce8a5dd 1228 if (ISA_HAS_64BIT_REGS (mips_opts.isa)
a325df1d 1229 && mips_opts.abi == EABI_ABI)
252b5132
RH
1230 mips_eabi64 = 1;
1231
e7af610e
NC
1232 /* If they asked for mips1 or mips2 and a cpu that is
1233 mips3 or greater, then mark the object file 32BITMODE. */
1234 if (mips_isa_from_cpu != ISA_UNKNOWN
1235 && ! ISA_HAS_64BIT_REGS (mips_opts.isa)
1236 && ISA_HAS_64BIT_REGS (mips_isa_from_cpu))
1237 mips_32bitmode = 1;
252b5132 1238
1f25f5d3
CD
1239 /* If the selected architecture includes support for ASEs, enable
1240 generation of code for them. */
1241 if (mips_opts.ase_mips3d == -1 && CPU_HAS_MIPS3D (mips_arch))
1242 mips_opts.ase_mips3d = 1;
1243
ec68c924 1244 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, mips_arch))
252b5132
RH
1245 as_warn (_("Could not set architecture and machine"));
1246
a8e8e863
DJ
1247 if (file_mips_gp32 < 0)
1248 file_mips_gp32 = 0;
1249 if (file_mips_fp32 < 0)
1250 file_mips_fp32 = 0;
1251
252b5132 1252 file_mips_isa = mips_opts.isa;
a325df1d 1253 file_mips_abi = mips_opts.abi;
1f25f5d3 1254 file_ase_mips3d = mips_opts.ase_mips3d;
a325df1d
TS
1255 mips_opts.gp32 = file_mips_gp32;
1256 mips_opts.fp32 = file_mips_fp32;
252b5132
RH
1257
1258 op_hash = hash_new ();
1259
1260 for (i = 0; i < NUMOPCODES;)
1261 {
1262 const char *name = mips_opcodes[i].name;
1263
1264 retval = hash_insert (op_hash, name, (PTR) &mips_opcodes[i]);
1265 if (retval != NULL)
1266 {
1267 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1268 mips_opcodes[i].name, retval);
1269 /* Probably a memory allocation problem? Give up now. */
1270 as_fatal (_("Broken assembler. No assembly attempted."));
1271 }
1272 do
1273 {
1274 if (mips_opcodes[i].pinfo != INSN_MACRO)
1275 {
1276 if (!validate_mips_insn (&mips_opcodes[i]))
1277 broken = 1;
1278 }
1279 ++i;
1280 }
1281 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1282 }
1283
1284 mips16_op_hash = hash_new ();
1285
1286 i = 0;
1287 while (i < bfd_mips16_num_opcodes)
1288 {
1289 const char *name = mips16_opcodes[i].name;
1290
1291 retval = hash_insert (mips16_op_hash, name, (PTR) &mips16_opcodes[i]);
1292 if (retval != NULL)
1293 as_fatal (_("internal: can't hash `%s': %s"),
1294 mips16_opcodes[i].name, retval);
1295 do
1296 {
1297 if (mips16_opcodes[i].pinfo != INSN_MACRO
1298 && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1299 != mips16_opcodes[i].match))
1300 {
1301 fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1302 mips16_opcodes[i].name, mips16_opcodes[i].args);
1303 broken = 1;
1304 }
1305 ++i;
1306 }
1307 while (i < bfd_mips16_num_opcodes
1308 && strcmp (mips16_opcodes[i].name, name) == 0);
1309 }
1310
1311 if (broken)
1312 as_fatal (_("Broken assembler. No assembly attempted."));
1313
1314 /* We add all the general register names to the symbol table. This
1315 helps us detect invalid uses of them. */
1316 for (i = 0; i < 32; i++)
1317 {
1318 char buf[5];
1319
1320 sprintf (buf, "$%d", i);
1321 symbol_table_insert (symbol_new (buf, reg_section, i,
1322 &zero_address_frag));
1323 }
1324 symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1325 &zero_address_frag));
1326 symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1327 &zero_address_frag));
1328 symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1329 &zero_address_frag));
1330 symbol_table_insert (symbol_new ("$at", reg_section, AT,
1331 &zero_address_frag));
1332 symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1333 &zero_address_frag));
1334 symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1335 &zero_address_frag));
1336 symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1337 &zero_address_frag));
1338
1339 mips_no_prev_insn (false);
1340
1341 mips_gprmask = 0;
1342 mips_cprmask[0] = 0;
1343 mips_cprmask[1] = 0;
1344 mips_cprmask[2] = 0;
1345 mips_cprmask[3] = 0;
1346
1347 /* set the default alignment for the text section (2**2) */
1348 record_alignment (text_section, 2);
1349
1350 if (USE_GLOBAL_POINTER_OPT)
1351 bfd_set_gp_size (stdoutput, g_switch_value);
1352
1353 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1354 {
1355 /* On a native system, sections must be aligned to 16 byte
1356 boundaries. When configured for an embedded ELF target, we
1357 don't bother. */
1358 if (strcmp (TARGET_OS, "elf") != 0)
1359 {
1360 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1361 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1362 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1363 }
1364
1365 /* Create a .reginfo section for register masks and a .mdebug
1366 section for debugging information. */
1367 {
1368 segT seg;
1369 subsegT subseg;
1370 flagword flags;
1371 segT sec;
1372
1373 seg = now_seg;
1374 subseg = now_subseg;
1375
1376 /* The ABI says this section should be loaded so that the
1377 running program can access it. However, we don't load it
1378 if we are configured for an embedded target */
1379 flags = SEC_READONLY | SEC_DATA;
1380 if (strcmp (TARGET_OS, "elf") != 0)
1381 flags |= SEC_ALLOC | SEC_LOAD;
1382
195325d2 1383 if (file_mips_abi != N64_ABI)
252b5132
RH
1384 {
1385 sec = subseg_new (".reginfo", (subsegT) 0);
1386
195325d2
TS
1387 bfd_set_section_flags (stdoutput, sec, flags);
1388 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
bdaaa2e1 1389
252b5132
RH
1390#ifdef OBJ_ELF
1391 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1392#endif
1393 }
1394 else
1395 {
1396 /* The 64-bit ABI uses a .MIPS.options section rather than
1397 .reginfo section. */
1398 sec = subseg_new (".MIPS.options", (subsegT) 0);
195325d2
TS
1399 bfd_set_section_flags (stdoutput, sec, flags);
1400 bfd_set_section_alignment (stdoutput, sec, 3);
252b5132
RH
1401
1402#ifdef OBJ_ELF
1403 /* Set up the option header. */
1404 {
1405 Elf_Internal_Options opthdr;
1406 char *f;
1407
1408 opthdr.kind = ODK_REGINFO;
1409 opthdr.size = (sizeof (Elf_External_Options)
1410 + sizeof (Elf64_External_RegInfo));
1411 opthdr.section = 0;
1412 opthdr.info = 0;
1413 f = frag_more (sizeof (Elf_External_Options));
1414 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1415 (Elf_External_Options *) f);
1416
1417 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1418 }
1419#endif
1420 }
1421
1422 if (ECOFF_DEBUGGING)
1423 {
1424 sec = subseg_new (".mdebug", (subsegT) 0);
1425 (void) bfd_set_section_flags (stdoutput, sec,
1426 SEC_HAS_CONTENTS | SEC_READONLY);
1427 (void) bfd_set_section_alignment (stdoutput, sec, 2);
1428 }
1429
1430#ifdef MIPS_STABS_ELF
1431 pdr_seg = subseg_new (".pdr", (subsegT) 0);
1432 (void) bfd_set_section_flags (stdoutput, pdr_seg,
1433 SEC_READONLY | SEC_RELOC | SEC_DEBUGGING);
1434 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1435#endif
1436
1437 subseg_set (seg, subseg);
1438 }
1439 }
1440
1441 if (! ECOFF_DEBUGGING)
1442 md_obj_begin ();
1443}
1444
1445void
1446md_mips_end ()
1447{
1448 if (! ECOFF_DEBUGGING)
1449 md_obj_end ();
1450}
1451
1452void
1453md_assemble (str)
1454 char *str;
1455{
1456 struct mips_cl_insn insn;
f6688943
TS
1457 bfd_reloc_code_real_type unused_reloc[3]
1458 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132
RH
1459
1460 imm_expr.X_op = O_absent;
252b5132
RH
1461 imm_unmatched_hi = false;
1462 offset_expr.X_op = O_absent;
f6688943
TS
1463 imm_reloc[0] = BFD_RELOC_UNUSED;
1464 imm_reloc[1] = BFD_RELOC_UNUSED;
1465 imm_reloc[2] = BFD_RELOC_UNUSED;
1466 offset_reloc[0] = BFD_RELOC_UNUSED;
1467 offset_reloc[1] = BFD_RELOC_UNUSED;
1468 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
1469
1470 if (mips_opts.mips16)
1471 mips16_ip (str, &insn);
1472 else
1473 {
1474 mips_ip (str, &insn);
beae10d5
KH
1475 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1476 str, insn.insn_opcode));
252b5132
RH
1477 }
1478
1479 if (insn_error)
1480 {
1481 as_bad ("%s `%s'", insn_error, str);
1482 return;
1483 }
1484
1485 if (insn.insn_mo->pinfo == INSN_MACRO)
1486 {
1487 if (mips_opts.mips16)
1488 mips16_macro (&insn);
1489 else
1490 macro (&insn);
1491 }
1492 else
1493 {
1494 if (imm_expr.X_op != O_absent)
c4e7957c 1495 append_insn (NULL, &insn, &imm_expr, imm_reloc, imm_unmatched_hi);
252b5132 1496 else if (offset_expr.X_op != O_absent)
c4e7957c 1497 append_insn (NULL, &insn, &offset_expr, offset_reloc, false);
252b5132 1498 else
c4e7957c 1499 append_insn (NULL, &insn, NULL, unused_reloc, false);
252b5132
RH
1500 }
1501}
1502
1503/* See whether instruction IP reads register REG. CLASS is the type
1504 of register. */
1505
1506static int
1507insn_uses_reg (ip, reg, class)
1508 struct mips_cl_insn *ip;
1509 unsigned int reg;
1510 enum mips_regclass class;
1511{
1512 if (class == MIPS16_REG)
1513 {
1514 assert (mips_opts.mips16);
1515 reg = mips16_to_32_reg_map[reg];
1516 class = MIPS_GR_REG;
1517 }
1518
1519 /* Don't report on general register 0, since it never changes. */
1520 if (class == MIPS_GR_REG && reg == 0)
1521 return 0;
1522
1523 if (class == MIPS_FP_REG)
1524 {
1525 assert (! mips_opts.mips16);
1526 /* If we are called with either $f0 or $f1, we must check $f0.
1527 This is not optimal, because it will introduce an unnecessary
1528 NOP between "lwc1 $f0" and "swc1 $f1". To fix this we would
1529 need to distinguish reading both $f0 and $f1 or just one of
1530 them. Note that we don't have to check the other way,
1531 because there is no instruction that sets both $f0 and $f1
1532 and requires a delay. */
1533 if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1534 && ((((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS) &~(unsigned)1)
1535 == (reg &~ (unsigned) 1)))
1536 return 1;
1537 if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1538 && ((((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT) &~(unsigned)1)
1539 == (reg &~ (unsigned) 1)))
1540 return 1;
1541 }
1542 else if (! mips_opts.mips16)
1543 {
1544 if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1545 && ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == reg)
1546 return 1;
1547 if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1548 && ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT) == reg)
1549 return 1;
1550 }
1551 else
1552 {
1553 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1554 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RX)
1555 & MIPS16OP_MASK_RX)]
1556 == reg))
1557 return 1;
1558 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1559 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RY)
1560 & MIPS16OP_MASK_RY)]
1561 == reg))
1562 return 1;
1563 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1564 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
1565 & MIPS16OP_MASK_MOVE32Z)]
1566 == reg))
1567 return 1;
1568 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1569 return 1;
1570 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1571 return 1;
1572 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1573 return 1;
1574 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1575 && ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
1576 & MIPS16OP_MASK_REGR32) == reg)
1577 return 1;
1578 }
1579
1580 return 0;
1581}
1582
1583/* This function returns true if modifying a register requires a
1584 delay. */
1585
1586static int
1587reg_needs_delay (reg)
156c2f8b 1588 unsigned int reg;
252b5132
RH
1589{
1590 unsigned long prev_pinfo;
1591
1592 prev_pinfo = prev_insn.insn_mo->pinfo;
1593 if (! mips_opts.noreorder
9ce8a5dd 1594 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
1595 && ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1596 || (! gpr_interlocks
1597 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1598 {
1599 /* A load from a coprocessor or from memory. All load
1600 delays delay the use of general register rt for one
1601 instruction on the r3000. The r6000 and r4000 use
1602 interlocks. */
bdaaa2e1 1603 /* Itbl support may require additional care here. */
252b5132
RH
1604 know (prev_pinfo & INSN_WRITE_GPR_T);
1605 if (reg == ((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT))
1606 return 1;
1607 }
1608
1609 return 0;
1610}
1611
1612/* Mark instruction labels in mips16 mode. This permits the linker to
1613 handle them specially, such as generating jalx instructions when
1614 needed. We also make them odd for the duration of the assembly, in
1615 order to generate the right sort of code. We will make them even
1616 in the adjust_symtab routine, while leaving them marked. This is
1617 convenient for the debugger and the disassembler. The linker knows
1618 to make them odd again. */
1619
1620static void
1621mips16_mark_labels ()
1622{
1623 if (mips_opts.mips16)
1624 {
1625 struct insn_label_list *l;
98aa84af 1626 valueT val;
252b5132
RH
1627
1628 for (l = insn_labels; l != NULL; l = l->next)
1629 {
1630#ifdef OBJ_ELF
1631 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1632 S_SET_OTHER (l->label, STO_MIPS16);
1633#endif
98aa84af
AM
1634 val = S_GET_VALUE (l->label);
1635 if ((val & 1) == 0)
1636 S_SET_VALUE (l->label, val + 1);
252b5132
RH
1637 }
1638 }
1639}
1640
1641/* Output an instruction. PLACE is where to put the instruction; if
1642 it is NULL, this uses frag_more to get room. IP is the instruction
1643 information. ADDRESS_EXPR is an operand of the instruction to be
1644 used with RELOC_TYPE. */
1645
1646static void
1647append_insn (place, ip, address_expr, reloc_type, unmatched_hi)
1648 char *place;
1649 struct mips_cl_insn *ip;
1650 expressionS *address_expr;
f6688943 1651 bfd_reloc_code_real_type *reloc_type;
252b5132
RH
1652 boolean unmatched_hi;
1653{
1654 register unsigned long prev_pinfo, pinfo;
1655 char *f;
f6688943 1656 fixS *fixp[3];
252b5132
RH
1657 int nops = 0;
1658
1659 /* Mark instruction labels in mips16 mode. */
f9419b05 1660 mips16_mark_labels ();
252b5132
RH
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 1817 {
f9419b05 1818 ++nops;
6b76fefe 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)))
f9419b05 1844 ++nops;
252b5132
RH
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)))
f9419b05 1866 ++nops;
252b5132
RH
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)
f9419b05 1953 ++val;
98aa84af 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 {
4db1a35d 2040 valueT tmp;
f6688943
TS
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
98d3f06f
KH
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))
f6688943
TS
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))
98d3f06f 2185 fixp[1]->fx_no_overflow = 1;
f6688943
TS
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))
98d3f06f 2217 fixp[2]->fx_no_overflow = 1;
f6688943
TS
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)
f9419b05 2251 mips_gprmask |= 1 << RA;
252b5132
RH
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)
f9419b05 2444 == RA))))
252b5132
RH
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)
f9419b05 2453 == RA))))
252b5132
RH
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)
f9419b05 2471 && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
252b5132
RH
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)
f9419b05 2829 ++val;
98aa84af 2830 S_SET_VALUE (l->label, val);
252b5132
RH
2831 }
2832 }
2833 }
2834
2835 /* Mark instruction labels in mips16 mode. */
f9419b05 2836 if (insns)
252b5132
RH
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
80cc45a5
EC
2884 /*
2885 * If the macro is about to expand into a second instruction,
2886 * and it is in a delay slot, print a warning.
2887 */
2888 if (place == NULL
2889 && *counter == 1
2890 && mips_opts.noreorder
2891 && (prev_prev_insn.insn_mo->pinfo
2892 & (INSN_UNCOND_BRANCH_DELAY | INSN_COND_BRANCH_DELAY
2b2e39bf 2893 | INSN_COND_BRANCH_LIKELY)) != 0)
80cc45a5
EC
2894 as_warn (_("Macro instruction expanded into multiple instructions in a branch delay slot"));
2895
252b5132 2896 if (place == NULL)
f9419b05 2897 ++*counter; /* bump instruction counter */
252b5132
RH
2898
2899 if (mips_opts.mips16)
2900 {
2901 mips16_macro_build (place, counter, ep, name, fmt, args);
2902 va_end (args);
2903 return;
2904 }
2905
f6688943
TS
2906 r[0] = BFD_RELOC_UNUSED;
2907 r[1] = BFD_RELOC_UNUSED;
2908 r[2] = BFD_RELOC_UNUSED;
252b5132
RH
2909 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
2910 assert (insn.insn_mo);
2911 assert (strcmp (name, insn.insn_mo->name) == 0);
2912
2913 /* Search until we get a match for NAME. */
2914 while (1)
2915 {
98d3f06f 2916 /* It is assumed here that macros will never generate
af55c2e6 2917 MIPS-3D instructions. */
252b5132
RH
2918 if (strcmp (fmt, insn.insn_mo->args) == 0
2919 && insn.insn_mo->pinfo != INSN_MACRO
af55c2e6 2920 && OPCODE_IS_MEMBER (insn.insn_mo, mips_opts.isa, mips_arch)
ec68c924 2921 && (mips_arch != CPU_R4650 || (insn.insn_mo->pinfo & FP_D) == 0))
252b5132
RH
2922 break;
2923
2924 ++insn.insn_mo;
2925 assert (insn.insn_mo->name);
2926 assert (strcmp (name, insn.insn_mo->name) == 0);
2927 }
2928
2929 insn.insn_opcode = insn.insn_mo->match;
2930 for (;;)
2931 {
2932 switch (*fmt++)
2933 {
2934 case '\0':
2935 break;
2936
2937 case ',':
2938 case '(':
2939 case ')':
2940 continue;
2941
2942 case 't':
2943 case 'w':
2944 case 'E':
38487616 2945 insn.insn_opcode |= va_arg (args, int) << OP_SH_RT;
252b5132
RH
2946 continue;
2947
2948 case 'c':
38487616
TS
2949 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE;
2950 continue;
2951
252b5132
RH
2952 case 'T':
2953 case 'W':
38487616 2954 insn.insn_opcode |= va_arg (args, int) << OP_SH_FT;
252b5132
RH
2955 continue;
2956
2957 case 'd':
2958 case 'G':
38487616 2959 insn.insn_opcode |= va_arg (args, int) << OP_SH_RD;
252b5132
RH
2960 continue;
2961
4372b673
NC
2962 case 'U':
2963 {
2964 int tmp = va_arg (args, int);
2965
38487616
TS
2966 insn.insn_opcode |= tmp << OP_SH_RT;
2967 insn.insn_opcode |= tmp << OP_SH_RD;
beae10d5 2968 continue;
4372b673
NC
2969 }
2970
252b5132
RH
2971 case 'V':
2972 case 'S':
38487616 2973 insn.insn_opcode |= va_arg (args, int) << OP_SH_FS;
252b5132
RH
2974 continue;
2975
2976 case 'z':
2977 continue;
2978
2979 case '<':
38487616 2980 insn.insn_opcode |= va_arg (args, int) << OP_SH_SHAMT;
252b5132
RH
2981 continue;
2982
2983 case 'D':
38487616 2984 insn.insn_opcode |= va_arg (args, int) << OP_SH_FD;
252b5132
RH
2985 continue;
2986
2987 case 'B':
38487616 2988 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE20;
252b5132
RH
2989 continue;
2990
4372b673 2991 case 'J':
38487616 2992 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE19;
4372b673
NC
2993 continue;
2994
252b5132 2995 case 'q':
38487616 2996 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE2;
252b5132
RH
2997 continue;
2998
2999 case 'b':
3000 case 's':
3001 case 'r':
3002 case 'v':
38487616 3003 insn.insn_opcode |= va_arg (args, int) << OP_SH_RS;
252b5132
RH
3004 continue;
3005
3006 case 'i':
3007 case 'j':
3008 case 'o':
f6688943 3009 *r = (bfd_reloc_code_real_type) va_arg (args, int);
cdf6fd85 3010 assert (*r == BFD_RELOC_GPREL16
f6688943
TS
3011 || *r == BFD_RELOC_MIPS_LITERAL
3012 || *r == BFD_RELOC_MIPS_HIGHER
3013 || *r == BFD_RELOC_HI16_S
3014 || *r == BFD_RELOC_LO16
3015 || *r == BFD_RELOC_MIPS_GOT16
3016 || *r == BFD_RELOC_MIPS_CALL16
3017 || *r == BFD_RELOC_MIPS_GOT_LO16
3018 || *r == BFD_RELOC_MIPS_CALL_LO16
252b5132 3019 || (ep->X_op == O_subtract
f6688943 3020 && *r == BFD_RELOC_PCREL_LO16));
252b5132
RH
3021 continue;
3022
3023 case 'u':
f6688943 3024 *r = (bfd_reloc_code_real_type) va_arg (args, int);
252b5132
RH
3025 assert (ep != NULL
3026 && (ep->X_op == O_constant
3027 || (ep->X_op == O_symbol
f6688943
TS
3028 && (*r == BFD_RELOC_MIPS_HIGHEST
3029 || *r == BFD_RELOC_HI16_S
3030 || *r == BFD_RELOC_HI16
3031 || *r == BFD_RELOC_GPREL16
3032 || *r == BFD_RELOC_MIPS_GOT_HI16
3033 || *r == BFD_RELOC_MIPS_CALL_HI16))
252b5132 3034 || (ep->X_op == O_subtract
f6688943 3035 && *r == BFD_RELOC_PCREL_HI16_S)));
252b5132
RH
3036 continue;
3037
3038 case 'p':
3039 assert (ep != NULL);
3040 /*
3041 * This allows macro() to pass an immediate expression for
3042 * creating short branches without creating a symbol.
3043 * Note that the expression still might come from the assembly
3044 * input, in which case the value is not checked for range nor
3045 * is a relocation entry generated (yuck).
3046 */
3047 if (ep->X_op == O_constant)
3048 {
3049 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
3050 ep = NULL;
3051 }
3052 else
cb56d3d3 3053 if (mips_pic == EMBEDDED_PIC)
f6688943 3054 *r = BFD_RELOC_16_PCREL_S2;
cb56d3d3 3055 else
f6688943 3056 *r = BFD_RELOC_16_PCREL;
252b5132
RH
3057 continue;
3058
3059 case 'a':
3060 assert (ep != NULL);
f6688943 3061 *r = BFD_RELOC_MIPS_JMP;
252b5132
RH
3062 continue;
3063
3064 case 'C':
3065 insn.insn_opcode |= va_arg (args, unsigned long);
3066 continue;
3067
3068 default:
3069 internalError ();
3070 }
3071 break;
3072 }
3073 va_end (args);
f6688943 3074 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132
RH
3075
3076 append_insn (place, &insn, ep, r, false);
3077}
3078
3079static void
3080mips16_macro_build (place, counter, ep, name, fmt, args)
3081 char *place;
43841e91 3082 int *counter ATTRIBUTE_UNUSED;
252b5132
RH
3083 expressionS *ep;
3084 const char *name;
3085 const char *fmt;
3086 va_list args;
3087{
3088 struct mips_cl_insn insn;
f6688943
TS
3089 bfd_reloc_code_real_type r[3]
3090 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 3091
252b5132
RH
3092 insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
3093 assert (insn.insn_mo);
3094 assert (strcmp (name, insn.insn_mo->name) == 0);
3095
3096 while (strcmp (fmt, insn.insn_mo->args) != 0
3097 || insn.insn_mo->pinfo == INSN_MACRO)
3098 {
3099 ++insn.insn_mo;
3100 assert (insn.insn_mo->name);
3101 assert (strcmp (name, insn.insn_mo->name) == 0);
3102 }
3103
3104 insn.insn_opcode = insn.insn_mo->match;
3105 insn.use_extend = false;
3106
3107 for (;;)
3108 {
3109 int c;
3110
3111 c = *fmt++;
3112 switch (c)
3113 {
3114 case '\0':
3115 break;
3116
3117 case ',':
3118 case '(':
3119 case ')':
3120 continue;
3121
3122 case 'y':
3123 case 'w':
3124 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
3125 continue;
3126
3127 case 'x':
3128 case 'v':
3129 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
3130 continue;
3131
3132 case 'z':
3133 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
3134 continue;
3135
3136 case 'Z':
3137 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
3138 continue;
3139
3140 case '0':
3141 case 'S':
3142 case 'P':
3143 case 'R':
3144 continue;
3145
3146 case 'X':
3147 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
3148 continue;
3149
3150 case 'Y':
3151 {
3152 int regno;
3153
3154 regno = va_arg (args, int);
3155 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
3156 insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
3157 }
3158 continue;
3159
3160 case '<':
3161 case '>':
3162 case '4':
3163 case '5':
3164 case 'H':
3165 case 'W':
3166 case 'D':
3167 case 'j':
3168 case '8':
3169 case 'V':
3170 case 'C':
3171 case 'U':
3172 case 'k':
3173 case 'K':
3174 case 'p':
3175 case 'q':
3176 {
3177 assert (ep != NULL);
3178
3179 if (ep->X_op != O_constant)
874e8986 3180 *r = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
3181 else
3182 {
c4e7957c
TS
3183 mips16_immed (NULL, 0, c, ep->X_add_number, false, false,
3184 false, &insn.insn_opcode, &insn.use_extend,
3185 &insn.extend);
252b5132 3186 ep = NULL;
f6688943 3187 *r = BFD_RELOC_UNUSED;
252b5132
RH
3188 }
3189 }
3190 continue;
3191
3192 case '6':
3193 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
3194 continue;
3195 }
3196
3197 break;
3198 }
3199
f6688943 3200 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132
RH
3201
3202 append_insn (place, &insn, ep, r, false);
3203}
3204
3205/*
3206 * Generate a "lui" instruction.
3207 */
3208static void
3209macro_build_lui (place, counter, ep, regnum)
3210 char *place;
3211 int *counter;
3212 expressionS *ep;
3213 int regnum;
3214{
3215 expressionS high_expr;
3216 struct mips_cl_insn insn;
f6688943
TS
3217 bfd_reloc_code_real_type r[3]
3218 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132
RH
3219 CONST char *name = "lui";
3220 CONST char *fmt = "t,u";
3221
3222 assert (! mips_opts.mips16);
3223
3224 if (place == NULL)
3225 high_expr = *ep;
3226 else
3227 {
3228 high_expr.X_op = O_constant;
3229 high_expr.X_add_number = ep->X_add_number;
3230 }
3231
3232 if (high_expr.X_op == O_constant)
3233 {
3234 /* we can compute the instruction now without a relocation entry */
e7d556df
TS
3235 high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3236 >> 16) & 0xffff;
f6688943 3237 *r = BFD_RELOC_UNUSED;
252b5132 3238 }
f6688943 3239 else if (! HAVE_NEWABI)
252b5132
RH
3240 {
3241 assert (ep->X_op == O_symbol);
3242 /* _gp_disp is a special case, used from s_cpload. */
3243 assert (mips_pic == NO_PIC
3244 || strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0);
f6688943 3245 *r = BFD_RELOC_HI16_S;
252b5132
RH
3246 }
3247
3248 /*
3249 * If the macro is about to expand into a second instruction,
3250 * print a warning if needed. We need to pass ip as a parameter
3251 * to generate a better warning message here...
3252 */
3253 if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
3254 as_warn (_("Macro instruction expanded into multiple instructions"));
3255
3256 if (place == NULL)
f9419b05 3257 ++*counter; /* bump instruction counter */
252b5132
RH
3258
3259 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3260 assert (insn.insn_mo);
3261 assert (strcmp (name, insn.insn_mo->name) == 0);
3262 assert (strcmp (fmt, insn.insn_mo->args) == 0);
3263
3264 insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
f6688943 3265 if (*r == BFD_RELOC_UNUSED)
252b5132
RH
3266 {
3267 insn.insn_opcode |= high_expr.X_add_number;
3268 append_insn (place, &insn, NULL, r, false);
3269 }
3270 else
3271 append_insn (place, &insn, &high_expr, r, false);
3272}
3273
3274/* set_at()
3275 * Generates code to set the $at register to true (one)
3276 * if reg is less than the immediate expression.
3277 */
3278static void
3279set_at (counter, reg, unsignedp)
3280 int *counter;
3281 int reg;
3282 int unsignedp;
3283{
3284 if (imm_expr.X_op == O_constant
3285 && imm_expr.X_add_number >= -0x8000
3286 && imm_expr.X_add_number < 0x8000)
3287 macro_build ((char *) NULL, counter, &imm_expr,
3288 unsignedp ? "sltiu" : "slti",
3289 "t,r,j", AT, reg, (int) BFD_RELOC_LO16);
3290 else
3291 {
4d34fb5f 3292 load_register (counter, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9 3293 macro_build ((char *) NULL, counter, (expressionS *) NULL,
252b5132
RH
3294 unsignedp ? "sltu" : "slt",
3295 "d,v,t", AT, reg, AT);
3296 }
3297}
3298
3299/* Warn if an expression is not a constant. */
3300
3301static void
3302check_absolute_expr (ip, ex)
3303 struct mips_cl_insn *ip;
3304 expressionS *ex;
3305{
3306 if (ex->X_op == O_big)
3307 as_bad (_("unsupported large constant"));
3308 else if (ex->X_op != O_constant)
3309 as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
3310}
3311
3312/* Count the leading zeroes by performing a binary chop. This is a
3313 bulky bit of source, but performance is a LOT better for the
3314 majority of values than a simple loop to count the bits:
3315 for (lcnt = 0; (lcnt < 32); lcnt++)
3316 if ((v) & (1 << (31 - lcnt)))
3317 break;
3318 However it is not code size friendly, and the gain will drop a bit
3319 on certain cached systems.
3320*/
3321#define COUNT_TOP_ZEROES(v) \
3322 (((v) & ~0xffff) == 0 \
3323 ? ((v) & ~0xff) == 0 \
3324 ? ((v) & ~0xf) == 0 \
3325 ? ((v) & ~0x3) == 0 \
3326 ? ((v) & ~0x1) == 0 \
3327 ? !(v) \
3328 ? 32 \
3329 : 31 \
3330 : 30 \
3331 : ((v) & ~0x7) == 0 \
3332 ? 29 \
3333 : 28 \
3334 : ((v) & ~0x3f) == 0 \
3335 ? ((v) & ~0x1f) == 0 \
3336 ? 27 \
3337 : 26 \
3338 : ((v) & ~0x7f) == 0 \
3339 ? 25 \
3340 : 24 \
3341 : ((v) & ~0xfff) == 0 \
3342 ? ((v) & ~0x3ff) == 0 \
3343 ? ((v) & ~0x1ff) == 0 \
3344 ? 23 \
3345 : 22 \
3346 : ((v) & ~0x7ff) == 0 \
3347 ? 21 \
3348 : 20 \
3349 : ((v) & ~0x3fff) == 0 \
3350 ? ((v) & ~0x1fff) == 0 \
3351 ? 19 \
3352 : 18 \
3353 : ((v) & ~0x7fff) == 0 \
3354 ? 17 \
3355 : 16 \
3356 : ((v) & ~0xffffff) == 0 \
3357 ? ((v) & ~0xfffff) == 0 \
3358 ? ((v) & ~0x3ffff) == 0 \
3359 ? ((v) & ~0x1ffff) == 0 \
3360 ? 15 \
3361 : 14 \
3362 : ((v) & ~0x7ffff) == 0 \
3363 ? 13 \
3364 : 12 \
3365 : ((v) & ~0x3fffff) == 0 \
3366 ? ((v) & ~0x1fffff) == 0 \
3367 ? 11 \
3368 : 10 \
3369 : ((v) & ~0x7fffff) == 0 \
3370 ? 9 \
3371 : 8 \
3372 : ((v) & ~0xfffffff) == 0 \
3373 ? ((v) & ~0x3ffffff) == 0 \
3374 ? ((v) & ~0x1ffffff) == 0 \
3375 ? 7 \
3376 : 6 \
3377 : ((v) & ~0x7ffffff) == 0 \
3378 ? 5 \
3379 : 4 \
3380 : ((v) & ~0x3fffffff) == 0 \
3381 ? ((v) & ~0x1fffffff) == 0 \
3382 ? 3 \
3383 : 2 \
3384 : ((v) & ~0x7fffffff) == 0 \
3385 ? 1 \
3386 : 0)
3387
6373ee54
CD
3388/* Is the given value a sign-extended 32-bit value? */
3389#define IS_SEXT_32BIT_NUM(x) \
3390 (((x) &~ (offsetT) 0x7fffffff) == 0 \
3391 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
3392
252b5132
RH
3393/* load_register()
3394 * This routine generates the least number of instructions neccessary to load
3395 * an absolute expression value into a register.
3396 */
3397static void
3398load_register (counter, reg, ep, dbl)
3399 int *counter;
3400 int reg;
3401 expressionS *ep;
3402 int dbl;
3403{
3404 int freg;
3405 expressionS hi32, lo32;
3406
3407 if (ep->X_op != O_big)
3408 {
3409 assert (ep->X_op == O_constant);
3410 if (ep->X_add_number < 0x8000
3411 && (ep->X_add_number >= 0
3412 || (ep->X_add_number >= -0x8000
3413 && (! dbl
3414 || ! ep->X_unsigned
3415 || sizeof (ep->X_add_number) > 4))))
3416 {
3417 /* We can handle 16 bit signed values with an addiu to
3418 $zero. No need to ever use daddiu here, since $zero and
3419 the result are always correct in 32 bit mode. */
3420 macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3421 (int) BFD_RELOC_LO16);
3422 return;
3423 }
3424 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3425 {
3426 /* We can handle 16 bit unsigned values with an ori to
3427 $zero. */
3428 macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, 0,
3429 (int) BFD_RELOC_LO16);
3430 return;
3431 }
6373ee54 3432 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)
252b5132
RH
3433 && (! dbl
3434 || ! ep->X_unsigned
3435 || sizeof (ep->X_add_number) > 4
3436 || (ep->X_add_number & 0x80000000) == 0))
ca4e0257 3437 || ((HAVE_32BIT_GPRS || ! dbl)
252b5132 3438 && (ep->X_add_number &~ (offsetT) 0xffffffff) == 0)
ca4e0257 3439 || (HAVE_32BIT_GPRS
252b5132
RH
3440 && ! dbl
3441 && ((ep->X_add_number &~ (offsetT) 0xffffffff)
3442 == ~ (offsetT) 0xffffffff)))
3443 {
3444 /* 32 bit values require an lui. */
3445 macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3446 (int) BFD_RELOC_HI16);
3447 if ((ep->X_add_number & 0xffff) != 0)
3448 macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, reg,
3449 (int) BFD_RELOC_LO16);
3450 return;
3451 }
3452 }
3453
3454 /* The value is larger than 32 bits. */
3455
ca4e0257 3456 if (HAVE_32BIT_GPRS)
252b5132 3457 {
956cd1d6
TS
3458 as_bad (_("Number (0x%lx) larger than 32 bits"),
3459 (unsigned long) ep->X_add_number);
252b5132
RH
3460 macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3461 (int) BFD_RELOC_LO16);
3462 return;
3463 }
3464
3465 if (ep->X_op != O_big)
3466 {
3467 hi32 = *ep;
3468 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3469 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3470 hi32.X_add_number &= 0xffffffff;
3471 lo32 = *ep;
3472 lo32.X_add_number &= 0xffffffff;
3473 }
3474 else
3475 {
3476 assert (ep->X_add_number > 2);
3477 if (ep->X_add_number == 3)
3478 generic_bignum[3] = 0;
3479 else if (ep->X_add_number > 4)
3480 as_bad (_("Number larger than 64 bits"));
3481 lo32.X_op = O_constant;
3482 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3483 hi32.X_op = O_constant;
3484 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3485 }
3486
3487 if (hi32.X_add_number == 0)
3488 freg = 0;
3489 else
3490 {
3491 int shift, bit;
3492 unsigned long hi, lo;
3493
956cd1d6 3494 if (hi32.X_add_number == (offsetT) 0xffffffff)
beae10d5
KH
3495 {
3496 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3497 {
3498 macro_build ((char *) NULL, counter, &lo32, "addiu", "t,r,j",
252b5132 3499 reg, 0, (int) BFD_RELOC_LO16);
beae10d5
KH
3500 return;
3501 }
3502 if (lo32.X_add_number & 0x80000000)
3503 {
3504 macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3505 (int) BFD_RELOC_HI16);
252b5132
RH
3506 if (lo32.X_add_number & 0xffff)
3507 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i",
3508 reg, reg, (int) BFD_RELOC_LO16);
beae10d5
KH
3509 return;
3510 }
3511 }
252b5132
RH
3512
3513 /* Check for 16bit shifted constant. We know that hi32 is
3514 non-zero, so start the mask on the first bit of the hi32
3515 value. */
3516 shift = 17;
3517 do
beae10d5
KH
3518 {
3519 unsigned long himask, lomask;
3520
3521 if (shift < 32)
3522 {
3523 himask = 0xffff >> (32 - shift);
3524 lomask = (0xffff << shift) & 0xffffffff;
3525 }
3526 else
3527 {
3528 himask = 0xffff << (shift - 32);
3529 lomask = 0;
3530 }
3531 if ((hi32.X_add_number & ~(offsetT) himask) == 0
3532 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3533 {
3534 expressionS tmp;
3535
3536 tmp.X_op = O_constant;
3537 if (shift < 32)
3538 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3539 | (lo32.X_add_number >> shift));
3540 else
3541 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
3542 macro_build ((char *) NULL, counter, &tmp,
3543 "ori", "t,r,i", reg, 0,
3544 (int) BFD_RELOC_LO16);
2396cfb9 3545 macro_build ((char *) NULL, counter, (expressionS *) NULL,
beae10d5
KH
3546 (shift >= 32) ? "dsll32" : "dsll",
3547 "d,w,<", reg, reg,
3548 (shift >= 32) ? shift - 32 : shift);
3549 return;
3550 }
f9419b05 3551 ++shift;
beae10d5
KH
3552 }
3553 while (shift <= (64 - 16));
252b5132
RH
3554
3555 /* Find the bit number of the lowest one bit, and store the
3556 shifted value in hi/lo. */
3557 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3558 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3559 if (lo != 0)
3560 {
3561 bit = 0;
3562 while ((lo & 1) == 0)
3563 {
3564 lo >>= 1;
3565 ++bit;
3566 }
3567 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3568 hi >>= bit;
3569 }
3570 else
3571 {
3572 bit = 32;
3573 while ((hi & 1) == 0)
3574 {
3575 hi >>= 1;
3576 ++bit;
3577 }
3578 lo = hi;
3579 hi = 0;
3580 }
3581
3582 /* Optimize if the shifted value is a (power of 2) - 1. */
3583 if ((hi == 0 && ((lo + 1) & lo) == 0)
3584 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
beae10d5
KH
3585 {
3586 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
252b5132 3587 if (shift != 0)
beae10d5 3588 {
252b5132
RH
3589 expressionS tmp;
3590
3591 /* This instruction will set the register to be all
3592 ones. */
beae10d5
KH
3593 tmp.X_op = O_constant;
3594 tmp.X_add_number = (offsetT) -1;
3595 macro_build ((char *) NULL, counter, &tmp, "addiu", "t,r,j",
252b5132 3596 reg, 0, (int) BFD_RELOC_LO16);
beae10d5
KH
3597 if (bit != 0)
3598 {
3599 bit += shift;
2396cfb9 3600 macro_build ((char *) NULL, counter, (expressionS *) NULL,
beae10d5
KH
3601 (bit >= 32) ? "dsll32" : "dsll",
3602 "d,w,<", reg, reg,
3603 (bit >= 32) ? bit - 32 : bit);
3604 }
2396cfb9 3605 macro_build ((char *) NULL, counter, (expressionS *) NULL,
252b5132 3606 (shift >= 32) ? "dsrl32" : "dsrl",
beae10d5 3607 "d,w,<", reg, reg,
252b5132 3608 (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
3609 return;
3610 }
3611 }
252b5132
RH
3612
3613 /* Sign extend hi32 before calling load_register, because we can
3614 generally get better code when we load a sign extended value. */
3615 if ((hi32.X_add_number & 0x80000000) != 0)
beae10d5 3616 hi32.X_add_number |= ~(offsetT) 0xffffffff;
252b5132
RH
3617 load_register (counter, reg, &hi32, 0);
3618 freg = reg;
3619 }
3620 if ((lo32.X_add_number & 0xffff0000) == 0)
3621 {
3622 if (freg != 0)
3623 {
2396cfb9
TS
3624 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3625 "dsll32", "d,w,<", reg, freg, 0);
252b5132
RH
3626 freg = reg;
3627 }
3628 }
3629 else
3630 {
3631 expressionS mid16;
3632
956cd1d6 3633 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
beae10d5 3634 {
252b5132
RH
3635 macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3636 (int) BFD_RELOC_HI16);
956cd1d6
TS
3637 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3638 "dsrl32", "d,w,<", reg, reg, 0);
beae10d5
KH
3639 return;
3640 }
252b5132
RH
3641
3642 if (freg != 0)
3643 {
956cd1d6
TS
3644 macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3645 "d,w,<", reg, freg, 16);
252b5132
RH
3646 freg = reg;
3647 }
3648 mid16 = lo32;
3649 mid16.X_add_number >>= 16;
3650 macro_build ((char *) NULL, counter, &mid16, "ori", "t,r,i", reg,
3651 freg, (int) BFD_RELOC_LO16);
956cd1d6
TS
3652 macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3653 "d,w,<", reg, reg, 16);
252b5132
RH
3654 freg = reg;
3655 }
3656 if ((lo32.X_add_number & 0xffff) != 0)
3657 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i", reg, freg,
3658 (int) BFD_RELOC_LO16);
3659}
3660
3661/* Load an address into a register. */
3662
3663static void
c9914766 3664load_address (counter, reg, ep, used_at)
252b5132
RH
3665 int *counter;
3666 int reg;
3667 expressionS *ep;
d6bc6245 3668 int *used_at;
252b5132 3669{
f9419b05 3670 char *p = NULL;
252b5132
RH
3671
3672 if (ep->X_op != O_constant
3673 && ep->X_op != O_symbol)
3674 {
3675 as_bad (_("expression too complex"));
3676 ep->X_op = O_constant;
3677 }
3678
3679 if (ep->X_op == O_constant)
3680 {
c9914766 3681 load_register (counter, reg, ep, HAVE_64BIT_ADDRESSES);
252b5132
RH
3682 return;
3683 }
3684
3685 if (mips_pic == NO_PIC)
3686 {
3687 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 3688 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
3689 Otherwise we want
3690 lui $reg,<sym> (BFD_RELOC_HI16_S)
3691 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
d6bc6245 3692 If we have an addend, we always use the latter form.
76b3015f 3693
d6bc6245
TS
3694 With 64bit address space and a usable $at we want
3695 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3696 lui $at,<sym> (BFD_RELOC_HI16_S)
3697 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3698 daddiu $at,<sym> (BFD_RELOC_LO16)
3699 dsll32 $reg,0
3700 dadd $reg,$reg,$at
76b3015f 3701
d6bc6245
TS
3702 If $at is already in use, we use an path which is suboptimal
3703 on superscalar processors.
3704 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3705 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3706 dsll $reg,16
3707 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
3708 dsll $reg,16
3709 daddiu $reg,<sym> (BFD_RELOC_LO16)
3710 */
c9914766 3711 if (HAVE_64BIT_ADDRESSES)
d6bc6245 3712 {
d6bc6245
TS
3713 /* We don't do GP optimization for now because RELAX_ENCODE can't
3714 hold the data for such large chunks. */
3715
3716 if (*used_at == 0)
3717 {
3718 macro_build (p, counter, ep, "lui", "t,u",
3719 reg, (int) BFD_RELOC_MIPS_HIGHEST);
3720 macro_build (p, counter, ep, "lui", "t,u",
3721 AT, (int) BFD_RELOC_HI16_S);
3722 macro_build (p, counter, ep, "daddiu", "t,r,j",
3723 reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3724 macro_build (p, counter, ep, "daddiu", "t,r,j",
3725 AT, AT, (int) BFD_RELOC_LO16);
2396cfb9
TS
3726 macro_build (p, counter, (expressionS *) NULL, "dsll32",
3727 "d,w,<", reg, reg, 0);
3728 macro_build (p, counter, (expressionS *) NULL, "dadd",
3729 "d,v,t", reg, reg, AT);
d6bc6245
TS
3730 *used_at = 1;
3731 }
3732 else
3733 {
3734 macro_build (p, counter, ep, "lui", "t,u",
3735 reg, (int) BFD_RELOC_MIPS_HIGHEST);
3736 macro_build (p, counter, ep, "daddiu", "t,r,j",
3737 reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
2396cfb9
TS
3738 macro_build (p, counter, (expressionS *) NULL, "dsll",
3739 "d,w,<", reg, reg, 16);
d6bc6245
TS
3740 macro_build (p, counter, ep, "daddiu", "t,r,j",
3741 reg, reg, (int) BFD_RELOC_HI16_S);
2396cfb9
TS
3742 macro_build (p, counter, (expressionS *) NULL, "dsll",
3743 "d,w,<", reg, reg, 16);
d6bc6245
TS
3744 macro_build (p, counter, ep, "daddiu", "t,r,j",
3745 reg, reg, (int) BFD_RELOC_LO16);
3746 }
3747 }
252b5132
RH
3748 else
3749 {
d6bc6245
TS
3750 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3751 && ! nopic_need_relax (ep->X_add_symbol, 1))
3752 {
3753 frag_grow (20);
3754 macro_build ((char *) NULL, counter, ep,
c9914766
TS
3755 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
3756 reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
d6bc6245
TS
3757 p = frag_var (rs_machine_dependent, 8, 0,
3758 RELAX_ENCODE (4, 8, 0, 4, 0,
3759 mips_opts.warn_about_macros),
956cd1d6 3760 ep->X_add_symbol, 0, NULL);
d6bc6245
TS
3761 }
3762 macro_build_lui (p, counter, ep, reg);
3763 if (p != NULL)
3764 p += 4;
c9914766
TS
3765 macro_build (p, counter, ep,
3766 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
d6bc6245
TS
3767 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3768 }
252b5132
RH
3769 }
3770 else if (mips_pic == SVR4_PIC && ! mips_big_got)
3771 {
3772 expressionS ex;
3773
3774 /* If this is a reference to an external symbol, we want
3775 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3776 Otherwise we want
3777 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3778 nop
3779 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3780 If there is a constant, it must be added in after. */
3781 ex.X_add_number = ep->X_add_number;
3782 ep->X_add_number = 0;
3783 frag_grow (20);
3784 macro_build ((char *) NULL, counter, ep,
c9914766
TS
3785 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
3786 reg, (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132
RH
3787 macro_build ((char *) NULL, counter, (expressionS *) NULL, "nop", "");
3788 p = frag_var (rs_machine_dependent, 4, 0,
3789 RELAX_ENCODE (0, 4, -8, 0, 0, mips_opts.warn_about_macros),
3790 ep->X_add_symbol, (offsetT) 0, (char *) NULL);
3791 macro_build (p, counter, ep,
ca4e0257 3792 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
3793 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3794 if (ex.X_add_number != 0)
3795 {
3796 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3797 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3798 ex.X_op = O_constant;
3799 macro_build ((char *) NULL, counter, &ex,
ca4e0257 3800 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
3801 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3802 }
3803 }
3804 else if (mips_pic == SVR4_PIC)
3805 {
3806 expressionS ex;
3807 int off;
3808
3809 /* This is the large GOT case. If this is a reference to an
3810 external symbol, we want
3811 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
3812 addu $reg,$reg,$gp
3813 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
3814 Otherwise, for a reference to a local symbol, we want
3815 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3816 nop
3817 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3818 If there is a constant, it must be added in after. */
3819 ex.X_add_number = ep->X_add_number;
3820 ep->X_add_number = 0;
f7ea7ef2 3821 if (reg_needs_delay (mips_gp_register))
252b5132
RH
3822 off = 4;
3823 else
3824 off = 0;
3825 frag_grow (32);
3826 macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3827 (int) BFD_RELOC_MIPS_GOT_HI16);
3828 macro_build ((char *) NULL, counter, (expressionS *) NULL,
f7ea7ef2
TS
3829 HAVE_32BIT_ADDRESSES ? "addu" : "daddu", "d,v,t", reg,
3830 reg, mips_gp_register);
3831 macro_build ((char *) NULL, counter, ep,
3832 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3833 (int) BFD_RELOC_MIPS_GOT_LO16, reg);
252b5132
RH
3834 p = frag_var (rs_machine_dependent, 12 + off, 0,
3835 RELAX_ENCODE (12, 12 + off, off, 8 + off, 0,
3836 mips_opts.warn_about_macros),
956cd1d6 3837 ep->X_add_symbol, 0, NULL);
252b5132
RH
3838 if (off > 0)
3839 {
3840 /* We need a nop before loading from $gp. This special
3841 check is required because the lui which starts the main
3842 instruction stream does not refer to $gp, and so will not
3843 insert the nop which may be required. */
3844 macro_build (p, counter, (expressionS *) NULL, "nop", "");
3845 p += 4;
3846 }
f7ea7ef2
TS
3847 macro_build (p, counter, ep, HAVE_32BIT_ADDRESSES ? "lw" : "ld",
3848 "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT16,
3849 mips_gp_register);
252b5132
RH
3850 p += 4;
3851 macro_build (p, counter, (expressionS *) NULL, "nop", "");
3852 p += 4;
f7ea7ef2 3853 macro_build (p, counter, ep, HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
3854 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3855 if (ex.X_add_number != 0)
3856 {
3857 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3858 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3859 ex.X_op = O_constant;
f7ea7ef2
TS
3860 macro_build ((char *) NULL, counter, &ex,
3861 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
3862 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3863 }
3864 }
3865 else if (mips_pic == EMBEDDED_PIC)
3866 {
3867 /* We always do
cdf6fd85 3868 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
c9914766
TS
3869 */
3870 macro_build ((char *) NULL, counter, ep,
3871 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3872 "t,r,j", reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
252b5132
RH
3873 }
3874 else
3875 abort ();
3876}
3877
ea1fb5dc
RS
3878/* Move the contents of register SOURCE into register DEST. */
3879
3880static void
3881move_register (counter, dest, source)
3882 int *counter;
3883 int dest;
3884 int source;
3885{
3886 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3887 HAVE_32BIT_GPRS ? "addu" : "daddu",
3888 "d,v,t", dest, source, 0);
3889}
3890
252b5132
RH
3891/*
3892 * Build macros
3893 * This routine implements the seemingly endless macro or synthesized
3894 * instructions and addressing modes in the mips assembly language. Many
3895 * of these macros are simple and are similar to each other. These could
3896 * probably be handled by some kind of table or grammer aproach instead of
3897 * this verbose method. Others are not simple macros but are more like
3898 * optimizing code generation.
3899 * One interesting optimization is when several store macros appear
3900 * consecutivly that would load AT with the upper half of the same address.
3901 * The ensuing load upper instructions are ommited. This implies some kind
3902 * of global optimization. We currently only optimize within a single macro.
3903 * For many of the load and store macros if the address is specified as a
3904 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
3905 * first load register 'at' with zero and use it as the base register. The
3906 * mips assembler simply uses register $zero. Just one tiny optimization
3907 * we're missing.
3908 */
3909static void
3910macro (ip)
3911 struct mips_cl_insn *ip;
3912{
3913 register int treg, sreg, dreg, breg;
3914 int tempreg;
3915 int mask;
3916 int icnt = 0;
43841e91 3917 int used_at = 0;
252b5132
RH
3918 expressionS expr1;
3919 const char *s;
3920 const char *s2;
3921 const char *fmt;
3922 int likely = 0;
3923 int dbl = 0;
3924 int coproc = 0;
3925 int lr = 0;
3926 int imm = 0;
3927 offsetT maxnum;
3928 int off;
3929 bfd_reloc_code_real_type r;
252b5132
RH
3930 int hold_mips_optimize;
3931
3932 assert (! mips_opts.mips16);
3933
3934 treg = (ip->insn_opcode >> 16) & 0x1f;
3935 dreg = (ip->insn_opcode >> 11) & 0x1f;
3936 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
3937 mask = ip->insn_mo->mask;
3938
3939 expr1.X_op = O_constant;
3940 expr1.X_op_symbol = NULL;
3941 expr1.X_add_symbol = NULL;
3942 expr1.X_add_number = 1;
3943
3944 switch (mask)
3945 {
3946 case M_DABS:
3947 dbl = 1;
3948 case M_ABS:
3949 /* bgez $a0,.+12
3950 move v0,$a0
3951 sub v0,$zero,$a0
3952 */
3953
3954 mips_emit_delays (true);
3955 ++mips_opts.noreorder;
3956 mips_any_noreorder = 1;
3957
3958 expr1.X_add_number = 8;
3959 macro_build ((char *) NULL, &icnt, &expr1, "bgez", "s,p", sreg);
3960 if (dreg == sreg)
2396cfb9
TS
3961 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
3962 0);
252b5132 3963 else
ea1fb5dc 3964 move_register (&icnt, dreg, sreg);
2396cfb9 3965 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 3966 dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
252b5132
RH
3967
3968 --mips_opts.noreorder;
3969 return;
3970
3971 case M_ADD_I:
3972 s = "addi";
3973 s2 = "add";
3974 goto do_addi;
3975 case M_ADDU_I:
3976 s = "addiu";
3977 s2 = "addu";
3978 goto do_addi;
3979 case M_DADD_I:
3980 dbl = 1;
3981 s = "daddi";
3982 s2 = "dadd";
3983 goto do_addi;
3984 case M_DADDU_I:
3985 dbl = 1;
3986 s = "daddiu";
3987 s2 = "daddu";
3988 do_addi:
3989 if (imm_expr.X_op == O_constant
3990 && imm_expr.X_add_number >= -0x8000
3991 && imm_expr.X_add_number < 0x8000)
3992 {
3993 macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,j", treg, sreg,
3994 (int) BFD_RELOC_LO16);
3995 return;
3996 }
3997 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9
TS
3998 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
3999 treg, sreg, AT);
252b5132
RH
4000 break;
4001
4002 case M_AND_I:
4003 s = "andi";
4004 s2 = "and";
4005 goto do_bit;
4006 case M_OR_I:
4007 s = "ori";
4008 s2 = "or";
4009 goto do_bit;
4010 case M_NOR_I:
4011 s = "";
4012 s2 = "nor";
4013 goto do_bit;
4014 case M_XOR_I:
4015 s = "xori";
4016 s2 = "xor";
4017 do_bit:
4018 if (imm_expr.X_op == O_constant
4019 && imm_expr.X_add_number >= 0
4020 && imm_expr.X_add_number < 0x10000)
4021 {
4022 if (mask != M_NOR_I)
4023 macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,i", treg,
4024 sreg, (int) BFD_RELOC_LO16);
4025 else
4026 {
4027 macro_build ((char *) NULL, &icnt, &imm_expr, "ori", "t,r,i",
4028 treg, sreg, (int) BFD_RELOC_LO16);
2396cfb9
TS
4029 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nor",
4030 "d,v,t", treg, treg, 0);
252b5132
RH
4031 }
4032 return;
4033 }
4034
d6bc6245 4035 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
4036 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
4037 treg, sreg, AT);
252b5132
RH
4038 break;
4039
4040 case M_BEQ_I:
4041 s = "beq";
4042 goto beq_i;
4043 case M_BEQL_I:
4044 s = "beql";
4045 likely = 1;
4046 goto beq_i;
4047 case M_BNE_I:
4048 s = "bne";
4049 goto beq_i;
4050 case M_BNEL_I:
4051 s = "bnel";
4052 likely = 1;
4053 beq_i:
4054 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4055 {
4056 macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg,
4057 0);
4058 return;
4059 }
4d34fb5f 4060 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
252b5132
RH
4061 macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg, AT);
4062 break;
4063
4064 case M_BGEL:
4065 likely = 1;
4066 case M_BGE:
4067 if (treg == 0)
4068 {
4069 macro_build ((char *) NULL, &icnt, &offset_expr,
2396cfb9 4070 likely ? "bgezl" : "bgez", "s,p", sreg);
252b5132
RH
4071 return;
4072 }
4073 if (sreg == 0)
4074 {
4075 macro_build ((char *) NULL, &icnt, &offset_expr,
2396cfb9 4076 likely ? "blezl" : "blez", "s,p", treg);
252b5132
RH
4077 return;
4078 }
2396cfb9
TS
4079 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4080 AT, sreg, treg);
252b5132 4081 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4082 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4083 break;
4084
4085 case M_BGTL_I:
4086 likely = 1;
4087 case M_BGT_I:
4088 /* check for > max integer */
4089 maxnum = 0x7fffffff;
ca4e0257 4090 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
4091 {
4092 maxnum <<= 16;
4093 maxnum |= 0xffff;
4094 maxnum <<= 16;
4095 maxnum |= 0xffff;
4096 }
4097 if (imm_expr.X_op == O_constant
4098 && imm_expr.X_add_number >= maxnum
ca4e0257 4099 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
4100 {
4101 do_false:
4102 /* result is always false */
4103 if (! likely)
4104 {
39c0a331
L
4105 if (warn_nops)
4106 as_warn (_("Branch %s is always false (nop)"),
4107 ip->insn_mo->name);
2396cfb9
TS
4108 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop",
4109 "", 0);
252b5132
RH
4110 }
4111 else
4112 {
39c0a331
L
4113 if (warn_nops)
4114 as_warn (_("Branch likely %s is always false"),
4115 ip->insn_mo->name);
252b5132
RH
4116 macro_build ((char *) NULL, &icnt, &offset_expr, "bnel",
4117 "s,t,p", 0, 0);
4118 }
4119 return;
4120 }
4121 if (imm_expr.X_op != O_constant)
4122 as_bad (_("Unsupported large constant"));
f9419b05 4123 ++imm_expr.X_add_number;
252b5132
RH
4124 /* FALLTHROUGH */
4125 case M_BGE_I:
4126 case M_BGEL_I:
4127 if (mask == M_BGEL_I)
4128 likely = 1;
4129 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4130 {
4131 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4132 likely ? "bgezl" : "bgez", "s,p", sreg);
252b5132
RH
4133 return;
4134 }
4135 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4136 {
4137 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4138 likely ? "bgtzl" : "bgtz", "s,p", sreg);
252b5132
RH
4139 return;
4140 }
4141 maxnum = 0x7fffffff;
ca4e0257 4142 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
4143 {
4144 maxnum <<= 16;
4145 maxnum |= 0xffff;
4146 maxnum <<= 16;
4147 maxnum |= 0xffff;
4148 }
4149 maxnum = - maxnum - 1;
4150 if (imm_expr.X_op == O_constant
4151 && imm_expr.X_add_number <= maxnum
ca4e0257 4152 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
4153 {
4154 do_true:
4155 /* result is always true */
4156 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
4157 macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
4158 return;
4159 }
4160 set_at (&icnt, sreg, 0);
4161 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4162 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4163 break;
4164
4165 case M_BGEUL:
4166 likely = 1;
4167 case M_BGEU:
4168 if (treg == 0)
4169 goto do_true;
4170 if (sreg == 0)
4171 {
4172 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4173 likely ? "beql" : "beq", "s,t,p", 0, treg);
252b5132
RH
4174 return;
4175 }
2396cfb9
TS
4176 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4177 "d,v,t", AT, sreg, treg);
252b5132 4178 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4179 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4180 break;
4181
4182 case M_BGTUL_I:
4183 likely = 1;
4184 case M_BGTU_I:
4185 if (sreg == 0
ca4e0257 4186 || (HAVE_32BIT_GPRS
252b5132 4187 && imm_expr.X_op == O_constant
956cd1d6 4188 && imm_expr.X_add_number == (offsetT) 0xffffffff))
252b5132
RH
4189 goto do_false;
4190 if (imm_expr.X_op != O_constant)
4191 as_bad (_("Unsupported large constant"));
f9419b05 4192 ++imm_expr.X_add_number;
252b5132
RH
4193 /* FALLTHROUGH */
4194 case M_BGEU_I:
4195 case M_BGEUL_I:
4196 if (mask == M_BGEUL_I)
4197 likely = 1;
4198 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4199 goto do_true;
4200 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4201 {
4202 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4203 likely ? "bnel" : "bne", "s,t,p", sreg, 0);
252b5132
RH
4204 return;
4205 }
4206 set_at (&icnt, sreg, 1);
4207 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4208 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4209 break;
4210
4211 case M_BGTL:
4212 likely = 1;
4213 case M_BGT:
4214 if (treg == 0)
4215 {
4216 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4217 likely ? "bgtzl" : "bgtz", "s,p", sreg);
252b5132
RH
4218 return;
4219 }
4220 if (sreg == 0)
4221 {
4222 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4223 likely ? "bltzl" : "bltz", "s,p", treg);
252b5132
RH
4224 return;
4225 }
2396cfb9
TS
4226 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4227 AT, treg, sreg);
252b5132 4228 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4229 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4230 break;
4231
4232 case M_BGTUL:
4233 likely = 1;
4234 case M_BGTU:
4235 if (treg == 0)
4236 {
4237 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4238 likely ? "bnel" : "bne", "s,t,p", sreg, 0);
252b5132
RH
4239 return;
4240 }
4241 if (sreg == 0)
4242 goto do_false;
2396cfb9
TS
4243 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4244 "d,v,t", AT, treg, sreg);
252b5132 4245 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4246 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4247 break;
4248
4249 case M_BLEL:
4250 likely = 1;
4251 case M_BLE:
4252 if (treg == 0)
4253 {
4254 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4255 likely ? "blezl" : "blez", "s,p", sreg);
252b5132
RH
4256 return;
4257 }
4258 if (sreg == 0)
4259 {
4260 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4261 likely ? "bgezl" : "bgez", "s,p", treg);
252b5132
RH
4262 return;
4263 }
2396cfb9
TS
4264 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4265 AT, treg, sreg);
252b5132 4266 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4267 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4268 break;
4269
4270 case M_BLEL_I:
4271 likely = 1;
4272 case M_BLE_I:
4273 maxnum = 0x7fffffff;
ca4e0257 4274 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
4275 {
4276 maxnum <<= 16;
4277 maxnum |= 0xffff;
4278 maxnum <<= 16;
4279 maxnum |= 0xffff;
4280 }
4281 if (imm_expr.X_op == O_constant
4282 && imm_expr.X_add_number >= maxnum
ca4e0257 4283 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
4284 goto do_true;
4285 if (imm_expr.X_op != O_constant)
4286 as_bad (_("Unsupported large constant"));
f9419b05 4287 ++imm_expr.X_add_number;
252b5132
RH
4288 /* FALLTHROUGH */
4289 case M_BLT_I:
4290 case M_BLTL_I:
4291 if (mask == M_BLTL_I)
4292 likely = 1;
4293 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4294 {
4295 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4296 likely ? "bltzl" : "bltz", "s,p", sreg);
252b5132
RH
4297 return;
4298 }
4299 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4300 {
4301 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4302 likely ? "blezl" : "blez", "s,p", sreg);
252b5132
RH
4303 return;
4304 }
4305 set_at (&icnt, sreg, 0);
4306 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4307 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4308 break;
4309
4310 case M_BLEUL:
4311 likely = 1;
4312 case M_BLEU:
4313 if (treg == 0)
4314 {
4315 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4316 likely ? "beql" : "beq", "s,t,p", sreg, 0);
252b5132
RH
4317 return;
4318 }
4319 if (sreg == 0)
4320 goto do_true;
2396cfb9
TS
4321 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4322 "d,v,t", AT, treg, sreg);
252b5132 4323 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4324 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4325 break;
4326
4327 case M_BLEUL_I:
4328 likely = 1;
4329 case M_BLEU_I:
4330 if (sreg == 0
ca4e0257 4331 || (HAVE_32BIT_GPRS
252b5132 4332 && imm_expr.X_op == O_constant
956cd1d6 4333 && imm_expr.X_add_number == (offsetT) 0xffffffff))
252b5132
RH
4334 goto do_true;
4335 if (imm_expr.X_op != O_constant)
4336 as_bad (_("Unsupported large constant"));
f9419b05 4337 ++imm_expr.X_add_number;
252b5132
RH
4338 /* FALLTHROUGH */
4339 case M_BLTU_I:
4340 case M_BLTUL_I:
4341 if (mask == M_BLTUL_I)
4342 likely = 1;
4343 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4344 goto do_false;
4345 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4346 {
4347 macro_build ((char *) NULL, &icnt, &offset_expr,
4348 likely ? "beql" : "beq",
4349 "s,t,p", sreg, 0);
4350 return;
4351 }
4352 set_at (&icnt, sreg, 1);
4353 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4354 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4355 break;
4356
4357 case M_BLTL:
4358 likely = 1;
4359 case M_BLT:
4360 if (treg == 0)
4361 {
4362 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4363 likely ? "bltzl" : "bltz", "s,p", sreg);
252b5132
RH
4364 return;
4365 }
4366 if (sreg == 0)
4367 {
4368 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4369 likely ? "bgtzl" : "bgtz", "s,p", treg);
252b5132
RH
4370 return;
4371 }
2396cfb9
TS
4372 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4373 AT, sreg, treg);
252b5132 4374 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4375 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4376 break;
4377
4378 case M_BLTUL:
4379 likely = 1;
4380 case M_BLTU:
4381 if (treg == 0)
4382 goto do_false;
4383 if (sreg == 0)
4384 {
4385 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4386 likely ? "bnel" : "bne", "s,t,p", 0, treg);
252b5132
RH
4387 return;
4388 }
2396cfb9
TS
4389 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4390 "d,v,t", AT, sreg,
252b5132
RH
4391 treg);
4392 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4393 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4394 break;
4395
4396 case M_DDIV_3:
4397 dbl = 1;
4398 case M_DIV_3:
4399 s = "mflo";
4400 goto do_div3;
4401 case M_DREM_3:
4402 dbl = 1;
4403 case M_REM_3:
4404 s = "mfhi";
4405 do_div3:
4406 if (treg == 0)
4407 {
4408 as_warn (_("Divide by zero."));
4409 if (mips_trap)
2396cfb9
TS
4410 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4411 "s,t", 0, 0);
252b5132 4412 else
2396cfb9
TS
4413 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4414 "c", 7);
252b5132
RH
4415 return;
4416 }
4417
4418 mips_emit_delays (true);
4419 ++mips_opts.noreorder;
4420 mips_any_noreorder = 1;
4421 if (mips_trap)
4422 {
2396cfb9
TS
4423 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4424 "s,t", treg, 0);
4425 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 4426 dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
252b5132
RH
4427 }
4428 else
4429 {
4430 expr1.X_add_number = 8;
4431 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
2396cfb9 4432 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 4433 dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
2396cfb9
TS
4434 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4435 "c", 7);
252b5132
RH
4436 }
4437 expr1.X_add_number = -1;
4438 macro_build ((char *) NULL, &icnt, &expr1,
4439 dbl ? "daddiu" : "addiu",
4440 "t,r,j", AT, 0, (int) BFD_RELOC_LO16);
4441 expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
4442 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, AT);
4443 if (dbl)
4444 {
4445 expr1.X_add_number = 1;
4446 macro_build ((char *) NULL, &icnt, &expr1, "daddiu", "t,r,j", AT, 0,
4447 (int) BFD_RELOC_LO16);
2396cfb9
TS
4448 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsll32",
4449 "d,w,<", AT, AT, 31);
252b5132
RH
4450 }
4451 else
4452 {
4453 expr1.X_add_number = 0x80000000;
4454 macro_build ((char *) NULL, &icnt, &expr1, "lui", "t,u", AT,
4455 (int) BFD_RELOC_HI16);
4456 }
4457 if (mips_trap)
4458 {
2396cfb9
TS
4459 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4460 "s,t", sreg, AT);
252b5132
RH
4461 /* We want to close the noreorder block as soon as possible, so
4462 that later insns are available for delay slot filling. */
4463 --mips_opts.noreorder;
4464 }
4465 else
4466 {
4467 expr1.X_add_number = 8;
4468 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", sreg, AT);
2396cfb9
TS
4469 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
4470 0);
252b5132
RH
4471
4472 /* We want to close the noreorder block as soon as possible, so
4473 that later insns are available for delay slot filling. */
4474 --mips_opts.noreorder;
4475
2396cfb9
TS
4476 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4477 "c", 6);
252b5132 4478 }
2396cfb9 4479 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d", dreg);
252b5132
RH
4480 break;
4481
4482 case M_DIV_3I:
4483 s = "div";
4484 s2 = "mflo";
4485 goto do_divi;
4486 case M_DIVU_3I:
4487 s = "divu";
4488 s2 = "mflo";
4489 goto do_divi;
4490 case M_REM_3I:
4491 s = "div";
4492 s2 = "mfhi";
4493 goto do_divi;
4494 case M_REMU_3I:
4495 s = "divu";
4496 s2 = "mfhi";
4497 goto do_divi;
4498 case M_DDIV_3I:
4499 dbl = 1;
4500 s = "ddiv";
4501 s2 = "mflo";
4502 goto do_divi;
4503 case M_DDIVU_3I:
4504 dbl = 1;
4505 s = "ddivu";
4506 s2 = "mflo";
4507 goto do_divi;
4508 case M_DREM_3I:
4509 dbl = 1;
4510 s = "ddiv";
4511 s2 = "mfhi";
4512 goto do_divi;
4513 case M_DREMU_3I:
4514 dbl = 1;
4515 s = "ddivu";
4516 s2 = "mfhi";
4517 do_divi:
4518 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4519 {
4520 as_warn (_("Divide by zero."));
4521 if (mips_trap)
2396cfb9
TS
4522 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4523 "s,t", 0, 0);
252b5132 4524 else
2396cfb9
TS
4525 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4526 "c", 7);
252b5132
RH
4527 return;
4528 }
4529 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4530 {
4531 if (strcmp (s2, "mflo") == 0)
ea1fb5dc 4532 move_register (&icnt, dreg, sreg);
252b5132 4533 else
ea1fb5dc 4534 move_register (&icnt, dreg, 0);
252b5132
RH
4535 return;
4536 }
4537 if (imm_expr.X_op == O_constant
4538 && imm_expr.X_add_number == -1
4539 && s[strlen (s) - 1] != 'u')
4540 {
4541 if (strcmp (s2, "mflo") == 0)
4542 {
2396cfb9
TS
4543 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4544 dbl ? "dneg" : "neg", "d,w", dreg, sreg);
252b5132
RH
4545 }
4546 else
ea1fb5dc 4547 move_register (&icnt, dreg, 0);
252b5132
RH
4548 return;
4549 }
4550
4551 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9
TS
4552 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4553 sreg, AT);
4554 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
252b5132
RH
4555 break;
4556
4557 case M_DIVU_3:
4558 s = "divu";
4559 s2 = "mflo";
4560 goto do_divu3;
4561 case M_REMU_3:
4562 s = "divu";
4563 s2 = "mfhi";
4564 goto do_divu3;
4565 case M_DDIVU_3:
4566 s = "ddivu";
4567 s2 = "mflo";
4568 goto do_divu3;
4569 case M_DREMU_3:
4570 s = "ddivu";
4571 s2 = "mfhi";
4572 do_divu3:
4573 mips_emit_delays (true);
4574 ++mips_opts.noreorder;
4575 mips_any_noreorder = 1;
4576 if (mips_trap)
4577 {
2396cfb9
TS
4578 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
4579 "s,t", treg, 0);
4580 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4581 sreg, treg);
252b5132
RH
4582 /* We want to close the noreorder block as soon as possible, so
4583 that later insns are available for delay slot filling. */
4584 --mips_opts.noreorder;
4585 }
4586 else
4587 {
4588 expr1.X_add_number = 8;
4589 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
2396cfb9
TS
4590 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4591 sreg, treg);
252b5132
RH
4592
4593 /* We want to close the noreorder block as soon as possible, so
4594 that later insns are available for delay slot filling. */
4595 --mips_opts.noreorder;
2396cfb9
TS
4596 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4597 "c", 7);
252b5132 4598 }
2396cfb9 4599 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
252b5132
RH
4600 return;
4601
4602 case M_DLA_AB:
4603 dbl = 1;
4604 case M_LA_AB:
4605 /* Load the address of a symbol into a register. If breg is not
4606 zero, we then add a base register to it. */
4607
3bec30a8
TS
4608 if (dbl && HAVE_32BIT_GPRS)
4609 as_warn (_("dla used to load 32-bit register"));
4610
c90bbe5b 4611 if (! dbl && HAVE_64BIT_OBJECTS)
3bec30a8
TS
4612 as_warn (_("la used to load 64-bit address"));
4613
afdbd6d0
CD
4614 if (treg == breg)
4615 {
4616 tempreg = AT;
4617 used_at = 1;
4618 }
4619 else
4620 {
4621 tempreg = treg;
4622 used_at = 0;
4623 }
4624
252b5132
RH
4625 /* When generating embedded PIC code, we permit expressions of
4626 the form
afdbd6d0
CD
4627 la $treg,foo-bar
4628 la $treg,foo-bar($breg)
bb2d6cd7 4629 where bar is an address in the current section. These are used
252b5132
RH
4630 when getting the addresses of functions. We don't permit
4631 X_add_number to be non-zero, because if the symbol is
4632 external the relaxing code needs to know that any addend is
4633 purely the offset to X_op_symbol. */
4634 if (mips_pic == EMBEDDED_PIC
4635 && offset_expr.X_op == O_subtract
49309057 4636 && (symbol_constant_p (offset_expr.X_op_symbol)
bb2d6cd7 4637 ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
49309057
ILT
4638 : (symbol_equated_p (offset_expr.X_op_symbol)
4639 && (S_GET_SEGMENT
4640 (symbol_get_value_expression (offset_expr.X_op_symbol)
4641 ->X_add_symbol)
bb2d6cd7 4642 == now_seg)))
bb2d6cd7
GK
4643 && (offset_expr.X_add_number == 0
4644 || OUTPUT_FLAVOR == bfd_target_elf_flavour))
252b5132 4645 {
afdbd6d0
CD
4646 if (breg == 0)
4647 {
4648 tempreg = treg;
4649 used_at = 0;
4650 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4651 tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4652 }
4653 else
4654 {
4655 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4656 tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4657 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4d34fb5f 4658 (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu",
afdbd6d0
CD
4659 "d,v,t", tempreg, tempreg, breg);
4660 }
252b5132 4661 macro_build ((char *) NULL, &icnt, &offset_expr,
4d34fb5f 4662 (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
afdbd6d0
CD
4663 "t,r,j", treg, tempreg, (int) BFD_RELOC_PCREL_LO16);
4664 if (! used_at)
4665 return;
4666 break;
252b5132
RH
4667 }
4668
4669 if (offset_expr.X_op != O_symbol
4670 && offset_expr.X_op != O_constant)
4671 {
4672 as_bad (_("expression too complex"));
4673 offset_expr.X_op = O_constant;
4674 }
4675
252b5132 4676 if (offset_expr.X_op == O_constant)
4d34fb5f
TS
4677 load_register (&icnt, tempreg, &offset_expr,
4678 ((mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
4679 ? (dbl || HAVE_64BIT_ADDRESSES)
4680 : HAVE_64BIT_ADDRESSES));
252b5132
RH
4681 else if (mips_pic == NO_PIC)
4682 {
d6bc6245 4683 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 4684 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
4685 Otherwise we want
4686 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
4687 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4688 If we have a constant, we need two instructions anyhow,
d6bc6245 4689 so we may as well always use the latter form.
76b3015f 4690
d6bc6245
TS
4691 With 64bit address space and a usable $at we want
4692 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4693 lui $at,<sym> (BFD_RELOC_HI16_S)
4694 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4695 daddiu $at,<sym> (BFD_RELOC_LO16)
4696 dsll32 $tempreg,0
4697 dadd $tempreg,$tempreg,$at
76b3015f 4698
d6bc6245
TS
4699 If $at is already in use, we use an path which is suboptimal
4700 on superscalar processors.
4701 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4702 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4703 dsll $tempreg,16
4704 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
4705 dsll $tempreg,16
4706 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
4707 */
f9419b05 4708 char *p = NULL;
d6bc6245 4709 if (HAVE_64BIT_ADDRESSES)
252b5132 4710 {
d6bc6245
TS
4711 /* We don't do GP optimization for now because RELAX_ENCODE can't
4712 hold the data for such large chunks. */
4713
98d3f06f
KH
4714 if (used_at == 0)
4715 {
4716 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4717 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4718 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4719 AT, (int) BFD_RELOC_HI16_S);
4720 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4721 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4722 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4723 AT, AT, (int) BFD_RELOC_LO16);
4724 macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
4725 "d,w,<", tempreg, tempreg, 0);
4726 macro_build (p, &icnt, (expressionS *) NULL, "dadd", "d,v,t",
4727 tempreg, tempreg, AT);
4728 used_at = 1;
4729 }
4730 else
4731 {
4732 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4733 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4734 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4735 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4736 macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4737 tempreg, tempreg, 16);
4738 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4739 tempreg, tempreg, (int) BFD_RELOC_HI16_S);
4740 macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4741 tempreg, tempreg, 16);
4742 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4743 tempreg, tempreg, (int) BFD_RELOC_LO16);
4744 }
4745 }
4746 else
4747 {
4748 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4749 && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
4750 {
4751 frag_grow (20);
4752 macro_build ((char *) NULL, &icnt, &offset_expr, "addiu",
c9914766
TS
4753 "t,r,j", tempreg, mips_gp_register,
4754 (int) BFD_RELOC_GPREL16);
98d3f06f
KH
4755 p = frag_var (rs_machine_dependent, 8, 0,
4756 RELAX_ENCODE (4, 8, 0, 4, 0,
4757 mips_opts.warn_about_macros),
4758 offset_expr.X_add_symbol, 0, NULL);
4759 }
4760 macro_build_lui (p, &icnt, &offset_expr, tempreg);
4761 if (p != NULL)
4762 p += 4;
4763 macro_build (p, &icnt, &offset_expr, "addiu",
4764 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4765 }
252b5132
RH
4766 }
4767 else if (mips_pic == SVR4_PIC && ! mips_big_got)
4768 {
9117d219
NC
4769 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
4770
252b5132
RH
4771 /* If this is a reference to an external symbol, and there
4772 is no constant, we want
4773 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9117d219
NC
4774 or if tempreg is PIC_CALL_REG
4775 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
252b5132
RH
4776 For a local symbol, we want
4777 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4778 nop
4779 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4780
4781 If we have a small constant, and this is a reference to
4782 an external symbol, we want
4783 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4784 nop
4785 addiu $tempreg,$tempreg,<constant>
4786 For a local symbol, we want the same instruction
4787 sequence, but we output a BFD_RELOC_LO16 reloc on the
4788 addiu instruction.
4789
4790 If we have a large constant, and this is a reference to
4791 an external symbol, we want
4792 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4793 lui $at,<hiconstant>
4794 addiu $at,$at,<loconstant>
4795 addu $tempreg,$tempreg,$at
4796 For a local symbol, we want the same instruction
4797 sequence, but we output a BFD_RELOC_LO16 reloc on the
4798 addiu instruction. */
4799 expr1.X_add_number = offset_expr.X_add_number;
4800 offset_expr.X_add_number = 0;
4801 frag_grow (32);
9117d219
NC
4802 if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4803 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
4d34fb5f
TS
4804 macro_build ((char *) NULL, &icnt, &offset_expr,
4805 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
c9914766 4806 "t,o(b)", tempreg, lw_reloc_type, mips_gp_register);
252b5132
RH
4807 if (expr1.X_add_number == 0)
4808 {
4809 int off;
f9419b05 4810 char *p;
252b5132
RH
4811
4812 if (breg == 0)
4813 off = 0;
4814 else
4815 {
4816 /* We're going to put in an addu instruction using
4817 tempreg, so we may as well insert the nop right
4818 now. */
4819 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4820 "nop", "");
4821 off = 4;
4822 }
4823 p = frag_var (rs_machine_dependent, 8 - off, 0,
4824 RELAX_ENCODE (0, 8 - off, -4 - off, 4 - off, 0,
4825 (breg == 0
4826 ? mips_opts.warn_about_macros
4827 : 0)),
c4e7957c 4828 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
4829 if (breg == 0)
4830 {
4831 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4832 p += 4;
4833 }
4834 macro_build (p, &icnt, &expr1,
ca4e0257 4835 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
4836 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4837 /* FIXME: If breg == 0, and the next instruction uses
4838 $tempreg, then if this variant case is used an extra
4839 nop will be generated. */
4840 }
4841 else if (expr1.X_add_number >= -0x8000
4842 && expr1.X_add_number < 0x8000)
4843 {
4844 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4845 "nop", "");
4846 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 4847 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132 4848 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
c4e7957c
TS
4849 frag_var (rs_machine_dependent, 0, 0,
4850 RELAX_ENCODE (0, 0, -12, -4, 0, 0),
4851 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
4852 }
4853 else
4854 {
4855 int off1;
4856
4857 /* If we are going to add in a base register, and the
4858 target register and the base register are the same,
4859 then we are using AT as a temporary register. Since
4860 we want to load the constant into AT, we add our
4861 current AT (from the global offset table) and the
4862 register into the register now, and pretend we were
4863 not using a base register. */
4864 if (breg != treg)
4865 off1 = 0;
4866 else
4867 {
4868 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4869 "nop", "");
4870 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 4871 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
4872 "d,v,t", treg, AT, breg);
4873 breg = 0;
4874 tempreg = treg;
4875 off1 = -8;
4876 }
4877
4878 /* Set mips_optimize around the lui instruction to avoid
4879 inserting an unnecessary nop after the lw. */
4880 hold_mips_optimize = mips_optimize;
4881 mips_optimize = 2;
c4e7957c 4882 macro_build_lui (NULL, &icnt, &expr1, AT);
252b5132
RH
4883 mips_optimize = hold_mips_optimize;
4884
4885 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 4886 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
4887 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4888 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 4889 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132 4890 "d,v,t", tempreg, tempreg, AT);
c4e7957c
TS
4891 frag_var (rs_machine_dependent, 0, 0,
4892 RELAX_ENCODE (0, 0, -16 + off1, -8, 0, 0),
4893 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
4894 used_at = 1;
4895 }
4896 }
4897 else if (mips_pic == SVR4_PIC)
4898 {
4899 int gpdel;
f9419b05 4900 char *p;
9117d219
NC
4901 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
4902 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
252b5132
RH
4903
4904 /* This is the large GOT case. If this is a reference to an
4905 external symbol, and there is no constant, we want
4906 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
4907 addu $tempreg,$tempreg,$gp
4908 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
9117d219
NC
4909 or if tempreg is PIC_CALL_REG
4910 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
4911 addu $tempreg,$tempreg,$gp
4912 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
252b5132
RH
4913 For a local symbol, we want
4914 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4915 nop
4916 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4917
4918 If we have a small constant, and this is a reference to
4919 an external symbol, we want
4920 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
4921 addu $tempreg,$tempreg,$gp
4922 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4923 nop
4924 addiu $tempreg,$tempreg,<constant>
4925 For a local symbol, we want
4926 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4927 nop
4928 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
4929
4930 If we have a large constant, and this is a reference to
4931 an external symbol, we want
4932 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
4933 addu $tempreg,$tempreg,$gp
4934 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4935 lui $at,<hiconstant>
4936 addiu $at,$at,<loconstant>
4937 addu $tempreg,$tempreg,$at
4938 For a local symbol, we want
4939 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4940 lui $at,<hiconstant>
4941 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
4942 addu $tempreg,$tempreg,$at
4943 */
4944 expr1.X_add_number = offset_expr.X_add_number;
4945 offset_expr.X_add_number = 0;
4946 frag_grow (52);
f7ea7ef2 4947 if (reg_needs_delay (mips_gp_register))
252b5132
RH
4948 gpdel = 4;
4949 else
4950 gpdel = 0;
9117d219
NC
4951 if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4952 {
4953 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
4954 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
4955 }
252b5132 4956 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
9117d219 4957 tempreg, lui_reloc_type);
252b5132 4958 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 4959 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 4960 "d,v,t", tempreg, tempreg, mips_gp_register);
252b5132 4961 macro_build ((char *) NULL, &icnt, &offset_expr,
4d34fb5f 4962 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
9117d219 4963 "t,o(b)", tempreg, lw_reloc_type, tempreg);
252b5132
RH
4964 if (expr1.X_add_number == 0)
4965 {
4966 int off;
4967
4968 if (breg == 0)
4969 off = 0;
4970 else
4971 {
4972 /* We're going to put in an addu instruction using
4973 tempreg, so we may as well insert the nop right
4974 now. */
4975 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4976 "nop", "");
4977 off = 4;
4978 }
4979
4980 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4981 RELAX_ENCODE (12 + off, 12 + gpdel, gpdel,
4982 8 + gpdel, 0,
4983 (breg == 0
4984 ? mips_opts.warn_about_macros
4985 : 0)),
c4e7957c 4986 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
4987 }
4988 else if (expr1.X_add_number >= -0x8000
4989 && expr1.X_add_number < 0x8000)
4990 {
4991 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4992 "nop", "");
4993 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 4994 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
4995 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4996
4997 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4998 RELAX_ENCODE (20, 12 + gpdel, gpdel, 8 + gpdel, 0,
4999 (breg == 0
5000 ? mips_opts.warn_about_macros
5001 : 0)),
c4e7957c 5002 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5003 }
5004 else
5005 {
5006 int adj, dreg;
5007
5008 /* If we are going to add in a base register, and the
5009 target register and the base register are the same,
5010 then we are using AT as a temporary register. Since
5011 we want to load the constant into AT, we add our
5012 current AT (from the global offset table) and the
5013 register into the register now, and pretend we were
5014 not using a base register. */
5015 if (breg != treg)
5016 {
5017 adj = 0;
5018 dreg = tempreg;
5019 }
5020 else
5021 {
5022 assert (tempreg == AT);
5023 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5024 "nop", "");
5025 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5026 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5027 "d,v,t", treg, AT, breg);
5028 dreg = treg;
5029 adj = 8;
5030 }
5031
5032 /* Set mips_optimize around the lui instruction to avoid
5033 inserting an unnecessary nop after the lw. */
5034 hold_mips_optimize = mips_optimize;
5035 mips_optimize = 2;
c4e7957c 5036 macro_build_lui (NULL, &icnt, &expr1, AT);
252b5132
RH
5037 mips_optimize = hold_mips_optimize;
5038
5039 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 5040 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
5041 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5042 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5043 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5044 "d,v,t", dreg, dreg, AT);
5045
5046 p = frag_var (rs_machine_dependent, 16 + gpdel + adj, 0,
5047 RELAX_ENCODE (24 + adj, 16 + gpdel + adj, gpdel,
5048 8 + gpdel, 0,
5049 (breg == 0
5050 ? mips_opts.warn_about_macros
5051 : 0)),
c4e7957c 5052 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5053
5054 used_at = 1;
5055 }
5056
5057 if (gpdel > 0)
5058 {
5059 /* This is needed because this instruction uses $gp, but
5060 the first instruction on the main stream does not. */
5061 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5062 p += 4;
5063 }
5064 macro_build (p, &icnt, &offset_expr,
4d34fb5f 5065 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
c9914766
TS
5066 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16,
5067 mips_gp_register);
252b5132
RH
5068 p += 4;
5069 if (expr1.X_add_number >= -0x8000
5070 && expr1.X_add_number < 0x8000)
5071 {
5072 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5073 p += 4;
5074 macro_build (p, &icnt, &expr1,
ca4e0257 5075 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
5076 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5077 /* FIXME: If add_number is 0, and there was no base
5078 register, the external symbol case ended with a load,
5079 so if the symbol turns out to not be external, and
5080 the next instruction uses tempreg, an unnecessary nop
5081 will be inserted. */
5082 }
5083 else
5084 {
5085 if (breg == treg)
5086 {
5087 /* We must add in the base register now, as in the
5088 external symbol case. */
5089 assert (tempreg == AT);
5090 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5091 p += 4;
5092 macro_build (p, &icnt, (expressionS *) NULL,
ca4e0257 5093 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5094 "d,v,t", treg, AT, breg);
5095 p += 4;
5096 tempreg = treg;
5097 /* We set breg to 0 because we have arranged to add
5098 it in in both cases. */
5099 breg = 0;
5100 }
5101
5102 macro_build_lui (p, &icnt, &expr1, AT);
5103 p += 4;
5104 macro_build (p, &icnt, &expr1,
ca4e0257 5105 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
5106 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
5107 p += 4;
5108 macro_build (p, &icnt, (expressionS *) NULL,
ca4e0257 5109 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5110 "d,v,t", tempreg, tempreg, AT);
5111 p += 4;
5112 }
5113 }
5114 else if (mips_pic == EMBEDDED_PIC)
5115 {
5116 /* We use
cdf6fd85 5117 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
5118 */
5119 macro_build ((char *) NULL, &icnt, &offset_expr,
c9914766
TS
5120 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
5121 tempreg, mips_gp_register, (int) BFD_RELOC_GPREL16);
252b5132
RH
5122 }
5123 else
5124 abort ();
5125
5126 if (breg != 0)
4d34fb5f
TS
5127 {
5128 char *s;
5129
5130 if (mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
5131 s = (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu";
5132 else
5133 s = HAVE_64BIT_ADDRESSES ? "daddu" : "addu";
5134
5135 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s,
5136 "d,v,t", treg, tempreg, breg);
5137 }
252b5132
RH
5138
5139 if (! used_at)
5140 return;
5141
5142 break;
5143
5144 case M_J_A:
5145 /* The j instruction may not be used in PIC code, since it
5146 requires an absolute address. We convert it to a b
5147 instruction. */
5148 if (mips_pic == NO_PIC)
5149 macro_build ((char *) NULL, &icnt, &offset_expr, "j", "a");
5150 else
5151 macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
5152 return;
5153
5154 /* The jal instructions must be handled as macros because when
5155 generating PIC code they expand to multi-instruction
5156 sequences. Normally they are simple instructions. */
5157 case M_JAL_1:
5158 dreg = RA;
5159 /* Fall through. */
5160 case M_JAL_2:
5161 if (mips_pic == NO_PIC
5162 || mips_pic == EMBEDDED_PIC)
5163 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5164 "d,s", dreg, sreg);
5165 else if (mips_pic == SVR4_PIC)
5166 {
5167 if (sreg != PIC_CALL_REG)
5168 as_warn (_("MIPS PIC call to register other than $25"));
bdaaa2e1 5169
252b5132
RH
5170 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5171 "d,s", dreg, sreg);
6478892d 5172 if (! HAVE_NEWABI)
252b5132 5173 {
6478892d
TS
5174 if (mips_cprestore_offset < 0)
5175 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5176 else
5177 {
7a621144
DJ
5178 if (! mips_frame_reg_valid)
5179 {
5180 as_warn (_("No .frame pseudo-op used in PIC code"));
5181 /* Quiet this warning. */
5182 mips_frame_reg_valid = 1;
5183 }
5184 if (! mips_cprestore_valid)
5185 {
5186 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5187 /* Quiet this warning. */
5188 mips_cprestore_valid = 1;
5189 }
6478892d
TS
5190 expr1.X_add_number = mips_cprestore_offset;
5191 macro_build ((char *) NULL, &icnt, &expr1,
5192 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
c9914766
TS
5193 mips_gp_register, (int) BFD_RELOC_LO16,
5194 mips_frame_reg);
6478892d 5195 }
252b5132
RH
5196 }
5197 }
5198 else
5199 abort ();
5200
5201 return;
5202
5203 case M_JAL_A:
5204 if (mips_pic == NO_PIC)
5205 macro_build ((char *) NULL, &icnt, &offset_expr, "jal", "a");
5206 else if (mips_pic == SVR4_PIC)
5207 {
f9419b05
TS
5208 char *p;
5209
252b5132
RH
5210 /* If this is a reference to an external symbol, and we are
5211 using a small GOT, we want
5212 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5213 nop
f9419b05 5214 jalr $ra,$25
252b5132
RH
5215 nop
5216 lw $gp,cprestore($sp)
5217 The cprestore value is set using the .cprestore
5218 pseudo-op. If we are using a big GOT, we want
5219 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5220 addu $25,$25,$gp
5221 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
5222 nop
f9419b05 5223 jalr $ra,$25
252b5132
RH
5224 nop
5225 lw $gp,cprestore($sp)
5226 If the symbol is not external, we want
5227 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5228 nop
5229 addiu $25,$25,<sym> (BFD_RELOC_LO16)
f9419b05 5230 jalr $ra,$25
252b5132
RH
5231 nop
5232 lw $gp,cprestore($sp) */
5233 frag_grow (40);
5234 if (! mips_big_got)
5235 {
5236 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 5237 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132 5238 "t,o(b)", PIC_CALL_REG,
f7ea7ef2 5239 (int) BFD_RELOC_MIPS_CALL16, mips_gp_register);
252b5132
RH
5240 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5241 "nop", "");
5242 p = frag_var (rs_machine_dependent, 4, 0,
5243 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
c4e7957c 5244 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5245 }
5246 else
5247 {
5248 int gpdel;
5249
f7ea7ef2 5250 if (reg_needs_delay (mips_gp_register))
252b5132
RH
5251 gpdel = 4;
5252 else
5253 gpdel = 0;
5254 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5255 PIC_CALL_REG, (int) BFD_RELOC_MIPS_CALL_HI16);
5256 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5257 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
f7ea7ef2
TS
5258 "d,v,t", PIC_CALL_REG, PIC_CALL_REG,
5259 mips_gp_register);
252b5132 5260 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 5261 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
5262 "t,o(b)", PIC_CALL_REG,
5263 (int) BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
5264 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5265 "nop", "");
5266 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5267 RELAX_ENCODE (16, 12 + gpdel, gpdel, 8 + gpdel,
5268 0, 0),
c4e7957c 5269 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5270 if (gpdel > 0)
5271 {
5272 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5273 p += 4;
5274 }
5275 macro_build (p, &icnt, &offset_expr,
ca4e0257 5276 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132 5277 "t,o(b)", PIC_CALL_REG,
f7ea7ef2 5278 (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132
RH
5279 p += 4;
5280 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5281 p += 4;
bdaaa2e1 5282 }
252b5132 5283 macro_build (p, &icnt, &offset_expr,
ca4e0257 5284 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
5285 "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
5286 (int) BFD_RELOC_LO16);
5287 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5288 "jalr", "s", PIC_CALL_REG);
6478892d 5289 if (! HAVE_NEWABI)
252b5132 5290 {
6478892d
TS
5291 if (mips_cprestore_offset < 0)
5292 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5293 else
5294 {
7a621144
DJ
5295 if (! mips_frame_reg_valid)
5296 {
5297 as_warn (_("No .frame pseudo-op used in PIC code"));
5298 /* Quiet this warning. */
5299 mips_frame_reg_valid = 1;
5300 }
5301 if (! mips_cprestore_valid)
5302 {
5303 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5304 /* Quiet this warning. */
5305 mips_cprestore_valid = 1;
5306 }
6478892d
TS
5307 if (mips_opts.noreorder)
5308 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
98d3f06f 5309 "nop", "");
6478892d
TS
5310 expr1.X_add_number = mips_cprestore_offset;
5311 macro_build ((char *) NULL, &icnt, &expr1,
5312 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
c9914766
TS
5313 mips_gp_register, (int) BFD_RELOC_LO16,
5314 mips_frame_reg);
6478892d 5315 }
252b5132
RH
5316 }
5317 }
5318 else if (mips_pic == EMBEDDED_PIC)
5319 {
5320 macro_build ((char *) NULL, &icnt, &offset_expr, "bal", "p");
5321 /* The linker may expand the call to a longer sequence which
5322 uses $at, so we must break rather than return. */
5323 break;
5324 }
5325 else
5326 abort ();
5327
5328 return;
5329
5330 case M_LB_AB:
5331 s = "lb";
5332 goto ld;
5333 case M_LBU_AB:
5334 s = "lbu";
5335 goto ld;
5336 case M_LH_AB:
5337 s = "lh";
5338 goto ld;
5339 case M_LHU_AB:
5340 s = "lhu";
5341 goto ld;
5342 case M_LW_AB:
5343 s = "lw";
5344 goto ld;
5345 case M_LWC0_AB:
5346 s = "lwc0";
bdaaa2e1 5347 /* Itbl support may require additional care here. */
252b5132
RH
5348 coproc = 1;
5349 goto ld;
5350 case M_LWC1_AB:
5351 s = "lwc1";
bdaaa2e1 5352 /* Itbl support may require additional care here. */
252b5132
RH
5353 coproc = 1;
5354 goto ld;
5355 case M_LWC2_AB:
5356 s = "lwc2";
bdaaa2e1 5357 /* Itbl support may require additional care here. */
252b5132
RH
5358 coproc = 1;
5359 goto ld;
5360 case M_LWC3_AB:
5361 s = "lwc3";
bdaaa2e1 5362 /* Itbl support may require additional care here. */
252b5132
RH
5363 coproc = 1;
5364 goto ld;
5365 case M_LWL_AB:
5366 s = "lwl";
5367 lr = 1;
5368 goto ld;
5369 case M_LWR_AB:
5370 s = "lwr";
5371 lr = 1;
5372 goto ld;
5373 case M_LDC1_AB:
ec68c924 5374 if (mips_arch == CPU_R4650)
252b5132
RH
5375 {
5376 as_bad (_("opcode not supported on this processor"));
5377 return;
5378 }
5379 s = "ldc1";
bdaaa2e1 5380 /* Itbl support may require additional care here. */
252b5132
RH
5381 coproc = 1;
5382 goto ld;
5383 case M_LDC2_AB:
5384 s = "ldc2";
bdaaa2e1 5385 /* Itbl support may require additional care here. */
252b5132
RH
5386 coproc = 1;
5387 goto ld;
5388 case M_LDC3_AB:
5389 s = "ldc3";
bdaaa2e1 5390 /* Itbl support may require additional care here. */
252b5132
RH
5391 coproc = 1;
5392 goto ld;
5393 case M_LDL_AB:
5394 s = "ldl";
5395 lr = 1;
5396 goto ld;
5397 case M_LDR_AB:
5398 s = "ldr";
5399 lr = 1;
5400 goto ld;
5401 case M_LL_AB:
5402 s = "ll";
5403 goto ld;
5404 case M_LLD_AB:
5405 s = "lld";
5406 goto ld;
5407 case M_LWU_AB:
5408 s = "lwu";
5409 ld:
5410 if (breg == treg || coproc || lr)
5411 {
5412 tempreg = AT;
5413 used_at = 1;
5414 }
5415 else
5416 {
5417 tempreg = treg;
5418 used_at = 0;
5419 }
5420 goto ld_st;
5421 case M_SB_AB:
5422 s = "sb";
5423 goto st;
5424 case M_SH_AB:
5425 s = "sh";
5426 goto st;
5427 case M_SW_AB:
5428 s = "sw";
5429 goto st;
5430 case M_SWC0_AB:
5431 s = "swc0";
bdaaa2e1 5432 /* Itbl support may require additional care here. */
252b5132
RH
5433 coproc = 1;
5434 goto st;
5435 case M_SWC1_AB:
5436 s = "swc1";
bdaaa2e1 5437 /* Itbl support may require additional care here. */
252b5132
RH
5438 coproc = 1;
5439 goto st;
5440 case M_SWC2_AB:
5441 s = "swc2";
bdaaa2e1 5442 /* Itbl support may require additional care here. */
252b5132
RH
5443 coproc = 1;
5444 goto st;
5445 case M_SWC3_AB:
5446 s = "swc3";
bdaaa2e1 5447 /* Itbl support may require additional care here. */
252b5132
RH
5448 coproc = 1;
5449 goto st;
5450 case M_SWL_AB:
5451 s = "swl";
5452 goto st;
5453 case M_SWR_AB:
5454 s = "swr";
5455 goto st;
5456 case M_SC_AB:
5457 s = "sc";
5458 goto st;
5459 case M_SCD_AB:
5460 s = "scd";
5461 goto st;
5462 case M_SDC1_AB:
ec68c924 5463 if (mips_arch == CPU_R4650)
252b5132
RH
5464 {
5465 as_bad (_("opcode not supported on this processor"));
5466 return;
5467 }
5468 s = "sdc1";
5469 coproc = 1;
bdaaa2e1 5470 /* Itbl support may require additional care here. */
252b5132
RH
5471 goto st;
5472 case M_SDC2_AB:
5473 s = "sdc2";
bdaaa2e1 5474 /* Itbl support may require additional care here. */
252b5132
RH
5475 coproc = 1;
5476 goto st;
5477 case M_SDC3_AB:
5478 s = "sdc3";
bdaaa2e1 5479 /* Itbl support may require additional care here. */
252b5132
RH
5480 coproc = 1;
5481 goto st;
5482 case M_SDL_AB:
5483 s = "sdl";
5484 goto st;
5485 case M_SDR_AB:
5486 s = "sdr";
5487 st:
5488 tempreg = AT;
5489 used_at = 1;
5490 ld_st:
bdaaa2e1 5491 /* Itbl support may require additional care here. */
252b5132
RH
5492 if (mask == M_LWC1_AB
5493 || mask == M_SWC1_AB
5494 || mask == M_LDC1_AB
5495 || mask == M_SDC1_AB
5496 || mask == M_L_DAB
5497 || mask == M_S_DAB)
5498 fmt = "T,o(b)";
5499 else if (coproc)
5500 fmt = "E,o(b)";
5501 else
5502 fmt = "t,o(b)";
5503
afdbd6d0
CD
5504 /* For embedded PIC, we allow loads where the offset is calculated
5505 by subtracting a symbol in the current segment from an unknown
5506 symbol, relative to a base register, e.g.:
5507 <op> $treg, <sym>-<localsym>($breg)
5508 This is used by the compiler for switch statements. */
76b3015f 5509 if (mips_pic == EMBEDDED_PIC
afdbd6d0
CD
5510 && offset_expr.X_op == O_subtract
5511 && (symbol_constant_p (offset_expr.X_op_symbol)
5512 ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
5513 : (symbol_equated_p (offset_expr.X_op_symbol)
5514 && (S_GET_SEGMENT
5515 (symbol_get_value_expression (offset_expr.X_op_symbol)
5516 ->X_add_symbol)
5517 == now_seg)))
5518 && breg != 0
5519 && (offset_expr.X_add_number == 0
5520 || OUTPUT_FLAVOR == bfd_target_elf_flavour))
5521 {
5522 /* For this case, we output the instructions:
5523 lui $tempreg,<sym> (BFD_RELOC_PCREL_HI16_S)
5524 addiu $tempreg,$tempreg,$breg
5525 <op> $treg,<sym>($tempreg) (BFD_RELOC_PCREL_LO16)
5526 If the relocation would fit entirely in 16 bits, it would be
5527 nice to emit:
5528 <op> $treg,<sym>($breg) (BFD_RELOC_PCREL_LO16)
5529 instead, but that seems quite difficult. */
5530 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5531 tempreg, (int) BFD_RELOC_PCREL_HI16_S);
5532 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5533 ((bfd_arch_bits_per_address (stdoutput) == 32
5534 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
5535 ? "addu" : "daddu"),
5536 "d,v,t", tempreg, tempreg, breg);
5537 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt, treg,
5538 (int) BFD_RELOC_PCREL_LO16, tempreg);
5539 if (! used_at)
5540 return;
5541 break;
5542 }
5543
252b5132
RH
5544 if (offset_expr.X_op != O_constant
5545 && offset_expr.X_op != O_symbol)
5546 {
5547 as_bad (_("expression too complex"));
5548 offset_expr.X_op = O_constant;
5549 }
5550
5551 /* A constant expression in PIC code can be handled just as it
5552 is in non PIC code. */
5553 if (mips_pic == NO_PIC
5554 || offset_expr.X_op == O_constant)
5555 {
f9419b05
TS
5556 char *p;
5557
252b5132
RH
5558 /* If this is a reference to a GP relative symbol, and there
5559 is no base register, we want
cdf6fd85 5560 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
5561 Otherwise, if there is no base register, we want
5562 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5563 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5564 If we have a constant, we need two instructions anyhow,
5565 so we always use the latter form.
5566
5567 If we have a base register, and this is a reference to a
5568 GP relative symbol, we want
5569 addu $tempreg,$breg,$gp
cdf6fd85 5570 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
5571 Otherwise we want
5572 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5573 addu $tempreg,$tempreg,$breg
5574 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245 5575 With a constant we always use the latter case.
76b3015f 5576
d6bc6245
TS
5577 With 64bit address space and no base register and $at usable,
5578 we want
5579 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5580 lui $at,<sym> (BFD_RELOC_HI16_S)
5581 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5582 dsll32 $tempreg,0
5583 daddu $tempreg,$at
5584 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5585 If we have a base register, we want
5586 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5587 lui $at,<sym> (BFD_RELOC_HI16_S)
5588 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5589 daddu $at,$breg
5590 dsll32 $tempreg,0
5591 daddu $tempreg,$at
5592 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5593
5594 Without $at we can't generate the optimal path for superscalar
5595 processors here since this would require two temporary registers.
5596 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5597 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5598 dsll $tempreg,16
5599 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5600 dsll $tempreg,16
5601 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5602 If we have a base register, we want
5603 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5604 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5605 dsll $tempreg,16
5606 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5607 dsll $tempreg,16
5608 daddu $tempreg,$tempreg,$breg
5609 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6373ee54
CD
5610
5611 If we have 64-bit addresses, as an optimization, for
5612 addresses which are 32-bit constants (e.g. kseg0/kseg1
5613 addresses) we fall back to the 32-bit address generation
5614 mechanism since it is more efficient. This code should
5615 probably attempt to generate 64-bit constants more
5616 efficiently in general.
d6bc6245 5617 */
6373ee54
CD
5618 if (HAVE_64BIT_ADDRESSES
5619 && !(offset_expr.X_op == O_constant
5620 && IS_SEXT_32BIT_NUM (offset_expr.X_add_number)))
d6bc6245
TS
5621 {
5622 p = NULL;
5623
5624 /* We don't do GP optimization for now because RELAX_ENCODE can't
5625 hold the data for such large chunks. */
5626
5627 if (used_at == 0)
5628 {
5629 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5630 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5631 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5632 AT, (int) BFD_RELOC_HI16_S);
5633 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5634 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5635 if (breg != 0)
2396cfb9
TS
5636 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5637 "d,v,t", AT, AT, breg);
5638 macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
5639 "d,w,<", tempreg, tempreg, 0);
5640 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5641 "d,v,t", tempreg, tempreg, AT);
d6bc6245
TS
5642 macro_build (p, &icnt, &offset_expr, s,
5643 fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5644 used_at = 1;
5645 }
5646 else
5647 {
5648 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5649 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5650 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5651 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
2396cfb9
TS
5652 macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5653 "d,w,<", tempreg, tempreg, 16);
d6bc6245
TS
5654 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5655 tempreg, tempreg, (int) BFD_RELOC_HI16_S);
2396cfb9
TS
5656 macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5657 "d,w,<", tempreg, tempreg, 16);
d6bc6245 5658 if (breg != 0)
2396cfb9
TS
5659 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5660 "d,v,t", tempreg, tempreg, breg);
d6bc6245
TS
5661 macro_build (p, &icnt, &offset_expr, s,
5662 fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5663 }
5664
5665 return;
5666 }
76b3015f 5667
252b5132
RH
5668 if (breg == 0)
5669 {
e7d556df 5670 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
252b5132
RH
5671 || nopic_need_relax (offset_expr.X_add_symbol, 1))
5672 p = NULL;
5673 else
5674 {
5675 frag_grow (20);
5676 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
c9914766
TS
5677 treg, (int) BFD_RELOC_GPREL16,
5678 mips_gp_register);
252b5132
RH
5679 p = frag_var (rs_machine_dependent, 8, 0,
5680 RELAX_ENCODE (4, 8, 0, 4, 0,
5681 (mips_opts.warn_about_macros
5682 || (used_at
5683 && mips_opts.noat))),
956cd1d6 5684 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5685 used_at = 0;
5686 }
5687 macro_build_lui (p, &icnt, &offset_expr, tempreg);
5688 if (p != NULL)
5689 p += 4;
5690 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5691 (int) BFD_RELOC_LO16, tempreg);
5692 }
5693 else
5694 {
e7d556df 5695 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
252b5132
RH
5696 || nopic_need_relax (offset_expr.X_add_symbol, 1))
5697 p = NULL;
5698 else
5699 {
5700 frag_grow (28);
5701 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5702 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 5703 "d,v,t", tempreg, breg, mips_gp_register);
252b5132 5704 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
cdf6fd85 5705 treg, (int) BFD_RELOC_GPREL16, tempreg);
252b5132
RH
5706 p = frag_var (rs_machine_dependent, 12, 0,
5707 RELAX_ENCODE (8, 12, 0, 8, 0, 0),
956cd1d6 5708 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5709 }
5710 macro_build_lui (p, &icnt, &offset_expr, tempreg);
5711 if (p != NULL)
5712 p += 4;
5713 macro_build (p, &icnt, (expressionS *) NULL,
ca4e0257 5714 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5715 "d,v,t", tempreg, tempreg, breg);
5716 if (p != NULL)
5717 p += 4;
5718 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5719 (int) BFD_RELOC_LO16, tempreg);
5720 }
5721 }
5722 else if (mips_pic == SVR4_PIC && ! mips_big_got)
5723 {
f9419b05
TS
5724 char *p;
5725
252b5132
RH
5726 /* If this is a reference to an external symbol, we want
5727 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5728 nop
5729 <op> $treg,0($tempreg)
5730 Otherwise we want
5731 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5732 nop
5733 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5734 <op> $treg,0($tempreg)
5735 If there is a base register, we add it to $tempreg before
5736 the <op>. If there is a constant, we stick it in the
5737 <op> instruction. We don't handle constants larger than
5738 16 bits, because we have no way to load the upper 16 bits
5739 (actually, we could handle them for the subset of cases
5740 in which we are not using $at). */
5741 assert (offset_expr.X_op == O_symbol);
5742 expr1.X_add_number = offset_expr.X_add_number;
5743 offset_expr.X_add_number = 0;
5744 if (expr1.X_add_number < -0x8000
5745 || expr1.X_add_number >= 0x8000)
5746 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5747 frag_grow (20);
5748 macro_build ((char *) NULL, &icnt, &offset_expr,
c9914766
TS
5749 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", tempreg,
5750 (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132 5751 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
bdaaa2e1 5752 p = frag_var (rs_machine_dependent, 4, 0,
252b5132 5753 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
c4e7957c 5754 offset_expr.X_add_symbol, 0, NULL);
252b5132 5755 macro_build (p, &icnt, &offset_expr,
ca4e0257 5756 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
5757 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5758 if (breg != 0)
5759 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5760 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5761 "d,v,t", tempreg, tempreg, breg);
5762 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5763 (int) BFD_RELOC_LO16, tempreg);
5764 }
5765 else if (mips_pic == SVR4_PIC)
5766 {
5767 int gpdel;
f9419b05 5768 char *p;
252b5132
RH
5769
5770 /* If this is a reference to an external symbol, we want
5771 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5772 addu $tempreg,$tempreg,$gp
5773 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5774 <op> $treg,0($tempreg)
5775 Otherwise we want
5776 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5777 nop
5778 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5779 <op> $treg,0($tempreg)
5780 If there is a base register, we add it to $tempreg before
5781 the <op>. If there is a constant, we stick it in the
5782 <op> instruction. We don't handle constants larger than
5783 16 bits, because we have no way to load the upper 16 bits
5784 (actually, we could handle them for the subset of cases
5785 in which we are not using $at). */
5786 assert (offset_expr.X_op == O_symbol);
5787 expr1.X_add_number = offset_expr.X_add_number;
5788 offset_expr.X_add_number = 0;
5789 if (expr1.X_add_number < -0x8000
5790 || expr1.X_add_number >= 0x8000)
5791 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
f7ea7ef2 5792 if (reg_needs_delay (mips_gp_register))
252b5132
RH
5793 gpdel = 4;
5794 else
5795 gpdel = 0;
5796 frag_grow (36);
5797 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5798 tempreg, (int) BFD_RELOC_MIPS_GOT_HI16);
5799 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5800 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 5801 "d,v,t", tempreg, tempreg, mips_gp_register);
252b5132 5802 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 5803 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
5804 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_LO16,
5805 tempreg);
5806 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5807 RELAX_ENCODE (12, 12 + gpdel, gpdel, 8 + gpdel, 0, 0),
c4e7957c 5808 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5809 if (gpdel > 0)
5810 {
5811 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5812 p += 4;
5813 }
5814 macro_build (p, &icnt, &offset_expr,
ca4e0257 5815 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
c9914766
TS
5816 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16,
5817 mips_gp_register);
252b5132
RH
5818 p += 4;
5819 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5820 p += 4;
5821 macro_build (p, &icnt, &offset_expr,
ca4e0257 5822 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
5823 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5824 if (breg != 0)
5825 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5826 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5827 "d,v,t", tempreg, tempreg, breg);
5828 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5829 (int) BFD_RELOC_LO16, tempreg);
5830 }
5831 else if (mips_pic == EMBEDDED_PIC)
5832 {
5833 /* If there is no base register, we want
cdf6fd85 5834 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
5835 If there is a base register, we want
5836 addu $tempreg,$breg,$gp
cdf6fd85 5837 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
5838 */
5839 assert (offset_expr.X_op == O_symbol);
5840 if (breg == 0)
5841 {
5842 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
c9914766 5843 treg, (int) BFD_RELOC_GPREL16, mips_gp_register);
252b5132
RH
5844 used_at = 0;
5845 }
5846 else
5847 {
5848 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5849 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 5850 "d,v,t", tempreg, breg, mips_gp_register);
252b5132 5851 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
cdf6fd85 5852 treg, (int) BFD_RELOC_GPREL16, tempreg);
252b5132
RH
5853 }
5854 }
5855 else
5856 abort ();
5857
5858 if (! used_at)
5859 return;
5860
5861 break;
5862
5863 case M_LI:
5864 case M_LI_S:
5865 load_register (&icnt, treg, &imm_expr, 0);
5866 return;
5867
5868 case M_DLI:
5869 load_register (&icnt, treg, &imm_expr, 1);
5870 return;
5871
5872 case M_LI_SS:
5873 if (imm_expr.X_op == O_constant)
5874 {
5875 load_register (&icnt, AT, &imm_expr, 0);
5876 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5877 "mtc1", "t,G", AT, treg);
5878 break;
5879 }
5880 else
5881 {
5882 assert (offset_expr.X_op == O_symbol
5883 && strcmp (segment_name (S_GET_SEGMENT
5884 (offset_expr.X_add_symbol)),
5885 ".lit4") == 0
5886 && offset_expr.X_add_number == 0);
5887 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
c9914766 5888 treg, (int) BFD_RELOC_MIPS_LITERAL, mips_gp_register);
252b5132
RH
5889 return;
5890 }
5891
5892 case M_LI_D:
ca4e0257
RS
5893 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
5894 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
5895 order 32 bits of the value and the low order 32 bits are either
5896 zero or in OFFSET_EXPR. */
252b5132
RH
5897 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
5898 {
ca4e0257 5899 if (HAVE_64BIT_GPRS)
252b5132
RH
5900 load_register (&icnt, treg, &imm_expr, 1);
5901 else
5902 {
5903 int hreg, lreg;
5904
5905 if (target_big_endian)
5906 {
5907 hreg = treg;
5908 lreg = treg + 1;
5909 }
5910 else
5911 {
5912 hreg = treg + 1;
5913 lreg = treg;
5914 }
5915
5916 if (hreg <= 31)
5917 load_register (&icnt, hreg, &imm_expr, 0);
5918 if (lreg <= 31)
5919 {
5920 if (offset_expr.X_op == O_absent)
ea1fb5dc 5921 move_register (&icnt, lreg, 0);
252b5132
RH
5922 else
5923 {
5924 assert (offset_expr.X_op == O_constant);
5925 load_register (&icnt, lreg, &offset_expr, 0);
5926 }
5927 }
5928 }
5929 return;
5930 }
5931
5932 /* We know that sym is in the .rdata section. First we get the
5933 upper 16 bits of the address. */
5934 if (mips_pic == NO_PIC)
5935 {
956cd1d6 5936 macro_build_lui (NULL, &icnt, &offset_expr, AT);
252b5132
RH
5937 }
5938 else if (mips_pic == SVR4_PIC)
5939 {
5940 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 5941 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
c9914766
TS
5942 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
5943 mips_gp_register);
252b5132
RH
5944 }
5945 else if (mips_pic == EMBEDDED_PIC)
5946 {
5947 /* For embedded PIC we pick up the entire address off $gp in
5948 a single instruction. */
5949 macro_build ((char *) NULL, &icnt, &offset_expr,
c9914766
TS
5950 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j", AT,
5951 mips_gp_register, (int) BFD_RELOC_GPREL16);
252b5132
RH
5952 offset_expr.X_op = O_constant;
5953 offset_expr.X_add_number = 0;
5954 }
5955 else
5956 abort ();
bdaaa2e1 5957
252b5132 5958 /* Now we load the register(s). */
ca4e0257 5959 if (HAVE_64BIT_GPRS)
252b5132
RH
5960 macro_build ((char *) NULL, &icnt, &offset_expr, "ld", "t,o(b)",
5961 treg, (int) BFD_RELOC_LO16, AT);
5962 else
5963 {
5964 macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
5965 treg, (int) BFD_RELOC_LO16, AT);
f9419b05 5966 if (treg != RA)
252b5132
RH
5967 {
5968 /* FIXME: How in the world do we deal with the possible
5969 overflow here? */
5970 offset_expr.X_add_number += 4;
5971 macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
5972 treg + 1, (int) BFD_RELOC_LO16, AT);
5973 }
5974 }
5975
5976 /* To avoid confusion in tc_gen_reloc, we must ensure that this
5977 does not become a variant frag. */
5978 frag_wane (frag_now);
5979 frag_new (0);
5980
5981 break;
5982
5983 case M_LI_DD:
ca4e0257
RS
5984 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
5985 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
5986 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
5987 the value and the low order 32 bits are either zero or in
5988 OFFSET_EXPR. */
252b5132
RH
5989 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
5990 {
ca4e0257
RS
5991 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_FPRS);
5992 if (HAVE_64BIT_FPRS)
5993 {
5994 assert (HAVE_64BIT_GPRS);
5995 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5996 "dmtc1", "t,S", AT, treg);
5997 }
252b5132
RH
5998 else
5999 {
6000 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6001 "mtc1", "t,G", AT, treg + 1);
6002 if (offset_expr.X_op == O_absent)
6003 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6004 "mtc1", "t,G", 0, treg);
6005 else
6006 {
6007 assert (offset_expr.X_op == O_constant);
6008 load_register (&icnt, AT, &offset_expr, 0);
6009 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6010 "mtc1", "t,G", AT, treg);
6011 }
6012 }
6013 break;
6014 }
6015
6016 assert (offset_expr.X_op == O_symbol
6017 && offset_expr.X_add_number == 0);
6018 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
6019 if (strcmp (s, ".lit8") == 0)
6020 {
e7af610e 6021 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
6022 {
6023 macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
c9914766
TS
6024 "T,o(b)", treg, (int) BFD_RELOC_MIPS_LITERAL,
6025 mips_gp_register);
252b5132
RH
6026 return;
6027 }
c9914766 6028 breg = mips_gp_register;
252b5132
RH
6029 r = BFD_RELOC_MIPS_LITERAL;
6030 goto dob;
6031 }
6032 else
6033 {
6034 assert (strcmp (s, RDATA_SECTION_NAME) == 0);
6035 if (mips_pic == SVR4_PIC)
6036 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 6037 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
c9914766
TS
6038 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6039 mips_gp_register);
252b5132
RH
6040 else
6041 {
6042 /* FIXME: This won't work for a 64 bit address. */
956cd1d6 6043 macro_build_lui (NULL, &icnt, &offset_expr, AT);
252b5132 6044 }
bdaaa2e1 6045
e7af610e 6046 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
6047 {
6048 macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
6049 "T,o(b)", treg, (int) BFD_RELOC_LO16, AT);
6050
6051 /* To avoid confusion in tc_gen_reloc, we must ensure
6052 that this does not become a variant frag. */
6053 frag_wane (frag_now);
6054 frag_new (0);
6055
6056 break;
6057 }
6058 breg = AT;
6059 r = BFD_RELOC_LO16;
6060 goto dob;
6061 }
6062
6063 case M_L_DOB:
ec68c924 6064 if (mips_arch == CPU_R4650)
252b5132
RH
6065 {
6066 as_bad (_("opcode not supported on this processor"));
6067 return;
6068 }
6069 /* Even on a big endian machine $fn comes before $fn+1. We have
6070 to adjust when loading from memory. */
6071 r = BFD_RELOC_LO16;
6072 dob:
e7af610e 6073 assert (mips_opts.isa == ISA_MIPS1);
252b5132
RH
6074 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6075 target_big_endian ? treg + 1 : treg,
6076 (int) r, breg);
6077 /* FIXME: A possible overflow which I don't know how to deal
6078 with. */
6079 offset_expr.X_add_number += 4;
6080 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
6081 target_big_endian ? treg : treg + 1,
6082 (int) r, breg);
6083
6084 /* To avoid confusion in tc_gen_reloc, we must ensure that this
6085 does not become a variant frag. */
6086 frag_wane (frag_now);
6087 frag_new (0);
6088
6089 if (breg != AT)
6090 return;
6091 break;
6092
6093 case M_L_DAB:
6094 /*
6095 * The MIPS assembler seems to check for X_add_number not
6096 * being double aligned and generating:
6097 * lui at,%hi(foo+1)
6098 * addu at,at,v1
6099 * addiu at,at,%lo(foo+1)
6100 * lwc1 f2,0(at)
6101 * lwc1 f3,4(at)
6102 * But, the resulting address is the same after relocation so why
6103 * generate the extra instruction?
6104 */
ec68c924 6105 if (mips_arch == CPU_R4650)
252b5132
RH
6106 {
6107 as_bad (_("opcode not supported on this processor"));
6108 return;
6109 }
bdaaa2e1 6110 /* Itbl support may require additional care here. */
252b5132 6111 coproc = 1;
e7af610e 6112 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
6113 {
6114 s = "ldc1";
6115 goto ld;
6116 }
6117
6118 s = "lwc1";
6119 fmt = "T,o(b)";
6120 goto ldd_std;
6121
6122 case M_S_DAB:
ec68c924 6123 if (mips_arch == CPU_R4650)
252b5132
RH
6124 {
6125 as_bad (_("opcode not supported on this processor"));
6126 return;
6127 }
6128
e7af610e 6129 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
6130 {
6131 s = "sdc1";
6132 goto st;
6133 }
6134
6135 s = "swc1";
6136 fmt = "T,o(b)";
bdaaa2e1 6137 /* Itbl support may require additional care here. */
252b5132
RH
6138 coproc = 1;
6139 goto ldd_std;
6140
6141 case M_LD_AB:
ca4e0257 6142 if (HAVE_64BIT_GPRS)
252b5132
RH
6143 {
6144 s = "ld";
6145 goto ld;
6146 }
6147
6148 s = "lw";
6149 fmt = "t,o(b)";
6150 goto ldd_std;
6151
6152 case M_SD_AB:
ca4e0257 6153 if (HAVE_64BIT_GPRS)
252b5132
RH
6154 {
6155 s = "sd";
6156 goto st;
6157 }
6158
6159 s = "sw";
6160 fmt = "t,o(b)";
6161
6162 ldd_std:
afdbd6d0
CD
6163 /* We do _not_ bother to allow embedded PIC (symbol-local_symbol)
6164 loads for the case of doing a pair of loads to simulate an 'ld'.
6165 This is not currently done by the compiler, and assembly coders
6166 writing embedded-pic code can cope. */
6167
252b5132
RH
6168 if (offset_expr.X_op != O_symbol
6169 && offset_expr.X_op != O_constant)
6170 {
6171 as_bad (_("expression too complex"));
6172 offset_expr.X_op = O_constant;
6173 }
6174
6175 /* Even on a big endian machine $fn comes before $fn+1. We have
6176 to adjust when loading from memory. We set coproc if we must
6177 load $fn+1 first. */
bdaaa2e1 6178 /* Itbl support may require additional care here. */
252b5132
RH
6179 if (! target_big_endian)
6180 coproc = 0;
6181
6182 if (mips_pic == NO_PIC
6183 || offset_expr.X_op == O_constant)
6184 {
f9419b05
TS
6185 char *p;
6186
252b5132 6187 /* If this is a reference to a GP relative symbol, we want
cdf6fd85
TS
6188 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6189 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
6190 If we have a base register, we use this
6191 addu $at,$breg,$gp
cdf6fd85
TS
6192 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6193 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
6194 If this is not a GP relative symbol, we want
6195 lui $at,<sym> (BFD_RELOC_HI16_S)
6196 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6197 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6198 If there is a base register, we add it to $at after the
6199 lui instruction. If there is a constant, we always use
6200 the last case. */
e7d556df 6201 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
252b5132
RH
6202 || nopic_need_relax (offset_expr.X_add_symbol, 1))
6203 {
6204 p = NULL;
6205 used_at = 1;
6206 }
6207 else
6208 {
6209 int off;
6210
6211 if (breg == 0)
6212 {
6213 frag_grow (28);
c9914766 6214 tempreg = mips_gp_register;
252b5132
RH
6215 off = 0;
6216 used_at = 0;
6217 }
6218 else
6219 {
6220 frag_grow (36);
6221 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6222 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 6223 "d,v,t", AT, breg, mips_gp_register);
252b5132
RH
6224 tempreg = AT;
6225 off = 4;
6226 used_at = 1;
6227 }
6228
beae10d5 6229 /* Itbl support may require additional care here. */
252b5132
RH
6230 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6231 coproc ? treg + 1 : treg,
cdf6fd85 6232 (int) BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6233 offset_expr.X_add_number += 4;
6234
6235 /* Set mips_optimize to 2 to avoid inserting an
6236 undesired nop. */
6237 hold_mips_optimize = mips_optimize;
6238 mips_optimize = 2;
beae10d5 6239 /* Itbl support may require additional care here. */
252b5132
RH
6240 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6241 coproc ? treg : treg + 1,
cdf6fd85 6242 (int) BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6243 mips_optimize = hold_mips_optimize;
6244
6245 p = frag_var (rs_machine_dependent, 12 + off, 0,
6246 RELAX_ENCODE (8 + off, 12 + off, 0, 4 + off, 1,
6247 used_at && mips_opts.noat),
956cd1d6 6248 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
6249
6250 /* We just generated two relocs. When tc_gen_reloc
6251 handles this case, it will skip the first reloc and
6252 handle the second. The second reloc already has an
6253 extra addend of 4, which we added above. We must
6254 subtract it out, and then subtract another 4 to make
6255 the first reloc come out right. The second reloc
6256 will come out right because we are going to add 4 to
6257 offset_expr when we build its instruction below.
6258
6259 If we have a symbol, then we don't want to include
6260 the offset, because it will wind up being included
6261 when we generate the reloc. */
6262
6263 if (offset_expr.X_op == O_constant)
6264 offset_expr.X_add_number -= 8;
6265 else
6266 {
6267 offset_expr.X_add_number = -4;
6268 offset_expr.X_op = O_constant;
6269 }
6270 }
6271 macro_build_lui (p, &icnt, &offset_expr, AT);
6272 if (p != NULL)
6273 p += 4;
6274 if (breg != 0)
6275 {
6276 macro_build (p, &icnt, (expressionS *) NULL,
ca4e0257 6277 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
6278 "d,v,t", AT, breg, AT);
6279 if (p != NULL)
6280 p += 4;
6281 }
beae10d5 6282 /* Itbl support may require additional care here. */
252b5132
RH
6283 macro_build (p, &icnt, &offset_expr, s, fmt,
6284 coproc ? treg + 1 : treg,
6285 (int) BFD_RELOC_LO16, AT);
6286 if (p != NULL)
6287 p += 4;
6288 /* FIXME: How do we handle overflow here? */
6289 offset_expr.X_add_number += 4;
beae10d5 6290 /* Itbl support may require additional care here. */
252b5132
RH
6291 macro_build (p, &icnt, &offset_expr, s, fmt,
6292 coproc ? treg : treg + 1,
6293 (int) BFD_RELOC_LO16, AT);
bdaaa2e1 6294 }
252b5132
RH
6295 else if (mips_pic == SVR4_PIC && ! mips_big_got)
6296 {
6297 int off;
6298
6299 /* If this is a reference to an external symbol, we want
6300 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6301 nop
6302 <op> $treg,0($at)
6303 <op> $treg+1,4($at)
6304 Otherwise we want
6305 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6306 nop
6307 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6308 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6309 If there is a base register we add it to $at before the
6310 lwc1 instructions. If there is a constant we include it
6311 in the lwc1 instructions. */
6312 used_at = 1;
6313 expr1.X_add_number = offset_expr.X_add_number;
6314 offset_expr.X_add_number = 0;
6315 if (expr1.X_add_number < -0x8000
6316 || expr1.X_add_number >= 0x8000 - 4)
6317 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6318 if (breg == 0)
6319 off = 0;
6320 else
6321 off = 4;
6322 frag_grow (24 + off);
6323 macro_build ((char *) NULL, &icnt, &offset_expr,
c9914766
TS
6324 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", AT,
6325 (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132
RH
6326 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6327 if (breg != 0)
6328 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6329 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132 6330 "d,v,t", AT, breg, AT);
beae10d5 6331 /* Itbl support may require additional care here. */
252b5132
RH
6332 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6333 coproc ? treg + 1 : treg,
6334 (int) BFD_RELOC_LO16, AT);
6335 expr1.X_add_number += 4;
6336
6337 /* Set mips_optimize to 2 to avoid inserting an undesired
6338 nop. */
6339 hold_mips_optimize = mips_optimize;
6340 mips_optimize = 2;
beae10d5 6341 /* Itbl support may require additional care here. */
252b5132
RH
6342 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6343 coproc ? treg : treg + 1,
6344 (int) BFD_RELOC_LO16, AT);
6345 mips_optimize = hold_mips_optimize;
6346
6347 (void) frag_var (rs_machine_dependent, 0, 0,
6348 RELAX_ENCODE (0, 0, -16 - off, -8, 1, 0),
c4e7957c 6349 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
6350 }
6351 else if (mips_pic == SVR4_PIC)
6352 {
6353 int gpdel, off;
f9419b05 6354 char *p;
252b5132
RH
6355
6356 /* If this is a reference to an external symbol, we want
6357 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6358 addu $at,$at,$gp
6359 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
6360 nop
6361 <op> $treg,0($at)
6362 <op> $treg+1,4($at)
6363 Otherwise we want
6364 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6365 nop
6366 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6367 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6368 If there is a base register we add it to $at before the
6369 lwc1 instructions. If there is a constant we include it
6370 in the lwc1 instructions. */
6371 used_at = 1;
6372 expr1.X_add_number = offset_expr.X_add_number;
6373 offset_expr.X_add_number = 0;
6374 if (expr1.X_add_number < -0x8000
6375 || expr1.X_add_number >= 0x8000 - 4)
6376 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
c9914766 6377 if (reg_needs_delay (mips_gp_register))
252b5132
RH
6378 gpdel = 4;
6379 else
6380 gpdel = 0;
6381 if (breg == 0)
6382 off = 0;
6383 else
6384 off = 4;
6385 frag_grow (56);
6386 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
6387 AT, (int) BFD_RELOC_MIPS_GOT_HI16);
6388 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6389 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 6390 "d,v,t", AT, AT, mips_gp_register);
252b5132 6391 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 6392 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
6393 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT_LO16, AT);
6394 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6395 if (breg != 0)
6396 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6397 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132 6398 "d,v,t", AT, breg, AT);
beae10d5 6399 /* Itbl support may require additional care here. */
252b5132
RH
6400 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6401 coproc ? treg + 1 : treg,
6402 (int) BFD_RELOC_LO16, AT);
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 ((char *) NULL, &icnt, &expr1, s, fmt,
6411 coproc ? treg : treg + 1,
6412 (int) BFD_RELOC_LO16, AT);
6413 mips_optimize = hold_mips_optimize;
6414 expr1.X_add_number -= 4;
6415
6416 p = frag_var (rs_machine_dependent, 16 + gpdel + off, 0,
6417 RELAX_ENCODE (24 + off, 16 + gpdel + off, gpdel,
6418 8 + gpdel + off, 1, 0),
c4e7957c 6419 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
6420 if (gpdel > 0)
6421 {
6422 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6423 p += 4;
6424 }
6425 macro_build (p, &icnt, &offset_expr,
ca4e0257 6426 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
c9914766
TS
6427 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6428 mips_gp_register);
252b5132
RH
6429 p += 4;
6430 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6431 p += 4;
6432 if (breg != 0)
6433 {
6434 macro_build (p, &icnt, (expressionS *) NULL,
ca4e0257 6435 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
6436 "d,v,t", AT, breg, AT);
6437 p += 4;
6438 }
beae10d5 6439 /* Itbl support may require additional care here. */
252b5132
RH
6440 macro_build (p, &icnt, &expr1, s, fmt,
6441 coproc ? treg + 1 : treg,
6442 (int) BFD_RELOC_LO16, AT);
6443 p += 4;
6444 expr1.X_add_number += 4;
6445
6446 /* Set mips_optimize to 2 to avoid inserting an undesired
6447 nop. */
6448 hold_mips_optimize = mips_optimize;
6449 mips_optimize = 2;
beae10d5 6450 /* Itbl support may require additional care here. */
252b5132
RH
6451 macro_build (p, &icnt, &expr1, s, fmt,
6452 coproc ? treg : treg + 1,
6453 (int) BFD_RELOC_LO16, AT);
6454 mips_optimize = hold_mips_optimize;
6455 }
6456 else if (mips_pic == EMBEDDED_PIC)
6457 {
6458 /* If there is no base register, we use
cdf6fd85
TS
6459 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6460 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
6461 If we have a base register, we use
6462 addu $at,$breg,$gp
cdf6fd85
TS
6463 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6464 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
6465 */
6466 if (breg == 0)
6467 {
c9914766 6468 tempreg = mips_gp_register;
252b5132
RH
6469 used_at = 0;
6470 }
6471 else
6472 {
6473 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6474 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 6475 "d,v,t", AT, breg, mips_gp_register);
252b5132
RH
6476 tempreg = AT;
6477 used_at = 1;
6478 }
6479
beae10d5 6480 /* Itbl support may require additional care here. */
252b5132
RH
6481 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6482 coproc ? treg + 1 : treg,
cdf6fd85 6483 (int) BFD_RELOC_GPREL16, tempreg);
252b5132 6484 offset_expr.X_add_number += 4;
beae10d5 6485 /* Itbl support may require additional care here. */
252b5132
RH
6486 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6487 coproc ? treg : treg + 1,
cdf6fd85 6488 (int) BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6489 }
6490 else
6491 abort ();
6492
6493 if (! used_at)
6494 return;
6495
6496 break;
6497
6498 case M_LD_OB:
6499 s = "lw";
6500 goto sd_ob;
6501 case M_SD_OB:
6502 s = "sw";
6503 sd_ob:
ca4e0257 6504 assert (HAVE_32BIT_ADDRESSES);
252b5132
RH
6505 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
6506 (int) BFD_RELOC_LO16, breg);
6507 offset_expr.X_add_number += 4;
6508 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg + 1,
6509 (int) BFD_RELOC_LO16, breg);
6510 return;
6511
6512 /* New code added to support COPZ instructions.
6513 This code builds table entries out of the macros in mip_opcodes.
6514 R4000 uses interlocks to handle coproc delays.
6515 Other chips (like the R3000) require nops to be inserted for delays.
6516
f72c8c98 6517 FIXME: Currently, we require that the user handle delays.
252b5132
RH
6518 In order to fill delay slots for non-interlocked chips,
6519 we must have a way to specify delays based on the coprocessor.
6520 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
6521 What are the side-effects of the cop instruction?
6522 What cache support might we have and what are its effects?
6523 Both coprocessor & memory require delays. how long???
bdaaa2e1 6524 What registers are read/set/modified?
252b5132
RH
6525
6526 If an itbl is provided to interpret cop instructions,
bdaaa2e1 6527 this knowledge can be encoded in the itbl spec. */
252b5132
RH
6528
6529 case M_COP0:
6530 s = "c0";
6531 goto copz;
6532 case M_COP1:
6533 s = "c1";
6534 goto copz;
6535 case M_COP2:
6536 s = "c2";
6537 goto copz;
6538 case M_COP3:
6539 s = "c3";
6540 copz:
6541 /* For now we just do C (same as Cz). The parameter will be
6542 stored in insn_opcode by mips_ip. */
6543 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "C",
6544 ip->insn_opcode);
6545 return;
6546
ea1fb5dc
RS
6547 case M_MOVE:
6548 move_register (&icnt, dreg, sreg);
6549 return;
6550
252b5132
RH
6551#ifdef LOSING_COMPILER
6552 default:
6553 /* Try and see if this is a new itbl instruction.
6554 This code builds table entries out of the macros in mip_opcodes.
6555 FIXME: For now we just assemble the expression and pass it's
6556 value along as a 32-bit immediate.
bdaaa2e1 6557 We may want to have the assembler assemble this value,
252b5132
RH
6558 so that we gain the assembler's knowledge of delay slots,
6559 symbols, etc.
6560 Would it be more efficient to use mask (id) here? */
bdaaa2e1 6561 if (itbl_have_entries
252b5132 6562 && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
beae10d5 6563 {
252b5132
RH
6564 s = ip->insn_mo->name;
6565 s2 = "cop3";
6566 coproc = ITBL_DECODE_PNUM (immed_expr);;
6567 macro_build ((char *) NULL, &icnt, &immed_expr, s, "C");
6568 return;
beae10d5 6569 }
252b5132
RH
6570 macro2 (ip);
6571 return;
6572 }
6573 if (mips_opts.noat)
6574 as_warn (_("Macro used $at after \".set noat\""));
6575}
bdaaa2e1 6576
252b5132
RH
6577static void
6578macro2 (ip)
6579 struct mips_cl_insn *ip;
6580{
6581 register int treg, sreg, dreg, breg;
6582 int tempreg;
6583 int mask;
6584 int icnt = 0;
6585 int used_at;
6586 expressionS expr1;
6587 const char *s;
6588 const char *s2;
6589 const char *fmt;
6590 int likely = 0;
6591 int dbl = 0;
6592 int coproc = 0;
6593 int lr = 0;
6594 int imm = 0;
6595 int off;
6596 offsetT maxnum;
6597 bfd_reloc_code_real_type r;
6598 char *p;
bdaaa2e1 6599
252b5132
RH
6600 treg = (ip->insn_opcode >> 16) & 0x1f;
6601 dreg = (ip->insn_opcode >> 11) & 0x1f;
6602 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
6603 mask = ip->insn_mo->mask;
bdaaa2e1 6604
252b5132
RH
6605 expr1.X_op = O_constant;
6606 expr1.X_op_symbol = NULL;
6607 expr1.X_add_symbol = NULL;
6608 expr1.X_add_number = 1;
bdaaa2e1 6609
252b5132
RH
6610 switch (mask)
6611 {
6612#endif /* LOSING_COMPILER */
6613
6614 case M_DMUL:
6615 dbl = 1;
6616 case M_MUL:
2396cfb9
TS
6617 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6618 dbl ? "dmultu" : "multu", "s,t", sreg, treg);
6619 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6620 dreg);
252b5132
RH
6621 return;
6622
6623 case M_DMUL_I:
6624 dbl = 1;
6625 case M_MUL_I:
6626 /* The MIPS assembler some times generates shifts and adds. I'm
6627 not trying to be that fancy. GCC should do this for us
6628 anyway. */
6629 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9 6630 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 6631 dbl ? "dmult" : "mult", "s,t", sreg, AT);
2396cfb9
TS
6632 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6633 dreg);
252b5132
RH
6634 break;
6635
6636 case M_DMULO_I:
6637 dbl = 1;
6638 case M_MULO_I:
6639 imm = 1;
6640 goto do_mulo;
6641
6642 case M_DMULO:
6643 dbl = 1;
6644 case M_MULO:
6645 do_mulo:
6646 mips_emit_delays (true);
6647 ++mips_opts.noreorder;
6648 mips_any_noreorder = 1;
6649 if (imm)
6650 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9 6651 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 6652 dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
2396cfb9
TS
6653 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6654 dreg);
6655 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
f9419b05 6656 dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, RA);
2396cfb9
TS
6657 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6658 AT);
252b5132 6659 if (mips_trap)
2396cfb9
TS
6660 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne", "s,t",
6661 dreg, AT);
252b5132
RH
6662 else
6663 {
6664 expr1.X_add_number = 8;
2396cfb9
TS
6665 macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", dreg,
6666 AT);
6667 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6668 0);
6669 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6670 "c", 6);
252b5132
RH
6671 }
6672 --mips_opts.noreorder;
2396cfb9 6673 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d", dreg);
252b5132
RH
6674 break;
6675
6676 case M_DMULOU_I:
6677 dbl = 1;
6678 case M_MULOU_I:
6679 imm = 1;
6680 goto do_mulou;
6681
6682 case M_DMULOU:
6683 dbl = 1;
6684 case M_MULOU:
6685 do_mulou:
6686 mips_emit_delays (true);
6687 ++mips_opts.noreorder;
6688 mips_any_noreorder = 1;
6689 if (imm)
6690 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9 6691 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
252b5132
RH
6692 dbl ? "dmultu" : "multu",
6693 "s,t", sreg, imm ? AT : treg);
2396cfb9
TS
6694 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6695 AT);
6696 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6697 dreg);
252b5132 6698 if (mips_trap)
2396cfb9
TS
6699 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne", "s,t",
6700 AT, 0);
252b5132
RH
6701 else
6702 {
6703 expr1.X_add_number = 8;
6704 macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", AT, 0);
2396cfb9
TS
6705 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6706 0);
6707 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6708 "c", 6);
252b5132
RH
6709 }
6710 --mips_opts.noreorder;
6711 break;
6712
771c7ce4
TS
6713 case M_DROL:
6714 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
6715 "d,v,t", AT, 0, treg);
6716 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
6717 "d,t,s", AT, sreg, AT);
6718 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
6719 "d,t,s", dreg, sreg, treg);
6720 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6721 "d,v,t", dreg, dreg, AT);
6722 break;
6723
252b5132 6724 case M_ROL:
2396cfb9
TS
6725 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
6726 "d,v,t", AT, 0, treg);
6727 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
6728 "d,t,s", AT, sreg, AT);
6729 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
6730 "d,t,s", dreg, sreg, treg);
6731 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6732 "d,v,t", dreg, dreg, AT);
252b5132
RH
6733 break;
6734
771c7ce4
TS
6735 case M_DROL_I:
6736 {
6737 unsigned int rot;
6738 char *l, *r;
6739
6740 if (imm_expr.X_op != O_constant)
6741 as_bad (_("rotate count too large"));
6742 rot = imm_expr.X_add_number & 0x3f;
6743 if (! rot)
6744 break;
6745 l = (rot < 0x20) ? "dsll" : "dsll32";
6746 r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
6747 rot &= 0x1f;
6748 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
6749 "d,w,<", AT, sreg, rot);
6750 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
6751 "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6752 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6753 "d,v,t", dreg, dreg, AT);
6754 }
6755 break;
6756
252b5132 6757 case M_ROL_I:
771c7ce4
TS
6758 {
6759 unsigned int rot;
6760
6761 if (imm_expr.X_op != O_constant)
6762 as_bad (_("rotate count too large"));
6763 rot = imm_expr.X_add_number & 0x1f;
6764 if (! rot)
6765 break;
6766 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
6767 "d,w,<", AT, sreg, rot);
6768 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
6769 "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6770 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6771 "d,v,t", dreg, dreg, AT);
6772 }
6773 break;
6774
6775 case M_DROR:
6776 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
6777 "d,v,t", AT, 0, treg);
6778 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
6779 "d,t,s", AT, sreg, AT);
6780 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
6781 "d,t,s", dreg, sreg, treg);
6782 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6783 "d,v,t", dreg, dreg, AT);
252b5132
RH
6784 break;
6785
6786 case M_ROR:
2396cfb9
TS
6787 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
6788 "d,v,t", AT, 0, treg);
6789 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
6790 "d,t,s", AT, sreg, AT);
6791 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
6792 "d,t,s", dreg, sreg, treg);
6793 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6794 "d,v,t", dreg, dreg, AT);
252b5132
RH
6795 break;
6796
771c7ce4
TS
6797 case M_DROR_I:
6798 {
6799 unsigned int rot;
6800 char *l, *r;
6801
6802 if (imm_expr.X_op != O_constant)
6803 as_bad (_("rotate count too large"));
6804 rot = imm_expr.X_add_number & 0x3f;
6805 if (! rot)
6806 break;
6807 r = (rot < 0x20) ? "dsrl" : "dsrl32";
6808 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
6809 rot &= 0x1f;
6810 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
6811 "d,w,<", AT, sreg, rot);
6812 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
6813 "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6814 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6815 "d,v,t", dreg, dreg, AT);
6816 }
6817 break;
6818
252b5132 6819 case M_ROR_I:
771c7ce4
TS
6820 {
6821 unsigned int rot;
6822
6823 if (imm_expr.X_op != O_constant)
6824 as_bad (_("rotate count too large"));
6825 rot = imm_expr.X_add_number & 0x1f;
6826 if (! rot)
6827 break;
6828 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
6829 "d,w,<", AT, sreg, rot);
6830 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
6831 "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6832 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6833 "d,v,t", dreg, dreg, AT);
6834 }
252b5132
RH
6835 break;
6836
6837 case M_S_DOB:
ec68c924 6838 if (mips_arch == CPU_R4650)
252b5132
RH
6839 {
6840 as_bad (_("opcode not supported on this processor"));
6841 return;
6842 }
e7af610e 6843 assert (mips_opts.isa == ISA_MIPS1);
252b5132
RH
6844 /* Even on a big endian machine $fn comes before $fn+1. We have
6845 to adjust when storing to memory. */
6846 macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
6847 target_big_endian ? treg + 1 : treg,
6848 (int) BFD_RELOC_LO16, breg);
6849 offset_expr.X_add_number += 4;
6850 macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
6851 target_big_endian ? treg : treg + 1,
6852 (int) BFD_RELOC_LO16, breg);
6853 return;
6854
6855 case M_SEQ:
6856 if (sreg == 0)
6857 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6858 treg, (int) BFD_RELOC_LO16);
6859 else if (treg == 0)
6860 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6861 sreg, (int) BFD_RELOC_LO16);
6862 else
6863 {
2396cfb9
TS
6864 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6865 "d,v,t", dreg, sreg, treg);
252b5132
RH
6866 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6867 dreg, (int) BFD_RELOC_LO16);
6868 }
6869 return;
6870
6871 case M_SEQ_I:
6872 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6873 {
6874 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6875 sreg, (int) BFD_RELOC_LO16);
6876 return;
6877 }
6878 if (sreg == 0)
6879 {
6880 as_warn (_("Instruction %s: result is always false"),
6881 ip->insn_mo->name);
ea1fb5dc 6882 move_register (&icnt, dreg, 0);
252b5132
RH
6883 return;
6884 }
6885 if (imm_expr.X_op == O_constant
6886 && imm_expr.X_add_number >= 0
6887 && imm_expr.X_add_number < 0x10000)
6888 {
6889 macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg,
6890 sreg, (int) BFD_RELOC_LO16);
6891 used_at = 0;
6892 }
6893 else if (imm_expr.X_op == O_constant
6894 && imm_expr.X_add_number > -0x8000
6895 && imm_expr.X_add_number < 0)
6896 {
6897 imm_expr.X_add_number = -imm_expr.X_add_number;
6898 macro_build ((char *) NULL, &icnt, &imm_expr,
ca4e0257 6899 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
252b5132
RH
6900 "t,r,j", dreg, sreg,
6901 (int) BFD_RELOC_LO16);
6902 used_at = 0;
6903 }
6904 else
6905 {
4d34fb5f 6906 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
6907 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6908 "d,v,t", dreg, sreg, AT);
252b5132
RH
6909 used_at = 1;
6910 }
6911 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
6912 (int) BFD_RELOC_LO16);
6913 if (used_at)
6914 break;
6915 return;
6916
6917 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
6918 s = "slt";
6919 goto sge;
6920 case M_SGEU:
6921 s = "sltu";
6922 sge:
2396cfb9
TS
6923 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6924 dreg, sreg, treg);
252b5132
RH
6925 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6926 (int) BFD_RELOC_LO16);
6927 return;
6928
6929 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
6930 case M_SGEU_I:
6931 if (imm_expr.X_op == O_constant
6932 && imm_expr.X_add_number >= -0x8000
6933 && imm_expr.X_add_number < 0x8000)
6934 {
6935 macro_build ((char *) NULL, &icnt, &imm_expr,
6936 mask == M_SGE_I ? "slti" : "sltiu",
6937 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6938 used_at = 0;
6939 }
6940 else
6941 {
4d34fb5f 6942 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
6943 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6944 mask == M_SGE_I ? "slt" : "sltu", "d,v,t", dreg, sreg,
6945 AT);
252b5132
RH
6946 used_at = 1;
6947 }
6948 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6949 (int) BFD_RELOC_LO16);
6950 if (used_at)
6951 break;
6952 return;
6953
6954 case M_SGT: /* sreg > treg <==> treg < sreg */
6955 s = "slt";
6956 goto sgt;
6957 case M_SGTU:
6958 s = "sltu";
6959 sgt:
2396cfb9
TS
6960 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6961 dreg, treg, sreg);
252b5132
RH
6962 return;
6963
6964 case M_SGT_I: /* sreg > I <==> I < sreg */
6965 s = "slt";
6966 goto sgti;
6967 case M_SGTU_I:
6968 s = "sltu";
6969 sgti:
4d34fb5f 6970 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
6971 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6972 dreg, AT, sreg);
252b5132
RH
6973 break;
6974
2396cfb9 6975 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
252b5132
RH
6976 s = "slt";
6977 goto sle;
6978 case M_SLEU:
6979 s = "sltu";
6980 sle:
2396cfb9
TS
6981 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6982 dreg, treg, sreg);
252b5132
RH
6983 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6984 (int) BFD_RELOC_LO16);
6985 return;
6986
2396cfb9 6987 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
252b5132
RH
6988 s = "slt";
6989 goto slei;
6990 case M_SLEU_I:
6991 s = "sltu";
6992 slei:
4d34fb5f 6993 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
6994 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6995 dreg, AT, sreg);
252b5132
RH
6996 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6997 (int) BFD_RELOC_LO16);
6998 break;
6999
7000 case M_SLT_I:
7001 if (imm_expr.X_op == O_constant
7002 && imm_expr.X_add_number >= -0x8000
7003 && imm_expr.X_add_number < 0x8000)
7004 {
7005 macro_build ((char *) NULL, &icnt, &imm_expr, "slti", "t,r,j",
7006 dreg, sreg, (int) BFD_RELOC_LO16);
7007 return;
7008 }
4d34fb5f 7009 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
7010 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
7011 dreg, sreg, AT);
252b5132
RH
7012 break;
7013
7014 case M_SLTU_I:
7015 if (imm_expr.X_op == O_constant
7016 && imm_expr.X_add_number >= -0x8000
7017 && imm_expr.X_add_number < 0x8000)
7018 {
7019 macro_build ((char *) NULL, &icnt, &imm_expr, "sltiu", "t,r,j",
7020 dreg, sreg, (int) BFD_RELOC_LO16);
7021 return;
7022 }
4d34fb5f 7023 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
7024 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7025 "d,v,t", dreg, sreg, AT);
252b5132
RH
7026 break;
7027
7028 case M_SNE:
7029 if (sreg == 0)
2396cfb9
TS
7030 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7031 "d,v,t", dreg, 0, treg);
252b5132 7032 else if (treg == 0)
2396cfb9
TS
7033 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7034 "d,v,t", dreg, 0, sreg);
252b5132
RH
7035 else
7036 {
2396cfb9
TS
7037 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7038 "d,v,t", dreg, sreg, treg);
7039 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7040 "d,v,t", dreg, 0, dreg);
252b5132
RH
7041 }
7042 return;
7043
7044 case M_SNE_I:
7045 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7046 {
2396cfb9
TS
7047 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7048 "d,v,t", dreg, 0, sreg);
252b5132
RH
7049 return;
7050 }
7051 if (sreg == 0)
7052 {
7053 as_warn (_("Instruction %s: result is always true"),
7054 ip->insn_mo->name);
7055 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 7056 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
252b5132
RH
7057 "t,r,j", dreg, 0, (int) BFD_RELOC_LO16);
7058 return;
7059 }
7060 if (imm_expr.X_op == O_constant
7061 && imm_expr.X_add_number >= 0
7062 && imm_expr.X_add_number < 0x10000)
7063 {
7064 macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i",
7065 dreg, sreg, (int) BFD_RELOC_LO16);
7066 used_at = 0;
7067 }
7068 else if (imm_expr.X_op == O_constant
7069 && imm_expr.X_add_number > -0x8000
7070 && imm_expr.X_add_number < 0)
7071 {
7072 imm_expr.X_add_number = -imm_expr.X_add_number;
7073 macro_build ((char *) NULL, &icnt, &imm_expr,
ca4e0257 7074 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
252b5132
RH
7075 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7076 used_at = 0;
7077 }
7078 else
7079 {
4d34fb5f 7080 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
7081 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7082 "d,v,t", dreg, sreg, AT);
252b5132
RH
7083 used_at = 1;
7084 }
2396cfb9
TS
7085 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7086 "d,v,t", dreg, 0, dreg);
252b5132
RH
7087 if (used_at)
7088 break;
7089 return;
7090
7091 case M_DSUB_I:
7092 dbl = 1;
7093 case M_SUB_I:
7094 if (imm_expr.X_op == O_constant
7095 && imm_expr.X_add_number > -0x8000
7096 && imm_expr.X_add_number <= 0x8000)
7097 {
7098 imm_expr.X_add_number = -imm_expr.X_add_number;
7099 macro_build ((char *) NULL, &icnt, &imm_expr,
7100 dbl ? "daddi" : "addi",
7101 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7102 return;
7103 }
7104 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9 7105 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 7106 dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
252b5132
RH
7107 break;
7108
7109 case M_DSUBU_I:
7110 dbl = 1;
7111 case M_SUBU_I:
7112 if (imm_expr.X_op == O_constant
7113 && imm_expr.X_add_number > -0x8000
7114 && imm_expr.X_add_number <= 0x8000)
7115 {
7116 imm_expr.X_add_number = -imm_expr.X_add_number;
7117 macro_build ((char *) NULL, &icnt, &imm_expr,
7118 dbl ? "daddiu" : "addiu",
7119 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7120 return;
7121 }
7122 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9 7123 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 7124 dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
252b5132
RH
7125 break;
7126
7127 case M_TEQ_I:
7128 s = "teq";
7129 goto trap;
7130 case M_TGE_I:
7131 s = "tge";
7132 goto trap;
7133 case M_TGEU_I:
7134 s = "tgeu";
7135 goto trap;
7136 case M_TLT_I:
7137 s = "tlt";
7138 goto trap;
7139 case M_TLTU_I:
7140 s = "tltu";
7141 goto trap;
7142 case M_TNE_I:
7143 s = "tne";
7144 trap:
4d34fb5f 7145 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
7146 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "s,t", sreg,
7147 AT);
252b5132
RH
7148 break;
7149
252b5132 7150 case M_TRUNCWS:
43841e91 7151 case M_TRUNCWD:
e7af610e 7152 assert (mips_opts.isa == ISA_MIPS1);
252b5132
RH
7153 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
7154 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
7155
7156 /*
7157 * Is the double cfc1 instruction a bug in the mips assembler;
7158 * or is there a reason for it?
7159 */
7160 mips_emit_delays (true);
7161 ++mips_opts.noreorder;
7162 mips_any_noreorder = 1;
2396cfb9 7163 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
f9419b05 7164 treg, RA);
2396cfb9 7165 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
f9419b05 7166 treg, RA);
2396cfb9 7167 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
252b5132
RH
7168 expr1.X_add_number = 3;
7169 macro_build ((char *) NULL, &icnt, &expr1, "ori", "t,r,i", AT, treg,
7170 (int) BFD_RELOC_LO16);
7171 expr1.X_add_number = 2;
7172 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", AT, AT,
7173 (int) BFD_RELOC_LO16);
2396cfb9 7174 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
f9419b05 7175 AT, RA);
2396cfb9
TS
7176 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7177 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
252b5132 7178 mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S", dreg, sreg);
2396cfb9 7179 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
f9419b05 7180 treg, RA);
2396cfb9 7181 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
252b5132
RH
7182 --mips_opts.noreorder;
7183 break;
7184
7185 case M_ULH:
7186 s = "lb";
7187 goto ulh;
7188 case M_ULHU:
7189 s = "lbu";
7190 ulh:
7191 if (offset_expr.X_add_number >= 0x7fff)
7192 as_bad (_("operand overflow"));
7193 /* avoid load delay */
7194 if (! target_big_endian)
f9419b05 7195 ++offset_expr.X_add_number;
252b5132
RH
7196 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7197 (int) BFD_RELOC_LO16, breg);
7198 if (! target_big_endian)
f9419b05 7199 --offset_expr.X_add_number;
252b5132 7200 else
f9419b05 7201 ++offset_expr.X_add_number;
252b5132
RH
7202 macro_build ((char *) NULL, &icnt, &offset_expr, "lbu", "t,o(b)", AT,
7203 (int) BFD_RELOC_LO16, breg);
2396cfb9
TS
7204 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7205 treg, treg, 8);
7206 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7207 treg, treg, AT);
252b5132
RH
7208 break;
7209
7210 case M_ULD:
7211 s = "ldl";
7212 s2 = "ldr";
7213 off = 7;
7214 goto ulw;
7215 case M_ULW:
7216 s = "lwl";
7217 s2 = "lwr";
7218 off = 3;
7219 ulw:
7220 if (offset_expr.X_add_number >= 0x8000 - off)
7221 as_bad (_("operand overflow"));
7222 if (! target_big_endian)
7223 offset_expr.X_add_number += off;
7224 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7225 (int) BFD_RELOC_LO16, breg);
7226 if (! target_big_endian)
7227 offset_expr.X_add_number -= off;
7228 else
7229 offset_expr.X_add_number += off;
7230 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7231 (int) BFD_RELOC_LO16, breg);
7232 return;
7233
7234 case M_ULD_A:
7235 s = "ldl";
7236 s2 = "ldr";
7237 off = 7;
7238 goto ulwa;
7239 case M_ULW_A:
7240 s = "lwl";
7241 s2 = "lwr";
7242 off = 3;
7243 ulwa:
d6bc6245 7244 used_at = 1;
c9914766 7245 load_address (&icnt, AT, &offset_expr, &used_at);
252b5132
RH
7246 if (breg != 0)
7247 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 7248 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
7249 "d,v,t", AT, AT, breg);
7250 if (! target_big_endian)
7251 expr1.X_add_number = off;
7252 else
7253 expr1.X_add_number = 0;
7254 macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7255 (int) BFD_RELOC_LO16, AT);
7256 if (! target_big_endian)
7257 expr1.X_add_number = 0;
7258 else
7259 expr1.X_add_number = off;
7260 macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7261 (int) BFD_RELOC_LO16, AT);
7262 break;
7263
7264 case M_ULH_A:
7265 case M_ULHU_A:
d6bc6245 7266 used_at = 1;
c9914766 7267 load_address (&icnt, AT, &offset_expr, &used_at);
252b5132
RH
7268 if (breg != 0)
7269 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 7270 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
7271 "d,v,t", AT, AT, breg);
7272 if (target_big_endian)
7273 expr1.X_add_number = 0;
7274 macro_build ((char *) NULL, &icnt, &expr1,
7275 mask == M_ULH_A ? "lb" : "lbu", "t,o(b)", treg,
7276 (int) BFD_RELOC_LO16, AT);
7277 if (target_big_endian)
7278 expr1.X_add_number = 1;
7279 else
7280 expr1.X_add_number = 0;
7281 macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7282 (int) BFD_RELOC_LO16, AT);
2396cfb9
TS
7283 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7284 treg, treg, 8);
7285 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7286 treg, treg, AT);
252b5132
RH
7287 break;
7288
7289 case M_USH:
7290 if (offset_expr.X_add_number >= 0x7fff)
7291 as_bad (_("operand overflow"));
7292 if (target_big_endian)
f9419b05 7293 ++offset_expr.X_add_number;
252b5132
RH
7294 macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", treg,
7295 (int) BFD_RELOC_LO16, breg);
2396cfb9
TS
7296 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7297 AT, treg, 8);
252b5132 7298 if (target_big_endian)
f9419b05 7299 --offset_expr.X_add_number;
252b5132 7300 else
f9419b05 7301 ++offset_expr.X_add_number;
252b5132
RH
7302 macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", AT,
7303 (int) BFD_RELOC_LO16, breg);
7304 break;
7305
7306 case M_USD:
7307 s = "sdl";
7308 s2 = "sdr";
7309 off = 7;
7310 goto usw;
7311 case M_USW:
7312 s = "swl";
7313 s2 = "swr";
7314 off = 3;
7315 usw:
7316 if (offset_expr.X_add_number >= 0x8000 - off)
7317 as_bad (_("operand overflow"));
7318 if (! target_big_endian)
7319 offset_expr.X_add_number += off;
7320 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7321 (int) BFD_RELOC_LO16, breg);
7322 if (! target_big_endian)
7323 offset_expr.X_add_number -= off;
7324 else
7325 offset_expr.X_add_number += off;
7326 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7327 (int) BFD_RELOC_LO16, breg);
7328 return;
7329
7330 case M_USD_A:
7331 s = "sdl";
7332 s2 = "sdr";
7333 off = 7;
7334 goto uswa;
7335 case M_USW_A:
7336 s = "swl";
7337 s2 = "swr";
7338 off = 3;
7339 uswa:
d6bc6245 7340 used_at = 1;
c9914766 7341 load_address (&icnt, AT, &offset_expr, &used_at);
252b5132
RH
7342 if (breg != 0)
7343 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 7344 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
7345 "d,v,t", AT, AT, breg);
7346 if (! target_big_endian)
7347 expr1.X_add_number = off;
7348 else
7349 expr1.X_add_number = 0;
7350 macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7351 (int) BFD_RELOC_LO16, AT);
7352 if (! target_big_endian)
7353 expr1.X_add_number = 0;
7354 else
7355 expr1.X_add_number = off;
7356 macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7357 (int) BFD_RELOC_LO16, AT);
7358 break;
7359
7360 case M_USH_A:
d6bc6245 7361 used_at = 1;
c9914766 7362 load_address (&icnt, AT, &offset_expr, &used_at);
252b5132
RH
7363 if (breg != 0)
7364 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 7365 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
7366 "d,v,t", AT, AT, breg);
7367 if (! target_big_endian)
7368 expr1.X_add_number = 0;
7369 macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7370 (int) BFD_RELOC_LO16, AT);
2396cfb9
TS
7371 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7372 treg, treg, 8);
252b5132
RH
7373 if (! target_big_endian)
7374 expr1.X_add_number = 1;
7375 else
7376 expr1.X_add_number = 0;
7377 macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7378 (int) BFD_RELOC_LO16, AT);
7379 if (! target_big_endian)
7380 expr1.X_add_number = 0;
7381 else
7382 expr1.X_add_number = 1;
7383 macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7384 (int) BFD_RELOC_LO16, AT);
2396cfb9
TS
7385 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7386 treg, treg, 8);
7387 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7388 treg, treg, AT);
252b5132
RH
7389 break;
7390
7391 default:
7392 /* FIXME: Check if this is one of the itbl macros, since they
bdaaa2e1 7393 are added dynamically. */
252b5132
RH
7394 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7395 break;
7396 }
7397 if (mips_opts.noat)
7398 as_warn (_("Macro used $at after \".set noat\""));
7399}
7400
7401/* Implement macros in mips16 mode. */
7402
7403static void
7404mips16_macro (ip)
7405 struct mips_cl_insn *ip;
7406{
7407 int mask;
7408 int xreg, yreg, zreg, tmp;
7409 int icnt;
7410 expressionS expr1;
7411 int dbl;
7412 const char *s, *s2, *s3;
7413
7414 mask = ip->insn_mo->mask;
7415
7416 xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
7417 yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
7418 zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
7419
7420 icnt = 0;
7421
7422 expr1.X_op = O_constant;
7423 expr1.X_op_symbol = NULL;
7424 expr1.X_add_symbol = NULL;
7425 expr1.X_add_number = 1;
7426
7427 dbl = 0;
7428
7429 switch (mask)
7430 {
7431 default:
7432 internalError ();
7433
7434 case M_DDIV_3:
7435 dbl = 1;
7436 case M_DIV_3:
7437 s = "mflo";
7438 goto do_div3;
7439 case M_DREM_3:
7440 dbl = 1;
7441 case M_REM_3:
7442 s = "mfhi";
7443 do_div3:
7444 mips_emit_delays (true);
7445 ++mips_opts.noreorder;
7446 mips_any_noreorder = 1;
2396cfb9 7447 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
252b5132
RH
7448 dbl ? "ddiv" : "div",
7449 "0,x,y", xreg, yreg);
7450 expr1.X_add_number = 2;
7451 macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
2396cfb9
TS
7452 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break", "6",
7453 7);
bdaaa2e1 7454
252b5132
RH
7455 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7456 since that causes an overflow. We should do that as well,
7457 but I don't see how to do the comparisons without a temporary
7458 register. */
7459 --mips_opts.noreorder;
2396cfb9 7460 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x", zreg);
252b5132
RH
7461 break;
7462
7463 case M_DIVU_3:
7464 s = "divu";
7465 s2 = "mflo";
7466 goto do_divu3;
7467 case M_REMU_3:
7468 s = "divu";
7469 s2 = "mfhi";
7470 goto do_divu3;
7471 case M_DDIVU_3:
7472 s = "ddivu";
7473 s2 = "mflo";
7474 goto do_divu3;
7475 case M_DREMU_3:
7476 s = "ddivu";
7477 s2 = "mfhi";
7478 do_divu3:
7479 mips_emit_delays (true);
7480 ++mips_opts.noreorder;
7481 mips_any_noreorder = 1;
2396cfb9
TS
7482 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "0,x,y",
7483 xreg, yreg);
252b5132
RH
7484 expr1.X_add_number = 2;
7485 macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
98d3f06f
KH
7486 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
7487 "6", 7);
252b5132 7488 --mips_opts.noreorder;
2396cfb9 7489 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "x", zreg);
252b5132
RH
7490 break;
7491
7492 case M_DMUL:
7493 dbl = 1;
7494 case M_MUL:
2396cfb9 7495 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 7496 dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
2396cfb9
TS
7497 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "x",
7498 zreg);
252b5132
RH
7499 return;
7500
7501 case M_DSUBU_I:
7502 dbl = 1;
7503 goto do_subu;
7504 case M_SUBU_I:
7505 do_subu:
7506 if (imm_expr.X_op != O_constant)
7507 as_bad (_("Unsupported large constant"));
7508 imm_expr.X_add_number = -imm_expr.X_add_number;
7509 macro_build ((char *) NULL, &icnt, &imm_expr,
9a41af64 7510 dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
252b5132
RH
7511 break;
7512
7513 case M_SUBU_I_2:
7514 if (imm_expr.X_op != O_constant)
7515 as_bad (_("Unsupported large constant"));
7516 imm_expr.X_add_number = -imm_expr.X_add_number;
7517 macro_build ((char *) NULL, &icnt, &imm_expr, "addiu",
7518 "x,k", xreg);
7519 break;
7520
7521 case M_DSUBU_I_2:
7522 if (imm_expr.X_op != O_constant)
7523 as_bad (_("Unsupported large constant"));
7524 imm_expr.X_add_number = -imm_expr.X_add_number;
7525 macro_build ((char *) NULL, &icnt, &imm_expr, "daddiu",
7526 "y,j", yreg);
7527 break;
7528
7529 case M_BEQ:
7530 s = "cmp";
7531 s2 = "bteqz";
7532 goto do_branch;
7533 case M_BNE:
7534 s = "cmp";
7535 s2 = "btnez";
7536 goto do_branch;
7537 case M_BLT:
7538 s = "slt";
7539 s2 = "btnez";
7540 goto do_branch;
7541 case M_BLTU:
7542 s = "sltu";
7543 s2 = "btnez";
7544 goto do_branch;
7545 case M_BLE:
7546 s = "slt";
7547 s2 = "bteqz";
7548 goto do_reverse_branch;
7549 case M_BLEU:
7550 s = "sltu";
7551 s2 = "bteqz";
7552 goto do_reverse_branch;
7553 case M_BGE:
7554 s = "slt";
7555 s2 = "bteqz";
7556 goto do_branch;
7557 case M_BGEU:
7558 s = "sltu";
7559 s2 = "bteqz";
7560 goto do_branch;
7561 case M_BGT:
7562 s = "slt";
7563 s2 = "btnez";
7564 goto do_reverse_branch;
7565 case M_BGTU:
7566 s = "sltu";
7567 s2 = "btnez";
7568
7569 do_reverse_branch:
7570 tmp = xreg;
7571 xreg = yreg;
7572 yreg = tmp;
7573
7574 do_branch:
7575 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x,y",
7576 xreg, yreg);
7577 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7578 break;
7579
7580 case M_BEQ_I:
7581 s = "cmpi";
7582 s2 = "bteqz";
7583 s3 = "x,U";
7584 goto do_branch_i;
7585 case M_BNE_I:
7586 s = "cmpi";
7587 s2 = "btnez";
7588 s3 = "x,U";
7589 goto do_branch_i;
7590 case M_BLT_I:
7591 s = "slti";
7592 s2 = "btnez";
7593 s3 = "x,8";
7594 goto do_branch_i;
7595 case M_BLTU_I:
7596 s = "sltiu";
7597 s2 = "btnez";
7598 s3 = "x,8";
7599 goto do_branch_i;
7600 case M_BLE_I:
7601 s = "slti";
7602 s2 = "btnez";
7603 s3 = "x,8";
7604 goto do_addone_branch_i;
7605 case M_BLEU_I:
7606 s = "sltiu";
7607 s2 = "btnez";
7608 s3 = "x,8";
7609 goto do_addone_branch_i;
7610 case M_BGE_I:
7611 s = "slti";
7612 s2 = "bteqz";
7613 s3 = "x,8";
7614 goto do_branch_i;
7615 case M_BGEU_I:
7616 s = "sltiu";
7617 s2 = "bteqz";
7618 s3 = "x,8";
7619 goto do_branch_i;
7620 case M_BGT_I:
7621 s = "slti";
7622 s2 = "bteqz";
7623 s3 = "x,8";
7624 goto do_addone_branch_i;
7625 case M_BGTU_I:
7626 s = "sltiu";
7627 s2 = "bteqz";
7628 s3 = "x,8";
7629
7630 do_addone_branch_i:
7631 if (imm_expr.X_op != O_constant)
7632 as_bad (_("Unsupported large constant"));
7633 ++imm_expr.X_add_number;
7634
7635 do_branch_i:
7636 macro_build ((char *) NULL, &icnt, &imm_expr, s, s3, xreg);
7637 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7638 break;
7639
7640 case M_ABS:
7641 expr1.X_add_number = 0;
98d3f06f 7642 macro_build ((char *) NULL, &icnt, &expr1, "slti", "x,8", yreg);
252b5132 7643 if (xreg != yreg)
ea1fb5dc 7644 move_register (&icnt, xreg, yreg);
252b5132
RH
7645 expr1.X_add_number = 2;
7646 macro_build ((char *) NULL, &icnt, &expr1, "bteqz", "p");
7647 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7648 "neg", "x,w", xreg, xreg);
7649 }
7650}
7651
7652/* For consistency checking, verify that all bits are specified either
7653 by the match/mask part of the instruction definition, or by the
7654 operand list. */
7655static int
7656validate_mips_insn (opc)
7657 const struct mips_opcode *opc;
7658{
7659 const char *p = opc->args;
7660 char c;
7661 unsigned long used_bits = opc->mask;
7662
7663 if ((used_bits & opc->match) != opc->match)
7664 {
7665 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
7666 opc->name, opc->args);
7667 return 0;
7668 }
7669#define USE_BITS(mask,shift) (used_bits |= ((mask) << (shift)))
7670 while (*p)
7671 switch (c = *p++)
7672 {
7673 case ',': break;
7674 case '(': break;
7675 case ')': break;
7676 case '<': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7677 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7678 case 'A': break;
4372b673 7679 case 'B': USE_BITS (OP_MASK_CODE20, OP_SH_CODE20); break;
252b5132
RH
7680 case 'C': USE_BITS (OP_MASK_COPZ, OP_SH_COPZ); break;
7681 case 'D': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
7682 case 'E': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7683 case 'F': break;
7684 case 'G': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
156c2f8b 7685 case 'H': USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
252b5132 7686 case 'I': break;
e972090a 7687 case 'J': USE_BITS (OP_MASK_CODE19, OP_SH_CODE19); break;
252b5132
RH
7688 case 'L': break;
7689 case 'M': USE_BITS (OP_MASK_CCC, OP_SH_CCC); break;
7690 case 'N': USE_BITS (OP_MASK_BCC, OP_SH_BCC); break;
7691 case 'R': USE_BITS (OP_MASK_FR, OP_SH_FR); break;
7692 case 'S': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7693 case 'T': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7694 case 'V': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7695 case 'W': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7696 case 'a': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break;
7697 case 'b': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7698 case 'c': USE_BITS (OP_MASK_CODE, OP_SH_CODE); break;
7699 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
7700 case 'f': break;
7701 case 'h': USE_BITS (OP_MASK_PREFX, OP_SH_PREFX); break;
7702 case 'i': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
7703 case 'j': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7704 case 'k': USE_BITS (OP_MASK_CACHE, OP_SH_CACHE); break;
7705 case 'l': break;
7706 case 'o': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7707 case 'p': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7708 case 'q': USE_BITS (OP_MASK_CODE2, OP_SH_CODE2); break;
7709 case 'r': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7710 case 's': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7711 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7712 case 'u': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
7713 case 'v': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7714 case 'w': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7715 case 'x': break;
7716 case 'z': break;
7717 case 'P': USE_BITS (OP_MASK_PERFREG, OP_SH_PERFREG); break;
4372b673
NC
7718 case 'U': USE_BITS (OP_MASK_RD, OP_SH_RD);
7719 USE_BITS (OP_MASK_RT, OP_SH_RT); break;
252b5132
RH
7720 default:
7721 as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
7722 c, opc->name, opc->args);
7723 return 0;
7724 }
7725#undef USE_BITS
7726 if (used_bits != 0xffffffff)
7727 {
7728 as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
7729 ~used_bits & 0xffffffff, opc->name, opc->args);
7730 return 0;
7731 }
7732 return 1;
7733}
7734
7735/* This routine assembles an instruction into its binary format. As a
7736 side effect, it sets one of the global variables imm_reloc or
7737 offset_reloc to the type of relocation to do if one of the operands
7738 is an address expression. */
7739
7740static void
7741mips_ip (str, ip)
7742 char *str;
7743 struct mips_cl_insn *ip;
7744{
7745 char *s;
7746 const char *args;
43841e91 7747 char c = 0;
252b5132
RH
7748 struct mips_opcode *insn;
7749 char *argsStart;
7750 unsigned int regno;
7751 unsigned int lastregno = 0;
7752 char *s_reset;
7753 char save_c = 0;
252b5132
RH
7754
7755 insn_error = NULL;
7756
7757 /* If the instruction contains a '.', we first try to match an instruction
7758 including the '.'. Then we try again without the '.'. */
7759 insn = NULL;
3882b010 7760 for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
252b5132
RH
7761 continue;
7762
7763 /* If we stopped on whitespace, then replace the whitespace with null for
7764 the call to hash_find. Save the character we replaced just in case we
7765 have to re-parse the instruction. */
3882b010 7766 if (ISSPACE (*s))
252b5132
RH
7767 {
7768 save_c = *s;
7769 *s++ = '\0';
7770 }
bdaaa2e1 7771
252b5132
RH
7772 insn = (struct mips_opcode *) hash_find (op_hash, str);
7773
7774 /* If we didn't find the instruction in the opcode table, try again, but
7775 this time with just the instruction up to, but not including the
7776 first '.'. */
7777 if (insn == NULL)
7778 {
bdaaa2e1 7779 /* Restore the character we overwrite above (if any). */
252b5132
RH
7780 if (save_c)
7781 *(--s) = save_c;
7782
7783 /* Scan up to the first '.' or whitespace. */
3882b010
L
7784 for (s = str;
7785 *s != '\0' && *s != '.' && !ISSPACE (*s);
7786 ++s)
252b5132
RH
7787 continue;
7788
7789 /* If we did not find a '.', then we can quit now. */
7790 if (*s != '.')
7791 {
7792 insn_error = "unrecognized opcode";
7793 return;
7794 }
7795
7796 /* Lookup the instruction in the hash table. */
7797 *s++ = '\0';
7798 if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
7799 {
7800 insn_error = "unrecognized opcode";
7801 return;
7802 }
252b5132
RH
7803 }
7804
7805 argsStart = s;
7806 for (;;)
7807 {
252b5132
RH
7808 boolean ok;
7809
7810 assert (strcmp (insn->name, str) == 0);
7811
1f25f5d3
CD
7812 if (OPCODE_IS_MEMBER (insn,
7813 (mips_opts.isa
98d3f06f 7814 | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
1f25f5d3 7815 mips_arch))
252b5132 7816 ok = true;
bdaaa2e1 7817 else
252b5132 7818 ok = false;
bdaaa2e1 7819
252b5132
RH
7820 if (insn->pinfo != INSN_MACRO)
7821 {
ec68c924 7822 if (mips_arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
252b5132
RH
7823 ok = false;
7824 }
7825
7826 if (! ok)
7827 {
7828 if (insn + 1 < &mips_opcodes[NUMOPCODES]
7829 && strcmp (insn->name, insn[1].name) == 0)
7830 {
7831 ++insn;
7832 continue;
7833 }
252b5132 7834 else
beae10d5 7835 {
268f6bed
L
7836 if (!insn_error)
7837 {
7838 static char buf[100];
7839 sprintf (buf,
7840 _("opcode not supported on this processor: %s (%s)"),
ec68c924 7841 mips_cpu_to_str (mips_arch),
268f6bed 7842 mips_isa_to_str (mips_opts.isa));
bdaaa2e1 7843
268f6bed
L
7844 insn_error = buf;
7845 }
7846 if (save_c)
7847 *(--s) = save_c;
2bd7f1f3 7848 return;
252b5132 7849 }
252b5132
RH
7850 }
7851
7852 ip->insn_mo = insn;
7853 ip->insn_opcode = insn->match;
268f6bed 7854 insn_error = NULL;
252b5132
RH
7855 for (args = insn->args;; ++args)
7856 {
ad8d3bb3 7857 s += strspn (s, " \t");
252b5132
RH
7858 switch (*args)
7859 {
7860 case '\0': /* end of args */
7861 if (*s == '\0')
7862 return;
7863 break;
7864
7865 case ',':
7866 if (*s++ == *args)
7867 continue;
7868 s--;
7869 switch (*++args)
7870 {
7871 case 'r':
7872 case 'v':
38487616 7873 ip->insn_opcode |= lastregno << OP_SH_RS;
252b5132
RH
7874 continue;
7875
7876 case 'w':
38487616
TS
7877 ip->insn_opcode |= lastregno << OP_SH_RT;
7878 continue;
7879
252b5132 7880 case 'W':
38487616 7881 ip->insn_opcode |= lastregno << OP_SH_FT;
252b5132
RH
7882 continue;
7883
7884 case 'V':
38487616 7885 ip->insn_opcode |= lastregno << OP_SH_FS;
252b5132
RH
7886 continue;
7887 }
7888 break;
7889
7890 case '(':
7891 /* Handle optional base register.
7892 Either the base register is omitted or
bdaaa2e1 7893 we must have a left paren. */
252b5132
RH
7894 /* This is dependent on the next operand specifier
7895 is a base register specification. */
7896 assert (args[1] == 'b' || args[1] == '5'
7897 || args[1] == '-' || args[1] == '4');
7898 if (*s == '\0')
7899 return;
7900
7901 case ')': /* these must match exactly */
7902 if (*s++ == *args)
7903 continue;
7904 break;
7905
7906 case '<': /* must be at least one digit */
7907 /*
7908 * According to the manual, if the shift amount is greater
b6ff326e
KH
7909 * than 31 or less than 0, then the shift amount should be
7910 * mod 32. In reality the mips assembler issues an error.
252b5132
RH
7911 * We issue a warning and mask out all but the low 5 bits.
7912 */
7913 my_getExpression (&imm_expr, s);
7914 check_absolute_expr (ip, &imm_expr);
7915 if ((unsigned long) imm_expr.X_add_number > 31)
7916 {
7917 as_warn (_("Improper shift amount (%ld)"),
7918 (long) imm_expr.X_add_number);
38487616 7919 imm_expr.X_add_number &= OP_MASK_SHAMT;
252b5132 7920 }
38487616 7921 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SHAMT;
252b5132
RH
7922 imm_expr.X_op = O_absent;
7923 s = expr_end;
7924 continue;
7925
7926 case '>': /* shift amount minus 32 */
7927 my_getExpression (&imm_expr, s);
7928 check_absolute_expr (ip, &imm_expr);
7929 if ((unsigned long) imm_expr.X_add_number < 32
7930 || (unsigned long) imm_expr.X_add_number > 63)
7931 break;
38487616 7932 ip->insn_opcode |= (imm_expr.X_add_number - 32) << OP_SH_SHAMT;
252b5132
RH
7933 imm_expr.X_op = O_absent;
7934 s = expr_end;
7935 continue;
7936
252b5132
RH
7937 case 'k': /* cache code */
7938 case 'h': /* prefx code */
7939 my_getExpression (&imm_expr, s);
7940 check_absolute_expr (ip, &imm_expr);
7941 if ((unsigned long) imm_expr.X_add_number > 31)
7942 {
7943 as_warn (_("Invalid value for `%s' (%lu)"),
7944 ip->insn_mo->name,
7945 (unsigned long) imm_expr.X_add_number);
7946 imm_expr.X_add_number &= 0x1f;
7947 }
7948 if (*args == 'k')
7949 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
7950 else
7951 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
7952 imm_expr.X_op = O_absent;
7953 s = expr_end;
7954 continue;
7955
7956 case 'c': /* break code */
7957 my_getExpression (&imm_expr, s);
7958 check_absolute_expr (ip, &imm_expr);
7959 if ((unsigned) imm_expr.X_add_number > 1023)
7960 {
7961 as_warn (_("Illegal break code (%ld)"),
7962 (long) imm_expr.X_add_number);
38487616 7963 imm_expr.X_add_number &= OP_MASK_CODE;
252b5132 7964 }
38487616 7965 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE;
252b5132
RH
7966 imm_expr.X_op = O_absent;
7967 s = expr_end;
7968 continue;
7969
7970 case 'q': /* lower break code */
7971 my_getExpression (&imm_expr, s);
7972 check_absolute_expr (ip, &imm_expr);
7973 if ((unsigned) imm_expr.X_add_number > 1023)
7974 {
7975 as_warn (_("Illegal lower break code (%ld)"),
7976 (long) imm_expr.X_add_number);
38487616 7977 imm_expr.X_add_number &= OP_MASK_CODE2;
252b5132 7978 }
38487616 7979 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE2;
252b5132
RH
7980 imm_expr.X_op = O_absent;
7981 s = expr_end;
7982 continue;
7983
4372b673 7984 case 'B': /* 20-bit syscall/break code. */
156c2f8b 7985 my_getExpression (&imm_expr, s);
156c2f8b 7986 check_absolute_expr (ip, &imm_expr);
38487616 7987 if ((unsigned) imm_expr.X_add_number > OP_MASK_CODE20)
4372b673 7988 as_warn (_("Illegal 20-bit code (%ld)"),
252b5132 7989 (long) imm_expr.X_add_number);
38487616 7990 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE20;
252b5132
RH
7991 imm_expr.X_op = O_absent;
7992 s = expr_end;
7993 continue;
7994
98d3f06f 7995 case 'C': /* Coprocessor code */
beae10d5 7996 my_getExpression (&imm_expr, s);
252b5132 7997 check_absolute_expr (ip, &imm_expr);
98d3f06f 7998 if ((unsigned long) imm_expr.X_add_number >= (1 << 25))
252b5132 7999 {
beae10d5 8000 as_warn (_("Coproccesor code > 25 bits (%ld)"),
252b5132 8001 (long) imm_expr.X_add_number);
98d3f06f 8002 imm_expr.X_add_number &= ((1 << 25) - 1);
252b5132 8003 }
beae10d5
KH
8004 ip->insn_opcode |= imm_expr.X_add_number;
8005 imm_expr.X_op = O_absent;
8006 s = expr_end;
8007 continue;
252b5132 8008
4372b673
NC
8009 case 'J': /* 19-bit wait code. */
8010 my_getExpression (&imm_expr, s);
8011 check_absolute_expr (ip, &imm_expr);
38487616 8012 if ((unsigned) imm_expr.X_add_number > OP_MASK_CODE19)
4372b673
NC
8013 as_warn (_("Illegal 19-bit code (%ld)"),
8014 (long) imm_expr.X_add_number);
38487616 8015 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE19;
4372b673
NC
8016 imm_expr.X_op = O_absent;
8017 s = expr_end;
8018 continue;
8019
252b5132 8020 case 'P': /* Performance register */
beae10d5 8021 my_getExpression (&imm_expr, s);
252b5132 8022 check_absolute_expr (ip, &imm_expr);
beae10d5 8023 if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
252b5132 8024 {
38487616 8025 as_warn (_("Invalid performance register (%ld)"),
252b5132 8026 (long) imm_expr.X_add_number);
38487616 8027 imm_expr.X_add_number &= OP_MASK_PERFREG;
252b5132 8028 }
38487616 8029 ip->insn_opcode |= (imm_expr.X_add_number << OP_SH_PERFREG);
beae10d5
KH
8030 imm_expr.X_op = O_absent;
8031 s = expr_end;
8032 continue;
252b5132
RH
8033
8034 case 'b': /* base register */
8035 case 'd': /* destination register */
8036 case 's': /* source register */
8037 case 't': /* target register */
8038 case 'r': /* both target and source */
8039 case 'v': /* both dest and source */
8040 case 'w': /* both dest and target */
8041 case 'E': /* coprocessor target register */
8042 case 'G': /* coprocessor destination register */
8043 case 'x': /* ignore register name */
8044 case 'z': /* must be zero register */
4372b673 8045 case 'U': /* destination register (clo/clz). */
252b5132
RH
8046 s_reset = s;
8047 if (s[0] == '$')
8048 {
8049
3882b010 8050 if (ISDIGIT (s[1]))
252b5132
RH
8051 {
8052 ++s;
8053 regno = 0;
8054 do
8055 {
8056 regno *= 10;
8057 regno += *s - '0';
8058 ++s;
8059 }
3882b010 8060 while (ISDIGIT (*s));
252b5132
RH
8061 if (regno > 31)
8062 as_bad (_("Invalid register number (%d)"), regno);
8063 }
8064 else if (*args == 'E' || *args == 'G')
8065 goto notreg;
8066 else
8067 {
8068 if (s[1] == 'f' && s[2] == 'p')
8069 {
8070 s += 3;
8071 regno = FP;
8072 }
8073 else if (s[1] == 's' && s[2] == 'p')
8074 {
8075 s += 3;
8076 regno = SP;
8077 }
8078 else if (s[1] == 'g' && s[2] == 'p')
8079 {
8080 s += 3;
8081 regno = GP;
8082 }
8083 else if (s[1] == 'a' && s[2] == 't')
8084 {
8085 s += 3;
8086 regno = AT;
8087 }
8088 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8089 {
8090 s += 4;
8091 regno = KT0;
8092 }
8093 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8094 {
8095 s += 4;
8096 regno = KT1;
8097 }
8098 else if (itbl_have_entries)
8099 {
8100 char *p, *n;
d7ba4a77 8101 unsigned long r;
252b5132 8102
d7ba4a77 8103 p = s + 1; /* advance past '$' */
252b5132
RH
8104 n = itbl_get_field (&p); /* n is name */
8105
d7ba4a77
ILT
8106 /* See if this is a register defined in an
8107 itbl entry. */
8108 if (itbl_get_reg_val (n, &r))
252b5132
RH
8109 {
8110 /* Get_field advances to the start of
8111 the next field, so we need to back
d7ba4a77 8112 rack to the end of the last field. */
bdaaa2e1 8113 if (p)
252b5132 8114 s = p - 1;
bdaaa2e1 8115 else
d7ba4a77 8116 s = strchr (s, '\0');
252b5132
RH
8117 regno = r;
8118 }
8119 else
8120 goto notreg;
beae10d5 8121 }
252b5132
RH
8122 else
8123 goto notreg;
8124 }
8125 if (regno == AT
8126 && ! mips_opts.noat
8127 && *args != 'E'
8128 && *args != 'G')
8129 as_warn (_("Used $at without \".set noat\""));
8130 c = *args;
8131 if (*s == ' ')
f9419b05 8132 ++s;
252b5132
RH
8133 if (args[1] != *s)
8134 {
8135 if (c == 'r' || c == 'v' || c == 'w')
8136 {
8137 regno = lastregno;
8138 s = s_reset;
f9419b05 8139 ++args;
252b5132
RH
8140 }
8141 }
8142 /* 'z' only matches $0. */
8143 if (c == 'z' && regno != 0)
8144 break;
8145
bdaaa2e1
KH
8146 /* Now that we have assembled one operand, we use the args string
8147 * to figure out where it goes in the instruction. */
252b5132
RH
8148 switch (c)
8149 {
8150 case 'r':
8151 case 's':
8152 case 'v':
8153 case 'b':
38487616 8154 ip->insn_opcode |= regno << OP_SH_RS;
252b5132
RH
8155 break;
8156 case 'd':
8157 case 'G':
38487616 8158 ip->insn_opcode |= regno << OP_SH_RD;
252b5132 8159 break;
4372b673 8160 case 'U':
38487616
TS
8161 ip->insn_opcode |= regno << OP_SH_RD;
8162 ip->insn_opcode |= regno << OP_SH_RT;
4372b673 8163 break;
252b5132
RH
8164 case 'w':
8165 case 't':
8166 case 'E':
38487616 8167 ip->insn_opcode |= regno << OP_SH_RT;
252b5132
RH
8168 break;
8169 case 'x':
8170 /* This case exists because on the r3000 trunc
8171 expands into a macro which requires a gp
8172 register. On the r6000 or r4000 it is
8173 assembled into a single instruction which
8174 ignores the register. Thus the insn version
8175 is MIPS_ISA2 and uses 'x', and the macro
8176 version is MIPS_ISA1 and uses 't'. */
8177 break;
8178 case 'z':
8179 /* This case is for the div instruction, which
8180 acts differently if the destination argument
8181 is $0. This only matches $0, and is checked
8182 outside the switch. */
8183 break;
8184 case 'D':
8185 /* Itbl operand; not yet implemented. FIXME ?? */
8186 break;
8187 /* What about all other operands like 'i', which
8188 can be specified in the opcode table? */
8189 }
8190 lastregno = regno;
8191 continue;
8192 }
8193 notreg:
8194 switch (*args++)
8195 {
8196 case 'r':
8197 case 'v':
38487616 8198 ip->insn_opcode |= lastregno << OP_SH_RS;
252b5132
RH
8199 continue;
8200 case 'w':
38487616 8201 ip->insn_opcode |= lastregno << OP_SH_RT;
252b5132
RH
8202 continue;
8203 }
8204 break;
8205
8206 case 'D': /* floating point destination register */
8207 case 'S': /* floating point source register */
8208 case 'T': /* floating point target register */
8209 case 'R': /* floating point source register */
8210 case 'V':
8211 case 'W':
8212 s_reset = s;
3882b010
L
8213 if (s[0] == '$' && s[1] == 'f'
8214 && ISDIGIT (s[2]))
252b5132
RH
8215 {
8216 s += 2;
8217 regno = 0;
8218 do
8219 {
8220 regno *= 10;
8221 regno += *s - '0';
8222 ++s;
8223 }
3882b010 8224 while (ISDIGIT (*s));
252b5132
RH
8225
8226 if (regno > 31)
8227 as_bad (_("Invalid float register number (%d)"), regno);
8228
8229 if ((regno & 1) != 0
ca4e0257 8230 && HAVE_32BIT_FPRS
252b5132
RH
8231 && ! (strcmp (str, "mtc1") == 0
8232 || strcmp (str, "mfc1") == 0
8233 || strcmp (str, "lwc1") == 0
8234 || strcmp (str, "swc1") == 0
8235 || strcmp (str, "l.s") == 0
8236 || strcmp (str, "s.s") == 0))
8237 as_warn (_("Float register should be even, was %d"),
8238 regno);
8239
8240 c = *args;
8241 if (*s == ' ')
f9419b05 8242 ++s;
252b5132
RH
8243 if (args[1] != *s)
8244 {
8245 if (c == 'V' || c == 'W')
8246 {
8247 regno = lastregno;
8248 s = s_reset;
f9419b05 8249 ++args;
252b5132
RH
8250 }
8251 }
8252 switch (c)
8253 {
8254 case 'D':
38487616 8255 ip->insn_opcode |= regno << OP_SH_FD;
252b5132
RH
8256 break;
8257 case 'V':
8258 case 'S':
38487616 8259 ip->insn_opcode |= regno << OP_SH_FS;
252b5132
RH
8260 break;
8261 case 'W':
8262 case 'T':
38487616 8263 ip->insn_opcode |= regno << OP_SH_FT;
252b5132
RH
8264 break;
8265 case 'R':
38487616 8266 ip->insn_opcode |= regno << OP_SH_FR;
252b5132
RH
8267 break;
8268 }
8269 lastregno = regno;
8270 continue;
8271 }
8272
252b5132
RH
8273 switch (*args++)
8274 {
8275 case 'V':
38487616 8276 ip->insn_opcode |= lastregno << OP_SH_FS;
252b5132
RH
8277 continue;
8278 case 'W':
38487616 8279 ip->insn_opcode |= lastregno << OP_SH_FT;
252b5132
RH
8280 continue;
8281 }
8282 break;
8283
8284 case 'I':
8285 my_getExpression (&imm_expr, s);
8286 if (imm_expr.X_op != O_big
8287 && imm_expr.X_op != O_constant)
8288 insn_error = _("absolute expression required");
8289 s = expr_end;
8290 continue;
8291
8292 case 'A':
8293 my_getExpression (&offset_expr, s);
f6688943 8294 *imm_reloc = BFD_RELOC_32;
252b5132
RH
8295 s = expr_end;
8296 continue;
8297
8298 case 'F':
8299 case 'L':
8300 case 'f':
8301 case 'l':
8302 {
8303 int f64;
ca4e0257 8304 int using_gprs;
252b5132
RH
8305 char *save_in;
8306 char *err;
8307 unsigned char temp[8];
8308 int len;
8309 unsigned int length;
8310 segT seg;
8311 subsegT subseg;
8312 char *p;
8313
8314 /* These only appear as the last operand in an
8315 instruction, and every instruction that accepts
8316 them in any variant accepts them in all variants.
8317 This means we don't have to worry about backing out
8318 any changes if the instruction does not match.
8319
8320 The difference between them is the size of the
8321 floating point constant and where it goes. For 'F'
8322 and 'L' the constant is 64 bits; for 'f' and 'l' it
8323 is 32 bits. Where the constant is placed is based
8324 on how the MIPS assembler does things:
8325 F -- .rdata
8326 L -- .lit8
8327 f -- immediate value
8328 l -- .lit4
8329
8330 The .lit4 and .lit8 sections are only used if
8331 permitted by the -G argument.
8332
8333 When generating embedded PIC code, we use the
8334 .lit8 section but not the .lit4 section (we can do
8335 .lit4 inline easily; we need to put .lit8
8336 somewhere in the data segment, and using .lit8
8337 permits the linker to eventually combine identical
ca4e0257
RS
8338 .lit8 entries).
8339
8340 The code below needs to know whether the target register
8341 is 32 or 64 bits wide. It relies on the fact 'f' and
8342 'F' are used with GPR-based instructions and 'l' and
8343 'L' are used with FPR-based instructions. */
252b5132
RH
8344
8345 f64 = *args == 'F' || *args == 'L';
ca4e0257 8346 using_gprs = *args == 'F' || *args == 'f';
252b5132
RH
8347
8348 save_in = input_line_pointer;
8349 input_line_pointer = s;
8350 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
8351 length = len;
8352 s = input_line_pointer;
8353 input_line_pointer = save_in;
8354 if (err != NULL && *err != '\0')
8355 {
8356 as_bad (_("Bad floating point constant: %s"), err);
8357 memset (temp, '\0', sizeof temp);
8358 length = f64 ? 8 : 4;
8359 }
8360
156c2f8b 8361 assert (length == (unsigned) (f64 ? 8 : 4));
252b5132
RH
8362
8363 if (*args == 'f'
8364 || (*args == 'l'
8365 && (! USE_GLOBAL_POINTER_OPT
8366 || mips_pic == EMBEDDED_PIC
8367 || g_switch_value < 4
8368 || (temp[0] == 0 && temp[1] == 0)
8369 || (temp[2] == 0 && temp[3] == 0))))
8370 {
8371 imm_expr.X_op = O_constant;
8372 if (! target_big_endian)
8373 imm_expr.X_add_number = bfd_getl32 (temp);
8374 else
8375 imm_expr.X_add_number = bfd_getb32 (temp);
8376 }
8377 else if (length > 4
119d663a 8378 && ! mips_disable_float_construction
ca4e0257
RS
8379 /* Constants can only be constructed in GPRs and
8380 copied to FPRs if the GPRs are at least as wide
8381 as the FPRs. Force the constant into memory if
8382 we are using 64-bit FPRs but the GPRs are only
8383 32 bits wide. */
8384 && (using_gprs
8385 || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
252b5132
RH
8386 && ((temp[0] == 0 && temp[1] == 0)
8387 || (temp[2] == 0 && temp[3] == 0))
8388 && ((temp[4] == 0 && temp[5] == 0)
8389 || (temp[6] == 0 && temp[7] == 0)))
8390 {
ca4e0257
RS
8391 /* The value is simple enough to load with a couple of
8392 instructions. If using 32-bit registers, set
8393 imm_expr to the high order 32 bits and offset_expr to
8394 the low order 32 bits. Otherwise, set imm_expr to
8395 the entire 64 bit constant. */
8396 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
252b5132
RH
8397 {
8398 imm_expr.X_op = O_constant;
8399 offset_expr.X_op = O_constant;
8400 if (! target_big_endian)
8401 {
8402 imm_expr.X_add_number = bfd_getl32 (temp + 4);
8403 offset_expr.X_add_number = bfd_getl32 (temp);
8404 }
8405 else
8406 {
8407 imm_expr.X_add_number = bfd_getb32 (temp);
8408 offset_expr.X_add_number = bfd_getb32 (temp + 4);
8409 }
8410 if (offset_expr.X_add_number == 0)
8411 offset_expr.X_op = O_absent;
8412 }
8413 else if (sizeof (imm_expr.X_add_number) > 4)
8414 {
8415 imm_expr.X_op = O_constant;
8416 if (! target_big_endian)
8417 imm_expr.X_add_number = bfd_getl64 (temp);
8418 else
8419 imm_expr.X_add_number = bfd_getb64 (temp);
8420 }
8421 else
8422 {
8423 imm_expr.X_op = O_big;
8424 imm_expr.X_add_number = 4;
8425 if (! target_big_endian)
8426 {
8427 generic_bignum[0] = bfd_getl16 (temp);
8428 generic_bignum[1] = bfd_getl16 (temp + 2);
8429 generic_bignum[2] = bfd_getl16 (temp + 4);
8430 generic_bignum[3] = bfd_getl16 (temp + 6);
8431 }
8432 else
8433 {
8434 generic_bignum[0] = bfd_getb16 (temp + 6);
8435 generic_bignum[1] = bfd_getb16 (temp + 4);
8436 generic_bignum[2] = bfd_getb16 (temp + 2);
8437 generic_bignum[3] = bfd_getb16 (temp);
8438 }
8439 }
8440 }
8441 else
8442 {
8443 const char *newname;
8444 segT new_seg;
8445
8446 /* Switch to the right section. */
8447 seg = now_seg;
8448 subseg = now_subseg;
8449 switch (*args)
8450 {
8451 default: /* unused default case avoids warnings. */
8452 case 'L':
8453 newname = RDATA_SECTION_NAME;
bb2d6cd7
GK
8454 if ((USE_GLOBAL_POINTER_OPT && g_switch_value >= 8)
8455 || mips_pic == EMBEDDED_PIC)
252b5132
RH
8456 newname = ".lit8";
8457 break;
8458 case 'F':
bb2d6cd7
GK
8459 if (mips_pic == EMBEDDED_PIC)
8460 newname = ".lit8";
8461 else
8462 newname = RDATA_SECTION_NAME;
252b5132
RH
8463 break;
8464 case 'l':
8465 assert (!USE_GLOBAL_POINTER_OPT
8466 || g_switch_value >= 4);
8467 newname = ".lit4";
8468 break;
8469 }
8470 new_seg = subseg_new (newname, (subsegT) 0);
8471 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
8472 bfd_set_section_flags (stdoutput, new_seg,
8473 (SEC_ALLOC
8474 | SEC_LOAD
8475 | SEC_READONLY
8476 | SEC_DATA));
8477 frag_align (*args == 'l' ? 2 : 3, 0, 0);
8478 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
8479 && strcmp (TARGET_OS, "elf") != 0)
8480 record_alignment (new_seg, 4);
8481 else
8482 record_alignment (new_seg, *args == 'l' ? 2 : 3);
8483 if (seg == now_seg)
8484 as_bad (_("Can't use floating point insn in this section"));
8485
8486 /* Set the argument to the current address in the
8487 section. */
8488 offset_expr.X_op = O_symbol;
8489 offset_expr.X_add_symbol =
8490 symbol_new ("L0\001", now_seg,
8491 (valueT) frag_now_fix (), frag_now);
8492 offset_expr.X_add_number = 0;
8493
8494 /* Put the floating point number into the section. */
8495 p = frag_more ((int) length);
8496 memcpy (p, temp, length);
8497
8498 /* Switch back to the original section. */
8499 subseg_set (seg, subseg);
8500 }
8501 }
8502 continue;
8503
8504 case 'i': /* 16 bit unsigned immediate */
8505 case 'j': /* 16 bit signed immediate */
f6688943 8506 *imm_reloc = BFD_RELOC_LO16;
252b5132 8507 c = my_getSmallExpression (&imm_expr, s);
fb1b3232 8508 if (c != S_EX_NONE)
252b5132 8509 {
fb1b3232 8510 if (c != S_EX_LO)
252b5132
RH
8511 {
8512 if (imm_expr.X_op == O_constant)
8513 imm_expr.X_add_number =
8514 (imm_expr.X_add_number >> 16) & 0xffff;
ad8d3bb3 8515#ifdef OBJ_ELF
fb1b3232 8516 else if (c == S_EX_HIGHEST)
98d3f06f 8517 *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
fb1b3232 8518 else if (c == S_EX_HIGHER)
98d3f06f 8519 *imm_reloc = BFD_RELOC_MIPS_HIGHER;
ad8d3bb3
TS
8520 else if (c == S_EX_GP_REL)
8521 {
8522 /* This occurs in NewABI only. */
8523 c = my_getSmallExpression (&imm_expr, s);
8524 if (c != S_EX_NEG)
8525 as_bad (_("bad composition of relocations"));
8526 else
8527 {
8528 c = my_getSmallExpression (&imm_expr, s);
8529 if (c != S_EX_LO)
8530 as_bad (_("bad composition of relocations"));
8531 else
8532 {
8533 imm_reloc[0] = BFD_RELOC_GPREL16;
8534 imm_reloc[1] = BFD_RELOC_MIPS_SUB;
8535 imm_reloc[2] = BFD_RELOC_LO16;
8536 }
8537 }
8538 }
8539#endif
fb1b3232 8540 else if (c == S_EX_HI)
252b5132 8541 {
f6688943 8542 *imm_reloc = BFD_RELOC_HI16_S;
252b5132
RH
8543 imm_unmatched_hi = true;
8544 }
8545 else
f6688943 8546 *imm_reloc = BFD_RELOC_HI16;
252b5132
RH
8547 }
8548 else if (imm_expr.X_op == O_constant)
8549 imm_expr.X_add_number &= 0xffff;
8550 }
8551 if (*args == 'i')
8552 {
fb1b3232 8553 if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
252b5132 8554 || ((imm_expr.X_add_number < 0
beae10d5
KH
8555 || imm_expr.X_add_number >= 0x10000)
8556 && imm_expr.X_op == O_constant))
252b5132
RH
8557 {
8558 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8559 !strcmp (insn->name, insn[1].name))
8560 break;
2ae7e77b
AH
8561 if (imm_expr.X_op == O_constant
8562 || imm_expr.X_op == O_big)
252b5132
RH
8563 as_bad (_("16 bit expression not in range 0..65535"));
8564 }
8565 }
8566 else
8567 {
8568 int more;
8569 offsetT max;
8570
8571 /* The upper bound should be 0x8000, but
8572 unfortunately the MIPS assembler accepts numbers
8573 from 0x8000 to 0xffff and sign extends them, and
8574 we want to be compatible. We only permit this
8575 extended range for an instruction which does not
8576 provide any further alternates, since those
8577 alternates may handle other cases. People should
8578 use the numbers they mean, rather than relying on
8579 a mysterious sign extension. */
8580 more = (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8581 strcmp (insn->name, insn[1].name) == 0);
8582 if (more)
8583 max = 0x8000;
8584 else
8585 max = 0x10000;
fb1b3232 8586 if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
252b5132 8587 || ((imm_expr.X_add_number < -0x8000
beae10d5
KH
8588 || imm_expr.X_add_number >= max)
8589 && imm_expr.X_op == O_constant)
252b5132
RH
8590 || (more
8591 && imm_expr.X_add_number < 0
ca4e0257 8592 && HAVE_64BIT_GPRS
252b5132
RH
8593 && imm_expr.X_unsigned
8594 && sizeof (imm_expr.X_add_number) <= 4))
8595 {
8596 if (more)
8597 break;
2ae7e77b
AH
8598 if (imm_expr.X_op == O_constant
8599 || imm_expr.X_op == O_big)
252b5132
RH
8600 as_bad (_("16 bit expression not in range -32768..32767"));
8601 }
8602 }
8603 s = expr_end;
8604 continue;
8605
8606 case 'o': /* 16 bit offset */
8607 c = my_getSmallExpression (&offset_expr, s);
8608
8609 /* If this value won't fit into a 16 bit offset, then go
8610 find a macro that will generate the 32 bit offset
afdbd6d0 8611 code pattern. */
fb1b3232 8612 if (c == S_EX_NONE
252b5132
RH
8613 && (offset_expr.X_op != O_constant
8614 || offset_expr.X_add_number >= 0x8000
afdbd6d0 8615 || offset_expr.X_add_number < -0x8000))
252b5132
RH
8616 break;
8617
fb1b3232 8618 if (c == S_EX_HI)
252b5132
RH
8619 {
8620 if (offset_expr.X_op != O_constant)
8621 break;
8622 offset_expr.X_add_number =
8623 (offset_expr.X_add_number >> 16) & 0xffff;
8624 }
f6688943 8625 *offset_reloc = BFD_RELOC_LO16;
252b5132
RH
8626 s = expr_end;
8627 continue;
8628
8629 case 'p': /* pc relative offset */
cb56d3d3 8630 if (mips_pic == EMBEDDED_PIC)
f6688943 8631 *offset_reloc = BFD_RELOC_16_PCREL_S2;
cb56d3d3 8632 else
f6688943 8633 *offset_reloc = BFD_RELOC_16_PCREL;
252b5132
RH
8634 my_getExpression (&offset_expr, s);
8635 s = expr_end;
8636 continue;
8637
8638 case 'u': /* upper 16 bits */
8639 c = my_getSmallExpression (&imm_expr, s);
f6688943 8640 *imm_reloc = BFD_RELOC_LO16;
e7d556df 8641 if (c != S_EX_NONE)
252b5132 8642 {
fb1b3232 8643 if (c != S_EX_LO)
252b5132
RH
8644 {
8645 if (imm_expr.X_op == O_constant)
8646 imm_expr.X_add_number =
8647 (imm_expr.X_add_number >> 16) & 0xffff;
fb1b3232 8648 else if (c == S_EX_HI)
252b5132 8649 {
f6688943 8650 *imm_reloc = BFD_RELOC_HI16_S;
252b5132
RH
8651 imm_unmatched_hi = true;
8652 }
645dc66c
TS
8653#ifdef OBJ_ELF
8654 else if (c == S_EX_HIGHEST)
98d3f06f 8655 *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
ad8d3bb3
TS
8656 else if (c == S_EX_GP_REL)
8657 {
8658 /* This occurs in NewABI only. */
8659 c = my_getSmallExpression (&imm_expr, s);
8660 if (c != S_EX_NEG)
8661 as_bad (_("bad composition of relocations"));
8662 else
8663 {
8664 c = my_getSmallExpression (&imm_expr, s);
8665 if (c != S_EX_HI)
8666 as_bad (_("bad composition of relocations"));
8667 else
8668 {
8669 imm_reloc[0] = BFD_RELOC_GPREL16;
8670 imm_reloc[1] = BFD_RELOC_MIPS_SUB;
8671 imm_reloc[2] = BFD_RELOC_HI16_S;
8672 }
8673 }
8674 }
8675#endif
252b5132 8676 else
f6688943 8677 *imm_reloc = BFD_RELOC_HI16;
252b5132
RH
8678 }
8679 else if (imm_expr.X_op == O_constant)
8680 imm_expr.X_add_number &= 0xffff;
8681 }
8682 if (imm_expr.X_op == O_constant
8683 && (imm_expr.X_add_number < 0
8684 || imm_expr.X_add_number >= 0x10000))
8685 as_bad (_("lui expression not in range 0..65535"));
8686 s = expr_end;
8687 continue;
8688
8689 case 'a': /* 26 bit address */
8690 my_getExpression (&offset_expr, s);
8691 s = expr_end;
f6688943 8692 *offset_reloc = BFD_RELOC_MIPS_JMP;
252b5132
RH
8693 continue;
8694
8695 case 'N': /* 3 bit branch condition code */
8696 case 'M': /* 3 bit compare condition code */
8697 if (strncmp (s, "$fcc", 4) != 0)
8698 break;
8699 s += 4;
8700 regno = 0;
8701 do
8702 {
8703 regno *= 10;
8704 regno += *s - '0';
8705 ++s;
8706 }
3882b010 8707 while (ISDIGIT (*s));
252b5132
RH
8708 if (regno > 7)
8709 as_bad (_("invalid condition code register $fcc%d"), regno);
8710 if (*args == 'N')
8711 ip->insn_opcode |= regno << OP_SH_BCC;
8712 else
8713 ip->insn_opcode |= regno << OP_SH_CCC;
beae10d5 8714 continue;
252b5132 8715
156c2f8b
NC
8716 case 'H':
8717 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
8718 s += 2;
3882b010 8719 if (ISDIGIT (*s))
156c2f8b
NC
8720 {
8721 c = 0;
8722 do
8723 {
8724 c *= 10;
8725 c += *s - '0';
8726 ++s;
8727 }
3882b010 8728 while (ISDIGIT (*s));
156c2f8b
NC
8729 }
8730 else
8731 c = 8; /* Invalid sel value. */
8732
8733 if (c > 7)
8734 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
8735 ip->insn_opcode |= c;
8736 continue;
8737
252b5132
RH
8738 default:
8739 as_bad (_("bad char = '%c'\n"), *args);
8740 internalError ();
8741 }
8742 break;
8743 }
8744 /* Args don't match. */
8745 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8746 !strcmp (insn->name, insn[1].name))
8747 {
8748 ++insn;
8749 s = argsStart;
268f6bed 8750 insn_error = _("illegal operands");
252b5132
RH
8751 continue;
8752 }
268f6bed
L
8753 if (save_c)
8754 *(--s) = save_c;
252b5132
RH
8755 insn_error = _("illegal operands");
8756 return;
8757 }
8758}
8759
8760/* This routine assembles an instruction into its binary format when
8761 assembling for the mips16. As a side effect, it sets one of the
8762 global variables imm_reloc or offset_reloc to the type of
8763 relocation to do if one of the operands is an address expression.
8764 It also sets mips16_small and mips16_ext if the user explicitly
8765 requested a small or extended instruction. */
8766
8767static void
8768mips16_ip (str, ip)
8769 char *str;
8770 struct mips_cl_insn *ip;
8771{
8772 char *s;
8773 const char *args;
8774 struct mips_opcode *insn;
8775 char *argsstart;
8776 unsigned int regno;
8777 unsigned int lastregno = 0;
8778 char *s_reset;
8779
8780 insn_error = NULL;
8781
8782 mips16_small = false;
8783 mips16_ext = false;
8784
3882b010 8785 for (s = str; ISLOWER (*s); ++s)
252b5132
RH
8786 ;
8787 switch (*s)
8788 {
8789 case '\0':
8790 break;
8791
8792 case ' ':
8793 *s++ = '\0';
8794 break;
8795
8796 case '.':
8797 if (s[1] == 't' && s[2] == ' ')
8798 {
8799 *s = '\0';
8800 mips16_small = true;
8801 s += 3;
8802 break;
8803 }
8804 else if (s[1] == 'e' && s[2] == ' ')
8805 {
8806 *s = '\0';
8807 mips16_ext = true;
8808 s += 3;
8809 break;
8810 }
8811 /* Fall through. */
8812 default:
8813 insn_error = _("unknown opcode");
8814 return;
8815 }
8816
8817 if (mips_opts.noautoextend && ! mips16_ext)
8818 mips16_small = true;
8819
8820 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
8821 {
8822 insn_error = _("unrecognized opcode");
8823 return;
8824 }
8825
8826 argsstart = s;
8827 for (;;)
8828 {
8829 assert (strcmp (insn->name, str) == 0);
8830
8831 ip->insn_mo = insn;
8832 ip->insn_opcode = insn->match;
8833 ip->use_extend = false;
8834 imm_expr.X_op = O_absent;
f6688943
TS
8835 imm_reloc[0] = BFD_RELOC_UNUSED;
8836 imm_reloc[1] = BFD_RELOC_UNUSED;
8837 imm_reloc[2] = BFD_RELOC_UNUSED;
252b5132 8838 offset_expr.X_op = O_absent;
f6688943
TS
8839 offset_reloc[0] = BFD_RELOC_UNUSED;
8840 offset_reloc[1] = BFD_RELOC_UNUSED;
8841 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
8842 for (args = insn->args; 1; ++args)
8843 {
8844 int c;
8845
8846 if (*s == ' ')
8847 ++s;
8848
8849 /* In this switch statement we call break if we did not find
8850 a match, continue if we did find a match, or return if we
8851 are done. */
8852
8853 c = *args;
8854 switch (c)
8855 {
8856 case '\0':
8857 if (*s == '\0')
8858 {
8859 /* Stuff the immediate value in now, if we can. */
8860 if (imm_expr.X_op == O_constant
f6688943 8861 && *imm_reloc > BFD_RELOC_UNUSED
252b5132
RH
8862 && insn->pinfo != INSN_MACRO)
8863 {
c4e7957c 8864 mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
252b5132
RH
8865 imm_expr.X_add_number, true, mips16_small,
8866 mips16_ext, &ip->insn_opcode,
8867 &ip->use_extend, &ip->extend);
8868 imm_expr.X_op = O_absent;
f6688943 8869 *imm_reloc = BFD_RELOC_UNUSED;
252b5132
RH
8870 }
8871
8872 return;
8873 }
8874 break;
8875
8876 case ',':
8877 if (*s++ == c)
8878 continue;
8879 s--;
8880 switch (*++args)
8881 {
8882 case 'v':
8883 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
8884 continue;
8885 case 'w':
8886 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
8887 continue;
8888 }
8889 break;
8890
8891 case '(':
8892 case ')':
8893 if (*s++ == c)
8894 continue;
8895 break;
8896
8897 case 'v':
8898 case 'w':
8899 if (s[0] != '$')
8900 {
8901 if (c == 'v')
8902 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
8903 else
8904 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
8905 ++args;
8906 continue;
8907 }
8908 /* Fall through. */
8909 case 'x':
8910 case 'y':
8911 case 'z':
8912 case 'Z':
8913 case '0':
8914 case 'S':
8915 case 'R':
8916 case 'X':
8917 case 'Y':
8918 if (s[0] != '$')
8919 break;
8920 s_reset = s;
3882b010 8921 if (ISDIGIT (s[1]))
252b5132
RH
8922 {
8923 ++s;
8924 regno = 0;
8925 do
8926 {
8927 regno *= 10;
8928 regno += *s - '0';
8929 ++s;
8930 }
3882b010 8931 while (ISDIGIT (*s));
252b5132
RH
8932 if (regno > 31)
8933 {
8934 as_bad (_("invalid register number (%d)"), regno);
8935 regno = 2;
8936 }
8937 }
8938 else
8939 {
8940 if (s[1] == 'f' && s[2] == 'p')
8941 {
8942 s += 3;
8943 regno = FP;
8944 }
8945 else if (s[1] == 's' && s[2] == 'p')
8946 {
8947 s += 3;
8948 regno = SP;
8949 }
8950 else if (s[1] == 'g' && s[2] == 'p')
8951 {
8952 s += 3;
8953 regno = GP;
8954 }
8955 else if (s[1] == 'a' && s[2] == 't')
8956 {
8957 s += 3;
8958 regno = AT;
8959 }
8960 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8961 {
8962 s += 4;
8963 regno = KT0;
8964 }
8965 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8966 {
8967 s += 4;
8968 regno = KT1;
8969 }
8970 else
8971 break;
8972 }
8973
8974 if (*s == ' ')
8975 ++s;
8976 if (args[1] != *s)
8977 {
8978 if (c == 'v' || c == 'w')
8979 {
8980 regno = mips16_to_32_reg_map[lastregno];
8981 s = s_reset;
f9419b05 8982 ++args;
252b5132
RH
8983 }
8984 }
8985
8986 switch (c)
8987 {
8988 case 'x':
8989 case 'y':
8990 case 'z':
8991 case 'v':
8992 case 'w':
8993 case 'Z':
8994 regno = mips32_to_16_reg_map[regno];
8995 break;
8996
8997 case '0':
8998 if (regno != 0)
8999 regno = ILLEGAL_REG;
9000 break;
9001
9002 case 'S':
9003 if (regno != SP)
9004 regno = ILLEGAL_REG;
9005 break;
9006
9007 case 'R':
9008 if (regno != RA)
9009 regno = ILLEGAL_REG;
9010 break;
9011
9012 case 'X':
9013 case 'Y':
9014 if (regno == AT && ! mips_opts.noat)
9015 as_warn (_("used $at without \".set noat\""));
9016 break;
9017
9018 default:
9019 internalError ();
9020 }
9021
9022 if (regno == ILLEGAL_REG)
9023 break;
9024
9025 switch (c)
9026 {
9027 case 'x':
9028 case 'v':
9029 ip->insn_opcode |= regno << MIPS16OP_SH_RX;
9030 break;
9031 case 'y':
9032 case 'w':
9033 ip->insn_opcode |= regno << MIPS16OP_SH_RY;
9034 break;
9035 case 'z':
9036 ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
9037 break;
9038 case 'Z':
9039 ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
9040 case '0':
9041 case 'S':
9042 case 'R':
9043 break;
9044 case 'X':
9045 ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
9046 break;
9047 case 'Y':
9048 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
9049 ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
9050 break;
9051 default:
9052 internalError ();
9053 }
9054
9055 lastregno = regno;
9056 continue;
9057
9058 case 'P':
9059 if (strncmp (s, "$pc", 3) == 0)
9060 {
9061 s += 3;
9062 continue;
9063 }
9064 break;
9065
9066 case '<':
9067 case '>':
9068 case '[':
9069 case ']':
9070 case '4':
9071 case '5':
9072 case 'H':
9073 case 'W':
9074 case 'D':
9075 case 'j':
9076 case '8':
9077 case 'V':
9078 case 'C':
9079 case 'U':
9080 case 'k':
9081 case 'K':
9082 if (s[0] == '%'
9083 && strncmp (s + 1, "gprel(", sizeof "gprel(" - 1) == 0)
9084 {
9085 /* This is %gprel(SYMBOL). We need to read SYMBOL,
9086 and generate the appropriate reloc. If the text
9087 inside %gprel is not a symbol name with an
9088 optional offset, then we generate a normal reloc
9089 and will probably fail later. */
9090 my_getExpression (&imm_expr, s + sizeof "%gprel" - 1);
9091 if (imm_expr.X_op == O_symbol)
9092 {
9093 mips16_ext = true;
f6688943 9094 *imm_reloc = BFD_RELOC_MIPS16_GPREL;
252b5132
RH
9095 s = expr_end;
9096 ip->use_extend = true;
9097 ip->extend = 0;
9098 continue;
9099 }
9100 }
9101 else
9102 {
9103 /* Just pick up a normal expression. */
9104 my_getExpression (&imm_expr, s);
9105 }
9106
9107 if (imm_expr.X_op == O_register)
9108 {
9109 /* What we thought was an expression turned out to
9110 be a register. */
9111
9112 if (s[0] == '(' && args[1] == '(')
9113 {
9114 /* It looks like the expression was omitted
9115 before a register indirection, which means
9116 that the expression is implicitly zero. We
9117 still set up imm_expr, so that we handle
9118 explicit extensions correctly. */
9119 imm_expr.X_op = O_constant;
9120 imm_expr.X_add_number = 0;
f6688943 9121 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
9122 continue;
9123 }
9124
9125 break;
9126 }
9127
9128 /* We need to relax this instruction. */
f6688943 9129 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
9130 s = expr_end;
9131 continue;
9132
9133 case 'p':
9134 case 'q':
9135 case 'A':
9136 case 'B':
9137 case 'E':
9138 /* We use offset_reloc rather than imm_reloc for the PC
9139 relative operands. This lets macros with both
9140 immediate and address operands work correctly. */
9141 my_getExpression (&offset_expr, s);
9142
9143 if (offset_expr.X_op == O_register)
9144 break;
9145
9146 /* We need to relax this instruction. */
f6688943 9147 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
9148 s = expr_end;
9149 continue;
9150
9151 case '6': /* break code */
9152 my_getExpression (&imm_expr, s);
9153 check_absolute_expr (ip, &imm_expr);
9154 if ((unsigned long) imm_expr.X_add_number > 63)
9155 {
9156 as_warn (_("Invalid value for `%s' (%lu)"),
9157 ip->insn_mo->name,
9158 (unsigned long) imm_expr.X_add_number);
9159 imm_expr.X_add_number &= 0x3f;
9160 }
9161 ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
9162 imm_expr.X_op = O_absent;
9163 s = expr_end;
9164 continue;
9165
9166 case 'a': /* 26 bit address */
9167 my_getExpression (&offset_expr, s);
9168 s = expr_end;
f6688943 9169 *offset_reloc = BFD_RELOC_MIPS16_JMP;
252b5132
RH
9170 ip->insn_opcode <<= 16;
9171 continue;
9172
9173 case 'l': /* register list for entry macro */
9174 case 'L': /* register list for exit macro */
9175 {
9176 int mask;
9177
9178 if (c == 'l')
9179 mask = 0;
9180 else
9181 mask = 7 << 3;
9182 while (*s != '\0')
9183 {
9184 int freg, reg1, reg2;
9185
9186 while (*s == ' ' || *s == ',')
9187 ++s;
9188 if (*s != '$')
9189 {
9190 as_bad (_("can't parse register list"));
9191 break;
9192 }
9193 ++s;
9194 if (*s != 'f')
9195 freg = 0;
9196 else
9197 {
9198 freg = 1;
9199 ++s;
9200 }
9201 reg1 = 0;
3882b010 9202 while (ISDIGIT (*s))
252b5132
RH
9203 {
9204 reg1 *= 10;
9205 reg1 += *s - '0';
9206 ++s;
9207 }
9208 if (*s == ' ')
9209 ++s;
9210 if (*s != '-')
9211 reg2 = reg1;
9212 else
9213 {
9214 ++s;
9215 if (*s != '$')
9216 break;
9217 ++s;
9218 if (freg)
9219 {
9220 if (*s == 'f')
9221 ++s;
9222 else
9223 {
9224 as_bad (_("invalid register list"));
9225 break;
9226 }
9227 }
9228 reg2 = 0;
3882b010 9229 while (ISDIGIT (*s))
252b5132
RH
9230 {
9231 reg2 *= 10;
9232 reg2 += *s - '0';
9233 ++s;
9234 }
9235 }
9236 if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
9237 {
9238 mask &= ~ (7 << 3);
9239 mask |= 5 << 3;
9240 }
9241 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
9242 {
9243 mask &= ~ (7 << 3);
9244 mask |= 6 << 3;
9245 }
9246 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
9247 mask |= (reg2 - 3) << 3;
9248 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
9249 mask |= (reg2 - 15) << 1;
f9419b05 9250 else if (reg1 == RA && reg2 == RA)
252b5132
RH
9251 mask |= 1;
9252 else
9253 {
9254 as_bad (_("invalid register list"));
9255 break;
9256 }
9257 }
9258 /* The mask is filled in in the opcode table for the
9259 benefit of the disassembler. We remove it before
9260 applying the actual mask. */
9261 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
9262 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
9263 }
9264 continue;
9265
9266 case 'e': /* extend code */
9267 my_getExpression (&imm_expr, s);
9268 check_absolute_expr (ip, &imm_expr);
9269 if ((unsigned long) imm_expr.X_add_number > 0x7ff)
9270 {
9271 as_warn (_("Invalid value for `%s' (%lu)"),
9272 ip->insn_mo->name,
9273 (unsigned long) imm_expr.X_add_number);
9274 imm_expr.X_add_number &= 0x7ff;
9275 }
9276 ip->insn_opcode |= imm_expr.X_add_number;
9277 imm_expr.X_op = O_absent;
9278 s = expr_end;
9279 continue;
9280
9281 default:
9282 internalError ();
9283 }
9284 break;
9285 }
9286
9287 /* Args don't match. */
9288 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
9289 strcmp (insn->name, insn[1].name) == 0)
9290 {
9291 ++insn;
9292 s = argsstart;
9293 continue;
9294 }
9295
9296 insn_error = _("illegal operands");
9297
9298 return;
9299 }
9300}
9301
9302/* This structure holds information we know about a mips16 immediate
9303 argument type. */
9304
e972090a
NC
9305struct mips16_immed_operand
9306{
252b5132
RH
9307 /* The type code used in the argument string in the opcode table. */
9308 int type;
9309 /* The number of bits in the short form of the opcode. */
9310 int nbits;
9311 /* The number of bits in the extended form of the opcode. */
9312 int extbits;
9313 /* The amount by which the short form is shifted when it is used;
9314 for example, the sw instruction has a shift count of 2. */
9315 int shift;
9316 /* The amount by which the short form is shifted when it is stored
9317 into the instruction code. */
9318 int op_shift;
9319 /* Non-zero if the short form is unsigned. */
9320 int unsp;
9321 /* Non-zero if the extended form is unsigned. */
9322 int extu;
9323 /* Non-zero if the value is PC relative. */
9324 int pcrel;
9325};
9326
9327/* The mips16 immediate operand types. */
9328
9329static const struct mips16_immed_operand mips16_immed_operands[] =
9330{
9331 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9332 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9333 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9334 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9335 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
9336 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
9337 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
9338 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
9339 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
9340 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
9341 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
9342 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
9343 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
9344 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
9345 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
9346 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
9347 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9348 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9349 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
9350 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
9351 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
9352};
9353
9354#define MIPS16_NUM_IMMED \
9355 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
9356
9357/* Handle a mips16 instruction with an immediate value. This or's the
9358 small immediate value into *INSN. It sets *USE_EXTEND to indicate
9359 whether an extended value is needed; if one is needed, it sets
9360 *EXTEND to the value. The argument type is TYPE. The value is VAL.
9361 If SMALL is true, an unextended opcode was explicitly requested.
9362 If EXT is true, an extended opcode was explicitly requested. If
9363 WARN is true, warn if EXT does not match reality. */
9364
9365static void
9366mips16_immed (file, line, type, val, warn, small, ext, insn, use_extend,
9367 extend)
9368 char *file;
9369 unsigned int line;
9370 int type;
9371 offsetT val;
9372 boolean warn;
9373 boolean small;
9374 boolean ext;
9375 unsigned long *insn;
9376 boolean *use_extend;
9377 unsigned short *extend;
9378{
9379 register const struct mips16_immed_operand *op;
9380 int mintiny, maxtiny;
9381 boolean needext;
9382
9383 op = mips16_immed_operands;
9384 while (op->type != type)
9385 {
9386 ++op;
9387 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
9388 }
9389
9390 if (op->unsp)
9391 {
9392 if (type == '<' || type == '>' || type == '[' || type == ']')
9393 {
9394 mintiny = 1;
9395 maxtiny = 1 << op->nbits;
9396 }
9397 else
9398 {
9399 mintiny = 0;
9400 maxtiny = (1 << op->nbits) - 1;
9401 }
9402 }
9403 else
9404 {
9405 mintiny = - (1 << (op->nbits - 1));
9406 maxtiny = (1 << (op->nbits - 1)) - 1;
9407 }
9408
9409 /* Branch offsets have an implicit 0 in the lowest bit. */
9410 if (type == 'p' || type == 'q')
9411 val /= 2;
9412
9413 if ((val & ((1 << op->shift) - 1)) != 0
9414 || val < (mintiny << op->shift)
9415 || val > (maxtiny << op->shift))
9416 needext = true;
9417 else
9418 needext = false;
9419
9420 if (warn && ext && ! needext)
beae10d5
KH
9421 as_warn_where (file, line,
9422 _("extended operand requested but not required"));
252b5132
RH
9423 if (small && needext)
9424 as_bad_where (file, line, _("invalid unextended operand value"));
9425
9426 if (small || (! ext && ! needext))
9427 {
9428 int insnval;
9429
9430 *use_extend = false;
9431 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
9432 insnval <<= op->op_shift;
9433 *insn |= insnval;
9434 }
9435 else
9436 {
9437 long minext, maxext;
9438 int extval;
9439
9440 if (op->extu)
9441 {
9442 minext = 0;
9443 maxext = (1 << op->extbits) - 1;
9444 }
9445 else
9446 {
9447 minext = - (1 << (op->extbits - 1));
9448 maxext = (1 << (op->extbits - 1)) - 1;
9449 }
9450 if (val < minext || val > maxext)
9451 as_bad_where (file, line,
9452 _("operand value out of range for instruction"));
9453
9454 *use_extend = true;
9455 if (op->extbits == 16)
9456 {
9457 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
9458 val &= 0x1f;
9459 }
9460 else if (op->extbits == 15)
9461 {
9462 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
9463 val &= 0xf;
9464 }
9465 else
9466 {
9467 extval = ((val & 0x1f) << 6) | (val & 0x20);
9468 val = 0;
9469 }
9470
9471 *extend = (unsigned short) extval;
9472 *insn |= val;
9473 }
9474}
9475\f
ad8d3bb3
TS
9476static struct percent_op_match
9477{
9478 const char *str;
9479 const enum small_ex_type type;
9480} percent_op[] =
9481{
ad8d3bb3
TS
9482 {"%lo", S_EX_LO},
9483#ifdef OBJ_ELF
394f9b3a
TS
9484 {"%call_hi", S_EX_CALL_HI},
9485 {"%call_lo", S_EX_CALL_LO},
ad8d3bb3
TS
9486 {"%call16", S_EX_CALL16},
9487 {"%got_disp", S_EX_GOT_DISP},
9488 {"%got_page", S_EX_GOT_PAGE},
9489 {"%got_ofst", S_EX_GOT_OFST},
9490 {"%got_hi", S_EX_GOT_HI},
9491 {"%got_lo", S_EX_GOT_LO},
394f9b3a
TS
9492 {"%got", S_EX_GOT},
9493 {"%gp_rel", S_EX_GP_REL},
9494 {"%half", S_EX_HALF},
ad8d3bb3 9495 {"%highest", S_EX_HIGHEST},
394f9b3a
TS
9496 {"%higher", S_EX_HIGHER},
9497 {"%neg", S_EX_NEG},
ad8d3bb3 9498#endif
394f9b3a 9499 {"%hi", S_EX_HI}
ad8d3bb3
TS
9500};
9501
9502/* Parse small expression input. STR gets adjusted to eat up whitespace.
9503 It detects valid "%percent_op(...)" and "($reg)" strings. Percent_op's
9504 can be nested, this is handled by blanking the innermost, parsing the
9505 rest by subsequent calls. */
252b5132
RH
9506
9507static int
ad8d3bb3
TS
9508my_getSmallParser (str, len, nestlevel)
9509 char **str;
9510 unsigned int *len;
9511 int *nestlevel;
252b5132 9512{
ad8d3bb3
TS
9513 *len = 0;
9514 *str += strspn (*str, " \t");
394f9b3a 9515 /* Check for expression in parentheses. */
ad8d3bb3 9516 if (**str == '(')
252b5132 9517 {
ad8d3bb3
TS
9518 char *b = *str + 1 + strspn (*str + 1, " \t");
9519 char *e;
9520
9521 /* Check for base register. */
9522 if (b[0] == '$')
9523 {
9524 if (strchr (b, ')')
9525 && (e = b + strcspn (b, ") \t"))
9526 && e - b > 1 && e - b < 4)
9527 {
98d3f06f
KH
9528 if ((e - b == 3
9529 && ((b[1] == 'f' && b[2] == 'p')
9530 || (b[1] == 's' && b[2] == 'p')
9531 || (b[1] == 'g' && b[2] == 'p')
9532 || (b[1] == 'a' && b[2] == 't')
9533 || (ISDIGIT (b[1])
9534 && ISDIGIT (b[2]))))
9535 || (ISDIGIT (b[1])))
9536 {
9537 *len = strcspn (*str, ")") + 1;
9538 return S_EX_REGISTER;
9539 }
ad8d3bb3
TS
9540 }
9541 }
394f9b3a 9542 /* Check for percent_op (in parentheses). */
ad8d3bb3
TS
9543 else if (b[0] == '%')
9544 {
9545 *str = b;
394f9b3a 9546 return my_getPercentOp (str, len, nestlevel);
ad8d3bb3 9547 }
76b3015f 9548
394f9b3a
TS
9549 /* Some other expression in the parentheses, which can contain
9550 parentheses itself. Attempt to find the matching one. */
9551 {
9552 int pcnt = 1;
9553 char *s;
9554
9555 *len = 1;
9556 for (s = *str + 1; *s && pcnt; s++, (*len)++)
9557 {
9558 if (*s == '(')
f9419b05 9559 ++pcnt;
394f9b3a 9560 else if (*s == ')')
f9419b05 9561 --pcnt;
394f9b3a
TS
9562 }
9563 }
fb1b3232 9564 }
394f9b3a 9565 /* Check for percent_op (outside of parentheses). */
ad8d3bb3 9566 else if (*str[0] == '%')
394f9b3a
TS
9567 return my_getPercentOp (str, len, nestlevel);
9568
9569 /* Any other expression. */
9570 return S_EX_NONE;
9571}
ad8d3bb3 9572
394f9b3a
TS
9573static int
9574my_getPercentOp (str, len, nestlevel)
9575 char **str;
9576 unsigned int *len;
9577 int *nestlevel;
9578{
9579 char *tmp = *str + 1;
9580 unsigned int i = 0;
ad8d3bb3 9581
394f9b3a
TS
9582 while (ISALPHA (*tmp) || *tmp == '_')
9583 {
9584 *tmp = TOLOWER (*tmp);
9585 tmp++;
9586 }
9587 while (i < (sizeof (percent_op) / sizeof (struct percent_op_match)))
9588 {
9589 if (strncmp (*str, percent_op[i].str, strlen (percent_op[i].str)))
98d3f06f 9590 i++;
394f9b3a 9591 else
ad8d3bb3 9592 {
394f9b3a 9593 int type = percent_op[i].type;
ad8d3bb3 9594
394f9b3a
TS
9595 /* Only %hi and %lo are allowed for OldABI. */
9596 if (! HAVE_NEWABI && type != S_EX_HI && type != S_EX_LO)
9597 return S_EX_NONE;
ad8d3bb3 9598
394f9b3a 9599 *len = strlen (percent_op[i].str);
f9419b05 9600 ++(*nestlevel);
394f9b3a 9601 return type;
ad8d3bb3 9602 }
fb1b3232 9603 }
ad8d3bb3
TS
9604 return S_EX_NONE;
9605}
9606
9607static int
9608my_getSmallExpression (ep, str)
9609 expressionS *ep;
9610 char *str;
9611{
9612 static char *oldstr = NULL;
9613 int c = S_EX_NONE;
9614 int oldc;
394f9b3a 9615 int nestlevel = -1;
ad8d3bb3
TS
9616 unsigned int len;
9617
394f9b3a
TS
9618 /* Don't update oldstr if the last call had nested percent_op's. We need
9619 it to parse the outer ones later. */
ad8d3bb3
TS
9620 if (! oldstr)
9621 oldstr = str;
76b3015f 9622
ad8d3bb3 9623 do
fb1b3232 9624 {
ad8d3bb3 9625 oldc = c;
394f9b3a 9626 c = my_getSmallParser (&str, &len, &nestlevel);
ad8d3bb3
TS
9627 if (c != S_EX_NONE && c != S_EX_REGISTER)
9628 str += len;
fb1b3232 9629 }
ad8d3bb3
TS
9630 while (c != S_EX_NONE && c != S_EX_REGISTER);
9631
394f9b3a 9632 if (nestlevel >= 0)
fb1b3232 9633 {
394f9b3a
TS
9634 /* A percent_op was encountered. Don't try to get an expression if
9635 it is already blanked out. */
ad8d3bb3
TS
9636 if (*(str + strspn (str + 1, " )")) != ')')
9637 {
9638 char save;
9639
394f9b3a 9640 /* Let my_getExpression() stop at the closing parenthesis. */
ad8d3bb3
TS
9641 save = *(str + len);
9642 *(str + len) = '\0';
9643 my_getExpression (ep, str);
9644 *(str + len) = save;
9645 }
394f9b3a 9646 if (nestlevel > 0)
ad8d3bb3 9647 {
394f9b3a
TS
9648 /* Blank out including the % sign and the proper matching
9649 parenthesis. */
9650 int pcnt = 1;
9651 char *s = strrchr (oldstr, '%');
9652 char *end;
9653
9654 for (end = strchr (s, '(') + 1; *end && pcnt; end++)
9655 {
9656 if (*end == '(')
f9419b05 9657 ++pcnt;
394f9b3a 9658 else if (*end == ')')
f9419b05 9659 --pcnt;
394f9b3a
TS
9660 }
9661
9662 memset (s, ' ', end - s);
ad8d3bb3
TS
9663 str = oldstr;
9664 }
9665 else
394f9b3a
TS
9666 expr_end = str + len;
9667
ad8d3bb3 9668 c = oldc;
fb1b3232 9669 }
ad8d3bb3 9670 else if (c == S_EX_NONE)
fb1b3232 9671 {
ad8d3bb3 9672 my_getExpression (ep, str);
fb1b3232 9673 }
ad8d3bb3 9674 else if (c == S_EX_REGISTER)
fb1b3232 9675 {
ad8d3bb3
TS
9676 ep->X_op = O_constant;
9677 expr_end = str;
9678 ep->X_add_symbol = NULL;
9679 ep->X_op_symbol = NULL;
9680 ep->X_add_number = 0;
fb1b3232 9681 }
fb1b3232
TS
9682 else
9683 {
98d3f06f 9684 as_fatal (_("internal error"));
fb1b3232 9685 }
252b5132 9686
394f9b3a
TS
9687 if (nestlevel <= 0)
9688 /* All percent_op's have been handled. */
ad8d3bb3 9689 oldstr = NULL;
fb1b3232 9690
fb1b3232 9691 return c;
252b5132
RH
9692}
9693
9694static void
9695my_getExpression (ep, str)
9696 expressionS *ep;
9697 char *str;
9698{
9699 char *save_in;
98aa84af 9700 valueT val;
252b5132
RH
9701
9702 save_in = input_line_pointer;
9703 input_line_pointer = str;
9704 expression (ep);
9705 expr_end = input_line_pointer;
9706 input_line_pointer = save_in;
9707
9708 /* If we are in mips16 mode, and this is an expression based on `.',
9709 then we bump the value of the symbol by 1 since that is how other
9710 text symbols are handled. We don't bother to handle complex
9711 expressions, just `.' plus or minus a constant. */
9712 if (mips_opts.mips16
9713 && ep->X_op == O_symbol
9714 && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
9715 && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
49309057
ILT
9716 && symbol_get_frag (ep->X_add_symbol) == frag_now
9717 && symbol_constant_p (ep->X_add_symbol)
98aa84af
AM
9718 && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
9719 S_SET_VALUE (ep->X_add_symbol, val + 1);
252b5132
RH
9720}
9721
9722/* Turn a string in input_line_pointer into a floating point constant
bc0d738a
NC
9723 of type TYPE, and store the appropriate bytes in *LITP. The number
9724 of LITTLENUMS emitted is stored in *SIZEP. An error message is
252b5132
RH
9725 returned, or NULL on OK. */
9726
9727char *
9728md_atof (type, litP, sizeP)
9729 int type;
9730 char *litP;
9731 int *sizeP;
9732{
9733 int prec;
9734 LITTLENUM_TYPE words[4];
9735 char *t;
9736 int i;
9737
9738 switch (type)
9739 {
9740 case 'f':
9741 prec = 2;
9742 break;
9743
9744 case 'd':
9745 prec = 4;
9746 break;
9747
9748 default:
9749 *sizeP = 0;
9750 return _("bad call to md_atof");
9751 }
9752
9753 t = atof_ieee (input_line_pointer, type, words);
9754 if (t)
9755 input_line_pointer = t;
9756
9757 *sizeP = prec * 2;
9758
9759 if (! target_big_endian)
9760 {
9761 for (i = prec - 1; i >= 0; i--)
9762 {
9763 md_number_to_chars (litP, (valueT) words[i], 2);
9764 litP += 2;
9765 }
9766 }
9767 else
9768 {
9769 for (i = 0; i < prec; i++)
9770 {
9771 md_number_to_chars (litP, (valueT) words[i], 2);
9772 litP += 2;
9773 }
9774 }
bdaaa2e1 9775
252b5132
RH
9776 return NULL;
9777}
9778
9779void
9780md_number_to_chars (buf, val, n)
9781 char *buf;
9782 valueT val;
9783 int n;
9784{
9785 if (target_big_endian)
9786 number_to_chars_bigendian (buf, val, n);
9787 else
9788 number_to_chars_littleendian (buf, val, n);
9789}
9790\f
ae948b86 9791#ifdef OBJ_ELF
e013f690
TS
9792static int support_64bit_objects(void)
9793{
9794 const char **list, **l;
9795
9796 list = bfd_target_list ();
9797 for (l = list; *l != NULL; l++)
9798#ifdef TE_TMIPS
9799 /* This is traditional mips */
9800 if (strcmp (*l, "elf64-tradbigmips") == 0
9801 || strcmp (*l, "elf64-tradlittlemips") == 0)
9802#else
9803 if (strcmp (*l, "elf64-bigmips") == 0
9804 || strcmp (*l, "elf64-littlemips") == 0)
9805#endif
9806 break;
9807 free (list);
9808 return (*l != NULL);
9809}
ae948b86 9810#endif /* OBJ_ELF */
e013f690 9811
39c0a331 9812CONST char *md_shortopts = "nO::g::G:";
252b5132 9813
e972090a
NC
9814struct option md_longopts[] =
9815{
252b5132
RH
9816#define OPTION_MIPS1 (OPTION_MD_BASE + 1)
9817 {"mips0", no_argument, NULL, OPTION_MIPS1},
9818 {"mips1", no_argument, NULL, OPTION_MIPS1},
9819#define OPTION_MIPS2 (OPTION_MD_BASE + 2)
9820 {"mips2", no_argument, NULL, OPTION_MIPS2},
9821#define OPTION_MIPS3 (OPTION_MD_BASE + 3)
9822 {"mips3", no_argument, NULL, OPTION_MIPS3},
9823#define OPTION_MIPS4 (OPTION_MD_BASE + 4)
9824 {"mips4", no_argument, NULL, OPTION_MIPS4},
ae948b86
TS
9825#define OPTION_MIPS5 (OPTION_MD_BASE + 5)
9826 {"mips5", no_argument, NULL, OPTION_MIPS5},
9827#define OPTION_MIPS32 (OPTION_MD_BASE + 6)
9828 {"mips32", no_argument, NULL, OPTION_MIPS32},
9829#define OPTION_MIPS64 (OPTION_MD_BASE + 7)
9830 {"mips64", no_argument, NULL, OPTION_MIPS64},
9831#define OPTION_MEMBEDDED_PIC (OPTION_MD_BASE + 8)
252b5132 9832 {"membedded-pic", no_argument, NULL, OPTION_MEMBEDDED_PIC},
ae948b86 9833#define OPTION_TRAP (OPTION_MD_BASE + 9)
252b5132
RH
9834 {"trap", no_argument, NULL, OPTION_TRAP},
9835 {"no-break", no_argument, NULL, OPTION_TRAP},
ae948b86 9836#define OPTION_BREAK (OPTION_MD_BASE + 10)
252b5132
RH
9837 {"break", no_argument, NULL, OPTION_BREAK},
9838 {"no-trap", no_argument, NULL, OPTION_BREAK},
ae948b86 9839#define OPTION_EB (OPTION_MD_BASE + 11)
252b5132 9840 {"EB", no_argument, NULL, OPTION_EB},
ae948b86 9841#define OPTION_EL (OPTION_MD_BASE + 12)
252b5132 9842 {"EL", no_argument, NULL, OPTION_EL},
ae948b86 9843#define OPTION_MIPS16 (OPTION_MD_BASE + 13)
252b5132 9844 {"mips16", no_argument, NULL, OPTION_MIPS16},
ae948b86 9845#define OPTION_NO_MIPS16 (OPTION_MD_BASE + 14)
252b5132 9846 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
ae948b86 9847#define OPTION_M7000_HILO_FIX (OPTION_MD_BASE + 15)
6b76fefe 9848 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
ae948b86 9849#define OPTION_NO_M7000_HILO_FIX (OPTION_MD_BASE + 16)
6b76fefe 9850 {"no-fix-7000", no_argument, NULL, OPTION_NO_M7000_HILO_FIX},
ae948b86
TS
9851#define OPTION_FP32 (OPTION_MD_BASE + 17)
9852 {"mfp32", no_argument, NULL, OPTION_FP32},
9853#define OPTION_GP32 (OPTION_MD_BASE + 18)
c97ef257 9854 {"mgp32", no_argument, NULL, OPTION_GP32},
ae948b86 9855#define OPTION_CONSTRUCT_FLOATS (OPTION_MD_BASE + 19)
119d663a 9856 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
ae948b86 9857#define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MD_BASE + 20)
119d663a 9858 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
ae948b86 9859#define OPTION_MARCH (OPTION_MD_BASE + 21)
ec68c924 9860 {"march", required_argument, NULL, OPTION_MARCH},
ae948b86 9861#define OPTION_MTUNE (OPTION_MD_BASE + 22)
ec68c924 9862 {"mtune", required_argument, NULL, OPTION_MTUNE},
ae948b86
TS
9863#define OPTION_MCPU (OPTION_MD_BASE + 23)
9864 {"mcpu", required_argument, NULL, OPTION_MCPU},
9865#define OPTION_M4650 (OPTION_MD_BASE + 24)
9866 {"m4650", no_argument, NULL, OPTION_M4650},
9867#define OPTION_NO_M4650 (OPTION_MD_BASE + 25)
9868 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
9869#define OPTION_M4010 (OPTION_MD_BASE + 26)
9870 {"m4010", no_argument, NULL, OPTION_M4010},
9871#define OPTION_NO_M4010 (OPTION_MD_BASE + 27)
9872 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
9873#define OPTION_M4100 (OPTION_MD_BASE + 28)
9874 {"m4100", no_argument, NULL, OPTION_M4100},
9875#define OPTION_NO_M4100 (OPTION_MD_BASE + 29)
9876 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
9877#define OPTION_M3900 (OPTION_MD_BASE + 30)
9878 {"m3900", no_argument, NULL, OPTION_M3900},
9879#define OPTION_NO_M3900 (OPTION_MD_BASE + 31)
9880 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
9881#define OPTION_GP64 (OPTION_MD_BASE + 32)
9882 {"mgp64", no_argument, NULL, OPTION_GP64},
1f25f5d3
CD
9883#define OPTION_MIPS3D (OPTION_MD_BASE + 33)
9884 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
9885#define OPTION_NO_MIPS3D (OPTION_MD_BASE + 34)
9886 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
156c2f8b 9887#ifdef OBJ_ELF
1f25f5d3 9888#define OPTION_ELF_BASE (OPTION_MD_BASE + 35)
156c2f8b 9889#define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
156c2f8b
NC
9890 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
9891 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
ae948b86 9892#define OPTION_NON_SHARED (OPTION_ELF_BASE + 1)
156c2f8b 9893 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
ae948b86 9894#define OPTION_XGOT (OPTION_ELF_BASE + 2)
156c2f8b 9895 {"xgot", no_argument, NULL, OPTION_XGOT},
ae948b86
TS
9896#define OPTION_MABI (OPTION_ELF_BASE + 3)
9897 {"mabi", required_argument, NULL, OPTION_MABI},
9898#define OPTION_32 (OPTION_ELF_BASE + 4)
156c2f8b 9899 {"32", no_argument, NULL, OPTION_32},
ae948b86 9900#define OPTION_N32 (OPTION_ELF_BASE + 5)
e013f690 9901 {"n32", no_argument, NULL, OPTION_N32},
ae948b86 9902#define OPTION_64 (OPTION_ELF_BASE + 6)
156c2f8b 9903 {"64", no_argument, NULL, OPTION_64},
ae948b86 9904#endif /* OBJ_ELF */
252b5132
RH
9905 {NULL, no_argument, NULL, 0}
9906};
156c2f8b 9907size_t md_longopts_size = sizeof (md_longopts);
252b5132
RH
9908
9909int
9910md_parse_option (c, arg)
9911 int c;
9912 char *arg;
9913{
9914 switch (c)
9915 {
119d663a
NC
9916 case OPTION_CONSTRUCT_FLOATS:
9917 mips_disable_float_construction = 0;
9918 break;
bdaaa2e1 9919
119d663a
NC
9920 case OPTION_NO_CONSTRUCT_FLOATS:
9921 mips_disable_float_construction = 1;
9922 break;
bdaaa2e1 9923
252b5132
RH
9924 case OPTION_TRAP:
9925 mips_trap = 1;
9926 break;
9927
9928 case OPTION_BREAK:
9929 mips_trap = 0;
9930 break;
9931
9932 case OPTION_EB:
9933 target_big_endian = 1;
9934 break;
9935
9936 case OPTION_EL:
9937 target_big_endian = 0;
9938 break;
9939
39c0a331
L
9940 case 'n':
9941 warn_nops = 1;
9942 break;
9943
252b5132
RH
9944 case 'O':
9945 if (arg && arg[1] == '0')
9946 mips_optimize = 1;
9947 else
9948 mips_optimize = 2;
9949 break;
9950
9951 case 'g':
9952 if (arg == NULL)
9953 mips_debug = 2;
9954 else
9955 mips_debug = atoi (arg);
9956 /* When the MIPS assembler sees -g or -g2, it does not do
9957 optimizations which limit full symbolic debugging. We take
9958 that to be equivalent to -O0. */
9959 if (mips_debug == 2)
9960 mips_optimize = 1;
9961 break;
9962
9963 case OPTION_MIPS1:
e7af610e 9964 mips_opts.isa = ISA_MIPS1;
252b5132
RH
9965 break;
9966
9967 case OPTION_MIPS2:
e7af610e 9968 mips_opts.isa = ISA_MIPS2;
252b5132
RH
9969 break;
9970
9971 case OPTION_MIPS3:
e7af610e 9972 mips_opts.isa = ISA_MIPS3;
252b5132
RH
9973 break;
9974
9975 case OPTION_MIPS4:
e7af610e
NC
9976 mips_opts.isa = ISA_MIPS4;
9977 break;
9978
84ea6cf2
NC
9979 case OPTION_MIPS5:
9980 mips_opts.isa = ISA_MIPS5;
9981 break;
9982
e7af610e
NC
9983 case OPTION_MIPS32:
9984 mips_opts.isa = ISA_MIPS32;
252b5132
RH
9985 break;
9986
84ea6cf2
NC
9987 case OPTION_MIPS64:
9988 mips_opts.isa = ISA_MIPS64;
9989 break;
9990
ec68c924
EC
9991 case OPTION_MTUNE:
9992 case OPTION_MARCH:
252b5132
RH
9993 case OPTION_MCPU:
9994 {
ec68c924
EC
9995 int cpu = CPU_UNKNOWN;
9996
e7af610e 9997 /* Identify the processor type. */
ec68c924 9998 if (strcasecmp (arg, "default") != 0)
252b5132 9999 {
e7af610e 10000 const struct mips_cpu_info *ci;
252b5132 10001
e7af610e
NC
10002 ci = mips_cpu_info_from_name (arg);
10003 if (ci == NULL || ci->is_isa)
ec68c924
EC
10004 {
10005 switch (c)
10006 {
10007 case OPTION_MTUNE:
10008 as_fatal (_("invalid architecture -mtune=%s"), arg);
10009 break;
10010 case OPTION_MARCH:
10011 as_fatal (_("invalid architecture -march=%s"), arg);
10012 break;
10013 case OPTION_MCPU:
10014 as_fatal (_("invalid architecture -mcpu=%s"), arg);
10015 break;
10016 }
10017 }
e7af610e 10018 else
98d3f06f 10019 cpu = ci->cpu;
ec68c924
EC
10020 }
10021
10022 switch (c)
10023 {
10024 case OPTION_MTUNE:
6dce9e24 10025 if (mips_tune != CPU_UNKNOWN && mips_tune != cpu)
98d3f06f
KH
10026 as_warn (_("A different -mtune= was already specified, is now "
10027 "-mtune=%s"), arg);
ec68c924
EC
10028 mips_tune = cpu;
10029 break;
10030 case OPTION_MARCH:
6dce9e24 10031 if (mips_arch != CPU_UNKNOWN && mips_arch != cpu)
98d3f06f
KH
10032 as_warn (_("A different -march= was already specified, is now "
10033 "-march=%s"), arg);
ec68c924
EC
10034 mips_arch = cpu;
10035 break;
10036 case OPTION_MCPU:
6dce9e24 10037 if (mips_cpu != CPU_UNKNOWN && mips_cpu != cpu)
98d3f06f
KH
10038 as_warn (_("A different -mcpu= was already specified, is now "
10039 "-mcpu=%s"), arg);
ec68c924 10040 mips_cpu = cpu;
252b5132
RH
10041 }
10042 }
10043 break;
10044
10045 case OPTION_M4650:
6dce9e24
TS
10046 if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_R4650)
10047 || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_R4650))
98d3f06f
KH
10048 as_warn (_("A different -march= or -mtune= was already specified, "
10049 "is now -m4650"));
ec68c924
EC
10050 mips_arch = CPU_R4650;
10051 mips_tune = CPU_R4650;
252b5132
RH
10052 break;
10053
10054 case OPTION_NO_M4650:
10055 break;
10056
10057 case OPTION_M4010:
6dce9e24
TS
10058 if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_R4010)
10059 || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_R4010))
98d3f06f
KH
10060 as_warn (_("A different -march= or -mtune= was already specified, "
10061 "is now -m4010"));
ec68c924
EC
10062 mips_arch = CPU_R4010;
10063 mips_tune = CPU_R4010;
252b5132
RH
10064 break;
10065
10066 case OPTION_NO_M4010:
10067 break;
10068
10069 case OPTION_M4100:
6dce9e24
TS
10070 if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_VR4100)
10071 || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_VR4100))
98d3f06f
KH
10072 as_warn (_("A different -march= or -mtune= was already specified, "
10073 "is now -m4100"));
ec68c924
EC
10074 mips_arch = CPU_VR4100;
10075 mips_tune = CPU_VR4100;
252b5132
RH
10076 break;
10077
10078 case OPTION_NO_M4100:
10079 break;
10080
252b5132 10081 case OPTION_M3900:
6dce9e24
TS
10082 if ((mips_arch != CPU_UNKNOWN && mips_arch != CPU_R3900)
10083 || (mips_tune != CPU_UNKNOWN && mips_tune != CPU_R3900))
98d3f06f
KH
10084 as_warn (_("A different -march= or -mtune= was already specified, "
10085 "is now -m3900"));
ec68c924
EC
10086 mips_arch = CPU_R3900;
10087 mips_tune = CPU_R3900;
252b5132 10088 break;
bdaaa2e1 10089
252b5132
RH
10090 case OPTION_NO_M3900:
10091 break;
10092
10093 case OPTION_MIPS16:
10094 mips_opts.mips16 = 1;
10095 mips_no_prev_insn (false);
10096 break;
10097
10098 case OPTION_NO_MIPS16:
10099 mips_opts.mips16 = 0;
10100 mips_no_prev_insn (false);
10101 break;
10102
1f25f5d3
CD
10103 case OPTION_MIPS3D:
10104 mips_opts.ase_mips3d = 1;
10105 break;
10106
10107 case OPTION_NO_MIPS3D:
10108 mips_opts.ase_mips3d = 0;
10109 break;
10110
252b5132
RH
10111 case OPTION_MEMBEDDED_PIC:
10112 mips_pic = EMBEDDED_PIC;
10113 if (USE_GLOBAL_POINTER_OPT && g_switch_seen)
10114 {
10115 as_bad (_("-G may not be used with embedded PIC code"));
10116 return 0;
10117 }
10118 g_switch_value = 0x7fffffff;
10119 break;
10120
0f074f60 10121#ifdef OBJ_ELF
252b5132
RH
10122 /* When generating ELF code, we permit -KPIC and -call_shared to
10123 select SVR4_PIC, and -non_shared to select no PIC. This is
10124 intended to be compatible with Irix 5. */
10125 case OPTION_CALL_SHARED:
10126 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10127 {
10128 as_bad (_("-call_shared is supported only for ELF format"));
10129 return 0;
10130 }
10131 mips_pic = SVR4_PIC;
10132 if (g_switch_seen && g_switch_value != 0)
10133 {
10134 as_bad (_("-G may not be used with SVR4 PIC code"));
10135 return 0;
10136 }
10137 g_switch_value = 0;
10138 break;
10139
10140 case OPTION_NON_SHARED:
10141 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10142 {
10143 as_bad (_("-non_shared is supported only for ELF format"));
10144 return 0;
10145 }
10146 mips_pic = NO_PIC;
10147 break;
10148
10149 /* The -xgot option tells the assembler to use 32 offsets when
10150 accessing the got in SVR4_PIC mode. It is for Irix
10151 compatibility. */
10152 case OPTION_XGOT:
10153 mips_big_got = 1;
10154 break;
0f074f60 10155#endif /* OBJ_ELF */
252b5132
RH
10156
10157 case 'G':
10158 if (! USE_GLOBAL_POINTER_OPT)
10159 {
10160 as_bad (_("-G is not supported for this configuration"));
10161 return 0;
10162 }
10163 else if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
10164 {
10165 as_bad (_("-G may not be used with SVR4 or embedded PIC code"));
10166 return 0;
10167 }
10168 else
10169 g_switch_value = atoi (arg);
10170 g_switch_seen = 1;
10171 break;
10172
0f074f60 10173#ifdef OBJ_ELF
34ba82a8
TS
10174 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
10175 and -mabi=64. */
252b5132 10176 case OPTION_32:
34ba82a8
TS
10177 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10178 {
10179 as_bad (_("-32 is supported for ELF format only"));
10180 return 0;
10181 }
a325df1d 10182 mips_opts.abi = O32_ABI;
252b5132
RH
10183 break;
10184
e013f690 10185 case OPTION_N32:
34ba82a8
TS
10186 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10187 {
10188 as_bad (_("-n32 is supported for ELF format only"));
10189 return 0;
10190 }
a325df1d 10191 mips_opts.abi = N32_ABI;
e013f690 10192 break;
252b5132 10193
e013f690 10194 case OPTION_64:
34ba82a8
TS
10195 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10196 {
10197 as_bad (_("-64 is supported for ELF format only"));
10198 return 0;
10199 }
a325df1d 10200 mips_opts.abi = N64_ABI;
e013f690
TS
10201 if (! support_64bit_objects())
10202 as_fatal (_("No compiled in support for 64 bit object file format"));
252b5132 10203 break;
ae948b86 10204#endif /* OBJ_ELF */
252b5132 10205
c97ef257 10206 case OPTION_GP32:
a325df1d
TS
10207 file_mips_gp32 = 1;
10208 if (mips_opts.abi != O32_ABI)
10209 mips_opts.abi = NO_ABI;
c97ef257
AH
10210 break;
10211
10212 case OPTION_GP64:
a325df1d
TS
10213 file_mips_gp32 = 0;
10214 if (mips_opts.abi == O32_ABI)
10215 mips_opts.abi = NO_ABI;
c97ef257 10216 break;
252b5132 10217
ca4e0257 10218 case OPTION_FP32:
a325df1d
TS
10219 file_mips_fp32 = 1;
10220 if (mips_opts.abi != O32_ABI)
10221 mips_opts.abi = NO_ABI;
ca4e0257
RS
10222 break;
10223
ae948b86 10224#ifdef OBJ_ELF
252b5132 10225 case OPTION_MABI:
34ba82a8
TS
10226 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10227 {
10228 as_bad (_("-mabi is supported for ELF format only"));
10229 return 0;
10230 }
e013f690 10231 if (strcmp (arg, "32") == 0)
a325df1d 10232 mips_opts.abi = O32_ABI;
e013f690 10233 else if (strcmp (arg, "o64") == 0)
a325df1d 10234 mips_opts.abi = O64_ABI;
e013f690 10235 else if (strcmp (arg, "n32") == 0)
a325df1d 10236 mips_opts.abi = N32_ABI;
e013f690
TS
10237 else if (strcmp (arg, "64") == 0)
10238 {
a325df1d 10239 mips_opts.abi = N64_ABI;
e013f690
TS
10240 if (! support_64bit_objects())
10241 as_fatal (_("No compiled in support for 64 bit object file "
10242 "format"));
10243 }
10244 else if (strcmp (arg, "eabi") == 0)
a325df1d 10245 mips_opts.abi = EABI_ABI;
e013f690 10246 else
da0e507f
TS
10247 {
10248 as_fatal (_("invalid abi -mabi=%s"), arg);
10249 return 0;
10250 }
252b5132 10251 break;
e013f690 10252#endif /* OBJ_ELF */
252b5132 10253
6b76fefe
CM
10254 case OPTION_M7000_HILO_FIX:
10255 mips_7000_hilo_fix = true;
10256 break;
10257
10258 case OPTION_NO_M7000_HILO_FIX:
10259 mips_7000_hilo_fix = false;
10260 break;
10261
252b5132
RH
10262 default:
10263 return 0;
10264 }
10265
10266 return 1;
10267}
10268
252b5132
RH
10269static void
10270show (stream, string, col_p, first_p)
10271 FILE *stream;
10272 char *string;
10273 int *col_p;
10274 int *first_p;
10275{
10276 if (*first_p)
10277 {
10278 fprintf (stream, "%24s", "");
10279 *col_p = 24;
10280 }
10281 else
10282 {
10283 fprintf (stream, ", ");
10284 *col_p += 2;
10285 }
10286
10287 if (*col_p + strlen (string) > 72)
10288 {
10289 fprintf (stream, "\n%24s", "");
10290 *col_p = 24;
10291 }
10292
10293 fprintf (stream, "%s", string);
10294 *col_p += strlen (string);
10295
10296 *first_p = 0;
10297}
10298
252b5132
RH
10299void
10300md_show_usage (stream)
10301 FILE *stream;
10302{
10303 int column, first;
10304
beae10d5 10305 fprintf (stream, _("\
252b5132
RH
10306MIPS options:\n\
10307-membedded-pic generate embedded position independent code\n\
10308-EB generate big endian output\n\
10309-EL generate little endian output\n\
9a41af64 10310-g, -g2 do not remove unneeded NOPs or swap branches\n\
252b5132
RH
10311-G NUM allow referencing objects up to NUM bytes\n\
10312 implicitly with the gp register [default 8]\n"));
beae10d5 10313 fprintf (stream, _("\
252b5132
RH
10314-mips1 generate MIPS ISA I instructions\n\
10315-mips2 generate MIPS ISA II instructions\n\
10316-mips3 generate MIPS ISA III instructions\n\
10317-mips4 generate MIPS ISA IV instructions\n\
84ea6cf2 10318-mips5 generate MIPS ISA V instructions\n\
e7af610e 10319-mips32 generate MIPS32 ISA instructions\n\
84ea6cf2 10320-mips64 generate MIPS64 ISA instructions\n\
ec68c924 10321-march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
252b5132
RH
10322
10323 first = 1;
10324
10325 show (stream, "2000", &column, &first);
10326 show (stream, "3000", &column, &first);
10327 show (stream, "3900", &column, &first);
10328 show (stream, "4000", &column, &first);
10329 show (stream, "4010", &column, &first);
10330 show (stream, "4100", &column, &first);
10331 show (stream, "4111", &column, &first);
10332 show (stream, "4300", &column, &first);
10333 show (stream, "4400", &column, &first);
10334 show (stream, "4600", &column, &first);
10335 show (stream, "4650", &column, &first);
10336 show (stream, "5000", &column, &first);
18ae5d72
EC
10337 show (stream, "5200", &column, &first);
10338 show (stream, "5230", &column, &first);
10339 show (stream, "5231", &column, &first);
10340 show (stream, "5261", &column, &first);
10341 show (stream, "5721", &column, &first);
252b5132
RH
10342 show (stream, "6000", &column, &first);
10343 show (stream, "8000", &column, &first);
10344 show (stream, "10000", &column, &first);
d1cf510e 10345 show (stream, "12000", &column, &first);
2e4acd24 10346 show (stream, "sb1", &column, &first);
252b5132
RH
10347 fputc ('\n', stream);
10348
10349 fprintf (stream, _("\
ec68c924 10350-mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
252b5132
RH
10351-no-mCPU don't generate code specific to CPU.\n\
10352 For -mCPU and -no-mCPU, CPU must be one of:\n"));
10353
10354 first = 1;
10355
10356 show (stream, "3900", &column, &first);
10357 show (stream, "4010", &column, &first);
10358 show (stream, "4100", &column, &first);
10359 show (stream, "4650", &column, &first);
10360 fputc ('\n', stream);
10361
beae10d5 10362 fprintf (stream, _("\
252b5132
RH
10363-mips16 generate mips16 instructions\n\
10364-no-mips16 do not generate mips16 instructions\n"));
beae10d5 10365 fprintf (stream, _("\
ca4e0257
RS
10366-mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
10367-mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
252b5132
RH
10368-O0 remove unneeded NOPs, do not swap branches\n\
10369-O remove unneeded NOPs and swap branches\n\
63486801 10370-n warn about NOPs generated from macros\n\
119d663a 10371--[no-]construct-floats [dis]allow floating point values to be constructed\n\
252b5132
RH
10372--trap, --no-break trap exception on div by 0 and mult overflow\n\
10373--break, --no-trap break exception on div by 0 and mult overflow\n"));
10374#ifdef OBJ_ELF
beae10d5 10375 fprintf (stream, _("\
252b5132
RH
10376-KPIC, -call_shared generate SVR4 position independent code\n\
10377-non_shared do not generate position independent code\n\
10378-xgot assume a 32 bit GOT\n\
34ba82a8
TS
10379-mabi=ABI create ABI conformant object file for:\n"));
10380
10381 first = 1;
10382
10383 show (stream, "32", &column, &first);
10384 show (stream, "o64", &column, &first);
10385 show (stream, "n32", &column, &first);
10386 show (stream, "64", &column, &first);
10387 show (stream, "eabi", &column, &first);
80cc45a5 10388
34ba82a8
TS
10389 fputc ('\n', stream);
10390
10391 fprintf (stream, _("\
e013f690
TS
10392-32 create o32 ABI object file (default)\n\
10393-n32 create n32 ABI object file\n\
10394-64 create 64 ABI object file\n"));
252b5132
RH
10395#endif
10396}
10397\f
10398void
10399mips_init_after_args ()
10400{
10401 /* initialize opcodes */
10402 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
beae10d5 10403 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
252b5132
RH
10404}
10405
10406long
10407md_pcrel_from (fixP)
10408 fixS *fixP;
10409{
10410 if (OUTPUT_FLAVOR != bfd_target_aout_flavour
10411 && fixP->fx_addsy != (symbolS *) NULL
10412 && ! S_IS_DEFINED (fixP->fx_addsy))
10413 {
6478892d
TS
10414 /* This makes a branch to an undefined symbol be a branch to the
10415 current location. */
cb56d3d3 10416 if (mips_pic == EMBEDDED_PIC)
6478892d 10417 return 4;
cb56d3d3 10418 else
6478892d 10419 return 1;
252b5132
RH
10420 }
10421
c9914766 10422 /* Return the address of the delay slot. */
252b5132
RH
10423 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
10424}
10425
252b5132
RH
10426/* This is called before the symbol table is processed. In order to
10427 work with gcc when using mips-tfile, we must keep all local labels.
10428 However, in other cases, we want to discard them. If we were
10429 called with -g, but we didn't see any debugging information, it may
10430 mean that gcc is smuggling debugging information through to
10431 mips-tfile, in which case we must generate all local labels. */
10432
10433void
10434mips_frob_file_before_adjust ()
10435{
10436#ifndef NO_ECOFF_DEBUGGING
10437 if (ECOFF_DEBUGGING
10438 && mips_debug != 0
10439 && ! ecoff_debugging_seen)
10440 flag_keep_locals = 1;
10441#endif
10442}
10443
10444/* Sort any unmatched HI16_S relocs so that they immediately precede
94f592af 10445 the corresponding LO reloc. This is called before md_apply_fix3 and
252b5132
RH
10446 tc_gen_reloc. Unmatched HI16_S relocs can only be generated by
10447 explicit use of the %hi modifier. */
10448
10449void
10450mips_frob_file ()
10451{
10452 struct mips_hi_fixup *l;
10453
10454 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
10455 {
10456 segment_info_type *seginfo;
10457 int pass;
10458
10459 assert (l->fixp->fx_r_type == BFD_RELOC_HI16_S);
10460
10461 /* Check quickly whether the next fixup happens to be a matching
10462 %lo. */
10463 if (l->fixp->fx_next != NULL
10464 && l->fixp->fx_next->fx_r_type == BFD_RELOC_LO16
10465 && l->fixp->fx_addsy == l->fixp->fx_next->fx_addsy
10466 && l->fixp->fx_offset == l->fixp->fx_next->fx_offset)
10467 continue;
10468
10469 /* Look through the fixups for this segment for a matching %lo.
10470 When we find one, move the %hi just in front of it. We do
10471 this in two passes. In the first pass, we try to find a
10472 unique %lo. In the second pass, we permit multiple %hi
10473 relocs for a single %lo (this is a GNU extension). */
10474 seginfo = seg_info (l->seg);
10475 for (pass = 0; pass < 2; pass++)
10476 {
10477 fixS *f, *prev;
10478
10479 prev = NULL;
10480 for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
10481 {
10482 /* Check whether this is a %lo fixup which matches l->fixp. */
10483 if (f->fx_r_type == BFD_RELOC_LO16
10484 && f->fx_addsy == l->fixp->fx_addsy
10485 && f->fx_offset == l->fixp->fx_offset
10486 && (pass == 1
10487 || prev == NULL
10488 || prev->fx_r_type != BFD_RELOC_HI16_S
10489 || prev->fx_addsy != f->fx_addsy
10490 || prev->fx_offset != f->fx_offset))
10491 {
10492 fixS **pf;
10493
10494 /* Move l->fixp before f. */
10495 for (pf = &seginfo->fix_root;
10496 *pf != l->fixp;
10497 pf = &(*pf)->fx_next)
10498 assert (*pf != NULL);
10499
10500 *pf = l->fixp->fx_next;
10501
10502 l->fixp->fx_next = f;
10503 if (prev == NULL)
10504 seginfo->fix_root = l->fixp;
10505 else
10506 prev->fx_next = l->fixp;
10507
10508 break;
10509 }
10510
10511 prev = f;
10512 }
10513
10514 if (f != NULL)
10515 break;
10516
10517#if 0 /* GCC code motion plus incomplete dead code elimination
10518 can leave a %hi without a %lo. */
10519 if (pass == 1)
10520 as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
10521 _("Unmatched %%hi reloc"));
10522#endif
10523 }
10524 }
10525}
10526
10527/* When generating embedded PIC code we need to use a special
10528 relocation to represent the difference of two symbols in the .text
10529 section (switch tables use a difference of this sort). See
10530 include/coff/mips.h for details. This macro checks whether this
10531 fixup requires the special reloc. */
10532#define SWITCH_TABLE(fixp) \
10533 ((fixp)->fx_r_type == BFD_RELOC_32 \
bb2d6cd7 10534 && OUTPUT_FLAVOR != bfd_target_elf_flavour \
252b5132
RH
10535 && (fixp)->fx_addsy != NULL \
10536 && (fixp)->fx_subsy != NULL \
10537 && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
10538 && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
10539
10540/* When generating embedded PIC code we must keep all PC relative
10541 relocations, in case the linker has to relax a call. We also need
f6688943
TS
10542 to keep relocations for switch table entries.
10543
10544 We may have combined relocations without symbols in the N32/N64 ABI.
10545 We have to prevent gas from dropping them. */
252b5132 10546
252b5132
RH
10547int
10548mips_force_relocation (fixp)
10549 fixS *fixp;
10550{
10551 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
10552 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
10553 return 1;
10554
f6688943
TS
10555 if (HAVE_NEWABI
10556 && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
10557 && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
10558 || fixp->fx_r_type == BFD_RELOC_HI16_S
10559 || fixp->fx_r_type == BFD_RELOC_LO16))
10560 return 1;
10561
252b5132
RH
10562 return (mips_pic == EMBEDDED_PIC
10563 && (fixp->fx_pcrel
10564 || SWITCH_TABLE (fixp)
10565 || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
10566 || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
10567}
10568
add55e1f
RS
10569#ifdef OBJ_ELF
10570static int
10571mips_need_elf_addend_fixup (fixP)
10572 fixS *fixP;
10573{
2d2bf3e0
CD
10574 if (S_GET_OTHER (fixP->fx_addsy) == STO_MIPS16)
10575 return 1;
b25a253c
CD
10576 if (mips_pic == EMBEDDED_PIC
10577 && S_IS_WEAK (fixP->fx_addsy))
10578 return 1;
10579 if (mips_pic != EMBEDDED_PIC
10580 && (S_IS_WEAK (fixP->fx_addsy)
10581 || S_IS_EXTERN (fixP->fx_addsy))
2d2bf3e0
CD
10582 && !S_IS_COMMON (fixP->fx_addsy))
10583 return 1;
10584 if (symbol_used_in_reloc_p (fixP->fx_addsy)
10585 && (((bfd_get_section_flags (stdoutput,
10586 S_GET_SEGMENT (fixP->fx_addsy))
10587 & SEC_LINK_ONCE) != 0)
10588 || !strncmp (segment_name (S_GET_SEGMENT (fixP->fx_addsy)),
10589 ".gnu.linkonce",
10590 sizeof (".gnu.linkonce") - 1)))
10591 return 1;
10592 return 0;
add55e1f
RS
10593}
10594#endif
10595
252b5132
RH
10596/* Apply a fixup to the object file. */
10597
94f592af
NC
10598void
10599md_apply_fix3 (fixP, valP, seg)
252b5132 10600 fixS *fixP;
98d3f06f 10601 valueT *valP;
94f592af 10602 segT seg ATTRIBUTE_UNUSED;
252b5132 10603{
874e8986 10604 bfd_byte *buf;
98aa84af
AM
10605 long insn;
10606 valueT value;
252b5132
RH
10607
10608 assert (fixP->fx_size == 4
10609 || fixP->fx_r_type == BFD_RELOC_16
f6688943
TS
10610 || fixP->fx_r_type == BFD_RELOC_32
10611 || fixP->fx_r_type == BFD_RELOC_MIPS_JMP
10612 || fixP->fx_r_type == BFD_RELOC_HI16_S
10613 || fixP->fx_r_type == BFD_RELOC_LO16
10614 || fixP->fx_r_type == BFD_RELOC_GPREL16
76b3015f 10615 || fixP->fx_r_type == BFD_RELOC_MIPS_LITERAL
f6688943 10616 || fixP->fx_r_type == BFD_RELOC_GPREL32
252b5132 10617 || fixP->fx_r_type == BFD_RELOC_64
f6688943
TS
10618 || fixP->fx_r_type == BFD_RELOC_CTOR
10619 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
10620 || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHEST
10621 || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHER
10622 || fixP->fx_r_type == BFD_RELOC_MIPS_SCN_DISP
10623 || fixP->fx_r_type == BFD_RELOC_MIPS_REL16
10624 || fixP->fx_r_type == BFD_RELOC_MIPS_RELGOT
252b5132
RH
10625 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
10626 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY);
10627
98d3f06f 10628 value = *valP;
252b5132
RH
10629
10630 /* If we aren't adjusting this fixup to be against the section
10631 symbol, we need to adjust the value. */
10632#ifdef OBJ_ELF
10633 if (fixP->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour)
bb2d6cd7 10634 {
add55e1f 10635 if (mips_need_elf_addend_fixup (fixP))
98aa84af
AM
10636 {
10637 valueT symval = S_GET_VALUE (fixP->fx_addsy);
94f592af 10638
98aa84af 10639 value -= symval;
add55e1f 10640 if (value != 0 && ! fixP->fx_pcrel)
98aa84af
AM
10641 {
10642 /* In this case, the bfd_install_relocation routine will
10643 incorrectly add the symbol value back in. We just want
7461da6e 10644 the addend to appear in the object file. */
98aa84af 10645 value -= symval;
7461da6e
RS
10646
10647 /* Make sure the addend is still non-zero. If it became zero
10648 after the last operation, set it to a spurious value and
10649 subtract the same value from the object file's contents. */
10650 if (value == 0)
10651 {
10652 value = 8;
10653
10654 /* The in-place addends for LO16 relocations are signed;
10655 leave the matching HI16 in-place addends as zero. */
10656 if (fixP->fx_r_type != BFD_RELOC_HI16_S)
10657 {
10658 reloc_howto_type *howto;
10659 bfd_vma contents, mask, field;
10660
10661 howto = bfd_reloc_type_lookup (stdoutput,
10662 fixP->fx_r_type);
10663
10664 contents = bfd_get_bits (fixP->fx_frag->fr_literal
10665 + fixP->fx_where,
10666 fixP->fx_size * 8,
10667 target_big_endian);
10668
10669 /* MASK has bits set where the relocation should go.
10670 FIELD is -value, shifted into the appropriate place
10671 for this relocation. */
10672 mask = 1 << (howto->bitsize - 1);
10673 mask = (((mask - 1) << 1) | 1) << howto->bitpos;
10674 field = (-value >> howto->rightshift) << howto->bitpos;
10675
10676 bfd_put_bits ((field & mask) | (contents & ~mask),
10677 fixP->fx_frag->fr_literal + fixP->fx_where,
10678 fixP->fx_size * 8,
10679 target_big_endian);
10680 }
10681 }
98aa84af
AM
10682 }
10683 }
252b5132 10684
bb2d6cd7
GK
10685 /* This code was generated using trial and error and so is
10686 fragile and not trustworthy. If you change it, you should
10687 rerun the elf-rel, elf-rel2, and empic testcases and ensure
10688 they still pass. */
10689 if (fixP->fx_pcrel || fixP->fx_subsy != NULL)
10690 {
10691 value += fixP->fx_frag->fr_address + fixP->fx_where;
10692
10693 /* BFD's REL handling, for MIPS, is _very_ weird.
10694 This gives the right results, but it can't possibly
10695 be the way things are supposed to work. */
cb56d3d3
TS
10696 if ((fixP->fx_r_type != BFD_RELOC_16_PCREL
10697 && fixP->fx_r_type != BFD_RELOC_16_PCREL_S2)
bb2d6cd7
GK
10698 || S_GET_SEGMENT (fixP->fx_addsy) != undefined_section)
10699 value += fixP->fx_frag->fr_address + fixP->fx_where;
10700 }
10701 }
10702#endif
252b5132 10703
94f592af 10704 fixP->fx_addnumber = value; /* Remember value for tc_gen_reloc. */
252b5132
RH
10705
10706 if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel)
10707 fixP->fx_done = 1;
10708
10709 switch (fixP->fx_r_type)
10710 {
10711 case BFD_RELOC_MIPS_JMP:
e369bcce
TS
10712 case BFD_RELOC_MIPS_SHIFT5:
10713 case BFD_RELOC_MIPS_SHIFT6:
10714 case BFD_RELOC_MIPS_GOT_DISP:
10715 case BFD_RELOC_MIPS_GOT_PAGE:
10716 case BFD_RELOC_MIPS_GOT_OFST:
10717 case BFD_RELOC_MIPS_SUB:
10718 case BFD_RELOC_MIPS_INSERT_A:
10719 case BFD_RELOC_MIPS_INSERT_B:
10720 case BFD_RELOC_MIPS_DELETE:
10721 case BFD_RELOC_MIPS_HIGHEST:
10722 case BFD_RELOC_MIPS_HIGHER:
10723 case BFD_RELOC_MIPS_SCN_DISP:
10724 case BFD_RELOC_MIPS_REL16:
10725 case BFD_RELOC_MIPS_RELGOT:
10726 case BFD_RELOC_MIPS_JALR:
252b5132
RH
10727 case BFD_RELOC_HI16:
10728 case BFD_RELOC_HI16_S:
cdf6fd85 10729 case BFD_RELOC_GPREL16:
252b5132
RH
10730 case BFD_RELOC_MIPS_LITERAL:
10731 case BFD_RELOC_MIPS_CALL16:
10732 case BFD_RELOC_MIPS_GOT16:
cdf6fd85 10733 case BFD_RELOC_GPREL32:
252b5132
RH
10734 case BFD_RELOC_MIPS_GOT_HI16:
10735 case BFD_RELOC_MIPS_GOT_LO16:
10736 case BFD_RELOC_MIPS_CALL_HI16:
10737 case BFD_RELOC_MIPS_CALL_LO16:
10738 case BFD_RELOC_MIPS16_GPREL:
10739 if (fixP->fx_pcrel)
10740 as_bad_where (fixP->fx_file, fixP->fx_line,
10741 _("Invalid PC relative reloc"));
10742 /* Nothing needed to do. The value comes from the reloc entry */
10743 break;
10744
10745 case BFD_RELOC_MIPS16_JMP:
10746 /* We currently always generate a reloc against a symbol, which
10747 means that we don't want an addend even if the symbol is
10748 defined. */
10749 fixP->fx_addnumber = 0;
10750 break;
10751
10752 case BFD_RELOC_PCREL_HI16_S:
10753 /* The addend for this is tricky if it is internal, so we just
10754 do everything here rather than in bfd_install_relocation. */
bdaaa2e1 10755 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
bb2d6cd7
GK
10756 && !fixP->fx_done
10757 && value != 0)
10758 break;
10759 if (fixP->fx_addsy
10760 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
252b5132
RH
10761 {
10762 /* For an external symbol adjust by the address to make it
10763 pcrel_offset. We use the address of the RELLO reloc
10764 which follows this one. */
10765 value += (fixP->fx_next->fx_frag->fr_address
10766 + fixP->fx_next->fx_where);
10767 }
e7d556df 10768 value = ((value + 0x8000) >> 16) & 0xffff;
874e8986 10769 buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
252b5132
RH
10770 if (target_big_endian)
10771 buf += 2;
874e8986 10772 md_number_to_chars ((char *) buf, value, 2);
252b5132
RH
10773 break;
10774
10775 case BFD_RELOC_PCREL_LO16:
10776 /* The addend for this is tricky if it is internal, so we just
10777 do everything here rather than in bfd_install_relocation. */
bdaaa2e1 10778 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
bb2d6cd7
GK
10779 && !fixP->fx_done
10780 && value != 0)
10781 break;
10782 if (fixP->fx_addsy
10783 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
252b5132 10784 value += fixP->fx_frag->fr_address + fixP->fx_where;
874e8986 10785 buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
252b5132
RH
10786 if (target_big_endian)
10787 buf += 2;
874e8986 10788 md_number_to_chars ((char *) buf, value, 2);
252b5132
RH
10789 break;
10790
10791 case BFD_RELOC_64:
10792 /* This is handled like BFD_RELOC_32, but we output a sign
10793 extended value if we are only 32 bits. */
10794 if (fixP->fx_done
10795 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
10796 {
10797 if (8 <= sizeof (valueT))
10798 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
10799 value, 8);
10800 else
10801 {
10802 long w1, w2;
10803 long hiv;
10804
10805 w1 = w2 = fixP->fx_where;
10806 if (target_big_endian)
10807 w1 += 4;
10808 else
10809 w2 += 4;
10810 md_number_to_chars (fixP->fx_frag->fr_literal + w1, value, 4);
10811 if ((value & 0x80000000) != 0)
10812 hiv = 0xffffffff;
10813 else
10814 hiv = 0;
10815 md_number_to_chars (fixP->fx_frag->fr_literal + w2, hiv, 4);
10816 }
10817 }
10818 break;
10819
056350c6 10820 case BFD_RELOC_RVA:
252b5132
RH
10821 case BFD_RELOC_32:
10822 /* If we are deleting this reloc entry, we must fill in the
10823 value now. This can happen if we have a .word which is not
10824 resolved when it appears but is later defined. We also need
10825 to fill in the value if this is an embedded PIC switch table
10826 entry. */
10827 if (fixP->fx_done
10828 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
10829 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
10830 value, 4);
10831 break;
10832
10833 case BFD_RELOC_16:
10834 /* If we are deleting this reloc entry, we must fill in the
10835 value now. */
10836 assert (fixP->fx_size == 2);
10837 if (fixP->fx_done)
10838 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
10839 value, 2);
10840 break;
10841
10842 case BFD_RELOC_LO16:
10843 /* When handling an embedded PIC switch statement, we can wind
10844 up deleting a LO16 reloc. See the 'o' case in mips_ip. */
10845 if (fixP->fx_done)
10846 {
98aa84af 10847 if (value + 0x8000 > 0xffff)
252b5132
RH
10848 as_bad_where (fixP->fx_file, fixP->fx_line,
10849 _("relocation overflow"));
874e8986 10850 buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
252b5132
RH
10851 if (target_big_endian)
10852 buf += 2;
874e8986 10853 md_number_to_chars ((char *) buf, value, 2);
252b5132
RH
10854 }
10855 break;
10856
10857 case BFD_RELOC_16_PCREL_S2:
cb56d3d3
TS
10858 if ((value & 0x3) != 0)
10859 as_bad_where (fixP->fx_file, fixP->fx_line,
10860 _("Branch to odd address (%lx)"), (long) value);
10861
10862 /* Fall through. */
10863
10864 case BFD_RELOC_16_PCREL:
252b5132
RH
10865 /*
10866 * We need to save the bits in the instruction since fixup_segment()
10867 * might be deleting the relocation entry (i.e., a branch within
10868 * the current segment).
10869 */
bb2d6cd7
GK
10870 if (!fixP->fx_done && value != 0)
10871 break;
10872 /* If 'value' is zero, the remaining reloc code won't actually
10873 do the store, so it must be done here. This is probably
10874 a bug somewhere. */
b25a253c
CD
10875 if (!fixP->fx_done
10876 && (fixP->fx_r_type != BFD_RELOC_16_PCREL_S2
10877 || fixP->fx_addsy == NULL /* ??? */
10878 || ! S_IS_DEFINED (fixP->fx_addsy)))
bb2d6cd7 10879 value -= fixP->fx_frag->fr_address + fixP->fx_where;
bdaaa2e1 10880
98aa84af 10881 value = (offsetT) value >> 2;
252b5132
RH
10882
10883 /* update old instruction data */
874e8986 10884 buf = (bfd_byte *) (fixP->fx_where + fixP->fx_frag->fr_literal);
252b5132
RH
10885 if (target_big_endian)
10886 insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
10887 else
10888 insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
10889
98aa84af 10890 if (value + 0x8000 <= 0xffff)
252b5132
RH
10891 insn |= value & 0xffff;
10892 else
10893 {
10894 /* The branch offset is too large. If this is an
10895 unconditional branch, and we are not generating PIC code,
10896 we can convert it to an absolute jump instruction. */
10897 if (mips_pic == NO_PIC
10898 && fixP->fx_done
10899 && fixP->fx_frag->fr_address >= text_section->vma
10900 && (fixP->fx_frag->fr_address
10901 < text_section->vma + text_section->_raw_size)
10902 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
10903 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
10904 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
10905 {
10906 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
10907 insn = 0x0c000000; /* jal */
10908 else
10909 insn = 0x08000000; /* j */
10910 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
10911 fixP->fx_done = 0;
10912 fixP->fx_addsy = section_symbol (text_section);
10913 fixP->fx_addnumber = (value << 2) + md_pcrel_from (fixP);
10914 }
10915 else
10916 {
10917 /* FIXME. It would be possible in principle to handle
10918 conditional branches which overflow. They could be
10919 transformed into a branch around a jump. This would
10920 require setting up variant frags for each different
10921 branch type. The native MIPS assembler attempts to
10922 handle these cases, but it appears to do it
10923 incorrectly. */
10924 as_bad_where (fixP->fx_file, fixP->fx_line,
10925 _("Branch out of range"));
10926 }
10927 }
10928
10929 md_number_to_chars ((char *) buf, (valueT) insn, 4);
10930 break;
10931
10932 case BFD_RELOC_VTABLE_INHERIT:
10933 fixP->fx_done = 0;
10934 if (fixP->fx_addsy
10935 && !S_IS_DEFINED (fixP->fx_addsy)
10936 && !S_IS_WEAK (fixP->fx_addsy))
10937 S_SET_WEAK (fixP->fx_addsy);
10938 break;
10939
10940 case BFD_RELOC_VTABLE_ENTRY:
10941 fixP->fx_done = 0;
10942 break;
10943
10944 default:
10945 internalError ();
10946 }
252b5132
RH
10947}
10948
10949#if 0
10950void
10951printInsn (oc)
10952 unsigned long oc;
10953{
10954 const struct mips_opcode *p;
10955 int treg, sreg, dreg, shamt;
10956 short imm;
10957 const char *args;
10958 int i;
10959
10960 for (i = 0; i < NUMOPCODES; ++i)
10961 {
10962 p = &mips_opcodes[i];
10963 if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
10964 {
10965 printf ("%08lx %s\t", oc, p->name);
10966 treg = (oc >> 16) & 0x1f;
10967 sreg = (oc >> 21) & 0x1f;
10968 dreg = (oc >> 11) & 0x1f;
10969 shamt = (oc >> 6) & 0x1f;
10970 imm = oc;
10971 for (args = p->args;; ++args)
10972 {
10973 switch (*args)
10974 {
10975 case '\0':
10976 printf ("\n");
10977 break;
10978
10979 case ',':
10980 case '(':
10981 case ')':
10982 printf ("%c", *args);
10983 continue;
10984
10985 case 'r':
10986 assert (treg == sreg);
10987 printf ("$%d,$%d", treg, sreg);
10988 continue;
10989
10990 case 'd':
10991 case 'G':
10992 printf ("$%d", dreg);
10993 continue;
10994
10995 case 't':
10996 case 'E':
10997 printf ("$%d", treg);
10998 continue;
10999
11000 case 'k':
11001 printf ("0x%x", treg);
11002 continue;
11003
11004 case 'b':
11005 case 's':
11006 printf ("$%d", sreg);
11007 continue;
11008
11009 case 'a':
11010 printf ("0x%08lx", oc & 0x1ffffff);
11011 continue;
11012
11013 case 'i':
11014 case 'j':
11015 case 'o':
11016 case 'u':
11017 printf ("%d", imm);
11018 continue;
11019
11020 case '<':
11021 case '>':
11022 printf ("$%d", shamt);
11023 continue;
11024
11025 default:
11026 internalError ();
11027 }
11028 break;
11029 }
11030 return;
11031 }
11032 }
11033 printf (_("%08lx UNDEFINED\n"), oc);
11034}
11035#endif
11036
11037static symbolS *
11038get_symbol ()
11039{
11040 int c;
11041 char *name;
11042 symbolS *p;
11043
11044 name = input_line_pointer;
11045 c = get_symbol_end ();
11046 p = (symbolS *) symbol_find_or_make (name);
11047 *input_line_pointer = c;
11048 return p;
11049}
11050
11051/* Align the current frag to a given power of two. The MIPS assembler
11052 also automatically adjusts any preceding label. */
11053
11054static void
11055mips_align (to, fill, label)
11056 int to;
11057 int fill;
11058 symbolS *label;
11059{
11060 mips_emit_delays (false);
11061 frag_align (to, fill, 0);
11062 record_alignment (now_seg, to);
11063 if (label != NULL)
11064 {
11065 assert (S_GET_SEGMENT (label) == now_seg);
49309057 11066 symbol_set_frag (label, frag_now);
252b5132
RH
11067 S_SET_VALUE (label, (valueT) frag_now_fix ());
11068 }
11069}
11070
11071/* Align to a given power of two. .align 0 turns off the automatic
11072 alignment used by the data creating pseudo-ops. */
11073
11074static void
11075s_align (x)
43841e91 11076 int x ATTRIBUTE_UNUSED;
252b5132
RH
11077{
11078 register int temp;
11079 register long temp_fill;
11080 long max_alignment = 15;
11081
11082 /*
11083
11084 o Note that the assembler pulls down any immediately preceeding label
11085 to the aligned address.
11086 o It's not documented but auto alignment is reinstated by
11087 a .align pseudo instruction.
11088 o Note also that after auto alignment is turned off the mips assembler
11089 issues an error on attempt to assemble an improperly aligned data item.
11090 We don't.
11091
11092 */
11093
11094 temp = get_absolute_expression ();
11095 if (temp > max_alignment)
11096 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
11097 else if (temp < 0)
11098 {
11099 as_warn (_("Alignment negative: 0 assumed."));
11100 temp = 0;
11101 }
11102 if (*input_line_pointer == ',')
11103 {
f9419b05 11104 ++input_line_pointer;
252b5132
RH
11105 temp_fill = get_absolute_expression ();
11106 }
11107 else
11108 temp_fill = 0;
11109 if (temp)
11110 {
11111 auto_align = 1;
11112 mips_align (temp, (int) temp_fill,
11113 insn_labels != NULL ? insn_labels->label : NULL);
11114 }
11115 else
11116 {
11117 auto_align = 0;
11118 }
11119
11120 demand_empty_rest_of_line ();
11121}
11122
11123void
11124mips_flush_pending_output ()
11125{
11126 mips_emit_delays (false);
11127 mips_clear_insn_labels ();
11128}
11129
11130static void
11131s_change_sec (sec)
11132 int sec;
11133{
11134 segT seg;
11135
11136 /* When generating embedded PIC code, we only use the .text, .lit8,
11137 .sdata and .sbss sections. We change the .data and .rdata
11138 pseudo-ops to use .sdata. */
11139 if (mips_pic == EMBEDDED_PIC
11140 && (sec == 'd' || sec == 'r'))
11141 sec = 's';
11142
11143#ifdef OBJ_ELF
11144 /* The ELF backend needs to know that we are changing sections, so
11145 that .previous works correctly. We could do something like check
b6ff326e 11146 for an obj_section_change_hook macro, but that might be confusing
252b5132
RH
11147 as it would not be appropriate to use it in the section changing
11148 functions in read.c, since obj-elf.c intercepts those. FIXME:
11149 This should be cleaner, somehow. */
11150 obj_elf_section_change_hook ();
11151#endif
11152
11153 mips_emit_delays (false);
11154 switch (sec)
11155 {
11156 case 't':
11157 s_text (0);
11158 break;
11159 case 'd':
11160 s_data (0);
11161 break;
11162 case 'b':
11163 subseg_set (bss_section, (subsegT) get_absolute_expression ());
11164 demand_empty_rest_of_line ();
11165 break;
11166
11167 case 'r':
11168 if (USE_GLOBAL_POINTER_OPT)
11169 {
11170 seg = subseg_new (RDATA_SECTION_NAME,
11171 (subsegT) get_absolute_expression ());
11172 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11173 {
11174 bfd_set_section_flags (stdoutput, seg,
11175 (SEC_ALLOC
11176 | SEC_LOAD
11177 | SEC_READONLY
11178 | SEC_RELOC
11179 | SEC_DATA));
11180 if (strcmp (TARGET_OS, "elf") != 0)
e799a695 11181 record_alignment (seg, 4);
252b5132
RH
11182 }
11183 demand_empty_rest_of_line ();
11184 }
11185 else
11186 {
11187 as_bad (_("No read only data section in this object file format"));
11188 demand_empty_rest_of_line ();
11189 return;
11190 }
11191 break;
11192
11193 case 's':
11194 if (USE_GLOBAL_POINTER_OPT)
11195 {
11196 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
11197 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11198 {
11199 bfd_set_section_flags (stdoutput, seg,
11200 SEC_ALLOC | SEC_LOAD | SEC_RELOC
11201 | SEC_DATA);
11202 if (strcmp (TARGET_OS, "elf") != 0)
e799a695 11203 record_alignment (seg, 4);
252b5132
RH
11204 }
11205 demand_empty_rest_of_line ();
11206 break;
11207 }
11208 else
11209 {
11210 as_bad (_("Global pointers not supported; recompile -G 0"));
11211 demand_empty_rest_of_line ();
11212 return;
11213 }
11214 }
11215
11216 auto_align = 1;
11217}
11218
11219void
11220mips_enable_auto_align ()
11221{
11222 auto_align = 1;
11223}
11224
11225static void
11226s_cons (log_size)
11227 int log_size;
11228{
11229 symbolS *label;
11230
11231 label = insn_labels != NULL ? insn_labels->label : NULL;
11232 mips_emit_delays (false);
11233 if (log_size > 0 && auto_align)
11234 mips_align (log_size, 0, label);
11235 mips_clear_insn_labels ();
11236 cons (1 << log_size);
11237}
11238
11239static void
11240s_float_cons (type)
11241 int type;
11242{
11243 symbolS *label;
11244
11245 label = insn_labels != NULL ? insn_labels->label : NULL;
11246
11247 mips_emit_delays (false);
11248
11249 if (auto_align)
49309057
ILT
11250 {
11251 if (type == 'd')
11252 mips_align (3, 0, label);
11253 else
11254 mips_align (2, 0, label);
11255 }
252b5132
RH
11256
11257 mips_clear_insn_labels ();
11258
11259 float_cons (type);
11260}
11261
11262/* Handle .globl. We need to override it because on Irix 5 you are
11263 permitted to say
11264 .globl foo .text
11265 where foo is an undefined symbol, to mean that foo should be
11266 considered to be the address of a function. */
11267
11268static void
11269s_mips_globl (x)
43841e91 11270 int x ATTRIBUTE_UNUSED;
252b5132
RH
11271{
11272 char *name;
11273 int c;
11274 symbolS *symbolP;
11275 flagword flag;
11276
11277 name = input_line_pointer;
11278 c = get_symbol_end ();
11279 symbolP = symbol_find_or_make (name);
11280 *input_line_pointer = c;
11281 SKIP_WHITESPACE ();
11282
11283 /* On Irix 5, every global symbol that is not explicitly labelled as
11284 being a function is apparently labelled as being an object. */
11285 flag = BSF_OBJECT;
11286
11287 if (! is_end_of_line[(unsigned char) *input_line_pointer])
11288 {
11289 char *secname;
11290 asection *sec;
11291
11292 secname = input_line_pointer;
11293 c = get_symbol_end ();
11294 sec = bfd_get_section_by_name (stdoutput, secname);
11295 if (sec == NULL)
11296 as_bad (_("%s: no such section"), secname);
11297 *input_line_pointer = c;
11298
11299 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
11300 flag = BSF_FUNCTION;
11301 }
11302
49309057 11303 symbol_get_bfdsym (symbolP)->flags |= flag;
252b5132
RH
11304
11305 S_SET_EXTERNAL (symbolP);
11306 demand_empty_rest_of_line ();
11307}
11308
11309static void
11310s_option (x)
43841e91 11311 int x ATTRIBUTE_UNUSED;
252b5132
RH
11312{
11313 char *opt;
11314 char c;
11315
11316 opt = input_line_pointer;
11317 c = get_symbol_end ();
11318
11319 if (*opt == 'O')
11320 {
11321 /* FIXME: What does this mean? */
11322 }
11323 else if (strncmp (opt, "pic", 3) == 0)
11324 {
11325 int i;
11326
11327 i = atoi (opt + 3);
11328 if (i == 0)
11329 mips_pic = NO_PIC;
11330 else if (i == 2)
11331 mips_pic = SVR4_PIC;
11332 else
11333 as_bad (_(".option pic%d not supported"), i);
11334
11335 if (USE_GLOBAL_POINTER_OPT && mips_pic == SVR4_PIC)
11336 {
11337 if (g_switch_seen && g_switch_value != 0)
11338 as_warn (_("-G may not be used with SVR4 PIC code"));
11339 g_switch_value = 0;
11340 bfd_set_gp_size (stdoutput, 0);
11341 }
11342 }
11343 else
11344 as_warn (_("Unrecognized option \"%s\""), opt);
11345
11346 *input_line_pointer = c;
11347 demand_empty_rest_of_line ();
11348}
11349
11350/* This structure is used to hold a stack of .set values. */
11351
e972090a
NC
11352struct mips_option_stack
11353{
252b5132
RH
11354 struct mips_option_stack *next;
11355 struct mips_set_options options;
11356};
11357
11358static struct mips_option_stack *mips_opts_stack;
11359
11360/* Handle the .set pseudo-op. */
11361
11362static void
11363s_mipsset (x)
43841e91 11364 int x ATTRIBUTE_UNUSED;
252b5132
RH
11365{
11366 char *name = input_line_pointer, ch;
11367
11368 while (!is_end_of_line[(unsigned char) *input_line_pointer])
f9419b05 11369 ++input_line_pointer;
252b5132
RH
11370 ch = *input_line_pointer;
11371 *input_line_pointer = '\0';
11372
11373 if (strcmp (name, "reorder") == 0)
11374 {
11375 if (mips_opts.noreorder && prev_nop_frag != NULL)
11376 {
11377 /* If we still have pending nops, we can discard them. The
11378 usual nop handling will insert any that are still
bdaaa2e1 11379 needed. */
252b5132
RH
11380 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11381 * (mips_opts.mips16 ? 2 : 4));
11382 prev_nop_frag = NULL;
11383 }
11384 mips_opts.noreorder = 0;
11385 }
11386 else if (strcmp (name, "noreorder") == 0)
11387 {
11388 mips_emit_delays (true);
11389 mips_opts.noreorder = 1;
11390 mips_any_noreorder = 1;
11391 }
11392 else if (strcmp (name, "at") == 0)
11393 {
11394 mips_opts.noat = 0;
11395 }
11396 else if (strcmp (name, "noat") == 0)
11397 {
11398 mips_opts.noat = 1;
11399 }
11400 else if (strcmp (name, "macro") == 0)
11401 {
11402 mips_opts.warn_about_macros = 0;
11403 }
11404 else if (strcmp (name, "nomacro") == 0)
11405 {
11406 if (mips_opts.noreorder == 0)
11407 as_bad (_("`noreorder' must be set before `nomacro'"));
11408 mips_opts.warn_about_macros = 1;
11409 }
11410 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
11411 {
11412 mips_opts.nomove = 0;
11413 }
11414 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
11415 {
11416 mips_opts.nomove = 1;
11417 }
11418 else if (strcmp (name, "bopt") == 0)
11419 {
11420 mips_opts.nobopt = 0;
11421 }
11422 else if (strcmp (name, "nobopt") == 0)
11423 {
11424 mips_opts.nobopt = 1;
11425 }
11426 else if (strcmp (name, "mips16") == 0
11427 || strcmp (name, "MIPS-16") == 0)
11428 mips_opts.mips16 = 1;
11429 else if (strcmp (name, "nomips16") == 0
11430 || strcmp (name, "noMIPS-16") == 0)
11431 mips_opts.mips16 = 0;
1f25f5d3
CD
11432 else if (strcmp (name, "mips3d") == 0)
11433 mips_opts.ase_mips3d = 1;
11434 else if (strcmp (name, "nomips3d") == 0)
11435 mips_opts.ase_mips3d = 0;
252b5132
RH
11436 else if (strncmp (name, "mips", 4) == 0)
11437 {
11438 int isa;
11439
11440 /* Permit the user to change the ISA on the fly. Needless to
11441 say, misuse can cause serious problems. */
11442 isa = atoi (name + 4);
553178e4 11443 switch (isa)
98d3f06f
KH
11444 {
11445 case 0:
11446 mips_opts.gp32 = file_mips_gp32;
11447 mips_opts.fp32 = file_mips_fp32;
11448 mips_opts.abi = file_mips_abi;
11449 break;
11450 case 1:
11451 case 2:
11452 case 32:
11453 mips_opts.gp32 = 1;
11454 mips_opts.fp32 = 1;
11455 break;
11456 case 3:
11457 case 4:
11458 case 5:
11459 case 64:
11460 /* Loosen ABI register width restriction. */
11461 if (mips_opts.abi == O32_ABI)
11462 mips_opts.abi = NO_ABI;
11463 mips_opts.gp32 = 0;
11464 mips_opts.fp32 = 0;
11465 break;
11466 default:
11467 as_bad (_("unknown ISA level %s"), name + 4);
11468 break;
11469 }
553178e4 11470
e7af610e 11471 switch (isa)
98d3f06f
KH
11472 {
11473 case 0: mips_opts.isa = file_mips_isa; break;
11474 case 1: mips_opts.isa = ISA_MIPS1; break;
11475 case 2: mips_opts.isa = ISA_MIPS2; break;
11476 case 3: mips_opts.isa = ISA_MIPS3; break;
11477 case 4: mips_opts.isa = ISA_MIPS4; break;
11478 case 5: mips_opts.isa = ISA_MIPS5; break;
11479 case 32: mips_opts.isa = ISA_MIPS32; break;
11480 case 64: mips_opts.isa = ISA_MIPS64; break;
11481 default: as_bad (_("unknown ISA level %s"), name + 4); break;
11482 }
252b5132
RH
11483 }
11484 else if (strcmp (name, "autoextend") == 0)
11485 mips_opts.noautoextend = 0;
11486 else if (strcmp (name, "noautoextend") == 0)
11487 mips_opts.noautoextend = 1;
11488 else if (strcmp (name, "push") == 0)
11489 {
11490 struct mips_option_stack *s;
11491
11492 s = (struct mips_option_stack *) xmalloc (sizeof *s);
11493 s->next = mips_opts_stack;
11494 s->options = mips_opts;
11495 mips_opts_stack = s;
11496 }
11497 else if (strcmp (name, "pop") == 0)
11498 {
11499 struct mips_option_stack *s;
11500
11501 s = mips_opts_stack;
11502 if (s == NULL)
11503 as_bad (_(".set pop with no .set push"));
11504 else
11505 {
11506 /* If we're changing the reorder mode we need to handle
11507 delay slots correctly. */
11508 if (s->options.noreorder && ! mips_opts.noreorder)
11509 mips_emit_delays (true);
11510 else if (! s->options.noreorder && mips_opts.noreorder)
11511 {
11512 if (prev_nop_frag != NULL)
11513 {
11514 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11515 * (mips_opts.mips16 ? 2 : 4));
11516 prev_nop_frag = NULL;
11517 }
11518 }
11519
11520 mips_opts = s->options;
11521 mips_opts_stack = s->next;
11522 free (s);
11523 }
11524 }
11525 else
11526 {
11527 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
11528 }
11529 *input_line_pointer = ch;
11530 demand_empty_rest_of_line ();
11531}
11532
11533/* Handle the .abicalls pseudo-op. I believe this is equivalent to
11534 .option pic2. It means to generate SVR4 PIC calls. */
11535
11536static void
11537s_abicalls (ignore)
43841e91 11538 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
11539{
11540 mips_pic = SVR4_PIC;
11541 if (USE_GLOBAL_POINTER_OPT)
11542 {
11543 if (g_switch_seen && g_switch_value != 0)
11544 as_warn (_("-G may not be used with SVR4 PIC code"));
11545 g_switch_value = 0;
11546 }
11547 bfd_set_gp_size (stdoutput, 0);
11548 demand_empty_rest_of_line ();
11549}
11550
11551/* Handle the .cpload pseudo-op. This is used when generating SVR4
11552 PIC code. It sets the $gp register for the function based on the
11553 function address, which is in the register named in the argument.
11554 This uses a relocation against _gp_disp, which is handled specially
11555 by the linker. The result is:
11556 lui $gp,%hi(_gp_disp)
11557 addiu $gp,$gp,%lo(_gp_disp)
11558 addu $gp,$gp,.cpload argument
11559 The .cpload argument is normally $25 == $t9. */
11560
11561static void
11562s_cpload (ignore)
43841e91 11563 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
11564{
11565 expressionS ex;
11566 int icnt = 0;
11567
6478892d
TS
11568 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
11569 .cpload is ignored. */
11570 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
11571 {
11572 s_ignore (0);
11573 return;
11574 }
11575
d3ecfc59 11576 /* .cpload should be in a .set noreorder section. */
252b5132
RH
11577 if (mips_opts.noreorder == 0)
11578 as_warn (_(".cpload not in noreorder section"));
11579
11580 ex.X_op = O_symbol;
11581 ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
11582 ex.X_op_symbol = NULL;
11583 ex.X_add_number = 0;
11584
11585 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
49309057 11586 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
252b5132 11587
c9914766
TS
11588 macro_build_lui (NULL, &icnt, &ex, mips_gp_register);
11589 macro_build ((char *) NULL, &icnt, &ex, "addiu", "t,r,j",
11590 mips_gp_register, mips_gp_register, (int) BFD_RELOC_LO16);
252b5132
RH
11591
11592 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "addu", "d,v,t",
c9914766 11593 mips_gp_register, mips_gp_register, tc_get_register (0));
252b5132
RH
11594
11595 demand_empty_rest_of_line ();
11596}
11597
6478892d
TS
11598/* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
11599 .cpsetup $reg1, offset|$reg2, label
11600
11601 If offset is given, this results in:
11602 sd $gp, offset($sp)
956cd1d6
TS
11603 lui $gp, %hi(%neg(%gp_rel(label)))
11604 daddiu $gp, $gp, %lo(%neg(%gp_rel(label)))
6478892d
TS
11605 addu $gp, $gp, $reg1
11606
11607 If $reg2 is given, this results in:
11608 daddu $reg2, $gp, $0
956cd1d6
TS
11609 lui $gp, %hi(%neg(%gp_rel(label)))
11610 daddiu $gp, $gp, %lo(%neg(%gp_rel(label)))
6478892d
TS
11611 addu $gp, $gp, $reg1
11612 */
11613static void
11614s_cpsetup (ignore)
11615 int ignore ATTRIBUTE_UNUSED;
11616{
11617 expressionS ex_off;
11618 expressionS ex_sym;
11619 int reg1;
11620 int icnt = 0;
11621 char *sym;
11622
8586fc66 11623 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
6478892d
TS
11624 We also need NewABI support. */
11625 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11626 {
11627 s_ignore (0);
11628 return;
11629 }
11630
11631 reg1 = tc_get_register (0);
11632 SKIP_WHITESPACE ();
11633 if (*input_line_pointer != ',')
11634 {
11635 as_bad (_("missing argument separator ',' for .cpsetup"));
11636 return;
11637 }
11638 else
11639 input_line_pointer++;
11640 SKIP_WHITESPACE ();
11641 if (*input_line_pointer == '$')
11642 mips_cpreturn_register = tc_get_register (0);
11643 else
11644 mips_cpreturn_offset = get_absolute_expression ();
11645 SKIP_WHITESPACE ();
11646 if (*input_line_pointer != ',')
11647 {
11648 as_bad (_("missing argument separator ',' for .cpsetup"));
11649 return;
11650 }
11651 else
f9419b05 11652 ++input_line_pointer;
6478892d
TS
11653 SKIP_WHITESPACE ();
11654 sym = input_line_pointer;
11655 while (ISALNUM (*input_line_pointer))
f9419b05 11656 ++input_line_pointer;
6478892d
TS
11657 *input_line_pointer = 0;
11658
11659 ex_sym.X_op = O_symbol;
11660 ex_sym.X_add_symbol = symbol_find_or_make (sym);
11661 ex_sym.X_op_symbol = NULL;
11662 ex_sym.X_add_number = 0;
11663
11664 if (mips_cpreturn_register == -1)
11665 {
11666 ex_off.X_op = O_constant;
11667 ex_off.X_add_symbol = NULL;
11668 ex_off.X_op_symbol = NULL;
11669 ex_off.X_add_number = mips_cpreturn_offset;
11670
11671 macro_build ((char *) NULL, &icnt, &ex_off, "sd", "t,o(b)",
11672 mips_gp_register, (int) BFD_RELOC_LO16, SP);
11673 }
11674 else
11675 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
11676 "d,v,t", mips_cpreturn_register, mips_gp_register, 0);
11677
11678 macro_build ((char *) NULL, &icnt, &ex_sym, "lui", "t,u", mips_gp_register,
11679 (int) BFD_RELOC_GPREL16);
8586fc66
TS
11680 fix_new (frag_now, prev_insn_where, 0, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
11681 fix_new (frag_now, prev_insn_where, 0, NULL, 0, 0, BFD_RELOC_HI16_S);
6478892d
TS
11682 macro_build ((char *) NULL, &icnt, &ex_sym, "addiu", "t,r,j",
11683 mips_gp_register, mips_gp_register, (int) BFD_RELOC_GPREL16);
8586fc66
TS
11684 fix_new (frag_now, prev_insn_where, 0, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
11685 fix_new (frag_now, prev_insn_where, 0, NULL, 0, 0, BFD_RELOC_LO16);
11686 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
11687 HAVE_64BIT_ADDRESSES ? "daddu" : "addu", "d,v,t",
11688 mips_gp_register, mips_gp_register, reg1);
6478892d
TS
11689
11690 demand_empty_rest_of_line ();
11691}
11692
11693static void
11694s_cplocal (ignore)
11695 int ignore ATTRIBUTE_UNUSED;
11696{
11697 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
11698 .cplocal is ignored. */
11699 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11700 {
11701 s_ignore (0);
11702 return;
11703 }
11704
11705 mips_gp_register = tc_get_register (0);
11706}
11707
252b5132
RH
11708/* Handle the .cprestore pseudo-op. This stores $gp into a given
11709 offset from $sp. The offset is remembered, and after making a PIC
11710 call $gp is restored from that location. */
11711
11712static void
11713s_cprestore (ignore)
43841e91 11714 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
11715{
11716 expressionS ex;
11717 int icnt = 0;
11718
6478892d 11719 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
c9914766 11720 .cprestore is ignored. */
6478892d 11721 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
11722 {
11723 s_ignore (0);
11724 return;
11725 }
11726
11727 mips_cprestore_offset = get_absolute_expression ();
7a621144 11728 mips_cprestore_valid = 1;
252b5132
RH
11729
11730 ex.X_op = O_constant;
11731 ex.X_add_symbol = NULL;
11732 ex.X_op_symbol = NULL;
11733 ex.X_add_number = mips_cprestore_offset;
11734
c9914766
TS
11735 macro_build ((char *) NULL, &icnt, &ex, HAVE_32BIT_ADDRESSES ? "sw" : "sd",
11736 "t,o(b)", mips_gp_register, (int) BFD_RELOC_LO16, SP);
252b5132
RH
11737
11738 demand_empty_rest_of_line ();
11739}
11740
6478892d
TS
11741/* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
11742 was given in the preceeding .gpsetup, it results in:
11743 ld $gp, offset($sp)
76b3015f 11744
6478892d
TS
11745 If a register $reg2 was given there, it results in:
11746 daddiu $gp, $gp, $reg2
11747 */
11748static void
11749s_cpreturn (ignore)
11750 int ignore ATTRIBUTE_UNUSED;
11751{
11752 expressionS ex;
11753 int icnt = 0;
11754
11755 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
11756 We also need NewABI support. */
11757 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11758 {
11759 s_ignore (0);
11760 return;
11761 }
11762
11763 if (mips_cpreturn_register == -1)
11764 {
11765 ex.X_op = O_constant;
11766 ex.X_add_symbol = NULL;
11767 ex.X_op_symbol = NULL;
11768 ex.X_add_number = mips_cpreturn_offset;
11769
11770 macro_build ((char *) NULL, &icnt, &ex, "ld", "t,o(b)",
11771 mips_gp_register, (int) BFD_RELOC_LO16, SP);
11772 }
11773 else
11774 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
11775 "d,v,t", mips_gp_register, mips_cpreturn_register, 0);
11776
11777 demand_empty_rest_of_line ();
11778}
11779
11780/* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
11781 code. It sets the offset to use in gp_rel relocations. */
11782
11783static void
11784s_gpvalue (ignore)
11785 int ignore ATTRIBUTE_UNUSED;
11786{
11787 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
11788 We also need NewABI support. */
11789 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11790 {
11791 s_ignore (0);
11792 return;
11793 }
11794
def2e0dd 11795 mips_gprel_offset = get_absolute_expression ();
6478892d
TS
11796
11797 demand_empty_rest_of_line ();
11798}
11799
252b5132
RH
11800/* Handle the .gpword pseudo-op. This is used when generating PIC
11801 code. It generates a 32 bit GP relative reloc. */
11802
11803static void
11804s_gpword (ignore)
43841e91 11805 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
11806{
11807 symbolS *label;
11808 expressionS ex;
11809 char *p;
11810
11811 /* When not generating PIC code, this is treated as .word. */
11812 if (mips_pic != SVR4_PIC)
11813 {
11814 s_cons (2);
11815 return;
11816 }
11817
11818 label = insn_labels != NULL ? insn_labels->label : NULL;
11819 mips_emit_delays (true);
11820 if (auto_align)
11821 mips_align (2, 0, label);
11822 mips_clear_insn_labels ();
11823
11824 expression (&ex);
11825
11826 if (ex.X_op != O_symbol || ex.X_add_number != 0)
11827 {
11828 as_bad (_("Unsupported use of .gpword"));
11829 ignore_rest_of_line ();
11830 }
11831
11832 p = frag_more (4);
11833 md_number_to_chars (p, (valueT) 0, 4);
c9914766 11834 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, false,
cdf6fd85 11835 BFD_RELOC_GPREL32);
252b5132
RH
11836
11837 demand_empty_rest_of_line ();
11838}
11839
11840/* Handle the .cpadd pseudo-op. This is used when dealing with switch
11841 tables in SVR4 PIC code. */
11842
11843static void
11844s_cpadd (ignore)
43841e91 11845 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
11846{
11847 int icnt = 0;
11848 int reg;
11849
6478892d
TS
11850 /* This is ignored when not generating SVR4 PIC code or if this is NewABI
11851 code. */
11852 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
11853 {
11854 s_ignore (0);
11855 return;
11856 }
11857
11858 /* Add $gp to the register named as an argument. */
11859 reg = tc_get_register (0);
11860 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 11861 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 11862 "d,v,t", reg, reg, mips_gp_register);
252b5132 11863
bdaaa2e1 11864 demand_empty_rest_of_line ();
252b5132
RH
11865}
11866
11867/* Handle the .insn pseudo-op. This marks instruction labels in
11868 mips16 mode. This permits the linker to handle them specially,
11869 such as generating jalx instructions when needed. We also make
11870 them odd for the duration of the assembly, in order to generate the
11871 right sort of code. We will make them even in the adjust_symtab
11872 routine, while leaving them marked. This is convenient for the
11873 debugger and the disassembler. The linker knows to make them odd
11874 again. */
11875
11876static void
11877s_insn (ignore)
43841e91 11878 int ignore ATTRIBUTE_UNUSED;
252b5132 11879{
f9419b05 11880 mips16_mark_labels ();
252b5132
RH
11881
11882 demand_empty_rest_of_line ();
11883}
11884
11885/* Handle a .stabn directive. We need these in order to mark a label
11886 as being a mips16 text label correctly. Sometimes the compiler
11887 will emit a label, followed by a .stabn, and then switch sections.
11888 If the label and .stabn are in mips16 mode, then the label is
11889 really a mips16 text label. */
11890
11891static void
11892s_mips_stab (type)
11893 int type;
11894{
f9419b05 11895 if (type == 'n')
252b5132
RH
11896 mips16_mark_labels ();
11897
11898 s_stab (type);
11899}
11900
11901/* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
11902 */
11903
11904static void
11905s_mips_weakext (ignore)
43841e91 11906 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
11907{
11908 char *name;
11909 int c;
11910 symbolS *symbolP;
11911 expressionS exp;
11912
11913 name = input_line_pointer;
11914 c = get_symbol_end ();
11915 symbolP = symbol_find_or_make (name);
11916 S_SET_WEAK (symbolP);
11917 *input_line_pointer = c;
11918
11919 SKIP_WHITESPACE ();
11920
11921 if (! is_end_of_line[(unsigned char) *input_line_pointer])
11922 {
11923 if (S_IS_DEFINED (symbolP))
11924 {
956cd1d6 11925 as_bad ("ignoring attempt to redefine symbol %s",
252b5132
RH
11926 S_GET_NAME (symbolP));
11927 ignore_rest_of_line ();
11928 return;
11929 }
bdaaa2e1 11930
252b5132
RH
11931 if (*input_line_pointer == ',')
11932 {
11933 ++input_line_pointer;
11934 SKIP_WHITESPACE ();
11935 }
bdaaa2e1 11936
252b5132
RH
11937 expression (&exp);
11938 if (exp.X_op != O_symbol)
11939 {
11940 as_bad ("bad .weakext directive");
98d3f06f 11941 ignore_rest_of_line ();
252b5132
RH
11942 return;
11943 }
49309057 11944 symbol_set_value_expression (symbolP, &exp);
252b5132
RH
11945 }
11946
11947 demand_empty_rest_of_line ();
11948}
11949
11950/* Parse a register string into a number. Called from the ECOFF code
11951 to parse .frame. The argument is non-zero if this is the frame
11952 register, so that we can record it in mips_frame_reg. */
11953
11954int
11955tc_get_register (frame)
11956 int frame;
11957{
11958 int reg;
11959
11960 SKIP_WHITESPACE ();
11961 if (*input_line_pointer++ != '$')
11962 {
11963 as_warn (_("expected `$'"));
11964 reg = 0;
11965 }
3882b010 11966 else if (ISDIGIT (*input_line_pointer))
252b5132
RH
11967 {
11968 reg = get_absolute_expression ();
11969 if (reg < 0 || reg >= 32)
11970 {
11971 as_warn (_("Bad register number"));
11972 reg = 0;
11973 }
11974 }
11975 else
11976 {
11977 if (strncmp (input_line_pointer, "fp", 2) == 0)
11978 reg = FP;
11979 else if (strncmp (input_line_pointer, "sp", 2) == 0)
11980 reg = SP;
11981 else if (strncmp (input_line_pointer, "gp", 2) == 0)
11982 reg = GP;
11983 else if (strncmp (input_line_pointer, "at", 2) == 0)
11984 reg = AT;
11985 else
11986 {
11987 as_warn (_("Unrecognized register name"));
11988 reg = 0;
11989 }
11990 input_line_pointer += 2;
11991 }
11992 if (frame)
7a621144
DJ
11993 {
11994 mips_frame_reg = reg != 0 ? reg : SP;
11995 mips_frame_reg_valid = 1;
11996 mips_cprestore_valid = 0;
11997 }
252b5132
RH
11998 return reg;
11999}
12000
12001valueT
12002md_section_align (seg, addr)
12003 asection *seg;
12004 valueT addr;
12005{
12006 int align = bfd_get_section_alignment (stdoutput, seg);
12007
12008#ifdef OBJ_ELF
12009 /* We don't need to align ELF sections to the full alignment.
12010 However, Irix 5 may prefer that we align them at least to a 16
12011 byte boundary. We don't bother to align the sections if we are
12012 targeted for an embedded system. */
12013 if (strcmp (TARGET_OS, "elf") == 0)
12014 return addr;
12015 if (align > 4)
12016 align = 4;
12017#endif
12018
12019 return ((addr + (1 << align) - 1) & (-1 << align));
12020}
12021
12022/* Utility routine, called from above as well. If called while the
12023 input file is still being read, it's only an approximation. (For
12024 example, a symbol may later become defined which appeared to be
12025 undefined earlier.) */
12026
12027static int
12028nopic_need_relax (sym, before_relaxing)
12029 symbolS *sym;
12030 int before_relaxing;
12031{
12032 if (sym == 0)
12033 return 0;
12034
6478892d 12035 if (USE_GLOBAL_POINTER_OPT && g_switch_value > 0)
252b5132
RH
12036 {
12037 const char *symname;
12038 int change;
12039
c9914766 12040 /* Find out whether this symbol can be referenced off the $gp
252b5132
RH
12041 register. It can be if it is smaller than the -G size or if
12042 it is in the .sdata or .sbss section. Certain symbols can
c9914766 12043 not be referenced off the $gp, although it appears as though
252b5132
RH
12044 they can. */
12045 symname = S_GET_NAME (sym);
12046 if (symname != (const char *) NULL
12047 && (strcmp (symname, "eprol") == 0
12048 || strcmp (symname, "etext") == 0
12049 || strcmp (symname, "_gp") == 0
12050 || strcmp (symname, "edata") == 0
12051 || strcmp (symname, "_fbss") == 0
12052 || strcmp (symname, "_fdata") == 0
12053 || strcmp (symname, "_ftext") == 0
12054 || strcmp (symname, "end") == 0
12055 || strcmp (symname, "_gp_disp") == 0))
12056 change = 1;
12057 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
12058 && (0
12059#ifndef NO_ECOFF_DEBUGGING
49309057
ILT
12060 || (symbol_get_obj (sym)->ecoff_extern_size != 0
12061 && (symbol_get_obj (sym)->ecoff_extern_size
12062 <= g_switch_value))
252b5132
RH
12063#endif
12064 /* We must defer this decision until after the whole
12065 file has been read, since there might be a .extern
12066 after the first use of this symbol. */
12067 || (before_relaxing
12068#ifndef NO_ECOFF_DEBUGGING
49309057 12069 && symbol_get_obj (sym)->ecoff_extern_size == 0
252b5132
RH
12070#endif
12071 && S_GET_VALUE (sym) == 0)
12072 || (S_GET_VALUE (sym) != 0
12073 && S_GET_VALUE (sym) <= g_switch_value)))
12074 change = 0;
12075 else
12076 {
12077 const char *segname;
12078
12079 segname = segment_name (S_GET_SEGMENT (sym));
12080 assert (strcmp (segname, ".lit8") != 0
12081 && strcmp (segname, ".lit4") != 0);
12082 change = (strcmp (segname, ".sdata") != 0
fba2b7f9
GK
12083 && strcmp (segname, ".sbss") != 0
12084 && strncmp (segname, ".sdata.", 7) != 0
12085 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
252b5132
RH
12086 }
12087 return change;
12088 }
12089 else
c9914766 12090 /* We are not optimizing for the $gp register. */
252b5132
RH
12091 return 1;
12092}
12093
12094/* Given a mips16 variant frag FRAGP, return non-zero if it needs an
12095 extended opcode. SEC is the section the frag is in. */
12096
12097static int
12098mips16_extended_frag (fragp, sec, stretch)
12099 fragS *fragp;
12100 asection *sec;
12101 long stretch;
12102{
12103 int type;
12104 register const struct mips16_immed_operand *op;
12105 offsetT val;
12106 int mintiny, maxtiny;
12107 segT symsec;
98aa84af 12108 fragS *sym_frag;
252b5132
RH
12109
12110 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
12111 return 0;
12112 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
12113 return 1;
12114
12115 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12116 op = mips16_immed_operands;
12117 while (op->type != type)
12118 {
12119 ++op;
12120 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
12121 }
12122
12123 if (op->unsp)
12124 {
12125 if (type == '<' || type == '>' || type == '[' || type == ']')
12126 {
12127 mintiny = 1;
12128 maxtiny = 1 << op->nbits;
12129 }
12130 else
12131 {
12132 mintiny = 0;
12133 maxtiny = (1 << op->nbits) - 1;
12134 }
12135 }
12136 else
12137 {
12138 mintiny = - (1 << (op->nbits - 1));
12139 maxtiny = (1 << (op->nbits - 1)) - 1;
12140 }
12141
98aa84af 12142 sym_frag = symbol_get_frag (fragp->fr_symbol);
ac62c346 12143 val = S_GET_VALUE (fragp->fr_symbol);
98aa84af 12144 symsec = S_GET_SEGMENT (fragp->fr_symbol);
252b5132
RH
12145
12146 if (op->pcrel)
12147 {
12148 addressT addr;
12149
12150 /* We won't have the section when we are called from
12151 mips_relax_frag. However, we will always have been called
12152 from md_estimate_size_before_relax first. If this is a
12153 branch to a different section, we mark it as such. If SEC is
12154 NULL, and the frag is not marked, then it must be a branch to
12155 the same section. */
12156 if (sec == NULL)
12157 {
12158 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
12159 return 1;
12160 }
12161 else
12162 {
98aa84af 12163 /* Must have been called from md_estimate_size_before_relax. */
252b5132
RH
12164 if (symsec != sec)
12165 {
12166 fragp->fr_subtype =
12167 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12168
12169 /* FIXME: We should support this, and let the linker
12170 catch branches and loads that are out of range. */
12171 as_bad_where (fragp->fr_file, fragp->fr_line,
12172 _("unsupported PC relative reference to different section"));
12173
12174 return 1;
12175 }
98aa84af
AM
12176 if (fragp != sym_frag && sym_frag->fr_address == 0)
12177 /* Assume non-extended on the first relaxation pass.
12178 The address we have calculated will be bogus if this is
12179 a forward branch to another frag, as the forward frag
12180 will have fr_address == 0. */
12181 return 0;
252b5132
RH
12182 }
12183
12184 /* In this case, we know for sure that the symbol fragment is in
98aa84af
AM
12185 the same section. If the relax_marker of the symbol fragment
12186 differs from the relax_marker of this fragment, we have not
12187 yet adjusted the symbol fragment fr_address. We want to add
252b5132
RH
12188 in STRETCH in order to get a better estimate of the address.
12189 This particularly matters because of the shift bits. */
12190 if (stretch != 0
98aa84af 12191 && sym_frag->relax_marker != fragp->relax_marker)
252b5132
RH
12192 {
12193 fragS *f;
12194
12195 /* Adjust stretch for any alignment frag. Note that if have
12196 been expanding the earlier code, the symbol may be
12197 defined in what appears to be an earlier frag. FIXME:
12198 This doesn't handle the fr_subtype field, which specifies
12199 a maximum number of bytes to skip when doing an
12200 alignment. */
98aa84af 12201 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
252b5132
RH
12202 {
12203 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
12204 {
12205 if (stretch < 0)
12206 stretch = - ((- stretch)
12207 & ~ ((1 << (int) f->fr_offset) - 1));
12208 else
12209 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
12210 if (stretch == 0)
12211 break;
12212 }
12213 }
12214 if (f != NULL)
12215 val += stretch;
12216 }
12217
12218 addr = fragp->fr_address + fragp->fr_fix;
12219
12220 /* The base address rules are complicated. The base address of
12221 a branch is the following instruction. The base address of a
12222 PC relative load or add is the instruction itself, but if it
12223 is in a delay slot (in which case it can not be extended) use
12224 the address of the instruction whose delay slot it is in. */
12225 if (type == 'p' || type == 'q')
12226 {
12227 addr += 2;
12228
12229 /* If we are currently assuming that this frag should be
12230 extended, then, the current address is two bytes
bdaaa2e1 12231 higher. */
252b5132
RH
12232 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12233 addr += 2;
12234
12235 /* Ignore the low bit in the target, since it will be set
12236 for a text label. */
12237 if ((val & 1) != 0)
12238 --val;
12239 }
12240 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12241 addr -= 4;
12242 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12243 addr -= 2;
12244
12245 val -= addr & ~ ((1 << op->shift) - 1);
12246
12247 /* Branch offsets have an implicit 0 in the lowest bit. */
12248 if (type == 'p' || type == 'q')
12249 val /= 2;
12250
12251 /* If any of the shifted bits are set, we must use an extended
12252 opcode. If the address depends on the size of this
12253 instruction, this can lead to a loop, so we arrange to always
12254 use an extended opcode. We only check this when we are in
12255 the main relaxation loop, when SEC is NULL. */
12256 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
12257 {
12258 fragp->fr_subtype =
12259 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12260 return 1;
12261 }
12262
12263 /* If we are about to mark a frag as extended because the value
12264 is precisely maxtiny + 1, then there is a chance of an
12265 infinite loop as in the following code:
12266 la $4,foo
12267 .skip 1020
12268 .align 2
12269 foo:
12270 In this case when the la is extended, foo is 0x3fc bytes
12271 away, so the la can be shrunk, but then foo is 0x400 away, so
12272 the la must be extended. To avoid this loop, we mark the
12273 frag as extended if it was small, and is about to become
12274 extended with a value of maxtiny + 1. */
12275 if (val == ((maxtiny + 1) << op->shift)
12276 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
12277 && sec == NULL)
12278 {
12279 fragp->fr_subtype =
12280 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12281 return 1;
12282 }
12283 }
12284 else if (symsec != absolute_section && sec != NULL)
12285 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
12286
12287 if ((val & ((1 << op->shift) - 1)) != 0
12288 || val < (mintiny << op->shift)
12289 || val > (maxtiny << op->shift))
12290 return 1;
12291 else
12292 return 0;
12293}
12294
12295/* Estimate the size of a frag before relaxing. Unless this is the
12296 mips16, we are not really relaxing here, and the final size is
12297 encoded in the subtype information. For the mips16, we have to
12298 decide whether we are using an extended opcode or not. */
12299
252b5132
RH
12300int
12301md_estimate_size_before_relax (fragp, segtype)
12302 fragS *fragp;
12303 asection *segtype;
12304{
43841e91 12305 int change = 0;
8614eeee 12306 boolean linkonce = false;
252b5132
RH
12307
12308 if (RELAX_MIPS16_P (fragp->fr_subtype))
177b4a6a
AO
12309 /* We don't want to modify the EXTENDED bit here; it might get us
12310 into infinite loops. We change it only in mips_relax_frag(). */
12311 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
252b5132
RH
12312
12313 if (mips_pic == NO_PIC)
12314 {
12315 change = nopic_need_relax (fragp->fr_symbol, 0);
12316 }
12317 else if (mips_pic == SVR4_PIC)
12318 {
12319 symbolS *sym;
12320 asection *symsec;
12321
12322 sym = fragp->fr_symbol;
12323
12324 /* Handle the case of a symbol equated to another symbol. */
e0890092 12325 while (symbol_equated_reloc_p (sym))
252b5132
RH
12326 {
12327 symbolS *n;
12328
12329 /* It's possible to get a loop here in a badly written
12330 program. */
49309057 12331 n = symbol_get_value_expression (sym)->X_add_symbol;
252b5132
RH
12332 if (n == sym)
12333 break;
12334 sym = n;
12335 }
12336
12337 symsec = S_GET_SEGMENT (sym);
12338
8614eeee
UC
12339 /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
12340 if (symsec != segtype && ! S_IS_LOCAL (sym))
beae10d5
KH
12341 {
12342 if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
12343 != 0)
12344 linkonce = true;
12345
12346 /* The GNU toolchain uses an extension for ELF: a section
12347 beginning with the magic string .gnu.linkonce is a linkonce
12348 section. */
12349 if (strncmp (segment_name (symsec), ".gnu.linkonce",
12350 sizeof ".gnu.linkonce" - 1) == 0)
12351 linkonce = true;
12352 }
8614eeee 12353
252b5132
RH
12354 /* This must duplicate the test in adjust_reloc_syms. */
12355 change = (symsec != &bfd_und_section
12356 && symsec != &bfd_abs_section
426b0403 12357 && ! bfd_is_com_section (symsec)
8614eeee 12358 && !linkonce
426b0403 12359#ifdef OBJ_ELF
ea4ff978 12360 /* A global or weak symbol is treated as external. */
9151e8bf 12361 && (OUTPUT_FLAVOR != bfd_target_elf_flavour
b25a253c
CD
12362 || (! S_IS_WEAK (sym)
12363 && (! S_IS_EXTERN (sym) || mips_pic == EMBEDDED_PIC)))
426b0403
AM
12364#endif
12365 );
252b5132
RH
12366 }
12367 else
12368 abort ();
12369
12370 if (change)
12371 {
12372 /* Record the offset to the first reloc in the fr_opcode field.
12373 This lets md_convert_frag and tc_gen_reloc know that the code
12374 must be expanded. */
12375 fragp->fr_opcode = (fragp->fr_literal
12376 + fragp->fr_fix
12377 - RELAX_OLD (fragp->fr_subtype)
12378 + RELAX_RELOC1 (fragp->fr_subtype));
12379 /* FIXME: This really needs as_warn_where. */
12380 if (RELAX_WARN (fragp->fr_subtype))
9a41af64
TS
12381 as_warn (_("AT used after \".set noat\" or macro used after "
12382 "\".set nomacro\""));
12383
12384 return RELAX_NEW (fragp->fr_subtype) - RELAX_OLD (fragp->fr_subtype);
252b5132
RH
12385 }
12386
9a41af64 12387 return 0;
252b5132
RH
12388}
12389
12390/* This is called to see whether a reloc against a defined symbol
12391 should be converted into a reloc against a section. Don't adjust
12392 MIPS16 jump relocations, so we don't have to worry about the format
12393 of the offset in the .o file. Don't adjust relocations against
12394 mips16 symbols, so that the linker can find them if it needs to set
12395 up a stub. */
12396
12397int
12398mips_fix_adjustable (fixp)
12399 fixS *fixp;
12400{
ea4ff978
L
12401#ifdef OBJ_ELF
12402 /* Prevent all adjustments to global symbols. */
46bac6de 12403 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
b25a253c 12404 && mips_pic != EMBEDDED_PIC
46bac6de 12405 && (S_IS_EXTERN (fixp->fx_addsy) || S_IS_WEAK (fixp->fx_addsy)))
ea4ff978
L
12406 return 0;
12407#endif
252b5132
RH
12408 if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
12409 return 0;
12410 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
12411 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12412 return 0;
12413 if (fixp->fx_addsy == NULL)
12414 return 1;
12415#ifdef OBJ_ELF
12416 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
12417 && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
12418 && fixp->fx_subsy == NULL)
12419 return 0;
12420#endif
12421 return 1;
12422}
12423
12424/* Translate internal representation of relocation info to BFD target
12425 format. */
12426
12427arelent **
12428tc_gen_reloc (section, fixp)
43841e91 12429 asection *section ATTRIBUTE_UNUSED;
252b5132
RH
12430 fixS *fixp;
12431{
12432 static arelent *retval[4];
12433 arelent *reloc;
12434 bfd_reloc_code_real_type code;
12435
12436 reloc = retval[0] = (arelent *) xmalloc (sizeof (arelent));
12437 retval[1] = NULL;
12438
49309057
ILT
12439 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
12440 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
12441 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
12442
12443 if (mips_pic == EMBEDDED_PIC
12444 && SWITCH_TABLE (fixp))
12445 {
12446 /* For a switch table entry we use a special reloc. The addend
12447 is actually the difference between the reloc address and the
12448 subtrahend. */
12449 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
12450 if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
12451 as_fatal (_("Double check fx_r_type in tc-mips.c:tc_gen_reloc"));
12452 fixp->fx_r_type = BFD_RELOC_GPREL32;
12453 }
12454 else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16)
12455 {
4514d474
CD
12456 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
12457 reloc->addend = fixp->fx_addnumber;
252b5132 12458 else
4514d474
CD
12459 {
12460 /* We use a special addend for an internal RELLO reloc. */
12461 if (symbol_section_p (fixp->fx_addsy))
12462 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
12463 else
12464 reloc->addend = fixp->fx_addnumber + reloc->address;
12465 }
252b5132
RH
12466 }
12467 else if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
12468 {
12469 assert (fixp->fx_next != NULL
12470 && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
4514d474
CD
12471
12472 /* The reloc is relative to the RELLO; adjust the addend
252b5132 12473 accordingly. */
4514d474
CD
12474 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
12475 reloc->addend = fixp->fx_next->fx_addnumber;
252b5132 12476 else
4514d474
CD
12477 {
12478 /* We use a special addend for an internal RELHI reloc. */
12479 if (symbol_section_p (fixp->fx_addsy))
12480 reloc->addend = (fixp->fx_next->fx_frag->fr_address
12481 + fixp->fx_next->fx_where
12482 - S_GET_VALUE (fixp->fx_subsy));
12483 else
12484 reloc->addend = (fixp->fx_addnumber
12485 + fixp->fx_next->fx_frag->fr_address
12486 + fixp->fx_next->fx_where);
12487 }
252b5132 12488 }
4514d474
CD
12489 else if (fixp->fx_pcrel == 0 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
12490 reloc->addend = fixp->fx_addnumber;
252b5132
RH
12491 else
12492 {
12493 if (OUTPUT_FLAVOR != bfd_target_aout_flavour)
12494 /* A gruesome hack which is a result of the gruesome gas reloc
12495 handling. */
12496 reloc->addend = reloc->address;
12497 else
12498 reloc->addend = -reloc->address;
12499 }
12500
12501 /* If this is a variant frag, we may need to adjust the existing
12502 reloc and generate a new one. */
12503 if (fixp->fx_frag->fr_opcode != NULL
cdf6fd85 12504 && (fixp->fx_r_type == BFD_RELOC_GPREL16
252b5132
RH
12505 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
12506 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL16
12507 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
12508 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16
12509 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
6478892d
TS
12510 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_LO16)
12511 && ! HAVE_NEWABI)
252b5132
RH
12512 {
12513 arelent *reloc2;
12514
12515 assert (! RELAX_MIPS16_P (fixp->fx_frag->fr_subtype));
12516
12517 /* If this is not the last reloc in this frag, then we have two
12518 GPREL relocs, or a GOT_HI16/GOT_LO16 pair, or a
12519 CALL_HI16/CALL_LO16, both of which are being replaced. Let
12520 the second one handle all of them. */
12521 if (fixp->fx_next != NULL
12522 && fixp->fx_frag == fixp->fx_next->fx_frag)
12523 {
cdf6fd85
TS
12524 assert ((fixp->fx_r_type == BFD_RELOC_GPREL16
12525 && fixp->fx_next->fx_r_type == BFD_RELOC_GPREL16)
252b5132
RH
12526 || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
12527 && (fixp->fx_next->fx_r_type
12528 == BFD_RELOC_MIPS_GOT_LO16))
12529 || (fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
12530 && (fixp->fx_next->fx_r_type
12531 == BFD_RELOC_MIPS_CALL_LO16)));
12532 retval[0] = NULL;
12533 return retval;
12534 }
12535
12536 fixp->fx_where = fixp->fx_frag->fr_opcode - fixp->fx_frag->fr_literal;
12537 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
12538 reloc2 = retval[1] = (arelent *) xmalloc (sizeof (arelent));
12539 retval[2] = NULL;
49309057
ILT
12540 reloc2->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
12541 *reloc2->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
12542 reloc2->address = (reloc->address
12543 + (RELAX_RELOC2 (fixp->fx_frag->fr_subtype)
12544 - RELAX_RELOC1 (fixp->fx_frag->fr_subtype)));
12545 reloc2->addend = fixp->fx_addnumber;
12546 reloc2->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO16);
12547 assert (reloc2->howto != NULL);
12548
12549 if (RELAX_RELOC3 (fixp->fx_frag->fr_subtype))
12550 {
12551 arelent *reloc3;
12552
12553 reloc3 = retval[2] = (arelent *) xmalloc (sizeof (arelent));
12554 retval[3] = NULL;
12555 *reloc3 = *reloc2;
12556 reloc3->address += 4;
12557 }
12558
12559 if (mips_pic == NO_PIC)
12560 {
cdf6fd85 12561 assert (fixp->fx_r_type == BFD_RELOC_GPREL16);
252b5132
RH
12562 fixp->fx_r_type = BFD_RELOC_HI16_S;
12563 }
12564 else if (mips_pic == SVR4_PIC)
12565 {
12566 switch (fixp->fx_r_type)
12567 {
12568 default:
12569 abort ();
12570 case BFD_RELOC_MIPS_GOT16:
12571 break;
12572 case BFD_RELOC_MIPS_CALL16:
12573 case BFD_RELOC_MIPS_GOT_LO16:
12574 case BFD_RELOC_MIPS_CALL_LO16:
12575 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
12576 break;
12577 }
12578 }
12579 else
12580 abort ();
12581 }
12582
12583 /* Since MIPS ELF uses Rel instead of Rela, encode the vtable entry
12584 to be used in the relocation's section offset. */
12585 if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12586 {
12587 reloc->address = reloc->addend;
12588 reloc->addend = 0;
12589 }
12590
12591 /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
12592 fixup_segment converted a non-PC relative reloc into a PC
12593 relative reloc. In such a case, we need to convert the reloc
12594 code. */
12595 code = fixp->fx_r_type;
12596 if (fixp->fx_pcrel)
12597 {
12598 switch (code)
12599 {
12600 case BFD_RELOC_8:
12601 code = BFD_RELOC_8_PCREL;
12602 break;
12603 case BFD_RELOC_16:
12604 code = BFD_RELOC_16_PCREL;
12605 break;
12606 case BFD_RELOC_32:
12607 code = BFD_RELOC_32_PCREL;
12608 break;
12609 case BFD_RELOC_64:
12610 code = BFD_RELOC_64_PCREL;
12611 break;
12612 case BFD_RELOC_8_PCREL:
12613 case BFD_RELOC_16_PCREL:
12614 case BFD_RELOC_32_PCREL:
12615 case BFD_RELOC_64_PCREL:
12616 case BFD_RELOC_16_PCREL_S2:
12617 case BFD_RELOC_PCREL_HI16_S:
12618 case BFD_RELOC_PCREL_LO16:
12619 break;
12620 default:
12621 as_bad_where (fixp->fx_file, fixp->fx_line,
12622 _("Cannot make %s relocation PC relative"),
12623 bfd_get_reloc_code_name (code));
12624 }
12625 }
12626
add55e1f
RS
12627#ifdef OBJ_ELF
12628 /* md_apply_fix3 has a double-subtraction hack to get
12629 bfd_install_relocation to behave nicely. GPREL relocations are
12630 handled correctly without this hack, so undo it here. We can't
12631 stop md_apply_fix3 from subtracting twice in the first place since
12632 the fake addend is required for variant frags above. */
12633 if (fixp->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour
d11008e1 12634 && code == BFD_RELOC_GPREL16
add55e1f
RS
12635 && reloc->addend != 0
12636 && mips_need_elf_addend_fixup (fixp))
12637 reloc->addend += S_GET_VALUE (fixp->fx_addsy);
12638#endif
12639
252b5132
RH
12640 /* To support a PC relative reloc when generating embedded PIC code
12641 for ECOFF, we use a Cygnus extension. We check for that here to
12642 make sure that we don't let such a reloc escape normally. */
bb2d6cd7
GK
12643 if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
12644 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
252b5132
RH
12645 && code == BFD_RELOC_16_PCREL_S2
12646 && mips_pic != EMBEDDED_PIC)
12647 reloc->howto = NULL;
12648 else
12649 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
12650
12651 if (reloc->howto == NULL)
12652 {
12653 as_bad_where (fixp->fx_file, fixp->fx_line,
12654 _("Can not represent %s relocation in this object file format"),
12655 bfd_get_reloc_code_name (code));
12656 retval[0] = NULL;
12657 }
12658
12659 return retval;
12660}
12661
12662/* Relax a machine dependent frag. This returns the amount by which
12663 the current size of the frag should change. */
12664
12665int
12666mips_relax_frag (fragp, stretch)
12667 fragS *fragp;
12668 long stretch;
12669{
12670 if (! RELAX_MIPS16_P (fragp->fr_subtype))
12671 return 0;
12672
c4e7957c 12673 if (mips16_extended_frag (fragp, NULL, stretch))
252b5132
RH
12674 {
12675 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12676 return 0;
12677 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
12678 return 2;
12679 }
12680 else
12681 {
12682 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12683 return 0;
12684 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
12685 return -2;
12686 }
12687
12688 return 0;
12689}
12690
12691/* Convert a machine dependent frag. */
12692
12693void
12694md_convert_frag (abfd, asec, fragp)
43841e91 12695 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
12696 segT asec;
12697 fragS *fragp;
12698{
12699 int old, new;
12700 char *fixptr;
12701
12702 if (RELAX_MIPS16_P (fragp->fr_subtype))
12703 {
12704 int type;
12705 register const struct mips16_immed_operand *op;
12706 boolean small, ext;
12707 offsetT val;
12708 bfd_byte *buf;
12709 unsigned long insn;
12710 boolean use_extend;
12711 unsigned short extend;
12712
12713 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12714 op = mips16_immed_operands;
12715 while (op->type != type)
12716 ++op;
12717
12718 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12719 {
12720 small = false;
12721 ext = true;
12722 }
12723 else
12724 {
12725 small = true;
12726 ext = false;
12727 }
12728
6386f3a7 12729 resolve_symbol_value (fragp->fr_symbol);
252b5132
RH
12730 val = S_GET_VALUE (fragp->fr_symbol);
12731 if (op->pcrel)
12732 {
12733 addressT addr;
12734
12735 addr = fragp->fr_address + fragp->fr_fix;
12736
12737 /* The rules for the base address of a PC relative reloc are
12738 complicated; see mips16_extended_frag. */
12739 if (type == 'p' || type == 'q')
12740 {
12741 addr += 2;
12742 if (ext)
12743 addr += 2;
12744 /* Ignore the low bit in the target, since it will be
12745 set for a text label. */
12746 if ((val & 1) != 0)
12747 --val;
12748 }
12749 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12750 addr -= 4;
12751 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12752 addr -= 2;
12753
12754 addr &= ~ (addressT) ((1 << op->shift) - 1);
12755 val -= addr;
12756
12757 /* Make sure the section winds up with the alignment we have
12758 assumed. */
12759 if (op->shift > 0)
12760 record_alignment (asec, op->shift);
12761 }
12762
12763 if (ext
12764 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
12765 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
12766 as_warn_where (fragp->fr_file, fragp->fr_line,
12767 _("extended instruction in delay slot"));
12768
12769 buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
12770
12771 if (target_big_endian)
12772 insn = bfd_getb16 (buf);
12773 else
12774 insn = bfd_getl16 (buf);
12775
12776 mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
12777 RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
12778 small, ext, &insn, &use_extend, &extend);
12779
12780 if (use_extend)
12781 {
874e8986 12782 md_number_to_chars ((char *) buf, 0xf000 | extend, 2);
252b5132
RH
12783 fragp->fr_fix += 2;
12784 buf += 2;
12785 }
12786
874e8986 12787 md_number_to_chars ((char *) buf, insn, 2);
252b5132
RH
12788 fragp->fr_fix += 2;
12789 buf += 2;
12790 }
12791 else
12792 {
12793 if (fragp->fr_opcode == NULL)
12794 return;
12795
12796 old = RELAX_OLD (fragp->fr_subtype);
12797 new = RELAX_NEW (fragp->fr_subtype);
12798 fixptr = fragp->fr_literal + fragp->fr_fix;
12799
12800 if (new > 0)
12801 memcpy (fixptr - old, fixptr, new);
12802
12803 fragp->fr_fix += new - old;
12804 }
12805}
12806
12807#ifdef OBJ_ELF
12808
12809/* This function is called after the relocs have been generated.
12810 We've been storing mips16 text labels as odd. Here we convert them
12811 back to even for the convenience of the debugger. */
12812
12813void
12814mips_frob_file_after_relocs ()
12815{
12816 asymbol **syms;
12817 unsigned int count, i;
12818
12819 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
12820 return;
12821
12822 syms = bfd_get_outsymbols (stdoutput);
12823 count = bfd_get_symcount (stdoutput);
12824 for (i = 0; i < count; i++, syms++)
12825 {
12826 if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
12827 && ((*syms)->value & 1) != 0)
12828 {
12829 (*syms)->value &= ~1;
12830 /* If the symbol has an odd size, it was probably computed
12831 incorrectly, so adjust that as well. */
12832 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
12833 ++elf_symbol (*syms)->internal_elf_sym.st_size;
12834 }
12835 }
12836}
12837
12838#endif
12839
12840/* This function is called whenever a label is defined. It is used
12841 when handling branch delays; if a branch has a label, we assume we
12842 can not move it. */
12843
12844void
12845mips_define_label (sym)
12846 symbolS *sym;
12847{
12848 struct insn_label_list *l;
12849
12850 if (free_insn_labels == NULL)
12851 l = (struct insn_label_list *) xmalloc (sizeof *l);
12852 else
12853 {
12854 l = free_insn_labels;
12855 free_insn_labels = l->next;
12856 }
12857
12858 l->label = sym;
12859 l->next = insn_labels;
12860 insn_labels = l;
12861}
12862\f
12863#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
12864
12865/* Some special processing for a MIPS ELF file. */
12866
12867void
12868mips_elf_final_processing ()
12869{
12870 /* Write out the register information. */
195325d2 12871 if (file_mips_abi != N64_ABI)
252b5132
RH
12872 {
12873 Elf32_RegInfo s;
12874
12875 s.ri_gprmask = mips_gprmask;
12876 s.ri_cprmask[0] = mips_cprmask[0];
12877 s.ri_cprmask[1] = mips_cprmask[1];
12878 s.ri_cprmask[2] = mips_cprmask[2];
12879 s.ri_cprmask[3] = mips_cprmask[3];
12880 /* The gp_value field is set by the MIPS ELF backend. */
12881
12882 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
12883 ((Elf32_External_RegInfo *)
12884 mips_regmask_frag));
12885 }
12886 else
12887 {
12888 Elf64_Internal_RegInfo s;
12889
12890 s.ri_gprmask = mips_gprmask;
12891 s.ri_pad = 0;
12892 s.ri_cprmask[0] = mips_cprmask[0];
12893 s.ri_cprmask[1] = mips_cprmask[1];
12894 s.ri_cprmask[2] = mips_cprmask[2];
12895 s.ri_cprmask[3] = mips_cprmask[3];
12896 /* The gp_value field is set by the MIPS ELF backend. */
12897
12898 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
12899 ((Elf64_External_RegInfo *)
12900 mips_regmask_frag));
12901 }
12902
12903 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
12904 sort of BFD interface for this. */
12905 if (mips_any_noreorder)
12906 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
12907 if (mips_pic != NO_PIC)
12908 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
12909
98d3f06f 12910 /* Set MIPS ELF flags for ASEs. */
1f25f5d3
CD
12911#if 0 /* XXX FIXME */
12912 if (file_ase_mips3d)
12913 elf_elfheader (stdoutput)->e_flags |= ???;
12914#endif
12915
bdaaa2e1 12916 /* Set the MIPS ELF ABI flags. */
a325df1d 12917 if (file_mips_abi == NO_ABI)
252b5132 12918 ;
a325df1d 12919 else if (file_mips_abi == O32_ABI)
252b5132 12920 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
a325df1d 12921 else if (file_mips_abi == O64_ABI)
252b5132 12922 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
a325df1d 12923 else if (file_mips_abi == EABI_ABI)
252b5132
RH
12924 {
12925 if (mips_eabi64)
12926 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
12927 else
12928 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
12929 }
a325df1d 12930 else if (file_mips_abi == N32_ABI)
be00bddd
TS
12931 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
12932
c9914766 12933 /* Nothing to do for N64_ABI. */
252b5132
RH
12934
12935 if (mips_32bitmode)
12936 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
12937}
12938
12939#endif /* OBJ_ELF || OBJ_MAYBE_ELF */
12940\f
beae10d5
KH
12941typedef struct proc {
12942 symbolS *isym;
12943 unsigned long reg_mask;
12944 unsigned long reg_offset;
12945 unsigned long fpreg_mask;
12946 unsigned long fpreg_offset;
12947 unsigned long frame_offset;
12948 unsigned long frame_reg;
12949 unsigned long pc_reg;
12950} procS;
252b5132
RH
12951
12952static procS cur_proc;
12953static procS *cur_proc_ptr;
12954static int numprocs;
12955
0a9ef439 12956/* Fill in an rs_align_code fragment. */
a19d8eb0 12957
0a9ef439
RH
12958void
12959mips_handle_align (fragp)
12960 fragS *fragp;
a19d8eb0 12961{
0a9ef439
RH
12962 if (fragp->fr_type != rs_align_code)
12963 return;
12964
12965 if (mips_opts.mips16)
a19d8eb0
CP
12966 {
12967 static const unsigned char be_nop[] = { 0x65, 0x00 };
12968 static const unsigned char le_nop[] = { 0x00, 0x65 };
12969
0a9ef439
RH
12970 int bytes;
12971 char *p;
a19d8eb0 12972
0a9ef439
RH
12973 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
12974 p = fragp->fr_literal + fragp->fr_fix;
12975
12976 if (bytes & 1)
12977 {
12978 *p++ = 0;
f9419b05 12979 fragp->fr_fix++;
0a9ef439
RH
12980 }
12981
12982 memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
12983 fragp->fr_var = 2;
a19d8eb0
CP
12984 }
12985
0a9ef439 12986 /* For mips32, a nop is a zero, which we trivially get by doing nothing. */
a19d8eb0
CP
12987}
12988
252b5132
RH
12989static void
12990md_obj_begin ()
12991{
12992}
12993
12994static void
12995md_obj_end ()
12996{
12997 /* check for premature end, nesting errors, etc */
12998 if (cur_proc_ptr)
9a41af64 12999 as_warn (_("missing .end at end of assembly"));
252b5132
RH
13000}
13001
13002static long
13003get_number ()
13004{
13005 int negative = 0;
13006 long val = 0;
13007
13008 if (*input_line_pointer == '-')
13009 {
13010 ++input_line_pointer;
13011 negative = 1;
13012 }
3882b010 13013 if (!ISDIGIT (*input_line_pointer))
956cd1d6 13014 as_bad (_("expected simple number"));
252b5132
RH
13015 if (input_line_pointer[0] == '0')
13016 {
13017 if (input_line_pointer[1] == 'x')
13018 {
13019 input_line_pointer += 2;
3882b010 13020 while (ISXDIGIT (*input_line_pointer))
252b5132
RH
13021 {
13022 val <<= 4;
13023 val |= hex_value (*input_line_pointer++);
13024 }
13025 return negative ? -val : val;
13026 }
13027 else
13028 {
13029 ++input_line_pointer;
3882b010 13030 while (ISDIGIT (*input_line_pointer))
252b5132
RH
13031 {
13032 val <<= 3;
13033 val |= *input_line_pointer++ - '0';
13034 }
13035 return negative ? -val : val;
13036 }
13037 }
3882b010 13038 if (!ISDIGIT (*input_line_pointer))
252b5132
RH
13039 {
13040 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
13041 *input_line_pointer, *input_line_pointer);
956cd1d6 13042 as_warn (_("invalid number"));
252b5132
RH
13043 return -1;
13044 }
3882b010 13045 while (ISDIGIT (*input_line_pointer))
252b5132
RH
13046 {
13047 val *= 10;
13048 val += *input_line_pointer++ - '0';
13049 }
13050 return negative ? -val : val;
13051}
13052
13053/* The .file directive; just like the usual .file directive, but there
13054 is an initial number which is the ECOFF file index. */
13055
13056static void
13057s_file (x)
43841e91 13058 int x ATTRIBUTE_UNUSED;
252b5132 13059{
874e8986 13060 get_number ();
252b5132
RH
13061 s_app_file (0);
13062}
13063
252b5132
RH
13064/* The .end directive. */
13065
13066static void
13067s_mips_end (x)
43841e91 13068 int x ATTRIBUTE_UNUSED;
252b5132
RH
13069{
13070 symbolS *p;
13071 int maybe_text;
13072
7a621144
DJ
13073 /* Following functions need their own .frame and .cprestore directives. */
13074 mips_frame_reg_valid = 0;
13075 mips_cprestore_valid = 0;
13076
252b5132
RH
13077 if (!is_end_of_line[(unsigned char) *input_line_pointer])
13078 {
13079 p = get_symbol ();
13080 demand_empty_rest_of_line ();
13081 }
13082 else
13083 p = NULL;
13084
13085#ifdef BFD_ASSEMBLER
13086 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
13087 maybe_text = 1;
13088 else
13089 maybe_text = 0;
13090#else
13091 if (now_seg != data_section && now_seg != bss_section)
13092 maybe_text = 1;
13093 else
13094 maybe_text = 0;
13095#endif
13096
13097 if (!maybe_text)
13098 as_warn (_(".end not in text section"));
13099
13100 if (!cur_proc_ptr)
13101 {
13102 as_warn (_(".end directive without a preceding .ent directive."));
13103 demand_empty_rest_of_line ();
13104 return;
13105 }
13106
13107 if (p != NULL)
13108 {
13109 assert (S_GET_NAME (p));
13110 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->isym)))
13111 as_warn (_(".end symbol does not match .ent symbol."));
13112 }
13113 else
13114 as_warn (_(".end directive missing or unknown symbol"));
13115
13116#ifdef MIPS_STABS_ELF
13117 {
13118 segT saved_seg = now_seg;
13119 subsegT saved_subseg = now_subseg;
252b5132 13120 valueT dot;
252b5132
RH
13121 expressionS exp;
13122 char *fragp;
13123
13124 dot = frag_now_fix ();
13125
13126#ifdef md_flush_pending_output
13127 md_flush_pending_output ();
13128#endif
13129
13130 assert (pdr_seg);
13131 subseg_set (pdr_seg, 0);
13132
beae10d5 13133 /* Write the symbol. */
252b5132
RH
13134 exp.X_op = O_symbol;
13135 exp.X_add_symbol = p;
13136 exp.X_add_number = 0;
13137 emit_expr (&exp, 4);
13138
beae10d5 13139 fragp = frag_more (7 * 4);
252b5132 13140
beae10d5
KH
13141 md_number_to_chars (fragp, (valueT) cur_proc_ptr->reg_mask, 4);
13142 md_number_to_chars (fragp + 4, (valueT) cur_proc_ptr->reg_offset, 4);
13143 md_number_to_chars (fragp + 8, (valueT) cur_proc_ptr->fpreg_mask, 4);
13144 md_number_to_chars (fragp + 12, (valueT) cur_proc_ptr->fpreg_offset, 4);
13145 md_number_to_chars (fragp + 16, (valueT) cur_proc_ptr->frame_offset, 4);
13146 md_number_to_chars (fragp + 20, (valueT) cur_proc_ptr->frame_reg, 4);
13147 md_number_to_chars (fragp + 24, (valueT) cur_proc_ptr->pc_reg, 4);
252b5132
RH
13148
13149 subseg_set (saved_seg, saved_subseg);
13150 }
f9419b05 13151#endif /* MIPS_STABS_ELF */
252b5132
RH
13152
13153 cur_proc_ptr = NULL;
13154}
13155
13156/* The .aent and .ent directives. */
13157
13158static void
13159s_mips_ent (aent)
13160 int aent;
13161{
252b5132
RH
13162 symbolS *symbolP;
13163 int maybe_text;
13164
13165 symbolP = get_symbol ();
13166 if (*input_line_pointer == ',')
f9419b05 13167 ++input_line_pointer;
252b5132 13168 SKIP_WHITESPACE ();
3882b010 13169 if (ISDIGIT (*input_line_pointer)
d9a62219 13170 || *input_line_pointer == '-')
874e8986 13171 get_number ();
252b5132
RH
13172
13173#ifdef BFD_ASSEMBLER
13174 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
13175 maybe_text = 1;
13176 else
13177 maybe_text = 0;
13178#else
13179 if (now_seg != data_section && now_seg != bss_section)
13180 maybe_text = 1;
13181 else
13182 maybe_text = 0;
13183#endif
13184
13185 if (!maybe_text)
13186 as_warn (_(".ent or .aent not in text section."));
13187
13188 if (!aent && cur_proc_ptr)
9a41af64 13189 as_warn (_("missing .end"));
252b5132
RH
13190
13191 if (!aent)
13192 {
7a621144
DJ
13193 /* This function needs its own .frame and .cprestore directives. */
13194 mips_frame_reg_valid = 0;
13195 mips_cprestore_valid = 0;
13196
252b5132
RH
13197 cur_proc_ptr = &cur_proc;
13198 memset (cur_proc_ptr, '\0', sizeof (procS));
13199
13200 cur_proc_ptr->isym = symbolP;
13201
49309057 13202 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
252b5132 13203
f9419b05 13204 ++numprocs;
252b5132
RH
13205 }
13206
13207 demand_empty_rest_of_line ();
13208}
13209
13210/* The .frame directive. If the mdebug section is present (IRIX 5 native)
bdaaa2e1 13211 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
252b5132 13212 s_mips_frame is used so that we can set the PDR information correctly.
bdaaa2e1 13213 We can't use the ecoff routines because they make reference to the ecoff
252b5132
RH
13214 symbol table (in the mdebug section). */
13215
13216static void
13217s_mips_frame (ignore)
2b3c5a5d 13218 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
13219{
13220#ifdef MIPS_STABS_ELF
13221
13222 long val;
13223
beae10d5 13224 if (cur_proc_ptr == (procS *) NULL)
252b5132
RH
13225 {
13226 as_warn (_(".frame outside of .ent"));
13227 demand_empty_rest_of_line ();
13228 return;
13229 }
13230
13231 cur_proc_ptr->frame_reg = tc_get_register (1);
13232
13233 SKIP_WHITESPACE ();
13234 if (*input_line_pointer++ != ','
13235 || get_absolute_expression_and_terminator (&val) != ',')
13236 {
13237 as_warn (_("Bad .frame directive"));
13238 --input_line_pointer;
13239 demand_empty_rest_of_line ();
13240 return;
13241 }
13242
13243 cur_proc_ptr->frame_offset = val;
13244 cur_proc_ptr->pc_reg = tc_get_register (0);
13245
13246 demand_empty_rest_of_line ();
13247#else
13248 s_ignore (ignore);
13249#endif /* MIPS_STABS_ELF */
13250}
13251
bdaaa2e1
KH
13252/* The .fmask and .mask directives. If the mdebug section is present
13253 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
252b5132 13254 embedded targets, s_mips_mask is used so that we can set the PDR
bdaaa2e1 13255 information correctly. We can't use the ecoff routines because they
252b5132
RH
13256 make reference to the ecoff symbol table (in the mdebug section). */
13257
13258static void
13259s_mips_mask (reg_type)
13260 char reg_type;
13261{
13262#ifdef MIPS_STABS_ELF
13263 long mask, off;
bdaaa2e1 13264
252b5132
RH
13265 if (cur_proc_ptr == (procS *) NULL)
13266 {
13267 as_warn (_(".mask/.fmask outside of .ent"));
13268 demand_empty_rest_of_line ();
13269 return;
13270 }
13271
13272 if (get_absolute_expression_and_terminator (&mask) != ',')
13273 {
13274 as_warn (_("Bad .mask/.fmask directive"));
13275 --input_line_pointer;
13276 demand_empty_rest_of_line ();
13277 return;
13278 }
13279
13280 off = get_absolute_expression ();
13281
13282 if (reg_type == 'F')
13283 {
13284 cur_proc_ptr->fpreg_mask = mask;
13285 cur_proc_ptr->fpreg_offset = off;
13286 }
13287 else
13288 {
13289 cur_proc_ptr->reg_mask = mask;
13290 cur_proc_ptr->reg_offset = off;
13291 }
13292
13293 demand_empty_rest_of_line ();
13294#else
13295 s_ignore (reg_type);
13296#endif /* MIPS_STABS_ELF */
13297}
13298
13299/* The .loc directive. */
13300
13301#if 0
13302static void
13303s_loc (x)
13304 int x;
13305{
13306 symbolS *symbolP;
13307 int lineno;
13308 int addroff;
13309
13310 assert (now_seg == text_section);
13311
13312 lineno = get_number ();
13313 addroff = frag_now_fix ();
13314
13315 symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
13316 S_SET_TYPE (symbolP, N_SLINE);
13317 S_SET_OTHER (symbolP, 0);
13318 S_SET_DESC (symbolP, lineno);
13319 symbolP->sy_segment = now_seg;
13320}
13321#endif
e7af610e
NC
13322
13323/* CPU name/ISA/number mapping table.
13324
13325 Entries are grouped by type. The first matching CPU or ISA entry
13326 gets chosen by CPU or ISA, so it should be the 'canonical' name
13327 for that type. Entries after that within the type are sorted
13328 alphabetically.
13329
13330 Case is ignored in comparison, so put the canonical entry in the
13331 appropriate case but everything else in lower case to ease eye pain. */
e972090a
NC
13332static const struct mips_cpu_info mips_cpu_info_table[] =
13333{
e7af610e
NC
13334 /* MIPS1 ISA */
13335 { "MIPS1", 1, ISA_MIPS1, CPU_R3000, },
13336 { "mips", 1, ISA_MIPS1, CPU_R3000, },
13337
13338 /* MIPS2 ISA */
13339 { "MIPS2", 1, ISA_MIPS2, CPU_R6000, },
13340
13341 /* MIPS3 ISA */
13342 { "MIPS3", 1, ISA_MIPS3, CPU_R4000, },
13343
13344 /* MIPS4 ISA */
13345 { "MIPS4", 1, ISA_MIPS4, CPU_R8000, },
13346
84ea6cf2
NC
13347 /* MIPS5 ISA */
13348 { "MIPS5", 1, ISA_MIPS5, CPU_MIPS5, },
13349 { "Generic-MIPS5", 0, ISA_MIPS5, CPU_MIPS5, },
13350
e7af610e
NC
13351 /* MIPS32 ISA */
13352 { "MIPS32", 1, ISA_MIPS32, CPU_MIPS32, },
3c02b2ab 13353 { "mipsisa32", 0, ISA_MIPS32, CPU_MIPS32, },
e7af610e 13354 { "Generic-MIPS32", 0, ISA_MIPS32, CPU_MIPS32, },
3c02b2ab
EC
13355 { "4kc", 0, ISA_MIPS32, CPU_MIPS32, },
13356 { "4km", 0, ISA_MIPS32, CPU_MIPS32, },
13357 { "4kp", 0, ISA_MIPS32, CPU_MIPS32, },
13358
13359 /* For historical reasons. */
13360 { "MIPS64", 1, ISA_MIPS3, CPU_R4000, },
e7af610e 13361
84ea6cf2 13362 /* MIPS64 ISA */
3c02b2ab 13363 { "mipsisa64", 1, ISA_MIPS64, CPU_MIPS64, },
84ea6cf2 13364 { "Generic-MIPS64", 0, ISA_MIPS64, CPU_MIPS64, },
3c02b2ab
EC
13365 { "5kc", 0, ISA_MIPS64, CPU_MIPS64, },
13366 { "20kc", 0, ISA_MIPS64, CPU_MIPS64, },
e7af610e
NC
13367
13368 /* R2000 CPU */
13369 { "R2000", 0, ISA_MIPS1, CPU_R2000, },
13370 { "2000", 0, ISA_MIPS1, CPU_R2000, },
13371 { "2k", 0, ISA_MIPS1, CPU_R2000, },
13372 { "r2k", 0, ISA_MIPS1, CPU_R2000, },
13373
13374 /* R3000 CPU */
13375 { "R3000", 0, ISA_MIPS1, CPU_R3000, },
13376 { "3000", 0, ISA_MIPS1, CPU_R3000, },
13377 { "3k", 0, ISA_MIPS1, CPU_R3000, },
13378 { "r3k", 0, ISA_MIPS1, CPU_R3000, },
13379
13380 /* TX3900 CPU */
13381 { "R3900", 0, ISA_MIPS1, CPU_R3900, },
13382 { "3900", 0, ISA_MIPS1, CPU_R3900, },
e972090a 13383 { "mipstx39", 0, ISA_MIPS1, CPU_R3900, },
e7af610e
NC
13384
13385 /* R4000 CPU */
13386 { "R4000", 0, ISA_MIPS3, CPU_R4000, },
13387 { "4000", 0, ISA_MIPS3, CPU_R4000, },
13388 { "4k", 0, ISA_MIPS3, CPU_R4000, }, /* beware */
13389 { "r4k", 0, ISA_MIPS3, CPU_R4000, },
13390
13391 /* R4010 CPU */
13392 { "R4010", 0, ISA_MIPS2, CPU_R4010, },
13393 { "4010", 0, ISA_MIPS2, CPU_R4010, },
13394
13395 /* R4400 CPU */
13396 { "R4400", 0, ISA_MIPS3, CPU_R4400, },
13397 { "4400", 0, ISA_MIPS3, CPU_R4400, },
13398
13399 /* R4600 CPU */
13400 { "R4600", 0, ISA_MIPS3, CPU_R4600, },
13401 { "4600", 0, ISA_MIPS3, CPU_R4600, },
13402 { "mips64orion", 0, ISA_MIPS3, CPU_R4600, },
13403 { "orion", 0, ISA_MIPS3, CPU_R4600, },
13404
13405 /* R4650 CPU */
13406 { "R4650", 0, ISA_MIPS3, CPU_R4650, },
13407 { "4650", 0, ISA_MIPS3, CPU_R4650, },
13408
13409 /* R6000 CPU */
13410 { "R6000", 0, ISA_MIPS2, CPU_R6000, },
13411 { "6000", 0, ISA_MIPS2, CPU_R6000, },
13412 { "6k", 0, ISA_MIPS2, CPU_R6000, },
13413 { "r6k", 0, ISA_MIPS2, CPU_R6000, },
13414
13415 /* R8000 CPU */
13416 { "R8000", 0, ISA_MIPS4, CPU_R8000, },
13417 { "8000", 0, ISA_MIPS4, CPU_R8000, },
13418 { "8k", 0, ISA_MIPS4, CPU_R8000, },
13419 { "r8k", 0, ISA_MIPS4, CPU_R8000, },
13420
13421 /* R10000 CPU */
13422 { "R10000", 0, ISA_MIPS4, CPU_R10000, },
13423 { "10000", 0, ISA_MIPS4, CPU_R10000, },
13424 { "10k", 0, ISA_MIPS4, CPU_R10000, },
13425 { "r10k", 0, ISA_MIPS4, CPU_R10000, },
13426
d1cf510e
NC
13427 /* R12000 CPU */
13428 { "R12000", 0, ISA_MIPS4, CPU_R12000, },
13429 { "12000", 0, ISA_MIPS4, CPU_R12000, },
13430 { "12k", 0, ISA_MIPS4, CPU_R12000, },
13431 { "r12k", 0, ISA_MIPS4, CPU_R12000, },
13432
e7af610e
NC
13433 /* VR4100 CPU */
13434 { "VR4100", 0, ISA_MIPS3, CPU_VR4100, },
13435 { "4100", 0, ISA_MIPS3, CPU_VR4100, },
13436 { "mips64vr4100", 0, ISA_MIPS3, CPU_VR4100, },
13437 { "r4100", 0, ISA_MIPS3, CPU_VR4100, },
13438
13439 /* VR4111 CPU */
13440 { "VR4111", 0, ISA_MIPS3, CPU_R4111, },
13441 { "4111", 0, ISA_MIPS3, CPU_R4111, },
13442 { "mips64vr4111", 0, ISA_MIPS3, CPU_R4111, },
13443 { "r4111", 0, ISA_MIPS3, CPU_R4111, },
13444
13445 /* VR4300 CPU */
13446 { "VR4300", 0, ISA_MIPS3, CPU_R4300, },
13447 { "4300", 0, ISA_MIPS3, CPU_R4300, },
13448 { "mips64vr4300", 0, ISA_MIPS3, CPU_R4300, },
13449 { "r4300", 0, ISA_MIPS3, CPU_R4300, },
13450
13451 /* VR5000 CPU */
13452 { "VR5000", 0, ISA_MIPS4, CPU_R5000, },
13453 { "5000", 0, ISA_MIPS4, CPU_R5000, },
13454 { "5k", 0, ISA_MIPS4, CPU_R5000, },
13455 { "mips64vr5000", 0, ISA_MIPS4, CPU_R5000, },
13456 { "r5000", 0, ISA_MIPS4, CPU_R5000, },
13457 { "r5200", 0, ISA_MIPS4, CPU_R5000, },
18ae5d72 13458 { "rm5200", 0, ISA_MIPS4, CPU_R5000, },
e7af610e 13459 { "r5230", 0, ISA_MIPS4, CPU_R5000, },
18ae5d72 13460 { "rm5230", 0, ISA_MIPS4, CPU_R5000, },
e7af610e 13461 { "r5231", 0, ISA_MIPS4, CPU_R5000, },
18ae5d72 13462 { "rm5231", 0, ISA_MIPS4, CPU_R5000, },
e7af610e 13463 { "r5261", 0, ISA_MIPS4, CPU_R5000, },
18ae5d72 13464 { "rm5261", 0, ISA_MIPS4, CPU_R5000, },
e7af610e 13465 { "r5721", 0, ISA_MIPS4, CPU_R5000, },
18ae5d72 13466 { "rm5721", 0, ISA_MIPS4, CPU_R5000, },
e7af610e
NC
13467 { "r5k", 0, ISA_MIPS4, CPU_R5000, },
13468 { "r7000", 0, ISA_MIPS4, CPU_R5000, },
13469
2e4acd24 13470 /* Broadcom SB-1 CPU */
c6c98b38 13471 { "SB-1", 0, ISA_MIPS64, CPU_SB1, },
e972090a 13472 { "sb-1250", 0, ISA_MIPS64, CPU_SB1, },
c6c98b38
NC
13473 { "sb1", 0, ISA_MIPS64, CPU_SB1, },
13474 { "sb1250", 0, ISA_MIPS64, CPU_SB1, },
13475
beae10d5 13476 /* End marker. */
e7af610e
NC
13477 { NULL, 0, 0, 0, },
13478};
13479
13480static const struct mips_cpu_info *
13481mips_cpu_info_from_name (name)
13482 const char *name;
13483{
13484 int i;
13485
13486 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
beae10d5 13487 if (strcasecmp (name, mips_cpu_info_table[i].name) == 0)
e7af610e
NC
13488 return (&mips_cpu_info_table[i]);
13489
e972090a 13490 return NULL;
e7af610e
NC
13491}
13492
13493static const struct mips_cpu_info *
13494mips_cpu_info_from_isa (isa)
13495 int isa;
13496{
13497 int i;
13498
13499 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13500 if (mips_cpu_info_table[i].is_isa
13501 && isa == mips_cpu_info_table[i].isa)
13502 return (&mips_cpu_info_table[i]);
13503
e972090a 13504 return NULL;
e7af610e
NC
13505}
13506
13507static const struct mips_cpu_info *
13508mips_cpu_info_from_cpu (cpu)
13509 int cpu;
13510{
13511 int i;
13512
13513 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13514 if (!mips_cpu_info_table[i].is_isa
13515 && cpu == mips_cpu_info_table[i].cpu)
13516 return (&mips_cpu_info_table[i]);
13517
e972090a 13518 return NULL;
e7af610e 13519}
This page took 0.997045 seconds and 4 git commands to generate.