]> Git Repo - binutils.git/blame - gas/config/tc-mips.c
* config/tc-mips.c (pdr_seg): Define only for ELF.
[binutils.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
a161fe53 325enum mips_pic_level mips_pic;
252b5132 326
39c0a331
L
327/* Warn about all NOPS that the assembler generates. */
328static int warn_nops = 0;
329
c9914766 330/* 1 if we should generate 32 bit offsets from the $gp register in
252b5132 331 SVR4_PIC mode. Currently has no meaning in other modes. */
c9914766 332static int mips_big_got = 0;
252b5132
RH
333
334/* 1 if trap instructions should used for overflow rather than break
335 instructions. */
c9914766 336static int mips_trap = 0;
252b5132 337
119d663a 338/* 1 if double width floating point constants should not be constructed
b6ff326e 339 by assembling two single width halves into two single width floating
119d663a
NC
340 point registers which just happen to alias the double width destination
341 register. On some architectures this aliasing can be disabled by a bit
d547a75e 342 in the status register, and the setting of this bit cannot be determined
119d663a
NC
343 automatically at assemble time. */
344static int mips_disable_float_construction;
345
252b5132
RH
346/* Non-zero if any .set noreorder directives were used. */
347
348static int mips_any_noreorder;
349
6b76fefe
CM
350/* Non-zero if nops should be inserted when the register referenced in
351 an mfhi/mflo instruction is read in the next two instructions. */
352static int mips_7000_hilo_fix;
353
252b5132 354/* The size of the small data section. */
156c2f8b 355static unsigned int g_switch_value = 8;
252b5132
RH
356/* Whether the -G option was used. */
357static int g_switch_seen = 0;
358
359#define N_RMASK 0xc4
360#define N_VFP 0xd4
361
362/* If we can determine in advance that GP optimization won't be
363 possible, we can skip the relaxation stuff that tries to produce
364 GP-relative references. This makes delay slot optimization work
365 better.
366
367 This function can only provide a guess, but it seems to work for
fba2b7f9
GK
368 gcc output. It needs to guess right for gcc, otherwise gcc
369 will put what it thinks is a GP-relative instruction in a branch
370 delay slot.
252b5132
RH
371
372 I don't know if a fix is needed for the SVR4_PIC mode. I've only
373 fixed it for the non-PIC mode. KR 95/04/07 */
374static int nopic_need_relax PARAMS ((symbolS *, int));
375
376/* handle of the OPCODE hash table */
377static struct hash_control *op_hash = NULL;
378
379/* The opcode hash table we use for the mips16. */
380static struct hash_control *mips16_op_hash = NULL;
381
382/* This array holds the chars that always start a comment. If the
383 pre-processor is disabled, these aren't very useful */
384const char comment_chars[] = "#";
385
386/* This array holds the chars that only start a comment at the beginning of
387 a line. If the line seems to have the form '# 123 filename'
388 .line and .file directives will appear in the pre-processed output */
389/* Note that input_file.c hand checks for '#' at the beginning of the
390 first line of the input file. This is because the compiler outputs
bdaaa2e1 391 #NO_APP at the beginning of its output. */
252b5132
RH
392/* Also note that C style comments are always supported. */
393const char line_comment_chars[] = "#";
394
bdaaa2e1 395/* This array holds machine specific line separator characters. */
63a0b638 396const char line_separator_chars[] = ";";
252b5132
RH
397
398/* Chars that can be used to separate mant from exp in floating point nums */
399const char EXP_CHARS[] = "eE";
400
401/* Chars that mean this number is a floating point constant */
402/* As in 0f12.456 */
403/* or 0d1.2345e12 */
404const char FLT_CHARS[] = "rRsSfFdDxXpP";
405
406/* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
407 changed in read.c . Ideally it shouldn't have to know about it at all,
408 but nothing is ideal around here.
409 */
410
411static char *insn_error;
412
413static int auto_align = 1;
414
415/* When outputting SVR4 PIC code, the assembler needs to know the
416 offset in the stack frame from which to restore the $gp register.
417 This is set by the .cprestore pseudo-op, and saved in this
418 variable. */
419static offsetT mips_cprestore_offset = -1;
420
6478892d
TS
421/* Similiar for NewABI PIC code, where $gp is callee-saved. NewABI has some
422 more optimizations, it can use a register value instead of a memory-saved
956cd1d6 423 offset and even an other register than $gp as global pointer. */
6478892d
TS
424static offsetT mips_cpreturn_offset = -1;
425static int mips_cpreturn_register = -1;
426static int mips_gp_register = GP;
def2e0dd 427static int mips_gprel_offset = 0;
6478892d 428
7a621144
DJ
429/* Whether mips_cprestore_offset has been set in the current function
430 (or whether it has already been warned about, if not). */
431static int mips_cprestore_valid = 0;
432
252b5132
RH
433/* This is the register which holds the stack frame, as set by the
434 .frame pseudo-op. This is needed to implement .cprestore. */
435static int mips_frame_reg = SP;
436
7a621144
DJ
437/* Whether mips_frame_reg has been set in the current function
438 (or whether it has already been warned about, if not). */
439static int mips_frame_reg_valid = 0;
440
252b5132
RH
441/* To output NOP instructions correctly, we need to keep information
442 about the previous two instructions. */
443
444/* Whether we are optimizing. The default value of 2 means to remove
445 unneeded NOPs and swap branch instructions when possible. A value
446 of 1 means to not swap branches. A value of 0 means to always
447 insert NOPs. */
448static int mips_optimize = 2;
449
450/* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
451 equivalent to seeing no -g option at all. */
452static int mips_debug = 0;
453
454/* The previous instruction. */
455static struct mips_cl_insn prev_insn;
456
457/* The instruction before prev_insn. */
458static struct mips_cl_insn prev_prev_insn;
459
460/* If we don't want information for prev_insn or prev_prev_insn, we
461 point the insn_mo field at this dummy integer. */
43841e91 462static const struct mips_opcode dummy_opcode = { NULL, NULL, 0, 0, 0, 0 };
252b5132
RH
463
464/* Non-zero if prev_insn is valid. */
465static int prev_insn_valid;
466
467/* The frag for the previous instruction. */
468static struct frag *prev_insn_frag;
469
470/* The offset into prev_insn_frag for the previous instruction. */
471static long prev_insn_where;
472
473/* The reloc type for the previous instruction, if any. */
f6688943 474static bfd_reloc_code_real_type prev_insn_reloc_type[3];
252b5132
RH
475
476/* The reloc for the previous instruction, if any. */
f6688943 477static fixS *prev_insn_fixp[3];
252b5132
RH
478
479/* Non-zero if the previous instruction was in a delay slot. */
480static int prev_insn_is_delay_slot;
481
482/* Non-zero if the previous instruction was in a .set noreorder. */
483static int prev_insn_unreordered;
484
485/* Non-zero if the previous instruction uses an extend opcode (if
486 mips16). */
487static int prev_insn_extended;
488
489/* Non-zero if the previous previous instruction was in a .set
490 noreorder. */
491static int prev_prev_insn_unreordered;
492
493/* If this is set, it points to a frag holding nop instructions which
494 were inserted before the start of a noreorder section. If those
495 nops turn out to be unnecessary, the size of the frag can be
496 decreased. */
497static fragS *prev_nop_frag;
498
499/* The number of nop instructions we created in prev_nop_frag. */
500static int prev_nop_frag_holds;
501
502/* The number of nop instructions that we know we need in
bdaaa2e1 503 prev_nop_frag. */
252b5132
RH
504static int prev_nop_frag_required;
505
506/* The number of instructions we've seen since prev_nop_frag. */
507static int prev_nop_frag_since;
508
509/* For ECOFF and ELF, relocations against symbols are done in two
510 parts, with a HI relocation and a LO relocation. Each relocation
511 has only 16 bits of space to store an addend. This means that in
512 order for the linker to handle carries correctly, it must be able
513 to locate both the HI and the LO relocation. This means that the
514 relocations must appear in order in the relocation table.
515
516 In order to implement this, we keep track of each unmatched HI
517 relocation. We then sort them so that they immediately precede the
bdaaa2e1 518 corresponding LO relocation. */
252b5132 519
e972090a
NC
520struct mips_hi_fixup
521{
252b5132
RH
522 /* Next HI fixup. */
523 struct mips_hi_fixup *next;
524 /* This fixup. */
525 fixS *fixp;
526 /* The section this fixup is in. */
527 segT seg;
528};
529
530/* The list of unmatched HI relocs. */
531
532static struct mips_hi_fixup *mips_hi_fixup_list;
533
534/* Map normal MIPS register numbers to mips16 register numbers. */
535
536#define X ILLEGAL_REG
e972090a
NC
537static const int mips32_to_16_reg_map[] =
538{
252b5132
RH
539 X, X, 2, 3, 4, 5, 6, 7,
540 X, X, X, X, X, X, X, X,
541 0, 1, X, X, X, X, X, X,
542 X, X, X, X, X, X, X, X
543};
544#undef X
545
546/* Map mips16 register numbers to normal MIPS register numbers. */
547
e972090a
NC
548static const unsigned int mips16_to_32_reg_map[] =
549{
252b5132
RH
550 16, 17, 2, 3, 4, 5, 6, 7
551};
552\f
553/* Since the MIPS does not have multiple forms of PC relative
554 instructions, we do not have to do relaxing as is done on other
555 platforms. However, we do have to handle GP relative addressing
556 correctly, which turns out to be a similar problem.
557
558 Every macro that refers to a symbol can occur in (at least) two
559 forms, one with GP relative addressing and one without. For
560 example, loading a global variable into a register generally uses
561 a macro instruction like this:
562 lw $4,i
563 If i can be addressed off the GP register (this is true if it is in
564 the .sbss or .sdata section, or if it is known to be smaller than
565 the -G argument) this will generate the following instruction:
566 lw $4,i($gp)
567 This instruction will use a GPREL reloc. If i can not be addressed
568 off the GP register, the following instruction sequence will be used:
569 lui $at,i
570 lw $4,i($at)
571 In this case the first instruction will have a HI16 reloc, and the
572 second reloc will have a LO16 reloc. Both relocs will be against
573 the symbol i.
574
575 The issue here is that we may not know whether i is GP addressable
576 until after we see the instruction that uses it. Therefore, we
577 want to be able to choose the final instruction sequence only at
578 the end of the assembly. This is similar to the way other
579 platforms choose the size of a PC relative instruction only at the
580 end of assembly.
581
582 When generating position independent code we do not use GP
583 addressing in quite the same way, but the issue still arises as
584 external symbols and local symbols must be handled differently.
585
586 We handle these issues by actually generating both possible
587 instruction sequences. The longer one is put in a frag_var with
588 type rs_machine_dependent. We encode what to do with the frag in
589 the subtype field. We encode (1) the number of existing bytes to
590 replace, (2) the number of new bytes to use, (3) the offset from
591 the start of the existing bytes to the first reloc we must generate
592 (that is, the offset is applied from the start of the existing
593 bytes after they are replaced by the new bytes, if any), (4) the
594 offset from the start of the existing bytes to the second reloc,
595 (5) whether a third reloc is needed (the third reloc is always four
596 bytes after the second reloc), and (6) whether to warn if this
597 variant is used (this is sometimes needed if .set nomacro or .set
598 noat is in effect). All these numbers are reasonably small.
599
600 Generating two instruction sequences must be handled carefully to
601 ensure that delay slots are handled correctly. Fortunately, there
602 are a limited number of cases. When the second instruction
603 sequence is generated, append_insn is directed to maintain the
604 existing delay slot information, so it continues to apply to any
605 code after the second instruction sequence. This means that the
606 second instruction sequence must not impose any requirements not
607 required by the first instruction sequence.
608
609 These variant frags are then handled in functions called by the
610 machine independent code. md_estimate_size_before_relax returns
611 the final size of the frag. md_convert_frag sets up the final form
612 of the frag. tc_gen_reloc adjust the first reloc and adds a second
613 one if needed. */
614#define RELAX_ENCODE(old, new, reloc1, reloc2, reloc3, warn) \
615 ((relax_substateT) \
616 (((old) << 23) \
617 | ((new) << 16) \
618 | (((reloc1) + 64) << 9) \
619 | (((reloc2) + 64) << 2) \
620 | ((reloc3) ? (1 << 1) : 0) \
621 | ((warn) ? 1 : 0)))
622#define RELAX_OLD(i) (((i) >> 23) & 0x7f)
623#define RELAX_NEW(i) (((i) >> 16) & 0x7f)
9a41af64
TS
624#define RELAX_RELOC1(i) ((valueT) (((i) >> 9) & 0x7f) - 64)
625#define RELAX_RELOC2(i) ((valueT) (((i) >> 2) & 0x7f) - 64)
252b5132
RH
626#define RELAX_RELOC3(i) (((i) >> 1) & 1)
627#define RELAX_WARN(i) ((i) & 1)
628
629/* For mips16 code, we use an entirely different form of relaxation.
630 mips16 supports two versions of most instructions which take
631 immediate values: a small one which takes some small value, and a
632 larger one which takes a 16 bit value. Since branches also follow
633 this pattern, relaxing these values is required.
634
635 We can assemble both mips16 and normal MIPS code in a single
636 object. Therefore, we need to support this type of relaxation at
637 the same time that we support the relaxation described above. We
638 use the high bit of the subtype field to distinguish these cases.
639
640 The information we store for this type of relaxation is the
641 argument code found in the opcode file for this relocation, whether
642 the user explicitly requested a small or extended form, and whether
643 the relocation is in a jump or jal delay slot. That tells us the
644 size of the value, and how it should be stored. We also store
645 whether the fragment is considered to be extended or not. We also
646 store whether this is known to be a branch to a different section,
647 whether we have tried to relax this frag yet, and whether we have
648 ever extended a PC relative fragment because of a shift count. */
649#define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
650 (0x80000000 \
651 | ((type) & 0xff) \
652 | ((small) ? 0x100 : 0) \
653 | ((ext) ? 0x200 : 0) \
654 | ((dslot) ? 0x400 : 0) \
655 | ((jal_dslot) ? 0x800 : 0))
656#define RELAX_MIPS16_P(i) (((i) & 0x80000000) != 0)
657#define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
658#define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
659#define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
660#define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
661#define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
662#define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
663#define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
664#define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
665#define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
666#define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
667#define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
668\f
669/* Prototypes for static functions. */
670
671#ifdef __STDC__
672#define internalError() \
673 as_fatal (_("internal Error, line %d, %s"), __LINE__, __FILE__)
674#else
675#define internalError() as_fatal (_("MIPS internal Error"));
676#endif
677
678enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
679
680static int insn_uses_reg PARAMS ((struct mips_cl_insn *ip,
681 unsigned int reg, enum mips_regclass class));
156c2f8b 682static int reg_needs_delay PARAMS ((unsigned int));
252b5132
RH
683static void mips16_mark_labels PARAMS ((void));
684static void append_insn PARAMS ((char *place,
685 struct mips_cl_insn * ip,
686 expressionS * p,
f6688943 687 bfd_reloc_code_real_type *r,
252b5132
RH
688 boolean));
689static void mips_no_prev_insn PARAMS ((int));
690static void mips_emit_delays PARAMS ((boolean));
691#ifdef USE_STDARG
692static void macro_build PARAMS ((char *place, int *counter, expressionS * ep,
693 const char *name, const char *fmt,
694 ...));
695#else
696static void macro_build ();
697#endif
698static void mips16_macro_build PARAMS ((char *, int *, expressionS *,
699 const char *, const char *,
700 va_list));
438c16b8 701static void macro_build_jalr PARAMS ((int, expressionS *));
252b5132
RH
702static void macro_build_lui PARAMS ((char *place, int *counter,
703 expressionS * ep, int regnum));
704static void set_at PARAMS ((int *counter, int reg, int unsignedp));
705static void check_absolute_expr PARAMS ((struct mips_cl_insn * ip,
706 expressionS *));
707static void load_register PARAMS ((int *, int, expressionS *, int));
c9914766 708static void load_address PARAMS ((int *, int, expressionS *, int *));
ea1fb5dc 709static void move_register PARAMS ((int *, int, int));
252b5132
RH
710static void macro PARAMS ((struct mips_cl_insn * ip));
711static void mips16_macro PARAMS ((struct mips_cl_insn * ip));
712#ifdef LOSING_COMPILER
713static void macro2 PARAMS ((struct mips_cl_insn * ip));
714#endif
715static void mips_ip PARAMS ((char *str, struct mips_cl_insn * ip));
716static void mips16_ip PARAMS ((char *str, struct mips_cl_insn * ip));
717static void mips16_immed PARAMS ((char *, unsigned int, int, offsetT, boolean,
718 boolean, boolean, unsigned long *,
719 boolean *, unsigned short *));
394f9b3a 720static int my_getPercentOp PARAMS ((char **, unsigned int *, int *));
ad8d3bb3
TS
721static int my_getSmallParser PARAMS ((char **, unsigned int *, int *));
722static int my_getSmallExpression PARAMS ((expressionS *, char *));
723static void my_getExpression PARAMS ((expressionS *, char *));
ae948b86 724#ifdef OBJ_ELF
e013f690 725static int support_64bit_objects PARAMS((void));
ae948b86 726#endif
316f5878 727static void mips_set_option_string PARAMS ((const char **, const char *));
252b5132
RH
728static symbolS *get_symbol PARAMS ((void));
729static void mips_align PARAMS ((int to, int fill, symbolS *label));
730static void s_align PARAMS ((int));
731static void s_change_sec PARAMS ((int));
cca86cc8 732static void s_change_section PARAMS ((int));
252b5132
RH
733static void s_cons PARAMS ((int));
734static void s_float_cons PARAMS ((int));
735static void s_mips_globl PARAMS ((int));
736static void s_option PARAMS ((int));
737static void s_mipsset PARAMS ((int));
738static void s_abicalls PARAMS ((int));
739static void s_cpload PARAMS ((int));
6478892d
TS
740static void s_cpsetup PARAMS ((int));
741static void s_cplocal PARAMS ((int));
252b5132 742static void s_cprestore PARAMS ((int));
6478892d
TS
743static void s_cpreturn PARAMS ((int));
744static void s_gpvalue PARAMS ((int));
252b5132
RH
745static void s_gpword PARAMS ((int));
746static void s_cpadd PARAMS ((int));
747static void s_insn PARAMS ((int));
748static void md_obj_begin PARAMS ((void));
749static void md_obj_end PARAMS ((void));
750static long get_number PARAMS ((void));
751static void s_mips_ent PARAMS ((int));
752static void s_mips_end PARAMS ((int));
753static void s_mips_frame PARAMS ((int));
754static void s_mips_mask PARAMS ((int));
755static void s_mips_stab PARAMS ((int));
756static void s_mips_weakext PARAMS ((int));
c5dd6aab
DJ
757static void s_mips_file PARAMS ((int));
758static void s_mips_loc PARAMS ((int));
252b5132 759static int mips16_extended_frag PARAMS ((fragS *, asection *, long));
252b5132 760static int validate_mips_insn PARAMS ((const struct mips_opcode *));
316f5878 761static void show PARAMS ((FILE *, const char *, int *, int *));
add55e1f
RS
762#ifdef OBJ_ELF
763static int mips_need_elf_addend_fixup PARAMS ((fixS *));
764#endif
e7af610e 765
ad8d3bb3 766/* Return values of my_getSmallExpression(). */
fb1b3232 767
ad8d3bb3 768enum small_ex_type
fb1b3232
TS
769{
770 S_EX_NONE = 0,
ad8d3bb3
TS
771 S_EX_REGISTER,
772
773 /* Direct relocation creation by %percent_op(). */
774 S_EX_HALF,
fb1b3232 775 S_EX_HI,
ad8d3bb3
TS
776 S_EX_LO,
777 S_EX_GP_REL,
778 S_EX_GOT,
779 S_EX_CALL16,
780 S_EX_GOT_DISP,
781 S_EX_GOT_PAGE,
782 S_EX_GOT_OFST,
783 S_EX_GOT_HI,
784 S_EX_GOT_LO,
785 S_EX_NEG,
fb1b3232
TS
786 S_EX_HIGHER,
787 S_EX_HIGHEST,
ad8d3bb3
TS
788 S_EX_CALL_HI,
789 S_EX_CALL_LO
fb1b3232
TS
790};
791
e7af610e
NC
792/* Table and functions used to map between CPU/ISA names, and
793 ISA levels, and CPU numbers. */
794
e972090a
NC
795struct mips_cpu_info
796{
e7af610e
NC
797 const char *name; /* CPU or ISA name. */
798 int is_isa; /* Is this an ISA? (If 0, a CPU.) */
799 int isa; /* ISA level. */
800 int cpu; /* CPU number (default CPU if ISA). */
801};
802
316f5878
RS
803static void mips_set_architecture PARAMS ((const struct mips_cpu_info *));
804static void mips_set_tune PARAMS ((const struct mips_cpu_info *));
805static boolean mips_strict_matching_cpu_name_p PARAMS ((const char *,
806 const char *));
807static boolean mips_matching_cpu_name_p PARAMS ((const char *, const char *));
808static const struct mips_cpu_info *mips_parse_cpu PARAMS ((const char *,
809 const char *));
e7af610e 810static const struct mips_cpu_info *mips_cpu_info_from_isa PARAMS ((int));
252b5132
RH
811\f
812/* Pseudo-op table.
813
814 The following pseudo-ops from the Kane and Heinrich MIPS book
815 should be defined here, but are currently unsupported: .alias,
816 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
817
818 The following pseudo-ops from the Kane and Heinrich MIPS book are
819 specific to the type of debugging information being generated, and
820 should be defined by the object format: .aent, .begin, .bend,
821 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
822 .vreg.
823
824 The following pseudo-ops from the Kane and Heinrich MIPS book are
825 not MIPS CPU specific, but are also not specific to the object file
826 format. This file is probably the best place to define them, but
827 they are not currently supported: .asm0, .endr, .lab, .repeat,
828 .struct. */
829
e972090a
NC
830static const pseudo_typeS mips_pseudo_table[] =
831{
beae10d5 832 /* MIPS specific pseudo-ops. */
252b5132
RH
833 {"option", s_option, 0},
834 {"set", s_mipsset, 0},
835 {"rdata", s_change_sec, 'r'},
836 {"sdata", s_change_sec, 's'},
837 {"livereg", s_ignore, 0},
838 {"abicalls", s_abicalls, 0},
839 {"cpload", s_cpload, 0},
6478892d
TS
840 {"cpsetup", s_cpsetup, 0},
841 {"cplocal", s_cplocal, 0},
252b5132 842 {"cprestore", s_cprestore, 0},
6478892d
TS
843 {"cpreturn", s_cpreturn, 0},
844 {"gpvalue", s_gpvalue, 0},
252b5132
RH
845 {"gpword", s_gpword, 0},
846 {"cpadd", s_cpadd, 0},
847 {"insn", s_insn, 0},
848
beae10d5 849 /* Relatively generic pseudo-ops that happen to be used on MIPS
252b5132
RH
850 chips. */
851 {"asciiz", stringer, 1},
852 {"bss", s_change_sec, 'b'},
853 {"err", s_err, 0},
854 {"half", s_cons, 1},
855 {"dword", s_cons, 3},
856 {"weakext", s_mips_weakext, 0},
857
beae10d5 858 /* These pseudo-ops are defined in read.c, but must be overridden
252b5132
RH
859 here for one reason or another. */
860 {"align", s_align, 0},
861 {"byte", s_cons, 0},
862 {"data", s_change_sec, 'd'},
863 {"double", s_float_cons, 'd'},
864 {"float", s_float_cons, 'f'},
865 {"globl", s_mips_globl, 0},
866 {"global", s_mips_globl, 0},
867 {"hword", s_cons, 1},
868 {"int", s_cons, 2},
869 {"long", s_cons, 2},
870 {"octa", s_cons, 4},
871 {"quad", s_cons, 3},
cca86cc8 872 {"section", s_change_section, 0},
252b5132
RH
873 {"short", s_cons, 1},
874 {"single", s_float_cons, 'f'},
875 {"stabn", s_mips_stab, 'n'},
876 {"text", s_change_sec, 't'},
877 {"word", s_cons, 2},
add56521 878
add56521 879 { "extern", ecoff_directive_extern, 0},
add56521 880
43841e91 881 { NULL, NULL, 0 },
252b5132
RH
882};
883
e972090a
NC
884static const pseudo_typeS mips_nonecoff_pseudo_table[] =
885{
beae10d5
KH
886 /* These pseudo-ops should be defined by the object file format.
887 However, a.out doesn't support them, so we have versions here. */
252b5132
RH
888 {"aent", s_mips_ent, 1},
889 {"bgnb", s_ignore, 0},
890 {"end", s_mips_end, 0},
891 {"endb", s_ignore, 0},
892 {"ent", s_mips_ent, 0},
c5dd6aab 893 {"file", s_mips_file, 0},
252b5132
RH
894 {"fmask", s_mips_mask, 'F'},
895 {"frame", s_mips_frame, 0},
c5dd6aab 896 {"loc", s_mips_loc, 0},
252b5132
RH
897 {"mask", s_mips_mask, 'R'},
898 {"verstamp", s_ignore, 0},
43841e91 899 { NULL, NULL, 0 },
252b5132
RH
900};
901
902extern void pop_insert PARAMS ((const pseudo_typeS *));
903
904void
905mips_pop_insert ()
906{
907 pop_insert (mips_pseudo_table);
908 if (! ECOFF_DEBUGGING)
909 pop_insert (mips_nonecoff_pseudo_table);
910}
911\f
912/* Symbols labelling the current insn. */
913
e972090a
NC
914struct insn_label_list
915{
252b5132
RH
916 struct insn_label_list *next;
917 symbolS *label;
918};
919
920static struct insn_label_list *insn_labels;
921static struct insn_label_list *free_insn_labels;
922
923static void mips_clear_insn_labels PARAMS ((void));
924
925static inline void
926mips_clear_insn_labels ()
927{
928 register struct insn_label_list **pl;
929
930 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
931 ;
932 *pl = insn_labels;
933 insn_labels = NULL;
934}
935\f
936static char *expr_end;
937
938/* Expressions which appear in instructions. These are set by
939 mips_ip. */
940
941static expressionS imm_expr;
942static expressionS offset_expr;
943
944/* Relocs associated with imm_expr and offset_expr. */
945
f6688943
TS
946static bfd_reloc_code_real_type imm_reloc[3]
947 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
948static bfd_reloc_code_real_type offset_reloc[3]
949 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132
RH
950
951/* This is set by mips_ip if imm_reloc is an unmatched HI16_S reloc. */
952
953static boolean imm_unmatched_hi;
954
955/* These are set by mips16_ip if an explicit extension is used. */
956
957static boolean mips16_small, mips16_ext;
958
7ed4a06a 959#ifdef OBJ_ELF
ecb4347a
DJ
960/* The pdr segment for per procedure frame/regmask info. Not used for
961 ECOFF debugging. */
252b5132
RH
962
963static segT pdr_seg;
7ed4a06a 964#endif
252b5132 965
e013f690
TS
966/* The default target format to use. */
967
968const char *
969mips_target_format ()
970{
971 switch (OUTPUT_FLAVOR)
972 {
973 case bfd_target_aout_flavour:
974 return target_big_endian ? "a.out-mips-big" : "a.out-mips-little";
975 case bfd_target_ecoff_flavour:
976 return target_big_endian ? "ecoff-bigmips" : ECOFF_LITTLE_FORMAT;
977 case bfd_target_coff_flavour:
978 return "pe-mips";
979 case bfd_target_elf_flavour:
980#ifdef TE_TMIPS
cfe86eaa 981 /* This is traditional mips. */
e013f690 982 return (target_big_endian
cfe86eaa
TS
983 ? (HAVE_64BIT_OBJECTS
984 ? "elf64-tradbigmips"
985 : (HAVE_NEWABI
986 ? "elf32-ntradbigmips" : "elf32-tradbigmips"))
987 : (HAVE_64BIT_OBJECTS
988 ? "elf64-tradlittlemips"
989 : (HAVE_NEWABI
990 ? "elf32-ntradlittlemips" : "elf32-tradlittlemips")));
e013f690
TS
991#else
992 return (target_big_endian
cfe86eaa
TS
993 ? (HAVE_64BIT_OBJECTS
994 ? "elf64-bigmips"
995 : (HAVE_NEWABI
996 ? "elf32-nbigmips" : "elf32-bigmips"))
997 : (HAVE_64BIT_OBJECTS
998 ? "elf64-littlemips"
999 : (HAVE_NEWABI
1000 ? "elf32-nlittlemips" : "elf32-littlemips")));
e013f690
TS
1001#endif
1002 default:
1003 abort ();
1004 return NULL;
1005 }
1006}
1007
156c2f8b
NC
1008/* This function is called once, at assembler startup time. It should
1009 set up all the tables, etc. that the MD part of the assembler will need. */
1010
252b5132
RH
1011void
1012md_begin ()
1013{
252b5132 1014 register const char *retval = NULL;
156c2f8b 1015 int i = 0;
252b5132 1016 int broken = 0;
1f25f5d3 1017
ec68c924 1018 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, mips_arch))
252b5132
RH
1019 as_warn (_("Could not set architecture and machine"));
1020
252b5132
RH
1021 op_hash = hash_new ();
1022
1023 for (i = 0; i < NUMOPCODES;)
1024 {
1025 const char *name = mips_opcodes[i].name;
1026
1027 retval = hash_insert (op_hash, name, (PTR) &mips_opcodes[i]);
1028 if (retval != NULL)
1029 {
1030 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1031 mips_opcodes[i].name, retval);
1032 /* Probably a memory allocation problem? Give up now. */
1033 as_fatal (_("Broken assembler. No assembly attempted."));
1034 }
1035 do
1036 {
1037 if (mips_opcodes[i].pinfo != INSN_MACRO)
1038 {
1039 if (!validate_mips_insn (&mips_opcodes[i]))
1040 broken = 1;
1041 }
1042 ++i;
1043 }
1044 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
1045 }
1046
1047 mips16_op_hash = hash_new ();
1048
1049 i = 0;
1050 while (i < bfd_mips16_num_opcodes)
1051 {
1052 const char *name = mips16_opcodes[i].name;
1053
1054 retval = hash_insert (mips16_op_hash, name, (PTR) &mips16_opcodes[i]);
1055 if (retval != NULL)
1056 as_fatal (_("internal: can't hash `%s': %s"),
1057 mips16_opcodes[i].name, retval);
1058 do
1059 {
1060 if (mips16_opcodes[i].pinfo != INSN_MACRO
1061 && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
1062 != mips16_opcodes[i].match))
1063 {
1064 fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
1065 mips16_opcodes[i].name, mips16_opcodes[i].args);
1066 broken = 1;
1067 }
1068 ++i;
1069 }
1070 while (i < bfd_mips16_num_opcodes
1071 && strcmp (mips16_opcodes[i].name, name) == 0);
1072 }
1073
1074 if (broken)
1075 as_fatal (_("Broken assembler. No assembly attempted."));
1076
1077 /* We add all the general register names to the symbol table. This
1078 helps us detect invalid uses of them. */
1079 for (i = 0; i < 32; i++)
1080 {
1081 char buf[5];
1082
1083 sprintf (buf, "$%d", i);
1084 symbol_table_insert (symbol_new (buf, reg_section, i,
1085 &zero_address_frag));
1086 }
76db943d
TS
1087 symbol_table_insert (symbol_new ("$ra", reg_section, RA,
1088 &zero_address_frag));
252b5132
RH
1089 symbol_table_insert (symbol_new ("$fp", reg_section, FP,
1090 &zero_address_frag));
1091 symbol_table_insert (symbol_new ("$sp", reg_section, SP,
1092 &zero_address_frag));
1093 symbol_table_insert (symbol_new ("$gp", reg_section, GP,
1094 &zero_address_frag));
1095 symbol_table_insert (symbol_new ("$at", reg_section, AT,
1096 &zero_address_frag));
1097 symbol_table_insert (symbol_new ("$kt0", reg_section, KT0,
1098 &zero_address_frag));
1099 symbol_table_insert (symbol_new ("$kt1", reg_section, KT1,
1100 &zero_address_frag));
85b51719
TS
1101 symbol_table_insert (symbol_new ("$zero", reg_section, ZERO,
1102 &zero_address_frag));
252b5132
RH
1103 symbol_table_insert (symbol_new ("$pc", reg_section, -1,
1104 &zero_address_frag));
1105
1106 mips_no_prev_insn (false);
1107
1108 mips_gprmask = 0;
1109 mips_cprmask[0] = 0;
1110 mips_cprmask[1] = 0;
1111 mips_cprmask[2] = 0;
1112 mips_cprmask[3] = 0;
1113
1114 /* set the default alignment for the text section (2**2) */
1115 record_alignment (text_section, 2);
1116
1117 if (USE_GLOBAL_POINTER_OPT)
1118 bfd_set_gp_size (stdoutput, g_switch_value);
1119
1120 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1121 {
1122 /* On a native system, sections must be aligned to 16 byte
1123 boundaries. When configured for an embedded ELF target, we
1124 don't bother. */
1125 if (strcmp (TARGET_OS, "elf") != 0)
1126 {
1127 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
1128 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
1129 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
1130 }
1131
1132 /* Create a .reginfo section for register masks and a .mdebug
1133 section for debugging information. */
1134 {
1135 segT seg;
1136 subsegT subseg;
1137 flagword flags;
1138 segT sec;
1139
1140 seg = now_seg;
1141 subseg = now_subseg;
1142
1143 /* The ABI says this section should be loaded so that the
1144 running program can access it. However, we don't load it
1145 if we are configured for an embedded target */
1146 flags = SEC_READONLY | SEC_DATA;
1147 if (strcmp (TARGET_OS, "elf") != 0)
1148 flags |= SEC_ALLOC | SEC_LOAD;
1149
316f5878 1150 if (mips_abi != N64_ABI)
252b5132
RH
1151 {
1152 sec = subseg_new (".reginfo", (subsegT) 0);
1153
195325d2
TS
1154 bfd_set_section_flags (stdoutput, sec, flags);
1155 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
bdaaa2e1 1156
252b5132
RH
1157#ifdef OBJ_ELF
1158 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
1159#endif
1160 }
1161 else
1162 {
1163 /* The 64-bit ABI uses a .MIPS.options section rather than
1164 .reginfo section. */
1165 sec = subseg_new (".MIPS.options", (subsegT) 0);
195325d2
TS
1166 bfd_set_section_flags (stdoutput, sec, flags);
1167 bfd_set_section_alignment (stdoutput, sec, 3);
252b5132
RH
1168
1169#ifdef OBJ_ELF
1170 /* Set up the option header. */
1171 {
1172 Elf_Internal_Options opthdr;
1173 char *f;
1174
1175 opthdr.kind = ODK_REGINFO;
1176 opthdr.size = (sizeof (Elf_External_Options)
1177 + sizeof (Elf64_External_RegInfo));
1178 opthdr.section = 0;
1179 opthdr.info = 0;
1180 f = frag_more (sizeof (Elf_External_Options));
1181 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
1182 (Elf_External_Options *) f);
1183
1184 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
1185 }
1186#endif
1187 }
1188
1189 if (ECOFF_DEBUGGING)
1190 {
1191 sec = subseg_new (".mdebug", (subsegT) 0);
1192 (void) bfd_set_section_flags (stdoutput, sec,
1193 SEC_HAS_CONTENTS | SEC_READONLY);
1194 (void) bfd_set_section_alignment (stdoutput, sec, 2);
1195 }
ecb4347a
DJ
1196#ifdef OBJ_ELF
1197 else if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1198 {
1199 pdr_seg = subseg_new (".pdr", (subsegT) 0);
1200 (void) bfd_set_section_flags (stdoutput, pdr_seg,
1201 SEC_READONLY | SEC_RELOC
1202 | SEC_DEBUGGING);
1203 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
1204 }
252b5132
RH
1205#endif
1206
1207 subseg_set (seg, subseg);
1208 }
1209 }
1210
1211 if (! ECOFF_DEBUGGING)
1212 md_obj_begin ();
1213}
1214
1215void
1216md_mips_end ()
1217{
1218 if (! ECOFF_DEBUGGING)
1219 md_obj_end ();
1220}
1221
1222void
1223md_assemble (str)
1224 char *str;
1225{
1226 struct mips_cl_insn insn;
f6688943
TS
1227 bfd_reloc_code_real_type unused_reloc[3]
1228 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132
RH
1229
1230 imm_expr.X_op = O_absent;
252b5132
RH
1231 imm_unmatched_hi = false;
1232 offset_expr.X_op = O_absent;
f6688943
TS
1233 imm_reloc[0] = BFD_RELOC_UNUSED;
1234 imm_reloc[1] = BFD_RELOC_UNUSED;
1235 imm_reloc[2] = BFD_RELOC_UNUSED;
1236 offset_reloc[0] = BFD_RELOC_UNUSED;
1237 offset_reloc[1] = BFD_RELOC_UNUSED;
1238 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
1239
1240 if (mips_opts.mips16)
1241 mips16_ip (str, &insn);
1242 else
1243 {
1244 mips_ip (str, &insn);
beae10d5
KH
1245 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
1246 str, insn.insn_opcode));
252b5132
RH
1247 }
1248
1249 if (insn_error)
1250 {
1251 as_bad ("%s `%s'", insn_error, str);
1252 return;
1253 }
1254
1255 if (insn.insn_mo->pinfo == INSN_MACRO)
1256 {
1257 if (mips_opts.mips16)
1258 mips16_macro (&insn);
1259 else
1260 macro (&insn);
1261 }
1262 else
1263 {
1264 if (imm_expr.X_op != O_absent)
c4e7957c 1265 append_insn (NULL, &insn, &imm_expr, imm_reloc, imm_unmatched_hi);
252b5132 1266 else if (offset_expr.X_op != O_absent)
c4e7957c 1267 append_insn (NULL, &insn, &offset_expr, offset_reloc, false);
252b5132 1268 else
c4e7957c 1269 append_insn (NULL, &insn, NULL, unused_reloc, false);
252b5132
RH
1270 }
1271}
1272
1273/* See whether instruction IP reads register REG. CLASS is the type
1274 of register. */
1275
1276static int
1277insn_uses_reg (ip, reg, class)
1278 struct mips_cl_insn *ip;
1279 unsigned int reg;
1280 enum mips_regclass class;
1281{
1282 if (class == MIPS16_REG)
1283 {
1284 assert (mips_opts.mips16);
1285 reg = mips16_to_32_reg_map[reg];
1286 class = MIPS_GR_REG;
1287 }
1288
85b51719
TS
1289 /* Don't report on general register ZERO, since it never changes. */
1290 if (class == MIPS_GR_REG && reg == ZERO)
252b5132
RH
1291 return 0;
1292
1293 if (class == MIPS_FP_REG)
1294 {
1295 assert (! mips_opts.mips16);
1296 /* If we are called with either $f0 or $f1, we must check $f0.
1297 This is not optimal, because it will introduce an unnecessary
1298 NOP between "lwc1 $f0" and "swc1 $f1". To fix this we would
1299 need to distinguish reading both $f0 and $f1 or just one of
1300 them. Note that we don't have to check the other way,
1301 because there is no instruction that sets both $f0 and $f1
1302 and requires a delay. */
1303 if ((ip->insn_mo->pinfo & INSN_READ_FPR_S)
1304 && ((((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS) &~(unsigned)1)
1305 == (reg &~ (unsigned) 1)))
1306 return 1;
1307 if ((ip->insn_mo->pinfo & INSN_READ_FPR_T)
1308 && ((((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT) &~(unsigned)1)
1309 == (reg &~ (unsigned) 1)))
1310 return 1;
1311 }
1312 else if (! mips_opts.mips16)
1313 {
1314 if ((ip->insn_mo->pinfo & INSN_READ_GPR_S)
1315 && ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == reg)
1316 return 1;
1317 if ((ip->insn_mo->pinfo & INSN_READ_GPR_T)
1318 && ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT) == reg)
1319 return 1;
1320 }
1321 else
1322 {
1323 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_X)
1324 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RX)
1325 & MIPS16OP_MASK_RX)]
1326 == reg))
1327 return 1;
1328 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Y)
1329 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_RY)
1330 & MIPS16OP_MASK_RY)]
1331 == reg))
1332 return 1;
1333 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_Z)
1334 && (mips16_to_32_reg_map[((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
1335 & MIPS16OP_MASK_MOVE32Z)]
1336 == reg))
1337 return 1;
1338 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_T) && reg == TREG)
1339 return 1;
1340 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_SP) && reg == SP)
1341 return 1;
1342 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_31) && reg == RA)
1343 return 1;
1344 if ((ip->insn_mo->pinfo & MIPS16_INSN_READ_GPR_X)
1345 && ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
1346 & MIPS16OP_MASK_REGR32) == reg)
1347 return 1;
1348 }
1349
1350 return 0;
1351}
1352
1353/* This function returns true if modifying a register requires a
1354 delay. */
1355
1356static int
1357reg_needs_delay (reg)
156c2f8b 1358 unsigned int reg;
252b5132
RH
1359{
1360 unsigned long prev_pinfo;
1361
1362 prev_pinfo = prev_insn.insn_mo->pinfo;
1363 if (! mips_opts.noreorder
9ce8a5dd 1364 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
1365 && ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1366 || (! gpr_interlocks
1367 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1368 {
1369 /* A load from a coprocessor or from memory. All load
1370 delays delay the use of general register rt for one
1371 instruction on the r3000. The r6000 and r4000 use
1372 interlocks. */
bdaaa2e1 1373 /* Itbl support may require additional care here. */
252b5132
RH
1374 know (prev_pinfo & INSN_WRITE_GPR_T);
1375 if (reg == ((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT))
1376 return 1;
1377 }
1378
1379 return 0;
1380}
1381
1382/* Mark instruction labels in mips16 mode. This permits the linker to
1383 handle them specially, such as generating jalx instructions when
1384 needed. We also make them odd for the duration of the assembly, in
1385 order to generate the right sort of code. We will make them even
1386 in the adjust_symtab routine, while leaving them marked. This is
1387 convenient for the debugger and the disassembler. The linker knows
1388 to make them odd again. */
1389
1390static void
1391mips16_mark_labels ()
1392{
1393 if (mips_opts.mips16)
1394 {
1395 struct insn_label_list *l;
98aa84af 1396 valueT val;
252b5132
RH
1397
1398 for (l = insn_labels; l != NULL; l = l->next)
1399 {
1400#ifdef OBJ_ELF
1401 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
1402 S_SET_OTHER (l->label, STO_MIPS16);
1403#endif
98aa84af
AM
1404 val = S_GET_VALUE (l->label);
1405 if ((val & 1) == 0)
1406 S_SET_VALUE (l->label, val + 1);
252b5132
RH
1407 }
1408 }
1409}
1410
1411/* Output an instruction. PLACE is where to put the instruction; if
1412 it is NULL, this uses frag_more to get room. IP is the instruction
1413 information. ADDRESS_EXPR is an operand of the instruction to be
1414 used with RELOC_TYPE. */
1415
1416static void
1417append_insn (place, ip, address_expr, reloc_type, unmatched_hi)
1418 char *place;
1419 struct mips_cl_insn *ip;
1420 expressionS *address_expr;
f6688943 1421 bfd_reloc_code_real_type *reloc_type;
252b5132
RH
1422 boolean unmatched_hi;
1423{
1424 register unsigned long prev_pinfo, pinfo;
1425 char *f;
f6688943 1426 fixS *fixp[3];
252b5132
RH
1427 int nops = 0;
1428
1429 /* Mark instruction labels in mips16 mode. */
f9419b05 1430 mips16_mark_labels ();
252b5132
RH
1431
1432 prev_pinfo = prev_insn.insn_mo->pinfo;
1433 pinfo = ip->insn_mo->pinfo;
1434
1435 if (place == NULL && (! mips_opts.noreorder || prev_nop_frag != NULL))
1436 {
1437 int prev_prev_nop;
1438
1439 /* If the previous insn required any delay slots, see if we need
1440 to insert a NOP or two. There are eight kinds of possible
1441 hazards, of which an instruction can have at most one type.
1442 (1) a load from memory delay
1443 (2) a load from a coprocessor delay
1444 (3) an unconditional branch delay
1445 (4) a conditional branch delay
1446 (5) a move to coprocessor register delay
1447 (6) a load coprocessor register from memory delay
1448 (7) a coprocessor condition code delay
1449 (8) a HI/LO special register delay
1450
1451 There are a lot of optimizations we could do that we don't.
1452 In particular, we do not, in general, reorder instructions.
1453 If you use gcc with optimization, it will reorder
1454 instructions and generally do much more optimization then we
1455 do here; repeating all that work in the assembler would only
1456 benefit hand written assembly code, and does not seem worth
1457 it. */
1458
1459 /* This is how a NOP is emitted. */
1460#define emit_nop() \
1461 (mips_opts.mips16 \
1462 ? md_number_to_chars (frag_more (2), 0x6500, 2) \
1463 : md_number_to_chars (frag_more (4), 0, 4))
1464
1465 /* The previous insn might require a delay slot, depending upon
1466 the contents of the current insn. */
1467 if (! mips_opts.mips16
9ce8a5dd 1468 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
1469 && (((prev_pinfo & INSN_LOAD_COPROC_DELAY)
1470 && ! cop_interlocks)
1471 || (! gpr_interlocks
1472 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))))
1473 {
1474 /* A load from a coprocessor or from memory. All load
1475 delays delay the use of general register rt for one
1476 instruction on the r3000. The r6000 and r4000 use
1477 interlocks. */
beae10d5 1478 /* Itbl support may require additional care here. */
252b5132
RH
1479 know (prev_pinfo & INSN_WRITE_GPR_T);
1480 if (mips_optimize == 0
1481 || insn_uses_reg (ip,
1482 ((prev_insn.insn_opcode >> OP_SH_RT)
1483 & OP_MASK_RT),
1484 MIPS_GR_REG))
1485 ++nops;
1486 }
1487 else if (! mips_opts.mips16
9ce8a5dd 1488 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132 1489 && (((prev_pinfo & INSN_COPROC_MOVE_DELAY)
beae10d5 1490 && ! cop_interlocks)
e7af610e 1491 || (mips_opts.isa == ISA_MIPS1
252b5132
RH
1492 && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))))
1493 {
1494 /* A generic coprocessor delay. The previous instruction
1495 modified a coprocessor general or control register. If
1496 it modified a control register, we need to avoid any
1497 coprocessor instruction (this is probably not always
1498 required, but it sometimes is). If it modified a general
1499 register, we avoid using that register.
1500
1501 On the r6000 and r4000 loading a coprocessor register
1502 from memory is interlocked, and does not require a delay.
1503
1504 This case is not handled very well. There is no special
1505 knowledge of CP0 handling, and the coprocessors other
1506 than the floating point unit are not distinguished at
1507 all. */
1508 /* Itbl support may require additional care here. FIXME!
bdaaa2e1 1509 Need to modify this to include knowledge about
252b5132
RH
1510 user specified delays! */
1511 if (prev_pinfo & INSN_WRITE_FPR_T)
1512 {
1513 if (mips_optimize == 0
1514 || insn_uses_reg (ip,
1515 ((prev_insn.insn_opcode >> OP_SH_FT)
1516 & OP_MASK_FT),
1517 MIPS_FP_REG))
1518 ++nops;
1519 }
1520 else if (prev_pinfo & INSN_WRITE_FPR_S)
1521 {
1522 if (mips_optimize == 0
1523 || insn_uses_reg (ip,
1524 ((prev_insn.insn_opcode >> OP_SH_FS)
1525 & OP_MASK_FS),
1526 MIPS_FP_REG))
1527 ++nops;
1528 }
1529 else
1530 {
1531 /* We don't know exactly what the previous instruction
1532 does. If the current instruction uses a coprocessor
1533 register, we must insert a NOP. If previous
1534 instruction may set the condition codes, and the
1535 current instruction uses them, we must insert two
1536 NOPS. */
bdaaa2e1 1537 /* Itbl support may require additional care here. */
252b5132
RH
1538 if (mips_optimize == 0
1539 || ((prev_pinfo & INSN_WRITE_COND_CODE)
1540 && (pinfo & INSN_READ_COND_CODE)))
1541 nops += 2;
1542 else if (pinfo & INSN_COP)
1543 ++nops;
1544 }
1545 }
1546 else if (! mips_opts.mips16
9ce8a5dd 1547 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
1548 && (prev_pinfo & INSN_WRITE_COND_CODE)
1549 && ! cop_interlocks)
1550 {
1551 /* The previous instruction sets the coprocessor condition
1552 codes, but does not require a general coprocessor delay
1553 (this means it is a floating point comparison
1554 instruction). If this instruction uses the condition
1555 codes, we need to insert a single NOP. */
beae10d5 1556 /* Itbl support may require additional care here. */
252b5132
RH
1557 if (mips_optimize == 0
1558 || (pinfo & INSN_READ_COND_CODE))
1559 ++nops;
1560 }
6b76fefe
CM
1561
1562 /* If we're fixing up mfhi/mflo for the r7000 and the
1563 previous insn was an mfhi/mflo and the current insn
1564 reads the register that the mfhi/mflo wrote to, then
1565 insert two nops. */
1566
1567 else if (mips_7000_hilo_fix
1568 && MF_HILO_INSN (prev_pinfo)
1569 && insn_uses_reg (ip, ((prev_insn.insn_opcode >> OP_SH_RD)
beae10d5
KH
1570 & OP_MASK_RD),
1571 MIPS_GR_REG))
6b76fefe
CM
1572 {
1573 nops += 2;
1574 }
1575
1576 /* If we're fixing up mfhi/mflo for the r7000 and the
1577 2nd previous insn was an mfhi/mflo and the current insn
1578 reads the register that the mfhi/mflo wrote to, then
1579 insert one nop. */
1580
1581 else if (mips_7000_hilo_fix
1582 && MF_HILO_INSN (prev_prev_insn.insn_opcode)
1583 && insn_uses_reg (ip, ((prev_prev_insn.insn_opcode >> OP_SH_RD)
1584 & OP_MASK_RD),
1585 MIPS_GR_REG))
bdaaa2e1 1586
6b76fefe 1587 {
f9419b05 1588 ++nops;
6b76fefe 1589 }
bdaaa2e1 1590
252b5132
RH
1591 else if (prev_pinfo & INSN_READ_LO)
1592 {
1593 /* The previous instruction reads the LO register; if the
1594 current instruction writes to the LO register, we must
bdaaa2e1
KH
1595 insert two NOPS. Some newer processors have interlocks.
1596 Also the tx39's multiply instructions can be exectuted
252b5132 1597 immediatly after a read from HI/LO (without the delay),
bdaaa2e1
KH
1598 though the tx39's divide insns still do require the
1599 delay. */
252b5132 1600 if (! (hilo_interlocks
ec68c924 1601 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
252b5132
RH
1602 && (mips_optimize == 0
1603 || (pinfo & INSN_WRITE_LO)))
1604 nops += 2;
1605 /* Most mips16 branch insns don't have a delay slot.
1606 If a read from LO is immediately followed by a branch
1607 to a write to LO we have a read followed by a write
1608 less than 2 insns away. We assume the target of
1609 a branch might be a write to LO, and insert a nop
bdaaa2e1 1610 between a read and an immediately following branch. */
252b5132
RH
1611 else if (mips_opts.mips16
1612 && (mips_optimize == 0
1613 || (pinfo & MIPS16_INSN_BRANCH)))
f9419b05 1614 ++nops;
252b5132
RH
1615 }
1616 else if (prev_insn.insn_mo->pinfo & INSN_READ_HI)
1617 {
1618 /* The previous instruction reads the HI register; if the
1619 current instruction writes to the HI register, we must
1620 insert a NOP. Some newer processors have interlocks.
bdaaa2e1 1621 Also the note tx39's multiply above. */
252b5132 1622 if (! (hilo_interlocks
ec68c924 1623 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
252b5132
RH
1624 && (mips_optimize == 0
1625 || (pinfo & INSN_WRITE_HI)))
1626 nops += 2;
1627 /* Most mips16 branch insns don't have a delay slot.
1628 If a read from HI is immediately followed by a branch
1629 to a write to HI we have a read followed by a write
1630 less than 2 insns away. We assume the target of
1631 a branch might be a write to HI, and insert a nop
bdaaa2e1 1632 between a read and an immediately following branch. */
252b5132
RH
1633 else if (mips_opts.mips16
1634 && (mips_optimize == 0
1635 || (pinfo & MIPS16_INSN_BRANCH)))
f9419b05 1636 ++nops;
252b5132
RH
1637 }
1638
1639 /* If the previous instruction was in a noreorder section, then
1640 we don't want to insert the nop after all. */
bdaaa2e1 1641 /* Itbl support may require additional care here. */
252b5132
RH
1642 if (prev_insn_unreordered)
1643 nops = 0;
1644
1645 /* There are two cases which require two intervening
1646 instructions: 1) setting the condition codes using a move to
1647 coprocessor instruction which requires a general coprocessor
1648 delay and then reading the condition codes 2) reading the HI
1649 or LO register and then writing to it (except on processors
1650 which have interlocks). If we are not already emitting a NOP
1651 instruction, we must check for these cases compared to the
1652 instruction previous to the previous instruction. */
1653 if ((! mips_opts.mips16
9ce8a5dd 1654 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
1655 && (prev_prev_insn.insn_mo->pinfo & INSN_COPROC_MOVE_DELAY)
1656 && (prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE)
1657 && (pinfo & INSN_READ_COND_CODE)
1658 && ! cop_interlocks)
1659 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_LO)
1660 && (pinfo & INSN_WRITE_LO)
1661 && ! (hilo_interlocks
ec68c924 1662 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT))))
252b5132
RH
1663 || ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
1664 && (pinfo & INSN_WRITE_HI)
1665 && ! (hilo_interlocks
ec68c924 1666 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))))
252b5132
RH
1667 prev_prev_nop = 1;
1668 else
1669 prev_prev_nop = 0;
1670
1671 if (prev_prev_insn_unreordered)
1672 prev_prev_nop = 0;
1673
1674 if (prev_prev_nop && nops == 0)
1675 ++nops;
1676
1677 /* If we are being given a nop instruction, don't bother with
1678 one of the nops we would otherwise output. This will only
1679 happen when a nop instruction is used with mips_optimize set
1680 to 0. */
1681 if (nops > 0
1682 && ! mips_opts.noreorder
156c2f8b 1683 && ip->insn_opcode == (unsigned) (mips_opts.mips16 ? 0x6500 : 0))
252b5132
RH
1684 --nops;
1685
1686 /* Now emit the right number of NOP instructions. */
1687 if (nops > 0 && ! mips_opts.noreorder)
1688 {
1689 fragS *old_frag;
1690 unsigned long old_frag_offset;
1691 int i;
1692 struct insn_label_list *l;
1693
1694 old_frag = frag_now;
1695 old_frag_offset = frag_now_fix ();
1696
1697 for (i = 0; i < nops; i++)
1698 emit_nop ();
1699
1700 if (listing)
1701 {
1702 listing_prev_line ();
1703 /* We may be at the start of a variant frag. In case we
1704 are, make sure there is enough space for the frag
1705 after the frags created by listing_prev_line. The
1706 argument to frag_grow here must be at least as large
1707 as the argument to all other calls to frag_grow in
1708 this file. We don't have to worry about being in the
1709 middle of a variant frag, because the variants insert
1710 all needed nop instructions themselves. */
1711 frag_grow (40);
1712 }
1713
1714 for (l = insn_labels; l != NULL; l = l->next)
1715 {
98aa84af
AM
1716 valueT val;
1717
252b5132 1718 assert (S_GET_SEGMENT (l->label) == now_seg);
49309057 1719 symbol_set_frag (l->label, frag_now);
98aa84af 1720 val = (valueT) frag_now_fix ();
252b5132
RH
1721 /* mips16 text labels are stored as odd. */
1722 if (mips_opts.mips16)
f9419b05 1723 ++val;
98aa84af 1724 S_SET_VALUE (l->label, val);
252b5132
RH
1725 }
1726
1727#ifndef NO_ECOFF_DEBUGGING
1728 if (ECOFF_DEBUGGING)
1729 ecoff_fix_loc (old_frag, old_frag_offset);
1730#endif
1731 }
1732 else if (prev_nop_frag != NULL)
1733 {
1734 /* We have a frag holding nops we may be able to remove. If
1735 we don't need any nops, we can decrease the size of
1736 prev_nop_frag by the size of one instruction. If we do
bdaaa2e1 1737 need some nops, we count them in prev_nops_required. */
252b5132
RH
1738 if (prev_nop_frag_since == 0)
1739 {
1740 if (nops == 0)
1741 {
1742 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1743 --prev_nop_frag_holds;
1744 }
1745 else
1746 prev_nop_frag_required += nops;
1747 }
1748 else
1749 {
1750 if (prev_prev_nop == 0)
1751 {
1752 prev_nop_frag->fr_fix -= mips_opts.mips16 ? 2 : 4;
1753 --prev_nop_frag_holds;
1754 }
1755 else
1756 ++prev_nop_frag_required;
1757 }
1758
1759 if (prev_nop_frag_holds <= prev_nop_frag_required)
1760 prev_nop_frag = NULL;
1761
1762 ++prev_nop_frag_since;
1763
1764 /* Sanity check: by the time we reach the second instruction
1765 after prev_nop_frag, we should have used up all the nops
1766 one way or another. */
1767 assert (prev_nop_frag_since <= 1 || prev_nop_frag == NULL);
1768 }
1769 }
1770
f6688943 1771 if (*reloc_type > BFD_RELOC_UNUSED)
252b5132
RH
1772 {
1773 /* We need to set up a variant frag. */
1774 assert (mips_opts.mips16 && address_expr != NULL);
1775 f = frag_var (rs_machine_dependent, 4, 0,
f6688943 1776 RELAX_MIPS16_ENCODE (*reloc_type - BFD_RELOC_UNUSED,
252b5132
RH
1777 mips16_small, mips16_ext,
1778 (prev_pinfo
1779 & INSN_UNCOND_BRANCH_DELAY),
f6688943 1780 (*prev_insn_reloc_type
252b5132 1781 == BFD_RELOC_MIPS16_JMP)),
c4e7957c 1782 make_expr_symbol (address_expr), 0, NULL);
252b5132
RH
1783 }
1784 else if (place != NULL)
1785 f = place;
1786 else if (mips_opts.mips16
1787 && ! ip->use_extend
f6688943 1788 && *reloc_type != BFD_RELOC_MIPS16_JMP)
252b5132
RH
1789 {
1790 /* Make sure there is enough room to swap this instruction with
1791 a following jump instruction. */
1792 frag_grow (6);
1793 f = frag_more (2);
1794 }
1795 else
1796 {
1797 if (mips_opts.mips16
1798 && mips_opts.noreorder
1799 && (prev_pinfo & INSN_UNCOND_BRANCH_DELAY) != 0)
1800 as_warn (_("extended instruction in delay slot"));
1801
1802 f = frag_more (4);
1803 }
1804
f6688943
TS
1805 fixp[0] = fixp[1] = fixp[2] = NULL;
1806 if (address_expr != NULL && *reloc_type < BFD_RELOC_UNUSED)
252b5132
RH
1807 {
1808 if (address_expr->X_op == O_constant)
1809 {
4db1a35d 1810 valueT tmp;
f6688943
TS
1811
1812 switch (*reloc_type)
252b5132
RH
1813 {
1814 case BFD_RELOC_32:
1815 ip->insn_opcode |= address_expr->X_add_number;
1816 break;
1817
f6688943
TS
1818 case BFD_RELOC_MIPS_HIGHEST:
1819 tmp = (address_expr->X_add_number + 0x800080008000) >> 16;
1820 tmp >>= 16;
1821 ip->insn_opcode |= (tmp >> 16) & 0xffff;
1822 break;
1823
1824 case BFD_RELOC_MIPS_HIGHER:
1825 tmp = (address_expr->X_add_number + 0x80008000) >> 16;
1826 ip->insn_opcode |= (tmp >> 16) & 0xffff;
1827 break;
1828
1829 case BFD_RELOC_HI16_S:
1830 ip->insn_opcode |= ((address_expr->X_add_number + 0x8000)
1831 >> 16) & 0xffff;
1832 break;
1833
1834 case BFD_RELOC_HI16:
1835 ip->insn_opcode |= (address_expr->X_add_number >> 16) & 0xffff;
1836 break;
1837
252b5132
RH
1838 case BFD_RELOC_LO16:
1839 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
1840 break;
1841
1842 case BFD_RELOC_MIPS_JMP:
1843 if ((address_expr->X_add_number & 3) != 0)
1844 as_bad (_("jump to misaligned address (0x%lx)"),
1845 (unsigned long) address_expr->X_add_number);
7496292d
TS
1846 if (address_expr->X_add_number & ~0xfffffff
1847 || address_expr->X_add_number > 0x7fffffc)
1848 as_bad (_("jump address range overflow (0x%lx)"),
1849 (unsigned long) address_expr->X_add_number);
252b5132
RH
1850 ip->insn_opcode |= (address_expr->X_add_number >> 2) & 0x3ffffff;
1851 break;
1852
1853 case BFD_RELOC_MIPS16_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 |=
1862 (((address_expr->X_add_number & 0x7c0000) << 3)
1863 | ((address_expr->X_add_number & 0xf800000) >> 7)
1864 | ((address_expr->X_add_number & 0x3fffc) >> 2));
1865 break;
1866
cb56d3d3 1867 case BFD_RELOC_16_PCREL:
233b8738 1868 ip->insn_opcode |= address_expr->X_add_number & 0xffff;
cb56d3d3
TS
1869 break;
1870
252b5132
RH
1871 case BFD_RELOC_16_PCREL_S2:
1872 goto need_reloc;
1873
1874 default:
1875 internalError ();
1876 }
1877 }
1878 else
1879 {
1880 need_reloc:
f6688943 1881 /* Don't generate a reloc if we are writing into a variant frag. */
252b5132
RH
1882 if (place == NULL)
1883 {
f6688943
TS
1884 fixp[0] = fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
1885 address_expr,
1886 (*reloc_type == BFD_RELOC_16_PCREL
1887 || *reloc_type == BFD_RELOC_16_PCREL_S2),
1888 reloc_type[0]);
1889
b6ff326e 1890 /* These relocations can have an addend that won't fit in
f6688943
TS
1891 4 octets for 64bit assembly. */
1892 if (HAVE_64BIT_GPRS &&
1893 (*reloc_type == BFD_RELOC_16
98d3f06f
KH
1894 || *reloc_type == BFD_RELOC_32
1895 || *reloc_type == BFD_RELOC_MIPS_JMP
1896 || *reloc_type == BFD_RELOC_HI16_S
1897 || *reloc_type == BFD_RELOC_LO16
1898 || *reloc_type == BFD_RELOC_GPREL16
1899 || *reloc_type == BFD_RELOC_MIPS_LITERAL
1900 || *reloc_type == BFD_RELOC_GPREL32
1901 || *reloc_type == BFD_RELOC_64
1902 || *reloc_type == BFD_RELOC_CTOR
1903 || *reloc_type == BFD_RELOC_MIPS_SUB
1904 || *reloc_type == BFD_RELOC_MIPS_HIGHEST
1905 || *reloc_type == BFD_RELOC_MIPS_HIGHER
1906 || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
1907 || *reloc_type == BFD_RELOC_MIPS_REL16
1908 || *reloc_type == BFD_RELOC_MIPS_RELGOT))
f6688943
TS
1909 fixp[0]->fx_no_overflow = 1;
1910
252b5132
RH
1911 if (unmatched_hi)
1912 {
1913 struct mips_hi_fixup *hi_fixup;
1914
f6688943 1915 assert (*reloc_type == BFD_RELOC_HI16_S);
252b5132
RH
1916 hi_fixup = ((struct mips_hi_fixup *)
1917 xmalloc (sizeof (struct mips_hi_fixup)));
f6688943 1918 hi_fixup->fixp = fixp[0];
252b5132
RH
1919 hi_fixup->seg = now_seg;
1920 hi_fixup->next = mips_hi_fixup_list;
1921 mips_hi_fixup_list = hi_fixup;
1922 }
f6688943
TS
1923
1924 if (reloc_type[1] != BFD_RELOC_UNUSED)
1925 {
1926 /* FIXME: This symbol can be one of
1927 RSS_UNDEF, RSS_GP, RSS_GP0, RSS_LOC. */
1928 address_expr->X_op = O_absent;
1929 address_expr->X_add_symbol = 0;
1930 address_expr->X_add_number = 0;
1931
1932 fixp[1] = fix_new_exp (frag_now, f - frag_now->fr_literal,
1933 4, address_expr, false,
1934 reloc_type[1]);
1935
b6ff326e 1936 /* These relocations can have an addend that won't fit in
f6688943
TS
1937 4 octets for 64bit assembly. */
1938 if (HAVE_64BIT_GPRS &&
1939 (*reloc_type == BFD_RELOC_16
1940 || *reloc_type == BFD_RELOC_32
1941 || *reloc_type == BFD_RELOC_MIPS_JMP
1942 || *reloc_type == BFD_RELOC_HI16_S
1943 || *reloc_type == BFD_RELOC_LO16
1944 || *reloc_type == BFD_RELOC_GPREL16
1945 || *reloc_type == BFD_RELOC_MIPS_LITERAL
1946 || *reloc_type == BFD_RELOC_GPREL32
1947 || *reloc_type == BFD_RELOC_64
1948 || *reloc_type == BFD_RELOC_CTOR
1949 || *reloc_type == BFD_RELOC_MIPS_SUB
1950 || *reloc_type == BFD_RELOC_MIPS_HIGHEST
1951 || *reloc_type == BFD_RELOC_MIPS_HIGHER
1952 || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
1953 || *reloc_type == BFD_RELOC_MIPS_REL16
1954 || *reloc_type == BFD_RELOC_MIPS_RELGOT))
98d3f06f 1955 fixp[1]->fx_no_overflow = 1;
f6688943
TS
1956
1957 if (reloc_type[2] != BFD_RELOC_UNUSED)
1958 {
1959 address_expr->X_op = O_absent;
1960 address_expr->X_add_symbol = 0;
1961 address_expr->X_add_number = 0;
1962
1963 fixp[2] = fix_new_exp (frag_now,
1964 f - frag_now->fr_literal, 4,
1965 address_expr, false,
1966 reloc_type[2]);
1967
b6ff326e 1968 /* These relocations can have an addend that won't fit in
f6688943
TS
1969 4 octets for 64bit assembly. */
1970 if (HAVE_64BIT_GPRS &&
1971 (*reloc_type == BFD_RELOC_16
1972 || *reloc_type == BFD_RELOC_32
1973 || *reloc_type == BFD_RELOC_MIPS_JMP
1974 || *reloc_type == BFD_RELOC_HI16_S
1975 || *reloc_type == BFD_RELOC_LO16
1976 || *reloc_type == BFD_RELOC_GPREL16
1977 || *reloc_type == BFD_RELOC_MIPS_LITERAL
1978 || *reloc_type == BFD_RELOC_GPREL32
1979 || *reloc_type == BFD_RELOC_64
1980 || *reloc_type == BFD_RELOC_CTOR
1981 || *reloc_type == BFD_RELOC_MIPS_SUB
1982 || *reloc_type == BFD_RELOC_MIPS_HIGHEST
1983 || *reloc_type == BFD_RELOC_MIPS_HIGHER
1984 || *reloc_type == BFD_RELOC_MIPS_SCN_DISP
1985 || *reloc_type == BFD_RELOC_MIPS_REL16
1986 || *reloc_type == BFD_RELOC_MIPS_RELGOT))
98d3f06f 1987 fixp[2]->fx_no_overflow = 1;
f6688943
TS
1988 }
1989 }
252b5132
RH
1990 }
1991 }
1992 }
1993
1994 if (! mips_opts.mips16)
c5dd6aab
DJ
1995 {
1996 md_number_to_chars (f, ip->insn_opcode, 4);
1997#ifdef OBJ_ELF
1998 dwarf2_emit_insn (4);
1999#endif
2000 }
f6688943 2001 else if (*reloc_type == BFD_RELOC_MIPS16_JMP)
252b5132
RH
2002 {
2003 md_number_to_chars (f, ip->insn_opcode >> 16, 2);
2004 md_number_to_chars (f + 2, ip->insn_opcode & 0xffff, 2);
c5dd6aab
DJ
2005#ifdef OBJ_ELF
2006 dwarf2_emit_insn (4);
2007#endif
252b5132
RH
2008 }
2009 else
2010 {
2011 if (ip->use_extend)
2012 {
2013 md_number_to_chars (f, 0xf000 | ip->extend, 2);
2014 f += 2;
2015 }
2016 md_number_to_chars (f, ip->insn_opcode, 2);
c5dd6aab
DJ
2017#ifdef OBJ_ELF
2018 dwarf2_emit_insn (ip->use_extend ? 4 : 2);
2019#endif
252b5132
RH
2020 }
2021
2022 /* Update the register mask information. */
2023 if (! mips_opts.mips16)
2024 {
2025 if (pinfo & INSN_WRITE_GPR_D)
2026 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD);
2027 if ((pinfo & (INSN_WRITE_GPR_T | INSN_READ_GPR_T)) != 0)
2028 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RT) & OP_MASK_RT);
2029 if (pinfo & INSN_READ_GPR_S)
2030 mips_gprmask |= 1 << ((ip->insn_opcode >> OP_SH_RS) & OP_MASK_RS);
2031 if (pinfo & INSN_WRITE_GPR_31)
f9419b05 2032 mips_gprmask |= 1 << RA;
252b5132
RH
2033 if (pinfo & INSN_WRITE_FPR_D)
2034 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FD) & OP_MASK_FD);
2035 if ((pinfo & (INSN_WRITE_FPR_S | INSN_READ_FPR_S)) != 0)
2036 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FS) & OP_MASK_FS);
2037 if ((pinfo & (INSN_WRITE_FPR_T | INSN_READ_FPR_T)) != 0)
2038 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FT) & OP_MASK_FT);
2039 if ((pinfo & INSN_READ_FPR_R) != 0)
2040 mips_cprmask[1] |= 1 << ((ip->insn_opcode >> OP_SH_FR) & OP_MASK_FR);
2041 if (pinfo & INSN_COP)
2042 {
bdaaa2e1
KH
2043 /* We don't keep enough information to sort these cases out.
2044 The itbl support does keep this information however, although
2045 we currently don't support itbl fprmats as part of the cop
2046 instruction. May want to add this support in the future. */
252b5132
RH
2047 }
2048 /* Never set the bit for $0, which is always zero. */
beae10d5 2049 mips_gprmask &= ~1 << 0;
252b5132
RH
2050 }
2051 else
2052 {
2053 if (pinfo & (MIPS16_INSN_WRITE_X | MIPS16_INSN_READ_X))
2054 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RX)
2055 & MIPS16OP_MASK_RX);
2056 if (pinfo & (MIPS16_INSN_WRITE_Y | MIPS16_INSN_READ_Y))
2057 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RY)
2058 & MIPS16OP_MASK_RY);
2059 if (pinfo & MIPS16_INSN_WRITE_Z)
2060 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_RZ)
2061 & MIPS16OP_MASK_RZ);
2062 if (pinfo & (MIPS16_INSN_WRITE_T | MIPS16_INSN_READ_T))
2063 mips_gprmask |= 1 << TREG;
2064 if (pinfo & (MIPS16_INSN_WRITE_SP | MIPS16_INSN_READ_SP))
2065 mips_gprmask |= 1 << SP;
2066 if (pinfo & (MIPS16_INSN_WRITE_31 | MIPS16_INSN_READ_31))
2067 mips_gprmask |= 1 << RA;
2068 if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
2069 mips_gprmask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
2070 if (pinfo & MIPS16_INSN_READ_Z)
2071 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_MOVE32Z)
2072 & MIPS16OP_MASK_MOVE32Z);
2073 if (pinfo & MIPS16_INSN_READ_GPR_X)
2074 mips_gprmask |= 1 << ((ip->insn_opcode >> MIPS16OP_SH_REGR32)
2075 & MIPS16OP_MASK_REGR32);
2076 }
2077
2078 if (place == NULL && ! mips_opts.noreorder)
2079 {
2080 /* Filling the branch delay slot is more complex. We try to
2081 switch the branch with the previous instruction, which we can
2082 do if the previous instruction does not set up a condition
2083 that the branch tests and if the branch is not itself the
2084 target of any branch. */
2085 if ((pinfo & INSN_UNCOND_BRANCH_DELAY)
2086 || (pinfo & INSN_COND_BRANCH_DELAY))
2087 {
2088 if (mips_optimize < 2
2089 /* If we have seen .set volatile or .set nomove, don't
2090 optimize. */
2091 || mips_opts.nomove != 0
2092 /* If we had to emit any NOP instructions, then we
2093 already know we can not swap. */
2094 || nops != 0
2095 /* If we don't even know the previous insn, we can not
bdaaa2e1 2096 swap. */
252b5132
RH
2097 || ! prev_insn_valid
2098 /* If the previous insn is already in a branch delay
2099 slot, then we can not swap. */
2100 || prev_insn_is_delay_slot
2101 /* If the previous previous insn was in a .set
2102 noreorder, we can't swap. Actually, the MIPS
2103 assembler will swap in this situation. However, gcc
2104 configured -with-gnu-as will generate code like
2105 .set noreorder
2106 lw $4,XXX
2107 .set reorder
2108 INSN
2109 bne $4,$0,foo
2110 in which we can not swap the bne and INSN. If gcc is
2111 not configured -with-gnu-as, it does not output the
2112 .set pseudo-ops. We don't have to check
2113 prev_insn_unreordered, because prev_insn_valid will
2114 be 0 in that case. We don't want to use
2115 prev_prev_insn_valid, because we do want to be able
2116 to swap at the start of a function. */
2117 || prev_prev_insn_unreordered
2118 /* If the branch is itself the target of a branch, we
2119 can not swap. We cheat on this; all we check for is
2120 whether there is a label on this instruction. If
2121 there are any branches to anything other than a
2122 label, users must use .set noreorder. */
2123 || insn_labels != NULL
2124 /* If the previous instruction is in a variant frag, we
2125 can not do the swap. This does not apply to the
2126 mips16, which uses variant frags for different
2127 purposes. */
2128 || (! mips_opts.mips16
2129 && prev_insn_frag->fr_type == rs_machine_dependent)
2130 /* If the branch reads the condition codes, we don't
2131 even try to swap, because in the sequence
2132 ctc1 $X,$31
2133 INSN
2134 INSN
2135 bc1t LABEL
2136 we can not swap, and I don't feel like handling that
2137 case. */
2138 || (! mips_opts.mips16
9ce8a5dd 2139 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
2140 && (pinfo & INSN_READ_COND_CODE))
2141 /* We can not swap with an instruction that requires a
2142 delay slot, becase the target of the branch might
2143 interfere with that instruction. */
2144 || (! mips_opts.mips16
9ce8a5dd 2145 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132 2146 && (prev_pinfo
bdaaa2e1 2147 /* Itbl support may require additional care here. */
252b5132
RH
2148 & (INSN_LOAD_COPROC_DELAY
2149 | INSN_COPROC_MOVE_DELAY
2150 | INSN_WRITE_COND_CODE)))
2151 || (! (hilo_interlocks
ec68c924 2152 || (mips_tune == CPU_R3900 && (pinfo & INSN_MULT)))
252b5132
RH
2153 && (prev_pinfo
2154 & (INSN_READ_LO
2155 | INSN_READ_HI)))
2156 || (! mips_opts.mips16
2157 && ! gpr_interlocks
2158 && (prev_pinfo & INSN_LOAD_MEMORY_DELAY))
2159 || (! mips_opts.mips16
e7af610e 2160 && mips_opts.isa == ISA_MIPS1
bdaaa2e1 2161 /* Itbl support may require additional care here. */
252b5132
RH
2162 && (prev_pinfo & INSN_COPROC_MEMORY_DELAY))
2163 /* We can not swap with a branch instruction. */
2164 || (prev_pinfo
2165 & (INSN_UNCOND_BRANCH_DELAY
2166 | INSN_COND_BRANCH_DELAY
2167 | INSN_COND_BRANCH_LIKELY))
2168 /* We do not swap with a trap instruction, since it
2169 complicates trap handlers to have the trap
2170 instruction be in a delay slot. */
2171 || (prev_pinfo & INSN_TRAP)
2172 /* If the branch reads a register that the previous
2173 instruction sets, we can not swap. */
2174 || (! mips_opts.mips16
2175 && (prev_pinfo & INSN_WRITE_GPR_T)
2176 && insn_uses_reg (ip,
2177 ((prev_insn.insn_opcode >> OP_SH_RT)
2178 & OP_MASK_RT),
2179 MIPS_GR_REG))
2180 || (! mips_opts.mips16
2181 && (prev_pinfo & INSN_WRITE_GPR_D)
2182 && insn_uses_reg (ip,
2183 ((prev_insn.insn_opcode >> OP_SH_RD)
2184 & OP_MASK_RD),
2185 MIPS_GR_REG))
2186 || (mips_opts.mips16
2187 && (((prev_pinfo & MIPS16_INSN_WRITE_X)
2188 && insn_uses_reg (ip,
2189 ((prev_insn.insn_opcode
2190 >> MIPS16OP_SH_RX)
2191 & MIPS16OP_MASK_RX),
2192 MIPS16_REG))
2193 || ((prev_pinfo & MIPS16_INSN_WRITE_Y)
2194 && insn_uses_reg (ip,
2195 ((prev_insn.insn_opcode
2196 >> MIPS16OP_SH_RY)
2197 & MIPS16OP_MASK_RY),
2198 MIPS16_REG))
2199 || ((prev_pinfo & MIPS16_INSN_WRITE_Z)
2200 && insn_uses_reg (ip,
2201 ((prev_insn.insn_opcode
2202 >> MIPS16OP_SH_RZ)
2203 & MIPS16OP_MASK_RZ),
2204 MIPS16_REG))
2205 || ((prev_pinfo & MIPS16_INSN_WRITE_T)
2206 && insn_uses_reg (ip, TREG, MIPS_GR_REG))
2207 || ((prev_pinfo & MIPS16_INSN_WRITE_31)
2208 && insn_uses_reg (ip, RA, MIPS_GR_REG))
2209 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2210 && insn_uses_reg (ip,
2211 MIPS16OP_EXTRACT_REG32R (prev_insn.
2212 insn_opcode),
2213 MIPS_GR_REG))))
2214 /* If the branch writes a register that the previous
2215 instruction sets, we can not swap (we know that
2216 branches write only to RD or to $31). */
2217 || (! mips_opts.mips16
2218 && (prev_pinfo & INSN_WRITE_GPR_T)
2219 && (((pinfo & INSN_WRITE_GPR_D)
2220 && (((prev_insn.insn_opcode >> OP_SH_RT) & OP_MASK_RT)
2221 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2222 || ((pinfo & INSN_WRITE_GPR_31)
2223 && (((prev_insn.insn_opcode >> OP_SH_RT)
2224 & OP_MASK_RT)
f9419b05 2225 == RA))))
252b5132
RH
2226 || (! mips_opts.mips16
2227 && (prev_pinfo & INSN_WRITE_GPR_D)
2228 && (((pinfo & INSN_WRITE_GPR_D)
2229 && (((prev_insn.insn_opcode >> OP_SH_RD) & OP_MASK_RD)
2230 == ((ip->insn_opcode >> OP_SH_RD) & OP_MASK_RD)))
2231 || ((pinfo & INSN_WRITE_GPR_31)
2232 && (((prev_insn.insn_opcode >> OP_SH_RD)
2233 & OP_MASK_RD)
f9419b05 2234 == RA))))
252b5132
RH
2235 || (mips_opts.mips16
2236 && (pinfo & MIPS16_INSN_WRITE_31)
2237 && ((prev_pinfo & MIPS16_INSN_WRITE_31)
2238 || ((prev_pinfo & MIPS16_INSN_WRITE_GPR_Y)
2239 && (MIPS16OP_EXTRACT_REG32R (prev_insn.insn_opcode)
2240 == RA))))
2241 /* If the branch writes a register that the previous
2242 instruction reads, we can not swap (we know that
2243 branches only write to RD or to $31). */
2244 || (! mips_opts.mips16
2245 && (pinfo & INSN_WRITE_GPR_D)
2246 && insn_uses_reg (&prev_insn,
2247 ((ip->insn_opcode >> OP_SH_RD)
2248 & OP_MASK_RD),
2249 MIPS_GR_REG))
2250 || (! mips_opts.mips16
2251 && (pinfo & INSN_WRITE_GPR_31)
f9419b05 2252 && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
252b5132
RH
2253 || (mips_opts.mips16
2254 && (pinfo & MIPS16_INSN_WRITE_31)
2255 && insn_uses_reg (&prev_insn, RA, MIPS_GR_REG))
2256 /* If we are generating embedded PIC code, the branch
2257 might be expanded into a sequence which uses $at, so
2258 we can't swap with an instruction which reads it. */
2259 || (mips_pic == EMBEDDED_PIC
2260 && insn_uses_reg (&prev_insn, AT, MIPS_GR_REG))
2261 /* If the previous previous instruction has a load
2262 delay, and sets a register that the branch reads, we
2263 can not swap. */
2264 || (! mips_opts.mips16
9ce8a5dd 2265 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
bdaaa2e1 2266 /* Itbl support may require additional care here. */
252b5132
RH
2267 && ((prev_prev_insn.insn_mo->pinfo & INSN_LOAD_COPROC_DELAY)
2268 || (! gpr_interlocks
2269 && (prev_prev_insn.insn_mo->pinfo
2270 & INSN_LOAD_MEMORY_DELAY)))
2271 && insn_uses_reg (ip,
2272 ((prev_prev_insn.insn_opcode >> OP_SH_RT)
2273 & OP_MASK_RT),
2274 MIPS_GR_REG))
2275 /* If one instruction sets a condition code and the
2276 other one uses a condition code, we can not swap. */
2277 || ((pinfo & INSN_READ_COND_CODE)
2278 && (prev_pinfo & INSN_WRITE_COND_CODE))
2279 || ((pinfo & INSN_WRITE_COND_CODE)
2280 && (prev_pinfo & INSN_READ_COND_CODE))
2281 /* If the previous instruction uses the PC, we can not
2282 swap. */
2283 || (mips_opts.mips16
2284 && (prev_pinfo & MIPS16_INSN_READ_PC))
2285 /* If the previous instruction was extended, we can not
2286 swap. */
2287 || (mips_opts.mips16 && prev_insn_extended)
2288 /* If the previous instruction had a fixup in mips16
2289 mode, we can not swap. This normally means that the
2290 previous instruction was a 4 byte branch anyhow. */
f6688943 2291 || (mips_opts.mips16 && prev_insn_fixp[0])
bdaaa2e1
KH
2292 /* If the previous instruction is a sync, sync.l, or
2293 sync.p, we can not swap. */
f173e82e 2294 || (prev_pinfo & INSN_SYNC))
252b5132
RH
2295 {
2296 /* We could do even better for unconditional branches to
2297 portions of this object file; we could pick up the
2298 instruction at the destination, put it in the delay
2299 slot, and bump the destination address. */
2300 emit_nop ();
2301 /* Update the previous insn information. */
2302 prev_prev_insn = *ip;
2303 prev_insn.insn_mo = &dummy_opcode;
2304 }
2305 else
2306 {
2307 /* It looks like we can actually do the swap. */
2308 if (! mips_opts.mips16)
2309 {
2310 char *prev_f;
2311 char temp[4];
2312
2313 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2314 memcpy (temp, prev_f, 4);
2315 memcpy (prev_f, f, 4);
2316 memcpy (f, temp, 4);
f6688943
TS
2317 if (prev_insn_fixp[0])
2318 {
2319 prev_insn_fixp[0]->fx_frag = frag_now;
2320 prev_insn_fixp[0]->fx_where = f - frag_now->fr_literal;
2321 }
2322 if (prev_insn_fixp[1])
2323 {
2324 prev_insn_fixp[1]->fx_frag = frag_now;
2325 prev_insn_fixp[1]->fx_where = f - frag_now->fr_literal;
2326 }
2327 if (prev_insn_fixp[2])
252b5132 2328 {
f6688943
TS
2329 prev_insn_fixp[2]->fx_frag = frag_now;
2330 prev_insn_fixp[2]->fx_where = f - frag_now->fr_literal;
252b5132 2331 }
f6688943 2332 if (fixp[0])
252b5132 2333 {
f6688943
TS
2334 fixp[0]->fx_frag = prev_insn_frag;
2335 fixp[0]->fx_where = prev_insn_where;
2336 }
2337 if (fixp[1])
2338 {
2339 fixp[1]->fx_frag = prev_insn_frag;
2340 fixp[1]->fx_where = prev_insn_where;
2341 }
2342 if (fixp[2])
2343 {
2344 fixp[2]->fx_frag = prev_insn_frag;
2345 fixp[2]->fx_where = prev_insn_where;
252b5132
RH
2346 }
2347 }
2348 else
2349 {
2350 char *prev_f;
2351 char temp[2];
2352
f6688943
TS
2353 assert (prev_insn_fixp[0] == NULL);
2354 assert (prev_insn_fixp[1] == NULL);
2355 assert (prev_insn_fixp[2] == NULL);
252b5132
RH
2356 prev_f = prev_insn_frag->fr_literal + prev_insn_where;
2357 memcpy (temp, prev_f, 2);
2358 memcpy (prev_f, f, 2);
f6688943 2359 if (*reloc_type != BFD_RELOC_MIPS16_JMP)
252b5132 2360 {
f6688943 2361 assert (*reloc_type == BFD_RELOC_UNUSED);
252b5132
RH
2362 memcpy (f, temp, 2);
2363 }
2364 else
2365 {
2366 memcpy (f, f + 2, 2);
2367 memcpy (f + 2, temp, 2);
2368 }
f6688943
TS
2369 if (fixp[0])
2370 {
2371 fixp[0]->fx_frag = prev_insn_frag;
2372 fixp[0]->fx_where = prev_insn_where;
2373 }
2374 if (fixp[1])
2375 {
2376 fixp[1]->fx_frag = prev_insn_frag;
2377 fixp[1]->fx_where = prev_insn_where;
2378 }
2379 if (fixp[2])
252b5132 2380 {
f6688943
TS
2381 fixp[2]->fx_frag = prev_insn_frag;
2382 fixp[2]->fx_where = prev_insn_where;
252b5132
RH
2383 }
2384 }
2385
2386 /* Update the previous insn information; leave prev_insn
2387 unchanged. */
2388 prev_prev_insn = *ip;
2389 }
2390 prev_insn_is_delay_slot = 1;
2391
2392 /* If that was an unconditional branch, forget the previous
2393 insn information. */
2394 if (pinfo & INSN_UNCOND_BRANCH_DELAY)
2395 {
2396 prev_prev_insn.insn_mo = &dummy_opcode;
2397 prev_insn.insn_mo = &dummy_opcode;
2398 }
2399
f6688943
TS
2400 prev_insn_fixp[0] = NULL;
2401 prev_insn_fixp[1] = NULL;
2402 prev_insn_fixp[2] = NULL;
2403 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2404 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2405 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
252b5132
RH
2406 prev_insn_extended = 0;
2407 }
2408 else if (pinfo & INSN_COND_BRANCH_LIKELY)
2409 {
2410 /* We don't yet optimize a branch likely. What we should do
2411 is look at the target, copy the instruction found there
2412 into the delay slot, and increment the branch to jump to
2413 the next instruction. */
2414 emit_nop ();
2415 /* Update the previous insn information. */
2416 prev_prev_insn = *ip;
2417 prev_insn.insn_mo = &dummy_opcode;
f6688943
TS
2418 prev_insn_fixp[0] = NULL;
2419 prev_insn_fixp[1] = NULL;
2420 prev_insn_fixp[2] = NULL;
2421 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2422 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2423 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
252b5132
RH
2424 prev_insn_extended = 0;
2425 }
2426 else
2427 {
2428 /* Update the previous insn information. */
2429 if (nops > 0)
2430 prev_prev_insn.insn_mo = &dummy_opcode;
2431 else
2432 prev_prev_insn = prev_insn;
2433 prev_insn = *ip;
2434
2435 /* Any time we see a branch, we always fill the delay slot
2436 immediately; since this insn is not a branch, we know it
2437 is not in a delay slot. */
2438 prev_insn_is_delay_slot = 0;
2439
f6688943
TS
2440 prev_insn_fixp[0] = fixp[0];
2441 prev_insn_fixp[1] = fixp[1];
2442 prev_insn_fixp[2] = fixp[2];
2443 prev_insn_reloc_type[0] = reloc_type[0];
2444 prev_insn_reloc_type[1] = reloc_type[1];
2445 prev_insn_reloc_type[2] = reloc_type[2];
252b5132
RH
2446 if (mips_opts.mips16)
2447 prev_insn_extended = (ip->use_extend
f6688943 2448 || *reloc_type > BFD_RELOC_UNUSED);
252b5132
RH
2449 }
2450
2451 prev_prev_insn_unreordered = prev_insn_unreordered;
2452 prev_insn_unreordered = 0;
2453 prev_insn_frag = frag_now;
2454 prev_insn_where = f - frag_now->fr_literal;
2455 prev_insn_valid = 1;
2456 }
2457 else if (place == NULL)
2458 {
2459 /* We need to record a bit of information even when we are not
2460 reordering, in order to determine the base address for mips16
2461 PC relative relocs. */
2462 prev_prev_insn = prev_insn;
2463 prev_insn = *ip;
f6688943
TS
2464 prev_insn_reloc_type[0] = reloc_type[0];
2465 prev_insn_reloc_type[1] = reloc_type[1];
2466 prev_insn_reloc_type[2] = reloc_type[2];
252b5132
RH
2467 prev_prev_insn_unreordered = prev_insn_unreordered;
2468 prev_insn_unreordered = 1;
2469 }
2470
2471 /* We just output an insn, so the next one doesn't have a label. */
2472 mips_clear_insn_labels ();
2473
2474 /* We must ensure that a fixup associated with an unmatched %hi
2475 reloc does not become a variant frag. Otherwise, the
2476 rearrangement of %hi relocs in frob_file may confuse
2477 tc_gen_reloc. */
2478 if (unmatched_hi)
2479 {
2480 frag_wane (frag_now);
2481 frag_new (0);
2482 }
2483}
2484
2485/* This function forgets that there was any previous instruction or
2486 label. If PRESERVE is non-zero, it remembers enough information to
bdaaa2e1 2487 know whether nops are needed before a noreorder section. */
252b5132
RH
2488
2489static void
2490mips_no_prev_insn (preserve)
2491 int preserve;
2492{
2493 if (! preserve)
2494 {
2495 prev_insn.insn_mo = &dummy_opcode;
2496 prev_prev_insn.insn_mo = &dummy_opcode;
2497 prev_nop_frag = NULL;
2498 prev_nop_frag_holds = 0;
2499 prev_nop_frag_required = 0;
2500 prev_nop_frag_since = 0;
2501 }
2502 prev_insn_valid = 0;
2503 prev_insn_is_delay_slot = 0;
2504 prev_insn_unreordered = 0;
2505 prev_insn_extended = 0;
f6688943
TS
2506 prev_insn_reloc_type[0] = BFD_RELOC_UNUSED;
2507 prev_insn_reloc_type[1] = BFD_RELOC_UNUSED;
2508 prev_insn_reloc_type[2] = BFD_RELOC_UNUSED;
252b5132
RH
2509 prev_prev_insn_unreordered = 0;
2510 mips_clear_insn_labels ();
2511}
2512
2513/* This function must be called whenever we turn on noreorder or emit
2514 something other than instructions. It inserts any NOPS which might
2515 be needed by the previous instruction, and clears the information
2516 kept for the previous instructions. The INSNS parameter is true if
bdaaa2e1 2517 instructions are to follow. */
252b5132
RH
2518
2519static void
2520mips_emit_delays (insns)
2521 boolean insns;
2522{
2523 if (! mips_opts.noreorder)
2524 {
2525 int nops;
2526
2527 nops = 0;
2528 if ((! mips_opts.mips16
9ce8a5dd 2529 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
2530 && (! cop_interlocks
2531 && (prev_insn.insn_mo->pinfo
2532 & (INSN_LOAD_COPROC_DELAY
2533 | INSN_COPROC_MOVE_DELAY
2534 | INSN_WRITE_COND_CODE))))
2535 || (! hilo_interlocks
2536 && (prev_insn.insn_mo->pinfo
2537 & (INSN_READ_LO
2538 | INSN_READ_HI)))
2539 || (! mips_opts.mips16
2540 && ! gpr_interlocks
bdaaa2e1 2541 && (prev_insn.insn_mo->pinfo
252b5132
RH
2542 & INSN_LOAD_MEMORY_DELAY))
2543 || (! mips_opts.mips16
e7af610e 2544 && mips_opts.isa == ISA_MIPS1
252b5132
RH
2545 && (prev_insn.insn_mo->pinfo
2546 & INSN_COPROC_MEMORY_DELAY)))
2547 {
beae10d5 2548 /* Itbl support may require additional care here. */
252b5132
RH
2549 ++nops;
2550 if ((! mips_opts.mips16
9ce8a5dd 2551 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
2552 && (! cop_interlocks
2553 && prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2554 || (! hilo_interlocks
2555 && ((prev_insn.insn_mo->pinfo & INSN_READ_HI)
2556 || (prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2557 ++nops;
2558
2559 if (prev_insn_unreordered)
2560 nops = 0;
2561 }
2562 else if ((! mips_opts.mips16
9ce8a5dd 2563 && ISA_HAS_COPROC_DELAYS (mips_opts.isa)
252b5132
RH
2564 && (! cop_interlocks
2565 && prev_prev_insn.insn_mo->pinfo & INSN_WRITE_COND_CODE))
2566 || (! hilo_interlocks
2567 && ((prev_prev_insn.insn_mo->pinfo & INSN_READ_HI)
2568 || (prev_prev_insn.insn_mo->pinfo & INSN_READ_LO))))
2569 {
beae10d5 2570 /* Itbl support may require additional care here. */
252b5132
RH
2571 if (! prev_prev_insn_unreordered)
2572 ++nops;
2573 }
2574
2575 if (nops > 0)
2576 {
2577 struct insn_label_list *l;
2578
2579 if (insns)
2580 {
2581 /* Record the frag which holds the nop instructions, so
2582 that we can remove them if we don't need them. */
2583 frag_grow (mips_opts.mips16 ? nops * 2 : nops * 4);
2584 prev_nop_frag = frag_now;
2585 prev_nop_frag_holds = nops;
2586 prev_nop_frag_required = 0;
2587 prev_nop_frag_since = 0;
2588 }
2589
2590 for (; nops > 0; --nops)
2591 emit_nop ();
2592
2593 if (insns)
2594 {
2595 /* Move on to a new frag, so that it is safe to simply
bdaaa2e1 2596 decrease the size of prev_nop_frag. */
252b5132
RH
2597 frag_wane (frag_now);
2598 frag_new (0);
2599 }
2600
2601 for (l = insn_labels; l != NULL; l = l->next)
2602 {
98aa84af
AM
2603 valueT val;
2604
252b5132 2605 assert (S_GET_SEGMENT (l->label) == now_seg);
49309057 2606 symbol_set_frag (l->label, frag_now);
98aa84af 2607 val = (valueT) frag_now_fix ();
252b5132
RH
2608 /* mips16 text labels are stored as odd. */
2609 if (mips_opts.mips16)
f9419b05 2610 ++val;
98aa84af 2611 S_SET_VALUE (l->label, val);
252b5132
RH
2612 }
2613 }
2614 }
2615
2616 /* Mark instruction labels in mips16 mode. */
f9419b05 2617 if (insns)
252b5132
RH
2618 mips16_mark_labels ();
2619
2620 mips_no_prev_insn (insns);
2621}
2622
2623/* Build an instruction created by a macro expansion. This is passed
2624 a pointer to the count of instructions created so far, an
2625 expression, the name of the instruction to build, an operand format
2626 string, and corresponding arguments. */
2627
2628#ifdef USE_STDARG
2629static void
2630macro_build (char *place,
2631 int *counter,
2632 expressionS * ep,
2633 const char *name,
2634 const char *fmt,
2635 ...)
2636#else
2637static void
2638macro_build (place, counter, ep, name, fmt, va_alist)
2639 char *place;
2640 int *counter;
2641 expressionS *ep;
2642 const char *name;
2643 const char *fmt;
2644 va_dcl
2645#endif
2646{
2647 struct mips_cl_insn insn;
f6688943 2648 bfd_reloc_code_real_type r[3];
252b5132 2649 va_list args;
252b5132
RH
2650
2651#ifdef USE_STDARG
2652 va_start (args, fmt);
2653#else
2654 va_start (args);
2655#endif
2656
2657 /*
2658 * If the macro is about to expand into a second instruction,
2659 * print a warning if needed. We need to pass ip as a parameter
2660 * to generate a better warning message here...
2661 */
2662 if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
2663 as_warn (_("Macro instruction expanded into multiple instructions"));
2664
80cc45a5
EC
2665 /*
2666 * If the macro is about to expand into a second instruction,
2667 * and it is in a delay slot, print a warning.
2668 */
2669 if (place == NULL
2670 && *counter == 1
2671 && mips_opts.noreorder
2672 && (prev_prev_insn.insn_mo->pinfo
2673 & (INSN_UNCOND_BRANCH_DELAY | INSN_COND_BRANCH_DELAY
2b2e39bf 2674 | INSN_COND_BRANCH_LIKELY)) != 0)
80cc45a5
EC
2675 as_warn (_("Macro instruction expanded into multiple instructions in a branch delay slot"));
2676
252b5132 2677 if (place == NULL)
f9419b05 2678 ++*counter; /* bump instruction counter */
252b5132
RH
2679
2680 if (mips_opts.mips16)
2681 {
2682 mips16_macro_build (place, counter, ep, name, fmt, args);
2683 va_end (args);
2684 return;
2685 }
2686
f6688943
TS
2687 r[0] = BFD_RELOC_UNUSED;
2688 r[1] = BFD_RELOC_UNUSED;
2689 r[2] = BFD_RELOC_UNUSED;
252b5132
RH
2690 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
2691 assert (insn.insn_mo);
2692 assert (strcmp (name, insn.insn_mo->name) == 0);
2693
2694 /* Search until we get a match for NAME. */
2695 while (1)
2696 {
deec1734
CD
2697 /* It is assumed here that macros will never generate
2698 MDMX or MIPS-3D instructions. */
252b5132
RH
2699 if (strcmp (fmt, insn.insn_mo->args) == 0
2700 && insn.insn_mo->pinfo != INSN_MACRO
aec421e0
TS
2701 && OPCODE_IS_MEMBER (insn.insn_mo,
2702 (mips_opts.isa
2703 | (mips_opts.mips16 ? INSN_MIPS16 : 0)),
2704 mips_arch)
ec68c924 2705 && (mips_arch != CPU_R4650 || (insn.insn_mo->pinfo & FP_D) == 0))
252b5132
RH
2706 break;
2707
2708 ++insn.insn_mo;
2709 assert (insn.insn_mo->name);
2710 assert (strcmp (name, insn.insn_mo->name) == 0);
2711 }
2712
2713 insn.insn_opcode = insn.insn_mo->match;
2714 for (;;)
2715 {
2716 switch (*fmt++)
2717 {
2718 case '\0':
2719 break;
2720
2721 case ',':
2722 case '(':
2723 case ')':
2724 continue;
2725
2726 case 't':
2727 case 'w':
2728 case 'E':
38487616 2729 insn.insn_opcode |= va_arg (args, int) << OP_SH_RT;
252b5132
RH
2730 continue;
2731
2732 case 'c':
38487616
TS
2733 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE;
2734 continue;
2735
252b5132
RH
2736 case 'T':
2737 case 'W':
38487616 2738 insn.insn_opcode |= va_arg (args, int) << OP_SH_FT;
252b5132
RH
2739 continue;
2740
2741 case 'd':
2742 case 'G':
38487616 2743 insn.insn_opcode |= va_arg (args, int) << OP_SH_RD;
252b5132
RH
2744 continue;
2745
4372b673
NC
2746 case 'U':
2747 {
2748 int tmp = va_arg (args, int);
2749
38487616
TS
2750 insn.insn_opcode |= tmp << OP_SH_RT;
2751 insn.insn_opcode |= tmp << OP_SH_RD;
beae10d5 2752 continue;
4372b673
NC
2753 }
2754
252b5132
RH
2755 case 'V':
2756 case 'S':
38487616 2757 insn.insn_opcode |= va_arg (args, int) << OP_SH_FS;
252b5132
RH
2758 continue;
2759
2760 case 'z':
2761 continue;
2762
2763 case '<':
38487616 2764 insn.insn_opcode |= va_arg (args, int) << OP_SH_SHAMT;
252b5132
RH
2765 continue;
2766
2767 case 'D':
38487616 2768 insn.insn_opcode |= va_arg (args, int) << OP_SH_FD;
252b5132
RH
2769 continue;
2770
2771 case 'B':
38487616 2772 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE20;
252b5132
RH
2773 continue;
2774
4372b673 2775 case 'J':
38487616 2776 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE19;
4372b673
NC
2777 continue;
2778
252b5132 2779 case 'q':
38487616 2780 insn.insn_opcode |= va_arg (args, int) << OP_SH_CODE2;
252b5132
RH
2781 continue;
2782
2783 case 'b':
2784 case 's':
2785 case 'r':
2786 case 'v':
38487616 2787 insn.insn_opcode |= va_arg (args, int) << OP_SH_RS;
252b5132
RH
2788 continue;
2789
2790 case 'i':
2791 case 'j':
2792 case 'o':
f6688943 2793 *r = (bfd_reloc_code_real_type) va_arg (args, int);
cdf6fd85 2794 assert (*r == BFD_RELOC_GPREL16
f6688943
TS
2795 || *r == BFD_RELOC_MIPS_LITERAL
2796 || *r == BFD_RELOC_MIPS_HIGHER
2797 || *r == BFD_RELOC_HI16_S
2798 || *r == BFD_RELOC_LO16
2799 || *r == BFD_RELOC_MIPS_GOT16
2800 || *r == BFD_RELOC_MIPS_CALL16
438c16b8
TS
2801 || *r == BFD_RELOC_MIPS_GOT_DISP
2802 || *r == BFD_RELOC_MIPS_GOT_PAGE
2803 || *r == BFD_RELOC_MIPS_GOT_OFST
f6688943
TS
2804 || *r == BFD_RELOC_MIPS_GOT_LO16
2805 || *r == BFD_RELOC_MIPS_CALL_LO16
252b5132 2806 || (ep->X_op == O_subtract
f6688943 2807 && *r == BFD_RELOC_PCREL_LO16));
252b5132
RH
2808 continue;
2809
2810 case 'u':
f6688943 2811 *r = (bfd_reloc_code_real_type) va_arg (args, int);
252b5132
RH
2812 assert (ep != NULL
2813 && (ep->X_op == O_constant
2814 || (ep->X_op == O_symbol
f6688943
TS
2815 && (*r == BFD_RELOC_MIPS_HIGHEST
2816 || *r == BFD_RELOC_HI16_S
2817 || *r == BFD_RELOC_HI16
2818 || *r == BFD_RELOC_GPREL16
2819 || *r == BFD_RELOC_MIPS_GOT_HI16
2820 || *r == BFD_RELOC_MIPS_CALL_HI16))
252b5132 2821 || (ep->X_op == O_subtract
f6688943 2822 && *r == BFD_RELOC_PCREL_HI16_S)));
252b5132
RH
2823 continue;
2824
2825 case 'p':
2826 assert (ep != NULL);
2827 /*
2828 * This allows macro() to pass an immediate expression for
2829 * creating short branches without creating a symbol.
2830 * Note that the expression still might come from the assembly
2831 * input, in which case the value is not checked for range nor
2832 * is a relocation entry generated (yuck).
2833 */
2834 if (ep->X_op == O_constant)
2835 {
2836 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
2837 ep = NULL;
2838 }
2839 else
cb56d3d3 2840 if (mips_pic == EMBEDDED_PIC)
f6688943 2841 *r = BFD_RELOC_16_PCREL_S2;
cb56d3d3 2842 else
f6688943 2843 *r = BFD_RELOC_16_PCREL;
252b5132
RH
2844 continue;
2845
2846 case 'a':
2847 assert (ep != NULL);
f6688943 2848 *r = BFD_RELOC_MIPS_JMP;
252b5132
RH
2849 continue;
2850
2851 case 'C':
2852 insn.insn_opcode |= va_arg (args, unsigned long);
2853 continue;
2854
2855 default:
2856 internalError ();
2857 }
2858 break;
2859 }
2860 va_end (args);
f6688943 2861 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132
RH
2862
2863 append_insn (place, &insn, ep, r, false);
2864}
2865
2866static void
2867mips16_macro_build (place, counter, ep, name, fmt, args)
2868 char *place;
43841e91 2869 int *counter ATTRIBUTE_UNUSED;
252b5132
RH
2870 expressionS *ep;
2871 const char *name;
2872 const char *fmt;
2873 va_list args;
2874{
2875 struct mips_cl_insn insn;
f6688943
TS
2876 bfd_reloc_code_real_type r[3]
2877 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 2878
252b5132
RH
2879 insn.insn_mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
2880 assert (insn.insn_mo);
2881 assert (strcmp (name, insn.insn_mo->name) == 0);
2882
2883 while (strcmp (fmt, insn.insn_mo->args) != 0
2884 || insn.insn_mo->pinfo == INSN_MACRO)
2885 {
2886 ++insn.insn_mo;
2887 assert (insn.insn_mo->name);
2888 assert (strcmp (name, insn.insn_mo->name) == 0);
2889 }
2890
2891 insn.insn_opcode = insn.insn_mo->match;
2892 insn.use_extend = false;
2893
2894 for (;;)
2895 {
2896 int c;
2897
2898 c = *fmt++;
2899 switch (c)
2900 {
2901 case '\0':
2902 break;
2903
2904 case ',':
2905 case '(':
2906 case ')':
2907 continue;
2908
2909 case 'y':
2910 case 'w':
2911 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RY;
2912 continue;
2913
2914 case 'x':
2915 case 'v':
2916 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RX;
2917 continue;
2918
2919 case 'z':
2920 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_RZ;
2921 continue;
2922
2923 case 'Z':
2924 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_MOVE32Z;
2925 continue;
2926
2927 case '0':
2928 case 'S':
2929 case 'P':
2930 case 'R':
2931 continue;
2932
2933 case 'X':
2934 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_REGR32;
2935 continue;
2936
2937 case 'Y':
2938 {
2939 int regno;
2940
2941 regno = va_arg (args, int);
2942 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
2943 insn.insn_opcode |= regno << MIPS16OP_SH_REG32R;
2944 }
2945 continue;
2946
2947 case '<':
2948 case '>':
2949 case '4':
2950 case '5':
2951 case 'H':
2952 case 'W':
2953 case 'D':
2954 case 'j':
2955 case '8':
2956 case 'V':
2957 case 'C':
2958 case 'U':
2959 case 'k':
2960 case 'K':
2961 case 'p':
2962 case 'q':
2963 {
2964 assert (ep != NULL);
2965
2966 if (ep->X_op != O_constant)
874e8986 2967 *r = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
2968 else
2969 {
c4e7957c
TS
2970 mips16_immed (NULL, 0, c, ep->X_add_number, false, false,
2971 false, &insn.insn_opcode, &insn.use_extend,
2972 &insn.extend);
252b5132 2973 ep = NULL;
f6688943 2974 *r = BFD_RELOC_UNUSED;
252b5132
RH
2975 }
2976 }
2977 continue;
2978
2979 case '6':
2980 insn.insn_opcode |= va_arg (args, int) << MIPS16OP_SH_IMM6;
2981 continue;
2982 }
2983
2984 break;
2985 }
2986
f6688943 2987 assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132
RH
2988
2989 append_insn (place, &insn, ep, r, false);
2990}
2991
438c16b8
TS
2992/*
2993 * Generate a "jalr" instruction with a relocation hint to the called
2994 * function. This occurs in NewABI PIC code.
2995 */
2996static void
2997macro_build_jalr (icnt, ep)
2998 int icnt;
2999 expressionS *ep;
3000{
f21f8242
AO
3001 char *f;
3002
438c16b8 3003 if (HAVE_NEWABI)
f21f8242
AO
3004 {
3005 frag_grow (4);
3006 f = frag_more (0);
3007 }
438c16b8
TS
3008 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr", "d,s",
3009 RA, PIC_CALL_REG);
3010 if (HAVE_NEWABI)
f21f8242
AO
3011 fix_new_exp (frag_now, f - frag_now->fr_literal,
3012 0, ep, false, BFD_RELOC_MIPS_JALR);
438c16b8
TS
3013}
3014
252b5132
RH
3015/*
3016 * Generate a "lui" instruction.
3017 */
3018static void
3019macro_build_lui (place, counter, ep, regnum)
3020 char *place;
3021 int *counter;
3022 expressionS *ep;
3023 int regnum;
3024{
3025 expressionS high_expr;
3026 struct mips_cl_insn insn;
f6688943
TS
3027 bfd_reloc_code_real_type r[3]
3028 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
5a38dc70
AM
3029 const char *name = "lui";
3030 const char *fmt = "t,u";
252b5132
RH
3031
3032 assert (! mips_opts.mips16);
3033
3034 if (place == NULL)
3035 high_expr = *ep;
3036 else
3037 {
3038 high_expr.X_op = O_constant;
3039 high_expr.X_add_number = ep->X_add_number;
3040 }
3041
3042 if (high_expr.X_op == O_constant)
3043 {
3044 /* we can compute the instruction now without a relocation entry */
e7d556df
TS
3045 high_expr.X_add_number = ((high_expr.X_add_number + 0x8000)
3046 >> 16) & 0xffff;
f6688943 3047 *r = BFD_RELOC_UNUSED;
252b5132 3048 }
f6688943 3049 else if (! HAVE_NEWABI)
252b5132
RH
3050 {
3051 assert (ep->X_op == O_symbol);
3052 /* _gp_disp is a special case, used from s_cpload. */
3053 assert (mips_pic == NO_PIC
3054 || strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0);
f6688943 3055 *r = BFD_RELOC_HI16_S;
252b5132
RH
3056 }
3057
3058 /*
3059 * If the macro is about to expand into a second instruction,
3060 * print a warning if needed. We need to pass ip as a parameter
3061 * to generate a better warning message here...
3062 */
3063 if (mips_opts.warn_about_macros && place == NULL && *counter == 1)
3064 as_warn (_("Macro instruction expanded into multiple instructions"));
3065
3066 if (place == NULL)
f9419b05 3067 ++*counter; /* bump instruction counter */
252b5132
RH
3068
3069 insn.insn_mo = (struct mips_opcode *) hash_find (op_hash, name);
3070 assert (insn.insn_mo);
3071 assert (strcmp (name, insn.insn_mo->name) == 0);
3072 assert (strcmp (fmt, insn.insn_mo->args) == 0);
3073
3074 insn.insn_opcode = insn.insn_mo->match | (regnum << OP_SH_RT);
f6688943 3075 if (*r == BFD_RELOC_UNUSED)
252b5132
RH
3076 {
3077 insn.insn_opcode |= high_expr.X_add_number;
3078 append_insn (place, &insn, NULL, r, false);
3079 }
3080 else
3081 append_insn (place, &insn, &high_expr, r, false);
3082}
3083
3084/* set_at()
3085 * Generates code to set the $at register to true (one)
3086 * if reg is less than the immediate expression.
3087 */
3088static void
3089set_at (counter, reg, unsignedp)
3090 int *counter;
3091 int reg;
3092 int unsignedp;
3093{
3094 if (imm_expr.X_op == O_constant
3095 && imm_expr.X_add_number >= -0x8000
3096 && imm_expr.X_add_number < 0x8000)
3097 macro_build ((char *) NULL, counter, &imm_expr,
3098 unsignedp ? "sltiu" : "slti",
3099 "t,r,j", AT, reg, (int) BFD_RELOC_LO16);
3100 else
3101 {
4d34fb5f 3102 load_register (counter, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9 3103 macro_build ((char *) NULL, counter, (expressionS *) NULL,
252b5132
RH
3104 unsignedp ? "sltu" : "slt",
3105 "d,v,t", AT, reg, AT);
3106 }
3107}
3108
3109/* Warn if an expression is not a constant. */
3110
3111static void
3112check_absolute_expr (ip, ex)
3113 struct mips_cl_insn *ip;
3114 expressionS *ex;
3115{
3116 if (ex->X_op == O_big)
3117 as_bad (_("unsupported large constant"));
3118 else if (ex->X_op != O_constant)
3119 as_bad (_("Instruction %s requires absolute expression"), ip->insn_mo->name);
3120}
3121
3122/* Count the leading zeroes by performing a binary chop. This is a
3123 bulky bit of source, but performance is a LOT better for the
3124 majority of values than a simple loop to count the bits:
3125 for (lcnt = 0; (lcnt < 32); lcnt++)
3126 if ((v) & (1 << (31 - lcnt)))
3127 break;
3128 However it is not code size friendly, and the gain will drop a bit
3129 on certain cached systems.
3130*/
3131#define COUNT_TOP_ZEROES(v) \
3132 (((v) & ~0xffff) == 0 \
3133 ? ((v) & ~0xff) == 0 \
3134 ? ((v) & ~0xf) == 0 \
3135 ? ((v) & ~0x3) == 0 \
3136 ? ((v) & ~0x1) == 0 \
3137 ? !(v) \
3138 ? 32 \
3139 : 31 \
3140 : 30 \
3141 : ((v) & ~0x7) == 0 \
3142 ? 29 \
3143 : 28 \
3144 : ((v) & ~0x3f) == 0 \
3145 ? ((v) & ~0x1f) == 0 \
3146 ? 27 \
3147 : 26 \
3148 : ((v) & ~0x7f) == 0 \
3149 ? 25 \
3150 : 24 \
3151 : ((v) & ~0xfff) == 0 \
3152 ? ((v) & ~0x3ff) == 0 \
3153 ? ((v) & ~0x1ff) == 0 \
3154 ? 23 \
3155 : 22 \
3156 : ((v) & ~0x7ff) == 0 \
3157 ? 21 \
3158 : 20 \
3159 : ((v) & ~0x3fff) == 0 \
3160 ? ((v) & ~0x1fff) == 0 \
3161 ? 19 \
3162 : 18 \
3163 : ((v) & ~0x7fff) == 0 \
3164 ? 17 \
3165 : 16 \
3166 : ((v) & ~0xffffff) == 0 \
3167 ? ((v) & ~0xfffff) == 0 \
3168 ? ((v) & ~0x3ffff) == 0 \
3169 ? ((v) & ~0x1ffff) == 0 \
3170 ? 15 \
3171 : 14 \
3172 : ((v) & ~0x7ffff) == 0 \
3173 ? 13 \
3174 : 12 \
3175 : ((v) & ~0x3fffff) == 0 \
3176 ? ((v) & ~0x1fffff) == 0 \
3177 ? 11 \
3178 : 10 \
3179 : ((v) & ~0x7fffff) == 0 \
3180 ? 9 \
3181 : 8 \
3182 : ((v) & ~0xfffffff) == 0 \
3183 ? ((v) & ~0x3ffffff) == 0 \
3184 ? ((v) & ~0x1ffffff) == 0 \
3185 ? 7 \
3186 : 6 \
3187 : ((v) & ~0x7ffffff) == 0 \
3188 ? 5 \
3189 : 4 \
3190 : ((v) & ~0x3fffffff) == 0 \
3191 ? ((v) & ~0x1fffffff) == 0 \
3192 ? 3 \
3193 : 2 \
3194 : ((v) & ~0x7fffffff) == 0 \
3195 ? 1 \
3196 : 0)
3197
6373ee54
CD
3198/* Is the given value a sign-extended 32-bit value? */
3199#define IS_SEXT_32BIT_NUM(x) \
3200 (((x) &~ (offsetT) 0x7fffffff) == 0 \
3201 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
3202
252b5132
RH
3203/* load_register()
3204 * This routine generates the least number of instructions neccessary to load
3205 * an absolute expression value into a register.
3206 */
3207static void
3208load_register (counter, reg, ep, dbl)
3209 int *counter;
3210 int reg;
3211 expressionS *ep;
3212 int dbl;
3213{
3214 int freg;
3215 expressionS hi32, lo32;
3216
3217 if (ep->X_op != O_big)
3218 {
3219 assert (ep->X_op == O_constant);
3220 if (ep->X_add_number < 0x8000
3221 && (ep->X_add_number >= 0
3222 || (ep->X_add_number >= -0x8000
3223 && (! dbl
3224 || ! ep->X_unsigned
3225 || sizeof (ep->X_add_number) > 4))))
3226 {
3227 /* We can handle 16 bit signed values with an addiu to
3228 $zero. No need to ever use daddiu here, since $zero and
3229 the result are always correct in 32 bit mode. */
3230 macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3231 (int) BFD_RELOC_LO16);
3232 return;
3233 }
3234 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
3235 {
3236 /* We can handle 16 bit unsigned values with an ori to
3237 $zero. */
3238 macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, 0,
3239 (int) BFD_RELOC_LO16);
3240 return;
3241 }
6373ee54 3242 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)
252b5132
RH
3243 && (! dbl
3244 || ! ep->X_unsigned
3245 || sizeof (ep->X_add_number) > 4
3246 || (ep->X_add_number & 0x80000000) == 0))
ca4e0257 3247 || ((HAVE_32BIT_GPRS || ! dbl)
252b5132 3248 && (ep->X_add_number &~ (offsetT) 0xffffffff) == 0)
ca4e0257 3249 || (HAVE_32BIT_GPRS
252b5132
RH
3250 && ! dbl
3251 && ((ep->X_add_number &~ (offsetT) 0xffffffff)
3252 == ~ (offsetT) 0xffffffff)))
3253 {
3254 /* 32 bit values require an lui. */
3255 macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3256 (int) BFD_RELOC_HI16);
3257 if ((ep->X_add_number & 0xffff) != 0)
3258 macro_build ((char *) NULL, counter, ep, "ori", "t,r,i", reg, reg,
3259 (int) BFD_RELOC_LO16);
3260 return;
3261 }
3262 }
3263
3264 /* The value is larger than 32 bits. */
3265
ca4e0257 3266 if (HAVE_32BIT_GPRS)
252b5132 3267 {
956cd1d6
TS
3268 as_bad (_("Number (0x%lx) larger than 32 bits"),
3269 (unsigned long) ep->X_add_number);
252b5132
RH
3270 macro_build ((char *) NULL, counter, ep, "addiu", "t,r,j", reg, 0,
3271 (int) BFD_RELOC_LO16);
3272 return;
3273 }
3274
3275 if (ep->X_op != O_big)
3276 {
3277 hi32 = *ep;
3278 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3279 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
3280 hi32.X_add_number &= 0xffffffff;
3281 lo32 = *ep;
3282 lo32.X_add_number &= 0xffffffff;
3283 }
3284 else
3285 {
3286 assert (ep->X_add_number > 2);
3287 if (ep->X_add_number == 3)
3288 generic_bignum[3] = 0;
3289 else if (ep->X_add_number > 4)
3290 as_bad (_("Number larger than 64 bits"));
3291 lo32.X_op = O_constant;
3292 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
3293 hi32.X_op = O_constant;
3294 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
3295 }
3296
3297 if (hi32.X_add_number == 0)
3298 freg = 0;
3299 else
3300 {
3301 int shift, bit;
3302 unsigned long hi, lo;
3303
956cd1d6 3304 if (hi32.X_add_number == (offsetT) 0xffffffff)
beae10d5
KH
3305 {
3306 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
3307 {
3308 macro_build ((char *) NULL, counter, &lo32, "addiu", "t,r,j",
252b5132 3309 reg, 0, (int) BFD_RELOC_LO16);
beae10d5
KH
3310 return;
3311 }
3312 if (lo32.X_add_number & 0x80000000)
3313 {
3314 macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3315 (int) BFD_RELOC_HI16);
252b5132
RH
3316 if (lo32.X_add_number & 0xffff)
3317 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i",
3318 reg, reg, (int) BFD_RELOC_LO16);
beae10d5
KH
3319 return;
3320 }
3321 }
252b5132
RH
3322
3323 /* Check for 16bit shifted constant. We know that hi32 is
3324 non-zero, so start the mask on the first bit of the hi32
3325 value. */
3326 shift = 17;
3327 do
beae10d5
KH
3328 {
3329 unsigned long himask, lomask;
3330
3331 if (shift < 32)
3332 {
3333 himask = 0xffff >> (32 - shift);
3334 lomask = (0xffff << shift) & 0xffffffff;
3335 }
3336 else
3337 {
3338 himask = 0xffff << (shift - 32);
3339 lomask = 0;
3340 }
3341 if ((hi32.X_add_number & ~(offsetT) himask) == 0
3342 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
3343 {
3344 expressionS tmp;
3345
3346 tmp.X_op = O_constant;
3347 if (shift < 32)
3348 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
3349 | (lo32.X_add_number >> shift));
3350 else
3351 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
3352 macro_build ((char *) NULL, counter, &tmp,
3353 "ori", "t,r,i", reg, 0,
3354 (int) BFD_RELOC_LO16);
2396cfb9 3355 macro_build ((char *) NULL, counter, (expressionS *) NULL,
beae10d5
KH
3356 (shift >= 32) ? "dsll32" : "dsll",
3357 "d,w,<", reg, reg,
3358 (shift >= 32) ? shift - 32 : shift);
3359 return;
3360 }
f9419b05 3361 ++shift;
beae10d5
KH
3362 }
3363 while (shift <= (64 - 16));
252b5132
RH
3364
3365 /* Find the bit number of the lowest one bit, and store the
3366 shifted value in hi/lo. */
3367 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
3368 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
3369 if (lo != 0)
3370 {
3371 bit = 0;
3372 while ((lo & 1) == 0)
3373 {
3374 lo >>= 1;
3375 ++bit;
3376 }
3377 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
3378 hi >>= bit;
3379 }
3380 else
3381 {
3382 bit = 32;
3383 while ((hi & 1) == 0)
3384 {
3385 hi >>= 1;
3386 ++bit;
3387 }
3388 lo = hi;
3389 hi = 0;
3390 }
3391
3392 /* Optimize if the shifted value is a (power of 2) - 1. */
3393 if ((hi == 0 && ((lo + 1) & lo) == 0)
3394 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
beae10d5
KH
3395 {
3396 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
252b5132 3397 if (shift != 0)
beae10d5 3398 {
252b5132
RH
3399 expressionS tmp;
3400
3401 /* This instruction will set the register to be all
3402 ones. */
beae10d5
KH
3403 tmp.X_op = O_constant;
3404 tmp.X_add_number = (offsetT) -1;
3405 macro_build ((char *) NULL, counter, &tmp, "addiu", "t,r,j",
252b5132 3406 reg, 0, (int) BFD_RELOC_LO16);
beae10d5
KH
3407 if (bit != 0)
3408 {
3409 bit += shift;
2396cfb9 3410 macro_build ((char *) NULL, counter, (expressionS *) NULL,
beae10d5
KH
3411 (bit >= 32) ? "dsll32" : "dsll",
3412 "d,w,<", reg, reg,
3413 (bit >= 32) ? bit - 32 : bit);
3414 }
2396cfb9 3415 macro_build ((char *) NULL, counter, (expressionS *) NULL,
252b5132 3416 (shift >= 32) ? "dsrl32" : "dsrl",
beae10d5 3417 "d,w,<", reg, reg,
252b5132 3418 (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
3419 return;
3420 }
3421 }
252b5132
RH
3422
3423 /* Sign extend hi32 before calling load_register, because we can
3424 generally get better code when we load a sign extended value. */
3425 if ((hi32.X_add_number & 0x80000000) != 0)
beae10d5 3426 hi32.X_add_number |= ~(offsetT) 0xffffffff;
252b5132
RH
3427 load_register (counter, reg, &hi32, 0);
3428 freg = reg;
3429 }
3430 if ((lo32.X_add_number & 0xffff0000) == 0)
3431 {
3432 if (freg != 0)
3433 {
2396cfb9
TS
3434 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3435 "dsll32", "d,w,<", reg, freg, 0);
252b5132
RH
3436 freg = reg;
3437 }
3438 }
3439 else
3440 {
3441 expressionS mid16;
3442
956cd1d6 3443 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
beae10d5 3444 {
252b5132
RH
3445 macro_build ((char *) NULL, counter, &lo32, "lui", "t,u", reg,
3446 (int) BFD_RELOC_HI16);
956cd1d6
TS
3447 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3448 "dsrl32", "d,w,<", reg, reg, 0);
beae10d5
KH
3449 return;
3450 }
252b5132
RH
3451
3452 if (freg != 0)
3453 {
956cd1d6
TS
3454 macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3455 "d,w,<", reg, freg, 16);
252b5132
RH
3456 freg = reg;
3457 }
3458 mid16 = lo32;
3459 mid16.X_add_number >>= 16;
3460 macro_build ((char *) NULL, counter, &mid16, "ori", "t,r,i", reg,
3461 freg, (int) BFD_RELOC_LO16);
956cd1d6
TS
3462 macro_build ((char *) NULL, counter, (expressionS *) NULL, "dsll",
3463 "d,w,<", reg, reg, 16);
252b5132
RH
3464 freg = reg;
3465 }
3466 if ((lo32.X_add_number & 0xffff) != 0)
3467 macro_build ((char *) NULL, counter, &lo32, "ori", "t,r,i", reg, freg,
3468 (int) BFD_RELOC_LO16);
3469}
3470
3471/* Load an address into a register. */
3472
3473static void
c9914766 3474load_address (counter, reg, ep, used_at)
252b5132
RH
3475 int *counter;
3476 int reg;
3477 expressionS *ep;
d6bc6245 3478 int *used_at;
252b5132 3479{
f9419b05 3480 char *p = NULL;
252b5132
RH
3481
3482 if (ep->X_op != O_constant
3483 && ep->X_op != O_symbol)
3484 {
3485 as_bad (_("expression too complex"));
3486 ep->X_op = O_constant;
3487 }
3488
3489 if (ep->X_op == O_constant)
3490 {
c9914766 3491 load_register (counter, reg, ep, HAVE_64BIT_ADDRESSES);
252b5132
RH
3492 return;
3493 }
3494
3495 if (mips_pic == NO_PIC)
3496 {
3497 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 3498 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
3499 Otherwise we want
3500 lui $reg,<sym> (BFD_RELOC_HI16_S)
3501 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
d6bc6245 3502 If we have an addend, we always use the latter form.
76b3015f 3503
d6bc6245
TS
3504 With 64bit address space and a usable $at we want
3505 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3506 lui $at,<sym> (BFD_RELOC_HI16_S)
3507 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3508 daddiu $at,<sym> (BFD_RELOC_LO16)
3509 dsll32 $reg,0
3a482fd5 3510 daddu $reg,$reg,$at
76b3015f 3511
d6bc6245
TS
3512 If $at is already in use, we use an path which is suboptimal
3513 on superscalar processors.
3514 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
3515 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
3516 dsll $reg,16
3517 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
3518 dsll $reg,16
3519 daddiu $reg,<sym> (BFD_RELOC_LO16)
3520 */
c9914766 3521 if (HAVE_64BIT_ADDRESSES)
d6bc6245 3522 {
d6bc6245
TS
3523 /* We don't do GP optimization for now because RELAX_ENCODE can't
3524 hold the data for such large chunks. */
3525
460597ba 3526 if (*used_at == 0 && ! mips_opts.noat)
d6bc6245
TS
3527 {
3528 macro_build (p, counter, ep, "lui", "t,u",
3529 reg, (int) BFD_RELOC_MIPS_HIGHEST);
3530 macro_build (p, counter, ep, "lui", "t,u",
3531 AT, (int) BFD_RELOC_HI16_S);
3532 macro_build (p, counter, ep, "daddiu", "t,r,j",
3533 reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
3534 macro_build (p, counter, ep, "daddiu", "t,r,j",
3535 AT, AT, (int) BFD_RELOC_LO16);
2396cfb9
TS
3536 macro_build (p, counter, (expressionS *) NULL, "dsll32",
3537 "d,w,<", reg, reg, 0);
3a482fd5 3538 macro_build (p, counter, (expressionS *) NULL, "daddu",
2396cfb9 3539 "d,v,t", reg, reg, AT);
d6bc6245
TS
3540 *used_at = 1;
3541 }
3542 else
3543 {
3544 macro_build (p, counter, ep, "lui", "t,u",
3545 reg, (int) BFD_RELOC_MIPS_HIGHEST);
3546 macro_build (p, counter, ep, "daddiu", "t,r,j",
3547 reg, reg, (int) BFD_RELOC_MIPS_HIGHER);
2396cfb9
TS
3548 macro_build (p, counter, (expressionS *) NULL, "dsll",
3549 "d,w,<", reg, reg, 16);
d6bc6245
TS
3550 macro_build (p, counter, ep, "daddiu", "t,r,j",
3551 reg, reg, (int) BFD_RELOC_HI16_S);
2396cfb9
TS
3552 macro_build (p, counter, (expressionS *) NULL, "dsll",
3553 "d,w,<", reg, reg, 16);
d6bc6245
TS
3554 macro_build (p, counter, ep, "daddiu", "t,r,j",
3555 reg, reg, (int) BFD_RELOC_LO16);
3556 }
3557 }
252b5132
RH
3558 else
3559 {
d6bc6245
TS
3560 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
3561 && ! nopic_need_relax (ep->X_add_symbol, 1))
3562 {
3563 frag_grow (20);
3564 macro_build ((char *) NULL, counter, ep,
c9914766
TS
3565 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
3566 reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
d6bc6245
TS
3567 p = frag_var (rs_machine_dependent, 8, 0,
3568 RELAX_ENCODE (4, 8, 0, 4, 0,
3569 mips_opts.warn_about_macros),
956cd1d6 3570 ep->X_add_symbol, 0, NULL);
d6bc6245
TS
3571 }
3572 macro_build_lui (p, counter, ep, reg);
3573 if (p != NULL)
3574 p += 4;
c9914766
TS
3575 macro_build (p, counter, ep,
3576 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
d6bc6245
TS
3577 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3578 }
252b5132
RH
3579 }
3580 else if (mips_pic == SVR4_PIC && ! mips_big_got)
3581 {
3582 expressionS ex;
3583
3584 /* If this is a reference to an external symbol, we want
3585 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3586 Otherwise we want
3587 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3588 nop
3589 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
3590 If there is a constant, it must be added in after. */
3591 ex.X_add_number = ep->X_add_number;
3592 ep->X_add_number = 0;
3593 frag_grow (20);
3594 macro_build ((char *) NULL, counter, ep,
c9914766
TS
3595 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
3596 reg, (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132
RH
3597 macro_build ((char *) NULL, counter, (expressionS *) NULL, "nop", "");
3598 p = frag_var (rs_machine_dependent, 4, 0,
3599 RELAX_ENCODE (0, 4, -8, 0, 0, mips_opts.warn_about_macros),
3600 ep->X_add_symbol, (offsetT) 0, (char *) NULL);
3601 macro_build (p, counter, ep,
ca4e0257 3602 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
3603 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3604 if (ex.X_add_number != 0)
3605 {
3606 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3607 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3608 ex.X_op = O_constant;
3609 macro_build ((char *) NULL, counter, &ex,
ca4e0257 3610 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
3611 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3612 }
3613 }
3614 else if (mips_pic == SVR4_PIC)
3615 {
3616 expressionS ex;
3617 int off;
3618
3619 /* This is the large GOT case. If this is a reference to an
3620 external symbol, we want
3621 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
3622 addu $reg,$reg,$gp
3623 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
3624 Otherwise, for a reference to a local symbol, we want
3625 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
3626 nop
3627 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
438c16b8
TS
3628 If we have NewABI, we want
3629 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
3630 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
252b5132
RH
3631 If there is a constant, it must be added in after. */
3632 ex.X_add_number = ep->X_add_number;
3633 ep->X_add_number = 0;
438c16b8
TS
3634 if (HAVE_NEWABI)
3635 {
3636 macro_build ((char *) NULL, counter, ep,
3637 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3638 (int) BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
3639 macro_build (p, counter, ep,
3640 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
3641 reg, reg, (int) BFD_RELOC_MIPS_GOT_OFST);
3642 }
252b5132 3643 else
438c16b8
TS
3644 {
3645 if (reg_needs_delay (mips_gp_register))
3646 off = 4;
3647 else
3648 off = 0;
3649 frag_grow (32);
3650 macro_build ((char *) NULL, counter, ep, "lui", "t,u", reg,
3651 (int) BFD_RELOC_MIPS_GOT_HI16);
3652 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3653 HAVE_32BIT_ADDRESSES ? "addu" : "daddu", "d,v,t", reg,
3654 reg, mips_gp_register);
3655 macro_build ((char *) NULL, counter, ep,
3656 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
3657 "t,o(b)", reg, (int) BFD_RELOC_MIPS_GOT_LO16, reg);
3658 p = frag_var (rs_machine_dependent, 12 + off, 0,
3659 RELAX_ENCODE (12, 12 + off, off, 8 + off, 0,
3660 mips_opts.warn_about_macros),
3661 ep->X_add_symbol, 0, NULL);
3662 if (off > 0)
3663 {
3664 /* We need a nop before loading from $gp. This special
3665 check is required because the lui which starts the main
3666 instruction stream does not refer to $gp, and so will not
3667 insert the nop which may be required. */
3668 macro_build (p, counter, (expressionS *) NULL, "nop", "");
3669 p += 4;
3670 }
3671 macro_build (p, counter, ep,
3672 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", reg,
3673 (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
3674 p += 4;
252b5132
RH
3675 macro_build (p, counter, (expressionS *) NULL, "nop", "");
3676 p += 4;
438c16b8
TS
3677 macro_build (p, counter, ep,
3678 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3679 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
252b5132 3680 }
438c16b8 3681
252b5132
RH
3682 if (ex.X_add_number != 0)
3683 {
3684 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
3685 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
3686 ex.X_op = O_constant;
f7ea7ef2
TS
3687 macro_build ((char *) NULL, counter, &ex,
3688 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
3689 "t,r,j", reg, reg, (int) BFD_RELOC_LO16);
3690 }
3691 }
3692 else if (mips_pic == EMBEDDED_PIC)
3693 {
3694 /* We always do
cdf6fd85 3695 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
c9914766
TS
3696 */
3697 macro_build ((char *) NULL, counter, ep,
3698 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
3699 "t,r,j", reg, mips_gp_register, (int) BFD_RELOC_GPREL16);
252b5132
RH
3700 }
3701 else
3702 abort ();
3703}
3704
ea1fb5dc
RS
3705/* Move the contents of register SOURCE into register DEST. */
3706
3707static void
3708move_register (counter, dest, source)
3709 int *counter;
3710 int dest;
3711 int source;
3712{
3713 macro_build ((char *) NULL, counter, (expressionS *) NULL,
3714 HAVE_32BIT_GPRS ? "addu" : "daddu",
3715 "d,v,t", dest, source, 0);
3716}
3717
252b5132
RH
3718/*
3719 * Build macros
3720 * This routine implements the seemingly endless macro or synthesized
3721 * instructions and addressing modes in the mips assembly language. Many
3722 * of these macros are simple and are similar to each other. These could
3723 * probably be handled by some kind of table or grammer aproach instead of
3724 * this verbose method. Others are not simple macros but are more like
3725 * optimizing code generation.
3726 * One interesting optimization is when several store macros appear
3727 * consecutivly that would load AT with the upper half of the same address.
3728 * The ensuing load upper instructions are ommited. This implies some kind
3729 * of global optimization. We currently only optimize within a single macro.
3730 * For many of the load and store macros if the address is specified as a
3731 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
3732 * first load register 'at' with zero and use it as the base register. The
3733 * mips assembler simply uses register $zero. Just one tiny optimization
3734 * we're missing.
3735 */
3736static void
3737macro (ip)
3738 struct mips_cl_insn *ip;
3739{
3740 register int treg, sreg, dreg, breg;
3741 int tempreg;
3742 int mask;
3743 int icnt = 0;
43841e91 3744 int used_at = 0;
252b5132
RH
3745 expressionS expr1;
3746 const char *s;
3747 const char *s2;
3748 const char *fmt;
3749 int likely = 0;
3750 int dbl = 0;
3751 int coproc = 0;
3752 int lr = 0;
3753 int imm = 0;
3754 offsetT maxnum;
3755 int off;
3756 bfd_reloc_code_real_type r;
252b5132
RH
3757 int hold_mips_optimize;
3758
3759 assert (! mips_opts.mips16);
3760
3761 treg = (ip->insn_opcode >> 16) & 0x1f;
3762 dreg = (ip->insn_opcode >> 11) & 0x1f;
3763 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
3764 mask = ip->insn_mo->mask;
3765
3766 expr1.X_op = O_constant;
3767 expr1.X_op_symbol = NULL;
3768 expr1.X_add_symbol = NULL;
3769 expr1.X_add_number = 1;
3770
3771 switch (mask)
3772 {
3773 case M_DABS:
3774 dbl = 1;
3775 case M_ABS:
3776 /* bgez $a0,.+12
3777 move v0,$a0
3778 sub v0,$zero,$a0
3779 */
3780
3781 mips_emit_delays (true);
3782 ++mips_opts.noreorder;
3783 mips_any_noreorder = 1;
3784
3785 expr1.X_add_number = 8;
3786 macro_build ((char *) NULL, &icnt, &expr1, "bgez", "s,p", sreg);
3787 if (dreg == sreg)
2396cfb9
TS
3788 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
3789 0);
252b5132 3790 else
ea1fb5dc 3791 move_register (&icnt, dreg, sreg);
2396cfb9 3792 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 3793 dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
252b5132
RH
3794
3795 --mips_opts.noreorder;
3796 return;
3797
3798 case M_ADD_I:
3799 s = "addi";
3800 s2 = "add";
3801 goto do_addi;
3802 case M_ADDU_I:
3803 s = "addiu";
3804 s2 = "addu";
3805 goto do_addi;
3806 case M_DADD_I:
3807 dbl = 1;
3808 s = "daddi";
3809 s2 = "dadd";
3810 goto do_addi;
3811 case M_DADDU_I:
3812 dbl = 1;
3813 s = "daddiu";
3814 s2 = "daddu";
3815 do_addi:
3816 if (imm_expr.X_op == O_constant
3817 && imm_expr.X_add_number >= -0x8000
3818 && imm_expr.X_add_number < 0x8000)
3819 {
3820 macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,j", treg, sreg,
3821 (int) BFD_RELOC_LO16);
3822 return;
3823 }
3824 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9
TS
3825 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
3826 treg, sreg, AT);
252b5132
RH
3827 break;
3828
3829 case M_AND_I:
3830 s = "andi";
3831 s2 = "and";
3832 goto do_bit;
3833 case M_OR_I:
3834 s = "ori";
3835 s2 = "or";
3836 goto do_bit;
3837 case M_NOR_I:
3838 s = "";
3839 s2 = "nor";
3840 goto do_bit;
3841 case M_XOR_I:
3842 s = "xori";
3843 s2 = "xor";
3844 do_bit:
3845 if (imm_expr.X_op == O_constant
3846 && imm_expr.X_add_number >= 0
3847 && imm_expr.X_add_number < 0x10000)
3848 {
3849 if (mask != M_NOR_I)
3850 macro_build ((char *) NULL, &icnt, &imm_expr, s, "t,r,i", treg,
3851 sreg, (int) BFD_RELOC_LO16);
3852 else
3853 {
3854 macro_build ((char *) NULL, &icnt, &imm_expr, "ori", "t,r,i",
3855 treg, sreg, (int) BFD_RELOC_LO16);
2396cfb9
TS
3856 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nor",
3857 "d,v,t", treg, treg, 0);
252b5132
RH
3858 }
3859 return;
3860 }
3861
d6bc6245 3862 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
3863 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d,v,t",
3864 treg, sreg, AT);
252b5132
RH
3865 break;
3866
3867 case M_BEQ_I:
3868 s = "beq";
3869 goto beq_i;
3870 case M_BEQL_I:
3871 s = "beql";
3872 likely = 1;
3873 goto beq_i;
3874 case M_BNE_I:
3875 s = "bne";
3876 goto beq_i;
3877 case M_BNEL_I:
3878 s = "bnel";
3879 likely = 1;
3880 beq_i:
3881 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
3882 {
3883 macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg,
3884 0);
3885 return;
3886 }
4d34fb5f 3887 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
252b5132
RH
3888 macro_build ((char *) NULL, &icnt, &offset_expr, s, "s,t,p", sreg, AT);
3889 break;
3890
3891 case M_BGEL:
3892 likely = 1;
3893 case M_BGE:
3894 if (treg == 0)
3895 {
3896 macro_build ((char *) NULL, &icnt, &offset_expr,
2396cfb9 3897 likely ? "bgezl" : "bgez", "s,p", sreg);
252b5132
RH
3898 return;
3899 }
3900 if (sreg == 0)
3901 {
3902 macro_build ((char *) NULL, &icnt, &offset_expr,
2396cfb9 3903 likely ? "blezl" : "blez", "s,p", treg);
252b5132
RH
3904 return;
3905 }
2396cfb9
TS
3906 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
3907 AT, sreg, treg);
252b5132 3908 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 3909 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
3910 break;
3911
3912 case M_BGTL_I:
3913 likely = 1;
3914 case M_BGT_I:
3915 /* check for > max integer */
3916 maxnum = 0x7fffffff;
ca4e0257 3917 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
3918 {
3919 maxnum <<= 16;
3920 maxnum |= 0xffff;
3921 maxnum <<= 16;
3922 maxnum |= 0xffff;
3923 }
3924 if (imm_expr.X_op == O_constant
3925 && imm_expr.X_add_number >= maxnum
ca4e0257 3926 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
3927 {
3928 do_false:
3929 /* result is always false */
3930 if (! likely)
3931 {
39c0a331
L
3932 if (warn_nops)
3933 as_warn (_("Branch %s is always false (nop)"),
3934 ip->insn_mo->name);
2396cfb9
TS
3935 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop",
3936 "", 0);
252b5132
RH
3937 }
3938 else
3939 {
39c0a331
L
3940 if (warn_nops)
3941 as_warn (_("Branch likely %s is always false"),
3942 ip->insn_mo->name);
252b5132
RH
3943 macro_build ((char *) NULL, &icnt, &offset_expr, "bnel",
3944 "s,t,p", 0, 0);
3945 }
3946 return;
3947 }
3948 if (imm_expr.X_op != O_constant)
3949 as_bad (_("Unsupported large constant"));
f9419b05 3950 ++imm_expr.X_add_number;
252b5132
RH
3951 /* FALLTHROUGH */
3952 case M_BGE_I:
3953 case M_BGEL_I:
3954 if (mask == M_BGEL_I)
3955 likely = 1;
3956 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
3957 {
3958 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 3959 likely ? "bgezl" : "bgez", "s,p", sreg);
252b5132
RH
3960 return;
3961 }
3962 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
3963 {
3964 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 3965 likely ? "bgtzl" : "bgtz", "s,p", sreg);
252b5132
RH
3966 return;
3967 }
3968 maxnum = 0x7fffffff;
ca4e0257 3969 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
3970 {
3971 maxnum <<= 16;
3972 maxnum |= 0xffff;
3973 maxnum <<= 16;
3974 maxnum |= 0xffff;
3975 }
3976 maxnum = - maxnum - 1;
3977 if (imm_expr.X_op == O_constant
3978 && imm_expr.X_add_number <= maxnum
ca4e0257 3979 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
3980 {
3981 do_true:
3982 /* result is always true */
3983 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
3984 macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
3985 return;
3986 }
3987 set_at (&icnt, sreg, 0);
3988 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 3989 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
3990 break;
3991
3992 case M_BGEUL:
3993 likely = 1;
3994 case M_BGEU:
3995 if (treg == 0)
3996 goto do_true;
3997 if (sreg == 0)
3998 {
3999 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4000 likely ? "beql" : "beq", "s,t,p", 0, treg);
252b5132
RH
4001 return;
4002 }
2396cfb9
TS
4003 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4004 "d,v,t", AT, sreg, treg);
252b5132 4005 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4006 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4007 break;
4008
4009 case M_BGTUL_I:
4010 likely = 1;
4011 case M_BGTU_I:
4012 if (sreg == 0
ca4e0257 4013 || (HAVE_32BIT_GPRS
252b5132 4014 && imm_expr.X_op == O_constant
956cd1d6 4015 && imm_expr.X_add_number == (offsetT) 0xffffffff))
252b5132
RH
4016 goto do_false;
4017 if (imm_expr.X_op != O_constant)
4018 as_bad (_("Unsupported large constant"));
f9419b05 4019 ++imm_expr.X_add_number;
252b5132
RH
4020 /* FALLTHROUGH */
4021 case M_BGEU_I:
4022 case M_BGEUL_I:
4023 if (mask == M_BGEUL_I)
4024 likely = 1;
4025 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4026 goto do_true;
4027 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4028 {
4029 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4030 likely ? "bnel" : "bne", "s,t,p", sreg, 0);
252b5132
RH
4031 return;
4032 }
4033 set_at (&icnt, sreg, 1);
4034 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4035 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4036 break;
4037
4038 case M_BGTL:
4039 likely = 1;
4040 case M_BGT:
4041 if (treg == 0)
4042 {
4043 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4044 likely ? "bgtzl" : "bgtz", "s,p", sreg);
252b5132
RH
4045 return;
4046 }
4047 if (sreg == 0)
4048 {
4049 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4050 likely ? "bltzl" : "bltz", "s,p", treg);
252b5132
RH
4051 return;
4052 }
2396cfb9
TS
4053 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4054 AT, treg, sreg);
252b5132 4055 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4056 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4057 break;
4058
4059 case M_BGTUL:
4060 likely = 1;
4061 case M_BGTU:
4062 if (treg == 0)
4063 {
4064 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4065 likely ? "bnel" : "bne", "s,t,p", sreg, 0);
252b5132
RH
4066 return;
4067 }
4068 if (sreg == 0)
4069 goto do_false;
2396cfb9
TS
4070 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4071 "d,v,t", AT, treg, sreg);
252b5132 4072 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4073 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4074 break;
4075
4076 case M_BLEL:
4077 likely = 1;
4078 case M_BLE:
4079 if (treg == 0)
4080 {
4081 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4082 likely ? "blezl" : "blez", "s,p", sreg);
252b5132
RH
4083 return;
4084 }
4085 if (sreg == 0)
4086 {
4087 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4088 likely ? "bgezl" : "bgez", "s,p", treg);
252b5132
RH
4089 return;
4090 }
2396cfb9
TS
4091 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4092 AT, treg, sreg);
252b5132 4093 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4094 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4095 break;
4096
4097 case M_BLEL_I:
4098 likely = 1;
4099 case M_BLE_I:
4100 maxnum = 0x7fffffff;
ca4e0257 4101 if (HAVE_64BIT_GPRS && sizeof (maxnum) > 4)
252b5132
RH
4102 {
4103 maxnum <<= 16;
4104 maxnum |= 0xffff;
4105 maxnum <<= 16;
4106 maxnum |= 0xffff;
4107 }
4108 if (imm_expr.X_op == O_constant
4109 && imm_expr.X_add_number >= maxnum
ca4e0257 4110 && (HAVE_32BIT_GPRS || sizeof (maxnum) > 4))
252b5132
RH
4111 goto do_true;
4112 if (imm_expr.X_op != O_constant)
4113 as_bad (_("Unsupported large constant"));
f9419b05 4114 ++imm_expr.X_add_number;
252b5132
RH
4115 /* FALLTHROUGH */
4116 case M_BLT_I:
4117 case M_BLTL_I:
4118 if (mask == M_BLTL_I)
4119 likely = 1;
4120 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4121 {
4122 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4123 likely ? "bltzl" : "bltz", "s,p", sreg);
252b5132
RH
4124 return;
4125 }
4126 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4127 {
4128 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4129 likely ? "blezl" : "blez", "s,p", sreg);
252b5132
RH
4130 return;
4131 }
4132 set_at (&icnt, sreg, 0);
4133 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4134 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4135 break;
4136
4137 case M_BLEUL:
4138 likely = 1;
4139 case M_BLEU:
4140 if (treg == 0)
4141 {
4142 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4143 likely ? "beql" : "beq", "s,t,p", sreg, 0);
252b5132
RH
4144 return;
4145 }
4146 if (sreg == 0)
4147 goto do_true;
2396cfb9
TS
4148 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4149 "d,v,t", AT, treg, sreg);
252b5132 4150 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4151 likely ? "beql" : "beq", "s,t,p", AT, 0);
252b5132
RH
4152 break;
4153
4154 case M_BLEUL_I:
4155 likely = 1;
4156 case M_BLEU_I:
4157 if (sreg == 0
ca4e0257 4158 || (HAVE_32BIT_GPRS
252b5132 4159 && imm_expr.X_op == O_constant
956cd1d6 4160 && imm_expr.X_add_number == (offsetT) 0xffffffff))
252b5132
RH
4161 goto do_true;
4162 if (imm_expr.X_op != O_constant)
4163 as_bad (_("Unsupported large constant"));
f9419b05 4164 ++imm_expr.X_add_number;
252b5132
RH
4165 /* FALLTHROUGH */
4166 case M_BLTU_I:
4167 case M_BLTUL_I:
4168 if (mask == M_BLTUL_I)
4169 likely = 1;
4170 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4171 goto do_false;
4172 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4173 {
4174 macro_build ((char *) NULL, &icnt, &offset_expr,
4175 likely ? "beql" : "beq",
4176 "s,t,p", sreg, 0);
4177 return;
4178 }
4179 set_at (&icnt, sreg, 1);
4180 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4181 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4182 break;
4183
4184 case M_BLTL:
4185 likely = 1;
4186 case M_BLT:
4187 if (treg == 0)
4188 {
4189 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4190 likely ? "bltzl" : "bltz", "s,p", sreg);
252b5132
RH
4191 return;
4192 }
4193 if (sreg == 0)
4194 {
4195 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4196 likely ? "bgtzl" : "bgtz", "s,p", treg);
252b5132
RH
4197 return;
4198 }
2396cfb9
TS
4199 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
4200 AT, sreg, treg);
252b5132 4201 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4202 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4203 break;
4204
4205 case M_BLTUL:
4206 likely = 1;
4207 case M_BLTU:
4208 if (treg == 0)
4209 goto do_false;
4210 if (sreg == 0)
4211 {
4212 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4213 likely ? "bnel" : "bne", "s,t,p", 0, treg);
252b5132
RH
4214 return;
4215 }
2396cfb9
TS
4216 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
4217 "d,v,t", AT, sreg,
252b5132
RH
4218 treg);
4219 macro_build ((char *) NULL, &icnt, &offset_expr,
9a41af64 4220 likely ? "bnel" : "bne", "s,t,p", AT, 0);
252b5132
RH
4221 break;
4222
4223 case M_DDIV_3:
4224 dbl = 1;
4225 case M_DIV_3:
4226 s = "mflo";
4227 goto do_div3;
4228 case M_DREM_3:
4229 dbl = 1;
4230 case M_REM_3:
4231 s = "mfhi";
4232 do_div3:
4233 if (treg == 0)
4234 {
4235 as_warn (_("Divide by zero."));
4236 if (mips_trap)
2396cfb9 4237 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
9bd7d936 4238 "s,t,q", 0, 0, 7);
252b5132 4239 else
2396cfb9
TS
4240 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4241 "c", 7);
252b5132
RH
4242 return;
4243 }
4244
4245 mips_emit_delays (true);
4246 ++mips_opts.noreorder;
4247 mips_any_noreorder = 1;
4248 if (mips_trap)
4249 {
2396cfb9 4250 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
9bd7d936 4251 "s,t,q", treg, 0, 7);
2396cfb9 4252 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 4253 dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
252b5132
RH
4254 }
4255 else
4256 {
4257 expr1.X_add_number = 8;
4258 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
2396cfb9 4259 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 4260 dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
2396cfb9
TS
4261 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4262 "c", 7);
252b5132
RH
4263 }
4264 expr1.X_add_number = -1;
4265 macro_build ((char *) NULL, &icnt, &expr1,
4266 dbl ? "daddiu" : "addiu",
4267 "t,r,j", AT, 0, (int) BFD_RELOC_LO16);
4268 expr1.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
4269 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, AT);
4270 if (dbl)
4271 {
4272 expr1.X_add_number = 1;
4273 macro_build ((char *) NULL, &icnt, &expr1, "daddiu", "t,r,j", AT, 0,
4274 (int) BFD_RELOC_LO16);
2396cfb9
TS
4275 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsll32",
4276 "d,w,<", AT, AT, 31);
252b5132
RH
4277 }
4278 else
4279 {
4280 expr1.X_add_number = 0x80000000;
4281 macro_build ((char *) NULL, &icnt, &expr1, "lui", "t,u", AT,
4282 (int) BFD_RELOC_HI16);
4283 }
4284 if (mips_trap)
4285 {
2396cfb9 4286 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
9bd7d936 4287 "s,t,q", sreg, AT, 6);
252b5132
RH
4288 /* We want to close the noreorder block as soon as possible, so
4289 that later insns are available for delay slot filling. */
4290 --mips_opts.noreorder;
4291 }
4292 else
4293 {
4294 expr1.X_add_number = 8;
4295 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", sreg, AT);
2396cfb9
TS
4296 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
4297 0);
252b5132
RH
4298
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
2396cfb9
TS
4303 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4304 "c", 6);
252b5132 4305 }
2396cfb9 4306 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d", dreg);
252b5132
RH
4307 break;
4308
4309 case M_DIV_3I:
4310 s = "div";
4311 s2 = "mflo";
4312 goto do_divi;
4313 case M_DIVU_3I:
4314 s = "divu";
4315 s2 = "mflo";
4316 goto do_divi;
4317 case M_REM_3I:
4318 s = "div";
4319 s2 = "mfhi";
4320 goto do_divi;
4321 case M_REMU_3I:
4322 s = "divu";
4323 s2 = "mfhi";
4324 goto do_divi;
4325 case M_DDIV_3I:
4326 dbl = 1;
4327 s = "ddiv";
4328 s2 = "mflo";
4329 goto do_divi;
4330 case M_DDIVU_3I:
4331 dbl = 1;
4332 s = "ddivu";
4333 s2 = "mflo";
4334 goto do_divi;
4335 case M_DREM_3I:
4336 dbl = 1;
4337 s = "ddiv";
4338 s2 = "mfhi";
4339 goto do_divi;
4340 case M_DREMU_3I:
4341 dbl = 1;
4342 s = "ddivu";
4343 s2 = "mfhi";
4344 do_divi:
4345 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
4346 {
4347 as_warn (_("Divide by zero."));
4348 if (mips_trap)
2396cfb9 4349 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
9bd7d936 4350 "s,t,q", 0, 0, 7);
252b5132 4351 else
2396cfb9
TS
4352 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4353 "c", 7);
252b5132
RH
4354 return;
4355 }
4356 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
4357 {
4358 if (strcmp (s2, "mflo") == 0)
ea1fb5dc 4359 move_register (&icnt, dreg, sreg);
252b5132 4360 else
ea1fb5dc 4361 move_register (&icnt, dreg, 0);
252b5132
RH
4362 return;
4363 }
4364 if (imm_expr.X_op == O_constant
4365 && imm_expr.X_add_number == -1
4366 && s[strlen (s) - 1] != 'u')
4367 {
4368 if (strcmp (s2, "mflo") == 0)
4369 {
2396cfb9
TS
4370 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4371 dbl ? "dneg" : "neg", "d,w", dreg, sreg);
252b5132
RH
4372 }
4373 else
ea1fb5dc 4374 move_register (&icnt, dreg, 0);
252b5132
RH
4375 return;
4376 }
4377
4378 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9
TS
4379 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4380 sreg, AT);
4381 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
252b5132
RH
4382 break;
4383
4384 case M_DIVU_3:
4385 s = "divu";
4386 s2 = "mflo";
4387 goto do_divu3;
4388 case M_REMU_3:
4389 s = "divu";
4390 s2 = "mfhi";
4391 goto do_divu3;
4392 case M_DDIVU_3:
4393 s = "ddivu";
4394 s2 = "mflo";
4395 goto do_divu3;
4396 case M_DREMU_3:
4397 s = "ddivu";
4398 s2 = "mfhi";
4399 do_divu3:
4400 mips_emit_delays (true);
4401 ++mips_opts.noreorder;
4402 mips_any_noreorder = 1;
4403 if (mips_trap)
4404 {
2396cfb9 4405 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "teq",
9bd7d936 4406 "s,t,q", treg, 0, 7);
2396cfb9
TS
4407 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4408 sreg, treg);
252b5132
RH
4409 /* We want to close the noreorder block as soon as possible, so
4410 that later insns are available for delay slot filling. */
4411 --mips_opts.noreorder;
4412 }
4413 else
4414 {
4415 expr1.X_add_number = 8;
4416 macro_build ((char *) NULL, &icnt, &expr1, "bne", "s,t,p", treg, 0);
2396cfb9
TS
4417 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "z,s,t",
4418 sreg, treg);
252b5132
RH
4419
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;
2396cfb9
TS
4423 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
4424 "c", 7);
252b5132 4425 }
2396cfb9 4426 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "d", dreg);
252b5132
RH
4427 return;
4428
4429 case M_DLA_AB:
4430 dbl = 1;
4431 case M_LA_AB:
4432 /* Load the address of a symbol into a register. If breg is not
4433 zero, we then add a base register to it. */
4434
3bec30a8
TS
4435 if (dbl && HAVE_32BIT_GPRS)
4436 as_warn (_("dla used to load 32-bit register"));
4437
c90bbe5b 4438 if (! dbl && HAVE_64BIT_OBJECTS)
3bec30a8
TS
4439 as_warn (_("la used to load 64-bit address"));
4440
0c11417f
MR
4441 if (offset_expr.X_op == O_constant
4442 && offset_expr.X_add_number >= -0x8000
4443 && offset_expr.X_add_number < 0x8000)
4444 {
4445 macro_build ((char *) NULL, &icnt, &offset_expr,
4446 (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
4447 "t,r,j", treg, sreg, (int) BFD_RELOC_LO16);
4448 return;
4449 }
4450
afdbd6d0
CD
4451 if (treg == breg)
4452 {
4453 tempreg = AT;
4454 used_at = 1;
4455 }
4456 else
4457 {
4458 tempreg = treg;
4459 used_at = 0;
4460 }
4461
252b5132
RH
4462 /* When generating embedded PIC code, we permit expressions of
4463 the form
afdbd6d0
CD
4464 la $treg,foo-bar
4465 la $treg,foo-bar($breg)
bb2d6cd7 4466 where bar is an address in the current section. These are used
252b5132
RH
4467 when getting the addresses of functions. We don't permit
4468 X_add_number to be non-zero, because if the symbol is
4469 external the relaxing code needs to know that any addend is
4470 purely the offset to X_op_symbol. */
4471 if (mips_pic == EMBEDDED_PIC
4472 && offset_expr.X_op == O_subtract
49309057 4473 && (symbol_constant_p (offset_expr.X_op_symbol)
bb2d6cd7 4474 ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
49309057
ILT
4475 : (symbol_equated_p (offset_expr.X_op_symbol)
4476 && (S_GET_SEGMENT
4477 (symbol_get_value_expression (offset_expr.X_op_symbol)
4478 ->X_add_symbol)
bb2d6cd7 4479 == now_seg)))
bb2d6cd7
GK
4480 && (offset_expr.X_add_number == 0
4481 || OUTPUT_FLAVOR == bfd_target_elf_flavour))
252b5132 4482 {
afdbd6d0
CD
4483 if (breg == 0)
4484 {
4485 tempreg = treg;
4486 used_at = 0;
4487 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4488 tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4489 }
4490 else
4491 {
4492 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
4493 tempreg, (int) BFD_RELOC_PCREL_HI16_S);
4494 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4d34fb5f 4495 (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu",
afdbd6d0
CD
4496 "d,v,t", tempreg, tempreg, breg);
4497 }
252b5132 4498 macro_build ((char *) NULL, &icnt, &offset_expr,
4d34fb5f 4499 (dbl || HAVE_64BIT_ADDRESSES) ? "daddiu" : "addiu",
afdbd6d0
CD
4500 "t,r,j", treg, tempreg, (int) BFD_RELOC_PCREL_LO16);
4501 if (! used_at)
4502 return;
4503 break;
252b5132
RH
4504 }
4505
4506 if (offset_expr.X_op != O_symbol
4507 && offset_expr.X_op != O_constant)
4508 {
4509 as_bad (_("expression too complex"));
4510 offset_expr.X_op = O_constant;
4511 }
4512
252b5132 4513 if (offset_expr.X_op == O_constant)
4d34fb5f
TS
4514 load_register (&icnt, tempreg, &offset_expr,
4515 ((mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
4516 ? (dbl || HAVE_64BIT_ADDRESSES)
4517 : HAVE_64BIT_ADDRESSES));
252b5132
RH
4518 else if (mips_pic == NO_PIC)
4519 {
d6bc6245 4520 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 4521 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
4522 Otherwise we want
4523 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
4524 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4525 If we have a constant, we need two instructions anyhow,
d6bc6245 4526 so we may as well always use the latter form.
76b3015f 4527
d6bc6245
TS
4528 With 64bit address space and a usable $at we want
4529 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4530 lui $at,<sym> (BFD_RELOC_HI16_S)
4531 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4532 daddiu $at,<sym> (BFD_RELOC_LO16)
4533 dsll32 $tempreg,0
3a482fd5 4534 daddu $tempreg,$tempreg,$at
76b3015f 4535
d6bc6245
TS
4536 If $at is already in use, we use an path which is suboptimal
4537 on superscalar processors.
4538 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
4539 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
4540 dsll $tempreg,16
4541 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
4542 dsll $tempreg,16
4543 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
4544 */
f9419b05 4545 char *p = NULL;
d6bc6245 4546 if (HAVE_64BIT_ADDRESSES)
252b5132 4547 {
d6bc6245
TS
4548 /* We don't do GP optimization for now because RELAX_ENCODE can't
4549 hold the data for such large chunks. */
4550
460597ba 4551 if (used_at == 0 && ! mips_opts.noat)
98d3f06f
KH
4552 {
4553 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4554 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4555 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4556 AT, (int) BFD_RELOC_HI16_S);
4557 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4558 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4559 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4560 AT, AT, (int) BFD_RELOC_LO16);
4561 macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
4562 "d,w,<", tempreg, tempreg, 0);
3a482fd5
MR
4563 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
4564 "d,v,t", tempreg, tempreg, AT);
98d3f06f
KH
4565 used_at = 1;
4566 }
4567 else
4568 {
4569 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
4570 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
4571 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4572 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
4573 macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4574 tempreg, tempreg, 16);
4575 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4576 tempreg, tempreg, (int) BFD_RELOC_HI16_S);
4577 macro_build (p, &icnt, (expressionS *) NULL, "dsll", "d,w,<",
4578 tempreg, tempreg, 16);
4579 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
4580 tempreg, tempreg, (int) BFD_RELOC_LO16);
4581 }
4582 }
4583 else
4584 {
4585 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
4586 && ! nopic_need_relax (offset_expr.X_add_symbol, 1))
4587 {
4588 frag_grow (20);
4589 macro_build ((char *) NULL, &icnt, &offset_expr, "addiu",
c9914766
TS
4590 "t,r,j", tempreg, mips_gp_register,
4591 (int) BFD_RELOC_GPREL16);
98d3f06f
KH
4592 p = frag_var (rs_machine_dependent, 8, 0,
4593 RELAX_ENCODE (4, 8, 0, 4, 0,
4594 mips_opts.warn_about_macros),
4595 offset_expr.X_add_symbol, 0, NULL);
4596 }
4597 macro_build_lui (p, &icnt, &offset_expr, tempreg);
4598 if (p != NULL)
4599 p += 4;
4600 macro_build (p, &icnt, &offset_expr, "addiu",
4601 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4602 }
252b5132
RH
4603 }
4604 else if (mips_pic == SVR4_PIC && ! mips_big_got)
4605 {
9117d219
NC
4606 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
4607
252b5132
RH
4608 /* If this is a reference to an external symbol, and there
4609 is no constant, we want
4610 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9117d219
NC
4611 or if tempreg is PIC_CALL_REG
4612 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
252b5132
RH
4613 For a local symbol, we want
4614 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4615 nop
4616 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4617
4618 If we have a small constant, and this is a reference to
4619 an external symbol, we want
4620 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4621 nop
4622 addiu $tempreg,$tempreg,<constant>
4623 For a local symbol, we want the same instruction
4624 sequence, but we output a BFD_RELOC_LO16 reloc on the
4625 addiu instruction.
4626
4627 If we have a large constant, and this is a reference to
4628 an external symbol, we want
4629 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4630 lui $at,<hiconstant>
4631 addiu $at,$at,<loconstant>
4632 addu $tempreg,$tempreg,$at
4633 For a local symbol, we want the same instruction
4634 sequence, but we output a BFD_RELOC_LO16 reloc on the
4635 addiu instruction. */
4636 expr1.X_add_number = offset_expr.X_add_number;
4637 offset_expr.X_add_number = 0;
4638 frag_grow (32);
9117d219
NC
4639 if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4640 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
4d34fb5f
TS
4641 macro_build ((char *) NULL, &icnt, &offset_expr,
4642 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
c9914766 4643 "t,o(b)", tempreg, lw_reloc_type, mips_gp_register);
252b5132
RH
4644 if (expr1.X_add_number == 0)
4645 {
4646 int off;
f9419b05 4647 char *p;
252b5132
RH
4648
4649 if (breg == 0)
4650 off = 0;
4651 else
4652 {
4653 /* We're going to put in an addu instruction using
4654 tempreg, so we may as well insert the nop right
4655 now. */
4656 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4657 "nop", "");
4658 off = 4;
4659 }
4660 p = frag_var (rs_machine_dependent, 8 - off, 0,
4661 RELAX_ENCODE (0, 8 - off, -4 - off, 4 - off, 0,
4662 (breg == 0
4663 ? mips_opts.warn_about_macros
4664 : 0)),
c4e7957c 4665 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
4666 if (breg == 0)
4667 {
4668 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4669 p += 4;
4670 }
4671 macro_build (p, &icnt, &expr1,
ca4e0257 4672 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
4673 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4674 /* FIXME: If breg == 0, and the next instruction uses
4675 $tempreg, then if this variant case is used an extra
4676 nop will be generated. */
4677 }
4678 else if (expr1.X_add_number >= -0x8000
4679 && expr1.X_add_number < 0x8000)
4680 {
4681 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4682 "nop", "");
4683 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 4684 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132 4685 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
c4e7957c
TS
4686 frag_var (rs_machine_dependent, 0, 0,
4687 RELAX_ENCODE (0, 0, -12, -4, 0, 0),
4688 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
4689 }
4690 else
4691 {
4692 int off1;
4693
4694 /* If we are going to add in a base register, and the
4695 target register and the base register are the same,
4696 then we are using AT as a temporary register. Since
4697 we want to load the constant into AT, we add our
4698 current AT (from the global offset table) and the
4699 register into the register now, and pretend we were
4700 not using a base register. */
4701 if (breg != treg)
4702 off1 = 0;
4703 else
4704 {
4705 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4706 "nop", "");
4707 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 4708 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
4709 "d,v,t", treg, AT, breg);
4710 breg = 0;
4711 tempreg = treg;
4712 off1 = -8;
4713 }
4714
4715 /* Set mips_optimize around the lui instruction to avoid
4716 inserting an unnecessary nop after the lw. */
4717 hold_mips_optimize = mips_optimize;
4718 mips_optimize = 2;
c4e7957c 4719 macro_build_lui (NULL, &icnt, &expr1, AT);
252b5132
RH
4720 mips_optimize = hold_mips_optimize;
4721
4722 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 4723 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
4724 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4725 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 4726 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132 4727 "d,v,t", tempreg, tempreg, AT);
c4e7957c
TS
4728 frag_var (rs_machine_dependent, 0, 0,
4729 RELAX_ENCODE (0, 0, -16 + off1, -8, 0, 0),
4730 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
4731 used_at = 1;
4732 }
4733 }
4734 else if (mips_pic == SVR4_PIC)
4735 {
4736 int gpdel;
f9419b05 4737 char *p;
9117d219
NC
4738 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
4739 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
252b5132
RH
4740
4741 /* This is the large GOT case. If this is a reference to an
4742 external symbol, and there is no constant, we want
4743 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
4744 addu $tempreg,$tempreg,$gp
4745 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
9117d219
NC
4746 or if tempreg is PIC_CALL_REG
4747 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
4748 addu $tempreg,$tempreg,$gp
4749 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
252b5132
RH
4750 For a local symbol, we want
4751 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4752 nop
4753 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
4754
4755 If we have a small constant, and this is a reference to
4756 an external symbol, we want
4757 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
4758 addu $tempreg,$tempreg,$gp
4759 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4760 nop
4761 addiu $tempreg,$tempreg,<constant>
4762 For a local symbol, we want
4763 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4764 nop
4765 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
4766
4767 If we have a large constant, and this is a reference to
4768 an external symbol, we want
4769 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
4770 addu $tempreg,$tempreg,$gp
4771 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
4772 lui $at,<hiconstant>
4773 addiu $at,$at,<loconstant>
4774 addu $tempreg,$tempreg,$at
4775 For a local symbol, we want
4776 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
4777 lui $at,<hiconstant>
4778 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
4779 addu $tempreg,$tempreg,$at
438c16b8
TS
4780
4781 For NewABI, we want for data addresses
4782 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
4783 If tempreg is PIC_CALL_REG pointing to a external symbol, we want
4784 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
4785 */
4786 if (HAVE_NEWABI)
4787 {
4788 int reloc_type = (tempreg == PIC_CALL_REG
4789 ? BFD_RELOC_MIPS_CALL16
4790 : BFD_RELOC_MIPS_GOT_DISP);
4791
4792 macro_build ((char *) NULL, &icnt, &offset_expr,
4793 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
4794 "t,o(b)", tempreg, reloc_type, mips_gp_register);
4795
4796 if (breg != 0)
4797 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4798 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
4799 "d,v,t", treg, tempreg, breg);
4800
4801 if (! used_at)
4802 return;
4803
4804 break;
4805 }
252b5132
RH
4806 expr1.X_add_number = offset_expr.X_add_number;
4807 offset_expr.X_add_number = 0;
4808 frag_grow (52);
f7ea7ef2 4809 if (reg_needs_delay (mips_gp_register))
252b5132
RH
4810 gpdel = 4;
4811 else
4812 gpdel = 0;
9117d219
NC
4813 if (expr1.X_add_number == 0 && tempreg == PIC_CALL_REG)
4814 {
4815 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
4816 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
4817 }
252b5132 4818 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
9117d219 4819 tempreg, lui_reloc_type);
252b5132 4820 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 4821 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 4822 "d,v,t", tempreg, tempreg, mips_gp_register);
252b5132 4823 macro_build ((char *) NULL, &icnt, &offset_expr,
4d34fb5f 4824 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
9117d219 4825 "t,o(b)", tempreg, lw_reloc_type, tempreg);
252b5132
RH
4826 if (expr1.X_add_number == 0)
4827 {
4828 int off;
4829
4830 if (breg == 0)
4831 off = 0;
4832 else
4833 {
4834 /* We're going to put in an addu instruction using
4835 tempreg, so we may as well insert the nop right
4836 now. */
4837 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4838 "nop", "");
4839 off = 4;
4840 }
4841
4842 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4843 RELAX_ENCODE (12 + off, 12 + gpdel, gpdel,
4844 8 + gpdel, 0,
4845 (breg == 0
4846 ? mips_opts.warn_about_macros
4847 : 0)),
c4e7957c 4848 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
4849 }
4850 else if (expr1.X_add_number >= -0x8000
4851 && expr1.X_add_number < 0x8000)
4852 {
4853 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4854 "nop", "");
4855 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 4856 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
4857 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4858
4859 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
4860 RELAX_ENCODE (20, 12 + gpdel, gpdel, 8 + gpdel, 0,
4861 (breg == 0
4862 ? mips_opts.warn_about_macros
4863 : 0)),
c4e7957c 4864 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
4865 }
4866 else
4867 {
4868 int adj, dreg;
4869
4870 /* If we are going to add in a base register, and the
4871 target register and the base register are the same,
4872 then we are using AT as a temporary register. Since
4873 we want to load the constant into AT, we add our
4874 current AT (from the global offset table) and the
4875 register into the register now, and pretend we were
4876 not using a base register. */
4877 if (breg != treg)
4878 {
4879 adj = 0;
4880 dreg = tempreg;
4881 }
4882 else
4883 {
4884 assert (tempreg == AT);
4885 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
4886 "nop", "");
4887 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 4888 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
4889 "d,v,t", treg, AT, breg);
4890 dreg = treg;
4891 adj = 8;
4892 }
4893
4894 /* Set mips_optimize around the lui instruction to avoid
4895 inserting an unnecessary nop after the lw. */
4896 hold_mips_optimize = mips_optimize;
4897 mips_optimize = 2;
c4e7957c 4898 macro_build_lui (NULL, &icnt, &expr1, AT);
252b5132
RH
4899 mips_optimize = hold_mips_optimize;
4900
4901 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 4902 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
4903 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4904 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 4905 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
4906 "d,v,t", dreg, dreg, AT);
4907
4908 p = frag_var (rs_machine_dependent, 16 + gpdel + adj, 0,
4909 RELAX_ENCODE (24 + adj, 16 + gpdel + adj, gpdel,
4910 8 + gpdel, 0,
4911 (breg == 0
4912 ? mips_opts.warn_about_macros
4913 : 0)),
c4e7957c 4914 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
4915
4916 used_at = 1;
4917 }
4918
4919 if (gpdel > 0)
4920 {
4921 /* This is needed because this instruction uses $gp, but
4922 the first instruction on the main stream does not. */
4923 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4924 p += 4;
4925 }
4926 macro_build (p, &icnt, &offset_expr,
4d34fb5f 4927 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
c9914766
TS
4928 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16,
4929 mips_gp_register);
252b5132
RH
4930 p += 4;
4931 if (expr1.X_add_number >= -0x8000
4932 && expr1.X_add_number < 0x8000)
4933 {
4934 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4935 p += 4;
4936 macro_build (p, &icnt, &expr1,
ca4e0257 4937 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
4938 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
4939 /* FIXME: If add_number is 0, and there was no base
4940 register, the external symbol case ended with a load,
4941 so if the symbol turns out to not be external, and
4942 the next instruction uses tempreg, an unnecessary nop
4943 will be inserted. */
4944 }
4945 else
4946 {
4947 if (breg == treg)
4948 {
4949 /* We must add in the base register now, as in the
4950 external symbol case. */
4951 assert (tempreg == AT);
4952 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
4953 p += 4;
4954 macro_build (p, &icnt, (expressionS *) NULL,
ca4e0257 4955 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
4956 "d,v,t", treg, AT, breg);
4957 p += 4;
4958 tempreg = treg;
4959 /* We set breg to 0 because we have arranged to add
4960 it in in both cases. */
4961 breg = 0;
4962 }
4963
4964 macro_build_lui (p, &icnt, &expr1, AT);
4965 p += 4;
4966 macro_build (p, &icnt, &expr1,
ca4e0257 4967 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
4968 "t,r,j", AT, AT, (int) BFD_RELOC_LO16);
4969 p += 4;
4970 macro_build (p, &icnt, (expressionS *) NULL,
ca4e0257 4971 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
4972 "d,v,t", tempreg, tempreg, AT);
4973 p += 4;
4974 }
4975 }
4976 else if (mips_pic == EMBEDDED_PIC)
4977 {
4978 /* We use
cdf6fd85 4979 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
4980 */
4981 macro_build ((char *) NULL, &icnt, &offset_expr,
c9914766
TS
4982 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j",
4983 tempreg, mips_gp_register, (int) BFD_RELOC_GPREL16);
252b5132
RH
4984 }
4985 else
4986 abort ();
4987
4988 if (breg != 0)
4d34fb5f
TS
4989 {
4990 char *s;
4991
4992 if (mips_pic == EMBEDDED_PIC || mips_pic == NO_PIC)
4993 s = (dbl || HAVE_64BIT_ADDRESSES) ? "daddu" : "addu";
4994 else
4995 s = HAVE_64BIT_ADDRESSES ? "daddu" : "addu";
4996
4997 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s,
4998 "d,v,t", treg, tempreg, breg);
4999 }
252b5132
RH
5000
5001 if (! used_at)
5002 return;
5003
5004 break;
5005
5006 case M_J_A:
5007 /* The j instruction may not be used in PIC code, since it
5008 requires an absolute address. We convert it to a b
5009 instruction. */
5010 if (mips_pic == NO_PIC)
5011 macro_build ((char *) NULL, &icnt, &offset_expr, "j", "a");
5012 else
5013 macro_build ((char *) NULL, &icnt, &offset_expr, "b", "p");
5014 return;
5015
5016 /* The jal instructions must be handled as macros because when
5017 generating PIC code they expand to multi-instruction
5018 sequences. Normally they are simple instructions. */
5019 case M_JAL_1:
5020 dreg = RA;
5021 /* Fall through. */
5022 case M_JAL_2:
5023 if (mips_pic == NO_PIC
5024 || mips_pic == EMBEDDED_PIC)
5025 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5026 "d,s", dreg, sreg);
5027 else if (mips_pic == SVR4_PIC)
5028 {
5029 if (sreg != PIC_CALL_REG)
5030 as_warn (_("MIPS PIC call to register other than $25"));
bdaaa2e1 5031
252b5132
RH
5032 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "jalr",
5033 "d,s", dreg, sreg);
6478892d 5034 if (! HAVE_NEWABI)
252b5132 5035 {
6478892d
TS
5036 if (mips_cprestore_offset < 0)
5037 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5038 else
5039 {
7a621144
DJ
5040 if (! mips_frame_reg_valid)
5041 {
5042 as_warn (_("No .frame pseudo-op used in PIC code"));
5043 /* Quiet this warning. */
5044 mips_frame_reg_valid = 1;
5045 }
5046 if (! mips_cprestore_valid)
5047 {
5048 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5049 /* Quiet this warning. */
5050 mips_cprestore_valid = 1;
5051 }
6478892d
TS
5052 expr1.X_add_number = mips_cprestore_offset;
5053 macro_build ((char *) NULL, &icnt, &expr1,
5054 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
c9914766
TS
5055 mips_gp_register, (int) BFD_RELOC_LO16,
5056 mips_frame_reg);
6478892d 5057 }
252b5132
RH
5058 }
5059 }
5060 else
5061 abort ();
5062
5063 return;
5064
5065 case M_JAL_A:
5066 if (mips_pic == NO_PIC)
5067 macro_build ((char *) NULL, &icnt, &offset_expr, "jal", "a");
5068 else if (mips_pic == SVR4_PIC)
5069 {
f9419b05
TS
5070 char *p;
5071
252b5132
RH
5072 /* If this is a reference to an external symbol, and we are
5073 using a small GOT, we want
5074 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
5075 nop
f9419b05 5076 jalr $ra,$25
252b5132
RH
5077 nop
5078 lw $gp,cprestore($sp)
5079 The cprestore value is set using the .cprestore
5080 pseudo-op. If we are using a big GOT, we want
5081 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
5082 addu $25,$25,$gp
5083 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
5084 nop
f9419b05 5085 jalr $ra,$25
252b5132
RH
5086 nop
5087 lw $gp,cprestore($sp)
5088 If the symbol is not external, we want
5089 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5090 nop
5091 addiu $25,$25,<sym> (BFD_RELOC_LO16)
f9419b05 5092 jalr $ra,$25
252b5132 5093 nop
438c16b8
TS
5094 lw $gp,cprestore($sp)
5095 For NewABI, we want
5096 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
5097 jalr $ra,$25 (BFD_RELOC_MIPS_JALR)
5098 */
5099 if (HAVE_NEWABI)
252b5132
RH
5100 {
5101 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 5102 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132 5103 "t,o(b)", PIC_CALL_REG,
438c16b8
TS
5104 (int) BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
5105 macro_build_jalr (icnt, &offset_expr);
252b5132
RH
5106 }
5107 else
5108 {
438c16b8
TS
5109 frag_grow (40);
5110 if (! mips_big_got)
5111 {
5112 macro_build ((char *) NULL, &icnt, &offset_expr,
5113 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5114 "t,o(b)", PIC_CALL_REG,
5115 (int) BFD_RELOC_MIPS_CALL16, mips_gp_register);
5116 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5117 "nop", "");
5118 p = frag_var (rs_machine_dependent, 4, 0,
5119 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
5120 offset_expr.X_add_symbol, 0, NULL);
5121 }
252b5132 5122 else
252b5132 5123 {
438c16b8
TS
5124 int gpdel;
5125
5126 if (reg_needs_delay (mips_gp_register))
5127 gpdel = 4;
5128 else
5129 gpdel = 0;
5130 macro_build ((char *) NULL, &icnt, &offset_expr, "lui",
5131 "t,u", PIC_CALL_REG,
5132 (int) BFD_RELOC_MIPS_CALL_HI16);
5133 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5134 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5135 "d,v,t", PIC_CALL_REG, PIC_CALL_REG,
5136 mips_gp_register);
5137 macro_build ((char *) NULL, &icnt, &offset_expr,
5138 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5139 "t,o(b)", PIC_CALL_REG,
5140 (int) BFD_RELOC_MIPS_CALL_LO16, PIC_CALL_REG);
5141 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5142 "nop", "");
5143 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5144 RELAX_ENCODE (16, 12 + gpdel, gpdel,
5145 8 + gpdel, 0, 0),
5146 offset_expr.X_add_symbol, 0, NULL);
5147 if (gpdel > 0)
5148 {
5149 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5150 p += 4;
5151 }
5152 macro_build (p, &icnt, &offset_expr,
5153 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5154 "t,o(b)", PIC_CALL_REG,
5155 (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
5156 p += 4;
252b5132
RH
5157 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5158 p += 4;
5159 }
5160 macro_build (p, &icnt, &offset_expr,
438c16b8
TS
5161 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5162 "t,r,j", PIC_CALL_REG, PIC_CALL_REG,
5163 (int) BFD_RELOC_LO16);
5164 macro_build_jalr (icnt, &offset_expr);
5165
6478892d
TS
5166 if (mips_cprestore_offset < 0)
5167 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5168 else
5169 {
7a621144
DJ
5170 if (! mips_frame_reg_valid)
5171 {
5172 as_warn (_("No .frame pseudo-op used in PIC code"));
5173 /* Quiet this warning. */
5174 mips_frame_reg_valid = 1;
5175 }
5176 if (! mips_cprestore_valid)
5177 {
5178 as_warn (_("No .cprestore pseudo-op used in PIC code"));
5179 /* Quiet this warning. */
5180 mips_cprestore_valid = 1;
5181 }
6478892d
TS
5182 if (mips_opts.noreorder)
5183 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
98d3f06f 5184 "nop", "");
6478892d
TS
5185 expr1.X_add_number = mips_cprestore_offset;
5186 macro_build ((char *) NULL, &icnt, &expr1,
5187 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)",
c9914766
TS
5188 mips_gp_register, (int) BFD_RELOC_LO16,
5189 mips_frame_reg);
6478892d 5190 }
252b5132
RH
5191 }
5192 }
5193 else if (mips_pic == EMBEDDED_PIC)
5194 {
5195 macro_build ((char *) NULL, &icnt, &offset_expr, "bal", "p");
5196 /* The linker may expand the call to a longer sequence which
5197 uses $at, so we must break rather than return. */
5198 break;
5199 }
5200 else
5201 abort ();
5202
5203 return;
5204
5205 case M_LB_AB:
5206 s = "lb";
5207 goto ld;
5208 case M_LBU_AB:
5209 s = "lbu";
5210 goto ld;
5211 case M_LH_AB:
5212 s = "lh";
5213 goto ld;
5214 case M_LHU_AB:
5215 s = "lhu";
5216 goto ld;
5217 case M_LW_AB:
5218 s = "lw";
5219 goto ld;
5220 case M_LWC0_AB:
5221 s = "lwc0";
bdaaa2e1 5222 /* Itbl support may require additional care here. */
252b5132
RH
5223 coproc = 1;
5224 goto ld;
5225 case M_LWC1_AB:
5226 s = "lwc1";
bdaaa2e1 5227 /* Itbl support may require additional care here. */
252b5132
RH
5228 coproc = 1;
5229 goto ld;
5230 case M_LWC2_AB:
5231 s = "lwc2";
bdaaa2e1 5232 /* Itbl support may require additional care here. */
252b5132
RH
5233 coproc = 1;
5234 goto ld;
5235 case M_LWC3_AB:
5236 s = "lwc3";
bdaaa2e1 5237 /* Itbl support may require additional care here. */
252b5132
RH
5238 coproc = 1;
5239 goto ld;
5240 case M_LWL_AB:
5241 s = "lwl";
5242 lr = 1;
5243 goto ld;
5244 case M_LWR_AB:
5245 s = "lwr";
5246 lr = 1;
5247 goto ld;
5248 case M_LDC1_AB:
ec68c924 5249 if (mips_arch == CPU_R4650)
252b5132
RH
5250 {
5251 as_bad (_("opcode not supported on this processor"));
5252 return;
5253 }
5254 s = "ldc1";
bdaaa2e1 5255 /* Itbl support may require additional care here. */
252b5132
RH
5256 coproc = 1;
5257 goto ld;
5258 case M_LDC2_AB:
5259 s = "ldc2";
bdaaa2e1 5260 /* Itbl support may require additional care here. */
252b5132
RH
5261 coproc = 1;
5262 goto ld;
5263 case M_LDC3_AB:
5264 s = "ldc3";
bdaaa2e1 5265 /* Itbl support may require additional care here. */
252b5132
RH
5266 coproc = 1;
5267 goto ld;
5268 case M_LDL_AB:
5269 s = "ldl";
5270 lr = 1;
5271 goto ld;
5272 case M_LDR_AB:
5273 s = "ldr";
5274 lr = 1;
5275 goto ld;
5276 case M_LL_AB:
5277 s = "ll";
5278 goto ld;
5279 case M_LLD_AB:
5280 s = "lld";
5281 goto ld;
5282 case M_LWU_AB:
5283 s = "lwu";
5284 ld:
5285 if (breg == treg || coproc || lr)
5286 {
5287 tempreg = AT;
5288 used_at = 1;
5289 }
5290 else
5291 {
5292 tempreg = treg;
5293 used_at = 0;
5294 }
5295 goto ld_st;
5296 case M_SB_AB:
5297 s = "sb";
5298 goto st;
5299 case M_SH_AB:
5300 s = "sh";
5301 goto st;
5302 case M_SW_AB:
5303 s = "sw";
5304 goto st;
5305 case M_SWC0_AB:
5306 s = "swc0";
bdaaa2e1 5307 /* Itbl support may require additional care here. */
252b5132
RH
5308 coproc = 1;
5309 goto st;
5310 case M_SWC1_AB:
5311 s = "swc1";
bdaaa2e1 5312 /* Itbl support may require additional care here. */
252b5132
RH
5313 coproc = 1;
5314 goto st;
5315 case M_SWC2_AB:
5316 s = "swc2";
bdaaa2e1 5317 /* Itbl support may require additional care here. */
252b5132
RH
5318 coproc = 1;
5319 goto st;
5320 case M_SWC3_AB:
5321 s = "swc3";
bdaaa2e1 5322 /* Itbl support may require additional care here. */
252b5132
RH
5323 coproc = 1;
5324 goto st;
5325 case M_SWL_AB:
5326 s = "swl";
5327 goto st;
5328 case M_SWR_AB:
5329 s = "swr";
5330 goto st;
5331 case M_SC_AB:
5332 s = "sc";
5333 goto st;
5334 case M_SCD_AB:
5335 s = "scd";
5336 goto st;
5337 case M_SDC1_AB:
ec68c924 5338 if (mips_arch == CPU_R4650)
252b5132
RH
5339 {
5340 as_bad (_("opcode not supported on this processor"));
5341 return;
5342 }
5343 s = "sdc1";
5344 coproc = 1;
bdaaa2e1 5345 /* Itbl support may require additional care here. */
252b5132
RH
5346 goto st;
5347 case M_SDC2_AB:
5348 s = "sdc2";
bdaaa2e1 5349 /* Itbl support may require additional care here. */
252b5132
RH
5350 coproc = 1;
5351 goto st;
5352 case M_SDC3_AB:
5353 s = "sdc3";
bdaaa2e1 5354 /* Itbl support may require additional care here. */
252b5132
RH
5355 coproc = 1;
5356 goto st;
5357 case M_SDL_AB:
5358 s = "sdl";
5359 goto st;
5360 case M_SDR_AB:
5361 s = "sdr";
5362 st:
5363 tempreg = AT;
5364 used_at = 1;
5365 ld_st:
bdaaa2e1 5366 /* Itbl support may require additional care here. */
252b5132
RH
5367 if (mask == M_LWC1_AB
5368 || mask == M_SWC1_AB
5369 || mask == M_LDC1_AB
5370 || mask == M_SDC1_AB
5371 || mask == M_L_DAB
5372 || mask == M_S_DAB)
5373 fmt = "T,o(b)";
5374 else if (coproc)
5375 fmt = "E,o(b)";
5376 else
5377 fmt = "t,o(b)";
5378
afdbd6d0
CD
5379 /* For embedded PIC, we allow loads where the offset is calculated
5380 by subtracting a symbol in the current segment from an unknown
5381 symbol, relative to a base register, e.g.:
5382 <op> $treg, <sym>-<localsym>($breg)
5383 This is used by the compiler for switch statements. */
76b3015f 5384 if (mips_pic == EMBEDDED_PIC
afdbd6d0
CD
5385 && offset_expr.X_op == O_subtract
5386 && (symbol_constant_p (offset_expr.X_op_symbol)
5387 ? S_GET_SEGMENT (offset_expr.X_op_symbol) == now_seg
5388 : (symbol_equated_p (offset_expr.X_op_symbol)
5389 && (S_GET_SEGMENT
5390 (symbol_get_value_expression (offset_expr.X_op_symbol)
5391 ->X_add_symbol)
5392 == now_seg)))
5393 && breg != 0
5394 && (offset_expr.X_add_number == 0
5395 || OUTPUT_FLAVOR == bfd_target_elf_flavour))
5396 {
5397 /* For this case, we output the instructions:
5398 lui $tempreg,<sym> (BFD_RELOC_PCREL_HI16_S)
5399 addiu $tempreg,$tempreg,$breg
5400 <op> $treg,<sym>($tempreg) (BFD_RELOC_PCREL_LO16)
5401 If the relocation would fit entirely in 16 bits, it would be
5402 nice to emit:
5403 <op> $treg,<sym>($breg) (BFD_RELOC_PCREL_LO16)
5404 instead, but that seems quite difficult. */
5405 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5406 tempreg, (int) BFD_RELOC_PCREL_HI16_S);
5407 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5408 ((bfd_arch_bits_per_address (stdoutput) == 32
5409 || ! ISA_HAS_64BIT_REGS (mips_opts.isa))
5410 ? "addu" : "daddu"),
5411 "d,v,t", tempreg, tempreg, breg);
5412 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt, treg,
5413 (int) BFD_RELOC_PCREL_LO16, tempreg);
5414 if (! used_at)
5415 return;
5416 break;
5417 }
5418
252b5132
RH
5419 if (offset_expr.X_op != O_constant
5420 && offset_expr.X_op != O_symbol)
5421 {
5422 as_bad (_("expression too complex"));
5423 offset_expr.X_op = O_constant;
5424 }
5425
5426 /* A constant expression in PIC code can be handled just as it
5427 is in non PIC code. */
5428 if (mips_pic == NO_PIC
5429 || offset_expr.X_op == O_constant)
5430 {
f9419b05
TS
5431 char *p;
5432
252b5132
RH
5433 /* If this is a reference to a GP relative symbol, and there
5434 is no base register, we want
cdf6fd85 5435 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
5436 Otherwise, if there is no base register, we want
5437 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5438 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5439 If we have a constant, we need two instructions anyhow,
5440 so we always use the latter form.
5441
5442 If we have a base register, and this is a reference to a
5443 GP relative symbol, we want
5444 addu $tempreg,$breg,$gp
cdf6fd85 5445 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
5446 Otherwise we want
5447 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
5448 addu $tempreg,$tempreg,$breg
5449 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245 5450 With a constant we always use the latter case.
76b3015f 5451
d6bc6245
TS
5452 With 64bit address space and no base register and $at usable,
5453 we want
5454 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5455 lui $at,<sym> (BFD_RELOC_HI16_S)
5456 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5457 dsll32 $tempreg,0
5458 daddu $tempreg,$at
5459 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5460 If we have a base register, we want
5461 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5462 lui $at,<sym> (BFD_RELOC_HI16_S)
5463 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5464 daddu $at,$breg
5465 dsll32 $tempreg,0
5466 daddu $tempreg,$at
5467 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5468
5469 Without $at we can't generate the optimal path for superscalar
5470 processors here since this would require two temporary registers.
5471 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5472 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5473 dsll $tempreg,16
5474 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5475 dsll $tempreg,16
5476 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
5477 If we have a base register, we want
5478 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
5479 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
5480 dsll $tempreg,16
5481 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
5482 dsll $tempreg,16
5483 daddu $tempreg,$tempreg,$breg
5484 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6373ee54
CD
5485
5486 If we have 64-bit addresses, as an optimization, for
5487 addresses which are 32-bit constants (e.g. kseg0/kseg1
5488 addresses) we fall back to the 32-bit address generation
78d32a17
MR
5489 mechanism since it is more efficient. Note that due to
5490 the signed offset used by memory operations, the 32-bit
5491 range is shifted down by 32768 here. This code should
6373ee54
CD
5492 probably attempt to generate 64-bit constants more
5493 efficiently in general.
d6bc6245 5494 */
6373ee54
CD
5495 if (HAVE_64BIT_ADDRESSES
5496 && !(offset_expr.X_op == O_constant
78d32a17 5497 && IS_SEXT_32BIT_NUM (offset_expr.X_add_number + 0x8000)))
d6bc6245
TS
5498 {
5499 p = NULL;
5500
5501 /* We don't do GP optimization for now because RELAX_ENCODE can't
5502 hold the data for such large chunks. */
5503
460597ba 5504 if (used_at == 0 && ! mips_opts.noat)
d6bc6245
TS
5505 {
5506 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5507 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5508 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5509 AT, (int) BFD_RELOC_HI16_S);
5510 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5511 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
5512 if (breg != 0)
2396cfb9
TS
5513 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5514 "d,v,t", AT, AT, breg);
5515 macro_build (p, &icnt, (expressionS *) NULL, "dsll32",
5516 "d,w,<", tempreg, tempreg, 0);
5517 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5518 "d,v,t", tempreg, tempreg, AT);
d6bc6245
TS
5519 macro_build (p, &icnt, &offset_expr, s,
5520 fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5521 used_at = 1;
5522 }
5523 else
5524 {
5525 macro_build (p, &icnt, &offset_expr, "lui", "t,u",
5526 tempreg, (int) BFD_RELOC_MIPS_HIGHEST);
5527 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5528 tempreg, tempreg, (int) BFD_RELOC_MIPS_HIGHER);
2396cfb9
TS
5529 macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5530 "d,w,<", tempreg, tempreg, 16);
d6bc6245
TS
5531 macro_build (p, &icnt, &offset_expr, "daddiu", "t,r,j",
5532 tempreg, tempreg, (int) BFD_RELOC_HI16_S);
2396cfb9
TS
5533 macro_build (p, &icnt, (expressionS *) NULL, "dsll",
5534 "d,w,<", tempreg, tempreg, 16);
d6bc6245 5535 if (breg != 0)
2396cfb9
TS
5536 macro_build (p, &icnt, (expressionS *) NULL, "daddu",
5537 "d,v,t", tempreg, tempreg, breg);
d6bc6245
TS
5538 macro_build (p, &icnt, &offset_expr, s,
5539 fmt, treg, (int) BFD_RELOC_LO16, tempreg);
5540 }
5541
5542 return;
5543 }
76b3015f 5544
252b5132
RH
5545 if (breg == 0)
5546 {
e7d556df 5547 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
252b5132
RH
5548 || nopic_need_relax (offset_expr.X_add_symbol, 1))
5549 p = NULL;
5550 else
5551 {
5552 frag_grow (20);
5553 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
c9914766
TS
5554 treg, (int) BFD_RELOC_GPREL16,
5555 mips_gp_register);
252b5132
RH
5556 p = frag_var (rs_machine_dependent, 8, 0,
5557 RELAX_ENCODE (4, 8, 0, 4, 0,
5558 (mips_opts.warn_about_macros
5559 || (used_at
5560 && mips_opts.noat))),
956cd1d6 5561 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5562 used_at = 0;
5563 }
5564 macro_build_lui (p, &icnt, &offset_expr, tempreg);
5565 if (p != NULL)
5566 p += 4;
5567 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5568 (int) BFD_RELOC_LO16, tempreg);
5569 }
5570 else
5571 {
e7d556df 5572 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
252b5132
RH
5573 || nopic_need_relax (offset_expr.X_add_symbol, 1))
5574 p = NULL;
5575 else
5576 {
5577 frag_grow (28);
5578 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5579 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 5580 "d,v,t", tempreg, breg, mips_gp_register);
252b5132 5581 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
cdf6fd85 5582 treg, (int) BFD_RELOC_GPREL16, tempreg);
252b5132
RH
5583 p = frag_var (rs_machine_dependent, 12, 0,
5584 RELAX_ENCODE (8, 12, 0, 8, 0, 0),
956cd1d6 5585 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5586 }
5587 macro_build_lui (p, &icnt, &offset_expr, tempreg);
5588 if (p != NULL)
5589 p += 4;
5590 macro_build (p, &icnt, (expressionS *) NULL,
ca4e0257 5591 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5592 "d,v,t", tempreg, tempreg, breg);
5593 if (p != NULL)
5594 p += 4;
5595 macro_build (p, &icnt, &offset_expr, s, fmt, treg,
5596 (int) BFD_RELOC_LO16, tempreg);
5597 }
5598 }
5599 else if (mips_pic == SVR4_PIC && ! mips_big_got)
5600 {
f9419b05
TS
5601 char *p;
5602
252b5132
RH
5603 /* If this is a reference to an external symbol, we want
5604 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5605 nop
5606 <op> $treg,0($tempreg)
5607 Otherwise we want
5608 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5609 nop
5610 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5611 <op> $treg,0($tempreg)
5612 If there is a base register, we add it to $tempreg before
5613 the <op>. If there is a constant, we stick it in the
5614 <op> instruction. We don't handle constants larger than
5615 16 bits, because we have no way to load the upper 16 bits
5616 (actually, we could handle them for the subset of cases
5617 in which we are not using $at). */
5618 assert (offset_expr.X_op == O_symbol);
5619 expr1.X_add_number = offset_expr.X_add_number;
5620 offset_expr.X_add_number = 0;
5621 if (expr1.X_add_number < -0x8000
5622 || expr1.X_add_number >= 0x8000)
5623 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
5624 frag_grow (20);
5625 macro_build ((char *) NULL, &icnt, &offset_expr,
c9914766
TS
5626 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", tempreg,
5627 (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132 5628 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
bdaaa2e1 5629 p = frag_var (rs_machine_dependent, 4, 0,
252b5132 5630 RELAX_ENCODE (0, 4, -8, 0, 0, 0),
c4e7957c 5631 offset_expr.X_add_symbol, 0, NULL);
252b5132 5632 macro_build (p, &icnt, &offset_expr,
ca4e0257 5633 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
5634 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5635 if (breg != 0)
5636 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5637 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5638 "d,v,t", tempreg, tempreg, breg);
5639 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5640 (int) BFD_RELOC_LO16, tempreg);
5641 }
5642 else if (mips_pic == SVR4_PIC)
5643 {
5644 int gpdel;
f9419b05 5645 char *p;
252b5132
RH
5646
5647 /* If this is a reference to an external symbol, we want
5648 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
5649 addu $tempreg,$tempreg,$gp
5650 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
5651 <op> $treg,0($tempreg)
5652 Otherwise we want
5653 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
5654 nop
5655 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
5656 <op> $treg,0($tempreg)
5657 If there is a base register, we add it to $tempreg before
5658 the <op>. If there is a constant, we stick it in the
5659 <op> instruction. We don't handle constants larger than
5660 16 bits, because we have no way to load the upper 16 bits
5661 (actually, we could handle them for the subset of cases
438c16b8
TS
5662 in which we are not using $at).
5663
5664 For NewABI, we want
5665 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
5666 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
5667 <op> $treg,0($tempreg)
5668 */
252b5132
RH
5669 assert (offset_expr.X_op == O_symbol);
5670 expr1.X_add_number = offset_expr.X_add_number;
5671 offset_expr.X_add_number = 0;
5672 if (expr1.X_add_number < -0x8000
5673 || expr1.X_add_number >= 0x8000)
5674 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
438c16b8
TS
5675 if (HAVE_NEWABI)
5676 {
5677 macro_build ((char *) NULL, &icnt, &offset_expr,
5678 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
5679 "t,o(b)", tempreg, BFD_RELOC_MIPS_GOT_PAGE,
5680 mips_gp_register);
5681 macro_build ((char *) NULL, &icnt, &offset_expr,
5682 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
5683 "t,r,j", tempreg, tempreg,
5684 BFD_RELOC_MIPS_GOT_OFST);
5685 if (breg != 0)
5686 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5687 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
5688 "d,v,t", tempreg, tempreg, breg);
5689 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5690 (int) BFD_RELOC_LO16, tempreg);
5691
5692 if (! used_at)
5693 return;
5694
5695 break;
5696 }
f7ea7ef2 5697 if (reg_needs_delay (mips_gp_register))
252b5132
RH
5698 gpdel = 4;
5699 else
5700 gpdel = 0;
5701 frag_grow (36);
5702 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
5703 tempreg, (int) BFD_RELOC_MIPS_GOT_HI16);
5704 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5705 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 5706 "d,v,t", tempreg, tempreg, mips_gp_register);
252b5132 5707 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 5708 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
5709 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT_LO16,
5710 tempreg);
5711 p = frag_var (rs_machine_dependent, 12 + gpdel, 0,
5712 RELAX_ENCODE (12, 12 + gpdel, gpdel, 8 + gpdel, 0, 0),
c4e7957c 5713 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
5714 if (gpdel > 0)
5715 {
5716 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5717 p += 4;
5718 }
5719 macro_build (p, &icnt, &offset_expr,
ca4e0257 5720 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
c9914766
TS
5721 "t,o(b)", tempreg, (int) BFD_RELOC_MIPS_GOT16,
5722 mips_gp_register);
252b5132
RH
5723 p += 4;
5724 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
5725 p += 4;
5726 macro_build (p, &icnt, &offset_expr,
ca4e0257 5727 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu",
252b5132
RH
5728 "t,r,j", tempreg, tempreg, (int) BFD_RELOC_LO16);
5729 if (breg != 0)
5730 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5731 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
5732 "d,v,t", tempreg, tempreg, breg);
5733 macro_build ((char *) NULL, &icnt, &expr1, s, fmt, treg,
5734 (int) BFD_RELOC_LO16, tempreg);
5735 }
5736 else if (mips_pic == EMBEDDED_PIC)
5737 {
5738 /* If there is no base register, we want
cdf6fd85 5739 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
5740 If there is a base register, we want
5741 addu $tempreg,$breg,$gp
cdf6fd85 5742 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
5743 */
5744 assert (offset_expr.X_op == O_symbol);
5745 if (breg == 0)
5746 {
5747 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
c9914766 5748 treg, (int) BFD_RELOC_GPREL16, mips_gp_register);
252b5132
RH
5749 used_at = 0;
5750 }
5751 else
5752 {
5753 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 5754 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 5755 "d,v,t", tempreg, breg, mips_gp_register);
252b5132 5756 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
cdf6fd85 5757 treg, (int) BFD_RELOC_GPREL16, tempreg);
252b5132
RH
5758 }
5759 }
5760 else
5761 abort ();
5762
5763 if (! used_at)
5764 return;
5765
5766 break;
5767
5768 case M_LI:
5769 case M_LI_S:
5770 load_register (&icnt, treg, &imm_expr, 0);
5771 return;
5772
5773 case M_DLI:
5774 load_register (&icnt, treg, &imm_expr, 1);
5775 return;
5776
5777 case M_LI_SS:
5778 if (imm_expr.X_op == O_constant)
5779 {
5780 load_register (&icnt, AT, &imm_expr, 0);
5781 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5782 "mtc1", "t,G", AT, treg);
5783 break;
5784 }
5785 else
5786 {
5787 assert (offset_expr.X_op == O_symbol
5788 && strcmp (segment_name (S_GET_SEGMENT
5789 (offset_expr.X_add_symbol)),
5790 ".lit4") == 0
5791 && offset_expr.X_add_number == 0);
5792 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
c9914766 5793 treg, (int) BFD_RELOC_MIPS_LITERAL, mips_gp_register);
252b5132
RH
5794 return;
5795 }
5796
5797 case M_LI_D:
ca4e0257
RS
5798 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
5799 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
5800 order 32 bits of the value and the low order 32 bits are either
5801 zero or in OFFSET_EXPR. */
252b5132
RH
5802 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
5803 {
ca4e0257 5804 if (HAVE_64BIT_GPRS)
252b5132
RH
5805 load_register (&icnt, treg, &imm_expr, 1);
5806 else
5807 {
5808 int hreg, lreg;
5809
5810 if (target_big_endian)
5811 {
5812 hreg = treg;
5813 lreg = treg + 1;
5814 }
5815 else
5816 {
5817 hreg = treg + 1;
5818 lreg = treg;
5819 }
5820
5821 if (hreg <= 31)
5822 load_register (&icnt, hreg, &imm_expr, 0);
5823 if (lreg <= 31)
5824 {
5825 if (offset_expr.X_op == O_absent)
ea1fb5dc 5826 move_register (&icnt, lreg, 0);
252b5132
RH
5827 else
5828 {
5829 assert (offset_expr.X_op == O_constant);
5830 load_register (&icnt, lreg, &offset_expr, 0);
5831 }
5832 }
5833 }
5834 return;
5835 }
5836
5837 /* We know that sym is in the .rdata section. First we get the
5838 upper 16 bits of the address. */
5839 if (mips_pic == NO_PIC)
5840 {
956cd1d6 5841 macro_build_lui (NULL, &icnt, &offset_expr, AT);
252b5132
RH
5842 }
5843 else if (mips_pic == SVR4_PIC)
5844 {
5845 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 5846 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
c9914766
TS
5847 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
5848 mips_gp_register);
252b5132
RH
5849 }
5850 else if (mips_pic == EMBEDDED_PIC)
5851 {
5852 /* For embedded PIC we pick up the entire address off $gp in
5853 a single instruction. */
5854 macro_build ((char *) NULL, &icnt, &offset_expr,
c9914766
TS
5855 HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu", "t,r,j", AT,
5856 mips_gp_register, (int) BFD_RELOC_GPREL16);
252b5132
RH
5857 offset_expr.X_op = O_constant;
5858 offset_expr.X_add_number = 0;
5859 }
5860 else
5861 abort ();
bdaaa2e1 5862
252b5132 5863 /* Now we load the register(s). */
ca4e0257 5864 if (HAVE_64BIT_GPRS)
252b5132
RH
5865 macro_build ((char *) NULL, &icnt, &offset_expr, "ld", "t,o(b)",
5866 treg, (int) BFD_RELOC_LO16, AT);
5867 else
5868 {
5869 macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
5870 treg, (int) BFD_RELOC_LO16, AT);
f9419b05 5871 if (treg != RA)
252b5132
RH
5872 {
5873 /* FIXME: How in the world do we deal with the possible
5874 overflow here? */
5875 offset_expr.X_add_number += 4;
5876 macro_build ((char *) NULL, &icnt, &offset_expr, "lw", "t,o(b)",
5877 treg + 1, (int) BFD_RELOC_LO16, AT);
5878 }
5879 }
5880
5881 /* To avoid confusion in tc_gen_reloc, we must ensure that this
5882 does not become a variant frag. */
5883 frag_wane (frag_now);
5884 frag_new (0);
5885
5886 break;
5887
5888 case M_LI_DD:
ca4e0257
RS
5889 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
5890 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
5891 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
5892 the value and the low order 32 bits are either zero or in
5893 OFFSET_EXPR. */
252b5132
RH
5894 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
5895 {
ca4e0257
RS
5896 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_FPRS);
5897 if (HAVE_64BIT_FPRS)
5898 {
5899 assert (HAVE_64BIT_GPRS);
5900 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5901 "dmtc1", "t,S", AT, treg);
5902 }
252b5132
RH
5903 else
5904 {
5905 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5906 "mtc1", "t,G", AT, treg + 1);
5907 if (offset_expr.X_op == O_absent)
5908 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5909 "mtc1", "t,G", 0, treg);
5910 else
5911 {
5912 assert (offset_expr.X_op == O_constant);
5913 load_register (&icnt, AT, &offset_expr, 0);
5914 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
5915 "mtc1", "t,G", AT, treg);
5916 }
5917 }
5918 break;
5919 }
5920
5921 assert (offset_expr.X_op == O_symbol
5922 && offset_expr.X_add_number == 0);
5923 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
5924 if (strcmp (s, ".lit8") == 0)
5925 {
e7af610e 5926 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
5927 {
5928 macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
c9914766
TS
5929 "T,o(b)", treg, (int) BFD_RELOC_MIPS_LITERAL,
5930 mips_gp_register);
252b5132
RH
5931 return;
5932 }
c9914766 5933 breg = mips_gp_register;
252b5132
RH
5934 r = BFD_RELOC_MIPS_LITERAL;
5935 goto dob;
5936 }
5937 else
5938 {
5939 assert (strcmp (s, RDATA_SECTION_NAME) == 0);
5940 if (mips_pic == SVR4_PIC)
5941 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 5942 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
c9914766
TS
5943 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
5944 mips_gp_register);
252b5132
RH
5945 else
5946 {
5947 /* FIXME: This won't work for a 64 bit address. */
956cd1d6 5948 macro_build_lui (NULL, &icnt, &offset_expr, AT);
252b5132 5949 }
bdaaa2e1 5950
e7af610e 5951 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
5952 {
5953 macro_build ((char *) NULL, &icnt, &offset_expr, "ldc1",
5954 "T,o(b)", treg, (int) BFD_RELOC_LO16, AT);
5955
5956 /* To avoid confusion in tc_gen_reloc, we must ensure
5957 that this does not become a variant frag. */
5958 frag_wane (frag_now);
5959 frag_new (0);
5960
5961 break;
5962 }
5963 breg = AT;
5964 r = BFD_RELOC_LO16;
5965 goto dob;
5966 }
5967
5968 case M_L_DOB:
ec68c924 5969 if (mips_arch == CPU_R4650)
252b5132
RH
5970 {
5971 as_bad (_("opcode not supported on this processor"));
5972 return;
5973 }
5974 /* Even on a big endian machine $fn comes before $fn+1. We have
5975 to adjust when loading from memory. */
5976 r = BFD_RELOC_LO16;
5977 dob:
e7af610e 5978 assert (mips_opts.isa == ISA_MIPS1);
252b5132
RH
5979 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
5980 target_big_endian ? treg + 1 : treg,
5981 (int) r, breg);
5982 /* FIXME: A possible overflow which I don't know how to deal
5983 with. */
5984 offset_expr.X_add_number += 4;
5985 macro_build ((char *) NULL, &icnt, &offset_expr, "lwc1", "T,o(b)",
5986 target_big_endian ? treg : treg + 1,
5987 (int) r, breg);
5988
5989 /* To avoid confusion in tc_gen_reloc, we must ensure that this
5990 does not become a variant frag. */
5991 frag_wane (frag_now);
5992 frag_new (0);
5993
5994 if (breg != AT)
5995 return;
5996 break;
5997
5998 case M_L_DAB:
5999 /*
6000 * The MIPS assembler seems to check for X_add_number not
6001 * being double aligned and generating:
6002 * lui at,%hi(foo+1)
6003 * addu at,at,v1
6004 * addiu at,at,%lo(foo+1)
6005 * lwc1 f2,0(at)
6006 * lwc1 f3,4(at)
6007 * But, the resulting address is the same after relocation so why
6008 * generate the extra instruction?
6009 */
ec68c924 6010 if (mips_arch == CPU_R4650)
252b5132
RH
6011 {
6012 as_bad (_("opcode not supported on this processor"));
6013 return;
6014 }
bdaaa2e1 6015 /* Itbl support may require additional care here. */
252b5132 6016 coproc = 1;
e7af610e 6017 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
6018 {
6019 s = "ldc1";
6020 goto ld;
6021 }
6022
6023 s = "lwc1";
6024 fmt = "T,o(b)";
6025 goto ldd_std;
6026
6027 case M_S_DAB:
ec68c924 6028 if (mips_arch == CPU_R4650)
252b5132
RH
6029 {
6030 as_bad (_("opcode not supported on this processor"));
6031 return;
6032 }
6033
e7af610e 6034 if (mips_opts.isa != ISA_MIPS1)
252b5132
RH
6035 {
6036 s = "sdc1";
6037 goto st;
6038 }
6039
6040 s = "swc1";
6041 fmt = "T,o(b)";
bdaaa2e1 6042 /* Itbl support may require additional care here. */
252b5132
RH
6043 coproc = 1;
6044 goto ldd_std;
6045
6046 case M_LD_AB:
ca4e0257 6047 if (HAVE_64BIT_GPRS)
252b5132
RH
6048 {
6049 s = "ld";
6050 goto ld;
6051 }
6052
6053 s = "lw";
6054 fmt = "t,o(b)";
6055 goto ldd_std;
6056
6057 case M_SD_AB:
ca4e0257 6058 if (HAVE_64BIT_GPRS)
252b5132
RH
6059 {
6060 s = "sd";
6061 goto st;
6062 }
6063
6064 s = "sw";
6065 fmt = "t,o(b)";
6066
6067 ldd_std:
afdbd6d0
CD
6068 /* We do _not_ bother to allow embedded PIC (symbol-local_symbol)
6069 loads for the case of doing a pair of loads to simulate an 'ld'.
6070 This is not currently done by the compiler, and assembly coders
6071 writing embedded-pic code can cope. */
6072
252b5132
RH
6073 if (offset_expr.X_op != O_symbol
6074 && offset_expr.X_op != O_constant)
6075 {
6076 as_bad (_("expression too complex"));
6077 offset_expr.X_op = O_constant;
6078 }
6079
6080 /* Even on a big endian machine $fn comes before $fn+1. We have
6081 to adjust when loading from memory. We set coproc if we must
6082 load $fn+1 first. */
bdaaa2e1 6083 /* Itbl support may require additional care here. */
252b5132
RH
6084 if (! target_big_endian)
6085 coproc = 0;
6086
6087 if (mips_pic == NO_PIC
6088 || offset_expr.X_op == O_constant)
6089 {
f9419b05
TS
6090 char *p;
6091
252b5132 6092 /* If this is a reference to a GP relative symbol, we want
cdf6fd85
TS
6093 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6094 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
6095 If we have a base register, we use this
6096 addu $at,$breg,$gp
cdf6fd85
TS
6097 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6098 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
6099 If this is not a GP relative symbol, we want
6100 lui $at,<sym> (BFD_RELOC_HI16_S)
6101 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6102 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6103 If there is a base register, we add it to $at after the
6104 lui instruction. If there is a constant, we always use
6105 the last case. */
e7d556df 6106 if ((valueT) offset_expr.X_add_number > MAX_GPREL_OFFSET
252b5132
RH
6107 || nopic_need_relax (offset_expr.X_add_symbol, 1))
6108 {
6109 p = NULL;
6110 used_at = 1;
6111 }
6112 else
6113 {
6114 int off;
6115
6116 if (breg == 0)
6117 {
6118 frag_grow (28);
c9914766 6119 tempreg = mips_gp_register;
252b5132
RH
6120 off = 0;
6121 used_at = 0;
6122 }
6123 else
6124 {
6125 frag_grow (36);
6126 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6127 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 6128 "d,v,t", AT, breg, mips_gp_register);
252b5132
RH
6129 tempreg = AT;
6130 off = 4;
6131 used_at = 1;
6132 }
6133
beae10d5 6134 /* Itbl support may require additional care here. */
252b5132
RH
6135 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6136 coproc ? treg + 1 : treg,
cdf6fd85 6137 (int) BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6138 offset_expr.X_add_number += 4;
6139
6140 /* Set mips_optimize to 2 to avoid inserting an
6141 undesired nop. */
6142 hold_mips_optimize = mips_optimize;
6143 mips_optimize = 2;
beae10d5 6144 /* Itbl support may require additional care here. */
252b5132
RH
6145 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6146 coproc ? treg : treg + 1,
cdf6fd85 6147 (int) BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6148 mips_optimize = hold_mips_optimize;
6149
6150 p = frag_var (rs_machine_dependent, 12 + off, 0,
6151 RELAX_ENCODE (8 + off, 12 + off, 0, 4 + off, 1,
6152 used_at && mips_opts.noat),
956cd1d6 6153 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
6154
6155 /* We just generated two relocs. When tc_gen_reloc
6156 handles this case, it will skip the first reloc and
6157 handle the second. The second reloc already has an
6158 extra addend of 4, which we added above. We must
6159 subtract it out, and then subtract another 4 to make
6160 the first reloc come out right. The second reloc
6161 will come out right because we are going to add 4 to
6162 offset_expr when we build its instruction below.
6163
6164 If we have a symbol, then we don't want to include
6165 the offset, because it will wind up being included
6166 when we generate the reloc. */
6167
6168 if (offset_expr.X_op == O_constant)
6169 offset_expr.X_add_number -= 8;
6170 else
6171 {
6172 offset_expr.X_add_number = -4;
6173 offset_expr.X_op = O_constant;
6174 }
6175 }
6176 macro_build_lui (p, &icnt, &offset_expr, AT);
6177 if (p != NULL)
6178 p += 4;
6179 if (breg != 0)
6180 {
6181 macro_build (p, &icnt, (expressionS *) NULL,
ca4e0257 6182 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
6183 "d,v,t", AT, breg, AT);
6184 if (p != NULL)
6185 p += 4;
6186 }
beae10d5 6187 /* Itbl support may require additional care here. */
252b5132
RH
6188 macro_build (p, &icnt, &offset_expr, s, fmt,
6189 coproc ? treg + 1 : treg,
6190 (int) BFD_RELOC_LO16, AT);
6191 if (p != NULL)
6192 p += 4;
6193 /* FIXME: How do we handle overflow here? */
6194 offset_expr.X_add_number += 4;
beae10d5 6195 /* Itbl support may require additional care here. */
252b5132
RH
6196 macro_build (p, &icnt, &offset_expr, s, fmt,
6197 coproc ? treg : treg + 1,
6198 (int) BFD_RELOC_LO16, AT);
bdaaa2e1 6199 }
252b5132
RH
6200 else if (mips_pic == SVR4_PIC && ! mips_big_got)
6201 {
6202 int off;
6203
6204 /* If this is a reference to an external symbol, we want
6205 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6206 nop
6207 <op> $treg,0($at)
6208 <op> $treg+1,4($at)
6209 Otherwise we want
6210 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6211 nop
6212 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6213 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6214 If there is a base register we add it to $at before the
6215 lwc1 instructions. If there is a constant we include it
6216 in the lwc1 instructions. */
6217 used_at = 1;
6218 expr1.X_add_number = offset_expr.X_add_number;
6219 offset_expr.X_add_number = 0;
6220 if (expr1.X_add_number < -0x8000
6221 || expr1.X_add_number >= 0x8000 - 4)
6222 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6223 if (breg == 0)
6224 off = 0;
6225 else
6226 off = 4;
6227 frag_grow (24 + off);
6228 macro_build ((char *) NULL, &icnt, &offset_expr,
c9914766
TS
6229 HAVE_32BIT_ADDRESSES ? "lw" : "ld", "t,o(b)", AT,
6230 (int) BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132
RH
6231 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6232 if (breg != 0)
6233 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6234 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132 6235 "d,v,t", AT, breg, AT);
beae10d5 6236 /* Itbl support may require additional care here. */
252b5132
RH
6237 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6238 coproc ? treg + 1 : treg,
6239 (int) BFD_RELOC_LO16, AT);
6240 expr1.X_add_number += 4;
6241
6242 /* Set mips_optimize to 2 to avoid inserting an undesired
6243 nop. */
6244 hold_mips_optimize = mips_optimize;
6245 mips_optimize = 2;
beae10d5 6246 /* Itbl support may require additional care here. */
252b5132
RH
6247 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6248 coproc ? treg : treg + 1,
6249 (int) BFD_RELOC_LO16, AT);
6250 mips_optimize = hold_mips_optimize;
6251
6252 (void) frag_var (rs_machine_dependent, 0, 0,
6253 RELAX_ENCODE (0, 0, -16 - off, -8, 1, 0),
c4e7957c 6254 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
6255 }
6256 else if (mips_pic == SVR4_PIC)
6257 {
6258 int gpdel, off;
f9419b05 6259 char *p;
252b5132
RH
6260
6261 /* If this is a reference to an external symbol, we want
6262 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6263 addu $at,$at,$gp
6264 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
6265 nop
6266 <op> $treg,0($at)
6267 <op> $treg+1,4($at)
6268 Otherwise we want
6269 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6270 nop
6271 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
6272 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
6273 If there is a base register we add it to $at before the
6274 lwc1 instructions. If there is a constant we include it
6275 in the lwc1 instructions. */
6276 used_at = 1;
6277 expr1.X_add_number = offset_expr.X_add_number;
6278 offset_expr.X_add_number = 0;
6279 if (expr1.X_add_number < -0x8000
6280 || expr1.X_add_number >= 0x8000 - 4)
6281 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
c9914766 6282 if (reg_needs_delay (mips_gp_register))
252b5132
RH
6283 gpdel = 4;
6284 else
6285 gpdel = 0;
6286 if (breg == 0)
6287 off = 0;
6288 else
6289 off = 4;
6290 frag_grow (56);
6291 macro_build ((char *) NULL, &icnt, &offset_expr, "lui", "t,u",
6292 AT, (int) BFD_RELOC_MIPS_GOT_HI16);
6293 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6294 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 6295 "d,v,t", AT, AT, mips_gp_register);
252b5132 6296 macro_build ((char *) NULL, &icnt, &offset_expr,
ca4e0257 6297 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
252b5132
RH
6298 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT_LO16, AT);
6299 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
6300 if (breg != 0)
6301 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6302 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132 6303 "d,v,t", AT, breg, AT);
beae10d5 6304 /* Itbl support may require additional care here. */
252b5132
RH
6305 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6306 coproc ? treg + 1 : treg,
6307 (int) BFD_RELOC_LO16, AT);
6308 expr1.X_add_number += 4;
6309
6310 /* Set mips_optimize to 2 to avoid inserting an undesired
6311 nop. */
6312 hold_mips_optimize = mips_optimize;
6313 mips_optimize = 2;
beae10d5 6314 /* Itbl support may require additional care here. */
252b5132
RH
6315 macro_build ((char *) NULL, &icnt, &expr1, s, fmt,
6316 coproc ? treg : treg + 1,
6317 (int) BFD_RELOC_LO16, AT);
6318 mips_optimize = hold_mips_optimize;
6319 expr1.X_add_number -= 4;
6320
6321 p = frag_var (rs_machine_dependent, 16 + gpdel + off, 0,
6322 RELAX_ENCODE (24 + off, 16 + gpdel + off, gpdel,
6323 8 + gpdel + off, 1, 0),
c4e7957c 6324 offset_expr.X_add_symbol, 0, NULL);
252b5132
RH
6325 if (gpdel > 0)
6326 {
6327 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6328 p += 4;
6329 }
6330 macro_build (p, &icnt, &offset_expr,
ca4e0257 6331 HAVE_32BIT_ADDRESSES ? "lw" : "ld",
c9914766
TS
6332 "t,o(b)", AT, (int) BFD_RELOC_MIPS_GOT16,
6333 mips_gp_register);
252b5132
RH
6334 p += 4;
6335 macro_build (p, &icnt, (expressionS *) NULL, "nop", "");
6336 p += 4;
6337 if (breg != 0)
6338 {
6339 macro_build (p, &icnt, (expressionS *) NULL,
ca4e0257 6340 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
6341 "d,v,t", AT, breg, AT);
6342 p += 4;
6343 }
beae10d5 6344 /* Itbl support may require additional care here. */
252b5132
RH
6345 macro_build (p, &icnt, &expr1, s, fmt,
6346 coproc ? treg + 1 : treg,
6347 (int) BFD_RELOC_LO16, AT);
6348 p += 4;
6349 expr1.X_add_number += 4;
6350
6351 /* Set mips_optimize to 2 to avoid inserting an undesired
6352 nop. */
6353 hold_mips_optimize = mips_optimize;
6354 mips_optimize = 2;
beae10d5 6355 /* Itbl support may require additional care here. */
252b5132
RH
6356 macro_build (p, &icnt, &expr1, s, fmt,
6357 coproc ? treg : treg + 1,
6358 (int) BFD_RELOC_LO16, AT);
6359 mips_optimize = hold_mips_optimize;
6360 }
6361 else if (mips_pic == EMBEDDED_PIC)
6362 {
6363 /* If there is no base register, we use
cdf6fd85
TS
6364 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
6365 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
6366 If we have a base register, we use
6367 addu $at,$breg,$gp
cdf6fd85
TS
6368 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
6369 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
6370 */
6371 if (breg == 0)
6372 {
c9914766 6373 tempreg = mips_gp_register;
252b5132
RH
6374 used_at = 0;
6375 }
6376 else
6377 {
6378 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 6379 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 6380 "d,v,t", AT, breg, mips_gp_register);
252b5132
RH
6381 tempreg = AT;
6382 used_at = 1;
6383 }
6384
beae10d5 6385 /* Itbl support may require additional care here. */
252b5132
RH
6386 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6387 coproc ? treg + 1 : treg,
cdf6fd85 6388 (int) BFD_RELOC_GPREL16, tempreg);
252b5132 6389 offset_expr.X_add_number += 4;
beae10d5 6390 /* Itbl support may require additional care here. */
252b5132
RH
6391 macro_build ((char *) NULL, &icnt, &offset_expr, s, fmt,
6392 coproc ? treg : treg + 1,
cdf6fd85 6393 (int) BFD_RELOC_GPREL16, tempreg);
252b5132
RH
6394 }
6395 else
6396 abort ();
6397
6398 if (! used_at)
6399 return;
6400
6401 break;
6402
6403 case M_LD_OB:
6404 s = "lw";
6405 goto sd_ob;
6406 case M_SD_OB:
6407 s = "sw";
6408 sd_ob:
ca4e0257 6409 assert (HAVE_32BIT_ADDRESSES);
252b5132
RH
6410 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
6411 (int) BFD_RELOC_LO16, breg);
6412 offset_expr.X_add_number += 4;
6413 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg + 1,
6414 (int) BFD_RELOC_LO16, breg);
6415 return;
6416
6417 /* New code added to support COPZ instructions.
6418 This code builds table entries out of the macros in mip_opcodes.
6419 R4000 uses interlocks to handle coproc delays.
6420 Other chips (like the R3000) require nops to be inserted for delays.
6421
f72c8c98 6422 FIXME: Currently, we require that the user handle delays.
252b5132
RH
6423 In order to fill delay slots for non-interlocked chips,
6424 we must have a way to specify delays based on the coprocessor.
6425 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
6426 What are the side-effects of the cop instruction?
6427 What cache support might we have and what are its effects?
6428 Both coprocessor & memory require delays. how long???
bdaaa2e1 6429 What registers are read/set/modified?
252b5132
RH
6430
6431 If an itbl is provided to interpret cop instructions,
bdaaa2e1 6432 this knowledge can be encoded in the itbl spec. */
252b5132
RH
6433
6434 case M_COP0:
6435 s = "c0";
6436 goto copz;
6437 case M_COP1:
6438 s = "c1";
6439 goto copz;
6440 case M_COP2:
6441 s = "c2";
6442 goto copz;
6443 case M_COP3:
6444 s = "c3";
6445 copz:
6446 /* For now we just do C (same as Cz). The parameter will be
6447 stored in insn_opcode by mips_ip. */
6448 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "C",
6449 ip->insn_opcode);
6450 return;
6451
ea1fb5dc
RS
6452 case M_MOVE:
6453 move_register (&icnt, dreg, sreg);
6454 return;
6455
252b5132
RH
6456#ifdef LOSING_COMPILER
6457 default:
6458 /* Try and see if this is a new itbl instruction.
6459 This code builds table entries out of the macros in mip_opcodes.
6460 FIXME: For now we just assemble the expression and pass it's
6461 value along as a 32-bit immediate.
bdaaa2e1 6462 We may want to have the assembler assemble this value,
252b5132
RH
6463 so that we gain the assembler's knowledge of delay slots,
6464 symbols, etc.
6465 Would it be more efficient to use mask (id) here? */
bdaaa2e1 6466 if (itbl_have_entries
252b5132 6467 && (immed_expr = itbl_assemble (ip->insn_mo->name, "")))
beae10d5 6468 {
252b5132
RH
6469 s = ip->insn_mo->name;
6470 s2 = "cop3";
6471 coproc = ITBL_DECODE_PNUM (immed_expr);;
6472 macro_build ((char *) NULL, &icnt, &immed_expr, s, "C");
6473 return;
beae10d5 6474 }
252b5132
RH
6475 macro2 (ip);
6476 return;
6477 }
6478 if (mips_opts.noat)
6479 as_warn (_("Macro used $at after \".set noat\""));
6480}
bdaaa2e1 6481
252b5132
RH
6482static void
6483macro2 (ip)
6484 struct mips_cl_insn *ip;
6485{
6486 register int treg, sreg, dreg, breg;
6487 int tempreg;
6488 int mask;
6489 int icnt = 0;
6490 int used_at;
6491 expressionS expr1;
6492 const char *s;
6493 const char *s2;
6494 const char *fmt;
6495 int likely = 0;
6496 int dbl = 0;
6497 int coproc = 0;
6498 int lr = 0;
6499 int imm = 0;
6500 int off;
6501 offsetT maxnum;
6502 bfd_reloc_code_real_type r;
6503 char *p;
bdaaa2e1 6504
252b5132
RH
6505 treg = (ip->insn_opcode >> 16) & 0x1f;
6506 dreg = (ip->insn_opcode >> 11) & 0x1f;
6507 sreg = breg = (ip->insn_opcode >> 21) & 0x1f;
6508 mask = ip->insn_mo->mask;
bdaaa2e1 6509
252b5132
RH
6510 expr1.X_op = O_constant;
6511 expr1.X_op_symbol = NULL;
6512 expr1.X_add_symbol = NULL;
6513 expr1.X_add_number = 1;
bdaaa2e1 6514
252b5132
RH
6515 switch (mask)
6516 {
6517#endif /* LOSING_COMPILER */
6518
6519 case M_DMUL:
6520 dbl = 1;
6521 case M_MUL:
2396cfb9
TS
6522 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6523 dbl ? "dmultu" : "multu", "s,t", sreg, treg);
6524 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6525 dreg);
252b5132
RH
6526 return;
6527
6528 case M_DMUL_I:
6529 dbl = 1;
6530 case M_MUL_I:
6531 /* The MIPS assembler some times generates shifts and adds. I'm
6532 not trying to be that fancy. GCC should do this for us
6533 anyway. */
6534 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9 6535 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 6536 dbl ? "dmult" : "mult", "s,t", sreg, AT);
2396cfb9
TS
6537 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6538 dreg);
252b5132
RH
6539 break;
6540
6541 case M_DMULO_I:
6542 dbl = 1;
6543 case M_MULO_I:
6544 imm = 1;
6545 goto do_mulo;
6546
6547 case M_DMULO:
6548 dbl = 1;
6549 case M_MULO:
6550 do_mulo:
6551 mips_emit_delays (true);
6552 ++mips_opts.noreorder;
6553 mips_any_noreorder = 1;
6554 if (imm)
6555 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9 6556 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 6557 dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
2396cfb9
TS
6558 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6559 dreg);
6560 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
f9419b05 6561 dbl ? "dsra32" : "sra", "d,w,<", dreg, dreg, RA);
2396cfb9
TS
6562 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6563 AT);
252b5132 6564 if (mips_trap)
9bd7d936
MR
6565 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne",
6566 "s,t,q", dreg, AT, 6);
252b5132
RH
6567 else
6568 {
6569 expr1.X_add_number = 8;
2396cfb9
TS
6570 macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", dreg,
6571 AT);
6572 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6573 0);
6574 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6575 "c", 6);
252b5132
RH
6576 }
6577 --mips_opts.noreorder;
2396cfb9 6578 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d", dreg);
252b5132
RH
6579 break;
6580
6581 case M_DMULOU_I:
6582 dbl = 1;
6583 case M_MULOU_I:
6584 imm = 1;
6585 goto do_mulou;
6586
6587 case M_DMULOU:
6588 dbl = 1;
6589 case M_MULOU:
6590 do_mulou:
6591 mips_emit_delays (true);
6592 ++mips_opts.noreorder;
6593 mips_any_noreorder = 1;
6594 if (imm)
6595 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9 6596 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
252b5132
RH
6597 dbl ? "dmultu" : "multu",
6598 "s,t", sreg, imm ? AT : treg);
2396cfb9
TS
6599 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mfhi", "d",
6600 AT);
6601 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "d",
6602 dreg);
252b5132 6603 if (mips_trap)
9bd7d936
MR
6604 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "tne",
6605 "s,t,q", AT, 0, 6);
252b5132
RH
6606 else
6607 {
6608 expr1.X_add_number = 8;
6609 macro_build ((char *) NULL, &icnt, &expr1, "beq", "s,t,p", AT, 0);
2396cfb9
TS
6610 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "",
6611 0);
6612 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
6613 "c", 6);
252b5132
RH
6614 }
6615 --mips_opts.noreorder;
6616 break;
6617
771c7ce4
TS
6618 case M_DROL:
6619 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
6620 "d,v,t", AT, 0, treg);
6621 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
6622 "d,t,s", AT, sreg, AT);
6623 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
6624 "d,t,s", dreg, sreg, treg);
6625 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6626 "d,v,t", dreg, dreg, AT);
6627 break;
6628
252b5132 6629 case M_ROL:
2396cfb9
TS
6630 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
6631 "d,v,t", AT, 0, treg);
6632 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
6633 "d,t,s", AT, sreg, AT);
6634 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
6635 "d,t,s", dreg, sreg, treg);
6636 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6637 "d,v,t", dreg, dreg, AT);
252b5132
RH
6638 break;
6639
771c7ce4
TS
6640 case M_DROL_I:
6641 {
6642 unsigned int rot;
771c7ce4
TS
6643
6644 if (imm_expr.X_op != O_constant)
6645 as_bad (_("rotate count too large"));
6646 rot = imm_expr.X_add_number & 0x3f;
483fc7cd
RS
6647 if (rot == 0)
6648 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrl",
6649 "d,w,<", dreg, sreg, 0);
6650 else
6651 {
6652 char *l, *r;
6653
6654 l = (rot < 0x20) ? "dsll" : "dsll32";
6655 r = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
6656 rot &= 0x1f;
6657 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
6658 "d,w,<", AT, sreg, rot);
6659 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
6660 "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6661 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6662 "d,v,t", dreg, dreg, AT);
6663 }
771c7ce4
TS
6664 }
6665 break;
6666
252b5132 6667 case M_ROL_I:
771c7ce4
TS
6668 {
6669 unsigned int rot;
6670
6671 if (imm_expr.X_op != O_constant)
6672 as_bad (_("rotate count too large"));
6673 rot = imm_expr.X_add_number & 0x1f;
483fc7cd
RS
6674 if (rot == 0)
6675 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
6676 "d,w,<", dreg, sreg, 0);
6677 else
6678 {
6679 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
6680 "d,w,<", AT, sreg, rot);
6681 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
6682 "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6683 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6684 "d,v,t", dreg, dreg, AT);
6685 }
771c7ce4
TS
6686 }
6687 break;
6688
6689 case M_DROR:
6690 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsubu",
6691 "d,v,t", AT, 0, treg);
6692 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsllv",
6693 "d,t,s", AT, sreg, AT);
6694 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrlv",
6695 "d,t,s", dreg, sreg, treg);
6696 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6697 "d,v,t", dreg, dreg, AT);
252b5132
RH
6698 break;
6699
6700 case M_ROR:
2396cfb9
TS
6701 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "subu",
6702 "d,v,t", AT, 0, treg);
6703 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sllv",
6704 "d,t,s", AT, sreg, AT);
6705 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srlv",
6706 "d,t,s", dreg, sreg, treg);
6707 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6708 "d,v,t", dreg, dreg, AT);
252b5132
RH
6709 break;
6710
771c7ce4
TS
6711 case M_DROR_I:
6712 {
6713 unsigned int rot;
771c7ce4
TS
6714
6715 if (imm_expr.X_op != O_constant)
6716 as_bad (_("rotate count too large"));
6717 rot = imm_expr.X_add_number & 0x3f;
483fc7cd
RS
6718 if (rot == 0)
6719 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "dsrl",
6720 "d,w,<", dreg, sreg, 0);
6721 else
6722 {
6723 char *l, *r;
6724
6725 r = (rot < 0x20) ? "dsrl" : "dsrl32";
6726 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
6727 rot &= 0x1f;
6728 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, r,
6729 "d,w,<", AT, sreg, rot);
6730 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, l,
6731 "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6732 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6733 "d,v,t", dreg, dreg, AT);
6734 }
771c7ce4
TS
6735 }
6736 break;
6737
252b5132 6738 case M_ROR_I:
771c7ce4
TS
6739 {
6740 unsigned int rot;
6741
6742 if (imm_expr.X_op != O_constant)
6743 as_bad (_("rotate count too large"));
6744 rot = imm_expr.X_add_number & 0x1f;
483fc7cd
RS
6745 if (rot == 0)
6746 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
6747 "d,w,<", dreg, sreg, 0);
6748 else
6749 {
6750 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl",
6751 "d,w,<", AT, sreg, rot);
6752 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll",
6753 "d,w,<", dreg, sreg, (0x20 - rot) & 0x1f);
6754 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or",
6755 "d,v,t", dreg, dreg, AT);
6756 }
771c7ce4 6757 }
252b5132
RH
6758 break;
6759
6760 case M_S_DOB:
ec68c924 6761 if (mips_arch == CPU_R4650)
252b5132
RH
6762 {
6763 as_bad (_("opcode not supported on this processor"));
6764 return;
6765 }
e7af610e 6766 assert (mips_opts.isa == ISA_MIPS1);
252b5132
RH
6767 /* Even on a big endian machine $fn comes before $fn+1. We have
6768 to adjust when storing to memory. */
6769 macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
6770 target_big_endian ? treg + 1 : treg,
6771 (int) BFD_RELOC_LO16, breg);
6772 offset_expr.X_add_number += 4;
6773 macro_build ((char *) NULL, &icnt, &offset_expr, "swc1", "T,o(b)",
6774 target_big_endian ? treg : treg + 1,
6775 (int) BFD_RELOC_LO16, breg);
6776 return;
6777
6778 case M_SEQ:
6779 if (sreg == 0)
6780 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6781 treg, (int) BFD_RELOC_LO16);
6782 else if (treg == 0)
6783 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6784 sreg, (int) BFD_RELOC_LO16);
6785 else
6786 {
2396cfb9
TS
6787 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6788 "d,v,t", dreg, sreg, treg);
252b5132
RH
6789 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6790 dreg, (int) BFD_RELOC_LO16);
6791 }
6792 return;
6793
6794 case M_SEQ_I:
6795 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6796 {
6797 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg,
6798 sreg, (int) BFD_RELOC_LO16);
6799 return;
6800 }
6801 if (sreg == 0)
6802 {
6803 as_warn (_("Instruction %s: result is always false"),
6804 ip->insn_mo->name);
ea1fb5dc 6805 move_register (&icnt, dreg, 0);
252b5132
RH
6806 return;
6807 }
6808 if (imm_expr.X_op == O_constant
6809 && imm_expr.X_add_number >= 0
6810 && imm_expr.X_add_number < 0x10000)
6811 {
6812 macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i", dreg,
6813 sreg, (int) BFD_RELOC_LO16);
6814 used_at = 0;
6815 }
6816 else if (imm_expr.X_op == O_constant
6817 && imm_expr.X_add_number > -0x8000
6818 && imm_expr.X_add_number < 0)
6819 {
6820 imm_expr.X_add_number = -imm_expr.X_add_number;
6821 macro_build ((char *) NULL, &icnt, &imm_expr,
ca4e0257 6822 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
252b5132
RH
6823 "t,r,j", dreg, sreg,
6824 (int) BFD_RELOC_LO16);
6825 used_at = 0;
6826 }
6827 else
6828 {
4d34fb5f 6829 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
6830 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6831 "d,v,t", dreg, sreg, AT);
252b5132
RH
6832 used_at = 1;
6833 }
6834 macro_build ((char *) NULL, &icnt, &expr1, "sltiu", "t,r,j", dreg, dreg,
6835 (int) BFD_RELOC_LO16);
6836 if (used_at)
6837 break;
6838 return;
6839
6840 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
6841 s = "slt";
6842 goto sge;
6843 case M_SGEU:
6844 s = "sltu";
6845 sge:
2396cfb9
TS
6846 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6847 dreg, sreg, treg);
252b5132
RH
6848 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6849 (int) BFD_RELOC_LO16);
6850 return;
6851
6852 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
6853 case M_SGEU_I:
6854 if (imm_expr.X_op == O_constant
6855 && imm_expr.X_add_number >= -0x8000
6856 && imm_expr.X_add_number < 0x8000)
6857 {
6858 macro_build ((char *) NULL, &icnt, &imm_expr,
6859 mask == M_SGE_I ? "slti" : "sltiu",
6860 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6861 used_at = 0;
6862 }
6863 else
6864 {
4d34fb5f 6865 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
6866 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
6867 mask == M_SGE_I ? "slt" : "sltu", "d,v,t", dreg, sreg,
6868 AT);
252b5132
RH
6869 used_at = 1;
6870 }
6871 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6872 (int) BFD_RELOC_LO16);
6873 if (used_at)
6874 break;
6875 return;
6876
6877 case M_SGT: /* sreg > treg <==> treg < sreg */
6878 s = "slt";
6879 goto sgt;
6880 case M_SGTU:
6881 s = "sltu";
6882 sgt:
2396cfb9
TS
6883 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6884 dreg, treg, sreg);
252b5132
RH
6885 return;
6886
6887 case M_SGT_I: /* sreg > I <==> I < sreg */
6888 s = "slt";
6889 goto sgti;
6890 case M_SGTU_I:
6891 s = "sltu";
6892 sgti:
4d34fb5f 6893 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
6894 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6895 dreg, AT, sreg);
252b5132
RH
6896 break;
6897
2396cfb9 6898 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
252b5132
RH
6899 s = "slt";
6900 goto sle;
6901 case M_SLEU:
6902 s = "sltu";
6903 sle:
2396cfb9
TS
6904 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6905 dreg, treg, sreg);
252b5132
RH
6906 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6907 (int) BFD_RELOC_LO16);
6908 return;
6909
2396cfb9 6910 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
252b5132
RH
6911 s = "slt";
6912 goto slei;
6913 case M_SLEU_I:
6914 s = "sltu";
6915 slei:
4d34fb5f 6916 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
6917 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "d,v,t",
6918 dreg, AT, sreg);
252b5132
RH
6919 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", dreg, dreg,
6920 (int) BFD_RELOC_LO16);
6921 break;
6922
6923 case M_SLT_I:
6924 if (imm_expr.X_op == O_constant
6925 && imm_expr.X_add_number >= -0x8000
6926 && imm_expr.X_add_number < 0x8000)
6927 {
6928 macro_build ((char *) NULL, &icnt, &imm_expr, "slti", "t,r,j",
6929 dreg, sreg, (int) BFD_RELOC_LO16);
6930 return;
6931 }
4d34fb5f 6932 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
6933 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "slt", "d,v,t",
6934 dreg, sreg, AT);
252b5132
RH
6935 break;
6936
6937 case M_SLTU_I:
6938 if (imm_expr.X_op == O_constant
6939 && imm_expr.X_add_number >= -0x8000
6940 && imm_expr.X_add_number < 0x8000)
6941 {
6942 macro_build ((char *) NULL, &icnt, &imm_expr, "sltiu", "t,r,j",
6943 dreg, sreg, (int) BFD_RELOC_LO16);
6944 return;
6945 }
4d34fb5f 6946 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
6947 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6948 "d,v,t", dreg, sreg, AT);
252b5132
RH
6949 break;
6950
6951 case M_SNE:
6952 if (sreg == 0)
2396cfb9
TS
6953 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6954 "d,v,t", dreg, 0, treg);
252b5132 6955 else if (treg == 0)
2396cfb9
TS
6956 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6957 "d,v,t", dreg, 0, sreg);
252b5132
RH
6958 else
6959 {
2396cfb9
TS
6960 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
6961 "d,v,t", dreg, sreg, treg);
6962 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6963 "d,v,t", dreg, 0, dreg);
252b5132
RH
6964 }
6965 return;
6966
6967 case M_SNE_I:
6968 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6969 {
2396cfb9
TS
6970 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
6971 "d,v,t", dreg, 0, sreg);
252b5132
RH
6972 return;
6973 }
6974 if (sreg == 0)
6975 {
6976 as_warn (_("Instruction %s: result is always true"),
6977 ip->insn_mo->name);
6978 macro_build ((char *) NULL, &icnt, &expr1,
ca4e0257 6979 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
252b5132
RH
6980 "t,r,j", dreg, 0, (int) BFD_RELOC_LO16);
6981 return;
6982 }
6983 if (imm_expr.X_op == O_constant
6984 && imm_expr.X_add_number >= 0
6985 && imm_expr.X_add_number < 0x10000)
6986 {
6987 macro_build ((char *) NULL, &icnt, &imm_expr, "xori", "t,r,i",
6988 dreg, sreg, (int) BFD_RELOC_LO16);
6989 used_at = 0;
6990 }
6991 else if (imm_expr.X_op == O_constant
6992 && imm_expr.X_add_number > -0x8000
6993 && imm_expr.X_add_number < 0)
6994 {
6995 imm_expr.X_add_number = -imm_expr.X_add_number;
6996 macro_build ((char *) NULL, &icnt, &imm_expr,
ca4e0257 6997 HAVE_32BIT_GPRS ? "addiu" : "daddiu",
252b5132
RH
6998 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
6999 used_at = 0;
7000 }
7001 else
7002 {
4d34fb5f 7003 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
7004 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "xor",
7005 "d,v,t", dreg, sreg, AT);
252b5132
RH
7006 used_at = 1;
7007 }
2396cfb9
TS
7008 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sltu",
7009 "d,v,t", dreg, 0, dreg);
252b5132
RH
7010 if (used_at)
7011 break;
7012 return;
7013
7014 case M_DSUB_I:
7015 dbl = 1;
7016 case M_SUB_I:
7017 if (imm_expr.X_op == O_constant
7018 && imm_expr.X_add_number > -0x8000
7019 && imm_expr.X_add_number <= 0x8000)
7020 {
7021 imm_expr.X_add_number = -imm_expr.X_add_number;
7022 macro_build ((char *) NULL, &icnt, &imm_expr,
7023 dbl ? "daddi" : "addi",
7024 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7025 return;
7026 }
7027 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9 7028 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 7029 dbl ? "dsub" : "sub", "d,v,t", dreg, sreg, AT);
252b5132
RH
7030 break;
7031
7032 case M_DSUBU_I:
7033 dbl = 1;
7034 case M_SUBU_I:
7035 if (imm_expr.X_op == O_constant
7036 && imm_expr.X_add_number > -0x8000
7037 && imm_expr.X_add_number <= 0x8000)
7038 {
7039 imm_expr.X_add_number = -imm_expr.X_add_number;
7040 macro_build ((char *) NULL, &icnt, &imm_expr,
7041 dbl ? "daddiu" : "addiu",
7042 "t,r,j", dreg, sreg, (int) BFD_RELOC_LO16);
7043 return;
7044 }
7045 load_register (&icnt, AT, &imm_expr, dbl);
2396cfb9 7046 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 7047 dbl ? "dsubu" : "subu", "d,v,t", dreg, sreg, AT);
252b5132
RH
7048 break;
7049
7050 case M_TEQ_I:
7051 s = "teq";
7052 goto trap;
7053 case M_TGE_I:
7054 s = "tge";
7055 goto trap;
7056 case M_TGEU_I:
7057 s = "tgeu";
7058 goto trap;
7059 case M_TLT_I:
7060 s = "tlt";
7061 goto trap;
7062 case M_TLTU_I:
7063 s = "tltu";
7064 goto trap;
7065 case M_TNE_I:
7066 s = "tne";
7067 trap:
4d34fb5f 7068 load_register (&icnt, AT, &imm_expr, HAVE_64BIT_GPRS);
2396cfb9
TS
7069 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "s,t", sreg,
7070 AT);
252b5132
RH
7071 break;
7072
252b5132 7073 case M_TRUNCWS:
43841e91 7074 case M_TRUNCWD:
e7af610e 7075 assert (mips_opts.isa == ISA_MIPS1);
252b5132
RH
7076 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
7077 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
7078
7079 /*
7080 * Is the double cfc1 instruction a bug in the mips assembler;
7081 * or is there a reason for it?
7082 */
7083 mips_emit_delays (true);
7084 ++mips_opts.noreorder;
7085 mips_any_noreorder = 1;
2396cfb9 7086 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
f9419b05 7087 treg, RA);
2396cfb9 7088 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "cfc1", "t,G",
f9419b05 7089 treg, RA);
2396cfb9 7090 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
252b5132
RH
7091 expr1.X_add_number = 3;
7092 macro_build ((char *) NULL, &icnt, &expr1, "ori", "t,r,i", AT, treg,
7093 (int) BFD_RELOC_LO16);
7094 expr1.X_add_number = 2;
7095 macro_build ((char *) NULL, &icnt, &expr1, "xori", "t,r,i", AT, AT,
7096 (int) BFD_RELOC_LO16);
2396cfb9 7097 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
f9419b05 7098 AT, RA);
2396cfb9
TS
7099 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
7100 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
252b5132 7101 mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S", dreg, sreg);
2396cfb9 7102 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "ctc1", "t,G",
f9419b05 7103 treg, RA);
2396cfb9 7104 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "nop", "");
252b5132
RH
7105 --mips_opts.noreorder;
7106 break;
7107
7108 case M_ULH:
7109 s = "lb";
7110 goto ulh;
7111 case M_ULHU:
7112 s = "lbu";
7113 ulh:
7114 if (offset_expr.X_add_number >= 0x7fff)
7115 as_bad (_("operand overflow"));
7116 /* avoid load delay */
7117 if (! target_big_endian)
f9419b05 7118 ++offset_expr.X_add_number;
252b5132
RH
7119 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7120 (int) BFD_RELOC_LO16, breg);
7121 if (! target_big_endian)
f9419b05 7122 --offset_expr.X_add_number;
252b5132 7123 else
f9419b05 7124 ++offset_expr.X_add_number;
252b5132
RH
7125 macro_build ((char *) NULL, &icnt, &offset_expr, "lbu", "t,o(b)", AT,
7126 (int) BFD_RELOC_LO16, breg);
2396cfb9
TS
7127 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7128 treg, treg, 8);
7129 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7130 treg, treg, AT);
252b5132
RH
7131 break;
7132
7133 case M_ULD:
7134 s = "ldl";
7135 s2 = "ldr";
7136 off = 7;
7137 goto ulw;
7138 case M_ULW:
7139 s = "lwl";
7140 s2 = "lwr";
7141 off = 3;
7142 ulw:
7143 if (offset_expr.X_add_number >= 0x8000 - off)
7144 as_bad (_("operand overflow"));
7145 if (! target_big_endian)
7146 offset_expr.X_add_number += off;
7147 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7148 (int) BFD_RELOC_LO16, breg);
7149 if (! target_big_endian)
7150 offset_expr.X_add_number -= off;
7151 else
7152 offset_expr.X_add_number += off;
7153 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7154 (int) BFD_RELOC_LO16, breg);
7155 return;
7156
7157 case M_ULD_A:
7158 s = "ldl";
7159 s2 = "ldr";
7160 off = 7;
7161 goto ulwa;
7162 case M_ULW_A:
7163 s = "lwl";
7164 s2 = "lwr";
7165 off = 3;
7166 ulwa:
d6bc6245 7167 used_at = 1;
c9914766 7168 load_address (&icnt, AT, &offset_expr, &used_at);
252b5132
RH
7169 if (breg != 0)
7170 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 7171 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
7172 "d,v,t", AT, AT, breg);
7173 if (! target_big_endian)
7174 expr1.X_add_number = off;
7175 else
7176 expr1.X_add_number = 0;
7177 macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7178 (int) BFD_RELOC_LO16, AT);
7179 if (! target_big_endian)
7180 expr1.X_add_number = 0;
7181 else
7182 expr1.X_add_number = off;
7183 macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7184 (int) BFD_RELOC_LO16, AT);
7185 break;
7186
7187 case M_ULH_A:
7188 case M_ULHU_A:
d6bc6245 7189 used_at = 1;
c9914766 7190 load_address (&icnt, AT, &offset_expr, &used_at);
252b5132
RH
7191 if (breg != 0)
7192 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 7193 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
7194 "d,v,t", AT, AT, breg);
7195 if (target_big_endian)
7196 expr1.X_add_number = 0;
7197 macro_build ((char *) NULL, &icnt, &expr1,
7198 mask == M_ULH_A ? "lb" : "lbu", "t,o(b)", treg,
7199 (int) BFD_RELOC_LO16, AT);
7200 if (target_big_endian)
7201 expr1.X_add_number = 1;
7202 else
7203 expr1.X_add_number = 0;
7204 macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7205 (int) BFD_RELOC_LO16, AT);
2396cfb9
TS
7206 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7207 treg, treg, 8);
7208 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7209 treg, treg, AT);
252b5132
RH
7210 break;
7211
7212 case M_USH:
7213 if (offset_expr.X_add_number >= 0x7fff)
7214 as_bad (_("operand overflow"));
7215 if (target_big_endian)
f9419b05 7216 ++offset_expr.X_add_number;
252b5132
RH
7217 macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", treg,
7218 (int) BFD_RELOC_LO16, breg);
2396cfb9
TS
7219 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7220 AT, treg, 8);
252b5132 7221 if (target_big_endian)
f9419b05 7222 --offset_expr.X_add_number;
252b5132 7223 else
f9419b05 7224 ++offset_expr.X_add_number;
252b5132
RH
7225 macro_build ((char *) NULL, &icnt, &offset_expr, "sb", "t,o(b)", AT,
7226 (int) BFD_RELOC_LO16, breg);
7227 break;
7228
7229 case M_USD:
7230 s = "sdl";
7231 s2 = "sdr";
7232 off = 7;
7233 goto usw;
7234 case M_USW:
7235 s = "swl";
7236 s2 = "swr";
7237 off = 3;
7238 usw:
7239 if (offset_expr.X_add_number >= 0x8000 - off)
7240 as_bad (_("operand overflow"));
7241 if (! target_big_endian)
7242 offset_expr.X_add_number += off;
7243 macro_build ((char *) NULL, &icnt, &offset_expr, s, "t,o(b)", treg,
7244 (int) BFD_RELOC_LO16, breg);
7245 if (! target_big_endian)
7246 offset_expr.X_add_number -= off;
7247 else
7248 offset_expr.X_add_number += off;
7249 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "t,o(b)", treg,
7250 (int) BFD_RELOC_LO16, breg);
7251 return;
7252
7253 case M_USD_A:
7254 s = "sdl";
7255 s2 = "sdr";
7256 off = 7;
7257 goto uswa;
7258 case M_USW_A:
7259 s = "swl";
7260 s2 = "swr";
7261 off = 3;
7262 uswa:
d6bc6245 7263 used_at = 1;
c9914766 7264 load_address (&icnt, AT, &offset_expr, &used_at);
252b5132
RH
7265 if (breg != 0)
7266 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 7267 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
7268 "d,v,t", AT, AT, breg);
7269 if (! target_big_endian)
7270 expr1.X_add_number = off;
7271 else
7272 expr1.X_add_number = 0;
7273 macro_build ((char *) NULL, &icnt, &expr1, s, "t,o(b)", treg,
7274 (int) BFD_RELOC_LO16, AT);
7275 if (! target_big_endian)
7276 expr1.X_add_number = 0;
7277 else
7278 expr1.X_add_number = off;
7279 macro_build ((char *) NULL, &icnt, &expr1, s2, "t,o(b)", treg,
7280 (int) BFD_RELOC_LO16, AT);
7281 break;
7282
7283 case M_USH_A:
d6bc6245 7284 used_at = 1;
c9914766 7285 load_address (&icnt, AT, &offset_expr, &used_at);
252b5132
RH
7286 if (breg != 0)
7287 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 7288 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
252b5132
RH
7289 "d,v,t", AT, AT, breg);
7290 if (! target_big_endian)
7291 expr1.X_add_number = 0;
7292 macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7293 (int) BFD_RELOC_LO16, AT);
2396cfb9
TS
7294 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "srl", "d,w,<",
7295 treg, treg, 8);
252b5132
RH
7296 if (! target_big_endian)
7297 expr1.X_add_number = 1;
7298 else
7299 expr1.X_add_number = 0;
7300 macro_build ((char *) NULL, &icnt, &expr1, "sb", "t,o(b)", treg,
7301 (int) BFD_RELOC_LO16, AT);
7302 if (! target_big_endian)
7303 expr1.X_add_number = 0;
7304 else
7305 expr1.X_add_number = 1;
7306 macro_build ((char *) NULL, &icnt, &expr1, "lbu", "t,o(b)", AT,
7307 (int) BFD_RELOC_LO16, AT);
2396cfb9
TS
7308 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "sll", "d,w,<",
7309 treg, treg, 8);
7310 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "or", "d,v,t",
7311 treg, treg, AT);
252b5132
RH
7312 break;
7313
7314 default:
7315 /* FIXME: Check if this is one of the itbl macros, since they
bdaaa2e1 7316 are added dynamically. */
252b5132
RH
7317 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
7318 break;
7319 }
7320 if (mips_opts.noat)
7321 as_warn (_("Macro used $at after \".set noat\""));
7322}
7323
7324/* Implement macros in mips16 mode. */
7325
7326static void
7327mips16_macro (ip)
7328 struct mips_cl_insn *ip;
7329{
7330 int mask;
7331 int xreg, yreg, zreg, tmp;
7332 int icnt;
7333 expressionS expr1;
7334 int dbl;
7335 const char *s, *s2, *s3;
7336
7337 mask = ip->insn_mo->mask;
7338
7339 xreg = (ip->insn_opcode >> MIPS16OP_SH_RX) & MIPS16OP_MASK_RX;
7340 yreg = (ip->insn_opcode >> MIPS16OP_SH_RY) & MIPS16OP_MASK_RY;
7341 zreg = (ip->insn_opcode >> MIPS16OP_SH_RZ) & MIPS16OP_MASK_RZ;
7342
7343 icnt = 0;
7344
7345 expr1.X_op = O_constant;
7346 expr1.X_op_symbol = NULL;
7347 expr1.X_add_symbol = NULL;
7348 expr1.X_add_number = 1;
7349
7350 dbl = 0;
7351
7352 switch (mask)
7353 {
7354 default:
7355 internalError ();
7356
7357 case M_DDIV_3:
7358 dbl = 1;
7359 case M_DIV_3:
7360 s = "mflo";
7361 goto do_div3;
7362 case M_DREM_3:
7363 dbl = 1;
7364 case M_REM_3:
7365 s = "mfhi";
7366 do_div3:
7367 mips_emit_delays (true);
7368 ++mips_opts.noreorder;
7369 mips_any_noreorder = 1;
2396cfb9 7370 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
252b5132
RH
7371 dbl ? "ddiv" : "div",
7372 "0,x,y", xreg, yreg);
7373 expr1.X_add_number = 2;
7374 macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
2396cfb9
TS
7375 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break", "6",
7376 7);
bdaaa2e1 7377
252b5132
RH
7378 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
7379 since that causes an overflow. We should do that as well,
7380 but I don't see how to do the comparisons without a temporary
7381 register. */
7382 --mips_opts.noreorder;
2396cfb9 7383 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x", zreg);
252b5132
RH
7384 break;
7385
7386 case M_DIVU_3:
7387 s = "divu";
7388 s2 = "mflo";
7389 goto do_divu3;
7390 case M_REMU_3:
7391 s = "divu";
7392 s2 = "mfhi";
7393 goto do_divu3;
7394 case M_DDIVU_3:
7395 s = "ddivu";
7396 s2 = "mflo";
7397 goto do_divu3;
7398 case M_DREMU_3:
7399 s = "ddivu";
7400 s2 = "mfhi";
7401 do_divu3:
7402 mips_emit_delays (true);
7403 ++mips_opts.noreorder;
7404 mips_any_noreorder = 1;
2396cfb9
TS
7405 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "0,x,y",
7406 xreg, yreg);
252b5132
RH
7407 expr1.X_add_number = 2;
7408 macro_build ((char *) NULL, &icnt, &expr1, "bnez", "x,p", yreg);
98d3f06f
KH
7409 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "break",
7410 "6", 7);
252b5132 7411 --mips_opts.noreorder;
2396cfb9 7412 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s2, "x", zreg);
252b5132
RH
7413 break;
7414
7415 case M_DMUL:
7416 dbl = 1;
7417 case M_MUL:
2396cfb9 7418 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
9a41af64 7419 dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
2396cfb9
TS
7420 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "mflo", "x",
7421 zreg);
252b5132
RH
7422 return;
7423
7424 case M_DSUBU_I:
7425 dbl = 1;
7426 goto do_subu;
7427 case M_SUBU_I:
7428 do_subu:
7429 if (imm_expr.X_op != O_constant)
7430 as_bad (_("Unsupported large constant"));
7431 imm_expr.X_add_number = -imm_expr.X_add_number;
7432 macro_build ((char *) NULL, &icnt, &imm_expr,
9a41af64 7433 dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
252b5132
RH
7434 break;
7435
7436 case M_SUBU_I_2:
7437 if (imm_expr.X_op != O_constant)
7438 as_bad (_("Unsupported large constant"));
7439 imm_expr.X_add_number = -imm_expr.X_add_number;
7440 macro_build ((char *) NULL, &icnt, &imm_expr, "addiu",
7441 "x,k", xreg);
7442 break;
7443
7444 case M_DSUBU_I_2:
7445 if (imm_expr.X_op != O_constant)
7446 as_bad (_("Unsupported large constant"));
7447 imm_expr.X_add_number = -imm_expr.X_add_number;
7448 macro_build ((char *) NULL, &icnt, &imm_expr, "daddiu",
7449 "y,j", yreg);
7450 break;
7451
7452 case M_BEQ:
7453 s = "cmp";
7454 s2 = "bteqz";
7455 goto do_branch;
7456 case M_BNE:
7457 s = "cmp";
7458 s2 = "btnez";
7459 goto do_branch;
7460 case M_BLT:
7461 s = "slt";
7462 s2 = "btnez";
7463 goto do_branch;
7464 case M_BLTU:
7465 s = "sltu";
7466 s2 = "btnez";
7467 goto do_branch;
7468 case M_BLE:
7469 s = "slt";
7470 s2 = "bteqz";
7471 goto do_reverse_branch;
7472 case M_BLEU:
7473 s = "sltu";
7474 s2 = "bteqz";
7475 goto do_reverse_branch;
7476 case M_BGE:
7477 s = "slt";
7478 s2 = "bteqz";
7479 goto do_branch;
7480 case M_BGEU:
7481 s = "sltu";
7482 s2 = "bteqz";
7483 goto do_branch;
7484 case M_BGT:
7485 s = "slt";
7486 s2 = "btnez";
7487 goto do_reverse_branch;
7488 case M_BGTU:
7489 s = "sltu";
7490 s2 = "btnez";
7491
7492 do_reverse_branch:
7493 tmp = xreg;
7494 xreg = yreg;
7495 yreg = tmp;
7496
7497 do_branch:
7498 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, s, "x,y",
7499 xreg, yreg);
7500 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7501 break;
7502
7503 case M_BEQ_I:
7504 s = "cmpi";
7505 s2 = "bteqz";
7506 s3 = "x,U";
7507 goto do_branch_i;
7508 case M_BNE_I:
7509 s = "cmpi";
7510 s2 = "btnez";
7511 s3 = "x,U";
7512 goto do_branch_i;
7513 case M_BLT_I:
7514 s = "slti";
7515 s2 = "btnez";
7516 s3 = "x,8";
7517 goto do_branch_i;
7518 case M_BLTU_I:
7519 s = "sltiu";
7520 s2 = "btnez";
7521 s3 = "x,8";
7522 goto do_branch_i;
7523 case M_BLE_I:
7524 s = "slti";
7525 s2 = "btnez";
7526 s3 = "x,8";
7527 goto do_addone_branch_i;
7528 case M_BLEU_I:
7529 s = "sltiu";
7530 s2 = "btnez";
7531 s3 = "x,8";
7532 goto do_addone_branch_i;
7533 case M_BGE_I:
7534 s = "slti";
7535 s2 = "bteqz";
7536 s3 = "x,8";
7537 goto do_branch_i;
7538 case M_BGEU_I:
7539 s = "sltiu";
7540 s2 = "bteqz";
7541 s3 = "x,8";
7542 goto do_branch_i;
7543 case M_BGT_I:
7544 s = "slti";
7545 s2 = "bteqz";
7546 s3 = "x,8";
7547 goto do_addone_branch_i;
7548 case M_BGTU_I:
7549 s = "sltiu";
7550 s2 = "bteqz";
7551 s3 = "x,8";
7552
7553 do_addone_branch_i:
7554 if (imm_expr.X_op != O_constant)
7555 as_bad (_("Unsupported large constant"));
7556 ++imm_expr.X_add_number;
7557
7558 do_branch_i:
7559 macro_build ((char *) NULL, &icnt, &imm_expr, s, s3, xreg);
7560 macro_build ((char *) NULL, &icnt, &offset_expr, s2, "p");
7561 break;
7562
7563 case M_ABS:
7564 expr1.X_add_number = 0;
98d3f06f 7565 macro_build ((char *) NULL, &icnt, &expr1, "slti", "x,8", yreg);
252b5132 7566 if (xreg != yreg)
ea1fb5dc 7567 move_register (&icnt, xreg, yreg);
252b5132
RH
7568 expr1.X_add_number = 2;
7569 macro_build ((char *) NULL, &icnt, &expr1, "bteqz", "p");
7570 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
7571 "neg", "x,w", xreg, xreg);
7572 }
7573}
7574
7575/* For consistency checking, verify that all bits are specified either
7576 by the match/mask part of the instruction definition, or by the
7577 operand list. */
7578static int
7579validate_mips_insn (opc)
7580 const struct mips_opcode *opc;
7581{
7582 const char *p = opc->args;
7583 char c;
7584 unsigned long used_bits = opc->mask;
7585
7586 if ((used_bits & opc->match) != opc->match)
7587 {
7588 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
7589 opc->name, opc->args);
7590 return 0;
7591 }
7592#define USE_BITS(mask,shift) (used_bits |= ((mask) << (shift)))
7593 while (*p)
7594 switch (c = *p++)
7595 {
7596 case ',': break;
7597 case '(': break;
7598 case ')': break;
7599 case '<': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7600 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
7601 case 'A': break;
4372b673 7602 case 'B': USE_BITS (OP_MASK_CODE20, OP_SH_CODE20); break;
252b5132
RH
7603 case 'C': USE_BITS (OP_MASK_COPZ, OP_SH_COPZ); break;
7604 case 'D': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
7605 case 'E': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7606 case 'F': break;
7607 case 'G': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
156c2f8b 7608 case 'H': USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
252b5132 7609 case 'I': break;
e972090a 7610 case 'J': USE_BITS (OP_MASK_CODE19, OP_SH_CODE19); break;
252b5132
RH
7611 case 'L': break;
7612 case 'M': USE_BITS (OP_MASK_CCC, OP_SH_CCC); break;
7613 case 'N': USE_BITS (OP_MASK_BCC, OP_SH_BCC); break;
deec1734
CD
7614 case 'O': USE_BITS (OP_MASK_ALN, OP_SH_ALN); break;
7615 case 'Q': USE_BITS (OP_MASK_VSEL, OP_SH_VSEL);
7616 USE_BITS (OP_MASK_FT, OP_SH_FT); break;
252b5132
RH
7617 case 'R': USE_BITS (OP_MASK_FR, OP_SH_FR); break;
7618 case 'S': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7619 case 'T': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
7620 case 'V': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7621 case 'W': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
deec1734
CD
7622 case 'X': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
7623 case 'Y': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
7624 case 'Z': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
252b5132
RH
7625 case 'a': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break;
7626 case 'b': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7627 case 'c': USE_BITS (OP_MASK_CODE, OP_SH_CODE); break;
7628 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
7629 case 'f': break;
7630 case 'h': USE_BITS (OP_MASK_PREFX, OP_SH_PREFX); break;
7631 case 'i': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
7632 case 'j': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7633 case 'k': USE_BITS (OP_MASK_CACHE, OP_SH_CACHE); break;
7634 case 'l': break;
7635 case 'o': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7636 case 'p': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
7637 case 'q': USE_BITS (OP_MASK_CODE2, OP_SH_CODE2); break;
7638 case 'r': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7639 case 's': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7640 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7641 case 'u': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
7642 case 'v': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
7643 case 'w': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
7644 case 'x': break;
7645 case 'z': break;
7646 case 'P': USE_BITS (OP_MASK_PERFREG, OP_SH_PERFREG); break;
4372b673
NC
7647 case 'U': USE_BITS (OP_MASK_RD, OP_SH_RD);
7648 USE_BITS (OP_MASK_RT, OP_SH_RT); break;
252b5132
RH
7649 default:
7650 as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
7651 c, opc->name, opc->args);
7652 return 0;
7653 }
7654#undef USE_BITS
7655 if (used_bits != 0xffffffff)
7656 {
7657 as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
7658 ~used_bits & 0xffffffff, opc->name, opc->args);
7659 return 0;
7660 }
7661 return 1;
7662}
7663
7664/* This routine assembles an instruction into its binary format. As a
7665 side effect, it sets one of the global variables imm_reloc or
7666 offset_reloc to the type of relocation to do if one of the operands
7667 is an address expression. */
7668
7669static void
7670mips_ip (str, ip)
7671 char *str;
7672 struct mips_cl_insn *ip;
7673{
7674 char *s;
7675 const char *args;
43841e91 7676 char c = 0;
252b5132
RH
7677 struct mips_opcode *insn;
7678 char *argsStart;
7679 unsigned int regno;
7680 unsigned int lastregno = 0;
7681 char *s_reset;
7682 char save_c = 0;
252b5132
RH
7683
7684 insn_error = NULL;
7685
7686 /* If the instruction contains a '.', we first try to match an instruction
7687 including the '.'. Then we try again without the '.'. */
7688 insn = NULL;
3882b010 7689 for (s = str; *s != '\0' && !ISSPACE (*s); ++s)
252b5132
RH
7690 continue;
7691
7692 /* If we stopped on whitespace, then replace the whitespace with null for
7693 the call to hash_find. Save the character we replaced just in case we
7694 have to re-parse the instruction. */
3882b010 7695 if (ISSPACE (*s))
252b5132
RH
7696 {
7697 save_c = *s;
7698 *s++ = '\0';
7699 }
bdaaa2e1 7700
252b5132
RH
7701 insn = (struct mips_opcode *) hash_find (op_hash, str);
7702
7703 /* If we didn't find the instruction in the opcode table, try again, but
7704 this time with just the instruction up to, but not including the
7705 first '.'. */
7706 if (insn == NULL)
7707 {
bdaaa2e1 7708 /* Restore the character we overwrite above (if any). */
252b5132
RH
7709 if (save_c)
7710 *(--s) = save_c;
7711
7712 /* Scan up to the first '.' or whitespace. */
3882b010
L
7713 for (s = str;
7714 *s != '\0' && *s != '.' && !ISSPACE (*s);
7715 ++s)
252b5132
RH
7716 continue;
7717
7718 /* If we did not find a '.', then we can quit now. */
7719 if (*s != '.')
7720 {
7721 insn_error = "unrecognized opcode";
7722 return;
7723 }
7724
7725 /* Lookup the instruction in the hash table. */
7726 *s++ = '\0';
7727 if ((insn = (struct mips_opcode *) hash_find (op_hash, str)) == NULL)
7728 {
7729 insn_error = "unrecognized opcode";
7730 return;
7731 }
252b5132
RH
7732 }
7733
7734 argsStart = s;
7735 for (;;)
7736 {
252b5132
RH
7737 boolean ok;
7738
7739 assert (strcmp (insn->name, str) == 0);
7740
1f25f5d3
CD
7741 if (OPCODE_IS_MEMBER (insn,
7742 (mips_opts.isa
aec421e0 7743 | (mips_opts.mips16 ? INSN_MIPS16 : 0)
deec1734 7744 | (mips_opts.ase_mdmx ? INSN_MDMX : 0)
98d3f06f 7745 | (mips_opts.ase_mips3d ? INSN_MIPS3D : 0)),
1f25f5d3 7746 mips_arch))
252b5132 7747 ok = true;
bdaaa2e1 7748 else
252b5132 7749 ok = false;
bdaaa2e1 7750
252b5132
RH
7751 if (insn->pinfo != INSN_MACRO)
7752 {
ec68c924 7753 if (mips_arch == CPU_R4650 && (insn->pinfo & FP_D) != 0)
252b5132
RH
7754 ok = false;
7755 }
7756
7757 if (! ok)
7758 {
7759 if (insn + 1 < &mips_opcodes[NUMOPCODES]
7760 && strcmp (insn->name, insn[1].name) == 0)
7761 {
7762 ++insn;
7763 continue;
7764 }
252b5132 7765 else
beae10d5 7766 {
268f6bed
L
7767 if (!insn_error)
7768 {
7769 static char buf[100];
316f5878
RS
7770 if (mips_arch_info->is_isa)
7771 sprintf (buf,
7772 _("opcode not supported at this ISA level (%s)"),
7773 mips_cpu_info_from_isa (mips_opts.isa)->name);
7774 else
7775 sprintf (buf,
7776 _("opcode not supported on this processor: %s (%s)"),
7777 mips_arch_info->name,
7778 mips_cpu_info_from_isa (mips_opts.isa)->name);
268f6bed
L
7779 insn_error = buf;
7780 }
7781 if (save_c)
7782 *(--s) = save_c;
2bd7f1f3 7783 return;
252b5132 7784 }
252b5132
RH
7785 }
7786
7787 ip->insn_mo = insn;
7788 ip->insn_opcode = insn->match;
268f6bed 7789 insn_error = NULL;
252b5132
RH
7790 for (args = insn->args;; ++args)
7791 {
deec1734
CD
7792 int is_mdmx;
7793
ad8d3bb3 7794 s += strspn (s, " \t");
deec1734 7795 is_mdmx = 0;
252b5132
RH
7796 switch (*args)
7797 {
7798 case '\0': /* end of args */
7799 if (*s == '\0')
7800 return;
7801 break;
7802
7803 case ',':
7804 if (*s++ == *args)
7805 continue;
7806 s--;
7807 switch (*++args)
7808 {
7809 case 'r':
7810 case 'v':
38487616 7811 ip->insn_opcode |= lastregno << OP_SH_RS;
252b5132
RH
7812 continue;
7813
7814 case 'w':
38487616
TS
7815 ip->insn_opcode |= lastregno << OP_SH_RT;
7816 continue;
7817
252b5132 7818 case 'W':
38487616 7819 ip->insn_opcode |= lastregno << OP_SH_FT;
252b5132
RH
7820 continue;
7821
7822 case 'V':
38487616 7823 ip->insn_opcode |= lastregno << OP_SH_FS;
252b5132
RH
7824 continue;
7825 }
7826 break;
7827
7828 case '(':
7829 /* Handle optional base register.
7830 Either the base register is omitted or
bdaaa2e1 7831 we must have a left paren. */
252b5132
RH
7832 /* This is dependent on the next operand specifier
7833 is a base register specification. */
7834 assert (args[1] == 'b' || args[1] == '5'
7835 || args[1] == '-' || args[1] == '4');
7836 if (*s == '\0')
7837 return;
7838
7839 case ')': /* these must match exactly */
7840 if (*s++ == *args)
7841 continue;
7842 break;
7843
7844 case '<': /* must be at least one digit */
7845 /*
7846 * According to the manual, if the shift amount is greater
b6ff326e
KH
7847 * than 31 or less than 0, then the shift amount should be
7848 * mod 32. In reality the mips assembler issues an error.
252b5132
RH
7849 * We issue a warning and mask out all but the low 5 bits.
7850 */
7851 my_getExpression (&imm_expr, s);
7852 check_absolute_expr (ip, &imm_expr);
7853 if ((unsigned long) imm_expr.X_add_number > 31)
7854 {
793b27f4
TS
7855 as_warn (_("Improper shift amount (%lu)"),
7856 (unsigned long) imm_expr.X_add_number);
38487616 7857 imm_expr.X_add_number &= OP_MASK_SHAMT;
252b5132 7858 }
38487616 7859 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_SHAMT;
252b5132
RH
7860 imm_expr.X_op = O_absent;
7861 s = expr_end;
7862 continue;
7863
7864 case '>': /* shift amount minus 32 */
7865 my_getExpression (&imm_expr, s);
7866 check_absolute_expr (ip, &imm_expr);
7867 if ((unsigned long) imm_expr.X_add_number < 32
7868 || (unsigned long) imm_expr.X_add_number > 63)
7869 break;
38487616 7870 ip->insn_opcode |= (imm_expr.X_add_number - 32) << OP_SH_SHAMT;
252b5132
RH
7871 imm_expr.X_op = O_absent;
7872 s = expr_end;
7873 continue;
7874
252b5132
RH
7875 case 'k': /* cache code */
7876 case 'h': /* prefx code */
7877 my_getExpression (&imm_expr, s);
7878 check_absolute_expr (ip, &imm_expr);
7879 if ((unsigned long) imm_expr.X_add_number > 31)
7880 {
7881 as_warn (_("Invalid value for `%s' (%lu)"),
7882 ip->insn_mo->name,
7883 (unsigned long) imm_expr.X_add_number);
7884 imm_expr.X_add_number &= 0x1f;
7885 }
7886 if (*args == 'k')
7887 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CACHE;
7888 else
7889 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_PREFX;
7890 imm_expr.X_op = O_absent;
7891 s = expr_end;
7892 continue;
7893
7894 case 'c': /* break code */
7895 my_getExpression (&imm_expr, s);
7896 check_absolute_expr (ip, &imm_expr);
793b27f4 7897 if ((unsigned long) imm_expr.X_add_number > 1023)
252b5132 7898 {
793b27f4
TS
7899 as_warn (_("Illegal break code (%lu)"),
7900 (unsigned long) imm_expr.X_add_number);
38487616 7901 imm_expr.X_add_number &= OP_MASK_CODE;
252b5132 7902 }
38487616 7903 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE;
252b5132
RH
7904 imm_expr.X_op = O_absent;
7905 s = expr_end;
7906 continue;
7907
7908 case 'q': /* lower break code */
7909 my_getExpression (&imm_expr, s);
7910 check_absolute_expr (ip, &imm_expr);
793b27f4 7911 if ((unsigned long) imm_expr.X_add_number > 1023)
252b5132 7912 {
793b27f4
TS
7913 as_warn (_("Illegal lower break code (%lu)"),
7914 (unsigned long) imm_expr.X_add_number);
38487616 7915 imm_expr.X_add_number &= OP_MASK_CODE2;
252b5132 7916 }
38487616 7917 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE2;
252b5132
RH
7918 imm_expr.X_op = O_absent;
7919 s = expr_end;
7920 continue;
7921
4372b673 7922 case 'B': /* 20-bit syscall/break code. */
156c2f8b 7923 my_getExpression (&imm_expr, s);
156c2f8b 7924 check_absolute_expr (ip, &imm_expr);
793b27f4
TS
7925 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE20)
7926 as_warn (_("Illegal 20-bit code (%lu)"),
7927 (unsigned long) imm_expr.X_add_number);
38487616 7928 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE20;
252b5132
RH
7929 imm_expr.X_op = O_absent;
7930 s = expr_end;
7931 continue;
7932
98d3f06f 7933 case 'C': /* Coprocessor code */
beae10d5 7934 my_getExpression (&imm_expr, s);
252b5132 7935 check_absolute_expr (ip, &imm_expr);
98d3f06f 7936 if ((unsigned long) imm_expr.X_add_number >= (1 << 25))
252b5132 7937 {
793b27f4
TS
7938 as_warn (_("Coproccesor code > 25 bits (%lu)"),
7939 (unsigned long) imm_expr.X_add_number);
98d3f06f 7940 imm_expr.X_add_number &= ((1 << 25) - 1);
252b5132 7941 }
beae10d5
KH
7942 ip->insn_opcode |= imm_expr.X_add_number;
7943 imm_expr.X_op = O_absent;
7944 s = expr_end;
7945 continue;
252b5132 7946
4372b673
NC
7947 case 'J': /* 19-bit wait code. */
7948 my_getExpression (&imm_expr, s);
7949 check_absolute_expr (ip, &imm_expr);
793b27f4
TS
7950 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
7951 as_warn (_("Illegal 19-bit code (%lu)"),
7952 (unsigned long) imm_expr.X_add_number);
38487616 7953 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_CODE19;
4372b673
NC
7954 imm_expr.X_op = O_absent;
7955 s = expr_end;
7956 continue;
7957
252b5132 7958 case 'P': /* Performance register */
beae10d5 7959 my_getExpression (&imm_expr, s);
252b5132 7960 check_absolute_expr (ip, &imm_expr);
beae10d5 7961 if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
252b5132 7962 {
793b27f4
TS
7963 as_warn (_("Invalid performance register (%lu)"),
7964 (unsigned long) imm_expr.X_add_number);
38487616 7965 imm_expr.X_add_number &= OP_MASK_PERFREG;
252b5132 7966 }
38487616 7967 ip->insn_opcode |= (imm_expr.X_add_number << OP_SH_PERFREG);
beae10d5
KH
7968 imm_expr.X_op = O_absent;
7969 s = expr_end;
7970 continue;
252b5132
RH
7971
7972 case 'b': /* base register */
7973 case 'd': /* destination register */
7974 case 's': /* source register */
7975 case 't': /* target register */
7976 case 'r': /* both target and source */
7977 case 'v': /* both dest and source */
7978 case 'w': /* both dest and target */
7979 case 'E': /* coprocessor target register */
7980 case 'G': /* coprocessor destination register */
7981 case 'x': /* ignore register name */
7982 case 'z': /* must be zero register */
4372b673 7983 case 'U': /* destination register (clo/clz). */
252b5132
RH
7984 s_reset = s;
7985 if (s[0] == '$')
7986 {
7987
3882b010 7988 if (ISDIGIT (s[1]))
252b5132
RH
7989 {
7990 ++s;
7991 regno = 0;
7992 do
7993 {
7994 regno *= 10;
7995 regno += *s - '0';
7996 ++s;
7997 }
3882b010 7998 while (ISDIGIT (*s));
252b5132
RH
7999 if (regno > 31)
8000 as_bad (_("Invalid register number (%d)"), regno);
8001 }
8002 else if (*args == 'E' || *args == 'G')
8003 goto notreg;
8004 else
8005 {
76db943d
TS
8006 if (s[1] == 'r' && s[2] == 'a')
8007 {
8008 s += 3;
8009 regno = RA;
8010 }
8011 else if (s[1] == 'f' && s[2] == 'p')
252b5132
RH
8012 {
8013 s += 3;
8014 regno = FP;
8015 }
8016 else if (s[1] == 's' && s[2] == 'p')
8017 {
8018 s += 3;
8019 regno = SP;
8020 }
8021 else if (s[1] == 'g' && s[2] == 'p')
8022 {
8023 s += 3;
8024 regno = GP;
8025 }
8026 else if (s[1] == 'a' && s[2] == 't')
8027 {
8028 s += 3;
8029 regno = AT;
8030 }
8031 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8032 {
8033 s += 4;
8034 regno = KT0;
8035 }
8036 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8037 {
8038 s += 4;
8039 regno = KT1;
8040 }
85b51719
TS
8041 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
8042 {
8043 s += 5;
8044 regno = ZERO;
8045 }
252b5132
RH
8046 else if (itbl_have_entries)
8047 {
8048 char *p, *n;
d7ba4a77 8049 unsigned long r;
252b5132 8050
d7ba4a77 8051 p = s + 1; /* advance past '$' */
252b5132
RH
8052 n = itbl_get_field (&p); /* n is name */
8053
d7ba4a77
ILT
8054 /* See if this is a register defined in an
8055 itbl entry. */
8056 if (itbl_get_reg_val (n, &r))
252b5132
RH
8057 {
8058 /* Get_field advances to the start of
8059 the next field, so we need to back
d7ba4a77 8060 rack to the end of the last field. */
bdaaa2e1 8061 if (p)
252b5132 8062 s = p - 1;
bdaaa2e1 8063 else
d7ba4a77 8064 s = strchr (s, '\0');
252b5132
RH
8065 regno = r;
8066 }
8067 else
8068 goto notreg;
beae10d5 8069 }
252b5132
RH
8070 else
8071 goto notreg;
8072 }
8073 if (regno == AT
8074 && ! mips_opts.noat
8075 && *args != 'E'
8076 && *args != 'G')
8077 as_warn (_("Used $at without \".set noat\""));
8078 c = *args;
8079 if (*s == ' ')
f9419b05 8080 ++s;
252b5132
RH
8081 if (args[1] != *s)
8082 {
8083 if (c == 'r' || c == 'v' || c == 'w')
8084 {
8085 regno = lastregno;
8086 s = s_reset;
f9419b05 8087 ++args;
252b5132
RH
8088 }
8089 }
8090 /* 'z' only matches $0. */
8091 if (c == 'z' && regno != 0)
8092 break;
8093
bdaaa2e1
KH
8094 /* Now that we have assembled one operand, we use the args string
8095 * to figure out where it goes in the instruction. */
252b5132
RH
8096 switch (c)
8097 {
8098 case 'r':
8099 case 's':
8100 case 'v':
8101 case 'b':
38487616 8102 ip->insn_opcode |= regno << OP_SH_RS;
252b5132
RH
8103 break;
8104 case 'd':
8105 case 'G':
38487616 8106 ip->insn_opcode |= regno << OP_SH_RD;
252b5132 8107 break;
4372b673 8108 case 'U':
38487616
TS
8109 ip->insn_opcode |= regno << OP_SH_RD;
8110 ip->insn_opcode |= regno << OP_SH_RT;
4372b673 8111 break;
252b5132
RH
8112 case 'w':
8113 case 't':
8114 case 'E':
38487616 8115 ip->insn_opcode |= regno << OP_SH_RT;
252b5132
RH
8116 break;
8117 case 'x':
8118 /* This case exists because on the r3000 trunc
8119 expands into a macro which requires a gp
8120 register. On the r6000 or r4000 it is
8121 assembled into a single instruction which
8122 ignores the register. Thus the insn version
8123 is MIPS_ISA2 and uses 'x', and the macro
8124 version is MIPS_ISA1 and uses 't'. */
8125 break;
8126 case 'z':
8127 /* This case is for the div instruction, which
8128 acts differently if the destination argument
8129 is $0. This only matches $0, and is checked
8130 outside the switch. */
8131 break;
8132 case 'D':
8133 /* Itbl operand; not yet implemented. FIXME ?? */
8134 break;
8135 /* What about all other operands like 'i', which
8136 can be specified in the opcode table? */
8137 }
8138 lastregno = regno;
8139 continue;
8140 }
8141 notreg:
8142 switch (*args++)
8143 {
8144 case 'r':
8145 case 'v':
38487616 8146 ip->insn_opcode |= lastregno << OP_SH_RS;
252b5132
RH
8147 continue;
8148 case 'w':
38487616 8149 ip->insn_opcode |= lastregno << OP_SH_RT;
252b5132
RH
8150 continue;
8151 }
8152 break;
8153
deec1734
CD
8154 case 'O': /* MDMX alignment immediate constant. */
8155 my_getExpression (&imm_expr, s);
8156 check_absolute_expr (ip, &imm_expr);
8157 if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
8158 {
8159 as_warn ("Improper align amount (%ld), using low bits",
8160 (long) imm_expr.X_add_number);
8161 imm_expr.X_add_number &= OP_MASK_ALN;
8162 }
8163 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_ALN;
8164 imm_expr.X_op = O_absent;
8165 s = expr_end;
8166 continue;
8167
8168 case 'Q': /* MDMX vector, element sel, or const. */
8169 if (s[0] != '$')
8170 {
8171 /* MDMX Immediate. */
8172 my_getExpression (&imm_expr, s);
8173 check_absolute_expr (ip, &imm_expr);
8174 if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
8175 {
8176 as_warn (_("Invalid MDMX Immediate (%ld)"),
8177 (long) imm_expr.X_add_number);
8178 imm_expr.X_add_number &= OP_MASK_FT;
8179 }
8180 imm_expr.X_add_number &= OP_MASK_FT;
8181 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8182 ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
8183 else
8184 ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
8185 ip->insn_opcode |= imm_expr.X_add_number << OP_SH_FT;
8186 imm_expr.X_op = O_absent;
8187 s = expr_end;
8188 continue;
8189 }
8190 /* Not MDMX Immediate. Fall through. */
8191 case 'X': /* MDMX destination register. */
8192 case 'Y': /* MDMX source register. */
8193 case 'Z': /* MDMX target register. */
8194 is_mdmx = 1;
252b5132
RH
8195 case 'D': /* floating point destination register */
8196 case 'S': /* floating point source register */
8197 case 'T': /* floating point target register */
8198 case 'R': /* floating point source register */
8199 case 'V':
8200 case 'W':
8201 s_reset = s;
deec1734
CD
8202 /* Accept $fN for FP and MDMX register numbers, and in
8203 addition accept $vN for MDMX register numbers. */
8204 if ((s[0] == '$' && s[1] == 'f' && ISDIGIT (s[2]))
8205 || (is_mdmx != 0 && s[0] == '$' && s[1] == 'v'
8206 && ISDIGIT (s[2])))
252b5132
RH
8207 {
8208 s += 2;
8209 regno = 0;
8210 do
8211 {
8212 regno *= 10;
8213 regno += *s - '0';
8214 ++s;
8215 }
3882b010 8216 while (ISDIGIT (*s));
252b5132
RH
8217
8218 if (regno > 31)
8219 as_bad (_("Invalid float register number (%d)"), regno);
8220
8221 if ((regno & 1) != 0
ca4e0257 8222 && HAVE_32BIT_FPRS
252b5132
RH
8223 && ! (strcmp (str, "mtc1") == 0
8224 || strcmp (str, "mfc1") == 0
8225 || strcmp (str, "lwc1") == 0
8226 || strcmp (str, "swc1") == 0
8227 || strcmp (str, "l.s") == 0
8228 || strcmp (str, "s.s") == 0))
8229 as_warn (_("Float register should be even, was %d"),
8230 regno);
8231
8232 c = *args;
8233 if (*s == ' ')
f9419b05 8234 ++s;
252b5132
RH
8235 if (args[1] != *s)
8236 {
8237 if (c == 'V' || c == 'W')
8238 {
8239 regno = lastregno;
8240 s = s_reset;
f9419b05 8241 ++args;
252b5132
RH
8242 }
8243 }
8244 switch (c)
8245 {
8246 case 'D':
deec1734 8247 case 'X':
38487616 8248 ip->insn_opcode |= regno << OP_SH_FD;
252b5132
RH
8249 break;
8250 case 'V':
8251 case 'S':
deec1734 8252 case 'Y':
38487616 8253 ip->insn_opcode |= regno << OP_SH_FS;
252b5132 8254 break;
deec1734
CD
8255 case 'Q':
8256 /* This is like 'Z', but also needs to fix the MDMX
8257 vector/scalar select bits. Note that the
8258 scalar immediate case is handled above. */
8259 if (*s == '[')
8260 {
8261 int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
8262 int max_el = (is_qh ? 3 : 7);
8263 s++;
8264 my_getExpression(&imm_expr, s);
8265 check_absolute_expr (ip, &imm_expr);
8266 s = expr_end;
8267 if (imm_expr.X_add_number > max_el)
8268 as_bad(_("Bad element selector %ld"),
8269 (long) imm_expr.X_add_number);
8270 imm_expr.X_add_number &= max_el;
8271 ip->insn_opcode |= (imm_expr.X_add_number
8272 << (OP_SH_VSEL +
8273 (is_qh ? 2 : 1)));
8274 if (*s != ']')
8275 as_warn(_("Expecting ']' found '%s'"), s);
8276 else
8277 s++;
8278 }
8279 else
8280 {
8281 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
8282 ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
8283 << OP_SH_VSEL);
8284 else
8285 ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
8286 OP_SH_VSEL);
8287 }
8288 /* Fall through */
252b5132
RH
8289 case 'W':
8290 case 'T':
deec1734 8291 case 'Z':
38487616 8292 ip->insn_opcode |= regno << OP_SH_FT;
252b5132
RH
8293 break;
8294 case 'R':
38487616 8295 ip->insn_opcode |= regno << OP_SH_FR;
252b5132
RH
8296 break;
8297 }
8298 lastregno = regno;
8299 continue;
8300 }
8301
252b5132
RH
8302 switch (*args++)
8303 {
8304 case 'V':
38487616 8305 ip->insn_opcode |= lastregno << OP_SH_FS;
252b5132
RH
8306 continue;
8307 case 'W':
38487616 8308 ip->insn_opcode |= lastregno << OP_SH_FT;
252b5132
RH
8309 continue;
8310 }
8311 break;
8312
8313 case 'I':
8314 my_getExpression (&imm_expr, s);
8315 if (imm_expr.X_op != O_big
8316 && imm_expr.X_op != O_constant)
8317 insn_error = _("absolute expression required");
8318 s = expr_end;
8319 continue;
8320
8321 case 'A':
8322 my_getExpression (&offset_expr, s);
f6688943 8323 *imm_reloc = BFD_RELOC_32;
252b5132
RH
8324 s = expr_end;
8325 continue;
8326
8327 case 'F':
8328 case 'L':
8329 case 'f':
8330 case 'l':
8331 {
8332 int f64;
ca4e0257 8333 int using_gprs;
252b5132
RH
8334 char *save_in;
8335 char *err;
8336 unsigned char temp[8];
8337 int len;
8338 unsigned int length;
8339 segT seg;
8340 subsegT subseg;
8341 char *p;
8342
8343 /* These only appear as the last operand in an
8344 instruction, and every instruction that accepts
8345 them in any variant accepts them in all variants.
8346 This means we don't have to worry about backing out
8347 any changes if the instruction does not match.
8348
8349 The difference between them is the size of the
8350 floating point constant and where it goes. For 'F'
8351 and 'L' the constant is 64 bits; for 'f' and 'l' it
8352 is 32 bits. Where the constant is placed is based
8353 on how the MIPS assembler does things:
8354 F -- .rdata
8355 L -- .lit8
8356 f -- immediate value
8357 l -- .lit4
8358
8359 The .lit4 and .lit8 sections are only used if
8360 permitted by the -G argument.
8361
8362 When generating embedded PIC code, we use the
8363 .lit8 section but not the .lit4 section (we can do
8364 .lit4 inline easily; we need to put .lit8
8365 somewhere in the data segment, and using .lit8
8366 permits the linker to eventually combine identical
ca4e0257
RS
8367 .lit8 entries).
8368
8369 The code below needs to know whether the target register
8370 is 32 or 64 bits wide. It relies on the fact 'f' and
8371 'F' are used with GPR-based instructions and 'l' and
8372 'L' are used with FPR-based instructions. */
252b5132
RH
8373
8374 f64 = *args == 'F' || *args == 'L';
ca4e0257 8375 using_gprs = *args == 'F' || *args == 'f';
252b5132
RH
8376
8377 save_in = input_line_pointer;
8378 input_line_pointer = s;
8379 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
8380 length = len;
8381 s = input_line_pointer;
8382 input_line_pointer = save_in;
8383 if (err != NULL && *err != '\0')
8384 {
8385 as_bad (_("Bad floating point constant: %s"), err);
8386 memset (temp, '\0', sizeof temp);
8387 length = f64 ? 8 : 4;
8388 }
8389
156c2f8b 8390 assert (length == (unsigned) (f64 ? 8 : 4));
252b5132
RH
8391
8392 if (*args == 'f'
8393 || (*args == 'l'
8394 && (! USE_GLOBAL_POINTER_OPT
8395 || mips_pic == EMBEDDED_PIC
8396 || g_switch_value < 4
8397 || (temp[0] == 0 && temp[1] == 0)
8398 || (temp[2] == 0 && temp[3] == 0))))
8399 {
8400 imm_expr.X_op = O_constant;
8401 if (! target_big_endian)
8402 imm_expr.X_add_number = bfd_getl32 (temp);
8403 else
8404 imm_expr.X_add_number = bfd_getb32 (temp);
8405 }
8406 else if (length > 4
119d663a 8407 && ! mips_disable_float_construction
ca4e0257
RS
8408 /* Constants can only be constructed in GPRs and
8409 copied to FPRs if the GPRs are at least as wide
8410 as the FPRs. Force the constant into memory if
8411 we are using 64-bit FPRs but the GPRs are only
8412 32 bits wide. */
8413 && (using_gprs
8414 || ! (HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
252b5132
RH
8415 && ((temp[0] == 0 && temp[1] == 0)
8416 || (temp[2] == 0 && temp[3] == 0))
8417 && ((temp[4] == 0 && temp[5] == 0)
8418 || (temp[6] == 0 && temp[7] == 0)))
8419 {
ca4e0257
RS
8420 /* The value is simple enough to load with a couple of
8421 instructions. If using 32-bit registers, set
8422 imm_expr to the high order 32 bits and offset_expr to
8423 the low order 32 bits. Otherwise, set imm_expr to
8424 the entire 64 bit constant. */
8425 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
252b5132
RH
8426 {
8427 imm_expr.X_op = O_constant;
8428 offset_expr.X_op = O_constant;
8429 if (! target_big_endian)
8430 {
8431 imm_expr.X_add_number = bfd_getl32 (temp + 4);
8432 offset_expr.X_add_number = bfd_getl32 (temp);
8433 }
8434 else
8435 {
8436 imm_expr.X_add_number = bfd_getb32 (temp);
8437 offset_expr.X_add_number = bfd_getb32 (temp + 4);
8438 }
8439 if (offset_expr.X_add_number == 0)
8440 offset_expr.X_op = O_absent;
8441 }
8442 else if (sizeof (imm_expr.X_add_number) > 4)
8443 {
8444 imm_expr.X_op = O_constant;
8445 if (! target_big_endian)
8446 imm_expr.X_add_number = bfd_getl64 (temp);
8447 else
8448 imm_expr.X_add_number = bfd_getb64 (temp);
8449 }
8450 else
8451 {
8452 imm_expr.X_op = O_big;
8453 imm_expr.X_add_number = 4;
8454 if (! target_big_endian)
8455 {
8456 generic_bignum[0] = bfd_getl16 (temp);
8457 generic_bignum[1] = bfd_getl16 (temp + 2);
8458 generic_bignum[2] = bfd_getl16 (temp + 4);
8459 generic_bignum[3] = bfd_getl16 (temp + 6);
8460 }
8461 else
8462 {
8463 generic_bignum[0] = bfd_getb16 (temp + 6);
8464 generic_bignum[1] = bfd_getb16 (temp + 4);
8465 generic_bignum[2] = bfd_getb16 (temp + 2);
8466 generic_bignum[3] = bfd_getb16 (temp);
8467 }
8468 }
8469 }
8470 else
8471 {
8472 const char *newname;
8473 segT new_seg;
8474
8475 /* Switch to the right section. */
8476 seg = now_seg;
8477 subseg = now_subseg;
8478 switch (*args)
8479 {
8480 default: /* unused default case avoids warnings. */
8481 case 'L':
8482 newname = RDATA_SECTION_NAME;
bb2d6cd7
GK
8483 if ((USE_GLOBAL_POINTER_OPT && g_switch_value >= 8)
8484 || mips_pic == EMBEDDED_PIC)
252b5132
RH
8485 newname = ".lit8";
8486 break;
8487 case 'F':
bb2d6cd7
GK
8488 if (mips_pic == EMBEDDED_PIC)
8489 newname = ".lit8";
8490 else
8491 newname = RDATA_SECTION_NAME;
252b5132
RH
8492 break;
8493 case 'l':
8494 assert (!USE_GLOBAL_POINTER_OPT
8495 || g_switch_value >= 4);
8496 newname = ".lit4";
8497 break;
8498 }
8499 new_seg = subseg_new (newname, (subsegT) 0);
8500 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
8501 bfd_set_section_flags (stdoutput, new_seg,
8502 (SEC_ALLOC
8503 | SEC_LOAD
8504 | SEC_READONLY
8505 | SEC_DATA));
8506 frag_align (*args == 'l' ? 2 : 3, 0, 0);
8507 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
8508 && strcmp (TARGET_OS, "elf") != 0)
8509 record_alignment (new_seg, 4);
8510 else
8511 record_alignment (new_seg, *args == 'l' ? 2 : 3);
8512 if (seg == now_seg)
8513 as_bad (_("Can't use floating point insn in this section"));
8514
8515 /* Set the argument to the current address in the
8516 section. */
8517 offset_expr.X_op = O_symbol;
8518 offset_expr.X_add_symbol =
8519 symbol_new ("L0\001", now_seg,
8520 (valueT) frag_now_fix (), frag_now);
8521 offset_expr.X_add_number = 0;
8522
8523 /* Put the floating point number into the section. */
8524 p = frag_more ((int) length);
8525 memcpy (p, temp, length);
8526
8527 /* Switch back to the original section. */
8528 subseg_set (seg, subseg);
8529 }
8530 }
8531 continue;
8532
8533 case 'i': /* 16 bit unsigned immediate */
8534 case 'j': /* 16 bit signed immediate */
f6688943 8535 *imm_reloc = BFD_RELOC_LO16;
252b5132 8536 c = my_getSmallExpression (&imm_expr, s);
fb1b3232 8537 if (c != S_EX_NONE)
252b5132 8538 {
fb1b3232 8539 if (c != S_EX_LO)
252b5132 8540 {
fdb987ee
RS
8541 if (c == S_EX_HI)
8542 {
8543 *imm_reloc = BFD_RELOC_HI16_S;
8544 imm_unmatched_hi = true;
8545 }
ad8d3bb3 8546#ifdef OBJ_ELF
fb1b3232 8547 else if (c == S_EX_HIGHEST)
98d3f06f 8548 *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
fb1b3232 8549 else if (c == S_EX_HIGHER)
98d3f06f 8550 *imm_reloc = BFD_RELOC_MIPS_HIGHER;
ad8d3bb3
TS
8551 else if (c == S_EX_GP_REL)
8552 {
8553 /* This occurs in NewABI only. */
8554 c = my_getSmallExpression (&imm_expr, s);
8555 if (c != S_EX_NEG)
8556 as_bad (_("bad composition of relocations"));
8557 else
8558 {
8559 c = my_getSmallExpression (&imm_expr, s);
8560 if (c != S_EX_LO)
8561 as_bad (_("bad composition of relocations"));
8562 else
8563 {
8564 imm_reloc[0] = BFD_RELOC_GPREL16;
8565 imm_reloc[1] = BFD_RELOC_MIPS_SUB;
8566 imm_reloc[2] = BFD_RELOC_LO16;
8567 }
8568 }
8569 }
8570#endif
252b5132 8571 else
f6688943 8572 *imm_reloc = BFD_RELOC_HI16;
252b5132
RH
8573 }
8574 else if (imm_expr.X_op == O_constant)
8575 imm_expr.X_add_number &= 0xffff;
8576 }
8577 if (*args == 'i')
8578 {
fb1b3232 8579 if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
252b5132 8580 || ((imm_expr.X_add_number < 0
beae10d5
KH
8581 || imm_expr.X_add_number >= 0x10000)
8582 && imm_expr.X_op == O_constant))
252b5132
RH
8583 {
8584 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8585 !strcmp (insn->name, insn[1].name))
8586 break;
2ae7e77b
AH
8587 if (imm_expr.X_op == O_constant
8588 || imm_expr.X_op == O_big)
252b5132
RH
8589 as_bad (_("16 bit expression not in range 0..65535"));
8590 }
8591 }
8592 else
8593 {
8594 int more;
8595 offsetT max;
8596
8597 /* The upper bound should be 0x8000, but
8598 unfortunately the MIPS assembler accepts numbers
8599 from 0x8000 to 0xffff and sign extends them, and
8600 we want to be compatible. We only permit this
8601 extended range for an instruction which does not
8602 provide any further alternates, since those
8603 alternates may handle other cases. People should
8604 use the numbers they mean, rather than relying on
8605 a mysterious sign extension. */
8606 more = (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8607 strcmp (insn->name, insn[1].name) == 0);
8608 if (more)
8609 max = 0x8000;
8610 else
8611 max = 0x10000;
fb1b3232 8612 if ((c == S_EX_NONE && imm_expr.X_op != O_constant)
252b5132 8613 || ((imm_expr.X_add_number < -0x8000
beae10d5
KH
8614 || imm_expr.X_add_number >= max)
8615 && imm_expr.X_op == O_constant)
252b5132
RH
8616 || (more
8617 && imm_expr.X_add_number < 0
ca4e0257 8618 && HAVE_64BIT_GPRS
252b5132
RH
8619 && imm_expr.X_unsigned
8620 && sizeof (imm_expr.X_add_number) <= 4))
8621 {
8622 if (more)
8623 break;
2ae7e77b
AH
8624 if (imm_expr.X_op == O_constant
8625 || imm_expr.X_op == O_big)
252b5132
RH
8626 as_bad (_("16 bit expression not in range -32768..32767"));
8627 }
8628 }
8629 s = expr_end;
8630 continue;
8631
8632 case 'o': /* 16 bit offset */
8633 c = my_getSmallExpression (&offset_expr, s);
8634
8635 /* If this value won't fit into a 16 bit offset, then go
8636 find a macro that will generate the 32 bit offset
afdbd6d0 8637 code pattern. */
fb1b3232 8638 if (c == S_EX_NONE
252b5132
RH
8639 && (offset_expr.X_op != O_constant
8640 || offset_expr.X_add_number >= 0x8000
afdbd6d0 8641 || offset_expr.X_add_number < -0x8000))
252b5132
RH
8642 break;
8643
fb1b3232 8644 if (c == S_EX_HI)
252b5132
RH
8645 {
8646 if (offset_expr.X_op != O_constant)
8647 break;
8648 offset_expr.X_add_number =
8649 (offset_expr.X_add_number >> 16) & 0xffff;
8650 }
f6688943 8651 *offset_reloc = BFD_RELOC_LO16;
252b5132
RH
8652 s = expr_end;
8653 continue;
8654
8655 case 'p': /* pc relative offset */
cb56d3d3 8656 if (mips_pic == EMBEDDED_PIC)
f6688943 8657 *offset_reloc = BFD_RELOC_16_PCREL_S2;
cb56d3d3 8658 else
f6688943 8659 *offset_reloc = BFD_RELOC_16_PCREL;
252b5132
RH
8660 my_getExpression (&offset_expr, s);
8661 s = expr_end;
8662 continue;
8663
8664 case 'u': /* upper 16 bits */
8665 c = my_getSmallExpression (&imm_expr, s);
f6688943 8666 *imm_reloc = BFD_RELOC_LO16;
e7d556df 8667 if (c != S_EX_NONE)
252b5132 8668 {
fb1b3232 8669 if (c != S_EX_LO)
252b5132 8670 {
fdb987ee 8671 if (c == S_EX_HI)
252b5132 8672 {
f6688943 8673 *imm_reloc = BFD_RELOC_HI16_S;
252b5132
RH
8674 imm_unmatched_hi = true;
8675 }
645dc66c
TS
8676#ifdef OBJ_ELF
8677 else if (c == S_EX_HIGHEST)
98d3f06f 8678 *imm_reloc = BFD_RELOC_MIPS_HIGHEST;
ad8d3bb3
TS
8679 else if (c == S_EX_GP_REL)
8680 {
8681 /* This occurs in NewABI only. */
8682 c = my_getSmallExpression (&imm_expr, s);
8683 if (c != S_EX_NEG)
8684 as_bad (_("bad composition of relocations"));
8685 else
8686 {
8687 c = my_getSmallExpression (&imm_expr, s);
8688 if (c != S_EX_HI)
8689 as_bad (_("bad composition of relocations"));
8690 else
8691 {
8692 imm_reloc[0] = BFD_RELOC_GPREL16;
8693 imm_reloc[1] = BFD_RELOC_MIPS_SUB;
8694 imm_reloc[2] = BFD_RELOC_HI16_S;
8695 }
8696 }
8697 }
8698#endif
252b5132 8699 else
f6688943 8700 *imm_reloc = BFD_RELOC_HI16;
252b5132
RH
8701 }
8702 else if (imm_expr.X_op == O_constant)
8703 imm_expr.X_add_number &= 0xffff;
8704 }
fdb987ee
RS
8705 else if (imm_expr.X_op == O_constant
8706 && (imm_expr.X_add_number < 0
8707 || imm_expr.X_add_number >= 0x10000))
252b5132
RH
8708 as_bad (_("lui expression not in range 0..65535"));
8709 s = expr_end;
8710 continue;
8711
8712 case 'a': /* 26 bit address */
8713 my_getExpression (&offset_expr, s);
8714 s = expr_end;
f6688943 8715 *offset_reloc = BFD_RELOC_MIPS_JMP;
252b5132
RH
8716 continue;
8717
8718 case 'N': /* 3 bit branch condition code */
8719 case 'M': /* 3 bit compare condition code */
8720 if (strncmp (s, "$fcc", 4) != 0)
8721 break;
8722 s += 4;
8723 regno = 0;
8724 do
8725 {
8726 regno *= 10;
8727 regno += *s - '0';
8728 ++s;
8729 }
3882b010 8730 while (ISDIGIT (*s));
252b5132
RH
8731 if (regno > 7)
8732 as_bad (_("invalid condition code register $fcc%d"), regno);
8733 if (*args == 'N')
8734 ip->insn_opcode |= regno << OP_SH_BCC;
8735 else
8736 ip->insn_opcode |= regno << OP_SH_CCC;
beae10d5 8737 continue;
252b5132 8738
156c2f8b
NC
8739 case 'H':
8740 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
8741 s += 2;
3882b010 8742 if (ISDIGIT (*s))
156c2f8b
NC
8743 {
8744 c = 0;
8745 do
8746 {
8747 c *= 10;
8748 c += *s - '0';
8749 ++s;
8750 }
3882b010 8751 while (ISDIGIT (*s));
156c2f8b
NC
8752 }
8753 else
8754 c = 8; /* Invalid sel value. */
8755
8756 if (c > 7)
8757 as_bad (_("invalid coprocessor sub-selection value (0-7)"));
8758 ip->insn_opcode |= c;
8759 continue;
8760
252b5132
RH
8761 default:
8762 as_bad (_("bad char = '%c'\n"), *args);
8763 internalError ();
8764 }
8765 break;
8766 }
8767 /* Args don't match. */
8768 if (insn + 1 < &mips_opcodes[NUMOPCODES] &&
8769 !strcmp (insn->name, insn[1].name))
8770 {
8771 ++insn;
8772 s = argsStart;
268f6bed 8773 insn_error = _("illegal operands");
252b5132
RH
8774 continue;
8775 }
268f6bed
L
8776 if (save_c)
8777 *(--s) = save_c;
252b5132
RH
8778 insn_error = _("illegal operands");
8779 return;
8780 }
8781}
8782
8783/* This routine assembles an instruction into its binary format when
8784 assembling for the mips16. As a side effect, it sets one of the
8785 global variables imm_reloc or offset_reloc to the type of
8786 relocation to do if one of the operands is an address expression.
8787 It also sets mips16_small and mips16_ext if the user explicitly
8788 requested a small or extended instruction. */
8789
8790static void
8791mips16_ip (str, ip)
8792 char *str;
8793 struct mips_cl_insn *ip;
8794{
8795 char *s;
8796 const char *args;
8797 struct mips_opcode *insn;
8798 char *argsstart;
8799 unsigned int regno;
8800 unsigned int lastregno = 0;
8801 char *s_reset;
8802
8803 insn_error = NULL;
8804
8805 mips16_small = false;
8806 mips16_ext = false;
8807
3882b010 8808 for (s = str; ISLOWER (*s); ++s)
252b5132
RH
8809 ;
8810 switch (*s)
8811 {
8812 case '\0':
8813 break;
8814
8815 case ' ':
8816 *s++ = '\0';
8817 break;
8818
8819 case '.':
8820 if (s[1] == 't' && s[2] == ' ')
8821 {
8822 *s = '\0';
8823 mips16_small = true;
8824 s += 3;
8825 break;
8826 }
8827 else if (s[1] == 'e' && s[2] == ' ')
8828 {
8829 *s = '\0';
8830 mips16_ext = true;
8831 s += 3;
8832 break;
8833 }
8834 /* Fall through. */
8835 default:
8836 insn_error = _("unknown opcode");
8837 return;
8838 }
8839
8840 if (mips_opts.noautoextend && ! mips16_ext)
8841 mips16_small = true;
8842
8843 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
8844 {
8845 insn_error = _("unrecognized opcode");
8846 return;
8847 }
8848
8849 argsstart = s;
8850 for (;;)
8851 {
8852 assert (strcmp (insn->name, str) == 0);
8853
8854 ip->insn_mo = insn;
8855 ip->insn_opcode = insn->match;
8856 ip->use_extend = false;
8857 imm_expr.X_op = O_absent;
f6688943
TS
8858 imm_reloc[0] = BFD_RELOC_UNUSED;
8859 imm_reloc[1] = BFD_RELOC_UNUSED;
8860 imm_reloc[2] = BFD_RELOC_UNUSED;
252b5132 8861 offset_expr.X_op = O_absent;
f6688943
TS
8862 offset_reloc[0] = BFD_RELOC_UNUSED;
8863 offset_reloc[1] = BFD_RELOC_UNUSED;
8864 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
8865 for (args = insn->args; 1; ++args)
8866 {
8867 int c;
8868
8869 if (*s == ' ')
8870 ++s;
8871
8872 /* In this switch statement we call break if we did not find
8873 a match, continue if we did find a match, or return if we
8874 are done. */
8875
8876 c = *args;
8877 switch (c)
8878 {
8879 case '\0':
8880 if (*s == '\0')
8881 {
8882 /* Stuff the immediate value in now, if we can. */
8883 if (imm_expr.X_op == O_constant
f6688943 8884 && *imm_reloc > BFD_RELOC_UNUSED
252b5132
RH
8885 && insn->pinfo != INSN_MACRO)
8886 {
c4e7957c 8887 mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
252b5132
RH
8888 imm_expr.X_add_number, true, mips16_small,
8889 mips16_ext, &ip->insn_opcode,
8890 &ip->use_extend, &ip->extend);
8891 imm_expr.X_op = O_absent;
f6688943 8892 *imm_reloc = BFD_RELOC_UNUSED;
252b5132
RH
8893 }
8894
8895 return;
8896 }
8897 break;
8898
8899 case ',':
8900 if (*s++ == c)
8901 continue;
8902 s--;
8903 switch (*++args)
8904 {
8905 case 'v':
8906 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
8907 continue;
8908 case 'w':
8909 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
8910 continue;
8911 }
8912 break;
8913
8914 case '(':
8915 case ')':
8916 if (*s++ == c)
8917 continue;
8918 break;
8919
8920 case 'v':
8921 case 'w':
8922 if (s[0] != '$')
8923 {
8924 if (c == 'v')
8925 ip->insn_opcode |= lastregno << MIPS16OP_SH_RX;
8926 else
8927 ip->insn_opcode |= lastregno << MIPS16OP_SH_RY;
8928 ++args;
8929 continue;
8930 }
8931 /* Fall through. */
8932 case 'x':
8933 case 'y':
8934 case 'z':
8935 case 'Z':
8936 case '0':
8937 case 'S':
8938 case 'R':
8939 case 'X':
8940 case 'Y':
8941 if (s[0] != '$')
8942 break;
8943 s_reset = s;
3882b010 8944 if (ISDIGIT (s[1]))
252b5132
RH
8945 {
8946 ++s;
8947 regno = 0;
8948 do
8949 {
8950 regno *= 10;
8951 regno += *s - '0';
8952 ++s;
8953 }
3882b010 8954 while (ISDIGIT (*s));
252b5132
RH
8955 if (regno > 31)
8956 {
8957 as_bad (_("invalid register number (%d)"), regno);
8958 regno = 2;
8959 }
8960 }
8961 else
8962 {
76db943d
TS
8963 if (s[1] == 'r' && s[2] == 'a')
8964 {
8965 s += 3;
8966 regno = RA;
8967 }
8968 else if (s[1] == 'f' && s[2] == 'p')
252b5132
RH
8969 {
8970 s += 3;
8971 regno = FP;
8972 }
8973 else if (s[1] == 's' && s[2] == 'p')
8974 {
8975 s += 3;
8976 regno = SP;
8977 }
8978 else if (s[1] == 'g' && s[2] == 'p')
8979 {
8980 s += 3;
8981 regno = GP;
8982 }
8983 else if (s[1] == 'a' && s[2] == 't')
8984 {
8985 s += 3;
8986 regno = AT;
8987 }
8988 else if (s[1] == 'k' && s[2] == 't' && s[3] == '0')
8989 {
8990 s += 4;
8991 regno = KT0;
8992 }
8993 else if (s[1] == 'k' && s[2] == 't' && s[3] == '1')
8994 {
8995 s += 4;
8996 regno = KT1;
8997 }
85b51719
TS
8998 else if (s[1] == 'z' && s[2] == 'e' && s[3] == 'r' && s[4] == 'o')
8999 {
9000 s += 5;
9001 regno = ZERO;
9002 }
252b5132
RH
9003 else
9004 break;
9005 }
9006
9007 if (*s == ' ')
9008 ++s;
9009 if (args[1] != *s)
9010 {
9011 if (c == 'v' || c == 'w')
9012 {
9013 regno = mips16_to_32_reg_map[lastregno];
9014 s = s_reset;
f9419b05 9015 ++args;
252b5132
RH
9016 }
9017 }
9018
9019 switch (c)
9020 {
9021 case 'x':
9022 case 'y':
9023 case 'z':
9024 case 'v':
9025 case 'w':
9026 case 'Z':
9027 regno = mips32_to_16_reg_map[regno];
9028 break;
9029
9030 case '0':
9031 if (regno != 0)
9032 regno = ILLEGAL_REG;
9033 break;
9034
9035 case 'S':
9036 if (regno != SP)
9037 regno = ILLEGAL_REG;
9038 break;
9039
9040 case 'R':
9041 if (regno != RA)
9042 regno = ILLEGAL_REG;
9043 break;
9044
9045 case 'X':
9046 case 'Y':
9047 if (regno == AT && ! mips_opts.noat)
9048 as_warn (_("used $at without \".set noat\""));
9049 break;
9050
9051 default:
9052 internalError ();
9053 }
9054
9055 if (regno == ILLEGAL_REG)
9056 break;
9057
9058 switch (c)
9059 {
9060 case 'x':
9061 case 'v':
9062 ip->insn_opcode |= regno << MIPS16OP_SH_RX;
9063 break;
9064 case 'y':
9065 case 'w':
9066 ip->insn_opcode |= regno << MIPS16OP_SH_RY;
9067 break;
9068 case 'z':
9069 ip->insn_opcode |= regno << MIPS16OP_SH_RZ;
9070 break;
9071 case 'Z':
9072 ip->insn_opcode |= regno << MIPS16OP_SH_MOVE32Z;
9073 case '0':
9074 case 'S':
9075 case 'R':
9076 break;
9077 case 'X':
9078 ip->insn_opcode |= regno << MIPS16OP_SH_REGR32;
9079 break;
9080 case 'Y':
9081 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
9082 ip->insn_opcode |= regno << MIPS16OP_SH_REG32R;
9083 break;
9084 default:
9085 internalError ();
9086 }
9087
9088 lastregno = regno;
9089 continue;
9090
9091 case 'P':
9092 if (strncmp (s, "$pc", 3) == 0)
9093 {
9094 s += 3;
9095 continue;
9096 }
9097 break;
9098
9099 case '<':
9100 case '>':
9101 case '[':
9102 case ']':
9103 case '4':
9104 case '5':
9105 case 'H':
9106 case 'W':
9107 case 'D':
9108 case 'j':
9109 case '8':
9110 case 'V':
9111 case 'C':
9112 case 'U':
9113 case 'k':
9114 case 'K':
9115 if (s[0] == '%'
9116 && strncmp (s + 1, "gprel(", sizeof "gprel(" - 1) == 0)
9117 {
9118 /* This is %gprel(SYMBOL). We need to read SYMBOL,
9119 and generate the appropriate reloc. If the text
9120 inside %gprel is not a symbol name with an
9121 optional offset, then we generate a normal reloc
9122 and will probably fail later. */
9123 my_getExpression (&imm_expr, s + sizeof "%gprel" - 1);
9124 if (imm_expr.X_op == O_symbol)
9125 {
9126 mips16_ext = true;
f6688943 9127 *imm_reloc = BFD_RELOC_MIPS16_GPREL;
252b5132
RH
9128 s = expr_end;
9129 ip->use_extend = true;
9130 ip->extend = 0;
9131 continue;
9132 }
9133 }
9134 else
9135 {
9136 /* Just pick up a normal expression. */
9137 my_getExpression (&imm_expr, s);
9138 }
9139
9140 if (imm_expr.X_op == O_register)
9141 {
9142 /* What we thought was an expression turned out to
9143 be a register. */
9144
9145 if (s[0] == '(' && args[1] == '(')
9146 {
9147 /* It looks like the expression was omitted
9148 before a register indirection, which means
9149 that the expression is implicitly zero. We
9150 still set up imm_expr, so that we handle
9151 explicit extensions correctly. */
9152 imm_expr.X_op = O_constant;
9153 imm_expr.X_add_number = 0;
f6688943 9154 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
9155 continue;
9156 }
9157
9158 break;
9159 }
9160
9161 /* We need to relax this instruction. */
f6688943 9162 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
9163 s = expr_end;
9164 continue;
9165
9166 case 'p':
9167 case 'q':
9168 case 'A':
9169 case 'B':
9170 case 'E':
9171 /* We use offset_reloc rather than imm_reloc for the PC
9172 relative operands. This lets macros with both
9173 immediate and address operands work correctly. */
9174 my_getExpression (&offset_expr, s);
9175
9176 if (offset_expr.X_op == O_register)
9177 break;
9178
9179 /* We need to relax this instruction. */
f6688943 9180 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
9181 s = expr_end;
9182 continue;
9183
9184 case '6': /* break code */
9185 my_getExpression (&imm_expr, s);
9186 check_absolute_expr (ip, &imm_expr);
9187 if ((unsigned long) imm_expr.X_add_number > 63)
9188 {
9189 as_warn (_("Invalid value for `%s' (%lu)"),
9190 ip->insn_mo->name,
9191 (unsigned long) imm_expr.X_add_number);
9192 imm_expr.X_add_number &= 0x3f;
9193 }
9194 ip->insn_opcode |= imm_expr.X_add_number << MIPS16OP_SH_IMM6;
9195 imm_expr.X_op = O_absent;
9196 s = expr_end;
9197 continue;
9198
9199 case 'a': /* 26 bit address */
9200 my_getExpression (&offset_expr, s);
9201 s = expr_end;
f6688943 9202 *offset_reloc = BFD_RELOC_MIPS16_JMP;
252b5132
RH
9203 ip->insn_opcode <<= 16;
9204 continue;
9205
9206 case 'l': /* register list for entry macro */
9207 case 'L': /* register list for exit macro */
9208 {
9209 int mask;
9210
9211 if (c == 'l')
9212 mask = 0;
9213 else
9214 mask = 7 << 3;
9215 while (*s != '\0')
9216 {
9217 int freg, reg1, reg2;
9218
9219 while (*s == ' ' || *s == ',')
9220 ++s;
9221 if (*s != '$')
9222 {
9223 as_bad (_("can't parse register list"));
9224 break;
9225 }
9226 ++s;
9227 if (*s != 'f')
9228 freg = 0;
9229 else
9230 {
9231 freg = 1;
9232 ++s;
9233 }
9234 reg1 = 0;
3882b010 9235 while (ISDIGIT (*s))
252b5132
RH
9236 {
9237 reg1 *= 10;
9238 reg1 += *s - '0';
9239 ++s;
9240 }
9241 if (*s == ' ')
9242 ++s;
9243 if (*s != '-')
9244 reg2 = reg1;
9245 else
9246 {
9247 ++s;
9248 if (*s != '$')
9249 break;
9250 ++s;
9251 if (freg)
9252 {
9253 if (*s == 'f')
9254 ++s;
9255 else
9256 {
9257 as_bad (_("invalid register list"));
9258 break;
9259 }
9260 }
9261 reg2 = 0;
3882b010 9262 while (ISDIGIT (*s))
252b5132
RH
9263 {
9264 reg2 *= 10;
9265 reg2 += *s - '0';
9266 ++s;
9267 }
9268 }
9269 if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
9270 {
9271 mask &= ~ (7 << 3);
9272 mask |= 5 << 3;
9273 }
9274 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
9275 {
9276 mask &= ~ (7 << 3);
9277 mask |= 6 << 3;
9278 }
9279 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
9280 mask |= (reg2 - 3) << 3;
9281 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
9282 mask |= (reg2 - 15) << 1;
f9419b05 9283 else if (reg1 == RA && reg2 == RA)
252b5132
RH
9284 mask |= 1;
9285 else
9286 {
9287 as_bad (_("invalid register list"));
9288 break;
9289 }
9290 }
9291 /* The mask is filled in in the opcode table for the
9292 benefit of the disassembler. We remove it before
9293 applying the actual mask. */
9294 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
9295 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
9296 }
9297 continue;
9298
9299 case 'e': /* extend code */
9300 my_getExpression (&imm_expr, s);
9301 check_absolute_expr (ip, &imm_expr);
9302 if ((unsigned long) imm_expr.X_add_number > 0x7ff)
9303 {
9304 as_warn (_("Invalid value for `%s' (%lu)"),
9305 ip->insn_mo->name,
9306 (unsigned long) imm_expr.X_add_number);
9307 imm_expr.X_add_number &= 0x7ff;
9308 }
9309 ip->insn_opcode |= imm_expr.X_add_number;
9310 imm_expr.X_op = O_absent;
9311 s = expr_end;
9312 continue;
9313
9314 default:
9315 internalError ();
9316 }
9317 break;
9318 }
9319
9320 /* Args don't match. */
9321 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
9322 strcmp (insn->name, insn[1].name) == 0)
9323 {
9324 ++insn;
9325 s = argsstart;
9326 continue;
9327 }
9328
9329 insn_error = _("illegal operands");
9330
9331 return;
9332 }
9333}
9334
9335/* This structure holds information we know about a mips16 immediate
9336 argument type. */
9337
e972090a
NC
9338struct mips16_immed_operand
9339{
252b5132
RH
9340 /* The type code used in the argument string in the opcode table. */
9341 int type;
9342 /* The number of bits in the short form of the opcode. */
9343 int nbits;
9344 /* The number of bits in the extended form of the opcode. */
9345 int extbits;
9346 /* The amount by which the short form is shifted when it is used;
9347 for example, the sw instruction has a shift count of 2. */
9348 int shift;
9349 /* The amount by which the short form is shifted when it is stored
9350 into the instruction code. */
9351 int op_shift;
9352 /* Non-zero if the short form is unsigned. */
9353 int unsp;
9354 /* Non-zero if the extended form is unsigned. */
9355 int extu;
9356 /* Non-zero if the value is PC relative. */
9357 int pcrel;
9358};
9359
9360/* The mips16 immediate operand types. */
9361
9362static const struct mips16_immed_operand mips16_immed_operands[] =
9363{
9364 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9365 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9366 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
9367 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 },
9368 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
9369 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
9370 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
9371 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
9372 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
9373 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
9374 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
9375 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
9376 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
9377 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
9378 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
9379 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
9380 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9381 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
9382 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
9383 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
9384 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
9385};
9386
9387#define MIPS16_NUM_IMMED \
9388 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
9389
9390/* Handle a mips16 instruction with an immediate value. This or's the
9391 small immediate value into *INSN. It sets *USE_EXTEND to indicate
9392 whether an extended value is needed; if one is needed, it sets
9393 *EXTEND to the value. The argument type is TYPE. The value is VAL.
9394 If SMALL is true, an unextended opcode was explicitly requested.
9395 If EXT is true, an extended opcode was explicitly requested. If
9396 WARN is true, warn if EXT does not match reality. */
9397
9398static void
9399mips16_immed (file, line, type, val, warn, small, ext, insn, use_extend,
9400 extend)
9401 char *file;
9402 unsigned int line;
9403 int type;
9404 offsetT val;
9405 boolean warn;
9406 boolean small;
9407 boolean ext;
9408 unsigned long *insn;
9409 boolean *use_extend;
9410 unsigned short *extend;
9411{
9412 register const struct mips16_immed_operand *op;
9413 int mintiny, maxtiny;
9414 boolean needext;
9415
9416 op = mips16_immed_operands;
9417 while (op->type != type)
9418 {
9419 ++op;
9420 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
9421 }
9422
9423 if (op->unsp)
9424 {
9425 if (type == '<' || type == '>' || type == '[' || type == ']')
9426 {
9427 mintiny = 1;
9428 maxtiny = 1 << op->nbits;
9429 }
9430 else
9431 {
9432 mintiny = 0;
9433 maxtiny = (1 << op->nbits) - 1;
9434 }
9435 }
9436 else
9437 {
9438 mintiny = - (1 << (op->nbits - 1));
9439 maxtiny = (1 << (op->nbits - 1)) - 1;
9440 }
9441
9442 /* Branch offsets have an implicit 0 in the lowest bit. */
9443 if (type == 'p' || type == 'q')
9444 val /= 2;
9445
9446 if ((val & ((1 << op->shift) - 1)) != 0
9447 || val < (mintiny << op->shift)
9448 || val > (maxtiny << op->shift))
9449 needext = true;
9450 else
9451 needext = false;
9452
9453 if (warn && ext && ! needext)
beae10d5
KH
9454 as_warn_where (file, line,
9455 _("extended operand requested but not required"));
252b5132
RH
9456 if (small && needext)
9457 as_bad_where (file, line, _("invalid unextended operand value"));
9458
9459 if (small || (! ext && ! needext))
9460 {
9461 int insnval;
9462
9463 *use_extend = false;
9464 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
9465 insnval <<= op->op_shift;
9466 *insn |= insnval;
9467 }
9468 else
9469 {
9470 long minext, maxext;
9471 int extval;
9472
9473 if (op->extu)
9474 {
9475 minext = 0;
9476 maxext = (1 << op->extbits) - 1;
9477 }
9478 else
9479 {
9480 minext = - (1 << (op->extbits - 1));
9481 maxext = (1 << (op->extbits - 1)) - 1;
9482 }
9483 if (val < minext || val > maxext)
9484 as_bad_where (file, line,
9485 _("operand value out of range for instruction"));
9486
9487 *use_extend = true;
9488 if (op->extbits == 16)
9489 {
9490 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
9491 val &= 0x1f;
9492 }
9493 else if (op->extbits == 15)
9494 {
9495 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
9496 val &= 0xf;
9497 }
9498 else
9499 {
9500 extval = ((val & 0x1f) << 6) | (val & 0x20);
9501 val = 0;
9502 }
9503
9504 *extend = (unsigned short) extval;
9505 *insn |= val;
9506 }
9507}
9508\f
ad8d3bb3
TS
9509static struct percent_op_match
9510{
9511 const char *str;
9512 const enum small_ex_type type;
9513} percent_op[] =
9514{
ad8d3bb3
TS
9515 {"%lo", S_EX_LO},
9516#ifdef OBJ_ELF
394f9b3a
TS
9517 {"%call_hi", S_EX_CALL_HI},
9518 {"%call_lo", S_EX_CALL_LO},
ad8d3bb3
TS
9519 {"%call16", S_EX_CALL16},
9520 {"%got_disp", S_EX_GOT_DISP},
9521 {"%got_page", S_EX_GOT_PAGE},
9522 {"%got_ofst", S_EX_GOT_OFST},
9523 {"%got_hi", S_EX_GOT_HI},
9524 {"%got_lo", S_EX_GOT_LO},
394f9b3a
TS
9525 {"%got", S_EX_GOT},
9526 {"%gp_rel", S_EX_GP_REL},
9527 {"%half", S_EX_HALF},
ad8d3bb3 9528 {"%highest", S_EX_HIGHEST},
394f9b3a
TS
9529 {"%higher", S_EX_HIGHER},
9530 {"%neg", S_EX_NEG},
ad8d3bb3 9531#endif
394f9b3a 9532 {"%hi", S_EX_HI}
ad8d3bb3
TS
9533};
9534
9535/* Parse small expression input. STR gets adjusted to eat up whitespace.
9536 It detects valid "%percent_op(...)" and "($reg)" strings. Percent_op's
9537 can be nested, this is handled by blanking the innermost, parsing the
9538 rest by subsequent calls. */
252b5132
RH
9539
9540static int
ad8d3bb3
TS
9541my_getSmallParser (str, len, nestlevel)
9542 char **str;
9543 unsigned int *len;
9544 int *nestlevel;
252b5132 9545{
ad8d3bb3
TS
9546 *len = 0;
9547 *str += strspn (*str, " \t");
394f9b3a 9548 /* Check for expression in parentheses. */
ad8d3bb3 9549 if (**str == '(')
252b5132 9550 {
ad8d3bb3
TS
9551 char *b = *str + 1 + strspn (*str + 1, " \t");
9552 char *e;
9553
9554 /* Check for base register. */
9555 if (b[0] == '$')
9556 {
9557 if (strchr (b, ')')
9558 && (e = b + strcspn (b, ") \t"))
9559 && e - b > 1 && e - b < 4)
9560 {
98d3f06f
KH
9561 if ((e - b == 3
9562 && ((b[1] == 'f' && b[2] == 'p')
9563 || (b[1] == 's' && b[2] == 'p')
9564 || (b[1] == 'g' && b[2] == 'p')
9565 || (b[1] == 'a' && b[2] == 't')
9566 || (ISDIGIT (b[1])
9567 && ISDIGIT (b[2]))))
9568 || (ISDIGIT (b[1])))
9569 {
9570 *len = strcspn (*str, ")") + 1;
9571 return S_EX_REGISTER;
9572 }
ad8d3bb3
TS
9573 }
9574 }
394f9b3a 9575 /* Check for percent_op (in parentheses). */
ad8d3bb3
TS
9576 else if (b[0] == '%')
9577 {
9578 *str = b;
394f9b3a 9579 return my_getPercentOp (str, len, nestlevel);
ad8d3bb3 9580 }
76b3015f 9581
394f9b3a
TS
9582 /* Some other expression in the parentheses, which can contain
9583 parentheses itself. Attempt to find the matching one. */
9584 {
9585 int pcnt = 1;
9586 char *s;
9587
9588 *len = 1;
9589 for (s = *str + 1; *s && pcnt; s++, (*len)++)
9590 {
9591 if (*s == '(')
f9419b05 9592 ++pcnt;
394f9b3a 9593 else if (*s == ')')
f9419b05 9594 --pcnt;
394f9b3a
TS
9595 }
9596 }
fb1b3232 9597 }
394f9b3a 9598 /* Check for percent_op (outside of parentheses). */
ad8d3bb3 9599 else if (*str[0] == '%')
394f9b3a
TS
9600 return my_getPercentOp (str, len, nestlevel);
9601
9602 /* Any other expression. */
9603 return S_EX_NONE;
9604}
ad8d3bb3 9605
394f9b3a
TS
9606static int
9607my_getPercentOp (str, len, nestlevel)
9608 char **str;
9609 unsigned int *len;
9610 int *nestlevel;
9611{
9612 char *tmp = *str + 1;
9613 unsigned int i = 0;
ad8d3bb3 9614
394f9b3a
TS
9615 while (ISALPHA (*tmp) || *tmp == '_')
9616 {
9617 *tmp = TOLOWER (*tmp);
9618 tmp++;
9619 }
9620 while (i < (sizeof (percent_op) / sizeof (struct percent_op_match)))
9621 {
9622 if (strncmp (*str, percent_op[i].str, strlen (percent_op[i].str)))
98d3f06f 9623 i++;
394f9b3a 9624 else
ad8d3bb3 9625 {
394f9b3a 9626 int type = percent_op[i].type;
ad8d3bb3 9627
394f9b3a
TS
9628 /* Only %hi and %lo are allowed for OldABI. */
9629 if (! HAVE_NEWABI && type != S_EX_HI && type != S_EX_LO)
9630 return S_EX_NONE;
ad8d3bb3 9631
394f9b3a 9632 *len = strlen (percent_op[i].str);
f9419b05 9633 ++(*nestlevel);
394f9b3a 9634 return type;
ad8d3bb3 9635 }
fb1b3232 9636 }
ad8d3bb3
TS
9637 return S_EX_NONE;
9638}
9639
9640static int
9641my_getSmallExpression (ep, str)
9642 expressionS *ep;
9643 char *str;
9644{
9645 static char *oldstr = NULL;
9646 int c = S_EX_NONE;
9647 int oldc;
394f9b3a 9648 int nestlevel = -1;
ad8d3bb3
TS
9649 unsigned int len;
9650
394f9b3a
TS
9651 /* Don't update oldstr if the last call had nested percent_op's. We need
9652 it to parse the outer ones later. */
ad8d3bb3
TS
9653 if (! oldstr)
9654 oldstr = str;
76b3015f 9655
ad8d3bb3 9656 do
fb1b3232 9657 {
ad8d3bb3 9658 oldc = c;
394f9b3a 9659 c = my_getSmallParser (&str, &len, &nestlevel);
ad8d3bb3
TS
9660 if (c != S_EX_NONE && c != S_EX_REGISTER)
9661 str += len;
fb1b3232 9662 }
ad8d3bb3
TS
9663 while (c != S_EX_NONE && c != S_EX_REGISTER);
9664
394f9b3a 9665 if (nestlevel >= 0)
fb1b3232 9666 {
394f9b3a
TS
9667 /* A percent_op was encountered. Don't try to get an expression if
9668 it is already blanked out. */
ad8d3bb3
TS
9669 if (*(str + strspn (str + 1, " )")) != ')')
9670 {
9671 char save;
9672
394f9b3a 9673 /* Let my_getExpression() stop at the closing parenthesis. */
ad8d3bb3
TS
9674 save = *(str + len);
9675 *(str + len) = '\0';
9676 my_getExpression (ep, str);
9677 *(str + len) = save;
9678 }
394f9b3a 9679 if (nestlevel > 0)
ad8d3bb3 9680 {
394f9b3a
TS
9681 /* Blank out including the % sign and the proper matching
9682 parenthesis. */
9683 int pcnt = 1;
9684 char *s = strrchr (oldstr, '%');
9685 char *end;
9686
9687 for (end = strchr (s, '(') + 1; *end && pcnt; end++)
9688 {
9689 if (*end == '(')
f9419b05 9690 ++pcnt;
394f9b3a 9691 else if (*end == ')')
f9419b05 9692 --pcnt;
394f9b3a
TS
9693 }
9694
9695 memset (s, ' ', end - s);
ad8d3bb3
TS
9696 str = oldstr;
9697 }
9698 else
394f9b3a
TS
9699 expr_end = str + len;
9700
ad8d3bb3 9701 c = oldc;
fb1b3232 9702 }
ad8d3bb3 9703 else if (c == S_EX_NONE)
fb1b3232 9704 {
ad8d3bb3 9705 my_getExpression (ep, str);
fb1b3232 9706 }
ad8d3bb3 9707 else if (c == S_EX_REGISTER)
fb1b3232 9708 {
ad8d3bb3
TS
9709 ep->X_op = O_constant;
9710 expr_end = str;
9711 ep->X_add_symbol = NULL;
9712 ep->X_op_symbol = NULL;
9713 ep->X_add_number = 0;
fb1b3232 9714 }
fb1b3232
TS
9715 else
9716 {
98d3f06f 9717 as_fatal (_("internal error"));
fb1b3232 9718 }
252b5132 9719
394f9b3a
TS
9720 if (nestlevel <= 0)
9721 /* All percent_op's have been handled. */
ad8d3bb3 9722 oldstr = NULL;
fb1b3232 9723
fb1b3232 9724 return c;
252b5132
RH
9725}
9726
9727static void
9728my_getExpression (ep, str)
9729 expressionS *ep;
9730 char *str;
9731{
9732 char *save_in;
98aa84af 9733 valueT val;
252b5132
RH
9734
9735 save_in = input_line_pointer;
9736 input_line_pointer = str;
9737 expression (ep);
9738 expr_end = input_line_pointer;
9739 input_line_pointer = save_in;
9740
9741 /* If we are in mips16 mode, and this is an expression based on `.',
9742 then we bump the value of the symbol by 1 since that is how other
9743 text symbols are handled. We don't bother to handle complex
9744 expressions, just `.' plus or minus a constant. */
9745 if (mips_opts.mips16
9746 && ep->X_op == O_symbol
9747 && strcmp (S_GET_NAME (ep->X_add_symbol), FAKE_LABEL_NAME) == 0
9748 && S_GET_SEGMENT (ep->X_add_symbol) == now_seg
49309057
ILT
9749 && symbol_get_frag (ep->X_add_symbol) == frag_now
9750 && symbol_constant_p (ep->X_add_symbol)
98aa84af
AM
9751 && (val = S_GET_VALUE (ep->X_add_symbol)) == frag_now_fix ())
9752 S_SET_VALUE (ep->X_add_symbol, val + 1);
252b5132
RH
9753}
9754
9755/* Turn a string in input_line_pointer into a floating point constant
bc0d738a
NC
9756 of type TYPE, and store the appropriate bytes in *LITP. The number
9757 of LITTLENUMS emitted is stored in *SIZEP. An error message is
252b5132
RH
9758 returned, or NULL on OK. */
9759
9760char *
9761md_atof (type, litP, sizeP)
9762 int type;
9763 char *litP;
9764 int *sizeP;
9765{
9766 int prec;
9767 LITTLENUM_TYPE words[4];
9768 char *t;
9769 int i;
9770
9771 switch (type)
9772 {
9773 case 'f':
9774 prec = 2;
9775 break;
9776
9777 case 'd':
9778 prec = 4;
9779 break;
9780
9781 default:
9782 *sizeP = 0;
9783 return _("bad call to md_atof");
9784 }
9785
9786 t = atof_ieee (input_line_pointer, type, words);
9787 if (t)
9788 input_line_pointer = t;
9789
9790 *sizeP = prec * 2;
9791
9792 if (! target_big_endian)
9793 {
9794 for (i = prec - 1; i >= 0; i--)
9795 {
9796 md_number_to_chars (litP, (valueT) words[i], 2);
9797 litP += 2;
9798 }
9799 }
9800 else
9801 {
9802 for (i = 0; i < prec; i++)
9803 {
9804 md_number_to_chars (litP, (valueT) words[i], 2);
9805 litP += 2;
9806 }
9807 }
bdaaa2e1 9808
252b5132
RH
9809 return NULL;
9810}
9811
9812void
9813md_number_to_chars (buf, val, n)
9814 char *buf;
9815 valueT val;
9816 int n;
9817{
9818 if (target_big_endian)
9819 number_to_chars_bigendian (buf, val, n);
9820 else
9821 number_to_chars_littleendian (buf, val, n);
9822}
9823\f
ae948b86 9824#ifdef OBJ_ELF
e013f690
TS
9825static int support_64bit_objects(void)
9826{
9827 const char **list, **l;
9828
9829 list = bfd_target_list ();
9830 for (l = list; *l != NULL; l++)
9831#ifdef TE_TMIPS
9832 /* This is traditional mips */
9833 if (strcmp (*l, "elf64-tradbigmips") == 0
9834 || strcmp (*l, "elf64-tradlittlemips") == 0)
9835#else
9836 if (strcmp (*l, "elf64-bigmips") == 0
9837 || strcmp (*l, "elf64-littlemips") == 0)
9838#endif
9839 break;
9840 free (list);
9841 return (*l != NULL);
9842}
ae948b86 9843#endif /* OBJ_ELF */
e013f690 9844
5a38dc70 9845const char *md_shortopts = "nO::g::G:";
252b5132 9846
e972090a
NC
9847struct option md_longopts[] =
9848{
252b5132
RH
9849#define OPTION_MIPS1 (OPTION_MD_BASE + 1)
9850 {"mips0", no_argument, NULL, OPTION_MIPS1},
9851 {"mips1", no_argument, NULL, OPTION_MIPS1},
9852#define OPTION_MIPS2 (OPTION_MD_BASE + 2)
9853 {"mips2", no_argument, NULL, OPTION_MIPS2},
9854#define OPTION_MIPS3 (OPTION_MD_BASE + 3)
9855 {"mips3", no_argument, NULL, OPTION_MIPS3},
9856#define OPTION_MIPS4 (OPTION_MD_BASE + 4)
9857 {"mips4", no_argument, NULL, OPTION_MIPS4},
ae948b86
TS
9858#define OPTION_MIPS5 (OPTION_MD_BASE + 5)
9859 {"mips5", no_argument, NULL, OPTION_MIPS5},
9860#define OPTION_MIPS32 (OPTION_MD_BASE + 6)
9861 {"mips32", no_argument, NULL, OPTION_MIPS32},
9862#define OPTION_MIPS64 (OPTION_MD_BASE + 7)
9863 {"mips64", no_argument, NULL, OPTION_MIPS64},
9864#define OPTION_MEMBEDDED_PIC (OPTION_MD_BASE + 8)
252b5132 9865 {"membedded-pic", no_argument, NULL, OPTION_MEMBEDDED_PIC},
ae948b86 9866#define OPTION_TRAP (OPTION_MD_BASE + 9)
252b5132
RH
9867 {"trap", no_argument, NULL, OPTION_TRAP},
9868 {"no-break", no_argument, NULL, OPTION_TRAP},
ae948b86 9869#define OPTION_BREAK (OPTION_MD_BASE + 10)
252b5132
RH
9870 {"break", no_argument, NULL, OPTION_BREAK},
9871 {"no-trap", no_argument, NULL, OPTION_BREAK},
ae948b86 9872#define OPTION_EB (OPTION_MD_BASE + 11)
252b5132 9873 {"EB", no_argument, NULL, OPTION_EB},
ae948b86 9874#define OPTION_EL (OPTION_MD_BASE + 12)
252b5132 9875 {"EL", no_argument, NULL, OPTION_EL},
ae948b86 9876#define OPTION_MIPS16 (OPTION_MD_BASE + 13)
252b5132 9877 {"mips16", no_argument, NULL, OPTION_MIPS16},
ae948b86 9878#define OPTION_NO_MIPS16 (OPTION_MD_BASE + 14)
252b5132 9879 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
ae948b86 9880#define OPTION_M7000_HILO_FIX (OPTION_MD_BASE + 15)
6b76fefe 9881 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
9ee72ff1
TS
9882#define OPTION_MNO_7000_HILO_FIX (OPTION_MD_BASE + 16)
9883 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
9884 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
ae948b86
TS
9885#define OPTION_FP32 (OPTION_MD_BASE + 17)
9886 {"mfp32", no_argument, NULL, OPTION_FP32},
9887#define OPTION_GP32 (OPTION_MD_BASE + 18)
c97ef257 9888 {"mgp32", no_argument, NULL, OPTION_GP32},
ae948b86 9889#define OPTION_CONSTRUCT_FLOATS (OPTION_MD_BASE + 19)
119d663a 9890 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
ae948b86 9891#define OPTION_NO_CONSTRUCT_FLOATS (OPTION_MD_BASE + 20)
119d663a 9892 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
ae948b86 9893#define OPTION_MARCH (OPTION_MD_BASE + 21)
ec68c924 9894 {"march", required_argument, NULL, OPTION_MARCH},
ae948b86 9895#define OPTION_MTUNE (OPTION_MD_BASE + 22)
ec68c924 9896 {"mtune", required_argument, NULL, OPTION_MTUNE},
316f5878
RS
9897#define OPTION_FP64 (OPTION_MD_BASE + 23)
9898 {"mfp64", no_argument, NULL, OPTION_FP64},
ae948b86
TS
9899#define OPTION_M4650 (OPTION_MD_BASE + 24)
9900 {"m4650", no_argument, NULL, OPTION_M4650},
9901#define OPTION_NO_M4650 (OPTION_MD_BASE + 25)
9902 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
9903#define OPTION_M4010 (OPTION_MD_BASE + 26)
9904 {"m4010", no_argument, NULL, OPTION_M4010},
9905#define OPTION_NO_M4010 (OPTION_MD_BASE + 27)
9906 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
9907#define OPTION_M4100 (OPTION_MD_BASE + 28)
9908 {"m4100", no_argument, NULL, OPTION_M4100},
9909#define OPTION_NO_M4100 (OPTION_MD_BASE + 29)
9910 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
9911#define OPTION_M3900 (OPTION_MD_BASE + 30)
9912 {"m3900", no_argument, NULL, OPTION_M3900},
9913#define OPTION_NO_M3900 (OPTION_MD_BASE + 31)
9914 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
9915#define OPTION_GP64 (OPTION_MD_BASE + 32)
9916 {"mgp64", no_argument, NULL, OPTION_GP64},
1f25f5d3
CD
9917#define OPTION_MIPS3D (OPTION_MD_BASE + 33)
9918 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
9919#define OPTION_NO_MIPS3D (OPTION_MD_BASE + 34)
9920 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
deec1734
CD
9921#define OPTION_MDMX (OPTION_MD_BASE + 35)
9922 {"mdmx", no_argument, NULL, OPTION_MDMX},
9923#define OPTION_NO_MDMX (OPTION_MD_BASE + 36)
9924 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
156c2f8b 9925#ifdef OBJ_ELF
deec1734 9926#define OPTION_ELF_BASE (OPTION_MD_BASE + 37)
156c2f8b 9927#define OPTION_CALL_SHARED (OPTION_ELF_BASE + 0)
156c2f8b
NC
9928 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
9929 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
ae948b86 9930#define OPTION_NON_SHARED (OPTION_ELF_BASE + 1)
156c2f8b 9931 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
ae948b86 9932#define OPTION_XGOT (OPTION_ELF_BASE + 2)
156c2f8b 9933 {"xgot", no_argument, NULL, OPTION_XGOT},
ae948b86
TS
9934#define OPTION_MABI (OPTION_ELF_BASE + 3)
9935 {"mabi", required_argument, NULL, OPTION_MABI},
9936#define OPTION_32 (OPTION_ELF_BASE + 4)
156c2f8b 9937 {"32", no_argument, NULL, OPTION_32},
ae948b86 9938#define OPTION_N32 (OPTION_ELF_BASE + 5)
e013f690 9939 {"n32", no_argument, NULL, OPTION_N32},
ae948b86 9940#define OPTION_64 (OPTION_ELF_BASE + 6)
156c2f8b 9941 {"64", no_argument, NULL, OPTION_64},
ecb4347a
DJ
9942#define OPTION_MDEBUG (OPTION_ELF_BASE + 7)
9943 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
9944#define OPTION_NO_MDEBUG (OPTION_ELF_BASE + 8)
9945 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
ae948b86 9946#endif /* OBJ_ELF */
252b5132
RH
9947 {NULL, no_argument, NULL, 0}
9948};
156c2f8b 9949size_t md_longopts_size = sizeof (md_longopts);
252b5132 9950
316f5878
RS
9951/* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
9952 NEW_VALUE. Warn if another value was already specified. Note:
9953 we have to defer parsing the -march and -mtune arguments in order
9954 to handle 'from-abi' correctly, since the ABI might be specified
9955 in a later argument. */
9956
9957static void
9958mips_set_option_string (string_ptr, new_value)
9959 const char **string_ptr, *new_value;
9960{
9961 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
9962 as_warn (_("A different %s was already specified, is now %s"),
9963 string_ptr == &mips_arch_string ? "-march" : "-mtune",
9964 new_value);
9965
9966 *string_ptr = new_value;
9967}
9968
252b5132
RH
9969int
9970md_parse_option (c, arg)
9971 int c;
9972 char *arg;
9973{
9974 switch (c)
9975 {
119d663a
NC
9976 case OPTION_CONSTRUCT_FLOATS:
9977 mips_disable_float_construction = 0;
9978 break;
bdaaa2e1 9979
119d663a
NC
9980 case OPTION_NO_CONSTRUCT_FLOATS:
9981 mips_disable_float_construction = 1;
9982 break;
bdaaa2e1 9983
252b5132
RH
9984 case OPTION_TRAP:
9985 mips_trap = 1;
9986 break;
9987
9988 case OPTION_BREAK:
9989 mips_trap = 0;
9990 break;
9991
9992 case OPTION_EB:
9993 target_big_endian = 1;
9994 break;
9995
9996 case OPTION_EL:
9997 target_big_endian = 0;
9998 break;
9999
39c0a331
L
10000 case 'n':
10001 warn_nops = 1;
10002 break;
10003
252b5132
RH
10004 case 'O':
10005 if (arg && arg[1] == '0')
10006 mips_optimize = 1;
10007 else
10008 mips_optimize = 2;
10009 break;
10010
10011 case 'g':
10012 if (arg == NULL)
10013 mips_debug = 2;
10014 else
10015 mips_debug = atoi (arg);
10016 /* When the MIPS assembler sees -g or -g2, it does not do
10017 optimizations which limit full symbolic debugging. We take
10018 that to be equivalent to -O0. */
10019 if (mips_debug == 2)
10020 mips_optimize = 1;
10021 break;
10022
10023 case OPTION_MIPS1:
316f5878 10024 file_mips_isa = ISA_MIPS1;
252b5132
RH
10025 break;
10026
10027 case OPTION_MIPS2:
316f5878 10028 file_mips_isa = ISA_MIPS2;
252b5132
RH
10029 break;
10030
10031 case OPTION_MIPS3:
316f5878 10032 file_mips_isa = ISA_MIPS3;
252b5132
RH
10033 break;
10034
10035 case OPTION_MIPS4:
316f5878 10036 file_mips_isa = ISA_MIPS4;
e7af610e
NC
10037 break;
10038
84ea6cf2 10039 case OPTION_MIPS5:
316f5878 10040 file_mips_isa = ISA_MIPS5;
84ea6cf2
NC
10041 break;
10042
e7af610e 10043 case OPTION_MIPS32:
316f5878 10044 file_mips_isa = ISA_MIPS32;
252b5132
RH
10045 break;
10046
84ea6cf2 10047 case OPTION_MIPS64:
316f5878 10048 file_mips_isa = ISA_MIPS64;
84ea6cf2
NC
10049 break;
10050
ec68c924 10051 case OPTION_MTUNE:
316f5878
RS
10052 mips_set_option_string (&mips_tune_string, arg);
10053 break;
ec68c924 10054
316f5878
RS
10055 case OPTION_MARCH:
10056 mips_set_option_string (&mips_arch_string, arg);
252b5132
RH
10057 break;
10058
10059 case OPTION_M4650:
316f5878
RS
10060 mips_set_option_string (&mips_arch_string, "4650");
10061 mips_set_option_string (&mips_tune_string, "4650");
252b5132
RH
10062 break;
10063
10064 case OPTION_NO_M4650:
10065 break;
10066
10067 case OPTION_M4010:
316f5878
RS
10068 mips_set_option_string (&mips_arch_string, "4010");
10069 mips_set_option_string (&mips_tune_string, "4010");
252b5132
RH
10070 break;
10071
10072 case OPTION_NO_M4010:
10073 break;
10074
10075 case OPTION_M4100:
316f5878
RS
10076 mips_set_option_string (&mips_arch_string, "4100");
10077 mips_set_option_string (&mips_tune_string, "4100");
252b5132
RH
10078 break;
10079
10080 case OPTION_NO_M4100:
10081 break;
10082
252b5132 10083 case OPTION_M3900:
316f5878
RS
10084 mips_set_option_string (&mips_arch_string, "3900");
10085 mips_set_option_string (&mips_tune_string, "3900");
252b5132 10086 break;
bdaaa2e1 10087
252b5132
RH
10088 case OPTION_NO_M3900:
10089 break;
10090
deec1734
CD
10091 case OPTION_MDMX:
10092 mips_opts.ase_mdmx = 1;
10093 break;
10094
10095 case OPTION_NO_MDMX:
10096 mips_opts.ase_mdmx = 0;
10097 break;
10098
252b5132
RH
10099 case OPTION_MIPS16:
10100 mips_opts.mips16 = 1;
10101 mips_no_prev_insn (false);
10102 break;
10103
10104 case OPTION_NO_MIPS16:
10105 mips_opts.mips16 = 0;
10106 mips_no_prev_insn (false);
10107 break;
10108
1f25f5d3
CD
10109 case OPTION_MIPS3D:
10110 mips_opts.ase_mips3d = 1;
10111 break;
10112
10113 case OPTION_NO_MIPS3D:
10114 mips_opts.ase_mips3d = 0;
10115 break;
10116
252b5132
RH
10117 case OPTION_MEMBEDDED_PIC:
10118 mips_pic = EMBEDDED_PIC;
10119 if (USE_GLOBAL_POINTER_OPT && g_switch_seen)
10120 {
10121 as_bad (_("-G may not be used with embedded PIC code"));
10122 return 0;
10123 }
10124 g_switch_value = 0x7fffffff;
10125 break;
10126
0f074f60 10127#ifdef OBJ_ELF
252b5132
RH
10128 /* When generating ELF code, we permit -KPIC and -call_shared to
10129 select SVR4_PIC, and -non_shared to select no PIC. This is
10130 intended to be compatible with Irix 5. */
10131 case OPTION_CALL_SHARED:
10132 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10133 {
10134 as_bad (_("-call_shared is supported only for ELF format"));
10135 return 0;
10136 }
10137 mips_pic = SVR4_PIC;
10138 if (g_switch_seen && g_switch_value != 0)
10139 {
10140 as_bad (_("-G may not be used with SVR4 PIC code"));
10141 return 0;
10142 }
10143 g_switch_value = 0;
10144 break;
10145
10146 case OPTION_NON_SHARED:
10147 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10148 {
10149 as_bad (_("-non_shared is supported only for ELF format"));
10150 return 0;
10151 }
10152 mips_pic = NO_PIC;
10153 break;
10154
10155 /* The -xgot option tells the assembler to use 32 offsets when
10156 accessing the got in SVR4_PIC mode. It is for Irix
10157 compatibility. */
10158 case OPTION_XGOT:
10159 mips_big_got = 1;
10160 break;
0f074f60 10161#endif /* OBJ_ELF */
252b5132
RH
10162
10163 case 'G':
10164 if (! USE_GLOBAL_POINTER_OPT)
10165 {
10166 as_bad (_("-G is not supported for this configuration"));
10167 return 0;
10168 }
10169 else if (mips_pic == SVR4_PIC || mips_pic == EMBEDDED_PIC)
10170 {
10171 as_bad (_("-G may not be used with SVR4 or embedded PIC code"));
10172 return 0;
10173 }
10174 else
10175 g_switch_value = atoi (arg);
10176 g_switch_seen = 1;
10177 break;
10178
0f074f60 10179#ifdef OBJ_ELF
34ba82a8
TS
10180 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
10181 and -mabi=64. */
252b5132 10182 case OPTION_32:
34ba82a8
TS
10183 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10184 {
10185 as_bad (_("-32 is supported for ELF format only"));
10186 return 0;
10187 }
316f5878 10188 mips_abi = O32_ABI;
252b5132
RH
10189 break;
10190
e013f690 10191 case OPTION_N32:
34ba82a8
TS
10192 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10193 {
10194 as_bad (_("-n32 is supported for ELF format only"));
10195 return 0;
10196 }
316f5878 10197 mips_abi = N32_ABI;
e013f690 10198 break;
252b5132 10199
e013f690 10200 case OPTION_64:
34ba82a8
TS
10201 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10202 {
10203 as_bad (_("-64 is supported for ELF format only"));
10204 return 0;
10205 }
316f5878 10206 mips_abi = N64_ABI;
e013f690
TS
10207 if (! support_64bit_objects())
10208 as_fatal (_("No compiled in support for 64 bit object file format"));
252b5132 10209 break;
ae948b86 10210#endif /* OBJ_ELF */
252b5132 10211
c97ef257 10212 case OPTION_GP32:
a325df1d 10213 file_mips_gp32 = 1;
c97ef257
AH
10214 break;
10215
10216 case OPTION_GP64:
a325df1d 10217 file_mips_gp32 = 0;
c97ef257 10218 break;
252b5132 10219
ca4e0257 10220 case OPTION_FP32:
a325df1d 10221 file_mips_fp32 = 1;
316f5878
RS
10222 break;
10223
10224 case OPTION_FP64:
10225 file_mips_fp32 = 0;
ca4e0257
RS
10226 break;
10227
ae948b86 10228#ifdef OBJ_ELF
252b5132 10229 case OPTION_MABI:
34ba82a8
TS
10230 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
10231 {
10232 as_bad (_("-mabi is supported for ELF format only"));
10233 return 0;
10234 }
e013f690 10235 if (strcmp (arg, "32") == 0)
316f5878 10236 mips_abi = O32_ABI;
e013f690 10237 else if (strcmp (arg, "o64") == 0)
316f5878 10238 mips_abi = O64_ABI;
e013f690 10239 else if (strcmp (arg, "n32") == 0)
316f5878 10240 mips_abi = N32_ABI;
e013f690
TS
10241 else if (strcmp (arg, "64") == 0)
10242 {
316f5878 10243 mips_abi = N64_ABI;
e013f690
TS
10244 if (! support_64bit_objects())
10245 as_fatal (_("No compiled in support for 64 bit object file "
10246 "format"));
10247 }
10248 else if (strcmp (arg, "eabi") == 0)
316f5878 10249 mips_abi = EABI_ABI;
e013f690 10250 else
da0e507f
TS
10251 {
10252 as_fatal (_("invalid abi -mabi=%s"), arg);
10253 return 0;
10254 }
252b5132 10255 break;
e013f690 10256#endif /* OBJ_ELF */
252b5132 10257
6b76fefe
CM
10258 case OPTION_M7000_HILO_FIX:
10259 mips_7000_hilo_fix = true;
10260 break;
10261
9ee72ff1 10262 case OPTION_MNO_7000_HILO_FIX:
6b76fefe
CM
10263 mips_7000_hilo_fix = false;
10264 break;
10265
ecb4347a
DJ
10266#ifdef OBJ_ELF
10267 case OPTION_MDEBUG:
10268 mips_flag_mdebug = true;
10269 break;
10270
10271 case OPTION_NO_MDEBUG:
10272 mips_flag_mdebug = false;
10273 break;
10274#endif /* OBJ_ELF */
10275
252b5132
RH
10276 default:
10277 return 0;
10278 }
10279
10280 return 1;
10281}
316f5878
RS
10282\f
10283/* Set up globals to generate code for the ISA or processor
10284 described by INFO. */
252b5132 10285
252b5132 10286static void
316f5878
RS
10287mips_set_architecture (info)
10288 const struct mips_cpu_info *info;
252b5132 10289{
316f5878 10290 if (info != 0)
252b5132 10291 {
316f5878
RS
10292 mips_arch_info = info;
10293 mips_arch = info->cpu;
10294 mips_opts.isa = info->isa;
252b5132 10295 }
252b5132
RH
10296}
10297
252b5132 10298
316f5878 10299/* Likewise for tuning. */
252b5132 10300
316f5878
RS
10301static void
10302mips_set_tune (info)
10303 const struct mips_cpu_info *info;
10304{
10305 if (info != 0)
10306 {
10307 mips_tune_info = info;
10308 mips_tune = info->cpu;
10309 }
10310}
80cc45a5 10311
34ba82a8 10312
252b5132 10313void
e9670677
MR
10314mips_after_parse_args ()
10315{
e9670677
MR
10316 /* GP relative stuff not working for PE */
10317 if (strncmp (TARGET_OS, "pe", 2) == 0
10318 && g_switch_value != 0)
10319 {
10320 if (g_switch_seen)
10321 as_bad (_("-G not supported in this configuration."));
10322 g_switch_value = 0;
10323 }
10324
22923709
RS
10325 /* The following code determines the architecture and register size.
10326 Similar code was added to GCC 3.3 (see override_options() in
10327 config/mips/mips.c). The GAS and GCC code should be kept in sync
10328 as much as possible. */
e9670677 10329
316f5878
RS
10330 if (mips_arch_string != 0)
10331 mips_set_architecture (mips_parse_cpu ("-march", mips_arch_string));
e9670677 10332
316f5878
RS
10333 if (mips_tune_string != 0)
10334 mips_set_tune (mips_parse_cpu ("-mtune", mips_tune_string));
e9670677 10335
316f5878 10336 if (file_mips_isa != ISA_UNKNOWN)
e9670677 10337 {
316f5878
RS
10338 /* Handle -mipsN. At this point, file_mips_isa contains the
10339 ISA level specified by -mipsN, while mips_opts.isa contains
10340 the -march selection (if any). */
10341 if (mips_arch_info != 0)
e9670677 10342 {
316f5878
RS
10343 /* -march takes precedence over -mipsN, since it is more descriptive.
10344 There's no harm in specifying both as long as the ISA levels
10345 are the same. */
10346 if (file_mips_isa != mips_opts.isa)
10347 as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
10348 mips_cpu_info_from_isa (file_mips_isa)->name,
10349 mips_cpu_info_from_isa (mips_opts.isa)->name);
e9670677 10350 }
316f5878
RS
10351 else
10352 mips_set_architecture (mips_cpu_info_from_isa (file_mips_isa));
e9670677
MR
10353 }
10354
316f5878
RS
10355 if (mips_arch_info == 0)
10356 mips_set_architecture (mips_parse_cpu ("default CPU",
10357 MIPS_CPU_STRING_DEFAULT));
e9670677 10358
316f5878
RS
10359 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (mips_opts.isa))
10360 as_bad ("-march=%s is not compatible with the selected ABI",
10361 mips_arch_info->name);
e9670677 10362
316f5878
RS
10363 /* Optimize for mips_arch, unless -mtune selects a different processor. */
10364 if (mips_tune_info == 0)
10365 mips_set_tune (mips_arch_info);
e9670677 10366
316f5878 10367 if (file_mips_gp32 >= 0)
e9670677 10368 {
316f5878
RS
10369 /* The user specified the size of the integer registers. Make sure
10370 it agrees with the ABI and ISA. */
10371 if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
10372 as_bad (_("-mgp64 used with a 32-bit processor"));
10373 else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
10374 as_bad (_("-mgp32 used with a 64-bit ABI"));
10375 else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
10376 as_bad (_("-mgp64 used with a 32-bit ABI"));
e9670677
MR
10377 }
10378 else
10379 {
316f5878
RS
10380 /* Infer the integer register size from the ABI and processor.
10381 Restrict ourselves to 32-bit registers if that's all the
10382 processor has, or if the ABI cannot handle 64-bit registers. */
10383 file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
10384 || !ISA_HAS_64BIT_REGS (mips_opts.isa));
e9670677
MR
10385 }
10386
316f5878
RS
10387 /* ??? GAS treats single-float processors as though they had 64-bit
10388 float registers (although it complains when double-precision
10389 instructions are used). As things stand, saying they have 32-bit
10390 registers would lead to spurious "register must be even" messages.
10391 So here we assume float registers are always the same size as
10392 integer ones, unless the user says otherwise. */
10393 if (file_mips_fp32 < 0)
10394 file_mips_fp32 = file_mips_gp32;
e9670677 10395
316f5878 10396 /* End of GCC-shared inference code. */
e9670677 10397
316f5878
RS
10398 /* ??? When do we want this flag to be set? Who uses it? */
10399 if (file_mips_gp32 == 1
10400 && mips_abi == NO_ABI
10401 && ISA_HAS_64BIT_REGS (mips_opts.isa))
10402 mips_32bitmode = 1;
e9670677
MR
10403
10404 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
10405 as_bad (_("trap exception not supported at ISA 1"));
10406
e9670677
MR
10407 /* If the selected architecture includes support for ASEs, enable
10408 generation of code for them. */
a4672219
TS
10409 if (mips_opts.mips16 == -1)
10410 mips_opts.mips16 = (CPU_HAS_MIPS16 (mips_arch)) ? 1 : 0;
ffdefa66 10411 if (mips_opts.ase_mips3d == -1)
a4672219 10412 mips_opts.ase_mips3d = (CPU_HAS_MIPS3D (mips_arch)) ? 1 : 0;
ffdefa66 10413 if (mips_opts.ase_mdmx == -1)
a4672219 10414 mips_opts.ase_mdmx = (CPU_HAS_MDMX (mips_arch)) ? 1 : 0;
e9670677 10415
e9670677 10416 file_mips_isa = mips_opts.isa;
a4672219 10417 file_ase_mips16 = mips_opts.mips16;
e9670677
MR
10418 file_ase_mips3d = mips_opts.ase_mips3d;
10419 file_ase_mdmx = mips_opts.ase_mdmx;
10420 mips_opts.gp32 = file_mips_gp32;
10421 mips_opts.fp32 = file_mips_fp32;
10422
10423 if (HAVE_NEWABI)
10424 mips_big_got = 1;
ecb4347a
DJ
10425
10426 if (mips_flag_mdebug < 0)
10427 {
10428#ifdef OBJ_MAYBE_ECOFF
10429 if (OUTPUT_FLAVOR == bfd_target_ecoff_flavour)
10430 mips_flag_mdebug = 1;
10431 else
10432#endif /* OBJ_MAYBE_ECOFF */
10433 mips_flag_mdebug = 0;
10434 }
e9670677
MR
10435}
10436\f
10437void
252b5132
RH
10438mips_init_after_args ()
10439{
10440 /* initialize opcodes */
10441 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
beae10d5 10442 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
252b5132
RH
10443}
10444
10445long
10446md_pcrel_from (fixP)
10447 fixS *fixP;
10448{
10449 if (OUTPUT_FLAVOR != bfd_target_aout_flavour
10450 && fixP->fx_addsy != (symbolS *) NULL
10451 && ! S_IS_DEFINED (fixP->fx_addsy))
10452 {
6478892d
TS
10453 /* This makes a branch to an undefined symbol be a branch to the
10454 current location. */
cb56d3d3 10455 if (mips_pic == EMBEDDED_PIC)
6478892d 10456 return 4;
cb56d3d3 10457 else
6478892d 10458 return 1;
252b5132
RH
10459 }
10460
c9914766 10461 /* Return the address of the delay slot. */
252b5132
RH
10462 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
10463}
10464
252b5132
RH
10465/* This is called before the symbol table is processed. In order to
10466 work with gcc when using mips-tfile, we must keep all local labels.
10467 However, in other cases, we want to discard them. If we were
10468 called with -g, but we didn't see any debugging information, it may
10469 mean that gcc is smuggling debugging information through to
10470 mips-tfile, in which case we must generate all local labels. */
10471
10472void
10473mips_frob_file_before_adjust ()
10474{
10475#ifndef NO_ECOFF_DEBUGGING
10476 if (ECOFF_DEBUGGING
10477 && mips_debug != 0
10478 && ! ecoff_debugging_seen)
10479 flag_keep_locals = 1;
10480#endif
10481}
10482
10483/* Sort any unmatched HI16_S relocs so that they immediately precede
94f592af 10484 the corresponding LO reloc. This is called before md_apply_fix3 and
252b5132
RH
10485 tc_gen_reloc. Unmatched HI16_S relocs can only be generated by
10486 explicit use of the %hi modifier. */
10487
10488void
10489mips_frob_file ()
10490{
10491 struct mips_hi_fixup *l;
10492
10493 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
10494 {
10495 segment_info_type *seginfo;
10496 int pass;
10497
10498 assert (l->fixp->fx_r_type == BFD_RELOC_HI16_S);
10499
10500 /* Check quickly whether the next fixup happens to be a matching
10501 %lo. */
10502 if (l->fixp->fx_next != NULL
10503 && l->fixp->fx_next->fx_r_type == BFD_RELOC_LO16
10504 && l->fixp->fx_addsy == l->fixp->fx_next->fx_addsy
10505 && l->fixp->fx_offset == l->fixp->fx_next->fx_offset)
10506 continue;
10507
10508 /* Look through the fixups for this segment for a matching %lo.
10509 When we find one, move the %hi just in front of it. We do
10510 this in two passes. In the first pass, we try to find a
10511 unique %lo. In the second pass, we permit multiple %hi
10512 relocs for a single %lo (this is a GNU extension). */
10513 seginfo = seg_info (l->seg);
10514 for (pass = 0; pass < 2; pass++)
10515 {
10516 fixS *f, *prev;
10517
10518 prev = NULL;
10519 for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
10520 {
10521 /* Check whether this is a %lo fixup which matches l->fixp. */
10522 if (f->fx_r_type == BFD_RELOC_LO16
10523 && f->fx_addsy == l->fixp->fx_addsy
10524 && f->fx_offset == l->fixp->fx_offset
10525 && (pass == 1
10526 || prev == NULL
10527 || prev->fx_r_type != BFD_RELOC_HI16_S
10528 || prev->fx_addsy != f->fx_addsy
10529 || prev->fx_offset != f->fx_offset))
10530 {
10531 fixS **pf;
10532
10533 /* Move l->fixp before f. */
10534 for (pf = &seginfo->fix_root;
10535 *pf != l->fixp;
10536 pf = &(*pf)->fx_next)
10537 assert (*pf != NULL);
10538
10539 *pf = l->fixp->fx_next;
10540
10541 l->fixp->fx_next = f;
10542 if (prev == NULL)
10543 seginfo->fix_root = l->fixp;
10544 else
10545 prev->fx_next = l->fixp;
10546
10547 break;
10548 }
10549
10550 prev = f;
10551 }
10552
10553 if (f != NULL)
10554 break;
10555
10556#if 0 /* GCC code motion plus incomplete dead code elimination
10557 can leave a %hi without a %lo. */
10558 if (pass == 1)
10559 as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
10560 _("Unmatched %%hi reloc"));
10561#endif
10562 }
10563 }
10564}
10565
10566/* When generating embedded PIC code we need to use a special
10567 relocation to represent the difference of two symbols in the .text
10568 section (switch tables use a difference of this sort). See
10569 include/coff/mips.h for details. This macro checks whether this
10570 fixup requires the special reloc. */
10571#define SWITCH_TABLE(fixp) \
10572 ((fixp)->fx_r_type == BFD_RELOC_32 \
bb2d6cd7 10573 && OUTPUT_FLAVOR != bfd_target_elf_flavour \
252b5132
RH
10574 && (fixp)->fx_addsy != NULL \
10575 && (fixp)->fx_subsy != NULL \
10576 && S_GET_SEGMENT ((fixp)->fx_addsy) == text_section \
10577 && S_GET_SEGMENT ((fixp)->fx_subsy) == text_section)
10578
10579/* When generating embedded PIC code we must keep all PC relative
10580 relocations, in case the linker has to relax a call. We also need
f6688943
TS
10581 to keep relocations for switch table entries.
10582
10583 We may have combined relocations without symbols in the N32/N64 ABI.
10584 We have to prevent gas from dropping them. */
252b5132 10585
252b5132
RH
10586int
10587mips_force_relocation (fixp)
10588 fixS *fixp;
10589{
10590 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
a161fe53
AM
10591 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY
10592 || S_FORCE_RELOC (fixp->fx_addsy))
252b5132
RH
10593 return 1;
10594
f6688943
TS
10595 if (HAVE_NEWABI
10596 && S_GET_SEGMENT (fixp->fx_addsy) == bfd_abs_section_ptr
10597 && (fixp->fx_r_type == BFD_RELOC_MIPS_SUB
10598 || fixp->fx_r_type == BFD_RELOC_HI16_S
10599 || fixp->fx_r_type == BFD_RELOC_LO16))
10600 return 1;
10601
252b5132
RH
10602 return (mips_pic == EMBEDDED_PIC
10603 && (fixp->fx_pcrel
10604 || SWITCH_TABLE (fixp)
10605 || fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S
10606 || fixp->fx_r_type == BFD_RELOC_PCREL_LO16));
10607}
10608
add55e1f
RS
10609#ifdef OBJ_ELF
10610static int
10611mips_need_elf_addend_fixup (fixP)
10612 fixS *fixP;
10613{
2d2bf3e0
CD
10614 if (S_GET_OTHER (fixP->fx_addsy) == STO_MIPS16)
10615 return 1;
b25a253c
CD
10616 if (mips_pic == EMBEDDED_PIC
10617 && S_IS_WEAK (fixP->fx_addsy))
10618 return 1;
10619 if (mips_pic != EMBEDDED_PIC
10620 && (S_IS_WEAK (fixP->fx_addsy)
bad9ca53 10621 || S_IS_EXTERNAL (fixP->fx_addsy))
2d2bf3e0
CD
10622 && !S_IS_COMMON (fixP->fx_addsy))
10623 return 1;
10624 if (symbol_used_in_reloc_p (fixP->fx_addsy)
10625 && (((bfd_get_section_flags (stdoutput,
10626 S_GET_SEGMENT (fixP->fx_addsy))
10627 & SEC_LINK_ONCE) != 0)
10628 || !strncmp (segment_name (S_GET_SEGMENT (fixP->fx_addsy)),
10629 ".gnu.linkonce",
10630 sizeof (".gnu.linkonce") - 1)))
10631 return 1;
10632 return 0;
add55e1f
RS
10633}
10634#endif
10635
252b5132
RH
10636/* Apply a fixup to the object file. */
10637
94f592af
NC
10638void
10639md_apply_fix3 (fixP, valP, seg)
252b5132 10640 fixS *fixP;
98d3f06f 10641 valueT *valP;
94f592af 10642 segT seg ATTRIBUTE_UNUSED;
252b5132 10643{
874e8986 10644 bfd_byte *buf;
98aa84af
AM
10645 long insn;
10646 valueT value;
252b5132
RH
10647
10648 assert (fixP->fx_size == 4
10649 || fixP->fx_r_type == BFD_RELOC_16
f6688943
TS
10650 || fixP->fx_r_type == BFD_RELOC_32
10651 || fixP->fx_r_type == BFD_RELOC_MIPS_JMP
10652 || fixP->fx_r_type == BFD_RELOC_HI16_S
10653 || fixP->fx_r_type == BFD_RELOC_LO16
10654 || fixP->fx_r_type == BFD_RELOC_GPREL16
76b3015f 10655 || fixP->fx_r_type == BFD_RELOC_MIPS_LITERAL
f6688943 10656 || fixP->fx_r_type == BFD_RELOC_GPREL32
252b5132 10657 || fixP->fx_r_type == BFD_RELOC_64
f6688943
TS
10658 || fixP->fx_r_type == BFD_RELOC_CTOR
10659 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
10660 || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHEST
10661 || fixP->fx_r_type == BFD_RELOC_MIPS_HIGHER
10662 || fixP->fx_r_type == BFD_RELOC_MIPS_SCN_DISP
10663 || fixP->fx_r_type == BFD_RELOC_MIPS_REL16
10664 || fixP->fx_r_type == BFD_RELOC_MIPS_RELGOT
252b5132 10665 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
438c16b8
TS
10666 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
10667 || fixP->fx_r_type == BFD_RELOC_MIPS_JALR);
252b5132 10668
98d3f06f 10669 value = *valP;
252b5132
RH
10670
10671 /* If we aren't adjusting this fixup to be against the section
10672 symbol, we need to adjust the value. */
10673#ifdef OBJ_ELF
10674 if (fixP->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour)
bb2d6cd7 10675 {
add55e1f 10676 if (mips_need_elf_addend_fixup (fixP))
98aa84af 10677 {
d6e9d61a 10678 reloc_howto_type *howto;
98aa84af 10679 valueT symval = S_GET_VALUE (fixP->fx_addsy);
94f592af 10680
98aa84af 10681 value -= symval;
d6e9d61a
MR
10682
10683 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
10684 if (value != 0 && howto->partial_inplace && ! fixP->fx_pcrel)
98aa84af
AM
10685 {
10686 /* In this case, the bfd_install_relocation routine will
10687 incorrectly add the symbol value back in. We just want
7461da6e 10688 the addend to appear in the object file. */
98aa84af 10689 value -= symval;
7461da6e
RS
10690
10691 /* Make sure the addend is still non-zero. If it became zero
10692 after the last operation, set it to a spurious value and
10693 subtract the same value from the object file's contents. */
10694 if (value == 0)
10695 {
10696 value = 8;
10697
10698 /* The in-place addends for LO16 relocations are signed;
10699 leave the matching HI16 in-place addends as zero. */
10700 if (fixP->fx_r_type != BFD_RELOC_HI16_S)
10701 {
7461da6e
RS
10702 bfd_vma contents, mask, field;
10703
7461da6e
RS
10704 contents = bfd_get_bits (fixP->fx_frag->fr_literal
10705 + fixP->fx_where,
10706 fixP->fx_size * 8,
10707 target_big_endian);
10708
10709 /* MASK has bits set where the relocation should go.
10710 FIELD is -value, shifted into the appropriate place
10711 for this relocation. */
10712 mask = 1 << (howto->bitsize - 1);
10713 mask = (((mask - 1) << 1) | 1) << howto->bitpos;
10714 field = (-value >> howto->rightshift) << howto->bitpos;
10715
10716 bfd_put_bits ((field & mask) | (contents & ~mask),
10717 fixP->fx_frag->fr_literal + fixP->fx_where,
10718 fixP->fx_size * 8,
10719 target_big_endian);
10720 }
10721 }
98aa84af
AM
10722 }
10723 }
252b5132 10724
bb2d6cd7
GK
10725 /* This code was generated using trial and error and so is
10726 fragile and not trustworthy. If you change it, you should
10727 rerun the elf-rel, elf-rel2, and empic testcases and ensure
10728 they still pass. */
10729 if (fixP->fx_pcrel || fixP->fx_subsy != NULL)
10730 {
10731 value += fixP->fx_frag->fr_address + fixP->fx_where;
10732
10733 /* BFD's REL handling, for MIPS, is _very_ weird.
10734 This gives the right results, but it can't possibly
10735 be the way things are supposed to work. */
cb56d3d3
TS
10736 if ((fixP->fx_r_type != BFD_RELOC_16_PCREL
10737 && fixP->fx_r_type != BFD_RELOC_16_PCREL_S2)
bb2d6cd7
GK
10738 || S_GET_SEGMENT (fixP->fx_addsy) != undefined_section)
10739 value += fixP->fx_frag->fr_address + fixP->fx_where;
10740 }
10741 }
10742#endif
252b5132 10743
94f592af 10744 fixP->fx_addnumber = value; /* Remember value for tc_gen_reloc. */
252b5132
RH
10745
10746 if (fixP->fx_addsy == NULL && ! fixP->fx_pcrel)
10747 fixP->fx_done = 1;
10748
10749 switch (fixP->fx_r_type)
10750 {
10751 case BFD_RELOC_MIPS_JMP:
e369bcce
TS
10752 case BFD_RELOC_MIPS_SHIFT5:
10753 case BFD_RELOC_MIPS_SHIFT6:
10754 case BFD_RELOC_MIPS_GOT_DISP:
10755 case BFD_RELOC_MIPS_GOT_PAGE:
10756 case BFD_RELOC_MIPS_GOT_OFST:
10757 case BFD_RELOC_MIPS_SUB:
10758 case BFD_RELOC_MIPS_INSERT_A:
10759 case BFD_RELOC_MIPS_INSERT_B:
10760 case BFD_RELOC_MIPS_DELETE:
10761 case BFD_RELOC_MIPS_HIGHEST:
10762 case BFD_RELOC_MIPS_HIGHER:
10763 case BFD_RELOC_MIPS_SCN_DISP:
10764 case BFD_RELOC_MIPS_REL16:
10765 case BFD_RELOC_MIPS_RELGOT:
10766 case BFD_RELOC_MIPS_JALR:
252b5132
RH
10767 case BFD_RELOC_HI16:
10768 case BFD_RELOC_HI16_S:
cdf6fd85 10769 case BFD_RELOC_GPREL16:
252b5132
RH
10770 case BFD_RELOC_MIPS_LITERAL:
10771 case BFD_RELOC_MIPS_CALL16:
10772 case BFD_RELOC_MIPS_GOT16:
cdf6fd85 10773 case BFD_RELOC_GPREL32:
252b5132
RH
10774 case BFD_RELOC_MIPS_GOT_HI16:
10775 case BFD_RELOC_MIPS_GOT_LO16:
10776 case BFD_RELOC_MIPS_CALL_HI16:
10777 case BFD_RELOC_MIPS_CALL_LO16:
10778 case BFD_RELOC_MIPS16_GPREL:
10779 if (fixP->fx_pcrel)
10780 as_bad_where (fixP->fx_file, fixP->fx_line,
10781 _("Invalid PC relative reloc"));
10782 /* Nothing needed to do. The value comes from the reloc entry */
10783 break;
10784
10785 case BFD_RELOC_MIPS16_JMP:
10786 /* We currently always generate a reloc against a symbol, which
10787 means that we don't want an addend even if the symbol is
10788 defined. */
10789 fixP->fx_addnumber = 0;
10790 break;
10791
10792 case BFD_RELOC_PCREL_HI16_S:
10793 /* The addend for this is tricky if it is internal, so we just
10794 do everything here rather than in bfd_install_relocation. */
bdaaa2e1 10795 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
bb2d6cd7
GK
10796 && !fixP->fx_done
10797 && value != 0)
10798 break;
10799 if (fixP->fx_addsy
10800 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
252b5132
RH
10801 {
10802 /* For an external symbol adjust by the address to make it
10803 pcrel_offset. We use the address of the RELLO reloc
10804 which follows this one. */
10805 value += (fixP->fx_next->fx_frag->fr_address
10806 + fixP->fx_next->fx_where);
10807 }
e7d556df 10808 value = ((value + 0x8000) >> 16) & 0xffff;
874e8986 10809 buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
252b5132
RH
10810 if (target_big_endian)
10811 buf += 2;
874e8986 10812 md_number_to_chars ((char *) buf, value, 2);
252b5132
RH
10813 break;
10814
10815 case BFD_RELOC_PCREL_LO16:
10816 /* The addend for this is tricky if it is internal, so we just
10817 do everything here rather than in bfd_install_relocation. */
bdaaa2e1 10818 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
bb2d6cd7
GK
10819 && !fixP->fx_done
10820 && value != 0)
10821 break;
10822 if (fixP->fx_addsy
10823 && (symbol_get_bfdsym (fixP->fx_addsy)->flags & BSF_SECTION_SYM) == 0)
252b5132 10824 value += fixP->fx_frag->fr_address + fixP->fx_where;
874e8986 10825 buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
252b5132
RH
10826 if (target_big_endian)
10827 buf += 2;
874e8986 10828 md_number_to_chars ((char *) buf, value, 2);
252b5132
RH
10829 break;
10830
10831 case BFD_RELOC_64:
10832 /* This is handled like BFD_RELOC_32, but we output a sign
10833 extended value if we are only 32 bits. */
10834 if (fixP->fx_done
10835 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
10836 {
10837 if (8 <= sizeof (valueT))
10838 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
10839 value, 8);
10840 else
10841 {
10842 long w1, w2;
10843 long hiv;
10844
10845 w1 = w2 = fixP->fx_where;
10846 if (target_big_endian)
10847 w1 += 4;
10848 else
10849 w2 += 4;
10850 md_number_to_chars (fixP->fx_frag->fr_literal + w1, value, 4);
10851 if ((value & 0x80000000) != 0)
10852 hiv = 0xffffffff;
10853 else
10854 hiv = 0;
10855 md_number_to_chars (fixP->fx_frag->fr_literal + w2, hiv, 4);
10856 }
10857 }
10858 break;
10859
056350c6 10860 case BFD_RELOC_RVA:
252b5132
RH
10861 case BFD_RELOC_32:
10862 /* If we are deleting this reloc entry, we must fill in the
10863 value now. This can happen if we have a .word which is not
10864 resolved when it appears but is later defined. We also need
10865 to fill in the value if this is an embedded PIC switch table
10866 entry. */
10867 if (fixP->fx_done
10868 || (mips_pic == EMBEDDED_PIC && SWITCH_TABLE (fixP)))
10869 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
10870 value, 4);
10871 break;
10872
10873 case BFD_RELOC_16:
10874 /* If we are deleting this reloc entry, we must fill in the
10875 value now. */
10876 assert (fixP->fx_size == 2);
10877 if (fixP->fx_done)
10878 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
10879 value, 2);
10880 break;
10881
10882 case BFD_RELOC_LO16:
10883 /* When handling an embedded PIC switch statement, we can wind
10884 up deleting a LO16 reloc. See the 'o' case in mips_ip. */
10885 if (fixP->fx_done)
10886 {
98aa84af 10887 if (value + 0x8000 > 0xffff)
252b5132
RH
10888 as_bad_where (fixP->fx_file, fixP->fx_line,
10889 _("relocation overflow"));
874e8986 10890 buf = (bfd_byte *) fixP->fx_frag->fr_literal + fixP->fx_where;
252b5132
RH
10891 if (target_big_endian)
10892 buf += 2;
874e8986 10893 md_number_to_chars ((char *) buf, value, 2);
252b5132
RH
10894 }
10895 break;
10896
10897 case BFD_RELOC_16_PCREL_S2:
cb56d3d3
TS
10898 if ((value & 0x3) != 0)
10899 as_bad_where (fixP->fx_file, fixP->fx_line,
10900 _("Branch to odd address (%lx)"), (long) value);
10901
10902 /* Fall through. */
10903
10904 case BFD_RELOC_16_PCREL:
252b5132
RH
10905 /*
10906 * We need to save the bits in the instruction since fixup_segment()
10907 * might be deleting the relocation entry (i.e., a branch within
10908 * the current segment).
10909 */
bb2d6cd7
GK
10910 if (!fixP->fx_done && value != 0)
10911 break;
10912 /* If 'value' is zero, the remaining reloc code won't actually
10913 do the store, so it must be done here. This is probably
10914 a bug somewhere. */
b25a253c
CD
10915 if (!fixP->fx_done
10916 && (fixP->fx_r_type != BFD_RELOC_16_PCREL_S2
10917 || fixP->fx_addsy == NULL /* ??? */
10918 || ! S_IS_DEFINED (fixP->fx_addsy)))
bb2d6cd7 10919 value -= fixP->fx_frag->fr_address + fixP->fx_where;
bdaaa2e1 10920
98aa84af 10921 value = (offsetT) value >> 2;
252b5132
RH
10922
10923 /* update old instruction data */
874e8986 10924 buf = (bfd_byte *) (fixP->fx_where + fixP->fx_frag->fr_literal);
252b5132
RH
10925 if (target_big_endian)
10926 insn = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
10927 else
10928 insn = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
10929
98aa84af 10930 if (value + 0x8000 <= 0xffff)
252b5132
RH
10931 insn |= value & 0xffff;
10932 else
10933 {
10934 /* The branch offset is too large. If this is an
10935 unconditional branch, and we are not generating PIC code,
10936 we can convert it to an absolute jump instruction. */
10937 if (mips_pic == NO_PIC
10938 && fixP->fx_done
10939 && fixP->fx_frag->fr_address >= text_section->vma
10940 && (fixP->fx_frag->fr_address
10941 < text_section->vma + text_section->_raw_size)
10942 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
10943 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
10944 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
10945 {
10946 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
10947 insn = 0x0c000000; /* jal */
10948 else
10949 insn = 0x08000000; /* j */
10950 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
10951 fixP->fx_done = 0;
10952 fixP->fx_addsy = section_symbol (text_section);
10953 fixP->fx_addnumber = (value << 2) + md_pcrel_from (fixP);
10954 }
10955 else
10956 {
10957 /* FIXME. It would be possible in principle to handle
10958 conditional branches which overflow. They could be
10959 transformed into a branch around a jump. This would
10960 require setting up variant frags for each different
10961 branch type. The native MIPS assembler attempts to
10962 handle these cases, but it appears to do it
10963 incorrectly. */
10964 as_bad_where (fixP->fx_file, fixP->fx_line,
10965 _("Branch out of range"));
10966 }
10967 }
10968
10969 md_number_to_chars ((char *) buf, (valueT) insn, 4);
10970 break;
10971
10972 case BFD_RELOC_VTABLE_INHERIT:
10973 fixP->fx_done = 0;
10974 if (fixP->fx_addsy
10975 && !S_IS_DEFINED (fixP->fx_addsy)
10976 && !S_IS_WEAK (fixP->fx_addsy))
10977 S_SET_WEAK (fixP->fx_addsy);
10978 break;
10979
10980 case BFD_RELOC_VTABLE_ENTRY:
10981 fixP->fx_done = 0;
10982 break;
10983
10984 default:
10985 internalError ();
10986 }
252b5132
RH
10987}
10988
10989#if 0
10990void
10991printInsn (oc)
10992 unsigned long oc;
10993{
10994 const struct mips_opcode *p;
10995 int treg, sreg, dreg, shamt;
10996 short imm;
10997 const char *args;
10998 int i;
10999
11000 for (i = 0; i < NUMOPCODES; ++i)
11001 {
11002 p = &mips_opcodes[i];
11003 if (((oc & p->mask) == p->match) && (p->pinfo != INSN_MACRO))
11004 {
11005 printf ("%08lx %s\t", oc, p->name);
11006 treg = (oc >> 16) & 0x1f;
11007 sreg = (oc >> 21) & 0x1f;
11008 dreg = (oc >> 11) & 0x1f;
11009 shamt = (oc >> 6) & 0x1f;
11010 imm = oc;
11011 for (args = p->args;; ++args)
11012 {
11013 switch (*args)
11014 {
11015 case '\0':
11016 printf ("\n");
11017 break;
11018
11019 case ',':
11020 case '(':
11021 case ')':
11022 printf ("%c", *args);
11023 continue;
11024
11025 case 'r':
11026 assert (treg == sreg);
11027 printf ("$%d,$%d", treg, sreg);
11028 continue;
11029
11030 case 'd':
11031 case 'G':
11032 printf ("$%d", dreg);
11033 continue;
11034
11035 case 't':
11036 case 'E':
11037 printf ("$%d", treg);
11038 continue;
11039
11040 case 'k':
11041 printf ("0x%x", treg);
11042 continue;
11043
11044 case 'b':
11045 case 's':
11046 printf ("$%d", sreg);
11047 continue;
11048
11049 case 'a':
11050 printf ("0x%08lx", oc & 0x1ffffff);
11051 continue;
11052
11053 case 'i':
11054 case 'j':
11055 case 'o':
11056 case 'u':
11057 printf ("%d", imm);
11058 continue;
11059
11060 case '<':
11061 case '>':
11062 printf ("$%d", shamt);
11063 continue;
11064
11065 default:
11066 internalError ();
11067 }
11068 break;
11069 }
11070 return;
11071 }
11072 }
11073 printf (_("%08lx UNDEFINED\n"), oc);
11074}
11075#endif
11076
11077static symbolS *
11078get_symbol ()
11079{
11080 int c;
11081 char *name;
11082 symbolS *p;
11083
11084 name = input_line_pointer;
11085 c = get_symbol_end ();
11086 p = (symbolS *) symbol_find_or_make (name);
11087 *input_line_pointer = c;
11088 return p;
11089}
11090
11091/* Align the current frag to a given power of two. The MIPS assembler
11092 also automatically adjusts any preceding label. */
11093
11094static void
11095mips_align (to, fill, label)
11096 int to;
11097 int fill;
11098 symbolS *label;
11099{
11100 mips_emit_delays (false);
11101 frag_align (to, fill, 0);
11102 record_alignment (now_seg, to);
11103 if (label != NULL)
11104 {
11105 assert (S_GET_SEGMENT (label) == now_seg);
49309057 11106 symbol_set_frag (label, frag_now);
252b5132
RH
11107 S_SET_VALUE (label, (valueT) frag_now_fix ());
11108 }
11109}
11110
11111/* Align to a given power of two. .align 0 turns off the automatic
11112 alignment used by the data creating pseudo-ops. */
11113
11114static void
11115s_align (x)
43841e91 11116 int x ATTRIBUTE_UNUSED;
252b5132
RH
11117{
11118 register int temp;
11119 register long temp_fill;
11120 long max_alignment = 15;
11121
11122 /*
11123
11124 o Note that the assembler pulls down any immediately preceeding label
11125 to the aligned address.
11126 o It's not documented but auto alignment is reinstated by
11127 a .align pseudo instruction.
11128 o Note also that after auto alignment is turned off the mips assembler
11129 issues an error on attempt to assemble an improperly aligned data item.
11130 We don't.
11131
11132 */
11133
11134 temp = get_absolute_expression ();
11135 if (temp > max_alignment)
11136 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
11137 else if (temp < 0)
11138 {
11139 as_warn (_("Alignment negative: 0 assumed."));
11140 temp = 0;
11141 }
11142 if (*input_line_pointer == ',')
11143 {
f9419b05 11144 ++input_line_pointer;
252b5132
RH
11145 temp_fill = get_absolute_expression ();
11146 }
11147 else
11148 temp_fill = 0;
11149 if (temp)
11150 {
11151 auto_align = 1;
11152 mips_align (temp, (int) temp_fill,
11153 insn_labels != NULL ? insn_labels->label : NULL);
11154 }
11155 else
11156 {
11157 auto_align = 0;
11158 }
11159
11160 demand_empty_rest_of_line ();
11161}
11162
11163void
11164mips_flush_pending_output ()
11165{
11166 mips_emit_delays (false);
11167 mips_clear_insn_labels ();
11168}
11169
11170static void
11171s_change_sec (sec)
11172 int sec;
11173{
11174 segT seg;
11175
11176 /* When generating embedded PIC code, we only use the .text, .lit8,
11177 .sdata and .sbss sections. We change the .data and .rdata
11178 pseudo-ops to use .sdata. */
11179 if (mips_pic == EMBEDDED_PIC
11180 && (sec == 'd' || sec == 'r'))
11181 sec = 's';
11182
11183#ifdef OBJ_ELF
11184 /* The ELF backend needs to know that we are changing sections, so
11185 that .previous works correctly. We could do something like check
b6ff326e 11186 for an obj_section_change_hook macro, but that might be confusing
252b5132
RH
11187 as it would not be appropriate to use it in the section changing
11188 functions in read.c, since obj-elf.c intercepts those. FIXME:
11189 This should be cleaner, somehow. */
11190 obj_elf_section_change_hook ();
11191#endif
11192
11193 mips_emit_delays (false);
11194 switch (sec)
11195 {
11196 case 't':
11197 s_text (0);
11198 break;
11199 case 'd':
11200 s_data (0);
11201 break;
11202 case 'b':
11203 subseg_set (bss_section, (subsegT) get_absolute_expression ());
11204 demand_empty_rest_of_line ();
11205 break;
11206
11207 case 'r':
11208 if (USE_GLOBAL_POINTER_OPT)
11209 {
11210 seg = subseg_new (RDATA_SECTION_NAME,
11211 (subsegT) get_absolute_expression ());
11212 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11213 {
11214 bfd_set_section_flags (stdoutput, seg,
11215 (SEC_ALLOC
11216 | SEC_LOAD
11217 | SEC_READONLY
11218 | SEC_RELOC
11219 | SEC_DATA));
11220 if (strcmp (TARGET_OS, "elf") != 0)
e799a695 11221 record_alignment (seg, 4);
252b5132
RH
11222 }
11223 demand_empty_rest_of_line ();
11224 }
11225 else
11226 {
11227 as_bad (_("No read only data section in this object file format"));
11228 demand_empty_rest_of_line ();
11229 return;
11230 }
11231 break;
11232
11233 case 's':
11234 if (USE_GLOBAL_POINTER_OPT)
11235 {
11236 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
11237 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
11238 {
11239 bfd_set_section_flags (stdoutput, seg,
11240 SEC_ALLOC | SEC_LOAD | SEC_RELOC
11241 | SEC_DATA);
11242 if (strcmp (TARGET_OS, "elf") != 0)
e799a695 11243 record_alignment (seg, 4);
252b5132
RH
11244 }
11245 demand_empty_rest_of_line ();
11246 break;
11247 }
11248 else
11249 {
11250 as_bad (_("Global pointers not supported; recompile -G 0"));
11251 demand_empty_rest_of_line ();
11252 return;
11253 }
11254 }
11255
11256 auto_align = 1;
11257}
cca86cc8
SC
11258
11259void
11260s_change_section (ignore)
11261 int ignore ATTRIBUTE_UNUSED;
11262{
7ed4a06a 11263#ifdef OBJ_ELF
cca86cc8
SC
11264 char *section_name;
11265 char c;
11266 char *next_c;
11267 char *p;
11268 int section_type;
11269 int section_flag;
11270 int section_entry_size;
11271 int section_alignment;
cca86cc8 11272
7ed4a06a
TS
11273 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
11274 return;
11275
cca86cc8
SC
11276 section_name = input_line_pointer;
11277 c = get_symbol_end ();
11278 next_c = input_line_pointer + 1;
11279 /* just after name is now '\0' */
11280 p = input_line_pointer;
11281
11282 /* Do we have .section Name<,"flags"> */
11283 if (c == '\n' || (c == ',' && *next_c == '"') || c == '"')
11284 {
11285 *p = c;
11286 input_line_pointer = section_name;
11287 obj_elf_section (ignore);
11288 return;
11289 }
11290 input_line_pointer++;
11291
11292 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
11293 if (c == ',')
11294 section_type = get_absolute_expression ();
11295 else
11296 section_type = 0;
11297 if (*input_line_pointer++ == ',')
11298 section_flag = get_absolute_expression ();
11299 else
11300 section_flag = 0;
11301 if (*input_line_pointer++ == ',')
11302 section_entry_size = get_absolute_expression ();
11303 else
11304 section_entry_size = 0;
11305 if (*input_line_pointer++ == ',')
11306 section_alignment = get_absolute_expression ();
11307 else
11308 section_alignment = 0;
11309
11310 obj_elf_change_section (section_name, section_type, section_flag,
11311 section_entry_size, 0, 0, 0);
7ed4a06a 11312#endif /* OBJ_ELF */
cca86cc8 11313}
252b5132
RH
11314
11315void
11316mips_enable_auto_align ()
11317{
11318 auto_align = 1;
11319}
11320
11321static void
11322s_cons (log_size)
11323 int log_size;
11324{
11325 symbolS *label;
11326
11327 label = insn_labels != NULL ? insn_labels->label : NULL;
11328 mips_emit_delays (false);
11329 if (log_size > 0 && auto_align)
11330 mips_align (log_size, 0, label);
11331 mips_clear_insn_labels ();
11332 cons (1 << log_size);
11333}
11334
11335static void
11336s_float_cons (type)
11337 int type;
11338{
11339 symbolS *label;
11340
11341 label = insn_labels != NULL ? insn_labels->label : NULL;
11342
11343 mips_emit_delays (false);
11344
11345 if (auto_align)
49309057
ILT
11346 {
11347 if (type == 'd')
11348 mips_align (3, 0, label);
11349 else
11350 mips_align (2, 0, label);
11351 }
252b5132
RH
11352
11353 mips_clear_insn_labels ();
11354
11355 float_cons (type);
11356}
11357
11358/* Handle .globl. We need to override it because on Irix 5 you are
11359 permitted to say
11360 .globl foo .text
11361 where foo is an undefined symbol, to mean that foo should be
11362 considered to be the address of a function. */
11363
11364static void
11365s_mips_globl (x)
43841e91 11366 int x ATTRIBUTE_UNUSED;
252b5132
RH
11367{
11368 char *name;
11369 int c;
11370 symbolS *symbolP;
11371 flagword flag;
11372
11373 name = input_line_pointer;
11374 c = get_symbol_end ();
11375 symbolP = symbol_find_or_make (name);
11376 *input_line_pointer = c;
11377 SKIP_WHITESPACE ();
11378
11379 /* On Irix 5, every global symbol that is not explicitly labelled as
11380 being a function is apparently labelled as being an object. */
11381 flag = BSF_OBJECT;
11382
11383 if (! is_end_of_line[(unsigned char) *input_line_pointer])
11384 {
11385 char *secname;
11386 asection *sec;
11387
11388 secname = input_line_pointer;
11389 c = get_symbol_end ();
11390 sec = bfd_get_section_by_name (stdoutput, secname);
11391 if (sec == NULL)
11392 as_bad (_("%s: no such section"), secname);
11393 *input_line_pointer = c;
11394
11395 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
11396 flag = BSF_FUNCTION;
11397 }
11398
49309057 11399 symbol_get_bfdsym (symbolP)->flags |= flag;
252b5132
RH
11400
11401 S_SET_EXTERNAL (symbolP);
11402 demand_empty_rest_of_line ();
11403}
11404
11405static void
11406s_option (x)
43841e91 11407 int x ATTRIBUTE_UNUSED;
252b5132
RH
11408{
11409 char *opt;
11410 char c;
11411
11412 opt = input_line_pointer;
11413 c = get_symbol_end ();
11414
11415 if (*opt == 'O')
11416 {
11417 /* FIXME: What does this mean? */
11418 }
11419 else if (strncmp (opt, "pic", 3) == 0)
11420 {
11421 int i;
11422
11423 i = atoi (opt + 3);
11424 if (i == 0)
11425 mips_pic = NO_PIC;
11426 else if (i == 2)
11427 mips_pic = SVR4_PIC;
11428 else
11429 as_bad (_(".option pic%d not supported"), i);
11430
11431 if (USE_GLOBAL_POINTER_OPT && mips_pic == SVR4_PIC)
11432 {
11433 if (g_switch_seen && g_switch_value != 0)
11434 as_warn (_("-G may not be used with SVR4 PIC code"));
11435 g_switch_value = 0;
11436 bfd_set_gp_size (stdoutput, 0);
11437 }
11438 }
11439 else
11440 as_warn (_("Unrecognized option \"%s\""), opt);
11441
11442 *input_line_pointer = c;
11443 demand_empty_rest_of_line ();
11444}
11445
11446/* This structure is used to hold a stack of .set values. */
11447
e972090a
NC
11448struct mips_option_stack
11449{
252b5132
RH
11450 struct mips_option_stack *next;
11451 struct mips_set_options options;
11452};
11453
11454static struct mips_option_stack *mips_opts_stack;
11455
11456/* Handle the .set pseudo-op. */
11457
11458static void
11459s_mipsset (x)
43841e91 11460 int x ATTRIBUTE_UNUSED;
252b5132
RH
11461{
11462 char *name = input_line_pointer, ch;
11463
11464 while (!is_end_of_line[(unsigned char) *input_line_pointer])
f9419b05 11465 ++input_line_pointer;
252b5132
RH
11466 ch = *input_line_pointer;
11467 *input_line_pointer = '\0';
11468
11469 if (strcmp (name, "reorder") == 0)
11470 {
11471 if (mips_opts.noreorder && prev_nop_frag != NULL)
11472 {
11473 /* If we still have pending nops, we can discard them. The
11474 usual nop handling will insert any that are still
bdaaa2e1 11475 needed. */
252b5132
RH
11476 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11477 * (mips_opts.mips16 ? 2 : 4));
11478 prev_nop_frag = NULL;
11479 }
11480 mips_opts.noreorder = 0;
11481 }
11482 else if (strcmp (name, "noreorder") == 0)
11483 {
11484 mips_emit_delays (true);
11485 mips_opts.noreorder = 1;
11486 mips_any_noreorder = 1;
11487 }
11488 else if (strcmp (name, "at") == 0)
11489 {
11490 mips_opts.noat = 0;
11491 }
11492 else if (strcmp (name, "noat") == 0)
11493 {
11494 mips_opts.noat = 1;
11495 }
11496 else if (strcmp (name, "macro") == 0)
11497 {
11498 mips_opts.warn_about_macros = 0;
11499 }
11500 else if (strcmp (name, "nomacro") == 0)
11501 {
11502 if (mips_opts.noreorder == 0)
11503 as_bad (_("`noreorder' must be set before `nomacro'"));
11504 mips_opts.warn_about_macros = 1;
11505 }
11506 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
11507 {
11508 mips_opts.nomove = 0;
11509 }
11510 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
11511 {
11512 mips_opts.nomove = 1;
11513 }
11514 else if (strcmp (name, "bopt") == 0)
11515 {
11516 mips_opts.nobopt = 0;
11517 }
11518 else if (strcmp (name, "nobopt") == 0)
11519 {
11520 mips_opts.nobopt = 1;
11521 }
11522 else if (strcmp (name, "mips16") == 0
11523 || strcmp (name, "MIPS-16") == 0)
11524 mips_opts.mips16 = 1;
11525 else if (strcmp (name, "nomips16") == 0
11526 || strcmp (name, "noMIPS-16") == 0)
11527 mips_opts.mips16 = 0;
1f25f5d3
CD
11528 else if (strcmp (name, "mips3d") == 0)
11529 mips_opts.ase_mips3d = 1;
11530 else if (strcmp (name, "nomips3d") == 0)
11531 mips_opts.ase_mips3d = 0;
a4672219
TS
11532 else if (strcmp (name, "mdmx") == 0)
11533 mips_opts.ase_mdmx = 1;
11534 else if (strcmp (name, "nomdmx") == 0)
11535 mips_opts.ase_mdmx = 0;
252b5132
RH
11536 else if (strncmp (name, "mips", 4) == 0)
11537 {
11538 int isa;
11539
11540 /* Permit the user to change the ISA on the fly. Needless to
11541 say, misuse can cause serious problems. */
11542 isa = atoi (name + 4);
553178e4 11543 switch (isa)
98d3f06f
KH
11544 {
11545 case 0:
11546 mips_opts.gp32 = file_mips_gp32;
11547 mips_opts.fp32 = file_mips_fp32;
98d3f06f
KH
11548 break;
11549 case 1:
11550 case 2:
11551 case 32:
11552 mips_opts.gp32 = 1;
11553 mips_opts.fp32 = 1;
11554 break;
11555 case 3:
11556 case 4:
11557 case 5:
11558 case 64:
98d3f06f
KH
11559 mips_opts.gp32 = 0;
11560 mips_opts.fp32 = 0;
11561 break;
11562 default:
11563 as_bad (_("unknown ISA level %s"), name + 4);
11564 break;
11565 }
553178e4 11566
e7af610e 11567 switch (isa)
98d3f06f
KH
11568 {
11569 case 0: mips_opts.isa = file_mips_isa; break;
11570 case 1: mips_opts.isa = ISA_MIPS1; break;
11571 case 2: mips_opts.isa = ISA_MIPS2; break;
11572 case 3: mips_opts.isa = ISA_MIPS3; break;
11573 case 4: mips_opts.isa = ISA_MIPS4; break;
11574 case 5: mips_opts.isa = ISA_MIPS5; break;
11575 case 32: mips_opts.isa = ISA_MIPS32; break;
11576 case 64: mips_opts.isa = ISA_MIPS64; break;
11577 default: as_bad (_("unknown ISA level %s"), name + 4); break;
11578 }
252b5132
RH
11579 }
11580 else if (strcmp (name, "autoextend") == 0)
11581 mips_opts.noautoextend = 0;
11582 else if (strcmp (name, "noautoextend") == 0)
11583 mips_opts.noautoextend = 1;
11584 else if (strcmp (name, "push") == 0)
11585 {
11586 struct mips_option_stack *s;
11587
11588 s = (struct mips_option_stack *) xmalloc (sizeof *s);
11589 s->next = mips_opts_stack;
11590 s->options = mips_opts;
11591 mips_opts_stack = s;
11592 }
11593 else if (strcmp (name, "pop") == 0)
11594 {
11595 struct mips_option_stack *s;
11596
11597 s = mips_opts_stack;
11598 if (s == NULL)
11599 as_bad (_(".set pop with no .set push"));
11600 else
11601 {
11602 /* If we're changing the reorder mode we need to handle
11603 delay slots correctly. */
11604 if (s->options.noreorder && ! mips_opts.noreorder)
11605 mips_emit_delays (true);
11606 else if (! s->options.noreorder && mips_opts.noreorder)
11607 {
11608 if (prev_nop_frag != NULL)
11609 {
11610 prev_nop_frag->fr_fix -= (prev_nop_frag_holds
11611 * (mips_opts.mips16 ? 2 : 4));
11612 prev_nop_frag = NULL;
11613 }
11614 }
11615
11616 mips_opts = s->options;
11617 mips_opts_stack = s->next;
11618 free (s);
11619 }
11620 }
11621 else
11622 {
11623 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
11624 }
11625 *input_line_pointer = ch;
11626 demand_empty_rest_of_line ();
11627}
11628
11629/* Handle the .abicalls pseudo-op. I believe this is equivalent to
11630 .option pic2. It means to generate SVR4 PIC calls. */
11631
11632static void
11633s_abicalls (ignore)
43841e91 11634 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
11635{
11636 mips_pic = SVR4_PIC;
11637 if (USE_GLOBAL_POINTER_OPT)
11638 {
11639 if (g_switch_seen && g_switch_value != 0)
11640 as_warn (_("-G may not be used with SVR4 PIC code"));
11641 g_switch_value = 0;
11642 }
11643 bfd_set_gp_size (stdoutput, 0);
11644 demand_empty_rest_of_line ();
11645}
11646
11647/* Handle the .cpload pseudo-op. This is used when generating SVR4
11648 PIC code. It sets the $gp register for the function based on the
11649 function address, which is in the register named in the argument.
11650 This uses a relocation against _gp_disp, which is handled specially
11651 by the linker. The result is:
11652 lui $gp,%hi(_gp_disp)
11653 addiu $gp,$gp,%lo(_gp_disp)
11654 addu $gp,$gp,.cpload argument
11655 The .cpload argument is normally $25 == $t9. */
11656
11657static void
11658s_cpload (ignore)
43841e91 11659 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
11660{
11661 expressionS ex;
11662 int icnt = 0;
11663
6478892d
TS
11664 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
11665 .cpload is ignored. */
11666 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
11667 {
11668 s_ignore (0);
11669 return;
11670 }
11671
d3ecfc59 11672 /* .cpload should be in a .set noreorder section. */
252b5132
RH
11673 if (mips_opts.noreorder == 0)
11674 as_warn (_(".cpload not in noreorder section"));
11675
11676 ex.X_op = O_symbol;
11677 ex.X_add_symbol = symbol_find_or_make ("_gp_disp");
11678 ex.X_op_symbol = NULL;
11679 ex.X_add_number = 0;
11680
11681 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
49309057 11682 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
252b5132 11683
c9914766
TS
11684 macro_build_lui (NULL, &icnt, &ex, mips_gp_register);
11685 macro_build ((char *) NULL, &icnt, &ex, "addiu", "t,r,j",
11686 mips_gp_register, mips_gp_register, (int) BFD_RELOC_LO16);
252b5132
RH
11687
11688 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "addu", "d,v,t",
c9914766 11689 mips_gp_register, mips_gp_register, tc_get_register (0));
252b5132
RH
11690
11691 demand_empty_rest_of_line ();
11692}
11693
6478892d
TS
11694/* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
11695 .cpsetup $reg1, offset|$reg2, label
11696
11697 If offset is given, this results in:
11698 sd $gp, offset($sp)
956cd1d6 11699 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
11700 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
11701 daddu $gp, $gp, $reg1
6478892d
TS
11702
11703 If $reg2 is given, this results in:
11704 daddu $reg2, $gp, $0
956cd1d6 11705 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
11706 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
11707 daddu $gp, $gp, $reg1
11708 $reg1 is normally $25 == $t9. */
6478892d
TS
11709static void
11710s_cpsetup (ignore)
11711 int ignore ATTRIBUTE_UNUSED;
11712{
11713 expressionS ex_off;
11714 expressionS ex_sym;
11715 int reg1;
11716 int icnt = 0;
f21f8242 11717 char *f;
6478892d 11718
8586fc66 11719 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
6478892d
TS
11720 We also need NewABI support. */
11721 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11722 {
11723 s_ignore (0);
11724 return;
11725 }
11726
11727 reg1 = tc_get_register (0);
11728 SKIP_WHITESPACE ();
11729 if (*input_line_pointer != ',')
11730 {
11731 as_bad (_("missing argument separator ',' for .cpsetup"));
11732 return;
11733 }
11734 else
80245285 11735 ++input_line_pointer;
6478892d
TS
11736 SKIP_WHITESPACE ();
11737 if (*input_line_pointer == '$')
80245285
TS
11738 {
11739 mips_cpreturn_register = tc_get_register (0);
11740 mips_cpreturn_offset = -1;
11741 }
6478892d 11742 else
80245285
TS
11743 {
11744 mips_cpreturn_offset = get_absolute_expression ();
11745 mips_cpreturn_register = -1;
11746 }
6478892d
TS
11747 SKIP_WHITESPACE ();
11748 if (*input_line_pointer != ',')
11749 {
11750 as_bad (_("missing argument separator ',' for .cpsetup"));
11751 return;
11752 }
11753 else
f9419b05 11754 ++input_line_pointer;
6478892d 11755 SKIP_WHITESPACE ();
f21f8242 11756 expression (&ex_sym);
6478892d
TS
11757
11758 if (mips_cpreturn_register == -1)
11759 {
11760 ex_off.X_op = O_constant;
11761 ex_off.X_add_symbol = NULL;
11762 ex_off.X_op_symbol = NULL;
11763 ex_off.X_add_number = mips_cpreturn_offset;
11764
11765 macro_build ((char *) NULL, &icnt, &ex_off, "sd", "t,o(b)",
11766 mips_gp_register, (int) BFD_RELOC_LO16, SP);
11767 }
11768 else
11769 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
11770 "d,v,t", mips_cpreturn_register, mips_gp_register, 0);
11771
f21f8242
AO
11772 /* Ensure there's room for the next two instructions, so that `f'
11773 doesn't end up with an address in the wrong frag. */
11774 frag_grow (8);
11775 f = frag_more (0);
6478892d
TS
11776 macro_build ((char *) NULL, &icnt, &ex_sym, "lui", "t,u", mips_gp_register,
11777 (int) BFD_RELOC_GPREL16);
f21f8242
AO
11778 fix_new (frag_now, f - frag_now->fr_literal,
11779 0, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
11780 fix_new (frag_now, f - frag_now->fr_literal,
11781 0, NULL, 0, 0, BFD_RELOC_HI16_S);
11782
11783 f = frag_more (0);
6478892d
TS
11784 macro_build ((char *) NULL, &icnt, &ex_sym, "addiu", "t,r,j",
11785 mips_gp_register, mips_gp_register, (int) BFD_RELOC_GPREL16);
f21f8242
AO
11786 fix_new (frag_now, f - frag_now->fr_literal,
11787 0, NULL, 0, 0, BFD_RELOC_MIPS_SUB);
11788 fix_new (frag_now, f - frag_now->fr_literal,
11789 0, NULL, 0, 0, BFD_RELOC_LO16);
11790
8586fc66
TS
11791 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
11792 HAVE_64BIT_ADDRESSES ? "daddu" : "addu", "d,v,t",
11793 mips_gp_register, mips_gp_register, reg1);
6478892d
TS
11794
11795 demand_empty_rest_of_line ();
11796}
11797
11798static void
11799s_cplocal (ignore)
11800 int ignore ATTRIBUTE_UNUSED;
11801{
11802 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
11803 .cplocal is ignored. */
11804 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11805 {
11806 s_ignore (0);
11807 return;
11808 }
11809
11810 mips_gp_register = tc_get_register (0);
85b51719 11811 demand_empty_rest_of_line ();
6478892d
TS
11812}
11813
252b5132
RH
11814/* Handle the .cprestore pseudo-op. This stores $gp into a given
11815 offset from $sp. The offset is remembered, and after making a PIC
11816 call $gp is restored from that location. */
11817
11818static void
11819s_cprestore (ignore)
43841e91 11820 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
11821{
11822 expressionS ex;
11823 int icnt = 0;
11824
6478892d 11825 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
c9914766 11826 .cprestore is ignored. */
6478892d 11827 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
11828 {
11829 s_ignore (0);
11830 return;
11831 }
11832
11833 mips_cprestore_offset = get_absolute_expression ();
7a621144 11834 mips_cprestore_valid = 1;
252b5132
RH
11835
11836 ex.X_op = O_constant;
11837 ex.X_add_symbol = NULL;
11838 ex.X_op_symbol = NULL;
11839 ex.X_add_number = mips_cprestore_offset;
11840
c9914766
TS
11841 macro_build ((char *) NULL, &icnt, &ex, HAVE_32BIT_ADDRESSES ? "sw" : "sd",
11842 "t,o(b)", mips_gp_register, (int) BFD_RELOC_LO16, SP);
252b5132
RH
11843
11844 demand_empty_rest_of_line ();
11845}
11846
6478892d
TS
11847/* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
11848 was given in the preceeding .gpsetup, it results in:
11849 ld $gp, offset($sp)
76b3015f 11850
6478892d
TS
11851 If a register $reg2 was given there, it results in:
11852 daddiu $gp, $gp, $reg2
11853 */
11854static void
11855s_cpreturn (ignore)
11856 int ignore ATTRIBUTE_UNUSED;
11857{
11858 expressionS ex;
11859 int icnt = 0;
11860
11861 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
11862 We also need NewABI support. */
11863 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11864 {
11865 s_ignore (0);
11866 return;
11867 }
11868
11869 if (mips_cpreturn_register == -1)
11870 {
11871 ex.X_op = O_constant;
11872 ex.X_add_symbol = NULL;
11873 ex.X_op_symbol = NULL;
11874 ex.X_add_number = mips_cpreturn_offset;
11875
11876 macro_build ((char *) NULL, &icnt, &ex, "ld", "t,o(b)",
11877 mips_gp_register, (int) BFD_RELOC_LO16, SP);
11878 }
11879 else
11880 macro_build ((char *) NULL, &icnt, (expressionS *) NULL, "daddu",
11881 "d,v,t", mips_gp_register, mips_cpreturn_register, 0);
11882
11883 demand_empty_rest_of_line ();
11884}
11885
11886/* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
11887 code. It sets the offset to use in gp_rel relocations. */
11888
11889static void
11890s_gpvalue (ignore)
11891 int ignore ATTRIBUTE_UNUSED;
11892{
11893 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
11894 We also need NewABI support. */
11895 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
11896 {
11897 s_ignore (0);
11898 return;
11899 }
11900
def2e0dd 11901 mips_gprel_offset = get_absolute_expression ();
6478892d
TS
11902
11903 demand_empty_rest_of_line ();
11904}
11905
252b5132
RH
11906/* Handle the .gpword pseudo-op. This is used when generating PIC
11907 code. It generates a 32 bit GP relative reloc. */
11908
11909static void
11910s_gpword (ignore)
43841e91 11911 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
11912{
11913 symbolS *label;
11914 expressionS ex;
11915 char *p;
11916
11917 /* When not generating PIC code, this is treated as .word. */
11918 if (mips_pic != SVR4_PIC)
11919 {
11920 s_cons (2);
11921 return;
11922 }
11923
11924 label = insn_labels != NULL ? insn_labels->label : NULL;
11925 mips_emit_delays (true);
11926 if (auto_align)
11927 mips_align (2, 0, label);
11928 mips_clear_insn_labels ();
11929
11930 expression (&ex);
11931
11932 if (ex.X_op != O_symbol || ex.X_add_number != 0)
11933 {
11934 as_bad (_("Unsupported use of .gpword"));
11935 ignore_rest_of_line ();
11936 }
11937
11938 p = frag_more (4);
11939 md_number_to_chars (p, (valueT) 0, 4);
c9914766 11940 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, false,
cdf6fd85 11941 BFD_RELOC_GPREL32);
252b5132
RH
11942
11943 demand_empty_rest_of_line ();
11944}
11945
11946/* Handle the .cpadd pseudo-op. This is used when dealing with switch
11947 tables in SVR4 PIC code. */
11948
11949static void
11950s_cpadd (ignore)
43841e91 11951 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
11952{
11953 int icnt = 0;
11954 int reg;
11955
6478892d
TS
11956 /* This is ignored when not generating SVR4 PIC code or if this is NewABI
11957 code. */
11958 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
11959 {
11960 s_ignore (0);
11961 return;
11962 }
11963
11964 /* Add $gp to the register named as an argument. */
11965 reg = tc_get_register (0);
11966 macro_build ((char *) NULL, &icnt, (expressionS *) NULL,
ca4e0257 11967 HAVE_32BIT_ADDRESSES ? "addu" : "daddu",
c9914766 11968 "d,v,t", reg, reg, mips_gp_register);
252b5132 11969
bdaaa2e1 11970 demand_empty_rest_of_line ();
252b5132
RH
11971}
11972
11973/* Handle the .insn pseudo-op. This marks instruction labels in
11974 mips16 mode. This permits the linker to handle them specially,
11975 such as generating jalx instructions when needed. We also make
11976 them odd for the duration of the assembly, in order to generate the
11977 right sort of code. We will make them even in the adjust_symtab
11978 routine, while leaving them marked. This is convenient for the
11979 debugger and the disassembler. The linker knows to make them odd
11980 again. */
11981
11982static void
11983s_insn (ignore)
43841e91 11984 int ignore ATTRIBUTE_UNUSED;
252b5132 11985{
f9419b05 11986 mips16_mark_labels ();
252b5132
RH
11987
11988 demand_empty_rest_of_line ();
11989}
11990
11991/* Handle a .stabn directive. We need these in order to mark a label
11992 as being a mips16 text label correctly. Sometimes the compiler
11993 will emit a label, followed by a .stabn, and then switch sections.
11994 If the label and .stabn are in mips16 mode, then the label is
11995 really a mips16 text label. */
11996
11997static void
11998s_mips_stab (type)
11999 int type;
12000{
f9419b05 12001 if (type == 'n')
252b5132
RH
12002 mips16_mark_labels ();
12003
12004 s_stab (type);
12005}
12006
12007/* Handle the .weakext pseudo-op as defined in Kane and Heinrich.
12008 */
12009
12010static void
12011s_mips_weakext (ignore)
43841e91 12012 int ignore ATTRIBUTE_UNUSED;
252b5132
RH
12013{
12014 char *name;
12015 int c;
12016 symbolS *symbolP;
12017 expressionS exp;
12018
12019 name = input_line_pointer;
12020 c = get_symbol_end ();
12021 symbolP = symbol_find_or_make (name);
12022 S_SET_WEAK (symbolP);
12023 *input_line_pointer = c;
12024
12025 SKIP_WHITESPACE ();
12026
12027 if (! is_end_of_line[(unsigned char) *input_line_pointer])
12028 {
12029 if (S_IS_DEFINED (symbolP))
12030 {
956cd1d6 12031 as_bad ("ignoring attempt to redefine symbol %s",
252b5132
RH
12032 S_GET_NAME (symbolP));
12033 ignore_rest_of_line ();
12034 return;
12035 }
bdaaa2e1 12036
252b5132
RH
12037 if (*input_line_pointer == ',')
12038 {
12039 ++input_line_pointer;
12040 SKIP_WHITESPACE ();
12041 }
bdaaa2e1 12042
252b5132
RH
12043 expression (&exp);
12044 if (exp.X_op != O_symbol)
12045 {
12046 as_bad ("bad .weakext directive");
98d3f06f 12047 ignore_rest_of_line ();
252b5132
RH
12048 return;
12049 }
49309057 12050 symbol_set_value_expression (symbolP, &exp);
252b5132
RH
12051 }
12052
12053 demand_empty_rest_of_line ();
12054}
12055
12056/* Parse a register string into a number. Called from the ECOFF code
12057 to parse .frame. The argument is non-zero if this is the frame
12058 register, so that we can record it in mips_frame_reg. */
12059
12060int
12061tc_get_register (frame)
12062 int frame;
12063{
12064 int reg;
12065
12066 SKIP_WHITESPACE ();
12067 if (*input_line_pointer++ != '$')
12068 {
12069 as_warn (_("expected `$'"));
85b51719 12070 reg = ZERO;
252b5132 12071 }
3882b010 12072 else if (ISDIGIT (*input_line_pointer))
252b5132
RH
12073 {
12074 reg = get_absolute_expression ();
12075 if (reg < 0 || reg >= 32)
12076 {
12077 as_warn (_("Bad register number"));
85b51719 12078 reg = ZERO;
252b5132
RH
12079 }
12080 }
12081 else
12082 {
76db943d 12083 if (strncmp (input_line_pointer, "ra", 2) == 0)
85b51719
TS
12084 {
12085 reg = RA;
12086 input_line_pointer += 2;
12087 }
76db943d 12088 else if (strncmp (input_line_pointer, "fp", 2) == 0)
85b51719
TS
12089 {
12090 reg = FP;
12091 input_line_pointer += 2;
12092 }
252b5132 12093 else if (strncmp (input_line_pointer, "sp", 2) == 0)
85b51719
TS
12094 {
12095 reg = SP;
12096 input_line_pointer += 2;
12097 }
252b5132 12098 else if (strncmp (input_line_pointer, "gp", 2) == 0)
85b51719
TS
12099 {
12100 reg = GP;
12101 input_line_pointer += 2;
12102 }
252b5132 12103 else if (strncmp (input_line_pointer, "at", 2) == 0)
85b51719
TS
12104 {
12105 reg = AT;
12106 input_line_pointer += 2;
12107 }
12108 else if (strncmp (input_line_pointer, "kt0", 3) == 0)
12109 {
12110 reg = KT0;
12111 input_line_pointer += 3;
12112 }
12113 else if (strncmp (input_line_pointer, "kt1", 3) == 0)
12114 {
12115 reg = KT1;
12116 input_line_pointer += 3;
12117 }
12118 else if (strncmp (input_line_pointer, "zero", 4) == 0)
12119 {
12120 reg = ZERO;
12121 input_line_pointer += 4;
12122 }
252b5132
RH
12123 else
12124 {
12125 as_warn (_("Unrecognized register name"));
85b51719
TS
12126 reg = ZERO;
12127 while (ISALNUM(*input_line_pointer))
12128 input_line_pointer++;
252b5132 12129 }
252b5132
RH
12130 }
12131 if (frame)
7a621144
DJ
12132 {
12133 mips_frame_reg = reg != 0 ? reg : SP;
12134 mips_frame_reg_valid = 1;
12135 mips_cprestore_valid = 0;
12136 }
252b5132
RH
12137 return reg;
12138}
12139
12140valueT
12141md_section_align (seg, addr)
12142 asection *seg;
12143 valueT addr;
12144{
12145 int align = bfd_get_section_alignment (stdoutput, seg);
12146
12147#ifdef OBJ_ELF
12148 /* We don't need to align ELF sections to the full alignment.
12149 However, Irix 5 may prefer that we align them at least to a 16
12150 byte boundary. We don't bother to align the sections if we are
12151 targeted for an embedded system. */
12152 if (strcmp (TARGET_OS, "elf") == 0)
12153 return addr;
12154 if (align > 4)
12155 align = 4;
12156#endif
12157
12158 return ((addr + (1 << align) - 1) & (-1 << align));
12159}
12160
12161/* Utility routine, called from above as well. If called while the
12162 input file is still being read, it's only an approximation. (For
12163 example, a symbol may later become defined which appeared to be
12164 undefined earlier.) */
12165
12166static int
12167nopic_need_relax (sym, before_relaxing)
12168 symbolS *sym;
12169 int before_relaxing;
12170{
12171 if (sym == 0)
12172 return 0;
12173
6478892d 12174 if (USE_GLOBAL_POINTER_OPT && g_switch_value > 0)
252b5132
RH
12175 {
12176 const char *symname;
12177 int change;
12178
c9914766 12179 /* Find out whether this symbol can be referenced off the $gp
252b5132
RH
12180 register. It can be if it is smaller than the -G size or if
12181 it is in the .sdata or .sbss section. Certain symbols can
c9914766 12182 not be referenced off the $gp, although it appears as though
252b5132
RH
12183 they can. */
12184 symname = S_GET_NAME (sym);
12185 if (symname != (const char *) NULL
12186 && (strcmp (symname, "eprol") == 0
12187 || strcmp (symname, "etext") == 0
12188 || strcmp (symname, "_gp") == 0
12189 || strcmp (symname, "edata") == 0
12190 || strcmp (symname, "_fbss") == 0
12191 || strcmp (symname, "_fdata") == 0
12192 || strcmp (symname, "_ftext") == 0
12193 || strcmp (symname, "end") == 0
12194 || strcmp (symname, "_gp_disp") == 0))
12195 change = 1;
12196 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
12197 && (0
12198#ifndef NO_ECOFF_DEBUGGING
49309057
ILT
12199 || (symbol_get_obj (sym)->ecoff_extern_size != 0
12200 && (symbol_get_obj (sym)->ecoff_extern_size
12201 <= g_switch_value))
252b5132
RH
12202#endif
12203 /* We must defer this decision until after the whole
12204 file has been read, since there might be a .extern
12205 after the first use of this symbol. */
12206 || (before_relaxing
12207#ifndef NO_ECOFF_DEBUGGING
49309057 12208 && symbol_get_obj (sym)->ecoff_extern_size == 0
252b5132
RH
12209#endif
12210 && S_GET_VALUE (sym) == 0)
12211 || (S_GET_VALUE (sym) != 0
12212 && S_GET_VALUE (sym) <= g_switch_value)))
12213 change = 0;
12214 else
12215 {
12216 const char *segname;
12217
12218 segname = segment_name (S_GET_SEGMENT (sym));
12219 assert (strcmp (segname, ".lit8") != 0
12220 && strcmp (segname, ".lit4") != 0);
12221 change = (strcmp (segname, ".sdata") != 0
fba2b7f9
GK
12222 && strcmp (segname, ".sbss") != 0
12223 && strncmp (segname, ".sdata.", 7) != 0
12224 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
252b5132
RH
12225 }
12226 return change;
12227 }
12228 else
c9914766 12229 /* We are not optimizing for the $gp register. */
252b5132
RH
12230 return 1;
12231}
12232
12233/* Given a mips16 variant frag FRAGP, return non-zero if it needs an
12234 extended opcode. SEC is the section the frag is in. */
12235
12236static int
12237mips16_extended_frag (fragp, sec, stretch)
12238 fragS *fragp;
12239 asection *sec;
12240 long stretch;
12241{
12242 int type;
12243 register const struct mips16_immed_operand *op;
12244 offsetT val;
12245 int mintiny, maxtiny;
12246 segT symsec;
98aa84af 12247 fragS *sym_frag;
252b5132
RH
12248
12249 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
12250 return 0;
12251 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
12252 return 1;
12253
12254 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12255 op = mips16_immed_operands;
12256 while (op->type != type)
12257 {
12258 ++op;
12259 assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
12260 }
12261
12262 if (op->unsp)
12263 {
12264 if (type == '<' || type == '>' || type == '[' || type == ']')
12265 {
12266 mintiny = 1;
12267 maxtiny = 1 << op->nbits;
12268 }
12269 else
12270 {
12271 mintiny = 0;
12272 maxtiny = (1 << op->nbits) - 1;
12273 }
12274 }
12275 else
12276 {
12277 mintiny = - (1 << (op->nbits - 1));
12278 maxtiny = (1 << (op->nbits - 1)) - 1;
12279 }
12280
98aa84af 12281 sym_frag = symbol_get_frag (fragp->fr_symbol);
ac62c346 12282 val = S_GET_VALUE (fragp->fr_symbol);
98aa84af 12283 symsec = S_GET_SEGMENT (fragp->fr_symbol);
252b5132
RH
12284
12285 if (op->pcrel)
12286 {
12287 addressT addr;
12288
12289 /* We won't have the section when we are called from
12290 mips_relax_frag. However, we will always have been called
12291 from md_estimate_size_before_relax first. If this is a
12292 branch to a different section, we mark it as such. If SEC is
12293 NULL, and the frag is not marked, then it must be a branch to
12294 the same section. */
12295 if (sec == NULL)
12296 {
12297 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
12298 return 1;
12299 }
12300 else
12301 {
98aa84af 12302 /* Must have been called from md_estimate_size_before_relax. */
252b5132
RH
12303 if (symsec != sec)
12304 {
12305 fragp->fr_subtype =
12306 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12307
12308 /* FIXME: We should support this, and let the linker
12309 catch branches and loads that are out of range. */
12310 as_bad_where (fragp->fr_file, fragp->fr_line,
12311 _("unsupported PC relative reference to different section"));
12312
12313 return 1;
12314 }
98aa84af
AM
12315 if (fragp != sym_frag && sym_frag->fr_address == 0)
12316 /* Assume non-extended on the first relaxation pass.
12317 The address we have calculated will be bogus if this is
12318 a forward branch to another frag, as the forward frag
12319 will have fr_address == 0. */
12320 return 0;
252b5132
RH
12321 }
12322
12323 /* In this case, we know for sure that the symbol fragment is in
98aa84af
AM
12324 the same section. If the relax_marker of the symbol fragment
12325 differs from the relax_marker of this fragment, we have not
12326 yet adjusted the symbol fragment fr_address. We want to add
252b5132
RH
12327 in STRETCH in order to get a better estimate of the address.
12328 This particularly matters because of the shift bits. */
12329 if (stretch != 0
98aa84af 12330 && sym_frag->relax_marker != fragp->relax_marker)
252b5132
RH
12331 {
12332 fragS *f;
12333
12334 /* Adjust stretch for any alignment frag. Note that if have
12335 been expanding the earlier code, the symbol may be
12336 defined in what appears to be an earlier frag. FIXME:
12337 This doesn't handle the fr_subtype field, which specifies
12338 a maximum number of bytes to skip when doing an
12339 alignment. */
98aa84af 12340 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
252b5132
RH
12341 {
12342 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
12343 {
12344 if (stretch < 0)
12345 stretch = - ((- stretch)
12346 & ~ ((1 << (int) f->fr_offset) - 1));
12347 else
12348 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
12349 if (stretch == 0)
12350 break;
12351 }
12352 }
12353 if (f != NULL)
12354 val += stretch;
12355 }
12356
12357 addr = fragp->fr_address + fragp->fr_fix;
12358
12359 /* The base address rules are complicated. The base address of
12360 a branch is the following instruction. The base address of a
12361 PC relative load or add is the instruction itself, but if it
12362 is in a delay slot (in which case it can not be extended) use
12363 the address of the instruction whose delay slot it is in. */
12364 if (type == 'p' || type == 'q')
12365 {
12366 addr += 2;
12367
12368 /* If we are currently assuming that this frag should be
12369 extended, then, the current address is two bytes
bdaaa2e1 12370 higher. */
252b5132
RH
12371 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12372 addr += 2;
12373
12374 /* Ignore the low bit in the target, since it will be set
12375 for a text label. */
12376 if ((val & 1) != 0)
12377 --val;
12378 }
12379 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12380 addr -= 4;
12381 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12382 addr -= 2;
12383
12384 val -= addr & ~ ((1 << op->shift) - 1);
12385
12386 /* Branch offsets have an implicit 0 in the lowest bit. */
12387 if (type == 'p' || type == 'q')
12388 val /= 2;
12389
12390 /* If any of the shifted bits are set, we must use an extended
12391 opcode. If the address depends on the size of this
12392 instruction, this can lead to a loop, so we arrange to always
12393 use an extended opcode. We only check this when we are in
12394 the main relaxation loop, when SEC is NULL. */
12395 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
12396 {
12397 fragp->fr_subtype =
12398 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12399 return 1;
12400 }
12401
12402 /* If we are about to mark a frag as extended because the value
12403 is precisely maxtiny + 1, then there is a chance of an
12404 infinite loop as in the following code:
12405 la $4,foo
12406 .skip 1020
12407 .align 2
12408 foo:
12409 In this case when the la is extended, foo is 0x3fc bytes
12410 away, so the la can be shrunk, but then foo is 0x400 away, so
12411 the la must be extended. To avoid this loop, we mark the
12412 frag as extended if it was small, and is about to become
12413 extended with a value of maxtiny + 1. */
12414 if (val == ((maxtiny + 1) << op->shift)
12415 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
12416 && sec == NULL)
12417 {
12418 fragp->fr_subtype =
12419 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
12420 return 1;
12421 }
12422 }
12423 else if (symsec != absolute_section && sec != NULL)
12424 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
12425
12426 if ((val & ((1 << op->shift) - 1)) != 0
12427 || val < (mintiny << op->shift)
12428 || val > (maxtiny << op->shift))
12429 return 1;
12430 else
12431 return 0;
12432}
12433
12434/* Estimate the size of a frag before relaxing. Unless this is the
12435 mips16, we are not really relaxing here, and the final size is
12436 encoded in the subtype information. For the mips16, we have to
12437 decide whether we are using an extended opcode or not. */
12438
252b5132
RH
12439int
12440md_estimate_size_before_relax (fragp, segtype)
12441 fragS *fragp;
12442 asection *segtype;
12443{
43841e91 12444 int change = 0;
8614eeee 12445 boolean linkonce = false;
252b5132
RH
12446
12447 if (RELAX_MIPS16_P (fragp->fr_subtype))
177b4a6a
AO
12448 /* We don't want to modify the EXTENDED bit here; it might get us
12449 into infinite loops. We change it only in mips_relax_frag(). */
12450 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
252b5132
RH
12451
12452 if (mips_pic == NO_PIC)
12453 {
12454 change = nopic_need_relax (fragp->fr_symbol, 0);
12455 }
12456 else if (mips_pic == SVR4_PIC)
12457 {
12458 symbolS *sym;
12459 asection *symsec;
12460
12461 sym = fragp->fr_symbol;
12462
12463 /* Handle the case of a symbol equated to another symbol. */
e0890092 12464 while (symbol_equated_reloc_p (sym))
252b5132
RH
12465 {
12466 symbolS *n;
12467
12468 /* It's possible to get a loop here in a badly written
12469 program. */
49309057 12470 n = symbol_get_value_expression (sym)->X_add_symbol;
252b5132
RH
12471 if (n == sym)
12472 break;
12473 sym = n;
12474 }
12475
12476 symsec = S_GET_SEGMENT (sym);
12477
8614eeee
UC
12478 /* duplicate the test for LINK_ONCE sections as in adjust_reloc_syms */
12479 if (symsec != segtype && ! S_IS_LOCAL (sym))
beae10d5
KH
12480 {
12481 if ((bfd_get_section_flags (stdoutput, symsec) & SEC_LINK_ONCE)
12482 != 0)
12483 linkonce = true;
12484
12485 /* The GNU toolchain uses an extension for ELF: a section
12486 beginning with the magic string .gnu.linkonce is a linkonce
12487 section. */
12488 if (strncmp (segment_name (symsec), ".gnu.linkonce",
12489 sizeof ".gnu.linkonce" - 1) == 0)
12490 linkonce = true;
12491 }
8614eeee 12492
252b5132
RH
12493 /* This must duplicate the test in adjust_reloc_syms. */
12494 change = (symsec != &bfd_und_section
12495 && symsec != &bfd_abs_section
426b0403 12496 && ! bfd_is_com_section (symsec)
8614eeee 12497 && !linkonce
426b0403 12498#ifdef OBJ_ELF
ea4ff978 12499 /* A global or weak symbol is treated as external. */
9151e8bf 12500 && (OUTPUT_FLAVOR != bfd_target_elf_flavour
b25a253c 12501 || (! S_IS_WEAK (sym)
bad9ca53
TS
12502 && (! S_IS_EXTERNAL (sym)
12503 || mips_pic == EMBEDDED_PIC)))
426b0403
AM
12504#endif
12505 );
252b5132
RH
12506 }
12507 else
12508 abort ();
12509
12510 if (change)
12511 {
12512 /* Record the offset to the first reloc in the fr_opcode field.
12513 This lets md_convert_frag and tc_gen_reloc know that the code
12514 must be expanded. */
12515 fragp->fr_opcode = (fragp->fr_literal
12516 + fragp->fr_fix
12517 - RELAX_OLD (fragp->fr_subtype)
12518 + RELAX_RELOC1 (fragp->fr_subtype));
12519 /* FIXME: This really needs as_warn_where. */
12520 if (RELAX_WARN (fragp->fr_subtype))
9a41af64
TS
12521 as_warn (_("AT used after \".set noat\" or macro used after "
12522 "\".set nomacro\""));
12523
12524 return RELAX_NEW (fragp->fr_subtype) - RELAX_OLD (fragp->fr_subtype);
252b5132
RH
12525 }
12526
9a41af64 12527 return 0;
252b5132
RH
12528}
12529
12530/* This is called to see whether a reloc against a defined symbol
12531 should be converted into a reloc against a section. Don't adjust
12532 MIPS16 jump relocations, so we don't have to worry about the format
12533 of the offset in the .o file. Don't adjust relocations against
12534 mips16 symbols, so that the linker can find them if it needs to set
12535 up a stub. */
12536
12537int
12538mips_fix_adjustable (fixp)
12539 fixS *fixp;
12540{
12541 if (fixp->fx_r_type == BFD_RELOC_MIPS16_JMP)
12542 return 0;
a161fe53 12543
252b5132
RH
12544 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
12545 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
12546 return 0;
a161fe53 12547
252b5132
RH
12548 if (fixp->fx_addsy == NULL)
12549 return 1;
a161fe53 12550
252b5132
RH
12551#ifdef OBJ_ELF
12552 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
12553 && S_GET_OTHER (fixp->fx_addsy) == STO_MIPS16
12554 && fixp->fx_subsy == NULL)
12555 return 0;
12556#endif
a161fe53 12557
252b5132
RH
12558 return 1;
12559}
12560
12561/* Translate internal representation of relocation info to BFD target
12562 format. */
12563
12564arelent **
12565tc_gen_reloc (section, fixp)
43841e91 12566 asection *section ATTRIBUTE_UNUSED;
252b5132
RH
12567 fixS *fixp;
12568{
12569 static arelent *retval[4];
12570 arelent *reloc;
12571 bfd_reloc_code_real_type code;
12572
12573 reloc = retval[0] = (arelent *) xmalloc (sizeof (arelent));
12574 retval[1] = NULL;
12575
49309057
ILT
12576 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
12577 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
12578 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
12579
12580 if (mips_pic == EMBEDDED_PIC
12581 && SWITCH_TABLE (fixp))
12582 {
12583 /* For a switch table entry we use a special reloc. The addend
12584 is actually the difference between the reloc address and the
12585 subtrahend. */
12586 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
12587 if (OUTPUT_FLAVOR != bfd_target_ecoff_flavour)
12588 as_fatal (_("Double check fx_r_type in tc-mips.c:tc_gen_reloc"));
12589 fixp->fx_r_type = BFD_RELOC_GPREL32;
12590 }
12591 else if (fixp->fx_r_type == BFD_RELOC_PCREL_LO16)
12592 {
4514d474
CD
12593 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
12594 reloc->addend = fixp->fx_addnumber;
252b5132 12595 else
4514d474
CD
12596 {
12597 /* We use a special addend for an internal RELLO reloc. */
12598 if (symbol_section_p (fixp->fx_addsy))
12599 reloc->addend = reloc->address - S_GET_VALUE (fixp->fx_subsy);
12600 else
12601 reloc->addend = fixp->fx_addnumber + reloc->address;
12602 }
252b5132
RH
12603 }
12604 else if (fixp->fx_r_type == BFD_RELOC_PCREL_HI16_S)
12605 {
12606 assert (fixp->fx_next != NULL
12607 && fixp->fx_next->fx_r_type == BFD_RELOC_PCREL_LO16);
4514d474
CD
12608
12609 /* The reloc is relative to the RELLO; adjust the addend
252b5132 12610 accordingly. */
4514d474
CD
12611 if (OUTPUT_FLAVOR == bfd_target_elf_flavour)
12612 reloc->addend = fixp->fx_next->fx_addnumber;
252b5132 12613 else
4514d474
CD
12614 {
12615 /* We use a special addend for an internal RELHI reloc. */
12616 if (symbol_section_p (fixp->fx_addsy))
12617 reloc->addend = (fixp->fx_next->fx_frag->fr_address
12618 + fixp->fx_next->fx_where
12619 - S_GET_VALUE (fixp->fx_subsy));
12620 else
12621 reloc->addend = (fixp->fx_addnumber
12622 + fixp->fx_next->fx_frag->fr_address
12623 + fixp->fx_next->fx_where);
12624 }
252b5132 12625 }
4514d474
CD
12626 else if (fixp->fx_pcrel == 0 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
12627 reloc->addend = fixp->fx_addnumber;
252b5132
RH
12628 else
12629 {
12630 if (OUTPUT_FLAVOR != bfd_target_aout_flavour)
12631 /* A gruesome hack which is a result of the gruesome gas reloc
12632 handling. */
12633 reloc->addend = reloc->address;
12634 else
12635 reloc->addend = -reloc->address;
12636 }
12637
12638 /* If this is a variant frag, we may need to adjust the existing
12639 reloc and generate a new one. */
12640 if (fixp->fx_frag->fr_opcode != NULL
cdf6fd85 12641 && (fixp->fx_r_type == BFD_RELOC_GPREL16
252b5132
RH
12642 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT16
12643 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL16
12644 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
12645 || fixp->fx_r_type == BFD_RELOC_MIPS_GOT_LO16
12646 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
6478892d
TS
12647 || fixp->fx_r_type == BFD_RELOC_MIPS_CALL_LO16)
12648 && ! HAVE_NEWABI)
252b5132
RH
12649 {
12650 arelent *reloc2;
12651
12652 assert (! RELAX_MIPS16_P (fixp->fx_frag->fr_subtype));
12653
12654 /* If this is not the last reloc in this frag, then we have two
12655 GPREL relocs, or a GOT_HI16/GOT_LO16 pair, or a
12656 CALL_HI16/CALL_LO16, both of which are being replaced. Let
12657 the second one handle all of them. */
12658 if (fixp->fx_next != NULL
12659 && fixp->fx_frag == fixp->fx_next->fx_frag)
12660 {
cdf6fd85
TS
12661 assert ((fixp->fx_r_type == BFD_RELOC_GPREL16
12662 && fixp->fx_next->fx_r_type == BFD_RELOC_GPREL16)
252b5132
RH
12663 || (fixp->fx_r_type == BFD_RELOC_MIPS_GOT_HI16
12664 && (fixp->fx_next->fx_r_type
12665 == BFD_RELOC_MIPS_GOT_LO16))
12666 || (fixp->fx_r_type == BFD_RELOC_MIPS_CALL_HI16
12667 && (fixp->fx_next->fx_r_type
12668 == BFD_RELOC_MIPS_CALL_LO16)));
12669 retval[0] = NULL;
12670 return retval;
12671 }
12672
12673 fixp->fx_where = fixp->fx_frag->fr_opcode - fixp->fx_frag->fr_literal;
12674 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
12675 reloc2 = retval[1] = (arelent *) xmalloc (sizeof (arelent));
12676 retval[2] = NULL;
49309057
ILT
12677 reloc2->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
12678 *reloc2->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
12679 reloc2->address = (reloc->address
12680 + (RELAX_RELOC2 (fixp->fx_frag->fr_subtype)
12681 - RELAX_RELOC1 (fixp->fx_frag->fr_subtype)));
12682 reloc2->addend = fixp->fx_addnumber;
12683 reloc2->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_LO16);
12684 assert (reloc2->howto != NULL);
12685
12686 if (RELAX_RELOC3 (fixp->fx_frag->fr_subtype))
12687 {
12688 arelent *reloc3;
12689
12690 reloc3 = retval[2] = (arelent *) xmalloc (sizeof (arelent));
12691 retval[3] = NULL;
12692 *reloc3 = *reloc2;
12693 reloc3->address += 4;
12694 }
12695
12696 if (mips_pic == NO_PIC)
12697 {
cdf6fd85 12698 assert (fixp->fx_r_type == BFD_RELOC_GPREL16);
252b5132
RH
12699 fixp->fx_r_type = BFD_RELOC_HI16_S;
12700 }
12701 else if (mips_pic == SVR4_PIC)
12702 {
12703 switch (fixp->fx_r_type)
12704 {
12705 default:
12706 abort ();
12707 case BFD_RELOC_MIPS_GOT16:
12708 break;
12709 case BFD_RELOC_MIPS_CALL16:
12710 case BFD_RELOC_MIPS_GOT_LO16:
12711 case BFD_RELOC_MIPS_CALL_LO16:
12712 fixp->fx_r_type = BFD_RELOC_MIPS_GOT16;
12713 break;
12714 }
12715 }
12716 else
12717 abort ();
12718 }
12719
438c16b8
TS
12720 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
12721 entry to be used in the relocation's section offset. */
12722 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
252b5132
RH
12723 {
12724 reloc->address = reloc->addend;
12725 reloc->addend = 0;
12726 }
12727
12728 /* Since DIFF_EXPR_OK is defined in tc-mips.h, it is possible that
12729 fixup_segment converted a non-PC relative reloc into a PC
12730 relative reloc. In such a case, we need to convert the reloc
12731 code. */
12732 code = fixp->fx_r_type;
12733 if (fixp->fx_pcrel)
12734 {
12735 switch (code)
12736 {
12737 case BFD_RELOC_8:
12738 code = BFD_RELOC_8_PCREL;
12739 break;
12740 case BFD_RELOC_16:
12741 code = BFD_RELOC_16_PCREL;
12742 break;
12743 case BFD_RELOC_32:
12744 code = BFD_RELOC_32_PCREL;
12745 break;
12746 case BFD_RELOC_64:
12747 code = BFD_RELOC_64_PCREL;
12748 break;
12749 case BFD_RELOC_8_PCREL:
12750 case BFD_RELOC_16_PCREL:
12751 case BFD_RELOC_32_PCREL:
12752 case BFD_RELOC_64_PCREL:
12753 case BFD_RELOC_16_PCREL_S2:
12754 case BFD_RELOC_PCREL_HI16_S:
12755 case BFD_RELOC_PCREL_LO16:
12756 break;
12757 default:
12758 as_bad_where (fixp->fx_file, fixp->fx_line,
12759 _("Cannot make %s relocation PC relative"),
12760 bfd_get_reloc_code_name (code));
12761 }
12762 }
12763
add55e1f
RS
12764#ifdef OBJ_ELF
12765 /* md_apply_fix3 has a double-subtraction hack to get
12766 bfd_install_relocation to behave nicely. GPREL relocations are
12767 handled correctly without this hack, so undo it here. We can't
12768 stop md_apply_fix3 from subtracting twice in the first place since
12769 the fake addend is required for variant frags above. */
12770 if (fixp->fx_addsy != NULL && OUTPUT_FLAVOR == bfd_target_elf_flavour
98605598 12771 && (code == BFD_RELOC_GPREL16 || code == BFD_RELOC_MIPS16_GPREL)
add55e1f
RS
12772 && reloc->addend != 0
12773 && mips_need_elf_addend_fixup (fixp))
12774 reloc->addend += S_GET_VALUE (fixp->fx_addsy);
12775#endif
12776
252b5132
RH
12777 /* To support a PC relative reloc when generating embedded PIC code
12778 for ECOFF, we use a Cygnus extension. We check for that here to
12779 make sure that we don't let such a reloc escape normally. */
bb2d6cd7
GK
12780 if ((OUTPUT_FLAVOR == bfd_target_ecoff_flavour
12781 || OUTPUT_FLAVOR == bfd_target_elf_flavour)
252b5132
RH
12782 && code == BFD_RELOC_16_PCREL_S2
12783 && mips_pic != EMBEDDED_PIC)
12784 reloc->howto = NULL;
12785 else
12786 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
12787
12788 if (reloc->howto == NULL)
12789 {
12790 as_bad_where (fixp->fx_file, fixp->fx_line,
12791 _("Can not represent %s relocation in this object file format"),
12792 bfd_get_reloc_code_name (code));
12793 retval[0] = NULL;
12794 }
12795
12796 return retval;
12797}
12798
12799/* Relax a machine dependent frag. This returns the amount by which
12800 the current size of the frag should change. */
12801
12802int
12803mips_relax_frag (fragp, stretch)
12804 fragS *fragp;
12805 long stretch;
12806{
12807 if (! RELAX_MIPS16_P (fragp->fr_subtype))
12808 return 0;
12809
c4e7957c 12810 if (mips16_extended_frag (fragp, NULL, stretch))
252b5132
RH
12811 {
12812 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12813 return 0;
12814 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
12815 return 2;
12816 }
12817 else
12818 {
12819 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12820 return 0;
12821 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
12822 return -2;
12823 }
12824
12825 return 0;
12826}
12827
12828/* Convert a machine dependent frag. */
12829
12830void
12831md_convert_frag (abfd, asec, fragp)
43841e91 12832 bfd *abfd ATTRIBUTE_UNUSED;
252b5132
RH
12833 segT asec;
12834 fragS *fragp;
12835{
12836 int old, new;
12837 char *fixptr;
12838
12839 if (RELAX_MIPS16_P (fragp->fr_subtype))
12840 {
12841 int type;
12842 register const struct mips16_immed_operand *op;
12843 boolean small, ext;
12844 offsetT val;
12845 bfd_byte *buf;
12846 unsigned long insn;
12847 boolean use_extend;
12848 unsigned short extend;
12849
12850 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
12851 op = mips16_immed_operands;
12852 while (op->type != type)
12853 ++op;
12854
12855 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
12856 {
12857 small = false;
12858 ext = true;
12859 }
12860 else
12861 {
12862 small = true;
12863 ext = false;
12864 }
12865
6386f3a7 12866 resolve_symbol_value (fragp->fr_symbol);
252b5132
RH
12867 val = S_GET_VALUE (fragp->fr_symbol);
12868 if (op->pcrel)
12869 {
12870 addressT addr;
12871
12872 addr = fragp->fr_address + fragp->fr_fix;
12873
12874 /* The rules for the base address of a PC relative reloc are
12875 complicated; see mips16_extended_frag. */
12876 if (type == 'p' || type == 'q')
12877 {
12878 addr += 2;
12879 if (ext)
12880 addr += 2;
12881 /* Ignore the low bit in the target, since it will be
12882 set for a text label. */
12883 if ((val & 1) != 0)
12884 --val;
12885 }
12886 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
12887 addr -= 4;
12888 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
12889 addr -= 2;
12890
12891 addr &= ~ (addressT) ((1 << op->shift) - 1);
12892 val -= addr;
12893
12894 /* Make sure the section winds up with the alignment we have
12895 assumed. */
12896 if (op->shift > 0)
12897 record_alignment (asec, op->shift);
12898 }
12899
12900 if (ext
12901 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
12902 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
12903 as_warn_where (fragp->fr_file, fragp->fr_line,
12904 _("extended instruction in delay slot"));
12905
12906 buf = (bfd_byte *) (fragp->fr_literal + fragp->fr_fix);
12907
12908 if (target_big_endian)
12909 insn = bfd_getb16 (buf);
12910 else
12911 insn = bfd_getl16 (buf);
12912
12913 mips16_immed (fragp->fr_file, fragp->fr_line, type, val,
12914 RELAX_MIPS16_USER_EXT (fragp->fr_subtype),
12915 small, ext, &insn, &use_extend, &extend);
12916
12917 if (use_extend)
12918 {
874e8986 12919 md_number_to_chars ((char *) buf, 0xf000 | extend, 2);
252b5132
RH
12920 fragp->fr_fix += 2;
12921 buf += 2;
12922 }
12923
874e8986 12924 md_number_to_chars ((char *) buf, insn, 2);
252b5132
RH
12925 fragp->fr_fix += 2;
12926 buf += 2;
12927 }
12928 else
12929 {
12930 if (fragp->fr_opcode == NULL)
12931 return;
12932
12933 old = RELAX_OLD (fragp->fr_subtype);
12934 new = RELAX_NEW (fragp->fr_subtype);
12935 fixptr = fragp->fr_literal + fragp->fr_fix;
12936
12937 if (new > 0)
12938 memcpy (fixptr - old, fixptr, new);
12939
12940 fragp->fr_fix += new - old;
12941 }
12942}
12943
12944#ifdef OBJ_ELF
12945
12946/* This function is called after the relocs have been generated.
12947 We've been storing mips16 text labels as odd. Here we convert them
12948 back to even for the convenience of the debugger. */
12949
12950void
12951mips_frob_file_after_relocs ()
12952{
12953 asymbol **syms;
12954 unsigned int count, i;
12955
12956 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
12957 return;
12958
12959 syms = bfd_get_outsymbols (stdoutput);
12960 count = bfd_get_symcount (stdoutput);
12961 for (i = 0; i < count; i++, syms++)
12962 {
12963 if (elf_symbol (*syms)->internal_elf_sym.st_other == STO_MIPS16
12964 && ((*syms)->value & 1) != 0)
12965 {
12966 (*syms)->value &= ~1;
12967 /* If the symbol has an odd size, it was probably computed
12968 incorrectly, so adjust that as well. */
12969 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
12970 ++elf_symbol (*syms)->internal_elf_sym.st_size;
12971 }
12972 }
12973}
12974
12975#endif
12976
12977/* This function is called whenever a label is defined. It is used
12978 when handling branch delays; if a branch has a label, we assume we
12979 can not move it. */
12980
12981void
12982mips_define_label (sym)
12983 symbolS *sym;
12984{
12985 struct insn_label_list *l;
12986
12987 if (free_insn_labels == NULL)
12988 l = (struct insn_label_list *) xmalloc (sizeof *l);
12989 else
12990 {
12991 l = free_insn_labels;
12992 free_insn_labels = l->next;
12993 }
12994
12995 l->label = sym;
12996 l->next = insn_labels;
12997 insn_labels = l;
12998}
12999\f
13000#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13001
13002/* Some special processing for a MIPS ELF file. */
13003
13004void
13005mips_elf_final_processing ()
13006{
13007 /* Write out the register information. */
316f5878 13008 if (mips_abi != N64_ABI)
252b5132
RH
13009 {
13010 Elf32_RegInfo s;
13011
13012 s.ri_gprmask = mips_gprmask;
13013 s.ri_cprmask[0] = mips_cprmask[0];
13014 s.ri_cprmask[1] = mips_cprmask[1];
13015 s.ri_cprmask[2] = mips_cprmask[2];
13016 s.ri_cprmask[3] = mips_cprmask[3];
13017 /* The gp_value field is set by the MIPS ELF backend. */
13018
13019 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
13020 ((Elf32_External_RegInfo *)
13021 mips_regmask_frag));
13022 }
13023 else
13024 {
13025 Elf64_Internal_RegInfo s;
13026
13027 s.ri_gprmask = mips_gprmask;
13028 s.ri_pad = 0;
13029 s.ri_cprmask[0] = mips_cprmask[0];
13030 s.ri_cprmask[1] = mips_cprmask[1];
13031 s.ri_cprmask[2] = mips_cprmask[2];
13032 s.ri_cprmask[3] = mips_cprmask[3];
13033 /* The gp_value field is set by the MIPS ELF backend. */
13034
13035 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
13036 ((Elf64_External_RegInfo *)
13037 mips_regmask_frag));
13038 }
13039
13040 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
13041 sort of BFD interface for this. */
13042 if (mips_any_noreorder)
13043 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
13044 if (mips_pic != NO_PIC)
13045 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
13046
98d3f06f 13047 /* Set MIPS ELF flags for ASEs. */
a4672219
TS
13048 if (file_ase_mips16)
13049 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
1f25f5d3
CD
13050#if 0 /* XXX FIXME */
13051 if (file_ase_mips3d)
13052 elf_elfheader (stdoutput)->e_flags |= ???;
13053#endif
deec1734
CD
13054 if (file_ase_mdmx)
13055 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
1f25f5d3 13056
bdaaa2e1 13057 /* Set the MIPS ELF ABI flags. */
316f5878 13058 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
252b5132 13059 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
316f5878 13060 else if (mips_abi == O64_ABI)
252b5132 13061 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
316f5878 13062 else if (mips_abi == EABI_ABI)
252b5132 13063 {
316f5878 13064 if (!file_mips_gp32)
252b5132
RH
13065 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
13066 else
13067 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
13068 }
316f5878 13069 else if (mips_abi == N32_ABI)
be00bddd
TS
13070 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
13071
c9914766 13072 /* Nothing to do for N64_ABI. */
252b5132
RH
13073
13074 if (mips_32bitmode)
13075 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
13076}
13077
13078#endif /* OBJ_ELF || OBJ_MAYBE_ELF */
13079\f
beae10d5
KH
13080typedef struct proc {
13081 symbolS *isym;
13082 unsigned long reg_mask;
13083 unsigned long reg_offset;
13084 unsigned long fpreg_mask;
13085 unsigned long fpreg_offset;
13086 unsigned long frame_offset;
13087 unsigned long frame_reg;
13088 unsigned long pc_reg;
13089} procS;
252b5132
RH
13090
13091static procS cur_proc;
13092static procS *cur_proc_ptr;
13093static int numprocs;
13094
0a9ef439 13095/* Fill in an rs_align_code fragment. */
a19d8eb0 13096
0a9ef439
RH
13097void
13098mips_handle_align (fragp)
13099 fragS *fragp;
a19d8eb0 13100{
0a9ef439
RH
13101 if (fragp->fr_type != rs_align_code)
13102 return;
13103
13104 if (mips_opts.mips16)
a19d8eb0
CP
13105 {
13106 static const unsigned char be_nop[] = { 0x65, 0x00 };
13107 static const unsigned char le_nop[] = { 0x00, 0x65 };
13108
0a9ef439
RH
13109 int bytes;
13110 char *p;
a19d8eb0 13111
0a9ef439
RH
13112 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
13113 p = fragp->fr_literal + fragp->fr_fix;
13114
13115 if (bytes & 1)
13116 {
13117 *p++ = 0;
f9419b05 13118 fragp->fr_fix++;
0a9ef439
RH
13119 }
13120
13121 memcpy (p, (target_big_endian ? be_nop : le_nop), 2);
13122 fragp->fr_var = 2;
a19d8eb0
CP
13123 }
13124
0a9ef439 13125 /* For mips32, a nop is a zero, which we trivially get by doing nothing. */
a19d8eb0
CP
13126}
13127
252b5132
RH
13128static void
13129md_obj_begin ()
13130{
13131}
13132
13133static void
13134md_obj_end ()
13135{
13136 /* check for premature end, nesting errors, etc */
13137 if (cur_proc_ptr)
9a41af64 13138 as_warn (_("missing .end at end of assembly"));
252b5132
RH
13139}
13140
13141static long
13142get_number ()
13143{
13144 int negative = 0;
13145 long val = 0;
13146
13147 if (*input_line_pointer == '-')
13148 {
13149 ++input_line_pointer;
13150 negative = 1;
13151 }
3882b010 13152 if (!ISDIGIT (*input_line_pointer))
956cd1d6 13153 as_bad (_("expected simple number"));
252b5132
RH
13154 if (input_line_pointer[0] == '0')
13155 {
13156 if (input_line_pointer[1] == 'x')
13157 {
13158 input_line_pointer += 2;
3882b010 13159 while (ISXDIGIT (*input_line_pointer))
252b5132
RH
13160 {
13161 val <<= 4;
13162 val |= hex_value (*input_line_pointer++);
13163 }
13164 return negative ? -val : val;
13165 }
13166 else
13167 {
13168 ++input_line_pointer;
3882b010 13169 while (ISDIGIT (*input_line_pointer))
252b5132
RH
13170 {
13171 val <<= 3;
13172 val |= *input_line_pointer++ - '0';
13173 }
13174 return negative ? -val : val;
13175 }
13176 }
3882b010 13177 if (!ISDIGIT (*input_line_pointer))
252b5132
RH
13178 {
13179 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
13180 *input_line_pointer, *input_line_pointer);
956cd1d6 13181 as_warn (_("invalid number"));
252b5132
RH
13182 return -1;
13183 }
3882b010 13184 while (ISDIGIT (*input_line_pointer))
252b5132
RH
13185 {
13186 val *= 10;
13187 val += *input_line_pointer++ - '0';
13188 }
13189 return negative ? -val : val;
13190}
13191
13192/* The .file directive; just like the usual .file directive, but there
c5dd6aab
DJ
13193 is an initial number which is the ECOFF file index. In the non-ECOFF
13194 case .file implies DWARF-2. */
13195
13196static void
13197s_mips_file (x)
13198 int x ATTRIBUTE_UNUSED;
13199{
ecb4347a
DJ
13200 static int first_file_directive = 0;
13201
c5dd6aab
DJ
13202 if (ECOFF_DEBUGGING)
13203 {
13204 get_number ();
13205 s_app_file (0);
13206 }
13207 else
ecb4347a
DJ
13208 {
13209 char *filename;
13210
13211 filename = dwarf2_directive_file (0);
13212
13213 /* Versions of GCC up to 3.1 start files with a ".file"
13214 directive even for stabs output. Make sure that this
13215 ".file" is handled. Note that you need a version of GCC
13216 after 3.1 in order to support DWARF-2 on MIPS. */
13217 if (filename != NULL && ! first_file_directive)
13218 {
13219 (void) new_logical_line (filename, -1);
13220 s_app_file_string (filename);
13221 }
13222 first_file_directive = 1;
13223 }
c5dd6aab
DJ
13224}
13225
13226/* The .loc directive, implying DWARF-2. */
252b5132
RH
13227
13228static void
c5dd6aab 13229s_mips_loc (x)
43841e91 13230 int x ATTRIBUTE_UNUSED;
252b5132 13231{
c5dd6aab
DJ
13232 if (!ECOFF_DEBUGGING)
13233 dwarf2_directive_loc (0);
252b5132
RH
13234}
13235
252b5132
RH
13236/* The .end directive. */
13237
13238static void
13239s_mips_end (x)
43841e91 13240 int x ATTRIBUTE_UNUSED;
252b5132
RH
13241{
13242 symbolS *p;
13243 int maybe_text;
13244
7a621144
DJ
13245 /* Following functions need their own .frame and .cprestore directives. */
13246 mips_frame_reg_valid = 0;
13247 mips_cprestore_valid = 0;
13248
252b5132
RH
13249 if (!is_end_of_line[(unsigned char) *input_line_pointer])
13250 {
13251 p = get_symbol ();
13252 demand_empty_rest_of_line ();
13253 }
13254 else
13255 p = NULL;
13256
13257#ifdef BFD_ASSEMBLER
13258 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
13259 maybe_text = 1;
13260 else
13261 maybe_text = 0;
13262#else
13263 if (now_seg != data_section && now_seg != bss_section)
13264 maybe_text = 1;
13265 else
13266 maybe_text = 0;
13267#endif
13268
13269 if (!maybe_text)
13270 as_warn (_(".end not in text section"));
13271
13272 if (!cur_proc_ptr)
13273 {
13274 as_warn (_(".end directive without a preceding .ent directive."));
13275 demand_empty_rest_of_line ();
13276 return;
13277 }
13278
13279 if (p != NULL)
13280 {
13281 assert (S_GET_NAME (p));
13282 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->isym)))
13283 as_warn (_(".end symbol does not match .ent symbol."));
ecb4347a
DJ
13284
13285 if (debug_type == DEBUG_STABS)
13286 stabs_generate_asm_endfunc (S_GET_NAME (p),
13287 S_GET_NAME (p));
252b5132
RH
13288 }
13289 else
13290 as_warn (_(".end directive missing or unknown symbol"));
13291
ecb4347a
DJ
13292#ifdef OBJ_ELF
13293 /* Generate a .pdr section. */
13294 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
13295 {
13296 segT saved_seg = now_seg;
13297 subsegT saved_subseg = now_subseg;
13298 valueT dot;
13299 expressionS exp;
13300 char *fragp;
252b5132 13301
ecb4347a 13302 dot = frag_now_fix ();
252b5132
RH
13303
13304#ifdef md_flush_pending_output
ecb4347a 13305 md_flush_pending_output ();
252b5132
RH
13306#endif
13307
ecb4347a
DJ
13308 assert (pdr_seg);
13309 subseg_set (pdr_seg, 0);
252b5132 13310
ecb4347a
DJ
13311 /* Write the symbol. */
13312 exp.X_op = O_symbol;
13313 exp.X_add_symbol = p;
13314 exp.X_add_number = 0;
13315 emit_expr (&exp, 4);
252b5132 13316
ecb4347a 13317 fragp = frag_more (7 * 4);
252b5132 13318
ecb4347a
DJ
13319 md_number_to_chars (fragp, (valueT) cur_proc_ptr->reg_mask, 4);
13320 md_number_to_chars (fragp + 4, (valueT) cur_proc_ptr->reg_offset, 4);
13321 md_number_to_chars (fragp + 8, (valueT) cur_proc_ptr->fpreg_mask, 4);
13322 md_number_to_chars (fragp + 12, (valueT) cur_proc_ptr->fpreg_offset, 4);
13323 md_number_to_chars (fragp + 16, (valueT) cur_proc_ptr->frame_offset, 4);
13324 md_number_to_chars (fragp + 20, (valueT) cur_proc_ptr->frame_reg, 4);
13325 md_number_to_chars (fragp + 24, (valueT) cur_proc_ptr->pc_reg, 4);
252b5132 13326
ecb4347a
DJ
13327 subseg_set (saved_seg, saved_subseg);
13328 }
13329#endif /* OBJ_ELF */
252b5132
RH
13330
13331 cur_proc_ptr = NULL;
13332}
13333
13334/* The .aent and .ent directives. */
13335
13336static void
13337s_mips_ent (aent)
13338 int aent;
13339{
252b5132
RH
13340 symbolS *symbolP;
13341 int maybe_text;
13342
13343 symbolP = get_symbol ();
13344 if (*input_line_pointer == ',')
f9419b05 13345 ++input_line_pointer;
252b5132 13346 SKIP_WHITESPACE ();
3882b010 13347 if (ISDIGIT (*input_line_pointer)
d9a62219 13348 || *input_line_pointer == '-')
874e8986 13349 get_number ();
252b5132
RH
13350
13351#ifdef BFD_ASSEMBLER
13352 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) != 0)
13353 maybe_text = 1;
13354 else
13355 maybe_text = 0;
13356#else
13357 if (now_seg != data_section && now_seg != bss_section)
13358 maybe_text = 1;
13359 else
13360 maybe_text = 0;
13361#endif
13362
13363 if (!maybe_text)
13364 as_warn (_(".ent or .aent not in text section."));
13365
13366 if (!aent && cur_proc_ptr)
9a41af64 13367 as_warn (_("missing .end"));
252b5132
RH
13368
13369 if (!aent)
13370 {
7a621144
DJ
13371 /* This function needs its own .frame and .cprestore directives. */
13372 mips_frame_reg_valid = 0;
13373 mips_cprestore_valid = 0;
13374
252b5132
RH
13375 cur_proc_ptr = &cur_proc;
13376 memset (cur_proc_ptr, '\0', sizeof (procS));
13377
13378 cur_proc_ptr->isym = symbolP;
13379
49309057 13380 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
252b5132 13381
f9419b05 13382 ++numprocs;
ecb4347a
DJ
13383
13384 if (debug_type == DEBUG_STABS)
13385 stabs_generate_asm_func (S_GET_NAME (symbolP),
13386 S_GET_NAME (symbolP));
252b5132
RH
13387 }
13388
13389 demand_empty_rest_of_line ();
13390}
13391
13392/* The .frame directive. If the mdebug section is present (IRIX 5 native)
bdaaa2e1 13393 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
252b5132 13394 s_mips_frame is used so that we can set the PDR information correctly.
bdaaa2e1 13395 We can't use the ecoff routines because they make reference to the ecoff
252b5132
RH
13396 symbol table (in the mdebug section). */
13397
13398static void
13399s_mips_frame (ignore)
2b3c5a5d 13400 int ignore ATTRIBUTE_UNUSED;
252b5132 13401{
ecb4347a
DJ
13402#ifdef OBJ_ELF
13403 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
13404 {
13405 long val;
252b5132 13406
ecb4347a
DJ
13407 if (cur_proc_ptr == (procS *) NULL)
13408 {
13409 as_warn (_(".frame outside of .ent"));
13410 demand_empty_rest_of_line ();
13411 return;
13412 }
252b5132 13413
ecb4347a
DJ
13414 cur_proc_ptr->frame_reg = tc_get_register (1);
13415
13416 SKIP_WHITESPACE ();
13417 if (*input_line_pointer++ != ','
13418 || get_absolute_expression_and_terminator (&val) != ',')
13419 {
13420 as_warn (_("Bad .frame directive"));
13421 --input_line_pointer;
13422 demand_empty_rest_of_line ();
13423 return;
13424 }
252b5132 13425
ecb4347a
DJ
13426 cur_proc_ptr->frame_offset = val;
13427 cur_proc_ptr->pc_reg = tc_get_register (0);
252b5132 13428
252b5132 13429 demand_empty_rest_of_line ();
252b5132 13430 }
ecb4347a
DJ
13431 else
13432#endif /* OBJ_ELF */
13433 s_ignore (ignore);
252b5132
RH
13434}
13435
bdaaa2e1
KH
13436/* The .fmask and .mask directives. If the mdebug section is present
13437 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
252b5132 13438 embedded targets, s_mips_mask is used so that we can set the PDR
bdaaa2e1 13439 information correctly. We can't use the ecoff routines because they
252b5132
RH
13440 make reference to the ecoff symbol table (in the mdebug section). */
13441
13442static void
13443s_mips_mask (reg_type)
13444 char reg_type;
13445{
ecb4347a
DJ
13446#ifdef OBJ_ELF
13447 if (OUTPUT_FLAVOR == bfd_target_elf_flavour && ! ECOFF_DEBUGGING)
252b5132 13448 {
ecb4347a 13449 long mask, off;
252b5132 13450
ecb4347a
DJ
13451 if (cur_proc_ptr == (procS *) NULL)
13452 {
13453 as_warn (_(".mask/.fmask outside of .ent"));
13454 demand_empty_rest_of_line ();
13455 return;
13456 }
252b5132 13457
ecb4347a
DJ
13458 if (get_absolute_expression_and_terminator (&mask) != ',')
13459 {
13460 as_warn (_("Bad .mask/.fmask directive"));
13461 --input_line_pointer;
13462 demand_empty_rest_of_line ();
13463 return;
13464 }
252b5132 13465
ecb4347a
DJ
13466 off = get_absolute_expression ();
13467
13468 if (reg_type == 'F')
13469 {
13470 cur_proc_ptr->fpreg_mask = mask;
13471 cur_proc_ptr->fpreg_offset = off;
13472 }
13473 else
13474 {
13475 cur_proc_ptr->reg_mask = mask;
13476 cur_proc_ptr->reg_offset = off;
13477 }
13478
13479 demand_empty_rest_of_line ();
252b5132
RH
13480 }
13481 else
ecb4347a
DJ
13482#endif /* OBJ_ELF */
13483 s_ignore (reg_type);
252b5132
RH
13484}
13485
13486/* The .loc directive. */
13487
13488#if 0
13489static void
13490s_loc (x)
13491 int x;
13492{
13493 symbolS *symbolP;
13494 int lineno;
13495 int addroff;
13496
13497 assert (now_seg == text_section);
13498
13499 lineno = get_number ();
13500 addroff = frag_now_fix ();
13501
13502 symbolP = symbol_new ("", N_SLINE, addroff, frag_now);
13503 S_SET_TYPE (symbolP, N_SLINE);
13504 S_SET_OTHER (symbolP, 0);
13505 S_SET_DESC (symbolP, lineno);
13506 symbolP->sy_segment = now_seg;
13507}
13508#endif
e7af610e 13509
316f5878
RS
13510/* A table describing all the processors gas knows about. Names are
13511 matched in the order listed.
e7af610e 13512
316f5878
RS
13513 To ease comparison, please keep this table in the same order as
13514 gcc's mips_cpu_info_table[]. */
e972090a
NC
13515static const struct mips_cpu_info mips_cpu_info_table[] =
13516{
316f5878
RS
13517 /* Entries for generic ISAs */
13518 { "mips1", 1, ISA_MIPS1, CPU_R3000 },
13519 { "mips2", 1, ISA_MIPS2, CPU_R6000 },
13520 { "mips3", 1, ISA_MIPS3, CPU_R4000 },
13521 { "mips4", 1, ISA_MIPS4, CPU_R8000 },
13522 { "mips5", 1, ISA_MIPS5, CPU_MIPS5 },
13523 { "mips32", 1, ISA_MIPS32, CPU_MIPS32 },
13524 { "mips64", 1, ISA_MIPS64, CPU_MIPS64 },
13525
13526 /* MIPS I */
13527 { "r3000", 0, ISA_MIPS1, CPU_R3000 },
13528 { "r2000", 0, ISA_MIPS1, CPU_R3000 },
13529 { "r3900", 0, ISA_MIPS1, CPU_R3900 },
13530
13531 /* MIPS II */
13532 { "r6000", 0, ISA_MIPS2, CPU_R6000 },
13533
13534 /* MIPS III */
13535 { "r4000", 0, ISA_MIPS3, CPU_R4000 },
13536 { "r4010", 0, ISA_MIPS2, CPU_R4010 },
13537 { "vr4100", 0, ISA_MIPS3, CPU_VR4100 },
13538 { "vr4111", 0, ISA_MIPS3, CPU_R4111 },
13539 { "vr4300", 0, ISA_MIPS3, CPU_R4300 },
13540 { "r4400", 0, ISA_MIPS3, CPU_R4400 },
13541 { "r4600", 0, ISA_MIPS3, CPU_R4600 },
13542 { "orion", 0, ISA_MIPS3, CPU_R4600 },
13543 { "r4650", 0, ISA_MIPS3, CPU_R4650 },
13544
13545 /* MIPS IV */
13546 { "r8000", 0, ISA_MIPS4, CPU_R8000 },
13547 { "r10000", 0, ISA_MIPS4, CPU_R10000 },
13548 { "r12000", 0, ISA_MIPS4, CPU_R12000 },
13549 { "vr5000", 0, ISA_MIPS4, CPU_R5000 },
13550 { "rm5200", 0, ISA_MIPS4, CPU_R5000 },
13551 { "rm5230", 0, ISA_MIPS4, CPU_R5000 },
13552 { "rm5231", 0, ISA_MIPS4, CPU_R5000 },
13553 { "rm5261", 0, ISA_MIPS4, CPU_R5000 },
13554 { "rm5721", 0, ISA_MIPS4, CPU_R5000 },
13555 { "r7000", 0, ISA_MIPS4, CPU_R5000 },
13556
13557 /* MIPS 32 */
13558 { "4kc", 0, ISA_MIPS32, CPU_MIPS32, },
13559 { "4km", 0, ISA_MIPS32, CPU_MIPS32 },
13560 { "4kp", 0, ISA_MIPS32, CPU_MIPS32 },
e7af610e 13561
316f5878
RS
13562 /* MIPS 64 */
13563 { "5kc", 0, ISA_MIPS64, CPU_MIPS64 },
13564 { "20kc", 0, ISA_MIPS64, CPU_MIPS64 },
e7af610e 13565
c7a23324 13566 /* Broadcom SB-1 CPU core */
316f5878 13567 { "sb1", 0, ISA_MIPS64, CPU_SB1 },
e7af610e 13568
316f5878
RS
13569 /* End marker */
13570 { NULL, 0, 0, 0 }
13571};
e7af610e 13572
84ea6cf2 13573
316f5878
RS
13574/* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
13575 with a final "000" replaced by "k". Ignore case.
e7af610e 13576
316f5878 13577 Note: this function is shared between GCC and GAS. */
c6c98b38 13578
316f5878
RS
13579static boolean
13580mips_strict_matching_cpu_name_p (canonical, given)
13581 const char *canonical, *given;
13582{
13583 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
13584 given++, canonical++;
13585
13586 return ((*given == 0 && *canonical == 0)
13587 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
13588}
13589
13590
13591/* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
13592 CPU name. We've traditionally allowed a lot of variation here.
13593
13594 Note: this function is shared between GCC and GAS. */
13595
13596static boolean
13597mips_matching_cpu_name_p (canonical, given)
13598 const char *canonical, *given;
13599{
13600 /* First see if the name matches exactly, or with a final "000"
13601 turned into "k". */
13602 if (mips_strict_matching_cpu_name_p (canonical, given))
13603 return true;
13604
13605 /* If not, try comparing based on numerical designation alone.
13606 See if GIVEN is an unadorned number, or 'r' followed by a number. */
13607 if (TOLOWER (*given) == 'r')
13608 given++;
13609 if (!ISDIGIT (*given))
13610 return false;
13611
13612 /* Skip over some well-known prefixes in the canonical name,
13613 hoping to find a number there too. */
13614 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
13615 canonical += 2;
13616 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
13617 canonical += 2;
13618 else if (TOLOWER (canonical[0]) == 'r')
13619 canonical += 1;
13620
13621 return mips_strict_matching_cpu_name_p (canonical, given);
13622}
13623
13624
13625/* Parse an option that takes the name of a processor as its argument.
13626 OPTION is the name of the option and CPU_STRING is the argument.
13627 Return the corresponding processor enumeration if the CPU_STRING is
13628 recognized, otherwise report an error and return null.
13629
13630 A similar function exists in GCC. */
e7af610e
NC
13631
13632static const struct mips_cpu_info *
316f5878
RS
13633mips_parse_cpu (option, cpu_string)
13634 const char *option, *cpu_string;
e7af610e 13635{
316f5878 13636 const struct mips_cpu_info *p;
e7af610e 13637
316f5878
RS
13638 /* 'from-abi' selects the most compatible architecture for the given
13639 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
13640 EABIs, we have to decide whether we're using the 32-bit or 64-bit
13641 version. Look first at the -mgp options, if given, otherwise base
13642 the choice on MIPS_DEFAULT_64BIT.
e7af610e 13643
316f5878
RS
13644 Treat NO_ABI like the EABIs. One reason to do this is that the
13645 plain 'mips' and 'mips64' configs have 'from-abi' as their default
13646 architecture. This code picks MIPS I for 'mips' and MIPS III for
13647 'mips64', just as we did in the days before 'from-abi'. */
13648 if (strcasecmp (cpu_string, "from-abi") == 0)
13649 {
13650 if (ABI_NEEDS_32BIT_REGS (mips_abi))
13651 return mips_cpu_info_from_isa (ISA_MIPS1);
13652
13653 if (ABI_NEEDS_64BIT_REGS (mips_abi))
13654 return mips_cpu_info_from_isa (ISA_MIPS3);
13655
13656 if (file_mips_gp32 >= 0)
13657 return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
13658
13659 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
13660 ? ISA_MIPS3
13661 : ISA_MIPS1);
13662 }
13663
13664 /* 'default' has traditionally been a no-op. Probably not very useful. */
13665 if (strcasecmp (cpu_string, "default") == 0)
13666 return 0;
13667
13668 for (p = mips_cpu_info_table; p->name != 0; p++)
13669 if (mips_matching_cpu_name_p (p->name, cpu_string))
13670 return p;
13671
13672 as_bad ("Bad value (%s) for %s", cpu_string, option);
13673 return 0;
e7af610e
NC
13674}
13675
316f5878
RS
13676/* Return the canonical processor information for ISA (a member of the
13677 ISA_MIPS* enumeration). */
13678
e7af610e
NC
13679static const struct mips_cpu_info *
13680mips_cpu_info_from_isa (isa)
13681 int isa;
13682{
13683 int i;
13684
13685 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
13686 if (mips_cpu_info_table[i].is_isa
316f5878 13687 && isa == mips_cpu_info_table[i].isa)
e7af610e
NC
13688 return (&mips_cpu_info_table[i]);
13689
e972090a 13690 return NULL;
e7af610e 13691}
316f5878
RS
13692\f
13693static void
13694show (stream, string, col_p, first_p)
13695 FILE *stream;
13696 const char *string;
13697 int *col_p;
13698 int *first_p;
13699{
13700 if (*first_p)
13701 {
13702 fprintf (stream, "%24s", "");
13703 *col_p = 24;
13704 }
13705 else
13706 {
13707 fprintf (stream, ", ");
13708 *col_p += 2;
13709 }
e7af610e 13710
316f5878
RS
13711 if (*col_p + strlen (string) > 72)
13712 {
13713 fprintf (stream, "\n%24s", "");
13714 *col_p = 24;
13715 }
13716
13717 fprintf (stream, "%s", string);
13718 *col_p += strlen (string);
13719
13720 *first_p = 0;
13721}
13722
13723void
13724md_show_usage (stream)
13725 FILE *stream;
e7af610e 13726{
316f5878
RS
13727 int column, first;
13728 size_t i;
13729
13730 fprintf (stream, _("\
13731MIPS options:\n\
13732-membedded-pic generate embedded position independent code\n\
13733-EB generate big endian output\n\
13734-EL generate little endian output\n\
13735-g, -g2 do not remove unneeded NOPs or swap branches\n\
13736-G NUM allow referencing objects up to NUM bytes\n\
13737 implicitly with the gp register [default 8]\n"));
13738 fprintf (stream, _("\
13739-mips1 generate MIPS ISA I instructions\n\
13740-mips2 generate MIPS ISA II instructions\n\
13741-mips3 generate MIPS ISA III instructions\n\
13742-mips4 generate MIPS ISA IV instructions\n\
13743-mips5 generate MIPS ISA V instructions\n\
13744-mips32 generate MIPS32 ISA instructions\n\
13745-mips64 generate MIPS64 ISA instructions\n\
13746-march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
13747
13748 first = 1;
e7af610e
NC
13749
13750 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
316f5878
RS
13751 show (stream, mips_cpu_info_table[i].name, &column, &first);
13752 show (stream, "from-abi", &column, &first);
13753 fputc ('\n', stream);
e7af610e 13754
316f5878
RS
13755 fprintf (stream, _("\
13756-mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
13757-no-mCPU don't generate code specific to CPU.\n\
13758 For -mCPU and -no-mCPU, CPU must be one of:\n"));
13759
13760 first = 1;
13761
13762 show (stream, "3900", &column, &first);
13763 show (stream, "4010", &column, &first);
13764 show (stream, "4100", &column, &first);
13765 show (stream, "4650", &column, &first);
13766 fputc ('\n', stream);
13767
13768 fprintf (stream, _("\
13769-mips16 generate mips16 instructions\n\
13770-no-mips16 do not generate mips16 instructions\n"));
13771 fprintf (stream, _("\
13772-mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
13773-mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
13774-O0 remove unneeded NOPs, do not swap branches\n\
13775-O remove unneeded NOPs and swap branches\n\
13776-n warn about NOPs generated from macros\n\
13777--[no-]construct-floats [dis]allow floating point values to be constructed\n\
13778--trap, --no-break trap exception on div by 0 and mult overflow\n\
13779--break, --no-trap break exception on div by 0 and mult overflow\n"));
13780#ifdef OBJ_ELF
13781 fprintf (stream, _("\
13782-KPIC, -call_shared generate SVR4 position independent code\n\
13783-non_shared do not generate position independent code\n\
13784-xgot assume a 32 bit GOT\n\
13785-mabi=ABI create ABI conformant object file for:\n"));
13786
13787 first = 1;
13788
13789 show (stream, "32", &column, &first);
13790 show (stream, "o64", &column, &first);
13791 show (stream, "n32", &column, &first);
13792 show (stream, "64", &column, &first);
13793 show (stream, "eabi", &column, &first);
13794
13795 fputc ('\n', stream);
13796
13797 fprintf (stream, _("\
13798-32 create o32 ABI object file (default)\n\
13799-n32 create n32 ABI object file\n\
13800-64 create 64 ABI object file\n"));
13801#endif
e7af610e 13802}
This page took 2.386738 seconds and 4 git commands to generate.