* config/tc-mips.c (macro_build_jalr): Make sure we generate
[deliverable/binutils-gdb.git] / gas / config / tc-mips.c
CommitLineData
252b5132 1/* tc-mips.c -- assemble code for a MIPS chip.
98d3f06f 2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
82efde3a 3 Free Software Foundation, Inc.
252b5132
RH
4 Contributed by the OSF and Ralph Campbell.
5 Written by Keith Knowles and Ralph Campbell, working independently.
6 Modified for ECOFF and R4000 support by Ian Lance Taylor of Cygnus
7 Support.
8
9 This file is part of GAS.
10
11 GAS is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2, or (at your option)
14 any later version.
15
16 GAS is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with GAS; see the file COPYING. If not, write to the Free
23 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 02111-1307, USA. */
25
26#include "as.h"
27#include "config.h"
28#include "subsegs.h"
3882b010 29#include "safe-ctype.h"
252b5132
RH
30
31#ifdef USE_STDARG
32#include <stdarg.h>
33#endif
34#ifdef USE_VARARGS
35#include <varargs.h>
36#endif
37
38#include "opcode/mips.h"
39#include "itbl-ops.h"
c5dd6aab 40#include "dwarf2dbg.h"
252b5132
RH
41
42#ifdef DEBUG
43#define DBG(x) printf x
44#else
45#define DBG(x)
46#endif
47
48#ifdef OBJ_MAYBE_ELF
49/* Clean up namespace so we can include obj-elf.h too. */
50static int mips_output_flavor PARAMS ((void));
51static int mips_output_flavor () { return OUTPUT_FLAVOR; }
52#undef OBJ_PROCESS_STAB
53#undef OUTPUT_FLAVOR
54#undef S_GET_ALIGN
55#undef S_GET_SIZE
56#undef S_SET_ALIGN
57#undef S_SET_SIZE
252b5132
RH
58#undef obj_frob_file
59#undef obj_frob_file_after_relocs
60#undef obj_frob_symbol
61#undef obj_pop_insert
62#undef obj_sec_sym_ok_for_reloc
63#undef OBJ_COPY_SYMBOL_ATTRIBUTES
64
65#include "obj-elf.h"
66/* Fix any of them that we actually care about. */
67#undef OUTPUT_FLAVOR
68#define OUTPUT_FLAVOR mips_output_flavor()
69#endif
70
71#if defined (OBJ_ELF)
72#include "elf/mips.h"
73#endif
74
75#ifndef ECOFF_DEBUGGING
76#define NO_ECOFF_DEBUGGING
77#define ECOFF_DEBUGGING 0
78#endif
79
ecb4347a
DJ
80int mips_flag_mdebug = -1;
81
252b5132
RH
82#include "ecoff.h"
83
84#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
85static char *mips_regmask_frag;
86#endif
87
85b51719 88#define ZERO 0
252b5132
RH
89#define AT 1
90#define TREG 24
91#define PIC_CALL_REG 25
92#define KT0 26
93#define KT1 27
94#define GP 28
95#define SP 29
96#define FP 30
97#define RA 31
98
99#define ILLEGAL_REG (32)
100
101/* Allow override of standard little-endian ECOFF format. */
102
103#ifndef ECOFF_LITTLE_FORMAT
104#define ECOFF_LITTLE_FORMAT "ecoff-littlemips"
105#endif
106
107extern int target_big_endian;
108
252b5132
RH
109/* The name of the readonly data section. */
110#define RDATA_SECTION_NAME (OUTPUT_FLAVOR == bfd_target_aout_flavour \
111 ? ".data" \
112 : OUTPUT_FLAVOR == bfd_target_ecoff_flavour \
113 ? ".rdata" \
056350c6
NC
114 : OUTPUT_FLAVOR == bfd_target_coff_flavour \
115 ? ".rdata" \
252b5132
RH
116 : OUTPUT_FLAVOR == bfd_target_elf_flavour \
117 ? ".rodata" \
118 : (abort (), ""))
119
a325df1d
TS
120/* The ABI to use. */
121enum mips_abi_level
122{
123 NO_ABI = 0,
124 O32_ABI,
125 O64_ABI,
126 N32_ABI,
127 N64_ABI,
128 EABI_ABI
129};
130
131/* MIPS ABI we are using for this output file. */
316f5878 132static enum mips_abi_level mips_abi = NO_ABI;
a325df1d 133
252b5132
RH
134/* This is the set of options which may be modified by the .set
135 pseudo-op. We use a struct so that .set push and .set pop are more
136 reliable. */
137
e972090a
NC
138struct mips_set_options
139{
252b5132
RH
140 /* MIPS ISA (Instruction Set Architecture) level. This is set to -1
141 if it has not been initialized. Changed by `.set mipsN', and the
142 -mipsN command line option, and the default CPU. */
143 int isa;
1f25f5d3
CD
144 /* Enabled Application Specific Extensions (ASEs). These are set to -1
145 if they have not been initialized. Changed by `.set <asename>', by
146 command line options, and based on the default architecture. */
147 int ase_mips3d;
deec1734 148 int ase_mdmx;
252b5132
RH
149 /* Whether we are assembling for the mips16 processor. 0 if we are
150 not, 1 if we are, and -1 if the value has not been initialized.
151 Changed by `.set mips16' and `.set nomips16', and the -mips16 and
152 -nomips16 command line options, and the default CPU. */
153 int mips16;
154 /* Non-zero if we should not reorder instructions. Changed by `.set
155 reorder' and `.set noreorder'. */
156 int noreorder;
157 /* Non-zero if we should not permit the $at ($1) register to be used
158 in instructions. Changed by `.set at' and `.set noat'. */
159 int noat;
160 /* Non-zero if we should warn when a macro instruction expands into
161 more than one machine instruction. Changed by `.set nomacro' and
162 `.set macro'. */
163 int warn_about_macros;
164 /* Non-zero if we should not move instructions. Changed by `.set
165 move', `.set volatile', `.set nomove', and `.set novolatile'. */
166 int nomove;
167 /* Non-zero if we should not optimize branches by moving the target
168 of the branch into the delay slot. Actually, we don't perform
169 this optimization anyhow. Changed by `.set bopt' and `.set
170 nobopt'. */
171 int nobopt;
172 /* Non-zero if we should not autoextend mips16 instructions.
173 Changed by `.set autoextend' and `.set noautoextend'. */
174 int noautoextend;
a325df1d
TS
175 /* Restrict general purpose registers and floating point registers
176 to 32 bit. This is initially determined when -mgp32 or -mfp32
177 is passed but can changed if the assembler code uses .set mipsN. */
178 int gp32;
179 int fp32;
252b5132
RH
180};
181
a325df1d 182/* True if -mgp32 was passed. */
a8e8e863 183static int file_mips_gp32 = -1;
a325df1d
TS
184
185/* True if -mfp32 was passed. */
a8e8e863 186static int file_mips_fp32 = -1;
a325df1d 187
252b5132 188/* This is the struct we use to hold the current set of options. Note
a4672219 189 that we must set the isa field to ISA_UNKNOWN and the ASE fields to
e7af610e 190 -1 to indicate that they have not been initialized. */
252b5132 191
e972090a
NC
192static struct mips_set_options mips_opts =
193{
316f5878 194 ISA_UNKNOWN, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0
e7af610e 195};
252b5132
RH
196
197/* These variables are filled in with the masks of registers used.
198 The object format code reads them and puts them in the appropriate
199 place. */
200unsigned long mips_gprmask;
201unsigned long mips_cprmask[4];
202
203/* MIPS ISA we are using for this output file. */
e7af610e 204static int file_mips_isa = ISA_UNKNOWN;
252b5132 205
a4672219
TS
206/* True if -mips16 was passed or implied by arguments passed on the
207 command line (e.g., by -march). */
208static int file_ase_mips16;
209
1f25f5d3
CD
210/* True if -mips3d was passed or implied by arguments passed on the
211 command line (e.g., by -march). */
212static int file_ase_mips3d;
213
deec1734
CD
214/* True if -mdmx was passed or implied by arguments passed on the
215 command line (e.g., by -march). */
216static int file_ase_mdmx;
217
ec68c924
EC
218/* The argument of the -march= flag. The architecture we are assembling. */
219static int mips_arch = CPU_UNKNOWN;
316f5878
RS
220static const char *mips_arch_string;
221static const struct mips_cpu_info *mips_arch_info;
ec68c924
EC
222
223/* The argument of the -mtune= flag. The architecture for which we
224 are optimizing. */
225static int mips_tune = CPU_UNKNOWN;
316f5878
RS
226static const char *mips_tune_string;
227static const struct mips_cpu_info *mips_tune_info;
ec68c924 228
316f5878 229/* True when generating 32-bit code for a 64-bit processor. */
252b5132
RH
230static int mips_32bitmode = 0;
231
9ce8a5dd
GRK
232/* Some ISA's have delay slots for instructions which read or write
233 from a coprocessor (eg. mips1-mips3); some don't (eg mips4).
bdaaa2e1 234 Return true if instructions marked INSN_LOAD_COPROC_DELAY,
9ce8a5dd
GRK
235 INSN_COPROC_MOVE_DELAY, or INSN_WRITE_COND_CODE actually have a
236 delay slot in this ISA. The uses of this macro assume that any
237 ISA that has delay slots for one of these, has them for all. They
238 also assume that ISAs which don't have delays for these insns, don't
bdaaa2e1 239 have delays for the INSN_LOAD_MEMORY_DELAY instructions either. */
9ce8a5dd 240#define ISA_HAS_COPROC_DELAYS(ISA) ( \
e7af610e
NC
241 (ISA) == ISA_MIPS1 \
242 || (ISA) == ISA_MIPS2 \
243 || (ISA) == ISA_MIPS3 \
9ce8a5dd
GRK
244 )
245
316f5878
RS
246/* True if the given ABI requires 32-bit registers. */
247#define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
248
249/* Likewise 64-bit registers. */
250#define ABI_NEEDS_64BIT_REGS(ABI) \
251 ((ABI) == N32_ABI \
252 || (ABI) == N64_ABI \
253 || (ABI) == O64_ABI)
254
bdaaa2e1 255/* Return true if ISA supports 64 bit gp register instructions. */
9ce8a5dd 256#define ISA_HAS_64BIT_REGS(ISA) ( \
e7af610e
NC
257 (ISA) == ISA_MIPS3 \
258 || (ISA) == ISA_MIPS4 \
84ea6cf2 259 || (ISA) == ISA_MIPS5 \
d1cf510e 260 || (ISA) == ISA_MIPS64 \
9ce8a5dd
GRK
261 )
262
e013f690 263#define HAVE_32BIT_GPRS \
316f5878 264 (mips_opts.gp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
ca4e0257 265
e013f690 266#define HAVE_32BIT_FPRS \
316f5878 267 (mips_opts.fp32 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
ca4e0257
RS
268
269#define HAVE_64BIT_GPRS (! HAVE_32BIT_GPRS)
270#define HAVE_64BIT_FPRS (! HAVE_32BIT_FPRS)
271
316f5878 272#define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
e013f690 273
316f5878 274#define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
e013f690
TS
275
276/* We can only have 64bit addresses if the object file format
277 supports it. */
afdbd6d0
CD
278#define HAVE_32BIT_ADDRESSES \
279 (HAVE_32BIT_GPRS \
280 || ((bfd_arch_bits_per_address (stdoutput) == 32 \
281 || ! HAVE_64BIT_OBJECTS) \
282 && mips_pic != EMBEDDED_PIC))
e013f690
TS
283
284#define HAVE_64BIT_ADDRESSES (! HAVE_32BIT_ADDRESSES)
ca4e0257 285
a4672219
TS
286/* Return true if the given CPU supports the MIPS16 ASE. */
287#define CPU_HAS_MIPS16(cpu) \
288 (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0)
289
1f25f5d3
CD
290/* Return true if the given CPU supports the MIPS3D ASE. */
291#define CPU_HAS_MIPS3D(cpu) ((cpu) == CPU_SB1 \
292 )
293
deec1734 294/* Return true if the given CPU supports the MDMX ASE. */
a4672219 295#define CPU_HAS_MDMX(cpu) (false \
deec1734
CD
296 )
297
bdaaa2e1 298/* Whether the processor uses hardware interlocks to protect
252b5132 299 reads from the HI and LO registers, and thus does not
ec68c924 300 require nops to be inserted. */
252b5132 301
ec68c924 302#define hilo_interlocks (mips_arch == CPU_R4010 \
0a758a12 303 || mips_arch == CPU_SB1 \
252b5132
RH
304 )
305
306/* Whether the processor uses hardware interlocks to protect reads
307 from the GPRs, and thus does not require nops to be inserted. */
308#define gpr_interlocks \
e7af610e 309 (mips_opts.isa != ISA_MIPS1 \
ec68c924 310 || mips_arch == CPU_R3900)
252b5132
RH
311
312/* As with other "interlocks" this is used by hardware that has FP
313 (co-processor) interlocks. */
bdaaa2e1 314/* Itbl support may require additional care here. */
ec68c924 315#define cop_interlocks (mips_arch == CPU_R4300 \
0a758a12 316 || mips_arch == CPU_SB1 \
252b5132
RH
317 )
318
6b76fefe
CM
319/* Is this a mfhi or mflo instruction? */
320#define MF_HILO_INSN(PINFO) \
321 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
322
252b5132
RH
323/* MIPS PIC level. */
324
e972090a
NC
325enum mips_pic_level
326{
252b5132
RH
327 /* Do not generate PIC code. */
328 NO_PIC,
329
252b5132
RH
330 /* Generate PIC code as in the SVR4 MIPS ABI. */
331 SVR4_PIC,
332
333 /* Generate PIC code without using a global offset table: the data
334 segment has a maximum size of 64K, all data references are off
335 the $gp register, and all text references are PC relative. This
336 is used on some embedded systems. */
337 EMBEDDED_PIC
338};
339
340static enum mips_pic_level mips_pic;
341
39c0a331
L
342/* Warn about all NOPS that the assembler generates. */
343static int warn_nops = 0;
344
c9914766 345/* 1 if we should generate 32 bit offsets from the $gp register in
252b5132 346 SVR4_PIC mode. Currently has no meaning in other modes. */
c9914766 347static int mips_big_got = 0;
252b5132
RH
348
349/* 1 if trap instructions should used for overflow rather than break
350 instructions. */
c9914766 351static int mips_trap = 0;
252b5132 352
119d663a 353/* 1 if double width floating point constants should not be constructed
b6ff326e 354 by assembling two single width halves into two single width floating
119d663a
NC
355 point registers which just happen to alias the double width destination
356 register. On some architectures this aliasing can be disabled by a bit
d547a75e 357 in the status register, and the setting of this bit cannot be determined
119d663a
NC
358 automatically at assemble time. */
359static int mips_disable_float_construction;
360
252b5132
RH
361/* Non-zero if any .set noreorder directives were used. */
362
363static int mips_any_noreorder;
364
6b76fefe
CM
365/* Non-zero if nops should be inserted when the register referenced in
366 an mfhi/mflo instruction is read in the next two instructions. */
367static int mips_7000_hilo_fix;
368
252b5132 369/* The size of the small data section. */
156c2f8b 370static unsigned int g_switch_value = 8;
252b5132
RH
371/* Whether the -G option was used. */
372static int g_switch_seen = 0;
373
374#define N_RMASK 0xc4
375#define N_VFP 0xd4
376
377/* If we can determine in advance that GP optimization won't be
378 possible, we can skip the relaxation stuff that tries to produce
379 GP-relative references. This makes delay slot optimization work
380 better.
381
382 This function can only provide a guess, but it seems to work for
fba2b7f9
GK
383 gcc output. It needs to guess right for gcc, otherwise gcc
384 will put what it thinks is a GP-relative instruction in a branch
385 delay slot.
252b5132
RH
386
387 I don't know if a fix is needed for the SVR4_PIC mode. I've only
388 fixed it for the non-PIC mode. KR 95/04/07 */
389static int nopic_need_relax PARAMS ((symbolS *, int));
390
391/* handle of the OPCODE hash table */
392static struct hash_control *op_hash = NULL;
393
394/* The opcode hash table we use for the mips16. */
395static struct hash_control *mips16_op_hash = NULL;
396
397/* This array holds the chars that always start a comment. If the
398 pre-processor is disabled, these aren't very useful */
399const char comment_chars[] = "#";
400
401/* This array holds the chars that only start a comment at the beginning of
402 a line. If the line seems to have the form '# 123 filename'
403 .line and .file directives will appear in the pre-processed output */
404/* Note that input_file.c hand checks for '#' at the beginning of the
405 first line of the input file. This is because the compiler outputs
bdaaa2e1 406 #NO_APP at the beginning of its output. */
252b5132
RH
407/* Also note that C style comments are always supported. */
408const char line_comment_chars[] = "#";
409
bdaaa2e1 410/* This array holds machine specific line separator characters. */
63a0b638 411const char line_separator_chars[] = ";";
252b5132
RH
412
413/* Chars that can be used to separate mant from exp in floating point nums */
414const char EXP_CHARS[] = "eE";
415
416/* Chars that mean this number is a floating point constant */
417/* As in 0f12.456 */
418/* or 0d1.2345e12 */
419const char FLT_CHARS[] = "rRsSfFdDxXpP";
420
421/* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
422 changed in read.c . Ideally it shouldn't have to know about it at all,
423 but nothing is ideal around here.
424 */
425
426static char *insn_error;
427
428static int auto_align = 1;
429
430/* When outputting SVR4 PIC code, the assembler needs to know the
431 offset in the stack frame from which to restore the $gp register.
432 This is set by the .cprestore pseudo-op, and saved in this
433 variable. */
434static offsetT mips_cprestore_offset = -1;
435
6478892d
TS
436/* Similiar for NewABI PIC code, where $gp is callee-saved. NewABI has some
437 more optimizations, it can use a register value instead of a memory-saved
956cd1d6 438 offset and even an other register than $gp as global pointer. */
6478892d
TS
439static offsetT mips_cpreturn_offset = -1;
440static int mips_cpreturn_register = -1;
441static int mips_gp_register = GP;
def2e0dd 442static int mips_gprel_offset = 0;
6478892d 443
7a621144
DJ
444/* Whether mips_cprestore_offset has been set in the current function
445 (or whether it has already been warned about, if not). */
446static int mips_cprestore_valid = 0;
447
252b5132
RH
448/* This is the register which holds the stack frame, as set by the
449 .frame pseudo-op. This is needed to implement .cprestore. */
450static int mips_frame_reg = SP;
451
7a621144
DJ
452/* Whether mips_frame_reg has been set in the current function
453 (or whether it has already been warned about, if not). */
454static int mips_frame_reg_valid = 0;
455
252b5132
RH
456/* To output NOP instructions correctly, we need to keep information
457 about the previous two instructions. */
458
459/* Whether we are optimizing. The default value of 2 means to remove
460 unneeded NOPs and swap branch instructions when possible. A value
461 of 1 means to not swap branches. A value of 0 means to always
462 insert NOPs. */
463static int mips_optimize = 2;
464
465/* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
466 equivalent to seeing no -g option at all. */
467static int mips_debug = 0;
468
469/* The previous instruction. */
470static struct mips_cl_insn prev_insn;
471
472/* The instruction before prev_insn. */
473static struct mips_cl_insn prev_prev_insn;
474
475/* If we don't want information for prev_insn or prev_prev_insn, we
476 point the insn_mo field at this dummy integer. */
43841e91 477static const struct mips_opcode dummy_opcode = { NULL, NULL, 0, 0, 0, 0 };
252b5132
RH
478
479/* Non-zero if prev_insn is valid. */
480static int prev_insn_valid;
481
482/* The frag for the previous instruction. */
483static struct frag *prev_insn_frag;
484
485/* The offset into prev_insn_frag for the previous instruction. */
486static long prev_insn_where;
487
488/* The reloc type for the previous instruction, if any. */
f6688943 489static bfd_reloc_code_real_type prev_insn_reloc_type[3];
252b5132
RH
490
491/* The reloc for the previous instruction, if any. */
f6688943 492static fixS *prev_insn_fixp[3];
252b5132
RH
493
494/* Non-zero if the previous instruction was in a delay slot. */
495static int prev_insn_is_delay_slot;
496
497/* Non-zero if the previous instruction was in a .set noreorder. */
498static int prev_insn_unreordered;
499
500/* Non-zero if the previous instruction uses an extend opcode (if
501 mips16). */
502static int prev_insn_extended;
503
504/* Non-zero if the previous previous instruction was in a .set
505 noreorder. */
506static int prev_prev_insn_unreordered;
507
508/* If this is set, it points to a frag holding nop instructions which
509 were inserted before the start of a noreorder section. If those
510 nops turn out to be unnecessary, the size of the frag can be
511 decreased. */
512static fragS *prev_nop_frag;
513
514/* The number of nop instructions we created in prev_nop_frag. */
515static int prev_nop_frag_holds;
516
517/* The number of nop instructions that we know we need in
bdaaa2e1 518 prev_nop_frag. */
252b5132
RH
519static int prev_nop_frag_required;
520
521/* The number of instructions we've seen since prev_nop_frag. */
522static int prev_nop_frag_since;
523
524/* For ECOFF and ELF, relocations against symbols are done in two
525 parts, with a HI relocation and a LO relocation. Each relocation
526 has only 16 bits of space to store an addend. This means that in
527 order for the linker to handle carries correctly, it must be able
528 to locate both the HI and the LO relocation. This means that the
529 relocations must appear in order in the relocation table.
530
531 In order to implement this, we keep track of each unmatched HI
532 relocation. We then sort them so that they immediately precede the
bdaaa2e1 533 corresponding LO relocation. */
252b5132 534
e972090a
NC
535struct mips_hi_fixup
536{
252b5132
RH
537 /* Next HI fixup. */
538 struct mips_hi_fixup *next;
539 /* This fixup. */
540 fixS *fixp;
541 /* The section this fixup is in. */
542 segT seg;
543};
544
545/* The list of unmatched HI relocs. */
546
547static struct mips_hi_fixup *mips_hi_fixup_list;
548
549/* Map normal MIPS register numbers to mips16 register numbers. */
550
551#define X ILLEGAL_REG
e972090a
NC
552static const int mips32_to_16_reg_map[] =
553{
252b5132
RH
554 X, X, 2, 3, 4, 5, 6, 7,
555 X, X, X, X, X, X, X, X,
556 0, 1, X, X, X, X, X, X,
557 X, X, X, X, X, X, X, X
558};
559#undef X
560
561/* Map mips16 register numbers to normal MIPS register numbers. */
562
e972090a
NC
563static const unsigned int mips16_to_32_reg_map[] =
564{
252b5132
RH
565 16, 17, 2, 3, 4, 5, 6, 7
566};
567\f
568/* Since the MIPS does not have multiple forms of PC relative
569 instructions, we do not have to do relaxing as is done on other
570 platforms. However, we do have to handle GP relative addressing
571 correctly, which turns out to be a similar problem.
572
573 Every macro that refers to a symbol can occur in (at least) two
574 forms, one with GP relative addressing and one without. For
575 example, loading a global variable into a register generally uses
576 a macro instruction like this:
577 lw $4,i
578 If i can be addressed off the GP register (this is true if it is in
579 the .sbss or .sdata section, or if it is known to be smaller than
580 the -G argument) this will generate the following instruction:
581 lw $4,i($gp)
582 This instruction will use a GPREL reloc. If i can not be addressed
583 off the GP register, the following instruction sequence will be used:
584 lui $at,i
585 lw $4,i($at)
586 In this case the first instruction will have a HI16 reloc, and the
587 second reloc will have a LO16 reloc. Both relocs will be against
588 the symbol i.
589
590 The issue here is that we may not know whether i is GP addressable
591 until after we see the instruction that uses it. Therefore, we
592 want to be able to choose the final instruction sequence only at
593 the end of the assembly. This is similar to the way other
594 platforms choose the size of a PC relative instruction only at the
595 end of assembly.
596
597 When generating position independent code we do not use GP
598 addressing in quite the same way, but the issue still arises as
599 external symbols and local symbols must be handled differently.
600
601 We handle these issues by actually generating both possible
602 instruction sequences. The longer one is put in a frag_var with
603 type rs_machine_dependent. We encode what to do with the frag in
604 the subtype field. We encode (1) the number of existing bytes to
605 replace, (2) the number of new bytes to use, (3) the offset from
606 the start of the existing bytes to the first reloc we must generate
607 (that is, the offset is applied from the start of the existing
608 bytes after they are replaced by the new bytes, if any), (4) the
609 offset from the start of the existing bytes to the second reloc,
610 (5) whether a third reloc is needed (the third reloc is always four
611 bytes after the second reloc), and (6) whether to warn if this
612 variant is used (this is sometimes needed if .set nomacro or .set
613 noat is in effect). All these numbers are reasonably small.
614
615 Generating two instruction sequences must be handled carefully to
616 ensure that delay slots are handled correctly. Fortunately, there
617 are a limited number of cases. When the second instruction
618 sequence is generated, append_insn is directed to maintain the
619 existing delay slot information, so it continues to apply to any
620 code after the second instruction sequence. This means that the
621 second instruction sequence must not impose any requirements not
622 required by the first instruction sequence.
623
624 These variant frags are then handled in functions called by the
625 machine independent code. md_estimate_size_before_relax returns
626 the final size of the frag. md_convert_frag sets up the final form
627 of the frag. tc_gen_reloc adjust the first reloc and adds a second
628 one if needed. */
629#define RELAX_ENCODE(old, new, reloc1, reloc2, reloc3, warn) \
630 ((relax_substateT) \
631 (((old) << 23) \
632 | ((new) << 16) \
633 | (((reloc1) + 64) << 9) \
634 | (((reloc2) + 64) << 2) \
635 | ((reloc3) ? (1 << 1) : 0) \
636 | ((warn) ? 1 : 0)))
637#define RELAX_OLD(i) (((i) >> 23) & 0x7f)
638#define RELAX_NEW(i) (((i) >> 16) & 0x7f)
9a41af64
TS
639#define RELAX_RELOC1(i) ((valueT) (((i) >> 9) & 0x7f) - 64)
640#define RELAX_RELOC2(i) ((valueT) (((i) >> 2) & 0x7f) - 64)
252b5132
RH
641#define RELAX_RELOC3(i) (((i) >> 1) & 1)
642#define RELAX_WARN(i) ((i) & 1)
643
644/* For mips16 code, we use an entirely different form of relaxation.
645 mips16 supports two versions of most instructions which take
646 immediate values: a small one which takes some small value, and a
647 larger one which takes a 16 bit value. Since branches also follow
648 this pattern, relaxing these values is required.
649
650 We can assemble both mips16 and normal MIPS code in a single
651 object. Therefore, we need to support this type of relaxation at
652 the same time that we support the relaxation described above. We
653 use the high bit of the subtype field to distinguish these cases.
654
655 The information we store for this type of relaxation is the
656 argument code found in the opcode file for this relocation, whether
657 the user explicitly requested a small or extended form, and whether
658 the relocation is in a jump or jal delay slot. That tells us the
659 size of the value, and how it should be stored. We also store
660 whether the fragment is considered to be extended or not. We also
661 store whether this is known to be a branch to a different section,
662 whether we have tried to relax this frag yet, and whether we have
663 ever extended a PC relative fragment because of a shift count. */
664#define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
665 (0x80000000 \
666 | ((type) & 0xff) \
667 | ((small) ? 0x100 : 0) \
668 | ((ext) ? 0x200 : 0) \
669 | ((dslot) ? 0x400 : 0) \
670 | ((jal_dslot) ? 0x800 : 0))
671#define RELAX_MIPS16_P(i) (((i) & 0x80000000) != 0)
672#define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
673#define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
674#define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
675#define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
676#define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
677#define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
678#define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
679#define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
680#define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
681#define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
682#define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
683\f
684/* Prototypes for static functions. */
685
686#ifdef __STDC__
687#define internalError() \
688 as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
689#else
690#define internalError() as_fatal (_("MIPS internal Error"));
691#endif
692
693enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
694
695static int insn_uses_reg PARAMS ((struct mips_cl_insn *ip,
696 unsigned int reg, enum mips_regclass class));
156c2f8b 697static int reg_needs_delay PARAMS ((unsigned int));
252b5132
RH
698static void mips16_mark_labels PARAMS ((void));
699static void append_insn PARAMS ((char *place,
700 struct mips_cl_insn * ip,
701 expressionS * p,
f6688943 702 bfd_reloc_code_real_type *r,
252b5132
RH
703 boolean));
704static void mips_no_prev_insn PARAMS ((int));
705static void mips_emit_delays PARAMS ((boolean));
706#ifdef USE_STDARG
707static void macro_build PARAMS ((char *place, int *counter, expressionS * ep,
708 const char *name, const char *fmt,
709 ...));
710#else
711static void macro_build ();
712#endif
713static void mips16_macro_build PARAMS ((char *, int *, expressionS *,
714 const char *, const char *,
715 va_list));
438c16b8 716static void macro_build_jalr PARAMS ((int, expressionS *));
252b5132
RH
717static void macro_build_lui PARAMS ((char *place, int *counter,
718 expressionS * ep, int regnum));
719static void set_at PARAMS ((int *counter, int reg, int unsignedp));
720static void check_absolute_expr PARAMS ((struct mips_cl_insn * ip,
721 expressionS *));
722static void load_register PARAMS ((int *, int, expressionS *, int));
c9914766 723static void load_address PARAMS ((int *, int, expressionS *, int *));
ea1fb5dc 724static void move_register PARAMS ((int *, int, int));
252b5132
RH
725static void macro PARAMS ((struct mips_cl_insn * ip));
726static void mips16_macro PARAMS ((struct mips_cl_insn * ip));
727#ifdef LOSING_COMPILER
728static void macro2 PARAMS ((struct mips_cl_insn * ip));
729#endif
730static void mips_ip PARAMS ((char *str, struct mips_cl_insn * ip));
731static void mips16_ip PARAMS ((char *str, struct mips_cl_insn * ip));
732static void mips16_immed PARAMS ((char *, unsigned int, int, offsetT, boolean,
733 boolean, boolean, unsigned long *,
734 boolean *, unsigned short *));
394f9b3a 735static int my_getPercentOp PARAMS ((char **, unsigned int *, int *));
ad8d3bb3
TS
736static int my_getSmallParser PARAMS ((char **, unsigned int *, int *));
737static int my_getSmallExpression PARAMS ((expressionS *, char *));
738static void my_getExpression PARAMS ((expressionS *, char *));
ae948b86 739#ifdef OBJ_ELF
e013f690 740static int support_64bit_objects PARAMS((void));
ae948b86 741#endif
316f5878 742static void mips_set_option_string PARAMS ((const char **, const char *));
252b5132
RH
743static symbolS *get_symbol PARAMS ((void));
744static void mips_align PARAMS ((int to, int fill, symbolS *label));
745static void s_align PARAMS ((int));
746static void s_change_sec PARAMS ((int));
747static void s_cons PARAMS ((int));
748static void s_float_cons PARAMS ((int));
749static void s_mips_globl PARAMS ((int));
750static void s_option PARAMS ((int));
751static void s_mipsset PARAMS ((int));
752static void s_abicalls PARAMS ((int));
753static void s_cpload PARAMS ((int));
6478892d
TS
754static void s_cpsetup PARAMS ((int));
755static void s_cplocal PARAMS ((int));
252b5132 756static void s_cprestore PARAMS ((int));
6478892d
TS
757static void s_cpreturn PARAMS ((int));
758static void s_gpvalue PARAMS ((int));
252b5132
RH
759static void s_gpword PARAMS ((int));
760static void s_cpadd PARAMS ((int));
761static void s_insn PARAMS ((int));
762static void md_obj_begin PARAMS ((void));
763static void md_obj_end PARAMS ((void));
764static long get_number PARAMS ((void));
765static void s_mips_ent PARAMS ((int));
766static void s_mips_end PARAMS ((int));
767static void s_mips_frame PARAMS ((int));
768static void s_mips_mask PARAMS ((int));
769static void s_mips_stab PARAMS ((int));
770static void s_mips_weakext PARAMS ((int));
c5dd6aab
DJ
771static void s_mips_file PARAMS ((int));
772static void s_mips_loc PARAMS ((int));
252b5132 773static int mips16_extended_frag PARAMS ((fragS *, asection *, long));
252b5132 774static int validate_mips_insn PARAMS ((const struct mips_opcode *));
316f5878 775static void show PARAMS ((FILE *, const char *, int *, int *));
add55e1f
RS
776#ifdef OBJ_ELF
777static int mips_need_elf_addend_fixup PARAMS ((fixS *));
778#endif
e7af610e 779
ad8d3bb3 780/* Return values of my_getSmallExpression(). */
fb1b3232 781
ad8d3bb3 782enum small_ex_type
fb1b3232
TS
783{
784 S_EX_NONE = 0,
ad8d3bb3
TS
785 S_EX_REGISTER,
786
787 /* Direct relocation creation by %percent_op(). */
788 S_EX_HALF,
fb1b3232 789 S_EX_HI,
ad8d3bb3
TS
790 S_EX_LO,
791 S_EX_GP_REL,
792 S_EX_GOT,
793 S_EX_CALL16,
794 S_EX_GOT_DISP,
795 S_EX_GOT_PAGE,
796 S_EX_GOT_OFST,
797 S_EX_GOT_HI,
798 S_EX_GOT_LO,
799 S_EX_NEG,
fb1b3232
TS
800 S_EX_HIGHER,
801 S_EX_HIGHEST,
ad8d3bb3
TS
802 S_EX_CALL_HI,
803 S_EX_CALL_LO
fb1b3232
TS
804};
805
e7af610e
NC
806/* Table and functions used to map between CPU/ISA names, and
807 ISA levels, and CPU numbers. */
808
e972090a
NC
809struct mips_cpu_info
810{
e7af610e
NC
811 const char *name; /* CPU or ISA name. */
812 int is_isa; /* Is this an ISA? (If 0, a CPU.) */
813 int isa; /* ISA level. */
814 int cpu; /* CPU number (default CPU if ISA). */
815};
816
316f5878
RS
817static void mips_set_architecture PARAMS ((const struct mips_cpu_info *));
818static void mips_set_tune PARAMS ((const struct mips_cpu_info *));
819static boolean mips_strict_matching_cpu_name_p PARAMS ((const char *,
820 const char *));
821static boolean mips_matching_cpu_name_p PARAMS ((const char *, const char *));
822static const struct mips_cpu_info *mips_parse_cpu PARAMS ((const char *,
823 const char *));
e7af610e 824static const struct mips_cpu_info *mips_cpu_info_from_isa PARAMS ((int));
252b5132
RH
825\f
826/* Pseudo-op table.
827
828 The following pseudo-ops from the Kane and Heinrich MIPS book
829 should be defined here, but are currently unsupported: .alias,
830 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
831
832 The following pseudo-ops from the Kane and Heinrich MIPS book are
833 specific to the type of debugging information being generated, and
834 should be defined by the object format: .aent, .begin, .bend,
835 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
836 .vreg.
837
838 The following pseudo-ops from the Kane and Heinrich MIPS book are
839 not MIPS CPU specific, but are also not specific to the object file
840 format. This file is probably the best place to define them, but
841 they are not currently supported: .asm0, .endr, .lab, .repeat,
842 .struct. */
843
e972090a
NC
844static const pseudo_typeS mips_pseudo_table[] =
845{
beae10d5 846 /* MIPS specific pseudo-ops. */
252b5132
RH
847 {"option", s_option, 0},
848 {"set", s_mipsset, 0},
849 {"rdata", s_change_sec, 'r'},
850 {"sdata", s_change_sec, 's'},
851 {"livereg", s_ignore, 0},
852 {"abicalls", s_abicalls, 0},
853 {"cpload", s_cpload, 0},
6478892d
TS
854 {"cpsetup", s_cpsetup, 0},
855 {"cplocal", s_cplocal, 0},
252b5132 856 {"cprestore", s_cprestore, 0},
6478892d
TS
857 {"cpreturn", s_cpreturn, 0},
858 {"gpvalue", s_gpvalue, 0},
252b5132
RH
859 {"gpword", s_gpword, 0},
860 {"cpadd", s_cpadd, 0},
861 {"insn", s_insn, 0},
862
beae10d5 863 /* Relatively generic pseudo-ops that happen to be used on MIPS
252b5132
RH
864 chips. */
865 {"asciiz", stringer, 1},
866 {"bss", s_change_sec, 'b'},
867 {"err", s_err, 0},
868 {"half", s_cons, 1},
869 {"dword", s_cons, 3},
870 {"weakext", s_mips_weakext, 0},
871
beae10d5 872 /* These pseudo-ops are defined in read.c, but must be overridden
252b5132
RH
873 here for one reason or another. */
874 {"align", s_align, 0},
875 {"byte", s_cons, 0},
876 {"data", s_change_sec, 'd'},
877 {"double", s_float_cons, 'd'},
878 {"float", s_float_cons, 'f'},
879 {"globl", s_mips_globl, 0},
880 {"global", s_mips_globl, 0},
881 {"hword", s_cons, 1},
882 {"int", s_cons, 2},
883 {"long", s_cons, 2},
884 {"octa", s_cons, 4},
885 {"quad", s_cons, 3},
886 {"short", s_cons, 1},
887 {"single", s_float_cons, 'f'},
888 {"stabn", s_mips_stab, 'n'},
889 {"text", s_change_sec, 't'},
890 {"word", s_cons, 2},
add56521 891
add56521 892 { "extern", ecoff_directive_extern, 0},
add56521 893
43841e91 894 { NULL, NULL, 0 },
252b5132
RH
895};
896
e972090a
NC
897static const pseudo_typeS mips_nonecoff_pseudo_table[] =
898{
beae10d5
KH
899 /* These pseudo-ops should be defined by the object file format.
900 However, a.out doesn't support them, so we have versions here. */
252b5132
RH
901 {"aent", s_mips_ent, 1},
902 {"bgnb", s_ignore, 0},
903 {"end", s_mips_end, 0},
904 {"endb", s_ignore, 0},
905 {"ent", s_mips_ent, 0},
c5dd6aab 906 {"file", s_mips_file, 0},
252b5132
RH
907 {"fmask", s_mips_mask, 'F'},
908 {"frame", s_mips_frame, 0},
c5dd6aab 909 {"loc", s_mips_loc, 0},
252b5132
RH
910 {"mask", s_mips_mask, 'R'},
911 {"verstamp", s_ignore, 0},
43841e91 912 { NULL, NULL, 0 },
252b5132
RH
913};
914
915extern void pop_insert PARAMS ((const pseudo_typeS *));
916
917void
918mips_pop_insert ()
919{
920 pop_insert (mips_pseudo_table);
921 if (! ECOFF_DEBUGGING)
922 pop_insert (mips_nonecoff_pseudo_table);
923}
924\f
925/* Symbols labelling the current insn. */
926
e972090a
NC
927struct insn_label_list
928{
252b5132
RH
929 struct insn_label_list *next;
930 symbolS *label;
931};
932
933static struct insn_label_list *insn_labels;
934static struct insn_label_list *free_insn_labels;
935
936static void mips_clear_insn_labels PARAMS ((void));
937
938static inline void
939mips_clear_insn_labels ()
940{
941 register struct insn_label_list **pl;
942
943 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
944 ;
945 *pl = insn_labels;
946 insn_labels = NULL;
947}
948\f
949static char *expr_end;
950
951/* Expressions which appear in instructions. These are set by
952 mips_ip. */
953
954static expressionS imm_expr;
955static expressionS offset_expr;
956
957/* Relocs associated with imm_expr and offset_expr. */
958
f6688943
TS
959static bfd_reloc_code_real_type imm_reloc[3]
960 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
961static bfd_reloc_code_real_type offset_reloc[3]
962 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132
RH
963
964/* This is set by mips_ip if imm_reloc is an unmatched HI16_S reloc. */
965
966static boolean imm_unmatched_hi;
967
968/* These are set by mips16_ip if an explicit extension is used. */
969
970static boolean mips16_small, mips16_ext;
971
ecb4347a
DJ
972/* The pdr segment for per procedure frame/regmask info. Not used for
973 ECOFF debugging. */
252b5132
RH
974
975static segT pdr_seg;
252b5132 976
e013f690
TS
977/* The default target format to use. */
978
979const char *
980mips_target_format ()
981{
982 switch (OUTPUT_FLAVOR)
983 {
984 case bfd_target_aout_flavour:
985 return target_big_endian ? "a.out-mips-big" : "a.out-mips-little";
986 case bfd_target_ecoff_flavour:
987 return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
988 case bfd_target_coff_flavour:
989 return "pe-mips";
990 case bfd_target_elf_flavour:
991#ifdef TE_TMIPS
cfe86eaa 992 /* This is traditional mips. */
e013f690 993 return (target_big_endian
cfe86eaa
TS
994 ? (HAVE_64BIT_OBJECTS
995 ? "elf64-tradbigmips"
996 : (HAVE_NEWABI
997 ? "elf32-ntradbigmips" : "elf32-tradbigmips"))
998 : (HAVE_64BIT_OBJECTS
999 ? "elf64-tradlittlemips"
1000 : (HAVE_NEWABI
1001 ? "elf32-ntradlittlemips" : "elf32-tradlittlemips")));
e013f690
TS
1002#else
1003 return (target_big_endian
cfe86eaa
TS
1004 ? (HAVE_64BIT_OBJECTS
1005 ? "elf64-bigmips"
1006 : (HAVE_NEWABI
1007 ? "elf32-nbigmips" : "elf32-bigmips"))
1008 : (HAVE_64BIT_OBJECTS
1009 ? "elf64-littlemips"
1010 : (HAVE_NEWABI
1011 ? "elf32-nlittlemips" : "elf32-littlemips")));
e013f690
TS
1012#endif
1013 default:
1014 abort ();
1015 return NULL;
1016 }
1017}
1018
156c2f8b
NC
1019/* This function is called once, at assembler startup time. It should
1020 set up all the tables, etc. that the MD part of the assembler will need. */
1021
252b5132
RH
1022void
1023md_begin ()
1024{
252b5132 1025 register const char *retval = NULL;
156c2f8b 1026 int i = 0;
252b5132 1027 int broken = 0;
1f25f5d3 1028
ec68c924 1029 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, mips_arch))
252b5132
RH
1030 as_warn (_("Could not set architecture and machine"));
1031
252b5132
RH
1032 op_hash = hash_new ();
1033
1034 for (i = 0; i < NUMOPCODES;)
1035 {
1036 const char *name = mips_opcodes[i].name;
1037
1038 retval = hash_insert (op_hash, name, (PTR) &mips_opcodes[i]);
1039 if (retval != NULL)
1040 {
1041 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1042 mips_opcodes[i].name, retval);
1043 /* Probably a memory allocation problem? Give up now. */
1044 as_fatal (_("Broken assembler. No assembly attempted."));
1045 }
1046 do
1047 {
1048 if (mips_opcodes[i].pinfo != INSN_MACRO)
1049 {
1050 if (!validate_mips_insn (&mips_opcodes[i]))
1051 broken = 1;
1052 }
1053 ++i;
1054 }
1055 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1056 }
1057
1058 mips16_op_hash = hash_new ();
1059
1060 i = 0;
1061 while (i < bfd_mips16_num_opcodes)
1062 {
1063 const char *name = mips16_opcodes[i].name;
1064
1065 retval = hash_insert (mips16_op_hash, name, (PTR) &mips16_opcodes[i]);
1066 if (retval != NULL)
1067 as_fatal (_("internal: can't hash `%s': %s"),
1068 mips16_opcodes[i].name, retval);
1069 do
1070 {
1071 if (mips16_opcodes[i].pinfo != INSN_MACRO
1072 && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1073 != mips16_opcodes[i].match))
1074 {
1075 fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1076 mips16_opcodes[i].name, mips16_opcodes[i].args);
1077 broken = 1;
1078 }
1079 ++i;
1080 }
1081 while (i < bfd_mips16_num_opcodes
1082 && strcmp (mips16_opcodes[i].name, name) == 0);
1083 }
1084
1085 if (broken)
1086 as_fatal (_("Broken assembler. No assembly attempted."));
1087
1088 /* We add all the general register names to the symbol table. This
1089 helps us detect invalid uses of them. */
1090 for (i = 0; i < 32; i++)
1091 {
1092 char buf[5];
1093
1094 sprintf (buf, "$%d", i);
1095 symbol_table_insert (symbol_new (buf, reg_section, i,
1096 &zero_address_frag));
1097 }
76db943d
TS
1098 symbol_table_insert (symbol_new ("$ra", reg_section, RA,
1099 &zero_address_frag));
252b5132
RH
1100 symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1101 &zero_address_frag));
1102 symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1103 &zero_address_frag));
1104 symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1105 &zero_address_frag));
1106 symbol_table_insert (symbol_new ("$at", reg_section, AT,
1107 &zero_address_frag));
1108 symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1109 &zero_address_frag));
1110 symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1111 &zero_address_frag));
85b51719
TS
1112 symbol_table_insert (symbol_new ("$zero", reg_section, ZERO,
1113 &zero_address_frag));
252b5132
RH
1114 symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1115 &zero_address_frag));
1116
1117 mips_no_prev_insn (false);
1118
1119 mips_gprmask = 0;
1120 mips_cprmask[0] = 0;
1121 mips_cprmask[1] = 0;
1122 mips_cprmask[2] = 0;
1123 mips_cprmask[3] = 0;
1124
1125 /* set the default alignment for the text section (2**2) */
1126 record_alignment (text_section, 2);
1127
1128 if (USE_GLOBAL_POINTER_OPT)
1129 bfd_set_gp_size (stdoutput, g_switch_value);
1130
1131 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1132 {
1133 /* On a native system, sections must be aligned to 16 byte
1134 boundaries. When configured for an embedded ELF target, we
1135 don't bother. */
1136 if (strcmp (TARGET_OS, "elf") != 0)
1137 {
1138 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1139 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1140 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1141 }
1142
1143 /* Create a .reginfo section for register masks and a .mdebug
1144 section for debugging information. */
1145 {
1146 segT seg;
1147 subsegT subseg;
1148 flagword flags;
1149 segT sec;
1150
1151 seg = now_seg;
1152 subseg = now_subseg;
1153
1154 /* The ABI says this section should be loaded so that the
1155 running program can access it. However, we don't load it
1156 if we are configured for an embedded target */
1157 flags = SEC_READONLY | SEC_DATA;
1158 if (strcmp (TARGET_OS, "elf") != 0)
1159 flags |= SEC_ALLOC | SEC_LOAD;
1160
316f5878 1161 if (mips_abi != N64_ABI)
252b5132
RH
1162 {
1163 sec = subseg_new (".reginfo", (subsegT) 0);
1164
195325d2
TS
1165 bfd_set_section_flags (stdoutput, sec, flags);
1166 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
bdaaa2e1 1167
252b5132
RH
1168#ifdef OBJ_ELF
1169 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1170#endif
1171 }
1172 else
1173 {
1174 /* The 64-bit ABI uses a .MIPS.options section rather than
1175 .reginfo section. */
1176 sec = subseg_new (".MIPS.options", (subsegT) 0);
195325d2
TS
1177 bfd_set_section_flags (stdoutput, sec, flags);
1178 bfd_set_section_alignment (stdoutput, sec, 3);
252b5132
RH
1179
1180#ifdef OBJ_ELF
1181 /* Set up the option header. */
1182 {
1183 Elf_Internal_Options opthdr;
1184 char *f;
1185
1186 opthdr.kind = ODK_REGINFO;
1187 opthdr.size = (sizeof (Elf_External_Options)
1188 + sizeof (Elf64_External_RegInfo));
1189 opthdr.section = 0;
1190 opthdr.info = 0;
1191 f = frag_more (sizeof (Elf_External_Options));
1192 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1193 (Elf_External_Options *) f);
1194
1195 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1196 }
1197#endif
1198 }
1199
1200 if (ECOFF_DEBUGGING)
1201 {
1202 sec = subseg_new (".mdebug", (subsegT) 0);
1203 (void) bfd_set_section_flags (stdoutput, sec,
1204 SEC_HAS_CONTENTS | SEC_READONLY);
1205 (void) bfd_set_section_alignment (stdoutput, sec, 2);
1206 }
ecb4347a
DJ
1207#ifdef OBJ_ELF
1208 else if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1209 {
1210 pdr_seg = subseg_new (".pdr", (subsegT) 0);
1211 (void) bfd_set_section_flags (stdoutput, pdr_seg,
1212 SEC_READONLY | SEC_RELOC
1213 | SEC_DEBUGGING);
1214 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1215 }
252b5132
RH
1216#endif
1217
1218 subseg_set (seg, subseg);
1219 }
1220 }
1221
1222 if (! ECOFF_DEBUGGING)
1223 md_obj_begin ();
1224}
1225
1226void
1227md_mips_end ()
1228{
1229 if (! ECOFF_DEBUGGING)
1230 md_obj_end ();
1231}
1232
1233void
1234md_assemble (str)
1235 char *str;
1236{
1237 struct mips_cl_insn insn;
f6688943
TS
1238 bfd_reloc_code_real_type unused_reloc[3]
1239 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132
RH
1240
1241 imm_expr.X_op = O_absent;
252b5132
RH
1242 imm_unmatched_hi = false;
1243 offset_expr.X_op = O_absent;
f6688943
TS
1244 imm_reloc[0] = BFD_RELOC_UNUSED;
1245 imm_reloc[1] = BFD_RELOC_UNUSED;
1246 imm_reloc[2] = BFD_RELOC_UNUSED;
1247 offset_reloc[0] = BFD_RELOC_UNUSED;
1248 offset_reloc[1] = BFD_RELOC_UNUSED;
1249 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
1250
1251 if (mips_opts.mips16)
1252 mips16_ip (str, &insn);
1253 else
1254 {
1255 mips_ip (str, &insn);
beae10d5
KH
1256 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1257 str, insn.insn_opcode));
252b5132
RH
1258 }
1259
1260 if (insn_error)
1261 {
1262 as_bad ("%s `%s'", insn_error, str);
1263 return;
1264 }
1265
1266 if (insn.insn_mo->pinfo == INSN_MACRO)
1267 {
1268 if (mips_opts.mips16)
1269 mips16_macro (&insn);
1270 else
1271 macro (&insn);
1272 }
1273 else
1274 {
1275 if (imm_expr.X_op != O_absent)
c4e7957c 1276 append_insn (NULL, &insn, &imm_expr, imm_reloc, imm_unmatched_hi);
252b5132 1277 else if (offset_expr.X_op != O_absent)
c4e7957c 1278 append_insn (NULL, &insn, &offset_expr, offset_reloc, false);
252b5132 1279 else
c4e7957c 1280 append_insn (NULL, &insn, NULL, unused_reloc, false);
252b5132
RH
1281 }
1282}
1283
1284/* See whether instruction IP reads register REG. CLASS is the type
1285 of register. */
1286
1287static int
1288insn_uses_reg (ip, reg, class)
1289 struct mips_cl_insn *ip;
1290 unsigned int reg;
1291 enum mips_regclass class;
1292{
1293 if (class == MIPS16_REG)
1294 {
1295 assert (mips_opts.mips16);
1296 reg = mips16_to_32_reg_map[reg];
1297 class = MIPS_GR_REG;
1298 }
1299
85b51719
TS
1300 /* Don't report on general register ZERO, since it never changes. */
1301 if (class == MIPS_GR_REG && reg == ZERO)
252b5132
RH
1302 return 0;
1303
1304 if (class == MIPS_FP_REG)
1305 {
1306 assert (! mips_opts.mips16);
1307 /* If we are called with either $f0 or $f1, we must check $f0.
1308 This is not optimal, because it will introduce an unnecessary
1309 NOP between "lwc1 $f0" and "swc1 $f1". To fix this we would
1310 need to distinguish reading both $f0 and $f1 or just one of
1311 them. Note that we don't have to check the other way,
1312 because there is no instruction that sets both $f0 and $f1
1313 and requires a delay. */
1314 if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1315 && ((((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS) &~(unsigned)1)
1316 == (reg &~ (unsigned) 1)))
1317 return 1;
1318 if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1319 && ((((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT) &~(unsigned)1)
1320 == (reg &~ (unsigned) 1)))
1321 return 1;
1322 }
1323 else if (! mips_opts.mips16)
1324 {
1325 if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1326 && ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == reg)
1327 return 1;
1328 if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1329 && ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT) == reg)
1330 return 1;
1331 }
1332 else
1333 {
1334 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1335 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RX)
1336 & MIPS16OP_MASK_RX)]
1337 == reg))
1338 return 1;
1339 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1340 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RY)
1341 & MIPS16OP_MASK_RY)]
1342 == reg))
1343 return 1;
1344 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1345 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
1346 & MIPS16OP_MASK_MOVE32Z)]
1347 == reg))
1348 return 1;
1349 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1350 return 1;
1351 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1352 return 1;
1353 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1354 return 1;
1355 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1356 && ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
1357 & MIPS16OP_MASK_REGR32) == reg)
1358 return 1;
1359 }
1360
1361 return 0;
1362}
1363
1364/* This function returns true if modifying a register requires a
1365 delay. */
1366
1367static int
1368reg_needs_delay (reg)
156c2f8b 1369 unsigned int reg;
252b5132
RH
1370{
1371 unsigned long prev_pinfo;
1372
1373 prev_pinfo = prev_insn.insn_mo->pinfo;
1374 if (! mips_opts.noreorder
9ce8a5dd 1375 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
1376 && ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1377 || (! gpr_interlocks
1378 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1379 {
1380 /* A load from a coprocessor or from memory. All load
1381 delays delay the use of general register rt for one
1382 instruction on the r3000. The r6000 and r4000 use
1383 interlocks. */
bdaaa2e1 1384 /* Itbl support may require additional care here. */
252b5132
RH
1385 know (prev_pinfo & INSN_WRITE_GPR_T);
1386 if (reg == ((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT))
1387 return 1;
1388 }
1389
1390 return 0;
1391}
1392
1393/* Mark instruction labels in mips16 mode. This permits the linker to
1394 handle them specially, such as generating jalx instructions when
1395 needed. We also make them odd for the duration of the assembly, in
1396 order to generate the right sort of code. We will make them even
1397 in the adjust_symtab routine, while leaving them marked. This is
1398 convenient for the debugger and the disassembler. The linker knows
1399 to make them odd again. */
1400
1401static void
1402mips16_mark_labels ()
1403{
1404 if (mips_opts.mips16)
1405 {
1406 struct insn_label_list *l;
98aa84af 1407 valueT val;
252b5132
RH
1408
1409 for (l = insn_labels; l != NULL; l = l->next)
1410 {
1411#ifdef OBJ_ELF
1412 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1413 S_SET_OTHER (l->label, STO_MIPS16);
1414#endif
98aa84af
AM
1415 val = S_GET_VALUE (l->label);
1416 if ((val & 1) == 0)
1417 S_SET_VALUE (l->label, val + 1);
252b5132
RH
1418 }
1419 }
1420}
1421
1422/* Output an instruction. PLACE is where to put the instruction; if
1423 it is NULL, this uses frag_more to get room. IP is the instruction
1424 information. ADDRESS_EXPR is an operand of the instruction to be
1425 used with RELOC_TYPE. */
1426
1427static void
1428append_insn (place, ip, address_expr, reloc_type, unmatched_hi)
1429 char *place;
1430 struct mips_cl_insn *ip;
1431 expressionS *address_expr;
f6688943 1432 bfd_reloc_code_real_type *reloc_type;
252b5132
RH
1433 boolean unmatched_hi;
1434{
1435 register unsigned long prev_pinfo, pinfo;
1436 char *f;
f6688943 1437 fixS *fixp[3];
252b5132
RH
1438 int nops = 0;
1439
1440 /* Mark instruction labels in mips16 mode. */
f9419b05 1441 mips16_mark_labels ();
252b5132
RH
1442
1443 prev_pinfo = prev_insn.insn_mo->pinfo;
1444 pinfo = ip->insn_mo->pinfo;
1445
1446 if (place == NULL && (! mips_opts.noreorder || prev_nop_frag != NULL))
1447 {
1448 int prev_prev_nop;
1449
1450 /* If the previous insn required any delay slots, see if we need
1451 to insert a NOP or two. There are eight kinds of possible
1452 hazards, of which an instruction can have at most one type.
1453 (1) a load from memory delay
1454 (2) a load from a coprocessor delay
1455 (3) an unconditional branch delay
1456 (4) a conditional branch delay
1457 (5) a move to coprocessor register delay
1458 (6) a load coprocessor register from memory delay
1459 (7) a coprocessor condition code delay
1460 (8) a HI/LO special register delay
1461
1462 There are a lot of optimizations we could do that we don't.
1463 In particular, we do not, in general, reorder instructions.
1464 If you use gcc with optimization, it will reorder
1465 instructions and generally do much more optimization then we
1466 do here; repeating all that work in the assembler would only
1467 benefit hand written assembly code, and does not seem worth
1468 it. */
1469
1470 /* This is how a NOP is emitted. */
1471#define emit_nop() \
1472 (mips_opts.mips16 \
1473 ? md_number_to_chars (frag_more (2), 0x6500, 2) \
1474 : md_number_to_chars (frag_more (4), 0, 4))
1475
1476 /* The previous insn might require a delay slot, depending upon
1477 the contents of the current insn. */
1478 if (! mips_opts.mips16
9ce8a5dd 1479 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
1480 && (((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1481 && ! cop_interlocks)
1482 || (! gpr_interlocks
1483 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1484 {
1485 /* A load from a coprocessor or from memory. All load
1486 delays delay the use of general register rt for one
1487 instruction on the r3000. The r6000 and r4000 use
1488 interlocks. */
beae10d5 1489 /* Itbl support may require additional care here. */
252b5132
RH
1490 know (prev_pinfo & INSN_WRITE_GPR_T);
1491 if (mips_optimize == 0
1492 || insn_uses_reg (ip,
1493 ((prev_insn.insn_opcode >> OP_SH_RT)
1494 & OP_MASK_RT),
1495 MIPS_GR_REG))
1496 ++nops;
1497 }
1498 else if (! mips_opts.mips16
9ce8a5dd 1499 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132 1500 && (((prev_pinfo & INSN_COPROC_MOVE_DELAY)
beae10d5 1501 && ! cop_interlocks)
e7af610e 1502 || (mips_opts.isa == ISA_MIPS1
252b5132
RH
1503 && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))))
1504 {
1505 /* A generic coprocessor delay. The previous instruction
1506 modified a coprocessor general or control register. If
1507 it modified a control register, we need to avoid any
1508 coprocessor instruction (this is probably not always
1509 required, but it sometimes is). If it modified a general
1510 register, we avoid using that register.
1511
1512 On the r6000 and r4000 loading a coprocessor register
1513 from memory is interlocked, and does not require a delay.
1514
1515 This case is not handled very well. There is no special
1516 knowledge of CP0 handling, and the coprocessors other
1517 than the floating point unit are not distinguished at
1518 all. */
1519 /* Itbl support may require additional care here. FIXME!
bdaaa2e1 1520 Need to modify this to include knowledge about
252b5132
RH
1521 user specified delays! */
1522 if (prev_pinfo & INSN_WRITE_FPR_T)
1523 {
1524 if (mips_optimize == 0
1525 || insn_uses_reg (ip,
1526 ((prev_insn.insn_opcode >> OP_SH_FT)
1527 & OP_MASK_FT),
1528 MIPS_FP_REG))
1529 ++nops;
1530 }
1531 else if (prev_pinfo & INSN_WRITE_FPR_S)
1532 {
1533 if (mips_optimize == 0
1534 || insn_uses_reg (ip,
1535 ((prev_insn.insn_opcode >> OP_SH_FS)
1536 & OP_MASK_FS),
1537 MIPS_FP_REG))
1538 ++nops;
1539 }
1540 else
1541 {
1542 /* We don't know exactly what the previous instruction
1543 does. If the current instruction uses a coprocessor
1544 register, we must insert a NOP. If previous
1545 instruction may set the condition codes, and the
1546 current instruction uses them, we must insert two
1547 NOPS. */
bdaaa2e1 1548 /* Itbl support may require additional care here. */
252b5132
RH
1549 if (mips_optimize == 0
1550 || ((prev_pinfo & INSN_WRITE_COND_CODE)
1551 && (pinfo & INSN_READ_COND_CODE)))
1552 nops += 2;
1553 else if (pinfo & INSN_COP)
1554 ++nops;
1555 }
1556 }
1557 else if (! mips_opts.mips16
9ce8a5dd 1558 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
1559 && (prev_pinfo & INSN_WRITE_COND_CODE)
1560 && ! cop_interlocks)
1561 {
1562 /* The previous instruction sets the coprocessor condition
1563 codes, but does not require a general coprocessor delay
1564 (this means it is a floating point comparison
1565 instruction). If this instruction uses the condition
1566 codes, we need to insert a single NOP. */
beae10d5 1567 /* Itbl support may require additional care here. */
252b5132
RH
1568 if (mips_optimize == 0
1569 || (pinfo & INSN_READ_COND_CODE))
1570 ++nops;
1571 }
6b76fefe
CM
1572
1573 /* If we're fixing up mfhi/mflo for the r7000 and the
1574 previous insn was an mfhi/mflo and the current insn
1575 reads the register that the mfhi/mflo wrote to, then
1576 insert two nops. */
1577
1578 else if (mips_7000_hilo_fix
1579 && MF_HILO_INSN (prev_pinfo)
1580 && insn_uses_reg (ip, ((prev_insn.insn_opcode >> OP_SH_RD)
beae10d5
KH
1581 & OP_MASK_RD),
1582 MIPS_GR_REG))
6b76fefe
CM
1583 {
1584 nops += 2;
1585 }
1586
1587 /* If we're fixing up mfhi/mflo for the r7000 and the
1588 2nd previous insn was an mfhi/mflo and the current insn
1589 reads the register that the mfhi/mflo wrote to, then
1590 insert one nop. */
1591
1592 else if (mips_7000_hilo_fix
1593 && MF_HILO_INSN (prev_prev_insn.insn_opcode)
1594 && insn_uses_reg (ip, ((prev_prev_insn.insn_opcode >> OP_SH_RD)
1595 & OP_MASK_RD),
1596 MIPS_GR_REG))
bdaaa2e1 1597
6b76fefe 1598 {
f9419b05 1599 ++nops;
6b76fefe 1600 }
bdaaa2e1 1601
252b5132
RH
1602 else if (prev_pinfo & INSN_READ_LO)
1603 {
1604 /* The previous instruction reads the LO register; if the
1605 current instruction writes to the LO register, we must
bdaaa2e1
KH
1606 insert two NOPS. Some newer processors have interlocks.
1607 Also the tx39's multiply instructions can be exectuted
252b5132 1608 immediatly after a read from HI/LO (without the delay),
bdaaa2e1
KH
1609 though the tx39's divide insns still do require the
1610 delay. */
252b5132 1611 if (! (hilo_interlocks
ec68c924 1612 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
252b5132
RH
1613 && (mips_optimize == 0
1614 || (pinfo & INSN_WRITE_LO)))
1615 nops += 2;
1616 /* Most mips16 branch insns don't have a delay slot.
1617 If a read from LO is immediately followed by a branch
1618 to a write to LO we have a read followed by a write
1619 less than 2 insns away. We assume the target of
1620 a branch might be a write to LO, and insert a nop
bdaaa2e1 1621 between a read and an immediately following branch. */
252b5132
RH
1622 else if (mips_opts.mips16
1623 && (mips_optimize == 0
1624 || (pinfo & MIPS16_INSN_BRANCH)))
f9419b05 1625 ++nops;
252b5132
RH
1626 }
1627 else if (prev_insn.insn_mo->pinfo & INSN_READ_HI)
1628 {
1629 /* The previous instruction reads the HI register; if the
1630 current instruction writes to the HI register, we must
1631 insert a NOP. Some newer processors have interlocks.
bdaaa2e1 1632 Also the note tx39's multiply above. */
252b5132 1633 if (! (hilo_interlocks
ec68c924 1634 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
252b5132
RH
1635 && (mips_optimize == 0
1636 || (pinfo & INSN_WRITE_HI)))
1637 nops += 2;
1638 /* Most mips16 branch insns don't have a delay slot.
1639 If a read from HI is immediately followed by a branch
1640 to a write to HI we have a read followed by a write
1641 less than 2 insns away. We assume the target of
1642 a branch might be a write to HI, and insert a nop
bdaaa2e1 1643 between a read and an immediately following branch. */
252b5132
RH
1644 else if (mips_opts.mips16
1645 && (mips_optimize == 0
1646 || (pinfo & MIPS16_INSN_BRANCH)))
f9419b05 1647 ++nops;
252b5132
RH
1648 }
1649
1650 /* If the previous instruction was in a noreorder section, then
1651 we don't want to insert the nop after all. */
bdaaa2e1 1652 /* Itbl support may require additional care here. */
252b5132
RH
1653 if (prev_insn_unreordered)
1654 nops = 0;
1655
1656 /* There are two cases which require two intervening
1657 instructions: 1) setting the condition codes using a move to
1658 coprocessor instruction which requires a general coprocessor
1659 delay and then reading the condition codes 2) reading the HI
1660 or LO register and then writing to it (except on processors
1661 which have interlocks). If we are not already emitting a NOP
1662 instruction, we must check for these cases compared to the
1663 instruction previous to the previous instruction. */
1664 if ((! mips_opts.mips16
9ce8a5dd 1665 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
1666 && (prev_prev_insn.insn_mo->pinfo & INSN_COPROC_MOVE_DELAY)
1667 && (prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
1668 && (pinfo & INSN_READ_COND_CODE)
1669 && ! cop_interlocks)
1670 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_LO)
1671 && (pinfo & INSN_WRITE_LO)
1672 && ! (hilo_interlocks
ec68c924 1673 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT))))
252b5132
RH
1674 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
1675 && (pinfo & INSN_WRITE_HI)
1676 && ! (hilo_interlocks
ec68c924 1677 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))))
252b5132
RH
1678 prev_prev_nop = 1;
1679 else
1680 prev_prev_nop = 0;
1681
1682 if (prev_prev_insn_unreordered)
1683 prev_prev_nop = 0;
1684
1685 if (prev_prev_nop && nops == 0)
1686 ++nops;
1687
1688 /* If we are being given a nop instruction, don't bother with
1689 one of the nops we would otherwise output. This will only
1690 happen when a nop instruction is used with mips_optimize set
1691 to 0. */
1692 if (nops > 0
1693 && ! mips_opts.noreorder
156c2f8b 1694 && ip->insn_opcode == (unsigned) (mips_opts.mips16 ? 0x6500 : 0))
252b5132
RH
1695 --nops;
1696
1697 /* Now emit the right number of NOP instructions. */
1698 if (nops > 0 && ! mips_opts.noreorder)
1699 {
1700 fragS *old_frag;
1701 unsigned long old_frag_offset;
1702 int i;
1703 struct insn_label_list *l;
1704
1705 old_frag = frag_now;
1706 old_frag_offset = frag_now_fix ();
1707
1708 for (i = 0; i < nops; i++)
1709 emit_nop ();
1710
1711 if (listing)
1712 {
1713 listing_prev_line ();
1714 /* We may be at the start of a variant frag. In case we
1715 are, make sure there is enough space for the frag
1716 after the frags created by listing_prev_line. The
1717 argument to frag_grow here must be at least as large
1718 as the argument to all other calls to frag_grow in
1719 this file. We don't have to worry about being in the
1720 middle of a variant frag, because the variants insert
1721 all needed nop instructions themselves. */
1722 frag_grow (40);
1723 }
1724
1725 for (l = insn_labels; l != NULL; l = l->next)
1726 {
98aa84af
AM
1727 valueT val;
1728
252b5132 1729 assert (S_GET_SEGMENT (l->label) == now_seg);
49309057 1730 symbol_set_frag (l->label, frag_now);
98aa84af 1731 val = (valueT) frag_now_fix ();
252b5132
RH
1732 /* mips16 text labels are stored as odd. */
1733 if (mips_opts.mips16)
f9419b05 1734 ++val;
98aa84af 1735 S_SET_VALUE (l->label, val);
252b5132
RH
1736 }
1737
1738#ifndef NO_ECOFF_DEBUGGING
1739 if (ECOFF_DEBUGGING)
1740 ecoff_fix_loc (old_frag, old_frag_offset);
1741#endif
1742 }
1743 else if (prev_nop_frag != NULL)
1744 {
1745 /* We have a frag holding nops we may be able to remove. If
1746 we don't need any nops, we can decrease the size of
1747 prev_nop_frag by the size of one instruction. If we do
bdaaa2e1 1748 need some nops, we count them in prev_nops_required. */
252b5132
RH
1749 if (prev_nop_frag_since == 0)
1750 {
1751 if (nops == 0)
1752 {
1753 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1754 --prev_nop_frag_holds;
1755 }
1756 else
1757 prev_nop_frag_required += nops;
1758 }
1759 else
1760 {
1761 if (prev_prev_nop == 0)
1762 {
1763 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1764 --prev_nop_frag_holds;
1765 }
1766 else
1767 ++prev_nop_frag_required;
1768 }
1769
1770 if (prev_nop_frag_holds <= prev_nop_frag_required)
1771 prev_nop_frag = NULL;
1772
1773 ++prev_nop_frag_since;
1774
1775 /* Sanity check: by the time we reach the second instruction
1776 after prev_nop_frag, we should have used up all the nops
1777 one way or another. */
1778 assert (prev_nop_frag_since <= 1 || prev_nop_frag == NULL);
1779 }
1780 }
1781
f6688943 1782 if (*reloc_type > BFD_RELOC_UNUSED)
252b5132
RH
1783 {
1784 /* We need to set up a variant frag. */
1785 assert (mips_opts.mips16 && address_expr != NULL);
1786 f = frag_var (rs_machine_dependent, 4, 0,
f6688943 1787 RELAX_MIPS16_ENCODE (*reloc_type - BFD_RELOC_UNUSED,
252b5132
RH
1788 mips16_small, mips16_ext,
1789 (prev_pinfo
1790 & INSN_UNCOND_BRANCH_DELAY),
f6688943 1791 (*prev_insn_reloc_type
252b5132 1792 == BFD_RELOC_MIPS16_JMP)),
c4e7957c 1793 make_expr_symbol (address_expr), 0, NULL);
252b5132
RH
1794 }
1795 else if (place != NULL)
1796 f = place;
1797 else if (mips_opts.mips16
1798 && ! ip->use_extend
f6688943 1799 && *reloc_type != BFD_RELOC_MIPS16_JMP)
252b5132
RH
1800 {
1801 /* Make sure there is enough room to swap this instruction with
1802 a following jump instruction. */
1803 frag_grow (6);
1804 f = frag_more (2);
1805 }
1806 else
1807 {
1808 if (mips_opts.mips16
1809 && mips_opts.noreorder
1810 && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
1811 as_warn (_("extended instruction in delay slot"));
1812
1813 f = frag_more (4);
1814 }
1815
f6688943
TS
1816 fixp[0] = fixp[1] = fixp[2] = NULL;
1817 if (address_expr != NULL && *reloc_type < BFD_RELOC_UNUSED)
252b5132
RH
1818 {
1819 if (address_expr->X_op == O_constant)
1820 {
4db1a35d 1821 valueT tmp;
f6688943
TS
1822
1823 switch (*reloc_type)
252b5132
RH
1824 {
1825 case BFD_RELOC_32:
1826 ip->insn_opcode |= address_expr->X_add_number;
1827 break;
1828
f6688943
TS
1829 case BFD_RELOC_MIPS_HIGHEST:
1830 tmp = (address_expr->X_add_number + 0x800080008000) >> 16;
1831 tmp >>= 16;
1832 ip->insn_opcode |= (tmp >> 16) & 0xffff;
1833 break;
1834
1835 case BFD_RELOC_MIPS_HIGHER:
1836 tmp = (address_expr->X_add_number + 0x80008000) >> 16;
1837 ip->insn_opcode |= (tmp >> 16) & 0xffff;
1838 break;
1839
1840 case BFD_RELOC_HI16_S:
1841 ip->insn_opcode |= ((address_expr->X_add_number + 0x8000)
1842 >> 16) & 0xffff;
1843 break;
1844
1845 case BFD_RELOC_HI16:
1846 ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
1847 break;
1848
252b5132
RH
1849 case BFD_RELOC_LO16:
1850 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
1851 break;
1852
1853 case BFD_RELOC_MIPS_JMP:
1854 if ((address_expr->X_add_number & 3) != 0)
1855 as_bad (_("jump to misaligned address (0x%lx)"),
1856 (unsigned long) address_expr->X_add_number);
7496292d
TS
1857 if (address_expr->X_add_number & ~0xfffffff
1858 || address_expr->X_add_number > 0x7fffffc)
1859 as_bad (_("jump address range overflow (0x%lx)"),
1860 (unsigned long) address_expr->X_add_number);
252b5132
RH
1861 ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
1862 break;
1863
1864 case BFD_RELOC_MIPS16_JMP:
1865 if ((address_expr->X_add_number & 3) != 0)
1866 as_bad (_("jump to misaligned address (0x%lx)"),
1867 (unsigned long) address_expr->X_add_number);
7496292d
TS
1868 if (address_expr->X_add_number & ~0xfffffff
1869 || address_expr->X_add_number > 0x7fffffc)
1870 as_bad (_("jump address range overflow (0x%lx)"),
1871 (unsigned long) address_expr->X_add_number);
252b5132
RH
1872 ip->insn_opcode |=
1873 (((address_expr->X_add_number & 0x7c0000) << 3)
1874 | ((address_expr->X_add_number & 0xf800000) >> 7)
1875 | ((address_expr->X_add_number & 0x3fffc) >> 2));
1876 break;
1877
cb56d3d3 1878 case BFD_RELOC_16_PCREL:
233b8738 1879 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
cb56d3d3
TS
1880 break;
1881
252b5132
RH
1882 case BFD_RELOC_16_PCREL_S2:
1883 goto need_reloc;
1884
1885 default:
1886 internalError ();
1887 }
1888 }
1889 else
1890 {
1891 need_reloc:
f6688943 1892 /* Don't generate a reloc if we are writing into a variant frag. */
252b5132
RH
1893 if (place == NULL)
1894 {
f6688943
TS
1895 fixp[0] = fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
1896 address_expr,
1897 (*reloc_type == BFD_RELOC_16_PCREL
1898 || *reloc_type == BFD_RELOC_16_PCREL_S2),
1899 reloc_type[0]);
1900
b6ff326e 1901 /* These relocations can have an addend that won't fit in
f6688943
TS
1902 4 octets for 64bit assembly. */
1903 if (HAVE_64BIT_GPRS &&
1904 (*reloc_type == BFD_RELOC_16
98d3f06f
KH
1905 || *reloc_type == BFD_RELOC_32
1906 || *reloc_type == BFD_RELOC_MIPS_JMP
1907 || *reloc_type == BFD_RELOC_HI16_S
1908 || *reloc_type == BFD_RELOC_LO16
1909 || *reloc_type == BFD_RELOC_GPREL16
1910 || *reloc_type == BFD_RELOC_MIPS_LITERAL
1911 || *reloc_type == BFD_RELOC_GPREL32
1912 || *reloc_type == BFD_RELOC_64
1913 || *reloc_type == BFD_RELOC_CTOR
1914 || *reloc_type == BFD_RELOC_MIPS_SUB
1915 || *reloc_type == BFD_RELOC_MIPS_HIGHEST
1916 || *reloc_type == BFD_RELOC_MIPS_HIGHER
1917 || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
1918 || *reloc_type == BFD_RELOC_MIPS_REL16
1919 || *reloc_type == BFD_RELOC_MIPS_RELGOT))
f6688943
TS
1920 fixp[0]->fx_no_overflow = 1;
1921
252b5132
RH
1922 if (unmatched_hi)
1923 {
1924 struct mips_hi_fixup *hi_fixup;
1925
f6688943 1926 assert (*reloc_type == BFD_RELOC_HI16_S);
252b5132
RH
1927 hi_fixup = ((struct mips_hi_fixup *)
1928 xmalloc (sizeof (struct mips_hi_fixup)));
f6688943 1929 hi_fixup->fixp = fixp[0];
252b5132
RH
1930 hi_fixup->seg = now_seg;
1931 hi_fixup->next = mips_hi_fixup_list;
1932 mips_hi_fixup_list = hi_fixup;
1933 }
f6688943
TS
1934
1935 if (reloc_type[1] != BFD_RELOC_UNUSED)
1936 {
1937 /* FIXME: This symbol can be one of
1938 RSS_UNDEF, RSS_GP, RSS_GP0, RSS_LOC. */
1939 address_expr->X_op = O_absent;
1940 address_expr->X_add_symbol = 0;
1941 address_expr->X_add_number = 0;
1942
1943 fixp[1] = fix_new_exp (frag_now, f - frag_now->fr_literal,
1944 4, address_expr, false,
1945 reloc_type[1]);
1946
b6ff326e 1947 /* These relocations can have an addend that won't fit in
f6688943
TS
1948 4 octets for 64bit assembly. */
1949 if (HAVE_64BIT_GPRS &&
1950 (*reloc_type == BFD_RELOC_16
1951 || *reloc_type == BFD_RELOC_32
1952 || *reloc_type == BFD_RELOC_MIPS_JMP
1953 || *reloc_type == BFD_RELOC_HI16_S
1954 || *reloc_type == BFD_RELOC_LO16
1955 || *reloc_type == BFD_RELOC_GPREL16
1956 || *reloc_type == BFD_RELOC_MIPS_LITERAL
1957 || *reloc_type == BFD_RELOC_GPREL32
1958 || *reloc_type == BFD_RELOC_64
1959 || *reloc_type == BFD_RELOC_CTOR
1960 || *reloc_type == BFD_RELOC_MIPS_SUB
1961 || *reloc_type == BFD_RELOC_MIPS_HIGHEST
1962 || *reloc_type == BFD_RELOC_MIPS_HIGHER
1963 || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
1964 || *reloc_type == BFD_RELOC_MIPS_REL16
1965 || *reloc_type == BFD_RELOC_MIPS_RELGOT))
98d3f06f 1966 fixp[1]->fx_no_overflow = 1;
f6688943
TS
1967
1968 if (reloc_type[2] != BFD_RELOC_UNUSED)
1969 {
1970 address_expr->X_op = O_absent;
1971 address_expr->X_add_symbol = 0;
1972 address_expr->X_add_number = 0;
1973
1974 fixp[2] = fix_new_exp (frag_now,
1975 f - frag_now->fr_literal, 4,
1976 address_expr, false,
1977 reloc_type[2]);
1978
b6ff326e 1979 /* These relocations can have an addend that won't fit in
f6688943
TS
1980 4 octets for 64bit assembly. */
1981 if (HAVE_64BIT_GPRS &&
1982 (*reloc_type == BFD_RELOC_16
1983 || *reloc_type == BFD_RELOC_32
1984 || *reloc_type == BFD_RELOC_MIPS_JMP
1985 || *reloc_type == BFD_RELOC_HI16_S
1986 || *reloc_type == BFD_RELOC_LO16
1987 || *reloc_type == BFD_RELOC_GPREL16
1988 || *reloc_type == BFD_RELOC_MIPS_LITERAL
1989 || *reloc_type == BFD_RELOC_GPREL32
1990 || *reloc_type == BFD_RELOC_64
1991 || *reloc_type == BFD_RELOC_CTOR
1992 || *reloc_type == BFD_RELOC_MIPS_SUB
1993 || *reloc_type == BFD_RELOC_MIPS_HIGHEST
1994 || *reloc_type == BFD_RELOC_MIPS_HIGHER
1995 || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
1996 || *reloc_type == BFD_RELOC_MIPS_REL16
1997 || *reloc_type == BFD_RELOC_MIPS_RELGOT))
98d3f06f 1998 fixp[2]->fx_no_overflow = 1;
f6688943
TS
1999 }
2000 }
252b5132
RH
2001 }
2002 }
2003 }
2004
2005 if (! mips_opts.mips16)
c5dd6aab
DJ
2006 {
2007 md_number_to_chars (f, ip->insn_opcode, 4);
2008#ifdef OBJ_ELF
2009 dwarf2_emit_insn (4);
2010#endif
2011 }
f6688943 2012 else if (*reloc_type == BFD_RELOC_MIPS16_JMP)
252b5132
RH
2013 {
2014 md_number_to_chars (f, ip->insn_opcode >> 16, 2);
2015 md_number_to_chars (f + 2, ip->insn_opcode & 0xffff, 2);
c5dd6aab
DJ
2016#ifdef OBJ_ELF
2017 dwarf2_emit_insn (4);
2018#endif
252b5132
RH
2019 }
2020 else
2021 {
2022 if (ip->use_extend)
2023 {
2024 md_number_to_chars (f, 0xf000 | ip->extend, 2);
2025 f += 2;
2026 }
2027 md_number_to_chars (f, ip->insn_opcode, 2);
c5dd6aab
DJ
2028#ifdef OBJ_ELF
2029 dwarf2_emit_insn (ip->use_extend ? 4 : 2);
2030#endif
252b5132
RH
2031 }
2032
2033 /* Update the register mask information. */
2034 if (! mips_opts.mips16)
2035 {
2036 if (pinfo & INSN_WRITE_GPR_D)
2037 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
2038 if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2039 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
2040 if (pinfo & INSN_READ_GPR_S)
2041 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
2042 if (pinfo & INSN_WRITE_GPR_31)
f9419b05 2043 mips_gprmask |= 1 << RA;
252b5132
RH
2044 if (pinfo & INSN_WRITE_FPR_D)
2045 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
2046 if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2047 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
2048 if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2049 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
2050 if ((pinfo & INSN_READ_FPR_R) != 0)
2051 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
2052 if (pinfo & INSN_COP)
2053 {
bdaaa2e1
KH
2054 /* We don't keep enough information to sort these cases out.
2055 The itbl support does keep this information however, although
2056 we currently don't support itbl fprmats as part of the cop
2057 instruction. May want to add this support in the future. */
252b5132
RH
2058 }
2059 /* Never set the bit for $0, which is always zero. */
beae10d5 2060 mips_gprmask &= ~1 << 0;
252b5132
RH
2061 }
2062 else
2063 {
2064 if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2065 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RX)
2066 & MIPS16OP_MASK_RX);
2067 if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2068 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RY)
2069 & MIPS16OP_MASK_RY);
2070 if (pinfo & MIPS16_INSN_WRITE_Z)
2071 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RZ)
2072 & MIPS16OP_MASK_RZ);
2073 if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2074 mips_gprmask |= 1 << TREG;
2075 if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2076 mips_gprmask |= 1 << SP;
2077 if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2078 mips_gprmask |= 1 << RA;
2079 if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2080 mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2081 if (pinfo & MIPS16_INSN_READ_Z)
2082 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
2083 & MIPS16OP_MASK_MOVE32Z);
2084 if (pinfo & MIPS16_INSN_READ_GPR_X)
2085 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
2086 & MIPS16OP_MASK_REGR32);
2087 }
2088
2089 if (place == NULL && ! mips_opts.noreorder)
2090 {
2091 /* Filling the branch delay slot is more complex. We try to
2092 switch the branch with the previous instruction, which we can
2093 do if the previous instruction does not set up a condition
2094 that the branch tests and if the branch is not itself the
2095 target of any branch. */
2096 if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2097 || (pinfo & INSN_COND_BRANCH_DELAY))
2098 {
2099 if (mips_optimize < 2
2100 /* If we have seen .set volatile or .set nomove, don't
2101 optimize. */
2102 || mips_opts.nomove != 0
2103 /* If we had to emit any NOP instructions, then we
2104 already know we can not swap. */
2105 || nops != 0
2106 /* If we don't even know the previous insn, we can not
bdaaa2e1 2107 swap. */
252b5132
RH
2108 || ! prev_insn_valid
2109 /* If the previous insn is already in a branch delay
2110 slot, then we can not swap. */
2111 || prev_insn_is_delay_slot
2112 /* If the previous previous insn was in a .set
2113 noreorder, we can't swap. Actually, the MIPS
2114 assembler will swap in this situation. However, gcc
2115 configured -with-gnu-as will generate code like
2116 .set noreorder
2117 lw $4,XXX
2118 .set reorder
2119 INSN
2120 bne $4,$0,foo
2121 in which we can not swap the bne and INSN. If gcc is
2122 not configured -with-gnu-as, it does not output the
2123 .set pseudo-ops. We don't have to check
2124 prev_insn_unreordered, because prev_insn_valid will
2125 be 0 in that case. We don't want to use
2126 prev_prev_insn_valid, because we do want to be able
2127 to swap at the start of a function. */
2128 || prev_prev_insn_unreordered
2129 /* If the branch is itself the target of a branch, we
2130 can not swap. We cheat on this; all we check for is
2131 whether there is a label on this instruction. If
2132 there are any branches to anything other than a
2133 label, users must use .set noreorder. */
2134 || insn_labels != NULL
2135 /* If the previous instruction is in a variant frag, we
2136 can not do the swap. This does not apply to the
2137 mips16, which uses variant frags for different
2138 purposes. */
2139 || (! mips_opts.mips16
2140 && prev_insn_frag->fr_type == rs_machine_dependent)
2141 /* If the branch reads the condition codes, we don't
2142 even try to swap, because in the sequence
2143 ctc1 $X,$31
2144 INSN
2145 INSN
2146 bc1t LABEL
2147 we can not swap, and I don't feel like handling that
2148 case. */
2149 || (! mips_opts.mips16
9ce8a5dd 2150 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
2151 && (pinfo & INSN_READ_COND_CODE))
2152 /* We can not swap with an instruction that requires a
2153 delay slot, becase the target of the branch might
2154 interfere with that instruction. */
2155 || (! mips_opts.mips16
9ce8a5dd 2156 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132 2157 && (prev_pinfo
bdaaa2e1 2158 /* Itbl support may require additional care here. */
252b5132
RH
2159 & (INSN_LOAD_COPROC_DELAY
2160 | INSN_COPROC_MOVE_DELAY
2161 | INSN_WRITE_COND_CODE)))
2162 || (! (hilo_interlocks
ec68c924 2163 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
252b5132
RH
2164 && (prev_pinfo
2165 & (INSN_READ_LO
2166 | INSN_READ_HI)))
2167 || (! mips_opts.mips16
2168 && ! gpr_interlocks
2169 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))
2170 || (! mips_opts.mips16
e7af610e 2171 && mips_opts.isa == ISA_MIPS1
bdaaa2e1 2172 /* Itbl support may require additional care here. */
252b5132
RH
2173 && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))
2174 /* We can not swap with a branch instruction. */
2175 || (prev_pinfo
2176 & (INSN_UNCOND_BRANCH_DELAY
2177 | INSN_COND_BRANCH_DELAY
2178 | INSN_COND_BRANCH_LIKELY))
2179 /* We do not swap with a trap instruction, since it
2180 complicates trap handlers to have the trap
2181 instruction be in a delay slot. */
2182 || (prev_pinfo & INSN_TRAP)
2183 /* If the branch reads a register that the previous
2184 instruction sets, we can not swap. */
2185 || (! mips_opts.mips16
2186 && (prev_pinfo & INSN_WRITE_GPR_T)
2187 && insn_uses_reg (ip,
2188 ((prev_insn.insn_opcode >> OP_SH_RT)
2189 & OP_MASK_RT),
2190 MIPS_GR_REG))
2191 || (! mips_opts.mips16
2192 && (prev_pinfo & INSN_WRITE_GPR_D)
2193 && insn_uses_reg (ip,
2194 ((prev_insn.insn_opcode >> OP_SH_RD)
2195 & OP_MASK_RD),
2196 MIPS_GR_REG))
2197 || (mips_opts.mips16
2198 && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2199 && insn_uses_reg (ip,
2200 ((prev_insn.insn_opcode
2201 >> MIPS16OP_SH_RX)
2202 & MIPS16OP_MASK_RX),
2203 MIPS16_REG))
2204 || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2205 && insn_uses_reg (ip,
2206 ((prev_insn.insn_opcode
2207 >> MIPS16OP_SH_RY)
2208 & MIPS16OP_MASK_RY),
2209 MIPS16_REG))
2210 || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2211 && insn_uses_reg (ip,
2212 ((prev_insn.insn_opcode
2213 >> MIPS16OP_SH_RZ)
2214 & MIPS16OP_MASK_RZ),
2215 MIPS16_REG))
2216 || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2217 && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2218 || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2219 && insn_uses_reg (ip, RA, MIPS_GR_REG))
2220 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2221 && insn_uses_reg (ip,
2222 MIPS16OP_EXTRACT_REG32R (prev_insn.
2223 insn_opcode),
2224 MIPS_GR_REG))))
2225 /* If the branch writes a register that the previous
2226 instruction sets, we can not swap (we know that
2227 branches write only to RD or to $31). */
2228 || (! mips_opts.mips16
2229 && (prev_pinfo & INSN_WRITE_GPR_T)
2230 && (((pinfo & INSN_WRITE_GPR_D)
2231 && (((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT)
2232 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2233 || ((pinfo & INSN_WRITE_GPR_31)
2234 && (((prev_insn.insn_opcode >> OP_SH_RT)
2235 & OP_MASK_RT)
f9419b05 2236 == RA))))
252b5132
RH
2237 || (! mips_opts.mips16
2238 && (prev_pinfo & INSN_WRITE_GPR_D)
2239 && (((pinfo & INSN_WRITE_GPR_D)
2240 && (((prev_insn.insn_opcode >> OP_SH_RD) & OP_MASK_RD)
2241 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2242 || ((pinfo & INSN_WRITE_GPR_31)
2243 && (((prev_insn.insn_opcode >> OP_SH_RD)
2244 & OP_MASK_RD)
f9419b05 2245 == RA))))
252b5132
RH
2246 || (mips_opts.mips16
2247 && (pinfo & MIPS16_INSN_WRITE_31)
2248 && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2249 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2250 && (MIPS16OP_EXTRACT_REG32R (prev_insn.insn_opcode)
2251 == RA))))
2252 /* If the branch writes a register that the previous
2253 instruction reads, we can not swap (we know that
2254 branches only write to RD or to $31). */
2255 || (! mips_opts.mips16
2256 && (pinfo & INSN_WRITE_GPR_D)
2257 && insn_uses_reg (&prev_insn,
2258 ((ip->insn_opcode >> OP_SH_RD)
2259 & OP_MASK_RD),
2260 MIPS_GR_REG))
2261 || (! mips_opts.mips16
2262 && (pinfo & INSN_WRITE_GPR_31)
f9419b05 2263 && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
252b5132
RH
2264 || (mips_opts.mips16
2265 && (pinfo & MIPS16_INSN_WRITE_31)
2266 && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2267 /* If we are generating embedded PIC code, the branch
2268 might be expanded into a sequence which uses $at, so
2269 we can't swap with an instruction which reads it. */
2270 || (mips_pic == EMBEDDED_PIC
2271 && insn_uses_reg (&prev_insn, AT, MIPS_GR_REG))
2272 /* If the previous previous instruction has a load
2273 delay, and sets a register that the branch reads, we
2274 can not swap. */
2275 || (! mips_opts.mips16
9ce8a5dd 2276 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
bdaaa2e1 2277 /* Itbl support may require additional care here. */
252b5132
RH
2278 && ((prev_prev_insn.insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
2279 || (! gpr_interlocks
2280 && (prev_prev_insn.insn_mo->pinfo
2281 & INSN_LOAD_MEMORY_DELAY)))
2282 && insn_uses_reg (ip,
2283 ((prev_prev_insn.insn_opcode >> OP_SH_RT)
2284 & OP_MASK_RT),
2285 MIPS_GR_REG))
2286 /* If one instruction sets a condition code and the
2287 other one uses a condition code, we can not swap. */
2288 || ((pinfo & INSN_READ_COND_CODE)
2289 && (prev_pinfo & INSN_WRITE_COND_CODE))
2290 || ((pinfo & INSN_WRITE_COND_CODE)
2291 && (prev_pinfo & INSN_READ_COND_CODE))
2292 /* If the previous instruction uses the PC, we can not
2293 swap. */
2294 || (mips_opts.mips16
2295 && (prev_pinfo & MIPS16_INSN_READ_PC))
2296 /* If the previous instruction was extended, we can not
2297 swap. */
2298 || (mips_opts.mips16 && prev_insn_extended)
2299 /* If the previous instruction had a fixup in mips16
2300 mode, we can not swap. This normally means that the
2301 previous instruction was a 4 byte branch anyhow. */
f6688943 2302 || (mips_opts.mips16 && prev_insn_fixp[0])
bdaaa2e1
KH
2303 /* If the previous instruction is a sync, sync.l, or
2304 sync.p, we can not swap. */
f173e82e 2305 || (prev_pinfo & INSN_SYNC))
252b5132
RH
2306 {
2307 /* We could do even better for unconditional branches to
2308 portions of this object file; we could pick up the
2309 instruction at the destination, put it in the delay
2310 slot, and bump the destination address. */
2311 emit_nop ();
2312 /* Update the previous insn information. */
2313 prev_prev_insn = *ip;
2314 prev_insn.insn_mo = &dummy_opcode;
2315 }
2316 else
2317 {
2318 /* It looks like we can actually do the swap. */
2319 if (! mips_opts.mips16)
2320 {
2321 char *prev_f;
2322 char temp[4];
2323
2324 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2325 memcpy (temp, prev_f, 4);
2326 memcpy (prev_f, f, 4);
2327 memcpy (f, temp, 4);
f6688943
TS
2328 if (prev_insn_fixp[0])
2329 {
2330 prev_insn_fixp[0]->fx_frag = frag_now;
2331 prev_insn_fixp[0]->fx_where = f - frag_now->fr_literal;
2332 }
2333 if (prev_insn_fixp[1])
2334 {
2335 prev_insn_fixp[1]->fx_frag = frag_now;
2336 prev_insn_fixp[1]->fx_where = f - frag_now->fr_literal;
2337 }
2338 if (prev_insn_fixp[2])
252b5132 2339 {
f6688943
TS
2340 prev_insn_fixp[2]->fx_frag = frag_now;
2341 prev_insn_fixp[2]->fx_where = f - frag_now->fr_literal;
252b5132 2342 }
f6688943 2343 if (fixp[0])
252b5132 2344 {
f6688943
TS
2345 fixp[0]->fx_frag = prev_insn_frag;
2346 fixp[0]->fx_where = prev_insn_where;
2347 }
2348 if (fixp[1])
2349 {
2350 fixp[1]->fx_frag = prev_insn_frag;
2351 fixp[1]->fx_where = prev_insn_where;
2352 }
2353 if (fixp[2])
2354 {
2355 fixp[2]->fx_frag = prev_insn_frag;
2356 fixp[2]->fx_where = prev_insn_where;
252b5132
RH
2357 }
2358 }
2359 else
2360 {
2361 char *prev_f;
2362 char temp[2];
2363
f6688943
TS
2364 assert (prev_insn_fixp[0] == NULL);
2365 assert (prev_insn_fixp[1] == NULL);
2366 assert (prev_insn_fixp[2] == NULL);
252b5132
RH
2367 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2368 memcpy (temp, prev_f, 2);
2369 memcpy (prev_f, f, 2);
f6688943 2370 if (*reloc_type != BFD_RELOC_MIPS16_JMP)
252b5132 2371 {
f6688943 2372 assert (*reloc_type == BFD_RELOC_UNUSED);
252b5132
RH
2373 memcpy (f, temp, 2);
2374 }
2375 else
2376 {
2377 memcpy (f, f + 2, 2);
2378 memcpy (f + 2, temp, 2);
2379 }
f6688943
TS
2380 if (fixp[0])
2381 {
2382 fixp[0]->fx_frag = prev_insn_frag;
2383 fixp[0]->fx_where = prev_insn_where;
2384 }
2385 if (fixp[1])
2386 {
2387 fixp[1]->fx_frag = prev_insn_frag;
2388 fixp[1]->fx_where = prev_insn_where;
2389 }
2390 if (fixp[2])
252b5132 2391 {
f6688943
TS
2392 fixp[2]->fx_frag = prev_insn_frag;
2393 fixp[2]->fx_where = prev_insn_where;
252b5132
RH
2394 }
2395 }
2396
2397 /* Update the previous insn information; leave prev_insn
2398 unchanged. */
2399 prev_prev_insn = *ip;
2400 }
2401 prev_insn_is_delay_slot = 1;
2402
2403 /* If that was an unconditional branch, forget the previous
2404 insn information. */
2405 if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2406 {
2407 prev_prev_insn.insn_mo = &dummy_opcode;
2408 prev_insn.insn_mo = &dummy_opcode;
2409 }
2410
f6688943
TS
2411 prev_insn_fixp[0] = NULL;
2412 prev_insn_fixp[1] = NULL;
2413 prev_insn_fixp[2] = NULL;
2414 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2415 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2416 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
252b5132
RH
2417 prev_insn_extended = 0;
2418 }
2419 else if (pinfo & INSN_COND_BRANCH_LIKELY)
2420 {
2421 /* We don't yet optimize a branch likely. What we should do
2422 is look at the target, copy the instruction found there
2423 into the delay slot, and increment the branch to jump to
2424 the next instruction. */
2425 emit_nop ();
2426 /* Update the previous insn information. */
2427 prev_prev_insn = *ip;
2428 prev_insn.insn_mo = &dummy_opcode;
f6688943
TS
2429 prev_insn_fixp[0] = NULL;
2430 prev_insn_fixp[1] = NULL;
2431 prev_insn_fixp[2] = NULL;
2432 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2433 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2434 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
252b5132
RH
2435 prev_insn_extended = 0;
2436 }
2437 else
2438 {
2439 /* Update the previous insn information. */
2440 if (nops > 0)
2441 prev_prev_insn.insn_mo = &dummy_opcode;
2442 else
2443 prev_prev_insn = prev_insn;
2444 prev_insn = *ip;
2445
2446 /* Any time we see a branch, we always fill the delay slot
2447 immediately; since this insn is not a branch, we know it
2448 is not in a delay slot. */
2449 prev_insn_is_delay_slot = 0;
2450
f6688943
TS
2451 prev_insn_fixp[0] = fixp[0];
2452 prev_insn_fixp[1] = fixp[1];
2453 prev_insn_fixp[2] = fixp[2];
2454 prev_insn_reloc_type[0] = reloc_type[0];
2455 prev_insn_reloc_type[1] = reloc_type[1];
2456 prev_insn_reloc_type[2] = reloc_type[2];
252b5132
RH
2457 if (mips_opts.mips16)
2458 prev_insn_extended = (ip->use_extend
f6688943 2459 || *reloc_type > BFD_RELOC_UNUSED);
252b5132
RH
2460 }
2461
2462 prev_prev_insn_unreordered = prev_insn_unreordered;
2463 prev_insn_unreordered = 0;
2464 prev_insn_frag = frag_now;
2465 prev_insn_where = f - frag_now->fr_literal;
2466 prev_insn_valid = 1;
2467 }
2468 else if (place == NULL)
2469 {
2470 /* We need to record a bit of information even when we are not
2471 reordering, in order to determine the base address for mips16
2472 PC relative relocs. */
2473 prev_prev_insn = prev_insn;
2474 prev_insn = *ip;
f6688943
TS
2475 prev_insn_reloc_type[0] = reloc_type[0];
2476 prev_insn_reloc_type[1] = reloc_type[1];
2477 prev_insn_reloc_type[2] = reloc_type[2];
252b5132
RH
2478 prev_prev_insn_unreordered = prev_insn_unreordered;
2479 prev_insn_unreordered = 1;
2480 }
2481
2482 /* We just output an insn, so the next one doesn't have a label. */
2483 mips_clear_insn_labels ();
2484
2485 /* We must ensure that a fixup associated with an unmatched %hi
2486 reloc does not become a variant frag. Otherwise, the
2487 rearrangement of %hi relocs in frob_file may confuse
2488 tc_gen_reloc. */
2489 if (unmatched_hi)
2490 {
2491 frag_wane (frag_now);
2492 frag_new (0);
2493 }
2494}
2495
2496/* This function forgets that there was any previous instruction or
2497 label. If PRESERVE is non-zero, it remembers enough information to
bdaaa2e1 2498 know whether nops are needed before a noreorder section. */
252b5132
RH
2499
2500static void
2501mips_no_prev_insn (preserve)
2502 int preserve;
2503{
2504 if (! preserve)
2505 {
2506 prev_insn.insn_mo = &dummy_opcode;
2507 prev_prev_insn.insn_mo = &dummy_opcode;
2508 prev_nop_frag = NULL;
2509 prev_nop_frag_holds = 0;
2510 prev_nop_frag_required = 0;
2511 prev_nop_frag_since = 0;
2512 }
2513 prev_insn_valid = 0;
2514 prev_insn_is_delay_slot = 0;
2515 prev_insn_unreordered = 0;
2516 prev_insn_extended = 0;
f6688943
TS
2517 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2518 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2519 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
252b5132
RH
2520 prev_prev_insn_unreordered = 0;
2521 mips_clear_insn_labels ();
2522}
2523
2524/* This function must be called whenever we turn on noreorder or emit
2525 something other than instructions. It inserts any NOPS which might
2526 be needed by the previous instruction, and clears the information
2527 kept for the previous instructions. The INSNS parameter is true if
bdaaa2e1 2528 instructions are to follow. */
252b5132
RH
2529
2530static void
2531mips_emit_delays (insns)
2532 boolean insns;
2533{
2534 if (! mips_opts.noreorder)
2535 {
2536 int nops;
2537
2538 nops = 0;
2539 if ((! mips_opts.mips16
9ce8a5dd 2540 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
2541 && (! cop_interlocks
2542 && (prev_insn.insn_mo->pinfo
2543 & (INSN_LOAD_COPROC_DELAY
2544 | INSN_COPROC_MOVE_DELAY
2545 | INSN_WRITE_COND_CODE))))
2546 || (! hilo_interlocks
2547 && (prev_insn.insn_mo->pinfo
2548 & (INSN_READ_LO
2549 | INSN_READ_HI)))
2550 || (! mips_opts.mips16
2551 && ! gpr_interlocks
bdaaa2e1 2552 && (prev_insn.insn_mo->pinfo
252b5132
RH
2553 & INSN_LOAD_MEMORY_DELAY))
2554 || (! mips_opts.mips16
e7af610e 2555 && mips_opts.isa == ISA_MIPS1
252b5132
RH
2556 && (prev_insn.insn_mo->pinfo
2557 & INSN_COPROC_MEMORY_DELAY)))
2558 {
beae10d5 2559 /* Itbl support may require additional care here. */
252b5132
RH
2560 ++nops;
2561 if ((! mips_opts.mips16
9ce8a5dd 2562 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
2563 && (! cop_interlocks
2564 && prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2565 || (! hilo_interlocks
2566 && ((prev_insn.insn_mo->pinfo & INSN_READ_HI)
2567 || (prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2568 ++nops;
2569
2570 if (prev_insn_unreordered)
2571 nops = 0;
2572 }
2573 else if ((! mips_opts.mips16
9ce8a5dd 2574 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
2575 && (! cop_interlocks
2576 && prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2577 || (! hilo_interlocks
2578 && ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
2579 || (prev_prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2580 {
beae10d5 2581 /* Itbl support may require additional care here. */
252b5132
RH
2582 if (! prev_prev_insn_unreordered)
2583 ++nops;
2584 }
2585
2586 if (nops > 0)
2587 {
2588 struct insn_label_list *l;
2589
2590 if (insns)
2591 {
2592 /* Record the frag which holds the nop instructions, so
2593 that we can remove them if we don't need them. */
2594 frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2595 prev_nop_frag = frag_now;
2596 prev_nop_frag_holds = nops;
2597 prev_nop_frag_required = 0;
2598 prev_nop_frag_since = 0;
2599 }
2600
2601 for (; nops > 0; --nops)
2602 emit_nop ();
2603
2604 if (insns)
2605 {
2606 /* Move on to a new frag, so that it is safe to simply
bdaaa2e1 2607 decrease the size of prev_nop_frag. */
252b5132
RH
2608 frag_wane (frag_now);
2609 frag_new (0);
2610 }
2611
2612 for (l = insn_labels; l != NULL; l = l->next)
2613 {
98aa84af
AM
2614 valueT val;
2615
252b5132 2616 assert (S_GET_SEGMENT (l->label) == now_seg);
49309057 2617 symbol_set_frag (l->label, frag_now);
98aa84af 2618 val = (valueT) frag_now_fix ();
252b5132
RH
2619 /* mips16 text labels are stored as odd. */
2620 if (mips_opts.mips16)
f9419b05 2621 ++val;
98aa84af 2622 S_SET_VALUE (l->label, val);
252b5132
RH
2623 }
2624 }
2625 }
2626
2627 /* Mark instruction labels in mips16 mode. */
f9419b05 2628 if (insns)
252b5132
RH
2629 mips16_mark_labels ();
2630
2631 mips_no_prev_insn (insns);
2632}
2633
2634/* Build an instruction created by a macro expansion. This is passed
2635 a pointer to the count of instructions created so far, an
2636 expression, the name of the instruction to build, an operand format
2637 string, and corresponding arguments. */
2638
2639#ifdef USE_STDARG
2640static void
2641macro_build (char *place,
2642 int *counter,
2643 expressionS * ep,
2644 const char *name,
2645 const char *fmt,
2646 ...)
2647#else
2648static void
2649macro_build (place, counter, ep, name, fmt, va_alist)
2650 char *place;
2651 int *counter;
2652 expressionS *ep;
2653 const char *name;
2654 const char *fmt;
2655 va_dcl
2656#endif
2657{
2658 struct mips_cl_insn insn;
f6688943 2659 bfd_reloc_code_real_type r[3];
252b5132 2660 va_list args;
252b5132
RH
2661
2662#ifdef USE_STDARG
2663 va_start (args, fmt);
2664#else
2665 va_start (args);
2666#endif
2667
2668 /*
2669 * If the macro is about to expand into a second instruction,
2670 * print a warning if needed. We need to pass ip as a parameter
2671 * to generate a better warning message here...
2672 */
2673 if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
2674 as_warn (_("Macro instruction expanded into multiple instructions"));
2675
80cc45a5
EC
2676 /*
2677 * If the macro is about to expand into a second instruction,
2678 * and it is in a delay slot, print a warning.
2679 */
2680 if (place == NULL
2681 && *counter == 1
2682 && mips_opts.noreorder
2683 && (prev_prev_insn.insn_mo->pinfo
2684 & (INSN_UNCOND_BRANCH_DELAY | INSN_COND_BRANCH_DELAY
2b2e39bf 2685 | INSN_COND_BRANCH_LIKELY)) != 0)
80cc45a5
EC
2686 as_warn (_("Macro instruction expanded into multiple instructions in a branch delay slot"));
2687
252b5132 2688 if (place == NULL)
f9419b05 2689 ++*counter; /* bump instruction counter */
252b5132
RH
2690
2691 if (mips_opts.mips16)
2692 {
2693 mips16_macro_build (place, counter, ep, name, fmt, args);
2694 va_end (args);
2695 return;
2696 }
2697
f6688943
TS
2698 r[0] = BFD_RELOC_UNUSED;
2699 r[1] = BFD_RELOC_UNUSED;
2700 r[2] = BFD_RELOC_UNUSED;
252b5132
RH
2701 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
2702 assert (insn.insn_mo);
2703 assert (strcmp (name, insn.insn_mo->name) == 0);
2704
2705 /* Search until we get a match for NAME. */
2706 while (1)
2707 {
deec1734
CD
2708 /* It is assumed here that macros will never generate
2709 MDMX or MIPS-3D instructions. */
252b5132
RH
2710 if (strcmp (fmt, insn.insn_mo->args) == 0
2711 && insn.insn_mo->pinfo != INSN_MACRO
aec421e0
TS
2712 && OPCODE_IS_MEMBER (insn.insn_mo,
2713 (mips_opts.isa
2714 | (mips_opts.mips16 ? INSN_MIPS16 : 0)),
2715 mips_arch)
ec68c924 2716 && (mips_arch != CPU_R4650 || (insn.insn_mo->pinfo & FP_D) == 0))
252b5132
RH
2717 break;
2718
2719 ++insn.insn_mo;
2720 assert (insn.insn_mo->name);
2721 assert (strcmp (name, insn.insn_mo->name) == 0);
2722 }
2723
2724 insn.insn_opcode = insn.insn_mo->match;
2725 for (;;)
2726 {
2727 switch (*fmt++)
2728 {
2729 case '\0':
2730 break;
2731
2732 case ',':
2733 case '(':
2734 case ')':
2735 continue;
2736
2737 case 't':
2738 case 'w':
2739 case 'E':
38487616 2740 insn.insn_opcode |= va_arg (args, int) << OP_SH_RT;
252b5132
RH
2741 continue;
2742
2743 case 'c':
38487616
TS
2744 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE;
2745 continue;
2746
252b5132
RH
2747 case 'T':
2748 case 'W':
38487616 2749 insn.insn_opcode |= va_arg (args, int) << OP_SH_FT;
252b5132
RH
2750 continue;
2751
2752 case 'd':
2753 case 'G':
38487616 2754 insn.insn_opcode |= va_arg (args, int) << OP_SH_RD;
252b5132
RH
2755 continue;
2756
4372b673
NC
2757 case 'U':
2758 {
2759 int tmp = va_arg (args, int);
2760
38487616
TS
2761 insn.insn_opcode |= tmp << OP_SH_RT;
2762 insn.insn_opcode |= tmp << OP_SH_RD;
beae10d5 2763 continue;
4372b673
NC
2764 }
2765
252b5132
RH
2766 case 'V':
2767 case 'S':
38487616 2768 insn.insn_opcode |= va_arg (args, int) << OP_SH_FS;
252b5132
RH
2769 continue;
2770
2771 case 'z':
2772 continue;
2773
2774 case '<':
38487616 2775 insn.insn_opcode |= va_arg (args, int) << OP_SH_SHAMT;
252b5132
RH
2776 continue;
2777
2778 case 'D':
38487616 2779 insn.insn_opcode |= va_arg (args, int) << OP_SH_FD;
252b5132
RH
2780 continue;
2781
2782 case 'B':
38487616 2783 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE20;
252b5132
RH
2784 continue;
2785
4372b673 2786 case 'J':
38487616 2787 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE19;
4372b673
NC
2788 continue;
2789
252b5132 2790 case 'q':
38487616 2791 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE2;
252b5132
RH
2792 continue;
2793
2794 case 'b':
2795 case 's':
2796 case 'r':
2797 case 'v':
38487616 2798 insn.insn_opcode |= va_arg (args, int) << OP_SH_RS;
252b5132
RH
2799 continue;
2800
2801 case 'i':
2802 case 'j':
2803 case 'o':
f6688943 2804 *r = (bfd_reloc_code_real_type) va_arg (args, int);
cdf6fd85 2805 assert (*r == BFD_RELOC_GPREL16
f6688943
TS
2806 || *r == BFD_RELOC_MIPS_LITERAL
2807 || *r == BFD_RELOC_MIPS_HIGHER
2808 || *r == BFD_RELOC_HI16_S
2809 || *r == BFD_RELOC_LO16
2810 || *r == BFD_RELOC_MIPS_GOT16
2811 || *r == BFD_RELOC_MIPS_CALL16
438c16b8
TS
2812 || *r == BFD_RELOC_MIPS_GOT_DISP
2813 || *r == BFD_RELOC_MIPS_GOT_PAGE
2814 || *r == BFD_RELOC_MIPS_GOT_OFST
f6688943
TS
2815 || *r == BFD_RELOC_MIPS_GOT_LO16
2816 || *r == BFD_RELOC_MIPS_CALL_LO16
252b5132 2817 || (ep->X_op == O_subtract
f6688943 2818 && *r == BFD_RELOC_PCREL_LO16));
252b5132
RH
2819 continue;
2820
2821 case 'u':
f6688943 2822 *r = (bfd_reloc_code_real_type) va_arg (args, int);
252b5132
RH
2823 assert (ep != NULL
2824 && (ep->X_op == O_constant
2825 || (ep->X_op == O_symbol
f6688943
TS
2826 && (*r == BFD_RELOC_MIPS_HIGHEST
2827 || *r == BFD_RELOC_HI16_S
2828 || *r == BFD_RELOC_HI16
2829 || *r == BFD_RELOC_GPREL16
2830 || *r == BFD_RELOC_MIPS_GOT_HI16
2831 || *r == BFD_RELOC_MIPS_CALL_HI16))
252b5132 2832 || (ep->X_op == O_subtract
f6688943 2833 && *r == BFD_RELOC_PCREL_HI16_S)));
252b5132
RH
2834 continue;
2835
2836 case 'p':
2837 assert (ep != NULL);
2838 /*
2839 * This allows macro() to pass an immediate expression for
2840 * creating short branches without creating a symbol.
2841 * Note that the expression still might come from the assembly
2842 * input, in which case the value is not checked for range nor
2843 * is a relocation entry generated (yuck).
2844 */
2845 if (ep->X_op == O_constant)
2846 {
2847 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
2848 ep = NULL;
2849 }
2850 else
cb56d3d3 2851 if (mips_pic == EMBEDDED_PIC)
f6688943 2852 *r = BFD_RELOC_16_PCREL_S2;
cb56d3d3 2853 else
f6688943 2854 *r = BFD_RELOC_16_PCREL;
252b5132
RH
2855 continue;
2856
2857 case 'a':
2858 assert (ep != NULL);
f6688943 2859 *r = BFD_RELOC_MIPS_JMP;
252b5132
RH
2860 continue;
2861
2862 case 'C':
2863 insn.insn_opcode |= va_arg (args, unsigned long);
2864 continue;
2865
2866 default:
2867 internalError ();
2868 }
2869 break;
2870 }
2871 va_end (args);
f6688943 2872 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132
RH
2873
2874 append_insn (place, &insn, ep, r, false);
2875}
2876
2877static void
2878mips16_macro_build (place, counter, ep, name, fmt, args)
2879 char *place;
43841e91 2880 int *counter ATTRIBUTE_UNUSED;
252b5132
RH
2881 expressionS *ep;
2882 const char *name;
2883 const char *fmt;
2884 va_list args;
2885{
2886 struct mips_cl_insn insn;
f6688943
TS
2887 bfd_reloc_code_real_type r[3]
2888 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 2889
252b5132
RH
2890 insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
2891 assert (insn.insn_mo);
2892 assert (strcmp (name, insn.insn_mo->name) == 0);
2893
2894 while (strcmp (fmt, insn.insn_mo->args) != 0
2895 || insn.insn_mo->pinfo == INSN_MACRO)
2896 {
2897 ++insn.insn_mo;
2898 assert (insn.insn_mo->name);
2899 assert (strcmp (name, insn.insn_mo->name) == 0);
2900 }
2901
2902 insn.insn_opcode = insn.insn_mo->match;
2903 insn.use_extend = false;
2904
2905 for (;;)
2906 {
2907 int c;
2908
2909 c = *fmt++;
2910 switch (c)
2911 {
2912 case '\0':
2913 break;
2914
2915 case ',':
2916 case '(':
2917 case ')':
2918 continue;
2919
2920 case 'y':
2921 case 'w':
2922 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
2923 continue;
2924
2925 case 'x':
2926 case 'v':
2927 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
2928 continue;
2929
2930 case 'z':
2931 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
2932 continue;
2933
2934 case 'Z':
2935 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
2936 continue;
2937
2938 case '0':
2939 case 'S':
2940 case 'P':
2941 case 'R':
2942 continue;
2943
2944 case 'X':
2945 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
2946 continue;
2947
2948 case 'Y':
2949 {
2950 int regno;
2951
2952 regno = va_arg (args, int);
2953 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
2954 insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
2955 }
2956 continue;
2957
2958 case '<':
2959 case '>':
2960 case '4':
2961 case '5':
2962 case 'H':
2963 case 'W':
2964 case 'D':
2965 case 'j':
2966 case '8':
2967 case 'V':
2968 case 'C':
2969 case 'U':
2970 case 'k':
2971 case 'K':
2972 case 'p':
2973 case 'q':
2974 {
2975 assert (ep != NULL);
2976
2977 if (ep->X_op != O_constant)
874e8986 2978 *r = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
2979 else
2980 {
c4e7957c
TS
2981 mips16_immed (NULL, 0, c, ep->X_add_number, false, false,
2982 false, &insn.insn_opcode, &insn.use_extend,
2983 &insn.extend);
252b5132 2984 ep = NULL;
f6688943 2985 *r = BFD_RELOC_UNUSED;
252b5132
RH
2986 }
2987 }
2988 continue;
2989
2990 case '6':
2991 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
2992 continue;
2993 }
2994
2995 break;
2996 }
2997
f6688943 2998 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132
RH
2999
3000 append_insn (place, &insn, ep, r, false);
3001}
3002
438c16b8
TS
3003/*
3004 * Generate a "jalr" instruction with a relocation hint to the called
3005 * function. This occurs in NewABI PIC code.
3006 */
3007static void
3008macro_build_jalr (icnt, ep)
3009 int icnt;
3010 expressionS *ep;
3011{
f21f8242
AO
3012 char *f;
3013
438c16b8 3014 if (HAVE_NEWABI)
f21f8242
AO
3015 {
3016 frag_grow (4);
3017 f = frag_more (0);
3018 }
438c16b8
TS
3019 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr", "d,s",
3020 RA, PIC_CALL_REG);
3021 if (HAVE_NEWABI)
f21f8242
AO
3022 fix_new_exp (frag_now, f - frag_now->fr_literal,
3023 0, ep, false, BFD_RELOC_MIPS_JALR);
438c16b8
TS
3024}
3025
252b5132
RH
3026/*
3027 * Generate a "lui" instruction.
3028 */
3029static void
3030macro_build_lui (place, counter, ep, regnum)
3031 char *place;
3032 int *counter;
3033 expressionS *ep;
3034 int regnum;
3035{
3036 expressionS high_expr;
3037 struct mips_cl_insn insn;
f6688943
TS
3038 bfd_reloc_code_real_type r[3]
3039 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
5a38dc70
AM
3040 const char *name = "lui";
3041 const char *fmt = "t,u";
252b5132
RH
3042
3043 assert (! mips_opts.mips16);
3044
3045 if (place == NULL)
3046 high_expr = *ep;
3047 else
3048 {
3049 high_expr.X_op = O_constant;
3050 high_expr.X_add_number = ep->X_add_number;
3051 }
3052
3053 if (high_expr.X_op == O_constant)
3054 {
3055 /* we can compute the instruction now without a relocation entry */
e7d556df
TS
3056 high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3057 >> 16) & 0xffff;
f6688943 3058 *r = BFD_RELOC_UNUSED;
252b5132 3059 }
f6688943 3060 else if (! HAVE_NEWABI)
252b5132
RH
3061 {
3062 assert (ep->X_op == O_symbol);
3063 /* _gp_disp is a special case, used from s_cpload. */
3064 assert (mips_pic == NO_PIC
3065 || strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0);
f6688943 3066 *r = BFD_RELOC_HI16_S;
252b5132
RH
3067 }
3068
3069 /*
3070 * If the macro is about to expand into a second instruction,
3071 * print a warning if needed. We need to pass ip as a parameter
3072 * to generate a better warning message here...
3073 */
3074 if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
3075 as_warn (_("Macro instruction expanded into multiple instructions"));
3076
3077 if (place == NULL)
f9419b05 3078 ++*counter; /* bump instruction counter */
252b5132
RH
3079
3080 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3081 assert (insn.insn_mo);
3082 assert (strcmp (name, insn.insn_mo->name) == 0);
3083 assert (strcmp (fmt, insn.insn_mo->args) == 0);
3084
3085 insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
f6688943 3086 if (*r == BFD_RELOC_UNUSED)
252b5132
RH
3087 {
3088 insn.insn_opcode |= high_expr.X_add_number;
3089 append_insn (place, &insn, NULL, r, false);
3090 }
3091 else
3092 append_insn (place, &insn, &high_expr, r, false);
3093}
3094
3095/* set_at()
3096 * Generates code to set the $at register to true (one)
3097 * if reg is less than the immediate expression.
3098 */
3099static void
3100set_at (counter, reg, unsignedp)
3101 int *counter;
3102 int reg;
3103 int unsignedp;
3104{
3105 if (imm_expr.X_op == O_constant
3106 && imm_expr.X_add_number >= -0x8000
3107 && imm_expr.X_add_number < 0x8000)
3108 macro_build ((char *) NULL, counter, &imm_expr,
3109 unsignedp ? "sltiu" : "slti",
3110 "t,r,j", AT, reg, (int) BFD_RELOC_LO16);
3111 else
3112 {
4d34fb5f 3113 load_register (counter, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9 3114 macro_build ((char *) NULL, counter, (expressionS *) NULL,
252b5132
RH
3115 unsignedp ? "sltu" : "slt",
3116 "d,v,t", AT, reg, AT);
3117 }
3118}
3119
3120/* Warn if an expression is not a constant. */
3121
3122static void
3123check_absolute_expr (ip, ex)
3124 struct mips_cl_insn *ip;
3125 expressionS *ex;
3126{
3127 if (ex->X_op == O_big)
3128 as_bad (_("unsupported large constant"));
3129 else if (ex->X_op != O_constant)
3130 as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
3131}
3132
3133/* Count the leading zeroes by performing a binary chop. This is a
3134 bulky bit of source, but performance is a LOT better for the
3135 majority of values than a simple loop to count the bits:
3136 for (lcnt = 0; (lcnt < 32); lcnt++)
3137 if ((v) & (1 << (31 - lcnt)))
3138 break;
3139 However it is not code size friendly, and the gain will drop a bit
3140 on certain cached systems.
3141*/
3142#define COUNT_TOP_ZEROES(v) \
3143 (((v) & ~0xffff) == 0 \
3144 ? ((v) & ~0xff) == 0 \
3145 ? ((v) & ~0xf) == 0 \
3146 ? ((v) & ~0x3) == 0 \
3147 ? ((v) & ~0x1) == 0 \
3148 ? !(v) \
3149 ? 32 \
3150 : 31 \
3151 : 30 \
3152 : ((v) & ~0x7) == 0 \
3153 ? 29 \
3154 : 28 \
3155 : ((v) & ~0x3f) == 0 \
3156 ? ((v) & ~0x1f) == 0 \
3157 ? 27 \
3158 : 26 \
3159 : ((v) & ~0x7f) == 0 \
3160 ? 25 \
3161 : 24 \
3162 : ((v) & ~0xfff) == 0 \
3163 ? ((v) & ~0x3ff) == 0 \
3164 ? ((v) & ~0x1ff) == 0 \
3165 ? 23 \
3166 : 22 \
3167 : ((v) & ~0x7ff) == 0 \
3168 ? 21 \
3169 : 20 \
3170 : ((v) & ~0x3fff) == 0 \
3171 ? ((v) & ~0x1fff) == 0 \
3172 ? 19 \
3173 : 18 \
3174 : ((v) & ~0x7fff) == 0 \
3175 ? 17 \
3176 : 16 \
3177 : ((v) & ~0xffffff) == 0 \
3178 ? ((v) & ~0xfffff) == 0 \
3179 ? ((v) & ~0x3ffff) == 0 \
3180 ? ((v) & ~0x1ffff) == 0 \
3181 ? 15 \
3182 : 14 \
3183 : ((v) & ~0x7ffff) == 0 \
3184 ? 13 \
3185 : 12 \
3186 : ((v) & ~0x3fffff) == 0 \
3187 ? ((v) & ~0x1fffff) == 0 \
3188 ? 11 \
3189 : 10 \
3190 : ((v) & ~0x7fffff) == 0 \
3191 ? 9 \
3192 : 8 \
3193 : ((v) & ~0xfffffff) == 0 \
3194 ? ((v) & ~0x3ffffff) == 0 \
3195 ? ((v) & ~0x1ffffff) == 0 \
3196 ? 7 \
3197 : 6 \
3198 : ((v) & ~0x7ffffff) == 0 \
3199 ? 5 \
3200 : 4 \
3201 : ((v) & ~0x3fffffff) == 0 \
3202 ? ((v) & ~0x1fffffff) == 0 \
3203 ? 3 \
3204 : 2 \
3205 : ((v) & ~0x7fffffff) == 0 \
3206 ? 1 \
3207 : 0)
3208
6373ee54
CD
3209/* Is the given value a sign-extended 32-bit value? */
3210#define IS_SEXT_32BIT_NUM(x) \
3211 (((x) &~ (offsetT) 0x7fffffff) == 0 \
3212 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
3213
252b5132
RH
3214/* load_register()
3215 * This routine generates the least number of instructions neccessary to load
3216 * an absolute expression value into a register.
3217 */
3218static void
3219load_register (counter, reg, ep, dbl)
3220 int *counter;
3221 int reg;
3222 expressionS *ep;
3223 int dbl;
3224{
3225 int freg;
3226 expressionS hi32, lo32;
3227
3228 if (ep->X_op != O_big)
3229 {
3230 assert (ep->X_op == O_constant);
3231 if (ep->X_add_number < 0x8000
3232 && (ep->X_add_number >= 0
3233 || (ep->X_add_number >= -0x8000
3234 && (! dbl
3235 || ! ep->X_unsigned
3236 || sizeof (ep->X_add_number) > 4))))
3237 {
3238 /* We can handle 16 bit signed values with an addiu to
3239 $zero. No need to ever use daddiu here, since $zero and
3240 the result are always correct in 32 bit mode. */
3241 macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3242 (int) BFD_RELOC_LO16);
3243 return;
3244 }
3245 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3246 {
3247 /* We can handle 16 bit unsigned values with an ori to
3248 $zero. */
3249 macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, 0,
3250 (int) BFD_RELOC_LO16);
3251 return;
3252 }
6373ee54 3253 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)
252b5132
RH
3254 && (! dbl
3255 || ! ep->X_unsigned
3256 || sizeof (ep->X_add_number) > 4
3257 || (ep->X_add_number & 0x80000000) == 0))
ca4e0257 3258 || ((HAVE_32BIT_GPRS || ! dbl)
252b5132 3259 && (ep->X_add_number &~ (offsetT) 0xffffffff) == 0)
ca4e0257 3260 || (HAVE_32BIT_GPRS
252b5132
RH
3261 && ! dbl
3262 && ((ep->X_add_number &~ (offsetT) 0xffffffff)
3263 == ~ (offsetT) 0xffffffff)))
3264 {
3265 /* 32 bit values require an lui. */
3266 macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3267 (int) BFD_RELOC_HI16);
3268 if ((ep->X_add_number & 0xffff) != 0)
3269 macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, reg,
3270 (int) BFD_RELOC_LO16);
3271 return;
3272 }
3273 }
3274
3275 /* The value is larger than 32 bits. */
3276
ca4e0257 3277 if (HAVE_32BIT_GPRS)
252b5132 3278 {
956cd1d6
TS
3279 as_bad (_("Number (0x%lx) larger than 32 bits"),
3280 (unsigned long) ep->X_add_number);
252b5132
RH
3281 macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3282 (int) BFD_RELOC_LO16);
3283 return;
3284 }
3285
3286 if (ep->X_op != O_big)
3287 {
3288 hi32 = *ep;
3289 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3290 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3291 hi32.X_add_number &= 0xffffffff;
3292 lo32 = *ep;
3293 lo32.X_add_number &= 0xffffffff;
3294 }
3295 else
3296 {
3297 assert (ep->X_add_number > 2);
3298 if (ep->X_add_number == 3)
3299 generic_bignum[3] = 0;
3300 else if (ep->X_add_number > 4)
3301 as_bad (_("Number larger than 64 bits"));
3302 lo32.X_op = O_constant;
3303 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3304 hi32.X_op = O_constant;
3305 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3306 }
3307
3308 if (hi32.X_add_number == 0)
3309 freg = 0;
3310 else
3311 {
3312 int shift, bit;
3313 unsigned long hi, lo;
3314
956cd1d6 3315 if (hi32.X_add_number == (offsetT) 0xffffffff)
beae10d5
KH
3316 {
3317 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3318 {
3319 macro_build ((char *) NULL, counter, &lo32, "addiu", "t,r,j",
252b5132 3320 reg, 0, (int) BFD_RELOC_LO16);
beae10d5
KH
3321 return;
3322 }
3323 if (lo32.X_add_number & 0x80000000)
3324 {
3325 macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3326 (int) BFD_RELOC_HI16);
252b5132
RH
3327 if (lo32.X_add_number & 0xffff)
3328 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i",
3329 reg, reg, (int) BFD_RELOC_LO16);
beae10d5
KH
3330 return;
3331 }
3332 }
252b5132
RH
3333
3334 /* Check for 16bit shifted constant. We know that hi32 is
3335 non-zero, so start the mask on the first bit of the hi32
3336 value. */
3337 shift = 17;
3338 do
beae10d5
KH
3339 {
3340 unsigned long himask, lomask;
3341
3342 if (shift < 32)
3343 {
3344 himask = 0xffff >> (32 - shift);
3345 lomask = (0xffff << shift) & 0xffffffff;
3346 }
3347 else
3348 {
3349 himask = 0xffff << (shift - 32);
3350 lomask = 0;
3351 }
3352 if ((hi32.X_add_number & ~(offsetT) himask) == 0
3353 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3354 {
3355 expressionS tmp;
3356
3357 tmp.X_op = O_constant;
3358 if (shift < 32)
3359 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3360 | (lo32.X_add_number >> shift));
3361 else
3362 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
3363 macro_build ((char *) NULL, counter, &tmp,
3364 "ori", "t,r,i", reg, 0,
3365 (int) BFD_RELOC_LO16);
2396cfb9 3366 macro_build ((char *) NULL, counter, (expressionS *) NULL,
beae10d5
KH
3367 (shift >= 32) ? "dsll32" : "dsll",
3368 "d,w,<", reg, reg,
3369 (shift >= 32) ? shift - 32 : shift);
3370 return;
3371 }
f9419b05 3372 ++shift;
beae10d5
KH
3373 }
3374 while (shift <= (64 - 16));
252b5132
RH
3375
3376 /* Find the bit number of the lowest one bit, and store the
3377 shifted value in hi/lo. */
3378 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3379 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3380 if (lo != 0)
3381 {
3382 bit = 0;
3383 while ((lo & 1) == 0)
3384 {
3385 lo >>= 1;
3386 ++bit;
3387 }
3388 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3389 hi >>= bit;
3390 }
3391 else
3392 {
3393 bit = 32;
3394 while ((hi & 1) == 0)
3395 {
3396 hi >>= 1;
3397 ++bit;
3398 }
3399 lo = hi;
3400 hi = 0;
3401 }
3402
3403 /* Optimize if the shifted value is a (power of 2) - 1. */
3404 if ((hi == 0 && ((lo + 1) & lo) == 0)
3405 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
beae10d5
KH
3406 {
3407 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
252b5132 3408 if (shift != 0)
beae10d5 3409 {
252b5132
RH
3410 expressionS tmp;
3411
3412 /* This instruction will set the register to be all
3413 ones. */
beae10d5
KH
3414 tmp.X_op = O_constant;
3415 tmp.X_add_number = (offsetT) -1;
3416 macro_build ((char *) NULL, counter, &tmp, "addiu", "t,r,j",
252b5132 3417 reg, 0, (int) BFD_RELOC_LO16);
beae10d5
KH
3418 if (bit != 0)
3419 {
3420 bit += shift;
2396cfb9 3421 macro_build ((char *) NULL, counter, (expressionS *) NULL,
beae10d5
KH
3422 (bit >= 32) ? "dsll32" : "dsll",
3423 "d,w,<", reg, reg,
3424 (bit >= 32) ? bit - 32 : bit);
3425 }
2396cfb9 3426 macro_build ((char *) NULL, counter, (expressionS *) NULL,
252b5132 3427 (shift >= 32) ? "dsrl32" : "dsrl",
beae10d5 3428 "d,w,<", reg, reg,
252b5132 3429 (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
3430 return;
3431 }
3432 }
252b5132
RH
3433
3434 /* Sign extend hi32 before calling load_register, because we can
3435 generally get better code when we load a sign extended value. */
3436 if ((hi32.X_add_number & 0x80000000) != 0)
beae10d5 3437 hi32.X_add_number |= ~(offsetT) 0xffffffff;
252b5132
RH
3438 load_register (counter, reg, &hi32, 0);
3439 freg = reg;
3440 }
3441 if ((lo32.X_add_number & 0xffff0000) == 0)
3442 {
3443 if (freg != 0)
3444 {
2396cfb9
TS
3445 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3446 "dsll32", "d,w,<", reg, freg, 0);
252b5132
RH
3447 freg = reg;
3448 }
3449 }
3450 else
3451 {
3452 expressionS mid16;
3453
956cd1d6 3454 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
beae10d5 3455 {
252b5132
RH
3456 macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3457 (int) BFD_RELOC_HI16);
956cd1d6
TS
3458 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3459 "dsrl32", "d,w,<", reg, reg, 0);
beae10d5
KH
3460 return;
3461 }
252b5132
RH
3462
3463 if (freg != 0)
3464 {
956cd1d6
TS
3465 macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3466 "d,w,<", reg, freg, 16);
252b5132
RH
3467 freg = reg;
3468 }
3469 mid16 = lo32;
3470 mid16.X_add_number >>= 16;
3471 macro_build ((char *) NULL, counter, &mid16, "ori", "t,r,i", reg,
3472 freg, (int) BFD_RELOC_LO16);
956cd1d6
TS
3473 macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3474 "d,w,<", reg, reg, 16);
252b5132
RH
3475 freg = reg;
3476 }
3477 if ((lo32.X_add_number & 0xffff) != 0)
3478 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i", reg, freg,
3479 (int) BFD_RELOC_LO16);
3480}
3481
3482/* Load an address into a register. */
3483
3484static void
c9914766 3485load_address (counter, reg, ep, used_at)
252b5132
RH
3486 int *counter;
3487 int reg;
3488 expressionS *ep;
d6bc6245 3489 int *used_at;
252b5132 3490{
f9419b05 3491 char *p = NULL;
252b5132
RH
3492
3493 if (ep->X_op != O_constant
3494 && ep->X_op != O_symbol)
3495 {
3496 as_bad (_("expression too complex"));
3497 ep->X_op = O_constant;
3498 }
3499
3500 if (ep->X_op == O_constant)
3501 {
c9914766 3502 load_register (counter, reg, ep, HAVE_64BIT_ADDRESSES);
252b5132
RH
3503 return;
3504 }
3505
3506 if (mips_pic == NO_PIC)
3507 {
3508 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 3509 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
3510 Otherwise we want
3511 lui $reg,<sym> (BFD_RELOC_HI16_S)
3512 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
d6bc6245 3513 If we have an addend, we always use the latter form.
76b3015f 3514
d6bc6245
TS
3515 With 64bit address space and a usable $at we want
3516 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3517 lui $at,<sym> (BFD_RELOC_HI16_S)
3518 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3519 daddiu $at,<sym> (BFD_RELOC_LO16)
3520 dsll32 $reg,0
3a482fd5 3521 daddu $reg,$reg,$at
76b3015f 3522
d6bc6245
TS
3523 If $at is already in use, we use an path which is suboptimal
3524 on superscalar processors.
3525 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3526 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3527 dsll $reg,16
3528 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
3529 dsll $reg,16
3530 daddiu $reg,<sym> (BFD_RELOC_LO16)
3531 */
c9914766 3532 if (HAVE_64BIT_ADDRESSES)
d6bc6245 3533 {
d6bc6245
TS
3534 /* We don't do GP optimization for now because RELAX_ENCODE can't
3535 hold the data for such large chunks. */
3536
460597ba 3537 if (*used_at == 0 && ! mips_opts.noat)
d6bc6245
TS
3538 {
3539 macro_build (p, counter, ep, "lui", "t,u",
3540 reg, (int) BFD_RELOC_MIPS_HIGHEST);
3541 macro_build (p, counter, ep, "lui", "t,u",
3542 AT, (int) BFD_RELOC_HI16_S);
3543 macro_build (p, counter, ep, "daddiu", "t,r,j",
3544 reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3545 macro_build (p, counter, ep, "daddiu", "t,r,j",
3546 AT, AT, (int) BFD_RELOC_LO16);
2396cfb9
TS
3547 macro_build (p, counter, (expressionS *) NULL, "dsll32",
3548 "d,w,<", reg, reg, 0);
3a482fd5 3549 macro_build (p, counter, (expressionS *) NULL, "daddu",
2396cfb9 3550 "d,v,t", reg, reg, AT);
d6bc6245
TS
3551 *used_at = 1;
3552 }
3553 else
3554 {
3555 macro_build (p, counter, ep, "lui", "t,u",
3556 reg, (int) BFD_RELOC_MIPS_HIGHEST);
3557 macro_build (p, counter, ep, "daddiu", "t,r,j",
3558 reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
2396cfb9
TS
3559 macro_build (p, counter, (expressionS *) NULL, "dsll",
3560 "d,w,<", reg, reg, 16);
d6bc6245
TS
3561 macro_build (p, counter, ep, "daddiu", "t,r,j",
3562 reg, reg, (int) BFD_RELOC_HI16_S);
2396cfb9
TS
3563 macro_build (p, counter, (expressionS *) NULL, "dsll",
3564 "d,w,<", reg, reg, 16);
d6bc6245
TS
3565 macro_build (p, counter, ep, "daddiu", "t,r,j",
3566 reg, reg, (int) BFD_RELOC_LO16);
3567 }
3568 }
252b5132
RH
3569 else
3570 {
d6bc6245
TS
3571 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3572 && ! nopic_need_relax (ep->X_add_symbol, 1))
3573 {
3574 frag_grow (20);
3575 macro_build ((char *) NULL, counter, ep,
c9914766
TS
3576 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
3577 reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
d6bc6245
TS
3578 p = frag_var (rs_machine_dependent, 8, 0,
3579 RELAX_ENCODE (4, 8, 0, 4, 0,
3580 mips_opts.warn_about_macros),
956cd1d6 3581 ep->X_add_symbol, 0, NULL);
d6bc6245
TS
3582 }
3583 macro_build_lui (p, counter, ep, reg);
3584 if (p != NULL)
3585 p += 4;
c9914766
TS
3586 macro_build (p, counter, ep,
3587 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
d6bc6245
TS
3588 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3589 }
252b5132
RH
3590 }
3591 else if (mips_pic == SVR4_PIC && ! mips_big_got)
3592 {
3593 expressionS ex;
3594
3595 /* If this is a reference to an external symbol, we want
3596 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3597 Otherwise we want
3598 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3599 nop
3600 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3601 If there is a constant, it must be added in after. */
3602 ex.X_add_number = ep->X_add_number;
3603 ep->X_add_number = 0;
3604 frag_grow (20);
3605 macro_build ((char *) NULL, counter, ep,
c9914766
TS
3606 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
3607 reg, (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132
RH
3608 macro_build ((char *) NULL, counter, (expressionS *) NULL, "nop", "");
3609 p = frag_var (rs_machine_dependent, 4, 0,
3610 RELAX_ENCODE (0, 4, -8, 0, 0, mips_opts.warn_about_macros),
3611 ep->X_add_symbol, (offsetT) 0, (char *) NULL);
3612 macro_build (p, counter, ep,
ca4e0257 3613 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
3614 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3615 if (ex.X_add_number != 0)
3616 {
3617 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3618 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3619 ex.X_op = O_constant;
3620 macro_build ((char *) NULL, counter, &ex,
ca4e0257 3621 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
3622 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3623 }
3624 }
3625 else if (mips_pic == SVR4_PIC)
3626 {
3627 expressionS ex;
3628 int off;
3629
3630 /* This is the large GOT case. If this is a reference to an
3631 external symbol, we want
3632 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
3633 addu $reg,$reg,$gp
3634 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
3635 Otherwise, for a reference to a local symbol, we want
3636 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3637 nop
3638 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
438c16b8
TS
3639 If we have NewABI, we want
3640 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
3641 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
252b5132
RH
3642 If there is a constant, it must be added in after. */
3643 ex.X_add_number = ep->X_add_number;
3644 ep->X_add_number = 0;
438c16b8
TS
3645 if (HAVE_NEWABI)
3646 {
3647 macro_build ((char *) NULL, counter, ep,
3648 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3649 (int) BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
3650 macro_build (p, counter, ep,
3651 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
3652 reg, reg, (int) BFD_RELOC_MIPS_GOT_OFST);
3653 }
252b5132 3654 else
438c16b8
TS
3655 {
3656 if (reg_needs_delay (mips_gp_register))
3657 off = 4;
3658 else
3659 off = 0;
3660 frag_grow (32);
3661 macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3662 (int) BFD_RELOC_MIPS_GOT_HI16);
3663 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3664 HAVE_32BIT_ADDRESSES ? "addu" : "daddu", "d,v,t", reg,
3665 reg, mips_gp_register);
3666 macro_build ((char *) NULL, counter, ep,
3667 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
3668 "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT_LO16, reg);
3669 p = frag_var (rs_machine_dependent, 12 + off, 0,
3670 RELAX_ENCODE (12, 12 + off, off, 8 + off, 0,
3671 mips_opts.warn_about_macros),
3672 ep->X_add_symbol, 0, NULL);
3673 if (off > 0)
3674 {
3675 /* We need a nop before loading from $gp. This special
3676 check is required because the lui which starts the main
3677 instruction stream does not refer to $gp, and so will not
3678 insert the nop which may be required. */
3679 macro_build (p, counter, (expressionS *) NULL, "nop", "");
3680 p += 4;
3681 }
3682 macro_build (p, counter, ep,
3683 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3684 (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
3685 p += 4;
252b5132
RH
3686 macro_build (p, counter, (expressionS *) NULL, "nop", "");
3687 p += 4;
438c16b8
TS
3688 macro_build (p, counter, ep,
3689 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3690 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
252b5132 3691 }
438c16b8 3692
252b5132
RH
3693 if (ex.X_add_number != 0)
3694 {
3695 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3696 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3697 ex.X_op = O_constant;
f7ea7ef2
TS
3698 macro_build ((char *) NULL, counter, &ex,
3699 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
3700 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3701 }
3702 }
3703 else if (mips_pic == EMBEDDED_PIC)
3704 {
3705 /* We always do
cdf6fd85 3706 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
c9914766
TS
3707 */
3708 macro_build ((char *) NULL, counter, ep,
3709 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3710 "t,r,j", reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
252b5132
RH
3711 }
3712 else
3713 abort ();
3714}
3715
ea1fb5dc
RS
3716/* Move the contents of register SOURCE into register DEST. */
3717
3718static void
3719move_register (counter, dest, source)
3720 int *counter;
3721 int dest;
3722 int source;
3723{
3724 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3725 HAVE_32BIT_GPRS ? "addu" : "daddu",
3726 "d,v,t", dest, source, 0);
3727}
3728
252b5132
RH
3729/*
3730 * Build macros
3731 * This routine implements the seemingly endless macro or synthesized
3732 * instructions and addressing modes in the mips assembly language. Many
3733 * of these macros are simple and are similar to each other. These could
3734 * probably be handled by some kind of table or grammer aproach instead of
3735 * this verbose method. Others are not simple macros but are more like
3736 * optimizing code generation.
3737 * One interesting optimization is when several store macros appear
3738 * consecutivly that would load AT with the upper half of the same address.
3739 * The ensuing load upper instructions are ommited. This implies some kind
3740 * of global optimization. We currently only optimize within a single macro.
3741 * For many of the load and store macros if the address is specified as a
3742 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
3743 * first load register 'at' with zero and use it as the base register. The
3744 * mips assembler simply uses register $zero. Just one tiny optimization
3745 * we're missing.
3746 */
3747static void
3748macro (ip)
3749 struct mips_cl_insn *ip;
3750{
3751 register int treg, sreg, dreg, breg;
3752 int tempreg;
3753 int mask;
3754 int icnt = 0;
43841e91 3755 int used_at = 0;
252b5132
RH
3756 expressionS expr1;
3757 const char *s;
3758 const char *s2;
3759 const char *fmt;
3760 int likely = 0;
3761 int dbl = 0;
3762 int coproc = 0;
3763 int lr = 0;
3764 int imm = 0;
3765 offsetT maxnum;
3766 int off;
3767 bfd_reloc_code_real_type r;
252b5132
RH
3768 int hold_mips_optimize;
3769
3770 assert (! mips_opts.mips16);
3771
3772 treg = (ip->insn_opcode >> 16) & 0x1f;
3773 dreg = (ip->insn_opcode >> 11) & 0x1f;
3774 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
3775 mask = ip->insn_mo->mask;
3776
3777 expr1.X_op = O_constant;
3778 expr1.X_op_symbol = NULL;
3779 expr1.X_add_symbol = NULL;
3780 expr1.X_add_number = 1;
3781
3782 switch (mask)
3783 {
3784 case M_DABS:
3785 dbl = 1;
3786 case M_ABS:
3787 /* bgez $a0,.+12
3788 move v0,$a0
3789 sub v0,$zero,$a0
3790 */
3791
3792 mips_emit_delays (true);
3793 ++mips_opts.noreorder;
3794 mips_any_noreorder = 1;
3795
3796 expr1.X_add_number = 8;
3797 macro_build ((char *) NULL, &icnt, &expr1, "bgez", "s,p", sreg);
3798 if (dreg == sreg)
2396cfb9
TS
3799 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
3800 0);
252b5132 3801 else
ea1fb5dc 3802 move_register (&icnt, dreg, sreg);
2396cfb9 3803 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 3804 dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
252b5132
RH
3805
3806 --mips_opts.noreorder;
3807 return;
3808
3809 case M_ADD_I:
3810 s = "addi";
3811 s2 = "add";
3812 goto do_addi;
3813 case M_ADDU_I:
3814 s = "addiu";
3815 s2 = "addu";
3816 goto do_addi;
3817 case M_DADD_I:
3818 dbl = 1;
3819 s = "daddi";
3820 s2 = "dadd";
3821 goto do_addi;
3822 case M_DADDU_I:
3823 dbl = 1;
3824 s = "daddiu";
3825 s2 = "daddu";
3826 do_addi:
3827 if (imm_expr.X_op == O_constant
3828 && imm_expr.X_add_number >= -0x8000
3829 && imm_expr.X_add_number < 0x8000)
3830 {
3831 macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,j", treg, sreg,
3832 (int) BFD_RELOC_LO16);
3833 return;
3834 }
3835 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9
TS
3836 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
3837 treg, sreg, AT);
252b5132
RH
3838 break;
3839
3840 case M_AND_I:
3841 s = "andi";
3842 s2 = "and";
3843 goto do_bit;
3844 case M_OR_I:
3845 s = "ori";
3846 s2 = "or";
3847 goto do_bit;
3848 case M_NOR_I:
3849 s = "";
3850 s2 = "nor";
3851 goto do_bit;
3852 case M_XOR_I:
3853 s = "xori";
3854 s2 = "xor";
3855 do_bit:
3856 if (imm_expr.X_op == O_constant
3857 && imm_expr.X_add_number >= 0
3858 && imm_expr.X_add_number < 0x10000)
3859 {
3860 if (mask != M_NOR_I)
3861 macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,i", treg,
3862 sreg, (int) BFD_RELOC_LO16);
3863 else
3864 {
3865 macro_build ((char *) NULL, &icnt, &imm_expr, "ori", "t,r,i",
3866 treg, sreg, (int) BFD_RELOC_LO16);
2396cfb9
TS
3867 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nor",
3868 "d,v,t", treg, treg, 0);
252b5132
RH
3869 }
3870 return;
3871 }
3872
d6bc6245 3873 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
3874 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
3875 treg, sreg, AT);
252b5132
RH
3876 break;
3877
3878 case M_BEQ_I:
3879 s = "beq";
3880 goto beq_i;
3881 case M_BEQL_I:
3882 s = "beql";
3883 likely = 1;
3884 goto beq_i;
3885 case M_BNE_I:
3886 s = "bne";
3887 goto beq_i;
3888 case M_BNEL_I:
3889 s = "bnel";
3890 likely = 1;
3891 beq_i:
3892 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
3893 {
3894 macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg,
3895 0);
3896 return;
3897 }
4d34fb5f 3898 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
252b5132
RH
3899 macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg, AT);
3900 break;
3901
3902 case M_BGEL:
3903 likely = 1;
3904 case M_BGE:
3905 if (treg == 0)
3906 {
3907 macro_build ((char *) NULL, &icnt, &offset_expr,
2396cfb9 3908 likely ? "bgezl" : "bgez", "s,p", sreg);
252b5132
RH
3909 return;
3910 }
3911 if (sreg == 0)
3912 {
3913 macro_build ((char *) NULL, &icnt, &offset_expr,
2396cfb9 3914 likely ? "blezl" : "blez", "s,p", treg);
252b5132
RH
3915 return;
3916 }
2396cfb9
TS
3917 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
3918 AT, sreg, treg);
252b5132 3919 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 3920 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
3921 break;
3922
3923 case M_BGTL_I:
3924 likely = 1;
3925 case M_BGT_I:
3926 /* check for > max integer */
3927 maxnum = 0x7fffffff;
ca4e0257 3928 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
3929 {
3930 maxnum <<= 16;
3931 maxnum |= 0xffff;
3932 maxnum <<= 16;
3933 maxnum |= 0xffff;
3934 }
3935 if (imm_expr.X_op == O_constant
3936 && imm_expr.X_add_number >= maxnum
ca4e0257 3937 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
3938 {
3939 do_false:
3940 /* result is always false */
3941 if (! likely)
3942 {
39c0a331
L
3943 if (warn_nops)
3944 as_warn (_("Branch %s is always false (nop)"),
3945 ip->insn_mo->name);
2396cfb9
TS
3946 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop",
3947 "", 0);
252b5132
RH
3948 }
3949 else
3950 {
39c0a331
L
3951 if (warn_nops)
3952 as_warn (_("Branch likely %s is always false"),
3953 ip->insn_mo->name);
252b5132
RH
3954 macro_build ((char *) NULL, &icnt, &offset_expr, "bnel",
3955 "s,t,p", 0, 0);
3956 }
3957 return;
3958 }
3959 if (imm_expr.X_op != O_constant)
3960 as_bad (_("Unsupported large constant"));
f9419b05 3961 ++imm_expr.X_add_number;
252b5132
RH
3962 /* FALLTHROUGH */
3963 case M_BGE_I:
3964 case M_BGEL_I:
3965 if (mask == M_BGEL_I)
3966 likely = 1;
3967 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
3968 {
3969 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 3970 likely ? "bgezl" : "bgez", "s,p", sreg);
252b5132
RH
3971 return;
3972 }
3973 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
3974 {
3975 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 3976 likely ? "bgtzl" : "bgtz", "s,p", sreg);
252b5132
RH
3977 return;
3978 }
3979 maxnum = 0x7fffffff;
ca4e0257 3980 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
3981 {
3982 maxnum <<= 16;
3983 maxnum |= 0xffff;
3984 maxnum <<= 16;
3985 maxnum |= 0xffff;
3986 }
3987 maxnum = - maxnum - 1;
3988 if (imm_expr.X_op == O_constant
3989 && imm_expr.X_add_number <= maxnum
ca4e0257 3990 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
3991 {
3992 do_true:
3993 /* result is always true */
3994 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
3995 macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
3996 return;
3997 }
3998 set_at (&icnt, sreg, 0);
3999 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4000 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4001 break;
4002
4003 case M_BGEUL:
4004 likely = 1;
4005 case M_BGEU:
4006 if (treg == 0)
4007 goto do_true;
4008 if (sreg == 0)
4009 {
4010 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4011 likely ? "beql" : "beq", "s,t,p", 0, treg);
252b5132
RH
4012 return;
4013 }
2396cfb9
TS
4014 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4015 "d,v,t", AT, sreg, treg);
252b5132 4016 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4017 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4018 break;
4019
4020 case M_BGTUL_I:
4021 likely = 1;
4022 case M_BGTU_I:
4023 if (sreg == 0
ca4e0257 4024 || (HAVE_32BIT_GPRS
252b5132 4025 && imm_expr.X_op == O_constant
956cd1d6 4026 && imm_expr.X_add_number == (offsetT) 0xffffffff))
252b5132
RH
4027 goto do_false;
4028 if (imm_expr.X_op != O_constant)
4029 as_bad (_("Unsupported large constant"));
f9419b05 4030 ++imm_expr.X_add_number;
252b5132
RH
4031 /* FALLTHROUGH */
4032 case M_BGEU_I:
4033 case M_BGEUL_I:
4034 if (mask == M_BGEUL_I)
4035 likely = 1;
4036 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4037 goto do_true;
4038 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4039 {
4040 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4041 likely ? "bnel" : "bne", "s,t,p", sreg, 0);
252b5132
RH
4042 return;
4043 }
4044 set_at (&icnt, sreg, 1);
4045 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4046 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4047 break;
4048
4049 case M_BGTL:
4050 likely = 1;
4051 case M_BGT:
4052 if (treg == 0)
4053 {
4054 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4055 likely ? "bgtzl" : "bgtz", "s,p", sreg);
252b5132
RH
4056 return;
4057 }
4058 if (sreg == 0)
4059 {
4060 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4061 likely ? "bltzl" : "bltz", "s,p", treg);
252b5132
RH
4062 return;
4063 }
2396cfb9
TS
4064 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4065 AT, treg, sreg);
252b5132 4066 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4067 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4068 break;
4069
4070 case M_BGTUL:
4071 likely = 1;
4072 case M_BGTU:
4073 if (treg == 0)
4074 {
4075 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4076 likely ? "bnel" : "bne", "s,t,p", sreg, 0);
252b5132
RH
4077 return;
4078 }
4079 if (sreg == 0)
4080 goto do_false;
2396cfb9
TS
4081 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4082 "d,v,t", AT, treg, sreg);
252b5132 4083 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4084 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4085 break;
4086
4087 case M_BLEL:
4088 likely = 1;
4089 case M_BLE:
4090 if (treg == 0)
4091 {
4092 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4093 likely ? "blezl" : "blez", "s,p", sreg);
252b5132
RH
4094 return;
4095 }
4096 if (sreg == 0)
4097 {
4098 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4099 likely ? "bgezl" : "bgez", "s,p", treg);
252b5132
RH
4100 return;
4101 }
2396cfb9
TS
4102 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4103 AT, treg, sreg);
252b5132 4104 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4105 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4106 break;
4107
4108 case M_BLEL_I:
4109 likely = 1;
4110 case M_BLE_I:
4111 maxnum = 0x7fffffff;
ca4e0257 4112 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
4113 {
4114 maxnum <<= 16;
4115 maxnum |= 0xffff;
4116 maxnum <<= 16;
4117 maxnum |= 0xffff;
4118 }
4119 if (imm_expr.X_op == O_constant
4120 && imm_expr.X_add_number >= maxnum
ca4e0257 4121 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
4122 goto do_true;
4123 if (imm_expr.X_op != O_constant)
4124 as_bad (_("Unsupported large constant"));
f9419b05 4125 ++imm_expr.X_add_number;
252b5132
RH
4126 /* FALLTHROUGH */
4127 case M_BLT_I:
4128 case M_BLTL_I:
4129 if (mask == M_BLTL_I)
4130 likely = 1;
4131 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4132 {
4133 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4134 likely ? "bltzl" : "bltz", "s,p", sreg);
252b5132
RH
4135 return;
4136 }
4137 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4138 {
4139 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4140 likely ? "blezl" : "blez", "s,p", sreg);
252b5132
RH
4141 return;
4142 }
4143 set_at (&icnt, sreg, 0);
4144 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4145 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4146 break;
4147
4148 case M_BLEUL:
4149 likely = 1;
4150 case M_BLEU:
4151 if (treg == 0)
4152 {
4153 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4154 likely ? "beql" : "beq", "s,t,p", sreg, 0);
252b5132
RH
4155 return;
4156 }
4157 if (sreg == 0)
4158 goto do_true;
2396cfb9
TS
4159 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4160 "d,v,t", AT, treg, sreg);
252b5132 4161 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4162 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4163 break;
4164
4165 case M_BLEUL_I:
4166 likely = 1;
4167 case M_BLEU_I:
4168 if (sreg == 0
ca4e0257 4169 || (HAVE_32BIT_GPRS
252b5132 4170 && imm_expr.X_op == O_constant
956cd1d6 4171 && imm_expr.X_add_number == (offsetT) 0xffffffff))
252b5132
RH
4172 goto do_true;
4173 if (imm_expr.X_op != O_constant)
4174 as_bad (_("Unsupported large constant"));
f9419b05 4175 ++imm_expr.X_add_number;
252b5132
RH
4176 /* FALLTHROUGH */
4177 case M_BLTU_I:
4178 case M_BLTUL_I:
4179 if (mask == M_BLTUL_I)
4180 likely = 1;
4181 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4182 goto do_false;
4183 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4184 {
4185 macro_build ((char *) NULL, &icnt, &offset_expr,
4186 likely ? "beql" : "beq",
4187 "s,t,p", sreg, 0);
4188 return;
4189 }
4190 set_at (&icnt, sreg, 1);
4191 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4192 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4193 break;
4194
4195 case M_BLTL:
4196 likely = 1;
4197 case M_BLT:
4198 if (treg == 0)
4199 {
4200 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4201 likely ? "bltzl" : "bltz", "s,p", sreg);
252b5132
RH
4202 return;
4203 }
4204 if (sreg == 0)
4205 {
4206 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4207 likely ? "bgtzl" : "bgtz", "s,p", treg);
252b5132
RH
4208 return;
4209 }
2396cfb9
TS
4210 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4211 AT, sreg, treg);
252b5132 4212 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4213 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4214 break;
4215
4216 case M_BLTUL:
4217 likely = 1;
4218 case M_BLTU:
4219 if (treg == 0)
4220 goto do_false;
4221 if (sreg == 0)
4222 {
4223 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4224 likely ? "bnel" : "bne", "s,t,p", 0, treg);
252b5132
RH
4225 return;
4226 }
2396cfb9
TS
4227 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4228 "d,v,t", AT, sreg,
252b5132
RH
4229 treg);
4230 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4231 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4232 break;
4233
4234 case M_DDIV_3:
4235 dbl = 1;
4236 case M_DIV_3:
4237 s = "mflo";
4238 goto do_div3;
4239 case M_DREM_3:
4240 dbl = 1;
4241 case M_REM_3:
4242 s = "mfhi";
4243 do_div3:
4244 if (treg == 0)
4245 {
4246 as_warn (_("Divide by zero."));
4247 if (mips_trap)
2396cfb9 4248 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
9bd7d936 4249 "s,t,q", 0, 0, 7);
252b5132 4250 else
2396cfb9
TS
4251 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4252 "c", 7);
252b5132
RH
4253 return;
4254 }
4255
4256 mips_emit_delays (true);
4257 ++mips_opts.noreorder;
4258 mips_any_noreorder = 1;
4259 if (mips_trap)
4260 {
2396cfb9 4261 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
9bd7d936 4262 "s,t,q", treg, 0, 7);
2396cfb9 4263 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 4264 dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
252b5132
RH
4265 }
4266 else
4267 {
4268 expr1.X_add_number = 8;
4269 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
2396cfb9 4270 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 4271 dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
2396cfb9
TS
4272 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4273 "c", 7);
252b5132
RH
4274 }
4275 expr1.X_add_number = -1;
4276 macro_build ((char *) NULL, &icnt, &expr1,
4277 dbl ? "daddiu" : "addiu",
4278 "t,r,j", AT, 0, (int) BFD_RELOC_LO16);
4279 expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
4280 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, AT);
4281 if (dbl)
4282 {
4283 expr1.X_add_number = 1;
4284 macro_build ((char *) NULL, &icnt, &expr1, "daddiu", "t,r,j", AT, 0,
4285 (int) BFD_RELOC_LO16);
2396cfb9
TS
4286 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsll32",
4287 "d,w,<", AT, AT, 31);
252b5132
RH
4288 }
4289 else
4290 {
4291 expr1.X_add_number = 0x80000000;
4292 macro_build ((char *) NULL, &icnt, &expr1, "lui", "t,u", AT,
4293 (int) BFD_RELOC_HI16);
4294 }
4295 if (mips_trap)
4296 {
2396cfb9 4297 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
9bd7d936 4298 "s,t,q", sreg, AT, 6);
252b5132
RH
4299 /* We want to close the noreorder block as soon as possible, so
4300 that later insns are available for delay slot filling. */
4301 --mips_opts.noreorder;
4302 }
4303 else
4304 {
4305 expr1.X_add_number = 8;
4306 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", sreg, AT);
2396cfb9
TS
4307 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
4308 0);
252b5132
RH
4309
4310 /* We want to close the noreorder block as soon as possible, so
4311 that later insns are available for delay slot filling. */
4312 --mips_opts.noreorder;
4313
2396cfb9
TS
4314 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4315 "c", 6);
252b5132 4316 }
2396cfb9 4317 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d", dreg);
252b5132
RH
4318 break;
4319
4320 case M_DIV_3I:
4321 s = "div";
4322 s2 = "mflo";
4323 goto do_divi;
4324 case M_DIVU_3I:
4325 s = "divu";
4326 s2 = "mflo";
4327 goto do_divi;
4328 case M_REM_3I:
4329 s = "div";
4330 s2 = "mfhi";
4331 goto do_divi;
4332 case M_REMU_3I:
4333 s = "divu";
4334 s2 = "mfhi";
4335 goto do_divi;
4336 case M_DDIV_3I:
4337 dbl = 1;
4338 s = "ddiv";
4339 s2 = "mflo";
4340 goto do_divi;
4341 case M_DDIVU_3I:
4342 dbl = 1;
4343 s = "ddivu";
4344 s2 = "mflo";
4345 goto do_divi;
4346 case M_DREM_3I:
4347 dbl = 1;
4348 s = "ddiv";
4349 s2 = "mfhi";
4350 goto do_divi;
4351 case M_DREMU_3I:
4352 dbl = 1;
4353 s = "ddivu";
4354 s2 = "mfhi";
4355 do_divi:
4356 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4357 {
4358 as_warn (_("Divide by zero."));
4359 if (mips_trap)
2396cfb9 4360 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
9bd7d936 4361 "s,t,q", 0, 0, 7);
252b5132 4362 else
2396cfb9
TS
4363 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4364 "c", 7);
252b5132
RH
4365 return;
4366 }
4367 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4368 {
4369 if (strcmp (s2, "mflo") == 0)
ea1fb5dc 4370 move_register (&icnt, dreg, sreg);
252b5132 4371 else
ea1fb5dc 4372 move_register (&icnt, dreg, 0);
252b5132
RH
4373 return;
4374 }
4375 if (imm_expr.X_op == O_constant
4376 && imm_expr.X_add_number == -1
4377 && s[strlen (s) - 1] != 'u')
4378 {
4379 if (strcmp (s2, "mflo") == 0)
4380 {
2396cfb9
TS
4381 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4382 dbl ? "dneg" : "neg", "d,w", dreg, sreg);
252b5132
RH
4383 }
4384 else
ea1fb5dc 4385 move_register (&icnt, dreg, 0);
252b5132
RH
4386 return;
4387 }
4388
4389 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9
TS
4390 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4391 sreg, AT);
4392 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
252b5132
RH
4393 break;
4394
4395 case M_DIVU_3:
4396 s = "divu";
4397 s2 = "mflo";
4398 goto do_divu3;
4399 case M_REMU_3:
4400 s = "divu";
4401 s2 = "mfhi";
4402 goto do_divu3;
4403 case M_DDIVU_3:
4404 s = "ddivu";
4405 s2 = "mflo";
4406 goto do_divu3;
4407 case M_DREMU_3:
4408 s = "ddivu";
4409 s2 = "mfhi";
4410 do_divu3:
4411 mips_emit_delays (true);
4412 ++mips_opts.noreorder;
4413 mips_any_noreorder = 1;
4414 if (mips_trap)
4415 {
2396cfb9 4416 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
9bd7d936 4417 "s,t,q", treg, 0, 7);
2396cfb9
TS
4418 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4419 sreg, treg);
252b5132
RH
4420 /* We want to close the noreorder block as soon as possible, so
4421 that later insns are available for delay slot filling. */
4422 --mips_opts.noreorder;
4423 }
4424 else
4425 {
4426 expr1.X_add_number = 8;
4427 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
2396cfb9
TS
4428 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4429 sreg, treg);
252b5132
RH
4430
4431 /* We want to close the noreorder block as soon as possible, so
4432 that later insns are available for delay slot filling. */
4433 --mips_opts.noreorder;
2396cfb9
TS
4434 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4435 "c", 7);
252b5132 4436 }
2396cfb9 4437 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
252b5132
RH
4438 return;
4439
4440 case M_DLA_AB:
4441 dbl = 1;
4442 case M_LA_AB:
4443 /* Load the address of a symbol into a register. If breg is not
4444 zero, we then add a base register to it. */
4445
3bec30a8
TS
4446 if (dbl && HAVE_32BIT_GPRS)
4447 as_warn (_("dla used to load 32-bit register"));
4448
c90bbe5b 4449 if (! dbl && HAVE_64BIT_OBJECTS)
3bec30a8
TS
4450 as_warn (_("la used to load 64-bit address"));
4451
0c11417f
MR
4452 if (offset_expr.X_op == O_constant
4453 && offset_expr.X_add_number >= -0x8000
4454 && offset_expr.X_add_number < 0x8000)
4455 {
4456 macro_build ((char *) NULL, &icnt, &offset_expr,
4457 (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
4458 "t,r,j", treg, sreg, (int) BFD_RELOC_LO16);
4459 return;
4460 }
4461
afdbd6d0
CD
4462 if (treg == breg)
4463 {
4464 tempreg = AT;
4465 used_at = 1;
4466 }
4467 else
4468 {
4469 tempreg = treg;
4470 used_at = 0;
4471 }
4472
252b5132
RH
4473 /* When generating embedded PIC code, we permit expressions of
4474 the form
afdbd6d0
CD
4475 la $treg,foo-bar
4476 la $treg,foo-bar($breg)
bb2d6cd7 4477 where bar is an address in the current section. These are used
252b5132
RH
4478 when getting the addresses of functions. We don't permit
4479 X_add_number to be non-zero, because if the symbol is
4480 external the relaxing code needs to know that any addend is
4481 purely the offset to X_op_symbol. */
4482 if (mips_pic == EMBEDDED_PIC
4483 && offset_expr.X_op == O_subtract
49309057 4484 && (symbol_constant_p (offset_expr.X_op_symbol)
bb2d6cd7 4485 ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
49309057
ILT
4486 : (symbol_equated_p (offset_expr.X_op_symbol)
4487 && (S_GET_SEGMENT
4488 (symbol_get_value_expression (offset_expr.X_op_symbol)
4489 ->X_add_symbol)
bb2d6cd7 4490 == now_seg)))
bb2d6cd7
GK
4491 && (offset_expr.X_add_number == 0
4492 || OUTPUT_FLAVOR == bfd_target_elf_flavour))
252b5132 4493 {
afdbd6d0
CD
4494 if (breg == 0)
4495 {
4496 tempreg = treg;
4497 used_at = 0;
4498 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4499 tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4500 }
4501 else
4502 {
4503 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4504 tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4505 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4d34fb5f 4506 (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu",
afdbd6d0
CD
4507 "d,v,t", tempreg, tempreg, breg);
4508 }
252b5132 4509 macro_build ((char *) NULL, &icnt, &offset_expr,
4d34fb5f 4510 (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
afdbd6d0
CD
4511 "t,r,j", treg, tempreg, (int) BFD_RELOC_PCREL_LO16);
4512 if (! used_at)
4513 return;
4514 break;
252b5132
RH
4515 }
4516
4517 if (offset_expr.X_op != O_symbol
4518 && offset_expr.X_op != O_constant)
4519 {
4520 as_bad (_("expression too complex"));
4521 offset_expr.X_op = O_constant;
4522 }
4523
252b5132 4524 if (offset_expr.X_op == O_constant)
4d34fb5f
TS
4525 load_register (&icnt, tempreg, &offset_expr,
4526 ((mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
4527 ? (dbl || HAVE_64BIT_ADDRESSES)
4528 : HAVE_64BIT_ADDRESSES));
252b5132
RH
4529 else if (mips_pic == NO_PIC)
4530 {
d6bc6245 4531 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 4532 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
4533 Otherwise we want
4534 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
4535 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4536 If we have a constant, we need two instructions anyhow,
d6bc6245 4537 so we may as well always use the latter form.
76b3015f 4538
d6bc6245
TS
4539 With 64bit address space and a usable $at we want
4540 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4541 lui $at,<sym> (BFD_RELOC_HI16_S)
4542 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4543 daddiu $at,<sym> (BFD_RELOC_LO16)
4544 dsll32 $tempreg,0
3a482fd5 4545 daddu $tempreg,$tempreg,$at
76b3015f 4546
d6bc6245
TS
4547 If $at is already in use, we use an path which is suboptimal
4548 on superscalar processors.
4549 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4550 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4551 dsll $tempreg,16
4552 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
4553 dsll $tempreg,16
4554 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
4555 */
f9419b05 4556 char *p = NULL;
d6bc6245 4557 if (HAVE_64BIT_ADDRESSES)
252b5132 4558 {
d6bc6245
TS
4559 /* We don't do GP optimization for now because RELAX_ENCODE can't
4560 hold the data for such large chunks. */
4561
460597ba 4562 if (used_at == 0 && ! mips_opts.noat)
98d3f06f
KH
4563 {
4564 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4565 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4566 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4567 AT, (int) BFD_RELOC_HI16_S);
4568 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4569 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4570 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4571 AT, AT, (int) BFD_RELOC_LO16);
4572 macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
4573 "d,w,<", tempreg, tempreg, 0);
3a482fd5
MR
4574 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
4575 "d,v,t", tempreg, tempreg, AT);
98d3f06f
KH
4576 used_at = 1;
4577 }
4578 else
4579 {
4580 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4581 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4582 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4583 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4584 macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4585 tempreg, tempreg, 16);
4586 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4587 tempreg, tempreg, (int) BFD_RELOC_HI16_S);
4588 macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4589 tempreg, tempreg, 16);
4590 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4591 tempreg, tempreg, (int) BFD_RELOC_LO16);
4592 }
4593 }
4594 else
4595 {
4596 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4597 && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
4598 {
4599 frag_grow (20);
4600 macro_build ((char *) NULL, &icnt, &offset_expr, "addiu",
c9914766
TS
4601 "t,r,j", tempreg, mips_gp_register,
4602 (int) BFD_RELOC_GPREL16);
98d3f06f
KH
4603 p = frag_var (rs_machine_dependent, 8, 0,
4604 RELAX_ENCODE (4, 8, 0, 4, 0,
4605 mips_opts.warn_about_macros),
4606 offset_expr.X_add_symbol, 0, NULL);
4607 }
4608 macro_build_lui (p, &icnt, &offset_expr, tempreg);
4609 if (p != NULL)
4610 p += 4;
4611 macro_build (p, &icnt, &offset_expr, "addiu",
4612 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4613 }
252b5132
RH
4614 }
4615 else if (mips_pic == SVR4_PIC && ! mips_big_got)
4616 {
9117d219
NC
4617 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
4618
252b5132
RH
4619 /* If this is a reference to an external symbol, and there
4620 is no constant, we want
4621 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9117d219
NC
4622 or if tempreg is PIC_CALL_REG
4623 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
252b5132
RH
4624 For a local symbol, we want
4625 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4626 nop
4627 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4628
4629 If we have a small constant, and this is a reference to
4630 an external symbol, we want
4631 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4632 nop
4633 addiu $tempreg,$tempreg,<constant>
4634 For a local symbol, we want the same instruction
4635 sequence, but we output a BFD_RELOC_LO16 reloc on the
4636 addiu instruction.
4637
4638 If we have a large constant, and this is a reference to
4639 an external symbol, we want
4640 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4641 lui $at,<hiconstant>
4642 addiu $at,$at,<loconstant>
4643 addu $tempreg,$tempreg,$at
4644 For a local symbol, we want the same instruction
4645 sequence, but we output a BFD_RELOC_LO16 reloc on the
4646 addiu instruction. */
4647 expr1.X_add_number = offset_expr.X_add_number;
4648 offset_expr.X_add_number = 0;
4649 frag_grow (32);
9117d219
NC
4650 if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4651 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
4d34fb5f
TS
4652 macro_build ((char *) NULL, &icnt, &offset_expr,
4653 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
c9914766 4654 "t,o(b)", tempreg, lw_reloc_type, mips_gp_register);
252b5132
RH
4655 if (expr1.X_add_number == 0)
4656 {
4657 int off;
f9419b05 4658 char *p;
252b5132
RH
4659
4660 if (breg == 0)
4661 off = 0;
4662 else
4663 {
4664 /* We're going to put in an addu instruction using
4665 tempreg, so we may as well insert the nop right
4666 now. */
4667 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4668 "nop", "");
4669 off = 4;
4670 }
4671 p = frag_var (rs_machine_dependent, 8 - off, 0,
4672 RELAX_ENCODE (0, 8 - off, -4 - off, 4 - off, 0,
4673 (breg == 0
4674 ? mips_opts.warn_about_macros
4675 : 0)),
c4e7957c 4676 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
4677 if (breg == 0)
4678 {
4679 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4680 p += 4;
4681 }
4682 macro_build (p, &icnt, &expr1,
ca4e0257 4683 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
4684 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4685 /* FIXME: If breg == 0, and the next instruction uses
4686 $tempreg, then if this variant case is used an extra
4687 nop will be generated. */
4688 }
4689 else if (expr1.X_add_number >= -0x8000
4690 && expr1.X_add_number < 0x8000)
4691 {
4692 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4693 "nop", "");
4694 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 4695 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132 4696 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
c4e7957c
TS
4697 frag_var (rs_machine_dependent, 0, 0,
4698 RELAX_ENCODE (0, 0, -12, -4, 0, 0),
4699 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
4700 }
4701 else
4702 {
4703 int off1;
4704
4705 /* If we are going to add in a base register, and the
4706 target register and the base register are the same,
4707 then we are using AT as a temporary register. Since
4708 we want to load the constant into AT, we add our
4709 current AT (from the global offset table) and the
4710 register into the register now, and pretend we were
4711 not using a base register. */
4712 if (breg != treg)
4713 off1 = 0;
4714 else
4715 {
4716 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4717 "nop", "");
4718 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 4719 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
4720 "d,v,t", treg, AT, breg);
4721 breg = 0;
4722 tempreg = treg;
4723 off1 = -8;
4724 }
4725
4726 /* Set mips_optimize around the lui instruction to avoid
4727 inserting an unnecessary nop after the lw. */
4728 hold_mips_optimize = mips_optimize;
4729 mips_optimize = 2;
c4e7957c 4730 macro_build_lui (NULL, &icnt, &expr1, AT);
252b5132
RH
4731 mips_optimize = hold_mips_optimize;
4732
4733 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 4734 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
4735 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4736 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 4737 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132 4738 "d,v,t", tempreg, tempreg, AT);
c4e7957c
TS
4739 frag_var (rs_machine_dependent, 0, 0,
4740 RELAX_ENCODE (0, 0, -16 + off1, -8, 0, 0),
4741 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
4742 used_at = 1;
4743 }
4744 }
4745 else if (mips_pic == SVR4_PIC)
4746 {
4747 int gpdel;
f9419b05 4748 char *p;
9117d219
NC
4749 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
4750 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
252b5132
RH
4751
4752 /* This is the large GOT case. If this is a reference to an
4753 external symbol, and there is no constant, we want
4754 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
4755 addu $tempreg,$tempreg,$gp
4756 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
9117d219
NC
4757 or if tempreg is PIC_CALL_REG
4758 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
4759 addu $tempreg,$tempreg,$gp
4760 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
252b5132
RH
4761 For a local symbol, we want
4762 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4763 nop
4764 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4765
4766 If we have a small constant, and this is a reference to
4767 an external symbol, we want
4768 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
4769 addu $tempreg,$tempreg,$gp
4770 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4771 nop
4772 addiu $tempreg,$tempreg,<constant>
4773 For a local symbol, we want
4774 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4775 nop
4776 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
4777
4778 If we have a large constant, and this is a reference to
4779 an external symbol, we want
4780 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
4781 addu $tempreg,$tempreg,$gp
4782 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4783 lui $at,<hiconstant>
4784 addiu $at,$at,<loconstant>
4785 addu $tempreg,$tempreg,$at
4786 For a local symbol, we want
4787 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4788 lui $at,<hiconstant>
4789 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
4790 addu $tempreg,$tempreg,$at
438c16b8
TS
4791
4792 For NewABI, we want for data addresses
4793 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
4794 If tempreg is PIC_CALL_REG pointing to a external symbol, we want
4795 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
4796 */
4797 if (HAVE_NEWABI)
4798 {
4799 int reloc_type = (tempreg == PIC_CALL_REG
4800 ? BFD_RELOC_MIPS_CALL16
4801 : BFD_RELOC_MIPS_GOT_DISP);
4802
4803 macro_build ((char *) NULL, &icnt, &offset_expr,
4804 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4805 "t,o(b)", tempreg, reloc_type, mips_gp_register);
4806
4807 if (breg != 0)
4808 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4809 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4810 "d,v,t", treg, tempreg, breg);
4811
4812 if (! used_at)
4813 return;
4814
4815 break;
4816 }
252b5132
RH
4817 expr1.X_add_number = offset_expr.X_add_number;
4818 offset_expr.X_add_number = 0;
4819 frag_grow (52);
f7ea7ef2 4820 if (reg_needs_delay (mips_gp_register))
252b5132
RH
4821 gpdel = 4;
4822 else
4823 gpdel = 0;
9117d219
NC
4824 if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4825 {
4826 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
4827 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
4828 }
252b5132 4829 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
9117d219 4830 tempreg, lui_reloc_type);
252b5132 4831 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 4832 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 4833 "d,v,t", tempreg, tempreg, mips_gp_register);
252b5132 4834 macro_build ((char *) NULL, &icnt, &offset_expr,
4d34fb5f 4835 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
9117d219 4836 "t,o(b)", tempreg, lw_reloc_type, tempreg);
252b5132
RH
4837 if (expr1.X_add_number == 0)
4838 {
4839 int off;
4840
4841 if (breg == 0)
4842 off = 0;
4843 else
4844 {
4845 /* We're going to put in an addu instruction using
4846 tempreg, so we may as well insert the nop right
4847 now. */
4848 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4849 "nop", "");
4850 off = 4;
4851 }
4852
4853 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4854 RELAX_ENCODE (12 + off, 12 + gpdel, gpdel,
4855 8 + gpdel, 0,
4856 (breg == 0
4857 ? mips_opts.warn_about_macros
4858 : 0)),
c4e7957c 4859 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
4860 }
4861 else if (expr1.X_add_number >= -0x8000
4862 && expr1.X_add_number < 0x8000)
4863 {
4864 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4865 "nop", "");
4866 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 4867 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
4868 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4869
4870 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4871 RELAX_ENCODE (20, 12 + gpdel, gpdel, 8 + gpdel, 0,
4872 (breg == 0
4873 ? mips_opts.warn_about_macros
4874 : 0)),
c4e7957c 4875 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
4876 }
4877 else
4878 {
4879 int adj, dreg;
4880
4881 /* If we are going to add in a base register, and the
4882 target register and the base register are the same,
4883 then we are using AT as a temporary register. Since
4884 we want to load the constant into AT, we add our
4885 current AT (from the global offset table) and the
4886 register into the register now, and pretend we were
4887 not using a base register. */
4888 if (breg != treg)
4889 {
4890 adj = 0;
4891 dreg = tempreg;
4892 }
4893 else
4894 {
4895 assert (tempreg == AT);
4896 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4897 "nop", "");
4898 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 4899 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
4900 "d,v,t", treg, AT, breg);
4901 dreg = treg;
4902 adj = 8;
4903 }
4904
4905 /* Set mips_optimize around the lui instruction to avoid
4906 inserting an unnecessary nop after the lw. */
4907 hold_mips_optimize = mips_optimize;
4908 mips_optimize = 2;
c4e7957c 4909 macro_build_lui (NULL, &icnt, &expr1, AT);
252b5132
RH
4910 mips_optimize = hold_mips_optimize;
4911
4912 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 4913 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
4914 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4915 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 4916 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
4917 "d,v,t", dreg, dreg, AT);
4918
4919 p = frag_var (rs_machine_dependent, 16 + gpdel + adj, 0,
4920 RELAX_ENCODE (24 + adj, 16 + gpdel + adj, gpdel,
4921 8 + gpdel, 0,
4922 (breg == 0
4923 ? mips_opts.warn_about_macros
4924 : 0)),
c4e7957c 4925 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
4926
4927 used_at = 1;
4928 }
4929
4930 if (gpdel > 0)
4931 {
4932 /* This is needed because this instruction uses $gp, but
4933 the first instruction on the main stream does not. */
4934 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4935 p += 4;
4936 }
4937 macro_build (p, &icnt, &offset_expr,
4d34fb5f 4938 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
c9914766
TS
4939 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16,
4940 mips_gp_register);
252b5132
RH
4941 p += 4;
4942 if (expr1.X_add_number >= -0x8000
4943 && expr1.X_add_number < 0x8000)
4944 {
4945 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4946 p += 4;
4947 macro_build (p, &icnt, &expr1,
ca4e0257 4948 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
4949 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4950 /* FIXME: If add_number is 0, and there was no base
4951 register, the external symbol case ended with a load,
4952 so if the symbol turns out to not be external, and
4953 the next instruction uses tempreg, an unnecessary nop
4954 will be inserted. */
4955 }
4956 else
4957 {
4958 if (breg == treg)
4959 {
4960 /* We must add in the base register now, as in the
4961 external symbol case. */
4962 assert (tempreg == AT);
4963 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4964 p += 4;
4965 macro_build (p, &icnt, (expressionS *) NULL,
ca4e0257 4966 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
4967 "d,v,t", treg, AT, breg);
4968 p += 4;
4969 tempreg = treg;
4970 /* We set breg to 0 because we have arranged to add
4971 it in in both cases. */
4972 breg = 0;
4973 }
4974
4975 macro_build_lui (p, &icnt, &expr1, AT);
4976 p += 4;
4977 macro_build (p, &icnt, &expr1,
ca4e0257 4978 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
4979 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4980 p += 4;
4981 macro_build (p, &icnt, (expressionS *) NULL,
ca4e0257 4982 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
4983 "d,v,t", tempreg, tempreg, AT);
4984 p += 4;
4985 }
4986 }
4987 else if (mips_pic == EMBEDDED_PIC)
4988 {
4989 /* We use
cdf6fd85 4990 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
4991 */
4992 macro_build ((char *) NULL, &icnt, &offset_expr,
c9914766
TS
4993 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
4994 tempreg, mips_gp_register, (int) BFD_RELOC_GPREL16);
252b5132
RH
4995 }
4996 else
4997 abort ();
4998
4999 if (breg != 0)
4d34fb5f
TS
5000 {
5001 char *s;
5002
5003 if (mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
5004 s = (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu";
5005 else
5006 s = HAVE_64BIT_ADDRESSES ? "daddu" : "addu";
5007
5008 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s,
5009 "d,v,t", treg, tempreg, breg);
5010 }
252b5132
RH
5011
5012 if (! used_at)
5013 return;
5014
5015 break;
5016
5017 case M_J_A:
5018 /* The j instruction may not be used in PIC code, since it
5019 requires an absolute address. We convert it to a b
5020 instruction. */
5021 if (mips_pic == NO_PIC)
5022 macro_build ((char *) NULL, &icnt, &offset_expr, "j", "a");
5023 else
5024 macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
5025 return;
5026
5027 /* The jal instructions must be handled as macros because when
5028 generating PIC code they expand to multi-instruction
5029 sequences. Normally they are simple instructions. */
5030 case M_JAL_1:
5031 dreg = RA;
5032 /* Fall through. */
5033 case M_JAL_2:
5034 if (mips_pic == NO_PIC
5035 || mips_pic == EMBEDDED_PIC)
5036 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5037 "d,s", dreg, sreg);
5038 else if (mips_pic == SVR4_PIC)
5039 {
5040 if (sreg != PIC_CALL_REG)
5041 as_warn (_("MIPS PIC call to register other than $25"));
bdaaa2e1 5042
252b5132
RH
5043 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5044 "d,s", dreg, sreg);
6478892d 5045 if (! HAVE_NEWABI)
252b5132 5046 {
6478892d
TS
5047 if (mips_cprestore_offset < 0)
5048 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5049 else
5050 {
7a621144
DJ
5051 if (! mips_frame_reg_valid)
5052 {
5053 as_warn (_("No .frame pseudo-op used in PIC code"));
5054 /* Quiet this warning. */
5055 mips_frame_reg_valid = 1;
5056 }
5057 if (! mips_cprestore_valid)
5058 {
5059 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5060 /* Quiet this warning. */
5061 mips_cprestore_valid = 1;
5062 }
6478892d
TS
5063 expr1.X_add_number = mips_cprestore_offset;
5064 macro_build ((char *) NULL, &icnt, &expr1,
5065 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
c9914766
TS
5066 mips_gp_register, (int) BFD_RELOC_LO16,
5067 mips_frame_reg);
6478892d 5068 }
252b5132
RH
5069 }
5070 }
5071 else
5072 abort ();
5073
5074 return;
5075
5076 case M_JAL_A:
5077 if (mips_pic == NO_PIC)
5078 macro_build ((char *) NULL, &icnt, &offset_expr, "jal", "a");
5079 else if (mips_pic == SVR4_PIC)
5080 {
f9419b05
TS
5081 char *p;
5082
252b5132
RH
5083 /* If this is a reference to an external symbol, and we are
5084 using a small GOT, we want
5085 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5086 nop
f9419b05 5087 jalr $ra,$25
252b5132
RH
5088 nop
5089 lw $gp,cprestore($sp)
5090 The cprestore value is set using the .cprestore
5091 pseudo-op. If we are using a big GOT, we want
5092 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5093 addu $25,$25,$gp
5094 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
5095 nop
f9419b05 5096 jalr $ra,$25
252b5132
RH
5097 nop
5098 lw $gp,cprestore($sp)
5099 If the symbol is not external, we want
5100 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5101 nop
5102 addiu $25,$25,<sym> (BFD_RELOC_LO16)
f9419b05 5103 jalr $ra,$25
252b5132 5104 nop
438c16b8
TS
5105 lw $gp,cprestore($sp)
5106 For NewABI, we want
5107 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5108 jalr $ra,$25 (BFD_RELOC_MIPS_JALR)
5109 */
5110 if (HAVE_NEWABI)
252b5132
RH
5111 {
5112 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 5113 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132 5114 "t,o(b)", PIC_CALL_REG,
438c16b8
TS
5115 (int) BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5116 macro_build_jalr (icnt, &offset_expr);
252b5132
RH
5117 }
5118 else
5119 {
438c16b8
TS
5120 frag_grow (40);
5121 if (! mips_big_got)
5122 {
5123 macro_build ((char *) NULL, &icnt, &offset_expr,
5124 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5125 "t,o(b)", PIC_CALL_REG,
5126 (int) BFD_RELOC_MIPS_CALL16, mips_gp_register);
5127 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5128 "nop", "");
5129 p = frag_var (rs_machine_dependent, 4, 0,
5130 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5131 offset_expr.X_add_symbol, 0, NULL);
5132 }
252b5132 5133 else
252b5132 5134 {
438c16b8
TS
5135 int gpdel;
5136
5137 if (reg_needs_delay (mips_gp_register))
5138 gpdel = 4;
5139 else
5140 gpdel = 0;
5141 macro_build ((char *) NULL, &icnt, &offset_expr, "lui",
5142 "t,u", PIC_CALL_REG,
5143 (int) BFD_RELOC_MIPS_CALL_HI16);
5144 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5145 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5146 "d,v,t", PIC_CALL_REG, PIC_CALL_REG,
5147 mips_gp_register);
5148 macro_build ((char *) NULL, &icnt, &offset_expr,
5149 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5150 "t,o(b)", PIC_CALL_REG,
5151 (int) BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
5152 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5153 "nop", "");
5154 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5155 RELAX_ENCODE (16, 12 + gpdel, gpdel,
5156 8 + gpdel, 0, 0),
5157 offset_expr.X_add_symbol, 0, NULL);
5158 if (gpdel > 0)
5159 {
5160 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5161 p += 4;
5162 }
5163 macro_build (p, &icnt, &offset_expr,
5164 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5165 "t,o(b)", PIC_CALL_REG,
5166 (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
5167 p += 4;
252b5132
RH
5168 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5169 p += 4;
5170 }
5171 macro_build (p, &icnt, &offset_expr,
438c16b8
TS
5172 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5173 "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
5174 (int) BFD_RELOC_LO16);
5175 macro_build_jalr (icnt, &offset_expr);
5176
6478892d
TS
5177 if (mips_cprestore_offset < 0)
5178 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5179 else
5180 {
7a621144
DJ
5181 if (! mips_frame_reg_valid)
5182 {
5183 as_warn (_("No .frame pseudo-op used in PIC code"));
5184 /* Quiet this warning. */
5185 mips_frame_reg_valid = 1;
5186 }
5187 if (! mips_cprestore_valid)
5188 {
5189 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5190 /* Quiet this warning. */
5191 mips_cprestore_valid = 1;
5192 }
6478892d
TS
5193 if (mips_opts.noreorder)
5194 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
98d3f06f 5195 "nop", "");
6478892d
TS
5196 expr1.X_add_number = mips_cprestore_offset;
5197 macro_build ((char *) NULL, &icnt, &expr1,
5198 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
c9914766
TS
5199 mips_gp_register, (int) BFD_RELOC_LO16,
5200 mips_frame_reg);
6478892d 5201 }
252b5132
RH
5202 }
5203 }
5204 else if (mips_pic == EMBEDDED_PIC)
5205 {
5206 macro_build ((char *) NULL, &icnt, &offset_expr, "bal", "p");
5207 /* The linker may expand the call to a longer sequence which
5208 uses $at, so we must break rather than return. */
5209 break;
5210 }
5211 else
5212 abort ();
5213
5214 return;
5215
5216 case M_LB_AB:
5217 s = "lb";
5218 goto ld;
5219 case M_LBU_AB:
5220 s = "lbu";
5221 goto ld;
5222 case M_LH_AB:
5223 s = "lh";
5224 goto ld;
5225 case M_LHU_AB:
5226 s = "lhu";
5227 goto ld;
5228 case M_LW_AB:
5229 s = "lw";
5230 goto ld;
5231 case M_LWC0_AB:
5232 s = "lwc0";
bdaaa2e1 5233 /* Itbl support may require additional care here. */
252b5132
RH
5234 coproc = 1;
5235 goto ld;
5236 case M_LWC1_AB:
5237 s = "lwc1";
bdaaa2e1 5238 /* Itbl support may require additional care here. */
252b5132
RH
5239 coproc = 1;
5240 goto ld;
5241 case M_LWC2_AB:
5242 s = "lwc2";
bdaaa2e1 5243 /* Itbl support may require additional care here. */
252b5132
RH
5244 coproc = 1;
5245 goto ld;
5246 case M_LWC3_AB:
5247 s = "lwc3";
bdaaa2e1 5248 /* Itbl support may require additional care here. */
252b5132
RH
5249 coproc = 1;
5250 goto ld;
5251 case M_LWL_AB:
5252 s = "lwl";
5253 lr = 1;
5254 goto ld;
5255 case M_LWR_AB:
5256 s = "lwr";
5257 lr = 1;
5258 goto ld;
5259 case M_LDC1_AB:
ec68c924 5260 if (mips_arch == CPU_R4650)
252b5132
RH
5261 {
5262 as_bad (_("opcode not supported on this processor"));
5263 return;
5264 }
5265 s = "ldc1";
bdaaa2e1 5266 /* Itbl support may require additional care here. */
252b5132
RH
5267 coproc = 1;
5268 goto ld;
5269 case M_LDC2_AB:
5270 s = "ldc2";
bdaaa2e1 5271 /* Itbl support may require additional care here. */
252b5132
RH
5272 coproc = 1;
5273 goto ld;
5274 case M_LDC3_AB:
5275 s = "ldc3";
bdaaa2e1 5276 /* Itbl support may require additional care here. */
252b5132
RH
5277 coproc = 1;
5278 goto ld;
5279 case M_LDL_AB:
5280 s = "ldl";
5281 lr = 1;
5282 goto ld;
5283 case M_LDR_AB:
5284 s = "ldr";
5285 lr = 1;
5286 goto ld;
5287 case M_LL_AB:
5288 s = "ll";
5289 goto ld;
5290 case M_LLD_AB:
5291 s = "lld";
5292 goto ld;
5293 case M_LWU_AB:
5294 s = "lwu";
5295 ld:
5296 if (breg == treg || coproc || lr)
5297 {
5298 tempreg = AT;
5299 used_at = 1;
5300 }
5301 else
5302 {
5303 tempreg = treg;
5304 used_at = 0;
5305 }
5306 goto ld_st;
5307 case M_SB_AB:
5308 s = "sb";
5309 goto st;
5310 case M_SH_AB:
5311 s = "sh";
5312 goto st;
5313 case M_SW_AB:
5314 s = "sw";
5315 goto st;
5316 case M_SWC0_AB:
5317 s = "swc0";
bdaaa2e1 5318 /* Itbl support may require additional care here. */
252b5132
RH
5319 coproc = 1;
5320 goto st;
5321 case M_SWC1_AB:
5322 s = "swc1";
bdaaa2e1 5323 /* Itbl support may require additional care here. */
252b5132
RH
5324 coproc = 1;
5325 goto st;
5326 case M_SWC2_AB:
5327 s = "swc2";
bdaaa2e1 5328 /* Itbl support may require additional care here. */
252b5132
RH
5329 coproc = 1;
5330 goto st;
5331 case M_SWC3_AB:
5332 s = "swc3";
bdaaa2e1 5333 /* Itbl support may require additional care here. */
252b5132
RH
5334 coproc = 1;
5335 goto st;
5336 case M_SWL_AB:
5337 s = "swl";
5338 goto st;
5339 case M_SWR_AB:
5340 s = "swr";
5341 goto st;
5342 case M_SC_AB:
5343 s = "sc";
5344 goto st;
5345 case M_SCD_AB:
5346 s = "scd";
5347 goto st;
5348 case M_SDC1_AB:
ec68c924 5349 if (mips_arch == CPU_R4650)
252b5132
RH
5350 {
5351 as_bad (_("opcode not supported on this processor"));
5352 return;
5353 }
5354 s = "sdc1";
5355 coproc = 1;
bdaaa2e1 5356 /* Itbl support may require additional care here. */
252b5132
RH
5357 goto st;
5358 case M_SDC2_AB:
5359 s = "sdc2";
bdaaa2e1 5360 /* Itbl support may require additional care here. */
252b5132
RH
5361 coproc = 1;
5362 goto st;
5363 case M_SDC3_AB:
5364 s = "sdc3";
bdaaa2e1 5365 /* Itbl support may require additional care here. */
252b5132
RH
5366 coproc = 1;
5367 goto st;
5368 case M_SDL_AB:
5369 s = "sdl";
5370 goto st;
5371 case M_SDR_AB:
5372 s = "sdr";
5373 st:
5374 tempreg = AT;
5375 used_at = 1;
5376 ld_st:
bdaaa2e1 5377 /* Itbl support may require additional care here. */
252b5132
RH
5378 if (mask == M_LWC1_AB
5379 || mask == M_SWC1_AB
5380 || mask == M_LDC1_AB
5381 || mask == M_SDC1_AB
5382 || mask == M_L_DAB
5383 || mask == M_S_DAB)
5384 fmt = "T,o(b)";
5385 else if (coproc)
5386 fmt = "E,o(b)";
5387 else
5388 fmt = "t,o(b)";
5389
afdbd6d0
CD
5390 /* For embedded PIC, we allow loads where the offset is calculated
5391 by subtracting a symbol in the current segment from an unknown
5392 symbol, relative to a base register, e.g.:
5393 <op> $treg, <sym>-<localsym>($breg)
5394 This is used by the compiler for switch statements. */
76b3015f 5395 if (mips_pic == EMBEDDED_PIC
afdbd6d0
CD
5396 && offset_expr.X_op == O_subtract
5397 && (symbol_constant_p (offset_expr.X_op_symbol)
5398 ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
5399 : (symbol_equated_p (offset_expr.X_op_symbol)
5400 && (S_GET_SEGMENT
5401 (symbol_get_value_expression (offset_expr.X_op_symbol)
5402 ->X_add_symbol)
5403 == now_seg)))
5404 && breg != 0
5405 && (offset_expr.X_add_number == 0
5406 || OUTPUT_FLAVOR == bfd_target_elf_flavour))
5407 {
5408 /* For this case, we output the instructions:
5409 lui $tempreg,<sym> (BFD_RELOC_PCREL_HI16_S)
5410 addiu $tempreg,$tempreg,$breg
5411 <op> $treg,<sym>($tempreg) (BFD_RELOC_PCREL_LO16)
5412 If the relocation would fit entirely in 16 bits, it would be
5413 nice to emit:
5414 <op> $treg,<sym>($breg) (BFD_RELOC_PCREL_LO16)
5415 instead, but that seems quite difficult. */
5416 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5417 tempreg, (int) BFD_RELOC_PCREL_HI16_S);
5418 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5419 ((bfd_arch_bits_per_address (stdoutput) == 32
5420 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
5421 ? "addu" : "daddu"),
5422 "d,v,t", tempreg, tempreg, breg);
5423 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt, treg,
5424 (int) BFD_RELOC_PCREL_LO16, tempreg);
5425 if (! used_at)
5426 return;
5427 break;
5428 }
5429
252b5132
RH
5430 if (offset_expr.X_op != O_constant
5431 && offset_expr.X_op != O_symbol)
5432 {
5433 as_bad (_("expression too complex"));
5434 offset_expr.X_op = O_constant;
5435 }
5436
5437 /* A constant expression in PIC code can be handled just as it
5438 is in non PIC code. */
5439 if (mips_pic == NO_PIC
5440 || offset_expr.X_op == O_constant)
5441 {
f9419b05
TS
5442 char *p;
5443
252b5132
RH
5444 /* If this is a reference to a GP relative symbol, and there
5445 is no base register, we want
cdf6fd85 5446 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
5447 Otherwise, if there is no base register, we want
5448 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5449 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5450 If we have a constant, we need two instructions anyhow,
5451 so we always use the latter form.
5452
5453 If we have a base register, and this is a reference to a
5454 GP relative symbol, we want
5455 addu $tempreg,$breg,$gp
cdf6fd85 5456 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
5457 Otherwise we want
5458 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5459 addu $tempreg,$tempreg,$breg
5460 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245 5461 With a constant we always use the latter case.
76b3015f 5462
d6bc6245
TS
5463 With 64bit address space and no base register and $at usable,
5464 we want
5465 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5466 lui $at,<sym> (BFD_RELOC_HI16_S)
5467 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5468 dsll32 $tempreg,0
5469 daddu $tempreg,$at
5470 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5471 If we have a base register, we want
5472 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5473 lui $at,<sym> (BFD_RELOC_HI16_S)
5474 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5475 daddu $at,$breg
5476 dsll32 $tempreg,0
5477 daddu $tempreg,$at
5478 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5479
5480 Without $at we can't generate the optimal path for superscalar
5481 processors here since this would require two temporary registers.
5482 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5483 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5484 dsll $tempreg,16
5485 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5486 dsll $tempreg,16
5487 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5488 If we have a base register, we want
5489 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5490 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5491 dsll $tempreg,16
5492 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5493 dsll $tempreg,16
5494 daddu $tempreg,$tempreg,$breg
5495 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6373ee54
CD
5496
5497 If we have 64-bit addresses, as an optimization, for
5498 addresses which are 32-bit constants (e.g. kseg0/kseg1
5499 addresses) we fall back to the 32-bit address generation
78d32a17
MR
5500 mechanism since it is more efficient. Note that due to
5501 the signed offset used by memory operations, the 32-bit
5502 range is shifted down by 32768 here. This code should
6373ee54
CD
5503 probably attempt to generate 64-bit constants more
5504 efficiently in general.
d6bc6245 5505 */
6373ee54
CD
5506 if (HAVE_64BIT_ADDRESSES
5507 && !(offset_expr.X_op == O_constant
78d32a17 5508 && IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000)))
d6bc6245
TS
5509 {
5510 p = NULL;
5511
5512 /* We don't do GP optimization for now because RELAX_ENCODE can't
5513 hold the data for such large chunks. */
5514
460597ba 5515 if (used_at == 0 && ! mips_opts.noat)
d6bc6245
TS
5516 {
5517 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5518 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5519 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5520 AT, (int) BFD_RELOC_HI16_S);
5521 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5522 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5523 if (breg != 0)
2396cfb9
TS
5524 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5525 "d,v,t", AT, AT, breg);
5526 macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
5527 "d,w,<", tempreg, tempreg, 0);
5528 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5529 "d,v,t", tempreg, tempreg, AT);
d6bc6245
TS
5530 macro_build (p, &icnt, &offset_expr, s,
5531 fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5532 used_at = 1;
5533 }
5534 else
5535 {
5536 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5537 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5538 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5539 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
2396cfb9
TS
5540 macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5541 "d,w,<", tempreg, tempreg, 16);
d6bc6245
TS
5542 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5543 tempreg, tempreg, (int) BFD_RELOC_HI16_S);
2396cfb9
TS
5544 macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5545 "d,w,<", tempreg, tempreg, 16);
d6bc6245 5546 if (breg != 0)
2396cfb9
TS
5547 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5548 "d,v,t", tempreg, tempreg, breg);
d6bc6245
TS
5549 macro_build (p, &icnt, &offset_expr, s,
5550 fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5551 }
5552
5553 return;
5554 }
76b3015f 5555
252b5132
RH
5556 if (breg == 0)
5557 {
e7d556df 5558 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
252b5132
RH
5559 || nopic_need_relax (offset_expr.X_add_symbol, 1))
5560 p = NULL;
5561 else
5562 {
5563 frag_grow (20);
5564 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
c9914766
TS
5565 treg, (int) BFD_RELOC_GPREL16,
5566 mips_gp_register);
252b5132
RH
5567 p = frag_var (rs_machine_dependent, 8, 0,
5568 RELAX_ENCODE (4, 8, 0, 4, 0,
5569 (mips_opts.warn_about_macros
5570 || (used_at
5571 && mips_opts.noat))),
956cd1d6 5572 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5573 used_at = 0;
5574 }
5575 macro_build_lui (p, &icnt, &offset_expr, tempreg);
5576 if (p != NULL)
5577 p += 4;
5578 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5579 (int) BFD_RELOC_LO16, tempreg);
5580 }
5581 else
5582 {
e7d556df 5583 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
252b5132
RH
5584 || nopic_need_relax (offset_expr.X_add_symbol, 1))
5585 p = NULL;
5586 else
5587 {
5588 frag_grow (28);
5589 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5590 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 5591 "d,v,t", tempreg, breg, mips_gp_register);
252b5132 5592 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
cdf6fd85 5593 treg, (int) BFD_RELOC_GPREL16, tempreg);
252b5132
RH
5594 p = frag_var (rs_machine_dependent, 12, 0,
5595 RELAX_ENCODE (8, 12, 0, 8, 0, 0),
956cd1d6 5596 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5597 }
5598 macro_build_lui (p, &icnt, &offset_expr, tempreg);
5599 if (p != NULL)
5600 p += 4;
5601 macro_build (p, &icnt, (expressionS *) NULL,
ca4e0257 5602 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5603 "d,v,t", tempreg, tempreg, breg);
5604 if (p != NULL)
5605 p += 4;
5606 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5607 (int) BFD_RELOC_LO16, tempreg);
5608 }
5609 }
5610 else if (mips_pic == SVR4_PIC && ! mips_big_got)
5611 {
f9419b05
TS
5612 char *p;
5613
252b5132
RH
5614 /* If this is a reference to an external symbol, we want
5615 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5616 nop
5617 <op> $treg,0($tempreg)
5618 Otherwise we want
5619 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5620 nop
5621 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5622 <op> $treg,0($tempreg)
5623 If there is a base register, we add it to $tempreg before
5624 the <op>. If there is a constant, we stick it in the
5625 <op> instruction. We don't handle constants larger than
5626 16 bits, because we have no way to load the upper 16 bits
5627 (actually, we could handle them for the subset of cases
5628 in which we are not using $at). */
5629 assert (offset_expr.X_op == O_symbol);
5630 expr1.X_add_number = offset_expr.X_add_number;
5631 offset_expr.X_add_number = 0;
5632 if (expr1.X_add_number < -0x8000
5633 || expr1.X_add_number >= 0x8000)
5634 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5635 frag_grow (20);
5636 macro_build ((char *) NULL, &icnt, &offset_expr,
c9914766
TS
5637 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", tempreg,
5638 (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132 5639 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
bdaaa2e1 5640 p = frag_var (rs_machine_dependent, 4, 0,
252b5132 5641 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
c4e7957c 5642 offset_expr.X_add_symbol, 0, NULL);
252b5132 5643 macro_build (p, &icnt, &offset_expr,
ca4e0257 5644 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
5645 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5646 if (breg != 0)
5647 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5648 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5649 "d,v,t", tempreg, tempreg, breg);
5650 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5651 (int) BFD_RELOC_LO16, tempreg);
5652 }
5653 else if (mips_pic == SVR4_PIC)
5654 {
5655 int gpdel;
f9419b05 5656 char *p;
252b5132
RH
5657
5658 /* If this is a reference to an external symbol, we want
5659 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5660 addu $tempreg,$tempreg,$gp
5661 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5662 <op> $treg,0($tempreg)
5663 Otherwise we want
5664 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5665 nop
5666 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5667 <op> $treg,0($tempreg)
5668 If there is a base register, we add it to $tempreg before
5669 the <op>. If there is a constant, we stick it in the
5670 <op> instruction. We don't handle constants larger than
5671 16 bits, because we have no way to load the upper 16 bits
5672 (actually, we could handle them for the subset of cases
438c16b8
TS
5673 in which we are not using $at).
5674
5675 For NewABI, we want
5676 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
5677 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
5678 <op> $treg,0($tempreg)
5679 */
252b5132
RH
5680 assert (offset_expr.X_op == O_symbol);
5681 expr1.X_add_number = offset_expr.X_add_number;
5682 offset_expr.X_add_number = 0;
5683 if (expr1.X_add_number < -0x8000
5684 || expr1.X_add_number >= 0x8000)
5685 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
438c16b8
TS
5686 if (HAVE_NEWABI)
5687 {
5688 macro_build ((char *) NULL, &icnt, &offset_expr,
5689 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5690 "t,o(b)", tempreg, BFD_RELOC_MIPS_GOT_PAGE,
5691 mips_gp_register);
5692 macro_build ((char *) NULL, &icnt, &offset_expr,
5693 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5694 "t,r,j", tempreg, tempreg,
5695 BFD_RELOC_MIPS_GOT_OFST);
5696 if (breg != 0)
5697 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5698 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5699 "d,v,t", tempreg, tempreg, breg);
5700 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5701 (int) BFD_RELOC_LO16, tempreg);
5702
5703 if (! used_at)
5704 return;
5705
5706 break;
5707 }
f7ea7ef2 5708 if (reg_needs_delay (mips_gp_register))
252b5132
RH
5709 gpdel = 4;
5710 else
5711 gpdel = 0;
5712 frag_grow (36);
5713 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5714 tempreg, (int) BFD_RELOC_MIPS_GOT_HI16);
5715 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5716 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 5717 "d,v,t", tempreg, tempreg, mips_gp_register);
252b5132 5718 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 5719 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
5720 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_LO16,
5721 tempreg);
5722 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5723 RELAX_ENCODE (12, 12 + gpdel, gpdel, 8 + gpdel, 0, 0),
c4e7957c 5724 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5725 if (gpdel > 0)
5726 {
5727 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5728 p += 4;
5729 }
5730 macro_build (p, &icnt, &offset_expr,
ca4e0257 5731 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
c9914766
TS
5732 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16,
5733 mips_gp_register);
252b5132
RH
5734 p += 4;
5735 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5736 p += 4;
5737 macro_build (p, &icnt, &offset_expr,
ca4e0257 5738 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
5739 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5740 if (breg != 0)
5741 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5742 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5743 "d,v,t", tempreg, tempreg, breg);
5744 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5745 (int) BFD_RELOC_LO16, tempreg);
5746 }
5747 else if (mips_pic == EMBEDDED_PIC)
5748 {
5749 /* If there is no base register, we want
cdf6fd85 5750 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
5751 If there is a base register, we want
5752 addu $tempreg,$breg,$gp
cdf6fd85 5753 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
5754 */
5755 assert (offset_expr.X_op == O_symbol);
5756 if (breg == 0)
5757 {
5758 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
c9914766 5759 treg, (int) BFD_RELOC_GPREL16, mips_gp_register);
252b5132
RH
5760 used_at = 0;
5761 }
5762 else
5763 {
5764 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5765 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 5766 "d,v,t", tempreg, breg, mips_gp_register);
252b5132 5767 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
cdf6fd85 5768 treg, (int) BFD_RELOC_GPREL16, tempreg);
252b5132
RH
5769 }
5770 }
5771 else
5772 abort ();
5773
5774 if (! used_at)
5775 return;
5776
5777 break;
5778
5779 case M_LI:
5780 case M_LI_S:
5781 load_register (&icnt, treg, &imm_expr, 0);
5782 return;
5783
5784 case M_DLI:
5785 load_register (&icnt, treg, &imm_expr, 1);
5786 return;
5787
5788 case M_LI_SS:
5789 if (imm_expr.X_op == O_constant)
5790 {
5791 load_register (&icnt, AT, &imm_expr, 0);
5792 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5793 "mtc1", "t,G", AT, treg);
5794 break;
5795 }
5796 else
5797 {
5798 assert (offset_expr.X_op == O_symbol
5799 && strcmp (segment_name (S_GET_SEGMENT
5800 (offset_expr.X_add_symbol)),
5801 ".lit4") == 0
5802 && offset_expr.X_add_number == 0);
5803 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
c9914766 5804 treg, (int) BFD_RELOC_MIPS_LITERAL, mips_gp_register);
252b5132
RH
5805 return;
5806 }
5807
5808 case M_LI_D:
ca4e0257
RS
5809 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
5810 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
5811 order 32 bits of the value and the low order 32 bits are either
5812 zero or in OFFSET_EXPR. */
252b5132
RH
5813 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
5814 {
ca4e0257 5815 if (HAVE_64BIT_GPRS)
252b5132
RH
5816 load_register (&icnt, treg, &imm_expr, 1);
5817 else
5818 {
5819 int hreg, lreg;
5820
5821 if (target_big_endian)
5822 {
5823 hreg = treg;
5824 lreg = treg + 1;
5825 }
5826 else
5827 {
5828 hreg = treg + 1;
5829 lreg = treg;
5830 }
5831
5832 if (hreg <= 31)
5833 load_register (&icnt, hreg, &imm_expr, 0);
5834 if (lreg <= 31)
5835 {
5836 if (offset_expr.X_op == O_absent)
ea1fb5dc 5837 move_register (&icnt, lreg, 0);
252b5132
RH
5838 else
5839 {
5840 assert (offset_expr.X_op == O_constant);
5841 load_register (&icnt, lreg, &offset_expr, 0);
5842 }
5843 }
5844 }
5845 return;
5846 }
5847
5848 /* We know that sym is in the .rdata section. First we get the
5849 upper 16 bits of the address. */
5850 if (mips_pic == NO_PIC)
5851 {
956cd1d6 5852 macro_build_lui (NULL, &icnt, &offset_expr, AT);
252b5132
RH
5853 }
5854 else if (mips_pic == SVR4_PIC)
5855 {
5856 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 5857 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
c9914766
TS
5858 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
5859 mips_gp_register);
252b5132
RH
5860 }
5861 else if (mips_pic == EMBEDDED_PIC)
5862 {
5863 /* For embedded PIC we pick up the entire address off $gp in
5864 a single instruction. */
5865 macro_build ((char *) NULL, &icnt, &offset_expr,
c9914766
TS
5866 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j", AT,
5867 mips_gp_register, (int) BFD_RELOC_GPREL16);
252b5132
RH
5868 offset_expr.X_op = O_constant;
5869 offset_expr.X_add_number = 0;
5870 }
5871 else
5872 abort ();
bdaaa2e1 5873
252b5132 5874 /* Now we load the register(s). */
ca4e0257 5875 if (HAVE_64BIT_GPRS)
252b5132
RH
5876 macro_build ((char *) NULL, &icnt, &offset_expr, "ld", "t,o(b)",
5877 treg, (int) BFD_RELOC_LO16, AT);
5878 else
5879 {
5880 macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
5881 treg, (int) BFD_RELOC_LO16, AT);
f9419b05 5882 if (treg != RA)
252b5132
RH
5883 {
5884 /* FIXME: How in the world do we deal with the possible
5885 overflow here? */
5886 offset_expr.X_add_number += 4;
5887 macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
5888 treg + 1, (int) BFD_RELOC_LO16, AT);
5889 }
5890 }
5891
5892 /* To avoid confusion in tc_gen_reloc, we must ensure that this
5893 does not become a variant frag. */
5894 frag_wane (frag_now);
5895 frag_new (0);
5896
5897 break;
5898
5899 case M_LI_DD:
ca4e0257
RS
5900 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
5901 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
5902 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
5903 the value and the low order 32 bits are either zero or in
5904 OFFSET_EXPR. */
252b5132
RH
5905 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
5906 {
ca4e0257
RS
5907 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_FPRS);
5908 if (HAVE_64BIT_FPRS)
5909 {
5910 assert (HAVE_64BIT_GPRS);
5911 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5912 "dmtc1", "t,S", AT, treg);
5913 }
252b5132
RH
5914 else
5915 {
5916 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5917 "mtc1", "t,G", AT, treg + 1);
5918 if (offset_expr.X_op == O_absent)
5919 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5920 "mtc1", "t,G", 0, treg);
5921 else
5922 {
5923 assert (offset_expr.X_op == O_constant);
5924 load_register (&icnt, AT, &offset_expr, 0);
5925 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5926 "mtc1", "t,G", AT, treg);
5927 }
5928 }
5929 break;
5930 }
5931
5932 assert (offset_expr.X_op == O_symbol
5933 && offset_expr.X_add_number == 0);
5934 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
5935 if (strcmp (s, ".lit8") == 0)
5936 {
e7af610e 5937 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
5938 {
5939 macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
c9914766
TS
5940 "T,o(b)", treg, (int) BFD_RELOC_MIPS_LITERAL,
5941 mips_gp_register);
252b5132
RH
5942 return;
5943 }
c9914766 5944 breg = mips_gp_register;
252b5132
RH
5945 r = BFD_RELOC_MIPS_LITERAL;
5946 goto dob;
5947 }
5948 else
5949 {
5950 assert (strcmp (s, RDATA_SECTION_NAME) == 0);
5951 if (mips_pic == SVR4_PIC)
5952 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 5953 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
c9914766
TS
5954 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
5955 mips_gp_register);
252b5132
RH
5956 else
5957 {
5958 /* FIXME: This won't work for a 64 bit address. */
956cd1d6 5959 macro_build_lui (NULL, &icnt, &offset_expr, AT);
252b5132 5960 }
bdaaa2e1 5961
e7af610e 5962 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
5963 {
5964 macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
5965 "T,o(b)", treg, (int) BFD_RELOC_LO16, AT);
5966
5967 /* To avoid confusion in tc_gen_reloc, we must ensure
5968 that this does not become a variant frag. */
5969 frag_wane (frag_now);
5970 frag_new (0);
5971
5972 break;
5973 }
5974 breg = AT;
5975 r = BFD_RELOC_LO16;
5976 goto dob;
5977 }
5978
5979 case M_L_DOB:
ec68c924 5980 if (mips_arch == CPU_R4650)
252b5132
RH
5981 {
5982 as_bad (_("opcode not supported on this processor"));
5983 return;
5984 }
5985 /* Even on a big endian machine $fn comes before $fn+1. We have
5986 to adjust when loading from memory. */
5987 r = BFD_RELOC_LO16;
5988 dob:
e7af610e 5989 assert (mips_opts.isa == ISA_MIPS1);
252b5132
RH
5990 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
5991 target_big_endian ? treg + 1 : treg,
5992 (int) r, breg);
5993 /* FIXME: A possible overflow which I don't know how to deal
5994 with. */
5995 offset_expr.X_add_number += 4;
5996 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
5997 target_big_endian ? treg : treg + 1,
5998 (int) r, breg);
5999
6000 /* To avoid confusion in tc_gen_reloc, we must ensure that this
6001 does not become a variant frag. */
6002 frag_wane (frag_now);
6003 frag_new (0);
6004
6005 if (breg != AT)
6006 return;
6007 break;
6008
6009 case M_L_DAB:
6010 /*
6011 * The MIPS assembler seems to check for X_add_number not
6012 * being double aligned and generating:
6013 * lui at,%hi(foo+1)
6014 * addu at,at,v1
6015 * addiu at,at,%lo(foo+1)
6016 * lwc1 f2,0(at)
6017 * lwc1 f3,4(at)
6018 * But, the resulting address is the same after relocation so why
6019 * generate the extra instruction?
6020 */
ec68c924 6021 if (mips_arch == CPU_R4650)
252b5132
RH
6022 {
6023 as_bad (_("opcode not supported on this processor"));
6024 return;
6025 }
bdaaa2e1 6026 /* Itbl support may require additional care here. */
252b5132 6027 coproc = 1;
e7af610e 6028 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
6029 {
6030 s = "ldc1";
6031 goto ld;
6032 }
6033
6034 s = "lwc1";
6035 fmt = "T,o(b)";
6036 goto ldd_std;
6037
6038 case M_S_DAB:
ec68c924 6039 if (mips_arch == CPU_R4650)
252b5132
RH
6040 {
6041 as_bad (_("opcode not supported on this processor"));
6042 return;
6043 }
6044
e7af610e 6045 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
6046 {
6047 s = "sdc1";
6048 goto st;
6049 }
6050
6051 s = "swc1";
6052 fmt = "T,o(b)";
bdaaa2e1 6053 /* Itbl support may require additional care here. */
252b5132
RH
6054 coproc = 1;
6055 goto ldd_std;
6056
6057 case M_LD_AB:
ca4e0257 6058 if (HAVE_64BIT_GPRS)
252b5132
RH
6059 {
6060 s = "ld";
6061 goto ld;
6062 }
6063
6064 s = "lw";
6065 fmt = "t,o(b)";
6066 goto ldd_std;
6067
6068 case M_SD_AB:
ca4e0257 6069 if (HAVE_64BIT_GPRS)
252b5132
RH
6070 {
6071 s = "sd";
6072 goto st;
6073 }
6074
6075 s = "sw";
6076 fmt = "t,o(b)";
6077
6078 ldd_std:
afdbd6d0
CD
6079 /* We do _not_ bother to allow embedded PIC (symbol-local_symbol)
6080 loads for the case of doing a pair of loads to simulate an 'ld'.
6081 This is not currently done by the compiler, and assembly coders
6082 writing embedded-pic code can cope. */
6083
252b5132
RH
6084 if (offset_expr.X_op != O_symbol
6085 && offset_expr.X_op != O_constant)
6086 {
6087 as_bad (_("expression too complex"));
6088 offset_expr.X_op = O_constant;
6089 }
6090
6091 /* Even on a big endian machine $fn comes before $fn+1. We have
6092 to adjust when loading from memory. We set coproc if we must
6093 load $fn+1 first. */
bdaaa2e1 6094 /* Itbl support may require additional care here. */
252b5132
RH
6095 if (! target_big_endian)
6096 coproc = 0;
6097
6098 if (mips_pic == NO_PIC
6099 || offset_expr.X_op == O_constant)
6100 {
f9419b05
TS
6101 char *p;
6102
252b5132 6103 /* If this is a reference to a GP relative symbol, we want
cdf6fd85
TS
6104 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6105 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
6106 If we have a base register, we use this
6107 addu $at,$breg,$gp
cdf6fd85
TS
6108 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6109 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
6110 If this is not a GP relative symbol, we want
6111 lui $at,<sym> (BFD_RELOC_HI16_S)
6112 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6113 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6114 If there is a base register, we add it to $at after the
6115 lui instruction. If there is a constant, we always use
6116 the last case. */
e7d556df 6117 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
252b5132
RH
6118 || nopic_need_relax (offset_expr.X_add_symbol, 1))
6119 {
6120 p = NULL;
6121 used_at = 1;
6122 }
6123 else
6124 {
6125 int off;
6126
6127 if (breg == 0)
6128 {
6129 frag_grow (28);
c9914766 6130 tempreg = mips_gp_register;
252b5132
RH
6131 off = 0;
6132 used_at = 0;
6133 }
6134 else
6135 {
6136 frag_grow (36);
6137 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6138 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 6139 "d,v,t", AT, breg, mips_gp_register);
252b5132
RH
6140 tempreg = AT;
6141 off = 4;
6142 used_at = 1;
6143 }
6144
beae10d5 6145 /* Itbl support may require additional care here. */
252b5132
RH
6146 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6147 coproc ? treg + 1 : treg,
cdf6fd85 6148 (int) BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6149 offset_expr.X_add_number += 4;
6150
6151 /* Set mips_optimize to 2 to avoid inserting an
6152 undesired nop. */
6153 hold_mips_optimize = mips_optimize;
6154 mips_optimize = 2;
beae10d5 6155 /* Itbl support may require additional care here. */
252b5132
RH
6156 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6157 coproc ? treg : treg + 1,
cdf6fd85 6158 (int) BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6159 mips_optimize = hold_mips_optimize;
6160
6161 p = frag_var (rs_machine_dependent, 12 + off, 0,
6162 RELAX_ENCODE (8 + off, 12 + off, 0, 4 + off, 1,
6163 used_at && mips_opts.noat),
956cd1d6 6164 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
6165
6166 /* We just generated two relocs. When tc_gen_reloc
6167 handles this case, it will skip the first reloc and
6168 handle the second. The second reloc already has an
6169 extra addend of 4, which we added above. We must
6170 subtract it out, and then subtract another 4 to make
6171 the first reloc come out right. The second reloc
6172 will come out right because we are going to add 4 to
6173 offset_expr when we build its instruction below.
6174
6175 If we have a symbol, then we don't want to include
6176 the offset, because it will wind up being included
6177 when we generate the reloc. */
6178
6179 if (offset_expr.X_op == O_constant)
6180 offset_expr.X_add_number -= 8;
6181 else
6182 {
6183 offset_expr.X_add_number = -4;
6184 offset_expr.X_op = O_constant;
6185 }
6186 }
6187 macro_build_lui (p, &icnt, &offset_expr, AT);
6188 if (p != NULL)
6189 p += 4;
6190 if (breg != 0)
6191 {
6192 macro_build (p, &icnt, (expressionS *) NULL,
ca4e0257 6193 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
6194 "d,v,t", AT, breg, AT);
6195 if (p != NULL)
6196 p += 4;
6197 }
beae10d5 6198 /* Itbl support may require additional care here. */
252b5132
RH
6199 macro_build (p, &icnt, &offset_expr, s, fmt,
6200 coproc ? treg + 1 : treg,
6201 (int) BFD_RELOC_LO16, AT);
6202 if (p != NULL)
6203 p += 4;
6204 /* FIXME: How do we handle overflow here? */
6205 offset_expr.X_add_number += 4;
beae10d5 6206 /* Itbl support may require additional care here. */
252b5132
RH
6207 macro_build (p, &icnt, &offset_expr, s, fmt,
6208 coproc ? treg : treg + 1,
6209 (int) BFD_RELOC_LO16, AT);
bdaaa2e1 6210 }
252b5132
RH
6211 else if (mips_pic == SVR4_PIC && ! mips_big_got)
6212 {
6213 int off;
6214
6215 /* If this is a reference to an external symbol, we want
6216 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6217 nop
6218 <op> $treg,0($at)
6219 <op> $treg+1,4($at)
6220 Otherwise we want
6221 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6222 nop
6223 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6224 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6225 If there is a base register we add it to $at before the
6226 lwc1 instructions. If there is a constant we include it
6227 in the lwc1 instructions. */
6228 used_at = 1;
6229 expr1.X_add_number = offset_expr.X_add_number;
6230 offset_expr.X_add_number = 0;
6231 if (expr1.X_add_number < -0x8000
6232 || expr1.X_add_number >= 0x8000 - 4)
6233 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6234 if (breg == 0)
6235 off = 0;
6236 else
6237 off = 4;
6238 frag_grow (24 + off);
6239 macro_build ((char *) NULL, &icnt, &offset_expr,
c9914766
TS
6240 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", AT,
6241 (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132
RH
6242 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6243 if (breg != 0)
6244 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6245 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132 6246 "d,v,t", AT, breg, AT);
beae10d5 6247 /* Itbl support may require additional care here. */
252b5132
RH
6248 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6249 coproc ? treg + 1 : treg,
6250 (int) BFD_RELOC_LO16, AT);
6251 expr1.X_add_number += 4;
6252
6253 /* Set mips_optimize to 2 to avoid inserting an undesired
6254 nop. */
6255 hold_mips_optimize = mips_optimize;
6256 mips_optimize = 2;
beae10d5 6257 /* Itbl support may require additional care here. */
252b5132
RH
6258 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6259 coproc ? treg : treg + 1,
6260 (int) BFD_RELOC_LO16, AT);
6261 mips_optimize = hold_mips_optimize;
6262
6263 (void) frag_var (rs_machine_dependent, 0, 0,
6264 RELAX_ENCODE (0, 0, -16 - off, -8, 1, 0),
c4e7957c 6265 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
6266 }
6267 else if (mips_pic == SVR4_PIC)
6268 {
6269 int gpdel, off;
f9419b05 6270 char *p;
252b5132
RH
6271
6272 /* If this is a reference to an external symbol, we want
6273 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6274 addu $at,$at,$gp
6275 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
6276 nop
6277 <op> $treg,0($at)
6278 <op> $treg+1,4($at)
6279 Otherwise we want
6280 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6281 nop
6282 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6283 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6284 If there is a base register we add it to $at before the
6285 lwc1 instructions. If there is a constant we include it
6286 in the lwc1 instructions. */
6287 used_at = 1;
6288 expr1.X_add_number = offset_expr.X_add_number;
6289 offset_expr.X_add_number = 0;
6290 if (expr1.X_add_number < -0x8000
6291 || expr1.X_add_number >= 0x8000 - 4)
6292 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
c9914766 6293 if (reg_needs_delay (mips_gp_register))
252b5132
RH
6294 gpdel = 4;
6295 else
6296 gpdel = 0;
6297 if (breg == 0)
6298 off = 0;
6299 else
6300 off = 4;
6301 frag_grow (56);
6302 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
6303 AT, (int) BFD_RELOC_MIPS_GOT_HI16);
6304 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6305 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 6306 "d,v,t", AT, AT, mips_gp_register);
252b5132 6307 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 6308 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
6309 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT_LO16, AT);
6310 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6311 if (breg != 0)
6312 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6313 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132 6314 "d,v,t", AT, breg, AT);
beae10d5 6315 /* Itbl support may require additional care here. */
252b5132
RH
6316 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6317 coproc ? treg + 1 : treg,
6318 (int) BFD_RELOC_LO16, AT);
6319 expr1.X_add_number += 4;
6320
6321 /* Set mips_optimize to 2 to avoid inserting an undesired
6322 nop. */
6323 hold_mips_optimize = mips_optimize;
6324 mips_optimize = 2;
beae10d5 6325 /* Itbl support may require additional care here. */
252b5132
RH
6326 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6327 coproc ? treg : treg + 1,
6328 (int) BFD_RELOC_LO16, AT);
6329 mips_optimize = hold_mips_optimize;
6330 expr1.X_add_number -= 4;
6331
6332 p = frag_var (rs_machine_dependent, 16 + gpdel + off, 0,
6333 RELAX_ENCODE (24 + off, 16 + gpdel + off, gpdel,
6334 8 + gpdel + off, 1, 0),
c4e7957c 6335 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
6336 if (gpdel > 0)
6337 {
6338 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6339 p += 4;
6340 }
6341 macro_build (p, &icnt, &offset_expr,
ca4e0257 6342 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
c9914766
TS
6343 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6344 mips_gp_register);
252b5132
RH
6345 p += 4;
6346 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6347 p += 4;
6348 if (breg != 0)
6349 {
6350 macro_build (p, &icnt, (expressionS *) NULL,
ca4e0257 6351 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
6352 "d,v,t", AT, breg, AT);
6353 p += 4;
6354 }
beae10d5 6355 /* Itbl support may require additional care here. */
252b5132
RH
6356 macro_build (p, &icnt, &expr1, s, fmt,
6357 coproc ? treg + 1 : treg,
6358 (int) BFD_RELOC_LO16, AT);
6359 p += 4;
6360 expr1.X_add_number += 4;
6361
6362 /* Set mips_optimize to 2 to avoid inserting an undesired
6363 nop. */
6364 hold_mips_optimize = mips_optimize;
6365 mips_optimize = 2;
beae10d5 6366 /* Itbl support may require additional care here. */
252b5132
RH
6367 macro_build (p, &icnt, &expr1, s, fmt,
6368 coproc ? treg : treg + 1,
6369 (int) BFD_RELOC_LO16, AT);
6370 mips_optimize = hold_mips_optimize;
6371 }
6372 else if (mips_pic == EMBEDDED_PIC)
6373 {
6374 /* If there is no base register, we use
cdf6fd85
TS
6375 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6376 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
6377 If we have a base register, we use
6378 addu $at,$breg,$gp
cdf6fd85
TS
6379 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6380 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
6381 */
6382 if (breg == 0)
6383 {
c9914766 6384 tempreg = mips_gp_register;
252b5132
RH
6385 used_at = 0;
6386 }
6387 else
6388 {
6389 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6390 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 6391 "d,v,t", AT, breg, mips_gp_register);
252b5132
RH
6392 tempreg = AT;
6393 used_at = 1;
6394 }
6395
beae10d5 6396 /* Itbl support may require additional care here. */
252b5132
RH
6397 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6398 coproc ? treg + 1 : treg,
cdf6fd85 6399 (int) BFD_RELOC_GPREL16, tempreg);
252b5132 6400 offset_expr.X_add_number += 4;
beae10d5 6401 /* Itbl support may require additional care here. */
252b5132
RH
6402 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6403 coproc ? treg : treg + 1,
cdf6fd85 6404 (int) BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6405 }
6406 else
6407 abort ();
6408
6409 if (! used_at)
6410 return;
6411
6412 break;
6413
6414 case M_LD_OB:
6415 s = "lw";
6416 goto sd_ob;
6417 case M_SD_OB:
6418 s = "sw";
6419 sd_ob:
ca4e0257 6420 assert (HAVE_32BIT_ADDRESSES);
252b5132
RH
6421 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
6422 (int) BFD_RELOC_LO16, breg);
6423 offset_expr.X_add_number += 4;
6424 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg + 1,
6425 (int) BFD_RELOC_LO16, breg);
6426 return;
6427
6428 /* New code added to support COPZ instructions.
6429 This code builds table entries out of the macros in mip_opcodes.
6430 R4000 uses interlocks to handle coproc delays.
6431 Other chips (like the R3000) require nops to be inserted for delays.
6432
f72c8c98 6433 FIXME: Currently, we require that the user handle delays.
252b5132
RH
6434 In order to fill delay slots for non-interlocked chips,
6435 we must have a way to specify delays based on the coprocessor.
6436 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
6437 What are the side-effects of the cop instruction?
6438 What cache support might we have and what are its effects?
6439 Both coprocessor & memory require delays. how long???
bdaaa2e1 6440 What registers are read/set/modified?
252b5132
RH
6441
6442 If an itbl is provided to interpret cop instructions,
bdaaa2e1 6443 this knowledge can be encoded in the itbl spec. */
252b5132
RH
6444
6445 case M_COP0:
6446 s = "c0";
6447 goto copz;
6448 case M_COP1:
6449 s = "c1";
6450 goto copz;
6451 case M_COP2:
6452 s = "c2";
6453 goto copz;
6454 case M_COP3:
6455 s = "c3";
6456 copz:
6457 /* For now we just do C (same as Cz). The parameter will be
6458 stored in insn_opcode by mips_ip. */
6459 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "C",
6460 ip->insn_opcode);
6461 return;
6462
ea1fb5dc
RS
6463 case M_MOVE:
6464 move_register (&icnt, dreg, sreg);
6465 return;
6466
252b5132
RH
6467#ifdef LOSING_COMPILER
6468 default:
6469 /* Try and see if this is a new itbl instruction.
6470 This code builds table entries out of the macros in mip_opcodes.
6471 FIXME: For now we just assemble the expression and pass it's
6472 value along as a 32-bit immediate.
bdaaa2e1 6473 We may want to have the assembler assemble this value,
252b5132
RH
6474 so that we gain the assembler's knowledge of delay slots,
6475 symbols, etc.
6476 Would it be more efficient to use mask (id) here? */
bdaaa2e1 6477 if (itbl_have_entries
252b5132 6478 && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
beae10d5 6479 {
252b5132
RH
6480 s = ip->insn_mo->name;
6481 s2 = "cop3";
6482 coproc = ITBL_DECODE_PNUM (immed_expr);;
6483 macro_build ((char *) NULL, &icnt, &immed_expr, s, "C");
6484 return;
beae10d5 6485 }
252b5132
RH
6486 macro2 (ip);
6487 return;
6488 }
6489 if (mips_opts.noat)
6490 as_warn (_("Macro used $at after \".set noat\""));
6491}
bdaaa2e1 6492
252b5132
RH
6493static void
6494macro2 (ip)
6495 struct mips_cl_insn *ip;
6496{
6497 register int treg, sreg, dreg, breg;
6498 int tempreg;
6499 int mask;
6500 int icnt = 0;
6501 int used_at;
6502 expressionS expr1;
6503 const char *s;
6504 const char *s2;
6505 const char *fmt;
6506 int likely = 0;
6507 int dbl = 0;
6508 int coproc = 0;
6509 int lr = 0;
6510 int imm = 0;
6511 int off;
6512 offsetT maxnum;
6513 bfd_reloc_code_real_type r;
6514 char *p;
bdaaa2e1 6515
252b5132
RH
6516 treg = (ip->insn_opcode >> 16) & 0x1f;
6517 dreg = (ip->insn_opcode >> 11) & 0x1f;
6518 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
6519 mask = ip->insn_mo->mask;
bdaaa2e1 6520
252b5132
RH
6521 expr1.X_op = O_constant;
6522 expr1.X_op_symbol = NULL;
6523 expr1.X_add_symbol = NULL;
6524 expr1.X_add_number = 1;
bdaaa2e1 6525
252b5132
RH
6526 switch (mask)
6527 {
6528#endif /* LOSING_COMPILER */
6529
6530 case M_DMUL:
6531 dbl = 1;
6532 case M_MUL:
2396cfb9
TS
6533 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6534 dbl ? "dmultu" : "multu", "s,t", sreg, treg);
6535 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6536 dreg);
252b5132
RH
6537 return;
6538
6539 case M_DMUL_I:
6540 dbl = 1;
6541 case M_MUL_I:
6542 /* The MIPS assembler some times generates shifts and adds. I'm
6543 not trying to be that fancy. GCC should do this for us
6544 anyway. */
6545 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9 6546 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 6547 dbl ? "dmult" : "mult", "s,t", sreg, AT);
2396cfb9
TS
6548 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6549 dreg);
252b5132
RH
6550 break;
6551
6552 case M_DMULO_I:
6553 dbl = 1;
6554 case M_MULO_I:
6555 imm = 1;
6556 goto do_mulo;
6557
6558 case M_DMULO:
6559 dbl = 1;
6560 case M_MULO:
6561 do_mulo:
6562 mips_emit_delays (true);
6563 ++mips_opts.noreorder;
6564 mips_any_noreorder = 1;
6565 if (imm)
6566 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9 6567 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 6568 dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
2396cfb9
TS
6569 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6570 dreg);
6571 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
f9419b05 6572 dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, RA);
2396cfb9
TS
6573 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6574 AT);
252b5132 6575 if (mips_trap)
9bd7d936
MR
6576 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne",
6577 "s,t,q", dreg, AT, 6);
252b5132
RH
6578 else
6579 {
6580 expr1.X_add_number = 8;
2396cfb9
TS
6581 macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", dreg,
6582 AT);
6583 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6584 0);
6585 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6586 "c", 6);
252b5132
RH
6587 }
6588 --mips_opts.noreorder;
2396cfb9 6589 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d", dreg);
252b5132
RH
6590 break;
6591
6592 case M_DMULOU_I:
6593 dbl = 1;
6594 case M_MULOU_I:
6595 imm = 1;
6596 goto do_mulou;
6597
6598 case M_DMULOU:
6599 dbl = 1;
6600 case M_MULOU:
6601 do_mulou:
6602 mips_emit_delays (true);
6603 ++mips_opts.noreorder;
6604 mips_any_noreorder = 1;
6605 if (imm)
6606 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9 6607 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
252b5132
RH
6608 dbl ? "dmultu" : "multu",
6609 "s,t", sreg, imm ? AT : treg);
2396cfb9
TS
6610 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6611 AT);
6612 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6613 dreg);
252b5132 6614 if (mips_trap)
9bd7d936
MR
6615 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne",
6616 "s,t,q", AT, 0, 6);
252b5132
RH
6617 else
6618 {
6619 expr1.X_add_number = 8;
6620 macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", AT, 0);
2396cfb9
TS
6621 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6622 0);
6623 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6624 "c", 6);
252b5132
RH
6625 }
6626 --mips_opts.noreorder;
6627 break;
6628
771c7ce4
TS
6629 case M_DROL:
6630 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
6631 "d,v,t", AT, 0, treg);
6632 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
6633 "d,t,s", AT, sreg, AT);
6634 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
6635 "d,t,s", dreg, sreg, treg);
6636 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6637 "d,v,t", dreg, dreg, AT);
6638 break;
6639
252b5132 6640 case M_ROL:
2396cfb9
TS
6641 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
6642 "d,v,t", AT, 0, treg);
6643 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
6644 "d,t,s", AT, sreg, AT);
6645 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
6646 "d,t,s", dreg, sreg, treg);
6647 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6648 "d,v,t", dreg, dreg, AT);
252b5132
RH
6649 break;
6650
771c7ce4
TS
6651 case M_DROL_I:
6652 {
6653 unsigned int rot;
6654 char *l, *r;
6655
6656 if (imm_expr.X_op != O_constant)
6657 as_bad (_("rotate count too large"));
6658 rot = imm_expr.X_add_number & 0x3f;
6659 if (! rot)
6660 break;
6661 l = (rot < 0x20) ? "dsll" : "dsll32";
6662 r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
6663 rot &= 0x1f;
6664 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
6665 "d,w,<", AT, sreg, rot);
6666 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
6667 "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6668 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6669 "d,v,t", dreg, dreg, AT);
6670 }
6671 break;
6672
252b5132 6673 case M_ROL_I:
771c7ce4
TS
6674 {
6675 unsigned int rot;
6676
6677 if (imm_expr.X_op != O_constant)
6678 as_bad (_("rotate count too large"));
6679 rot = imm_expr.X_add_number & 0x1f;
6680 if (! rot)
6681 break;
6682 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
6683 "d,w,<", AT, sreg, rot);
6684 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
6685 "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6686 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6687 "d,v,t", dreg, dreg, AT);
6688 }
6689 break;
6690
6691 case M_DROR:
6692 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
6693 "d,v,t", AT, 0, treg);
6694 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
6695 "d,t,s", AT, sreg, AT);
6696 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
6697 "d,t,s", dreg, sreg, treg);
6698 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6699 "d,v,t", dreg, dreg, AT);
252b5132
RH
6700 break;
6701
6702 case M_ROR:
2396cfb9
TS
6703 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
6704 "d,v,t", AT, 0, treg);
6705 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
6706 "d,t,s", AT, sreg, AT);
6707 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
6708 "d,t,s", dreg, sreg, treg);
6709 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6710 "d,v,t", dreg, dreg, AT);
252b5132
RH
6711 break;
6712
771c7ce4
TS
6713 case M_DROR_I:
6714 {
6715 unsigned int rot;
6716 char *l, *r;
6717
6718 if (imm_expr.X_op != O_constant)
6719 as_bad (_("rotate count too large"));
6720 rot = imm_expr.X_add_number & 0x3f;
6721 if (! rot)
6722 break;
6723 r = (rot < 0x20) ? "dsrl" : "dsrl32";
6724 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
6725 rot &= 0x1f;
6726 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
6727 "d,w,<", AT, sreg, rot);
6728 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
6729 "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6730 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6731 "d,v,t", dreg, dreg, AT);
6732 }
6733 break;
6734
252b5132 6735 case M_ROR_I:
771c7ce4
TS
6736 {
6737 unsigned int rot;
6738
6739 if (imm_expr.X_op != O_constant)
6740 as_bad (_("rotate count too large"));
6741 rot = imm_expr.X_add_number & 0x1f;
6742 if (! rot)
6743 break;
6744 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
6745 "d,w,<", AT, sreg, rot);
6746 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
6747 "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6748 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6749 "d,v,t", dreg, dreg, AT);
6750 }
252b5132
RH
6751 break;
6752
6753 case M_S_DOB:
ec68c924 6754 if (mips_arch == CPU_R4650)
252b5132
RH
6755 {
6756 as_bad (_("opcode not supported on this processor"));
6757 return;
6758 }
e7af610e 6759 assert (mips_opts.isa == ISA_MIPS1);
252b5132
RH
6760 /* Even on a big endian machine $fn comes before $fn+1. We have
6761 to adjust when storing to memory. */
6762 macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
6763 target_big_endian ? treg + 1 : treg,
6764 (int) BFD_RELOC_LO16, breg);
6765 offset_expr.X_add_number += 4;
6766 macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
6767 target_big_endian ? treg : treg + 1,
6768 (int) BFD_RELOC_LO16, breg);
6769 return;
6770
6771 case M_SEQ:
6772 if (sreg == 0)
6773 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6774 treg, (int) BFD_RELOC_LO16);
6775 else if (treg == 0)
6776 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6777 sreg, (int) BFD_RELOC_LO16);
6778 else
6779 {
2396cfb9
TS
6780 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6781 "d,v,t", dreg, sreg, treg);
252b5132
RH
6782 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6783 dreg, (int) BFD_RELOC_LO16);
6784 }
6785 return;
6786
6787 case M_SEQ_I:
6788 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6789 {
6790 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6791 sreg, (int) BFD_RELOC_LO16);
6792 return;
6793 }
6794 if (sreg == 0)
6795 {
6796 as_warn (_("Instruction %s: result is always false"),
6797 ip->insn_mo->name);
ea1fb5dc 6798 move_register (&icnt, dreg, 0);
252b5132
RH
6799 return;
6800 }
6801 if (imm_expr.X_op == O_constant
6802 && imm_expr.X_add_number >= 0
6803 && imm_expr.X_add_number < 0x10000)
6804 {
6805 macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg,
6806 sreg, (int) BFD_RELOC_LO16);
6807 used_at = 0;
6808 }
6809 else if (imm_expr.X_op == O_constant
6810 && imm_expr.X_add_number > -0x8000
6811 && imm_expr.X_add_number < 0)
6812 {
6813 imm_expr.X_add_number = -imm_expr.X_add_number;
6814 macro_build ((char *) NULL, &icnt, &imm_expr,
ca4e0257 6815 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
252b5132
RH
6816 "t,r,j", dreg, sreg,
6817 (int) BFD_RELOC_LO16);
6818 used_at = 0;
6819 }
6820 else
6821 {
4d34fb5f 6822 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
6823 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6824 "d,v,t", dreg, sreg, AT);
252b5132
RH
6825 used_at = 1;
6826 }
6827 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
6828 (int) BFD_RELOC_LO16);
6829 if (used_at)
6830 break;
6831 return;
6832
6833 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
6834 s = "slt";
6835 goto sge;
6836 case M_SGEU:
6837 s = "sltu";
6838 sge:
2396cfb9
TS
6839 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6840 dreg, sreg, treg);
252b5132
RH
6841 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6842 (int) BFD_RELOC_LO16);
6843 return;
6844
6845 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
6846 case M_SGEU_I:
6847 if (imm_expr.X_op == O_constant
6848 && imm_expr.X_add_number >= -0x8000
6849 && imm_expr.X_add_number < 0x8000)
6850 {
6851 macro_build ((char *) NULL, &icnt, &imm_expr,
6852 mask == M_SGE_I ? "slti" : "sltiu",
6853 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6854 used_at = 0;
6855 }
6856 else
6857 {
4d34fb5f 6858 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
6859 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6860 mask == M_SGE_I ? "slt" : "sltu", "d,v,t", dreg, sreg,
6861 AT);
252b5132
RH
6862 used_at = 1;
6863 }
6864 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6865 (int) BFD_RELOC_LO16);
6866 if (used_at)
6867 break;
6868 return;
6869
6870 case M_SGT: /* sreg > treg <==> treg < sreg */
6871 s = "slt";
6872 goto sgt;
6873 case M_SGTU:
6874 s = "sltu";
6875 sgt:
2396cfb9
TS
6876 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6877 dreg, treg, sreg);
252b5132
RH
6878 return;
6879
6880 case M_SGT_I: /* sreg > I <==> I < sreg */
6881 s = "slt";
6882 goto sgti;
6883 case M_SGTU_I:
6884 s = "sltu";
6885 sgti:
4d34fb5f 6886 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
6887 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6888 dreg, AT, sreg);
252b5132
RH
6889 break;
6890
2396cfb9 6891 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
252b5132
RH
6892 s = "slt";
6893 goto sle;
6894 case M_SLEU:
6895 s = "sltu";
6896 sle:
2396cfb9
TS
6897 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6898 dreg, treg, sreg);
252b5132
RH
6899 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6900 (int) BFD_RELOC_LO16);
6901 return;
6902
2396cfb9 6903 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
252b5132
RH
6904 s = "slt";
6905 goto slei;
6906 case M_SLEU_I:
6907 s = "sltu";
6908 slei:
4d34fb5f 6909 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
6910 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6911 dreg, AT, sreg);
252b5132
RH
6912 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6913 (int) BFD_RELOC_LO16);
6914 break;
6915
6916 case M_SLT_I:
6917 if (imm_expr.X_op == O_constant
6918 && imm_expr.X_add_number >= -0x8000
6919 && imm_expr.X_add_number < 0x8000)
6920 {
6921 macro_build ((char *) NULL, &icnt, &imm_expr, "slti", "t,r,j",
6922 dreg, sreg, (int) BFD_RELOC_LO16);
6923 return;
6924 }
4d34fb5f 6925 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
6926 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
6927 dreg, sreg, AT);
252b5132
RH
6928 break;
6929
6930 case M_SLTU_I:
6931 if (imm_expr.X_op == O_constant
6932 && imm_expr.X_add_number >= -0x8000
6933 && imm_expr.X_add_number < 0x8000)
6934 {
6935 macro_build ((char *) NULL, &icnt, &imm_expr, "sltiu", "t,r,j",
6936 dreg, sreg, (int) BFD_RELOC_LO16);
6937 return;
6938 }
4d34fb5f 6939 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
6940 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6941 "d,v,t", dreg, sreg, AT);
252b5132
RH
6942 break;
6943
6944 case M_SNE:
6945 if (sreg == 0)
2396cfb9
TS
6946 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6947 "d,v,t", dreg, 0, treg);
252b5132 6948 else if (treg == 0)
2396cfb9
TS
6949 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6950 "d,v,t", dreg, 0, sreg);
252b5132
RH
6951 else
6952 {
2396cfb9
TS
6953 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6954 "d,v,t", dreg, sreg, treg);
6955 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6956 "d,v,t", dreg, 0, dreg);
252b5132
RH
6957 }
6958 return;
6959
6960 case M_SNE_I:
6961 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6962 {
2396cfb9
TS
6963 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6964 "d,v,t", dreg, 0, sreg);
252b5132
RH
6965 return;
6966 }
6967 if (sreg == 0)
6968 {
6969 as_warn (_("Instruction %s: result is always true"),
6970 ip->insn_mo->name);
6971 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 6972 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
252b5132
RH
6973 "t,r,j", dreg, 0, (int) BFD_RELOC_LO16);
6974 return;
6975 }
6976 if (imm_expr.X_op == O_constant
6977 && imm_expr.X_add_number >= 0
6978 && imm_expr.X_add_number < 0x10000)
6979 {
6980 macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i",
6981 dreg, sreg, (int) BFD_RELOC_LO16);
6982 used_at = 0;
6983 }
6984 else if (imm_expr.X_op == O_constant
6985 && imm_expr.X_add_number > -0x8000
6986 && imm_expr.X_add_number < 0)
6987 {
6988 imm_expr.X_add_number = -imm_expr.X_add_number;
6989 macro_build ((char *) NULL, &icnt, &imm_expr,
ca4e0257 6990 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
252b5132
RH
6991 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6992 used_at = 0;
6993 }
6994 else
6995 {
4d34fb5f 6996 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
6997 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6998 "d,v,t", dreg, sreg, AT);
252b5132
RH
6999 used_at = 1;
7000 }
2396cfb9
TS
7001 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7002 "d,v,t", dreg, 0, dreg);
252b5132
RH
7003 if (used_at)
7004 break;
7005 return;
7006
7007 case M_DSUB_I:
7008 dbl = 1;
7009 case M_SUB_I:
7010 if (imm_expr.X_op == O_constant
7011 && imm_expr.X_add_number > -0x8000
7012 && imm_expr.X_add_number <= 0x8000)
7013 {
7014 imm_expr.X_add_number = -imm_expr.X_add_number;
7015 macro_build ((char *) NULL, &icnt, &imm_expr,
7016 dbl ? "daddi" : "addi",
7017 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7018 return;
7019 }
7020 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9 7021 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 7022 dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
252b5132
RH
7023 break;
7024
7025 case M_DSUBU_I:
7026 dbl = 1;
7027 case M_SUBU_I:
7028 if (imm_expr.X_op == O_constant
7029 && imm_expr.X_add_number > -0x8000
7030 && imm_expr.X_add_number <= 0x8000)
7031 {
7032 imm_expr.X_add_number = -imm_expr.X_add_number;
7033 macro_build ((char *) NULL, &icnt, &imm_expr,
7034 dbl ? "daddiu" : "addiu",
7035 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7036 return;
7037 }
7038 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9 7039 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 7040 dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
252b5132
RH
7041 break;
7042
7043 case M_TEQ_I:
7044 s = "teq";
7045 goto trap;
7046 case M_TGE_I:
7047 s = "tge";
7048 goto trap;
7049 case M_TGEU_I:
7050 s = "tgeu";
7051 goto trap;
7052 case M_TLT_I:
7053 s = "tlt";
7054 goto trap;
7055 case M_TLTU_I:
7056 s = "tltu";
7057 goto trap;
7058 case M_TNE_I:
7059 s = "tne";
7060 trap:
4d34fb5f 7061 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
7062 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "s,t", sreg,
7063 AT);
252b5132
RH
7064 break;
7065
252b5132 7066 case M_TRUNCWS:
43841e91 7067 case M_TRUNCWD:
e7af610e 7068 assert (mips_opts.isa == ISA_MIPS1);
252b5132
RH
7069 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
7070 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
7071
7072 /*
7073 * Is the double cfc1 instruction a bug in the mips assembler;
7074 * or is there a reason for it?
7075 */
7076 mips_emit_delays (true);
7077 ++mips_opts.noreorder;
7078 mips_any_noreorder = 1;
2396cfb9 7079 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
f9419b05 7080 treg, RA);
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, "nop", "");
252b5132
RH
7084 expr1.X_add_number = 3;
7085 macro_build ((char *) NULL, &icnt, &expr1, "ori", "t,r,i", AT, treg,
7086 (int) BFD_RELOC_LO16);
7087 expr1.X_add_number = 2;
7088 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", AT, AT,
7089 (int) BFD_RELOC_LO16);
2396cfb9 7090 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
f9419b05 7091 AT, RA);
2396cfb9
TS
7092 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7093 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
252b5132 7094 mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S", dreg, sreg);
2396cfb9 7095 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
f9419b05 7096 treg, RA);
2396cfb9 7097 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
252b5132
RH
7098 --mips_opts.noreorder;
7099 break;
7100
7101 case M_ULH:
7102 s = "lb";
7103 goto ulh;
7104 case M_ULHU:
7105 s = "lbu";
7106 ulh:
7107 if (offset_expr.X_add_number >= 0x7fff)
7108 as_bad (_("operand overflow"));
7109 /* avoid load delay */
7110 if (! target_big_endian)
f9419b05 7111 ++offset_expr.X_add_number;
252b5132
RH
7112 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7113 (int) BFD_RELOC_LO16, breg);
7114 if (! target_big_endian)
f9419b05 7115 --offset_expr.X_add_number;
252b5132 7116 else
f9419b05 7117 ++offset_expr.X_add_number;
252b5132
RH
7118 macro_build ((char *) NULL, &icnt, &offset_expr, "lbu", "t,o(b)", AT,
7119 (int) BFD_RELOC_LO16, breg);
2396cfb9
TS
7120 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7121 treg, treg, 8);
7122 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7123 treg, treg, AT);
252b5132
RH
7124 break;
7125
7126 case M_ULD:
7127 s = "ldl";
7128 s2 = "ldr";
7129 off = 7;
7130 goto ulw;
7131 case M_ULW:
7132 s = "lwl";
7133 s2 = "lwr";
7134 off = 3;
7135 ulw:
7136 if (offset_expr.X_add_number >= 0x8000 - off)
7137 as_bad (_("operand overflow"));
7138 if (! target_big_endian)
7139 offset_expr.X_add_number += off;
7140 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7141 (int) BFD_RELOC_LO16, breg);
7142 if (! target_big_endian)
7143 offset_expr.X_add_number -= off;
7144 else
7145 offset_expr.X_add_number += off;
7146 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7147 (int) BFD_RELOC_LO16, breg);
7148 return;
7149
7150 case M_ULD_A:
7151 s = "ldl";
7152 s2 = "ldr";
7153 off = 7;
7154 goto ulwa;
7155 case M_ULW_A:
7156 s = "lwl";
7157 s2 = "lwr";
7158 off = 3;
7159 ulwa:
d6bc6245 7160 used_at = 1;
c9914766 7161 load_address (&icnt, AT, &offset_expr, &used_at);
252b5132
RH
7162 if (breg != 0)
7163 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 7164 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
7165 "d,v,t", AT, AT, breg);
7166 if (! target_big_endian)
7167 expr1.X_add_number = off;
7168 else
7169 expr1.X_add_number = 0;
7170 macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7171 (int) BFD_RELOC_LO16, AT);
7172 if (! target_big_endian)
7173 expr1.X_add_number = 0;
7174 else
7175 expr1.X_add_number = off;
7176 macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7177 (int) BFD_RELOC_LO16, AT);
7178 break;
7179
7180 case M_ULH_A:
7181 case M_ULHU_A:
d6bc6245 7182 used_at = 1;
c9914766 7183 load_address (&icnt, AT, &offset_expr, &used_at);
252b5132
RH
7184 if (breg != 0)
7185 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 7186 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
7187 "d,v,t", AT, AT, breg);
7188 if (target_big_endian)
7189 expr1.X_add_number = 0;
7190 macro_build ((char *) NULL, &icnt, &expr1,
7191 mask == M_ULH_A ? "lb" : "lbu", "t,o(b)", treg,
7192 (int) BFD_RELOC_LO16, AT);
7193 if (target_big_endian)
7194 expr1.X_add_number = 1;
7195 else
7196 expr1.X_add_number = 0;
7197 macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7198 (int) BFD_RELOC_LO16, AT);
2396cfb9
TS
7199 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7200 treg, treg, 8);
7201 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7202 treg, treg, AT);
252b5132
RH
7203 break;
7204
7205 case M_USH:
7206 if (offset_expr.X_add_number >= 0x7fff)
7207 as_bad (_("operand overflow"));
7208 if (target_big_endian)
f9419b05 7209 ++offset_expr.X_add_number;
252b5132
RH
7210 macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", treg,
7211 (int) BFD_RELOC_LO16, breg);
2396cfb9
TS
7212 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7213 AT, treg, 8);
252b5132 7214 if (target_big_endian)
f9419b05 7215 --offset_expr.X_add_number;
252b5132 7216 else
f9419b05 7217 ++offset_expr.X_add_number;
252b5132
RH
7218 macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", AT,
7219 (int) BFD_RELOC_LO16, breg);
7220 break;
7221
7222 case M_USD:
7223 s = "sdl";
7224 s2 = "sdr";
7225 off = 7;
7226 goto usw;
7227 case M_USW:
7228 s = "swl";
7229 s2 = "swr";
7230 off = 3;
7231 usw:
7232 if (offset_expr.X_add_number >= 0x8000 - off)
7233 as_bad (_("operand overflow"));
7234 if (! target_big_endian)
7235 offset_expr.X_add_number += off;
7236 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7237 (int) BFD_RELOC_LO16, breg);
7238 if (! target_big_endian)
7239 offset_expr.X_add_number -= off;
7240 else
7241 offset_expr.X_add_number += off;
7242 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7243 (int) BFD_RELOC_LO16, breg);
7244 return;
7245
7246 case M_USD_A:
7247 s = "sdl";
7248 s2 = "sdr";
7249 off = 7;
7250 goto uswa;
7251 case M_USW_A:
7252 s = "swl";
7253 s2 = "swr";
7254 off = 3;
7255 uswa:
d6bc6245 7256 used_at = 1;
c9914766 7257 load_address (&icnt, AT, &offset_expr, &used_at);
252b5132
RH
7258 if (breg != 0)
7259 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 7260 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
7261 "d,v,t", AT, AT, breg);
7262 if (! target_big_endian)
7263 expr1.X_add_number = off;
7264 else
7265 expr1.X_add_number = 0;
7266 macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7267 (int) BFD_RELOC_LO16, AT);
7268 if (! target_big_endian)
7269 expr1.X_add_number = 0;
7270 else
7271 expr1.X_add_number = off;
7272 macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7273 (int) BFD_RELOC_LO16, AT);
7274 break;
7275
7276 case M_USH_A:
d6bc6245 7277 used_at = 1;
c9914766 7278 load_address (&icnt, AT, &offset_expr, &used_at);
252b5132
RH
7279 if (breg != 0)
7280 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 7281 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
7282 "d,v,t", AT, AT, breg);
7283 if (! target_big_endian)
7284 expr1.X_add_number = 0;
7285 macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7286 (int) BFD_RELOC_LO16, AT);
2396cfb9
TS
7287 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7288 treg, treg, 8);
252b5132
RH
7289 if (! target_big_endian)
7290 expr1.X_add_number = 1;
7291 else
7292 expr1.X_add_number = 0;
7293 macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7294 (int) BFD_RELOC_LO16, AT);
7295 if (! target_big_endian)
7296 expr1.X_add_number = 0;
7297 else
7298 expr1.X_add_number = 1;
7299 macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7300 (int) BFD_RELOC_LO16, AT);
2396cfb9
TS
7301 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7302 treg, treg, 8);
7303 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7304 treg, treg, AT);
252b5132
RH
7305 break;
7306
7307 default:
7308 /* FIXME: Check if this is one of the itbl macros, since they
bdaaa2e1 7309 are added dynamically. */
252b5132
RH
7310 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7311 break;
7312 }
7313 if (mips_opts.noat)
7314 as_warn (_("Macro used $at after \".set noat\""));
7315}
7316
7317/* Implement macros in mips16 mode. */
7318
7319static void
7320mips16_macro (ip)
7321 struct mips_cl_insn *ip;
7322{
7323 int mask;
7324 int xreg, yreg, zreg, tmp;
7325 int icnt;
7326 expressionS expr1;
7327 int dbl;
7328 const char *s, *s2, *s3;
7329
7330 mask = ip->insn_mo->mask;
7331
7332 xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
7333 yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
7334 zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
7335
7336 icnt = 0;
7337
7338 expr1.X_op = O_constant;
7339 expr1.X_op_symbol = NULL;
7340 expr1.X_add_symbol = NULL;
7341 expr1.X_add_number = 1;
7342
7343 dbl = 0;
7344
7345 switch (mask)
7346 {
7347 default:
7348 internalError ();
7349
7350 case M_DDIV_3:
7351 dbl = 1;
7352 case M_DIV_3:
7353 s = "mflo";
7354 goto do_div3;
7355 case M_DREM_3:
7356 dbl = 1;
7357 case M_REM_3:
7358 s = "mfhi";
7359 do_div3:
7360 mips_emit_delays (true);
7361 ++mips_opts.noreorder;
7362 mips_any_noreorder = 1;
2396cfb9 7363 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
252b5132
RH
7364 dbl ? "ddiv" : "div",
7365 "0,x,y", xreg, yreg);
7366 expr1.X_add_number = 2;
7367 macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
2396cfb9
TS
7368 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break", "6",
7369 7);
bdaaa2e1 7370
252b5132
RH
7371 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7372 since that causes an overflow. We should do that as well,
7373 but I don't see how to do the comparisons without a temporary
7374 register. */
7375 --mips_opts.noreorder;
2396cfb9 7376 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x", zreg);
252b5132
RH
7377 break;
7378
7379 case M_DIVU_3:
7380 s = "divu";
7381 s2 = "mflo";
7382 goto do_divu3;
7383 case M_REMU_3:
7384 s = "divu";
7385 s2 = "mfhi";
7386 goto do_divu3;
7387 case M_DDIVU_3:
7388 s = "ddivu";
7389 s2 = "mflo";
7390 goto do_divu3;
7391 case M_DREMU_3:
7392 s = "ddivu";
7393 s2 = "mfhi";
7394 do_divu3:
7395 mips_emit_delays (true);
7396 ++mips_opts.noreorder;
7397 mips_any_noreorder = 1;
2396cfb9
TS
7398 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "0,x,y",
7399 xreg, yreg);
252b5132
RH
7400 expr1.X_add_number = 2;
7401 macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
98d3f06f
KH
7402 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
7403 "6", 7);
252b5132 7404 --mips_opts.noreorder;
2396cfb9 7405 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "x", zreg);
252b5132
RH
7406 break;
7407
7408 case M_DMUL:
7409 dbl = 1;
7410 case M_MUL:
2396cfb9 7411 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 7412 dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
2396cfb9
TS
7413 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "x",
7414 zreg);
252b5132
RH
7415 return;
7416
7417 case M_DSUBU_I:
7418 dbl = 1;
7419 goto do_subu;
7420 case M_SUBU_I:
7421 do_subu:
7422 if (imm_expr.X_op != O_constant)
7423 as_bad (_("Unsupported large constant"));
7424 imm_expr.X_add_number = -imm_expr.X_add_number;
7425 macro_build ((char *) NULL, &icnt, &imm_expr,
9a41af64 7426 dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
252b5132
RH
7427 break;
7428
7429 case M_SUBU_I_2:
7430 if (imm_expr.X_op != O_constant)
7431 as_bad (_("Unsupported large constant"));
7432 imm_expr.X_add_number = -imm_expr.X_add_number;
7433 macro_build ((char *) NULL, &icnt, &imm_expr, "addiu",
7434 "x,k", xreg);
7435 break;
7436
7437 case M_DSUBU_I_2:
7438 if (imm_expr.X_op != O_constant)
7439 as_bad (_("Unsupported large constant"));
7440 imm_expr.X_add_number = -imm_expr.X_add_number;
7441 macro_build ((char *) NULL, &icnt, &imm_expr, "daddiu",
7442 "y,j", yreg);
7443 break;
7444
7445 case M_BEQ:
7446 s = "cmp";
7447 s2 = "bteqz";
7448 goto do_branch;
7449 case M_BNE:
7450 s = "cmp";
7451 s2 = "btnez";
7452 goto do_branch;
7453 case M_BLT:
7454 s = "slt";
7455 s2 = "btnez";
7456 goto do_branch;
7457 case M_BLTU:
7458 s = "sltu";
7459 s2 = "btnez";
7460 goto do_branch;
7461 case M_BLE:
7462 s = "slt";
7463 s2 = "bteqz";
7464 goto do_reverse_branch;
7465 case M_BLEU:
7466 s = "sltu";
7467 s2 = "bteqz";
7468 goto do_reverse_branch;
7469 case M_BGE:
7470 s = "slt";
7471 s2 = "bteqz";
7472 goto do_branch;
7473 case M_BGEU:
7474 s = "sltu";
7475 s2 = "bteqz";
7476 goto do_branch;
7477 case M_BGT:
7478 s = "slt";
7479 s2 = "btnez";
7480 goto do_reverse_branch;
7481 case M_BGTU:
7482 s = "sltu";
7483 s2 = "btnez";
7484
7485 do_reverse_branch:
7486 tmp = xreg;
7487 xreg = yreg;
7488 yreg = tmp;
7489
7490 do_branch:
7491 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x,y",
7492 xreg, yreg);
7493 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7494 break;
7495
7496 case M_BEQ_I:
7497 s = "cmpi";
7498 s2 = "bteqz";
7499 s3 = "x,U";
7500 goto do_branch_i;
7501 case M_BNE_I:
7502 s = "cmpi";
7503 s2 = "btnez";
7504 s3 = "x,U";
7505 goto do_branch_i;
7506 case M_BLT_I:
7507 s = "slti";
7508 s2 = "btnez";
7509 s3 = "x,8";
7510 goto do_branch_i;
7511 case M_BLTU_I:
7512 s = "sltiu";
7513 s2 = "btnez";
7514 s3 = "x,8";
7515 goto do_branch_i;
7516 case M_BLE_I:
7517 s = "slti";
7518 s2 = "btnez";
7519 s3 = "x,8";
7520 goto do_addone_branch_i;
7521 case M_BLEU_I:
7522 s = "sltiu";
7523 s2 = "btnez";
7524 s3 = "x,8";
7525 goto do_addone_branch_i;
7526 case M_BGE_I:
7527 s = "slti";
7528 s2 = "bteqz";
7529 s3 = "x,8";
7530 goto do_branch_i;
7531 case M_BGEU_I:
7532 s = "sltiu";
7533 s2 = "bteqz";
7534 s3 = "x,8";
7535 goto do_branch_i;
7536 case M_BGT_I:
7537 s = "slti";
7538 s2 = "bteqz";
7539 s3 = "x,8";
7540 goto do_addone_branch_i;
7541 case M_BGTU_I:
7542 s = "sltiu";
7543 s2 = "bteqz";
7544 s3 = "x,8";
7545
7546 do_addone_branch_i:
7547 if (imm_expr.X_op != O_constant)
7548 as_bad (_("Unsupported large constant"));
7549 ++imm_expr.X_add_number;
7550
7551 do_branch_i:
7552 macro_build ((char *) NULL, &icnt, &imm_expr, s, s3, xreg);
7553 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7554 break;
7555
7556 case M_ABS:
7557 expr1.X_add_number = 0;
98d3f06f 7558 macro_build ((char *) NULL, &icnt, &expr1, "slti", "x,8", yreg);
252b5132 7559 if (xreg != yreg)
ea1fb5dc 7560 move_register (&icnt, xreg, yreg);
252b5132
RH
7561 expr1.X_add_number = 2;
7562 macro_build ((char *) NULL, &icnt, &expr1, "bteqz", "p");
7563 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7564 "neg", "x,w", xreg, xreg);
7565 }
7566}
7567
7568/* For consistency checking, verify that all bits are specified either
7569 by the match/mask part of the instruction definition, or by the
7570 operand list. */
7571static int
7572validate_mips_insn (opc)
7573 const struct mips_opcode *opc;
7574{
7575 const char *p = opc->args;
7576 char c;
7577 unsigned long used_bits = opc->mask;
7578
7579 if ((used_bits & opc->match) != opc->match)
7580 {
7581 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
7582 opc->name, opc->args);
7583 return 0;
7584 }
7585#define USE_BITS(mask,shift) (used_bits |= ((mask) << (shift)))
7586 while (*p)
7587 switch (c = *p++)
7588 {
7589 case ',': break;
7590 case '(': break;
7591 case ')': break;
7592 case '<': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7593 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7594 case 'A': break;
4372b673 7595 case 'B': USE_BITS (OP_MASK_CODE20, OP_SH_CODE20); break;
252b5132
RH
7596 case 'C': USE_BITS (OP_MASK_COPZ, OP_SH_COPZ); break;
7597 case 'D': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
7598 case 'E': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7599 case 'F': break;
7600 case 'G': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
156c2f8b 7601 case 'H': USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
252b5132 7602 case 'I': break;
e972090a 7603 case 'J': USE_BITS (OP_MASK_CODE19, OP_SH_CODE19); break;
252b5132
RH
7604 case 'L': break;
7605 case 'M': USE_BITS (OP_MASK_CCC, OP_SH_CCC); break;
7606 case 'N': USE_BITS (OP_MASK_BCC, OP_SH_BCC); break;
deec1734
CD
7607 case 'O': USE_BITS (OP_MASK_ALN, OP_SH_ALN); break;
7608 case 'Q': USE_BITS (OP_MASK_VSEL, OP_SH_VSEL);
7609 USE_BITS (OP_MASK_FT, OP_SH_FT); break;
252b5132
RH
7610 case 'R': USE_BITS (OP_MASK_FR, OP_SH_FR); break;
7611 case 'S': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7612 case 'T': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7613 case 'V': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7614 case 'W': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
deec1734
CD
7615 case 'X': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
7616 case 'Y': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7617 case 'Z': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
252b5132
RH
7618 case 'a': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break;
7619 case 'b': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7620 case 'c': USE_BITS (OP_MASK_CODE, OP_SH_CODE); break;
7621 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
7622 case 'f': break;
7623 case 'h': USE_BITS (OP_MASK_PREFX, OP_SH_PREFX); break;
7624 case 'i': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
7625 case 'j': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7626 case 'k': USE_BITS (OP_MASK_CACHE, OP_SH_CACHE); break;
7627 case 'l': break;
7628 case 'o': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7629 case 'p': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7630 case 'q': USE_BITS (OP_MASK_CODE2, OP_SH_CODE2); break;
7631 case 'r': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7632 case 's': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7633 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7634 case 'u': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
7635 case 'v': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7636 case 'w': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7637 case 'x': break;
7638 case 'z': break;
7639 case 'P': USE_BITS (OP_MASK_PERFREG, OP_SH_PERFREG); break;
4372b673
NC
7640 case 'U': USE_BITS (OP_MASK_RD, OP_SH_RD);
7641 USE_BITS (OP_MASK_RT, OP_SH_RT); break;
252b5132
RH
7642 default:
7643 as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
7644 c, opc->name, opc->args);
7645 return 0;
7646 }
7647#undef USE_BITS
7648 if (used_bits != 0xffffffff)
7649 {
7650 as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
7651 ~used_bits & 0xffffffff, opc->name, opc->args);
7652 return 0;
7653 }
7654 return 1;
7655}
7656
7657/* This routine assembles an instruction into its binary format. As a
7658 side effect, it sets one of the global variables imm_reloc or
7659 offset_reloc to the type of relocation to do if one of the operands
7660 is an address expression. */
7661
7662static void
7663mips_ip (str, ip)
7664 char *str;
7665 struct mips_cl_insn *ip;
7666{
7667 char *s;
7668 const char *args;
43841e91 7669 char c = 0;
252b5132
RH
7670 struct mips_opcode *insn;
7671 char *argsStart;
7672 unsigned int regno;
7673 unsigned int lastregno = 0;
7674 char *s_reset;
7675 char save_c = 0;
252b5132
RH
7676
7677 insn_error = NULL;
7678
7679 /* If the instruction contains a '.', we first try to match an instruction
7680 including the '.'. Then we try again without the '.'. */
7681 insn = NULL;
3882b010 7682 for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
252b5132
RH
7683 continue;
7684
7685 /* If we stopped on whitespace, then replace the whitespace with null for
7686 the call to hash_find. Save the character we replaced just in case we
7687 have to re-parse the instruction. */
3882b010 7688 if (ISSPACE (*s))
252b5132
RH
7689 {
7690 save_c = *s;
7691 *s++ = '\0';
7692 }
bdaaa2e1 7693
252b5132
RH
7694 insn = (struct mips_opcode *) hash_find (op_hash, str);
7695
7696 /* If we didn't find the instruction in the opcode table, try again, but
7697 this time with just the instruction up to, but not including the
7698 first '.'. */
7699 if (insn == NULL)
7700 {
bdaaa2e1 7701 /* Restore the character we overwrite above (if any). */
252b5132
RH
7702 if (save_c)
7703 *(--s) = save_c;
7704
7705 /* Scan up to the first '.' or whitespace. */
3882b010
L
7706 for (s = str;
7707 *s != '\0' && *s != '.' && !ISSPACE (*s);
7708 ++s)
252b5132
RH
7709 continue;
7710
7711 /* If we did not find a '.', then we can quit now. */
7712 if (*s != '.')
7713 {
7714 insn_error = "unrecognized opcode";
7715 return;
7716 }
7717
7718 /* Lookup the instruction in the hash table. */
7719 *s++ = '\0';
7720 if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
7721 {
7722 insn_error = "unrecognized opcode";
7723 return;
7724 }
252b5132
RH
7725 }
7726
7727 argsStart = s;
7728 for (;;)
7729 {
252b5132
RH
7730 boolean ok;
7731
7732 assert (strcmp (insn->name, str) == 0);
7733
1f25f5d3
CD
7734 if (OPCODE_IS_MEMBER (insn,
7735 (mips_opts.isa
aec421e0 7736 | (mips_opts.mips16 ? INSN_MIPS16 : 0)
deec1734 7737 | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
98d3f06f 7738 | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
1f25f5d3 7739 mips_arch))
252b5132 7740 ok = true;
bdaaa2e1 7741 else
252b5132 7742 ok = false;
bdaaa2e1 7743
252b5132
RH
7744 if (insn->pinfo != INSN_MACRO)
7745 {
ec68c924 7746 if (mips_arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
252b5132
RH
7747 ok = false;
7748 }
7749
7750 if (! ok)
7751 {
7752 if (insn + 1 < &mips_opcodes[NUMOPCODES]
7753 && strcmp (insn->name, insn[1].name) == 0)
7754 {
7755 ++insn;
7756 continue;
7757 }
252b5132 7758 else
beae10d5 7759 {
268f6bed
L
7760 if (!insn_error)
7761 {
7762 static char buf[100];
316f5878
RS
7763 if (mips_arch_info->is_isa)
7764 sprintf (buf,
7765 _("opcode not supported at this ISA level (%s)"),
7766 mips_cpu_info_from_isa (mips_opts.isa)->name);
7767 else
7768 sprintf (buf,
7769 _("opcode not supported on this processor: %s (%s)"),
7770 mips_arch_info->name,
7771 mips_cpu_info_from_isa (mips_opts.isa)->name);
268f6bed
L
7772 insn_error = buf;
7773 }
7774 if (save_c)
7775 *(--s) = save_c;
2bd7f1f3 7776 return;
252b5132 7777 }
252b5132
RH
7778 }
7779
7780 ip->insn_mo = insn;
7781 ip->insn_opcode = insn->match;
268f6bed 7782 insn_error = NULL;
252b5132
RH
7783 for (args = insn->args;; ++args)
7784 {
deec1734
CD
7785 int is_mdmx;
7786
ad8d3bb3 7787 s += strspn (s, " \t");
deec1734 7788 is_mdmx = 0;
252b5132
RH
7789 switch (*args)
7790 {
7791 case '\0': /* end of args */
7792 if (*s == '\0')
7793 return;
7794 break;
7795
7796 case ',':
7797 if (*s++ == *args)
7798 continue;
7799 s--;
7800 switch (*++args)
7801 {
7802 case 'r':
7803 case 'v':
38487616 7804 ip->insn_opcode |= lastregno << OP_SH_RS;
252b5132
RH
7805 continue;
7806
7807 case 'w':
38487616
TS
7808 ip->insn_opcode |= lastregno << OP_SH_RT;
7809 continue;
7810
252b5132 7811 case 'W':
38487616 7812 ip->insn_opcode |= lastregno << OP_SH_FT;
252b5132
RH
7813 continue;
7814
7815 case 'V':
38487616 7816 ip->insn_opcode |= lastregno << OP_SH_FS;
252b5132
RH
7817 continue;
7818 }
7819 break;
7820
7821 case '(':
7822 /* Handle optional base register.
7823 Either the base register is omitted or
bdaaa2e1 7824 we must have a left paren. */
252b5132
RH
7825 /* This is dependent on the next operand specifier
7826 is a base register specification. */
7827 assert (args[1] == 'b' || args[1] == '5'
7828 || args[1] == '-' || args[1] == '4');
7829 if (*s == '\0')
7830 return;
7831
7832 case ')': /* these must match exactly */
7833 if (*s++ == *args)
7834 continue;
7835 break;
7836
7837 case '<': /* must be at least one digit */
7838 /*
7839 * According to the manual, if the shift amount is greater
b6ff326e
KH
7840 * than 31 or less than 0, then the shift amount should be
7841 * mod 32. In reality the mips assembler issues an error.
252b5132
RH
7842 * We issue a warning and mask out all but the low 5 bits.
7843 */
7844 my_getExpression (&imm_expr, s);
7845 check_absolute_expr (ip, &imm_expr);
7846 if ((unsigned long) imm_expr.X_add_number > 31)
7847 {
793b27f4
TS
7848 as_warn (_("Improper shift amount (%lu)"),
7849 (unsigned long) imm_expr.X_add_number);
38487616 7850 imm_expr.X_add_number &= OP_MASK_SHAMT;
252b5132 7851 }
38487616 7852 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SHAMT;
252b5132
RH
7853 imm_expr.X_op = O_absent;
7854 s = expr_end;
7855 continue;
7856
7857 case '>': /* shift amount minus 32 */
7858 my_getExpression (&imm_expr, s);
7859 check_absolute_expr (ip, &imm_expr);
7860 if ((unsigned long) imm_expr.X_add_number < 32
7861 || (unsigned long) imm_expr.X_add_number > 63)
7862 break;
38487616 7863 ip->insn_opcode |= (imm_expr.X_add_number - 32) << OP_SH_SHAMT;
252b5132
RH
7864 imm_expr.X_op = O_absent;
7865 s = expr_end;
7866 continue;
7867
252b5132
RH
7868 case 'k': /* cache code */
7869 case 'h': /* prefx code */
7870 my_getExpression (&imm_expr, s);
7871 check_absolute_expr (ip, &imm_expr);
7872 if ((unsigned long) imm_expr.X_add_number > 31)
7873 {
7874 as_warn (_("Invalid value for `%s' (%lu)"),
7875 ip->insn_mo->name,
7876 (unsigned long) imm_expr.X_add_number);
7877 imm_expr.X_add_number &= 0x1f;
7878 }
7879 if (*args == 'k')
7880 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
7881 else
7882 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
7883 imm_expr.X_op = O_absent;
7884 s = expr_end;
7885 continue;
7886
7887 case 'c': /* break code */
7888 my_getExpression (&imm_expr, s);
7889 check_absolute_expr (ip, &imm_expr);
793b27f4 7890 if ((unsigned long) imm_expr.X_add_number > 1023)
252b5132 7891 {
793b27f4
TS
7892 as_warn (_("Illegal break code (%lu)"),
7893 (unsigned long) imm_expr.X_add_number);
38487616 7894 imm_expr.X_add_number &= OP_MASK_CODE;
252b5132 7895 }
38487616 7896 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE;
252b5132
RH
7897 imm_expr.X_op = O_absent;
7898 s = expr_end;
7899 continue;
7900
7901 case 'q': /* lower break code */
7902 my_getExpression (&imm_expr, s);
7903 check_absolute_expr (ip, &imm_expr);
793b27f4 7904 if ((unsigned long) imm_expr.X_add_number > 1023)
252b5132 7905 {
793b27f4
TS
7906 as_warn (_("Illegal lower break code (%lu)"),
7907 (unsigned long) imm_expr.X_add_number);
38487616 7908 imm_expr.X_add_number &= OP_MASK_CODE2;
252b5132 7909 }
38487616 7910 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE2;
252b5132
RH
7911 imm_expr.X_op = O_absent;
7912 s = expr_end;
7913 continue;
7914
4372b673 7915 case 'B': /* 20-bit syscall/break code. */
156c2f8b 7916 my_getExpression (&imm_expr, s);
156c2f8b 7917 check_absolute_expr (ip, &imm_expr);
793b27f4
TS
7918 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
7919 as_warn (_("Illegal 20-bit code (%lu)"),
7920 (unsigned long) imm_expr.X_add_number);
38487616 7921 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE20;
252b5132
RH
7922 imm_expr.X_op = O_absent;
7923 s = expr_end;
7924 continue;
7925
98d3f06f 7926 case 'C': /* Coprocessor code */
beae10d5 7927 my_getExpression (&imm_expr, s);
252b5132 7928 check_absolute_expr (ip, &imm_expr);
98d3f06f 7929 if ((unsigned long) imm_expr.X_add_number >= (1 << 25))
252b5132 7930 {
793b27f4
TS
7931 as_warn (_("Coproccesor code > 25 bits (%lu)"),
7932 (unsigned long) imm_expr.X_add_number);
98d3f06f 7933 imm_expr.X_add_number &= ((1 << 25) - 1);
252b5132 7934 }
beae10d5
KH
7935 ip->insn_opcode |= imm_expr.X_add_number;
7936 imm_expr.X_op = O_absent;
7937 s = expr_end;
7938 continue;
252b5132 7939
4372b673
NC
7940 case 'J': /* 19-bit wait code. */
7941 my_getExpression (&imm_expr, s);
7942 check_absolute_expr (ip, &imm_expr);
793b27f4
TS
7943 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
7944 as_warn (_("Illegal 19-bit code (%lu)"),
7945 (unsigned long) imm_expr.X_add_number);
38487616 7946 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE19;
4372b673
NC
7947 imm_expr.X_op = O_absent;
7948 s = expr_end;
7949 continue;
7950
252b5132 7951 case 'P': /* Performance register */
beae10d5 7952 my_getExpression (&imm_expr, s);
252b5132 7953 check_absolute_expr (ip, &imm_expr);
beae10d5 7954 if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
252b5132 7955 {
793b27f4
TS
7956 as_warn (_("Invalid performance register (%lu)"),
7957 (unsigned long) imm_expr.X_add_number);
38487616 7958 imm_expr.X_add_number &= OP_MASK_PERFREG;
252b5132 7959 }
38487616 7960 ip->insn_opcode |= (imm_expr.X_add_number << OP_SH_PERFREG);
beae10d5
KH
7961 imm_expr.X_op = O_absent;
7962 s = expr_end;
7963 continue;
252b5132
RH
7964
7965 case 'b': /* base register */
7966 case 'd': /* destination register */
7967 case 's': /* source register */
7968 case 't': /* target register */
7969 case 'r': /* both target and source */
7970 case 'v': /* both dest and source */
7971 case 'w': /* both dest and target */
7972 case 'E': /* coprocessor target register */
7973 case 'G': /* coprocessor destination register */
7974 case 'x': /* ignore register name */
7975 case 'z': /* must be zero register */
4372b673 7976 case 'U': /* destination register (clo/clz). */
252b5132
RH
7977 s_reset = s;
7978 if (s[0] == '$')
7979 {
7980
3882b010 7981 if (ISDIGIT (s[1]))
252b5132
RH
7982 {
7983 ++s;
7984 regno = 0;
7985 do
7986 {
7987 regno *= 10;
7988 regno += *s - '0';
7989 ++s;
7990 }
3882b010 7991 while (ISDIGIT (*s));
252b5132
RH
7992 if (regno > 31)
7993 as_bad (_("Invalid register number (%d)"), regno);
7994 }
7995 else if (*args == 'E' || *args == 'G')
7996 goto notreg;
7997 else
7998 {
76db943d
TS
7999 if (s[1] == 'r' && s[2] == 'a')
8000 {
8001 s += 3;
8002 regno = RA;
8003 }
8004 else if (s[1] == 'f' && s[2] == 'p')
252b5132
RH
8005 {
8006 s += 3;
8007 regno = FP;
8008 }
8009 else if (s[1] == 's' && s[2] == 'p')
8010 {
8011 s += 3;
8012 regno = SP;
8013 }
8014 else if (s[1] == 'g' && s[2] == 'p')
8015 {
8016 s += 3;
8017 regno = GP;
8018 }
8019 else if (s[1] == 'a' && s[2] == 't')
8020 {
8021 s += 3;
8022 regno = AT;
8023 }
8024 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8025 {
8026 s += 4;
8027 regno = KT0;
8028 }
8029 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8030 {
8031 s += 4;
8032 regno = KT1;
8033 }
85b51719
TS
8034 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
8035 {
8036 s += 5;
8037 regno = ZERO;
8038 }
252b5132
RH
8039 else if (itbl_have_entries)
8040 {
8041 char *p, *n;
d7ba4a77 8042 unsigned long r;
252b5132 8043
d7ba4a77 8044 p = s + 1; /* advance past '$' */
252b5132
RH
8045 n = itbl_get_field (&p); /* n is name */
8046
d7ba4a77
ILT
8047 /* See if this is a register defined in an
8048 itbl entry. */
8049 if (itbl_get_reg_val (n, &r))
252b5132
RH
8050 {
8051 /* Get_field advances to the start of
8052 the next field, so we need to back
d7ba4a77 8053 rack to the end of the last field. */
bdaaa2e1 8054 if (p)
252b5132 8055 s = p - 1;
bdaaa2e1 8056 else
d7ba4a77 8057 s = strchr (s, '\0');
252b5132
RH
8058 regno = r;
8059 }
8060 else
8061 goto notreg;
beae10d5 8062 }
252b5132
RH
8063 else
8064 goto notreg;
8065 }
8066 if (regno == AT
8067 && ! mips_opts.noat
8068 && *args != 'E'
8069 && *args != 'G')
8070 as_warn (_("Used $at without \".set noat\""));
8071 c = *args;
8072 if (*s == ' ')
f9419b05 8073 ++s;
252b5132
RH
8074 if (args[1] != *s)
8075 {
8076 if (c == 'r' || c == 'v' || c == 'w')
8077 {
8078 regno = lastregno;
8079 s = s_reset;
f9419b05 8080 ++args;
252b5132
RH
8081 }
8082 }
8083 /* 'z' only matches $0. */
8084 if (c == 'z' && regno != 0)
8085 break;
8086
bdaaa2e1
KH
8087 /* Now that we have assembled one operand, we use the args string
8088 * to figure out where it goes in the instruction. */
252b5132
RH
8089 switch (c)
8090 {
8091 case 'r':
8092 case 's':
8093 case 'v':
8094 case 'b':
38487616 8095 ip->insn_opcode |= regno << OP_SH_RS;
252b5132
RH
8096 break;
8097 case 'd':
8098 case 'G':
38487616 8099 ip->insn_opcode |= regno << OP_SH_RD;
252b5132 8100 break;
4372b673 8101 case 'U':
38487616
TS
8102 ip->insn_opcode |= regno << OP_SH_RD;
8103 ip->insn_opcode |= regno << OP_SH_RT;
4372b673 8104 break;
252b5132
RH
8105 case 'w':
8106 case 't':
8107 case 'E':
38487616 8108 ip->insn_opcode |= regno << OP_SH_RT;
252b5132
RH
8109 break;
8110 case 'x':
8111 /* This case exists because on the r3000 trunc
8112 expands into a macro which requires a gp
8113 register. On the r6000 or r4000 it is
8114 assembled into a single instruction which
8115 ignores the register. Thus the insn version
8116 is MIPS_ISA2 and uses 'x', and the macro
8117 version is MIPS_ISA1 and uses 't'. */
8118 break;
8119 case 'z':
8120 /* This case is for the div instruction, which
8121 acts differently if the destination argument
8122 is $0. This only matches $0, and is checked
8123 outside the switch. */
8124 break;
8125 case 'D':
8126 /* Itbl operand; not yet implemented. FIXME ?? */
8127 break;
8128 /* What about all other operands like 'i', which
8129 can be specified in the opcode table? */
8130 }
8131 lastregno = regno;
8132 continue;
8133 }
8134 notreg:
8135 switch (*args++)
8136 {
8137 case 'r':
8138 case 'v':
38487616 8139 ip->insn_opcode |= lastregno << OP_SH_RS;
252b5132
RH
8140 continue;
8141 case 'w':
38487616 8142 ip->insn_opcode |= lastregno << OP_SH_RT;
252b5132
RH
8143 continue;
8144 }
8145 break;
8146
deec1734
CD
8147 case 'O': /* MDMX alignment immediate constant. */
8148 my_getExpression (&imm_expr, s);
8149 check_absolute_expr (ip, &imm_expr);
8150 if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
8151 {
8152 as_warn ("Improper align amount (%ld), using low bits",
8153 (long) imm_expr.X_add_number);
8154 imm_expr.X_add_number &= OP_MASK_ALN;
8155 }
8156 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_ALN;
8157 imm_expr.X_op = O_absent;
8158 s = expr_end;
8159 continue;
8160
8161 case 'Q': /* MDMX vector, element sel, or const. */
8162 if (s[0] != '$')
8163 {
8164 /* MDMX Immediate. */
8165 my_getExpression (&imm_expr, s);
8166 check_absolute_expr (ip, &imm_expr);
8167 if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
8168 {
8169 as_warn (_("Invalid MDMX Immediate (%ld)"),
8170 (long) imm_expr.X_add_number);
8171 imm_expr.X_add_number &= OP_MASK_FT;
8172 }
8173 imm_expr.X_add_number &= OP_MASK_FT;
8174 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8175 ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
8176 else
8177 ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
8178 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_FT;
8179 imm_expr.X_op = O_absent;
8180 s = expr_end;
8181 continue;
8182 }
8183 /* Not MDMX Immediate. Fall through. */
8184 case 'X': /* MDMX destination register. */
8185 case 'Y': /* MDMX source register. */
8186 case 'Z': /* MDMX target register. */
8187 is_mdmx = 1;
252b5132
RH
8188 case 'D': /* floating point destination register */
8189 case 'S': /* floating point source register */
8190 case 'T': /* floating point target register */
8191 case 'R': /* floating point source register */
8192 case 'V':
8193 case 'W':
8194 s_reset = s;
deec1734
CD
8195 /* Accept $fN for FP and MDMX register numbers, and in
8196 addition accept $vN for MDMX register numbers. */
8197 if ((s[0] == '$' && s[1] == 'f' && ISDIGIT (s[2]))
8198 || (is_mdmx != 0 && s[0] == '$' && s[1] == 'v'
8199 && ISDIGIT (s[2])))
252b5132
RH
8200 {
8201 s += 2;
8202 regno = 0;
8203 do
8204 {
8205 regno *= 10;
8206 regno += *s - '0';
8207 ++s;
8208 }
3882b010 8209 while (ISDIGIT (*s));
252b5132
RH
8210
8211 if (regno > 31)
8212 as_bad (_("Invalid float register number (%d)"), regno);
8213
8214 if ((regno & 1) != 0
ca4e0257 8215 && HAVE_32BIT_FPRS
252b5132
RH
8216 && ! (strcmp (str, "mtc1") == 0
8217 || strcmp (str, "mfc1") == 0
8218 || strcmp (str, "lwc1") == 0
8219 || strcmp (str, "swc1") == 0
8220 || strcmp (str, "l.s") == 0
8221 || strcmp (str, "s.s") == 0))
8222 as_warn (_("Float register should be even, was %d"),
8223 regno);
8224
8225 c = *args;
8226 if (*s == ' ')
f9419b05 8227 ++s;
252b5132
RH
8228 if (args[1] != *s)
8229 {
8230 if (c == 'V' || c == 'W')
8231 {
8232 regno = lastregno;
8233 s = s_reset;
f9419b05 8234 ++args;
252b5132
RH
8235 }
8236 }
8237 switch (c)
8238 {
8239 case 'D':
deec1734 8240 case 'X':
38487616 8241 ip->insn_opcode |= regno << OP_SH_FD;
252b5132
RH
8242 break;
8243 case 'V':
8244 case 'S':
deec1734 8245 case 'Y':
38487616 8246 ip->insn_opcode |= regno << OP_SH_FS;
252b5132 8247 break;
deec1734
CD
8248 case 'Q':
8249 /* This is like 'Z', but also needs to fix the MDMX
8250 vector/scalar select bits. Note that the
8251 scalar immediate case is handled above. */
8252 if (*s == '[')
8253 {
8254 int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
8255 int max_el = (is_qh ? 3 : 7);
8256 s++;
8257 my_getExpression(&imm_expr, s);
8258 check_absolute_expr (ip, &imm_expr);
8259 s = expr_end;
8260 if (imm_expr.X_add_number > max_el)
8261 as_bad(_("Bad element selector %ld"),
8262 (long) imm_expr.X_add_number);
8263 imm_expr.X_add_number &= max_el;
8264 ip->insn_opcode |= (imm_expr.X_add_number
8265 << (OP_SH_VSEL +
8266 (is_qh ? 2 : 1)));
8267 if (*s != ']')
8268 as_warn(_("Expecting ']' found '%s'"), s);
8269 else
8270 s++;
8271 }
8272 else
8273 {
8274 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8275 ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
8276 << OP_SH_VSEL);
8277 else
8278 ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
8279 OP_SH_VSEL);
8280 }
8281 /* Fall through */
252b5132
RH
8282 case 'W':
8283 case 'T':
deec1734 8284 case 'Z':
38487616 8285 ip->insn_opcode |= regno << OP_SH_FT;
252b5132
RH
8286 break;
8287 case 'R':
38487616 8288 ip->insn_opcode |= regno << OP_SH_FR;
252b5132
RH
8289 break;
8290 }
8291 lastregno = regno;
8292 continue;
8293 }
8294
252b5132
RH
8295 switch (*args++)
8296 {
8297 case 'V':
38487616 8298 ip->insn_opcode |= lastregno << OP_SH_FS;
252b5132
RH
8299 continue;
8300 case 'W':
38487616 8301 ip->insn_opcode |= lastregno << OP_SH_FT;
252b5132
RH
8302 continue;
8303 }
8304 break;
8305
8306 case 'I':
8307 my_getExpression (&imm_expr, s);
8308 if (imm_expr.X_op != O_big
8309 && imm_expr.X_op != O_constant)
8310 insn_error = _("absolute expression required");
8311 s = expr_end;
8312 continue;
8313
8314 case 'A':
8315 my_getExpression (&offset_expr, s);
f6688943 8316 *imm_reloc = BFD_RELOC_32;
252b5132
RH
8317 s = expr_end;
8318 continue;
8319
8320 case 'F':
8321 case 'L':
8322 case 'f':
8323 case 'l':
8324 {
8325 int f64;
ca4e0257 8326 int using_gprs;
252b5132
RH
8327 char *save_in;
8328 char *err;
8329 unsigned char temp[8];
8330 int len;
8331 unsigned int length;
8332 segT seg;
8333 subsegT subseg;
8334 char *p;
8335
8336 /* These only appear as the last operand in an
8337 instruction, and every instruction that accepts
8338 them in any variant accepts them in all variants.
8339 This means we don't have to worry about backing out
8340 any changes if the instruction does not match.
8341
8342 The difference between them is the size of the
8343 floating point constant and where it goes. For 'F'
8344 and 'L' the constant is 64 bits; for 'f' and 'l' it
8345 is 32 bits. Where the constant is placed is based
8346 on how the MIPS assembler does things:
8347 F -- .rdata
8348 L -- .lit8
8349 f -- immediate value
8350 l -- .lit4
8351
8352 The .lit4 and .lit8 sections are only used if
8353 permitted by the -G argument.
8354
8355 When generating embedded PIC code, we use the
8356 .lit8 section but not the .lit4 section (we can do
8357 .lit4 inline easily; we need to put .lit8
8358 somewhere in the data segment, and using .lit8
8359 permits the linker to eventually combine identical
ca4e0257
RS
8360 .lit8 entries).
8361
8362 The code below needs to know whether the target register
8363 is 32 or 64 bits wide. It relies on the fact 'f' and
8364 'F' are used with GPR-based instructions and 'l' and
8365 'L' are used with FPR-based instructions. */
252b5132
RH
8366
8367 f64 = *args == 'F' || *args == 'L';
ca4e0257 8368 using_gprs = *args == 'F' || *args == 'f';
252b5132
RH
8369
8370 save_in = input_line_pointer;
8371 input_line_pointer = s;
8372 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
8373 length = len;
8374 s = input_line_pointer;
8375 input_line_pointer = save_in;
8376 if (err != NULL && *err != '\0')
8377 {
8378 as_bad (_("Bad floating point constant: %s"), err);
8379 memset (temp, '\0', sizeof temp);
8380 length = f64 ? 8 : 4;
8381 }
8382
156c2f8b 8383 assert (length == (unsigned) (f64 ? 8 : 4));
252b5132
RH
8384
8385 if (*args == 'f'
8386 || (*args == 'l'
8387 && (! USE_GLOBAL_POINTER_OPT
8388 || mips_pic == EMBEDDED_PIC
8389 || g_switch_value < 4
8390 || (temp[0] == 0 && temp[1] == 0)
8391 || (temp[2] == 0 && temp[3] == 0))))
8392 {
8393 imm_expr.X_op = O_constant;
8394 if (! target_big_endian)
8395 imm_expr.X_add_number = bfd_getl32 (temp);
8396 else
8397 imm_expr.X_add_number = bfd_getb32 (temp);
8398 }
8399 else if (length > 4
119d663a 8400 && ! mips_disable_float_construction
ca4e0257
RS
8401 /* Constants can only be constructed in GPRs and
8402 copied to FPRs if the GPRs are at least as wide
8403 as the FPRs. Force the constant into memory if
8404 we are using 64-bit FPRs but the GPRs are only
8405 32 bits wide. */
8406 && (using_gprs
8407 || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
252b5132
RH
8408 && ((temp[0] == 0 && temp[1] == 0)
8409 || (temp[2] == 0 && temp[3] == 0))
8410 && ((temp[4] == 0 && temp[5] == 0)
8411 || (temp[6] == 0 && temp[7] == 0)))
8412 {
ca4e0257
RS
8413 /* The value is simple enough to load with a couple of
8414 instructions. If using 32-bit registers, set
8415 imm_expr to the high order 32 bits and offset_expr to
8416 the low order 32 bits. Otherwise, set imm_expr to
8417 the entire 64 bit constant. */
8418 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
252b5132
RH
8419 {
8420 imm_expr.X_op = O_constant;
8421 offset_expr.X_op = O_constant;
8422 if (! target_big_endian)
8423 {
8424 imm_expr.X_add_number = bfd_getl32 (temp + 4);
8425 offset_expr.X_add_number = bfd_getl32 (temp);
8426 }
8427 else
8428 {
8429 imm_expr.X_add_number = bfd_getb32 (temp);
8430 offset_expr.X_add_number = bfd_getb32 (temp + 4);
8431 }
8432 if (offset_expr.X_add_number == 0)
8433 offset_expr.X_op = O_absent;
8434 }
8435 else if (sizeof (imm_expr.X_add_number) > 4)
8436 {
8437 imm_expr.X_op = O_constant;
8438 if (! target_big_endian)
8439 imm_expr.X_add_number = bfd_getl64 (temp);
8440 else
8441 imm_expr.X_add_number = bfd_getb64 (temp);
8442 }
8443 else
8444 {
8445 imm_expr.X_op = O_big;
8446 imm_expr.X_add_number = 4;
8447 if (! target_big_endian)
8448 {
8449 generic_bignum[0] = bfd_getl16 (temp);
8450 generic_bignum[1] = bfd_getl16 (temp + 2);
8451 generic_bignum[2] = bfd_getl16 (temp + 4);
8452 generic_bignum[3] = bfd_getl16 (temp + 6);
8453 }
8454 else
8455 {
8456 generic_bignum[0] = bfd_getb16 (temp + 6);
8457 generic_bignum[1] = bfd_getb16 (temp + 4);
8458 generic_bignum[2] = bfd_getb16 (temp + 2);
8459 generic_bignum[3] = bfd_getb16 (temp);
8460 }
8461 }
8462 }
8463 else
8464 {
8465 const char *newname;
8466 segT new_seg;
8467
8468 /* Switch to the right section. */
8469 seg = now_seg;
8470 subseg = now_subseg;
8471 switch (*args)
8472 {
8473 default: /* unused default case avoids warnings. */
8474 case 'L':
8475 newname = RDATA_SECTION_NAME;
bb2d6cd7
GK
8476 if ((USE_GLOBAL_POINTER_OPT && g_switch_value >= 8)
8477 || mips_pic == EMBEDDED_PIC)
252b5132
RH
8478 newname = ".lit8";
8479 break;
8480 case 'F':
bb2d6cd7
GK
8481 if (mips_pic == EMBEDDED_PIC)
8482 newname = ".lit8";
8483 else
8484 newname = RDATA_SECTION_NAME;
252b5132
RH
8485 break;
8486 case 'l':
8487 assert (!USE_GLOBAL_POINTER_OPT
8488 || g_switch_value >= 4);
8489 newname = ".lit4";
8490 break;
8491 }
8492 new_seg = subseg_new (newname, (subsegT) 0);
8493 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
8494 bfd_set_section_flags (stdoutput, new_seg,
8495 (SEC_ALLOC
8496 | SEC_LOAD
8497 | SEC_READONLY
8498 | SEC_DATA));
8499 frag_align (*args == 'l' ? 2 : 3, 0, 0);
8500 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
8501 && strcmp (TARGET_OS, "elf") != 0)
8502 record_alignment (new_seg, 4);
8503 else
8504 record_alignment (new_seg, *args == 'l' ? 2 : 3);
8505 if (seg == now_seg)
8506 as_bad (_("Can't use floating point insn in this section"));
8507
8508 /* Set the argument to the current address in the
8509 section. */
8510 offset_expr.X_op = O_symbol;
8511 offset_expr.X_add_symbol =
8512 symbol_new ("L0\001", now_seg,
8513 (valueT) frag_now_fix (), frag_now);
8514 offset_expr.X_add_number = 0;
8515
8516 /* Put the floating point number into the section. */
8517 p = frag_more ((int) length);
8518 memcpy (p, temp, length);
8519
8520 /* Switch back to the original section. */
8521 subseg_set (seg, subseg);
8522 }
8523 }
8524 continue;
8525
8526 case 'i': /* 16 bit unsigned immediate */
8527 case 'j': /* 16 bit signed immediate */
f6688943 8528 *imm_reloc = BFD_RELOC_LO16;
252b5132 8529 c = my_getSmallExpression (&imm_expr, s);
fb1b3232 8530 if (c != S_EX_NONE)
252b5132 8531 {
fb1b3232 8532 if (c != S_EX_LO)
252b5132 8533 {
fdb987ee
RS
8534 if (c == S_EX_HI)
8535 {
8536 *imm_reloc = BFD_RELOC_HI16_S;
8537 imm_unmatched_hi = true;
8538 }
ad8d3bb3 8539#ifdef OBJ_ELF
fb1b3232 8540 else if (c == S_EX_HIGHEST)
98d3f06f 8541 *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
fb1b3232 8542 else if (c == S_EX_HIGHER)
98d3f06f 8543 *imm_reloc = BFD_RELOC_MIPS_HIGHER;
ad8d3bb3
TS
8544 else if (c == S_EX_GP_REL)
8545 {
8546 /* This occurs in NewABI only. */
8547 c = my_getSmallExpression (&imm_expr, s);
8548 if (c != S_EX_NEG)
8549 as_bad (_("bad composition of relocations"));
8550 else
8551 {
8552 c = my_getSmallExpression (&imm_expr, s);
8553 if (c != S_EX_LO)
8554 as_bad (_("bad composition of relocations"));
8555 else
8556 {
8557 imm_reloc[0] = BFD_RELOC_GPREL16;
8558 imm_reloc[1] = BFD_RELOC_MIPS_SUB;
8559 imm_reloc[2] = BFD_RELOC_LO16;
8560 }
8561 }
8562 }
8563#endif
252b5132 8564 else
f6688943 8565 *imm_reloc = BFD_RELOC_HI16;
252b5132
RH
8566 }
8567 else if (imm_expr.X_op == O_constant)
8568 imm_expr.X_add_number &= 0xffff;
8569 }
8570 if (*args == 'i')
8571 {
fb1b3232 8572 if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
252b5132 8573 || ((imm_expr.X_add_number < 0
beae10d5
KH
8574 || imm_expr.X_add_number >= 0x10000)
8575 && imm_expr.X_op == O_constant))
252b5132
RH
8576 {
8577 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8578 !strcmp (insn->name, insn[1].name))
8579 break;
2ae7e77b
AH
8580 if (imm_expr.X_op == O_constant
8581 || imm_expr.X_op == O_big)
252b5132
RH
8582 as_bad (_("16 bit expression not in range 0..65535"));
8583 }
8584 }
8585 else
8586 {
8587 int more;
8588 offsetT max;
8589
8590 /* The upper bound should be 0x8000, but
8591 unfortunately the MIPS assembler accepts numbers
8592 from 0x8000 to 0xffff and sign extends them, and
8593 we want to be compatible. We only permit this
8594 extended range for an instruction which does not
8595 provide any further alternates, since those
8596 alternates may handle other cases. People should
8597 use the numbers they mean, rather than relying on
8598 a mysterious sign extension. */
8599 more = (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8600 strcmp (insn->name, insn[1].name) == 0);
8601 if (more)
8602 max = 0x8000;
8603 else
8604 max = 0x10000;
fb1b3232 8605 if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
252b5132 8606 || ((imm_expr.X_add_number < -0x8000
beae10d5
KH
8607 || imm_expr.X_add_number >= max)
8608 && imm_expr.X_op == O_constant)
252b5132
RH
8609 || (more
8610 && imm_expr.X_add_number < 0
ca4e0257 8611 && HAVE_64BIT_GPRS
252b5132
RH
8612 && imm_expr.X_unsigned
8613 && sizeof (imm_expr.X_add_number) <= 4))
8614 {
8615 if (more)
8616 break;
2ae7e77b
AH
8617 if (imm_expr.X_op == O_constant
8618 || imm_expr.X_op == O_big)
252b5132
RH
8619 as_bad (_("16 bit expression not in range -32768..32767"));
8620 }
8621 }
8622 s = expr_end;
8623 continue;
8624
8625 case 'o': /* 16 bit offset */
8626 c = my_getSmallExpression (&offset_expr, s);
8627
8628 /* If this value won't fit into a 16 bit offset, then go
8629 find a macro that will generate the 32 bit offset
afdbd6d0 8630 code pattern. */
fb1b3232 8631 if (c == S_EX_NONE
252b5132
RH
8632 && (offset_expr.X_op != O_constant
8633 || offset_expr.X_add_number >= 0x8000
afdbd6d0 8634 || offset_expr.X_add_number < -0x8000))
252b5132
RH
8635 break;
8636
fb1b3232 8637 if (c == S_EX_HI)
252b5132
RH
8638 {
8639 if (offset_expr.X_op != O_constant)
8640 break;
8641 offset_expr.X_add_number =
8642 (offset_expr.X_add_number >> 16) & 0xffff;
8643 }
f6688943 8644 *offset_reloc = BFD_RELOC_LO16;
252b5132
RH
8645 s = expr_end;
8646 continue;
8647
8648 case 'p': /* pc relative offset */
cb56d3d3 8649 if (mips_pic == EMBEDDED_PIC)
f6688943 8650 *offset_reloc = BFD_RELOC_16_PCREL_S2;
cb56d3d3 8651 else
f6688943 8652 *offset_reloc = BFD_RELOC_16_PCREL;
252b5132
RH
8653 my_getExpression (&offset_expr, s);
8654 s = expr_end;
8655 continue;
8656
8657 case 'u': /* upper 16 bits */
8658 c = my_getSmallExpression (&imm_expr, s);
f6688943 8659 *imm_reloc = BFD_RELOC_LO16;
e7d556df 8660 if (c != S_EX_NONE)
252b5132 8661 {
fb1b3232 8662 if (c != S_EX_LO)
252b5132 8663 {
fdb987ee 8664 if (c == S_EX_HI)
252b5132 8665 {
f6688943 8666 *imm_reloc = BFD_RELOC_HI16_S;
252b5132
RH
8667 imm_unmatched_hi = true;
8668 }
645dc66c
TS
8669#ifdef OBJ_ELF
8670 else if (c == S_EX_HIGHEST)
98d3f06f 8671 *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
ad8d3bb3
TS
8672 else if (c == S_EX_GP_REL)
8673 {
8674 /* This occurs in NewABI only. */
8675 c = my_getSmallExpression (&imm_expr, s);
8676 if (c != S_EX_NEG)
8677 as_bad (_("bad composition of relocations"));
8678 else
8679 {
8680 c = my_getSmallExpression (&imm_expr, s);
8681 if (c != S_EX_HI)
8682 as_bad (_("bad composition of relocations"));
8683 else
8684 {
8685 imm_reloc[0] = BFD_RELOC_GPREL16;
8686 imm_reloc[1] = BFD_RELOC_MIPS_SUB;
8687 imm_reloc[2] = BFD_RELOC_HI16_S;
8688 }
8689 }
8690 }
8691#endif
252b5132 8692 else
f6688943 8693 *imm_reloc = BFD_RELOC_HI16;
252b5132
RH
8694 }
8695 else if (imm_expr.X_op == O_constant)
8696 imm_expr.X_add_number &= 0xffff;
8697 }
fdb987ee
RS
8698 else if (imm_expr.X_op == O_constant
8699 && (imm_expr.X_add_number < 0
8700 || imm_expr.X_add_number >= 0x10000))
252b5132
RH
8701 as_bad (_("lui expression not in range 0..65535"));
8702 s = expr_end;
8703 continue;
8704
8705 case 'a': /* 26 bit address */
8706 my_getExpression (&offset_expr, s);
8707 s = expr_end;
f6688943 8708 *offset_reloc = BFD_RELOC_MIPS_JMP;
252b5132
RH
8709 continue;
8710
8711 case 'N': /* 3 bit branch condition code */
8712 case 'M': /* 3 bit compare condition code */
8713 if (strncmp (s, "$fcc", 4) != 0)
8714 break;
8715 s += 4;
8716 regno = 0;
8717 do
8718 {
8719 regno *= 10;
8720 regno += *s - '0';
8721 ++s;
8722 }
3882b010 8723 while (ISDIGIT (*s));
252b5132
RH
8724 if (regno > 7)
8725 as_bad (_("invalid condition code register $fcc%d"), regno);
8726 if (*args == 'N')
8727 ip->insn_opcode |= regno << OP_SH_BCC;
8728 else
8729 ip->insn_opcode |= regno << OP_SH_CCC;
beae10d5 8730 continue;
252b5132 8731
156c2f8b
NC
8732 case 'H':
8733 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
8734 s += 2;
3882b010 8735 if (ISDIGIT (*s))
156c2f8b
NC
8736 {
8737 c = 0;
8738 do
8739 {
8740 c *= 10;
8741 c += *s - '0';
8742 ++s;
8743 }
3882b010 8744 while (ISDIGIT (*s));
156c2f8b
NC
8745 }
8746 else
8747 c = 8; /* Invalid sel value. */
8748
8749 if (c > 7)
8750 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
8751 ip->insn_opcode |= c;
8752 continue;
8753
252b5132
RH
8754 default:
8755 as_bad (_("bad char = '%c'\n"), *args);
8756 internalError ();
8757 }
8758 break;
8759 }
8760 /* Args don't match. */
8761 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8762 !strcmp (insn->name, insn[1].name))
8763 {
8764 ++insn;
8765 s = argsStart;
268f6bed 8766 insn_error = _("illegal operands");
252b5132
RH
8767 continue;
8768 }
268f6bed
L
8769 if (save_c)
8770 *(--s) = save_c;
252b5132
RH
8771 insn_error = _("illegal operands");
8772 return;
8773 }
8774}
8775
8776/* This routine assembles an instruction into its binary format when
8777 assembling for the mips16. As a side effect, it sets one of the
8778 global variables imm_reloc or offset_reloc to the type of
8779 relocation to do if one of the operands is an address expression.
8780 It also sets mips16_small and mips16_ext if the user explicitly
8781 requested a small or extended instruction. */
8782
8783static void
8784mips16_ip (str, ip)
8785 char *str;
8786 struct mips_cl_insn *ip;
8787{
8788 char *s;
8789 const char *args;
8790 struct mips_opcode *insn;
8791 char *argsstart;
8792 unsigned int regno;
8793 unsigned int lastregno = 0;
8794 char *s_reset;
8795
8796 insn_error = NULL;
8797
8798 mips16_small = false;
8799 mips16_ext = false;
8800
3882b010 8801 for (s = str; ISLOWER (*s); ++s)
252b5132
RH
8802 ;
8803 switch (*s)
8804 {
8805 case '\0':
8806 break;
8807
8808 case ' ':
8809 *s++ = '\0';
8810 break;
8811
8812 case '.':
8813 if (s[1] == 't' && s[2] == ' ')
8814 {
8815 *s = '\0';
8816 mips16_small = true;
8817 s += 3;
8818 break;
8819 }
8820 else if (s[1] == 'e' && s[2] == ' ')
8821 {
8822 *s = '\0';
8823 mips16_ext = true;
8824 s += 3;
8825 break;
8826 }
8827 /* Fall through. */
8828 default:
8829 insn_error = _("unknown opcode");
8830 return;
8831 }
8832
8833 if (mips_opts.noautoextend && ! mips16_ext)
8834 mips16_small = true;
8835
8836 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
8837 {
8838 insn_error = _("unrecognized opcode");
8839 return;
8840 }
8841
8842 argsstart = s;
8843 for (;;)
8844 {
8845 assert (strcmp (insn->name, str) == 0);
8846
8847 ip->insn_mo = insn;
8848 ip->insn_opcode = insn->match;
8849 ip->use_extend = false;
8850 imm_expr.X_op = O_absent;
f6688943
TS
8851 imm_reloc[0] = BFD_RELOC_UNUSED;
8852 imm_reloc[1] = BFD_RELOC_UNUSED;
8853 imm_reloc[2] = BFD_RELOC_UNUSED;
252b5132 8854 offset_expr.X_op = O_absent;
f6688943
TS
8855 offset_reloc[0] = BFD_RELOC_UNUSED;
8856 offset_reloc[1] = BFD_RELOC_UNUSED;
8857 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
8858 for (args = insn->args; 1; ++args)
8859 {
8860 int c;
8861
8862 if (*s == ' ')
8863 ++s;
8864
8865 /* In this switch statement we call break if we did not find
8866 a match, continue if we did find a match, or return if we
8867 are done. */
8868
8869 c = *args;
8870 switch (c)
8871 {
8872 case '\0':
8873 if (*s == '\0')
8874 {
8875 /* Stuff the immediate value in now, if we can. */
8876 if (imm_expr.X_op == O_constant
f6688943 8877 && *imm_reloc > BFD_RELOC_UNUSED
252b5132
RH
8878 && insn->pinfo != INSN_MACRO)
8879 {
c4e7957c 8880 mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
252b5132
RH
8881 imm_expr.X_add_number, true, mips16_small,
8882 mips16_ext, &ip->insn_opcode,
8883 &ip->use_extend, &ip->extend);
8884 imm_expr.X_op = O_absent;
f6688943 8885 *imm_reloc = BFD_RELOC_UNUSED;
252b5132
RH
8886 }
8887
8888 return;
8889 }
8890 break;
8891
8892 case ',':
8893 if (*s++ == c)
8894 continue;
8895 s--;
8896 switch (*++args)
8897 {
8898 case 'v':
8899 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
8900 continue;
8901 case 'w':
8902 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
8903 continue;
8904 }
8905 break;
8906
8907 case '(':
8908 case ')':
8909 if (*s++ == c)
8910 continue;
8911 break;
8912
8913 case 'v':
8914 case 'w':
8915 if (s[0] != '$')
8916 {
8917 if (c == 'v')
8918 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
8919 else
8920 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
8921 ++args;
8922 continue;
8923 }
8924 /* Fall through. */
8925 case 'x':
8926 case 'y':
8927 case 'z':
8928 case 'Z':
8929 case '0':
8930 case 'S':
8931 case 'R':
8932 case 'X':
8933 case 'Y':
8934 if (s[0] != '$')
8935 break;
8936 s_reset = s;
3882b010 8937 if (ISDIGIT (s[1]))
252b5132
RH
8938 {
8939 ++s;
8940 regno = 0;
8941 do
8942 {
8943 regno *= 10;
8944 regno += *s - '0';
8945 ++s;
8946 }
3882b010 8947 while (ISDIGIT (*s));
252b5132
RH
8948 if (regno > 31)
8949 {
8950 as_bad (_("invalid register number (%d)"), regno);
8951 regno = 2;
8952 }
8953 }
8954 else
8955 {
76db943d
TS
8956 if (s[1] == 'r' && s[2] == 'a')
8957 {
8958 s += 3;
8959 regno = RA;
8960 }
8961 else if (s[1] == 'f' && s[2] == 'p')
252b5132
RH
8962 {
8963 s += 3;
8964 regno = FP;
8965 }
8966 else if (s[1] == 's' && s[2] == 'p')
8967 {
8968 s += 3;
8969 regno = SP;
8970 }
8971 else if (s[1] == 'g' && s[2] == 'p')
8972 {
8973 s += 3;
8974 regno = GP;
8975 }
8976 else if (s[1] == 'a' && s[2] == 't')
8977 {
8978 s += 3;
8979 regno = AT;
8980 }
8981 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8982 {
8983 s += 4;
8984 regno = KT0;
8985 }
8986 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8987 {
8988 s += 4;
8989 regno = KT1;
8990 }
85b51719
TS
8991 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
8992 {
8993 s += 5;
8994 regno = ZERO;
8995 }
252b5132
RH
8996 else
8997 break;
8998 }
8999
9000 if (*s == ' ')
9001 ++s;
9002 if (args[1] != *s)
9003 {
9004 if (c == 'v' || c == 'w')
9005 {
9006 regno = mips16_to_32_reg_map[lastregno];
9007 s = s_reset;
f9419b05 9008 ++args;
252b5132
RH
9009 }
9010 }
9011
9012 switch (c)
9013 {
9014 case 'x':
9015 case 'y':
9016 case 'z':
9017 case 'v':
9018 case 'w':
9019 case 'Z':
9020 regno = mips32_to_16_reg_map[regno];
9021 break;
9022
9023 case '0':
9024 if (regno != 0)
9025 regno = ILLEGAL_REG;
9026 break;
9027
9028 case 'S':
9029 if (regno != SP)
9030 regno = ILLEGAL_REG;
9031 break;
9032
9033 case 'R':
9034 if (regno != RA)
9035 regno = ILLEGAL_REG;
9036 break;
9037
9038 case 'X':
9039 case 'Y':
9040 if (regno == AT && ! mips_opts.noat)
9041 as_warn (_("used $at without \".set noat\""));
9042 break;
9043
9044 default:
9045 internalError ();
9046 }
9047
9048 if (regno == ILLEGAL_REG)
9049 break;
9050
9051 switch (c)
9052 {
9053 case 'x':
9054 case 'v':
9055 ip->insn_opcode |= regno << MIPS16OP_SH_RX;
9056 break;
9057 case 'y':
9058 case 'w':
9059 ip->insn_opcode |= regno << MIPS16OP_SH_RY;
9060 break;
9061 case 'z':
9062 ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
9063 break;
9064 case 'Z':
9065 ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
9066 case '0':
9067 case 'S':
9068 case 'R':
9069 break;
9070 case 'X':
9071 ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
9072 break;
9073 case 'Y':
9074 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
9075 ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
9076 break;
9077 default:
9078 internalError ();
9079 }
9080
9081 lastregno = regno;
9082 continue;
9083
9084 case 'P':
9085 if (strncmp (s, "$pc", 3) == 0)
9086 {
9087 s += 3;
9088 continue;
9089 }
9090 break;
9091
9092 case '<':
9093 case '>':
9094 case '[':
9095 case ']':
9096 case '4':
9097 case '5':
9098 case 'H':
9099 case 'W':
9100 case 'D':
9101 case 'j':
9102 case '8':
9103 case 'V':
9104 case 'C':
9105 case 'U':
9106 case 'k':
9107 case 'K':
9108 if (s[0] == '%'
9109 && strncmp (s + 1, "gprel(", sizeof "gprel(" - 1) == 0)
9110 {
9111 /* This is %gprel(SYMBOL). We need to read SYMBOL,
9112 and generate the appropriate reloc. If the text
9113 inside %gprel is not a symbol name with an
9114 optional offset, then we generate a normal reloc
9115 and will probably fail later. */
9116 my_getExpression (&imm_expr, s + sizeof "%gprel" - 1);
9117 if (imm_expr.X_op == O_symbol)
9118 {
9119 mips16_ext = true;
f6688943 9120 *imm_reloc = BFD_RELOC_MIPS16_GPREL;
252b5132
RH
9121 s = expr_end;
9122 ip->use_extend = true;
9123 ip->extend = 0;
9124 continue;
9125 }
9126 }
9127 else
9128 {
9129 /* Just pick up a normal expression. */
9130 my_getExpression (&imm_expr, s);
9131 }
9132
9133 if (imm_expr.X_op == O_register)
9134 {
9135 /* What we thought was an expression turned out to
9136 be a register. */
9137
9138 if (s[0] == '(' && args[1] == '(')
9139 {
9140 /* It looks like the expression was omitted
9141 before a register indirection, which means
9142 that the expression is implicitly zero. We
9143 still set up imm_expr, so that we handle
9144 explicit extensions correctly. */
9145 imm_expr.X_op = O_constant;
9146 imm_expr.X_add_number = 0;
f6688943 9147 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
9148 continue;
9149 }
9150
9151 break;
9152 }
9153
9154 /* We need to relax this instruction. */
f6688943 9155 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
9156 s = expr_end;
9157 continue;
9158
9159 case 'p':
9160 case 'q':
9161 case 'A':
9162 case 'B':
9163 case 'E':
9164 /* We use offset_reloc rather than imm_reloc for the PC
9165 relative operands. This lets macros with both
9166 immediate and address operands work correctly. */
9167 my_getExpression (&offset_expr, s);
9168
9169 if (offset_expr.X_op == O_register)
9170 break;
9171
9172 /* We need to relax this instruction. */
f6688943 9173 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
9174 s = expr_end;
9175 continue;
9176
9177 case '6': /* break code */
9178 my_getExpression (&imm_expr, s);
9179 check_absolute_expr (ip, &imm_expr);
9180 if ((unsigned long) imm_expr.X_add_number > 63)
9181 {
9182 as_warn (_("Invalid value for `%s' (%lu)"),
9183 ip->insn_mo->name,
9184 (unsigned long) imm_expr.X_add_number);
9185 imm_expr.X_add_number &= 0x3f;
9186 }
9187 ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
9188 imm_expr.X_op = O_absent;
9189 s = expr_end;
9190 continue;
9191
9192 case 'a': /* 26 bit address */
9193 my_getExpression (&offset_expr, s);
9194 s = expr_end;
f6688943 9195 *offset_reloc = BFD_RELOC_MIPS16_JMP;
252b5132
RH
9196 ip->insn_opcode <<= 16;
9197 continue;
9198
9199 case 'l': /* register list for entry macro */
9200 case 'L': /* register list for exit macro */
9201 {
9202 int mask;
9203
9204 if (c == 'l')
9205 mask = 0;
9206 else
9207 mask = 7 << 3;
9208 while (*s != '\0')
9209 {
9210 int freg, reg1, reg2;
9211
9212 while (*s == ' ' || *s == ',')
9213 ++s;
9214 if (*s != '$')
9215 {
9216 as_bad (_("can't parse register list"));
9217 break;
9218 }
9219 ++s;
9220 if (*s != 'f')
9221 freg = 0;
9222 else
9223 {
9224 freg = 1;
9225 ++s;
9226 }
9227 reg1 = 0;
3882b010 9228 while (ISDIGIT (*s))
252b5132
RH
9229 {
9230 reg1 *= 10;
9231 reg1 += *s - '0';
9232 ++s;
9233 }
9234 if (*s == ' ')
9235 ++s;
9236 if (*s != '-')
9237 reg2 = reg1;
9238 else
9239 {
9240 ++s;
9241 if (*s != '$')
9242 break;
9243 ++s;
9244 if (freg)
9245 {
9246 if (*s == 'f')
9247 ++s;
9248 else
9249 {
9250 as_bad (_("invalid register list"));
9251 break;
9252 }
9253 }
9254 reg2 = 0;
3882b010 9255 while (ISDIGIT (*s))
252b5132
RH
9256 {
9257 reg2 *= 10;
9258 reg2 += *s - '0';
9259 ++s;
9260 }
9261 }
9262 if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
9263 {
9264 mask &= ~ (7 << 3);
9265 mask |= 5 << 3;
9266 }
9267 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
9268 {
9269 mask &= ~ (7 << 3);
9270 mask |= 6 << 3;
9271 }
9272 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
9273 mask |= (reg2 - 3) << 3;
9274 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
9275 mask |= (reg2 - 15) << 1;
f9419b05 9276 else if (reg1 == RA && reg2 == RA)
252b5132
RH
9277 mask |= 1;
9278 else
9279 {
9280 as_bad (_("invalid register list"));
9281 break;
9282 }
9283 }
9284 /* The mask is filled in in the opcode table for the
9285 benefit of the disassembler. We remove it before
9286 applying the actual mask. */
9287 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
9288 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
9289 }
9290 continue;
9291
9292 case 'e': /* extend code */
9293 my_getExpression (&imm_expr, s);
9294 check_absolute_expr (ip, &imm_expr);
9295 if ((unsigned long) imm_expr.X_add_number > 0x7ff)
9296 {
9297 as_warn (_("Invalid value for `%s' (%lu)"),
9298 ip->insn_mo->name,
9299 (unsigned long) imm_expr.X_add_number);
9300 imm_expr.X_add_number &= 0x7ff;
9301 }
9302 ip->insn_opcode |= imm_expr.X_add_number;
9303 imm_expr.X_op = O_absent;
9304 s = expr_end;
9305 continue;
9306
9307 default:
9308 internalError ();
9309 }
9310 break;
9311 }
9312
9313 /* Args don't match. */
9314 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
9315 strcmp (insn->name, insn[1].name) == 0)
9316 {
9317 ++insn;
9318 s = argsstart;
9319 continue;
9320 }
9321
9322 insn_error = _("illegal operands");
9323
9324 return;
9325 }
9326}
9327
9328/* This structure holds information we know about a mips16 immediate
9329 argument type. */
9330
e972090a
NC
9331struct mips16_immed_operand
9332{
252b5132
RH
9333 /* The type code used in the argument string in the opcode table. */
9334 int type;
9335 /* The number of bits in the short form of the opcode. */
9336 int nbits;
9337 /* The number of bits in the extended form of the opcode. */
9338 int extbits;
9339 /* The amount by which the short form is shifted when it is used;
9340 for example, the sw instruction has a shift count of 2. */
9341 int shift;
9342 /* The amount by which the short form is shifted when it is stored
9343 into the instruction code. */
9344 int op_shift;
9345 /* Non-zero if the short form is unsigned. */
9346 int unsp;
9347 /* Non-zero if the extended form is unsigned. */
9348 int extu;
9349 /* Non-zero if the value is PC relative. */
9350 int pcrel;
9351};
9352
9353/* The mips16 immediate operand types. */
9354
9355static const struct mips16_immed_operand mips16_immed_operands[] =
9356{
9357 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9358 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9359 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9360 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9361 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
9362 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
9363 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
9364 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
9365 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
9366 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
9367 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
9368 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
9369 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
9370 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
9371 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
9372 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
9373 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9374 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9375 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
9376 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
9377 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
9378};
9379
9380#define MIPS16_NUM_IMMED \
9381 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
9382
9383/* Handle a mips16 instruction with an immediate value. This or's the
9384 small immediate value into *INSN. It sets *USE_EXTEND to indicate
9385 whether an extended value is needed; if one is needed, it sets
9386 *EXTEND to the value. The argument type is TYPE. The value is VAL.
9387 If SMALL is true, an unextended opcode was explicitly requested.
9388 If EXT is true, an extended opcode was explicitly requested. If
9389 WARN is true, warn if EXT does not match reality. */
9390
9391static void
9392mips16_immed (file, line, type, val, warn, small, ext, insn, use_extend,
9393 extend)
9394 char *file;
9395 unsigned int line;
9396 int type;
9397 offsetT val;
9398 boolean warn;
9399 boolean small;
9400 boolean ext;
9401 unsigned long *insn;
9402 boolean *use_extend;
9403 unsigned short *extend;
9404{
9405 register const struct mips16_immed_operand *op;
9406 int mintiny, maxtiny;
9407 boolean needext;
9408
9409 op = mips16_immed_operands;
9410 while (op->type != type)
9411 {
9412 ++op;
9413 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
9414 }
9415
9416 if (op->unsp)
9417 {
9418 if (type == '<' || type == '>' || type == '[' || type == ']')
9419 {
9420 mintiny = 1;
9421 maxtiny = 1 << op->nbits;
9422 }
9423 else
9424 {
9425 mintiny = 0;
9426 maxtiny = (1 << op->nbits) - 1;
9427 }
9428 }
9429 else
9430 {
9431 mintiny = - (1 << (op->nbits - 1));
9432 maxtiny = (1 << (op->nbits - 1)) - 1;
9433 }
9434
9435 /* Branch offsets have an implicit 0 in the lowest bit. */
9436 if (type == 'p' || type == 'q')
9437 val /= 2;
9438
9439 if ((val & ((1 << op->shift) - 1)) != 0
9440 || val < (mintiny << op->shift)
9441 || val > (maxtiny << op->shift))
9442 needext = true;
9443 else
9444 needext = false;
9445
9446 if (warn && ext && ! needext)
beae10d5
KH
9447 as_warn_where (file, line,
9448 _("extended operand requested but not required"));
252b5132
RH
9449 if (small && needext)
9450 as_bad_where (file, line, _("invalid unextended operand value"));
9451
9452 if (small || (! ext && ! needext))
9453 {
9454 int insnval;
9455
9456 *use_extend = false;
9457 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
9458 insnval <<= op->op_shift;
9459 *insn |= insnval;
9460 }
9461 else
9462 {
9463 long minext, maxext;
9464 int extval;
9465
9466 if (op->extu)
9467 {
9468 minext = 0;
9469 maxext = (1 << op->extbits) - 1;
9470 }
9471 else
9472 {
9473 minext = - (1 << (op->extbits - 1));
9474 maxext = (1 << (op->extbits - 1)) - 1;
9475 }
9476 if (val < minext || val > maxext)
9477 as_bad_where (file, line,
9478 _("operand value out of range for instruction"));
9479
9480 *use_extend = true;
9481 if (op->extbits == 16)
9482 {
9483 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
9484 val &= 0x1f;
9485 }
9486 else if (op->extbits == 15)
9487 {
9488 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
9489 val &= 0xf;
9490 }
9491 else
9492 {
9493 extval = ((val & 0x1f) << 6) | (val & 0x20);
9494 val = 0;
9495 }
9496
9497 *extend = (unsigned short) extval;
9498 *insn |= val;
9499 }
9500}
9501\f
ad8d3bb3
TS
9502static struct percent_op_match
9503{
9504 const char *str;
9505 const enum small_ex_type type;
9506} percent_op[] =
9507{
ad8d3bb3
TS
9508 {"%lo", S_EX_LO},
9509#ifdef OBJ_ELF
394f9b3a
TS
9510 {"%call_hi", S_EX_CALL_HI},
9511 {"%call_lo", S_EX_CALL_LO},
ad8d3bb3
TS
9512 {"%call16", S_EX_CALL16},
9513 {"%got_disp", S_EX_GOT_DISP},
9514 {"%got_page", S_EX_GOT_PAGE},
9515 {"%got_ofst", S_EX_GOT_OFST},
9516 {"%got_hi", S_EX_GOT_HI},
9517 {"%got_lo", S_EX_GOT_LO},
394f9b3a
TS
9518 {"%got", S_EX_GOT},
9519 {"%gp_rel", S_EX_GP_REL},
9520 {"%half", S_EX_HALF},
ad8d3bb3 9521 {"%highest", S_EX_HIGHEST},
394f9b3a
TS
9522 {"%higher", S_EX_HIGHER},
9523 {"%neg", S_EX_NEG},
ad8d3bb3 9524#endif
394f9b3a 9525 {"%hi", S_EX_HI}
ad8d3bb3
TS
9526};
9527
9528/* Parse small expression input. STR gets adjusted to eat up whitespace.
9529 It detects valid "%percent_op(...)" and "($reg)" strings. Percent_op's
9530 can be nested, this is handled by blanking the innermost, parsing the
9531 rest by subsequent calls. */
252b5132
RH
9532
9533static int
ad8d3bb3
TS
9534my_getSmallParser (str, len, nestlevel)
9535 char **str;
9536 unsigned int *len;
9537 int *nestlevel;
252b5132 9538{
ad8d3bb3
TS
9539 *len = 0;
9540 *str += strspn (*str, " \t");
394f9b3a 9541 /* Check for expression in parentheses. */
ad8d3bb3 9542 if (**str == '(')
252b5132 9543 {
ad8d3bb3
TS
9544 char *b = *str + 1 + strspn (*str + 1, " \t");
9545 char *e;
9546
9547 /* Check for base register. */
9548 if (b[0] == '$')
9549 {
9550 if (strchr (b, ')')
9551 && (e = b + strcspn (b, ") \t"))
9552 && e - b > 1 && e - b < 4)
9553 {
98d3f06f
KH
9554 if ((e - b == 3
9555 && ((b[1] == 'f' && b[2] == 'p')
9556 || (b[1] == 's' && b[2] == 'p')
9557 || (b[1] == 'g' && b[2] == 'p')
9558 || (b[1] == 'a' && b[2] == 't')
9559 || (ISDIGIT (b[1])
9560 && ISDIGIT (b[2]))))
9561 || (ISDIGIT (b[1])))
9562 {
9563 *len = strcspn (*str, ")") + 1;
9564 return S_EX_REGISTER;
9565 }
ad8d3bb3
TS
9566 }
9567 }
394f9b3a 9568 /* Check for percent_op (in parentheses). */
ad8d3bb3
TS
9569 else if (b[0] == '%')
9570 {
9571 *str = b;
394f9b3a 9572 return my_getPercentOp (str, len, nestlevel);
ad8d3bb3 9573 }
76b3015f 9574
394f9b3a
TS
9575 /* Some other expression in the parentheses, which can contain
9576 parentheses itself. Attempt to find the matching one. */
9577 {
9578 int pcnt = 1;
9579 char *s;
9580
9581 *len = 1;
9582 for (s = *str + 1; *s && pcnt; s++, (*len)++)
9583 {
9584 if (*s == '(')
f9419b05 9585 ++pcnt;
394f9b3a 9586 else if (*s == ')')
f9419b05 9587 --pcnt;
394f9b3a
TS
9588 }
9589 }
fb1b3232 9590 }
394f9b3a 9591 /* Check for percent_op (outside of parentheses). */
ad8d3bb3 9592 else if (*str[0] == '%')
394f9b3a
TS
9593 return my_getPercentOp (str, len, nestlevel);
9594
9595 /* Any other expression. */
9596 return S_EX_NONE;
9597}
ad8d3bb3 9598
394f9b3a
TS
9599static int
9600my_getPercentOp (str, len, nestlevel)
9601 char **str;
9602 unsigned int *len;
9603 int *nestlevel;
9604{
9605 char *tmp = *str + 1;
9606 unsigned int i = 0;
ad8d3bb3 9607
394f9b3a
TS
9608 while (ISALPHA (*tmp) || *tmp == '_')
9609 {
9610 *tmp = TOLOWER (*tmp);
9611 tmp++;
9612 }
9613 while (i < (sizeof (percent_op) / sizeof (struct percent_op_match)))
9614 {
9615 if (strncmp (*str, percent_op[i].str, strlen (percent_op[i].str)))
98d3f06f 9616 i++;
394f9b3a 9617 else
ad8d3bb3 9618 {
394f9b3a 9619 int type = percent_op[i].type;
ad8d3bb3 9620
394f9b3a
TS
9621 /* Only %hi and %lo are allowed for OldABI. */
9622 if (! HAVE_NEWABI && type != S_EX_HI && type != S_EX_LO)
9623 return S_EX_NONE;
ad8d3bb3 9624
394f9b3a 9625 *len = strlen (percent_op[i].str);
f9419b05 9626 ++(*nestlevel);
394f9b3a 9627 return type;
ad8d3bb3 9628 }
fb1b3232 9629 }
ad8d3bb3
TS
9630 return S_EX_NONE;
9631}
9632
9633static int
9634my_getSmallExpression (ep, str)
9635 expressionS *ep;
9636 char *str;
9637{
9638 static char *oldstr = NULL;
9639 int c = S_EX_NONE;
9640 int oldc;
394f9b3a 9641 int nestlevel = -1;
ad8d3bb3
TS
9642 unsigned int len;
9643
394f9b3a
TS
9644 /* Don't update oldstr if the last call had nested percent_op's. We need
9645 it to parse the outer ones later. */
ad8d3bb3
TS
9646 if (! oldstr)
9647 oldstr = str;
76b3015f 9648
ad8d3bb3 9649 do
fb1b3232 9650 {
ad8d3bb3 9651 oldc = c;
394f9b3a 9652 c = my_getSmallParser (&str, &len, &nestlevel);
ad8d3bb3
TS
9653 if (c != S_EX_NONE && c != S_EX_REGISTER)
9654 str += len;
fb1b3232 9655 }
ad8d3bb3
TS
9656 while (c != S_EX_NONE && c != S_EX_REGISTER);
9657
394f9b3a 9658 if (nestlevel >= 0)
fb1b3232 9659 {
394f9b3a
TS
9660 /* A percent_op was encountered. Don't try to get an expression if
9661 it is already blanked out. */
ad8d3bb3
TS
9662 if (*(str + strspn (str + 1, " )")) != ')')
9663 {
9664 char save;
9665
394f9b3a 9666 /* Let my_getExpression() stop at the closing parenthesis. */
ad8d3bb3
TS
9667 save = *(str + len);
9668 *(str + len) = '\0';
9669 my_getExpression (ep, str);
9670 *(str + len) = save;
9671 }
394f9b3a 9672 if (nestlevel > 0)
ad8d3bb3 9673 {
394f9b3a
TS
9674 /* Blank out including the % sign and the proper matching
9675 parenthesis. */
9676 int pcnt = 1;
9677 char *s = strrchr (oldstr, '%');
9678 char *end;
9679
9680 for (end = strchr (s, '(') + 1; *end && pcnt; end++)
9681 {
9682 if (*end == '(')
f9419b05 9683 ++pcnt;
394f9b3a 9684 else if (*end == ')')
f9419b05 9685 --pcnt;
394f9b3a
TS
9686 }
9687
9688 memset (s, ' ', end - s);
ad8d3bb3
TS
9689 str = oldstr;
9690 }
9691 else
394f9b3a
TS
9692 expr_end = str + len;
9693
ad8d3bb3 9694 c = oldc;
fb1b3232 9695 }
ad8d3bb3 9696 else if (c == S_EX_NONE)
fb1b3232 9697 {
ad8d3bb3 9698 my_getExpression (ep, str);
fb1b3232 9699 }
ad8d3bb3 9700 else if (c == S_EX_REGISTER)
fb1b3232 9701 {
ad8d3bb3
TS
9702 ep->X_op = O_constant;
9703 expr_end = str;
9704 ep->X_add_symbol = NULL;
9705 ep->X_op_symbol = NULL;
9706 ep->X_add_number = 0;
fb1b3232 9707 }
fb1b3232
TS
9708 else
9709 {
98d3f06f 9710 as_fatal (_("internal error"));
fb1b3232 9711 }
252b5132 9712
394f9b3a
TS
9713 if (nestlevel <= 0)
9714 /* All percent_op's have been handled. */
ad8d3bb3 9715 oldstr = NULL;
fb1b3232 9716
fb1b3232 9717 return c;
252b5132
RH
9718}
9719
9720static void
9721my_getExpression (ep, str)
9722 expressionS *ep;
9723 char *str;
9724{
9725 char *save_in;
98aa84af 9726 valueT val;
252b5132
RH
9727
9728 save_in = input_line_pointer;
9729 input_line_pointer = str;
9730 expression (ep);
9731 expr_end = input_line_pointer;
9732 input_line_pointer = save_in;
9733
9734 /* If we are in mips16 mode, and this is an expression based on `.',
9735 then we bump the value of the symbol by 1 since that is how other
9736 text symbols are handled. We don't bother to handle complex
9737 expressions, just `.' plus or minus a constant. */
9738 if (mips_opts.mips16
9739 && ep->X_op == O_symbol
9740 && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
9741 && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
49309057
ILT
9742 && symbol_get_frag (ep->X_add_symbol) == frag_now
9743 && symbol_constant_p (ep->X_add_symbol)
98aa84af
AM
9744 && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
9745 S_SET_VALUE (ep->X_add_symbol, val + 1);
252b5132
RH
9746}
9747
9748/* Turn a string in input_line_pointer into a floating point constant
bc0d738a
NC
9749 of type TYPE, and store the appropriate bytes in *LITP. The number
9750 of LITTLENUMS emitted is stored in *SIZEP. An error message is
252b5132
RH
9751 returned, or NULL on OK. */
9752
9753char *
9754md_atof (type, litP, sizeP)
9755 int type;
9756 char *litP;
9757 int *sizeP;
9758{
9759 int prec;
9760 LITTLENUM_TYPE words[4];
9761 char *t;
9762 int i;
9763
9764 switch (type)
9765 {
9766 case 'f':
9767 prec = 2;
9768 break;
9769
9770 case 'd':
9771 prec = 4;
9772 break;
9773
9774 default:
9775 *sizeP = 0;
9776 return _("bad call to md_atof");
9777 }
9778
9779 t = atof_ieee (input_line_pointer, type, words);
9780 if (t)
9781 input_line_pointer = t;
9782
9783 *sizeP = prec * 2;
9784
9785 if (! target_big_endian)
9786 {
9787 for (i = prec - 1; i >= 0; i--)
9788 {
9789 md_number_to_chars (litP, (valueT) words[i], 2);
9790 litP += 2;
9791 }
9792 }
9793 else
9794 {
9795 for (i = 0; i < prec; i++)
9796 {
9797 md_number_to_chars (litP, (valueT) words[i], 2);
9798 litP += 2;
9799 }
9800 }
bdaaa2e1 9801
252b5132
RH
9802 return NULL;
9803}
9804
9805void
9806md_number_to_chars (buf, val, n)
9807 char *buf;
9808 valueT val;
9809 int n;
9810{
9811 if (target_big_endian)
9812 number_to_chars_bigendian (buf, val, n);
9813 else
9814 number_to_chars_littleendian (buf, val, n);
9815}
9816\f
ae948b86 9817#ifdef OBJ_ELF
e013f690
TS
9818static int support_64bit_objects(void)
9819{
9820 const char **list, **l;
9821
9822 list = bfd_target_list ();
9823 for (l = list; *l != NULL; l++)
9824#ifdef TE_TMIPS
9825 /* This is traditional mips */
9826 if (strcmp (*l, "elf64-tradbigmips") == 0
9827 || strcmp (*l, "elf64-tradlittlemips") == 0)
9828#else
9829 if (strcmp (*l, "elf64-bigmips") == 0
9830 || strcmp (*l, "elf64-littlemips") == 0)
9831#endif
9832 break;
9833 free (list);
9834 return (*l != NULL);
9835}
ae948b86 9836#endif /* OBJ_ELF */
e013f690 9837
5a38dc70 9838const char *md_shortopts = "nO::g::G:";
252b5132 9839
e972090a
NC
9840struct option md_longopts[] =
9841{
252b5132
RH
9842#define OPTION_MIPS1 (OPTION_MD_BASE + 1)
9843 {"mips0", no_argument, NULL, OPTION_MIPS1},
9844 {"mips1", no_argument, NULL, OPTION_MIPS1},
9845#define OPTION_MIPS2 (OPTION_MD_BASE + 2)
9846 {"mips2", no_argument, NULL, OPTION_MIPS2},
9847#define OPTION_MIPS3 (OPTION_MD_BASE + 3)
9848 {"mips3", no_argument, NULL, OPTION_MIPS3},
9849#define OPTION_MIPS4 (OPTION_MD_BASE + 4)
9850 {"mips4", no_argument, NULL, OPTION_MIPS4},
ae948b86
TS
9851#define OPTION_MIPS5 (OPTION_MD_BASE + 5)
9852 {"mips5", no_argument, NULL, OPTION_MIPS5},
9853#define OPTION_MIPS32 (OPTION_MD_BASE + 6)
9854 {"mips32", no_argument, NULL, OPTION_MIPS32},
9855#define OPTION_MIPS64 (OPTION_MD_BASE + 7)
9856 {"mips64", no_argument, NULL, OPTION_MIPS64},
9857#define OPTION_MEMBEDDED_PIC (OPTION_MD_BASE + 8)
252b5132 9858 {"membedded-pic", no_argument, NULL, OPTION_MEMBEDDED_PIC},
ae948b86 9859#define OPTION_TRAP (OPTION_MD_BASE + 9)
252b5132
RH
9860 {"trap", no_argument, NULL, OPTION_TRAP},
9861 {"no-break", no_argument, NULL, OPTION_TRAP},
ae948b86 9862#define OPTION_BREAK (OPTION_MD_BASE + 10)
252b5132
RH
9863 {"break", no_argument, NULL, OPTION_BREAK},
9864 {"no-trap", no_argument, NULL, OPTION_BREAK},
ae948b86 9865#define OPTION_EB (OPTION_MD_BASE + 11)
252b5132 9866 {"EB", no_argument, NULL, OPTION_EB},
ae948b86 9867#define OPTION_EL (OPTION_MD_BASE + 12)
252b5132 9868 {"EL", no_argument, NULL, OPTION_EL},
ae948b86 9869#define OPTION_MIPS16 (OPTION_MD_BASE + 13)
252b5132 9870 {"mips16", no_argument, NULL, OPTION_MIPS16},
ae948b86 9871#define OPTION_NO_MIPS16 (OPTION_MD_BASE + 14)
252b5132 9872 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
ae948b86 9873#define OPTION_M7000_HILO_FIX (OPTION_MD_BASE + 15)
6b76fefe 9874 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
9ee72ff1
TS
9875#define OPTION_MNO_7000_HILO_FIX (OPTION_MD_BASE + 16)
9876 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
9877 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
ae948b86
TS
9878#define OPTION_FP32 (OPTION_MD_BASE + 17)
9879 {"mfp32", no_argument, NULL, OPTION_FP32},
9880#define OPTION_GP32 (OPTION_MD_BASE + 18)
c97ef257 9881 {"mgp32", no_argument, NULL, OPTION_GP32},
ae948b86 9882#define OPTION_CONSTRUCT_FLOATS (OPTION_MD_BASE + 19)
119d663a 9883 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
ae948b86 9884#define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MD_BASE + 20)
119d663a 9885 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
ae948b86 9886#define OPTION_MARCH (OPTION_MD_BASE + 21)
ec68c924 9887 {"march", required_argument, NULL, OPTION_MARCH},
ae948b86 9888#define OPTION_MTUNE (OPTION_MD_BASE + 22)
ec68c924 9889 {"mtune", required_argument, NULL, OPTION_MTUNE},
316f5878
RS
9890#define OPTION_FP64 (OPTION_MD_BASE + 23)
9891 {"mfp64", no_argument, NULL, OPTION_FP64},
ae948b86
TS
9892#define OPTION_M4650 (OPTION_MD_BASE + 24)
9893 {"m4650", no_argument, NULL, OPTION_M4650},
9894#define OPTION_NO_M4650 (OPTION_MD_BASE + 25)
9895 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
9896#define OPTION_M4010 (OPTION_MD_BASE + 26)
9897 {"m4010", no_argument, NULL, OPTION_M4010},
9898#define OPTION_NO_M4010 (OPTION_MD_BASE + 27)
9899 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
9900#define OPTION_M4100 (OPTION_MD_BASE + 28)
9901 {"m4100", no_argument, NULL, OPTION_M4100},
9902#define OPTION_NO_M4100 (OPTION_MD_BASE + 29)
9903 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
9904#define OPTION_M3900 (OPTION_MD_BASE + 30)
9905 {"m3900", no_argument, NULL, OPTION_M3900},
9906#define OPTION_NO_M3900 (OPTION_MD_BASE + 31)
9907 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
9908#define OPTION_GP64 (OPTION_MD_BASE + 32)
9909 {"mgp64", no_argument, NULL, OPTION_GP64},
1f25f5d3
CD
9910#define OPTION_MIPS3D (OPTION_MD_BASE + 33)
9911 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
9912#define OPTION_NO_MIPS3D (OPTION_MD_BASE + 34)
9913 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
deec1734
CD
9914#define OPTION_MDMX (OPTION_MD_BASE + 35)
9915 {"mdmx", no_argument, NULL, OPTION_MDMX},
9916#define OPTION_NO_MDMX (OPTION_MD_BASE + 36)
9917 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
156c2f8b 9918#ifdef OBJ_ELF
deec1734 9919#define OPTION_ELF_BASE (OPTION_MD_BASE + 37)
156c2f8b 9920#define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
156c2f8b
NC
9921 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
9922 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
ae948b86 9923#define OPTION_NON_SHARED (OPTION_ELF_BASE + 1)
156c2f8b 9924 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
ae948b86 9925#define OPTION_XGOT (OPTION_ELF_BASE + 2)
156c2f8b 9926 {"xgot", no_argument, NULL, OPTION_XGOT},
ae948b86
TS
9927#define OPTION_MABI (OPTION_ELF_BASE + 3)
9928 {"mabi", required_argument, NULL, OPTION_MABI},
9929#define OPTION_32 (OPTION_ELF_BASE + 4)
156c2f8b 9930 {"32", no_argument, NULL, OPTION_32},
ae948b86 9931#define OPTION_N32 (OPTION_ELF_BASE + 5)
e013f690 9932 {"n32", no_argument, NULL, OPTION_N32},
ae948b86 9933#define OPTION_64 (OPTION_ELF_BASE + 6)
156c2f8b 9934 {"64", no_argument, NULL, OPTION_64},
ecb4347a
DJ
9935#define OPTION_MDEBUG (OPTION_ELF_BASE + 7)
9936 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
9937#define OPTION_NO_MDEBUG (OPTION_ELF_BASE + 8)
9938 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
ae948b86 9939#endif /* OBJ_ELF */
252b5132
RH
9940 {NULL, no_argument, NULL, 0}
9941};
156c2f8b 9942size_t md_longopts_size = sizeof (md_longopts);
252b5132 9943
316f5878
RS
9944/* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
9945 NEW_VALUE. Warn if another value was already specified. Note:
9946 we have to defer parsing the -march and -mtune arguments in order
9947 to handle 'from-abi' correctly, since the ABI might be specified
9948 in a later argument. */
9949
9950static void
9951mips_set_option_string (string_ptr, new_value)
9952 const char **string_ptr, *new_value;
9953{
9954 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
9955 as_warn (_("A different %s was already specified, is now %s"),
9956 string_ptr == &mips_arch_string ? "-march" : "-mtune",
9957 new_value);
9958
9959 *string_ptr = new_value;
9960}
9961
252b5132
RH
9962int
9963md_parse_option (c, arg)
9964 int c;
9965 char *arg;
9966{
9967 switch (c)
9968 {
119d663a
NC
9969 case OPTION_CONSTRUCT_FLOATS:
9970 mips_disable_float_construction = 0;
9971 break;
bdaaa2e1 9972
119d663a
NC
9973 case OPTION_NO_CONSTRUCT_FLOATS:
9974 mips_disable_float_construction = 1;
9975 break;
bdaaa2e1 9976
252b5132
RH
9977 case OPTION_TRAP:
9978 mips_trap = 1;
9979 break;
9980
9981 case OPTION_BREAK:
9982 mips_trap = 0;
9983 break;
9984
9985 case OPTION_EB:
9986 target_big_endian = 1;
9987 break;
9988
9989 case OPTION_EL:
9990 target_big_endian = 0;
9991 break;
9992
39c0a331
L
9993 case 'n':
9994 warn_nops = 1;
9995 break;
9996
252b5132
RH
9997 case 'O':
9998 if (arg && arg[1] == '0')
9999 mips_optimize = 1;
10000 else
10001 mips_optimize = 2;
10002 break;
10003
10004 case 'g':
10005 if (arg == NULL)
10006 mips_debug = 2;
10007 else
10008 mips_debug = atoi (arg);
10009 /* When the MIPS assembler sees -g or -g2, it does not do
10010 optimizations which limit full symbolic debugging. We take
10011 that to be equivalent to -O0. */
10012 if (mips_debug == 2)
10013 mips_optimize = 1;
10014 break;
10015
10016 case OPTION_MIPS1:
316f5878 10017 file_mips_isa = ISA_MIPS1;
252b5132
RH
10018 break;
10019
10020 case OPTION_MIPS2:
316f5878 10021 file_mips_isa = ISA_MIPS2;
252b5132
RH
10022 break;
10023
10024 case OPTION_MIPS3:
316f5878 10025 file_mips_isa = ISA_MIPS3;
252b5132
RH
10026 break;
10027
10028 case OPTION_MIPS4:
316f5878 10029 file_mips_isa = ISA_MIPS4;
e7af610e
NC
10030 break;
10031
84ea6cf2 10032 case OPTION_MIPS5:
316f5878 10033 file_mips_isa = ISA_MIPS5;
84ea6cf2
NC
10034 break;
10035
e7af610e 10036 case OPTION_MIPS32:
316f5878 10037 file_mips_isa = ISA_MIPS32;
252b5132
RH
10038 break;
10039
84ea6cf2 10040 case OPTION_MIPS64:
316f5878 10041 file_mips_isa = ISA_MIPS64;
84ea6cf2
NC
10042 break;
10043
ec68c924 10044 case OPTION_MTUNE:
316f5878
RS
10045 mips_set_option_string (&mips_tune_string, arg);
10046 break;
ec68c924 10047
316f5878
RS
10048 case OPTION_MARCH:
10049 mips_set_option_string (&mips_arch_string, arg);
252b5132
RH
10050 break;
10051
10052 case OPTION_M4650:
316f5878
RS
10053 mips_set_option_string (&mips_arch_string, "4650");
10054 mips_set_option_string (&mips_tune_string, "4650");
252b5132
RH
10055 break;
10056
10057 case OPTION_NO_M4650:
10058 break;
10059
10060 case OPTION_M4010:
316f5878
RS
10061 mips_set_option_string (&mips_arch_string, "4010");
10062 mips_set_option_string (&mips_tune_string, "4010");
252b5132
RH
10063 break;
10064
10065 case OPTION_NO_M4010:
10066 break;
10067
10068 case OPTION_M4100:
316f5878
RS
10069 mips_set_option_string (&mips_arch_string, "4100");
10070 mips_set_option_string (&mips_tune_string, "4100");
252b5132
RH
10071 break;
10072
10073 case OPTION_NO_M4100:
10074 break;
10075
252b5132 10076 case OPTION_M3900:
316f5878
RS
10077 mips_set_option_string (&mips_arch_string, "3900");
10078 mips_set_option_string (&mips_tune_string, "3900");
252b5132 10079 break;
bdaaa2e1 10080
252b5132
RH
10081 case OPTION_NO_M3900:
10082 break;
10083
deec1734
CD
10084 case OPTION_MDMX:
10085 mips_opts.ase_mdmx = 1;
10086 break;
10087
10088 case OPTION_NO_MDMX:
10089 mips_opts.ase_mdmx = 0;
10090 break;
10091
252b5132
RH
10092 case OPTION_MIPS16:
10093 mips_opts.mips16 = 1;
10094 mips_no_prev_insn (false);
10095 break;
10096
10097 case OPTION_NO_MIPS16:
10098 mips_opts.mips16 = 0;
10099 mips_no_prev_insn (false);
10100 break;
10101
1f25f5d3
CD
10102 case OPTION_MIPS3D:
10103 mips_opts.ase_mips3d = 1;
10104 break;
10105
10106 case OPTION_NO_MIPS3D:
10107 mips_opts.ase_mips3d = 0;
10108 break;
10109
252b5132
RH
10110 case OPTION_MEMBEDDED_PIC:
10111 mips_pic = EMBEDDED_PIC;
10112 if (USE_GLOBAL_POINTER_OPT && g_switch_seen)
10113 {
10114 as_bad (_("-G may not be used with embedded PIC code"));
10115 return 0;
10116 }
10117 g_switch_value = 0x7fffffff;
10118 break;
10119
0f074f60 10120#ifdef OBJ_ELF
252b5132
RH
10121 /* When generating ELF code, we permit -KPIC and -call_shared to
10122 select SVR4_PIC, and -non_shared to select no PIC. This is
10123 intended to be compatible with Irix 5. */
10124 case OPTION_CALL_SHARED:
10125 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10126 {
10127 as_bad (_("-call_shared is supported only for ELF format"));
10128 return 0;
10129 }
10130 mips_pic = SVR4_PIC;
10131 if (g_switch_seen && g_switch_value != 0)
10132 {
10133 as_bad (_("-G may not be used with SVR4 PIC code"));
10134 return 0;
10135 }
10136 g_switch_value = 0;
10137 break;
10138
10139 case OPTION_NON_SHARED:
10140 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10141 {
10142 as_bad (_("-non_shared is supported only for ELF format"));
10143 return 0;
10144 }
10145 mips_pic = NO_PIC;
10146 break;
10147
10148 /* The -xgot option tells the assembler to use 32 offsets when
10149 accessing the got in SVR4_PIC mode. It is for Irix
10150 compatibility. */
10151 case OPTION_XGOT:
10152 mips_big_got = 1;
10153 break;
0f074f60 10154#endif /* OBJ_ELF */
252b5132
RH
10155
10156 case 'G':
10157 if (! USE_GLOBAL_POINTER_OPT)
10158 {
10159 as_bad (_("-G is not supported for this configuration"));
10160 return 0;
10161 }
10162 else if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
10163 {
10164 as_bad (_("-G may not be used with SVR4 or embedded PIC code"));
10165 return 0;
10166 }
10167 else
10168 g_switch_value = atoi (arg);
10169 g_switch_seen = 1;
10170 break;
10171
0f074f60 10172#ifdef OBJ_ELF
34ba82a8
TS
10173 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
10174 and -mabi=64. */
252b5132 10175 case OPTION_32:
34ba82a8
TS
10176 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10177 {
10178 as_bad (_("-32 is supported for ELF format only"));
10179 return 0;
10180 }
316f5878 10181 mips_abi = O32_ABI;
252b5132
RH
10182 break;
10183
e013f690 10184 case OPTION_N32:
34ba82a8
TS
10185 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10186 {
10187 as_bad (_("-n32 is supported for ELF format only"));
10188 return 0;
10189 }
316f5878 10190 mips_abi = N32_ABI;
e013f690 10191 break;
252b5132 10192
e013f690 10193 case OPTION_64:
34ba82a8
TS
10194 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10195 {
10196 as_bad (_("-64 is supported for ELF format only"));
10197 return 0;
10198 }
316f5878 10199 mips_abi = N64_ABI;
e013f690
TS
10200 if (! support_64bit_objects())
10201 as_fatal (_("No compiled in support for 64 bit object file format"));
252b5132 10202 break;
ae948b86 10203#endif /* OBJ_ELF */
252b5132 10204
c97ef257 10205 case OPTION_GP32:
a325df1d 10206 file_mips_gp32 = 1;
c97ef257
AH
10207 break;
10208
10209 case OPTION_GP64:
a325df1d 10210 file_mips_gp32 = 0;
c97ef257 10211 break;
252b5132 10212
ca4e0257 10213 case OPTION_FP32:
a325df1d 10214 file_mips_fp32 = 1;
316f5878
RS
10215 break;
10216
10217 case OPTION_FP64:
10218 file_mips_fp32 = 0;
ca4e0257
RS
10219 break;
10220
ae948b86 10221#ifdef OBJ_ELF
252b5132 10222 case OPTION_MABI:
34ba82a8
TS
10223 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10224 {
10225 as_bad (_("-mabi is supported for ELF format only"));
10226 return 0;
10227 }
e013f690 10228 if (strcmp (arg, "32") == 0)
316f5878 10229 mips_abi = O32_ABI;
e013f690 10230 else if (strcmp (arg, "o64") == 0)
316f5878 10231 mips_abi = O64_ABI;
e013f690 10232 else if (strcmp (arg, "n32") == 0)
316f5878 10233 mips_abi = N32_ABI;
e013f690
TS
10234 else if (strcmp (arg, "64") == 0)
10235 {
316f5878 10236 mips_abi = N64_ABI;
e013f690
TS
10237 if (! support_64bit_objects())
10238 as_fatal (_("No compiled in support for 64 bit object file "
10239 "format"));
10240 }
10241 else if (strcmp (arg, "eabi") == 0)
316f5878 10242 mips_abi = EABI_ABI;
e013f690 10243 else
da0e507f
TS
10244 {
10245 as_fatal (_("invalid abi -mabi=%s"), arg);
10246 return 0;
10247 }
252b5132 10248 break;
e013f690 10249#endif /* OBJ_ELF */
252b5132 10250
6b76fefe
CM
10251 case OPTION_M7000_HILO_FIX:
10252 mips_7000_hilo_fix = true;
10253 break;
10254
9ee72ff1 10255 case OPTION_MNO_7000_HILO_FIX:
6b76fefe
CM
10256 mips_7000_hilo_fix = false;
10257 break;
10258
ecb4347a
DJ
10259#ifdef OBJ_ELF
10260 case OPTION_MDEBUG:
10261 mips_flag_mdebug = true;
10262 break;
10263
10264 case OPTION_NO_MDEBUG:
10265 mips_flag_mdebug = false;
10266 break;
10267#endif /* OBJ_ELF */
10268
252b5132
RH
10269 default:
10270 return 0;
10271 }
10272
10273 return 1;
10274}
316f5878
RS
10275\f
10276/* Set up globals to generate code for the ISA or processor
10277 described by INFO. */
252b5132 10278
252b5132 10279static void
316f5878
RS
10280mips_set_architecture (info)
10281 const struct mips_cpu_info *info;
252b5132 10282{
316f5878 10283 if (info != 0)
252b5132 10284 {
316f5878
RS
10285 mips_arch_info = info;
10286 mips_arch = info->cpu;
10287 mips_opts.isa = info->isa;
252b5132 10288 }
252b5132
RH
10289}
10290
252b5132 10291
316f5878 10292/* Likewise for tuning. */
252b5132 10293
316f5878
RS
10294static void
10295mips_set_tune (info)
10296 const struct mips_cpu_info *info;
10297{
10298 if (info != 0)
10299 {
10300 mips_tune_info = info;
10301 mips_tune = info->cpu;
10302 }
10303}
80cc45a5 10304
34ba82a8 10305
252b5132 10306void
e9670677
MR
10307mips_after_parse_args ()
10308{
e9670677
MR
10309 /* GP relative stuff not working for PE */
10310 if (strncmp (TARGET_OS, "pe", 2) == 0
10311 && g_switch_value != 0)
10312 {
10313 if (g_switch_seen)
10314 as_bad (_("-G not supported in this configuration."));
10315 g_switch_value = 0;
10316 }
10317
22923709
RS
10318 /* The following code determines the architecture and register size.
10319 Similar code was added to GCC 3.3 (see override_options() in
10320 config/mips/mips.c). The GAS and GCC code should be kept in sync
10321 as much as possible. */
e9670677 10322
316f5878
RS
10323 if (mips_arch_string != 0)
10324 mips_set_architecture (mips_parse_cpu ("-march", mips_arch_string));
e9670677 10325
316f5878
RS
10326 if (mips_tune_string != 0)
10327 mips_set_tune (mips_parse_cpu ("-mtune", mips_tune_string));
e9670677 10328
316f5878 10329 if (file_mips_isa != ISA_UNKNOWN)
e9670677 10330 {
316f5878
RS
10331 /* Handle -mipsN. At this point, file_mips_isa contains the
10332 ISA level specified by -mipsN, while mips_opts.isa contains
10333 the -march selection (if any). */
10334 if (mips_arch_info != 0)
e9670677 10335 {
316f5878
RS
10336 /* -march takes precedence over -mipsN, since it is more descriptive.
10337 There's no harm in specifying both as long as the ISA levels
10338 are the same. */
10339 if (file_mips_isa != mips_opts.isa)
10340 as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
10341 mips_cpu_info_from_isa (file_mips_isa)->name,
10342 mips_cpu_info_from_isa (mips_opts.isa)->name);
e9670677 10343 }
316f5878
RS
10344 else
10345 mips_set_architecture (mips_cpu_info_from_isa (file_mips_isa));
e9670677
MR
10346 }
10347
316f5878
RS
10348 if (mips_arch_info == 0)
10349 mips_set_architecture (mips_parse_cpu ("default CPU",
10350 MIPS_CPU_STRING_DEFAULT));
e9670677 10351
316f5878
RS
10352 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (mips_opts.isa))
10353 as_bad ("-march=%s is not compatible with the selected ABI",
10354 mips_arch_info->name);
e9670677 10355
316f5878
RS
10356 /* Optimize for mips_arch, unless -mtune selects a different processor. */
10357 if (mips_tune_info == 0)
10358 mips_set_tune (mips_arch_info);
e9670677 10359
316f5878 10360 if (file_mips_gp32 >= 0)
e9670677 10361 {
316f5878
RS
10362 /* The user specified the size of the integer registers. Make sure
10363 it agrees with the ABI and ISA. */
10364 if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
10365 as_bad (_("-mgp64 used with a 32-bit processor"));
10366 else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
10367 as_bad (_("-mgp32 used with a 64-bit ABI"));
10368 else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
10369 as_bad (_("-mgp64 used with a 32-bit ABI"));
e9670677
MR
10370 }
10371 else
10372 {
316f5878
RS
10373 /* Infer the integer register size from the ABI and processor.
10374 Restrict ourselves to 32-bit registers if that's all the
10375 processor has, or if the ABI cannot handle 64-bit registers. */
10376 file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
10377 || !ISA_HAS_64BIT_REGS (mips_opts.isa));
e9670677
MR
10378 }
10379
316f5878
RS
10380 /* ??? GAS treats single-float processors as though they had 64-bit
10381 float registers (although it complains when double-precision
10382 instructions are used). As things stand, saying they have 32-bit
10383 registers would lead to spurious "register must be even" messages.
10384 So here we assume float registers are always the same size as
10385 integer ones, unless the user says otherwise. */
10386 if (file_mips_fp32 < 0)
10387 file_mips_fp32 = file_mips_gp32;
e9670677 10388
316f5878 10389 /* End of GCC-shared inference code. */
e9670677 10390
316f5878
RS
10391 /* ??? When do we want this flag to be set? Who uses it? */
10392 if (file_mips_gp32 == 1
10393 && mips_abi == NO_ABI
10394 && ISA_HAS_64BIT_REGS (mips_opts.isa))
10395 mips_32bitmode = 1;
e9670677
MR
10396
10397 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
10398 as_bad (_("trap exception not supported at ISA 1"));
10399
e9670677
MR
10400 /* If the selected architecture includes support for ASEs, enable
10401 generation of code for them. */
a4672219
TS
10402 if (mips_opts.mips16 == -1)
10403 mips_opts.mips16 = (CPU_HAS_MIPS16 (mips_arch)) ? 1 : 0;
ffdefa66 10404 if (mips_opts.ase_mips3d == -1)
a4672219 10405 mips_opts.ase_mips3d = (CPU_HAS_MIPS3D (mips_arch)) ? 1 : 0;
ffdefa66 10406 if (mips_opts.ase_mdmx == -1)
a4672219 10407 mips_opts.ase_mdmx = (CPU_HAS_MDMX (mips_arch)) ? 1 : 0;
e9670677 10408
e9670677 10409 file_mips_isa = mips_opts.isa;
a4672219 10410 file_ase_mips16 = mips_opts.mips16;
e9670677
MR
10411 file_ase_mips3d = mips_opts.ase_mips3d;
10412 file_ase_mdmx = mips_opts.ase_mdmx;
10413 mips_opts.gp32 = file_mips_gp32;
10414 mips_opts.fp32 = file_mips_fp32;
10415
10416 if (HAVE_NEWABI)
10417 mips_big_got = 1;
ecb4347a
DJ
10418
10419 if (mips_flag_mdebug < 0)
10420 {
10421#ifdef OBJ_MAYBE_ECOFF
10422 if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
10423 mips_flag_mdebug = 1;
10424 else
10425#endif /* OBJ_MAYBE_ECOFF */
10426 mips_flag_mdebug = 0;
10427 }
e9670677
MR
10428}
10429\f
10430void
252b5132
RH
10431mips_init_after_args ()
10432{
10433 /* initialize opcodes */
10434 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
beae10d5 10435 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
252b5132
RH
10436}
10437
10438long
10439md_pcrel_from (fixP)
10440 fixS *fixP;
10441{
10442 if (OUTPUT_FLAVOR != bfd_target_aout_flavour
10443 && fixP->fx_addsy != (symbolS *) NULL
10444 && ! S_IS_DEFINED (fixP->fx_addsy))
10445 {
6478892d
TS
10446 /* This makes a branch to an undefined symbol be a branch to the
10447 current location. */
cb56d3d3 10448 if (mips_pic == EMBEDDED_PIC)
6478892d 10449 return 4;
cb56d3d3 10450 else
6478892d 10451 return 1;
252b5132
RH
10452 }
10453
c9914766 10454 /* Return the address of the delay slot. */
252b5132
RH
10455 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
10456}
10457
252b5132
RH
10458/* This is called before the symbol table is processed. In order to
10459 work with gcc when using mips-tfile, we must keep all local labels.
10460 However, in other cases, we want to discard them. If we were
10461 called with -g, but we didn't see any debugging information, it may
10462 mean that gcc is smuggling debugging information through to
10463 mips-tfile, in which case we must generate all local labels. */
10464
10465void
10466mips_frob_file_before_adjust ()
10467{
10468#ifndef NO_ECOFF_DEBUGGING
10469 if (ECOFF_DEBUGGING
10470 && mips_debug != 0
10471 && ! ecoff_debugging_seen)
10472 flag_keep_locals = 1;
10473#endif
10474}
10475
10476/* Sort any unmatched HI16_S relocs so that they immediately precede
94f592af 10477 the corresponding LO reloc. This is called before md_apply_fix3 and
252b5132
RH
10478 tc_gen_reloc. Unmatched HI16_S relocs can only be generated by
10479 explicit use of the %hi modifier. */
10480
10481void
10482mips_frob_file ()
10483{
10484 struct mips_hi_fixup *l;
10485
10486 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
10487 {
10488 segment_info_type *seginfo;
10489 int pass;
10490
10491 assert (l->fixp->fx_r_type == BFD_RELOC_HI16_S);
10492
10493 /* Check quickly whether the next fixup happens to be a matching
10494 %lo. */
10495 if (l->fixp->fx_next != NULL
10496 && l->fixp->fx_next->fx_r_type == BFD_RELOC_LO16
10497 && l->fixp->fx_addsy == l->fixp->fx_next->fx_addsy
10498 && l->fixp->fx_offset == l->fixp->fx_next->fx_offset)
10499 continue;
10500
10501 /* Look through the fixups for this segment for a matching %lo.
10502 When we find one, move the %hi just in front of it. We do
10503 this in two passes. In the first pass, we try to find a
10504 unique %lo. In the second pass, we permit multiple %hi
10505 relocs for a single %lo (this is a GNU extension). */
10506 seginfo = seg_info (l->seg);
10507 for (pass = 0; pass < 2; pass++)
10508 {
10509 fixS *f, *prev;
10510
10511 prev = NULL;
10512 for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
10513 {
10514 /* Check whether this is a %lo fixup which matches l->fixp. */
10515 if (f->fx_r_type == BFD_RELOC_LO16
10516 && f->fx_addsy == l->fixp->fx_addsy
10517 && f->fx_offset == l->fixp->fx_offset
10518 && (pass == 1
10519 || prev == NULL
10520 || prev->fx_r_type != BFD_RELOC_HI16_S
10521 || prev->fx_addsy != f->fx_addsy
10522 || prev->fx_offset != f->fx_offset))
10523 {
10524 fixS **pf;
10525
10526 /* Move l->fixp before f. */
10527 for (pf = &seginfo->fix_root;
10528 *pf != l->fixp;
10529 pf = &(*pf)->fx_next)
10530 assert (*pf != NULL);
10531
10532 *pf = l->fixp->fx_next;
10533
10534 l->fixp->fx_next = f;
10535 if (prev == NULL)
10536 seginfo->fix_root = l->fixp;
10537 else
10538 prev->fx_next = l->fixp;
10539
10540 break;
10541 }
10542
10543 prev = f;
10544 }
10545
10546 if (f != NULL)
10547 break;
10548
10549#if 0 /* GCC code motion plus incomplete dead code elimination
10550 can leave a %hi without a %lo. */
10551 if (pass == 1)
10552 as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
10553 _("Unmatched %%hi reloc"));
10554#endif
10555 }
10556 }
10557}
10558
10559/* When generating embedded PIC code we need to use a special
10560 relocation to represent the difference of two symbols in the .text
10561 section (switch tables use a difference of this sort). See
10562 include/coff/mips.h for details. This macro checks whether this
10563 fixup requires the special reloc. */
10564#define SWITCH_TABLE(fixp) \
10565 ((fixp)->fx_r_type == BFD_RELOC_32 \
bb2d6cd7 10566 && OUTPUT_FLAVOR != bfd_target_elf_flavour \
252b5132
RH
10567 && (fixp)->fx_addsy != NULL \
10568 && (fixp)->fx_subsy != NULL \
10569 && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
10570 && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
10571
10572/* When generating embedded PIC code we must keep all PC relative
10573 relocations, in case the linker has to relax a call. We also need
f6688943
TS
10574 to keep relocations for switch table entries.
10575
10576 We may have combined relocations without symbols in the N32/N64 ABI.
10577 We have to prevent gas from dropping them. */
252b5132 10578
252b5132
RH
10579int
10580mips_force_relocation (fixp)
10581 fixS *fixp;
10582{
10583 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
10584 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
10585 return 1;
10586
f6688943
TS
10587 if (HAVE_NEWABI
10588 && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
10589 && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
10590 || fixp->fx_r_type == BFD_RELOC_HI16_S
10591 || fixp->fx_r_type == BFD_RELOC_LO16))
10592 return 1;
10593
252b5132
RH
10594 return (mips_pic == EMBEDDED_PIC
10595 && (fixp->fx_pcrel
10596 || SWITCH_TABLE (fixp)
10597 || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
10598 || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
10599}
10600
add55e1f
RS
10601#ifdef OBJ_ELF
10602static int
10603mips_need_elf_addend_fixup (fixP)
10604 fixS *fixP;
10605{
2d2bf3e0
CD
10606 if (S_GET_OTHER (fixP->fx_addsy) == STO_MIPS16)
10607 return 1;
b25a253c
CD
10608 if (mips_pic == EMBEDDED_PIC
10609 && S_IS_WEAK (fixP->fx_addsy))
10610 return 1;
10611 if (mips_pic != EMBEDDED_PIC
10612 && (S_IS_WEAK (fixP->fx_addsy)
bad9ca53 10613 || S_IS_EXTERNAL (fixP->fx_addsy))
2d2bf3e0
CD
10614 && !S_IS_COMMON (fixP->fx_addsy))
10615 return 1;
10616 if (symbol_used_in_reloc_p (fixP->fx_addsy)
10617 && (((bfd_get_section_flags (stdoutput,
10618 S_GET_SEGMENT (fixP->fx_addsy))
10619 & SEC_LINK_ONCE) != 0)
10620 || !strncmp (segment_name (S_GET_SEGMENT (fixP->fx_addsy)),
10621 ".gnu.linkonce",
10622 sizeof (".gnu.linkonce") - 1)))
10623 return 1;
10624 return 0;
add55e1f
RS
10625}
10626#endif
10627
252b5132
RH
10628/* Apply a fixup to the object file. */
10629
94f592af
NC
10630void
10631md_apply_fix3 (fixP, valP, seg)
252b5132 10632 fixS *fixP;
98d3f06f 10633 valueT *valP;
94f592af 10634 segT seg ATTRIBUTE_UNUSED;
252b5132 10635{
874e8986 10636 bfd_byte *buf;
98aa84af
AM
10637 long insn;
10638 valueT value;
252b5132
RH
10639
10640 assert (fixP->fx_size == 4
10641 || fixP->fx_r_type == BFD_RELOC_16
f6688943
TS
10642 || fixP->fx_r_type == BFD_RELOC_32
10643 || fixP->fx_r_type == BFD_RELOC_MIPS_JMP
10644 || fixP->fx_r_type == BFD_RELOC_HI16_S
10645 || fixP->fx_r_type == BFD_RELOC_LO16
10646 || fixP->fx_r_type == BFD_RELOC_GPREL16
76b3015f 10647 || fixP->fx_r_type == BFD_RELOC_MIPS_LITERAL
f6688943 10648 || fixP->fx_r_type == BFD_RELOC_GPREL32
252b5132 10649 || fixP->fx_r_type == BFD_RELOC_64
f6688943
TS
10650 || fixP->fx_r_type == BFD_RELOC_CTOR
10651 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
10652 || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHEST
10653 || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHER
10654 || fixP->fx_r_type == BFD_RELOC_MIPS_SCN_DISP
10655 || fixP->fx_r_type == BFD_RELOC_MIPS_REL16
10656 || fixP->fx_r_type == BFD_RELOC_MIPS_RELGOT
252b5132 10657 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
438c16b8
TS
10658 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
10659 || fixP->fx_r_type == BFD_RELOC_MIPS_JALR);
252b5132 10660
98d3f06f 10661 value = *valP;
252b5132
RH
10662
10663 /* If we aren't adjusting this fixup to be against the section
10664 symbol, we need to adjust the value. */
10665#ifdef OBJ_ELF
10666 if (fixP->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour)
bb2d6cd7 10667 {
add55e1f 10668 if (mips_need_elf_addend_fixup (fixP))
98aa84af 10669 {
d6e9d61a 10670 reloc_howto_type *howto;
98aa84af 10671 valueT symval = S_GET_VALUE (fixP->fx_addsy);
94f592af 10672
98aa84af 10673 value -= symval;
d6e9d61a
MR
10674
10675 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
10676 if (value != 0 && howto->partial_inplace && ! fixP->fx_pcrel)
98aa84af
AM
10677 {
10678 /* In this case, the bfd_install_relocation routine will
10679 incorrectly add the symbol value back in. We just want
7461da6e 10680 the addend to appear in the object file. */
98aa84af 10681 value -= symval;
7461da6e
RS
10682
10683 /* Make sure the addend is still non-zero. If it became zero
10684 after the last operation, set it to a spurious value and
10685 subtract the same value from the object file's contents. */
10686 if (value == 0)
10687 {
10688 value = 8;
10689
10690 /* The in-place addends for LO16 relocations are signed;
10691 leave the matching HI16 in-place addends as zero. */
10692 if (fixP->fx_r_type != BFD_RELOC_HI16_S)
10693 {
7461da6e
RS
10694 bfd_vma contents, mask, field;
10695
7461da6e
RS
10696 contents = bfd_get_bits (fixP->fx_frag->fr_literal
10697 + fixP->fx_where,
10698 fixP->fx_size * 8,
10699 target_big_endian);
10700
10701 /* MASK has bits set where the relocation should go.
10702 FIELD is -value, shifted into the appropriate place
10703 for this relocation. */
10704 mask = 1 << (howto->bitsize - 1);
10705 mask = (((mask - 1) << 1) | 1) << howto->bitpos;
10706 field = (-value >> howto->rightshift) << howto->bitpos;
10707
10708 bfd_put_bits ((field & mask) | (contents & ~mask),
10709 fixP->fx_frag->fr_literal + fixP->fx_where,
10710 fixP->fx_size * 8,
10711 target_big_endian);
10712 }
10713 }
98aa84af
AM
10714 }
10715 }
252b5132 10716
bb2d6cd7
GK
10717 /* This code was generated using trial and error and so is
10718 fragile and not trustworthy. If you change it, you should
10719 rerun the elf-rel, elf-rel2, and empic testcases and ensure
10720 they still pass. */
10721 if (fixP->fx_pcrel || fixP->fx_subsy != NULL)
10722 {
10723 value += fixP->fx_frag->fr_address + fixP->fx_where;
10724
10725 /* BFD's REL handling, for MIPS, is _very_ weird.
10726 This gives the right results, but it can't possibly
10727 be the way things are supposed to work. */
cb56d3d3
TS
10728 if ((fixP->fx_r_type != BFD_RELOC_16_PCREL
10729 && fixP->fx_r_type != BFD_RELOC_16_PCREL_S2)
bb2d6cd7
GK
10730 || S_GET_SEGMENT (fixP->fx_addsy) != undefined_section)
10731 value += fixP->fx_frag->fr_address + fixP->fx_where;
10732 }
10733 }
10734#endif
252b5132 10735
94f592af 10736 fixP->fx_addnumber = value; /* Remember value for tc_gen_reloc. */
252b5132
RH
10737
10738 if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel)
10739 fixP->fx_done = 1;
10740
10741 switch (fixP->fx_r_type)
10742 {
10743 case BFD_RELOC_MIPS_JMP:
e369bcce
TS
10744 case BFD_RELOC_MIPS_SHIFT5:
10745 case BFD_RELOC_MIPS_SHIFT6:
10746 case BFD_RELOC_MIPS_GOT_DISP:
10747 case BFD_RELOC_MIPS_GOT_PAGE:
10748 case BFD_RELOC_MIPS_GOT_OFST:
10749 case BFD_RELOC_MIPS_SUB:
10750 case BFD_RELOC_MIPS_INSERT_A:
10751 case BFD_RELOC_MIPS_INSERT_B:
10752 case BFD_RELOC_MIPS_DELETE:
10753 case BFD_RELOC_MIPS_HIGHEST:
10754 case BFD_RELOC_MIPS_HIGHER:
10755 case BFD_RELOC_MIPS_SCN_DISP:
10756 case BFD_RELOC_MIPS_REL16:
10757 case BFD_RELOC_MIPS_RELGOT:
10758 case BFD_RELOC_MIPS_JALR:
252b5132
RH
10759 case BFD_RELOC_HI16:
10760 case BFD_RELOC_HI16_S:
cdf6fd85 10761 case BFD_RELOC_GPREL16:
252b5132
RH
10762 case BFD_RELOC_MIPS_LITERAL:
10763 case BFD_RELOC_MIPS_CALL16:
10764 case BFD_RELOC_MIPS_GOT16:
cdf6fd85 10765 case BFD_RELOC_GPREL32:
252b5132
RH
10766 case BFD_RELOC_MIPS_GOT_HI16:
10767 case BFD_RELOC_MIPS_GOT_LO16:
10768 case BFD_RELOC_MIPS_CALL_HI16:
10769 case BFD_RELOC_MIPS_CALL_LO16:
10770 case BFD_RELOC_MIPS16_GPREL:
10771 if (fixP->fx_pcrel)
10772 as_bad_where (fixP->fx_file, fixP->fx_line,
10773 _("Invalid PC relative reloc"));
10774 /* Nothing needed to do. The value comes from the reloc entry */
10775 break;
10776
10777 case BFD_RELOC_MIPS16_JMP:
10778 /* We currently always generate a reloc against a symbol, which
10779 means that we don't want an addend even if the symbol is
10780 defined. */
10781 fixP->fx_addnumber = 0;
10782 break;
10783
10784 case BFD_RELOC_PCREL_HI16_S:
10785 /* The addend for this is tricky if it is internal, so we just
10786 do everything here rather than in bfd_install_relocation. */
bdaaa2e1 10787 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
bb2d6cd7
GK
10788 && !fixP->fx_done
10789 && value != 0)
10790 break;
10791 if (fixP->fx_addsy
10792 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
252b5132
RH
10793 {
10794 /* For an external symbol adjust by the address to make it
10795 pcrel_offset. We use the address of the RELLO reloc
10796 which follows this one. */
10797 value += (fixP->fx_next->fx_frag->fr_address
10798 + fixP->fx_next->fx_where);
10799 }
e7d556df 10800 value = ((value + 0x8000) >> 16) & 0xffff;
874e8986 10801 buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
252b5132
RH
10802 if (target_big_endian)
10803 buf += 2;
874e8986 10804 md_number_to_chars ((char *) buf, value, 2);
252b5132
RH
10805 break;
10806
10807 case BFD_RELOC_PCREL_LO16:
10808 /* The addend for this is tricky if it is internal, so we just
10809 do everything here rather than in bfd_install_relocation. */
bdaaa2e1 10810 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
bb2d6cd7
GK
10811 && !fixP->fx_done
10812 && value != 0)
10813 break;
10814 if (fixP->fx_addsy
10815 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
252b5132 10816 value += fixP->fx_frag->fr_address + fixP->fx_where;
874e8986 10817 buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
252b5132
RH
10818 if (target_big_endian)
10819 buf += 2;
874e8986 10820 md_number_to_chars ((char *) buf, value, 2);
252b5132
RH
10821 break;
10822
10823 case BFD_RELOC_64:
10824 /* This is handled like BFD_RELOC_32, but we output a sign
10825 extended value if we are only 32 bits. */
10826 if (fixP->fx_done
10827 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
10828 {
10829 if (8 <= sizeof (valueT))
10830 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
10831 value, 8);
10832 else
10833 {
10834 long w1, w2;
10835 long hiv;
10836
10837 w1 = w2 = fixP->fx_where;
10838 if (target_big_endian)
10839 w1 += 4;
10840 else
10841 w2 += 4;
10842 md_number_to_chars (fixP->fx_frag->fr_literal + w1, value, 4);
10843 if ((value & 0x80000000) != 0)
10844 hiv = 0xffffffff;
10845 else
10846 hiv = 0;
10847 md_number_to_chars (fixP->fx_frag->fr_literal + w2, hiv, 4);
10848 }
10849 }
10850 break;
10851
056350c6 10852 case BFD_RELOC_RVA:
252b5132
RH
10853 case BFD_RELOC_32:
10854 /* If we are deleting this reloc entry, we must fill in the
10855 value now. This can happen if we have a .word which is not
10856 resolved when it appears but is later defined. We also need
10857 to fill in the value if this is an embedded PIC switch table
10858 entry. */
10859 if (fixP->fx_done
10860 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
10861 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
10862 value, 4);
10863 break;
10864
10865 case BFD_RELOC_16:
10866 /* If we are deleting this reloc entry, we must fill in the
10867 value now. */
10868 assert (fixP->fx_size == 2);
10869 if (fixP->fx_done)
10870 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
10871 value, 2);
10872 break;
10873
10874 case BFD_RELOC_LO16:
10875 /* When handling an embedded PIC switch statement, we can wind
10876 up deleting a LO16 reloc. See the 'o' case in mips_ip. */
10877 if (fixP->fx_done)
10878 {
98aa84af 10879 if (value + 0x8000 > 0xffff)
252b5132
RH
10880 as_bad_where (fixP->fx_file, fixP->fx_line,
10881 _("relocation overflow"));
874e8986 10882 buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
252b5132
RH
10883 if (target_big_endian)
10884 buf += 2;
874e8986 10885 md_number_to_chars ((char *) buf, value, 2);
252b5132
RH
10886 }
10887 break;
10888
10889 case BFD_RELOC_16_PCREL_S2:
cb56d3d3
TS
10890 if ((value & 0x3) != 0)
10891 as_bad_where (fixP->fx_file, fixP->fx_line,
10892 _("Branch to odd address (%lx)"), (long) value);
10893
10894 /* Fall through. */
10895
10896 case BFD_RELOC_16_PCREL:
252b5132
RH
10897 /*
10898 * We need to save the bits in the instruction since fixup_segment()
10899 * might be deleting the relocation entry (i.e., a branch within
10900 * the current segment).
10901 */
bb2d6cd7
GK
10902 if (!fixP->fx_done && value != 0)
10903 break;
10904 /* If 'value' is zero, the remaining reloc code won't actually
10905 do the store, so it must be done here. This is probably
10906 a bug somewhere. */
b25a253c
CD
10907 if (!fixP->fx_done
10908 && (fixP->fx_r_type != BFD_RELOC_16_PCREL_S2
10909 || fixP->fx_addsy == NULL /* ??? */
10910 || ! S_IS_DEFINED (fixP->fx_addsy)))
bb2d6cd7 10911 value -= fixP->fx_frag->fr_address + fixP->fx_where;
bdaaa2e1 10912
98aa84af 10913 value = (offsetT) value >> 2;
252b5132
RH
10914
10915 /* update old instruction data */
874e8986 10916 buf = (bfd_byte *) (fixP->fx_where + fixP->fx_frag->fr_literal);
252b5132
RH
10917 if (target_big_endian)
10918 insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
10919 else
10920 insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
10921
98aa84af 10922 if (value + 0x8000 <= 0xffff)
252b5132
RH
10923 insn |= value & 0xffff;
10924 else
10925 {
10926 /* The branch offset is too large. If this is an
10927 unconditional branch, and we are not generating PIC code,
10928 we can convert it to an absolute jump instruction. */
10929 if (mips_pic == NO_PIC
10930 && fixP->fx_done
10931 && fixP->fx_frag->fr_address >= text_section->vma
10932 && (fixP->fx_frag->fr_address
10933 < text_section->vma + text_section->_raw_size)
10934 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
10935 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
10936 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
10937 {
10938 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
10939 insn = 0x0c000000; /* jal */
10940 else
10941 insn = 0x08000000; /* j */
10942 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
10943 fixP->fx_done = 0;
10944 fixP->fx_addsy = section_symbol (text_section);
10945 fixP->fx_addnumber = (value << 2) + md_pcrel_from (fixP);
10946 }
10947 else
10948 {
10949 /* FIXME. It would be possible in principle to handle
10950 conditional branches which overflow. They could be
10951 transformed into a branch around a jump. This would
10952 require setting up variant frags for each different
10953 branch type. The native MIPS assembler attempts to
10954 handle these cases, but it appears to do it
10955 incorrectly. */
10956 as_bad_where (fixP->fx_file, fixP->fx_line,
10957 _("Branch out of range"));
10958 }
10959 }
10960
10961 md_number_to_chars ((char *) buf, (valueT) insn, 4);
10962 break;
10963
10964 case BFD_RELOC_VTABLE_INHERIT:
10965 fixP->fx_done = 0;
10966 if (fixP->fx_addsy
10967 && !S_IS_DEFINED (fixP->fx_addsy)
10968 && !S_IS_WEAK (fixP->fx_addsy))
10969 S_SET_WEAK (fixP->fx_addsy);
10970 break;
10971
10972 case BFD_RELOC_VTABLE_ENTRY:
10973 fixP->fx_done = 0;
10974 break;
10975
10976 default:
10977 internalError ();
10978 }
252b5132
RH
10979}
10980
10981#if 0
10982void
10983printInsn (oc)
10984 unsigned long oc;
10985{
10986 const struct mips_opcode *p;
10987 int treg, sreg, dreg, shamt;
10988 short imm;
10989 const char *args;
10990 int i;
10991
10992 for (i = 0; i < NUMOPCODES; ++i)
10993 {
10994 p = &mips_opcodes[i];
10995 if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
10996 {
10997 printf ("%08lx %s\t", oc, p->name);
10998 treg = (oc >> 16) & 0x1f;
10999 sreg = (oc >> 21) & 0x1f;
11000 dreg = (oc >> 11) & 0x1f;
11001 shamt = (oc >> 6) & 0x1f;
11002 imm = oc;
11003 for (args = p->args;; ++args)
11004 {
11005 switch (*args)
11006 {
11007 case '\0':
11008 printf ("\n");
11009 break;
11010
11011 case ',':
11012 case '(':
11013 case ')':
11014 printf ("%c", *args);
11015 continue;
11016
11017 case 'r':
11018 assert (treg == sreg);
11019 printf ("$%d,$%d", treg, sreg);
11020 continue;
11021
11022 case 'd':
11023 case 'G':
11024 printf ("$%d", dreg);
11025 continue;
11026
11027 case 't':
11028 case 'E':
11029 printf ("$%d", treg);
11030 continue;
11031
11032 case 'k':
11033 printf ("0x%x", treg);
11034 continue;
11035
11036 case 'b':
11037 case 's':
11038 printf ("$%d", sreg);
11039 continue;
11040
11041 case 'a':
11042 printf ("0x%08lx", oc & 0x1ffffff);
11043 continue;
11044
11045 case 'i':
11046 case 'j':
11047 case 'o':
11048 case 'u':
11049 printf ("%d", imm);
11050 continue;
11051
11052 case '<':
11053 case '>':
11054 printf ("$%d", shamt);
11055 continue;
11056
11057 default:
11058 internalError ();
11059 }
11060 break;
11061 }
11062 return;
11063 }
11064 }
11065 printf (_("%08lx UNDEFINED\n"), oc);
11066}
11067#endif
11068
11069static symbolS *
11070get_symbol ()
11071{
11072 int c;
11073 char *name;
11074 symbolS *p;
11075
11076 name = input_line_pointer;
11077 c = get_symbol_end ();
11078 p = (symbolS *) symbol_find_or_make (name);
11079 *input_line_pointer = c;
11080 return p;
11081}
11082
11083/* Align the current frag to a given power of two. The MIPS assembler
11084 also automatically adjusts any preceding label. */
11085
11086static void
11087mips_align (to, fill, label)
11088 int to;
11089 int fill;
11090 symbolS *label;
11091{
11092 mips_emit_delays (false);
11093 frag_align (to, fill, 0);
11094 record_alignment (now_seg, to);
11095 if (label != NULL)
11096 {
11097 assert (S_GET_SEGMENT (label) == now_seg);
49309057 11098 symbol_set_frag (label, frag_now);
252b5132
RH
11099 S_SET_VALUE (label, (valueT) frag_now_fix ());
11100 }
11101}
11102
11103/* Align to a given power of two. .align 0 turns off the automatic
11104 alignment used by the data creating pseudo-ops. */
11105
11106static void
11107s_align (x)
43841e91 11108 int x ATTRIBUTE_UNUSED;
252b5132
RH
11109{
11110 register int temp;
11111 register long temp_fill;
11112 long max_alignment = 15;
11113
11114 /*
11115
11116 o Note that the assembler pulls down any immediately preceeding label
11117 to the aligned address.
11118 o It's not documented but auto alignment is reinstated by
11119 a .align pseudo instruction.
11120 o Note also that after auto alignment is turned off the mips assembler
11121 issues an error on attempt to assemble an improperly aligned data item.
11122 We don't.
11123
11124 */
11125
11126 temp = get_absolute_expression ();
11127 if (temp > max_alignment)
11128 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
11129 else if (temp < 0)
11130 {
11131 as_warn (_("Alignment negative: 0 assumed."));
11132 temp = 0;
11133 }
11134 if (*input_line_pointer == ',')
11135 {
f9419b05 11136 ++input_line_pointer;
252b5132
RH
11137 temp_fill = get_absolute_expression ();
11138 }
11139 else
11140 temp_fill = 0;
11141 if (temp)
11142 {
11143 auto_align = 1;
11144 mips_align (temp, (int) temp_fill,
11145 insn_labels != NULL ? insn_labels->label : NULL);
11146 }
11147 else
11148 {
11149 auto_align = 0;
11150 }
11151
11152 demand_empty_rest_of_line ();
11153}
11154
11155void
11156mips_flush_pending_output ()
11157{
11158 mips_emit_delays (false);
11159 mips_clear_insn_labels ();
11160}
11161
11162static void
11163s_change_sec (sec)
11164 int sec;
11165{
11166 segT seg;
11167
11168 /* When generating embedded PIC code, we only use the .text, .lit8,
11169 .sdata and .sbss sections. We change the .data and .rdata
11170 pseudo-ops to use .sdata. */
11171 if (mips_pic == EMBEDDED_PIC
11172 && (sec == 'd' || sec == 'r'))
11173 sec = 's';
11174
11175#ifdef OBJ_ELF
11176 /* The ELF backend needs to know that we are changing sections, so
11177 that .previous works correctly. We could do something like check
b6ff326e 11178 for an obj_section_change_hook macro, but that might be confusing
252b5132
RH
11179 as it would not be appropriate to use it in the section changing
11180 functions in read.c, since obj-elf.c intercepts those. FIXME:
11181 This should be cleaner, somehow. */
11182 obj_elf_section_change_hook ();
11183#endif
11184
11185 mips_emit_delays (false);
11186 switch (sec)
11187 {
11188 case 't':
11189 s_text (0);
11190 break;
11191 case 'd':
11192 s_data (0);
11193 break;
11194 case 'b':
11195 subseg_set (bss_section, (subsegT) get_absolute_expression ());
11196 demand_empty_rest_of_line ();
11197 break;
11198
11199 case 'r':
11200 if (USE_GLOBAL_POINTER_OPT)
11201 {
11202 seg = subseg_new (RDATA_SECTION_NAME,
11203 (subsegT) get_absolute_expression ());
11204 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11205 {
11206 bfd_set_section_flags (stdoutput, seg,
11207 (SEC_ALLOC
11208 | SEC_LOAD
11209 | SEC_READONLY
11210 | SEC_RELOC
11211 | SEC_DATA));
11212 if (strcmp (TARGET_OS, "elf") != 0)
e799a695 11213 record_alignment (seg, 4);
252b5132
RH
11214 }
11215 demand_empty_rest_of_line ();
11216 }
11217 else
11218 {
11219 as_bad (_("No read only data section in this object file format"));
11220 demand_empty_rest_of_line ();
11221 return;
11222 }
11223 break;
11224
11225 case 's':
11226 if (USE_GLOBAL_POINTER_OPT)
11227 {
11228 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
11229 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11230 {
11231 bfd_set_section_flags (stdoutput, seg,
11232 SEC_ALLOC | SEC_LOAD | SEC_RELOC
11233 | SEC_DATA);
11234 if (strcmp (TARGET_OS, "elf") != 0)
e799a695 11235 record_alignment (seg, 4);
252b5132
RH
11236 }
11237 demand_empty_rest_of_line ();
11238 break;
11239 }
11240 else
11241 {
11242 as_bad (_("Global pointers not supported; recompile -G 0"));
11243 demand_empty_rest_of_line ();
11244 return;
11245 }
11246 }
11247
11248 auto_align = 1;
11249}
11250
11251void
11252mips_enable_auto_align ()
11253{
11254 auto_align = 1;
11255}
11256
11257static void
11258s_cons (log_size)
11259 int log_size;
11260{
11261 symbolS *label;
11262
11263 label = insn_labels != NULL ? insn_labels->label : NULL;
11264 mips_emit_delays (false);
11265 if (log_size > 0 && auto_align)
11266 mips_align (log_size, 0, label);
11267 mips_clear_insn_labels ();
11268 cons (1 << log_size);
11269}
11270
11271static void
11272s_float_cons (type)
11273 int type;
11274{
11275 symbolS *label;
11276
11277 label = insn_labels != NULL ? insn_labels->label : NULL;
11278
11279 mips_emit_delays (false);
11280
11281 if (auto_align)
49309057
ILT
11282 {
11283 if (type == 'd')
11284 mips_align (3, 0, label);
11285 else
11286 mips_align (2, 0, label);
11287 }
252b5132
RH
11288
11289 mips_clear_insn_labels ();
11290
11291 float_cons (type);
11292}
11293
11294/* Handle .globl. We need to override it because on Irix 5 you are
11295 permitted to say
11296 .globl foo .text
11297 where foo is an undefined symbol, to mean that foo should be
11298 considered to be the address of a function. */
11299
11300static void
11301s_mips_globl (x)
43841e91 11302 int x ATTRIBUTE_UNUSED;
252b5132
RH
11303{
11304 char *name;
11305 int c;
11306 symbolS *symbolP;
11307 flagword flag;
11308
11309 name = input_line_pointer;
11310 c = get_symbol_end ();
11311 symbolP = symbol_find_or_make (name);
11312 *input_line_pointer = c;
11313 SKIP_WHITESPACE ();
11314
11315 /* On Irix 5, every global symbol that is not explicitly labelled as
11316 being a function is apparently labelled as being an object. */
11317 flag = BSF_OBJECT;
11318
11319 if (! is_end_of_line[(unsigned char) *input_line_pointer])
11320 {
11321 char *secname;
11322 asection *sec;
11323
11324 secname = input_line_pointer;
11325 c = get_symbol_end ();
11326 sec = bfd_get_section_by_name (stdoutput, secname);
11327 if (sec == NULL)
11328 as_bad (_("%s: no such section"), secname);
11329 *input_line_pointer = c;
11330
11331 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
11332 flag = BSF_FUNCTION;
11333 }
11334
49309057 11335 symbol_get_bfdsym (symbolP)->flags |= flag;
252b5132
RH
11336
11337 S_SET_EXTERNAL (symbolP);
11338 demand_empty_rest_of_line ();
11339}
11340
11341static void
11342s_option (x)
43841e91 11343 int x ATTRIBUTE_UNUSED;
252b5132
RH
11344{
11345 char *opt;
11346 char c;
11347
11348 opt = input_line_pointer;
11349 c = get_symbol_end ();
11350
11351 if (*opt == 'O')
11352 {
11353 /* FIXME: What does this mean? */
11354 }
11355 else if (strncmp (opt, "pic", 3) == 0)
11356 {
11357 int i;
11358
11359 i = atoi (opt + 3);
11360 if (i == 0)
11361 mips_pic = NO_PIC;
11362 else if (i == 2)
11363 mips_pic = SVR4_PIC;
11364 else
11365 as_bad (_(".option pic%d not supported"), i);
11366
11367 if (USE_GLOBAL_POINTER_OPT && mips_pic == SVR4_PIC)
11368 {
11369 if (g_switch_seen && g_switch_value != 0)
11370 as_warn (_("-G may not be used with SVR4 PIC code"));
11371 g_switch_value = 0;
11372 bfd_set_gp_size (stdoutput, 0);
11373 }
11374 }
11375 else
11376 as_warn (_("Unrecognized option \"%s\""), opt);
11377
11378 *input_line_pointer = c;
11379 demand_empty_rest_of_line ();
11380}
11381
11382/* This structure is used to hold a stack of .set values. */
11383
e972090a
NC
11384struct mips_option_stack
11385{
252b5132
RH
11386 struct mips_option_stack *next;
11387 struct mips_set_options options;
11388};
11389
11390static struct mips_option_stack *mips_opts_stack;
11391
11392/* Handle the .set pseudo-op. */
11393
11394static void
11395s_mipsset (x)
43841e91 11396 int x ATTRIBUTE_UNUSED;
252b5132
RH
11397{
11398 char *name = input_line_pointer, ch;
11399
11400 while (!is_end_of_line[(unsigned char) *input_line_pointer])
f9419b05 11401 ++input_line_pointer;
252b5132
RH
11402 ch = *input_line_pointer;
11403 *input_line_pointer = '\0';
11404
11405 if (strcmp (name, "reorder") == 0)
11406 {
11407 if (mips_opts.noreorder && prev_nop_frag != NULL)
11408 {
11409 /* If we still have pending nops, we can discard them. The
11410 usual nop handling will insert any that are still
bdaaa2e1 11411 needed. */
252b5132
RH
11412 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11413 * (mips_opts.mips16 ? 2 : 4));
11414 prev_nop_frag = NULL;
11415 }
11416 mips_opts.noreorder = 0;
11417 }
11418 else if (strcmp (name, "noreorder") == 0)
11419 {
11420 mips_emit_delays (true);
11421 mips_opts.noreorder = 1;
11422 mips_any_noreorder = 1;
11423 }
11424 else if (strcmp (name, "at") == 0)
11425 {
11426 mips_opts.noat = 0;
11427 }
11428 else if (strcmp (name, "noat") == 0)
11429 {
11430 mips_opts.noat = 1;
11431 }
11432 else if (strcmp (name, "macro") == 0)
11433 {
11434 mips_opts.warn_about_macros = 0;
11435 }
11436 else if (strcmp (name, "nomacro") == 0)
11437 {
11438 if (mips_opts.noreorder == 0)
11439 as_bad (_("`noreorder' must be set before `nomacro'"));
11440 mips_opts.warn_about_macros = 1;
11441 }
11442 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
11443 {
11444 mips_opts.nomove = 0;
11445 }
11446 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
11447 {
11448 mips_opts.nomove = 1;
11449 }
11450 else if (strcmp (name, "bopt") == 0)
11451 {
11452 mips_opts.nobopt = 0;
11453 }
11454 else if (strcmp (name, "nobopt") == 0)
11455 {
11456 mips_opts.nobopt = 1;
11457 }
11458 else if (strcmp (name, "mips16") == 0
11459 || strcmp (name, "MIPS-16") == 0)
11460 mips_opts.mips16 = 1;
11461 else if (strcmp (name, "nomips16") == 0
11462 || strcmp (name, "noMIPS-16") == 0)
11463 mips_opts.mips16 = 0;
1f25f5d3
CD
11464 else if (strcmp (name, "mips3d") == 0)
11465 mips_opts.ase_mips3d = 1;
11466 else if (strcmp (name, "nomips3d") == 0)
11467 mips_opts.ase_mips3d = 0;
a4672219
TS
11468 else if (strcmp (name, "mdmx") == 0)
11469 mips_opts.ase_mdmx = 1;
11470 else if (strcmp (name, "nomdmx") == 0)
11471 mips_opts.ase_mdmx = 0;
252b5132
RH
11472 else if (strncmp (name, "mips", 4) == 0)
11473 {
11474 int isa;
11475
11476 /* Permit the user to change the ISA on the fly. Needless to
11477 say, misuse can cause serious problems. */
11478 isa = atoi (name + 4);
553178e4 11479 switch (isa)
98d3f06f
KH
11480 {
11481 case 0:
11482 mips_opts.gp32 = file_mips_gp32;
11483 mips_opts.fp32 = file_mips_fp32;
98d3f06f
KH
11484 break;
11485 case 1:
11486 case 2:
11487 case 32:
11488 mips_opts.gp32 = 1;
11489 mips_opts.fp32 = 1;
11490 break;
11491 case 3:
11492 case 4:
11493 case 5:
11494 case 64:
98d3f06f
KH
11495 mips_opts.gp32 = 0;
11496 mips_opts.fp32 = 0;
11497 break;
11498 default:
11499 as_bad (_("unknown ISA level %s"), name + 4);
11500 break;
11501 }
553178e4 11502
e7af610e 11503 switch (isa)
98d3f06f
KH
11504 {
11505 case 0: mips_opts.isa = file_mips_isa; break;
11506 case 1: mips_opts.isa = ISA_MIPS1; break;
11507 case 2: mips_opts.isa = ISA_MIPS2; break;
11508 case 3: mips_opts.isa = ISA_MIPS3; break;
11509 case 4: mips_opts.isa = ISA_MIPS4; break;
11510 case 5: mips_opts.isa = ISA_MIPS5; break;
11511 case 32: mips_opts.isa = ISA_MIPS32; break;
11512 case 64: mips_opts.isa = ISA_MIPS64; break;
11513 default: as_bad (_("unknown ISA level %s"), name + 4); break;
11514 }
252b5132
RH
11515 }
11516 else if (strcmp (name, "autoextend") == 0)
11517 mips_opts.noautoextend = 0;
11518 else if (strcmp (name, "noautoextend") == 0)
11519 mips_opts.noautoextend = 1;
11520 else if (strcmp (name, "push") == 0)
11521 {
11522 struct mips_option_stack *s;
11523
11524 s = (struct mips_option_stack *) xmalloc (sizeof *s);
11525 s->next = mips_opts_stack;
11526 s->options = mips_opts;
11527 mips_opts_stack = s;
11528 }
11529 else if (strcmp (name, "pop") == 0)
11530 {
11531 struct mips_option_stack *s;
11532
11533 s = mips_opts_stack;
11534 if (s == NULL)
11535 as_bad (_(".set pop with no .set push"));
11536 else
11537 {
11538 /* If we're changing the reorder mode we need to handle
11539 delay slots correctly. */
11540 if (s->options.noreorder && ! mips_opts.noreorder)
11541 mips_emit_delays (true);
11542 else if (! s->options.noreorder && mips_opts.noreorder)
11543 {
11544 if (prev_nop_frag != NULL)
11545 {
11546 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11547 * (mips_opts.mips16 ? 2 : 4));
11548 prev_nop_frag = NULL;
11549 }
11550 }
11551
11552 mips_opts = s->options;
11553 mips_opts_stack = s->next;
11554 free (s);
11555 }
11556 }
11557 else
11558 {
11559 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
11560 }
11561 *input_line_pointer = ch;
11562 demand_empty_rest_of_line ();
11563}
11564
11565/* Handle the .abicalls pseudo-op. I believe this is equivalent to
11566 .option pic2. It means to generate SVR4 PIC calls. */
11567
11568static void
11569s_abicalls (ignore)
43841e91 11570 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
11571{
11572 mips_pic = SVR4_PIC;
11573 if (USE_GLOBAL_POINTER_OPT)
11574 {
11575 if (g_switch_seen && g_switch_value != 0)
11576 as_warn (_("-G may not be used with SVR4 PIC code"));
11577 g_switch_value = 0;
11578 }
11579 bfd_set_gp_size (stdoutput, 0);
11580 demand_empty_rest_of_line ();
11581}
11582
11583/* Handle the .cpload pseudo-op. This is used when generating SVR4
11584 PIC code. It sets the $gp register for the function based on the
11585 function address, which is in the register named in the argument.
11586 This uses a relocation against _gp_disp, which is handled specially
11587 by the linker. The result is:
11588 lui $gp,%hi(_gp_disp)
11589 addiu $gp,$gp,%lo(_gp_disp)
11590 addu $gp,$gp,.cpload argument
11591 The .cpload argument is normally $25 == $t9. */
11592
11593static void
11594s_cpload (ignore)
43841e91 11595 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
11596{
11597 expressionS ex;
11598 int icnt = 0;
11599
6478892d
TS
11600 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
11601 .cpload is ignored. */
11602 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
11603 {
11604 s_ignore (0);
11605 return;
11606 }
11607
d3ecfc59 11608 /* .cpload should be in a .set noreorder section. */
252b5132
RH
11609 if (mips_opts.noreorder == 0)
11610 as_warn (_(".cpload not in noreorder section"));
11611
11612 ex.X_op = O_symbol;
11613 ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
11614 ex.X_op_symbol = NULL;
11615 ex.X_add_number = 0;
11616
11617 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
49309057 11618 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
252b5132 11619
c9914766
TS
11620 macro_build_lui (NULL, &icnt, &ex, mips_gp_register);
11621 macro_build ((char *) NULL, &icnt, &ex, "addiu", "t,r,j",
11622 mips_gp_register, mips_gp_register, (int) BFD_RELOC_LO16);
252b5132
RH
11623
11624 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "addu", "d,v,t",
c9914766 11625 mips_gp_register, mips_gp_register, tc_get_register (0));
252b5132
RH
11626
11627 demand_empty_rest_of_line ();
11628}
11629
6478892d
TS
11630/* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
11631 .cpsetup $reg1, offset|$reg2, label
11632
11633 If offset is given, this results in:
11634 sd $gp, offset($sp)
956cd1d6 11635 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
11636 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
11637 daddu $gp, $gp, $reg1
6478892d
TS
11638
11639 If $reg2 is given, this results in:
11640 daddu $reg2, $gp, $0
956cd1d6 11641 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
11642 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
11643 daddu $gp, $gp, $reg1
11644 $reg1 is normally $25 == $t9. */
6478892d
TS
11645static void
11646s_cpsetup (ignore)
11647 int ignore ATTRIBUTE_UNUSED;
11648{
11649 expressionS ex_off;
11650 expressionS ex_sym;
11651 int reg1;
11652 int icnt = 0;
f21f8242 11653 char *f;
6478892d 11654
8586fc66 11655 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
6478892d
TS
11656 We also need NewABI support. */
11657 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11658 {
11659 s_ignore (0);
11660 return;
11661 }
11662
11663 reg1 = tc_get_register (0);
11664 SKIP_WHITESPACE ();
11665 if (*input_line_pointer != ',')
11666 {
11667 as_bad (_("missing argument separator ',' for .cpsetup"));
11668 return;
11669 }
11670 else
80245285 11671 ++input_line_pointer;
6478892d
TS
11672 SKIP_WHITESPACE ();
11673 if (*input_line_pointer == '$')
80245285
TS
11674 {
11675 mips_cpreturn_register = tc_get_register (0);
11676 mips_cpreturn_offset = -1;
11677 }
6478892d 11678 else
80245285
TS
11679 {
11680 mips_cpreturn_offset = get_absolute_expression ();
11681 mips_cpreturn_register = -1;
11682 }
6478892d
TS
11683 SKIP_WHITESPACE ();
11684 if (*input_line_pointer != ',')
11685 {
11686 as_bad (_("missing argument separator ',' for .cpsetup"));
11687 return;
11688 }
11689 else
f9419b05 11690 ++input_line_pointer;
6478892d 11691 SKIP_WHITESPACE ();
f21f8242 11692 expression (&ex_sym);
6478892d
TS
11693
11694 if (mips_cpreturn_register == -1)
11695 {
11696 ex_off.X_op = O_constant;
11697 ex_off.X_add_symbol = NULL;
11698 ex_off.X_op_symbol = NULL;
11699 ex_off.X_add_number = mips_cpreturn_offset;
11700
11701 macro_build ((char *) NULL, &icnt, &ex_off, "sd", "t,o(b)",
11702 mips_gp_register, (int) BFD_RELOC_LO16, SP);
11703 }
11704 else
11705 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
11706 "d,v,t", mips_cpreturn_register, mips_gp_register, 0);
11707
f21f8242
AO
11708 /* Ensure there's room for the next two instructions, so that `f'
11709 doesn't end up with an address in the wrong frag. */
11710 frag_grow (8);
11711 f = frag_more (0);
6478892d
TS
11712 macro_build ((char *) NULL, &icnt, &ex_sym, "lui", "t,u", mips_gp_register,
11713 (int) BFD_RELOC_GPREL16);
f21f8242
AO
11714 fix_new (frag_now, f - frag_now->fr_literal,
11715 0, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
11716 fix_new (frag_now, f - frag_now->fr_literal,
11717 0, NULL, 0, 0, BFD_RELOC_HI16_S);
11718
11719 f = frag_more (0);
6478892d
TS
11720 macro_build ((char *) NULL, &icnt, &ex_sym, "addiu", "t,r,j",
11721 mips_gp_register, mips_gp_register, (int) BFD_RELOC_GPREL16);
f21f8242
AO
11722 fix_new (frag_now, f - frag_now->fr_literal,
11723 0, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
11724 fix_new (frag_now, f - frag_now->fr_literal,
11725 0, NULL, 0, 0, BFD_RELOC_LO16);
11726
8586fc66
TS
11727 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
11728 HAVE_64BIT_ADDRESSES ? "daddu" : "addu", "d,v,t",
11729 mips_gp_register, mips_gp_register, reg1);
6478892d
TS
11730
11731 demand_empty_rest_of_line ();
11732}
11733
11734static void
11735s_cplocal (ignore)
11736 int ignore ATTRIBUTE_UNUSED;
11737{
11738 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
11739 .cplocal is ignored. */
11740 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11741 {
11742 s_ignore (0);
11743 return;
11744 }
11745
11746 mips_gp_register = tc_get_register (0);
85b51719 11747 demand_empty_rest_of_line ();
6478892d
TS
11748}
11749
252b5132
RH
11750/* Handle the .cprestore pseudo-op. This stores $gp into a given
11751 offset from $sp. The offset is remembered, and after making a PIC
11752 call $gp is restored from that location. */
11753
11754static void
11755s_cprestore (ignore)
43841e91 11756 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
11757{
11758 expressionS ex;
11759 int icnt = 0;
11760
6478892d 11761 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
c9914766 11762 .cprestore is ignored. */
6478892d 11763 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
11764 {
11765 s_ignore (0);
11766 return;
11767 }
11768
11769 mips_cprestore_offset = get_absolute_expression ();
7a621144 11770 mips_cprestore_valid = 1;
252b5132
RH
11771
11772 ex.X_op = O_constant;
11773 ex.X_add_symbol = NULL;
11774 ex.X_op_symbol = NULL;
11775 ex.X_add_number = mips_cprestore_offset;
11776
c9914766
TS
11777 macro_build ((char *) NULL, &icnt, &ex, HAVE_32BIT_ADDRESSES ? "sw" : "sd",
11778 "t,o(b)", mips_gp_register, (int) BFD_RELOC_LO16, SP);
252b5132
RH
11779
11780 demand_empty_rest_of_line ();
11781}
11782
6478892d
TS
11783/* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
11784 was given in the preceeding .gpsetup, it results in:
11785 ld $gp, offset($sp)
76b3015f 11786
6478892d
TS
11787 If a register $reg2 was given there, it results in:
11788 daddiu $gp, $gp, $reg2
11789 */
11790static void
11791s_cpreturn (ignore)
11792 int ignore ATTRIBUTE_UNUSED;
11793{
11794 expressionS ex;
11795 int icnt = 0;
11796
11797 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
11798 We also need NewABI support. */
11799 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11800 {
11801 s_ignore (0);
11802 return;
11803 }
11804
11805 if (mips_cpreturn_register == -1)
11806 {
11807 ex.X_op = O_constant;
11808 ex.X_add_symbol = NULL;
11809 ex.X_op_symbol = NULL;
11810 ex.X_add_number = mips_cpreturn_offset;
11811
11812 macro_build ((char *) NULL, &icnt, &ex, "ld", "t,o(b)",
11813 mips_gp_register, (int) BFD_RELOC_LO16, SP);
11814 }
11815 else
11816 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
11817 "d,v,t", mips_gp_register, mips_cpreturn_register, 0);
11818
11819 demand_empty_rest_of_line ();
11820}
11821
11822/* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
11823 code. It sets the offset to use in gp_rel relocations. */
11824
11825static void
11826s_gpvalue (ignore)
11827 int ignore ATTRIBUTE_UNUSED;
11828{
11829 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
11830 We also need NewABI support. */
11831 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11832 {
11833 s_ignore (0);
11834 return;
11835 }
11836
def2e0dd 11837 mips_gprel_offset = get_absolute_expression ();
6478892d
TS
11838
11839 demand_empty_rest_of_line ();
11840}
11841
252b5132
RH
11842/* Handle the .gpword pseudo-op. This is used when generating PIC
11843 code. It generates a 32 bit GP relative reloc. */
11844
11845static void
11846s_gpword (ignore)
43841e91 11847 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
11848{
11849 symbolS *label;
11850 expressionS ex;
11851 char *p;
11852
11853 /* When not generating PIC code, this is treated as .word. */
11854 if (mips_pic != SVR4_PIC)
11855 {
11856 s_cons (2);
11857 return;
11858 }
11859
11860 label = insn_labels != NULL ? insn_labels->label : NULL;
11861 mips_emit_delays (true);
11862 if (auto_align)
11863 mips_align (2, 0, label);
11864 mips_clear_insn_labels ();
11865
11866 expression (&ex);
11867
11868 if (ex.X_op != O_symbol || ex.X_add_number != 0)
11869 {
11870 as_bad (_("Unsupported use of .gpword"));
11871 ignore_rest_of_line ();
11872 }
11873
11874 p = frag_more (4);
11875 md_number_to_chars (p, (valueT) 0, 4);
c9914766 11876 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, false,
cdf6fd85 11877 BFD_RELOC_GPREL32);
252b5132
RH
11878
11879 demand_empty_rest_of_line ();
11880}
11881
11882/* Handle the .cpadd pseudo-op. This is used when dealing with switch
11883 tables in SVR4 PIC code. */
11884
11885static void
11886s_cpadd (ignore)
43841e91 11887 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
11888{
11889 int icnt = 0;
11890 int reg;
11891
6478892d
TS
11892 /* This is ignored when not generating SVR4 PIC code or if this is NewABI
11893 code. */
11894 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
11895 {
11896 s_ignore (0);
11897 return;
11898 }
11899
11900 /* Add $gp to the register named as an argument. */
11901 reg = tc_get_register (0);
11902 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 11903 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 11904 "d,v,t", reg, reg, mips_gp_register);
252b5132 11905
bdaaa2e1 11906 demand_empty_rest_of_line ();
252b5132
RH
11907}
11908
11909/* Handle the .insn pseudo-op. This marks instruction labels in
11910 mips16 mode. This permits the linker to handle them specially,
11911 such as generating jalx instructions when needed. We also make
11912 them odd for the duration of the assembly, in order to generate the
11913 right sort of code. We will make them even in the adjust_symtab
11914 routine, while leaving them marked. This is convenient for the
11915 debugger and the disassembler. The linker knows to make them odd
11916 again. */
11917
11918static void
11919s_insn (ignore)
43841e91 11920 int ignore ATTRIBUTE_UNUSED;
252b5132 11921{
f9419b05 11922 mips16_mark_labels ();
252b5132
RH
11923
11924 demand_empty_rest_of_line ();
11925}
11926
11927/* Handle a .stabn directive. We need these in order to mark a label
11928 as being a mips16 text label correctly. Sometimes the compiler
11929 will emit a label, followed by a .stabn, and then switch sections.
11930 If the label and .stabn are in mips16 mode, then the label is
11931 really a mips16 text label. */
11932
11933static void
11934s_mips_stab (type)
11935 int type;
11936{
f9419b05 11937 if (type == 'n')
252b5132
RH
11938 mips16_mark_labels ();
11939
11940 s_stab (type);
11941}
11942
11943/* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
11944 */
11945
11946static void
11947s_mips_weakext (ignore)
43841e91 11948 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
11949{
11950 char *name;
11951 int c;
11952 symbolS *symbolP;
11953 expressionS exp;
11954
11955 name = input_line_pointer;
11956 c = get_symbol_end ();
11957 symbolP = symbol_find_or_make (name);
11958 S_SET_WEAK (symbolP);
11959 *input_line_pointer = c;
11960
11961 SKIP_WHITESPACE ();
11962
11963 if (! is_end_of_line[(unsigned char) *input_line_pointer])
11964 {
11965 if (S_IS_DEFINED (symbolP))
11966 {
956cd1d6 11967 as_bad ("ignoring attempt to redefine symbol %s",
252b5132
RH
11968 S_GET_NAME (symbolP));
11969 ignore_rest_of_line ();
11970 return;
11971 }
bdaaa2e1 11972
252b5132
RH
11973 if (*input_line_pointer == ',')
11974 {
11975 ++input_line_pointer;
11976 SKIP_WHITESPACE ();
11977 }
bdaaa2e1 11978
252b5132
RH
11979 expression (&exp);
11980 if (exp.X_op != O_symbol)
11981 {
11982 as_bad ("bad .weakext directive");
98d3f06f 11983 ignore_rest_of_line ();
252b5132
RH
11984 return;
11985 }
49309057 11986 symbol_set_value_expression (symbolP, &exp);
252b5132
RH
11987 }
11988
11989 demand_empty_rest_of_line ();
11990}
11991
11992/* Parse a register string into a number. Called from the ECOFF code
11993 to parse .frame. The argument is non-zero if this is the frame
11994 register, so that we can record it in mips_frame_reg. */
11995
11996int
11997tc_get_register (frame)
11998 int frame;
11999{
12000 int reg;
12001
12002 SKIP_WHITESPACE ();
12003 if (*input_line_pointer++ != '$')
12004 {
12005 as_warn (_("expected `$'"));
85b51719 12006 reg = ZERO;
252b5132 12007 }
3882b010 12008 else if (ISDIGIT (*input_line_pointer))
252b5132
RH
12009 {
12010 reg = get_absolute_expression ();
12011 if (reg < 0 || reg >= 32)
12012 {
12013 as_warn (_("Bad register number"));
85b51719 12014 reg = ZERO;
252b5132
RH
12015 }
12016 }
12017 else
12018 {
76db943d 12019 if (strncmp (input_line_pointer, "ra", 2) == 0)
85b51719
TS
12020 {
12021 reg = RA;
12022 input_line_pointer += 2;
12023 }
76db943d 12024 else if (strncmp (input_line_pointer, "fp", 2) == 0)
85b51719
TS
12025 {
12026 reg = FP;
12027 input_line_pointer += 2;
12028 }
252b5132 12029 else if (strncmp (input_line_pointer, "sp", 2) == 0)
85b51719
TS
12030 {
12031 reg = SP;
12032 input_line_pointer += 2;
12033 }
252b5132 12034 else if (strncmp (input_line_pointer, "gp", 2) == 0)
85b51719
TS
12035 {
12036 reg = GP;
12037 input_line_pointer += 2;
12038 }
252b5132 12039 else if (strncmp (input_line_pointer, "at", 2) == 0)
85b51719
TS
12040 {
12041 reg = AT;
12042 input_line_pointer += 2;
12043 }
12044 else if (strncmp (input_line_pointer, "kt0", 3) == 0)
12045 {
12046 reg = KT0;
12047 input_line_pointer += 3;
12048 }
12049 else if (strncmp (input_line_pointer, "kt1", 3) == 0)
12050 {
12051 reg = KT1;
12052 input_line_pointer += 3;
12053 }
12054 else if (strncmp (input_line_pointer, "zero", 4) == 0)
12055 {
12056 reg = ZERO;
12057 input_line_pointer += 4;
12058 }
252b5132
RH
12059 else
12060 {
12061 as_warn (_("Unrecognized register name"));
85b51719
TS
12062 reg = ZERO;
12063 while (ISALNUM(*input_line_pointer))
12064 input_line_pointer++;
252b5132 12065 }
252b5132
RH
12066 }
12067 if (frame)
7a621144
DJ
12068 {
12069 mips_frame_reg = reg != 0 ? reg : SP;
12070 mips_frame_reg_valid = 1;
12071 mips_cprestore_valid = 0;
12072 }
252b5132
RH
12073 return reg;
12074}
12075
12076valueT
12077md_section_align (seg, addr)
12078 asection *seg;
12079 valueT addr;
12080{
12081 int align = bfd_get_section_alignment (stdoutput, seg);
12082
12083#ifdef OBJ_ELF
12084 /* We don't need to align ELF sections to the full alignment.
12085 However, Irix 5 may prefer that we align them at least to a 16
12086 byte boundary. We don't bother to align the sections if we are
12087 targeted for an embedded system. */
12088 if (strcmp (TARGET_OS, "elf") == 0)
12089 return addr;
12090 if (align > 4)
12091 align = 4;
12092#endif
12093
12094 return ((addr + (1 << align) - 1) & (-1 << align));
12095}
12096
12097/* Utility routine, called from above as well. If called while the
12098 input file is still being read, it's only an approximation. (For
12099 example, a symbol may later become defined which appeared to be
12100 undefined earlier.) */
12101
12102static int
12103nopic_need_relax (sym, before_relaxing)
12104 symbolS *sym;
12105 int before_relaxing;
12106{
12107 if (sym == 0)
12108 return 0;
12109
6478892d 12110 if (USE_GLOBAL_POINTER_OPT && g_switch_value > 0)
252b5132
RH
12111 {
12112 const char *symname;
12113 int change;
12114
c9914766 12115 /* Find out whether this symbol can be referenced off the $gp
252b5132
RH
12116 register. It can be if it is smaller than the -G size or if
12117 it is in the .sdata or .sbss section. Certain symbols can
c9914766 12118 not be referenced off the $gp, although it appears as though
252b5132
RH
12119 they can. */
12120 symname = S_GET_NAME (sym);
12121 if (symname != (const char *) NULL
12122 && (strcmp (symname, "eprol") == 0
12123 || strcmp (symname, "etext") == 0
12124 || strcmp (symname, "_gp") == 0
12125 || strcmp (symname, "edata") == 0
12126 || strcmp (symname, "_fbss") == 0
12127 || strcmp (symname, "_fdata") == 0
12128 || strcmp (symname, "_ftext") == 0
12129 || strcmp (symname, "end") == 0
12130 || strcmp (symname, "_gp_disp") == 0))
12131 change = 1;
12132 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
12133 && (0
12134#ifndef NO_ECOFF_DEBUGGING
49309057
ILT
12135 || (symbol_get_obj (sym)->ecoff_extern_size != 0
12136 && (symbol_get_obj (sym)->ecoff_extern_size
12137 <= g_switch_value))
252b5132
RH
12138#endif
12139 /* We must defer this decision until after the whole
12140 file has been read, since there might be a .extern
12141 after the first use of this symbol. */
12142 || (before_relaxing
12143#ifndef NO_ECOFF_DEBUGGING
49309057 12144 && symbol_get_obj (sym)->ecoff_extern_size == 0
252b5132
RH
12145#endif
12146 && S_GET_VALUE (sym) == 0)
12147 || (S_GET_VALUE (sym) != 0
12148 && S_GET_VALUE (sym) <= g_switch_value)))
12149 change = 0;
12150 else
12151 {
12152 const char *segname;
12153
12154 segname = segment_name (S_GET_SEGMENT (sym));
12155 assert (strcmp (segname, ".lit8") != 0
12156 && strcmp (segname, ".lit4") != 0);
12157 change = (strcmp (segname, ".sdata") != 0
fba2b7f9
GK
12158 && strcmp (segname, ".sbss") != 0
12159 && strncmp (segname, ".sdata.", 7) != 0
12160 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
252b5132
RH
12161 }
12162 return change;
12163 }
12164 else
c9914766 12165 /* We are not optimizing for the $gp register. */
252b5132
RH
12166 return 1;
12167}
12168
12169/* Given a mips16 variant frag FRAGP, return non-zero if it needs an
12170 extended opcode. SEC is the section the frag is in. */
12171
12172static int
12173mips16_extended_frag (fragp, sec, stretch)
12174 fragS *fragp;
12175 asection *sec;
12176 long stretch;
12177{
12178 int type;
12179 register const struct mips16_immed_operand *op;
12180 offsetT val;
12181 int mintiny, maxtiny;
12182 segT symsec;
98aa84af 12183 fragS *sym_frag;
252b5132
RH
12184
12185 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
12186 return 0;
12187 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
12188 return 1;
12189
12190 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12191 op = mips16_immed_operands;
12192 while (op->type != type)
12193 {
12194 ++op;
12195 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
12196 }
12197
12198 if (op->unsp)
12199 {
12200 if (type == '<' || type == '>' || type == '[' || type == ']')
12201 {
12202 mintiny = 1;
12203 maxtiny = 1 << op->nbits;
12204 }
12205 else
12206 {
12207 mintiny = 0;
12208 maxtiny = (1 << op->nbits) - 1;
12209 }
12210 }
12211 else
12212 {
12213 mintiny = - (1 << (op->nbits - 1));
12214 maxtiny = (1 << (op->nbits - 1)) - 1;
12215 }
12216
98aa84af 12217 sym_frag = symbol_get_frag (fragp->fr_symbol);
ac62c346 12218 val = S_GET_VALUE (fragp->fr_symbol);
98aa84af 12219 symsec = S_GET_SEGMENT (fragp->fr_symbol);
252b5132
RH
12220
12221 if (op->pcrel)
12222 {
12223 addressT addr;
12224
12225 /* We won't have the section when we are called from
12226 mips_relax_frag. However, we will always have been called
12227 from md_estimate_size_before_relax first. If this is a
12228 branch to a different section, we mark it as such. If SEC is
12229 NULL, and the frag is not marked, then it must be a branch to
12230 the same section. */
12231 if (sec == NULL)
12232 {
12233 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
12234 return 1;
12235 }
12236 else
12237 {
98aa84af 12238 /* Must have been called from md_estimate_size_before_relax. */
252b5132
RH
12239 if (symsec != sec)
12240 {
12241 fragp->fr_subtype =
12242 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12243
12244 /* FIXME: We should support this, and let the linker
12245 catch branches and loads that are out of range. */
12246 as_bad_where (fragp->fr_file, fragp->fr_line,
12247 _("unsupported PC relative reference to different section"));
12248
12249 return 1;
12250 }
98aa84af
AM
12251 if (fragp != sym_frag && sym_frag->fr_address == 0)
12252 /* Assume non-extended on the first relaxation pass.
12253 The address we have calculated will be bogus if this is
12254 a forward branch to another frag, as the forward frag
12255 will have fr_address == 0. */
12256 return 0;
252b5132
RH
12257 }
12258
12259 /* In this case, we know for sure that the symbol fragment is in
98aa84af
AM
12260 the same section. If the relax_marker of the symbol fragment
12261 differs from the relax_marker of this fragment, we have not
12262 yet adjusted the symbol fragment fr_address. We want to add
252b5132
RH
12263 in STRETCH in order to get a better estimate of the address.
12264 This particularly matters because of the shift bits. */
12265 if (stretch != 0
98aa84af 12266 && sym_frag->relax_marker != fragp->relax_marker)
252b5132
RH
12267 {
12268 fragS *f;
12269
12270 /* Adjust stretch for any alignment frag. Note that if have
12271 been expanding the earlier code, the symbol may be
12272 defined in what appears to be an earlier frag. FIXME:
12273 This doesn't handle the fr_subtype field, which specifies
12274 a maximum number of bytes to skip when doing an
12275 alignment. */
98aa84af 12276 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
252b5132
RH
12277 {
12278 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
12279 {
12280 if (stretch < 0)
12281 stretch = - ((- stretch)
12282 & ~ ((1 << (int) f->fr_offset) - 1));
12283 else
12284 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
12285 if (stretch == 0)
12286 break;
12287 }
12288 }
12289 if (f != NULL)
12290 val += stretch;
12291 }
12292
12293 addr = fragp->fr_address + fragp->fr_fix;
12294
12295 /* The base address rules are complicated. The base address of
12296 a branch is the following instruction. The base address of a
12297 PC relative load or add is the instruction itself, but if it
12298 is in a delay slot (in which case it can not be extended) use
12299 the address of the instruction whose delay slot it is in. */
12300 if (type == 'p' || type == 'q')
12301 {
12302 addr += 2;
12303
12304 /* If we are currently assuming that this frag should be
12305 extended, then, the current address is two bytes
bdaaa2e1 12306 higher. */
252b5132
RH
12307 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12308 addr += 2;
12309
12310 /* Ignore the low bit in the target, since it will be set
12311 for a text label. */
12312 if ((val & 1) != 0)
12313 --val;
12314 }
12315 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12316 addr -= 4;
12317 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12318 addr -= 2;
12319
12320 val -= addr & ~ ((1 << op->shift) - 1);
12321
12322 /* Branch offsets have an implicit 0 in the lowest bit. */
12323 if (type == 'p' || type == 'q')
12324 val /= 2;
12325
12326 /* If any of the shifted bits are set, we must use an extended
12327 opcode. If the address depends on the size of this
12328 instruction, this can lead to a loop, so we arrange to always
12329 use an extended opcode. We only check this when we are in
12330 the main relaxation loop, when SEC is NULL. */
12331 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
12332 {
12333 fragp->fr_subtype =
12334 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12335 return 1;
12336 }
12337
12338 /* If we are about to mark a frag as extended because the value
12339 is precisely maxtiny + 1, then there is a chance of an
12340 infinite loop as in the following code:
12341 la $4,foo
12342 .skip 1020
12343 .align 2
12344 foo:
12345 In this case when the la is extended, foo is 0x3fc bytes
12346 away, so the la can be shrunk, but then foo is 0x400 away, so
12347 the la must be extended. To avoid this loop, we mark the
12348 frag as extended if it was small, and is about to become
12349 extended with a value of maxtiny + 1. */
12350 if (val == ((maxtiny + 1) << op->shift)
12351 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
12352 && sec == NULL)
12353 {
12354 fragp->fr_subtype =
12355 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12356 return 1;
12357 }
12358 }
12359 else if (symsec != absolute_section && sec != NULL)
12360 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
12361
12362 if ((val & ((1 << op->shift) - 1)) != 0
12363 || val < (mintiny << op->shift)
12364 || val > (maxtiny << op->shift))
12365 return 1;
12366 else
12367 return 0;
12368}
12369
12370/* Estimate the size of a frag before relaxing. Unless this is the
12371 mips16, we are not really relaxing here, and the final size is
12372 encoded in the subtype information. For the mips16, we have to
12373 decide whether we are using an extended opcode or not. */
12374
252b5132
RH
12375int
12376md_estimate_size_before_relax (fragp, segtype)
12377 fragS *fragp;
12378 asection *segtype;
12379{
43841e91 12380 int change = 0;
8614eeee 12381 boolean linkonce = false;
252b5132
RH
12382
12383 if (RELAX_MIPS16_P (fragp->fr_subtype))
177b4a6a
AO
12384 /* We don't want to modify the EXTENDED bit here; it might get us
12385 into infinite loops. We change it only in mips_relax_frag(). */
12386 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
252b5132
RH
12387
12388 if (mips_pic == NO_PIC)
12389 {
12390 change = nopic_need_relax (fragp->fr_symbol, 0);
12391 }
12392 else if (mips_pic == SVR4_PIC)
12393 {
12394 symbolS *sym;
12395 asection *symsec;
12396
12397 sym = fragp->fr_symbol;
12398
12399 /* Handle the case of a symbol equated to another symbol. */
e0890092 12400 while (symbol_equated_reloc_p (sym))
252b5132
RH
12401 {
12402 symbolS *n;
12403
12404 /* It's possible to get a loop here in a badly written
12405 program. */
49309057 12406 n = symbol_get_value_expression (sym)->X_add_symbol;
252b5132
RH
12407 if (n == sym)
12408 break;
12409 sym = n;
12410 }
12411
12412 symsec = S_GET_SEGMENT (sym);
12413
8614eeee
UC
12414 /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
12415 if (symsec != segtype && ! S_IS_LOCAL (sym))
beae10d5
KH
12416 {
12417 if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
12418 != 0)
12419 linkonce = true;
12420
12421 /* The GNU toolchain uses an extension for ELF: a section
12422 beginning with the magic string .gnu.linkonce is a linkonce
12423 section. */
12424 if (strncmp (segment_name (symsec), ".gnu.linkonce",
12425 sizeof ".gnu.linkonce" - 1) == 0)
12426 linkonce = true;
12427 }
8614eeee 12428
252b5132
RH
12429 /* This must duplicate the test in adjust_reloc_syms. */
12430 change = (symsec != &bfd_und_section
12431 && symsec != &bfd_abs_section
426b0403 12432 && ! bfd_is_com_section (symsec)
8614eeee 12433 && !linkonce
426b0403 12434#ifdef OBJ_ELF
ea4ff978 12435 /* A global or weak symbol is treated as external. */
9151e8bf 12436 && (OUTPUT_FLAVOR != bfd_target_elf_flavour
b25a253c 12437 || (! S_IS_WEAK (sym)
bad9ca53
TS
12438 && (! S_IS_EXTERNAL (sym)
12439 || mips_pic == EMBEDDED_PIC)))
426b0403
AM
12440#endif
12441 );
252b5132
RH
12442 }
12443 else
12444 abort ();
12445
12446 if (change)
12447 {
12448 /* Record the offset to the first reloc in the fr_opcode field.
12449 This lets md_convert_frag and tc_gen_reloc know that the code
12450 must be expanded. */
12451 fragp->fr_opcode = (fragp->fr_literal
12452 + fragp->fr_fix
12453 - RELAX_OLD (fragp->fr_subtype)
12454 + RELAX_RELOC1 (fragp->fr_subtype));
12455 /* FIXME: This really needs as_warn_where. */
12456 if (RELAX_WARN (fragp->fr_subtype))
9a41af64
TS
12457 as_warn (_("AT used after \".set noat\" or macro used after "
12458 "\".set nomacro\""));
12459
12460 return RELAX_NEW (fragp->fr_subtype) - RELAX_OLD (fragp->fr_subtype);
252b5132
RH
12461 }
12462
9a41af64 12463 return 0;
252b5132
RH
12464}
12465
12466/* This is called to see whether a reloc against a defined symbol
12467 should be converted into a reloc against a section. Don't adjust
12468 MIPS16 jump relocations, so we don't have to worry about the format
12469 of the offset in the .o file. Don't adjust relocations against
12470 mips16 symbols, so that the linker can find them if it needs to set
12471 up a stub. */
12472
12473int
12474mips_fix_adjustable (fixp)
12475 fixS *fixp;
12476{
ea4ff978
L
12477#ifdef OBJ_ELF
12478 /* Prevent all adjustments to global symbols. */
46bac6de 12479 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
b25a253c 12480 && mips_pic != EMBEDDED_PIC
bad9ca53 12481 && (S_IS_EXTERNAL (fixp->fx_addsy) || S_IS_WEAK (fixp->fx_addsy)))
ea4ff978
L
12482 return 0;
12483#endif
252b5132
RH
12484 if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
12485 return 0;
12486 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
12487 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12488 return 0;
12489 if (fixp->fx_addsy == NULL)
12490 return 1;
12491#ifdef OBJ_ELF
12492 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
12493 && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
12494 && fixp->fx_subsy == NULL)
12495 return 0;
12496#endif
12497 return 1;
12498}
12499
12500/* Translate internal representation of relocation info to BFD target
12501 format. */
12502
12503arelent **
12504tc_gen_reloc (section, fixp)
43841e91 12505 asection *section ATTRIBUTE_UNUSED;
252b5132
RH
12506 fixS *fixp;
12507{
12508 static arelent *retval[4];
12509 arelent *reloc;
12510 bfd_reloc_code_real_type code;
12511
12512 reloc = retval[0] = (arelent *) xmalloc (sizeof (arelent));
12513 retval[1] = NULL;
12514
49309057
ILT
12515 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
12516 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
12517 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
12518
12519 if (mips_pic == EMBEDDED_PIC
12520 && SWITCH_TABLE (fixp))
12521 {
12522 /* For a switch table entry we use a special reloc. The addend
12523 is actually the difference between the reloc address and the
12524 subtrahend. */
12525 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
12526 if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
12527 as_fatal (_("Double check fx_r_type in tc-mips.c:tc_gen_reloc"));
12528 fixp->fx_r_type = BFD_RELOC_GPREL32;
12529 }
12530 else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16)
12531 {
4514d474
CD
12532 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
12533 reloc->addend = fixp->fx_addnumber;
252b5132 12534 else
4514d474
CD
12535 {
12536 /* We use a special addend for an internal RELLO reloc. */
12537 if (symbol_section_p (fixp->fx_addsy))
12538 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
12539 else
12540 reloc->addend = fixp->fx_addnumber + reloc->address;
12541 }
252b5132
RH
12542 }
12543 else if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
12544 {
12545 assert (fixp->fx_next != NULL
12546 && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
4514d474
CD
12547
12548 /* The reloc is relative to the RELLO; adjust the addend
252b5132 12549 accordingly. */
4514d474
CD
12550 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
12551 reloc->addend = fixp->fx_next->fx_addnumber;
252b5132 12552 else
4514d474
CD
12553 {
12554 /* We use a special addend for an internal RELHI reloc. */
12555 if (symbol_section_p (fixp->fx_addsy))
12556 reloc->addend = (fixp->fx_next->fx_frag->fr_address
12557 + fixp->fx_next->fx_where
12558 - S_GET_VALUE (fixp->fx_subsy));
12559 else
12560 reloc->addend = (fixp->fx_addnumber
12561 + fixp->fx_next->fx_frag->fr_address
12562 + fixp->fx_next->fx_where);
12563 }
252b5132 12564 }
4514d474
CD
12565 else if (fixp->fx_pcrel == 0 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
12566 reloc->addend = fixp->fx_addnumber;
252b5132
RH
12567 else
12568 {
12569 if (OUTPUT_FLAVOR != bfd_target_aout_flavour)
12570 /* A gruesome hack which is a result of the gruesome gas reloc
12571 handling. */
12572 reloc->addend = reloc->address;
12573 else
12574 reloc->addend = -reloc->address;
12575 }
12576
12577 /* If this is a variant frag, we may need to adjust the existing
12578 reloc and generate a new one. */
12579 if (fixp->fx_frag->fr_opcode != NULL
cdf6fd85 12580 && (fixp->fx_r_type == BFD_RELOC_GPREL16
252b5132
RH
12581 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
12582 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL16
12583 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
12584 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16
12585 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
6478892d
TS
12586 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_LO16)
12587 && ! HAVE_NEWABI)
252b5132
RH
12588 {
12589 arelent *reloc2;
12590
12591 assert (! RELAX_MIPS16_P (fixp->fx_frag->fr_subtype));
12592
12593 /* If this is not the last reloc in this frag, then we have two
12594 GPREL relocs, or a GOT_HI16/GOT_LO16 pair, or a
12595 CALL_HI16/CALL_LO16, both of which are being replaced. Let
12596 the second one handle all of them. */
12597 if (fixp->fx_next != NULL
12598 && fixp->fx_frag == fixp->fx_next->fx_frag)
12599 {
cdf6fd85
TS
12600 assert ((fixp->fx_r_type == BFD_RELOC_GPREL16
12601 && fixp->fx_next->fx_r_type == BFD_RELOC_GPREL16)
252b5132
RH
12602 || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
12603 && (fixp->fx_next->fx_r_type
12604 == BFD_RELOC_MIPS_GOT_LO16))
12605 || (fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
12606 && (fixp->fx_next->fx_r_type
12607 == BFD_RELOC_MIPS_CALL_LO16)));
12608 retval[0] = NULL;
12609 return retval;
12610 }
12611
12612 fixp->fx_where = fixp->fx_frag->fr_opcode - fixp->fx_frag->fr_literal;
12613 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
12614 reloc2 = retval[1] = (arelent *) xmalloc (sizeof (arelent));
12615 retval[2] = NULL;
49309057
ILT
12616 reloc2->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
12617 *reloc2->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
12618 reloc2->address = (reloc->address
12619 + (RELAX_RELOC2 (fixp->fx_frag->fr_subtype)
12620 - RELAX_RELOC1 (fixp->fx_frag->fr_subtype)));
12621 reloc2->addend = fixp->fx_addnumber;
12622 reloc2->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO16);
12623 assert (reloc2->howto != NULL);
12624
12625 if (RELAX_RELOC3 (fixp->fx_frag->fr_subtype))
12626 {
12627 arelent *reloc3;
12628
12629 reloc3 = retval[2] = (arelent *) xmalloc (sizeof (arelent));
12630 retval[3] = NULL;
12631 *reloc3 = *reloc2;
12632 reloc3->address += 4;
12633 }
12634
12635 if (mips_pic == NO_PIC)
12636 {
cdf6fd85 12637 assert (fixp->fx_r_type == BFD_RELOC_GPREL16);
252b5132
RH
12638 fixp->fx_r_type = BFD_RELOC_HI16_S;
12639 }
12640 else if (mips_pic == SVR4_PIC)
12641 {
12642 switch (fixp->fx_r_type)
12643 {
12644 default:
12645 abort ();
12646 case BFD_RELOC_MIPS_GOT16:
12647 break;
12648 case BFD_RELOC_MIPS_CALL16:
12649 case BFD_RELOC_MIPS_GOT_LO16:
12650 case BFD_RELOC_MIPS_CALL_LO16:
12651 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
12652 break;
12653 }
12654 }
12655 else
12656 abort ();
12657 }
12658
438c16b8
TS
12659 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
12660 entry to be used in the relocation's section offset. */
12661 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
252b5132
RH
12662 {
12663 reloc->address = reloc->addend;
12664 reloc->addend = 0;
12665 }
12666
12667 /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
12668 fixup_segment converted a non-PC relative reloc into a PC
12669 relative reloc. In such a case, we need to convert the reloc
12670 code. */
12671 code = fixp->fx_r_type;
12672 if (fixp->fx_pcrel)
12673 {
12674 switch (code)
12675 {
12676 case BFD_RELOC_8:
12677 code = BFD_RELOC_8_PCREL;
12678 break;
12679 case BFD_RELOC_16:
12680 code = BFD_RELOC_16_PCREL;
12681 break;
12682 case BFD_RELOC_32:
12683 code = BFD_RELOC_32_PCREL;
12684 break;
12685 case BFD_RELOC_64:
12686 code = BFD_RELOC_64_PCREL;
12687 break;
12688 case BFD_RELOC_8_PCREL:
12689 case BFD_RELOC_16_PCREL:
12690 case BFD_RELOC_32_PCREL:
12691 case BFD_RELOC_64_PCREL:
12692 case BFD_RELOC_16_PCREL_S2:
12693 case BFD_RELOC_PCREL_HI16_S:
12694 case BFD_RELOC_PCREL_LO16:
12695 break;
12696 default:
12697 as_bad_where (fixp->fx_file, fixp->fx_line,
12698 _("Cannot make %s relocation PC relative"),
12699 bfd_get_reloc_code_name (code));
12700 }
12701 }
12702
add55e1f
RS
12703#ifdef OBJ_ELF
12704 /* md_apply_fix3 has a double-subtraction hack to get
12705 bfd_install_relocation to behave nicely. GPREL relocations are
12706 handled correctly without this hack, so undo it here. We can't
12707 stop md_apply_fix3 from subtracting twice in the first place since
12708 the fake addend is required for variant frags above. */
12709 if (fixp->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour
98605598 12710 && (code == BFD_RELOC_GPREL16 || code == BFD_RELOC_MIPS16_GPREL)
add55e1f
RS
12711 && reloc->addend != 0
12712 && mips_need_elf_addend_fixup (fixp))
12713 reloc->addend += S_GET_VALUE (fixp->fx_addsy);
12714#endif
12715
252b5132
RH
12716 /* To support a PC relative reloc when generating embedded PIC code
12717 for ECOFF, we use a Cygnus extension. We check for that here to
12718 make sure that we don't let such a reloc escape normally. */
bb2d6cd7
GK
12719 if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
12720 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
252b5132
RH
12721 && code == BFD_RELOC_16_PCREL_S2
12722 && mips_pic != EMBEDDED_PIC)
12723 reloc->howto = NULL;
12724 else
12725 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
12726
12727 if (reloc->howto == NULL)
12728 {
12729 as_bad_where (fixp->fx_file, fixp->fx_line,
12730 _("Can not represent %s relocation in this object file format"),
12731 bfd_get_reloc_code_name (code));
12732 retval[0] = NULL;
12733 }
12734
12735 return retval;
12736}
12737
12738/* Relax a machine dependent frag. This returns the amount by which
12739 the current size of the frag should change. */
12740
12741int
12742mips_relax_frag (fragp, stretch)
12743 fragS *fragp;
12744 long stretch;
12745{
12746 if (! RELAX_MIPS16_P (fragp->fr_subtype))
12747 return 0;
12748
c4e7957c 12749 if (mips16_extended_frag (fragp, NULL, stretch))
252b5132
RH
12750 {
12751 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12752 return 0;
12753 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
12754 return 2;
12755 }
12756 else
12757 {
12758 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12759 return 0;
12760 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
12761 return -2;
12762 }
12763
12764 return 0;
12765}
12766
12767/* Convert a machine dependent frag. */
12768
12769void
12770md_convert_frag (abfd, asec, fragp)
43841e91 12771 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
12772 segT asec;
12773 fragS *fragp;
12774{
12775 int old, new;
12776 char *fixptr;
12777
12778 if (RELAX_MIPS16_P (fragp->fr_subtype))
12779 {
12780 int type;
12781 register const struct mips16_immed_operand *op;
12782 boolean small, ext;
12783 offsetT val;
12784 bfd_byte *buf;
12785 unsigned long insn;
12786 boolean use_extend;
12787 unsigned short extend;
12788
12789 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12790 op = mips16_immed_operands;
12791 while (op->type != type)
12792 ++op;
12793
12794 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12795 {
12796 small = false;
12797 ext = true;
12798 }
12799 else
12800 {
12801 small = true;
12802 ext = false;
12803 }
12804
6386f3a7 12805 resolve_symbol_value (fragp->fr_symbol);
252b5132
RH
12806 val = S_GET_VALUE (fragp->fr_symbol);
12807 if (op->pcrel)
12808 {
12809 addressT addr;
12810
12811 addr = fragp->fr_address + fragp->fr_fix;
12812
12813 /* The rules for the base address of a PC relative reloc are
12814 complicated; see mips16_extended_frag. */
12815 if (type == 'p' || type == 'q')
12816 {
12817 addr += 2;
12818 if (ext)
12819 addr += 2;
12820 /* Ignore the low bit in the target, since it will be
12821 set for a text label. */
12822 if ((val & 1) != 0)
12823 --val;
12824 }
12825 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12826 addr -= 4;
12827 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12828 addr -= 2;
12829
12830 addr &= ~ (addressT) ((1 << op->shift) - 1);
12831 val -= addr;
12832
12833 /* Make sure the section winds up with the alignment we have
12834 assumed. */
12835 if (op->shift > 0)
12836 record_alignment (asec, op->shift);
12837 }
12838
12839 if (ext
12840 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
12841 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
12842 as_warn_where (fragp->fr_file, fragp->fr_line,
12843 _("extended instruction in delay slot"));
12844
12845 buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
12846
12847 if (target_big_endian)
12848 insn = bfd_getb16 (buf);
12849 else
12850 insn = bfd_getl16 (buf);
12851
12852 mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
12853 RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
12854 small, ext, &insn, &use_extend, &extend);
12855
12856 if (use_extend)
12857 {
874e8986 12858 md_number_to_chars ((char *) buf, 0xf000 | extend, 2);
252b5132
RH
12859 fragp->fr_fix += 2;
12860 buf += 2;
12861 }
12862
874e8986 12863 md_number_to_chars ((char *) buf, insn, 2);
252b5132
RH
12864 fragp->fr_fix += 2;
12865 buf += 2;
12866 }
12867 else
12868 {
12869 if (fragp->fr_opcode == NULL)
12870 return;
12871
12872 old = RELAX_OLD (fragp->fr_subtype);
12873 new = RELAX_NEW (fragp->fr_subtype);
12874 fixptr = fragp->fr_literal + fragp->fr_fix;
12875
12876 if (new > 0)
12877 memcpy (fixptr - old, fixptr, new);
12878
12879 fragp->fr_fix += new - old;
12880 }
12881}
12882
12883#ifdef OBJ_ELF
12884
12885/* This function is called after the relocs have been generated.
12886 We've been storing mips16 text labels as odd. Here we convert them
12887 back to even for the convenience of the debugger. */
12888
12889void
12890mips_frob_file_after_relocs ()
12891{
12892 asymbol **syms;
12893 unsigned int count, i;
12894
12895 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
12896 return;
12897
12898 syms = bfd_get_outsymbols (stdoutput);
12899 count = bfd_get_symcount (stdoutput);
12900 for (i = 0; i < count; i++, syms++)
12901 {
12902 if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
12903 && ((*syms)->value & 1) != 0)
12904 {
12905 (*syms)->value &= ~1;
12906 /* If the symbol has an odd size, it was probably computed
12907 incorrectly, so adjust that as well. */
12908 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
12909 ++elf_symbol (*syms)->internal_elf_sym.st_size;
12910 }
12911 }
12912}
12913
12914#endif
12915
12916/* This function is called whenever a label is defined. It is used
12917 when handling branch delays; if a branch has a label, we assume we
12918 can not move it. */
12919
12920void
12921mips_define_label (sym)
12922 symbolS *sym;
12923{
12924 struct insn_label_list *l;
12925
12926 if (free_insn_labels == NULL)
12927 l = (struct insn_label_list *) xmalloc (sizeof *l);
12928 else
12929 {
12930 l = free_insn_labels;
12931 free_insn_labels = l->next;
12932 }
12933
12934 l->label = sym;
12935 l->next = insn_labels;
12936 insn_labels = l;
12937}
12938\f
12939#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
12940
12941/* Some special processing for a MIPS ELF file. */
12942
12943void
12944mips_elf_final_processing ()
12945{
12946 /* Write out the register information. */
316f5878 12947 if (mips_abi != N64_ABI)
252b5132
RH
12948 {
12949 Elf32_RegInfo s;
12950
12951 s.ri_gprmask = mips_gprmask;
12952 s.ri_cprmask[0] = mips_cprmask[0];
12953 s.ri_cprmask[1] = mips_cprmask[1];
12954 s.ri_cprmask[2] = mips_cprmask[2];
12955 s.ri_cprmask[3] = mips_cprmask[3];
12956 /* The gp_value field is set by the MIPS ELF backend. */
12957
12958 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
12959 ((Elf32_External_RegInfo *)
12960 mips_regmask_frag));
12961 }
12962 else
12963 {
12964 Elf64_Internal_RegInfo s;
12965
12966 s.ri_gprmask = mips_gprmask;
12967 s.ri_pad = 0;
12968 s.ri_cprmask[0] = mips_cprmask[0];
12969 s.ri_cprmask[1] = mips_cprmask[1];
12970 s.ri_cprmask[2] = mips_cprmask[2];
12971 s.ri_cprmask[3] = mips_cprmask[3];
12972 /* The gp_value field is set by the MIPS ELF backend. */
12973
12974 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
12975 ((Elf64_External_RegInfo *)
12976 mips_regmask_frag));
12977 }
12978
12979 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
12980 sort of BFD interface for this. */
12981 if (mips_any_noreorder)
12982 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
12983 if (mips_pic != NO_PIC)
12984 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
12985
98d3f06f 12986 /* Set MIPS ELF flags for ASEs. */
a4672219
TS
12987 if (file_ase_mips16)
12988 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
1f25f5d3
CD
12989#if 0 /* XXX FIXME */
12990 if (file_ase_mips3d)
12991 elf_elfheader (stdoutput)->e_flags |= ???;
12992#endif
deec1734
CD
12993 if (file_ase_mdmx)
12994 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
1f25f5d3 12995
bdaaa2e1 12996 /* Set the MIPS ELF ABI flags. */
316f5878 12997 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
252b5132 12998 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
316f5878 12999 else if (mips_abi == O64_ABI)
252b5132 13000 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
316f5878 13001 else if (mips_abi == EABI_ABI)
252b5132 13002 {
316f5878 13003 if (!file_mips_gp32)
252b5132
RH
13004 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
13005 else
13006 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
13007 }
316f5878 13008 else if (mips_abi == N32_ABI)
be00bddd
TS
13009 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
13010
c9914766 13011 /* Nothing to do for N64_ABI. */
252b5132
RH
13012
13013 if (mips_32bitmode)
13014 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
13015}
13016
13017#endif /* OBJ_ELF || OBJ_MAYBE_ELF */
13018\f
beae10d5
KH
13019typedef struct proc {
13020 symbolS *isym;
13021 unsigned long reg_mask;
13022 unsigned long reg_offset;
13023 unsigned long fpreg_mask;
13024 unsigned long fpreg_offset;
13025 unsigned long frame_offset;
13026 unsigned long frame_reg;
13027 unsigned long pc_reg;
13028} procS;
252b5132
RH
13029
13030static procS cur_proc;
13031static procS *cur_proc_ptr;
13032static int numprocs;
13033
0a9ef439 13034/* Fill in an rs_align_code fragment. */
a19d8eb0 13035
0a9ef439
RH
13036void
13037mips_handle_align (fragp)
13038 fragS *fragp;
a19d8eb0 13039{
0a9ef439
RH
13040 if (fragp->fr_type != rs_align_code)
13041 return;
13042
13043 if (mips_opts.mips16)
a19d8eb0
CP
13044 {
13045 static const unsigned char be_nop[] = { 0x65, 0x00 };
13046 static const unsigned char le_nop[] = { 0x00, 0x65 };
13047
0a9ef439
RH
13048 int bytes;
13049 char *p;
a19d8eb0 13050
0a9ef439
RH
13051 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
13052 p = fragp->fr_literal + fragp->fr_fix;
13053
13054 if (bytes & 1)
13055 {
13056 *p++ = 0;
f9419b05 13057 fragp->fr_fix++;
0a9ef439
RH
13058 }
13059
13060 memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
13061 fragp->fr_var = 2;
a19d8eb0
CP
13062 }
13063
0a9ef439 13064 /* For mips32, a nop is a zero, which we trivially get by doing nothing. */
a19d8eb0
CP
13065}
13066
252b5132
RH
13067static void
13068md_obj_begin ()
13069{
13070}
13071
13072static void
13073md_obj_end ()
13074{
13075 /* check for premature end, nesting errors, etc */
13076 if (cur_proc_ptr)
9a41af64 13077 as_warn (_("missing .end at end of assembly"));
252b5132
RH
13078}
13079
13080static long
13081get_number ()
13082{
13083 int negative = 0;
13084 long val = 0;
13085
13086 if (*input_line_pointer == '-')
13087 {
13088 ++input_line_pointer;
13089 negative = 1;
13090 }
3882b010 13091 if (!ISDIGIT (*input_line_pointer))
956cd1d6 13092 as_bad (_("expected simple number"));
252b5132
RH
13093 if (input_line_pointer[0] == '0')
13094 {
13095 if (input_line_pointer[1] == 'x')
13096 {
13097 input_line_pointer += 2;
3882b010 13098 while (ISXDIGIT (*input_line_pointer))
252b5132
RH
13099 {
13100 val <<= 4;
13101 val |= hex_value (*input_line_pointer++);
13102 }
13103 return negative ? -val : val;
13104 }
13105 else
13106 {
13107 ++input_line_pointer;
3882b010 13108 while (ISDIGIT (*input_line_pointer))
252b5132
RH
13109 {
13110 val <<= 3;
13111 val |= *input_line_pointer++ - '0';
13112 }
13113 return negative ? -val : val;
13114 }
13115 }
3882b010 13116 if (!ISDIGIT (*input_line_pointer))
252b5132
RH
13117 {
13118 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
13119 *input_line_pointer, *input_line_pointer);
956cd1d6 13120 as_warn (_("invalid number"));
252b5132
RH
13121 return -1;
13122 }
3882b010 13123 while (ISDIGIT (*input_line_pointer))
252b5132
RH
13124 {
13125 val *= 10;
13126 val += *input_line_pointer++ - '0';
13127 }
13128 return negative ? -val : val;
13129}
13130
13131/* The .file directive; just like the usual .file directive, but there
c5dd6aab
DJ
13132 is an initial number which is the ECOFF file index. In the non-ECOFF
13133 case .file implies DWARF-2. */
13134
13135static void
13136s_mips_file (x)
13137 int x ATTRIBUTE_UNUSED;
13138{
ecb4347a
DJ
13139 static int first_file_directive = 0;
13140
c5dd6aab
DJ
13141 if (ECOFF_DEBUGGING)
13142 {
13143 get_number ();
13144 s_app_file (0);
13145 }
13146 else
ecb4347a
DJ
13147 {
13148 char *filename;
13149
13150 filename = dwarf2_directive_file (0);
13151
13152 /* Versions of GCC up to 3.1 start files with a ".file"
13153 directive even for stabs output. Make sure that this
13154 ".file" is handled. Note that you need a version of GCC
13155 after 3.1 in order to support DWARF-2 on MIPS. */
13156 if (filename != NULL && ! first_file_directive)
13157 {
13158 (void) new_logical_line (filename, -1);
13159 s_app_file_string (filename);
13160 }
13161 first_file_directive = 1;
13162 }
c5dd6aab
DJ
13163}
13164
13165/* The .loc directive, implying DWARF-2. */
252b5132
RH
13166
13167static void
c5dd6aab 13168s_mips_loc (x)
43841e91 13169 int x ATTRIBUTE_UNUSED;
252b5132 13170{
c5dd6aab
DJ
13171 if (!ECOFF_DEBUGGING)
13172 dwarf2_directive_loc (0);
252b5132
RH
13173}
13174
252b5132
RH
13175/* The .end directive. */
13176
13177static void
13178s_mips_end (x)
43841e91 13179 int x ATTRIBUTE_UNUSED;
252b5132
RH
13180{
13181 symbolS *p;
13182 int maybe_text;
13183
7a621144
DJ
13184 /* Following functions need their own .frame and .cprestore directives. */
13185 mips_frame_reg_valid = 0;
13186 mips_cprestore_valid = 0;
13187
252b5132
RH
13188 if (!is_end_of_line[(unsigned char) *input_line_pointer])
13189 {
13190 p = get_symbol ();
13191 demand_empty_rest_of_line ();
13192 }
13193 else
13194 p = NULL;
13195
13196#ifdef BFD_ASSEMBLER
13197 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
13198 maybe_text = 1;
13199 else
13200 maybe_text = 0;
13201#else
13202 if (now_seg != data_section && now_seg != bss_section)
13203 maybe_text = 1;
13204 else
13205 maybe_text = 0;
13206#endif
13207
13208 if (!maybe_text)
13209 as_warn (_(".end not in text section"));
13210
13211 if (!cur_proc_ptr)
13212 {
13213 as_warn (_(".end directive without a preceding .ent directive."));
13214 demand_empty_rest_of_line ();
13215 return;
13216 }
13217
13218 if (p != NULL)
13219 {
13220 assert (S_GET_NAME (p));
13221 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->isym)))
13222 as_warn (_(".end symbol does not match .ent symbol."));
ecb4347a
DJ
13223
13224 if (debug_type == DEBUG_STABS)
13225 stabs_generate_asm_endfunc (S_GET_NAME (p),
13226 S_GET_NAME (p));
252b5132
RH
13227 }
13228 else
13229 as_warn (_(".end directive missing or unknown symbol"));
13230
ecb4347a
DJ
13231#ifdef OBJ_ELF
13232 /* Generate a .pdr section. */
13233 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
13234 {
13235 segT saved_seg = now_seg;
13236 subsegT saved_subseg = now_subseg;
13237 valueT dot;
13238 expressionS exp;
13239 char *fragp;
252b5132 13240
ecb4347a 13241 dot = frag_now_fix ();
252b5132
RH
13242
13243#ifdef md_flush_pending_output
ecb4347a 13244 md_flush_pending_output ();
252b5132
RH
13245#endif
13246
ecb4347a
DJ
13247 assert (pdr_seg);
13248 subseg_set (pdr_seg, 0);
252b5132 13249
ecb4347a
DJ
13250 /* Write the symbol. */
13251 exp.X_op = O_symbol;
13252 exp.X_add_symbol = p;
13253 exp.X_add_number = 0;
13254 emit_expr (&exp, 4);
252b5132 13255
ecb4347a 13256 fragp = frag_more (7 * 4);
252b5132 13257
ecb4347a
DJ
13258 md_number_to_chars (fragp, (valueT) cur_proc_ptr->reg_mask, 4);
13259 md_number_to_chars (fragp + 4, (valueT) cur_proc_ptr->reg_offset, 4);
13260 md_number_to_chars (fragp + 8, (valueT) cur_proc_ptr->fpreg_mask, 4);
13261 md_number_to_chars (fragp + 12, (valueT) cur_proc_ptr->fpreg_offset, 4);
13262 md_number_to_chars (fragp + 16, (valueT) cur_proc_ptr->frame_offset, 4);
13263 md_number_to_chars (fragp + 20, (valueT) cur_proc_ptr->frame_reg, 4);
13264 md_number_to_chars (fragp + 24, (valueT) cur_proc_ptr->pc_reg, 4);
252b5132 13265
ecb4347a
DJ
13266 subseg_set (saved_seg, saved_subseg);
13267 }
13268#endif /* OBJ_ELF */
252b5132
RH
13269
13270 cur_proc_ptr = NULL;
13271}
13272
13273/* The .aent and .ent directives. */
13274
13275static void
13276s_mips_ent (aent)
13277 int aent;
13278{
252b5132
RH
13279 symbolS *symbolP;
13280 int maybe_text;
13281
13282 symbolP = get_symbol ();
13283 if (*input_line_pointer == ',')
f9419b05 13284 ++input_line_pointer;
252b5132 13285 SKIP_WHITESPACE ();
3882b010 13286 if (ISDIGIT (*input_line_pointer)
d9a62219 13287 || *input_line_pointer == '-')
874e8986 13288 get_number ();
252b5132
RH
13289
13290#ifdef BFD_ASSEMBLER
13291 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
13292 maybe_text = 1;
13293 else
13294 maybe_text = 0;
13295#else
13296 if (now_seg != data_section && now_seg != bss_section)
13297 maybe_text = 1;
13298 else
13299 maybe_text = 0;
13300#endif
13301
13302 if (!maybe_text)
13303 as_warn (_(".ent or .aent not in text section."));
13304
13305 if (!aent && cur_proc_ptr)
9a41af64 13306 as_warn (_("missing .end"));
252b5132
RH
13307
13308 if (!aent)
13309 {
7a621144
DJ
13310 /* This function needs its own .frame and .cprestore directives. */
13311 mips_frame_reg_valid = 0;
13312 mips_cprestore_valid = 0;
13313
252b5132
RH
13314 cur_proc_ptr = &cur_proc;
13315 memset (cur_proc_ptr, '\0', sizeof (procS));
13316
13317 cur_proc_ptr->isym = symbolP;
13318
49309057 13319 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
252b5132 13320
f9419b05 13321 ++numprocs;
ecb4347a
DJ
13322
13323 if (debug_type == DEBUG_STABS)
13324 stabs_generate_asm_func (S_GET_NAME (symbolP),
13325 S_GET_NAME (symbolP));
252b5132
RH
13326 }
13327
13328 demand_empty_rest_of_line ();
13329}
13330
13331/* The .frame directive. If the mdebug section is present (IRIX 5 native)
bdaaa2e1 13332 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
252b5132 13333 s_mips_frame is used so that we can set the PDR information correctly.
bdaaa2e1 13334 We can't use the ecoff routines because they make reference to the ecoff
252b5132
RH
13335 symbol table (in the mdebug section). */
13336
13337static void
13338s_mips_frame (ignore)
2b3c5a5d 13339 int ignore ATTRIBUTE_UNUSED;
252b5132 13340{
ecb4347a
DJ
13341#ifdef OBJ_ELF
13342 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
13343 {
13344 long val;
252b5132 13345
ecb4347a
DJ
13346 if (cur_proc_ptr == (procS *) NULL)
13347 {
13348 as_warn (_(".frame outside of .ent"));
13349 demand_empty_rest_of_line ();
13350 return;
13351 }
252b5132 13352
ecb4347a
DJ
13353 cur_proc_ptr->frame_reg = tc_get_register (1);
13354
13355 SKIP_WHITESPACE ();
13356 if (*input_line_pointer++ != ','
13357 || get_absolute_expression_and_terminator (&val) != ',')
13358 {
13359 as_warn (_("Bad .frame directive"));
13360 --input_line_pointer;
13361 demand_empty_rest_of_line ();
13362 return;
13363 }
252b5132 13364
ecb4347a
DJ
13365 cur_proc_ptr->frame_offset = val;
13366 cur_proc_ptr->pc_reg = tc_get_register (0);
252b5132 13367
252b5132 13368 demand_empty_rest_of_line ();
252b5132 13369 }
ecb4347a
DJ
13370 else
13371#endif /* OBJ_ELF */
13372 s_ignore (ignore);
252b5132
RH
13373}
13374
bdaaa2e1
KH
13375/* The .fmask and .mask directives. If the mdebug section is present
13376 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
252b5132 13377 embedded targets, s_mips_mask is used so that we can set the PDR
bdaaa2e1 13378 information correctly. We can't use the ecoff routines because they
252b5132
RH
13379 make reference to the ecoff symbol table (in the mdebug section). */
13380
13381static void
13382s_mips_mask (reg_type)
13383 char reg_type;
13384{
ecb4347a
DJ
13385#ifdef OBJ_ELF
13386 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
252b5132 13387 {
ecb4347a 13388 long mask, off;
252b5132 13389
ecb4347a
DJ
13390 if (cur_proc_ptr == (procS *) NULL)
13391 {
13392 as_warn (_(".mask/.fmask outside of .ent"));
13393 demand_empty_rest_of_line ();
13394 return;
13395 }
252b5132 13396
ecb4347a
DJ
13397 if (get_absolute_expression_and_terminator (&mask) != ',')
13398 {
13399 as_warn (_("Bad .mask/.fmask directive"));
13400 --input_line_pointer;
13401 demand_empty_rest_of_line ();
13402 return;
13403 }
252b5132 13404
ecb4347a
DJ
13405 off = get_absolute_expression ();
13406
13407 if (reg_type == 'F')
13408 {
13409 cur_proc_ptr->fpreg_mask = mask;
13410 cur_proc_ptr->fpreg_offset = off;
13411 }
13412 else
13413 {
13414 cur_proc_ptr->reg_mask = mask;
13415 cur_proc_ptr->reg_offset = off;
13416 }
13417
13418 demand_empty_rest_of_line ();
252b5132
RH
13419 }
13420 else
ecb4347a
DJ
13421#endif /* OBJ_ELF */
13422 s_ignore (reg_type);
252b5132
RH
13423}
13424
13425/* The .loc directive. */
13426
13427#if 0
13428static void
13429s_loc (x)
13430 int x;
13431{
13432 symbolS *symbolP;
13433 int lineno;
13434 int addroff;
13435
13436 assert (now_seg == text_section);
13437
13438 lineno = get_number ();
13439 addroff = frag_now_fix ();
13440
13441 symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
13442 S_SET_TYPE (symbolP, N_SLINE);
13443 S_SET_OTHER (symbolP, 0);
13444 S_SET_DESC (symbolP, lineno);
13445 symbolP->sy_segment = now_seg;
13446}
13447#endif
e7af610e 13448
316f5878
RS
13449/* A table describing all the processors gas knows about. Names are
13450 matched in the order listed.
e7af610e 13451
316f5878
RS
13452 To ease comparison, please keep this table in the same order as
13453 gcc's mips_cpu_info_table[]. */
e972090a
NC
13454static const struct mips_cpu_info mips_cpu_info_table[] =
13455{
316f5878
RS
13456 /* Entries for generic ISAs */
13457 { "mips1", 1, ISA_MIPS1, CPU_R3000 },
13458 { "mips2", 1, ISA_MIPS2, CPU_R6000 },
13459 { "mips3", 1, ISA_MIPS3, CPU_R4000 },
13460 { "mips4", 1, ISA_MIPS4, CPU_R8000 },
13461 { "mips5", 1, ISA_MIPS5, CPU_MIPS5 },
13462 { "mips32", 1, ISA_MIPS32, CPU_MIPS32 },
13463 { "mips64", 1, ISA_MIPS64, CPU_MIPS64 },
13464
13465 /* MIPS I */
13466 { "r3000", 0, ISA_MIPS1, CPU_R3000 },
13467 { "r2000", 0, ISA_MIPS1, CPU_R3000 },
13468 { "r3900", 0, ISA_MIPS1, CPU_R3900 },
13469
13470 /* MIPS II */
13471 { "r6000", 0, ISA_MIPS2, CPU_R6000 },
13472
13473 /* MIPS III */
13474 { "r4000", 0, ISA_MIPS3, CPU_R4000 },
13475 { "r4010", 0, ISA_MIPS2, CPU_R4010 },
13476 { "vr4100", 0, ISA_MIPS3, CPU_VR4100 },
13477 { "vr4111", 0, ISA_MIPS3, CPU_R4111 },
13478 { "vr4300", 0, ISA_MIPS3, CPU_R4300 },
13479 { "r4400", 0, ISA_MIPS3, CPU_R4400 },
13480 { "r4600", 0, ISA_MIPS3, CPU_R4600 },
13481 { "orion", 0, ISA_MIPS3, CPU_R4600 },
13482 { "r4650", 0, ISA_MIPS3, CPU_R4650 },
13483
13484 /* MIPS IV */
13485 { "r8000", 0, ISA_MIPS4, CPU_R8000 },
13486 { "r10000", 0, ISA_MIPS4, CPU_R10000 },
13487 { "r12000", 0, ISA_MIPS4, CPU_R12000 },
13488 { "vr5000", 0, ISA_MIPS4, CPU_R5000 },
13489 { "rm5200", 0, ISA_MIPS4, CPU_R5000 },
13490 { "rm5230", 0, ISA_MIPS4, CPU_R5000 },
13491 { "rm5231", 0, ISA_MIPS4, CPU_R5000 },
13492 { "rm5261", 0, ISA_MIPS4, CPU_R5000 },
13493 { "rm5721", 0, ISA_MIPS4, CPU_R5000 },
13494 { "r7000", 0, ISA_MIPS4, CPU_R5000 },
13495
13496 /* MIPS 32 */
13497 { "4kc", 0, ISA_MIPS32, CPU_MIPS32, },
13498 { "4km", 0, ISA_MIPS32, CPU_MIPS32 },
13499 { "4kp", 0, ISA_MIPS32, CPU_MIPS32 },
e7af610e 13500
316f5878
RS
13501 /* MIPS 64 */
13502 { "5kc", 0, ISA_MIPS64, CPU_MIPS64 },
13503 { "20kc", 0, ISA_MIPS64, CPU_MIPS64 },
e7af610e 13504
c7a23324 13505 /* Broadcom SB-1 CPU core */
316f5878 13506 { "sb1", 0, ISA_MIPS64, CPU_SB1 },
e7af610e 13507
316f5878
RS
13508 /* End marker */
13509 { NULL, 0, 0, 0 }
13510};
e7af610e 13511
84ea6cf2 13512
316f5878
RS
13513/* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
13514 with a final "000" replaced by "k". Ignore case.
e7af610e 13515
316f5878 13516 Note: this function is shared between GCC and GAS. */
c6c98b38 13517
316f5878
RS
13518static boolean
13519mips_strict_matching_cpu_name_p (canonical, given)
13520 const char *canonical, *given;
13521{
13522 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
13523 given++, canonical++;
13524
13525 return ((*given == 0 && *canonical == 0)
13526 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
13527}
13528
13529
13530/* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
13531 CPU name. We've traditionally allowed a lot of variation here.
13532
13533 Note: this function is shared between GCC and GAS. */
13534
13535static boolean
13536mips_matching_cpu_name_p (canonical, given)
13537 const char *canonical, *given;
13538{
13539 /* First see if the name matches exactly, or with a final "000"
13540 turned into "k". */
13541 if (mips_strict_matching_cpu_name_p (canonical, given))
13542 return true;
13543
13544 /* If not, try comparing based on numerical designation alone.
13545 See if GIVEN is an unadorned number, or 'r' followed by a number. */
13546 if (TOLOWER (*given) == 'r')
13547 given++;
13548 if (!ISDIGIT (*given))
13549 return false;
13550
13551 /* Skip over some well-known prefixes in the canonical name,
13552 hoping to find a number there too. */
13553 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
13554 canonical += 2;
13555 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
13556 canonical += 2;
13557 else if (TOLOWER (canonical[0]) == 'r')
13558 canonical += 1;
13559
13560 return mips_strict_matching_cpu_name_p (canonical, given);
13561}
13562
13563
13564/* Parse an option that takes the name of a processor as its argument.
13565 OPTION is the name of the option and CPU_STRING is the argument.
13566 Return the corresponding processor enumeration if the CPU_STRING is
13567 recognized, otherwise report an error and return null.
13568
13569 A similar function exists in GCC. */
e7af610e
NC
13570
13571static const struct mips_cpu_info *
316f5878
RS
13572mips_parse_cpu (option, cpu_string)
13573 const char *option, *cpu_string;
e7af610e 13574{
316f5878 13575 const struct mips_cpu_info *p;
e7af610e 13576
316f5878
RS
13577 /* 'from-abi' selects the most compatible architecture for the given
13578 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
13579 EABIs, we have to decide whether we're using the 32-bit or 64-bit
13580 version. Look first at the -mgp options, if given, otherwise base
13581 the choice on MIPS_DEFAULT_64BIT.
e7af610e 13582
316f5878
RS
13583 Treat NO_ABI like the EABIs. One reason to do this is that the
13584 plain 'mips' and 'mips64' configs have 'from-abi' as their default
13585 architecture. This code picks MIPS I for 'mips' and MIPS III for
13586 'mips64', just as we did in the days before 'from-abi'. */
13587 if (strcasecmp (cpu_string, "from-abi") == 0)
13588 {
13589 if (ABI_NEEDS_32BIT_REGS (mips_abi))
13590 return mips_cpu_info_from_isa (ISA_MIPS1);
13591
13592 if (ABI_NEEDS_64BIT_REGS (mips_abi))
13593 return mips_cpu_info_from_isa (ISA_MIPS3);
13594
13595 if (file_mips_gp32 >= 0)
13596 return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
13597
13598 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
13599 ? ISA_MIPS3
13600 : ISA_MIPS1);
13601 }
13602
13603 /* 'default' has traditionally been a no-op. Probably not very useful. */
13604 if (strcasecmp (cpu_string, "default") == 0)
13605 return 0;
13606
13607 for (p = mips_cpu_info_table; p->name != 0; p++)
13608 if (mips_matching_cpu_name_p (p->name, cpu_string))
13609 return p;
13610
13611 as_bad ("Bad value (%s) for %s", cpu_string, option);
13612 return 0;
e7af610e
NC
13613}
13614
316f5878
RS
13615/* Return the canonical processor information for ISA (a member of the
13616 ISA_MIPS* enumeration). */
13617
e7af610e
NC
13618static const struct mips_cpu_info *
13619mips_cpu_info_from_isa (isa)
13620 int isa;
13621{
13622 int i;
13623
13624 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13625 if (mips_cpu_info_table[i].is_isa
316f5878 13626 && isa == mips_cpu_info_table[i].isa)
e7af610e
NC
13627 return (&mips_cpu_info_table[i]);
13628
e972090a 13629 return NULL;
e7af610e 13630}
316f5878
RS
13631\f
13632static void
13633show (stream, string, col_p, first_p)
13634 FILE *stream;
13635 const char *string;
13636 int *col_p;
13637 int *first_p;
13638{
13639 if (*first_p)
13640 {
13641 fprintf (stream, "%24s", "");
13642 *col_p = 24;
13643 }
13644 else
13645 {
13646 fprintf (stream, ", ");
13647 *col_p += 2;
13648 }
e7af610e 13649
316f5878
RS
13650 if (*col_p + strlen (string) > 72)
13651 {
13652 fprintf (stream, "\n%24s", "");
13653 *col_p = 24;
13654 }
13655
13656 fprintf (stream, "%s", string);
13657 *col_p += strlen (string);
13658
13659 *first_p = 0;
13660}
13661
13662void
13663md_show_usage (stream)
13664 FILE *stream;
e7af610e 13665{
316f5878
RS
13666 int column, first;
13667 size_t i;
13668
13669 fprintf (stream, _("\
13670MIPS options:\n\
13671-membedded-pic generate embedded position independent code\n\
13672-EB generate big endian output\n\
13673-EL generate little endian output\n\
13674-g, -g2 do not remove unneeded NOPs or swap branches\n\
13675-G NUM allow referencing objects up to NUM bytes\n\
13676 implicitly with the gp register [default 8]\n"));
13677 fprintf (stream, _("\
13678-mips1 generate MIPS ISA I instructions\n\
13679-mips2 generate MIPS ISA II instructions\n\
13680-mips3 generate MIPS ISA III instructions\n\
13681-mips4 generate MIPS ISA IV instructions\n\
13682-mips5 generate MIPS ISA V instructions\n\
13683-mips32 generate MIPS32 ISA instructions\n\
13684-mips64 generate MIPS64 ISA instructions\n\
13685-march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
13686
13687 first = 1;
e7af610e
NC
13688
13689 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
316f5878
RS
13690 show (stream, mips_cpu_info_table[i].name, &column, &first);
13691 show (stream, "from-abi", &column, &first);
13692 fputc ('\n', stream);
e7af610e 13693
316f5878
RS
13694 fprintf (stream, _("\
13695-mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
13696-no-mCPU don't generate code specific to CPU.\n\
13697 For -mCPU and -no-mCPU, CPU must be one of:\n"));
13698
13699 first = 1;
13700
13701 show (stream, "3900", &column, &first);
13702 show (stream, "4010", &column, &first);
13703 show (stream, "4100", &column, &first);
13704 show (stream, "4650", &column, &first);
13705 fputc ('\n', stream);
13706
13707 fprintf (stream, _("\
13708-mips16 generate mips16 instructions\n\
13709-no-mips16 do not generate mips16 instructions\n"));
13710 fprintf (stream, _("\
13711-mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
13712-mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
13713-O0 remove unneeded NOPs, do not swap branches\n\
13714-O remove unneeded NOPs and swap branches\n\
13715-n warn about NOPs generated from macros\n\
13716--[no-]construct-floats [dis]allow floating point values to be constructed\n\
13717--trap, --no-break trap exception on div by 0 and mult overflow\n\
13718--break, --no-trap break exception on div by 0 and mult overflow\n"));
13719#ifdef OBJ_ELF
13720 fprintf (stream, _("\
13721-KPIC, -call_shared generate SVR4 position independent code\n\
13722-non_shared do not generate position independent code\n\
13723-xgot assume a 32 bit GOT\n\
13724-mabi=ABI create ABI conformant object file for:\n"));
13725
13726 first = 1;
13727
13728 show (stream, "32", &column, &first);
13729 show (stream, "o64", &column, &first);
13730 show (stream, "n32", &column, &first);
13731 show (stream, "64", &column, &first);
13732 show (stream, "eabi", &column, &first);
13733
13734 fputc ('\n', stream);
13735
13736 fprintf (stream, _("\
13737-32 create o32 ABI object file (default)\n\
13738-n32 create n32 ABI object file\n\
13739-64 create 64 ABI object file\n"));
13740#endif
e7af610e 13741}
This page took 0.995392 seconds and 4 git commands to generate.