2002-08-15 Andrew Cagney <ac131313@redhat.com>
[deliverable/binutils-gdb.git] / gas / config / tc-mips.c
CommitLineData
252b5132 1/* tc-mips.c -- assemble code for a MIPS chip.
98d3f06f 2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
82efde3a 3 Free Software Foundation, Inc.
252b5132
RH
4 Contributed by the OSF and Ralph Campbell.
5 Written by Keith Knowles and Ralph Campbell, working independently.
6 Modified for ECOFF and R4000 support by Ian Lance Taylor of Cygnus
7 Support.
8
9 This file is part of GAS.
10
11 GAS is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2, or (at your option)
14 any later version.
15
16 GAS is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with GAS; see the file COPYING. If not, write to the Free
23 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 02111-1307, USA. */
25
26#include "as.h"
27#include "config.h"
28#include "subsegs.h"
3882b010 29#include "safe-ctype.h"
252b5132
RH
30
31#ifdef USE_STDARG
32#include <stdarg.h>
33#endif
34#ifdef USE_VARARGS
35#include <varargs.h>
36#endif
37
38#include "opcode/mips.h"
39#include "itbl-ops.h"
c5dd6aab 40#include "dwarf2dbg.h"
252b5132
RH
41
42#ifdef DEBUG
43#define DBG(x) printf x
44#else
45#define DBG(x)
46#endif
47
48#ifdef OBJ_MAYBE_ELF
49/* Clean up namespace so we can include obj-elf.h too. */
50static int mips_output_flavor PARAMS ((void));
51static int mips_output_flavor () { return OUTPUT_FLAVOR; }
52#undef OBJ_PROCESS_STAB
53#undef OUTPUT_FLAVOR
54#undef S_GET_ALIGN
55#undef S_GET_SIZE
56#undef S_SET_ALIGN
57#undef S_SET_SIZE
252b5132
RH
58#undef obj_frob_file
59#undef obj_frob_file_after_relocs
60#undef obj_frob_symbol
61#undef obj_pop_insert
62#undef obj_sec_sym_ok_for_reloc
63#undef OBJ_COPY_SYMBOL_ATTRIBUTES
64
65#include "obj-elf.h"
66/* Fix any of them that we actually care about. */
67#undef OUTPUT_FLAVOR
68#define OUTPUT_FLAVOR mips_output_flavor()
69#endif
70
71#if defined (OBJ_ELF)
72#include "elf/mips.h"
73#endif
74
75#ifndef ECOFF_DEBUGGING
76#define NO_ECOFF_DEBUGGING
77#define ECOFF_DEBUGGING 0
78#endif
79
ecb4347a
DJ
80int mips_flag_mdebug = -1;
81
252b5132
RH
82#include "ecoff.h"
83
84#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
85static char *mips_regmask_frag;
86#endif
87
85b51719 88#define ZERO 0
252b5132
RH
89#define AT 1
90#define TREG 24
91#define PIC_CALL_REG 25
92#define KT0 26
93#define KT1 27
94#define GP 28
95#define SP 29
96#define FP 30
97#define RA 31
98
99#define ILLEGAL_REG (32)
100
101/* Allow override of standard little-endian ECOFF format. */
102
103#ifndef ECOFF_LITTLE_FORMAT
104#define ECOFF_LITTLE_FORMAT "ecoff-littlemips"
105#endif
106
107extern int target_big_endian;
108
252b5132
RH
109/* The name of the readonly data section. */
110#define RDATA_SECTION_NAME (OUTPUT_FLAVOR == bfd_target_aout_flavour \
111 ? ".data" \
112 : OUTPUT_FLAVOR == bfd_target_ecoff_flavour \
113 ? ".rdata" \
056350c6
NC
114 : OUTPUT_FLAVOR == bfd_target_coff_flavour \
115 ? ".rdata" \
252b5132
RH
116 : OUTPUT_FLAVOR == bfd_target_elf_flavour \
117 ? ".rodata" \
118 : (abort (), ""))
119
a325df1d
TS
120/* The ABI to use. */
121enum mips_abi_level
122{
123 NO_ABI = 0,
124 O32_ABI,
125 O64_ABI,
126 N32_ABI,
127 N64_ABI,
128 EABI_ABI
129};
130
131/* MIPS ABI we are using for this output file. */
316f5878 132static enum mips_abi_level mips_abi = NO_ABI;
a325df1d 133
252b5132
RH
134/* This is the set of options which may be modified by the .set
135 pseudo-op. We use a struct so that .set push and .set pop are more
136 reliable. */
137
e972090a
NC
138struct mips_set_options
139{
252b5132
RH
140 /* MIPS ISA (Instruction Set Architecture) level. This is set to -1
141 if it has not been initialized. Changed by `.set mipsN', and the
142 -mipsN command line option, and the default CPU. */
143 int isa;
1f25f5d3
CD
144 /* Enabled Application Specific Extensions (ASEs). These are set to -1
145 if they have not been initialized. Changed by `.set <asename>', by
146 command line options, and based on the default architecture. */
147 int ase_mips3d;
deec1734 148 int ase_mdmx;
252b5132
RH
149 /* Whether we are assembling for the mips16 processor. 0 if we are
150 not, 1 if we are, and -1 if the value has not been initialized.
151 Changed by `.set mips16' and `.set nomips16', and the -mips16 and
152 -nomips16 command line options, and the default CPU. */
153 int mips16;
154 /* Non-zero if we should not reorder instructions. Changed by `.set
155 reorder' and `.set noreorder'. */
156 int noreorder;
157 /* Non-zero if we should not permit the $at ($1) register to be used
158 in instructions. Changed by `.set at' and `.set noat'. */
159 int noat;
160 /* Non-zero if we should warn when a macro instruction expands into
161 more than one machine instruction. Changed by `.set nomacro' and
162 `.set macro'. */
163 int warn_about_macros;
164 /* Non-zero if we should not move instructions. Changed by `.set
165 move', `.set volatile', `.set nomove', and `.set novolatile'. */
166 int nomove;
167 /* Non-zero if we should not optimize branches by moving the target
168 of the branch into the delay slot. Actually, we don't perform
169 this optimization anyhow. Changed by `.set bopt' and `.set
170 nobopt'. */
171 int nobopt;
172 /* Non-zero if we should not autoextend mips16 instructions.
173 Changed by `.set autoextend' and `.set noautoextend'. */
174 int noautoextend;
a325df1d
TS
175 /* Restrict general purpose registers and floating point registers
176 to 32 bit. This is initially determined when -mgp32 or -mfp32
177 is passed but can changed if the assembler code uses .set mipsN. */
178 int gp32;
179 int fp32;
252b5132
RH
180};
181
a325df1d 182/* True if -mgp32 was passed. */
a8e8e863 183static int file_mips_gp32 = -1;
a325df1d
TS
184
185/* True if -mfp32 was passed. */
a8e8e863 186static int file_mips_fp32 = -1;
a325df1d 187
252b5132 188/* This is the struct we use to hold the current set of options. Note
a4672219 189 that we must set the isa field to ISA_UNKNOWN and the ASE fields to
e7af610e 190 -1 to indicate that they have not been initialized. */
252b5132 191
e972090a
NC
192static struct mips_set_options mips_opts =
193{
316f5878 194 ISA_UNKNOWN, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0
e7af610e 195};
252b5132
RH
196
197/* These variables are filled in with the masks of registers used.
198 The object format code reads them and puts them in the appropriate
199 place. */
200unsigned long mips_gprmask;
201unsigned long mips_cprmask[4];
202
203/* MIPS ISA we are using for this output file. */
e7af610e 204static int file_mips_isa = ISA_UNKNOWN;
252b5132 205
a4672219
TS
206/* True if -mips16 was passed or implied by arguments passed on the
207 command line (e.g., by -march). */
208static int file_ase_mips16;
209
1f25f5d3
CD
210/* True if -mips3d was passed or implied by arguments passed on the
211 command line (e.g., by -march). */
212static int file_ase_mips3d;
213
deec1734
CD
214/* True if -mdmx was passed or implied by arguments passed on the
215 command line (e.g., by -march). */
216static int file_ase_mdmx;
217
ec68c924
EC
218/* The argument of the -march= flag. The architecture we are assembling. */
219static int mips_arch = CPU_UNKNOWN;
316f5878
RS
220static const char *mips_arch_string;
221static const struct mips_cpu_info *mips_arch_info;
ec68c924
EC
222
223/* The argument of the -mtune= flag. The architecture for which we
224 are optimizing. */
225static int mips_tune = CPU_UNKNOWN;
316f5878
RS
226static const char *mips_tune_string;
227static const struct mips_cpu_info *mips_tune_info;
ec68c924 228
316f5878 229/* True when generating 32-bit code for a 64-bit processor. */
252b5132
RH
230static int mips_32bitmode = 0;
231
9ce8a5dd
GRK
232/* Some ISA's have delay slots for instructions which read or write
233 from a coprocessor (eg. mips1-mips3); some don't (eg mips4).
bdaaa2e1 234 Return true if instructions marked INSN_LOAD_COPROC_DELAY,
9ce8a5dd
GRK
235 INSN_COPROC_MOVE_DELAY, or INSN_WRITE_COND_CODE actually have a
236 delay slot in this ISA. The uses of this macro assume that any
237 ISA that has delay slots for one of these, has them for all. They
238 also assume that ISAs which don't have delays for these insns, don't
bdaaa2e1 239 have delays for the INSN_LOAD_MEMORY_DELAY instructions either. */
9ce8a5dd 240#define ISA_HAS_COPROC_DELAYS(ISA) ( \
e7af610e
NC
241 (ISA) == ISA_MIPS1 \
242 || (ISA) == ISA_MIPS2 \
243 || (ISA) == ISA_MIPS3 \
9ce8a5dd
GRK
244 )
245
316f5878
RS
246/* True if the given ABI requires 32-bit registers. */
247#define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
248
249/* Likewise 64-bit registers. */
250#define ABI_NEEDS_64BIT_REGS(ABI) \
251 ((ABI) == N32_ABI \
252 || (ABI) == N64_ABI \
253 || (ABI) == O64_ABI)
254
bdaaa2e1 255/* Return true if ISA supports 64 bit gp register instructions. */
9ce8a5dd 256#define ISA_HAS_64BIT_REGS(ISA) ( \
e7af610e
NC
257 (ISA) == ISA_MIPS3 \
258 || (ISA) == ISA_MIPS4 \
84ea6cf2 259 || (ISA) == ISA_MIPS5 \
d1cf510e 260 || (ISA) == ISA_MIPS64 \
9ce8a5dd
GRK
261 )
262
e013f690 263#define HAVE_32BIT_GPRS \
316f5878 264 (mips_opts.gp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
ca4e0257 265
e013f690 266#define HAVE_32BIT_FPRS \
316f5878 267 (mips_opts.fp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
ca4e0257
RS
268
269#define HAVE_64BIT_GPRS (! HAVE_32BIT_GPRS)
270#define HAVE_64BIT_FPRS (! HAVE_32BIT_FPRS)
271
316f5878 272#define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
e013f690 273
316f5878 274#define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
e013f690
TS
275
276/* We can only have 64bit addresses if the object file format
277 supports it. */
afdbd6d0
CD
278#define HAVE_32BIT_ADDRESSES \
279 (HAVE_32BIT_GPRS \
280 || ((bfd_arch_bits_per_address (stdoutput) == 32 \
281 || ! HAVE_64BIT_OBJECTS) \
282 && mips_pic != EMBEDDED_PIC))
e013f690
TS
283
284#define HAVE_64BIT_ADDRESSES (! HAVE_32BIT_ADDRESSES)
ca4e0257 285
a4672219
TS
286/* Return true if the given CPU supports the MIPS16 ASE. */
287#define CPU_HAS_MIPS16(cpu) \
288 (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0)
289
1f25f5d3
CD
290/* Return true if the given CPU supports the MIPS3D ASE. */
291#define CPU_HAS_MIPS3D(cpu) ((cpu) == CPU_SB1 \
292 )
293
deec1734 294/* Return true if the given CPU supports the MDMX ASE. */
a4672219 295#define CPU_HAS_MDMX(cpu) (false \
deec1734
CD
296 )
297
bdaaa2e1 298/* Whether the processor uses hardware interlocks to protect
252b5132 299 reads from the HI and LO registers, and thus does not
ec68c924 300 require nops to be inserted. */
252b5132 301
ec68c924 302#define hilo_interlocks (mips_arch == CPU_R4010 \
0a758a12 303 || mips_arch == CPU_SB1 \
252b5132
RH
304 )
305
306/* Whether the processor uses hardware interlocks to protect reads
307 from the GPRs, and thus does not require nops to be inserted. */
308#define gpr_interlocks \
e7af610e 309 (mips_opts.isa != ISA_MIPS1 \
ec68c924 310 || mips_arch == CPU_R3900)
252b5132
RH
311
312/* As with other "interlocks" this is used by hardware that has FP
313 (co-processor) interlocks. */
bdaaa2e1 314/* Itbl support may require additional care here. */
ec68c924 315#define cop_interlocks (mips_arch == CPU_R4300 \
0a758a12 316 || mips_arch == CPU_SB1 \
252b5132
RH
317 )
318
6b76fefe
CM
319/* Is this a mfhi or mflo instruction? */
320#define MF_HILO_INSN(PINFO) \
321 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
322
252b5132
RH
323/* MIPS PIC level. */
324
e972090a
NC
325enum mips_pic_level
326{
252b5132
RH
327 /* Do not generate PIC code. */
328 NO_PIC,
329
252b5132
RH
330 /* Generate PIC code as in the SVR4 MIPS ABI. */
331 SVR4_PIC,
332
333 /* Generate PIC code without using a global offset table: the data
334 segment has a maximum size of 64K, all data references are off
335 the $gp register, and all text references are PC relative. This
336 is used on some embedded systems. */
337 EMBEDDED_PIC
338};
339
340static enum mips_pic_level mips_pic;
341
39c0a331
L
342/* Warn about all NOPS that the assembler generates. */
343static int warn_nops = 0;
344
c9914766 345/* 1 if we should generate 32 bit offsets from the $gp register in
252b5132 346 SVR4_PIC mode. Currently has no meaning in other modes. */
c9914766 347static int mips_big_got = 0;
252b5132
RH
348
349/* 1 if trap instructions should used for overflow rather than break
350 instructions. */
c9914766 351static int mips_trap = 0;
252b5132 352
119d663a 353/* 1 if double width floating point constants should not be constructed
b6ff326e 354 by assembling two single width halves into two single width floating
119d663a
NC
355 point registers which just happen to alias the double width destination
356 register. On some architectures this aliasing can be disabled by a bit
d547a75e 357 in the status register, and the setting of this bit cannot be determined
119d663a
NC
358 automatically at assemble time. */
359static int mips_disable_float_construction;
360
252b5132
RH
361/* Non-zero if any .set noreorder directives were used. */
362
363static int mips_any_noreorder;
364
6b76fefe
CM
365/* Non-zero if nops should be inserted when the register referenced in
366 an mfhi/mflo instruction is read in the next two instructions. */
367static int mips_7000_hilo_fix;
368
252b5132 369/* The size of the small data section. */
156c2f8b 370static unsigned int g_switch_value = 8;
252b5132
RH
371/* Whether the -G option was used. */
372static int g_switch_seen = 0;
373
374#define N_RMASK 0xc4
375#define N_VFP 0xd4
376
377/* If we can determine in advance that GP optimization won't be
378 possible, we can skip the relaxation stuff that tries to produce
379 GP-relative references. This makes delay slot optimization work
380 better.
381
382 This function can only provide a guess, but it seems to work for
fba2b7f9
GK
383 gcc output. It needs to guess right for gcc, otherwise gcc
384 will put what it thinks is a GP-relative instruction in a branch
385 delay slot.
252b5132
RH
386
387 I don't know if a fix is needed for the SVR4_PIC mode. I've only
388 fixed it for the non-PIC mode. KR 95/04/07 */
389static int nopic_need_relax PARAMS ((symbolS *, int));
390
391/* handle of the OPCODE hash table */
392static struct hash_control *op_hash = NULL;
393
394/* The opcode hash table we use for the mips16. */
395static struct hash_control *mips16_op_hash = NULL;
396
397/* This array holds the chars that always start a comment. If the
398 pre-processor is disabled, these aren't very useful */
399const char comment_chars[] = "#";
400
401/* This array holds the chars that only start a comment at the beginning of
402 a line. If the line seems to have the form '# 123 filename'
403 .line and .file directives will appear in the pre-processed output */
404/* Note that input_file.c hand checks for '#' at the beginning of the
405 first line of the input file. This is because the compiler outputs
bdaaa2e1 406 #NO_APP at the beginning of its output. */
252b5132
RH
407/* Also note that C style comments are always supported. */
408const char line_comment_chars[] = "#";
409
bdaaa2e1 410/* This array holds machine specific line separator characters. */
63a0b638 411const char line_separator_chars[] = ";";
252b5132
RH
412
413/* Chars that can be used to separate mant from exp in floating point nums */
414const char EXP_CHARS[] = "eE";
415
416/* Chars that mean this number is a floating point constant */
417/* As in 0f12.456 */
418/* or 0d1.2345e12 */
419const char FLT_CHARS[] = "rRsSfFdDxXpP";
420
421/* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
422 changed in read.c . Ideally it shouldn't have to know about it at all,
423 but nothing is ideal around here.
424 */
425
426static char *insn_error;
427
428static int auto_align = 1;
429
430/* When outputting SVR4 PIC code, the assembler needs to know the
431 offset in the stack frame from which to restore the $gp register.
432 This is set by the .cprestore pseudo-op, and saved in this
433 variable. */
434static offsetT mips_cprestore_offset = -1;
435
6478892d
TS
436/* Similiar for NewABI PIC code, where $gp is callee-saved. NewABI has some
437 more optimizations, it can use a register value instead of a memory-saved
956cd1d6 438 offset and even an other register than $gp as global pointer. */
6478892d
TS
439static offsetT mips_cpreturn_offset = -1;
440static int mips_cpreturn_register = -1;
441static int mips_gp_register = GP;
def2e0dd 442static int mips_gprel_offset = 0;
6478892d 443
7a621144
DJ
444/* Whether mips_cprestore_offset has been set in the current function
445 (or whether it has already been warned about, if not). */
446static int mips_cprestore_valid = 0;
447
252b5132
RH
448/* This is the register which holds the stack frame, as set by the
449 .frame pseudo-op. This is needed to implement .cprestore. */
450static int mips_frame_reg = SP;
451
7a621144
DJ
452/* Whether mips_frame_reg has been set in the current function
453 (or whether it has already been warned about, if not). */
454static int mips_frame_reg_valid = 0;
455
252b5132
RH
456/* To output NOP instructions correctly, we need to keep information
457 about the previous two instructions. */
458
459/* Whether we are optimizing. The default value of 2 means to remove
460 unneeded NOPs and swap branch instructions when possible. A value
461 of 1 means to not swap branches. A value of 0 means to always
462 insert NOPs. */
463static int mips_optimize = 2;
464
465/* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
466 equivalent to seeing no -g option at all. */
467static int mips_debug = 0;
468
469/* The previous instruction. */
470static struct mips_cl_insn prev_insn;
471
472/* The instruction before prev_insn. */
473static struct mips_cl_insn prev_prev_insn;
474
475/* If we don't want information for prev_insn or prev_prev_insn, we
476 point the insn_mo field at this dummy integer. */
43841e91 477static const struct mips_opcode dummy_opcode = { NULL, NULL, 0, 0, 0, 0 };
252b5132
RH
478
479/* Non-zero if prev_insn is valid. */
480static int prev_insn_valid;
481
482/* The frag for the previous instruction. */
483static struct frag *prev_insn_frag;
484
485/* The offset into prev_insn_frag for the previous instruction. */
486static long prev_insn_where;
487
488/* The reloc type for the previous instruction, if any. */
f6688943 489static bfd_reloc_code_real_type prev_insn_reloc_type[3];
252b5132
RH
490
491/* The reloc for the previous instruction, if any. */
f6688943 492static fixS *prev_insn_fixp[3];
252b5132
RH
493
494/* Non-zero if the previous instruction was in a delay slot. */
495static int prev_insn_is_delay_slot;
496
497/* Non-zero if the previous instruction was in a .set noreorder. */
498static int prev_insn_unreordered;
499
500/* Non-zero if the previous instruction uses an extend opcode (if
501 mips16). */
502static int prev_insn_extended;
503
504/* Non-zero if the previous previous instruction was in a .set
505 noreorder. */
506static int prev_prev_insn_unreordered;
507
508/* If this is set, it points to a frag holding nop instructions which
509 were inserted before the start of a noreorder section. If those
510 nops turn out to be unnecessary, the size of the frag can be
511 decreased. */
512static fragS *prev_nop_frag;
513
514/* The number of nop instructions we created in prev_nop_frag. */
515static int prev_nop_frag_holds;
516
517/* The number of nop instructions that we know we need in
bdaaa2e1 518 prev_nop_frag. */
252b5132
RH
519static int prev_nop_frag_required;
520
521/* The number of instructions we've seen since prev_nop_frag. */
522static int prev_nop_frag_since;
523
524/* For ECOFF and ELF, relocations against symbols are done in two
525 parts, with a HI relocation and a LO relocation. Each relocation
526 has only 16 bits of space to store an addend. This means that in
527 order for the linker to handle carries correctly, it must be able
528 to locate both the HI and the LO relocation. This means that the
529 relocations must appear in order in the relocation table.
530
531 In order to implement this, we keep track of each unmatched HI
532 relocation. We then sort them so that they immediately precede the
bdaaa2e1 533 corresponding LO relocation. */
252b5132 534
e972090a
NC
535struct mips_hi_fixup
536{
252b5132
RH
537 /* Next HI fixup. */
538 struct mips_hi_fixup *next;
539 /* This fixup. */
540 fixS *fixp;
541 /* The section this fixup is in. */
542 segT seg;
543};
544
545/* The list of unmatched HI relocs. */
546
547static struct mips_hi_fixup *mips_hi_fixup_list;
548
549/* Map normal MIPS register numbers to mips16 register numbers. */
550
551#define X ILLEGAL_REG
e972090a
NC
552static const int mips32_to_16_reg_map[] =
553{
252b5132
RH
554 X, X, 2, 3, 4, 5, 6, 7,
555 X, X, X, X, X, X, X, X,
556 0, 1, X, X, X, X, X, X,
557 X, X, X, X, X, X, X, X
558};
559#undef X
560
561/* Map mips16 register numbers to normal MIPS register numbers. */
562
e972090a
NC
563static const unsigned int mips16_to_32_reg_map[] =
564{
252b5132
RH
565 16, 17, 2, 3, 4, 5, 6, 7
566};
567\f
568/* Since the MIPS does not have multiple forms of PC relative
569 instructions, we do not have to do relaxing as is done on other
570 platforms. However, we do have to handle GP relative addressing
571 correctly, which turns out to be a similar problem.
572
573 Every macro that refers to a symbol can occur in (at least) two
574 forms, one with GP relative addressing and one without. For
575 example, loading a global variable into a register generally uses
576 a macro instruction like this:
577 lw $4,i
578 If i can be addressed off the GP register (this is true if it is in
579 the .sbss or .sdata section, or if it is known to be smaller than
580 the -G argument) this will generate the following instruction:
581 lw $4,i($gp)
582 This instruction will use a GPREL reloc. If i can not be addressed
583 off the GP register, the following instruction sequence will be used:
584 lui $at,i
585 lw $4,i($at)
586 In this case the first instruction will have a HI16 reloc, and the
587 second reloc will have a LO16 reloc. Both relocs will be against
588 the symbol i.
589
590 The issue here is that we may not know whether i is GP addressable
591 until after we see the instruction that uses it. Therefore, we
592 want to be able to choose the final instruction sequence only at
593 the end of the assembly. This is similar to the way other
594 platforms choose the size of a PC relative instruction only at the
595 end of assembly.
596
597 When generating position independent code we do not use GP
598 addressing in quite the same way, but the issue still arises as
599 external symbols and local symbols must be handled differently.
600
601 We handle these issues by actually generating both possible
602 instruction sequences. The longer one is put in a frag_var with
603 type rs_machine_dependent. We encode what to do with the frag in
604 the subtype field. We encode (1) the number of existing bytes to
605 replace, (2) the number of new bytes to use, (3) the offset from
606 the start of the existing bytes to the first reloc we must generate
607 (that is, the offset is applied from the start of the existing
608 bytes after they are replaced by the new bytes, if any), (4) the
609 offset from the start of the existing bytes to the second reloc,
610 (5) whether a third reloc is needed (the third reloc is always four
611 bytes after the second reloc), and (6) whether to warn if this
612 variant is used (this is sometimes needed if .set nomacro or .set
613 noat is in effect). All these numbers are reasonably small.
614
615 Generating two instruction sequences must be handled carefully to
616 ensure that delay slots are handled correctly. Fortunately, there
617 are a limited number of cases. When the second instruction
618 sequence is generated, append_insn is directed to maintain the
619 existing delay slot information, so it continues to apply to any
620 code after the second instruction sequence. This means that the
621 second instruction sequence must not impose any requirements not
622 required by the first instruction sequence.
623
624 These variant frags are then handled in functions called by the
625 machine independent code. md_estimate_size_before_relax returns
626 the final size of the frag. md_convert_frag sets up the final form
627 of the frag. tc_gen_reloc adjust the first reloc and adds a second
628 one if needed. */
629#define RELAX_ENCODE(old, new, reloc1, reloc2, reloc3, warn) \
630 ((relax_substateT) \
631 (((old) << 23) \
632 | ((new) << 16) \
633 | (((reloc1) + 64) << 9) \
634 | (((reloc2) + 64) << 2) \
635 | ((reloc3) ? (1 << 1) : 0) \
636 | ((warn) ? 1 : 0)))
637#define RELAX_OLD(i) (((i) >> 23) & 0x7f)
638#define RELAX_NEW(i) (((i) >> 16) & 0x7f)
9a41af64
TS
639#define RELAX_RELOC1(i) ((valueT) (((i) >> 9) & 0x7f) - 64)
640#define RELAX_RELOC2(i) ((valueT) (((i) >> 2) & 0x7f) - 64)
252b5132
RH
641#define RELAX_RELOC3(i) (((i) >> 1) & 1)
642#define RELAX_WARN(i) ((i) & 1)
643
644/* For mips16 code, we use an entirely different form of relaxation.
645 mips16 supports two versions of most instructions which take
646 immediate values: a small one which takes some small value, and a
647 larger one which takes a 16 bit value. Since branches also follow
648 this pattern, relaxing these values is required.
649
650 We can assemble both mips16 and normal MIPS code in a single
651 object. Therefore, we need to support this type of relaxation at
652 the same time that we support the relaxation described above. We
653 use the high bit of the subtype field to distinguish these cases.
654
655 The information we store for this type of relaxation is the
656 argument code found in the opcode file for this relocation, whether
657 the user explicitly requested a small or extended form, and whether
658 the relocation is in a jump or jal delay slot. That tells us the
659 size of the value, and how it should be stored. We also store
660 whether the fragment is considered to be extended or not. We also
661 store whether this is known to be a branch to a different section,
662 whether we have tried to relax this frag yet, and whether we have
663 ever extended a PC relative fragment because of a shift count. */
664#define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
665 (0x80000000 \
666 | ((type) & 0xff) \
667 | ((small) ? 0x100 : 0) \
668 | ((ext) ? 0x200 : 0) \
669 | ((dslot) ? 0x400 : 0) \
670 | ((jal_dslot) ? 0x800 : 0))
671#define RELAX_MIPS16_P(i) (((i) & 0x80000000) != 0)
672#define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
673#define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
674#define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
675#define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
676#define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
677#define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
678#define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
679#define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
680#define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
681#define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
682#define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
683\f
684/* Prototypes for static functions. */
685
686#ifdef __STDC__
687#define internalError() \
688 as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
689#else
690#define internalError() as_fatal (_("MIPS internal Error"));
691#endif
692
693enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
694
695static int insn_uses_reg PARAMS ((struct mips_cl_insn *ip,
696 unsigned int reg, enum mips_regclass class));
156c2f8b 697static int reg_needs_delay PARAMS ((unsigned int));
252b5132
RH
698static void mips16_mark_labels PARAMS ((void));
699static void append_insn PARAMS ((char *place,
700 struct mips_cl_insn * ip,
701 expressionS * p,
f6688943 702 bfd_reloc_code_real_type *r,
252b5132
RH
703 boolean));
704static void mips_no_prev_insn PARAMS ((int));
705static void mips_emit_delays PARAMS ((boolean));
706#ifdef USE_STDARG
707static void macro_build PARAMS ((char *place, int *counter, expressionS * ep,
708 const char *name, const char *fmt,
709 ...));
710#else
711static void macro_build ();
712#endif
713static void mips16_macro_build PARAMS ((char *, int *, expressionS *,
714 const char *, const char *,
715 va_list));
438c16b8 716static void macro_build_jalr PARAMS ((int, expressionS *));
252b5132
RH
717static void macro_build_lui PARAMS ((char *place, int *counter,
718 expressionS * ep, int regnum));
719static void set_at PARAMS ((int *counter, int reg, int unsignedp));
720static void check_absolute_expr PARAMS ((struct mips_cl_insn * ip,
721 expressionS *));
722static void load_register PARAMS ((int *, int, expressionS *, int));
c9914766 723static void load_address PARAMS ((int *, int, expressionS *, int *));
ea1fb5dc 724static void move_register PARAMS ((int *, int, int));
252b5132
RH
725static void macro PARAMS ((struct mips_cl_insn * ip));
726static void mips16_macro PARAMS ((struct mips_cl_insn * ip));
727#ifdef LOSING_COMPILER
728static void macro2 PARAMS ((struct mips_cl_insn * ip));
729#endif
730static void mips_ip PARAMS ((char *str, struct mips_cl_insn * ip));
731static void mips16_ip PARAMS ((char *str, struct mips_cl_insn * ip));
732static void mips16_immed PARAMS ((char *, unsigned int, int, offsetT, boolean,
733 boolean, boolean, unsigned long *,
734 boolean *, unsigned short *));
394f9b3a 735static int my_getPercentOp PARAMS ((char **, unsigned int *, int *));
ad8d3bb3
TS
736static int my_getSmallParser PARAMS ((char **, unsigned int *, int *));
737static int my_getSmallExpression PARAMS ((expressionS *, char *));
738static void my_getExpression PARAMS ((expressionS *, char *));
ae948b86 739#ifdef OBJ_ELF
e013f690 740static int support_64bit_objects PARAMS((void));
ae948b86 741#endif
316f5878 742static void mips_set_option_string PARAMS ((const char **, const char *));
252b5132
RH
743static symbolS *get_symbol PARAMS ((void));
744static void mips_align PARAMS ((int to, int fill, symbolS *label));
745static void s_align PARAMS ((int));
746static void s_change_sec PARAMS ((int));
747static void s_cons PARAMS ((int));
748static void s_float_cons PARAMS ((int));
749static void s_mips_globl PARAMS ((int));
750static void s_option PARAMS ((int));
751static void s_mipsset PARAMS ((int));
752static void s_abicalls PARAMS ((int));
753static void s_cpload PARAMS ((int));
6478892d
TS
754static void s_cpsetup PARAMS ((int));
755static void s_cplocal PARAMS ((int));
252b5132 756static void s_cprestore PARAMS ((int));
6478892d
TS
757static void s_cpreturn PARAMS ((int));
758static void s_gpvalue PARAMS ((int));
252b5132
RH
759static void s_gpword PARAMS ((int));
760static void s_cpadd PARAMS ((int));
761static void s_insn PARAMS ((int));
762static void md_obj_begin PARAMS ((void));
763static void md_obj_end PARAMS ((void));
764static long get_number PARAMS ((void));
765static void s_mips_ent PARAMS ((int));
766static void s_mips_end PARAMS ((int));
767static void s_mips_frame PARAMS ((int));
768static void s_mips_mask PARAMS ((int));
769static void s_mips_stab PARAMS ((int));
770static void s_mips_weakext PARAMS ((int));
c5dd6aab
DJ
771static void s_mips_file PARAMS ((int));
772static void s_mips_loc PARAMS ((int));
252b5132 773static int mips16_extended_frag PARAMS ((fragS *, asection *, long));
252b5132 774static int validate_mips_insn PARAMS ((const struct mips_opcode *));
316f5878 775static void show PARAMS ((FILE *, const char *, int *, int *));
add55e1f
RS
776#ifdef OBJ_ELF
777static int mips_need_elf_addend_fixup PARAMS ((fixS *));
778#endif
e7af610e 779
ad8d3bb3 780/* Return values of my_getSmallExpression(). */
fb1b3232 781
ad8d3bb3 782enum small_ex_type
fb1b3232
TS
783{
784 S_EX_NONE = 0,
ad8d3bb3
TS
785 S_EX_REGISTER,
786
787 /* Direct relocation creation by %percent_op(). */
788 S_EX_HALF,
fb1b3232 789 S_EX_HI,
ad8d3bb3
TS
790 S_EX_LO,
791 S_EX_GP_REL,
792 S_EX_GOT,
793 S_EX_CALL16,
794 S_EX_GOT_DISP,
795 S_EX_GOT_PAGE,
796 S_EX_GOT_OFST,
797 S_EX_GOT_HI,
798 S_EX_GOT_LO,
799 S_EX_NEG,
fb1b3232
TS
800 S_EX_HIGHER,
801 S_EX_HIGHEST,
ad8d3bb3
TS
802 S_EX_CALL_HI,
803 S_EX_CALL_LO
fb1b3232
TS
804};
805
e7af610e
NC
806/* Table and functions used to map between CPU/ISA names, and
807 ISA levels, and CPU numbers. */
808
e972090a
NC
809struct mips_cpu_info
810{
e7af610e
NC
811 const char *name; /* CPU or ISA name. */
812 int is_isa; /* Is this an ISA? (If 0, a CPU.) */
813 int isa; /* ISA level. */
814 int cpu; /* CPU number (default CPU if ISA). */
815};
816
316f5878
RS
817static void mips_set_architecture PARAMS ((const struct mips_cpu_info *));
818static void mips_set_tune PARAMS ((const struct mips_cpu_info *));
819static boolean mips_strict_matching_cpu_name_p PARAMS ((const char *,
820 const char *));
821static boolean mips_matching_cpu_name_p PARAMS ((const char *, const char *));
822static const struct mips_cpu_info *mips_parse_cpu PARAMS ((const char *,
823 const char *));
e7af610e 824static const struct mips_cpu_info *mips_cpu_info_from_isa PARAMS ((int));
252b5132
RH
825\f
826/* Pseudo-op table.
827
828 The following pseudo-ops from the Kane and Heinrich MIPS book
829 should be defined here, but are currently unsupported: .alias,
830 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
831
832 The following pseudo-ops from the Kane and Heinrich MIPS book are
833 specific to the type of debugging information being generated, and
834 should be defined by the object format: .aent, .begin, .bend,
835 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
836 .vreg.
837
838 The following pseudo-ops from the Kane and Heinrich MIPS book are
839 not MIPS CPU specific, but are also not specific to the object file
840 format. This file is probably the best place to define them, but
841 they are not currently supported: .asm0, .endr, .lab, .repeat,
842 .struct. */
843
e972090a
NC
844static const pseudo_typeS mips_pseudo_table[] =
845{
beae10d5 846 /* MIPS specific pseudo-ops. */
252b5132
RH
847 {"option", s_option, 0},
848 {"set", s_mipsset, 0},
849 {"rdata", s_change_sec, 'r'},
850 {"sdata", s_change_sec, 's'},
851 {"livereg", s_ignore, 0},
852 {"abicalls", s_abicalls, 0},
853 {"cpload", s_cpload, 0},
6478892d
TS
854 {"cpsetup", s_cpsetup, 0},
855 {"cplocal", s_cplocal, 0},
252b5132 856 {"cprestore", s_cprestore, 0},
6478892d
TS
857 {"cpreturn", s_cpreturn, 0},
858 {"gpvalue", s_gpvalue, 0},
252b5132
RH
859 {"gpword", s_gpword, 0},
860 {"cpadd", s_cpadd, 0},
861 {"insn", s_insn, 0},
862
beae10d5 863 /* Relatively generic pseudo-ops that happen to be used on MIPS
252b5132
RH
864 chips. */
865 {"asciiz", stringer, 1},
866 {"bss", s_change_sec, 'b'},
867 {"err", s_err, 0},
868 {"half", s_cons, 1},
869 {"dword", s_cons, 3},
870 {"weakext", s_mips_weakext, 0},
871
beae10d5 872 /* These pseudo-ops are defined in read.c, but must be overridden
252b5132
RH
873 here for one reason or another. */
874 {"align", s_align, 0},
875 {"byte", s_cons, 0},
876 {"data", s_change_sec, 'd'},
877 {"double", s_float_cons, 'd'},
878 {"float", s_float_cons, 'f'},
879 {"globl", s_mips_globl, 0},
880 {"global", s_mips_globl, 0},
881 {"hword", s_cons, 1},
882 {"int", s_cons, 2},
883 {"long", s_cons, 2},
884 {"octa", s_cons, 4},
885 {"quad", s_cons, 3},
886 {"short", s_cons, 1},
887 {"single", s_float_cons, 'f'},
888 {"stabn", s_mips_stab, 'n'},
889 {"text", s_change_sec, 't'},
890 {"word", s_cons, 2},
add56521 891
add56521 892 { "extern", ecoff_directive_extern, 0},
add56521 893
43841e91 894 { NULL, NULL, 0 },
252b5132
RH
895};
896
e972090a
NC
897static const pseudo_typeS mips_nonecoff_pseudo_table[] =
898{
beae10d5
KH
899 /* These pseudo-ops should be defined by the object file format.
900 However, a.out doesn't support them, so we have versions here. */
252b5132
RH
901 {"aent", s_mips_ent, 1},
902 {"bgnb", s_ignore, 0},
903 {"end", s_mips_end, 0},
904 {"endb", s_ignore, 0},
905 {"ent", s_mips_ent, 0},
c5dd6aab 906 {"file", s_mips_file, 0},
252b5132
RH
907 {"fmask", s_mips_mask, 'F'},
908 {"frame", s_mips_frame, 0},
c5dd6aab 909 {"loc", s_mips_loc, 0},
252b5132
RH
910 {"mask", s_mips_mask, 'R'},
911 {"verstamp", s_ignore, 0},
43841e91 912 { NULL, NULL, 0 },
252b5132
RH
913};
914
915extern void pop_insert PARAMS ((const pseudo_typeS *));
916
917void
918mips_pop_insert ()
919{
920 pop_insert (mips_pseudo_table);
921 if (! ECOFF_DEBUGGING)
922 pop_insert (mips_nonecoff_pseudo_table);
923}
924\f
925/* Symbols labelling the current insn. */
926
e972090a
NC
927struct insn_label_list
928{
252b5132
RH
929 struct insn_label_list *next;
930 symbolS *label;
931};
932
933static struct insn_label_list *insn_labels;
934static struct insn_label_list *free_insn_labels;
935
936static void mips_clear_insn_labels PARAMS ((void));
937
938static inline void
939mips_clear_insn_labels ()
940{
941 register struct insn_label_list **pl;
942
943 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
944 ;
945 *pl = insn_labels;
946 insn_labels = NULL;
947}
948\f
949static char *expr_end;
950
951/* Expressions which appear in instructions. These are set by
952 mips_ip. */
953
954static expressionS imm_expr;
955static expressionS offset_expr;
956
957/* Relocs associated with imm_expr and offset_expr. */
958
f6688943
TS
959static bfd_reloc_code_real_type imm_reloc[3]
960 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
961static bfd_reloc_code_real_type offset_reloc[3]
962 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132
RH
963
964/* This is set by mips_ip if imm_reloc is an unmatched HI16_S reloc. */
965
966static boolean imm_unmatched_hi;
967
968/* These are set by mips16_ip if an explicit extension is used. */
969
970static boolean mips16_small, mips16_ext;
971
ecb4347a
DJ
972/* The pdr segment for per procedure frame/regmask info. Not used for
973 ECOFF debugging. */
252b5132
RH
974
975static segT pdr_seg;
252b5132 976
e013f690
TS
977/* The default target format to use. */
978
979const char *
980mips_target_format ()
981{
982 switch (OUTPUT_FLAVOR)
983 {
984 case bfd_target_aout_flavour:
985 return target_big_endian ? "a.out-mips-big" : "a.out-mips-little";
986 case bfd_target_ecoff_flavour:
987 return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
988 case bfd_target_coff_flavour:
989 return "pe-mips";
990 case bfd_target_elf_flavour:
991#ifdef TE_TMIPS
cfe86eaa 992 /* This is traditional mips. */
e013f690 993 return (target_big_endian
cfe86eaa
TS
994 ? (HAVE_64BIT_OBJECTS
995 ? "elf64-tradbigmips"
996 : (HAVE_NEWABI
997 ? "elf32-ntradbigmips" : "elf32-tradbigmips"))
998 : (HAVE_64BIT_OBJECTS
999 ? "elf64-tradlittlemips"
1000 : (HAVE_NEWABI
1001 ? "elf32-ntradlittlemips" : "elf32-tradlittlemips")));
e013f690
TS
1002#else
1003 return (target_big_endian
cfe86eaa
TS
1004 ? (HAVE_64BIT_OBJECTS
1005 ? "elf64-bigmips"
1006 : (HAVE_NEWABI
1007 ? "elf32-nbigmips" : "elf32-bigmips"))
1008 : (HAVE_64BIT_OBJECTS
1009 ? "elf64-littlemips"
1010 : (HAVE_NEWABI
1011 ? "elf32-nlittlemips" : "elf32-littlemips")));
e013f690
TS
1012#endif
1013 default:
1014 abort ();
1015 return NULL;
1016 }
1017}
1018
156c2f8b
NC
1019/* This function is called once, at assembler startup time. It should
1020 set up all the tables, etc. that the MD part of the assembler will need. */
1021
252b5132
RH
1022void
1023md_begin ()
1024{
252b5132 1025 register const char *retval = NULL;
156c2f8b 1026 int i = 0;
252b5132 1027 int broken = 0;
1f25f5d3 1028
ec68c924 1029 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, mips_arch))
252b5132
RH
1030 as_warn (_("Could not set architecture and machine"));
1031
252b5132
RH
1032 op_hash = hash_new ();
1033
1034 for (i = 0; i < NUMOPCODES;)
1035 {
1036 const char *name = mips_opcodes[i].name;
1037
1038 retval = hash_insert (op_hash, name, (PTR) &mips_opcodes[i]);
1039 if (retval != NULL)
1040 {
1041 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1042 mips_opcodes[i].name, retval);
1043 /* Probably a memory allocation problem? Give up now. */
1044 as_fatal (_("Broken assembler. No assembly attempted."));
1045 }
1046 do
1047 {
1048 if (mips_opcodes[i].pinfo != INSN_MACRO)
1049 {
1050 if (!validate_mips_insn (&mips_opcodes[i]))
1051 broken = 1;
1052 }
1053 ++i;
1054 }
1055 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1056 }
1057
1058 mips16_op_hash = hash_new ();
1059
1060 i = 0;
1061 while (i < bfd_mips16_num_opcodes)
1062 {
1063 const char *name = mips16_opcodes[i].name;
1064
1065 retval = hash_insert (mips16_op_hash, name, (PTR) &mips16_opcodes[i]);
1066 if (retval != NULL)
1067 as_fatal (_("internal: can't hash `%s': %s"),
1068 mips16_opcodes[i].name, retval);
1069 do
1070 {
1071 if (mips16_opcodes[i].pinfo != INSN_MACRO
1072 && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1073 != mips16_opcodes[i].match))
1074 {
1075 fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1076 mips16_opcodes[i].name, mips16_opcodes[i].args);
1077 broken = 1;
1078 }
1079 ++i;
1080 }
1081 while (i < bfd_mips16_num_opcodes
1082 && strcmp (mips16_opcodes[i].name, name) == 0);
1083 }
1084
1085 if (broken)
1086 as_fatal (_("Broken assembler. No assembly attempted."));
1087
1088 /* We add all the general register names to the symbol table. This
1089 helps us detect invalid uses of them. */
1090 for (i = 0; i < 32; i++)
1091 {
1092 char buf[5];
1093
1094 sprintf (buf, "$%d", i);
1095 symbol_table_insert (symbol_new (buf, reg_section, i,
1096 &zero_address_frag));
1097 }
76db943d
TS
1098 symbol_table_insert (symbol_new ("$ra", reg_section, RA,
1099 &zero_address_frag));
252b5132
RH
1100 symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1101 &zero_address_frag));
1102 symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1103 &zero_address_frag));
1104 symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1105 &zero_address_frag));
1106 symbol_table_insert (symbol_new ("$at", reg_section, AT,
1107 &zero_address_frag));
1108 symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1109 &zero_address_frag));
1110 symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1111 &zero_address_frag));
85b51719
TS
1112 symbol_table_insert (symbol_new ("$zero", reg_section, ZERO,
1113 &zero_address_frag));
252b5132
RH
1114 symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1115 &zero_address_frag));
1116
1117 mips_no_prev_insn (false);
1118
1119 mips_gprmask = 0;
1120 mips_cprmask[0] = 0;
1121 mips_cprmask[1] = 0;
1122 mips_cprmask[2] = 0;
1123 mips_cprmask[3] = 0;
1124
1125 /* set the default alignment for the text section (2**2) */
1126 record_alignment (text_section, 2);
1127
1128 if (USE_GLOBAL_POINTER_OPT)
1129 bfd_set_gp_size (stdoutput, g_switch_value);
1130
1131 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1132 {
1133 /* On a native system, sections must be aligned to 16 byte
1134 boundaries. When configured for an embedded ELF target, we
1135 don't bother. */
1136 if (strcmp (TARGET_OS, "elf") != 0)
1137 {
1138 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1139 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1140 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1141 }
1142
1143 /* Create a .reginfo section for register masks and a .mdebug
1144 section for debugging information. */
1145 {
1146 segT seg;
1147 subsegT subseg;
1148 flagword flags;
1149 segT sec;
1150
1151 seg = now_seg;
1152 subseg = now_subseg;
1153
1154 /* The ABI says this section should be loaded so that the
1155 running program can access it. However, we don't load it
1156 if we are configured for an embedded target */
1157 flags = SEC_READONLY | SEC_DATA;
1158 if (strcmp (TARGET_OS, "elf") != 0)
1159 flags |= SEC_ALLOC | SEC_LOAD;
1160
316f5878 1161 if (mips_abi != N64_ABI)
252b5132
RH
1162 {
1163 sec = subseg_new (".reginfo", (subsegT) 0);
1164
195325d2
TS
1165 bfd_set_section_flags (stdoutput, sec, flags);
1166 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
bdaaa2e1 1167
252b5132
RH
1168#ifdef OBJ_ELF
1169 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1170#endif
1171 }
1172 else
1173 {
1174 /* The 64-bit ABI uses a .MIPS.options section rather than
1175 .reginfo section. */
1176 sec = subseg_new (".MIPS.options", (subsegT) 0);
195325d2
TS
1177 bfd_set_section_flags (stdoutput, sec, flags);
1178 bfd_set_section_alignment (stdoutput, sec, 3);
252b5132
RH
1179
1180#ifdef OBJ_ELF
1181 /* Set up the option header. */
1182 {
1183 Elf_Internal_Options opthdr;
1184 char *f;
1185
1186 opthdr.kind = ODK_REGINFO;
1187 opthdr.size = (sizeof (Elf_External_Options)
1188 + sizeof (Elf64_External_RegInfo));
1189 opthdr.section = 0;
1190 opthdr.info = 0;
1191 f = frag_more (sizeof (Elf_External_Options));
1192 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1193 (Elf_External_Options *) f);
1194
1195 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1196 }
1197#endif
1198 }
1199
1200 if (ECOFF_DEBUGGING)
1201 {
1202 sec = subseg_new (".mdebug", (subsegT) 0);
1203 (void) bfd_set_section_flags (stdoutput, sec,
1204 SEC_HAS_CONTENTS | SEC_READONLY);
1205 (void) bfd_set_section_alignment (stdoutput, sec, 2);
1206 }
ecb4347a
DJ
1207#ifdef OBJ_ELF
1208 else if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1209 {
1210 pdr_seg = subseg_new (".pdr", (subsegT) 0);
1211 (void) bfd_set_section_flags (stdoutput, pdr_seg,
1212 SEC_READONLY | SEC_RELOC
1213 | SEC_DEBUGGING);
1214 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1215 }
252b5132
RH
1216#endif
1217
1218 subseg_set (seg, subseg);
1219 }
1220 }
1221
1222 if (! ECOFF_DEBUGGING)
1223 md_obj_begin ();
1224}
1225
1226void
1227md_mips_end ()
1228{
1229 if (! ECOFF_DEBUGGING)
1230 md_obj_end ();
1231}
1232
1233void
1234md_assemble (str)
1235 char *str;
1236{
1237 struct mips_cl_insn insn;
f6688943
TS
1238 bfd_reloc_code_real_type unused_reloc[3]
1239 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132
RH
1240
1241 imm_expr.X_op = O_absent;
252b5132
RH
1242 imm_unmatched_hi = false;
1243 offset_expr.X_op = O_absent;
f6688943
TS
1244 imm_reloc[0] = BFD_RELOC_UNUSED;
1245 imm_reloc[1] = BFD_RELOC_UNUSED;
1246 imm_reloc[2] = BFD_RELOC_UNUSED;
1247 offset_reloc[0] = BFD_RELOC_UNUSED;
1248 offset_reloc[1] = BFD_RELOC_UNUSED;
1249 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
1250
1251 if (mips_opts.mips16)
1252 mips16_ip (str, &insn);
1253 else
1254 {
1255 mips_ip (str, &insn);
beae10d5
KH
1256 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1257 str, insn.insn_opcode));
252b5132
RH
1258 }
1259
1260 if (insn_error)
1261 {
1262 as_bad ("%s `%s'", insn_error, str);
1263 return;
1264 }
1265
1266 if (insn.insn_mo->pinfo == INSN_MACRO)
1267 {
1268 if (mips_opts.mips16)
1269 mips16_macro (&insn);
1270 else
1271 macro (&insn);
1272 }
1273 else
1274 {
1275 if (imm_expr.X_op != O_absent)
c4e7957c 1276 append_insn (NULL, &insn, &imm_expr, imm_reloc, imm_unmatched_hi);
252b5132 1277 else if (offset_expr.X_op != O_absent)
c4e7957c 1278 append_insn (NULL, &insn, &offset_expr, offset_reloc, false);
252b5132 1279 else
c4e7957c 1280 append_insn (NULL, &insn, NULL, unused_reloc, false);
252b5132
RH
1281 }
1282}
1283
1284/* See whether instruction IP reads register REG. CLASS is the type
1285 of register. */
1286
1287static int
1288insn_uses_reg (ip, reg, class)
1289 struct mips_cl_insn *ip;
1290 unsigned int reg;
1291 enum mips_regclass class;
1292{
1293 if (class == MIPS16_REG)
1294 {
1295 assert (mips_opts.mips16);
1296 reg = mips16_to_32_reg_map[reg];
1297 class = MIPS_GR_REG;
1298 }
1299
85b51719
TS
1300 /* Don't report on general register ZERO, since it never changes. */
1301 if (class == MIPS_GR_REG && reg == ZERO)
252b5132
RH
1302 return 0;
1303
1304 if (class == MIPS_FP_REG)
1305 {
1306 assert (! mips_opts.mips16);
1307 /* If we are called with either $f0 or $f1, we must check $f0.
1308 This is not optimal, because it will introduce an unnecessary
1309 NOP between "lwc1 $f0" and "swc1 $f1". To fix this we would
1310 need to distinguish reading both $f0 and $f1 or just one of
1311 them. Note that we don't have to check the other way,
1312 because there is no instruction that sets both $f0 and $f1
1313 and requires a delay. */
1314 if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1315 && ((((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS) &~(unsigned)1)
1316 == (reg &~ (unsigned) 1)))
1317 return 1;
1318 if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1319 && ((((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT) &~(unsigned)1)
1320 == (reg &~ (unsigned) 1)))
1321 return 1;
1322 }
1323 else if (! mips_opts.mips16)
1324 {
1325 if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1326 && ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == reg)
1327 return 1;
1328 if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1329 && ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT) == reg)
1330 return 1;
1331 }
1332 else
1333 {
1334 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1335 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RX)
1336 & MIPS16OP_MASK_RX)]
1337 == reg))
1338 return 1;
1339 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1340 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RY)
1341 & MIPS16OP_MASK_RY)]
1342 == reg))
1343 return 1;
1344 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1345 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
1346 & MIPS16OP_MASK_MOVE32Z)]
1347 == reg))
1348 return 1;
1349 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1350 return 1;
1351 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1352 return 1;
1353 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1354 return 1;
1355 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1356 && ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
1357 & MIPS16OP_MASK_REGR32) == reg)
1358 return 1;
1359 }
1360
1361 return 0;
1362}
1363
1364/* This function returns true if modifying a register requires a
1365 delay. */
1366
1367static int
1368reg_needs_delay (reg)
156c2f8b 1369 unsigned int reg;
252b5132
RH
1370{
1371 unsigned long prev_pinfo;
1372
1373 prev_pinfo = prev_insn.insn_mo->pinfo;
1374 if (! mips_opts.noreorder
9ce8a5dd 1375 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
1376 && ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1377 || (! gpr_interlocks
1378 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1379 {
1380 /* A load from a coprocessor or from memory. All load
1381 delays delay the use of general register rt for one
1382 instruction on the r3000. The r6000 and r4000 use
1383 interlocks. */
bdaaa2e1 1384 /* Itbl support may require additional care here. */
252b5132
RH
1385 know (prev_pinfo & INSN_WRITE_GPR_T);
1386 if (reg == ((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT))
1387 return 1;
1388 }
1389
1390 return 0;
1391}
1392
1393/* Mark instruction labels in mips16 mode. This permits the linker to
1394 handle them specially, such as generating jalx instructions when
1395 needed. We also make them odd for the duration of the assembly, in
1396 order to generate the right sort of code. We will make them even
1397 in the adjust_symtab routine, while leaving them marked. This is
1398 convenient for the debugger and the disassembler. The linker knows
1399 to make them odd again. */
1400
1401static void
1402mips16_mark_labels ()
1403{
1404 if (mips_opts.mips16)
1405 {
1406 struct insn_label_list *l;
98aa84af 1407 valueT val;
252b5132
RH
1408
1409 for (l = insn_labels; l != NULL; l = l->next)
1410 {
1411#ifdef OBJ_ELF
1412 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1413 S_SET_OTHER (l->label, STO_MIPS16);
1414#endif
98aa84af
AM
1415 val = S_GET_VALUE (l->label);
1416 if ((val & 1) == 0)
1417 S_SET_VALUE (l->label, val + 1);
252b5132
RH
1418 }
1419 }
1420}
1421
1422/* Output an instruction. PLACE is where to put the instruction; if
1423 it is NULL, this uses frag_more to get room. IP is the instruction
1424 information. ADDRESS_EXPR is an operand of the instruction to be
1425 used with RELOC_TYPE. */
1426
1427static void
1428append_insn (place, ip, address_expr, reloc_type, unmatched_hi)
1429 char *place;
1430 struct mips_cl_insn *ip;
1431 expressionS *address_expr;
f6688943 1432 bfd_reloc_code_real_type *reloc_type;
252b5132
RH
1433 boolean unmatched_hi;
1434{
1435 register unsigned long prev_pinfo, pinfo;
1436 char *f;
f6688943 1437 fixS *fixp[3];
252b5132
RH
1438 int nops = 0;
1439
1440 /* Mark instruction labels in mips16 mode. */
f9419b05 1441 mips16_mark_labels ();
252b5132
RH
1442
1443 prev_pinfo = prev_insn.insn_mo->pinfo;
1444 pinfo = ip->insn_mo->pinfo;
1445
1446 if (place == NULL && (! mips_opts.noreorder || prev_nop_frag != NULL))
1447 {
1448 int prev_prev_nop;
1449
1450 /* If the previous insn required any delay slots, see if we need
1451 to insert a NOP or two. There are eight kinds of possible
1452 hazards, of which an instruction can have at most one type.
1453 (1) a load from memory delay
1454 (2) a load from a coprocessor delay
1455 (3) an unconditional branch delay
1456 (4) a conditional branch delay
1457 (5) a move to coprocessor register delay
1458 (6) a load coprocessor register from memory delay
1459 (7) a coprocessor condition code delay
1460 (8) a HI/LO special register delay
1461
1462 There are a lot of optimizations we could do that we don't.
1463 In particular, we do not, in general, reorder instructions.
1464 If you use gcc with optimization, it will reorder
1465 instructions and generally do much more optimization then we
1466 do here; repeating all that work in the assembler would only
1467 benefit hand written assembly code, and does not seem worth
1468 it. */
1469
1470 /* This is how a NOP is emitted. */
1471#define emit_nop() \
1472 (mips_opts.mips16 \
1473 ? md_number_to_chars (frag_more (2), 0x6500, 2) \
1474 : md_number_to_chars (frag_more (4), 0, 4))
1475
1476 /* The previous insn might require a delay slot, depending upon
1477 the contents of the current insn. */
1478 if (! mips_opts.mips16
9ce8a5dd 1479 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
1480 && (((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1481 && ! cop_interlocks)
1482 || (! gpr_interlocks
1483 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1484 {
1485 /* A load from a coprocessor or from memory. All load
1486 delays delay the use of general register rt for one
1487 instruction on the r3000. The r6000 and r4000 use
1488 interlocks. */
beae10d5 1489 /* Itbl support may require additional care here. */
252b5132
RH
1490 know (prev_pinfo & INSN_WRITE_GPR_T);
1491 if (mips_optimize == 0
1492 || insn_uses_reg (ip,
1493 ((prev_insn.insn_opcode >> OP_SH_RT)
1494 & OP_MASK_RT),
1495 MIPS_GR_REG))
1496 ++nops;
1497 }
1498 else if (! mips_opts.mips16
9ce8a5dd 1499 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132 1500 && (((prev_pinfo & INSN_COPROC_MOVE_DELAY)
beae10d5 1501 && ! cop_interlocks)
e7af610e 1502 || (mips_opts.isa == ISA_MIPS1
252b5132
RH
1503 && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))))
1504 {
1505 /* A generic coprocessor delay. The previous instruction
1506 modified a coprocessor general or control register. If
1507 it modified a control register, we need to avoid any
1508 coprocessor instruction (this is probably not always
1509 required, but it sometimes is). If it modified a general
1510 register, we avoid using that register.
1511
1512 On the r6000 and r4000 loading a coprocessor register
1513 from memory is interlocked, and does not require a delay.
1514
1515 This case is not handled very well. There is no special
1516 knowledge of CP0 handling, and the coprocessors other
1517 than the floating point unit are not distinguished at
1518 all. */
1519 /* Itbl support may require additional care here. FIXME!
bdaaa2e1 1520 Need to modify this to include knowledge about
252b5132
RH
1521 user specified delays! */
1522 if (prev_pinfo & INSN_WRITE_FPR_T)
1523 {
1524 if (mips_optimize == 0
1525 || insn_uses_reg (ip,
1526 ((prev_insn.insn_opcode >> OP_SH_FT)
1527 & OP_MASK_FT),
1528 MIPS_FP_REG))
1529 ++nops;
1530 }
1531 else if (prev_pinfo & INSN_WRITE_FPR_S)
1532 {
1533 if (mips_optimize == 0
1534 || insn_uses_reg (ip,
1535 ((prev_insn.insn_opcode >> OP_SH_FS)
1536 & OP_MASK_FS),
1537 MIPS_FP_REG))
1538 ++nops;
1539 }
1540 else
1541 {
1542 /* We don't know exactly what the previous instruction
1543 does. If the current instruction uses a coprocessor
1544 register, we must insert a NOP. If previous
1545 instruction may set the condition codes, and the
1546 current instruction uses them, we must insert two
1547 NOPS. */
bdaaa2e1 1548 /* Itbl support may require additional care here. */
252b5132
RH
1549 if (mips_optimize == 0
1550 || ((prev_pinfo & INSN_WRITE_COND_CODE)
1551 && (pinfo & INSN_READ_COND_CODE)))
1552 nops += 2;
1553 else if (pinfo & INSN_COP)
1554 ++nops;
1555 }
1556 }
1557 else if (! mips_opts.mips16
9ce8a5dd 1558 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
1559 && (prev_pinfo & INSN_WRITE_COND_CODE)
1560 && ! cop_interlocks)
1561 {
1562 /* The previous instruction sets the coprocessor condition
1563 codes, but does not require a general coprocessor delay
1564 (this means it is a floating point comparison
1565 instruction). If this instruction uses the condition
1566 codes, we need to insert a single NOP. */
beae10d5 1567 /* Itbl support may require additional care here. */
252b5132
RH
1568 if (mips_optimize == 0
1569 || (pinfo & INSN_READ_COND_CODE))
1570 ++nops;
1571 }
6b76fefe
CM
1572
1573 /* If we're fixing up mfhi/mflo for the r7000 and the
1574 previous insn was an mfhi/mflo and the current insn
1575 reads the register that the mfhi/mflo wrote to, then
1576 insert two nops. */
1577
1578 else if (mips_7000_hilo_fix
1579 && MF_HILO_INSN (prev_pinfo)
1580 && insn_uses_reg (ip, ((prev_insn.insn_opcode >> OP_SH_RD)
beae10d5
KH
1581 & OP_MASK_RD),
1582 MIPS_GR_REG))
6b76fefe
CM
1583 {
1584 nops += 2;
1585 }
1586
1587 /* If we're fixing up mfhi/mflo for the r7000 and the
1588 2nd previous insn was an mfhi/mflo and the current insn
1589 reads the register that the mfhi/mflo wrote to, then
1590 insert one nop. */
1591
1592 else if (mips_7000_hilo_fix
1593 && MF_HILO_INSN (prev_prev_insn.insn_opcode)
1594 && insn_uses_reg (ip, ((prev_prev_insn.insn_opcode >> OP_SH_RD)
1595 & OP_MASK_RD),
1596 MIPS_GR_REG))
bdaaa2e1 1597
6b76fefe 1598 {
f9419b05 1599 ++nops;
6b76fefe 1600 }
bdaaa2e1 1601
252b5132
RH
1602 else if (prev_pinfo & INSN_READ_LO)
1603 {
1604 /* The previous instruction reads the LO register; if the
1605 current instruction writes to the LO register, we must
bdaaa2e1
KH
1606 insert two NOPS. Some newer processors have interlocks.
1607 Also the tx39's multiply instructions can be exectuted
252b5132 1608 immediatly after a read from HI/LO (without the delay),
bdaaa2e1
KH
1609 though the tx39's divide insns still do require the
1610 delay. */
252b5132 1611 if (! (hilo_interlocks
ec68c924 1612 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
252b5132
RH
1613 && (mips_optimize == 0
1614 || (pinfo & INSN_WRITE_LO)))
1615 nops += 2;
1616 /* Most mips16 branch insns don't have a delay slot.
1617 If a read from LO is immediately followed by a branch
1618 to a write to LO we have a read followed by a write
1619 less than 2 insns away. We assume the target of
1620 a branch might be a write to LO, and insert a nop
bdaaa2e1 1621 between a read and an immediately following branch. */
252b5132
RH
1622 else if (mips_opts.mips16
1623 && (mips_optimize == 0
1624 || (pinfo & MIPS16_INSN_BRANCH)))
f9419b05 1625 ++nops;
252b5132
RH
1626 }
1627 else if (prev_insn.insn_mo->pinfo & INSN_READ_HI)
1628 {
1629 /* The previous instruction reads the HI register; if the
1630 current instruction writes to the HI register, we must
1631 insert a NOP. Some newer processors have interlocks.
bdaaa2e1 1632 Also the note tx39's multiply above. */
252b5132 1633 if (! (hilo_interlocks
ec68c924 1634 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
252b5132
RH
1635 && (mips_optimize == 0
1636 || (pinfo & INSN_WRITE_HI)))
1637 nops += 2;
1638 /* Most mips16 branch insns don't have a delay slot.
1639 If a read from HI is immediately followed by a branch
1640 to a write to HI we have a read followed by a write
1641 less than 2 insns away. We assume the target of
1642 a branch might be a write to HI, and insert a nop
bdaaa2e1 1643 between a read and an immediately following branch. */
252b5132
RH
1644 else if (mips_opts.mips16
1645 && (mips_optimize == 0
1646 || (pinfo & MIPS16_INSN_BRANCH)))
f9419b05 1647 ++nops;
252b5132
RH
1648 }
1649
1650 /* If the previous instruction was in a noreorder section, then
1651 we don't want to insert the nop after all. */
bdaaa2e1 1652 /* Itbl support may require additional care here. */
252b5132
RH
1653 if (prev_insn_unreordered)
1654 nops = 0;
1655
1656 /* There are two cases which require two intervening
1657 instructions: 1) setting the condition codes using a move to
1658 coprocessor instruction which requires a general coprocessor
1659 delay and then reading the condition codes 2) reading the HI
1660 or LO register and then writing to it (except on processors
1661 which have interlocks). If we are not already emitting a NOP
1662 instruction, we must check for these cases compared to the
1663 instruction previous to the previous instruction. */
1664 if ((! mips_opts.mips16
9ce8a5dd 1665 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
1666 && (prev_prev_insn.insn_mo->pinfo & INSN_COPROC_MOVE_DELAY)
1667 && (prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
1668 && (pinfo & INSN_READ_COND_CODE)
1669 && ! cop_interlocks)
1670 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_LO)
1671 && (pinfo & INSN_WRITE_LO)
1672 && ! (hilo_interlocks
ec68c924 1673 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT))))
252b5132
RH
1674 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
1675 && (pinfo & INSN_WRITE_HI)
1676 && ! (hilo_interlocks
ec68c924 1677 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))))
252b5132
RH
1678 prev_prev_nop = 1;
1679 else
1680 prev_prev_nop = 0;
1681
1682 if (prev_prev_insn_unreordered)
1683 prev_prev_nop = 0;
1684
1685 if (prev_prev_nop && nops == 0)
1686 ++nops;
1687
1688 /* If we are being given a nop instruction, don't bother with
1689 one of the nops we would otherwise output. This will only
1690 happen when a nop instruction is used with mips_optimize set
1691 to 0. */
1692 if (nops > 0
1693 && ! mips_opts.noreorder
156c2f8b 1694 && ip->insn_opcode == (unsigned) (mips_opts.mips16 ? 0x6500 : 0))
252b5132
RH
1695 --nops;
1696
1697 /* Now emit the right number of NOP instructions. */
1698 if (nops > 0 && ! mips_opts.noreorder)
1699 {
1700 fragS *old_frag;
1701 unsigned long old_frag_offset;
1702 int i;
1703 struct insn_label_list *l;
1704
1705 old_frag = frag_now;
1706 old_frag_offset = frag_now_fix ();
1707
1708 for (i = 0; i < nops; i++)
1709 emit_nop ();
1710
1711 if (listing)
1712 {
1713 listing_prev_line ();
1714 /* We may be at the start of a variant frag. In case we
1715 are, make sure there is enough space for the frag
1716 after the frags created by listing_prev_line. The
1717 argument to frag_grow here must be at least as large
1718 as the argument to all other calls to frag_grow in
1719 this file. We don't have to worry about being in the
1720 middle of a variant frag, because the variants insert
1721 all needed nop instructions themselves. */
1722 frag_grow (40);
1723 }
1724
1725 for (l = insn_labels; l != NULL; l = l->next)
1726 {
98aa84af
AM
1727 valueT val;
1728
252b5132 1729 assert (S_GET_SEGMENT (l->label) == now_seg);
49309057 1730 symbol_set_frag (l->label, frag_now);
98aa84af 1731 val = (valueT) frag_now_fix ();
252b5132
RH
1732 /* mips16 text labels are stored as odd. */
1733 if (mips_opts.mips16)
f9419b05 1734 ++val;
98aa84af 1735 S_SET_VALUE (l->label, val);
252b5132
RH
1736 }
1737
1738#ifndef NO_ECOFF_DEBUGGING
1739 if (ECOFF_DEBUGGING)
1740 ecoff_fix_loc (old_frag, old_frag_offset);
1741#endif
1742 }
1743 else if (prev_nop_frag != NULL)
1744 {
1745 /* We have a frag holding nops we may be able to remove. If
1746 we don't need any nops, we can decrease the size of
1747 prev_nop_frag by the size of one instruction. If we do
bdaaa2e1 1748 need some nops, we count them in prev_nops_required. */
252b5132
RH
1749 if (prev_nop_frag_since == 0)
1750 {
1751 if (nops == 0)
1752 {
1753 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1754 --prev_nop_frag_holds;
1755 }
1756 else
1757 prev_nop_frag_required += nops;
1758 }
1759 else
1760 {
1761 if (prev_prev_nop == 0)
1762 {
1763 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1764 --prev_nop_frag_holds;
1765 }
1766 else
1767 ++prev_nop_frag_required;
1768 }
1769
1770 if (prev_nop_frag_holds <= prev_nop_frag_required)
1771 prev_nop_frag = NULL;
1772
1773 ++prev_nop_frag_since;
1774
1775 /* Sanity check: by the time we reach the second instruction
1776 after prev_nop_frag, we should have used up all the nops
1777 one way or another. */
1778 assert (prev_nop_frag_since <= 1 || prev_nop_frag == NULL);
1779 }
1780 }
1781
f6688943 1782 if (*reloc_type > BFD_RELOC_UNUSED)
252b5132
RH
1783 {
1784 /* We need to set up a variant frag. */
1785 assert (mips_opts.mips16 && address_expr != NULL);
1786 f = frag_var (rs_machine_dependent, 4, 0,
f6688943 1787 RELAX_MIPS16_ENCODE (*reloc_type - BFD_RELOC_UNUSED,
252b5132
RH
1788 mips16_small, mips16_ext,
1789 (prev_pinfo
1790 & INSN_UNCOND_BRANCH_DELAY),
f6688943 1791 (*prev_insn_reloc_type
252b5132 1792 == BFD_RELOC_MIPS16_JMP)),
c4e7957c 1793 make_expr_symbol (address_expr), 0, NULL);
252b5132
RH
1794 }
1795 else if (place != NULL)
1796 f = place;
1797 else if (mips_opts.mips16
1798 && ! ip->use_extend
f6688943 1799 && *reloc_type != BFD_RELOC_MIPS16_JMP)
252b5132
RH
1800 {
1801 /* Make sure there is enough room to swap this instruction with
1802 a following jump instruction. */
1803 frag_grow (6);
1804 f = frag_more (2);
1805 }
1806 else
1807 {
1808 if (mips_opts.mips16
1809 && mips_opts.noreorder
1810 && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
1811 as_warn (_("extended instruction in delay slot"));
1812
1813 f = frag_more (4);
1814 }
1815
f6688943
TS
1816 fixp[0] = fixp[1] = fixp[2] = NULL;
1817 if (address_expr != NULL && *reloc_type < BFD_RELOC_UNUSED)
252b5132
RH
1818 {
1819 if (address_expr->X_op == O_constant)
1820 {
4db1a35d 1821 valueT tmp;
f6688943
TS
1822
1823 switch (*reloc_type)
252b5132
RH
1824 {
1825 case BFD_RELOC_32:
1826 ip->insn_opcode |= address_expr->X_add_number;
1827 break;
1828
f6688943
TS
1829 case BFD_RELOC_MIPS_HIGHEST:
1830 tmp = (address_expr->X_add_number + 0x800080008000) >> 16;
1831 tmp >>= 16;
1832 ip->insn_opcode |= (tmp >> 16) & 0xffff;
1833 break;
1834
1835 case BFD_RELOC_MIPS_HIGHER:
1836 tmp = (address_expr->X_add_number + 0x80008000) >> 16;
1837 ip->insn_opcode |= (tmp >> 16) & 0xffff;
1838 break;
1839
1840 case BFD_RELOC_HI16_S:
1841 ip->insn_opcode |= ((address_expr->X_add_number + 0x8000)
1842 >> 16) & 0xffff;
1843 break;
1844
1845 case BFD_RELOC_HI16:
1846 ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
1847 break;
1848
252b5132
RH
1849 case BFD_RELOC_LO16:
1850 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
1851 break;
1852
1853 case BFD_RELOC_MIPS_JMP:
1854 if ((address_expr->X_add_number & 3) != 0)
1855 as_bad (_("jump to misaligned address (0x%lx)"),
1856 (unsigned long) address_expr->X_add_number);
7496292d
TS
1857 if (address_expr->X_add_number & ~0xfffffff
1858 || address_expr->X_add_number > 0x7fffffc)
1859 as_bad (_("jump address range overflow (0x%lx)"),
1860 (unsigned long) address_expr->X_add_number);
252b5132
RH
1861 ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
1862 break;
1863
1864 case BFD_RELOC_MIPS16_JMP:
1865 if ((address_expr->X_add_number & 3) != 0)
1866 as_bad (_("jump to misaligned address (0x%lx)"),
1867 (unsigned long) address_expr->X_add_number);
7496292d
TS
1868 if (address_expr->X_add_number & ~0xfffffff
1869 || address_expr->X_add_number > 0x7fffffc)
1870 as_bad (_("jump address range overflow (0x%lx)"),
1871 (unsigned long) address_expr->X_add_number);
252b5132
RH
1872 ip->insn_opcode |=
1873 (((address_expr->X_add_number & 0x7c0000) << 3)
1874 | ((address_expr->X_add_number & 0xf800000) >> 7)
1875 | ((address_expr->X_add_number & 0x3fffc) >> 2));
1876 break;
1877
cb56d3d3 1878 case BFD_RELOC_16_PCREL:
233b8738 1879 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
cb56d3d3
TS
1880 break;
1881
252b5132
RH
1882 case BFD_RELOC_16_PCREL_S2:
1883 goto need_reloc;
1884
1885 default:
1886 internalError ();
1887 }
1888 }
1889 else
1890 {
1891 need_reloc:
f6688943 1892 /* Don't generate a reloc if we are writing into a variant frag. */
252b5132
RH
1893 if (place == NULL)
1894 {
f6688943
TS
1895 fixp[0] = fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
1896 address_expr,
1897 (*reloc_type == BFD_RELOC_16_PCREL
1898 || *reloc_type == BFD_RELOC_16_PCREL_S2),
1899 reloc_type[0]);
1900
b6ff326e 1901 /* These relocations can have an addend that won't fit in
f6688943
TS
1902 4 octets for 64bit assembly. */
1903 if (HAVE_64BIT_GPRS &&
1904 (*reloc_type == BFD_RELOC_16
98d3f06f
KH
1905 || *reloc_type == BFD_RELOC_32
1906 || *reloc_type == BFD_RELOC_MIPS_JMP
1907 || *reloc_type == BFD_RELOC_HI16_S
1908 || *reloc_type == BFD_RELOC_LO16
1909 || *reloc_type == BFD_RELOC_GPREL16
1910 || *reloc_type == BFD_RELOC_MIPS_LITERAL
1911 || *reloc_type == BFD_RELOC_GPREL32
1912 || *reloc_type == BFD_RELOC_64
1913 || *reloc_type == BFD_RELOC_CTOR
1914 || *reloc_type == BFD_RELOC_MIPS_SUB
1915 || *reloc_type == BFD_RELOC_MIPS_HIGHEST
1916 || *reloc_type == BFD_RELOC_MIPS_HIGHER
1917 || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
1918 || *reloc_type == BFD_RELOC_MIPS_REL16
1919 || *reloc_type == BFD_RELOC_MIPS_RELGOT))
f6688943
TS
1920 fixp[0]->fx_no_overflow = 1;
1921
252b5132
RH
1922 if (unmatched_hi)
1923 {
1924 struct mips_hi_fixup *hi_fixup;
1925
f6688943 1926 assert (*reloc_type == BFD_RELOC_HI16_S);
252b5132
RH
1927 hi_fixup = ((struct mips_hi_fixup *)
1928 xmalloc (sizeof (struct mips_hi_fixup)));
f6688943 1929 hi_fixup->fixp = fixp[0];
252b5132
RH
1930 hi_fixup->seg = now_seg;
1931 hi_fixup->next = mips_hi_fixup_list;
1932 mips_hi_fixup_list = hi_fixup;
1933 }
f6688943
TS
1934
1935 if (reloc_type[1] != BFD_RELOC_UNUSED)
1936 {
1937 /* FIXME: This symbol can be one of
1938 RSS_UNDEF, RSS_GP, RSS_GP0, RSS_LOC. */
1939 address_expr->X_op = O_absent;
1940 address_expr->X_add_symbol = 0;
1941 address_expr->X_add_number = 0;
1942
1943 fixp[1] = fix_new_exp (frag_now, f - frag_now->fr_literal,
1944 4, address_expr, false,
1945 reloc_type[1]);
1946
b6ff326e 1947 /* These relocations can have an addend that won't fit in
f6688943
TS
1948 4 octets for 64bit assembly. */
1949 if (HAVE_64BIT_GPRS &&
1950 (*reloc_type == BFD_RELOC_16
1951 || *reloc_type == BFD_RELOC_32
1952 || *reloc_type == BFD_RELOC_MIPS_JMP
1953 || *reloc_type == BFD_RELOC_HI16_S
1954 || *reloc_type == BFD_RELOC_LO16
1955 || *reloc_type == BFD_RELOC_GPREL16
1956 || *reloc_type == BFD_RELOC_MIPS_LITERAL
1957 || *reloc_type == BFD_RELOC_GPREL32
1958 || *reloc_type == BFD_RELOC_64
1959 || *reloc_type == BFD_RELOC_CTOR
1960 || *reloc_type == BFD_RELOC_MIPS_SUB
1961 || *reloc_type == BFD_RELOC_MIPS_HIGHEST
1962 || *reloc_type == BFD_RELOC_MIPS_HIGHER
1963 || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
1964 || *reloc_type == BFD_RELOC_MIPS_REL16
1965 || *reloc_type == BFD_RELOC_MIPS_RELGOT))
98d3f06f 1966 fixp[1]->fx_no_overflow = 1;
f6688943
TS
1967
1968 if (reloc_type[2] != BFD_RELOC_UNUSED)
1969 {
1970 address_expr->X_op = O_absent;
1971 address_expr->X_add_symbol = 0;
1972 address_expr->X_add_number = 0;
1973
1974 fixp[2] = fix_new_exp (frag_now,
1975 f - frag_now->fr_literal, 4,
1976 address_expr, false,
1977 reloc_type[2]);
1978
b6ff326e 1979 /* These relocations can have an addend that won't fit in
f6688943
TS
1980 4 octets for 64bit assembly. */
1981 if (HAVE_64BIT_GPRS &&
1982 (*reloc_type == BFD_RELOC_16
1983 || *reloc_type == BFD_RELOC_32
1984 || *reloc_type == BFD_RELOC_MIPS_JMP
1985 || *reloc_type == BFD_RELOC_HI16_S
1986 || *reloc_type == BFD_RELOC_LO16
1987 || *reloc_type == BFD_RELOC_GPREL16
1988 || *reloc_type == BFD_RELOC_MIPS_LITERAL
1989 || *reloc_type == BFD_RELOC_GPREL32
1990 || *reloc_type == BFD_RELOC_64
1991 || *reloc_type == BFD_RELOC_CTOR
1992 || *reloc_type == BFD_RELOC_MIPS_SUB
1993 || *reloc_type == BFD_RELOC_MIPS_HIGHEST
1994 || *reloc_type == BFD_RELOC_MIPS_HIGHER
1995 || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
1996 || *reloc_type == BFD_RELOC_MIPS_REL16
1997 || *reloc_type == BFD_RELOC_MIPS_RELGOT))
98d3f06f 1998 fixp[2]->fx_no_overflow = 1;
f6688943
TS
1999 }
2000 }
252b5132
RH
2001 }
2002 }
2003 }
2004
2005 if (! mips_opts.mips16)
c5dd6aab
DJ
2006 {
2007 md_number_to_chars (f, ip->insn_opcode, 4);
2008#ifdef OBJ_ELF
2009 dwarf2_emit_insn (4);
2010#endif
2011 }
f6688943 2012 else if (*reloc_type == BFD_RELOC_MIPS16_JMP)
252b5132
RH
2013 {
2014 md_number_to_chars (f, ip->insn_opcode >> 16, 2);
2015 md_number_to_chars (f + 2, ip->insn_opcode & 0xffff, 2);
c5dd6aab
DJ
2016#ifdef OBJ_ELF
2017 dwarf2_emit_insn (4);
2018#endif
252b5132
RH
2019 }
2020 else
2021 {
2022 if (ip->use_extend)
2023 {
2024 md_number_to_chars (f, 0xf000 | ip->extend, 2);
2025 f += 2;
2026 }
2027 md_number_to_chars (f, ip->insn_opcode, 2);
c5dd6aab
DJ
2028#ifdef OBJ_ELF
2029 dwarf2_emit_insn (ip->use_extend ? 4 : 2);
2030#endif
252b5132
RH
2031 }
2032
2033 /* Update the register mask information. */
2034 if (! mips_opts.mips16)
2035 {
2036 if (pinfo & INSN_WRITE_GPR_D)
2037 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
2038 if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2039 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
2040 if (pinfo & INSN_READ_GPR_S)
2041 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
2042 if (pinfo & INSN_WRITE_GPR_31)
f9419b05 2043 mips_gprmask |= 1 << RA;
252b5132
RH
2044 if (pinfo & INSN_WRITE_FPR_D)
2045 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
2046 if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2047 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
2048 if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2049 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
2050 if ((pinfo & INSN_READ_FPR_R) != 0)
2051 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
2052 if (pinfo & INSN_COP)
2053 {
bdaaa2e1
KH
2054 /* We don't keep enough information to sort these cases out.
2055 The itbl support does keep this information however, although
2056 we currently don't support itbl fprmats as part of the cop
2057 instruction. May want to add this support in the future. */
252b5132
RH
2058 }
2059 /* Never set the bit for $0, which is always zero. */
beae10d5 2060 mips_gprmask &= ~1 << 0;
252b5132
RH
2061 }
2062 else
2063 {
2064 if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2065 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RX)
2066 & MIPS16OP_MASK_RX);
2067 if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2068 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RY)
2069 & MIPS16OP_MASK_RY);
2070 if (pinfo & MIPS16_INSN_WRITE_Z)
2071 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RZ)
2072 & MIPS16OP_MASK_RZ);
2073 if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2074 mips_gprmask |= 1 << TREG;
2075 if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2076 mips_gprmask |= 1 << SP;
2077 if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2078 mips_gprmask |= 1 << RA;
2079 if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2080 mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2081 if (pinfo & MIPS16_INSN_READ_Z)
2082 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
2083 & MIPS16OP_MASK_MOVE32Z);
2084 if (pinfo & MIPS16_INSN_READ_GPR_X)
2085 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
2086 & MIPS16OP_MASK_REGR32);
2087 }
2088
2089 if (place == NULL && ! mips_opts.noreorder)
2090 {
2091 /* Filling the branch delay slot is more complex. We try to
2092 switch the branch with the previous instruction, which we can
2093 do if the previous instruction does not set up a condition
2094 that the branch tests and if the branch is not itself the
2095 target of any branch. */
2096 if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2097 || (pinfo & INSN_COND_BRANCH_DELAY))
2098 {
2099 if (mips_optimize < 2
2100 /* If we have seen .set volatile or .set nomove, don't
2101 optimize. */
2102 || mips_opts.nomove != 0
2103 /* If we had to emit any NOP instructions, then we
2104 already know we can not swap. */
2105 || nops != 0
2106 /* If we don't even know the previous insn, we can not
bdaaa2e1 2107 swap. */
252b5132
RH
2108 || ! prev_insn_valid
2109 /* If the previous insn is already in a branch delay
2110 slot, then we can not swap. */
2111 || prev_insn_is_delay_slot
2112 /* If the previous previous insn was in a .set
2113 noreorder, we can't swap. Actually, the MIPS
2114 assembler will swap in this situation. However, gcc
2115 configured -with-gnu-as will generate code like
2116 .set noreorder
2117 lw $4,XXX
2118 .set reorder
2119 INSN
2120 bne $4,$0,foo
2121 in which we can not swap the bne and INSN. If gcc is
2122 not configured -with-gnu-as, it does not output the
2123 .set pseudo-ops. We don't have to check
2124 prev_insn_unreordered, because prev_insn_valid will
2125 be 0 in that case. We don't want to use
2126 prev_prev_insn_valid, because we do want to be able
2127 to swap at the start of a function. */
2128 || prev_prev_insn_unreordered
2129 /* If the branch is itself the target of a branch, we
2130 can not swap. We cheat on this; all we check for is
2131 whether there is a label on this instruction. If
2132 there are any branches to anything other than a
2133 label, users must use .set noreorder. */
2134 || insn_labels != NULL
2135 /* If the previous instruction is in a variant frag, we
2136 can not do the swap. This does not apply to the
2137 mips16, which uses variant frags for different
2138 purposes. */
2139 || (! mips_opts.mips16
2140 && prev_insn_frag->fr_type == rs_machine_dependent)
2141 /* If the branch reads the condition codes, we don't
2142 even try to swap, because in the sequence
2143 ctc1 $X,$31
2144 INSN
2145 INSN
2146 bc1t LABEL
2147 we can not swap, and I don't feel like handling that
2148 case. */
2149 || (! mips_opts.mips16
9ce8a5dd 2150 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
2151 && (pinfo & INSN_READ_COND_CODE))
2152 /* We can not swap with an instruction that requires a
2153 delay slot, becase the target of the branch might
2154 interfere with that instruction. */
2155 || (! mips_opts.mips16
9ce8a5dd 2156 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132 2157 && (prev_pinfo
bdaaa2e1 2158 /* Itbl support may require additional care here. */
252b5132
RH
2159 & (INSN_LOAD_COPROC_DELAY
2160 | INSN_COPROC_MOVE_DELAY
2161 | INSN_WRITE_COND_CODE)))
2162 || (! (hilo_interlocks
ec68c924 2163 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
252b5132
RH
2164 && (prev_pinfo
2165 & (INSN_READ_LO
2166 | INSN_READ_HI)))
2167 || (! mips_opts.mips16
2168 && ! gpr_interlocks
2169 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))
2170 || (! mips_opts.mips16
e7af610e 2171 && mips_opts.isa == ISA_MIPS1
bdaaa2e1 2172 /* Itbl support may require additional care here. */
252b5132
RH
2173 && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))
2174 /* We can not swap with a branch instruction. */
2175 || (prev_pinfo
2176 & (INSN_UNCOND_BRANCH_DELAY
2177 | INSN_COND_BRANCH_DELAY
2178 | INSN_COND_BRANCH_LIKELY))
2179 /* We do not swap with a trap instruction, since it
2180 complicates trap handlers to have the trap
2181 instruction be in a delay slot. */
2182 || (prev_pinfo & INSN_TRAP)
2183 /* If the branch reads a register that the previous
2184 instruction sets, we can not swap. */
2185 || (! mips_opts.mips16
2186 && (prev_pinfo & INSN_WRITE_GPR_T)
2187 && insn_uses_reg (ip,
2188 ((prev_insn.insn_opcode >> OP_SH_RT)
2189 & OP_MASK_RT),
2190 MIPS_GR_REG))
2191 || (! mips_opts.mips16
2192 && (prev_pinfo & INSN_WRITE_GPR_D)
2193 && insn_uses_reg (ip,
2194 ((prev_insn.insn_opcode >> OP_SH_RD)
2195 & OP_MASK_RD),
2196 MIPS_GR_REG))
2197 || (mips_opts.mips16
2198 && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2199 && insn_uses_reg (ip,
2200 ((prev_insn.insn_opcode
2201 >> MIPS16OP_SH_RX)
2202 & MIPS16OP_MASK_RX),
2203 MIPS16_REG))
2204 || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2205 && insn_uses_reg (ip,
2206 ((prev_insn.insn_opcode
2207 >> MIPS16OP_SH_RY)
2208 & MIPS16OP_MASK_RY),
2209 MIPS16_REG))
2210 || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2211 && insn_uses_reg (ip,
2212 ((prev_insn.insn_opcode
2213 >> MIPS16OP_SH_RZ)
2214 & MIPS16OP_MASK_RZ),
2215 MIPS16_REG))
2216 || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2217 && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2218 || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2219 && insn_uses_reg (ip, RA, MIPS_GR_REG))
2220 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2221 && insn_uses_reg (ip,
2222 MIPS16OP_EXTRACT_REG32R (prev_insn.
2223 insn_opcode),
2224 MIPS_GR_REG))))
2225 /* If the branch writes a register that the previous
2226 instruction sets, we can not swap (we know that
2227 branches write only to RD or to $31). */
2228 || (! mips_opts.mips16
2229 && (prev_pinfo & INSN_WRITE_GPR_T)
2230 && (((pinfo & INSN_WRITE_GPR_D)
2231 && (((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT)
2232 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2233 || ((pinfo & INSN_WRITE_GPR_31)
2234 && (((prev_insn.insn_opcode >> OP_SH_RT)
2235 & OP_MASK_RT)
f9419b05 2236 == RA))))
252b5132
RH
2237 || (! mips_opts.mips16
2238 && (prev_pinfo & INSN_WRITE_GPR_D)
2239 && (((pinfo & INSN_WRITE_GPR_D)
2240 && (((prev_insn.insn_opcode >> OP_SH_RD) & OP_MASK_RD)
2241 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2242 || ((pinfo & INSN_WRITE_GPR_31)
2243 && (((prev_insn.insn_opcode >> OP_SH_RD)
2244 & OP_MASK_RD)
f9419b05 2245 == RA))))
252b5132
RH
2246 || (mips_opts.mips16
2247 && (pinfo & MIPS16_INSN_WRITE_31)
2248 && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2249 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2250 && (MIPS16OP_EXTRACT_REG32R (prev_insn.insn_opcode)
2251 == RA))))
2252 /* If the branch writes a register that the previous
2253 instruction reads, we can not swap (we know that
2254 branches only write to RD or to $31). */
2255 || (! mips_opts.mips16
2256 && (pinfo & INSN_WRITE_GPR_D)
2257 && insn_uses_reg (&prev_insn,
2258 ((ip->insn_opcode >> OP_SH_RD)
2259 & OP_MASK_RD),
2260 MIPS_GR_REG))
2261 || (! mips_opts.mips16
2262 && (pinfo & INSN_WRITE_GPR_31)
f9419b05 2263 && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
252b5132
RH
2264 || (mips_opts.mips16
2265 && (pinfo & MIPS16_INSN_WRITE_31)
2266 && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2267 /* If we are generating embedded PIC code, the branch
2268 might be expanded into a sequence which uses $at, so
2269 we can't swap with an instruction which reads it. */
2270 || (mips_pic == EMBEDDED_PIC
2271 && insn_uses_reg (&prev_insn, AT, MIPS_GR_REG))
2272 /* If the previous previous instruction has a load
2273 delay, and sets a register that the branch reads, we
2274 can not swap. */
2275 || (! mips_opts.mips16
9ce8a5dd 2276 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
bdaaa2e1 2277 /* Itbl support may require additional care here. */
252b5132
RH
2278 && ((prev_prev_insn.insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
2279 || (! gpr_interlocks
2280 && (prev_prev_insn.insn_mo->pinfo
2281 & INSN_LOAD_MEMORY_DELAY)))
2282 && insn_uses_reg (ip,
2283 ((prev_prev_insn.insn_opcode >> OP_SH_RT)
2284 & OP_MASK_RT),
2285 MIPS_GR_REG))
2286 /* If one instruction sets a condition code and the
2287 other one uses a condition code, we can not swap. */
2288 || ((pinfo & INSN_READ_COND_CODE)
2289 && (prev_pinfo & INSN_WRITE_COND_CODE))
2290 || ((pinfo & INSN_WRITE_COND_CODE)
2291 && (prev_pinfo & INSN_READ_COND_CODE))
2292 /* If the previous instruction uses the PC, we can not
2293 swap. */
2294 || (mips_opts.mips16
2295 && (prev_pinfo & MIPS16_INSN_READ_PC))
2296 /* If the previous instruction was extended, we can not
2297 swap. */
2298 || (mips_opts.mips16 && prev_insn_extended)
2299 /* If the previous instruction had a fixup in mips16
2300 mode, we can not swap. This normally means that the
2301 previous instruction was a 4 byte branch anyhow. */
f6688943 2302 || (mips_opts.mips16 && prev_insn_fixp[0])
bdaaa2e1
KH
2303 /* If the previous instruction is a sync, sync.l, or
2304 sync.p, we can not swap. */
f173e82e 2305 || (prev_pinfo & INSN_SYNC))
252b5132
RH
2306 {
2307 /* We could do even better for unconditional branches to
2308 portions of this object file; we could pick up the
2309 instruction at the destination, put it in the delay
2310 slot, and bump the destination address. */
2311 emit_nop ();
2312 /* Update the previous insn information. */
2313 prev_prev_insn = *ip;
2314 prev_insn.insn_mo = &dummy_opcode;
2315 }
2316 else
2317 {
2318 /* It looks like we can actually do the swap. */
2319 if (! mips_opts.mips16)
2320 {
2321 char *prev_f;
2322 char temp[4];
2323
2324 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2325 memcpy (temp, prev_f, 4);
2326 memcpy (prev_f, f, 4);
2327 memcpy (f, temp, 4);
f6688943
TS
2328 if (prev_insn_fixp[0])
2329 {
2330 prev_insn_fixp[0]->fx_frag = frag_now;
2331 prev_insn_fixp[0]->fx_where = f - frag_now->fr_literal;
2332 }
2333 if (prev_insn_fixp[1])
2334 {
2335 prev_insn_fixp[1]->fx_frag = frag_now;
2336 prev_insn_fixp[1]->fx_where = f - frag_now->fr_literal;
2337 }
2338 if (prev_insn_fixp[2])
252b5132 2339 {
f6688943
TS
2340 prev_insn_fixp[2]->fx_frag = frag_now;
2341 prev_insn_fixp[2]->fx_where = f - frag_now->fr_literal;
252b5132 2342 }
f6688943 2343 if (fixp[0])
252b5132 2344 {
f6688943
TS
2345 fixp[0]->fx_frag = prev_insn_frag;
2346 fixp[0]->fx_where = prev_insn_where;
2347 }
2348 if (fixp[1])
2349 {
2350 fixp[1]->fx_frag = prev_insn_frag;
2351 fixp[1]->fx_where = prev_insn_where;
2352 }
2353 if (fixp[2])
2354 {
2355 fixp[2]->fx_frag = prev_insn_frag;
2356 fixp[2]->fx_where = prev_insn_where;
252b5132
RH
2357 }
2358 }
2359 else
2360 {
2361 char *prev_f;
2362 char temp[2];
2363
f6688943
TS
2364 assert (prev_insn_fixp[0] == NULL);
2365 assert (prev_insn_fixp[1] == NULL);
2366 assert (prev_insn_fixp[2] == NULL);
252b5132
RH
2367 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2368 memcpy (temp, prev_f, 2);
2369 memcpy (prev_f, f, 2);
f6688943 2370 if (*reloc_type != BFD_RELOC_MIPS16_JMP)
252b5132 2371 {
f6688943 2372 assert (*reloc_type == BFD_RELOC_UNUSED);
252b5132
RH
2373 memcpy (f, temp, 2);
2374 }
2375 else
2376 {
2377 memcpy (f, f + 2, 2);
2378 memcpy (f + 2, temp, 2);
2379 }
f6688943
TS
2380 if (fixp[0])
2381 {
2382 fixp[0]->fx_frag = prev_insn_frag;
2383 fixp[0]->fx_where = prev_insn_where;
2384 }
2385 if (fixp[1])
2386 {
2387 fixp[1]->fx_frag = prev_insn_frag;
2388 fixp[1]->fx_where = prev_insn_where;
2389 }
2390 if (fixp[2])
252b5132 2391 {
f6688943
TS
2392 fixp[2]->fx_frag = prev_insn_frag;
2393 fixp[2]->fx_where = prev_insn_where;
252b5132
RH
2394 }
2395 }
2396
2397 /* Update the previous insn information; leave prev_insn
2398 unchanged. */
2399 prev_prev_insn = *ip;
2400 }
2401 prev_insn_is_delay_slot = 1;
2402
2403 /* If that was an unconditional branch, forget the previous
2404 insn information. */
2405 if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2406 {
2407 prev_prev_insn.insn_mo = &dummy_opcode;
2408 prev_insn.insn_mo = &dummy_opcode;
2409 }
2410
f6688943
TS
2411 prev_insn_fixp[0] = NULL;
2412 prev_insn_fixp[1] = NULL;
2413 prev_insn_fixp[2] = NULL;
2414 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2415 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2416 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
252b5132
RH
2417 prev_insn_extended = 0;
2418 }
2419 else if (pinfo & INSN_COND_BRANCH_LIKELY)
2420 {
2421 /* We don't yet optimize a branch likely. What we should do
2422 is look at the target, copy the instruction found there
2423 into the delay slot, and increment the branch to jump to
2424 the next instruction. */
2425 emit_nop ();
2426 /* Update the previous insn information. */
2427 prev_prev_insn = *ip;
2428 prev_insn.insn_mo = &dummy_opcode;
f6688943
TS
2429 prev_insn_fixp[0] = NULL;
2430 prev_insn_fixp[1] = NULL;
2431 prev_insn_fixp[2] = NULL;
2432 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2433 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2434 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
252b5132
RH
2435 prev_insn_extended = 0;
2436 }
2437 else
2438 {
2439 /* Update the previous insn information. */
2440 if (nops > 0)
2441 prev_prev_insn.insn_mo = &dummy_opcode;
2442 else
2443 prev_prev_insn = prev_insn;
2444 prev_insn = *ip;
2445
2446 /* Any time we see a branch, we always fill the delay slot
2447 immediately; since this insn is not a branch, we know it
2448 is not in a delay slot. */
2449 prev_insn_is_delay_slot = 0;
2450
f6688943
TS
2451 prev_insn_fixp[0] = fixp[0];
2452 prev_insn_fixp[1] = fixp[1];
2453 prev_insn_fixp[2] = fixp[2];
2454 prev_insn_reloc_type[0] = reloc_type[0];
2455 prev_insn_reloc_type[1] = reloc_type[1];
2456 prev_insn_reloc_type[2] = reloc_type[2];
252b5132
RH
2457 if (mips_opts.mips16)
2458 prev_insn_extended = (ip->use_extend
f6688943 2459 || *reloc_type > BFD_RELOC_UNUSED);
252b5132
RH
2460 }
2461
2462 prev_prev_insn_unreordered = prev_insn_unreordered;
2463 prev_insn_unreordered = 0;
2464 prev_insn_frag = frag_now;
2465 prev_insn_where = f - frag_now->fr_literal;
2466 prev_insn_valid = 1;
2467 }
2468 else if (place == NULL)
2469 {
2470 /* We need to record a bit of information even when we are not
2471 reordering, in order to determine the base address for mips16
2472 PC relative relocs. */
2473 prev_prev_insn = prev_insn;
2474 prev_insn = *ip;
f6688943
TS
2475 prev_insn_reloc_type[0] = reloc_type[0];
2476 prev_insn_reloc_type[1] = reloc_type[1];
2477 prev_insn_reloc_type[2] = reloc_type[2];
252b5132
RH
2478 prev_prev_insn_unreordered = prev_insn_unreordered;
2479 prev_insn_unreordered = 1;
2480 }
2481
2482 /* We just output an insn, so the next one doesn't have a label. */
2483 mips_clear_insn_labels ();
2484
2485 /* We must ensure that a fixup associated with an unmatched %hi
2486 reloc does not become a variant frag. Otherwise, the
2487 rearrangement of %hi relocs in frob_file may confuse
2488 tc_gen_reloc. */
2489 if (unmatched_hi)
2490 {
2491 frag_wane (frag_now);
2492 frag_new (0);
2493 }
2494}
2495
2496/* This function forgets that there was any previous instruction or
2497 label. If PRESERVE is non-zero, it remembers enough information to
bdaaa2e1 2498 know whether nops are needed before a noreorder section. */
252b5132
RH
2499
2500static void
2501mips_no_prev_insn (preserve)
2502 int preserve;
2503{
2504 if (! preserve)
2505 {
2506 prev_insn.insn_mo = &dummy_opcode;
2507 prev_prev_insn.insn_mo = &dummy_opcode;
2508 prev_nop_frag = NULL;
2509 prev_nop_frag_holds = 0;
2510 prev_nop_frag_required = 0;
2511 prev_nop_frag_since = 0;
2512 }
2513 prev_insn_valid = 0;
2514 prev_insn_is_delay_slot = 0;
2515 prev_insn_unreordered = 0;
2516 prev_insn_extended = 0;
f6688943
TS
2517 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2518 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2519 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
252b5132
RH
2520 prev_prev_insn_unreordered = 0;
2521 mips_clear_insn_labels ();
2522}
2523
2524/* This function must be called whenever we turn on noreorder or emit
2525 something other than instructions. It inserts any NOPS which might
2526 be needed by the previous instruction, and clears the information
2527 kept for the previous instructions. The INSNS parameter is true if
bdaaa2e1 2528 instructions are to follow. */
252b5132
RH
2529
2530static void
2531mips_emit_delays (insns)
2532 boolean insns;
2533{
2534 if (! mips_opts.noreorder)
2535 {
2536 int nops;
2537
2538 nops = 0;
2539 if ((! mips_opts.mips16
9ce8a5dd 2540 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
2541 && (! cop_interlocks
2542 && (prev_insn.insn_mo->pinfo
2543 & (INSN_LOAD_COPROC_DELAY
2544 | INSN_COPROC_MOVE_DELAY
2545 | INSN_WRITE_COND_CODE))))
2546 || (! hilo_interlocks
2547 && (prev_insn.insn_mo->pinfo
2548 & (INSN_READ_LO
2549 | INSN_READ_HI)))
2550 || (! mips_opts.mips16
2551 && ! gpr_interlocks
bdaaa2e1 2552 && (prev_insn.insn_mo->pinfo
252b5132
RH
2553 & INSN_LOAD_MEMORY_DELAY))
2554 || (! mips_opts.mips16
e7af610e 2555 && mips_opts.isa == ISA_MIPS1
252b5132
RH
2556 && (prev_insn.insn_mo->pinfo
2557 & INSN_COPROC_MEMORY_DELAY)))
2558 {
beae10d5 2559 /* Itbl support may require additional care here. */
252b5132
RH
2560 ++nops;
2561 if ((! mips_opts.mips16
9ce8a5dd 2562 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
2563 && (! cop_interlocks
2564 && prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2565 || (! hilo_interlocks
2566 && ((prev_insn.insn_mo->pinfo & INSN_READ_HI)
2567 || (prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2568 ++nops;
2569
2570 if (prev_insn_unreordered)
2571 nops = 0;
2572 }
2573 else if ((! mips_opts.mips16
9ce8a5dd 2574 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
2575 && (! cop_interlocks
2576 && prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2577 || (! hilo_interlocks
2578 && ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
2579 || (prev_prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2580 {
beae10d5 2581 /* Itbl support may require additional care here. */
252b5132
RH
2582 if (! prev_prev_insn_unreordered)
2583 ++nops;
2584 }
2585
2586 if (nops > 0)
2587 {
2588 struct insn_label_list *l;
2589
2590 if (insns)
2591 {
2592 /* Record the frag which holds the nop instructions, so
2593 that we can remove them if we don't need them. */
2594 frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2595 prev_nop_frag = frag_now;
2596 prev_nop_frag_holds = nops;
2597 prev_nop_frag_required = 0;
2598 prev_nop_frag_since = 0;
2599 }
2600
2601 for (; nops > 0; --nops)
2602 emit_nop ();
2603
2604 if (insns)
2605 {
2606 /* Move on to a new frag, so that it is safe to simply
bdaaa2e1 2607 decrease the size of prev_nop_frag. */
252b5132
RH
2608 frag_wane (frag_now);
2609 frag_new (0);
2610 }
2611
2612 for (l = insn_labels; l != NULL; l = l->next)
2613 {
98aa84af
AM
2614 valueT val;
2615
252b5132 2616 assert (S_GET_SEGMENT (l->label) == now_seg);
49309057 2617 symbol_set_frag (l->label, frag_now);
98aa84af 2618 val = (valueT) frag_now_fix ();
252b5132
RH
2619 /* mips16 text labels are stored as odd. */
2620 if (mips_opts.mips16)
f9419b05 2621 ++val;
98aa84af 2622 S_SET_VALUE (l->label, val);
252b5132
RH
2623 }
2624 }
2625 }
2626
2627 /* Mark instruction labels in mips16 mode. */
f9419b05 2628 if (insns)
252b5132
RH
2629 mips16_mark_labels ();
2630
2631 mips_no_prev_insn (insns);
2632}
2633
2634/* Build an instruction created by a macro expansion. This is passed
2635 a pointer to the count of instructions created so far, an
2636 expression, the name of the instruction to build, an operand format
2637 string, and corresponding arguments. */
2638
2639#ifdef USE_STDARG
2640static void
2641macro_build (char *place,
2642 int *counter,
2643 expressionS * ep,
2644 const char *name,
2645 const char *fmt,
2646 ...)
2647#else
2648static void
2649macro_build (place, counter, ep, name, fmt, va_alist)
2650 char *place;
2651 int *counter;
2652 expressionS *ep;
2653 const char *name;
2654 const char *fmt;
2655 va_dcl
2656#endif
2657{
2658 struct mips_cl_insn insn;
f6688943 2659 bfd_reloc_code_real_type r[3];
252b5132 2660 va_list args;
252b5132
RH
2661
2662#ifdef USE_STDARG
2663 va_start (args, fmt);
2664#else
2665 va_start (args);
2666#endif
2667
2668 /*
2669 * If the macro is about to expand into a second instruction,
2670 * print a warning if needed. We need to pass ip as a parameter
2671 * to generate a better warning message here...
2672 */
2673 if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
2674 as_warn (_("Macro instruction expanded into multiple instructions"));
2675
80cc45a5
EC
2676 /*
2677 * If the macro is about to expand into a second instruction,
2678 * and it is in a delay slot, print a warning.
2679 */
2680 if (place == NULL
2681 && *counter == 1
2682 && mips_opts.noreorder
2683 && (prev_prev_insn.insn_mo->pinfo
2684 & (INSN_UNCOND_BRANCH_DELAY | INSN_COND_BRANCH_DELAY
2b2e39bf 2685 | INSN_COND_BRANCH_LIKELY)) != 0)
80cc45a5
EC
2686 as_warn (_("Macro instruction expanded into multiple instructions in a branch delay slot"));
2687
252b5132 2688 if (place == NULL)
f9419b05 2689 ++*counter; /* bump instruction counter */
252b5132
RH
2690
2691 if (mips_opts.mips16)
2692 {
2693 mips16_macro_build (place, counter, ep, name, fmt, args);
2694 va_end (args);
2695 return;
2696 }
2697
f6688943
TS
2698 r[0] = BFD_RELOC_UNUSED;
2699 r[1] = BFD_RELOC_UNUSED;
2700 r[2] = BFD_RELOC_UNUSED;
252b5132
RH
2701 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
2702 assert (insn.insn_mo);
2703 assert (strcmp (name, insn.insn_mo->name) == 0);
2704
2705 /* Search until we get a match for NAME. */
2706 while (1)
2707 {
deec1734
CD
2708 /* It is assumed here that macros will never generate
2709 MDMX or MIPS-3D instructions. */
252b5132
RH
2710 if (strcmp (fmt, insn.insn_mo->args) == 0
2711 && insn.insn_mo->pinfo != INSN_MACRO
aec421e0
TS
2712 && OPCODE_IS_MEMBER (insn.insn_mo,
2713 (mips_opts.isa
2714 | (mips_opts.mips16 ? INSN_MIPS16 : 0)),
2715 mips_arch)
ec68c924 2716 && (mips_arch != CPU_R4650 || (insn.insn_mo->pinfo & FP_D) == 0))
252b5132
RH
2717 break;
2718
2719 ++insn.insn_mo;
2720 assert (insn.insn_mo->name);
2721 assert (strcmp (name, insn.insn_mo->name) == 0);
2722 }
2723
2724 insn.insn_opcode = insn.insn_mo->match;
2725 for (;;)
2726 {
2727 switch (*fmt++)
2728 {
2729 case '\0':
2730 break;
2731
2732 case ',':
2733 case '(':
2734 case ')':
2735 continue;
2736
2737 case 't':
2738 case 'w':
2739 case 'E':
38487616 2740 insn.insn_opcode |= va_arg (args, int) << OP_SH_RT;
252b5132
RH
2741 continue;
2742
2743 case 'c':
38487616
TS
2744 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE;
2745 continue;
2746
252b5132
RH
2747 case 'T':
2748 case 'W':
38487616 2749 insn.insn_opcode |= va_arg (args, int) << OP_SH_FT;
252b5132
RH
2750 continue;
2751
2752 case 'd':
2753 case 'G':
38487616 2754 insn.insn_opcode |= va_arg (args, int) << OP_SH_RD;
252b5132
RH
2755 continue;
2756
4372b673
NC
2757 case 'U':
2758 {
2759 int tmp = va_arg (args, int);
2760
38487616
TS
2761 insn.insn_opcode |= tmp << OP_SH_RT;
2762 insn.insn_opcode |= tmp << OP_SH_RD;
beae10d5 2763 continue;
4372b673
NC
2764 }
2765
252b5132
RH
2766 case 'V':
2767 case 'S':
38487616 2768 insn.insn_opcode |= va_arg (args, int) << OP_SH_FS;
252b5132
RH
2769 continue;
2770
2771 case 'z':
2772 continue;
2773
2774 case '<':
38487616 2775 insn.insn_opcode |= va_arg (args, int) << OP_SH_SHAMT;
252b5132
RH
2776 continue;
2777
2778 case 'D':
38487616 2779 insn.insn_opcode |= va_arg (args, int) << OP_SH_FD;
252b5132
RH
2780 continue;
2781
2782 case 'B':
38487616 2783 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE20;
252b5132
RH
2784 continue;
2785
4372b673 2786 case 'J':
38487616 2787 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE19;
4372b673
NC
2788 continue;
2789
252b5132 2790 case 'q':
38487616 2791 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE2;
252b5132
RH
2792 continue;
2793
2794 case 'b':
2795 case 's':
2796 case 'r':
2797 case 'v':
38487616 2798 insn.insn_opcode |= va_arg (args, int) << OP_SH_RS;
252b5132
RH
2799 continue;
2800
2801 case 'i':
2802 case 'j':
2803 case 'o':
f6688943 2804 *r = (bfd_reloc_code_real_type) va_arg (args, int);
cdf6fd85 2805 assert (*r == BFD_RELOC_GPREL16
f6688943
TS
2806 || *r == BFD_RELOC_MIPS_LITERAL
2807 || *r == BFD_RELOC_MIPS_HIGHER
2808 || *r == BFD_RELOC_HI16_S
2809 || *r == BFD_RELOC_LO16
2810 || *r == BFD_RELOC_MIPS_GOT16
2811 || *r == BFD_RELOC_MIPS_CALL16
438c16b8
TS
2812 || *r == BFD_RELOC_MIPS_GOT_DISP
2813 || *r == BFD_RELOC_MIPS_GOT_PAGE
2814 || *r == BFD_RELOC_MIPS_GOT_OFST
f6688943
TS
2815 || *r == BFD_RELOC_MIPS_GOT_LO16
2816 || *r == BFD_RELOC_MIPS_CALL_LO16
252b5132 2817 || (ep->X_op == O_subtract
f6688943 2818 && *r == BFD_RELOC_PCREL_LO16));
252b5132
RH
2819 continue;
2820
2821 case 'u':
f6688943 2822 *r = (bfd_reloc_code_real_type) va_arg (args, int);
252b5132
RH
2823 assert (ep != NULL
2824 && (ep->X_op == O_constant
2825 || (ep->X_op == O_symbol
f6688943
TS
2826 && (*r == BFD_RELOC_MIPS_HIGHEST
2827 || *r == BFD_RELOC_HI16_S
2828 || *r == BFD_RELOC_HI16
2829 || *r == BFD_RELOC_GPREL16
2830 || *r == BFD_RELOC_MIPS_GOT_HI16
2831 || *r == BFD_RELOC_MIPS_CALL_HI16))
252b5132 2832 || (ep->X_op == O_subtract
f6688943 2833 && *r == BFD_RELOC_PCREL_HI16_S)));
252b5132
RH
2834 continue;
2835
2836 case 'p':
2837 assert (ep != NULL);
2838 /*
2839 * This allows macro() to pass an immediate expression for
2840 * creating short branches without creating a symbol.
2841 * Note that the expression still might come from the assembly
2842 * input, in which case the value is not checked for range nor
2843 * is a relocation entry generated (yuck).
2844 */
2845 if (ep->X_op == O_constant)
2846 {
2847 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
2848 ep = NULL;
2849 }
2850 else
cb56d3d3 2851 if (mips_pic == EMBEDDED_PIC)
f6688943 2852 *r = BFD_RELOC_16_PCREL_S2;
cb56d3d3 2853 else
f6688943 2854 *r = BFD_RELOC_16_PCREL;
252b5132
RH
2855 continue;
2856
2857 case 'a':
2858 assert (ep != NULL);
f6688943 2859 *r = BFD_RELOC_MIPS_JMP;
252b5132
RH
2860 continue;
2861
2862 case 'C':
2863 insn.insn_opcode |= va_arg (args, unsigned long);
2864 continue;
2865
2866 default:
2867 internalError ();
2868 }
2869 break;
2870 }
2871 va_end (args);
f6688943 2872 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132
RH
2873
2874 append_insn (place, &insn, ep, r, false);
2875}
2876
2877static void
2878mips16_macro_build (place, counter, ep, name, fmt, args)
2879 char *place;
43841e91 2880 int *counter ATTRIBUTE_UNUSED;
252b5132
RH
2881 expressionS *ep;
2882 const char *name;
2883 const char *fmt;
2884 va_list args;
2885{
2886 struct mips_cl_insn insn;
f6688943
TS
2887 bfd_reloc_code_real_type r[3]
2888 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 2889
252b5132
RH
2890 insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
2891 assert (insn.insn_mo);
2892 assert (strcmp (name, insn.insn_mo->name) == 0);
2893
2894 while (strcmp (fmt, insn.insn_mo->args) != 0
2895 || insn.insn_mo->pinfo == INSN_MACRO)
2896 {
2897 ++insn.insn_mo;
2898 assert (insn.insn_mo->name);
2899 assert (strcmp (name, insn.insn_mo->name) == 0);
2900 }
2901
2902 insn.insn_opcode = insn.insn_mo->match;
2903 insn.use_extend = false;
2904
2905 for (;;)
2906 {
2907 int c;
2908
2909 c = *fmt++;
2910 switch (c)
2911 {
2912 case '\0':
2913 break;
2914
2915 case ',':
2916 case '(':
2917 case ')':
2918 continue;
2919
2920 case 'y':
2921 case 'w':
2922 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
2923 continue;
2924
2925 case 'x':
2926 case 'v':
2927 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
2928 continue;
2929
2930 case 'z':
2931 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
2932 continue;
2933
2934 case 'Z':
2935 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
2936 continue;
2937
2938 case '0':
2939 case 'S':
2940 case 'P':
2941 case 'R':
2942 continue;
2943
2944 case 'X':
2945 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
2946 continue;
2947
2948 case 'Y':
2949 {
2950 int regno;
2951
2952 regno = va_arg (args, int);
2953 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
2954 insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
2955 }
2956 continue;
2957
2958 case '<':
2959 case '>':
2960 case '4':
2961 case '5':
2962 case 'H':
2963 case 'W':
2964 case 'D':
2965 case 'j':
2966 case '8':
2967 case 'V':
2968 case 'C':
2969 case 'U':
2970 case 'k':
2971 case 'K':
2972 case 'p':
2973 case 'q':
2974 {
2975 assert (ep != NULL);
2976
2977 if (ep->X_op != O_constant)
874e8986 2978 *r = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
2979 else
2980 {
c4e7957c
TS
2981 mips16_immed (NULL, 0, c, ep->X_add_number, false, false,
2982 false, &insn.insn_opcode, &insn.use_extend,
2983 &insn.extend);
252b5132 2984 ep = NULL;
f6688943 2985 *r = BFD_RELOC_UNUSED;
252b5132
RH
2986 }
2987 }
2988 continue;
2989
2990 case '6':
2991 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
2992 continue;
2993 }
2994
2995 break;
2996 }
2997
f6688943 2998 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132
RH
2999
3000 append_insn (place, &insn, ep, r, false);
3001}
3002
438c16b8
TS
3003/*
3004 * Generate a "jalr" instruction with a relocation hint to the called
3005 * function. This occurs in NewABI PIC code.
3006 */
3007static void
3008macro_build_jalr (icnt, ep)
3009 int icnt;
3010 expressionS *ep;
3011{
3012 if (HAVE_NEWABI)
3013 frag_more (0);
3014 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr", "d,s",
3015 RA, PIC_CALL_REG);
3016 if (HAVE_NEWABI)
3017 fix_new_exp (frag_now, 0, 0, ep, false, BFD_RELOC_MIPS_JALR);
3018}
3019
252b5132
RH
3020/*
3021 * Generate a "lui" instruction.
3022 */
3023static void
3024macro_build_lui (place, counter, ep, regnum)
3025 char *place;
3026 int *counter;
3027 expressionS *ep;
3028 int regnum;
3029{
3030 expressionS high_expr;
3031 struct mips_cl_insn insn;
f6688943
TS
3032 bfd_reloc_code_real_type r[3]
3033 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
5a38dc70
AM
3034 const char *name = "lui";
3035 const char *fmt = "t,u";
252b5132
RH
3036
3037 assert (! mips_opts.mips16);
3038
3039 if (place == NULL)
3040 high_expr = *ep;
3041 else
3042 {
3043 high_expr.X_op = O_constant;
3044 high_expr.X_add_number = ep->X_add_number;
3045 }
3046
3047 if (high_expr.X_op == O_constant)
3048 {
3049 /* we can compute the instruction now without a relocation entry */
e7d556df
TS
3050 high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3051 >> 16) & 0xffff;
f6688943 3052 *r = BFD_RELOC_UNUSED;
252b5132 3053 }
f6688943 3054 else if (! HAVE_NEWABI)
252b5132
RH
3055 {
3056 assert (ep->X_op == O_symbol);
3057 /* _gp_disp is a special case, used from s_cpload. */
3058 assert (mips_pic == NO_PIC
3059 || strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0);
f6688943 3060 *r = BFD_RELOC_HI16_S;
252b5132
RH
3061 }
3062
3063 /*
3064 * If the macro is about to expand into a second instruction,
3065 * print a warning if needed. We need to pass ip as a parameter
3066 * to generate a better warning message here...
3067 */
3068 if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
3069 as_warn (_("Macro instruction expanded into multiple instructions"));
3070
3071 if (place == NULL)
f9419b05 3072 ++*counter; /* bump instruction counter */
252b5132
RH
3073
3074 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3075 assert (insn.insn_mo);
3076 assert (strcmp (name, insn.insn_mo->name) == 0);
3077 assert (strcmp (fmt, insn.insn_mo->args) == 0);
3078
3079 insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
f6688943 3080 if (*r == BFD_RELOC_UNUSED)
252b5132
RH
3081 {
3082 insn.insn_opcode |= high_expr.X_add_number;
3083 append_insn (place, &insn, NULL, r, false);
3084 }
3085 else
3086 append_insn (place, &insn, &high_expr, r, false);
3087}
3088
3089/* set_at()
3090 * Generates code to set the $at register to true (one)
3091 * if reg is less than the immediate expression.
3092 */
3093static void
3094set_at (counter, reg, unsignedp)
3095 int *counter;
3096 int reg;
3097 int unsignedp;
3098{
3099 if (imm_expr.X_op == O_constant
3100 && imm_expr.X_add_number >= -0x8000
3101 && imm_expr.X_add_number < 0x8000)
3102 macro_build ((char *) NULL, counter, &imm_expr,
3103 unsignedp ? "sltiu" : "slti",
3104 "t,r,j", AT, reg, (int) BFD_RELOC_LO16);
3105 else
3106 {
4d34fb5f 3107 load_register (counter, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9 3108 macro_build ((char *) NULL, counter, (expressionS *) NULL,
252b5132
RH
3109 unsignedp ? "sltu" : "slt",
3110 "d,v,t", AT, reg, AT);
3111 }
3112}
3113
3114/* Warn if an expression is not a constant. */
3115
3116static void
3117check_absolute_expr (ip, ex)
3118 struct mips_cl_insn *ip;
3119 expressionS *ex;
3120{
3121 if (ex->X_op == O_big)
3122 as_bad (_("unsupported large constant"));
3123 else if (ex->X_op != O_constant)
3124 as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
3125}
3126
3127/* Count the leading zeroes by performing a binary chop. This is a
3128 bulky bit of source, but performance is a LOT better for the
3129 majority of values than a simple loop to count the bits:
3130 for (lcnt = 0; (lcnt < 32); lcnt++)
3131 if ((v) & (1 << (31 - lcnt)))
3132 break;
3133 However it is not code size friendly, and the gain will drop a bit
3134 on certain cached systems.
3135*/
3136#define COUNT_TOP_ZEROES(v) \
3137 (((v) & ~0xffff) == 0 \
3138 ? ((v) & ~0xff) == 0 \
3139 ? ((v) & ~0xf) == 0 \
3140 ? ((v) & ~0x3) == 0 \
3141 ? ((v) & ~0x1) == 0 \
3142 ? !(v) \
3143 ? 32 \
3144 : 31 \
3145 : 30 \
3146 : ((v) & ~0x7) == 0 \
3147 ? 29 \
3148 : 28 \
3149 : ((v) & ~0x3f) == 0 \
3150 ? ((v) & ~0x1f) == 0 \
3151 ? 27 \
3152 : 26 \
3153 : ((v) & ~0x7f) == 0 \
3154 ? 25 \
3155 : 24 \
3156 : ((v) & ~0xfff) == 0 \
3157 ? ((v) & ~0x3ff) == 0 \
3158 ? ((v) & ~0x1ff) == 0 \
3159 ? 23 \
3160 : 22 \
3161 : ((v) & ~0x7ff) == 0 \
3162 ? 21 \
3163 : 20 \
3164 : ((v) & ~0x3fff) == 0 \
3165 ? ((v) & ~0x1fff) == 0 \
3166 ? 19 \
3167 : 18 \
3168 : ((v) & ~0x7fff) == 0 \
3169 ? 17 \
3170 : 16 \
3171 : ((v) & ~0xffffff) == 0 \
3172 ? ((v) & ~0xfffff) == 0 \
3173 ? ((v) & ~0x3ffff) == 0 \
3174 ? ((v) & ~0x1ffff) == 0 \
3175 ? 15 \
3176 : 14 \
3177 : ((v) & ~0x7ffff) == 0 \
3178 ? 13 \
3179 : 12 \
3180 : ((v) & ~0x3fffff) == 0 \
3181 ? ((v) & ~0x1fffff) == 0 \
3182 ? 11 \
3183 : 10 \
3184 : ((v) & ~0x7fffff) == 0 \
3185 ? 9 \
3186 : 8 \
3187 : ((v) & ~0xfffffff) == 0 \
3188 ? ((v) & ~0x3ffffff) == 0 \
3189 ? ((v) & ~0x1ffffff) == 0 \
3190 ? 7 \
3191 : 6 \
3192 : ((v) & ~0x7ffffff) == 0 \
3193 ? 5 \
3194 : 4 \
3195 : ((v) & ~0x3fffffff) == 0 \
3196 ? ((v) & ~0x1fffffff) == 0 \
3197 ? 3 \
3198 : 2 \
3199 : ((v) & ~0x7fffffff) == 0 \
3200 ? 1 \
3201 : 0)
3202
6373ee54
CD
3203/* Is the given value a sign-extended 32-bit value? */
3204#define IS_SEXT_32BIT_NUM(x) \
3205 (((x) &~ (offsetT) 0x7fffffff) == 0 \
3206 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
3207
252b5132
RH
3208/* load_register()
3209 * This routine generates the least number of instructions neccessary to load
3210 * an absolute expression value into a register.
3211 */
3212static void
3213load_register (counter, reg, ep, dbl)
3214 int *counter;
3215 int reg;
3216 expressionS *ep;
3217 int dbl;
3218{
3219 int freg;
3220 expressionS hi32, lo32;
3221
3222 if (ep->X_op != O_big)
3223 {
3224 assert (ep->X_op == O_constant);
3225 if (ep->X_add_number < 0x8000
3226 && (ep->X_add_number >= 0
3227 || (ep->X_add_number >= -0x8000
3228 && (! dbl
3229 || ! ep->X_unsigned
3230 || sizeof (ep->X_add_number) > 4))))
3231 {
3232 /* We can handle 16 bit signed values with an addiu to
3233 $zero. No need to ever use daddiu here, since $zero and
3234 the result are always correct in 32 bit mode. */
3235 macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3236 (int) BFD_RELOC_LO16);
3237 return;
3238 }
3239 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3240 {
3241 /* We can handle 16 bit unsigned values with an ori to
3242 $zero. */
3243 macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, 0,
3244 (int) BFD_RELOC_LO16);
3245 return;
3246 }
6373ee54 3247 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)
252b5132
RH
3248 && (! dbl
3249 || ! ep->X_unsigned
3250 || sizeof (ep->X_add_number) > 4
3251 || (ep->X_add_number & 0x80000000) == 0))
ca4e0257 3252 || ((HAVE_32BIT_GPRS || ! dbl)
252b5132 3253 && (ep->X_add_number &~ (offsetT) 0xffffffff) == 0)
ca4e0257 3254 || (HAVE_32BIT_GPRS
252b5132
RH
3255 && ! dbl
3256 && ((ep->X_add_number &~ (offsetT) 0xffffffff)
3257 == ~ (offsetT) 0xffffffff)))
3258 {
3259 /* 32 bit values require an lui. */
3260 macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3261 (int) BFD_RELOC_HI16);
3262 if ((ep->X_add_number & 0xffff) != 0)
3263 macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, reg,
3264 (int) BFD_RELOC_LO16);
3265 return;
3266 }
3267 }
3268
3269 /* The value is larger than 32 bits. */
3270
ca4e0257 3271 if (HAVE_32BIT_GPRS)
252b5132 3272 {
956cd1d6
TS
3273 as_bad (_("Number (0x%lx) larger than 32 bits"),
3274 (unsigned long) ep->X_add_number);
252b5132
RH
3275 macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3276 (int) BFD_RELOC_LO16);
3277 return;
3278 }
3279
3280 if (ep->X_op != O_big)
3281 {
3282 hi32 = *ep;
3283 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3284 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3285 hi32.X_add_number &= 0xffffffff;
3286 lo32 = *ep;
3287 lo32.X_add_number &= 0xffffffff;
3288 }
3289 else
3290 {
3291 assert (ep->X_add_number > 2);
3292 if (ep->X_add_number == 3)
3293 generic_bignum[3] = 0;
3294 else if (ep->X_add_number > 4)
3295 as_bad (_("Number larger than 64 bits"));
3296 lo32.X_op = O_constant;
3297 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3298 hi32.X_op = O_constant;
3299 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3300 }
3301
3302 if (hi32.X_add_number == 0)
3303 freg = 0;
3304 else
3305 {
3306 int shift, bit;
3307 unsigned long hi, lo;
3308
956cd1d6 3309 if (hi32.X_add_number == (offsetT) 0xffffffff)
beae10d5
KH
3310 {
3311 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3312 {
3313 macro_build ((char *) NULL, counter, &lo32, "addiu", "t,r,j",
252b5132 3314 reg, 0, (int) BFD_RELOC_LO16);
beae10d5
KH
3315 return;
3316 }
3317 if (lo32.X_add_number & 0x80000000)
3318 {
3319 macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3320 (int) BFD_RELOC_HI16);
252b5132
RH
3321 if (lo32.X_add_number & 0xffff)
3322 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i",
3323 reg, reg, (int) BFD_RELOC_LO16);
beae10d5
KH
3324 return;
3325 }
3326 }
252b5132
RH
3327
3328 /* Check for 16bit shifted constant. We know that hi32 is
3329 non-zero, so start the mask on the first bit of the hi32
3330 value. */
3331 shift = 17;
3332 do
beae10d5
KH
3333 {
3334 unsigned long himask, lomask;
3335
3336 if (shift < 32)
3337 {
3338 himask = 0xffff >> (32 - shift);
3339 lomask = (0xffff << shift) & 0xffffffff;
3340 }
3341 else
3342 {
3343 himask = 0xffff << (shift - 32);
3344 lomask = 0;
3345 }
3346 if ((hi32.X_add_number & ~(offsetT) himask) == 0
3347 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3348 {
3349 expressionS tmp;
3350
3351 tmp.X_op = O_constant;
3352 if (shift < 32)
3353 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3354 | (lo32.X_add_number >> shift));
3355 else
3356 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
3357 macro_build ((char *) NULL, counter, &tmp,
3358 "ori", "t,r,i", reg, 0,
3359 (int) BFD_RELOC_LO16);
2396cfb9 3360 macro_build ((char *) NULL, counter, (expressionS *) NULL,
beae10d5
KH
3361 (shift >= 32) ? "dsll32" : "dsll",
3362 "d,w,<", reg, reg,
3363 (shift >= 32) ? shift - 32 : shift);
3364 return;
3365 }
f9419b05 3366 ++shift;
beae10d5
KH
3367 }
3368 while (shift <= (64 - 16));
252b5132
RH
3369
3370 /* Find the bit number of the lowest one bit, and store the
3371 shifted value in hi/lo. */
3372 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3373 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3374 if (lo != 0)
3375 {
3376 bit = 0;
3377 while ((lo & 1) == 0)
3378 {
3379 lo >>= 1;
3380 ++bit;
3381 }
3382 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3383 hi >>= bit;
3384 }
3385 else
3386 {
3387 bit = 32;
3388 while ((hi & 1) == 0)
3389 {
3390 hi >>= 1;
3391 ++bit;
3392 }
3393 lo = hi;
3394 hi = 0;
3395 }
3396
3397 /* Optimize if the shifted value is a (power of 2) - 1. */
3398 if ((hi == 0 && ((lo + 1) & lo) == 0)
3399 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
beae10d5
KH
3400 {
3401 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
252b5132 3402 if (shift != 0)
beae10d5 3403 {
252b5132
RH
3404 expressionS tmp;
3405
3406 /* This instruction will set the register to be all
3407 ones. */
beae10d5
KH
3408 tmp.X_op = O_constant;
3409 tmp.X_add_number = (offsetT) -1;
3410 macro_build ((char *) NULL, counter, &tmp, "addiu", "t,r,j",
252b5132 3411 reg, 0, (int) BFD_RELOC_LO16);
beae10d5
KH
3412 if (bit != 0)
3413 {
3414 bit += shift;
2396cfb9 3415 macro_build ((char *) NULL, counter, (expressionS *) NULL,
beae10d5
KH
3416 (bit >= 32) ? "dsll32" : "dsll",
3417 "d,w,<", reg, reg,
3418 (bit >= 32) ? bit - 32 : bit);
3419 }
2396cfb9 3420 macro_build ((char *) NULL, counter, (expressionS *) NULL,
252b5132 3421 (shift >= 32) ? "dsrl32" : "dsrl",
beae10d5 3422 "d,w,<", reg, reg,
252b5132 3423 (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
3424 return;
3425 }
3426 }
252b5132
RH
3427
3428 /* Sign extend hi32 before calling load_register, because we can
3429 generally get better code when we load a sign extended value. */
3430 if ((hi32.X_add_number & 0x80000000) != 0)
beae10d5 3431 hi32.X_add_number |= ~(offsetT) 0xffffffff;
252b5132
RH
3432 load_register (counter, reg, &hi32, 0);
3433 freg = reg;
3434 }
3435 if ((lo32.X_add_number & 0xffff0000) == 0)
3436 {
3437 if (freg != 0)
3438 {
2396cfb9
TS
3439 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3440 "dsll32", "d,w,<", reg, freg, 0);
252b5132
RH
3441 freg = reg;
3442 }
3443 }
3444 else
3445 {
3446 expressionS mid16;
3447
956cd1d6 3448 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
beae10d5 3449 {
252b5132
RH
3450 macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3451 (int) BFD_RELOC_HI16);
956cd1d6
TS
3452 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3453 "dsrl32", "d,w,<", reg, reg, 0);
beae10d5
KH
3454 return;
3455 }
252b5132
RH
3456
3457 if (freg != 0)
3458 {
956cd1d6
TS
3459 macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3460 "d,w,<", reg, freg, 16);
252b5132
RH
3461 freg = reg;
3462 }
3463 mid16 = lo32;
3464 mid16.X_add_number >>= 16;
3465 macro_build ((char *) NULL, counter, &mid16, "ori", "t,r,i", reg,
3466 freg, (int) BFD_RELOC_LO16);
956cd1d6
TS
3467 macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3468 "d,w,<", reg, reg, 16);
252b5132
RH
3469 freg = reg;
3470 }
3471 if ((lo32.X_add_number & 0xffff) != 0)
3472 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i", reg, freg,
3473 (int) BFD_RELOC_LO16);
3474}
3475
3476/* Load an address into a register. */
3477
3478static void
c9914766 3479load_address (counter, reg, ep, used_at)
252b5132
RH
3480 int *counter;
3481 int reg;
3482 expressionS *ep;
d6bc6245 3483 int *used_at;
252b5132 3484{
f9419b05 3485 char *p = NULL;
252b5132
RH
3486
3487 if (ep->X_op != O_constant
3488 && ep->X_op != O_symbol)
3489 {
3490 as_bad (_("expression too complex"));
3491 ep->X_op = O_constant;
3492 }
3493
3494 if (ep->X_op == O_constant)
3495 {
c9914766 3496 load_register (counter, reg, ep, HAVE_64BIT_ADDRESSES);
252b5132
RH
3497 return;
3498 }
3499
3500 if (mips_pic == NO_PIC)
3501 {
3502 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 3503 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
3504 Otherwise we want
3505 lui $reg,<sym> (BFD_RELOC_HI16_S)
3506 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
d6bc6245 3507 If we have an addend, we always use the latter form.
76b3015f 3508
d6bc6245
TS
3509 With 64bit address space and a usable $at we want
3510 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3511 lui $at,<sym> (BFD_RELOC_HI16_S)
3512 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3513 daddiu $at,<sym> (BFD_RELOC_LO16)
3514 dsll32 $reg,0
3a482fd5 3515 daddu $reg,$reg,$at
76b3015f 3516
d6bc6245
TS
3517 If $at is already in use, we use an path which is suboptimal
3518 on superscalar processors.
3519 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3520 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3521 dsll $reg,16
3522 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
3523 dsll $reg,16
3524 daddiu $reg,<sym> (BFD_RELOC_LO16)
3525 */
c9914766 3526 if (HAVE_64BIT_ADDRESSES)
d6bc6245 3527 {
d6bc6245
TS
3528 /* We don't do GP optimization for now because RELAX_ENCODE can't
3529 hold the data for such large chunks. */
3530
460597ba 3531 if (*used_at == 0 && ! mips_opts.noat)
d6bc6245
TS
3532 {
3533 macro_build (p, counter, ep, "lui", "t,u",
3534 reg, (int) BFD_RELOC_MIPS_HIGHEST);
3535 macro_build (p, counter, ep, "lui", "t,u",
3536 AT, (int) BFD_RELOC_HI16_S);
3537 macro_build (p, counter, ep, "daddiu", "t,r,j",
3538 reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3539 macro_build (p, counter, ep, "daddiu", "t,r,j",
3540 AT, AT, (int) BFD_RELOC_LO16);
2396cfb9
TS
3541 macro_build (p, counter, (expressionS *) NULL, "dsll32",
3542 "d,w,<", reg, reg, 0);
3a482fd5 3543 macro_build (p, counter, (expressionS *) NULL, "daddu",
2396cfb9 3544 "d,v,t", reg, reg, AT);
d6bc6245
TS
3545 *used_at = 1;
3546 }
3547 else
3548 {
3549 macro_build (p, counter, ep, "lui", "t,u",
3550 reg, (int) BFD_RELOC_MIPS_HIGHEST);
3551 macro_build (p, counter, ep, "daddiu", "t,r,j",
3552 reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
2396cfb9
TS
3553 macro_build (p, counter, (expressionS *) NULL, "dsll",
3554 "d,w,<", reg, reg, 16);
d6bc6245
TS
3555 macro_build (p, counter, ep, "daddiu", "t,r,j",
3556 reg, reg, (int) BFD_RELOC_HI16_S);
2396cfb9
TS
3557 macro_build (p, counter, (expressionS *) NULL, "dsll",
3558 "d,w,<", reg, reg, 16);
d6bc6245
TS
3559 macro_build (p, counter, ep, "daddiu", "t,r,j",
3560 reg, reg, (int) BFD_RELOC_LO16);
3561 }
3562 }
252b5132
RH
3563 else
3564 {
d6bc6245
TS
3565 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3566 && ! nopic_need_relax (ep->X_add_symbol, 1))
3567 {
3568 frag_grow (20);
3569 macro_build ((char *) NULL, counter, ep,
c9914766
TS
3570 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
3571 reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
d6bc6245
TS
3572 p = frag_var (rs_machine_dependent, 8, 0,
3573 RELAX_ENCODE (4, 8, 0, 4, 0,
3574 mips_opts.warn_about_macros),
956cd1d6 3575 ep->X_add_symbol, 0, NULL);
d6bc6245
TS
3576 }
3577 macro_build_lui (p, counter, ep, reg);
3578 if (p != NULL)
3579 p += 4;
c9914766
TS
3580 macro_build (p, counter, ep,
3581 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
d6bc6245
TS
3582 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3583 }
252b5132
RH
3584 }
3585 else if (mips_pic == SVR4_PIC && ! mips_big_got)
3586 {
3587 expressionS ex;
3588
3589 /* If this is a reference to an external symbol, we want
3590 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3591 Otherwise we want
3592 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3593 nop
3594 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3595 If there is a constant, it must be added in after. */
3596 ex.X_add_number = ep->X_add_number;
3597 ep->X_add_number = 0;
3598 frag_grow (20);
3599 macro_build ((char *) NULL, counter, ep,
c9914766
TS
3600 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
3601 reg, (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132
RH
3602 macro_build ((char *) NULL, counter, (expressionS *) NULL, "nop", "");
3603 p = frag_var (rs_machine_dependent, 4, 0,
3604 RELAX_ENCODE (0, 4, -8, 0, 0, mips_opts.warn_about_macros),
3605 ep->X_add_symbol, (offsetT) 0, (char *) NULL);
3606 macro_build (p, counter, ep,
ca4e0257 3607 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
3608 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3609 if (ex.X_add_number != 0)
3610 {
3611 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3612 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3613 ex.X_op = O_constant;
3614 macro_build ((char *) NULL, counter, &ex,
ca4e0257 3615 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
3616 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3617 }
3618 }
3619 else if (mips_pic == SVR4_PIC)
3620 {
3621 expressionS ex;
3622 int off;
3623
3624 /* This is the large GOT case. If this is a reference to an
3625 external symbol, we want
3626 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
3627 addu $reg,$reg,$gp
3628 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
3629 Otherwise, for a reference to a local symbol, we want
3630 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3631 nop
3632 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
438c16b8
TS
3633 If we have NewABI, we want
3634 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
3635 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
252b5132
RH
3636 If there is a constant, it must be added in after. */
3637 ex.X_add_number = ep->X_add_number;
3638 ep->X_add_number = 0;
438c16b8
TS
3639 if (HAVE_NEWABI)
3640 {
3641 macro_build ((char *) NULL, counter, ep,
3642 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3643 (int) BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
3644 macro_build (p, counter, ep,
3645 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
3646 reg, reg, (int) BFD_RELOC_MIPS_GOT_OFST);
3647 }
252b5132 3648 else
438c16b8
TS
3649 {
3650 if (reg_needs_delay (mips_gp_register))
3651 off = 4;
3652 else
3653 off = 0;
3654 frag_grow (32);
3655 macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3656 (int) BFD_RELOC_MIPS_GOT_HI16);
3657 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3658 HAVE_32BIT_ADDRESSES ? "addu" : "daddu", "d,v,t", reg,
3659 reg, mips_gp_register);
3660 macro_build ((char *) NULL, counter, ep,
3661 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
3662 "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT_LO16, reg);
3663 p = frag_var (rs_machine_dependent, 12 + off, 0,
3664 RELAX_ENCODE (12, 12 + off, off, 8 + off, 0,
3665 mips_opts.warn_about_macros),
3666 ep->X_add_symbol, 0, NULL);
3667 if (off > 0)
3668 {
3669 /* We need a nop before loading from $gp. This special
3670 check is required because the lui which starts the main
3671 instruction stream does not refer to $gp, and so will not
3672 insert the nop which may be required. */
3673 macro_build (p, counter, (expressionS *) NULL, "nop", "");
3674 p += 4;
3675 }
3676 macro_build (p, counter, ep,
3677 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3678 (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
3679 p += 4;
252b5132
RH
3680 macro_build (p, counter, (expressionS *) NULL, "nop", "");
3681 p += 4;
438c16b8
TS
3682 macro_build (p, counter, ep,
3683 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3684 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
252b5132 3685 }
438c16b8 3686
252b5132
RH
3687 if (ex.X_add_number != 0)
3688 {
3689 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3690 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3691 ex.X_op = O_constant;
f7ea7ef2
TS
3692 macro_build ((char *) NULL, counter, &ex,
3693 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
3694 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3695 }
3696 }
3697 else if (mips_pic == EMBEDDED_PIC)
3698 {
3699 /* We always do
cdf6fd85 3700 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
c9914766
TS
3701 */
3702 macro_build ((char *) NULL, counter, ep,
3703 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3704 "t,r,j", reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
252b5132
RH
3705 }
3706 else
3707 abort ();
3708}
3709
ea1fb5dc
RS
3710/* Move the contents of register SOURCE into register DEST. */
3711
3712static void
3713move_register (counter, dest, source)
3714 int *counter;
3715 int dest;
3716 int source;
3717{
3718 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3719 HAVE_32BIT_GPRS ? "addu" : "daddu",
3720 "d,v,t", dest, source, 0);
3721}
3722
252b5132
RH
3723/*
3724 * Build macros
3725 * This routine implements the seemingly endless macro or synthesized
3726 * instructions and addressing modes in the mips assembly language. Many
3727 * of these macros are simple and are similar to each other. These could
3728 * probably be handled by some kind of table or grammer aproach instead of
3729 * this verbose method. Others are not simple macros but are more like
3730 * optimizing code generation.
3731 * One interesting optimization is when several store macros appear
3732 * consecutivly that would load AT with the upper half of the same address.
3733 * The ensuing load upper instructions are ommited. This implies some kind
3734 * of global optimization. We currently only optimize within a single macro.
3735 * For many of the load and store macros if the address is specified as a
3736 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
3737 * first load register 'at' with zero and use it as the base register. The
3738 * mips assembler simply uses register $zero. Just one tiny optimization
3739 * we're missing.
3740 */
3741static void
3742macro (ip)
3743 struct mips_cl_insn *ip;
3744{
3745 register int treg, sreg, dreg, breg;
3746 int tempreg;
3747 int mask;
3748 int icnt = 0;
43841e91 3749 int used_at = 0;
252b5132
RH
3750 expressionS expr1;
3751 const char *s;
3752 const char *s2;
3753 const char *fmt;
3754 int likely = 0;
3755 int dbl = 0;
3756 int coproc = 0;
3757 int lr = 0;
3758 int imm = 0;
3759 offsetT maxnum;
3760 int off;
3761 bfd_reloc_code_real_type r;
252b5132
RH
3762 int hold_mips_optimize;
3763
3764 assert (! mips_opts.mips16);
3765
3766 treg = (ip->insn_opcode >> 16) & 0x1f;
3767 dreg = (ip->insn_opcode >> 11) & 0x1f;
3768 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
3769 mask = ip->insn_mo->mask;
3770
3771 expr1.X_op = O_constant;
3772 expr1.X_op_symbol = NULL;
3773 expr1.X_add_symbol = NULL;
3774 expr1.X_add_number = 1;
3775
3776 switch (mask)
3777 {
3778 case M_DABS:
3779 dbl = 1;
3780 case M_ABS:
3781 /* bgez $a0,.+12
3782 move v0,$a0
3783 sub v0,$zero,$a0
3784 */
3785
3786 mips_emit_delays (true);
3787 ++mips_opts.noreorder;
3788 mips_any_noreorder = 1;
3789
3790 expr1.X_add_number = 8;
3791 macro_build ((char *) NULL, &icnt, &expr1, "bgez", "s,p", sreg);
3792 if (dreg == sreg)
2396cfb9
TS
3793 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
3794 0);
252b5132 3795 else
ea1fb5dc 3796 move_register (&icnt, dreg, sreg);
2396cfb9 3797 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 3798 dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
252b5132
RH
3799
3800 --mips_opts.noreorder;
3801 return;
3802
3803 case M_ADD_I:
3804 s = "addi";
3805 s2 = "add";
3806 goto do_addi;
3807 case M_ADDU_I:
3808 s = "addiu";
3809 s2 = "addu";
3810 goto do_addi;
3811 case M_DADD_I:
3812 dbl = 1;
3813 s = "daddi";
3814 s2 = "dadd";
3815 goto do_addi;
3816 case M_DADDU_I:
3817 dbl = 1;
3818 s = "daddiu";
3819 s2 = "daddu";
3820 do_addi:
3821 if (imm_expr.X_op == O_constant
3822 && imm_expr.X_add_number >= -0x8000
3823 && imm_expr.X_add_number < 0x8000)
3824 {
3825 macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,j", treg, sreg,
3826 (int) BFD_RELOC_LO16);
3827 return;
3828 }
3829 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9
TS
3830 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
3831 treg, sreg, AT);
252b5132
RH
3832 break;
3833
3834 case M_AND_I:
3835 s = "andi";
3836 s2 = "and";
3837 goto do_bit;
3838 case M_OR_I:
3839 s = "ori";
3840 s2 = "or";
3841 goto do_bit;
3842 case M_NOR_I:
3843 s = "";
3844 s2 = "nor";
3845 goto do_bit;
3846 case M_XOR_I:
3847 s = "xori";
3848 s2 = "xor";
3849 do_bit:
3850 if (imm_expr.X_op == O_constant
3851 && imm_expr.X_add_number >= 0
3852 && imm_expr.X_add_number < 0x10000)
3853 {
3854 if (mask != M_NOR_I)
3855 macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,i", treg,
3856 sreg, (int) BFD_RELOC_LO16);
3857 else
3858 {
3859 macro_build ((char *) NULL, &icnt, &imm_expr, "ori", "t,r,i",
3860 treg, sreg, (int) BFD_RELOC_LO16);
2396cfb9
TS
3861 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nor",
3862 "d,v,t", treg, treg, 0);
252b5132
RH
3863 }
3864 return;
3865 }
3866
d6bc6245 3867 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
3868 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
3869 treg, sreg, AT);
252b5132
RH
3870 break;
3871
3872 case M_BEQ_I:
3873 s = "beq";
3874 goto beq_i;
3875 case M_BEQL_I:
3876 s = "beql";
3877 likely = 1;
3878 goto beq_i;
3879 case M_BNE_I:
3880 s = "bne";
3881 goto beq_i;
3882 case M_BNEL_I:
3883 s = "bnel";
3884 likely = 1;
3885 beq_i:
3886 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
3887 {
3888 macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg,
3889 0);
3890 return;
3891 }
4d34fb5f 3892 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
252b5132
RH
3893 macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg, AT);
3894 break;
3895
3896 case M_BGEL:
3897 likely = 1;
3898 case M_BGE:
3899 if (treg == 0)
3900 {
3901 macro_build ((char *) NULL, &icnt, &offset_expr,
2396cfb9 3902 likely ? "bgezl" : "bgez", "s,p", sreg);
252b5132
RH
3903 return;
3904 }
3905 if (sreg == 0)
3906 {
3907 macro_build ((char *) NULL, &icnt, &offset_expr,
2396cfb9 3908 likely ? "blezl" : "blez", "s,p", treg);
252b5132
RH
3909 return;
3910 }
2396cfb9
TS
3911 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
3912 AT, sreg, treg);
252b5132 3913 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 3914 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
3915 break;
3916
3917 case M_BGTL_I:
3918 likely = 1;
3919 case M_BGT_I:
3920 /* check for > max integer */
3921 maxnum = 0x7fffffff;
ca4e0257 3922 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
3923 {
3924 maxnum <<= 16;
3925 maxnum |= 0xffff;
3926 maxnum <<= 16;
3927 maxnum |= 0xffff;
3928 }
3929 if (imm_expr.X_op == O_constant
3930 && imm_expr.X_add_number >= maxnum
ca4e0257 3931 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
3932 {
3933 do_false:
3934 /* result is always false */
3935 if (! likely)
3936 {
39c0a331
L
3937 if (warn_nops)
3938 as_warn (_("Branch %s is always false (nop)"),
3939 ip->insn_mo->name);
2396cfb9
TS
3940 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop",
3941 "", 0);
252b5132
RH
3942 }
3943 else
3944 {
39c0a331
L
3945 if (warn_nops)
3946 as_warn (_("Branch likely %s is always false"),
3947 ip->insn_mo->name);
252b5132
RH
3948 macro_build ((char *) NULL, &icnt, &offset_expr, "bnel",
3949 "s,t,p", 0, 0);
3950 }
3951 return;
3952 }
3953 if (imm_expr.X_op != O_constant)
3954 as_bad (_("Unsupported large constant"));
f9419b05 3955 ++imm_expr.X_add_number;
252b5132
RH
3956 /* FALLTHROUGH */
3957 case M_BGE_I:
3958 case M_BGEL_I:
3959 if (mask == M_BGEL_I)
3960 likely = 1;
3961 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
3962 {
3963 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 3964 likely ? "bgezl" : "bgez", "s,p", sreg);
252b5132
RH
3965 return;
3966 }
3967 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
3968 {
3969 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 3970 likely ? "bgtzl" : "bgtz", "s,p", sreg);
252b5132
RH
3971 return;
3972 }
3973 maxnum = 0x7fffffff;
ca4e0257 3974 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
3975 {
3976 maxnum <<= 16;
3977 maxnum |= 0xffff;
3978 maxnum <<= 16;
3979 maxnum |= 0xffff;
3980 }
3981 maxnum = - maxnum - 1;
3982 if (imm_expr.X_op == O_constant
3983 && imm_expr.X_add_number <= maxnum
ca4e0257 3984 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
3985 {
3986 do_true:
3987 /* result is always true */
3988 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
3989 macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
3990 return;
3991 }
3992 set_at (&icnt, sreg, 0);
3993 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 3994 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
3995 break;
3996
3997 case M_BGEUL:
3998 likely = 1;
3999 case M_BGEU:
4000 if (treg == 0)
4001 goto do_true;
4002 if (sreg == 0)
4003 {
4004 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4005 likely ? "beql" : "beq", "s,t,p", 0, treg);
252b5132
RH
4006 return;
4007 }
2396cfb9
TS
4008 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4009 "d,v,t", AT, sreg, treg);
252b5132 4010 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4011 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4012 break;
4013
4014 case M_BGTUL_I:
4015 likely = 1;
4016 case M_BGTU_I:
4017 if (sreg == 0
ca4e0257 4018 || (HAVE_32BIT_GPRS
252b5132 4019 && imm_expr.X_op == O_constant
956cd1d6 4020 && imm_expr.X_add_number == (offsetT) 0xffffffff))
252b5132
RH
4021 goto do_false;
4022 if (imm_expr.X_op != O_constant)
4023 as_bad (_("Unsupported large constant"));
f9419b05 4024 ++imm_expr.X_add_number;
252b5132
RH
4025 /* FALLTHROUGH */
4026 case M_BGEU_I:
4027 case M_BGEUL_I:
4028 if (mask == M_BGEUL_I)
4029 likely = 1;
4030 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4031 goto do_true;
4032 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4033 {
4034 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4035 likely ? "bnel" : "bne", "s,t,p", sreg, 0);
252b5132
RH
4036 return;
4037 }
4038 set_at (&icnt, sreg, 1);
4039 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4040 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4041 break;
4042
4043 case M_BGTL:
4044 likely = 1;
4045 case M_BGT:
4046 if (treg == 0)
4047 {
4048 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4049 likely ? "bgtzl" : "bgtz", "s,p", sreg);
252b5132
RH
4050 return;
4051 }
4052 if (sreg == 0)
4053 {
4054 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4055 likely ? "bltzl" : "bltz", "s,p", treg);
252b5132
RH
4056 return;
4057 }
2396cfb9
TS
4058 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4059 AT, treg, sreg);
252b5132 4060 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4061 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4062 break;
4063
4064 case M_BGTUL:
4065 likely = 1;
4066 case M_BGTU:
4067 if (treg == 0)
4068 {
4069 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4070 likely ? "bnel" : "bne", "s,t,p", sreg, 0);
252b5132
RH
4071 return;
4072 }
4073 if (sreg == 0)
4074 goto do_false;
2396cfb9
TS
4075 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4076 "d,v,t", AT, treg, sreg);
252b5132 4077 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4078 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4079 break;
4080
4081 case M_BLEL:
4082 likely = 1;
4083 case M_BLE:
4084 if (treg == 0)
4085 {
4086 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4087 likely ? "blezl" : "blez", "s,p", sreg);
252b5132
RH
4088 return;
4089 }
4090 if (sreg == 0)
4091 {
4092 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4093 likely ? "bgezl" : "bgez", "s,p", treg);
252b5132
RH
4094 return;
4095 }
2396cfb9
TS
4096 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4097 AT, treg, sreg);
252b5132 4098 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4099 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4100 break;
4101
4102 case M_BLEL_I:
4103 likely = 1;
4104 case M_BLE_I:
4105 maxnum = 0x7fffffff;
ca4e0257 4106 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
4107 {
4108 maxnum <<= 16;
4109 maxnum |= 0xffff;
4110 maxnum <<= 16;
4111 maxnum |= 0xffff;
4112 }
4113 if (imm_expr.X_op == O_constant
4114 && imm_expr.X_add_number >= maxnum
ca4e0257 4115 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
4116 goto do_true;
4117 if (imm_expr.X_op != O_constant)
4118 as_bad (_("Unsupported large constant"));
f9419b05 4119 ++imm_expr.X_add_number;
252b5132
RH
4120 /* FALLTHROUGH */
4121 case M_BLT_I:
4122 case M_BLTL_I:
4123 if (mask == M_BLTL_I)
4124 likely = 1;
4125 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4126 {
4127 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4128 likely ? "bltzl" : "bltz", "s,p", sreg);
252b5132
RH
4129 return;
4130 }
4131 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4132 {
4133 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4134 likely ? "blezl" : "blez", "s,p", sreg);
252b5132
RH
4135 return;
4136 }
4137 set_at (&icnt, sreg, 0);
4138 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4139 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4140 break;
4141
4142 case M_BLEUL:
4143 likely = 1;
4144 case M_BLEU:
4145 if (treg == 0)
4146 {
4147 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4148 likely ? "beql" : "beq", "s,t,p", sreg, 0);
252b5132
RH
4149 return;
4150 }
4151 if (sreg == 0)
4152 goto do_true;
2396cfb9
TS
4153 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4154 "d,v,t", AT, treg, sreg);
252b5132 4155 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4156 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4157 break;
4158
4159 case M_BLEUL_I:
4160 likely = 1;
4161 case M_BLEU_I:
4162 if (sreg == 0
ca4e0257 4163 || (HAVE_32BIT_GPRS
252b5132 4164 && imm_expr.X_op == O_constant
956cd1d6 4165 && imm_expr.X_add_number == (offsetT) 0xffffffff))
252b5132
RH
4166 goto do_true;
4167 if (imm_expr.X_op != O_constant)
4168 as_bad (_("Unsupported large constant"));
f9419b05 4169 ++imm_expr.X_add_number;
252b5132
RH
4170 /* FALLTHROUGH */
4171 case M_BLTU_I:
4172 case M_BLTUL_I:
4173 if (mask == M_BLTUL_I)
4174 likely = 1;
4175 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4176 goto do_false;
4177 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4178 {
4179 macro_build ((char *) NULL, &icnt, &offset_expr,
4180 likely ? "beql" : "beq",
4181 "s,t,p", sreg, 0);
4182 return;
4183 }
4184 set_at (&icnt, sreg, 1);
4185 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4186 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4187 break;
4188
4189 case M_BLTL:
4190 likely = 1;
4191 case M_BLT:
4192 if (treg == 0)
4193 {
4194 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4195 likely ? "bltzl" : "bltz", "s,p", sreg);
252b5132
RH
4196 return;
4197 }
4198 if (sreg == 0)
4199 {
4200 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4201 likely ? "bgtzl" : "bgtz", "s,p", treg);
252b5132
RH
4202 return;
4203 }
2396cfb9
TS
4204 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4205 AT, sreg, treg);
252b5132 4206 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4207 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4208 break;
4209
4210 case M_BLTUL:
4211 likely = 1;
4212 case M_BLTU:
4213 if (treg == 0)
4214 goto do_false;
4215 if (sreg == 0)
4216 {
4217 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4218 likely ? "bnel" : "bne", "s,t,p", 0, treg);
252b5132
RH
4219 return;
4220 }
2396cfb9
TS
4221 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4222 "d,v,t", AT, sreg,
252b5132
RH
4223 treg);
4224 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4225 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4226 break;
4227
4228 case M_DDIV_3:
4229 dbl = 1;
4230 case M_DIV_3:
4231 s = "mflo";
4232 goto do_div3;
4233 case M_DREM_3:
4234 dbl = 1;
4235 case M_REM_3:
4236 s = "mfhi";
4237 do_div3:
4238 if (treg == 0)
4239 {
4240 as_warn (_("Divide by zero."));
4241 if (mips_trap)
2396cfb9 4242 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
9bd7d936 4243 "s,t,q", 0, 0, 7);
252b5132 4244 else
2396cfb9
TS
4245 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4246 "c", 7);
252b5132
RH
4247 return;
4248 }
4249
4250 mips_emit_delays (true);
4251 ++mips_opts.noreorder;
4252 mips_any_noreorder = 1;
4253 if (mips_trap)
4254 {
2396cfb9 4255 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
9bd7d936 4256 "s,t,q", treg, 0, 7);
2396cfb9 4257 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 4258 dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
252b5132
RH
4259 }
4260 else
4261 {
4262 expr1.X_add_number = 8;
4263 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
2396cfb9 4264 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 4265 dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
2396cfb9
TS
4266 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4267 "c", 7);
252b5132
RH
4268 }
4269 expr1.X_add_number = -1;
4270 macro_build ((char *) NULL, &icnt, &expr1,
4271 dbl ? "daddiu" : "addiu",
4272 "t,r,j", AT, 0, (int) BFD_RELOC_LO16);
4273 expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
4274 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, AT);
4275 if (dbl)
4276 {
4277 expr1.X_add_number = 1;
4278 macro_build ((char *) NULL, &icnt, &expr1, "daddiu", "t,r,j", AT, 0,
4279 (int) BFD_RELOC_LO16);
2396cfb9
TS
4280 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsll32",
4281 "d,w,<", AT, AT, 31);
252b5132
RH
4282 }
4283 else
4284 {
4285 expr1.X_add_number = 0x80000000;
4286 macro_build ((char *) NULL, &icnt, &expr1, "lui", "t,u", AT,
4287 (int) BFD_RELOC_HI16);
4288 }
4289 if (mips_trap)
4290 {
2396cfb9 4291 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
9bd7d936 4292 "s,t,q", sreg, AT, 6);
252b5132
RH
4293 /* We want to close the noreorder block as soon as possible, so
4294 that later insns are available for delay slot filling. */
4295 --mips_opts.noreorder;
4296 }
4297 else
4298 {
4299 expr1.X_add_number = 8;
4300 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", sreg, AT);
2396cfb9
TS
4301 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
4302 0);
252b5132
RH
4303
4304 /* We want to close the noreorder block as soon as possible, so
4305 that later insns are available for delay slot filling. */
4306 --mips_opts.noreorder;
4307
2396cfb9
TS
4308 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4309 "c", 6);
252b5132 4310 }
2396cfb9 4311 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d", dreg);
252b5132
RH
4312 break;
4313
4314 case M_DIV_3I:
4315 s = "div";
4316 s2 = "mflo";
4317 goto do_divi;
4318 case M_DIVU_3I:
4319 s = "divu";
4320 s2 = "mflo";
4321 goto do_divi;
4322 case M_REM_3I:
4323 s = "div";
4324 s2 = "mfhi";
4325 goto do_divi;
4326 case M_REMU_3I:
4327 s = "divu";
4328 s2 = "mfhi";
4329 goto do_divi;
4330 case M_DDIV_3I:
4331 dbl = 1;
4332 s = "ddiv";
4333 s2 = "mflo";
4334 goto do_divi;
4335 case M_DDIVU_3I:
4336 dbl = 1;
4337 s = "ddivu";
4338 s2 = "mflo";
4339 goto do_divi;
4340 case M_DREM_3I:
4341 dbl = 1;
4342 s = "ddiv";
4343 s2 = "mfhi";
4344 goto do_divi;
4345 case M_DREMU_3I:
4346 dbl = 1;
4347 s = "ddivu";
4348 s2 = "mfhi";
4349 do_divi:
4350 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4351 {
4352 as_warn (_("Divide by zero."));
4353 if (mips_trap)
2396cfb9 4354 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
9bd7d936 4355 "s,t,q", 0, 0, 7);
252b5132 4356 else
2396cfb9
TS
4357 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4358 "c", 7);
252b5132
RH
4359 return;
4360 }
4361 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4362 {
4363 if (strcmp (s2, "mflo") == 0)
ea1fb5dc 4364 move_register (&icnt, dreg, sreg);
252b5132 4365 else
ea1fb5dc 4366 move_register (&icnt, dreg, 0);
252b5132
RH
4367 return;
4368 }
4369 if (imm_expr.X_op == O_constant
4370 && imm_expr.X_add_number == -1
4371 && s[strlen (s) - 1] != 'u')
4372 {
4373 if (strcmp (s2, "mflo") == 0)
4374 {
2396cfb9
TS
4375 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4376 dbl ? "dneg" : "neg", "d,w", dreg, sreg);
252b5132
RH
4377 }
4378 else
ea1fb5dc 4379 move_register (&icnt, dreg, 0);
252b5132
RH
4380 return;
4381 }
4382
4383 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9
TS
4384 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4385 sreg, AT);
4386 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
252b5132
RH
4387 break;
4388
4389 case M_DIVU_3:
4390 s = "divu";
4391 s2 = "mflo";
4392 goto do_divu3;
4393 case M_REMU_3:
4394 s = "divu";
4395 s2 = "mfhi";
4396 goto do_divu3;
4397 case M_DDIVU_3:
4398 s = "ddivu";
4399 s2 = "mflo";
4400 goto do_divu3;
4401 case M_DREMU_3:
4402 s = "ddivu";
4403 s2 = "mfhi";
4404 do_divu3:
4405 mips_emit_delays (true);
4406 ++mips_opts.noreorder;
4407 mips_any_noreorder = 1;
4408 if (mips_trap)
4409 {
2396cfb9 4410 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
9bd7d936 4411 "s,t,q", treg, 0, 7);
2396cfb9
TS
4412 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4413 sreg, treg);
252b5132
RH
4414 /* We want to close the noreorder block as soon as possible, so
4415 that later insns are available for delay slot filling. */
4416 --mips_opts.noreorder;
4417 }
4418 else
4419 {
4420 expr1.X_add_number = 8;
4421 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
2396cfb9
TS
4422 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4423 sreg, treg);
252b5132
RH
4424
4425 /* We want to close the noreorder block as soon as possible, so
4426 that later insns are available for delay slot filling. */
4427 --mips_opts.noreorder;
2396cfb9
TS
4428 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4429 "c", 7);
252b5132 4430 }
2396cfb9 4431 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
252b5132
RH
4432 return;
4433
4434 case M_DLA_AB:
4435 dbl = 1;
4436 case M_LA_AB:
4437 /* Load the address of a symbol into a register. If breg is not
4438 zero, we then add a base register to it. */
4439
3bec30a8
TS
4440 if (dbl && HAVE_32BIT_GPRS)
4441 as_warn (_("dla used to load 32-bit register"));
4442
c90bbe5b 4443 if (! dbl && HAVE_64BIT_OBJECTS)
3bec30a8
TS
4444 as_warn (_("la used to load 64-bit address"));
4445
0c11417f
MR
4446 if (offset_expr.X_op == O_constant
4447 && offset_expr.X_add_number >= -0x8000
4448 && offset_expr.X_add_number < 0x8000)
4449 {
4450 macro_build ((char *) NULL, &icnt, &offset_expr,
4451 (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
4452 "t,r,j", treg, sreg, (int) BFD_RELOC_LO16);
4453 return;
4454 }
4455
afdbd6d0
CD
4456 if (treg == breg)
4457 {
4458 tempreg = AT;
4459 used_at = 1;
4460 }
4461 else
4462 {
4463 tempreg = treg;
4464 used_at = 0;
4465 }
4466
252b5132
RH
4467 /* When generating embedded PIC code, we permit expressions of
4468 the form
afdbd6d0
CD
4469 la $treg,foo-bar
4470 la $treg,foo-bar($breg)
bb2d6cd7 4471 where bar is an address in the current section. These are used
252b5132
RH
4472 when getting the addresses of functions. We don't permit
4473 X_add_number to be non-zero, because if the symbol is
4474 external the relaxing code needs to know that any addend is
4475 purely the offset to X_op_symbol. */
4476 if (mips_pic == EMBEDDED_PIC
4477 && offset_expr.X_op == O_subtract
49309057 4478 && (symbol_constant_p (offset_expr.X_op_symbol)
bb2d6cd7 4479 ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
49309057
ILT
4480 : (symbol_equated_p (offset_expr.X_op_symbol)
4481 && (S_GET_SEGMENT
4482 (symbol_get_value_expression (offset_expr.X_op_symbol)
4483 ->X_add_symbol)
bb2d6cd7 4484 == now_seg)))
bb2d6cd7
GK
4485 && (offset_expr.X_add_number == 0
4486 || OUTPUT_FLAVOR == bfd_target_elf_flavour))
252b5132 4487 {
afdbd6d0
CD
4488 if (breg == 0)
4489 {
4490 tempreg = treg;
4491 used_at = 0;
4492 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4493 tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4494 }
4495 else
4496 {
4497 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4498 tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4499 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4d34fb5f 4500 (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu",
afdbd6d0
CD
4501 "d,v,t", tempreg, tempreg, breg);
4502 }
252b5132 4503 macro_build ((char *) NULL, &icnt, &offset_expr,
4d34fb5f 4504 (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
afdbd6d0
CD
4505 "t,r,j", treg, tempreg, (int) BFD_RELOC_PCREL_LO16);
4506 if (! used_at)
4507 return;
4508 break;
252b5132
RH
4509 }
4510
4511 if (offset_expr.X_op != O_symbol
4512 && offset_expr.X_op != O_constant)
4513 {
4514 as_bad (_("expression too complex"));
4515 offset_expr.X_op = O_constant;
4516 }
4517
252b5132 4518 if (offset_expr.X_op == O_constant)
4d34fb5f
TS
4519 load_register (&icnt, tempreg, &offset_expr,
4520 ((mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
4521 ? (dbl || HAVE_64BIT_ADDRESSES)
4522 : HAVE_64BIT_ADDRESSES));
252b5132
RH
4523 else if (mips_pic == NO_PIC)
4524 {
d6bc6245 4525 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 4526 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
4527 Otherwise we want
4528 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
4529 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4530 If we have a constant, we need two instructions anyhow,
d6bc6245 4531 so we may as well always use the latter form.
76b3015f 4532
d6bc6245
TS
4533 With 64bit address space and a usable $at we want
4534 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4535 lui $at,<sym> (BFD_RELOC_HI16_S)
4536 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4537 daddiu $at,<sym> (BFD_RELOC_LO16)
4538 dsll32 $tempreg,0
3a482fd5 4539 daddu $tempreg,$tempreg,$at
76b3015f 4540
d6bc6245
TS
4541 If $at is already in use, we use an path which is suboptimal
4542 on superscalar processors.
4543 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4544 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4545 dsll $tempreg,16
4546 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
4547 dsll $tempreg,16
4548 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
4549 */
f9419b05 4550 char *p = NULL;
d6bc6245 4551 if (HAVE_64BIT_ADDRESSES)
252b5132 4552 {
d6bc6245
TS
4553 /* We don't do GP optimization for now because RELAX_ENCODE can't
4554 hold the data for such large chunks. */
4555
460597ba 4556 if (used_at == 0 && ! mips_opts.noat)
98d3f06f
KH
4557 {
4558 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4559 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4560 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4561 AT, (int) BFD_RELOC_HI16_S);
4562 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4563 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4564 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4565 AT, AT, (int) BFD_RELOC_LO16);
4566 macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
4567 "d,w,<", tempreg, tempreg, 0);
3a482fd5
MR
4568 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
4569 "d,v,t", tempreg, tempreg, AT);
98d3f06f
KH
4570 used_at = 1;
4571 }
4572 else
4573 {
4574 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4575 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4576 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4577 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4578 macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4579 tempreg, tempreg, 16);
4580 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4581 tempreg, tempreg, (int) BFD_RELOC_HI16_S);
4582 macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4583 tempreg, tempreg, 16);
4584 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4585 tempreg, tempreg, (int) BFD_RELOC_LO16);
4586 }
4587 }
4588 else
4589 {
4590 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4591 && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
4592 {
4593 frag_grow (20);
4594 macro_build ((char *) NULL, &icnt, &offset_expr, "addiu",
c9914766
TS
4595 "t,r,j", tempreg, mips_gp_register,
4596 (int) BFD_RELOC_GPREL16);
98d3f06f
KH
4597 p = frag_var (rs_machine_dependent, 8, 0,
4598 RELAX_ENCODE (4, 8, 0, 4, 0,
4599 mips_opts.warn_about_macros),
4600 offset_expr.X_add_symbol, 0, NULL);
4601 }
4602 macro_build_lui (p, &icnt, &offset_expr, tempreg);
4603 if (p != NULL)
4604 p += 4;
4605 macro_build (p, &icnt, &offset_expr, "addiu",
4606 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4607 }
252b5132
RH
4608 }
4609 else if (mips_pic == SVR4_PIC && ! mips_big_got)
4610 {
9117d219
NC
4611 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
4612
252b5132
RH
4613 /* If this is a reference to an external symbol, and there
4614 is no constant, we want
4615 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9117d219
NC
4616 or if tempreg is PIC_CALL_REG
4617 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
252b5132
RH
4618 For a local symbol, we want
4619 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4620 nop
4621 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4622
4623 If we have a small constant, and this is a reference to
4624 an external symbol, we want
4625 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4626 nop
4627 addiu $tempreg,$tempreg,<constant>
4628 For a local symbol, we want the same instruction
4629 sequence, but we output a BFD_RELOC_LO16 reloc on the
4630 addiu instruction.
4631
4632 If we have a large constant, and this is a reference to
4633 an external symbol, we want
4634 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4635 lui $at,<hiconstant>
4636 addiu $at,$at,<loconstant>
4637 addu $tempreg,$tempreg,$at
4638 For a local symbol, we want the same instruction
4639 sequence, but we output a BFD_RELOC_LO16 reloc on the
4640 addiu instruction. */
4641 expr1.X_add_number = offset_expr.X_add_number;
4642 offset_expr.X_add_number = 0;
4643 frag_grow (32);
9117d219
NC
4644 if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4645 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
4d34fb5f
TS
4646 macro_build ((char *) NULL, &icnt, &offset_expr,
4647 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
c9914766 4648 "t,o(b)", tempreg, lw_reloc_type, mips_gp_register);
252b5132
RH
4649 if (expr1.X_add_number == 0)
4650 {
4651 int off;
f9419b05 4652 char *p;
252b5132
RH
4653
4654 if (breg == 0)
4655 off = 0;
4656 else
4657 {
4658 /* We're going to put in an addu instruction using
4659 tempreg, so we may as well insert the nop right
4660 now. */
4661 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4662 "nop", "");
4663 off = 4;
4664 }
4665 p = frag_var (rs_machine_dependent, 8 - off, 0,
4666 RELAX_ENCODE (0, 8 - off, -4 - off, 4 - off, 0,
4667 (breg == 0
4668 ? mips_opts.warn_about_macros
4669 : 0)),
c4e7957c 4670 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
4671 if (breg == 0)
4672 {
4673 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4674 p += 4;
4675 }
4676 macro_build (p, &icnt, &expr1,
ca4e0257 4677 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
4678 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4679 /* FIXME: If breg == 0, and the next instruction uses
4680 $tempreg, then if this variant case is used an extra
4681 nop will be generated. */
4682 }
4683 else if (expr1.X_add_number >= -0x8000
4684 && expr1.X_add_number < 0x8000)
4685 {
4686 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4687 "nop", "");
4688 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 4689 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132 4690 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
c4e7957c
TS
4691 frag_var (rs_machine_dependent, 0, 0,
4692 RELAX_ENCODE (0, 0, -12, -4, 0, 0),
4693 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
4694 }
4695 else
4696 {
4697 int off1;
4698
4699 /* If we are going to add in a base register, and the
4700 target register and the base register are the same,
4701 then we are using AT as a temporary register. Since
4702 we want to load the constant into AT, we add our
4703 current AT (from the global offset table) and the
4704 register into the register now, and pretend we were
4705 not using a base register. */
4706 if (breg != treg)
4707 off1 = 0;
4708 else
4709 {
4710 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4711 "nop", "");
4712 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 4713 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
4714 "d,v,t", treg, AT, breg);
4715 breg = 0;
4716 tempreg = treg;
4717 off1 = -8;
4718 }
4719
4720 /* Set mips_optimize around the lui instruction to avoid
4721 inserting an unnecessary nop after the lw. */
4722 hold_mips_optimize = mips_optimize;
4723 mips_optimize = 2;
c4e7957c 4724 macro_build_lui (NULL, &icnt, &expr1, AT);
252b5132
RH
4725 mips_optimize = hold_mips_optimize;
4726
4727 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 4728 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
4729 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4730 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 4731 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132 4732 "d,v,t", tempreg, tempreg, AT);
c4e7957c
TS
4733 frag_var (rs_machine_dependent, 0, 0,
4734 RELAX_ENCODE (0, 0, -16 + off1, -8, 0, 0),
4735 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
4736 used_at = 1;
4737 }
4738 }
4739 else if (mips_pic == SVR4_PIC)
4740 {
4741 int gpdel;
f9419b05 4742 char *p;
9117d219
NC
4743 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
4744 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
252b5132
RH
4745
4746 /* This is the large GOT case. If this is a reference to an
4747 external symbol, and there is no constant, we want
4748 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
4749 addu $tempreg,$tempreg,$gp
4750 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
9117d219
NC
4751 or if tempreg is PIC_CALL_REG
4752 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
4753 addu $tempreg,$tempreg,$gp
4754 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
252b5132
RH
4755 For a local symbol, we want
4756 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4757 nop
4758 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4759
4760 If we have a small constant, and this is a reference to
4761 an external symbol, we want
4762 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
4763 addu $tempreg,$tempreg,$gp
4764 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4765 nop
4766 addiu $tempreg,$tempreg,<constant>
4767 For a local symbol, we want
4768 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4769 nop
4770 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
4771
4772 If we have a large constant, and this is a reference to
4773 an external symbol, we want
4774 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
4775 addu $tempreg,$tempreg,$gp
4776 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4777 lui $at,<hiconstant>
4778 addiu $at,$at,<loconstant>
4779 addu $tempreg,$tempreg,$at
4780 For a local symbol, we want
4781 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4782 lui $at,<hiconstant>
4783 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
4784 addu $tempreg,$tempreg,$at
438c16b8
TS
4785
4786 For NewABI, we want for data addresses
4787 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
4788 If tempreg is PIC_CALL_REG pointing to a external symbol, we want
4789 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
4790 */
4791 if (HAVE_NEWABI)
4792 {
4793 int reloc_type = (tempreg == PIC_CALL_REG
4794 ? BFD_RELOC_MIPS_CALL16
4795 : BFD_RELOC_MIPS_GOT_DISP);
4796
4797 macro_build ((char *) NULL, &icnt, &offset_expr,
4798 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4799 "t,o(b)", tempreg, reloc_type, mips_gp_register);
4800
4801 if (breg != 0)
4802 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4803 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4804 "d,v,t", treg, tempreg, breg);
4805
4806 if (! used_at)
4807 return;
4808
4809 break;
4810 }
252b5132
RH
4811 expr1.X_add_number = offset_expr.X_add_number;
4812 offset_expr.X_add_number = 0;
4813 frag_grow (52);
f7ea7ef2 4814 if (reg_needs_delay (mips_gp_register))
252b5132
RH
4815 gpdel = 4;
4816 else
4817 gpdel = 0;
9117d219
NC
4818 if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4819 {
4820 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
4821 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
4822 }
252b5132 4823 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
9117d219 4824 tempreg, lui_reloc_type);
252b5132 4825 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 4826 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 4827 "d,v,t", tempreg, tempreg, mips_gp_register);
252b5132 4828 macro_build ((char *) NULL, &icnt, &offset_expr,
4d34fb5f 4829 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
9117d219 4830 "t,o(b)", tempreg, lw_reloc_type, tempreg);
252b5132
RH
4831 if (expr1.X_add_number == 0)
4832 {
4833 int off;
4834
4835 if (breg == 0)
4836 off = 0;
4837 else
4838 {
4839 /* We're going to put in an addu instruction using
4840 tempreg, so we may as well insert the nop right
4841 now. */
4842 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4843 "nop", "");
4844 off = 4;
4845 }
4846
4847 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4848 RELAX_ENCODE (12 + off, 12 + gpdel, gpdel,
4849 8 + gpdel, 0,
4850 (breg == 0
4851 ? mips_opts.warn_about_macros
4852 : 0)),
c4e7957c 4853 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
4854 }
4855 else if (expr1.X_add_number >= -0x8000
4856 && expr1.X_add_number < 0x8000)
4857 {
4858 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4859 "nop", "");
4860 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 4861 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
4862 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4863
4864 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4865 RELAX_ENCODE (20, 12 + gpdel, gpdel, 8 + gpdel, 0,
4866 (breg == 0
4867 ? mips_opts.warn_about_macros
4868 : 0)),
c4e7957c 4869 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
4870 }
4871 else
4872 {
4873 int adj, dreg;
4874
4875 /* If we are going to add in a base register, and the
4876 target register and the base register are the same,
4877 then we are using AT as a temporary register. Since
4878 we want to load the constant into AT, we add our
4879 current AT (from the global offset table) and the
4880 register into the register now, and pretend we were
4881 not using a base register. */
4882 if (breg != treg)
4883 {
4884 adj = 0;
4885 dreg = tempreg;
4886 }
4887 else
4888 {
4889 assert (tempreg == AT);
4890 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4891 "nop", "");
4892 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 4893 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
4894 "d,v,t", treg, AT, breg);
4895 dreg = treg;
4896 adj = 8;
4897 }
4898
4899 /* Set mips_optimize around the lui instruction to avoid
4900 inserting an unnecessary nop after the lw. */
4901 hold_mips_optimize = mips_optimize;
4902 mips_optimize = 2;
c4e7957c 4903 macro_build_lui (NULL, &icnt, &expr1, AT);
252b5132
RH
4904 mips_optimize = hold_mips_optimize;
4905
4906 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 4907 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
4908 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4909 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 4910 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
4911 "d,v,t", dreg, dreg, AT);
4912
4913 p = frag_var (rs_machine_dependent, 16 + gpdel + adj, 0,
4914 RELAX_ENCODE (24 + adj, 16 + gpdel + adj, gpdel,
4915 8 + gpdel, 0,
4916 (breg == 0
4917 ? mips_opts.warn_about_macros
4918 : 0)),
c4e7957c 4919 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
4920
4921 used_at = 1;
4922 }
4923
4924 if (gpdel > 0)
4925 {
4926 /* This is needed because this instruction uses $gp, but
4927 the first instruction on the main stream does not. */
4928 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4929 p += 4;
4930 }
4931 macro_build (p, &icnt, &offset_expr,
4d34fb5f 4932 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
c9914766
TS
4933 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16,
4934 mips_gp_register);
252b5132
RH
4935 p += 4;
4936 if (expr1.X_add_number >= -0x8000
4937 && expr1.X_add_number < 0x8000)
4938 {
4939 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4940 p += 4;
4941 macro_build (p, &icnt, &expr1,
ca4e0257 4942 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
4943 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4944 /* FIXME: If add_number is 0, and there was no base
4945 register, the external symbol case ended with a load,
4946 so if the symbol turns out to not be external, and
4947 the next instruction uses tempreg, an unnecessary nop
4948 will be inserted. */
4949 }
4950 else
4951 {
4952 if (breg == treg)
4953 {
4954 /* We must add in the base register now, as in the
4955 external symbol case. */
4956 assert (tempreg == AT);
4957 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4958 p += 4;
4959 macro_build (p, &icnt, (expressionS *) NULL,
ca4e0257 4960 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
4961 "d,v,t", treg, AT, breg);
4962 p += 4;
4963 tempreg = treg;
4964 /* We set breg to 0 because we have arranged to add
4965 it in in both cases. */
4966 breg = 0;
4967 }
4968
4969 macro_build_lui (p, &icnt, &expr1, AT);
4970 p += 4;
4971 macro_build (p, &icnt, &expr1,
ca4e0257 4972 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
4973 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4974 p += 4;
4975 macro_build (p, &icnt, (expressionS *) NULL,
ca4e0257 4976 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
4977 "d,v,t", tempreg, tempreg, AT);
4978 p += 4;
4979 }
4980 }
4981 else if (mips_pic == EMBEDDED_PIC)
4982 {
4983 /* We use
cdf6fd85 4984 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
4985 */
4986 macro_build ((char *) NULL, &icnt, &offset_expr,
c9914766
TS
4987 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
4988 tempreg, mips_gp_register, (int) BFD_RELOC_GPREL16);
252b5132
RH
4989 }
4990 else
4991 abort ();
4992
4993 if (breg != 0)
4d34fb5f
TS
4994 {
4995 char *s;
4996
4997 if (mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
4998 s = (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu";
4999 else
5000 s = HAVE_64BIT_ADDRESSES ? "daddu" : "addu";
5001
5002 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s,
5003 "d,v,t", treg, tempreg, breg);
5004 }
252b5132
RH
5005
5006 if (! used_at)
5007 return;
5008
5009 break;
5010
5011 case M_J_A:
5012 /* The j instruction may not be used in PIC code, since it
5013 requires an absolute address. We convert it to a b
5014 instruction. */
5015 if (mips_pic == NO_PIC)
5016 macro_build ((char *) NULL, &icnt, &offset_expr, "j", "a");
5017 else
5018 macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
5019 return;
5020
5021 /* The jal instructions must be handled as macros because when
5022 generating PIC code they expand to multi-instruction
5023 sequences. Normally they are simple instructions. */
5024 case M_JAL_1:
5025 dreg = RA;
5026 /* Fall through. */
5027 case M_JAL_2:
5028 if (mips_pic == NO_PIC
5029 || mips_pic == EMBEDDED_PIC)
5030 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5031 "d,s", dreg, sreg);
5032 else if (mips_pic == SVR4_PIC)
5033 {
5034 if (sreg != PIC_CALL_REG)
5035 as_warn (_("MIPS PIC call to register other than $25"));
bdaaa2e1 5036
252b5132
RH
5037 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5038 "d,s", dreg, sreg);
6478892d 5039 if (! HAVE_NEWABI)
252b5132 5040 {
6478892d
TS
5041 if (mips_cprestore_offset < 0)
5042 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5043 else
5044 {
7a621144
DJ
5045 if (! mips_frame_reg_valid)
5046 {
5047 as_warn (_("No .frame pseudo-op used in PIC code"));
5048 /* Quiet this warning. */
5049 mips_frame_reg_valid = 1;
5050 }
5051 if (! mips_cprestore_valid)
5052 {
5053 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5054 /* Quiet this warning. */
5055 mips_cprestore_valid = 1;
5056 }
6478892d
TS
5057 expr1.X_add_number = mips_cprestore_offset;
5058 macro_build ((char *) NULL, &icnt, &expr1,
5059 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
c9914766
TS
5060 mips_gp_register, (int) BFD_RELOC_LO16,
5061 mips_frame_reg);
6478892d 5062 }
252b5132
RH
5063 }
5064 }
5065 else
5066 abort ();
5067
5068 return;
5069
5070 case M_JAL_A:
5071 if (mips_pic == NO_PIC)
5072 macro_build ((char *) NULL, &icnt, &offset_expr, "jal", "a");
5073 else if (mips_pic == SVR4_PIC)
5074 {
f9419b05
TS
5075 char *p;
5076
252b5132
RH
5077 /* If this is a reference to an external symbol, and we are
5078 using a small GOT, we want
5079 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5080 nop
f9419b05 5081 jalr $ra,$25
252b5132
RH
5082 nop
5083 lw $gp,cprestore($sp)
5084 The cprestore value is set using the .cprestore
5085 pseudo-op. If we are using a big GOT, we want
5086 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5087 addu $25,$25,$gp
5088 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
5089 nop
f9419b05 5090 jalr $ra,$25
252b5132
RH
5091 nop
5092 lw $gp,cprestore($sp)
5093 If the symbol is not external, we want
5094 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5095 nop
5096 addiu $25,$25,<sym> (BFD_RELOC_LO16)
f9419b05 5097 jalr $ra,$25
252b5132 5098 nop
438c16b8
TS
5099 lw $gp,cprestore($sp)
5100 For NewABI, we want
5101 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5102 jalr $ra,$25 (BFD_RELOC_MIPS_JALR)
5103 */
5104 if (HAVE_NEWABI)
252b5132
RH
5105 {
5106 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 5107 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132 5108 "t,o(b)", PIC_CALL_REG,
438c16b8
TS
5109 (int) BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5110 macro_build_jalr (icnt, &offset_expr);
252b5132
RH
5111 }
5112 else
5113 {
438c16b8
TS
5114 frag_grow (40);
5115 if (! mips_big_got)
5116 {
5117 macro_build ((char *) NULL, &icnt, &offset_expr,
5118 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5119 "t,o(b)", PIC_CALL_REG,
5120 (int) BFD_RELOC_MIPS_CALL16, mips_gp_register);
5121 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5122 "nop", "");
5123 p = frag_var (rs_machine_dependent, 4, 0,
5124 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5125 offset_expr.X_add_symbol, 0, NULL);
5126 }
252b5132 5127 else
252b5132 5128 {
438c16b8
TS
5129 int gpdel;
5130
5131 if (reg_needs_delay (mips_gp_register))
5132 gpdel = 4;
5133 else
5134 gpdel = 0;
5135 macro_build ((char *) NULL, &icnt, &offset_expr, "lui",
5136 "t,u", PIC_CALL_REG,
5137 (int) BFD_RELOC_MIPS_CALL_HI16);
5138 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5139 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5140 "d,v,t", PIC_CALL_REG, PIC_CALL_REG,
5141 mips_gp_register);
5142 macro_build ((char *) NULL, &icnt, &offset_expr,
5143 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5144 "t,o(b)", PIC_CALL_REG,
5145 (int) BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
5146 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5147 "nop", "");
5148 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5149 RELAX_ENCODE (16, 12 + gpdel, gpdel,
5150 8 + gpdel, 0, 0),
5151 offset_expr.X_add_symbol, 0, NULL);
5152 if (gpdel > 0)
5153 {
5154 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5155 p += 4;
5156 }
5157 macro_build (p, &icnt, &offset_expr,
5158 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5159 "t,o(b)", PIC_CALL_REG,
5160 (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
5161 p += 4;
252b5132
RH
5162 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5163 p += 4;
5164 }
5165 macro_build (p, &icnt, &offset_expr,
438c16b8
TS
5166 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5167 "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
5168 (int) BFD_RELOC_LO16);
5169 macro_build_jalr (icnt, &offset_expr);
5170
6478892d
TS
5171 if (mips_cprestore_offset < 0)
5172 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5173 else
5174 {
7a621144
DJ
5175 if (! mips_frame_reg_valid)
5176 {
5177 as_warn (_("No .frame pseudo-op used in PIC code"));
5178 /* Quiet this warning. */
5179 mips_frame_reg_valid = 1;
5180 }
5181 if (! mips_cprestore_valid)
5182 {
5183 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5184 /* Quiet this warning. */
5185 mips_cprestore_valid = 1;
5186 }
6478892d
TS
5187 if (mips_opts.noreorder)
5188 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
98d3f06f 5189 "nop", "");
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 if (mips_pic == EMBEDDED_PIC)
5199 {
5200 macro_build ((char *) NULL, &icnt, &offset_expr, "bal", "p");
5201 /* The linker may expand the call to a longer sequence which
5202 uses $at, so we must break rather than return. */
5203 break;
5204 }
5205 else
5206 abort ();
5207
5208 return;
5209
5210 case M_LB_AB:
5211 s = "lb";
5212 goto ld;
5213 case M_LBU_AB:
5214 s = "lbu";
5215 goto ld;
5216 case M_LH_AB:
5217 s = "lh";
5218 goto ld;
5219 case M_LHU_AB:
5220 s = "lhu";
5221 goto ld;
5222 case M_LW_AB:
5223 s = "lw";
5224 goto ld;
5225 case M_LWC0_AB:
5226 s = "lwc0";
bdaaa2e1 5227 /* Itbl support may require additional care here. */
252b5132
RH
5228 coproc = 1;
5229 goto ld;
5230 case M_LWC1_AB:
5231 s = "lwc1";
bdaaa2e1 5232 /* Itbl support may require additional care here. */
252b5132
RH
5233 coproc = 1;
5234 goto ld;
5235 case M_LWC2_AB:
5236 s = "lwc2";
bdaaa2e1 5237 /* Itbl support may require additional care here. */
252b5132
RH
5238 coproc = 1;
5239 goto ld;
5240 case M_LWC3_AB:
5241 s = "lwc3";
bdaaa2e1 5242 /* Itbl support may require additional care here. */
252b5132
RH
5243 coproc = 1;
5244 goto ld;
5245 case M_LWL_AB:
5246 s = "lwl";
5247 lr = 1;
5248 goto ld;
5249 case M_LWR_AB:
5250 s = "lwr";
5251 lr = 1;
5252 goto ld;
5253 case M_LDC1_AB:
ec68c924 5254 if (mips_arch == CPU_R4650)
252b5132
RH
5255 {
5256 as_bad (_("opcode not supported on this processor"));
5257 return;
5258 }
5259 s = "ldc1";
bdaaa2e1 5260 /* Itbl support may require additional care here. */
252b5132
RH
5261 coproc = 1;
5262 goto ld;
5263 case M_LDC2_AB:
5264 s = "ldc2";
bdaaa2e1 5265 /* Itbl support may require additional care here. */
252b5132
RH
5266 coproc = 1;
5267 goto ld;
5268 case M_LDC3_AB:
5269 s = "ldc3";
bdaaa2e1 5270 /* Itbl support may require additional care here. */
252b5132
RH
5271 coproc = 1;
5272 goto ld;
5273 case M_LDL_AB:
5274 s = "ldl";
5275 lr = 1;
5276 goto ld;
5277 case M_LDR_AB:
5278 s = "ldr";
5279 lr = 1;
5280 goto ld;
5281 case M_LL_AB:
5282 s = "ll";
5283 goto ld;
5284 case M_LLD_AB:
5285 s = "lld";
5286 goto ld;
5287 case M_LWU_AB:
5288 s = "lwu";
5289 ld:
5290 if (breg == treg || coproc || lr)
5291 {
5292 tempreg = AT;
5293 used_at = 1;
5294 }
5295 else
5296 {
5297 tempreg = treg;
5298 used_at = 0;
5299 }
5300 goto ld_st;
5301 case M_SB_AB:
5302 s = "sb";
5303 goto st;
5304 case M_SH_AB:
5305 s = "sh";
5306 goto st;
5307 case M_SW_AB:
5308 s = "sw";
5309 goto st;
5310 case M_SWC0_AB:
5311 s = "swc0";
bdaaa2e1 5312 /* Itbl support may require additional care here. */
252b5132
RH
5313 coproc = 1;
5314 goto st;
5315 case M_SWC1_AB:
5316 s = "swc1";
bdaaa2e1 5317 /* Itbl support may require additional care here. */
252b5132
RH
5318 coproc = 1;
5319 goto st;
5320 case M_SWC2_AB:
5321 s = "swc2";
bdaaa2e1 5322 /* Itbl support may require additional care here. */
252b5132
RH
5323 coproc = 1;
5324 goto st;
5325 case M_SWC3_AB:
5326 s = "swc3";
bdaaa2e1 5327 /* Itbl support may require additional care here. */
252b5132
RH
5328 coproc = 1;
5329 goto st;
5330 case M_SWL_AB:
5331 s = "swl";
5332 goto st;
5333 case M_SWR_AB:
5334 s = "swr";
5335 goto st;
5336 case M_SC_AB:
5337 s = "sc";
5338 goto st;
5339 case M_SCD_AB:
5340 s = "scd";
5341 goto st;
5342 case M_SDC1_AB:
ec68c924 5343 if (mips_arch == CPU_R4650)
252b5132
RH
5344 {
5345 as_bad (_("opcode not supported on this processor"));
5346 return;
5347 }
5348 s = "sdc1";
5349 coproc = 1;
bdaaa2e1 5350 /* Itbl support may require additional care here. */
252b5132
RH
5351 goto st;
5352 case M_SDC2_AB:
5353 s = "sdc2";
bdaaa2e1 5354 /* Itbl support may require additional care here. */
252b5132
RH
5355 coproc = 1;
5356 goto st;
5357 case M_SDC3_AB:
5358 s = "sdc3";
bdaaa2e1 5359 /* Itbl support may require additional care here. */
252b5132
RH
5360 coproc = 1;
5361 goto st;
5362 case M_SDL_AB:
5363 s = "sdl";
5364 goto st;
5365 case M_SDR_AB:
5366 s = "sdr";
5367 st:
5368 tempreg = AT;
5369 used_at = 1;
5370 ld_st:
bdaaa2e1 5371 /* Itbl support may require additional care here. */
252b5132
RH
5372 if (mask == M_LWC1_AB
5373 || mask == M_SWC1_AB
5374 || mask == M_LDC1_AB
5375 || mask == M_SDC1_AB
5376 || mask == M_L_DAB
5377 || mask == M_S_DAB)
5378 fmt = "T,o(b)";
5379 else if (coproc)
5380 fmt = "E,o(b)";
5381 else
5382 fmt = "t,o(b)";
5383
afdbd6d0
CD
5384 /* For embedded PIC, we allow loads where the offset is calculated
5385 by subtracting a symbol in the current segment from an unknown
5386 symbol, relative to a base register, e.g.:
5387 <op> $treg, <sym>-<localsym>($breg)
5388 This is used by the compiler for switch statements. */
76b3015f 5389 if (mips_pic == EMBEDDED_PIC
afdbd6d0
CD
5390 && offset_expr.X_op == O_subtract
5391 && (symbol_constant_p (offset_expr.X_op_symbol)
5392 ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
5393 : (symbol_equated_p (offset_expr.X_op_symbol)
5394 && (S_GET_SEGMENT
5395 (symbol_get_value_expression (offset_expr.X_op_symbol)
5396 ->X_add_symbol)
5397 == now_seg)))
5398 && breg != 0
5399 && (offset_expr.X_add_number == 0
5400 || OUTPUT_FLAVOR == bfd_target_elf_flavour))
5401 {
5402 /* For this case, we output the instructions:
5403 lui $tempreg,<sym> (BFD_RELOC_PCREL_HI16_S)
5404 addiu $tempreg,$tempreg,$breg
5405 <op> $treg,<sym>($tempreg) (BFD_RELOC_PCREL_LO16)
5406 If the relocation would fit entirely in 16 bits, it would be
5407 nice to emit:
5408 <op> $treg,<sym>($breg) (BFD_RELOC_PCREL_LO16)
5409 instead, but that seems quite difficult. */
5410 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5411 tempreg, (int) BFD_RELOC_PCREL_HI16_S);
5412 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5413 ((bfd_arch_bits_per_address (stdoutput) == 32
5414 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
5415 ? "addu" : "daddu"),
5416 "d,v,t", tempreg, tempreg, breg);
5417 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt, treg,
5418 (int) BFD_RELOC_PCREL_LO16, tempreg);
5419 if (! used_at)
5420 return;
5421 break;
5422 }
5423
252b5132
RH
5424 if (offset_expr.X_op != O_constant
5425 && offset_expr.X_op != O_symbol)
5426 {
5427 as_bad (_("expression too complex"));
5428 offset_expr.X_op = O_constant;
5429 }
5430
5431 /* A constant expression in PIC code can be handled just as it
5432 is in non PIC code. */
5433 if (mips_pic == NO_PIC
5434 || offset_expr.X_op == O_constant)
5435 {
f9419b05
TS
5436 char *p;
5437
252b5132
RH
5438 /* If this is a reference to a GP relative symbol, and there
5439 is no base register, we want
cdf6fd85 5440 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
5441 Otherwise, if there is no base register, we want
5442 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5443 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5444 If we have a constant, we need two instructions anyhow,
5445 so we always use the latter form.
5446
5447 If we have a base register, and this is a reference to a
5448 GP relative symbol, we want
5449 addu $tempreg,$breg,$gp
cdf6fd85 5450 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
5451 Otherwise we want
5452 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5453 addu $tempreg,$tempreg,$breg
5454 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245 5455 With a constant we always use the latter case.
76b3015f 5456
d6bc6245
TS
5457 With 64bit address space and no base register and $at usable,
5458 we want
5459 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5460 lui $at,<sym> (BFD_RELOC_HI16_S)
5461 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5462 dsll32 $tempreg,0
5463 daddu $tempreg,$at
5464 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5465 If we have a base register, we want
5466 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5467 lui $at,<sym> (BFD_RELOC_HI16_S)
5468 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5469 daddu $at,$breg
5470 dsll32 $tempreg,0
5471 daddu $tempreg,$at
5472 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5473
5474 Without $at we can't generate the optimal path for superscalar
5475 processors here since this would require two temporary registers.
5476 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5477 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5478 dsll $tempreg,16
5479 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5480 dsll $tempreg,16
5481 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5482 If we have a base register, we want
5483 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5484 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5485 dsll $tempreg,16
5486 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5487 dsll $tempreg,16
5488 daddu $tempreg,$tempreg,$breg
5489 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6373ee54
CD
5490
5491 If we have 64-bit addresses, as an optimization, for
5492 addresses which are 32-bit constants (e.g. kseg0/kseg1
5493 addresses) we fall back to the 32-bit address generation
78d32a17
MR
5494 mechanism since it is more efficient. Note that due to
5495 the signed offset used by memory operations, the 32-bit
5496 range is shifted down by 32768 here. This code should
6373ee54
CD
5497 probably attempt to generate 64-bit constants more
5498 efficiently in general.
d6bc6245 5499 */
6373ee54
CD
5500 if (HAVE_64BIT_ADDRESSES
5501 && !(offset_expr.X_op == O_constant
78d32a17 5502 && IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000)))
d6bc6245
TS
5503 {
5504 p = NULL;
5505
5506 /* We don't do GP optimization for now because RELAX_ENCODE can't
5507 hold the data for such large chunks. */
5508
460597ba 5509 if (used_at == 0 && ! mips_opts.noat)
d6bc6245
TS
5510 {
5511 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5512 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5513 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5514 AT, (int) BFD_RELOC_HI16_S);
5515 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5516 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5517 if (breg != 0)
2396cfb9
TS
5518 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5519 "d,v,t", AT, AT, breg);
5520 macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
5521 "d,w,<", tempreg, tempreg, 0);
5522 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5523 "d,v,t", tempreg, tempreg, AT);
d6bc6245
TS
5524 macro_build (p, &icnt, &offset_expr, s,
5525 fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5526 used_at = 1;
5527 }
5528 else
5529 {
5530 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5531 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5532 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5533 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
2396cfb9
TS
5534 macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5535 "d,w,<", tempreg, tempreg, 16);
d6bc6245
TS
5536 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5537 tempreg, tempreg, (int) BFD_RELOC_HI16_S);
2396cfb9
TS
5538 macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5539 "d,w,<", tempreg, tempreg, 16);
d6bc6245 5540 if (breg != 0)
2396cfb9
TS
5541 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5542 "d,v,t", tempreg, tempreg, breg);
d6bc6245
TS
5543 macro_build (p, &icnt, &offset_expr, s,
5544 fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5545 }
5546
5547 return;
5548 }
76b3015f 5549
252b5132
RH
5550 if (breg == 0)
5551 {
e7d556df 5552 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
252b5132
RH
5553 || nopic_need_relax (offset_expr.X_add_symbol, 1))
5554 p = NULL;
5555 else
5556 {
5557 frag_grow (20);
5558 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
c9914766
TS
5559 treg, (int) BFD_RELOC_GPREL16,
5560 mips_gp_register);
252b5132
RH
5561 p = frag_var (rs_machine_dependent, 8, 0,
5562 RELAX_ENCODE (4, 8, 0, 4, 0,
5563 (mips_opts.warn_about_macros
5564 || (used_at
5565 && mips_opts.noat))),
956cd1d6 5566 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5567 used_at = 0;
5568 }
5569 macro_build_lui (p, &icnt, &offset_expr, tempreg);
5570 if (p != NULL)
5571 p += 4;
5572 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5573 (int) BFD_RELOC_LO16, tempreg);
5574 }
5575 else
5576 {
e7d556df 5577 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
252b5132
RH
5578 || nopic_need_relax (offset_expr.X_add_symbol, 1))
5579 p = NULL;
5580 else
5581 {
5582 frag_grow (28);
5583 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5584 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 5585 "d,v,t", tempreg, breg, mips_gp_register);
252b5132 5586 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
cdf6fd85 5587 treg, (int) BFD_RELOC_GPREL16, tempreg);
252b5132
RH
5588 p = frag_var (rs_machine_dependent, 12, 0,
5589 RELAX_ENCODE (8, 12, 0, 8, 0, 0),
956cd1d6 5590 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5591 }
5592 macro_build_lui (p, &icnt, &offset_expr, tempreg);
5593 if (p != NULL)
5594 p += 4;
5595 macro_build (p, &icnt, (expressionS *) NULL,
ca4e0257 5596 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5597 "d,v,t", tempreg, tempreg, breg);
5598 if (p != NULL)
5599 p += 4;
5600 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5601 (int) BFD_RELOC_LO16, tempreg);
5602 }
5603 }
5604 else if (mips_pic == SVR4_PIC && ! mips_big_got)
5605 {
f9419b05
TS
5606 char *p;
5607
252b5132
RH
5608 /* If this is a reference to an external symbol, we want
5609 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5610 nop
5611 <op> $treg,0($tempreg)
5612 Otherwise we want
5613 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5614 nop
5615 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5616 <op> $treg,0($tempreg)
5617 If there is a base register, we add it to $tempreg before
5618 the <op>. If there is a constant, we stick it in the
5619 <op> instruction. We don't handle constants larger than
5620 16 bits, because we have no way to load the upper 16 bits
5621 (actually, we could handle them for the subset of cases
5622 in which we are not using $at). */
5623 assert (offset_expr.X_op == O_symbol);
5624 expr1.X_add_number = offset_expr.X_add_number;
5625 offset_expr.X_add_number = 0;
5626 if (expr1.X_add_number < -0x8000
5627 || expr1.X_add_number >= 0x8000)
5628 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5629 frag_grow (20);
5630 macro_build ((char *) NULL, &icnt, &offset_expr,
c9914766
TS
5631 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", tempreg,
5632 (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132 5633 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
bdaaa2e1 5634 p = frag_var (rs_machine_dependent, 4, 0,
252b5132 5635 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
c4e7957c 5636 offset_expr.X_add_symbol, 0, NULL);
252b5132 5637 macro_build (p, &icnt, &offset_expr,
ca4e0257 5638 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
5639 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5640 if (breg != 0)
5641 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5642 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5643 "d,v,t", tempreg, tempreg, breg);
5644 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5645 (int) BFD_RELOC_LO16, tempreg);
5646 }
5647 else if (mips_pic == SVR4_PIC)
5648 {
5649 int gpdel;
f9419b05 5650 char *p;
252b5132
RH
5651
5652 /* If this is a reference to an external symbol, we want
5653 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5654 addu $tempreg,$tempreg,$gp
5655 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5656 <op> $treg,0($tempreg)
5657 Otherwise we want
5658 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5659 nop
5660 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5661 <op> $treg,0($tempreg)
5662 If there is a base register, we add it to $tempreg before
5663 the <op>. If there is a constant, we stick it in the
5664 <op> instruction. We don't handle constants larger than
5665 16 bits, because we have no way to load the upper 16 bits
5666 (actually, we could handle them for the subset of cases
438c16b8
TS
5667 in which we are not using $at).
5668
5669 For NewABI, we want
5670 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
5671 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
5672 <op> $treg,0($tempreg)
5673 */
252b5132
RH
5674 assert (offset_expr.X_op == O_symbol);
5675 expr1.X_add_number = offset_expr.X_add_number;
5676 offset_expr.X_add_number = 0;
5677 if (expr1.X_add_number < -0x8000
5678 || expr1.X_add_number >= 0x8000)
5679 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
438c16b8
TS
5680 if (HAVE_NEWABI)
5681 {
5682 macro_build ((char *) NULL, &icnt, &offset_expr,
5683 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5684 "t,o(b)", tempreg, BFD_RELOC_MIPS_GOT_PAGE,
5685 mips_gp_register);
5686 macro_build ((char *) NULL, &icnt, &offset_expr,
5687 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5688 "t,r,j", tempreg, tempreg,
5689 BFD_RELOC_MIPS_GOT_OFST);
5690 if (breg != 0)
5691 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5692 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5693 "d,v,t", tempreg, tempreg, breg);
5694 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5695 (int) BFD_RELOC_LO16, tempreg);
5696
5697 if (! used_at)
5698 return;
5699
5700 break;
5701 }
f7ea7ef2 5702 if (reg_needs_delay (mips_gp_register))
252b5132
RH
5703 gpdel = 4;
5704 else
5705 gpdel = 0;
5706 frag_grow (36);
5707 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5708 tempreg, (int) BFD_RELOC_MIPS_GOT_HI16);
5709 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5710 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 5711 "d,v,t", tempreg, tempreg, mips_gp_register);
252b5132 5712 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 5713 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
5714 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_LO16,
5715 tempreg);
5716 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5717 RELAX_ENCODE (12, 12 + gpdel, gpdel, 8 + gpdel, 0, 0),
c4e7957c 5718 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5719 if (gpdel > 0)
5720 {
5721 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5722 p += 4;
5723 }
5724 macro_build (p, &icnt, &offset_expr,
ca4e0257 5725 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
c9914766
TS
5726 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16,
5727 mips_gp_register);
252b5132
RH
5728 p += 4;
5729 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5730 p += 4;
5731 macro_build (p, &icnt, &offset_expr,
ca4e0257 5732 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
5733 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5734 if (breg != 0)
5735 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5736 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5737 "d,v,t", tempreg, tempreg, breg);
5738 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5739 (int) BFD_RELOC_LO16, tempreg);
5740 }
5741 else if (mips_pic == EMBEDDED_PIC)
5742 {
5743 /* If there is no base register, we want
cdf6fd85 5744 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
5745 If there is a base register, we want
5746 addu $tempreg,$breg,$gp
cdf6fd85 5747 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
5748 */
5749 assert (offset_expr.X_op == O_symbol);
5750 if (breg == 0)
5751 {
5752 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
c9914766 5753 treg, (int) BFD_RELOC_GPREL16, mips_gp_register);
252b5132
RH
5754 used_at = 0;
5755 }
5756 else
5757 {
5758 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5759 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 5760 "d,v,t", tempreg, breg, mips_gp_register);
252b5132 5761 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
cdf6fd85 5762 treg, (int) BFD_RELOC_GPREL16, tempreg);
252b5132
RH
5763 }
5764 }
5765 else
5766 abort ();
5767
5768 if (! used_at)
5769 return;
5770
5771 break;
5772
5773 case M_LI:
5774 case M_LI_S:
5775 load_register (&icnt, treg, &imm_expr, 0);
5776 return;
5777
5778 case M_DLI:
5779 load_register (&icnt, treg, &imm_expr, 1);
5780 return;
5781
5782 case M_LI_SS:
5783 if (imm_expr.X_op == O_constant)
5784 {
5785 load_register (&icnt, AT, &imm_expr, 0);
5786 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5787 "mtc1", "t,G", AT, treg);
5788 break;
5789 }
5790 else
5791 {
5792 assert (offset_expr.X_op == O_symbol
5793 && strcmp (segment_name (S_GET_SEGMENT
5794 (offset_expr.X_add_symbol)),
5795 ".lit4") == 0
5796 && offset_expr.X_add_number == 0);
5797 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
c9914766 5798 treg, (int) BFD_RELOC_MIPS_LITERAL, mips_gp_register);
252b5132
RH
5799 return;
5800 }
5801
5802 case M_LI_D:
ca4e0257
RS
5803 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
5804 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
5805 order 32 bits of the value and the low order 32 bits are either
5806 zero or in OFFSET_EXPR. */
252b5132
RH
5807 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
5808 {
ca4e0257 5809 if (HAVE_64BIT_GPRS)
252b5132
RH
5810 load_register (&icnt, treg, &imm_expr, 1);
5811 else
5812 {
5813 int hreg, lreg;
5814
5815 if (target_big_endian)
5816 {
5817 hreg = treg;
5818 lreg = treg + 1;
5819 }
5820 else
5821 {
5822 hreg = treg + 1;
5823 lreg = treg;
5824 }
5825
5826 if (hreg <= 31)
5827 load_register (&icnt, hreg, &imm_expr, 0);
5828 if (lreg <= 31)
5829 {
5830 if (offset_expr.X_op == O_absent)
ea1fb5dc 5831 move_register (&icnt, lreg, 0);
252b5132
RH
5832 else
5833 {
5834 assert (offset_expr.X_op == O_constant);
5835 load_register (&icnt, lreg, &offset_expr, 0);
5836 }
5837 }
5838 }
5839 return;
5840 }
5841
5842 /* We know that sym is in the .rdata section. First we get the
5843 upper 16 bits of the address. */
5844 if (mips_pic == NO_PIC)
5845 {
956cd1d6 5846 macro_build_lui (NULL, &icnt, &offset_expr, AT);
252b5132
RH
5847 }
5848 else if (mips_pic == SVR4_PIC)
5849 {
5850 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 5851 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
c9914766
TS
5852 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
5853 mips_gp_register);
252b5132
RH
5854 }
5855 else if (mips_pic == EMBEDDED_PIC)
5856 {
5857 /* For embedded PIC we pick up the entire address off $gp in
5858 a single instruction. */
5859 macro_build ((char *) NULL, &icnt, &offset_expr,
c9914766
TS
5860 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j", AT,
5861 mips_gp_register, (int) BFD_RELOC_GPREL16);
252b5132
RH
5862 offset_expr.X_op = O_constant;
5863 offset_expr.X_add_number = 0;
5864 }
5865 else
5866 abort ();
bdaaa2e1 5867
252b5132 5868 /* Now we load the register(s). */
ca4e0257 5869 if (HAVE_64BIT_GPRS)
252b5132
RH
5870 macro_build ((char *) NULL, &icnt, &offset_expr, "ld", "t,o(b)",
5871 treg, (int) BFD_RELOC_LO16, AT);
5872 else
5873 {
5874 macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
5875 treg, (int) BFD_RELOC_LO16, AT);
f9419b05 5876 if (treg != RA)
252b5132
RH
5877 {
5878 /* FIXME: How in the world do we deal with the possible
5879 overflow here? */
5880 offset_expr.X_add_number += 4;
5881 macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
5882 treg + 1, (int) BFD_RELOC_LO16, AT);
5883 }
5884 }
5885
5886 /* To avoid confusion in tc_gen_reloc, we must ensure that this
5887 does not become a variant frag. */
5888 frag_wane (frag_now);
5889 frag_new (0);
5890
5891 break;
5892
5893 case M_LI_DD:
ca4e0257
RS
5894 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
5895 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
5896 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
5897 the value and the low order 32 bits are either zero or in
5898 OFFSET_EXPR. */
252b5132
RH
5899 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
5900 {
ca4e0257
RS
5901 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_FPRS);
5902 if (HAVE_64BIT_FPRS)
5903 {
5904 assert (HAVE_64BIT_GPRS);
5905 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5906 "dmtc1", "t,S", AT, treg);
5907 }
252b5132
RH
5908 else
5909 {
5910 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5911 "mtc1", "t,G", AT, treg + 1);
5912 if (offset_expr.X_op == O_absent)
5913 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5914 "mtc1", "t,G", 0, treg);
5915 else
5916 {
5917 assert (offset_expr.X_op == O_constant);
5918 load_register (&icnt, AT, &offset_expr, 0);
5919 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5920 "mtc1", "t,G", AT, treg);
5921 }
5922 }
5923 break;
5924 }
5925
5926 assert (offset_expr.X_op == O_symbol
5927 && offset_expr.X_add_number == 0);
5928 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
5929 if (strcmp (s, ".lit8") == 0)
5930 {
e7af610e 5931 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
5932 {
5933 macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
c9914766
TS
5934 "T,o(b)", treg, (int) BFD_RELOC_MIPS_LITERAL,
5935 mips_gp_register);
252b5132
RH
5936 return;
5937 }
c9914766 5938 breg = mips_gp_register;
252b5132
RH
5939 r = BFD_RELOC_MIPS_LITERAL;
5940 goto dob;
5941 }
5942 else
5943 {
5944 assert (strcmp (s, RDATA_SECTION_NAME) == 0);
5945 if (mips_pic == SVR4_PIC)
5946 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 5947 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
c9914766
TS
5948 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
5949 mips_gp_register);
252b5132
RH
5950 else
5951 {
5952 /* FIXME: This won't work for a 64 bit address. */
956cd1d6 5953 macro_build_lui (NULL, &icnt, &offset_expr, AT);
252b5132 5954 }
bdaaa2e1 5955
e7af610e 5956 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
5957 {
5958 macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
5959 "T,o(b)", treg, (int) BFD_RELOC_LO16, AT);
5960
5961 /* To avoid confusion in tc_gen_reloc, we must ensure
5962 that this does not become a variant frag. */
5963 frag_wane (frag_now);
5964 frag_new (0);
5965
5966 break;
5967 }
5968 breg = AT;
5969 r = BFD_RELOC_LO16;
5970 goto dob;
5971 }
5972
5973 case M_L_DOB:
ec68c924 5974 if (mips_arch == CPU_R4650)
252b5132
RH
5975 {
5976 as_bad (_("opcode not supported on this processor"));
5977 return;
5978 }
5979 /* Even on a big endian machine $fn comes before $fn+1. We have
5980 to adjust when loading from memory. */
5981 r = BFD_RELOC_LO16;
5982 dob:
e7af610e 5983 assert (mips_opts.isa == ISA_MIPS1);
252b5132
RH
5984 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
5985 target_big_endian ? treg + 1 : treg,
5986 (int) r, breg);
5987 /* FIXME: A possible overflow which I don't know how to deal
5988 with. */
5989 offset_expr.X_add_number += 4;
5990 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
5991 target_big_endian ? treg : treg + 1,
5992 (int) r, breg);
5993
5994 /* To avoid confusion in tc_gen_reloc, we must ensure that this
5995 does not become a variant frag. */
5996 frag_wane (frag_now);
5997 frag_new (0);
5998
5999 if (breg != AT)
6000 return;
6001 break;
6002
6003 case M_L_DAB:
6004 /*
6005 * The MIPS assembler seems to check for X_add_number not
6006 * being double aligned and generating:
6007 * lui at,%hi(foo+1)
6008 * addu at,at,v1
6009 * addiu at,at,%lo(foo+1)
6010 * lwc1 f2,0(at)
6011 * lwc1 f3,4(at)
6012 * But, the resulting address is the same after relocation so why
6013 * generate the extra instruction?
6014 */
ec68c924 6015 if (mips_arch == CPU_R4650)
252b5132
RH
6016 {
6017 as_bad (_("opcode not supported on this processor"));
6018 return;
6019 }
bdaaa2e1 6020 /* Itbl support may require additional care here. */
252b5132 6021 coproc = 1;
e7af610e 6022 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
6023 {
6024 s = "ldc1";
6025 goto ld;
6026 }
6027
6028 s = "lwc1";
6029 fmt = "T,o(b)";
6030 goto ldd_std;
6031
6032 case M_S_DAB:
ec68c924 6033 if (mips_arch == CPU_R4650)
252b5132
RH
6034 {
6035 as_bad (_("opcode not supported on this processor"));
6036 return;
6037 }
6038
e7af610e 6039 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
6040 {
6041 s = "sdc1";
6042 goto st;
6043 }
6044
6045 s = "swc1";
6046 fmt = "T,o(b)";
bdaaa2e1 6047 /* Itbl support may require additional care here. */
252b5132
RH
6048 coproc = 1;
6049 goto ldd_std;
6050
6051 case M_LD_AB:
ca4e0257 6052 if (HAVE_64BIT_GPRS)
252b5132
RH
6053 {
6054 s = "ld";
6055 goto ld;
6056 }
6057
6058 s = "lw";
6059 fmt = "t,o(b)";
6060 goto ldd_std;
6061
6062 case M_SD_AB:
ca4e0257 6063 if (HAVE_64BIT_GPRS)
252b5132
RH
6064 {
6065 s = "sd";
6066 goto st;
6067 }
6068
6069 s = "sw";
6070 fmt = "t,o(b)";
6071
6072 ldd_std:
afdbd6d0
CD
6073 /* We do _not_ bother to allow embedded PIC (symbol-local_symbol)
6074 loads for the case of doing a pair of loads to simulate an 'ld'.
6075 This is not currently done by the compiler, and assembly coders
6076 writing embedded-pic code can cope. */
6077
252b5132
RH
6078 if (offset_expr.X_op != O_symbol
6079 && offset_expr.X_op != O_constant)
6080 {
6081 as_bad (_("expression too complex"));
6082 offset_expr.X_op = O_constant;
6083 }
6084
6085 /* Even on a big endian machine $fn comes before $fn+1. We have
6086 to adjust when loading from memory. We set coproc if we must
6087 load $fn+1 first. */
bdaaa2e1 6088 /* Itbl support may require additional care here. */
252b5132
RH
6089 if (! target_big_endian)
6090 coproc = 0;
6091
6092 if (mips_pic == NO_PIC
6093 || offset_expr.X_op == O_constant)
6094 {
f9419b05
TS
6095 char *p;
6096
252b5132 6097 /* If this is a reference to a GP relative symbol, we want
cdf6fd85
TS
6098 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6099 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
6100 If we have a base register, we use this
6101 addu $at,$breg,$gp
cdf6fd85
TS
6102 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6103 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
6104 If this is not a GP relative symbol, we want
6105 lui $at,<sym> (BFD_RELOC_HI16_S)
6106 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6107 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6108 If there is a base register, we add it to $at after the
6109 lui instruction. If there is a constant, we always use
6110 the last case. */
e7d556df 6111 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
252b5132
RH
6112 || nopic_need_relax (offset_expr.X_add_symbol, 1))
6113 {
6114 p = NULL;
6115 used_at = 1;
6116 }
6117 else
6118 {
6119 int off;
6120
6121 if (breg == 0)
6122 {
6123 frag_grow (28);
c9914766 6124 tempreg = mips_gp_register;
252b5132
RH
6125 off = 0;
6126 used_at = 0;
6127 }
6128 else
6129 {
6130 frag_grow (36);
6131 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6132 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 6133 "d,v,t", AT, breg, mips_gp_register);
252b5132
RH
6134 tempreg = AT;
6135 off = 4;
6136 used_at = 1;
6137 }
6138
beae10d5 6139 /* Itbl support may require additional care here. */
252b5132
RH
6140 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6141 coproc ? treg + 1 : treg,
cdf6fd85 6142 (int) BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6143 offset_expr.X_add_number += 4;
6144
6145 /* Set mips_optimize to 2 to avoid inserting an
6146 undesired nop. */
6147 hold_mips_optimize = mips_optimize;
6148 mips_optimize = 2;
beae10d5 6149 /* Itbl support may require additional care here. */
252b5132
RH
6150 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6151 coproc ? treg : treg + 1,
cdf6fd85 6152 (int) BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6153 mips_optimize = hold_mips_optimize;
6154
6155 p = frag_var (rs_machine_dependent, 12 + off, 0,
6156 RELAX_ENCODE (8 + off, 12 + off, 0, 4 + off, 1,
6157 used_at && mips_opts.noat),
956cd1d6 6158 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
6159
6160 /* We just generated two relocs. When tc_gen_reloc
6161 handles this case, it will skip the first reloc and
6162 handle the second. The second reloc already has an
6163 extra addend of 4, which we added above. We must
6164 subtract it out, and then subtract another 4 to make
6165 the first reloc come out right. The second reloc
6166 will come out right because we are going to add 4 to
6167 offset_expr when we build its instruction below.
6168
6169 If we have a symbol, then we don't want to include
6170 the offset, because it will wind up being included
6171 when we generate the reloc. */
6172
6173 if (offset_expr.X_op == O_constant)
6174 offset_expr.X_add_number -= 8;
6175 else
6176 {
6177 offset_expr.X_add_number = -4;
6178 offset_expr.X_op = O_constant;
6179 }
6180 }
6181 macro_build_lui (p, &icnt, &offset_expr, AT);
6182 if (p != NULL)
6183 p += 4;
6184 if (breg != 0)
6185 {
6186 macro_build (p, &icnt, (expressionS *) NULL,
ca4e0257 6187 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
6188 "d,v,t", AT, breg, AT);
6189 if (p != NULL)
6190 p += 4;
6191 }
beae10d5 6192 /* Itbl support may require additional care here. */
252b5132
RH
6193 macro_build (p, &icnt, &offset_expr, s, fmt,
6194 coproc ? treg + 1 : treg,
6195 (int) BFD_RELOC_LO16, AT);
6196 if (p != NULL)
6197 p += 4;
6198 /* FIXME: How do we handle overflow here? */
6199 offset_expr.X_add_number += 4;
beae10d5 6200 /* Itbl support may require additional care here. */
252b5132
RH
6201 macro_build (p, &icnt, &offset_expr, s, fmt,
6202 coproc ? treg : treg + 1,
6203 (int) BFD_RELOC_LO16, AT);
bdaaa2e1 6204 }
252b5132
RH
6205 else if (mips_pic == SVR4_PIC && ! mips_big_got)
6206 {
6207 int off;
6208
6209 /* If this is a reference to an external symbol, we want
6210 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6211 nop
6212 <op> $treg,0($at)
6213 <op> $treg+1,4($at)
6214 Otherwise we want
6215 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6216 nop
6217 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6218 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6219 If there is a base register we add it to $at before the
6220 lwc1 instructions. If there is a constant we include it
6221 in the lwc1 instructions. */
6222 used_at = 1;
6223 expr1.X_add_number = offset_expr.X_add_number;
6224 offset_expr.X_add_number = 0;
6225 if (expr1.X_add_number < -0x8000
6226 || expr1.X_add_number >= 0x8000 - 4)
6227 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6228 if (breg == 0)
6229 off = 0;
6230 else
6231 off = 4;
6232 frag_grow (24 + off);
6233 macro_build ((char *) NULL, &icnt, &offset_expr,
c9914766
TS
6234 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", AT,
6235 (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132
RH
6236 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6237 if (breg != 0)
6238 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6239 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132 6240 "d,v,t", AT, breg, AT);
beae10d5 6241 /* Itbl support may require additional care here. */
252b5132
RH
6242 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6243 coproc ? treg + 1 : treg,
6244 (int) BFD_RELOC_LO16, AT);
6245 expr1.X_add_number += 4;
6246
6247 /* Set mips_optimize to 2 to avoid inserting an undesired
6248 nop. */
6249 hold_mips_optimize = mips_optimize;
6250 mips_optimize = 2;
beae10d5 6251 /* Itbl support may require additional care here. */
252b5132
RH
6252 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6253 coproc ? treg : treg + 1,
6254 (int) BFD_RELOC_LO16, AT);
6255 mips_optimize = hold_mips_optimize;
6256
6257 (void) frag_var (rs_machine_dependent, 0, 0,
6258 RELAX_ENCODE (0, 0, -16 - off, -8, 1, 0),
c4e7957c 6259 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
6260 }
6261 else if (mips_pic == SVR4_PIC)
6262 {
6263 int gpdel, off;
f9419b05 6264 char *p;
252b5132
RH
6265
6266 /* If this is a reference to an external symbol, we want
6267 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6268 addu $at,$at,$gp
6269 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
6270 nop
6271 <op> $treg,0($at)
6272 <op> $treg+1,4($at)
6273 Otherwise we want
6274 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6275 nop
6276 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6277 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6278 If there is a base register we add it to $at before the
6279 lwc1 instructions. If there is a constant we include it
6280 in the lwc1 instructions. */
6281 used_at = 1;
6282 expr1.X_add_number = offset_expr.X_add_number;
6283 offset_expr.X_add_number = 0;
6284 if (expr1.X_add_number < -0x8000
6285 || expr1.X_add_number >= 0x8000 - 4)
6286 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
c9914766 6287 if (reg_needs_delay (mips_gp_register))
252b5132
RH
6288 gpdel = 4;
6289 else
6290 gpdel = 0;
6291 if (breg == 0)
6292 off = 0;
6293 else
6294 off = 4;
6295 frag_grow (56);
6296 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
6297 AT, (int) BFD_RELOC_MIPS_GOT_HI16);
6298 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6299 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 6300 "d,v,t", AT, AT, mips_gp_register);
252b5132 6301 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 6302 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
6303 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT_LO16, AT);
6304 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6305 if (breg != 0)
6306 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6307 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132 6308 "d,v,t", AT, breg, AT);
beae10d5 6309 /* Itbl support may require additional care here. */
252b5132
RH
6310 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6311 coproc ? treg + 1 : treg,
6312 (int) BFD_RELOC_LO16, AT);
6313 expr1.X_add_number += 4;
6314
6315 /* Set mips_optimize to 2 to avoid inserting an undesired
6316 nop. */
6317 hold_mips_optimize = mips_optimize;
6318 mips_optimize = 2;
beae10d5 6319 /* Itbl support may require additional care here. */
252b5132
RH
6320 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6321 coproc ? treg : treg + 1,
6322 (int) BFD_RELOC_LO16, AT);
6323 mips_optimize = hold_mips_optimize;
6324 expr1.X_add_number -= 4;
6325
6326 p = frag_var (rs_machine_dependent, 16 + gpdel + off, 0,
6327 RELAX_ENCODE (24 + off, 16 + gpdel + off, gpdel,
6328 8 + gpdel + off, 1, 0),
c4e7957c 6329 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
6330 if (gpdel > 0)
6331 {
6332 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6333 p += 4;
6334 }
6335 macro_build (p, &icnt, &offset_expr,
ca4e0257 6336 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
c9914766
TS
6337 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6338 mips_gp_register);
252b5132
RH
6339 p += 4;
6340 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6341 p += 4;
6342 if (breg != 0)
6343 {
6344 macro_build (p, &icnt, (expressionS *) NULL,
ca4e0257 6345 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
6346 "d,v,t", AT, breg, AT);
6347 p += 4;
6348 }
beae10d5 6349 /* Itbl support may require additional care here. */
252b5132
RH
6350 macro_build (p, &icnt, &expr1, s, fmt,
6351 coproc ? treg + 1 : treg,
6352 (int) BFD_RELOC_LO16, AT);
6353 p += 4;
6354 expr1.X_add_number += 4;
6355
6356 /* Set mips_optimize to 2 to avoid inserting an undesired
6357 nop. */
6358 hold_mips_optimize = mips_optimize;
6359 mips_optimize = 2;
beae10d5 6360 /* Itbl support may require additional care here. */
252b5132
RH
6361 macro_build (p, &icnt, &expr1, s, fmt,
6362 coproc ? treg : treg + 1,
6363 (int) BFD_RELOC_LO16, AT);
6364 mips_optimize = hold_mips_optimize;
6365 }
6366 else if (mips_pic == EMBEDDED_PIC)
6367 {
6368 /* If there is no base register, we use
cdf6fd85
TS
6369 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6370 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
6371 If we have a base register, we use
6372 addu $at,$breg,$gp
cdf6fd85
TS
6373 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6374 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
6375 */
6376 if (breg == 0)
6377 {
c9914766 6378 tempreg = mips_gp_register;
252b5132
RH
6379 used_at = 0;
6380 }
6381 else
6382 {
6383 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6384 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 6385 "d,v,t", AT, breg, mips_gp_register);
252b5132
RH
6386 tempreg = AT;
6387 used_at = 1;
6388 }
6389
beae10d5 6390 /* Itbl support may require additional care here. */
252b5132
RH
6391 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6392 coproc ? treg + 1 : treg,
cdf6fd85 6393 (int) BFD_RELOC_GPREL16, tempreg);
252b5132 6394 offset_expr.X_add_number += 4;
beae10d5 6395 /* Itbl support may require additional care here. */
252b5132
RH
6396 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6397 coproc ? treg : treg + 1,
cdf6fd85 6398 (int) BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6399 }
6400 else
6401 abort ();
6402
6403 if (! used_at)
6404 return;
6405
6406 break;
6407
6408 case M_LD_OB:
6409 s = "lw";
6410 goto sd_ob;
6411 case M_SD_OB:
6412 s = "sw";
6413 sd_ob:
ca4e0257 6414 assert (HAVE_32BIT_ADDRESSES);
252b5132
RH
6415 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
6416 (int) BFD_RELOC_LO16, breg);
6417 offset_expr.X_add_number += 4;
6418 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg + 1,
6419 (int) BFD_RELOC_LO16, breg);
6420 return;
6421
6422 /* New code added to support COPZ instructions.
6423 This code builds table entries out of the macros in mip_opcodes.
6424 R4000 uses interlocks to handle coproc delays.
6425 Other chips (like the R3000) require nops to be inserted for delays.
6426
f72c8c98 6427 FIXME: Currently, we require that the user handle delays.
252b5132
RH
6428 In order to fill delay slots for non-interlocked chips,
6429 we must have a way to specify delays based on the coprocessor.
6430 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
6431 What are the side-effects of the cop instruction?
6432 What cache support might we have and what are its effects?
6433 Both coprocessor & memory require delays. how long???
bdaaa2e1 6434 What registers are read/set/modified?
252b5132
RH
6435
6436 If an itbl is provided to interpret cop instructions,
bdaaa2e1 6437 this knowledge can be encoded in the itbl spec. */
252b5132
RH
6438
6439 case M_COP0:
6440 s = "c0";
6441 goto copz;
6442 case M_COP1:
6443 s = "c1";
6444 goto copz;
6445 case M_COP2:
6446 s = "c2";
6447 goto copz;
6448 case M_COP3:
6449 s = "c3";
6450 copz:
6451 /* For now we just do C (same as Cz). The parameter will be
6452 stored in insn_opcode by mips_ip. */
6453 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "C",
6454 ip->insn_opcode);
6455 return;
6456
ea1fb5dc
RS
6457 case M_MOVE:
6458 move_register (&icnt, dreg, sreg);
6459 return;
6460
252b5132
RH
6461#ifdef LOSING_COMPILER
6462 default:
6463 /* Try and see if this is a new itbl instruction.
6464 This code builds table entries out of the macros in mip_opcodes.
6465 FIXME: For now we just assemble the expression and pass it's
6466 value along as a 32-bit immediate.
bdaaa2e1 6467 We may want to have the assembler assemble this value,
252b5132
RH
6468 so that we gain the assembler's knowledge of delay slots,
6469 symbols, etc.
6470 Would it be more efficient to use mask (id) here? */
bdaaa2e1 6471 if (itbl_have_entries
252b5132 6472 && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
beae10d5 6473 {
252b5132
RH
6474 s = ip->insn_mo->name;
6475 s2 = "cop3";
6476 coproc = ITBL_DECODE_PNUM (immed_expr);;
6477 macro_build ((char *) NULL, &icnt, &immed_expr, s, "C");
6478 return;
beae10d5 6479 }
252b5132
RH
6480 macro2 (ip);
6481 return;
6482 }
6483 if (mips_opts.noat)
6484 as_warn (_("Macro used $at after \".set noat\""));
6485}
bdaaa2e1 6486
252b5132
RH
6487static void
6488macro2 (ip)
6489 struct mips_cl_insn *ip;
6490{
6491 register int treg, sreg, dreg, breg;
6492 int tempreg;
6493 int mask;
6494 int icnt = 0;
6495 int used_at;
6496 expressionS expr1;
6497 const char *s;
6498 const char *s2;
6499 const char *fmt;
6500 int likely = 0;
6501 int dbl = 0;
6502 int coproc = 0;
6503 int lr = 0;
6504 int imm = 0;
6505 int off;
6506 offsetT maxnum;
6507 bfd_reloc_code_real_type r;
6508 char *p;
bdaaa2e1 6509
252b5132
RH
6510 treg = (ip->insn_opcode >> 16) & 0x1f;
6511 dreg = (ip->insn_opcode >> 11) & 0x1f;
6512 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
6513 mask = ip->insn_mo->mask;
bdaaa2e1 6514
252b5132
RH
6515 expr1.X_op = O_constant;
6516 expr1.X_op_symbol = NULL;
6517 expr1.X_add_symbol = NULL;
6518 expr1.X_add_number = 1;
bdaaa2e1 6519
252b5132
RH
6520 switch (mask)
6521 {
6522#endif /* LOSING_COMPILER */
6523
6524 case M_DMUL:
6525 dbl = 1;
6526 case M_MUL:
2396cfb9
TS
6527 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6528 dbl ? "dmultu" : "multu", "s,t", sreg, treg);
6529 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6530 dreg);
252b5132
RH
6531 return;
6532
6533 case M_DMUL_I:
6534 dbl = 1;
6535 case M_MUL_I:
6536 /* The MIPS assembler some times generates shifts and adds. I'm
6537 not trying to be that fancy. GCC should do this for us
6538 anyway. */
6539 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9 6540 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 6541 dbl ? "dmult" : "mult", "s,t", sreg, AT);
2396cfb9
TS
6542 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6543 dreg);
252b5132
RH
6544 break;
6545
6546 case M_DMULO_I:
6547 dbl = 1;
6548 case M_MULO_I:
6549 imm = 1;
6550 goto do_mulo;
6551
6552 case M_DMULO:
6553 dbl = 1;
6554 case M_MULO:
6555 do_mulo:
6556 mips_emit_delays (true);
6557 ++mips_opts.noreorder;
6558 mips_any_noreorder = 1;
6559 if (imm)
6560 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9 6561 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 6562 dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
2396cfb9
TS
6563 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6564 dreg);
6565 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
f9419b05 6566 dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, RA);
2396cfb9
TS
6567 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6568 AT);
252b5132 6569 if (mips_trap)
9bd7d936
MR
6570 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne",
6571 "s,t,q", dreg, AT, 6);
252b5132
RH
6572 else
6573 {
6574 expr1.X_add_number = 8;
2396cfb9
TS
6575 macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", dreg,
6576 AT);
6577 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6578 0);
6579 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6580 "c", 6);
252b5132
RH
6581 }
6582 --mips_opts.noreorder;
2396cfb9 6583 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d", dreg);
252b5132
RH
6584 break;
6585
6586 case M_DMULOU_I:
6587 dbl = 1;
6588 case M_MULOU_I:
6589 imm = 1;
6590 goto do_mulou;
6591
6592 case M_DMULOU:
6593 dbl = 1;
6594 case M_MULOU:
6595 do_mulou:
6596 mips_emit_delays (true);
6597 ++mips_opts.noreorder;
6598 mips_any_noreorder = 1;
6599 if (imm)
6600 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9 6601 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
252b5132
RH
6602 dbl ? "dmultu" : "multu",
6603 "s,t", sreg, imm ? AT : treg);
2396cfb9
TS
6604 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6605 AT);
6606 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6607 dreg);
252b5132 6608 if (mips_trap)
9bd7d936
MR
6609 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne",
6610 "s,t,q", AT, 0, 6);
252b5132
RH
6611 else
6612 {
6613 expr1.X_add_number = 8;
6614 macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", AT, 0);
2396cfb9
TS
6615 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6616 0);
6617 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6618 "c", 6);
252b5132
RH
6619 }
6620 --mips_opts.noreorder;
6621 break;
6622
771c7ce4
TS
6623 case M_DROL:
6624 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
6625 "d,v,t", AT, 0, treg);
6626 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
6627 "d,t,s", AT, sreg, AT);
6628 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
6629 "d,t,s", dreg, sreg, treg);
6630 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6631 "d,v,t", dreg, dreg, AT);
6632 break;
6633
252b5132 6634 case M_ROL:
2396cfb9
TS
6635 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
6636 "d,v,t", AT, 0, treg);
6637 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
6638 "d,t,s", AT, sreg, AT);
6639 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
6640 "d,t,s", dreg, sreg, treg);
6641 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6642 "d,v,t", dreg, dreg, AT);
252b5132
RH
6643 break;
6644
771c7ce4
TS
6645 case M_DROL_I:
6646 {
6647 unsigned int rot;
6648 char *l, *r;
6649
6650 if (imm_expr.X_op != O_constant)
6651 as_bad (_("rotate count too large"));
6652 rot = imm_expr.X_add_number & 0x3f;
6653 if (! rot)
6654 break;
6655 l = (rot < 0x20) ? "dsll" : "dsll32";
6656 r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
6657 rot &= 0x1f;
6658 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
6659 "d,w,<", AT, sreg, rot);
6660 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
6661 "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6662 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6663 "d,v,t", dreg, dreg, AT);
6664 }
6665 break;
6666
252b5132 6667 case M_ROL_I:
771c7ce4
TS
6668 {
6669 unsigned int rot;
6670
6671 if (imm_expr.X_op != O_constant)
6672 as_bad (_("rotate count too large"));
6673 rot = imm_expr.X_add_number & 0x1f;
6674 if (! rot)
6675 break;
6676 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
6677 "d,w,<", AT, sreg, rot);
6678 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
6679 "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6680 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6681 "d,v,t", dreg, dreg, AT);
6682 }
6683 break;
6684
6685 case M_DROR:
6686 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
6687 "d,v,t", AT, 0, treg);
6688 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
6689 "d,t,s", AT, sreg, AT);
6690 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
6691 "d,t,s", dreg, sreg, treg);
6692 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6693 "d,v,t", dreg, dreg, AT);
252b5132
RH
6694 break;
6695
6696 case M_ROR:
2396cfb9
TS
6697 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
6698 "d,v,t", AT, 0, treg);
6699 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
6700 "d,t,s", AT, sreg, AT);
6701 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
6702 "d,t,s", dreg, sreg, treg);
6703 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6704 "d,v,t", dreg, dreg, AT);
252b5132
RH
6705 break;
6706
771c7ce4
TS
6707 case M_DROR_I:
6708 {
6709 unsigned int rot;
6710 char *l, *r;
6711
6712 if (imm_expr.X_op != O_constant)
6713 as_bad (_("rotate count too large"));
6714 rot = imm_expr.X_add_number & 0x3f;
6715 if (! rot)
6716 break;
6717 r = (rot < 0x20) ? "dsrl" : "dsrl32";
6718 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
6719 rot &= 0x1f;
6720 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
6721 "d,w,<", AT, sreg, rot);
6722 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
6723 "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6724 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6725 "d,v,t", dreg, dreg, AT);
6726 }
6727 break;
6728
252b5132 6729 case M_ROR_I:
771c7ce4
TS
6730 {
6731 unsigned int rot;
6732
6733 if (imm_expr.X_op != O_constant)
6734 as_bad (_("rotate count too large"));
6735 rot = imm_expr.X_add_number & 0x1f;
6736 if (! rot)
6737 break;
6738 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
6739 "d,w,<", AT, sreg, rot);
6740 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
6741 "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6742 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6743 "d,v,t", dreg, dreg, AT);
6744 }
252b5132
RH
6745 break;
6746
6747 case M_S_DOB:
ec68c924 6748 if (mips_arch == CPU_R4650)
252b5132
RH
6749 {
6750 as_bad (_("opcode not supported on this processor"));
6751 return;
6752 }
e7af610e 6753 assert (mips_opts.isa == ISA_MIPS1);
252b5132
RH
6754 /* Even on a big endian machine $fn comes before $fn+1. We have
6755 to adjust when storing to memory. */
6756 macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
6757 target_big_endian ? treg + 1 : treg,
6758 (int) BFD_RELOC_LO16, breg);
6759 offset_expr.X_add_number += 4;
6760 macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
6761 target_big_endian ? treg : treg + 1,
6762 (int) BFD_RELOC_LO16, breg);
6763 return;
6764
6765 case M_SEQ:
6766 if (sreg == 0)
6767 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6768 treg, (int) BFD_RELOC_LO16);
6769 else if (treg == 0)
6770 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6771 sreg, (int) BFD_RELOC_LO16);
6772 else
6773 {
2396cfb9
TS
6774 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6775 "d,v,t", dreg, sreg, treg);
252b5132
RH
6776 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6777 dreg, (int) BFD_RELOC_LO16);
6778 }
6779 return;
6780
6781 case M_SEQ_I:
6782 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6783 {
6784 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6785 sreg, (int) BFD_RELOC_LO16);
6786 return;
6787 }
6788 if (sreg == 0)
6789 {
6790 as_warn (_("Instruction %s: result is always false"),
6791 ip->insn_mo->name);
ea1fb5dc 6792 move_register (&icnt, dreg, 0);
252b5132
RH
6793 return;
6794 }
6795 if (imm_expr.X_op == O_constant
6796 && imm_expr.X_add_number >= 0
6797 && imm_expr.X_add_number < 0x10000)
6798 {
6799 macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg,
6800 sreg, (int) BFD_RELOC_LO16);
6801 used_at = 0;
6802 }
6803 else if (imm_expr.X_op == O_constant
6804 && imm_expr.X_add_number > -0x8000
6805 && imm_expr.X_add_number < 0)
6806 {
6807 imm_expr.X_add_number = -imm_expr.X_add_number;
6808 macro_build ((char *) NULL, &icnt, &imm_expr,
ca4e0257 6809 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
252b5132
RH
6810 "t,r,j", dreg, sreg,
6811 (int) BFD_RELOC_LO16);
6812 used_at = 0;
6813 }
6814 else
6815 {
4d34fb5f 6816 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
6817 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6818 "d,v,t", dreg, sreg, AT);
252b5132
RH
6819 used_at = 1;
6820 }
6821 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
6822 (int) BFD_RELOC_LO16);
6823 if (used_at)
6824 break;
6825 return;
6826
6827 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
6828 s = "slt";
6829 goto sge;
6830 case M_SGEU:
6831 s = "sltu";
6832 sge:
2396cfb9
TS
6833 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6834 dreg, sreg, treg);
252b5132
RH
6835 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6836 (int) BFD_RELOC_LO16);
6837 return;
6838
6839 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
6840 case M_SGEU_I:
6841 if (imm_expr.X_op == O_constant
6842 && imm_expr.X_add_number >= -0x8000
6843 && imm_expr.X_add_number < 0x8000)
6844 {
6845 macro_build ((char *) NULL, &icnt, &imm_expr,
6846 mask == M_SGE_I ? "slti" : "sltiu",
6847 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6848 used_at = 0;
6849 }
6850 else
6851 {
4d34fb5f 6852 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
6853 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6854 mask == M_SGE_I ? "slt" : "sltu", "d,v,t", dreg, sreg,
6855 AT);
252b5132
RH
6856 used_at = 1;
6857 }
6858 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6859 (int) BFD_RELOC_LO16);
6860 if (used_at)
6861 break;
6862 return;
6863
6864 case M_SGT: /* sreg > treg <==> treg < sreg */
6865 s = "slt";
6866 goto sgt;
6867 case M_SGTU:
6868 s = "sltu";
6869 sgt:
2396cfb9
TS
6870 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6871 dreg, treg, sreg);
252b5132
RH
6872 return;
6873
6874 case M_SGT_I: /* sreg > I <==> I < sreg */
6875 s = "slt";
6876 goto sgti;
6877 case M_SGTU_I:
6878 s = "sltu";
6879 sgti:
4d34fb5f 6880 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
6881 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6882 dreg, AT, sreg);
252b5132
RH
6883 break;
6884
2396cfb9 6885 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
252b5132
RH
6886 s = "slt";
6887 goto sle;
6888 case M_SLEU:
6889 s = "sltu";
6890 sle:
2396cfb9
TS
6891 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6892 dreg, treg, sreg);
252b5132
RH
6893 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6894 (int) BFD_RELOC_LO16);
6895 return;
6896
2396cfb9 6897 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
252b5132
RH
6898 s = "slt";
6899 goto slei;
6900 case M_SLEU_I:
6901 s = "sltu";
6902 slei:
4d34fb5f 6903 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
6904 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6905 dreg, AT, sreg);
252b5132
RH
6906 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6907 (int) BFD_RELOC_LO16);
6908 break;
6909
6910 case M_SLT_I:
6911 if (imm_expr.X_op == O_constant
6912 && imm_expr.X_add_number >= -0x8000
6913 && imm_expr.X_add_number < 0x8000)
6914 {
6915 macro_build ((char *) NULL, &icnt, &imm_expr, "slti", "t,r,j",
6916 dreg, sreg, (int) BFD_RELOC_LO16);
6917 return;
6918 }
4d34fb5f 6919 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
6920 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
6921 dreg, sreg, AT);
252b5132
RH
6922 break;
6923
6924 case M_SLTU_I:
6925 if (imm_expr.X_op == O_constant
6926 && imm_expr.X_add_number >= -0x8000
6927 && imm_expr.X_add_number < 0x8000)
6928 {
6929 macro_build ((char *) NULL, &icnt, &imm_expr, "sltiu", "t,r,j",
6930 dreg, sreg, (int) BFD_RELOC_LO16);
6931 return;
6932 }
4d34fb5f 6933 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
6934 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6935 "d,v,t", dreg, sreg, AT);
252b5132
RH
6936 break;
6937
6938 case M_SNE:
6939 if (sreg == 0)
2396cfb9
TS
6940 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6941 "d,v,t", dreg, 0, treg);
252b5132 6942 else if (treg == 0)
2396cfb9
TS
6943 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6944 "d,v,t", dreg, 0, sreg);
252b5132
RH
6945 else
6946 {
2396cfb9
TS
6947 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6948 "d,v,t", dreg, sreg, treg);
6949 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6950 "d,v,t", dreg, 0, dreg);
252b5132
RH
6951 }
6952 return;
6953
6954 case M_SNE_I:
6955 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6956 {
2396cfb9
TS
6957 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6958 "d,v,t", dreg, 0, sreg);
252b5132
RH
6959 return;
6960 }
6961 if (sreg == 0)
6962 {
6963 as_warn (_("Instruction %s: result is always true"),
6964 ip->insn_mo->name);
6965 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 6966 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
252b5132
RH
6967 "t,r,j", dreg, 0, (int) BFD_RELOC_LO16);
6968 return;
6969 }
6970 if (imm_expr.X_op == O_constant
6971 && imm_expr.X_add_number >= 0
6972 && imm_expr.X_add_number < 0x10000)
6973 {
6974 macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i",
6975 dreg, sreg, (int) BFD_RELOC_LO16);
6976 used_at = 0;
6977 }
6978 else if (imm_expr.X_op == O_constant
6979 && imm_expr.X_add_number > -0x8000
6980 && imm_expr.X_add_number < 0)
6981 {
6982 imm_expr.X_add_number = -imm_expr.X_add_number;
6983 macro_build ((char *) NULL, &icnt, &imm_expr,
ca4e0257 6984 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
252b5132
RH
6985 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6986 used_at = 0;
6987 }
6988 else
6989 {
4d34fb5f 6990 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
6991 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6992 "d,v,t", dreg, sreg, AT);
252b5132
RH
6993 used_at = 1;
6994 }
2396cfb9
TS
6995 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6996 "d,v,t", dreg, 0, dreg);
252b5132
RH
6997 if (used_at)
6998 break;
6999 return;
7000
7001 case M_DSUB_I:
7002 dbl = 1;
7003 case M_SUB_I:
7004 if (imm_expr.X_op == O_constant
7005 && imm_expr.X_add_number > -0x8000
7006 && imm_expr.X_add_number <= 0x8000)
7007 {
7008 imm_expr.X_add_number = -imm_expr.X_add_number;
7009 macro_build ((char *) NULL, &icnt, &imm_expr,
7010 dbl ? "daddi" : "addi",
7011 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7012 return;
7013 }
7014 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9 7015 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 7016 dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
252b5132
RH
7017 break;
7018
7019 case M_DSUBU_I:
7020 dbl = 1;
7021 case M_SUBU_I:
7022 if (imm_expr.X_op == O_constant
7023 && imm_expr.X_add_number > -0x8000
7024 && imm_expr.X_add_number <= 0x8000)
7025 {
7026 imm_expr.X_add_number = -imm_expr.X_add_number;
7027 macro_build ((char *) NULL, &icnt, &imm_expr,
7028 dbl ? "daddiu" : "addiu",
7029 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7030 return;
7031 }
7032 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9 7033 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 7034 dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
252b5132
RH
7035 break;
7036
7037 case M_TEQ_I:
7038 s = "teq";
7039 goto trap;
7040 case M_TGE_I:
7041 s = "tge";
7042 goto trap;
7043 case M_TGEU_I:
7044 s = "tgeu";
7045 goto trap;
7046 case M_TLT_I:
7047 s = "tlt";
7048 goto trap;
7049 case M_TLTU_I:
7050 s = "tltu";
7051 goto trap;
7052 case M_TNE_I:
7053 s = "tne";
7054 trap:
4d34fb5f 7055 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
7056 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "s,t", sreg,
7057 AT);
252b5132
RH
7058 break;
7059
252b5132 7060 case M_TRUNCWS:
43841e91 7061 case M_TRUNCWD:
e7af610e 7062 assert (mips_opts.isa == ISA_MIPS1);
252b5132
RH
7063 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
7064 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
7065
7066 /*
7067 * Is the double cfc1 instruction a bug in the mips assembler;
7068 * or is there a reason for it?
7069 */
7070 mips_emit_delays (true);
7071 ++mips_opts.noreorder;
7072 mips_any_noreorder = 1;
2396cfb9 7073 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
f9419b05 7074 treg, RA);
2396cfb9 7075 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
f9419b05 7076 treg, RA);
2396cfb9 7077 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
252b5132
RH
7078 expr1.X_add_number = 3;
7079 macro_build ((char *) NULL, &icnt, &expr1, "ori", "t,r,i", AT, treg,
7080 (int) BFD_RELOC_LO16);
7081 expr1.X_add_number = 2;
7082 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", AT, AT,
7083 (int) BFD_RELOC_LO16);
2396cfb9 7084 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
f9419b05 7085 AT, RA);
2396cfb9
TS
7086 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7087 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
252b5132 7088 mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S", dreg, sreg);
2396cfb9 7089 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
f9419b05 7090 treg, RA);
2396cfb9 7091 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
252b5132
RH
7092 --mips_opts.noreorder;
7093 break;
7094
7095 case M_ULH:
7096 s = "lb";
7097 goto ulh;
7098 case M_ULHU:
7099 s = "lbu";
7100 ulh:
7101 if (offset_expr.X_add_number >= 0x7fff)
7102 as_bad (_("operand overflow"));
7103 /* avoid load delay */
7104 if (! target_big_endian)
f9419b05 7105 ++offset_expr.X_add_number;
252b5132
RH
7106 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7107 (int) BFD_RELOC_LO16, breg);
7108 if (! target_big_endian)
f9419b05 7109 --offset_expr.X_add_number;
252b5132 7110 else
f9419b05 7111 ++offset_expr.X_add_number;
252b5132
RH
7112 macro_build ((char *) NULL, &icnt, &offset_expr, "lbu", "t,o(b)", AT,
7113 (int) BFD_RELOC_LO16, breg);
2396cfb9
TS
7114 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7115 treg, treg, 8);
7116 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7117 treg, treg, AT);
252b5132
RH
7118 break;
7119
7120 case M_ULD:
7121 s = "ldl";
7122 s2 = "ldr";
7123 off = 7;
7124 goto ulw;
7125 case M_ULW:
7126 s = "lwl";
7127 s2 = "lwr";
7128 off = 3;
7129 ulw:
7130 if (offset_expr.X_add_number >= 0x8000 - off)
7131 as_bad (_("operand overflow"));
7132 if (! target_big_endian)
7133 offset_expr.X_add_number += off;
7134 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7135 (int) BFD_RELOC_LO16, breg);
7136 if (! target_big_endian)
7137 offset_expr.X_add_number -= off;
7138 else
7139 offset_expr.X_add_number += off;
7140 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7141 (int) BFD_RELOC_LO16, breg);
7142 return;
7143
7144 case M_ULD_A:
7145 s = "ldl";
7146 s2 = "ldr";
7147 off = 7;
7148 goto ulwa;
7149 case M_ULW_A:
7150 s = "lwl";
7151 s2 = "lwr";
7152 off = 3;
7153 ulwa:
d6bc6245 7154 used_at = 1;
c9914766 7155 load_address (&icnt, AT, &offset_expr, &used_at);
252b5132
RH
7156 if (breg != 0)
7157 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 7158 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
7159 "d,v,t", AT, AT, breg);
7160 if (! target_big_endian)
7161 expr1.X_add_number = off;
7162 else
7163 expr1.X_add_number = 0;
7164 macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7165 (int) BFD_RELOC_LO16, AT);
7166 if (! target_big_endian)
7167 expr1.X_add_number = 0;
7168 else
7169 expr1.X_add_number = off;
7170 macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7171 (int) BFD_RELOC_LO16, AT);
7172 break;
7173
7174 case M_ULH_A:
7175 case M_ULHU_A:
d6bc6245 7176 used_at = 1;
c9914766 7177 load_address (&icnt, AT, &offset_expr, &used_at);
252b5132
RH
7178 if (breg != 0)
7179 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 7180 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
7181 "d,v,t", AT, AT, breg);
7182 if (target_big_endian)
7183 expr1.X_add_number = 0;
7184 macro_build ((char *) NULL, &icnt, &expr1,
7185 mask == M_ULH_A ? "lb" : "lbu", "t,o(b)", treg,
7186 (int) BFD_RELOC_LO16, AT);
7187 if (target_big_endian)
7188 expr1.X_add_number = 1;
7189 else
7190 expr1.X_add_number = 0;
7191 macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7192 (int) BFD_RELOC_LO16, AT);
2396cfb9
TS
7193 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7194 treg, treg, 8);
7195 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7196 treg, treg, AT);
252b5132
RH
7197 break;
7198
7199 case M_USH:
7200 if (offset_expr.X_add_number >= 0x7fff)
7201 as_bad (_("operand overflow"));
7202 if (target_big_endian)
f9419b05 7203 ++offset_expr.X_add_number;
252b5132
RH
7204 macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", treg,
7205 (int) BFD_RELOC_LO16, breg);
2396cfb9
TS
7206 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7207 AT, treg, 8);
252b5132 7208 if (target_big_endian)
f9419b05 7209 --offset_expr.X_add_number;
252b5132 7210 else
f9419b05 7211 ++offset_expr.X_add_number;
252b5132
RH
7212 macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", AT,
7213 (int) BFD_RELOC_LO16, breg);
7214 break;
7215
7216 case M_USD:
7217 s = "sdl";
7218 s2 = "sdr";
7219 off = 7;
7220 goto usw;
7221 case M_USW:
7222 s = "swl";
7223 s2 = "swr";
7224 off = 3;
7225 usw:
7226 if (offset_expr.X_add_number >= 0x8000 - off)
7227 as_bad (_("operand overflow"));
7228 if (! target_big_endian)
7229 offset_expr.X_add_number += off;
7230 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7231 (int) BFD_RELOC_LO16, breg);
7232 if (! target_big_endian)
7233 offset_expr.X_add_number -= off;
7234 else
7235 offset_expr.X_add_number += off;
7236 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7237 (int) BFD_RELOC_LO16, breg);
7238 return;
7239
7240 case M_USD_A:
7241 s = "sdl";
7242 s2 = "sdr";
7243 off = 7;
7244 goto uswa;
7245 case M_USW_A:
7246 s = "swl";
7247 s2 = "swr";
7248 off = 3;
7249 uswa:
d6bc6245 7250 used_at = 1;
c9914766 7251 load_address (&icnt, AT, &offset_expr, &used_at);
252b5132
RH
7252 if (breg != 0)
7253 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 7254 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
7255 "d,v,t", AT, AT, breg);
7256 if (! target_big_endian)
7257 expr1.X_add_number = off;
7258 else
7259 expr1.X_add_number = 0;
7260 macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7261 (int) BFD_RELOC_LO16, AT);
7262 if (! target_big_endian)
7263 expr1.X_add_number = 0;
7264 else
7265 expr1.X_add_number = off;
7266 macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7267 (int) BFD_RELOC_LO16, AT);
7268 break;
7269
7270 case M_USH_A:
d6bc6245 7271 used_at = 1;
c9914766 7272 load_address (&icnt, AT, &offset_expr, &used_at);
252b5132
RH
7273 if (breg != 0)
7274 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 7275 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
7276 "d,v,t", AT, AT, breg);
7277 if (! target_big_endian)
7278 expr1.X_add_number = 0;
7279 macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7280 (int) BFD_RELOC_LO16, AT);
2396cfb9
TS
7281 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7282 treg, treg, 8);
252b5132
RH
7283 if (! target_big_endian)
7284 expr1.X_add_number = 1;
7285 else
7286 expr1.X_add_number = 0;
7287 macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7288 (int) BFD_RELOC_LO16, AT);
7289 if (! target_big_endian)
7290 expr1.X_add_number = 0;
7291 else
7292 expr1.X_add_number = 1;
7293 macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7294 (int) BFD_RELOC_LO16, AT);
2396cfb9
TS
7295 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7296 treg, treg, 8);
7297 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7298 treg, treg, AT);
252b5132
RH
7299 break;
7300
7301 default:
7302 /* FIXME: Check if this is one of the itbl macros, since they
bdaaa2e1 7303 are added dynamically. */
252b5132
RH
7304 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7305 break;
7306 }
7307 if (mips_opts.noat)
7308 as_warn (_("Macro used $at after \".set noat\""));
7309}
7310
7311/* Implement macros in mips16 mode. */
7312
7313static void
7314mips16_macro (ip)
7315 struct mips_cl_insn *ip;
7316{
7317 int mask;
7318 int xreg, yreg, zreg, tmp;
7319 int icnt;
7320 expressionS expr1;
7321 int dbl;
7322 const char *s, *s2, *s3;
7323
7324 mask = ip->insn_mo->mask;
7325
7326 xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
7327 yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
7328 zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
7329
7330 icnt = 0;
7331
7332 expr1.X_op = O_constant;
7333 expr1.X_op_symbol = NULL;
7334 expr1.X_add_symbol = NULL;
7335 expr1.X_add_number = 1;
7336
7337 dbl = 0;
7338
7339 switch (mask)
7340 {
7341 default:
7342 internalError ();
7343
7344 case M_DDIV_3:
7345 dbl = 1;
7346 case M_DIV_3:
7347 s = "mflo";
7348 goto do_div3;
7349 case M_DREM_3:
7350 dbl = 1;
7351 case M_REM_3:
7352 s = "mfhi";
7353 do_div3:
7354 mips_emit_delays (true);
7355 ++mips_opts.noreorder;
7356 mips_any_noreorder = 1;
2396cfb9 7357 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
252b5132
RH
7358 dbl ? "ddiv" : "div",
7359 "0,x,y", xreg, yreg);
7360 expr1.X_add_number = 2;
7361 macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
2396cfb9
TS
7362 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break", "6",
7363 7);
bdaaa2e1 7364
252b5132
RH
7365 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7366 since that causes an overflow. We should do that as well,
7367 but I don't see how to do the comparisons without a temporary
7368 register. */
7369 --mips_opts.noreorder;
2396cfb9 7370 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x", zreg);
252b5132
RH
7371 break;
7372
7373 case M_DIVU_3:
7374 s = "divu";
7375 s2 = "mflo";
7376 goto do_divu3;
7377 case M_REMU_3:
7378 s = "divu";
7379 s2 = "mfhi";
7380 goto do_divu3;
7381 case M_DDIVU_3:
7382 s = "ddivu";
7383 s2 = "mflo";
7384 goto do_divu3;
7385 case M_DREMU_3:
7386 s = "ddivu";
7387 s2 = "mfhi";
7388 do_divu3:
7389 mips_emit_delays (true);
7390 ++mips_opts.noreorder;
7391 mips_any_noreorder = 1;
2396cfb9
TS
7392 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "0,x,y",
7393 xreg, yreg);
252b5132
RH
7394 expr1.X_add_number = 2;
7395 macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
98d3f06f
KH
7396 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
7397 "6", 7);
252b5132 7398 --mips_opts.noreorder;
2396cfb9 7399 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "x", zreg);
252b5132
RH
7400 break;
7401
7402 case M_DMUL:
7403 dbl = 1;
7404 case M_MUL:
2396cfb9 7405 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 7406 dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
2396cfb9
TS
7407 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "x",
7408 zreg);
252b5132
RH
7409 return;
7410
7411 case M_DSUBU_I:
7412 dbl = 1;
7413 goto do_subu;
7414 case M_SUBU_I:
7415 do_subu:
7416 if (imm_expr.X_op != O_constant)
7417 as_bad (_("Unsupported large constant"));
7418 imm_expr.X_add_number = -imm_expr.X_add_number;
7419 macro_build ((char *) NULL, &icnt, &imm_expr,
9a41af64 7420 dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
252b5132
RH
7421 break;
7422
7423 case M_SUBU_I_2:
7424 if (imm_expr.X_op != O_constant)
7425 as_bad (_("Unsupported large constant"));
7426 imm_expr.X_add_number = -imm_expr.X_add_number;
7427 macro_build ((char *) NULL, &icnt, &imm_expr, "addiu",
7428 "x,k", xreg);
7429 break;
7430
7431 case M_DSUBU_I_2:
7432 if (imm_expr.X_op != O_constant)
7433 as_bad (_("Unsupported large constant"));
7434 imm_expr.X_add_number = -imm_expr.X_add_number;
7435 macro_build ((char *) NULL, &icnt, &imm_expr, "daddiu",
7436 "y,j", yreg);
7437 break;
7438
7439 case M_BEQ:
7440 s = "cmp";
7441 s2 = "bteqz";
7442 goto do_branch;
7443 case M_BNE:
7444 s = "cmp";
7445 s2 = "btnez";
7446 goto do_branch;
7447 case M_BLT:
7448 s = "slt";
7449 s2 = "btnez";
7450 goto do_branch;
7451 case M_BLTU:
7452 s = "sltu";
7453 s2 = "btnez";
7454 goto do_branch;
7455 case M_BLE:
7456 s = "slt";
7457 s2 = "bteqz";
7458 goto do_reverse_branch;
7459 case M_BLEU:
7460 s = "sltu";
7461 s2 = "bteqz";
7462 goto do_reverse_branch;
7463 case M_BGE:
7464 s = "slt";
7465 s2 = "bteqz";
7466 goto do_branch;
7467 case M_BGEU:
7468 s = "sltu";
7469 s2 = "bteqz";
7470 goto do_branch;
7471 case M_BGT:
7472 s = "slt";
7473 s2 = "btnez";
7474 goto do_reverse_branch;
7475 case M_BGTU:
7476 s = "sltu";
7477 s2 = "btnez";
7478
7479 do_reverse_branch:
7480 tmp = xreg;
7481 xreg = yreg;
7482 yreg = tmp;
7483
7484 do_branch:
7485 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x,y",
7486 xreg, yreg);
7487 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7488 break;
7489
7490 case M_BEQ_I:
7491 s = "cmpi";
7492 s2 = "bteqz";
7493 s3 = "x,U";
7494 goto do_branch_i;
7495 case M_BNE_I:
7496 s = "cmpi";
7497 s2 = "btnez";
7498 s3 = "x,U";
7499 goto do_branch_i;
7500 case M_BLT_I:
7501 s = "slti";
7502 s2 = "btnez";
7503 s3 = "x,8";
7504 goto do_branch_i;
7505 case M_BLTU_I:
7506 s = "sltiu";
7507 s2 = "btnez";
7508 s3 = "x,8";
7509 goto do_branch_i;
7510 case M_BLE_I:
7511 s = "slti";
7512 s2 = "btnez";
7513 s3 = "x,8";
7514 goto do_addone_branch_i;
7515 case M_BLEU_I:
7516 s = "sltiu";
7517 s2 = "btnez";
7518 s3 = "x,8";
7519 goto do_addone_branch_i;
7520 case M_BGE_I:
7521 s = "slti";
7522 s2 = "bteqz";
7523 s3 = "x,8";
7524 goto do_branch_i;
7525 case M_BGEU_I:
7526 s = "sltiu";
7527 s2 = "bteqz";
7528 s3 = "x,8";
7529 goto do_branch_i;
7530 case M_BGT_I:
7531 s = "slti";
7532 s2 = "bteqz";
7533 s3 = "x,8";
7534 goto do_addone_branch_i;
7535 case M_BGTU_I:
7536 s = "sltiu";
7537 s2 = "bteqz";
7538 s3 = "x,8";
7539
7540 do_addone_branch_i:
7541 if (imm_expr.X_op != O_constant)
7542 as_bad (_("Unsupported large constant"));
7543 ++imm_expr.X_add_number;
7544
7545 do_branch_i:
7546 macro_build ((char *) NULL, &icnt, &imm_expr, s, s3, xreg);
7547 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7548 break;
7549
7550 case M_ABS:
7551 expr1.X_add_number = 0;
98d3f06f 7552 macro_build ((char *) NULL, &icnt, &expr1, "slti", "x,8", yreg);
252b5132 7553 if (xreg != yreg)
ea1fb5dc 7554 move_register (&icnt, xreg, yreg);
252b5132
RH
7555 expr1.X_add_number = 2;
7556 macro_build ((char *) NULL, &icnt, &expr1, "bteqz", "p");
7557 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7558 "neg", "x,w", xreg, xreg);
7559 }
7560}
7561
7562/* For consistency checking, verify that all bits are specified either
7563 by the match/mask part of the instruction definition, or by the
7564 operand list. */
7565static int
7566validate_mips_insn (opc)
7567 const struct mips_opcode *opc;
7568{
7569 const char *p = opc->args;
7570 char c;
7571 unsigned long used_bits = opc->mask;
7572
7573 if ((used_bits & opc->match) != opc->match)
7574 {
7575 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
7576 opc->name, opc->args);
7577 return 0;
7578 }
7579#define USE_BITS(mask,shift) (used_bits |= ((mask) << (shift)))
7580 while (*p)
7581 switch (c = *p++)
7582 {
7583 case ',': break;
7584 case '(': break;
7585 case ')': break;
7586 case '<': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7587 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7588 case 'A': break;
4372b673 7589 case 'B': USE_BITS (OP_MASK_CODE20, OP_SH_CODE20); break;
252b5132
RH
7590 case 'C': USE_BITS (OP_MASK_COPZ, OP_SH_COPZ); break;
7591 case 'D': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
7592 case 'E': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7593 case 'F': break;
7594 case 'G': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
156c2f8b 7595 case 'H': USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
252b5132 7596 case 'I': break;
e972090a 7597 case 'J': USE_BITS (OP_MASK_CODE19, OP_SH_CODE19); break;
252b5132
RH
7598 case 'L': break;
7599 case 'M': USE_BITS (OP_MASK_CCC, OP_SH_CCC); break;
7600 case 'N': USE_BITS (OP_MASK_BCC, OP_SH_BCC); break;
deec1734
CD
7601 case 'O': USE_BITS (OP_MASK_ALN, OP_SH_ALN); break;
7602 case 'Q': USE_BITS (OP_MASK_VSEL, OP_SH_VSEL);
7603 USE_BITS (OP_MASK_FT, OP_SH_FT); break;
252b5132
RH
7604 case 'R': USE_BITS (OP_MASK_FR, OP_SH_FR); break;
7605 case 'S': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7606 case 'T': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7607 case 'V': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7608 case 'W': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
deec1734
CD
7609 case 'X': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
7610 case 'Y': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7611 case 'Z': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
252b5132
RH
7612 case 'a': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break;
7613 case 'b': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7614 case 'c': USE_BITS (OP_MASK_CODE, OP_SH_CODE); break;
7615 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
7616 case 'f': break;
7617 case 'h': USE_BITS (OP_MASK_PREFX, OP_SH_PREFX); break;
7618 case 'i': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
7619 case 'j': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7620 case 'k': USE_BITS (OP_MASK_CACHE, OP_SH_CACHE); break;
7621 case 'l': break;
7622 case 'o': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7623 case 'p': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7624 case 'q': USE_BITS (OP_MASK_CODE2, OP_SH_CODE2); break;
7625 case 'r': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7626 case 's': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7627 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7628 case 'u': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
7629 case 'v': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7630 case 'w': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7631 case 'x': break;
7632 case 'z': break;
7633 case 'P': USE_BITS (OP_MASK_PERFREG, OP_SH_PERFREG); break;
4372b673
NC
7634 case 'U': USE_BITS (OP_MASK_RD, OP_SH_RD);
7635 USE_BITS (OP_MASK_RT, OP_SH_RT); break;
252b5132
RH
7636 default:
7637 as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
7638 c, opc->name, opc->args);
7639 return 0;
7640 }
7641#undef USE_BITS
7642 if (used_bits != 0xffffffff)
7643 {
7644 as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
7645 ~used_bits & 0xffffffff, opc->name, opc->args);
7646 return 0;
7647 }
7648 return 1;
7649}
7650
7651/* This routine assembles an instruction into its binary format. As a
7652 side effect, it sets one of the global variables imm_reloc or
7653 offset_reloc to the type of relocation to do if one of the operands
7654 is an address expression. */
7655
7656static void
7657mips_ip (str, ip)
7658 char *str;
7659 struct mips_cl_insn *ip;
7660{
7661 char *s;
7662 const char *args;
43841e91 7663 char c = 0;
252b5132
RH
7664 struct mips_opcode *insn;
7665 char *argsStart;
7666 unsigned int regno;
7667 unsigned int lastregno = 0;
7668 char *s_reset;
7669 char save_c = 0;
252b5132
RH
7670
7671 insn_error = NULL;
7672
7673 /* If the instruction contains a '.', we first try to match an instruction
7674 including the '.'. Then we try again without the '.'. */
7675 insn = NULL;
3882b010 7676 for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
252b5132
RH
7677 continue;
7678
7679 /* If we stopped on whitespace, then replace the whitespace with null for
7680 the call to hash_find. Save the character we replaced just in case we
7681 have to re-parse the instruction. */
3882b010 7682 if (ISSPACE (*s))
252b5132
RH
7683 {
7684 save_c = *s;
7685 *s++ = '\0';
7686 }
bdaaa2e1 7687
252b5132
RH
7688 insn = (struct mips_opcode *) hash_find (op_hash, str);
7689
7690 /* If we didn't find the instruction in the opcode table, try again, but
7691 this time with just the instruction up to, but not including the
7692 first '.'. */
7693 if (insn == NULL)
7694 {
bdaaa2e1 7695 /* Restore the character we overwrite above (if any). */
252b5132
RH
7696 if (save_c)
7697 *(--s) = save_c;
7698
7699 /* Scan up to the first '.' or whitespace. */
3882b010
L
7700 for (s = str;
7701 *s != '\0' && *s != '.' && !ISSPACE (*s);
7702 ++s)
252b5132
RH
7703 continue;
7704
7705 /* If we did not find a '.', then we can quit now. */
7706 if (*s != '.')
7707 {
7708 insn_error = "unrecognized opcode";
7709 return;
7710 }
7711
7712 /* Lookup the instruction in the hash table. */
7713 *s++ = '\0';
7714 if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
7715 {
7716 insn_error = "unrecognized opcode";
7717 return;
7718 }
252b5132
RH
7719 }
7720
7721 argsStart = s;
7722 for (;;)
7723 {
252b5132
RH
7724 boolean ok;
7725
7726 assert (strcmp (insn->name, str) == 0);
7727
1f25f5d3
CD
7728 if (OPCODE_IS_MEMBER (insn,
7729 (mips_opts.isa
aec421e0 7730 | (mips_opts.mips16 ? INSN_MIPS16 : 0)
deec1734 7731 | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
98d3f06f 7732 | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
1f25f5d3 7733 mips_arch))
252b5132 7734 ok = true;
bdaaa2e1 7735 else
252b5132 7736 ok = false;
bdaaa2e1 7737
252b5132
RH
7738 if (insn->pinfo != INSN_MACRO)
7739 {
ec68c924 7740 if (mips_arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
252b5132
RH
7741 ok = false;
7742 }
7743
7744 if (! ok)
7745 {
7746 if (insn + 1 < &mips_opcodes[NUMOPCODES]
7747 && strcmp (insn->name, insn[1].name) == 0)
7748 {
7749 ++insn;
7750 continue;
7751 }
252b5132 7752 else
beae10d5 7753 {
268f6bed
L
7754 if (!insn_error)
7755 {
7756 static char buf[100];
316f5878
RS
7757 if (mips_arch_info->is_isa)
7758 sprintf (buf,
7759 _("opcode not supported at this ISA level (%s)"),
7760 mips_cpu_info_from_isa (mips_opts.isa)->name);
7761 else
7762 sprintf (buf,
7763 _("opcode not supported on this processor: %s (%s)"),
7764 mips_arch_info->name,
7765 mips_cpu_info_from_isa (mips_opts.isa)->name);
268f6bed
L
7766 insn_error = buf;
7767 }
7768 if (save_c)
7769 *(--s) = save_c;
2bd7f1f3 7770 return;
252b5132 7771 }
252b5132
RH
7772 }
7773
7774 ip->insn_mo = insn;
7775 ip->insn_opcode = insn->match;
268f6bed 7776 insn_error = NULL;
252b5132
RH
7777 for (args = insn->args;; ++args)
7778 {
deec1734
CD
7779 int is_mdmx;
7780
ad8d3bb3 7781 s += strspn (s, " \t");
deec1734 7782 is_mdmx = 0;
252b5132
RH
7783 switch (*args)
7784 {
7785 case '\0': /* end of args */
7786 if (*s == '\0')
7787 return;
7788 break;
7789
7790 case ',':
7791 if (*s++ == *args)
7792 continue;
7793 s--;
7794 switch (*++args)
7795 {
7796 case 'r':
7797 case 'v':
38487616 7798 ip->insn_opcode |= lastregno << OP_SH_RS;
252b5132
RH
7799 continue;
7800
7801 case 'w':
38487616
TS
7802 ip->insn_opcode |= lastregno << OP_SH_RT;
7803 continue;
7804
252b5132 7805 case 'W':
38487616 7806 ip->insn_opcode |= lastregno << OP_SH_FT;
252b5132
RH
7807 continue;
7808
7809 case 'V':
38487616 7810 ip->insn_opcode |= lastregno << OP_SH_FS;
252b5132
RH
7811 continue;
7812 }
7813 break;
7814
7815 case '(':
7816 /* Handle optional base register.
7817 Either the base register is omitted or
bdaaa2e1 7818 we must have a left paren. */
252b5132
RH
7819 /* This is dependent on the next operand specifier
7820 is a base register specification. */
7821 assert (args[1] == 'b' || args[1] == '5'
7822 || args[1] == '-' || args[1] == '4');
7823 if (*s == '\0')
7824 return;
7825
7826 case ')': /* these must match exactly */
7827 if (*s++ == *args)
7828 continue;
7829 break;
7830
7831 case '<': /* must be at least one digit */
7832 /*
7833 * According to the manual, if the shift amount is greater
b6ff326e
KH
7834 * than 31 or less than 0, then the shift amount should be
7835 * mod 32. In reality the mips assembler issues an error.
252b5132
RH
7836 * We issue a warning and mask out all but the low 5 bits.
7837 */
7838 my_getExpression (&imm_expr, s);
7839 check_absolute_expr (ip, &imm_expr);
7840 if ((unsigned long) imm_expr.X_add_number > 31)
7841 {
793b27f4
TS
7842 as_warn (_("Improper shift amount (%lu)"),
7843 (unsigned long) imm_expr.X_add_number);
38487616 7844 imm_expr.X_add_number &= OP_MASK_SHAMT;
252b5132 7845 }
38487616 7846 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SHAMT;
252b5132
RH
7847 imm_expr.X_op = O_absent;
7848 s = expr_end;
7849 continue;
7850
7851 case '>': /* shift amount minus 32 */
7852 my_getExpression (&imm_expr, s);
7853 check_absolute_expr (ip, &imm_expr);
7854 if ((unsigned long) imm_expr.X_add_number < 32
7855 || (unsigned long) imm_expr.X_add_number > 63)
7856 break;
38487616 7857 ip->insn_opcode |= (imm_expr.X_add_number - 32) << OP_SH_SHAMT;
252b5132
RH
7858 imm_expr.X_op = O_absent;
7859 s = expr_end;
7860 continue;
7861
252b5132
RH
7862 case 'k': /* cache code */
7863 case 'h': /* prefx code */
7864 my_getExpression (&imm_expr, s);
7865 check_absolute_expr (ip, &imm_expr);
7866 if ((unsigned long) imm_expr.X_add_number > 31)
7867 {
7868 as_warn (_("Invalid value for `%s' (%lu)"),
7869 ip->insn_mo->name,
7870 (unsigned long) imm_expr.X_add_number);
7871 imm_expr.X_add_number &= 0x1f;
7872 }
7873 if (*args == 'k')
7874 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
7875 else
7876 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
7877 imm_expr.X_op = O_absent;
7878 s = expr_end;
7879 continue;
7880
7881 case 'c': /* break code */
7882 my_getExpression (&imm_expr, s);
7883 check_absolute_expr (ip, &imm_expr);
793b27f4 7884 if ((unsigned long) imm_expr.X_add_number > 1023)
252b5132 7885 {
793b27f4
TS
7886 as_warn (_("Illegal break code (%lu)"),
7887 (unsigned long) imm_expr.X_add_number);
38487616 7888 imm_expr.X_add_number &= OP_MASK_CODE;
252b5132 7889 }
38487616 7890 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE;
252b5132
RH
7891 imm_expr.X_op = O_absent;
7892 s = expr_end;
7893 continue;
7894
7895 case 'q': /* lower break code */
7896 my_getExpression (&imm_expr, s);
7897 check_absolute_expr (ip, &imm_expr);
793b27f4 7898 if ((unsigned long) imm_expr.X_add_number > 1023)
252b5132 7899 {
793b27f4
TS
7900 as_warn (_("Illegal lower break code (%lu)"),
7901 (unsigned long) imm_expr.X_add_number);
38487616 7902 imm_expr.X_add_number &= OP_MASK_CODE2;
252b5132 7903 }
38487616 7904 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE2;
252b5132
RH
7905 imm_expr.X_op = O_absent;
7906 s = expr_end;
7907 continue;
7908
4372b673 7909 case 'B': /* 20-bit syscall/break code. */
156c2f8b 7910 my_getExpression (&imm_expr, s);
156c2f8b 7911 check_absolute_expr (ip, &imm_expr);
793b27f4
TS
7912 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
7913 as_warn (_("Illegal 20-bit code (%lu)"),
7914 (unsigned long) imm_expr.X_add_number);
38487616 7915 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE20;
252b5132
RH
7916 imm_expr.X_op = O_absent;
7917 s = expr_end;
7918 continue;
7919
98d3f06f 7920 case 'C': /* Coprocessor code */
beae10d5 7921 my_getExpression (&imm_expr, s);
252b5132 7922 check_absolute_expr (ip, &imm_expr);
98d3f06f 7923 if ((unsigned long) imm_expr.X_add_number >= (1 << 25))
252b5132 7924 {
793b27f4
TS
7925 as_warn (_("Coproccesor code > 25 bits (%lu)"),
7926 (unsigned long) imm_expr.X_add_number);
98d3f06f 7927 imm_expr.X_add_number &= ((1 << 25) - 1);
252b5132 7928 }
beae10d5
KH
7929 ip->insn_opcode |= imm_expr.X_add_number;
7930 imm_expr.X_op = O_absent;
7931 s = expr_end;
7932 continue;
252b5132 7933
4372b673
NC
7934 case 'J': /* 19-bit wait code. */
7935 my_getExpression (&imm_expr, s);
7936 check_absolute_expr (ip, &imm_expr);
793b27f4
TS
7937 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
7938 as_warn (_("Illegal 19-bit code (%lu)"),
7939 (unsigned long) imm_expr.X_add_number);
38487616 7940 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE19;
4372b673
NC
7941 imm_expr.X_op = O_absent;
7942 s = expr_end;
7943 continue;
7944
252b5132 7945 case 'P': /* Performance register */
beae10d5 7946 my_getExpression (&imm_expr, s);
252b5132 7947 check_absolute_expr (ip, &imm_expr);
beae10d5 7948 if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
252b5132 7949 {
793b27f4
TS
7950 as_warn (_("Invalid performance register (%lu)"),
7951 (unsigned long) imm_expr.X_add_number);
38487616 7952 imm_expr.X_add_number &= OP_MASK_PERFREG;
252b5132 7953 }
38487616 7954 ip->insn_opcode |= (imm_expr.X_add_number << OP_SH_PERFREG);
beae10d5
KH
7955 imm_expr.X_op = O_absent;
7956 s = expr_end;
7957 continue;
252b5132
RH
7958
7959 case 'b': /* base register */
7960 case 'd': /* destination register */
7961 case 's': /* source register */
7962 case 't': /* target register */
7963 case 'r': /* both target and source */
7964 case 'v': /* both dest and source */
7965 case 'w': /* both dest and target */
7966 case 'E': /* coprocessor target register */
7967 case 'G': /* coprocessor destination register */
7968 case 'x': /* ignore register name */
7969 case 'z': /* must be zero register */
4372b673 7970 case 'U': /* destination register (clo/clz). */
252b5132
RH
7971 s_reset = s;
7972 if (s[0] == '$')
7973 {
7974
3882b010 7975 if (ISDIGIT (s[1]))
252b5132
RH
7976 {
7977 ++s;
7978 regno = 0;
7979 do
7980 {
7981 regno *= 10;
7982 regno += *s - '0';
7983 ++s;
7984 }
3882b010 7985 while (ISDIGIT (*s));
252b5132
RH
7986 if (regno > 31)
7987 as_bad (_("Invalid register number (%d)"), regno);
7988 }
7989 else if (*args == 'E' || *args == 'G')
7990 goto notreg;
7991 else
7992 {
76db943d
TS
7993 if (s[1] == 'r' && s[2] == 'a')
7994 {
7995 s += 3;
7996 regno = RA;
7997 }
7998 else if (s[1] == 'f' && s[2] == 'p')
252b5132
RH
7999 {
8000 s += 3;
8001 regno = FP;
8002 }
8003 else if (s[1] == 's' && s[2] == 'p')
8004 {
8005 s += 3;
8006 regno = SP;
8007 }
8008 else if (s[1] == 'g' && s[2] == 'p')
8009 {
8010 s += 3;
8011 regno = GP;
8012 }
8013 else if (s[1] == 'a' && s[2] == 't')
8014 {
8015 s += 3;
8016 regno = AT;
8017 }
8018 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8019 {
8020 s += 4;
8021 regno = KT0;
8022 }
8023 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8024 {
8025 s += 4;
8026 regno = KT1;
8027 }
85b51719
TS
8028 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
8029 {
8030 s += 5;
8031 regno = ZERO;
8032 }
252b5132
RH
8033 else if (itbl_have_entries)
8034 {
8035 char *p, *n;
d7ba4a77 8036 unsigned long r;
252b5132 8037
d7ba4a77 8038 p = s + 1; /* advance past '$' */
252b5132
RH
8039 n = itbl_get_field (&p); /* n is name */
8040
d7ba4a77
ILT
8041 /* See if this is a register defined in an
8042 itbl entry. */
8043 if (itbl_get_reg_val (n, &r))
252b5132
RH
8044 {
8045 /* Get_field advances to the start of
8046 the next field, so we need to back
d7ba4a77 8047 rack to the end of the last field. */
bdaaa2e1 8048 if (p)
252b5132 8049 s = p - 1;
bdaaa2e1 8050 else
d7ba4a77 8051 s = strchr (s, '\0');
252b5132
RH
8052 regno = r;
8053 }
8054 else
8055 goto notreg;
beae10d5 8056 }
252b5132
RH
8057 else
8058 goto notreg;
8059 }
8060 if (regno == AT
8061 && ! mips_opts.noat
8062 && *args != 'E'
8063 && *args != 'G')
8064 as_warn (_("Used $at without \".set noat\""));
8065 c = *args;
8066 if (*s == ' ')
f9419b05 8067 ++s;
252b5132
RH
8068 if (args[1] != *s)
8069 {
8070 if (c == 'r' || c == 'v' || c == 'w')
8071 {
8072 regno = lastregno;
8073 s = s_reset;
f9419b05 8074 ++args;
252b5132
RH
8075 }
8076 }
8077 /* 'z' only matches $0. */
8078 if (c == 'z' && regno != 0)
8079 break;
8080
bdaaa2e1
KH
8081 /* Now that we have assembled one operand, we use the args string
8082 * to figure out where it goes in the instruction. */
252b5132
RH
8083 switch (c)
8084 {
8085 case 'r':
8086 case 's':
8087 case 'v':
8088 case 'b':
38487616 8089 ip->insn_opcode |= regno << OP_SH_RS;
252b5132
RH
8090 break;
8091 case 'd':
8092 case 'G':
38487616 8093 ip->insn_opcode |= regno << OP_SH_RD;
252b5132 8094 break;
4372b673 8095 case 'U':
38487616
TS
8096 ip->insn_opcode |= regno << OP_SH_RD;
8097 ip->insn_opcode |= regno << OP_SH_RT;
4372b673 8098 break;
252b5132
RH
8099 case 'w':
8100 case 't':
8101 case 'E':
38487616 8102 ip->insn_opcode |= regno << OP_SH_RT;
252b5132
RH
8103 break;
8104 case 'x':
8105 /* This case exists because on the r3000 trunc
8106 expands into a macro which requires a gp
8107 register. On the r6000 or r4000 it is
8108 assembled into a single instruction which
8109 ignores the register. Thus the insn version
8110 is MIPS_ISA2 and uses 'x', and the macro
8111 version is MIPS_ISA1 and uses 't'. */
8112 break;
8113 case 'z':
8114 /* This case is for the div instruction, which
8115 acts differently if the destination argument
8116 is $0. This only matches $0, and is checked
8117 outside the switch. */
8118 break;
8119 case 'D':
8120 /* Itbl operand; not yet implemented. FIXME ?? */
8121 break;
8122 /* What about all other operands like 'i', which
8123 can be specified in the opcode table? */
8124 }
8125 lastregno = regno;
8126 continue;
8127 }
8128 notreg:
8129 switch (*args++)
8130 {
8131 case 'r':
8132 case 'v':
38487616 8133 ip->insn_opcode |= lastregno << OP_SH_RS;
252b5132
RH
8134 continue;
8135 case 'w':
38487616 8136 ip->insn_opcode |= lastregno << OP_SH_RT;
252b5132
RH
8137 continue;
8138 }
8139 break;
8140
deec1734
CD
8141 case 'O': /* MDMX alignment immediate constant. */
8142 my_getExpression (&imm_expr, s);
8143 check_absolute_expr (ip, &imm_expr);
8144 if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
8145 {
8146 as_warn ("Improper align amount (%ld), using low bits",
8147 (long) imm_expr.X_add_number);
8148 imm_expr.X_add_number &= OP_MASK_ALN;
8149 }
8150 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_ALN;
8151 imm_expr.X_op = O_absent;
8152 s = expr_end;
8153 continue;
8154
8155 case 'Q': /* MDMX vector, element sel, or const. */
8156 if (s[0] != '$')
8157 {
8158 /* MDMX Immediate. */
8159 my_getExpression (&imm_expr, s);
8160 check_absolute_expr (ip, &imm_expr);
8161 if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
8162 {
8163 as_warn (_("Invalid MDMX Immediate (%ld)"),
8164 (long) imm_expr.X_add_number);
8165 imm_expr.X_add_number &= OP_MASK_FT;
8166 }
8167 imm_expr.X_add_number &= OP_MASK_FT;
8168 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8169 ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
8170 else
8171 ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
8172 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_FT;
8173 imm_expr.X_op = O_absent;
8174 s = expr_end;
8175 continue;
8176 }
8177 /* Not MDMX Immediate. Fall through. */
8178 case 'X': /* MDMX destination register. */
8179 case 'Y': /* MDMX source register. */
8180 case 'Z': /* MDMX target register. */
8181 is_mdmx = 1;
252b5132
RH
8182 case 'D': /* floating point destination register */
8183 case 'S': /* floating point source register */
8184 case 'T': /* floating point target register */
8185 case 'R': /* floating point source register */
8186 case 'V':
8187 case 'W':
8188 s_reset = s;
deec1734
CD
8189 /* Accept $fN for FP and MDMX register numbers, and in
8190 addition accept $vN for MDMX register numbers. */
8191 if ((s[0] == '$' && s[1] == 'f' && ISDIGIT (s[2]))
8192 || (is_mdmx != 0 && s[0] == '$' && s[1] == 'v'
8193 && ISDIGIT (s[2])))
252b5132
RH
8194 {
8195 s += 2;
8196 regno = 0;
8197 do
8198 {
8199 regno *= 10;
8200 regno += *s - '0';
8201 ++s;
8202 }
3882b010 8203 while (ISDIGIT (*s));
252b5132
RH
8204
8205 if (regno > 31)
8206 as_bad (_("Invalid float register number (%d)"), regno);
8207
8208 if ((regno & 1) != 0
ca4e0257 8209 && HAVE_32BIT_FPRS
252b5132
RH
8210 && ! (strcmp (str, "mtc1") == 0
8211 || strcmp (str, "mfc1") == 0
8212 || strcmp (str, "lwc1") == 0
8213 || strcmp (str, "swc1") == 0
8214 || strcmp (str, "l.s") == 0
8215 || strcmp (str, "s.s") == 0))
8216 as_warn (_("Float register should be even, was %d"),
8217 regno);
8218
8219 c = *args;
8220 if (*s == ' ')
f9419b05 8221 ++s;
252b5132
RH
8222 if (args[1] != *s)
8223 {
8224 if (c == 'V' || c == 'W')
8225 {
8226 regno = lastregno;
8227 s = s_reset;
f9419b05 8228 ++args;
252b5132
RH
8229 }
8230 }
8231 switch (c)
8232 {
8233 case 'D':
deec1734 8234 case 'X':
38487616 8235 ip->insn_opcode |= regno << OP_SH_FD;
252b5132
RH
8236 break;
8237 case 'V':
8238 case 'S':
deec1734 8239 case 'Y':
38487616 8240 ip->insn_opcode |= regno << OP_SH_FS;
252b5132 8241 break;
deec1734
CD
8242 case 'Q':
8243 /* This is like 'Z', but also needs to fix the MDMX
8244 vector/scalar select bits. Note that the
8245 scalar immediate case is handled above. */
8246 if (*s == '[')
8247 {
8248 int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
8249 int max_el = (is_qh ? 3 : 7);
8250 s++;
8251 my_getExpression(&imm_expr, s);
8252 check_absolute_expr (ip, &imm_expr);
8253 s = expr_end;
8254 if (imm_expr.X_add_number > max_el)
8255 as_bad(_("Bad element selector %ld"),
8256 (long) imm_expr.X_add_number);
8257 imm_expr.X_add_number &= max_el;
8258 ip->insn_opcode |= (imm_expr.X_add_number
8259 << (OP_SH_VSEL +
8260 (is_qh ? 2 : 1)));
8261 if (*s != ']')
8262 as_warn(_("Expecting ']' found '%s'"), s);
8263 else
8264 s++;
8265 }
8266 else
8267 {
8268 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8269 ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
8270 << OP_SH_VSEL);
8271 else
8272 ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
8273 OP_SH_VSEL);
8274 }
8275 /* Fall through */
252b5132
RH
8276 case 'W':
8277 case 'T':
deec1734 8278 case 'Z':
38487616 8279 ip->insn_opcode |= regno << OP_SH_FT;
252b5132
RH
8280 break;
8281 case 'R':
38487616 8282 ip->insn_opcode |= regno << OP_SH_FR;
252b5132
RH
8283 break;
8284 }
8285 lastregno = regno;
8286 continue;
8287 }
8288
252b5132
RH
8289 switch (*args++)
8290 {
8291 case 'V':
38487616 8292 ip->insn_opcode |= lastregno << OP_SH_FS;
252b5132
RH
8293 continue;
8294 case 'W':
38487616 8295 ip->insn_opcode |= lastregno << OP_SH_FT;
252b5132
RH
8296 continue;
8297 }
8298 break;
8299
8300 case 'I':
8301 my_getExpression (&imm_expr, s);
8302 if (imm_expr.X_op != O_big
8303 && imm_expr.X_op != O_constant)
8304 insn_error = _("absolute expression required");
8305 s = expr_end;
8306 continue;
8307
8308 case 'A':
8309 my_getExpression (&offset_expr, s);
f6688943 8310 *imm_reloc = BFD_RELOC_32;
252b5132
RH
8311 s = expr_end;
8312 continue;
8313
8314 case 'F':
8315 case 'L':
8316 case 'f':
8317 case 'l':
8318 {
8319 int f64;
ca4e0257 8320 int using_gprs;
252b5132
RH
8321 char *save_in;
8322 char *err;
8323 unsigned char temp[8];
8324 int len;
8325 unsigned int length;
8326 segT seg;
8327 subsegT subseg;
8328 char *p;
8329
8330 /* These only appear as the last operand in an
8331 instruction, and every instruction that accepts
8332 them in any variant accepts them in all variants.
8333 This means we don't have to worry about backing out
8334 any changes if the instruction does not match.
8335
8336 The difference between them is the size of the
8337 floating point constant and where it goes. For 'F'
8338 and 'L' the constant is 64 bits; for 'f' and 'l' it
8339 is 32 bits. Where the constant is placed is based
8340 on how the MIPS assembler does things:
8341 F -- .rdata
8342 L -- .lit8
8343 f -- immediate value
8344 l -- .lit4
8345
8346 The .lit4 and .lit8 sections are only used if
8347 permitted by the -G argument.
8348
8349 When generating embedded PIC code, we use the
8350 .lit8 section but not the .lit4 section (we can do
8351 .lit4 inline easily; we need to put .lit8
8352 somewhere in the data segment, and using .lit8
8353 permits the linker to eventually combine identical
ca4e0257
RS
8354 .lit8 entries).
8355
8356 The code below needs to know whether the target register
8357 is 32 or 64 bits wide. It relies on the fact 'f' and
8358 'F' are used with GPR-based instructions and 'l' and
8359 'L' are used with FPR-based instructions. */
252b5132
RH
8360
8361 f64 = *args == 'F' || *args == 'L';
ca4e0257 8362 using_gprs = *args == 'F' || *args == 'f';
252b5132
RH
8363
8364 save_in = input_line_pointer;
8365 input_line_pointer = s;
8366 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
8367 length = len;
8368 s = input_line_pointer;
8369 input_line_pointer = save_in;
8370 if (err != NULL && *err != '\0')
8371 {
8372 as_bad (_("Bad floating point constant: %s"), err);
8373 memset (temp, '\0', sizeof temp);
8374 length = f64 ? 8 : 4;
8375 }
8376
156c2f8b 8377 assert (length == (unsigned) (f64 ? 8 : 4));
252b5132
RH
8378
8379 if (*args == 'f'
8380 || (*args == 'l'
8381 && (! USE_GLOBAL_POINTER_OPT
8382 || mips_pic == EMBEDDED_PIC
8383 || g_switch_value < 4
8384 || (temp[0] == 0 && temp[1] == 0)
8385 || (temp[2] == 0 && temp[3] == 0))))
8386 {
8387 imm_expr.X_op = O_constant;
8388 if (! target_big_endian)
8389 imm_expr.X_add_number = bfd_getl32 (temp);
8390 else
8391 imm_expr.X_add_number = bfd_getb32 (temp);
8392 }
8393 else if (length > 4
119d663a 8394 && ! mips_disable_float_construction
ca4e0257
RS
8395 /* Constants can only be constructed in GPRs and
8396 copied to FPRs if the GPRs are at least as wide
8397 as the FPRs. Force the constant into memory if
8398 we are using 64-bit FPRs but the GPRs are only
8399 32 bits wide. */
8400 && (using_gprs
8401 || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
252b5132
RH
8402 && ((temp[0] == 0 && temp[1] == 0)
8403 || (temp[2] == 0 && temp[3] == 0))
8404 && ((temp[4] == 0 && temp[5] == 0)
8405 || (temp[6] == 0 && temp[7] == 0)))
8406 {
ca4e0257
RS
8407 /* The value is simple enough to load with a couple of
8408 instructions. If using 32-bit registers, set
8409 imm_expr to the high order 32 bits and offset_expr to
8410 the low order 32 bits. Otherwise, set imm_expr to
8411 the entire 64 bit constant. */
8412 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
252b5132
RH
8413 {
8414 imm_expr.X_op = O_constant;
8415 offset_expr.X_op = O_constant;
8416 if (! target_big_endian)
8417 {
8418 imm_expr.X_add_number = bfd_getl32 (temp + 4);
8419 offset_expr.X_add_number = bfd_getl32 (temp);
8420 }
8421 else
8422 {
8423 imm_expr.X_add_number = bfd_getb32 (temp);
8424 offset_expr.X_add_number = bfd_getb32 (temp + 4);
8425 }
8426 if (offset_expr.X_add_number == 0)
8427 offset_expr.X_op = O_absent;
8428 }
8429 else if (sizeof (imm_expr.X_add_number) > 4)
8430 {
8431 imm_expr.X_op = O_constant;
8432 if (! target_big_endian)
8433 imm_expr.X_add_number = bfd_getl64 (temp);
8434 else
8435 imm_expr.X_add_number = bfd_getb64 (temp);
8436 }
8437 else
8438 {
8439 imm_expr.X_op = O_big;
8440 imm_expr.X_add_number = 4;
8441 if (! target_big_endian)
8442 {
8443 generic_bignum[0] = bfd_getl16 (temp);
8444 generic_bignum[1] = bfd_getl16 (temp + 2);
8445 generic_bignum[2] = bfd_getl16 (temp + 4);
8446 generic_bignum[3] = bfd_getl16 (temp + 6);
8447 }
8448 else
8449 {
8450 generic_bignum[0] = bfd_getb16 (temp + 6);
8451 generic_bignum[1] = bfd_getb16 (temp + 4);
8452 generic_bignum[2] = bfd_getb16 (temp + 2);
8453 generic_bignum[3] = bfd_getb16 (temp);
8454 }
8455 }
8456 }
8457 else
8458 {
8459 const char *newname;
8460 segT new_seg;
8461
8462 /* Switch to the right section. */
8463 seg = now_seg;
8464 subseg = now_subseg;
8465 switch (*args)
8466 {
8467 default: /* unused default case avoids warnings. */
8468 case 'L':
8469 newname = RDATA_SECTION_NAME;
bb2d6cd7
GK
8470 if ((USE_GLOBAL_POINTER_OPT && g_switch_value >= 8)
8471 || mips_pic == EMBEDDED_PIC)
252b5132
RH
8472 newname = ".lit8";
8473 break;
8474 case 'F':
bb2d6cd7
GK
8475 if (mips_pic == EMBEDDED_PIC)
8476 newname = ".lit8";
8477 else
8478 newname = RDATA_SECTION_NAME;
252b5132
RH
8479 break;
8480 case 'l':
8481 assert (!USE_GLOBAL_POINTER_OPT
8482 || g_switch_value >= 4);
8483 newname = ".lit4";
8484 break;
8485 }
8486 new_seg = subseg_new (newname, (subsegT) 0);
8487 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
8488 bfd_set_section_flags (stdoutput, new_seg,
8489 (SEC_ALLOC
8490 | SEC_LOAD
8491 | SEC_READONLY
8492 | SEC_DATA));
8493 frag_align (*args == 'l' ? 2 : 3, 0, 0);
8494 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
8495 && strcmp (TARGET_OS, "elf") != 0)
8496 record_alignment (new_seg, 4);
8497 else
8498 record_alignment (new_seg, *args == 'l' ? 2 : 3);
8499 if (seg == now_seg)
8500 as_bad (_("Can't use floating point insn in this section"));
8501
8502 /* Set the argument to the current address in the
8503 section. */
8504 offset_expr.X_op = O_symbol;
8505 offset_expr.X_add_symbol =
8506 symbol_new ("L0\001", now_seg,
8507 (valueT) frag_now_fix (), frag_now);
8508 offset_expr.X_add_number = 0;
8509
8510 /* Put the floating point number into the section. */
8511 p = frag_more ((int) length);
8512 memcpy (p, temp, length);
8513
8514 /* Switch back to the original section. */
8515 subseg_set (seg, subseg);
8516 }
8517 }
8518 continue;
8519
8520 case 'i': /* 16 bit unsigned immediate */
8521 case 'j': /* 16 bit signed immediate */
f6688943 8522 *imm_reloc = BFD_RELOC_LO16;
252b5132 8523 c = my_getSmallExpression (&imm_expr, s);
fb1b3232 8524 if (c != S_EX_NONE)
252b5132 8525 {
fb1b3232 8526 if (c != S_EX_LO)
252b5132 8527 {
fdb987ee
RS
8528 if (c == S_EX_HI)
8529 {
8530 *imm_reloc = BFD_RELOC_HI16_S;
8531 imm_unmatched_hi = true;
8532 }
ad8d3bb3 8533#ifdef OBJ_ELF
fb1b3232 8534 else if (c == S_EX_HIGHEST)
98d3f06f 8535 *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
fb1b3232 8536 else if (c == S_EX_HIGHER)
98d3f06f 8537 *imm_reloc = BFD_RELOC_MIPS_HIGHER;
ad8d3bb3
TS
8538 else if (c == S_EX_GP_REL)
8539 {
8540 /* This occurs in NewABI only. */
8541 c = my_getSmallExpression (&imm_expr, s);
8542 if (c != S_EX_NEG)
8543 as_bad (_("bad composition of relocations"));
8544 else
8545 {
8546 c = my_getSmallExpression (&imm_expr, s);
8547 if (c != S_EX_LO)
8548 as_bad (_("bad composition of relocations"));
8549 else
8550 {
8551 imm_reloc[0] = BFD_RELOC_GPREL16;
8552 imm_reloc[1] = BFD_RELOC_MIPS_SUB;
8553 imm_reloc[2] = BFD_RELOC_LO16;
8554 }
8555 }
8556 }
8557#endif
252b5132 8558 else
f6688943 8559 *imm_reloc = BFD_RELOC_HI16;
252b5132
RH
8560 }
8561 else if (imm_expr.X_op == O_constant)
8562 imm_expr.X_add_number &= 0xffff;
8563 }
8564 if (*args == 'i')
8565 {
fb1b3232 8566 if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
252b5132 8567 || ((imm_expr.X_add_number < 0
beae10d5
KH
8568 || imm_expr.X_add_number >= 0x10000)
8569 && imm_expr.X_op == O_constant))
252b5132
RH
8570 {
8571 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8572 !strcmp (insn->name, insn[1].name))
8573 break;
2ae7e77b
AH
8574 if (imm_expr.X_op == O_constant
8575 || imm_expr.X_op == O_big)
252b5132
RH
8576 as_bad (_("16 bit expression not in range 0..65535"));
8577 }
8578 }
8579 else
8580 {
8581 int more;
8582 offsetT max;
8583
8584 /* The upper bound should be 0x8000, but
8585 unfortunately the MIPS assembler accepts numbers
8586 from 0x8000 to 0xffff and sign extends them, and
8587 we want to be compatible. We only permit this
8588 extended range for an instruction which does not
8589 provide any further alternates, since those
8590 alternates may handle other cases. People should
8591 use the numbers they mean, rather than relying on
8592 a mysterious sign extension. */
8593 more = (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8594 strcmp (insn->name, insn[1].name) == 0);
8595 if (more)
8596 max = 0x8000;
8597 else
8598 max = 0x10000;
fb1b3232 8599 if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
252b5132 8600 || ((imm_expr.X_add_number < -0x8000
beae10d5
KH
8601 || imm_expr.X_add_number >= max)
8602 && imm_expr.X_op == O_constant)
252b5132
RH
8603 || (more
8604 && imm_expr.X_add_number < 0
ca4e0257 8605 && HAVE_64BIT_GPRS
252b5132
RH
8606 && imm_expr.X_unsigned
8607 && sizeof (imm_expr.X_add_number) <= 4))
8608 {
8609 if (more)
8610 break;
2ae7e77b
AH
8611 if (imm_expr.X_op == O_constant
8612 || imm_expr.X_op == O_big)
252b5132
RH
8613 as_bad (_("16 bit expression not in range -32768..32767"));
8614 }
8615 }
8616 s = expr_end;
8617 continue;
8618
8619 case 'o': /* 16 bit offset */
8620 c = my_getSmallExpression (&offset_expr, s);
8621
8622 /* If this value won't fit into a 16 bit offset, then go
8623 find a macro that will generate the 32 bit offset
afdbd6d0 8624 code pattern. */
fb1b3232 8625 if (c == S_EX_NONE
252b5132
RH
8626 && (offset_expr.X_op != O_constant
8627 || offset_expr.X_add_number >= 0x8000
afdbd6d0 8628 || offset_expr.X_add_number < -0x8000))
252b5132
RH
8629 break;
8630
fb1b3232 8631 if (c == S_EX_HI)
252b5132
RH
8632 {
8633 if (offset_expr.X_op != O_constant)
8634 break;
8635 offset_expr.X_add_number =
8636 (offset_expr.X_add_number >> 16) & 0xffff;
8637 }
f6688943 8638 *offset_reloc = BFD_RELOC_LO16;
252b5132
RH
8639 s = expr_end;
8640 continue;
8641
8642 case 'p': /* pc relative offset */
cb56d3d3 8643 if (mips_pic == EMBEDDED_PIC)
f6688943 8644 *offset_reloc = BFD_RELOC_16_PCREL_S2;
cb56d3d3 8645 else
f6688943 8646 *offset_reloc = BFD_RELOC_16_PCREL;
252b5132
RH
8647 my_getExpression (&offset_expr, s);
8648 s = expr_end;
8649 continue;
8650
8651 case 'u': /* upper 16 bits */
8652 c = my_getSmallExpression (&imm_expr, s);
f6688943 8653 *imm_reloc = BFD_RELOC_LO16;
e7d556df 8654 if (c != S_EX_NONE)
252b5132 8655 {
fb1b3232 8656 if (c != S_EX_LO)
252b5132 8657 {
fdb987ee 8658 if (c == S_EX_HI)
252b5132 8659 {
f6688943 8660 *imm_reloc = BFD_RELOC_HI16_S;
252b5132
RH
8661 imm_unmatched_hi = true;
8662 }
645dc66c
TS
8663#ifdef OBJ_ELF
8664 else if (c == S_EX_HIGHEST)
98d3f06f 8665 *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
ad8d3bb3
TS
8666 else if (c == S_EX_GP_REL)
8667 {
8668 /* This occurs in NewABI only. */
8669 c = my_getSmallExpression (&imm_expr, s);
8670 if (c != S_EX_NEG)
8671 as_bad (_("bad composition of relocations"));
8672 else
8673 {
8674 c = my_getSmallExpression (&imm_expr, s);
8675 if (c != S_EX_HI)
8676 as_bad (_("bad composition of relocations"));
8677 else
8678 {
8679 imm_reloc[0] = BFD_RELOC_GPREL16;
8680 imm_reloc[1] = BFD_RELOC_MIPS_SUB;
8681 imm_reloc[2] = BFD_RELOC_HI16_S;
8682 }
8683 }
8684 }
8685#endif
252b5132 8686 else
f6688943 8687 *imm_reloc = BFD_RELOC_HI16;
252b5132
RH
8688 }
8689 else if (imm_expr.X_op == O_constant)
8690 imm_expr.X_add_number &= 0xffff;
8691 }
fdb987ee
RS
8692 else if (imm_expr.X_op == O_constant
8693 && (imm_expr.X_add_number < 0
8694 || imm_expr.X_add_number >= 0x10000))
252b5132
RH
8695 as_bad (_("lui expression not in range 0..65535"));
8696 s = expr_end;
8697 continue;
8698
8699 case 'a': /* 26 bit address */
8700 my_getExpression (&offset_expr, s);
8701 s = expr_end;
f6688943 8702 *offset_reloc = BFD_RELOC_MIPS_JMP;
252b5132
RH
8703 continue;
8704
8705 case 'N': /* 3 bit branch condition code */
8706 case 'M': /* 3 bit compare condition code */
8707 if (strncmp (s, "$fcc", 4) != 0)
8708 break;
8709 s += 4;
8710 regno = 0;
8711 do
8712 {
8713 regno *= 10;
8714 regno += *s - '0';
8715 ++s;
8716 }
3882b010 8717 while (ISDIGIT (*s));
252b5132
RH
8718 if (regno > 7)
8719 as_bad (_("invalid condition code register $fcc%d"), regno);
8720 if (*args == 'N')
8721 ip->insn_opcode |= regno << OP_SH_BCC;
8722 else
8723 ip->insn_opcode |= regno << OP_SH_CCC;
beae10d5 8724 continue;
252b5132 8725
156c2f8b
NC
8726 case 'H':
8727 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
8728 s += 2;
3882b010 8729 if (ISDIGIT (*s))
156c2f8b
NC
8730 {
8731 c = 0;
8732 do
8733 {
8734 c *= 10;
8735 c += *s - '0';
8736 ++s;
8737 }
3882b010 8738 while (ISDIGIT (*s));
156c2f8b
NC
8739 }
8740 else
8741 c = 8; /* Invalid sel value. */
8742
8743 if (c > 7)
8744 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
8745 ip->insn_opcode |= c;
8746 continue;
8747
252b5132
RH
8748 default:
8749 as_bad (_("bad char = '%c'\n"), *args);
8750 internalError ();
8751 }
8752 break;
8753 }
8754 /* Args don't match. */
8755 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8756 !strcmp (insn->name, insn[1].name))
8757 {
8758 ++insn;
8759 s = argsStart;
268f6bed 8760 insn_error = _("illegal operands");
252b5132
RH
8761 continue;
8762 }
268f6bed
L
8763 if (save_c)
8764 *(--s) = save_c;
252b5132
RH
8765 insn_error = _("illegal operands");
8766 return;
8767 }
8768}
8769
8770/* This routine assembles an instruction into its binary format when
8771 assembling for the mips16. As a side effect, it sets one of the
8772 global variables imm_reloc or offset_reloc to the type of
8773 relocation to do if one of the operands is an address expression.
8774 It also sets mips16_small and mips16_ext if the user explicitly
8775 requested a small or extended instruction. */
8776
8777static void
8778mips16_ip (str, ip)
8779 char *str;
8780 struct mips_cl_insn *ip;
8781{
8782 char *s;
8783 const char *args;
8784 struct mips_opcode *insn;
8785 char *argsstart;
8786 unsigned int regno;
8787 unsigned int lastregno = 0;
8788 char *s_reset;
8789
8790 insn_error = NULL;
8791
8792 mips16_small = false;
8793 mips16_ext = false;
8794
3882b010 8795 for (s = str; ISLOWER (*s); ++s)
252b5132
RH
8796 ;
8797 switch (*s)
8798 {
8799 case '\0':
8800 break;
8801
8802 case ' ':
8803 *s++ = '\0';
8804 break;
8805
8806 case '.':
8807 if (s[1] == 't' && s[2] == ' ')
8808 {
8809 *s = '\0';
8810 mips16_small = true;
8811 s += 3;
8812 break;
8813 }
8814 else if (s[1] == 'e' && s[2] == ' ')
8815 {
8816 *s = '\0';
8817 mips16_ext = true;
8818 s += 3;
8819 break;
8820 }
8821 /* Fall through. */
8822 default:
8823 insn_error = _("unknown opcode");
8824 return;
8825 }
8826
8827 if (mips_opts.noautoextend && ! mips16_ext)
8828 mips16_small = true;
8829
8830 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
8831 {
8832 insn_error = _("unrecognized opcode");
8833 return;
8834 }
8835
8836 argsstart = s;
8837 for (;;)
8838 {
8839 assert (strcmp (insn->name, str) == 0);
8840
8841 ip->insn_mo = insn;
8842 ip->insn_opcode = insn->match;
8843 ip->use_extend = false;
8844 imm_expr.X_op = O_absent;
f6688943
TS
8845 imm_reloc[0] = BFD_RELOC_UNUSED;
8846 imm_reloc[1] = BFD_RELOC_UNUSED;
8847 imm_reloc[2] = BFD_RELOC_UNUSED;
252b5132 8848 offset_expr.X_op = O_absent;
f6688943
TS
8849 offset_reloc[0] = BFD_RELOC_UNUSED;
8850 offset_reloc[1] = BFD_RELOC_UNUSED;
8851 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
8852 for (args = insn->args; 1; ++args)
8853 {
8854 int c;
8855
8856 if (*s == ' ')
8857 ++s;
8858
8859 /* In this switch statement we call break if we did not find
8860 a match, continue if we did find a match, or return if we
8861 are done. */
8862
8863 c = *args;
8864 switch (c)
8865 {
8866 case '\0':
8867 if (*s == '\0')
8868 {
8869 /* Stuff the immediate value in now, if we can. */
8870 if (imm_expr.X_op == O_constant
f6688943 8871 && *imm_reloc > BFD_RELOC_UNUSED
252b5132
RH
8872 && insn->pinfo != INSN_MACRO)
8873 {
c4e7957c 8874 mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
252b5132
RH
8875 imm_expr.X_add_number, true, mips16_small,
8876 mips16_ext, &ip->insn_opcode,
8877 &ip->use_extend, &ip->extend);
8878 imm_expr.X_op = O_absent;
f6688943 8879 *imm_reloc = BFD_RELOC_UNUSED;
252b5132
RH
8880 }
8881
8882 return;
8883 }
8884 break;
8885
8886 case ',':
8887 if (*s++ == c)
8888 continue;
8889 s--;
8890 switch (*++args)
8891 {
8892 case 'v':
8893 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
8894 continue;
8895 case 'w':
8896 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
8897 continue;
8898 }
8899 break;
8900
8901 case '(':
8902 case ')':
8903 if (*s++ == c)
8904 continue;
8905 break;
8906
8907 case 'v':
8908 case 'w':
8909 if (s[0] != '$')
8910 {
8911 if (c == 'v')
8912 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
8913 else
8914 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
8915 ++args;
8916 continue;
8917 }
8918 /* Fall through. */
8919 case 'x':
8920 case 'y':
8921 case 'z':
8922 case 'Z':
8923 case '0':
8924 case 'S':
8925 case 'R':
8926 case 'X':
8927 case 'Y':
8928 if (s[0] != '$')
8929 break;
8930 s_reset = s;
3882b010 8931 if (ISDIGIT (s[1]))
252b5132
RH
8932 {
8933 ++s;
8934 regno = 0;
8935 do
8936 {
8937 regno *= 10;
8938 regno += *s - '0';
8939 ++s;
8940 }
3882b010 8941 while (ISDIGIT (*s));
252b5132
RH
8942 if (regno > 31)
8943 {
8944 as_bad (_("invalid register number (%d)"), regno);
8945 regno = 2;
8946 }
8947 }
8948 else
8949 {
76db943d
TS
8950 if (s[1] == 'r' && s[2] == 'a')
8951 {
8952 s += 3;
8953 regno = RA;
8954 }
8955 else if (s[1] == 'f' && s[2] == 'p')
252b5132
RH
8956 {
8957 s += 3;
8958 regno = FP;
8959 }
8960 else if (s[1] == 's' && s[2] == 'p')
8961 {
8962 s += 3;
8963 regno = SP;
8964 }
8965 else if (s[1] == 'g' && s[2] == 'p')
8966 {
8967 s += 3;
8968 regno = GP;
8969 }
8970 else if (s[1] == 'a' && s[2] == 't')
8971 {
8972 s += 3;
8973 regno = AT;
8974 }
8975 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8976 {
8977 s += 4;
8978 regno = KT0;
8979 }
8980 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8981 {
8982 s += 4;
8983 regno = KT1;
8984 }
85b51719
TS
8985 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
8986 {
8987 s += 5;
8988 regno = ZERO;
8989 }
252b5132
RH
8990 else
8991 break;
8992 }
8993
8994 if (*s == ' ')
8995 ++s;
8996 if (args[1] != *s)
8997 {
8998 if (c == 'v' || c == 'w')
8999 {
9000 regno = mips16_to_32_reg_map[lastregno];
9001 s = s_reset;
f9419b05 9002 ++args;
252b5132
RH
9003 }
9004 }
9005
9006 switch (c)
9007 {
9008 case 'x':
9009 case 'y':
9010 case 'z':
9011 case 'v':
9012 case 'w':
9013 case 'Z':
9014 regno = mips32_to_16_reg_map[regno];
9015 break;
9016
9017 case '0':
9018 if (regno != 0)
9019 regno = ILLEGAL_REG;
9020 break;
9021
9022 case 'S':
9023 if (regno != SP)
9024 regno = ILLEGAL_REG;
9025 break;
9026
9027 case 'R':
9028 if (regno != RA)
9029 regno = ILLEGAL_REG;
9030 break;
9031
9032 case 'X':
9033 case 'Y':
9034 if (regno == AT && ! mips_opts.noat)
9035 as_warn (_("used $at without \".set noat\""));
9036 break;
9037
9038 default:
9039 internalError ();
9040 }
9041
9042 if (regno == ILLEGAL_REG)
9043 break;
9044
9045 switch (c)
9046 {
9047 case 'x':
9048 case 'v':
9049 ip->insn_opcode |= regno << MIPS16OP_SH_RX;
9050 break;
9051 case 'y':
9052 case 'w':
9053 ip->insn_opcode |= regno << MIPS16OP_SH_RY;
9054 break;
9055 case 'z':
9056 ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
9057 break;
9058 case 'Z':
9059 ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
9060 case '0':
9061 case 'S':
9062 case 'R':
9063 break;
9064 case 'X':
9065 ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
9066 break;
9067 case 'Y':
9068 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
9069 ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
9070 break;
9071 default:
9072 internalError ();
9073 }
9074
9075 lastregno = regno;
9076 continue;
9077
9078 case 'P':
9079 if (strncmp (s, "$pc", 3) == 0)
9080 {
9081 s += 3;
9082 continue;
9083 }
9084 break;
9085
9086 case '<':
9087 case '>':
9088 case '[':
9089 case ']':
9090 case '4':
9091 case '5':
9092 case 'H':
9093 case 'W':
9094 case 'D':
9095 case 'j':
9096 case '8':
9097 case 'V':
9098 case 'C':
9099 case 'U':
9100 case 'k':
9101 case 'K':
9102 if (s[0] == '%'
9103 && strncmp (s + 1, "gprel(", sizeof "gprel(" - 1) == 0)
9104 {
9105 /* This is %gprel(SYMBOL). We need to read SYMBOL,
9106 and generate the appropriate reloc. If the text
9107 inside %gprel is not a symbol name with an
9108 optional offset, then we generate a normal reloc
9109 and will probably fail later. */
9110 my_getExpression (&imm_expr, s + sizeof "%gprel" - 1);
9111 if (imm_expr.X_op == O_symbol)
9112 {
9113 mips16_ext = true;
f6688943 9114 *imm_reloc = BFD_RELOC_MIPS16_GPREL;
252b5132
RH
9115 s = expr_end;
9116 ip->use_extend = true;
9117 ip->extend = 0;
9118 continue;
9119 }
9120 }
9121 else
9122 {
9123 /* Just pick up a normal expression. */
9124 my_getExpression (&imm_expr, s);
9125 }
9126
9127 if (imm_expr.X_op == O_register)
9128 {
9129 /* What we thought was an expression turned out to
9130 be a register. */
9131
9132 if (s[0] == '(' && args[1] == '(')
9133 {
9134 /* It looks like the expression was omitted
9135 before a register indirection, which means
9136 that the expression is implicitly zero. We
9137 still set up imm_expr, so that we handle
9138 explicit extensions correctly. */
9139 imm_expr.X_op = O_constant;
9140 imm_expr.X_add_number = 0;
f6688943 9141 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
9142 continue;
9143 }
9144
9145 break;
9146 }
9147
9148 /* We need to relax this instruction. */
f6688943 9149 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
9150 s = expr_end;
9151 continue;
9152
9153 case 'p':
9154 case 'q':
9155 case 'A':
9156 case 'B':
9157 case 'E':
9158 /* We use offset_reloc rather than imm_reloc for the PC
9159 relative operands. This lets macros with both
9160 immediate and address operands work correctly. */
9161 my_getExpression (&offset_expr, s);
9162
9163 if (offset_expr.X_op == O_register)
9164 break;
9165
9166 /* We need to relax this instruction. */
f6688943 9167 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
9168 s = expr_end;
9169 continue;
9170
9171 case '6': /* break code */
9172 my_getExpression (&imm_expr, s);
9173 check_absolute_expr (ip, &imm_expr);
9174 if ((unsigned long) imm_expr.X_add_number > 63)
9175 {
9176 as_warn (_("Invalid value for `%s' (%lu)"),
9177 ip->insn_mo->name,
9178 (unsigned long) imm_expr.X_add_number);
9179 imm_expr.X_add_number &= 0x3f;
9180 }
9181 ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
9182 imm_expr.X_op = O_absent;
9183 s = expr_end;
9184 continue;
9185
9186 case 'a': /* 26 bit address */
9187 my_getExpression (&offset_expr, s);
9188 s = expr_end;
f6688943 9189 *offset_reloc = BFD_RELOC_MIPS16_JMP;
252b5132
RH
9190 ip->insn_opcode <<= 16;
9191 continue;
9192
9193 case 'l': /* register list for entry macro */
9194 case 'L': /* register list for exit macro */
9195 {
9196 int mask;
9197
9198 if (c == 'l')
9199 mask = 0;
9200 else
9201 mask = 7 << 3;
9202 while (*s != '\0')
9203 {
9204 int freg, reg1, reg2;
9205
9206 while (*s == ' ' || *s == ',')
9207 ++s;
9208 if (*s != '$')
9209 {
9210 as_bad (_("can't parse register list"));
9211 break;
9212 }
9213 ++s;
9214 if (*s != 'f')
9215 freg = 0;
9216 else
9217 {
9218 freg = 1;
9219 ++s;
9220 }
9221 reg1 = 0;
3882b010 9222 while (ISDIGIT (*s))
252b5132
RH
9223 {
9224 reg1 *= 10;
9225 reg1 += *s - '0';
9226 ++s;
9227 }
9228 if (*s == ' ')
9229 ++s;
9230 if (*s != '-')
9231 reg2 = reg1;
9232 else
9233 {
9234 ++s;
9235 if (*s != '$')
9236 break;
9237 ++s;
9238 if (freg)
9239 {
9240 if (*s == 'f')
9241 ++s;
9242 else
9243 {
9244 as_bad (_("invalid register list"));
9245 break;
9246 }
9247 }
9248 reg2 = 0;
3882b010 9249 while (ISDIGIT (*s))
252b5132
RH
9250 {
9251 reg2 *= 10;
9252 reg2 += *s - '0';
9253 ++s;
9254 }
9255 }
9256 if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
9257 {
9258 mask &= ~ (7 << 3);
9259 mask |= 5 << 3;
9260 }
9261 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
9262 {
9263 mask &= ~ (7 << 3);
9264 mask |= 6 << 3;
9265 }
9266 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
9267 mask |= (reg2 - 3) << 3;
9268 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
9269 mask |= (reg2 - 15) << 1;
f9419b05 9270 else if (reg1 == RA && reg2 == RA)
252b5132
RH
9271 mask |= 1;
9272 else
9273 {
9274 as_bad (_("invalid register list"));
9275 break;
9276 }
9277 }
9278 /* The mask is filled in in the opcode table for the
9279 benefit of the disassembler. We remove it before
9280 applying the actual mask. */
9281 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
9282 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
9283 }
9284 continue;
9285
9286 case 'e': /* extend code */
9287 my_getExpression (&imm_expr, s);
9288 check_absolute_expr (ip, &imm_expr);
9289 if ((unsigned long) imm_expr.X_add_number > 0x7ff)
9290 {
9291 as_warn (_("Invalid value for `%s' (%lu)"),
9292 ip->insn_mo->name,
9293 (unsigned long) imm_expr.X_add_number);
9294 imm_expr.X_add_number &= 0x7ff;
9295 }
9296 ip->insn_opcode |= imm_expr.X_add_number;
9297 imm_expr.X_op = O_absent;
9298 s = expr_end;
9299 continue;
9300
9301 default:
9302 internalError ();
9303 }
9304 break;
9305 }
9306
9307 /* Args don't match. */
9308 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
9309 strcmp (insn->name, insn[1].name) == 0)
9310 {
9311 ++insn;
9312 s = argsstart;
9313 continue;
9314 }
9315
9316 insn_error = _("illegal operands");
9317
9318 return;
9319 }
9320}
9321
9322/* This structure holds information we know about a mips16 immediate
9323 argument type. */
9324
e972090a
NC
9325struct mips16_immed_operand
9326{
252b5132
RH
9327 /* The type code used in the argument string in the opcode table. */
9328 int type;
9329 /* The number of bits in the short form of the opcode. */
9330 int nbits;
9331 /* The number of bits in the extended form of the opcode. */
9332 int extbits;
9333 /* The amount by which the short form is shifted when it is used;
9334 for example, the sw instruction has a shift count of 2. */
9335 int shift;
9336 /* The amount by which the short form is shifted when it is stored
9337 into the instruction code. */
9338 int op_shift;
9339 /* Non-zero if the short form is unsigned. */
9340 int unsp;
9341 /* Non-zero if the extended form is unsigned. */
9342 int extu;
9343 /* Non-zero if the value is PC relative. */
9344 int pcrel;
9345};
9346
9347/* The mips16 immediate operand types. */
9348
9349static const struct mips16_immed_operand mips16_immed_operands[] =
9350{
9351 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9352 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9353 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9354 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9355 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
9356 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
9357 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
9358 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
9359 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
9360 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
9361 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
9362 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
9363 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
9364 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
9365 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
9366 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
9367 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9368 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9369 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
9370 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
9371 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
9372};
9373
9374#define MIPS16_NUM_IMMED \
9375 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
9376
9377/* Handle a mips16 instruction with an immediate value. This or's the
9378 small immediate value into *INSN. It sets *USE_EXTEND to indicate
9379 whether an extended value is needed; if one is needed, it sets
9380 *EXTEND to the value. The argument type is TYPE. The value is VAL.
9381 If SMALL is true, an unextended opcode was explicitly requested.
9382 If EXT is true, an extended opcode was explicitly requested. If
9383 WARN is true, warn if EXT does not match reality. */
9384
9385static void
9386mips16_immed (file, line, type, val, warn, small, ext, insn, use_extend,
9387 extend)
9388 char *file;
9389 unsigned int line;
9390 int type;
9391 offsetT val;
9392 boolean warn;
9393 boolean small;
9394 boolean ext;
9395 unsigned long *insn;
9396 boolean *use_extend;
9397 unsigned short *extend;
9398{
9399 register const struct mips16_immed_operand *op;
9400 int mintiny, maxtiny;
9401 boolean needext;
9402
9403 op = mips16_immed_operands;
9404 while (op->type != type)
9405 {
9406 ++op;
9407 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
9408 }
9409
9410 if (op->unsp)
9411 {
9412 if (type == '<' || type == '>' || type == '[' || type == ']')
9413 {
9414 mintiny = 1;
9415 maxtiny = 1 << op->nbits;
9416 }
9417 else
9418 {
9419 mintiny = 0;
9420 maxtiny = (1 << op->nbits) - 1;
9421 }
9422 }
9423 else
9424 {
9425 mintiny = - (1 << (op->nbits - 1));
9426 maxtiny = (1 << (op->nbits - 1)) - 1;
9427 }
9428
9429 /* Branch offsets have an implicit 0 in the lowest bit. */
9430 if (type == 'p' || type == 'q')
9431 val /= 2;
9432
9433 if ((val & ((1 << op->shift) - 1)) != 0
9434 || val < (mintiny << op->shift)
9435 || val > (maxtiny << op->shift))
9436 needext = true;
9437 else
9438 needext = false;
9439
9440 if (warn && ext && ! needext)
beae10d5
KH
9441 as_warn_where (file, line,
9442 _("extended operand requested but not required"));
252b5132
RH
9443 if (small && needext)
9444 as_bad_where (file, line, _("invalid unextended operand value"));
9445
9446 if (small || (! ext && ! needext))
9447 {
9448 int insnval;
9449
9450 *use_extend = false;
9451 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
9452 insnval <<= op->op_shift;
9453 *insn |= insnval;
9454 }
9455 else
9456 {
9457 long minext, maxext;
9458 int extval;
9459
9460 if (op->extu)
9461 {
9462 minext = 0;
9463 maxext = (1 << op->extbits) - 1;
9464 }
9465 else
9466 {
9467 minext = - (1 << (op->extbits - 1));
9468 maxext = (1 << (op->extbits - 1)) - 1;
9469 }
9470 if (val < minext || val > maxext)
9471 as_bad_where (file, line,
9472 _("operand value out of range for instruction"));
9473
9474 *use_extend = true;
9475 if (op->extbits == 16)
9476 {
9477 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
9478 val &= 0x1f;
9479 }
9480 else if (op->extbits == 15)
9481 {
9482 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
9483 val &= 0xf;
9484 }
9485 else
9486 {
9487 extval = ((val & 0x1f) << 6) | (val & 0x20);
9488 val = 0;
9489 }
9490
9491 *extend = (unsigned short) extval;
9492 *insn |= val;
9493 }
9494}
9495\f
ad8d3bb3
TS
9496static struct percent_op_match
9497{
9498 const char *str;
9499 const enum small_ex_type type;
9500} percent_op[] =
9501{
ad8d3bb3
TS
9502 {"%lo", S_EX_LO},
9503#ifdef OBJ_ELF
394f9b3a
TS
9504 {"%call_hi", S_EX_CALL_HI},
9505 {"%call_lo", S_EX_CALL_LO},
ad8d3bb3
TS
9506 {"%call16", S_EX_CALL16},
9507 {"%got_disp", S_EX_GOT_DISP},
9508 {"%got_page", S_EX_GOT_PAGE},
9509 {"%got_ofst", S_EX_GOT_OFST},
9510 {"%got_hi", S_EX_GOT_HI},
9511 {"%got_lo", S_EX_GOT_LO},
394f9b3a
TS
9512 {"%got", S_EX_GOT},
9513 {"%gp_rel", S_EX_GP_REL},
9514 {"%half", S_EX_HALF},
ad8d3bb3 9515 {"%highest", S_EX_HIGHEST},
394f9b3a
TS
9516 {"%higher", S_EX_HIGHER},
9517 {"%neg", S_EX_NEG},
ad8d3bb3 9518#endif
394f9b3a 9519 {"%hi", S_EX_HI}
ad8d3bb3
TS
9520};
9521
9522/* Parse small expression input. STR gets adjusted to eat up whitespace.
9523 It detects valid "%percent_op(...)" and "($reg)" strings. Percent_op's
9524 can be nested, this is handled by blanking the innermost, parsing the
9525 rest by subsequent calls. */
252b5132
RH
9526
9527static int
ad8d3bb3
TS
9528my_getSmallParser (str, len, nestlevel)
9529 char **str;
9530 unsigned int *len;
9531 int *nestlevel;
252b5132 9532{
ad8d3bb3
TS
9533 *len = 0;
9534 *str += strspn (*str, " \t");
394f9b3a 9535 /* Check for expression in parentheses. */
ad8d3bb3 9536 if (**str == '(')
252b5132 9537 {
ad8d3bb3
TS
9538 char *b = *str + 1 + strspn (*str + 1, " \t");
9539 char *e;
9540
9541 /* Check for base register. */
9542 if (b[0] == '$')
9543 {
9544 if (strchr (b, ')')
9545 && (e = b + strcspn (b, ") \t"))
9546 && e - b > 1 && e - b < 4)
9547 {
98d3f06f
KH
9548 if ((e - b == 3
9549 && ((b[1] == 'f' && b[2] == 'p')
9550 || (b[1] == 's' && b[2] == 'p')
9551 || (b[1] == 'g' && b[2] == 'p')
9552 || (b[1] == 'a' && b[2] == 't')
9553 || (ISDIGIT (b[1])
9554 && ISDIGIT (b[2]))))
9555 || (ISDIGIT (b[1])))
9556 {
9557 *len = strcspn (*str, ")") + 1;
9558 return S_EX_REGISTER;
9559 }
ad8d3bb3
TS
9560 }
9561 }
394f9b3a 9562 /* Check for percent_op (in parentheses). */
ad8d3bb3
TS
9563 else if (b[0] == '%')
9564 {
9565 *str = b;
394f9b3a 9566 return my_getPercentOp (str, len, nestlevel);
ad8d3bb3 9567 }
76b3015f 9568
394f9b3a
TS
9569 /* Some other expression in the parentheses, which can contain
9570 parentheses itself. Attempt to find the matching one. */
9571 {
9572 int pcnt = 1;
9573 char *s;
9574
9575 *len = 1;
9576 for (s = *str + 1; *s && pcnt; s++, (*len)++)
9577 {
9578 if (*s == '(')
f9419b05 9579 ++pcnt;
394f9b3a 9580 else if (*s == ')')
f9419b05 9581 --pcnt;
394f9b3a
TS
9582 }
9583 }
fb1b3232 9584 }
394f9b3a 9585 /* Check for percent_op (outside of parentheses). */
ad8d3bb3 9586 else if (*str[0] == '%')
394f9b3a
TS
9587 return my_getPercentOp (str, len, nestlevel);
9588
9589 /* Any other expression. */
9590 return S_EX_NONE;
9591}
ad8d3bb3 9592
394f9b3a
TS
9593static int
9594my_getPercentOp (str, len, nestlevel)
9595 char **str;
9596 unsigned int *len;
9597 int *nestlevel;
9598{
9599 char *tmp = *str + 1;
9600 unsigned int i = 0;
ad8d3bb3 9601
394f9b3a
TS
9602 while (ISALPHA (*tmp) || *tmp == '_')
9603 {
9604 *tmp = TOLOWER (*tmp);
9605 tmp++;
9606 }
9607 while (i < (sizeof (percent_op) / sizeof (struct percent_op_match)))
9608 {
9609 if (strncmp (*str, percent_op[i].str, strlen (percent_op[i].str)))
98d3f06f 9610 i++;
394f9b3a 9611 else
ad8d3bb3 9612 {
394f9b3a 9613 int type = percent_op[i].type;
ad8d3bb3 9614
394f9b3a
TS
9615 /* Only %hi and %lo are allowed for OldABI. */
9616 if (! HAVE_NEWABI && type != S_EX_HI && type != S_EX_LO)
9617 return S_EX_NONE;
ad8d3bb3 9618
394f9b3a 9619 *len = strlen (percent_op[i].str);
f9419b05 9620 ++(*nestlevel);
394f9b3a 9621 return type;
ad8d3bb3 9622 }
fb1b3232 9623 }
ad8d3bb3
TS
9624 return S_EX_NONE;
9625}
9626
9627static int
9628my_getSmallExpression (ep, str)
9629 expressionS *ep;
9630 char *str;
9631{
9632 static char *oldstr = NULL;
9633 int c = S_EX_NONE;
9634 int oldc;
394f9b3a 9635 int nestlevel = -1;
ad8d3bb3
TS
9636 unsigned int len;
9637
394f9b3a
TS
9638 /* Don't update oldstr if the last call had nested percent_op's. We need
9639 it to parse the outer ones later. */
ad8d3bb3
TS
9640 if (! oldstr)
9641 oldstr = str;
76b3015f 9642
ad8d3bb3 9643 do
fb1b3232 9644 {
ad8d3bb3 9645 oldc = c;
394f9b3a 9646 c = my_getSmallParser (&str, &len, &nestlevel);
ad8d3bb3
TS
9647 if (c != S_EX_NONE && c != S_EX_REGISTER)
9648 str += len;
fb1b3232 9649 }
ad8d3bb3
TS
9650 while (c != S_EX_NONE && c != S_EX_REGISTER);
9651
394f9b3a 9652 if (nestlevel >= 0)
fb1b3232 9653 {
394f9b3a
TS
9654 /* A percent_op was encountered. Don't try to get an expression if
9655 it is already blanked out. */
ad8d3bb3
TS
9656 if (*(str + strspn (str + 1, " )")) != ')')
9657 {
9658 char save;
9659
394f9b3a 9660 /* Let my_getExpression() stop at the closing parenthesis. */
ad8d3bb3
TS
9661 save = *(str + len);
9662 *(str + len) = '\0';
9663 my_getExpression (ep, str);
9664 *(str + len) = save;
9665 }
394f9b3a 9666 if (nestlevel > 0)
ad8d3bb3 9667 {
394f9b3a
TS
9668 /* Blank out including the % sign and the proper matching
9669 parenthesis. */
9670 int pcnt = 1;
9671 char *s = strrchr (oldstr, '%');
9672 char *end;
9673
9674 for (end = strchr (s, '(') + 1; *end && pcnt; end++)
9675 {
9676 if (*end == '(')
f9419b05 9677 ++pcnt;
394f9b3a 9678 else if (*end == ')')
f9419b05 9679 --pcnt;
394f9b3a
TS
9680 }
9681
9682 memset (s, ' ', end - s);
ad8d3bb3
TS
9683 str = oldstr;
9684 }
9685 else
394f9b3a
TS
9686 expr_end = str + len;
9687
ad8d3bb3 9688 c = oldc;
fb1b3232 9689 }
ad8d3bb3 9690 else if (c == S_EX_NONE)
fb1b3232 9691 {
ad8d3bb3 9692 my_getExpression (ep, str);
fb1b3232 9693 }
ad8d3bb3 9694 else if (c == S_EX_REGISTER)
fb1b3232 9695 {
ad8d3bb3
TS
9696 ep->X_op = O_constant;
9697 expr_end = str;
9698 ep->X_add_symbol = NULL;
9699 ep->X_op_symbol = NULL;
9700 ep->X_add_number = 0;
fb1b3232 9701 }
fb1b3232
TS
9702 else
9703 {
98d3f06f 9704 as_fatal (_("internal error"));
fb1b3232 9705 }
252b5132 9706
394f9b3a
TS
9707 if (nestlevel <= 0)
9708 /* All percent_op's have been handled. */
ad8d3bb3 9709 oldstr = NULL;
fb1b3232 9710
fb1b3232 9711 return c;
252b5132
RH
9712}
9713
9714static void
9715my_getExpression (ep, str)
9716 expressionS *ep;
9717 char *str;
9718{
9719 char *save_in;
98aa84af 9720 valueT val;
252b5132
RH
9721
9722 save_in = input_line_pointer;
9723 input_line_pointer = str;
9724 expression (ep);
9725 expr_end = input_line_pointer;
9726 input_line_pointer = save_in;
9727
9728 /* If we are in mips16 mode, and this is an expression based on `.',
9729 then we bump the value of the symbol by 1 since that is how other
9730 text symbols are handled. We don't bother to handle complex
9731 expressions, just `.' plus or minus a constant. */
9732 if (mips_opts.mips16
9733 && ep->X_op == O_symbol
9734 && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
9735 && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
49309057
ILT
9736 && symbol_get_frag (ep->X_add_symbol) == frag_now
9737 && symbol_constant_p (ep->X_add_symbol)
98aa84af
AM
9738 && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
9739 S_SET_VALUE (ep->X_add_symbol, val + 1);
252b5132
RH
9740}
9741
9742/* Turn a string in input_line_pointer into a floating point constant
bc0d738a
NC
9743 of type TYPE, and store the appropriate bytes in *LITP. The number
9744 of LITTLENUMS emitted is stored in *SIZEP. An error message is
252b5132
RH
9745 returned, or NULL on OK. */
9746
9747char *
9748md_atof (type, litP, sizeP)
9749 int type;
9750 char *litP;
9751 int *sizeP;
9752{
9753 int prec;
9754 LITTLENUM_TYPE words[4];
9755 char *t;
9756 int i;
9757
9758 switch (type)
9759 {
9760 case 'f':
9761 prec = 2;
9762 break;
9763
9764 case 'd':
9765 prec = 4;
9766 break;
9767
9768 default:
9769 *sizeP = 0;
9770 return _("bad call to md_atof");
9771 }
9772
9773 t = atof_ieee (input_line_pointer, type, words);
9774 if (t)
9775 input_line_pointer = t;
9776
9777 *sizeP = prec * 2;
9778
9779 if (! target_big_endian)
9780 {
9781 for (i = prec - 1; i >= 0; i--)
9782 {
9783 md_number_to_chars (litP, (valueT) words[i], 2);
9784 litP += 2;
9785 }
9786 }
9787 else
9788 {
9789 for (i = 0; i < prec; i++)
9790 {
9791 md_number_to_chars (litP, (valueT) words[i], 2);
9792 litP += 2;
9793 }
9794 }
bdaaa2e1 9795
252b5132
RH
9796 return NULL;
9797}
9798
9799void
9800md_number_to_chars (buf, val, n)
9801 char *buf;
9802 valueT val;
9803 int n;
9804{
9805 if (target_big_endian)
9806 number_to_chars_bigendian (buf, val, n);
9807 else
9808 number_to_chars_littleendian (buf, val, n);
9809}
9810\f
ae948b86 9811#ifdef OBJ_ELF
e013f690
TS
9812static int support_64bit_objects(void)
9813{
9814 const char **list, **l;
9815
9816 list = bfd_target_list ();
9817 for (l = list; *l != NULL; l++)
9818#ifdef TE_TMIPS
9819 /* This is traditional mips */
9820 if (strcmp (*l, "elf64-tradbigmips") == 0
9821 || strcmp (*l, "elf64-tradlittlemips") == 0)
9822#else
9823 if (strcmp (*l, "elf64-bigmips") == 0
9824 || strcmp (*l, "elf64-littlemips") == 0)
9825#endif
9826 break;
9827 free (list);
9828 return (*l != NULL);
9829}
ae948b86 9830#endif /* OBJ_ELF */
e013f690 9831
5a38dc70 9832const char *md_shortopts = "nO::g::G:";
252b5132 9833
e972090a
NC
9834struct option md_longopts[] =
9835{
252b5132
RH
9836#define OPTION_MIPS1 (OPTION_MD_BASE + 1)
9837 {"mips0", no_argument, NULL, OPTION_MIPS1},
9838 {"mips1", no_argument, NULL, OPTION_MIPS1},
9839#define OPTION_MIPS2 (OPTION_MD_BASE + 2)
9840 {"mips2", no_argument, NULL, OPTION_MIPS2},
9841#define OPTION_MIPS3 (OPTION_MD_BASE + 3)
9842 {"mips3", no_argument, NULL, OPTION_MIPS3},
9843#define OPTION_MIPS4 (OPTION_MD_BASE + 4)
9844 {"mips4", no_argument, NULL, OPTION_MIPS4},
ae948b86
TS
9845#define OPTION_MIPS5 (OPTION_MD_BASE + 5)
9846 {"mips5", no_argument, NULL, OPTION_MIPS5},
9847#define OPTION_MIPS32 (OPTION_MD_BASE + 6)
9848 {"mips32", no_argument, NULL, OPTION_MIPS32},
9849#define OPTION_MIPS64 (OPTION_MD_BASE + 7)
9850 {"mips64", no_argument, NULL, OPTION_MIPS64},
9851#define OPTION_MEMBEDDED_PIC (OPTION_MD_BASE + 8)
252b5132 9852 {"membedded-pic", no_argument, NULL, OPTION_MEMBEDDED_PIC},
ae948b86 9853#define OPTION_TRAP (OPTION_MD_BASE + 9)
252b5132
RH
9854 {"trap", no_argument, NULL, OPTION_TRAP},
9855 {"no-break", no_argument, NULL, OPTION_TRAP},
ae948b86 9856#define OPTION_BREAK (OPTION_MD_BASE + 10)
252b5132
RH
9857 {"break", no_argument, NULL, OPTION_BREAK},
9858 {"no-trap", no_argument, NULL, OPTION_BREAK},
ae948b86 9859#define OPTION_EB (OPTION_MD_BASE + 11)
252b5132 9860 {"EB", no_argument, NULL, OPTION_EB},
ae948b86 9861#define OPTION_EL (OPTION_MD_BASE + 12)
252b5132 9862 {"EL", no_argument, NULL, OPTION_EL},
ae948b86 9863#define OPTION_MIPS16 (OPTION_MD_BASE + 13)
252b5132 9864 {"mips16", no_argument, NULL, OPTION_MIPS16},
ae948b86 9865#define OPTION_NO_MIPS16 (OPTION_MD_BASE + 14)
252b5132 9866 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
ae948b86 9867#define OPTION_M7000_HILO_FIX (OPTION_MD_BASE + 15)
6b76fefe 9868 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
9ee72ff1
TS
9869#define OPTION_MNO_7000_HILO_FIX (OPTION_MD_BASE + 16)
9870 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
9871 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
ae948b86
TS
9872#define OPTION_FP32 (OPTION_MD_BASE + 17)
9873 {"mfp32", no_argument, NULL, OPTION_FP32},
9874#define OPTION_GP32 (OPTION_MD_BASE + 18)
c97ef257 9875 {"mgp32", no_argument, NULL, OPTION_GP32},
ae948b86 9876#define OPTION_CONSTRUCT_FLOATS (OPTION_MD_BASE + 19)
119d663a 9877 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
ae948b86 9878#define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MD_BASE + 20)
119d663a 9879 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
ae948b86 9880#define OPTION_MARCH (OPTION_MD_BASE + 21)
ec68c924 9881 {"march", required_argument, NULL, OPTION_MARCH},
ae948b86 9882#define OPTION_MTUNE (OPTION_MD_BASE + 22)
ec68c924 9883 {"mtune", required_argument, NULL, OPTION_MTUNE},
316f5878
RS
9884#define OPTION_FP64 (OPTION_MD_BASE + 23)
9885 {"mfp64", no_argument, NULL, OPTION_FP64},
ae948b86
TS
9886#define OPTION_M4650 (OPTION_MD_BASE + 24)
9887 {"m4650", no_argument, NULL, OPTION_M4650},
9888#define OPTION_NO_M4650 (OPTION_MD_BASE + 25)
9889 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
9890#define OPTION_M4010 (OPTION_MD_BASE + 26)
9891 {"m4010", no_argument, NULL, OPTION_M4010},
9892#define OPTION_NO_M4010 (OPTION_MD_BASE + 27)
9893 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
9894#define OPTION_M4100 (OPTION_MD_BASE + 28)
9895 {"m4100", no_argument, NULL, OPTION_M4100},
9896#define OPTION_NO_M4100 (OPTION_MD_BASE + 29)
9897 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
9898#define OPTION_M3900 (OPTION_MD_BASE + 30)
9899 {"m3900", no_argument, NULL, OPTION_M3900},
9900#define OPTION_NO_M3900 (OPTION_MD_BASE + 31)
9901 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
9902#define OPTION_GP64 (OPTION_MD_BASE + 32)
9903 {"mgp64", no_argument, NULL, OPTION_GP64},
1f25f5d3
CD
9904#define OPTION_MIPS3D (OPTION_MD_BASE + 33)
9905 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
9906#define OPTION_NO_MIPS3D (OPTION_MD_BASE + 34)
9907 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
deec1734
CD
9908#define OPTION_MDMX (OPTION_MD_BASE + 35)
9909 {"mdmx", no_argument, NULL, OPTION_MDMX},
9910#define OPTION_NO_MDMX (OPTION_MD_BASE + 36)
9911 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
156c2f8b 9912#ifdef OBJ_ELF
deec1734 9913#define OPTION_ELF_BASE (OPTION_MD_BASE + 37)
156c2f8b 9914#define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
156c2f8b
NC
9915 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
9916 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
ae948b86 9917#define OPTION_NON_SHARED (OPTION_ELF_BASE + 1)
156c2f8b 9918 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
ae948b86 9919#define OPTION_XGOT (OPTION_ELF_BASE + 2)
156c2f8b 9920 {"xgot", no_argument, NULL, OPTION_XGOT},
ae948b86
TS
9921#define OPTION_MABI (OPTION_ELF_BASE + 3)
9922 {"mabi", required_argument, NULL, OPTION_MABI},
9923#define OPTION_32 (OPTION_ELF_BASE + 4)
156c2f8b 9924 {"32", no_argument, NULL, OPTION_32},
ae948b86 9925#define OPTION_N32 (OPTION_ELF_BASE + 5)
e013f690 9926 {"n32", no_argument, NULL, OPTION_N32},
ae948b86 9927#define OPTION_64 (OPTION_ELF_BASE + 6)
156c2f8b 9928 {"64", no_argument, NULL, OPTION_64},
ecb4347a
DJ
9929#define OPTION_MDEBUG (OPTION_ELF_BASE + 7)
9930 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
9931#define OPTION_NO_MDEBUG (OPTION_ELF_BASE + 8)
9932 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
ae948b86 9933#endif /* OBJ_ELF */
252b5132
RH
9934 {NULL, no_argument, NULL, 0}
9935};
156c2f8b 9936size_t md_longopts_size = sizeof (md_longopts);
252b5132 9937
316f5878
RS
9938/* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
9939 NEW_VALUE. Warn if another value was already specified. Note:
9940 we have to defer parsing the -march and -mtune arguments in order
9941 to handle 'from-abi' correctly, since the ABI might be specified
9942 in a later argument. */
9943
9944static void
9945mips_set_option_string (string_ptr, new_value)
9946 const char **string_ptr, *new_value;
9947{
9948 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
9949 as_warn (_("A different %s was already specified, is now %s"),
9950 string_ptr == &mips_arch_string ? "-march" : "-mtune",
9951 new_value);
9952
9953 *string_ptr = new_value;
9954}
9955
252b5132
RH
9956int
9957md_parse_option (c, arg)
9958 int c;
9959 char *arg;
9960{
9961 switch (c)
9962 {
119d663a
NC
9963 case OPTION_CONSTRUCT_FLOATS:
9964 mips_disable_float_construction = 0;
9965 break;
bdaaa2e1 9966
119d663a
NC
9967 case OPTION_NO_CONSTRUCT_FLOATS:
9968 mips_disable_float_construction = 1;
9969 break;
bdaaa2e1 9970
252b5132
RH
9971 case OPTION_TRAP:
9972 mips_trap = 1;
9973 break;
9974
9975 case OPTION_BREAK:
9976 mips_trap = 0;
9977 break;
9978
9979 case OPTION_EB:
9980 target_big_endian = 1;
9981 break;
9982
9983 case OPTION_EL:
9984 target_big_endian = 0;
9985 break;
9986
39c0a331
L
9987 case 'n':
9988 warn_nops = 1;
9989 break;
9990
252b5132
RH
9991 case 'O':
9992 if (arg && arg[1] == '0')
9993 mips_optimize = 1;
9994 else
9995 mips_optimize = 2;
9996 break;
9997
9998 case 'g':
9999 if (arg == NULL)
10000 mips_debug = 2;
10001 else
10002 mips_debug = atoi (arg);
10003 /* When the MIPS assembler sees -g or -g2, it does not do
10004 optimizations which limit full symbolic debugging. We take
10005 that to be equivalent to -O0. */
10006 if (mips_debug == 2)
10007 mips_optimize = 1;
10008 break;
10009
10010 case OPTION_MIPS1:
316f5878 10011 file_mips_isa = ISA_MIPS1;
252b5132
RH
10012 break;
10013
10014 case OPTION_MIPS2:
316f5878 10015 file_mips_isa = ISA_MIPS2;
252b5132
RH
10016 break;
10017
10018 case OPTION_MIPS3:
316f5878 10019 file_mips_isa = ISA_MIPS3;
252b5132
RH
10020 break;
10021
10022 case OPTION_MIPS4:
316f5878 10023 file_mips_isa = ISA_MIPS4;
e7af610e
NC
10024 break;
10025
84ea6cf2 10026 case OPTION_MIPS5:
316f5878 10027 file_mips_isa = ISA_MIPS5;
84ea6cf2
NC
10028 break;
10029
e7af610e 10030 case OPTION_MIPS32:
316f5878 10031 file_mips_isa = ISA_MIPS32;
252b5132
RH
10032 break;
10033
84ea6cf2 10034 case OPTION_MIPS64:
316f5878 10035 file_mips_isa = ISA_MIPS64;
84ea6cf2
NC
10036 break;
10037
ec68c924 10038 case OPTION_MTUNE:
316f5878
RS
10039 mips_set_option_string (&mips_tune_string, arg);
10040 break;
ec68c924 10041
316f5878
RS
10042 case OPTION_MARCH:
10043 mips_set_option_string (&mips_arch_string, arg);
252b5132
RH
10044 break;
10045
10046 case OPTION_M4650:
316f5878
RS
10047 mips_set_option_string (&mips_arch_string, "4650");
10048 mips_set_option_string (&mips_tune_string, "4650");
252b5132
RH
10049 break;
10050
10051 case OPTION_NO_M4650:
10052 break;
10053
10054 case OPTION_M4010:
316f5878
RS
10055 mips_set_option_string (&mips_arch_string, "4010");
10056 mips_set_option_string (&mips_tune_string, "4010");
252b5132
RH
10057 break;
10058
10059 case OPTION_NO_M4010:
10060 break;
10061
10062 case OPTION_M4100:
316f5878
RS
10063 mips_set_option_string (&mips_arch_string, "4100");
10064 mips_set_option_string (&mips_tune_string, "4100");
252b5132
RH
10065 break;
10066
10067 case OPTION_NO_M4100:
10068 break;
10069
252b5132 10070 case OPTION_M3900:
316f5878
RS
10071 mips_set_option_string (&mips_arch_string, "3900");
10072 mips_set_option_string (&mips_tune_string, "3900");
252b5132 10073 break;
bdaaa2e1 10074
252b5132
RH
10075 case OPTION_NO_M3900:
10076 break;
10077
deec1734
CD
10078 case OPTION_MDMX:
10079 mips_opts.ase_mdmx = 1;
10080 break;
10081
10082 case OPTION_NO_MDMX:
10083 mips_opts.ase_mdmx = 0;
10084 break;
10085
252b5132
RH
10086 case OPTION_MIPS16:
10087 mips_opts.mips16 = 1;
10088 mips_no_prev_insn (false);
10089 break;
10090
10091 case OPTION_NO_MIPS16:
10092 mips_opts.mips16 = 0;
10093 mips_no_prev_insn (false);
10094 break;
10095
1f25f5d3
CD
10096 case OPTION_MIPS3D:
10097 mips_opts.ase_mips3d = 1;
10098 break;
10099
10100 case OPTION_NO_MIPS3D:
10101 mips_opts.ase_mips3d = 0;
10102 break;
10103
252b5132
RH
10104 case OPTION_MEMBEDDED_PIC:
10105 mips_pic = EMBEDDED_PIC;
10106 if (USE_GLOBAL_POINTER_OPT && g_switch_seen)
10107 {
10108 as_bad (_("-G may not be used with embedded PIC code"));
10109 return 0;
10110 }
10111 g_switch_value = 0x7fffffff;
10112 break;
10113
0f074f60 10114#ifdef OBJ_ELF
252b5132
RH
10115 /* When generating ELF code, we permit -KPIC and -call_shared to
10116 select SVR4_PIC, and -non_shared to select no PIC. This is
10117 intended to be compatible with Irix 5. */
10118 case OPTION_CALL_SHARED:
10119 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10120 {
10121 as_bad (_("-call_shared is supported only for ELF format"));
10122 return 0;
10123 }
10124 mips_pic = SVR4_PIC;
10125 if (g_switch_seen && g_switch_value != 0)
10126 {
10127 as_bad (_("-G may not be used with SVR4 PIC code"));
10128 return 0;
10129 }
10130 g_switch_value = 0;
10131 break;
10132
10133 case OPTION_NON_SHARED:
10134 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10135 {
10136 as_bad (_("-non_shared is supported only for ELF format"));
10137 return 0;
10138 }
10139 mips_pic = NO_PIC;
10140 break;
10141
10142 /* The -xgot option tells the assembler to use 32 offsets when
10143 accessing the got in SVR4_PIC mode. It is for Irix
10144 compatibility. */
10145 case OPTION_XGOT:
10146 mips_big_got = 1;
10147 break;
0f074f60 10148#endif /* OBJ_ELF */
252b5132
RH
10149
10150 case 'G':
10151 if (! USE_GLOBAL_POINTER_OPT)
10152 {
10153 as_bad (_("-G is not supported for this configuration"));
10154 return 0;
10155 }
10156 else if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
10157 {
10158 as_bad (_("-G may not be used with SVR4 or embedded PIC code"));
10159 return 0;
10160 }
10161 else
10162 g_switch_value = atoi (arg);
10163 g_switch_seen = 1;
10164 break;
10165
0f074f60 10166#ifdef OBJ_ELF
34ba82a8
TS
10167 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
10168 and -mabi=64. */
252b5132 10169 case OPTION_32:
34ba82a8
TS
10170 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10171 {
10172 as_bad (_("-32 is supported for ELF format only"));
10173 return 0;
10174 }
316f5878 10175 mips_abi = O32_ABI;
252b5132
RH
10176 break;
10177
e013f690 10178 case OPTION_N32:
34ba82a8
TS
10179 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10180 {
10181 as_bad (_("-n32 is supported for ELF format only"));
10182 return 0;
10183 }
316f5878 10184 mips_abi = N32_ABI;
e013f690 10185 break;
252b5132 10186
e013f690 10187 case OPTION_64:
34ba82a8
TS
10188 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10189 {
10190 as_bad (_("-64 is supported for ELF format only"));
10191 return 0;
10192 }
316f5878 10193 mips_abi = N64_ABI;
e013f690
TS
10194 if (! support_64bit_objects())
10195 as_fatal (_("No compiled in support for 64 bit object file format"));
252b5132 10196 break;
ae948b86 10197#endif /* OBJ_ELF */
252b5132 10198
c97ef257 10199 case OPTION_GP32:
a325df1d 10200 file_mips_gp32 = 1;
c97ef257
AH
10201 break;
10202
10203 case OPTION_GP64:
a325df1d 10204 file_mips_gp32 = 0;
c97ef257 10205 break;
252b5132 10206
ca4e0257 10207 case OPTION_FP32:
a325df1d 10208 file_mips_fp32 = 1;
316f5878
RS
10209 break;
10210
10211 case OPTION_FP64:
10212 file_mips_fp32 = 0;
ca4e0257
RS
10213 break;
10214
ae948b86 10215#ifdef OBJ_ELF
252b5132 10216 case OPTION_MABI:
34ba82a8
TS
10217 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10218 {
10219 as_bad (_("-mabi is supported for ELF format only"));
10220 return 0;
10221 }
e013f690 10222 if (strcmp (arg, "32") == 0)
316f5878 10223 mips_abi = O32_ABI;
e013f690 10224 else if (strcmp (arg, "o64") == 0)
316f5878 10225 mips_abi = O64_ABI;
e013f690 10226 else if (strcmp (arg, "n32") == 0)
316f5878 10227 mips_abi = N32_ABI;
e013f690
TS
10228 else if (strcmp (arg, "64") == 0)
10229 {
316f5878 10230 mips_abi = N64_ABI;
e013f690
TS
10231 if (! support_64bit_objects())
10232 as_fatal (_("No compiled in support for 64 bit object file "
10233 "format"));
10234 }
10235 else if (strcmp (arg, "eabi") == 0)
316f5878 10236 mips_abi = EABI_ABI;
e013f690 10237 else
da0e507f
TS
10238 {
10239 as_fatal (_("invalid abi -mabi=%s"), arg);
10240 return 0;
10241 }
252b5132 10242 break;
e013f690 10243#endif /* OBJ_ELF */
252b5132 10244
6b76fefe
CM
10245 case OPTION_M7000_HILO_FIX:
10246 mips_7000_hilo_fix = true;
10247 break;
10248
9ee72ff1 10249 case OPTION_MNO_7000_HILO_FIX:
6b76fefe
CM
10250 mips_7000_hilo_fix = false;
10251 break;
10252
ecb4347a
DJ
10253#ifdef OBJ_ELF
10254 case OPTION_MDEBUG:
10255 mips_flag_mdebug = true;
10256 break;
10257
10258 case OPTION_NO_MDEBUG:
10259 mips_flag_mdebug = false;
10260 break;
10261#endif /* OBJ_ELF */
10262
252b5132
RH
10263 default:
10264 return 0;
10265 }
10266
10267 return 1;
10268}
316f5878
RS
10269\f
10270/* Set up globals to generate code for the ISA or processor
10271 described by INFO. */
252b5132 10272
252b5132 10273static void
316f5878
RS
10274mips_set_architecture (info)
10275 const struct mips_cpu_info *info;
252b5132 10276{
316f5878 10277 if (info != 0)
252b5132 10278 {
316f5878
RS
10279 mips_arch_info = info;
10280 mips_arch = info->cpu;
10281 mips_opts.isa = info->isa;
252b5132 10282 }
252b5132
RH
10283}
10284
252b5132 10285
316f5878 10286/* Likewise for tuning. */
252b5132 10287
316f5878
RS
10288static void
10289mips_set_tune (info)
10290 const struct mips_cpu_info *info;
10291{
10292 if (info != 0)
10293 {
10294 mips_tune_info = info;
10295 mips_tune = info->cpu;
10296 }
10297}
80cc45a5 10298
34ba82a8 10299
252b5132 10300void
e9670677
MR
10301mips_after_parse_args ()
10302{
e9670677
MR
10303 /* GP relative stuff not working for PE */
10304 if (strncmp (TARGET_OS, "pe", 2) == 0
10305 && g_switch_value != 0)
10306 {
10307 if (g_switch_seen)
10308 as_bad (_("-G not supported in this configuration."));
10309 g_switch_value = 0;
10310 }
10311
22923709
RS
10312 /* The following code determines the architecture and register size.
10313 Similar code was added to GCC 3.3 (see override_options() in
10314 config/mips/mips.c). The GAS and GCC code should be kept in sync
10315 as much as possible. */
e9670677 10316
316f5878
RS
10317 if (mips_arch_string != 0)
10318 mips_set_architecture (mips_parse_cpu ("-march", mips_arch_string));
e9670677 10319
316f5878
RS
10320 if (mips_tune_string != 0)
10321 mips_set_tune (mips_parse_cpu ("-mtune", mips_tune_string));
e9670677 10322
316f5878 10323 if (file_mips_isa != ISA_UNKNOWN)
e9670677 10324 {
316f5878
RS
10325 /* Handle -mipsN. At this point, file_mips_isa contains the
10326 ISA level specified by -mipsN, while mips_opts.isa contains
10327 the -march selection (if any). */
10328 if (mips_arch_info != 0)
e9670677 10329 {
316f5878
RS
10330 /* -march takes precedence over -mipsN, since it is more descriptive.
10331 There's no harm in specifying both as long as the ISA levels
10332 are the same. */
10333 if (file_mips_isa != mips_opts.isa)
10334 as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
10335 mips_cpu_info_from_isa (file_mips_isa)->name,
10336 mips_cpu_info_from_isa (mips_opts.isa)->name);
e9670677 10337 }
316f5878
RS
10338 else
10339 mips_set_architecture (mips_cpu_info_from_isa (file_mips_isa));
e9670677
MR
10340 }
10341
316f5878
RS
10342 if (mips_arch_info == 0)
10343 mips_set_architecture (mips_parse_cpu ("default CPU",
10344 MIPS_CPU_STRING_DEFAULT));
e9670677 10345
316f5878
RS
10346 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (mips_opts.isa))
10347 as_bad ("-march=%s is not compatible with the selected ABI",
10348 mips_arch_info->name);
e9670677 10349
316f5878
RS
10350 /* Optimize for mips_arch, unless -mtune selects a different processor. */
10351 if (mips_tune_info == 0)
10352 mips_set_tune (mips_arch_info);
e9670677 10353
316f5878 10354 if (file_mips_gp32 >= 0)
e9670677 10355 {
316f5878
RS
10356 /* The user specified the size of the integer registers. Make sure
10357 it agrees with the ABI and ISA. */
10358 if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
10359 as_bad (_("-mgp64 used with a 32-bit processor"));
10360 else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
10361 as_bad (_("-mgp32 used with a 64-bit ABI"));
10362 else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
10363 as_bad (_("-mgp64 used with a 32-bit ABI"));
e9670677
MR
10364 }
10365 else
10366 {
316f5878
RS
10367 /* Infer the integer register size from the ABI and processor.
10368 Restrict ourselves to 32-bit registers if that's all the
10369 processor has, or if the ABI cannot handle 64-bit registers. */
10370 file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
10371 || !ISA_HAS_64BIT_REGS (mips_opts.isa));
e9670677
MR
10372 }
10373
316f5878
RS
10374 /* ??? GAS treats single-float processors as though they had 64-bit
10375 float registers (although it complains when double-precision
10376 instructions are used). As things stand, saying they have 32-bit
10377 registers would lead to spurious "register must be even" messages.
10378 So here we assume float registers are always the same size as
10379 integer ones, unless the user says otherwise. */
10380 if (file_mips_fp32 < 0)
10381 file_mips_fp32 = file_mips_gp32;
e9670677 10382
316f5878 10383 /* End of GCC-shared inference code. */
e9670677 10384
316f5878
RS
10385 /* ??? When do we want this flag to be set? Who uses it? */
10386 if (file_mips_gp32 == 1
10387 && mips_abi == NO_ABI
10388 && ISA_HAS_64BIT_REGS (mips_opts.isa))
10389 mips_32bitmode = 1;
e9670677
MR
10390
10391 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
10392 as_bad (_("trap exception not supported at ISA 1"));
10393
e9670677
MR
10394 /* If the selected architecture includes support for ASEs, enable
10395 generation of code for them. */
a4672219
TS
10396 if (mips_opts.mips16 == -1)
10397 mips_opts.mips16 = (CPU_HAS_MIPS16 (mips_arch)) ? 1 : 0;
ffdefa66 10398 if (mips_opts.ase_mips3d == -1)
a4672219 10399 mips_opts.ase_mips3d = (CPU_HAS_MIPS3D (mips_arch)) ? 1 : 0;
ffdefa66 10400 if (mips_opts.ase_mdmx == -1)
a4672219 10401 mips_opts.ase_mdmx = (CPU_HAS_MDMX (mips_arch)) ? 1 : 0;
e9670677 10402
e9670677 10403 file_mips_isa = mips_opts.isa;
a4672219 10404 file_ase_mips16 = mips_opts.mips16;
e9670677
MR
10405 file_ase_mips3d = mips_opts.ase_mips3d;
10406 file_ase_mdmx = mips_opts.ase_mdmx;
10407 mips_opts.gp32 = file_mips_gp32;
10408 mips_opts.fp32 = file_mips_fp32;
10409
10410 if (HAVE_NEWABI)
10411 mips_big_got = 1;
ecb4347a
DJ
10412
10413 if (mips_flag_mdebug < 0)
10414 {
10415#ifdef OBJ_MAYBE_ECOFF
10416 if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
10417 mips_flag_mdebug = 1;
10418 else
10419#endif /* OBJ_MAYBE_ECOFF */
10420 mips_flag_mdebug = 0;
10421 }
e9670677
MR
10422}
10423\f
10424void
252b5132
RH
10425mips_init_after_args ()
10426{
10427 /* initialize opcodes */
10428 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
beae10d5 10429 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
252b5132
RH
10430}
10431
10432long
10433md_pcrel_from (fixP)
10434 fixS *fixP;
10435{
10436 if (OUTPUT_FLAVOR != bfd_target_aout_flavour
10437 && fixP->fx_addsy != (symbolS *) NULL
10438 && ! S_IS_DEFINED (fixP->fx_addsy))
10439 {
6478892d
TS
10440 /* This makes a branch to an undefined symbol be a branch to the
10441 current location. */
cb56d3d3 10442 if (mips_pic == EMBEDDED_PIC)
6478892d 10443 return 4;
cb56d3d3 10444 else
6478892d 10445 return 1;
252b5132
RH
10446 }
10447
c9914766 10448 /* Return the address of the delay slot. */
252b5132
RH
10449 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
10450}
10451
252b5132
RH
10452/* This is called before the symbol table is processed. In order to
10453 work with gcc when using mips-tfile, we must keep all local labels.
10454 However, in other cases, we want to discard them. If we were
10455 called with -g, but we didn't see any debugging information, it may
10456 mean that gcc is smuggling debugging information through to
10457 mips-tfile, in which case we must generate all local labels. */
10458
10459void
10460mips_frob_file_before_adjust ()
10461{
10462#ifndef NO_ECOFF_DEBUGGING
10463 if (ECOFF_DEBUGGING
10464 && mips_debug != 0
10465 && ! ecoff_debugging_seen)
10466 flag_keep_locals = 1;
10467#endif
10468}
10469
10470/* Sort any unmatched HI16_S relocs so that they immediately precede
94f592af 10471 the corresponding LO reloc. This is called before md_apply_fix3 and
252b5132
RH
10472 tc_gen_reloc. Unmatched HI16_S relocs can only be generated by
10473 explicit use of the %hi modifier. */
10474
10475void
10476mips_frob_file ()
10477{
10478 struct mips_hi_fixup *l;
10479
10480 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
10481 {
10482 segment_info_type *seginfo;
10483 int pass;
10484
10485 assert (l->fixp->fx_r_type == BFD_RELOC_HI16_S);
10486
10487 /* Check quickly whether the next fixup happens to be a matching
10488 %lo. */
10489 if (l->fixp->fx_next != NULL
10490 && l->fixp->fx_next->fx_r_type == BFD_RELOC_LO16
10491 && l->fixp->fx_addsy == l->fixp->fx_next->fx_addsy
10492 && l->fixp->fx_offset == l->fixp->fx_next->fx_offset)
10493 continue;
10494
10495 /* Look through the fixups for this segment for a matching %lo.
10496 When we find one, move the %hi just in front of it. We do
10497 this in two passes. In the first pass, we try to find a
10498 unique %lo. In the second pass, we permit multiple %hi
10499 relocs for a single %lo (this is a GNU extension). */
10500 seginfo = seg_info (l->seg);
10501 for (pass = 0; pass < 2; pass++)
10502 {
10503 fixS *f, *prev;
10504
10505 prev = NULL;
10506 for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
10507 {
10508 /* Check whether this is a %lo fixup which matches l->fixp. */
10509 if (f->fx_r_type == BFD_RELOC_LO16
10510 && f->fx_addsy == l->fixp->fx_addsy
10511 && f->fx_offset == l->fixp->fx_offset
10512 && (pass == 1
10513 || prev == NULL
10514 || prev->fx_r_type != BFD_RELOC_HI16_S
10515 || prev->fx_addsy != f->fx_addsy
10516 || prev->fx_offset != f->fx_offset))
10517 {
10518 fixS **pf;
10519
10520 /* Move l->fixp before f. */
10521 for (pf = &seginfo->fix_root;
10522 *pf != l->fixp;
10523 pf = &(*pf)->fx_next)
10524 assert (*pf != NULL);
10525
10526 *pf = l->fixp->fx_next;
10527
10528 l->fixp->fx_next = f;
10529 if (prev == NULL)
10530 seginfo->fix_root = l->fixp;
10531 else
10532 prev->fx_next = l->fixp;
10533
10534 break;
10535 }
10536
10537 prev = f;
10538 }
10539
10540 if (f != NULL)
10541 break;
10542
10543#if 0 /* GCC code motion plus incomplete dead code elimination
10544 can leave a %hi without a %lo. */
10545 if (pass == 1)
10546 as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
10547 _("Unmatched %%hi reloc"));
10548#endif
10549 }
10550 }
10551}
10552
10553/* When generating embedded PIC code we need to use a special
10554 relocation to represent the difference of two symbols in the .text
10555 section (switch tables use a difference of this sort). See
10556 include/coff/mips.h for details. This macro checks whether this
10557 fixup requires the special reloc. */
10558#define SWITCH_TABLE(fixp) \
10559 ((fixp)->fx_r_type == BFD_RELOC_32 \
bb2d6cd7 10560 && OUTPUT_FLAVOR != bfd_target_elf_flavour \
252b5132
RH
10561 && (fixp)->fx_addsy != NULL \
10562 && (fixp)->fx_subsy != NULL \
10563 && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
10564 && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
10565
10566/* When generating embedded PIC code we must keep all PC relative
10567 relocations, in case the linker has to relax a call. We also need
f6688943
TS
10568 to keep relocations for switch table entries.
10569
10570 We may have combined relocations without symbols in the N32/N64 ABI.
10571 We have to prevent gas from dropping them. */
252b5132 10572
252b5132
RH
10573int
10574mips_force_relocation (fixp)
10575 fixS *fixp;
10576{
10577 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
10578 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
10579 return 1;
10580
f6688943
TS
10581 if (HAVE_NEWABI
10582 && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
10583 && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
10584 || fixp->fx_r_type == BFD_RELOC_HI16_S
10585 || fixp->fx_r_type == BFD_RELOC_LO16))
10586 return 1;
10587
252b5132
RH
10588 return (mips_pic == EMBEDDED_PIC
10589 && (fixp->fx_pcrel
10590 || SWITCH_TABLE (fixp)
10591 || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
10592 || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
10593}
10594
add55e1f
RS
10595#ifdef OBJ_ELF
10596static int
10597mips_need_elf_addend_fixup (fixP)
10598 fixS *fixP;
10599{
2d2bf3e0
CD
10600 if (S_GET_OTHER (fixP->fx_addsy) == STO_MIPS16)
10601 return 1;
b25a253c
CD
10602 if (mips_pic == EMBEDDED_PIC
10603 && S_IS_WEAK (fixP->fx_addsy))
10604 return 1;
10605 if (mips_pic != EMBEDDED_PIC
10606 && (S_IS_WEAK (fixP->fx_addsy)
bad9ca53 10607 || S_IS_EXTERNAL (fixP->fx_addsy))
2d2bf3e0
CD
10608 && !S_IS_COMMON (fixP->fx_addsy))
10609 return 1;
10610 if (symbol_used_in_reloc_p (fixP->fx_addsy)
10611 && (((bfd_get_section_flags (stdoutput,
10612 S_GET_SEGMENT (fixP->fx_addsy))
10613 & SEC_LINK_ONCE) != 0)
10614 || !strncmp (segment_name (S_GET_SEGMENT (fixP->fx_addsy)),
10615 ".gnu.linkonce",
10616 sizeof (".gnu.linkonce") - 1)))
10617 return 1;
10618 return 0;
add55e1f
RS
10619}
10620#endif
10621
252b5132
RH
10622/* Apply a fixup to the object file. */
10623
94f592af
NC
10624void
10625md_apply_fix3 (fixP, valP, seg)
252b5132 10626 fixS *fixP;
98d3f06f 10627 valueT *valP;
94f592af 10628 segT seg ATTRIBUTE_UNUSED;
252b5132 10629{
874e8986 10630 bfd_byte *buf;
98aa84af
AM
10631 long insn;
10632 valueT value;
252b5132
RH
10633
10634 assert (fixP->fx_size == 4
10635 || fixP->fx_r_type == BFD_RELOC_16
f6688943
TS
10636 || fixP->fx_r_type == BFD_RELOC_32
10637 || fixP->fx_r_type == BFD_RELOC_MIPS_JMP
10638 || fixP->fx_r_type == BFD_RELOC_HI16_S
10639 || fixP->fx_r_type == BFD_RELOC_LO16
10640 || fixP->fx_r_type == BFD_RELOC_GPREL16
76b3015f 10641 || fixP->fx_r_type == BFD_RELOC_MIPS_LITERAL
f6688943 10642 || fixP->fx_r_type == BFD_RELOC_GPREL32
252b5132 10643 || fixP->fx_r_type == BFD_RELOC_64
f6688943
TS
10644 || fixP->fx_r_type == BFD_RELOC_CTOR
10645 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
10646 || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHEST
10647 || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHER
10648 || fixP->fx_r_type == BFD_RELOC_MIPS_SCN_DISP
10649 || fixP->fx_r_type == BFD_RELOC_MIPS_REL16
10650 || fixP->fx_r_type == BFD_RELOC_MIPS_RELGOT
252b5132 10651 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
438c16b8
TS
10652 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
10653 || fixP->fx_r_type == BFD_RELOC_MIPS_JALR);
252b5132 10654
98d3f06f 10655 value = *valP;
252b5132
RH
10656
10657 /* If we aren't adjusting this fixup to be against the section
10658 symbol, we need to adjust the value. */
10659#ifdef OBJ_ELF
10660 if (fixP->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour)
bb2d6cd7 10661 {
add55e1f 10662 if (mips_need_elf_addend_fixup (fixP))
98aa84af 10663 {
d6e9d61a 10664 reloc_howto_type *howto;
98aa84af 10665 valueT symval = S_GET_VALUE (fixP->fx_addsy);
94f592af 10666
98aa84af 10667 value -= symval;
d6e9d61a
MR
10668
10669 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
10670 if (value != 0 && howto->partial_inplace && ! fixP->fx_pcrel)
98aa84af
AM
10671 {
10672 /* In this case, the bfd_install_relocation routine will
10673 incorrectly add the symbol value back in. We just want
7461da6e 10674 the addend to appear in the object file. */
98aa84af 10675 value -= symval;
7461da6e
RS
10676
10677 /* Make sure the addend is still non-zero. If it became zero
10678 after the last operation, set it to a spurious value and
10679 subtract the same value from the object file's contents. */
10680 if (value == 0)
10681 {
10682 value = 8;
10683
10684 /* The in-place addends for LO16 relocations are signed;
10685 leave the matching HI16 in-place addends as zero. */
10686 if (fixP->fx_r_type != BFD_RELOC_HI16_S)
10687 {
7461da6e
RS
10688 bfd_vma contents, mask, field;
10689
7461da6e
RS
10690 contents = bfd_get_bits (fixP->fx_frag->fr_literal
10691 + fixP->fx_where,
10692 fixP->fx_size * 8,
10693 target_big_endian);
10694
10695 /* MASK has bits set where the relocation should go.
10696 FIELD is -value, shifted into the appropriate place
10697 for this relocation. */
10698 mask = 1 << (howto->bitsize - 1);
10699 mask = (((mask - 1) << 1) | 1) << howto->bitpos;
10700 field = (-value >> howto->rightshift) << howto->bitpos;
10701
10702 bfd_put_bits ((field & mask) | (contents & ~mask),
10703 fixP->fx_frag->fr_literal + fixP->fx_where,
10704 fixP->fx_size * 8,
10705 target_big_endian);
10706 }
10707 }
98aa84af
AM
10708 }
10709 }
252b5132 10710
bb2d6cd7
GK
10711 /* This code was generated using trial and error and so is
10712 fragile and not trustworthy. If you change it, you should
10713 rerun the elf-rel, elf-rel2, and empic testcases and ensure
10714 they still pass. */
10715 if (fixP->fx_pcrel || fixP->fx_subsy != NULL)
10716 {
10717 value += fixP->fx_frag->fr_address + fixP->fx_where;
10718
10719 /* BFD's REL handling, for MIPS, is _very_ weird.
10720 This gives the right results, but it can't possibly
10721 be the way things are supposed to work. */
cb56d3d3
TS
10722 if ((fixP->fx_r_type != BFD_RELOC_16_PCREL
10723 && fixP->fx_r_type != BFD_RELOC_16_PCREL_S2)
bb2d6cd7
GK
10724 || S_GET_SEGMENT (fixP->fx_addsy) != undefined_section)
10725 value += fixP->fx_frag->fr_address + fixP->fx_where;
10726 }
10727 }
10728#endif
252b5132 10729
94f592af 10730 fixP->fx_addnumber = value; /* Remember value for tc_gen_reloc. */
252b5132
RH
10731
10732 if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel)
10733 fixP->fx_done = 1;
10734
10735 switch (fixP->fx_r_type)
10736 {
10737 case BFD_RELOC_MIPS_JMP:
e369bcce
TS
10738 case BFD_RELOC_MIPS_SHIFT5:
10739 case BFD_RELOC_MIPS_SHIFT6:
10740 case BFD_RELOC_MIPS_GOT_DISP:
10741 case BFD_RELOC_MIPS_GOT_PAGE:
10742 case BFD_RELOC_MIPS_GOT_OFST:
10743 case BFD_RELOC_MIPS_SUB:
10744 case BFD_RELOC_MIPS_INSERT_A:
10745 case BFD_RELOC_MIPS_INSERT_B:
10746 case BFD_RELOC_MIPS_DELETE:
10747 case BFD_RELOC_MIPS_HIGHEST:
10748 case BFD_RELOC_MIPS_HIGHER:
10749 case BFD_RELOC_MIPS_SCN_DISP:
10750 case BFD_RELOC_MIPS_REL16:
10751 case BFD_RELOC_MIPS_RELGOT:
10752 case BFD_RELOC_MIPS_JALR:
252b5132
RH
10753 case BFD_RELOC_HI16:
10754 case BFD_RELOC_HI16_S:
cdf6fd85 10755 case BFD_RELOC_GPREL16:
252b5132
RH
10756 case BFD_RELOC_MIPS_LITERAL:
10757 case BFD_RELOC_MIPS_CALL16:
10758 case BFD_RELOC_MIPS_GOT16:
cdf6fd85 10759 case BFD_RELOC_GPREL32:
252b5132
RH
10760 case BFD_RELOC_MIPS_GOT_HI16:
10761 case BFD_RELOC_MIPS_GOT_LO16:
10762 case BFD_RELOC_MIPS_CALL_HI16:
10763 case BFD_RELOC_MIPS_CALL_LO16:
10764 case BFD_RELOC_MIPS16_GPREL:
10765 if (fixP->fx_pcrel)
10766 as_bad_where (fixP->fx_file, fixP->fx_line,
10767 _("Invalid PC relative reloc"));
10768 /* Nothing needed to do. The value comes from the reloc entry */
10769 break;
10770
10771 case BFD_RELOC_MIPS16_JMP:
10772 /* We currently always generate a reloc against a symbol, which
10773 means that we don't want an addend even if the symbol is
10774 defined. */
10775 fixP->fx_addnumber = 0;
10776 break;
10777
10778 case BFD_RELOC_PCREL_HI16_S:
10779 /* The addend for this is tricky if it is internal, so we just
10780 do everything here rather than in bfd_install_relocation. */
bdaaa2e1 10781 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
bb2d6cd7
GK
10782 && !fixP->fx_done
10783 && value != 0)
10784 break;
10785 if (fixP->fx_addsy
10786 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
252b5132
RH
10787 {
10788 /* For an external symbol adjust by the address to make it
10789 pcrel_offset. We use the address of the RELLO reloc
10790 which follows this one. */
10791 value += (fixP->fx_next->fx_frag->fr_address
10792 + fixP->fx_next->fx_where);
10793 }
e7d556df 10794 value = ((value + 0x8000) >> 16) & 0xffff;
874e8986 10795 buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
252b5132
RH
10796 if (target_big_endian)
10797 buf += 2;
874e8986 10798 md_number_to_chars ((char *) buf, value, 2);
252b5132
RH
10799 break;
10800
10801 case BFD_RELOC_PCREL_LO16:
10802 /* The addend for this is tricky if it is internal, so we just
10803 do everything here rather than in bfd_install_relocation. */
bdaaa2e1 10804 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
bb2d6cd7
GK
10805 && !fixP->fx_done
10806 && value != 0)
10807 break;
10808 if (fixP->fx_addsy
10809 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
252b5132 10810 value += fixP->fx_frag->fr_address + fixP->fx_where;
874e8986 10811 buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
252b5132
RH
10812 if (target_big_endian)
10813 buf += 2;
874e8986 10814 md_number_to_chars ((char *) buf, value, 2);
252b5132
RH
10815 break;
10816
10817 case BFD_RELOC_64:
10818 /* This is handled like BFD_RELOC_32, but we output a sign
10819 extended value if we are only 32 bits. */
10820 if (fixP->fx_done
10821 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
10822 {
10823 if (8 <= sizeof (valueT))
10824 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
10825 value, 8);
10826 else
10827 {
10828 long w1, w2;
10829 long hiv;
10830
10831 w1 = w2 = fixP->fx_where;
10832 if (target_big_endian)
10833 w1 += 4;
10834 else
10835 w2 += 4;
10836 md_number_to_chars (fixP->fx_frag->fr_literal + w1, value, 4);
10837 if ((value & 0x80000000) != 0)
10838 hiv = 0xffffffff;
10839 else
10840 hiv = 0;
10841 md_number_to_chars (fixP->fx_frag->fr_literal + w2, hiv, 4);
10842 }
10843 }
10844 break;
10845
056350c6 10846 case BFD_RELOC_RVA:
252b5132
RH
10847 case BFD_RELOC_32:
10848 /* If we are deleting this reloc entry, we must fill in the
10849 value now. This can happen if we have a .word which is not
10850 resolved when it appears but is later defined. We also need
10851 to fill in the value if this is an embedded PIC switch table
10852 entry. */
10853 if (fixP->fx_done
10854 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
10855 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
10856 value, 4);
10857 break;
10858
10859 case BFD_RELOC_16:
10860 /* If we are deleting this reloc entry, we must fill in the
10861 value now. */
10862 assert (fixP->fx_size == 2);
10863 if (fixP->fx_done)
10864 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
10865 value, 2);
10866 break;
10867
10868 case BFD_RELOC_LO16:
10869 /* When handling an embedded PIC switch statement, we can wind
10870 up deleting a LO16 reloc. See the 'o' case in mips_ip. */
10871 if (fixP->fx_done)
10872 {
98aa84af 10873 if (value + 0x8000 > 0xffff)
252b5132
RH
10874 as_bad_where (fixP->fx_file, fixP->fx_line,
10875 _("relocation overflow"));
874e8986 10876 buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
252b5132
RH
10877 if (target_big_endian)
10878 buf += 2;
874e8986 10879 md_number_to_chars ((char *) buf, value, 2);
252b5132
RH
10880 }
10881 break;
10882
10883 case BFD_RELOC_16_PCREL_S2:
cb56d3d3
TS
10884 if ((value & 0x3) != 0)
10885 as_bad_where (fixP->fx_file, fixP->fx_line,
10886 _("Branch to odd address (%lx)"), (long) value);
10887
10888 /* Fall through. */
10889
10890 case BFD_RELOC_16_PCREL:
252b5132
RH
10891 /*
10892 * We need to save the bits in the instruction since fixup_segment()
10893 * might be deleting the relocation entry (i.e., a branch within
10894 * the current segment).
10895 */
bb2d6cd7
GK
10896 if (!fixP->fx_done && value != 0)
10897 break;
10898 /* If 'value' is zero, the remaining reloc code won't actually
10899 do the store, so it must be done here. This is probably
10900 a bug somewhere. */
b25a253c
CD
10901 if (!fixP->fx_done
10902 && (fixP->fx_r_type != BFD_RELOC_16_PCREL_S2
10903 || fixP->fx_addsy == NULL /* ??? */
10904 || ! S_IS_DEFINED (fixP->fx_addsy)))
bb2d6cd7 10905 value -= fixP->fx_frag->fr_address + fixP->fx_where;
bdaaa2e1 10906
98aa84af 10907 value = (offsetT) value >> 2;
252b5132
RH
10908
10909 /* update old instruction data */
874e8986 10910 buf = (bfd_byte *) (fixP->fx_where + fixP->fx_frag->fr_literal);
252b5132
RH
10911 if (target_big_endian)
10912 insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
10913 else
10914 insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
10915
98aa84af 10916 if (value + 0x8000 <= 0xffff)
252b5132
RH
10917 insn |= value & 0xffff;
10918 else
10919 {
10920 /* The branch offset is too large. If this is an
10921 unconditional branch, and we are not generating PIC code,
10922 we can convert it to an absolute jump instruction. */
10923 if (mips_pic == NO_PIC
10924 && fixP->fx_done
10925 && fixP->fx_frag->fr_address >= text_section->vma
10926 && (fixP->fx_frag->fr_address
10927 < text_section->vma + text_section->_raw_size)
10928 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
10929 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
10930 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
10931 {
10932 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
10933 insn = 0x0c000000; /* jal */
10934 else
10935 insn = 0x08000000; /* j */
10936 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
10937 fixP->fx_done = 0;
10938 fixP->fx_addsy = section_symbol (text_section);
10939 fixP->fx_addnumber = (value << 2) + md_pcrel_from (fixP);
10940 }
10941 else
10942 {
10943 /* FIXME. It would be possible in principle to handle
10944 conditional branches which overflow. They could be
10945 transformed into a branch around a jump. This would
10946 require setting up variant frags for each different
10947 branch type. The native MIPS assembler attempts to
10948 handle these cases, but it appears to do it
10949 incorrectly. */
10950 as_bad_where (fixP->fx_file, fixP->fx_line,
10951 _("Branch out of range"));
10952 }
10953 }
10954
10955 md_number_to_chars ((char *) buf, (valueT) insn, 4);
10956 break;
10957
10958 case BFD_RELOC_VTABLE_INHERIT:
10959 fixP->fx_done = 0;
10960 if (fixP->fx_addsy
10961 && !S_IS_DEFINED (fixP->fx_addsy)
10962 && !S_IS_WEAK (fixP->fx_addsy))
10963 S_SET_WEAK (fixP->fx_addsy);
10964 break;
10965
10966 case BFD_RELOC_VTABLE_ENTRY:
10967 fixP->fx_done = 0;
10968 break;
10969
10970 default:
10971 internalError ();
10972 }
252b5132
RH
10973}
10974
10975#if 0
10976void
10977printInsn (oc)
10978 unsigned long oc;
10979{
10980 const struct mips_opcode *p;
10981 int treg, sreg, dreg, shamt;
10982 short imm;
10983 const char *args;
10984 int i;
10985
10986 for (i = 0; i < NUMOPCODES; ++i)
10987 {
10988 p = &mips_opcodes[i];
10989 if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
10990 {
10991 printf ("%08lx %s\t", oc, p->name);
10992 treg = (oc >> 16) & 0x1f;
10993 sreg = (oc >> 21) & 0x1f;
10994 dreg = (oc >> 11) & 0x1f;
10995 shamt = (oc >> 6) & 0x1f;
10996 imm = oc;
10997 for (args = p->args;; ++args)
10998 {
10999 switch (*args)
11000 {
11001 case '\0':
11002 printf ("\n");
11003 break;
11004
11005 case ',':
11006 case '(':
11007 case ')':
11008 printf ("%c", *args);
11009 continue;
11010
11011 case 'r':
11012 assert (treg == sreg);
11013 printf ("$%d,$%d", treg, sreg);
11014 continue;
11015
11016 case 'd':
11017 case 'G':
11018 printf ("$%d", dreg);
11019 continue;
11020
11021 case 't':
11022 case 'E':
11023 printf ("$%d", treg);
11024 continue;
11025
11026 case 'k':
11027 printf ("0x%x", treg);
11028 continue;
11029
11030 case 'b':
11031 case 's':
11032 printf ("$%d", sreg);
11033 continue;
11034
11035 case 'a':
11036 printf ("0x%08lx", oc & 0x1ffffff);
11037 continue;
11038
11039 case 'i':
11040 case 'j':
11041 case 'o':
11042 case 'u':
11043 printf ("%d", imm);
11044 continue;
11045
11046 case '<':
11047 case '>':
11048 printf ("$%d", shamt);
11049 continue;
11050
11051 default:
11052 internalError ();
11053 }
11054 break;
11055 }
11056 return;
11057 }
11058 }
11059 printf (_("%08lx UNDEFINED\n"), oc);
11060}
11061#endif
11062
11063static symbolS *
11064get_symbol ()
11065{
11066 int c;
11067 char *name;
11068 symbolS *p;
11069
11070 name = input_line_pointer;
11071 c = get_symbol_end ();
11072 p = (symbolS *) symbol_find_or_make (name);
11073 *input_line_pointer = c;
11074 return p;
11075}
11076
11077/* Align the current frag to a given power of two. The MIPS assembler
11078 also automatically adjusts any preceding label. */
11079
11080static void
11081mips_align (to, fill, label)
11082 int to;
11083 int fill;
11084 symbolS *label;
11085{
11086 mips_emit_delays (false);
11087 frag_align (to, fill, 0);
11088 record_alignment (now_seg, to);
11089 if (label != NULL)
11090 {
11091 assert (S_GET_SEGMENT (label) == now_seg);
49309057 11092 symbol_set_frag (label, frag_now);
252b5132
RH
11093 S_SET_VALUE (label, (valueT) frag_now_fix ());
11094 }
11095}
11096
11097/* Align to a given power of two. .align 0 turns off the automatic
11098 alignment used by the data creating pseudo-ops. */
11099
11100static void
11101s_align (x)
43841e91 11102 int x ATTRIBUTE_UNUSED;
252b5132
RH
11103{
11104 register int temp;
11105 register long temp_fill;
11106 long max_alignment = 15;
11107
11108 /*
11109
11110 o Note that the assembler pulls down any immediately preceeding label
11111 to the aligned address.
11112 o It's not documented but auto alignment is reinstated by
11113 a .align pseudo instruction.
11114 o Note also that after auto alignment is turned off the mips assembler
11115 issues an error on attempt to assemble an improperly aligned data item.
11116 We don't.
11117
11118 */
11119
11120 temp = get_absolute_expression ();
11121 if (temp > max_alignment)
11122 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
11123 else if (temp < 0)
11124 {
11125 as_warn (_("Alignment negative: 0 assumed."));
11126 temp = 0;
11127 }
11128 if (*input_line_pointer == ',')
11129 {
f9419b05 11130 ++input_line_pointer;
252b5132
RH
11131 temp_fill = get_absolute_expression ();
11132 }
11133 else
11134 temp_fill = 0;
11135 if (temp)
11136 {
11137 auto_align = 1;
11138 mips_align (temp, (int) temp_fill,
11139 insn_labels != NULL ? insn_labels->label : NULL);
11140 }
11141 else
11142 {
11143 auto_align = 0;
11144 }
11145
11146 demand_empty_rest_of_line ();
11147}
11148
11149void
11150mips_flush_pending_output ()
11151{
11152 mips_emit_delays (false);
11153 mips_clear_insn_labels ();
11154}
11155
11156static void
11157s_change_sec (sec)
11158 int sec;
11159{
11160 segT seg;
11161
11162 /* When generating embedded PIC code, we only use the .text, .lit8,
11163 .sdata and .sbss sections. We change the .data and .rdata
11164 pseudo-ops to use .sdata. */
11165 if (mips_pic == EMBEDDED_PIC
11166 && (sec == 'd' || sec == 'r'))
11167 sec = 's';
11168
11169#ifdef OBJ_ELF
11170 /* The ELF backend needs to know that we are changing sections, so
11171 that .previous works correctly. We could do something like check
b6ff326e 11172 for an obj_section_change_hook macro, but that might be confusing
252b5132
RH
11173 as it would not be appropriate to use it in the section changing
11174 functions in read.c, since obj-elf.c intercepts those. FIXME:
11175 This should be cleaner, somehow. */
11176 obj_elf_section_change_hook ();
11177#endif
11178
11179 mips_emit_delays (false);
11180 switch (sec)
11181 {
11182 case 't':
11183 s_text (0);
11184 break;
11185 case 'd':
11186 s_data (0);
11187 break;
11188 case 'b':
11189 subseg_set (bss_section, (subsegT) get_absolute_expression ());
11190 demand_empty_rest_of_line ();
11191 break;
11192
11193 case 'r':
11194 if (USE_GLOBAL_POINTER_OPT)
11195 {
11196 seg = subseg_new (RDATA_SECTION_NAME,
11197 (subsegT) get_absolute_expression ());
11198 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11199 {
11200 bfd_set_section_flags (stdoutput, seg,
11201 (SEC_ALLOC
11202 | SEC_LOAD
11203 | SEC_READONLY
11204 | SEC_RELOC
11205 | SEC_DATA));
11206 if (strcmp (TARGET_OS, "elf") != 0)
e799a695 11207 record_alignment (seg, 4);
252b5132
RH
11208 }
11209 demand_empty_rest_of_line ();
11210 }
11211 else
11212 {
11213 as_bad (_("No read only data section in this object file format"));
11214 demand_empty_rest_of_line ();
11215 return;
11216 }
11217 break;
11218
11219 case 's':
11220 if (USE_GLOBAL_POINTER_OPT)
11221 {
11222 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
11223 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11224 {
11225 bfd_set_section_flags (stdoutput, seg,
11226 SEC_ALLOC | SEC_LOAD | SEC_RELOC
11227 | SEC_DATA);
11228 if (strcmp (TARGET_OS, "elf") != 0)
e799a695 11229 record_alignment (seg, 4);
252b5132
RH
11230 }
11231 demand_empty_rest_of_line ();
11232 break;
11233 }
11234 else
11235 {
11236 as_bad (_("Global pointers not supported; recompile -G 0"));
11237 demand_empty_rest_of_line ();
11238 return;
11239 }
11240 }
11241
11242 auto_align = 1;
11243}
11244
11245void
11246mips_enable_auto_align ()
11247{
11248 auto_align = 1;
11249}
11250
11251static void
11252s_cons (log_size)
11253 int log_size;
11254{
11255 symbolS *label;
11256
11257 label = insn_labels != NULL ? insn_labels->label : NULL;
11258 mips_emit_delays (false);
11259 if (log_size > 0 && auto_align)
11260 mips_align (log_size, 0, label);
11261 mips_clear_insn_labels ();
11262 cons (1 << log_size);
11263}
11264
11265static void
11266s_float_cons (type)
11267 int type;
11268{
11269 symbolS *label;
11270
11271 label = insn_labels != NULL ? insn_labels->label : NULL;
11272
11273 mips_emit_delays (false);
11274
11275 if (auto_align)
49309057
ILT
11276 {
11277 if (type == 'd')
11278 mips_align (3, 0, label);
11279 else
11280 mips_align (2, 0, label);
11281 }
252b5132
RH
11282
11283 mips_clear_insn_labels ();
11284
11285 float_cons (type);
11286}
11287
11288/* Handle .globl. We need to override it because on Irix 5 you are
11289 permitted to say
11290 .globl foo .text
11291 where foo is an undefined symbol, to mean that foo should be
11292 considered to be the address of a function. */
11293
11294static void
11295s_mips_globl (x)
43841e91 11296 int x ATTRIBUTE_UNUSED;
252b5132
RH
11297{
11298 char *name;
11299 int c;
11300 symbolS *symbolP;
11301 flagword flag;
11302
11303 name = input_line_pointer;
11304 c = get_symbol_end ();
11305 symbolP = symbol_find_or_make (name);
11306 *input_line_pointer = c;
11307 SKIP_WHITESPACE ();
11308
11309 /* On Irix 5, every global symbol that is not explicitly labelled as
11310 being a function is apparently labelled as being an object. */
11311 flag = BSF_OBJECT;
11312
11313 if (! is_end_of_line[(unsigned char) *input_line_pointer])
11314 {
11315 char *secname;
11316 asection *sec;
11317
11318 secname = input_line_pointer;
11319 c = get_symbol_end ();
11320 sec = bfd_get_section_by_name (stdoutput, secname);
11321 if (sec == NULL)
11322 as_bad (_("%s: no such section"), secname);
11323 *input_line_pointer = c;
11324
11325 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
11326 flag = BSF_FUNCTION;
11327 }
11328
49309057 11329 symbol_get_bfdsym (symbolP)->flags |= flag;
252b5132
RH
11330
11331 S_SET_EXTERNAL (symbolP);
11332 demand_empty_rest_of_line ();
11333}
11334
11335static void
11336s_option (x)
43841e91 11337 int x ATTRIBUTE_UNUSED;
252b5132
RH
11338{
11339 char *opt;
11340 char c;
11341
11342 opt = input_line_pointer;
11343 c = get_symbol_end ();
11344
11345 if (*opt == 'O')
11346 {
11347 /* FIXME: What does this mean? */
11348 }
11349 else if (strncmp (opt, "pic", 3) == 0)
11350 {
11351 int i;
11352
11353 i = atoi (opt + 3);
11354 if (i == 0)
11355 mips_pic = NO_PIC;
11356 else if (i == 2)
11357 mips_pic = SVR4_PIC;
11358 else
11359 as_bad (_(".option pic%d not supported"), i);
11360
11361 if (USE_GLOBAL_POINTER_OPT && mips_pic == SVR4_PIC)
11362 {
11363 if (g_switch_seen && g_switch_value != 0)
11364 as_warn (_("-G may not be used with SVR4 PIC code"));
11365 g_switch_value = 0;
11366 bfd_set_gp_size (stdoutput, 0);
11367 }
11368 }
11369 else
11370 as_warn (_("Unrecognized option \"%s\""), opt);
11371
11372 *input_line_pointer = c;
11373 demand_empty_rest_of_line ();
11374}
11375
11376/* This structure is used to hold a stack of .set values. */
11377
e972090a
NC
11378struct mips_option_stack
11379{
252b5132
RH
11380 struct mips_option_stack *next;
11381 struct mips_set_options options;
11382};
11383
11384static struct mips_option_stack *mips_opts_stack;
11385
11386/* Handle the .set pseudo-op. */
11387
11388static void
11389s_mipsset (x)
43841e91 11390 int x ATTRIBUTE_UNUSED;
252b5132
RH
11391{
11392 char *name = input_line_pointer, ch;
11393
11394 while (!is_end_of_line[(unsigned char) *input_line_pointer])
f9419b05 11395 ++input_line_pointer;
252b5132
RH
11396 ch = *input_line_pointer;
11397 *input_line_pointer = '\0';
11398
11399 if (strcmp (name, "reorder") == 0)
11400 {
11401 if (mips_opts.noreorder && prev_nop_frag != NULL)
11402 {
11403 /* If we still have pending nops, we can discard them. The
11404 usual nop handling will insert any that are still
bdaaa2e1 11405 needed. */
252b5132
RH
11406 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11407 * (mips_opts.mips16 ? 2 : 4));
11408 prev_nop_frag = NULL;
11409 }
11410 mips_opts.noreorder = 0;
11411 }
11412 else if (strcmp (name, "noreorder") == 0)
11413 {
11414 mips_emit_delays (true);
11415 mips_opts.noreorder = 1;
11416 mips_any_noreorder = 1;
11417 }
11418 else if (strcmp (name, "at") == 0)
11419 {
11420 mips_opts.noat = 0;
11421 }
11422 else if (strcmp (name, "noat") == 0)
11423 {
11424 mips_opts.noat = 1;
11425 }
11426 else if (strcmp (name, "macro") == 0)
11427 {
11428 mips_opts.warn_about_macros = 0;
11429 }
11430 else if (strcmp (name, "nomacro") == 0)
11431 {
11432 if (mips_opts.noreorder == 0)
11433 as_bad (_("`noreorder' must be set before `nomacro'"));
11434 mips_opts.warn_about_macros = 1;
11435 }
11436 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
11437 {
11438 mips_opts.nomove = 0;
11439 }
11440 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
11441 {
11442 mips_opts.nomove = 1;
11443 }
11444 else if (strcmp (name, "bopt") == 0)
11445 {
11446 mips_opts.nobopt = 0;
11447 }
11448 else if (strcmp (name, "nobopt") == 0)
11449 {
11450 mips_opts.nobopt = 1;
11451 }
11452 else if (strcmp (name, "mips16") == 0
11453 || strcmp (name, "MIPS-16") == 0)
11454 mips_opts.mips16 = 1;
11455 else if (strcmp (name, "nomips16") == 0
11456 || strcmp (name, "noMIPS-16") == 0)
11457 mips_opts.mips16 = 0;
1f25f5d3
CD
11458 else if (strcmp (name, "mips3d") == 0)
11459 mips_opts.ase_mips3d = 1;
11460 else if (strcmp (name, "nomips3d") == 0)
11461 mips_opts.ase_mips3d = 0;
a4672219
TS
11462 else if (strcmp (name, "mdmx") == 0)
11463 mips_opts.ase_mdmx = 1;
11464 else if (strcmp (name, "nomdmx") == 0)
11465 mips_opts.ase_mdmx = 0;
252b5132
RH
11466 else if (strncmp (name, "mips", 4) == 0)
11467 {
11468 int isa;
11469
11470 /* Permit the user to change the ISA on the fly. Needless to
11471 say, misuse can cause serious problems. */
11472 isa = atoi (name + 4);
553178e4 11473 switch (isa)
98d3f06f
KH
11474 {
11475 case 0:
11476 mips_opts.gp32 = file_mips_gp32;
11477 mips_opts.fp32 = file_mips_fp32;
98d3f06f
KH
11478 break;
11479 case 1:
11480 case 2:
11481 case 32:
11482 mips_opts.gp32 = 1;
11483 mips_opts.fp32 = 1;
11484 break;
11485 case 3:
11486 case 4:
11487 case 5:
11488 case 64:
98d3f06f
KH
11489 mips_opts.gp32 = 0;
11490 mips_opts.fp32 = 0;
11491 break;
11492 default:
11493 as_bad (_("unknown ISA level %s"), name + 4);
11494 break;
11495 }
553178e4 11496
e7af610e 11497 switch (isa)
98d3f06f
KH
11498 {
11499 case 0: mips_opts.isa = file_mips_isa; break;
11500 case 1: mips_opts.isa = ISA_MIPS1; break;
11501 case 2: mips_opts.isa = ISA_MIPS2; break;
11502 case 3: mips_opts.isa = ISA_MIPS3; break;
11503 case 4: mips_opts.isa = ISA_MIPS4; break;
11504 case 5: mips_opts.isa = ISA_MIPS5; break;
11505 case 32: mips_opts.isa = ISA_MIPS32; break;
11506 case 64: mips_opts.isa = ISA_MIPS64; break;
11507 default: as_bad (_("unknown ISA level %s"), name + 4); break;
11508 }
252b5132
RH
11509 }
11510 else if (strcmp (name, "autoextend") == 0)
11511 mips_opts.noautoextend = 0;
11512 else if (strcmp (name, "noautoextend") == 0)
11513 mips_opts.noautoextend = 1;
11514 else if (strcmp (name, "push") == 0)
11515 {
11516 struct mips_option_stack *s;
11517
11518 s = (struct mips_option_stack *) xmalloc (sizeof *s);
11519 s->next = mips_opts_stack;
11520 s->options = mips_opts;
11521 mips_opts_stack = s;
11522 }
11523 else if (strcmp (name, "pop") == 0)
11524 {
11525 struct mips_option_stack *s;
11526
11527 s = mips_opts_stack;
11528 if (s == NULL)
11529 as_bad (_(".set pop with no .set push"));
11530 else
11531 {
11532 /* If we're changing the reorder mode we need to handle
11533 delay slots correctly. */
11534 if (s->options.noreorder && ! mips_opts.noreorder)
11535 mips_emit_delays (true);
11536 else if (! s->options.noreorder && mips_opts.noreorder)
11537 {
11538 if (prev_nop_frag != NULL)
11539 {
11540 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11541 * (mips_opts.mips16 ? 2 : 4));
11542 prev_nop_frag = NULL;
11543 }
11544 }
11545
11546 mips_opts = s->options;
11547 mips_opts_stack = s->next;
11548 free (s);
11549 }
11550 }
11551 else
11552 {
11553 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
11554 }
11555 *input_line_pointer = ch;
11556 demand_empty_rest_of_line ();
11557}
11558
11559/* Handle the .abicalls pseudo-op. I believe this is equivalent to
11560 .option pic2. It means to generate SVR4 PIC calls. */
11561
11562static void
11563s_abicalls (ignore)
43841e91 11564 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
11565{
11566 mips_pic = SVR4_PIC;
11567 if (USE_GLOBAL_POINTER_OPT)
11568 {
11569 if (g_switch_seen && g_switch_value != 0)
11570 as_warn (_("-G may not be used with SVR4 PIC code"));
11571 g_switch_value = 0;
11572 }
11573 bfd_set_gp_size (stdoutput, 0);
11574 demand_empty_rest_of_line ();
11575}
11576
11577/* Handle the .cpload pseudo-op. This is used when generating SVR4
11578 PIC code. It sets the $gp register for the function based on the
11579 function address, which is in the register named in the argument.
11580 This uses a relocation against _gp_disp, which is handled specially
11581 by the linker. The result is:
11582 lui $gp,%hi(_gp_disp)
11583 addiu $gp,$gp,%lo(_gp_disp)
11584 addu $gp,$gp,.cpload argument
11585 The .cpload argument is normally $25 == $t9. */
11586
11587static void
11588s_cpload (ignore)
43841e91 11589 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
11590{
11591 expressionS ex;
11592 int icnt = 0;
11593
6478892d
TS
11594 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
11595 .cpload is ignored. */
11596 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
11597 {
11598 s_ignore (0);
11599 return;
11600 }
11601
d3ecfc59 11602 /* .cpload should be in a .set noreorder section. */
252b5132
RH
11603 if (mips_opts.noreorder == 0)
11604 as_warn (_(".cpload not in noreorder section"));
11605
11606 ex.X_op = O_symbol;
11607 ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
11608 ex.X_op_symbol = NULL;
11609 ex.X_add_number = 0;
11610
11611 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
49309057 11612 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
252b5132 11613
c9914766
TS
11614 macro_build_lui (NULL, &icnt, &ex, mips_gp_register);
11615 macro_build ((char *) NULL, &icnt, &ex, "addiu", "t,r,j",
11616 mips_gp_register, mips_gp_register, (int) BFD_RELOC_LO16);
252b5132
RH
11617
11618 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "addu", "d,v,t",
c9914766 11619 mips_gp_register, mips_gp_register, tc_get_register (0));
252b5132
RH
11620
11621 demand_empty_rest_of_line ();
11622}
11623
6478892d
TS
11624/* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
11625 .cpsetup $reg1, offset|$reg2, label
11626
11627 If offset is given, this results in:
11628 sd $gp, offset($sp)
956cd1d6 11629 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
11630 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
11631 daddu $gp, $gp, $reg1
6478892d
TS
11632
11633 If $reg2 is given, this results in:
11634 daddu $reg2, $gp, $0
956cd1d6 11635 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
11636 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
11637 daddu $gp, $gp, $reg1
11638 $reg1 is normally $25 == $t9. */
6478892d
TS
11639static void
11640s_cpsetup (ignore)
11641 int ignore ATTRIBUTE_UNUSED;
11642{
11643 expressionS ex_off;
11644 expressionS ex_sym;
11645 int reg1;
11646 int icnt = 0;
11647 char *sym;
11648
8586fc66 11649 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
6478892d
TS
11650 We also need NewABI support. */
11651 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11652 {
11653 s_ignore (0);
11654 return;
11655 }
11656
11657 reg1 = tc_get_register (0);
11658 SKIP_WHITESPACE ();
11659 if (*input_line_pointer != ',')
11660 {
11661 as_bad (_("missing argument separator ',' for .cpsetup"));
11662 return;
11663 }
11664 else
80245285 11665 ++input_line_pointer;
6478892d
TS
11666 SKIP_WHITESPACE ();
11667 if (*input_line_pointer == '$')
80245285
TS
11668 {
11669 mips_cpreturn_register = tc_get_register (0);
11670 mips_cpreturn_offset = -1;
11671 }
6478892d 11672 else
80245285
TS
11673 {
11674 mips_cpreturn_offset = get_absolute_expression ();
11675 mips_cpreturn_register = -1;
11676 }
6478892d
TS
11677 SKIP_WHITESPACE ();
11678 if (*input_line_pointer != ',')
11679 {
11680 as_bad (_("missing argument separator ',' for .cpsetup"));
11681 return;
11682 }
11683 else
f9419b05 11684 ++input_line_pointer;
6478892d
TS
11685 SKIP_WHITESPACE ();
11686 sym = input_line_pointer;
11687 while (ISALNUM (*input_line_pointer))
f9419b05 11688 ++input_line_pointer;
6478892d
TS
11689 *input_line_pointer = 0;
11690
11691 ex_sym.X_op = O_symbol;
11692 ex_sym.X_add_symbol = symbol_find_or_make (sym);
11693 ex_sym.X_op_symbol = NULL;
11694 ex_sym.X_add_number = 0;
11695
11696 if (mips_cpreturn_register == -1)
11697 {
11698 ex_off.X_op = O_constant;
11699 ex_off.X_add_symbol = NULL;
11700 ex_off.X_op_symbol = NULL;
11701 ex_off.X_add_number = mips_cpreturn_offset;
11702
11703 macro_build ((char *) NULL, &icnt, &ex_off, "sd", "t,o(b)",
11704 mips_gp_register, (int) BFD_RELOC_LO16, SP);
11705 }
11706 else
11707 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
11708 "d,v,t", mips_cpreturn_register, mips_gp_register, 0);
11709
11710 macro_build ((char *) NULL, &icnt, &ex_sym, "lui", "t,u", mips_gp_register,
11711 (int) BFD_RELOC_GPREL16);
8586fc66
TS
11712 fix_new (frag_now, prev_insn_where, 0, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
11713 fix_new (frag_now, prev_insn_where, 0, NULL, 0, 0, BFD_RELOC_HI16_S);
6478892d
TS
11714 macro_build ((char *) NULL, &icnt, &ex_sym, "addiu", "t,r,j",
11715 mips_gp_register, mips_gp_register, (int) BFD_RELOC_GPREL16);
8586fc66
TS
11716 fix_new (frag_now, prev_insn_where, 0, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
11717 fix_new (frag_now, prev_insn_where, 0, NULL, 0, 0, BFD_RELOC_LO16);
11718 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
11719 HAVE_64BIT_ADDRESSES ? "daddu" : "addu", "d,v,t",
11720 mips_gp_register, mips_gp_register, reg1);
6478892d
TS
11721
11722 demand_empty_rest_of_line ();
11723}
11724
11725static void
11726s_cplocal (ignore)
11727 int ignore ATTRIBUTE_UNUSED;
11728{
11729 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
11730 .cplocal is ignored. */
11731 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11732 {
11733 s_ignore (0);
11734 return;
11735 }
11736
11737 mips_gp_register = tc_get_register (0);
85b51719 11738 demand_empty_rest_of_line ();
6478892d
TS
11739}
11740
252b5132
RH
11741/* Handle the .cprestore pseudo-op. This stores $gp into a given
11742 offset from $sp. The offset is remembered, and after making a PIC
11743 call $gp is restored from that location. */
11744
11745static void
11746s_cprestore (ignore)
43841e91 11747 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
11748{
11749 expressionS ex;
11750 int icnt = 0;
11751
6478892d 11752 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
c9914766 11753 .cprestore is ignored. */
6478892d 11754 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
11755 {
11756 s_ignore (0);
11757 return;
11758 }
11759
11760 mips_cprestore_offset = get_absolute_expression ();
7a621144 11761 mips_cprestore_valid = 1;
252b5132
RH
11762
11763 ex.X_op = O_constant;
11764 ex.X_add_symbol = NULL;
11765 ex.X_op_symbol = NULL;
11766 ex.X_add_number = mips_cprestore_offset;
11767
c9914766
TS
11768 macro_build ((char *) NULL, &icnt, &ex, HAVE_32BIT_ADDRESSES ? "sw" : "sd",
11769 "t,o(b)", mips_gp_register, (int) BFD_RELOC_LO16, SP);
252b5132
RH
11770
11771 demand_empty_rest_of_line ();
11772}
11773
6478892d
TS
11774/* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
11775 was given in the preceeding .gpsetup, it results in:
11776 ld $gp, offset($sp)
76b3015f 11777
6478892d
TS
11778 If a register $reg2 was given there, it results in:
11779 daddiu $gp, $gp, $reg2
11780 */
11781static void
11782s_cpreturn (ignore)
11783 int ignore ATTRIBUTE_UNUSED;
11784{
11785 expressionS ex;
11786 int icnt = 0;
11787
11788 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
11789 We also need NewABI support. */
11790 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11791 {
11792 s_ignore (0);
11793 return;
11794 }
11795
11796 if (mips_cpreturn_register == -1)
11797 {
11798 ex.X_op = O_constant;
11799 ex.X_add_symbol = NULL;
11800 ex.X_op_symbol = NULL;
11801 ex.X_add_number = mips_cpreturn_offset;
11802
11803 macro_build ((char *) NULL, &icnt, &ex, "ld", "t,o(b)",
11804 mips_gp_register, (int) BFD_RELOC_LO16, SP);
11805 }
11806 else
11807 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
11808 "d,v,t", mips_gp_register, mips_cpreturn_register, 0);
11809
11810 demand_empty_rest_of_line ();
11811}
11812
11813/* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
11814 code. It sets the offset to use in gp_rel relocations. */
11815
11816static void
11817s_gpvalue (ignore)
11818 int ignore ATTRIBUTE_UNUSED;
11819{
11820 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
11821 We also need NewABI support. */
11822 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11823 {
11824 s_ignore (0);
11825 return;
11826 }
11827
def2e0dd 11828 mips_gprel_offset = get_absolute_expression ();
6478892d
TS
11829
11830 demand_empty_rest_of_line ();
11831}
11832
252b5132
RH
11833/* Handle the .gpword pseudo-op. This is used when generating PIC
11834 code. It generates a 32 bit GP relative reloc. */
11835
11836static void
11837s_gpword (ignore)
43841e91 11838 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
11839{
11840 symbolS *label;
11841 expressionS ex;
11842 char *p;
11843
11844 /* When not generating PIC code, this is treated as .word. */
11845 if (mips_pic != SVR4_PIC)
11846 {
11847 s_cons (2);
11848 return;
11849 }
11850
11851 label = insn_labels != NULL ? insn_labels->label : NULL;
11852 mips_emit_delays (true);
11853 if (auto_align)
11854 mips_align (2, 0, label);
11855 mips_clear_insn_labels ();
11856
11857 expression (&ex);
11858
11859 if (ex.X_op != O_symbol || ex.X_add_number != 0)
11860 {
11861 as_bad (_("Unsupported use of .gpword"));
11862 ignore_rest_of_line ();
11863 }
11864
11865 p = frag_more (4);
11866 md_number_to_chars (p, (valueT) 0, 4);
c9914766 11867 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, false,
cdf6fd85 11868 BFD_RELOC_GPREL32);
252b5132
RH
11869
11870 demand_empty_rest_of_line ();
11871}
11872
11873/* Handle the .cpadd pseudo-op. This is used when dealing with switch
11874 tables in SVR4 PIC code. */
11875
11876static void
11877s_cpadd (ignore)
43841e91 11878 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
11879{
11880 int icnt = 0;
11881 int reg;
11882
6478892d
TS
11883 /* This is ignored when not generating SVR4 PIC code or if this is NewABI
11884 code. */
11885 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
11886 {
11887 s_ignore (0);
11888 return;
11889 }
11890
11891 /* Add $gp to the register named as an argument. */
11892 reg = tc_get_register (0);
11893 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 11894 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 11895 "d,v,t", reg, reg, mips_gp_register);
252b5132 11896
bdaaa2e1 11897 demand_empty_rest_of_line ();
252b5132
RH
11898}
11899
11900/* Handle the .insn pseudo-op. This marks instruction labels in
11901 mips16 mode. This permits the linker to handle them specially,
11902 such as generating jalx instructions when needed. We also make
11903 them odd for the duration of the assembly, in order to generate the
11904 right sort of code. We will make them even in the adjust_symtab
11905 routine, while leaving them marked. This is convenient for the
11906 debugger and the disassembler. The linker knows to make them odd
11907 again. */
11908
11909static void
11910s_insn (ignore)
43841e91 11911 int ignore ATTRIBUTE_UNUSED;
252b5132 11912{
f9419b05 11913 mips16_mark_labels ();
252b5132
RH
11914
11915 demand_empty_rest_of_line ();
11916}
11917
11918/* Handle a .stabn directive. We need these in order to mark a label
11919 as being a mips16 text label correctly. Sometimes the compiler
11920 will emit a label, followed by a .stabn, and then switch sections.
11921 If the label and .stabn are in mips16 mode, then the label is
11922 really a mips16 text label. */
11923
11924static void
11925s_mips_stab (type)
11926 int type;
11927{
f9419b05 11928 if (type == 'n')
252b5132
RH
11929 mips16_mark_labels ();
11930
11931 s_stab (type);
11932}
11933
11934/* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
11935 */
11936
11937static void
11938s_mips_weakext (ignore)
43841e91 11939 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
11940{
11941 char *name;
11942 int c;
11943 symbolS *symbolP;
11944 expressionS exp;
11945
11946 name = input_line_pointer;
11947 c = get_symbol_end ();
11948 symbolP = symbol_find_or_make (name);
11949 S_SET_WEAK (symbolP);
11950 *input_line_pointer = c;
11951
11952 SKIP_WHITESPACE ();
11953
11954 if (! is_end_of_line[(unsigned char) *input_line_pointer])
11955 {
11956 if (S_IS_DEFINED (symbolP))
11957 {
956cd1d6 11958 as_bad ("ignoring attempt to redefine symbol %s",
252b5132
RH
11959 S_GET_NAME (symbolP));
11960 ignore_rest_of_line ();
11961 return;
11962 }
bdaaa2e1 11963
252b5132
RH
11964 if (*input_line_pointer == ',')
11965 {
11966 ++input_line_pointer;
11967 SKIP_WHITESPACE ();
11968 }
bdaaa2e1 11969
252b5132
RH
11970 expression (&exp);
11971 if (exp.X_op != O_symbol)
11972 {
11973 as_bad ("bad .weakext directive");
98d3f06f 11974 ignore_rest_of_line ();
252b5132
RH
11975 return;
11976 }
49309057 11977 symbol_set_value_expression (symbolP, &exp);
252b5132
RH
11978 }
11979
11980 demand_empty_rest_of_line ();
11981}
11982
11983/* Parse a register string into a number. Called from the ECOFF code
11984 to parse .frame. The argument is non-zero if this is the frame
11985 register, so that we can record it in mips_frame_reg. */
11986
11987int
11988tc_get_register (frame)
11989 int frame;
11990{
11991 int reg;
11992
11993 SKIP_WHITESPACE ();
11994 if (*input_line_pointer++ != '$')
11995 {
11996 as_warn (_("expected `$'"));
85b51719 11997 reg = ZERO;
252b5132 11998 }
3882b010 11999 else if (ISDIGIT (*input_line_pointer))
252b5132
RH
12000 {
12001 reg = get_absolute_expression ();
12002 if (reg < 0 || reg >= 32)
12003 {
12004 as_warn (_("Bad register number"));
85b51719 12005 reg = ZERO;
252b5132
RH
12006 }
12007 }
12008 else
12009 {
76db943d 12010 if (strncmp (input_line_pointer, "ra", 2) == 0)
85b51719
TS
12011 {
12012 reg = RA;
12013 input_line_pointer += 2;
12014 }
76db943d 12015 else if (strncmp (input_line_pointer, "fp", 2) == 0)
85b51719
TS
12016 {
12017 reg = FP;
12018 input_line_pointer += 2;
12019 }
252b5132 12020 else if (strncmp (input_line_pointer, "sp", 2) == 0)
85b51719
TS
12021 {
12022 reg = SP;
12023 input_line_pointer += 2;
12024 }
252b5132 12025 else if (strncmp (input_line_pointer, "gp", 2) == 0)
85b51719
TS
12026 {
12027 reg = GP;
12028 input_line_pointer += 2;
12029 }
252b5132 12030 else if (strncmp (input_line_pointer, "at", 2) == 0)
85b51719
TS
12031 {
12032 reg = AT;
12033 input_line_pointer += 2;
12034 }
12035 else if (strncmp (input_line_pointer, "kt0", 3) == 0)
12036 {
12037 reg = KT0;
12038 input_line_pointer += 3;
12039 }
12040 else if (strncmp (input_line_pointer, "kt1", 3) == 0)
12041 {
12042 reg = KT1;
12043 input_line_pointer += 3;
12044 }
12045 else if (strncmp (input_line_pointer, "zero", 4) == 0)
12046 {
12047 reg = ZERO;
12048 input_line_pointer += 4;
12049 }
252b5132
RH
12050 else
12051 {
12052 as_warn (_("Unrecognized register name"));
85b51719
TS
12053 reg = ZERO;
12054 while (ISALNUM(*input_line_pointer))
12055 input_line_pointer++;
252b5132 12056 }
252b5132
RH
12057 }
12058 if (frame)
7a621144
DJ
12059 {
12060 mips_frame_reg = reg != 0 ? reg : SP;
12061 mips_frame_reg_valid = 1;
12062 mips_cprestore_valid = 0;
12063 }
252b5132
RH
12064 return reg;
12065}
12066
12067valueT
12068md_section_align (seg, addr)
12069 asection *seg;
12070 valueT addr;
12071{
12072 int align = bfd_get_section_alignment (stdoutput, seg);
12073
12074#ifdef OBJ_ELF
12075 /* We don't need to align ELF sections to the full alignment.
12076 However, Irix 5 may prefer that we align them at least to a 16
12077 byte boundary. We don't bother to align the sections if we are
12078 targeted for an embedded system. */
12079 if (strcmp (TARGET_OS, "elf") == 0)
12080 return addr;
12081 if (align > 4)
12082 align = 4;
12083#endif
12084
12085 return ((addr + (1 << align) - 1) & (-1 << align));
12086}
12087
12088/* Utility routine, called from above as well. If called while the
12089 input file is still being read, it's only an approximation. (For
12090 example, a symbol may later become defined which appeared to be
12091 undefined earlier.) */
12092
12093static int
12094nopic_need_relax (sym, before_relaxing)
12095 symbolS *sym;
12096 int before_relaxing;
12097{
12098 if (sym == 0)
12099 return 0;
12100
6478892d 12101 if (USE_GLOBAL_POINTER_OPT && g_switch_value > 0)
252b5132
RH
12102 {
12103 const char *symname;
12104 int change;
12105
c9914766 12106 /* Find out whether this symbol can be referenced off the $gp
252b5132
RH
12107 register. It can be if it is smaller than the -G size or if
12108 it is in the .sdata or .sbss section. Certain symbols can
c9914766 12109 not be referenced off the $gp, although it appears as though
252b5132
RH
12110 they can. */
12111 symname = S_GET_NAME (sym);
12112 if (symname != (const char *) NULL
12113 && (strcmp (symname, "eprol") == 0
12114 || strcmp (symname, "etext") == 0
12115 || strcmp (symname, "_gp") == 0
12116 || strcmp (symname, "edata") == 0
12117 || strcmp (symname, "_fbss") == 0
12118 || strcmp (symname, "_fdata") == 0
12119 || strcmp (symname, "_ftext") == 0
12120 || strcmp (symname, "end") == 0
12121 || strcmp (symname, "_gp_disp") == 0))
12122 change = 1;
12123 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
12124 && (0
12125#ifndef NO_ECOFF_DEBUGGING
49309057
ILT
12126 || (symbol_get_obj (sym)->ecoff_extern_size != 0
12127 && (symbol_get_obj (sym)->ecoff_extern_size
12128 <= g_switch_value))
252b5132
RH
12129#endif
12130 /* We must defer this decision until after the whole
12131 file has been read, since there might be a .extern
12132 after the first use of this symbol. */
12133 || (before_relaxing
12134#ifndef NO_ECOFF_DEBUGGING
49309057 12135 && symbol_get_obj (sym)->ecoff_extern_size == 0
252b5132
RH
12136#endif
12137 && S_GET_VALUE (sym) == 0)
12138 || (S_GET_VALUE (sym) != 0
12139 && S_GET_VALUE (sym) <= g_switch_value)))
12140 change = 0;
12141 else
12142 {
12143 const char *segname;
12144
12145 segname = segment_name (S_GET_SEGMENT (sym));
12146 assert (strcmp (segname, ".lit8") != 0
12147 && strcmp (segname, ".lit4") != 0);
12148 change = (strcmp (segname, ".sdata") != 0
fba2b7f9
GK
12149 && strcmp (segname, ".sbss") != 0
12150 && strncmp (segname, ".sdata.", 7) != 0
12151 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
252b5132
RH
12152 }
12153 return change;
12154 }
12155 else
c9914766 12156 /* We are not optimizing for the $gp register. */
252b5132
RH
12157 return 1;
12158}
12159
12160/* Given a mips16 variant frag FRAGP, return non-zero if it needs an
12161 extended opcode. SEC is the section the frag is in. */
12162
12163static int
12164mips16_extended_frag (fragp, sec, stretch)
12165 fragS *fragp;
12166 asection *sec;
12167 long stretch;
12168{
12169 int type;
12170 register const struct mips16_immed_operand *op;
12171 offsetT val;
12172 int mintiny, maxtiny;
12173 segT symsec;
98aa84af 12174 fragS *sym_frag;
252b5132
RH
12175
12176 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
12177 return 0;
12178 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
12179 return 1;
12180
12181 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12182 op = mips16_immed_operands;
12183 while (op->type != type)
12184 {
12185 ++op;
12186 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
12187 }
12188
12189 if (op->unsp)
12190 {
12191 if (type == '<' || type == '>' || type == '[' || type == ']')
12192 {
12193 mintiny = 1;
12194 maxtiny = 1 << op->nbits;
12195 }
12196 else
12197 {
12198 mintiny = 0;
12199 maxtiny = (1 << op->nbits) - 1;
12200 }
12201 }
12202 else
12203 {
12204 mintiny = - (1 << (op->nbits - 1));
12205 maxtiny = (1 << (op->nbits - 1)) - 1;
12206 }
12207
98aa84af 12208 sym_frag = symbol_get_frag (fragp->fr_symbol);
ac62c346 12209 val = S_GET_VALUE (fragp->fr_symbol);
98aa84af 12210 symsec = S_GET_SEGMENT (fragp->fr_symbol);
252b5132
RH
12211
12212 if (op->pcrel)
12213 {
12214 addressT addr;
12215
12216 /* We won't have the section when we are called from
12217 mips_relax_frag. However, we will always have been called
12218 from md_estimate_size_before_relax first. If this is a
12219 branch to a different section, we mark it as such. If SEC is
12220 NULL, and the frag is not marked, then it must be a branch to
12221 the same section. */
12222 if (sec == NULL)
12223 {
12224 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
12225 return 1;
12226 }
12227 else
12228 {
98aa84af 12229 /* Must have been called from md_estimate_size_before_relax. */
252b5132
RH
12230 if (symsec != sec)
12231 {
12232 fragp->fr_subtype =
12233 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12234
12235 /* FIXME: We should support this, and let the linker
12236 catch branches and loads that are out of range. */
12237 as_bad_where (fragp->fr_file, fragp->fr_line,
12238 _("unsupported PC relative reference to different section"));
12239
12240 return 1;
12241 }
98aa84af
AM
12242 if (fragp != sym_frag && sym_frag->fr_address == 0)
12243 /* Assume non-extended on the first relaxation pass.
12244 The address we have calculated will be bogus if this is
12245 a forward branch to another frag, as the forward frag
12246 will have fr_address == 0. */
12247 return 0;
252b5132
RH
12248 }
12249
12250 /* In this case, we know for sure that the symbol fragment is in
98aa84af
AM
12251 the same section. If the relax_marker of the symbol fragment
12252 differs from the relax_marker of this fragment, we have not
12253 yet adjusted the symbol fragment fr_address. We want to add
252b5132
RH
12254 in STRETCH in order to get a better estimate of the address.
12255 This particularly matters because of the shift bits. */
12256 if (stretch != 0
98aa84af 12257 && sym_frag->relax_marker != fragp->relax_marker)
252b5132
RH
12258 {
12259 fragS *f;
12260
12261 /* Adjust stretch for any alignment frag. Note that if have
12262 been expanding the earlier code, the symbol may be
12263 defined in what appears to be an earlier frag. FIXME:
12264 This doesn't handle the fr_subtype field, which specifies
12265 a maximum number of bytes to skip when doing an
12266 alignment. */
98aa84af 12267 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
252b5132
RH
12268 {
12269 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
12270 {
12271 if (stretch < 0)
12272 stretch = - ((- stretch)
12273 & ~ ((1 << (int) f->fr_offset) - 1));
12274 else
12275 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
12276 if (stretch == 0)
12277 break;
12278 }
12279 }
12280 if (f != NULL)
12281 val += stretch;
12282 }
12283
12284 addr = fragp->fr_address + fragp->fr_fix;
12285
12286 /* The base address rules are complicated. The base address of
12287 a branch is the following instruction. The base address of a
12288 PC relative load or add is the instruction itself, but if it
12289 is in a delay slot (in which case it can not be extended) use
12290 the address of the instruction whose delay slot it is in. */
12291 if (type == 'p' || type == 'q')
12292 {
12293 addr += 2;
12294
12295 /* If we are currently assuming that this frag should be
12296 extended, then, the current address is two bytes
bdaaa2e1 12297 higher. */
252b5132
RH
12298 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12299 addr += 2;
12300
12301 /* Ignore the low bit in the target, since it will be set
12302 for a text label. */
12303 if ((val & 1) != 0)
12304 --val;
12305 }
12306 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12307 addr -= 4;
12308 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12309 addr -= 2;
12310
12311 val -= addr & ~ ((1 << op->shift) - 1);
12312
12313 /* Branch offsets have an implicit 0 in the lowest bit. */
12314 if (type == 'p' || type == 'q')
12315 val /= 2;
12316
12317 /* If any of the shifted bits are set, we must use an extended
12318 opcode. If the address depends on the size of this
12319 instruction, this can lead to a loop, so we arrange to always
12320 use an extended opcode. We only check this when we are in
12321 the main relaxation loop, when SEC is NULL. */
12322 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
12323 {
12324 fragp->fr_subtype =
12325 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12326 return 1;
12327 }
12328
12329 /* If we are about to mark a frag as extended because the value
12330 is precisely maxtiny + 1, then there is a chance of an
12331 infinite loop as in the following code:
12332 la $4,foo
12333 .skip 1020
12334 .align 2
12335 foo:
12336 In this case when the la is extended, foo is 0x3fc bytes
12337 away, so the la can be shrunk, but then foo is 0x400 away, so
12338 the la must be extended. To avoid this loop, we mark the
12339 frag as extended if it was small, and is about to become
12340 extended with a value of maxtiny + 1. */
12341 if (val == ((maxtiny + 1) << op->shift)
12342 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
12343 && sec == NULL)
12344 {
12345 fragp->fr_subtype =
12346 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12347 return 1;
12348 }
12349 }
12350 else if (symsec != absolute_section && sec != NULL)
12351 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
12352
12353 if ((val & ((1 << op->shift) - 1)) != 0
12354 || val < (mintiny << op->shift)
12355 || val > (maxtiny << op->shift))
12356 return 1;
12357 else
12358 return 0;
12359}
12360
12361/* Estimate the size of a frag before relaxing. Unless this is the
12362 mips16, we are not really relaxing here, and the final size is
12363 encoded in the subtype information. For the mips16, we have to
12364 decide whether we are using an extended opcode or not. */
12365
252b5132
RH
12366int
12367md_estimate_size_before_relax (fragp, segtype)
12368 fragS *fragp;
12369 asection *segtype;
12370{
43841e91 12371 int change = 0;
8614eeee 12372 boolean linkonce = false;
252b5132
RH
12373
12374 if (RELAX_MIPS16_P (fragp->fr_subtype))
177b4a6a
AO
12375 /* We don't want to modify the EXTENDED bit here; it might get us
12376 into infinite loops. We change it only in mips_relax_frag(). */
12377 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
252b5132
RH
12378
12379 if (mips_pic == NO_PIC)
12380 {
12381 change = nopic_need_relax (fragp->fr_symbol, 0);
12382 }
12383 else if (mips_pic == SVR4_PIC)
12384 {
12385 symbolS *sym;
12386 asection *symsec;
12387
12388 sym = fragp->fr_symbol;
12389
12390 /* Handle the case of a symbol equated to another symbol. */
e0890092 12391 while (symbol_equated_reloc_p (sym))
252b5132
RH
12392 {
12393 symbolS *n;
12394
12395 /* It's possible to get a loop here in a badly written
12396 program. */
49309057 12397 n = symbol_get_value_expression (sym)->X_add_symbol;
252b5132
RH
12398 if (n == sym)
12399 break;
12400 sym = n;
12401 }
12402
12403 symsec = S_GET_SEGMENT (sym);
12404
8614eeee
UC
12405 /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
12406 if (symsec != segtype && ! S_IS_LOCAL (sym))
beae10d5
KH
12407 {
12408 if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
12409 != 0)
12410 linkonce = true;
12411
12412 /* The GNU toolchain uses an extension for ELF: a section
12413 beginning with the magic string .gnu.linkonce is a linkonce
12414 section. */
12415 if (strncmp (segment_name (symsec), ".gnu.linkonce",
12416 sizeof ".gnu.linkonce" - 1) == 0)
12417 linkonce = true;
12418 }
8614eeee 12419
252b5132
RH
12420 /* This must duplicate the test in adjust_reloc_syms. */
12421 change = (symsec != &bfd_und_section
12422 && symsec != &bfd_abs_section
426b0403 12423 && ! bfd_is_com_section (symsec)
8614eeee 12424 && !linkonce
426b0403 12425#ifdef OBJ_ELF
ea4ff978 12426 /* A global or weak symbol is treated as external. */
9151e8bf 12427 && (OUTPUT_FLAVOR != bfd_target_elf_flavour
b25a253c 12428 || (! S_IS_WEAK (sym)
bad9ca53
TS
12429 && (! S_IS_EXTERNAL (sym)
12430 || mips_pic == EMBEDDED_PIC)))
426b0403
AM
12431#endif
12432 );
252b5132
RH
12433 }
12434 else
12435 abort ();
12436
12437 if (change)
12438 {
12439 /* Record the offset to the first reloc in the fr_opcode field.
12440 This lets md_convert_frag and tc_gen_reloc know that the code
12441 must be expanded. */
12442 fragp->fr_opcode = (fragp->fr_literal
12443 + fragp->fr_fix
12444 - RELAX_OLD (fragp->fr_subtype)
12445 + RELAX_RELOC1 (fragp->fr_subtype));
12446 /* FIXME: This really needs as_warn_where. */
12447 if (RELAX_WARN (fragp->fr_subtype))
9a41af64
TS
12448 as_warn (_("AT used after \".set noat\" or macro used after "
12449 "\".set nomacro\""));
12450
12451 return RELAX_NEW (fragp->fr_subtype) - RELAX_OLD (fragp->fr_subtype);
252b5132
RH
12452 }
12453
9a41af64 12454 return 0;
252b5132
RH
12455}
12456
12457/* This is called to see whether a reloc against a defined symbol
12458 should be converted into a reloc against a section. Don't adjust
12459 MIPS16 jump relocations, so we don't have to worry about the format
12460 of the offset in the .o file. Don't adjust relocations against
12461 mips16 symbols, so that the linker can find them if it needs to set
12462 up a stub. */
12463
12464int
12465mips_fix_adjustable (fixp)
12466 fixS *fixp;
12467{
ea4ff978
L
12468#ifdef OBJ_ELF
12469 /* Prevent all adjustments to global symbols. */
46bac6de 12470 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
b25a253c 12471 && mips_pic != EMBEDDED_PIC
bad9ca53 12472 && (S_IS_EXTERNAL (fixp->fx_addsy) || S_IS_WEAK (fixp->fx_addsy)))
ea4ff978
L
12473 return 0;
12474#endif
252b5132
RH
12475 if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
12476 return 0;
12477 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
12478 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12479 return 0;
12480 if (fixp->fx_addsy == NULL)
12481 return 1;
12482#ifdef OBJ_ELF
12483 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
12484 && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
12485 && fixp->fx_subsy == NULL)
12486 return 0;
12487#endif
12488 return 1;
12489}
12490
12491/* Translate internal representation of relocation info to BFD target
12492 format. */
12493
12494arelent **
12495tc_gen_reloc (section, fixp)
43841e91 12496 asection *section ATTRIBUTE_UNUSED;
252b5132
RH
12497 fixS *fixp;
12498{
12499 static arelent *retval[4];
12500 arelent *reloc;
12501 bfd_reloc_code_real_type code;
12502
12503 reloc = retval[0] = (arelent *) xmalloc (sizeof (arelent));
12504 retval[1] = NULL;
12505
49309057
ILT
12506 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
12507 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
12508 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
12509
12510 if (mips_pic == EMBEDDED_PIC
12511 && SWITCH_TABLE (fixp))
12512 {
12513 /* For a switch table entry we use a special reloc. The addend
12514 is actually the difference between the reloc address and the
12515 subtrahend. */
12516 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
12517 if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
12518 as_fatal (_("Double check fx_r_type in tc-mips.c:tc_gen_reloc"));
12519 fixp->fx_r_type = BFD_RELOC_GPREL32;
12520 }
12521 else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16)
12522 {
4514d474
CD
12523 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
12524 reloc->addend = fixp->fx_addnumber;
252b5132 12525 else
4514d474
CD
12526 {
12527 /* We use a special addend for an internal RELLO reloc. */
12528 if (symbol_section_p (fixp->fx_addsy))
12529 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
12530 else
12531 reloc->addend = fixp->fx_addnumber + reloc->address;
12532 }
252b5132
RH
12533 }
12534 else if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
12535 {
12536 assert (fixp->fx_next != NULL
12537 && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
4514d474
CD
12538
12539 /* The reloc is relative to the RELLO; adjust the addend
252b5132 12540 accordingly. */
4514d474
CD
12541 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
12542 reloc->addend = fixp->fx_next->fx_addnumber;
252b5132 12543 else
4514d474
CD
12544 {
12545 /* We use a special addend for an internal RELHI reloc. */
12546 if (symbol_section_p (fixp->fx_addsy))
12547 reloc->addend = (fixp->fx_next->fx_frag->fr_address
12548 + fixp->fx_next->fx_where
12549 - S_GET_VALUE (fixp->fx_subsy));
12550 else
12551 reloc->addend = (fixp->fx_addnumber
12552 + fixp->fx_next->fx_frag->fr_address
12553 + fixp->fx_next->fx_where);
12554 }
252b5132 12555 }
4514d474
CD
12556 else if (fixp->fx_pcrel == 0 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
12557 reloc->addend = fixp->fx_addnumber;
252b5132
RH
12558 else
12559 {
12560 if (OUTPUT_FLAVOR != bfd_target_aout_flavour)
12561 /* A gruesome hack which is a result of the gruesome gas reloc
12562 handling. */
12563 reloc->addend = reloc->address;
12564 else
12565 reloc->addend = -reloc->address;
12566 }
12567
12568 /* If this is a variant frag, we may need to adjust the existing
12569 reloc and generate a new one. */
12570 if (fixp->fx_frag->fr_opcode != NULL
cdf6fd85 12571 && (fixp->fx_r_type == BFD_RELOC_GPREL16
252b5132
RH
12572 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
12573 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL16
12574 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
12575 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16
12576 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
6478892d
TS
12577 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_LO16)
12578 && ! HAVE_NEWABI)
252b5132
RH
12579 {
12580 arelent *reloc2;
12581
12582 assert (! RELAX_MIPS16_P (fixp->fx_frag->fr_subtype));
12583
12584 /* If this is not the last reloc in this frag, then we have two
12585 GPREL relocs, or a GOT_HI16/GOT_LO16 pair, or a
12586 CALL_HI16/CALL_LO16, both of which are being replaced. Let
12587 the second one handle all of them. */
12588 if (fixp->fx_next != NULL
12589 && fixp->fx_frag == fixp->fx_next->fx_frag)
12590 {
cdf6fd85
TS
12591 assert ((fixp->fx_r_type == BFD_RELOC_GPREL16
12592 && fixp->fx_next->fx_r_type == BFD_RELOC_GPREL16)
252b5132
RH
12593 || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
12594 && (fixp->fx_next->fx_r_type
12595 == BFD_RELOC_MIPS_GOT_LO16))
12596 || (fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
12597 && (fixp->fx_next->fx_r_type
12598 == BFD_RELOC_MIPS_CALL_LO16)));
12599 retval[0] = NULL;
12600 return retval;
12601 }
12602
12603 fixp->fx_where = fixp->fx_frag->fr_opcode - fixp->fx_frag->fr_literal;
12604 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
12605 reloc2 = retval[1] = (arelent *) xmalloc (sizeof (arelent));
12606 retval[2] = NULL;
49309057
ILT
12607 reloc2->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
12608 *reloc2->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
12609 reloc2->address = (reloc->address
12610 + (RELAX_RELOC2 (fixp->fx_frag->fr_subtype)
12611 - RELAX_RELOC1 (fixp->fx_frag->fr_subtype)));
12612 reloc2->addend = fixp->fx_addnumber;
12613 reloc2->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO16);
12614 assert (reloc2->howto != NULL);
12615
12616 if (RELAX_RELOC3 (fixp->fx_frag->fr_subtype))
12617 {
12618 arelent *reloc3;
12619
12620 reloc3 = retval[2] = (arelent *) xmalloc (sizeof (arelent));
12621 retval[3] = NULL;
12622 *reloc3 = *reloc2;
12623 reloc3->address += 4;
12624 }
12625
12626 if (mips_pic == NO_PIC)
12627 {
cdf6fd85 12628 assert (fixp->fx_r_type == BFD_RELOC_GPREL16);
252b5132
RH
12629 fixp->fx_r_type = BFD_RELOC_HI16_S;
12630 }
12631 else if (mips_pic == SVR4_PIC)
12632 {
12633 switch (fixp->fx_r_type)
12634 {
12635 default:
12636 abort ();
12637 case BFD_RELOC_MIPS_GOT16:
12638 break;
12639 case BFD_RELOC_MIPS_CALL16:
12640 case BFD_RELOC_MIPS_GOT_LO16:
12641 case BFD_RELOC_MIPS_CALL_LO16:
12642 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
12643 break;
12644 }
12645 }
12646 else
12647 abort ();
12648 }
12649
438c16b8
TS
12650 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
12651 entry to be used in the relocation's section offset. */
12652 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
252b5132
RH
12653 {
12654 reloc->address = reloc->addend;
12655 reloc->addend = 0;
12656 }
12657
12658 /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
12659 fixup_segment converted a non-PC relative reloc into a PC
12660 relative reloc. In such a case, we need to convert the reloc
12661 code. */
12662 code = fixp->fx_r_type;
12663 if (fixp->fx_pcrel)
12664 {
12665 switch (code)
12666 {
12667 case BFD_RELOC_8:
12668 code = BFD_RELOC_8_PCREL;
12669 break;
12670 case BFD_RELOC_16:
12671 code = BFD_RELOC_16_PCREL;
12672 break;
12673 case BFD_RELOC_32:
12674 code = BFD_RELOC_32_PCREL;
12675 break;
12676 case BFD_RELOC_64:
12677 code = BFD_RELOC_64_PCREL;
12678 break;
12679 case BFD_RELOC_8_PCREL:
12680 case BFD_RELOC_16_PCREL:
12681 case BFD_RELOC_32_PCREL:
12682 case BFD_RELOC_64_PCREL:
12683 case BFD_RELOC_16_PCREL_S2:
12684 case BFD_RELOC_PCREL_HI16_S:
12685 case BFD_RELOC_PCREL_LO16:
12686 break;
12687 default:
12688 as_bad_where (fixp->fx_file, fixp->fx_line,
12689 _("Cannot make %s relocation PC relative"),
12690 bfd_get_reloc_code_name (code));
12691 }
12692 }
12693
add55e1f
RS
12694#ifdef OBJ_ELF
12695 /* md_apply_fix3 has a double-subtraction hack to get
12696 bfd_install_relocation to behave nicely. GPREL relocations are
12697 handled correctly without this hack, so undo it here. We can't
12698 stop md_apply_fix3 from subtracting twice in the first place since
12699 the fake addend is required for variant frags above. */
12700 if (fixp->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour
98605598 12701 && (code == BFD_RELOC_GPREL16 || code == BFD_RELOC_MIPS16_GPREL)
add55e1f
RS
12702 && reloc->addend != 0
12703 && mips_need_elf_addend_fixup (fixp))
12704 reloc->addend += S_GET_VALUE (fixp->fx_addsy);
12705#endif
12706
252b5132
RH
12707 /* To support a PC relative reloc when generating embedded PIC code
12708 for ECOFF, we use a Cygnus extension. We check for that here to
12709 make sure that we don't let such a reloc escape normally. */
bb2d6cd7
GK
12710 if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
12711 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
252b5132
RH
12712 && code == BFD_RELOC_16_PCREL_S2
12713 && mips_pic != EMBEDDED_PIC)
12714 reloc->howto = NULL;
12715 else
12716 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
12717
12718 if (reloc->howto == NULL)
12719 {
12720 as_bad_where (fixp->fx_file, fixp->fx_line,
12721 _("Can not represent %s relocation in this object file format"),
12722 bfd_get_reloc_code_name (code));
12723 retval[0] = NULL;
12724 }
12725
12726 return retval;
12727}
12728
12729/* Relax a machine dependent frag. This returns the amount by which
12730 the current size of the frag should change. */
12731
12732int
12733mips_relax_frag (fragp, stretch)
12734 fragS *fragp;
12735 long stretch;
12736{
12737 if (! RELAX_MIPS16_P (fragp->fr_subtype))
12738 return 0;
12739
c4e7957c 12740 if (mips16_extended_frag (fragp, NULL, stretch))
252b5132
RH
12741 {
12742 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12743 return 0;
12744 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
12745 return 2;
12746 }
12747 else
12748 {
12749 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12750 return 0;
12751 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
12752 return -2;
12753 }
12754
12755 return 0;
12756}
12757
12758/* Convert a machine dependent frag. */
12759
12760void
12761md_convert_frag (abfd, asec, fragp)
43841e91 12762 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
12763 segT asec;
12764 fragS *fragp;
12765{
12766 int old, new;
12767 char *fixptr;
12768
12769 if (RELAX_MIPS16_P (fragp->fr_subtype))
12770 {
12771 int type;
12772 register const struct mips16_immed_operand *op;
12773 boolean small, ext;
12774 offsetT val;
12775 bfd_byte *buf;
12776 unsigned long insn;
12777 boolean use_extend;
12778 unsigned short extend;
12779
12780 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12781 op = mips16_immed_operands;
12782 while (op->type != type)
12783 ++op;
12784
12785 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12786 {
12787 small = false;
12788 ext = true;
12789 }
12790 else
12791 {
12792 small = true;
12793 ext = false;
12794 }
12795
6386f3a7 12796 resolve_symbol_value (fragp->fr_symbol);
252b5132
RH
12797 val = S_GET_VALUE (fragp->fr_symbol);
12798 if (op->pcrel)
12799 {
12800 addressT addr;
12801
12802 addr = fragp->fr_address + fragp->fr_fix;
12803
12804 /* The rules for the base address of a PC relative reloc are
12805 complicated; see mips16_extended_frag. */
12806 if (type == 'p' || type == 'q')
12807 {
12808 addr += 2;
12809 if (ext)
12810 addr += 2;
12811 /* Ignore the low bit in the target, since it will be
12812 set for a text label. */
12813 if ((val & 1) != 0)
12814 --val;
12815 }
12816 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12817 addr -= 4;
12818 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12819 addr -= 2;
12820
12821 addr &= ~ (addressT) ((1 << op->shift) - 1);
12822 val -= addr;
12823
12824 /* Make sure the section winds up with the alignment we have
12825 assumed. */
12826 if (op->shift > 0)
12827 record_alignment (asec, op->shift);
12828 }
12829
12830 if (ext
12831 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
12832 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
12833 as_warn_where (fragp->fr_file, fragp->fr_line,
12834 _("extended instruction in delay slot"));
12835
12836 buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
12837
12838 if (target_big_endian)
12839 insn = bfd_getb16 (buf);
12840 else
12841 insn = bfd_getl16 (buf);
12842
12843 mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
12844 RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
12845 small, ext, &insn, &use_extend, &extend);
12846
12847 if (use_extend)
12848 {
874e8986 12849 md_number_to_chars ((char *) buf, 0xf000 | extend, 2);
252b5132
RH
12850 fragp->fr_fix += 2;
12851 buf += 2;
12852 }
12853
874e8986 12854 md_number_to_chars ((char *) buf, insn, 2);
252b5132
RH
12855 fragp->fr_fix += 2;
12856 buf += 2;
12857 }
12858 else
12859 {
12860 if (fragp->fr_opcode == NULL)
12861 return;
12862
12863 old = RELAX_OLD (fragp->fr_subtype);
12864 new = RELAX_NEW (fragp->fr_subtype);
12865 fixptr = fragp->fr_literal + fragp->fr_fix;
12866
12867 if (new > 0)
12868 memcpy (fixptr - old, fixptr, new);
12869
12870 fragp->fr_fix += new - old;
12871 }
12872}
12873
12874#ifdef OBJ_ELF
12875
12876/* This function is called after the relocs have been generated.
12877 We've been storing mips16 text labels as odd. Here we convert them
12878 back to even for the convenience of the debugger. */
12879
12880void
12881mips_frob_file_after_relocs ()
12882{
12883 asymbol **syms;
12884 unsigned int count, i;
12885
12886 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
12887 return;
12888
12889 syms = bfd_get_outsymbols (stdoutput);
12890 count = bfd_get_symcount (stdoutput);
12891 for (i = 0; i < count; i++, syms++)
12892 {
12893 if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
12894 && ((*syms)->value & 1) != 0)
12895 {
12896 (*syms)->value &= ~1;
12897 /* If the symbol has an odd size, it was probably computed
12898 incorrectly, so adjust that as well. */
12899 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
12900 ++elf_symbol (*syms)->internal_elf_sym.st_size;
12901 }
12902 }
12903}
12904
12905#endif
12906
12907/* This function is called whenever a label is defined. It is used
12908 when handling branch delays; if a branch has a label, we assume we
12909 can not move it. */
12910
12911void
12912mips_define_label (sym)
12913 symbolS *sym;
12914{
12915 struct insn_label_list *l;
12916
12917 if (free_insn_labels == NULL)
12918 l = (struct insn_label_list *) xmalloc (sizeof *l);
12919 else
12920 {
12921 l = free_insn_labels;
12922 free_insn_labels = l->next;
12923 }
12924
12925 l->label = sym;
12926 l->next = insn_labels;
12927 insn_labels = l;
12928}
12929\f
12930#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
12931
12932/* Some special processing for a MIPS ELF file. */
12933
12934void
12935mips_elf_final_processing ()
12936{
12937 /* Write out the register information. */
316f5878 12938 if (mips_abi != N64_ABI)
252b5132
RH
12939 {
12940 Elf32_RegInfo s;
12941
12942 s.ri_gprmask = mips_gprmask;
12943 s.ri_cprmask[0] = mips_cprmask[0];
12944 s.ri_cprmask[1] = mips_cprmask[1];
12945 s.ri_cprmask[2] = mips_cprmask[2];
12946 s.ri_cprmask[3] = mips_cprmask[3];
12947 /* The gp_value field is set by the MIPS ELF backend. */
12948
12949 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
12950 ((Elf32_External_RegInfo *)
12951 mips_regmask_frag));
12952 }
12953 else
12954 {
12955 Elf64_Internal_RegInfo s;
12956
12957 s.ri_gprmask = mips_gprmask;
12958 s.ri_pad = 0;
12959 s.ri_cprmask[0] = mips_cprmask[0];
12960 s.ri_cprmask[1] = mips_cprmask[1];
12961 s.ri_cprmask[2] = mips_cprmask[2];
12962 s.ri_cprmask[3] = mips_cprmask[3];
12963 /* The gp_value field is set by the MIPS ELF backend. */
12964
12965 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
12966 ((Elf64_External_RegInfo *)
12967 mips_regmask_frag));
12968 }
12969
12970 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
12971 sort of BFD interface for this. */
12972 if (mips_any_noreorder)
12973 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
12974 if (mips_pic != NO_PIC)
12975 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
12976
98d3f06f 12977 /* Set MIPS ELF flags for ASEs. */
a4672219
TS
12978 if (file_ase_mips16)
12979 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
1f25f5d3
CD
12980#if 0 /* XXX FIXME */
12981 if (file_ase_mips3d)
12982 elf_elfheader (stdoutput)->e_flags |= ???;
12983#endif
deec1734
CD
12984 if (file_ase_mdmx)
12985 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
1f25f5d3 12986
bdaaa2e1 12987 /* Set the MIPS ELF ABI flags. */
316f5878 12988 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
252b5132 12989 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
316f5878 12990 else if (mips_abi == O64_ABI)
252b5132 12991 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
316f5878 12992 else if (mips_abi == EABI_ABI)
252b5132 12993 {
316f5878 12994 if (!file_mips_gp32)
252b5132
RH
12995 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
12996 else
12997 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
12998 }
316f5878 12999 else if (mips_abi == N32_ABI)
be00bddd
TS
13000 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
13001
c9914766 13002 /* Nothing to do for N64_ABI. */
252b5132
RH
13003
13004 if (mips_32bitmode)
13005 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
13006}
13007
13008#endif /* OBJ_ELF || OBJ_MAYBE_ELF */
13009\f
beae10d5
KH
13010typedef struct proc {
13011 symbolS *isym;
13012 unsigned long reg_mask;
13013 unsigned long reg_offset;
13014 unsigned long fpreg_mask;
13015 unsigned long fpreg_offset;
13016 unsigned long frame_offset;
13017 unsigned long frame_reg;
13018 unsigned long pc_reg;
13019} procS;
252b5132
RH
13020
13021static procS cur_proc;
13022static procS *cur_proc_ptr;
13023static int numprocs;
13024
0a9ef439 13025/* Fill in an rs_align_code fragment. */
a19d8eb0 13026
0a9ef439
RH
13027void
13028mips_handle_align (fragp)
13029 fragS *fragp;
a19d8eb0 13030{
0a9ef439
RH
13031 if (fragp->fr_type != rs_align_code)
13032 return;
13033
13034 if (mips_opts.mips16)
a19d8eb0
CP
13035 {
13036 static const unsigned char be_nop[] = { 0x65, 0x00 };
13037 static const unsigned char le_nop[] = { 0x00, 0x65 };
13038
0a9ef439
RH
13039 int bytes;
13040 char *p;
a19d8eb0 13041
0a9ef439
RH
13042 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
13043 p = fragp->fr_literal + fragp->fr_fix;
13044
13045 if (bytes & 1)
13046 {
13047 *p++ = 0;
f9419b05 13048 fragp->fr_fix++;
0a9ef439
RH
13049 }
13050
13051 memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
13052 fragp->fr_var = 2;
a19d8eb0
CP
13053 }
13054
0a9ef439 13055 /* For mips32, a nop is a zero, which we trivially get by doing nothing. */
a19d8eb0
CP
13056}
13057
252b5132
RH
13058static void
13059md_obj_begin ()
13060{
13061}
13062
13063static void
13064md_obj_end ()
13065{
13066 /* check for premature end, nesting errors, etc */
13067 if (cur_proc_ptr)
9a41af64 13068 as_warn (_("missing .end at end of assembly"));
252b5132
RH
13069}
13070
13071static long
13072get_number ()
13073{
13074 int negative = 0;
13075 long val = 0;
13076
13077 if (*input_line_pointer == '-')
13078 {
13079 ++input_line_pointer;
13080 negative = 1;
13081 }
3882b010 13082 if (!ISDIGIT (*input_line_pointer))
956cd1d6 13083 as_bad (_("expected simple number"));
252b5132
RH
13084 if (input_line_pointer[0] == '0')
13085 {
13086 if (input_line_pointer[1] == 'x')
13087 {
13088 input_line_pointer += 2;
3882b010 13089 while (ISXDIGIT (*input_line_pointer))
252b5132
RH
13090 {
13091 val <<= 4;
13092 val |= hex_value (*input_line_pointer++);
13093 }
13094 return negative ? -val : val;
13095 }
13096 else
13097 {
13098 ++input_line_pointer;
3882b010 13099 while (ISDIGIT (*input_line_pointer))
252b5132
RH
13100 {
13101 val <<= 3;
13102 val |= *input_line_pointer++ - '0';
13103 }
13104 return negative ? -val : val;
13105 }
13106 }
3882b010 13107 if (!ISDIGIT (*input_line_pointer))
252b5132
RH
13108 {
13109 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
13110 *input_line_pointer, *input_line_pointer);
956cd1d6 13111 as_warn (_("invalid number"));
252b5132
RH
13112 return -1;
13113 }
3882b010 13114 while (ISDIGIT (*input_line_pointer))
252b5132
RH
13115 {
13116 val *= 10;
13117 val += *input_line_pointer++ - '0';
13118 }
13119 return negative ? -val : val;
13120}
13121
13122/* The .file directive; just like the usual .file directive, but there
c5dd6aab
DJ
13123 is an initial number which is the ECOFF file index. In the non-ECOFF
13124 case .file implies DWARF-2. */
13125
13126static void
13127s_mips_file (x)
13128 int x ATTRIBUTE_UNUSED;
13129{
ecb4347a
DJ
13130 static int first_file_directive = 0;
13131
c5dd6aab
DJ
13132 if (ECOFF_DEBUGGING)
13133 {
13134 get_number ();
13135 s_app_file (0);
13136 }
13137 else
ecb4347a
DJ
13138 {
13139 char *filename;
13140
13141 filename = dwarf2_directive_file (0);
13142
13143 /* Versions of GCC up to 3.1 start files with a ".file"
13144 directive even for stabs output. Make sure that this
13145 ".file" is handled. Note that you need a version of GCC
13146 after 3.1 in order to support DWARF-2 on MIPS. */
13147 if (filename != NULL && ! first_file_directive)
13148 {
13149 (void) new_logical_line (filename, -1);
13150 s_app_file_string (filename);
13151 }
13152 first_file_directive = 1;
13153 }
c5dd6aab
DJ
13154}
13155
13156/* The .loc directive, implying DWARF-2. */
252b5132
RH
13157
13158static void
c5dd6aab 13159s_mips_loc (x)
43841e91 13160 int x ATTRIBUTE_UNUSED;
252b5132 13161{
c5dd6aab
DJ
13162 if (!ECOFF_DEBUGGING)
13163 dwarf2_directive_loc (0);
252b5132
RH
13164}
13165
252b5132
RH
13166/* The .end directive. */
13167
13168static void
13169s_mips_end (x)
43841e91 13170 int x ATTRIBUTE_UNUSED;
252b5132
RH
13171{
13172 symbolS *p;
13173 int maybe_text;
13174
7a621144
DJ
13175 /* Following functions need their own .frame and .cprestore directives. */
13176 mips_frame_reg_valid = 0;
13177 mips_cprestore_valid = 0;
13178
252b5132
RH
13179 if (!is_end_of_line[(unsigned char) *input_line_pointer])
13180 {
13181 p = get_symbol ();
13182 demand_empty_rest_of_line ();
13183 }
13184 else
13185 p = NULL;
13186
13187#ifdef BFD_ASSEMBLER
13188 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
13189 maybe_text = 1;
13190 else
13191 maybe_text = 0;
13192#else
13193 if (now_seg != data_section && now_seg != bss_section)
13194 maybe_text = 1;
13195 else
13196 maybe_text = 0;
13197#endif
13198
13199 if (!maybe_text)
13200 as_warn (_(".end not in text section"));
13201
13202 if (!cur_proc_ptr)
13203 {
13204 as_warn (_(".end directive without a preceding .ent directive."));
13205 demand_empty_rest_of_line ();
13206 return;
13207 }
13208
13209 if (p != NULL)
13210 {
13211 assert (S_GET_NAME (p));
13212 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->isym)))
13213 as_warn (_(".end symbol does not match .ent symbol."));
ecb4347a
DJ
13214
13215 if (debug_type == DEBUG_STABS)
13216 stabs_generate_asm_endfunc (S_GET_NAME (p),
13217 S_GET_NAME (p));
252b5132
RH
13218 }
13219 else
13220 as_warn (_(".end directive missing or unknown symbol"));
13221
ecb4347a
DJ
13222#ifdef OBJ_ELF
13223 /* Generate a .pdr section. */
13224 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
13225 {
13226 segT saved_seg = now_seg;
13227 subsegT saved_subseg = now_subseg;
13228 valueT dot;
13229 expressionS exp;
13230 char *fragp;
252b5132 13231
ecb4347a 13232 dot = frag_now_fix ();
252b5132
RH
13233
13234#ifdef md_flush_pending_output
ecb4347a 13235 md_flush_pending_output ();
252b5132
RH
13236#endif
13237
ecb4347a
DJ
13238 assert (pdr_seg);
13239 subseg_set (pdr_seg, 0);
252b5132 13240
ecb4347a
DJ
13241 /* Write the symbol. */
13242 exp.X_op = O_symbol;
13243 exp.X_add_symbol = p;
13244 exp.X_add_number = 0;
13245 emit_expr (&exp, 4);
252b5132 13246
ecb4347a 13247 fragp = frag_more (7 * 4);
252b5132 13248
ecb4347a
DJ
13249 md_number_to_chars (fragp, (valueT) cur_proc_ptr->reg_mask, 4);
13250 md_number_to_chars (fragp + 4, (valueT) cur_proc_ptr->reg_offset, 4);
13251 md_number_to_chars (fragp + 8, (valueT) cur_proc_ptr->fpreg_mask, 4);
13252 md_number_to_chars (fragp + 12, (valueT) cur_proc_ptr->fpreg_offset, 4);
13253 md_number_to_chars (fragp + 16, (valueT) cur_proc_ptr->frame_offset, 4);
13254 md_number_to_chars (fragp + 20, (valueT) cur_proc_ptr->frame_reg, 4);
13255 md_number_to_chars (fragp + 24, (valueT) cur_proc_ptr->pc_reg, 4);
252b5132 13256
ecb4347a
DJ
13257 subseg_set (saved_seg, saved_subseg);
13258 }
13259#endif /* OBJ_ELF */
252b5132
RH
13260
13261 cur_proc_ptr = NULL;
13262}
13263
13264/* The .aent and .ent directives. */
13265
13266static void
13267s_mips_ent (aent)
13268 int aent;
13269{
252b5132
RH
13270 symbolS *symbolP;
13271 int maybe_text;
13272
13273 symbolP = get_symbol ();
13274 if (*input_line_pointer == ',')
f9419b05 13275 ++input_line_pointer;
252b5132 13276 SKIP_WHITESPACE ();
3882b010 13277 if (ISDIGIT (*input_line_pointer)
d9a62219 13278 || *input_line_pointer == '-')
874e8986 13279 get_number ();
252b5132
RH
13280
13281#ifdef BFD_ASSEMBLER
13282 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
13283 maybe_text = 1;
13284 else
13285 maybe_text = 0;
13286#else
13287 if (now_seg != data_section && now_seg != bss_section)
13288 maybe_text = 1;
13289 else
13290 maybe_text = 0;
13291#endif
13292
13293 if (!maybe_text)
13294 as_warn (_(".ent or .aent not in text section."));
13295
13296 if (!aent && cur_proc_ptr)
9a41af64 13297 as_warn (_("missing .end"));
252b5132
RH
13298
13299 if (!aent)
13300 {
7a621144
DJ
13301 /* This function needs its own .frame and .cprestore directives. */
13302 mips_frame_reg_valid = 0;
13303 mips_cprestore_valid = 0;
13304
252b5132
RH
13305 cur_proc_ptr = &cur_proc;
13306 memset (cur_proc_ptr, '\0', sizeof (procS));
13307
13308 cur_proc_ptr->isym = symbolP;
13309
49309057 13310 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
252b5132 13311
f9419b05 13312 ++numprocs;
ecb4347a
DJ
13313
13314 if (debug_type == DEBUG_STABS)
13315 stabs_generate_asm_func (S_GET_NAME (symbolP),
13316 S_GET_NAME (symbolP));
252b5132
RH
13317 }
13318
13319 demand_empty_rest_of_line ();
13320}
13321
13322/* The .frame directive. If the mdebug section is present (IRIX 5 native)
bdaaa2e1 13323 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
252b5132 13324 s_mips_frame is used so that we can set the PDR information correctly.
bdaaa2e1 13325 We can't use the ecoff routines because they make reference to the ecoff
252b5132
RH
13326 symbol table (in the mdebug section). */
13327
13328static void
13329s_mips_frame (ignore)
2b3c5a5d 13330 int ignore ATTRIBUTE_UNUSED;
252b5132 13331{
ecb4347a
DJ
13332#ifdef OBJ_ELF
13333 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
13334 {
13335 long val;
252b5132 13336
ecb4347a
DJ
13337 if (cur_proc_ptr == (procS *) NULL)
13338 {
13339 as_warn (_(".frame outside of .ent"));
13340 demand_empty_rest_of_line ();
13341 return;
13342 }
252b5132 13343
ecb4347a
DJ
13344 cur_proc_ptr->frame_reg = tc_get_register (1);
13345
13346 SKIP_WHITESPACE ();
13347 if (*input_line_pointer++ != ','
13348 || get_absolute_expression_and_terminator (&val) != ',')
13349 {
13350 as_warn (_("Bad .frame directive"));
13351 --input_line_pointer;
13352 demand_empty_rest_of_line ();
13353 return;
13354 }
252b5132 13355
ecb4347a
DJ
13356 cur_proc_ptr->frame_offset = val;
13357 cur_proc_ptr->pc_reg = tc_get_register (0);
252b5132 13358
252b5132 13359 demand_empty_rest_of_line ();
252b5132 13360 }
ecb4347a
DJ
13361 else
13362#endif /* OBJ_ELF */
13363 s_ignore (ignore);
252b5132
RH
13364}
13365
bdaaa2e1
KH
13366/* The .fmask and .mask directives. If the mdebug section is present
13367 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
252b5132 13368 embedded targets, s_mips_mask is used so that we can set the PDR
bdaaa2e1 13369 information correctly. We can't use the ecoff routines because they
252b5132
RH
13370 make reference to the ecoff symbol table (in the mdebug section). */
13371
13372static void
13373s_mips_mask (reg_type)
13374 char reg_type;
13375{
ecb4347a
DJ
13376#ifdef OBJ_ELF
13377 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
252b5132 13378 {
ecb4347a 13379 long mask, off;
252b5132 13380
ecb4347a
DJ
13381 if (cur_proc_ptr == (procS *) NULL)
13382 {
13383 as_warn (_(".mask/.fmask outside of .ent"));
13384 demand_empty_rest_of_line ();
13385 return;
13386 }
252b5132 13387
ecb4347a
DJ
13388 if (get_absolute_expression_and_terminator (&mask) != ',')
13389 {
13390 as_warn (_("Bad .mask/.fmask directive"));
13391 --input_line_pointer;
13392 demand_empty_rest_of_line ();
13393 return;
13394 }
252b5132 13395
ecb4347a
DJ
13396 off = get_absolute_expression ();
13397
13398 if (reg_type == 'F')
13399 {
13400 cur_proc_ptr->fpreg_mask = mask;
13401 cur_proc_ptr->fpreg_offset = off;
13402 }
13403 else
13404 {
13405 cur_proc_ptr->reg_mask = mask;
13406 cur_proc_ptr->reg_offset = off;
13407 }
13408
13409 demand_empty_rest_of_line ();
252b5132
RH
13410 }
13411 else
ecb4347a
DJ
13412#endif /* OBJ_ELF */
13413 s_ignore (reg_type);
252b5132
RH
13414}
13415
13416/* The .loc directive. */
13417
13418#if 0
13419static void
13420s_loc (x)
13421 int x;
13422{
13423 symbolS *symbolP;
13424 int lineno;
13425 int addroff;
13426
13427 assert (now_seg == text_section);
13428
13429 lineno = get_number ();
13430 addroff = frag_now_fix ();
13431
13432 symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
13433 S_SET_TYPE (symbolP, N_SLINE);
13434 S_SET_OTHER (symbolP, 0);
13435 S_SET_DESC (symbolP, lineno);
13436 symbolP->sy_segment = now_seg;
13437}
13438#endif
e7af610e 13439
316f5878
RS
13440/* A table describing all the processors gas knows about. Names are
13441 matched in the order listed.
e7af610e 13442
316f5878
RS
13443 To ease comparison, please keep this table in the same order as
13444 gcc's mips_cpu_info_table[]. */
e972090a
NC
13445static const struct mips_cpu_info mips_cpu_info_table[] =
13446{
316f5878
RS
13447 /* Entries for generic ISAs */
13448 { "mips1", 1, ISA_MIPS1, CPU_R3000 },
13449 { "mips2", 1, ISA_MIPS2, CPU_R6000 },
13450 { "mips3", 1, ISA_MIPS3, CPU_R4000 },
13451 { "mips4", 1, ISA_MIPS4, CPU_R8000 },
13452 { "mips5", 1, ISA_MIPS5, CPU_MIPS5 },
13453 { "mips32", 1, ISA_MIPS32, CPU_MIPS32 },
13454 { "mips64", 1, ISA_MIPS64, CPU_MIPS64 },
13455
13456 /* MIPS I */
13457 { "r3000", 0, ISA_MIPS1, CPU_R3000 },
13458 { "r2000", 0, ISA_MIPS1, CPU_R3000 },
13459 { "r3900", 0, ISA_MIPS1, CPU_R3900 },
13460
13461 /* MIPS II */
13462 { "r6000", 0, ISA_MIPS2, CPU_R6000 },
13463
13464 /* MIPS III */
13465 { "r4000", 0, ISA_MIPS3, CPU_R4000 },
13466 { "r4010", 0, ISA_MIPS2, CPU_R4010 },
13467 { "vr4100", 0, ISA_MIPS3, CPU_VR4100 },
13468 { "vr4111", 0, ISA_MIPS3, CPU_R4111 },
13469 { "vr4300", 0, ISA_MIPS3, CPU_R4300 },
13470 { "r4400", 0, ISA_MIPS3, CPU_R4400 },
13471 { "r4600", 0, ISA_MIPS3, CPU_R4600 },
13472 { "orion", 0, ISA_MIPS3, CPU_R4600 },
13473 { "r4650", 0, ISA_MIPS3, CPU_R4650 },
13474
13475 /* MIPS IV */
13476 { "r8000", 0, ISA_MIPS4, CPU_R8000 },
13477 { "r10000", 0, ISA_MIPS4, CPU_R10000 },
13478 { "r12000", 0, ISA_MIPS4, CPU_R12000 },
13479 { "vr5000", 0, ISA_MIPS4, CPU_R5000 },
13480 { "rm5200", 0, ISA_MIPS4, CPU_R5000 },
13481 { "rm5230", 0, ISA_MIPS4, CPU_R5000 },
13482 { "rm5231", 0, ISA_MIPS4, CPU_R5000 },
13483 { "rm5261", 0, ISA_MIPS4, CPU_R5000 },
13484 { "rm5721", 0, ISA_MIPS4, CPU_R5000 },
13485 { "r7000", 0, ISA_MIPS4, CPU_R5000 },
13486
13487 /* MIPS 32 */
13488 { "4kc", 0, ISA_MIPS32, CPU_MIPS32, },
13489 { "4km", 0, ISA_MIPS32, CPU_MIPS32 },
13490 { "4kp", 0, ISA_MIPS32, CPU_MIPS32 },
e7af610e 13491
316f5878
RS
13492 /* MIPS 64 */
13493 { "5kc", 0, ISA_MIPS64, CPU_MIPS64 },
13494 { "20kc", 0, ISA_MIPS64, CPU_MIPS64 },
e7af610e 13495
c7a23324 13496 /* Broadcom SB-1 CPU core */
316f5878 13497 { "sb1", 0, ISA_MIPS64, CPU_SB1 },
e7af610e 13498
316f5878
RS
13499 /* End marker */
13500 { NULL, 0, 0, 0 }
13501};
e7af610e 13502
84ea6cf2 13503
316f5878
RS
13504/* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
13505 with a final "000" replaced by "k". Ignore case.
e7af610e 13506
316f5878 13507 Note: this function is shared between GCC and GAS. */
c6c98b38 13508
316f5878
RS
13509static boolean
13510mips_strict_matching_cpu_name_p (canonical, given)
13511 const char *canonical, *given;
13512{
13513 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
13514 given++, canonical++;
13515
13516 return ((*given == 0 && *canonical == 0)
13517 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
13518}
13519
13520
13521/* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
13522 CPU name. We've traditionally allowed a lot of variation here.
13523
13524 Note: this function is shared between GCC and GAS. */
13525
13526static boolean
13527mips_matching_cpu_name_p (canonical, given)
13528 const char *canonical, *given;
13529{
13530 /* First see if the name matches exactly, or with a final "000"
13531 turned into "k". */
13532 if (mips_strict_matching_cpu_name_p (canonical, given))
13533 return true;
13534
13535 /* If not, try comparing based on numerical designation alone.
13536 See if GIVEN is an unadorned number, or 'r' followed by a number. */
13537 if (TOLOWER (*given) == 'r')
13538 given++;
13539 if (!ISDIGIT (*given))
13540 return false;
13541
13542 /* Skip over some well-known prefixes in the canonical name,
13543 hoping to find a number there too. */
13544 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
13545 canonical += 2;
13546 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
13547 canonical += 2;
13548 else if (TOLOWER (canonical[0]) == 'r')
13549 canonical += 1;
13550
13551 return mips_strict_matching_cpu_name_p (canonical, given);
13552}
13553
13554
13555/* Parse an option that takes the name of a processor as its argument.
13556 OPTION is the name of the option and CPU_STRING is the argument.
13557 Return the corresponding processor enumeration if the CPU_STRING is
13558 recognized, otherwise report an error and return null.
13559
13560 A similar function exists in GCC. */
e7af610e
NC
13561
13562static const struct mips_cpu_info *
316f5878
RS
13563mips_parse_cpu (option, cpu_string)
13564 const char *option, *cpu_string;
e7af610e 13565{
316f5878 13566 const struct mips_cpu_info *p;
e7af610e 13567
316f5878
RS
13568 /* 'from-abi' selects the most compatible architecture for the given
13569 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
13570 EABIs, we have to decide whether we're using the 32-bit or 64-bit
13571 version. Look first at the -mgp options, if given, otherwise base
13572 the choice on MIPS_DEFAULT_64BIT.
e7af610e 13573
316f5878
RS
13574 Treat NO_ABI like the EABIs. One reason to do this is that the
13575 plain 'mips' and 'mips64' configs have 'from-abi' as their default
13576 architecture. This code picks MIPS I for 'mips' and MIPS III for
13577 'mips64', just as we did in the days before 'from-abi'. */
13578 if (strcasecmp (cpu_string, "from-abi") == 0)
13579 {
13580 if (ABI_NEEDS_32BIT_REGS (mips_abi))
13581 return mips_cpu_info_from_isa (ISA_MIPS1);
13582
13583 if (ABI_NEEDS_64BIT_REGS (mips_abi))
13584 return mips_cpu_info_from_isa (ISA_MIPS3);
13585
13586 if (file_mips_gp32 >= 0)
13587 return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
13588
13589 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
13590 ? ISA_MIPS3
13591 : ISA_MIPS1);
13592 }
13593
13594 /* 'default' has traditionally been a no-op. Probably not very useful. */
13595 if (strcasecmp (cpu_string, "default") == 0)
13596 return 0;
13597
13598 for (p = mips_cpu_info_table; p->name != 0; p++)
13599 if (mips_matching_cpu_name_p (p->name, cpu_string))
13600 return p;
13601
13602 as_bad ("Bad value (%s) for %s", cpu_string, option);
13603 return 0;
e7af610e
NC
13604}
13605
316f5878
RS
13606/* Return the canonical processor information for ISA (a member of the
13607 ISA_MIPS* enumeration). */
13608
e7af610e
NC
13609static const struct mips_cpu_info *
13610mips_cpu_info_from_isa (isa)
13611 int isa;
13612{
13613 int i;
13614
13615 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13616 if (mips_cpu_info_table[i].is_isa
316f5878 13617 && isa == mips_cpu_info_table[i].isa)
e7af610e
NC
13618 return (&mips_cpu_info_table[i]);
13619
e972090a 13620 return NULL;
e7af610e 13621}
316f5878
RS
13622\f
13623static void
13624show (stream, string, col_p, first_p)
13625 FILE *stream;
13626 const char *string;
13627 int *col_p;
13628 int *first_p;
13629{
13630 if (*first_p)
13631 {
13632 fprintf (stream, "%24s", "");
13633 *col_p = 24;
13634 }
13635 else
13636 {
13637 fprintf (stream, ", ");
13638 *col_p += 2;
13639 }
e7af610e 13640
316f5878
RS
13641 if (*col_p + strlen (string) > 72)
13642 {
13643 fprintf (stream, "\n%24s", "");
13644 *col_p = 24;
13645 }
13646
13647 fprintf (stream, "%s", string);
13648 *col_p += strlen (string);
13649
13650 *first_p = 0;
13651}
13652
13653void
13654md_show_usage (stream)
13655 FILE *stream;
e7af610e 13656{
316f5878
RS
13657 int column, first;
13658 size_t i;
13659
13660 fprintf (stream, _("\
13661MIPS options:\n\
13662-membedded-pic generate embedded position independent code\n\
13663-EB generate big endian output\n\
13664-EL generate little endian output\n\
13665-g, -g2 do not remove unneeded NOPs or swap branches\n\
13666-G NUM allow referencing objects up to NUM bytes\n\
13667 implicitly with the gp register [default 8]\n"));
13668 fprintf (stream, _("\
13669-mips1 generate MIPS ISA I instructions\n\
13670-mips2 generate MIPS ISA II instructions\n\
13671-mips3 generate MIPS ISA III instructions\n\
13672-mips4 generate MIPS ISA IV instructions\n\
13673-mips5 generate MIPS ISA V instructions\n\
13674-mips32 generate MIPS32 ISA instructions\n\
13675-mips64 generate MIPS64 ISA instructions\n\
13676-march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
13677
13678 first = 1;
e7af610e
NC
13679
13680 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
316f5878
RS
13681 show (stream, mips_cpu_info_table[i].name, &column, &first);
13682 show (stream, "from-abi", &column, &first);
13683 fputc ('\n', stream);
e7af610e 13684
316f5878
RS
13685 fprintf (stream, _("\
13686-mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
13687-no-mCPU don't generate code specific to CPU.\n\
13688 For -mCPU and -no-mCPU, CPU must be one of:\n"));
13689
13690 first = 1;
13691
13692 show (stream, "3900", &column, &first);
13693 show (stream, "4010", &column, &first);
13694 show (stream, "4100", &column, &first);
13695 show (stream, "4650", &column, &first);
13696 fputc ('\n', stream);
13697
13698 fprintf (stream, _("\
13699-mips16 generate mips16 instructions\n\
13700-no-mips16 do not generate mips16 instructions\n"));
13701 fprintf (stream, _("\
13702-mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
13703-mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
13704-O0 remove unneeded NOPs, do not swap branches\n\
13705-O remove unneeded NOPs and swap branches\n\
13706-n warn about NOPs generated from macros\n\
13707--[no-]construct-floats [dis]allow floating point values to be constructed\n\
13708--trap, --no-break trap exception on div by 0 and mult overflow\n\
13709--break, --no-trap break exception on div by 0 and mult overflow\n"));
13710#ifdef OBJ_ELF
13711 fprintf (stream, _("\
13712-KPIC, -call_shared generate SVR4 position independent code\n\
13713-non_shared do not generate position independent code\n\
13714-xgot assume a 32 bit GOT\n\
13715-mabi=ABI create ABI conformant object file for:\n"));
13716
13717 first = 1;
13718
13719 show (stream, "32", &column, &first);
13720 show (stream, "o64", &column, &first);
13721 show (stream, "n32", &column, &first);
13722 show (stream, "64", &column, &first);
13723 show (stream, "eabi", &column, &first);
13724
13725 fputc ('\n', stream);
13726
13727 fprintf (stream, _("\
13728-32 create o32 ABI object file (default)\n\
13729-n32 create n32 ABI object file\n\
13730-64 create 64 ABI object file\n"));
13731#endif
e7af610e 13732}
This page took 1.078102 seconds and 4 git commands to generate.