* Makefile.in (GCC_FOR_TARGET): Prepend STAGE_CC_WRAPPER.
[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));
cca86cc8 747static void s_change_section PARAMS ((int));
252b5132
RH
748static void s_cons PARAMS ((int));
749static void s_float_cons PARAMS ((int));
750static void s_mips_globl PARAMS ((int));
751static void s_option PARAMS ((int));
752static void s_mipsset PARAMS ((int));
753static void s_abicalls PARAMS ((int));
754static void s_cpload PARAMS ((int));
6478892d
TS
755static void s_cpsetup PARAMS ((int));
756static void s_cplocal PARAMS ((int));
252b5132 757static void s_cprestore PARAMS ((int));
6478892d
TS
758static void s_cpreturn PARAMS ((int));
759static void s_gpvalue PARAMS ((int));
252b5132
RH
760static void s_gpword PARAMS ((int));
761static void s_cpadd PARAMS ((int));
762static void s_insn PARAMS ((int));
763static void md_obj_begin PARAMS ((void));
764static void md_obj_end PARAMS ((void));
765static long get_number PARAMS ((void));
766static void s_mips_ent PARAMS ((int));
767static void s_mips_end PARAMS ((int));
768static void s_mips_frame PARAMS ((int));
769static void s_mips_mask PARAMS ((int));
770static void s_mips_stab PARAMS ((int));
771static void s_mips_weakext PARAMS ((int));
c5dd6aab
DJ
772static void s_mips_file PARAMS ((int));
773static void s_mips_loc PARAMS ((int));
252b5132 774static int mips16_extended_frag PARAMS ((fragS *, asection *, long));
252b5132 775static int validate_mips_insn PARAMS ((const struct mips_opcode *));
316f5878 776static void show PARAMS ((FILE *, const char *, int *, int *));
add55e1f
RS
777#ifdef OBJ_ELF
778static int mips_need_elf_addend_fixup PARAMS ((fixS *));
779#endif
e7af610e 780
ad8d3bb3 781/* Return values of my_getSmallExpression(). */
fb1b3232 782
ad8d3bb3 783enum small_ex_type
fb1b3232
TS
784{
785 S_EX_NONE = 0,
ad8d3bb3
TS
786 S_EX_REGISTER,
787
788 /* Direct relocation creation by %percent_op(). */
789 S_EX_HALF,
fb1b3232 790 S_EX_HI,
ad8d3bb3
TS
791 S_EX_LO,
792 S_EX_GP_REL,
793 S_EX_GOT,
794 S_EX_CALL16,
795 S_EX_GOT_DISP,
796 S_EX_GOT_PAGE,
797 S_EX_GOT_OFST,
798 S_EX_GOT_HI,
799 S_EX_GOT_LO,
800 S_EX_NEG,
fb1b3232
TS
801 S_EX_HIGHER,
802 S_EX_HIGHEST,
ad8d3bb3
TS
803 S_EX_CALL_HI,
804 S_EX_CALL_LO
fb1b3232
TS
805};
806
e7af610e
NC
807/* Table and functions used to map between CPU/ISA names, and
808 ISA levels, and CPU numbers. */
809
e972090a
NC
810struct mips_cpu_info
811{
e7af610e
NC
812 const char *name; /* CPU or ISA name. */
813 int is_isa; /* Is this an ISA? (If 0, a CPU.) */
814 int isa; /* ISA level. */
815 int cpu; /* CPU number (default CPU if ISA). */
816};
817
316f5878
RS
818static void mips_set_architecture PARAMS ((const struct mips_cpu_info *));
819static void mips_set_tune PARAMS ((const struct mips_cpu_info *));
820static boolean mips_strict_matching_cpu_name_p PARAMS ((const char *,
821 const char *));
822static boolean mips_matching_cpu_name_p PARAMS ((const char *, const char *));
823static const struct mips_cpu_info *mips_parse_cpu PARAMS ((const char *,
824 const char *));
e7af610e 825static const struct mips_cpu_info *mips_cpu_info_from_isa PARAMS ((int));
252b5132
RH
826\f
827/* Pseudo-op table.
828
829 The following pseudo-ops from the Kane and Heinrich MIPS book
830 should be defined here, but are currently unsupported: .alias,
831 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
832
833 The following pseudo-ops from the Kane and Heinrich MIPS book are
834 specific to the type of debugging information being generated, and
835 should be defined by the object format: .aent, .begin, .bend,
836 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
837 .vreg.
838
839 The following pseudo-ops from the Kane and Heinrich MIPS book are
840 not MIPS CPU specific, but are also not specific to the object file
841 format. This file is probably the best place to define them, but
842 they are not currently supported: .asm0, .endr, .lab, .repeat,
843 .struct. */
844
e972090a
NC
845static const pseudo_typeS mips_pseudo_table[] =
846{
beae10d5 847 /* MIPS specific pseudo-ops. */
252b5132
RH
848 {"option", s_option, 0},
849 {"set", s_mipsset, 0},
850 {"rdata", s_change_sec, 'r'},
851 {"sdata", s_change_sec, 's'},
852 {"livereg", s_ignore, 0},
853 {"abicalls", s_abicalls, 0},
854 {"cpload", s_cpload, 0},
6478892d
TS
855 {"cpsetup", s_cpsetup, 0},
856 {"cplocal", s_cplocal, 0},
252b5132 857 {"cprestore", s_cprestore, 0},
6478892d
TS
858 {"cpreturn", s_cpreturn, 0},
859 {"gpvalue", s_gpvalue, 0},
252b5132
RH
860 {"gpword", s_gpword, 0},
861 {"cpadd", s_cpadd, 0},
862 {"insn", s_insn, 0},
863
beae10d5 864 /* Relatively generic pseudo-ops that happen to be used on MIPS
252b5132
RH
865 chips. */
866 {"asciiz", stringer, 1},
867 {"bss", s_change_sec, 'b'},
868 {"err", s_err, 0},
869 {"half", s_cons, 1},
870 {"dword", s_cons, 3},
871 {"weakext", s_mips_weakext, 0},
872
beae10d5 873 /* These pseudo-ops are defined in read.c, but must be overridden
252b5132
RH
874 here for one reason or another. */
875 {"align", s_align, 0},
876 {"byte", s_cons, 0},
877 {"data", s_change_sec, 'd'},
878 {"double", s_float_cons, 'd'},
879 {"float", s_float_cons, 'f'},
880 {"globl", s_mips_globl, 0},
881 {"global", s_mips_globl, 0},
882 {"hword", s_cons, 1},
883 {"int", s_cons, 2},
884 {"long", s_cons, 2},
885 {"octa", s_cons, 4},
886 {"quad", s_cons, 3},
cca86cc8 887 {"section", s_change_section, 0},
252b5132
RH
888 {"short", s_cons, 1},
889 {"single", s_float_cons, 'f'},
890 {"stabn", s_mips_stab, 'n'},
891 {"text", s_change_sec, 't'},
892 {"word", s_cons, 2},
add56521 893
add56521 894 { "extern", ecoff_directive_extern, 0},
add56521 895
43841e91 896 { NULL, NULL, 0 },
252b5132
RH
897};
898
e972090a
NC
899static const pseudo_typeS mips_nonecoff_pseudo_table[] =
900{
beae10d5
KH
901 /* These pseudo-ops should be defined by the object file format.
902 However, a.out doesn't support them, so we have versions here. */
252b5132
RH
903 {"aent", s_mips_ent, 1},
904 {"bgnb", s_ignore, 0},
905 {"end", s_mips_end, 0},
906 {"endb", s_ignore, 0},
907 {"ent", s_mips_ent, 0},
c5dd6aab 908 {"file", s_mips_file, 0},
252b5132
RH
909 {"fmask", s_mips_mask, 'F'},
910 {"frame", s_mips_frame, 0},
c5dd6aab 911 {"loc", s_mips_loc, 0},
252b5132
RH
912 {"mask", s_mips_mask, 'R'},
913 {"verstamp", s_ignore, 0},
43841e91 914 { NULL, NULL, 0 },
252b5132
RH
915};
916
917extern void pop_insert PARAMS ((const pseudo_typeS *));
918
919void
920mips_pop_insert ()
921{
922 pop_insert (mips_pseudo_table);
923 if (! ECOFF_DEBUGGING)
924 pop_insert (mips_nonecoff_pseudo_table);
925}
926\f
927/* Symbols labelling the current insn. */
928
e972090a
NC
929struct insn_label_list
930{
252b5132
RH
931 struct insn_label_list *next;
932 symbolS *label;
933};
934
935static struct insn_label_list *insn_labels;
936static struct insn_label_list *free_insn_labels;
937
938static void mips_clear_insn_labels PARAMS ((void));
939
940static inline void
941mips_clear_insn_labels ()
942{
943 register struct insn_label_list **pl;
944
945 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
946 ;
947 *pl = insn_labels;
948 insn_labels = NULL;
949}
950\f
951static char *expr_end;
952
953/* Expressions which appear in instructions. These are set by
954 mips_ip. */
955
956static expressionS imm_expr;
957static expressionS offset_expr;
958
959/* Relocs associated with imm_expr and offset_expr. */
960
f6688943
TS
961static bfd_reloc_code_real_type imm_reloc[3]
962 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
963static bfd_reloc_code_real_type offset_reloc[3]
964 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132
RH
965
966/* This is set by mips_ip if imm_reloc is an unmatched HI16_S reloc. */
967
968static boolean imm_unmatched_hi;
969
970/* These are set by mips16_ip if an explicit extension is used. */
971
972static boolean mips16_small, mips16_ext;
973
ecb4347a
DJ
974/* The pdr segment for per procedure frame/regmask info. Not used for
975 ECOFF debugging. */
252b5132
RH
976
977static segT pdr_seg;
252b5132 978
e013f690
TS
979/* The default target format to use. */
980
981const char *
982mips_target_format ()
983{
984 switch (OUTPUT_FLAVOR)
985 {
986 case bfd_target_aout_flavour:
987 return target_big_endian ? "a.out-mips-big" : "a.out-mips-little";
988 case bfd_target_ecoff_flavour:
989 return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
990 case bfd_target_coff_flavour:
991 return "pe-mips";
992 case bfd_target_elf_flavour:
993#ifdef TE_TMIPS
cfe86eaa 994 /* This is traditional mips. */
e013f690 995 return (target_big_endian
cfe86eaa
TS
996 ? (HAVE_64BIT_OBJECTS
997 ? "elf64-tradbigmips"
998 : (HAVE_NEWABI
999 ? "elf32-ntradbigmips" : "elf32-tradbigmips"))
1000 : (HAVE_64BIT_OBJECTS
1001 ? "elf64-tradlittlemips"
1002 : (HAVE_NEWABI
1003 ? "elf32-ntradlittlemips" : "elf32-tradlittlemips")));
e013f690
TS
1004#else
1005 return (target_big_endian
cfe86eaa
TS
1006 ? (HAVE_64BIT_OBJECTS
1007 ? "elf64-bigmips"
1008 : (HAVE_NEWABI
1009 ? "elf32-nbigmips" : "elf32-bigmips"))
1010 : (HAVE_64BIT_OBJECTS
1011 ? "elf64-littlemips"
1012 : (HAVE_NEWABI
1013 ? "elf32-nlittlemips" : "elf32-littlemips")));
e013f690
TS
1014#endif
1015 default:
1016 abort ();
1017 return NULL;
1018 }
1019}
1020
156c2f8b
NC
1021/* This function is called once, at assembler startup time. It should
1022 set up all the tables, etc. that the MD part of the assembler will need. */
1023
252b5132
RH
1024void
1025md_begin ()
1026{
252b5132 1027 register const char *retval = NULL;
156c2f8b 1028 int i = 0;
252b5132 1029 int broken = 0;
1f25f5d3 1030
ec68c924 1031 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, mips_arch))
252b5132
RH
1032 as_warn (_("Could not set architecture and machine"));
1033
252b5132
RH
1034 op_hash = hash_new ();
1035
1036 for (i = 0; i < NUMOPCODES;)
1037 {
1038 const char *name = mips_opcodes[i].name;
1039
1040 retval = hash_insert (op_hash, name, (PTR) &mips_opcodes[i]);
1041 if (retval != NULL)
1042 {
1043 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1044 mips_opcodes[i].name, retval);
1045 /* Probably a memory allocation problem? Give up now. */
1046 as_fatal (_("Broken assembler. No assembly attempted."));
1047 }
1048 do
1049 {
1050 if (mips_opcodes[i].pinfo != INSN_MACRO)
1051 {
1052 if (!validate_mips_insn (&mips_opcodes[i]))
1053 broken = 1;
1054 }
1055 ++i;
1056 }
1057 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1058 }
1059
1060 mips16_op_hash = hash_new ();
1061
1062 i = 0;
1063 while (i < bfd_mips16_num_opcodes)
1064 {
1065 const char *name = mips16_opcodes[i].name;
1066
1067 retval = hash_insert (mips16_op_hash, name, (PTR) &mips16_opcodes[i]);
1068 if (retval != NULL)
1069 as_fatal (_("internal: can't hash `%s': %s"),
1070 mips16_opcodes[i].name, retval);
1071 do
1072 {
1073 if (mips16_opcodes[i].pinfo != INSN_MACRO
1074 && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1075 != mips16_opcodes[i].match))
1076 {
1077 fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1078 mips16_opcodes[i].name, mips16_opcodes[i].args);
1079 broken = 1;
1080 }
1081 ++i;
1082 }
1083 while (i < bfd_mips16_num_opcodes
1084 && strcmp (mips16_opcodes[i].name, name) == 0);
1085 }
1086
1087 if (broken)
1088 as_fatal (_("Broken assembler. No assembly attempted."));
1089
1090 /* We add all the general register names to the symbol table. This
1091 helps us detect invalid uses of them. */
1092 for (i = 0; i < 32; i++)
1093 {
1094 char buf[5];
1095
1096 sprintf (buf, "$%d", i);
1097 symbol_table_insert (symbol_new (buf, reg_section, i,
1098 &zero_address_frag));
1099 }
76db943d
TS
1100 symbol_table_insert (symbol_new ("$ra", reg_section, RA,
1101 &zero_address_frag));
252b5132
RH
1102 symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1103 &zero_address_frag));
1104 symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1105 &zero_address_frag));
1106 symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1107 &zero_address_frag));
1108 symbol_table_insert (symbol_new ("$at", reg_section, AT,
1109 &zero_address_frag));
1110 symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1111 &zero_address_frag));
1112 symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1113 &zero_address_frag));
85b51719
TS
1114 symbol_table_insert (symbol_new ("$zero", reg_section, ZERO,
1115 &zero_address_frag));
252b5132
RH
1116 symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1117 &zero_address_frag));
1118
1119 mips_no_prev_insn (false);
1120
1121 mips_gprmask = 0;
1122 mips_cprmask[0] = 0;
1123 mips_cprmask[1] = 0;
1124 mips_cprmask[2] = 0;
1125 mips_cprmask[3] = 0;
1126
1127 /* set the default alignment for the text section (2**2) */
1128 record_alignment (text_section, 2);
1129
1130 if (USE_GLOBAL_POINTER_OPT)
1131 bfd_set_gp_size (stdoutput, g_switch_value);
1132
1133 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1134 {
1135 /* On a native system, sections must be aligned to 16 byte
1136 boundaries. When configured for an embedded ELF target, we
1137 don't bother. */
1138 if (strcmp (TARGET_OS, "elf") != 0)
1139 {
1140 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1141 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1142 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1143 }
1144
1145 /* Create a .reginfo section for register masks and a .mdebug
1146 section for debugging information. */
1147 {
1148 segT seg;
1149 subsegT subseg;
1150 flagword flags;
1151 segT sec;
1152
1153 seg = now_seg;
1154 subseg = now_subseg;
1155
1156 /* The ABI says this section should be loaded so that the
1157 running program can access it. However, we don't load it
1158 if we are configured for an embedded target */
1159 flags = SEC_READONLY | SEC_DATA;
1160 if (strcmp (TARGET_OS, "elf") != 0)
1161 flags |= SEC_ALLOC | SEC_LOAD;
1162
316f5878 1163 if (mips_abi != N64_ABI)
252b5132
RH
1164 {
1165 sec = subseg_new (".reginfo", (subsegT) 0);
1166
195325d2
TS
1167 bfd_set_section_flags (stdoutput, sec, flags);
1168 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
bdaaa2e1 1169
252b5132
RH
1170#ifdef OBJ_ELF
1171 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1172#endif
1173 }
1174 else
1175 {
1176 /* The 64-bit ABI uses a .MIPS.options section rather than
1177 .reginfo section. */
1178 sec = subseg_new (".MIPS.options", (subsegT) 0);
195325d2
TS
1179 bfd_set_section_flags (stdoutput, sec, flags);
1180 bfd_set_section_alignment (stdoutput, sec, 3);
252b5132
RH
1181
1182#ifdef OBJ_ELF
1183 /* Set up the option header. */
1184 {
1185 Elf_Internal_Options opthdr;
1186 char *f;
1187
1188 opthdr.kind = ODK_REGINFO;
1189 opthdr.size = (sizeof (Elf_External_Options)
1190 + sizeof (Elf64_External_RegInfo));
1191 opthdr.section = 0;
1192 opthdr.info = 0;
1193 f = frag_more (sizeof (Elf_External_Options));
1194 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1195 (Elf_External_Options *) f);
1196
1197 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1198 }
1199#endif
1200 }
1201
1202 if (ECOFF_DEBUGGING)
1203 {
1204 sec = subseg_new (".mdebug", (subsegT) 0);
1205 (void) bfd_set_section_flags (stdoutput, sec,
1206 SEC_HAS_CONTENTS | SEC_READONLY);
1207 (void) bfd_set_section_alignment (stdoutput, sec, 2);
1208 }
ecb4347a
DJ
1209#ifdef OBJ_ELF
1210 else if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1211 {
1212 pdr_seg = subseg_new (".pdr", (subsegT) 0);
1213 (void) bfd_set_section_flags (stdoutput, pdr_seg,
1214 SEC_READONLY | SEC_RELOC
1215 | SEC_DEBUGGING);
1216 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1217 }
252b5132
RH
1218#endif
1219
1220 subseg_set (seg, subseg);
1221 }
1222 }
1223
1224 if (! ECOFF_DEBUGGING)
1225 md_obj_begin ();
1226}
1227
1228void
1229md_mips_end ()
1230{
1231 if (! ECOFF_DEBUGGING)
1232 md_obj_end ();
1233}
1234
1235void
1236md_assemble (str)
1237 char *str;
1238{
1239 struct mips_cl_insn insn;
f6688943
TS
1240 bfd_reloc_code_real_type unused_reloc[3]
1241 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132
RH
1242
1243 imm_expr.X_op = O_absent;
252b5132
RH
1244 imm_unmatched_hi = false;
1245 offset_expr.X_op = O_absent;
f6688943
TS
1246 imm_reloc[0] = BFD_RELOC_UNUSED;
1247 imm_reloc[1] = BFD_RELOC_UNUSED;
1248 imm_reloc[2] = BFD_RELOC_UNUSED;
1249 offset_reloc[0] = BFD_RELOC_UNUSED;
1250 offset_reloc[1] = BFD_RELOC_UNUSED;
1251 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
1252
1253 if (mips_opts.mips16)
1254 mips16_ip (str, &insn);
1255 else
1256 {
1257 mips_ip (str, &insn);
beae10d5
KH
1258 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1259 str, insn.insn_opcode));
252b5132
RH
1260 }
1261
1262 if (insn_error)
1263 {
1264 as_bad ("%s `%s'", insn_error, str);
1265 return;
1266 }
1267
1268 if (insn.insn_mo->pinfo == INSN_MACRO)
1269 {
1270 if (mips_opts.mips16)
1271 mips16_macro (&insn);
1272 else
1273 macro (&insn);
1274 }
1275 else
1276 {
1277 if (imm_expr.X_op != O_absent)
c4e7957c 1278 append_insn (NULL, &insn, &imm_expr, imm_reloc, imm_unmatched_hi);
252b5132 1279 else if (offset_expr.X_op != O_absent)
c4e7957c 1280 append_insn (NULL, &insn, &offset_expr, offset_reloc, false);
252b5132 1281 else
c4e7957c 1282 append_insn (NULL, &insn, NULL, unused_reloc, false);
252b5132
RH
1283 }
1284}
1285
1286/* See whether instruction IP reads register REG. CLASS is the type
1287 of register. */
1288
1289static int
1290insn_uses_reg (ip, reg, class)
1291 struct mips_cl_insn *ip;
1292 unsigned int reg;
1293 enum mips_regclass class;
1294{
1295 if (class == MIPS16_REG)
1296 {
1297 assert (mips_opts.mips16);
1298 reg = mips16_to_32_reg_map[reg];
1299 class = MIPS_GR_REG;
1300 }
1301
85b51719
TS
1302 /* Don't report on general register ZERO, since it never changes. */
1303 if (class == MIPS_GR_REG && reg == ZERO)
252b5132
RH
1304 return 0;
1305
1306 if (class == MIPS_FP_REG)
1307 {
1308 assert (! mips_opts.mips16);
1309 /* If we are called with either $f0 or $f1, we must check $f0.
1310 This is not optimal, because it will introduce an unnecessary
1311 NOP between "lwc1 $f0" and "swc1 $f1". To fix this we would
1312 need to distinguish reading both $f0 and $f1 or just one of
1313 them. Note that we don't have to check the other way,
1314 because there is no instruction that sets both $f0 and $f1
1315 and requires a delay. */
1316 if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1317 && ((((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS) &~(unsigned)1)
1318 == (reg &~ (unsigned) 1)))
1319 return 1;
1320 if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1321 && ((((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT) &~(unsigned)1)
1322 == (reg &~ (unsigned) 1)))
1323 return 1;
1324 }
1325 else if (! mips_opts.mips16)
1326 {
1327 if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1328 && ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == reg)
1329 return 1;
1330 if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1331 && ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT) == reg)
1332 return 1;
1333 }
1334 else
1335 {
1336 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1337 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RX)
1338 & MIPS16OP_MASK_RX)]
1339 == reg))
1340 return 1;
1341 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1342 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RY)
1343 & MIPS16OP_MASK_RY)]
1344 == reg))
1345 return 1;
1346 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1347 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
1348 & MIPS16OP_MASK_MOVE32Z)]
1349 == reg))
1350 return 1;
1351 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1352 return 1;
1353 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1354 return 1;
1355 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1356 return 1;
1357 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1358 && ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
1359 & MIPS16OP_MASK_REGR32) == reg)
1360 return 1;
1361 }
1362
1363 return 0;
1364}
1365
1366/* This function returns true if modifying a register requires a
1367 delay. */
1368
1369static int
1370reg_needs_delay (reg)
156c2f8b 1371 unsigned int reg;
252b5132
RH
1372{
1373 unsigned long prev_pinfo;
1374
1375 prev_pinfo = prev_insn.insn_mo->pinfo;
1376 if (! mips_opts.noreorder
9ce8a5dd 1377 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
1378 && ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1379 || (! gpr_interlocks
1380 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1381 {
1382 /* A load from a coprocessor or from memory. All load
1383 delays delay the use of general register rt for one
1384 instruction on the r3000. The r6000 and r4000 use
1385 interlocks. */
bdaaa2e1 1386 /* Itbl support may require additional care here. */
252b5132
RH
1387 know (prev_pinfo & INSN_WRITE_GPR_T);
1388 if (reg == ((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT))
1389 return 1;
1390 }
1391
1392 return 0;
1393}
1394
1395/* Mark instruction labels in mips16 mode. This permits the linker to
1396 handle them specially, such as generating jalx instructions when
1397 needed. We also make them odd for the duration of the assembly, in
1398 order to generate the right sort of code. We will make them even
1399 in the adjust_symtab routine, while leaving them marked. This is
1400 convenient for the debugger and the disassembler. The linker knows
1401 to make them odd again. */
1402
1403static void
1404mips16_mark_labels ()
1405{
1406 if (mips_opts.mips16)
1407 {
1408 struct insn_label_list *l;
98aa84af 1409 valueT val;
252b5132
RH
1410
1411 for (l = insn_labels; l != NULL; l = l->next)
1412 {
1413#ifdef OBJ_ELF
1414 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1415 S_SET_OTHER (l->label, STO_MIPS16);
1416#endif
98aa84af
AM
1417 val = S_GET_VALUE (l->label);
1418 if ((val & 1) == 0)
1419 S_SET_VALUE (l->label, val + 1);
252b5132
RH
1420 }
1421 }
1422}
1423
1424/* Output an instruction. PLACE is where to put the instruction; if
1425 it is NULL, this uses frag_more to get room. IP is the instruction
1426 information. ADDRESS_EXPR is an operand of the instruction to be
1427 used with RELOC_TYPE. */
1428
1429static void
1430append_insn (place, ip, address_expr, reloc_type, unmatched_hi)
1431 char *place;
1432 struct mips_cl_insn *ip;
1433 expressionS *address_expr;
f6688943 1434 bfd_reloc_code_real_type *reloc_type;
252b5132
RH
1435 boolean unmatched_hi;
1436{
1437 register unsigned long prev_pinfo, pinfo;
1438 char *f;
f6688943 1439 fixS *fixp[3];
252b5132
RH
1440 int nops = 0;
1441
1442 /* Mark instruction labels in mips16 mode. */
f9419b05 1443 mips16_mark_labels ();
252b5132
RH
1444
1445 prev_pinfo = prev_insn.insn_mo->pinfo;
1446 pinfo = ip->insn_mo->pinfo;
1447
1448 if (place == NULL && (! mips_opts.noreorder || prev_nop_frag != NULL))
1449 {
1450 int prev_prev_nop;
1451
1452 /* If the previous insn required any delay slots, see if we need
1453 to insert a NOP or two. There are eight kinds of possible
1454 hazards, of which an instruction can have at most one type.
1455 (1) a load from memory delay
1456 (2) a load from a coprocessor delay
1457 (3) an unconditional branch delay
1458 (4) a conditional branch delay
1459 (5) a move to coprocessor register delay
1460 (6) a load coprocessor register from memory delay
1461 (7) a coprocessor condition code delay
1462 (8) a HI/LO special register delay
1463
1464 There are a lot of optimizations we could do that we don't.
1465 In particular, we do not, in general, reorder instructions.
1466 If you use gcc with optimization, it will reorder
1467 instructions and generally do much more optimization then we
1468 do here; repeating all that work in the assembler would only
1469 benefit hand written assembly code, and does not seem worth
1470 it. */
1471
1472 /* This is how a NOP is emitted. */
1473#define emit_nop() \
1474 (mips_opts.mips16 \
1475 ? md_number_to_chars (frag_more (2), 0x6500, 2) \
1476 : md_number_to_chars (frag_more (4), 0, 4))
1477
1478 /* The previous insn might require a delay slot, depending upon
1479 the contents of the current insn. */
1480 if (! mips_opts.mips16
9ce8a5dd 1481 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
1482 && (((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1483 && ! cop_interlocks)
1484 || (! gpr_interlocks
1485 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1486 {
1487 /* A load from a coprocessor or from memory. All load
1488 delays delay the use of general register rt for one
1489 instruction on the r3000. The r6000 and r4000 use
1490 interlocks. */
beae10d5 1491 /* Itbl support may require additional care here. */
252b5132
RH
1492 know (prev_pinfo & INSN_WRITE_GPR_T);
1493 if (mips_optimize == 0
1494 || insn_uses_reg (ip,
1495 ((prev_insn.insn_opcode >> OP_SH_RT)
1496 & OP_MASK_RT),
1497 MIPS_GR_REG))
1498 ++nops;
1499 }
1500 else if (! mips_opts.mips16
9ce8a5dd 1501 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132 1502 && (((prev_pinfo & INSN_COPROC_MOVE_DELAY)
beae10d5 1503 && ! cop_interlocks)
e7af610e 1504 || (mips_opts.isa == ISA_MIPS1
252b5132
RH
1505 && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))))
1506 {
1507 /* A generic coprocessor delay. The previous instruction
1508 modified a coprocessor general or control register. If
1509 it modified a control register, we need to avoid any
1510 coprocessor instruction (this is probably not always
1511 required, but it sometimes is). If it modified a general
1512 register, we avoid using that register.
1513
1514 On the r6000 and r4000 loading a coprocessor register
1515 from memory is interlocked, and does not require a delay.
1516
1517 This case is not handled very well. There is no special
1518 knowledge of CP0 handling, and the coprocessors other
1519 than the floating point unit are not distinguished at
1520 all. */
1521 /* Itbl support may require additional care here. FIXME!
bdaaa2e1 1522 Need to modify this to include knowledge about
252b5132
RH
1523 user specified delays! */
1524 if (prev_pinfo & INSN_WRITE_FPR_T)
1525 {
1526 if (mips_optimize == 0
1527 || insn_uses_reg (ip,
1528 ((prev_insn.insn_opcode >> OP_SH_FT)
1529 & OP_MASK_FT),
1530 MIPS_FP_REG))
1531 ++nops;
1532 }
1533 else if (prev_pinfo & INSN_WRITE_FPR_S)
1534 {
1535 if (mips_optimize == 0
1536 || insn_uses_reg (ip,
1537 ((prev_insn.insn_opcode >> OP_SH_FS)
1538 & OP_MASK_FS),
1539 MIPS_FP_REG))
1540 ++nops;
1541 }
1542 else
1543 {
1544 /* We don't know exactly what the previous instruction
1545 does. If the current instruction uses a coprocessor
1546 register, we must insert a NOP. If previous
1547 instruction may set the condition codes, and the
1548 current instruction uses them, we must insert two
1549 NOPS. */
bdaaa2e1 1550 /* Itbl support may require additional care here. */
252b5132
RH
1551 if (mips_optimize == 0
1552 || ((prev_pinfo & INSN_WRITE_COND_CODE)
1553 && (pinfo & INSN_READ_COND_CODE)))
1554 nops += 2;
1555 else if (pinfo & INSN_COP)
1556 ++nops;
1557 }
1558 }
1559 else if (! mips_opts.mips16
9ce8a5dd 1560 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
1561 && (prev_pinfo & INSN_WRITE_COND_CODE)
1562 && ! cop_interlocks)
1563 {
1564 /* The previous instruction sets the coprocessor condition
1565 codes, but does not require a general coprocessor delay
1566 (this means it is a floating point comparison
1567 instruction). If this instruction uses the condition
1568 codes, we need to insert a single NOP. */
beae10d5 1569 /* Itbl support may require additional care here. */
252b5132
RH
1570 if (mips_optimize == 0
1571 || (pinfo & INSN_READ_COND_CODE))
1572 ++nops;
1573 }
6b76fefe
CM
1574
1575 /* If we're fixing up mfhi/mflo for the r7000 and the
1576 previous insn was an mfhi/mflo and the current insn
1577 reads the register that the mfhi/mflo wrote to, then
1578 insert two nops. */
1579
1580 else if (mips_7000_hilo_fix
1581 && MF_HILO_INSN (prev_pinfo)
1582 && insn_uses_reg (ip, ((prev_insn.insn_opcode >> OP_SH_RD)
beae10d5
KH
1583 & OP_MASK_RD),
1584 MIPS_GR_REG))
6b76fefe
CM
1585 {
1586 nops += 2;
1587 }
1588
1589 /* If we're fixing up mfhi/mflo for the r7000 and the
1590 2nd previous insn was an mfhi/mflo and the current insn
1591 reads the register that the mfhi/mflo wrote to, then
1592 insert one nop. */
1593
1594 else if (mips_7000_hilo_fix
1595 && MF_HILO_INSN (prev_prev_insn.insn_opcode)
1596 && insn_uses_reg (ip, ((prev_prev_insn.insn_opcode >> OP_SH_RD)
1597 & OP_MASK_RD),
1598 MIPS_GR_REG))
bdaaa2e1 1599
6b76fefe 1600 {
f9419b05 1601 ++nops;
6b76fefe 1602 }
bdaaa2e1 1603
252b5132
RH
1604 else if (prev_pinfo & INSN_READ_LO)
1605 {
1606 /* The previous instruction reads the LO register; if the
1607 current instruction writes to the LO register, we must
bdaaa2e1
KH
1608 insert two NOPS. Some newer processors have interlocks.
1609 Also the tx39's multiply instructions can be exectuted
252b5132 1610 immediatly after a read from HI/LO (without the delay),
bdaaa2e1
KH
1611 though the tx39's divide insns still do require the
1612 delay. */
252b5132 1613 if (! (hilo_interlocks
ec68c924 1614 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
252b5132
RH
1615 && (mips_optimize == 0
1616 || (pinfo & INSN_WRITE_LO)))
1617 nops += 2;
1618 /* Most mips16 branch insns don't have a delay slot.
1619 If a read from LO is immediately followed by a branch
1620 to a write to LO we have a read followed by a write
1621 less than 2 insns away. We assume the target of
1622 a branch might be a write to LO, and insert a nop
bdaaa2e1 1623 between a read and an immediately following branch. */
252b5132
RH
1624 else if (mips_opts.mips16
1625 && (mips_optimize == 0
1626 || (pinfo & MIPS16_INSN_BRANCH)))
f9419b05 1627 ++nops;
252b5132
RH
1628 }
1629 else if (prev_insn.insn_mo->pinfo & INSN_READ_HI)
1630 {
1631 /* The previous instruction reads the HI register; if the
1632 current instruction writes to the HI register, we must
1633 insert a NOP. Some newer processors have interlocks.
bdaaa2e1 1634 Also the note tx39's multiply above. */
252b5132 1635 if (! (hilo_interlocks
ec68c924 1636 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
252b5132
RH
1637 && (mips_optimize == 0
1638 || (pinfo & INSN_WRITE_HI)))
1639 nops += 2;
1640 /* Most mips16 branch insns don't have a delay slot.
1641 If a read from HI is immediately followed by a branch
1642 to a write to HI we have a read followed by a write
1643 less than 2 insns away. We assume the target of
1644 a branch might be a write to HI, and insert a nop
bdaaa2e1 1645 between a read and an immediately following branch. */
252b5132
RH
1646 else if (mips_opts.mips16
1647 && (mips_optimize == 0
1648 || (pinfo & MIPS16_INSN_BRANCH)))
f9419b05 1649 ++nops;
252b5132
RH
1650 }
1651
1652 /* If the previous instruction was in a noreorder section, then
1653 we don't want to insert the nop after all. */
bdaaa2e1 1654 /* Itbl support may require additional care here. */
252b5132
RH
1655 if (prev_insn_unreordered)
1656 nops = 0;
1657
1658 /* There are two cases which require two intervening
1659 instructions: 1) setting the condition codes using a move to
1660 coprocessor instruction which requires a general coprocessor
1661 delay and then reading the condition codes 2) reading the HI
1662 or LO register and then writing to it (except on processors
1663 which have interlocks). If we are not already emitting a NOP
1664 instruction, we must check for these cases compared to the
1665 instruction previous to the previous instruction. */
1666 if ((! mips_opts.mips16
9ce8a5dd 1667 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
1668 && (prev_prev_insn.insn_mo->pinfo & INSN_COPROC_MOVE_DELAY)
1669 && (prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
1670 && (pinfo & INSN_READ_COND_CODE)
1671 && ! cop_interlocks)
1672 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_LO)
1673 && (pinfo & INSN_WRITE_LO)
1674 && ! (hilo_interlocks
ec68c924 1675 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT))))
252b5132
RH
1676 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
1677 && (pinfo & INSN_WRITE_HI)
1678 && ! (hilo_interlocks
ec68c924 1679 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))))
252b5132
RH
1680 prev_prev_nop = 1;
1681 else
1682 prev_prev_nop = 0;
1683
1684 if (prev_prev_insn_unreordered)
1685 prev_prev_nop = 0;
1686
1687 if (prev_prev_nop && nops == 0)
1688 ++nops;
1689
1690 /* If we are being given a nop instruction, don't bother with
1691 one of the nops we would otherwise output. This will only
1692 happen when a nop instruction is used with mips_optimize set
1693 to 0. */
1694 if (nops > 0
1695 && ! mips_opts.noreorder
156c2f8b 1696 && ip->insn_opcode == (unsigned) (mips_opts.mips16 ? 0x6500 : 0))
252b5132
RH
1697 --nops;
1698
1699 /* Now emit the right number of NOP instructions. */
1700 if (nops > 0 && ! mips_opts.noreorder)
1701 {
1702 fragS *old_frag;
1703 unsigned long old_frag_offset;
1704 int i;
1705 struct insn_label_list *l;
1706
1707 old_frag = frag_now;
1708 old_frag_offset = frag_now_fix ();
1709
1710 for (i = 0; i < nops; i++)
1711 emit_nop ();
1712
1713 if (listing)
1714 {
1715 listing_prev_line ();
1716 /* We may be at the start of a variant frag. In case we
1717 are, make sure there is enough space for the frag
1718 after the frags created by listing_prev_line. The
1719 argument to frag_grow here must be at least as large
1720 as the argument to all other calls to frag_grow in
1721 this file. We don't have to worry about being in the
1722 middle of a variant frag, because the variants insert
1723 all needed nop instructions themselves. */
1724 frag_grow (40);
1725 }
1726
1727 for (l = insn_labels; l != NULL; l = l->next)
1728 {
98aa84af
AM
1729 valueT val;
1730
252b5132 1731 assert (S_GET_SEGMENT (l->label) == now_seg);
49309057 1732 symbol_set_frag (l->label, frag_now);
98aa84af 1733 val = (valueT) frag_now_fix ();
252b5132
RH
1734 /* mips16 text labels are stored as odd. */
1735 if (mips_opts.mips16)
f9419b05 1736 ++val;
98aa84af 1737 S_SET_VALUE (l->label, val);
252b5132
RH
1738 }
1739
1740#ifndef NO_ECOFF_DEBUGGING
1741 if (ECOFF_DEBUGGING)
1742 ecoff_fix_loc (old_frag, old_frag_offset);
1743#endif
1744 }
1745 else if (prev_nop_frag != NULL)
1746 {
1747 /* We have a frag holding nops we may be able to remove. If
1748 we don't need any nops, we can decrease the size of
1749 prev_nop_frag by the size of one instruction. If we do
bdaaa2e1 1750 need some nops, we count them in prev_nops_required. */
252b5132
RH
1751 if (prev_nop_frag_since == 0)
1752 {
1753 if (nops == 0)
1754 {
1755 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1756 --prev_nop_frag_holds;
1757 }
1758 else
1759 prev_nop_frag_required += nops;
1760 }
1761 else
1762 {
1763 if (prev_prev_nop == 0)
1764 {
1765 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1766 --prev_nop_frag_holds;
1767 }
1768 else
1769 ++prev_nop_frag_required;
1770 }
1771
1772 if (prev_nop_frag_holds <= prev_nop_frag_required)
1773 prev_nop_frag = NULL;
1774
1775 ++prev_nop_frag_since;
1776
1777 /* Sanity check: by the time we reach the second instruction
1778 after prev_nop_frag, we should have used up all the nops
1779 one way or another. */
1780 assert (prev_nop_frag_since <= 1 || prev_nop_frag == NULL);
1781 }
1782 }
1783
f6688943 1784 if (*reloc_type > BFD_RELOC_UNUSED)
252b5132
RH
1785 {
1786 /* We need to set up a variant frag. */
1787 assert (mips_opts.mips16 && address_expr != NULL);
1788 f = frag_var (rs_machine_dependent, 4, 0,
f6688943 1789 RELAX_MIPS16_ENCODE (*reloc_type - BFD_RELOC_UNUSED,
252b5132
RH
1790 mips16_small, mips16_ext,
1791 (prev_pinfo
1792 & INSN_UNCOND_BRANCH_DELAY),
f6688943 1793 (*prev_insn_reloc_type
252b5132 1794 == BFD_RELOC_MIPS16_JMP)),
c4e7957c 1795 make_expr_symbol (address_expr), 0, NULL);
252b5132
RH
1796 }
1797 else if (place != NULL)
1798 f = place;
1799 else if (mips_opts.mips16
1800 && ! ip->use_extend
f6688943 1801 && *reloc_type != BFD_RELOC_MIPS16_JMP)
252b5132
RH
1802 {
1803 /* Make sure there is enough room to swap this instruction with
1804 a following jump instruction. */
1805 frag_grow (6);
1806 f = frag_more (2);
1807 }
1808 else
1809 {
1810 if (mips_opts.mips16
1811 && mips_opts.noreorder
1812 && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
1813 as_warn (_("extended instruction in delay slot"));
1814
1815 f = frag_more (4);
1816 }
1817
f6688943
TS
1818 fixp[0] = fixp[1] = fixp[2] = NULL;
1819 if (address_expr != NULL && *reloc_type < BFD_RELOC_UNUSED)
252b5132
RH
1820 {
1821 if (address_expr->X_op == O_constant)
1822 {
4db1a35d 1823 valueT tmp;
f6688943
TS
1824
1825 switch (*reloc_type)
252b5132
RH
1826 {
1827 case BFD_RELOC_32:
1828 ip->insn_opcode |= address_expr->X_add_number;
1829 break;
1830
f6688943
TS
1831 case BFD_RELOC_MIPS_HIGHEST:
1832 tmp = (address_expr->X_add_number + 0x800080008000) >> 16;
1833 tmp >>= 16;
1834 ip->insn_opcode |= (tmp >> 16) & 0xffff;
1835 break;
1836
1837 case BFD_RELOC_MIPS_HIGHER:
1838 tmp = (address_expr->X_add_number + 0x80008000) >> 16;
1839 ip->insn_opcode |= (tmp >> 16) & 0xffff;
1840 break;
1841
1842 case BFD_RELOC_HI16_S:
1843 ip->insn_opcode |= ((address_expr->X_add_number + 0x8000)
1844 >> 16) & 0xffff;
1845 break;
1846
1847 case BFD_RELOC_HI16:
1848 ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
1849 break;
1850
252b5132
RH
1851 case BFD_RELOC_LO16:
1852 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
1853 break;
1854
1855 case BFD_RELOC_MIPS_JMP:
1856 if ((address_expr->X_add_number & 3) != 0)
1857 as_bad (_("jump to misaligned address (0x%lx)"),
1858 (unsigned long) address_expr->X_add_number);
7496292d
TS
1859 if (address_expr->X_add_number & ~0xfffffff
1860 || address_expr->X_add_number > 0x7fffffc)
1861 as_bad (_("jump address range overflow (0x%lx)"),
1862 (unsigned long) address_expr->X_add_number);
252b5132
RH
1863 ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
1864 break;
1865
1866 case BFD_RELOC_MIPS16_JMP:
1867 if ((address_expr->X_add_number & 3) != 0)
1868 as_bad (_("jump to misaligned address (0x%lx)"),
1869 (unsigned long) address_expr->X_add_number);
7496292d
TS
1870 if (address_expr->X_add_number & ~0xfffffff
1871 || address_expr->X_add_number > 0x7fffffc)
1872 as_bad (_("jump address range overflow (0x%lx)"),
1873 (unsigned long) address_expr->X_add_number);
252b5132
RH
1874 ip->insn_opcode |=
1875 (((address_expr->X_add_number & 0x7c0000) << 3)
1876 | ((address_expr->X_add_number & 0xf800000) >> 7)
1877 | ((address_expr->X_add_number & 0x3fffc) >> 2));
1878 break;
1879
cb56d3d3 1880 case BFD_RELOC_16_PCREL:
233b8738 1881 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
cb56d3d3
TS
1882 break;
1883
252b5132
RH
1884 case BFD_RELOC_16_PCREL_S2:
1885 goto need_reloc;
1886
1887 default:
1888 internalError ();
1889 }
1890 }
1891 else
1892 {
1893 need_reloc:
f6688943 1894 /* Don't generate a reloc if we are writing into a variant frag. */
252b5132
RH
1895 if (place == NULL)
1896 {
f6688943
TS
1897 fixp[0] = fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
1898 address_expr,
1899 (*reloc_type == BFD_RELOC_16_PCREL
1900 || *reloc_type == BFD_RELOC_16_PCREL_S2),
1901 reloc_type[0]);
1902
b6ff326e 1903 /* These relocations can have an addend that won't fit in
f6688943
TS
1904 4 octets for 64bit assembly. */
1905 if (HAVE_64BIT_GPRS &&
1906 (*reloc_type == BFD_RELOC_16
98d3f06f
KH
1907 || *reloc_type == BFD_RELOC_32
1908 || *reloc_type == BFD_RELOC_MIPS_JMP
1909 || *reloc_type == BFD_RELOC_HI16_S
1910 || *reloc_type == BFD_RELOC_LO16
1911 || *reloc_type == BFD_RELOC_GPREL16
1912 || *reloc_type == BFD_RELOC_MIPS_LITERAL
1913 || *reloc_type == BFD_RELOC_GPREL32
1914 || *reloc_type == BFD_RELOC_64
1915 || *reloc_type == BFD_RELOC_CTOR
1916 || *reloc_type == BFD_RELOC_MIPS_SUB
1917 || *reloc_type == BFD_RELOC_MIPS_HIGHEST
1918 || *reloc_type == BFD_RELOC_MIPS_HIGHER
1919 || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
1920 || *reloc_type == BFD_RELOC_MIPS_REL16
1921 || *reloc_type == BFD_RELOC_MIPS_RELGOT))
f6688943
TS
1922 fixp[0]->fx_no_overflow = 1;
1923
252b5132
RH
1924 if (unmatched_hi)
1925 {
1926 struct mips_hi_fixup *hi_fixup;
1927
f6688943 1928 assert (*reloc_type == BFD_RELOC_HI16_S);
252b5132
RH
1929 hi_fixup = ((struct mips_hi_fixup *)
1930 xmalloc (sizeof (struct mips_hi_fixup)));
f6688943 1931 hi_fixup->fixp = fixp[0];
252b5132
RH
1932 hi_fixup->seg = now_seg;
1933 hi_fixup->next = mips_hi_fixup_list;
1934 mips_hi_fixup_list = hi_fixup;
1935 }
f6688943
TS
1936
1937 if (reloc_type[1] != BFD_RELOC_UNUSED)
1938 {
1939 /* FIXME: This symbol can be one of
1940 RSS_UNDEF, RSS_GP, RSS_GP0, RSS_LOC. */
1941 address_expr->X_op = O_absent;
1942 address_expr->X_add_symbol = 0;
1943 address_expr->X_add_number = 0;
1944
1945 fixp[1] = fix_new_exp (frag_now, f - frag_now->fr_literal,
1946 4, address_expr, false,
1947 reloc_type[1]);
1948
b6ff326e 1949 /* These relocations can have an addend that won't fit in
f6688943
TS
1950 4 octets for 64bit assembly. */
1951 if (HAVE_64BIT_GPRS &&
1952 (*reloc_type == BFD_RELOC_16
1953 || *reloc_type == BFD_RELOC_32
1954 || *reloc_type == BFD_RELOC_MIPS_JMP
1955 || *reloc_type == BFD_RELOC_HI16_S
1956 || *reloc_type == BFD_RELOC_LO16
1957 || *reloc_type == BFD_RELOC_GPREL16
1958 || *reloc_type == BFD_RELOC_MIPS_LITERAL
1959 || *reloc_type == BFD_RELOC_GPREL32
1960 || *reloc_type == BFD_RELOC_64
1961 || *reloc_type == BFD_RELOC_CTOR
1962 || *reloc_type == BFD_RELOC_MIPS_SUB
1963 || *reloc_type == BFD_RELOC_MIPS_HIGHEST
1964 || *reloc_type == BFD_RELOC_MIPS_HIGHER
1965 || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
1966 || *reloc_type == BFD_RELOC_MIPS_REL16
1967 || *reloc_type == BFD_RELOC_MIPS_RELGOT))
98d3f06f 1968 fixp[1]->fx_no_overflow = 1;
f6688943
TS
1969
1970 if (reloc_type[2] != BFD_RELOC_UNUSED)
1971 {
1972 address_expr->X_op = O_absent;
1973 address_expr->X_add_symbol = 0;
1974 address_expr->X_add_number = 0;
1975
1976 fixp[2] = fix_new_exp (frag_now,
1977 f - frag_now->fr_literal, 4,
1978 address_expr, false,
1979 reloc_type[2]);
1980
b6ff326e 1981 /* These relocations can have an addend that won't fit in
f6688943
TS
1982 4 octets for 64bit assembly. */
1983 if (HAVE_64BIT_GPRS &&
1984 (*reloc_type == BFD_RELOC_16
1985 || *reloc_type == BFD_RELOC_32
1986 || *reloc_type == BFD_RELOC_MIPS_JMP
1987 || *reloc_type == BFD_RELOC_HI16_S
1988 || *reloc_type == BFD_RELOC_LO16
1989 || *reloc_type == BFD_RELOC_GPREL16
1990 || *reloc_type == BFD_RELOC_MIPS_LITERAL
1991 || *reloc_type == BFD_RELOC_GPREL32
1992 || *reloc_type == BFD_RELOC_64
1993 || *reloc_type == BFD_RELOC_CTOR
1994 || *reloc_type == BFD_RELOC_MIPS_SUB
1995 || *reloc_type == BFD_RELOC_MIPS_HIGHEST
1996 || *reloc_type == BFD_RELOC_MIPS_HIGHER
1997 || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
1998 || *reloc_type == BFD_RELOC_MIPS_REL16
1999 || *reloc_type == BFD_RELOC_MIPS_RELGOT))
98d3f06f 2000 fixp[2]->fx_no_overflow = 1;
f6688943
TS
2001 }
2002 }
252b5132
RH
2003 }
2004 }
2005 }
2006
2007 if (! mips_opts.mips16)
c5dd6aab
DJ
2008 {
2009 md_number_to_chars (f, ip->insn_opcode, 4);
2010#ifdef OBJ_ELF
2011 dwarf2_emit_insn (4);
2012#endif
2013 }
f6688943 2014 else if (*reloc_type == BFD_RELOC_MIPS16_JMP)
252b5132
RH
2015 {
2016 md_number_to_chars (f, ip->insn_opcode >> 16, 2);
2017 md_number_to_chars (f + 2, ip->insn_opcode & 0xffff, 2);
c5dd6aab
DJ
2018#ifdef OBJ_ELF
2019 dwarf2_emit_insn (4);
2020#endif
252b5132
RH
2021 }
2022 else
2023 {
2024 if (ip->use_extend)
2025 {
2026 md_number_to_chars (f, 0xf000 | ip->extend, 2);
2027 f += 2;
2028 }
2029 md_number_to_chars (f, ip->insn_opcode, 2);
c5dd6aab
DJ
2030#ifdef OBJ_ELF
2031 dwarf2_emit_insn (ip->use_extend ? 4 : 2);
2032#endif
252b5132
RH
2033 }
2034
2035 /* Update the register mask information. */
2036 if (! mips_opts.mips16)
2037 {
2038 if (pinfo & INSN_WRITE_GPR_D)
2039 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
2040 if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2041 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
2042 if (pinfo & INSN_READ_GPR_S)
2043 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
2044 if (pinfo & INSN_WRITE_GPR_31)
f9419b05 2045 mips_gprmask |= 1 << RA;
252b5132
RH
2046 if (pinfo & INSN_WRITE_FPR_D)
2047 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
2048 if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2049 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
2050 if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2051 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
2052 if ((pinfo & INSN_READ_FPR_R) != 0)
2053 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
2054 if (pinfo & INSN_COP)
2055 {
bdaaa2e1
KH
2056 /* We don't keep enough information to sort these cases out.
2057 The itbl support does keep this information however, although
2058 we currently don't support itbl fprmats as part of the cop
2059 instruction. May want to add this support in the future. */
252b5132
RH
2060 }
2061 /* Never set the bit for $0, which is always zero. */
beae10d5 2062 mips_gprmask &= ~1 << 0;
252b5132
RH
2063 }
2064 else
2065 {
2066 if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2067 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RX)
2068 & MIPS16OP_MASK_RX);
2069 if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2070 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RY)
2071 & MIPS16OP_MASK_RY);
2072 if (pinfo & MIPS16_INSN_WRITE_Z)
2073 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RZ)
2074 & MIPS16OP_MASK_RZ);
2075 if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2076 mips_gprmask |= 1 << TREG;
2077 if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2078 mips_gprmask |= 1 << SP;
2079 if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2080 mips_gprmask |= 1 << RA;
2081 if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2082 mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2083 if (pinfo & MIPS16_INSN_READ_Z)
2084 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
2085 & MIPS16OP_MASK_MOVE32Z);
2086 if (pinfo & MIPS16_INSN_READ_GPR_X)
2087 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
2088 & MIPS16OP_MASK_REGR32);
2089 }
2090
2091 if (place == NULL && ! mips_opts.noreorder)
2092 {
2093 /* Filling the branch delay slot is more complex. We try to
2094 switch the branch with the previous instruction, which we can
2095 do if the previous instruction does not set up a condition
2096 that the branch tests and if the branch is not itself the
2097 target of any branch. */
2098 if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2099 || (pinfo & INSN_COND_BRANCH_DELAY))
2100 {
2101 if (mips_optimize < 2
2102 /* If we have seen .set volatile or .set nomove, don't
2103 optimize. */
2104 || mips_opts.nomove != 0
2105 /* If we had to emit any NOP instructions, then we
2106 already know we can not swap. */
2107 || nops != 0
2108 /* If we don't even know the previous insn, we can not
bdaaa2e1 2109 swap. */
252b5132
RH
2110 || ! prev_insn_valid
2111 /* If the previous insn is already in a branch delay
2112 slot, then we can not swap. */
2113 || prev_insn_is_delay_slot
2114 /* If the previous previous insn was in a .set
2115 noreorder, we can't swap. Actually, the MIPS
2116 assembler will swap in this situation. However, gcc
2117 configured -with-gnu-as will generate code like
2118 .set noreorder
2119 lw $4,XXX
2120 .set reorder
2121 INSN
2122 bne $4,$0,foo
2123 in which we can not swap the bne and INSN. If gcc is
2124 not configured -with-gnu-as, it does not output the
2125 .set pseudo-ops. We don't have to check
2126 prev_insn_unreordered, because prev_insn_valid will
2127 be 0 in that case. We don't want to use
2128 prev_prev_insn_valid, because we do want to be able
2129 to swap at the start of a function. */
2130 || prev_prev_insn_unreordered
2131 /* If the branch is itself the target of a branch, we
2132 can not swap. We cheat on this; all we check for is
2133 whether there is a label on this instruction. If
2134 there are any branches to anything other than a
2135 label, users must use .set noreorder. */
2136 || insn_labels != NULL
2137 /* If the previous instruction is in a variant frag, we
2138 can not do the swap. This does not apply to the
2139 mips16, which uses variant frags for different
2140 purposes. */
2141 || (! mips_opts.mips16
2142 && prev_insn_frag->fr_type == rs_machine_dependent)
2143 /* If the branch reads the condition codes, we don't
2144 even try to swap, because in the sequence
2145 ctc1 $X,$31
2146 INSN
2147 INSN
2148 bc1t LABEL
2149 we can not swap, and I don't feel like handling that
2150 case. */
2151 || (! mips_opts.mips16
9ce8a5dd 2152 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
2153 && (pinfo & INSN_READ_COND_CODE))
2154 /* We can not swap with an instruction that requires a
2155 delay slot, becase the target of the branch might
2156 interfere with that instruction. */
2157 || (! mips_opts.mips16
9ce8a5dd 2158 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132 2159 && (prev_pinfo
bdaaa2e1 2160 /* Itbl support may require additional care here. */
252b5132
RH
2161 & (INSN_LOAD_COPROC_DELAY
2162 | INSN_COPROC_MOVE_DELAY
2163 | INSN_WRITE_COND_CODE)))
2164 || (! (hilo_interlocks
ec68c924 2165 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
252b5132
RH
2166 && (prev_pinfo
2167 & (INSN_READ_LO
2168 | INSN_READ_HI)))
2169 || (! mips_opts.mips16
2170 && ! gpr_interlocks
2171 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))
2172 || (! mips_opts.mips16
e7af610e 2173 && mips_opts.isa == ISA_MIPS1
bdaaa2e1 2174 /* Itbl support may require additional care here. */
252b5132
RH
2175 && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))
2176 /* We can not swap with a branch instruction. */
2177 || (prev_pinfo
2178 & (INSN_UNCOND_BRANCH_DELAY
2179 | INSN_COND_BRANCH_DELAY
2180 | INSN_COND_BRANCH_LIKELY))
2181 /* We do not swap with a trap instruction, since it
2182 complicates trap handlers to have the trap
2183 instruction be in a delay slot. */
2184 || (prev_pinfo & INSN_TRAP)
2185 /* If the branch reads a register that the previous
2186 instruction sets, we can not swap. */
2187 || (! mips_opts.mips16
2188 && (prev_pinfo & INSN_WRITE_GPR_T)
2189 && insn_uses_reg (ip,
2190 ((prev_insn.insn_opcode >> OP_SH_RT)
2191 & OP_MASK_RT),
2192 MIPS_GR_REG))
2193 || (! mips_opts.mips16
2194 && (prev_pinfo & INSN_WRITE_GPR_D)
2195 && insn_uses_reg (ip,
2196 ((prev_insn.insn_opcode >> OP_SH_RD)
2197 & OP_MASK_RD),
2198 MIPS_GR_REG))
2199 || (mips_opts.mips16
2200 && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2201 && insn_uses_reg (ip,
2202 ((prev_insn.insn_opcode
2203 >> MIPS16OP_SH_RX)
2204 & MIPS16OP_MASK_RX),
2205 MIPS16_REG))
2206 || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2207 && insn_uses_reg (ip,
2208 ((prev_insn.insn_opcode
2209 >> MIPS16OP_SH_RY)
2210 & MIPS16OP_MASK_RY),
2211 MIPS16_REG))
2212 || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2213 && insn_uses_reg (ip,
2214 ((prev_insn.insn_opcode
2215 >> MIPS16OP_SH_RZ)
2216 & MIPS16OP_MASK_RZ),
2217 MIPS16_REG))
2218 || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2219 && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2220 || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2221 && insn_uses_reg (ip, RA, MIPS_GR_REG))
2222 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2223 && insn_uses_reg (ip,
2224 MIPS16OP_EXTRACT_REG32R (prev_insn.
2225 insn_opcode),
2226 MIPS_GR_REG))))
2227 /* If the branch writes a register that the previous
2228 instruction sets, we can not swap (we know that
2229 branches write only to RD or to $31). */
2230 || (! mips_opts.mips16
2231 && (prev_pinfo & INSN_WRITE_GPR_T)
2232 && (((pinfo & INSN_WRITE_GPR_D)
2233 && (((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT)
2234 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2235 || ((pinfo & INSN_WRITE_GPR_31)
2236 && (((prev_insn.insn_opcode >> OP_SH_RT)
2237 & OP_MASK_RT)
f9419b05 2238 == RA))))
252b5132
RH
2239 || (! mips_opts.mips16
2240 && (prev_pinfo & INSN_WRITE_GPR_D)
2241 && (((pinfo & INSN_WRITE_GPR_D)
2242 && (((prev_insn.insn_opcode >> OP_SH_RD) & OP_MASK_RD)
2243 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2244 || ((pinfo & INSN_WRITE_GPR_31)
2245 && (((prev_insn.insn_opcode >> OP_SH_RD)
2246 & OP_MASK_RD)
f9419b05 2247 == RA))))
252b5132
RH
2248 || (mips_opts.mips16
2249 && (pinfo & MIPS16_INSN_WRITE_31)
2250 && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2251 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2252 && (MIPS16OP_EXTRACT_REG32R (prev_insn.insn_opcode)
2253 == RA))))
2254 /* If the branch writes a register that the previous
2255 instruction reads, we can not swap (we know that
2256 branches only write to RD or to $31). */
2257 || (! mips_opts.mips16
2258 && (pinfo & INSN_WRITE_GPR_D)
2259 && insn_uses_reg (&prev_insn,
2260 ((ip->insn_opcode >> OP_SH_RD)
2261 & OP_MASK_RD),
2262 MIPS_GR_REG))
2263 || (! mips_opts.mips16
2264 && (pinfo & INSN_WRITE_GPR_31)
f9419b05 2265 && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
252b5132
RH
2266 || (mips_opts.mips16
2267 && (pinfo & MIPS16_INSN_WRITE_31)
2268 && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2269 /* If we are generating embedded PIC code, the branch
2270 might be expanded into a sequence which uses $at, so
2271 we can't swap with an instruction which reads it. */
2272 || (mips_pic == EMBEDDED_PIC
2273 && insn_uses_reg (&prev_insn, AT, MIPS_GR_REG))
2274 /* If the previous previous instruction has a load
2275 delay, and sets a register that the branch reads, we
2276 can not swap. */
2277 || (! mips_opts.mips16
9ce8a5dd 2278 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
bdaaa2e1 2279 /* Itbl support may require additional care here. */
252b5132
RH
2280 && ((prev_prev_insn.insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
2281 || (! gpr_interlocks
2282 && (prev_prev_insn.insn_mo->pinfo
2283 & INSN_LOAD_MEMORY_DELAY)))
2284 && insn_uses_reg (ip,
2285 ((prev_prev_insn.insn_opcode >> OP_SH_RT)
2286 & OP_MASK_RT),
2287 MIPS_GR_REG))
2288 /* If one instruction sets a condition code and the
2289 other one uses a condition code, we can not swap. */
2290 || ((pinfo & INSN_READ_COND_CODE)
2291 && (prev_pinfo & INSN_WRITE_COND_CODE))
2292 || ((pinfo & INSN_WRITE_COND_CODE)
2293 && (prev_pinfo & INSN_READ_COND_CODE))
2294 /* If the previous instruction uses the PC, we can not
2295 swap. */
2296 || (mips_opts.mips16
2297 && (prev_pinfo & MIPS16_INSN_READ_PC))
2298 /* If the previous instruction was extended, we can not
2299 swap. */
2300 || (mips_opts.mips16 && prev_insn_extended)
2301 /* If the previous instruction had a fixup in mips16
2302 mode, we can not swap. This normally means that the
2303 previous instruction was a 4 byte branch anyhow. */
f6688943 2304 || (mips_opts.mips16 && prev_insn_fixp[0])
bdaaa2e1
KH
2305 /* If the previous instruction is a sync, sync.l, or
2306 sync.p, we can not swap. */
f173e82e 2307 || (prev_pinfo & INSN_SYNC))
252b5132
RH
2308 {
2309 /* We could do even better for unconditional branches to
2310 portions of this object file; we could pick up the
2311 instruction at the destination, put it in the delay
2312 slot, and bump the destination address. */
2313 emit_nop ();
2314 /* Update the previous insn information. */
2315 prev_prev_insn = *ip;
2316 prev_insn.insn_mo = &dummy_opcode;
2317 }
2318 else
2319 {
2320 /* It looks like we can actually do the swap. */
2321 if (! mips_opts.mips16)
2322 {
2323 char *prev_f;
2324 char temp[4];
2325
2326 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2327 memcpy (temp, prev_f, 4);
2328 memcpy (prev_f, f, 4);
2329 memcpy (f, temp, 4);
f6688943
TS
2330 if (prev_insn_fixp[0])
2331 {
2332 prev_insn_fixp[0]->fx_frag = frag_now;
2333 prev_insn_fixp[0]->fx_where = f - frag_now->fr_literal;
2334 }
2335 if (prev_insn_fixp[1])
2336 {
2337 prev_insn_fixp[1]->fx_frag = frag_now;
2338 prev_insn_fixp[1]->fx_where = f - frag_now->fr_literal;
2339 }
2340 if (prev_insn_fixp[2])
252b5132 2341 {
f6688943
TS
2342 prev_insn_fixp[2]->fx_frag = frag_now;
2343 prev_insn_fixp[2]->fx_where = f - frag_now->fr_literal;
252b5132 2344 }
f6688943 2345 if (fixp[0])
252b5132 2346 {
f6688943
TS
2347 fixp[0]->fx_frag = prev_insn_frag;
2348 fixp[0]->fx_where = prev_insn_where;
2349 }
2350 if (fixp[1])
2351 {
2352 fixp[1]->fx_frag = prev_insn_frag;
2353 fixp[1]->fx_where = prev_insn_where;
2354 }
2355 if (fixp[2])
2356 {
2357 fixp[2]->fx_frag = prev_insn_frag;
2358 fixp[2]->fx_where = prev_insn_where;
252b5132
RH
2359 }
2360 }
2361 else
2362 {
2363 char *prev_f;
2364 char temp[2];
2365
f6688943
TS
2366 assert (prev_insn_fixp[0] == NULL);
2367 assert (prev_insn_fixp[1] == NULL);
2368 assert (prev_insn_fixp[2] == NULL);
252b5132
RH
2369 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2370 memcpy (temp, prev_f, 2);
2371 memcpy (prev_f, f, 2);
f6688943 2372 if (*reloc_type != BFD_RELOC_MIPS16_JMP)
252b5132 2373 {
f6688943 2374 assert (*reloc_type == BFD_RELOC_UNUSED);
252b5132
RH
2375 memcpy (f, temp, 2);
2376 }
2377 else
2378 {
2379 memcpy (f, f + 2, 2);
2380 memcpy (f + 2, temp, 2);
2381 }
f6688943
TS
2382 if (fixp[0])
2383 {
2384 fixp[0]->fx_frag = prev_insn_frag;
2385 fixp[0]->fx_where = prev_insn_where;
2386 }
2387 if (fixp[1])
2388 {
2389 fixp[1]->fx_frag = prev_insn_frag;
2390 fixp[1]->fx_where = prev_insn_where;
2391 }
2392 if (fixp[2])
252b5132 2393 {
f6688943
TS
2394 fixp[2]->fx_frag = prev_insn_frag;
2395 fixp[2]->fx_where = prev_insn_where;
252b5132
RH
2396 }
2397 }
2398
2399 /* Update the previous insn information; leave prev_insn
2400 unchanged. */
2401 prev_prev_insn = *ip;
2402 }
2403 prev_insn_is_delay_slot = 1;
2404
2405 /* If that was an unconditional branch, forget the previous
2406 insn information. */
2407 if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2408 {
2409 prev_prev_insn.insn_mo = &dummy_opcode;
2410 prev_insn.insn_mo = &dummy_opcode;
2411 }
2412
f6688943
TS
2413 prev_insn_fixp[0] = NULL;
2414 prev_insn_fixp[1] = NULL;
2415 prev_insn_fixp[2] = NULL;
2416 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2417 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2418 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
252b5132
RH
2419 prev_insn_extended = 0;
2420 }
2421 else if (pinfo & INSN_COND_BRANCH_LIKELY)
2422 {
2423 /* We don't yet optimize a branch likely. What we should do
2424 is look at the target, copy the instruction found there
2425 into the delay slot, and increment the branch to jump to
2426 the next instruction. */
2427 emit_nop ();
2428 /* Update the previous insn information. */
2429 prev_prev_insn = *ip;
2430 prev_insn.insn_mo = &dummy_opcode;
f6688943
TS
2431 prev_insn_fixp[0] = NULL;
2432 prev_insn_fixp[1] = NULL;
2433 prev_insn_fixp[2] = NULL;
2434 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2435 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2436 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
252b5132
RH
2437 prev_insn_extended = 0;
2438 }
2439 else
2440 {
2441 /* Update the previous insn information. */
2442 if (nops > 0)
2443 prev_prev_insn.insn_mo = &dummy_opcode;
2444 else
2445 prev_prev_insn = prev_insn;
2446 prev_insn = *ip;
2447
2448 /* Any time we see a branch, we always fill the delay slot
2449 immediately; since this insn is not a branch, we know it
2450 is not in a delay slot. */
2451 prev_insn_is_delay_slot = 0;
2452
f6688943
TS
2453 prev_insn_fixp[0] = fixp[0];
2454 prev_insn_fixp[1] = fixp[1];
2455 prev_insn_fixp[2] = fixp[2];
2456 prev_insn_reloc_type[0] = reloc_type[0];
2457 prev_insn_reloc_type[1] = reloc_type[1];
2458 prev_insn_reloc_type[2] = reloc_type[2];
252b5132
RH
2459 if (mips_opts.mips16)
2460 prev_insn_extended = (ip->use_extend
f6688943 2461 || *reloc_type > BFD_RELOC_UNUSED);
252b5132
RH
2462 }
2463
2464 prev_prev_insn_unreordered = prev_insn_unreordered;
2465 prev_insn_unreordered = 0;
2466 prev_insn_frag = frag_now;
2467 prev_insn_where = f - frag_now->fr_literal;
2468 prev_insn_valid = 1;
2469 }
2470 else if (place == NULL)
2471 {
2472 /* We need to record a bit of information even when we are not
2473 reordering, in order to determine the base address for mips16
2474 PC relative relocs. */
2475 prev_prev_insn = prev_insn;
2476 prev_insn = *ip;
f6688943
TS
2477 prev_insn_reloc_type[0] = reloc_type[0];
2478 prev_insn_reloc_type[1] = reloc_type[1];
2479 prev_insn_reloc_type[2] = reloc_type[2];
252b5132
RH
2480 prev_prev_insn_unreordered = prev_insn_unreordered;
2481 prev_insn_unreordered = 1;
2482 }
2483
2484 /* We just output an insn, so the next one doesn't have a label. */
2485 mips_clear_insn_labels ();
2486
2487 /* We must ensure that a fixup associated with an unmatched %hi
2488 reloc does not become a variant frag. Otherwise, the
2489 rearrangement of %hi relocs in frob_file may confuse
2490 tc_gen_reloc. */
2491 if (unmatched_hi)
2492 {
2493 frag_wane (frag_now);
2494 frag_new (0);
2495 }
2496}
2497
2498/* This function forgets that there was any previous instruction or
2499 label. If PRESERVE is non-zero, it remembers enough information to
bdaaa2e1 2500 know whether nops are needed before a noreorder section. */
252b5132
RH
2501
2502static void
2503mips_no_prev_insn (preserve)
2504 int preserve;
2505{
2506 if (! preserve)
2507 {
2508 prev_insn.insn_mo = &dummy_opcode;
2509 prev_prev_insn.insn_mo = &dummy_opcode;
2510 prev_nop_frag = NULL;
2511 prev_nop_frag_holds = 0;
2512 prev_nop_frag_required = 0;
2513 prev_nop_frag_since = 0;
2514 }
2515 prev_insn_valid = 0;
2516 prev_insn_is_delay_slot = 0;
2517 prev_insn_unreordered = 0;
2518 prev_insn_extended = 0;
f6688943
TS
2519 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2520 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2521 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
252b5132
RH
2522 prev_prev_insn_unreordered = 0;
2523 mips_clear_insn_labels ();
2524}
2525
2526/* This function must be called whenever we turn on noreorder or emit
2527 something other than instructions. It inserts any NOPS which might
2528 be needed by the previous instruction, and clears the information
2529 kept for the previous instructions. The INSNS parameter is true if
bdaaa2e1 2530 instructions are to follow. */
252b5132
RH
2531
2532static void
2533mips_emit_delays (insns)
2534 boolean insns;
2535{
2536 if (! mips_opts.noreorder)
2537 {
2538 int nops;
2539
2540 nops = 0;
2541 if ((! mips_opts.mips16
9ce8a5dd 2542 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
2543 && (! cop_interlocks
2544 && (prev_insn.insn_mo->pinfo
2545 & (INSN_LOAD_COPROC_DELAY
2546 | INSN_COPROC_MOVE_DELAY
2547 | INSN_WRITE_COND_CODE))))
2548 || (! hilo_interlocks
2549 && (prev_insn.insn_mo->pinfo
2550 & (INSN_READ_LO
2551 | INSN_READ_HI)))
2552 || (! mips_opts.mips16
2553 && ! gpr_interlocks
bdaaa2e1 2554 && (prev_insn.insn_mo->pinfo
252b5132
RH
2555 & INSN_LOAD_MEMORY_DELAY))
2556 || (! mips_opts.mips16
e7af610e 2557 && mips_opts.isa == ISA_MIPS1
252b5132
RH
2558 && (prev_insn.insn_mo->pinfo
2559 & INSN_COPROC_MEMORY_DELAY)))
2560 {
beae10d5 2561 /* Itbl support may require additional care here. */
252b5132
RH
2562 ++nops;
2563 if ((! mips_opts.mips16
9ce8a5dd 2564 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
2565 && (! cop_interlocks
2566 && prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2567 || (! hilo_interlocks
2568 && ((prev_insn.insn_mo->pinfo & INSN_READ_HI)
2569 || (prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2570 ++nops;
2571
2572 if (prev_insn_unreordered)
2573 nops = 0;
2574 }
2575 else if ((! mips_opts.mips16
9ce8a5dd 2576 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
2577 && (! cop_interlocks
2578 && prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2579 || (! hilo_interlocks
2580 && ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
2581 || (prev_prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2582 {
beae10d5 2583 /* Itbl support may require additional care here. */
252b5132
RH
2584 if (! prev_prev_insn_unreordered)
2585 ++nops;
2586 }
2587
2588 if (nops > 0)
2589 {
2590 struct insn_label_list *l;
2591
2592 if (insns)
2593 {
2594 /* Record the frag which holds the nop instructions, so
2595 that we can remove them if we don't need them. */
2596 frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2597 prev_nop_frag = frag_now;
2598 prev_nop_frag_holds = nops;
2599 prev_nop_frag_required = 0;
2600 prev_nop_frag_since = 0;
2601 }
2602
2603 for (; nops > 0; --nops)
2604 emit_nop ();
2605
2606 if (insns)
2607 {
2608 /* Move on to a new frag, so that it is safe to simply
bdaaa2e1 2609 decrease the size of prev_nop_frag. */
252b5132
RH
2610 frag_wane (frag_now);
2611 frag_new (0);
2612 }
2613
2614 for (l = insn_labels; l != NULL; l = l->next)
2615 {
98aa84af
AM
2616 valueT val;
2617
252b5132 2618 assert (S_GET_SEGMENT (l->label) == now_seg);
49309057 2619 symbol_set_frag (l->label, frag_now);
98aa84af 2620 val = (valueT) frag_now_fix ();
252b5132
RH
2621 /* mips16 text labels are stored as odd. */
2622 if (mips_opts.mips16)
f9419b05 2623 ++val;
98aa84af 2624 S_SET_VALUE (l->label, val);
252b5132
RH
2625 }
2626 }
2627 }
2628
2629 /* Mark instruction labels in mips16 mode. */
f9419b05 2630 if (insns)
252b5132
RH
2631 mips16_mark_labels ();
2632
2633 mips_no_prev_insn (insns);
2634}
2635
2636/* Build an instruction created by a macro expansion. This is passed
2637 a pointer to the count of instructions created so far, an
2638 expression, the name of the instruction to build, an operand format
2639 string, and corresponding arguments. */
2640
2641#ifdef USE_STDARG
2642static void
2643macro_build (char *place,
2644 int *counter,
2645 expressionS * ep,
2646 const char *name,
2647 const char *fmt,
2648 ...)
2649#else
2650static void
2651macro_build (place, counter, ep, name, fmt, va_alist)
2652 char *place;
2653 int *counter;
2654 expressionS *ep;
2655 const char *name;
2656 const char *fmt;
2657 va_dcl
2658#endif
2659{
2660 struct mips_cl_insn insn;
f6688943 2661 bfd_reloc_code_real_type r[3];
252b5132 2662 va_list args;
252b5132
RH
2663
2664#ifdef USE_STDARG
2665 va_start (args, fmt);
2666#else
2667 va_start (args);
2668#endif
2669
2670 /*
2671 * If the macro is about to expand into a second instruction,
2672 * print a warning if needed. We need to pass ip as a parameter
2673 * to generate a better warning message here...
2674 */
2675 if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
2676 as_warn (_("Macro instruction expanded into multiple instructions"));
2677
80cc45a5
EC
2678 /*
2679 * If the macro is about to expand into a second instruction,
2680 * and it is in a delay slot, print a warning.
2681 */
2682 if (place == NULL
2683 && *counter == 1
2684 && mips_opts.noreorder
2685 && (prev_prev_insn.insn_mo->pinfo
2686 & (INSN_UNCOND_BRANCH_DELAY | INSN_COND_BRANCH_DELAY
2b2e39bf 2687 | INSN_COND_BRANCH_LIKELY)) != 0)
80cc45a5
EC
2688 as_warn (_("Macro instruction expanded into multiple instructions in a branch delay slot"));
2689
252b5132 2690 if (place == NULL)
f9419b05 2691 ++*counter; /* bump instruction counter */
252b5132
RH
2692
2693 if (mips_opts.mips16)
2694 {
2695 mips16_macro_build (place, counter, ep, name, fmt, args);
2696 va_end (args);
2697 return;
2698 }
2699
f6688943
TS
2700 r[0] = BFD_RELOC_UNUSED;
2701 r[1] = BFD_RELOC_UNUSED;
2702 r[2] = BFD_RELOC_UNUSED;
252b5132
RH
2703 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
2704 assert (insn.insn_mo);
2705 assert (strcmp (name, insn.insn_mo->name) == 0);
2706
2707 /* Search until we get a match for NAME. */
2708 while (1)
2709 {
deec1734
CD
2710 /* It is assumed here that macros will never generate
2711 MDMX or MIPS-3D instructions. */
252b5132
RH
2712 if (strcmp (fmt, insn.insn_mo->args) == 0
2713 && insn.insn_mo->pinfo != INSN_MACRO
aec421e0
TS
2714 && OPCODE_IS_MEMBER (insn.insn_mo,
2715 (mips_opts.isa
2716 | (mips_opts.mips16 ? INSN_MIPS16 : 0)),
2717 mips_arch)
ec68c924 2718 && (mips_arch != CPU_R4650 || (insn.insn_mo->pinfo & FP_D) == 0))
252b5132
RH
2719 break;
2720
2721 ++insn.insn_mo;
2722 assert (insn.insn_mo->name);
2723 assert (strcmp (name, insn.insn_mo->name) == 0);
2724 }
2725
2726 insn.insn_opcode = insn.insn_mo->match;
2727 for (;;)
2728 {
2729 switch (*fmt++)
2730 {
2731 case '\0':
2732 break;
2733
2734 case ',':
2735 case '(':
2736 case ')':
2737 continue;
2738
2739 case 't':
2740 case 'w':
2741 case 'E':
38487616 2742 insn.insn_opcode |= va_arg (args, int) << OP_SH_RT;
252b5132
RH
2743 continue;
2744
2745 case 'c':
38487616
TS
2746 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE;
2747 continue;
2748
252b5132
RH
2749 case 'T':
2750 case 'W':
38487616 2751 insn.insn_opcode |= va_arg (args, int) << OP_SH_FT;
252b5132
RH
2752 continue;
2753
2754 case 'd':
2755 case 'G':
38487616 2756 insn.insn_opcode |= va_arg (args, int) << OP_SH_RD;
252b5132
RH
2757 continue;
2758
4372b673
NC
2759 case 'U':
2760 {
2761 int tmp = va_arg (args, int);
2762
38487616
TS
2763 insn.insn_opcode |= tmp << OP_SH_RT;
2764 insn.insn_opcode |= tmp << OP_SH_RD;
beae10d5 2765 continue;
4372b673
NC
2766 }
2767
252b5132
RH
2768 case 'V':
2769 case 'S':
38487616 2770 insn.insn_opcode |= va_arg (args, int) << OP_SH_FS;
252b5132
RH
2771 continue;
2772
2773 case 'z':
2774 continue;
2775
2776 case '<':
38487616 2777 insn.insn_opcode |= va_arg (args, int) << OP_SH_SHAMT;
252b5132
RH
2778 continue;
2779
2780 case 'D':
38487616 2781 insn.insn_opcode |= va_arg (args, int) << OP_SH_FD;
252b5132
RH
2782 continue;
2783
2784 case 'B':
38487616 2785 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE20;
252b5132
RH
2786 continue;
2787
4372b673 2788 case 'J':
38487616 2789 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE19;
4372b673
NC
2790 continue;
2791
252b5132 2792 case 'q':
38487616 2793 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE2;
252b5132
RH
2794 continue;
2795
2796 case 'b':
2797 case 's':
2798 case 'r':
2799 case 'v':
38487616 2800 insn.insn_opcode |= va_arg (args, int) << OP_SH_RS;
252b5132
RH
2801 continue;
2802
2803 case 'i':
2804 case 'j':
2805 case 'o':
f6688943 2806 *r = (bfd_reloc_code_real_type) va_arg (args, int);
cdf6fd85 2807 assert (*r == BFD_RELOC_GPREL16
f6688943
TS
2808 || *r == BFD_RELOC_MIPS_LITERAL
2809 || *r == BFD_RELOC_MIPS_HIGHER
2810 || *r == BFD_RELOC_HI16_S
2811 || *r == BFD_RELOC_LO16
2812 || *r == BFD_RELOC_MIPS_GOT16
2813 || *r == BFD_RELOC_MIPS_CALL16
438c16b8
TS
2814 || *r == BFD_RELOC_MIPS_GOT_DISP
2815 || *r == BFD_RELOC_MIPS_GOT_PAGE
2816 || *r == BFD_RELOC_MIPS_GOT_OFST
f6688943
TS
2817 || *r == BFD_RELOC_MIPS_GOT_LO16
2818 || *r == BFD_RELOC_MIPS_CALL_LO16
252b5132 2819 || (ep->X_op == O_subtract
f6688943 2820 && *r == BFD_RELOC_PCREL_LO16));
252b5132
RH
2821 continue;
2822
2823 case 'u':
f6688943 2824 *r = (bfd_reloc_code_real_type) va_arg (args, int);
252b5132
RH
2825 assert (ep != NULL
2826 && (ep->X_op == O_constant
2827 || (ep->X_op == O_symbol
f6688943
TS
2828 && (*r == BFD_RELOC_MIPS_HIGHEST
2829 || *r == BFD_RELOC_HI16_S
2830 || *r == BFD_RELOC_HI16
2831 || *r == BFD_RELOC_GPREL16
2832 || *r == BFD_RELOC_MIPS_GOT_HI16
2833 || *r == BFD_RELOC_MIPS_CALL_HI16))
252b5132 2834 || (ep->X_op == O_subtract
f6688943 2835 && *r == BFD_RELOC_PCREL_HI16_S)));
252b5132
RH
2836 continue;
2837
2838 case 'p':
2839 assert (ep != NULL);
2840 /*
2841 * This allows macro() to pass an immediate expression for
2842 * creating short branches without creating a symbol.
2843 * Note that the expression still might come from the assembly
2844 * input, in which case the value is not checked for range nor
2845 * is a relocation entry generated (yuck).
2846 */
2847 if (ep->X_op == O_constant)
2848 {
2849 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
2850 ep = NULL;
2851 }
2852 else
cb56d3d3 2853 if (mips_pic == EMBEDDED_PIC)
f6688943 2854 *r = BFD_RELOC_16_PCREL_S2;
cb56d3d3 2855 else
f6688943 2856 *r = BFD_RELOC_16_PCREL;
252b5132
RH
2857 continue;
2858
2859 case 'a':
2860 assert (ep != NULL);
f6688943 2861 *r = BFD_RELOC_MIPS_JMP;
252b5132
RH
2862 continue;
2863
2864 case 'C':
2865 insn.insn_opcode |= va_arg (args, unsigned long);
2866 continue;
2867
2868 default:
2869 internalError ();
2870 }
2871 break;
2872 }
2873 va_end (args);
f6688943 2874 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132
RH
2875
2876 append_insn (place, &insn, ep, r, false);
2877}
2878
2879static void
2880mips16_macro_build (place, counter, ep, name, fmt, args)
2881 char *place;
43841e91 2882 int *counter ATTRIBUTE_UNUSED;
252b5132
RH
2883 expressionS *ep;
2884 const char *name;
2885 const char *fmt;
2886 va_list args;
2887{
2888 struct mips_cl_insn insn;
f6688943
TS
2889 bfd_reloc_code_real_type r[3]
2890 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 2891
252b5132
RH
2892 insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
2893 assert (insn.insn_mo);
2894 assert (strcmp (name, insn.insn_mo->name) == 0);
2895
2896 while (strcmp (fmt, insn.insn_mo->args) != 0
2897 || insn.insn_mo->pinfo == INSN_MACRO)
2898 {
2899 ++insn.insn_mo;
2900 assert (insn.insn_mo->name);
2901 assert (strcmp (name, insn.insn_mo->name) == 0);
2902 }
2903
2904 insn.insn_opcode = insn.insn_mo->match;
2905 insn.use_extend = false;
2906
2907 for (;;)
2908 {
2909 int c;
2910
2911 c = *fmt++;
2912 switch (c)
2913 {
2914 case '\0':
2915 break;
2916
2917 case ',':
2918 case '(':
2919 case ')':
2920 continue;
2921
2922 case 'y':
2923 case 'w':
2924 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
2925 continue;
2926
2927 case 'x':
2928 case 'v':
2929 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
2930 continue;
2931
2932 case 'z':
2933 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
2934 continue;
2935
2936 case 'Z':
2937 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
2938 continue;
2939
2940 case '0':
2941 case 'S':
2942 case 'P':
2943 case 'R':
2944 continue;
2945
2946 case 'X':
2947 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
2948 continue;
2949
2950 case 'Y':
2951 {
2952 int regno;
2953
2954 regno = va_arg (args, int);
2955 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
2956 insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
2957 }
2958 continue;
2959
2960 case '<':
2961 case '>':
2962 case '4':
2963 case '5':
2964 case 'H':
2965 case 'W':
2966 case 'D':
2967 case 'j':
2968 case '8':
2969 case 'V':
2970 case 'C':
2971 case 'U':
2972 case 'k':
2973 case 'K':
2974 case 'p':
2975 case 'q':
2976 {
2977 assert (ep != NULL);
2978
2979 if (ep->X_op != O_constant)
874e8986 2980 *r = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
2981 else
2982 {
c4e7957c
TS
2983 mips16_immed (NULL, 0, c, ep->X_add_number, false, false,
2984 false, &insn.insn_opcode, &insn.use_extend,
2985 &insn.extend);
252b5132 2986 ep = NULL;
f6688943 2987 *r = BFD_RELOC_UNUSED;
252b5132
RH
2988 }
2989 }
2990 continue;
2991
2992 case '6':
2993 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
2994 continue;
2995 }
2996
2997 break;
2998 }
2999
f6688943 3000 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132
RH
3001
3002 append_insn (place, &insn, ep, r, false);
3003}
3004
438c16b8
TS
3005/*
3006 * Generate a "jalr" instruction with a relocation hint to the called
3007 * function. This occurs in NewABI PIC code.
3008 */
3009static void
3010macro_build_jalr (icnt, ep)
3011 int icnt;
3012 expressionS *ep;
3013{
f21f8242
AO
3014 char *f;
3015
438c16b8 3016 if (HAVE_NEWABI)
f21f8242
AO
3017 {
3018 frag_grow (4);
3019 f = frag_more (0);
3020 }
438c16b8
TS
3021 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr", "d,s",
3022 RA, PIC_CALL_REG);
3023 if (HAVE_NEWABI)
f21f8242
AO
3024 fix_new_exp (frag_now, f - frag_now->fr_literal,
3025 0, ep, false, BFD_RELOC_MIPS_JALR);
438c16b8
TS
3026}
3027
252b5132
RH
3028/*
3029 * Generate a "lui" instruction.
3030 */
3031static void
3032macro_build_lui (place, counter, ep, regnum)
3033 char *place;
3034 int *counter;
3035 expressionS *ep;
3036 int regnum;
3037{
3038 expressionS high_expr;
3039 struct mips_cl_insn insn;
f6688943
TS
3040 bfd_reloc_code_real_type r[3]
3041 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
5a38dc70
AM
3042 const char *name = "lui";
3043 const char *fmt = "t,u";
252b5132
RH
3044
3045 assert (! mips_opts.mips16);
3046
3047 if (place == NULL)
3048 high_expr = *ep;
3049 else
3050 {
3051 high_expr.X_op = O_constant;
3052 high_expr.X_add_number = ep->X_add_number;
3053 }
3054
3055 if (high_expr.X_op == O_constant)
3056 {
3057 /* we can compute the instruction now without a relocation entry */
e7d556df
TS
3058 high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3059 >> 16) & 0xffff;
f6688943 3060 *r = BFD_RELOC_UNUSED;
252b5132 3061 }
f6688943 3062 else if (! HAVE_NEWABI)
252b5132
RH
3063 {
3064 assert (ep->X_op == O_symbol);
3065 /* _gp_disp is a special case, used from s_cpload. */
3066 assert (mips_pic == NO_PIC
3067 || strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0);
f6688943 3068 *r = BFD_RELOC_HI16_S;
252b5132
RH
3069 }
3070
3071 /*
3072 * If the macro is about to expand into a second instruction,
3073 * print a warning if needed. We need to pass ip as a parameter
3074 * to generate a better warning message here...
3075 */
3076 if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
3077 as_warn (_("Macro instruction expanded into multiple instructions"));
3078
3079 if (place == NULL)
f9419b05 3080 ++*counter; /* bump instruction counter */
252b5132
RH
3081
3082 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3083 assert (insn.insn_mo);
3084 assert (strcmp (name, insn.insn_mo->name) == 0);
3085 assert (strcmp (fmt, insn.insn_mo->args) == 0);
3086
3087 insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
f6688943 3088 if (*r == BFD_RELOC_UNUSED)
252b5132
RH
3089 {
3090 insn.insn_opcode |= high_expr.X_add_number;
3091 append_insn (place, &insn, NULL, r, false);
3092 }
3093 else
3094 append_insn (place, &insn, &high_expr, r, false);
3095}
3096
3097/* set_at()
3098 * Generates code to set the $at register to true (one)
3099 * if reg is less than the immediate expression.
3100 */
3101static void
3102set_at (counter, reg, unsignedp)
3103 int *counter;
3104 int reg;
3105 int unsignedp;
3106{
3107 if (imm_expr.X_op == O_constant
3108 && imm_expr.X_add_number >= -0x8000
3109 && imm_expr.X_add_number < 0x8000)
3110 macro_build ((char *) NULL, counter, &imm_expr,
3111 unsignedp ? "sltiu" : "slti",
3112 "t,r,j", AT, reg, (int) BFD_RELOC_LO16);
3113 else
3114 {
4d34fb5f 3115 load_register (counter, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9 3116 macro_build ((char *) NULL, counter, (expressionS *) NULL,
252b5132
RH
3117 unsignedp ? "sltu" : "slt",
3118 "d,v,t", AT, reg, AT);
3119 }
3120}
3121
3122/* Warn if an expression is not a constant. */
3123
3124static void
3125check_absolute_expr (ip, ex)
3126 struct mips_cl_insn *ip;
3127 expressionS *ex;
3128{
3129 if (ex->X_op == O_big)
3130 as_bad (_("unsupported large constant"));
3131 else if (ex->X_op != O_constant)
3132 as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
3133}
3134
3135/* Count the leading zeroes by performing a binary chop. This is a
3136 bulky bit of source, but performance is a LOT better for the
3137 majority of values than a simple loop to count the bits:
3138 for (lcnt = 0; (lcnt < 32); lcnt++)
3139 if ((v) & (1 << (31 - lcnt)))
3140 break;
3141 However it is not code size friendly, and the gain will drop a bit
3142 on certain cached systems.
3143*/
3144#define COUNT_TOP_ZEROES(v) \
3145 (((v) & ~0xffff) == 0 \
3146 ? ((v) & ~0xff) == 0 \
3147 ? ((v) & ~0xf) == 0 \
3148 ? ((v) & ~0x3) == 0 \
3149 ? ((v) & ~0x1) == 0 \
3150 ? !(v) \
3151 ? 32 \
3152 : 31 \
3153 : 30 \
3154 : ((v) & ~0x7) == 0 \
3155 ? 29 \
3156 : 28 \
3157 : ((v) & ~0x3f) == 0 \
3158 ? ((v) & ~0x1f) == 0 \
3159 ? 27 \
3160 : 26 \
3161 : ((v) & ~0x7f) == 0 \
3162 ? 25 \
3163 : 24 \
3164 : ((v) & ~0xfff) == 0 \
3165 ? ((v) & ~0x3ff) == 0 \
3166 ? ((v) & ~0x1ff) == 0 \
3167 ? 23 \
3168 : 22 \
3169 : ((v) & ~0x7ff) == 0 \
3170 ? 21 \
3171 : 20 \
3172 : ((v) & ~0x3fff) == 0 \
3173 ? ((v) & ~0x1fff) == 0 \
3174 ? 19 \
3175 : 18 \
3176 : ((v) & ~0x7fff) == 0 \
3177 ? 17 \
3178 : 16 \
3179 : ((v) & ~0xffffff) == 0 \
3180 ? ((v) & ~0xfffff) == 0 \
3181 ? ((v) & ~0x3ffff) == 0 \
3182 ? ((v) & ~0x1ffff) == 0 \
3183 ? 15 \
3184 : 14 \
3185 : ((v) & ~0x7ffff) == 0 \
3186 ? 13 \
3187 : 12 \
3188 : ((v) & ~0x3fffff) == 0 \
3189 ? ((v) & ~0x1fffff) == 0 \
3190 ? 11 \
3191 : 10 \
3192 : ((v) & ~0x7fffff) == 0 \
3193 ? 9 \
3194 : 8 \
3195 : ((v) & ~0xfffffff) == 0 \
3196 ? ((v) & ~0x3ffffff) == 0 \
3197 ? ((v) & ~0x1ffffff) == 0 \
3198 ? 7 \
3199 : 6 \
3200 : ((v) & ~0x7ffffff) == 0 \
3201 ? 5 \
3202 : 4 \
3203 : ((v) & ~0x3fffffff) == 0 \
3204 ? ((v) & ~0x1fffffff) == 0 \
3205 ? 3 \
3206 : 2 \
3207 : ((v) & ~0x7fffffff) == 0 \
3208 ? 1 \
3209 : 0)
3210
6373ee54
CD
3211/* Is the given value a sign-extended 32-bit value? */
3212#define IS_SEXT_32BIT_NUM(x) \
3213 (((x) &~ (offsetT) 0x7fffffff) == 0 \
3214 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
3215
252b5132
RH
3216/* load_register()
3217 * This routine generates the least number of instructions neccessary to load
3218 * an absolute expression value into a register.
3219 */
3220static void
3221load_register (counter, reg, ep, dbl)
3222 int *counter;
3223 int reg;
3224 expressionS *ep;
3225 int dbl;
3226{
3227 int freg;
3228 expressionS hi32, lo32;
3229
3230 if (ep->X_op != O_big)
3231 {
3232 assert (ep->X_op == O_constant);
3233 if (ep->X_add_number < 0x8000
3234 && (ep->X_add_number >= 0
3235 || (ep->X_add_number >= -0x8000
3236 && (! dbl
3237 || ! ep->X_unsigned
3238 || sizeof (ep->X_add_number) > 4))))
3239 {
3240 /* We can handle 16 bit signed values with an addiu to
3241 $zero. No need to ever use daddiu here, since $zero and
3242 the result are always correct in 32 bit mode. */
3243 macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3244 (int) BFD_RELOC_LO16);
3245 return;
3246 }
3247 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3248 {
3249 /* We can handle 16 bit unsigned values with an ori to
3250 $zero. */
3251 macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, 0,
3252 (int) BFD_RELOC_LO16);
3253 return;
3254 }
6373ee54 3255 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)
252b5132
RH
3256 && (! dbl
3257 || ! ep->X_unsigned
3258 || sizeof (ep->X_add_number) > 4
3259 || (ep->X_add_number & 0x80000000) == 0))
ca4e0257 3260 || ((HAVE_32BIT_GPRS || ! dbl)
252b5132 3261 && (ep->X_add_number &~ (offsetT) 0xffffffff) == 0)
ca4e0257 3262 || (HAVE_32BIT_GPRS
252b5132
RH
3263 && ! dbl
3264 && ((ep->X_add_number &~ (offsetT) 0xffffffff)
3265 == ~ (offsetT) 0xffffffff)))
3266 {
3267 /* 32 bit values require an lui. */
3268 macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3269 (int) BFD_RELOC_HI16);
3270 if ((ep->X_add_number & 0xffff) != 0)
3271 macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, reg,
3272 (int) BFD_RELOC_LO16);
3273 return;
3274 }
3275 }
3276
3277 /* The value is larger than 32 bits. */
3278
ca4e0257 3279 if (HAVE_32BIT_GPRS)
252b5132 3280 {
956cd1d6
TS
3281 as_bad (_("Number (0x%lx) larger than 32 bits"),
3282 (unsigned long) ep->X_add_number);
252b5132
RH
3283 macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3284 (int) BFD_RELOC_LO16);
3285 return;
3286 }
3287
3288 if (ep->X_op != O_big)
3289 {
3290 hi32 = *ep;
3291 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3292 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3293 hi32.X_add_number &= 0xffffffff;
3294 lo32 = *ep;
3295 lo32.X_add_number &= 0xffffffff;
3296 }
3297 else
3298 {
3299 assert (ep->X_add_number > 2);
3300 if (ep->X_add_number == 3)
3301 generic_bignum[3] = 0;
3302 else if (ep->X_add_number > 4)
3303 as_bad (_("Number larger than 64 bits"));
3304 lo32.X_op = O_constant;
3305 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3306 hi32.X_op = O_constant;
3307 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3308 }
3309
3310 if (hi32.X_add_number == 0)
3311 freg = 0;
3312 else
3313 {
3314 int shift, bit;
3315 unsigned long hi, lo;
3316
956cd1d6 3317 if (hi32.X_add_number == (offsetT) 0xffffffff)
beae10d5
KH
3318 {
3319 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3320 {
3321 macro_build ((char *) NULL, counter, &lo32, "addiu", "t,r,j",
252b5132 3322 reg, 0, (int) BFD_RELOC_LO16);
beae10d5
KH
3323 return;
3324 }
3325 if (lo32.X_add_number & 0x80000000)
3326 {
3327 macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3328 (int) BFD_RELOC_HI16);
252b5132
RH
3329 if (lo32.X_add_number & 0xffff)
3330 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i",
3331 reg, reg, (int) BFD_RELOC_LO16);
beae10d5
KH
3332 return;
3333 }
3334 }
252b5132
RH
3335
3336 /* Check for 16bit shifted constant. We know that hi32 is
3337 non-zero, so start the mask on the first bit of the hi32
3338 value. */
3339 shift = 17;
3340 do
beae10d5
KH
3341 {
3342 unsigned long himask, lomask;
3343
3344 if (shift < 32)
3345 {
3346 himask = 0xffff >> (32 - shift);
3347 lomask = (0xffff << shift) & 0xffffffff;
3348 }
3349 else
3350 {
3351 himask = 0xffff << (shift - 32);
3352 lomask = 0;
3353 }
3354 if ((hi32.X_add_number & ~(offsetT) himask) == 0
3355 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3356 {
3357 expressionS tmp;
3358
3359 tmp.X_op = O_constant;
3360 if (shift < 32)
3361 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3362 | (lo32.X_add_number >> shift));
3363 else
3364 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
3365 macro_build ((char *) NULL, counter, &tmp,
3366 "ori", "t,r,i", reg, 0,
3367 (int) BFD_RELOC_LO16);
2396cfb9 3368 macro_build ((char *) NULL, counter, (expressionS *) NULL,
beae10d5
KH
3369 (shift >= 32) ? "dsll32" : "dsll",
3370 "d,w,<", reg, reg,
3371 (shift >= 32) ? shift - 32 : shift);
3372 return;
3373 }
f9419b05 3374 ++shift;
beae10d5
KH
3375 }
3376 while (shift <= (64 - 16));
252b5132
RH
3377
3378 /* Find the bit number of the lowest one bit, and store the
3379 shifted value in hi/lo. */
3380 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3381 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3382 if (lo != 0)
3383 {
3384 bit = 0;
3385 while ((lo & 1) == 0)
3386 {
3387 lo >>= 1;
3388 ++bit;
3389 }
3390 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3391 hi >>= bit;
3392 }
3393 else
3394 {
3395 bit = 32;
3396 while ((hi & 1) == 0)
3397 {
3398 hi >>= 1;
3399 ++bit;
3400 }
3401 lo = hi;
3402 hi = 0;
3403 }
3404
3405 /* Optimize if the shifted value is a (power of 2) - 1. */
3406 if ((hi == 0 && ((lo + 1) & lo) == 0)
3407 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
beae10d5
KH
3408 {
3409 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
252b5132 3410 if (shift != 0)
beae10d5 3411 {
252b5132
RH
3412 expressionS tmp;
3413
3414 /* This instruction will set the register to be all
3415 ones. */
beae10d5
KH
3416 tmp.X_op = O_constant;
3417 tmp.X_add_number = (offsetT) -1;
3418 macro_build ((char *) NULL, counter, &tmp, "addiu", "t,r,j",
252b5132 3419 reg, 0, (int) BFD_RELOC_LO16);
beae10d5
KH
3420 if (bit != 0)
3421 {
3422 bit += shift;
2396cfb9 3423 macro_build ((char *) NULL, counter, (expressionS *) NULL,
beae10d5
KH
3424 (bit >= 32) ? "dsll32" : "dsll",
3425 "d,w,<", reg, reg,
3426 (bit >= 32) ? bit - 32 : bit);
3427 }
2396cfb9 3428 macro_build ((char *) NULL, counter, (expressionS *) NULL,
252b5132 3429 (shift >= 32) ? "dsrl32" : "dsrl",
beae10d5 3430 "d,w,<", reg, reg,
252b5132 3431 (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
3432 return;
3433 }
3434 }
252b5132
RH
3435
3436 /* Sign extend hi32 before calling load_register, because we can
3437 generally get better code when we load a sign extended value. */
3438 if ((hi32.X_add_number & 0x80000000) != 0)
beae10d5 3439 hi32.X_add_number |= ~(offsetT) 0xffffffff;
252b5132
RH
3440 load_register (counter, reg, &hi32, 0);
3441 freg = reg;
3442 }
3443 if ((lo32.X_add_number & 0xffff0000) == 0)
3444 {
3445 if (freg != 0)
3446 {
2396cfb9
TS
3447 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3448 "dsll32", "d,w,<", reg, freg, 0);
252b5132
RH
3449 freg = reg;
3450 }
3451 }
3452 else
3453 {
3454 expressionS mid16;
3455
956cd1d6 3456 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
beae10d5 3457 {
252b5132
RH
3458 macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3459 (int) BFD_RELOC_HI16);
956cd1d6
TS
3460 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3461 "dsrl32", "d,w,<", reg, reg, 0);
beae10d5
KH
3462 return;
3463 }
252b5132
RH
3464
3465 if (freg != 0)
3466 {
956cd1d6
TS
3467 macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3468 "d,w,<", reg, freg, 16);
252b5132
RH
3469 freg = reg;
3470 }
3471 mid16 = lo32;
3472 mid16.X_add_number >>= 16;
3473 macro_build ((char *) NULL, counter, &mid16, "ori", "t,r,i", reg,
3474 freg, (int) BFD_RELOC_LO16);
956cd1d6
TS
3475 macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3476 "d,w,<", reg, reg, 16);
252b5132
RH
3477 freg = reg;
3478 }
3479 if ((lo32.X_add_number & 0xffff) != 0)
3480 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i", reg, freg,
3481 (int) BFD_RELOC_LO16);
3482}
3483
3484/* Load an address into a register. */
3485
3486static void
c9914766 3487load_address (counter, reg, ep, used_at)
252b5132
RH
3488 int *counter;
3489 int reg;
3490 expressionS *ep;
d6bc6245 3491 int *used_at;
252b5132 3492{
f9419b05 3493 char *p = NULL;
252b5132
RH
3494
3495 if (ep->X_op != O_constant
3496 && ep->X_op != O_symbol)
3497 {
3498 as_bad (_("expression too complex"));
3499 ep->X_op = O_constant;
3500 }
3501
3502 if (ep->X_op == O_constant)
3503 {
c9914766 3504 load_register (counter, reg, ep, HAVE_64BIT_ADDRESSES);
252b5132
RH
3505 return;
3506 }
3507
3508 if (mips_pic == NO_PIC)
3509 {
3510 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 3511 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
3512 Otherwise we want
3513 lui $reg,<sym> (BFD_RELOC_HI16_S)
3514 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
d6bc6245 3515 If we have an addend, we always use the latter form.
76b3015f 3516
d6bc6245
TS
3517 With 64bit address space and a usable $at we want
3518 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3519 lui $at,<sym> (BFD_RELOC_HI16_S)
3520 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3521 daddiu $at,<sym> (BFD_RELOC_LO16)
3522 dsll32 $reg,0
3a482fd5 3523 daddu $reg,$reg,$at
76b3015f 3524
d6bc6245
TS
3525 If $at is already in use, we use an path which is suboptimal
3526 on superscalar processors.
3527 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3528 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3529 dsll $reg,16
3530 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
3531 dsll $reg,16
3532 daddiu $reg,<sym> (BFD_RELOC_LO16)
3533 */
c9914766 3534 if (HAVE_64BIT_ADDRESSES)
d6bc6245 3535 {
d6bc6245
TS
3536 /* We don't do GP optimization for now because RELAX_ENCODE can't
3537 hold the data for such large chunks. */
3538
460597ba 3539 if (*used_at == 0 && ! mips_opts.noat)
d6bc6245
TS
3540 {
3541 macro_build (p, counter, ep, "lui", "t,u",
3542 reg, (int) BFD_RELOC_MIPS_HIGHEST);
3543 macro_build (p, counter, ep, "lui", "t,u",
3544 AT, (int) BFD_RELOC_HI16_S);
3545 macro_build (p, counter, ep, "daddiu", "t,r,j",
3546 reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3547 macro_build (p, counter, ep, "daddiu", "t,r,j",
3548 AT, AT, (int) BFD_RELOC_LO16);
2396cfb9
TS
3549 macro_build (p, counter, (expressionS *) NULL, "dsll32",
3550 "d,w,<", reg, reg, 0);
3a482fd5 3551 macro_build (p, counter, (expressionS *) NULL, "daddu",
2396cfb9 3552 "d,v,t", reg, reg, AT);
d6bc6245
TS
3553 *used_at = 1;
3554 }
3555 else
3556 {
3557 macro_build (p, counter, ep, "lui", "t,u",
3558 reg, (int) BFD_RELOC_MIPS_HIGHEST);
3559 macro_build (p, counter, ep, "daddiu", "t,r,j",
3560 reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
2396cfb9
TS
3561 macro_build (p, counter, (expressionS *) NULL, "dsll",
3562 "d,w,<", reg, reg, 16);
d6bc6245
TS
3563 macro_build (p, counter, ep, "daddiu", "t,r,j",
3564 reg, reg, (int) BFD_RELOC_HI16_S);
2396cfb9
TS
3565 macro_build (p, counter, (expressionS *) NULL, "dsll",
3566 "d,w,<", reg, reg, 16);
d6bc6245
TS
3567 macro_build (p, counter, ep, "daddiu", "t,r,j",
3568 reg, reg, (int) BFD_RELOC_LO16);
3569 }
3570 }
252b5132
RH
3571 else
3572 {
d6bc6245
TS
3573 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3574 && ! nopic_need_relax (ep->X_add_symbol, 1))
3575 {
3576 frag_grow (20);
3577 macro_build ((char *) NULL, counter, ep,
c9914766
TS
3578 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
3579 reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
d6bc6245
TS
3580 p = frag_var (rs_machine_dependent, 8, 0,
3581 RELAX_ENCODE (4, 8, 0, 4, 0,
3582 mips_opts.warn_about_macros),
956cd1d6 3583 ep->X_add_symbol, 0, NULL);
d6bc6245
TS
3584 }
3585 macro_build_lui (p, counter, ep, reg);
3586 if (p != NULL)
3587 p += 4;
c9914766
TS
3588 macro_build (p, counter, ep,
3589 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
d6bc6245
TS
3590 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3591 }
252b5132
RH
3592 }
3593 else if (mips_pic == SVR4_PIC && ! mips_big_got)
3594 {
3595 expressionS ex;
3596
3597 /* If this is a reference to an external symbol, we want
3598 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3599 Otherwise we want
3600 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3601 nop
3602 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3603 If there is a constant, it must be added in after. */
3604 ex.X_add_number = ep->X_add_number;
3605 ep->X_add_number = 0;
3606 frag_grow (20);
3607 macro_build ((char *) NULL, counter, ep,
c9914766
TS
3608 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
3609 reg, (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132
RH
3610 macro_build ((char *) NULL, counter, (expressionS *) NULL, "nop", "");
3611 p = frag_var (rs_machine_dependent, 4, 0,
3612 RELAX_ENCODE (0, 4, -8, 0, 0, mips_opts.warn_about_macros),
3613 ep->X_add_symbol, (offsetT) 0, (char *) NULL);
3614 macro_build (p, counter, ep,
ca4e0257 3615 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
3616 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3617 if (ex.X_add_number != 0)
3618 {
3619 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3620 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3621 ex.X_op = O_constant;
3622 macro_build ((char *) NULL, counter, &ex,
ca4e0257 3623 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
3624 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3625 }
3626 }
3627 else if (mips_pic == SVR4_PIC)
3628 {
3629 expressionS ex;
3630 int off;
3631
3632 /* This is the large GOT case. If this is a reference to an
3633 external symbol, we want
3634 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
3635 addu $reg,$reg,$gp
3636 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
3637 Otherwise, for a reference to a local symbol, we want
3638 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3639 nop
3640 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
438c16b8
TS
3641 If we have NewABI, we want
3642 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
3643 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
252b5132
RH
3644 If there is a constant, it must be added in after. */
3645 ex.X_add_number = ep->X_add_number;
3646 ep->X_add_number = 0;
438c16b8
TS
3647 if (HAVE_NEWABI)
3648 {
3649 macro_build ((char *) NULL, counter, ep,
3650 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3651 (int) BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
3652 macro_build (p, counter, ep,
3653 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
3654 reg, reg, (int) BFD_RELOC_MIPS_GOT_OFST);
3655 }
252b5132 3656 else
438c16b8
TS
3657 {
3658 if (reg_needs_delay (mips_gp_register))
3659 off = 4;
3660 else
3661 off = 0;
3662 frag_grow (32);
3663 macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3664 (int) BFD_RELOC_MIPS_GOT_HI16);
3665 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3666 HAVE_32BIT_ADDRESSES ? "addu" : "daddu", "d,v,t", reg,
3667 reg, mips_gp_register);
3668 macro_build ((char *) NULL, counter, ep,
3669 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
3670 "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT_LO16, reg);
3671 p = frag_var (rs_machine_dependent, 12 + off, 0,
3672 RELAX_ENCODE (12, 12 + off, off, 8 + off, 0,
3673 mips_opts.warn_about_macros),
3674 ep->X_add_symbol, 0, NULL);
3675 if (off > 0)
3676 {
3677 /* We need a nop before loading from $gp. This special
3678 check is required because the lui which starts the main
3679 instruction stream does not refer to $gp, and so will not
3680 insert the nop which may be required. */
3681 macro_build (p, counter, (expressionS *) NULL, "nop", "");
3682 p += 4;
3683 }
3684 macro_build (p, counter, ep,
3685 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3686 (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
3687 p += 4;
252b5132
RH
3688 macro_build (p, counter, (expressionS *) NULL, "nop", "");
3689 p += 4;
438c16b8
TS
3690 macro_build (p, counter, ep,
3691 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3692 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
252b5132 3693 }
438c16b8 3694
252b5132
RH
3695 if (ex.X_add_number != 0)
3696 {
3697 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3698 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3699 ex.X_op = O_constant;
f7ea7ef2
TS
3700 macro_build ((char *) NULL, counter, &ex,
3701 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
3702 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3703 }
3704 }
3705 else if (mips_pic == EMBEDDED_PIC)
3706 {
3707 /* We always do
cdf6fd85 3708 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
c9914766
TS
3709 */
3710 macro_build ((char *) NULL, counter, ep,
3711 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3712 "t,r,j", reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
252b5132
RH
3713 }
3714 else
3715 abort ();
3716}
3717
ea1fb5dc
RS
3718/* Move the contents of register SOURCE into register DEST. */
3719
3720static void
3721move_register (counter, dest, source)
3722 int *counter;
3723 int dest;
3724 int source;
3725{
3726 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3727 HAVE_32BIT_GPRS ? "addu" : "daddu",
3728 "d,v,t", dest, source, 0);
3729}
3730
252b5132
RH
3731/*
3732 * Build macros
3733 * This routine implements the seemingly endless macro or synthesized
3734 * instructions and addressing modes in the mips assembly language. Many
3735 * of these macros are simple and are similar to each other. These could
3736 * probably be handled by some kind of table or grammer aproach instead of
3737 * this verbose method. Others are not simple macros but are more like
3738 * optimizing code generation.
3739 * One interesting optimization is when several store macros appear
3740 * consecutivly that would load AT with the upper half of the same address.
3741 * The ensuing load upper instructions are ommited. This implies some kind
3742 * of global optimization. We currently only optimize within a single macro.
3743 * For many of the load and store macros if the address is specified as a
3744 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
3745 * first load register 'at' with zero and use it as the base register. The
3746 * mips assembler simply uses register $zero. Just one tiny optimization
3747 * we're missing.
3748 */
3749static void
3750macro (ip)
3751 struct mips_cl_insn *ip;
3752{
3753 register int treg, sreg, dreg, breg;
3754 int tempreg;
3755 int mask;
3756 int icnt = 0;
43841e91 3757 int used_at = 0;
252b5132
RH
3758 expressionS expr1;
3759 const char *s;
3760 const char *s2;
3761 const char *fmt;
3762 int likely = 0;
3763 int dbl = 0;
3764 int coproc = 0;
3765 int lr = 0;
3766 int imm = 0;
3767 offsetT maxnum;
3768 int off;
3769 bfd_reloc_code_real_type r;
252b5132
RH
3770 int hold_mips_optimize;
3771
3772 assert (! mips_opts.mips16);
3773
3774 treg = (ip->insn_opcode >> 16) & 0x1f;
3775 dreg = (ip->insn_opcode >> 11) & 0x1f;
3776 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
3777 mask = ip->insn_mo->mask;
3778
3779 expr1.X_op = O_constant;
3780 expr1.X_op_symbol = NULL;
3781 expr1.X_add_symbol = NULL;
3782 expr1.X_add_number = 1;
3783
3784 switch (mask)
3785 {
3786 case M_DABS:
3787 dbl = 1;
3788 case M_ABS:
3789 /* bgez $a0,.+12
3790 move v0,$a0
3791 sub v0,$zero,$a0
3792 */
3793
3794 mips_emit_delays (true);
3795 ++mips_opts.noreorder;
3796 mips_any_noreorder = 1;
3797
3798 expr1.X_add_number = 8;
3799 macro_build ((char *) NULL, &icnt, &expr1, "bgez", "s,p", sreg);
3800 if (dreg == sreg)
2396cfb9
TS
3801 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
3802 0);
252b5132 3803 else
ea1fb5dc 3804 move_register (&icnt, dreg, sreg);
2396cfb9 3805 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 3806 dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
252b5132
RH
3807
3808 --mips_opts.noreorder;
3809 return;
3810
3811 case M_ADD_I:
3812 s = "addi";
3813 s2 = "add";
3814 goto do_addi;
3815 case M_ADDU_I:
3816 s = "addiu";
3817 s2 = "addu";
3818 goto do_addi;
3819 case M_DADD_I:
3820 dbl = 1;
3821 s = "daddi";
3822 s2 = "dadd";
3823 goto do_addi;
3824 case M_DADDU_I:
3825 dbl = 1;
3826 s = "daddiu";
3827 s2 = "daddu";
3828 do_addi:
3829 if (imm_expr.X_op == O_constant
3830 && imm_expr.X_add_number >= -0x8000
3831 && imm_expr.X_add_number < 0x8000)
3832 {
3833 macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,j", treg, sreg,
3834 (int) BFD_RELOC_LO16);
3835 return;
3836 }
3837 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9
TS
3838 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
3839 treg, sreg, AT);
252b5132
RH
3840 break;
3841
3842 case M_AND_I:
3843 s = "andi";
3844 s2 = "and";
3845 goto do_bit;
3846 case M_OR_I:
3847 s = "ori";
3848 s2 = "or";
3849 goto do_bit;
3850 case M_NOR_I:
3851 s = "";
3852 s2 = "nor";
3853 goto do_bit;
3854 case M_XOR_I:
3855 s = "xori";
3856 s2 = "xor";
3857 do_bit:
3858 if (imm_expr.X_op == O_constant
3859 && imm_expr.X_add_number >= 0
3860 && imm_expr.X_add_number < 0x10000)
3861 {
3862 if (mask != M_NOR_I)
3863 macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,i", treg,
3864 sreg, (int) BFD_RELOC_LO16);
3865 else
3866 {
3867 macro_build ((char *) NULL, &icnt, &imm_expr, "ori", "t,r,i",
3868 treg, sreg, (int) BFD_RELOC_LO16);
2396cfb9
TS
3869 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nor",
3870 "d,v,t", treg, treg, 0);
252b5132
RH
3871 }
3872 return;
3873 }
3874
d6bc6245 3875 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
3876 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
3877 treg, sreg, AT);
252b5132
RH
3878 break;
3879
3880 case M_BEQ_I:
3881 s = "beq";
3882 goto beq_i;
3883 case M_BEQL_I:
3884 s = "beql";
3885 likely = 1;
3886 goto beq_i;
3887 case M_BNE_I:
3888 s = "bne";
3889 goto beq_i;
3890 case M_BNEL_I:
3891 s = "bnel";
3892 likely = 1;
3893 beq_i:
3894 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
3895 {
3896 macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg,
3897 0);
3898 return;
3899 }
4d34fb5f 3900 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
252b5132
RH
3901 macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg, AT);
3902 break;
3903
3904 case M_BGEL:
3905 likely = 1;
3906 case M_BGE:
3907 if (treg == 0)
3908 {
3909 macro_build ((char *) NULL, &icnt, &offset_expr,
2396cfb9 3910 likely ? "bgezl" : "bgez", "s,p", sreg);
252b5132
RH
3911 return;
3912 }
3913 if (sreg == 0)
3914 {
3915 macro_build ((char *) NULL, &icnt, &offset_expr,
2396cfb9 3916 likely ? "blezl" : "blez", "s,p", treg);
252b5132
RH
3917 return;
3918 }
2396cfb9
TS
3919 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
3920 AT, sreg, treg);
252b5132 3921 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 3922 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
3923 break;
3924
3925 case M_BGTL_I:
3926 likely = 1;
3927 case M_BGT_I:
3928 /* check for > max integer */
3929 maxnum = 0x7fffffff;
ca4e0257 3930 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
3931 {
3932 maxnum <<= 16;
3933 maxnum |= 0xffff;
3934 maxnum <<= 16;
3935 maxnum |= 0xffff;
3936 }
3937 if (imm_expr.X_op == O_constant
3938 && imm_expr.X_add_number >= maxnum
ca4e0257 3939 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
3940 {
3941 do_false:
3942 /* result is always false */
3943 if (! likely)
3944 {
39c0a331
L
3945 if (warn_nops)
3946 as_warn (_("Branch %s is always false (nop)"),
3947 ip->insn_mo->name);
2396cfb9
TS
3948 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop",
3949 "", 0);
252b5132
RH
3950 }
3951 else
3952 {
39c0a331
L
3953 if (warn_nops)
3954 as_warn (_("Branch likely %s is always false"),
3955 ip->insn_mo->name);
252b5132
RH
3956 macro_build ((char *) NULL, &icnt, &offset_expr, "bnel",
3957 "s,t,p", 0, 0);
3958 }
3959 return;
3960 }
3961 if (imm_expr.X_op != O_constant)
3962 as_bad (_("Unsupported large constant"));
f9419b05 3963 ++imm_expr.X_add_number;
252b5132
RH
3964 /* FALLTHROUGH */
3965 case M_BGE_I:
3966 case M_BGEL_I:
3967 if (mask == M_BGEL_I)
3968 likely = 1;
3969 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
3970 {
3971 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 3972 likely ? "bgezl" : "bgez", "s,p", sreg);
252b5132
RH
3973 return;
3974 }
3975 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
3976 {
3977 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 3978 likely ? "bgtzl" : "bgtz", "s,p", sreg);
252b5132
RH
3979 return;
3980 }
3981 maxnum = 0x7fffffff;
ca4e0257 3982 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
3983 {
3984 maxnum <<= 16;
3985 maxnum |= 0xffff;
3986 maxnum <<= 16;
3987 maxnum |= 0xffff;
3988 }
3989 maxnum = - maxnum - 1;
3990 if (imm_expr.X_op == O_constant
3991 && imm_expr.X_add_number <= maxnum
ca4e0257 3992 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
3993 {
3994 do_true:
3995 /* result is always true */
3996 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
3997 macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
3998 return;
3999 }
4000 set_at (&icnt, sreg, 0);
4001 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4002 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4003 break;
4004
4005 case M_BGEUL:
4006 likely = 1;
4007 case M_BGEU:
4008 if (treg == 0)
4009 goto do_true;
4010 if (sreg == 0)
4011 {
4012 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4013 likely ? "beql" : "beq", "s,t,p", 0, treg);
252b5132
RH
4014 return;
4015 }
2396cfb9
TS
4016 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4017 "d,v,t", AT, sreg, treg);
252b5132 4018 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4019 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4020 break;
4021
4022 case M_BGTUL_I:
4023 likely = 1;
4024 case M_BGTU_I:
4025 if (sreg == 0
ca4e0257 4026 || (HAVE_32BIT_GPRS
252b5132 4027 && imm_expr.X_op == O_constant
956cd1d6 4028 && imm_expr.X_add_number == (offsetT) 0xffffffff))
252b5132
RH
4029 goto do_false;
4030 if (imm_expr.X_op != O_constant)
4031 as_bad (_("Unsupported large constant"));
f9419b05 4032 ++imm_expr.X_add_number;
252b5132
RH
4033 /* FALLTHROUGH */
4034 case M_BGEU_I:
4035 case M_BGEUL_I:
4036 if (mask == M_BGEUL_I)
4037 likely = 1;
4038 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4039 goto do_true;
4040 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4041 {
4042 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4043 likely ? "bnel" : "bne", "s,t,p", sreg, 0);
252b5132
RH
4044 return;
4045 }
4046 set_at (&icnt, sreg, 1);
4047 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4048 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4049 break;
4050
4051 case M_BGTL:
4052 likely = 1;
4053 case M_BGT:
4054 if (treg == 0)
4055 {
4056 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4057 likely ? "bgtzl" : "bgtz", "s,p", sreg);
252b5132
RH
4058 return;
4059 }
4060 if (sreg == 0)
4061 {
4062 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4063 likely ? "bltzl" : "bltz", "s,p", treg);
252b5132
RH
4064 return;
4065 }
2396cfb9
TS
4066 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4067 AT, treg, sreg);
252b5132 4068 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4069 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4070 break;
4071
4072 case M_BGTUL:
4073 likely = 1;
4074 case M_BGTU:
4075 if (treg == 0)
4076 {
4077 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4078 likely ? "bnel" : "bne", "s,t,p", sreg, 0);
252b5132
RH
4079 return;
4080 }
4081 if (sreg == 0)
4082 goto do_false;
2396cfb9
TS
4083 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4084 "d,v,t", AT, treg, sreg);
252b5132 4085 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4086 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4087 break;
4088
4089 case M_BLEL:
4090 likely = 1;
4091 case M_BLE:
4092 if (treg == 0)
4093 {
4094 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4095 likely ? "blezl" : "blez", "s,p", sreg);
252b5132
RH
4096 return;
4097 }
4098 if (sreg == 0)
4099 {
4100 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4101 likely ? "bgezl" : "bgez", "s,p", treg);
252b5132
RH
4102 return;
4103 }
2396cfb9
TS
4104 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4105 AT, treg, sreg);
252b5132 4106 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4107 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4108 break;
4109
4110 case M_BLEL_I:
4111 likely = 1;
4112 case M_BLE_I:
4113 maxnum = 0x7fffffff;
ca4e0257 4114 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
4115 {
4116 maxnum <<= 16;
4117 maxnum |= 0xffff;
4118 maxnum <<= 16;
4119 maxnum |= 0xffff;
4120 }
4121 if (imm_expr.X_op == O_constant
4122 && imm_expr.X_add_number >= maxnum
ca4e0257 4123 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
4124 goto do_true;
4125 if (imm_expr.X_op != O_constant)
4126 as_bad (_("Unsupported large constant"));
f9419b05 4127 ++imm_expr.X_add_number;
252b5132
RH
4128 /* FALLTHROUGH */
4129 case M_BLT_I:
4130 case M_BLTL_I:
4131 if (mask == M_BLTL_I)
4132 likely = 1;
4133 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4134 {
4135 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4136 likely ? "bltzl" : "bltz", "s,p", sreg);
252b5132
RH
4137 return;
4138 }
4139 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4140 {
4141 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4142 likely ? "blezl" : "blez", "s,p", sreg);
252b5132
RH
4143 return;
4144 }
4145 set_at (&icnt, sreg, 0);
4146 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4147 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4148 break;
4149
4150 case M_BLEUL:
4151 likely = 1;
4152 case M_BLEU:
4153 if (treg == 0)
4154 {
4155 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4156 likely ? "beql" : "beq", "s,t,p", sreg, 0);
252b5132
RH
4157 return;
4158 }
4159 if (sreg == 0)
4160 goto do_true;
2396cfb9
TS
4161 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4162 "d,v,t", AT, treg, sreg);
252b5132 4163 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4164 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4165 break;
4166
4167 case M_BLEUL_I:
4168 likely = 1;
4169 case M_BLEU_I:
4170 if (sreg == 0
ca4e0257 4171 || (HAVE_32BIT_GPRS
252b5132 4172 && imm_expr.X_op == O_constant
956cd1d6 4173 && imm_expr.X_add_number == (offsetT) 0xffffffff))
252b5132
RH
4174 goto do_true;
4175 if (imm_expr.X_op != O_constant)
4176 as_bad (_("Unsupported large constant"));
f9419b05 4177 ++imm_expr.X_add_number;
252b5132
RH
4178 /* FALLTHROUGH */
4179 case M_BLTU_I:
4180 case M_BLTUL_I:
4181 if (mask == M_BLTUL_I)
4182 likely = 1;
4183 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4184 goto do_false;
4185 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4186 {
4187 macro_build ((char *) NULL, &icnt, &offset_expr,
4188 likely ? "beql" : "beq",
4189 "s,t,p", sreg, 0);
4190 return;
4191 }
4192 set_at (&icnt, sreg, 1);
4193 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4194 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4195 break;
4196
4197 case M_BLTL:
4198 likely = 1;
4199 case M_BLT:
4200 if (treg == 0)
4201 {
4202 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4203 likely ? "bltzl" : "bltz", "s,p", sreg);
252b5132
RH
4204 return;
4205 }
4206 if (sreg == 0)
4207 {
4208 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4209 likely ? "bgtzl" : "bgtz", "s,p", treg);
252b5132
RH
4210 return;
4211 }
2396cfb9
TS
4212 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4213 AT, sreg, treg);
252b5132 4214 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4215 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4216 break;
4217
4218 case M_BLTUL:
4219 likely = 1;
4220 case M_BLTU:
4221 if (treg == 0)
4222 goto do_false;
4223 if (sreg == 0)
4224 {
4225 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4226 likely ? "bnel" : "bne", "s,t,p", 0, treg);
252b5132
RH
4227 return;
4228 }
2396cfb9
TS
4229 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4230 "d,v,t", AT, sreg,
252b5132
RH
4231 treg);
4232 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4233 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4234 break;
4235
4236 case M_DDIV_3:
4237 dbl = 1;
4238 case M_DIV_3:
4239 s = "mflo";
4240 goto do_div3;
4241 case M_DREM_3:
4242 dbl = 1;
4243 case M_REM_3:
4244 s = "mfhi";
4245 do_div3:
4246 if (treg == 0)
4247 {
4248 as_warn (_("Divide by zero."));
4249 if (mips_trap)
2396cfb9 4250 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
9bd7d936 4251 "s,t,q", 0, 0, 7);
252b5132 4252 else
2396cfb9
TS
4253 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4254 "c", 7);
252b5132
RH
4255 return;
4256 }
4257
4258 mips_emit_delays (true);
4259 ++mips_opts.noreorder;
4260 mips_any_noreorder = 1;
4261 if (mips_trap)
4262 {
2396cfb9 4263 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
9bd7d936 4264 "s,t,q", treg, 0, 7);
2396cfb9 4265 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 4266 dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
252b5132
RH
4267 }
4268 else
4269 {
4270 expr1.X_add_number = 8;
4271 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
2396cfb9 4272 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 4273 dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
2396cfb9
TS
4274 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4275 "c", 7);
252b5132
RH
4276 }
4277 expr1.X_add_number = -1;
4278 macro_build ((char *) NULL, &icnt, &expr1,
4279 dbl ? "daddiu" : "addiu",
4280 "t,r,j", AT, 0, (int) BFD_RELOC_LO16);
4281 expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
4282 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, AT);
4283 if (dbl)
4284 {
4285 expr1.X_add_number = 1;
4286 macro_build ((char *) NULL, &icnt, &expr1, "daddiu", "t,r,j", AT, 0,
4287 (int) BFD_RELOC_LO16);
2396cfb9
TS
4288 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsll32",
4289 "d,w,<", AT, AT, 31);
252b5132
RH
4290 }
4291 else
4292 {
4293 expr1.X_add_number = 0x80000000;
4294 macro_build ((char *) NULL, &icnt, &expr1, "lui", "t,u", AT,
4295 (int) BFD_RELOC_HI16);
4296 }
4297 if (mips_trap)
4298 {
2396cfb9 4299 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
9bd7d936 4300 "s,t,q", sreg, AT, 6);
252b5132
RH
4301 /* We want to close the noreorder block as soon as possible, so
4302 that later insns are available for delay slot filling. */
4303 --mips_opts.noreorder;
4304 }
4305 else
4306 {
4307 expr1.X_add_number = 8;
4308 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", sreg, AT);
2396cfb9
TS
4309 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
4310 0);
252b5132
RH
4311
4312 /* We want to close the noreorder block as soon as possible, so
4313 that later insns are available for delay slot filling. */
4314 --mips_opts.noreorder;
4315
2396cfb9
TS
4316 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4317 "c", 6);
252b5132 4318 }
2396cfb9 4319 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d", dreg);
252b5132
RH
4320 break;
4321
4322 case M_DIV_3I:
4323 s = "div";
4324 s2 = "mflo";
4325 goto do_divi;
4326 case M_DIVU_3I:
4327 s = "divu";
4328 s2 = "mflo";
4329 goto do_divi;
4330 case M_REM_3I:
4331 s = "div";
4332 s2 = "mfhi";
4333 goto do_divi;
4334 case M_REMU_3I:
4335 s = "divu";
4336 s2 = "mfhi";
4337 goto do_divi;
4338 case M_DDIV_3I:
4339 dbl = 1;
4340 s = "ddiv";
4341 s2 = "mflo";
4342 goto do_divi;
4343 case M_DDIVU_3I:
4344 dbl = 1;
4345 s = "ddivu";
4346 s2 = "mflo";
4347 goto do_divi;
4348 case M_DREM_3I:
4349 dbl = 1;
4350 s = "ddiv";
4351 s2 = "mfhi";
4352 goto do_divi;
4353 case M_DREMU_3I:
4354 dbl = 1;
4355 s = "ddivu";
4356 s2 = "mfhi";
4357 do_divi:
4358 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4359 {
4360 as_warn (_("Divide by zero."));
4361 if (mips_trap)
2396cfb9 4362 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
9bd7d936 4363 "s,t,q", 0, 0, 7);
252b5132 4364 else
2396cfb9
TS
4365 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4366 "c", 7);
252b5132
RH
4367 return;
4368 }
4369 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4370 {
4371 if (strcmp (s2, "mflo") == 0)
ea1fb5dc 4372 move_register (&icnt, dreg, sreg);
252b5132 4373 else
ea1fb5dc 4374 move_register (&icnt, dreg, 0);
252b5132
RH
4375 return;
4376 }
4377 if (imm_expr.X_op == O_constant
4378 && imm_expr.X_add_number == -1
4379 && s[strlen (s) - 1] != 'u')
4380 {
4381 if (strcmp (s2, "mflo") == 0)
4382 {
2396cfb9
TS
4383 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4384 dbl ? "dneg" : "neg", "d,w", dreg, sreg);
252b5132
RH
4385 }
4386 else
ea1fb5dc 4387 move_register (&icnt, dreg, 0);
252b5132
RH
4388 return;
4389 }
4390
4391 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9
TS
4392 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4393 sreg, AT);
4394 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
252b5132
RH
4395 break;
4396
4397 case M_DIVU_3:
4398 s = "divu";
4399 s2 = "mflo";
4400 goto do_divu3;
4401 case M_REMU_3:
4402 s = "divu";
4403 s2 = "mfhi";
4404 goto do_divu3;
4405 case M_DDIVU_3:
4406 s = "ddivu";
4407 s2 = "mflo";
4408 goto do_divu3;
4409 case M_DREMU_3:
4410 s = "ddivu";
4411 s2 = "mfhi";
4412 do_divu3:
4413 mips_emit_delays (true);
4414 ++mips_opts.noreorder;
4415 mips_any_noreorder = 1;
4416 if (mips_trap)
4417 {
2396cfb9 4418 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
9bd7d936 4419 "s,t,q", treg, 0, 7);
2396cfb9
TS
4420 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4421 sreg, treg);
252b5132
RH
4422 /* We want to close the noreorder block as soon as possible, so
4423 that later insns are available for delay slot filling. */
4424 --mips_opts.noreorder;
4425 }
4426 else
4427 {
4428 expr1.X_add_number = 8;
4429 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
2396cfb9
TS
4430 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4431 sreg, treg);
252b5132
RH
4432
4433 /* We want to close the noreorder block as soon as possible, so
4434 that later insns are available for delay slot filling. */
4435 --mips_opts.noreorder;
2396cfb9
TS
4436 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4437 "c", 7);
252b5132 4438 }
2396cfb9 4439 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
252b5132
RH
4440 return;
4441
4442 case M_DLA_AB:
4443 dbl = 1;
4444 case M_LA_AB:
4445 /* Load the address of a symbol into a register. If breg is not
4446 zero, we then add a base register to it. */
4447
3bec30a8
TS
4448 if (dbl && HAVE_32BIT_GPRS)
4449 as_warn (_("dla used to load 32-bit register"));
4450
c90bbe5b 4451 if (! dbl && HAVE_64BIT_OBJECTS)
3bec30a8
TS
4452 as_warn (_("la used to load 64-bit address"));
4453
0c11417f
MR
4454 if (offset_expr.X_op == O_constant
4455 && offset_expr.X_add_number >= -0x8000
4456 && offset_expr.X_add_number < 0x8000)
4457 {
4458 macro_build ((char *) NULL, &icnt, &offset_expr,
4459 (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
4460 "t,r,j", treg, sreg, (int) BFD_RELOC_LO16);
4461 return;
4462 }
4463
afdbd6d0
CD
4464 if (treg == breg)
4465 {
4466 tempreg = AT;
4467 used_at = 1;
4468 }
4469 else
4470 {
4471 tempreg = treg;
4472 used_at = 0;
4473 }
4474
252b5132
RH
4475 /* When generating embedded PIC code, we permit expressions of
4476 the form
afdbd6d0
CD
4477 la $treg,foo-bar
4478 la $treg,foo-bar($breg)
bb2d6cd7 4479 where bar is an address in the current section. These are used
252b5132
RH
4480 when getting the addresses of functions. We don't permit
4481 X_add_number to be non-zero, because if the symbol is
4482 external the relaxing code needs to know that any addend is
4483 purely the offset to X_op_symbol. */
4484 if (mips_pic == EMBEDDED_PIC
4485 && offset_expr.X_op == O_subtract
49309057 4486 && (symbol_constant_p (offset_expr.X_op_symbol)
bb2d6cd7 4487 ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
49309057
ILT
4488 : (symbol_equated_p (offset_expr.X_op_symbol)
4489 && (S_GET_SEGMENT
4490 (symbol_get_value_expression (offset_expr.X_op_symbol)
4491 ->X_add_symbol)
bb2d6cd7 4492 == now_seg)))
bb2d6cd7
GK
4493 && (offset_expr.X_add_number == 0
4494 || OUTPUT_FLAVOR == bfd_target_elf_flavour))
252b5132 4495 {
afdbd6d0
CD
4496 if (breg == 0)
4497 {
4498 tempreg = treg;
4499 used_at = 0;
4500 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4501 tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4502 }
4503 else
4504 {
4505 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4506 tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4507 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4d34fb5f 4508 (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu",
afdbd6d0
CD
4509 "d,v,t", tempreg, tempreg, breg);
4510 }
252b5132 4511 macro_build ((char *) NULL, &icnt, &offset_expr,
4d34fb5f 4512 (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
afdbd6d0
CD
4513 "t,r,j", treg, tempreg, (int) BFD_RELOC_PCREL_LO16);
4514 if (! used_at)
4515 return;
4516 break;
252b5132
RH
4517 }
4518
4519 if (offset_expr.X_op != O_symbol
4520 && offset_expr.X_op != O_constant)
4521 {
4522 as_bad (_("expression too complex"));
4523 offset_expr.X_op = O_constant;
4524 }
4525
252b5132 4526 if (offset_expr.X_op == O_constant)
4d34fb5f
TS
4527 load_register (&icnt, tempreg, &offset_expr,
4528 ((mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
4529 ? (dbl || HAVE_64BIT_ADDRESSES)
4530 : HAVE_64BIT_ADDRESSES));
252b5132
RH
4531 else if (mips_pic == NO_PIC)
4532 {
d6bc6245 4533 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 4534 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
4535 Otherwise we want
4536 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
4537 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4538 If we have a constant, we need two instructions anyhow,
d6bc6245 4539 so we may as well always use the latter form.
76b3015f 4540
d6bc6245
TS
4541 With 64bit address space and a usable $at we want
4542 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4543 lui $at,<sym> (BFD_RELOC_HI16_S)
4544 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4545 daddiu $at,<sym> (BFD_RELOC_LO16)
4546 dsll32 $tempreg,0
3a482fd5 4547 daddu $tempreg,$tempreg,$at
76b3015f 4548
d6bc6245
TS
4549 If $at is already in use, we use an path which is suboptimal
4550 on superscalar processors.
4551 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4552 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4553 dsll $tempreg,16
4554 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
4555 dsll $tempreg,16
4556 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
4557 */
f9419b05 4558 char *p = NULL;
d6bc6245 4559 if (HAVE_64BIT_ADDRESSES)
252b5132 4560 {
d6bc6245
TS
4561 /* We don't do GP optimization for now because RELAX_ENCODE can't
4562 hold the data for such large chunks. */
4563
460597ba 4564 if (used_at == 0 && ! mips_opts.noat)
98d3f06f
KH
4565 {
4566 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4567 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4568 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4569 AT, (int) BFD_RELOC_HI16_S);
4570 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4571 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4572 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4573 AT, AT, (int) BFD_RELOC_LO16);
4574 macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
4575 "d,w,<", tempreg, tempreg, 0);
3a482fd5
MR
4576 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
4577 "d,v,t", tempreg, tempreg, AT);
98d3f06f
KH
4578 used_at = 1;
4579 }
4580 else
4581 {
4582 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4583 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4584 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4585 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4586 macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4587 tempreg, tempreg, 16);
4588 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4589 tempreg, tempreg, (int) BFD_RELOC_HI16_S);
4590 macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4591 tempreg, tempreg, 16);
4592 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4593 tempreg, tempreg, (int) BFD_RELOC_LO16);
4594 }
4595 }
4596 else
4597 {
4598 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4599 && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
4600 {
4601 frag_grow (20);
4602 macro_build ((char *) NULL, &icnt, &offset_expr, "addiu",
c9914766
TS
4603 "t,r,j", tempreg, mips_gp_register,
4604 (int) BFD_RELOC_GPREL16);
98d3f06f
KH
4605 p = frag_var (rs_machine_dependent, 8, 0,
4606 RELAX_ENCODE (4, 8, 0, 4, 0,
4607 mips_opts.warn_about_macros),
4608 offset_expr.X_add_symbol, 0, NULL);
4609 }
4610 macro_build_lui (p, &icnt, &offset_expr, tempreg);
4611 if (p != NULL)
4612 p += 4;
4613 macro_build (p, &icnt, &offset_expr, "addiu",
4614 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4615 }
252b5132
RH
4616 }
4617 else if (mips_pic == SVR4_PIC && ! mips_big_got)
4618 {
9117d219
NC
4619 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
4620
252b5132
RH
4621 /* If this is a reference to an external symbol, and there
4622 is no constant, we want
4623 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9117d219
NC
4624 or if tempreg is PIC_CALL_REG
4625 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
252b5132
RH
4626 For a local symbol, we want
4627 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4628 nop
4629 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4630
4631 If we have a small constant, and this is a reference to
4632 an external symbol, we want
4633 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4634 nop
4635 addiu $tempreg,$tempreg,<constant>
4636 For a local symbol, we want the same instruction
4637 sequence, but we output a BFD_RELOC_LO16 reloc on the
4638 addiu instruction.
4639
4640 If we have a large constant, and this is a reference to
4641 an external symbol, we want
4642 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4643 lui $at,<hiconstant>
4644 addiu $at,$at,<loconstant>
4645 addu $tempreg,$tempreg,$at
4646 For a local symbol, we want the same instruction
4647 sequence, but we output a BFD_RELOC_LO16 reloc on the
4648 addiu instruction. */
4649 expr1.X_add_number = offset_expr.X_add_number;
4650 offset_expr.X_add_number = 0;
4651 frag_grow (32);
9117d219
NC
4652 if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4653 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
4d34fb5f
TS
4654 macro_build ((char *) NULL, &icnt, &offset_expr,
4655 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
c9914766 4656 "t,o(b)", tempreg, lw_reloc_type, mips_gp_register);
252b5132
RH
4657 if (expr1.X_add_number == 0)
4658 {
4659 int off;
f9419b05 4660 char *p;
252b5132
RH
4661
4662 if (breg == 0)
4663 off = 0;
4664 else
4665 {
4666 /* We're going to put in an addu instruction using
4667 tempreg, so we may as well insert the nop right
4668 now. */
4669 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4670 "nop", "");
4671 off = 4;
4672 }
4673 p = frag_var (rs_machine_dependent, 8 - off, 0,
4674 RELAX_ENCODE (0, 8 - off, -4 - off, 4 - off, 0,
4675 (breg == 0
4676 ? mips_opts.warn_about_macros
4677 : 0)),
c4e7957c 4678 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
4679 if (breg == 0)
4680 {
4681 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4682 p += 4;
4683 }
4684 macro_build (p, &icnt, &expr1,
ca4e0257 4685 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
4686 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4687 /* FIXME: If breg == 0, and the next instruction uses
4688 $tempreg, then if this variant case is used an extra
4689 nop will be generated. */
4690 }
4691 else if (expr1.X_add_number >= -0x8000
4692 && expr1.X_add_number < 0x8000)
4693 {
4694 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4695 "nop", "");
4696 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 4697 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132 4698 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
c4e7957c
TS
4699 frag_var (rs_machine_dependent, 0, 0,
4700 RELAX_ENCODE (0, 0, -12, -4, 0, 0),
4701 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
4702 }
4703 else
4704 {
4705 int off1;
4706
4707 /* If we are going to add in a base register, and the
4708 target register and the base register are the same,
4709 then we are using AT as a temporary register. Since
4710 we want to load the constant into AT, we add our
4711 current AT (from the global offset table) and the
4712 register into the register now, and pretend we were
4713 not using a base register. */
4714 if (breg != treg)
4715 off1 = 0;
4716 else
4717 {
4718 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4719 "nop", "");
4720 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 4721 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
4722 "d,v,t", treg, AT, breg);
4723 breg = 0;
4724 tempreg = treg;
4725 off1 = -8;
4726 }
4727
4728 /* Set mips_optimize around the lui instruction to avoid
4729 inserting an unnecessary nop after the lw. */
4730 hold_mips_optimize = mips_optimize;
4731 mips_optimize = 2;
c4e7957c 4732 macro_build_lui (NULL, &icnt, &expr1, AT);
252b5132
RH
4733 mips_optimize = hold_mips_optimize;
4734
4735 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 4736 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
4737 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4738 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 4739 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132 4740 "d,v,t", tempreg, tempreg, AT);
c4e7957c
TS
4741 frag_var (rs_machine_dependent, 0, 0,
4742 RELAX_ENCODE (0, 0, -16 + off1, -8, 0, 0),
4743 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
4744 used_at = 1;
4745 }
4746 }
4747 else if (mips_pic == SVR4_PIC)
4748 {
4749 int gpdel;
f9419b05 4750 char *p;
9117d219
NC
4751 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
4752 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
252b5132
RH
4753
4754 /* This is the large GOT case. If this is a reference to an
4755 external symbol, and there is no constant, we want
4756 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
4757 addu $tempreg,$tempreg,$gp
4758 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
9117d219
NC
4759 or if tempreg is PIC_CALL_REG
4760 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
4761 addu $tempreg,$tempreg,$gp
4762 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
252b5132
RH
4763 For a local symbol, we want
4764 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4765 nop
4766 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4767
4768 If we have a small constant, and this is a reference to
4769 an external symbol, we want
4770 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
4771 addu $tempreg,$tempreg,$gp
4772 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4773 nop
4774 addiu $tempreg,$tempreg,<constant>
4775 For a local symbol, we want
4776 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4777 nop
4778 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
4779
4780 If we have a large constant, and this is a reference to
4781 an external symbol, we want
4782 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
4783 addu $tempreg,$tempreg,$gp
4784 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4785 lui $at,<hiconstant>
4786 addiu $at,$at,<loconstant>
4787 addu $tempreg,$tempreg,$at
4788 For a local symbol, we want
4789 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4790 lui $at,<hiconstant>
4791 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
4792 addu $tempreg,$tempreg,$at
438c16b8
TS
4793
4794 For NewABI, we want for data addresses
4795 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
4796 If tempreg is PIC_CALL_REG pointing to a external symbol, we want
4797 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
4798 */
4799 if (HAVE_NEWABI)
4800 {
4801 int reloc_type = (tempreg == PIC_CALL_REG
4802 ? BFD_RELOC_MIPS_CALL16
4803 : BFD_RELOC_MIPS_GOT_DISP);
4804
4805 macro_build ((char *) NULL, &icnt, &offset_expr,
4806 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4807 "t,o(b)", tempreg, reloc_type, mips_gp_register);
4808
4809 if (breg != 0)
4810 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4811 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4812 "d,v,t", treg, tempreg, breg);
4813
4814 if (! used_at)
4815 return;
4816
4817 break;
4818 }
252b5132
RH
4819 expr1.X_add_number = offset_expr.X_add_number;
4820 offset_expr.X_add_number = 0;
4821 frag_grow (52);
f7ea7ef2 4822 if (reg_needs_delay (mips_gp_register))
252b5132
RH
4823 gpdel = 4;
4824 else
4825 gpdel = 0;
9117d219
NC
4826 if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4827 {
4828 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
4829 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
4830 }
252b5132 4831 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
9117d219 4832 tempreg, lui_reloc_type);
252b5132 4833 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 4834 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 4835 "d,v,t", tempreg, tempreg, mips_gp_register);
252b5132 4836 macro_build ((char *) NULL, &icnt, &offset_expr,
4d34fb5f 4837 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
9117d219 4838 "t,o(b)", tempreg, lw_reloc_type, tempreg);
252b5132
RH
4839 if (expr1.X_add_number == 0)
4840 {
4841 int off;
4842
4843 if (breg == 0)
4844 off = 0;
4845 else
4846 {
4847 /* We're going to put in an addu instruction using
4848 tempreg, so we may as well insert the nop right
4849 now. */
4850 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4851 "nop", "");
4852 off = 4;
4853 }
4854
4855 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4856 RELAX_ENCODE (12 + off, 12 + gpdel, gpdel,
4857 8 + gpdel, 0,
4858 (breg == 0
4859 ? mips_opts.warn_about_macros
4860 : 0)),
c4e7957c 4861 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
4862 }
4863 else if (expr1.X_add_number >= -0x8000
4864 && expr1.X_add_number < 0x8000)
4865 {
4866 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4867 "nop", "");
4868 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 4869 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
4870 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4871
4872 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4873 RELAX_ENCODE (20, 12 + gpdel, gpdel, 8 + gpdel, 0,
4874 (breg == 0
4875 ? mips_opts.warn_about_macros
4876 : 0)),
c4e7957c 4877 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
4878 }
4879 else
4880 {
4881 int adj, dreg;
4882
4883 /* If we are going to add in a base register, and the
4884 target register and the base register are the same,
4885 then we are using AT as a temporary register. Since
4886 we want to load the constant into AT, we add our
4887 current AT (from the global offset table) and the
4888 register into the register now, and pretend we were
4889 not using a base register. */
4890 if (breg != treg)
4891 {
4892 adj = 0;
4893 dreg = tempreg;
4894 }
4895 else
4896 {
4897 assert (tempreg == AT);
4898 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4899 "nop", "");
4900 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 4901 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
4902 "d,v,t", treg, AT, breg);
4903 dreg = treg;
4904 adj = 8;
4905 }
4906
4907 /* Set mips_optimize around the lui instruction to avoid
4908 inserting an unnecessary nop after the lw. */
4909 hold_mips_optimize = mips_optimize;
4910 mips_optimize = 2;
c4e7957c 4911 macro_build_lui (NULL, &icnt, &expr1, AT);
252b5132
RH
4912 mips_optimize = hold_mips_optimize;
4913
4914 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 4915 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
4916 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4917 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 4918 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
4919 "d,v,t", dreg, dreg, AT);
4920
4921 p = frag_var (rs_machine_dependent, 16 + gpdel + adj, 0,
4922 RELAX_ENCODE (24 + adj, 16 + gpdel + adj, gpdel,
4923 8 + gpdel, 0,
4924 (breg == 0
4925 ? mips_opts.warn_about_macros
4926 : 0)),
c4e7957c 4927 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
4928
4929 used_at = 1;
4930 }
4931
4932 if (gpdel > 0)
4933 {
4934 /* This is needed because this instruction uses $gp, but
4935 the first instruction on the main stream does not. */
4936 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4937 p += 4;
4938 }
4939 macro_build (p, &icnt, &offset_expr,
4d34fb5f 4940 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
c9914766
TS
4941 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16,
4942 mips_gp_register);
252b5132
RH
4943 p += 4;
4944 if (expr1.X_add_number >= -0x8000
4945 && expr1.X_add_number < 0x8000)
4946 {
4947 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4948 p += 4;
4949 macro_build (p, &icnt, &expr1,
ca4e0257 4950 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
4951 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4952 /* FIXME: If add_number is 0, and there was no base
4953 register, the external symbol case ended with a load,
4954 so if the symbol turns out to not be external, and
4955 the next instruction uses tempreg, an unnecessary nop
4956 will be inserted. */
4957 }
4958 else
4959 {
4960 if (breg == treg)
4961 {
4962 /* We must add in the base register now, as in the
4963 external symbol case. */
4964 assert (tempreg == AT);
4965 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4966 p += 4;
4967 macro_build (p, &icnt, (expressionS *) NULL,
ca4e0257 4968 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
4969 "d,v,t", treg, AT, breg);
4970 p += 4;
4971 tempreg = treg;
4972 /* We set breg to 0 because we have arranged to add
4973 it in in both cases. */
4974 breg = 0;
4975 }
4976
4977 macro_build_lui (p, &icnt, &expr1, AT);
4978 p += 4;
4979 macro_build (p, &icnt, &expr1,
ca4e0257 4980 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
4981 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4982 p += 4;
4983 macro_build (p, &icnt, (expressionS *) NULL,
ca4e0257 4984 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
4985 "d,v,t", tempreg, tempreg, AT);
4986 p += 4;
4987 }
4988 }
4989 else if (mips_pic == EMBEDDED_PIC)
4990 {
4991 /* We use
cdf6fd85 4992 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
4993 */
4994 macro_build ((char *) NULL, &icnt, &offset_expr,
c9914766
TS
4995 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
4996 tempreg, mips_gp_register, (int) BFD_RELOC_GPREL16);
252b5132
RH
4997 }
4998 else
4999 abort ();
5000
5001 if (breg != 0)
4d34fb5f
TS
5002 {
5003 char *s;
5004
5005 if (mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
5006 s = (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu";
5007 else
5008 s = HAVE_64BIT_ADDRESSES ? "daddu" : "addu";
5009
5010 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s,
5011 "d,v,t", treg, tempreg, breg);
5012 }
252b5132
RH
5013
5014 if (! used_at)
5015 return;
5016
5017 break;
5018
5019 case M_J_A:
5020 /* The j instruction may not be used in PIC code, since it
5021 requires an absolute address. We convert it to a b
5022 instruction. */
5023 if (mips_pic == NO_PIC)
5024 macro_build ((char *) NULL, &icnt, &offset_expr, "j", "a");
5025 else
5026 macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
5027 return;
5028
5029 /* The jal instructions must be handled as macros because when
5030 generating PIC code they expand to multi-instruction
5031 sequences. Normally they are simple instructions. */
5032 case M_JAL_1:
5033 dreg = RA;
5034 /* Fall through. */
5035 case M_JAL_2:
5036 if (mips_pic == NO_PIC
5037 || mips_pic == EMBEDDED_PIC)
5038 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5039 "d,s", dreg, sreg);
5040 else if (mips_pic == SVR4_PIC)
5041 {
5042 if (sreg != PIC_CALL_REG)
5043 as_warn (_("MIPS PIC call to register other than $25"));
bdaaa2e1 5044
252b5132
RH
5045 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5046 "d,s", dreg, sreg);
6478892d 5047 if (! HAVE_NEWABI)
252b5132 5048 {
6478892d
TS
5049 if (mips_cprestore_offset < 0)
5050 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5051 else
5052 {
7a621144
DJ
5053 if (! mips_frame_reg_valid)
5054 {
5055 as_warn (_("No .frame pseudo-op used in PIC code"));
5056 /* Quiet this warning. */
5057 mips_frame_reg_valid = 1;
5058 }
5059 if (! mips_cprestore_valid)
5060 {
5061 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5062 /* Quiet this warning. */
5063 mips_cprestore_valid = 1;
5064 }
6478892d
TS
5065 expr1.X_add_number = mips_cprestore_offset;
5066 macro_build ((char *) NULL, &icnt, &expr1,
5067 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
c9914766
TS
5068 mips_gp_register, (int) BFD_RELOC_LO16,
5069 mips_frame_reg);
6478892d 5070 }
252b5132
RH
5071 }
5072 }
5073 else
5074 abort ();
5075
5076 return;
5077
5078 case M_JAL_A:
5079 if (mips_pic == NO_PIC)
5080 macro_build ((char *) NULL, &icnt, &offset_expr, "jal", "a");
5081 else if (mips_pic == SVR4_PIC)
5082 {
f9419b05
TS
5083 char *p;
5084
252b5132
RH
5085 /* If this is a reference to an external symbol, and we are
5086 using a small GOT, we want
5087 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5088 nop
f9419b05 5089 jalr $ra,$25
252b5132
RH
5090 nop
5091 lw $gp,cprestore($sp)
5092 The cprestore value is set using the .cprestore
5093 pseudo-op. If we are using a big GOT, we want
5094 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5095 addu $25,$25,$gp
5096 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
5097 nop
f9419b05 5098 jalr $ra,$25
252b5132
RH
5099 nop
5100 lw $gp,cprestore($sp)
5101 If the symbol is not external, we want
5102 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5103 nop
5104 addiu $25,$25,<sym> (BFD_RELOC_LO16)
f9419b05 5105 jalr $ra,$25
252b5132 5106 nop
438c16b8
TS
5107 lw $gp,cprestore($sp)
5108 For NewABI, we want
5109 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5110 jalr $ra,$25 (BFD_RELOC_MIPS_JALR)
5111 */
5112 if (HAVE_NEWABI)
252b5132
RH
5113 {
5114 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 5115 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132 5116 "t,o(b)", PIC_CALL_REG,
438c16b8
TS
5117 (int) BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5118 macro_build_jalr (icnt, &offset_expr);
252b5132
RH
5119 }
5120 else
5121 {
438c16b8
TS
5122 frag_grow (40);
5123 if (! mips_big_got)
5124 {
5125 macro_build ((char *) NULL, &icnt, &offset_expr,
5126 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5127 "t,o(b)", PIC_CALL_REG,
5128 (int) BFD_RELOC_MIPS_CALL16, mips_gp_register);
5129 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5130 "nop", "");
5131 p = frag_var (rs_machine_dependent, 4, 0,
5132 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5133 offset_expr.X_add_symbol, 0, NULL);
5134 }
252b5132 5135 else
252b5132 5136 {
438c16b8
TS
5137 int gpdel;
5138
5139 if (reg_needs_delay (mips_gp_register))
5140 gpdel = 4;
5141 else
5142 gpdel = 0;
5143 macro_build ((char *) NULL, &icnt, &offset_expr, "lui",
5144 "t,u", PIC_CALL_REG,
5145 (int) BFD_RELOC_MIPS_CALL_HI16);
5146 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5147 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5148 "d,v,t", PIC_CALL_REG, PIC_CALL_REG,
5149 mips_gp_register);
5150 macro_build ((char *) NULL, &icnt, &offset_expr,
5151 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5152 "t,o(b)", PIC_CALL_REG,
5153 (int) BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
5154 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5155 "nop", "");
5156 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5157 RELAX_ENCODE (16, 12 + gpdel, gpdel,
5158 8 + gpdel, 0, 0),
5159 offset_expr.X_add_symbol, 0, NULL);
5160 if (gpdel > 0)
5161 {
5162 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5163 p += 4;
5164 }
5165 macro_build (p, &icnt, &offset_expr,
5166 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5167 "t,o(b)", PIC_CALL_REG,
5168 (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
5169 p += 4;
252b5132
RH
5170 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5171 p += 4;
5172 }
5173 macro_build (p, &icnt, &offset_expr,
438c16b8
TS
5174 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5175 "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
5176 (int) BFD_RELOC_LO16);
5177 macro_build_jalr (icnt, &offset_expr);
5178
6478892d
TS
5179 if (mips_cprestore_offset < 0)
5180 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5181 else
5182 {
7a621144
DJ
5183 if (! mips_frame_reg_valid)
5184 {
5185 as_warn (_("No .frame pseudo-op used in PIC code"));
5186 /* Quiet this warning. */
5187 mips_frame_reg_valid = 1;
5188 }
5189 if (! mips_cprestore_valid)
5190 {
5191 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5192 /* Quiet this warning. */
5193 mips_cprestore_valid = 1;
5194 }
6478892d
TS
5195 if (mips_opts.noreorder)
5196 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
98d3f06f 5197 "nop", "");
6478892d
TS
5198 expr1.X_add_number = mips_cprestore_offset;
5199 macro_build ((char *) NULL, &icnt, &expr1,
5200 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
c9914766
TS
5201 mips_gp_register, (int) BFD_RELOC_LO16,
5202 mips_frame_reg);
6478892d 5203 }
252b5132
RH
5204 }
5205 }
5206 else if (mips_pic == EMBEDDED_PIC)
5207 {
5208 macro_build ((char *) NULL, &icnt, &offset_expr, "bal", "p");
5209 /* The linker may expand the call to a longer sequence which
5210 uses $at, so we must break rather than return. */
5211 break;
5212 }
5213 else
5214 abort ();
5215
5216 return;
5217
5218 case M_LB_AB:
5219 s = "lb";
5220 goto ld;
5221 case M_LBU_AB:
5222 s = "lbu";
5223 goto ld;
5224 case M_LH_AB:
5225 s = "lh";
5226 goto ld;
5227 case M_LHU_AB:
5228 s = "lhu";
5229 goto ld;
5230 case M_LW_AB:
5231 s = "lw";
5232 goto ld;
5233 case M_LWC0_AB:
5234 s = "lwc0";
bdaaa2e1 5235 /* Itbl support may require additional care here. */
252b5132
RH
5236 coproc = 1;
5237 goto ld;
5238 case M_LWC1_AB:
5239 s = "lwc1";
bdaaa2e1 5240 /* Itbl support may require additional care here. */
252b5132
RH
5241 coproc = 1;
5242 goto ld;
5243 case M_LWC2_AB:
5244 s = "lwc2";
bdaaa2e1 5245 /* Itbl support may require additional care here. */
252b5132
RH
5246 coproc = 1;
5247 goto ld;
5248 case M_LWC3_AB:
5249 s = "lwc3";
bdaaa2e1 5250 /* Itbl support may require additional care here. */
252b5132
RH
5251 coproc = 1;
5252 goto ld;
5253 case M_LWL_AB:
5254 s = "lwl";
5255 lr = 1;
5256 goto ld;
5257 case M_LWR_AB:
5258 s = "lwr";
5259 lr = 1;
5260 goto ld;
5261 case M_LDC1_AB:
ec68c924 5262 if (mips_arch == CPU_R4650)
252b5132
RH
5263 {
5264 as_bad (_("opcode not supported on this processor"));
5265 return;
5266 }
5267 s = "ldc1";
bdaaa2e1 5268 /* Itbl support may require additional care here. */
252b5132
RH
5269 coproc = 1;
5270 goto ld;
5271 case M_LDC2_AB:
5272 s = "ldc2";
bdaaa2e1 5273 /* Itbl support may require additional care here. */
252b5132
RH
5274 coproc = 1;
5275 goto ld;
5276 case M_LDC3_AB:
5277 s = "ldc3";
bdaaa2e1 5278 /* Itbl support may require additional care here. */
252b5132
RH
5279 coproc = 1;
5280 goto ld;
5281 case M_LDL_AB:
5282 s = "ldl";
5283 lr = 1;
5284 goto ld;
5285 case M_LDR_AB:
5286 s = "ldr";
5287 lr = 1;
5288 goto ld;
5289 case M_LL_AB:
5290 s = "ll";
5291 goto ld;
5292 case M_LLD_AB:
5293 s = "lld";
5294 goto ld;
5295 case M_LWU_AB:
5296 s = "lwu";
5297 ld:
5298 if (breg == treg || coproc || lr)
5299 {
5300 tempreg = AT;
5301 used_at = 1;
5302 }
5303 else
5304 {
5305 tempreg = treg;
5306 used_at = 0;
5307 }
5308 goto ld_st;
5309 case M_SB_AB:
5310 s = "sb";
5311 goto st;
5312 case M_SH_AB:
5313 s = "sh";
5314 goto st;
5315 case M_SW_AB:
5316 s = "sw";
5317 goto st;
5318 case M_SWC0_AB:
5319 s = "swc0";
bdaaa2e1 5320 /* Itbl support may require additional care here. */
252b5132
RH
5321 coproc = 1;
5322 goto st;
5323 case M_SWC1_AB:
5324 s = "swc1";
bdaaa2e1 5325 /* Itbl support may require additional care here. */
252b5132
RH
5326 coproc = 1;
5327 goto st;
5328 case M_SWC2_AB:
5329 s = "swc2";
bdaaa2e1 5330 /* Itbl support may require additional care here. */
252b5132
RH
5331 coproc = 1;
5332 goto st;
5333 case M_SWC3_AB:
5334 s = "swc3";
bdaaa2e1 5335 /* Itbl support may require additional care here. */
252b5132
RH
5336 coproc = 1;
5337 goto st;
5338 case M_SWL_AB:
5339 s = "swl";
5340 goto st;
5341 case M_SWR_AB:
5342 s = "swr";
5343 goto st;
5344 case M_SC_AB:
5345 s = "sc";
5346 goto st;
5347 case M_SCD_AB:
5348 s = "scd";
5349 goto st;
5350 case M_SDC1_AB:
ec68c924 5351 if (mips_arch == CPU_R4650)
252b5132
RH
5352 {
5353 as_bad (_("opcode not supported on this processor"));
5354 return;
5355 }
5356 s = "sdc1";
5357 coproc = 1;
bdaaa2e1 5358 /* Itbl support may require additional care here. */
252b5132
RH
5359 goto st;
5360 case M_SDC2_AB:
5361 s = "sdc2";
bdaaa2e1 5362 /* Itbl support may require additional care here. */
252b5132
RH
5363 coproc = 1;
5364 goto st;
5365 case M_SDC3_AB:
5366 s = "sdc3";
bdaaa2e1 5367 /* Itbl support may require additional care here. */
252b5132
RH
5368 coproc = 1;
5369 goto st;
5370 case M_SDL_AB:
5371 s = "sdl";
5372 goto st;
5373 case M_SDR_AB:
5374 s = "sdr";
5375 st:
5376 tempreg = AT;
5377 used_at = 1;
5378 ld_st:
bdaaa2e1 5379 /* Itbl support may require additional care here. */
252b5132
RH
5380 if (mask == M_LWC1_AB
5381 || mask == M_SWC1_AB
5382 || mask == M_LDC1_AB
5383 || mask == M_SDC1_AB
5384 || mask == M_L_DAB
5385 || mask == M_S_DAB)
5386 fmt = "T,o(b)";
5387 else if (coproc)
5388 fmt = "E,o(b)";
5389 else
5390 fmt = "t,o(b)";
5391
afdbd6d0
CD
5392 /* For embedded PIC, we allow loads where the offset is calculated
5393 by subtracting a symbol in the current segment from an unknown
5394 symbol, relative to a base register, e.g.:
5395 <op> $treg, <sym>-<localsym>($breg)
5396 This is used by the compiler for switch statements. */
76b3015f 5397 if (mips_pic == EMBEDDED_PIC
afdbd6d0
CD
5398 && offset_expr.X_op == O_subtract
5399 && (symbol_constant_p (offset_expr.X_op_symbol)
5400 ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
5401 : (symbol_equated_p (offset_expr.X_op_symbol)
5402 && (S_GET_SEGMENT
5403 (symbol_get_value_expression (offset_expr.X_op_symbol)
5404 ->X_add_symbol)
5405 == now_seg)))
5406 && breg != 0
5407 && (offset_expr.X_add_number == 0
5408 || OUTPUT_FLAVOR == bfd_target_elf_flavour))
5409 {
5410 /* For this case, we output the instructions:
5411 lui $tempreg,<sym> (BFD_RELOC_PCREL_HI16_S)
5412 addiu $tempreg,$tempreg,$breg
5413 <op> $treg,<sym>($tempreg) (BFD_RELOC_PCREL_LO16)
5414 If the relocation would fit entirely in 16 bits, it would be
5415 nice to emit:
5416 <op> $treg,<sym>($breg) (BFD_RELOC_PCREL_LO16)
5417 instead, but that seems quite difficult. */
5418 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5419 tempreg, (int) BFD_RELOC_PCREL_HI16_S);
5420 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5421 ((bfd_arch_bits_per_address (stdoutput) == 32
5422 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
5423 ? "addu" : "daddu"),
5424 "d,v,t", tempreg, tempreg, breg);
5425 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt, treg,
5426 (int) BFD_RELOC_PCREL_LO16, tempreg);
5427 if (! used_at)
5428 return;
5429 break;
5430 }
5431
252b5132
RH
5432 if (offset_expr.X_op != O_constant
5433 && offset_expr.X_op != O_symbol)
5434 {
5435 as_bad (_("expression too complex"));
5436 offset_expr.X_op = O_constant;
5437 }
5438
5439 /* A constant expression in PIC code can be handled just as it
5440 is in non PIC code. */
5441 if (mips_pic == NO_PIC
5442 || offset_expr.X_op == O_constant)
5443 {
f9419b05
TS
5444 char *p;
5445
252b5132
RH
5446 /* If this is a reference to a GP relative symbol, and there
5447 is no base register, we want
cdf6fd85 5448 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
5449 Otherwise, if there is no base register, we want
5450 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5451 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5452 If we have a constant, we need two instructions anyhow,
5453 so we always use the latter form.
5454
5455 If we have a base register, and this is a reference to a
5456 GP relative symbol, we want
5457 addu $tempreg,$breg,$gp
cdf6fd85 5458 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
5459 Otherwise we want
5460 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5461 addu $tempreg,$tempreg,$breg
5462 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245 5463 With a constant we always use the latter case.
76b3015f 5464
d6bc6245
TS
5465 With 64bit address space and no base register and $at usable,
5466 we want
5467 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5468 lui $at,<sym> (BFD_RELOC_HI16_S)
5469 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5470 dsll32 $tempreg,0
5471 daddu $tempreg,$at
5472 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5473 If we have a base register, we want
5474 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5475 lui $at,<sym> (BFD_RELOC_HI16_S)
5476 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5477 daddu $at,$breg
5478 dsll32 $tempreg,0
5479 daddu $tempreg,$at
5480 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5481
5482 Without $at we can't generate the optimal path for superscalar
5483 processors here since this would require two temporary registers.
5484 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5485 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5486 dsll $tempreg,16
5487 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5488 dsll $tempreg,16
5489 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5490 If we have a base register, we want
5491 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5492 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5493 dsll $tempreg,16
5494 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5495 dsll $tempreg,16
5496 daddu $tempreg,$tempreg,$breg
5497 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6373ee54
CD
5498
5499 If we have 64-bit addresses, as an optimization, for
5500 addresses which are 32-bit constants (e.g. kseg0/kseg1
5501 addresses) we fall back to the 32-bit address generation
78d32a17
MR
5502 mechanism since it is more efficient. Note that due to
5503 the signed offset used by memory operations, the 32-bit
5504 range is shifted down by 32768 here. This code should
6373ee54
CD
5505 probably attempt to generate 64-bit constants more
5506 efficiently in general.
d6bc6245 5507 */
6373ee54
CD
5508 if (HAVE_64BIT_ADDRESSES
5509 && !(offset_expr.X_op == O_constant
78d32a17 5510 && IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000)))
d6bc6245
TS
5511 {
5512 p = NULL;
5513
5514 /* We don't do GP optimization for now because RELAX_ENCODE can't
5515 hold the data for such large chunks. */
5516
460597ba 5517 if (used_at == 0 && ! mips_opts.noat)
d6bc6245
TS
5518 {
5519 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5520 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5521 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5522 AT, (int) BFD_RELOC_HI16_S);
5523 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5524 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5525 if (breg != 0)
2396cfb9
TS
5526 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5527 "d,v,t", AT, AT, breg);
5528 macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
5529 "d,w,<", tempreg, tempreg, 0);
5530 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5531 "d,v,t", tempreg, tempreg, AT);
d6bc6245
TS
5532 macro_build (p, &icnt, &offset_expr, s,
5533 fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5534 used_at = 1;
5535 }
5536 else
5537 {
5538 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5539 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5540 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5541 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
2396cfb9
TS
5542 macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5543 "d,w,<", tempreg, tempreg, 16);
d6bc6245
TS
5544 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5545 tempreg, tempreg, (int) BFD_RELOC_HI16_S);
2396cfb9
TS
5546 macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5547 "d,w,<", tempreg, tempreg, 16);
d6bc6245 5548 if (breg != 0)
2396cfb9
TS
5549 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5550 "d,v,t", tempreg, tempreg, breg);
d6bc6245
TS
5551 macro_build (p, &icnt, &offset_expr, s,
5552 fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5553 }
5554
5555 return;
5556 }
76b3015f 5557
252b5132
RH
5558 if (breg == 0)
5559 {
e7d556df 5560 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
252b5132
RH
5561 || nopic_need_relax (offset_expr.X_add_symbol, 1))
5562 p = NULL;
5563 else
5564 {
5565 frag_grow (20);
5566 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
c9914766
TS
5567 treg, (int) BFD_RELOC_GPREL16,
5568 mips_gp_register);
252b5132
RH
5569 p = frag_var (rs_machine_dependent, 8, 0,
5570 RELAX_ENCODE (4, 8, 0, 4, 0,
5571 (mips_opts.warn_about_macros
5572 || (used_at
5573 && mips_opts.noat))),
956cd1d6 5574 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5575 used_at = 0;
5576 }
5577 macro_build_lui (p, &icnt, &offset_expr, tempreg);
5578 if (p != NULL)
5579 p += 4;
5580 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5581 (int) BFD_RELOC_LO16, tempreg);
5582 }
5583 else
5584 {
e7d556df 5585 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
252b5132
RH
5586 || nopic_need_relax (offset_expr.X_add_symbol, 1))
5587 p = NULL;
5588 else
5589 {
5590 frag_grow (28);
5591 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5592 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 5593 "d,v,t", tempreg, breg, mips_gp_register);
252b5132 5594 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
cdf6fd85 5595 treg, (int) BFD_RELOC_GPREL16, tempreg);
252b5132
RH
5596 p = frag_var (rs_machine_dependent, 12, 0,
5597 RELAX_ENCODE (8, 12, 0, 8, 0, 0),
956cd1d6 5598 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5599 }
5600 macro_build_lui (p, &icnt, &offset_expr, tempreg);
5601 if (p != NULL)
5602 p += 4;
5603 macro_build (p, &icnt, (expressionS *) NULL,
ca4e0257 5604 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5605 "d,v,t", tempreg, tempreg, breg);
5606 if (p != NULL)
5607 p += 4;
5608 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5609 (int) BFD_RELOC_LO16, tempreg);
5610 }
5611 }
5612 else if (mips_pic == SVR4_PIC && ! mips_big_got)
5613 {
f9419b05
TS
5614 char *p;
5615
252b5132
RH
5616 /* If this is a reference to an external symbol, we want
5617 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5618 nop
5619 <op> $treg,0($tempreg)
5620 Otherwise we want
5621 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5622 nop
5623 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5624 <op> $treg,0($tempreg)
5625 If there is a base register, we add it to $tempreg before
5626 the <op>. If there is a constant, we stick it in the
5627 <op> instruction. We don't handle constants larger than
5628 16 bits, because we have no way to load the upper 16 bits
5629 (actually, we could handle them for the subset of cases
5630 in which we are not using $at). */
5631 assert (offset_expr.X_op == O_symbol);
5632 expr1.X_add_number = offset_expr.X_add_number;
5633 offset_expr.X_add_number = 0;
5634 if (expr1.X_add_number < -0x8000
5635 || expr1.X_add_number >= 0x8000)
5636 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5637 frag_grow (20);
5638 macro_build ((char *) NULL, &icnt, &offset_expr,
c9914766
TS
5639 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", tempreg,
5640 (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132 5641 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
bdaaa2e1 5642 p = frag_var (rs_machine_dependent, 4, 0,
252b5132 5643 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
c4e7957c 5644 offset_expr.X_add_symbol, 0, NULL);
252b5132 5645 macro_build (p, &icnt, &offset_expr,
ca4e0257 5646 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
5647 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5648 if (breg != 0)
5649 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5650 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5651 "d,v,t", tempreg, tempreg, breg);
5652 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5653 (int) BFD_RELOC_LO16, tempreg);
5654 }
5655 else if (mips_pic == SVR4_PIC)
5656 {
5657 int gpdel;
f9419b05 5658 char *p;
252b5132
RH
5659
5660 /* If this is a reference to an external symbol, we want
5661 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5662 addu $tempreg,$tempreg,$gp
5663 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5664 <op> $treg,0($tempreg)
5665 Otherwise we want
5666 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5667 nop
5668 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5669 <op> $treg,0($tempreg)
5670 If there is a base register, we add it to $tempreg before
5671 the <op>. If there is a constant, we stick it in the
5672 <op> instruction. We don't handle constants larger than
5673 16 bits, because we have no way to load the upper 16 bits
5674 (actually, we could handle them for the subset of cases
438c16b8
TS
5675 in which we are not using $at).
5676
5677 For NewABI, we want
5678 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
5679 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
5680 <op> $treg,0($tempreg)
5681 */
252b5132
RH
5682 assert (offset_expr.X_op == O_symbol);
5683 expr1.X_add_number = offset_expr.X_add_number;
5684 offset_expr.X_add_number = 0;
5685 if (expr1.X_add_number < -0x8000
5686 || expr1.X_add_number >= 0x8000)
5687 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
438c16b8
TS
5688 if (HAVE_NEWABI)
5689 {
5690 macro_build ((char *) NULL, &icnt, &offset_expr,
5691 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5692 "t,o(b)", tempreg, BFD_RELOC_MIPS_GOT_PAGE,
5693 mips_gp_register);
5694 macro_build ((char *) NULL, &icnt, &offset_expr,
5695 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5696 "t,r,j", tempreg, tempreg,
5697 BFD_RELOC_MIPS_GOT_OFST);
5698 if (breg != 0)
5699 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5700 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5701 "d,v,t", tempreg, tempreg, breg);
5702 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5703 (int) BFD_RELOC_LO16, tempreg);
5704
5705 if (! used_at)
5706 return;
5707
5708 break;
5709 }
f7ea7ef2 5710 if (reg_needs_delay (mips_gp_register))
252b5132
RH
5711 gpdel = 4;
5712 else
5713 gpdel = 0;
5714 frag_grow (36);
5715 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5716 tempreg, (int) BFD_RELOC_MIPS_GOT_HI16);
5717 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5718 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 5719 "d,v,t", tempreg, tempreg, mips_gp_register);
252b5132 5720 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 5721 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
5722 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_LO16,
5723 tempreg);
5724 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5725 RELAX_ENCODE (12, 12 + gpdel, gpdel, 8 + gpdel, 0, 0),
c4e7957c 5726 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5727 if (gpdel > 0)
5728 {
5729 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5730 p += 4;
5731 }
5732 macro_build (p, &icnt, &offset_expr,
ca4e0257 5733 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
c9914766
TS
5734 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16,
5735 mips_gp_register);
252b5132
RH
5736 p += 4;
5737 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5738 p += 4;
5739 macro_build (p, &icnt, &offset_expr,
ca4e0257 5740 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
5741 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5742 if (breg != 0)
5743 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5744 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5745 "d,v,t", tempreg, tempreg, breg);
5746 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5747 (int) BFD_RELOC_LO16, tempreg);
5748 }
5749 else if (mips_pic == EMBEDDED_PIC)
5750 {
5751 /* If there is no base register, we want
cdf6fd85 5752 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
5753 If there is a base register, we want
5754 addu $tempreg,$breg,$gp
cdf6fd85 5755 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
5756 */
5757 assert (offset_expr.X_op == O_symbol);
5758 if (breg == 0)
5759 {
5760 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
c9914766 5761 treg, (int) BFD_RELOC_GPREL16, mips_gp_register);
252b5132
RH
5762 used_at = 0;
5763 }
5764 else
5765 {
5766 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5767 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 5768 "d,v,t", tempreg, breg, mips_gp_register);
252b5132 5769 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
cdf6fd85 5770 treg, (int) BFD_RELOC_GPREL16, tempreg);
252b5132
RH
5771 }
5772 }
5773 else
5774 abort ();
5775
5776 if (! used_at)
5777 return;
5778
5779 break;
5780
5781 case M_LI:
5782 case M_LI_S:
5783 load_register (&icnt, treg, &imm_expr, 0);
5784 return;
5785
5786 case M_DLI:
5787 load_register (&icnt, treg, &imm_expr, 1);
5788 return;
5789
5790 case M_LI_SS:
5791 if (imm_expr.X_op == O_constant)
5792 {
5793 load_register (&icnt, AT, &imm_expr, 0);
5794 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5795 "mtc1", "t,G", AT, treg);
5796 break;
5797 }
5798 else
5799 {
5800 assert (offset_expr.X_op == O_symbol
5801 && strcmp (segment_name (S_GET_SEGMENT
5802 (offset_expr.X_add_symbol)),
5803 ".lit4") == 0
5804 && offset_expr.X_add_number == 0);
5805 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
c9914766 5806 treg, (int) BFD_RELOC_MIPS_LITERAL, mips_gp_register);
252b5132
RH
5807 return;
5808 }
5809
5810 case M_LI_D:
ca4e0257
RS
5811 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
5812 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
5813 order 32 bits of the value and the low order 32 bits are either
5814 zero or in OFFSET_EXPR. */
252b5132
RH
5815 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
5816 {
ca4e0257 5817 if (HAVE_64BIT_GPRS)
252b5132
RH
5818 load_register (&icnt, treg, &imm_expr, 1);
5819 else
5820 {
5821 int hreg, lreg;
5822
5823 if (target_big_endian)
5824 {
5825 hreg = treg;
5826 lreg = treg + 1;
5827 }
5828 else
5829 {
5830 hreg = treg + 1;
5831 lreg = treg;
5832 }
5833
5834 if (hreg <= 31)
5835 load_register (&icnt, hreg, &imm_expr, 0);
5836 if (lreg <= 31)
5837 {
5838 if (offset_expr.X_op == O_absent)
ea1fb5dc 5839 move_register (&icnt, lreg, 0);
252b5132
RH
5840 else
5841 {
5842 assert (offset_expr.X_op == O_constant);
5843 load_register (&icnt, lreg, &offset_expr, 0);
5844 }
5845 }
5846 }
5847 return;
5848 }
5849
5850 /* We know that sym is in the .rdata section. First we get the
5851 upper 16 bits of the address. */
5852 if (mips_pic == NO_PIC)
5853 {
956cd1d6 5854 macro_build_lui (NULL, &icnt, &offset_expr, AT);
252b5132
RH
5855 }
5856 else if (mips_pic == SVR4_PIC)
5857 {
5858 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 5859 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
c9914766
TS
5860 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
5861 mips_gp_register);
252b5132
RH
5862 }
5863 else if (mips_pic == EMBEDDED_PIC)
5864 {
5865 /* For embedded PIC we pick up the entire address off $gp in
5866 a single instruction. */
5867 macro_build ((char *) NULL, &icnt, &offset_expr,
c9914766
TS
5868 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j", AT,
5869 mips_gp_register, (int) BFD_RELOC_GPREL16);
252b5132
RH
5870 offset_expr.X_op = O_constant;
5871 offset_expr.X_add_number = 0;
5872 }
5873 else
5874 abort ();
bdaaa2e1 5875
252b5132 5876 /* Now we load the register(s). */
ca4e0257 5877 if (HAVE_64BIT_GPRS)
252b5132
RH
5878 macro_build ((char *) NULL, &icnt, &offset_expr, "ld", "t,o(b)",
5879 treg, (int) BFD_RELOC_LO16, AT);
5880 else
5881 {
5882 macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
5883 treg, (int) BFD_RELOC_LO16, AT);
f9419b05 5884 if (treg != RA)
252b5132
RH
5885 {
5886 /* FIXME: How in the world do we deal with the possible
5887 overflow here? */
5888 offset_expr.X_add_number += 4;
5889 macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
5890 treg + 1, (int) BFD_RELOC_LO16, AT);
5891 }
5892 }
5893
5894 /* To avoid confusion in tc_gen_reloc, we must ensure that this
5895 does not become a variant frag. */
5896 frag_wane (frag_now);
5897 frag_new (0);
5898
5899 break;
5900
5901 case M_LI_DD:
ca4e0257
RS
5902 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
5903 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
5904 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
5905 the value and the low order 32 bits are either zero or in
5906 OFFSET_EXPR. */
252b5132
RH
5907 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
5908 {
ca4e0257
RS
5909 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_FPRS);
5910 if (HAVE_64BIT_FPRS)
5911 {
5912 assert (HAVE_64BIT_GPRS);
5913 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5914 "dmtc1", "t,S", AT, treg);
5915 }
252b5132
RH
5916 else
5917 {
5918 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5919 "mtc1", "t,G", AT, treg + 1);
5920 if (offset_expr.X_op == O_absent)
5921 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5922 "mtc1", "t,G", 0, treg);
5923 else
5924 {
5925 assert (offset_expr.X_op == O_constant);
5926 load_register (&icnt, AT, &offset_expr, 0);
5927 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5928 "mtc1", "t,G", AT, treg);
5929 }
5930 }
5931 break;
5932 }
5933
5934 assert (offset_expr.X_op == O_symbol
5935 && offset_expr.X_add_number == 0);
5936 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
5937 if (strcmp (s, ".lit8") == 0)
5938 {
e7af610e 5939 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
5940 {
5941 macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
c9914766
TS
5942 "T,o(b)", treg, (int) BFD_RELOC_MIPS_LITERAL,
5943 mips_gp_register);
252b5132
RH
5944 return;
5945 }
c9914766 5946 breg = mips_gp_register;
252b5132
RH
5947 r = BFD_RELOC_MIPS_LITERAL;
5948 goto dob;
5949 }
5950 else
5951 {
5952 assert (strcmp (s, RDATA_SECTION_NAME) == 0);
5953 if (mips_pic == SVR4_PIC)
5954 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 5955 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
c9914766
TS
5956 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
5957 mips_gp_register);
252b5132
RH
5958 else
5959 {
5960 /* FIXME: This won't work for a 64 bit address. */
956cd1d6 5961 macro_build_lui (NULL, &icnt, &offset_expr, AT);
252b5132 5962 }
bdaaa2e1 5963
e7af610e 5964 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
5965 {
5966 macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
5967 "T,o(b)", treg, (int) BFD_RELOC_LO16, AT);
5968
5969 /* To avoid confusion in tc_gen_reloc, we must ensure
5970 that this does not become a variant frag. */
5971 frag_wane (frag_now);
5972 frag_new (0);
5973
5974 break;
5975 }
5976 breg = AT;
5977 r = BFD_RELOC_LO16;
5978 goto dob;
5979 }
5980
5981 case M_L_DOB:
ec68c924 5982 if (mips_arch == CPU_R4650)
252b5132
RH
5983 {
5984 as_bad (_("opcode not supported on this processor"));
5985 return;
5986 }
5987 /* Even on a big endian machine $fn comes before $fn+1. We have
5988 to adjust when loading from memory. */
5989 r = BFD_RELOC_LO16;
5990 dob:
e7af610e 5991 assert (mips_opts.isa == ISA_MIPS1);
252b5132
RH
5992 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
5993 target_big_endian ? treg + 1 : treg,
5994 (int) r, breg);
5995 /* FIXME: A possible overflow which I don't know how to deal
5996 with. */
5997 offset_expr.X_add_number += 4;
5998 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
5999 target_big_endian ? treg : treg + 1,
6000 (int) r, breg);
6001
6002 /* To avoid confusion in tc_gen_reloc, we must ensure that this
6003 does not become a variant frag. */
6004 frag_wane (frag_now);
6005 frag_new (0);
6006
6007 if (breg != AT)
6008 return;
6009 break;
6010
6011 case M_L_DAB:
6012 /*
6013 * The MIPS assembler seems to check for X_add_number not
6014 * being double aligned and generating:
6015 * lui at,%hi(foo+1)
6016 * addu at,at,v1
6017 * addiu at,at,%lo(foo+1)
6018 * lwc1 f2,0(at)
6019 * lwc1 f3,4(at)
6020 * But, the resulting address is the same after relocation so why
6021 * generate the extra instruction?
6022 */
ec68c924 6023 if (mips_arch == CPU_R4650)
252b5132
RH
6024 {
6025 as_bad (_("opcode not supported on this processor"));
6026 return;
6027 }
bdaaa2e1 6028 /* Itbl support may require additional care here. */
252b5132 6029 coproc = 1;
e7af610e 6030 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
6031 {
6032 s = "ldc1";
6033 goto ld;
6034 }
6035
6036 s = "lwc1";
6037 fmt = "T,o(b)";
6038 goto ldd_std;
6039
6040 case M_S_DAB:
ec68c924 6041 if (mips_arch == CPU_R4650)
252b5132
RH
6042 {
6043 as_bad (_("opcode not supported on this processor"));
6044 return;
6045 }
6046
e7af610e 6047 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
6048 {
6049 s = "sdc1";
6050 goto st;
6051 }
6052
6053 s = "swc1";
6054 fmt = "T,o(b)";
bdaaa2e1 6055 /* Itbl support may require additional care here. */
252b5132
RH
6056 coproc = 1;
6057 goto ldd_std;
6058
6059 case M_LD_AB:
ca4e0257 6060 if (HAVE_64BIT_GPRS)
252b5132
RH
6061 {
6062 s = "ld";
6063 goto ld;
6064 }
6065
6066 s = "lw";
6067 fmt = "t,o(b)";
6068 goto ldd_std;
6069
6070 case M_SD_AB:
ca4e0257 6071 if (HAVE_64BIT_GPRS)
252b5132
RH
6072 {
6073 s = "sd";
6074 goto st;
6075 }
6076
6077 s = "sw";
6078 fmt = "t,o(b)";
6079
6080 ldd_std:
afdbd6d0
CD
6081 /* We do _not_ bother to allow embedded PIC (symbol-local_symbol)
6082 loads for the case of doing a pair of loads to simulate an 'ld'.
6083 This is not currently done by the compiler, and assembly coders
6084 writing embedded-pic code can cope. */
6085
252b5132
RH
6086 if (offset_expr.X_op != O_symbol
6087 && offset_expr.X_op != O_constant)
6088 {
6089 as_bad (_("expression too complex"));
6090 offset_expr.X_op = O_constant;
6091 }
6092
6093 /* Even on a big endian machine $fn comes before $fn+1. We have
6094 to adjust when loading from memory. We set coproc if we must
6095 load $fn+1 first. */
bdaaa2e1 6096 /* Itbl support may require additional care here. */
252b5132
RH
6097 if (! target_big_endian)
6098 coproc = 0;
6099
6100 if (mips_pic == NO_PIC
6101 || offset_expr.X_op == O_constant)
6102 {
f9419b05
TS
6103 char *p;
6104
252b5132 6105 /* If this is a reference to a GP relative symbol, we want
cdf6fd85
TS
6106 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6107 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
6108 If we have a base register, we use this
6109 addu $at,$breg,$gp
cdf6fd85
TS
6110 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6111 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
6112 If this is not a GP relative symbol, we want
6113 lui $at,<sym> (BFD_RELOC_HI16_S)
6114 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6115 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6116 If there is a base register, we add it to $at after the
6117 lui instruction. If there is a constant, we always use
6118 the last case. */
e7d556df 6119 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
252b5132
RH
6120 || nopic_need_relax (offset_expr.X_add_symbol, 1))
6121 {
6122 p = NULL;
6123 used_at = 1;
6124 }
6125 else
6126 {
6127 int off;
6128
6129 if (breg == 0)
6130 {
6131 frag_grow (28);
c9914766 6132 tempreg = mips_gp_register;
252b5132
RH
6133 off = 0;
6134 used_at = 0;
6135 }
6136 else
6137 {
6138 frag_grow (36);
6139 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6140 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 6141 "d,v,t", AT, breg, mips_gp_register);
252b5132
RH
6142 tempreg = AT;
6143 off = 4;
6144 used_at = 1;
6145 }
6146
beae10d5 6147 /* Itbl support may require additional care here. */
252b5132
RH
6148 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6149 coproc ? treg + 1 : treg,
cdf6fd85 6150 (int) BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6151 offset_expr.X_add_number += 4;
6152
6153 /* Set mips_optimize to 2 to avoid inserting an
6154 undesired nop. */
6155 hold_mips_optimize = mips_optimize;
6156 mips_optimize = 2;
beae10d5 6157 /* Itbl support may require additional care here. */
252b5132
RH
6158 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6159 coproc ? treg : treg + 1,
cdf6fd85 6160 (int) BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6161 mips_optimize = hold_mips_optimize;
6162
6163 p = frag_var (rs_machine_dependent, 12 + off, 0,
6164 RELAX_ENCODE (8 + off, 12 + off, 0, 4 + off, 1,
6165 used_at && mips_opts.noat),
956cd1d6 6166 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
6167
6168 /* We just generated two relocs. When tc_gen_reloc
6169 handles this case, it will skip the first reloc and
6170 handle the second. The second reloc already has an
6171 extra addend of 4, which we added above. We must
6172 subtract it out, and then subtract another 4 to make
6173 the first reloc come out right. The second reloc
6174 will come out right because we are going to add 4 to
6175 offset_expr when we build its instruction below.
6176
6177 If we have a symbol, then we don't want to include
6178 the offset, because it will wind up being included
6179 when we generate the reloc. */
6180
6181 if (offset_expr.X_op == O_constant)
6182 offset_expr.X_add_number -= 8;
6183 else
6184 {
6185 offset_expr.X_add_number = -4;
6186 offset_expr.X_op = O_constant;
6187 }
6188 }
6189 macro_build_lui (p, &icnt, &offset_expr, AT);
6190 if (p != NULL)
6191 p += 4;
6192 if (breg != 0)
6193 {
6194 macro_build (p, &icnt, (expressionS *) NULL,
ca4e0257 6195 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
6196 "d,v,t", AT, breg, AT);
6197 if (p != NULL)
6198 p += 4;
6199 }
beae10d5 6200 /* Itbl support may require additional care here. */
252b5132
RH
6201 macro_build (p, &icnt, &offset_expr, s, fmt,
6202 coproc ? treg + 1 : treg,
6203 (int) BFD_RELOC_LO16, AT);
6204 if (p != NULL)
6205 p += 4;
6206 /* FIXME: How do we handle overflow here? */
6207 offset_expr.X_add_number += 4;
beae10d5 6208 /* Itbl support may require additional care here. */
252b5132
RH
6209 macro_build (p, &icnt, &offset_expr, s, fmt,
6210 coproc ? treg : treg + 1,
6211 (int) BFD_RELOC_LO16, AT);
bdaaa2e1 6212 }
252b5132
RH
6213 else if (mips_pic == SVR4_PIC && ! mips_big_got)
6214 {
6215 int off;
6216
6217 /* If this is a reference to an external symbol, we want
6218 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6219 nop
6220 <op> $treg,0($at)
6221 <op> $treg+1,4($at)
6222 Otherwise we want
6223 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6224 nop
6225 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6226 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6227 If there is a base register we add it to $at before the
6228 lwc1 instructions. If there is a constant we include it
6229 in the lwc1 instructions. */
6230 used_at = 1;
6231 expr1.X_add_number = offset_expr.X_add_number;
6232 offset_expr.X_add_number = 0;
6233 if (expr1.X_add_number < -0x8000
6234 || expr1.X_add_number >= 0x8000 - 4)
6235 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6236 if (breg == 0)
6237 off = 0;
6238 else
6239 off = 4;
6240 frag_grow (24 + off);
6241 macro_build ((char *) NULL, &icnt, &offset_expr,
c9914766
TS
6242 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", AT,
6243 (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132
RH
6244 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6245 if (breg != 0)
6246 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6247 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132 6248 "d,v,t", AT, breg, AT);
beae10d5 6249 /* Itbl support may require additional care here. */
252b5132
RH
6250 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6251 coproc ? treg + 1 : treg,
6252 (int) BFD_RELOC_LO16, AT);
6253 expr1.X_add_number += 4;
6254
6255 /* Set mips_optimize to 2 to avoid inserting an undesired
6256 nop. */
6257 hold_mips_optimize = mips_optimize;
6258 mips_optimize = 2;
beae10d5 6259 /* Itbl support may require additional care here. */
252b5132
RH
6260 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6261 coproc ? treg : treg + 1,
6262 (int) BFD_RELOC_LO16, AT);
6263 mips_optimize = hold_mips_optimize;
6264
6265 (void) frag_var (rs_machine_dependent, 0, 0,
6266 RELAX_ENCODE (0, 0, -16 - off, -8, 1, 0),
c4e7957c 6267 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
6268 }
6269 else if (mips_pic == SVR4_PIC)
6270 {
6271 int gpdel, off;
f9419b05 6272 char *p;
252b5132
RH
6273
6274 /* If this is a reference to an external symbol, we want
6275 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6276 addu $at,$at,$gp
6277 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
6278 nop
6279 <op> $treg,0($at)
6280 <op> $treg+1,4($at)
6281 Otherwise we want
6282 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6283 nop
6284 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6285 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6286 If there is a base register we add it to $at before the
6287 lwc1 instructions. If there is a constant we include it
6288 in the lwc1 instructions. */
6289 used_at = 1;
6290 expr1.X_add_number = offset_expr.X_add_number;
6291 offset_expr.X_add_number = 0;
6292 if (expr1.X_add_number < -0x8000
6293 || expr1.X_add_number >= 0x8000 - 4)
6294 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
c9914766 6295 if (reg_needs_delay (mips_gp_register))
252b5132
RH
6296 gpdel = 4;
6297 else
6298 gpdel = 0;
6299 if (breg == 0)
6300 off = 0;
6301 else
6302 off = 4;
6303 frag_grow (56);
6304 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
6305 AT, (int) BFD_RELOC_MIPS_GOT_HI16);
6306 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6307 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 6308 "d,v,t", AT, AT, mips_gp_register);
252b5132 6309 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 6310 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
6311 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT_LO16, AT);
6312 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6313 if (breg != 0)
6314 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6315 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132 6316 "d,v,t", AT, breg, AT);
beae10d5 6317 /* Itbl support may require additional care here. */
252b5132
RH
6318 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6319 coproc ? treg + 1 : treg,
6320 (int) BFD_RELOC_LO16, AT);
6321 expr1.X_add_number += 4;
6322
6323 /* Set mips_optimize to 2 to avoid inserting an undesired
6324 nop. */
6325 hold_mips_optimize = mips_optimize;
6326 mips_optimize = 2;
beae10d5 6327 /* Itbl support may require additional care here. */
252b5132
RH
6328 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6329 coproc ? treg : treg + 1,
6330 (int) BFD_RELOC_LO16, AT);
6331 mips_optimize = hold_mips_optimize;
6332 expr1.X_add_number -= 4;
6333
6334 p = frag_var (rs_machine_dependent, 16 + gpdel + off, 0,
6335 RELAX_ENCODE (24 + off, 16 + gpdel + off, gpdel,
6336 8 + gpdel + off, 1, 0),
c4e7957c 6337 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
6338 if (gpdel > 0)
6339 {
6340 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6341 p += 4;
6342 }
6343 macro_build (p, &icnt, &offset_expr,
ca4e0257 6344 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
c9914766
TS
6345 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6346 mips_gp_register);
252b5132
RH
6347 p += 4;
6348 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6349 p += 4;
6350 if (breg != 0)
6351 {
6352 macro_build (p, &icnt, (expressionS *) NULL,
ca4e0257 6353 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
6354 "d,v,t", AT, breg, AT);
6355 p += 4;
6356 }
beae10d5 6357 /* Itbl support may require additional care here. */
252b5132
RH
6358 macro_build (p, &icnt, &expr1, s, fmt,
6359 coproc ? treg + 1 : treg,
6360 (int) BFD_RELOC_LO16, AT);
6361 p += 4;
6362 expr1.X_add_number += 4;
6363
6364 /* Set mips_optimize to 2 to avoid inserting an undesired
6365 nop. */
6366 hold_mips_optimize = mips_optimize;
6367 mips_optimize = 2;
beae10d5 6368 /* Itbl support may require additional care here. */
252b5132
RH
6369 macro_build (p, &icnt, &expr1, s, fmt,
6370 coproc ? treg : treg + 1,
6371 (int) BFD_RELOC_LO16, AT);
6372 mips_optimize = hold_mips_optimize;
6373 }
6374 else if (mips_pic == EMBEDDED_PIC)
6375 {
6376 /* If there is no base register, we use
cdf6fd85
TS
6377 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6378 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
6379 If we have a base register, we use
6380 addu $at,$breg,$gp
cdf6fd85
TS
6381 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6382 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
6383 */
6384 if (breg == 0)
6385 {
c9914766 6386 tempreg = mips_gp_register;
252b5132
RH
6387 used_at = 0;
6388 }
6389 else
6390 {
6391 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6392 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 6393 "d,v,t", AT, breg, mips_gp_register);
252b5132
RH
6394 tempreg = AT;
6395 used_at = 1;
6396 }
6397
beae10d5 6398 /* Itbl support may require additional care here. */
252b5132
RH
6399 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6400 coproc ? treg + 1 : treg,
cdf6fd85 6401 (int) BFD_RELOC_GPREL16, tempreg);
252b5132 6402 offset_expr.X_add_number += 4;
beae10d5 6403 /* Itbl support may require additional care here. */
252b5132
RH
6404 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6405 coproc ? treg : treg + 1,
cdf6fd85 6406 (int) BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6407 }
6408 else
6409 abort ();
6410
6411 if (! used_at)
6412 return;
6413
6414 break;
6415
6416 case M_LD_OB:
6417 s = "lw";
6418 goto sd_ob;
6419 case M_SD_OB:
6420 s = "sw";
6421 sd_ob:
ca4e0257 6422 assert (HAVE_32BIT_ADDRESSES);
252b5132
RH
6423 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
6424 (int) BFD_RELOC_LO16, breg);
6425 offset_expr.X_add_number += 4;
6426 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg + 1,
6427 (int) BFD_RELOC_LO16, breg);
6428 return;
6429
6430 /* New code added to support COPZ instructions.
6431 This code builds table entries out of the macros in mip_opcodes.
6432 R4000 uses interlocks to handle coproc delays.
6433 Other chips (like the R3000) require nops to be inserted for delays.
6434
f72c8c98 6435 FIXME: Currently, we require that the user handle delays.
252b5132
RH
6436 In order to fill delay slots for non-interlocked chips,
6437 we must have a way to specify delays based on the coprocessor.
6438 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
6439 What are the side-effects of the cop instruction?
6440 What cache support might we have and what are its effects?
6441 Both coprocessor & memory require delays. how long???
bdaaa2e1 6442 What registers are read/set/modified?
252b5132
RH
6443
6444 If an itbl is provided to interpret cop instructions,
bdaaa2e1 6445 this knowledge can be encoded in the itbl spec. */
252b5132
RH
6446
6447 case M_COP0:
6448 s = "c0";
6449 goto copz;
6450 case M_COP1:
6451 s = "c1";
6452 goto copz;
6453 case M_COP2:
6454 s = "c2";
6455 goto copz;
6456 case M_COP3:
6457 s = "c3";
6458 copz:
6459 /* For now we just do C (same as Cz). The parameter will be
6460 stored in insn_opcode by mips_ip. */
6461 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "C",
6462 ip->insn_opcode);
6463 return;
6464
ea1fb5dc
RS
6465 case M_MOVE:
6466 move_register (&icnt, dreg, sreg);
6467 return;
6468
252b5132
RH
6469#ifdef LOSING_COMPILER
6470 default:
6471 /* Try and see if this is a new itbl instruction.
6472 This code builds table entries out of the macros in mip_opcodes.
6473 FIXME: For now we just assemble the expression and pass it's
6474 value along as a 32-bit immediate.
bdaaa2e1 6475 We may want to have the assembler assemble this value,
252b5132
RH
6476 so that we gain the assembler's knowledge of delay slots,
6477 symbols, etc.
6478 Would it be more efficient to use mask (id) here? */
bdaaa2e1 6479 if (itbl_have_entries
252b5132 6480 && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
beae10d5 6481 {
252b5132
RH
6482 s = ip->insn_mo->name;
6483 s2 = "cop3";
6484 coproc = ITBL_DECODE_PNUM (immed_expr);;
6485 macro_build ((char *) NULL, &icnt, &immed_expr, s, "C");
6486 return;
beae10d5 6487 }
252b5132
RH
6488 macro2 (ip);
6489 return;
6490 }
6491 if (mips_opts.noat)
6492 as_warn (_("Macro used $at after \".set noat\""));
6493}
bdaaa2e1 6494
252b5132
RH
6495static void
6496macro2 (ip)
6497 struct mips_cl_insn *ip;
6498{
6499 register int treg, sreg, dreg, breg;
6500 int tempreg;
6501 int mask;
6502 int icnt = 0;
6503 int used_at;
6504 expressionS expr1;
6505 const char *s;
6506 const char *s2;
6507 const char *fmt;
6508 int likely = 0;
6509 int dbl = 0;
6510 int coproc = 0;
6511 int lr = 0;
6512 int imm = 0;
6513 int off;
6514 offsetT maxnum;
6515 bfd_reloc_code_real_type r;
6516 char *p;
bdaaa2e1 6517
252b5132
RH
6518 treg = (ip->insn_opcode >> 16) & 0x1f;
6519 dreg = (ip->insn_opcode >> 11) & 0x1f;
6520 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
6521 mask = ip->insn_mo->mask;
bdaaa2e1 6522
252b5132
RH
6523 expr1.X_op = O_constant;
6524 expr1.X_op_symbol = NULL;
6525 expr1.X_add_symbol = NULL;
6526 expr1.X_add_number = 1;
bdaaa2e1 6527
252b5132
RH
6528 switch (mask)
6529 {
6530#endif /* LOSING_COMPILER */
6531
6532 case M_DMUL:
6533 dbl = 1;
6534 case M_MUL:
2396cfb9
TS
6535 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6536 dbl ? "dmultu" : "multu", "s,t", sreg, treg);
6537 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6538 dreg);
252b5132
RH
6539 return;
6540
6541 case M_DMUL_I:
6542 dbl = 1;
6543 case M_MUL_I:
6544 /* The MIPS assembler some times generates shifts and adds. I'm
6545 not trying to be that fancy. GCC should do this for us
6546 anyway. */
6547 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9 6548 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 6549 dbl ? "dmult" : "mult", "s,t", sreg, AT);
2396cfb9
TS
6550 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6551 dreg);
252b5132
RH
6552 break;
6553
6554 case M_DMULO_I:
6555 dbl = 1;
6556 case M_MULO_I:
6557 imm = 1;
6558 goto do_mulo;
6559
6560 case M_DMULO:
6561 dbl = 1;
6562 case M_MULO:
6563 do_mulo:
6564 mips_emit_delays (true);
6565 ++mips_opts.noreorder;
6566 mips_any_noreorder = 1;
6567 if (imm)
6568 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9 6569 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 6570 dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
2396cfb9
TS
6571 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6572 dreg);
6573 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
f9419b05 6574 dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, RA);
2396cfb9
TS
6575 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6576 AT);
252b5132 6577 if (mips_trap)
9bd7d936
MR
6578 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne",
6579 "s,t,q", dreg, AT, 6);
252b5132
RH
6580 else
6581 {
6582 expr1.X_add_number = 8;
2396cfb9
TS
6583 macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", dreg,
6584 AT);
6585 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6586 0);
6587 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6588 "c", 6);
252b5132
RH
6589 }
6590 --mips_opts.noreorder;
2396cfb9 6591 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d", dreg);
252b5132
RH
6592 break;
6593
6594 case M_DMULOU_I:
6595 dbl = 1;
6596 case M_MULOU_I:
6597 imm = 1;
6598 goto do_mulou;
6599
6600 case M_DMULOU:
6601 dbl = 1;
6602 case M_MULOU:
6603 do_mulou:
6604 mips_emit_delays (true);
6605 ++mips_opts.noreorder;
6606 mips_any_noreorder = 1;
6607 if (imm)
6608 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9 6609 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
252b5132
RH
6610 dbl ? "dmultu" : "multu",
6611 "s,t", sreg, imm ? AT : treg);
2396cfb9
TS
6612 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6613 AT);
6614 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6615 dreg);
252b5132 6616 if (mips_trap)
9bd7d936
MR
6617 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne",
6618 "s,t,q", AT, 0, 6);
252b5132
RH
6619 else
6620 {
6621 expr1.X_add_number = 8;
6622 macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", AT, 0);
2396cfb9
TS
6623 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6624 0);
6625 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6626 "c", 6);
252b5132
RH
6627 }
6628 --mips_opts.noreorder;
6629 break;
6630
771c7ce4
TS
6631 case M_DROL:
6632 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
6633 "d,v,t", AT, 0, treg);
6634 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
6635 "d,t,s", AT, sreg, AT);
6636 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
6637 "d,t,s", dreg, sreg, treg);
6638 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6639 "d,v,t", dreg, dreg, AT);
6640 break;
6641
252b5132 6642 case M_ROL:
2396cfb9
TS
6643 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
6644 "d,v,t", AT, 0, treg);
6645 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
6646 "d,t,s", AT, sreg, AT);
6647 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
6648 "d,t,s", dreg, sreg, treg);
6649 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6650 "d,v,t", dreg, dreg, AT);
252b5132
RH
6651 break;
6652
771c7ce4
TS
6653 case M_DROL_I:
6654 {
6655 unsigned int rot;
6656 char *l, *r;
6657
6658 if (imm_expr.X_op != O_constant)
6659 as_bad (_("rotate count too large"));
6660 rot = imm_expr.X_add_number & 0x3f;
6661 if (! rot)
6662 break;
6663 l = (rot < 0x20) ? "dsll" : "dsll32";
6664 r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
6665 rot &= 0x1f;
6666 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
6667 "d,w,<", AT, sreg, rot);
6668 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
6669 "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6670 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6671 "d,v,t", dreg, dreg, AT);
6672 }
6673 break;
6674
252b5132 6675 case M_ROL_I:
771c7ce4
TS
6676 {
6677 unsigned int rot;
6678
6679 if (imm_expr.X_op != O_constant)
6680 as_bad (_("rotate count too large"));
6681 rot = imm_expr.X_add_number & 0x1f;
6682 if (! rot)
6683 break;
6684 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
6685 "d,w,<", AT, sreg, rot);
6686 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
6687 "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6688 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6689 "d,v,t", dreg, dreg, AT);
6690 }
6691 break;
6692
6693 case M_DROR:
6694 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
6695 "d,v,t", AT, 0, treg);
6696 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
6697 "d,t,s", AT, sreg, AT);
6698 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
6699 "d,t,s", dreg, sreg, treg);
6700 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6701 "d,v,t", dreg, dreg, AT);
252b5132
RH
6702 break;
6703
6704 case M_ROR:
2396cfb9
TS
6705 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
6706 "d,v,t", AT, 0, treg);
6707 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
6708 "d,t,s", AT, sreg, AT);
6709 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
6710 "d,t,s", dreg, sreg, treg);
6711 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6712 "d,v,t", dreg, dreg, AT);
252b5132
RH
6713 break;
6714
771c7ce4
TS
6715 case M_DROR_I:
6716 {
6717 unsigned int rot;
6718 char *l, *r;
6719
6720 if (imm_expr.X_op != O_constant)
6721 as_bad (_("rotate count too large"));
6722 rot = imm_expr.X_add_number & 0x3f;
6723 if (! rot)
6724 break;
6725 r = (rot < 0x20) ? "dsrl" : "dsrl32";
6726 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
6727 rot &= 0x1f;
6728 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
6729 "d,w,<", AT, sreg, rot);
6730 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
6731 "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6732 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6733 "d,v,t", dreg, dreg, AT);
6734 }
6735 break;
6736
252b5132 6737 case M_ROR_I:
771c7ce4
TS
6738 {
6739 unsigned int rot;
6740
6741 if (imm_expr.X_op != O_constant)
6742 as_bad (_("rotate count too large"));
6743 rot = imm_expr.X_add_number & 0x1f;
6744 if (! rot)
6745 break;
6746 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
6747 "d,w,<", AT, sreg, rot);
6748 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
6749 "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6750 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6751 "d,v,t", dreg, dreg, AT);
6752 }
252b5132
RH
6753 break;
6754
6755 case M_S_DOB:
ec68c924 6756 if (mips_arch == CPU_R4650)
252b5132
RH
6757 {
6758 as_bad (_("opcode not supported on this processor"));
6759 return;
6760 }
e7af610e 6761 assert (mips_opts.isa == ISA_MIPS1);
252b5132
RH
6762 /* Even on a big endian machine $fn comes before $fn+1. We have
6763 to adjust when storing to memory. */
6764 macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
6765 target_big_endian ? treg + 1 : treg,
6766 (int) BFD_RELOC_LO16, breg);
6767 offset_expr.X_add_number += 4;
6768 macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
6769 target_big_endian ? treg : treg + 1,
6770 (int) BFD_RELOC_LO16, breg);
6771 return;
6772
6773 case M_SEQ:
6774 if (sreg == 0)
6775 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6776 treg, (int) BFD_RELOC_LO16);
6777 else if (treg == 0)
6778 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6779 sreg, (int) BFD_RELOC_LO16);
6780 else
6781 {
2396cfb9
TS
6782 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6783 "d,v,t", dreg, sreg, treg);
252b5132
RH
6784 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6785 dreg, (int) BFD_RELOC_LO16);
6786 }
6787 return;
6788
6789 case M_SEQ_I:
6790 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6791 {
6792 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6793 sreg, (int) BFD_RELOC_LO16);
6794 return;
6795 }
6796 if (sreg == 0)
6797 {
6798 as_warn (_("Instruction %s: result is always false"),
6799 ip->insn_mo->name);
ea1fb5dc 6800 move_register (&icnt, dreg, 0);
252b5132
RH
6801 return;
6802 }
6803 if (imm_expr.X_op == O_constant
6804 && imm_expr.X_add_number >= 0
6805 && imm_expr.X_add_number < 0x10000)
6806 {
6807 macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg,
6808 sreg, (int) BFD_RELOC_LO16);
6809 used_at = 0;
6810 }
6811 else if (imm_expr.X_op == O_constant
6812 && imm_expr.X_add_number > -0x8000
6813 && imm_expr.X_add_number < 0)
6814 {
6815 imm_expr.X_add_number = -imm_expr.X_add_number;
6816 macro_build ((char *) NULL, &icnt, &imm_expr,
ca4e0257 6817 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
252b5132
RH
6818 "t,r,j", dreg, sreg,
6819 (int) BFD_RELOC_LO16);
6820 used_at = 0;
6821 }
6822 else
6823 {
4d34fb5f 6824 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
6825 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6826 "d,v,t", dreg, sreg, AT);
252b5132
RH
6827 used_at = 1;
6828 }
6829 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
6830 (int) BFD_RELOC_LO16);
6831 if (used_at)
6832 break;
6833 return;
6834
6835 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
6836 s = "slt";
6837 goto sge;
6838 case M_SGEU:
6839 s = "sltu";
6840 sge:
2396cfb9
TS
6841 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6842 dreg, sreg, treg);
252b5132
RH
6843 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6844 (int) BFD_RELOC_LO16);
6845 return;
6846
6847 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
6848 case M_SGEU_I:
6849 if (imm_expr.X_op == O_constant
6850 && imm_expr.X_add_number >= -0x8000
6851 && imm_expr.X_add_number < 0x8000)
6852 {
6853 macro_build ((char *) NULL, &icnt, &imm_expr,
6854 mask == M_SGE_I ? "slti" : "sltiu",
6855 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6856 used_at = 0;
6857 }
6858 else
6859 {
4d34fb5f 6860 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
6861 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6862 mask == M_SGE_I ? "slt" : "sltu", "d,v,t", dreg, sreg,
6863 AT);
252b5132
RH
6864 used_at = 1;
6865 }
6866 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6867 (int) BFD_RELOC_LO16);
6868 if (used_at)
6869 break;
6870 return;
6871
6872 case M_SGT: /* sreg > treg <==> treg < sreg */
6873 s = "slt";
6874 goto sgt;
6875 case M_SGTU:
6876 s = "sltu";
6877 sgt:
2396cfb9
TS
6878 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6879 dreg, treg, sreg);
252b5132
RH
6880 return;
6881
6882 case M_SGT_I: /* sreg > I <==> I < sreg */
6883 s = "slt";
6884 goto sgti;
6885 case M_SGTU_I:
6886 s = "sltu";
6887 sgti:
4d34fb5f 6888 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
6889 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6890 dreg, AT, sreg);
252b5132
RH
6891 break;
6892
2396cfb9 6893 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
252b5132
RH
6894 s = "slt";
6895 goto sle;
6896 case M_SLEU:
6897 s = "sltu";
6898 sle:
2396cfb9
TS
6899 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6900 dreg, treg, sreg);
252b5132
RH
6901 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6902 (int) BFD_RELOC_LO16);
6903 return;
6904
2396cfb9 6905 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
252b5132
RH
6906 s = "slt";
6907 goto slei;
6908 case M_SLEU_I:
6909 s = "sltu";
6910 slei:
4d34fb5f 6911 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
6912 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6913 dreg, AT, sreg);
252b5132
RH
6914 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6915 (int) BFD_RELOC_LO16);
6916 break;
6917
6918 case M_SLT_I:
6919 if (imm_expr.X_op == O_constant
6920 && imm_expr.X_add_number >= -0x8000
6921 && imm_expr.X_add_number < 0x8000)
6922 {
6923 macro_build ((char *) NULL, &icnt, &imm_expr, "slti", "t,r,j",
6924 dreg, sreg, (int) BFD_RELOC_LO16);
6925 return;
6926 }
4d34fb5f 6927 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
6928 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
6929 dreg, sreg, AT);
252b5132
RH
6930 break;
6931
6932 case M_SLTU_I:
6933 if (imm_expr.X_op == O_constant
6934 && imm_expr.X_add_number >= -0x8000
6935 && imm_expr.X_add_number < 0x8000)
6936 {
6937 macro_build ((char *) NULL, &icnt, &imm_expr, "sltiu", "t,r,j",
6938 dreg, sreg, (int) BFD_RELOC_LO16);
6939 return;
6940 }
4d34fb5f 6941 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
6942 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6943 "d,v,t", dreg, sreg, AT);
252b5132
RH
6944 break;
6945
6946 case M_SNE:
6947 if (sreg == 0)
2396cfb9
TS
6948 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6949 "d,v,t", dreg, 0, treg);
252b5132 6950 else if (treg == 0)
2396cfb9
TS
6951 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6952 "d,v,t", dreg, 0, sreg);
252b5132
RH
6953 else
6954 {
2396cfb9
TS
6955 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6956 "d,v,t", dreg, sreg, treg);
6957 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6958 "d,v,t", dreg, 0, dreg);
252b5132
RH
6959 }
6960 return;
6961
6962 case M_SNE_I:
6963 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6964 {
2396cfb9
TS
6965 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6966 "d,v,t", dreg, 0, sreg);
252b5132
RH
6967 return;
6968 }
6969 if (sreg == 0)
6970 {
6971 as_warn (_("Instruction %s: result is always true"),
6972 ip->insn_mo->name);
6973 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 6974 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
252b5132
RH
6975 "t,r,j", dreg, 0, (int) BFD_RELOC_LO16);
6976 return;
6977 }
6978 if (imm_expr.X_op == O_constant
6979 && imm_expr.X_add_number >= 0
6980 && imm_expr.X_add_number < 0x10000)
6981 {
6982 macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i",
6983 dreg, sreg, (int) BFD_RELOC_LO16);
6984 used_at = 0;
6985 }
6986 else if (imm_expr.X_op == O_constant
6987 && imm_expr.X_add_number > -0x8000
6988 && imm_expr.X_add_number < 0)
6989 {
6990 imm_expr.X_add_number = -imm_expr.X_add_number;
6991 macro_build ((char *) NULL, &icnt, &imm_expr,
ca4e0257 6992 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
252b5132
RH
6993 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6994 used_at = 0;
6995 }
6996 else
6997 {
4d34fb5f 6998 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
6999 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7000 "d,v,t", dreg, sreg, AT);
252b5132
RH
7001 used_at = 1;
7002 }
2396cfb9
TS
7003 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7004 "d,v,t", dreg, 0, dreg);
252b5132
RH
7005 if (used_at)
7006 break;
7007 return;
7008
7009 case M_DSUB_I:
7010 dbl = 1;
7011 case M_SUB_I:
7012 if (imm_expr.X_op == O_constant
7013 && imm_expr.X_add_number > -0x8000
7014 && imm_expr.X_add_number <= 0x8000)
7015 {
7016 imm_expr.X_add_number = -imm_expr.X_add_number;
7017 macro_build ((char *) NULL, &icnt, &imm_expr,
7018 dbl ? "daddi" : "addi",
7019 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7020 return;
7021 }
7022 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9 7023 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 7024 dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
252b5132
RH
7025 break;
7026
7027 case M_DSUBU_I:
7028 dbl = 1;
7029 case M_SUBU_I:
7030 if (imm_expr.X_op == O_constant
7031 && imm_expr.X_add_number > -0x8000
7032 && imm_expr.X_add_number <= 0x8000)
7033 {
7034 imm_expr.X_add_number = -imm_expr.X_add_number;
7035 macro_build ((char *) NULL, &icnt, &imm_expr,
7036 dbl ? "daddiu" : "addiu",
7037 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7038 return;
7039 }
7040 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9 7041 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 7042 dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
252b5132
RH
7043 break;
7044
7045 case M_TEQ_I:
7046 s = "teq";
7047 goto trap;
7048 case M_TGE_I:
7049 s = "tge";
7050 goto trap;
7051 case M_TGEU_I:
7052 s = "tgeu";
7053 goto trap;
7054 case M_TLT_I:
7055 s = "tlt";
7056 goto trap;
7057 case M_TLTU_I:
7058 s = "tltu";
7059 goto trap;
7060 case M_TNE_I:
7061 s = "tne";
7062 trap:
4d34fb5f 7063 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
7064 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "s,t", sreg,
7065 AT);
252b5132
RH
7066 break;
7067
252b5132 7068 case M_TRUNCWS:
43841e91 7069 case M_TRUNCWD:
e7af610e 7070 assert (mips_opts.isa == ISA_MIPS1);
252b5132
RH
7071 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
7072 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
7073
7074 /*
7075 * Is the double cfc1 instruction a bug in the mips assembler;
7076 * or is there a reason for it?
7077 */
7078 mips_emit_delays (true);
7079 ++mips_opts.noreorder;
7080 mips_any_noreorder = 1;
2396cfb9 7081 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
f9419b05 7082 treg, RA);
2396cfb9 7083 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
f9419b05 7084 treg, RA);
2396cfb9 7085 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
252b5132
RH
7086 expr1.X_add_number = 3;
7087 macro_build ((char *) NULL, &icnt, &expr1, "ori", "t,r,i", AT, treg,
7088 (int) BFD_RELOC_LO16);
7089 expr1.X_add_number = 2;
7090 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", AT, AT,
7091 (int) BFD_RELOC_LO16);
2396cfb9 7092 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
f9419b05 7093 AT, RA);
2396cfb9
TS
7094 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7095 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
252b5132 7096 mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S", dreg, sreg);
2396cfb9 7097 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
f9419b05 7098 treg, RA);
2396cfb9 7099 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
252b5132
RH
7100 --mips_opts.noreorder;
7101 break;
7102
7103 case M_ULH:
7104 s = "lb";
7105 goto ulh;
7106 case M_ULHU:
7107 s = "lbu";
7108 ulh:
7109 if (offset_expr.X_add_number >= 0x7fff)
7110 as_bad (_("operand overflow"));
7111 /* avoid load delay */
7112 if (! target_big_endian)
f9419b05 7113 ++offset_expr.X_add_number;
252b5132
RH
7114 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7115 (int) BFD_RELOC_LO16, breg);
7116 if (! target_big_endian)
f9419b05 7117 --offset_expr.X_add_number;
252b5132 7118 else
f9419b05 7119 ++offset_expr.X_add_number;
252b5132
RH
7120 macro_build ((char *) NULL, &icnt, &offset_expr, "lbu", "t,o(b)", AT,
7121 (int) BFD_RELOC_LO16, breg);
2396cfb9
TS
7122 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7123 treg, treg, 8);
7124 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7125 treg, treg, AT);
252b5132
RH
7126 break;
7127
7128 case M_ULD:
7129 s = "ldl";
7130 s2 = "ldr";
7131 off = 7;
7132 goto ulw;
7133 case M_ULW:
7134 s = "lwl";
7135 s2 = "lwr";
7136 off = 3;
7137 ulw:
7138 if (offset_expr.X_add_number >= 0x8000 - off)
7139 as_bad (_("operand overflow"));
7140 if (! target_big_endian)
7141 offset_expr.X_add_number += off;
7142 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7143 (int) BFD_RELOC_LO16, breg);
7144 if (! target_big_endian)
7145 offset_expr.X_add_number -= off;
7146 else
7147 offset_expr.X_add_number += off;
7148 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7149 (int) BFD_RELOC_LO16, breg);
7150 return;
7151
7152 case M_ULD_A:
7153 s = "ldl";
7154 s2 = "ldr";
7155 off = 7;
7156 goto ulwa;
7157 case M_ULW_A:
7158 s = "lwl";
7159 s2 = "lwr";
7160 off = 3;
7161 ulwa:
d6bc6245 7162 used_at = 1;
c9914766 7163 load_address (&icnt, AT, &offset_expr, &used_at);
252b5132
RH
7164 if (breg != 0)
7165 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 7166 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
7167 "d,v,t", AT, AT, breg);
7168 if (! target_big_endian)
7169 expr1.X_add_number = off;
7170 else
7171 expr1.X_add_number = 0;
7172 macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7173 (int) BFD_RELOC_LO16, AT);
7174 if (! target_big_endian)
7175 expr1.X_add_number = 0;
7176 else
7177 expr1.X_add_number = off;
7178 macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7179 (int) BFD_RELOC_LO16, AT);
7180 break;
7181
7182 case M_ULH_A:
7183 case M_ULHU_A:
d6bc6245 7184 used_at = 1;
c9914766 7185 load_address (&icnt, AT, &offset_expr, &used_at);
252b5132
RH
7186 if (breg != 0)
7187 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 7188 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
7189 "d,v,t", AT, AT, breg);
7190 if (target_big_endian)
7191 expr1.X_add_number = 0;
7192 macro_build ((char *) NULL, &icnt, &expr1,
7193 mask == M_ULH_A ? "lb" : "lbu", "t,o(b)", treg,
7194 (int) BFD_RELOC_LO16, AT);
7195 if (target_big_endian)
7196 expr1.X_add_number = 1;
7197 else
7198 expr1.X_add_number = 0;
7199 macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7200 (int) BFD_RELOC_LO16, AT);
2396cfb9
TS
7201 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7202 treg, treg, 8);
7203 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7204 treg, treg, AT);
252b5132
RH
7205 break;
7206
7207 case M_USH:
7208 if (offset_expr.X_add_number >= 0x7fff)
7209 as_bad (_("operand overflow"));
7210 if (target_big_endian)
f9419b05 7211 ++offset_expr.X_add_number;
252b5132
RH
7212 macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", treg,
7213 (int) BFD_RELOC_LO16, breg);
2396cfb9
TS
7214 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7215 AT, treg, 8);
252b5132 7216 if (target_big_endian)
f9419b05 7217 --offset_expr.X_add_number;
252b5132 7218 else
f9419b05 7219 ++offset_expr.X_add_number;
252b5132
RH
7220 macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", AT,
7221 (int) BFD_RELOC_LO16, breg);
7222 break;
7223
7224 case M_USD:
7225 s = "sdl";
7226 s2 = "sdr";
7227 off = 7;
7228 goto usw;
7229 case M_USW:
7230 s = "swl";
7231 s2 = "swr";
7232 off = 3;
7233 usw:
7234 if (offset_expr.X_add_number >= 0x8000 - off)
7235 as_bad (_("operand overflow"));
7236 if (! target_big_endian)
7237 offset_expr.X_add_number += off;
7238 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7239 (int) BFD_RELOC_LO16, breg);
7240 if (! target_big_endian)
7241 offset_expr.X_add_number -= off;
7242 else
7243 offset_expr.X_add_number += off;
7244 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7245 (int) BFD_RELOC_LO16, breg);
7246 return;
7247
7248 case M_USD_A:
7249 s = "sdl";
7250 s2 = "sdr";
7251 off = 7;
7252 goto uswa;
7253 case M_USW_A:
7254 s = "swl";
7255 s2 = "swr";
7256 off = 3;
7257 uswa:
d6bc6245 7258 used_at = 1;
c9914766 7259 load_address (&icnt, AT, &offset_expr, &used_at);
252b5132
RH
7260 if (breg != 0)
7261 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 7262 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
7263 "d,v,t", AT, AT, breg);
7264 if (! target_big_endian)
7265 expr1.X_add_number = off;
7266 else
7267 expr1.X_add_number = 0;
7268 macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7269 (int) BFD_RELOC_LO16, AT);
7270 if (! target_big_endian)
7271 expr1.X_add_number = 0;
7272 else
7273 expr1.X_add_number = off;
7274 macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7275 (int) BFD_RELOC_LO16, AT);
7276 break;
7277
7278 case M_USH_A:
d6bc6245 7279 used_at = 1;
c9914766 7280 load_address (&icnt, AT, &offset_expr, &used_at);
252b5132
RH
7281 if (breg != 0)
7282 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 7283 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
7284 "d,v,t", AT, AT, breg);
7285 if (! target_big_endian)
7286 expr1.X_add_number = 0;
7287 macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7288 (int) BFD_RELOC_LO16, AT);
2396cfb9
TS
7289 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7290 treg, treg, 8);
252b5132
RH
7291 if (! target_big_endian)
7292 expr1.X_add_number = 1;
7293 else
7294 expr1.X_add_number = 0;
7295 macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7296 (int) BFD_RELOC_LO16, AT);
7297 if (! target_big_endian)
7298 expr1.X_add_number = 0;
7299 else
7300 expr1.X_add_number = 1;
7301 macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7302 (int) BFD_RELOC_LO16, AT);
2396cfb9
TS
7303 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7304 treg, treg, 8);
7305 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7306 treg, treg, AT);
252b5132
RH
7307 break;
7308
7309 default:
7310 /* FIXME: Check if this is one of the itbl macros, since they
bdaaa2e1 7311 are added dynamically. */
252b5132
RH
7312 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7313 break;
7314 }
7315 if (mips_opts.noat)
7316 as_warn (_("Macro used $at after \".set noat\""));
7317}
7318
7319/* Implement macros in mips16 mode. */
7320
7321static void
7322mips16_macro (ip)
7323 struct mips_cl_insn *ip;
7324{
7325 int mask;
7326 int xreg, yreg, zreg, tmp;
7327 int icnt;
7328 expressionS expr1;
7329 int dbl;
7330 const char *s, *s2, *s3;
7331
7332 mask = ip->insn_mo->mask;
7333
7334 xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
7335 yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
7336 zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
7337
7338 icnt = 0;
7339
7340 expr1.X_op = O_constant;
7341 expr1.X_op_symbol = NULL;
7342 expr1.X_add_symbol = NULL;
7343 expr1.X_add_number = 1;
7344
7345 dbl = 0;
7346
7347 switch (mask)
7348 {
7349 default:
7350 internalError ();
7351
7352 case M_DDIV_3:
7353 dbl = 1;
7354 case M_DIV_3:
7355 s = "mflo";
7356 goto do_div3;
7357 case M_DREM_3:
7358 dbl = 1;
7359 case M_REM_3:
7360 s = "mfhi";
7361 do_div3:
7362 mips_emit_delays (true);
7363 ++mips_opts.noreorder;
7364 mips_any_noreorder = 1;
2396cfb9 7365 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
252b5132
RH
7366 dbl ? "ddiv" : "div",
7367 "0,x,y", xreg, yreg);
7368 expr1.X_add_number = 2;
7369 macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
2396cfb9
TS
7370 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break", "6",
7371 7);
bdaaa2e1 7372
252b5132
RH
7373 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7374 since that causes an overflow. We should do that as well,
7375 but I don't see how to do the comparisons without a temporary
7376 register. */
7377 --mips_opts.noreorder;
2396cfb9 7378 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x", zreg);
252b5132
RH
7379 break;
7380
7381 case M_DIVU_3:
7382 s = "divu";
7383 s2 = "mflo";
7384 goto do_divu3;
7385 case M_REMU_3:
7386 s = "divu";
7387 s2 = "mfhi";
7388 goto do_divu3;
7389 case M_DDIVU_3:
7390 s = "ddivu";
7391 s2 = "mflo";
7392 goto do_divu3;
7393 case M_DREMU_3:
7394 s = "ddivu";
7395 s2 = "mfhi";
7396 do_divu3:
7397 mips_emit_delays (true);
7398 ++mips_opts.noreorder;
7399 mips_any_noreorder = 1;
2396cfb9
TS
7400 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "0,x,y",
7401 xreg, yreg);
252b5132
RH
7402 expr1.X_add_number = 2;
7403 macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
98d3f06f
KH
7404 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
7405 "6", 7);
252b5132 7406 --mips_opts.noreorder;
2396cfb9 7407 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "x", zreg);
252b5132
RH
7408 break;
7409
7410 case M_DMUL:
7411 dbl = 1;
7412 case M_MUL:
2396cfb9 7413 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 7414 dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
2396cfb9
TS
7415 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "x",
7416 zreg);
252b5132
RH
7417 return;
7418
7419 case M_DSUBU_I:
7420 dbl = 1;
7421 goto do_subu;
7422 case M_SUBU_I:
7423 do_subu:
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,
9a41af64 7428 dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
252b5132
RH
7429 break;
7430
7431 case M_SUBU_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, "addiu",
7436 "x,k", xreg);
7437 break;
7438
7439 case M_DSUBU_I_2:
7440 if (imm_expr.X_op != O_constant)
7441 as_bad (_("Unsupported large constant"));
7442 imm_expr.X_add_number = -imm_expr.X_add_number;
7443 macro_build ((char *) NULL, &icnt, &imm_expr, "daddiu",
7444 "y,j", yreg);
7445 break;
7446
7447 case M_BEQ:
7448 s = "cmp";
7449 s2 = "bteqz";
7450 goto do_branch;
7451 case M_BNE:
7452 s = "cmp";
7453 s2 = "btnez";
7454 goto do_branch;
7455 case M_BLT:
7456 s = "slt";
7457 s2 = "btnez";
7458 goto do_branch;
7459 case M_BLTU:
7460 s = "sltu";
7461 s2 = "btnez";
7462 goto do_branch;
7463 case M_BLE:
7464 s = "slt";
7465 s2 = "bteqz";
7466 goto do_reverse_branch;
7467 case M_BLEU:
7468 s = "sltu";
7469 s2 = "bteqz";
7470 goto do_reverse_branch;
7471 case M_BGE:
7472 s = "slt";
7473 s2 = "bteqz";
7474 goto do_branch;
7475 case M_BGEU:
7476 s = "sltu";
7477 s2 = "bteqz";
7478 goto do_branch;
7479 case M_BGT:
7480 s = "slt";
7481 s2 = "btnez";
7482 goto do_reverse_branch;
7483 case M_BGTU:
7484 s = "sltu";
7485 s2 = "btnez";
7486
7487 do_reverse_branch:
7488 tmp = xreg;
7489 xreg = yreg;
7490 yreg = tmp;
7491
7492 do_branch:
7493 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x,y",
7494 xreg, yreg);
7495 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7496 break;
7497
7498 case M_BEQ_I:
7499 s = "cmpi";
7500 s2 = "bteqz";
7501 s3 = "x,U";
7502 goto do_branch_i;
7503 case M_BNE_I:
7504 s = "cmpi";
7505 s2 = "btnez";
7506 s3 = "x,U";
7507 goto do_branch_i;
7508 case M_BLT_I:
7509 s = "slti";
7510 s2 = "btnez";
7511 s3 = "x,8";
7512 goto do_branch_i;
7513 case M_BLTU_I:
7514 s = "sltiu";
7515 s2 = "btnez";
7516 s3 = "x,8";
7517 goto do_branch_i;
7518 case M_BLE_I:
7519 s = "slti";
7520 s2 = "btnez";
7521 s3 = "x,8";
7522 goto do_addone_branch_i;
7523 case M_BLEU_I:
7524 s = "sltiu";
7525 s2 = "btnez";
7526 s3 = "x,8";
7527 goto do_addone_branch_i;
7528 case M_BGE_I:
7529 s = "slti";
7530 s2 = "bteqz";
7531 s3 = "x,8";
7532 goto do_branch_i;
7533 case M_BGEU_I:
7534 s = "sltiu";
7535 s2 = "bteqz";
7536 s3 = "x,8";
7537 goto do_branch_i;
7538 case M_BGT_I:
7539 s = "slti";
7540 s2 = "bteqz";
7541 s3 = "x,8";
7542 goto do_addone_branch_i;
7543 case M_BGTU_I:
7544 s = "sltiu";
7545 s2 = "bteqz";
7546 s3 = "x,8";
7547
7548 do_addone_branch_i:
7549 if (imm_expr.X_op != O_constant)
7550 as_bad (_("Unsupported large constant"));
7551 ++imm_expr.X_add_number;
7552
7553 do_branch_i:
7554 macro_build ((char *) NULL, &icnt, &imm_expr, s, s3, xreg);
7555 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7556 break;
7557
7558 case M_ABS:
7559 expr1.X_add_number = 0;
98d3f06f 7560 macro_build ((char *) NULL, &icnt, &expr1, "slti", "x,8", yreg);
252b5132 7561 if (xreg != yreg)
ea1fb5dc 7562 move_register (&icnt, xreg, yreg);
252b5132
RH
7563 expr1.X_add_number = 2;
7564 macro_build ((char *) NULL, &icnt, &expr1, "bteqz", "p");
7565 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7566 "neg", "x,w", xreg, xreg);
7567 }
7568}
7569
7570/* For consistency checking, verify that all bits are specified either
7571 by the match/mask part of the instruction definition, or by the
7572 operand list. */
7573static int
7574validate_mips_insn (opc)
7575 const struct mips_opcode *opc;
7576{
7577 const char *p = opc->args;
7578 char c;
7579 unsigned long used_bits = opc->mask;
7580
7581 if ((used_bits & opc->match) != opc->match)
7582 {
7583 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
7584 opc->name, opc->args);
7585 return 0;
7586 }
7587#define USE_BITS(mask,shift) (used_bits |= ((mask) << (shift)))
7588 while (*p)
7589 switch (c = *p++)
7590 {
7591 case ',': break;
7592 case '(': break;
7593 case ')': break;
7594 case '<': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7595 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7596 case 'A': break;
4372b673 7597 case 'B': USE_BITS (OP_MASK_CODE20, OP_SH_CODE20); break;
252b5132
RH
7598 case 'C': USE_BITS (OP_MASK_COPZ, OP_SH_COPZ); break;
7599 case 'D': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
7600 case 'E': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7601 case 'F': break;
7602 case 'G': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
156c2f8b 7603 case 'H': USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
252b5132 7604 case 'I': break;
e972090a 7605 case 'J': USE_BITS (OP_MASK_CODE19, OP_SH_CODE19); break;
252b5132
RH
7606 case 'L': break;
7607 case 'M': USE_BITS (OP_MASK_CCC, OP_SH_CCC); break;
7608 case 'N': USE_BITS (OP_MASK_BCC, OP_SH_BCC); break;
deec1734
CD
7609 case 'O': USE_BITS (OP_MASK_ALN, OP_SH_ALN); break;
7610 case 'Q': USE_BITS (OP_MASK_VSEL, OP_SH_VSEL);
7611 USE_BITS (OP_MASK_FT, OP_SH_FT); break;
252b5132
RH
7612 case 'R': USE_BITS (OP_MASK_FR, OP_SH_FR); break;
7613 case 'S': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7614 case 'T': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7615 case 'V': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7616 case 'W': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
deec1734
CD
7617 case 'X': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
7618 case 'Y': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7619 case 'Z': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
252b5132
RH
7620 case 'a': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break;
7621 case 'b': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7622 case 'c': USE_BITS (OP_MASK_CODE, OP_SH_CODE); break;
7623 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
7624 case 'f': break;
7625 case 'h': USE_BITS (OP_MASK_PREFX, OP_SH_PREFX); break;
7626 case 'i': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
7627 case 'j': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7628 case 'k': USE_BITS (OP_MASK_CACHE, OP_SH_CACHE); break;
7629 case 'l': break;
7630 case 'o': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7631 case 'p': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7632 case 'q': USE_BITS (OP_MASK_CODE2, OP_SH_CODE2); break;
7633 case 'r': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7634 case 's': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7635 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7636 case 'u': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
7637 case 'v': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7638 case 'w': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7639 case 'x': break;
7640 case 'z': break;
7641 case 'P': USE_BITS (OP_MASK_PERFREG, OP_SH_PERFREG); break;
4372b673
NC
7642 case 'U': USE_BITS (OP_MASK_RD, OP_SH_RD);
7643 USE_BITS (OP_MASK_RT, OP_SH_RT); break;
252b5132
RH
7644 default:
7645 as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
7646 c, opc->name, opc->args);
7647 return 0;
7648 }
7649#undef USE_BITS
7650 if (used_bits != 0xffffffff)
7651 {
7652 as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
7653 ~used_bits & 0xffffffff, opc->name, opc->args);
7654 return 0;
7655 }
7656 return 1;
7657}
7658
7659/* This routine assembles an instruction into its binary format. As a
7660 side effect, it sets one of the global variables imm_reloc or
7661 offset_reloc to the type of relocation to do if one of the operands
7662 is an address expression. */
7663
7664static void
7665mips_ip (str, ip)
7666 char *str;
7667 struct mips_cl_insn *ip;
7668{
7669 char *s;
7670 const char *args;
43841e91 7671 char c = 0;
252b5132
RH
7672 struct mips_opcode *insn;
7673 char *argsStart;
7674 unsigned int regno;
7675 unsigned int lastregno = 0;
7676 char *s_reset;
7677 char save_c = 0;
252b5132
RH
7678
7679 insn_error = NULL;
7680
7681 /* If the instruction contains a '.', we first try to match an instruction
7682 including the '.'. Then we try again without the '.'. */
7683 insn = NULL;
3882b010 7684 for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
252b5132
RH
7685 continue;
7686
7687 /* If we stopped on whitespace, then replace the whitespace with null for
7688 the call to hash_find. Save the character we replaced just in case we
7689 have to re-parse the instruction. */
3882b010 7690 if (ISSPACE (*s))
252b5132
RH
7691 {
7692 save_c = *s;
7693 *s++ = '\0';
7694 }
bdaaa2e1 7695
252b5132
RH
7696 insn = (struct mips_opcode *) hash_find (op_hash, str);
7697
7698 /* If we didn't find the instruction in the opcode table, try again, but
7699 this time with just the instruction up to, but not including the
7700 first '.'. */
7701 if (insn == NULL)
7702 {
bdaaa2e1 7703 /* Restore the character we overwrite above (if any). */
252b5132
RH
7704 if (save_c)
7705 *(--s) = save_c;
7706
7707 /* Scan up to the first '.' or whitespace. */
3882b010
L
7708 for (s = str;
7709 *s != '\0' && *s != '.' && !ISSPACE (*s);
7710 ++s)
252b5132
RH
7711 continue;
7712
7713 /* If we did not find a '.', then we can quit now. */
7714 if (*s != '.')
7715 {
7716 insn_error = "unrecognized opcode";
7717 return;
7718 }
7719
7720 /* Lookup the instruction in the hash table. */
7721 *s++ = '\0';
7722 if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
7723 {
7724 insn_error = "unrecognized opcode";
7725 return;
7726 }
252b5132
RH
7727 }
7728
7729 argsStart = s;
7730 for (;;)
7731 {
252b5132
RH
7732 boolean ok;
7733
7734 assert (strcmp (insn->name, str) == 0);
7735
1f25f5d3
CD
7736 if (OPCODE_IS_MEMBER (insn,
7737 (mips_opts.isa
aec421e0 7738 | (mips_opts.mips16 ? INSN_MIPS16 : 0)
deec1734 7739 | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
98d3f06f 7740 | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
1f25f5d3 7741 mips_arch))
252b5132 7742 ok = true;
bdaaa2e1 7743 else
252b5132 7744 ok = false;
bdaaa2e1 7745
252b5132
RH
7746 if (insn->pinfo != INSN_MACRO)
7747 {
ec68c924 7748 if (mips_arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
252b5132
RH
7749 ok = false;
7750 }
7751
7752 if (! ok)
7753 {
7754 if (insn + 1 < &mips_opcodes[NUMOPCODES]
7755 && strcmp (insn->name, insn[1].name) == 0)
7756 {
7757 ++insn;
7758 continue;
7759 }
252b5132 7760 else
beae10d5 7761 {
268f6bed
L
7762 if (!insn_error)
7763 {
7764 static char buf[100];
316f5878
RS
7765 if (mips_arch_info->is_isa)
7766 sprintf (buf,
7767 _("opcode not supported at this ISA level (%s)"),
7768 mips_cpu_info_from_isa (mips_opts.isa)->name);
7769 else
7770 sprintf (buf,
7771 _("opcode not supported on this processor: %s (%s)"),
7772 mips_arch_info->name,
7773 mips_cpu_info_from_isa (mips_opts.isa)->name);
268f6bed
L
7774 insn_error = buf;
7775 }
7776 if (save_c)
7777 *(--s) = save_c;
2bd7f1f3 7778 return;
252b5132 7779 }
252b5132
RH
7780 }
7781
7782 ip->insn_mo = insn;
7783 ip->insn_opcode = insn->match;
268f6bed 7784 insn_error = NULL;
252b5132
RH
7785 for (args = insn->args;; ++args)
7786 {
deec1734
CD
7787 int is_mdmx;
7788
ad8d3bb3 7789 s += strspn (s, " \t");
deec1734 7790 is_mdmx = 0;
252b5132
RH
7791 switch (*args)
7792 {
7793 case '\0': /* end of args */
7794 if (*s == '\0')
7795 return;
7796 break;
7797
7798 case ',':
7799 if (*s++ == *args)
7800 continue;
7801 s--;
7802 switch (*++args)
7803 {
7804 case 'r':
7805 case 'v':
38487616 7806 ip->insn_opcode |= lastregno << OP_SH_RS;
252b5132
RH
7807 continue;
7808
7809 case 'w':
38487616
TS
7810 ip->insn_opcode |= lastregno << OP_SH_RT;
7811 continue;
7812
252b5132 7813 case 'W':
38487616 7814 ip->insn_opcode |= lastregno << OP_SH_FT;
252b5132
RH
7815 continue;
7816
7817 case 'V':
38487616 7818 ip->insn_opcode |= lastregno << OP_SH_FS;
252b5132
RH
7819 continue;
7820 }
7821 break;
7822
7823 case '(':
7824 /* Handle optional base register.
7825 Either the base register is omitted or
bdaaa2e1 7826 we must have a left paren. */
252b5132
RH
7827 /* This is dependent on the next operand specifier
7828 is a base register specification. */
7829 assert (args[1] == 'b' || args[1] == '5'
7830 || args[1] == '-' || args[1] == '4');
7831 if (*s == '\0')
7832 return;
7833
7834 case ')': /* these must match exactly */
7835 if (*s++ == *args)
7836 continue;
7837 break;
7838
7839 case '<': /* must be at least one digit */
7840 /*
7841 * According to the manual, if the shift amount is greater
b6ff326e
KH
7842 * than 31 or less than 0, then the shift amount should be
7843 * mod 32. In reality the mips assembler issues an error.
252b5132
RH
7844 * We issue a warning and mask out all but the low 5 bits.
7845 */
7846 my_getExpression (&imm_expr, s);
7847 check_absolute_expr (ip, &imm_expr);
7848 if ((unsigned long) imm_expr.X_add_number > 31)
7849 {
793b27f4
TS
7850 as_warn (_("Improper shift amount (%lu)"),
7851 (unsigned long) imm_expr.X_add_number);
38487616 7852 imm_expr.X_add_number &= OP_MASK_SHAMT;
252b5132 7853 }
38487616 7854 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SHAMT;
252b5132
RH
7855 imm_expr.X_op = O_absent;
7856 s = expr_end;
7857 continue;
7858
7859 case '>': /* shift amount minus 32 */
7860 my_getExpression (&imm_expr, s);
7861 check_absolute_expr (ip, &imm_expr);
7862 if ((unsigned long) imm_expr.X_add_number < 32
7863 || (unsigned long) imm_expr.X_add_number > 63)
7864 break;
38487616 7865 ip->insn_opcode |= (imm_expr.X_add_number - 32) << OP_SH_SHAMT;
252b5132
RH
7866 imm_expr.X_op = O_absent;
7867 s = expr_end;
7868 continue;
7869
252b5132
RH
7870 case 'k': /* cache code */
7871 case 'h': /* prefx code */
7872 my_getExpression (&imm_expr, s);
7873 check_absolute_expr (ip, &imm_expr);
7874 if ((unsigned long) imm_expr.X_add_number > 31)
7875 {
7876 as_warn (_("Invalid value for `%s' (%lu)"),
7877 ip->insn_mo->name,
7878 (unsigned long) imm_expr.X_add_number);
7879 imm_expr.X_add_number &= 0x1f;
7880 }
7881 if (*args == 'k')
7882 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
7883 else
7884 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
7885 imm_expr.X_op = O_absent;
7886 s = expr_end;
7887 continue;
7888
7889 case 'c': /* break code */
7890 my_getExpression (&imm_expr, s);
7891 check_absolute_expr (ip, &imm_expr);
793b27f4 7892 if ((unsigned long) imm_expr.X_add_number > 1023)
252b5132 7893 {
793b27f4
TS
7894 as_warn (_("Illegal break code (%lu)"),
7895 (unsigned long) imm_expr.X_add_number);
38487616 7896 imm_expr.X_add_number &= OP_MASK_CODE;
252b5132 7897 }
38487616 7898 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE;
252b5132
RH
7899 imm_expr.X_op = O_absent;
7900 s = expr_end;
7901 continue;
7902
7903 case 'q': /* lower break code */
7904 my_getExpression (&imm_expr, s);
7905 check_absolute_expr (ip, &imm_expr);
793b27f4 7906 if ((unsigned long) imm_expr.X_add_number > 1023)
252b5132 7907 {
793b27f4
TS
7908 as_warn (_("Illegal lower break code (%lu)"),
7909 (unsigned long) imm_expr.X_add_number);
38487616 7910 imm_expr.X_add_number &= OP_MASK_CODE2;
252b5132 7911 }
38487616 7912 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE2;
252b5132
RH
7913 imm_expr.X_op = O_absent;
7914 s = expr_end;
7915 continue;
7916
4372b673 7917 case 'B': /* 20-bit syscall/break code. */
156c2f8b 7918 my_getExpression (&imm_expr, s);
156c2f8b 7919 check_absolute_expr (ip, &imm_expr);
793b27f4
TS
7920 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
7921 as_warn (_("Illegal 20-bit code (%lu)"),
7922 (unsigned long) imm_expr.X_add_number);
38487616 7923 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE20;
252b5132
RH
7924 imm_expr.X_op = O_absent;
7925 s = expr_end;
7926 continue;
7927
98d3f06f 7928 case 'C': /* Coprocessor code */
beae10d5 7929 my_getExpression (&imm_expr, s);
252b5132 7930 check_absolute_expr (ip, &imm_expr);
98d3f06f 7931 if ((unsigned long) imm_expr.X_add_number >= (1 << 25))
252b5132 7932 {
793b27f4
TS
7933 as_warn (_("Coproccesor code > 25 bits (%lu)"),
7934 (unsigned long) imm_expr.X_add_number);
98d3f06f 7935 imm_expr.X_add_number &= ((1 << 25) - 1);
252b5132 7936 }
beae10d5
KH
7937 ip->insn_opcode |= imm_expr.X_add_number;
7938 imm_expr.X_op = O_absent;
7939 s = expr_end;
7940 continue;
252b5132 7941
4372b673
NC
7942 case 'J': /* 19-bit wait code. */
7943 my_getExpression (&imm_expr, s);
7944 check_absolute_expr (ip, &imm_expr);
793b27f4
TS
7945 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
7946 as_warn (_("Illegal 19-bit code (%lu)"),
7947 (unsigned long) imm_expr.X_add_number);
38487616 7948 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE19;
4372b673
NC
7949 imm_expr.X_op = O_absent;
7950 s = expr_end;
7951 continue;
7952
252b5132 7953 case 'P': /* Performance register */
beae10d5 7954 my_getExpression (&imm_expr, s);
252b5132 7955 check_absolute_expr (ip, &imm_expr);
beae10d5 7956 if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
252b5132 7957 {
793b27f4
TS
7958 as_warn (_("Invalid performance register (%lu)"),
7959 (unsigned long) imm_expr.X_add_number);
38487616 7960 imm_expr.X_add_number &= OP_MASK_PERFREG;
252b5132 7961 }
38487616 7962 ip->insn_opcode |= (imm_expr.X_add_number << OP_SH_PERFREG);
beae10d5
KH
7963 imm_expr.X_op = O_absent;
7964 s = expr_end;
7965 continue;
252b5132
RH
7966
7967 case 'b': /* base register */
7968 case 'd': /* destination register */
7969 case 's': /* source register */
7970 case 't': /* target register */
7971 case 'r': /* both target and source */
7972 case 'v': /* both dest and source */
7973 case 'w': /* both dest and target */
7974 case 'E': /* coprocessor target register */
7975 case 'G': /* coprocessor destination register */
7976 case 'x': /* ignore register name */
7977 case 'z': /* must be zero register */
4372b673 7978 case 'U': /* destination register (clo/clz). */
252b5132
RH
7979 s_reset = s;
7980 if (s[0] == '$')
7981 {
7982
3882b010 7983 if (ISDIGIT (s[1]))
252b5132
RH
7984 {
7985 ++s;
7986 regno = 0;
7987 do
7988 {
7989 regno *= 10;
7990 regno += *s - '0';
7991 ++s;
7992 }
3882b010 7993 while (ISDIGIT (*s));
252b5132
RH
7994 if (regno > 31)
7995 as_bad (_("Invalid register number (%d)"), regno);
7996 }
7997 else if (*args == 'E' || *args == 'G')
7998 goto notreg;
7999 else
8000 {
76db943d
TS
8001 if (s[1] == 'r' && s[2] == 'a')
8002 {
8003 s += 3;
8004 regno = RA;
8005 }
8006 else if (s[1] == 'f' && s[2] == 'p')
252b5132
RH
8007 {
8008 s += 3;
8009 regno = FP;
8010 }
8011 else if (s[1] == 's' && s[2] == 'p')
8012 {
8013 s += 3;
8014 regno = SP;
8015 }
8016 else if (s[1] == 'g' && s[2] == 'p')
8017 {
8018 s += 3;
8019 regno = GP;
8020 }
8021 else if (s[1] == 'a' && s[2] == 't')
8022 {
8023 s += 3;
8024 regno = AT;
8025 }
8026 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8027 {
8028 s += 4;
8029 regno = KT0;
8030 }
8031 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8032 {
8033 s += 4;
8034 regno = KT1;
8035 }
85b51719
TS
8036 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
8037 {
8038 s += 5;
8039 regno = ZERO;
8040 }
252b5132
RH
8041 else if (itbl_have_entries)
8042 {
8043 char *p, *n;
d7ba4a77 8044 unsigned long r;
252b5132 8045
d7ba4a77 8046 p = s + 1; /* advance past '$' */
252b5132
RH
8047 n = itbl_get_field (&p); /* n is name */
8048
d7ba4a77
ILT
8049 /* See if this is a register defined in an
8050 itbl entry. */
8051 if (itbl_get_reg_val (n, &r))
252b5132
RH
8052 {
8053 /* Get_field advances to the start of
8054 the next field, so we need to back
d7ba4a77 8055 rack to the end of the last field. */
bdaaa2e1 8056 if (p)
252b5132 8057 s = p - 1;
bdaaa2e1 8058 else
d7ba4a77 8059 s = strchr (s, '\0');
252b5132
RH
8060 regno = r;
8061 }
8062 else
8063 goto notreg;
beae10d5 8064 }
252b5132
RH
8065 else
8066 goto notreg;
8067 }
8068 if (regno == AT
8069 && ! mips_opts.noat
8070 && *args != 'E'
8071 && *args != 'G')
8072 as_warn (_("Used $at without \".set noat\""));
8073 c = *args;
8074 if (*s == ' ')
f9419b05 8075 ++s;
252b5132
RH
8076 if (args[1] != *s)
8077 {
8078 if (c == 'r' || c == 'v' || c == 'w')
8079 {
8080 regno = lastregno;
8081 s = s_reset;
f9419b05 8082 ++args;
252b5132
RH
8083 }
8084 }
8085 /* 'z' only matches $0. */
8086 if (c == 'z' && regno != 0)
8087 break;
8088
bdaaa2e1
KH
8089 /* Now that we have assembled one operand, we use the args string
8090 * to figure out where it goes in the instruction. */
252b5132
RH
8091 switch (c)
8092 {
8093 case 'r':
8094 case 's':
8095 case 'v':
8096 case 'b':
38487616 8097 ip->insn_opcode |= regno << OP_SH_RS;
252b5132
RH
8098 break;
8099 case 'd':
8100 case 'G':
38487616 8101 ip->insn_opcode |= regno << OP_SH_RD;
252b5132 8102 break;
4372b673 8103 case 'U':
38487616
TS
8104 ip->insn_opcode |= regno << OP_SH_RD;
8105 ip->insn_opcode |= regno << OP_SH_RT;
4372b673 8106 break;
252b5132
RH
8107 case 'w':
8108 case 't':
8109 case 'E':
38487616 8110 ip->insn_opcode |= regno << OP_SH_RT;
252b5132
RH
8111 break;
8112 case 'x':
8113 /* This case exists because on the r3000 trunc
8114 expands into a macro which requires a gp
8115 register. On the r6000 or r4000 it is
8116 assembled into a single instruction which
8117 ignores the register. Thus the insn version
8118 is MIPS_ISA2 and uses 'x', and the macro
8119 version is MIPS_ISA1 and uses 't'. */
8120 break;
8121 case 'z':
8122 /* This case is for the div instruction, which
8123 acts differently if the destination argument
8124 is $0. This only matches $0, and is checked
8125 outside the switch. */
8126 break;
8127 case 'D':
8128 /* Itbl operand; not yet implemented. FIXME ?? */
8129 break;
8130 /* What about all other operands like 'i', which
8131 can be specified in the opcode table? */
8132 }
8133 lastregno = regno;
8134 continue;
8135 }
8136 notreg:
8137 switch (*args++)
8138 {
8139 case 'r':
8140 case 'v':
38487616 8141 ip->insn_opcode |= lastregno << OP_SH_RS;
252b5132
RH
8142 continue;
8143 case 'w':
38487616 8144 ip->insn_opcode |= lastregno << OP_SH_RT;
252b5132
RH
8145 continue;
8146 }
8147 break;
8148
deec1734
CD
8149 case 'O': /* MDMX alignment immediate constant. */
8150 my_getExpression (&imm_expr, s);
8151 check_absolute_expr (ip, &imm_expr);
8152 if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
8153 {
8154 as_warn ("Improper align amount (%ld), using low bits",
8155 (long) imm_expr.X_add_number);
8156 imm_expr.X_add_number &= OP_MASK_ALN;
8157 }
8158 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_ALN;
8159 imm_expr.X_op = O_absent;
8160 s = expr_end;
8161 continue;
8162
8163 case 'Q': /* MDMX vector, element sel, or const. */
8164 if (s[0] != '$')
8165 {
8166 /* MDMX Immediate. */
8167 my_getExpression (&imm_expr, s);
8168 check_absolute_expr (ip, &imm_expr);
8169 if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
8170 {
8171 as_warn (_("Invalid MDMX Immediate (%ld)"),
8172 (long) imm_expr.X_add_number);
8173 imm_expr.X_add_number &= OP_MASK_FT;
8174 }
8175 imm_expr.X_add_number &= OP_MASK_FT;
8176 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8177 ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
8178 else
8179 ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
8180 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_FT;
8181 imm_expr.X_op = O_absent;
8182 s = expr_end;
8183 continue;
8184 }
8185 /* Not MDMX Immediate. Fall through. */
8186 case 'X': /* MDMX destination register. */
8187 case 'Y': /* MDMX source register. */
8188 case 'Z': /* MDMX target register. */
8189 is_mdmx = 1;
252b5132
RH
8190 case 'D': /* floating point destination register */
8191 case 'S': /* floating point source register */
8192 case 'T': /* floating point target register */
8193 case 'R': /* floating point source register */
8194 case 'V':
8195 case 'W':
8196 s_reset = s;
deec1734
CD
8197 /* Accept $fN for FP and MDMX register numbers, and in
8198 addition accept $vN for MDMX register numbers. */
8199 if ((s[0] == '$' && s[1] == 'f' && ISDIGIT (s[2]))
8200 || (is_mdmx != 0 && s[0] == '$' && s[1] == 'v'
8201 && ISDIGIT (s[2])))
252b5132
RH
8202 {
8203 s += 2;
8204 regno = 0;
8205 do
8206 {
8207 regno *= 10;
8208 regno += *s - '0';
8209 ++s;
8210 }
3882b010 8211 while (ISDIGIT (*s));
252b5132
RH
8212
8213 if (regno > 31)
8214 as_bad (_("Invalid float register number (%d)"), regno);
8215
8216 if ((regno & 1) != 0
ca4e0257 8217 && HAVE_32BIT_FPRS
252b5132
RH
8218 && ! (strcmp (str, "mtc1") == 0
8219 || strcmp (str, "mfc1") == 0
8220 || strcmp (str, "lwc1") == 0
8221 || strcmp (str, "swc1") == 0
8222 || strcmp (str, "l.s") == 0
8223 || strcmp (str, "s.s") == 0))
8224 as_warn (_("Float register should be even, was %d"),
8225 regno);
8226
8227 c = *args;
8228 if (*s == ' ')
f9419b05 8229 ++s;
252b5132
RH
8230 if (args[1] != *s)
8231 {
8232 if (c == 'V' || c == 'W')
8233 {
8234 regno = lastregno;
8235 s = s_reset;
f9419b05 8236 ++args;
252b5132
RH
8237 }
8238 }
8239 switch (c)
8240 {
8241 case 'D':
deec1734 8242 case 'X':
38487616 8243 ip->insn_opcode |= regno << OP_SH_FD;
252b5132
RH
8244 break;
8245 case 'V':
8246 case 'S':
deec1734 8247 case 'Y':
38487616 8248 ip->insn_opcode |= regno << OP_SH_FS;
252b5132 8249 break;
deec1734
CD
8250 case 'Q':
8251 /* This is like 'Z', but also needs to fix the MDMX
8252 vector/scalar select bits. Note that the
8253 scalar immediate case is handled above. */
8254 if (*s == '[')
8255 {
8256 int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
8257 int max_el = (is_qh ? 3 : 7);
8258 s++;
8259 my_getExpression(&imm_expr, s);
8260 check_absolute_expr (ip, &imm_expr);
8261 s = expr_end;
8262 if (imm_expr.X_add_number > max_el)
8263 as_bad(_("Bad element selector %ld"),
8264 (long) imm_expr.X_add_number);
8265 imm_expr.X_add_number &= max_el;
8266 ip->insn_opcode |= (imm_expr.X_add_number
8267 << (OP_SH_VSEL +
8268 (is_qh ? 2 : 1)));
8269 if (*s != ']')
8270 as_warn(_("Expecting ']' found '%s'"), s);
8271 else
8272 s++;
8273 }
8274 else
8275 {
8276 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8277 ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
8278 << OP_SH_VSEL);
8279 else
8280 ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
8281 OP_SH_VSEL);
8282 }
8283 /* Fall through */
252b5132
RH
8284 case 'W':
8285 case 'T':
deec1734 8286 case 'Z':
38487616 8287 ip->insn_opcode |= regno << OP_SH_FT;
252b5132
RH
8288 break;
8289 case 'R':
38487616 8290 ip->insn_opcode |= regno << OP_SH_FR;
252b5132
RH
8291 break;
8292 }
8293 lastregno = regno;
8294 continue;
8295 }
8296
252b5132
RH
8297 switch (*args++)
8298 {
8299 case 'V':
38487616 8300 ip->insn_opcode |= lastregno << OP_SH_FS;
252b5132
RH
8301 continue;
8302 case 'W':
38487616 8303 ip->insn_opcode |= lastregno << OP_SH_FT;
252b5132
RH
8304 continue;
8305 }
8306 break;
8307
8308 case 'I':
8309 my_getExpression (&imm_expr, s);
8310 if (imm_expr.X_op != O_big
8311 && imm_expr.X_op != O_constant)
8312 insn_error = _("absolute expression required");
8313 s = expr_end;
8314 continue;
8315
8316 case 'A':
8317 my_getExpression (&offset_expr, s);
f6688943 8318 *imm_reloc = BFD_RELOC_32;
252b5132
RH
8319 s = expr_end;
8320 continue;
8321
8322 case 'F':
8323 case 'L':
8324 case 'f':
8325 case 'l':
8326 {
8327 int f64;
ca4e0257 8328 int using_gprs;
252b5132
RH
8329 char *save_in;
8330 char *err;
8331 unsigned char temp[8];
8332 int len;
8333 unsigned int length;
8334 segT seg;
8335 subsegT subseg;
8336 char *p;
8337
8338 /* These only appear as the last operand in an
8339 instruction, and every instruction that accepts
8340 them in any variant accepts them in all variants.
8341 This means we don't have to worry about backing out
8342 any changes if the instruction does not match.
8343
8344 The difference between them is the size of the
8345 floating point constant and where it goes. For 'F'
8346 and 'L' the constant is 64 bits; for 'f' and 'l' it
8347 is 32 bits. Where the constant is placed is based
8348 on how the MIPS assembler does things:
8349 F -- .rdata
8350 L -- .lit8
8351 f -- immediate value
8352 l -- .lit4
8353
8354 The .lit4 and .lit8 sections are only used if
8355 permitted by the -G argument.
8356
8357 When generating embedded PIC code, we use the
8358 .lit8 section but not the .lit4 section (we can do
8359 .lit4 inline easily; we need to put .lit8
8360 somewhere in the data segment, and using .lit8
8361 permits the linker to eventually combine identical
ca4e0257
RS
8362 .lit8 entries).
8363
8364 The code below needs to know whether the target register
8365 is 32 or 64 bits wide. It relies on the fact 'f' and
8366 'F' are used with GPR-based instructions and 'l' and
8367 'L' are used with FPR-based instructions. */
252b5132
RH
8368
8369 f64 = *args == 'F' || *args == 'L';
ca4e0257 8370 using_gprs = *args == 'F' || *args == 'f';
252b5132
RH
8371
8372 save_in = input_line_pointer;
8373 input_line_pointer = s;
8374 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
8375 length = len;
8376 s = input_line_pointer;
8377 input_line_pointer = save_in;
8378 if (err != NULL && *err != '\0')
8379 {
8380 as_bad (_("Bad floating point constant: %s"), err);
8381 memset (temp, '\0', sizeof temp);
8382 length = f64 ? 8 : 4;
8383 }
8384
156c2f8b 8385 assert (length == (unsigned) (f64 ? 8 : 4));
252b5132
RH
8386
8387 if (*args == 'f'
8388 || (*args == 'l'
8389 && (! USE_GLOBAL_POINTER_OPT
8390 || mips_pic == EMBEDDED_PIC
8391 || g_switch_value < 4
8392 || (temp[0] == 0 && temp[1] == 0)
8393 || (temp[2] == 0 && temp[3] == 0))))
8394 {
8395 imm_expr.X_op = O_constant;
8396 if (! target_big_endian)
8397 imm_expr.X_add_number = bfd_getl32 (temp);
8398 else
8399 imm_expr.X_add_number = bfd_getb32 (temp);
8400 }
8401 else if (length > 4
119d663a 8402 && ! mips_disable_float_construction
ca4e0257
RS
8403 /* Constants can only be constructed in GPRs and
8404 copied to FPRs if the GPRs are at least as wide
8405 as the FPRs. Force the constant into memory if
8406 we are using 64-bit FPRs but the GPRs are only
8407 32 bits wide. */
8408 && (using_gprs
8409 || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
252b5132
RH
8410 && ((temp[0] == 0 && temp[1] == 0)
8411 || (temp[2] == 0 && temp[3] == 0))
8412 && ((temp[4] == 0 && temp[5] == 0)
8413 || (temp[6] == 0 && temp[7] == 0)))
8414 {
ca4e0257
RS
8415 /* The value is simple enough to load with a couple of
8416 instructions. If using 32-bit registers, set
8417 imm_expr to the high order 32 bits and offset_expr to
8418 the low order 32 bits. Otherwise, set imm_expr to
8419 the entire 64 bit constant. */
8420 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
252b5132
RH
8421 {
8422 imm_expr.X_op = O_constant;
8423 offset_expr.X_op = O_constant;
8424 if (! target_big_endian)
8425 {
8426 imm_expr.X_add_number = bfd_getl32 (temp + 4);
8427 offset_expr.X_add_number = bfd_getl32 (temp);
8428 }
8429 else
8430 {
8431 imm_expr.X_add_number = bfd_getb32 (temp);
8432 offset_expr.X_add_number = bfd_getb32 (temp + 4);
8433 }
8434 if (offset_expr.X_add_number == 0)
8435 offset_expr.X_op = O_absent;
8436 }
8437 else if (sizeof (imm_expr.X_add_number) > 4)
8438 {
8439 imm_expr.X_op = O_constant;
8440 if (! target_big_endian)
8441 imm_expr.X_add_number = bfd_getl64 (temp);
8442 else
8443 imm_expr.X_add_number = bfd_getb64 (temp);
8444 }
8445 else
8446 {
8447 imm_expr.X_op = O_big;
8448 imm_expr.X_add_number = 4;
8449 if (! target_big_endian)
8450 {
8451 generic_bignum[0] = bfd_getl16 (temp);
8452 generic_bignum[1] = bfd_getl16 (temp + 2);
8453 generic_bignum[2] = bfd_getl16 (temp + 4);
8454 generic_bignum[3] = bfd_getl16 (temp + 6);
8455 }
8456 else
8457 {
8458 generic_bignum[0] = bfd_getb16 (temp + 6);
8459 generic_bignum[1] = bfd_getb16 (temp + 4);
8460 generic_bignum[2] = bfd_getb16 (temp + 2);
8461 generic_bignum[3] = bfd_getb16 (temp);
8462 }
8463 }
8464 }
8465 else
8466 {
8467 const char *newname;
8468 segT new_seg;
8469
8470 /* Switch to the right section. */
8471 seg = now_seg;
8472 subseg = now_subseg;
8473 switch (*args)
8474 {
8475 default: /* unused default case avoids warnings. */
8476 case 'L':
8477 newname = RDATA_SECTION_NAME;
bb2d6cd7
GK
8478 if ((USE_GLOBAL_POINTER_OPT && g_switch_value >= 8)
8479 || mips_pic == EMBEDDED_PIC)
252b5132
RH
8480 newname = ".lit8";
8481 break;
8482 case 'F':
bb2d6cd7
GK
8483 if (mips_pic == EMBEDDED_PIC)
8484 newname = ".lit8";
8485 else
8486 newname = RDATA_SECTION_NAME;
252b5132
RH
8487 break;
8488 case 'l':
8489 assert (!USE_GLOBAL_POINTER_OPT
8490 || g_switch_value >= 4);
8491 newname = ".lit4";
8492 break;
8493 }
8494 new_seg = subseg_new (newname, (subsegT) 0);
8495 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
8496 bfd_set_section_flags (stdoutput, new_seg,
8497 (SEC_ALLOC
8498 | SEC_LOAD
8499 | SEC_READONLY
8500 | SEC_DATA));
8501 frag_align (*args == 'l' ? 2 : 3, 0, 0);
8502 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
8503 && strcmp (TARGET_OS, "elf") != 0)
8504 record_alignment (new_seg, 4);
8505 else
8506 record_alignment (new_seg, *args == 'l' ? 2 : 3);
8507 if (seg == now_seg)
8508 as_bad (_("Can't use floating point insn in this section"));
8509
8510 /* Set the argument to the current address in the
8511 section. */
8512 offset_expr.X_op = O_symbol;
8513 offset_expr.X_add_symbol =
8514 symbol_new ("L0\001", now_seg,
8515 (valueT) frag_now_fix (), frag_now);
8516 offset_expr.X_add_number = 0;
8517
8518 /* Put the floating point number into the section. */
8519 p = frag_more ((int) length);
8520 memcpy (p, temp, length);
8521
8522 /* Switch back to the original section. */
8523 subseg_set (seg, subseg);
8524 }
8525 }
8526 continue;
8527
8528 case 'i': /* 16 bit unsigned immediate */
8529 case 'j': /* 16 bit signed immediate */
f6688943 8530 *imm_reloc = BFD_RELOC_LO16;
252b5132 8531 c = my_getSmallExpression (&imm_expr, s);
fb1b3232 8532 if (c != S_EX_NONE)
252b5132 8533 {
fb1b3232 8534 if (c != S_EX_LO)
252b5132 8535 {
fdb987ee
RS
8536 if (c == S_EX_HI)
8537 {
8538 *imm_reloc = BFD_RELOC_HI16_S;
8539 imm_unmatched_hi = true;
8540 }
ad8d3bb3 8541#ifdef OBJ_ELF
fb1b3232 8542 else if (c == S_EX_HIGHEST)
98d3f06f 8543 *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
fb1b3232 8544 else if (c == S_EX_HIGHER)
98d3f06f 8545 *imm_reloc = BFD_RELOC_MIPS_HIGHER;
ad8d3bb3
TS
8546 else if (c == S_EX_GP_REL)
8547 {
8548 /* This occurs in NewABI only. */
8549 c = my_getSmallExpression (&imm_expr, s);
8550 if (c != S_EX_NEG)
8551 as_bad (_("bad composition of relocations"));
8552 else
8553 {
8554 c = my_getSmallExpression (&imm_expr, s);
8555 if (c != S_EX_LO)
8556 as_bad (_("bad composition of relocations"));
8557 else
8558 {
8559 imm_reloc[0] = BFD_RELOC_GPREL16;
8560 imm_reloc[1] = BFD_RELOC_MIPS_SUB;
8561 imm_reloc[2] = BFD_RELOC_LO16;
8562 }
8563 }
8564 }
8565#endif
252b5132 8566 else
f6688943 8567 *imm_reloc = BFD_RELOC_HI16;
252b5132
RH
8568 }
8569 else if (imm_expr.X_op == O_constant)
8570 imm_expr.X_add_number &= 0xffff;
8571 }
8572 if (*args == 'i')
8573 {
fb1b3232 8574 if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
252b5132 8575 || ((imm_expr.X_add_number < 0
beae10d5
KH
8576 || imm_expr.X_add_number >= 0x10000)
8577 && imm_expr.X_op == O_constant))
252b5132
RH
8578 {
8579 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8580 !strcmp (insn->name, insn[1].name))
8581 break;
2ae7e77b
AH
8582 if (imm_expr.X_op == O_constant
8583 || imm_expr.X_op == O_big)
252b5132
RH
8584 as_bad (_("16 bit expression not in range 0..65535"));
8585 }
8586 }
8587 else
8588 {
8589 int more;
8590 offsetT max;
8591
8592 /* The upper bound should be 0x8000, but
8593 unfortunately the MIPS assembler accepts numbers
8594 from 0x8000 to 0xffff and sign extends them, and
8595 we want to be compatible. We only permit this
8596 extended range for an instruction which does not
8597 provide any further alternates, since those
8598 alternates may handle other cases. People should
8599 use the numbers they mean, rather than relying on
8600 a mysterious sign extension. */
8601 more = (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8602 strcmp (insn->name, insn[1].name) == 0);
8603 if (more)
8604 max = 0x8000;
8605 else
8606 max = 0x10000;
fb1b3232 8607 if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
252b5132 8608 || ((imm_expr.X_add_number < -0x8000
beae10d5
KH
8609 || imm_expr.X_add_number >= max)
8610 && imm_expr.X_op == O_constant)
252b5132
RH
8611 || (more
8612 && imm_expr.X_add_number < 0
ca4e0257 8613 && HAVE_64BIT_GPRS
252b5132
RH
8614 && imm_expr.X_unsigned
8615 && sizeof (imm_expr.X_add_number) <= 4))
8616 {
8617 if (more)
8618 break;
2ae7e77b
AH
8619 if (imm_expr.X_op == O_constant
8620 || imm_expr.X_op == O_big)
252b5132
RH
8621 as_bad (_("16 bit expression not in range -32768..32767"));
8622 }
8623 }
8624 s = expr_end;
8625 continue;
8626
8627 case 'o': /* 16 bit offset */
8628 c = my_getSmallExpression (&offset_expr, s);
8629
8630 /* If this value won't fit into a 16 bit offset, then go
8631 find a macro that will generate the 32 bit offset
afdbd6d0 8632 code pattern. */
fb1b3232 8633 if (c == S_EX_NONE
252b5132
RH
8634 && (offset_expr.X_op != O_constant
8635 || offset_expr.X_add_number >= 0x8000
afdbd6d0 8636 || offset_expr.X_add_number < -0x8000))
252b5132
RH
8637 break;
8638
fb1b3232 8639 if (c == S_EX_HI)
252b5132
RH
8640 {
8641 if (offset_expr.X_op != O_constant)
8642 break;
8643 offset_expr.X_add_number =
8644 (offset_expr.X_add_number >> 16) & 0xffff;
8645 }
f6688943 8646 *offset_reloc = BFD_RELOC_LO16;
252b5132
RH
8647 s = expr_end;
8648 continue;
8649
8650 case 'p': /* pc relative offset */
cb56d3d3 8651 if (mips_pic == EMBEDDED_PIC)
f6688943 8652 *offset_reloc = BFD_RELOC_16_PCREL_S2;
cb56d3d3 8653 else
f6688943 8654 *offset_reloc = BFD_RELOC_16_PCREL;
252b5132
RH
8655 my_getExpression (&offset_expr, s);
8656 s = expr_end;
8657 continue;
8658
8659 case 'u': /* upper 16 bits */
8660 c = my_getSmallExpression (&imm_expr, s);
f6688943 8661 *imm_reloc = BFD_RELOC_LO16;
e7d556df 8662 if (c != S_EX_NONE)
252b5132 8663 {
fb1b3232 8664 if (c != S_EX_LO)
252b5132 8665 {
fdb987ee 8666 if (c == S_EX_HI)
252b5132 8667 {
f6688943 8668 *imm_reloc = BFD_RELOC_HI16_S;
252b5132
RH
8669 imm_unmatched_hi = true;
8670 }
645dc66c
TS
8671#ifdef OBJ_ELF
8672 else if (c == S_EX_HIGHEST)
98d3f06f 8673 *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
ad8d3bb3
TS
8674 else if (c == S_EX_GP_REL)
8675 {
8676 /* This occurs in NewABI only. */
8677 c = my_getSmallExpression (&imm_expr, s);
8678 if (c != S_EX_NEG)
8679 as_bad (_("bad composition of relocations"));
8680 else
8681 {
8682 c = my_getSmallExpression (&imm_expr, s);
8683 if (c != S_EX_HI)
8684 as_bad (_("bad composition of relocations"));
8685 else
8686 {
8687 imm_reloc[0] = BFD_RELOC_GPREL16;
8688 imm_reloc[1] = BFD_RELOC_MIPS_SUB;
8689 imm_reloc[2] = BFD_RELOC_HI16_S;
8690 }
8691 }
8692 }
8693#endif
252b5132 8694 else
f6688943 8695 *imm_reloc = BFD_RELOC_HI16;
252b5132
RH
8696 }
8697 else if (imm_expr.X_op == O_constant)
8698 imm_expr.X_add_number &= 0xffff;
8699 }
fdb987ee
RS
8700 else if (imm_expr.X_op == O_constant
8701 && (imm_expr.X_add_number < 0
8702 || imm_expr.X_add_number >= 0x10000))
252b5132
RH
8703 as_bad (_("lui expression not in range 0..65535"));
8704 s = expr_end;
8705 continue;
8706
8707 case 'a': /* 26 bit address */
8708 my_getExpression (&offset_expr, s);
8709 s = expr_end;
f6688943 8710 *offset_reloc = BFD_RELOC_MIPS_JMP;
252b5132
RH
8711 continue;
8712
8713 case 'N': /* 3 bit branch condition code */
8714 case 'M': /* 3 bit compare condition code */
8715 if (strncmp (s, "$fcc", 4) != 0)
8716 break;
8717 s += 4;
8718 regno = 0;
8719 do
8720 {
8721 regno *= 10;
8722 regno += *s - '0';
8723 ++s;
8724 }
3882b010 8725 while (ISDIGIT (*s));
252b5132
RH
8726 if (regno > 7)
8727 as_bad (_("invalid condition code register $fcc%d"), regno);
8728 if (*args == 'N')
8729 ip->insn_opcode |= regno << OP_SH_BCC;
8730 else
8731 ip->insn_opcode |= regno << OP_SH_CCC;
beae10d5 8732 continue;
252b5132 8733
156c2f8b
NC
8734 case 'H':
8735 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
8736 s += 2;
3882b010 8737 if (ISDIGIT (*s))
156c2f8b
NC
8738 {
8739 c = 0;
8740 do
8741 {
8742 c *= 10;
8743 c += *s - '0';
8744 ++s;
8745 }
3882b010 8746 while (ISDIGIT (*s));
156c2f8b
NC
8747 }
8748 else
8749 c = 8; /* Invalid sel value. */
8750
8751 if (c > 7)
8752 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
8753 ip->insn_opcode |= c;
8754 continue;
8755
252b5132
RH
8756 default:
8757 as_bad (_("bad char = '%c'\n"), *args);
8758 internalError ();
8759 }
8760 break;
8761 }
8762 /* Args don't match. */
8763 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8764 !strcmp (insn->name, insn[1].name))
8765 {
8766 ++insn;
8767 s = argsStart;
268f6bed 8768 insn_error = _("illegal operands");
252b5132
RH
8769 continue;
8770 }
268f6bed
L
8771 if (save_c)
8772 *(--s) = save_c;
252b5132
RH
8773 insn_error = _("illegal operands");
8774 return;
8775 }
8776}
8777
8778/* This routine assembles an instruction into its binary format when
8779 assembling for the mips16. As a side effect, it sets one of the
8780 global variables imm_reloc or offset_reloc to the type of
8781 relocation to do if one of the operands is an address expression.
8782 It also sets mips16_small and mips16_ext if the user explicitly
8783 requested a small or extended instruction. */
8784
8785static void
8786mips16_ip (str, ip)
8787 char *str;
8788 struct mips_cl_insn *ip;
8789{
8790 char *s;
8791 const char *args;
8792 struct mips_opcode *insn;
8793 char *argsstart;
8794 unsigned int regno;
8795 unsigned int lastregno = 0;
8796 char *s_reset;
8797
8798 insn_error = NULL;
8799
8800 mips16_small = false;
8801 mips16_ext = false;
8802
3882b010 8803 for (s = str; ISLOWER (*s); ++s)
252b5132
RH
8804 ;
8805 switch (*s)
8806 {
8807 case '\0':
8808 break;
8809
8810 case ' ':
8811 *s++ = '\0';
8812 break;
8813
8814 case '.':
8815 if (s[1] == 't' && s[2] == ' ')
8816 {
8817 *s = '\0';
8818 mips16_small = true;
8819 s += 3;
8820 break;
8821 }
8822 else if (s[1] == 'e' && s[2] == ' ')
8823 {
8824 *s = '\0';
8825 mips16_ext = true;
8826 s += 3;
8827 break;
8828 }
8829 /* Fall through. */
8830 default:
8831 insn_error = _("unknown opcode");
8832 return;
8833 }
8834
8835 if (mips_opts.noautoextend && ! mips16_ext)
8836 mips16_small = true;
8837
8838 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
8839 {
8840 insn_error = _("unrecognized opcode");
8841 return;
8842 }
8843
8844 argsstart = s;
8845 for (;;)
8846 {
8847 assert (strcmp (insn->name, str) == 0);
8848
8849 ip->insn_mo = insn;
8850 ip->insn_opcode = insn->match;
8851 ip->use_extend = false;
8852 imm_expr.X_op = O_absent;
f6688943
TS
8853 imm_reloc[0] = BFD_RELOC_UNUSED;
8854 imm_reloc[1] = BFD_RELOC_UNUSED;
8855 imm_reloc[2] = BFD_RELOC_UNUSED;
252b5132 8856 offset_expr.X_op = O_absent;
f6688943
TS
8857 offset_reloc[0] = BFD_RELOC_UNUSED;
8858 offset_reloc[1] = BFD_RELOC_UNUSED;
8859 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
8860 for (args = insn->args; 1; ++args)
8861 {
8862 int c;
8863
8864 if (*s == ' ')
8865 ++s;
8866
8867 /* In this switch statement we call break if we did not find
8868 a match, continue if we did find a match, or return if we
8869 are done. */
8870
8871 c = *args;
8872 switch (c)
8873 {
8874 case '\0':
8875 if (*s == '\0')
8876 {
8877 /* Stuff the immediate value in now, if we can. */
8878 if (imm_expr.X_op == O_constant
f6688943 8879 && *imm_reloc > BFD_RELOC_UNUSED
252b5132
RH
8880 && insn->pinfo != INSN_MACRO)
8881 {
c4e7957c 8882 mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
252b5132
RH
8883 imm_expr.X_add_number, true, mips16_small,
8884 mips16_ext, &ip->insn_opcode,
8885 &ip->use_extend, &ip->extend);
8886 imm_expr.X_op = O_absent;
f6688943 8887 *imm_reloc = BFD_RELOC_UNUSED;
252b5132
RH
8888 }
8889
8890 return;
8891 }
8892 break;
8893
8894 case ',':
8895 if (*s++ == c)
8896 continue;
8897 s--;
8898 switch (*++args)
8899 {
8900 case 'v':
8901 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
8902 continue;
8903 case 'w':
8904 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
8905 continue;
8906 }
8907 break;
8908
8909 case '(':
8910 case ')':
8911 if (*s++ == c)
8912 continue;
8913 break;
8914
8915 case 'v':
8916 case 'w':
8917 if (s[0] != '$')
8918 {
8919 if (c == 'v')
8920 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
8921 else
8922 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
8923 ++args;
8924 continue;
8925 }
8926 /* Fall through. */
8927 case 'x':
8928 case 'y':
8929 case 'z':
8930 case 'Z':
8931 case '0':
8932 case 'S':
8933 case 'R':
8934 case 'X':
8935 case 'Y':
8936 if (s[0] != '$')
8937 break;
8938 s_reset = s;
3882b010 8939 if (ISDIGIT (s[1]))
252b5132
RH
8940 {
8941 ++s;
8942 regno = 0;
8943 do
8944 {
8945 regno *= 10;
8946 regno += *s - '0';
8947 ++s;
8948 }
3882b010 8949 while (ISDIGIT (*s));
252b5132
RH
8950 if (regno > 31)
8951 {
8952 as_bad (_("invalid register number (%d)"), regno);
8953 regno = 2;
8954 }
8955 }
8956 else
8957 {
76db943d
TS
8958 if (s[1] == 'r' && s[2] == 'a')
8959 {
8960 s += 3;
8961 regno = RA;
8962 }
8963 else if (s[1] == 'f' && s[2] == 'p')
252b5132
RH
8964 {
8965 s += 3;
8966 regno = FP;
8967 }
8968 else if (s[1] == 's' && s[2] == 'p')
8969 {
8970 s += 3;
8971 regno = SP;
8972 }
8973 else if (s[1] == 'g' && s[2] == 'p')
8974 {
8975 s += 3;
8976 regno = GP;
8977 }
8978 else if (s[1] == 'a' && s[2] == 't')
8979 {
8980 s += 3;
8981 regno = AT;
8982 }
8983 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8984 {
8985 s += 4;
8986 regno = KT0;
8987 }
8988 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8989 {
8990 s += 4;
8991 regno = KT1;
8992 }
85b51719
TS
8993 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
8994 {
8995 s += 5;
8996 regno = ZERO;
8997 }
252b5132
RH
8998 else
8999 break;
9000 }
9001
9002 if (*s == ' ')
9003 ++s;
9004 if (args[1] != *s)
9005 {
9006 if (c == 'v' || c == 'w')
9007 {
9008 regno = mips16_to_32_reg_map[lastregno];
9009 s = s_reset;
f9419b05 9010 ++args;
252b5132
RH
9011 }
9012 }
9013
9014 switch (c)
9015 {
9016 case 'x':
9017 case 'y':
9018 case 'z':
9019 case 'v':
9020 case 'w':
9021 case 'Z':
9022 regno = mips32_to_16_reg_map[regno];
9023 break;
9024
9025 case '0':
9026 if (regno != 0)
9027 regno = ILLEGAL_REG;
9028 break;
9029
9030 case 'S':
9031 if (regno != SP)
9032 regno = ILLEGAL_REG;
9033 break;
9034
9035 case 'R':
9036 if (regno != RA)
9037 regno = ILLEGAL_REG;
9038 break;
9039
9040 case 'X':
9041 case 'Y':
9042 if (regno == AT && ! mips_opts.noat)
9043 as_warn (_("used $at without \".set noat\""));
9044 break;
9045
9046 default:
9047 internalError ();
9048 }
9049
9050 if (regno == ILLEGAL_REG)
9051 break;
9052
9053 switch (c)
9054 {
9055 case 'x':
9056 case 'v':
9057 ip->insn_opcode |= regno << MIPS16OP_SH_RX;
9058 break;
9059 case 'y':
9060 case 'w':
9061 ip->insn_opcode |= regno << MIPS16OP_SH_RY;
9062 break;
9063 case 'z':
9064 ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
9065 break;
9066 case 'Z':
9067 ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
9068 case '0':
9069 case 'S':
9070 case 'R':
9071 break;
9072 case 'X':
9073 ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
9074 break;
9075 case 'Y':
9076 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
9077 ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
9078 break;
9079 default:
9080 internalError ();
9081 }
9082
9083 lastregno = regno;
9084 continue;
9085
9086 case 'P':
9087 if (strncmp (s, "$pc", 3) == 0)
9088 {
9089 s += 3;
9090 continue;
9091 }
9092 break;
9093
9094 case '<':
9095 case '>':
9096 case '[':
9097 case ']':
9098 case '4':
9099 case '5':
9100 case 'H':
9101 case 'W':
9102 case 'D':
9103 case 'j':
9104 case '8':
9105 case 'V':
9106 case 'C':
9107 case 'U':
9108 case 'k':
9109 case 'K':
9110 if (s[0] == '%'
9111 && strncmp (s + 1, "gprel(", sizeof "gprel(" - 1) == 0)
9112 {
9113 /* This is %gprel(SYMBOL). We need to read SYMBOL,
9114 and generate the appropriate reloc. If the text
9115 inside %gprel is not a symbol name with an
9116 optional offset, then we generate a normal reloc
9117 and will probably fail later. */
9118 my_getExpression (&imm_expr, s + sizeof "%gprel" - 1);
9119 if (imm_expr.X_op == O_symbol)
9120 {
9121 mips16_ext = true;
f6688943 9122 *imm_reloc = BFD_RELOC_MIPS16_GPREL;
252b5132
RH
9123 s = expr_end;
9124 ip->use_extend = true;
9125 ip->extend = 0;
9126 continue;
9127 }
9128 }
9129 else
9130 {
9131 /* Just pick up a normal expression. */
9132 my_getExpression (&imm_expr, s);
9133 }
9134
9135 if (imm_expr.X_op == O_register)
9136 {
9137 /* What we thought was an expression turned out to
9138 be a register. */
9139
9140 if (s[0] == '(' && args[1] == '(')
9141 {
9142 /* It looks like the expression was omitted
9143 before a register indirection, which means
9144 that the expression is implicitly zero. We
9145 still set up imm_expr, so that we handle
9146 explicit extensions correctly. */
9147 imm_expr.X_op = O_constant;
9148 imm_expr.X_add_number = 0;
f6688943 9149 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
9150 continue;
9151 }
9152
9153 break;
9154 }
9155
9156 /* We need to relax this instruction. */
f6688943 9157 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
9158 s = expr_end;
9159 continue;
9160
9161 case 'p':
9162 case 'q':
9163 case 'A':
9164 case 'B':
9165 case 'E':
9166 /* We use offset_reloc rather than imm_reloc for the PC
9167 relative operands. This lets macros with both
9168 immediate and address operands work correctly. */
9169 my_getExpression (&offset_expr, s);
9170
9171 if (offset_expr.X_op == O_register)
9172 break;
9173
9174 /* We need to relax this instruction. */
f6688943 9175 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
9176 s = expr_end;
9177 continue;
9178
9179 case '6': /* break code */
9180 my_getExpression (&imm_expr, s);
9181 check_absolute_expr (ip, &imm_expr);
9182 if ((unsigned long) imm_expr.X_add_number > 63)
9183 {
9184 as_warn (_("Invalid value for `%s' (%lu)"),
9185 ip->insn_mo->name,
9186 (unsigned long) imm_expr.X_add_number);
9187 imm_expr.X_add_number &= 0x3f;
9188 }
9189 ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
9190 imm_expr.X_op = O_absent;
9191 s = expr_end;
9192 continue;
9193
9194 case 'a': /* 26 bit address */
9195 my_getExpression (&offset_expr, s);
9196 s = expr_end;
f6688943 9197 *offset_reloc = BFD_RELOC_MIPS16_JMP;
252b5132
RH
9198 ip->insn_opcode <<= 16;
9199 continue;
9200
9201 case 'l': /* register list for entry macro */
9202 case 'L': /* register list for exit macro */
9203 {
9204 int mask;
9205
9206 if (c == 'l')
9207 mask = 0;
9208 else
9209 mask = 7 << 3;
9210 while (*s != '\0')
9211 {
9212 int freg, reg1, reg2;
9213
9214 while (*s == ' ' || *s == ',')
9215 ++s;
9216 if (*s != '$')
9217 {
9218 as_bad (_("can't parse register list"));
9219 break;
9220 }
9221 ++s;
9222 if (*s != 'f')
9223 freg = 0;
9224 else
9225 {
9226 freg = 1;
9227 ++s;
9228 }
9229 reg1 = 0;
3882b010 9230 while (ISDIGIT (*s))
252b5132
RH
9231 {
9232 reg1 *= 10;
9233 reg1 += *s - '0';
9234 ++s;
9235 }
9236 if (*s == ' ')
9237 ++s;
9238 if (*s != '-')
9239 reg2 = reg1;
9240 else
9241 {
9242 ++s;
9243 if (*s != '$')
9244 break;
9245 ++s;
9246 if (freg)
9247 {
9248 if (*s == 'f')
9249 ++s;
9250 else
9251 {
9252 as_bad (_("invalid register list"));
9253 break;
9254 }
9255 }
9256 reg2 = 0;
3882b010 9257 while (ISDIGIT (*s))
252b5132
RH
9258 {
9259 reg2 *= 10;
9260 reg2 += *s - '0';
9261 ++s;
9262 }
9263 }
9264 if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
9265 {
9266 mask &= ~ (7 << 3);
9267 mask |= 5 << 3;
9268 }
9269 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
9270 {
9271 mask &= ~ (7 << 3);
9272 mask |= 6 << 3;
9273 }
9274 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
9275 mask |= (reg2 - 3) << 3;
9276 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
9277 mask |= (reg2 - 15) << 1;
f9419b05 9278 else if (reg1 == RA && reg2 == RA)
252b5132
RH
9279 mask |= 1;
9280 else
9281 {
9282 as_bad (_("invalid register list"));
9283 break;
9284 }
9285 }
9286 /* The mask is filled in in the opcode table for the
9287 benefit of the disassembler. We remove it before
9288 applying the actual mask. */
9289 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
9290 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
9291 }
9292 continue;
9293
9294 case 'e': /* extend code */
9295 my_getExpression (&imm_expr, s);
9296 check_absolute_expr (ip, &imm_expr);
9297 if ((unsigned long) imm_expr.X_add_number > 0x7ff)
9298 {
9299 as_warn (_("Invalid value for `%s' (%lu)"),
9300 ip->insn_mo->name,
9301 (unsigned long) imm_expr.X_add_number);
9302 imm_expr.X_add_number &= 0x7ff;
9303 }
9304 ip->insn_opcode |= imm_expr.X_add_number;
9305 imm_expr.X_op = O_absent;
9306 s = expr_end;
9307 continue;
9308
9309 default:
9310 internalError ();
9311 }
9312 break;
9313 }
9314
9315 /* Args don't match. */
9316 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
9317 strcmp (insn->name, insn[1].name) == 0)
9318 {
9319 ++insn;
9320 s = argsstart;
9321 continue;
9322 }
9323
9324 insn_error = _("illegal operands");
9325
9326 return;
9327 }
9328}
9329
9330/* This structure holds information we know about a mips16 immediate
9331 argument type. */
9332
e972090a
NC
9333struct mips16_immed_operand
9334{
252b5132
RH
9335 /* The type code used in the argument string in the opcode table. */
9336 int type;
9337 /* The number of bits in the short form of the opcode. */
9338 int nbits;
9339 /* The number of bits in the extended form of the opcode. */
9340 int extbits;
9341 /* The amount by which the short form is shifted when it is used;
9342 for example, the sw instruction has a shift count of 2. */
9343 int shift;
9344 /* The amount by which the short form is shifted when it is stored
9345 into the instruction code. */
9346 int op_shift;
9347 /* Non-zero if the short form is unsigned. */
9348 int unsp;
9349 /* Non-zero if the extended form is unsigned. */
9350 int extu;
9351 /* Non-zero if the value is PC relative. */
9352 int pcrel;
9353};
9354
9355/* The mips16 immediate operand types. */
9356
9357static const struct mips16_immed_operand mips16_immed_operands[] =
9358{
9359 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9360 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9361 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9362 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9363 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
9364 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
9365 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
9366 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
9367 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
9368 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
9369 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
9370 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
9371 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
9372 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
9373 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
9374 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
9375 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9376 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9377 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
9378 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
9379 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
9380};
9381
9382#define MIPS16_NUM_IMMED \
9383 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
9384
9385/* Handle a mips16 instruction with an immediate value. This or's the
9386 small immediate value into *INSN. It sets *USE_EXTEND to indicate
9387 whether an extended value is needed; if one is needed, it sets
9388 *EXTEND to the value. The argument type is TYPE. The value is VAL.
9389 If SMALL is true, an unextended opcode was explicitly requested.
9390 If EXT is true, an extended opcode was explicitly requested. If
9391 WARN is true, warn if EXT does not match reality. */
9392
9393static void
9394mips16_immed (file, line, type, val, warn, small, ext, insn, use_extend,
9395 extend)
9396 char *file;
9397 unsigned int line;
9398 int type;
9399 offsetT val;
9400 boolean warn;
9401 boolean small;
9402 boolean ext;
9403 unsigned long *insn;
9404 boolean *use_extend;
9405 unsigned short *extend;
9406{
9407 register const struct mips16_immed_operand *op;
9408 int mintiny, maxtiny;
9409 boolean needext;
9410
9411 op = mips16_immed_operands;
9412 while (op->type != type)
9413 {
9414 ++op;
9415 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
9416 }
9417
9418 if (op->unsp)
9419 {
9420 if (type == '<' || type == '>' || type == '[' || type == ']')
9421 {
9422 mintiny = 1;
9423 maxtiny = 1 << op->nbits;
9424 }
9425 else
9426 {
9427 mintiny = 0;
9428 maxtiny = (1 << op->nbits) - 1;
9429 }
9430 }
9431 else
9432 {
9433 mintiny = - (1 << (op->nbits - 1));
9434 maxtiny = (1 << (op->nbits - 1)) - 1;
9435 }
9436
9437 /* Branch offsets have an implicit 0 in the lowest bit. */
9438 if (type == 'p' || type == 'q')
9439 val /= 2;
9440
9441 if ((val & ((1 << op->shift) - 1)) != 0
9442 || val < (mintiny << op->shift)
9443 || val > (maxtiny << op->shift))
9444 needext = true;
9445 else
9446 needext = false;
9447
9448 if (warn && ext && ! needext)
beae10d5
KH
9449 as_warn_where (file, line,
9450 _("extended operand requested but not required"));
252b5132
RH
9451 if (small && needext)
9452 as_bad_where (file, line, _("invalid unextended operand value"));
9453
9454 if (small || (! ext && ! needext))
9455 {
9456 int insnval;
9457
9458 *use_extend = false;
9459 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
9460 insnval <<= op->op_shift;
9461 *insn |= insnval;
9462 }
9463 else
9464 {
9465 long minext, maxext;
9466 int extval;
9467
9468 if (op->extu)
9469 {
9470 minext = 0;
9471 maxext = (1 << op->extbits) - 1;
9472 }
9473 else
9474 {
9475 minext = - (1 << (op->extbits - 1));
9476 maxext = (1 << (op->extbits - 1)) - 1;
9477 }
9478 if (val < minext || val > maxext)
9479 as_bad_where (file, line,
9480 _("operand value out of range for instruction"));
9481
9482 *use_extend = true;
9483 if (op->extbits == 16)
9484 {
9485 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
9486 val &= 0x1f;
9487 }
9488 else if (op->extbits == 15)
9489 {
9490 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
9491 val &= 0xf;
9492 }
9493 else
9494 {
9495 extval = ((val & 0x1f) << 6) | (val & 0x20);
9496 val = 0;
9497 }
9498
9499 *extend = (unsigned short) extval;
9500 *insn |= val;
9501 }
9502}
9503\f
ad8d3bb3
TS
9504static struct percent_op_match
9505{
9506 const char *str;
9507 const enum small_ex_type type;
9508} percent_op[] =
9509{
ad8d3bb3
TS
9510 {"%lo", S_EX_LO},
9511#ifdef OBJ_ELF
394f9b3a
TS
9512 {"%call_hi", S_EX_CALL_HI},
9513 {"%call_lo", S_EX_CALL_LO},
ad8d3bb3
TS
9514 {"%call16", S_EX_CALL16},
9515 {"%got_disp", S_EX_GOT_DISP},
9516 {"%got_page", S_EX_GOT_PAGE},
9517 {"%got_ofst", S_EX_GOT_OFST},
9518 {"%got_hi", S_EX_GOT_HI},
9519 {"%got_lo", S_EX_GOT_LO},
394f9b3a
TS
9520 {"%got", S_EX_GOT},
9521 {"%gp_rel", S_EX_GP_REL},
9522 {"%half", S_EX_HALF},
ad8d3bb3 9523 {"%highest", S_EX_HIGHEST},
394f9b3a
TS
9524 {"%higher", S_EX_HIGHER},
9525 {"%neg", S_EX_NEG},
ad8d3bb3 9526#endif
394f9b3a 9527 {"%hi", S_EX_HI}
ad8d3bb3
TS
9528};
9529
9530/* Parse small expression input. STR gets adjusted to eat up whitespace.
9531 It detects valid "%percent_op(...)" and "($reg)" strings. Percent_op's
9532 can be nested, this is handled by blanking the innermost, parsing the
9533 rest by subsequent calls. */
252b5132
RH
9534
9535static int
ad8d3bb3
TS
9536my_getSmallParser (str, len, nestlevel)
9537 char **str;
9538 unsigned int *len;
9539 int *nestlevel;
252b5132 9540{
ad8d3bb3
TS
9541 *len = 0;
9542 *str += strspn (*str, " \t");
394f9b3a 9543 /* Check for expression in parentheses. */
ad8d3bb3 9544 if (**str == '(')
252b5132 9545 {
ad8d3bb3
TS
9546 char *b = *str + 1 + strspn (*str + 1, " \t");
9547 char *e;
9548
9549 /* Check for base register. */
9550 if (b[0] == '$')
9551 {
9552 if (strchr (b, ')')
9553 && (e = b + strcspn (b, ") \t"))
9554 && e - b > 1 && e - b < 4)
9555 {
98d3f06f
KH
9556 if ((e - b == 3
9557 && ((b[1] == 'f' && b[2] == 'p')
9558 || (b[1] == 's' && b[2] == 'p')
9559 || (b[1] == 'g' && b[2] == 'p')
9560 || (b[1] == 'a' && b[2] == 't')
9561 || (ISDIGIT (b[1])
9562 && ISDIGIT (b[2]))))
9563 || (ISDIGIT (b[1])))
9564 {
9565 *len = strcspn (*str, ")") + 1;
9566 return S_EX_REGISTER;
9567 }
ad8d3bb3
TS
9568 }
9569 }
394f9b3a 9570 /* Check for percent_op (in parentheses). */
ad8d3bb3
TS
9571 else if (b[0] == '%')
9572 {
9573 *str = b;
394f9b3a 9574 return my_getPercentOp (str, len, nestlevel);
ad8d3bb3 9575 }
76b3015f 9576
394f9b3a
TS
9577 /* Some other expression in the parentheses, which can contain
9578 parentheses itself. Attempt to find the matching one. */
9579 {
9580 int pcnt = 1;
9581 char *s;
9582
9583 *len = 1;
9584 for (s = *str + 1; *s && pcnt; s++, (*len)++)
9585 {
9586 if (*s == '(')
f9419b05 9587 ++pcnt;
394f9b3a 9588 else if (*s == ')')
f9419b05 9589 --pcnt;
394f9b3a
TS
9590 }
9591 }
fb1b3232 9592 }
394f9b3a 9593 /* Check for percent_op (outside of parentheses). */
ad8d3bb3 9594 else if (*str[0] == '%')
394f9b3a
TS
9595 return my_getPercentOp (str, len, nestlevel);
9596
9597 /* Any other expression. */
9598 return S_EX_NONE;
9599}
ad8d3bb3 9600
394f9b3a
TS
9601static int
9602my_getPercentOp (str, len, nestlevel)
9603 char **str;
9604 unsigned int *len;
9605 int *nestlevel;
9606{
9607 char *tmp = *str + 1;
9608 unsigned int i = 0;
ad8d3bb3 9609
394f9b3a
TS
9610 while (ISALPHA (*tmp) || *tmp == '_')
9611 {
9612 *tmp = TOLOWER (*tmp);
9613 tmp++;
9614 }
9615 while (i < (sizeof (percent_op) / sizeof (struct percent_op_match)))
9616 {
9617 if (strncmp (*str, percent_op[i].str, strlen (percent_op[i].str)))
98d3f06f 9618 i++;
394f9b3a 9619 else
ad8d3bb3 9620 {
394f9b3a 9621 int type = percent_op[i].type;
ad8d3bb3 9622
394f9b3a
TS
9623 /* Only %hi and %lo are allowed for OldABI. */
9624 if (! HAVE_NEWABI && type != S_EX_HI && type != S_EX_LO)
9625 return S_EX_NONE;
ad8d3bb3 9626
394f9b3a 9627 *len = strlen (percent_op[i].str);
f9419b05 9628 ++(*nestlevel);
394f9b3a 9629 return type;
ad8d3bb3 9630 }
fb1b3232 9631 }
ad8d3bb3
TS
9632 return S_EX_NONE;
9633}
9634
9635static int
9636my_getSmallExpression (ep, str)
9637 expressionS *ep;
9638 char *str;
9639{
9640 static char *oldstr = NULL;
9641 int c = S_EX_NONE;
9642 int oldc;
394f9b3a 9643 int nestlevel = -1;
ad8d3bb3
TS
9644 unsigned int len;
9645
394f9b3a
TS
9646 /* Don't update oldstr if the last call had nested percent_op's. We need
9647 it to parse the outer ones later. */
ad8d3bb3
TS
9648 if (! oldstr)
9649 oldstr = str;
76b3015f 9650
ad8d3bb3 9651 do
fb1b3232 9652 {
ad8d3bb3 9653 oldc = c;
394f9b3a 9654 c = my_getSmallParser (&str, &len, &nestlevel);
ad8d3bb3
TS
9655 if (c != S_EX_NONE && c != S_EX_REGISTER)
9656 str += len;
fb1b3232 9657 }
ad8d3bb3
TS
9658 while (c != S_EX_NONE && c != S_EX_REGISTER);
9659
394f9b3a 9660 if (nestlevel >= 0)
fb1b3232 9661 {
394f9b3a
TS
9662 /* A percent_op was encountered. Don't try to get an expression if
9663 it is already blanked out. */
ad8d3bb3
TS
9664 if (*(str + strspn (str + 1, " )")) != ')')
9665 {
9666 char save;
9667
394f9b3a 9668 /* Let my_getExpression() stop at the closing parenthesis. */
ad8d3bb3
TS
9669 save = *(str + len);
9670 *(str + len) = '\0';
9671 my_getExpression (ep, str);
9672 *(str + len) = save;
9673 }
394f9b3a 9674 if (nestlevel > 0)
ad8d3bb3 9675 {
394f9b3a
TS
9676 /* Blank out including the % sign and the proper matching
9677 parenthesis. */
9678 int pcnt = 1;
9679 char *s = strrchr (oldstr, '%');
9680 char *end;
9681
9682 for (end = strchr (s, '(') + 1; *end && pcnt; end++)
9683 {
9684 if (*end == '(')
f9419b05 9685 ++pcnt;
394f9b3a 9686 else if (*end == ')')
f9419b05 9687 --pcnt;
394f9b3a
TS
9688 }
9689
9690 memset (s, ' ', end - s);
ad8d3bb3
TS
9691 str = oldstr;
9692 }
9693 else
394f9b3a
TS
9694 expr_end = str + len;
9695
ad8d3bb3 9696 c = oldc;
fb1b3232 9697 }
ad8d3bb3 9698 else if (c == S_EX_NONE)
fb1b3232 9699 {
ad8d3bb3 9700 my_getExpression (ep, str);
fb1b3232 9701 }
ad8d3bb3 9702 else if (c == S_EX_REGISTER)
fb1b3232 9703 {
ad8d3bb3
TS
9704 ep->X_op = O_constant;
9705 expr_end = str;
9706 ep->X_add_symbol = NULL;
9707 ep->X_op_symbol = NULL;
9708 ep->X_add_number = 0;
fb1b3232 9709 }
fb1b3232
TS
9710 else
9711 {
98d3f06f 9712 as_fatal (_("internal error"));
fb1b3232 9713 }
252b5132 9714
394f9b3a
TS
9715 if (nestlevel <= 0)
9716 /* All percent_op's have been handled. */
ad8d3bb3 9717 oldstr = NULL;
fb1b3232 9718
fb1b3232 9719 return c;
252b5132
RH
9720}
9721
9722static void
9723my_getExpression (ep, str)
9724 expressionS *ep;
9725 char *str;
9726{
9727 char *save_in;
98aa84af 9728 valueT val;
252b5132
RH
9729
9730 save_in = input_line_pointer;
9731 input_line_pointer = str;
9732 expression (ep);
9733 expr_end = input_line_pointer;
9734 input_line_pointer = save_in;
9735
9736 /* If we are in mips16 mode, and this is an expression based on `.',
9737 then we bump the value of the symbol by 1 since that is how other
9738 text symbols are handled. We don't bother to handle complex
9739 expressions, just `.' plus or minus a constant. */
9740 if (mips_opts.mips16
9741 && ep->X_op == O_symbol
9742 && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
9743 && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
49309057
ILT
9744 && symbol_get_frag (ep->X_add_symbol) == frag_now
9745 && symbol_constant_p (ep->X_add_symbol)
98aa84af
AM
9746 && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
9747 S_SET_VALUE (ep->X_add_symbol, val + 1);
252b5132
RH
9748}
9749
9750/* Turn a string in input_line_pointer into a floating point constant
bc0d738a
NC
9751 of type TYPE, and store the appropriate bytes in *LITP. The number
9752 of LITTLENUMS emitted is stored in *SIZEP. An error message is
252b5132
RH
9753 returned, or NULL on OK. */
9754
9755char *
9756md_atof (type, litP, sizeP)
9757 int type;
9758 char *litP;
9759 int *sizeP;
9760{
9761 int prec;
9762 LITTLENUM_TYPE words[4];
9763 char *t;
9764 int i;
9765
9766 switch (type)
9767 {
9768 case 'f':
9769 prec = 2;
9770 break;
9771
9772 case 'd':
9773 prec = 4;
9774 break;
9775
9776 default:
9777 *sizeP = 0;
9778 return _("bad call to md_atof");
9779 }
9780
9781 t = atof_ieee (input_line_pointer, type, words);
9782 if (t)
9783 input_line_pointer = t;
9784
9785 *sizeP = prec * 2;
9786
9787 if (! target_big_endian)
9788 {
9789 for (i = prec - 1; i >= 0; i--)
9790 {
9791 md_number_to_chars (litP, (valueT) words[i], 2);
9792 litP += 2;
9793 }
9794 }
9795 else
9796 {
9797 for (i = 0; i < prec; i++)
9798 {
9799 md_number_to_chars (litP, (valueT) words[i], 2);
9800 litP += 2;
9801 }
9802 }
bdaaa2e1 9803
252b5132
RH
9804 return NULL;
9805}
9806
9807void
9808md_number_to_chars (buf, val, n)
9809 char *buf;
9810 valueT val;
9811 int n;
9812{
9813 if (target_big_endian)
9814 number_to_chars_bigendian (buf, val, n);
9815 else
9816 number_to_chars_littleendian (buf, val, n);
9817}
9818\f
ae948b86 9819#ifdef OBJ_ELF
e013f690
TS
9820static int support_64bit_objects(void)
9821{
9822 const char **list, **l;
9823
9824 list = bfd_target_list ();
9825 for (l = list; *l != NULL; l++)
9826#ifdef TE_TMIPS
9827 /* This is traditional mips */
9828 if (strcmp (*l, "elf64-tradbigmips") == 0
9829 || strcmp (*l, "elf64-tradlittlemips") == 0)
9830#else
9831 if (strcmp (*l, "elf64-bigmips") == 0
9832 || strcmp (*l, "elf64-littlemips") == 0)
9833#endif
9834 break;
9835 free (list);
9836 return (*l != NULL);
9837}
ae948b86 9838#endif /* OBJ_ELF */
e013f690 9839
5a38dc70 9840const char *md_shortopts = "nO::g::G:";
252b5132 9841
e972090a
NC
9842struct option md_longopts[] =
9843{
252b5132
RH
9844#define OPTION_MIPS1 (OPTION_MD_BASE + 1)
9845 {"mips0", no_argument, NULL, OPTION_MIPS1},
9846 {"mips1", no_argument, NULL, OPTION_MIPS1},
9847#define OPTION_MIPS2 (OPTION_MD_BASE + 2)
9848 {"mips2", no_argument, NULL, OPTION_MIPS2},
9849#define OPTION_MIPS3 (OPTION_MD_BASE + 3)
9850 {"mips3", no_argument, NULL, OPTION_MIPS3},
9851#define OPTION_MIPS4 (OPTION_MD_BASE + 4)
9852 {"mips4", no_argument, NULL, OPTION_MIPS4},
ae948b86
TS
9853#define OPTION_MIPS5 (OPTION_MD_BASE + 5)
9854 {"mips5", no_argument, NULL, OPTION_MIPS5},
9855#define OPTION_MIPS32 (OPTION_MD_BASE + 6)
9856 {"mips32", no_argument, NULL, OPTION_MIPS32},
9857#define OPTION_MIPS64 (OPTION_MD_BASE + 7)
9858 {"mips64", no_argument, NULL, OPTION_MIPS64},
9859#define OPTION_MEMBEDDED_PIC (OPTION_MD_BASE + 8)
252b5132 9860 {"membedded-pic", no_argument, NULL, OPTION_MEMBEDDED_PIC},
ae948b86 9861#define OPTION_TRAP (OPTION_MD_BASE + 9)
252b5132
RH
9862 {"trap", no_argument, NULL, OPTION_TRAP},
9863 {"no-break", no_argument, NULL, OPTION_TRAP},
ae948b86 9864#define OPTION_BREAK (OPTION_MD_BASE + 10)
252b5132
RH
9865 {"break", no_argument, NULL, OPTION_BREAK},
9866 {"no-trap", no_argument, NULL, OPTION_BREAK},
ae948b86 9867#define OPTION_EB (OPTION_MD_BASE + 11)
252b5132 9868 {"EB", no_argument, NULL, OPTION_EB},
ae948b86 9869#define OPTION_EL (OPTION_MD_BASE + 12)
252b5132 9870 {"EL", no_argument, NULL, OPTION_EL},
ae948b86 9871#define OPTION_MIPS16 (OPTION_MD_BASE + 13)
252b5132 9872 {"mips16", no_argument, NULL, OPTION_MIPS16},
ae948b86 9873#define OPTION_NO_MIPS16 (OPTION_MD_BASE + 14)
252b5132 9874 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
ae948b86 9875#define OPTION_M7000_HILO_FIX (OPTION_MD_BASE + 15)
6b76fefe 9876 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
9ee72ff1
TS
9877#define OPTION_MNO_7000_HILO_FIX (OPTION_MD_BASE + 16)
9878 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
9879 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
ae948b86
TS
9880#define OPTION_FP32 (OPTION_MD_BASE + 17)
9881 {"mfp32", no_argument, NULL, OPTION_FP32},
9882#define OPTION_GP32 (OPTION_MD_BASE + 18)
c97ef257 9883 {"mgp32", no_argument, NULL, OPTION_GP32},
ae948b86 9884#define OPTION_CONSTRUCT_FLOATS (OPTION_MD_BASE + 19)
119d663a 9885 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
ae948b86 9886#define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MD_BASE + 20)
119d663a 9887 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
ae948b86 9888#define OPTION_MARCH (OPTION_MD_BASE + 21)
ec68c924 9889 {"march", required_argument, NULL, OPTION_MARCH},
ae948b86 9890#define OPTION_MTUNE (OPTION_MD_BASE + 22)
ec68c924 9891 {"mtune", required_argument, NULL, OPTION_MTUNE},
316f5878
RS
9892#define OPTION_FP64 (OPTION_MD_BASE + 23)
9893 {"mfp64", no_argument, NULL, OPTION_FP64},
ae948b86
TS
9894#define OPTION_M4650 (OPTION_MD_BASE + 24)
9895 {"m4650", no_argument, NULL, OPTION_M4650},
9896#define OPTION_NO_M4650 (OPTION_MD_BASE + 25)
9897 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
9898#define OPTION_M4010 (OPTION_MD_BASE + 26)
9899 {"m4010", no_argument, NULL, OPTION_M4010},
9900#define OPTION_NO_M4010 (OPTION_MD_BASE + 27)
9901 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
9902#define OPTION_M4100 (OPTION_MD_BASE + 28)
9903 {"m4100", no_argument, NULL, OPTION_M4100},
9904#define OPTION_NO_M4100 (OPTION_MD_BASE + 29)
9905 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
9906#define OPTION_M3900 (OPTION_MD_BASE + 30)
9907 {"m3900", no_argument, NULL, OPTION_M3900},
9908#define OPTION_NO_M3900 (OPTION_MD_BASE + 31)
9909 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
9910#define OPTION_GP64 (OPTION_MD_BASE + 32)
9911 {"mgp64", no_argument, NULL, OPTION_GP64},
1f25f5d3
CD
9912#define OPTION_MIPS3D (OPTION_MD_BASE + 33)
9913 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
9914#define OPTION_NO_MIPS3D (OPTION_MD_BASE + 34)
9915 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
deec1734
CD
9916#define OPTION_MDMX (OPTION_MD_BASE + 35)
9917 {"mdmx", no_argument, NULL, OPTION_MDMX},
9918#define OPTION_NO_MDMX (OPTION_MD_BASE + 36)
9919 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
156c2f8b 9920#ifdef OBJ_ELF
deec1734 9921#define OPTION_ELF_BASE (OPTION_MD_BASE + 37)
156c2f8b 9922#define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
156c2f8b
NC
9923 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
9924 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
ae948b86 9925#define OPTION_NON_SHARED (OPTION_ELF_BASE + 1)
156c2f8b 9926 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
ae948b86 9927#define OPTION_XGOT (OPTION_ELF_BASE + 2)
156c2f8b 9928 {"xgot", no_argument, NULL, OPTION_XGOT},
ae948b86
TS
9929#define OPTION_MABI (OPTION_ELF_BASE + 3)
9930 {"mabi", required_argument, NULL, OPTION_MABI},
9931#define OPTION_32 (OPTION_ELF_BASE + 4)
156c2f8b 9932 {"32", no_argument, NULL, OPTION_32},
ae948b86 9933#define OPTION_N32 (OPTION_ELF_BASE + 5)
e013f690 9934 {"n32", no_argument, NULL, OPTION_N32},
ae948b86 9935#define OPTION_64 (OPTION_ELF_BASE + 6)
156c2f8b 9936 {"64", no_argument, NULL, OPTION_64},
ecb4347a
DJ
9937#define OPTION_MDEBUG (OPTION_ELF_BASE + 7)
9938 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
9939#define OPTION_NO_MDEBUG (OPTION_ELF_BASE + 8)
9940 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
ae948b86 9941#endif /* OBJ_ELF */
252b5132
RH
9942 {NULL, no_argument, NULL, 0}
9943};
156c2f8b 9944size_t md_longopts_size = sizeof (md_longopts);
252b5132 9945
316f5878
RS
9946/* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
9947 NEW_VALUE. Warn if another value was already specified. Note:
9948 we have to defer parsing the -march and -mtune arguments in order
9949 to handle 'from-abi' correctly, since the ABI might be specified
9950 in a later argument. */
9951
9952static void
9953mips_set_option_string (string_ptr, new_value)
9954 const char **string_ptr, *new_value;
9955{
9956 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
9957 as_warn (_("A different %s was already specified, is now %s"),
9958 string_ptr == &mips_arch_string ? "-march" : "-mtune",
9959 new_value);
9960
9961 *string_ptr = new_value;
9962}
9963
252b5132
RH
9964int
9965md_parse_option (c, arg)
9966 int c;
9967 char *arg;
9968{
9969 switch (c)
9970 {
119d663a
NC
9971 case OPTION_CONSTRUCT_FLOATS:
9972 mips_disable_float_construction = 0;
9973 break;
bdaaa2e1 9974
119d663a
NC
9975 case OPTION_NO_CONSTRUCT_FLOATS:
9976 mips_disable_float_construction = 1;
9977 break;
bdaaa2e1 9978
252b5132
RH
9979 case OPTION_TRAP:
9980 mips_trap = 1;
9981 break;
9982
9983 case OPTION_BREAK:
9984 mips_trap = 0;
9985 break;
9986
9987 case OPTION_EB:
9988 target_big_endian = 1;
9989 break;
9990
9991 case OPTION_EL:
9992 target_big_endian = 0;
9993 break;
9994
39c0a331
L
9995 case 'n':
9996 warn_nops = 1;
9997 break;
9998
252b5132
RH
9999 case 'O':
10000 if (arg && arg[1] == '0')
10001 mips_optimize = 1;
10002 else
10003 mips_optimize = 2;
10004 break;
10005
10006 case 'g':
10007 if (arg == NULL)
10008 mips_debug = 2;
10009 else
10010 mips_debug = atoi (arg);
10011 /* When the MIPS assembler sees -g or -g2, it does not do
10012 optimizations which limit full symbolic debugging. We take
10013 that to be equivalent to -O0. */
10014 if (mips_debug == 2)
10015 mips_optimize = 1;
10016 break;
10017
10018 case OPTION_MIPS1:
316f5878 10019 file_mips_isa = ISA_MIPS1;
252b5132
RH
10020 break;
10021
10022 case OPTION_MIPS2:
316f5878 10023 file_mips_isa = ISA_MIPS2;
252b5132
RH
10024 break;
10025
10026 case OPTION_MIPS3:
316f5878 10027 file_mips_isa = ISA_MIPS3;
252b5132
RH
10028 break;
10029
10030 case OPTION_MIPS4:
316f5878 10031 file_mips_isa = ISA_MIPS4;
e7af610e
NC
10032 break;
10033
84ea6cf2 10034 case OPTION_MIPS5:
316f5878 10035 file_mips_isa = ISA_MIPS5;
84ea6cf2
NC
10036 break;
10037
e7af610e 10038 case OPTION_MIPS32:
316f5878 10039 file_mips_isa = ISA_MIPS32;
252b5132
RH
10040 break;
10041
84ea6cf2 10042 case OPTION_MIPS64:
316f5878 10043 file_mips_isa = ISA_MIPS64;
84ea6cf2
NC
10044 break;
10045
ec68c924 10046 case OPTION_MTUNE:
316f5878
RS
10047 mips_set_option_string (&mips_tune_string, arg);
10048 break;
ec68c924 10049
316f5878
RS
10050 case OPTION_MARCH:
10051 mips_set_option_string (&mips_arch_string, arg);
252b5132
RH
10052 break;
10053
10054 case OPTION_M4650:
316f5878
RS
10055 mips_set_option_string (&mips_arch_string, "4650");
10056 mips_set_option_string (&mips_tune_string, "4650");
252b5132
RH
10057 break;
10058
10059 case OPTION_NO_M4650:
10060 break;
10061
10062 case OPTION_M4010:
316f5878
RS
10063 mips_set_option_string (&mips_arch_string, "4010");
10064 mips_set_option_string (&mips_tune_string, "4010");
252b5132
RH
10065 break;
10066
10067 case OPTION_NO_M4010:
10068 break;
10069
10070 case OPTION_M4100:
316f5878
RS
10071 mips_set_option_string (&mips_arch_string, "4100");
10072 mips_set_option_string (&mips_tune_string, "4100");
252b5132
RH
10073 break;
10074
10075 case OPTION_NO_M4100:
10076 break;
10077
252b5132 10078 case OPTION_M3900:
316f5878
RS
10079 mips_set_option_string (&mips_arch_string, "3900");
10080 mips_set_option_string (&mips_tune_string, "3900");
252b5132 10081 break;
bdaaa2e1 10082
252b5132
RH
10083 case OPTION_NO_M3900:
10084 break;
10085
deec1734
CD
10086 case OPTION_MDMX:
10087 mips_opts.ase_mdmx = 1;
10088 break;
10089
10090 case OPTION_NO_MDMX:
10091 mips_opts.ase_mdmx = 0;
10092 break;
10093
252b5132
RH
10094 case OPTION_MIPS16:
10095 mips_opts.mips16 = 1;
10096 mips_no_prev_insn (false);
10097 break;
10098
10099 case OPTION_NO_MIPS16:
10100 mips_opts.mips16 = 0;
10101 mips_no_prev_insn (false);
10102 break;
10103
1f25f5d3
CD
10104 case OPTION_MIPS3D:
10105 mips_opts.ase_mips3d = 1;
10106 break;
10107
10108 case OPTION_NO_MIPS3D:
10109 mips_opts.ase_mips3d = 0;
10110 break;
10111
252b5132
RH
10112 case OPTION_MEMBEDDED_PIC:
10113 mips_pic = EMBEDDED_PIC;
10114 if (USE_GLOBAL_POINTER_OPT && g_switch_seen)
10115 {
10116 as_bad (_("-G may not be used with embedded PIC code"));
10117 return 0;
10118 }
10119 g_switch_value = 0x7fffffff;
10120 break;
10121
0f074f60 10122#ifdef OBJ_ELF
252b5132
RH
10123 /* When generating ELF code, we permit -KPIC and -call_shared to
10124 select SVR4_PIC, and -non_shared to select no PIC. This is
10125 intended to be compatible with Irix 5. */
10126 case OPTION_CALL_SHARED:
10127 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10128 {
10129 as_bad (_("-call_shared is supported only for ELF format"));
10130 return 0;
10131 }
10132 mips_pic = SVR4_PIC;
10133 if (g_switch_seen && g_switch_value != 0)
10134 {
10135 as_bad (_("-G may not be used with SVR4 PIC code"));
10136 return 0;
10137 }
10138 g_switch_value = 0;
10139 break;
10140
10141 case OPTION_NON_SHARED:
10142 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10143 {
10144 as_bad (_("-non_shared is supported only for ELF format"));
10145 return 0;
10146 }
10147 mips_pic = NO_PIC;
10148 break;
10149
10150 /* The -xgot option tells the assembler to use 32 offsets when
10151 accessing the got in SVR4_PIC mode. It is for Irix
10152 compatibility. */
10153 case OPTION_XGOT:
10154 mips_big_got = 1;
10155 break;
0f074f60 10156#endif /* OBJ_ELF */
252b5132
RH
10157
10158 case 'G':
10159 if (! USE_GLOBAL_POINTER_OPT)
10160 {
10161 as_bad (_("-G is not supported for this configuration"));
10162 return 0;
10163 }
10164 else if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
10165 {
10166 as_bad (_("-G may not be used with SVR4 or embedded PIC code"));
10167 return 0;
10168 }
10169 else
10170 g_switch_value = atoi (arg);
10171 g_switch_seen = 1;
10172 break;
10173
0f074f60 10174#ifdef OBJ_ELF
34ba82a8
TS
10175 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
10176 and -mabi=64. */
252b5132 10177 case OPTION_32:
34ba82a8
TS
10178 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10179 {
10180 as_bad (_("-32 is supported for ELF format only"));
10181 return 0;
10182 }
316f5878 10183 mips_abi = O32_ABI;
252b5132
RH
10184 break;
10185
e013f690 10186 case OPTION_N32:
34ba82a8
TS
10187 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10188 {
10189 as_bad (_("-n32 is supported for ELF format only"));
10190 return 0;
10191 }
316f5878 10192 mips_abi = N32_ABI;
e013f690 10193 break;
252b5132 10194
e013f690 10195 case OPTION_64:
34ba82a8
TS
10196 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10197 {
10198 as_bad (_("-64 is supported for ELF format only"));
10199 return 0;
10200 }
316f5878 10201 mips_abi = N64_ABI;
e013f690
TS
10202 if (! support_64bit_objects())
10203 as_fatal (_("No compiled in support for 64 bit object file format"));
252b5132 10204 break;
ae948b86 10205#endif /* OBJ_ELF */
252b5132 10206
c97ef257 10207 case OPTION_GP32:
a325df1d 10208 file_mips_gp32 = 1;
c97ef257
AH
10209 break;
10210
10211 case OPTION_GP64:
a325df1d 10212 file_mips_gp32 = 0;
c97ef257 10213 break;
252b5132 10214
ca4e0257 10215 case OPTION_FP32:
a325df1d 10216 file_mips_fp32 = 1;
316f5878
RS
10217 break;
10218
10219 case OPTION_FP64:
10220 file_mips_fp32 = 0;
ca4e0257
RS
10221 break;
10222
ae948b86 10223#ifdef OBJ_ELF
252b5132 10224 case OPTION_MABI:
34ba82a8
TS
10225 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10226 {
10227 as_bad (_("-mabi is supported for ELF format only"));
10228 return 0;
10229 }
e013f690 10230 if (strcmp (arg, "32") == 0)
316f5878 10231 mips_abi = O32_ABI;
e013f690 10232 else if (strcmp (arg, "o64") == 0)
316f5878 10233 mips_abi = O64_ABI;
e013f690 10234 else if (strcmp (arg, "n32") == 0)
316f5878 10235 mips_abi = N32_ABI;
e013f690
TS
10236 else if (strcmp (arg, "64") == 0)
10237 {
316f5878 10238 mips_abi = N64_ABI;
e013f690
TS
10239 if (! support_64bit_objects())
10240 as_fatal (_("No compiled in support for 64 bit object file "
10241 "format"));
10242 }
10243 else if (strcmp (arg, "eabi") == 0)
316f5878 10244 mips_abi = EABI_ABI;
e013f690 10245 else
da0e507f
TS
10246 {
10247 as_fatal (_("invalid abi -mabi=%s"), arg);
10248 return 0;
10249 }
252b5132 10250 break;
e013f690 10251#endif /* OBJ_ELF */
252b5132 10252
6b76fefe
CM
10253 case OPTION_M7000_HILO_FIX:
10254 mips_7000_hilo_fix = true;
10255 break;
10256
9ee72ff1 10257 case OPTION_MNO_7000_HILO_FIX:
6b76fefe
CM
10258 mips_7000_hilo_fix = false;
10259 break;
10260
ecb4347a
DJ
10261#ifdef OBJ_ELF
10262 case OPTION_MDEBUG:
10263 mips_flag_mdebug = true;
10264 break;
10265
10266 case OPTION_NO_MDEBUG:
10267 mips_flag_mdebug = false;
10268 break;
10269#endif /* OBJ_ELF */
10270
252b5132
RH
10271 default:
10272 return 0;
10273 }
10274
10275 return 1;
10276}
316f5878
RS
10277\f
10278/* Set up globals to generate code for the ISA or processor
10279 described by INFO. */
252b5132 10280
252b5132 10281static void
316f5878
RS
10282mips_set_architecture (info)
10283 const struct mips_cpu_info *info;
252b5132 10284{
316f5878 10285 if (info != 0)
252b5132 10286 {
316f5878
RS
10287 mips_arch_info = info;
10288 mips_arch = info->cpu;
10289 mips_opts.isa = info->isa;
252b5132 10290 }
252b5132
RH
10291}
10292
252b5132 10293
316f5878 10294/* Likewise for tuning. */
252b5132 10295
316f5878
RS
10296static void
10297mips_set_tune (info)
10298 const struct mips_cpu_info *info;
10299{
10300 if (info != 0)
10301 {
10302 mips_tune_info = info;
10303 mips_tune = info->cpu;
10304 }
10305}
80cc45a5 10306
34ba82a8 10307
252b5132 10308void
e9670677
MR
10309mips_after_parse_args ()
10310{
e9670677
MR
10311 /* GP relative stuff not working for PE */
10312 if (strncmp (TARGET_OS, "pe", 2) == 0
10313 && g_switch_value != 0)
10314 {
10315 if (g_switch_seen)
10316 as_bad (_("-G not supported in this configuration."));
10317 g_switch_value = 0;
10318 }
10319
22923709
RS
10320 /* The following code determines the architecture and register size.
10321 Similar code was added to GCC 3.3 (see override_options() in
10322 config/mips/mips.c). The GAS and GCC code should be kept in sync
10323 as much as possible. */
e9670677 10324
316f5878
RS
10325 if (mips_arch_string != 0)
10326 mips_set_architecture (mips_parse_cpu ("-march", mips_arch_string));
e9670677 10327
316f5878
RS
10328 if (mips_tune_string != 0)
10329 mips_set_tune (mips_parse_cpu ("-mtune", mips_tune_string));
e9670677 10330
316f5878 10331 if (file_mips_isa != ISA_UNKNOWN)
e9670677 10332 {
316f5878
RS
10333 /* Handle -mipsN. At this point, file_mips_isa contains the
10334 ISA level specified by -mipsN, while mips_opts.isa contains
10335 the -march selection (if any). */
10336 if (mips_arch_info != 0)
e9670677 10337 {
316f5878
RS
10338 /* -march takes precedence over -mipsN, since it is more descriptive.
10339 There's no harm in specifying both as long as the ISA levels
10340 are the same. */
10341 if (file_mips_isa != mips_opts.isa)
10342 as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
10343 mips_cpu_info_from_isa (file_mips_isa)->name,
10344 mips_cpu_info_from_isa (mips_opts.isa)->name);
e9670677 10345 }
316f5878
RS
10346 else
10347 mips_set_architecture (mips_cpu_info_from_isa (file_mips_isa));
e9670677
MR
10348 }
10349
316f5878
RS
10350 if (mips_arch_info == 0)
10351 mips_set_architecture (mips_parse_cpu ("default CPU",
10352 MIPS_CPU_STRING_DEFAULT));
e9670677 10353
316f5878
RS
10354 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (mips_opts.isa))
10355 as_bad ("-march=%s is not compatible with the selected ABI",
10356 mips_arch_info->name);
e9670677 10357
316f5878
RS
10358 /* Optimize for mips_arch, unless -mtune selects a different processor. */
10359 if (mips_tune_info == 0)
10360 mips_set_tune (mips_arch_info);
e9670677 10361
316f5878 10362 if (file_mips_gp32 >= 0)
e9670677 10363 {
316f5878
RS
10364 /* The user specified the size of the integer registers. Make sure
10365 it agrees with the ABI and ISA. */
10366 if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
10367 as_bad (_("-mgp64 used with a 32-bit processor"));
10368 else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
10369 as_bad (_("-mgp32 used with a 64-bit ABI"));
10370 else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
10371 as_bad (_("-mgp64 used with a 32-bit ABI"));
e9670677
MR
10372 }
10373 else
10374 {
316f5878
RS
10375 /* Infer the integer register size from the ABI and processor.
10376 Restrict ourselves to 32-bit registers if that's all the
10377 processor has, or if the ABI cannot handle 64-bit registers. */
10378 file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
10379 || !ISA_HAS_64BIT_REGS (mips_opts.isa));
e9670677
MR
10380 }
10381
316f5878
RS
10382 /* ??? GAS treats single-float processors as though they had 64-bit
10383 float registers (although it complains when double-precision
10384 instructions are used). As things stand, saying they have 32-bit
10385 registers would lead to spurious "register must be even" messages.
10386 So here we assume float registers are always the same size as
10387 integer ones, unless the user says otherwise. */
10388 if (file_mips_fp32 < 0)
10389 file_mips_fp32 = file_mips_gp32;
e9670677 10390
316f5878 10391 /* End of GCC-shared inference code. */
e9670677 10392
316f5878
RS
10393 /* ??? When do we want this flag to be set? Who uses it? */
10394 if (file_mips_gp32 == 1
10395 && mips_abi == NO_ABI
10396 && ISA_HAS_64BIT_REGS (mips_opts.isa))
10397 mips_32bitmode = 1;
e9670677
MR
10398
10399 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
10400 as_bad (_("trap exception not supported at ISA 1"));
10401
e9670677
MR
10402 /* If the selected architecture includes support for ASEs, enable
10403 generation of code for them. */
a4672219
TS
10404 if (mips_opts.mips16 == -1)
10405 mips_opts.mips16 = (CPU_HAS_MIPS16 (mips_arch)) ? 1 : 0;
ffdefa66 10406 if (mips_opts.ase_mips3d == -1)
a4672219 10407 mips_opts.ase_mips3d = (CPU_HAS_MIPS3D (mips_arch)) ? 1 : 0;
ffdefa66 10408 if (mips_opts.ase_mdmx == -1)
a4672219 10409 mips_opts.ase_mdmx = (CPU_HAS_MDMX (mips_arch)) ? 1 : 0;
e9670677 10410
e9670677 10411 file_mips_isa = mips_opts.isa;
a4672219 10412 file_ase_mips16 = mips_opts.mips16;
e9670677
MR
10413 file_ase_mips3d = mips_opts.ase_mips3d;
10414 file_ase_mdmx = mips_opts.ase_mdmx;
10415 mips_opts.gp32 = file_mips_gp32;
10416 mips_opts.fp32 = file_mips_fp32;
10417
10418 if (HAVE_NEWABI)
10419 mips_big_got = 1;
ecb4347a
DJ
10420
10421 if (mips_flag_mdebug < 0)
10422 {
10423#ifdef OBJ_MAYBE_ECOFF
10424 if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
10425 mips_flag_mdebug = 1;
10426 else
10427#endif /* OBJ_MAYBE_ECOFF */
10428 mips_flag_mdebug = 0;
10429 }
e9670677
MR
10430}
10431\f
10432void
252b5132
RH
10433mips_init_after_args ()
10434{
10435 /* initialize opcodes */
10436 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
beae10d5 10437 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
252b5132
RH
10438}
10439
10440long
10441md_pcrel_from (fixP)
10442 fixS *fixP;
10443{
10444 if (OUTPUT_FLAVOR != bfd_target_aout_flavour
10445 && fixP->fx_addsy != (symbolS *) NULL
10446 && ! S_IS_DEFINED (fixP->fx_addsy))
10447 {
6478892d
TS
10448 /* This makes a branch to an undefined symbol be a branch to the
10449 current location. */
cb56d3d3 10450 if (mips_pic == EMBEDDED_PIC)
6478892d 10451 return 4;
cb56d3d3 10452 else
6478892d 10453 return 1;
252b5132
RH
10454 }
10455
c9914766 10456 /* Return the address of the delay slot. */
252b5132
RH
10457 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
10458}
10459
252b5132
RH
10460/* This is called before the symbol table is processed. In order to
10461 work with gcc when using mips-tfile, we must keep all local labels.
10462 However, in other cases, we want to discard them. If we were
10463 called with -g, but we didn't see any debugging information, it may
10464 mean that gcc is smuggling debugging information through to
10465 mips-tfile, in which case we must generate all local labels. */
10466
10467void
10468mips_frob_file_before_adjust ()
10469{
10470#ifndef NO_ECOFF_DEBUGGING
10471 if (ECOFF_DEBUGGING
10472 && mips_debug != 0
10473 && ! ecoff_debugging_seen)
10474 flag_keep_locals = 1;
10475#endif
10476}
10477
10478/* Sort any unmatched HI16_S relocs so that they immediately precede
94f592af 10479 the corresponding LO reloc. This is called before md_apply_fix3 and
252b5132
RH
10480 tc_gen_reloc. Unmatched HI16_S relocs can only be generated by
10481 explicit use of the %hi modifier. */
10482
10483void
10484mips_frob_file ()
10485{
10486 struct mips_hi_fixup *l;
10487
10488 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
10489 {
10490 segment_info_type *seginfo;
10491 int pass;
10492
10493 assert (l->fixp->fx_r_type == BFD_RELOC_HI16_S);
10494
10495 /* Check quickly whether the next fixup happens to be a matching
10496 %lo. */
10497 if (l->fixp->fx_next != NULL
10498 && l->fixp->fx_next->fx_r_type == BFD_RELOC_LO16
10499 && l->fixp->fx_addsy == l->fixp->fx_next->fx_addsy
10500 && l->fixp->fx_offset == l->fixp->fx_next->fx_offset)
10501 continue;
10502
10503 /* Look through the fixups for this segment for a matching %lo.
10504 When we find one, move the %hi just in front of it. We do
10505 this in two passes. In the first pass, we try to find a
10506 unique %lo. In the second pass, we permit multiple %hi
10507 relocs for a single %lo (this is a GNU extension). */
10508 seginfo = seg_info (l->seg);
10509 for (pass = 0; pass < 2; pass++)
10510 {
10511 fixS *f, *prev;
10512
10513 prev = NULL;
10514 for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
10515 {
10516 /* Check whether this is a %lo fixup which matches l->fixp. */
10517 if (f->fx_r_type == BFD_RELOC_LO16
10518 && f->fx_addsy == l->fixp->fx_addsy
10519 && f->fx_offset == l->fixp->fx_offset
10520 && (pass == 1
10521 || prev == NULL
10522 || prev->fx_r_type != BFD_RELOC_HI16_S
10523 || prev->fx_addsy != f->fx_addsy
10524 || prev->fx_offset != f->fx_offset))
10525 {
10526 fixS **pf;
10527
10528 /* Move l->fixp before f. */
10529 for (pf = &seginfo->fix_root;
10530 *pf != l->fixp;
10531 pf = &(*pf)->fx_next)
10532 assert (*pf != NULL);
10533
10534 *pf = l->fixp->fx_next;
10535
10536 l->fixp->fx_next = f;
10537 if (prev == NULL)
10538 seginfo->fix_root = l->fixp;
10539 else
10540 prev->fx_next = l->fixp;
10541
10542 break;
10543 }
10544
10545 prev = f;
10546 }
10547
10548 if (f != NULL)
10549 break;
10550
10551#if 0 /* GCC code motion plus incomplete dead code elimination
10552 can leave a %hi without a %lo. */
10553 if (pass == 1)
10554 as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
10555 _("Unmatched %%hi reloc"));
10556#endif
10557 }
10558 }
10559}
10560
10561/* When generating embedded PIC code we need to use a special
10562 relocation to represent the difference of two symbols in the .text
10563 section (switch tables use a difference of this sort). See
10564 include/coff/mips.h for details. This macro checks whether this
10565 fixup requires the special reloc. */
10566#define SWITCH_TABLE(fixp) \
10567 ((fixp)->fx_r_type == BFD_RELOC_32 \
bb2d6cd7 10568 && OUTPUT_FLAVOR != bfd_target_elf_flavour \
252b5132
RH
10569 && (fixp)->fx_addsy != NULL \
10570 && (fixp)->fx_subsy != NULL \
10571 && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
10572 && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
10573
10574/* When generating embedded PIC code we must keep all PC relative
10575 relocations, in case the linker has to relax a call. We also need
f6688943
TS
10576 to keep relocations for switch table entries.
10577
10578 We may have combined relocations without symbols in the N32/N64 ABI.
10579 We have to prevent gas from dropping them. */
252b5132 10580
252b5132
RH
10581int
10582mips_force_relocation (fixp)
10583 fixS *fixp;
10584{
10585 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
10586 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
10587 return 1;
10588
f6688943
TS
10589 if (HAVE_NEWABI
10590 && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
10591 && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
10592 || fixp->fx_r_type == BFD_RELOC_HI16_S
10593 || fixp->fx_r_type == BFD_RELOC_LO16))
10594 return 1;
10595
252b5132
RH
10596 return (mips_pic == EMBEDDED_PIC
10597 && (fixp->fx_pcrel
10598 || SWITCH_TABLE (fixp)
10599 || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
10600 || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
10601}
10602
add55e1f
RS
10603#ifdef OBJ_ELF
10604static int
10605mips_need_elf_addend_fixup (fixP)
10606 fixS *fixP;
10607{
2d2bf3e0
CD
10608 if (S_GET_OTHER (fixP->fx_addsy) == STO_MIPS16)
10609 return 1;
b25a253c
CD
10610 if (mips_pic == EMBEDDED_PIC
10611 && S_IS_WEAK (fixP->fx_addsy))
10612 return 1;
10613 if (mips_pic != EMBEDDED_PIC
10614 && (S_IS_WEAK (fixP->fx_addsy)
bad9ca53 10615 || S_IS_EXTERNAL (fixP->fx_addsy))
2d2bf3e0
CD
10616 && !S_IS_COMMON (fixP->fx_addsy))
10617 return 1;
10618 if (symbol_used_in_reloc_p (fixP->fx_addsy)
10619 && (((bfd_get_section_flags (stdoutput,
10620 S_GET_SEGMENT (fixP->fx_addsy))
10621 & SEC_LINK_ONCE) != 0)
10622 || !strncmp (segment_name (S_GET_SEGMENT (fixP->fx_addsy)),
10623 ".gnu.linkonce",
10624 sizeof (".gnu.linkonce") - 1)))
10625 return 1;
10626 return 0;
add55e1f
RS
10627}
10628#endif
10629
252b5132
RH
10630/* Apply a fixup to the object file. */
10631
94f592af
NC
10632void
10633md_apply_fix3 (fixP, valP, seg)
252b5132 10634 fixS *fixP;
98d3f06f 10635 valueT *valP;
94f592af 10636 segT seg ATTRIBUTE_UNUSED;
252b5132 10637{
874e8986 10638 bfd_byte *buf;
98aa84af
AM
10639 long insn;
10640 valueT value;
252b5132
RH
10641
10642 assert (fixP->fx_size == 4
10643 || fixP->fx_r_type == BFD_RELOC_16
f6688943
TS
10644 || fixP->fx_r_type == BFD_RELOC_32
10645 || fixP->fx_r_type == BFD_RELOC_MIPS_JMP
10646 || fixP->fx_r_type == BFD_RELOC_HI16_S
10647 || fixP->fx_r_type == BFD_RELOC_LO16
10648 || fixP->fx_r_type == BFD_RELOC_GPREL16
76b3015f 10649 || fixP->fx_r_type == BFD_RELOC_MIPS_LITERAL
f6688943 10650 || fixP->fx_r_type == BFD_RELOC_GPREL32
252b5132 10651 || fixP->fx_r_type == BFD_RELOC_64
f6688943
TS
10652 || fixP->fx_r_type == BFD_RELOC_CTOR
10653 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
10654 || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHEST
10655 || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHER
10656 || fixP->fx_r_type == BFD_RELOC_MIPS_SCN_DISP
10657 || fixP->fx_r_type == BFD_RELOC_MIPS_REL16
10658 || fixP->fx_r_type == BFD_RELOC_MIPS_RELGOT
252b5132 10659 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
438c16b8
TS
10660 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
10661 || fixP->fx_r_type == BFD_RELOC_MIPS_JALR);
252b5132 10662
98d3f06f 10663 value = *valP;
252b5132
RH
10664
10665 /* If we aren't adjusting this fixup to be against the section
10666 symbol, we need to adjust the value. */
10667#ifdef OBJ_ELF
10668 if (fixP->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour)
bb2d6cd7 10669 {
add55e1f 10670 if (mips_need_elf_addend_fixup (fixP))
98aa84af 10671 {
d6e9d61a 10672 reloc_howto_type *howto;
98aa84af 10673 valueT symval = S_GET_VALUE (fixP->fx_addsy);
94f592af 10674
98aa84af 10675 value -= symval;
d6e9d61a
MR
10676
10677 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
10678 if (value != 0 && howto->partial_inplace && ! fixP->fx_pcrel)
98aa84af
AM
10679 {
10680 /* In this case, the bfd_install_relocation routine will
10681 incorrectly add the symbol value back in. We just want
7461da6e 10682 the addend to appear in the object file. */
98aa84af 10683 value -= symval;
7461da6e
RS
10684
10685 /* Make sure the addend is still non-zero. If it became zero
10686 after the last operation, set it to a spurious value and
10687 subtract the same value from the object file's contents. */
10688 if (value == 0)
10689 {
10690 value = 8;
10691
10692 /* The in-place addends for LO16 relocations are signed;
10693 leave the matching HI16 in-place addends as zero. */
10694 if (fixP->fx_r_type != BFD_RELOC_HI16_S)
10695 {
7461da6e
RS
10696 bfd_vma contents, mask, field;
10697
7461da6e
RS
10698 contents = bfd_get_bits (fixP->fx_frag->fr_literal
10699 + fixP->fx_where,
10700 fixP->fx_size * 8,
10701 target_big_endian);
10702
10703 /* MASK has bits set where the relocation should go.
10704 FIELD is -value, shifted into the appropriate place
10705 for this relocation. */
10706 mask = 1 << (howto->bitsize - 1);
10707 mask = (((mask - 1) << 1) | 1) << howto->bitpos;
10708 field = (-value >> howto->rightshift) << howto->bitpos;
10709
10710 bfd_put_bits ((field & mask) | (contents & ~mask),
10711 fixP->fx_frag->fr_literal + fixP->fx_where,
10712 fixP->fx_size * 8,
10713 target_big_endian);
10714 }
10715 }
98aa84af
AM
10716 }
10717 }
252b5132 10718
bb2d6cd7
GK
10719 /* This code was generated using trial and error and so is
10720 fragile and not trustworthy. If you change it, you should
10721 rerun the elf-rel, elf-rel2, and empic testcases and ensure
10722 they still pass. */
10723 if (fixP->fx_pcrel || fixP->fx_subsy != NULL)
10724 {
10725 value += fixP->fx_frag->fr_address + fixP->fx_where;
10726
10727 /* BFD's REL handling, for MIPS, is _very_ weird.
10728 This gives the right results, but it can't possibly
10729 be the way things are supposed to work. */
cb56d3d3
TS
10730 if ((fixP->fx_r_type != BFD_RELOC_16_PCREL
10731 && fixP->fx_r_type != BFD_RELOC_16_PCREL_S2)
bb2d6cd7
GK
10732 || S_GET_SEGMENT (fixP->fx_addsy) != undefined_section)
10733 value += fixP->fx_frag->fr_address + fixP->fx_where;
10734 }
10735 }
10736#endif
252b5132 10737
94f592af 10738 fixP->fx_addnumber = value; /* Remember value for tc_gen_reloc. */
252b5132
RH
10739
10740 if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel)
10741 fixP->fx_done = 1;
10742
10743 switch (fixP->fx_r_type)
10744 {
10745 case BFD_RELOC_MIPS_JMP:
e369bcce
TS
10746 case BFD_RELOC_MIPS_SHIFT5:
10747 case BFD_RELOC_MIPS_SHIFT6:
10748 case BFD_RELOC_MIPS_GOT_DISP:
10749 case BFD_RELOC_MIPS_GOT_PAGE:
10750 case BFD_RELOC_MIPS_GOT_OFST:
10751 case BFD_RELOC_MIPS_SUB:
10752 case BFD_RELOC_MIPS_INSERT_A:
10753 case BFD_RELOC_MIPS_INSERT_B:
10754 case BFD_RELOC_MIPS_DELETE:
10755 case BFD_RELOC_MIPS_HIGHEST:
10756 case BFD_RELOC_MIPS_HIGHER:
10757 case BFD_RELOC_MIPS_SCN_DISP:
10758 case BFD_RELOC_MIPS_REL16:
10759 case BFD_RELOC_MIPS_RELGOT:
10760 case BFD_RELOC_MIPS_JALR:
252b5132
RH
10761 case BFD_RELOC_HI16:
10762 case BFD_RELOC_HI16_S:
cdf6fd85 10763 case BFD_RELOC_GPREL16:
252b5132
RH
10764 case BFD_RELOC_MIPS_LITERAL:
10765 case BFD_RELOC_MIPS_CALL16:
10766 case BFD_RELOC_MIPS_GOT16:
cdf6fd85 10767 case BFD_RELOC_GPREL32:
252b5132
RH
10768 case BFD_RELOC_MIPS_GOT_HI16:
10769 case BFD_RELOC_MIPS_GOT_LO16:
10770 case BFD_RELOC_MIPS_CALL_HI16:
10771 case BFD_RELOC_MIPS_CALL_LO16:
10772 case BFD_RELOC_MIPS16_GPREL:
10773 if (fixP->fx_pcrel)
10774 as_bad_where (fixP->fx_file, fixP->fx_line,
10775 _("Invalid PC relative reloc"));
10776 /* Nothing needed to do. The value comes from the reloc entry */
10777 break;
10778
10779 case BFD_RELOC_MIPS16_JMP:
10780 /* We currently always generate a reloc against a symbol, which
10781 means that we don't want an addend even if the symbol is
10782 defined. */
10783 fixP->fx_addnumber = 0;
10784 break;
10785
10786 case BFD_RELOC_PCREL_HI16_S:
10787 /* The addend for this is tricky if it is internal, so we just
10788 do everything here rather than in bfd_install_relocation. */
bdaaa2e1 10789 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
bb2d6cd7
GK
10790 && !fixP->fx_done
10791 && value != 0)
10792 break;
10793 if (fixP->fx_addsy
10794 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
252b5132
RH
10795 {
10796 /* For an external symbol adjust by the address to make it
10797 pcrel_offset. We use the address of the RELLO reloc
10798 which follows this one. */
10799 value += (fixP->fx_next->fx_frag->fr_address
10800 + fixP->fx_next->fx_where);
10801 }
e7d556df 10802 value = ((value + 0x8000) >> 16) & 0xffff;
874e8986 10803 buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
252b5132
RH
10804 if (target_big_endian)
10805 buf += 2;
874e8986 10806 md_number_to_chars ((char *) buf, value, 2);
252b5132
RH
10807 break;
10808
10809 case BFD_RELOC_PCREL_LO16:
10810 /* The addend for this is tricky if it is internal, so we just
10811 do everything here rather than in bfd_install_relocation. */
bdaaa2e1 10812 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
bb2d6cd7
GK
10813 && !fixP->fx_done
10814 && value != 0)
10815 break;
10816 if (fixP->fx_addsy
10817 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
252b5132 10818 value += fixP->fx_frag->fr_address + fixP->fx_where;
874e8986 10819 buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
252b5132
RH
10820 if (target_big_endian)
10821 buf += 2;
874e8986 10822 md_number_to_chars ((char *) buf, value, 2);
252b5132
RH
10823 break;
10824
10825 case BFD_RELOC_64:
10826 /* This is handled like BFD_RELOC_32, but we output a sign
10827 extended value if we are only 32 bits. */
10828 if (fixP->fx_done
10829 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
10830 {
10831 if (8 <= sizeof (valueT))
10832 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
10833 value, 8);
10834 else
10835 {
10836 long w1, w2;
10837 long hiv;
10838
10839 w1 = w2 = fixP->fx_where;
10840 if (target_big_endian)
10841 w1 += 4;
10842 else
10843 w2 += 4;
10844 md_number_to_chars (fixP->fx_frag->fr_literal + w1, value, 4);
10845 if ((value & 0x80000000) != 0)
10846 hiv = 0xffffffff;
10847 else
10848 hiv = 0;
10849 md_number_to_chars (fixP->fx_frag->fr_literal + w2, hiv, 4);
10850 }
10851 }
10852 break;
10853
056350c6 10854 case BFD_RELOC_RVA:
252b5132
RH
10855 case BFD_RELOC_32:
10856 /* If we are deleting this reloc entry, we must fill in the
10857 value now. This can happen if we have a .word which is not
10858 resolved when it appears but is later defined. We also need
10859 to fill in the value if this is an embedded PIC switch table
10860 entry. */
10861 if (fixP->fx_done
10862 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
10863 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
10864 value, 4);
10865 break;
10866
10867 case BFD_RELOC_16:
10868 /* If we are deleting this reloc entry, we must fill in the
10869 value now. */
10870 assert (fixP->fx_size == 2);
10871 if (fixP->fx_done)
10872 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
10873 value, 2);
10874 break;
10875
10876 case BFD_RELOC_LO16:
10877 /* When handling an embedded PIC switch statement, we can wind
10878 up deleting a LO16 reloc. See the 'o' case in mips_ip. */
10879 if (fixP->fx_done)
10880 {
98aa84af 10881 if (value + 0x8000 > 0xffff)
252b5132
RH
10882 as_bad_where (fixP->fx_file, fixP->fx_line,
10883 _("relocation overflow"));
874e8986 10884 buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
252b5132
RH
10885 if (target_big_endian)
10886 buf += 2;
874e8986 10887 md_number_to_chars ((char *) buf, value, 2);
252b5132
RH
10888 }
10889 break;
10890
10891 case BFD_RELOC_16_PCREL_S2:
cb56d3d3
TS
10892 if ((value & 0x3) != 0)
10893 as_bad_where (fixP->fx_file, fixP->fx_line,
10894 _("Branch to odd address (%lx)"), (long) value);
10895
10896 /* Fall through. */
10897
10898 case BFD_RELOC_16_PCREL:
252b5132
RH
10899 /*
10900 * We need to save the bits in the instruction since fixup_segment()
10901 * might be deleting the relocation entry (i.e., a branch within
10902 * the current segment).
10903 */
bb2d6cd7
GK
10904 if (!fixP->fx_done && value != 0)
10905 break;
10906 /* If 'value' is zero, the remaining reloc code won't actually
10907 do the store, so it must be done here. This is probably
10908 a bug somewhere. */
b25a253c
CD
10909 if (!fixP->fx_done
10910 && (fixP->fx_r_type != BFD_RELOC_16_PCREL_S2
10911 || fixP->fx_addsy == NULL /* ??? */
10912 || ! S_IS_DEFINED (fixP->fx_addsy)))
bb2d6cd7 10913 value -= fixP->fx_frag->fr_address + fixP->fx_where;
bdaaa2e1 10914
98aa84af 10915 value = (offsetT) value >> 2;
252b5132
RH
10916
10917 /* update old instruction data */
874e8986 10918 buf = (bfd_byte *) (fixP->fx_where + fixP->fx_frag->fr_literal);
252b5132
RH
10919 if (target_big_endian)
10920 insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
10921 else
10922 insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
10923
98aa84af 10924 if (value + 0x8000 <= 0xffff)
252b5132
RH
10925 insn |= value & 0xffff;
10926 else
10927 {
10928 /* The branch offset is too large. If this is an
10929 unconditional branch, and we are not generating PIC code,
10930 we can convert it to an absolute jump instruction. */
10931 if (mips_pic == NO_PIC
10932 && fixP->fx_done
10933 && fixP->fx_frag->fr_address >= text_section->vma
10934 && (fixP->fx_frag->fr_address
10935 < text_section->vma + text_section->_raw_size)
10936 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
10937 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
10938 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
10939 {
10940 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
10941 insn = 0x0c000000; /* jal */
10942 else
10943 insn = 0x08000000; /* j */
10944 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
10945 fixP->fx_done = 0;
10946 fixP->fx_addsy = section_symbol (text_section);
10947 fixP->fx_addnumber = (value << 2) + md_pcrel_from (fixP);
10948 }
10949 else
10950 {
10951 /* FIXME. It would be possible in principle to handle
10952 conditional branches which overflow. They could be
10953 transformed into a branch around a jump. This would
10954 require setting up variant frags for each different
10955 branch type. The native MIPS assembler attempts to
10956 handle these cases, but it appears to do it
10957 incorrectly. */
10958 as_bad_where (fixP->fx_file, fixP->fx_line,
10959 _("Branch out of range"));
10960 }
10961 }
10962
10963 md_number_to_chars ((char *) buf, (valueT) insn, 4);
10964 break;
10965
10966 case BFD_RELOC_VTABLE_INHERIT:
10967 fixP->fx_done = 0;
10968 if (fixP->fx_addsy
10969 && !S_IS_DEFINED (fixP->fx_addsy)
10970 && !S_IS_WEAK (fixP->fx_addsy))
10971 S_SET_WEAK (fixP->fx_addsy);
10972 break;
10973
10974 case BFD_RELOC_VTABLE_ENTRY:
10975 fixP->fx_done = 0;
10976 break;
10977
10978 default:
10979 internalError ();
10980 }
252b5132
RH
10981}
10982
10983#if 0
10984void
10985printInsn (oc)
10986 unsigned long oc;
10987{
10988 const struct mips_opcode *p;
10989 int treg, sreg, dreg, shamt;
10990 short imm;
10991 const char *args;
10992 int i;
10993
10994 for (i = 0; i < NUMOPCODES; ++i)
10995 {
10996 p = &mips_opcodes[i];
10997 if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
10998 {
10999 printf ("%08lx %s\t", oc, p->name);
11000 treg = (oc >> 16) & 0x1f;
11001 sreg = (oc >> 21) & 0x1f;
11002 dreg = (oc >> 11) & 0x1f;
11003 shamt = (oc >> 6) & 0x1f;
11004 imm = oc;
11005 for (args = p->args;; ++args)
11006 {
11007 switch (*args)
11008 {
11009 case '\0':
11010 printf ("\n");
11011 break;
11012
11013 case ',':
11014 case '(':
11015 case ')':
11016 printf ("%c", *args);
11017 continue;
11018
11019 case 'r':
11020 assert (treg == sreg);
11021 printf ("$%d,$%d", treg, sreg);
11022 continue;
11023
11024 case 'd':
11025 case 'G':
11026 printf ("$%d", dreg);
11027 continue;
11028
11029 case 't':
11030 case 'E':
11031 printf ("$%d", treg);
11032 continue;
11033
11034 case 'k':
11035 printf ("0x%x", treg);
11036 continue;
11037
11038 case 'b':
11039 case 's':
11040 printf ("$%d", sreg);
11041 continue;
11042
11043 case 'a':
11044 printf ("0x%08lx", oc & 0x1ffffff);
11045 continue;
11046
11047 case 'i':
11048 case 'j':
11049 case 'o':
11050 case 'u':
11051 printf ("%d", imm);
11052 continue;
11053
11054 case '<':
11055 case '>':
11056 printf ("$%d", shamt);
11057 continue;
11058
11059 default:
11060 internalError ();
11061 }
11062 break;
11063 }
11064 return;
11065 }
11066 }
11067 printf (_("%08lx UNDEFINED\n"), oc);
11068}
11069#endif
11070
11071static symbolS *
11072get_symbol ()
11073{
11074 int c;
11075 char *name;
11076 symbolS *p;
11077
11078 name = input_line_pointer;
11079 c = get_symbol_end ();
11080 p = (symbolS *) symbol_find_or_make (name);
11081 *input_line_pointer = c;
11082 return p;
11083}
11084
11085/* Align the current frag to a given power of two. The MIPS assembler
11086 also automatically adjusts any preceding label. */
11087
11088static void
11089mips_align (to, fill, label)
11090 int to;
11091 int fill;
11092 symbolS *label;
11093{
11094 mips_emit_delays (false);
11095 frag_align (to, fill, 0);
11096 record_alignment (now_seg, to);
11097 if (label != NULL)
11098 {
11099 assert (S_GET_SEGMENT (label) == now_seg);
49309057 11100 symbol_set_frag (label, frag_now);
252b5132
RH
11101 S_SET_VALUE (label, (valueT) frag_now_fix ());
11102 }
11103}
11104
11105/* Align to a given power of two. .align 0 turns off the automatic
11106 alignment used by the data creating pseudo-ops. */
11107
11108static void
11109s_align (x)
43841e91 11110 int x ATTRIBUTE_UNUSED;
252b5132
RH
11111{
11112 register int temp;
11113 register long temp_fill;
11114 long max_alignment = 15;
11115
11116 /*
11117
11118 o Note that the assembler pulls down any immediately preceeding label
11119 to the aligned address.
11120 o It's not documented but auto alignment is reinstated by
11121 a .align pseudo instruction.
11122 o Note also that after auto alignment is turned off the mips assembler
11123 issues an error on attempt to assemble an improperly aligned data item.
11124 We don't.
11125
11126 */
11127
11128 temp = get_absolute_expression ();
11129 if (temp > max_alignment)
11130 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
11131 else if (temp < 0)
11132 {
11133 as_warn (_("Alignment negative: 0 assumed."));
11134 temp = 0;
11135 }
11136 if (*input_line_pointer == ',')
11137 {
f9419b05 11138 ++input_line_pointer;
252b5132
RH
11139 temp_fill = get_absolute_expression ();
11140 }
11141 else
11142 temp_fill = 0;
11143 if (temp)
11144 {
11145 auto_align = 1;
11146 mips_align (temp, (int) temp_fill,
11147 insn_labels != NULL ? insn_labels->label : NULL);
11148 }
11149 else
11150 {
11151 auto_align = 0;
11152 }
11153
11154 demand_empty_rest_of_line ();
11155}
11156
11157void
11158mips_flush_pending_output ()
11159{
11160 mips_emit_delays (false);
11161 mips_clear_insn_labels ();
11162}
11163
11164static void
11165s_change_sec (sec)
11166 int sec;
11167{
11168 segT seg;
11169
11170 /* When generating embedded PIC code, we only use the .text, .lit8,
11171 .sdata and .sbss sections. We change the .data and .rdata
11172 pseudo-ops to use .sdata. */
11173 if (mips_pic == EMBEDDED_PIC
11174 && (sec == 'd' || sec == 'r'))
11175 sec = 's';
11176
11177#ifdef OBJ_ELF
11178 /* The ELF backend needs to know that we are changing sections, so
11179 that .previous works correctly. We could do something like check
b6ff326e 11180 for an obj_section_change_hook macro, but that might be confusing
252b5132
RH
11181 as it would not be appropriate to use it in the section changing
11182 functions in read.c, since obj-elf.c intercepts those. FIXME:
11183 This should be cleaner, somehow. */
11184 obj_elf_section_change_hook ();
11185#endif
11186
11187 mips_emit_delays (false);
11188 switch (sec)
11189 {
11190 case 't':
11191 s_text (0);
11192 break;
11193 case 'd':
11194 s_data (0);
11195 break;
11196 case 'b':
11197 subseg_set (bss_section, (subsegT) get_absolute_expression ());
11198 demand_empty_rest_of_line ();
11199 break;
11200
11201 case 'r':
11202 if (USE_GLOBAL_POINTER_OPT)
11203 {
11204 seg = subseg_new (RDATA_SECTION_NAME,
11205 (subsegT) get_absolute_expression ());
11206 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11207 {
11208 bfd_set_section_flags (stdoutput, seg,
11209 (SEC_ALLOC
11210 | SEC_LOAD
11211 | SEC_READONLY
11212 | SEC_RELOC
11213 | SEC_DATA));
11214 if (strcmp (TARGET_OS, "elf") != 0)
e799a695 11215 record_alignment (seg, 4);
252b5132
RH
11216 }
11217 demand_empty_rest_of_line ();
11218 }
11219 else
11220 {
11221 as_bad (_("No read only data section in this object file format"));
11222 demand_empty_rest_of_line ();
11223 return;
11224 }
11225 break;
11226
11227 case 's':
11228 if (USE_GLOBAL_POINTER_OPT)
11229 {
11230 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
11231 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11232 {
11233 bfd_set_section_flags (stdoutput, seg,
11234 SEC_ALLOC | SEC_LOAD | SEC_RELOC
11235 | SEC_DATA);
11236 if (strcmp (TARGET_OS, "elf") != 0)
e799a695 11237 record_alignment (seg, 4);
252b5132
RH
11238 }
11239 demand_empty_rest_of_line ();
11240 break;
11241 }
11242 else
11243 {
11244 as_bad (_("Global pointers not supported; recompile -G 0"));
11245 demand_empty_rest_of_line ();
11246 return;
11247 }
11248 }
11249
11250 auto_align = 1;
11251}
cca86cc8
SC
11252
11253void
11254s_change_section (ignore)
11255 int ignore ATTRIBUTE_UNUSED;
11256{
11257 expressionS rep_exp;
11258
11259 char *section_name;
11260 char c;
11261 char *next_c;
11262 char *p;
11263 int section_type;
11264 int section_flag;
11265 int section_entry_size;
11266 int section_alignment;
11267 int log = -1;
11268 flagword flags;
11269
11270 section_name = input_line_pointer;
11271 c = get_symbol_end ();
11272 next_c = input_line_pointer + 1;
11273 /* just after name is now '\0' */
11274 p = input_line_pointer;
11275
11276 /* Do we have .section Name<,"flags"> */
11277 if (c == '\n' || (c == ',' && *next_c == '"') || c == '"')
11278 {
11279 *p = c;
11280 input_line_pointer = section_name;
11281 obj_elf_section (ignore);
11282 return;
11283 }
11284 input_line_pointer++;
11285
11286 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
11287 if (c == ',')
11288 section_type = get_absolute_expression ();
11289 else
11290 section_type = 0;
11291 if (*input_line_pointer++ == ',')
11292 section_flag = get_absolute_expression ();
11293 else
11294 section_flag = 0;
11295 if (*input_line_pointer++ == ',')
11296 section_entry_size = get_absolute_expression ();
11297 else
11298 section_entry_size = 0;
11299 if (*input_line_pointer++ == ',')
11300 section_alignment = get_absolute_expression ();
11301 else
11302 section_alignment = 0;
11303
11304 obj_elf_change_section (section_name, section_type, section_flag,
11305 section_entry_size, 0, 0, 0);
11306}
252b5132
RH
11307
11308void
11309mips_enable_auto_align ()
11310{
11311 auto_align = 1;
11312}
11313
11314static void
11315s_cons (log_size)
11316 int log_size;
11317{
11318 symbolS *label;
11319
11320 label = insn_labels != NULL ? insn_labels->label : NULL;
11321 mips_emit_delays (false);
11322 if (log_size > 0 && auto_align)
11323 mips_align (log_size, 0, label);
11324 mips_clear_insn_labels ();
11325 cons (1 << log_size);
11326}
11327
11328static void
11329s_float_cons (type)
11330 int type;
11331{
11332 symbolS *label;
11333
11334 label = insn_labels != NULL ? insn_labels->label : NULL;
11335
11336 mips_emit_delays (false);
11337
11338 if (auto_align)
49309057
ILT
11339 {
11340 if (type == 'd')
11341 mips_align (3, 0, label);
11342 else
11343 mips_align (2, 0, label);
11344 }
252b5132
RH
11345
11346 mips_clear_insn_labels ();
11347
11348 float_cons (type);
11349}
11350
11351/* Handle .globl. We need to override it because on Irix 5 you are
11352 permitted to say
11353 .globl foo .text
11354 where foo is an undefined symbol, to mean that foo should be
11355 considered to be the address of a function. */
11356
11357static void
11358s_mips_globl (x)
43841e91 11359 int x ATTRIBUTE_UNUSED;
252b5132
RH
11360{
11361 char *name;
11362 int c;
11363 symbolS *symbolP;
11364 flagword flag;
11365
11366 name = input_line_pointer;
11367 c = get_symbol_end ();
11368 symbolP = symbol_find_or_make (name);
11369 *input_line_pointer = c;
11370 SKIP_WHITESPACE ();
11371
11372 /* On Irix 5, every global symbol that is not explicitly labelled as
11373 being a function is apparently labelled as being an object. */
11374 flag = BSF_OBJECT;
11375
11376 if (! is_end_of_line[(unsigned char) *input_line_pointer])
11377 {
11378 char *secname;
11379 asection *sec;
11380
11381 secname = input_line_pointer;
11382 c = get_symbol_end ();
11383 sec = bfd_get_section_by_name (stdoutput, secname);
11384 if (sec == NULL)
11385 as_bad (_("%s: no such section"), secname);
11386 *input_line_pointer = c;
11387
11388 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
11389 flag = BSF_FUNCTION;
11390 }
11391
49309057 11392 symbol_get_bfdsym (symbolP)->flags |= flag;
252b5132
RH
11393
11394 S_SET_EXTERNAL (symbolP);
11395 demand_empty_rest_of_line ();
11396}
11397
11398static void
11399s_option (x)
43841e91 11400 int x ATTRIBUTE_UNUSED;
252b5132
RH
11401{
11402 char *opt;
11403 char c;
11404
11405 opt = input_line_pointer;
11406 c = get_symbol_end ();
11407
11408 if (*opt == 'O')
11409 {
11410 /* FIXME: What does this mean? */
11411 }
11412 else if (strncmp (opt, "pic", 3) == 0)
11413 {
11414 int i;
11415
11416 i = atoi (opt + 3);
11417 if (i == 0)
11418 mips_pic = NO_PIC;
11419 else if (i == 2)
11420 mips_pic = SVR4_PIC;
11421 else
11422 as_bad (_(".option pic%d not supported"), i);
11423
11424 if (USE_GLOBAL_POINTER_OPT && mips_pic == SVR4_PIC)
11425 {
11426 if (g_switch_seen && g_switch_value != 0)
11427 as_warn (_("-G may not be used with SVR4 PIC code"));
11428 g_switch_value = 0;
11429 bfd_set_gp_size (stdoutput, 0);
11430 }
11431 }
11432 else
11433 as_warn (_("Unrecognized option \"%s\""), opt);
11434
11435 *input_line_pointer = c;
11436 demand_empty_rest_of_line ();
11437}
11438
11439/* This structure is used to hold a stack of .set values. */
11440
e972090a
NC
11441struct mips_option_stack
11442{
252b5132
RH
11443 struct mips_option_stack *next;
11444 struct mips_set_options options;
11445};
11446
11447static struct mips_option_stack *mips_opts_stack;
11448
11449/* Handle the .set pseudo-op. */
11450
11451static void
11452s_mipsset (x)
43841e91 11453 int x ATTRIBUTE_UNUSED;
252b5132
RH
11454{
11455 char *name = input_line_pointer, ch;
11456
11457 while (!is_end_of_line[(unsigned char) *input_line_pointer])
f9419b05 11458 ++input_line_pointer;
252b5132
RH
11459 ch = *input_line_pointer;
11460 *input_line_pointer = '\0';
11461
11462 if (strcmp (name, "reorder") == 0)
11463 {
11464 if (mips_opts.noreorder && prev_nop_frag != NULL)
11465 {
11466 /* If we still have pending nops, we can discard them. The
11467 usual nop handling will insert any that are still
bdaaa2e1 11468 needed. */
252b5132
RH
11469 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11470 * (mips_opts.mips16 ? 2 : 4));
11471 prev_nop_frag = NULL;
11472 }
11473 mips_opts.noreorder = 0;
11474 }
11475 else if (strcmp (name, "noreorder") == 0)
11476 {
11477 mips_emit_delays (true);
11478 mips_opts.noreorder = 1;
11479 mips_any_noreorder = 1;
11480 }
11481 else if (strcmp (name, "at") == 0)
11482 {
11483 mips_opts.noat = 0;
11484 }
11485 else if (strcmp (name, "noat") == 0)
11486 {
11487 mips_opts.noat = 1;
11488 }
11489 else if (strcmp (name, "macro") == 0)
11490 {
11491 mips_opts.warn_about_macros = 0;
11492 }
11493 else if (strcmp (name, "nomacro") == 0)
11494 {
11495 if (mips_opts.noreorder == 0)
11496 as_bad (_("`noreorder' must be set before `nomacro'"));
11497 mips_opts.warn_about_macros = 1;
11498 }
11499 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
11500 {
11501 mips_opts.nomove = 0;
11502 }
11503 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
11504 {
11505 mips_opts.nomove = 1;
11506 }
11507 else if (strcmp (name, "bopt") == 0)
11508 {
11509 mips_opts.nobopt = 0;
11510 }
11511 else if (strcmp (name, "nobopt") == 0)
11512 {
11513 mips_opts.nobopt = 1;
11514 }
11515 else if (strcmp (name, "mips16") == 0
11516 || strcmp (name, "MIPS-16") == 0)
11517 mips_opts.mips16 = 1;
11518 else if (strcmp (name, "nomips16") == 0
11519 || strcmp (name, "noMIPS-16") == 0)
11520 mips_opts.mips16 = 0;
1f25f5d3
CD
11521 else if (strcmp (name, "mips3d") == 0)
11522 mips_opts.ase_mips3d = 1;
11523 else if (strcmp (name, "nomips3d") == 0)
11524 mips_opts.ase_mips3d = 0;
a4672219
TS
11525 else if (strcmp (name, "mdmx") == 0)
11526 mips_opts.ase_mdmx = 1;
11527 else if (strcmp (name, "nomdmx") == 0)
11528 mips_opts.ase_mdmx = 0;
252b5132
RH
11529 else if (strncmp (name, "mips", 4) == 0)
11530 {
11531 int isa;
11532
11533 /* Permit the user to change the ISA on the fly. Needless to
11534 say, misuse can cause serious problems. */
11535 isa = atoi (name + 4);
553178e4 11536 switch (isa)
98d3f06f
KH
11537 {
11538 case 0:
11539 mips_opts.gp32 = file_mips_gp32;
11540 mips_opts.fp32 = file_mips_fp32;
98d3f06f
KH
11541 break;
11542 case 1:
11543 case 2:
11544 case 32:
11545 mips_opts.gp32 = 1;
11546 mips_opts.fp32 = 1;
11547 break;
11548 case 3:
11549 case 4:
11550 case 5:
11551 case 64:
98d3f06f
KH
11552 mips_opts.gp32 = 0;
11553 mips_opts.fp32 = 0;
11554 break;
11555 default:
11556 as_bad (_("unknown ISA level %s"), name + 4);
11557 break;
11558 }
553178e4 11559
e7af610e 11560 switch (isa)
98d3f06f
KH
11561 {
11562 case 0: mips_opts.isa = file_mips_isa; break;
11563 case 1: mips_opts.isa = ISA_MIPS1; break;
11564 case 2: mips_opts.isa = ISA_MIPS2; break;
11565 case 3: mips_opts.isa = ISA_MIPS3; break;
11566 case 4: mips_opts.isa = ISA_MIPS4; break;
11567 case 5: mips_opts.isa = ISA_MIPS5; break;
11568 case 32: mips_opts.isa = ISA_MIPS32; break;
11569 case 64: mips_opts.isa = ISA_MIPS64; break;
11570 default: as_bad (_("unknown ISA level %s"), name + 4); break;
11571 }
252b5132
RH
11572 }
11573 else if (strcmp (name, "autoextend") == 0)
11574 mips_opts.noautoextend = 0;
11575 else if (strcmp (name, "noautoextend") == 0)
11576 mips_opts.noautoextend = 1;
11577 else if (strcmp (name, "push") == 0)
11578 {
11579 struct mips_option_stack *s;
11580
11581 s = (struct mips_option_stack *) xmalloc (sizeof *s);
11582 s->next = mips_opts_stack;
11583 s->options = mips_opts;
11584 mips_opts_stack = s;
11585 }
11586 else if (strcmp (name, "pop") == 0)
11587 {
11588 struct mips_option_stack *s;
11589
11590 s = mips_opts_stack;
11591 if (s == NULL)
11592 as_bad (_(".set pop with no .set push"));
11593 else
11594 {
11595 /* If we're changing the reorder mode we need to handle
11596 delay slots correctly. */
11597 if (s->options.noreorder && ! mips_opts.noreorder)
11598 mips_emit_delays (true);
11599 else if (! s->options.noreorder && mips_opts.noreorder)
11600 {
11601 if (prev_nop_frag != NULL)
11602 {
11603 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11604 * (mips_opts.mips16 ? 2 : 4));
11605 prev_nop_frag = NULL;
11606 }
11607 }
11608
11609 mips_opts = s->options;
11610 mips_opts_stack = s->next;
11611 free (s);
11612 }
11613 }
11614 else
11615 {
11616 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
11617 }
11618 *input_line_pointer = ch;
11619 demand_empty_rest_of_line ();
11620}
11621
11622/* Handle the .abicalls pseudo-op. I believe this is equivalent to
11623 .option pic2. It means to generate SVR4 PIC calls. */
11624
11625static void
11626s_abicalls (ignore)
43841e91 11627 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
11628{
11629 mips_pic = SVR4_PIC;
11630 if (USE_GLOBAL_POINTER_OPT)
11631 {
11632 if (g_switch_seen && g_switch_value != 0)
11633 as_warn (_("-G may not be used with SVR4 PIC code"));
11634 g_switch_value = 0;
11635 }
11636 bfd_set_gp_size (stdoutput, 0);
11637 demand_empty_rest_of_line ();
11638}
11639
11640/* Handle the .cpload pseudo-op. This is used when generating SVR4
11641 PIC code. It sets the $gp register for the function based on the
11642 function address, which is in the register named in the argument.
11643 This uses a relocation against _gp_disp, which is handled specially
11644 by the linker. The result is:
11645 lui $gp,%hi(_gp_disp)
11646 addiu $gp,$gp,%lo(_gp_disp)
11647 addu $gp,$gp,.cpload argument
11648 The .cpload argument is normally $25 == $t9. */
11649
11650static void
11651s_cpload (ignore)
43841e91 11652 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
11653{
11654 expressionS ex;
11655 int icnt = 0;
11656
6478892d
TS
11657 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
11658 .cpload is ignored. */
11659 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
11660 {
11661 s_ignore (0);
11662 return;
11663 }
11664
d3ecfc59 11665 /* .cpload should be in a .set noreorder section. */
252b5132
RH
11666 if (mips_opts.noreorder == 0)
11667 as_warn (_(".cpload not in noreorder section"));
11668
11669 ex.X_op = O_symbol;
11670 ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
11671 ex.X_op_symbol = NULL;
11672 ex.X_add_number = 0;
11673
11674 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
49309057 11675 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
252b5132 11676
c9914766
TS
11677 macro_build_lui (NULL, &icnt, &ex, mips_gp_register);
11678 macro_build ((char *) NULL, &icnt, &ex, "addiu", "t,r,j",
11679 mips_gp_register, mips_gp_register, (int) BFD_RELOC_LO16);
252b5132
RH
11680
11681 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "addu", "d,v,t",
c9914766 11682 mips_gp_register, mips_gp_register, tc_get_register (0));
252b5132
RH
11683
11684 demand_empty_rest_of_line ();
11685}
11686
6478892d
TS
11687/* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
11688 .cpsetup $reg1, offset|$reg2, label
11689
11690 If offset is given, this results in:
11691 sd $gp, offset($sp)
956cd1d6 11692 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
11693 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
11694 daddu $gp, $gp, $reg1
6478892d
TS
11695
11696 If $reg2 is given, this results in:
11697 daddu $reg2, $gp, $0
956cd1d6 11698 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
11699 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
11700 daddu $gp, $gp, $reg1
11701 $reg1 is normally $25 == $t9. */
6478892d
TS
11702static void
11703s_cpsetup (ignore)
11704 int ignore ATTRIBUTE_UNUSED;
11705{
11706 expressionS ex_off;
11707 expressionS ex_sym;
11708 int reg1;
11709 int icnt = 0;
f21f8242 11710 char *f;
6478892d 11711
8586fc66 11712 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
6478892d
TS
11713 We also need NewABI support. */
11714 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11715 {
11716 s_ignore (0);
11717 return;
11718 }
11719
11720 reg1 = tc_get_register (0);
11721 SKIP_WHITESPACE ();
11722 if (*input_line_pointer != ',')
11723 {
11724 as_bad (_("missing argument separator ',' for .cpsetup"));
11725 return;
11726 }
11727 else
80245285 11728 ++input_line_pointer;
6478892d
TS
11729 SKIP_WHITESPACE ();
11730 if (*input_line_pointer == '$')
80245285
TS
11731 {
11732 mips_cpreturn_register = tc_get_register (0);
11733 mips_cpreturn_offset = -1;
11734 }
6478892d 11735 else
80245285
TS
11736 {
11737 mips_cpreturn_offset = get_absolute_expression ();
11738 mips_cpreturn_register = -1;
11739 }
6478892d
TS
11740 SKIP_WHITESPACE ();
11741 if (*input_line_pointer != ',')
11742 {
11743 as_bad (_("missing argument separator ',' for .cpsetup"));
11744 return;
11745 }
11746 else
f9419b05 11747 ++input_line_pointer;
6478892d 11748 SKIP_WHITESPACE ();
f21f8242 11749 expression (&ex_sym);
6478892d
TS
11750
11751 if (mips_cpreturn_register == -1)
11752 {
11753 ex_off.X_op = O_constant;
11754 ex_off.X_add_symbol = NULL;
11755 ex_off.X_op_symbol = NULL;
11756 ex_off.X_add_number = mips_cpreturn_offset;
11757
11758 macro_build ((char *) NULL, &icnt, &ex_off, "sd", "t,o(b)",
11759 mips_gp_register, (int) BFD_RELOC_LO16, SP);
11760 }
11761 else
11762 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
11763 "d,v,t", mips_cpreturn_register, mips_gp_register, 0);
11764
f21f8242
AO
11765 /* Ensure there's room for the next two instructions, so that `f'
11766 doesn't end up with an address in the wrong frag. */
11767 frag_grow (8);
11768 f = frag_more (0);
6478892d
TS
11769 macro_build ((char *) NULL, &icnt, &ex_sym, "lui", "t,u", mips_gp_register,
11770 (int) BFD_RELOC_GPREL16);
f21f8242
AO
11771 fix_new (frag_now, f - frag_now->fr_literal,
11772 0, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
11773 fix_new (frag_now, f - frag_now->fr_literal,
11774 0, NULL, 0, 0, BFD_RELOC_HI16_S);
11775
11776 f = frag_more (0);
6478892d
TS
11777 macro_build ((char *) NULL, &icnt, &ex_sym, "addiu", "t,r,j",
11778 mips_gp_register, mips_gp_register, (int) BFD_RELOC_GPREL16);
f21f8242
AO
11779 fix_new (frag_now, f - frag_now->fr_literal,
11780 0, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
11781 fix_new (frag_now, f - frag_now->fr_literal,
11782 0, NULL, 0, 0, BFD_RELOC_LO16);
11783
8586fc66
TS
11784 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
11785 HAVE_64BIT_ADDRESSES ? "daddu" : "addu", "d,v,t",
11786 mips_gp_register, mips_gp_register, reg1);
6478892d
TS
11787
11788 demand_empty_rest_of_line ();
11789}
11790
11791static void
11792s_cplocal (ignore)
11793 int ignore ATTRIBUTE_UNUSED;
11794{
11795 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
11796 .cplocal is ignored. */
11797 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11798 {
11799 s_ignore (0);
11800 return;
11801 }
11802
11803 mips_gp_register = tc_get_register (0);
85b51719 11804 demand_empty_rest_of_line ();
6478892d
TS
11805}
11806
252b5132
RH
11807/* Handle the .cprestore pseudo-op. This stores $gp into a given
11808 offset from $sp. The offset is remembered, and after making a PIC
11809 call $gp is restored from that location. */
11810
11811static void
11812s_cprestore (ignore)
43841e91 11813 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
11814{
11815 expressionS ex;
11816 int icnt = 0;
11817
6478892d 11818 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
c9914766 11819 .cprestore is ignored. */
6478892d 11820 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
11821 {
11822 s_ignore (0);
11823 return;
11824 }
11825
11826 mips_cprestore_offset = get_absolute_expression ();
7a621144 11827 mips_cprestore_valid = 1;
252b5132
RH
11828
11829 ex.X_op = O_constant;
11830 ex.X_add_symbol = NULL;
11831 ex.X_op_symbol = NULL;
11832 ex.X_add_number = mips_cprestore_offset;
11833
c9914766
TS
11834 macro_build ((char *) NULL, &icnt, &ex, HAVE_32BIT_ADDRESSES ? "sw" : "sd",
11835 "t,o(b)", mips_gp_register, (int) BFD_RELOC_LO16, SP);
252b5132
RH
11836
11837 demand_empty_rest_of_line ();
11838}
11839
6478892d
TS
11840/* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
11841 was given in the preceeding .gpsetup, it results in:
11842 ld $gp, offset($sp)
76b3015f 11843
6478892d
TS
11844 If a register $reg2 was given there, it results in:
11845 daddiu $gp, $gp, $reg2
11846 */
11847static void
11848s_cpreturn (ignore)
11849 int ignore ATTRIBUTE_UNUSED;
11850{
11851 expressionS ex;
11852 int icnt = 0;
11853
11854 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
11855 We also need NewABI support. */
11856 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11857 {
11858 s_ignore (0);
11859 return;
11860 }
11861
11862 if (mips_cpreturn_register == -1)
11863 {
11864 ex.X_op = O_constant;
11865 ex.X_add_symbol = NULL;
11866 ex.X_op_symbol = NULL;
11867 ex.X_add_number = mips_cpreturn_offset;
11868
11869 macro_build ((char *) NULL, &icnt, &ex, "ld", "t,o(b)",
11870 mips_gp_register, (int) BFD_RELOC_LO16, SP);
11871 }
11872 else
11873 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
11874 "d,v,t", mips_gp_register, mips_cpreturn_register, 0);
11875
11876 demand_empty_rest_of_line ();
11877}
11878
11879/* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
11880 code. It sets the offset to use in gp_rel relocations. */
11881
11882static void
11883s_gpvalue (ignore)
11884 int ignore ATTRIBUTE_UNUSED;
11885{
11886 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
11887 We also need NewABI support. */
11888 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11889 {
11890 s_ignore (0);
11891 return;
11892 }
11893
def2e0dd 11894 mips_gprel_offset = get_absolute_expression ();
6478892d
TS
11895
11896 demand_empty_rest_of_line ();
11897}
11898
252b5132
RH
11899/* Handle the .gpword pseudo-op. This is used when generating PIC
11900 code. It generates a 32 bit GP relative reloc. */
11901
11902static void
11903s_gpword (ignore)
43841e91 11904 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
11905{
11906 symbolS *label;
11907 expressionS ex;
11908 char *p;
11909
11910 /* When not generating PIC code, this is treated as .word. */
11911 if (mips_pic != SVR4_PIC)
11912 {
11913 s_cons (2);
11914 return;
11915 }
11916
11917 label = insn_labels != NULL ? insn_labels->label : NULL;
11918 mips_emit_delays (true);
11919 if (auto_align)
11920 mips_align (2, 0, label);
11921 mips_clear_insn_labels ();
11922
11923 expression (&ex);
11924
11925 if (ex.X_op != O_symbol || ex.X_add_number != 0)
11926 {
11927 as_bad (_("Unsupported use of .gpword"));
11928 ignore_rest_of_line ();
11929 }
11930
11931 p = frag_more (4);
11932 md_number_to_chars (p, (valueT) 0, 4);
c9914766 11933 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, false,
cdf6fd85 11934 BFD_RELOC_GPREL32);
252b5132
RH
11935
11936 demand_empty_rest_of_line ();
11937}
11938
11939/* Handle the .cpadd pseudo-op. This is used when dealing with switch
11940 tables in SVR4 PIC code. */
11941
11942static void
11943s_cpadd (ignore)
43841e91 11944 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
11945{
11946 int icnt = 0;
11947 int reg;
11948
6478892d
TS
11949 /* This is ignored when not generating SVR4 PIC code or if this is NewABI
11950 code. */
11951 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
11952 {
11953 s_ignore (0);
11954 return;
11955 }
11956
11957 /* Add $gp to the register named as an argument. */
11958 reg = tc_get_register (0);
11959 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 11960 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 11961 "d,v,t", reg, reg, mips_gp_register);
252b5132 11962
bdaaa2e1 11963 demand_empty_rest_of_line ();
252b5132
RH
11964}
11965
11966/* Handle the .insn pseudo-op. This marks instruction labels in
11967 mips16 mode. This permits the linker to handle them specially,
11968 such as generating jalx instructions when needed. We also make
11969 them odd for the duration of the assembly, in order to generate the
11970 right sort of code. We will make them even in the adjust_symtab
11971 routine, while leaving them marked. This is convenient for the
11972 debugger and the disassembler. The linker knows to make them odd
11973 again. */
11974
11975static void
11976s_insn (ignore)
43841e91 11977 int ignore ATTRIBUTE_UNUSED;
252b5132 11978{
f9419b05 11979 mips16_mark_labels ();
252b5132
RH
11980
11981 demand_empty_rest_of_line ();
11982}
11983
11984/* Handle a .stabn directive. We need these in order to mark a label
11985 as being a mips16 text label correctly. Sometimes the compiler
11986 will emit a label, followed by a .stabn, and then switch sections.
11987 If the label and .stabn are in mips16 mode, then the label is
11988 really a mips16 text label. */
11989
11990static void
11991s_mips_stab (type)
11992 int type;
11993{
f9419b05 11994 if (type == 'n')
252b5132
RH
11995 mips16_mark_labels ();
11996
11997 s_stab (type);
11998}
11999
12000/* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
12001 */
12002
12003static void
12004s_mips_weakext (ignore)
43841e91 12005 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
12006{
12007 char *name;
12008 int c;
12009 symbolS *symbolP;
12010 expressionS exp;
12011
12012 name = input_line_pointer;
12013 c = get_symbol_end ();
12014 symbolP = symbol_find_or_make (name);
12015 S_SET_WEAK (symbolP);
12016 *input_line_pointer = c;
12017
12018 SKIP_WHITESPACE ();
12019
12020 if (! is_end_of_line[(unsigned char) *input_line_pointer])
12021 {
12022 if (S_IS_DEFINED (symbolP))
12023 {
956cd1d6 12024 as_bad ("ignoring attempt to redefine symbol %s",
252b5132
RH
12025 S_GET_NAME (symbolP));
12026 ignore_rest_of_line ();
12027 return;
12028 }
bdaaa2e1 12029
252b5132
RH
12030 if (*input_line_pointer == ',')
12031 {
12032 ++input_line_pointer;
12033 SKIP_WHITESPACE ();
12034 }
bdaaa2e1 12035
252b5132
RH
12036 expression (&exp);
12037 if (exp.X_op != O_symbol)
12038 {
12039 as_bad ("bad .weakext directive");
98d3f06f 12040 ignore_rest_of_line ();
252b5132
RH
12041 return;
12042 }
49309057 12043 symbol_set_value_expression (symbolP, &exp);
252b5132
RH
12044 }
12045
12046 demand_empty_rest_of_line ();
12047}
12048
12049/* Parse a register string into a number. Called from the ECOFF code
12050 to parse .frame. The argument is non-zero if this is the frame
12051 register, so that we can record it in mips_frame_reg. */
12052
12053int
12054tc_get_register (frame)
12055 int frame;
12056{
12057 int reg;
12058
12059 SKIP_WHITESPACE ();
12060 if (*input_line_pointer++ != '$')
12061 {
12062 as_warn (_("expected `$'"));
85b51719 12063 reg = ZERO;
252b5132 12064 }
3882b010 12065 else if (ISDIGIT (*input_line_pointer))
252b5132
RH
12066 {
12067 reg = get_absolute_expression ();
12068 if (reg < 0 || reg >= 32)
12069 {
12070 as_warn (_("Bad register number"));
85b51719 12071 reg = ZERO;
252b5132
RH
12072 }
12073 }
12074 else
12075 {
76db943d 12076 if (strncmp (input_line_pointer, "ra", 2) == 0)
85b51719
TS
12077 {
12078 reg = RA;
12079 input_line_pointer += 2;
12080 }
76db943d 12081 else if (strncmp (input_line_pointer, "fp", 2) == 0)
85b51719
TS
12082 {
12083 reg = FP;
12084 input_line_pointer += 2;
12085 }
252b5132 12086 else if (strncmp (input_line_pointer, "sp", 2) == 0)
85b51719
TS
12087 {
12088 reg = SP;
12089 input_line_pointer += 2;
12090 }
252b5132 12091 else if (strncmp (input_line_pointer, "gp", 2) == 0)
85b51719
TS
12092 {
12093 reg = GP;
12094 input_line_pointer += 2;
12095 }
252b5132 12096 else if (strncmp (input_line_pointer, "at", 2) == 0)
85b51719
TS
12097 {
12098 reg = AT;
12099 input_line_pointer += 2;
12100 }
12101 else if (strncmp (input_line_pointer, "kt0", 3) == 0)
12102 {
12103 reg = KT0;
12104 input_line_pointer += 3;
12105 }
12106 else if (strncmp (input_line_pointer, "kt1", 3) == 0)
12107 {
12108 reg = KT1;
12109 input_line_pointer += 3;
12110 }
12111 else if (strncmp (input_line_pointer, "zero", 4) == 0)
12112 {
12113 reg = ZERO;
12114 input_line_pointer += 4;
12115 }
252b5132
RH
12116 else
12117 {
12118 as_warn (_("Unrecognized register name"));
85b51719
TS
12119 reg = ZERO;
12120 while (ISALNUM(*input_line_pointer))
12121 input_line_pointer++;
252b5132 12122 }
252b5132
RH
12123 }
12124 if (frame)
7a621144
DJ
12125 {
12126 mips_frame_reg = reg != 0 ? reg : SP;
12127 mips_frame_reg_valid = 1;
12128 mips_cprestore_valid = 0;
12129 }
252b5132
RH
12130 return reg;
12131}
12132
12133valueT
12134md_section_align (seg, addr)
12135 asection *seg;
12136 valueT addr;
12137{
12138 int align = bfd_get_section_alignment (stdoutput, seg);
12139
12140#ifdef OBJ_ELF
12141 /* We don't need to align ELF sections to the full alignment.
12142 However, Irix 5 may prefer that we align them at least to a 16
12143 byte boundary. We don't bother to align the sections if we are
12144 targeted for an embedded system. */
12145 if (strcmp (TARGET_OS, "elf") == 0)
12146 return addr;
12147 if (align > 4)
12148 align = 4;
12149#endif
12150
12151 return ((addr + (1 << align) - 1) & (-1 << align));
12152}
12153
12154/* Utility routine, called from above as well. If called while the
12155 input file is still being read, it's only an approximation. (For
12156 example, a symbol may later become defined which appeared to be
12157 undefined earlier.) */
12158
12159static int
12160nopic_need_relax (sym, before_relaxing)
12161 symbolS *sym;
12162 int before_relaxing;
12163{
12164 if (sym == 0)
12165 return 0;
12166
6478892d 12167 if (USE_GLOBAL_POINTER_OPT && g_switch_value > 0)
252b5132
RH
12168 {
12169 const char *symname;
12170 int change;
12171
c9914766 12172 /* Find out whether this symbol can be referenced off the $gp
252b5132
RH
12173 register. It can be if it is smaller than the -G size or if
12174 it is in the .sdata or .sbss section. Certain symbols can
c9914766 12175 not be referenced off the $gp, although it appears as though
252b5132
RH
12176 they can. */
12177 symname = S_GET_NAME (sym);
12178 if (symname != (const char *) NULL
12179 && (strcmp (symname, "eprol") == 0
12180 || strcmp (symname, "etext") == 0
12181 || strcmp (symname, "_gp") == 0
12182 || strcmp (symname, "edata") == 0
12183 || strcmp (symname, "_fbss") == 0
12184 || strcmp (symname, "_fdata") == 0
12185 || strcmp (symname, "_ftext") == 0
12186 || strcmp (symname, "end") == 0
12187 || strcmp (symname, "_gp_disp") == 0))
12188 change = 1;
12189 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
12190 && (0
12191#ifndef NO_ECOFF_DEBUGGING
49309057
ILT
12192 || (symbol_get_obj (sym)->ecoff_extern_size != 0
12193 && (symbol_get_obj (sym)->ecoff_extern_size
12194 <= g_switch_value))
252b5132
RH
12195#endif
12196 /* We must defer this decision until after the whole
12197 file has been read, since there might be a .extern
12198 after the first use of this symbol. */
12199 || (before_relaxing
12200#ifndef NO_ECOFF_DEBUGGING
49309057 12201 && symbol_get_obj (sym)->ecoff_extern_size == 0
252b5132
RH
12202#endif
12203 && S_GET_VALUE (sym) == 0)
12204 || (S_GET_VALUE (sym) != 0
12205 && S_GET_VALUE (sym) <= g_switch_value)))
12206 change = 0;
12207 else
12208 {
12209 const char *segname;
12210
12211 segname = segment_name (S_GET_SEGMENT (sym));
12212 assert (strcmp (segname, ".lit8") != 0
12213 && strcmp (segname, ".lit4") != 0);
12214 change = (strcmp (segname, ".sdata") != 0
fba2b7f9
GK
12215 && strcmp (segname, ".sbss") != 0
12216 && strncmp (segname, ".sdata.", 7) != 0
12217 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
252b5132
RH
12218 }
12219 return change;
12220 }
12221 else
c9914766 12222 /* We are not optimizing for the $gp register. */
252b5132
RH
12223 return 1;
12224}
12225
12226/* Given a mips16 variant frag FRAGP, return non-zero if it needs an
12227 extended opcode. SEC is the section the frag is in. */
12228
12229static int
12230mips16_extended_frag (fragp, sec, stretch)
12231 fragS *fragp;
12232 asection *sec;
12233 long stretch;
12234{
12235 int type;
12236 register const struct mips16_immed_operand *op;
12237 offsetT val;
12238 int mintiny, maxtiny;
12239 segT symsec;
98aa84af 12240 fragS *sym_frag;
252b5132
RH
12241
12242 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
12243 return 0;
12244 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
12245 return 1;
12246
12247 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12248 op = mips16_immed_operands;
12249 while (op->type != type)
12250 {
12251 ++op;
12252 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
12253 }
12254
12255 if (op->unsp)
12256 {
12257 if (type == '<' || type == '>' || type == '[' || type == ']')
12258 {
12259 mintiny = 1;
12260 maxtiny = 1 << op->nbits;
12261 }
12262 else
12263 {
12264 mintiny = 0;
12265 maxtiny = (1 << op->nbits) - 1;
12266 }
12267 }
12268 else
12269 {
12270 mintiny = - (1 << (op->nbits - 1));
12271 maxtiny = (1 << (op->nbits - 1)) - 1;
12272 }
12273
98aa84af 12274 sym_frag = symbol_get_frag (fragp->fr_symbol);
ac62c346 12275 val = S_GET_VALUE (fragp->fr_symbol);
98aa84af 12276 symsec = S_GET_SEGMENT (fragp->fr_symbol);
252b5132
RH
12277
12278 if (op->pcrel)
12279 {
12280 addressT addr;
12281
12282 /* We won't have the section when we are called from
12283 mips_relax_frag. However, we will always have been called
12284 from md_estimate_size_before_relax first. If this is a
12285 branch to a different section, we mark it as such. If SEC is
12286 NULL, and the frag is not marked, then it must be a branch to
12287 the same section. */
12288 if (sec == NULL)
12289 {
12290 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
12291 return 1;
12292 }
12293 else
12294 {
98aa84af 12295 /* Must have been called from md_estimate_size_before_relax. */
252b5132
RH
12296 if (symsec != sec)
12297 {
12298 fragp->fr_subtype =
12299 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12300
12301 /* FIXME: We should support this, and let the linker
12302 catch branches and loads that are out of range. */
12303 as_bad_where (fragp->fr_file, fragp->fr_line,
12304 _("unsupported PC relative reference to different section"));
12305
12306 return 1;
12307 }
98aa84af
AM
12308 if (fragp != sym_frag && sym_frag->fr_address == 0)
12309 /* Assume non-extended on the first relaxation pass.
12310 The address we have calculated will be bogus if this is
12311 a forward branch to another frag, as the forward frag
12312 will have fr_address == 0. */
12313 return 0;
252b5132
RH
12314 }
12315
12316 /* In this case, we know for sure that the symbol fragment is in
98aa84af
AM
12317 the same section. If the relax_marker of the symbol fragment
12318 differs from the relax_marker of this fragment, we have not
12319 yet adjusted the symbol fragment fr_address. We want to add
252b5132
RH
12320 in STRETCH in order to get a better estimate of the address.
12321 This particularly matters because of the shift bits. */
12322 if (stretch != 0
98aa84af 12323 && sym_frag->relax_marker != fragp->relax_marker)
252b5132
RH
12324 {
12325 fragS *f;
12326
12327 /* Adjust stretch for any alignment frag. Note that if have
12328 been expanding the earlier code, the symbol may be
12329 defined in what appears to be an earlier frag. FIXME:
12330 This doesn't handle the fr_subtype field, which specifies
12331 a maximum number of bytes to skip when doing an
12332 alignment. */
98aa84af 12333 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
252b5132
RH
12334 {
12335 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
12336 {
12337 if (stretch < 0)
12338 stretch = - ((- stretch)
12339 & ~ ((1 << (int) f->fr_offset) - 1));
12340 else
12341 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
12342 if (stretch == 0)
12343 break;
12344 }
12345 }
12346 if (f != NULL)
12347 val += stretch;
12348 }
12349
12350 addr = fragp->fr_address + fragp->fr_fix;
12351
12352 /* The base address rules are complicated. The base address of
12353 a branch is the following instruction. The base address of a
12354 PC relative load or add is the instruction itself, but if it
12355 is in a delay slot (in which case it can not be extended) use
12356 the address of the instruction whose delay slot it is in. */
12357 if (type == 'p' || type == 'q')
12358 {
12359 addr += 2;
12360
12361 /* If we are currently assuming that this frag should be
12362 extended, then, the current address is two bytes
bdaaa2e1 12363 higher. */
252b5132
RH
12364 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12365 addr += 2;
12366
12367 /* Ignore the low bit in the target, since it will be set
12368 for a text label. */
12369 if ((val & 1) != 0)
12370 --val;
12371 }
12372 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12373 addr -= 4;
12374 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12375 addr -= 2;
12376
12377 val -= addr & ~ ((1 << op->shift) - 1);
12378
12379 /* Branch offsets have an implicit 0 in the lowest bit. */
12380 if (type == 'p' || type == 'q')
12381 val /= 2;
12382
12383 /* If any of the shifted bits are set, we must use an extended
12384 opcode. If the address depends on the size of this
12385 instruction, this can lead to a loop, so we arrange to always
12386 use an extended opcode. We only check this when we are in
12387 the main relaxation loop, when SEC is NULL. */
12388 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
12389 {
12390 fragp->fr_subtype =
12391 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12392 return 1;
12393 }
12394
12395 /* If we are about to mark a frag as extended because the value
12396 is precisely maxtiny + 1, then there is a chance of an
12397 infinite loop as in the following code:
12398 la $4,foo
12399 .skip 1020
12400 .align 2
12401 foo:
12402 In this case when the la is extended, foo is 0x3fc bytes
12403 away, so the la can be shrunk, but then foo is 0x400 away, so
12404 the la must be extended. To avoid this loop, we mark the
12405 frag as extended if it was small, and is about to become
12406 extended with a value of maxtiny + 1. */
12407 if (val == ((maxtiny + 1) << op->shift)
12408 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
12409 && sec == NULL)
12410 {
12411 fragp->fr_subtype =
12412 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12413 return 1;
12414 }
12415 }
12416 else if (symsec != absolute_section && sec != NULL)
12417 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
12418
12419 if ((val & ((1 << op->shift) - 1)) != 0
12420 || val < (mintiny << op->shift)
12421 || val > (maxtiny << op->shift))
12422 return 1;
12423 else
12424 return 0;
12425}
12426
12427/* Estimate the size of a frag before relaxing. Unless this is the
12428 mips16, we are not really relaxing here, and the final size is
12429 encoded in the subtype information. For the mips16, we have to
12430 decide whether we are using an extended opcode or not. */
12431
252b5132
RH
12432int
12433md_estimate_size_before_relax (fragp, segtype)
12434 fragS *fragp;
12435 asection *segtype;
12436{
43841e91 12437 int change = 0;
8614eeee 12438 boolean linkonce = false;
252b5132
RH
12439
12440 if (RELAX_MIPS16_P (fragp->fr_subtype))
177b4a6a
AO
12441 /* We don't want to modify the EXTENDED bit here; it might get us
12442 into infinite loops. We change it only in mips_relax_frag(). */
12443 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
252b5132
RH
12444
12445 if (mips_pic == NO_PIC)
12446 {
12447 change = nopic_need_relax (fragp->fr_symbol, 0);
12448 }
12449 else if (mips_pic == SVR4_PIC)
12450 {
12451 symbolS *sym;
12452 asection *symsec;
12453
12454 sym = fragp->fr_symbol;
12455
12456 /* Handle the case of a symbol equated to another symbol. */
e0890092 12457 while (symbol_equated_reloc_p (sym))
252b5132
RH
12458 {
12459 symbolS *n;
12460
12461 /* It's possible to get a loop here in a badly written
12462 program. */
49309057 12463 n = symbol_get_value_expression (sym)->X_add_symbol;
252b5132
RH
12464 if (n == sym)
12465 break;
12466 sym = n;
12467 }
12468
12469 symsec = S_GET_SEGMENT (sym);
12470
8614eeee
UC
12471 /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
12472 if (symsec != segtype && ! S_IS_LOCAL (sym))
beae10d5
KH
12473 {
12474 if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
12475 != 0)
12476 linkonce = true;
12477
12478 /* The GNU toolchain uses an extension for ELF: a section
12479 beginning with the magic string .gnu.linkonce is a linkonce
12480 section. */
12481 if (strncmp (segment_name (symsec), ".gnu.linkonce",
12482 sizeof ".gnu.linkonce" - 1) == 0)
12483 linkonce = true;
12484 }
8614eeee 12485
252b5132
RH
12486 /* This must duplicate the test in adjust_reloc_syms. */
12487 change = (symsec != &bfd_und_section
12488 && symsec != &bfd_abs_section
426b0403 12489 && ! bfd_is_com_section (symsec)
8614eeee 12490 && !linkonce
426b0403 12491#ifdef OBJ_ELF
ea4ff978 12492 /* A global or weak symbol is treated as external. */
9151e8bf 12493 && (OUTPUT_FLAVOR != bfd_target_elf_flavour
b25a253c 12494 || (! S_IS_WEAK (sym)
bad9ca53
TS
12495 && (! S_IS_EXTERNAL (sym)
12496 || mips_pic == EMBEDDED_PIC)))
426b0403
AM
12497#endif
12498 );
252b5132
RH
12499 }
12500 else
12501 abort ();
12502
12503 if (change)
12504 {
12505 /* Record the offset to the first reloc in the fr_opcode field.
12506 This lets md_convert_frag and tc_gen_reloc know that the code
12507 must be expanded. */
12508 fragp->fr_opcode = (fragp->fr_literal
12509 + fragp->fr_fix
12510 - RELAX_OLD (fragp->fr_subtype)
12511 + RELAX_RELOC1 (fragp->fr_subtype));
12512 /* FIXME: This really needs as_warn_where. */
12513 if (RELAX_WARN (fragp->fr_subtype))
9a41af64
TS
12514 as_warn (_("AT used after \".set noat\" or macro used after "
12515 "\".set nomacro\""));
12516
12517 return RELAX_NEW (fragp->fr_subtype) - RELAX_OLD (fragp->fr_subtype);
252b5132
RH
12518 }
12519
9a41af64 12520 return 0;
252b5132
RH
12521}
12522
12523/* This is called to see whether a reloc against a defined symbol
12524 should be converted into a reloc against a section. Don't adjust
12525 MIPS16 jump relocations, so we don't have to worry about the format
12526 of the offset in the .o file. Don't adjust relocations against
12527 mips16 symbols, so that the linker can find them if it needs to set
12528 up a stub. */
12529
12530int
12531mips_fix_adjustable (fixp)
12532 fixS *fixp;
12533{
ea4ff978
L
12534#ifdef OBJ_ELF
12535 /* Prevent all adjustments to global symbols. */
46bac6de 12536 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
b25a253c 12537 && mips_pic != EMBEDDED_PIC
bad9ca53 12538 && (S_IS_EXTERNAL (fixp->fx_addsy) || S_IS_WEAK (fixp->fx_addsy)))
ea4ff978
L
12539 return 0;
12540#endif
252b5132
RH
12541 if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
12542 return 0;
12543 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
12544 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12545 return 0;
12546 if (fixp->fx_addsy == NULL)
12547 return 1;
12548#ifdef OBJ_ELF
12549 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
12550 && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
12551 && fixp->fx_subsy == NULL)
12552 return 0;
12553#endif
12554 return 1;
12555}
12556
12557/* Translate internal representation of relocation info to BFD target
12558 format. */
12559
12560arelent **
12561tc_gen_reloc (section, fixp)
43841e91 12562 asection *section ATTRIBUTE_UNUSED;
252b5132
RH
12563 fixS *fixp;
12564{
12565 static arelent *retval[4];
12566 arelent *reloc;
12567 bfd_reloc_code_real_type code;
12568
12569 reloc = retval[0] = (arelent *) xmalloc (sizeof (arelent));
12570 retval[1] = NULL;
12571
49309057
ILT
12572 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
12573 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
12574 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
12575
12576 if (mips_pic == EMBEDDED_PIC
12577 && SWITCH_TABLE (fixp))
12578 {
12579 /* For a switch table entry we use a special reloc. The addend
12580 is actually the difference between the reloc address and the
12581 subtrahend. */
12582 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
12583 if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
12584 as_fatal (_("Double check fx_r_type in tc-mips.c:tc_gen_reloc"));
12585 fixp->fx_r_type = BFD_RELOC_GPREL32;
12586 }
12587 else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16)
12588 {
4514d474
CD
12589 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
12590 reloc->addend = fixp->fx_addnumber;
252b5132 12591 else
4514d474
CD
12592 {
12593 /* We use a special addend for an internal RELLO reloc. */
12594 if (symbol_section_p (fixp->fx_addsy))
12595 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
12596 else
12597 reloc->addend = fixp->fx_addnumber + reloc->address;
12598 }
252b5132
RH
12599 }
12600 else if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
12601 {
12602 assert (fixp->fx_next != NULL
12603 && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
4514d474
CD
12604
12605 /* The reloc is relative to the RELLO; adjust the addend
252b5132 12606 accordingly. */
4514d474
CD
12607 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
12608 reloc->addend = fixp->fx_next->fx_addnumber;
252b5132 12609 else
4514d474
CD
12610 {
12611 /* We use a special addend for an internal RELHI reloc. */
12612 if (symbol_section_p (fixp->fx_addsy))
12613 reloc->addend = (fixp->fx_next->fx_frag->fr_address
12614 + fixp->fx_next->fx_where
12615 - S_GET_VALUE (fixp->fx_subsy));
12616 else
12617 reloc->addend = (fixp->fx_addnumber
12618 + fixp->fx_next->fx_frag->fr_address
12619 + fixp->fx_next->fx_where);
12620 }
252b5132 12621 }
4514d474
CD
12622 else if (fixp->fx_pcrel == 0 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
12623 reloc->addend = fixp->fx_addnumber;
252b5132
RH
12624 else
12625 {
12626 if (OUTPUT_FLAVOR != bfd_target_aout_flavour)
12627 /* A gruesome hack which is a result of the gruesome gas reloc
12628 handling. */
12629 reloc->addend = reloc->address;
12630 else
12631 reloc->addend = -reloc->address;
12632 }
12633
12634 /* If this is a variant frag, we may need to adjust the existing
12635 reloc and generate a new one. */
12636 if (fixp->fx_frag->fr_opcode != NULL
cdf6fd85 12637 && (fixp->fx_r_type == BFD_RELOC_GPREL16
252b5132
RH
12638 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
12639 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL16
12640 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
12641 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16
12642 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
6478892d
TS
12643 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_LO16)
12644 && ! HAVE_NEWABI)
252b5132
RH
12645 {
12646 arelent *reloc2;
12647
12648 assert (! RELAX_MIPS16_P (fixp->fx_frag->fr_subtype));
12649
12650 /* If this is not the last reloc in this frag, then we have two
12651 GPREL relocs, or a GOT_HI16/GOT_LO16 pair, or a
12652 CALL_HI16/CALL_LO16, both of which are being replaced. Let
12653 the second one handle all of them. */
12654 if (fixp->fx_next != NULL
12655 && fixp->fx_frag == fixp->fx_next->fx_frag)
12656 {
cdf6fd85
TS
12657 assert ((fixp->fx_r_type == BFD_RELOC_GPREL16
12658 && fixp->fx_next->fx_r_type == BFD_RELOC_GPREL16)
252b5132
RH
12659 || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
12660 && (fixp->fx_next->fx_r_type
12661 == BFD_RELOC_MIPS_GOT_LO16))
12662 || (fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
12663 && (fixp->fx_next->fx_r_type
12664 == BFD_RELOC_MIPS_CALL_LO16)));
12665 retval[0] = NULL;
12666 return retval;
12667 }
12668
12669 fixp->fx_where = fixp->fx_frag->fr_opcode - fixp->fx_frag->fr_literal;
12670 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
12671 reloc2 = retval[1] = (arelent *) xmalloc (sizeof (arelent));
12672 retval[2] = NULL;
49309057
ILT
12673 reloc2->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
12674 *reloc2->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
12675 reloc2->address = (reloc->address
12676 + (RELAX_RELOC2 (fixp->fx_frag->fr_subtype)
12677 - RELAX_RELOC1 (fixp->fx_frag->fr_subtype)));
12678 reloc2->addend = fixp->fx_addnumber;
12679 reloc2->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO16);
12680 assert (reloc2->howto != NULL);
12681
12682 if (RELAX_RELOC3 (fixp->fx_frag->fr_subtype))
12683 {
12684 arelent *reloc3;
12685
12686 reloc3 = retval[2] = (arelent *) xmalloc (sizeof (arelent));
12687 retval[3] = NULL;
12688 *reloc3 = *reloc2;
12689 reloc3->address += 4;
12690 }
12691
12692 if (mips_pic == NO_PIC)
12693 {
cdf6fd85 12694 assert (fixp->fx_r_type == BFD_RELOC_GPREL16);
252b5132
RH
12695 fixp->fx_r_type = BFD_RELOC_HI16_S;
12696 }
12697 else if (mips_pic == SVR4_PIC)
12698 {
12699 switch (fixp->fx_r_type)
12700 {
12701 default:
12702 abort ();
12703 case BFD_RELOC_MIPS_GOT16:
12704 break;
12705 case BFD_RELOC_MIPS_CALL16:
12706 case BFD_RELOC_MIPS_GOT_LO16:
12707 case BFD_RELOC_MIPS_CALL_LO16:
12708 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
12709 break;
12710 }
12711 }
12712 else
12713 abort ();
12714 }
12715
438c16b8
TS
12716 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
12717 entry to be used in the relocation's section offset. */
12718 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
252b5132
RH
12719 {
12720 reloc->address = reloc->addend;
12721 reloc->addend = 0;
12722 }
12723
12724 /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
12725 fixup_segment converted a non-PC relative reloc into a PC
12726 relative reloc. In such a case, we need to convert the reloc
12727 code. */
12728 code = fixp->fx_r_type;
12729 if (fixp->fx_pcrel)
12730 {
12731 switch (code)
12732 {
12733 case BFD_RELOC_8:
12734 code = BFD_RELOC_8_PCREL;
12735 break;
12736 case BFD_RELOC_16:
12737 code = BFD_RELOC_16_PCREL;
12738 break;
12739 case BFD_RELOC_32:
12740 code = BFD_RELOC_32_PCREL;
12741 break;
12742 case BFD_RELOC_64:
12743 code = BFD_RELOC_64_PCREL;
12744 break;
12745 case BFD_RELOC_8_PCREL:
12746 case BFD_RELOC_16_PCREL:
12747 case BFD_RELOC_32_PCREL:
12748 case BFD_RELOC_64_PCREL:
12749 case BFD_RELOC_16_PCREL_S2:
12750 case BFD_RELOC_PCREL_HI16_S:
12751 case BFD_RELOC_PCREL_LO16:
12752 break;
12753 default:
12754 as_bad_where (fixp->fx_file, fixp->fx_line,
12755 _("Cannot make %s relocation PC relative"),
12756 bfd_get_reloc_code_name (code));
12757 }
12758 }
12759
add55e1f
RS
12760#ifdef OBJ_ELF
12761 /* md_apply_fix3 has a double-subtraction hack to get
12762 bfd_install_relocation to behave nicely. GPREL relocations are
12763 handled correctly without this hack, so undo it here. We can't
12764 stop md_apply_fix3 from subtracting twice in the first place since
12765 the fake addend is required for variant frags above. */
12766 if (fixp->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour
98605598 12767 && (code == BFD_RELOC_GPREL16 || code == BFD_RELOC_MIPS16_GPREL)
add55e1f
RS
12768 && reloc->addend != 0
12769 && mips_need_elf_addend_fixup (fixp))
12770 reloc->addend += S_GET_VALUE (fixp->fx_addsy);
12771#endif
12772
252b5132
RH
12773 /* To support a PC relative reloc when generating embedded PIC code
12774 for ECOFF, we use a Cygnus extension. We check for that here to
12775 make sure that we don't let such a reloc escape normally. */
bb2d6cd7
GK
12776 if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
12777 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
252b5132
RH
12778 && code == BFD_RELOC_16_PCREL_S2
12779 && mips_pic != EMBEDDED_PIC)
12780 reloc->howto = NULL;
12781 else
12782 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
12783
12784 if (reloc->howto == NULL)
12785 {
12786 as_bad_where (fixp->fx_file, fixp->fx_line,
12787 _("Can not represent %s relocation in this object file format"),
12788 bfd_get_reloc_code_name (code));
12789 retval[0] = NULL;
12790 }
12791
12792 return retval;
12793}
12794
12795/* Relax a machine dependent frag. This returns the amount by which
12796 the current size of the frag should change. */
12797
12798int
12799mips_relax_frag (fragp, stretch)
12800 fragS *fragp;
12801 long stretch;
12802{
12803 if (! RELAX_MIPS16_P (fragp->fr_subtype))
12804 return 0;
12805
c4e7957c 12806 if (mips16_extended_frag (fragp, NULL, stretch))
252b5132
RH
12807 {
12808 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12809 return 0;
12810 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
12811 return 2;
12812 }
12813 else
12814 {
12815 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12816 return 0;
12817 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
12818 return -2;
12819 }
12820
12821 return 0;
12822}
12823
12824/* Convert a machine dependent frag. */
12825
12826void
12827md_convert_frag (abfd, asec, fragp)
43841e91 12828 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
12829 segT asec;
12830 fragS *fragp;
12831{
12832 int old, new;
12833 char *fixptr;
12834
12835 if (RELAX_MIPS16_P (fragp->fr_subtype))
12836 {
12837 int type;
12838 register const struct mips16_immed_operand *op;
12839 boolean small, ext;
12840 offsetT val;
12841 bfd_byte *buf;
12842 unsigned long insn;
12843 boolean use_extend;
12844 unsigned short extend;
12845
12846 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12847 op = mips16_immed_operands;
12848 while (op->type != type)
12849 ++op;
12850
12851 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12852 {
12853 small = false;
12854 ext = true;
12855 }
12856 else
12857 {
12858 small = true;
12859 ext = false;
12860 }
12861
6386f3a7 12862 resolve_symbol_value (fragp->fr_symbol);
252b5132
RH
12863 val = S_GET_VALUE (fragp->fr_symbol);
12864 if (op->pcrel)
12865 {
12866 addressT addr;
12867
12868 addr = fragp->fr_address + fragp->fr_fix;
12869
12870 /* The rules for the base address of a PC relative reloc are
12871 complicated; see mips16_extended_frag. */
12872 if (type == 'p' || type == 'q')
12873 {
12874 addr += 2;
12875 if (ext)
12876 addr += 2;
12877 /* Ignore the low bit in the target, since it will be
12878 set for a text label. */
12879 if ((val & 1) != 0)
12880 --val;
12881 }
12882 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12883 addr -= 4;
12884 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12885 addr -= 2;
12886
12887 addr &= ~ (addressT) ((1 << op->shift) - 1);
12888 val -= addr;
12889
12890 /* Make sure the section winds up with the alignment we have
12891 assumed. */
12892 if (op->shift > 0)
12893 record_alignment (asec, op->shift);
12894 }
12895
12896 if (ext
12897 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
12898 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
12899 as_warn_where (fragp->fr_file, fragp->fr_line,
12900 _("extended instruction in delay slot"));
12901
12902 buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
12903
12904 if (target_big_endian)
12905 insn = bfd_getb16 (buf);
12906 else
12907 insn = bfd_getl16 (buf);
12908
12909 mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
12910 RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
12911 small, ext, &insn, &use_extend, &extend);
12912
12913 if (use_extend)
12914 {
874e8986 12915 md_number_to_chars ((char *) buf, 0xf000 | extend, 2);
252b5132
RH
12916 fragp->fr_fix += 2;
12917 buf += 2;
12918 }
12919
874e8986 12920 md_number_to_chars ((char *) buf, insn, 2);
252b5132
RH
12921 fragp->fr_fix += 2;
12922 buf += 2;
12923 }
12924 else
12925 {
12926 if (fragp->fr_opcode == NULL)
12927 return;
12928
12929 old = RELAX_OLD (fragp->fr_subtype);
12930 new = RELAX_NEW (fragp->fr_subtype);
12931 fixptr = fragp->fr_literal + fragp->fr_fix;
12932
12933 if (new > 0)
12934 memcpy (fixptr - old, fixptr, new);
12935
12936 fragp->fr_fix += new - old;
12937 }
12938}
12939
12940#ifdef OBJ_ELF
12941
12942/* This function is called after the relocs have been generated.
12943 We've been storing mips16 text labels as odd. Here we convert them
12944 back to even for the convenience of the debugger. */
12945
12946void
12947mips_frob_file_after_relocs ()
12948{
12949 asymbol **syms;
12950 unsigned int count, i;
12951
12952 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
12953 return;
12954
12955 syms = bfd_get_outsymbols (stdoutput);
12956 count = bfd_get_symcount (stdoutput);
12957 for (i = 0; i < count; i++, syms++)
12958 {
12959 if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
12960 && ((*syms)->value & 1) != 0)
12961 {
12962 (*syms)->value &= ~1;
12963 /* If the symbol has an odd size, it was probably computed
12964 incorrectly, so adjust that as well. */
12965 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
12966 ++elf_symbol (*syms)->internal_elf_sym.st_size;
12967 }
12968 }
12969}
12970
12971#endif
12972
12973/* This function is called whenever a label is defined. It is used
12974 when handling branch delays; if a branch has a label, we assume we
12975 can not move it. */
12976
12977void
12978mips_define_label (sym)
12979 symbolS *sym;
12980{
12981 struct insn_label_list *l;
12982
12983 if (free_insn_labels == NULL)
12984 l = (struct insn_label_list *) xmalloc (sizeof *l);
12985 else
12986 {
12987 l = free_insn_labels;
12988 free_insn_labels = l->next;
12989 }
12990
12991 l->label = sym;
12992 l->next = insn_labels;
12993 insn_labels = l;
12994}
12995\f
12996#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
12997
12998/* Some special processing for a MIPS ELF file. */
12999
13000void
13001mips_elf_final_processing ()
13002{
13003 /* Write out the register information. */
316f5878 13004 if (mips_abi != N64_ABI)
252b5132
RH
13005 {
13006 Elf32_RegInfo s;
13007
13008 s.ri_gprmask = mips_gprmask;
13009 s.ri_cprmask[0] = mips_cprmask[0];
13010 s.ri_cprmask[1] = mips_cprmask[1];
13011 s.ri_cprmask[2] = mips_cprmask[2];
13012 s.ri_cprmask[3] = mips_cprmask[3];
13013 /* The gp_value field is set by the MIPS ELF backend. */
13014
13015 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
13016 ((Elf32_External_RegInfo *)
13017 mips_regmask_frag));
13018 }
13019 else
13020 {
13021 Elf64_Internal_RegInfo s;
13022
13023 s.ri_gprmask = mips_gprmask;
13024 s.ri_pad = 0;
13025 s.ri_cprmask[0] = mips_cprmask[0];
13026 s.ri_cprmask[1] = mips_cprmask[1];
13027 s.ri_cprmask[2] = mips_cprmask[2];
13028 s.ri_cprmask[3] = mips_cprmask[3];
13029 /* The gp_value field is set by the MIPS ELF backend. */
13030
13031 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
13032 ((Elf64_External_RegInfo *)
13033 mips_regmask_frag));
13034 }
13035
13036 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
13037 sort of BFD interface for this. */
13038 if (mips_any_noreorder)
13039 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
13040 if (mips_pic != NO_PIC)
13041 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
13042
98d3f06f 13043 /* Set MIPS ELF flags for ASEs. */
a4672219
TS
13044 if (file_ase_mips16)
13045 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
1f25f5d3
CD
13046#if 0 /* XXX FIXME */
13047 if (file_ase_mips3d)
13048 elf_elfheader (stdoutput)->e_flags |= ???;
13049#endif
deec1734
CD
13050 if (file_ase_mdmx)
13051 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
1f25f5d3 13052
bdaaa2e1 13053 /* Set the MIPS ELF ABI flags. */
316f5878 13054 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
252b5132 13055 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
316f5878 13056 else if (mips_abi == O64_ABI)
252b5132 13057 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
316f5878 13058 else if (mips_abi == EABI_ABI)
252b5132 13059 {
316f5878 13060 if (!file_mips_gp32)
252b5132
RH
13061 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
13062 else
13063 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
13064 }
316f5878 13065 else if (mips_abi == N32_ABI)
be00bddd
TS
13066 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
13067
c9914766 13068 /* Nothing to do for N64_ABI. */
252b5132
RH
13069
13070 if (mips_32bitmode)
13071 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
13072}
13073
13074#endif /* OBJ_ELF || OBJ_MAYBE_ELF */
13075\f
beae10d5
KH
13076typedef struct proc {
13077 symbolS *isym;
13078 unsigned long reg_mask;
13079 unsigned long reg_offset;
13080 unsigned long fpreg_mask;
13081 unsigned long fpreg_offset;
13082 unsigned long frame_offset;
13083 unsigned long frame_reg;
13084 unsigned long pc_reg;
13085} procS;
252b5132
RH
13086
13087static procS cur_proc;
13088static procS *cur_proc_ptr;
13089static int numprocs;
13090
0a9ef439 13091/* Fill in an rs_align_code fragment. */
a19d8eb0 13092
0a9ef439
RH
13093void
13094mips_handle_align (fragp)
13095 fragS *fragp;
a19d8eb0 13096{
0a9ef439
RH
13097 if (fragp->fr_type != rs_align_code)
13098 return;
13099
13100 if (mips_opts.mips16)
a19d8eb0
CP
13101 {
13102 static const unsigned char be_nop[] = { 0x65, 0x00 };
13103 static const unsigned char le_nop[] = { 0x00, 0x65 };
13104
0a9ef439
RH
13105 int bytes;
13106 char *p;
a19d8eb0 13107
0a9ef439
RH
13108 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
13109 p = fragp->fr_literal + fragp->fr_fix;
13110
13111 if (bytes & 1)
13112 {
13113 *p++ = 0;
f9419b05 13114 fragp->fr_fix++;
0a9ef439
RH
13115 }
13116
13117 memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
13118 fragp->fr_var = 2;
a19d8eb0
CP
13119 }
13120
0a9ef439 13121 /* For mips32, a nop is a zero, which we trivially get by doing nothing. */
a19d8eb0
CP
13122}
13123
252b5132
RH
13124static void
13125md_obj_begin ()
13126{
13127}
13128
13129static void
13130md_obj_end ()
13131{
13132 /* check for premature end, nesting errors, etc */
13133 if (cur_proc_ptr)
9a41af64 13134 as_warn (_("missing .end at end of assembly"));
252b5132
RH
13135}
13136
13137static long
13138get_number ()
13139{
13140 int negative = 0;
13141 long val = 0;
13142
13143 if (*input_line_pointer == '-')
13144 {
13145 ++input_line_pointer;
13146 negative = 1;
13147 }
3882b010 13148 if (!ISDIGIT (*input_line_pointer))
956cd1d6 13149 as_bad (_("expected simple number"));
252b5132
RH
13150 if (input_line_pointer[0] == '0')
13151 {
13152 if (input_line_pointer[1] == 'x')
13153 {
13154 input_line_pointer += 2;
3882b010 13155 while (ISXDIGIT (*input_line_pointer))
252b5132
RH
13156 {
13157 val <<= 4;
13158 val |= hex_value (*input_line_pointer++);
13159 }
13160 return negative ? -val : val;
13161 }
13162 else
13163 {
13164 ++input_line_pointer;
3882b010 13165 while (ISDIGIT (*input_line_pointer))
252b5132
RH
13166 {
13167 val <<= 3;
13168 val |= *input_line_pointer++ - '0';
13169 }
13170 return negative ? -val : val;
13171 }
13172 }
3882b010 13173 if (!ISDIGIT (*input_line_pointer))
252b5132
RH
13174 {
13175 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
13176 *input_line_pointer, *input_line_pointer);
956cd1d6 13177 as_warn (_("invalid number"));
252b5132
RH
13178 return -1;
13179 }
3882b010 13180 while (ISDIGIT (*input_line_pointer))
252b5132
RH
13181 {
13182 val *= 10;
13183 val += *input_line_pointer++ - '0';
13184 }
13185 return negative ? -val : val;
13186}
13187
13188/* The .file directive; just like the usual .file directive, but there
c5dd6aab
DJ
13189 is an initial number which is the ECOFF file index. In the non-ECOFF
13190 case .file implies DWARF-2. */
13191
13192static void
13193s_mips_file (x)
13194 int x ATTRIBUTE_UNUSED;
13195{
ecb4347a
DJ
13196 static int first_file_directive = 0;
13197
c5dd6aab
DJ
13198 if (ECOFF_DEBUGGING)
13199 {
13200 get_number ();
13201 s_app_file (0);
13202 }
13203 else
ecb4347a
DJ
13204 {
13205 char *filename;
13206
13207 filename = dwarf2_directive_file (0);
13208
13209 /* Versions of GCC up to 3.1 start files with a ".file"
13210 directive even for stabs output. Make sure that this
13211 ".file" is handled. Note that you need a version of GCC
13212 after 3.1 in order to support DWARF-2 on MIPS. */
13213 if (filename != NULL && ! first_file_directive)
13214 {
13215 (void) new_logical_line (filename, -1);
13216 s_app_file_string (filename);
13217 }
13218 first_file_directive = 1;
13219 }
c5dd6aab
DJ
13220}
13221
13222/* The .loc directive, implying DWARF-2. */
252b5132
RH
13223
13224static void
c5dd6aab 13225s_mips_loc (x)
43841e91 13226 int x ATTRIBUTE_UNUSED;
252b5132 13227{
c5dd6aab
DJ
13228 if (!ECOFF_DEBUGGING)
13229 dwarf2_directive_loc (0);
252b5132
RH
13230}
13231
252b5132
RH
13232/* The .end directive. */
13233
13234static void
13235s_mips_end (x)
43841e91 13236 int x ATTRIBUTE_UNUSED;
252b5132
RH
13237{
13238 symbolS *p;
13239 int maybe_text;
13240
7a621144
DJ
13241 /* Following functions need their own .frame and .cprestore directives. */
13242 mips_frame_reg_valid = 0;
13243 mips_cprestore_valid = 0;
13244
252b5132
RH
13245 if (!is_end_of_line[(unsigned char) *input_line_pointer])
13246 {
13247 p = get_symbol ();
13248 demand_empty_rest_of_line ();
13249 }
13250 else
13251 p = NULL;
13252
13253#ifdef BFD_ASSEMBLER
13254 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
13255 maybe_text = 1;
13256 else
13257 maybe_text = 0;
13258#else
13259 if (now_seg != data_section && now_seg != bss_section)
13260 maybe_text = 1;
13261 else
13262 maybe_text = 0;
13263#endif
13264
13265 if (!maybe_text)
13266 as_warn (_(".end not in text section"));
13267
13268 if (!cur_proc_ptr)
13269 {
13270 as_warn (_(".end directive without a preceding .ent directive."));
13271 demand_empty_rest_of_line ();
13272 return;
13273 }
13274
13275 if (p != NULL)
13276 {
13277 assert (S_GET_NAME (p));
13278 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->isym)))
13279 as_warn (_(".end symbol does not match .ent symbol."));
ecb4347a
DJ
13280
13281 if (debug_type == DEBUG_STABS)
13282 stabs_generate_asm_endfunc (S_GET_NAME (p),
13283 S_GET_NAME (p));
252b5132
RH
13284 }
13285 else
13286 as_warn (_(".end directive missing or unknown symbol"));
13287
ecb4347a
DJ
13288#ifdef OBJ_ELF
13289 /* Generate a .pdr section. */
13290 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
13291 {
13292 segT saved_seg = now_seg;
13293 subsegT saved_subseg = now_subseg;
13294 valueT dot;
13295 expressionS exp;
13296 char *fragp;
252b5132 13297
ecb4347a 13298 dot = frag_now_fix ();
252b5132
RH
13299
13300#ifdef md_flush_pending_output
ecb4347a 13301 md_flush_pending_output ();
252b5132
RH
13302#endif
13303
ecb4347a
DJ
13304 assert (pdr_seg);
13305 subseg_set (pdr_seg, 0);
252b5132 13306
ecb4347a
DJ
13307 /* Write the symbol. */
13308 exp.X_op = O_symbol;
13309 exp.X_add_symbol = p;
13310 exp.X_add_number = 0;
13311 emit_expr (&exp, 4);
252b5132 13312
ecb4347a 13313 fragp = frag_more (7 * 4);
252b5132 13314
ecb4347a
DJ
13315 md_number_to_chars (fragp, (valueT) cur_proc_ptr->reg_mask, 4);
13316 md_number_to_chars (fragp + 4, (valueT) cur_proc_ptr->reg_offset, 4);
13317 md_number_to_chars (fragp + 8, (valueT) cur_proc_ptr->fpreg_mask, 4);
13318 md_number_to_chars (fragp + 12, (valueT) cur_proc_ptr->fpreg_offset, 4);
13319 md_number_to_chars (fragp + 16, (valueT) cur_proc_ptr->frame_offset, 4);
13320 md_number_to_chars (fragp + 20, (valueT) cur_proc_ptr->frame_reg, 4);
13321 md_number_to_chars (fragp + 24, (valueT) cur_proc_ptr->pc_reg, 4);
252b5132 13322
ecb4347a
DJ
13323 subseg_set (saved_seg, saved_subseg);
13324 }
13325#endif /* OBJ_ELF */
252b5132
RH
13326
13327 cur_proc_ptr = NULL;
13328}
13329
13330/* The .aent and .ent directives. */
13331
13332static void
13333s_mips_ent (aent)
13334 int aent;
13335{
252b5132
RH
13336 symbolS *symbolP;
13337 int maybe_text;
13338
13339 symbolP = get_symbol ();
13340 if (*input_line_pointer == ',')
f9419b05 13341 ++input_line_pointer;
252b5132 13342 SKIP_WHITESPACE ();
3882b010 13343 if (ISDIGIT (*input_line_pointer)
d9a62219 13344 || *input_line_pointer == '-')
874e8986 13345 get_number ();
252b5132
RH
13346
13347#ifdef BFD_ASSEMBLER
13348 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
13349 maybe_text = 1;
13350 else
13351 maybe_text = 0;
13352#else
13353 if (now_seg != data_section && now_seg != bss_section)
13354 maybe_text = 1;
13355 else
13356 maybe_text = 0;
13357#endif
13358
13359 if (!maybe_text)
13360 as_warn (_(".ent or .aent not in text section."));
13361
13362 if (!aent && cur_proc_ptr)
9a41af64 13363 as_warn (_("missing .end"));
252b5132
RH
13364
13365 if (!aent)
13366 {
7a621144
DJ
13367 /* This function needs its own .frame and .cprestore directives. */
13368 mips_frame_reg_valid = 0;
13369 mips_cprestore_valid = 0;
13370
252b5132
RH
13371 cur_proc_ptr = &cur_proc;
13372 memset (cur_proc_ptr, '\0', sizeof (procS));
13373
13374 cur_proc_ptr->isym = symbolP;
13375
49309057 13376 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
252b5132 13377
f9419b05 13378 ++numprocs;
ecb4347a
DJ
13379
13380 if (debug_type == DEBUG_STABS)
13381 stabs_generate_asm_func (S_GET_NAME (symbolP),
13382 S_GET_NAME (symbolP));
252b5132
RH
13383 }
13384
13385 demand_empty_rest_of_line ();
13386}
13387
13388/* The .frame directive. If the mdebug section is present (IRIX 5 native)
bdaaa2e1 13389 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
252b5132 13390 s_mips_frame is used so that we can set the PDR information correctly.
bdaaa2e1 13391 We can't use the ecoff routines because they make reference to the ecoff
252b5132
RH
13392 symbol table (in the mdebug section). */
13393
13394static void
13395s_mips_frame (ignore)
2b3c5a5d 13396 int ignore ATTRIBUTE_UNUSED;
252b5132 13397{
ecb4347a
DJ
13398#ifdef OBJ_ELF
13399 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
13400 {
13401 long val;
252b5132 13402
ecb4347a
DJ
13403 if (cur_proc_ptr == (procS *) NULL)
13404 {
13405 as_warn (_(".frame outside of .ent"));
13406 demand_empty_rest_of_line ();
13407 return;
13408 }
252b5132 13409
ecb4347a
DJ
13410 cur_proc_ptr->frame_reg = tc_get_register (1);
13411
13412 SKIP_WHITESPACE ();
13413 if (*input_line_pointer++ != ','
13414 || get_absolute_expression_and_terminator (&val) != ',')
13415 {
13416 as_warn (_("Bad .frame directive"));
13417 --input_line_pointer;
13418 demand_empty_rest_of_line ();
13419 return;
13420 }
252b5132 13421
ecb4347a
DJ
13422 cur_proc_ptr->frame_offset = val;
13423 cur_proc_ptr->pc_reg = tc_get_register (0);
252b5132 13424
252b5132 13425 demand_empty_rest_of_line ();
252b5132 13426 }
ecb4347a
DJ
13427 else
13428#endif /* OBJ_ELF */
13429 s_ignore (ignore);
252b5132
RH
13430}
13431
bdaaa2e1
KH
13432/* The .fmask and .mask directives. If the mdebug section is present
13433 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
252b5132 13434 embedded targets, s_mips_mask is used so that we can set the PDR
bdaaa2e1 13435 information correctly. We can't use the ecoff routines because they
252b5132
RH
13436 make reference to the ecoff symbol table (in the mdebug section). */
13437
13438static void
13439s_mips_mask (reg_type)
13440 char reg_type;
13441{
ecb4347a
DJ
13442#ifdef OBJ_ELF
13443 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
252b5132 13444 {
ecb4347a 13445 long mask, off;
252b5132 13446
ecb4347a
DJ
13447 if (cur_proc_ptr == (procS *) NULL)
13448 {
13449 as_warn (_(".mask/.fmask outside of .ent"));
13450 demand_empty_rest_of_line ();
13451 return;
13452 }
252b5132 13453
ecb4347a
DJ
13454 if (get_absolute_expression_and_terminator (&mask) != ',')
13455 {
13456 as_warn (_("Bad .mask/.fmask directive"));
13457 --input_line_pointer;
13458 demand_empty_rest_of_line ();
13459 return;
13460 }
252b5132 13461
ecb4347a
DJ
13462 off = get_absolute_expression ();
13463
13464 if (reg_type == 'F')
13465 {
13466 cur_proc_ptr->fpreg_mask = mask;
13467 cur_proc_ptr->fpreg_offset = off;
13468 }
13469 else
13470 {
13471 cur_proc_ptr->reg_mask = mask;
13472 cur_proc_ptr->reg_offset = off;
13473 }
13474
13475 demand_empty_rest_of_line ();
252b5132
RH
13476 }
13477 else
ecb4347a
DJ
13478#endif /* OBJ_ELF */
13479 s_ignore (reg_type);
252b5132
RH
13480}
13481
13482/* The .loc directive. */
13483
13484#if 0
13485static void
13486s_loc (x)
13487 int x;
13488{
13489 symbolS *symbolP;
13490 int lineno;
13491 int addroff;
13492
13493 assert (now_seg == text_section);
13494
13495 lineno = get_number ();
13496 addroff = frag_now_fix ();
13497
13498 symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
13499 S_SET_TYPE (symbolP, N_SLINE);
13500 S_SET_OTHER (symbolP, 0);
13501 S_SET_DESC (symbolP, lineno);
13502 symbolP->sy_segment = now_seg;
13503}
13504#endif
e7af610e 13505
316f5878
RS
13506/* A table describing all the processors gas knows about. Names are
13507 matched in the order listed.
e7af610e 13508
316f5878
RS
13509 To ease comparison, please keep this table in the same order as
13510 gcc's mips_cpu_info_table[]. */
e972090a
NC
13511static const struct mips_cpu_info mips_cpu_info_table[] =
13512{
316f5878
RS
13513 /* Entries for generic ISAs */
13514 { "mips1", 1, ISA_MIPS1, CPU_R3000 },
13515 { "mips2", 1, ISA_MIPS2, CPU_R6000 },
13516 { "mips3", 1, ISA_MIPS3, CPU_R4000 },
13517 { "mips4", 1, ISA_MIPS4, CPU_R8000 },
13518 { "mips5", 1, ISA_MIPS5, CPU_MIPS5 },
13519 { "mips32", 1, ISA_MIPS32, CPU_MIPS32 },
13520 { "mips64", 1, ISA_MIPS64, CPU_MIPS64 },
13521
13522 /* MIPS I */
13523 { "r3000", 0, ISA_MIPS1, CPU_R3000 },
13524 { "r2000", 0, ISA_MIPS1, CPU_R3000 },
13525 { "r3900", 0, ISA_MIPS1, CPU_R3900 },
13526
13527 /* MIPS II */
13528 { "r6000", 0, ISA_MIPS2, CPU_R6000 },
13529
13530 /* MIPS III */
13531 { "r4000", 0, ISA_MIPS3, CPU_R4000 },
13532 { "r4010", 0, ISA_MIPS2, CPU_R4010 },
13533 { "vr4100", 0, ISA_MIPS3, CPU_VR4100 },
13534 { "vr4111", 0, ISA_MIPS3, CPU_R4111 },
13535 { "vr4300", 0, ISA_MIPS3, CPU_R4300 },
13536 { "r4400", 0, ISA_MIPS3, CPU_R4400 },
13537 { "r4600", 0, ISA_MIPS3, CPU_R4600 },
13538 { "orion", 0, ISA_MIPS3, CPU_R4600 },
13539 { "r4650", 0, ISA_MIPS3, CPU_R4650 },
13540
13541 /* MIPS IV */
13542 { "r8000", 0, ISA_MIPS4, CPU_R8000 },
13543 { "r10000", 0, ISA_MIPS4, CPU_R10000 },
13544 { "r12000", 0, ISA_MIPS4, CPU_R12000 },
13545 { "vr5000", 0, ISA_MIPS4, CPU_R5000 },
13546 { "rm5200", 0, ISA_MIPS4, CPU_R5000 },
13547 { "rm5230", 0, ISA_MIPS4, CPU_R5000 },
13548 { "rm5231", 0, ISA_MIPS4, CPU_R5000 },
13549 { "rm5261", 0, ISA_MIPS4, CPU_R5000 },
13550 { "rm5721", 0, ISA_MIPS4, CPU_R5000 },
13551 { "r7000", 0, ISA_MIPS4, CPU_R5000 },
13552
13553 /* MIPS 32 */
13554 { "4kc", 0, ISA_MIPS32, CPU_MIPS32, },
13555 { "4km", 0, ISA_MIPS32, CPU_MIPS32 },
13556 { "4kp", 0, ISA_MIPS32, CPU_MIPS32 },
e7af610e 13557
316f5878
RS
13558 /* MIPS 64 */
13559 { "5kc", 0, ISA_MIPS64, CPU_MIPS64 },
13560 { "20kc", 0, ISA_MIPS64, CPU_MIPS64 },
e7af610e 13561
c7a23324 13562 /* Broadcom SB-1 CPU core */
316f5878 13563 { "sb1", 0, ISA_MIPS64, CPU_SB1 },
e7af610e 13564
316f5878
RS
13565 /* End marker */
13566 { NULL, 0, 0, 0 }
13567};
e7af610e 13568
84ea6cf2 13569
316f5878
RS
13570/* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
13571 with a final "000" replaced by "k". Ignore case.
e7af610e 13572
316f5878 13573 Note: this function is shared between GCC and GAS. */
c6c98b38 13574
316f5878
RS
13575static boolean
13576mips_strict_matching_cpu_name_p (canonical, given)
13577 const char *canonical, *given;
13578{
13579 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
13580 given++, canonical++;
13581
13582 return ((*given == 0 && *canonical == 0)
13583 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
13584}
13585
13586
13587/* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
13588 CPU name. We've traditionally allowed a lot of variation here.
13589
13590 Note: this function is shared between GCC and GAS. */
13591
13592static boolean
13593mips_matching_cpu_name_p (canonical, given)
13594 const char *canonical, *given;
13595{
13596 /* First see if the name matches exactly, or with a final "000"
13597 turned into "k". */
13598 if (mips_strict_matching_cpu_name_p (canonical, given))
13599 return true;
13600
13601 /* If not, try comparing based on numerical designation alone.
13602 See if GIVEN is an unadorned number, or 'r' followed by a number. */
13603 if (TOLOWER (*given) == 'r')
13604 given++;
13605 if (!ISDIGIT (*given))
13606 return false;
13607
13608 /* Skip over some well-known prefixes in the canonical name,
13609 hoping to find a number there too. */
13610 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
13611 canonical += 2;
13612 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
13613 canonical += 2;
13614 else if (TOLOWER (canonical[0]) == 'r')
13615 canonical += 1;
13616
13617 return mips_strict_matching_cpu_name_p (canonical, given);
13618}
13619
13620
13621/* Parse an option that takes the name of a processor as its argument.
13622 OPTION is the name of the option and CPU_STRING is the argument.
13623 Return the corresponding processor enumeration if the CPU_STRING is
13624 recognized, otherwise report an error and return null.
13625
13626 A similar function exists in GCC. */
e7af610e
NC
13627
13628static const struct mips_cpu_info *
316f5878
RS
13629mips_parse_cpu (option, cpu_string)
13630 const char *option, *cpu_string;
e7af610e 13631{
316f5878 13632 const struct mips_cpu_info *p;
e7af610e 13633
316f5878
RS
13634 /* 'from-abi' selects the most compatible architecture for the given
13635 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
13636 EABIs, we have to decide whether we're using the 32-bit or 64-bit
13637 version. Look first at the -mgp options, if given, otherwise base
13638 the choice on MIPS_DEFAULT_64BIT.
e7af610e 13639
316f5878
RS
13640 Treat NO_ABI like the EABIs. One reason to do this is that the
13641 plain 'mips' and 'mips64' configs have 'from-abi' as their default
13642 architecture. This code picks MIPS I for 'mips' and MIPS III for
13643 'mips64', just as we did in the days before 'from-abi'. */
13644 if (strcasecmp (cpu_string, "from-abi") == 0)
13645 {
13646 if (ABI_NEEDS_32BIT_REGS (mips_abi))
13647 return mips_cpu_info_from_isa (ISA_MIPS1);
13648
13649 if (ABI_NEEDS_64BIT_REGS (mips_abi))
13650 return mips_cpu_info_from_isa (ISA_MIPS3);
13651
13652 if (file_mips_gp32 >= 0)
13653 return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
13654
13655 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
13656 ? ISA_MIPS3
13657 : ISA_MIPS1);
13658 }
13659
13660 /* 'default' has traditionally been a no-op. Probably not very useful. */
13661 if (strcasecmp (cpu_string, "default") == 0)
13662 return 0;
13663
13664 for (p = mips_cpu_info_table; p->name != 0; p++)
13665 if (mips_matching_cpu_name_p (p->name, cpu_string))
13666 return p;
13667
13668 as_bad ("Bad value (%s) for %s", cpu_string, option);
13669 return 0;
e7af610e
NC
13670}
13671
316f5878
RS
13672/* Return the canonical processor information for ISA (a member of the
13673 ISA_MIPS* enumeration). */
13674
e7af610e
NC
13675static const struct mips_cpu_info *
13676mips_cpu_info_from_isa (isa)
13677 int isa;
13678{
13679 int i;
13680
13681 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13682 if (mips_cpu_info_table[i].is_isa
316f5878 13683 && isa == mips_cpu_info_table[i].isa)
e7af610e
NC
13684 return (&mips_cpu_info_table[i]);
13685
e972090a 13686 return NULL;
e7af610e 13687}
316f5878
RS
13688\f
13689static void
13690show (stream, string, col_p, first_p)
13691 FILE *stream;
13692 const char *string;
13693 int *col_p;
13694 int *first_p;
13695{
13696 if (*first_p)
13697 {
13698 fprintf (stream, "%24s", "");
13699 *col_p = 24;
13700 }
13701 else
13702 {
13703 fprintf (stream, ", ");
13704 *col_p += 2;
13705 }
e7af610e 13706
316f5878
RS
13707 if (*col_p + strlen (string) > 72)
13708 {
13709 fprintf (stream, "\n%24s", "");
13710 *col_p = 24;
13711 }
13712
13713 fprintf (stream, "%s", string);
13714 *col_p += strlen (string);
13715
13716 *first_p = 0;
13717}
13718
13719void
13720md_show_usage (stream)
13721 FILE *stream;
e7af610e 13722{
316f5878
RS
13723 int column, first;
13724 size_t i;
13725
13726 fprintf (stream, _("\
13727MIPS options:\n\
13728-membedded-pic generate embedded position independent code\n\
13729-EB generate big endian output\n\
13730-EL generate little endian output\n\
13731-g, -g2 do not remove unneeded NOPs or swap branches\n\
13732-G NUM allow referencing objects up to NUM bytes\n\
13733 implicitly with the gp register [default 8]\n"));
13734 fprintf (stream, _("\
13735-mips1 generate MIPS ISA I instructions\n\
13736-mips2 generate MIPS ISA II instructions\n\
13737-mips3 generate MIPS ISA III instructions\n\
13738-mips4 generate MIPS ISA IV instructions\n\
13739-mips5 generate MIPS ISA V instructions\n\
13740-mips32 generate MIPS32 ISA instructions\n\
13741-mips64 generate MIPS64 ISA instructions\n\
13742-march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
13743
13744 first = 1;
e7af610e
NC
13745
13746 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
316f5878
RS
13747 show (stream, mips_cpu_info_table[i].name, &column, &first);
13748 show (stream, "from-abi", &column, &first);
13749 fputc ('\n', stream);
e7af610e 13750
316f5878
RS
13751 fprintf (stream, _("\
13752-mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
13753-no-mCPU don't generate code specific to CPU.\n\
13754 For -mCPU and -no-mCPU, CPU must be one of:\n"));
13755
13756 first = 1;
13757
13758 show (stream, "3900", &column, &first);
13759 show (stream, "4010", &column, &first);
13760 show (stream, "4100", &column, &first);
13761 show (stream, "4650", &column, &first);
13762 fputc ('\n', stream);
13763
13764 fprintf (stream, _("\
13765-mips16 generate mips16 instructions\n\
13766-no-mips16 do not generate mips16 instructions\n"));
13767 fprintf (stream, _("\
13768-mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
13769-mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
13770-O0 remove unneeded NOPs, do not swap branches\n\
13771-O remove unneeded NOPs and swap branches\n\
13772-n warn about NOPs generated from macros\n\
13773--[no-]construct-floats [dis]allow floating point values to be constructed\n\
13774--trap, --no-break trap exception on div by 0 and mult overflow\n\
13775--break, --no-trap break exception on div by 0 and mult overflow\n"));
13776#ifdef OBJ_ELF
13777 fprintf (stream, _("\
13778-KPIC, -call_shared generate SVR4 position independent code\n\
13779-non_shared do not generate position independent code\n\
13780-xgot assume a 32 bit GOT\n\
13781-mabi=ABI create ABI conformant object file for:\n"));
13782
13783 first = 1;
13784
13785 show (stream, "32", &column, &first);
13786 show (stream, "o64", &column, &first);
13787 show (stream, "n32", &column, &first);
13788 show (stream, "64", &column, &first);
13789 show (stream, "eabi", &column, &first);
13790
13791 fputc ('\n', stream);
13792
13793 fprintf (stream, _("\
13794-32 create o32 ABI object file (default)\n\
13795-n32 create n32 ABI object file\n\
13796-64 create 64 ABI object file\n"));
13797#endif
e7af610e 13798}
This page took 0.969056 seconds and 4 git commands to generate.